test.c 1.5 MB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180221812218222183221842218522186221872218822189221902219122192221932219422195221962219722198221992220022201222022220322204222052220622207222082220922210222112221222213222142221522216222172221822219222202222122222222232222422225222262222722228222292223022231222322223322234222352223622237222382223922240222412224222243222442224522246222472224822249222502225122252222532225422255222562225722258222592226022261222622226322264222652226622267222682226922270222712227222273222742227522276222772227822279222802228122282222832228422285222862228722288222892229022291222922229322294222952229622297222982229922300223012230222303223042230522306223072230822309223102231122312223132231422315223162231722318223192232022321223222232322324223252232622327223282232922330223312233222333223342233522336223372233822339223402234122342223432234422345223462234722348223492235022351223522235322354223552235622357223582235922360223612236222363223642236522366223672236822369223702237122372223732237422375223762237722378223792238022381223822238322384223852238622387223882238922390223912239222393223942239522396223972239822399224002240122402224032240422405224062240722408224092241022411224122241322414224152241622417224182241922420224212242222423224242242522426224272242822429224302243122432224332243422435224362243722438224392244022441224422244322444224452244622447224482244922450224512245222453224542245522456224572245822459224602246122462224632246422465224662246722468224692247022471224722247322474224752247622477224782247922480224812248222483224842248522486224872248822489224902249122492224932249422495224962249722498224992250022501225022250322504225052250622507225082250922510225112251222513225142251522516225172251822519225202252122522225232252422525225262252722528225292253022531225322253322534225352253622537225382253922540225412254222543225442254522546225472254822549225502255122552225532255422555225562255722558225592256022561225622256322564225652256622567225682256922570225712257222573225742257522576225772257822579225802258122582225832258422585225862258722588225892259022591225922259322594225952259622597225982259922600226012260222603226042260522606226072260822609226102261122612226132261422615226162261722618226192262022621226222262322624226252262622627226282262922630226312263222633226342263522636226372263822639226402264122642226432264422645226462264722648226492265022651226522265322654226552265622657226582265922660226612266222663226642266522666226672266822669226702267122672226732267422675226762267722678226792268022681226822268322684226852268622687226882268922690226912269222693226942269522696226972269822699227002270122702227032270422705227062270722708227092271022711227122271322714227152271622717227182271922720227212272222723227242272522726227272272822729227302273122732227332273422735227362273722738227392274022741227422274322744227452274622747227482274922750227512275222753227542275522756227572275822759227602276122762227632276422765227662276722768227692277022771227722277322774227752277622777227782277922780227812278222783227842278522786227872278822789227902279122792227932279422795227962279722798227992280022801228022280322804228052280622807228082280922810228112281222813228142281522816228172281822819228202282122822228232282422825228262282722828228292283022831228322283322834228352283622837228382283922840228412284222843228442284522846228472284822849228502285122852228532285422855228562285722858228592286022861228622286322864228652286622867228682286922870228712287222873228742287522876228772287822879228802288122882228832288422885228862288722888228892289022891228922289322894228952289622897228982289922900229012290222903229042290522906229072290822909229102291122912229132291422915229162291722918229192292022921229222292322924229252292622927229282292922930229312293222933229342293522936229372293822939229402294122942229432294422945229462294722948229492295022951229522295322954229552295622957229582295922960229612296222963229642296522966229672296822969229702297122972229732297422975229762297722978229792298022981229822298322984229852298622987229882298922990229912299222993229942299522996229972299822999230002300123002230032300423005230062300723008230092301023011230122301323014230152301623017230182301923020230212302223023230242302523026230272302823029230302303123032230332303423035230362303723038230392304023041230422304323044230452304623047230482304923050230512305223053230542305523056230572305823059230602306123062230632306423065230662306723068230692307023071230722307323074230752307623077230782307923080230812308223083230842308523086230872308823089230902309123092230932309423095230962309723098230992310023101231022310323104231052310623107231082310923110231112311223113231142311523116231172311823119231202312123122231232312423125231262312723128231292313023131231322313323134231352313623137231382313923140231412314223143231442314523146231472314823149231502315123152231532315423155231562315723158231592316023161231622316323164231652316623167231682316923170231712317223173231742317523176231772317823179231802318123182231832318423185231862318723188231892319023191231922319323194231952319623197231982319923200232012320223203232042320523206232072320823209232102321123212232132321423215232162321723218232192322023221232222322323224232252322623227232282322923230232312323223233232342323523236232372323823239232402324123242232432324423245232462324723248232492325023251232522325323254232552325623257232582325923260232612326223263232642326523266232672326823269232702327123272232732327423275232762327723278232792328023281232822328323284232852328623287232882328923290232912329223293232942329523296232972329823299233002330123302233032330423305233062330723308233092331023311233122331323314233152331623317233182331923320233212332223323233242332523326233272332823329233302333123332233332333423335233362333723338233392334023341233422334323344233452334623347233482334923350233512335223353233542335523356233572335823359233602336123362233632336423365233662336723368233692337023371233722337323374233752337623377233782337923380233812338223383233842338523386233872338823389233902339123392233932339423395233962339723398233992340023401234022340323404234052340623407234082340923410234112341223413234142341523416234172341823419234202342123422234232342423425234262342723428234292343023431234322343323434234352343623437234382343923440234412344223443234442344523446234472344823449234502345123452234532345423455234562345723458234592346023461234622346323464234652346623467234682346923470234712347223473234742347523476234772347823479234802348123482234832348423485234862348723488234892349023491234922349323494234952349623497234982349923500235012350223503235042350523506235072350823509235102351123512235132351423515235162351723518235192352023521235222352323524235252352623527235282352923530235312353223533235342353523536235372353823539235402354123542235432354423545235462354723548235492355023551235522355323554235552355623557235582355923560235612356223563235642356523566235672356823569235702357123572235732357423575235762357723578235792358023581235822358323584235852358623587235882358923590235912359223593235942359523596235972359823599236002360123602236032360423605236062360723608236092361023611236122361323614236152361623617236182361923620236212362223623236242362523626236272362823629236302363123632236332363423635236362363723638236392364023641236422364323644236452364623647236482364923650236512365223653236542365523656236572365823659236602366123662236632366423665236662366723668236692367023671236722367323674236752367623677236782367923680236812368223683236842368523686236872368823689236902369123692236932369423695236962369723698236992370023701237022370323704237052370623707237082370923710237112371223713237142371523716237172371823719237202372123722237232372423725237262372723728237292373023731237322373323734237352373623737237382373923740237412374223743237442374523746237472374823749237502375123752237532375423755237562375723758237592376023761237622376323764237652376623767237682376923770237712377223773237742377523776237772377823779237802378123782237832378423785237862378723788237892379023791237922379323794237952379623797237982379923800238012380223803238042380523806238072380823809238102381123812238132381423815238162381723818238192382023821238222382323824238252382623827238282382923830238312383223833238342383523836238372383823839238402384123842238432384423845238462384723848238492385023851238522385323854238552385623857238582385923860238612386223863238642386523866238672386823869238702387123872238732387423875238762387723878238792388023881238822388323884238852388623887238882388923890238912389223893238942389523896238972389823899239002390123902239032390423905239062390723908239092391023911239122391323914239152391623917239182391923920239212392223923239242392523926239272392823929239302393123932239332393423935239362393723938239392394023941239422394323944239452394623947239482394923950239512395223953239542395523956239572395823959239602396123962239632396423965239662396723968239692397023971239722397323974239752397623977239782397923980239812398223983239842398523986239872398823989239902399123992239932399423995239962399723998239992400024001240022400324004240052400624007240082400924010240112401224013240142401524016240172401824019240202402124022240232402424025240262402724028240292403024031240322403324034240352403624037240382403924040240412404224043240442404524046240472404824049240502405124052240532405424055240562405724058240592406024061240622406324064240652406624067240682406924070240712407224073240742407524076240772407824079240802408124082240832408424085240862408724088240892409024091240922409324094240952409624097240982409924100241012410224103241042410524106241072410824109241102411124112241132411424115241162411724118241192412024121241222412324124241252412624127241282412924130241312413224133241342413524136241372413824139241402414124142241432414424145241462414724148241492415024151241522415324154241552415624157241582415924160241612416224163241642416524166241672416824169241702417124172241732417424175241762417724178241792418024181241822418324184241852418624187241882418924190241912419224193241942419524196241972419824199242002420124202242032420424205242062420724208242092421024211242122421324214242152421624217242182421924220242212422224223242242422524226242272422824229242302423124232242332423424235242362423724238242392424024241242422424324244242452424624247242482424924250242512425224253242542425524256242572425824259242602426124262242632426424265242662426724268242692427024271242722427324274242752427624277242782427924280242812428224283242842428524286242872428824289242902429124292242932429424295242962429724298242992430024301243022430324304243052430624307243082430924310243112431224313243142431524316243172431824319243202432124322243232432424325243262432724328243292433024331243322433324334243352433624337243382433924340243412434224343243442434524346243472434824349243502435124352243532435424355243562435724358243592436024361243622436324364243652436624367243682436924370243712437224373243742437524376243772437824379243802438124382243832438424385243862438724388243892439024391243922439324394243952439624397243982439924400244012440224403244042440524406244072440824409244102441124412244132441424415244162441724418244192442024421244222442324424244252442624427244282442924430244312443224433244342443524436244372443824439244402444124442244432444424445244462444724448244492445024451244522445324454244552445624457244582445924460244612446224463244642446524466244672446824469244702447124472244732447424475244762447724478244792448024481244822448324484244852448624487244882448924490244912449224493244942449524496244972449824499245002450124502245032450424505245062450724508245092451024511245122451324514245152451624517245182451924520245212452224523245242452524526245272452824529245302453124532245332453424535245362453724538245392454024541245422454324544245452454624547245482454924550245512455224553245542455524556245572455824559245602456124562245632456424565245662456724568245692457024571245722457324574245752457624577245782457924580245812458224583245842458524586245872458824589245902459124592245932459424595245962459724598245992460024601246022460324604246052460624607246082460924610246112461224613246142461524616246172461824619246202462124622246232462424625246262462724628246292463024631246322463324634246352463624637246382463924640246412464224643246442464524646246472464824649246502465124652246532465424655246562465724658246592466024661246622466324664246652466624667246682466924670246712467224673246742467524676246772467824679246802468124682246832468424685246862468724688246892469024691246922469324694246952469624697246982469924700247012470224703247042470524706247072470824709247102471124712247132471424715247162471724718247192472024721247222472324724247252472624727247282472924730247312473224733247342473524736247372473824739247402474124742247432474424745247462474724748247492475024751247522475324754247552475624757247582475924760247612476224763247642476524766247672476824769247702477124772247732477424775247762477724778247792478024781247822478324784247852478624787247882478924790247912479224793247942479524796247972479824799248002480124802248032480424805248062480724808248092481024811248122481324814248152481624817248182481924820248212482224823248242482524826248272482824829248302483124832248332483424835248362483724838248392484024841248422484324844248452484624847248482484924850248512485224853248542485524856248572485824859248602486124862248632486424865248662486724868248692487024871248722487324874248752487624877248782487924880248812488224883248842488524886248872488824889248902489124892248932489424895248962489724898248992490024901249022490324904249052490624907249082490924910249112491224913249142491524916249172491824919249202492124922249232492424925249262492724928249292493024931249322493324934249352493624937249382493924940249412494224943249442494524946249472494824949249502495124952249532495424955249562495724958249592496024961249622496324964249652496624967249682496924970249712497224973249742497524976249772497824979249802498124982249832498424985249862498724988249892499024991249922499324994249952499624997249982499925000250012500225003250042500525006250072500825009250102501125012250132501425015250162501725018250192502025021250222502325024250252502625027250282502925030250312503225033250342503525036250372503825039250402504125042250432504425045250462504725048250492505025051250522505325054250552505625057250582505925060250612506225063250642506525066250672506825069250702507125072250732507425075250762507725078250792508025081250822508325084250852508625087250882508925090250912509225093250942509525096250972509825099251002510125102251032510425105251062510725108251092511025111251122511325114251152511625117251182511925120251212512225123251242512525126251272512825129251302513125132251332513425135251362513725138251392514025141251422514325144251452514625147251482514925150251512515225153251542515525156251572515825159251602516125162251632516425165251662516725168251692517025171251722517325174251752517625177251782517925180251812518225183251842518525186251872518825189251902519125192251932519425195251962519725198251992520025201252022520325204252052520625207252082520925210252112521225213252142521525216252172521825219252202522125222252232522425225252262522725228252292523025231252322523325234252352523625237252382523925240252412524225243252442524525246252472524825249252502525125252252532525425255252562525725258252592526025261252622526325264252652526625267252682526925270252712527225273252742527525276252772527825279252802528125282252832528425285252862528725288252892529025291252922529325294252952529625297252982529925300253012530225303253042530525306253072530825309253102531125312253132531425315253162531725318253192532025321253222532325324253252532625327253282532925330253312533225333253342533525336253372533825339253402534125342253432534425345253462534725348253492535025351253522535325354253552535625357253582535925360253612536225363253642536525366253672536825369253702537125372253732537425375253762537725378253792538025381253822538325384253852538625387253882538925390253912539225393253942539525396253972539825399254002540125402254032540425405254062540725408254092541025411254122541325414254152541625417254182541925420254212542225423254242542525426254272542825429254302543125432254332543425435254362543725438254392544025441254422544325444254452544625447254482544925450254512545225453254542545525456254572545825459254602546125462254632546425465254662546725468254692547025471254722547325474254752547625477254782547925480254812548225483254842548525486254872548825489254902549125492254932549425495254962549725498254992550025501255022550325504255052550625507255082550925510255112551225513255142551525516255172551825519255202552125522255232552425525255262552725528255292553025531255322553325534255352553625537255382553925540255412554225543255442554525546255472554825549255502555125552255532555425555255562555725558255592556025561255622556325564255652556625567255682556925570255712557225573255742557525576255772557825579255802558125582255832558425585255862558725588255892559025591255922559325594255952559625597255982559925600256012560225603256042560525606256072560825609256102561125612256132561425615256162561725618256192562025621256222562325624256252562625627256282562925630256312563225633256342563525636256372563825639256402564125642256432564425645256462564725648256492565025651256522565325654256552565625657256582565925660256612566225663256642566525666256672566825669256702567125672256732567425675256762567725678256792568025681256822568325684256852568625687256882568925690256912569225693256942569525696256972569825699257002570125702257032570425705257062570725708257092571025711257122571325714257152571625717257182571925720257212572225723257242572525726257272572825729257302573125732257332573425735257362573725738257392574025741257422574325744257452574625747257482574925750257512575225753257542575525756257572575825759257602576125762257632576425765257662576725768257692577025771257722577325774257752577625777257782577925780257812578225783257842578525786257872578825789257902579125792257932579425795257962579725798257992580025801258022580325804258052580625807258082580925810258112581225813258142581525816258172581825819258202582125822258232582425825258262582725828258292583025831258322583325834258352583625837258382583925840258412584225843258442584525846258472584825849258502585125852258532585425855258562585725858258592586025861258622586325864258652586625867258682586925870258712587225873258742587525876258772587825879258802588125882258832588425885258862588725888258892589025891258922589325894258952589625897258982589925900259012590225903259042590525906259072590825909259102591125912259132591425915259162591725918259192592025921259222592325924259252592625927259282592925930259312593225933259342593525936259372593825939259402594125942259432594425945259462594725948259492595025951259522595325954259552595625957259582595925960259612596225963259642596525966259672596825969259702597125972259732597425975259762597725978259792598025981259822598325984259852598625987259882598925990259912599225993259942599525996259972599825999260002600126002260032600426005260062600726008260092601026011260122601326014260152601626017260182601926020260212602226023260242602526026260272602826029260302603126032260332603426035260362603726038260392604026041260422604326044260452604626047260482604926050260512605226053260542605526056260572605826059260602606126062260632606426065260662606726068260692607026071260722607326074260752607626077260782607926080260812608226083260842608526086260872608826089260902609126092260932609426095260962609726098260992610026101261022610326104261052610626107261082610926110261112611226113261142611526116261172611826119261202612126122261232612426125261262612726128261292613026131261322613326134261352613626137261382613926140261412614226143261442614526146261472614826149261502615126152261532615426155261562615726158261592616026161261622616326164261652616626167261682616926170261712617226173261742617526176261772617826179261802618126182261832618426185261862618726188261892619026191261922619326194261952619626197261982619926200262012620226203262042620526206262072620826209262102621126212262132621426215262162621726218262192622026221262222622326224262252622626227262282622926230262312623226233262342623526236262372623826239262402624126242262432624426245262462624726248262492625026251262522625326254262552625626257262582625926260262612626226263262642626526266262672626826269262702627126272262732627426275262762627726278262792628026281262822628326284262852628626287262882628926290262912629226293262942629526296262972629826299263002630126302263032630426305263062630726308263092631026311263122631326314263152631626317263182631926320263212632226323263242632526326263272632826329263302633126332263332633426335263362633726338263392634026341263422634326344263452634626347263482634926350263512635226353263542635526356263572635826359263602636126362263632636426365263662636726368263692637026371263722637326374263752637626377263782637926380263812638226383263842638526386263872638826389263902639126392263932639426395263962639726398263992640026401264022640326404264052640626407264082640926410264112641226413264142641526416264172641826419264202642126422264232642426425264262642726428264292643026431264322643326434264352643626437264382643926440264412644226443264442644526446264472644826449264502645126452264532645426455264562645726458264592646026461264622646326464264652646626467264682646926470264712647226473264742647526476264772647826479264802648126482264832648426485264862648726488264892649026491264922649326494264952649626497264982649926500265012650226503265042650526506265072650826509265102651126512265132651426515265162651726518265192652026521265222652326524265252652626527265282652926530265312653226533265342653526536265372653826539265402654126542265432654426545265462654726548265492655026551265522655326554265552655626557265582655926560265612656226563265642656526566265672656826569265702657126572265732657426575265762657726578265792658026581265822658326584265852658626587265882658926590265912659226593265942659526596265972659826599266002660126602266032660426605266062660726608266092661026611266122661326614266152661626617266182661926620266212662226623266242662526626266272662826629266302663126632266332663426635266362663726638266392664026641266422664326644266452664626647266482664926650266512665226653266542665526656266572665826659266602666126662266632666426665266662666726668266692667026671266722667326674266752667626677266782667926680266812668226683266842668526686266872668826689266902669126692266932669426695266962669726698266992670026701267022670326704267052670626707267082670926710267112671226713267142671526716267172671826719267202672126722267232672426725267262672726728267292673026731267322673326734267352673626737267382673926740267412674226743267442674526746267472674826749267502675126752267532675426755267562675726758267592676026761267622676326764267652676626767267682676926770267712677226773267742677526776267772677826779267802678126782267832678426785267862678726788267892679026791267922679326794267952679626797267982679926800268012680226803268042680526806268072680826809268102681126812268132681426815268162681726818268192682026821268222682326824268252682626827268282682926830268312683226833268342683526836268372683826839268402684126842268432684426845268462684726848268492685026851268522685326854268552685626857268582685926860268612686226863268642686526866268672686826869268702687126872268732687426875268762687726878268792688026881268822688326884268852688626887268882688926890268912689226893268942689526896268972689826899269002690126902269032690426905269062690726908269092691026911269122691326914269152691626917269182691926920269212692226923269242692526926269272692826929269302693126932269332693426935269362693726938269392694026941269422694326944269452694626947269482694926950269512695226953269542695526956269572695826959269602696126962269632696426965269662696726968269692697026971269722697326974269752697626977269782697926980269812698226983269842698526986269872698826989269902699126992269932699426995269962699726998269992700027001270022700327004270052700627007270082700927010270112701227013270142701527016270172701827019270202702127022270232702427025270262702727028270292703027031270322703327034270352703627037270382703927040270412704227043270442704527046270472704827049270502705127052270532705427055270562705727058270592706027061270622706327064270652706627067270682706927070270712707227073270742707527076270772707827079270802708127082270832708427085270862708727088270892709027091270922709327094270952709627097270982709927100271012710227103271042710527106271072710827109271102711127112271132711427115271162711727118271192712027121271222712327124271252712627127271282712927130271312713227133271342713527136271372713827139271402714127142271432714427145271462714727148271492715027151271522715327154271552715627157271582715927160271612716227163271642716527166271672716827169271702717127172271732717427175271762717727178271792718027181271822718327184271852718627187271882718927190271912719227193271942719527196271972719827199272002720127202272032720427205272062720727208272092721027211272122721327214272152721627217272182721927220272212722227223272242722527226272272722827229272302723127232272332723427235272362723727238272392724027241272422724327244272452724627247272482724927250272512725227253272542725527256272572725827259272602726127262272632726427265272662726727268272692727027271272722727327274272752727627277272782727927280272812728227283272842728527286272872728827289272902729127292272932729427295272962729727298272992730027301273022730327304273052730627307273082730927310273112731227313273142731527316273172731827319273202732127322273232732427325273262732727328273292733027331273322733327334273352733627337273382733927340273412734227343273442734527346273472734827349273502735127352273532735427355273562735727358273592736027361273622736327364273652736627367273682736927370273712737227373273742737527376273772737827379273802738127382273832738427385273862738727388273892739027391273922739327394273952739627397273982739927400274012740227403274042740527406274072740827409274102741127412274132741427415274162741727418274192742027421274222742327424274252742627427274282742927430274312743227433274342743527436274372743827439274402744127442274432744427445274462744727448274492745027451274522745327454274552745627457274582745927460274612746227463274642746527466274672746827469274702747127472274732747427475274762747727478274792748027481274822748327484274852748627487274882748927490274912749227493274942749527496274972749827499275002750127502275032750427505275062750727508275092751027511275122751327514275152751627517275182751927520275212752227523275242752527526275272752827529275302753127532275332753427535275362753727538275392754027541275422754327544275452754627547275482754927550275512755227553275542755527556275572755827559275602756127562275632756427565275662756727568275692757027571275722757327574275752757627577275782757927580275812758227583275842758527586275872758827589275902759127592275932759427595275962759727598275992760027601276022760327604276052760627607276082760927610276112761227613276142761527616276172761827619276202762127622276232762427625276262762727628276292763027631276322763327634276352763627637276382763927640276412764227643276442764527646276472764827649276502765127652276532765427655276562765727658276592766027661276622766327664276652766627667276682766927670276712767227673276742767527676276772767827679276802768127682276832768427685276862768727688276892769027691276922769327694276952769627697276982769927700277012770227703277042770527706277072770827709277102771127712277132771427715277162771727718277192772027721277222772327724277252772627727277282772927730277312773227733277342773527736277372773827739277402774127742277432774427745277462774727748277492775027751277522775327754277552775627757277582775927760277612776227763277642776527766277672776827769277702777127772277732777427775277762777727778277792778027781277822778327784277852778627787277882778927790277912779227793277942779527796277972779827799278002780127802278032780427805278062780727808278092781027811278122781327814278152781627817278182781927820278212782227823278242782527826278272782827829278302783127832278332783427835278362783727838278392784027841278422784327844278452784627847278482784927850278512785227853278542785527856278572785827859278602786127862278632786427865278662786727868278692787027871278722787327874278752787627877278782787927880278812788227883278842788527886278872788827889278902789127892278932789427895278962789727898278992790027901279022790327904279052790627907279082790927910279112791227913279142791527916279172791827919279202792127922279232792427925279262792727928279292793027931279322793327934279352793627937279382793927940279412794227943279442794527946279472794827949279502795127952279532795427955279562795727958279592796027961279622796327964279652796627967279682796927970279712797227973279742797527976279772797827979279802798127982279832798427985279862798727988279892799027991279922799327994279952799627997279982799928000280012800228003280042800528006280072800828009280102801128012280132801428015280162801728018280192802028021280222802328024280252802628027280282802928030280312803228033280342803528036280372803828039280402804128042280432804428045280462804728048280492805028051280522805328054280552805628057280582805928060280612806228063280642806528066280672806828069280702807128072280732807428075280762807728078280792808028081280822808328084280852808628087280882808928090280912809228093280942809528096280972809828099281002810128102281032810428105281062810728108281092811028111281122811328114281152811628117281182811928120281212812228123281242812528126281272812828129281302813128132281332813428135281362813728138281392814028141281422814328144281452814628147281482814928150281512815228153281542815528156281572815828159281602816128162281632816428165281662816728168281692817028171281722817328174281752817628177281782817928180281812818228183281842818528186281872818828189281902819128192281932819428195281962819728198281992820028201282022820328204282052820628207282082820928210282112821228213282142821528216282172821828219282202822128222282232822428225282262822728228282292823028231282322823328234282352823628237282382823928240282412824228243282442824528246282472824828249282502825128252282532825428255282562825728258282592826028261282622826328264282652826628267282682826928270282712827228273282742827528276282772827828279282802828128282282832828428285282862828728288282892829028291282922829328294282952829628297282982829928300283012830228303283042830528306283072830828309283102831128312283132831428315283162831728318283192832028321283222832328324283252832628327283282832928330283312833228333283342833528336283372833828339283402834128342283432834428345283462834728348283492835028351283522835328354283552835628357283582835928360283612836228363283642836528366283672836828369283702837128372283732837428375283762837728378283792838028381283822838328384283852838628387283882838928390283912839228393283942839528396283972839828399284002840128402284032840428405284062840728408284092841028411284122841328414284152841628417284182841928420284212842228423284242842528426284272842828429284302843128432284332843428435284362843728438284392844028441284422844328444284452844628447284482844928450284512845228453284542845528456284572845828459284602846128462284632846428465284662846728468284692847028471284722847328474284752847628477284782847928480284812848228483284842848528486284872848828489284902849128492284932849428495284962849728498284992850028501285022850328504285052850628507285082850928510285112851228513285142851528516285172851828519285202852128522285232852428525285262852728528285292853028531285322853328534285352853628537285382853928540285412854228543285442854528546285472854828549285502855128552285532855428555285562855728558285592856028561285622856328564285652856628567285682856928570285712857228573285742857528576285772857828579285802858128582285832858428585285862858728588285892859028591285922859328594285952859628597285982859928600286012860228603286042860528606286072860828609286102861128612286132861428615286162861728618286192862028621286222862328624286252862628627286282862928630286312863228633286342863528636286372863828639286402864128642286432864428645286462864728648286492865028651286522865328654286552865628657286582865928660286612866228663286642866528666286672866828669286702867128672286732867428675286762867728678286792868028681286822868328684286852868628687286882868928690286912869228693286942869528696286972869828699287002870128702287032870428705287062870728708287092871028711287122871328714287152871628717287182871928720287212872228723287242872528726287272872828729287302873128732287332873428735287362873728738287392874028741287422874328744287452874628747287482874928750287512875228753287542875528756287572875828759287602876128762287632876428765287662876728768287692877028771287722877328774287752877628777287782877928780287812878228783287842878528786287872878828789287902879128792287932879428795287962879728798287992880028801288022880328804288052880628807288082880928810288112881228813288142881528816288172881828819288202882128822288232882428825288262882728828288292883028831288322883328834288352883628837288382883928840288412884228843288442884528846288472884828849288502885128852288532885428855288562885728858288592886028861288622886328864288652886628867288682886928870288712887228873288742887528876288772887828879288802888128882288832888428885288862888728888288892889028891288922889328894288952889628897288982889928900289012890228903289042890528906289072890828909289102891128912289132891428915289162891728918289192892028921289222892328924289252892628927289282892928930289312893228933289342893528936289372893828939289402894128942289432894428945289462894728948289492895028951289522895328954289552895628957289582895928960289612896228963289642896528966289672896828969289702897128972289732897428975289762897728978289792898028981289822898328984289852898628987289882898928990289912899228993289942899528996289972899828999290002900129002290032900429005290062900729008290092901029011290122901329014290152901629017290182901929020290212902229023290242902529026290272902829029290302903129032290332903429035290362903729038290392904029041290422904329044290452904629047290482904929050290512905229053290542905529056290572905829059290602906129062290632906429065290662906729068290692907029071290722907329074290752907629077290782907929080290812908229083290842908529086290872908829089290902909129092290932909429095290962909729098290992910029101291022910329104291052910629107291082910929110291112911229113291142911529116291172911829119291202912129122291232912429125291262912729128291292913029131291322913329134291352913629137291382913929140291412914229143291442914529146291472914829149291502915129152291532915429155291562915729158291592916029161291622916329164291652916629167291682916929170291712917229173291742917529176291772917829179291802918129182291832918429185291862918729188291892919029191291922919329194291952919629197291982919929200292012920229203292042920529206292072920829209292102921129212292132921429215292162921729218292192922029221292222922329224292252922629227292282922929230292312923229233292342923529236292372923829239292402924129242292432924429245292462924729248292492925029251292522925329254292552925629257292582925929260292612926229263292642926529266292672926829269292702927129272292732927429275292762927729278292792928029281292822928329284292852928629287292882928929290292912929229293292942929529296292972929829299293002930129302293032930429305293062930729308293092931029311293122931329314293152931629317293182931929320293212932229323293242932529326293272932829329293302933129332293332933429335293362933729338293392934029341293422934329344293452934629347293482934929350293512935229353293542935529356293572935829359293602936129362293632936429365293662936729368293692937029371293722937329374293752937629377293782937929380293812938229383293842938529386293872938829389293902939129392293932939429395293962939729398293992940029401294022940329404294052940629407294082940929410294112941229413294142941529416294172941829419294202942129422294232942429425294262942729428294292943029431294322943329434294352943629437294382943929440294412944229443294442944529446294472944829449294502945129452294532945429455294562945729458294592946029461294622946329464294652946629467294682946929470294712947229473294742947529476294772947829479294802948129482294832948429485294862948729488294892949029491294922949329494294952949629497294982949929500295012950229503295042950529506295072950829509295102951129512295132951429515295162951729518295192952029521295222952329524295252952629527295282952929530295312953229533295342953529536295372953829539295402954129542295432954429545295462954729548295492955029551295522955329554295552955629557295582955929560295612956229563295642956529566295672956829569295702957129572295732957429575295762957729578295792958029581295822958329584295852958629587295882958929590295912959229593295942959529596295972959829599296002960129602296032960429605296062960729608296092961029611296122961329614296152961629617296182961929620296212962229623296242962529626296272962829629296302963129632296332963429635296362963729638296392964029641296422964329644296452964629647296482964929650296512965229653296542965529656296572965829659296602966129662296632966429665296662966729668296692967029671296722967329674296752967629677296782967929680296812968229683296842968529686296872968829689296902969129692296932969429695296962969729698296992970029701297022970329704297052970629707297082970929710297112971229713297142971529716297172971829719297202972129722297232972429725297262972729728297292973029731297322973329734297352973629737297382973929740297412974229743297442974529746297472974829749297502975129752297532975429755297562975729758297592976029761297622976329764297652976629767297682976929770297712977229773297742977529776297772977829779297802978129782297832978429785297862978729788297892979029791297922979329794297952979629797297982979929800298012980229803298042980529806298072980829809298102981129812298132981429815298162981729818298192982029821298222982329824298252982629827298282982929830298312983229833298342983529836298372983829839298402984129842298432984429845298462984729848298492985029851298522985329854298552985629857298582985929860298612986229863298642986529866298672986829869298702987129872298732987429875298762987729878298792988029881298822988329884298852988629887298882988929890298912989229893298942989529896298972989829899299002990129902299032990429905299062990729908299092991029911299122991329914299152991629917299182991929920299212992229923299242992529926299272992829929299302993129932299332993429935299362993729938299392994029941299422994329944299452994629947299482994929950299512995229953299542995529956299572995829959299602996129962299632996429965299662996729968299692997029971299722997329974299752997629977299782997929980299812998229983299842998529986299872998829989299902999129992299932999429995299962999729998299993000030001300023000330004300053000630007300083000930010300113001230013300143001530016300173001830019300203002130022300233002430025300263002730028300293003030031300323003330034300353003630037300383003930040300413004230043300443004530046300473004830049300503005130052300533005430055300563005730058300593006030061300623006330064300653006630067300683006930070300713007230073300743007530076300773007830079300803008130082300833008430085300863008730088300893009030091300923009330094300953009630097300983009930100301013010230103301043010530106301073010830109301103011130112301133011430115301163011730118301193012030121301223012330124301253012630127301283012930130301313013230133301343013530136301373013830139301403014130142301433014430145301463014730148301493015030151301523015330154301553015630157301583015930160301613016230163301643016530166301673016830169301703017130172301733017430175301763017730178301793018030181301823018330184301853018630187301883018930190301913019230193301943019530196301973019830199302003020130202302033020430205302063020730208302093021030211302123021330214302153021630217302183021930220302213022230223302243022530226302273022830229302303023130232302333023430235302363023730238302393024030241302423024330244302453024630247302483024930250302513025230253302543025530256302573025830259302603026130262302633026430265302663026730268302693027030271302723027330274302753027630277302783027930280302813028230283302843028530286302873028830289302903029130292302933029430295302963029730298302993030030301303023030330304303053030630307303083030930310303113031230313303143031530316303173031830319303203032130322303233032430325303263032730328303293033030331303323033330334303353033630337303383033930340303413034230343303443034530346303473034830349303503035130352303533035430355303563035730358303593036030361303623036330364303653036630367303683036930370303713037230373303743037530376303773037830379303803038130382303833038430385303863038730388303893039030391303923039330394303953039630397303983039930400304013040230403304043040530406304073040830409304103041130412304133041430415304163041730418304193042030421304223042330424304253042630427304283042930430304313043230433304343043530436304373043830439304403044130442304433044430445304463044730448304493045030451304523045330454304553045630457304583045930460304613046230463304643046530466304673046830469304703047130472304733047430475304763047730478304793048030481304823048330484304853048630487304883048930490304913049230493304943049530496304973049830499305003050130502305033050430505305063050730508305093051030511305123051330514305153051630517305183051930520305213052230523305243052530526305273052830529305303053130532305333053430535305363053730538305393054030541305423054330544305453054630547305483054930550305513055230553305543055530556305573055830559305603056130562305633056430565305663056730568305693057030571305723057330574305753057630577305783057930580305813058230583305843058530586305873058830589305903059130592305933059430595305963059730598305993060030601306023060330604306053060630607306083060930610306113061230613306143061530616306173061830619306203062130622306233062430625306263062730628306293063030631306323063330634306353063630637306383063930640306413064230643306443064530646306473064830649306503065130652306533065430655306563065730658306593066030661306623066330664306653066630667306683066930670306713067230673306743067530676306773067830679306803068130682306833068430685306863068730688306893069030691306923069330694306953069630697306983069930700307013070230703307043070530706307073070830709307103071130712307133071430715307163071730718307193072030721307223072330724307253072630727307283072930730307313073230733307343073530736307373073830739307403074130742307433074430745307463074730748307493075030751307523075330754307553075630757307583075930760307613076230763307643076530766307673076830769307703077130772307733077430775307763077730778307793078030781307823078330784307853078630787307883078930790307913079230793307943079530796307973079830799308003080130802308033080430805308063080730808308093081030811308123081330814308153081630817308183081930820308213082230823308243082530826308273082830829308303083130832308333083430835308363083730838308393084030841308423084330844308453084630847308483084930850308513085230853308543085530856308573085830859308603086130862308633086430865308663086730868308693087030871308723087330874308753087630877308783087930880308813088230883308843088530886308873088830889308903089130892308933089430895308963089730898308993090030901309023090330904309053090630907309083090930910309113091230913309143091530916309173091830919309203092130922309233092430925309263092730928309293093030931309323093330934309353093630937309383093930940309413094230943309443094530946309473094830949309503095130952309533095430955309563095730958309593096030961309623096330964309653096630967309683096930970309713097230973309743097530976309773097830979309803098130982309833098430985309863098730988309893099030991309923099330994309953099630997309983099931000310013100231003310043100531006310073100831009310103101131012310133101431015310163101731018310193102031021310223102331024310253102631027310283102931030310313103231033310343103531036310373103831039310403104131042310433104431045310463104731048310493105031051310523105331054310553105631057310583105931060310613106231063310643106531066310673106831069310703107131072310733107431075310763107731078310793108031081310823108331084310853108631087310883108931090310913109231093310943109531096310973109831099311003110131102311033110431105311063110731108311093111031111311123111331114311153111631117311183111931120311213112231123311243112531126311273112831129311303113131132311333113431135311363113731138311393114031141311423114331144311453114631147311483114931150311513115231153311543115531156311573115831159311603116131162311633116431165311663116731168311693117031171311723117331174311753117631177311783117931180311813118231183311843118531186311873118831189311903119131192311933119431195311963119731198311993120031201312023120331204312053120631207312083120931210312113121231213312143121531216312173121831219312203122131222312233122431225312263122731228312293123031231312323123331234312353123631237312383123931240312413124231243312443124531246312473124831249312503125131252312533125431255312563125731258312593126031261312623126331264312653126631267312683126931270312713127231273312743127531276312773127831279312803128131282312833128431285312863128731288312893129031291312923129331294312953129631297312983129931300313013130231303313043130531306313073130831309313103131131312313133131431315313163131731318313193132031321313223132331324313253132631327313283132931330313313133231333313343133531336313373133831339313403134131342313433134431345313463134731348313493135031351313523135331354313553135631357313583135931360313613136231363313643136531366313673136831369313703137131372313733137431375313763137731378313793138031381313823138331384313853138631387313883138931390313913139231393313943139531396313973139831399314003140131402314033140431405314063140731408314093141031411314123141331414314153141631417314183141931420314213142231423314243142531426314273142831429314303143131432314333143431435314363143731438314393144031441314423144331444314453144631447314483144931450314513145231453314543145531456314573145831459314603146131462314633146431465314663146731468314693147031471314723147331474314753147631477314783147931480314813148231483314843148531486314873148831489314903149131492314933149431495314963149731498314993150031501315023150331504315053150631507315083150931510315113151231513315143151531516315173151831519315203152131522315233152431525315263152731528315293153031531315323153331534315353153631537315383153931540315413154231543315443154531546315473154831549315503155131552315533155431555315563155731558315593156031561315623156331564315653156631567315683156931570315713157231573315743157531576315773157831579315803158131582315833158431585315863158731588315893159031591315923159331594315953159631597315983159931600316013160231603316043160531606316073160831609316103161131612316133161431615316163161731618316193162031621316223162331624316253162631627316283162931630316313163231633316343163531636316373163831639316403164131642316433164431645316463164731648316493165031651316523165331654316553165631657316583165931660316613166231663316643166531666316673166831669316703167131672316733167431675316763167731678316793168031681316823168331684316853168631687316883168931690316913169231693316943169531696316973169831699317003170131702317033170431705317063170731708317093171031711317123171331714317153171631717317183171931720317213172231723317243172531726317273172831729317303173131732317333173431735317363173731738317393174031741317423174331744317453174631747317483174931750317513175231753317543175531756317573175831759317603176131762317633176431765317663176731768317693177031771317723177331774317753177631777317783177931780317813178231783317843178531786317873178831789317903179131792317933179431795317963179731798317993180031801318023180331804318053180631807318083180931810318113181231813318143181531816318173181831819318203182131822318233182431825318263182731828318293183031831318323183331834318353183631837318383183931840318413184231843318443184531846318473184831849318503185131852318533185431855318563185731858318593186031861318623186331864318653186631867318683186931870318713187231873318743187531876318773187831879318803188131882318833188431885318863188731888318893189031891318923189331894318953189631897318983189931900319013190231903319043190531906319073190831909319103191131912319133191431915319163191731918319193192031921319223192331924319253192631927319283192931930319313193231933319343193531936319373193831939319403194131942319433194431945319463194731948319493195031951319523195331954319553195631957319583195931960319613196231963319643196531966319673196831969319703197131972319733197431975319763197731978319793198031981319823198331984319853198631987319883198931990319913199231993319943199531996319973199831999320003200132002320033200432005320063200732008320093201032011320123201332014320153201632017320183201932020320213202232023320243202532026320273202832029320303203132032320333203432035320363203732038320393204032041320423204332044320453204632047320483204932050320513205232053320543205532056320573205832059320603206132062320633206432065320663206732068320693207032071320723207332074320753207632077320783207932080320813208232083320843208532086320873208832089320903209132092320933209432095320963209732098320993210032101321023210332104321053210632107321083210932110321113211232113321143211532116321173211832119321203212132122321233212432125321263212732128321293213032131321323213332134321353213632137321383213932140321413214232143321443214532146321473214832149321503215132152321533215432155321563215732158321593216032161321623216332164321653216632167321683216932170321713217232173321743217532176321773217832179321803218132182321833218432185321863218732188321893219032191321923219332194321953219632197321983219932200322013220232203322043220532206322073220832209322103221132212322133221432215322163221732218322193222032221322223222332224322253222632227322283222932230322313223232233322343223532236322373223832239322403224132242322433224432245322463224732248322493225032251322523225332254322553225632257322583225932260322613226232263322643226532266322673226832269322703227132272322733227432275322763227732278322793228032281322823228332284322853228632287322883228932290322913229232293322943229532296322973229832299323003230132302323033230432305323063230732308323093231032311323123231332314323153231632317323183231932320323213232232323323243232532326323273232832329323303233132332323333233432335323363233732338323393234032341323423234332344323453234632347323483234932350323513235232353323543235532356323573235832359323603236132362323633236432365323663236732368323693237032371323723237332374323753237632377323783237932380323813238232383323843238532386323873238832389323903239132392323933239432395323963239732398323993240032401324023240332404324053240632407324083240932410324113241232413324143241532416324173241832419324203242132422324233242432425324263242732428324293243032431324323243332434324353243632437324383243932440324413244232443324443244532446324473244832449324503245132452324533245432455324563245732458324593246032461324623246332464324653246632467324683246932470324713247232473324743247532476324773247832479324803248132482324833248432485324863248732488324893249032491324923249332494324953249632497324983249932500325013250232503325043250532506325073250832509325103251132512325133251432515325163251732518325193252032521325223252332524325253252632527325283252932530325313253232533325343253532536325373253832539325403254132542325433254432545325463254732548325493255032551325523255332554325553255632557325583255932560325613256232563325643256532566325673256832569325703257132572325733257432575325763257732578325793258032581325823258332584325853258632587325883258932590325913259232593325943259532596325973259832599326003260132602326033260432605326063260732608326093261032611326123261332614326153261632617326183261932620326213262232623326243262532626326273262832629326303263132632326333263432635326363263732638326393264032641326423264332644326453264632647326483264932650326513265232653326543265532656326573265832659326603266132662326633266432665326663266732668326693267032671326723267332674326753267632677326783267932680326813268232683326843268532686326873268832689326903269132692326933269432695326963269732698326993270032701327023270332704327053270632707327083270932710327113271232713327143271532716327173271832719327203272132722327233272432725327263272732728327293273032731327323273332734327353273632737327383273932740327413274232743327443274532746327473274832749327503275132752327533275432755327563275732758327593276032761327623276332764327653276632767327683276932770327713277232773327743277532776327773277832779327803278132782327833278432785327863278732788327893279032791327923279332794327953279632797327983279932800328013280232803328043280532806328073280832809328103281132812328133281432815328163281732818328193282032821328223282332824328253282632827328283282932830328313283232833328343283532836328373283832839328403284132842328433284432845328463284732848328493285032851328523285332854328553285632857328583285932860328613286232863328643286532866328673286832869328703287132872328733287432875328763287732878328793288032881328823288332884328853288632887328883288932890328913289232893328943289532896328973289832899329003290132902329033290432905329063290732908329093291032911329123291332914329153291632917329183291932920329213292232923329243292532926329273292832929329303293132932329333293432935329363293732938329393294032941329423294332944329453294632947329483294932950329513295232953329543295532956329573295832959329603296132962329633296432965329663296732968329693297032971329723297332974329753297632977329783297932980329813298232983329843298532986329873298832989329903299132992329933299432995329963299732998329993300033001330023300333004330053300633007330083300933010330113301233013330143301533016330173301833019330203302133022330233302433025330263302733028330293303033031330323303333034330353303633037330383303933040330413304233043330443304533046330473304833049330503305133052330533305433055330563305733058330593306033061330623306333064330653306633067330683306933070330713307233073330743307533076330773307833079330803308133082330833308433085330863308733088330893309033091330923309333094330953309633097330983309933100331013310233103331043310533106331073310833109331103311133112331133311433115331163311733118331193312033121331223312333124331253312633127331283312933130331313313233133331343313533136331373313833139331403314133142331433314433145331463314733148331493315033151331523315333154331553315633157331583315933160331613316233163331643316533166331673316833169331703317133172331733317433175331763317733178331793318033181331823318333184331853318633187331883318933190331913319233193331943319533196331973319833199332003320133202332033320433205332063320733208332093321033211332123321333214332153321633217332183321933220332213322233223332243322533226332273322833229332303323133232332333323433235332363323733238332393324033241332423324333244332453324633247332483324933250332513325233253332543325533256332573325833259332603326133262332633326433265332663326733268332693327033271332723327333274332753327633277332783327933280332813328233283332843328533286332873328833289332903329133292332933329433295332963329733298332993330033301333023330333304333053330633307333083330933310333113331233313333143331533316333173331833319333203332133322333233332433325333263332733328333293333033331333323333333334333353333633337333383333933340333413334233343333443334533346333473334833349333503335133352333533335433355333563335733358333593336033361333623336333364333653336633367333683336933370333713337233373333743337533376333773337833379333803338133382333833338433385333863338733388333893339033391333923339333394333953339633397333983339933400334013340233403334043340533406334073340833409334103341133412334133341433415334163341733418334193342033421334223342333424334253342633427334283342933430334313343233433334343343533436334373343833439334403344133442334433344433445334463344733448334493345033451334523345333454334553345633457334583345933460334613346233463334643346533466334673346833469334703347133472334733347433475334763347733478334793348033481334823348333484334853348633487334883348933490334913349233493334943349533496334973349833499335003350133502335033350433505335063350733508335093351033511335123351333514335153351633517335183351933520335213352233523335243352533526335273352833529335303353133532335333353433535335363353733538335393354033541335423354333544335453354633547335483354933550335513355233553335543355533556335573355833559335603356133562335633356433565335663356733568335693357033571335723357333574335753357633577335783357933580335813358233583335843358533586335873358833589335903359133592335933359433595335963359733598335993360033601336023360333604336053360633607336083360933610336113361233613336143361533616336173361833619336203362133622336233362433625336263362733628336293363033631336323363333634336353363633637336383363933640336413364233643336443364533646336473364833649336503365133652336533365433655336563365733658336593366033661336623366333664336653366633667336683366933670336713367233673336743367533676336773367833679336803368133682336833368433685336863368733688336893369033691336923369333694336953369633697336983369933700337013370233703337043370533706337073370833709337103371133712337133371433715337163371733718337193372033721337223372333724337253372633727337283372933730337313373233733337343373533736337373373833739337403374133742337433374433745337463374733748337493375033751337523375333754337553375633757337583375933760337613376233763337643376533766337673376833769337703377133772337733377433775337763377733778337793378033781337823378333784337853378633787337883378933790337913379233793337943379533796337973379833799338003380133802338033380433805338063380733808338093381033811338123381333814338153381633817338183381933820338213382233823338243382533826338273382833829338303383133832338333383433835338363383733838338393384033841338423384333844338453384633847338483384933850338513385233853338543385533856338573385833859338603386133862338633386433865338663386733868338693387033871338723387333874338753387633877338783387933880338813388233883338843388533886338873388833889338903389133892338933389433895338963389733898338993390033901339023390333904339053390633907339083390933910339113391233913339143391533916339173391833919339203392133922339233392433925339263392733928339293393033931339323393333934339353393633937339383393933940339413394233943339443394533946339473394833949339503395133952339533395433955339563395733958339593396033961339623396333964339653396633967339683396933970339713397233973339743397533976339773397833979339803398133982339833398433985339863398733988339893399033991339923399333994339953399633997339983399934000340013400234003340043400534006340073400834009340103401134012340133401434015340163401734018340193402034021340223402334024340253402634027340283402934030340313403234033340343403534036340373403834039340403404134042340433404434045340463404734048340493405034051340523405334054340553405634057340583405934060340613406234063340643406534066340673406834069340703407134072340733407434075340763407734078340793408034081340823408334084340853408634087340883408934090340913409234093340943409534096340973409834099341003410134102341033410434105341063410734108341093411034111341123411334114341153411634117341183411934120341213412234123341243412534126341273412834129341303413134132341333413434135341363413734138341393414034141341423414334144341453414634147341483414934150341513415234153341543415534156341573415834159341603416134162341633416434165341663416734168341693417034171341723417334174341753417634177341783417934180341813418234183341843418534186341873418834189341903419134192341933419434195341963419734198341993420034201342023420334204342053420634207342083420934210342113421234213342143421534216342173421834219342203422134222342233422434225342263422734228342293423034231342323423334234342353423634237342383423934240342413424234243342443424534246342473424834249342503425134252342533425434255342563425734258342593426034261342623426334264342653426634267342683426934270342713427234273342743427534276342773427834279342803428134282342833428434285342863428734288342893429034291342923429334294342953429634297342983429934300343013430234303343043430534306343073430834309343103431134312343133431434315343163431734318343193432034321343223432334324343253432634327343283432934330343313433234333343343433534336343373433834339343403434134342343433434434345343463434734348343493435034351343523435334354343553435634357343583435934360343613436234363343643436534366343673436834369343703437134372343733437434375343763437734378343793438034381343823438334384343853438634387343883438934390343913439234393343943439534396343973439834399344003440134402344033440434405344063440734408344093441034411344123441334414344153441634417344183441934420344213442234423344243442534426344273442834429344303443134432344333443434435344363443734438344393444034441344423444334444344453444634447344483444934450344513445234453344543445534456344573445834459344603446134462344633446434465344663446734468344693447034471344723447334474344753447634477344783447934480344813448234483344843448534486344873448834489344903449134492344933449434495344963449734498344993450034501345023450334504345053450634507345083450934510345113451234513345143451534516345173451834519345203452134522345233452434525345263452734528345293453034531345323453334534345353453634537345383453934540345413454234543345443454534546345473454834549345503455134552345533455434555345563455734558345593456034561345623456334564345653456634567345683456934570345713457234573345743457534576345773457834579345803458134582345833458434585345863458734588345893459034591345923459334594345953459634597345983459934600346013460234603346043460534606346073460834609346103461134612346133461434615346163461734618346193462034621346223462334624346253462634627346283462934630346313463234633346343463534636346373463834639346403464134642346433464434645346463464734648346493465034651346523465334654346553465634657346583465934660346613466234663346643466534666346673466834669346703467134672346733467434675346763467734678346793468034681346823468334684346853468634687346883468934690346913469234693346943469534696346973469834699347003470134702347033470434705347063470734708347093471034711347123471334714347153471634717347183471934720347213472234723347243472534726347273472834729347303473134732347333473434735347363473734738347393474034741347423474334744347453474634747347483474934750347513475234753347543475534756347573475834759347603476134762347633476434765347663476734768347693477034771347723477334774347753477634777347783477934780347813478234783347843478534786347873478834789347903479134792347933479434795347963479734798347993480034801348023480334804348053480634807348083480934810348113481234813348143481534816348173481834819348203482134822348233482434825348263482734828348293483034831348323483334834348353483634837348383483934840348413484234843348443484534846348473484834849348503485134852348533485434855348563485734858348593486034861348623486334864348653486634867348683486934870348713487234873348743487534876348773487834879348803488134882348833488434885348863488734888348893489034891348923489334894348953489634897348983489934900349013490234903349043490534906349073490834909349103491134912349133491434915349163491734918349193492034921349223492334924349253492634927349283492934930349313493234933349343493534936349373493834939349403494134942349433494434945349463494734948349493495034951349523495334954349553495634957349583495934960349613496234963349643496534966349673496834969349703497134972349733497434975349763497734978349793498034981349823498334984349853498634987349883498934990349913499234993349943499534996349973499834999350003500135002350033500435005350063500735008350093501035011350123501335014350153501635017350183501935020350213502235023350243502535026350273502835029350303503135032350333503435035350363503735038350393504035041350423504335044350453504635047350483504935050350513505235053350543505535056350573505835059350603506135062350633506435065350663506735068350693507035071350723507335074350753507635077350783507935080350813508235083350843508535086350873508835089350903509135092350933509435095350963509735098350993510035101351023510335104351053510635107351083510935110351113511235113351143511535116351173511835119351203512135122351233512435125351263512735128351293513035131351323513335134351353513635137351383513935140351413514235143351443514535146351473514835149351503515135152351533515435155351563515735158351593516035161351623516335164351653516635167351683516935170351713517235173351743517535176351773517835179351803518135182351833518435185351863518735188351893519035191351923519335194351953519635197351983519935200352013520235203352043520535206352073520835209352103521135212352133521435215352163521735218352193522035221352223522335224352253522635227352283522935230352313523235233352343523535236352373523835239352403524135242352433524435245352463524735248352493525035251352523525335254352553525635257352583525935260352613526235263352643526535266352673526835269352703527135272352733527435275352763527735278352793528035281352823528335284352853528635287352883528935290352913529235293352943529535296352973529835299353003530135302353033530435305353063530735308353093531035311353123531335314353153531635317353183531935320353213532235323353243532535326353273532835329353303533135332353333533435335353363533735338353393534035341353423534335344353453534635347353483534935350353513535235353353543535535356353573535835359353603536135362353633536435365353663536735368353693537035371353723537335374353753537635377353783537935380353813538235383353843538535386353873538835389353903539135392353933539435395353963539735398353993540035401354023540335404354053540635407354083540935410354113541235413354143541535416354173541835419354203542135422354233542435425354263542735428354293543035431354323543335434354353543635437354383543935440354413544235443354443544535446354473544835449354503545135452354533545435455354563545735458354593546035461354623546335464354653546635467354683546935470354713547235473354743547535476354773547835479354803548135482354833548435485354863548735488354893549035491354923549335494354953549635497354983549935500355013550235503355043550535506355073550835509355103551135512355133551435515355163551735518355193552035521355223552335524355253552635527355283552935530355313553235533355343553535536355373553835539355403554135542355433554435545355463554735548355493555035551355523555335554355553555635557355583555935560355613556235563355643556535566355673556835569355703557135572355733557435575355763557735578355793558035581355823558335584355853558635587355883558935590355913559235593355943559535596355973559835599356003560135602356033560435605356063560735608356093561035611356123561335614356153561635617356183561935620356213562235623356243562535626356273562835629356303563135632356333563435635356363563735638356393564035641356423564335644356453564635647356483564935650356513565235653356543565535656356573565835659356603566135662356633566435665356663566735668356693567035671356723567335674356753567635677356783567935680356813568235683356843568535686356873568835689356903569135692356933569435695356963569735698356993570035701357023570335704357053570635707357083570935710357113571235713357143571535716357173571835719357203572135722357233572435725357263572735728357293573035731357323573335734357353573635737357383573935740357413574235743357443574535746357473574835749357503575135752357533575435755357563575735758357593576035761357623576335764357653576635767357683576935770357713577235773357743577535776357773577835779357803578135782357833578435785357863578735788357893579035791357923579335794357953579635797357983579935800358013580235803358043580535806358073580835809358103581135812358133581435815358163581735818358193582035821358223582335824358253582635827358283582935830358313583235833358343583535836358373583835839358403584135842358433584435845358463584735848358493585035851358523585335854358553585635857358583585935860358613586235863358643586535866358673586835869358703587135872358733587435875358763587735878358793588035881358823588335884358853588635887358883588935890358913589235893358943589535896358973589835899359003590135902359033590435905359063590735908359093591035911359123591335914359153591635917359183591935920359213592235923359243592535926359273592835929359303593135932359333593435935359363593735938359393594035941359423594335944359453594635947359483594935950359513595235953359543595535956359573595835959359603596135962359633596435965359663596735968359693597035971359723597335974359753597635977359783597935980359813598235983359843598535986359873598835989359903599135992359933599435995359963599735998359993600036001360023600336004360053600636007360083600936010360113601236013360143601536016360173601836019360203602136022360233602436025360263602736028360293603036031360323603336034360353603636037360383603936040360413604236043360443604536046360473604836049360503605136052360533605436055360563605736058360593606036061360623606336064360653606636067360683606936070360713607236073360743607536076360773607836079360803608136082360833608436085360863608736088360893609036091360923609336094360953609636097360983609936100361013610236103361043610536106361073610836109361103611136112361133611436115361163611736118361193612036121361223612336124361253612636127361283612936130361313613236133361343613536136361373613836139361403614136142361433614436145361463614736148361493615036151361523615336154361553615636157361583615936160361613616236163361643616536166361673616836169361703617136172361733617436175361763617736178361793618036181361823618336184361853618636187361883618936190361913619236193361943619536196361973619836199362003620136202362033620436205362063620736208362093621036211362123621336214362153621636217362183621936220362213622236223362243622536226362273622836229362303623136232362333623436235362363623736238362393624036241362423624336244362453624636247362483624936250362513625236253362543625536256362573625836259362603626136262362633626436265362663626736268362693627036271362723627336274362753627636277362783627936280362813628236283362843628536286362873628836289362903629136292362933629436295362963629736298362993630036301363023630336304363053630636307363083630936310363113631236313363143631536316363173631836319363203632136322363233632436325363263632736328363293633036331363323633336334363353633636337363383633936340363413634236343363443634536346363473634836349363503635136352363533635436355363563635736358363593636036361363623636336364363653636636367363683636936370363713637236373363743637536376363773637836379363803638136382363833638436385363863638736388363893639036391363923639336394363953639636397363983639936400364013640236403364043640536406364073640836409364103641136412364133641436415364163641736418364193642036421364223642336424364253642636427364283642936430364313643236433364343643536436364373643836439364403644136442364433644436445364463644736448364493645036451364523645336454364553645636457364583645936460364613646236463364643646536466364673646836469364703647136472364733647436475364763647736478364793648036481364823648336484364853648636487364883648936490364913649236493364943649536496364973649836499365003650136502365033650436505365063650736508365093651036511365123651336514365153651636517365183651936520365213652236523365243652536526365273652836529365303653136532365333653436535365363653736538365393654036541365423654336544365453654636547365483654936550365513655236553365543655536556365573655836559365603656136562365633656436565365663656736568365693657036571365723657336574365753657636577365783657936580365813658236583365843658536586365873658836589365903659136592365933659436595365963659736598365993660036601366023660336604366053660636607366083660936610366113661236613366143661536616366173661836619366203662136622366233662436625366263662736628366293663036631366323663336634366353663636637366383663936640366413664236643366443664536646366473664836649366503665136652366533665436655366563665736658366593666036661366623666336664366653666636667366683666936670366713667236673366743667536676366773667836679366803668136682366833668436685366863668736688366893669036691366923669336694366953669636697366983669936700367013670236703367043670536706367073670836709367103671136712367133671436715367163671736718367193672036721367223672336724367253672636727367283672936730367313673236733367343673536736367373673836739367403674136742367433674436745367463674736748367493675036751367523675336754367553675636757367583675936760367613676236763367643676536766367673676836769367703677136772367733677436775367763677736778367793678036781367823678336784367853678636787367883678936790367913679236793367943679536796367973679836799368003680136802368033680436805368063680736808368093681036811368123681336814368153681636817368183681936820368213682236823368243682536826368273682836829368303683136832368333683436835368363683736838368393684036841368423684336844368453684636847368483684936850368513685236853368543685536856368573685836859368603686136862368633686436865368663686736868368693687036871368723687336874368753687636877368783687936880368813688236883368843688536886368873688836889368903689136892368933689436895368963689736898368993690036901369023690336904369053690636907369083690936910369113691236913369143691536916369173691836919369203692136922369233692436925369263692736928369293693036931369323693336934369353693636937369383693936940369413694236943369443694536946369473694836949369503695136952369533695436955369563695736958369593696036961369623696336964369653696636967369683696936970369713697236973369743697536976369773697836979369803698136982369833698436985369863698736988369893699036991369923699336994369953699636997369983699937000370013700237003370043700537006370073700837009370103701137012370133701437015370163701737018370193702037021370223702337024370253702637027370283702937030370313703237033370343703537036370373703837039370403704137042370433704437045370463704737048370493705037051370523705337054370553705637057370583705937060370613706237063370643706537066370673706837069370703707137072370733707437075370763707737078370793708037081370823708337084370853708637087370883708937090370913709237093370943709537096370973709837099371003710137102371033710437105371063710737108371093711037111371123711337114371153711637117371183711937120371213712237123371243712537126371273712837129371303713137132371333713437135371363713737138371393714037141371423714337144371453714637147371483714937150371513715237153371543715537156371573715837159371603716137162371633716437165371663716737168371693717037171371723717337174371753717637177371783717937180371813718237183371843718537186371873718837189371903719137192371933719437195371963719737198371993720037201372023720337204372053720637207372083720937210372113721237213372143721537216372173721837219372203722137222372233722437225372263722737228372293723037231372323723337234372353723637237372383723937240372413724237243372443724537246372473724837249372503725137252372533725437255372563725737258372593726037261372623726337264372653726637267372683726937270372713727237273372743727537276372773727837279372803728137282372833728437285372863728737288372893729037291372923729337294372953729637297372983729937300373013730237303373043730537306373073730837309373103731137312373133731437315373163731737318373193732037321373223732337324373253732637327373283732937330373313733237333373343733537336373373733837339373403734137342373433734437345373463734737348373493735037351373523735337354373553735637357373583735937360373613736237363373643736537366373673736837369373703737137372373733737437375373763737737378373793738037381373823738337384373853738637387373883738937390373913739237393373943739537396373973739837399374003740137402374033740437405374063740737408374093741037411374123741337414374153741637417374183741937420374213742237423374243742537426374273742837429374303743137432374333743437435374363743737438374393744037441374423744337444374453744637447374483744937450374513745237453374543745537456374573745837459374603746137462374633746437465374663746737468374693747037471374723747337474374753747637477374783747937480374813748237483374843748537486374873748837489374903749137492374933749437495374963749737498374993750037501375023750337504375053750637507375083750937510375113751237513375143751537516375173751837519375203752137522375233752437525375263752737528375293753037531375323753337534375353753637537375383753937540375413754237543375443754537546375473754837549375503755137552375533755437555375563755737558375593756037561375623756337564375653756637567375683756937570375713757237573375743757537576375773757837579375803758137582375833758437585375863758737588375893759037591375923759337594375953759637597375983759937600376013760237603376043760537606376073760837609376103761137612376133761437615376163761737618376193762037621376223762337624376253762637627376283762937630376313763237633376343763537636376373763837639376403764137642376433764437645376463764737648376493765037651376523765337654376553765637657376583765937660376613766237663376643766537666376673766837669376703767137672376733767437675376763767737678376793768037681376823768337684376853768637687376883768937690376913769237693376943769537696376973769837699377003770137702377033770437705377063770737708377093771037711377123771337714377153771637717377183771937720377213772237723377243772537726377273772837729377303773137732377333773437735377363773737738377393774037741377423774337744377453774637747377483774937750377513775237753377543775537756377573775837759377603776137762377633776437765377663776737768377693777037771377723777337774377753777637777377783777937780377813778237783377843778537786377873778837789377903779137792377933779437795377963779737798377993780037801378023780337804378053780637807378083780937810378113781237813378143781537816378173781837819378203782137822378233782437825378263782737828378293783037831378323783337834378353783637837378383783937840378413784237843378443784537846378473784837849378503785137852378533785437855378563785737858378593786037861378623786337864378653786637867378683786937870378713787237873378743787537876378773787837879378803788137882378833788437885378863788737888378893789037891378923789337894378953789637897378983789937900379013790237903379043790537906379073790837909379103791137912379133791437915379163791737918379193792037921379223792337924379253792637927379283792937930379313793237933379343793537936379373793837939379403794137942379433794437945379463794737948379493795037951379523795337954379553795637957379583795937960379613796237963379643796537966379673796837969379703797137972379733797437975379763797737978379793798037981379823798337984379853798637987379883798937990379913799237993379943799537996379973799837999380003800138002380033800438005380063800738008380093801038011380123801338014380153801638017380183801938020380213802238023380243802538026380273802838029380303803138032380333803438035380363803738038380393804038041380423804338044380453804638047380483804938050380513805238053380543805538056380573805838059380603806138062380633806438065380663806738068380693807038071380723807338074380753807638077380783807938080380813808238083380843808538086380873808838089380903809138092380933809438095380963809738098380993810038101381023810338104381053810638107381083810938110381113811238113381143811538116381173811838119381203812138122381233812438125381263812738128381293813038131381323813338134381353813638137381383813938140381413814238143381443814538146381473814838149381503815138152381533815438155381563815738158381593816038161381623816338164381653816638167381683816938170381713817238173381743817538176381773817838179381803818138182381833818438185381863818738188381893819038191381923819338194381953819638197381983819938200382013820238203382043820538206382073820838209382103821138212382133821438215382163821738218382193822038221382223822338224382253822638227382283822938230382313823238233382343823538236382373823838239382403824138242382433824438245382463824738248382493825038251382523825338254382553825638257382583825938260382613826238263382643826538266382673826838269382703827138272382733827438275382763827738278382793828038281382823828338284382853828638287382883828938290382913829238293382943829538296382973829838299383003830138302383033830438305383063830738308383093831038311383123831338314383153831638317383183831938320383213832238323383243832538326383273832838329383303833138332383333833438335383363833738338383393834038341383423834338344383453834638347383483834938350383513835238353383543835538356383573835838359383603836138362383633836438365383663836738368383693837038371383723837338374383753837638377383783837938380383813838238383383843838538386383873838838389383903839138392383933839438395383963839738398383993840038401384023840338404384053840638407384083840938410384113841238413384143841538416384173841838419384203842138422384233842438425384263842738428384293843038431384323843338434384353843638437384383843938440384413844238443384443844538446384473844838449384503845138452384533845438455384563845738458384593846038461384623846338464384653846638467384683846938470384713847238473384743847538476384773847838479384803848138482384833848438485384863848738488384893849038491384923849338494384953849638497384983849938500385013850238503385043850538506385073850838509385103851138512385133851438515385163851738518385193852038521385223852338524385253852638527385283852938530385313853238533385343853538536385373853838539385403854138542385433854438545385463854738548385493855038551385523855338554385553855638557385583855938560385613856238563385643856538566385673856838569385703857138572385733857438575385763857738578385793858038581385823858338584385853858638587385883858938590385913859238593385943859538596385973859838599386003860138602386033860438605386063860738608386093861038611386123861338614386153861638617386183861938620386213862238623386243862538626386273862838629386303863138632386333863438635386363863738638386393864038641386423864338644386453864638647386483864938650386513865238653386543865538656386573865838659386603866138662386633866438665386663866738668386693867038671386723867338674386753867638677386783867938680386813868238683386843868538686386873868838689386903869138692386933869438695386963869738698386993870038701387023870338704387053870638707387083870938710387113871238713387143871538716387173871838719387203872138722387233872438725387263872738728387293873038731387323873338734387353873638737387383873938740387413874238743387443874538746387473874838749387503875138752387533875438755387563875738758387593876038761387623876338764387653876638767387683876938770387713877238773387743877538776387773877838779387803878138782387833878438785387863878738788387893879038791387923879338794387953879638797387983879938800388013880238803388043880538806388073880838809388103881138812388133881438815388163881738818388193882038821388223882338824388253882638827388283882938830388313883238833388343883538836388373883838839388403884138842388433884438845388463884738848388493885038851388523885338854388553885638857388583885938860388613886238863388643886538866388673886838869388703887138872388733887438875388763887738878388793888038881388823888338884388853888638887388883888938890388913889238893388943889538896388973889838899389003890138902389033890438905389063890738908389093891038911389123891338914389153891638917389183891938920389213892238923389243892538926389273892838929389303893138932389333893438935389363893738938389393894038941389423894338944389453894638947389483894938950389513895238953389543895538956389573895838959389603896138962389633896438965389663896738968389693897038971389723897338974389753897638977389783897938980389813898238983389843898538986389873898838989389903899138992389933899438995389963899738998389993900039001390023900339004390053900639007390083900939010390113901239013390143901539016390173901839019390203902139022390233902439025390263902739028390293903039031390323903339034390353903639037390383903939040390413904239043390443904539046390473904839049390503905139052390533905439055390563905739058390593906039061390623906339064390653906639067390683906939070390713907239073390743907539076390773907839079390803908139082390833908439085390863908739088390893909039091390923909339094390953909639097390983909939100391013910239103391043910539106391073910839109391103911139112391133911439115391163911739118391193912039121391223912339124391253912639127391283912939130391313913239133391343913539136391373913839139391403914139142391433914439145391463914739148391493915039151391523915339154391553915639157391583915939160391613916239163391643916539166391673916839169391703917139172391733917439175391763917739178391793918039181391823918339184391853918639187391883918939190391913919239193391943919539196391973919839199392003920139202392033920439205392063920739208392093921039211392123921339214392153921639217392183921939220392213922239223392243922539226392273922839229392303923139232392333923439235392363923739238392393924039241392423924339244392453924639247392483924939250392513925239253392543925539256392573925839259392603926139262392633926439265392663926739268392693927039271392723927339274392753927639277392783927939280392813928239283392843928539286392873928839289392903929139292392933929439295392963929739298392993930039301393023930339304393053930639307393083930939310393113931239313393143931539316393173931839319393203932139322393233932439325393263932739328393293933039331393323933339334393353933639337393383933939340393413934239343393443934539346393473934839349393503935139352393533935439355393563935739358393593936039361393623936339364393653936639367393683936939370393713937239373393743937539376393773937839379393803938139382393833938439385393863938739388393893939039391393923939339394393953939639397393983939939400394013940239403394043940539406394073940839409394103941139412394133941439415394163941739418394193942039421394223942339424394253942639427394283942939430394313943239433394343943539436394373943839439394403944139442394433944439445394463944739448394493945039451394523945339454394553945639457394583945939460394613946239463394643946539466394673946839469394703947139472394733947439475394763947739478394793948039481394823948339484394853948639487394883948939490394913949239493394943949539496394973949839499395003950139502395033950439505395063950739508395093951039511395123951339514395153951639517395183951939520395213952239523395243952539526395273952839529395303953139532395333953439535395363953739538395393954039541395423954339544395453954639547395483954939550395513955239553395543955539556395573955839559395603956139562395633956439565395663956739568395693957039571395723957339574395753957639577395783957939580395813958239583395843958539586395873958839589395903959139592395933959439595395963959739598395993960039601396023960339604396053960639607396083960939610396113961239613396143961539616396173961839619396203962139622396233962439625396263962739628396293963039631396323963339634396353963639637396383963939640396413964239643396443964539646396473964839649396503965139652396533965439655396563965739658396593966039661396623966339664396653966639667396683966939670396713967239673396743967539676396773967839679396803968139682396833968439685396863968739688396893969039691396923969339694396953969639697396983969939700397013970239703397043970539706397073970839709397103971139712397133971439715397163971739718397193972039721397223972339724397253972639727397283972939730397313973239733397343973539736397373973839739397403974139742397433974439745397463974739748397493975039751397523975339754397553975639757397583975939760397613976239763397643976539766397673976839769397703977139772397733977439775397763977739778397793978039781397823978339784397853978639787397883978939790397913979239793397943979539796397973979839799398003980139802398033980439805398063980739808398093981039811398123981339814398153981639817398183981939820398213982239823398243982539826398273982839829398303983139832398333983439835398363983739838398393984039841398423984339844398453984639847398483984939850398513985239853398543985539856398573985839859398603986139862398633986439865398663986739868398693987039871398723987339874398753987639877398783987939880398813988239883398843988539886398873988839889398903989139892398933989439895398963989739898398993990039901399023990339904399053990639907399083990939910399113991239913399143991539916399173991839919399203992139922399233992439925399263992739928399293993039931399323993339934399353993639937399383993939940399413994239943399443994539946399473994839949399503995139952399533995439955399563995739958399593996039961399623996339964399653996639967399683996939970399713997239973399743997539976399773997839979399803998139982399833998439985399863998739988399893999039991399923999339994399953999639997399983999940000400014000240003400044000540006400074000840009400104001140012400134001440015400164001740018400194002040021400224002340024400254002640027400284002940030400314003240033400344003540036400374003840039400404004140042400434004440045400464004740048400494005040051400524005340054400554005640057400584005940060400614006240063400644006540066400674006840069400704007140072400734007440075400764007740078400794008040081400824008340084400854008640087400884008940090400914009240093400944009540096400974009840099401004010140102401034010440105401064010740108401094011040111401124011340114401154011640117401184011940120401214012240123401244012540126401274012840129401304013140132401334013440135401364013740138401394014040141401424014340144401454014640147401484014940150401514015240153401544015540156401574015840159401604016140162401634016440165401664016740168401694017040171401724017340174401754017640177401784017940180401814018240183401844018540186401874018840189401904019140192401934019440195401964019740198401994020040201402024020340204402054020640207402084020940210402114021240213402144021540216402174021840219402204022140222402234022440225402264022740228402294023040231402324023340234402354023640237402384023940240402414024240243402444024540246402474024840249402504025140252402534025440255402564025740258402594026040261402624026340264402654026640267402684026940270402714027240273402744027540276402774027840279402804028140282402834028440285402864028740288402894029040291402924029340294402954029640297402984029940300403014030240303403044030540306403074030840309403104031140312403134031440315403164031740318403194032040321403224032340324403254032640327403284032940330403314033240333403344033540336403374033840339403404034140342403434034440345403464034740348403494035040351403524035340354403554035640357403584035940360403614036240363403644036540366403674036840369403704037140372403734037440375403764037740378403794038040381403824038340384403854038640387403884038940390403914039240393403944039540396403974039840399404004040140402404034040440405404064040740408404094041040411404124041340414404154041640417404184041940420404214042240423404244042540426404274042840429404304043140432404334043440435404364043740438404394044040441404424044340444404454044640447404484044940450404514045240453404544045540456404574045840459404604046140462404634046440465404664046740468404694047040471404724047340474404754047640477404784047940480404814048240483404844048540486404874048840489404904049140492404934049440495404964049740498404994050040501405024050340504405054050640507405084050940510405114051240513405144051540516405174051840519405204052140522405234052440525405264052740528405294053040531405324053340534405354053640537405384053940540405414054240543405444054540546405474054840549405504055140552405534055440555405564055740558405594056040561405624056340564405654056640567405684056940570405714057240573405744057540576405774057840579405804058140582405834058440585405864058740588405894059040591405924059340594405954059640597405984059940600406014060240603406044060540606406074060840609406104061140612406134061440615406164061740618406194062040621406224062340624406254062640627406284062940630406314063240633406344063540636406374063840639406404064140642406434064440645406464064740648406494065040651406524065340654406554065640657406584065940660406614066240663406644066540666406674066840669406704067140672406734067440675406764067740678406794068040681406824068340684406854068640687406884068940690406914069240693406944069540696406974069840699407004070140702407034070440705407064070740708407094071040711407124071340714407154071640717407184071940720407214072240723407244072540726407274072840729407304073140732407334073440735407364073740738407394074040741407424074340744407454074640747407484074940750407514075240753407544075540756407574075840759407604076140762407634076440765407664076740768407694077040771407724077340774407754077640777407784077940780407814078240783407844078540786407874078840789407904079140792407934079440795407964079740798407994080040801408024080340804408054080640807408084080940810408114081240813408144081540816408174081840819408204082140822408234082440825408264082740828408294083040831408324083340834408354083640837408384083940840408414084240843408444084540846408474084840849408504085140852408534085440855408564085740858408594086040861408624086340864408654086640867408684086940870408714087240873408744087540876408774087840879408804088140882408834088440885408864088740888408894089040891408924089340894408954089640897408984089940900409014090240903409044090540906409074090840909409104091140912409134091440915409164091740918409194092040921409224092340924409254092640927409284092940930409314093240933409344093540936409374093840939409404094140942409434094440945409464094740948409494095040951409524095340954409554095640957409584095940960409614096240963409644096540966409674096840969409704097140972409734097440975409764097740978409794098040981409824098340984409854098640987409884098940990409914099240993409944099540996409974099840999410004100141002410034100441005410064100741008410094101041011410124101341014410154101641017410184101941020410214102241023410244102541026410274102841029410304103141032410334103441035410364103741038410394104041041410424104341044410454104641047410484104941050410514105241053410544105541056410574105841059410604106141062410634106441065410664106741068410694107041071410724107341074410754107641077410784107941080410814108241083410844108541086410874108841089410904109141092410934109441095410964109741098410994110041101411024110341104411054110641107411084110941110411114111241113411144111541116411174111841119411204112141122411234112441125411264112741128411294113041131411324113341134411354113641137411384113941140411414114241143411444114541146411474114841149411504115141152411534115441155411564115741158411594116041161411624116341164411654116641167411684116941170411714117241173411744117541176411774117841179411804118141182411834118441185411864118741188411894119041191411924119341194411954119641197411984119941200412014120241203412044120541206412074120841209412104121141212412134121441215412164121741218412194122041221412224122341224412254122641227412284122941230412314123241233412344123541236412374123841239412404124141242412434124441245412464124741248412494125041251412524125341254412554125641257412584125941260412614126241263412644126541266412674126841269412704127141272412734127441275412764127741278412794128041281412824128341284412854128641287412884128941290412914129241293412944129541296412974129841299413004130141302413034130441305413064130741308413094131041311413124131341314413154131641317413184131941320413214132241323413244132541326413274132841329413304133141332413334133441335413364133741338413394134041341413424134341344413454134641347413484134941350413514135241353413544135541356413574135841359413604136141362413634136441365413664136741368413694137041371413724137341374413754137641377413784137941380413814138241383413844138541386413874138841389413904139141392413934139441395413964139741398413994140041401414024140341404414054140641407414084140941410414114141241413414144141541416414174141841419414204142141422414234142441425414264142741428414294143041431414324143341434414354143641437414384143941440414414144241443414444144541446414474144841449414504145141452414534145441455414564145741458414594146041461414624146341464414654146641467414684146941470414714147241473414744147541476414774147841479414804148141482414834148441485414864148741488414894149041491414924149341494414954149641497414984149941500415014150241503415044150541506415074150841509415104151141512415134151441515415164151741518415194152041521415224152341524415254152641527415284152941530415314153241533415344153541536415374153841539415404154141542415434154441545415464154741548415494155041551415524155341554415554155641557415584155941560415614156241563415644156541566415674156841569415704157141572415734157441575415764157741578415794158041581415824158341584415854158641587415884158941590415914159241593415944159541596415974159841599416004160141602416034160441605416064160741608416094161041611416124161341614416154161641617416184161941620416214162241623416244162541626416274162841629416304163141632416334163441635416364163741638416394164041641416424164341644416454164641647416484164941650416514165241653416544165541656416574165841659416604166141662416634166441665416664166741668416694167041671416724167341674416754167641677416784167941680416814168241683416844168541686416874168841689416904169141692416934169441695416964169741698416994170041701417024170341704417054170641707417084170941710417114171241713417144171541716417174171841719417204172141722417234172441725417264172741728417294173041731417324173341734417354173641737417384173941740417414174241743417444174541746417474174841749417504175141752417534175441755417564175741758417594176041761417624176341764417654176641767417684176941770417714177241773417744177541776417774177841779417804178141782417834178441785417864178741788417894179041791417924179341794417954179641797417984179941800418014180241803418044180541806418074180841809418104181141812418134181441815418164181741818418194182041821418224182341824418254182641827418284182941830418314183241833418344183541836418374183841839418404184141842418434184441845418464184741848418494185041851418524185341854418554185641857418584185941860418614186241863418644186541866418674186841869418704187141872418734187441875418764187741878418794188041881418824188341884418854188641887418884188941890418914189241893418944189541896418974189841899419004190141902419034190441905419064190741908419094191041911419124191341914419154191641917419184191941920419214192241923419244192541926419274192841929419304193141932419334193441935419364193741938419394194041941419424194341944419454194641947419484194941950419514195241953419544195541956419574195841959419604196141962419634196441965419664196741968419694197041971419724197341974419754197641977419784197941980419814198241983419844198541986419874198841989419904199141992419934199441995419964199741998419994200042001420024200342004420054200642007420084200942010420114201242013420144201542016420174201842019420204202142022420234202442025420264202742028420294203042031420324203342034420354203642037420384203942040420414204242043420444204542046420474204842049420504205142052420534205442055420564205742058420594206042061420624206342064420654206642067420684206942070420714207242073420744207542076420774207842079420804208142082420834208442085420864208742088420894209042091420924209342094420954209642097420984209942100421014210242103421044210542106421074210842109421104211142112421134211442115421164211742118421194212042121421224212342124421254212642127421284212942130421314213242133421344213542136421374213842139421404214142142421434214442145421464214742148421494215042151421524215342154421554215642157421584215942160421614216242163421644216542166421674216842169421704217142172421734217442175421764217742178421794218042181421824218342184421854218642187421884218942190421914219242193421944219542196421974219842199422004220142202422034220442205422064220742208422094221042211422124221342214422154221642217422184221942220422214222242223422244222542226422274222842229422304223142232422334223442235422364223742238422394224042241422424224342244422454224642247422484224942250422514225242253422544225542256422574225842259422604226142262422634226442265422664226742268422694227042271422724227342274422754227642277422784227942280422814228242283422844228542286422874228842289422904229142292422934229442295422964229742298422994230042301423024230342304423054230642307423084230942310423114231242313423144231542316423174231842319423204232142322423234232442325423264232742328423294233042331423324233342334423354233642337423384233942340423414234242343423444234542346423474234842349423504235142352423534235442355423564235742358423594236042361423624236342364423654236642367423684236942370423714237242373423744237542376423774237842379423804238142382423834238442385423864238742388423894239042391423924239342394423954239642397423984239942400424014240242403424044240542406424074240842409424104241142412424134241442415424164241742418424194242042421424224242342424424254242642427424284242942430424314243242433424344243542436424374243842439424404244142442424434244442445424464244742448424494245042451424524245342454424554245642457424584245942460424614246242463424644246542466424674246842469424704247142472424734247442475424764247742478424794248042481424824248342484424854248642487424884248942490424914249242493424944249542496424974249842499425004250142502425034250442505425064250742508425094251042511425124251342514425154251642517425184251942520425214252242523425244252542526425274252842529425304253142532425334253442535425364253742538425394254042541425424254342544425454254642547425484254942550425514255242553425544255542556425574255842559425604256142562425634256442565425664256742568425694257042571425724257342574425754257642577425784257942580425814258242583425844258542586425874258842589425904259142592425934259442595425964259742598425994260042601426024260342604426054260642607426084260942610426114261242613426144261542616426174261842619426204262142622426234262442625426264262742628426294263042631426324263342634426354263642637426384263942640426414264242643426444264542646426474264842649426504265142652426534265442655426564265742658426594266042661426624266342664426654266642667426684266942670426714267242673426744267542676426774267842679426804268142682426834268442685426864268742688426894269042691426924269342694426954269642697426984269942700427014270242703427044270542706427074270842709427104271142712427134271442715427164271742718427194272042721427224272342724427254272642727427284272942730427314273242733427344273542736427374273842739427404274142742427434274442745427464274742748427494275042751427524275342754427554275642757427584275942760427614276242763427644276542766427674276842769427704277142772427734277442775427764277742778427794278042781427824278342784427854278642787427884278942790427914279242793427944279542796427974279842799428004280142802428034280442805428064280742808428094281042811428124281342814428154281642817428184281942820428214282242823428244282542826428274282842829428304283142832428334283442835428364283742838428394284042841428424284342844428454284642847428484284942850428514285242853428544285542856428574285842859428604286142862428634286442865428664286742868428694287042871428724287342874428754287642877428784287942880428814288242883428844288542886428874288842889428904289142892428934289442895428964289742898428994290042901429024290342904429054290642907429084290942910429114291242913429144291542916429174291842919429204292142922429234292442925429264292742928429294293042931429324293342934429354293642937429384293942940429414294242943429444294542946429474294842949429504295142952429534295442955429564295742958429594296042961429624296342964429654296642967429684296942970429714297242973429744297542976429774297842979429804298142982429834298442985429864298742988429894299042991429924299342994429954299642997429984299943000430014300243003430044300543006430074300843009430104301143012430134301443015430164301743018430194302043021430224302343024430254302643027430284302943030430314303243033430344303543036430374303843039430404304143042430434304443045430464304743048430494305043051430524305343054430554305643057430584305943060430614306243063430644306543066430674306843069430704307143072430734307443075430764307743078430794308043081430824308343084430854308643087430884308943090430914309243093430944309543096430974309843099431004310143102431034310443105431064310743108431094311043111431124311343114431154311643117431184311943120431214312243123431244312543126431274312843129431304313143132431334313443135431364313743138431394314043141431424314343144431454314643147431484314943150431514315243153431544315543156431574315843159431604316143162431634316443165431664316743168431694317043171431724317343174431754317643177431784317943180431814318243183431844318543186431874318843189431904319143192431934319443195431964319743198431994320043201432024320343204432054320643207432084320943210432114321243213432144321543216432174321843219432204322143222432234322443225432264322743228432294323043231432324323343234432354323643237432384323943240432414324243243432444324543246432474324843249432504325143252432534325443255432564325743258432594326043261432624326343264432654326643267432684326943270432714327243273432744327543276432774327843279432804328143282432834328443285432864328743288432894329043291432924329343294432954329643297432984329943300433014330243303433044330543306433074330843309433104331143312433134331443315433164331743318433194332043321433224332343324433254332643327433284332943330433314333243333433344333543336433374333843339433404334143342433434334443345433464334743348433494335043351433524335343354433554335643357433584335943360433614336243363433644336543366433674336843369433704337143372433734337443375433764337743378433794338043381433824338343384433854338643387433884338943390433914339243393433944339543396433974339843399434004340143402434034340443405434064340743408434094341043411434124341343414434154341643417434184341943420434214342243423434244342543426434274342843429434304343143432434334343443435434364343743438434394344043441434424344343444434454344643447434484344943450434514345243453434544345543456434574345843459434604346143462434634346443465434664346743468434694347043471434724347343474434754347643477434784347943480434814348243483434844348543486434874348843489434904349143492434934349443495434964349743498434994350043501435024350343504435054350643507435084350943510435114351243513435144351543516435174351843519435204352143522435234352443525435264352743528435294353043531435324353343534435354353643537435384353943540435414354243543435444354543546435474354843549435504355143552435534355443555435564355743558435594356043561435624356343564435654356643567435684356943570435714357243573435744357543576435774357843579435804358143582435834358443585435864358743588435894359043591435924359343594435954359643597435984359943600436014360243603436044360543606436074360843609436104361143612436134361443615436164361743618436194362043621436224362343624436254362643627436284362943630436314363243633436344363543636436374363843639436404364143642436434364443645436464364743648436494365043651436524365343654436554365643657436584365943660436614366243663436644366543666436674366843669436704367143672436734367443675436764367743678436794368043681436824368343684436854368643687436884368943690436914369243693436944369543696436974369843699437004370143702437034370443705437064370743708437094371043711437124371343714437154371643717437184371943720437214372243723437244372543726437274372843729437304373143732437334373443735437364373743738437394374043741437424374343744437454374643747437484374943750437514375243753437544375543756437574375843759437604376143762437634376443765437664376743768437694377043771437724377343774437754377643777437784377943780437814378243783437844378543786437874378843789437904379143792437934379443795437964379743798437994380043801438024380343804438054380643807438084380943810438114381243813438144381543816438174381843819438204382143822438234382443825438264382743828438294383043831438324383343834438354383643837438384383943840438414384243843438444384543846438474384843849438504385143852438534385443855438564385743858438594386043861438624386343864438654386643867438684386943870438714387243873438744387543876438774387843879438804388143882438834388443885438864388743888438894389043891438924389343894438954389643897438984389943900439014390243903439044390543906439074390843909439104391143912439134391443915439164391743918439194392043921439224392343924439254392643927439284392943930439314393243933439344393543936439374393843939439404394143942439434394443945439464394743948439494395043951439524395343954439554395643957439584395943960439614396243963439644396543966439674396843969439704397143972439734397443975439764397743978439794398043981439824398343984439854398643987439884398943990439914399243993439944399543996439974399843999440004400144002440034400444005440064400744008440094401044011440124401344014440154401644017440184401944020440214402244023440244402544026440274402844029440304403144032440334403444035440364403744038440394404044041440424404344044440454404644047440484404944050440514405244053440544405544056440574405844059440604406144062440634406444065440664406744068440694407044071440724407344074440754407644077440784407944080440814408244083440844408544086440874408844089440904409144092440934409444095440964409744098440994410044101441024410344104441054410644107441084410944110441114411244113441144411544116441174411844119441204412144122441234412444125441264412744128441294413044131441324413344134441354413644137441384413944140441414414244143441444414544146441474414844149441504415144152441534415444155441564415744158441594416044161441624416344164441654416644167441684416944170441714417244173441744417544176441774417844179441804418144182441834418444185441864418744188441894419044191441924419344194441954419644197441984419944200442014420244203442044420544206442074420844209442104421144212442134421444215442164421744218442194422044221442224422344224442254422644227442284422944230442314423244233442344423544236442374423844239442404424144242442434424444245442464424744248442494425044251442524425344254442554425644257442584425944260442614426244263442644426544266442674426844269442704427144272442734427444275442764427744278442794428044281442824428344284442854428644287442884428944290442914429244293442944429544296442974429844299443004430144302443034430444305443064430744308443094431044311443124431344314443154431644317443184431944320443214432244323443244432544326443274432844329443304433144332443334433444335443364433744338443394434044341443424434344344443454434644347443484434944350443514435244353443544435544356443574435844359443604436144362443634436444365443664436744368443694437044371443724437344374443754437644377443784437944380443814438244383443844438544386443874438844389443904439144392443934439444395443964439744398443994440044401444024440344404444054440644407444084440944410444114441244413444144441544416444174441844419444204442144422444234442444425444264442744428444294443044431444324443344434444354443644437444384443944440444414444244443444444444544446444474444844449444504445144452444534445444455444564445744458444594446044461444624446344464444654446644467444684446944470444714447244473444744447544476444774447844479444804448144482444834448444485444864448744488444894449044491444924449344494444954449644497444984449944500445014450244503445044450544506445074450844509445104451144512445134451444515445164451744518445194452044521445224452344524445254452644527445284452944530445314453244533445344453544536445374453844539445404454144542445434454444545445464454744548445494455044551445524455344554445554455644557445584455944560445614456244563445644456544566445674456844569445704457144572445734457444575445764457744578445794458044581445824458344584445854458644587445884458944590445914459244593445944459544596445974459844599446004460144602446034460444605446064460744608446094461044611446124461344614446154461644617446184461944620446214462244623446244462544626446274462844629446304463144632446334463444635446364463744638446394464044641446424464344644446454464644647446484464944650446514465244653446544465544656446574465844659446604466144662446634466444665446664466744668446694467044671446724467344674446754467644677446784467944680446814468244683446844468544686446874468844689446904469144692446934469444695446964469744698446994470044701447024470344704447054470644707447084470944710447114471244713447144471544716447174471844719447204472144722447234472444725447264472744728447294473044731447324473344734447354473644737447384473944740447414474244743447444474544746447474474844749447504475144752447534475444755447564475744758447594476044761447624476344764447654476644767447684476944770447714477244773447744477544776447774477844779447804478144782447834478444785447864478744788447894479044791447924479344794447954479644797447984479944800448014480244803448044480544806448074480844809448104481144812448134481444815448164481744818448194482044821448224482344824448254482644827448284482944830448314483244833448344483544836448374483844839448404484144842448434484444845448464484744848448494485044851448524485344854448554485644857448584485944860448614486244863448644486544866448674486844869448704487144872448734487444875448764487744878448794488044881448824488344884448854488644887448884488944890448914489244893448944489544896448974489844899449004490144902449034490444905449064490744908449094491044911449124491344914449154491644917449184491944920449214492244923449244492544926449274492844929449304493144932449334493444935449364493744938449394494044941449424494344944449454494644947449484494944950449514495244953449544495544956449574495844959449604496144962449634496444965449664496744968449694497044971449724497344974449754497644977449784497944980449814498244983449844498544986449874498844989449904499144992449934499444995449964499744998449994500045001450024500345004450054500645007450084500945010450114501245013450144501545016450174501845019450204502145022450234502445025450264502745028450294503045031450324503345034450354503645037450384503945040450414504245043450444504545046450474504845049450504505145052450534505445055450564505745058450594506045061450624506345064450654506645067450684506945070450714507245073450744507545076450774507845079450804508145082450834508445085450864508745088450894509045091450924509345094450954509645097450984509945100451014510245103451044510545106451074510845109451104511145112451134511445115451164511745118451194512045121451224512345124451254512645127451284512945130451314513245133451344513545136451374513845139451404514145142451434514445145451464514745148451494515045151451524515345154451554515645157451584515945160451614516245163451644516545166451674516845169451704517145172451734517445175451764517745178451794518045181451824518345184451854518645187451884518945190451914519245193451944519545196451974519845199452004520145202452034520445205452064520745208452094521045211452124521345214452154521645217452184521945220452214522245223452244522545226452274522845229452304523145232452334523445235452364523745238452394524045241452424524345244452454524645247452484524945250452514525245253452544525545256452574525845259452604526145262452634526445265452664526745268452694527045271452724527345274452754527645277452784527945280452814528245283452844528545286452874528845289452904529145292452934529445295452964529745298452994530045301453024530345304453054530645307453084530945310453114531245313453144531545316453174531845319453204532145322453234532445325453264532745328453294533045331453324533345334453354533645337453384533945340453414534245343453444534545346453474534845349453504535145352453534535445355453564535745358453594536045361453624536345364453654536645367453684536945370453714537245373453744537545376453774537845379453804538145382453834538445385453864538745388453894539045391453924539345394453954539645397453984539945400454014540245403454044540545406454074540845409454104541145412454134541445415454164541745418454194542045421454224542345424454254542645427454284542945430454314543245433454344543545436454374543845439454404544145442454434544445445454464544745448454494545045451454524545345454454554545645457454584545945460454614546245463454644546545466454674546845469454704547145472454734547445475454764547745478454794548045481454824548345484454854548645487454884548945490454914549245493454944549545496454974549845499455004550145502455034550445505455064550745508455094551045511455124551345514455154551645517455184551945520455214552245523455244552545526455274552845529455304553145532455334553445535455364553745538455394554045541455424554345544455454554645547455484554945550455514555245553455544555545556455574555845559455604556145562455634556445565455664556745568455694557045571455724557345574455754557645577455784557945580455814558245583455844558545586455874558845589455904559145592455934559445595455964559745598455994560045601456024560345604456054560645607456084560945610456114561245613456144561545616456174561845619456204562145622456234562445625456264562745628456294563045631456324563345634456354563645637456384563945640456414564245643456444564545646456474564845649456504565145652456534565445655456564565745658456594566045661456624566345664456654566645667456684566945670456714567245673456744567545676456774567845679456804568145682456834568445685456864568745688456894569045691456924569345694456954569645697456984569945700457014570245703457044570545706457074570845709457104571145712457134571445715457164571745718457194572045721457224572345724457254572645727457284572945730457314573245733457344573545736457374573845739457404574145742457434574445745457464574745748457494575045751457524575345754457554575645757457584575945760457614576245763457644576545766457674576845769457704577145772457734577445775457764577745778457794578045781457824578345784457854578645787457884578945790457914579245793457944579545796457974579845799458004580145802458034580445805458064580745808458094581045811458124581345814458154581645817458184581945820458214582245823458244582545826458274582845829458304583145832458334583445835458364583745838458394584045841458424584345844458454584645847458484584945850458514585245853458544585545856458574585845859458604586145862458634586445865458664586745868458694587045871458724587345874458754587645877458784587945880458814588245883458844588545886458874588845889458904589145892458934589445895458964589745898458994590045901459024590345904459054590645907459084590945910459114591245913459144591545916459174591845919459204592145922459234592445925459264592745928459294593045931459324593345934459354593645937459384593945940459414594245943459444594545946459474594845949459504595145952459534595445955459564595745958459594596045961459624596345964459654596645967459684596945970459714597245973459744597545976459774597845979459804598145982459834598445985459864598745988459894599045991459924599345994459954599645997459984599946000460014600246003460044600546006460074600846009460104601146012460134601446015
  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. #include <esp_log.h>
  112. #elif defined(WOLFSSL_ZEPHYR)
  113. #include <stdio.h>
  114. #define printf printk
  115. #elif defined(MICRIUM)
  116. #include <os.h>
  117. #if (OS_VERSION < 50000)
  118. #include <bsp_ser.h>
  119. void BSP_Ser_Printf (CPU_CHAR* format, ...);
  120. #undef printf
  121. #define printf BSP_Ser_Printf
  122. #else
  123. #include <stdio.h>
  124. #endif
  125. #elif defined(WOLFSSL_PB)
  126. #include <stdarg.h>
  127. int wolfssl_pb_print(const char*, ...);
  128. #undef printf
  129. #define printf wolfssl_pb_print
  130. #elif defined(WOLFSSL_TELIT_M2MB)
  131. #include "wolfssl/wolfcrypt/wc_port.h" /* for m2mb headers */
  132. #include "m2m_log.h" /* for M2M_LOG_INFO - not standard API */
  133. /* remap printf */
  134. #undef printf
  135. #define printf M2M_LOG_INFO
  136. /* OS requires occasional sleep() */
  137. #ifndef TEST_SLEEP_MS
  138. #define TEST_SLEEP_MS 50
  139. #endif
  140. #define TEST_SLEEP() m2mb_os_taskSleep(M2MB_OS_MS2TICKS(TEST_SLEEP_MS))
  141. /* don't use file system for these tests, since ./certs dir isn't loaded */
  142. #undef NO_FILESYSTEM
  143. #define NO_FILESYSTEM
  144. #elif defined(THREADX) && !defined(WOLFSSL_WICED) && \
  145. !defined(THREADX_NO_DC_PRINTF)
  146. #ifndef NETOS
  147. /* since just testing, use THREADX log printf instead (NETOS prototypes
  148. * this elsewhere) */
  149. int dc_log_printf(char*, ...);
  150. #endif
  151. #undef printf
  152. #define printf dc_log_printf
  153. #elif defined(ANDROID)
  154. #ifdef XMALLOC_USER
  155. #include <stdlib.h> /* we're using malloc / free direct here */
  156. #endif
  157. #ifndef STRING_USER
  158. #include <stdio.h>
  159. #endif
  160. #include <android/log.h>
  161. #ifdef ANDROID_V454 /* See fips/android/wolfCrypt_v454_android */
  162. #ifndef NO_FILESYSTEM
  163. #define NO_FILESYSTEM /* Turn off tests that want to call SaveDerAndPem() */
  164. #endif
  165. #else
  166. #define printf(...) \
  167. __android_log_print(ANDROID_LOG_DEBUG, "[WOLFCRYPT]", __VA_ARGS__)
  168. #define fprintf(fp, ...) \
  169. __android_log_print(ANDROID_LOG_DEBUG, "[WOLFCRYPT]", __VA_ARGS__)
  170. #endif
  171. #elif defined(WOLFSSL_DEOS)
  172. #include <printx.h>
  173. #undef printf
  174. #define printf printx
  175. #else
  176. #ifdef XMALLOC_USER
  177. #include <stdlib.h> /* we're using malloc / free direct here */
  178. #endif
  179. #if !defined(STRING_USER) && !defined(WOLFSSL_LINUXKM)
  180. #include <stdio.h>
  181. #endif
  182. #if defined(WOLFSSL_LINUXKM) && !defined(WOLFSSL_LINUXKM_VERBOSE_DEBUG)
  183. #undef printf
  184. #define printf(...) ({})
  185. #endif
  186. /* enable way for customer to override test/bench printf */
  187. #ifdef XPRINTF
  188. #undef printf
  189. #define printf XPRINTF
  190. #elif !defined(printf)
  191. /* arrange for printf() to flush after every message -- this assures
  192. * redirected output (to a log file) records progress right up to the
  193. * moment of a crash/abort(); otherwise anything queued in stdout would
  194. * be lost.
  195. */
  196. #define printf(...) ( printf(__VA_ARGS__), fflush(stdout) )
  197. #endif
  198. #endif
  199. #endif /* !WOLFSSL_LOG_PRINTF */
  200. #include <wolfssl/wolfcrypt/memory.h>
  201. #include <wolfssl/wolfcrypt/wc_port.h>
  202. #include <wolfssl/wolfcrypt/logging.h>
  203. #include <wolfssl/wolfcrypt/types.h>
  204. #include <wolfssl/wolfcrypt/asn.h>
  205. #include <wolfssl/wolfcrypt/md2.h>
  206. #include <wolfssl/wolfcrypt/md5.h>
  207. #include <wolfssl/wolfcrypt/md4.h>
  208. #include <wolfssl/wolfcrypt/sha.h>
  209. #include <wolfssl/wolfcrypt/sha256.h>
  210. #include <wolfssl/wolfcrypt/sha512.h>
  211. #include <wolfssl/wolfcrypt/rc2.h>
  212. #include <wolfssl/wolfcrypt/arc4.h>
  213. #if defined(WC_NO_RNG)
  214. #include <wolfssl/wolfcrypt/integer.h>
  215. #else
  216. #include <wolfssl/wolfcrypt/random.h>
  217. #endif
  218. #include <wolfssl/wolfcrypt/coding.h>
  219. #include <wolfssl/wolfcrypt/signature.h>
  220. #include <wolfssl/wolfcrypt/rsa.h>
  221. #include <wolfssl/wolfcrypt/des3.h>
  222. #include <wolfssl/wolfcrypt/aes.h>
  223. #include <wolfssl/wolfcrypt/wc_encrypt.h>
  224. #include <wolfssl/wolfcrypt/cmac.h>
  225. #include <wolfssl/wolfcrypt/siphash.h>
  226. #include <wolfssl/wolfcrypt/poly1305.h>
  227. #include <wolfssl/wolfcrypt/camellia.h>
  228. #include <wolfssl/wolfcrypt/hmac.h>
  229. #include <wolfssl/wolfcrypt/kdf.h>
  230. #include <wolfssl/wolfcrypt/dh.h>
  231. #include <wolfssl/wolfcrypt/dsa.h>
  232. #include <wolfssl/wolfcrypt/srp.h>
  233. #include <wolfssl/wolfcrypt/chacha.h>
  234. #include <wolfssl/wolfcrypt/chacha20_poly1305.h>
  235. #include <wolfssl/wolfcrypt/pwdbased.h>
  236. #include <wolfssl/wolfcrypt/ripemd.h>
  237. #include <wolfssl/wolfcrypt/error-crypt.h>
  238. #ifdef HAVE_ECC
  239. #include <wolfssl/wolfcrypt/ecc.h>
  240. #endif
  241. #ifdef HAVE_CURVE25519
  242. #include <wolfssl/wolfcrypt/curve25519.h>
  243. #endif
  244. #ifdef HAVE_ED25519
  245. #include <wolfssl/wolfcrypt/ed25519.h>
  246. #endif
  247. #ifdef HAVE_CURVE448
  248. #include <wolfssl/wolfcrypt/curve448.h>
  249. #endif
  250. #ifdef HAVE_ED448
  251. #include <wolfssl/wolfcrypt/ed448.h>
  252. #endif
  253. #ifdef WOLFSSL_HAVE_KYBER
  254. #include <wolfssl/wolfcrypt/kyber.h>
  255. #ifdef WOLFSSL_WC_KYBER
  256. #include <wolfssl/wolfcrypt/wc_kyber.h>
  257. #endif
  258. #if defined(HAVE_LIBOQS) || defined(HAVE_PQM4)
  259. #include <wolfssl/wolfcrypt/ext_kyber.h>
  260. #endif
  261. #endif
  262. #ifdef WOLFCRYPT_HAVE_ECCSI
  263. #include <wolfssl/wolfcrypt/eccsi.h>
  264. #endif
  265. #ifdef WOLFCRYPT_HAVE_SAKKE
  266. #include <wolfssl/wolfcrypt/sakke.h>
  267. #endif
  268. #if defined(HAVE_BLAKE2) || defined(HAVE_BLAKE2S)
  269. #include <wolfssl/wolfcrypt/blake2.h>
  270. #endif
  271. #ifdef WOLFSSL_SHA3
  272. #include <wolfssl/wolfcrypt/sha3.h>
  273. #endif
  274. #ifdef HAVE_LIBZ
  275. #include <wolfssl/wolfcrypt/compress.h>
  276. #endif
  277. #ifdef HAVE_PKCS7
  278. #include <wolfssl/wolfcrypt/pkcs7.h>
  279. #endif
  280. #ifdef HAVE_FIPS
  281. #include <wolfssl/wolfcrypt/fips_test.h>
  282. #endif
  283. #ifdef HAVE_SELFTEST
  284. #include <wolfssl/wolfcrypt/selftest.h>
  285. #endif
  286. #ifdef WOLFSSL_ASYNC_CRYPT
  287. #include <wolfssl/wolfcrypt/async.h>
  288. #endif
  289. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  290. #include <wolfssl/wolfcrypt/logging.h>
  291. #endif
  292. #ifdef WOLFSSL_CAAM
  293. #include <wolfssl/wolfcrypt/port/caam/wolfcaam.h>
  294. #endif
  295. #ifdef WOLF_CRYPTO_CB
  296. #include <wolfssl/wolfcrypt/cryptocb.h>
  297. #ifdef HAVE_INTEL_QA_SYNC
  298. #include <wolfssl/wolfcrypt/port/intel/quickassist_sync.h>
  299. #endif
  300. #ifdef HAVE_CAVIUM_OCTEON_SYNC
  301. #include <wolfssl/wolfcrypt/port/cavium/cavium_octeon_sync.h>
  302. #endif
  303. #endif
  304. #ifdef _MSC_VER
  305. /* 4996 warning to use MS extensions e.g., strcpy_s instead of strncpy */
  306. #pragma warning(disable: 4996)
  307. #endif
  308. #ifdef OPENSSL_EXTRA
  309. #ifndef WOLFCRYPT_ONLY
  310. #include <wolfssl/openssl/evp.h>
  311. #include <wolfssl/openssl/hmac.h>
  312. #endif
  313. #include <wolfssl/openssl/rand.h>
  314. #include <wolfssl/openssl/aes.h>
  315. #include <wolfssl/openssl/des.h>
  316. #endif
  317. #if defined(NO_FILESYSTEM) || defined(WC_NO_RNG)
  318. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  319. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096)
  320. #define USE_CERT_BUFFERS_2048
  321. #endif
  322. #if !defined(USE_CERT_BUFFERS_256)
  323. #define USE_CERT_BUFFERS_256
  324. #endif
  325. #endif
  326. #if defined(WOLFSSL_CERT_GEN) && (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES))
  327. #define ENABLE_ECC384_CERT_GEN_TEST
  328. #endif
  329. #include <wolfssl/certs_test.h>
  330. #ifdef DEVKITPRO
  331. #include <wiiuse/wpad.h>
  332. #endif
  333. #ifdef WOLFSSL_STATIC_MEMORY
  334. static WOLFSSL_HEAP_HINT* HEAP_HINT;
  335. #else
  336. #define HEAP_HINT NULL
  337. #endif /* WOLFSSL_STATIC_MEMORY */
  338. /* these cases do not have intermediate hashing support */
  339. #if (defined(WOLFSSL_AFALG_XILINX_SHA3) && !defined(WOLFSSL_AFALG_HASH_KEEP)) \
  340. && !defined(WOLFSSL_XILINX_CRYPT) || defined(WOLFSSL_XILINX_CRYPT_VERSAL)
  341. #define NO_INTM_HASH_TEST
  342. #endif
  343. #if defined(WOLFSSL_RENESAS_TSIP) || defined(WOLFSSL_RENESAS_SCEPROTECT) || \
  344. defined(WOLFSSL_SECO_CAAM)
  345. #define HASH_SIZE_LIMIT
  346. #endif
  347. #if defined(WOLFSSL_CERT_GEN) && (!defined(NO_RSA) || defined(HAVE_ECC)) || \
  348. (defined(WOLFSSL_TEST_CERT) && (defined(HAVE_ED25519) || defined(HAVE_ED448)))
  349. static void initDefaultName(void);
  350. #endif
  351. /* for async devices */
  352. #ifdef WOLFSSL_CAAM_DEVID
  353. static int devId = WOLFSSL_CAAM_DEVID;
  354. #else
  355. static int devId = INVALID_DEVID;
  356. #endif
  357. #ifdef HAVE_WNR
  358. const char* wnrConfigFile = "wnr-example.conf";
  359. #endif
  360. #define TEST_STRING "Everyone gets Friday off."
  361. #define TEST_STRING_SZ 25
  362. typedef struct testVector {
  363. const char* input;
  364. const char* output;
  365. size_t inLen;
  366. size_t outLen;
  367. } testVector;
  368. #ifndef WOLFSSL_TEST_SUBROUTINE
  369. #define WOLFSSL_TEST_SUBROUTINE
  370. #endif
  371. PRAGMA_GCC("GCC diagnostic ignored \"-Wunused-function\"")
  372. PRAGMA_CLANG("clang diagnostic ignored \"-Wunused-function\"")
  373. WOLFSSL_TEST_SUBROUTINE int error_test(void);
  374. WOLFSSL_TEST_SUBROUTINE int base64_test(void);
  375. WOLFSSL_TEST_SUBROUTINE int base16_test(void);
  376. WOLFSSL_TEST_SUBROUTINE int asn_test(void);
  377. WOLFSSL_TEST_SUBROUTINE int md2_test(void);
  378. WOLFSSL_TEST_SUBROUTINE int md5_test(void);
  379. WOLFSSL_TEST_SUBROUTINE int md4_test(void);
  380. WOLFSSL_TEST_SUBROUTINE int sha_test(void);
  381. WOLFSSL_TEST_SUBROUTINE int sha224_test(void);
  382. WOLFSSL_TEST_SUBROUTINE int sha256_test(void);
  383. WOLFSSL_TEST_SUBROUTINE int sha512_test(void);
  384. WOLFSSL_TEST_SUBROUTINE int sha384_test(void);
  385. WOLFSSL_TEST_SUBROUTINE int sha3_test(void);
  386. WOLFSSL_TEST_SUBROUTINE int shake128_test(void);
  387. WOLFSSL_TEST_SUBROUTINE int shake256_test(void);
  388. WOLFSSL_TEST_SUBROUTINE int hash_test(void);
  389. WOLFSSL_TEST_SUBROUTINE int hmac_md5_test(void);
  390. WOLFSSL_TEST_SUBROUTINE int hmac_sha_test(void);
  391. WOLFSSL_TEST_SUBROUTINE int hmac_sha224_test(void);
  392. WOLFSSL_TEST_SUBROUTINE int hmac_sha256_test(void);
  393. WOLFSSL_TEST_SUBROUTINE int hmac_sha384_test(void);
  394. WOLFSSL_TEST_SUBROUTINE int hmac_sha512_test(void);
  395. WOLFSSL_TEST_SUBROUTINE int hmac_sha3_test(void);
  396. #if defined(HAVE_HKDF) && !defined(NO_HMAC)
  397. /* hkdf_test has issue with WOLFSSL_TEST_SUBROUTINE set on Xilinx with afalg */
  398. static int hkdf_test(void);
  399. #endif
  400. WOLFSSL_TEST_SUBROUTINE int sshkdf_test(void);
  401. #ifdef WOLFSSL_TLS13
  402. WOLFSSL_TEST_SUBROUTINE int tls13_kdf_test(void);
  403. #endif
  404. WOLFSSL_TEST_SUBROUTINE int x963kdf_test(void);
  405. WOLFSSL_TEST_SUBROUTINE int arc4_test(void);
  406. #ifdef WC_RC2
  407. WOLFSSL_TEST_SUBROUTINE int rc2_test(void);
  408. #endif
  409. WOLFSSL_TEST_SUBROUTINE int chacha_test(void);
  410. WOLFSSL_TEST_SUBROUTINE int XChaCha_test(void);
  411. WOLFSSL_TEST_SUBROUTINE int chacha20_poly1305_aead_test(void);
  412. WOLFSSL_TEST_SUBROUTINE int XChaCha20Poly1305_test(void);
  413. WOLFSSL_TEST_SUBROUTINE int des_test(void);
  414. WOLFSSL_TEST_SUBROUTINE int des3_test(void);
  415. WOLFSSL_TEST_SUBROUTINE int aes_test(void);
  416. WOLFSSL_TEST_SUBROUTINE int aes192_test(void);
  417. WOLFSSL_TEST_SUBROUTINE int aes256_test(void);
  418. WOLFSSL_TEST_SUBROUTINE int aesofb_test(void);
  419. WOLFSSL_TEST_SUBROUTINE int cmac_test(void);
  420. #if defined(WOLFSSL_SIPHASH)
  421. WOLFSSL_TEST_SUBROUTINE int siphash_test(void);
  422. #endif
  423. WOLFSSL_TEST_SUBROUTINE int poly1305_test(void);
  424. WOLFSSL_TEST_SUBROUTINE int aesgcm_test(void);
  425. WOLFSSL_TEST_SUBROUTINE int aesgcm_default_test(void);
  426. WOLFSSL_TEST_SUBROUTINE int gmac_test(void);
  427. WOLFSSL_TEST_SUBROUTINE int aesccm_test(void);
  428. WOLFSSL_TEST_SUBROUTINE int aeskeywrap_test(void);
  429. WOLFSSL_TEST_SUBROUTINE int camellia_test(void);
  430. #ifdef WC_RSA_NO_PADDING
  431. WOLFSSL_TEST_SUBROUTINE int rsa_no_pad_test(void);
  432. #endif
  433. WOLFSSL_TEST_SUBROUTINE int rsa_test(void);
  434. WOLFSSL_TEST_SUBROUTINE int dh_test(void);
  435. WOLFSSL_TEST_SUBROUTINE int dsa_test(void);
  436. WOLFSSL_TEST_SUBROUTINE int srp_test(void);
  437. #ifndef WC_NO_RNG
  438. WOLFSSL_TEST_SUBROUTINE int random_test(void);
  439. #endif /* WC_NO_RNG */
  440. WOLFSSL_TEST_SUBROUTINE int pwdbased_test(void);
  441. WOLFSSL_TEST_SUBROUTINE int ripemd_test(void);
  442. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  443. WOLFSSL_TEST_SUBROUTINE int openssl_test(void); /* test mini api */
  444. WOLFSSL_TEST_SUBROUTINE int openssl_pkey0_test(void);
  445. WOLFSSL_TEST_SUBROUTINE int openssl_pkey1_test(void);
  446. WOLFSSL_TEST_SUBROUTINE int openSSL_evpMD_test(void);
  447. WOLFSSL_TEST_SUBROUTINE int openssl_evpSig_test(void);
  448. #endif
  449. WOLFSSL_TEST_SUBROUTINE int pbkdf1_test(void);
  450. WOLFSSL_TEST_SUBROUTINE int pkcs12_test(void);
  451. WOLFSSL_TEST_SUBROUTINE int pbkdf2_test(void);
  452. WOLFSSL_TEST_SUBROUTINE int scrypt_test(void);
  453. #ifdef HAVE_ECC
  454. WOLFSSL_TEST_SUBROUTINE int ecc_test(void);
  455. #if defined(HAVE_ECC_ENCRYPT) && defined(HAVE_AES_CBC) && \
  456. (defined(WOLFSSL_AES_128) || defined(WOLFSSL_AES_256))
  457. WOLFSSL_TEST_SUBROUTINE int ecc_encrypt_test(void);
  458. #endif
  459. #if defined(USE_CERT_BUFFERS_256) && !defined(WOLFSSL_ATECC508A) && \
  460. !defined(WOLFSSL_ATECC608A) && !defined(NO_ECC256) && \
  461. defined(HAVE_ECC_VERIFY) && defined(HAVE_ECC_SIGN) && \
  462. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  463. /* skip for ATECC508/608A, cannot import private key buffers */
  464. WOLFSSL_TEST_SUBROUTINE int ecc_test_buffers(void);
  465. #endif
  466. #endif
  467. #ifdef HAVE_CURVE25519
  468. WOLFSSL_TEST_SUBROUTINE int curve25519_test(void);
  469. #endif
  470. #ifdef HAVE_ED25519
  471. WOLFSSL_TEST_SUBROUTINE int ed25519_test(void);
  472. #endif
  473. #ifdef HAVE_CURVE448
  474. WOLFSSL_TEST_SUBROUTINE int curve448_test(void);
  475. #endif
  476. #ifdef HAVE_ED448
  477. WOLFSSL_TEST_SUBROUTINE int ed448_test(void);
  478. #endif
  479. #ifdef WOLFSSL_HAVE_KYBER
  480. WOLFSSL_TEST_SUBROUTINE int kyber_test(void);
  481. #endif
  482. #ifdef WOLFCRYPT_HAVE_ECCSI
  483. WOLFSSL_TEST_SUBROUTINE int eccsi_test(void);
  484. #endif
  485. #ifdef WOLFCRYPT_HAVE_SAKKE
  486. WOLFSSL_TEST_SUBROUTINE int sakke_test(void);
  487. #endif
  488. #ifdef HAVE_BLAKE2
  489. WOLFSSL_TEST_SUBROUTINE int blake2b_test(void);
  490. #endif
  491. #ifdef HAVE_BLAKE2S
  492. WOLFSSL_TEST_SUBROUTINE int blake2s_test(void);
  493. #endif
  494. #ifdef HAVE_LIBZ
  495. WOLFSSL_TEST_SUBROUTINE int compress_test(void);
  496. #endif
  497. #ifdef HAVE_PKCS7
  498. #ifndef NO_PKCS7_ENCRYPTED_DATA
  499. WOLFSSL_TEST_SUBROUTINE int pkcs7encrypted_test(void);
  500. #endif
  501. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  502. WOLFSSL_TEST_SUBROUTINE int pkcs7compressed_test(void);
  503. #endif
  504. WOLFSSL_TEST_SUBROUTINE int pkcs7signed_test(void);
  505. WOLFSSL_TEST_SUBROUTINE int pkcs7enveloped_test(void);
  506. #if defined(HAVE_AESGCM) || defined(HAVE_AESCCM)
  507. WOLFSSL_TEST_SUBROUTINE int pkcs7authenveloped_test(void);
  508. #endif
  509. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  510. WOLFSSL_TEST_SUBROUTINE int pkcs7callback_test(byte* cert, word32 certSz, byte* key,
  511. word32 keySz);
  512. #endif
  513. #endif
  514. #if !defined(NO_ASN_TIME) && !defined(NO_RSA) && defined(WOLFSSL_TEST_CERT) && \
  515. !defined(NO_FILESYSTEM)
  516. WOLFSSL_TEST_SUBROUTINE int cert_test(void);
  517. #endif
  518. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_TEST_CERT) && \
  519. !defined(NO_FILESYSTEM) && defined(WOLFSSL_CERT_GEN)
  520. WOLFSSL_TEST_SUBROUTINE int certext_test(void);
  521. #endif
  522. #if defined(WOLFSSL_CERT_GEN_CACHE) && defined(WOLFSSL_TEST_CERT) && \
  523. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  524. WOLFSSL_TEST_SUBROUTINE int decodedCertCache_test(void);
  525. #endif
  526. WOLFSSL_TEST_SUBROUTINE int memory_test(void);
  527. #ifdef HAVE_VALGRIND
  528. WOLFSSL_TEST_SUBROUTINE int mp_test(void);
  529. #endif
  530. #if defined(WOLFSSL_PUBLIC_MP) && defined(WOLFSSL_KEY_GEN)
  531. WOLFSSL_TEST_SUBROUTINE int prime_test(void);
  532. #endif
  533. #if defined(ASN_BER_TO_DER) && \
  534. (defined(WOLFSSL_TEST_CERT) || defined(OPENSSL_EXTRA) || \
  535. defined(OPENSSL_EXTRA_X509_SMALL))
  536. WOLFSSL_TEST_SUBROUTINE int berder_test(void);
  537. #endif
  538. WOLFSSL_TEST_SUBROUTINE int logging_test(void);
  539. #if !defined(NO_ASN) && !defined(NO_ASN_TIME)
  540. WOLFSSL_TEST_SUBROUTINE int time_test(void);
  541. #endif
  542. WOLFSSL_TEST_SUBROUTINE int mutex_test(void);
  543. #if defined(USE_WOLFSSL_MEMORY) && !defined(FREERTOS)
  544. WOLFSSL_TEST_SUBROUTINE int memcb_test(void);
  545. #endif
  546. #ifdef WOLFSSL_IMX6_CAAM_BLOB
  547. WOLFSSL_TEST_SUBROUTINE int blob_test(void);
  548. #endif
  549. #ifdef WOLF_CRYPTO_CB
  550. WOLFSSL_TEST_SUBROUTINE int cryptocb_test(void);
  551. #endif
  552. #ifdef WOLFSSL_CERT_PIV
  553. WOLFSSL_TEST_SUBROUTINE int certpiv_test(void);
  554. #endif
  555. #ifdef WOLFSSL_AES_SIV
  556. WOLFSSL_TEST_SUBROUTINE int aes_siv_test(void);
  557. #endif
  558. /* General big buffer size for many tests. */
  559. #define FOURK_BUF 4096
  560. #define ERROR_OUT(err, eLabel) do { ret = (err); goto eLabel; } while (0)
  561. #ifdef HAVE_STACK_SIZE
  562. static THREAD_RETURN err_sys(const char* msg, int es)
  563. #else
  564. static int err_sys(const char* msg, int es)
  565. #endif
  566. {
  567. (void)msg;
  568. (void)es;
  569. #ifdef WOLFSSL_LINUXKM
  570. lkm_printf("%s error = %d\n", msg, es);
  571. EXIT_TEST(es);
  572. #else
  573. printf("%s error = %d\n", msg, es);
  574. EXIT_TEST(-1);
  575. #endif
  576. }
  577. #ifndef HAVE_WOLFCRYPT_TEST_OPTIONS
  578. /* func_args from test.h, so don't have to pull in other stuff */
  579. typedef struct func_args {
  580. int argc;
  581. char** argv;
  582. int return_code;
  583. } func_args;
  584. #endif /* !HAVE_WOLFCRYPT_TEST_OPTIONS */
  585. #if defined(HAVE_FIPS) && !defined(WOLFSSL_LINUXKM)
  586. static void myFipsCb(int ok, int err, const char* hash)
  587. {
  588. printf("in my Fips callback, ok = %d, err = %d\n", ok, err);
  589. printf("message = %s\n", wc_GetErrorString(err));
  590. printf("hash = %s\n", hash);
  591. if (err == IN_CORE_FIPS_E) {
  592. printf("In core integrity hash check failure, copy above hash\n");
  593. printf("into verifyCore[] in fips_test.c and rebuild\n");
  594. }
  595. }
  596. #endif /* HAVE_FIPS && !WOLFSSL_LINUXKM */
  597. #ifdef WOLFSSL_STATIC_MEMORY
  598. #if defined(WOLFSSL_STATIC_MEMORY_TEST_SZ)
  599. static byte gTestMemory[WOLFSSL_STATIC_MEMORY_TEST_SZ];
  600. #elif defined(BENCH_EMBEDDED)
  601. static byte gTestMemory[14000];
  602. #elif defined(WOLFSSL_CERT_EXT)
  603. static byte gTestMemory[140000];
  604. #elif (defined(WOLFSSL_SP_MATH_ALL) || defined(USE_FAST_MATH)) && \
  605. !defined(ALT_ECC_SIZE)
  606. static byte gTestMemory[160000];
  607. #else
  608. static byte gTestMemory[80000];
  609. #endif
  610. #endif
  611. #ifdef WOLFSSL_PB
  612. static int wolfssl_pb_print(const char* msg, ...)
  613. {
  614. int ret;
  615. va_list args;
  616. char tmpBuf[80];
  617. va_start(args, msg);
  618. ret = vsprint(tmpBuf, msg, args);
  619. va_end(args);
  620. fnDumpStringToSystemLog(tmpBuf);
  621. return ret;
  622. }
  623. #endif /* WOLFSSL_PB */
  624. /* optional macro to add sleep between tests */
  625. #ifndef TEST_SLEEP
  626. #define TEST_SLEEP()
  627. #else
  628. #define TEST_PASS test_pass
  629. #include <stdarg.h> /* for var args */
  630. static WC_INLINE void test_pass(const char* fmt, ...)
  631. {
  632. va_list args;
  633. va_start(args, fmt);
  634. STACK_SIZE_CHECKPOINT_WITH_MAX_CHECK(max_relative_stack, vprintf(fmt, args));
  635. va_end(args);
  636. PRINT_HEAP_CHECKPOINT();
  637. TEST_SLEEP();
  638. ASSERT_RESTORED_VECTOR_REGISTERS(exit(1););
  639. }
  640. #endif
  641. /* set test pass output to printf if not overriden */
  642. #ifndef TEST_PASS
  643. /* redirect to printf */
  644. #define TEST_PASS(...) { \
  645. if (STACK_SIZE_CHECKPOINT_WITH_MAX_CHECK \
  646. (max_relative_stack, printf(__VA_ARGS__)) < 0) { \
  647. return err_sys("post-test check failed", -1); \
  648. } \
  649. PRINT_HEAP_CHECKPOINT(); \
  650. ASSERT_RESTORED_VECTOR_REGISTERS(exit(1);); \
  651. }
  652. #endif
  653. #ifdef HAVE_STACK_SIZE
  654. THREAD_RETURN WOLFSSL_THREAD wolfcrypt_test(void* args)
  655. #else
  656. int wolfcrypt_test(void* args)
  657. #endif
  658. {
  659. int ret;
  660. #ifdef WOLFSSL_TRACK_MEMORY_VERBOSE
  661. long heap_baselineAllocs, heap_baselineBytes;
  662. #endif
  663. STACK_SIZE_INIT();
  664. #ifdef WOLFSSL_TRACK_MEMORY_VERBOSE
  665. (void)wolfCrypt_heap_peakAllocs_checkpoint();
  666. heap_baselineAllocs = wolfCrypt_heap_peakAllocs_checkpoint();
  667. (void)wolfCrypt_heap_peakBytes_checkpoint();
  668. heap_baselineBytes = wolfCrypt_heap_peakBytes_checkpoint();
  669. #endif
  670. printf("------------------------------------------------------------------------------\n");
  671. printf(" wolfSSL version %s\n", LIBWOLFSSL_VERSION_STRING);
  672. printf("------------------------------------------------------------------------------\n");
  673. if (args) {
  674. #ifdef HAVE_WOLFCRYPT_TEST_OPTIONS
  675. int ch;
  676. #endif
  677. ((func_args*)args)->return_code = -1; /* error state */
  678. #ifdef HAVE_WOLFCRYPT_TEST_OPTIONS
  679. while ((ch = mygetopt(((func_args*)args)->argc, ((func_args*)args)->argv, "s:m:a:h")) != -1) {
  680. switch(ch) {
  681. case 's':
  682. #ifdef HAVE_STACK_SIZE_VERBOSE
  683. max_relative_stack = (ssize_t)atoi(myoptarg);
  684. break;
  685. #else
  686. return err_sys("-s (max relative stack bytes) requires HAVE_STACK_SIZE_VERBOSE (--enable-stacksize=verbose).", -1);
  687. #endif
  688. case 'm':
  689. #ifdef WOLFSSL_TRACK_MEMORY_VERBOSE
  690. max_relative_heap_bytes = (ssize_t)atoi(myoptarg);
  691. break;
  692. #else
  693. return err_sys("-m (max relative heap memory bytes) requires WOLFSSL_TRACK_MEMORY_VERBOSE (--enable-trackmemory=verbose).", -1);
  694. #endif
  695. case 'a':
  696. #ifdef WOLFSSL_TRACK_MEMORY_VERBOSE
  697. max_relative_heap_allocs = (ssize_t)atoi(myoptarg);
  698. break;
  699. #else
  700. return err_sys("-a (max relative heap allocs) requires WOLFSSL_TRACK_MEMORY_VERBOSE (--enable-trackmemory=verbose).", -1);
  701. #endif
  702. case 'h':
  703. return err_sys("\
  704. options: [-s max_relative_stack_bytes] [-m max_relative_heap_memory_bytes]\n\
  705. [-a max_relative_heap_allocs] [-h]\n", 0);
  706. default:
  707. return err_sys("unknown test option. try -h.", -1);
  708. }
  709. }
  710. #endif
  711. }
  712. #ifdef WOLFSSL_STATIC_MEMORY
  713. if (wc_LoadStaticMemory(&HEAP_HINT, gTestMemory, sizeof(gTestMemory),
  714. WOLFMEM_GENERAL, 1) != 0) {
  715. printf("unable to load static memory.\n");
  716. return(EXIT_FAILURE);
  717. }
  718. #endif
  719. #if defined(DEBUG_WOLFSSL) && !defined(HAVE_VALGRIND)
  720. wolfSSL_Debugging_ON();
  721. #endif
  722. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  723. wc_SetLoggingHeap(HEAP_HINT);
  724. #endif
  725. #if defined(HAVE_FIPS) && !defined(WOLFSSL_LINUXKM)
  726. wolfCrypt_SetCb_fips(myFipsCb);
  727. #endif
  728. #if !defined(NO_BIG_INT)
  729. if (CheckCtcSettings() != 1) {
  730. printf("Sizeof mismatch (build) %x != (run) %lx\n",
  731. CTC_SETTINGS, (unsigned long)CheckRunTimeSettings());
  732. return err_sys("Build vs runtime math mismatch\n", -1000);
  733. }
  734. #if defined(USE_FAST_MATH) && \
  735. (!defined(NO_RSA) || !defined(NO_DH) || defined(HAVE_ECC))
  736. if (CheckFastMathSettings() != 1)
  737. return err_sys("Build vs runtime fastmath FP_MAX_BITS mismatch\n",
  738. -1001);
  739. #endif /* USE_FAST_MATH */
  740. #endif /* !NO_BIG_INT */
  741. #if defined(WOLFSSL_CERT_GEN) && (!defined(NO_RSA) || defined(HAVE_ECC)) || \
  742. (defined(WOLFSSL_TEST_CERT) && (defined(HAVE_ED25519) || defined(HAVE_ED448)))
  743. initDefaultName();
  744. #endif
  745. #ifdef WOLFSSL_ASYNC_CRYPT
  746. ret = wolfAsync_DevOpen(&devId);
  747. if (ret < 0) {
  748. printf("Async device open failed\nRunning without async\n");
  749. }
  750. #else
  751. (void)devId;
  752. #endif /* WOLFSSL_ASYNC_CRYPT */
  753. #ifdef WOLF_CRYPTO_CB
  754. #ifdef HAVE_INTEL_QA_SYNC
  755. devId = wc_CryptoCb_InitIntelQa();
  756. if (INVALID_DEVID == devId) {
  757. printf("Couldn't init the Intel QA\n");
  758. }
  759. #endif
  760. #ifdef HAVE_CAVIUM_OCTEON_SYNC
  761. devId = wc_CryptoCb_InitOcteon();
  762. if (INVALID_DEVID == devId) {
  763. printf("Couldn't init the Cavium Octeon\n");
  764. }
  765. #endif
  766. #endif
  767. #ifdef HAVE_SELFTEST
  768. if ( (ret = wolfCrypt_SelfTest()) != 0)
  769. return err_sys("CAVP selftest failed!\n", ret);
  770. else
  771. TEST_PASS("CAVP selftest passed!\n");
  772. #endif
  773. if ( (ret = error_test()) != 0)
  774. return err_sys("error test failed!\n", ret);
  775. else
  776. TEST_PASS("error test passed!\n");
  777. if ( (ret = memory_test()) != 0)
  778. return err_sys("MEMORY test failed!\n", ret);
  779. else
  780. TEST_PASS("MEMORY test passed!\n");
  781. #ifndef NO_CODING
  782. if ( (ret = base64_test()) != 0)
  783. return err_sys("base64 test failed!\n", ret);
  784. else
  785. TEST_PASS("base64 test passed!\n");
  786. #ifdef WOLFSSL_BASE16
  787. if ( (ret = base16_test()) != 0)
  788. return err_sys("base16 test failed!\n", ret);
  789. else
  790. TEST_PASS("base16 test passed!\n");
  791. #endif
  792. #endif /* !NO_CODING */
  793. #ifndef NO_ASN
  794. if ( (ret = asn_test()) != 0)
  795. return err_sys("asn test failed!\n", ret);
  796. else
  797. TEST_PASS("asn test passed!\n");
  798. #endif
  799. #ifndef WC_NO_RNG
  800. if ( (ret = random_test()) != 0)
  801. return err_sys("RANDOM test failed!\n", ret);
  802. else
  803. TEST_PASS("RANDOM test passed!\n");
  804. #endif /* WC_NO_RNG */
  805. #ifndef NO_MD5
  806. if ( (ret = md5_test()) != 0)
  807. return err_sys("MD5 test failed!\n", ret);
  808. else
  809. TEST_PASS("MD5 test passed!\n");
  810. #endif
  811. #ifdef WOLFSSL_MD2
  812. if ( (ret = md2_test()) != 0)
  813. return err_sys("MD2 test failed!\n", ret);
  814. else
  815. TEST_PASS("MD2 test passed!\n");
  816. #endif
  817. #ifndef NO_MD4
  818. if ( (ret = md4_test()) != 0)
  819. return err_sys("MD4 test failed!\n", ret);
  820. else
  821. TEST_PASS("MD4 test passed!\n");
  822. #endif
  823. #ifndef NO_SHA
  824. if ( (ret = sha_test()) != 0)
  825. return err_sys("SHA test failed!\n", ret);
  826. else
  827. TEST_PASS("SHA test passed!\n");
  828. #endif
  829. #ifdef WOLFSSL_SHA224
  830. if ( (ret = sha224_test()) != 0)
  831. return err_sys("SHA-224 test failed!\n", ret);
  832. else
  833. TEST_PASS("SHA-224 test passed!\n");
  834. #endif
  835. #ifndef NO_SHA256
  836. if ( (ret = sha256_test()) != 0)
  837. return err_sys("SHA-256 test failed!\n", ret);
  838. else
  839. TEST_PASS("SHA-256 test passed!\n");
  840. #endif
  841. #ifdef WOLFSSL_SHA384
  842. if ( (ret = sha384_test()) != 0)
  843. return err_sys("SHA-384 test failed!\n", ret);
  844. else
  845. TEST_PASS("SHA-384 test passed!\n");
  846. #endif
  847. #ifdef WOLFSSL_SHA512
  848. if ( (ret = sha512_test()) != 0)
  849. return err_sys("SHA-512 test failed!\n", ret);
  850. else
  851. TEST_PASS("SHA-512 test passed!\n");
  852. #endif
  853. #ifdef WOLFSSL_SHA3
  854. if ( (ret = sha3_test()) != 0)
  855. return err_sys("SHA-3 test failed!\n", ret);
  856. else
  857. TEST_PASS("SHA-3 test passed!\n");
  858. #endif
  859. #ifdef WOLFSSL_SHAKE128
  860. if ( (ret = shake128_test()) != 0)
  861. return err_sys("SHAKE128 test failed!\n", ret);
  862. else
  863. TEST_PASS("SHAKE128 test passed!\n");
  864. #endif
  865. #ifdef WOLFSSL_SHAKE256
  866. if ( (ret = shake256_test()) != 0)
  867. return err_sys("SHAKE256 test failed!\n", ret);
  868. else
  869. TEST_PASS("SHAKE256 test passed!\n");
  870. #endif
  871. #ifndef NO_HASH_WRAPPER
  872. if ( (ret = hash_test()) != 0)
  873. return err_sys("Hash test failed!\n", ret);
  874. else
  875. TEST_PASS("Hash test passed!\n");
  876. #endif
  877. #ifdef WOLFSSL_RIPEMD
  878. if ( (ret = ripemd_test()) != 0)
  879. return err_sys("RIPEMD test failed!\n", ret);
  880. else
  881. TEST_PASS("RIPEMD test passed!\n");
  882. #endif
  883. #ifdef HAVE_BLAKE2
  884. if ( (ret = blake2b_test()) != 0)
  885. return err_sys("BLAKE2b test failed!\n", ret);
  886. else
  887. TEST_PASS("BLAKE2b test passed!\n");
  888. #endif
  889. #ifdef HAVE_BLAKE2S
  890. if ( (ret = blake2s_test()) != 0)
  891. return err_sys("BLAKE2s test failed!\n", ret);
  892. else
  893. TEST_PASS("BLAKE2s test passed!\n");
  894. #endif
  895. #ifndef NO_HMAC
  896. #if !defined(NO_MD5) && !(defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) \
  897. && (HAVE_FIPS_VERSION >= 5))
  898. if ( (ret = hmac_md5_test()) != 0)
  899. return err_sys("HMAC-MD5 test failed!\n", ret);
  900. else
  901. TEST_PASS("HMAC-MD5 test passed!\n");
  902. #endif
  903. #ifndef NO_SHA
  904. if ( (ret = hmac_sha_test()) != 0)
  905. return err_sys("HMAC-SHA test failed!\n", ret);
  906. else
  907. TEST_PASS("HMAC-SHA test passed!\n");
  908. #endif
  909. #ifdef WOLFSSL_SHA224
  910. if ( (ret = hmac_sha224_test()) != 0)
  911. return err_sys("HMAC-SHA224 test failed!\n", ret);
  912. else
  913. TEST_PASS("HMAC-SHA224 test passed!\n");
  914. #endif
  915. #ifndef NO_SHA256
  916. if ( (ret = hmac_sha256_test()) != 0)
  917. return err_sys("HMAC-SHA256 test failed!\n", ret);
  918. else
  919. TEST_PASS("HMAC-SHA256 test passed!\n");
  920. #endif
  921. #ifdef WOLFSSL_SHA384
  922. if ( (ret = hmac_sha384_test()) != 0)
  923. return err_sys("HMAC-SHA384 test failed!\n", ret);
  924. else
  925. TEST_PASS("HMAC-SHA384 test passed!\n");
  926. #endif
  927. #ifdef WOLFSSL_SHA512
  928. if ( (ret = hmac_sha512_test()) != 0)
  929. return err_sys("HMAC-SHA512 test failed!\n", ret);
  930. else
  931. TEST_PASS("HMAC-SHA512 test passed!\n");
  932. #endif
  933. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA3) && \
  934. !defined(WOLFSSL_NOSHA3_224) && !defined(WOLFSSL_NOSHA3_256) && \
  935. !defined(WOLFSSL_NOSHA3_384) && !defined(WOLFSSL_NOSHA3_512)
  936. if ( (ret = hmac_sha3_test()) != 0)
  937. return err_sys("HMAC-SHA3 test failed!\n", ret);
  938. else
  939. TEST_PASS("HMAC-SHA3 test passed!\n");
  940. #endif
  941. #if defined(HAVE_HKDF) && !defined(NO_HMAC)
  942. PRIVATE_KEY_UNLOCK();
  943. if ( (ret = hkdf_test()) != 0)
  944. return err_sys("HMAC-KDF test failed!\n", ret);
  945. else
  946. TEST_PASS("HMAC-KDF test passed!\n");
  947. PRIVATE_KEY_LOCK();
  948. #endif
  949. #endif /* !NO_HMAC */
  950. #ifdef WOLFSSL_WOLFSSH
  951. PRIVATE_KEY_UNLOCK();
  952. if ( (ret = sshkdf_test()) != 0)
  953. return err_sys("SSH-KDF test failed!\n", ret);
  954. else
  955. TEST_PASS("SSH-KDF test passed!\n");
  956. PRIVATE_KEY_LOCK();
  957. #endif /* WOLFSSL_WOLFSSH */
  958. #ifdef WOLFSSL_TLS13
  959. PRIVATE_KEY_UNLOCK();
  960. if ( (ret = tls13_kdf_test()) != 0)
  961. return err_sys("TLSv1.3 KDF test failed!\n", ret);
  962. else
  963. TEST_PASS("TLSv1.3 KDF test passed!\n");
  964. PRIVATE_KEY_LOCK();
  965. #endif /* WOLFSSL_TLS13 */
  966. #if defined(HAVE_X963_KDF) && defined(HAVE_ECC)
  967. if ( (ret = x963kdf_test()) != 0)
  968. return err_sys("X963-KDF test failed!\n", ret);
  969. else
  970. TEST_PASS("X963-KDF test passed!\n");
  971. #endif
  972. #if defined(HAVE_AESGCM) && defined(WOLFSSL_AES_128) && \
  973. !defined(WOLFSSL_AFALG_XILINX_AES) && !defined(WOLFSSL_XILINX_CRYPT)
  974. if ( (ret = gmac_test()) != 0)
  975. return err_sys("GMAC test failed!\n", ret);
  976. else
  977. TEST_PASS("GMAC test passed!\n");
  978. #endif
  979. #ifdef WC_RC2
  980. if ( (ret = rc2_test()) != 0)
  981. return err_sys("RC2 test failed!\n", ret);
  982. else
  983. TEST_PASS("RC2 test passed!\n");
  984. #endif
  985. #ifndef NO_RC4
  986. if ( (ret = arc4_test()) != 0)
  987. return err_sys("ARC4 test failed!\n", ret);
  988. else
  989. TEST_PASS("ARC4 test passed!\n");
  990. #endif
  991. #ifdef HAVE_CHACHA
  992. if ( (ret = chacha_test()) != 0)
  993. return err_sys("Chacha test failed!\n", ret);
  994. else
  995. TEST_PASS("Chacha test passed!\n");
  996. #endif
  997. #ifdef HAVE_XCHACHA
  998. if ( (ret = XChaCha_test()) != 0)
  999. return err_sys("XChacha test failed!\n", ret);
  1000. else
  1001. TEST_PASS("XChacha test passed!\n");
  1002. #endif
  1003. #ifdef HAVE_POLY1305
  1004. if ( (ret = poly1305_test()) != 0)
  1005. return err_sys("POLY1305 test failed!\n", ret);
  1006. else
  1007. TEST_PASS("POLY1305 test passed!\n");
  1008. #endif
  1009. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  1010. if ( (ret = chacha20_poly1305_aead_test()) != 0)
  1011. return err_sys("ChaCha20-Poly1305 AEAD test failed!\n", ret);
  1012. else
  1013. TEST_PASS("ChaCha20-Poly1305 AEAD test passed!\n");
  1014. #endif
  1015. #if defined(HAVE_XCHACHA) && defined(HAVE_POLY1305)
  1016. if ( (ret = XChaCha20Poly1305_test()) != 0)
  1017. return err_sys("XChaCha20-Poly1305 AEAD test failed!\n", ret);
  1018. else
  1019. TEST_PASS("XChaCha20-Poly1305 AEAD test passed!\n");
  1020. #endif
  1021. #ifndef NO_DES3
  1022. if ( (ret = des_test()) != 0)
  1023. return err_sys("DES test failed!\n", ret);
  1024. else
  1025. TEST_PASS("DES test passed!\n");
  1026. #endif
  1027. #ifndef NO_DES3
  1028. if ( (ret = des3_test()) != 0)
  1029. return err_sys("DES3 test failed!\n", ret);
  1030. else
  1031. TEST_PASS("DES3 test passed!\n");
  1032. #endif
  1033. #ifndef NO_AES
  1034. if ( (ret = aes_test()) != 0)
  1035. return err_sys("AES test failed!\n", ret);
  1036. else
  1037. TEST_PASS("AES test passed!\n");
  1038. #ifdef WOLFSSL_AES_192
  1039. if ( (ret = aes192_test()) != 0)
  1040. return err_sys("AES192 test failed!\n", ret);
  1041. else
  1042. TEST_PASS("AES192 test passed!\n");
  1043. #endif
  1044. #ifdef WOLFSSL_AES_256
  1045. if ( (ret = aes256_test()) != 0)
  1046. return err_sys("AES256 test failed!\n", ret);
  1047. else
  1048. TEST_PASS("AES256 test passed!\n");
  1049. #endif
  1050. #ifdef WOLFSSL_AES_OFB
  1051. if ( (ret = aesofb_test()) != 0)
  1052. return err_sys("AES-OFB test failed!\n", ret);
  1053. else
  1054. TEST_PASS("AESOFB test passed!\n");
  1055. #endif
  1056. #ifdef HAVE_AESGCM
  1057. #if !defined(WOLFSSL_AFALG) && !defined(WOLFSSL_DEVCRYPTO)
  1058. if ( (ret = aesgcm_test()) != 0)
  1059. return err_sys("AES-GCM test failed!\n", ret);
  1060. #endif
  1061. #if !defined(WOLFSSL_AFALG_XILINX_AES) && !defined(WOLFSSL_XILINX_CRYPT) && \
  1062. !defined(WOLFSSL_KCAPI_AES) && !(defined(WOLF_CRYPTO_CB) && \
  1063. (defined(HAVE_INTEL_QA_SYNC) || defined(HAVE_CAVIUM_OCTEON_SYNC)))
  1064. if ((ret = aesgcm_default_test()) != 0) {
  1065. return err_sys("AES-GCM test failed!\n", ret);
  1066. }
  1067. #endif
  1068. if (ret == 0) {
  1069. TEST_PASS("AES-GCM test passed!\n");
  1070. }
  1071. #endif
  1072. #if defined(HAVE_AESCCM) && defined(WOLFSSL_AES_128)
  1073. if ( (ret = aesccm_test()) != 0)
  1074. return err_sys("AES-CCM test failed!\n", ret);
  1075. else
  1076. TEST_PASS("AES-CCM test passed!\n");
  1077. #endif
  1078. #ifdef HAVE_AES_KEYWRAP
  1079. if ( (ret = aeskeywrap_test()) != 0)
  1080. return err_sys("AES Key Wrap test failed!\n", ret);
  1081. else
  1082. TEST_PASS("AES Key Wrap test passed!\n");
  1083. #endif
  1084. #ifdef WOLFSSL_AES_SIV
  1085. if ( (ret = aes_siv_test()) != 0)
  1086. return err_sys("AES-SIV test failed!\n", ret);
  1087. else
  1088. TEST_PASS("AES-SIV test passed!\n");
  1089. #endif
  1090. #endif
  1091. #ifdef HAVE_CAMELLIA
  1092. if ( (ret = camellia_test()) != 0)
  1093. return err_sys("CAMELLIA test failed!\n", ret);
  1094. else
  1095. TEST_PASS("CAMELLIA test passed!\n");
  1096. #endif
  1097. #if !defined(NO_RSA)
  1098. #ifdef WC_RSA_NO_PADDING
  1099. if ( (ret = rsa_no_pad_test()) != 0)
  1100. return err_sys("RSA NOPAD test failed!\n", ret);
  1101. else
  1102. TEST_PASS("RSA NOPAD test passed!\n");
  1103. #endif
  1104. if ( (ret = rsa_test()) != 0)
  1105. return err_sys("RSA test failed!\n", ret);
  1106. else
  1107. TEST_PASS("RSA test passed!\n");
  1108. #endif
  1109. #ifndef NO_DH
  1110. PRIVATE_KEY_UNLOCK();
  1111. if ( (ret = dh_test()) != 0)
  1112. return err_sys("DH test failed!\n", ret);
  1113. else
  1114. TEST_PASS("DH test passed!\n");
  1115. PRIVATE_KEY_LOCK();
  1116. #endif
  1117. #ifndef NO_DSA
  1118. if ( (ret = dsa_test()) != 0)
  1119. return err_sys("DSA test failed!\n", ret);
  1120. else
  1121. TEST_PASS("DSA test passed!\n");
  1122. #endif
  1123. #ifdef WOLFCRYPT_HAVE_SRP
  1124. if ( (ret = srp_test()) != 0)
  1125. return err_sys("SRP test failed!\n", ret);
  1126. else
  1127. TEST_PASS("SRP test passed!\n");
  1128. #endif
  1129. #ifndef NO_PWDBASED
  1130. if ( (ret = pwdbased_test()) != 0)
  1131. return err_sys("PWDBASED test failed!\n", ret);
  1132. else
  1133. TEST_PASS("PWDBASED test passed!\n");
  1134. #endif
  1135. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  1136. if ( (ret = openssl_test()) != 0)
  1137. return err_sys("OPENSSL test failed!\n", ret);
  1138. else
  1139. TEST_PASS("OPENSSL test passed!\n");
  1140. if ( (ret = openSSL_evpMD_test()) != 0)
  1141. return err_sys("OPENSSL (EVP MD) test failed!\n", ret);
  1142. else
  1143. TEST_PASS("OPENSSL (EVP MD) passed!\n");
  1144. if ( (ret = openssl_pkey0_test()) != 0)
  1145. return err_sys("OPENSSL (PKEY0) test failed!\n", ret);
  1146. else
  1147. TEST_PASS("OPENSSL (PKEY0) passed!\n");
  1148. if ( (ret = openssl_pkey1_test()) != 0)
  1149. return err_sys("OPENSSL (PKEY1) test failed!\n", ret);
  1150. else
  1151. TEST_PASS("OPENSSL (PKEY1) passed!\n");
  1152. #if !defined(WOLF_CRYPTO_CB_ONLY_RSA) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  1153. if ( (ret = openssl_evpSig_test()) != 0)
  1154. return err_sys("OPENSSL (EVP Sign/Verify) test failed!\n", ret);
  1155. else
  1156. TEST_PASS("OPENSSL (EVP Sign/Verify) passed!\n");
  1157. #endif
  1158. #endif
  1159. #if defined(HAVE_ECC)
  1160. PRIVATE_KEY_UNLOCK();
  1161. if ( (ret = ecc_test()) != 0)
  1162. return err_sys("ECC test failed!\n", ret);
  1163. else
  1164. TEST_PASS("ECC test passed!\n");
  1165. PRIVATE_KEY_LOCK();
  1166. #if defined(HAVE_ECC_ENCRYPT) && defined(HAVE_AES_CBC) && \
  1167. (defined(WOLFSSL_AES_128) || defined(WOLFSSL_AES_256))
  1168. if ( (ret = ecc_encrypt_test()) != 0)
  1169. return err_sys("ECC Enc test failed!\n", ret);
  1170. else
  1171. TEST_PASS("ECC Enc test passed!\n");
  1172. #endif
  1173. #if defined(USE_CERT_BUFFERS_256) && !defined(WOLFSSL_ATECC508A) && \
  1174. !defined(WOLFSSL_ATECC608A) && !defined(NO_ECC256) && \
  1175. defined(HAVE_ECC_VERIFY) && defined(HAVE_ECC_SIGN) && \
  1176. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  1177. /* skip for ATECC508/608A, cannot import private key buffers */
  1178. if ( (ret = ecc_test_buffers()) != 0)
  1179. return err_sys("ECC buffer test failed!\n", ret);
  1180. else
  1181. TEST_PASS("ECC buffer test passed!\n");
  1182. #endif
  1183. #endif
  1184. #if !defined(NO_ASN_TIME) && !defined(NO_RSA) && defined(WOLFSSL_TEST_CERT) && \
  1185. !defined(NO_FILESYSTEM)
  1186. if ( (ret = cert_test()) != 0)
  1187. return err_sys("CERT test failed!\n", ret);
  1188. else
  1189. TEST_PASS("CERT test passed!\n");
  1190. #endif
  1191. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_TEST_CERT) && \
  1192. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && defined(WOLFSSL_GEN_CERT)
  1193. if ( (ret = certext_test()) != 0)
  1194. return err_sys("CERT EXT test failed!\n", ret);
  1195. else
  1196. TEST_PASS("CERT EXT test passed!\n");
  1197. #endif
  1198. #if defined(WOLFSSL_CERT_GEN_CACHE) && defined(WOLFSSL_TEST_CERT) && \
  1199. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  1200. if ( (ret = decodedCertCache_test()) != 0)
  1201. return err_sys("DECODED CERT CACHE test failed!\n", ret);
  1202. else
  1203. TEST_PASS("DECODED CERT CACHE test passed!\n");
  1204. #endif
  1205. #ifdef HAVE_CURVE25519
  1206. if ( (ret = curve25519_test()) != 0)
  1207. return err_sys("CURVE25519 test failed!\n", ret);
  1208. else
  1209. TEST_PASS("CURVE25519 test passed!\n");
  1210. #endif
  1211. #ifdef HAVE_ED25519
  1212. if ( (ret = ed25519_test()) != 0)
  1213. return err_sys("ED25519 test failed!\n", ret);
  1214. else
  1215. TEST_PASS("ED25519 test passed!\n");
  1216. #endif
  1217. #ifdef HAVE_CURVE448
  1218. if ( (ret = curve448_test()) != 0)
  1219. return err_sys("CURVE448 test failed!\n", ret);
  1220. else
  1221. TEST_PASS("CURVE448 test passed!\n");
  1222. #endif
  1223. #ifdef HAVE_ED448
  1224. if ( (ret = ed448_test()) != 0)
  1225. return err_sys("ED448 test failed!\n", ret);
  1226. else
  1227. TEST_PASS("ED448 test passed!\n");
  1228. #endif
  1229. #ifdef WOLFSSL_HAVE_KYBER
  1230. if ( (ret = kyber_test()) != 0)
  1231. return err_sys("KYBER test failed!\n", ret);
  1232. else
  1233. TEST_PASS("KYBER test passed!\n");
  1234. #endif
  1235. #ifdef WOLFCRYPT_HAVE_ECCSI
  1236. if ( (ret = eccsi_test()) != 0)
  1237. return err_sys("ECCSI test failed!\n", ret);
  1238. else
  1239. TEST_PASS("ECCSI test passed!\n");
  1240. #endif
  1241. #ifdef WOLFCRYPT_HAVE_SAKKE
  1242. if ( (ret = sakke_test()) != 0)
  1243. return err_sys("SAKKE test failed!\n", ret);
  1244. else
  1245. TEST_PASS("SAKKE test passed!\n");
  1246. #endif
  1247. #if defined(WOLFSSL_CMAC) && !defined(NO_AES)
  1248. if ( (ret = cmac_test()) != 0)
  1249. return err_sys("CMAC test failed!\n", ret);
  1250. else
  1251. TEST_PASS("CMAC test passed!\n");
  1252. #endif
  1253. #if defined(WOLFSSL_SIPHASH)
  1254. if ( (ret = siphash_test()) != 0)
  1255. return err_sys("SipHash test failed!\n", ret);
  1256. else
  1257. TEST_PASS("SipHash test passed!\n");
  1258. #endif
  1259. #ifdef HAVE_LIBZ
  1260. if ( (ret = compress_test()) != 0)
  1261. return err_sys("COMPRESS test failed!\n", ret);
  1262. else
  1263. TEST_PASS("COMPRESS test passed!\n");
  1264. #endif
  1265. #ifdef HAVE_PKCS7
  1266. #ifndef NO_PKCS7_ENCRYPTED_DATA
  1267. if ( (ret = pkcs7encrypted_test()) != 0)
  1268. return err_sys("PKCS7encrypted test failed!\n", ret);
  1269. else
  1270. TEST_PASS("PKCS7encrypted test passed!\n");
  1271. #endif
  1272. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  1273. if ( (ret = pkcs7compressed_test()) != 0)
  1274. return err_sys("PKCS7compressed test failed!\n", ret);
  1275. else
  1276. TEST_PASS("PKCS7compressed test passed!\n");
  1277. #endif
  1278. if ( (ret = pkcs7signed_test()) != 0)
  1279. return err_sys("PKCS7signed test failed!\n", ret);
  1280. else
  1281. TEST_PASS("PKCS7signed test passed!\n");
  1282. if ( (ret = pkcs7enveloped_test()) != 0)
  1283. return err_sys("PKCS7enveloped test failed!\n", ret);
  1284. else
  1285. TEST_PASS("PKCS7enveloped test passed!\n");
  1286. #if defined(HAVE_AESGCM) || defined(HAVE_AESCCM)
  1287. if ( (ret = pkcs7authenveloped_test()) != 0)
  1288. return err_sys("PKCS7authenveloped test failed!\n", ret);
  1289. else
  1290. TEST_PASS("PKCS7authenveloped test passed!\n");
  1291. #endif
  1292. #endif
  1293. #ifdef HAVE_VALGRIND
  1294. if ( (ret = mp_test()) != 0)
  1295. return err_sys("mp test failed!\n", ret);
  1296. else
  1297. TEST_PASS("mp test passed!\n");
  1298. #endif
  1299. #if defined(WOLFSSL_PUBLIC_MP) && defined(WOLFSSL_KEY_GEN)
  1300. if ( (ret = prime_test()) != 0)
  1301. return err_sys("prime test failed!\n", ret);
  1302. else
  1303. TEST_PASS("prime test passed!\n");
  1304. #endif
  1305. #if defined(ASN_BER_TO_DER) && \
  1306. (defined(WOLFSSL_TEST_CERT) || defined(OPENSSL_EXTRA) || \
  1307. defined(OPENSSL_EXTRA_X509_SMALL))
  1308. if ( (ret = berder_test()) != 0)
  1309. return err_sys("ber-der test failed!\n", ret);
  1310. else
  1311. TEST_PASS("ber-der test passed!\n");
  1312. #endif
  1313. if ( (ret = logging_test()) != 0)
  1314. return err_sys("logging test failed!\n", ret);
  1315. else
  1316. TEST_PASS("logging test passed!\n");
  1317. #if !defined(NO_ASN) && !defined(NO_ASN_TIME)
  1318. if ( (ret = time_test()) != 0)
  1319. return err_sys("time test failed!\n", ret);
  1320. else
  1321. TEST_PASS("time test passed!\n");
  1322. #endif
  1323. if ( (ret = mutex_test()) != 0)
  1324. return err_sys("mutex test failed!\n", ret);
  1325. else
  1326. TEST_PASS("mutex test passed!\n");
  1327. #if defined(USE_WOLFSSL_MEMORY) && !defined(FREERTOS)
  1328. if ( (ret = memcb_test()) != 0)
  1329. return err_sys("memcb test failed!\n", ret);
  1330. else
  1331. TEST_PASS("memcb test passed!\n");
  1332. #endif
  1333. #ifdef WOLFSSL_IMX6_CAAM_BLOB
  1334. if ( (ret = blob_test()) != 0)
  1335. return err_sys("blob test failed!\n", ret);
  1336. else
  1337. TEST_PASS("blob test passed!\n");
  1338. #endif
  1339. #if defined(WOLF_CRYPTO_CB) && \
  1340. !(defined(HAVE_INTEL_QAT_SYNC) || defined(HAVE_CAVIUM_OCTEON_SYNC) || \
  1341. defined(WOLFSSL_QNX_CAAM))
  1342. if ( (ret = cryptocb_test()) != 0)
  1343. return err_sys("crypto callback test failed!\n", ret);
  1344. else
  1345. TEST_PASS("crypto callback test passed!\n");
  1346. #endif
  1347. #ifdef WOLFSSL_CERT_PIV
  1348. if ( (ret = certpiv_test()) != 0)
  1349. return err_sys("cert piv test failed!\n", ret);
  1350. else
  1351. TEST_PASS("cert piv test passed!\n");
  1352. #endif
  1353. #ifdef WOLF_CRYPTO_CB
  1354. #ifdef HAVE_INTEL_QA_SYNC
  1355. wc_CryptoCb_CleanupIntelQa(&devId);
  1356. #endif
  1357. #ifdef HAVE_CAVIUM_OCTEON_SYNC
  1358. wc_CryptoCb_CleanupOcteon(&devId);
  1359. #endif
  1360. #endif
  1361. #ifdef WOLFSSL_ASYNC_CRYPT
  1362. wolfAsync_DevClose(&devId);
  1363. #endif
  1364. /* cleanup the thread if fixed point cache is enabled and have thread local */
  1365. #if defined(HAVE_THREAD_LS) && defined(HAVE_ECC) && defined(FP_ECC)
  1366. wc_ecc_fp_free();
  1367. #endif
  1368. if (args)
  1369. ((func_args*)args)->return_code = ret;
  1370. TEST_PASS("Test complete\n");
  1371. EXIT_TEST(ret);
  1372. }
  1373. #ifndef NO_MAIN_DRIVER
  1374. #ifdef HAVE_WOLFCRYPT_TEST_OPTIONS
  1375. int myoptind = 0;
  1376. char* myoptarg = NULL;
  1377. #endif
  1378. /* so overall tests can pull in test function */
  1379. #if defined(WOLFSSL_ESPIDF) || defined(_WIN32_WCE)
  1380. int wolf_test_task(void)
  1381. #else
  1382. #ifndef NO_MAIN_FUNCTION
  1383. int main(int argc, char** argv)
  1384. {
  1385. return wolfcrypt_test_main(argc, argv);
  1386. }
  1387. #endif
  1388. int wolfcrypt_test_main(int argc, char** argv)
  1389. #endif
  1390. {
  1391. int ret;
  1392. func_args args;
  1393. #if defined(WOLFSSL_ESPIDF) || defined(WOLFSSL_SE050)
  1394. /* set dummy wallclock time. */
  1395. struct timeval utctime;
  1396. struct timezone tz;
  1397. utctime.tv_sec = 1521725159; /* dummy time: 2018-03-22T13:25:59+00:00 */
  1398. utctime.tv_usec = 0;
  1399. tz.tz_minuteswest = 0;
  1400. tz.tz_dsttime = 0;
  1401. settimeofday(&utctime, &tz);
  1402. #endif
  1403. #ifdef WOLFSSL_APACHE_MYNEWT
  1404. #ifdef ARCH_sim
  1405. mcu_sim_parse_args(argc, argv);
  1406. #endif
  1407. sysinit();
  1408. /* set dummy wallclock time. */
  1409. struct os_timeval utctime;
  1410. struct os_timezone tz;
  1411. utctime.tv_sec = 1521725159; /* dummy time: 2018-03-22T13:25:59+00:00 */
  1412. utctime.tv_usec = 0;
  1413. tz.tz_minuteswest = 0;
  1414. tz.tz_dsttime = 0;
  1415. os_settimeofday(&utctime, &tz);
  1416. #endif
  1417. #ifdef WOLFSSL_ZEPHYR
  1418. /* set dummy wallclock time. */
  1419. struct timespec utctime;
  1420. utctime.tv_sec = 1521725159; /* dummy time: 2018-03-22T13:25:59+00:00 */
  1421. utctime.tv_nsec = 0;
  1422. clock_settime(CLOCK_REALTIME, &utctime);
  1423. #endif
  1424. #ifdef DEVKITPRO
  1425. void *framebuffer;
  1426. GXRModeObj *rmode = NULL;
  1427. VIDEO_Init();
  1428. WPAD_Init();
  1429. rmode = VIDEO_GetPreferredMode(NULL);
  1430. #pragma GCC diagnostic ignored "-Wbad-function-cast"
  1431. framebuffer = MEM_K0_TO_K1(SYS_AllocateFramebuffer(rmode));
  1432. #pragma GCC diagnostic pop
  1433. console_init(framebuffer,20,20,rmode->fbWidth,rmode->xfbHeight,rmode->fbWidth*VI_DISPLAY_PIX_SZ);
  1434. VIDEO_Configure(rmode);
  1435. VIDEO_SetNextFramebuffer(framebuffer);
  1436. VIDEO_SetBlack(FALSE);
  1437. VIDEO_Flush();
  1438. VIDEO_WaitVSync();
  1439. if(rmode->viTVMode&VI_NON_INTERLACE) VIDEO_WaitVSync();
  1440. #endif
  1441. #ifdef HAVE_WNR
  1442. if (wc_InitNetRandom(wnrConfigFile, NULL, 5000) != 0) {
  1443. err_sys("Whitewood netRandom global config failed", -1001);
  1444. return -1002;
  1445. }
  1446. #endif
  1447. #ifndef WOLFSSL_ESPIDF
  1448. args.argc = argc;
  1449. args.argv = argv;
  1450. #endif
  1451. if ((ret = wolfCrypt_Init()) != 0) {
  1452. printf("wolfCrypt_Init failed %d\n", ret);
  1453. err_sys("Error with wolfCrypt_Init!\n", -1003);
  1454. }
  1455. #ifdef WC_RNG_SEED_CB
  1456. wc_SetSeed_Cb(wc_GenerateSeed);
  1457. #endif
  1458. #ifdef HAVE_STACK_SIZE
  1459. StackSizeCheck(&args, wolfcrypt_test);
  1460. #else
  1461. wolfcrypt_test(&args);
  1462. #endif
  1463. if ((ret = wolfCrypt_Cleanup()) != 0) {
  1464. printf("wolfCrypt_Cleanup failed %d\n", ret);
  1465. err_sys("Error with wolfCrypt_Cleanup!\n", -1004);
  1466. }
  1467. #ifdef HAVE_WNR
  1468. if (wc_FreeNetRandom() < 0)
  1469. err_sys("Failed to free netRandom context", -1005);
  1470. #endif /* HAVE_WNR */
  1471. #ifdef DOLPHIN_EMULATOR
  1472. /* Returning from main panics the emulator. Just hang
  1473. * and let the user force quit the emulator window. */
  1474. printf("args.return_code: %d\n", args.return_code);
  1475. printf("Testing complete. You may close the window now\n");
  1476. while (1);
  1477. #endif
  1478. #ifdef WOLFSSL_ESPIDF
  1479. /* ESP_LOGI to print takes up a lot less memory than printf */
  1480. ESP_LOGI("wolfcrypt_test", "Exiting main with return code: % d\n", args.return_code);
  1481. #endif
  1482. /* everything else will use printf */
  1483. #if !defined(WOLFSSL_ESPIDF)
  1484. /* gate this for target platforms wishing to avoid printf reference */
  1485. printf("Exiting main with return code: %d\n", args.return_code);
  1486. #endif
  1487. return args.return_code;
  1488. } /* wolfcrypt_test_main or wolf_test_task */
  1489. #endif /* NO_MAIN_DRIVER */
  1490. /* helper to save DER, convert to PEM and save PEM */
  1491. #if !defined(NO_ASN) && (defined(HAVE_ECC) || !defined(NO_DSA) || \
  1492. (!defined(NO_RSA) && (defined(WOLFSSL_KEY_GEN) || defined(WOLFSSL_CERT_GEN)))) \
  1493. && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  1494. #if !defined(NO_FILESYSTEM) && !defined(NO_WRITE_TEMP_FILES)
  1495. #define SaveDerAndPem(d, dSz, fD, fP, pT, eB) _SaveDerAndPem(d, dSz, fD, fP, pT, eB)
  1496. #else
  1497. #define SaveDerAndPem(d, dSz, fD, fP, pT, eB) _SaveDerAndPem(d, dSz, NULL, NULL, pT, eB)
  1498. #endif
  1499. static int _SaveDerAndPem(const byte* der, int derSz,
  1500. const char* fileDer, const char* filePem, int pemType, int errBase)
  1501. {
  1502. #if !defined(NO_FILESYSTEM) && !defined(NO_WRITE_TEMP_FILES)
  1503. int ret;
  1504. XFILE derFile;
  1505. derFile = XFOPEN(fileDer, "wb");
  1506. if (!derFile) {
  1507. return errBase + 0;
  1508. }
  1509. ret = (int)XFWRITE(der, 1, derSz, derFile);
  1510. XFCLOSE(derFile);
  1511. if (ret != derSz) {
  1512. return errBase + 1;
  1513. }
  1514. #endif
  1515. #ifdef WOLFSSL_DER_TO_PEM
  1516. if (filePem) {
  1517. #if !defined(NO_FILESYSTEM) && !defined(NO_WRITE_TEMP_FILES)
  1518. XFILE pemFile;
  1519. #endif
  1520. byte* pem;
  1521. int pemSz;
  1522. /* calculate PEM size */
  1523. pemSz = wc_DerToPem(der, derSz, NULL, 0, pemType);
  1524. if (pemSz < 0) {
  1525. return pemSz;
  1526. }
  1527. pem = (byte*)XMALLOC(pemSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  1528. if (pem == NULL) {
  1529. return MEMORY_E;
  1530. }
  1531. /* Convert to PEM */
  1532. pemSz = wc_DerToPem(der, derSz, pem, pemSz, pemType);
  1533. if (pemSz < 0) {
  1534. XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  1535. return errBase + 2;
  1536. }
  1537. #if !defined(NO_FILESYSTEM) && !defined(NO_WRITE_TEMP_FILES)
  1538. pemFile = XFOPEN(filePem, "wb");
  1539. if (!pemFile) {
  1540. XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  1541. return errBase + 3;
  1542. }
  1543. ret = (int)XFWRITE(pem, 1, pemSz, pemFile);
  1544. XFCLOSE(pemFile);
  1545. if (ret != pemSz) {
  1546. XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  1547. return errBase + 4;
  1548. }
  1549. #endif
  1550. XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  1551. }
  1552. #endif /* WOLFSSL_DER_TO_PEM */
  1553. /* suppress unused variable warnings */
  1554. (void)der;
  1555. (void)derSz;
  1556. (void)filePem;
  1557. (void)fileDer;
  1558. (void)pemType;
  1559. (void)errBase;
  1560. return 0;
  1561. }
  1562. #endif /* WOLFSSL_KEY_GEN || WOLFSSL_CERT_GEN */
  1563. WOLFSSL_TEST_SUBROUTINE int error_test(void)
  1564. {
  1565. const char* errStr;
  1566. char out[WOLFSSL_MAX_ERROR_SZ];
  1567. const char* unknownStr = wc_GetErrorString(0);
  1568. #ifdef NO_ERROR_STRINGS
  1569. /* Ensure a valid error code's string matches an invalid code's.
  1570. * The string is that error strings are not available.
  1571. */
  1572. errStr = wc_GetErrorString(OPEN_RAN_E);
  1573. wc_ErrorString(OPEN_RAN_E, out);
  1574. if (XSTRCMP(errStr, unknownStr) != 0)
  1575. return -1100;
  1576. if (XSTRCMP(out, unknownStr) != 0)
  1577. return -1101;
  1578. #else
  1579. int i;
  1580. int j = 0;
  1581. /* Values that are not or no longer error codes. */
  1582. int missing[] = { -122, -123, -124, -127, -128, -129, -159,
  1583. -163, -164, -165, -166, -167, -168, -169, -233,
  1584. 0 };
  1585. /* Check that all errors have a string and it's the same through the two
  1586. * APIs. Check that the values that are not errors map to the unknown
  1587. * string.
  1588. */
  1589. for (i = MAX_CODE_E-1; i >= WC_LAST_E; i--) {
  1590. errStr = wc_GetErrorString(i);
  1591. wc_ErrorString(i, out);
  1592. if (i != missing[j]) {
  1593. if (XSTRCMP(errStr, unknownStr) == 0)
  1594. return -1102;
  1595. if (XSTRCMP(out, unknownStr) == 0)
  1596. return -1103;
  1597. if (XSTRCMP(errStr, out) != 0)
  1598. return -1104;
  1599. if (XSTRLEN(errStr) >= WOLFSSL_MAX_ERROR_SZ)
  1600. return -1105;
  1601. }
  1602. else {
  1603. j++;
  1604. if (XSTRCMP(errStr, unknownStr) != 0)
  1605. return -1106;
  1606. if (XSTRCMP(out, unknownStr) != 0)
  1607. return -1107;
  1608. }
  1609. }
  1610. /* Check if the next possible value has been given a string. */
  1611. errStr = wc_GetErrorString(i);
  1612. wc_ErrorString(i, out);
  1613. if (XSTRCMP(errStr, unknownStr) != 0)
  1614. return -1108;
  1615. if (XSTRCMP(out, unknownStr) != 0)
  1616. return -1109;
  1617. #endif
  1618. return 0;
  1619. }
  1620. #ifndef NO_CODING
  1621. WOLFSSL_TEST_SUBROUTINE int base64_test(void)
  1622. {
  1623. int ret;
  1624. WOLFSSL_SMALL_STACK_STATIC const byte good[] = "A+Gd\0\0\0";
  1625. WOLFSSL_SMALL_STACK_STATIC const byte goodEnd[] = "A+Gd \r\n";
  1626. WOLFSSL_SMALL_STACK_STATIC const byte good_spaces[] = " A + G d \0";
  1627. byte out[128];
  1628. word32 outLen;
  1629. #ifdef WOLFSSL_BASE64_ENCODE
  1630. byte data[3];
  1631. word32 dataLen;
  1632. byte longData[79] = { 0 };
  1633. WOLFSSL_SMALL_STACK_STATIC const byte symbols[] = "+/A=";
  1634. #endif
  1635. WOLFSSL_SMALL_STACK_STATIC const byte badSmall[] = "AAA!Gdj=";
  1636. WOLFSSL_SMALL_STACK_STATIC const byte badLarge[] = "AAA~Gdj=";
  1637. WOLFSSL_SMALL_STACK_STATIC const byte badEOL[] = "A+Gd!AA";
  1638. WOLFSSL_SMALL_STACK_STATIC const byte badPadding[] = "AA=A";
  1639. WOLFSSL_SMALL_STACK_STATIC const byte badChar[] = ",-.:;<=>?@[\\]^_`";
  1640. byte goodChar[] =
  1641. "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  1642. "abcdefghijklmnopqrstuvwxyz"
  1643. "0123456789+/;";
  1644. byte charTest[] = "A+Gd\0\0\0";
  1645. int i;
  1646. /* Good Base64 encodings. */
  1647. outLen = sizeof(out);
  1648. ret = Base64_Decode(good, sizeof(good), out, &outLen);
  1649. if (ret != 0)
  1650. return -1200;
  1651. outLen = sizeof(out);
  1652. ret = Base64_Decode(goodEnd, sizeof(goodEnd), out, &outLen);
  1653. if (ret != 0)
  1654. return -1201;
  1655. outLen = sizeof(goodChar);
  1656. ret = Base64_Decode(goodChar, sizeof(goodChar), goodChar, &outLen);
  1657. if (ret != 0)
  1658. return -1235;
  1659. if (outLen != 64 / 4 * 3)
  1660. return -1236;
  1661. outLen = sizeof(out);
  1662. ret = Base64_Decode(good_spaces, sizeof(good_spaces), out, &outLen);
  1663. if (ret != 0)
  1664. return -1201;
  1665. /* Bad parameters. */
  1666. outLen = 1;
  1667. ret = Base64_Decode(good, sizeof(good), out, &outLen);
  1668. if (ret != BAD_FUNC_ARG)
  1669. return -1202;
  1670. outLen = sizeof(out);
  1671. ret = Base64_Decode(badEOL, sizeof(badEOL), out, &outLen);
  1672. if (ret != ASN_INPUT_E)
  1673. return -1203;
  1674. outLen = sizeof(out);
  1675. ret = Base64_Decode(badPadding, sizeof(badPadding), out, &outLen);
  1676. if (ret != ASN_INPUT_E)
  1677. return -1203;
  1678. /* Bad character at each offset 0-3. */
  1679. for (i = 0; i < 4; i++) {
  1680. outLen = sizeof(out);
  1681. ret = Base64_Decode(badSmall + i, 4, out, &outLen);
  1682. if (ret != ASN_INPUT_E)
  1683. return -1204 - i;
  1684. ret = Base64_Decode(badLarge + i, 4, out, &outLen);
  1685. if (ret != ASN_INPUT_E)
  1686. return -1214 - i;
  1687. }
  1688. /* Invalid character less than 0x2b */
  1689. for (i = 1; i < 0x2b; i++) {
  1690. outLen = sizeof(out);
  1691. charTest[0] = (byte)i;
  1692. ret = Base64_Decode(charTest, sizeof(charTest), out, &outLen);
  1693. if (ret != ASN_INPUT_E)
  1694. return -1240 - i;
  1695. }
  1696. /* Bad characters in range 0x2b - 0x7a. */
  1697. for (i = 0; i < (int)sizeof(badChar) - 1; i++) {
  1698. outLen = sizeof(out);
  1699. charTest[0] = badChar[i];
  1700. ret = Base64_Decode(charTest, sizeof(charTest), out, &outLen);
  1701. if (ret != ASN_INPUT_E)
  1702. return -1270 - i;
  1703. }
  1704. /* Invalid character greater than 0x7a */
  1705. for (i = 0x7b; i < 0x100; i++) {
  1706. outLen = sizeof(out);
  1707. charTest[0] = (byte)i;
  1708. ret = Base64_Decode(charTest, sizeof(charTest), out, &outLen);
  1709. if (ret != ASN_INPUT_E)
  1710. return -1290 - i;
  1711. }
  1712. #ifdef WOLFSSL_BASE64_ENCODE
  1713. /* Decode and encode all symbols - non-alphanumeric. */
  1714. dataLen = sizeof(data);
  1715. ret = Base64_Decode(symbols, sizeof(symbols), data, &dataLen);
  1716. if (ret != 0)
  1717. return -1224;
  1718. outLen = sizeof(out);
  1719. ret = Base64_Encode(data, dataLen, NULL, &outLen);
  1720. if (ret != LENGTH_ONLY_E)
  1721. return -1225;
  1722. outLen = sizeof(out);
  1723. ret = Base64_Encode(data, dataLen, out, &outLen);
  1724. if (ret != 0)
  1725. return -1226;
  1726. outLen = 7;
  1727. ret = Base64_EncodeEsc(data, dataLen, out, &outLen);
  1728. if (ret != BUFFER_E)
  1729. return -1227;
  1730. outLen = sizeof(out);
  1731. ret = Base64_EncodeEsc(data, dataLen, NULL, &outLen);
  1732. if (ret != LENGTH_ONLY_E)
  1733. return -1228;
  1734. outLen = sizeof(out);
  1735. ret = Base64_EncodeEsc(data, dataLen, out, &outLen);
  1736. if (ret != 0)
  1737. return -1229;
  1738. outLen = sizeof(out);
  1739. ret = Base64_Encode_NoNl(data, dataLen, out, &outLen);
  1740. if (ret != 0)
  1741. return -1230;
  1742. /* Data that results in an encoding longer than one line. */
  1743. outLen = sizeof(out);
  1744. dataLen = sizeof(longData);
  1745. ret = Base64_Encode(longData, dataLen, out, &outLen);
  1746. if (ret != 0)
  1747. return -1231;
  1748. outLen = sizeof(out);
  1749. ret = Base64_EncodeEsc(longData, dataLen, out, &outLen);
  1750. if (ret != 0)
  1751. return -1232;
  1752. outLen = sizeof(out);
  1753. ret = Base64_Encode_NoNl(longData, dataLen, out, &outLen);
  1754. if (ret != 0)
  1755. return -1233;
  1756. #endif
  1757. return 0;
  1758. }
  1759. #ifdef WOLFSSL_BASE16
  1760. WOLFSSL_TEST_SUBROUTINE int base16_test(void)
  1761. {
  1762. int ret;
  1763. WOLFSSL_SMALL_STACK_STATIC const byte testData[] = "SomeDataToEncode\n";
  1764. WOLFSSL_SMALL_STACK_STATIC const byte encodedTestData[] = "536F6D6544617461546F456E636F64650A00";
  1765. byte encoded[40];
  1766. word32 encodedLen;
  1767. byte plain[40];
  1768. word32 len;
  1769. /* length returned includes null termination */
  1770. encodedLen = sizeof(encoded);
  1771. ret = Base16_Encode(testData, sizeof(testData), encoded, &encodedLen);
  1772. if (ret != 0)
  1773. return -1300;
  1774. len = (word32)XSTRLEN((char*)encoded);
  1775. if (len != encodedLen - 1)
  1776. return -1301;
  1777. len = sizeof(plain);
  1778. ret = Base16_Decode(encoded, encodedLen - 1, plain, &len);
  1779. if (ret != 0)
  1780. return -1302;
  1781. if (len != sizeof(testData) || XMEMCMP(testData, plain, len) != 0)
  1782. return -1303;
  1783. if (encodedLen != sizeof(encodedTestData) ||
  1784. XMEMCMP(encoded, encodedTestData, encodedLen) != 0) {
  1785. return -1304;
  1786. }
  1787. return 0;
  1788. }
  1789. #endif /* WOLFSSL_BASE16 */
  1790. #endif /* !NO_CODING */
  1791. #ifndef NO_ASN
  1792. WOLFSSL_TEST_SUBROUTINE int asn_test(void)
  1793. {
  1794. int ret;
  1795. /* ASN1 encoded date buffer */
  1796. WOLFSSL_SMALL_STACK_STATIC const byte dateBuf[] = {0x17, 0x0d, 0x31, 0x36, 0x30, 0x38, 0x31, 0x31,
  1797. 0x32, 0x30, 0x30, 0x37, 0x33, 0x37, 0x5a};
  1798. byte format;
  1799. int length;
  1800. const byte* datePart;
  1801. #ifndef NO_ASN_TIME
  1802. struct tm timearg;
  1803. time_t now;
  1804. #endif
  1805. ret = wc_GetDateInfo(dateBuf, (int)sizeof(dateBuf), &datePart, &format,
  1806. &length);
  1807. if (ret != 0)
  1808. return -1400;
  1809. #ifndef NO_ASN_TIME
  1810. /* Parameter Validation tests. */
  1811. if (wc_GetTime(NULL, sizeof(now)) != BAD_FUNC_ARG)
  1812. return -1401;
  1813. if (wc_GetTime(&now, 0) != BUFFER_E)
  1814. return -1402;
  1815. now = 0;
  1816. if (wc_GetTime(&now, sizeof(now)) != 0) {
  1817. return -1403;
  1818. }
  1819. if (now == 0) {
  1820. printf("RTC/Time not set!\n");
  1821. return -1404;
  1822. }
  1823. ret = wc_GetDateAsCalendarTime(datePart, length, format, &timearg);
  1824. if (ret != 0)
  1825. return -1405;
  1826. #endif /* !NO_ASN_TIME */
  1827. return 0;
  1828. }
  1829. #endif /* !NO_ASN */
  1830. #ifdef WOLFSSL_MD2
  1831. WOLFSSL_TEST_SUBROUTINE int md2_test(void)
  1832. {
  1833. int ret = 0;
  1834. Md2 md2;
  1835. byte hash[MD2_DIGEST_SIZE];
  1836. testVector a, b, c, d, e, f, g;
  1837. testVector test_md2[7];
  1838. int times = sizeof(test_md2) / sizeof(testVector), i;
  1839. a.input = "";
  1840. a.output = "\x83\x50\xe5\xa3\xe2\x4c\x15\x3d\xf2\x27\x5c\x9f\x80\x69"
  1841. "\x27\x73";
  1842. a.inLen = XSTRLEN(a.input);
  1843. a.outLen = MD2_DIGEST_SIZE;
  1844. b.input = "a";
  1845. b.output = "\x32\xec\x01\xec\x4a\x6d\xac\x72\xc0\xab\x96\xfb\x34\xc0"
  1846. "\xb5\xd1";
  1847. b.inLen = XSTRLEN(b.input);
  1848. b.outLen = MD2_DIGEST_SIZE;
  1849. c.input = "abc";
  1850. c.output = "\xda\x85\x3b\x0d\x3f\x88\xd9\x9b\x30\x28\x3a\x69\xe6\xde"
  1851. "\xd6\xbb";
  1852. c.inLen = XSTRLEN(c.input);
  1853. c.outLen = MD2_DIGEST_SIZE;
  1854. d.input = "message digest";
  1855. d.output = "\xab\x4f\x49\x6b\xfb\x2a\x53\x0b\x21\x9f\xf3\x30\x31\xfe"
  1856. "\x06\xb0";
  1857. d.inLen = XSTRLEN(d.input);
  1858. d.outLen = MD2_DIGEST_SIZE;
  1859. e.input = "abcdefghijklmnopqrstuvwxyz";
  1860. e.output = "\x4e\x8d\xdf\xf3\x65\x02\x92\xab\x5a\x41\x08\xc3\xaa\x47"
  1861. "\x94\x0b";
  1862. e.inLen = XSTRLEN(e.input);
  1863. e.outLen = MD2_DIGEST_SIZE;
  1864. f.input = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345"
  1865. "6789";
  1866. f.output = "\xda\x33\xde\xf2\xa4\x2d\xf1\x39\x75\x35\x28\x46\xc3\x03"
  1867. "\x38\xcd";
  1868. f.inLen = XSTRLEN(f.input);
  1869. f.outLen = MD2_DIGEST_SIZE;
  1870. g.input = "1234567890123456789012345678901234567890123456789012345678"
  1871. "9012345678901234567890";
  1872. g.output = "\xd5\x97\x6f\x79\xd8\x3d\x3a\x0d\xc9\x80\x6c\x3c\x66\xf3"
  1873. "\xef\xd8";
  1874. g.inLen = XSTRLEN(g.input);
  1875. g.outLen = MD2_DIGEST_SIZE;
  1876. test_md2[0] = a;
  1877. test_md2[1] = b;
  1878. test_md2[2] = c;
  1879. test_md2[3] = d;
  1880. test_md2[4] = e;
  1881. test_md2[5] = f;
  1882. test_md2[6] = g;
  1883. wc_InitMd2(&md2);
  1884. for (i = 0; i < times; ++i) {
  1885. wc_Md2Update(&md2, (byte*)test_md2[i].input, (word32)test_md2[i].inLen);
  1886. wc_Md2Final(&md2, hash);
  1887. if (XMEMCMP(hash, test_md2[i].output, MD2_DIGEST_SIZE) != 0)
  1888. return -1500 - i;
  1889. }
  1890. for (i = 0; i < times; ++i) {
  1891. ret = wc_Md2Hash((byte*)test_md2[i].input, (word32)test_md2[i].inLen, hash);
  1892. if (ret != 0) {
  1893. return -1507 - i;
  1894. }
  1895. if (XMEMCMP(hash, test_md2[i].output, MD2_DIGEST_SIZE) != 0) {
  1896. return -1507 - i;
  1897. }
  1898. }
  1899. return 0;
  1900. }
  1901. #endif
  1902. #ifndef NO_MD5
  1903. WOLFSSL_TEST_SUBROUTINE int md5_test(void)
  1904. {
  1905. int ret = 0;
  1906. wc_Md5 md5, md5Copy;
  1907. byte hash[WC_MD5_DIGEST_SIZE];
  1908. byte hashcopy[WC_MD5_DIGEST_SIZE];
  1909. testVector a, b, c, d, e, f;
  1910. testVector test_md5[6];
  1911. int times = sizeof(test_md5) / sizeof(testVector), i;
  1912. a.input = "";
  1913. a.output = "\xd4\x1d\x8c\xd9\x8f\x00\xb2\x04\xe9\x80\x09\x98\xec\xf8\x42"
  1914. "\x7e";
  1915. a.inLen = XSTRLEN(a.input);
  1916. a.outLen = WC_MD5_DIGEST_SIZE;
  1917. b.input = "abc";
  1918. b.output = "\x90\x01\x50\x98\x3c\xd2\x4f\xb0\xd6\x96\x3f\x7d\x28\xe1\x7f"
  1919. "\x72";
  1920. b.inLen = XSTRLEN(b.input);
  1921. b.outLen = WC_MD5_DIGEST_SIZE;
  1922. c.input = "message digest";
  1923. c.output = "\xf9\x6b\x69\x7d\x7c\xb7\x93\x8d\x52\x5a\x2f\x31\xaa\xf1\x61"
  1924. "\xd0";
  1925. c.inLen = XSTRLEN(c.input);
  1926. c.outLen = WC_MD5_DIGEST_SIZE;
  1927. d.input = "abcdefghijklmnopqrstuvwxyz";
  1928. d.output = "\xc3\xfc\xd3\xd7\x61\x92\xe4\x00\x7d\xfb\x49\x6c\xca\x67\xe1"
  1929. "\x3b";
  1930. d.inLen = XSTRLEN(d.input);
  1931. d.outLen = WC_MD5_DIGEST_SIZE;
  1932. e.input = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345"
  1933. "6789";
  1934. e.output = "\xd1\x74\xab\x98\xd2\x77\xd9\xf5\xa5\x61\x1c\x2c\x9f\x41\x9d"
  1935. "\x9f";
  1936. e.inLen = XSTRLEN(e.input);
  1937. e.outLen = WC_MD5_DIGEST_SIZE;
  1938. f.input = "1234567890123456789012345678901234567890123456789012345678"
  1939. "9012345678901234567890";
  1940. f.output = "\x57\xed\xf4\xa2\x2b\xe3\xc9\x55\xac\x49\xda\x2e\x21\x07\xb6"
  1941. "\x7a";
  1942. f.inLen = XSTRLEN(f.input);
  1943. f.outLen = WC_MD5_DIGEST_SIZE;
  1944. test_md5[0] = a;
  1945. test_md5[1] = b;
  1946. test_md5[2] = c;
  1947. test_md5[3] = d;
  1948. test_md5[4] = e;
  1949. test_md5[5] = f;
  1950. ret = wc_InitMd5_ex(&md5, HEAP_HINT, devId);
  1951. if (ret != 0)
  1952. return -1600;
  1953. ret = wc_InitMd5_ex(&md5Copy, HEAP_HINT, devId);
  1954. if (ret != 0) {
  1955. wc_Md5Free(&md5);
  1956. return -1601;
  1957. }
  1958. for (i = 0; i < times; ++i) {
  1959. ret = wc_Md5Update(&md5, (byte*)test_md5[i].input,
  1960. (word32)test_md5[i].inLen);
  1961. if (ret != 0)
  1962. ERROR_OUT(-1602 - i, exit);
  1963. ret = wc_Md5GetHash(&md5, hashcopy);
  1964. if (ret != 0)
  1965. ERROR_OUT(-1603 - i, exit);
  1966. ret = wc_Md5Copy(&md5, &md5Copy);
  1967. if (ret != 0)
  1968. ERROR_OUT(-1604 - i, exit);
  1969. ret = wc_Md5Final(&md5, hash);
  1970. if (ret != 0)
  1971. ERROR_OUT(-1605 - i, exit);
  1972. wc_Md5Free(&md5Copy);
  1973. if (XMEMCMP(hash, test_md5[i].output, WC_MD5_DIGEST_SIZE) != 0)
  1974. ERROR_OUT(-1606 - i, exit);
  1975. if (XMEMCMP(hash, hashcopy, WC_MD5_DIGEST_SIZE) != 0)
  1976. ERROR_OUT(-1607 - i, exit);
  1977. }
  1978. #ifndef NO_LARGE_HASH_TEST
  1979. /* BEGIN LARGE HASH TEST */ {
  1980. byte large_input[1024];
  1981. const char* large_digest =
  1982. "\x44\xd0\x88\xce\xf1\x36\xd1\x78\xe9\xc8\xba\x84\xc3\xfd\xf6\xca";
  1983. for (i = 0; i < (int)sizeof(large_input); i++) {
  1984. large_input[i] = (byte)(i & 0xFF);
  1985. }
  1986. times = 100;
  1987. #ifdef WOLFSSL_PIC32MZ_HASH
  1988. wc_Md5SizeSet(&md5, times * sizeof(large_input));
  1989. #endif
  1990. for (i = 0; i < times; ++i) {
  1991. ret = wc_Md5Update(&md5, (byte*)large_input,
  1992. (word32)sizeof(large_input));
  1993. if (ret != 0)
  1994. ERROR_OUT(-1608, exit);
  1995. }
  1996. ret = wc_Md5Final(&md5, hash);
  1997. if (ret != 0)
  1998. ERROR_OUT(-1609, exit);
  1999. if (XMEMCMP(hash, large_digest, WC_MD5_DIGEST_SIZE) != 0)
  2000. ERROR_OUT(-1610, exit);
  2001. } /* END LARGE HASH TEST */
  2002. #endif /* NO_LARGE_HASH_TEST */
  2003. exit:
  2004. wc_Md5Free(&md5);
  2005. wc_Md5Free(&md5Copy);
  2006. return ret;
  2007. }
  2008. #endif /* NO_MD5 */
  2009. #ifndef NO_MD4
  2010. WOLFSSL_TEST_SUBROUTINE int md4_test(void)
  2011. {
  2012. Md4 md4;
  2013. byte hash[MD4_DIGEST_SIZE];
  2014. testVector a, b, c, d, e, f, g;
  2015. testVector test_md4[7];
  2016. int times = sizeof(test_md4) / sizeof(testVector), i;
  2017. a.input = "";
  2018. a.output = "\x31\xd6\xcf\xe0\xd1\x6a\xe9\x31\xb7\x3c\x59\xd7\xe0\xc0\x89"
  2019. "\xc0";
  2020. a.inLen = XSTRLEN(a.input);
  2021. a.outLen = MD4_DIGEST_SIZE;
  2022. b.input = "a";
  2023. b.output = "\xbd\xe5\x2c\xb3\x1d\xe3\x3e\x46\x24\x5e\x05\xfb\xdb\xd6\xfb"
  2024. "\x24";
  2025. b.inLen = XSTRLEN(b.input);
  2026. b.outLen = MD4_DIGEST_SIZE;
  2027. c.input = "abc";
  2028. c.output = "\xa4\x48\x01\x7a\xaf\x21\xd8\x52\x5f\xc1\x0a\xe8\x7a\xa6\x72"
  2029. "\x9d";
  2030. c.inLen = XSTRLEN(c.input);
  2031. c.outLen = MD4_DIGEST_SIZE;
  2032. d.input = "message digest";
  2033. d.output = "\xd9\x13\x0a\x81\x64\x54\x9f\xe8\x18\x87\x48\x06\xe1\xc7\x01"
  2034. "\x4b";
  2035. d.inLen = XSTRLEN(d.input);
  2036. d.outLen = MD4_DIGEST_SIZE;
  2037. e.input = "abcdefghijklmnopqrstuvwxyz";
  2038. e.output = "\xd7\x9e\x1c\x30\x8a\xa5\xbb\xcd\xee\xa8\xed\x63\xdf\x41\x2d"
  2039. "\xa9";
  2040. e.inLen = XSTRLEN(e.input);
  2041. e.outLen = MD4_DIGEST_SIZE;
  2042. f.input = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345"
  2043. "6789";
  2044. f.output = "\x04\x3f\x85\x82\xf2\x41\xdb\x35\x1c\xe6\x27\xe1\x53\xe7\xf0"
  2045. "\xe4";
  2046. f.inLen = XSTRLEN(f.input);
  2047. f.outLen = MD4_DIGEST_SIZE;
  2048. g.input = "1234567890123456789012345678901234567890123456789012345678"
  2049. "9012345678901234567890";
  2050. g.output = "\xe3\x3b\x4d\xdc\x9c\x38\xf2\x19\x9c\x3e\x7b\x16\x4f\xcc\x05"
  2051. "\x36";
  2052. g.inLen = XSTRLEN(g.input);
  2053. g.outLen = MD4_DIGEST_SIZE;
  2054. test_md4[0] = a;
  2055. test_md4[1] = b;
  2056. test_md4[2] = c;
  2057. test_md4[3] = d;
  2058. test_md4[4] = e;
  2059. test_md4[5] = f;
  2060. test_md4[6] = g;
  2061. wc_InitMd4(&md4);
  2062. for (i = 0; i < times; ++i) {
  2063. wc_Md4Update(&md4, (byte*)test_md4[i].input, (word32)test_md4[i].inLen);
  2064. wc_Md4Final(&md4, hash);
  2065. if (XMEMCMP(hash, test_md4[i].output, MD4_DIGEST_SIZE) != 0)
  2066. return -1700 - i;
  2067. }
  2068. return 0;
  2069. }
  2070. #endif /* NO_MD4 */
  2071. #ifndef NO_SHA
  2072. WOLFSSL_TEST_SUBROUTINE int sha_test(void)
  2073. {
  2074. int ret = 0;
  2075. wc_Sha sha, shaCopy;
  2076. byte hash[WC_SHA_DIGEST_SIZE];
  2077. byte hashcopy[WC_SHA_DIGEST_SIZE];
  2078. testVector a, b, c, d, e;
  2079. testVector test_sha[5];
  2080. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2081. a.input = "";
  2082. a.output = "\xda\x39\xa3\xee\x5e\x6b\x4b\x0d\x32\x55\xbf\xef\x95\x60\x18"
  2083. "\x90\xaf\xd8\x07\x09";
  2084. a.inLen = XSTRLEN(a.input);
  2085. a.outLen = WC_SHA_DIGEST_SIZE;
  2086. b.input = "abc";
  2087. b.output = "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E\x25\x71\x78\x50\xC2"
  2088. "\x6C\x9C\xD0\xD8\x9D";
  2089. b.inLen = XSTRLEN(b.input);
  2090. b.outLen = WC_SHA_DIGEST_SIZE;
  2091. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  2092. c.output = "\x84\x98\x3E\x44\x1C\x3B\xD2\x6E\xBA\xAE\x4A\xA1\xF9\x51\x29"
  2093. "\xE5\xE5\x46\x70\xF1";
  2094. c.inLen = XSTRLEN(c.input);
  2095. c.outLen = WC_SHA_DIGEST_SIZE;
  2096. d.input = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  2097. "aaaaaa";
  2098. d.output = "\x00\x98\xBA\x82\x4B\x5C\x16\x42\x7B\xD7\xA1\x12\x2A\x5A\x44"
  2099. "\x2A\x25\xEC\x64\x4D";
  2100. d.inLen = XSTRLEN(d.input);
  2101. d.outLen = WC_SHA_DIGEST_SIZE;
  2102. e.input = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  2103. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  2104. "aaaaaaaaaa";
  2105. e.output = "\xAD\x5B\x3F\xDB\xCB\x52\x67\x78\xC2\x83\x9D\x2F\x15\x1E\xA7"
  2106. "\x53\x99\x5E\x26\xA0";
  2107. e.inLen = XSTRLEN(e.input);
  2108. e.outLen = WC_SHA_DIGEST_SIZE;
  2109. test_sha[0] = a;
  2110. test_sha[1] = b;
  2111. test_sha[2] = c;
  2112. test_sha[3] = d;
  2113. test_sha[4] = e;
  2114. ret = wc_InitSha_ex(&sha, HEAP_HINT, devId);
  2115. if (ret != 0)
  2116. return -1800;
  2117. ret = wc_InitSha_ex(&shaCopy, HEAP_HINT, devId);
  2118. if (ret != 0) {
  2119. wc_ShaFree(&sha);
  2120. return -1801;
  2121. }
  2122. for (i = 0; i < times; ++i) {
  2123. ret = wc_ShaUpdate(&sha, (byte*)test_sha[i].input,
  2124. (word32)test_sha[i].inLen);
  2125. if (ret != 0)
  2126. ERROR_OUT(-1802 - i, exit);
  2127. ret = wc_ShaGetHash(&sha, hashcopy);
  2128. if (ret != 0)
  2129. ERROR_OUT(-1803 - i, exit);
  2130. ret = wc_ShaCopy(&sha, &shaCopy);
  2131. if (ret != 0)
  2132. ERROR_OUT(-1804 - i, exit);
  2133. ret = wc_ShaFinal(&sha, hash);
  2134. if (ret != 0)
  2135. ERROR_OUT(-1805 - i, exit);
  2136. wc_ShaFree(&shaCopy);
  2137. if (XMEMCMP(hash, test_sha[i].output, WC_SHA_DIGEST_SIZE) != 0)
  2138. ERROR_OUT(-1806 - i, exit);
  2139. if (XMEMCMP(hash, hashcopy, WC_SHA_DIGEST_SIZE) != 0)
  2140. ERROR_OUT(-1807 - i, exit);
  2141. }
  2142. #ifndef NO_LARGE_HASH_TEST
  2143. /* BEGIN LARGE HASH TEST */ {
  2144. byte large_input[1024];
  2145. #if defined(WOLFSSL_RENESAS_TSIP) || defined(WOLFSSL_RENESAS_SCEPROTECT) || \
  2146. defined(HASH_SIZE_LIMIT)
  2147. const char* large_digest =
  2148. "\x1d\x6a\x5a\xf6\xe5\x7c\x86\xce\x7f\x7c\xaf\xd5\xdb\x08\xcd\x59"
  2149. "\x15\x8c\x6d\xb6";
  2150. #else
  2151. const char* large_digest =
  2152. "\x8b\x77\x02\x48\x39\xe8\xdb\xd3\x9a\xf4\x05\x24\x66\x12\x2d\x9e"
  2153. "\xc5\xd9\x0a\xac";
  2154. #endif
  2155. for (i = 0; i < (int)sizeof(large_input); i++) {
  2156. large_input[i] = (byte)(i & 0xFF);
  2157. }
  2158. #if defined(WOLFSSL_RENESAS_TSIP) || defined(WOLFSSL_RENESAS_SCEPROTECT) || \
  2159. defined(HASH_SIZE_LIMIT)
  2160. times = 20;
  2161. #else
  2162. times = 100;
  2163. #endif
  2164. #ifdef WOLFSSL_PIC32MZ_HASH
  2165. wc_ShaSizeSet(&sha, times * sizeof(large_input));
  2166. #endif
  2167. for (i = 0; i < times; ++i) {
  2168. ret = wc_ShaUpdate(&sha, (byte*)large_input,
  2169. (word32)sizeof(large_input));
  2170. if (ret != 0)
  2171. ERROR_OUT(-1808, exit);
  2172. }
  2173. ret = wc_ShaFinal(&sha, hash);
  2174. if (ret != 0)
  2175. ERROR_OUT(-1809, exit);
  2176. if (XMEMCMP(hash, large_digest, WC_SHA_DIGEST_SIZE) != 0)
  2177. ERROR_OUT(-1810, exit);
  2178. } /* END LARGE HASH TEST */
  2179. #endif /* NO_LARGE_HASH_TEST */
  2180. exit:
  2181. wc_ShaFree(&sha);
  2182. wc_ShaFree(&shaCopy);
  2183. return ret;
  2184. }
  2185. #endif /* NO_SHA */
  2186. #ifdef WOLFSSL_RIPEMD
  2187. WOLFSSL_TEST_SUBROUTINE int ripemd_test(void)
  2188. {
  2189. RipeMd ripemd;
  2190. int ret;
  2191. byte hash[RIPEMD_DIGEST_SIZE];
  2192. testVector a, b, c, d;
  2193. testVector test_ripemd[4];
  2194. int times = sizeof(test_ripemd) / sizeof(struct testVector), i;
  2195. a.input = "abc";
  2196. a.output = "\x8e\xb2\x08\xf7\xe0\x5d\x98\x7a\x9b\x04\x4a\x8e\x98\xc6"
  2197. "\xb0\x87\xf1\x5a\x0b\xfc";
  2198. a.inLen = XSTRLEN(a.input);
  2199. a.outLen = RIPEMD_DIGEST_SIZE;
  2200. b.input = "message digest";
  2201. b.output = "\x5d\x06\x89\xef\x49\xd2\xfa\xe5\x72\xb8\x81\xb1\x23\xa8"
  2202. "\x5f\xfa\x21\x59\x5f\x36";
  2203. b.inLen = XSTRLEN(b.input);
  2204. b.outLen = RIPEMD_DIGEST_SIZE;
  2205. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  2206. c.output = "\x12\xa0\x53\x38\x4a\x9c\x0c\x88\xe4\x05\xa0\x6c\x27\xdc"
  2207. "\xf4\x9a\xda\x62\xeb\x2b";
  2208. c.inLen = XSTRLEN(c.input);
  2209. c.outLen = RIPEMD_DIGEST_SIZE;
  2210. d.input = "12345678901234567890123456789012345678901234567890123456"
  2211. "789012345678901234567890";
  2212. d.output = "\x9b\x75\x2e\x45\x57\x3d\x4b\x39\xf4\xdb\xd3\x32\x3c\xab"
  2213. "\x82\xbf\x63\x32\x6b\xfb";
  2214. d.inLen = XSTRLEN(d.input);
  2215. d.outLen = RIPEMD_DIGEST_SIZE;
  2216. test_ripemd[0] = a;
  2217. test_ripemd[1] = b;
  2218. test_ripemd[2] = c;
  2219. test_ripemd[3] = d;
  2220. ret = wc_InitRipeMd(&ripemd);
  2221. if (ret != 0) {
  2222. return -1900;
  2223. }
  2224. for (i = 0; i < times; ++i) {
  2225. ret = wc_RipeMdUpdate(&ripemd, (byte*)test_ripemd[i].input,
  2226. (word32)test_ripemd[i].inLen);
  2227. if (ret != 0) {
  2228. return -1901 - i;
  2229. }
  2230. ret = wc_RipeMdFinal(&ripemd, hash);
  2231. if (ret != 0) {
  2232. return -1911 - i;
  2233. }
  2234. if (XMEMCMP(hash, test_ripemd[i].output, RIPEMD_DIGEST_SIZE) != 0)
  2235. return -1921 - i;
  2236. }
  2237. return 0;
  2238. }
  2239. #endif /* WOLFSSL_RIPEMD */
  2240. #ifdef HAVE_BLAKE2
  2241. #define BLAKE2B_TESTS 3
  2242. static const byte blake2b_vec[BLAKE2B_TESTS][BLAKE2B_OUTBYTES] =
  2243. {
  2244. {
  2245. 0x78, 0x6A, 0x02, 0xF7, 0x42, 0x01, 0x59, 0x03,
  2246. 0xC6, 0xC6, 0xFD, 0x85, 0x25, 0x52, 0xD2, 0x72,
  2247. 0x91, 0x2F, 0x47, 0x40, 0xE1, 0x58, 0x47, 0x61,
  2248. 0x8A, 0x86, 0xE2, 0x17, 0xF7, 0x1F, 0x54, 0x19,
  2249. 0xD2, 0x5E, 0x10, 0x31, 0xAF, 0xEE, 0x58, 0x53,
  2250. 0x13, 0x89, 0x64, 0x44, 0x93, 0x4E, 0xB0, 0x4B,
  2251. 0x90, 0x3A, 0x68, 0x5B, 0x14, 0x48, 0xB7, 0x55,
  2252. 0xD5, 0x6F, 0x70, 0x1A, 0xFE, 0x9B, 0xE2, 0xCE
  2253. },
  2254. {
  2255. 0x2F, 0xA3, 0xF6, 0x86, 0xDF, 0x87, 0x69, 0x95,
  2256. 0x16, 0x7E, 0x7C, 0x2E, 0x5D, 0x74, 0xC4, 0xC7,
  2257. 0xB6, 0xE4, 0x8F, 0x80, 0x68, 0xFE, 0x0E, 0x44,
  2258. 0x20, 0x83, 0x44, 0xD4, 0x80, 0xF7, 0x90, 0x4C,
  2259. 0x36, 0x96, 0x3E, 0x44, 0x11, 0x5F, 0xE3, 0xEB,
  2260. 0x2A, 0x3A, 0xC8, 0x69, 0x4C, 0x28, 0xBC, 0xB4,
  2261. 0xF5, 0xA0, 0xF3, 0x27, 0x6F, 0x2E, 0x79, 0x48,
  2262. 0x7D, 0x82, 0x19, 0x05, 0x7A, 0x50, 0x6E, 0x4B
  2263. },
  2264. {
  2265. 0x1C, 0x08, 0x79, 0x8D, 0xC6, 0x41, 0xAB, 0xA9,
  2266. 0xDE, 0xE4, 0x35, 0xE2, 0x25, 0x19, 0xA4, 0x72,
  2267. 0x9A, 0x09, 0xB2, 0xBF, 0xE0, 0xFF, 0x00, 0xEF,
  2268. 0x2D, 0xCD, 0x8E, 0xD6, 0xF8, 0xA0, 0x7D, 0x15,
  2269. 0xEA, 0xF4, 0xAE, 0xE5, 0x2B, 0xBF, 0x18, 0xAB,
  2270. 0x56, 0x08, 0xA6, 0x19, 0x0F, 0x70, 0xB9, 0x04,
  2271. 0x86, 0xC8, 0xA7, 0xD4, 0x87, 0x37, 0x10, 0xB1,
  2272. 0x11, 0x5D, 0x3D, 0xEB, 0xBB, 0x43, 0x27, 0xB5
  2273. }
  2274. };
  2275. WOLFSSL_TEST_SUBROUTINE int blake2b_test(void)
  2276. {
  2277. Blake2b b2b;
  2278. byte digest[64];
  2279. byte input[64];
  2280. int i, ret;
  2281. for (i = 0; i < (int)sizeof(input); i++)
  2282. input[i] = (byte)i;
  2283. for (i = 0; i < BLAKE2B_TESTS; i++) {
  2284. ret = wc_InitBlake2b(&b2b, 64);
  2285. if (ret != 0)
  2286. return -2000 - i;
  2287. ret = wc_Blake2bUpdate(&b2b, input, i);
  2288. if (ret != 0)
  2289. return -2010 - 1;
  2290. ret = wc_Blake2bFinal(&b2b, digest, 64);
  2291. if (ret != 0)
  2292. return -2020 - i;
  2293. if (XMEMCMP(digest, blake2b_vec[i], 64) != 0) {
  2294. return -2030 - i;
  2295. }
  2296. }
  2297. return 0;
  2298. }
  2299. #endif /* HAVE_BLAKE2 */
  2300. #ifdef HAVE_BLAKE2S
  2301. #define BLAKE2S_TESTS 3
  2302. static const byte blake2s_vec[BLAKE2S_TESTS][BLAKE2S_OUTBYTES] =
  2303. {
  2304. {
  2305. 0x69, 0x21, 0x7a, 0x30, 0x79, 0x90, 0x80, 0x94,
  2306. 0xe1, 0x11, 0x21, 0xd0, 0x42, 0x35, 0x4a, 0x7c,
  2307. 0x1f, 0x55, 0xb6, 0x48, 0x2c, 0xa1, 0xa5, 0x1e,
  2308. 0x1b, 0x25, 0x0d, 0xfd, 0x1e, 0xd0, 0xee, 0xf9,
  2309. },
  2310. {
  2311. 0xe3, 0x4d, 0x74, 0xdb, 0xaf, 0x4f, 0xf4, 0xc6,
  2312. 0xab, 0xd8, 0x71, 0xcc, 0x22, 0x04, 0x51, 0xd2,
  2313. 0xea, 0x26, 0x48, 0x84, 0x6c, 0x77, 0x57, 0xfb,
  2314. 0xaa, 0xc8, 0x2f, 0xe5, 0x1a, 0xd6, 0x4b, 0xea,
  2315. },
  2316. {
  2317. 0xdd, 0xad, 0x9a, 0xb1, 0x5d, 0xac, 0x45, 0x49,
  2318. 0xba, 0x42, 0xf4, 0x9d, 0x26, 0x24, 0x96, 0xbe,
  2319. 0xf6, 0xc0, 0xba, 0xe1, 0xdd, 0x34, 0x2a, 0x88,
  2320. 0x08, 0xf8, 0xea, 0x26, 0x7c, 0x6e, 0x21, 0x0c,
  2321. }
  2322. };
  2323. WOLFSSL_TEST_SUBROUTINE int blake2s_test(void)
  2324. {
  2325. Blake2s b2s;
  2326. byte digest[32];
  2327. byte input[64];
  2328. int i, ret;
  2329. for (i = 0; i < (int)sizeof(input); i++)
  2330. input[i] = (byte)i;
  2331. for (i = 0; i < BLAKE2S_TESTS; i++) {
  2332. ret = wc_InitBlake2s(&b2s, 32);
  2333. if (ret != 0)
  2334. return -2100 - i;
  2335. ret = wc_Blake2sUpdate(&b2s, input, i);
  2336. if (ret != 0)
  2337. return -2110 - 1;
  2338. ret = wc_Blake2sFinal(&b2s, digest, 32);
  2339. if (ret != 0)
  2340. return -2120 - i;
  2341. if (XMEMCMP(digest, blake2s_vec[i], 32) != 0) {
  2342. return -2130 - i;
  2343. }
  2344. }
  2345. return 0;
  2346. }
  2347. #endif /* HAVE_BLAKE2S */
  2348. #ifdef WOLFSSL_SHA224
  2349. WOLFSSL_TEST_SUBROUTINE int sha224_test(void)
  2350. {
  2351. wc_Sha224 sha, shaCopy;
  2352. byte hash[WC_SHA224_DIGEST_SIZE];
  2353. byte hashcopy[WC_SHA224_DIGEST_SIZE];
  2354. int ret = 0;
  2355. testVector a, b, c;
  2356. testVector test_sha[3];
  2357. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2358. a.input = "";
  2359. a.output = "\xd1\x4a\x02\x8c\x2a\x3a\x2b\xc9\x47\x61\x02\xbb\x28\x82\x34"
  2360. "\xc4\x15\xa2\xb0\x1f\x82\x8e\xa6\x2a\xc5\xb3\xe4\x2f";
  2361. a.inLen = XSTRLEN(a.input);
  2362. a.outLen = WC_SHA224_DIGEST_SIZE;
  2363. b.input = "abc";
  2364. b.output = "\x23\x09\x7d\x22\x34\x05\xd8\x22\x86\x42\xa4\x77\xbd\xa2\x55"
  2365. "\xb3\x2a\xad\xbc\xe4\xbd\xa0\xb3\xf7\xe3\x6c\x9d\xa7";
  2366. b.inLen = XSTRLEN(b.input);
  2367. b.outLen = WC_SHA224_DIGEST_SIZE;
  2368. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  2369. c.output = "\x75\x38\x8b\x16\x51\x27\x76\xcc\x5d\xba\x5d\xa1\xfd\x89\x01"
  2370. "\x50\xb0\xc6\x45\x5c\xb4\xf5\x8b\x19\x52\x52\x25\x25";
  2371. c.inLen = XSTRLEN(c.input);
  2372. c.outLen = WC_SHA224_DIGEST_SIZE;
  2373. test_sha[0] = a;
  2374. test_sha[1] = b;
  2375. test_sha[2] = c;
  2376. ret = wc_InitSha224_ex(&sha, HEAP_HINT, devId);
  2377. if (ret != 0)
  2378. return -2200;
  2379. ret = wc_InitSha224_ex(&shaCopy, HEAP_HINT, devId);
  2380. if (ret != 0) {
  2381. wc_Sha224Free(&sha);
  2382. return -2201;
  2383. }
  2384. for (i = 0; i < times; ++i) {
  2385. ret = wc_Sha224Update(&sha, (byte*)test_sha[i].input,
  2386. (word32)test_sha[i].inLen);
  2387. if (ret != 0)
  2388. ERROR_OUT(-2202 - i, exit);
  2389. ret = wc_Sha224GetHash(&sha, hashcopy);
  2390. if (ret != 0)
  2391. ERROR_OUT(-2203 - i, exit);
  2392. ret = wc_Sha224Copy(&sha, &shaCopy);
  2393. if (ret != 0)
  2394. ERROR_OUT(-2204 - i, exit);
  2395. ret = wc_Sha224Final(&sha, hash);
  2396. if (ret != 0)
  2397. ERROR_OUT(-2205 - i, exit);
  2398. wc_Sha224Free(&shaCopy);
  2399. if (XMEMCMP(hash, test_sha[i].output, WC_SHA224_DIGEST_SIZE) != 0)
  2400. ERROR_OUT(-2206 - i, exit);
  2401. if (XMEMCMP(hash, hashcopy, WC_SHA224_DIGEST_SIZE) != 0)
  2402. ERROR_OUT(-2207 - i, exit);
  2403. }
  2404. exit:
  2405. wc_Sha224Free(&sha);
  2406. wc_Sha224Free(&shaCopy);
  2407. return ret;
  2408. }
  2409. #endif
  2410. #ifndef NO_SHA256
  2411. WOLFSSL_TEST_SUBROUTINE int sha256_test(void)
  2412. {
  2413. wc_Sha256 sha, shaCopy;
  2414. byte hash[WC_SHA256_DIGEST_SIZE];
  2415. byte hashcopy[WC_SHA256_DIGEST_SIZE];
  2416. int ret = 0;
  2417. testVector a, b, c;
  2418. testVector test_sha[3];
  2419. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2420. a.input = "";
  2421. a.output = "\xe3\xb0\xc4\x42\x98\xfc\x1c\x14\x9a\xfb\xf4\xc8\x99\x6f\xb9"
  2422. "\x24\x27\xae\x41\xe4\x64\x9b\x93\x4c\xa4\x95\x99\x1b\x78\x52"
  2423. "\xb8\x55";
  2424. a.inLen = XSTRLEN(a.input);
  2425. a.outLen = WC_SHA256_DIGEST_SIZE;
  2426. b.input = "abc";
  2427. b.output = "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  2428. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  2429. "\x15\xAD";
  2430. b.inLen = XSTRLEN(b.input);
  2431. b.outLen = WC_SHA256_DIGEST_SIZE;
  2432. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  2433. c.output = "\x24\x8D\x6A\x61\xD2\x06\x38\xB8\xE5\xC0\x26\x93\x0C\x3E\x60"
  2434. "\x39\xA3\x3C\xE4\x59\x64\xFF\x21\x67\xF6\xEC\xED\xD4\x19\xDB"
  2435. "\x06\xC1";
  2436. c.inLen = XSTRLEN(c.input);
  2437. c.outLen = WC_SHA256_DIGEST_SIZE;
  2438. test_sha[0] = a;
  2439. test_sha[1] = b;
  2440. test_sha[2] = c;
  2441. ret = wc_InitSha256_ex(&sha, HEAP_HINT, devId);
  2442. if (ret != 0)
  2443. return -2300;
  2444. ret = wc_InitSha256_ex(&shaCopy, HEAP_HINT, devId);
  2445. if (ret != 0) {
  2446. wc_Sha256Free(&sha);
  2447. return -2301;
  2448. }
  2449. for (i = 0; i < times; ++i) {
  2450. ret = wc_Sha256Update(&sha, (byte*)test_sha[i].input,
  2451. (word32)test_sha[i].inLen);
  2452. if (ret != 0) {
  2453. ERROR_OUT(-2302 - i, exit);
  2454. }
  2455. ret = wc_Sha256GetHash(&sha, hashcopy);
  2456. if (ret != 0)
  2457. ERROR_OUT(-2303 - i, exit);
  2458. ret = wc_Sha256Copy(&sha, &shaCopy);
  2459. if (ret != 0)
  2460. ERROR_OUT(-2304 - i, exit);
  2461. ret = wc_Sha256Final(&sha, hash);
  2462. if (ret != 0)
  2463. ERROR_OUT(-2305 - i, exit);
  2464. wc_Sha256Free(&shaCopy);
  2465. if (XMEMCMP(hash, test_sha[i].output, WC_SHA256_DIGEST_SIZE) != 0)
  2466. ERROR_OUT(-2306 - i, exit);
  2467. if (XMEMCMP(hash, hashcopy, WC_SHA256_DIGEST_SIZE) != 0)
  2468. ERROR_OUT(-2307 - i, exit);
  2469. }
  2470. #ifndef NO_LARGE_HASH_TEST
  2471. /* BEGIN LARGE HASH TEST */ {
  2472. byte large_input[1024];
  2473. #ifdef HASH_SIZE_LIMIT
  2474. const char* large_digest =
  2475. "\xa4\x75\x9e\x7a\xa2\x03\x38\x32\x88\x66\xa2\xea\x17\xea\xf8\xc7"
  2476. "\xfe\x4e\xc6\xbb\xe3\xbb\x71\xce\xe7\xdf\x7c\x04\x61\xb3\xc2\x2f";
  2477. #else
  2478. const char* large_digest =
  2479. "\x27\x78\x3e\x87\x96\x3a\x4e\xfb\x68\x29\xb5\x31\xc9\xba\x57\xb4"
  2480. "\x4f\x45\x79\x7f\x67\x70\xbd\x63\x7f\xbf\x0d\x80\x7c\xbd\xba\xe0";
  2481. #endif
  2482. for (i = 0; i < (int)sizeof(large_input); i++) {
  2483. large_input[i] = (byte)(i & 0xFF);
  2484. }
  2485. #ifdef HASH_SIZE_LIMIT
  2486. times = 20;
  2487. #else
  2488. times = 100;
  2489. #endif
  2490. #ifdef WOLFSSL_PIC32MZ_HASH
  2491. wc_Sha256SizeSet(&sha, times * sizeof(large_input));
  2492. #endif
  2493. for (i = 0; i < times; ++i) {
  2494. ret = wc_Sha256Update(&sha, (byte*)large_input,
  2495. (word32)sizeof(large_input));
  2496. if (ret != 0)
  2497. ERROR_OUT(-2308, exit);
  2498. }
  2499. ret = wc_Sha256Final(&sha, hash);
  2500. if (ret != 0)
  2501. ERROR_OUT(-2309, exit);
  2502. if (XMEMCMP(hash, large_digest, WC_SHA256_DIGEST_SIZE) != 0)
  2503. ERROR_OUT(-2310, exit);
  2504. } /* END LARGE HASH TEST */
  2505. #endif /* NO_LARGE_HASH_TEST */
  2506. exit:
  2507. wc_Sha256Free(&sha);
  2508. wc_Sha256Free(&shaCopy);
  2509. return ret;
  2510. }
  2511. #endif
  2512. #ifdef WOLFSSL_SHA512
  2513. WOLFSSL_TEST_SUBROUTINE int sha512_test(void)
  2514. {
  2515. wc_Sha512 sha, shaCopy;
  2516. byte hash[WC_SHA512_DIGEST_SIZE];
  2517. byte hashcopy[WC_SHA512_DIGEST_SIZE];
  2518. int ret = 0;
  2519. testVector a, b, c;
  2520. testVector test_sha[3];
  2521. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2522. a.input = "";
  2523. a.output = "\xcf\x83\xe1\x35\x7e\xef\xb8\xbd\xf1\x54\x28\x50\xd6\x6d\x80"
  2524. "\x07\xd6\x20\xe4\x05\x0b\x57\x15\xdc\x83\xf4\xa9\x21\xd3\x6c"
  2525. "\xe9\xce\x47\xd0\xd1\x3c\x5d\x85\xf2\xb0\xff\x83\x18\xd2\x87"
  2526. "\x7e\xec\x2f\x63\xb9\x31\xbd\x47\x41\x7a\x81\xa5\x38\x32\x7a"
  2527. "\xf9\x27\xda\x3e";
  2528. a.inLen = XSTRLEN(a.input);
  2529. a.outLen = WC_SHA512_DIGEST_SIZE;
  2530. b.input = "abc";
  2531. b.output = "\xdd\xaf\x35\xa1\x93\x61\x7a\xba\xcc\x41\x73\x49\xae\x20\x41"
  2532. "\x31\x12\xe6\xfa\x4e\x89\xa9\x7e\xa2\x0a\x9e\xee\xe6\x4b\x55"
  2533. "\xd3\x9a\x21\x92\x99\x2a\x27\x4f\xc1\xa8\x36\xba\x3c\x23\xa3"
  2534. "\xfe\xeb\xbd\x45\x4d\x44\x23\x64\x3c\xe8\x0e\x2a\x9a\xc9\x4f"
  2535. "\xa5\x4c\xa4\x9f";
  2536. b.inLen = XSTRLEN(b.input);
  2537. b.outLen = WC_SHA512_DIGEST_SIZE;
  2538. c.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  2539. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  2540. c.output = "\x8e\x95\x9b\x75\xda\xe3\x13\xda\x8c\xf4\xf7\x28\x14\xfc\x14"
  2541. "\x3f\x8f\x77\x79\xc6\xeb\x9f\x7f\xa1\x72\x99\xae\xad\xb6\x88"
  2542. "\x90\x18\x50\x1d\x28\x9e\x49\x00\xf7\xe4\x33\x1b\x99\xde\xc4"
  2543. "\xb5\x43\x3a\xc7\xd3\x29\xee\xb6\xdd\x26\x54\x5e\x96\xe5\x5b"
  2544. "\x87\x4b\xe9\x09";
  2545. c.inLen = XSTRLEN(c.input);
  2546. c.outLen = WC_SHA512_DIGEST_SIZE;
  2547. test_sha[0] = a;
  2548. test_sha[1] = b;
  2549. test_sha[2] = c;
  2550. ret = wc_InitSha512_ex(&sha, HEAP_HINT, devId);
  2551. if (ret != 0)
  2552. return -2400;
  2553. ret = wc_InitSha512_ex(&shaCopy, HEAP_HINT, devId);
  2554. if (ret != 0) {
  2555. wc_Sha512Free(&sha);
  2556. return -2401;
  2557. }
  2558. for (i = 0; i < times; ++i) {
  2559. ret = wc_Sha512Update(&sha, (byte*)test_sha[i].input,
  2560. (word32)test_sha[i].inLen);
  2561. if (ret != 0)
  2562. ERROR_OUT(-2402 - i, exit);
  2563. ret = wc_Sha512GetHash(&sha, hashcopy);
  2564. if (ret != 0)
  2565. ERROR_OUT(-2403 - i, exit);
  2566. ret = wc_Sha512Copy(&sha, &shaCopy);
  2567. if (ret != 0)
  2568. ERROR_OUT(-2404 - i, exit);
  2569. ret = wc_Sha512Final(&sha, hash);
  2570. if (ret != 0)
  2571. ERROR_OUT(-2405 - i, exit);
  2572. wc_Sha512Free(&shaCopy);
  2573. if (XMEMCMP(hash, test_sha[i].output, WC_SHA512_DIGEST_SIZE) != 0)
  2574. ERROR_OUT(-2406 - i, exit);
  2575. if (XMEMCMP(hash, hashcopy, WC_SHA512_DIGEST_SIZE) != 0)
  2576. ERROR_OUT(-2407 - i, exit);
  2577. }
  2578. #ifndef NO_LARGE_HASH_TEST
  2579. /* BEGIN LARGE HASH TEST */ {
  2580. byte large_input[1024];
  2581. #ifdef HASH_SIZE_LIMIT
  2582. const char* large_digest =
  2583. "\x30\x9B\x96\xA6\xE9\x43\x78\x30\xA3\x71\x51\x61\xC1\xEB\xE1\xBE"
  2584. "\xC8\xA5\xF9\x13\x5A\xD6\x6D\x9E\x46\x31\x31\x67\x8D\xE2\xC0\x0B"
  2585. "\x2A\x1A\x03\xE1\xF3\x48\xA7\x33\xBD\x49\xF8\xFF\xF1\xC2\xC2\x95"
  2586. "\xCB\xF0\xAF\x87\x61\x85\x58\x63\x6A\xCA\x70\x9C\x8B\x83\x3F\x5D";
  2587. #else
  2588. const char* large_digest =
  2589. "\x5a\x1f\x73\x90\xbd\x8c\xe4\x63\x54\xce\xa0\x9b\xef\x32\x78\x2d"
  2590. "\x2e\xe7\x0d\x5e\x2f\x9d\x15\x1b\xdd\x2d\xde\x65\x0c\x7b\xfa\x83"
  2591. "\x5e\x80\x02\x13\x84\xb8\x3f\xff\x71\x62\xb5\x09\x89\x63\xe1\xdc"
  2592. "\xa5\xdc\xfc\xfa\x9d\x1a\x4d\xc0\xfa\x3a\x14\xf6\x01\x51\x90\xa4";
  2593. #endif
  2594. for (i = 0; i < (int)sizeof(large_input); i++) {
  2595. large_input[i] = (byte)(i & 0xFF);
  2596. }
  2597. #ifdef HASH_SIZE_LIMIT
  2598. times = 20;
  2599. #else
  2600. times = 100;
  2601. #endif
  2602. for (i = 0; i < times; ++i) {
  2603. ret = wc_Sha512Update(&sha, (byte*)large_input,
  2604. (word32)sizeof(large_input));
  2605. if (ret != 0)
  2606. ERROR_OUT(-2408, exit);
  2607. }
  2608. ret = wc_Sha512Final(&sha, hash);
  2609. if (ret != 0)
  2610. ERROR_OUT(-2409, exit);
  2611. if (XMEMCMP(hash, large_digest, WC_SHA512_DIGEST_SIZE) != 0)
  2612. ERROR_OUT(-2410, exit);
  2613. #ifndef NO_UNALIGNED_MEMORY_TEST
  2614. /* Unaligned memory access test */
  2615. for (i = 1; i < 16; i++) {
  2616. ret = wc_Sha512Update(&sha, (byte*)large_input + i,
  2617. (word32)sizeof(large_input) - i);
  2618. if (ret != 0)
  2619. ERROR_OUT(-2411, exit);
  2620. ret = wc_Sha512Final(&sha, hash);
  2621. }
  2622. #endif
  2623. } /* END LARGE HASH TEST */
  2624. #endif /* NO_LARGE_HASH_TEST */
  2625. exit:
  2626. wc_Sha512Free(&sha);
  2627. wc_Sha512Free(&shaCopy);
  2628. return ret;
  2629. }
  2630. #endif
  2631. #ifdef WOLFSSL_SHA384
  2632. WOLFSSL_TEST_SUBROUTINE int sha384_test(void)
  2633. {
  2634. wc_Sha384 sha, shaCopy;
  2635. byte hash[WC_SHA384_DIGEST_SIZE];
  2636. byte hashcopy[WC_SHA384_DIGEST_SIZE];
  2637. int ret = 0;
  2638. testVector a, b, c;
  2639. testVector test_sha[3];
  2640. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2641. a.input = "";
  2642. a.output = "\x38\xb0\x60\xa7\x51\xac\x96\x38\x4c\xd9\x32\x7e\xb1\xb1\xe3"
  2643. "\x6a\x21\xfd\xb7\x11\x14\xbe\x07\x43\x4c\x0c\xc7\xbf\x63\xf6"
  2644. "\xe1\xda\x27\x4e\xde\xbf\xe7\x6f\x65\xfb\xd5\x1a\xd2\xf1\x48"
  2645. "\x98\xb9\x5b";
  2646. a.inLen = XSTRLEN(a.input);
  2647. a.outLen = WC_SHA384_DIGEST_SIZE;
  2648. b.input = "abc";
  2649. b.output = "\xcb\x00\x75\x3f\x45\xa3\x5e\x8b\xb5\xa0\x3d\x69\x9a\xc6\x50"
  2650. "\x07\x27\x2c\x32\xab\x0e\xde\xd1\x63\x1a\x8b\x60\x5a\x43\xff"
  2651. "\x5b\xed\x80\x86\x07\x2b\xa1\xe7\xcc\x23\x58\xba\xec\xa1\x34"
  2652. "\xc8\x25\xa7";
  2653. b.inLen = XSTRLEN(b.input);
  2654. b.outLen = WC_SHA384_DIGEST_SIZE;
  2655. c.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  2656. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  2657. c.output = "\x09\x33\x0c\x33\xf7\x11\x47\xe8\x3d\x19\x2f\xc7\x82\xcd\x1b"
  2658. "\x47\x53\x11\x1b\x17\x3b\x3b\x05\xd2\x2f\xa0\x80\x86\xe3\xb0"
  2659. "\xf7\x12\xfc\xc7\xc7\x1a\x55\x7e\x2d\xb9\x66\xc3\xe9\xfa\x91"
  2660. "\x74\x60\x39";
  2661. c.inLen = XSTRLEN(c.input);
  2662. c.outLen = WC_SHA384_DIGEST_SIZE;
  2663. test_sha[0] = a;
  2664. test_sha[1] = b;
  2665. test_sha[2] = c;
  2666. ret = wc_InitSha384_ex(&sha, HEAP_HINT, devId);
  2667. if (ret != 0)
  2668. return -2500;
  2669. ret = wc_InitSha384_ex(&shaCopy, HEAP_HINT, devId);
  2670. if (ret != 0) {
  2671. wc_Sha384Free(&sha);
  2672. return -2501;
  2673. }
  2674. for (i = 0; i < times; ++i) {
  2675. ret = wc_Sha384Update(&sha, (byte*)test_sha[i].input,
  2676. (word32)test_sha[i].inLen);
  2677. if (ret != 0)
  2678. ERROR_OUT(-2502 - i, exit);
  2679. ret = wc_Sha384GetHash(&sha, hashcopy);
  2680. if (ret != 0)
  2681. ERROR_OUT(-2503 - i, exit);
  2682. ret = wc_Sha384Copy(&sha, &shaCopy);
  2683. if (ret != 0)
  2684. ERROR_OUT(-2504 - i, exit);
  2685. ret = wc_Sha384Final(&sha, hash);
  2686. if (ret != 0)
  2687. ERROR_OUT(-2505 - i, exit);
  2688. wc_Sha384Free(&shaCopy);
  2689. if (XMEMCMP(hash, test_sha[i].output, WC_SHA384_DIGEST_SIZE) != 0)
  2690. ERROR_OUT(-2506 - i, exit);
  2691. if (XMEMCMP(hash, hashcopy, WC_SHA384_DIGEST_SIZE) != 0)
  2692. ERROR_OUT(-2507 - i, exit);
  2693. }
  2694. #ifndef NO_LARGE_HASH_TEST
  2695. /* BEGIN LARGE HASH TEST */ {
  2696. byte large_input[1024];
  2697. #ifdef HASH_SIZE_LIMIT
  2698. const char* large_digest =
  2699. "\xB5\xAD\x66\x6F\xD9\x58\x5E\x68\xDD\x5E\x30\xD3\x95\x72\x33\xA4"
  2700. "\xE9\x4B\x99\x3A\xEF\xF8\xE1\xBF\x1F\x05\x32\xAA\x16\x00\x82\xEC"
  2701. "\x15\xDA\xF2\x75\xEE\xE9\x06\xAF\x52\x8A\x5C\xEF\x72\x81\x80\xD6";
  2702. #else
  2703. const char* large_digest =
  2704. "\x37\x01\xdb\xff\x1e\x40\x4f\xe1\xe2\xea\x0b\x40\xbb\x3b\x39\x9a"
  2705. "\xcc\xe8\x44\x8e\x7e\xe5\x64\xb5\x6b\x7f\x56\x64\xa7\x2b\x84\xe3"
  2706. "\xc5\xd7\x79\x03\x25\x90\xf7\xa4\x58\xcb\x97\xa8\x8b\xb1\xa4\x81";
  2707. #endif
  2708. for (i = 0; i < (int)sizeof(large_input); i++) {
  2709. large_input[i] = (byte)(i & 0xFF);
  2710. }
  2711. #ifdef HASH_SIZE_LIMIT
  2712. times = 20;
  2713. #else
  2714. times = 100;
  2715. #endif
  2716. for (i = 0; i < times; ++i) {
  2717. ret = wc_Sha384Update(&sha, (byte*)large_input,
  2718. (word32)sizeof(large_input));
  2719. if (ret != 0)
  2720. ERROR_OUT(-2508, exit);
  2721. }
  2722. ret = wc_Sha384Final(&sha, hash);
  2723. if (ret != 0)
  2724. ERROR_OUT(-2509, exit);
  2725. if (XMEMCMP(hash, large_digest, WC_SHA384_DIGEST_SIZE) != 0)
  2726. ERROR_OUT(-2510, exit);
  2727. } /* END LARGE HASH TEST */
  2728. #endif /* NO_LARGE_HASH_TEST */
  2729. exit:
  2730. wc_Sha384Free(&sha);
  2731. wc_Sha384Free(&shaCopy);
  2732. return ret;
  2733. }
  2734. #endif /* WOLFSSL_SHA384 */
  2735. #ifdef WOLFSSL_SHA3
  2736. #ifndef WOLFSSL_NOSHA3_224
  2737. static int sha3_224_test(void)
  2738. {
  2739. wc_Sha3 sha;
  2740. byte hash[WC_SHA3_224_DIGEST_SIZE];
  2741. byte hashcopy[WC_SHA3_224_DIGEST_SIZE];
  2742. testVector a, b, c;
  2743. testVector test_sha[3];
  2744. int ret = 0;
  2745. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2746. a.input = "";
  2747. a.output = "\x6b\x4e\x03\x42\x36\x67\xdb\xb7\x3b\x6e\x15\x45\x4f\x0e\xb1"
  2748. "\xab\xd4\x59\x7f\x9a\x1b\x07\x8e\x3f\x5b\x5a\x6b\xc7";
  2749. a.inLen = XSTRLEN(a.input);
  2750. a.outLen = WC_SHA3_224_DIGEST_SIZE;
  2751. b.input = "abc";
  2752. b.output = "\xe6\x42\x82\x4c\x3f\x8c\xf2\x4a\xd0\x92\x34\xee\x7d\x3c\x76"
  2753. "\x6f\xc9\xa3\xa5\x16\x8d\x0c\x94\xad\x73\xb4\x6f\xdf";
  2754. b.inLen = XSTRLEN(b.input);
  2755. b.outLen = WC_SHA3_224_DIGEST_SIZE;
  2756. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  2757. c.output = "\x8a\x24\x10\x8b\x15\x4a\xda\x21\xc9\xfd\x55\x74\x49\x44\x79"
  2758. "\xba\x5c\x7e\x7a\xb7\x6e\xf2\x64\xea\xd0\xfc\xce\x33";
  2759. c.inLen = XSTRLEN(c.input);
  2760. c.outLen = WC_SHA3_224_DIGEST_SIZE;
  2761. test_sha[0] = a;
  2762. test_sha[1] = b;
  2763. test_sha[2] = c;
  2764. ret = wc_InitSha3_224(&sha, HEAP_HINT, devId);
  2765. if (ret != 0)
  2766. return -2600;
  2767. for (i = 0; i < times; ++i) {
  2768. ret = wc_Sha3_224_Update(&sha, (byte*)test_sha[i].input,
  2769. (word32)test_sha[i].inLen);
  2770. if (ret != 0)
  2771. ERROR_OUT(-2601 - i, exit);
  2772. ret = wc_Sha3_224_GetHash(&sha, hashcopy);
  2773. if (ret != 0)
  2774. ERROR_OUT(-2602 - i, exit);
  2775. ret = wc_Sha3_224_Final(&sha, hash);
  2776. if (ret != 0)
  2777. ERROR_OUT(-2603 - i, exit);
  2778. if (XMEMCMP(hash, test_sha[i].output, WC_SHA3_224_DIGEST_SIZE) != 0)
  2779. ERROR_OUT(-2604 - i, exit);
  2780. if (XMEMCMP(hash, hashcopy, WC_SHA3_224_DIGEST_SIZE) != 0)
  2781. ERROR_OUT(-2605 - i, exit);
  2782. }
  2783. #ifndef NO_LARGE_HASH_TEST
  2784. /* BEGIN LARGE HASH TEST */ {
  2785. byte large_input[1024];
  2786. const char* large_digest =
  2787. "\x13\xe5\xd3\x98\x7b\x94\xda\x41\x12\xc7\x1e\x92\x3a\x19"
  2788. "\x21\x20\x86\x6f\x24\xbf\x0a\x31\xbc\xfd\xd6\x70\x36\xf3";
  2789. for (i = 0; i < (int)sizeof(large_input); i++) {
  2790. large_input[i] = (byte)(i & 0xFF);
  2791. }
  2792. times = 100;
  2793. for (i = 0; i < times; ++i) {
  2794. ret = wc_Sha3_224_Update(&sha, (byte*)large_input,
  2795. (word32)sizeof(large_input));
  2796. if (ret != 0)
  2797. ERROR_OUT(-2606, exit);
  2798. }
  2799. ret = wc_Sha3_224_Final(&sha, hash);
  2800. if (ret != 0)
  2801. ERROR_OUT(-2607, exit);
  2802. if (XMEMCMP(hash, large_digest, WC_SHA3_224_DIGEST_SIZE) != 0)
  2803. ERROR_OUT(-2608, exit);
  2804. } /* END LARGE HASH TEST */
  2805. #endif /* NO_LARGE_HASH_TEST */
  2806. exit:
  2807. wc_Sha3_224_Free(&sha);
  2808. return ret;
  2809. }
  2810. #endif /* WOLFSSL_NOSHA3_224 */
  2811. #ifndef WOLFSSL_NOSHA3_256
  2812. static int sha3_256_test(void)
  2813. {
  2814. wc_Sha3 sha;
  2815. byte hash[WC_SHA3_256_DIGEST_SIZE];
  2816. byte hashcopy[WC_SHA3_256_DIGEST_SIZE];
  2817. testVector a, b, c;
  2818. testVector test_sha[3];
  2819. int ret = 0;
  2820. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2821. byte large_input[1024];
  2822. const char* large_digest =
  2823. "\xdc\x90\xc0\xb1\x25\xdb\x2c\x34\x81\xa3\xff\xbc\x1e\x2e\x87\xeb"
  2824. "\x6d\x70\x85\x61\xe0\xe9\x63\x61\xff\xe5\x84\x4b\x1f\x68\x05\x15";
  2825. #if defined(WOLFSSL_HASH_FLAGS) && !defined(WOLFSSL_ASYNC_CRYPT)
  2826. /* test vector with hash of empty string */
  2827. const char* Keccak256EmptyOut =
  2828. "\xc5\xd2\x46\x01\x86\xf7\x23\x3c\x92\x7e\x7d\xb2\xdc\xc7\x03\xc0"
  2829. "\xe5\x00\xb6\x53\xca\x82\x27\x3b\x7b\xfa\xd8\x04\x5d\x85\xa4\x70";
  2830. #endif
  2831. a.input = "";
  2832. a.output = "\xa7\xff\xc6\xf8\xbf\x1e\xd7\x66\x51\xc1\x47\x56\xa0\x61\xd6"
  2833. "\x62\xf5\x80\xff\x4d\xe4\x3b\x49\xfa\x82\xd8\x0a\x4b\x80\xf8"
  2834. "\x43\x4a";
  2835. a.inLen = XSTRLEN(a.input);
  2836. a.outLen = WC_SHA3_256_DIGEST_SIZE;
  2837. b.input = "abc";
  2838. b.output = "\x3a\x98\x5d\xa7\x4f\xe2\x25\xb2\x04\x5c\x17\x2d\x6b\xd3\x90"
  2839. "\xbd\x85\x5f\x08\x6e\x3e\x9d\x52\x5b\x46\xbf\xe2\x45\x11\x43"
  2840. "\x15\x32";
  2841. b.inLen = XSTRLEN(b.input);
  2842. b.outLen = WC_SHA3_256_DIGEST_SIZE;
  2843. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  2844. c.output = "\x41\xc0\xdb\xa2\xa9\xd6\x24\x08\x49\x10\x03\x76\xa8\x23\x5e"
  2845. "\x2c\x82\xe1\xb9\x99\x8a\x99\x9e\x21\xdb\x32\xdd\x97\x49\x6d"
  2846. "\x33\x76";
  2847. c.inLen = XSTRLEN(c.input);
  2848. c.outLen = WC_SHA3_256_DIGEST_SIZE;
  2849. test_sha[0] = a;
  2850. test_sha[1] = b;
  2851. test_sha[2] = c;
  2852. ret = wc_InitSha3_256(&sha, HEAP_HINT, devId);
  2853. if (ret != 0)
  2854. return -2700;
  2855. for (i = 0; i < times; ++i) {
  2856. ret = wc_Sha3_256_Update(&sha, (byte*)test_sha[i].input,
  2857. (word32)test_sha[i].inLen);
  2858. if (ret != 0)
  2859. ERROR_OUT(-2701 - i, exit);
  2860. ret = wc_Sha3_256_GetHash(&sha, hashcopy);
  2861. if (ret != 0)
  2862. ERROR_OUT(-2702 - i, exit);
  2863. ret = wc_Sha3_256_Final(&sha, hash);
  2864. if (ret != 0)
  2865. ERROR_OUT(-2703 - i, exit);
  2866. if (XMEMCMP(hash, test_sha[i].output, WC_SHA3_256_DIGEST_SIZE) != 0)
  2867. ERROR_OUT(-2704 - i, exit);
  2868. if (XMEMCMP(hash, hashcopy, WC_SHA3_256_DIGEST_SIZE) != 0)
  2869. ERROR_OUT(-2705 - i, exit);
  2870. }
  2871. #ifndef NO_LARGE_HASH_TEST
  2872. /* BEGIN LARGE HASH TEST */ {
  2873. for (i = 0; i < (int)sizeof(large_input); i++) {
  2874. large_input[i] = (byte)(i & 0xFF);
  2875. }
  2876. times = 100;
  2877. for (i = 0; i < times; ++i) {
  2878. ret = wc_Sha3_256_Update(&sha, (byte*)large_input,
  2879. (word32)sizeof(large_input));
  2880. if (ret != 0)
  2881. ERROR_OUT(-2706, exit);
  2882. }
  2883. ret = wc_Sha3_256_Final(&sha, hash);
  2884. if (ret != 0)
  2885. ERROR_OUT(-2707, exit);
  2886. if (XMEMCMP(hash, large_digest, WC_SHA3_256_DIGEST_SIZE) != 0)
  2887. ERROR_OUT(-2708, exit);
  2888. } /* END LARGE HASH TEST */
  2889. #endif /* NO_LARGE_HASH_TEST */
  2890. /* this is a software only variant of SHA3 not supported by external hardware devices */
  2891. #if defined(WOLFSSL_HASH_FLAGS) && !defined(WOLFSSL_ASYNC_CRYPT)
  2892. /* Test for Keccak256 */
  2893. ret = wc_Sha3_SetFlags(&sha, WC_HASH_SHA3_KECCAK256);
  2894. if (ret != 0) {
  2895. ERROR_OUT(-2709, exit);
  2896. }
  2897. ret = wc_Sha3_256_Update(&sha, (byte*)"", 0);
  2898. if (ret != 0) {
  2899. ERROR_OUT(-2710, exit);
  2900. }
  2901. ret = wc_Sha3_256_Final(&sha, hash);
  2902. if (ret != 0) {
  2903. ERROR_OUT(-2711, exit);
  2904. }
  2905. if (XMEMCMP(hash, Keccak256EmptyOut, WC_SHA3_256_DIGEST_SIZE) != 0) {
  2906. ERROR_OUT(-2712, exit);
  2907. }
  2908. #endif /* WOLFSSL_HASH_FLAGS && !WOLFSSL_ASYNC_CRYPT */
  2909. exit:
  2910. wc_Sha3_256_Free(&sha);
  2911. return ret;
  2912. }
  2913. #endif /* WOLFSSL_NOSHA3_256 */
  2914. #ifndef WOLFSSL_NOSHA3_384
  2915. static int sha3_384_test(void)
  2916. {
  2917. wc_Sha3 sha;
  2918. byte hash[WC_SHA3_384_DIGEST_SIZE];
  2919. byte buf[64];
  2920. #ifndef NO_INTM_HASH_TEST
  2921. byte hashcopy[WC_SHA3_384_DIGEST_SIZE];
  2922. #endif
  2923. testVector a, b, c;
  2924. testVector test_sha[3];
  2925. int ret;
  2926. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2927. a.input = "";
  2928. a.output = "\x0c\x63\xa7\x5b\x84\x5e\x4f\x7d\x01\x10\x7d\x85\x2e\x4c\x24"
  2929. "\x85\xc5\x1a\x50\xaa\xaa\x94\xfc\x61\x99\x5e\x71\xbb\xee\x98"
  2930. "\x3a\x2a\xc3\x71\x38\x31\x26\x4a\xdb\x47\xfb\x6b\xd1\xe0\x58"
  2931. "\xd5\xf0\x04";
  2932. a.inLen = XSTRLEN(a.input);
  2933. a.outLen = WC_SHA3_384_DIGEST_SIZE;
  2934. #if defined(WOLFSSL_AFALG_XILINX_SHA3) || defined(WOLFSSL_XILINX_CRYPT) && !defined(WOLFSSL_XILINX_CRYPT_VERSAL)
  2935. /* NIST test vector with a length that is a multiple of 4 */
  2936. b.input = "\x7d\x80\xb1\x60\xc4\xb5\x36\xa3\xbe\xb7\x99\x80\x59\x93\x44"
  2937. "\x04\x7c\x5f\x82\xa1\xdf\xc3\xee\xd4";
  2938. b.output = "\x04\x1c\xc5\x86\x1b\xa3\x34\x56\x3c\x61\xd4\xef\x97\x10\xd4"
  2939. "\x89\x6c\x31\x1c\x92\xed\xbe\x0d\x7c\xd5\x3e\x80\x3b\xf2\xf4"
  2940. "\xeb\x60\x57\x23\x55\x70\x77\x0c\xe8\x7c\x55\x20\xd7\xec\x14"
  2941. "\x19\x87\x22";
  2942. b.inLen = XSTRLEN(b.input);
  2943. b.outLen = WC_SHA3_384_DIGEST_SIZE;
  2944. #else
  2945. b.input = "abc";
  2946. b.output = "\xec\x01\x49\x82\x88\x51\x6f\xc9\x26\x45\x9f\x58\xe2\xc6\xad"
  2947. "\x8d\xf9\xb4\x73\xcb\x0f\xc0\x8c\x25\x96\xda\x7c\xf0\xe4\x9b"
  2948. "\xe4\xb2\x98\xd8\x8c\xea\x92\x7a\xc7\xf5\x39\xf1\xed\xf2\x28"
  2949. "\x37\x6d\x25";
  2950. b.inLen = XSTRLEN(b.input);
  2951. b.outLen = WC_SHA3_384_DIGEST_SIZE;
  2952. #endif
  2953. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  2954. c.output = "\x99\x1c\x66\x57\x55\xeb\x3a\x4b\x6b\xbd\xfb\x75\xc7\x8a\x49"
  2955. "\x2e\x8c\x56\xa2\x2c\x5c\x4d\x7e\x42\x9b\xfd\xbc\x32\xb9\xd4"
  2956. "\xad\x5a\xa0\x4a\x1f\x07\x6e\x62\xfe\xa1\x9e\xef\x51\xac\xd0"
  2957. "\x65\x7c\x22";
  2958. c.inLen = XSTRLEN(c.input);
  2959. c.outLen = WC_SHA3_384_DIGEST_SIZE;
  2960. #if defined(WOLFSSL_XILINX_CRYPT) && !defined(WOLFSSL_XILINX_CRYPT_VERSAL)
  2961. test_sha[0] = b; /* hardware acc. pre-Versal can not handle "" string */
  2962. #else
  2963. test_sha[0] = a;
  2964. #endif
  2965. test_sha[1] = b;
  2966. test_sha[2] = c;
  2967. ret = wc_InitSha3_384(&sha, HEAP_HINT, devId);
  2968. if (ret != 0)
  2969. return -2800;
  2970. for (i = 0; i < times; ++i) {
  2971. XMEMCPY(buf, test_sha[i].input, test_sha[i].inLen);
  2972. ret = wc_Sha3_384_Update(&sha, buf,
  2973. (word32)test_sha[i].inLen);
  2974. if (ret != 0)
  2975. ERROR_OUT(-2801 - (i * 10), exit);
  2976. #ifndef NO_INTM_HASH_TEST
  2977. ret = wc_Sha3_384_GetHash(&sha, hashcopy);
  2978. if (ret != 0)
  2979. ERROR_OUT(-2802 - (i * 10), exit);
  2980. #endif
  2981. ret = wc_Sha3_384_Final(&sha, hash);
  2982. if (ret != 0)
  2983. ERROR_OUT(-2803 - (i * 10), exit);
  2984. if (XMEMCMP(hash, test_sha[i].output, WC_SHA3_384_DIGEST_SIZE) != 0)
  2985. ERROR_OUT(-2804 - (i * 10), exit);
  2986. #ifndef NO_INTM_HASH_TEST
  2987. if (XMEMCMP(hash, hashcopy, WC_SHA3_384_DIGEST_SIZE) != 0)
  2988. ERROR_OUT(-2805 - (i * 10), exit);
  2989. #endif
  2990. }
  2991. #ifndef NO_LARGE_HASH_TEST
  2992. /* BEGIN LARGE HASH TEST */ {
  2993. byte large_input[1024];
  2994. const char* large_digest =
  2995. "\x30\x44\xec\x17\xef\x47\x9f\x55\x36\x11\xd6\x3f\x8a\x31\x5a\x71"
  2996. "\x8a\x71\xa7\x1d\x8e\x84\xe8\x6c\x24\x02\x2f\x7a\x08\x4e\xea\xd7"
  2997. "\x42\x36\x5d\xa8\xc2\xb7\x42\xad\xec\x19\xfb\xca\xc6\x64\xb3\xa4";
  2998. for (i = 0; i < (int)sizeof(large_input); i++) {
  2999. large_input[i] = (byte)(i & 0xFF);
  3000. }
  3001. times = 100;
  3002. for (i = 0; i < times; ++i) {
  3003. ret = wc_Sha3_384_Update(&sha, (byte*)large_input,
  3004. (word32)sizeof(large_input));
  3005. if (ret != 0)
  3006. ERROR_OUT(-2806, exit);
  3007. }
  3008. ret = wc_Sha3_384_Final(&sha, hash);
  3009. if (ret != 0)
  3010. ERROR_OUT(-2807, exit);
  3011. if (XMEMCMP(hash, large_digest, WC_SHA3_384_DIGEST_SIZE) != 0)
  3012. ERROR_OUT(-2808, exit);
  3013. } /* END LARGE HASH TEST */
  3014. #endif /* NO_LARGE_HASH_TEST */
  3015. exit:
  3016. wc_Sha3_384_Free(&sha);
  3017. return ret;
  3018. }
  3019. #endif /* WOLFSSL_NOSHA3_384 */
  3020. #ifndef WOLFSSL_NOSHA3_512
  3021. static int sha3_512_test(void)
  3022. {
  3023. wc_Sha3 sha;
  3024. byte hash[WC_SHA3_512_DIGEST_SIZE];
  3025. byte hashcopy[WC_SHA3_512_DIGEST_SIZE];
  3026. testVector a, b, c;
  3027. testVector test_sha[3];
  3028. int ret;
  3029. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  3030. a.input = "";
  3031. a.output = "\xa6\x9f\x73\xcc\xa2\x3a\x9a\xc5\xc8\xb5\x67\xdc\x18\x5a\x75"
  3032. "\x6e\x97\xc9\x82\x16\x4f\xe2\x58\x59\xe0\xd1\xdc\xc1\x47\x5c"
  3033. "\x80\xa6\x15\xb2\x12\x3a\xf1\xf5\xf9\x4c\x11\xe3\xe9\x40\x2c"
  3034. "\x3a\xc5\x58\xf5\x00\x19\x9d\x95\xb6\xd3\xe3\x01\x75\x85\x86"
  3035. "\x28\x1d\xcd\x26";
  3036. a.inLen = XSTRLEN(a.input);
  3037. a.outLen = WC_SHA3_512_DIGEST_SIZE;
  3038. b.input = "abc";
  3039. b.output = "\xb7\x51\x85\x0b\x1a\x57\x16\x8a\x56\x93\xcd\x92\x4b\x6b\x09"
  3040. "\x6e\x08\xf6\x21\x82\x74\x44\xf7\x0d\x88\x4f\x5d\x02\x40\xd2"
  3041. "\x71\x2e\x10\xe1\x16\xe9\x19\x2a\xf3\xc9\x1a\x7e\xc5\x76\x47"
  3042. "\xe3\x93\x40\x57\x34\x0b\x4c\xf4\x08\xd5\xa5\x65\x92\xf8\x27"
  3043. "\x4e\xec\x53\xf0";
  3044. b.inLen = XSTRLEN(b.input);
  3045. b.outLen = WC_SHA3_512_DIGEST_SIZE;
  3046. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  3047. c.output = "\x04\xa3\x71\xe8\x4e\xcf\xb5\xb8\xb7\x7c\xb4\x86\x10\xfc\xa8"
  3048. "\x18\x2d\xd4\x57\xce\x6f\x32\x6a\x0f\xd3\xd7\xec\x2f\x1e\x91"
  3049. "\x63\x6d\xee\x69\x1f\xbe\x0c\x98\x53\x02\xba\x1b\x0d\x8d\xc7"
  3050. "\x8c\x08\x63\x46\xb5\x33\xb4\x9c\x03\x0d\x99\xa2\x7d\xaf\x11"
  3051. "\x39\xd6\xe7\x5e";
  3052. c.inLen = XSTRLEN(c.input);
  3053. c.outLen = WC_SHA3_512_DIGEST_SIZE;
  3054. test_sha[0] = a;
  3055. test_sha[1] = b;
  3056. test_sha[2] = c;
  3057. ret = wc_InitSha3_512(&sha, HEAP_HINT, devId);
  3058. if (ret != 0)
  3059. return -2900;
  3060. for (i = 0; i < times; ++i) {
  3061. ret = wc_Sha3_512_Update(&sha, (byte*)test_sha[i].input,
  3062. (word32)test_sha[i].inLen);
  3063. if (ret != 0)
  3064. ERROR_OUT(-2901 - i, exit);
  3065. ret = wc_Sha3_512_GetHash(&sha, hashcopy);
  3066. if (ret != 0)
  3067. ERROR_OUT(-2902 - i, exit);
  3068. ret = wc_Sha3_512_Final(&sha, hash);
  3069. if (ret != 0)
  3070. ERROR_OUT(-2903 - i, exit);
  3071. if (XMEMCMP(hash, test_sha[i].output, WC_SHA3_512_DIGEST_SIZE) != 0)
  3072. ERROR_OUT(-2904 - i, exit);
  3073. if (XMEMCMP(hash, hashcopy, WC_SHA3_512_DIGEST_SIZE) != 0)
  3074. ERROR_OUT(-2905 - i, exit);
  3075. }
  3076. #ifndef NO_LARGE_HASH_TEST
  3077. /* BEGIN LARGE HASH TEST */ {
  3078. byte large_input[1024];
  3079. const char* large_digest =
  3080. "\x9c\x13\x26\xb6\x26\xb2\x94\x31\xbc\xf4\x34\xe9\x6f\xf2\xd6\x29"
  3081. "\x9a\xd0\x9b\x32\x63\x2f\x18\xa7\x5f\x23\xc9\x60\xc2\x32\x0c\xbc"
  3082. "\x57\x77\x33\xf1\x83\x81\x8a\xd3\x15\x7c\x93\xdc\x80\x9f\xed\x61"
  3083. "\x41\xa7\x5b\xfd\x32\x0e\x38\x15\xb0\x46\x3b\x7a\x4f\xfd\x44\x88";
  3084. for (i = 0; i < (int)sizeof(large_input); i++) {
  3085. large_input[i] = (byte)(i & 0xFF);
  3086. }
  3087. times = 100;
  3088. for (i = 0; i < times; ++i) {
  3089. ret = wc_Sha3_512_Update(&sha, (byte*)large_input,
  3090. (word32)sizeof(large_input));
  3091. if (ret != 0)
  3092. ERROR_OUT(-2906, exit);
  3093. }
  3094. ret = wc_Sha3_512_Final(&sha, hash);
  3095. if (ret != 0)
  3096. ERROR_OUT(-2907, exit);
  3097. if (XMEMCMP(hash, large_digest, WC_SHA3_512_DIGEST_SIZE) != 0)
  3098. ERROR_OUT(-2908, exit);
  3099. } /* END LARGE HASH TEST */
  3100. #endif /* NO_LARGE_HASH_TEST */
  3101. exit:
  3102. wc_Sha3_512_Free(&sha);
  3103. return ret;
  3104. }
  3105. #endif /* WOLFSSL_NOSHA3_512 */
  3106. WOLFSSL_TEST_SUBROUTINE int sha3_test(void)
  3107. {
  3108. int ret;
  3109. (void)ret;
  3110. #ifndef WOLFSSL_NOSHA3_224
  3111. if ((ret = sha3_224_test()) != 0)
  3112. return ret;
  3113. #endif
  3114. #ifndef WOLFSSL_NOSHA3_256
  3115. if ((ret = sha3_256_test()) != 0)
  3116. return ret;
  3117. #endif
  3118. #ifndef WOLFSSL_NOSHA3_384
  3119. if ((ret = sha3_384_test()) != 0)
  3120. return ret;
  3121. #endif
  3122. #ifndef WOLFSSL_NOSHA3_512
  3123. if ((ret = sha3_512_test()) != 0)
  3124. return ret;
  3125. #endif
  3126. return 0;
  3127. }
  3128. #endif /* WOLFSSL_SHA3 */
  3129. #ifdef WOLFSSL_SHAKE128
  3130. static int shake128_absorb_test(wc_Shake* sha)
  3131. {
  3132. byte hash[WC_SHA3_128_BLOCK_SIZE*2];
  3133. testVector a, b, c, d, e;
  3134. testVector test_sha[5];
  3135. int ret = 0;
  3136. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  3137. byte large_input[1024];
  3138. const char* large_digest =
  3139. "\x2b\xd1\x69\x9f\xb3\x75\x40\x74\xb8\xb2\xd2\x0b\x92\x47\x9b\xfe"
  3140. "\xc9\x91\x48\xbe\xda\xa4\x09\xd7\x61\x35\x18\x05\x07\x71\xa5\x61"
  3141. "\x4d\xc4\x94\xad\xbe\x04\x7d\xad\x95\x2f\xeb\x2c\xc0\x10\x67\x43"
  3142. "\x40\xf1\x4a\x58\x1c\x54\xfa\x24\x1c\x1a\x4e\x8d\x9b\xbc\xea\xa7"
  3143. "\x32\xf2\x4c\xc7\x86\x05\x36\xdc\xb4\x42\xd8\x35\xd1\xb4\xa2\x79"
  3144. "\xa2\xe6\xee\x67\x4f\xbf\x2a\x93\x41\x88\x25\x56\x29\x90\x1a\x06"
  3145. "\xba\xfe\x9f\xa6\x1a\x74\xe8\x7e\x85\x4a\xc8\x58\x60\xb1\x7b\x18"
  3146. "\xdf\x77\x59\x46\x04\xc1\xff\x4b\x9b\xcb\xad\xfe\x91\x28\xf0\x01"
  3147. "\xc1\x33\xd0\x99\x99\x2e\x0c\x86\x84\x67\x4d\x37\xa4\x42\x45\x10"
  3148. "\xdc\x8f\xdb\x6f\xa6\x9b\xee\x8a\x60\xa5\x1f\x95\x3f\x8f\xf5\x31"
  3149. "\x4b\x1d\x48\x1e\x45\xff\x79\x5c\xbe\x72\xfc\x56\xed\x6d\x1a\x99"
  3150. "\x7f\x23\x7c\xd1\xa5\x50\x9e\xb0\x4d\x61\x37\xa5\xcb\x24\x71\x3b"
  3151. "\xa3\x60\x51\x2e\x80\x83\x8b\xe0\x55\x50\xa7\x1e\xcc\x9f\xac\x41"
  3152. "\x77\x2c\x79\x22\x30\x09\x1b\x1a\x83\x5b\x2c\x48\xdc\x09\x7d\x59"
  3153. "\x0d\xf0\x54\x17\xfb\x5e\x38\x68\xde\xdb\xc5\x93\xab\x17\x5f\x4b"
  3154. "\x4d\x6d\xf2\xc7\x4e\x15\x1e\x10\x76\xc4\xcb\x87\xd8\xb7\x9d\xa8"
  3155. "\xbf\xc5\x2e\x5e\xfc\xd3\x6c\x45\xd4\x5d\x72\x0f\x66\xeb\x67\x86"
  3156. "\xfa\x6c\xd6\x80\xa4\x23\xcb\x5d\xed\x3c\xde\xdc\x5b\x3d\xca\x95"
  3157. "\x43\x4b\xdc\xe8\x49\xd3\xe1\x01\xd4\xf1\xe4\x47\xcf\x56\xba\x71"
  3158. "\xb4\x69\xed\xe7\xdb\x0f\x89\xd6\xbb\xcd\x1a\xff\xb4\xbe\x72\x26"
  3159. "\xdc\x76\x79\xb3\x1a\x4b\xe6\x8d\x9b\x8e\xd9\xe9\xe6\xf9\xff\xa5";
  3160. a.input = "";
  3161. a.output = "\x7f\x9c\x2b\xa4\xe8\x8f\x82\x7d\x61\x60\x45\x50\x76\x05\x85"
  3162. "\x3e\xd7\x3b\x80\x93\xf6\xef\xbc\x88\xeb\x1a\x6e\xac\xfa\x66"
  3163. "\xef\x26\x3c\xb1\xee\xa9\x88\x00\x4b\x93\x10\x3c\xfb\x0a\xee"
  3164. "\xfd\x2a\x68\x6e\x01\xfa\x4a\x58\xe8\xa3\x63\x9c\xa8\xa1\xe3"
  3165. "\xf9\xae\x57\xe2\x35\xb8\xcc\x87\x3c\x23\xdc\x62\xb8\xd2\x60"
  3166. "\x16\x9a\xfa\x2f\x75\xab\x91\x6a\x58\xd9\x74\x91\x88\x35\xd2"
  3167. "\x5e\x6a\x43\x50\x85\xb2\xba\xdf\xd6\xdf\xaa\xc3\x59\xa5\xef"
  3168. "\xbb\x7b\xcc\x4b\x59\xd5\x38\xdf\x9a\x04\x30\x2e\x10\xc8\xbc"
  3169. "\x1c\xbf\x1a\x0b\x3a\x51\x20\xea\x17\xcd\xa7\xcf\xad\x76\x5f"
  3170. "\x56\x23\x47\x4d\x36\x8c\xcc\xa8\xaf\x00\x07\xcd\x9f\x5e\x4c"
  3171. "\x84\x9f\x16\x7a\x58\x0b\x14\xaa\xbd\xef\xae\xe7\xee\xf4\x7c"
  3172. "\xb0\xfc\xa9";
  3173. a.inLen = XSTRLEN(a.input);
  3174. a.outLen = WC_SHA3_128_BLOCK_SIZE;
  3175. b.input = "abc";
  3176. b.output = "\x58\x81\x09\x2d\xd8\x18\xbf\x5c\xf8\xa3\xdd\xb7\x93\xfb\xcb"
  3177. "\xa7\x40\x97\xd5\xc5\x26\xa6\xd3\x5f\x97\xb8\x33\x51\x94\x0f"
  3178. "\x2c\xc8\x44\xc5\x0a\xf3\x2a\xcd\x3f\x2c\xdd\x06\x65\x68\x70"
  3179. "\x6f\x50\x9b\xc1\xbd\xde\x58\x29\x5d\xae\x3f\x89\x1a\x9a\x0f"
  3180. "\xca\x57\x83\x78\x9a\x41\xf8\x61\x12\x14\xce\x61\x23\x94\xdf"
  3181. "\x28\x6a\x62\xd1\xa2\x25\x2a\xa9\x4d\xb9\xc5\x38\x95\x6c\x71"
  3182. "\x7d\xc2\xbe\xd4\xf2\x32\xa0\x29\x4c\x85\x7c\x73\x0a\xa1\x60"
  3183. "\x67\xac\x10\x62\xf1\x20\x1f\xb0\xd3\x77\xcf\xb9\xcd\xe4\xc6"
  3184. "\x35\x99\xb2\x7f\x34\x62\xbb\xa4\xa0\xed\x29\x6c\x80\x1f\x9f"
  3185. "\xf7\xf5\x73\x02\xbb\x30\x76\xee\x14\x5f\x97\xa3\x2a\xe6\x8e"
  3186. "\x76\xab\x66\xc4\x8d\x51\x67\x5b\xd4\x9a\xcc\x29\x08\x2f\x56"
  3187. "\x47\x58\x4e";
  3188. b.inLen = XSTRLEN(b.input);
  3189. b.outLen = WC_SHA3_128_BLOCK_SIZE;
  3190. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  3191. c.output = "\x1a\x96\x18\x2b\x50\xfb\x8c\x7e\x74\xe0\xa7\x07\x78\x8f\x55"
  3192. "\xe9\x82\x09\xb8\xd9\x1f\xad\xe8\xf3\x2f\x8d\xd5\xcf\xf7\xbf"
  3193. "\x21\xf5\x4e\xe5\xf1\x95\x50\x82\x5a\x6e\x07\x00\x30\x51\x9e"
  3194. "\x94\x42\x63\xac\x1c\x67\x65\x28\x70\x65\x62\x1f\x9f\xcb\x32"
  3195. "\x01\x72\x3e\x32\x23\xb6\x3a\x46\xc2\x93\x8a\xa9\x53\xba\x84"
  3196. "\x01\xd0\xea\x77\xb8\xd2\x64\x90\x77\x55\x66\x40\x7b\x95\x67"
  3197. "\x3c\x0f\x4c\xc1\xce\x9f\xd9\x66\x14\x8d\x7e\xfd\xff\x26\xbb"
  3198. "\xf9\xf4\x8a\x21\xc6\xda\x35\xbf\xaa\x54\x56\x54\xf7\x0a\xe5"
  3199. "\x86\xff\x10\x13\x14\x20\x77\x14\x83\xec\x92\xed\xab\x40\x8c"
  3200. "\x76\x7b\xf4\xc5\xb4\xff\xfa\xa8\x0c\x8c\xa2\x14\xd8\x4c\x4d"
  3201. "\xc7\x00\xd0\xc5\x06\x30\xb2\xff\xc3\x79\x3e\xa4\xd8\x72\x58"
  3202. "\xb4\xc9\x54";
  3203. c.inLen = XSTRLEN(c.input);
  3204. c.outLen = WC_SHA3_128_BLOCK_SIZE;
  3205. /* Taken from NIST CAVP test vectors - full rate output. */
  3206. d.input = "\xdc\x88\x6d\xf3\xf6\x9c\x49\x51\x3d\xe3\x62\x7e\x94\x81\xdb"
  3207. "\x58\x71\xe8\xee\x88\xeb\x9f\x99\x61\x15\x41\x93\x0a\x8b\xc8"
  3208. "\x85\xe0";
  3209. d.output = "\x93\x68\xf0\x15\x10\x92\x44\xeb\x02\x47\xfa\x3a\x0e\x57\xf5"
  3210. "\x2e\xa7\xd9\xeb\xa2\x3d\xae\x7a\x19\x7f\x0a\x29\xe9\x22\x55"
  3211. "\x06\x05\x98\x16\xb7\x84\x48\xb6\x49\x7a\x76\xeb\x96\x2d\xb3"
  3212. "\xf8\x4d\x37\x60\xf1\xfe\xb4\xbd\xc1\xfd\x4a\xc9\x4e\x91\x7a"
  3213. "\xc2\xea\x5e\x4f\x38\x37\x4a\xa5\x6e\x4f\x47\x67\xb8\xd7\x83"
  3214. "\x1b\x2d\x51\x49\x5a\xb8\xea\xb7\xc9\x82\x20\xaf\x13\x41\x5a"
  3215. "\x59\xbb\x7c\x17\x7a\xcd\x62\x8e\xf0\xff\xe3\x6c\xeb\x18\x59"
  3216. "\x5d\x14\x4c\xbf\x25\xef\xc0\x6c\xd9\x56\xa5\x78\x20\x6e\xa8"
  3217. "\xf9\x14\x5e\xf9\xce\x19\x50\x6a\x9d\x04\x4e\xc7\x00\x79\x9f"
  3218. "\xa1\x41\x9b\xaf\x60\x52\xc0\xc1\xb4\x45\xf8\x35\x17\x57\xb0"
  3219. "\xd0\x22\x87\x21\x89\xe2\xc0\x27\x3f\x82\xd9\x69\x69\x66\x3e"
  3220. "\x55\x4d\x09";
  3221. d.inLen = 32;
  3222. d.outLen = WC_SHA3_128_BLOCK_SIZE;
  3223. /* Taken from NIST CAVP test vectors - more than one output block. */
  3224. e.input = "\x8d\x80\x01\xe2\xc0\x96\xf1\xb8\x8e\x7c\x92\x24\xa0\x86\xef"
  3225. "\xd4\x79\x7f\xbf\x74\xa8\x03\x3a\x2d\x42\x2a\x2b\x6b\x8f\x67"
  3226. "\x47\xe4";
  3227. e.output = "\xe1\x7e\xab\x0d\xa4\x04\xf9\xb6\xac\xc0\x84\x97\x2f\xc5\x79"
  3228. "\xe8\x6d\xaa\x76\x10\xa5\xe1\x7c\x23\x2f\x79\x19\x83\x96\xfd"
  3229. "\x01\xc2\x4c\x34\xbb\x54\xf4\xb0\x1e\xf7\x40\xb4\x25\x33\x4a"
  3230. "\x55\xdd\x24\x81\x3d\xc8\xea\x86\xf5\x6e\xf7\x27\x67\x26\x2b"
  3231. "\xf2\x25\x74\x8c\xcc\x3d\x9f\x48\x6f\xfb\x72\x8f\x4e\xad\x29"
  3232. "\x60\xc9\x6c\x3e\x44\x63\x86\xea\xce\x21\x9c\x84\x28\x16\x11"
  3233. "\x63\x58\xb0\xf4\x2d\x7d\xff\xf7\xdd\x24\x11\xfa\x2a\x56\x79"
  3234. "\xfd\x7a\x94\x77\x45\x75\xba\xf9\xfc\xad\x68\xa1\x9e\x30\xd1"
  3235. "\x49\xb0\x59\xb5\x9c\x44\x6c\x4e\xdc\xa5\x9b\xc5\xa4\x79\x9d"
  3236. "\xc4\x65\xaa\x9e\x78\x2c\xed\x9f\x21\xc5\x5d\xe2\x42\xdd\x25"
  3237. "\xd0\xd9\xde\x60\xd0\x9f\xf8\x6a\xba\xf3\xa0\x3a\x76\x71\xb3"
  3238. "\x05\x42\xdf\xbe\x72\xfc\x56\xed\x6d\x1a\x99\x7f\x23\x7c\xd1"
  3239. "\xa5\x50\x9e\xb0\x4d\x61\x37\xa5\xcb\x24\x71\x3b\xa3\x60\x51"
  3240. "\x2e\x80\x83\x8b\xe0\x55\x50\xa7\x1e\xcc\x9f\xac\x41\x77\x2c"
  3241. "\x79\x22\x30\x09\x1b\x1a\x83\x5b\x2c\x48\xdc\x09\x7d\x59\x0d"
  3242. "\xf0\x54\x17\xfb\x5e\x38\x68\xde\xdb\xc5\x93\xab\x17\x5f\x4b"
  3243. "\x4d\x6d\xf2\xc7\x4e\x15\x1e\x10\x76\xc4\xcb\x87\xd8\xb7\x9d"
  3244. "\xa8\xbf\xc5\x2e\x5e\xfc\xd3\x6c\x45\xd4\x5d\x72\x0f\x66\xeb"
  3245. "\x67\x86\xfa\x6c\xd6\x80\xa4\x23\xcb\x5d\xed\x3c\xde\xdc\x5b"
  3246. "\x3d\xca\x95\x43\x4b\xdc\xe8\x49\xd3\xe1\x01\xd4\xf1\xe4\x47"
  3247. "\xcf\x56\xba\x71\xb4\x69\xed\xe7\xdb\x0f\x89\xd6\xbb\xcd\x1a"
  3248. "\xff\xb4\xbe\x72\x26\xdc\x76\x79\xb3\x1a\x4b\xe6\x8d\x9b\x8e"
  3249. "\xd9\xe9\xe6\xf9\xff\xa5";
  3250. e.inLen = 32;
  3251. e.outLen = 2 * WC_SHA3_128_BLOCK_SIZE;
  3252. test_sha[0] = a;
  3253. test_sha[1] = b;
  3254. test_sha[2] = c;
  3255. test_sha[3] = d;
  3256. test_sha[4] = e;
  3257. for (i = 0; i < times; ++i) {
  3258. ret = wc_InitShake128(sha, HEAP_HINT, devId);
  3259. if (ret != 0)
  3260. ERROR_OUT(-3100 - i, exit);
  3261. ret = wc_Shake128_Absorb(sha, (byte*)test_sha[i].input,
  3262. (word32)test_sha[i].inLen);
  3263. if (ret != 0)
  3264. ERROR_OUT(-3101 - i, exit);
  3265. ret = wc_Shake128_SqueezeBlocks(sha, hash,
  3266. (word32)test_sha[i].outLen / WC_SHA3_128_BLOCK_SIZE);
  3267. if (ret != 0)
  3268. ERROR_OUT(-3102 - i, exit);
  3269. if (XMEMCMP(hash, test_sha[i].output, (word32)test_sha[i].outLen) != 0)
  3270. ERROR_OUT(-3103 - i, exit);
  3271. }
  3272. #ifndef NO_LARGE_HASH_TEST
  3273. /* BEGIN LARGE HASH TEST */ {
  3274. for (i = 0; i < (int)sizeof(large_input); i++) {
  3275. large_input[i] = (byte)(i & 0xFF);
  3276. }
  3277. ret = wc_InitShake128(sha, HEAP_HINT, devId);
  3278. if (ret != 0)
  3279. ERROR_OUT(-3104, exit);
  3280. /* Absorb is non-incremental. */
  3281. ret = wc_Shake128_Absorb(sha, (byte*)large_input,
  3282. (word32)sizeof(large_input));
  3283. if (ret != 0)
  3284. ERROR_OUT(-3105, exit);
  3285. /* Able to squeeze out blocks incrementally. */
  3286. ret = wc_Shake128_SqueezeBlocks(sha, hash, 1);
  3287. if (ret != 0)
  3288. ERROR_OUT(-3106, exit);
  3289. ret = wc_Shake128_SqueezeBlocks(sha, hash,
  3290. ((word32)sizeof(hash) / WC_SHA3_128_BLOCK_SIZE) - 1);
  3291. if (ret != 0)
  3292. ERROR_OUT(-3106, exit);
  3293. if (XMEMCMP(hash, large_digest, sizeof(hash)) != 0)
  3294. ERROR_OUT(-3107, exit);
  3295. } /* END LARGE HASH TEST */
  3296. #endif /* NO_LARGE_HASH_TEST */
  3297. exit:
  3298. return ret;
  3299. }
  3300. WOLFSSL_TEST_SUBROUTINE int shake128_test(void)
  3301. {
  3302. wc_Shake sha;
  3303. byte hash[250];
  3304. testVector a, b, c, d, e;
  3305. testVector test_sha[5];
  3306. int ret = 0;
  3307. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  3308. byte large_input[1024];
  3309. const char* large_digest =
  3310. "\x88\xd7\x0e\x86\x46\x72\x6b\x3d\x7d\x22\xe1\xa9\x2d\x02\xdb\x35"
  3311. "\x92\x4f\x1b\x03\x90\xee\xa3\xce\xd1\x3a\x08\x3a\xd7\x4e\x10\xdf"
  3312. "\x09\x67\x33\x35\x4f\xdd\x38\x50\x5b\xcb\x75\xc7\xba\x65\xe5\xe8"
  3313. "\xb8\x76\xde\xc5\xee\xd7\xf1\x65\x93\x4e\x5e\xc4\xb1\xd7\x6b\xee"
  3314. "\x4b\x57\x48\xf5\x38\x49\x9e\x45\xa0\xf7\x32\xe9\x05\x26\x6a\x10"
  3315. "\x70\xd4\x7c\x19\x01\x1f\x6d\x37\xba\x7b\x74\xc2\xbc\xb6\xbc\x74"
  3316. "\xa3\x66\x6c\x9b\x11\x84\x9d\x4a\x36\xbc\x8a\x0d\x4c\xe3\x39\xfa"
  3317. "\xfa\x1b";
  3318. a.input = "";
  3319. a.output = "\x7f\x9c\x2b\xa4\xe8\x8f\x82\x7d\x61\x60\x45\x50\x76\x05\x85"
  3320. "\x3e\xd7\x3b\x80\x93\xf6\xef\xbc\x88\xeb\x1a\x6e\xac\xfa\x66"
  3321. "\xef\x26\x3c\xb1\xee\xa9\x88\x00\x4b\x93\x10\x3c\xfb\x0a\xee"
  3322. "\xfd\x2a\x68\x6e\x01\xfa\x4a\x58\xe8\xa3\x63\x9c\xa8\xa1\xe3"
  3323. "\xf9\xae\x57\xe2\x35\xb8\xcc\x87\x3c\x23\xdc\x62\xb8\xd2\x60"
  3324. "\x16\x9a\xfa\x2f\x75\xab\x91\x6a\x58\xd9\x74\x91\x88\x35\xd2"
  3325. "\x5e\x6a\x43\x50\x85\xb2\xba\xdf\xd6\xdf\xaa\xc3\x59\xa5\xef"
  3326. "\xbb\x7b\xcc\x4b\x59\xd5\x38\xdf\x9a";
  3327. a.inLen = XSTRLEN(a.input);
  3328. a.outLen = 114;
  3329. b.input = "abc";
  3330. b.output = "\x58\x81\x09\x2d\xd8\x18\xbf\x5c\xf8\xa3\xdd\xb7\x93\xfb\xcb"
  3331. "\xa7\x40\x97\xd5\xc5\x26\xa6\xd3\x5f\x97\xb8\x33\x51\x94\x0f"
  3332. "\x2c\xc8\x44\xc5\x0a\xf3\x2a\xcd\x3f\x2c\xdd\x06\x65\x68\x70"
  3333. "\x6f\x50\x9b\xc1\xbd\xde\x58\x29\x5d\xae\x3f\x89\x1a\x9a\x0f"
  3334. "\xca\x57\x83\x78\x9a\x41\xf8\x61\x12\x14\xce\x61\x23\x94\xdf"
  3335. "\x28\x6a\x62\xd1\xa2\x25\x2a\xa9\x4d\xb9\xc5\x38\x95\x6c\x71"
  3336. "\x7d\xc2\xbe\xd4\xf2\x32\xa0\x29\x4c\x85\x7c\x73\x0a\xa1\x60"
  3337. "\x67\xac\x10\x62\xf1\x20\x1f\xb0\xd3";
  3338. b.inLen = XSTRLEN(b.input);
  3339. b.outLen = 114;
  3340. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  3341. c.output = "\x1a\x96\x18\x2b\x50\xfb\x8c\x7e\x74\xe0\xa7\x07\x78\x8f\x55"
  3342. "\xe9\x82\x09\xb8\xd9\x1f\xad\xe8\xf3\x2f\x8d\xd5\xcf\xf7\xbf"
  3343. "\x21\xf5\x4e\xe5\xf1\x95\x50\x82\x5a\x6e\x07\x00\x30\x51\x9e"
  3344. "\x94\x42\x63\xac\x1c\x67\x65\x28\x70\x65\x62\x1f\x9f\xcb\x32"
  3345. "\x01\x72\x3e\x32\x23\xb6\x3a\x46\xc2\x93\x8a\xa9\x53\xba\x84"
  3346. "\x01\xd0\xea\x77\xb8\xd2\x64\x90\x77\x55\x66\x40\x7b\x95\x67"
  3347. "\x3c\x0f\x4c\xc1\xce\x9f\xd9\x66\x14\x8d\x7e\xfd\xff\x26\xbb"
  3348. "\xf9\xf4\x8a\x21\xc6\xda\x35\xbf\xaa";
  3349. c.inLen = XSTRLEN(c.input);
  3350. c.outLen = 114;
  3351. /* Taken from NIST CAVP test vectors - full rate output. */
  3352. d.input = "\xdc\x88\x6d\xf3\xf6\x9c\x49\x51\x3d\xe3\x62\x7e\x94\x81\xdb"
  3353. "\x58\x71\xe8\xee\x88\xeb\x9f\x99\x61\x15\x41\x93\x0a\x8b\xc8"
  3354. "\x85\xe0";
  3355. d.output = "\x93\x68\xf0\x15\x10\x92\x44\xeb\x02\x47\xfa\x3a\x0e\x57\xf5"
  3356. "\x2e\xa7\xd9\xeb\xa2\x3d\xae\x7a\x19\x7f\x0a\x29\xe9\x22\x55"
  3357. "\x06\x05\x98\x16\xb7\x84\x48\xb6\x49\x7a\x76\xeb\x96\x2d\xb3"
  3358. "\xf8\x4d\x37\x60\xf1\xfe\xb4\xbd\xc1\xfd\x4a\xc9\x4e\x91\x7a"
  3359. "\xc2\xea\x5e\x4f\x38\x37\x4a\xa5\x6e\x4f\x47\x67\xb8\xd7\x83"
  3360. "\x1b\x2d\x51\x49\x5a\xb8\xea\xb7\xc9\x82\x20\xaf\x13\x41\x5a"
  3361. "\x59\xbb\x7c\x17\x7a\xcd\x62\x8e\xf0\xff\xe3\x6c\xeb\x18\x59"
  3362. "\x5d\x14\x4c\xbf\x25\xef\xc0\x6c\xd9\x56\xa5\x78\x20\x6e\xa8"
  3363. "\xf9\x14\x5e\xf9\xce\x19\x50\x6a\x9d\x04\x4e\xc7\x00\x79\x9f"
  3364. "\xa1";
  3365. d.inLen = 32;
  3366. d.outLen = 136;
  3367. /* Taken from NIST CAVP test vectors - more than one output block. */
  3368. e.input = "\x8d\x80\x01\xe2\xc0\x96\xf1\xb8\x8e\x7c\x92\x24\xa0\x86\xef"
  3369. "\xd4\x79\x7f\xbf\x74\xa8\x03\x3a\x2d\x42\x2a\x2b\x6b\x8f\x67"
  3370. "\x47\xe4";
  3371. e.output = "\xe1\x7e\xab\x0d\xa4\x04\xf9\xb6\xac\xc0\x84\x97\x2f\xc5\x79"
  3372. "\xe8\x6d\xaa\x76\x10\xa5\xe1\x7c\x23\x2f\x79\x19\x83\x96\xfd"
  3373. "\x01\xc2\x4c\x34\xbb\x54\xf4\xb0\x1e\xf7\x40\xb4\x25\x33\x4a"
  3374. "\x55\xdd\x24\x81\x3d\xc8\xea\x86\xf5\x6e\xf7\x27\x67\x26\x2b"
  3375. "\xf2\x25\x74\x8c\xcc\x3d\x9f\x48\x6f\xfb\x72\x8f\x4e\xad\x29"
  3376. "\x60\xc9\x6c\x3e\x44\x63\x86\xea\xce\x21\x9c\x84\x28\x16\x11"
  3377. "\x63\x58\xb0\xf4\x2d\x7d\xff\xf7\xdd\x24\x11\xfa\x2a\x56\x79"
  3378. "\xfd\x7a\x94\x77\x45\x75\xba\xf9\xfc\xad\x68\xa1\x9e\x30\xd1"
  3379. "\x49\xb0\x59\xb5\x9c\x44\x6c\x4e\xdc\xa5\x9b\xc5\xa4\x79\x9d"
  3380. "\xc4\x65\xaa\x9e\x78\x2c\xed\x9f\x21\xc5\x5d\xe2\x42\xdd\x25"
  3381. "\xd0\xd9\xde\x60\xd0\x9f\xf8\x6a\xba\xf3\xa0\x3a\x76\x71\xb3"
  3382. "\x05\x42\xdf\xbe\x72\xfc\x56\xed\x6d\x1a\x99\x7f\x23\x7c\xd1"
  3383. "\xa5\x50\x9e\xb0\x4d\x61\x37\xa5\xcb\x24\x71\x3b\xa3\x60\x51"
  3384. "\x2e\x80\x83\x8b\xe0\x55\x50\xa7\x1e\xcc\x9f\xac\x41\x77\x2c"
  3385. "\x79\x22\x30\x09\x1b\x1a\x83\x5b\x2c\x48\xdc\x09\x7d\x59\x0d"
  3386. "\xf0\x54\x17\xfb\x5e\x38\x68\xde\xdb\xc5\x93\xab\x17\x5f\x4b"
  3387. "\x4d\x6d\xf2\xc7\x4e\x15\x1e\x10\x76\xc4";
  3388. e.inLen = 32;
  3389. e.outLen = 250;
  3390. test_sha[0] = a;
  3391. test_sha[1] = b;
  3392. test_sha[2] = c;
  3393. test_sha[3] = d;
  3394. test_sha[4] = e;
  3395. ret = wc_InitShake128(&sha, HEAP_HINT, devId);
  3396. if (ret != 0)
  3397. return -3100;
  3398. for (i = 0; i < times; ++i) {
  3399. ret = wc_Shake128_Update(&sha, (byte*)test_sha[i].input,
  3400. (word32)test_sha[i].inLen);
  3401. if (ret != 0)
  3402. ERROR_OUT(-3101 - i, exit);
  3403. ret = wc_Shake128_Final(&sha, hash, (word32)test_sha[i].outLen);
  3404. if (ret != 0)
  3405. ERROR_OUT(-3102 - i, exit);
  3406. if (XMEMCMP(hash, test_sha[i].output, test_sha[i].outLen) != 0)
  3407. ERROR_OUT(-3103 - i, exit);
  3408. }
  3409. #ifndef NO_LARGE_HASH_TEST
  3410. /* BEGIN LARGE HASH TEST */ {
  3411. for (i = 0; i < (int)sizeof(large_input); i++) {
  3412. large_input[i] = (byte)(i & 0xFF);
  3413. }
  3414. times = 100;
  3415. for (i = 0; i < times; ++i) {
  3416. ret = wc_Shake128_Update(&sha, (byte*)large_input,
  3417. (word32)sizeof(large_input));
  3418. if (ret != 0)
  3419. ERROR_OUT(-3104, exit);
  3420. }
  3421. ret = wc_Shake128_Final(&sha, hash, (word32)sizeof(hash));
  3422. if (ret != 0)
  3423. ERROR_OUT(-3105, exit);
  3424. if (XMEMCMP(hash, large_digest, 114) != 0)
  3425. ERROR_OUT(-3106, exit);
  3426. } /* END LARGE HASH TEST */
  3427. #endif /* NO_LARGE_HASH_TEST */
  3428. ret = shake128_absorb_test(&sha);
  3429. exit:
  3430. wc_Shake128_Free(&sha);
  3431. return ret;
  3432. }
  3433. #endif
  3434. #ifdef WOLFSSL_SHAKE256
  3435. static int shake256_absorb_test(wc_Shake* sha, byte *large_input_buf,
  3436. size_t large_input_buf_size)
  3437. {
  3438. byte hash[WC_SHA3_256_BLOCK_SIZE*2];
  3439. testVector a, b, c, d, e;
  3440. testVector test_sha[5];
  3441. int ret = 0;
  3442. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  3443. const char* large_digest =
  3444. "\x21\x25\x8e\xae\x6e\x4f\xa7\xe1\xb9\x6d\xa7\xc9\x7d\x46\x03\x69"
  3445. "\x29\x0d\x81\x49\xba\x5d\xaf\x37\xfd\xeb\x25\x52\x1d\xd9\xbd\x65"
  3446. "\xfa\x99\xb9\xd1\x70\x6b\xeb\xd4\xc1\x2c\xea\x24\x20\x27\xa7\xcd"
  3447. "\xfa\xe1\x81\xd9\xd5\xc1\x1c\xc7\xe9\x70\xc3\xc7\x21\x6f\x32\x22"
  3448. "\xe3\x27\xdb\x58\x5e\xea\x18\x2d\x63\x4d\x14\x6c\x94\xcf\x2b\x7e"
  3449. "\x6e\x2a\x74\xf3\xe0\xac\xb3\xb2\xcc\xef\x38\xe9\xe7\x35\xb3\xc5"
  3450. "\x77\x9d\xff\xe3\x08\x8e\xf8\x2c\x89\xbb\x45\x22\x16\x99\x91\xc0"
  3451. "\xe7\x71\x57\x75\xc5\xb1\xc6\xaf\x27\xcb\x64\x8c\xc4\xee\x3d\x5f"
  3452. "\x4c\x35\xfb\x1c\xf3\xf8\x0e\xfd\x5e\xfc\x07\xd8\x4d\x55\x32\x49"
  3453. "\x45\x0d\xab\x4a\x49\xc4\x83\xde\xd2\x50\xc9\x33\x8f\x85\xcd\x93"
  3454. "\x7a\xe6\x6b\xb4\x36\xf3\xb4\x02\x6e\x85\x9f\xda\x1c\xa5\x71\x43"
  3455. "\x2f\x3b\xfc\x09\xe7\xc0\x3c\xa4\xd1\x83\xb7\x41\x11\x1c\xa0\x48"
  3456. "\x3d\x0e\xda\xbc\x03\xfe\xb2\x3b\x17\xee\x48\xe8\x44\xba\x24\x08"
  3457. "\xd9\xdc\xfd\x01\x39\xd2\xe8\xc7\x31\x01\x25\xae\xe8\x01\xc6\x1a"
  3458. "\xb7\x90\x0d\x1e\xfc\x47\xc0\x78\x28\x17\x66\xf3\x61\xc5\xe6\x11"
  3459. "\x13\x46\x23\x5e\x1d\xc3\x83\x25\x66\x6c\x68\x1b\x30\xdd\xc4\xe6"
  3460. "\x83\x8b\x0f\x23\x58\x7e\x06\x5f\x4a\x2b\xed\xc9\x6c\x97\x68\x44";
  3461. a.input = "";
  3462. a.output = "\x46\xb9\xdd\x2b\x0b\xa8\x8d\x13\x23\x3b\x3f\xeb\x74\x3e\xeb"
  3463. "\x24\x3f\xcd\x52\xea\x62\xb8\x1b\x82\xb5\x0c\x27\x64\x6e\xd5"
  3464. "\x76\x2f\xd7\x5d\xc4\xdd\xd8\xc0\xf2\x00\xcb\x05\x01\x9d\x67"
  3465. "\xb5\x92\xf6\xfc\x82\x1c\x49\x47\x9a\xb4\x86\x40\x29\x2e\xac"
  3466. "\xb3\xb7\xc4\xbe\x14\x1e\x96\x61\x6f\xb1\x39\x57\x69\x2c\xc7"
  3467. "\xed\xd0\xb4\x5a\xe3\xdc\x07\x22\x3c\x8e\x92\x93\x7b\xef\x84"
  3468. "\xbc\x0e\xab\x86\x28\x53\x34\x9e\xc7\x55\x46\xf5\x8f\xb7\xc2"
  3469. "\x77\x5c\x38\x46\x2c\x50\x10\xd8\x46\xc1\x85\xc1\x51\x11\xe5"
  3470. "\x95\x52\x2a\x6b\xcd\x16\xcf\x86\xf3\xd1\x22\x10\x9e\x3b\x1f"
  3471. "\xdd";
  3472. a.inLen = XSTRLEN(a.input);
  3473. a.outLen = WC_SHA3_256_BLOCK_SIZE;
  3474. b.input = "abc";
  3475. b.output = "\x48\x33\x66\x60\x13\x60\xa8\x77\x1c\x68\x63\x08\x0c\xc4\x11"
  3476. "\x4d\x8d\xb4\x45\x30\xf8\xf1\xe1\xee\x4f\x94\xea\x37\xe7\x8b"
  3477. "\x57\x39\xd5\xa1\x5b\xef\x18\x6a\x53\x86\xc7\x57\x44\xc0\x52"
  3478. "\x7e\x1f\xaa\x9f\x87\x26\xe4\x62\xa1\x2a\x4f\xeb\x06\xbd\x88"
  3479. "\x01\xe7\x51\xe4\x13\x85\x14\x12\x04\xf3\x29\x97\x9f\xd3\x04"
  3480. "\x7a\x13\xc5\x65\x77\x24\xad\xa6\x4d\x24\x70\x15\x7b\x3c\xdc"
  3481. "\x28\x86\x20\x94\x4d\x78\xdb\xcd\xdb\xd9\x12\x99\x3f\x09\x13"
  3482. "\xf1\x64\xfb\x2c\xe9\x51\x31\xa2\xd0\x9a\x3e\x6d\x51\xcb\xfc"
  3483. "\x62\x27\x20\xd7\xa7\x5c\x63\x34\xe8\xa2\xd7\xec\x71\xa7\xcc"
  3484. "\x29";
  3485. b.inLen = XSTRLEN(b.input);
  3486. b.outLen = WC_SHA3_256_BLOCK_SIZE;
  3487. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  3488. c.output = "\x4d\x8c\x2d\xd2\x43\x5a\x01\x28\xee\xfb\xb8\xc3\x6f\x6f\x87"
  3489. "\x13\x3a\x79\x11\xe1\x8d\x97\x9e\xe1\xae\x6b\xe5\xd4\xfd\x2e"
  3490. "\x33\x29\x40\xd8\x68\x8a\x4e\x6a\x59\xaa\x80\x60\xf1\xf9\xbc"
  3491. "\x99\x6c\x05\xac\xa3\xc6\x96\xa8\xb6\x62\x79\xdc\x67\x2c\x74"
  3492. "\x0b\xb2\x24\xec\x37\xa9\x2b\x65\xdb\x05\x39\xc0\x20\x34\x55"
  3493. "\xf5\x1d\x97\xcc\xe4\xcf\xc4\x91\x27\xd7\x26\x0a\xfc\x67\x3a"
  3494. "\xf2\x08\xba\xf1\x9b\xe2\x12\x33\xf3\xde\xbe\x78\xd0\x67\x60"
  3495. "\xcf\xa5\x51\xee\x1e\x07\x91\x41\xd4\x9d\xd3\xef\x7e\x18\x2b"
  3496. "\x15\x24\xdf\x82\xea\x1c\xef\xe1\xc6\xc3\x96\x61\x75\xf0\x22"
  3497. "\x8d";
  3498. c.inLen = XSTRLEN(c.input);
  3499. c.outLen = WC_SHA3_256_BLOCK_SIZE;
  3500. /* Taken from NIST CAVP test vectors - full rate output. */
  3501. d.input = "\xdc\x88\x6d\xf3\xf6\x9c\x49\x51\x3d\xe3\x62\x7e\x94\x81\xdb"
  3502. "\x58\x71\xe8\xee\x88\xeb\x9f\x99\x61\x15\x41\x93\x0a\x8b\xc8"
  3503. "\x85\xe0";
  3504. d.output = "\x00\x64\x8a\xfb\xc5\xe6\x51\x64\x9d\xb1\xfd\x82\x93\x6b\x00"
  3505. "\xdb\xbc\x12\x2f\xb4\xc8\x77\x86\x0d\x38\x5c\x49\x50\xd5\x6d"
  3506. "\xe7\xe0\x96\xd6\x13\xd7\xa3\xf2\x7e\xd8\xf2\x63\x34\xb0\xcc"
  3507. "\xc1\x40\x7b\x41\xdc\xcb\x23\xdf\xaa\x52\x98\x18\xd1\x12\x5c"
  3508. "\xd5\x34\x80\x92\x52\x43\x66\xb8\x5f\xab\xb9\x7c\x6c\xd1\xe6"
  3509. "\x06\x6f\x45\x9b\xcc\x56\x6d\xa8\x7e\xc9\xb7\xba\x36\x79\x2d"
  3510. "\x11\x8a\xc3\x9a\x4c\xce\xf6\x19\x2b\xbf\x3a\x54\xaf\x18\xe5"
  3511. "\x7b\x0c\x14\x61\x01\xf6\xae\xaa\x82\x2b\xc4\xb4\xc9\x70\x8b"
  3512. "\x09\xf0\xb3\xba\xb4\x1b\xcc\xe9\x64\xd9\x99\xd1\x10\x7b\xd7"
  3513. "\xc2";
  3514. d.inLen = 32;
  3515. d.outLen = WC_SHA3_256_BLOCK_SIZE;
  3516. /* Taken from NIST CAVP test vectors - more than one output block. */
  3517. e.input = "\x8d\x80\x01\xe2\xc0\x96\xf1\xb8\x8e\x7c\x92\x24\xa0\x86\xef"
  3518. "\xd4\x79\x7f\xbf\x74\xa8\x03\x3a\x2d\x42\x2a\x2b\x6b\x8f\x67"
  3519. "\x47\xe4";
  3520. e.output = "\x2e\x97\x5f\x6a\x8a\x14\xf0\x70\x4d\x51\xb1\x36\x67\xd8\x19"
  3521. "\x5c\x21\x9f\x71\xe6\x34\x56\x96\xc4\x9f\xa4\xb9\xd0\x8e\x92"
  3522. "\x25\xd3\xd3\x93\x93\x42\x51\x52\xc9\x7e\x71\xdd\x24\x60\x1c"
  3523. "\x11\xab\xcf\xa0\xf1\x2f\x53\xc6\x80\xbd\x3a\xe7\x57\xb8\x13"
  3524. "\x4a\x9c\x10\xd4\x29\x61\x58\x69\x21\x7f\xdd\x58\x85\xc4\xdb"
  3525. "\x17\x49\x85\x70\x3a\x6d\x6d\xe9\x4a\x66\x7e\xac\x30\x23\x44"
  3526. "\x3a\x83\x37\xae\x1b\xc6\x01\xb7\x6d\x7d\x38\xec\x3c\x34\x46"
  3527. "\x31\x05\xf0\xd3\x94\x9d\x78\xe5\x62\xa0\x39\xe4\x46\x95\x48"
  3528. "\xb6\x09\x39\x5d\xe5\xa4\xfd\x43\xc4\x6c\xa9\xfd\x6e\xe2\x9a"
  3529. "\xda\x5e\xfc\x07\xd8\x4d\x55\x32\x49\x45\x0d\xab\x4a\x49\xc4"
  3530. "\x83\xde\xd2\x50\xc9\x33\x8f\x85\xcd\x93\x7a\xe6\x6b\xb4\x36"
  3531. "\xf3\xb4\x02\x6e\x85\x9f\xda\x1c\xa5\x71\x43\x2f\x3b\xfc\x09"
  3532. "\xe7\xc0\x3c\xa4\xd1\x83\xb7\x41\x11\x1c\xa0\x48\x3d\x0e\xda"
  3533. "\xbc\x03\xfe\xb2\x3b\x17\xee\x48\xe8\x44\xba\x24\x08\xd9\xdc"
  3534. "\xfd\x01\x39\xd2\xe8\xc7\x31\x01\x25\xae\xe8\x01\xc6\x1a\xb7"
  3535. "\x90\x0d\x1e\xfc\x47\xc0\x78\x28\x17\x66\xf3\x61\xc5\xe6\x11"
  3536. "\x13\x46\x23\x5e\x1d\xc3\x83\x25\x66\x6c\x68\x1b\x30\xdd\xc4"
  3537. "\xe6\x83\x8b\x0f\x23\x58\x7e\x06\x5f\x4a\x2b\xed\xc9\x6c\x97"
  3538. "\x68\x44";
  3539. e.inLen = 32;
  3540. e.outLen = 2 * WC_SHA3_256_BLOCK_SIZE;
  3541. test_sha[0] = a;
  3542. test_sha[1] = b;
  3543. test_sha[2] = c;
  3544. test_sha[3] = d;
  3545. test_sha[4] = e;
  3546. for (i = 0; i < times; ++i) {
  3547. ret = wc_InitShake256(sha, HEAP_HINT, devId);
  3548. if (ret != 0)
  3549. ERROR_OUT(-3100 - i, exit);
  3550. ret = wc_Shake256_Absorb(sha, (byte*)test_sha[i].input,
  3551. (word32)test_sha[i].inLen);
  3552. if (ret != 0)
  3553. ERROR_OUT(-3101 - i, exit);
  3554. ret = wc_Shake256_SqueezeBlocks(sha, hash,
  3555. (word32)test_sha[i].outLen / WC_SHA3_256_BLOCK_SIZE);
  3556. if (ret != 0)
  3557. ERROR_OUT(-3102 - i, exit);
  3558. if (XMEMCMP(hash, test_sha[i].output, (word32)test_sha[i].outLen) != 0)
  3559. ERROR_OUT(-3103 - i, exit);
  3560. }
  3561. #ifndef NO_LARGE_HASH_TEST
  3562. /* BEGIN LARGE HASH TEST */ {
  3563. for (i = 0; i < (int)large_input_buf_size; i++) {
  3564. large_input_buf[i] = (byte)(i & 0xFF);
  3565. }
  3566. ret = wc_InitShake256(sha, HEAP_HINT, devId);
  3567. if (ret != 0)
  3568. ERROR_OUT(-3104, exit);
  3569. /* Absorb is non-incremental. */
  3570. ret = wc_Shake256_Absorb(sha, large_input_buf,
  3571. (word32)large_input_buf_size);
  3572. if (ret != 0)
  3573. ERROR_OUT(-3105, exit);
  3574. /* Able to squeeze out blocks incrementally. */
  3575. ret = wc_Shake256_SqueezeBlocks(sha, hash, 1);
  3576. if (ret != 0)
  3577. ERROR_OUT(-3106, exit);
  3578. ret = wc_Shake256_SqueezeBlocks(sha, hash,
  3579. ((word32)sizeof(hash) / WC_SHA3_256_BLOCK_SIZE) - 1);
  3580. if (ret != 0)
  3581. ERROR_OUT(-3106, exit);
  3582. if (XMEMCMP(hash, large_digest, sizeof(hash)) != 0)
  3583. ERROR_OUT(-3107, exit);
  3584. } /* END LARGE HASH TEST */
  3585. #endif /* NO_LARGE_HASH_TEST */
  3586. exit:
  3587. return ret;
  3588. }
  3589. WOLFSSL_TEST_SUBROUTINE int shake256_test(void)
  3590. {
  3591. wc_Shake sha;
  3592. byte hash[250];
  3593. testVector a, b, c, d, e;
  3594. testVector test_sha[5];
  3595. int ret = 0;
  3596. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  3597. #define SHAKE256_LARGE_INPUT_BUFSIZ 1024
  3598. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  3599. byte *large_input = NULL;
  3600. #else
  3601. byte large_input[SHAKE256_LARGE_INPUT_BUFSIZ];
  3602. #endif
  3603. const char* large_digest =
  3604. "\x90\x32\x4a\xcc\xd1\xdf\xb8\x0b\x79\x1f\xb8\xc8\x5b\x54\xc8\xe7"
  3605. "\x45\xf5\x60\x6b\x38\x26\xb2\x0a\xee\x38\x01\xf3\xd9\xfa\x96\x9f"
  3606. "\x6a\xd7\x15\xdf\xb6\xc2\xf4\x20\x33\x44\x55\xe8\x2a\x09\x2b\x68"
  3607. "\x2e\x18\x65\x5e\x65\x93\x28\xbc\xb1\x9e\xe2\xb1\x92\xea\x98\xac"
  3608. "\x21\xef\x4c\xe1\xb4\xb7\xbe\x81\x5c\x1d\xd3\xb7\x17\xe5\xbb\xc5"
  3609. "\x8c\x68\xb7\xfb\xac\x55\x8a\x9b\x4d\x91\xe4\x9f\x72\xbb\x6e\x38"
  3610. "\xaf\x21\x7d\x21\xaa\x98\x4e\x75\xc4\xb4\x1c\x7c\x50\x45\x54\xf9"
  3611. "\xea\x26";
  3612. a.input = "";
  3613. a.output = "\x46\xb9\xdd\x2b\x0b\xa8\x8d\x13\x23\x3b\x3f\xeb\x74\x3e\xeb"
  3614. "\x24\x3f\xcd\x52\xea\x62\xb8\x1b\x82\xb5\x0c\x27\x64\x6e\xd5"
  3615. "\x76\x2f\xd7\x5d\xc4\xdd\xd8\xc0\xf2\x00\xcb\x05\x01\x9d\x67"
  3616. "\xb5\x92\xf6\xfc\x82\x1c\x49\x47\x9a\xb4\x86\x40\x29\x2e\xac"
  3617. "\xb3\xb7\xc4\xbe\x14\x1e\x96\x61\x6f\xb1\x39\x57\x69\x2c\xc7"
  3618. "\xed\xd0\xb4\x5a\xe3\xdc\x07\x22\x3c\x8e\x92\x93\x7b\xef\x84"
  3619. "\xbc\x0e\xab\x86\x28\x53\x34\x9e\xc7\x55\x46\xf5\x8f\xb7\xc2"
  3620. "\x77\x5c\x38\x46\x2c\x50\x10\xd8\x46";
  3621. a.inLen = XSTRLEN(a.input);
  3622. a.outLen = 114;
  3623. b.input = "abc";
  3624. b.output = "\x48\x33\x66\x60\x13\x60\xa8\x77\x1c\x68\x63\x08\x0c\xc4\x11"
  3625. "\x4d\x8d\xb4\x45\x30\xf8\xf1\xe1\xee\x4f\x94\xea\x37\xe7\x8b"
  3626. "\x57\x39\xd5\xa1\x5b\xef\x18\x6a\x53\x86\xc7\x57\x44\xc0\x52"
  3627. "\x7e\x1f\xaa\x9f\x87\x26\xe4\x62\xa1\x2a\x4f\xeb\x06\xbd\x88"
  3628. "\x01\xe7\x51\xe4\x13\x85\x14\x12\x04\xf3\x29\x97\x9f\xd3\x04"
  3629. "\x7a\x13\xc5\x65\x77\x24\xad\xa6\x4d\x24\x70\x15\x7b\x3c\xdc"
  3630. "\x28\x86\x20\x94\x4d\x78\xdb\xcd\xdb\xd9\x12\x99\x3f\x09\x13"
  3631. "\xf1\x64\xfb\x2c\xe9\x51\x31\xa2\xd0";
  3632. b.inLen = XSTRLEN(b.input);
  3633. b.outLen = 114;
  3634. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  3635. c.output = "\x4d\x8c\x2d\xd2\x43\x5a\x01\x28\xee\xfb\xb8\xc3\x6f\x6f\x87"
  3636. "\x13\x3a\x79\x11\xe1\x8d\x97\x9e\xe1\xae\x6b\xe5\xd4\xfd\x2e"
  3637. "\x33\x29\x40\xd8\x68\x8a\x4e\x6a\x59\xaa\x80\x60\xf1\xf9\xbc"
  3638. "\x99\x6c\x05\xac\xa3\xc6\x96\xa8\xb6\x62\x79\xdc\x67\x2c\x74"
  3639. "\x0b\xb2\x24\xec\x37\xa9\x2b\x65\xdb\x05\x39\xc0\x20\x34\x55"
  3640. "\xf5\x1d\x97\xcc\xe4\xcf\xc4\x91\x27\xd7\x26\x0a\xfc\x67\x3a"
  3641. "\xf2\x08\xba\xf1\x9b\xe2\x12\x33\xf3\xde\xbe\x78\xd0\x67\x60"
  3642. "\xcf\xa5\x51\xee\x1e\x07\x91\x41\xd4";
  3643. c.inLen = XSTRLEN(c.input);
  3644. c.outLen = 114;
  3645. /* Taken from NIST CAVP test vectors - full rate output. */
  3646. d.input = "\xdc\x88\x6d\xf3\xf6\x9c\x49\x51\x3d\xe3\x62\x7e\x94\x81\xdb"
  3647. "\x58\x71\xe8\xee\x88\xeb\x9f\x99\x61\x15\x41\x93\x0a\x8b\xc8"
  3648. "\x85\xe0";
  3649. d.output = "\x00\x64\x8a\xfb\xc5\xe6\x51\x64\x9d\xb1\xfd\x82\x93\x6b\x00"
  3650. "\xdb\xbc\x12\x2f\xb4\xc8\x77\x86\x0d\x38\x5c\x49\x50\xd5\x6d"
  3651. "\xe7\xe0\x96\xd6\x13\xd7\xa3\xf2\x7e\xd8\xf2\x63\x34\xb0\xcc"
  3652. "\xc1\x40\x7b\x41\xdc\xcb\x23\xdf\xaa\x52\x98\x18\xd1\x12\x5c"
  3653. "\xd5\x34\x80\x92\x52\x43\x66\xb8\x5f\xab\xb9\x7c\x6c\xd1\xe6"
  3654. "\x06\x6f\x45\x9b\xcc\x56\x6d\xa8\x7e\xc9\xb7\xba\x36\x79\x2d"
  3655. "\x11\x8a\xc3\x9a\x4c\xce\xf6\x19\x2b\xbf\x3a\x54\xaf\x18\xe5"
  3656. "\x7b\x0c\x14\x61\x01\xf6\xae\xaa\x82\x2b\xc4\xb4\xc9\x70\x8b"
  3657. "\x09\xf0\xb3\xba\xb4\x1b\xcc\xe9\x64\xd9\x99\xd1\x10\x7b\xd7"
  3658. "\xc2";
  3659. d.inLen = 32;
  3660. d.outLen = 136;
  3661. /* Taken from NIST CAVP test vectors - more than one output block. */
  3662. e.input = "\x8d\x80\x01\xe2\xc0\x96\xf1\xb8\x8e\x7c\x92\x24\xa0\x86\xef"
  3663. "\xd4\x79\x7f\xbf\x74\xa8\x03\x3a\x2d\x42\x2a\x2b\x6b\x8f\x67"
  3664. "\x47\xe4";
  3665. e.output = "\x2e\x97\x5f\x6a\x8a\x14\xf0\x70\x4d\x51\xb1\x36\x67\xd8\x19"
  3666. "\x5c\x21\x9f\x71\xe6\x34\x56\x96\xc4\x9f\xa4\xb9\xd0\x8e\x92"
  3667. "\x25\xd3\xd3\x93\x93\x42\x51\x52\xc9\x7e\x71\xdd\x24\x60\x1c"
  3668. "\x11\xab\xcf\xa0\xf1\x2f\x53\xc6\x80\xbd\x3a\xe7\x57\xb8\x13"
  3669. "\x4a\x9c\x10\xd4\x29\x61\x58\x69\x21\x7f\xdd\x58\x85\xc4\xdb"
  3670. "\x17\x49\x85\x70\x3a\x6d\x6d\xe9\x4a\x66\x7e\xac\x30\x23\x44"
  3671. "\x3a\x83\x37\xae\x1b\xc6\x01\xb7\x6d\x7d\x38\xec\x3c\x34\x46"
  3672. "\x31\x05\xf0\xd3\x94\x9d\x78\xe5\x62\xa0\x39\xe4\x46\x95\x48"
  3673. "\xb6\x09\x39\x5d\xe5\xa4\xfd\x43\xc4\x6c\xa9\xfd\x6e\xe2\x9a"
  3674. "\xda\x5e\xfc\x07\xd8\x4d\x55\x32\x49\x45\x0d\xab\x4a\x49\xc4"
  3675. "\x83\xde\xd2\x50\xc9\x33\x8f\x85\xcd\x93\x7a\xe6\x6b\xb4\x36"
  3676. "\xf3\xb4\x02\x6e\x85\x9f\xda\x1c\xa5\x71\x43\x2f\x3b\xfc\x09"
  3677. "\xe7\xc0\x3c\xa4\xd1\x83\xb7\x41\x11\x1c\xa0\x48\x3d\x0e\xda"
  3678. "\xbc\x03\xfe\xb2\x3b\x17\xee\x48\xe8\x44\xba\x24\x08\xd9\xdc"
  3679. "\xfd\x01\x39\xd2\xe8\xc7\x31\x01\x25\xae\xe8\x01\xc6\x1a\xb7"
  3680. "\x90\x0d\x1e\xfc\x47\xc0\x78\x28\x17\x66\xf3\x61\xc5\xe6\x11"
  3681. "\x13\x46\x23\x5e\x1d\xc3\x83\x25\x66\x6c";
  3682. e.inLen = 32;
  3683. e.outLen = 250;
  3684. test_sha[0] = a;
  3685. test_sha[1] = b;
  3686. test_sha[2] = c;
  3687. test_sha[3] = d;
  3688. test_sha[4] = e;
  3689. ret = wc_InitShake256(&sha, HEAP_HINT, devId);
  3690. if (ret != 0)
  3691. return -3100;
  3692. for (i = 0; i < times; ++i) {
  3693. ret = wc_Shake256_Update(&sha, (byte*)test_sha[i].input,
  3694. (word32)test_sha[i].inLen);
  3695. if (ret != 0)
  3696. ERROR_OUT(-3101 - i, exit);
  3697. ret = wc_Shake256_Final(&sha, hash, (word32)test_sha[i].outLen);
  3698. if (ret != 0)
  3699. ERROR_OUT(-3102 - i, exit);
  3700. if (XMEMCMP(hash, test_sha[i].output, test_sha[i].outLen) != 0)
  3701. ERROR_OUT(-3103 - i, exit);
  3702. }
  3703. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  3704. large_input = (byte *)XMALLOC(SHAKE256_LARGE_INPUT_BUFSIZ, NULL,
  3705. DYNAMIC_TYPE_TMP_BUFFER);
  3706. if (large_input == NULL)
  3707. ERROR_OUT(-3107, exit);
  3708. #endif
  3709. #ifndef NO_LARGE_HASH_TEST
  3710. /* BEGIN LARGE HASH TEST */ {
  3711. for (i = 0; i < SHAKE256_LARGE_INPUT_BUFSIZ; i++) {
  3712. large_input[i] = (byte)(i & 0xFF);
  3713. }
  3714. times = 100;
  3715. for (i = 0; i < times; ++i) {
  3716. ret = wc_Shake256_Update(&sha, (byte*)large_input,
  3717. SHAKE256_LARGE_INPUT_BUFSIZ);
  3718. if (ret != 0)
  3719. ERROR_OUT(-3104, exit);
  3720. }
  3721. ret = wc_Shake256_Final(&sha, hash, (word32)sizeof(hash));
  3722. if (ret != 0)
  3723. ERROR_OUT(-3105, exit);
  3724. if (XMEMCMP(hash, large_digest, 114) != 0)
  3725. ERROR_OUT(-3106, exit);
  3726. } /* END LARGE HASH TEST */
  3727. #endif /* NO_LARGE_HASH_TEST */
  3728. ret = shake256_absorb_test(&sha, large_input, SHAKE256_LARGE_INPUT_BUFSIZ);
  3729. exit:
  3730. wc_Shake256_Free(&sha);
  3731. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  3732. if (large_input != NULL)
  3733. XFREE(large_input, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  3734. #endif
  3735. return ret;
  3736. }
  3737. #endif
  3738. #ifndef NO_HASH_WRAPPER
  3739. WOLFSSL_TEST_SUBROUTINE int hash_test(void)
  3740. {
  3741. wc_HashAlg hash;
  3742. int ret, exp_ret;
  3743. int i, j;
  3744. int digestSz;
  3745. byte data[] = "0123456789abcdef0123456789abcdef0123456";
  3746. byte out[WC_MAX_DIGEST_SIZE];
  3747. byte hashOut[WC_MAX_DIGEST_SIZE];
  3748. #if !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC)
  3749. enum wc_HashType hashType;
  3750. #endif
  3751. enum wc_HashType typesGood[] = { WC_HASH_TYPE_MD5, WC_HASH_TYPE_SHA,
  3752. WC_HASH_TYPE_SHA224, WC_HASH_TYPE_SHA256,
  3753. WC_HASH_TYPE_SHA384, WC_HASH_TYPE_SHA512,
  3754. WC_HASH_TYPE_SHA3_224,
  3755. WC_HASH_TYPE_SHA3_256,
  3756. WC_HASH_TYPE_SHA3_384,
  3757. WC_HASH_TYPE_SHA3_512 };
  3758. enum wc_HashType typesNoImpl[] = {
  3759. #ifdef NO_MD5
  3760. WC_HASH_TYPE_MD5,
  3761. #endif
  3762. #ifdef NO_SHA
  3763. WC_HASH_TYPE_SHA,
  3764. #endif
  3765. #ifndef WOLFSSL_SHA224
  3766. WC_HASH_TYPE_SHA224,
  3767. #endif
  3768. #ifdef NO_SHA256
  3769. WC_HASH_TYPE_SHA256,
  3770. #endif
  3771. #ifndef WOLFSSL_SHA384
  3772. WC_HASH_TYPE_SHA384,
  3773. #endif
  3774. #ifndef WOLFSSL_SHA512
  3775. WC_HASH_TYPE_SHA512,
  3776. #endif
  3777. #if !defined(WOLFSSL_SHA3) || defined(WOLFSSL_NOSHA3_224)
  3778. WC_HASH_TYPE_SHA3_224,
  3779. #endif
  3780. #if !defined(WOLFSSL_SHA3) || defined(WOLFSSL_NOSHA3_256)
  3781. WC_HASH_TYPE_SHA3_256,
  3782. #endif
  3783. #if !defined(WOLFSSL_SHA3) || defined(WOLFSSL_NOSHA3_384)
  3784. WC_HASH_TYPE_SHA3_384,
  3785. #endif
  3786. #if !defined(WOLFSSL_SHA3) || defined(WOLFSSL_NOSHA3_512)
  3787. WC_HASH_TYPE_SHA3_512,
  3788. #endif
  3789. WC_HASH_TYPE_NONE
  3790. };
  3791. enum wc_HashType typesBad[] = { WC_HASH_TYPE_NONE, WC_HASH_TYPE_MD5_SHA,
  3792. WC_HASH_TYPE_MD2, WC_HASH_TYPE_MD4 };
  3793. enum wc_HashType typesHashBad[] = { WC_HASH_TYPE_MD2, WC_HASH_TYPE_MD4,
  3794. WC_HASH_TYPE_BLAKE2B,
  3795. WC_HASH_TYPE_NONE };
  3796. /* Parameter Validation testing. */
  3797. ret = wc_HashInit(NULL, WC_HASH_TYPE_SHA256);
  3798. if (ret != BAD_FUNC_ARG)
  3799. return -3200;
  3800. ret = wc_HashUpdate(NULL, WC_HASH_TYPE_SHA256, NULL, sizeof(data));
  3801. if (ret != BAD_FUNC_ARG)
  3802. return -3201;
  3803. ret = wc_HashUpdate(&hash, WC_HASH_TYPE_SHA256, NULL, sizeof(data));
  3804. if (ret != BAD_FUNC_ARG)
  3805. return -3202;
  3806. ret = wc_HashUpdate(NULL, WC_HASH_TYPE_SHA256, data, sizeof(data));
  3807. if (ret != BAD_FUNC_ARG)
  3808. return -3203;
  3809. ret = wc_HashFinal(NULL, WC_HASH_TYPE_SHA256, NULL);
  3810. if (ret != BAD_FUNC_ARG)
  3811. return -3204;
  3812. ret = wc_HashFinal(&hash, WC_HASH_TYPE_SHA256, NULL);
  3813. if (ret != BAD_FUNC_ARG)
  3814. return -3205;
  3815. ret = wc_HashFinal(NULL, WC_HASH_TYPE_SHA256, out);
  3816. if (ret != BAD_FUNC_ARG)
  3817. return -3206;
  3818. /* Try invalid hash algorithms. */
  3819. for (i = 0; i < (int)(sizeof(typesBad)/sizeof(*typesBad)); i++) {
  3820. ret = wc_HashInit(&hash, typesBad[i]);
  3821. if (ret != BAD_FUNC_ARG)
  3822. return -3207 - i;
  3823. ret = wc_HashUpdate(&hash, typesBad[i], data, sizeof(data));
  3824. if (ret != BAD_FUNC_ARG)
  3825. return -3217 - i;
  3826. ret = wc_HashFinal(&hash, typesBad[i], out);
  3827. if (ret != BAD_FUNC_ARG)
  3828. return -3227 - i;
  3829. wc_HashFree(&hash, typesBad[i]);
  3830. }
  3831. /* Try valid hash algorithms. */
  3832. for (i = 0, j = 0; i < (int)(sizeof(typesGood)/sizeof(*typesGood)); i++) {
  3833. exp_ret = 0;
  3834. if (typesGood[i] == typesNoImpl[j]) {
  3835. /* Recognized but no implementation compiled in. */
  3836. exp_ret = HASH_TYPE_E;
  3837. j++;
  3838. }
  3839. ret = wc_HashInit(&hash, typesGood[i]);
  3840. if (ret != exp_ret)
  3841. return -3237 - i;
  3842. ret = wc_HashUpdate(&hash, typesGood[i], data, sizeof(data));
  3843. if (ret != exp_ret)
  3844. return -3247 - i;
  3845. ret = wc_HashFinal(&hash, typesGood[i], out);
  3846. if (ret != exp_ret)
  3847. return -3257 - i;
  3848. wc_HashFree(&hash, typesGood[i]);
  3849. digestSz = wc_HashGetDigestSize(typesGood[i]);
  3850. if (exp_ret < 0 && digestSz != exp_ret)
  3851. return -3267 - i;
  3852. if (exp_ret == 0 && digestSz < 0)
  3853. return -3277 - i;
  3854. if (exp_ret == 0) {
  3855. ret = wc_Hash(typesGood[i], data, sizeof(data), hashOut,
  3856. digestSz - 1);
  3857. if (ret != BUFFER_E)
  3858. return -3287 - i;
  3859. }
  3860. ret = wc_Hash(typesGood[i], data, sizeof(data), hashOut, digestSz);
  3861. if (ret != exp_ret)
  3862. return -3297 - i;
  3863. if (exp_ret == 0 && XMEMCMP(out, hashOut, digestSz) != 0)
  3864. return -3307 -i;
  3865. ret = wc_HashGetBlockSize(typesGood[i]);
  3866. if (exp_ret < 0 && ret != exp_ret)
  3867. return -3308 - i;
  3868. if (exp_ret == 0 && ret < 0)
  3869. return -3318 - i;
  3870. #if !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC)
  3871. ret = wc_HashGetOID(typesGood[i]);
  3872. if (ret == BAD_FUNC_ARG ||
  3873. (exp_ret == 0 && ret == HASH_TYPE_E) ||
  3874. (exp_ret != 0 && ret != HASH_TYPE_E)) {
  3875. return -3328 - i;
  3876. }
  3877. hashType = wc_OidGetHash(ret);
  3878. if (exp_ret == 0 && hashType != typesGood[i])
  3879. return -3338 - i;
  3880. #endif /* !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC) */
  3881. }
  3882. for (i = 0; i < (int)(sizeof(typesHashBad)/sizeof(*typesHashBad)); i++) {
  3883. ret = wc_Hash(typesHashBad[i], data, sizeof(data), out, sizeof(out));
  3884. if (ret != BAD_FUNC_ARG && ret != BUFFER_E)
  3885. return -3348 - i;
  3886. }
  3887. #if !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC)
  3888. ret = wc_HashGetOID(WC_HASH_TYPE_MD2);
  3889. #ifdef WOLFSSL_MD2
  3890. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  3891. return -3358;
  3892. #else
  3893. if (ret != HASH_TYPE_E)
  3894. return -3359;
  3895. #endif
  3896. hashType = wc_OidGetHash(646); /* Md2h */
  3897. #ifdef WOLFSSL_MD2
  3898. if (hashType != WC_HASH_TYPE_MD2)
  3899. return -3360;
  3900. #else
  3901. if (hashType != WC_HASH_TYPE_NONE)
  3902. return -3361;
  3903. #endif
  3904. ret = wc_HashGetOID(WC_HASH_TYPE_MD5_SHA);
  3905. #ifndef NO_MD5
  3906. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  3907. return -3362;
  3908. #else
  3909. if (ret != HASH_TYPE_E)
  3910. return -3363;
  3911. #endif
  3912. ret = wc_HashGetOID(WC_HASH_TYPE_MD4);
  3913. if (ret != BAD_FUNC_ARG)
  3914. return -3364;
  3915. ret = wc_HashGetOID(WC_HASH_TYPE_NONE);
  3916. if (ret != BAD_FUNC_ARG)
  3917. return -3365;
  3918. hashType = wc_OidGetHash(0);
  3919. if (hashType != WC_HASH_TYPE_NONE)
  3920. return -3366;
  3921. #endif /* !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC) */
  3922. ret = wc_HashGetBlockSize(WC_HASH_TYPE_MD2);
  3923. #ifdef WOLFSSL_MD2
  3924. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  3925. return -3367;
  3926. #else
  3927. if (ret != HASH_TYPE_E)
  3928. return -3368;
  3929. #endif
  3930. ret = wc_HashGetDigestSize(WC_HASH_TYPE_MD2);
  3931. #ifdef WOLFSSL_MD2
  3932. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  3933. return -3369;
  3934. #else
  3935. if (ret != HASH_TYPE_E)
  3936. return -3370;
  3937. #endif
  3938. ret = wc_HashGetBlockSize(WC_HASH_TYPE_MD4);
  3939. #ifndef NO_MD4
  3940. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  3941. return -3371;
  3942. #else
  3943. if (ret != HASH_TYPE_E)
  3944. return -3372;
  3945. #endif
  3946. ret = wc_HashGetDigestSize(WC_HASH_TYPE_MD4);
  3947. #ifndef NO_MD4
  3948. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  3949. return -3373;
  3950. #else
  3951. if (ret != HASH_TYPE_E)
  3952. return -3374;
  3953. #endif
  3954. ret = wc_HashGetBlockSize(WC_HASH_TYPE_MD5_SHA);
  3955. #if !defined(NO_MD5) && !defined(NO_SHA)
  3956. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  3957. return -3375;
  3958. #else
  3959. if (ret != HASH_TYPE_E)
  3960. return -3376;
  3961. #endif
  3962. ret = wc_HashGetBlockSize(WC_HASH_TYPE_BLAKE2B);
  3963. #if defined(HAVE_BLAKE2) || defined(HAVE_BLAKE2S)
  3964. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  3965. return -3377;
  3966. #else
  3967. if (ret != HASH_TYPE_E)
  3968. return -3378;
  3969. #endif
  3970. ret = wc_HashGetDigestSize(WC_HASH_TYPE_BLAKE2B);
  3971. #if defined(HAVE_BLAKE2) || defined(HAVE_BLAKE2S)
  3972. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  3973. return -3379;
  3974. #else
  3975. if (ret != HASH_TYPE_E)
  3976. return -3380;
  3977. #endif
  3978. ret = wc_HashGetBlockSize(WC_HASH_TYPE_NONE);
  3979. if (ret != BAD_FUNC_ARG)
  3980. return -3381;
  3981. ret = wc_HashGetDigestSize(WC_HASH_TYPE_NONE);
  3982. if (ret != BAD_FUNC_ARG)
  3983. return -3382;
  3984. #if !defined(NO_CERTS) && !defined(NO_ASN)
  3985. #if defined(WOLFSSL_MD2) && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  3986. ret = wc_GetCTC_HashOID(MD2);
  3987. if (ret == 0)
  3988. return -3383;
  3989. #endif
  3990. #ifndef NO_MD5
  3991. ret = wc_GetCTC_HashOID(WC_MD5);
  3992. if (ret == 0)
  3993. return -3384;
  3994. #endif
  3995. #ifndef NO_SHA
  3996. ret = wc_GetCTC_HashOID(WC_SHA);
  3997. if (ret == 0)
  3998. return -3385;
  3999. #endif
  4000. #ifdef WOLFSSL_SHA224
  4001. ret = wc_GetCTC_HashOID(WC_SHA224);
  4002. if (ret == 0)
  4003. return -3386;
  4004. #endif
  4005. #ifndef NO_SHA256
  4006. ret = wc_GetCTC_HashOID(WC_SHA256);
  4007. if (ret == 0)
  4008. return -3387;
  4009. #endif
  4010. #ifdef WOLFSSL_SHA384
  4011. ret = wc_GetCTC_HashOID(WC_SHA384);
  4012. if (ret == 0)
  4013. return -3388;
  4014. #endif
  4015. #ifdef WOLFSSL_SHA512
  4016. ret = wc_GetCTC_HashOID(WC_SHA512);
  4017. if (ret == 0)
  4018. return -3389;
  4019. #endif
  4020. ret = wc_GetCTC_HashOID(-1);
  4021. if (ret != 0)
  4022. return -3390;
  4023. #endif
  4024. return 0;
  4025. }
  4026. #endif /* !NO_HASH_WRAPPER */
  4027. #if !defined(NO_HMAC) && !defined(NO_MD5) && !(defined(HAVE_FIPS) && \
  4028. defined(HAVE_FIPS_VERSION) && \
  4029. (HAVE_FIPS_VERSION >= 5))
  4030. WOLFSSL_TEST_SUBROUTINE int hmac_md5_test(void)
  4031. {
  4032. Hmac hmac;
  4033. byte hash[WC_MD5_DIGEST_SIZE];
  4034. const char* keys[]=
  4035. {
  4036. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
  4037. "Jefe",
  4038. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  4039. };
  4040. testVector a, b, c;
  4041. testVector test_hmac[3];
  4042. int ret;
  4043. int times = sizeof(test_hmac) / sizeof(testVector), i;
  4044. a.input = "Hi There";
  4045. a.output = "\x92\x94\x72\x7a\x36\x38\xbb\x1c\x13\xf4\x8e\xf8\x15\x8b\xfc"
  4046. "\x9d";
  4047. a.inLen = XSTRLEN(a.input);
  4048. a.outLen = WC_MD5_DIGEST_SIZE;
  4049. b.input = "what do ya want for nothing?";
  4050. b.output = "\x75\x0c\x78\x3e\x6a\xb0\xb5\x03\xea\xa8\x6e\x31\x0a\x5d\xb7"
  4051. "\x38";
  4052. b.inLen = XSTRLEN(b.input);
  4053. b.outLen = WC_MD5_DIGEST_SIZE;
  4054. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4055. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4056. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4057. "\xDD\xDD\xDD\xDD\xDD\xDD";
  4058. c.output = "\x56\xbe\x34\x52\x1d\x14\x4c\x88\xdb\xb8\xc7\x33\xf0\xe8\xb3"
  4059. "\xf6";
  4060. c.inLen = XSTRLEN(c.input);
  4061. c.outLen = WC_MD5_DIGEST_SIZE;
  4062. test_hmac[0] = a;
  4063. test_hmac[1] = b;
  4064. test_hmac[2] = c;
  4065. for (i = 0; i < times; ++i) {
  4066. #if defined(HAVE_FIPS) || defined(HAVE_CAVIUM)
  4067. if (i == 1) {
  4068. continue; /* cavium can't handle short keys, fips not allowed */
  4069. }
  4070. #endif
  4071. if (wc_HmacInit(&hmac, HEAP_HINT, devId) != 0) {
  4072. return -3400;
  4073. }
  4074. ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys[i],
  4075. (word32)XSTRLEN(keys[i]));
  4076. if (ret != 0)
  4077. return -3401;
  4078. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  4079. (word32)test_hmac[i].inLen);
  4080. if (ret != 0)
  4081. return -3402;
  4082. ret = wc_HmacFinal(&hmac, hash);
  4083. if (ret != 0)
  4084. return -3403;
  4085. if (XMEMCMP(hash, test_hmac[i].output, WC_MD5_DIGEST_SIZE) != 0)
  4086. return -3404 - i;
  4087. wc_HmacFree(&hmac);
  4088. }
  4089. #ifndef HAVE_FIPS
  4090. if (wc_HmacSizeByType(WC_MD5) != WC_MD5_DIGEST_SIZE)
  4091. return -3414;
  4092. #endif
  4093. return 0;
  4094. }
  4095. #endif /* !NO_HMAC && !NO_MD5 && (!HAVE_FIPS || (HAVE_FIPS_VERSION < 5)) */
  4096. #if !defined(NO_HMAC) && !defined(NO_SHA)
  4097. WOLFSSL_TEST_SUBROUTINE int hmac_sha_test(void)
  4098. {
  4099. Hmac hmac;
  4100. byte hash[WC_SHA_DIGEST_SIZE];
  4101. const char* keys[]=
  4102. {
  4103. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  4104. "\x0b\x0b\x0b",
  4105. "Jefe",
  4106. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  4107. "\xAA\xAA\xAA"
  4108. };
  4109. testVector a, b, c;
  4110. testVector test_hmac[3];
  4111. int ret;
  4112. int times = sizeof(test_hmac) / sizeof(testVector), i;
  4113. a.input = "Hi There";
  4114. a.output = "\xb6\x17\x31\x86\x55\x05\x72\x64\xe2\x8b\xc0\xb6\xfb\x37\x8c"
  4115. "\x8e\xf1\x46\xbe\x00";
  4116. a.inLen = XSTRLEN(a.input);
  4117. a.outLen = WC_SHA_DIGEST_SIZE;
  4118. b.input = "what do ya want for nothing?";
  4119. b.output = "\xef\xfc\xdf\x6a\xe5\xeb\x2f\xa2\xd2\x74\x16\xd5\xf1\x84\xdf"
  4120. "\x9c\x25\x9a\x7c\x79";
  4121. b.inLen = XSTRLEN(b.input);
  4122. b.outLen = WC_SHA_DIGEST_SIZE;
  4123. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4124. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4125. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4126. "\xDD\xDD\xDD\xDD\xDD\xDD";
  4127. c.output = "\x12\x5d\x73\x42\xb9\xac\x11\xcd\x91\xa3\x9a\xf4\x8a\xa1\x7b"
  4128. "\x4f\x63\xf1\x75\xd3";
  4129. c.inLen = XSTRLEN(c.input);
  4130. c.outLen = WC_SHA_DIGEST_SIZE;
  4131. test_hmac[0] = a;
  4132. test_hmac[1] = b;
  4133. test_hmac[2] = c;
  4134. for (i = 0; i < times; ++i) {
  4135. #if defined(HAVE_FIPS) || defined(HAVE_CAVIUM)
  4136. if (i == 1)
  4137. continue; /* cavium can't handle short keys, fips not allowed */
  4138. #endif
  4139. if (wc_HmacInit(&hmac, HEAP_HINT, devId) != 0)
  4140. return -3500;
  4141. ret = wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys[i],
  4142. (word32)XSTRLEN(keys[i]));
  4143. if (ret != 0)
  4144. return -3501;
  4145. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  4146. (word32)test_hmac[i].inLen);
  4147. if (ret != 0)
  4148. return -3502;
  4149. ret = wc_HmacFinal(&hmac, hash);
  4150. if (ret != 0)
  4151. return -3503;
  4152. if (XMEMCMP(hash, test_hmac[i].output, WC_SHA_DIGEST_SIZE) != 0)
  4153. return -3504 - i;
  4154. wc_HmacFree(&hmac);
  4155. }
  4156. #ifndef HAVE_FIPS
  4157. if (wc_HmacSizeByType(WC_SHA) != WC_SHA_DIGEST_SIZE)
  4158. return -3514;
  4159. #endif
  4160. return 0;
  4161. }
  4162. #endif
  4163. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
  4164. WOLFSSL_TEST_SUBROUTINE int hmac_sha224_test(void)
  4165. {
  4166. Hmac hmac;
  4167. byte hash[WC_SHA224_DIGEST_SIZE];
  4168. const char* keys[]=
  4169. {
  4170. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  4171. "\x0b\x0b\x0b",
  4172. "Jefe",
  4173. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  4174. "\xAA\xAA\xAA",
  4175. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4176. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4177. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4178. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4179. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4180. };
  4181. testVector a, b, c, d;
  4182. testVector test_hmac[4];
  4183. int ret;
  4184. int times = sizeof(test_hmac) / sizeof(testVector), i;
  4185. a.input = "Hi There";
  4186. a.output = "\x89\x6f\xb1\x12\x8a\xbb\xdf\x19\x68\x32\x10\x7c\xd4\x9d\xf3"
  4187. "\x3f\x47\xb4\xb1\x16\x99\x12\xba\x4f\x53\x68\x4b\x22";
  4188. a.inLen = XSTRLEN(a.input);
  4189. a.outLen = WC_SHA224_DIGEST_SIZE;
  4190. b.input = "what do ya want for nothing?";
  4191. b.output = "\xa3\x0e\x01\x09\x8b\xc6\xdb\xbf\x45\x69\x0f\x3a\x7e\x9e\x6d"
  4192. "\x0f\x8b\xbe\xa2\xa3\x9e\x61\x48\x00\x8f\xd0\x5e\x44";
  4193. b.inLen = XSTRLEN(b.input);
  4194. b.outLen = WC_SHA224_DIGEST_SIZE;
  4195. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4196. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4197. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4198. "\xDD\xDD\xDD\xDD\xDD\xDD";
  4199. c.output = "\x7f\xb3\xcb\x35\x88\xc6\xc1\xf6\xff\xa9\x69\x4d\x7d\x6a\xd2"
  4200. "\x64\x93\x65\xb0\xc1\xf6\x5d\x69\xd1\xec\x83\x33\xea";
  4201. c.inLen = XSTRLEN(c.input);
  4202. c.outLen = WC_SHA224_DIGEST_SIZE;
  4203. d.input = "Big Key Input";
  4204. d.output = "\xe7\x4e\x2b\x8a\xa9\xf0\x37\x2f\xed\xae\x70\x0c\x49\x47\xf1"
  4205. "\x46\x54\xa7\x32\x6b\x55\x01\x87\xd2\xc8\x02\x0e\x3a";
  4206. d.inLen = XSTRLEN(d.input);
  4207. d.outLen = WC_SHA224_DIGEST_SIZE;
  4208. test_hmac[0] = a;
  4209. test_hmac[1] = b;
  4210. test_hmac[2] = c;
  4211. test_hmac[3] = d;
  4212. for (i = 0; i < times; ++i) {
  4213. #if defined(HAVE_FIPS) || defined(HAVE_CAVIUM)
  4214. if (i == 1)
  4215. continue; /* cavium can't handle short keys, fips not allowed */
  4216. #endif
  4217. if (wc_HmacInit(&hmac, HEAP_HINT, devId) != 0)
  4218. return -3600;
  4219. ret = wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys[i],
  4220. (word32)XSTRLEN(keys[i]));
  4221. if (ret != 0)
  4222. return -3601;
  4223. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  4224. (word32)test_hmac[i].inLen);
  4225. if (ret != 0)
  4226. return -3602;
  4227. ret = wc_HmacFinal(&hmac, hash);
  4228. if (ret != 0)
  4229. return -3603;
  4230. if (XMEMCMP(hash, test_hmac[i].output, WC_SHA224_DIGEST_SIZE) != 0)
  4231. return -3604 - i;
  4232. wc_HmacFree(&hmac);
  4233. }
  4234. #ifndef HAVE_FIPS
  4235. if (wc_HmacSizeByType(WC_SHA224) != WC_SHA224_DIGEST_SIZE)
  4236. return -3614;
  4237. #endif
  4238. return 0;
  4239. }
  4240. #endif
  4241. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  4242. WOLFSSL_TEST_SUBROUTINE int hmac_sha256_test(void)
  4243. {
  4244. Hmac hmac;
  4245. byte hash[WC_SHA256_DIGEST_SIZE];
  4246. const char* keys[]=
  4247. {
  4248. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  4249. "\x0b\x0b\x0b",
  4250. "Jefe",
  4251. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  4252. "\xAA\xAA\xAA",
  4253. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  4254. "\xAA\xAA\xAA",
  4255. };
  4256. testVector a, b, c, d;
  4257. testVector test_hmac[4];
  4258. int ret;
  4259. int times = sizeof(test_hmac) / sizeof(testVector), i;
  4260. a.input = "Hi There";
  4261. a.output = "\xb0\x34\x4c\x61\xd8\xdb\x38\x53\x5c\xa8\xaf\xce\xaf\x0b\xf1"
  4262. "\x2b\x88\x1d\xc2\x00\xc9\x83\x3d\xa7\x26\xe9\x37\x6c\x2e\x32"
  4263. "\xcf\xf7";
  4264. a.inLen = XSTRLEN(a.input);
  4265. a.outLen = WC_SHA256_DIGEST_SIZE;
  4266. b.input = "what do ya want for nothing?";
  4267. b.output = "\x5b\xdc\xc1\x46\xbf\x60\x75\x4e\x6a\x04\x24\x26\x08\x95\x75"
  4268. "\xc7\x5a\x00\x3f\x08\x9d\x27\x39\x83\x9d\xec\x58\xb9\x64\xec"
  4269. "\x38\x43";
  4270. b.inLen = XSTRLEN(b.input);
  4271. b.outLen = WC_SHA256_DIGEST_SIZE;
  4272. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4273. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4274. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4275. "\xDD\xDD\xDD\xDD\xDD\xDD";
  4276. c.output = "\x77\x3e\xa9\x1e\x36\x80\x0e\x46\x85\x4d\xb8\xeb\xd0\x91\x81"
  4277. "\xa7\x29\x59\x09\x8b\x3e\xf8\xc1\x22\xd9\x63\x55\x14\xce\xd5"
  4278. "\x65\xfe";
  4279. c.inLen = XSTRLEN(c.input);
  4280. c.outLen = WC_SHA256_DIGEST_SIZE;
  4281. d.input = 0;
  4282. d.output = "\x86\xe5\x4f\xd4\x48\x72\x5d\x7e\x5d\xcf\xe2\x23\x53\xc8\x28"
  4283. "\xaf\x48\x78\x1e\xb4\x8c\xae\x81\x06\xa7\xe1\xd4\x98\x94\x9f"
  4284. "\x3e\x46";
  4285. d.inLen = 0;
  4286. d.outLen = WC_SHA256_DIGEST_SIZE;
  4287. test_hmac[0] = a;
  4288. test_hmac[1] = b;
  4289. test_hmac[2] = c;
  4290. test_hmac[3] = d;
  4291. for (i = 0; i < times; ++i) {
  4292. #if defined(HAVE_FIPS) || defined(HAVE_CAVIUM)
  4293. if (i == 1)
  4294. continue; /* cavium can't handle short keys, fips not allowed */
  4295. #endif
  4296. #if defined(HAVE_INTEL_QA) || defined(HAVE_CAVIUM)
  4297. if (i == 3)
  4298. continue; /* QuickAssist can't handle empty HMAC */
  4299. #endif
  4300. if (wc_HmacInit(&hmac, HEAP_HINT, devId) != 0)
  4301. return -3700 - i;
  4302. ret = wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys[i],
  4303. (word32)XSTRLEN(keys[i]));
  4304. if (ret != 0)
  4305. return -3710 - i;
  4306. if (test_hmac[i].input != NULL) {
  4307. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  4308. (word32)test_hmac[i].inLen);
  4309. if (ret != 0)
  4310. return -3720 - i;
  4311. }
  4312. ret = wc_HmacFinal(&hmac, hash);
  4313. if (ret != 0)
  4314. return -3730 - i;
  4315. if (XMEMCMP(hash, test_hmac[i].output, WC_SHA256_DIGEST_SIZE) != 0)
  4316. return -3740 - i;
  4317. wc_HmacFree(&hmac);
  4318. }
  4319. #ifndef HAVE_FIPS
  4320. if (wc_HmacSizeByType(WC_SHA256) != WC_SHA256_DIGEST_SIZE)
  4321. return -3750;
  4322. if (wc_HmacSizeByType(20) != BAD_FUNC_ARG)
  4323. return -3751;
  4324. #endif
  4325. if (wolfSSL_GetHmacMaxSize() != WC_MAX_DIGEST_SIZE)
  4326. return -3752;
  4327. return 0;
  4328. }
  4329. #endif
  4330. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  4331. WOLFSSL_TEST_SUBROUTINE int hmac_sha384_test(void)
  4332. {
  4333. Hmac hmac;
  4334. byte hash[WC_SHA384_DIGEST_SIZE];
  4335. const char* keys[]=
  4336. {
  4337. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  4338. "\x0b\x0b\x0b",
  4339. "Jefe",
  4340. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  4341. "\xAA\xAA\xAA",
  4342. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4343. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4344. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4345. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4346. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4347. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4348. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4349. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4350. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4351. };
  4352. testVector a, b, c, d;
  4353. testVector test_hmac[4];
  4354. int ret;
  4355. int times = sizeof(test_hmac) / sizeof(testVector), i;
  4356. a.input = "Hi There";
  4357. a.output = "\xaf\xd0\x39\x44\xd8\x48\x95\x62\x6b\x08\x25\xf4\xab\x46\x90"
  4358. "\x7f\x15\xf9\xda\xdb\xe4\x10\x1e\xc6\x82\xaa\x03\x4c\x7c\xeb"
  4359. "\xc5\x9c\xfa\xea\x9e\xa9\x07\x6e\xde\x7f\x4a\xf1\x52\xe8\xb2"
  4360. "\xfa\x9c\xb6";
  4361. a.inLen = XSTRLEN(a.input);
  4362. a.outLen = WC_SHA384_DIGEST_SIZE;
  4363. b.input = "what do ya want for nothing?";
  4364. b.output = "\xaf\x45\xd2\xe3\x76\x48\x40\x31\x61\x7f\x78\xd2\xb5\x8a\x6b"
  4365. "\x1b\x9c\x7e\xf4\x64\xf5\xa0\x1b\x47\xe4\x2e\xc3\x73\x63\x22"
  4366. "\x44\x5e\x8e\x22\x40\xca\x5e\x69\xe2\xc7\x8b\x32\x39\xec\xfa"
  4367. "\xb2\x16\x49";
  4368. b.inLen = XSTRLEN(b.input);
  4369. b.outLen = WC_SHA384_DIGEST_SIZE;
  4370. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4371. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4372. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4373. "\xDD\xDD\xDD\xDD\xDD\xDD";
  4374. c.output = "\x88\x06\x26\x08\xd3\xe6\xad\x8a\x0a\xa2\xac\xe0\x14\xc8\xa8"
  4375. "\x6f\x0a\xa6\x35\xd9\x47\xac\x9f\xeb\xe8\x3e\xf4\xe5\x59\x66"
  4376. "\x14\x4b\x2a\x5a\xb3\x9d\xc1\x38\x14\xb9\x4e\x3a\xb6\xe1\x01"
  4377. "\xa3\x4f\x27";
  4378. c.inLen = XSTRLEN(c.input);
  4379. c.outLen = WC_SHA384_DIGEST_SIZE;
  4380. d.input = "Big Key Input";
  4381. d.output = "\xd2\x3d\x29\x6e\xf5\x1e\x23\x23\x49\x18\xb3\xbf\x4c\x38\x7b"
  4382. "\x31\x21\x17\xbb\x09\x73\x27\xf8\x12\x9d\xe9\xc6\x5d\xf9\x54"
  4383. "\xd6\x38\x5a\x68\x53\x14\xee\xe0\xa6\x4f\x36\x7e\xb2\xf3\x1a"
  4384. "\x57\x41\x69";
  4385. d.inLen = XSTRLEN(d.input);
  4386. d.outLen = WC_SHA384_DIGEST_SIZE;
  4387. test_hmac[0] = a;
  4388. test_hmac[1] = b;
  4389. test_hmac[2] = c;
  4390. test_hmac[3] = d;
  4391. for (i = 0; i < times; ++i) {
  4392. #if defined(HAVE_FIPS)
  4393. if (i == 1)
  4394. continue; /* fips not allowed */
  4395. #endif
  4396. if (wc_HmacInit(&hmac, HEAP_HINT, devId) != 0)
  4397. return -3800;
  4398. ret = wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys[i],
  4399. (word32)XSTRLEN(keys[i]));
  4400. if (ret != 0)
  4401. return -3801;
  4402. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  4403. (word32)test_hmac[i].inLen);
  4404. if (ret != 0)
  4405. return -3802;
  4406. ret = wc_HmacFinal(&hmac, hash);
  4407. if (ret != 0)
  4408. return -3803;
  4409. if (XMEMCMP(hash, test_hmac[i].output, WC_SHA384_DIGEST_SIZE) != 0)
  4410. return -3804 - i;
  4411. wc_HmacFree(&hmac);
  4412. }
  4413. #ifndef HAVE_FIPS
  4414. if (wc_HmacSizeByType(WC_SHA384) != WC_SHA384_DIGEST_SIZE)
  4415. return -3814;
  4416. #endif
  4417. return 0;
  4418. }
  4419. #endif
  4420. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA512)
  4421. WOLFSSL_TEST_SUBROUTINE int hmac_sha512_test(void)
  4422. {
  4423. Hmac hmac;
  4424. byte hash[WC_SHA512_DIGEST_SIZE];
  4425. const char* keys[]=
  4426. {
  4427. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  4428. "\x0b\x0b\x0b",
  4429. "Jefe",
  4430. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  4431. "\xAA\xAA\xAA",
  4432. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4433. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4434. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4435. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4436. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4437. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4438. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4439. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4440. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4441. };
  4442. testVector a, b, c, d;
  4443. testVector test_hmac[4];
  4444. int ret;
  4445. int times = sizeof(test_hmac) / sizeof(testVector), i;
  4446. a.input = "Hi There";
  4447. a.output = "\x87\xaa\x7c\xde\xa5\xef\x61\x9d\x4f\xf0\xb4\x24\x1a\x1d\x6c"
  4448. "\xb0\x23\x79\xf4\xe2\xce\x4e\xc2\x78\x7a\xd0\xb3\x05\x45\xe1"
  4449. "\x7c\xde\xda\xa8\x33\xb7\xd6\xb8\xa7\x02\x03\x8b\x27\x4e\xae"
  4450. "\xa3\xf4\xe4\xbe\x9d\x91\x4e\xeb\x61\xf1\x70\x2e\x69\x6c\x20"
  4451. "\x3a\x12\x68\x54";
  4452. a.inLen = XSTRLEN(a.input);
  4453. a.outLen = WC_SHA512_DIGEST_SIZE;
  4454. b.input = "what do ya want for nothing?";
  4455. b.output = "\x16\x4b\x7a\x7b\xfc\xf8\x19\xe2\xe3\x95\xfb\xe7\x3b\x56\xe0"
  4456. "\xa3\x87\xbd\x64\x22\x2e\x83\x1f\xd6\x10\x27\x0c\xd7\xea\x25"
  4457. "\x05\x54\x97\x58\xbf\x75\xc0\x5a\x99\x4a\x6d\x03\x4f\x65\xf8"
  4458. "\xf0\xe6\xfd\xca\xea\xb1\xa3\x4d\x4a\x6b\x4b\x63\x6e\x07\x0a"
  4459. "\x38\xbc\xe7\x37";
  4460. b.inLen = XSTRLEN(b.input);
  4461. b.outLen = WC_SHA512_DIGEST_SIZE;
  4462. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4463. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4464. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4465. "\xDD\xDD\xDD\xDD\xDD\xDD";
  4466. c.output = "\xfa\x73\xb0\x08\x9d\x56\xa2\x84\xef\xb0\xf0\x75\x6c\x89\x0b"
  4467. "\xe9\xb1\xb5\xdb\xdd\x8e\xe8\x1a\x36\x55\xf8\x3e\x33\xb2\x27"
  4468. "\x9d\x39\xbf\x3e\x84\x82\x79\xa7\x22\xc8\x06\xb4\x85\xa4\x7e"
  4469. "\x67\xc8\x07\xb9\x46\xa3\x37\xbe\xe8\x94\x26\x74\x27\x88\x59"
  4470. "\xe1\x32\x92\xfb";
  4471. c.inLen = XSTRLEN(c.input);
  4472. c.outLen = WC_SHA512_DIGEST_SIZE;
  4473. d.input = "Big Key Input";
  4474. d.output = "\x3f\xa9\xc9\xe1\xbd\xbb\x04\x55\x1f\xef\xcc\x92\x33\x08\xeb"
  4475. "\xcf\xc1\x9a\x5b\x5b\xc0\x7c\x86\x84\xae\x8c\x40\xaf\xb1\x27"
  4476. "\x87\x38\x92\x04\xa8\xed\xd7\xd7\x07\xa9\x85\xa0\xc2\xcd\x30"
  4477. "\xc0\x56\x14\x49\xbc\x2f\x69\x15\x6a\x97\xd8\x79\x2f\xb3\x3b"
  4478. "\x1e\x18\xfe\xfa";
  4479. d.inLen = XSTRLEN(d.input);
  4480. d.outLen = WC_SHA512_DIGEST_SIZE;
  4481. test_hmac[0] = a;
  4482. test_hmac[1] = b;
  4483. test_hmac[2] = c;
  4484. test_hmac[3] = d;
  4485. for (i = 0; i < times; ++i) {
  4486. #if defined(HAVE_FIPS)
  4487. if (i == 1)
  4488. continue; /* fips not allowed */
  4489. #endif
  4490. if (wc_HmacInit(&hmac, HEAP_HINT, devId) != 0)
  4491. return -3900;
  4492. ret = wc_HmacSetKey(&hmac, WC_SHA512, (byte*)keys[i],
  4493. (word32)XSTRLEN(keys[i]));
  4494. if (ret != 0)
  4495. return -3901;
  4496. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  4497. (word32)test_hmac[i].inLen);
  4498. if (ret != 0)
  4499. return -3902;
  4500. ret = wc_HmacFinal(&hmac, hash);
  4501. if (ret != 0)
  4502. return -3903;
  4503. if (XMEMCMP(hash, test_hmac[i].output, WC_SHA512_DIGEST_SIZE) != 0)
  4504. return -3904 - i;
  4505. wc_HmacFree(&hmac);
  4506. }
  4507. #ifndef HAVE_FIPS
  4508. if (wc_HmacSizeByType(WC_SHA512) != WC_SHA512_DIGEST_SIZE)
  4509. return -3914;
  4510. #endif
  4511. return 0;
  4512. }
  4513. #endif
  4514. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA3) && \
  4515. !defined(WOLFSSL_NOSHA3_224) && !defined(WOLFSSL_NOSHA3_256) && \
  4516. !defined(WOLFSSL_NOSHA3_384) && !defined(WOLFSSL_NOSHA3_512)
  4517. WOLFSSL_TEST_SUBROUTINE int hmac_sha3_test(void)
  4518. {
  4519. Hmac hmac;
  4520. byte hash[WC_SHA3_512_DIGEST_SIZE];
  4521. const char* key[4] =
  4522. {
  4523. "Jefe",
  4524. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  4525. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
  4526. "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
  4527. "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa",
  4528. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4529. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4530. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4531. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4532. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4533. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4534. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4535. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4536. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4537. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4538. };
  4539. const char* input[4] =
  4540. {
  4541. "what do ya want for nothing?",
  4542. "Hi There",
  4543. "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
  4544. "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
  4545. "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
  4546. "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
  4547. "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd",
  4548. "Big Key Input"
  4549. };
  4550. const int hashType[4] =
  4551. {
  4552. WC_SHA3_224, WC_SHA3_256, WC_SHA3_384, WC_SHA3_512
  4553. };
  4554. const int hashSz[4] =
  4555. {
  4556. WC_SHA3_224_DIGEST_SIZE, WC_SHA3_256_DIGEST_SIZE,
  4557. WC_SHA3_384_DIGEST_SIZE, WC_SHA3_512_DIGEST_SIZE
  4558. };
  4559. const char* output[16] =
  4560. {
  4561. /* key = jefe, input = what do ya want for nothing? */
  4562. /* HMAC-SHA3-224 */
  4563. "\x7f\xdb\x8d\xd8\x8b\xd2\xf6\x0d\x1b\x79\x86\x34\xad\x38\x68\x11"
  4564. "\xc2\xcf\xc8\x5b\xfa\xf5\xd5\x2b\xba\xce\x5e\x66",
  4565. /* HMAC-SHA3-256 */
  4566. "\xc7\xd4\x07\x2e\x78\x88\x77\xae\x35\x96\xbb\xb0\xda\x73\xb8\x87"
  4567. "\xc9\x17\x1f\x93\x09\x5b\x29\x4a\xe8\x57\xfb\xe2\x64\x5e\x1b\xa5",
  4568. /* HMAC-SHA3-384 */
  4569. "\xf1\x10\x1f\x8c\xbf\x97\x66\xfd\x67\x64\xd2\xed\x61\x90\x3f\x21"
  4570. "\xca\x9b\x18\xf5\x7c\xf3\xe1\xa2\x3c\xa1\x35\x08\xa9\x32\x43\xce"
  4571. "\x48\xc0\x45\xdc\x00\x7f\x26\xa2\x1b\x3f\x5e\x0e\x9d\xf4\xc2\x0a",
  4572. /* HMAC-SHA3-512 */
  4573. "\x5a\x4b\xfe\xab\x61\x66\x42\x7c\x7a\x36\x47\xb7\x47\x29\x2b\x83"
  4574. "\x84\x53\x7c\xdb\x89\xaf\xb3\xbf\x56\x65\xe4\xc5\xe7\x09\x35\x0b"
  4575. "\x28\x7b\xae\xc9\x21\xfd\x7c\xa0\xee\x7a\x0c\x31\xd0\x22\xa9\x5e"
  4576. "\x1f\xc9\x2b\xa9\xd7\x7d\xf8\x83\x96\x02\x75\xbe\xb4\xe6\x20\x24",
  4577. /* key = 0b..., input = Hi There */
  4578. /* HMAC-SHA3-224 */
  4579. "\x3b\x16\x54\x6b\xbc\x7b\xe2\x70\x6a\x03\x1d\xca\xfd\x56\x37\x3d"
  4580. "\x98\x84\x36\x76\x41\xd8\xc5\x9a\xf3\xc8\x60\xf7",
  4581. /* HMAC-SHA3-256 */
  4582. "\xba\x85\x19\x23\x10\xdf\xfa\x96\xe2\xa3\xa4\x0e\x69\x77\x43\x51"
  4583. "\x14\x0b\xb7\x18\x5e\x12\x02\xcd\xcc\x91\x75\x89\xf9\x5e\x16\xbb",
  4584. /* HMAC-SHA3-384 */
  4585. "\x68\xd2\xdc\xf7\xfd\x4d\xdd\x0a\x22\x40\xc8\xa4\x37\x30\x5f\x61"
  4586. "\xfb\x73\x34\xcf\xb5\xd0\x22\x6e\x1b\xc2\x7d\xc1\x0a\x2e\x72\x3a"
  4587. "\x20\xd3\x70\xb4\x77\x43\x13\x0e\x26\xac\x7e\x3d\x53\x28\x86\xbd",
  4588. /* HMAC-SHA3-512 */
  4589. "\xeb\x3f\xbd\x4b\x2e\xaa\xb8\xf5\xc5\x04\xbd\x3a\x41\x46\x5a\xac"
  4590. "\xec\x15\x77\x0a\x7c\xab\xac\x53\x1e\x48\x2f\x86\x0b\x5e\xc7\xba"
  4591. "\x47\xcc\xb2\xc6\xf2\xaf\xce\x8f\x88\xd2\x2b\x6d\xc6\x13\x80\xf2"
  4592. "\x3a\x66\x8f\xd3\x88\x8b\xb8\x05\x37\xc0\xa0\xb8\x64\x07\x68\x9e",
  4593. /* key = aa..., output = dd... */
  4594. /* HMAC-SHA3-224 */
  4595. "\x67\x6c\xfc\x7d\x16\x15\x36\x38\x78\x03\x90\x69\x2b\xe1\x42\xd2"
  4596. "\xdf\x7c\xe9\x24\xb9\x09\xc0\xc0\x8d\xbf\xdc\x1a",
  4597. /* HMAC-SHA3-256 */
  4598. "\x84\xec\x79\x12\x4a\x27\x10\x78\x65\xce\xdd\x8b\xd8\x2d\xa9\x96"
  4599. "\x5e\x5e\xd8\xc3\x7b\x0a\xc9\x80\x05\xa7\xf3\x9e\xd5\x8a\x42\x07",
  4600. /* HMAC-SHA3-384 */
  4601. "\x27\x5c\xd0\xe6\x61\xbb\x8b\x15\x1c\x64\xd2\x88\xf1\xf7\x82\xfb"
  4602. "\x91\xa8\xab\xd5\x68\x58\xd7\x2b\xab\xb2\xd4\x76\xf0\x45\x83\x73"
  4603. "\xb4\x1b\x6a\xb5\xbf\x17\x4b\xec\x42\x2e\x53\xfc\x31\x35\xac\x6e",
  4604. /* HMAC-SHA3-512 */
  4605. "\x30\x9e\x99\xf9\xec\x07\x5e\xc6\xc6\xd4\x75\xed\xa1\x18\x06\x87"
  4606. "\xfc\xf1\x53\x11\x95\x80\x2a\x99\xb5\x67\x74\x49\xa8\x62\x51\x82"
  4607. "\x85\x1c\xb3\x32\xaf\xb6\xa8\x9c\x41\x13\x25\xfb\xcb\xcd\x42\xaf"
  4608. "\xcb\x7b\x6e\x5a\xab\x7e\xa4\x2c\x66\x0f\x97\xfd\x85\x84\xbf\x03",
  4609. /* key = big key, input = Big Key Input */
  4610. /* HMAC-SHA3-224 */
  4611. "\x29\xe0\x5e\x46\xc4\xa4\x5e\x46\x74\xbf\xd7\x2d\x1a\xd8\x66\xdb"
  4612. "\x2d\x0d\x10\x4e\x2b\xfa\xad\x53\x7d\x15\x69\x8b",
  4613. /* HMAC-SHA3-256 */
  4614. "\xb5\x5b\x8d\x64\xb6\x9c\x21\xd0\xbf\x20\x5c\xa2\xf7\xb9\xb1\x4e"
  4615. "\x88\x21\x61\x2c\x66\xc3\x91\xae\x6c\x95\x16\x85\x83\xe6\xf4\x9b",
  4616. /* HMAC-SHA3-384 */
  4617. "\xaa\x91\xb3\xa6\x2f\x56\xa1\xbe\x8c\x3e\x74\x38\xdb\x58\xd9\xd3"
  4618. "\x34\xde\xa0\x60\x6d\x8d\x46\xe0\xec\xa9\xf6\x06\x35\x14\xe6\xed"
  4619. "\x83\xe6\x7c\x77\x24\x6c\x11\xb5\x90\x82\xb5\x75\xda\x7b\x83\x2d",
  4620. /* HMAC-SHA3-512 */
  4621. "\x1c\xc3\xa9\x24\x4a\x4a\x3f\xbd\xc7\x20\x00\x16\x9b\x79\x47\x03"
  4622. "\x78\x75\x2c\xb5\xf1\x2e\x62\x7c\xbe\xef\x4e\x8f\x0b\x11\x2b\x32"
  4623. "\xa0\xee\xc9\xd0\x4d\x64\x64\x0b\x37\xf4\xdd\x66\xf7\x8b\xb3\xad"
  4624. "\x52\x52\x6b\x65\x12\xde\x0d\x7c\xc0\x8b\x60\x01\x6c\x37\xd7\xa8"
  4625. };
  4626. int i = 0, iMax = sizeof(input) / sizeof(input[0]),
  4627. j, jMax = sizeof(hashType) / sizeof(hashType[0]),
  4628. ret;
  4629. #ifdef HAVE_FIPS
  4630. /* FIPS requires a minimum length for HMAC keys, and "Jefe" is too
  4631. * short. Skip it in FIPS builds. */
  4632. i = 1;
  4633. #endif
  4634. for (; i < iMax; i++) {
  4635. for (j = 0; j < jMax; j++) {
  4636. if (wc_HmacInit(&hmac, HEAP_HINT, devId) != 0)
  4637. return -4000;
  4638. ret = wc_HmacSetKey(&hmac, hashType[j], (byte*)key[i],
  4639. (word32)XSTRLEN(key[i]));
  4640. if (ret != 0)
  4641. return -4001;
  4642. ret = wc_HmacUpdate(&hmac, (byte*)input[i],
  4643. (word32)XSTRLEN(input[i]));
  4644. if (ret != 0)
  4645. return -4002;
  4646. ret = wc_HmacFinal(&hmac, hash);
  4647. if (ret != 0)
  4648. return -4003;
  4649. if (XMEMCMP(hash, output[(i*jMax) + j], hashSz[j]) != 0)
  4650. return -4004;
  4651. wc_HmacFree(&hmac);
  4652. if (i > 0)
  4653. continue;
  4654. #ifndef HAVE_FIPS
  4655. ret = wc_HmacSizeByType(hashType[j]);
  4656. if (ret != hashSz[j])
  4657. return -4005;
  4658. #endif
  4659. }
  4660. }
  4661. return 0;
  4662. }
  4663. #endif
  4664. #ifdef WC_RC2
  4665. typedef struct rc2TestVector {
  4666. const char* input;
  4667. const char* output;
  4668. const char* key; /* Key, variable up to 128 bytes */
  4669. const char* iv; /* IV, 8-bytes */
  4670. int inLen;
  4671. int outLen;
  4672. int keyLen;
  4673. int effectiveKeyBits; /* Up to 1024 bits supported */
  4674. } rc2TestVector;
  4675. static int rc2_ecb_test(void)
  4676. {
  4677. int ret = 0;
  4678. byte cipher[RC2_BLOCK_SIZE];
  4679. byte plain[RC2_BLOCK_SIZE];
  4680. rc2TestVector a, b, c, d, e, f, g, h;
  4681. rc2TestVector test_rc2[8];
  4682. int times = sizeof(test_rc2) / sizeof(rc2TestVector), i;
  4683. a.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4684. a.output = "\xeb\xb7\x73\xf9\x93\x27\x8e\xff";
  4685. a.key = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4686. a.inLen = RC2_BLOCK_SIZE;
  4687. a.outLen = RC2_BLOCK_SIZE;
  4688. a.keyLen = 8;
  4689. a.effectiveKeyBits = 63;
  4690. b.input = "\xff\xff\xff\xff\xff\xff\xff\xff";
  4691. b.output = "\x27\x8b\x27\xe4\x2e\x2f\x0d\x49";
  4692. b.key = "\xff\xff\xff\xff\xff\xff\xff\xff";
  4693. b.inLen = RC2_BLOCK_SIZE;
  4694. b.outLen = RC2_BLOCK_SIZE;
  4695. b.keyLen = 8;
  4696. b.effectiveKeyBits = 64;
  4697. c.input = "\x10\x00\x00\x00\x00\x00\x00\x01";
  4698. c.output = "\x30\x64\x9e\xdf\x9b\xe7\xd2\xc2";
  4699. c.key = "\x30\x00\x00\x00\x00\x00\x00\x00";
  4700. c.inLen = RC2_BLOCK_SIZE;
  4701. c.outLen = RC2_BLOCK_SIZE;
  4702. c.keyLen = 8;
  4703. c.effectiveKeyBits = 64;
  4704. d.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4705. d.output = "\x61\xa8\xa2\x44\xad\xac\xcc\xf0";
  4706. d.key = "\x88";
  4707. d.inLen = RC2_BLOCK_SIZE;
  4708. d.outLen = RC2_BLOCK_SIZE;
  4709. d.keyLen = 1;
  4710. d.effectiveKeyBits = 64;
  4711. e.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4712. e.output = "\x6c\xcf\x43\x08\x97\x4c\x26\x7f";
  4713. e.key = "\x88\xbc\xa9\x0e\x90\x87\x5a";
  4714. e.inLen = RC2_BLOCK_SIZE;
  4715. e.outLen = RC2_BLOCK_SIZE;
  4716. e.keyLen = 7;
  4717. e.effectiveKeyBits = 64;
  4718. f.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4719. f.output = "\x1a\x80\x7d\x27\x2b\xbe\x5d\xb1";
  4720. f.key = "\x88\xbc\xa9\x0e\x90\x87\x5a\x7f"
  4721. "\x0f\x79\xc3\x84\x62\x7b\xaf\xb2";
  4722. f.inLen = RC2_BLOCK_SIZE;
  4723. f.outLen = RC2_BLOCK_SIZE;
  4724. f.keyLen = 16;
  4725. f.effectiveKeyBits = 64;
  4726. g.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4727. g.output = "\x22\x69\x55\x2a\xb0\xf8\x5c\xa6";
  4728. g.key = "\x88\xbc\xa9\x0e\x90\x87\x5a\x7f"
  4729. "\x0f\x79\xc3\x84\x62\x7b\xaf\xb2";
  4730. g.inLen = RC2_BLOCK_SIZE;
  4731. g.outLen = RC2_BLOCK_SIZE;
  4732. g.keyLen = 16;
  4733. g.effectiveKeyBits = 128;
  4734. h.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4735. h.output = "\x5b\x78\xd3\xa4\x3d\xff\xf1\xf1";
  4736. h.key = "\x88\xbc\xa9\x0e\x90\x87\x5a\x7f"
  4737. "\x0f\x79\xc3\x84\x62\x7b\xaf\xb2"
  4738. "\x16\xf8\x0a\x6f\x85\x92\x05\x84"
  4739. "\xc4\x2f\xce\xb0\xbe\x25\x5d\xaf"
  4740. "\x1e";
  4741. h.inLen = RC2_BLOCK_SIZE;
  4742. h.outLen = RC2_BLOCK_SIZE;
  4743. h.keyLen = 33;
  4744. h.effectiveKeyBits = 129;
  4745. a.iv = b.iv = c.iv = d.iv = e.iv = f.iv = g.iv = h.iv = NULL;
  4746. test_rc2[0] = a;
  4747. test_rc2[1] = b;
  4748. test_rc2[2] = c;
  4749. test_rc2[3] = d;
  4750. test_rc2[4] = e;
  4751. test_rc2[5] = f;
  4752. test_rc2[6] = g;
  4753. test_rc2[7] = h;
  4754. for (i = 0; i < times; ++i) {
  4755. Rc2 enc;
  4756. XMEMSET(cipher, 0, RC2_BLOCK_SIZE);
  4757. XMEMSET(plain, 0, RC2_BLOCK_SIZE);
  4758. ret = wc_Rc2SetKey(&enc, (byte*)test_rc2[i].key, test_rc2[i].keyLen,
  4759. NULL, test_rc2[i].effectiveKeyBits);
  4760. if (ret != 0) {
  4761. return -4100;
  4762. }
  4763. /* ECB encrypt */
  4764. ret = wc_Rc2EcbEncrypt(&enc, cipher, (byte*)test_rc2[i].input,
  4765. (word32)test_rc2[i].outLen);
  4766. if (ret != 0) {
  4767. return -4101;
  4768. }
  4769. if (XMEMCMP(cipher, test_rc2[i].output, test_rc2[i].outLen)) {
  4770. return -4102;
  4771. }
  4772. /* ECB decrypt */
  4773. ret = wc_Rc2EcbDecrypt(&enc, plain, cipher, RC2_BLOCK_SIZE);
  4774. if (ret != 0) {
  4775. return -4103;
  4776. }
  4777. if (XMEMCMP(plain, test_rc2[i].input, RC2_BLOCK_SIZE)) {
  4778. return -4104;
  4779. }
  4780. }
  4781. return 0;
  4782. }
  4783. static int rc2_cbc_test(void)
  4784. {
  4785. int ret = 0;
  4786. byte cipher[128];
  4787. byte plain[128];
  4788. rc2TestVector a, b, c, d, e, f, g, h, i;
  4789. rc2TestVector test_rc2[9];
  4790. int times = sizeof(test_rc2) / sizeof(rc2TestVector), j;
  4791. /* key length = 7, effective key bits = 63 */
  4792. a.input = "\x00\x00\x00\x00\x00\x00\x00\x00"
  4793. "\x00\x00\x00\x00\x00\x00\x00\x00";
  4794. a.output = "\xEB\xB7\x73\xF9\x93\x27\x8E\xFF"
  4795. "\xF0\x51\x77\x8B\x65\xDB\x13\x57";
  4796. a.key = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4797. a.iv = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4798. a.inLen = RC2_BLOCK_SIZE*2;
  4799. a.outLen = RC2_BLOCK_SIZE*2;
  4800. a.keyLen = 8;
  4801. a.effectiveKeyBits = 63;
  4802. /* key length = 8, effective key bits = 64, all 0xFF */
  4803. b.input = "\xff\xff\xff\xff\xff\xff\xff\xff"
  4804. "\xff\xff\xff\xff\xff\xff\xff\xff";
  4805. b.output = "\xA3\xA1\x12\x65\x4F\x81\xC5\xCD"
  4806. "\xB6\x94\x3E\xEA\x3E\x8B\x9D\x1F";
  4807. b.key = "\xff\xff\xff\xff\xff\xff\xff\xff";
  4808. b.iv = "\xff\xff\xff\xff\xff\xff\xff\xff";
  4809. b.inLen = RC2_BLOCK_SIZE*2;
  4810. b.outLen = RC2_BLOCK_SIZE*2;
  4811. b.keyLen = 8;
  4812. b.effectiveKeyBits = 64;
  4813. /* key length = 8, effective key bits = 64 */
  4814. c.input = "\x10\x00\x00\x00\x00\x00\x00\x01"
  4815. "\x10\x00\x00\x00\x00\x00\x00\x01";
  4816. c.output = "\xB5\x70\x14\xA2\x5F\x40\xE3\x6D"
  4817. "\x81\x99\x8D\xE0\xB5\xD5\x3A\x05";
  4818. c.key = "\x30\x00\x00\x00\x00\x00\x00\x00";
  4819. c.iv = "\x30\x00\x00\x00\x00\x00\x00\x00";
  4820. c.inLen = RC2_BLOCK_SIZE*2;
  4821. c.outLen = RC2_BLOCK_SIZE*2;
  4822. c.keyLen = 8;
  4823. c.effectiveKeyBits = 64;
  4824. /* key length = 1, effective key bits = 64 */
  4825. d.input = "\x00\x00\x00\x00\x00\x00\x00\x00"
  4826. "\x00\x00\x00\x00\x00\x00\x00\x00";
  4827. d.output = "\x61\xA8\xA2\x44\xAD\xAC\xCC\xF0"
  4828. "\x6D\x19\xE8\xF1\xFC\xE7\x38\x87";
  4829. d.key = "\x88";
  4830. d.iv = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4831. d.inLen = RC2_BLOCK_SIZE*2;
  4832. d.outLen = RC2_BLOCK_SIZE*2;
  4833. d.keyLen = 1;
  4834. d.effectiveKeyBits = 64;
  4835. /* key length = 7, effective key bits = 64 */
  4836. e.input = "\x00\x00\x00\x00\x00\x00\x00\x00"
  4837. "\x00\x00\x00\x00\x00\x00\x00\x00";
  4838. e.output = "\x6C\xCF\x43\x08\x97\x4C\x26\x7F"
  4839. "\xCC\x3C\x53\x57\x7C\xA1\xA4\x4B";
  4840. e.key = "\x88\xbc\xa9\x0e\x90\x87\x5a";
  4841. e.iv = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4842. e.inLen = RC2_BLOCK_SIZE*2;
  4843. e.outLen = RC2_BLOCK_SIZE*2;
  4844. e.keyLen = 7;
  4845. e.effectiveKeyBits = 64;
  4846. /* key length = 16, effective key bits = 64 */
  4847. f.input = "\x00\x00\x00\x00\x00\x00\x00\x00"
  4848. "\x00\x00\x00\x00\x00\x00\x00\x00";
  4849. f.output = "\x1A\x80\x7D\x27\x2B\xBE\x5D\xB1"
  4850. "\x64\xEF\xE1\xC3\xB8\xAD\xFB\xBA";
  4851. f.key = "\x88\xbc\xa9\x0e\x90\x87\x5a\x7f"
  4852. "\x0f\x79\xc3\x84\x62\x7b\xaf\xb2";
  4853. f.iv = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4854. f.inLen = RC2_BLOCK_SIZE*2;
  4855. f.outLen = RC2_BLOCK_SIZE*2;
  4856. f.keyLen = 16;
  4857. f.effectiveKeyBits = 64;
  4858. /* key length = 16, effective bits = 128 */
  4859. g.input = "\x00\x00\x00\x00\x00\x00\x00\x00"
  4860. "\x00\x00\x00\x00\x00\x00\x00\x00";
  4861. g.output = "\x22\x69\x55\x2A\xB0\xF8\x5C\xA6"
  4862. "\x53\x6E\xFD\x2D\x89\xE1\x2A\x73";
  4863. g.key = "\x88\xbc\xa9\x0e\x90\x87\x5a\x7f"
  4864. "\x0f\x79\xc3\x84\x62\x7b\xaf\xb2";
  4865. g.iv = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4866. g.inLen = RC2_BLOCK_SIZE*2;
  4867. g.outLen = RC2_BLOCK_SIZE*2;
  4868. g.keyLen = 16;
  4869. g.effectiveKeyBits = 128;
  4870. /* key length = 33, effective bits = 129 */
  4871. h.input = "\x00\x00\x00\x00\x00\x00\x00\x00"
  4872. "\x00\x00\x00\x00\x00\x00\x00\x00";
  4873. h.output = "\x5B\x78\xD3\xA4\x3D\xFF\xF1\xF1"
  4874. "\x45\x30\xA8\xD5\xC7\x7C\x46\x19";
  4875. h.key = "\x88\xbc\xa9\x0e\x90\x87\x5a\x7f"
  4876. "\x0f\x79\xc3\x84\x62\x7b\xaf\xb2"
  4877. "\x16\xf8\x0a\x6f\x85\x92\x05\x84"
  4878. "\xc4\x2f\xce\xb0\xbe\x25\x5d\xaf"
  4879. "\x1e";
  4880. h.iv = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4881. h.inLen = RC2_BLOCK_SIZE*2;
  4882. h.outLen = RC2_BLOCK_SIZE*2;
  4883. h.keyLen = 33;
  4884. h.effectiveKeyBits = 129;
  4885. /* key length = 10, effective bits = 40 */
  4886. i.input = "\x11\x22\x33\x44\x55\x66\x77\x88"
  4887. "\x99\xAA\xBB\xCC\xDD\xEE\xFF\x00"
  4888. "\x11\x22\x33\x44\x55\x66\x77\x88"
  4889. "\x99\xAA\xBB\xCC\xDD\xEE\xFF\x00";
  4890. i.output = "\x71\x2D\x11\x99\xC9\xA0\x78\x4F"
  4891. "\xCD\xF1\x1E\x3D\xFD\x21\x7E\xDB"
  4892. "\xB2\x6E\x0D\xA4\x72\xBC\x31\x51"
  4893. "\x48\xEF\x4E\x68\x3B\xDC\xCD\x7D";
  4894. i.key = "\x26\x1E\x57\x8E\xC9\x62\xBF\xB8"
  4895. "\x3E\x96";
  4896. i.iv = "\x01\x02\x03\x04\x05\x06\x07\x08";
  4897. i.inLen = RC2_BLOCK_SIZE*4;
  4898. i.outLen = RC2_BLOCK_SIZE*4;
  4899. i.keyLen = 10;
  4900. i.effectiveKeyBits = 40;
  4901. test_rc2[0] = a;
  4902. test_rc2[1] = b;
  4903. test_rc2[2] = c;
  4904. test_rc2[3] = d;
  4905. test_rc2[4] = e;
  4906. test_rc2[5] = f;
  4907. test_rc2[6] = g;
  4908. test_rc2[7] = h;
  4909. test_rc2[8] = i;
  4910. for (j = 0; j < times; ++j) {
  4911. Rc2 rc2;
  4912. XMEMSET(cipher, 0, sizeof(cipher));
  4913. XMEMSET(plain, 0, sizeof(plain));
  4914. ret = wc_Rc2SetKey(&rc2, (byte*)test_rc2[j].key, test_rc2[j].keyLen,
  4915. (byte*)test_rc2[j].iv, test_rc2[j].effectiveKeyBits);
  4916. if (ret != 0) {
  4917. return -4200;
  4918. }
  4919. ret = wc_Rc2CbcEncrypt(&rc2, cipher, (byte*)test_rc2[j].input,
  4920. test_rc2[j].inLen);
  4921. if (ret != 0) {
  4922. return -4201;
  4923. }
  4924. if (XMEMCMP(cipher, (byte*)test_rc2[j].output, test_rc2[j].outLen)) {
  4925. return -4202;
  4926. }
  4927. /* reset IV for decrypt, since overriden by encrypt operation */
  4928. ret = wc_Rc2SetIV(&rc2, (byte*)test_rc2[j].iv);
  4929. if (ret != 0) {
  4930. return -4203;
  4931. }
  4932. ret = wc_Rc2CbcDecrypt(&rc2, plain, cipher, test_rc2[j].outLen);
  4933. if (ret != 0) {
  4934. return -4204;
  4935. }
  4936. if (XMEMCMP(plain, (byte*)test_rc2[j].input, test_rc2[j].inLen)) {
  4937. return -4205;
  4938. }
  4939. }
  4940. return 0;
  4941. }
  4942. WOLFSSL_TEST_SUBROUTINE int rc2_test(void)
  4943. {
  4944. int ret = 0;
  4945. ret = rc2_ecb_test();
  4946. if (ret != 0) {
  4947. return ret;
  4948. }
  4949. return rc2_cbc_test();
  4950. }
  4951. #endif
  4952. #ifndef NO_RC4
  4953. WOLFSSL_TEST_SUBROUTINE int arc4_test(void)
  4954. {
  4955. byte cipher[16];
  4956. byte plain[16];
  4957. const char* keys[] =
  4958. {
  4959. "\x01\x23\x45\x67\x89\xab\xcd\xef",
  4960. "\x01\x23\x45\x67\x89\xab\xcd\xef",
  4961. "\x00\x00\x00\x00\x00\x00\x00\x00",
  4962. "\xef\x01\x23\x45"
  4963. };
  4964. testVector a, b, c, d;
  4965. testVector test_arc4[4];
  4966. int times = sizeof(test_arc4) / sizeof(testVector), i;
  4967. a.input = "\x01\x23\x45\x67\x89\xab\xcd\xef";
  4968. a.output = "\x75\xb7\x87\x80\x99\xe0\xc5\x96";
  4969. a.inLen = 8;
  4970. a.outLen = 8;
  4971. b.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4972. b.output = "\x74\x94\xc2\xe7\x10\x4b\x08\x79";
  4973. b.inLen = 8;
  4974. b.outLen = 8;
  4975. c.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4976. c.output = "\xde\x18\x89\x41\xa3\x37\x5d\x3a";
  4977. c.inLen = 8;
  4978. c.outLen = 8;
  4979. d.input = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";
  4980. d.output = "\xd6\xa1\x41\xa7\xec\x3c\x38\xdf\xbd\x61";
  4981. d.inLen = 10;
  4982. d.outLen = 10;
  4983. test_arc4[0] = a;
  4984. test_arc4[1] = b;
  4985. test_arc4[2] = c;
  4986. test_arc4[3] = d;
  4987. for (i = 0; i < times; ++i) {
  4988. Arc4 enc;
  4989. Arc4 dec;
  4990. int keylen = 8; /* XSTRLEN with key 0x00 not good */
  4991. if (i == 3)
  4992. keylen = 4;
  4993. if (wc_Arc4Init(&enc, HEAP_HINT, devId) != 0)
  4994. return -4400;
  4995. if (wc_Arc4Init(&dec, HEAP_HINT, devId) != 0)
  4996. return -4401;
  4997. wc_Arc4SetKey(&enc, (byte*)keys[i], keylen);
  4998. wc_Arc4SetKey(&dec, (byte*)keys[i], keylen);
  4999. wc_Arc4Process(&enc, cipher, (byte*)test_arc4[i].input,
  5000. (word32)test_arc4[i].outLen);
  5001. wc_Arc4Process(&dec, plain, cipher, (word32)test_arc4[i].outLen);
  5002. if (XMEMCMP(plain, test_arc4[i].input, test_arc4[i].outLen))
  5003. return -4402 - i;
  5004. if (XMEMCMP(cipher, test_arc4[i].output, test_arc4[i].outLen))
  5005. return -4412 - i;
  5006. wc_Arc4Free(&enc);
  5007. wc_Arc4Free(&dec);
  5008. }
  5009. return 0;
  5010. }
  5011. #endif
  5012. #ifdef HAVE_CHACHA
  5013. WOLFSSL_TEST_SUBROUTINE int chacha_test(void)
  5014. {
  5015. ChaCha enc;
  5016. ChaCha dec;
  5017. byte cipher[128];
  5018. byte plain[128];
  5019. byte sliver[64];
  5020. byte input[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  5021. word32 keySz = 32;
  5022. int ret = 0;
  5023. int i;
  5024. int times = 4;
  5025. WOLFSSL_SMALL_STACK_STATIC const byte key1[] =
  5026. {
  5027. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5028. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5029. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5030. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  5031. };
  5032. WOLFSSL_SMALL_STACK_STATIC const byte key2[] =
  5033. {
  5034. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5035. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5036. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5037. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  5038. };
  5039. WOLFSSL_SMALL_STACK_STATIC const byte key3[] =
  5040. {
  5041. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5042. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5043. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5044. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  5045. };
  5046. /* 128 bit key */
  5047. WOLFSSL_SMALL_STACK_STATIC const byte key4[] =
  5048. {
  5049. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5050. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  5051. };
  5052. const byte* keys[] = {key1, key2, key3, key4};
  5053. WOLFSSL_SMALL_STACK_STATIC const byte ivs1[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
  5054. WOLFSSL_SMALL_STACK_STATIC const byte ivs2[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
  5055. WOLFSSL_SMALL_STACK_STATIC const byte ivs3[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00};
  5056. WOLFSSL_SMALL_STACK_STATIC const byte ivs4[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
  5057. const byte* ivs[] = {ivs1, ivs2, ivs3, ivs4};
  5058. #ifndef BENCH_EMBEDDED
  5059. WOLFSSL_SMALL_STACK_STATIC const byte cipher_big_result[] = {
  5060. 0x06, 0xa6, 0x5d, 0x31, 0x21, 0x6c, 0xdb, 0x37, 0x48, 0x7c, 0x01, 0x9d,
  5061. 0x72, 0xdf, 0x0a, 0x5b, 0x64, 0x74, 0x20, 0xba, 0x9e, 0xe0, 0x26, 0x7a,
  5062. 0xbf, 0xdf, 0x83, 0x34, 0x3b, 0x4f, 0x94, 0x3f, 0x37, 0x89, 0xaf, 0x00,
  5063. 0xdf, 0x0f, 0x2e, 0x75, 0x16, 0x41, 0xf6, 0x7a, 0x86, 0x94, 0x9d, 0x32,
  5064. 0x56, 0xf0, 0x79, 0x71, 0x68, 0x6f, 0xa6, 0x6b, 0xc6, 0x59, 0x49, 0xf6,
  5065. 0x10, 0x34, 0x03, 0x03, 0x16, 0x53, 0x9a, 0x98, 0x2a, 0x46, 0xde, 0x17,
  5066. 0x06, 0x65, 0x70, 0xca, 0x0a, 0x1f, 0xab, 0x80, 0x26, 0x96, 0x3f, 0x3e,
  5067. 0x7a, 0x3c, 0xa8, 0x87, 0xbb, 0x65, 0xdd, 0x5e, 0x07, 0x7b, 0x34, 0xe0,
  5068. 0x56, 0xda, 0x32, 0x13, 0x30, 0xc9, 0x0c, 0xd7, 0xba, 0xe4, 0x1f, 0xa6,
  5069. 0x91, 0x4f, 0x72, 0x9f, 0xd9, 0x5c, 0x62, 0x7d, 0xa6, 0xc2, 0xbc, 0x87,
  5070. 0xae, 0x64, 0x11, 0x94, 0x3b, 0xbc, 0x6c, 0x23, 0xbd, 0x7d, 0x00, 0xb4,
  5071. 0x99, 0xf2, 0x68, 0xb5, 0x59, 0x70, 0x93, 0xad, 0x69, 0xd0, 0xb1, 0x28,
  5072. 0x70, 0x92, 0xeb, 0xec, 0x39, 0x80, 0x82, 0xde, 0x44, 0xe2, 0x8a, 0x26,
  5073. 0xb3, 0xe9, 0x45, 0xcf, 0x83, 0x76, 0x9f, 0x6a, 0xa0, 0x46, 0x4a, 0x3d,
  5074. 0x26, 0x56, 0xaf, 0x49, 0x41, 0x26, 0x1b, 0x6a, 0x41, 0x37, 0x65, 0x91,
  5075. 0x72, 0xc4, 0xe7, 0x3c, 0x17, 0x31, 0xae, 0x2e, 0x2b, 0x31, 0x45, 0xe4,
  5076. 0x93, 0xd3, 0x10, 0xaa, 0xc5, 0x62, 0xd5, 0x11, 0x4b, 0x57, 0x1d, 0xad,
  5077. 0x48, 0x06, 0xd0, 0x0d, 0x98, 0xa5, 0xc6, 0x5b, 0xd0, 0x9e, 0x22, 0xc0,
  5078. 0x00, 0x32, 0x5a, 0xf5, 0x1c, 0x89, 0x6d, 0x54, 0x97, 0x55, 0x6b, 0x46,
  5079. 0xc5, 0xc7, 0xc4, 0x48, 0x9c, 0xbf, 0x47, 0xdc, 0x03, 0xc4, 0x1b, 0xcb,
  5080. 0x65, 0xa6, 0x91, 0x9d, 0x6d, 0xf1, 0xb0, 0x7a, 0x4d, 0x3b, 0x03, 0x95,
  5081. 0xf4, 0x8b, 0x0b, 0xae, 0x39, 0xff, 0x3f, 0xf6, 0xc0, 0x14, 0x18, 0x8a,
  5082. 0xe5, 0x19, 0xbd, 0xc1, 0xb4, 0x05, 0x4e, 0x29, 0x2f, 0x0b, 0x33, 0x76,
  5083. 0x28, 0x16, 0xa4, 0xa6, 0x93, 0x04, 0xb5, 0x55, 0x6b, 0x89, 0x3d, 0xa5,
  5084. 0x0f, 0xd3, 0xad, 0xfa, 0xd9, 0xfd, 0x05, 0x5d, 0x48, 0x94, 0x25, 0x5a,
  5085. 0x2c, 0x9a, 0x94, 0x80, 0xb0, 0xe7, 0xcb, 0x4d, 0x77, 0xbf, 0xca, 0xd8,
  5086. 0x55, 0x48, 0xbd, 0x66, 0xb1, 0x85, 0x81, 0xb1, 0x37, 0x79, 0xab, 0x52,
  5087. 0x08, 0x14, 0x12, 0xac, 0xcd, 0x45, 0x4d, 0x53, 0x6b, 0xca, 0x96, 0xc7,
  5088. 0x3b, 0x2f, 0x73, 0xb1, 0x5a, 0x23, 0xbd, 0x65, 0xd5, 0xea, 0x17, 0xb3,
  5089. 0xdc, 0xa1, 0x17, 0x1b, 0x2d, 0xb3, 0x9c, 0xd0, 0xdb, 0x41, 0x77, 0xef,
  5090. 0x93, 0x20, 0x52, 0x3e, 0x9d, 0xf5, 0xbf, 0x33, 0xf7, 0x52, 0xc1, 0x90,
  5091. 0xa0, 0x15, 0x17, 0xce, 0xf7, 0xf7, 0xd0, 0x3a, 0x3b, 0xd1, 0x72, 0x56,
  5092. 0x31, 0x81, 0xae, 0x60, 0xab, 0x40, 0xc1, 0xd1, 0x28, 0x77, 0x53, 0xac,
  5093. 0x9f, 0x11, 0x0a, 0x88, 0x36, 0x4b, 0xda, 0x57, 0xa7, 0x28, 0x5c, 0x85,
  5094. 0xd3, 0x85, 0x9b, 0x79, 0xad, 0x05, 0x1c, 0x37, 0x14, 0x5e, 0x0d, 0xd0,
  5095. 0x23, 0x03, 0x42, 0x1d, 0x48, 0x5d, 0xc5, 0x3c, 0x5a, 0x08, 0xa9, 0x0d,
  5096. 0x6e, 0x82, 0x7c, 0x2e, 0x3c, 0x41, 0xcc, 0x96, 0x8e, 0xad, 0xee, 0x2a,
  5097. 0x61, 0x0b, 0x16, 0x0f, 0xa9, 0x24, 0x40, 0x85, 0xbc, 0x9f, 0x28, 0x8d,
  5098. 0xe6, 0x68, 0x4d, 0x8f, 0x30, 0x48, 0xd9, 0x73, 0x73, 0x6c, 0x9a, 0x7f,
  5099. 0x67, 0xf7, 0xde, 0x4c, 0x0a, 0x8b, 0xe4, 0xb3, 0x08, 0x2a, 0x52, 0xda,
  5100. 0x54, 0xee, 0xcd, 0xb5, 0x62, 0x4a, 0x26, 0x20, 0xfb, 0x40, 0xbb, 0x39,
  5101. 0x3a, 0x0f, 0x09, 0xe8, 0x00, 0xd1, 0x24, 0x97, 0x60, 0xe9, 0x83, 0x83,
  5102. 0xfe, 0x9f, 0x9c, 0x15, 0xcf, 0x69, 0x03, 0x9f, 0x03, 0xe1, 0xe8, 0x6e,
  5103. 0xbd, 0x87, 0x58, 0x68, 0xee, 0xec, 0xd8, 0x29, 0x46, 0x23, 0x49, 0x92,
  5104. 0x72, 0x95, 0x5b, 0x49, 0xca, 0xe0, 0x45, 0x59, 0xb2, 0xca, 0xf4, 0xfc,
  5105. 0xb7, 0x59, 0x37, 0x49, 0x28, 0xbc, 0xf3, 0xd7, 0x61, 0xbc, 0x4b, 0xf3,
  5106. 0xa9, 0x4b, 0x2f, 0x05, 0xa8, 0x01, 0xa5, 0xdc, 0x00, 0x6e, 0x01, 0xb6,
  5107. 0x45, 0x3c, 0xd5, 0x49, 0x7d, 0x5c, 0x25, 0xe8, 0x31, 0x87, 0xb2, 0xb9,
  5108. 0xbf, 0xb3, 0x01, 0x62, 0x0c, 0xd0, 0x48, 0x77, 0xa2, 0x34, 0x0f, 0x16,
  5109. 0x22, 0x28, 0xee, 0x54, 0x08, 0x93, 0x3b, 0xe4, 0xde, 0x7e, 0x63, 0xf7,
  5110. 0x97, 0x16, 0x5d, 0x71, 0x58, 0xc2, 0x2e, 0xf2, 0x36, 0xa6, 0x12, 0x65,
  5111. 0x94, 0x17, 0xac, 0x66, 0x23, 0x7e, 0xc6, 0x72, 0x79, 0x24, 0xce, 0x8f,
  5112. 0x55, 0x19, 0x97, 0x44, 0xfc, 0x55, 0xec, 0x85, 0x26, 0x27, 0xdb, 0x38,
  5113. 0xb1, 0x42, 0x0a, 0xdd, 0x05, 0x99, 0x28, 0xeb, 0x03, 0x6c, 0x9a, 0xe9,
  5114. 0x17, 0xf6, 0x2c, 0xb0, 0xfe, 0xe7, 0xa4, 0xa7, 0x31, 0xda, 0x4d, 0xb0,
  5115. 0x29, 0xdb, 0xdd, 0x8d, 0x12, 0x13, 0x9c, 0xb4, 0xcc, 0x83, 0x97, 0xfb,
  5116. 0x1a, 0xdc, 0x08, 0xd6, 0x30, 0x62, 0xe8, 0xeb, 0x8b, 0x61, 0xcb, 0x1d,
  5117. 0x06, 0xe3, 0xa5, 0x4d, 0x35, 0xdb, 0x59, 0xa8, 0x2d, 0x87, 0x27, 0x44,
  5118. 0x6f, 0xc0, 0x38, 0x97, 0xe4, 0x85, 0x00, 0x02, 0x09, 0xf6, 0x69, 0x3a,
  5119. 0xcf, 0x08, 0x1b, 0x21, 0xbb, 0x79, 0xb1, 0xa1, 0x34, 0x09, 0xe0, 0x80,
  5120. 0xca, 0xb0, 0x78, 0x8a, 0x11, 0x97, 0xd4, 0x07, 0xbe, 0x1b, 0x6a, 0x5d,
  5121. 0xdb, 0xd6, 0x1f, 0x76, 0x6b, 0x16, 0xf0, 0x58, 0x84, 0x5f, 0x59, 0xce,
  5122. 0x62, 0x34, 0xc3, 0xdf, 0x94, 0xb8, 0x2f, 0x84, 0x68, 0xf0, 0xb8, 0x51,
  5123. 0xd9, 0x6d, 0x8e, 0x4a, 0x1d, 0xe6, 0x5c, 0xd8, 0x86, 0x25, 0xe3, 0x24,
  5124. 0xfd, 0x21, 0x61, 0x13, 0x48, 0x3e, 0xf6, 0x7d, 0xa6, 0x71, 0x9b, 0xd2,
  5125. 0x6e, 0xe6, 0xd2, 0x08, 0x94, 0x62, 0x6c, 0x98, 0xfe, 0x2f, 0x9c, 0x88,
  5126. 0x7e, 0x78, 0x15, 0x02, 0x00, 0xf0, 0xba, 0x24, 0x91, 0xf2, 0xdc, 0x47,
  5127. 0x51, 0x4d, 0x15, 0x5e, 0x91, 0x5f, 0x57, 0x5b, 0x1d, 0x35, 0x24, 0x45,
  5128. 0x75, 0x9b, 0x88, 0x75, 0xf1, 0x2f, 0x85, 0xe7, 0x89, 0xd1, 0x01, 0xb4,
  5129. 0xc8, 0x18, 0xb7, 0x97, 0xef, 0x4b, 0x90, 0xf4, 0xbf, 0x10, 0x27, 0x3c,
  5130. 0x60, 0xff, 0xc4, 0x94, 0x20, 0x2f, 0x93, 0x4b, 0x4d, 0xe3, 0x80, 0xf7,
  5131. 0x2c, 0x71, 0xd9, 0xe3, 0x68, 0xb4, 0x77, 0x2b, 0xc7, 0x0d, 0x39, 0x92,
  5132. 0xef, 0x91, 0x0d, 0xb2, 0x11, 0x50, 0x0e, 0xe8, 0xad, 0x3b, 0xf6, 0xb5,
  5133. 0xc6, 0x14, 0x4d, 0x33, 0x53, 0xa7, 0x60, 0x15, 0xc7, 0x27, 0x51, 0xdc,
  5134. 0x54, 0x29, 0xa7, 0x0d, 0x6a, 0x7b, 0x72, 0x13, 0xad, 0x7d, 0x41, 0x19,
  5135. 0x4e, 0x42, 0x49, 0xcc, 0x42, 0xe4, 0xbd, 0x99, 0x13, 0xd9, 0x7f, 0xf3,
  5136. 0x38, 0xa4, 0xb6, 0x33, 0xed, 0x07, 0x48, 0x7e, 0x8e, 0x82, 0xfe, 0x3a,
  5137. 0x9d, 0x75, 0x93, 0xba, 0x25, 0x4e, 0x37, 0x3c, 0x0c, 0xd5, 0x69, 0xa9,
  5138. 0x2d, 0x9e, 0xfd, 0xe8, 0xbb, 0xf5, 0x0c, 0xe2, 0x86, 0xb9, 0x5e, 0x6f,
  5139. 0x28, 0xe4, 0x19, 0xb3, 0x0b, 0xa4, 0x86, 0xd7, 0x24, 0xd0, 0xb8, 0x89,
  5140. 0x7b, 0x76, 0xec, 0x05, 0x10, 0x5b, 0x68, 0xe9, 0x58, 0x66, 0xa3, 0xc5,
  5141. 0xb6, 0x63, 0x20, 0x0e, 0x0e, 0xea, 0x3d, 0x61, 0x5e, 0xda, 0x3d, 0x3c,
  5142. 0xf9, 0xfd, 0xed, 0xa9, 0xdb, 0x52, 0x94, 0x8a, 0x00, 0xca, 0x3c, 0x8d,
  5143. 0x66, 0x8f, 0xb0, 0xf0, 0x5a, 0xca, 0x3f, 0x63, 0x71, 0xbf, 0xca, 0x99,
  5144. 0x37, 0x9b, 0x75, 0x97, 0x89, 0x10, 0x6e, 0xcf, 0xf2, 0xf5, 0xe3, 0xd5,
  5145. 0x45, 0x9b, 0xad, 0x10, 0x71, 0x6c, 0x5f, 0x6f, 0x7f, 0x22, 0x77, 0x18,
  5146. 0x2f, 0xf9, 0x99, 0xc5, 0x69, 0x58, 0x03, 0x12, 0x86, 0x82, 0x3e, 0xbf,
  5147. 0xc2, 0x12, 0x35, 0x43, 0xa3, 0xd9, 0x18, 0x4f, 0x41, 0x11, 0x6b, 0xf3,
  5148. 0x67, 0xaf, 0x3d, 0x78, 0xe4, 0x22, 0x2d, 0xb3, 0x48, 0x43, 0x31, 0x1d,
  5149. 0xef, 0xa8, 0xba, 0x49, 0x8e, 0xa9, 0xa7, 0xb6, 0x18, 0x77, 0x84, 0xca,
  5150. 0xbd, 0xa2, 0x02, 0x1b, 0x6a, 0xf8, 0x5f, 0xda, 0xff, 0xcf, 0x01, 0x6a,
  5151. 0x86, 0x69, 0xa9, 0xe9, 0xcb, 0x60, 0x1e, 0x15, 0xdc, 0x8f, 0x5d, 0x39,
  5152. 0xb5, 0xce, 0x55, 0x5f, 0x47, 0x97, 0xb1, 0x19, 0x6e, 0x21, 0xd6, 0x13,
  5153. 0x39, 0xb2, 0x24, 0xe0, 0x62, 0x82, 0x9f, 0xed, 0x12, 0x81, 0xed, 0xee,
  5154. 0xab, 0xd0, 0x2f, 0x19, 0x89, 0x3f, 0x57, 0x2e, 0xc2, 0xe2, 0x67, 0xe8,
  5155. 0xae, 0x03, 0x56, 0xba, 0xd4, 0xd0, 0xa4, 0x89, 0x03, 0x06, 0x5b, 0xcc,
  5156. 0xf2, 0x22, 0xb8, 0x0e, 0x76, 0x79, 0x4a, 0x42, 0x1d, 0x37, 0x51, 0x5a,
  5157. 0xaa, 0x46, 0x6c, 0x2a, 0xdd, 0x66, 0xfe, 0xc6, 0x68, 0xc3, 0x38, 0xa2,
  5158. 0xae, 0x5b, 0x98, 0x24, 0x5d, 0x43, 0x05, 0x82, 0x38, 0x12, 0xd3, 0xd1,
  5159. 0x75, 0x2d, 0x4f, 0x61, 0xbd, 0xb9, 0x10, 0x87, 0x44, 0x2a, 0x78, 0x07,
  5160. 0xff, 0xf4, 0x0f, 0xa1, 0xf3, 0x68, 0x9f, 0xbe, 0xae, 0xa2, 0x91, 0xf0,
  5161. 0xc7, 0x55, 0x7a, 0x52, 0xd5, 0xa3, 0x8d, 0x6f, 0xe4, 0x90, 0x5c, 0xf3,
  5162. 0x5f, 0xce, 0x3d, 0x23, 0xf9, 0x8e, 0xae, 0x14, 0xfb, 0x82, 0x9a, 0xa3,
  5163. 0x04, 0x5f, 0xbf, 0xad, 0x3e, 0xf2, 0x97, 0x0a, 0x60, 0x40, 0x70, 0x19,
  5164. 0x72, 0xad, 0x66, 0xfb, 0x78, 0x1b, 0x84, 0x6c, 0x98, 0xbc, 0x8c, 0xf8,
  5165. 0x4f, 0xcb, 0xb5, 0xf6, 0xaf, 0x7a, 0xb7, 0x93, 0xef, 0x67, 0x48, 0x02,
  5166. 0x2c, 0xcb, 0xe6, 0x77, 0x0f, 0x7b, 0xc1, 0xee, 0xc5, 0xb6, 0x2d, 0x7e,
  5167. 0x62, 0xa0, 0xc0, 0xa7, 0xa5, 0x80, 0x31, 0x92, 0x50, 0xa1, 0x28, 0x22,
  5168. 0x95, 0x03, 0x17, 0xd1, 0x0f, 0xf6, 0x08, 0xe5, 0xec
  5169. };
  5170. #define CHACHA_BIG_TEST_SIZE 1305
  5171. #if !defined(WOLFSSL_SMALL_STACK) || defined(WOLFSSL_NO_MALLOC)
  5172. byte cipher_big[CHACHA_BIG_TEST_SIZE] = {0};
  5173. byte plain_big[CHACHA_BIG_TEST_SIZE] = {0};
  5174. byte input_big[CHACHA_BIG_TEST_SIZE] = {0};
  5175. #else
  5176. byte* cipher_big;
  5177. byte* plain_big;
  5178. byte* input_big;
  5179. #endif /* WOLFSSL_SMALL_STACK && !WOLFSSL_NO_MALLOC */
  5180. int block_size;
  5181. #endif /* BENCH_EMBEDDED */
  5182. byte a[] = {0x76,0xb8,0xe0,0xad,0xa0,0xf1,0x3d,0x90};
  5183. byte b[] = {0x45,0x40,0xf0,0x5a,0x9f,0x1f,0xb2,0x96};
  5184. byte c[] = {0xde,0x9c,0xba,0x7b,0xf3,0xd6,0x9e,0xf5};
  5185. byte d[] = {0x89,0x67,0x09,0x52,0x60,0x83,0x64,0xfd};
  5186. byte* test_chacha[4];
  5187. test_chacha[0] = a;
  5188. test_chacha[1] = b;
  5189. test_chacha[2] = c;
  5190. test_chacha[3] = d;
  5191. #ifndef BENCH_EMBEDDED
  5192. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  5193. cipher_big = (byte*)XMALLOC(CHACHA_BIG_TEST_SIZE, HEAP_HINT,
  5194. DYNAMIC_TYPE_TMP_BUFFER);
  5195. if (cipher_big == NULL) {
  5196. return MEMORY_E;
  5197. }
  5198. plain_big = (byte*)XMALLOC(CHACHA_BIG_TEST_SIZE, HEAP_HINT,
  5199. DYNAMIC_TYPE_TMP_BUFFER);
  5200. if (plain_big == NULL) {
  5201. return MEMORY_E;
  5202. }
  5203. input_big = (byte*)XMALLOC(CHACHA_BIG_TEST_SIZE, HEAP_HINT,
  5204. DYNAMIC_TYPE_TMP_BUFFER);
  5205. if (input_big == NULL) {
  5206. return MEMORY_E;
  5207. }
  5208. XMEMSET(cipher_big, 0, CHACHA_BIG_TEST_SIZE);
  5209. XMEMSET(plain_big, 0, CHACHA_BIG_TEST_SIZE);
  5210. XMEMSET(input_big, 0, CHACHA_BIG_TEST_SIZE);
  5211. #endif /* WOLFSSL_SMALL_STACK && !WOLFSSL_NO_MALLOC */
  5212. #endif /* BENCH_EMBEDDED */
  5213. for (i = 0; i < times; ++i) {
  5214. if (i < 3) {
  5215. keySz = 32;
  5216. }
  5217. else {
  5218. keySz = 16;
  5219. }
  5220. XMEMCPY(plain, keys[i], keySz);
  5221. XMEMSET(cipher, 0, 32);
  5222. XMEMCPY(cipher + 4, ivs[i], 8);
  5223. ret |= wc_Chacha_SetKey(&enc, keys[i], keySz);
  5224. ret |= wc_Chacha_SetKey(&dec, keys[i], keySz);
  5225. if (ret != 0)
  5226. return ret;
  5227. ret |= wc_Chacha_SetIV(&enc, cipher, 0);
  5228. ret |= wc_Chacha_SetIV(&dec, cipher, 0);
  5229. if (ret != 0)
  5230. return ret;
  5231. XMEMCPY(plain, input, 8);
  5232. ret |= wc_Chacha_Process(&enc, cipher, plain, (word32)8);
  5233. ret |= wc_Chacha_Process(&dec, plain, cipher, (word32)8);
  5234. if (ret != 0)
  5235. return ret;
  5236. if (XMEMCMP(test_chacha[i], cipher, 8))
  5237. return -4700 - i;
  5238. if (XMEMCMP(plain, input, 8))
  5239. return -4710 - i;
  5240. }
  5241. /* test of starting at a different counter
  5242. encrypts all of the information and decrypts starting at 2nd chunk */
  5243. XMEMSET(plain, 0, sizeof(plain));
  5244. XMEMSET(sliver, 1, sizeof(sliver)); /* set as 1's to not match plain */
  5245. XMEMSET(cipher, 0, sizeof(cipher));
  5246. XMEMCPY(cipher + 4, ivs[0], 8);
  5247. ret |= wc_Chacha_SetKey(&enc, keys[0], keySz);
  5248. ret |= wc_Chacha_SetKey(&dec, keys[0], keySz);
  5249. if (ret != 0)
  5250. return ret;
  5251. ret |= wc_Chacha_SetIV(&enc, cipher, 0);
  5252. ret |= wc_Chacha_SetIV(&dec, cipher, 1);
  5253. if (ret != 0)
  5254. return ret;
  5255. ret |= wc_Chacha_Process(&enc, cipher, plain, sizeof(plain));
  5256. ret |= wc_Chacha_Process(&dec, sliver, cipher + 64, sizeof(sliver));
  5257. if (ret != 0)
  5258. return ret;
  5259. if (XMEMCMP(plain + 64, sliver, 64))
  5260. return -4720;
  5261. #ifndef BENCH_EMBEDDED
  5262. /* test of encrypting more data */
  5263. keySz = 32;
  5264. ret |= wc_Chacha_SetKey(&enc, keys[0], keySz);
  5265. ret |= wc_Chacha_SetKey(&dec, keys[0], keySz);
  5266. if (ret != 0)
  5267. return ret;
  5268. ret |= wc_Chacha_SetIV(&enc, ivs[2], 0);
  5269. ret |= wc_Chacha_SetIV(&dec, ivs[2], 0);
  5270. if (ret != 0)
  5271. return ret;
  5272. ret |= wc_Chacha_Process(&enc, cipher_big, plain_big, CHACHA_BIG_TEST_SIZE);
  5273. ret |= wc_Chacha_Process(&dec, plain_big, cipher_big,
  5274. CHACHA_BIG_TEST_SIZE);
  5275. if (ret != 0)
  5276. return ret;
  5277. if (XMEMCMP(plain_big, input_big, CHACHA_BIG_TEST_SIZE))
  5278. return -4721;
  5279. if (XMEMCMP(cipher_big, cipher_big_result, CHACHA_BIG_TEST_SIZE))
  5280. return -4722;
  5281. for (i = 0; i < 18; ++i) {
  5282. /* this will test all paths
  5283. * block sizes: 1 3 7 15 31 63 127 255 511 (i = 0- 8)
  5284. * 2 4 8 16 32 64 128 256 512 (i = 9-17)
  5285. */
  5286. block_size = (2 << (i%9)) - (i<9?1:0);
  5287. keySz = 32;
  5288. ret |= wc_Chacha_SetKey(&enc, keys[0], keySz);
  5289. ret |= wc_Chacha_SetKey(&dec, keys[0], keySz);
  5290. if (ret != 0)
  5291. return ret;
  5292. ret |= wc_Chacha_SetIV(&enc, ivs[2], 0);
  5293. ret |= wc_Chacha_SetIV(&dec, ivs[2], 0);
  5294. if (ret != 0)
  5295. return ret;
  5296. ret |= wc_Chacha_Process(&enc, cipher_big, plain_big , block_size);
  5297. ret |= wc_Chacha_Process(&dec, plain_big , cipher_big, block_size);
  5298. if (ret != 0)
  5299. return ret;
  5300. if (XMEMCMP(plain_big, input_big, block_size))
  5301. return -4740-i*2;
  5302. if (XMEMCMP(cipher_big, cipher_big_result, block_size))
  5303. return -4741-i*2;
  5304. }
  5305. /* Streaming test */
  5306. for (i = 1; i <= (int)CHACHA_CHUNK_BYTES + 1; i++) {
  5307. int j, rem;
  5308. ret = wc_Chacha_SetKey(&enc, keys[0], keySz);
  5309. if (ret != 0)
  5310. return -4725;
  5311. ret = wc_Chacha_SetKey(&dec, keys[0], keySz);
  5312. if (ret != 0)
  5313. return -4726;
  5314. ret = wc_Chacha_SetIV(&enc, ivs[2], 0);
  5315. if (ret != 0)
  5316. return -4727;
  5317. ret = wc_Chacha_SetIV(&dec, ivs[2], 0);
  5318. if (ret != 0)
  5319. return -4728;
  5320. for (j = 0; j < CHACHA_BIG_TEST_SIZE - i; j+= i) {
  5321. ret = wc_Chacha_Process(&enc, cipher_big + j, plain_big + j, i);
  5322. if (ret != 0)
  5323. return -4729;
  5324. ret = wc_Chacha_Process(&dec, plain_big + j, cipher_big + j, i);
  5325. if (ret != 0)
  5326. return -4730;
  5327. }
  5328. rem = CHACHA_BIG_TEST_SIZE - j;
  5329. ret = wc_Chacha_Process(&enc, cipher_big + j, plain_big + j, rem);
  5330. if (ret != 0)
  5331. return -4731;
  5332. ret = wc_Chacha_Process(&dec, plain_big + j, cipher_big + j, rem);
  5333. if (ret != 0)
  5334. return -4732;
  5335. if (XMEMCMP(plain_big, input_big, CHACHA_BIG_TEST_SIZE))
  5336. return -4733;
  5337. if (XMEMCMP(cipher_big, cipher_big_result, CHACHA_BIG_TEST_SIZE))
  5338. return -4734;
  5339. }
  5340. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  5341. XFREE(cipher_big, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  5342. XFREE(plain_big, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  5343. XFREE(input_big, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  5344. #endif /* WOLFSSL_SMALL_STACK && !WOLFSSL_NO_MALLOC */
  5345. #endif /* BENCH_EMBEDDED */
  5346. return 0;
  5347. }
  5348. #endif /* HAVE_CHACHA */
  5349. #ifdef HAVE_POLY1305
  5350. WOLFSSL_TEST_SUBROUTINE int poly1305_test(void)
  5351. {
  5352. int ret = 0;
  5353. int i;
  5354. byte tag[16];
  5355. Poly1305 enc;
  5356. WOLFSSL_SMALL_STACK_STATIC const byte msg1[] =
  5357. {
  5358. 0x43,0x72,0x79,0x70,0x74,0x6f,0x67,0x72,
  5359. 0x61,0x70,0x68,0x69,0x63,0x20,0x46,0x6f,
  5360. 0x72,0x75,0x6d,0x20,0x52,0x65,0x73,0x65,
  5361. 0x61,0x72,0x63,0x68,0x20,0x47,0x72,0x6f,
  5362. 0x75,0x70
  5363. };
  5364. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] =
  5365. {
  5366. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x77,0x6f,0x72,
  5367. 0x6c,0x64,0x21
  5368. };
  5369. WOLFSSL_SMALL_STACK_STATIC const byte msg3[] =
  5370. {
  5371. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5372. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5373. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5374. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  5375. };
  5376. WOLFSSL_SMALL_STACK_STATIC const byte msg4[] =
  5377. {
  5378. 0xd3,0x1a,0x8d,0x34,0x64,0x8e,0x60,0xdb,
  5379. 0x7b,0x86,0xaf,0xbc,0x53,0xef,0x7e,0xc2,
  5380. 0xa4,0xad,0xed,0x51,0x29,0x6e,0x08,0xfe,
  5381. 0xa9,0xe2,0xb5,0xa7,0x36,0xee,0x62,0xd6,
  5382. 0x3d,0xbe,0xa4,0x5e,0x8c,0xa9,0x67,0x12,
  5383. 0x82,0xfa,0xfb,0x69,0xda,0x92,0x72,0x8b,
  5384. 0x1a,0x71,0xde,0x0a,0x9e,0x06,0x0b,0x29,
  5385. 0x05,0xd6,0xa5,0xb6,0x7e,0xcd,0x3b,0x36,
  5386. 0x92,0xdd,0xbd,0x7f,0x2d,0x77,0x8b,0x8c,
  5387. 0x98,0x03,0xae,0xe3,0x28,0x09,0x1b,0x58,
  5388. 0xfa,0xb3,0x24,0xe4,0xfa,0xd6,0x75,0x94,
  5389. 0x55,0x85,0x80,0x8b,0x48,0x31,0xd7,0xbc,
  5390. 0x3f,0xf4,0xde,0xf0,0x8e,0x4b,0x7a,0x9d,
  5391. 0xe5,0x76,0xd2,0x65,0x86,0xce,0xc6,0x4b,
  5392. 0x61,0x16
  5393. };
  5394. WOLFSSL_SMALL_STACK_STATIC const byte msg5[] =
  5395. {
  5396. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  5397. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  5398. };
  5399. WOLFSSL_SMALL_STACK_STATIC const byte msg6[] =
  5400. {
  5401. 0xd3,0x1a,0x8d,0x34,0x64,0x8e,0x60,0xdb,
  5402. 0x7b,0x86,0xaf,0xbc,0x53,0xef,0x7e,0xc2,
  5403. 0xa4,0xad,0xed,0x51,0x29,0x6e,0x08,0xfe,
  5404. 0xa9,0xe2,0xb5,0xa7,0x36,0xee,0x62,0xd6,
  5405. 0x3d,0xbe,0xa4,0x5e,0x8c,0xa9,0x67,0x12,
  5406. 0x82,0xfa,0xfb,0x69,0xda,0x92,0x72,0x8b,
  5407. 0xfa,0xb3,0x24,0xe4,0xfa,0xd6,0x75,0x94,
  5408. 0x1a,0x71,0xde,0x0a,0x9e,0x06,0x0b,0x29,
  5409. 0xa9,0xe2,0xb5,0xa7,0x36,0xee,0x62,0xd6,
  5410. 0x3d,0xbe,0xa4,0x5e,0x8c,0xa9,0x67,0x12,
  5411. 0xfa,0xb3,0x24,0xe4,0xfa,0xd6,0x75,0x94,
  5412. 0x05,0xd6,0xa5,0xb6,0x7e,0xcd,0x3b,0x36,
  5413. 0x92,0xdd,0xbd,0x7f,0x2d,0x77,0x8b,0x8c,
  5414. 0x7b,0x86,0xaf,0xbc,0x53,0xef,0x7e,0xc2,
  5415. 0x98,0x03,0xae,0xe3,0x28,0x09,0x1b,0x58,
  5416. 0xfa,0xb3,0x24,0xe4,0xfa,0xd6,0x75,0x94,
  5417. 0x55,0x85,0x80,0x8b,0x48,0x31,0xd7,0xbc,
  5418. 0x3f,0xf4,0xde,0xf0,0x8e,0x4b,0x7a,0x9d,
  5419. 0xe5,0x76,0xd2,0x65,0x86,0xce,0xc6,0x4b,
  5420. 0x61,0x16
  5421. };
  5422. byte additional[] =
  5423. {
  5424. 0x50,0x51,0x52,0x53,0xc0,0xc1,0xc2,0xc3,
  5425. 0xc4,0xc5,0xc6,0xc7
  5426. };
  5427. WOLFSSL_SMALL_STACK_STATIC const byte correct0[] =
  5428. {
  5429. 0x01,0x03,0x80,0x8a,0xfb,0x0d,0xb2,0xfd,
  5430. 0x4a,0xbf,0xf6,0xaf,0x41,0x49,0xf5,0x1b
  5431. };
  5432. WOLFSSL_SMALL_STACK_STATIC const byte correct1[] =
  5433. {
  5434. 0xa8,0x06,0x1d,0xc1,0x30,0x51,0x36,0xc6,
  5435. 0xc2,0x2b,0x8b,0xaf,0x0c,0x01,0x27,0xa9
  5436. };
  5437. WOLFSSL_SMALL_STACK_STATIC const byte correct2[] =
  5438. {
  5439. 0xa6,0xf7,0x45,0x00,0x8f,0x81,0xc9,0x16,
  5440. 0xa2,0x0d,0xcc,0x74,0xee,0xf2,0xb2,0xf0
  5441. };
  5442. WOLFSSL_SMALL_STACK_STATIC const byte correct3[] =
  5443. {
  5444. 0x49,0xec,0x78,0x09,0x0e,0x48,0x1e,0xc6,
  5445. 0xc2,0x6b,0x33,0xb9,0x1c,0xcc,0x03,0x07
  5446. };
  5447. WOLFSSL_SMALL_STACK_STATIC const byte correct4[] =
  5448. {
  5449. 0x1a,0xe1,0x0b,0x59,0x4f,0x09,0xe2,0x6a,
  5450. 0x7e,0x90,0x2e,0xcb,0xd0,0x60,0x06,0x91
  5451. };
  5452. WOLFSSL_SMALL_STACK_STATIC const byte correct5[] =
  5453. {
  5454. 0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5455. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5456. };
  5457. WOLFSSL_SMALL_STACK_STATIC const byte correct6[] =
  5458. {
  5459. 0xea,0x11,0x5c,0x4f,0xd0,0xc0,0x10,0xae,
  5460. 0xf7,0xdf,0xda,0x77,0xa2,0xe9,0xaf,0xca
  5461. };
  5462. WOLFSSL_SMALL_STACK_STATIC const byte key[] = {
  5463. 0x85,0xd6,0xbe,0x78,0x57,0x55,0x6d,0x33,
  5464. 0x7f,0x44,0x52,0xfe,0x42,0xd5,0x06,0xa8,
  5465. 0x01,0x03,0x80,0x8a,0xfb,0x0d,0xb2,0xfd,
  5466. 0x4a,0xbf,0xf6,0xaf,0x41,0x49,0xf5,0x1b
  5467. };
  5468. WOLFSSL_SMALL_STACK_STATIC const byte key2[] = {
  5469. 0x74,0x68,0x69,0x73,0x20,0x69,0x73,0x20,
  5470. 0x33,0x32,0x2d,0x62,0x79,0x74,0x65,0x20,
  5471. 0x6b,0x65,0x79,0x20,0x66,0x6f,0x72,0x20,
  5472. 0x50,0x6f,0x6c,0x79,0x31,0x33,0x30,0x35
  5473. };
  5474. WOLFSSL_SMALL_STACK_STATIC const byte key4[] = {
  5475. 0x7b,0xac,0x2b,0x25,0x2d,0xb4,0x47,0xaf,
  5476. 0x09,0xb6,0x7a,0x55,0xa4,0xe9,0x55,0x84,
  5477. 0x0a,0xe1,0xd6,0x73,0x10,0x75,0xd9,0xeb,
  5478. 0x2a,0x93,0x75,0x78,0x3e,0xd5,0x53,0xff
  5479. };
  5480. WOLFSSL_SMALL_STACK_STATIC const byte key5[] = {
  5481. 0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5482. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5483. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5484. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  5485. };
  5486. const byte* msgs[] = {NULL, msg1, msg2, msg3, msg5, msg6};
  5487. word32 szm[] = {0, sizeof(msg1), sizeof(msg2),
  5488. sizeof(msg3), sizeof(msg5), sizeof(msg6)};
  5489. const byte* keys[] = {key, key, key2, key2, key5, key};
  5490. const byte* tests[] = {correct0, correct1, correct2, correct3, correct5,
  5491. correct6};
  5492. for (i = 0; i < 6; i++) {
  5493. ret = wc_Poly1305SetKey(&enc, keys[i], 32);
  5494. if (ret != 0)
  5495. return -4800 - i;
  5496. ret = wc_Poly1305Update(&enc, msgs[i], szm[i]);
  5497. if (ret != 0)
  5498. return -4810 - i;
  5499. ret = wc_Poly1305Final(&enc, tag);
  5500. if (ret != 0)
  5501. return -4820 - i;
  5502. if (XMEMCMP(tag, tests[i], sizeof(tag)))
  5503. return -4830 - i;
  5504. }
  5505. /* Check TLS MAC function from 2.8.2 https://tools.ietf.org/html/rfc7539 */
  5506. XMEMSET(tag, 0, sizeof(tag));
  5507. ret = wc_Poly1305SetKey(&enc, key4, sizeof(key4));
  5508. if (ret != 0)
  5509. return -4840;
  5510. ret = wc_Poly1305_MAC(&enc, additional, sizeof(additional),
  5511. (byte*)msg4, sizeof(msg4), tag, sizeof(tag));
  5512. if (ret != 0)
  5513. return -4841;
  5514. if (XMEMCMP(tag, correct4, sizeof(tag)))
  5515. return -4842;
  5516. /* Check fail of TLS MAC function if altering additional data */
  5517. XMEMSET(tag, 0, sizeof(tag));
  5518. additional[0]++;
  5519. ret = wc_Poly1305_MAC(&enc, additional, sizeof(additional),
  5520. (byte*)msg4, sizeof(msg4), tag, sizeof(tag));
  5521. if (ret != 0)
  5522. return -4843;
  5523. if (XMEMCMP(tag, correct4, sizeof(tag)) == 0)
  5524. return -4844;
  5525. return 0;
  5526. }
  5527. #endif /* HAVE_POLY1305 */
  5528. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  5529. WOLFSSL_TEST_SUBROUTINE int chacha20_poly1305_aead_test(void)
  5530. {
  5531. /* Test #1 from Section 2.8.2 of draft-irtf-cfrg-chacha20-poly1305-10 */
  5532. /* https://tools.ietf.org/html/draft-irtf-cfrg-chacha20-poly1305-10 */
  5533. WOLFSSL_SMALL_STACK_STATIC const byte key1[] = {
  5534. 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
  5535. 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  5536. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
  5537. 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f
  5538. };
  5539. WOLFSSL_SMALL_STACK_STATIC const byte plaintext1[] = {
  5540. 0x4c, 0x61, 0x64, 0x69, 0x65, 0x73, 0x20, 0x61,
  5541. 0x6e, 0x64, 0x20, 0x47, 0x65, 0x6e, 0x74, 0x6c,
  5542. 0x65, 0x6d, 0x65, 0x6e, 0x20, 0x6f, 0x66, 0x20,
  5543. 0x74, 0x68, 0x65, 0x20, 0x63, 0x6c, 0x61, 0x73,
  5544. 0x73, 0x20, 0x6f, 0x66, 0x20, 0x27, 0x39, 0x39,
  5545. 0x3a, 0x20, 0x49, 0x66, 0x20, 0x49, 0x20, 0x63,
  5546. 0x6f, 0x75, 0x6c, 0x64, 0x20, 0x6f, 0x66, 0x66,
  5547. 0x65, 0x72, 0x20, 0x79, 0x6f, 0x75, 0x20, 0x6f,
  5548. 0x6e, 0x6c, 0x79, 0x20, 0x6f, 0x6e, 0x65, 0x20,
  5549. 0x74, 0x69, 0x70, 0x20, 0x66, 0x6f, 0x72, 0x20,
  5550. 0x74, 0x68, 0x65, 0x20, 0x66, 0x75, 0x74, 0x75,
  5551. 0x72, 0x65, 0x2c, 0x20, 0x73, 0x75, 0x6e, 0x73,
  5552. 0x63, 0x72, 0x65, 0x65, 0x6e, 0x20, 0x77, 0x6f,
  5553. 0x75, 0x6c, 0x64, 0x20, 0x62, 0x65, 0x20, 0x69,
  5554. 0x74, 0x2e
  5555. };
  5556. WOLFSSL_SMALL_STACK_STATIC const byte iv1[] = {
  5557. 0x07, 0x00, 0x00, 0x00, 0x40, 0x41, 0x42, 0x43,
  5558. 0x44, 0x45, 0x46, 0x47
  5559. };
  5560. WOLFSSL_SMALL_STACK_STATIC const byte aad1[] = { /* additional data */
  5561. 0x50, 0x51, 0x52, 0x53, 0xc0, 0xc1, 0xc2, 0xc3,
  5562. 0xc4, 0xc5, 0xc6, 0xc7
  5563. };
  5564. WOLFSSL_SMALL_STACK_STATIC const byte cipher1[] = { /* expected output from operation */
  5565. 0xd3, 0x1a, 0x8d, 0x34, 0x64, 0x8e, 0x60, 0xdb,
  5566. 0x7b, 0x86, 0xaf, 0xbc, 0x53, 0xef, 0x7e, 0xc2,
  5567. 0xa4, 0xad, 0xed, 0x51, 0x29, 0x6e, 0x08, 0xfe,
  5568. 0xa9, 0xe2, 0xb5, 0xa7, 0x36, 0xee, 0x62, 0xd6,
  5569. 0x3d, 0xbe, 0xa4, 0x5e, 0x8c, 0xa9, 0x67, 0x12,
  5570. 0x82, 0xfa, 0xfb, 0x69, 0xda, 0x92, 0x72, 0x8b,
  5571. 0x1a, 0x71, 0xde, 0x0a, 0x9e, 0x06, 0x0b, 0x29,
  5572. 0x05, 0xd6, 0xa5, 0xb6, 0x7e, 0xcd, 0x3b, 0x36,
  5573. 0x92, 0xdd, 0xbd, 0x7f, 0x2d, 0x77, 0x8b, 0x8c,
  5574. 0x98, 0x03, 0xae, 0xe3, 0x28, 0x09, 0x1b, 0x58,
  5575. 0xfa, 0xb3, 0x24, 0xe4, 0xfa, 0xd6, 0x75, 0x94,
  5576. 0x55, 0x85, 0x80, 0x8b, 0x48, 0x31, 0xd7, 0xbc,
  5577. 0x3f, 0xf4, 0xde, 0xf0, 0x8e, 0x4b, 0x7a, 0x9d,
  5578. 0xe5, 0x76, 0xd2, 0x65, 0x86, 0xce, 0xc6, 0x4b,
  5579. 0x61, 0x16
  5580. };
  5581. WOLFSSL_SMALL_STACK_STATIC const byte authTag1[] = { /* expected output from operation */
  5582. 0x1a, 0xe1, 0x0b, 0x59, 0x4f, 0x09, 0xe2, 0x6a,
  5583. 0x7e, 0x90, 0x2e, 0xcb, 0xd0, 0x60, 0x06, 0x91
  5584. };
  5585. /* Test #2 from Appendix A.2 in draft-irtf-cfrg-chacha20-poly1305-10 */
  5586. /* https://tools.ietf.org/html/draft-irtf-cfrg-chacha20-poly1305-10 */
  5587. WOLFSSL_SMALL_STACK_STATIC const byte key2[] = {
  5588. 0x1c, 0x92, 0x40, 0xa5, 0xeb, 0x55, 0xd3, 0x8a,
  5589. 0xf3, 0x33, 0x88, 0x86, 0x04, 0xf6, 0xb5, 0xf0,
  5590. 0x47, 0x39, 0x17, 0xc1, 0x40, 0x2b, 0x80, 0x09,
  5591. 0x9d, 0xca, 0x5c, 0xbc, 0x20, 0x70, 0x75, 0xc0
  5592. };
  5593. WOLFSSL_SMALL_STACK_STATIC const byte plaintext2[] = {
  5594. 0x49, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x65, 0x74,
  5595. 0x2d, 0x44, 0x72, 0x61, 0x66, 0x74, 0x73, 0x20,
  5596. 0x61, 0x72, 0x65, 0x20, 0x64, 0x72, 0x61, 0x66,
  5597. 0x74, 0x20, 0x64, 0x6f, 0x63, 0x75, 0x6d, 0x65,
  5598. 0x6e, 0x74, 0x73, 0x20, 0x76, 0x61, 0x6c, 0x69,
  5599. 0x64, 0x20, 0x66, 0x6f, 0x72, 0x20, 0x61, 0x20,
  5600. 0x6d, 0x61, 0x78, 0x69, 0x6d, 0x75, 0x6d, 0x20,
  5601. 0x6f, 0x66, 0x20, 0x73, 0x69, 0x78, 0x20, 0x6d,
  5602. 0x6f, 0x6e, 0x74, 0x68, 0x73, 0x20, 0x61, 0x6e,
  5603. 0x64, 0x20, 0x6d, 0x61, 0x79, 0x20, 0x62, 0x65,
  5604. 0x20, 0x75, 0x70, 0x64, 0x61, 0x74, 0x65, 0x64,
  5605. 0x2c, 0x20, 0x72, 0x65, 0x70, 0x6c, 0x61, 0x63,
  5606. 0x65, 0x64, 0x2c, 0x20, 0x6f, 0x72, 0x20, 0x6f,
  5607. 0x62, 0x73, 0x6f, 0x6c, 0x65, 0x74, 0x65, 0x64,
  5608. 0x20, 0x62, 0x79, 0x20, 0x6f, 0x74, 0x68, 0x65,
  5609. 0x72, 0x20, 0x64, 0x6f, 0x63, 0x75, 0x6d, 0x65,
  5610. 0x6e, 0x74, 0x73, 0x20, 0x61, 0x74, 0x20, 0x61,
  5611. 0x6e, 0x79, 0x20, 0x74, 0x69, 0x6d, 0x65, 0x2e,
  5612. 0x20, 0x49, 0x74, 0x20, 0x69, 0x73, 0x20, 0x69,
  5613. 0x6e, 0x61, 0x70, 0x70, 0x72, 0x6f, 0x70, 0x72,
  5614. 0x69, 0x61, 0x74, 0x65, 0x20, 0x74, 0x6f, 0x20,
  5615. 0x75, 0x73, 0x65, 0x20, 0x49, 0x6e, 0x74, 0x65,
  5616. 0x72, 0x6e, 0x65, 0x74, 0x2d, 0x44, 0x72, 0x61,
  5617. 0x66, 0x74, 0x73, 0x20, 0x61, 0x73, 0x20, 0x72,
  5618. 0x65, 0x66, 0x65, 0x72, 0x65, 0x6e, 0x63, 0x65,
  5619. 0x20, 0x6d, 0x61, 0x74, 0x65, 0x72, 0x69, 0x61,
  5620. 0x6c, 0x20, 0x6f, 0x72, 0x20, 0x74, 0x6f, 0x20,
  5621. 0x63, 0x69, 0x74, 0x65, 0x20, 0x74, 0x68, 0x65,
  5622. 0x6d, 0x20, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20,
  5623. 0x74, 0x68, 0x61, 0x6e, 0x20, 0x61, 0x73, 0x20,
  5624. 0x2f, 0xe2, 0x80, 0x9c, 0x77, 0x6f, 0x72, 0x6b,
  5625. 0x20, 0x69, 0x6e, 0x20, 0x70, 0x72, 0x6f, 0x67,
  5626. 0x72, 0x65, 0x73, 0x73, 0x2e, 0x2f, 0xe2, 0x80,
  5627. 0x9d
  5628. };
  5629. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] = {
  5630. 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04,
  5631. 0x05, 0x06, 0x07, 0x08
  5632. };
  5633. WOLFSSL_SMALL_STACK_STATIC const byte aad2[] = { /* additional data */
  5634. 0xf3, 0x33, 0x88, 0x86, 0x00, 0x00, 0x00, 0x00,
  5635. 0x00, 0x00, 0x4e, 0x91
  5636. };
  5637. WOLFSSL_SMALL_STACK_STATIC const byte cipher2[] = { /* expected output from operation */
  5638. 0x64, 0xa0, 0x86, 0x15, 0x75, 0x86, 0x1a, 0xf4,
  5639. 0x60, 0xf0, 0x62, 0xc7, 0x9b, 0xe6, 0x43, 0xbd,
  5640. 0x5e, 0x80, 0x5c, 0xfd, 0x34, 0x5c, 0xf3, 0x89,
  5641. 0xf1, 0x08, 0x67, 0x0a, 0xc7, 0x6c, 0x8c, 0xb2,
  5642. 0x4c, 0x6c, 0xfc, 0x18, 0x75, 0x5d, 0x43, 0xee,
  5643. 0xa0, 0x9e, 0xe9, 0x4e, 0x38, 0x2d, 0x26, 0xb0,
  5644. 0xbd, 0xb7, 0xb7, 0x3c, 0x32, 0x1b, 0x01, 0x00,
  5645. 0xd4, 0xf0, 0x3b, 0x7f, 0x35, 0x58, 0x94, 0xcf,
  5646. 0x33, 0x2f, 0x83, 0x0e, 0x71, 0x0b, 0x97, 0xce,
  5647. 0x98, 0xc8, 0xa8, 0x4a, 0xbd, 0x0b, 0x94, 0x81,
  5648. 0x14, 0xad, 0x17, 0x6e, 0x00, 0x8d, 0x33, 0xbd,
  5649. 0x60, 0xf9, 0x82, 0xb1, 0xff, 0x37, 0xc8, 0x55,
  5650. 0x97, 0x97, 0xa0, 0x6e, 0xf4, 0xf0, 0xef, 0x61,
  5651. 0xc1, 0x86, 0x32, 0x4e, 0x2b, 0x35, 0x06, 0x38,
  5652. 0x36, 0x06, 0x90, 0x7b, 0x6a, 0x7c, 0x02, 0xb0,
  5653. 0xf9, 0xf6, 0x15, 0x7b, 0x53, 0xc8, 0x67, 0xe4,
  5654. 0xb9, 0x16, 0x6c, 0x76, 0x7b, 0x80, 0x4d, 0x46,
  5655. 0xa5, 0x9b, 0x52, 0x16, 0xcd, 0xe7, 0xa4, 0xe9,
  5656. 0x90, 0x40, 0xc5, 0xa4, 0x04, 0x33, 0x22, 0x5e,
  5657. 0xe2, 0x82, 0xa1, 0xb0, 0xa0, 0x6c, 0x52, 0x3e,
  5658. 0xaf, 0x45, 0x34, 0xd7, 0xf8, 0x3f, 0xa1, 0x15,
  5659. 0x5b, 0x00, 0x47, 0x71, 0x8c, 0xbc, 0x54, 0x6a,
  5660. 0x0d, 0x07, 0x2b, 0x04, 0xb3, 0x56, 0x4e, 0xea,
  5661. 0x1b, 0x42, 0x22, 0x73, 0xf5, 0x48, 0x27, 0x1a,
  5662. 0x0b, 0xb2, 0x31, 0x60, 0x53, 0xfa, 0x76, 0x99,
  5663. 0x19, 0x55, 0xeb, 0xd6, 0x31, 0x59, 0x43, 0x4e,
  5664. 0xce, 0xbb, 0x4e, 0x46, 0x6d, 0xae, 0x5a, 0x10,
  5665. 0x73, 0xa6, 0x72, 0x76, 0x27, 0x09, 0x7a, 0x10,
  5666. 0x49, 0xe6, 0x17, 0xd9, 0x1d, 0x36, 0x10, 0x94,
  5667. 0xfa, 0x68, 0xf0, 0xff, 0x77, 0x98, 0x71, 0x30,
  5668. 0x30, 0x5b, 0xea, 0xba, 0x2e, 0xda, 0x04, 0xdf,
  5669. 0x99, 0x7b, 0x71, 0x4d, 0x6c, 0x6f, 0x2c, 0x29,
  5670. 0xa6, 0xad, 0x5c, 0xb4, 0x02, 0x2b, 0x02, 0x70,
  5671. 0x9b
  5672. };
  5673. WOLFSSL_SMALL_STACK_STATIC const byte authTag2[] = { /* expected output from operation */
  5674. 0xee, 0xad, 0x9d, 0x67, 0x89, 0x0c, 0xbb, 0x22,
  5675. 0x39, 0x23, 0x36, 0xfe, 0xa1, 0x85, 0x1f, 0x38
  5676. };
  5677. byte generatedCiphertext[265]; /* max plaintext2/cipher2 */
  5678. byte generatedPlaintext[265]; /* max plaintext2/cipher2 */
  5679. byte generatedAuthTag[CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE];
  5680. int err;
  5681. ChaChaPoly_Aead aead;
  5682. #if !defined(USE_INTEL_CHACHA_SPEEDUP) && !defined(WOLFSSL_ARMASM)
  5683. #define TEST_SMALL_CHACHA_CHUNKS 32
  5684. #else
  5685. #define TEST_SMALL_CHACHA_CHUNKS 64
  5686. #endif
  5687. #ifdef TEST_SMALL_CHACHA_CHUNKS
  5688. word32 testLen;
  5689. #endif
  5690. XMEMSET(generatedCiphertext, 0, sizeof(generatedCiphertext));
  5691. XMEMSET(generatedAuthTag, 0, sizeof(generatedAuthTag));
  5692. XMEMSET(generatedPlaintext, 0, sizeof(generatedPlaintext));
  5693. /* Parameter Validation testing */
  5694. /* Encrypt */
  5695. err = wc_ChaCha20Poly1305_Encrypt(NULL, iv1, aad1, sizeof(aad1), plaintext1,
  5696. sizeof(plaintext1), generatedCiphertext, generatedAuthTag);
  5697. if (err != BAD_FUNC_ARG)
  5698. return -4900;
  5699. err = wc_ChaCha20Poly1305_Encrypt(key1, NULL, aad1, sizeof(aad1),
  5700. plaintext1, sizeof(plaintext1), generatedCiphertext,
  5701. generatedAuthTag);
  5702. if (err != BAD_FUNC_ARG)
  5703. return -4901;
  5704. err = wc_ChaCha20Poly1305_Encrypt(key1, iv1, aad1, sizeof(aad1), NULL,
  5705. sizeof(plaintext1), generatedCiphertext, generatedAuthTag);
  5706. if (err != BAD_FUNC_ARG)
  5707. return -4902;
  5708. err = wc_ChaCha20Poly1305_Encrypt(key1, iv1, aad1, sizeof(aad1), plaintext1,
  5709. sizeof(plaintext1), NULL, generatedAuthTag);
  5710. if (err != BAD_FUNC_ARG)
  5711. return -4903;
  5712. err = wc_ChaCha20Poly1305_Encrypt(key1, iv1, aad1, sizeof(aad1), plaintext1,
  5713. sizeof(plaintext1), generatedCiphertext, NULL);
  5714. if (err != BAD_FUNC_ARG)
  5715. return -4904;
  5716. err = wc_ChaCha20Poly1305_Encrypt(key1, iv1, aad1, sizeof(aad1), NULL,
  5717. sizeof(plaintext1), generatedCiphertext, generatedAuthTag);
  5718. if (err != BAD_FUNC_ARG)
  5719. return -4905;
  5720. /* Decrypt */
  5721. err = wc_ChaCha20Poly1305_Decrypt(NULL, iv2, aad2, sizeof(aad2), cipher2,
  5722. sizeof(cipher2), authTag2, generatedPlaintext);
  5723. if (err != BAD_FUNC_ARG)
  5724. return -4906;
  5725. err = wc_ChaCha20Poly1305_Decrypt(key2, NULL, aad2, sizeof(aad2), cipher2,
  5726. sizeof(cipher2), authTag2, generatedPlaintext);
  5727. if (err != BAD_FUNC_ARG)
  5728. return -4907;
  5729. err = wc_ChaCha20Poly1305_Decrypt(key2, iv2, aad2, sizeof(aad2), NULL,
  5730. sizeof(cipher2), authTag2, generatedPlaintext);
  5731. if (err != BAD_FUNC_ARG)
  5732. return -4908;
  5733. err = wc_ChaCha20Poly1305_Decrypt(key2, iv2, aad2, sizeof(aad2), cipher2,
  5734. sizeof(cipher2), NULL, generatedPlaintext);
  5735. if (err != BAD_FUNC_ARG)
  5736. return -4909;
  5737. err = wc_ChaCha20Poly1305_Decrypt(key2, iv2, aad2, sizeof(aad2), cipher2,
  5738. sizeof(cipher2), authTag2, NULL);
  5739. if (err != BAD_FUNC_ARG)
  5740. return -4910;
  5741. err = wc_ChaCha20Poly1305_Decrypt(key2, iv2, aad2, sizeof(aad2), NULL,
  5742. sizeof(cipher2), authTag2, generatedPlaintext);
  5743. if (err != BAD_FUNC_ARG)
  5744. return -4911;
  5745. /* Test #1 */
  5746. err = wc_ChaCha20Poly1305_Encrypt(key1, iv1,
  5747. aad1, sizeof(aad1),
  5748. plaintext1, sizeof(plaintext1),
  5749. generatedCiphertext, generatedAuthTag);
  5750. if (err) {
  5751. return err;
  5752. }
  5753. /* -- Check the ciphertext and authtag */
  5754. if (XMEMCMP(generatedCiphertext, cipher1, sizeof(cipher1))) {
  5755. return -4912;
  5756. }
  5757. if (XMEMCMP(generatedAuthTag, authTag1, sizeof(authTag1))) {
  5758. return -4913;
  5759. }
  5760. /* -- Verify decryption works */
  5761. err = wc_ChaCha20Poly1305_Decrypt(key1, iv1,
  5762. aad1, sizeof(aad1),
  5763. cipher1, sizeof(cipher1),
  5764. authTag1, generatedPlaintext);
  5765. if (err) {
  5766. return err;
  5767. }
  5768. if (XMEMCMP(generatedPlaintext, plaintext1, sizeof(plaintext1))) {
  5769. return -4914;
  5770. }
  5771. XMEMSET(generatedCiphertext, 0, sizeof(generatedCiphertext));
  5772. XMEMSET(generatedAuthTag, 0, sizeof(generatedAuthTag));
  5773. XMEMSET(generatedPlaintext, 0, sizeof(generatedPlaintext));
  5774. /* Test #2 */
  5775. err = wc_ChaCha20Poly1305_Encrypt(key2, iv2,
  5776. aad2, sizeof(aad2),
  5777. plaintext2, sizeof(plaintext2),
  5778. generatedCiphertext, generatedAuthTag);
  5779. if (err) {
  5780. return err;
  5781. }
  5782. /* -- Check the ciphertext and authtag */
  5783. if (XMEMCMP(generatedCiphertext, cipher2, sizeof(cipher2))) {
  5784. return -4915;
  5785. }
  5786. if (XMEMCMP(generatedAuthTag, authTag2, sizeof(authTag2))) {
  5787. return -4916;
  5788. }
  5789. /* -- Verify decryption works */
  5790. err = wc_ChaCha20Poly1305_Decrypt(key2, iv2,
  5791. aad2, sizeof(aad2),
  5792. cipher2, sizeof(cipher2),
  5793. authTag2, generatedPlaintext);
  5794. if (err) {
  5795. return err;
  5796. }
  5797. if (XMEMCMP(generatedPlaintext, plaintext2, sizeof(plaintext2))) {
  5798. return -4917;
  5799. }
  5800. /* AEAD init/update/final - bad argument tests */
  5801. err = wc_ChaCha20Poly1305_Init(NULL, key1, iv1,
  5802. CHACHA20_POLY1305_AEAD_DECRYPT);
  5803. if (err != BAD_FUNC_ARG)
  5804. return -4918;
  5805. err = wc_ChaCha20Poly1305_Init(&aead, NULL, iv1,
  5806. CHACHA20_POLY1305_AEAD_DECRYPT);
  5807. if (err != BAD_FUNC_ARG)
  5808. return -4919;
  5809. err = wc_ChaCha20Poly1305_Init(&aead, key1, NULL,
  5810. CHACHA20_POLY1305_AEAD_DECRYPT);
  5811. if (err != BAD_FUNC_ARG)
  5812. return -4920;
  5813. err = wc_ChaCha20Poly1305_UpdateAad(NULL, aad1, sizeof(aad1));
  5814. if (err != BAD_FUNC_ARG)
  5815. return -4921;
  5816. err = wc_ChaCha20Poly1305_UpdateAad(&aead, NULL, sizeof(aad1));
  5817. if (err != BAD_FUNC_ARG)
  5818. return -4922;
  5819. err = wc_ChaCha20Poly1305_UpdateData(NULL, generatedPlaintext,
  5820. generatedPlaintext, sizeof(plaintext1));
  5821. if (err != BAD_FUNC_ARG)
  5822. return -4923;
  5823. err = wc_ChaCha20Poly1305_UpdateData(&aead, generatedPlaintext, NULL,
  5824. sizeof(plaintext1));
  5825. if (err != BAD_FUNC_ARG)
  5826. return -4924;
  5827. err = wc_ChaCha20Poly1305_UpdateData(&aead, NULL, generatedPlaintext,
  5828. sizeof(plaintext1));
  5829. if (err != BAD_FUNC_ARG)
  5830. return -4925;
  5831. err = wc_ChaCha20Poly1305_Final(NULL, generatedAuthTag);
  5832. if (err != BAD_FUNC_ARG)
  5833. return -4926;
  5834. err = wc_ChaCha20Poly1305_Final(&aead, NULL);
  5835. if (err != BAD_FUNC_ARG)
  5836. return -4927;
  5837. /* AEAD init/update/final - bad state tests */
  5838. /* clear struct - make valgrind happy to resolve
  5839. "Conditional jump or move depends on uninitialised value(s)".
  5840. The enum is "int" size and aead.state is "byte" */
  5841. /* The wc_ChaCha20Poly1305_Init function does this normally */
  5842. XMEMSET(&aead, 0, sizeof(aead));
  5843. aead.state = CHACHA20_POLY1305_STATE_INIT;
  5844. err = wc_ChaCha20Poly1305_UpdateAad(&aead, aad1, sizeof(aad1));
  5845. if (err != BAD_STATE_E)
  5846. return -4928;
  5847. aead.state = CHACHA20_POLY1305_STATE_DATA;
  5848. err = wc_ChaCha20Poly1305_UpdateAad(&aead, aad1, sizeof(aad1));
  5849. if (err != BAD_STATE_E)
  5850. return -4929;
  5851. aead.state = CHACHA20_POLY1305_STATE_INIT;
  5852. err = wc_ChaCha20Poly1305_UpdateData(&aead, generatedPlaintext,
  5853. generatedPlaintext, sizeof(plaintext1));
  5854. if (err != BAD_STATE_E)
  5855. return -4930;
  5856. aead.state = CHACHA20_POLY1305_STATE_INIT;
  5857. err = wc_ChaCha20Poly1305_Final(&aead, generatedAuthTag);
  5858. if (err != BAD_STATE_E)
  5859. return -4931;
  5860. aead.state = CHACHA20_POLY1305_STATE_READY;
  5861. err = wc_ChaCha20Poly1305_Final(&aead, generatedAuthTag);
  5862. if (err != BAD_STATE_E)
  5863. return -4932;
  5864. XMEMSET(generatedCiphertext, 0, sizeof(generatedCiphertext));
  5865. XMEMSET(generatedAuthTag, 0, sizeof(generatedAuthTag));
  5866. XMEMSET(generatedPlaintext, 0, sizeof(generatedPlaintext));
  5867. /* Test 1 - Encrypt */
  5868. err = wc_ChaCha20Poly1305_Init(&aead, key1, iv1,
  5869. CHACHA20_POLY1305_AEAD_ENCRYPT);
  5870. if (err != 0)
  5871. return -4933;
  5872. err = wc_ChaCha20Poly1305_UpdateAad(&aead, aad1, sizeof(aad1));
  5873. if (err != 0)
  5874. return -4934;
  5875. #ifdef TEST_SMALL_CHACHA_CHUNKS
  5876. /* test doing data in smaller chunks */
  5877. for (testLen=0; testLen<sizeof(plaintext1); ) {
  5878. word32 dataLen = sizeof(plaintext1) - testLen;
  5879. if (dataLen > TEST_SMALL_CHACHA_CHUNKS)
  5880. dataLen = TEST_SMALL_CHACHA_CHUNKS;
  5881. err = wc_ChaCha20Poly1305_UpdateData(&aead, &plaintext1[testLen],
  5882. &generatedCiphertext[testLen], dataLen);
  5883. if (err != 0)
  5884. return -4935;
  5885. testLen += dataLen;
  5886. }
  5887. #else
  5888. err = wc_ChaCha20Poly1305_UpdateData(&aead, plaintext1,
  5889. generatedCiphertext, sizeof(plaintext1));
  5890. #endif
  5891. err = wc_ChaCha20Poly1305_Final(&aead, generatedAuthTag);
  5892. if (err != 0)
  5893. return -4936;
  5894. err = wc_ChaCha20Poly1305_CheckTag(generatedAuthTag, authTag1);
  5895. if (err != 0)
  5896. return -4937;
  5897. if (XMEMCMP(generatedCiphertext, cipher1, sizeof(cipher1))) {
  5898. return -4938;
  5899. }
  5900. /* Test 1 - Decrypt */
  5901. err = wc_ChaCha20Poly1305_Init(&aead, key1, iv1,
  5902. CHACHA20_POLY1305_AEAD_DECRYPT);
  5903. if (err != 0)
  5904. return -4939;
  5905. err = wc_ChaCha20Poly1305_UpdateAad(&aead, aad1, sizeof(aad1));
  5906. if (err != 0)
  5907. return -4940;
  5908. #ifdef TEST_SMALL_CHACHA_CHUNKS
  5909. /* test doing data in smaller chunks */
  5910. for (testLen=0; testLen<sizeof(plaintext1); ) {
  5911. word32 dataLen = sizeof(plaintext1) - testLen;
  5912. if (dataLen > TEST_SMALL_CHACHA_CHUNKS)
  5913. dataLen = TEST_SMALL_CHACHA_CHUNKS;
  5914. err = wc_ChaCha20Poly1305_UpdateData(&aead,
  5915. &generatedCiphertext[testLen], &generatedPlaintext[testLen],
  5916. dataLen);
  5917. if (err != 0)
  5918. return -4941;
  5919. testLen += dataLen;
  5920. }
  5921. #else
  5922. err = wc_ChaCha20Poly1305_UpdateData(&aead, generatedCiphertext,
  5923. generatedPlaintext, sizeof(cipher1));
  5924. #endif
  5925. err = wc_ChaCha20Poly1305_Final(&aead, generatedAuthTag);
  5926. if (err != 0)
  5927. return -4942;
  5928. err = wc_ChaCha20Poly1305_CheckTag(generatedAuthTag, authTag1);
  5929. if (err != 0)
  5930. return -4943;
  5931. if (XMEMCMP(generatedPlaintext, plaintext1, sizeof(plaintext1))) {
  5932. return -4944;
  5933. }
  5934. XMEMSET(generatedCiphertext, 0, sizeof(generatedCiphertext));
  5935. XMEMSET(generatedAuthTag, 0, sizeof(generatedAuthTag));
  5936. XMEMSET(generatedPlaintext, 0, sizeof(generatedPlaintext));
  5937. /* Test 2 - Encrypt */
  5938. err = wc_ChaCha20Poly1305_Init(&aead, key2, iv2,
  5939. CHACHA20_POLY1305_AEAD_ENCRYPT);
  5940. if (err != 0)
  5941. return -4945;
  5942. err = wc_ChaCha20Poly1305_UpdateAad(&aead, aad2, sizeof(aad2));
  5943. if (err != 0)
  5944. return -4946;
  5945. #ifdef TEST_SMALL_CHACHA_CHUNKS
  5946. /* test doing data in smaller chunks */
  5947. for (testLen=0; testLen<sizeof(plaintext2); ) {
  5948. word32 dataLen = sizeof(plaintext2) - testLen;
  5949. if (dataLen > TEST_SMALL_CHACHA_CHUNKS)
  5950. dataLen = TEST_SMALL_CHACHA_CHUNKS;
  5951. err = wc_ChaCha20Poly1305_UpdateData(&aead, &plaintext2[testLen],
  5952. &generatedCiphertext[testLen], dataLen);
  5953. if (err != 0)
  5954. return -4947;
  5955. testLen += dataLen;
  5956. }
  5957. #else
  5958. err = wc_ChaCha20Poly1305_UpdateData(&aead, plaintext2, generatedCiphertext,
  5959. sizeof(plaintext2));
  5960. #endif
  5961. err = wc_ChaCha20Poly1305_Final(&aead, generatedAuthTag);
  5962. if (err != 0)
  5963. return -4948;
  5964. err = wc_ChaCha20Poly1305_CheckTag(generatedAuthTag, authTag2);
  5965. if (err != 0)
  5966. return -4949;
  5967. if (XMEMCMP(generatedCiphertext, cipher2, sizeof(cipher2))) {
  5968. return -4950;
  5969. }
  5970. /* Test 2 - Decrypt */
  5971. err = wc_ChaCha20Poly1305_Init(&aead, key2, iv2,
  5972. CHACHA20_POLY1305_AEAD_DECRYPT);
  5973. if (err != 0)
  5974. return -4951;
  5975. err = wc_ChaCha20Poly1305_UpdateAad(&aead, aad2, sizeof(aad2));
  5976. if (err != 0)
  5977. return -4952;
  5978. #ifdef TEST_SMALL_CHACHA_CHUNKS
  5979. /* test doing data in smaller chunks */
  5980. for (testLen=0; testLen<sizeof(plaintext2); ) {
  5981. word32 dataLen = sizeof(plaintext2) - testLen;
  5982. if (dataLen > TEST_SMALL_CHACHA_CHUNKS)
  5983. dataLen = TEST_SMALL_CHACHA_CHUNKS;
  5984. err = wc_ChaCha20Poly1305_UpdateData(&aead,
  5985. &generatedCiphertext[testLen], &generatedPlaintext[testLen],
  5986. dataLen);
  5987. if (err != 0)
  5988. return -4953;
  5989. testLen += dataLen;
  5990. }
  5991. #else
  5992. err = wc_ChaCha20Poly1305_UpdateData(&aead, generatedCiphertext,
  5993. generatedPlaintext, sizeof(cipher2));
  5994. #endif
  5995. err = wc_ChaCha20Poly1305_Final(&aead, generatedAuthTag);
  5996. if (err != 0)
  5997. return -4954;
  5998. err = wc_ChaCha20Poly1305_CheckTag(generatedAuthTag, authTag2);
  5999. if (err != 0)
  6000. return -4955;
  6001. if (XMEMCMP(generatedPlaintext, plaintext2, sizeof(plaintext2))) {
  6002. return -4956;
  6003. }
  6004. return err;
  6005. }
  6006. #endif /* HAVE_CHACHA && HAVE_POLY1305 */
  6007. #ifndef NO_DES3
  6008. WOLFSSL_TEST_SUBROUTINE int des_test(void)
  6009. {
  6010. WOLFSSL_SMALL_STACK_STATIC const byte vector[] = { /* "now is the time for all " w/o trailing 0 */
  6011. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  6012. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  6013. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  6014. };
  6015. byte plain[24];
  6016. byte cipher[24];
  6017. Des enc;
  6018. Des dec;
  6019. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  6020. {
  6021. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
  6022. };
  6023. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  6024. {
  6025. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef
  6026. };
  6027. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  6028. {
  6029. 0x8b,0x7c,0x52,0xb0,0x01,0x2b,0x6c,0xb8,
  6030. 0x4f,0x0f,0xeb,0xf3,0xfb,0x5f,0x86,0x73,
  6031. 0x15,0x85,0xb3,0x22,0x4b,0x86,0x2b,0x4b
  6032. };
  6033. int ret;
  6034. ret = wc_Des_SetKey(&enc, key, iv, DES_ENCRYPTION);
  6035. if (ret != 0)
  6036. return -5000;
  6037. ret = wc_Des_CbcEncrypt(&enc, cipher, vector, sizeof(vector));
  6038. if (ret != 0)
  6039. return -5001;
  6040. ret = wc_Des_SetKey(&dec, key, iv, DES_DECRYPTION);
  6041. if (ret != 0)
  6042. return -5002;
  6043. ret = wc_Des_CbcDecrypt(&dec, plain, cipher, sizeof(cipher));
  6044. if (ret != 0)
  6045. return -5003;
  6046. if (XMEMCMP(plain, vector, sizeof(plain)))
  6047. return -5004;
  6048. if (XMEMCMP(cipher, verify, sizeof(cipher)))
  6049. return -5005;
  6050. ret = wc_Des_CbcEncryptWithKey(cipher, vector, sizeof(vector), key, iv);
  6051. if (ret != 0)
  6052. return -5006;
  6053. #if defined(WOLFSSL_ENCRYPTED_KEYS) && !defined(NO_SHA)
  6054. {
  6055. EncryptedInfo info;
  6056. XMEMSET(&info, 0, sizeof(EncryptedInfo));
  6057. XMEMCPY(info.iv, iv, sizeof(iv));
  6058. info.ivSz = sizeof(iv);
  6059. info.keySz = sizeof(key);
  6060. info.cipherType = WC_CIPHER_DES;
  6061. ret = wc_BufferKeyEncrypt(&info, cipher, sizeof(cipher), key,
  6062. sizeof(key), WC_HASH_TYPE_SHA);
  6063. if (ret != 0)
  6064. return -5007;
  6065. /* Test invalid info ptr */
  6066. ret = wc_BufferKeyEncrypt(NULL, cipher, sizeof(cipher), key,
  6067. sizeof(key), WC_HASH_TYPE_SHA);
  6068. if (ret != BAD_FUNC_ARG)
  6069. return -5008;
  6070. #ifndef NO_PWDBASED
  6071. /* Test invalid hash type - only applies to wc_PBKDF1 call */
  6072. ret = wc_BufferKeyEncrypt(&info, cipher, sizeof(cipher), key,
  6073. sizeof(key), WC_HASH_TYPE_NONE);
  6074. if (ret == 0)
  6075. return -5009;
  6076. #endif /* !NO_PWDBASED */
  6077. }
  6078. #endif
  6079. return 0;
  6080. }
  6081. #endif /* !NO_DES3 */
  6082. #ifndef NO_DES3
  6083. WOLFSSL_TEST_SUBROUTINE int des3_test(void)
  6084. {
  6085. WOLFSSL_SMALL_STACK_STATIC const byte vector[] = { /* "Now is the time for all " w/o trailing 0 */
  6086. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  6087. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  6088. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  6089. };
  6090. byte plain[24];
  6091. byte cipher[24];
  6092. Des3 enc;
  6093. Des3 dec;
  6094. WOLFSSL_SMALL_STACK_STATIC const byte key3[] =
  6095. {
  6096. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  6097. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  6098. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  6099. };
  6100. WOLFSSL_SMALL_STACK_STATIC const byte iv3[] =
  6101. {
  6102. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  6103. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  6104. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  6105. };
  6106. WOLFSSL_SMALL_STACK_STATIC const byte verify3[] =
  6107. {
  6108. 0x43,0xa0,0x29,0x7e,0xd1,0x84,0xf8,0x0e,
  6109. 0x89,0x64,0x84,0x32,0x12,0xd5,0x08,0x98,
  6110. 0x18,0x94,0x15,0x74,0x87,0x12,0x7d,0xb0
  6111. };
  6112. int ret;
  6113. if (wc_Des3Init(&enc, HEAP_HINT, devId) != 0)
  6114. return -5100;
  6115. if (wc_Des3Init(&dec, HEAP_HINT, devId) != 0)
  6116. return -5101;
  6117. ret = wc_Des3_SetKey(&enc, key3, iv3, DES_ENCRYPTION);
  6118. if (ret != 0)
  6119. return -5102;
  6120. ret = wc_Des3_SetKey(&dec, key3, iv3, DES_DECRYPTION);
  6121. if (ret != 0)
  6122. return -5103;
  6123. ret = wc_Des3_CbcEncrypt(&enc, cipher, vector, sizeof(vector));
  6124. #if defined(WOLFSSL_ASYNC_CRYPT)
  6125. ret = wc_AsyncWait(ret, &enc.asyncDev, WC_ASYNC_FLAG_NONE);
  6126. #endif
  6127. if (ret != 0)
  6128. return -5104;
  6129. ret = wc_Des3_CbcDecrypt(&dec, plain, cipher, sizeof(cipher));
  6130. #if defined(WOLFSSL_ASYNC_CRYPT)
  6131. ret = wc_AsyncWait(ret, &dec.asyncDev, WC_ASYNC_FLAG_NONE);
  6132. #endif
  6133. if (ret != 0)
  6134. return -5105;
  6135. if (XMEMCMP(plain, vector, sizeof(plain)))
  6136. return -5106;
  6137. if (XMEMCMP(cipher, verify3, sizeof(cipher)))
  6138. return -5107;
  6139. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  6140. /* test the same vectors with using compatibility layer */
  6141. {
  6142. DES_key_schedule ks1;
  6143. DES_key_schedule ks2;
  6144. DES_key_schedule ks3;
  6145. DES_cblock iv4;
  6146. XMEMCPY(ks1, key3, sizeof(DES_key_schedule));
  6147. XMEMCPY(ks2, key3 + 8, sizeof(DES_key_schedule));
  6148. XMEMCPY(ks3, key3 + 16, sizeof(DES_key_schedule));
  6149. XMEMCPY(iv4, iv3, sizeof(DES_cblock));
  6150. XMEMSET(plain, 0, sizeof(plain));
  6151. XMEMSET(cipher, 0, sizeof(cipher));
  6152. DES_ede3_cbc_encrypt(vector, cipher, sizeof(vector), &ks1, &ks2, &ks3,
  6153. &iv4, DES_ENCRYPT);
  6154. DES_ede3_cbc_encrypt(cipher, plain, sizeof(cipher), &ks1, &ks2, &ks3,
  6155. &iv4, DES_DECRYPT);
  6156. if (XMEMCMP(plain, vector, sizeof(plain)))
  6157. return -5108;
  6158. if (XMEMCMP(cipher, verify3, sizeof(cipher)))
  6159. return -5109;
  6160. }
  6161. #endif /* OPENSSL_EXTRA */
  6162. wc_Des3Free(&enc);
  6163. wc_Des3Free(&dec);
  6164. #if defined(WOLFSSL_ENCRYPTED_KEYS) && !defined(NO_SHA)
  6165. {
  6166. EncryptedInfo info;
  6167. XMEMSET(&info, 0, sizeof(EncryptedInfo));
  6168. XMEMCPY(info.iv, iv3, sizeof(iv3));
  6169. info.ivSz = sizeof(iv3);
  6170. info.keySz = sizeof(key3);
  6171. info.cipherType = WC_CIPHER_DES3;
  6172. ret = wc_BufferKeyEncrypt(&info, cipher, sizeof(cipher), key3,
  6173. sizeof(key3), WC_HASH_TYPE_SHA);
  6174. if (ret != 0)
  6175. return -5110;
  6176. }
  6177. #endif
  6178. return 0;
  6179. }
  6180. #endif /* NO_DES3 */
  6181. #ifndef NO_AES
  6182. #if defined(WOLFSSL_AES_OFB) || defined(WOLFSSL_AES_CFB) || \
  6183. defined(WOLFSSL_AES_XTS)
  6184. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  6185. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  6186. /* pass in the function, key, iv, plain text and expected and this function
  6187. * tests that the encryption and decryption is successful */
  6188. static int EVP_test(const WOLFSSL_EVP_CIPHER* type, const byte* key,
  6189. const byte* iv, const byte* plain, int plainSz,
  6190. const byte* expected, int expectedSz)
  6191. {
  6192. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  6193. EVP_CIPHER_CTX *ctx = NULL;
  6194. #else
  6195. EVP_CIPHER_CTX ctx[1];
  6196. #endif
  6197. int idx, ret = 0, cipherSz;
  6198. byte* cipher;
  6199. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  6200. if ((ctx = wolfSSL_EVP_CIPHER_CTX_new()) == NULL)
  6201. return MEMORY_E;
  6202. #endif
  6203. cipher = (byte*)XMALLOC(plainSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  6204. if (cipher == NULL) {
  6205. ret = -5120;
  6206. goto EVP_TEST_END;
  6207. }
  6208. /* test encrypt */
  6209. EVP_CIPHER_CTX_init(ctx);
  6210. if (EVP_CipherInit(ctx, type, key, iv, 1) == 0) {
  6211. ret = -5121;
  6212. goto EVP_TEST_END;
  6213. }
  6214. if (EVP_CipherUpdate(ctx, cipher, &idx, plain, expectedSz) == 0) {
  6215. ret = -5122;
  6216. goto EVP_TEST_END;
  6217. }
  6218. cipherSz = idx;
  6219. if (EVP_CipherFinal(ctx, cipher + cipherSz, &idx) == 0) {
  6220. ret = -5123;
  6221. goto EVP_TEST_END;
  6222. }
  6223. cipherSz += idx;
  6224. if (XMEMCMP(cipher, expected, plainSz)) {
  6225. ret = -5124;
  6226. goto EVP_TEST_END;
  6227. }
  6228. /* test decrypt */
  6229. EVP_CIPHER_CTX_init(ctx);
  6230. if (EVP_CipherInit(ctx, type, key, iv, 0) == 0) {
  6231. ret = -5125;
  6232. goto EVP_TEST_END;
  6233. }
  6234. if (EVP_CipherUpdate(ctx, cipher, &idx, cipher, expectedSz) == 0) {
  6235. ret = -5126;
  6236. goto EVP_TEST_END;
  6237. }
  6238. cipherSz = idx;
  6239. if (EVP_CipherFinal(ctx, cipher + cipherSz, &idx) == 0) {
  6240. ret = -5127;
  6241. goto EVP_TEST_END;
  6242. }
  6243. cipherSz += idx;
  6244. if ((expectedSz != cipherSz) || XMEMCMP(plain, cipher, plainSz)) {
  6245. ret = -5128;
  6246. goto EVP_TEST_END;
  6247. }
  6248. EVP_TEST_END:
  6249. if (cipher)
  6250. XFREE(cipher, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  6251. (void)cipherSz;
  6252. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  6253. wolfSSL_EVP_CIPHER_CTX_free(ctx);
  6254. #endif
  6255. return ret;
  6256. }
  6257. #endif /* OPENSSL_EXTRA */
  6258. #endif /* WOLFSSL_AES_OFB || WOLFSSL_AES_CFB */
  6259. #ifdef WOLFSSL_AES_OFB
  6260. /* test vector from https://csrc.nist.gov/Projects/cryptographic-algorithm-validation-program/Block-Ciphers */
  6261. WOLFSSL_TEST_SUBROUTINE int aesofb_test(void)
  6262. {
  6263. #ifdef WOLFSSL_AES_256
  6264. WOLFSSL_SMALL_STACK_STATIC const byte key1[] =
  6265. {
  6266. 0xc4,0xc7,0xfa,0xd6,0x53,0x5c,0xb8,0x71,
  6267. 0x4a,0x5c,0x40,0x77,0x9a,0x8b,0xa1,0xd2,
  6268. 0x53,0x3e,0x23,0xb4,0xb2,0x58,0x73,0x2a,
  6269. 0x5b,0x78,0x01,0xf4,0xe3,0x71,0xa7,0x94
  6270. };
  6271. WOLFSSL_SMALL_STACK_STATIC const byte iv1[] =
  6272. {
  6273. 0x5e,0xb9,0x33,0x13,0xb8,0x71,0xff,0x16,
  6274. 0xb9,0x8a,0x9b,0xcb,0x43,0x33,0x0d,0x6f
  6275. };
  6276. WOLFSSL_SMALL_STACK_STATIC const byte plain1[] =
  6277. {
  6278. 0x6d,0x0b,0xb0,0x79,0x63,0x84,0x71,0xe9,
  6279. 0x39,0xd4,0x53,0x14,0x86,0xc1,0x4c,0x25,
  6280. 0x9a,0xee,0xc6,0xf3,0xc0,0x0d,0xfd,0xd6,
  6281. 0xc0,0x50,0xa8,0xba,0xa8,0x20,0xdb,0x71,
  6282. 0xcc,0x12,0x2c,0x4e,0x0c,0x17,0x15,0xef,
  6283. 0x55,0xf3,0x99,0x5a,0x6b,0xf0,0x2a,0x4c
  6284. };
  6285. WOLFSSL_SMALL_STACK_STATIC const byte cipher1[] =
  6286. {
  6287. 0x0f,0x54,0x61,0x71,0x59,0xd0,0x3f,0xfc,
  6288. 0x1b,0xfa,0xfb,0x60,0x29,0x30,0xd7,0x00,
  6289. 0xf4,0xa4,0xa8,0xe6,0xdd,0x93,0x94,0x46,
  6290. 0x64,0xd2,0x19,0xc4,0xc5,0x4d,0xde,0x1b,
  6291. 0x04,0x53,0xe1,0x73,0xf5,0x18,0x74,0xae,
  6292. 0xfd,0x64,0xa2,0xe1,0xe2,0x76,0x13,0xb0
  6293. };
  6294. #endif /* WOLFSSL_AES_256 */
  6295. #ifdef WOLFSSL_AES_128
  6296. WOLFSSL_SMALL_STACK_STATIC const byte key2[] =
  6297. {
  6298. 0x10,0xa5,0x88,0x69,0xd7,0x4b,0xe5,0xa3,
  6299. 0x74,0xcf,0x86,0x7c,0xfb,0x47,0x38,0x59
  6300. };
  6301. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] =
  6302. {
  6303. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6304. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  6305. };
  6306. WOLFSSL_SMALL_STACK_STATIC const byte plain2[] =
  6307. {
  6308. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6309. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  6310. };
  6311. WOLFSSL_SMALL_STACK_STATIC const byte cipher2[] =
  6312. {
  6313. 0x6d,0x25,0x1e,0x69,0x44,0xb0,0x51,0xe0,
  6314. 0x4e,0xaa,0x6f,0xb4,0xdb,0xf7,0x84,0x65
  6315. };
  6316. #endif /* WOLFSSL_AES_128 */
  6317. #ifdef WOLFSSL_AES_192
  6318. WOLFSSL_SMALL_STACK_STATIC const byte key3[] = {
  6319. 0xd0,0x77,0xa0,0x3b,0xd8,0xa3,0x89,0x73,
  6320. 0x92,0x8c,0xca,0xfe,0x4a,0x9d,0x2f,0x45,
  6321. 0x51,0x30,0xbd,0x0a,0xf5,0xae,0x46,0xa9
  6322. };
  6323. WOLFSSL_SMALL_STACK_STATIC const byte iv3[] =
  6324. {
  6325. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6326. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  6327. };
  6328. WOLFSSL_SMALL_STACK_STATIC const byte cipher3[] =
  6329. {
  6330. 0xab,0xc7,0x86,0xfb,0x1e,0xdb,0x50,0x45,
  6331. 0x80,0xc4,0xd8,0x82,0xef,0x29,0xa0,0xc7
  6332. };
  6333. WOLFSSL_SMALL_STACK_STATIC const byte plain3[] =
  6334. {
  6335. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6336. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  6337. };
  6338. #endif /* WOLFSSL_AES_192 */
  6339. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  6340. Aes *enc = NULL;
  6341. #else
  6342. Aes enc[1];
  6343. #endif
  6344. byte cipher[AES_BLOCK_SIZE * 4];
  6345. #ifdef HAVE_AES_DECRYPT
  6346. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  6347. Aes *dec = NULL;
  6348. #else
  6349. Aes dec[1];
  6350. #endif
  6351. byte plain [AES_BLOCK_SIZE * 4];
  6352. #endif
  6353. int ret = 0;
  6354. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  6355. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  6356. ERROR_OUT(-1, out);
  6357. #ifdef HAVE_AES_DECRYPT
  6358. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  6359. ERROR_OUT(-1, out);
  6360. #endif
  6361. #endif
  6362. XMEMSET(enc, 0, sizeof *enc);
  6363. #ifdef HAVE_AES_DECRYPT
  6364. XMEMSET(dec, 0, sizeof *dec);
  6365. #endif
  6366. #ifdef WOLFSSL_AES_128
  6367. /* 128 key size test */
  6368. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  6369. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  6370. ret = EVP_test(EVP_aes_128_ofb(), key2, iv2, plain2, sizeof(plain2),
  6371. cipher2, sizeof(cipher2));
  6372. if (ret != 0) {
  6373. goto out;
  6374. }
  6375. #endif
  6376. ret = wc_AesSetKey(enc, key2, sizeof(key2), iv2, AES_ENCRYPTION);
  6377. if (ret != 0)
  6378. ERROR_OUT(-5129, out);
  6379. #ifdef HAVE_AES_DECRYPT
  6380. /* decrypt uses AES_ENCRYPTION */
  6381. ret = wc_AesSetKey(dec, key2, sizeof(key2), iv2, AES_ENCRYPTION);
  6382. if (ret != 0)
  6383. ERROR_OUT(-5130, out);
  6384. #endif
  6385. XMEMSET(cipher, 0, sizeof(cipher));
  6386. ret = wc_AesOfbEncrypt(enc, cipher, plain2, AES_BLOCK_SIZE);
  6387. if (ret != 0)
  6388. ERROR_OUT(-5131, out);
  6389. if (XMEMCMP(cipher, cipher2, AES_BLOCK_SIZE))
  6390. ERROR_OUT(-5132, out);
  6391. #ifdef HAVE_AES_DECRYPT
  6392. ret = wc_AesOfbDecrypt(dec, plain, cipher2, AES_BLOCK_SIZE);
  6393. if (ret != 0)
  6394. ERROR_OUT(-5133, out);
  6395. if (XMEMCMP(plain, plain2, AES_BLOCK_SIZE))
  6396. ERROR_OUT(-5134, out);
  6397. #endif /* HAVE_AES_DECRYPT */
  6398. #endif /* WOLFSSL_AES_128 */
  6399. #ifdef WOLFSSL_AES_192
  6400. /* 192 key size test */
  6401. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  6402. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  6403. ret = EVP_test(EVP_aes_192_ofb(), key3, iv3, plain3, sizeof(plain3),
  6404. cipher3, sizeof(cipher3));
  6405. if (ret != 0) {
  6406. goto out;
  6407. }
  6408. #endif
  6409. ret = wc_AesSetKey(enc, key3, sizeof(key3), iv3, AES_ENCRYPTION);
  6410. if (ret != 0)
  6411. ERROR_OUT(-5135, out);
  6412. #ifdef HAVE_AES_DECRYPT
  6413. /* decrypt uses AES_ENCRYPTION */
  6414. ret = wc_AesSetKey(dec, key3, sizeof(key3), iv3, AES_ENCRYPTION);
  6415. if (ret != 0)
  6416. ERROR_OUT(-5136, out);
  6417. #endif
  6418. XMEMSET(cipher, 0, sizeof(cipher));
  6419. ret = wc_AesOfbEncrypt(enc, cipher, plain3, AES_BLOCK_SIZE);
  6420. if (ret != 0)
  6421. ERROR_OUT(-5137, out);
  6422. if (XMEMCMP(cipher, cipher3, AES_BLOCK_SIZE))
  6423. ERROR_OUT(-5138, out);
  6424. #ifdef HAVE_AES_DECRYPT
  6425. ret = wc_AesOfbDecrypt(dec, plain, cipher3, AES_BLOCK_SIZE);
  6426. if (ret != 0)
  6427. ERROR_OUT(-5139, out);
  6428. if (XMEMCMP(plain, plain3, AES_BLOCK_SIZE))
  6429. ERROR_OUT(-5140, out);
  6430. #endif /* HAVE_AES_DECRYPT */
  6431. #endif /* WOLFSSL_AES_192 */
  6432. #ifdef WOLFSSL_AES_256
  6433. /* 256 key size test */
  6434. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  6435. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  6436. ret = EVP_test(EVP_aes_256_ofb(), key1, iv1, plain1, sizeof(plain1),
  6437. cipher1, sizeof(cipher1));
  6438. if (ret != 0) {
  6439. goto out;
  6440. }
  6441. #endif
  6442. ret = wc_AesSetKey(enc, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  6443. if (ret != 0)
  6444. ERROR_OUT(-5141, out);
  6445. #ifdef HAVE_AES_DECRYPT
  6446. /* decrypt uses AES_ENCRYPTION */
  6447. ret = wc_AesSetKey(dec, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  6448. if (ret != 0)
  6449. ERROR_OUT(-5142, out);
  6450. #endif
  6451. XMEMSET(cipher, 0, sizeof(cipher));
  6452. ret = wc_AesOfbEncrypt(enc, cipher, plain1, AES_BLOCK_SIZE);
  6453. if (ret != 0)
  6454. ERROR_OUT(-5143, out);
  6455. if (XMEMCMP(cipher, cipher1, AES_BLOCK_SIZE))
  6456. ERROR_OUT(-5144, out);
  6457. ret = wc_AesOfbEncrypt(enc, cipher + AES_BLOCK_SIZE,
  6458. plain1 + AES_BLOCK_SIZE, AES_BLOCK_SIZE);
  6459. if (ret != 0)
  6460. ERROR_OUT(-5145, out);
  6461. if (XMEMCMP(cipher + AES_BLOCK_SIZE, cipher1 + AES_BLOCK_SIZE,
  6462. AES_BLOCK_SIZE))
  6463. ERROR_OUT(-5146, out);
  6464. #ifdef HAVE_AES_DECRYPT
  6465. ret = wc_AesOfbDecrypt(dec, plain, cipher1, AES_BLOCK_SIZE);
  6466. if (ret != 0)
  6467. ERROR_OUT(-5147, out);
  6468. if (XMEMCMP(plain, plain1, AES_BLOCK_SIZE))
  6469. ERROR_OUT(-5148, out);
  6470. ret = wc_AesOfbDecrypt(dec, plain + AES_BLOCK_SIZE,
  6471. cipher1 + AES_BLOCK_SIZE, AES_BLOCK_SIZE);
  6472. if (ret != 0)
  6473. ERROR_OUT(-5149, out);
  6474. if (XMEMCMP(plain + AES_BLOCK_SIZE, plain1 + AES_BLOCK_SIZE,
  6475. AES_BLOCK_SIZE))
  6476. ERROR_OUT(-5150, out);
  6477. #endif /* HAVE_AES_DECRYPT */
  6478. /* multiple blocks at once */
  6479. ret = wc_AesSetKey(enc, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  6480. if (ret != 0)
  6481. ERROR_OUT(-5151, out);
  6482. #ifdef HAVE_AES_DECRYPT
  6483. /* decrypt uses AES_ENCRYPTION */
  6484. ret = wc_AesSetKey(dec, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  6485. if (ret != 0)
  6486. ERROR_OUT(-5152, out);
  6487. #endif
  6488. XMEMSET(cipher, 0, sizeof(cipher));
  6489. ret = wc_AesOfbEncrypt(enc, cipher, plain1, AES_BLOCK_SIZE * 3);
  6490. if (ret != 0)
  6491. ERROR_OUT(-5153, out);
  6492. if (XMEMCMP(cipher, cipher1, AES_BLOCK_SIZE * 3))
  6493. ERROR_OUT(-5154, out);
  6494. #ifdef HAVE_AES_DECRYPT
  6495. ret = wc_AesOfbDecrypt(dec, plain, cipher1, AES_BLOCK_SIZE * 3);
  6496. if (ret != 0)
  6497. ERROR_OUT(-5155, out);
  6498. if (XMEMCMP(plain, plain1, AES_BLOCK_SIZE * 3))
  6499. ERROR_OUT(-5156, out);
  6500. #endif /* HAVE_AES_DECRYPT */
  6501. /* inline decrypt/encrypt*/
  6502. ret = wc_AesSetKey(enc, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  6503. if (ret != 0)
  6504. ERROR_OUT(-5157, out);
  6505. #ifdef HAVE_AES_DECRYPT
  6506. /* decrypt uses AES_ENCRYPTION */
  6507. ret = wc_AesSetKey(dec, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  6508. if (ret != 0)
  6509. ERROR_OUT(-5158, out);
  6510. #endif
  6511. XMEMCPY(cipher, plain1, AES_BLOCK_SIZE * 2);
  6512. ret = wc_AesOfbEncrypt(enc, cipher, cipher, AES_BLOCK_SIZE * 2);
  6513. if (ret != 0)
  6514. ERROR_OUT(-5159, out);
  6515. if (XMEMCMP(cipher, cipher1, AES_BLOCK_SIZE * 2))
  6516. ERROR_OUT(-5160, out);
  6517. #ifdef HAVE_AES_DECRYPT
  6518. ret = wc_AesOfbDecrypt(dec, cipher, cipher, AES_BLOCK_SIZE * 2);
  6519. if (ret != 0)
  6520. ERROR_OUT(-5161, out);
  6521. if (XMEMCMP(cipher, plain1, AES_BLOCK_SIZE * 2))
  6522. ERROR_OUT(-5162, out);
  6523. #endif /* HAVE_AES_DECRYPT */
  6524. /* 256 key size test leftover support */
  6525. ret = wc_AesSetKey(enc, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  6526. if (ret != 0)
  6527. ERROR_OUT(-5163, out);
  6528. #ifdef HAVE_AES_DECRYPT
  6529. /* decrypt uses AES_ENCRYPTION */
  6530. ret = wc_AesSetKey(dec, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  6531. if (ret != 0)
  6532. ERROR_OUT(-5164, out);
  6533. #endif
  6534. XMEMSET(cipher, 0, sizeof(cipher));
  6535. ret = wc_AesOfbEncrypt(enc, cipher, plain1, 3);
  6536. if (ret != 0)
  6537. ERROR_OUT(-5165, out);
  6538. if (XMEMCMP(cipher, cipher1, 3))
  6539. ERROR_OUT(-5166, out);
  6540. ret = wc_AesOfbEncrypt(enc, cipher + 3, plain1 + 3, AES_BLOCK_SIZE);
  6541. if (ret != 0)
  6542. ERROR_OUT(-5167, out);
  6543. if (XMEMCMP(cipher + 3, cipher1 + 3, AES_BLOCK_SIZE))
  6544. ERROR_OUT(-5168, out);
  6545. #ifdef HAVE_AES_DECRYPT
  6546. ret = wc_AesOfbDecrypt(dec, plain, cipher1, 6);
  6547. if (ret != 0)
  6548. ERROR_OUT(-5169, out);
  6549. if (XMEMCMP(plain, plain1, 6))
  6550. ERROR_OUT(-5170, out);
  6551. ret = wc_AesOfbDecrypt(dec, plain + 6, cipher1 + 6, AES_BLOCK_SIZE);
  6552. if (ret != 0)
  6553. ERROR_OUT(-5171, out);
  6554. if (XMEMCMP(plain + 6, plain1 + 6, AES_BLOCK_SIZE))
  6555. ERROR_OUT(-5172, out);
  6556. #endif /* HAVE_AES_DECRYPT */
  6557. out:
  6558. wc_AesFree(enc);
  6559. wc_AesFree(dec);
  6560. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  6561. if (enc)
  6562. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  6563. #ifdef HAVE_AES_DECRYPT
  6564. if (dec)
  6565. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  6566. #endif
  6567. #endif
  6568. #endif /* WOLFSSL_AES_256 */
  6569. return ret;
  6570. }
  6571. #endif /* WOLFSSL_AES_OFB */
  6572. #if defined(WOLFSSL_AES_CFB)
  6573. /* Test cases from NIST SP 800-38A, Recommendation for Block Cipher Modes of Operation Methods an*/
  6574. static int aescfb_test(void)
  6575. {
  6576. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  6577. Aes *enc = NULL;
  6578. #else
  6579. Aes enc[1];
  6580. #endif
  6581. int enc_inited = 0;
  6582. byte cipher[AES_BLOCK_SIZE * 4];
  6583. #ifdef HAVE_AES_DECRYPT
  6584. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  6585. Aes *dec = NULL;
  6586. #else
  6587. Aes dec[1];
  6588. #endif
  6589. int dec_inited = 0;
  6590. byte plain [AES_BLOCK_SIZE * 4];
  6591. #endif
  6592. int ret = 0;
  6593. WOLFSSL_SMALL_STACK_STATIC const byte iv[] = {
  6594. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  6595. 0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f
  6596. };
  6597. #ifdef WOLFSSL_AES_128
  6598. WOLFSSL_SMALL_STACK_STATIC const byte key1[] =
  6599. {
  6600. 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,
  6601. 0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c
  6602. };
  6603. WOLFSSL_SMALL_STACK_STATIC const byte cipher1[] =
  6604. {
  6605. 0x3b,0x3f,0xd9,0x2e,0xb7,0x2d,0xad,0x20,
  6606. 0x33,0x34,0x49,0xf8,0xe8,0x3c,0xfb,0x4a,
  6607. 0xc8,0xa6,0x45,0x37,0xa0,0xb3,0xa9,0x3f,
  6608. 0xcd,0xe3,0xcd,0xad,0x9f,0x1c,0xe5,0x8b,
  6609. 0x26,0x75,0x1f,0x67,0xa3,0xcb,0xb1,0x40,
  6610. 0xb1,0x80,0x8c,0xf1,0x87,0xa4,0xf4,0xdf
  6611. };
  6612. WOLFSSL_SMALL_STACK_STATIC const byte msg1[] =
  6613. {
  6614. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  6615. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  6616. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  6617. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  6618. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  6619. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef
  6620. };
  6621. #endif /* WOLFSSL_AES_128 */
  6622. #ifdef WOLFSSL_AES_192
  6623. /* 192 size key test */
  6624. WOLFSSL_SMALL_STACK_STATIC const byte key2[] =
  6625. {
  6626. 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  6627. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  6628. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b
  6629. };
  6630. WOLFSSL_SMALL_STACK_STATIC const byte cipher2[] =
  6631. {
  6632. 0xcd,0xc8,0x0d,0x6f,0xdd,0xf1,0x8c,0xab,
  6633. 0x34,0xc2,0x59,0x09,0xc9,0x9a,0x41,0x74,
  6634. 0x67,0xce,0x7f,0x7f,0x81,0x17,0x36,0x21,
  6635. 0x96,0x1a,0x2b,0x70,0x17,0x1d,0x3d,0x7a,
  6636. 0x2e,0x1e,0x8a,0x1d,0xd5,0x9b,0x88,0xb1,
  6637. 0xc8,0xe6,0x0f,0xed,0x1e,0xfa,0xc4,0xc9,
  6638. 0xc0,0x5f,0x9f,0x9c,0xa9,0x83,0x4f,0xa0,
  6639. 0x42,0xae,0x8f,0xba,0x58,0x4b,0x09,0xff
  6640. };
  6641. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] =
  6642. {
  6643. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  6644. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  6645. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  6646. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  6647. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  6648. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  6649. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  6650. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  6651. };
  6652. #endif /* WOLFSSL_AES_192 */
  6653. #ifdef WOLFSSL_AES_256
  6654. /* 256 size key simple test */
  6655. WOLFSSL_SMALL_STACK_STATIC const byte key3[] =
  6656. {
  6657. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  6658. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  6659. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  6660. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  6661. };
  6662. WOLFSSL_SMALL_STACK_STATIC const byte cipher3[] =
  6663. {
  6664. 0xdc,0x7e,0x84,0xbf,0xda,0x79,0x16,0x4b,
  6665. 0x7e,0xcd,0x84,0x86,0x98,0x5d,0x38,0x60,
  6666. 0x39,0xff,0xed,0x14,0x3b,0x28,0xb1,0xc8,
  6667. 0x32,0x11,0x3c,0x63,0x31,0xe5,0x40,0x7b,
  6668. 0xdf,0x10,0x13,0x24,0x15,0xe5,0x4b,0x92,
  6669. 0xa1,0x3e,0xd0,0xa8,0x26,0x7a,0xe2,0xf9,
  6670. 0x75,0xa3,0x85,0x74,0x1a,0xb9,0xce,0xf8,
  6671. 0x20,0x31,0x62,0x3d,0x55,0xb1,0xe4,0x71
  6672. };
  6673. WOLFSSL_SMALL_STACK_STATIC const byte msg3[] =
  6674. {
  6675. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  6676. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  6677. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  6678. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  6679. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  6680. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  6681. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  6682. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  6683. };
  6684. #endif /* WOLFSSL_AES_256 */
  6685. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  6686. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  6687. ERROR_OUT(-1, out);
  6688. #ifdef HAVE_AES_DECRYPT
  6689. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  6690. ERROR_OUT(-1, out);
  6691. #endif
  6692. #endif
  6693. if (wc_AesInit(enc, HEAP_HINT, devId) != 0)
  6694. ERROR_OUT(-5173, out);
  6695. else
  6696. enc_inited = 1;
  6697. #ifdef HAVE_AES_DECRYPT
  6698. if (wc_AesInit(dec, HEAP_HINT, devId) != 0)
  6699. ERROR_OUT(-5174, out);
  6700. else
  6701. dec_inited = 1;
  6702. #endif
  6703. #ifdef WOLFSSL_AES_128
  6704. /* 128 key tests */
  6705. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  6706. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  6707. ret = EVP_test(EVP_aes_128_cfb128(), key1, iv, msg1, sizeof(msg1),
  6708. cipher1, sizeof(cipher1));
  6709. if (ret != 0) {
  6710. return ret;
  6711. }
  6712. #endif
  6713. ret = wc_AesSetKey(enc, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  6714. if (ret != 0)
  6715. ERROR_OUT(-5175, out);
  6716. #ifdef HAVE_AES_DECRYPT
  6717. /* decrypt uses AES_ENCRYPTION */
  6718. ret = wc_AesSetKey(dec, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  6719. if (ret != 0)
  6720. ERROR_OUT(-5176, out);
  6721. #endif
  6722. XMEMSET(cipher, 0, sizeof(cipher));
  6723. ret = wc_AesCfbEncrypt(enc, cipher, msg1, AES_BLOCK_SIZE * 2);
  6724. if (ret != 0)
  6725. ERROR_OUT(-5177, out);
  6726. if (XMEMCMP(cipher, cipher1, AES_BLOCK_SIZE * 2))
  6727. ERROR_OUT(-5178, out);
  6728. /* test restarting encryption process */
  6729. ret = wc_AesCfbEncrypt(enc, cipher + (AES_BLOCK_SIZE * 2),
  6730. msg1 + (AES_BLOCK_SIZE * 2), AES_BLOCK_SIZE);
  6731. if (ret != 0)
  6732. ERROR_OUT(-5179, out);
  6733. if (XMEMCMP(cipher + (AES_BLOCK_SIZE * 2),
  6734. cipher1 + (AES_BLOCK_SIZE * 2), AES_BLOCK_SIZE))
  6735. ERROR_OUT(-5180, out);
  6736. #ifdef HAVE_AES_DECRYPT
  6737. ret = wc_AesCfbDecrypt(dec, plain, cipher, AES_BLOCK_SIZE * 3);
  6738. if (ret != 0)
  6739. ERROR_OUT(-5181, out);
  6740. if (XMEMCMP(plain, msg1, AES_BLOCK_SIZE * 3))
  6741. ERROR_OUT(-5182, out);
  6742. #endif /* HAVE_AES_DECRYPT */
  6743. #endif /* WOLFSSL_AES_128 */
  6744. #ifdef WOLFSSL_AES_192
  6745. /* 192 key size test */
  6746. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  6747. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  6748. ret = EVP_test(EVP_aes_192_cfb128(), key2, iv, msg2, sizeof(msg2),
  6749. cipher2, sizeof(cipher2));
  6750. if (ret != 0) {
  6751. return ret;
  6752. }
  6753. #endif
  6754. ret = wc_AesSetKey(enc, key2, sizeof(key2), iv, AES_ENCRYPTION);
  6755. if (ret != 0)
  6756. ERROR_OUT(-5183, out);
  6757. #ifdef HAVE_AES_DECRYPT
  6758. /* decrypt uses AES_ENCRYPTION */
  6759. ret = wc_AesSetKey(dec, key2, sizeof(key2), iv, AES_ENCRYPTION);
  6760. if (ret != 0)
  6761. ERROR_OUT(-5184, out);
  6762. #endif
  6763. XMEMSET(cipher, 0, sizeof(cipher));
  6764. ret = wc_AesCfbEncrypt(enc, cipher, msg2, AES_BLOCK_SIZE * 4);
  6765. if (ret != 0)
  6766. ERROR_OUT(-5185, out);
  6767. if (XMEMCMP(cipher, cipher2, AES_BLOCK_SIZE * 4))
  6768. ERROR_OUT(-5186, out);
  6769. #ifdef HAVE_AES_DECRYPT
  6770. ret = wc_AesCfbDecrypt(dec, plain, cipher, AES_BLOCK_SIZE * 4);
  6771. if (ret != 0)
  6772. ERROR_OUT(-5187, out);
  6773. if (XMEMCMP(plain, msg2, AES_BLOCK_SIZE * 4))
  6774. ERROR_OUT(-5188, out);
  6775. #endif /* HAVE_AES_DECRYPT */
  6776. #endif /* WOLFSSL_AES_192 */
  6777. #ifdef WOLFSSL_AES_256
  6778. /* 256 key size test */
  6779. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  6780. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  6781. ret = EVP_test(EVP_aes_256_cfb128(), key3, iv, msg3, sizeof(msg3),
  6782. cipher3, sizeof(cipher3));
  6783. if (ret != 0) {
  6784. return ret;
  6785. }
  6786. #endif
  6787. ret = wc_AesSetKey(enc, key3, sizeof(key3), iv, AES_ENCRYPTION);
  6788. if (ret != 0)
  6789. ERROR_OUT(-5189, out);
  6790. #ifdef HAVE_AES_DECRYPT
  6791. /* decrypt uses AES_ENCRYPTION */
  6792. ret = wc_AesSetKey(dec, key3, sizeof(key3), iv, AES_ENCRYPTION);
  6793. if (ret != 0)
  6794. ERROR_OUT(-5190, out);
  6795. #endif
  6796. /* test with data left overs, magic lengths are checking near edges */
  6797. XMEMSET(cipher, 0, sizeof(cipher));
  6798. ret = wc_AesCfbEncrypt(enc, cipher, msg3, 4);
  6799. if (ret != 0)
  6800. ERROR_OUT(-5191, out);
  6801. if (XMEMCMP(cipher, cipher3, 4))
  6802. ERROR_OUT(-5192, out);
  6803. ret = wc_AesCfbEncrypt(enc, cipher + 4, msg3 + 4, 27);
  6804. if (ret != 0)
  6805. ERROR_OUT(-5193, out);
  6806. if (XMEMCMP(cipher + 4, cipher3 + 4, 27))
  6807. ERROR_OUT(-5194, out);
  6808. ret = wc_AesCfbEncrypt(enc, cipher + 31, msg3 + 31,
  6809. (AES_BLOCK_SIZE * 4) - 31);
  6810. if (ret != 0)
  6811. ERROR_OUT(-5195, out);
  6812. if (XMEMCMP(cipher, cipher3, AES_BLOCK_SIZE * 4))
  6813. ERROR_OUT(-5196, out);
  6814. #ifdef HAVE_AES_DECRYPT
  6815. ret = wc_AesCfbDecrypt(dec, plain, cipher, 4);
  6816. if (ret != 0)
  6817. ERROR_OUT(-5197, out);
  6818. if (XMEMCMP(plain, msg3, 4))
  6819. ERROR_OUT(-5198, out);
  6820. ret = wc_AesCfbDecrypt(dec, plain + 4, cipher + 4, 4);
  6821. if (ret != 0)
  6822. ERROR_OUT(-5199, out);
  6823. ret = wc_AesCfbDecrypt(dec, plain + 8, cipher + 8, 23);
  6824. if (ret != 0)
  6825. ERROR_OUT(-5200, out);
  6826. if (XMEMCMP(plain + 4, msg3 + 4, 27))
  6827. ERROR_OUT(-5201, out);
  6828. ret = wc_AesCfbDecrypt(dec, plain + 31, cipher + 31,
  6829. (AES_BLOCK_SIZE * 4) - 31);
  6830. if (ret != 0)
  6831. ERROR_OUT(-5202, out);
  6832. if (XMEMCMP(plain, msg3, AES_BLOCK_SIZE * 4))
  6833. ERROR_OUT(-5203, out);
  6834. #endif /* HAVE_AES_DECRYPT */
  6835. #endif /* WOLFSSL_AES_256 */
  6836. out:
  6837. if (enc_inited)
  6838. wc_AesFree(enc);
  6839. if (dec_inited)
  6840. wc_AesFree(dec);
  6841. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  6842. if (enc)
  6843. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  6844. #ifdef HAVE_AES_DECRYPT
  6845. if (dec)
  6846. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  6847. #endif
  6848. #endif
  6849. return ret;
  6850. }
  6851. #if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  6852. static int aescfb1_test(void)
  6853. {
  6854. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  6855. Aes *enc = NULL;
  6856. #else
  6857. Aes enc[1];
  6858. #endif
  6859. int enc_inited = 0;
  6860. byte cipher[AES_BLOCK_SIZE];
  6861. #ifdef HAVE_AES_DECRYPT
  6862. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  6863. Aes *dec = NULL;
  6864. #else
  6865. Aes dec[1];
  6866. #endif
  6867. int dec_inited = 0;
  6868. byte plain [AES_BLOCK_SIZE];
  6869. #endif
  6870. int ret = 0;
  6871. #ifdef WOLFSSL_AES_128
  6872. WOLFSSL_SMALL_STACK_STATIC const byte iv[] = {
  6873. 0x4d,0xbb,0xdc,0xaa,0x59,0xf3,0x63,0xc9,
  6874. 0x2a,0x3b,0x98,0x43,0xad,0x20,0xe2,0xb7
  6875. };
  6876. WOLFSSL_SMALL_STACK_STATIC const byte key1[] =
  6877. {
  6878. 0xcd,0xef,0x9d,0x06,0x61,0xba,0xe4,0x73,
  6879. 0x8d,0x1a,0x58,0xa2,0xa6,0x22,0x8b,0x66
  6880. };
  6881. WOLFSSL_SMALL_STACK_STATIC const byte cipher1[] =
  6882. {
  6883. 0x00
  6884. };
  6885. WOLFSSL_SMALL_STACK_STATIC const byte msg1[] =
  6886. {
  6887. 0xC0
  6888. };
  6889. #endif /* WOLFSSL_AES_128 */
  6890. #ifdef WOLFSSL_AES_192
  6891. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] = {
  6892. 0x57,0xc6,0x89,0x7c,0x99,0x52,0x28,0x13,
  6893. 0xbf,0x67,0x9c,0xe1,0x13,0x70,0xaf,0x5e
  6894. };
  6895. WOLFSSL_SMALL_STACK_STATIC const byte key2[] =
  6896. {
  6897. 0xba,0xa1,0x58,0xa1,0x6b,0x50,0x4a,0x10,
  6898. 0x8e,0xd4,0x33,0x2e,0xe7,0xf2,0x9b,0xf6,
  6899. 0xd1,0xac,0x46,0xa8,0xde,0x5a,0xfe,0x7a
  6900. };
  6901. WOLFSSL_SMALL_STACK_STATIC const byte cipher2[] =
  6902. {
  6903. 0x30
  6904. };
  6905. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] =
  6906. {
  6907. 0x80
  6908. };
  6909. #endif /* WOLFSSL_AES_192 */
  6910. #ifdef WOLFSSL_AES_256
  6911. WOLFSSL_SMALL_STACK_STATIC const byte iv3[] = {
  6912. 0x63,0x2e,0x9f,0x83,0x1f,0xa3,0x80,0x5e,
  6913. 0x52,0x02,0xbc,0xe0,0x6d,0x04,0xf9,0xa0
  6914. };
  6915. WOLFSSL_SMALL_STACK_STATIC const byte key3[] =
  6916. {
  6917. 0xf6,0xfa,0xe4,0xf1,0x5d,0x91,0xfc,0x50,
  6918. 0x88,0x78,0x4f,0x84,0xa5,0x37,0x12,0x7e,
  6919. 0x32,0x63,0x55,0x9c,0x62,0x73,0x88,0x20,
  6920. 0xc2,0xcf,0x3d,0xe1,0x1c,0x2a,0x30,0x40
  6921. };
  6922. WOLFSSL_SMALL_STACK_STATIC const byte cipher3[] =
  6923. {
  6924. 0xF7, 0x00
  6925. };
  6926. WOLFSSL_SMALL_STACK_STATIC const byte msg3[] =
  6927. {
  6928. 0x41, 0xC0
  6929. };
  6930. #endif /* WOLFSSL_AES_256 */
  6931. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  6932. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  6933. ERROR_OUT(-1, out);
  6934. #ifdef HAVE_AES_DECRYPT
  6935. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  6936. ERROR_OUT(-1, out);
  6937. #endif
  6938. #endif
  6939. if (wc_AesInit(enc, HEAP_HINT, devId) != 0)
  6940. ERROR_OUT(-5204, out);
  6941. else
  6942. enc_inited = 1;
  6943. #ifdef HAVE_AES_DECRYPT
  6944. if (wc_AesInit(dec, HEAP_HINT, devId) != 0)
  6945. ERROR_OUT(-5205, out);
  6946. else
  6947. dec_inited = 1;
  6948. #endif
  6949. #ifdef WOLFSSL_AES_128
  6950. /* 128 key tests */
  6951. ret = wc_AesSetKey(enc, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  6952. if (ret != 0)
  6953. ERROR_OUT(-5206, out);
  6954. #ifdef HAVE_AES_DECRYPT
  6955. /* decrypt uses AES_ENCRYPTION */
  6956. ret = wc_AesSetKey(dec, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  6957. if (ret != 0)
  6958. ERROR_OUT(-5207, out);
  6959. #endif
  6960. XMEMSET(cipher, 0, sizeof(cipher));
  6961. ret = wc_AesCfb1Encrypt(enc, cipher, msg1, 2);
  6962. if (ret != 0)
  6963. ERROR_OUT(-5208, out);
  6964. if (cipher[0] != cipher1[0])
  6965. ERROR_OUT(-5209, out);
  6966. #ifdef HAVE_AES_DECRYPT
  6967. ret = wc_AesCfb1Decrypt(dec, plain, cipher, 2);
  6968. if (ret != 0)
  6969. ERROR_OUT(-5210, out);
  6970. if (plain[0] != msg1[0])
  6971. ERROR_OUT(-5211, out);
  6972. #endif /* HAVE_AES_DECRYPT */
  6973. #ifdef OPENSSL_EXTRA
  6974. ret = wc_AesSetKey(enc, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  6975. if (ret != 0)
  6976. ERROR_OUT(-5212, out);
  6977. XMEMSET(cipher, 0, sizeof(cipher));
  6978. ret = wc_AesCfb1Encrypt(enc, cipher, msg1,
  6979. sizeof(msg1) * WOLFSSL_BIT_SIZE);
  6980. if (ret != 0)
  6981. ERROR_OUT(-5213, out);
  6982. #ifndef WOLFCRYPT_ONLY
  6983. ret = EVP_test(EVP_aes_128_cfb1(), key1, iv, msg1, sizeof(msg1),
  6984. cipher, sizeof(msg1));
  6985. if (ret != 0) {
  6986. goto out;
  6987. }
  6988. #endif
  6989. #endif
  6990. #endif /* WOLFSSL_AES_128 */
  6991. #ifdef WOLFSSL_AES_192
  6992. /* 192 key tests */
  6993. ret = wc_AesSetKey(enc, key2, sizeof(key2), iv2, AES_ENCRYPTION);
  6994. if (ret != 0)
  6995. ERROR_OUT(-5214, out);
  6996. XMEMSET(cipher, 0, sizeof(cipher));
  6997. ret = wc_AesCfb1Encrypt(enc, cipher, msg2, 4);
  6998. if (ret != 0)
  6999. ERROR_OUT(-5215, out);
  7000. if (XMEMCMP(cipher, cipher2, sizeof(cipher2)) != 0)
  7001. ERROR_OUT(-5216, out);
  7002. #ifdef OPENSSL_EXTRA
  7003. ret = wc_AesSetKey(enc, key2, sizeof(key2), iv2, AES_ENCRYPTION);
  7004. if (ret != 0)
  7005. ERROR_OUT(-5217, out);
  7006. XMEMSET(cipher, 0, sizeof(cipher));
  7007. ret = wc_AesCfb1Encrypt(enc, cipher, msg2,
  7008. sizeof(msg2) * WOLFSSL_BIT_SIZE);
  7009. if (ret != 0)
  7010. ERROR_OUT(-5218, out);
  7011. #ifndef WOLFCRYPT_ONLY
  7012. ret = EVP_test(EVP_aes_192_cfb1(), key2, iv2, msg2, sizeof(msg2),
  7013. cipher, sizeof(msg2));
  7014. if (ret != 0) {
  7015. goto out;
  7016. }
  7017. #endif
  7018. #endif
  7019. #endif /* WOLFSSL_AES_192 */
  7020. #ifdef WOLFSSL_AES_256
  7021. /* 256 key tests */
  7022. ret = wc_AesSetKey(enc, key3, sizeof(key3), iv3, AES_ENCRYPTION);
  7023. if (ret != 0)
  7024. ERROR_OUT(-5219, out);
  7025. XMEMSET(cipher, 0, sizeof(cipher));
  7026. ret = wc_AesCfb1Encrypt(enc, cipher, msg3, 10);
  7027. if (ret != 0)
  7028. ERROR_OUT(-5220, out);
  7029. if (XMEMCMP(cipher, cipher3, sizeof(cipher3)) != 0)
  7030. ERROR_OUT(-5221, out);
  7031. #ifdef OPENSSL_EXTRA
  7032. ret = wc_AesSetKey(enc, key3, sizeof(key3), iv3, AES_ENCRYPTION);
  7033. if (ret != 0)
  7034. ERROR_OUT(-5222, out);
  7035. XMEMSET(cipher, 0, sizeof(cipher));
  7036. ret = wc_AesCfb1Encrypt(enc, cipher, msg3,
  7037. sizeof(msg3) * WOLFSSL_BIT_SIZE);
  7038. if (ret != 0)
  7039. ERROR_OUT(-5223, out);
  7040. #ifndef WOLFCRYPT_ONLY
  7041. ret = EVP_test(EVP_aes_256_cfb1(), key3, iv3, msg3, sizeof(msg3),
  7042. cipher, sizeof(msg3));
  7043. if (ret != 0) {
  7044. goto out;
  7045. }
  7046. #endif
  7047. #endif
  7048. out:
  7049. if (enc_inited)
  7050. wc_AesFree(enc);
  7051. #ifdef HAVE_AES_DECRYPT
  7052. if (dec_inited)
  7053. wc_AesFree(dec);
  7054. #endif
  7055. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7056. if (enc)
  7057. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  7058. #ifdef HAVE_AES_DECRYPT
  7059. if (dec)
  7060. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  7061. #endif
  7062. #endif
  7063. #endif /* WOLFSSL_AES_256 */
  7064. return ret;
  7065. }
  7066. static int aescfb8_test(void)
  7067. {
  7068. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7069. Aes *enc = NULL;
  7070. #else
  7071. Aes enc[1];
  7072. #endif
  7073. int enc_inited = 0;
  7074. byte cipher[AES_BLOCK_SIZE];
  7075. #ifdef HAVE_AES_DECRYPT
  7076. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7077. Aes *dec = NULL;
  7078. #else
  7079. Aes dec[1];
  7080. #endif
  7081. int dec_inited = 0;
  7082. byte plain [AES_BLOCK_SIZE];
  7083. #endif
  7084. int ret = 0;
  7085. #ifdef WOLFSSL_AES_128
  7086. WOLFSSL_SMALL_STACK_STATIC const byte iv[] = {
  7087. 0xf4,0x75,0xc6,0x49,0x91,0xb2,0x0e,0xae,
  7088. 0xe1,0x83,0xa2,0x26,0x29,0xe2,0x1e,0x22
  7089. };
  7090. WOLFSSL_SMALL_STACK_STATIC const byte key1[] =
  7091. {
  7092. 0xc8,0xfe,0x9b,0xf7,0x7b,0x93,0x0f,0x46,
  7093. 0xd2,0x07,0x8b,0x8c,0x0e,0x65,0x7c,0xd4
  7094. };
  7095. WOLFSSL_SMALL_STACK_STATIC const byte cipher1[] =
  7096. {
  7097. 0xd2,0x76,0x91
  7098. };
  7099. WOLFSSL_SMALL_STACK_STATIC const byte msg1[] =
  7100. {
  7101. 0xc9,0x06,0x35
  7102. };
  7103. #endif /* WOLFSSL_AES_128 */
  7104. #ifdef WOLFSSL_AES_192
  7105. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] = {
  7106. 0x0a,0x02,0x84,0x6b,0x62,0xab,0xb6,0x93,
  7107. 0xef,0x31,0xd7,0x54,0x84,0x2e,0xed,0x29
  7108. };
  7109. WOLFSSL_SMALL_STACK_STATIC const byte key2[] =
  7110. {
  7111. 0xba,0xf0,0x8b,0x76,0x31,0x7a,0x65,0xc5,
  7112. 0xf0,0x7a,0xe6,0xf5,0x7e,0xb0,0xe6,0x54,
  7113. 0x88,0x65,0x93,0x24,0xd2,0x97,0x09,0xe3
  7114. };
  7115. WOLFSSL_SMALL_STACK_STATIC const byte cipher2[] =
  7116. {
  7117. 0x72,0x9c,0x0b,0x6d,0xeb,0x75,0xfa,0x6e,
  7118. 0xb5,0xe8
  7119. };
  7120. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] =
  7121. {
  7122. 0x98,0x95,0x93,0x24,0x02,0x39,0x3d,0xc3,
  7123. 0x3a,0x60
  7124. };
  7125. #endif
  7126. #ifdef WOLFSSL_AES_256
  7127. WOLFSSL_SMALL_STACK_STATIC const byte iv3[] = {
  7128. 0x33,0x8c,0x55,0x2f,0xf1,0xec,0xa1,0x44,
  7129. 0x08,0xe0,0x5d,0x8c,0xf9,0xf3,0xb3,0x1b
  7130. };
  7131. WOLFSSL_SMALL_STACK_STATIC const byte key3[] =
  7132. {
  7133. 0x06,0x48,0x74,0x09,0x2f,0x7a,0x13,0xcc,
  7134. 0x44,0x62,0x24,0x7a,0xd4,0x23,0xd0,0xe9,
  7135. 0x6e,0xdf,0x42,0xe8,0xb6,0x7a,0x5a,0x23,
  7136. 0xb7,0xa0,0xa6,0x47,0x7b,0x09,0x8e,0x66
  7137. };
  7138. WOLFSSL_SMALL_STACK_STATIC const byte cipher3[] =
  7139. {
  7140. 0x1c,0xff,0x95
  7141. };
  7142. WOLFSSL_SMALL_STACK_STATIC const byte msg3[] =
  7143. {
  7144. 0xb9,0x74,0xfa
  7145. };
  7146. #endif
  7147. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7148. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  7149. ERROR_OUT(-5238, out);
  7150. #ifdef HAVE_AES_DECRYPT
  7151. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  7152. ERROR_OUT(-5239, out);
  7153. #endif
  7154. #endif
  7155. if (wc_AesInit(enc, HEAP_HINT, devId) != 0)
  7156. ERROR_OUT(-5224, out);
  7157. else
  7158. enc_inited = 1;
  7159. #ifdef HAVE_AES_DECRYPT
  7160. if (wc_AesInit(dec, HEAP_HINT, devId) != 0)
  7161. ERROR_OUT(-5225, out);
  7162. else
  7163. dec_inited = 1;
  7164. #endif
  7165. #ifdef WOLFSSL_AES_128
  7166. /* 128 key tests */
  7167. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  7168. ret = EVP_test(EVP_aes_128_cfb8(), key1, iv, msg1, sizeof(msg1),
  7169. cipher1, sizeof(cipher1));
  7170. if (ret != 0) {
  7171. return ret;
  7172. }
  7173. #endif
  7174. ret = wc_AesSetKey(enc, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  7175. if (ret != 0)
  7176. ERROR_OUT(-5226, out);
  7177. #ifdef HAVE_AES_DECRYPT
  7178. /* decrypt uses AES_ENCRYPTION */
  7179. ret = wc_AesSetKey(dec, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  7180. if (ret != 0)
  7181. ERROR_OUT(-5227, out);
  7182. #endif
  7183. XMEMSET(cipher, 0, sizeof(cipher));
  7184. ret = wc_AesCfb8Encrypt(enc, cipher, msg1, sizeof(msg1));
  7185. if (ret != 0)
  7186. ERROR_OUT(-5228, out);
  7187. if (XMEMCMP(cipher, cipher1, sizeof(cipher1)) != 0)
  7188. ERROR_OUT(-5229, out);
  7189. #ifdef HAVE_AES_DECRYPT
  7190. ret = wc_AesCfb8Decrypt(dec, plain, cipher, sizeof(msg1));
  7191. if (ret != 0)
  7192. ERROR_OUT(-5230, out);
  7193. if (XMEMCMP(plain, msg1, sizeof(msg1)) != 0)
  7194. ERROR_OUT(-5231, out);
  7195. #endif /* HAVE_AES_DECRYPT */
  7196. #endif /* WOLFSSL_AES_128 */
  7197. #ifdef WOLFSSL_AES_192
  7198. /* 192 key tests */
  7199. ret = wc_AesSetKey(enc, key2, sizeof(key2), iv2, AES_ENCRYPTION);
  7200. if (ret != 0)
  7201. ERROR_OUT(-5232, out);
  7202. XMEMSET(cipher, 0, sizeof(cipher));
  7203. ret = wc_AesCfb8Encrypt(enc, cipher, msg2, sizeof(msg2));
  7204. if (ret != 0)
  7205. ERROR_OUT(-5233, out);
  7206. if (XMEMCMP(cipher, cipher2, sizeof(msg2)) != 0)
  7207. ERROR_OUT(-5234, out);
  7208. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  7209. ret = EVP_test(EVP_aes_192_cfb8(), key2, iv2, msg2, sizeof(msg2),
  7210. cipher2, sizeof(msg2));
  7211. if (ret != 0) {
  7212. return ret;
  7213. }
  7214. #endif
  7215. #endif /* WOLFSSL_AES_192 */
  7216. #ifdef WOLFSSL_AES_256
  7217. /* 256 key tests */
  7218. ret = wc_AesSetKey(enc, key3, sizeof(key3), iv3, AES_ENCRYPTION);
  7219. if (ret != 0)
  7220. ERROR_OUT(-5235, out);
  7221. XMEMSET(cipher, 0, sizeof(cipher));
  7222. ret = wc_AesCfb8Encrypt(enc, cipher, msg3, sizeof(msg3));
  7223. if (ret != 0)
  7224. ERROR_OUT(-5236, out);
  7225. if (XMEMCMP(cipher, cipher3, sizeof(cipher3)) != 0)
  7226. ERROR_OUT(-5237, out);
  7227. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  7228. ret = EVP_test(EVP_aes_256_cfb8(), key3, iv3, msg3, sizeof(msg3),
  7229. cipher3, sizeof(msg3));
  7230. if (ret != 0) {
  7231. goto out;
  7232. }
  7233. #endif
  7234. out:
  7235. if (enc_inited)
  7236. wc_AesFree(enc);
  7237. #ifdef HAVE_AES_DECRYPT
  7238. if (dec_inited)
  7239. wc_AesFree(dec);
  7240. #endif
  7241. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7242. if (enc)
  7243. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  7244. #ifdef HAVE_AES_DECRYPT
  7245. if (dec)
  7246. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  7247. #endif
  7248. #endif
  7249. #endif /* WOLFSSL_AES_256 */
  7250. return ret;
  7251. }
  7252. #endif /* !HAVE_SELFTEST && !HAVE_FIPS */
  7253. #endif /* WOLFSSL_AES_CFB */
  7254. static int aes_key_size_test(void)
  7255. {
  7256. int ret;
  7257. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7258. Aes *aes;
  7259. #else
  7260. Aes aes[1];
  7261. #endif
  7262. byte key16[] = { 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  7263. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66 };
  7264. #ifndef WOLFSSL_CRYPTOCELL
  7265. byte key24[] = { 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  7266. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  7267. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37 };
  7268. #endif
  7269. byte key32[] = { 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  7270. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  7271. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  7272. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66 };
  7273. byte iv[] = "1234567890abcdef";
  7274. #ifndef HAVE_FIPS
  7275. word32 keySize;
  7276. #endif
  7277. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7278. if ((aes = (Aes *)XMALLOC(sizeof *aes, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  7279. return -5315;
  7280. #endif
  7281. #if !defined(HAVE_FIPS) || \
  7282. defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)
  7283. /* w/ FIPS v1 (cert 2425) wc_AesInit just returns 0 always as it's not
  7284. * supported with that FIPS version */
  7285. ret = wc_AesInit(NULL, HEAP_HINT, devId);
  7286. if (ret != BAD_FUNC_ARG)
  7287. ERROR_OUT(-5300, out);
  7288. #endif
  7289. ret = wc_AesInit(aes, HEAP_HINT, devId);
  7290. /* 0 check OK for FIPSv1 */
  7291. if (ret != 0)
  7292. ERROR_OUT(-5301, out);
  7293. #ifndef HAVE_FIPS
  7294. /* Parameter Validation testing. */
  7295. ret = wc_AesGetKeySize(NULL, NULL);
  7296. if (ret != BAD_FUNC_ARG)
  7297. ERROR_OUT(-5302, out);
  7298. ret = wc_AesGetKeySize(aes, NULL);
  7299. if (ret != BAD_FUNC_ARG)
  7300. ERROR_OUT(-5303, out);
  7301. ret = wc_AesGetKeySize(NULL, &keySize);
  7302. if (ret != BAD_FUNC_ARG)
  7303. ERROR_OUT(-5304, out);
  7304. /* Crashes in FIPS */
  7305. ret = wc_AesSetKey(NULL, key16, sizeof(key16), iv, AES_ENCRYPTION);
  7306. if (ret != BAD_FUNC_ARG)
  7307. ERROR_OUT(-5305, out);
  7308. #endif
  7309. /* NULL IV indicates to use all zeros IV. */
  7310. ret = wc_AesSetKey(aes, key16, sizeof(key16), NULL, AES_ENCRYPTION);
  7311. #ifdef WOLFSSL_AES_128
  7312. if (ret != 0)
  7313. #else
  7314. if (ret != BAD_FUNC_ARG)
  7315. #endif
  7316. ERROR_OUT(-5306, out);
  7317. ret = wc_AesSetKey(aes, key32, sizeof(key32) - 1, iv, AES_ENCRYPTION);
  7318. if (ret != BAD_FUNC_ARG)
  7319. ERROR_OUT(-5307, out);
  7320. /* CryptoCell handles rounds internally */
  7321. #if !defined(HAVE_FIPS) && !defined(WOLFSSL_CRYPTOCELL)
  7322. /* PSA don't use aes->rounds */
  7323. #if !defined(WOLFSSL_HAVE_PSA) || defined(WOLFSSL_PSA_NO_AES)
  7324. /* Force invalid rounds */
  7325. aes->rounds = 16;
  7326. ret = wc_AesGetKeySize(aes, &keySize);
  7327. if (ret != BAD_FUNC_ARG)
  7328. ERROR_OUT(-5308, out);
  7329. #endif
  7330. #endif
  7331. ret = wc_AesSetKey(aes, key16, sizeof(key16), iv, AES_ENCRYPTION);
  7332. #ifdef WOLFSSL_AES_128
  7333. if (ret != 0)
  7334. #else
  7335. if (ret != BAD_FUNC_ARG)
  7336. #endif
  7337. ERROR_OUT(-5309, out);
  7338. #if !defined(HAVE_FIPS) && defined(WOLFSSL_AES_128)
  7339. ret = wc_AesGetKeySize(aes, &keySize);
  7340. if (ret != 0 || keySize != sizeof(key16))
  7341. ERROR_OUT(-5310, out);
  7342. #endif
  7343. #ifndef WOLFSSL_CRYPTOCELL
  7344. /* Cryptocell only supports AES-128 key size */
  7345. ret = wc_AesSetKey(aes, key24, sizeof(key24), iv, AES_ENCRYPTION);
  7346. #ifdef WOLFSSL_AES_192
  7347. if (ret != 0)
  7348. #else
  7349. if (ret != BAD_FUNC_ARG)
  7350. #endif
  7351. ERROR_OUT(-5311, out);
  7352. #if !defined(HAVE_FIPS) && defined(WOLFSSL_AES_192)
  7353. ret = wc_AesGetKeySize(aes, &keySize);
  7354. if (ret != 0 || keySize != sizeof(key24))
  7355. ERROR_OUT(-5312, out);
  7356. #endif
  7357. ret = wc_AesSetKey(aes, key32, sizeof(key32), iv, AES_ENCRYPTION);
  7358. #ifdef WOLFSSL_AES_256
  7359. if (ret != 0)
  7360. #else
  7361. if (ret != BAD_FUNC_ARG)
  7362. #endif
  7363. ERROR_OUT(-5313, out);
  7364. #if !defined(HAVE_FIPS) && defined(WOLFSSL_AES_256)
  7365. ret = wc_AesGetKeySize(aes, &keySize);
  7366. if (ret != 0 || keySize != sizeof(key32))
  7367. ERROR_OUT(-5314, out);
  7368. #endif
  7369. #endif /* !WOLFSSL_CRYPTOCELL */
  7370. ret = 0; /* success */
  7371. out:
  7372. wc_AesFree(aes);
  7373. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7374. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_AES);
  7375. #endif
  7376. return ret;
  7377. }
  7378. #if defined(WOLFSSL_AES_XTS)
  7379. /* test vectors from http://csrc.nist.gov/groups/STM/cavp/block-cipher-modes.html */
  7380. #ifdef WOLFSSL_AES_128
  7381. static int aes_xts_128_test(void)
  7382. {
  7383. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7384. XtsAes *aes = NULL;
  7385. #else
  7386. XtsAes aes[1];
  7387. #endif
  7388. int aes_inited = 0;
  7389. int ret = 0;
  7390. unsigned char buf[AES_BLOCK_SIZE * 2];
  7391. unsigned char cipher[AES_BLOCK_SIZE * 2];
  7392. /* 128 key tests */
  7393. WOLFSSL_SMALL_STACK_STATIC unsigned char k1[] = {
  7394. 0xa1, 0xb9, 0x0c, 0xba, 0x3f, 0x06, 0xac, 0x35,
  7395. 0x3b, 0x2c, 0x34, 0x38, 0x76, 0x08, 0x17, 0x62,
  7396. 0x09, 0x09, 0x23, 0x02, 0x6e, 0x91, 0x77, 0x18,
  7397. 0x15, 0xf2, 0x9d, 0xab, 0x01, 0x93, 0x2f, 0x2f
  7398. };
  7399. WOLFSSL_SMALL_STACK_STATIC unsigned char i1[] = {
  7400. 0x4f, 0xae, 0xf7, 0x11, 0x7c, 0xda, 0x59, 0xc6,
  7401. 0x6e, 0x4b, 0x92, 0x01, 0x3e, 0x76, 0x8a, 0xd5
  7402. };
  7403. WOLFSSL_SMALL_STACK_STATIC unsigned char p1[] = {
  7404. 0xeb, 0xab, 0xce, 0x95, 0xb1, 0x4d, 0x3c, 0x8d,
  7405. 0x6f, 0xb3, 0x50, 0x39, 0x07, 0x90, 0x31, 0x1c
  7406. };
  7407. /* plain text test of partial block is not from NIST test vector list */
  7408. WOLFSSL_SMALL_STACK_STATIC unsigned char pp[] = {
  7409. 0xeb, 0xab, 0xce, 0x95, 0xb1, 0x4d, 0x3c, 0x8d,
  7410. 0x6f, 0xb3, 0x50, 0x39, 0x07, 0x90, 0x31, 0x1c,
  7411. 0x6e, 0x4b, 0x92, 0x01, 0x3e, 0x76, 0x8a, 0xd5
  7412. };
  7413. WOLFSSL_SMALL_STACK_STATIC unsigned char c1[] = {
  7414. 0x77, 0x8a, 0xe8, 0xb4, 0x3c, 0xb9, 0x8d, 0x5a,
  7415. 0x82, 0x50, 0x81, 0xd5, 0xbe, 0x47, 0x1c, 0x63
  7416. };
  7417. WOLFSSL_SMALL_STACK_STATIC unsigned char k2[] = {
  7418. 0x39, 0x25, 0x79, 0x05, 0xdf, 0xcc, 0x77, 0x76,
  7419. 0x6c, 0x87, 0x0a, 0x80, 0x6a, 0x60, 0xe3, 0xc0,
  7420. 0x93, 0xd1, 0x2a, 0xcf, 0xcb, 0x51, 0x42, 0xfa,
  7421. 0x09, 0x69, 0x89, 0x62, 0x5b, 0x60, 0xdb, 0x16
  7422. };
  7423. WOLFSSL_SMALL_STACK_STATIC unsigned char i2[] = {
  7424. 0x5c, 0xf7, 0x9d, 0xb6, 0xc5, 0xcd, 0x99, 0x1a,
  7425. 0x1c, 0x78, 0x81, 0x42, 0x24, 0x95, 0x1e, 0x84
  7426. };
  7427. WOLFSSL_SMALL_STACK_STATIC unsigned char p2[] = {
  7428. 0xbd, 0xc5, 0x46, 0x8f, 0xbc, 0x8d, 0x50, 0xa1,
  7429. 0x0d, 0x1c, 0x85, 0x7f, 0x79, 0x1c, 0x5c, 0xba,
  7430. 0xb3, 0x81, 0x0d, 0x0d, 0x73, 0xcf, 0x8f, 0x20,
  7431. 0x46, 0xb1, 0xd1, 0x9e, 0x7d, 0x5d, 0x8a, 0x56
  7432. };
  7433. WOLFSSL_SMALL_STACK_STATIC unsigned char c2[] = {
  7434. 0xd6, 0xbe, 0x04, 0x6d, 0x41, 0xf2, 0x3b, 0x5e,
  7435. 0xd7, 0x0b, 0x6b, 0x3d, 0x5c, 0x8e, 0x66, 0x23,
  7436. 0x2b, 0xe6, 0xb8, 0x07, 0xd4, 0xdc, 0xc6, 0x0e,
  7437. 0xff, 0x8d, 0xbc, 0x1d, 0x9f, 0x7f, 0xc8, 0x22
  7438. };
  7439. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7440. if ((aes = (XtsAes *)XMALLOC(sizeof *aes, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  7441. ERROR_OUT(-5417, out);
  7442. #endif
  7443. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  7444. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  7445. ret = EVP_test(EVP_aes_128_xts(), k2, i2, p2, sizeof(p2), c2, sizeof(c2));
  7446. if (ret != 0) {
  7447. printf("EVP_aes_128_xts failed!\n");
  7448. goto out;
  7449. }
  7450. #endif
  7451. XMEMSET(buf, 0, sizeof(buf));
  7452. if (wc_AesXtsSetKey(aes, k2, sizeof(k2), AES_ENCRYPTION,
  7453. HEAP_HINT, devId) != 0)
  7454. ERROR_OUT(-5400, out);
  7455. else
  7456. aes_inited = 1;
  7457. ret = wc_AesXtsEncrypt(aes, buf, p2, sizeof(p2), i2, sizeof(i2));
  7458. #if defined(WOLFSSL_ASYNC_CRYPT)
  7459. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7460. #endif
  7461. if (ret != 0)
  7462. ERROR_OUT(-5401, out);
  7463. if (XMEMCMP(c2, buf, sizeof(c2)))
  7464. ERROR_OUT(-5402, out);
  7465. XMEMSET(buf, 0, sizeof(buf));
  7466. wc_AesXtsFree(aes);
  7467. if (wc_AesXtsSetKey(aes, k1, sizeof(k1), AES_ENCRYPTION,
  7468. HEAP_HINT, devId) != 0)
  7469. ERROR_OUT(-5403, out);
  7470. ret = wc_AesXtsEncrypt(aes, buf, p1, sizeof(p1), i1, sizeof(i1));
  7471. #if defined(WOLFSSL_ASYNC_CRYPT)
  7472. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7473. #endif
  7474. if (ret != 0)
  7475. ERROR_OUT(-5404, out);
  7476. if (XMEMCMP(c1, buf, AES_BLOCK_SIZE))
  7477. ERROR_OUT(-5405, out);
  7478. /* partial block encryption test */
  7479. XMEMSET(cipher, 0, sizeof(cipher));
  7480. ret = wc_AesXtsEncrypt(aes, cipher, pp, sizeof(pp), i1, sizeof(i1));
  7481. #if defined(WOLFSSL_ASYNC_CRYPT)
  7482. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7483. #endif
  7484. if (ret != 0)
  7485. ERROR_OUT(-5406, out);
  7486. wc_AesXtsFree(aes);
  7487. /* partial block decrypt test */
  7488. XMEMSET(buf, 0, sizeof(buf));
  7489. if (wc_AesXtsSetKey(aes, k1, sizeof(k1), AES_DECRYPTION,
  7490. HEAP_HINT, devId) != 0)
  7491. ERROR_OUT(-5407, out);
  7492. ret = wc_AesXtsDecrypt(aes, buf, cipher, sizeof(pp), i1, sizeof(i1));
  7493. #if defined(WOLFSSL_ASYNC_CRYPT)
  7494. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7495. #endif
  7496. if (ret != 0)
  7497. ERROR_OUT(-5408, out);
  7498. if (XMEMCMP(pp, buf, sizeof(pp)))
  7499. ERROR_OUT(-5409, out);
  7500. /* NIST decrypt test vector */
  7501. XMEMSET(buf, 0, sizeof(buf));
  7502. ret = wc_AesXtsDecrypt(aes, buf, c1, sizeof(c1), i1, sizeof(i1));
  7503. #if defined(WOLFSSL_ASYNC_CRYPT)
  7504. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7505. #endif
  7506. if (ret != 0)
  7507. ERROR_OUT(-5410, out);
  7508. if (XMEMCMP(p1, buf, AES_BLOCK_SIZE))
  7509. ERROR_OUT(-5411, out);
  7510. /* fail case with decrypting using wrong key */
  7511. XMEMSET(buf, 0, sizeof(buf));
  7512. ret = wc_AesXtsDecrypt(aes, buf, c2, sizeof(c2), i2, sizeof(i2));
  7513. #if defined(WOLFSSL_ASYNC_CRYPT)
  7514. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7515. #endif
  7516. if (ret != 0)
  7517. ERROR_OUT(-5412, out);
  7518. if (XMEMCMP(p2, buf, sizeof(p2)) == 0) /* fail case with wrong key */
  7519. ERROR_OUT(-5413, out);
  7520. wc_AesXtsFree(aes);
  7521. /* set correct key and retest */
  7522. XMEMSET(buf, 0, sizeof(buf));
  7523. if (wc_AesXtsSetKey(aes, k2, sizeof(k2), AES_DECRYPTION,
  7524. HEAP_HINT, devId) != 0)
  7525. ERROR_OUT(-5414, out);
  7526. ret = wc_AesXtsDecrypt(aes, buf, c2, sizeof(c2), i2, sizeof(i2));
  7527. #if defined(WOLFSSL_ASYNC_CRYPT)
  7528. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7529. #endif
  7530. if (ret != 0)
  7531. ERROR_OUT(-5415, out);
  7532. if (XMEMCMP(p2, buf, sizeof(p2)))
  7533. ERROR_OUT(-5416, out);
  7534. out:
  7535. if (aes_inited)
  7536. wc_AesXtsFree(aes);
  7537. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7538. if (aes)
  7539. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_AES);
  7540. #endif
  7541. return ret;
  7542. }
  7543. #endif /* WOLFSSL_AES_128 */
  7544. #ifdef WOLFSSL_AES_256
  7545. static int aes_xts_256_test(void)
  7546. {
  7547. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7548. XtsAes *aes = NULL;
  7549. #else
  7550. XtsAes aes[1];
  7551. #endif
  7552. int aes_inited = 0;
  7553. int ret = 0;
  7554. unsigned char buf[AES_BLOCK_SIZE * 3];
  7555. unsigned char cipher[AES_BLOCK_SIZE * 3];
  7556. /* 256 key tests */
  7557. WOLFSSL_SMALL_STACK_STATIC unsigned char k1[] = {
  7558. 0x1e, 0xa6, 0x61, 0xc5, 0x8d, 0x94, 0x3a, 0x0e,
  7559. 0x48, 0x01, 0xe4, 0x2f, 0x4b, 0x09, 0x47, 0x14,
  7560. 0x9e, 0x7f, 0x9f, 0x8e, 0x3e, 0x68, 0xd0, 0xc7,
  7561. 0x50, 0x52, 0x10, 0xbd, 0x31, 0x1a, 0x0e, 0x7c,
  7562. 0xd6, 0xe1, 0x3f, 0xfd, 0xf2, 0x41, 0x8d, 0x8d,
  7563. 0x19, 0x11, 0xc0, 0x04, 0xcd, 0xa5, 0x8d, 0xa3,
  7564. 0xd6, 0x19, 0xb7, 0xe2, 0xb9, 0x14, 0x1e, 0x58,
  7565. 0x31, 0x8e, 0xea, 0x39, 0x2c, 0xf4, 0x1b, 0x08
  7566. };
  7567. WOLFSSL_SMALL_STACK_STATIC unsigned char i1[] = {
  7568. 0xad, 0xf8, 0xd9, 0x26, 0x27, 0x46, 0x4a, 0xd2,
  7569. 0xf0, 0x42, 0x8e, 0x84, 0xa9, 0xf8, 0x75, 0x64
  7570. };
  7571. WOLFSSL_SMALL_STACK_STATIC unsigned char p1[] = {
  7572. 0x2e, 0xed, 0xea, 0x52, 0xcd, 0x82, 0x15, 0xe1,
  7573. 0xac, 0xc6, 0x47, 0xe8, 0x10, 0xbb, 0xc3, 0x64,
  7574. 0x2e, 0x87, 0x28, 0x7f, 0x8d, 0x2e, 0x57, 0xe3,
  7575. 0x6c, 0x0a, 0x24, 0xfb, 0xc1, 0x2a, 0x20, 0x2e
  7576. };
  7577. /* plain text test of partial block is not from NIST test vector list */
  7578. WOLFSSL_SMALL_STACK_STATIC unsigned char pp[] = {
  7579. 0xeb, 0xab, 0xce, 0x95, 0xb1, 0x4d, 0x3c, 0x8d,
  7580. 0x6f, 0xb3, 0x50, 0x39, 0x07, 0x90, 0x31, 0x1c,
  7581. 0x6e, 0x4b, 0x92, 0x01, 0x3e, 0x76, 0x8a, 0xd5
  7582. };
  7583. WOLFSSL_SMALL_STACK_STATIC unsigned char c1[] = {
  7584. 0xcb, 0xaa, 0xd0, 0xe2, 0xf6, 0xce, 0xa3, 0xf5,
  7585. 0x0b, 0x37, 0xf9, 0x34, 0xd4, 0x6a, 0x9b, 0x13,
  7586. 0x0b, 0x9d, 0x54, 0xf0, 0x7e, 0x34, 0xf3, 0x6a,
  7587. 0xf7, 0x93, 0xe8, 0x6f, 0x73, 0xc6, 0xd7, 0xdb
  7588. };
  7589. WOLFSSL_SMALL_STACK_STATIC unsigned char k2[] = {
  7590. 0xad, 0x50, 0x4b, 0x85, 0xd7, 0x51, 0xbf, 0xba,
  7591. 0x69, 0x13, 0xb4, 0xcc, 0x79, 0xb6, 0x5a, 0x62,
  7592. 0xf7, 0xf3, 0x9d, 0x36, 0x0f, 0x35, 0xb5, 0xec,
  7593. 0x4a, 0x7e, 0x95, 0xbd, 0x9b, 0xa5, 0xf2, 0xec,
  7594. 0xc1, 0xd7, 0x7e, 0xa3, 0xc3, 0x74, 0xbd, 0x4b,
  7595. 0x13, 0x1b, 0x07, 0x83, 0x87, 0xdd, 0x55, 0x5a,
  7596. 0xb5, 0xb0, 0xc7, 0xe5, 0x2d, 0xb5, 0x06, 0x12,
  7597. 0xd2, 0xb5, 0x3a, 0xcb, 0x47, 0x8a, 0x53, 0xb4
  7598. };
  7599. WOLFSSL_SMALL_STACK_STATIC unsigned char i2[] = {
  7600. 0xe6, 0x42, 0x19, 0xed, 0xe0, 0xe1, 0xc2, 0xa0,
  7601. 0x0e, 0xf5, 0x58, 0x6a, 0xc4, 0x9b, 0xeb, 0x6f
  7602. };
  7603. WOLFSSL_SMALL_STACK_STATIC unsigned char p2[] = {
  7604. 0x24, 0xcb, 0x76, 0x22, 0x55, 0xb5, 0xa8, 0x00,
  7605. 0xf4, 0x6e, 0x80, 0x60, 0x56, 0x9e, 0x05, 0x53,
  7606. 0xbc, 0xfe, 0x86, 0x55, 0x3b, 0xca, 0xd5, 0x89,
  7607. 0xc7, 0x54, 0x1a, 0x73, 0xac, 0xc3, 0x9a, 0xbd,
  7608. 0x53, 0xc4, 0x07, 0x76, 0xd8, 0xe8, 0x22, 0x61,
  7609. 0x9e, 0xa9, 0xad, 0x77, 0xa0, 0x13, 0x4c, 0xfc
  7610. };
  7611. WOLFSSL_SMALL_STACK_STATIC unsigned char c2[] = {
  7612. 0xa3, 0xc6, 0xf3, 0xf3, 0x82, 0x79, 0x5b, 0x10,
  7613. 0x87, 0xd7, 0x02, 0x50, 0xdb, 0x2c, 0xd3, 0xb1,
  7614. 0xa1, 0x62, 0xa8, 0xb6, 0xdc, 0x12, 0x60, 0x61,
  7615. 0xc1, 0x0a, 0x84, 0xa5, 0x85, 0x3f, 0x3a, 0x89,
  7616. 0xe6, 0x6c, 0xdb, 0xb7, 0x9a, 0xb4, 0x28, 0x9b,
  7617. 0xc3, 0xea, 0xd8, 0x10, 0xe9, 0xc0, 0xaf, 0x92
  7618. };
  7619. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7620. if ((aes = (XtsAes *)XMALLOC(sizeof *aes, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  7621. ERROR_OUT(-5515, out);
  7622. #endif
  7623. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  7624. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  7625. ret = EVP_test(EVP_aes_256_xts(), k2, i2, p2, sizeof(p2), c2, sizeof(c2));
  7626. if (ret != 0) {
  7627. printf("EVP_aes_256_xts failed\n");
  7628. goto out;
  7629. }
  7630. #endif
  7631. XMEMSET(buf, 0, sizeof(buf));
  7632. if (wc_AesXtsSetKey(aes, k2, sizeof(k2), AES_ENCRYPTION,
  7633. HEAP_HINT, devId) != 0)
  7634. ERROR_OUT(-5500, out);
  7635. else
  7636. aes_inited = 1;
  7637. ret = wc_AesXtsEncrypt(aes, buf, p2, sizeof(p2), i2, sizeof(i2));
  7638. #if defined(WOLFSSL_ASYNC_CRYPT)
  7639. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7640. #endif
  7641. if (ret != 0)
  7642. ERROR_OUT(-5501, out);
  7643. if (XMEMCMP(c2, buf, sizeof(c2)))
  7644. ERROR_OUT(-5502, out);
  7645. wc_AesXtsFree(aes);
  7646. XMEMSET(buf, 0, sizeof(buf));
  7647. if (wc_AesXtsSetKey(aes, k1, sizeof(k1), AES_ENCRYPTION,
  7648. HEAP_HINT, devId) != 0)
  7649. ERROR_OUT(-5503, out);
  7650. ret = wc_AesXtsEncrypt(aes, buf, p1, sizeof(p1), i1, sizeof(i1));
  7651. #if defined(WOLFSSL_ASYNC_CRYPT)
  7652. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7653. #endif
  7654. if (ret != 0)
  7655. ERROR_OUT(-5504, out);
  7656. if (XMEMCMP(c1, buf, AES_BLOCK_SIZE))
  7657. ERROR_OUT(-5505, out);
  7658. /* partial block encryption test */
  7659. XMEMSET(cipher, 0, sizeof(cipher));
  7660. ret = wc_AesXtsEncrypt(aes, cipher, pp, sizeof(pp), i1, sizeof(i1));
  7661. #if defined(WOLFSSL_ASYNC_CRYPT)
  7662. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7663. #endif
  7664. if (ret != 0)
  7665. ERROR_OUT(-5506, out);
  7666. wc_AesXtsFree(aes);
  7667. /* partial block decrypt test */
  7668. XMEMSET(buf, 0, sizeof(buf));
  7669. if (wc_AesXtsSetKey(aes, k1, sizeof(k1), AES_DECRYPTION,
  7670. HEAP_HINT, devId) != 0)
  7671. ERROR_OUT(-5507, out);
  7672. ret = wc_AesXtsDecrypt(aes, buf, cipher, sizeof(pp), i1, sizeof(i1));
  7673. #if defined(WOLFSSL_ASYNC_CRYPT)
  7674. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7675. #endif
  7676. if (ret != 0)
  7677. ERROR_OUT(-5508, out);
  7678. if (XMEMCMP(pp, buf, sizeof(pp)))
  7679. ERROR_OUT(-5509, out);
  7680. /* NIST decrypt test vector */
  7681. XMEMSET(buf, 0, sizeof(buf));
  7682. ret = wc_AesXtsDecrypt(aes, buf, c1, sizeof(c1), i1, sizeof(i1));
  7683. #if defined(WOLFSSL_ASYNC_CRYPT)
  7684. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7685. #endif
  7686. if (ret != 0)
  7687. ERROR_OUT(-5510, out);
  7688. if (XMEMCMP(p1, buf, AES_BLOCK_SIZE))
  7689. ERROR_OUT(-5511, out);
  7690. wc_AesXtsFree(aes);
  7691. XMEMSET(buf, 0, sizeof(buf));
  7692. if (wc_AesXtsSetKey(aes, k2, sizeof(k2), AES_DECRYPTION,
  7693. HEAP_HINT, devId) != 0)
  7694. ERROR_OUT(-5512, out);
  7695. ret = wc_AesXtsDecrypt(aes, buf, c2, sizeof(c2), i2, sizeof(i2));
  7696. #if defined(WOLFSSL_ASYNC_CRYPT)
  7697. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7698. #endif
  7699. if (ret != 0)
  7700. ERROR_OUT(-5513, out);
  7701. if (XMEMCMP(p2, buf, sizeof(p2)))
  7702. ERROR_OUT(-5514, out);
  7703. out:
  7704. if (aes_inited)
  7705. wc_AesXtsFree(aes);
  7706. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7707. if (aes)
  7708. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_AES);
  7709. #endif
  7710. return ret;
  7711. }
  7712. #endif /* WOLFSSL_AES_256 */
  7713. #if defined(WOLFSSL_AES_128) && defined(WOLFSSL_AES_256)
  7714. /* both 128 and 256 bit key test */
  7715. static int aes_xts_sector_test(void)
  7716. {
  7717. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7718. XtsAes *aes = NULL;
  7719. #else
  7720. XtsAes aes[1];
  7721. #endif
  7722. int aes_inited = 0;
  7723. int ret = 0;
  7724. unsigned char buf[AES_BLOCK_SIZE * 2];
  7725. /* 128 key tests */
  7726. WOLFSSL_SMALL_STACK_STATIC unsigned char k1[] = {
  7727. 0xa3, 0xe4, 0x0d, 0x5b, 0xd4, 0xb6, 0xbb, 0xed,
  7728. 0xb2, 0xd1, 0x8c, 0x70, 0x0a, 0xd2, 0xdb, 0x22,
  7729. 0x10, 0xc8, 0x11, 0x90, 0x64, 0x6d, 0x67, 0x3c,
  7730. 0xbc, 0xa5, 0x3f, 0x13, 0x3e, 0xab, 0x37, 0x3c
  7731. };
  7732. WOLFSSL_SMALL_STACK_STATIC unsigned char p1[] = {
  7733. 0x20, 0xe0, 0x71, 0x94, 0x05, 0x99, 0x3f, 0x09,
  7734. 0xa6, 0x6a, 0xe5, 0xbb, 0x50, 0x0e, 0x56, 0x2c
  7735. };
  7736. WOLFSSL_SMALL_STACK_STATIC unsigned char c1[] = {
  7737. 0x74, 0x62, 0x35, 0x51, 0x21, 0x02, 0x16, 0xac,
  7738. 0x92, 0x6b, 0x96, 0x50, 0xb6, 0xd3, 0xfa, 0x52
  7739. };
  7740. word64 s1 = 141;
  7741. /* 256 key tests */
  7742. WOLFSSL_SMALL_STACK_STATIC unsigned char k2[] = {
  7743. 0xef, 0x01, 0x0c, 0xa1, 0xa3, 0x66, 0x3e, 0x32,
  7744. 0x53, 0x43, 0x49, 0xbc, 0x0b, 0xae, 0x62, 0x23,
  7745. 0x2a, 0x15, 0x73, 0x34, 0x85, 0x68, 0xfb, 0x9e,
  7746. 0xf4, 0x17, 0x68, 0xa7, 0x67, 0x4f, 0x50, 0x7a,
  7747. 0x72, 0x7f, 0x98, 0x75, 0x53, 0x97, 0xd0, 0xe0,
  7748. 0xaa, 0x32, 0xf8, 0x30, 0x33, 0x8c, 0xc7, 0xa9,
  7749. 0x26, 0xc7, 0x73, 0xf0, 0x9e, 0x57, 0xb3, 0x57,
  7750. 0xcd, 0x15, 0x6a, 0xfb, 0xca, 0x46, 0xe1, 0xa0
  7751. };
  7752. WOLFSSL_SMALL_STACK_STATIC unsigned char p2[] = {
  7753. 0xed, 0x98, 0xe0, 0x17, 0x70, 0xa8, 0x53, 0xb4,
  7754. 0x9d, 0xb9, 0xe6, 0xaa, 0xf8, 0x8f, 0x0a, 0x41,
  7755. 0xb9, 0xb5, 0x6e, 0x91, 0xa5, 0xa2, 0xb1, 0x1d,
  7756. 0x40, 0x52, 0x92, 0x54, 0xf5, 0x52, 0x3e, 0x75
  7757. };
  7758. WOLFSSL_SMALL_STACK_STATIC unsigned char c2[] = {
  7759. 0xca, 0x20, 0xc5, 0x5e, 0x8d, 0xc1, 0x49, 0x68,
  7760. 0x7d, 0x25, 0x41, 0xde, 0x39, 0xc3, 0xdf, 0x63,
  7761. 0x00, 0xbb, 0x5a, 0x16, 0x3c, 0x10, 0xce, 0xd3,
  7762. 0x66, 0x6b, 0x13, 0x57, 0xdb, 0x8b, 0xd3, 0x9d
  7763. };
  7764. word64 s2 = 187;
  7765. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7766. if ((aes = (XtsAes *)XMALLOC(sizeof *aes, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  7767. ERROR_OUT(-5612, out);
  7768. #endif
  7769. XMEMSET(buf, 0, sizeof(buf));
  7770. if (wc_AesXtsSetKey(aes, k1, sizeof(k1), AES_ENCRYPTION,
  7771. HEAP_HINT, devId) != 0)
  7772. ERROR_OUT(-5600, out);
  7773. else
  7774. aes_inited = 1;
  7775. ret = wc_AesXtsEncryptSector(aes, buf, p1, sizeof(p1), s1);
  7776. #if defined(WOLFSSL_ASYNC_CRYPT)
  7777. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7778. #endif
  7779. if (ret != 0)
  7780. ERROR_OUT(-5601, out);
  7781. if (XMEMCMP(c1, buf, AES_BLOCK_SIZE))
  7782. ERROR_OUT(-5602, out);
  7783. wc_AesXtsFree(aes);
  7784. /* decrypt test */
  7785. XMEMSET(buf, 0, sizeof(buf));
  7786. if (wc_AesXtsSetKey(aes, k1, sizeof(k1), AES_DECRYPTION,
  7787. HEAP_HINT, devId) != 0)
  7788. ERROR_OUT(-5603, out);
  7789. ret = wc_AesXtsDecryptSector(aes, buf, c1, sizeof(c1), s1);
  7790. #if defined(WOLFSSL_ASYNC_CRYPT)
  7791. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7792. #endif
  7793. if (ret != 0)
  7794. ERROR_OUT(-5604, out);
  7795. if (XMEMCMP(p1, buf, AES_BLOCK_SIZE))
  7796. ERROR_OUT(-5605, out);
  7797. wc_AesXtsFree(aes);
  7798. /* 256 bit key tests */
  7799. XMEMSET(buf, 0, sizeof(buf));
  7800. if (wc_AesXtsSetKey(aes, k2, sizeof(k2), AES_ENCRYPTION,
  7801. HEAP_HINT, devId) != 0)
  7802. ERROR_OUT(-5606, out);
  7803. ret = wc_AesXtsEncryptSector(aes, buf, p2, sizeof(p2), s2);
  7804. #if defined(WOLFSSL_ASYNC_CRYPT)
  7805. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7806. #endif
  7807. if (ret != 0)
  7808. ERROR_OUT(-5607, out);
  7809. if (XMEMCMP(c2, buf, sizeof(c2)))
  7810. ERROR_OUT(-5608, out);
  7811. wc_AesXtsFree(aes);
  7812. /* decrypt test */
  7813. XMEMSET(buf, 0, sizeof(buf));
  7814. if (wc_AesXtsSetKey(aes, k2, sizeof(k2), AES_DECRYPTION,
  7815. HEAP_HINT, devId) != 0)
  7816. ERROR_OUT(-5609, out);
  7817. ret = wc_AesXtsDecryptSector(aes, buf, c2, sizeof(c2), s2);
  7818. #if defined(WOLFSSL_ASYNC_CRYPT)
  7819. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7820. #endif
  7821. if (ret != 0)
  7822. ERROR_OUT(-5610, out);
  7823. if (XMEMCMP(p2, buf, sizeof(p2)))
  7824. ERROR_OUT(-5611, out);
  7825. out:
  7826. if (aes_inited)
  7827. wc_AesXtsFree(aes);
  7828. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7829. if (aes)
  7830. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_AES);
  7831. #endif
  7832. return ret;
  7833. }
  7834. #endif /* WOLFSSL_AES_128 && WOLFSSL_AES_256 */
  7835. #ifdef WOLFSSL_AES_128
  7836. /* testing of bad arguments */
  7837. static int aes_xts_args_test(void)
  7838. {
  7839. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7840. XtsAes *aes = NULL;
  7841. #else
  7842. XtsAes aes[1];
  7843. #endif
  7844. int aes_inited = 0;
  7845. int ret;
  7846. unsigned char buf[AES_BLOCK_SIZE * 2];
  7847. /* 128 key tests */
  7848. WOLFSSL_SMALL_STACK_STATIC unsigned char k1[] = {
  7849. 0xa3, 0xe4, 0x0d, 0x5b, 0xd4, 0xb6, 0xbb, 0xed,
  7850. 0xb2, 0xd1, 0x8c, 0x70, 0x0a, 0xd2, 0xdb, 0x22,
  7851. 0x10, 0xc8, 0x11, 0x90, 0x64, 0x6d, 0x67, 0x3c,
  7852. 0xbc, 0xa5, 0x3f, 0x13, 0x3e, 0xab, 0x37, 0x3c
  7853. };
  7854. WOLFSSL_SMALL_STACK_STATIC unsigned char p1[] = {
  7855. 0x20, 0xe0, 0x71, 0x94, 0x05, 0x99, 0x3f, 0x09,
  7856. 0xa6, 0x6a, 0xe5, 0xbb, 0x50, 0x0e, 0x56, 0x2c
  7857. };
  7858. WOLFSSL_SMALL_STACK_STATIC unsigned char c1[] = {
  7859. 0x74, 0x62, 0x35, 0x51, 0x21, 0x02, 0x16, 0xac,
  7860. 0x92, 0x6b, 0x96, 0x50, 0xb6, 0xd3, 0xfa, 0x52
  7861. };
  7862. word64 s1 = 141;
  7863. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7864. if ((aes = (XtsAes *)XMALLOC(sizeof *aes, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  7865. ERROR_OUT(-5708, out);
  7866. #endif
  7867. if (wc_AesXtsSetKey(NULL, k1, sizeof(k1), AES_ENCRYPTION,
  7868. HEAP_HINT, devId) == 0)
  7869. ERROR_OUT(-5700, out);
  7870. if (wc_AesXtsSetKey(aes, NULL, sizeof(k1), AES_ENCRYPTION,
  7871. HEAP_HINT, devId) == 0)
  7872. ERROR_OUT(-5701, out);
  7873. /* encryption operations */
  7874. if (wc_AesXtsSetKey(aes, k1, sizeof(k1), AES_ENCRYPTION,
  7875. HEAP_HINT, devId) != 0)
  7876. ERROR_OUT(-5702, out);
  7877. else
  7878. aes_inited = 1;
  7879. ret = wc_AesXtsEncryptSector(NULL, buf, p1, sizeof(p1), s1);
  7880. #if defined(WOLFSSL_ASYNC_CRYPT)
  7881. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7882. #endif
  7883. if (ret == 0)
  7884. ERROR_OUT(-5703, out);
  7885. ret = wc_AesXtsEncryptSector(aes, NULL, p1, sizeof(p1), s1);
  7886. #if defined(WOLFSSL_ASYNC_CRYPT)
  7887. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7888. #endif
  7889. if (ret == 0)
  7890. ERROR_OUT(-5704, out);
  7891. wc_AesXtsFree(aes);
  7892. /* decryption operations */
  7893. if (wc_AesXtsSetKey(aes, k1, sizeof(k1), AES_DECRYPTION,
  7894. HEAP_HINT, devId) != 0)
  7895. ERROR_OUT(-5705, out);
  7896. ret = wc_AesXtsDecryptSector(NULL, buf, c1, sizeof(c1), s1);
  7897. #if defined(WOLFSSL_ASYNC_CRYPT)
  7898. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7899. #endif
  7900. if (ret == 0)
  7901. ERROR_OUT(-5706, out);
  7902. ret = wc_AesXtsDecryptSector(aes, NULL, c1, sizeof(c1), s1);
  7903. #if defined(WOLFSSL_ASYNC_CRYPT)
  7904. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7905. #endif
  7906. if (ret == 0)
  7907. ERROR_OUT(-5707, out);
  7908. ret = 0;
  7909. out:
  7910. if (aes_inited)
  7911. wc_AesXtsFree(aes);
  7912. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7913. if (aes)
  7914. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_AES);
  7915. #endif
  7916. return ret;
  7917. }
  7918. #endif /* WOLFSSL_AES_128 */
  7919. #endif /* WOLFSSL_AES_XTS */
  7920. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  7921. static int aes_cbc_test(void)
  7922. {
  7923. byte cipher[AES_BLOCK_SIZE];
  7924. byte plain[AES_BLOCK_SIZE];
  7925. int ret;
  7926. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = { /* "Now is the time for all " w/o trailing 0 */
  7927. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  7928. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  7929. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  7930. };
  7931. byte key[] = "0123456789abcdef "; /* align */
  7932. byte iv[] = "1234567890abcdef "; /* align */
  7933. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  7934. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  7935. /* Parameter Validation testing. */
  7936. ret = wc_AesCbcEncryptWithKey(cipher, msg, AES_BLOCK_SIZE, key, 17, NULL);
  7937. if (ret != BAD_FUNC_ARG)
  7938. return -5800;
  7939. #ifdef HAVE_AES_DECRYPT
  7940. ret = wc_AesCbcDecryptWithKey(plain, cipher, AES_BLOCK_SIZE, key, 17, NULL);
  7941. if (ret != BAD_FUNC_ARG)
  7942. return -5801;
  7943. #endif
  7944. ret = wc_AesCbcEncryptWithKey(cipher, msg, AES_BLOCK_SIZE, key,
  7945. AES_BLOCK_SIZE, iv);
  7946. if (ret != 0)
  7947. return -5802;
  7948. #ifdef HAVE_AES_DECRYPT
  7949. ret = wc_AesCbcDecryptWithKey(plain, cipher, AES_BLOCK_SIZE, key,
  7950. AES_BLOCK_SIZE, iv);
  7951. if (ret != 0)
  7952. return -5803;
  7953. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE) != 0)
  7954. return -5804;
  7955. #endif /* HAVE_AES_DECRYPT */
  7956. (void)plain;
  7957. return 0;
  7958. }
  7959. #endif
  7960. #if defined(HAVE_AES_ECB) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  7961. static int aesecb_test(void)
  7962. {
  7963. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7964. Aes *enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  7965. #else
  7966. Aes enc[1];
  7967. #endif
  7968. byte cipher[AES_BLOCK_SIZE * 4];
  7969. #ifdef HAVE_AES_DECRYPT
  7970. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7971. Aes *dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  7972. #else
  7973. Aes dec[1];
  7974. #endif
  7975. byte plain [AES_BLOCK_SIZE * 4];
  7976. #endif /* HAVE_AES_DECRYPT */
  7977. int ret = 0;
  7978. #if defined(WOLFSSL_AES_256)
  7979. {
  7980. WOLFSSL_SMALL_STACK_STATIC const byte niPlain[] =
  7981. {
  7982. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  7983. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  7984. };
  7985. WOLFSSL_SMALL_STACK_STATIC const byte niCipher[] =
  7986. {
  7987. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  7988. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  7989. };
  7990. WOLFSSL_SMALL_STACK_STATIC const byte niKey[] =
  7991. {
  7992. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  7993. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  7994. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  7995. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  7996. };
  7997. if (wc_AesInit(enc, HEAP_HINT, devId) != 0)
  7998. ERROR_OUT(-5900, out);
  7999. #if defined(HAVE_AES_DECRYPT)
  8000. if (wc_AesInit(dec, HEAP_HINT, devId) != 0)
  8001. ERROR_OUT(-5901, out);
  8002. #endif
  8003. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  8004. ret = wc_AesSetKey(enc, niKey, sizeof(niKey), cipher, AES_ENCRYPTION);
  8005. if (ret != 0)
  8006. ERROR_OUT(-5923, out);
  8007. if (wc_AesEcbEncrypt(enc, cipher, niPlain, AES_BLOCK_SIZE) != 0)
  8008. ERROR_OUT(-5924, out);
  8009. if (XMEMCMP(cipher, niCipher, AES_BLOCK_SIZE) != 0)
  8010. ERROR_OUT(-5925, out);
  8011. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  8012. ret = wc_AesSetKey(dec, niKey, sizeof(niKey), plain, AES_DECRYPTION);
  8013. if (ret != 0)
  8014. ERROR_OUT(-5926, out);
  8015. if (wc_AesEcbDecrypt(dec, plain, niCipher, AES_BLOCK_SIZE) != 0)
  8016. ERROR_OUT(-5927, out);
  8017. wc_AesEcbDecrypt(dec, plain, niCipher, AES_BLOCK_SIZE);
  8018. if (XMEMCMP(plain, niPlain, AES_BLOCK_SIZE) != 0)
  8019. ERROR_OUT(-5928, out);
  8020. }
  8021. wc_AesFree(enc);
  8022. #ifdef HAVE_AES_DECRYPT
  8023. wc_AesFree(dec);
  8024. #endif
  8025. out:
  8026. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8027. if (enc)
  8028. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  8029. #ifdef HAVE_AES_DECRYPT
  8030. if (dec)
  8031. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  8032. #endif
  8033. #endif
  8034. #endif /* WOLFSSL_AES_256 */
  8035. return ret;
  8036. }
  8037. #endif /* HAVE_AES_ECB */
  8038. #ifdef WOLFSSL_AES_COUNTER
  8039. static int aesctr_test(Aes* enc, Aes* dec, byte* cipher, byte* plain)
  8040. {
  8041. int ret;
  8042. /* test vectors from "Recommendation for Block Cipher Modes of
  8043. * Operation" NIST Special Publication 800-38A */
  8044. WOLFSSL_SMALL_STACK_STATIC const byte ctrIv[] =
  8045. {
  8046. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  8047. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  8048. };
  8049. WOLFSSL_SMALL_STACK_STATIC const byte ctrPlain[] =
  8050. {
  8051. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  8052. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  8053. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  8054. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  8055. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  8056. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  8057. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  8058. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  8059. };
  8060. #ifdef WOLFSSL_ARMASM
  8061. WOLFSSL_SMALL_STACK_STATIC const byte ctrIvWrap32[] =
  8062. {
  8063. 0xff,0xff,0xff,0xff,0x0f,0xff,0xff,0xff,
  8064. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff
  8065. };
  8066. WOLFSSL_SMALL_STACK_STATIC const byte ctrIvWrap32_2[] =
  8067. {
  8068. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  8069. 0xff,0xff,0xff,0xff,0x0f,0xff,0xff,0xfe
  8070. };
  8071. WOLFSSL_SMALL_STACK_STATIC const byte ctrIvWrap64[] =
  8072. {
  8073. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  8074. 0x0f,0xff,0xff,0xff,0xff,0xff,0xff,0xff
  8075. };
  8076. WOLFSSL_SMALL_STACK_STATIC const byte ctrIvWrap64_2[] =
  8077. {
  8078. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xf0,
  8079. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfe
  8080. };
  8081. WOLFSSL_SMALL_STACK_STATIC const byte ctrIvWrap96[] =
  8082. {
  8083. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  8084. 0xff,0xff,0xff,0xff,0x0f,0xff,0xff,0xff
  8085. };
  8086. WOLFSSL_SMALL_STACK_STATIC const byte ctrIvWrap96_2[] =
  8087. {
  8088. 0xff,0xff,0xff,0xf0,0xff,0xff,0xff,0xff,
  8089. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfe
  8090. };
  8091. #endif
  8092. WOLFSSL_SMALL_STACK_STATIC const byte ctrIvWrap128[] =
  8093. {
  8094. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  8095. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff
  8096. };
  8097. #ifdef WOLFSSL_ARMASM
  8098. WOLFSSL_SMALL_STACK_STATIC const byte ctrIvWrap128_2[] =
  8099. {
  8100. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  8101. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfe
  8102. };
  8103. #endif
  8104. #ifdef WOLFSSL_AES_128
  8105. WOLFSSL_SMALL_STACK_STATIC const byte oddCipher[] =
  8106. {
  8107. 0xb9,0xd7,0xcb,0x08,0xb0,0xe1,0x7b,0xa0,
  8108. 0xc2
  8109. };
  8110. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Key[] =
  8111. {
  8112. 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,
  8113. 0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c
  8114. };
  8115. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Cipher[] =
  8116. {
  8117. 0x87,0x4d,0x61,0x91,0xb6,0x20,0xe3,0x26,
  8118. 0x1b,0xef,0x68,0x64,0x99,0x0d,0xb6,0xce,
  8119. 0x98,0x06,0xf6,0x6b,0x79,0x70,0xfd,0xff,
  8120. 0x86,0x17,0x18,0x7b,0xb9,0xff,0xfd,0xff,
  8121. 0x5a,0xe4,0xdf,0x3e,0xdb,0xd5,0xd3,0x5e,
  8122. 0x5b,0x4f,0x09,0x02,0x0d,0xb0,0x3e,0xab,
  8123. 0x1e,0x03,0x1d,0xda,0x2f,0xbe,0x03,0xd1,
  8124. 0x79,0x21,0x70,0xa0,0xf3,0x00,0x9c,0xee
  8125. };
  8126. #ifdef WOLFSSL_ARMASM
  8127. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap32Cipher[] =
  8128. {
  8129. 0xb3,0x8b,0x58,0xbc,0xce,0xf4,0x71,0x78,
  8130. 0xf6,0x7c,0xdb,0xb4,0x27,0x2b,0x0a,0xbf,
  8131. 0x7e,0xad,0xea,0x5c,0xd1
  8132. };
  8133. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap32CipherLong[] =
  8134. {
  8135. 0xb3,0x8b,0x58,0xbc,0xce,0xf4,0x71,0x78,
  8136. 0xf6,0x7c,0xdb,0xb4,0x27,0x2b,0x0a,0xbf,
  8137. 0x7e,0xad,0xea,0x5c,0xd1,0xb7,0x98,0xf0,
  8138. 0x22,0x20,0xfe,0x67,0xb0,0x02,0x23,0x50
  8139. };
  8140. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap32_2CipherLong[] =
  8141. {
  8142. 0x6e,0xa1,0x27,0x4d,0xea,0x20,0x5f,0x39,
  8143. 0x68,0xc8,0xb6,0x78,0xde,0xfc,0x53,0x5c,
  8144. 0x90,0xc8,0xf6,0xc6,0xfa,0xe0,0x7b,0x09,
  8145. 0x7c,0xf8,0x9c,0x6a,0x5a,0xa5,0x17,0x7f,
  8146. 0x03,0x92,0x5f,0x4e,0x85,0xea,0x26,0xc9,
  8147. 0x5a,0xc2,0x74,0xe2,0xbf,0xe4,0x1b,0xd4
  8148. };
  8149. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap64Cipher[] =
  8150. {
  8151. 0xdd,0x17,0x10,0x7c,0x45,0x04,0xac,0x43,
  8152. 0xef,0xa8,0xcc,0x32,0x34,0x87,0x88,0xd7,
  8153. 0xae,0x74,0x94,0x72,0x8e
  8154. };
  8155. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap64CipherLong[] =
  8156. {
  8157. 0xdd,0x17,0x10,0x7c,0x45,0x04,0xac,0x43,
  8158. 0xef,0xa8,0xcc,0x32,0x34,0x87,0x88,0xd7,
  8159. 0xae,0x74,0x94,0x72,0x8e,0xd0,0x71,0xc0,
  8160. 0x89,0x8a,0xa1,0xb0,0x29,0xa0,0x10,0x9e
  8161. };
  8162. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap64_2CipherLong[] =
  8163. {
  8164. 0x3f,0xe7,0xd5,0xf3,0xfa,0x09,0xfe,0x40,
  8165. 0xa6,0xa1,0x32,0x8b,0x57,0x12,0xb9,0xfa,
  8166. 0xf2,0x2d,0xe4,0x3c,0x66,0x1d,0x0a,0x8e,
  8167. 0x46,0xf8,0x2e,0x33,0xce,0x8d,0x4e,0x3b,
  8168. 0x17,0x67,0x9e,0x9f,0x76,0x9e,0xc2,0x99,
  8169. 0xd5,0xd4,0x71,0xed,0xb4,0x33,0xb2,0xcd
  8170. };
  8171. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap96Cipher[] =
  8172. {
  8173. 0x55,0x24,0xc2,0x73,0xca,0xa3,0x48,0x03,
  8174. 0x0b,0x72,0x8d,0xd7,0x6c,0x99,0x8e,0x04,
  8175. 0x9d,0x77,0xc9,0x5f,0x38
  8176. };
  8177. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap96CipherLong[] =
  8178. {
  8179. 0x55,0x24,0xc2,0x73,0xca,0xa3,0x48,0x03,
  8180. 0x0b,0x72,0x8d,0xd7,0x6c,0x99,0x8e,0x04,
  8181. 0x9d,0x77,0xc9,0x5f,0x38,0xb5,0x6e,0x44,
  8182. 0x21,0x8e,0xda,0x57,0xe0,0x41,0xc7,0x6a
  8183. };
  8184. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap96_2CipherLong[] =
  8185. {
  8186. 0xc8,0x81,0x1a,0xbe,0xc7,0x5b,0x93,0x6f,
  8187. 0xe6,0x52,0xe4,0xb1,0x2d,0x1c,0x39,0xbc,
  8188. 0xeb,0x82,0x27,0x0a,0x7e,0xa5,0x0e,0x2d,
  8189. 0x32,0xda,0xbe,0x10,0x7a,0x10,0xcc,0xd3,
  8190. 0x6f,0xc6,0x83,0x28,0x05,0x57,0x8a,0x24,
  8191. 0x44,0x76,0x17,0x81,0xb9,0x5c,0x94,0x81
  8192. };
  8193. #endif
  8194. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap128Cipher[] =
  8195. {
  8196. 0xe1,0x33,0x38,0xe3,0x6c,0xb7,0x19,0x62,
  8197. 0xe0,0x0d,0x02,0x0b,0x4c,0xed,0xbd,0x86,
  8198. 0xd3,0xda,0xe1,0x5b,0x04
  8199. };
  8200. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap128CipherLong[] =
  8201. {
  8202. 0xe1,0x33,0x38,0xe3,0x6c,0xb7,0x19,0x62,
  8203. 0xe0,0x0d,0x02,0x0b,0x4c,0xed,0xbd,0x86,
  8204. 0xd3,0xda,0xe1,0x5b,0x04,0xbb,0x35,0x2f,
  8205. 0xa0,0xf5,0x9f,0xeb,0xfc,0xb4,0xda,0x3e
  8206. };
  8207. #ifdef WOLFSSL_ARMASM
  8208. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap128_2CipherLong[] =
  8209. {
  8210. 0xba,0x76,0xaa,0x54,0xd5,0xb5,0x60,0x67,
  8211. 0xc1,0xa7,0x90,0x3b,0x3f,0xdd,0xfa,0x89,
  8212. 0x24,0xdf,0x0c,0x56,0x5c,0xf4,0x2a,0x68,
  8213. 0x97,0x87,0x13,0xb6,0x7a,0xd1,0x24,0xfd,
  8214. 0x4d,0x3f,0x77,0x4a,0xb9,0xe4,0x7d,0xa2,
  8215. 0xdb,0xb9,0x31,0x5e,0xa3,0x11,0x06,0x80
  8216. };
  8217. #endif
  8218. #endif /* WOLFSSL_AES_128 */
  8219. #ifdef WOLFSSL_AES_192
  8220. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Key[] =
  8221. {
  8222. 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  8223. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  8224. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b
  8225. };
  8226. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Cipher[] =
  8227. {
  8228. 0x1a,0xbc,0x93,0x24,0x17,0x52,0x1c,0xa2,
  8229. 0x4f,0x2b,0x04,0x59,0xfe,0x7e,0x6e,0x0b,
  8230. 0x09,0x03,0x39,0xec,0x0a,0xa6,0xfa,0xef,
  8231. 0xd5,0xcc,0xc2,0xc6,0xf4,0xce,0x8e,0x94,
  8232. 0x1e,0x36,0xb2,0x6b,0xd1,0xeb,0xc6,0x70,
  8233. 0xd1,0xbd,0x1d,0x66,0x56,0x20,0xab,0xf7,
  8234. 0x4f,0x78,0xa7,0xf6,0xd2,0x98,0x09,0x58,
  8235. 0x5a,0x97,0xda,0xec,0x58,0xc6,0xb0,0x50
  8236. };
  8237. #ifdef WOLFSSL_ARMASM
  8238. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap32Cipher[] =
  8239. {
  8240. 0x28,0xaa,0xfa,0x90,0x72,0x74,0x86,0xaf,
  8241. 0x72,0x73,0x35,0x17,0x70,0x4e,0x7d,0xca,
  8242. 0x0c,0x33,0x97,0x06,0xc0
  8243. };
  8244. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap32CipherLong[] =
  8245. {
  8246. 0x28,0xaa,0xfa,0x90,0x72,0x74,0x86,0xaf,
  8247. 0x72,0x73,0x35,0x17,0x70,0x4e,0x7d,0xca,
  8248. 0x0c,0x33,0x97,0x06,0xc0,0xbe,0x83,0x87,
  8249. 0xdd,0xd3,0xff,0xd8,0xe4,0x6a,0x5b,0x84
  8250. };
  8251. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap32_2CipherLong[] =
  8252. {
  8253. 0xf5,0x00,0xa2,0x91,0x54,0xa3,0x76,0xa2,
  8254. 0xdd,0xad,0x16,0x89,0xe5,0xf0,0x1d,0x40,
  8255. 0x84,0xcd,0x74,0x84,0xcb,0x8b,0x9e,0x29,
  8256. 0xa9,0x8a,0x12,0x65,0xa0,0x79,0x5e,0xce,
  8257. 0xd9,0x50,0x65,0x21,0x86,0xb0,0x85,0x0d,
  8258. 0x98,0x2d,0x9a,0x5a,0x11,0xbe,0xa5,0x7f
  8259. };
  8260. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap64Cipher[] =
  8261. {
  8262. 0xfe,0x39,0x27,0x97,0xac,0xe5,0xb8,0x74,
  8263. 0xb9,0x8c,0xbf,0x58,0x71,0xa4,0x80,0x33,
  8264. 0x3d,0xf7,0xb4,0xfd,0x8c
  8265. };
  8266. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap64CipherLong[] =
  8267. {
  8268. 0xfe,0x39,0x27,0x97,0xac,0xe5,0xb8,0x74,
  8269. 0xb9,0x8c,0xbf,0x58,0x71,0xa4,0x80,0x33,
  8270. 0x3d,0xf7,0xb4,0xfd,0x8c,0x55,0x47,0x10,
  8271. 0xd5,0x91,0x35,0xbe,0xd8,0x0d,0xa5,0x9e
  8272. };
  8273. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap64_2CipherLong[] =
  8274. {
  8275. 0x59,0xf1,0xed,0x70,0x62,0x42,0xa8,0x06,
  8276. 0x07,0x36,0xe1,0xc5,0x04,0x79,0xc3,0x9b,
  8277. 0xd1,0x14,0x5c,0xcc,0x6f,0x81,0x5f,0x2f,
  8278. 0xa0,0xde,0xcf,0x61,0x55,0x18,0x7a,0xac,
  8279. 0xb0,0x59,0x37,0x90,0x53,0xb3,0x00,0x88,
  8280. 0xb4,0x49,0x90,0x7b,0x96,0xcd,0xcc,0xc3
  8281. };
  8282. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap96Cipher[] =
  8283. {
  8284. 0x41,0x21,0x40,0x31,0xfb,0xc8,0xad,0x23,
  8285. 0xde,0x00,0x03,0xd8,0x96,0x45,0xc7,0xb5,
  8286. 0x47,0xb5,0xf3,0x30,0x3b
  8287. };
  8288. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap96CipherLong[] =
  8289. {
  8290. 0x41,0x21,0x40,0x31,0xfb,0xc8,0xad,0x23,
  8291. 0xde,0x00,0x03,0xd8,0x96,0x45,0xc7,0xb5,
  8292. 0x47,0xb5,0xf3,0x30,0x3b,0xef,0xcd,0x80,
  8293. 0xe3,0x61,0x34,0xef,0x4e,0x1b,0x79,0xc1
  8294. };
  8295. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap96_2CipherLong[] =
  8296. {
  8297. 0x3c,0xb2,0xff,0xc0,0x24,0xe1,0xf5,0xc4,
  8298. 0x0f,0xd1,0x0a,0x1b,0xbe,0x1f,0x23,0xa1,
  8299. 0x8e,0xbf,0x2b,0x96,0xb6,0x37,0xc8,0x25,
  8300. 0x06,0x90,0xe2,0xca,0x71,0x24,0x52,0x95,
  8301. 0xaa,0x8c,0x80,0xdf,0xb7,0xd7,0x30,0xb0,
  8302. 0xcc,0x06,0x4f,0x28,0xa2,0x74,0x27,0xf8
  8303. };
  8304. #endif
  8305. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap128Cipher[] =
  8306. {
  8307. 0x5c,0xc3,0x8f,0xab,0x30,0xb6,0xac,0x67,
  8308. 0xdc,0xc2,0x1e,0x7b,0x01,0x2e,0xcf,0x98,
  8309. 0x8c,0x68,0xa7,0xd9,0x57
  8310. };
  8311. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap128CipherLong[] =
  8312. {
  8313. 0x5c,0xc3,0x8f,0xab,0x30,0xb6,0xac,0x67,
  8314. 0xdc,0xc2,0x1e,0x7b,0x01,0x2e,0xcf,0x98,
  8315. 0x8c,0x68,0xa7,0xd9,0x57,0xab,0x09,0x0f,
  8316. 0x01,0xc4,0x4e,0x62,0xaf,0xc2,0xdf,0x1a
  8317. };
  8318. #ifdef WOLFSSL_ARMASM
  8319. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap128_2CipherLong[] =
  8320. {
  8321. 0x88,0x0a,0x26,0x4e,0xa8,0x26,0x21,0xe0,
  8322. 0xfc,0xbc,0x63,0xdc,0xd9,0x60,0x52,0xb2,
  8323. 0x99,0x2f,0xbb,0x1e,0x00,0xf5,0x9f,0x6d,
  8324. 0xab,0x48,0x0f,0xc6,0x37,0x12,0x56,0xe3,
  8325. 0x12,0x8d,0x31,0xc8,0xea,0xf4,0x41,0x82,
  8326. 0x7a,0x88,0xe0,0xd7,0xf0,0x67,0x03,0xa4
  8327. };
  8328. #endif
  8329. #endif
  8330. #ifdef WOLFSSL_AES_256
  8331. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Key[] =
  8332. {
  8333. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  8334. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  8335. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  8336. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  8337. };
  8338. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Cipher[] =
  8339. {
  8340. 0x60,0x1e,0xc3,0x13,0x77,0x57,0x89,0xa5,
  8341. 0xb7,0xa7,0xf5,0x04,0xbb,0xf3,0xd2,0x28,
  8342. 0xf4,0x43,0xe3,0xca,0x4d,0x62,0xb5,0x9a,
  8343. 0xca,0x84,0xe9,0x90,0xca,0xca,0xf5,0xc5,
  8344. 0x2b,0x09,0x30,0xda,0xa2,0x3d,0xe9,0x4c,
  8345. 0xe8,0x70,0x17,0xba,0x2d,0x84,0x98,0x8d,
  8346. 0xdf,0xc9,0xc5,0x8d,0xb6,0x7a,0xad,0xa6,
  8347. 0x13,0xc2,0xdd,0x08,0x45,0x79,0x41,0xa6
  8348. };
  8349. #ifdef WOLFSSL_ARMASM
  8350. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap32Cipher[] =
  8351. {
  8352. 0xb0,0xa8,0xc0,0x65,0x85,0x20,0x0d,0x5c,
  8353. 0x25,0xcf,0xe7,0x58,0x63,0xc8,0xd4,0xea,
  8354. 0xa2,0x13,0x47,0x74,0xda
  8355. };
  8356. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap32CipherLong[] =
  8357. {
  8358. 0xb0,0xa8,0xc0,0x65,0x85,0x20,0x0d,0x5c,
  8359. 0x25,0xcf,0xe7,0x58,0x63,0xc8,0xd4,0xea,
  8360. 0xa2,0x13,0x47,0x74,0xda,0x89,0x77,0x40,
  8361. 0x28,0x9c,0xe8,0x19,0x26,0x32,0xd8,0x1f
  8362. };
  8363. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap32_2CipherLong[] =
  8364. {
  8365. 0xf6,0xd9,0x22,0xc6,0x80,0x29,0xaf,0x14,
  8366. 0x54,0x6c,0x0a,0xce,0x42,0xea,0x3c,0xa1,
  8367. 0x7c,0xeb,0x36,0x0d,0x8e,0xd7,0x8c,0x59,
  8368. 0xa8,0x09,0x9f,0x9e,0xba,0x5b,0x95,0xfa,
  8369. 0x26,0x8c,0x37,0x59,0xf8,0xae,0x8e,0xaa,
  8370. 0x4d,0xe4,0x1c,0xfe,0x51,0xc7,0xb7,0xcc
  8371. };
  8372. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap64Cipher[] =
  8373. {
  8374. 0x74,0x1a,0x52,0x41,0x76,0xb4,0x11,0x8f,
  8375. 0xfd,0x57,0x31,0xfd,0x3d,0x76,0x8f,0x07,
  8376. 0xd4,0x94,0x4c,0xcd,0x4d
  8377. };
  8378. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap64CipherLong[] =
  8379. {
  8380. 0x74,0x1a,0x52,0x41,0x76,0xb4,0x11,0x8f,
  8381. 0xfd,0x57,0x31,0xfd,0x3d,0x76,0x8f,0x07,
  8382. 0xd4,0x94,0x4c,0xcd,0x4d,0x47,0x5a,0x92,
  8383. 0x26,0x49,0x81,0x7a,0xda,0x36,0x27,0x01
  8384. };
  8385. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap64_2CipherLong[] =
  8386. {
  8387. 0xf7,0x9c,0xbf,0xf6,0xa2,0xaa,0x8a,0x0a,
  8388. 0x63,0x8a,0x20,0x2f,0x12,0xf1,0x8e,0x49,
  8389. 0x30,0xc0,0x8d,0x5c,0x5f,0x8b,0xbc,0x16,
  8390. 0xdd,0x71,0xee,0x13,0x14,0x7b,0xe1,0x25,
  8391. 0xcb,0x87,0x8a,0xc6,0xdc,0x1d,0x54,0x7a,
  8392. 0xe1,0xe4,0x6f,0x0d,0x95,0x1b,0xd1,0x8b
  8393. };
  8394. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap96Cipher[] =
  8395. {
  8396. 0xb9,0x07,0x02,0xb8,0xbe,0x94,0xbf,0x53,
  8397. 0xdf,0x83,0x8e,0x23,0x8c,0x67,0x0c,0x81,
  8398. 0xb8,0x69,0xa1,0x48,0x45
  8399. };
  8400. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap96CipherLong[] =
  8401. {
  8402. 0xb9,0x07,0x02,0xb8,0xbe,0x94,0xbf,0x53,
  8403. 0xdf,0x83,0x8e,0x23,0x8c,0x67,0x0c,0x81,
  8404. 0xb8,0x69,0xa1,0x48,0x45,0xf1,0xc6,0x27,
  8405. 0x36,0xa8,0xb2,0x4b,0x0e,0x62,0x6b,0x72
  8406. };
  8407. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap96_2CipherLong[] =
  8408. {
  8409. 0xd5,0x56,0x73,0xaa,0xb8,0xe4,0x06,0xf6,
  8410. 0x83,0x45,0x3a,0xb4,0xb9,0x63,0xec,0xad,
  8411. 0x73,0xc5,0xab,0x78,0xb1,0x21,0xab,0xef,
  8412. 0x69,0x15,0xb7,0x0c,0xe9,0xb4,0x3a,0xe7,
  8413. 0xbc,0xc4,0x22,0xbd,0x93,0xba,0x52,0xe0,
  8414. 0x91,0x2f,0x5e,0x8d,0x6d,0x59,0xf7,0xc2
  8415. };
  8416. #endif
  8417. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap128Cipher[] =
  8418. {
  8419. 0x50,0xfd,0x97,0xc3,0xe6,0x1a,0xbb,0x48,
  8420. 0x73,0xfb,0x78,0xdf,0x1e,0x8e,0x77,0xe6,
  8421. 0x4b,0x45,0x7c,0xd6,0x8a
  8422. };
  8423. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap128CipherLong[] =
  8424. {
  8425. 0x50,0xfd,0x97,0xc3,0xe6,0x1a,0xbb,0x48,
  8426. 0x73,0xfb,0x78,0xdf,0x1e,0x8e,0x77,0xe6,
  8427. 0x4b,0x45,0x7c,0xd6,0x8a,0xcc,0xda,0x4a,
  8428. 0x89,0xfa,0x23,0x6c,0x06,0xbf,0x26,0x05
  8429. };
  8430. #ifdef WOLFSSL_ARMASM
  8431. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap128_2CipherLong[] =
  8432. {
  8433. 0x24,0x5c,0x09,0xa0,0x3b,0x1a,0x5a,0x94,
  8434. 0x2b,0x93,0x56,0x13,0x48,0xa0,0x21,0xce,
  8435. 0x95,0x11,0xa3,0x76,0xd6,0x59,0x88,0x42,
  8436. 0x04,0x71,0x69,0x62,0x28,0xb2,0xee,0x9d,
  8437. 0xd5,0xa0,0xea,0xc7,0x37,0x93,0x92,0xc7,
  8438. 0xf2,0xb6,0x8d,0xd9,0x59,0x1a,0xfa,0xbb
  8439. };
  8440. #endif
  8441. #endif
  8442. int i;
  8443. struct {
  8444. const byte* key;
  8445. int keySz;
  8446. const byte* iv;
  8447. const byte* plain;
  8448. int len;
  8449. const byte* cipher;
  8450. } testVec[] = {
  8451. #ifdef WOLFSSL_AES_128
  8452. { ctr128Key, (int)sizeof(ctr128Key), ctrIv,
  8453. ctrPlain, (int)sizeof(ctrPlain), ctr128Cipher },
  8454. /* let's try with just 9 bytes, non block size test */
  8455. { ctr128Key, (int)sizeof(ctr128Key), ctrIv,
  8456. ctrPlain, (int)sizeof(oddCipher), ctr128Cipher },
  8457. /* and an additional 9 bytes to reuse tmp left buffer */
  8458. { NULL, 0, NULL, ctrPlain, (int)sizeof(oddCipher), oddCipher },
  8459. /* Counter wrapping */
  8460. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap128,
  8461. ctrPlain, (int)sizeof(ctr128Wrap128Cipher), ctr128Wrap128Cipher },
  8462. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap128,
  8463. ctrPlain, (int)sizeof(ctr128Wrap128CipherLong),
  8464. ctr128Wrap128CipherLong },
  8465. #ifdef WOLFSSL_ARMASM
  8466. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap128_2,
  8467. ctrPlain, (int)sizeof(ctr128Wrap128_2CipherLong),
  8468. ctr128Wrap128_2CipherLong },
  8469. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap96,
  8470. ctrPlain, (int)sizeof(ctr128Wrap96Cipher), ctr128Wrap96Cipher },
  8471. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap96,
  8472. ctrPlain, (int)sizeof(ctr128Wrap96CipherLong),
  8473. ctr128Wrap96CipherLong },
  8474. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap96_2,
  8475. ctrPlain, (int)sizeof(ctr128Wrap96_2CipherLong),
  8476. ctr128Wrap96_2CipherLong },
  8477. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap64,
  8478. ctrPlain, (int)sizeof(ctr128Wrap64Cipher), ctr128Wrap64Cipher },
  8479. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap64,
  8480. ctrPlain, (int)sizeof(ctr128Wrap64CipherLong),
  8481. ctr128Wrap64CipherLong },
  8482. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap64_2,
  8483. ctrPlain, (int)sizeof(ctr128Wrap64_2CipherLong),
  8484. ctr128Wrap64_2CipherLong },
  8485. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap32,
  8486. ctrPlain, (int)sizeof(ctr128Wrap32Cipher), ctr128Wrap32Cipher },
  8487. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap32,
  8488. ctrPlain, (int)sizeof(ctr128Wrap32CipherLong),
  8489. ctr128Wrap32CipherLong },
  8490. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap32_2,
  8491. ctrPlain, (int)sizeof(ctr128Wrap32_2CipherLong),
  8492. ctr128Wrap32_2CipherLong },
  8493. #endif
  8494. #endif
  8495. #ifdef WOLFSSL_AES_192
  8496. { ctr192Key, (int)sizeof(ctr192Key), ctrIv,
  8497. ctrPlain, (int)sizeof(ctrPlain), ctr192Cipher },
  8498. /* let's try with just 9 bytes, non block size test */
  8499. { ctr192Key, (int)sizeof(ctr192Key), ctrIv,
  8500. ctrPlain, (int)sizeof(oddCipher), ctr192Cipher },
  8501. /* Counter wrapping */
  8502. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap128,
  8503. ctrPlain, (int)sizeof(ctr192Wrap128Cipher), ctr192Wrap128Cipher },
  8504. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap128,
  8505. ctrPlain, (int)sizeof(ctr192Wrap128CipherLong),
  8506. ctr192Wrap128CipherLong },
  8507. #ifdef WOLFSSL_ARMASM
  8508. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap128_2,
  8509. ctrPlain, (int)sizeof(ctr192Wrap128_2CipherLong),
  8510. ctr192Wrap128_2CipherLong },
  8511. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap96,
  8512. ctrPlain, (int)sizeof(ctr192Wrap96Cipher), ctr192Wrap96Cipher },
  8513. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap96,
  8514. ctrPlain, (int)sizeof(ctr192Wrap96CipherLong),
  8515. ctr192Wrap96CipherLong },
  8516. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap96_2,
  8517. ctrPlain, (int)sizeof(ctr192Wrap96_2CipherLong),
  8518. ctr192Wrap96_2CipherLong },
  8519. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap64,
  8520. ctrPlain, (int)sizeof(ctr192Wrap64Cipher), ctr192Wrap64Cipher },
  8521. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap64,
  8522. ctrPlain, (int)sizeof(ctr192Wrap64CipherLong),
  8523. ctr192Wrap64CipherLong },
  8524. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap64_2,
  8525. ctrPlain, (int)sizeof(ctr192Wrap64_2CipherLong),
  8526. ctr192Wrap64_2CipherLong },
  8527. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap32,
  8528. ctrPlain, (int)sizeof(ctr192Wrap32Cipher), ctr192Wrap32Cipher },
  8529. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap32,
  8530. ctrPlain, (int)sizeof(ctr192Wrap32CipherLong),
  8531. ctr192Wrap32CipherLong },
  8532. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap32_2,
  8533. ctrPlain, (int)sizeof(ctr192Wrap32_2CipherLong),
  8534. ctr192Wrap32_2CipherLong },
  8535. #endif
  8536. #endif
  8537. #ifdef WOLFSSL_AES_256
  8538. { ctr256Key, (int)sizeof(ctr256Key), ctrIv,
  8539. ctrPlain, (int)sizeof(ctrPlain), ctr256Cipher },
  8540. /* let's try with just 9 bytes, non block size test */
  8541. { ctr256Key, (int)sizeof(ctr256Key), ctrIv,
  8542. ctrPlain, (int)sizeof(oddCipher), ctr256Cipher },
  8543. /* Counter wrapping */
  8544. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap128,
  8545. ctrPlain, (int)sizeof(ctr256Wrap128Cipher), ctr256Wrap128Cipher },
  8546. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap128,
  8547. ctrPlain, (int)sizeof(ctr256Wrap128CipherLong),
  8548. ctr256Wrap128CipherLong },
  8549. #ifdef WOLFSSL_ARMASM
  8550. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap128_2,
  8551. ctrPlain, (int)sizeof(ctr256Wrap128_2CipherLong),
  8552. ctr256Wrap128_2CipherLong },
  8553. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap96,
  8554. ctrPlain, (int)sizeof(ctr256Wrap96Cipher), ctr256Wrap96Cipher },
  8555. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap96,
  8556. ctrPlain, (int)sizeof(ctr256Wrap96CipherLong),
  8557. ctr256Wrap96CipherLong },
  8558. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap96_2,
  8559. ctrPlain, (int)sizeof(ctr256Wrap96_2CipherLong),
  8560. ctr256Wrap96_2CipherLong },
  8561. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap64,
  8562. ctrPlain, (int)sizeof(ctr256Wrap64Cipher), ctr256Wrap64Cipher },
  8563. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap64,
  8564. ctrPlain, (int)sizeof(ctr256Wrap64CipherLong),
  8565. ctr256Wrap64CipherLong },
  8566. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap64_2,
  8567. ctrPlain, (int)sizeof(ctr256Wrap64_2CipherLong),
  8568. ctr256Wrap64_2CipherLong },
  8569. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap32,
  8570. ctrPlain, (int)sizeof(ctr256Wrap32Cipher), ctr256Wrap32Cipher },
  8571. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap32,
  8572. ctrPlain, (int)sizeof(ctr256Wrap32CipherLong),
  8573. ctr256Wrap32CipherLong },
  8574. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap32_2,
  8575. ctrPlain, (int)sizeof(ctr256Wrap32_2CipherLong),
  8576. ctr256Wrap32_2CipherLong },
  8577. #endif
  8578. #endif
  8579. };
  8580. #define AES_CTR_TEST_LEN (int)(sizeof(testVec) / sizeof(*testVec))
  8581. for (i = 0; i < AES_CTR_TEST_LEN; i++) {
  8582. if (testVec[i].key != NULL) {
  8583. ret = wc_AesSetKeyDirect(enc, testVec[i].key, testVec[i].keySz,
  8584. testVec[i].iv, AES_ENCRYPTION);
  8585. if (ret != 0) {
  8586. ERROR_OUT(-5930 - i * 10, out);
  8587. }
  8588. /* Ctr only uses encrypt, even on key setup */
  8589. ret = wc_AesSetKeyDirect(dec, testVec[i].key, testVec[i].keySz,
  8590. testVec[i].iv, AES_ENCRYPTION);
  8591. if (ret != 0) {
  8592. ERROR_OUT(-5931 - i * 10, out);
  8593. }
  8594. }
  8595. ret = wc_AesCtrEncrypt(enc, cipher, testVec[i].plain, testVec[i].len);
  8596. if (ret != 0) {
  8597. ERROR_OUT(-5932 - i * 10, out);
  8598. }
  8599. ret = wc_AesCtrEncrypt(dec, plain, cipher, testVec[i].len);
  8600. if (ret != 0) {
  8601. ERROR_OUT(-5933 - i * 10, out);
  8602. }
  8603. if (XMEMCMP(plain, ctrPlain, testVec[i].len)) {
  8604. ERROR_OUT(-5934 - i * 10, out);
  8605. }
  8606. #if !(FIPS_VERSION_EQ(2,0) && defined(WOLFSSL_ARMASM))
  8607. if (XMEMCMP(cipher, testVec[i].cipher, testVec[i].len)) {
  8608. ERROR_OUT(-5935 - i * 10, out);
  8609. }
  8610. #endif
  8611. }
  8612. out:
  8613. return ret;
  8614. }
  8615. #endif /* WOLFSSL_AES_COUNTER */
  8616. WOLFSSL_TEST_SUBROUTINE int aes_test(void)
  8617. {
  8618. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_COUNTER) || defined(WOLFSSL_AES_DIRECT)
  8619. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8620. Aes *enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  8621. #else
  8622. Aes enc[1];
  8623. #endif
  8624. byte cipher[AES_BLOCK_SIZE * 4];
  8625. #ifdef HAVE_AES_DECRYPT
  8626. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8627. Aes *dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  8628. #else
  8629. Aes dec[1];
  8630. #endif
  8631. byte plain [AES_BLOCK_SIZE * 4];
  8632. #endif /* HAVE_AES_DECRYPT */
  8633. #endif /* HAVE_AES_CBC || WOLFSSL_AES_COUNTER || WOLFSSL_AES_DIRECT */
  8634. int ret = 0;
  8635. #ifdef HAVE_AES_CBC
  8636. #ifdef WOLFSSL_AES_128
  8637. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = { /* "Now is the time for all " w/o trailing 0 */
  8638. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  8639. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  8640. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  8641. };
  8642. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  8643. {
  8644. 0x95,0x94,0x92,0x57,0x5f,0x42,0x81,0x53,
  8645. 0x2c,0xcc,0x9d,0x46,0x77,0xa2,0x33,0xcb
  8646. };
  8647. WOLFSSL_SMALL_STACK_STATIC const byte key[] = "0123456789abcdef "; /* align */
  8648. WOLFSSL_SMALL_STACK_STATIC const byte iv[] = "1234567890abcdef "; /* align */
  8649. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8650. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_COUNTER) || defined(WOLFSSL_AES_DIRECT)
  8651. if (enc == NULL)
  8652. ERROR_OUT(-5990, out);
  8653. #endif
  8654. #if defined(HAVE_AES_DECRYPT) || defined(WOLFSSL_AES_COUNTER) || defined(WOLFSSL_AES_DIRECT)
  8655. if (dec == NULL)
  8656. ERROR_OUT(-5991, out);
  8657. #endif
  8658. #endif
  8659. if (wc_AesInit(enc, HEAP_HINT, devId) != 0)
  8660. ERROR_OUT(-5900, out); /* note this error code is used programmatically in cleanup. */
  8661. #if defined(HAVE_AES_DECRYPT) || defined(WOLFSSL_AES_COUNTER)
  8662. if (wc_AesInit(dec, HEAP_HINT, devId) != 0)
  8663. ERROR_OUT(-5901, out); /* note this error code is used programmatically in cleanup. */
  8664. #endif
  8665. ret = wc_AesSetKey(enc, key, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  8666. if (ret != 0)
  8667. ERROR_OUT(-5902, out);
  8668. #if defined(HAVE_AES_DECRYPT) || defined(WOLFSSL_AES_COUNTER)
  8669. ret = wc_AesSetKey(dec, key, AES_BLOCK_SIZE, iv, AES_DECRYPTION);
  8670. if (ret != 0)
  8671. ERROR_OUT(-5903, out);
  8672. #endif
  8673. XMEMSET(cipher, 0, AES_BLOCK_SIZE * 4);
  8674. ret = wc_AesCbcEncrypt(enc, cipher, msg, AES_BLOCK_SIZE);
  8675. #if defined(WOLFSSL_ASYNC_CRYPT)
  8676. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  8677. #endif
  8678. if (ret != 0)
  8679. ERROR_OUT(-5904, out);
  8680. #ifdef HAVE_AES_DECRYPT
  8681. XMEMSET(plain, 0, AES_BLOCK_SIZE * 4);
  8682. ret = wc_AesCbcDecrypt(dec, plain, cipher, AES_BLOCK_SIZE);
  8683. #if defined(WOLFSSL_ASYNC_CRYPT)
  8684. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  8685. #endif
  8686. if (ret != 0)
  8687. ERROR_OUT(-5905, out);
  8688. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  8689. ERROR_OUT(-5906, out);
  8690. #endif /* HAVE_AES_DECRYPT */
  8691. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE))
  8692. ERROR_OUT(-5907, out);
  8693. #endif /* WOLFSSL_AES_128 */
  8694. #if defined(WOLFSSL_AESNI) && defined(HAVE_AES_DECRYPT)
  8695. {
  8696. WOLFSSL_SMALL_STACK_STATIC const byte bigMsg[] = {
  8697. /* "All work and no play makes Jack a dull boy. " */
  8698. 0x41,0x6c,0x6c,0x20,0x77,0x6f,0x72,0x6b,
  8699. 0x20,0x61,0x6e,0x64,0x20,0x6e,0x6f,0x20,
  8700. 0x70,0x6c,0x61,0x79,0x20,0x6d,0x61,0x6b,
  8701. 0x65,0x73,0x20,0x4a,0x61,0x63,0x6b,0x20,
  8702. 0x61,0x20,0x64,0x75,0x6c,0x6c,0x20,0x62,
  8703. 0x6f,0x79,0x2e,0x20,0x41,0x6c,0x6c,0x20,
  8704. 0x77,0x6f,0x72,0x6b,0x20,0x61,0x6e,0x64,
  8705. 0x20,0x6e,0x6f,0x20,0x70,0x6c,0x61,0x79,
  8706. 0x20,0x6d,0x61,0x6b,0x65,0x73,0x20,0x4a,
  8707. 0x61,0x63,0x6b,0x20,0x61,0x20,0x64,0x75,
  8708. 0x6c,0x6c,0x20,0x62,0x6f,0x79,0x2e,0x20,
  8709. 0x41,0x6c,0x6c,0x20,0x77,0x6f,0x72,0x6b,
  8710. 0x20,0x61,0x6e,0x64,0x20,0x6e,0x6f,0x20,
  8711. 0x70,0x6c,0x61,0x79,0x20,0x6d,0x61,0x6b,
  8712. 0x65,0x73,0x20,0x4a,0x61,0x63,0x6b,0x20,
  8713. 0x61,0x20,0x64,0x75,0x6c,0x6c,0x20,0x62,
  8714. 0x6f,0x79,0x2e,0x20,0x41,0x6c,0x6c,0x20,
  8715. 0x77,0x6f,0x72,0x6b,0x20,0x61,0x6e,0x64,
  8716. 0x20,0x6e,0x6f,0x20,0x70,0x6c,0x61,0x79,
  8717. 0x20,0x6d,0x61,0x6b,0x65,0x73,0x20,0x4a,
  8718. 0x61,0x63,0x6b,0x20,0x61,0x20,0x64,0x75,
  8719. 0x6c,0x6c,0x20,0x62,0x6f,0x79,0x2e,0x20,
  8720. 0x41,0x6c,0x6c,0x20,0x77,0x6f,0x72,0x6b,
  8721. 0x20,0x61,0x6e,0x64,0x20,0x6e,0x6f,0x20,
  8722. 0x70,0x6c,0x61,0x79,0x20,0x6d,0x61,0x6b,
  8723. 0x65,0x73,0x20,0x4a,0x61,0x63,0x6b,0x20,
  8724. 0x61,0x20,0x64,0x75,0x6c,0x6c,0x20,0x62,
  8725. 0x6f,0x79,0x2e,0x20,0x41,0x6c,0x6c,0x20,
  8726. 0x77,0x6f,0x72,0x6b,0x20,0x61,0x6e,0x64,
  8727. 0x20,0x6e,0x6f,0x20,0x70,0x6c,0x61,0x79,
  8728. 0x20,0x6d,0x61,0x6b,0x65,0x73,0x20,0x4a,
  8729. 0x61,0x63,0x6b,0x20,0x61,0x20,0x64,0x75,
  8730. 0x6c,0x6c,0x20,0x62,0x6f,0x79,0x2e,0x20,
  8731. 0x41,0x6c,0x6c,0x20,0x77,0x6f,0x72,0x6b,
  8732. 0x20,0x61,0x6e,0x64,0x20,0x6e,0x6f,0x20,
  8733. 0x70,0x6c,0x61,0x79,0x20,0x6d,0x61,0x6b,
  8734. 0x65,0x73,0x20,0x4a,0x61,0x63,0x6b,0x20,
  8735. 0x61,0x20,0x64,0x75,0x6c,0x6c,0x20,0x62,
  8736. 0x6f,0x79,0x2e,0x20,0x41,0x6c,0x6c,0x20,
  8737. 0x77,0x6f,0x72,0x6b,0x20,0x61,0x6e,0x64,
  8738. 0x20,0x6e,0x6f,0x20,0x70,0x6c,0x61,0x79,
  8739. 0x20,0x6d,0x61,0x6b,0x65,0x73,0x20,0x4a,
  8740. 0x61,0x63,0x6b,0x20,0x61,0x20,0x64,0x75,
  8741. 0x6c,0x6c,0x20,0x62,0x6f,0x79,0x2e,0x20,
  8742. 0x41,0x6c,0x6c,0x20,0x77,0x6f,0x72,0x6b,
  8743. 0x20,0x61,0x6e,0x64,0x20,0x6e,0x6f,0x20,
  8744. 0x70,0x6c,0x61,0x79,0x20,0x6d,0x61,0x6b,
  8745. 0x65,0x73,0x20,0x4a,0x61,0x63,0x6b,0x20
  8746. };
  8747. WOLFSSL_SMALL_STACK_STATIC const byte bigKey[] = "0123456789abcdeffedcba9876543210";
  8748. word32 keySz, msgSz;
  8749. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8750. byte *bigCipher = (byte *)XMALLOC(sizeof(bigMsg), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8751. byte *bigPlain = (byte *)XMALLOC(sizeof(bigMsg), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8752. if ((bigCipher == NULL) ||
  8753. (bigPlain == NULL)) {
  8754. if (bigCipher != NULL)
  8755. XFREE(bigCipher, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8756. ERROR_OUT(-5992, out);
  8757. }
  8758. #else
  8759. byte bigCipher[sizeof(bigMsg)];
  8760. byte bigPlain[sizeof(bigMsg)];
  8761. #endif
  8762. /* Iterate from one AES_BLOCK_SIZE of bigMsg through the whole
  8763. * message by AES_BLOCK_SIZE for each size of AES key. */
  8764. for (keySz = 16; keySz <= 32; keySz += 8) {
  8765. for (msgSz = AES_BLOCK_SIZE;
  8766. msgSz <= sizeof(bigMsg);
  8767. msgSz += AES_BLOCK_SIZE) {
  8768. XMEMSET(bigCipher, 0, sizeof(bigMsg));
  8769. XMEMSET(bigPlain, 0, sizeof(bigMsg));
  8770. ret = wc_AesSetKey(enc, bigKey, keySz, iv, AES_ENCRYPTION);
  8771. if (ret != 0) {
  8772. ret = -5908;
  8773. break;
  8774. }
  8775. ret = wc_AesSetKey(dec, bigKey, keySz, iv, AES_DECRYPTION);
  8776. if (ret != 0) {
  8777. ret = -5909;
  8778. break;
  8779. }
  8780. ret = wc_AesCbcEncrypt(enc, bigCipher, bigMsg, msgSz);
  8781. #if defined(WOLFSSL_ASYNC_CRYPT)
  8782. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  8783. #endif
  8784. if (ret != 0) {
  8785. ret = -5910;
  8786. break;
  8787. }
  8788. ret = wc_AesCbcDecrypt(dec, bigPlain, bigCipher, msgSz);
  8789. #if defined(WOLFSSL_ASYNC_CRYPT)
  8790. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  8791. #endif
  8792. if (ret != 0) {
  8793. ret = -5911;
  8794. break;
  8795. }
  8796. if (XMEMCMP(bigPlain, bigMsg, msgSz)) {
  8797. ret = -5912;
  8798. break;
  8799. }
  8800. }
  8801. if (ret != 0)
  8802. break;
  8803. }
  8804. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8805. XFREE(bigCipher, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8806. XFREE(bigPlain, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8807. #endif
  8808. if (ret != 0)
  8809. goto out;
  8810. }
  8811. #endif /* WOLFSSL_AESNI && HAVE_AES_DECRYPT */
  8812. /* Test of AES IV state with encrypt/decrypt */
  8813. #ifdef WOLFSSL_AES_128
  8814. {
  8815. /* Test Vector from "NIST Special Publication 800-38A, 2001 Edition"
  8816. * https://nvlpubs.nist.gov/nistpubs/legacy/sp/nistspecialpublication800-38a.pdf
  8817. */
  8818. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] =
  8819. {
  8820. 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
  8821. 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
  8822. 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c,
  8823. 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51
  8824. };
  8825. WOLFSSL_SMALL_STACK_STATIC const byte verify2[] =
  8826. {
  8827. 0x76, 0x49, 0xab, 0xac, 0x81, 0x19, 0xb2, 0x46,
  8828. 0xce, 0xe9, 0x8e, 0x9b, 0x12, 0xe9, 0x19, 0x7d,
  8829. 0x50, 0x86, 0xcb, 0x9b, 0x50, 0x72, 0x19, 0xee,
  8830. 0x95, 0xdb, 0x11, 0x3a, 0x91, 0x76, 0x78, 0xb2
  8831. };
  8832. WOLFSSL_SMALL_STACK_STATIC const byte key2[] = {
  8833. 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
  8834. 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c
  8835. };
  8836. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] = {
  8837. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  8838. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
  8839. };
  8840. ret = wc_AesSetKey(enc, key2, sizeof(key2), iv2, AES_ENCRYPTION);
  8841. if (ret != 0)
  8842. ERROR_OUT(-5913, out);
  8843. XMEMSET(cipher, 0, AES_BLOCK_SIZE * 2);
  8844. ret = wc_AesCbcEncrypt(enc, cipher, msg2, AES_BLOCK_SIZE);
  8845. #if defined(WOLFSSL_ASYNC_CRYPT)
  8846. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  8847. #endif
  8848. if (ret != 0)
  8849. ERROR_OUT(-5914, out);
  8850. if (XMEMCMP(cipher, verify2, AES_BLOCK_SIZE))
  8851. ERROR_OUT(-5915, out);
  8852. ret = wc_AesCbcEncrypt(enc, cipher + AES_BLOCK_SIZE,
  8853. msg2 + AES_BLOCK_SIZE, AES_BLOCK_SIZE);
  8854. #if defined(WOLFSSL_ASYNC_CRYPT)
  8855. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  8856. #endif
  8857. if (ret != 0)
  8858. ERROR_OUT(-5916, out);
  8859. if (XMEMCMP(cipher + AES_BLOCK_SIZE, verify2 + AES_BLOCK_SIZE,
  8860. AES_BLOCK_SIZE))
  8861. ERROR_OUT(-5917, out);
  8862. #if defined(HAVE_AES_DECRYPT)
  8863. ret = wc_AesSetKey(dec, key2, sizeof(key2), iv2, AES_DECRYPTION);
  8864. if (ret != 0)
  8865. ERROR_OUT(-5918, out);
  8866. XMEMSET(plain, 0, AES_BLOCK_SIZE * 2);
  8867. ret = wc_AesCbcDecrypt(dec, plain, verify2, AES_BLOCK_SIZE);
  8868. #if defined(WOLFSSL_ASYNC_CRYPT)
  8869. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  8870. #endif
  8871. if (ret != 0)
  8872. ERROR_OUT(-5919, out);
  8873. if (XMEMCMP(plain, msg2, AES_BLOCK_SIZE))
  8874. ERROR_OUT(-5920, out);
  8875. ret = wc_AesCbcDecrypt(dec, plain + AES_BLOCK_SIZE,
  8876. verify2 + AES_BLOCK_SIZE, AES_BLOCK_SIZE);
  8877. #if defined(WOLFSSL_ASYNC_CRYPT)
  8878. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  8879. #endif
  8880. if (ret != 0)
  8881. ERROR_OUT(-5921, out);
  8882. if (XMEMCMP(plain + AES_BLOCK_SIZE, msg2 + AES_BLOCK_SIZE,
  8883. AES_BLOCK_SIZE))
  8884. ERROR_OUT(-5922, out);
  8885. #endif /* HAVE_AES_DECRYPT */
  8886. }
  8887. #endif /* WOLFSSL_AES_128 */
  8888. #endif /* HAVE_AES_CBC */
  8889. #ifdef WOLFSSL_AES_COUNTER
  8890. ret = aesctr_test(enc, dec, cipher, plain);
  8891. if (ret != 0)
  8892. return ret;
  8893. #endif
  8894. #if defined(WOLFSSL_AES_DIRECT) && defined(WOLFSSL_AES_256)
  8895. {
  8896. WOLFSSL_SMALL_STACK_STATIC const byte niPlain[] =
  8897. {
  8898. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  8899. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  8900. };
  8901. WOLFSSL_SMALL_STACK_STATIC const byte niCipher[] =
  8902. {
  8903. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  8904. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  8905. };
  8906. WOLFSSL_SMALL_STACK_STATIC const byte niKey[] =
  8907. {
  8908. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  8909. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  8910. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  8911. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  8912. };
  8913. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  8914. ret = wc_AesSetKey(enc, niKey, sizeof(niKey), cipher, AES_ENCRYPTION);
  8915. if (ret != 0)
  8916. ERROR_OUT(-5994, out);
  8917. #if !defined(HAVE_SELFTEST) && \
  8918. (defined(WOLFSSL_LINUXKM) || \
  8919. !defined(HAVE_FIPS) || \
  8920. (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3)))
  8921. if (wc_AesEncryptDirect(enc, cipher, niPlain) != 0)
  8922. ERROR_OUT(-5995, out);
  8923. #else
  8924. wc_AesEncryptDirect(enc, cipher, niPlain);
  8925. #endif
  8926. if (XMEMCMP(cipher, niCipher, AES_BLOCK_SIZE) != 0)
  8927. ERROR_OUT(-5996, out);
  8928. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  8929. ret = wc_AesSetKey(dec, niKey, sizeof(niKey), plain, AES_DECRYPTION);
  8930. if (ret != 0)
  8931. ERROR_OUT(-5997, out);
  8932. #if !defined(HAVE_SELFTEST) && \
  8933. (defined(WOLFSSL_LINUXKM) || \
  8934. !defined(HAVE_FIPS) || \
  8935. (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3)))
  8936. if (wc_AesDecryptDirect(dec, plain, niCipher) != 0)
  8937. ERROR_OUT(-5998, out);
  8938. #else
  8939. wc_AesDecryptDirect(dec, plain, niCipher);
  8940. #endif
  8941. if (XMEMCMP(plain, niPlain, AES_BLOCK_SIZE) != 0)
  8942. ERROR_OUT(-5999, out);
  8943. }
  8944. #endif /* WOLFSSL_AES_DIRECT && WOLFSSL_AES_256 */
  8945. ret = aes_key_size_test();
  8946. if (ret != 0)
  8947. goto out;
  8948. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  8949. ret = aes_cbc_test();
  8950. if (ret != 0)
  8951. goto out;
  8952. #endif
  8953. #if defined(WOLFSSL_AES_XTS)
  8954. #ifdef WOLFSSL_AES_128
  8955. ret = aes_xts_128_test();
  8956. if (ret != 0)
  8957. goto out;
  8958. #endif
  8959. #ifdef WOLFSSL_AES_256
  8960. ret = aes_xts_256_test();
  8961. if (ret != 0)
  8962. goto out;
  8963. #endif
  8964. #if defined(WOLFSSL_AES_128) && defined(WOLFSSL_AES_256)
  8965. ret = aes_xts_sector_test();
  8966. if (ret != 0)
  8967. goto out;
  8968. #endif
  8969. #ifdef WOLFSSL_AES_128
  8970. ret = aes_xts_args_test();
  8971. if (ret != 0)
  8972. goto out;
  8973. #endif
  8974. #endif
  8975. #if defined(WOLFSSL_AES_CFB)
  8976. ret = aescfb_test();
  8977. if (ret != 0)
  8978. goto out;
  8979. #if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  8980. ret = aescfb1_test();
  8981. if (ret != 0)
  8982. goto out;
  8983. ret = aescfb8_test();
  8984. if (ret != 0)
  8985. goto out;
  8986. #endif
  8987. #endif
  8988. #if defined(HAVE_AES_ECB) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  8989. ret = aesecb_test();
  8990. if (ret != 0)
  8991. goto out;
  8992. #endif
  8993. out:
  8994. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_COUNTER) || defined(WOLFSSL_AES_DIRECT)
  8995. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8996. if (enc) {
  8997. if (ret != -5900) /* note this must match ERRROR_OUT() code
  8998. * for wc_AesInit(enc, ...) failure above.
  8999. */
  9000. wc_AesFree(enc);
  9001. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  9002. }
  9003. #else
  9004. if (ret != -5900)
  9005. wc_AesFree(enc);
  9006. #endif
  9007. (void)cipher;
  9008. #ifdef HAVE_AES_DECRYPT
  9009. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9010. if (dec) {
  9011. if ((ret != -5900) && (ret != -5901))
  9012. /* note these codes must match the ERRROR_OUT() codes for
  9013. * wc_AesInit() failures above.
  9014. */
  9015. wc_AesFree(dec);
  9016. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  9017. }
  9018. #else
  9019. if ((ret != -5900) && (ret != -5901))
  9020. wc_AesFree(dec);
  9021. #endif
  9022. (void)plain;
  9023. #endif /* HAVE_AES_DECRYPT */
  9024. #endif /* HAVE_AES_CBC || WOLFSSL_AES_COUNTER || WOLFSSL_AES_DIRECT */
  9025. return ret;
  9026. }
  9027. #ifdef WOLFSSL_AES_192
  9028. WOLFSSL_TEST_SUBROUTINE int aes192_test(void)
  9029. {
  9030. #ifdef HAVE_AES_CBC
  9031. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9032. Aes *enc = NULL;
  9033. #else
  9034. Aes enc[1];
  9035. #endif
  9036. byte cipher[AES_BLOCK_SIZE];
  9037. #ifdef HAVE_AES_DECRYPT
  9038. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9039. Aes *dec = NULL;
  9040. #else
  9041. Aes dec[1];
  9042. #endif
  9043. byte plain[AES_BLOCK_SIZE];
  9044. #endif
  9045. #endif /* HAVE_AES_CBC */
  9046. int ret = 0;
  9047. #ifdef HAVE_AES_CBC
  9048. /* Test vectors from NIST Special Publication 800-38A, 2001 Edition
  9049. * Appendix F.2.3 */
  9050. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = {
  9051. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  9052. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  9053. };
  9054. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  9055. {
  9056. 0x4f,0x02,0x1d,0xb2,0x43,0xbc,0x63,0x3d,
  9057. 0x71,0x78,0x18,0x3a,0x9f,0xa0,0x71,0xe8
  9058. };
  9059. WOLFSSL_SMALL_STACK_STATIC byte key[] = {
  9060. 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  9061. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  9062. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b
  9063. };
  9064. WOLFSSL_SMALL_STACK_STATIC byte iv[] = {
  9065. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  9066. 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F
  9067. };
  9068. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9069. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  9070. ERROR_OUT(-6008, out);
  9071. #ifdef HAVE_AES_DECRYPT
  9072. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  9073. ERROR_OUT(-6009, out);
  9074. #endif
  9075. #endif
  9076. if (wc_AesInit(enc, HEAP_HINT, devId) != 0)
  9077. ERROR_OUT(-6000, out);
  9078. #ifdef HAVE_AES_DECRYPT
  9079. if (wc_AesInit(dec, HEAP_HINT, devId) != 0)
  9080. ERROR_OUT(-6001, out);
  9081. #endif
  9082. ret = wc_AesSetKey(enc, key, (int) sizeof(key), iv, AES_ENCRYPTION);
  9083. if (ret != 0)
  9084. ERROR_OUT(-6002, out);
  9085. #ifdef HAVE_AES_DECRYPT
  9086. ret = wc_AesSetKey(dec, key, (int) sizeof(key), iv, AES_DECRYPTION);
  9087. if (ret != 0)
  9088. ERROR_OUT(-6003, out);
  9089. #endif
  9090. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  9091. ret = wc_AesCbcEncrypt(enc, cipher, msg, (int) sizeof(msg));
  9092. #if defined(WOLFSSL_ASYNC_CRYPT)
  9093. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  9094. #endif
  9095. if (ret != 0)
  9096. ERROR_OUT(-6004, out);
  9097. #ifdef HAVE_AES_DECRYPT
  9098. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  9099. ret = wc_AesCbcDecrypt(dec, plain, cipher, (int) sizeof(cipher));
  9100. #if defined(WOLFSSL_ASYNC_CRYPT)
  9101. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  9102. #endif
  9103. if (ret != 0)
  9104. ERROR_OUT(-6005, out);
  9105. if (XMEMCMP(plain, msg, (int) sizeof(plain))) {
  9106. ERROR_OUT(-6006, out);
  9107. }
  9108. #endif
  9109. if (XMEMCMP(cipher, verify, (int) sizeof(cipher)))
  9110. ERROR_OUT(-6007, out);
  9111. wc_AesFree(enc);
  9112. #ifdef HAVE_AES_DECRYPT
  9113. wc_AesFree(dec);
  9114. #endif
  9115. out:
  9116. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9117. if (enc)
  9118. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  9119. #ifdef HAVE_AES_DECRYPT
  9120. if (dec)
  9121. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  9122. #endif
  9123. #endif
  9124. #endif /* HAVE_AES_CBC */
  9125. return ret;
  9126. }
  9127. #endif /* WOLFSSL_AES_192 */
  9128. #ifdef WOLFSSL_AES_256
  9129. WOLFSSL_TEST_SUBROUTINE int aes256_test(void)
  9130. {
  9131. #ifdef HAVE_AES_CBC
  9132. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9133. Aes *enc = NULL;
  9134. #else
  9135. Aes enc[1];
  9136. #endif
  9137. byte cipher[AES_BLOCK_SIZE];
  9138. #ifdef HAVE_AES_DECRYPT
  9139. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9140. Aes *dec = NULL;
  9141. #else
  9142. Aes dec[1];
  9143. #endif
  9144. byte plain[AES_BLOCK_SIZE];
  9145. #endif
  9146. #endif /* HAVE_AES_CBC */
  9147. int ret = 0;
  9148. #ifdef HAVE_AES_CBC
  9149. /* Test vectors from NIST Special Publication 800-38A, 2001 Edition,
  9150. * Appendix F.2.5 */
  9151. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = {
  9152. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  9153. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  9154. };
  9155. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  9156. {
  9157. 0xf5,0x8c,0x4c,0x04,0xd6,0xe5,0xf1,0xba,
  9158. 0x77,0x9e,0xab,0xfb,0x5f,0x7b,0xfb,0xd6
  9159. };
  9160. WOLFSSL_SMALL_STACK_STATIC byte key[] = {
  9161. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  9162. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  9163. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  9164. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  9165. };
  9166. WOLFSSL_SMALL_STACK_STATIC byte iv[] = {
  9167. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  9168. 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F
  9169. };
  9170. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9171. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  9172. ERROR_OUT(-6108, out);
  9173. #ifdef HAVE_AES_DECRYPT
  9174. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  9175. ERROR_OUT(-6109, out);
  9176. #endif
  9177. #endif
  9178. if (wc_AesInit(enc, HEAP_HINT, devId) != 0)
  9179. ERROR_OUT(-6100, out);
  9180. #ifdef HAVE_AES_DECRYPT
  9181. if (wc_AesInit(dec, HEAP_HINT, devId) != 0)
  9182. ERROR_OUT(-6101, out);
  9183. #endif
  9184. ret = wc_AesSetKey(enc, key, (int) sizeof(key), iv, AES_ENCRYPTION);
  9185. if (ret != 0)
  9186. ERROR_OUT(-6102, out);
  9187. #ifdef HAVE_AES_DECRYPT
  9188. ret = wc_AesSetKey(dec, key, (int) sizeof(key), iv, AES_DECRYPTION);
  9189. if (ret != 0)
  9190. ERROR_OUT(-6103, out);
  9191. #endif
  9192. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  9193. ret = wc_AesCbcEncrypt(enc, cipher, msg, (int) sizeof(msg));
  9194. #if defined(WOLFSSL_ASYNC_CRYPT)
  9195. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  9196. #endif
  9197. if (ret != 0)
  9198. ERROR_OUT(-6104, out);
  9199. #ifdef HAVE_AES_DECRYPT
  9200. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  9201. ret = wc_AesCbcDecrypt(dec, plain, cipher, (int) sizeof(cipher));
  9202. #if defined(WOLFSSL_ASYNC_CRYPT)
  9203. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  9204. #endif
  9205. if (ret != 0)
  9206. ERROR_OUT(-6105, out);
  9207. if (XMEMCMP(plain, msg, (int) sizeof(plain))) {
  9208. ERROR_OUT(-6106, out);
  9209. }
  9210. #endif
  9211. if (XMEMCMP(cipher, verify, (int) sizeof(cipher)))
  9212. ERROR_OUT(-6107, out);
  9213. wc_AesFree(enc);
  9214. #ifdef HAVE_AES_DECRYPT
  9215. wc_AesFree(dec);
  9216. #endif
  9217. out:
  9218. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9219. if (enc)
  9220. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  9221. #ifdef HAVE_AES_DECRYPT
  9222. if (dec)
  9223. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  9224. #endif
  9225. #endif
  9226. #endif /* HAVE_AES_CBC */
  9227. return ret;
  9228. }
  9229. #endif /* WOLFSSL_AES_256 */
  9230. #ifdef HAVE_AESGCM
  9231. #ifdef WOLFSSL_AES_128
  9232. static int aesgcm_default_test_helper(byte* key, int keySz, byte* iv, int ivSz,
  9233. byte* plain, int plainSz, byte* cipher, int cipherSz,
  9234. byte* aad, int aadSz, byte* tag, int tagSz)
  9235. {
  9236. int ret, enc_inited = 0, dec_inited = 0;
  9237. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9238. Aes *enc = NULL;
  9239. Aes *dec = NULL;
  9240. #else
  9241. Aes enc[1];
  9242. Aes dec[1];
  9243. #endif
  9244. byte resultT[AES_BLOCK_SIZE];
  9245. byte resultP[AES_BLOCK_SIZE * 3];
  9246. byte resultC[AES_BLOCK_SIZE * 3];
  9247. int result;
  9248. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9249. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  9250. ERROR_OUT(-6118, out);
  9251. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  9252. ERROR_OUT(-6119, out);
  9253. #endif
  9254. XMEMSET(resultT, 0, sizeof(resultT));
  9255. XMEMSET(resultC, 0, sizeof(resultC));
  9256. XMEMSET(resultP, 0, sizeof(resultP));
  9257. if (wc_AesInit(enc, HEAP_HINT, devId) != 0)
  9258. ERROR_OUT(-6110, out);
  9259. else
  9260. enc_inited = 1;
  9261. if (wc_AesInit(dec, HEAP_HINT, devId) != 0)
  9262. ERROR_OUT(-6111, out);
  9263. else
  9264. dec_inited = 1;
  9265. result = wc_AesGcmSetKey(enc, key, keySz);
  9266. if (result != 0)
  9267. ERROR_OUT(-6112, out);
  9268. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  9269. result = wc_AesGcmEncrypt(enc, resultC, plain, plainSz, iv, ivSz,
  9270. resultT, tagSz, aad, aadSz);
  9271. #if defined(WOLFSSL_ASYNC_CRYPT)
  9272. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  9273. #endif
  9274. if (result != 0)
  9275. ERROR_OUT(-6113, out);
  9276. if (cipher != NULL) {
  9277. if (XMEMCMP(cipher, resultC, cipherSz))
  9278. ERROR_OUT(-6114, out);
  9279. }
  9280. if (XMEMCMP(tag, resultT, tagSz))
  9281. ERROR_OUT(-6115, out);
  9282. #ifdef HAVE_AES_DECRYPT
  9283. result = wc_AesGcmSetKey(dec, key, keySz);
  9284. if (result != 0)
  9285. ERROR_OUT(-6116, out);
  9286. result = wc_AesGcmDecrypt(dec, resultP, resultC, cipherSz,
  9287. iv, ivSz, resultT, tagSz, aad, aadSz);
  9288. #if defined(WOLFSSL_ASYNC_CRYPT)
  9289. result = wc_AsyncWait(result, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  9290. #endif
  9291. if (result != 0)
  9292. ERROR_OUT(-6117, out);
  9293. if (plain != NULL) {
  9294. if (XMEMCMP(plain, resultP, plainSz))
  9295. ERROR_OUT(-6118, out);
  9296. }
  9297. #endif /* HAVE_AES_DECRYPT */
  9298. ret = 0;
  9299. out:
  9300. if (enc_inited)
  9301. wc_AesFree(enc);
  9302. if (dec_inited)
  9303. wc_AesFree(dec);
  9304. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9305. if (enc)
  9306. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  9307. if (dec)
  9308. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  9309. #endif
  9310. return ret;
  9311. }
  9312. #endif
  9313. /* tests that only use 12 byte IV and 16 or less byte AAD
  9314. * test vectors are from NIST SP 800-38D
  9315. * https://csrc.nist.gov/Projects/Cryptographic-Algorithm-Validation-Program/CAVP-TESTING-BLOCK-CIPHER-MODES*/
  9316. WOLFSSL_TEST_SUBROUTINE int aesgcm_default_test(void)
  9317. {
  9318. #ifdef WOLFSSL_AES_128
  9319. byte key1[] = {
  9320. 0x29, 0x8e, 0xfa, 0x1c, 0xcf, 0x29, 0xcf, 0x62,
  9321. 0xae, 0x68, 0x24, 0xbf, 0xc1, 0x95, 0x57, 0xfc
  9322. };
  9323. byte iv1[] = {
  9324. 0x6f, 0x58, 0xa9, 0x3f, 0xe1, 0xd2, 0x07, 0xfa,
  9325. 0xe4, 0xed, 0x2f, 0x6d
  9326. };
  9327. ALIGN64 byte plain1[] = {
  9328. 0xcc, 0x38, 0xbc, 0xcd, 0x6b, 0xc5, 0x36, 0xad,
  9329. 0x91, 0x9b, 0x13, 0x95, 0xf5, 0xd6, 0x38, 0x01,
  9330. 0xf9, 0x9f, 0x80, 0x68, 0xd6, 0x5c, 0xa5, 0xac,
  9331. 0x63, 0x87, 0x2d, 0xaf, 0x16, 0xb9, 0x39, 0x01
  9332. };
  9333. byte aad1[] = {
  9334. 0x02, 0x1f, 0xaf, 0xd2, 0x38, 0x46, 0x39, 0x73,
  9335. 0xff, 0xe8, 0x02, 0x56, 0xe5, 0xb1, 0xc6, 0xb1
  9336. };
  9337. ALIGN64 byte cipher1[] = {
  9338. 0xdf, 0xce, 0x4e, 0x9c, 0xd2, 0x91, 0x10, 0x3d,
  9339. 0x7f, 0xe4, 0xe6, 0x33, 0x51, 0xd9, 0xe7, 0x9d,
  9340. 0x3d, 0xfd, 0x39, 0x1e, 0x32, 0x67, 0x10, 0x46,
  9341. 0x58, 0x21, 0x2d, 0xa9, 0x65, 0x21, 0xb7, 0xdb
  9342. };
  9343. byte tag1[] = {
  9344. 0x54, 0x24, 0x65, 0xef, 0x59, 0x93, 0x16, 0xf7,
  9345. 0x3a, 0x7a, 0x56, 0x05, 0x09, 0xa2, 0xd9, 0xf2
  9346. };
  9347. byte key2[] = {
  9348. 0x01, 0x6d, 0xbb, 0x38, 0xda, 0xa7, 0x6d, 0xfe,
  9349. 0x7d, 0xa3, 0x84, 0xeb, 0xf1, 0x24, 0x03, 0x64
  9350. };
  9351. byte iv2[] = {
  9352. 0x07, 0x93, 0xef, 0x3a, 0xda, 0x78, 0x2f, 0x78,
  9353. 0xc9, 0x8a, 0xff, 0xe3
  9354. };
  9355. ALIGN64 byte plain2[] = {
  9356. 0x4b, 0x34, 0xa9, 0xec, 0x57, 0x63, 0x52, 0x4b,
  9357. 0x19, 0x1d, 0x56, 0x16, 0xc5, 0x47, 0xf6, 0xb7
  9358. };
  9359. ALIGN64 byte cipher2[] = {
  9360. 0x60, 0x9a, 0xa3, 0xf4, 0x54, 0x1b, 0xc0, 0xfe,
  9361. 0x99, 0x31, 0xda, 0xad, 0x2e, 0xe1, 0x5d, 0x0c
  9362. };
  9363. byte tag2[] = {
  9364. 0x33, 0xaf, 0xec, 0x59, 0xc4, 0x5b, 0xaf, 0x68,
  9365. 0x9a, 0x5e, 0x1b, 0x13, 0xae, 0x42, 0x36, 0x19
  9366. };
  9367. byte key3[] = {
  9368. 0xb0, 0x1e, 0x45, 0xcc, 0x30, 0x88, 0xaa, 0xba,
  9369. 0x9f, 0xa4, 0x3d, 0x81, 0xd4, 0x81, 0x82, 0x3f
  9370. };
  9371. byte iv3[] = {
  9372. 0x5a, 0x2c, 0x4a, 0x66, 0x46, 0x87, 0x13, 0x45,
  9373. 0x6a, 0x4b, 0xd5, 0xe1
  9374. };
  9375. byte tag3[] = {
  9376. 0x01, 0x42, 0x80, 0xf9, 0x44, 0xf5, 0x3c, 0x68,
  9377. 0x11, 0x64, 0xb2, 0xff
  9378. };
  9379. int ret;
  9380. ret = aesgcm_default_test_helper(key1, sizeof(key1), iv1, sizeof(iv1),
  9381. plain1, sizeof(plain1), cipher1, sizeof(cipher1),
  9382. aad1, sizeof(aad1), tag1, sizeof(tag1));
  9383. if (ret != 0) {
  9384. return ret;
  9385. }
  9386. ret = aesgcm_default_test_helper(key2, sizeof(key2), iv2, sizeof(iv2),
  9387. plain2, sizeof(plain2), cipher2, sizeof(cipher2),
  9388. NULL, 0, tag2, sizeof(tag2));
  9389. if (ret != 0) {
  9390. return ret;
  9391. }
  9392. ret = aesgcm_default_test_helper(key3, sizeof(key3), iv3, sizeof(iv3),
  9393. NULL, 0, NULL, 0,
  9394. NULL, 0, tag3, sizeof(tag3));
  9395. if (ret != 0) {
  9396. return ret;
  9397. }
  9398. #endif
  9399. return 0;
  9400. }
  9401. WOLFSSL_TEST_SUBROUTINE int aesgcm_test(void)
  9402. {
  9403. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9404. Aes *enc = NULL;
  9405. Aes *dec = NULL;
  9406. #else
  9407. Aes enc[1];
  9408. Aes dec[1];
  9409. #endif
  9410. /*
  9411. * This is Test Case 16 from the document Galois/
  9412. * Counter Mode of Operation (GCM) by McGrew and
  9413. * Viega.
  9414. */
  9415. WOLFSSL_SMALL_STACK_STATIC const byte p[] =
  9416. {
  9417. 0xd9, 0x31, 0x32, 0x25, 0xf8, 0x84, 0x06, 0xe5,
  9418. 0xa5, 0x59, 0x09, 0xc5, 0xaf, 0xf5, 0x26, 0x9a,
  9419. 0x86, 0xa7, 0xa9, 0x53, 0x15, 0x34, 0xf7, 0xda,
  9420. 0x2e, 0x4c, 0x30, 0x3d, 0x8a, 0x31, 0x8a, 0x72,
  9421. 0x1c, 0x3c, 0x0c, 0x95, 0x95, 0x68, 0x09, 0x53,
  9422. 0x2f, 0xcf, 0x0e, 0x24, 0x49, 0xa6, 0xb5, 0x25,
  9423. 0xb1, 0x6a, 0xed, 0xf5, 0xaa, 0x0d, 0xe6, 0x57,
  9424. 0xba, 0x63, 0x7b, 0x39
  9425. };
  9426. #if defined(WOLFSSL_AES_256) || defined(WOLFSSL_AES_192)
  9427. WOLFSSL_SMALL_STACK_STATIC const byte a[] =
  9428. {
  9429. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  9430. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  9431. 0xab, 0xad, 0xda, 0xd2
  9432. };
  9433. #endif
  9434. #ifdef WOLFSSL_AES_256
  9435. WOLFSSL_SMALL_STACK_STATIC const byte k1[] =
  9436. {
  9437. 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c,
  9438. 0x6d, 0x6a, 0x8f, 0x94, 0x67, 0x30, 0x83, 0x08,
  9439. 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c,
  9440. 0x6d, 0x6a, 0x8f, 0x94, 0x67, 0x30, 0x83, 0x08
  9441. };
  9442. WOLFSSL_SMALL_STACK_STATIC const byte iv1[] =
  9443. {
  9444. 0xca, 0xfe, 0xba, 0xbe, 0xfa, 0xce, 0xdb, 0xad,
  9445. 0xde, 0xca, 0xf8, 0x88
  9446. };
  9447. #endif /* WOLFSSL_AES_256 */
  9448. #if defined(WOLFSSL_AES_256) || defined(WOLFSSL_AES_192)
  9449. WOLFSSL_SMALL_STACK_STATIC const byte c1[] =
  9450. {
  9451. 0x52, 0x2d, 0xc1, 0xf0, 0x99, 0x56, 0x7d, 0x07,
  9452. 0xf4, 0x7f, 0x37, 0xa3, 0x2a, 0x84, 0x42, 0x7d,
  9453. 0x64, 0x3a, 0x8c, 0xdc, 0xbf, 0xe5, 0xc0, 0xc9,
  9454. 0x75, 0x98, 0xa2, 0xbd, 0x25, 0x55, 0xd1, 0xaa,
  9455. 0x8c, 0xb0, 0x8e, 0x48, 0x59, 0x0d, 0xbb, 0x3d,
  9456. 0xa7, 0xb0, 0x8b, 0x10, 0x56, 0x82, 0x88, 0x38,
  9457. 0xc5, 0xf6, 0x1e, 0x63, 0x93, 0xba, 0x7a, 0x0a,
  9458. 0xbc, 0xc9, 0xf6, 0x62
  9459. };
  9460. #endif /* WOLFSSL_AES_256 || WOLFSSL_AES_192 */
  9461. WOLFSSL_SMALL_STACK_STATIC const byte t1[] =
  9462. {
  9463. 0x76, 0xfc, 0x6e, 0xce, 0x0f, 0x4e, 0x17, 0x68,
  9464. 0xcd, 0xdf, 0x88, 0x53, 0xbb, 0x2d, 0x55, 0x1b
  9465. };
  9466. /* FIPS, QAT and PIC32MZ HW Crypto only support 12-byte IV */
  9467. #if !defined(HAVE_FIPS) && \
  9468. !defined(WOLFSSL_PIC32MZ_CRYPT) && \
  9469. !defined(FREESCALE_LTC) && !defined(FREESCALE_MMCAU) && \
  9470. !defined(WOLFSSL_XILINX_CRYPT) && !defined(WOLFSSL_AFALG_XILINX_AES) && \
  9471. !defined(WOLFSSL_SILABS_SE_ACCEL) && !defined(WOLFSSL_KCAPI_AES) && \
  9472. !(defined(WOLF_CRYPTO_CB) && \
  9473. (defined(HAVE_INTEL_QA_SYNC) || defined(HAVE_CAVIUM_OCTEON_SYNC)))
  9474. #define ENABLE_NON_12BYTE_IV_TEST
  9475. #ifdef WOLFSSL_AES_192
  9476. /* Test Case 12, uses same plaintext and AAD data. */
  9477. WOLFSSL_SMALL_STACK_STATIC const byte k2[] =
  9478. {
  9479. 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c,
  9480. 0x6d, 0x6a, 0x8f, 0x94, 0x67, 0x30, 0x83, 0x08,
  9481. 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c
  9482. };
  9483. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] =
  9484. {
  9485. 0x93, 0x13, 0x22, 0x5d, 0xf8, 0x84, 0x06, 0xe5,
  9486. 0x55, 0x90, 0x9c, 0x5a, 0xff, 0x52, 0x69, 0xaa,
  9487. 0x6a, 0x7a, 0x95, 0x38, 0x53, 0x4f, 0x7d, 0xa1,
  9488. 0xe4, 0xc3, 0x03, 0xd2, 0xa3, 0x18, 0xa7, 0x28,
  9489. 0xc3, 0xc0, 0xc9, 0x51, 0x56, 0x80, 0x95, 0x39,
  9490. 0xfc, 0xf0, 0xe2, 0x42, 0x9a, 0x6b, 0x52, 0x54,
  9491. 0x16, 0xae, 0xdb, 0xf5, 0xa0, 0xde, 0x6a, 0x57,
  9492. 0xa6, 0x37, 0xb3, 0x9b
  9493. };
  9494. WOLFSSL_SMALL_STACK_STATIC const byte c2[] =
  9495. {
  9496. 0xd2, 0x7e, 0x88, 0x68, 0x1c, 0xe3, 0x24, 0x3c,
  9497. 0x48, 0x30, 0x16, 0x5a, 0x8f, 0xdc, 0xf9, 0xff,
  9498. 0x1d, 0xe9, 0xa1, 0xd8, 0xe6, 0xb4, 0x47, 0xef,
  9499. 0x6e, 0xf7, 0xb7, 0x98, 0x28, 0x66, 0x6e, 0x45,
  9500. 0x81, 0xe7, 0x90, 0x12, 0xaf, 0x34, 0xdd, 0xd9,
  9501. 0xe2, 0xf0, 0x37, 0x58, 0x9b, 0x29, 0x2d, 0xb3,
  9502. 0xe6, 0x7c, 0x03, 0x67, 0x45, 0xfa, 0x22, 0xe7,
  9503. 0xe9, 0xb7, 0x37, 0x3b
  9504. };
  9505. WOLFSSL_SMALL_STACK_STATIC const byte t2[] =
  9506. {
  9507. 0xdc, 0xf5, 0x66, 0xff, 0x29, 0x1c, 0x25, 0xbb,
  9508. 0xb8, 0x56, 0x8f, 0xc3, 0xd3, 0x76, 0xa6, 0xd9
  9509. };
  9510. #endif /* WOLFSSL_AES_192 */
  9511. #ifdef WOLFSSL_AES_128
  9512. /* The following is an interesting test case from the example
  9513. * FIPS test vectors for AES-GCM. IVlen = 1 byte */
  9514. WOLFSSL_SMALL_STACK_STATIC const byte p3[] =
  9515. {
  9516. 0x57, 0xce, 0x45, 0x1f, 0xa5, 0xe2, 0x35, 0xa5,
  9517. 0x8e, 0x1a, 0xa2, 0x3b, 0x77, 0xcb, 0xaf, 0xe2
  9518. };
  9519. WOLFSSL_SMALL_STACK_STATIC const byte k3[] =
  9520. {
  9521. 0xbb, 0x01, 0xd7, 0x03, 0x81, 0x1c, 0x10, 0x1a,
  9522. 0x35, 0xe0, 0xff, 0xd2, 0x91, 0xba, 0xf2, 0x4b
  9523. };
  9524. WOLFSSL_SMALL_STACK_STATIC const byte iv3[] =
  9525. {
  9526. 0xca
  9527. };
  9528. WOLFSSL_SMALL_STACK_STATIC const byte c3[] =
  9529. {
  9530. 0x6b, 0x5f, 0xb3, 0x9d, 0xc1, 0xc5, 0x7a, 0x4f,
  9531. 0xf3, 0x51, 0x4d, 0xc2, 0xd5, 0xf0, 0xd0, 0x07
  9532. };
  9533. WOLFSSL_SMALL_STACK_STATIC const byte a3[] =
  9534. {
  9535. 0x40, 0xfc, 0xdc, 0xd7, 0x4a, 0xd7, 0x8b, 0xf1,
  9536. 0x3e, 0x7c, 0x60, 0x55, 0x50, 0x51, 0xdd, 0x54
  9537. };
  9538. WOLFSSL_SMALL_STACK_STATIC const byte t3[] =
  9539. {
  9540. 0x06, 0x90, 0xed, 0x01, 0x34, 0xdd, 0xc6, 0x95,
  9541. 0x31, 0x2e, 0x2a, 0xf9, 0x57, 0x7a, 0x1e, 0xa6
  9542. };
  9543. #endif /* WOLFSSL_AES_128 */
  9544. #ifdef WOLFSSL_AES_256
  9545. int ivlen;
  9546. #endif
  9547. #endif
  9548. byte resultT[sizeof(t1) + AES_BLOCK_SIZE];
  9549. byte resultP[sizeof(p) + AES_BLOCK_SIZE];
  9550. byte resultC[sizeof(p) + AES_BLOCK_SIZE];
  9551. int result = 0;
  9552. int ret;
  9553. #ifdef WOLFSSL_AES_256
  9554. #if !(defined(WOLF_CRYPTO_CB) && defined(HAVE_INTEL_QA_SYNC))
  9555. int alen;
  9556. #endif
  9557. #if !defined(WOLFSSL_AFALG_XILINX_AES) && !defined(WOLFSSL_XILINX_CRYPT)
  9558. int plen;
  9559. #endif
  9560. #endif
  9561. #if defined(WOLFSSL_XILINX_CRYPT_VERSAL)
  9562. byte buf[sizeof(p) + AES_BLOCK_SIZE];
  9563. byte bufA[sizeof(a) + 1];
  9564. byte *large_aad = (byte*)XMALLOC((size_t)1024 + 16, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9565. #endif
  9566. #if !defined(BENCH_EMBEDDED) && !defined(HAVE_CAVIUM)
  9567. #if !defined(BENCH_AESGCM_LARGE)
  9568. #define BENCH_AESGCM_LARGE 1024
  9569. #endif
  9570. #ifndef WOLFSSL_NO_MALLOC
  9571. byte *large_input = (byte *)XMALLOC(BENCH_AESGCM_LARGE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9572. byte *large_output = (byte *)XMALLOC(BENCH_AESGCM_LARGE + AES_BLOCK_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9573. byte *large_outdec = (byte *)XMALLOC(BENCH_AESGCM_LARGE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9574. if ((! large_input) || (! large_output) || (! large_outdec))
  9575. ERROR_OUT(MEMORY_E, out);
  9576. #else
  9577. byte large_input[BENCH_AESGCM_LARGE];
  9578. byte large_output[BENCH_AESGCM_LARGE];
  9579. byte large_outdec[BENCH_AESGCM_LARGE];
  9580. #endif
  9581. XMEMSET(large_input, 0, BENCH_AESGCM_LARGE);
  9582. XMEMSET(large_output, 0, BENCH_AESGCM_LARGE + AES_BLOCK_SIZE);
  9583. XMEMSET(large_outdec, 0, BENCH_AESGCM_LARGE);
  9584. #endif
  9585. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9586. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  9587. ERROR_OUT(-6342, out);
  9588. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  9589. ERROR_OUT(-6343, out);
  9590. #endif
  9591. (void)result;
  9592. XMEMSET(resultT, 0, sizeof(resultT));
  9593. XMEMSET(resultC, 0, sizeof(resultC));
  9594. XMEMSET(resultP, 0, sizeof(resultP));
  9595. if (wc_AesInit(enc, HEAP_HINT, devId) != 0) {
  9596. ERROR_OUT(-6300, out);
  9597. }
  9598. if (wc_AesInit(dec, HEAP_HINT, devId) != 0) {
  9599. ERROR_OUT(-6301, out);
  9600. }
  9601. #ifdef WOLFSSL_AES_256
  9602. result = wc_AesGcmSetKey(enc, k1, sizeof(k1));
  9603. if (result != 0)
  9604. ERROR_OUT(-6302, out);
  9605. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  9606. result = wc_AesGcmEncrypt(enc, resultC, p, sizeof(p), iv1, sizeof(iv1),
  9607. resultT, sizeof(t1), a, sizeof(a));
  9608. #if defined(WOLFSSL_ASYNC_CRYPT)
  9609. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  9610. #endif
  9611. if (result != 0)
  9612. ERROR_OUT(-6303, out);
  9613. if (XMEMCMP(c1, resultC, sizeof(c1)))
  9614. ERROR_OUT(-6304, out);
  9615. if (XMEMCMP(t1, resultT, sizeof(t1)))
  9616. ERROR_OUT(-6305, out);
  9617. #ifdef HAVE_AES_DECRYPT
  9618. result = wc_AesGcmSetKey(dec, k1, sizeof(k1));
  9619. if (result != 0)
  9620. ERROR_OUT(-6306, out);
  9621. result = wc_AesGcmDecrypt(dec, resultP, resultC, sizeof(c1),
  9622. iv1, sizeof(iv1), resultT, sizeof(t1), a, sizeof(a));
  9623. #if defined(WOLFSSL_ASYNC_CRYPT)
  9624. result = wc_AsyncWait(result, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  9625. #endif
  9626. if (result != 0)
  9627. ERROR_OUT(-6307, out);
  9628. if (XMEMCMP(p, resultP, sizeof(p)))
  9629. ERROR_OUT(-6308, out);
  9630. #endif /* HAVE_AES_DECRYPT */
  9631. /* Large buffer test */
  9632. #ifdef BENCH_AESGCM_LARGE
  9633. /* setup test buffer */
  9634. for (alen=0; alen<BENCH_AESGCM_LARGE; alen++)
  9635. large_input[alen] = (byte)alen;
  9636. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  9637. result = wc_AesGcmEncrypt(enc, large_output, large_input,
  9638. BENCH_AESGCM_LARGE, iv1, sizeof(iv1),
  9639. resultT, sizeof(t1), a, sizeof(a));
  9640. #if defined(WOLFSSL_ASYNC_CRYPT)
  9641. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  9642. #endif
  9643. if (result != 0)
  9644. ERROR_OUT(-6309, out);
  9645. #ifdef HAVE_AES_DECRYPT
  9646. result = wc_AesGcmDecrypt(dec, large_outdec, large_output,
  9647. BENCH_AESGCM_LARGE, iv1, sizeof(iv1), resultT,
  9648. sizeof(t1), a, sizeof(a));
  9649. #if defined(WOLFSSL_ASYNC_CRYPT)
  9650. result = wc_AsyncWait(result, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  9651. #endif
  9652. if (result != 0)
  9653. ERROR_OUT(-6310, out);
  9654. if (XMEMCMP(large_input, large_outdec, BENCH_AESGCM_LARGE))
  9655. ERROR_OUT(-6311, out);
  9656. #endif /* HAVE_AES_DECRYPT */
  9657. #endif /* BENCH_AESGCM_LARGE */
  9658. #if defined(ENABLE_NON_12BYTE_IV_TEST) && defined(WOLFSSL_AES_256)
  9659. /* Variable IV length test */
  9660. for (ivlen=1; ivlen<(int)sizeof(k1); ivlen++) {
  9661. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  9662. result = wc_AesGcmEncrypt(enc, resultC, p, sizeof(p), k1,
  9663. (word32)ivlen, resultT, sizeof(t1), a, sizeof(a));
  9664. #if defined(WOLFSSL_ASYNC_CRYPT)
  9665. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  9666. #endif
  9667. if (result != 0)
  9668. ERROR_OUT(-6312, out);
  9669. #ifdef HAVE_AES_DECRYPT
  9670. result = wc_AesGcmDecrypt(dec, resultP, resultC, sizeof(c1), k1,
  9671. (word32)ivlen, resultT, sizeof(t1), a, sizeof(a));
  9672. #if defined(WOLFSSL_ASYNC_CRYPT)
  9673. result = wc_AsyncWait(result, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  9674. #endif
  9675. if (result != 0)
  9676. ERROR_OUT(-6313, out);
  9677. #endif /* HAVE_AES_DECRYPT */
  9678. }
  9679. #endif
  9680. #if !(defined(WOLF_CRYPTO_CB) && defined(HAVE_INTEL_QA_SYNC))
  9681. /* Variable authenticated data length test */
  9682. for (alen=0; alen<(int)sizeof(p); alen++) {
  9683. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  9684. result = wc_AesGcmEncrypt(enc, resultC, p, sizeof(p), iv1,
  9685. sizeof(iv1), resultT, sizeof(t1), p, (word32)alen);
  9686. #if defined(WOLFSSL_ASYNC_CRYPT)
  9687. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  9688. #endif
  9689. if (result != 0)
  9690. ERROR_OUT(-6314, out);
  9691. #ifdef HAVE_AES_DECRYPT
  9692. result = wc_AesGcmDecrypt(dec, resultP, resultC, sizeof(c1), iv1,
  9693. sizeof(iv1), resultT, sizeof(t1), p, (word32)alen);
  9694. #if defined(WOLFSSL_ASYNC_CRYPT)
  9695. result = wc_AsyncWait(result, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  9696. #endif
  9697. if (result != 0)
  9698. ERROR_OUT(-6315, out);
  9699. #endif /* HAVE_AES_DECRYPT */
  9700. }
  9701. #if defined(WOLFSSL_XILINX_CRYPT_VERSAL)
  9702. if (! large_aad)
  9703. ERROR_OUT(MEMORY_E, out);
  9704. XMEMSET(large_aad, 0, 1024+16);
  9705. /* Variable authenticated data length test */
  9706. for (alen=0; alen<=1024; alen+=16) {
  9707. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  9708. result = wc_AesGcmEncrypt(enc, resultC, p, sizeof(p), iv1,
  9709. sizeof(iv1), resultT, sizeof(t1), large_aad, (word32)alen);
  9710. if (result != 0)
  9711. ERROR_OUT(-6316, out);
  9712. #ifdef HAVE_AES_DECRYPT
  9713. result = wc_AesGcmDecrypt(dec, resultP, resultC, sizeof(c1), iv1,
  9714. sizeof(iv1), resultT, sizeof(t1), large_aad, (word32)alen);
  9715. if (result != 0)
  9716. ERROR_OUT(-6317, out);
  9717. #endif /* HAVE_AES_DECRYPT */
  9718. }
  9719. /* Test unaligned memory of all potential arguments */
  9720. result = wc_AesGcmSetKey(enc, k1, sizeof(k1));
  9721. if (result != 0)
  9722. ERROR_OUT(-6318, out);
  9723. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  9724. XMEMCPY(&buf[1], p, sizeof(p));
  9725. XMEMCPY(&bufA[1], a, sizeof(a));
  9726. result = wc_AesGcmEncrypt(enc, &resultC[1], &buf[1], sizeof(p), iv1, sizeof(iv1),
  9727. &resultT[1], sizeof(t1), &bufA[1], sizeof(a));
  9728. if (result != 0)
  9729. ERROR_OUT(-6319, out);
  9730. if (XMEMCMP(c1, &resultC[1], sizeof(c1)))
  9731. ERROR_OUT(-6320, out);
  9732. if (XMEMCMP(t1, &resultT[1], sizeof(t1)))
  9733. ERROR_OUT(-6321, out);
  9734. #ifdef HAVE_AES_DECRYPT
  9735. result = wc_AesGcmSetKey(dec, k1, sizeof(k1));
  9736. if (result != 0)
  9737. ERROR_OUT(-6322, out);
  9738. result = wc_AesGcmDecrypt(dec, &resultP[1], &resultC[1], sizeof(c1),
  9739. iv1, sizeof(iv1), &resultT[1], sizeof(t1), &bufA[1], sizeof(a));
  9740. if (result != 0)
  9741. ERROR_OUT(-6323, out);
  9742. if (XMEMCMP(p, &resultP[1], sizeof(p)))
  9743. ERROR_OUT(-6324, out);
  9744. #endif /* HAVE_AES_DECRYPT */
  9745. #endif /* Xilinx Versal */
  9746. #endif
  9747. #if !defined(WOLFSSL_AFALG_XILINX_AES) && !defined(WOLFSSL_XILINX_CRYPT)
  9748. #ifdef BENCH_AESGCM_LARGE
  9749. /* Variable plain text length test */
  9750. for (plen=1; plen<BENCH_AESGCM_LARGE; plen++) {
  9751. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  9752. result = wc_AesGcmEncrypt(enc, large_output, large_input,
  9753. plen, iv1, sizeof(iv1), resultT,
  9754. sizeof(t1), a, sizeof(a));
  9755. #if defined(WOLFSSL_ASYNC_CRYPT)
  9756. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  9757. #endif
  9758. if (result != 0)
  9759. ERROR_OUT(-6316, out);
  9760. #ifdef HAVE_AES_DECRYPT
  9761. result = wc_AesGcmDecrypt(dec, large_outdec, large_output,
  9762. plen, iv1, sizeof(iv1), resultT,
  9763. sizeof(t1), a, sizeof(a));
  9764. #if defined(WOLFSSL_ASYNC_CRYPT)
  9765. result = wc_AsyncWait(result, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  9766. #endif
  9767. if (result != 0)
  9768. ERROR_OUT(-6317, out);
  9769. #endif /* HAVE_AES_DECRYPT */
  9770. }
  9771. #else /* BENCH_AESGCM_LARGE */
  9772. /* Variable plain text length test */
  9773. for (plen=1; plen<(int)sizeof(p); plen++) {
  9774. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  9775. result = wc_AesGcmEncrypt(enc, resultC, p, (word32)plen, iv1,
  9776. sizeof(iv1), resultT, sizeof(t1), a, sizeof(a));
  9777. #if defined(WOLFSSL_ASYNC_CRYPT)
  9778. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  9779. #endif
  9780. if (result != 0)
  9781. ERROR_OUT(-6318, out);
  9782. #ifdef HAVE_AES_DECRYPT
  9783. result = wc_AesGcmDecrypt(dec, resultP, resultC, (word32)plen, iv1,
  9784. sizeof(iv1), resultT, sizeof(t1), a, sizeof(a));
  9785. #if defined(WOLFSSL_ASYNC_CRYPT)
  9786. result = wc_AsyncWait(result, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  9787. #endif
  9788. if (result != 0)
  9789. ERROR_OUT(-6319, out);
  9790. #endif /* HAVE_AES_DECRYPT */
  9791. }
  9792. #endif /* BENCH_AESGCM_LARGE */
  9793. #endif
  9794. #endif /* WOLFSSL_AES_256 */
  9795. /* test with IV != 12 bytes */
  9796. #ifdef ENABLE_NON_12BYTE_IV_TEST
  9797. XMEMSET(resultT, 0, sizeof(resultT));
  9798. XMEMSET(resultC, 0, sizeof(resultC));
  9799. XMEMSET(resultP, 0, sizeof(resultP));
  9800. #ifdef WOLFSSL_AES_192
  9801. wc_AesGcmSetKey(enc, k2, sizeof(k2));
  9802. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  9803. result = wc_AesGcmEncrypt(enc, resultC, p, sizeof(p), iv2, sizeof(iv2),
  9804. resultT, sizeof(t1), a, sizeof(a));
  9805. #if defined(WOLFSSL_ASYNC_CRYPT)
  9806. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  9807. #endif
  9808. if (result != 0)
  9809. ERROR_OUT(-6320, out);
  9810. if (XMEMCMP(c2, resultC, sizeof(c2)))
  9811. ERROR_OUT(-6321, out);
  9812. if (XMEMCMP(t2, resultT, sizeof(t1)))
  9813. ERROR_OUT(-6322, out);
  9814. #ifdef HAVE_AES_DECRYPT
  9815. result = wc_AesGcmDecrypt(enc, resultP, resultC, sizeof(c1),
  9816. iv2, sizeof(iv2), resultT, sizeof(t1), a, sizeof(a));
  9817. #if defined(WOLFSSL_ASYNC_CRYPT)
  9818. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  9819. #endif
  9820. if (result != 0)
  9821. ERROR_OUT(-6323, out);
  9822. if (XMEMCMP(p, resultP, sizeof(p)))
  9823. ERROR_OUT(-6324, out);
  9824. #endif /* HAVE_AES_DECRYPT */
  9825. XMEMSET(resultT, 0, sizeof(resultT));
  9826. XMEMSET(resultC, 0, sizeof(resultC));
  9827. XMEMSET(resultP, 0, sizeof(resultP));
  9828. #endif /* WOLFSSL_AES_192 */
  9829. #ifdef WOLFSSL_AES_128
  9830. wc_AesGcmSetKey(enc, k3, sizeof(k3));
  9831. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  9832. result = wc_AesGcmEncrypt(enc, resultC, p3, sizeof(p3), iv3, sizeof(iv3),
  9833. resultT, sizeof(t3), a3, sizeof(a3));
  9834. #if defined(WOLFSSL_ASYNC_CRYPT)
  9835. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  9836. #endif
  9837. if (result != 0)
  9838. ERROR_OUT(-6325, out);
  9839. if (XMEMCMP(c3, resultC, sizeof(c3)))
  9840. ERROR_OUT(-6326, out);
  9841. if (XMEMCMP(t3, resultT, sizeof(t3)))
  9842. ERROR_OUT(-6327, out);
  9843. #ifdef HAVE_AES_DECRYPT
  9844. result = wc_AesGcmDecrypt(enc, resultP, resultC, sizeof(c3),
  9845. iv3, sizeof(iv3), resultT, sizeof(t3), a3, sizeof(a3));
  9846. #if defined(WOLFSSL_ASYNC_CRYPT)
  9847. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  9848. #endif
  9849. if (result != 0)
  9850. ERROR_OUT(-6328, out);
  9851. if (XMEMCMP(p3, resultP, sizeof(p3)))
  9852. ERROR_OUT(-6329, out);
  9853. #endif /* HAVE_AES_DECRYPT */
  9854. #endif /* WOLFSSL_AES_128 */
  9855. #endif /* ENABLE_NON_12BYTE_IV_TEST */
  9856. #if defined(WOLFSSL_AES_256) && !defined(WOLFSSL_AFALG_XILINX_AES) && \
  9857. !defined(WOLFSSL_XILINX_CRYPT) && \
  9858. !(defined(WOLF_CRYPTO_CB) && \
  9859. defined(HAVE_INTEL_QA_SYNC) || defined(HAVE_CAVIUM_OCTEON_SYNC))
  9860. XMEMSET(resultT, 0, sizeof(resultT));
  9861. XMEMSET(resultC, 0, sizeof(resultC));
  9862. XMEMSET(resultP, 0, sizeof(resultP));
  9863. wc_AesGcmSetKey(enc, k1, sizeof(k1));
  9864. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  9865. result = wc_AesGcmEncrypt(enc, resultC, p, sizeof(p), iv1, sizeof(iv1),
  9866. resultT + 1, sizeof(t1) - 1, a, sizeof(a));
  9867. #if defined(WOLFSSL_ASYNC_CRYPT)
  9868. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  9869. #endif
  9870. if (result != 0)
  9871. ERROR_OUT(-6330, out);
  9872. if (XMEMCMP(c1, resultC, sizeof(c1)))
  9873. ERROR_OUT(-6331, out);
  9874. if (XMEMCMP(t1, resultT + 1, sizeof(t1) - 1))
  9875. ERROR_OUT(-6332, out);
  9876. #ifdef HAVE_AES_DECRYPT
  9877. result = wc_AesGcmDecrypt(enc, resultP, resultC, sizeof(p),
  9878. iv1, sizeof(iv1), resultT + 1, sizeof(t1) - 1, a, sizeof(a));
  9879. #if defined(WOLFSSL_ASYNC_CRYPT)
  9880. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  9881. #endif
  9882. if (result != 0)
  9883. ERROR_OUT(-6333, out);
  9884. if (XMEMCMP(p, resultP, sizeof(p)))
  9885. ERROR_OUT(-6334, out);
  9886. #endif /* HAVE_AES_DECRYPT */
  9887. #endif /* WOLFSSL_AES_256 */
  9888. #if !defined(HAVE_FIPS) || \
  9889. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))
  9890. /* Test encrypt with internally generated IV */
  9891. #if defined(WOLFSSL_AES_256) && !(defined(WC_NO_RNG) || defined(HAVE_SELFTEST)) \
  9892. && !(defined(WOLF_CRYPTO_CB) && defined(HAVE_CAVIUM_OCTEON_SYNC))
  9893. {
  9894. WC_RNG rng;
  9895. byte randIV[12];
  9896. result = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  9897. if (result != 0)
  9898. ERROR_OUT(-6335, out);
  9899. XMEMSET(randIV, 0, sizeof(randIV));
  9900. XMEMSET(resultT, 0, sizeof(resultT));
  9901. XMEMSET(resultC, 0, sizeof(resultC));
  9902. XMEMSET(resultP, 0, sizeof(resultP));
  9903. wc_AesGcmSetKey(enc, k1, sizeof(k1));
  9904. result = wc_AesGcmSetIV(enc, sizeof(randIV), NULL, 0, &rng);
  9905. if (result != 0)
  9906. ERROR_OUT(-6336, out);
  9907. result = wc_AesGcmEncrypt_ex(enc,
  9908. resultC, p, sizeof(p),
  9909. randIV, sizeof(randIV),
  9910. resultT, sizeof(t1),
  9911. a, sizeof(a));
  9912. #if defined(WOLFSSL_ASYNC_CRYPT)
  9913. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  9914. #endif
  9915. if (result != 0)
  9916. ERROR_OUT(-6337, out);
  9917. /* Check the IV has been set. */
  9918. {
  9919. word32 i, ivSum = 0;
  9920. for (i = 0; i < sizeof(randIV); i++)
  9921. ivSum += randIV[i];
  9922. if (ivSum == 0)
  9923. ERROR_OUT(-6338, out);
  9924. }
  9925. #ifdef HAVE_AES_DECRYPT
  9926. wc_AesGcmSetKey(dec, k1, sizeof(k1));
  9927. result = wc_AesGcmSetIV(dec, sizeof(randIV), NULL, 0, &rng);
  9928. if (result != 0)
  9929. ERROR_OUT(-6339, out);
  9930. result = wc_AesGcmDecrypt(dec,
  9931. resultP, resultC, sizeof(c1),
  9932. randIV, sizeof(randIV),
  9933. resultT, sizeof(t1),
  9934. a, sizeof(a));
  9935. #if defined(WOLFSSL_ASYNC_CRYPT)
  9936. result = wc_AsyncWait(result, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  9937. #endif
  9938. if (result != 0)
  9939. ERROR_OUT(-6340, out);
  9940. if (XMEMCMP(p, resultP, sizeof(p)))
  9941. ERROR_OUT(-6341, out);
  9942. #endif /* HAVE_AES_DECRYPT */
  9943. wc_FreeRng(&rng);
  9944. }
  9945. #endif /* WOLFSSL_AES_256 && !(WC_NO_RNG || HAVE_SELFTEST) */
  9946. #endif /* HAVE_FIPS_VERSION >= 2 */
  9947. #if !defined(WOLFSSL_AFALG_XILINX_AES) && !defined(WOLFSSL_XILINX_CRYPT)
  9948. #ifdef WOLFSSL_AES_256
  9949. #ifdef WOLFSSL_AESGCM_STREAM
  9950. result = wc_AesGcmEncryptInit(enc, k1, sizeof(k1), iv1, sizeof(iv1));
  9951. if (result != 0)
  9952. ERROR_OUT(-6360, out);
  9953. result = wc_AesGcmEncryptUpdate(enc, resultC, p, sizeof(p), a, sizeof(a));
  9954. if (result != 0)
  9955. ERROR_OUT(-6361, out);
  9956. result = wc_AesGcmEncryptFinal(enc, resultT, sizeof(t1));
  9957. if (result != 0)
  9958. ERROR_OUT(-6362, out);
  9959. if (XMEMCMP(resultC, c1, sizeof(c1)) != 0)
  9960. ERROR_OUT(-6363, out);
  9961. if (XMEMCMP(resultT, t1, sizeof(t1)) != 0)
  9962. ERROR_OUT(-6364, out);
  9963. #ifdef HAVE_AES_DECRYPT
  9964. result = wc_AesGcmDecryptInit(enc, k1, sizeof(k1), iv1, sizeof(iv1));
  9965. if (result != 0)
  9966. ERROR_OUT(-6370, out);
  9967. result = wc_AesGcmDecryptUpdate(enc, resultP, c1, sizeof(c1), a, sizeof(a));
  9968. if (result != 0)
  9969. ERROR_OUT(-6371, out);
  9970. result = wc_AesGcmDecryptFinal(enc, t1, sizeof(t1));
  9971. if (result != 0)
  9972. ERROR_OUT(-6372, out);
  9973. if (XMEMCMP(resultP, p, sizeof(p)) != 0)
  9974. ERROR_OUT(-6373, out);
  9975. #endif
  9976. /* alen is the size to pass in with each update. */
  9977. for (alen = 1; alen < AES_BLOCK_SIZE + 1; alen++) {
  9978. result = wc_AesGcmEncryptInit(enc, k1, sizeof(k1), iv1, sizeof(iv1));
  9979. if (result != 0)
  9980. ERROR_OUT(-6380, out);
  9981. /* plen is the offset into AAD to update with. */
  9982. for (plen = 0; plen < (int)sizeof(a); plen += alen) {
  9983. int len = sizeof(a) - plen;
  9984. if (len > alen) len = alen;
  9985. result = wc_AesGcmEncryptUpdate(enc, NULL, NULL, 0, a + plen, len);
  9986. if (result != 0)
  9987. ERROR_OUT(-6381, out);
  9988. }
  9989. /* plen is the offset into plaintext to update with. */
  9990. for (plen = 0; plen < (int)sizeof(p); plen += alen) {
  9991. int len = sizeof(p) - plen;
  9992. if (len > alen) len = alen;
  9993. result = wc_AesGcmEncryptUpdate(enc, resultC + plen, p + plen, len,
  9994. NULL, 0);
  9995. if (result != 0)
  9996. ERROR_OUT(-6382, out);
  9997. }
  9998. result = wc_AesGcmEncryptFinal(enc, resultT, sizeof(t1));
  9999. if (result != 0)
  10000. ERROR_OUT(-6383, out);
  10001. if (XMEMCMP(resultC, c1, sizeof(c1)) != 0)
  10002. ERROR_OUT(-6384, out);
  10003. if (XMEMCMP(resultT, t1, sizeof(t1)) != 0)
  10004. ERROR_OUT(-6385, out);
  10005. }
  10006. #ifdef HAVE_AES_DECRYPT
  10007. for (alen = 1; alen < AES_BLOCK_SIZE + 1; alen++) {
  10008. result = wc_AesGcmDecryptInit(enc, k1, sizeof(k1), iv1, sizeof(iv1));
  10009. if (result != 0)
  10010. ERROR_OUT(-6390, out);
  10011. /* plen is the offset into AAD to update with. */
  10012. for (plen = 0; plen < (int)sizeof(a); plen += alen) {
  10013. int len = sizeof(a) - plen;
  10014. if (len > alen) len = alen;
  10015. result = wc_AesGcmDecryptUpdate(enc, NULL, NULL, 0, a + plen, len);
  10016. if (result != 0)
  10017. ERROR_OUT(-6391, out);
  10018. }
  10019. /* plen is the offset into cipher text to update with. */
  10020. for (plen = 0; plen < (int)sizeof(c1); plen += alen) {
  10021. int len = sizeof(c1) - plen;
  10022. if (len > alen) len = alen;
  10023. result = wc_AesGcmDecryptUpdate(enc, resultP + plen, c1 + plen, len,
  10024. NULL, 0);
  10025. if (result != 0)
  10026. ERROR_OUT(-6392, out);
  10027. }
  10028. result = wc_AesGcmDecryptFinal(enc, t1, sizeof(t1));
  10029. if (result != 0)
  10030. ERROR_OUT(-6393, out);
  10031. if (XMEMCMP(resultP, p, sizeof(p)) != 0)
  10032. ERROR_OUT(-6394, out);
  10033. }
  10034. #endif /* HAVE_AES_DECRYPT */
  10035. #ifdef BENCH_AESGCM_LARGE
  10036. /* setup test buffer */
  10037. result = wc_AesGcmEncryptInit(enc, k1, sizeof(k1), iv1, sizeof(iv1));
  10038. if (result != 0)
  10039. ERROR_OUT(-6360, out);
  10040. result = wc_AesGcmEncryptUpdate(enc, large_output, large_input,
  10041. BENCH_AESGCM_LARGE, a, sizeof(a));
  10042. if (result != 0)
  10043. ERROR_OUT(-6361, out);
  10044. result = wc_AesGcmEncryptFinal(enc, resultT, sizeof(t1));
  10045. if (result != 0)
  10046. ERROR_OUT(-6362, out);
  10047. #ifdef HAVE_AES_DECRYPT
  10048. result = wc_AesGcmDecryptInit(enc, k1, sizeof(k1), iv1, sizeof(iv1));
  10049. if (result != 0)
  10050. ERROR_OUT(-6363, out);
  10051. result = wc_AesGcmDecryptUpdate(enc, large_outdec, large_output,
  10052. BENCH_AESGCM_LARGE, a, sizeof(a));
  10053. if (result != 0)
  10054. ERROR_OUT(-6364, out);
  10055. result = wc_AesGcmDecryptFinal(enc, resultT, sizeof(t1));
  10056. if (result != 0)
  10057. ERROR_OUT(-6365, out);
  10058. if (XMEMCMP(large_input, large_outdec, BENCH_AESGCM_LARGE))
  10059. ERROR_OUT(-6366, out);
  10060. #endif /* HAVE_AES_DECRYPT */
  10061. #endif /* BENCH_AESGCM_LARGE */
  10062. #endif /* WOLFSSL_AESGCM_STREAM */
  10063. #endif /* WOLFSSL_AES_256 */
  10064. #endif /* !WOLFSSL_AFALG_XILINX_AES && !WOLFSSL_XILINX_CRYPT */
  10065. wc_AesFree(enc);
  10066. wc_AesFree(dec);
  10067. ret = 0;
  10068. out:
  10069. #if !defined(BENCH_EMBEDDED) && !defined(HAVE_CAVIUM) && \
  10070. !defined(WOLFSSL_NO_MALLOC)
  10071. if (large_input)
  10072. XFREE(large_input, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10073. if (large_output)
  10074. XFREE(large_output, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10075. if (large_outdec)
  10076. XFREE(large_outdec, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10077. #endif
  10078. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10079. if (enc)
  10080. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  10081. if (dec)
  10082. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  10083. #endif
  10084. return ret;
  10085. }
  10086. #ifdef WOLFSSL_AES_128
  10087. WOLFSSL_TEST_SUBROUTINE int gmac_test(void)
  10088. {
  10089. int ret;
  10090. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10091. Gmac *gmac;
  10092. #else
  10093. Gmac gmac[1];
  10094. #endif
  10095. WOLFSSL_SMALL_STACK_STATIC const byte k1[] =
  10096. {
  10097. 0x89, 0xc9, 0x49, 0xe9, 0xc8, 0x04, 0xaf, 0x01,
  10098. 0x4d, 0x56, 0x04, 0xb3, 0x94, 0x59, 0xf2, 0xc8
  10099. };
  10100. WOLFSSL_SMALL_STACK_STATIC const byte iv1[] =
  10101. {
  10102. 0xd1, 0xb1, 0x04, 0xc8, 0x15, 0xbf, 0x1e, 0x94,
  10103. 0xe2, 0x8c, 0x8f, 0x16
  10104. };
  10105. WOLFSSL_SMALL_STACK_STATIC const byte a1[] =
  10106. {
  10107. 0x82, 0xad, 0xcd, 0x63, 0x8d, 0x3f, 0xa9, 0xd9,
  10108. 0xf3, 0xe8, 0x41, 0x00, 0xd6, 0x1e, 0x07, 0x77
  10109. };
  10110. WOLFSSL_SMALL_STACK_STATIC const byte t1[] =
  10111. {
  10112. 0x88, 0xdb, 0x9d, 0x62, 0x17, 0x2e, 0xd0, 0x43,
  10113. 0xaa, 0x10, 0xf1, 0x6d, 0x22, 0x7d, 0xc4, 0x1b
  10114. };
  10115. #if (!defined(HAVE_FIPS) || \
  10116. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)))
  10117. /* FIPS builds only allow 16-byte auth tags. */
  10118. /* This sample uses a 15-byte auth tag. */
  10119. WOLFSSL_SMALL_STACK_STATIC const byte k2[] =
  10120. {
  10121. 0x40, 0xf7, 0xec, 0xb2, 0x52, 0x6d, 0xaa, 0xd4,
  10122. 0x74, 0x25, 0x1d, 0xf4, 0x88, 0x9e, 0xf6, 0x5b
  10123. };
  10124. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] =
  10125. {
  10126. 0xee, 0x9c, 0x6e, 0x06, 0x15, 0x45, 0x45, 0x03,
  10127. 0x1a, 0x60, 0x24, 0xa7
  10128. };
  10129. WOLFSSL_SMALL_STACK_STATIC const byte a2[] =
  10130. {
  10131. 0x94, 0x81, 0x2c, 0x87, 0x07, 0x4e, 0x15, 0x18,
  10132. 0x34, 0xb8, 0x35, 0xaf, 0x1c, 0xa5, 0x7e, 0x56
  10133. };
  10134. WOLFSSL_SMALL_STACK_STATIC const byte t2[] =
  10135. {
  10136. 0xc6, 0x81, 0x79, 0x8e, 0x3d, 0xda, 0xb0, 0x9f,
  10137. 0x8d, 0x83, 0xb0, 0xbb, 0x14, 0xb6, 0x91
  10138. };
  10139. #endif
  10140. byte tag[16];
  10141. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10142. if ((gmac = (Gmac *)XMALLOC(sizeof *gmac, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  10143. return -6409;
  10144. #endif
  10145. XMEMSET(gmac, 0, sizeof *gmac); /* clear context */
  10146. (void)wc_AesInit(&gmac->aes, HEAP_HINT, INVALID_DEVID); /* Make sure devId updated */
  10147. XMEMSET(tag, 0, sizeof(tag));
  10148. wc_GmacSetKey(gmac, k1, sizeof(k1));
  10149. wc_GmacUpdate(gmac, iv1, sizeof(iv1), a1, sizeof(a1), tag, sizeof(t1));
  10150. if (XMEMCMP(t1, tag, sizeof(t1)) != 0)
  10151. ERROR_OUT(-6400, out);
  10152. #if (!defined(HAVE_FIPS) || \
  10153. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)) )
  10154. XMEMSET(tag, 0, sizeof(tag));
  10155. wc_GmacSetKey(gmac, k2, sizeof(k2));
  10156. wc_GmacUpdate(gmac, iv2, sizeof(iv2), a2, sizeof(a2), tag, sizeof(t2));
  10157. if (XMEMCMP(t2, tag, sizeof(t2)) != 0)
  10158. ERROR_OUT(-6401, out);
  10159. #if !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && !defined(NO_AES_DECRYPT)
  10160. {
  10161. WOLFSSL_SMALL_STACK_STATIC const byte badT[] =
  10162. {
  10163. 0xde, 0xad, 0xbe, 0xef, 0x17, 0x2e, 0xd0, 0x43,
  10164. 0xaa, 0x10, 0xf1, 0x6d, 0x22, 0x7d, 0xc4, 0x1b
  10165. };
  10166. WC_RNG rng;
  10167. byte iv[12];
  10168. #ifndef HAVE_FIPS
  10169. if (wc_InitRng_ex(&rng, HEAP_HINT, devId) != 0)
  10170. ERROR_OUT(-6402, out);
  10171. #else
  10172. if (wc_InitRng(&rng) != 0)
  10173. ERROR_OUT(-6403, out);
  10174. #endif
  10175. if (wc_GmacVerify(k1, sizeof(k1), iv1, sizeof(iv1), a1, sizeof(a1),
  10176. t1, sizeof(t1)) != 0)
  10177. ERROR_OUT(-6404, out);
  10178. if (wc_GmacVerify(k1, sizeof(k1), iv1, sizeof(iv1), a1, sizeof(a1),
  10179. badT, sizeof(badT)) != AES_GCM_AUTH_E)
  10180. ERROR_OUT(-6405, out);
  10181. if (wc_GmacVerify(k2, sizeof(k2), iv2, sizeof(iv2), a2, sizeof(a2),
  10182. t2, sizeof(t2)) != 0)
  10183. ERROR_OUT(-6406, out);
  10184. XMEMSET(tag, 0, sizeof(tag));
  10185. XMEMSET(iv, 0, sizeof(iv));
  10186. if (wc_Gmac(k1, sizeof(k1), iv, sizeof(iv), a1, sizeof(a1),
  10187. tag, sizeof(tag), &rng) != 0)
  10188. ERROR_OUT(-6407, out);
  10189. if (wc_GmacVerify(k1, sizeof(k1), iv, sizeof(iv), a1, sizeof(a1),
  10190. tag, sizeof(tag)) != 0)
  10191. ERROR_OUT(-6408, out);
  10192. wc_FreeRng(&rng);
  10193. }
  10194. #endif /* !WC_NO_RNG && !HAVE_SELFTEST && !NO_AES_DECRYPT */
  10195. #endif /* HAVE_FIPS */
  10196. ret = 0;
  10197. out:
  10198. wc_AesFree(&gmac->aes);
  10199. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10200. XFREE(gmac, HEAP_HINT, DYNAMIC_TYPE_AES);
  10201. #endif
  10202. return ret;
  10203. }
  10204. #endif /* WOLFSSL_AES_128 */
  10205. #endif /* HAVE_AESGCM */
  10206. #if defined(HAVE_AESCCM)
  10207. #if defined(WOLFSSL_AES_256)
  10208. static int aesccm_256_test(void)
  10209. {
  10210. int ret;
  10211. /* Test vectors from NIST AES CCM 256-bit CAST Example #1 */
  10212. WOLFSSL_SMALL_STACK_STATIC const byte in_key[32] = {
  10213. 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
  10214. 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F,
  10215. 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  10216. 0x58, 0x59, 0x5A, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F
  10217. };
  10218. WOLFSSL_SMALL_STACK_STATIC const byte in_nonce[7] = {
  10219. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16};
  10220. WOLFSSL_SMALL_STACK_STATIC const byte in_auth[8] = {
  10221. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
  10222. WOLFSSL_SMALL_STACK_STATIC const byte in_plaintext[4] = {
  10223. 0x20, 0x21, 0x22, 0x23};
  10224. WOLFSSL_SMALL_STACK_STATIC const byte exp_ciphertext[4] = {
  10225. 0x8A, 0xB1, 0xA8, 0x74};
  10226. WOLFSSL_SMALL_STACK_STATIC const byte exp_tag[4] = {
  10227. 0x95, 0xFC, 0x08, 0x20};
  10228. byte output[sizeof(in_plaintext)];
  10229. byte atag[sizeof(exp_tag)];
  10230. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10231. Aes* aes = (Aes*)XMALLOC(sizeof(Aes), HEAP_HINT, DYNAMIC_TYPE_AES);
  10232. if (aes == NULL) {
  10233. return MEMORY_E;
  10234. }
  10235. #else
  10236. Aes aes[1];
  10237. #endif
  10238. ret = wc_AesInit(aes, HEAP_HINT, devId);
  10239. if (ret == 0) {
  10240. ret = wc_AesCcmSetKey(aes, in_key, sizeof(in_key));
  10241. }
  10242. if (ret == 0) {
  10243. ret = wc_AesCcmEncrypt(aes, output, in_plaintext, sizeof(in_plaintext),
  10244. in_nonce, sizeof(in_nonce),
  10245. atag, sizeof(atag),
  10246. in_auth, sizeof(in_auth));
  10247. }
  10248. /* Verify we produce the proper ciphertext and tag */
  10249. if (ret == 0 &&
  10250. (XMEMCMP(output, exp_ciphertext, sizeof(output)) ||
  10251. XMEMCMP(atag, exp_tag, sizeof(atag)))) {
  10252. ret = -1;
  10253. }
  10254. if (ret == 0) {
  10255. /* decrypt inline */
  10256. ret = wc_AesCcmDecrypt(aes, output, output, sizeof(output),
  10257. in_nonce, sizeof(in_nonce),
  10258. atag, sizeof(atag),
  10259. in_auth, sizeof(in_auth));
  10260. }
  10261. /* Verify decryption was successful */
  10262. if (ret == 0 &&
  10263. XMEMCMP(output, in_plaintext, sizeof(output))) {
  10264. ret = -1;
  10265. }
  10266. wc_AesFree(aes);
  10267. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10268. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_AES);
  10269. #endif
  10270. return ret;
  10271. }
  10272. #endif /* WOLFSSL_AES_256 */
  10273. #if defined(WOLFSSL_AES_128)
  10274. static int aesccm_128_test(void)
  10275. {
  10276. int ret;
  10277. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10278. Aes *enc;
  10279. #else
  10280. Aes enc[1];
  10281. #endif
  10282. /* key */
  10283. WOLFSSL_SMALL_STACK_STATIC const byte k[] =
  10284. {
  10285. 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
  10286. 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf
  10287. };
  10288. /* nonce */
  10289. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  10290. {
  10291. 0x00, 0x00, 0x00, 0x03, 0x02, 0x01, 0x00, 0xa0,
  10292. 0xa1, 0xa2, 0xa3, 0xa4, 0xa5
  10293. };
  10294. /* plaintext */
  10295. WOLFSSL_SMALL_STACK_STATIC const byte p[] =
  10296. {
  10297. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  10298. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  10299. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e
  10300. };
  10301. /* plaintext - long */
  10302. WOLFSSL_SMALL_STACK_STATIC const byte pl[] =
  10303. {
  10304. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  10305. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  10306. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
  10307. 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  10308. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
  10309. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  10310. 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
  10311. 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
  10312. 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
  10313. 0x50
  10314. };
  10315. WOLFSSL_SMALL_STACK_STATIC const byte a[] =
  10316. {
  10317. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
  10318. };
  10319. /* ciphertext */
  10320. WOLFSSL_SMALL_STACK_STATIC const byte c[] =
  10321. {
  10322. 0x58, 0x8c, 0x97, 0x9a, 0x61, 0xc6, 0x63, 0xd2,
  10323. 0xf0, 0x66, 0xd0, 0xc2, 0xc0, 0xf9, 0x89, 0x80,
  10324. 0x6d, 0x5f, 0x6b, 0x61, 0xda, 0xc3, 0x84
  10325. };
  10326. /* tag - authentication */
  10327. WOLFSSL_SMALL_STACK_STATIC const byte t[] =
  10328. {
  10329. 0x17, 0xe8, 0xd1, 0x2c, 0xfd, 0xf9, 0x26, 0xe0
  10330. };
  10331. /* ciphertext - long */
  10332. WOLFSSL_SMALL_STACK_STATIC const byte cl[] =
  10333. {
  10334. 0x58, 0x8c, 0x97, 0x9a, 0x61, 0xc6, 0x63, 0xd2,
  10335. 0xf0, 0x66, 0xd0, 0xc2, 0xc0, 0xf9, 0x89, 0x80,
  10336. 0x6d, 0x5f, 0x6b, 0x61, 0xda, 0xc3, 0x84, 0xe0,
  10337. 0x44, 0x2d, 0xbe, 0x25, 0xfa, 0x48, 0x2b, 0xa8,
  10338. 0x36, 0x0b, 0xbf, 0x01, 0xc0, 0x12, 0x45, 0xa4,
  10339. 0x82, 0x9f, 0x20, 0x6c, 0xc3, 0xd6, 0xae, 0x5b,
  10340. 0x54, 0x8d, 0xd0, 0xb1, 0x69, 0x2c, 0xec, 0x5e,
  10341. 0x95, 0xa5, 0x6b, 0x48, 0xc3, 0xc6, 0xc8, 0x9e,
  10342. 0xc7, 0x92, 0x98, 0x9d, 0x26, 0x7d, 0x2a, 0x10,
  10343. 0x0b
  10344. };
  10345. /* tag - authentication - long */
  10346. WOLFSSL_SMALL_STACK_STATIC const byte tl[] =
  10347. {
  10348. 0x89, 0xd8, 0xd2, 0x02, 0xc5, 0xcf, 0xae, 0xf4
  10349. };
  10350. /* tag - authentication - empty plaintext */
  10351. WOLFSSL_SMALL_STACK_STATIC const byte t_empty[] =
  10352. {
  10353. 0xe4, 0x28, 0x8a, 0xc3, 0x78, 0x00, 0x0f, 0xf5
  10354. };
  10355. byte t2[sizeof(t)];
  10356. byte p2[sizeof(p)];
  10357. byte c2[sizeof(c)];
  10358. byte iv2[sizeof(iv)];
  10359. byte pl2[sizeof(pl)];
  10360. byte cl2[sizeof(cl)];
  10361. byte tl2[sizeof(tl)];
  10362. byte t_empty2[sizeof(t_empty)];
  10363. int result;
  10364. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10365. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  10366. return -6521;
  10367. #endif
  10368. XMEMSET(enc, 0, sizeof *enc); /* clear context */
  10369. XMEMSET(t2, 0, sizeof(t2));
  10370. XMEMSET(c2, 0, sizeof(c2));
  10371. XMEMSET(p2, 0, sizeof(p2));
  10372. result = wc_AesInit(enc, HEAP_HINT, devId);
  10373. if (result != 0)
  10374. ERROR_OUT(-6499, out);
  10375. result = wc_AesCcmSetKey(enc, k, sizeof(k));
  10376. if (result != 0)
  10377. ERROR_OUT(-6500, out);
  10378. /* AES-CCM encrypt and decrypt both use AES encrypt internally */
  10379. result = wc_AesCcmEncrypt(enc, c2, p, sizeof(c2), iv, sizeof(iv),
  10380. t2, sizeof(t2), a, sizeof(a));
  10381. if (result != 0)
  10382. ERROR_OUT(-6501, out);
  10383. if (XMEMCMP(c, c2, sizeof(c2)))
  10384. ERROR_OUT(-6502, out);
  10385. if (XMEMCMP(t, t2, sizeof(t2)))
  10386. ERROR_OUT(-6503, out);
  10387. result = wc_AesCcmDecrypt(enc, p2, c2, sizeof(p2), iv, sizeof(iv),
  10388. t2, sizeof(t2), a, sizeof(a));
  10389. if (result != 0)
  10390. ERROR_OUT(-6504, out);
  10391. if (XMEMCMP(p, p2, sizeof(p2)))
  10392. ERROR_OUT(-6505, out);
  10393. /* Test the authentication failure */
  10394. t2[0]++; /* Corrupt the authentication tag. */
  10395. result = wc_AesCcmDecrypt(enc, p2, c, sizeof(p2), iv, sizeof(iv),
  10396. t2, sizeof(t2), a, sizeof(a));
  10397. if (result == 0)
  10398. ERROR_OUT(-6506, out);
  10399. /* Clear c2 to compare against p2. p2 should be set to zero in case of
  10400. * authentication fail. */
  10401. XMEMSET(c2, 0, sizeof(c2));
  10402. if (XMEMCMP(p2, c2, sizeof(p2)))
  10403. ERROR_OUT(-6507, out);
  10404. wc_AesFree(enc);
  10405. XMEMSET(enc, 0, sizeof(Aes)); /* clear context */
  10406. XMEMSET(t2, 0, sizeof(t2));
  10407. XMEMSET(c2, 0, sizeof(c2));
  10408. XMEMSET(p2, 0, sizeof(p2));
  10409. XMEMSET(iv2, 0, sizeof(iv2));
  10410. #ifndef HAVE_SELFTEST
  10411. /* selftest build does not have wc_AesCcmSetNonce() or
  10412. * wc_AesCcmEncrypt_ex() */
  10413. if (wc_AesCcmSetKey(enc, k, sizeof(k)) != 0)
  10414. ERROR_OUT(-6508, out);
  10415. if (wc_AesCcmSetNonce(enc, iv, sizeof(iv)) != 0)
  10416. ERROR_OUT(-6509, out);
  10417. if (wc_AesCcmEncrypt_ex(enc, c2, p, sizeof(c2), iv2, sizeof(iv2),
  10418. t2, sizeof(t2), a, sizeof(a)) != 0)
  10419. ERROR_OUT(-6510, out);
  10420. if (XMEMCMP(iv, iv2, sizeof(iv2)))
  10421. ERROR_OUT(-6511, out);
  10422. if (XMEMCMP(c, c2, sizeof(c2)))
  10423. ERROR_OUT(-6512, out);
  10424. if (XMEMCMP(t, t2, sizeof(t2)))
  10425. ERROR_OUT(-6513, out);
  10426. #endif
  10427. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  10428. /* test fail on invalid IV sizes */
  10429. result = wc_AesCcmSetKey(enc, k, sizeof(k));
  10430. if (result != 0)
  10431. ERROR_OUT(-6514, out);
  10432. /* AES-CCM encrypt and decrypt both use AES encrypt internally */
  10433. result = wc_AesCcmEncrypt(enc, c2, p, sizeof(c2), iv, sizeof(iv),
  10434. t2, 1, a, sizeof(a));
  10435. if (result == 0) {
  10436. ERROR_OUT(-6515, out);
  10437. }
  10438. #endif
  10439. /* AES-CCM encrypt and decrypt both use AES encrypt internally */
  10440. result = wc_AesCcmEncrypt(enc, cl2, pl, sizeof(cl2), iv, sizeof(iv),
  10441. tl2, sizeof(tl2), a, sizeof(a));
  10442. if (result != 0)
  10443. ERROR_OUT(-6516, out);
  10444. if (XMEMCMP(cl, cl2, sizeof(cl2)))
  10445. ERROR_OUT(-6517, out);
  10446. if (XMEMCMP(tl, tl2, sizeof(tl2)))
  10447. ERROR_OUT(-6518, out);
  10448. result = wc_AesCcmDecrypt(enc, pl2, cl2, sizeof(pl2), iv, sizeof(iv),
  10449. tl2, sizeof(tl2), a, sizeof(a));
  10450. if (result != 0)
  10451. ERROR_OUT(-6519, out);
  10452. if (XMEMCMP(pl, pl2, sizeof(pl2)))
  10453. ERROR_OUT(-6520, out);
  10454. /* test empty message as null input or output with nonzero inSz. */
  10455. result = wc_AesCcmEncrypt(enc, pl2 /* out */, NULL /* in */, 1 /* inSz */,
  10456. iv, sizeof(iv), t_empty2, sizeof(t_empty2),
  10457. a, sizeof(a));
  10458. if (result != BAD_FUNC_ARG)
  10459. ERROR_OUT(-6527, out);
  10460. result = wc_AesCcmEncrypt(enc, NULL /* out */, (const byte *)"" /* in */, 1 /* inSz */,
  10461. iv, sizeof(iv), t_empty2, sizeof(t_empty2),
  10462. a, sizeof(a));
  10463. if (result != BAD_FUNC_ARG)
  10464. ERROR_OUT(-6528, out);
  10465. result = wc_AesCcmDecrypt(enc, pl2, NULL /* in */, 1 /* inSz */,
  10466. iv, sizeof(iv), t_empty2, sizeof(t_empty2), a,
  10467. sizeof(a));
  10468. if (result != BAD_FUNC_ARG)
  10469. ERROR_OUT(-6529, out);
  10470. result = wc_AesCcmDecrypt(enc, NULL /* out */, (const byte *)"" /* in */, 1 /* inSz */,
  10471. iv, sizeof(iv), t_empty2, sizeof(t_empty2), a,
  10472. sizeof(a));
  10473. if (result != BAD_FUNC_ARG)
  10474. ERROR_OUT(-6530, out);
  10475. /* test empty message as null input and output with zero inSz --
  10476. * must either succeed, or fail early with BAD_FUNC_ARG.
  10477. */
  10478. result = wc_AesCcmEncrypt(enc, NULL /* out */, NULL /* in */, 0 /* inSz */,
  10479. iv, sizeof(iv), t_empty2, sizeof(t_empty2),
  10480. a, sizeof(a));
  10481. if (result != BAD_FUNC_ARG) {
  10482. if (result != 0)
  10483. ERROR_OUT(-6521, out);
  10484. if (XMEMCMP(t_empty, t_empty2, sizeof(t_empty2)))
  10485. ERROR_OUT(-6522, out);
  10486. result = wc_AesCcmDecrypt(enc, NULL /* out */, NULL /* in */,
  10487. 0 /* inSz */, iv, sizeof(iv), t_empty2,
  10488. sizeof(t_empty2), a, sizeof(a));
  10489. if (result != 0)
  10490. ERROR_OUT(-6523, out);
  10491. }
  10492. /* test empty message as zero-length string -- must work. */
  10493. result = wc_AesCcmEncrypt(enc, pl2, (const byte *)"", 0 /* inSz */, iv,
  10494. sizeof(iv), t_empty2, sizeof(t_empty2), a,
  10495. sizeof(a));
  10496. if (result != 0)
  10497. ERROR_OUT(-6524, out);
  10498. if (XMEMCMP(t_empty, t_empty2, sizeof(t_empty2)))
  10499. ERROR_OUT(-6525, out);
  10500. result = wc_AesCcmDecrypt(enc, pl2, (const byte *)"", 0 /* inSz */,
  10501. iv, sizeof(iv), t_empty2, sizeof(t_empty2), a,
  10502. sizeof(a));
  10503. if (result != 0)
  10504. ERROR_OUT(-6526, out);
  10505. wc_AesFree(enc);
  10506. ret = 0;
  10507. out:
  10508. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10509. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  10510. #endif
  10511. return ret;
  10512. }
  10513. #endif /* WOLFSSL_AES_128 */
  10514. WOLFSSL_TEST_SUBROUTINE int aesccm_test(void)
  10515. {
  10516. int ret = 0;
  10517. #ifdef WOLFSSL_AES_128
  10518. if (ret == 0)
  10519. ret = aesccm_128_test();
  10520. #endif
  10521. #ifdef WOLFSSL_AES_256
  10522. if (ret == 0)
  10523. ret = aesccm_256_test();
  10524. #endif
  10525. return ret;
  10526. }
  10527. #endif /* HAVE_AESCCM */
  10528. #ifdef HAVE_AES_KEYWRAP
  10529. #define MAX_KEYWRAP_TEST_OUTLEN 40
  10530. #define MAX_KEYWRAP_TEST_PLAINLEN 32
  10531. typedef struct keywrapVector {
  10532. const byte* kek;
  10533. const byte* data;
  10534. const byte* verify;
  10535. word32 kekLen;
  10536. word32 dataLen;
  10537. word32 verifyLen;
  10538. } keywrapVector;
  10539. WOLFSSL_TEST_SUBROUTINE int aeskeywrap_test(void)
  10540. {
  10541. int wrapSz, plainSz, testSz, i;
  10542. /* test vectors from RFC 3394 (kek, data, verify) */
  10543. #ifdef WOLFSSL_AES_128
  10544. /* Wrap 128 bits of Key Data with a 128-bit KEK */
  10545. WOLFSSL_SMALL_STACK_STATIC const byte k1[] = {
  10546. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  10547. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  10548. };
  10549. WOLFSSL_SMALL_STACK_STATIC const byte d1[] = {
  10550. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  10551. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF
  10552. };
  10553. WOLFSSL_SMALL_STACK_STATIC const byte v1[] = {
  10554. 0x1F, 0xA6, 0x8B, 0x0A, 0x81, 0x12, 0xB4, 0x47,
  10555. 0xAE, 0xF3, 0x4B, 0xD8, 0xFB, 0x5A, 0x7B, 0x82,
  10556. 0x9D, 0x3E, 0x86, 0x23, 0x71, 0xD2, 0xCF, 0xE5
  10557. };
  10558. #endif /* WOLFSSL_AES_128 */
  10559. #ifdef WOLFSSL_AES_192
  10560. /* Wrap 128 bits of Key Data with a 192-bit KEK */
  10561. WOLFSSL_SMALL_STACK_STATIC const byte k2[] = {
  10562. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  10563. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  10564. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17
  10565. };
  10566. WOLFSSL_SMALL_STACK_STATIC const byte d2[] = {
  10567. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  10568. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF
  10569. };
  10570. WOLFSSL_SMALL_STACK_STATIC const byte v2[] = {
  10571. 0x96, 0x77, 0x8B, 0x25, 0xAE, 0x6C, 0xA4, 0x35,
  10572. 0xF9, 0x2B, 0x5B, 0x97, 0xC0, 0x50, 0xAE, 0xD2,
  10573. 0x46, 0x8A, 0xB8, 0xA1, 0x7A, 0xD8, 0x4E, 0x5D
  10574. };
  10575. #endif
  10576. #ifdef WOLFSSL_AES_256
  10577. /* Wrap 128 bits of Key Data with a 256-bit KEK */
  10578. WOLFSSL_SMALL_STACK_STATIC const byte k3[] = {
  10579. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  10580. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  10581. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  10582. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F
  10583. };
  10584. WOLFSSL_SMALL_STACK_STATIC const byte d3[] = {
  10585. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  10586. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF
  10587. };
  10588. WOLFSSL_SMALL_STACK_STATIC const byte v3[] = {
  10589. 0x64, 0xE8, 0xC3, 0xF9, 0xCE, 0x0F, 0x5B, 0xA2,
  10590. 0x63, 0xE9, 0x77, 0x79, 0x05, 0x81, 0x8A, 0x2A,
  10591. 0x93, 0xC8, 0x19, 0x1E, 0x7D, 0x6E, 0x8A, 0xE7
  10592. };
  10593. #endif
  10594. #ifdef WOLFSSL_AES_192
  10595. /* Wrap 192 bits of Key Data with a 192-bit KEK */
  10596. WOLFSSL_SMALL_STACK_STATIC const byte k4[] = {
  10597. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  10598. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  10599. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17
  10600. };
  10601. WOLFSSL_SMALL_STACK_STATIC const byte d4[] = {
  10602. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  10603. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF,
  10604. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
  10605. };
  10606. WOLFSSL_SMALL_STACK_STATIC const byte v4[] = {
  10607. 0x03, 0x1D, 0x33, 0x26, 0x4E, 0x15, 0xD3, 0x32,
  10608. 0x68, 0xF2, 0x4E, 0xC2, 0x60, 0x74, 0x3E, 0xDC,
  10609. 0xE1, 0xC6, 0xC7, 0xDD, 0xEE, 0x72, 0x5A, 0x93,
  10610. 0x6B, 0xA8, 0x14, 0x91, 0x5C, 0x67, 0x62, 0xD2
  10611. };
  10612. #endif
  10613. #ifdef WOLFSSL_AES_256
  10614. /* Wrap 192 bits of Key Data with a 256-bit KEK */
  10615. WOLFSSL_SMALL_STACK_STATIC const byte k5[] = {
  10616. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  10617. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  10618. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  10619. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F
  10620. };
  10621. WOLFSSL_SMALL_STACK_STATIC const byte d5[] = {
  10622. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  10623. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF,
  10624. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
  10625. };
  10626. WOLFSSL_SMALL_STACK_STATIC const byte v5[] = {
  10627. 0xA8, 0xF9, 0xBC, 0x16, 0x12, 0xC6, 0x8B, 0x3F,
  10628. 0xF6, 0xE6, 0xF4, 0xFB, 0xE3, 0x0E, 0x71, 0xE4,
  10629. 0x76, 0x9C, 0x8B, 0x80, 0xA3, 0x2C, 0xB8, 0x95,
  10630. 0x8C, 0xD5, 0xD1, 0x7D, 0x6B, 0x25, 0x4D, 0xA1
  10631. };
  10632. /* Wrap 256 bits of Key Data with a 256-bit KEK */
  10633. WOLFSSL_SMALL_STACK_STATIC const byte k6[] = {
  10634. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  10635. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  10636. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  10637. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F
  10638. };
  10639. WOLFSSL_SMALL_STACK_STATIC const byte d6[] = {
  10640. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  10641. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF,
  10642. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  10643. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  10644. };
  10645. WOLFSSL_SMALL_STACK_STATIC const byte v6[] = {
  10646. 0x28, 0xC9, 0xF4, 0x04, 0xC4, 0xB8, 0x10, 0xF4,
  10647. 0xCB, 0xCC, 0xB3, 0x5C, 0xFB, 0x87, 0xF8, 0x26,
  10648. 0x3F, 0x57, 0x86, 0xE2, 0xD8, 0x0E, 0xD3, 0x26,
  10649. 0xCB, 0xC7, 0xF0, 0xE7, 0x1A, 0x99, 0xF4, 0x3B,
  10650. 0xFB, 0x98, 0x8B, 0x9B, 0x7A, 0x02, 0xDD, 0x21
  10651. };
  10652. #endif /* WOLFSSL_AES_256 */
  10653. byte output[MAX_KEYWRAP_TEST_OUTLEN];
  10654. byte plain [MAX_KEYWRAP_TEST_PLAINLEN];
  10655. const keywrapVector test_wrap[] =
  10656. {
  10657. #ifdef WOLFSSL_AES_128
  10658. {k1, d1, v1, sizeof(k1), sizeof(d1), sizeof(v1)},
  10659. #endif
  10660. #ifdef WOLFSSL_AES_192
  10661. {k2, d2, v2, sizeof(k2), sizeof(d2), sizeof(v2)},
  10662. #endif
  10663. #ifdef WOLFSSL_AES_256
  10664. {k3, d3, v3, sizeof(k3), sizeof(d3), sizeof(v3)},
  10665. #endif
  10666. #ifdef WOLFSSL_AES_192
  10667. {k4, d4, v4, sizeof(k4), sizeof(d4), sizeof(v4)},
  10668. #endif
  10669. #ifdef WOLFSSL_AES_256
  10670. {k5, d5, v5, sizeof(k5), sizeof(d5), sizeof(v5)},
  10671. {k6, d6, v6, sizeof(k6), sizeof(d6), sizeof(v6)}
  10672. #endif
  10673. };
  10674. testSz = sizeof(test_wrap) / sizeof(keywrapVector);
  10675. XMEMSET(output, 0, sizeof(output));
  10676. XMEMSET(plain, 0, sizeof(plain));
  10677. for (i = 0; i < testSz; i++) {
  10678. wrapSz = wc_AesKeyWrap(test_wrap[i].kek, test_wrap[i].kekLen,
  10679. test_wrap[i].data, test_wrap[i].dataLen,
  10680. output, sizeof(output), NULL);
  10681. if ( (wrapSz < 0) || (wrapSz != (int)test_wrap[i].verifyLen) )
  10682. return -6600;
  10683. if (XMEMCMP(output, test_wrap[i].verify, test_wrap[i].verifyLen) != 0)
  10684. return -6601;
  10685. plainSz = wc_AesKeyUnWrap((byte*)test_wrap[i].kek, test_wrap[i].kekLen,
  10686. output, wrapSz,
  10687. plain, sizeof(plain), NULL);
  10688. if ( (plainSz < 0) || (plainSz != (int)test_wrap[i].dataLen) )
  10689. return -6602;
  10690. if (XMEMCMP(plain, test_wrap[i].data, test_wrap[i].dataLen) != 0)
  10691. return -6603 - i;
  10692. }
  10693. return 0;
  10694. }
  10695. #endif /* HAVE_AES_KEYWRAP */
  10696. #endif /* NO_AES */
  10697. #ifdef HAVE_CAMELLIA
  10698. enum {
  10699. CAM_ECB_ENC, CAM_ECB_DEC, CAM_CBC_ENC, CAM_CBC_DEC
  10700. };
  10701. typedef struct {
  10702. int type;
  10703. const byte* plaintext;
  10704. const byte* iv;
  10705. const byte* ciphertext;
  10706. const byte* key;
  10707. word32 keySz;
  10708. int errorCode;
  10709. } test_vector_t;
  10710. WOLFSSL_TEST_SUBROUTINE int camellia_test(void)
  10711. {
  10712. /* Camellia ECB Test Plaintext */
  10713. WOLFSSL_SMALL_STACK_STATIC const byte pte[] =
  10714. {
  10715. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  10716. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
  10717. };
  10718. /* Camellia ECB Test Initialization Vector */
  10719. WOLFSSL_SMALL_STACK_STATIC const byte ive[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  10720. /* Test 1: Camellia ECB 128-bit key */
  10721. WOLFSSL_SMALL_STACK_STATIC const byte k1[] =
  10722. {
  10723. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  10724. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
  10725. };
  10726. WOLFSSL_SMALL_STACK_STATIC const byte c1[] =
  10727. {
  10728. 0x67, 0x67, 0x31, 0x38, 0x54, 0x96, 0x69, 0x73,
  10729. 0x08, 0x57, 0x06, 0x56, 0x48, 0xea, 0xbe, 0x43
  10730. };
  10731. /* Test 2: Camellia ECB 192-bit key */
  10732. WOLFSSL_SMALL_STACK_STATIC const byte k2[] =
  10733. {
  10734. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  10735. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  10736. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  10737. };
  10738. WOLFSSL_SMALL_STACK_STATIC const byte c2[] =
  10739. {
  10740. 0xb4, 0x99, 0x34, 0x01, 0xb3, 0xe9, 0x96, 0xf8,
  10741. 0x4e, 0xe5, 0xce, 0xe7, 0xd7, 0x9b, 0x09, 0xb9
  10742. };
  10743. /* Test 3: Camellia ECB 256-bit key */
  10744. WOLFSSL_SMALL_STACK_STATIC const byte k3[] =
  10745. {
  10746. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  10747. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  10748. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  10749. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff
  10750. };
  10751. WOLFSSL_SMALL_STACK_STATIC const byte c3[] =
  10752. {
  10753. 0x9a, 0xcc, 0x23, 0x7d, 0xff, 0x16, 0xd7, 0x6c,
  10754. 0x20, 0xef, 0x7c, 0x91, 0x9e, 0x3a, 0x75, 0x09
  10755. };
  10756. /* Camellia CBC Test Plaintext */
  10757. WOLFSSL_SMALL_STACK_STATIC const byte ptc[] =
  10758. {
  10759. 0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96,
  10760. 0xE9, 0x3D, 0x7E, 0x11, 0x73, 0x93, 0x17, 0x2A
  10761. };
  10762. /* Camellia CBC Test Initialization Vector */
  10763. WOLFSSL_SMALL_STACK_STATIC const byte ivc[] =
  10764. {
  10765. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  10766. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  10767. };
  10768. /* Test 4: Camellia-CBC 128-bit key */
  10769. WOLFSSL_SMALL_STACK_STATIC const byte k4[] =
  10770. {
  10771. 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6,
  10772. 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C
  10773. };
  10774. WOLFSSL_SMALL_STACK_STATIC const byte c4[] =
  10775. {
  10776. 0x16, 0x07, 0xCF, 0x49, 0x4B, 0x36, 0xBB, 0xF0,
  10777. 0x0D, 0xAE, 0xB0, 0xB5, 0x03, 0xC8, 0x31, 0xAB
  10778. };
  10779. /* Test 5: Camellia-CBC 192-bit key */
  10780. WOLFSSL_SMALL_STACK_STATIC const byte k5[] =
  10781. {
  10782. 0x8E, 0x73, 0xB0, 0xF7, 0xDA, 0x0E, 0x64, 0x52,
  10783. 0xC8, 0x10, 0xF3, 0x2B, 0x80, 0x90, 0x79, 0xE5,
  10784. 0x62, 0xF8, 0xEA, 0xD2, 0x52, 0x2C, 0x6B, 0x7B
  10785. };
  10786. WOLFSSL_SMALL_STACK_STATIC const byte c5[] =
  10787. {
  10788. 0x2A, 0x48, 0x30, 0xAB, 0x5A, 0xC4, 0xA1, 0xA2,
  10789. 0x40, 0x59, 0x55, 0xFD, 0x21, 0x95, 0xCF, 0x93
  10790. };
  10791. /* Test 6: CBC 256-bit key */
  10792. WOLFSSL_SMALL_STACK_STATIC const byte k6[] =
  10793. {
  10794. 0x60, 0x3D, 0xEB, 0x10, 0x15, 0xCA, 0x71, 0xBE,
  10795. 0x2B, 0x73, 0xAE, 0xF0, 0x85, 0x7D, 0x77, 0x81,
  10796. 0x1F, 0x35, 0x2C, 0x07, 0x3B, 0x61, 0x08, 0xD7,
  10797. 0x2D, 0x98, 0x10, 0xA3, 0x09, 0x14, 0xDF, 0xF4
  10798. };
  10799. WOLFSSL_SMALL_STACK_STATIC const byte c6[] =
  10800. {
  10801. 0xE6, 0xCF, 0xA3, 0x5F, 0xC0, 0x2B, 0x13, 0x4A,
  10802. 0x4D, 0x2C, 0x0B, 0x67, 0x37, 0xAC, 0x3E, 0xDA
  10803. };
  10804. byte out[CAMELLIA_BLOCK_SIZE];
  10805. Camellia cam;
  10806. int i, testsSz, ret;
  10807. WOLFSSL_SMALL_STACK_STATIC const test_vector_t testVectors[] =
  10808. {
  10809. {CAM_ECB_ENC, pte, ive, c1, k1, sizeof(k1), -114},
  10810. {CAM_ECB_ENC, pte, ive, c2, k2, sizeof(k2), -115},
  10811. {CAM_ECB_ENC, pte, ive, c3, k3, sizeof(k3), -116},
  10812. {CAM_ECB_DEC, pte, ive, c1, k1, sizeof(k1), -117},
  10813. {CAM_ECB_DEC, pte, ive, c2, k2, sizeof(k2), -118},
  10814. {CAM_ECB_DEC, pte, ive, c3, k3, sizeof(k3), -119},
  10815. {CAM_CBC_ENC, ptc, ivc, c4, k4, sizeof(k4), -120},
  10816. {CAM_CBC_ENC, ptc, ivc, c5, k5, sizeof(k5), -121},
  10817. {CAM_CBC_ENC, ptc, ivc, c6, k6, sizeof(k6), -122},
  10818. {CAM_CBC_DEC, ptc, ivc, c4, k4, sizeof(k4), -123},
  10819. {CAM_CBC_DEC, ptc, ivc, c5, k5, sizeof(k5), -124},
  10820. {CAM_CBC_DEC, ptc, ivc, c6, k6, sizeof(k6), -125}
  10821. };
  10822. testsSz = sizeof(testVectors)/sizeof(test_vector_t);
  10823. for (i = 0; i < testsSz; i++) {
  10824. if (wc_CamelliaSetKey(&cam, testVectors[i].key, testVectors[i].keySz,
  10825. testVectors[i].iv) != 0)
  10826. return testVectors[i].errorCode;
  10827. switch (testVectors[i].type) {
  10828. case CAM_ECB_ENC:
  10829. ret = wc_CamelliaEncryptDirect(&cam, out,
  10830. testVectors[i].plaintext);
  10831. if (ret != 0 || XMEMCMP(out, testVectors[i].ciphertext,
  10832. CAMELLIA_BLOCK_SIZE))
  10833. return testVectors[i].errorCode;
  10834. break;
  10835. case CAM_ECB_DEC:
  10836. ret = wc_CamelliaDecryptDirect(&cam, out,
  10837. testVectors[i].ciphertext);
  10838. if (ret != 0 || XMEMCMP(out, testVectors[i].plaintext,
  10839. CAMELLIA_BLOCK_SIZE))
  10840. return testVectors[i].errorCode;
  10841. break;
  10842. case CAM_CBC_ENC:
  10843. ret = wc_CamelliaCbcEncrypt(&cam, out, testVectors[i].plaintext,
  10844. CAMELLIA_BLOCK_SIZE);
  10845. if (ret != 0 || XMEMCMP(out, testVectors[i].ciphertext,
  10846. CAMELLIA_BLOCK_SIZE))
  10847. return testVectors[i].errorCode;
  10848. break;
  10849. case CAM_CBC_DEC:
  10850. ret = wc_CamelliaCbcDecrypt(&cam, out,
  10851. testVectors[i].ciphertext, CAMELLIA_BLOCK_SIZE);
  10852. if (ret != 0 || XMEMCMP(out, testVectors[i].plaintext,
  10853. CAMELLIA_BLOCK_SIZE))
  10854. return testVectors[i].errorCode;
  10855. break;
  10856. default:
  10857. break;
  10858. }
  10859. }
  10860. /* Setting the IV and checking it was actually set. */
  10861. ret = wc_CamelliaSetIV(&cam, ivc);
  10862. if (ret != 0 || XMEMCMP(cam.reg, ivc, CAMELLIA_BLOCK_SIZE))
  10863. return -6700;
  10864. /* Setting the IV to NULL should be same as all zeros IV */
  10865. if (wc_CamelliaSetIV(&cam, NULL) != 0 ||
  10866. XMEMCMP(cam.reg, ive, CAMELLIA_BLOCK_SIZE))
  10867. return -6701;
  10868. /* First parameter should never be null */
  10869. if (wc_CamelliaSetIV(NULL, NULL) == 0)
  10870. return -6702;
  10871. /* First parameter should never be null, check it fails */
  10872. if (wc_CamelliaSetKey(NULL, k1, sizeof(k1), NULL) == 0)
  10873. return -6703;
  10874. /* Key should have a size of 16, 24, or 32 */
  10875. if (wc_CamelliaSetKey(&cam, k1, 0, NULL) == 0)
  10876. return -6704;
  10877. return 0;
  10878. }
  10879. #endif /* HAVE_CAMELLIA */
  10880. #ifdef HAVE_XCHACHA
  10881. WOLFSSL_TEST_SUBROUTINE int XChaCha_test(void) {
  10882. int ret = -6830;
  10883. WOLFSSL_SMALL_STACK_STATIC const byte Plaintext[] = {
  10884. 0x54, 0x68, 0x65, 0x20, 0x64, 0x68, 0x6f, 0x6c, 0x65, 0x20, 0x28, 0x70, 0x72, 0x6f, 0x6e, 0x6f, /* The dhole (prono */
  10885. 0x75, 0x6e, 0x63, 0x65, 0x64, 0x20, 0x22, 0x64, 0x6f, 0x6c, 0x65, 0x22, 0x29, 0x20, 0x69, 0x73, /* unced "dole") is */
  10886. 0x20, 0x61, 0x6c, 0x73, 0x6f, 0x20, 0x6b, 0x6e, 0x6f, 0x77, 0x6e, 0x20, 0x61, 0x73, 0x20, 0x74, /* also known as t */
  10887. 0x68, 0x65, 0x20, 0x41, 0x73, 0x69, 0x61, 0x74, 0x69, 0x63, 0x20, 0x77, 0x69, 0x6c, 0x64, 0x20, /* he Asiatic wild */
  10888. 0x64, 0x6f, 0x67, 0x2c, 0x20, 0x72, 0x65, 0x64, 0x20, 0x64, 0x6f, 0x67, 0x2c, 0x20, 0x61, 0x6e, /* dog, red dog, an */
  10889. 0x64, 0x20, 0x77, 0x68, 0x69, 0x73, 0x74, 0x6c, 0x69, 0x6e, 0x67, 0x20, 0x64, 0x6f, 0x67, 0x2e, /* d whistling dog. */
  10890. 0x20, 0x49, 0x74, 0x20, 0x69, 0x73, 0x20, 0x61, 0x62, 0x6f, 0x75, 0x74, 0x20, 0x74, 0x68, 0x65, /* It is about the */
  10891. 0x20, 0x73, 0x69, 0x7a, 0x65, 0x20, 0x6f, 0x66, 0x20, 0x61, 0x20, 0x47, 0x65, 0x72, 0x6d, 0x61, /* size of a Germa */
  10892. 0x6e, 0x20, 0x73, 0x68, 0x65, 0x70, 0x68, 0x65, 0x72, 0x64, 0x20, 0x62, 0x75, 0x74, 0x20, 0x6c, /* n shepherd but l */
  10893. 0x6f, 0x6f, 0x6b, 0x73, 0x20, 0x6d, 0x6f, 0x72, 0x65, 0x20, 0x6c, 0x69, 0x6b, 0x65, 0x20, 0x61, /* ooks more like a */
  10894. 0x20, 0x6c, 0x6f, 0x6e, 0x67, 0x2d, 0x6c, 0x65, 0x67, 0x67, 0x65, 0x64, 0x20, 0x66, 0x6f, 0x78, /* long-legged fox */
  10895. 0x2e, 0x20, 0x54, 0x68, 0x69, 0x73, 0x20, 0x68, 0x69, 0x67, 0x68, 0x6c, 0x79, 0x20, 0x65, 0x6c, /* . This highly el */
  10896. 0x75, 0x73, 0x69, 0x76, 0x65, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x73, 0x6b, 0x69, 0x6c, 0x6c, 0x65, /* usive and skille */
  10897. 0x64, 0x20, 0x6a, 0x75, 0x6d, 0x70, 0x65, 0x72, 0x20, 0x69, 0x73, 0x20, 0x63, 0x6c, 0x61, 0x73, /* d jumper is clas */
  10898. 0x73, 0x69, 0x66, 0x69, 0x65, 0x64, 0x20, 0x77, 0x69, 0x74, 0x68, 0x20, 0x77, 0x6f, 0x6c, 0x76, /* sified with wolv */
  10899. 0x65, 0x73, 0x2c, 0x20, 0x63, 0x6f, 0x79, 0x6f, 0x74, 0x65, 0x73, 0x2c, 0x20, 0x6a, 0x61, 0x63, /* es, coyotes, jac */
  10900. 0x6b, 0x61, 0x6c, 0x73, 0x2c, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x66, 0x6f, 0x78, 0x65, 0x73, 0x20, /* kals, and foxes */
  10901. 0x69, 0x6e, 0x20, 0x74, 0x68, 0x65, 0x20, 0x74, 0x61, 0x78, 0x6f, 0x6e, 0x6f, 0x6d, 0x69, 0x63, /* in the taxonomic */
  10902. 0x20, 0x66, 0x61, 0x6d, 0x69, 0x6c, 0x79, 0x20, 0x43, 0x61, 0x6e, 0x69, 0x64, 0x61, 0x65, 0x2e /* family Canidae. */
  10903. };
  10904. WOLFSSL_SMALL_STACK_STATIC const byte Key[] = {
  10905. 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  10906. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f
  10907. };
  10908. WOLFSSL_SMALL_STACK_STATIC const byte IV[] = {
  10909. 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, /* @ABCDEFGHIJKLMNO */
  10910. 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x58 }; /* PQRSTUVW */
  10911. WOLFSSL_SMALL_STACK_STATIC const byte Ciphertext[] = {
  10912. 0x45, 0x59, 0xab, 0xba, 0x4e, 0x48, 0xc1, 0x61, 0x02, 0xe8, 0xbb, 0x2c, 0x05, 0xe6, 0x94, 0x7f,
  10913. 0x50, 0xa7, 0x86, 0xde, 0x16, 0x2f, 0x9b, 0x0b, 0x7e, 0x59, 0x2a, 0x9b, 0x53, 0xd0, 0xd4, 0xe9,
  10914. 0x8d, 0x8d, 0x64, 0x10, 0xd5, 0x40, 0xa1, 0xa6, 0x37, 0x5b, 0x26, 0xd8, 0x0d, 0xac, 0xe4, 0xfa,
  10915. 0xb5, 0x23, 0x84, 0xc7, 0x31, 0xac, 0xbf, 0x16, 0xa5, 0x92, 0x3c, 0x0c, 0x48, 0xd3, 0x57, 0x5d,
  10916. 0x4d, 0x0d, 0x2c, 0x67, 0x3b, 0x66, 0x6f, 0xaa, 0x73, 0x10, 0x61, 0x27, 0x77, 0x01, 0x09, 0x3a,
  10917. 0x6b, 0xf7, 0xa1, 0x58, 0xa8, 0x86, 0x42, 0x92, 0xa4, 0x1c, 0x48, 0xe3, 0xa9, 0xb4, 0xc0, 0xda,
  10918. 0xec, 0xe0, 0xf8, 0xd9, 0x8d, 0x0d, 0x7e, 0x05, 0xb3, 0x7a, 0x30, 0x7b, 0xbb, 0x66, 0x33, 0x31,
  10919. 0x64, 0xec, 0x9e, 0x1b, 0x24, 0xea, 0x0d, 0x6c, 0x3f, 0xfd, 0xdc, 0xec, 0x4f, 0x68, 0xe7, 0x44,
  10920. 0x30, 0x56, 0x19, 0x3a, 0x03, 0xc8, 0x10, 0xe1, 0x13, 0x44, 0xca, 0x06, 0xd8, 0xed, 0x8a, 0x2b,
  10921. 0xfb, 0x1e, 0x8d, 0x48, 0xcf, 0xa6, 0xbc, 0x0e, 0xb4, 0xe2, 0x46, 0x4b, 0x74, 0x81, 0x42, 0x40,
  10922. 0x7c, 0x9f, 0x43, 0x1a, 0xee, 0x76, 0x99, 0x60, 0xe1, 0x5b, 0xa8, 0xb9, 0x68, 0x90, 0x46, 0x6e,
  10923. 0xf2, 0x45, 0x75, 0x99, 0x85, 0x23, 0x85, 0xc6, 0x61, 0xf7, 0x52, 0xce, 0x20, 0xf9, 0xda, 0x0c,
  10924. 0x09, 0xab, 0x6b, 0x19, 0xdf, 0x74, 0xe7, 0x6a, 0x95, 0x96, 0x74, 0x46, 0xf8, 0xd0, 0xfd, 0x41,
  10925. 0x5e, 0x7b, 0xee, 0x2a, 0x12, 0xa1, 0x14, 0xc2, 0x0e, 0xb5, 0x29, 0x2a, 0xe7, 0xa3, 0x49, 0xae,
  10926. 0x57, 0x78, 0x20, 0xd5, 0x52, 0x0a, 0x1f, 0x3f, 0xb6, 0x2a, 0x17, 0xce, 0x6a, 0x7e, 0x68, 0xfa,
  10927. 0x7c, 0x79, 0x11, 0x1d, 0x88, 0x60, 0x92, 0x0b, 0xc0, 0x48, 0xef, 0x43, 0xfe, 0x84, 0x48, 0x6c,
  10928. 0xcb, 0x87, 0xc2, 0x5f, 0x0a, 0xe0, 0x45, 0xf0, 0xcc, 0xe1, 0xe7, 0x98, 0x9a, 0x9a, 0xa2, 0x20,
  10929. 0xa2, 0x8b, 0xdd, 0x48, 0x27, 0xe7, 0x51, 0xa2, 0x4a, 0x6d, 0x5c, 0x62, 0xd7, 0x90, 0xa6, 0x63,
  10930. 0x93, 0xb9, 0x31, 0x11, 0xc1, 0xa5, 0x5d, 0xd7, 0x42, 0x1a, 0x10, 0x18, 0x49, 0x74, 0xc7, 0xc5
  10931. };
  10932. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10933. struct ChaCha *chacha = (struct ChaCha *)XMALLOC(sizeof *chacha, HEAP_HINT, DYNAMIC_TYPE_CIPHER);
  10934. byte *buf1 = (byte *)XMALLOC(sizeof Plaintext, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10935. byte *buf2 = (byte *)XMALLOC(sizeof Plaintext, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10936. if ((chacha == NULL) || (buf1 == NULL) || (buf2 == NULL))
  10937. ERROR_OUT(MEMORY_E, out);
  10938. #else
  10939. struct ChaCha chacha[1];
  10940. byte buf1[sizeof Plaintext];
  10941. byte buf2[sizeof Plaintext];
  10942. #endif
  10943. ret = wc_XChacha_SetKey(chacha, Key, sizeof Key, IV, sizeof IV, 0);
  10944. if (ret < 0)
  10945. ERROR_OUT(-6831, out);
  10946. ret = wc_Chacha_Process(chacha, buf1, Plaintext, sizeof Plaintext);
  10947. if (ret < 0)
  10948. ERROR_OUT(-6832, out);
  10949. if (XMEMCMP(buf1, Ciphertext, sizeof Plaintext))
  10950. ERROR_OUT(-6833, out);
  10951. ret = wc_XChacha_SetKey(chacha, Key, sizeof Key, IV, sizeof IV, 0);
  10952. if (ret < 0)
  10953. ERROR_OUT(-6834, out);
  10954. ret = wc_Chacha_Process(chacha, buf2, buf1, sizeof Plaintext);
  10955. if (ret < 0)
  10956. ERROR_OUT(-6835, out);
  10957. if (XMEMCMP(buf2, Plaintext, sizeof Plaintext))
  10958. ERROR_OUT(-6836, out);
  10959. out:
  10960. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10961. if (chacha)
  10962. XFREE(chacha, HEAP_HINT, DYNAMIC_TYPE_CIPHER);
  10963. if (buf1)
  10964. XFREE(buf1, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10965. if (buf2)
  10966. XFREE(buf2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10967. #endif
  10968. return ret;
  10969. }
  10970. #endif /* HAVE_XCHACHA */
  10971. #if defined(HAVE_XCHACHA) && defined(HAVE_POLY1305)
  10972. WOLFSSL_TEST_SUBROUTINE int XChaCha20Poly1305_test(void) {
  10973. int ret;
  10974. WOLFSSL_SMALL_STACK_STATIC const byte Plaintext[] = {
  10975. 0x4c, 0x61, 0x64, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x47, 0x65, 0x6e, 0x74, 0x6c, /* Ladies and Gentl */
  10976. 0x65, 0x6d, 0x65, 0x6e, 0x20, 0x6f, 0x66, 0x20, 0x74, 0x68, 0x65, 0x20, 0x63, 0x6c, 0x61, 0x73, /* emen of the clas */
  10977. 0x73, 0x20, 0x6f, 0x66, 0x20, 0x27, 0x39, 0x39, 0x3a, 0x20, 0x49, 0x66, 0x20, 0x49, 0x20, 0x63, /* s of '99: If I c */
  10978. 0x6f, 0x75, 0x6c, 0x64, 0x20, 0x6f, 0x66, 0x66, 0x65, 0x72, 0x20, 0x79, 0x6f, 0x75, 0x20, 0x6f, /* ould offer you o */
  10979. 0x6e, 0x6c, 0x79, 0x20, 0x6f, 0x6e, 0x65, 0x20, 0x74, 0x69, 0x70, 0x20, 0x66, 0x6f, 0x72, 0x20, /* nly one tip for */
  10980. 0x74, 0x68, 0x65, 0x20, 0x66, 0x75, 0x74, 0x75, 0x72, 0x65, 0x2c, 0x20, 0x73, 0x75, 0x6e, 0x73, /* the future, suns */
  10981. 0x63, 0x72, 0x65, 0x65, 0x6e, 0x20, 0x77, 0x6f, 0x75, 0x6c, 0x64, 0x20, 0x62, 0x65, 0x20, 0x69, /* creen would be i */
  10982. 0x74, 0x2e }; /* t. */
  10983. WOLFSSL_SMALL_STACK_STATIC const byte AAD[] = { 0x50, 0x51, 0x52, 0x53, 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7 }; /* PQRS........ */
  10984. WOLFSSL_SMALL_STACK_STATIC const byte Key[] = {
  10985. 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  10986. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f
  10987. };
  10988. WOLFSSL_SMALL_STACK_STATIC const byte IV[] = {
  10989. 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, /* @ABCDEFGHIJKLMNO */
  10990. 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57 }; /* PQRSTUVW */
  10991. WOLFSSL_SMALL_STACK_STATIC const byte Ciphertext[] = {
  10992. 0xbd, 0x6d, 0x17, 0x9d, 0x3e, 0x83, 0xd4, 0x3b, 0x95, 0x76, 0x57, 0x94, 0x93, 0xc0, 0xe9, 0x39,
  10993. 0x57, 0x2a, 0x17, 0x00, 0x25, 0x2b, 0xfa, 0xcc, 0xbe, 0xd2, 0x90, 0x2c, 0x21, 0x39, 0x6c, 0xbb,
  10994. 0x73, 0x1c, 0x7f, 0x1b, 0x0b, 0x4a, 0xa6, 0x44, 0x0b, 0xf3, 0xa8, 0x2f, 0x4e, 0xda, 0x7e, 0x39,
  10995. 0xae, 0x64, 0xc6, 0x70, 0x8c, 0x54, 0xc2, 0x16, 0xcb, 0x96, 0xb7, 0x2e, 0x12, 0x13, 0xb4, 0x52,
  10996. 0x2f, 0x8c, 0x9b, 0xa4, 0x0d, 0xb5, 0xd9, 0x45, 0xb1, 0x1b, 0x69, 0xb9, 0x82, 0xc1, 0xbb, 0x9e,
  10997. 0x3f, 0x3f, 0xac, 0x2b, 0xc3, 0x69, 0x48, 0x8f, 0x76, 0xb2, 0x38, 0x35, 0x65, 0xd3, 0xff, 0xf9,
  10998. 0x21, 0xf9, 0x66, 0x4c, 0x97, 0x63, 0x7d, 0xa9, 0x76, 0x88, 0x12, 0xf6, 0x15, 0xc6, 0x8b, 0x13,
  10999. 0xb5, 0x2e };
  11000. WOLFSSL_SMALL_STACK_STATIC const byte Tag[] = {
  11001. 0xc0, 0x87, 0x59, 0x24, 0xc1, 0xc7, 0x98, 0x79, 0x47, 0xde, 0xaf, 0xd8, 0x78, 0x0a, 0xcf, 0x49
  11002. };
  11003. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  11004. byte *buf1 = (byte *)XMALLOC(sizeof Ciphertext + sizeof Tag, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11005. byte *buf2 = (byte *)XMALLOC(sizeof Plaintext, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11006. if ((buf1 == NULL) || (buf2 == NULL))
  11007. ERROR_OUT(-6480, out);
  11008. #else
  11009. byte buf1[sizeof Ciphertext + sizeof Tag];
  11010. byte buf2[sizeof Plaintext];
  11011. #endif
  11012. ret = wc_XChaCha20Poly1305_Encrypt(buf1, sizeof Ciphertext + sizeof Tag,
  11013. Plaintext, sizeof Plaintext,
  11014. AAD, sizeof AAD,
  11015. IV, sizeof IV,
  11016. Key, sizeof Key);
  11017. if (ret < 0)
  11018. ERROR_OUT(-6841, out);
  11019. if (XMEMCMP(buf1, Ciphertext, sizeof Ciphertext))
  11020. ERROR_OUT(-6842, out);
  11021. if (XMEMCMP(buf1 + sizeof Ciphertext, Tag, CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE))
  11022. ERROR_OUT(-6843, out);
  11023. ret = wc_XChaCha20Poly1305_Decrypt(buf2, sizeof Plaintext,
  11024. buf1, sizeof Ciphertext + sizeof Tag,
  11025. AAD, sizeof AAD,
  11026. IV, sizeof IV,
  11027. Key, sizeof Key);
  11028. if (ret < 0)
  11029. ERROR_OUT(-6844, out);
  11030. if (XMEMCMP(buf2, Plaintext, sizeof Plaintext))
  11031. ERROR_OUT(-6845, out);
  11032. out:
  11033. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  11034. if (buf1 != NULL)
  11035. XFREE(buf1, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11036. if (buf2 != NULL)
  11037. XFREE(buf2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11038. #endif
  11039. return ret;
  11040. }
  11041. #endif /* defined(HAVE_XCHACHA) && defined(HAVE_POLY1305) */
  11042. #ifndef WC_NO_RNG
  11043. static int _rng_test(WC_RNG* rng, int errorOffset)
  11044. {
  11045. byte block[32];
  11046. int ret, i;
  11047. XMEMSET(block, 0, sizeof(block));
  11048. ret = wc_RNG_GenerateBlock(rng, block, sizeof(block));
  11049. if (ret != 0) {
  11050. ret = -6850;
  11051. goto exit;
  11052. }
  11053. /* Check for 0's */
  11054. for (i=0; i<(int)sizeof(block); i++) {
  11055. if (block[i] == 0) {
  11056. ret++;
  11057. }
  11058. }
  11059. /* All zeros count check */
  11060. if (ret >= (int)sizeof(block)) {
  11061. ret = -6851;
  11062. goto exit;
  11063. }
  11064. ret = wc_RNG_GenerateByte(rng, block);
  11065. if (ret != 0) {
  11066. ret = -6852;
  11067. goto exit;
  11068. }
  11069. /* Parameter validation testing. */
  11070. ret = wc_RNG_GenerateBlock(NULL, block, sizeof(block));
  11071. if (ret != BAD_FUNC_ARG) {
  11072. ret = -6853;
  11073. goto exit;
  11074. }
  11075. ret = wc_RNG_GenerateBlock(rng, NULL, sizeof(block));
  11076. if (ret != BAD_FUNC_ARG) {
  11077. ret = -6854;
  11078. goto exit;
  11079. }
  11080. ret = wc_RNG_GenerateByte(NULL, block);
  11081. if (ret != BAD_FUNC_ARG) {
  11082. ret = -6855;
  11083. goto exit;
  11084. }
  11085. ret = wc_RNG_GenerateByte(rng, NULL);
  11086. if (ret != BAD_FUNC_ARG) {
  11087. ret = -6856;
  11088. goto exit;
  11089. }
  11090. ret = 0;
  11091. exit:
  11092. if (ret != 0)
  11093. ret += errorOffset;
  11094. return ret;
  11095. }
  11096. static int random_rng_test(void)
  11097. {
  11098. WC_RNG localRng;
  11099. WC_RNG* rng;
  11100. int ret;
  11101. rng = &localRng;
  11102. /* Test stack based RNG. */
  11103. #ifndef HAVE_FIPS
  11104. ret = wc_InitRng_ex(rng, HEAP_HINT, devId);
  11105. #else
  11106. ret = wc_InitRng(rng);
  11107. #endif
  11108. if (ret != 0) return -6900;
  11109. ret = _rng_test(rng, -6300);
  11110. /* Make sure and free RNG */
  11111. wc_FreeRng(rng);
  11112. if (ret != 0) return ret;
  11113. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && !defined(WOLFSSL_NO_MALLOC)
  11114. {
  11115. byte nonce[8] = { 0 };
  11116. /* Test dynamic RNG. */
  11117. rng = wc_rng_new(nonce, (word32)sizeof(nonce), HEAP_HINT);
  11118. if (rng == NULL) return -6901;
  11119. ret = _rng_test(rng, -6310);
  11120. wc_rng_free(rng);
  11121. }
  11122. #endif
  11123. return ret;
  11124. }
  11125. #if defined(HAVE_HASHDRBG) && !defined(CUSTOM_RAND_GENERATE_BLOCK)
  11126. #ifdef WC_RNG_SEED_CB
  11127. static int seed_cb(OS_Seed* os, byte* output, word32 sz)
  11128. {
  11129. word32 i;
  11130. (void)os;
  11131. /* Known answer test. Set the seed to the same value every time. */
  11132. for (i = 0; i < sz; i++)
  11133. output[i] = (byte)i;
  11134. return 0;
  11135. }
  11136. static int rng_seed_test(void)
  11137. {
  11138. #ifndef HAVE_FIPS
  11139. WOLFSSL_SMALL_STACK_STATIC const byte check[] =
  11140. {
  11141. 0x83, 0x46, 0x65, 0x2f, 0x5c, 0x44, 0x16, 0x5f,
  11142. 0xb3, 0x89, 0x26, 0xde, 0x0b, 0x6b, 0xa2, 0x06,
  11143. 0x7e, 0xa7, 0x9a, 0x55, 0x22, 0x01, 0xb0, 0x22,
  11144. 0xf4, 0x7e, 0xa2, 0x66, 0xc4, 0x08, 0x6f, 0xba
  11145. };
  11146. #else
  11147. /* FIPS uses a longer seed, so different check value. */
  11148. WOLFSSL_SMALL_STACK_STATIC const byte check[] =
  11149. {
  11150. 0xaf, 0x31, 0xcc, 0xef, 0xa9, 0x29, 0x4c, 0x24,
  11151. 0xbd, 0xa5, 0xa3, 0x52, 0x69, 0xf3, 0xb9, 0xb2,
  11152. 0x1e, 0xd4, 0x52, 0x3b, 0x9a, 0x96, 0x06, 0x20,
  11153. 0xc0, 0x5f, 0x44, 0x06, 0x1f, 0x80, 0xdf, 0xe0
  11154. };
  11155. #endif
  11156. byte output[WC_SHA256_DIGEST_SIZE];
  11157. WC_RNG rng;
  11158. int ret;
  11159. ret = wc_SetSeed_Cb(seed_cb);
  11160. if (ret != 0) {
  11161. ret = -7007;
  11162. goto exit;
  11163. }
  11164. ret = wc_InitRng(&rng);
  11165. if (ret != 0) {
  11166. ret = -7008;
  11167. goto exit;
  11168. }
  11169. ret = wc_RNG_GenerateBlock(&rng, output, sizeof(output));
  11170. if (ret != 0) {
  11171. ret = -7009;
  11172. goto exit;
  11173. }
  11174. ret = XMEMCMP(output, check, sizeof(output));
  11175. if (ret != 0) {
  11176. ret = -7010;
  11177. goto exit;
  11178. }
  11179. ret = wc_FreeRng(&rng);
  11180. if (ret != 0) {
  11181. ret = -7011;
  11182. goto exit;
  11183. }
  11184. ret = wc_SetSeed_Cb(wc_GenerateSeed);
  11185. if (ret != 0) {
  11186. ret = -7012;
  11187. }
  11188. exit:
  11189. return ret;
  11190. }
  11191. #endif
  11192. WOLFSSL_TEST_SUBROUTINE int random_test(void)
  11193. {
  11194. WOLFSSL_SMALL_STACK_STATIC const byte test1Entropy[] =
  11195. {
  11196. 0xa6, 0x5a, 0xd0, 0xf3, 0x45, 0xdb, 0x4e, 0x0e, 0xff, 0xe8, 0x75, 0xc3,
  11197. 0xa2, 0xe7, 0x1f, 0x42, 0xc7, 0x12, 0x9d, 0x62, 0x0f, 0xf5, 0xc1, 0x19,
  11198. 0xa9, 0xef, 0x55, 0xf0, 0x51, 0x85, 0xe0, 0xfb, 0x85, 0x81, 0xf9, 0x31,
  11199. 0x75, 0x17, 0x27, 0x6e, 0x06, 0xe9, 0x60, 0x7d, 0xdb, 0xcb, 0xcc, 0x2e
  11200. };
  11201. WOLFSSL_SMALL_STACK_STATIC const byte test1Output[] =
  11202. {
  11203. 0xd3, 0xe1, 0x60, 0xc3, 0x5b, 0x99, 0xf3, 0x40, 0xb2, 0x62, 0x82, 0x64,
  11204. 0xd1, 0x75, 0x10, 0x60, 0xe0, 0x04, 0x5d, 0xa3, 0x83, 0xff, 0x57, 0xa5,
  11205. 0x7d, 0x73, 0xa6, 0x73, 0xd2, 0xb8, 0xd8, 0x0d, 0xaa, 0xf6, 0xa6, 0xc3,
  11206. 0x5a, 0x91, 0xbb, 0x45, 0x79, 0xd7, 0x3f, 0xd0, 0xc8, 0xfe, 0xd1, 0x11,
  11207. 0xb0, 0x39, 0x13, 0x06, 0x82, 0x8a, 0xdf, 0xed, 0x52, 0x8f, 0x01, 0x81,
  11208. 0x21, 0xb3, 0xfe, 0xbd, 0xc3, 0x43, 0xe7, 0x97, 0xb8, 0x7d, 0xbb, 0x63,
  11209. 0xdb, 0x13, 0x33, 0xde, 0xd9, 0xd1, 0xec, 0xe1, 0x77, 0xcf, 0xa6, 0xb7,
  11210. 0x1f, 0xe8, 0xab, 0x1d, 0xa4, 0x66, 0x24, 0xed, 0x64, 0x15, 0xe5, 0x1c,
  11211. 0xcd, 0xe2, 0xc7, 0xca, 0x86, 0xe2, 0x83, 0x99, 0x0e, 0xea, 0xeb, 0x91,
  11212. 0x12, 0x04, 0x15, 0x52, 0x8b, 0x22, 0x95, 0x91, 0x02, 0x81, 0xb0, 0x2d,
  11213. 0xd4, 0x31, 0xf4, 0xc9, 0xf7, 0x04, 0x27, 0xdf
  11214. };
  11215. WOLFSSL_SMALL_STACK_STATIC const byte test2EntropyA[] =
  11216. {
  11217. 0x63, 0x36, 0x33, 0x77, 0xe4, 0x1e, 0x86, 0x46, 0x8d, 0xeb, 0x0a, 0xb4,
  11218. 0xa8, 0xed, 0x68, 0x3f, 0x6a, 0x13, 0x4e, 0x47, 0xe0, 0x14, 0xc7, 0x00,
  11219. 0x45, 0x4e, 0x81, 0xe9, 0x53, 0x58, 0xa5, 0x69, 0x80, 0x8a, 0xa3, 0x8f,
  11220. 0x2a, 0x72, 0xa6, 0x23, 0x59, 0x91, 0x5a, 0x9f, 0x8a, 0x04, 0xca, 0x68
  11221. };
  11222. WOLFSSL_SMALL_STACK_STATIC const byte test2EntropyB[] =
  11223. {
  11224. 0xe6, 0x2b, 0x8a, 0x8e, 0xe8, 0xf1, 0x41, 0xb6, 0x98, 0x05, 0x66, 0xe3,
  11225. 0xbf, 0xe3, 0xc0, 0x49, 0x03, 0xda, 0xd4, 0xac, 0x2c, 0xdf, 0x9f, 0x22,
  11226. 0x80, 0x01, 0x0a, 0x67, 0x39, 0xbc, 0x83, 0xd3
  11227. };
  11228. WOLFSSL_SMALL_STACK_STATIC const byte test2Output[] =
  11229. {
  11230. 0x04, 0xee, 0xc6, 0x3b, 0xb2, 0x31, 0xdf, 0x2c, 0x63, 0x0a, 0x1a, 0xfb,
  11231. 0xe7, 0x24, 0x94, 0x9d, 0x00, 0x5a, 0x58, 0x78, 0x51, 0xe1, 0xaa, 0x79,
  11232. 0x5e, 0x47, 0x73, 0x47, 0xc8, 0xb0, 0x56, 0x62, 0x1c, 0x18, 0xbd, 0xdc,
  11233. 0xdd, 0x8d, 0x99, 0xfc, 0x5f, 0xc2, 0xb9, 0x20, 0x53, 0xd8, 0xcf, 0xac,
  11234. 0xfb, 0x0b, 0xb8, 0x83, 0x12, 0x05, 0xfa, 0xd1, 0xdd, 0xd6, 0xc0, 0x71,
  11235. 0x31, 0x8a, 0x60, 0x18, 0xf0, 0x3b, 0x73, 0xf5, 0xed, 0xe4, 0xd4, 0xd0,
  11236. 0x71, 0xf9, 0xde, 0x03, 0xfd, 0x7a, 0xea, 0x10, 0x5d, 0x92, 0x99, 0xb8,
  11237. 0xaf, 0x99, 0xaa, 0x07, 0x5b, 0xdb, 0x4d, 0xb9, 0xaa, 0x28, 0xc1, 0x8d,
  11238. 0x17, 0x4b, 0x56, 0xee, 0x2a, 0x01, 0x4d, 0x09, 0x88, 0x96, 0xff, 0x22,
  11239. 0x82, 0xc9, 0x55, 0xa8, 0x19, 0x69, 0xe0, 0x69, 0xfa, 0x8c, 0xe0, 0x07,
  11240. 0xa1, 0x80, 0x18, 0x3a, 0x07, 0xdf, 0xae, 0x17
  11241. };
  11242. byte output[WC_SHA256_DIGEST_SIZE * 4];
  11243. int ret;
  11244. ret = wc_RNG_HealthTest(0, test1Entropy, sizeof(test1Entropy), NULL, 0,
  11245. output, sizeof(output));
  11246. if (ret != 0)
  11247. return -7000;
  11248. if (XMEMCMP(test1Output, output, sizeof(output)) != 0)
  11249. return -7001;
  11250. ret = wc_RNG_HealthTest(1, test2EntropyA, sizeof(test2EntropyA),
  11251. test2EntropyB, sizeof(test2EntropyB),
  11252. output, sizeof(output));
  11253. if (ret != 0)
  11254. return -7002;
  11255. if (XMEMCMP(test2Output, output, sizeof(output)) != 0)
  11256. return -7003;
  11257. /* Basic RNG generate block test */
  11258. if ((ret = random_rng_test()) != 0)
  11259. return ret;
  11260. /* Test the seed check function. */
  11261. #if !(defined(HAVE_FIPS) || defined(HAVE_SELFTEST)) || \
  11262. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))
  11263. {
  11264. word32 i, outputSz;
  11265. /* Repeat the same byte over and over. Should fail. */
  11266. outputSz = sizeof(output);
  11267. XMEMSET(output, 1, outputSz);
  11268. ret = wc_RNG_TestSeed(output, outputSz);
  11269. if (ret == 0)
  11270. return -7004;
  11271. /* Every byte of the entropy scratch is different,
  11272. * entropy is a single byte that shouldn't match. */
  11273. outputSz = (sizeof(output) / 2) + 1;
  11274. for (i = 0; i < outputSz; i++)
  11275. output[i] = (byte)i;
  11276. ret = wc_RNG_TestSeed(output, outputSz);
  11277. if (ret != 0)
  11278. return -7005;
  11279. outputSz = sizeof(output);
  11280. for (i = 0; i < outputSz; i++)
  11281. output[i] = (byte)i;
  11282. ret = wc_RNG_TestSeed(output, outputSz);
  11283. if (ret != 0)
  11284. return -7006;
  11285. }
  11286. #endif
  11287. /* Test the seed callback. */
  11288. #ifdef WC_RNG_SEED_CB
  11289. if ((ret = rng_seed_test()) != 0)
  11290. return ret;
  11291. #endif
  11292. return 0;
  11293. }
  11294. #else
  11295. WOLFSSL_TEST_SUBROUTINE int random_test(void)
  11296. {
  11297. /* Basic RNG generate block test */
  11298. return random_rng_test();
  11299. }
  11300. #endif /* HAVE_HASHDRBG && !CUSTOM_RAND_GENERATE_BLOCK */
  11301. #endif /* WC_NO_RNG */
  11302. #ifndef MEM_TEST_SZ
  11303. #define MEM_TEST_SZ 1024
  11304. #endif
  11305. #if defined(WOLFSSL_STATIC_MEMORY) || !defined(WOLFSSL_NO_MALLOC)
  11306. static int simple_mem_test(int sz)
  11307. {
  11308. int ret = 0;
  11309. byte* b;
  11310. int i;
  11311. b = (byte*)XMALLOC(sz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11312. if (b == NULL) {
  11313. return -7110;
  11314. }
  11315. /* utilize memory */
  11316. for (i = 0; i < sz; i++) {
  11317. b[i] = (byte)i;
  11318. }
  11319. /* read back and verify */
  11320. for (i = 0; i < sz; i++) {
  11321. if (b[i] != (byte)i) {
  11322. ret = -7111;
  11323. break;
  11324. }
  11325. }
  11326. XFREE(b, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11327. return ret;
  11328. }
  11329. #endif
  11330. WOLFSSL_TEST_SUBROUTINE int memory_test(void)
  11331. {
  11332. int ret = 0;
  11333. #if defined(COMPLEX_MEM_TEST) || defined(WOLFSSL_STATIC_MEMORY)
  11334. int i;
  11335. #endif
  11336. #ifdef WOLFSSL_STATIC_MEMORY
  11337. word32 size[] = { WOLFMEM_BUCKETS };
  11338. word32 dist[] = { WOLFMEM_DIST };
  11339. byte buffer[30000]; /* make large enough to involve many bucket sizes */
  11340. int pad = -(int)((wc_ptr_t)buffer) & (WOLFSSL_STATIC_ALIGN - 1);
  11341. /* pad to account for if head of buffer is not at set memory
  11342. * alignment when tests are ran */
  11343. #endif
  11344. #ifdef WOLFSSL_STATIC_MEMORY
  11345. /* check macro settings */
  11346. if (sizeof(size)/sizeof(word32) != WOLFMEM_MAX_BUCKETS) {
  11347. return -7200;
  11348. }
  11349. if (sizeof(dist)/sizeof(word32) != WOLFMEM_MAX_BUCKETS) {
  11350. return -7201;
  11351. }
  11352. for (i = 0; i < WOLFMEM_MAX_BUCKETS; i++) {
  11353. if ((size[i] % WOLFSSL_STATIC_ALIGN) != 0) {
  11354. /* each element in array should be divisible by alignment size */
  11355. return -7202;
  11356. }
  11357. }
  11358. for (i = 1; i < WOLFMEM_MAX_BUCKETS; i++) {
  11359. if (size[i - 1] >= size[i]) {
  11360. return -7203; /* sizes should be in increasing order */
  11361. }
  11362. }
  11363. /* check that padding size returned is possible */
  11364. if (wolfSSL_MemoryPaddingSz() < WOLFSSL_STATIC_ALIGN) {
  11365. return -7204; /* no room for wc_Memory struct */
  11366. }
  11367. if (wolfSSL_MemoryPaddingSz() < 0) {
  11368. return -7205;
  11369. }
  11370. if (wolfSSL_MemoryPaddingSz() % WOLFSSL_STATIC_ALIGN != 0) {
  11371. return -7206; /* not aligned! */
  11372. }
  11373. /* check function to return optimum buffer size (rounded down) */
  11374. ret = wolfSSL_StaticBufferSz(buffer, sizeof(buffer), WOLFMEM_GENERAL);
  11375. if ((ret - pad) % WOLFSSL_STATIC_ALIGN != 0) {
  11376. return -7207; /* not aligned! */
  11377. }
  11378. if (ret < 0) {
  11379. return -7208;
  11380. }
  11381. if ((unsigned int)ret > sizeof(buffer)) {
  11382. return -7209; /* did not round down as expected */
  11383. }
  11384. if (ret != wolfSSL_StaticBufferSz(buffer, ret, WOLFMEM_GENERAL)) {
  11385. return -7210; /* return value changed when using suggested value */
  11386. }
  11387. ret = wolfSSL_MemoryPaddingSz();
  11388. ret += pad; /* add space that is going to be needed if buffer not aligned */
  11389. if (wolfSSL_StaticBufferSz(buffer, size[0] + ret + 1, WOLFMEM_GENERAL) !=
  11390. (ret + (int)size[0])) {
  11391. return -7211; /* did not round down to nearest bucket value */
  11392. }
  11393. ret = wolfSSL_StaticBufferSz(buffer, sizeof(buffer), WOLFMEM_IO_POOL);
  11394. if ((ret - pad) < 0) {
  11395. return -7212;
  11396. }
  11397. if (((ret - pad) % (WOLFMEM_IO_SZ + wolfSSL_MemoryPaddingSz())) != 0) {
  11398. return -7213; /* not even chunks of memory for IO size */
  11399. }
  11400. if (((ret - pad) % WOLFSSL_STATIC_ALIGN) != 0) {
  11401. return -7214; /* memory not aligned */
  11402. }
  11403. /* check for passing bad or unknown arguments to functions */
  11404. if (wolfSSL_StaticBufferSz(NULL, 1, WOLFMEM_GENERAL) > 0) {
  11405. return -7215;
  11406. }
  11407. if (wolfSSL_StaticBufferSz(buffer, 1, WOLFMEM_GENERAL) != 0) {
  11408. return -7216; /* should round to 0 since struct + bucket will not fit */
  11409. }
  11410. (void)dist; /* avoid static analysis warning of variable not used */
  11411. #endif
  11412. #if defined(WOLFSSL_STATIC_MEMORY) || !defined(WOLFSSL_NO_MALLOC)
  11413. /* simple test */
  11414. ret = simple_mem_test(MEM_TEST_SZ);
  11415. if (ret != 0)
  11416. return ret;
  11417. #endif
  11418. #ifdef COMPLEX_MEM_TEST
  11419. /* test various size blocks */
  11420. for (i = 1; i < MEM_TEST_SZ; i*=2) {
  11421. ret = simple_mem_test(i);
  11422. if (ret != 0)
  11423. return ret;
  11424. }
  11425. #endif
  11426. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_NO_MALLOC)
  11427. /* realloc test */
  11428. {
  11429. byte *c = NULL;
  11430. byte *b = (byte*)XMALLOC(MEM_TEST_SZ, HEAP_HINT,
  11431. DYNAMIC_TYPE_TMP_BUFFER);
  11432. if (b) {
  11433. c = (byte*)XREALLOC(b, MEM_TEST_SZ+sizeof(word32), HEAP_HINT,
  11434. DYNAMIC_TYPE_TMP_BUFFER);
  11435. if (c)
  11436. b = c;
  11437. }
  11438. if (b)
  11439. XFREE(b, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11440. if ((b == NULL) || (c == NULL)) {
  11441. return -7217;
  11442. }
  11443. }
  11444. #endif
  11445. return ret;
  11446. }
  11447. #ifndef NO_FILESYSTEM
  11448. /* Cert Paths */
  11449. #ifdef FREESCALE_MQX
  11450. #define CERT_PREFIX "a:\\"
  11451. #define CERT_PATH_SEP "\\"
  11452. #elif defined(WOLFSSL_uTKERNEL2)
  11453. #define CERT_PREFIX "/uda/"
  11454. #define CERT_PATH_SEP "/"
  11455. #elif defined(_WIN32_WCE)
  11456. #define CERT_PREFIX "\\windows\\"
  11457. #define CERT_PATH_SEP "\\"
  11458. #endif
  11459. #ifndef CERT_PREFIX
  11460. #define CERT_PREFIX "./"
  11461. #endif
  11462. #ifndef CERT_PATH_SEP
  11463. #define CERT_PATH_SEP "/"
  11464. #endif
  11465. #ifndef CERT_WRITE_TEMP_DIR
  11466. #define CERT_WRITE_TEMP_DIR CERT_PREFIX
  11467. #endif
  11468. #define CERT_ROOT CERT_PREFIX "certs" CERT_PATH_SEP
  11469. /* Generated Test Certs */
  11470. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  11471. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096)
  11472. #if !defined(NO_RSA) && !defined(NO_ASN)
  11473. static const char* clientKey = CERT_ROOT "client-key.der";
  11474. static const char* clientCert = CERT_ROOT "client-cert.der";
  11475. #ifdef WOLFSSL_CERT_EXT
  11476. static const char* clientKeyPub = CERT_ROOT "client-keyPub.der";
  11477. #endif
  11478. #endif /* !NO_RSA && !NO_ASN */
  11479. #endif
  11480. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  11481. #if !defined(NO_RSA) && !defined(NO_ASN)
  11482. #if defined(WOLFSSL_CERT_GEN) || defined(HAVE_PKCS7)
  11483. static const char* rsaCaKeyFile = CERT_ROOT "ca-key.der";
  11484. #ifdef WOLFSSL_CERT_GEN
  11485. static const char* rsaCaCertFile = CERT_ROOT "ca-cert.pem";
  11486. #endif
  11487. #if defined(WOLFSSL_ALT_NAMES) || defined(HAVE_PKCS7)
  11488. static const char* rsaCaCertDerFile = CERT_ROOT "ca-cert.der";
  11489. #endif
  11490. #ifdef HAVE_PKCS7
  11491. static const char* rsaServerCertDerFile =
  11492. CERT_ROOT "server-cert.der";
  11493. static const char* rsaServerKeyDerFile =
  11494. CERT_ROOT "server-key.der";
  11495. #endif
  11496. #endif
  11497. #endif /* !NO_RSA && !NO_ASN */
  11498. #endif /* !USE_CERT_BUFFER_* */
  11499. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  11500. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096)
  11501. #if !defined(NO_ASN) && !defined(NO_DH)
  11502. static const char* dhParamsFile = CERT_ROOT "dh2048.der";
  11503. #endif
  11504. #endif
  11505. #if !defined(NO_ASN) && !defined(NO_DH)
  11506. #if defined(WOLFSSL_DH_EXTRA) && (!defined(HAVE_FIPS) || \
  11507. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  11508. static const char* dhKeyFile = CERT_ROOT "statickeys/dh-ffdhe2048.der";
  11509. static const char* dhKeyPubFile = CERT_ROOT "statickeys/dh-ffdhe2048-pub.der";
  11510. #endif
  11511. #endif
  11512. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  11513. #ifndef NO_DSA
  11514. static const char* dsaKey = CERT_ROOT "dsa2048.der";
  11515. #endif
  11516. #endif /* !USE_CERT_BUFFER_* */
  11517. #if !defined(USE_CERT_BUFFERS_256) && !defined(NO_ECC256)
  11518. #ifdef HAVE_ECC
  11519. /* cert files to be used in rsa cert gen test, check if RSA enabled */
  11520. #ifdef HAVE_ECC_KEY_IMPORT
  11521. static const char* eccKeyDerFile = CERT_ROOT "ecc-key.der";
  11522. #endif
  11523. #endif
  11524. #if !defined(USE_CERT_BUFFERS_256) && !defined(NO_ASN)
  11525. #if defined(HAVE_ECC) && defined(WOLFSSL_CERT_GEN)
  11526. #ifndef NO_RSA
  11527. static const char* eccKeyPubFileDer = CERT_ROOT "ecc-keyPub.der";
  11528. #endif
  11529. static const char* eccCaKeyFile = CERT_ROOT "ca-ecc-key.der";
  11530. static const char* eccCaCertFile = CERT_ROOT "ca-ecc-cert.pem";
  11531. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  11532. static const char* eccCaKey384File =
  11533. CERT_ROOT "ca-ecc384-key.der";
  11534. static const char* eccCaCert384File =
  11535. CERT_ROOT "ca-ecc384-cert.pem";
  11536. #endif
  11537. #endif
  11538. #if defined(HAVE_PKCS7) && defined(HAVE_ECC)
  11539. static const char* eccClientKey = CERT_ROOT "ecc-client-key.der";
  11540. static const char* eccClientCert = CERT_ROOT "client-ecc-cert.der";
  11541. #endif
  11542. #endif /* HAVE_ECC */
  11543. #ifdef HAVE_ED25519
  11544. #ifdef WOLFSSL_TEST_CERT
  11545. static const char* serverEd25519Cert =
  11546. CERT_ROOT "ed25519/server-ed25519.der";
  11547. static const char* caEd25519Cert =
  11548. CERT_ROOT "ed25519/ca-ed25519.der";
  11549. #endif
  11550. #endif
  11551. #ifdef HAVE_ED448
  11552. #ifdef WOLFSSL_TEST_CERT
  11553. static const char* serverEd448Cert =
  11554. CERT_ROOT "ed448/server-ed448.der";
  11555. static const char* caEd448Cert = CERT_ROOT "ed448/ca-ed448.der";
  11556. #endif
  11557. #endif
  11558. #endif /* !USE_CERT_BUFFER_* */
  11559. #if !defined(NO_ASN_TIME) && !defined(NO_RSA) && defined(WOLFSSL_TEST_CERT) && \
  11560. !defined(NO_FILESYSTEM)
  11561. static const char* certExtNc =
  11562. CERT_ROOT "test" CERT_PATH_SEP "cert-ext-nc.der";
  11563. static const char* certExtIa =
  11564. CERT_ROOT "test" CERT_PATH_SEP "cert-ext-ia.der";
  11565. static const char* certExtNct =
  11566. CERT_ROOT "test" CERT_PATH_SEP "cert-ext-nct.der";
  11567. #endif
  11568. #ifndef NO_WRITE_TEMP_FILES
  11569. #ifdef HAVE_ECC
  11570. #ifdef WOLFSSL_CERT_GEN
  11571. static const char* certEccPemFile = CERT_WRITE_TEMP_DIR "certecc.pem";
  11572. static const char* certEccDerFile = CERT_WRITE_TEMP_DIR "certecc.der";
  11573. #endif
  11574. #if defined(WOLFSSL_CERT_GEN) && !defined(NO_RSA)
  11575. static const char* certEccRsaPemFile = CERT_WRITE_TEMP_DIR "certeccrsa.pem";
  11576. static const char* certEccRsaDerFile = CERT_WRITE_TEMP_DIR "certeccrsa.der";
  11577. #endif
  11578. #if defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG) && \
  11579. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  11580. static const char* eccCaKeyPemFile = CERT_WRITE_TEMP_DIR "ecc-key.pem";
  11581. static const char* eccPubKeyDerFile = CERT_WRITE_TEMP_DIR "ecc-public-key.der";
  11582. static const char* eccCaKeyTempFile = CERT_WRITE_TEMP_DIR "ecc-key.der";
  11583. #if defined(HAVE_PKCS8) && !defined(WC_NO_RNG) && \
  11584. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  11585. static const char* eccPkcs8KeyDerFile = CERT_WRITE_TEMP_DIR "ecc-key-pkcs8.der";
  11586. #endif
  11587. #endif /* HAVE_ECC_KEY_EXPORT */
  11588. #endif /* HAVE_ECC */
  11589. #ifndef NO_RSA
  11590. #ifdef WOLFSSL_CERT_GEN
  11591. static const char* otherCertDerFile = CERT_WRITE_TEMP_DIR "othercert.der";
  11592. static const char* certDerFile = CERT_WRITE_TEMP_DIR "cert.der";
  11593. static const char* otherCertPemFile = CERT_WRITE_TEMP_DIR "othercert.pem";
  11594. static const char* certPemFile = CERT_WRITE_TEMP_DIR "cert.pem";
  11595. #if defined(WOLFSSL_CERT_REQ) && !defined(WOLFSSL_NO_MALLOC)
  11596. static const char* certReqDerFile = CERT_WRITE_TEMP_DIR "certreq.der";
  11597. static const char* certReqPemFile = CERT_WRITE_TEMP_DIR "certreq.pem";
  11598. #endif
  11599. #endif
  11600. #endif /* !NO_RSA */
  11601. #if !defined(NO_RSA) || !defined(NO_DSA)
  11602. #ifdef WOLFSSL_KEY_GEN
  11603. static const char* keyDerFile = CERT_WRITE_TEMP_DIR "key.der";
  11604. static const char* keyPemFile = CERT_WRITE_TEMP_DIR "key.pem";
  11605. #endif
  11606. #endif
  11607. #endif /* !NO_WRITE_TEMP_FILES */
  11608. #endif /* !NO_FILESYSTEM */
  11609. #if defined(WOLFSSL_CERT_GEN) && (!defined(NO_RSA) || defined(HAVE_ECC)) || \
  11610. (defined(WOLFSSL_TEST_CERT) && (defined(HAVE_ED25519) || defined(HAVE_ED448)))
  11611. static CertName certDefaultName;
  11612. static void initDefaultName(void)
  11613. {
  11614. #if defined(WOLFSSL_MULTI_ATTRIB) && defined(WOLFSSL_TEST_CERT)
  11615. NameAttrib* n;
  11616. #endif
  11617. XMEMCPY(certDefaultName.country, "US", sizeof("US"));
  11618. certDefaultName.countryEnc = CTC_PRINTABLE;
  11619. XMEMCPY(certDefaultName.state, "Oregon", sizeof("Oregon"));
  11620. certDefaultName.stateEnc = CTC_UTF8;
  11621. XMEMCPY(certDefaultName.street, "Main St", sizeof("Main St"));
  11622. certDefaultName.streetEnc = CTC_UTF8;
  11623. XMEMCPY(certDefaultName.locality, "Portland", sizeof("Portland"));
  11624. certDefaultName.localityEnc = CTC_UTF8;
  11625. XMEMCPY(certDefaultName.sur, "Test", sizeof("Test"));
  11626. certDefaultName.surEnc = CTC_UTF8;
  11627. XMEMCPY(certDefaultName.org, "wolfSSL", sizeof("wolfSSL"));
  11628. certDefaultName.orgEnc = CTC_UTF8;
  11629. XMEMCPY(certDefaultName.unit, "Development", sizeof("Development"));
  11630. certDefaultName.unitEnc = CTC_UTF8;
  11631. XMEMCPY(certDefaultName.commonName, "www.wolfssl.com", sizeof("www.wolfssl.com"));
  11632. certDefaultName.commonNameEnc = CTC_UTF8;
  11633. XMEMCPY(certDefaultName.serialDev, "wolfSSL12345", sizeof("wolfSSL12345"));
  11634. certDefaultName.serialDevEnc = CTC_PRINTABLE;
  11635. XMEMCPY(certDefaultName.postalCode, "12-456", sizeof("12-456"));
  11636. certDefaultName.postalCodeEnc = CTC_PRINTABLE;
  11637. #ifdef WOLFSSL_CERT_EXT
  11638. XMEMCPY(certDefaultName.busCat, "Private Organization", sizeof("Private Organization"));
  11639. certDefaultName.busCatEnc = CTC_UTF8;
  11640. XMEMCPY(certDefaultName.joiSt, "US", sizeof("US"));
  11641. certDefaultName.joiStEnc = CTC_PRINTABLE;
  11642. XMEMCPY(certDefaultName.joiC, "Oregon", sizeof("Oregon"));
  11643. certDefaultName.joiCEnc = CTC_PRINTABLE;
  11644. #endif
  11645. XMEMCPY(certDefaultName.email, "info@wolfssl.com", sizeof("info@wolfssl.com"));
  11646. XMEMCPY(certDefaultName.userId, "TestUserID", sizeof("TestUserID"));
  11647. certDefaultName.userIdEnc = CTC_PRINTABLE;
  11648. #if defined(WOLFSSL_MULTI_ATTRIB) && defined(WOLFSSL_TEST_CERT)
  11649. /* test having additional OUs and setting DC */
  11650. n = &certDefaultName.name[0];
  11651. n->id = ASN_ORGUNIT_NAME;
  11652. n->type = CTC_UTF8;
  11653. n->sz = sizeof("Development-2");
  11654. XMEMCPY(n->value, "Development-2", sizeof("Development-2"));
  11655. #if CTC_MAX_ATTRIB > 3
  11656. n = &certDefaultName.name[1];
  11657. n->id = ASN_DOMAIN_COMPONENT;
  11658. n->type = CTC_UTF8;
  11659. n->sz = sizeof("com");
  11660. XMEMCPY(n->value, "com", sizeof("com"));
  11661. n = &certDefaultName.name[2];
  11662. n->id = ASN_DOMAIN_COMPONENT;
  11663. n->type = CTC_UTF8;
  11664. n->sz = sizeof("wolfssl");
  11665. XMEMCPY(n->value, "wolfssl", sizeof("wolfssl"));
  11666. #endif
  11667. #endif /* WOLFSSL_MULTI_ATTRIB && WOLFSSL_TEST_CERT */
  11668. #ifdef WOLFSSL_CUSTOM_OID
  11669. /* TODO: Add test case for custom OID's */
  11670. #endif
  11671. }
  11672. #ifdef WOLFSSL_CERT_EXT
  11673. #if ((defined(HAVE_ED25519) || defined(HAVE_ED448)) && \
  11674. defined(WOLFSSL_TEST_CERT)) || defined(HAVE_ECC)
  11675. WOLFSSL_SMALL_STACK_STATIC const char certKeyUsage[] =
  11676. "digitalSignature,nonRepudiation";
  11677. #endif
  11678. #if defined(WOLFSSL_CERT_REQ) && !defined(NO_RSA)
  11679. WOLFSSL_SMALL_STACK_STATIC const char certKeyUsage2[] =
  11680. "digitalSignature,nonRepudiation,keyEncipherment,keyAgreement";
  11681. #endif
  11682. #endif /* WOLFSSL_CERT_EXT */
  11683. #endif /* WOLFSSL_CERT_GEN */
  11684. #ifndef NO_RSA
  11685. #if !defined(NO_ASN_TIME) && !defined(NO_RSA) && defined(WOLFSSL_TEST_CERT) && \
  11686. !defined(NO_FILESYSTEM)
  11687. static byte minSerial[] = { 0x02, 0x01, 0x01 };
  11688. static byte minName[] = { 0x30, 0x00 };
  11689. static byte nameBad[] = {
  11690. 0x30, 0x08,
  11691. 0x31, 0x06,
  11692. 0x30, 0x04,
  11693. 0x06, 0x02,
  11694. 0x55, 0x04,
  11695. };
  11696. static byte minDates[] = {
  11697. 0x30, 0x1e,
  11698. 0x17, 0x0d,
  11699. 0x31, 0x38, 0x30, 0x34, 0x31, 0x33, 0x31, 0x35,
  11700. 0x32, 0x33, 0x31, 0x30, 0x5a,
  11701. 0x17, 0x0d,
  11702. 0x32, 0x31, 0x30, 0x31, 0x30, 0x37, 0x31, 0x35,
  11703. 0x32, 0x33, 0x31, 0x30, 0x5a
  11704. };
  11705. static byte minPubKey[] = {
  11706. 0x30, 0x1c,
  11707. 0x30, 0x0d,
  11708. 0x06, 0x09,
  11709. 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01,
  11710. 0x01,
  11711. 0x05, 0x00,
  11712. 0x03, 0x0b,
  11713. 0x00, 0x30, 0x08,
  11714. 0x02, 0x01,
  11715. 0x03,
  11716. 0x02, 0x03,
  11717. 0x01, 0x00, 0x01
  11718. };
  11719. static byte minSigAlg[] = {
  11720. 0x30, 0x0d,
  11721. 0x06, 0x09,
  11722. 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01,
  11723. 0x0b,
  11724. 0x05, 0x00
  11725. };
  11726. static byte minSig[] = {
  11727. 0x03, 0x01,
  11728. 0x00
  11729. };
  11730. static int add_seq(byte* certData, int offset, byte* data, byte length)
  11731. {
  11732. XMEMMOVE(certData + offset + 2, data, length);
  11733. certData[offset++] = 0x30;
  11734. certData[offset++] = length;
  11735. return offset + length;
  11736. }
  11737. static int add_data(byte* certData, int offset, byte* data, byte length)
  11738. {
  11739. XMEMCPY(certData + offset, data, length);
  11740. return offset + length;
  11741. }
  11742. static int cert_asn1_test(void)
  11743. {
  11744. int ret;
  11745. int len[3];
  11746. DecodedCert cert;
  11747. byte certData[114];
  11748. byte* badCert = NULL;
  11749. len[2] = add_data(certData, 0, minSerial, (byte)sizeof(minSerial));
  11750. len[2] = add_data(certData, len[2], minSigAlg, (byte)sizeof(minSigAlg));
  11751. len[2] = add_data(certData, len[2], minName, (byte)sizeof(minName));
  11752. len[2] = add_data(certData, len[2], minDates, (byte)sizeof(minDates));
  11753. len[2] = add_data(certData, len[2], minName, (byte)sizeof(minName));
  11754. len[2] = add_data(certData, len[2], minPubKey, (byte)sizeof(minPubKey));
  11755. len[1] = add_seq(certData, 0, certData, len[2]);
  11756. len[1] = add_data(certData, len[1], minSigAlg, (byte)sizeof(minSigAlg));
  11757. len[1] = add_data(certData, len[1], minSig, (byte)sizeof(minSig));
  11758. len[0] = add_seq(certData, 0, certData, len[1]);
  11759. /* Minimal good certificate */
  11760. InitDecodedCert(&cert, certData, len[0], 0);
  11761. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL);
  11762. FreeDecodedCert(&cert);
  11763. if (ret != 0) {
  11764. ERROR_OUT(-7300, done);
  11765. }
  11766. /* Bad issuer name */
  11767. len[2] = add_data(certData, 0, minSerial, (byte)sizeof(minSerial));
  11768. len[2] = add_data(certData, len[2], minSigAlg, (byte)sizeof(minSigAlg));
  11769. len[2] = add_data(certData, len[2], nameBad, (byte)sizeof(nameBad));
  11770. len[2] = add_data(certData, len[2], minDates, (byte)sizeof(minDates));
  11771. len[2] = add_data(certData, len[2], minName, (byte)sizeof(minName));
  11772. len[2] = add_data(certData, len[2], minPubKey, (byte)sizeof(minPubKey));
  11773. len[1] = add_seq(certData, 0, certData, len[2]);
  11774. len[1] = add_data(certData, len[1], minSigAlg, (byte)sizeof(minSigAlg));
  11775. len[1] = add_data(certData, len[1], minSig, (byte)sizeof(minSig));
  11776. len[0] = add_seq(certData, 0, certData, len[1]);
  11777. /* Put data into allocated buffer to allow access error checking. */
  11778. badCert = (byte*)XMALLOC(len[0], HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11779. XMEMCPY(badCert, certData, len[0]);
  11780. InitDecodedCert(&cert, badCert, len[0], 0);
  11781. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL);
  11782. FreeDecodedCert(&cert);
  11783. if (ret != ASN_PARSE_E) {
  11784. ERROR_OUT(-7301, done);
  11785. }
  11786. XFREE(badCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11787. badCert = NULL;
  11788. ret = 0;
  11789. done:
  11790. if (badCert != NULL)
  11791. XFREE(badCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11792. return ret;
  11793. }
  11794. WOLFSSL_TEST_SUBROUTINE int cert_test(void)
  11795. {
  11796. #if !defined(NO_FILESYSTEM)
  11797. DecodedCert cert;
  11798. byte* tmp;
  11799. size_t bytes;
  11800. XFILE file;
  11801. int ret;
  11802. tmp = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11803. if (tmp == NULL)
  11804. return -7400;
  11805. /* Certificate with Name Constraints extension. */
  11806. file = XFOPEN(certExtNc, "rb");
  11807. if (!file) {
  11808. ERROR_OUT(-7401, done);
  11809. }
  11810. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  11811. XFCLOSE(file);
  11812. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  11813. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL);
  11814. if (ret != 0) {
  11815. ERROR_OUT(-7402, done);
  11816. }
  11817. FreeDecodedCert(&cert);
  11818. /* Certificate with Inhibit Any Policy extension. */
  11819. file = XFOPEN(certExtIa, "rb");
  11820. if (!file) {
  11821. ERROR_OUT(-7403, done);
  11822. }
  11823. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  11824. XFCLOSE(file);
  11825. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  11826. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL);
  11827. if (ret != 0) {
  11828. ERROR_OUT(-7404, done);
  11829. }
  11830. FreeDecodedCert(&cert);
  11831. /* Certificate with Netscape Certificate Type extension. */
  11832. file = XFOPEN(certExtNct, "rb");
  11833. if (!file) {
  11834. ERROR_OUT(-7405, done);
  11835. }
  11836. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  11837. XFCLOSE(file);
  11838. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  11839. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL);
  11840. #ifndef IGNORE_NETSCAPE_CERT_TYPE
  11841. if (ret != 0) {
  11842. ERROR_OUT(-7406, done);
  11843. }
  11844. #else
  11845. if (ret != ASN_CRIT_EXT_E) {
  11846. ERROR_OUT(-7407, done);
  11847. }
  11848. ret = 0;
  11849. #endif
  11850. done:
  11851. FreeDecodedCert(&cert);
  11852. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11853. #endif /* !NO_FILESYSTEM */
  11854. if (ret == 0)
  11855. ret = cert_asn1_test();
  11856. return ret;
  11857. }
  11858. #endif /* WOLFSSL_TEST_CERT */
  11859. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_TEST_CERT) && \
  11860. !defined(NO_FILESYSTEM) && defined(WOLFSSL_CERT_GEN)
  11861. WOLFSSL_TEST_SUBROUTINE int certext_test(void)
  11862. {
  11863. DecodedCert cert;
  11864. byte* tmp;
  11865. size_t bytes;
  11866. XFILE file;
  11867. int ret;
  11868. /* created from rsa_test : othercert.der */
  11869. byte skid_rsa[] = "\x33\xD8\x45\x66\xD7\x68\x87\x18\x7E\x54"
  11870. "\x0D\x70\x27\x91\xC7\x26\xD7\x85\x65\xC0";
  11871. /* created from rsa_test : othercert.der */
  11872. byte akid_rsa[] = "\x27\x8E\x67\x11\x74\xC3\x26\x1D\x3F\xED"
  11873. "\x33\x63\xB3\xA4\xD8\x1D\x30\xE5\xE8\xD5";
  11874. #ifdef HAVE_ECC
  11875. /* created from ecc_test_cert_gen : certecc.der */
  11876. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  11877. /* Authority key id from ./certs/ca-ecc384-cert.pem */
  11878. byte akid_ecc[] = "\xAB\xE0\xC3\x26\x4C\x18\xD4\x72\xBB\xD2"
  11879. "\x84\x8C\x9C\x0A\x05\x92\x80\x12\x53\x52";
  11880. #else
  11881. /* Authority key id from ./certs/ca-ecc-cert.pem */
  11882. byte akid_ecc[] = "\x56\x8E\x9A\xC3\xF0\x42\xDE\x18\xB9\x45"
  11883. "\x55\x6E\xF9\x93\xCF\xEA\xC3\xF3\xA5\x21";
  11884. #endif
  11885. #endif /* HAVE_ECC */
  11886. /* created from rsa_test : cert.der */
  11887. byte kid_ca[] = "\x33\xD8\x45\x66\xD7\x68\x87\x18\x7E\x54"
  11888. "\x0D\x70\x27\x91\xC7\x26\xD7\x85\x65\xC0";
  11889. tmp = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11890. if (tmp == NULL)
  11891. return -7500;
  11892. /* load othercert.der (Cert signed by an authority) */
  11893. file = XFOPEN(otherCertDerFile, "rb");
  11894. if (!file) {
  11895. XFREE(tmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  11896. return -7501;
  11897. }
  11898. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  11899. XFCLOSE(file);
  11900. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  11901. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, 0);
  11902. if (ret != 0)
  11903. return -7502;
  11904. /* check the SKID from a RSA certificate */
  11905. if (XMEMCMP(skid_rsa, cert.extSubjKeyId, sizeof(cert.extSubjKeyId)))
  11906. return -7503;
  11907. /* check the AKID from an RSA certificate */
  11908. if (XMEMCMP(akid_rsa, cert.extAuthKeyId, sizeof(cert.extAuthKeyId)))
  11909. return -7504;
  11910. /* check the Key Usage from an RSA certificate */
  11911. if (!cert.extKeyUsageSet)
  11912. return -7505;
  11913. if (cert.extKeyUsage != (KEYUSE_KEY_ENCIPHER|KEYUSE_KEY_AGREE))
  11914. return -7506;
  11915. /* check the CA Basic Constraints from an RSA certificate */
  11916. if (cert.isCA)
  11917. return -7507;
  11918. #ifndef WOLFSSL_SEP /* test only if not using SEP policies */
  11919. /* check the Certificate Policies Id */
  11920. if (cert.extCertPoliciesNb != 1)
  11921. return -7508;
  11922. if (strncmp(cert.extCertPolicies[0], "2.16.840.1.101.3.4.1.42", 23))
  11923. return -7509;
  11924. #endif
  11925. FreeDecodedCert(&cert);
  11926. #ifdef HAVE_ECC
  11927. /* load certecc.der (Cert signed by our ECC CA test in ecc_test_cert_gen) */
  11928. file = XFOPEN(certEccDerFile, "rb");
  11929. if (!file) {
  11930. XFREE(tmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  11931. return -7510;
  11932. }
  11933. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  11934. XFCLOSE(file);
  11935. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  11936. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, 0);
  11937. if (ret != 0)
  11938. return -7511;
  11939. /* check the SKID from a ECC certificate - generated dynamically */
  11940. /* check the AKID from an ECC certificate */
  11941. if (XMEMCMP(akid_ecc, cert.extAuthKeyId, sizeof(cert.extAuthKeyId)))
  11942. return -7512;
  11943. /* check the Key Usage from an ECC certificate */
  11944. if (!cert.extKeyUsageSet)
  11945. return -7513;
  11946. if (cert.extKeyUsage != (KEYUSE_DIGITAL_SIG|KEYUSE_CONTENT_COMMIT))
  11947. return -7514;
  11948. /* check the CA Basic Constraints from an ECC certificate */
  11949. if (cert.isCA)
  11950. return -7515;
  11951. #ifndef WOLFSSL_SEP /* test only if not using SEP policies */
  11952. /* check the Certificate Policies Id */
  11953. if (cert.extCertPoliciesNb != 2)
  11954. return -7516;
  11955. if (strncmp(cert.extCertPolicies[0], "2.4.589440.587.101.2.1.9632587.1", 32))
  11956. return -7517;
  11957. if (strncmp(cert.extCertPolicies[1], "1.2.13025.489.1.113549", 22))
  11958. return -7518;
  11959. #endif
  11960. FreeDecodedCert(&cert);
  11961. #endif /* HAVE_ECC */
  11962. /* load cert.der (self signed certificate) */
  11963. file = XFOPEN(certDerFile, "rb");
  11964. if (!file) {
  11965. XFREE(tmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  11966. return -7519;
  11967. }
  11968. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  11969. XFCLOSE(file);
  11970. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  11971. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, 0);
  11972. if (ret != 0)
  11973. return -7520;
  11974. /* check the SKID from a CA certificate */
  11975. if (XMEMCMP(kid_ca, cert.extSubjKeyId, sizeof(cert.extSubjKeyId)))
  11976. return -7521;
  11977. /* check the AKID from an CA certificate */
  11978. if (XMEMCMP(kid_ca, cert.extAuthKeyId, sizeof(cert.extAuthKeyId)))
  11979. return -7522;
  11980. /* check the Key Usage from CA certificate */
  11981. if (!cert.extKeyUsageSet)
  11982. return -7523;
  11983. if (cert.extKeyUsage != (KEYUSE_KEY_CERT_SIGN|KEYUSE_CRL_SIGN))
  11984. return -7524;
  11985. /* check the CA Basic Constraints CA certificate */
  11986. if (!cert.isCA)
  11987. return -7525;
  11988. #ifndef WOLFSSL_SEP /* test only if not using SEP policies */
  11989. /* check the Certificate Policies Id */
  11990. if (cert.extCertPoliciesNb != 2)
  11991. return -7526;
  11992. if (strncmp(cert.extCertPolicies[0], "2.16.840.1.101.3.4.1.42", 23))
  11993. return -7527;
  11994. if (strncmp(cert.extCertPolicies[1], "1.2.840.113549.1.9.16.6.5", 25))
  11995. return -7528;
  11996. #endif
  11997. FreeDecodedCert(&cert);
  11998. XFREE(tmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  11999. return 0;
  12000. }
  12001. #endif /* WOLFSSL_CERT_EXT && WOLFSSL_TEST_CERT &&
  12002. !NO_FILESYSTEM && WOLFSSL_CERT_GEN */
  12003. #if defined(WOLFSSL_CERT_GEN_CACHE) && defined(WOLFSSL_TEST_CERT) && \
  12004. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  12005. WOLFSSL_TEST_SUBROUTINE int decodedCertCache_test(void)
  12006. {
  12007. int ret = 0;
  12008. Cert cert;
  12009. FILE* file;
  12010. byte* der;
  12011. word32 derSz;
  12012. derSz = FOURK_BUF;
  12013. der = (byte *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12014. if (der == NULL)
  12015. ret = -7600;
  12016. if (ret == 0) {
  12017. /* load cert.der */
  12018. file = XFOPEN(certDerFile, "rb");
  12019. if (file != NULL) {
  12020. derSz = (word32)XFREAD(der, 1, FOURK_BUF, file);
  12021. XFCLOSE(file);
  12022. }
  12023. else
  12024. ret = -7601;
  12025. }
  12026. if (ret == 0) {
  12027. if (wc_InitCert_ex(&cert, HEAP_HINT, devId)) {
  12028. ret = -7602;
  12029. }
  12030. }
  12031. if (ret == 0) {
  12032. ret = wc_SetSubjectBuffer(&cert, der, derSz);
  12033. }
  12034. if (ret == 0) {
  12035. if(wc_SetSubjectBuffer(NULL, der, derSz) != BAD_FUNC_ARG)
  12036. ret = -7603;
  12037. }
  12038. if (ret == 0) {
  12039. if (wc_SetSubjectRaw(&cert, der, derSz) != 0)
  12040. ret = -7604;
  12041. }
  12042. if (ret == 0) {
  12043. if(wc_SetSubjectRaw(NULL, der, derSz) != BAD_FUNC_ARG)
  12044. ret = -7605;
  12045. }
  12046. if (ret == 0) {
  12047. if(wc_SetIssuerBuffer(&cert, der, derSz) != 0)
  12048. ret = -7606;
  12049. }
  12050. if (ret == 0) {
  12051. if(wc_SetIssuerBuffer(NULL, der, derSz) != BAD_FUNC_ARG)
  12052. ret = -7607;
  12053. }
  12054. if (ret == 0) {
  12055. if(wc_SetIssuerRaw(&cert, der, derSz) != 0)
  12056. ret = -7608;
  12057. }
  12058. if (ret == 0) {
  12059. if(wc_SetIssuerRaw(NULL, der, derSz) != BAD_FUNC_ARG)
  12060. ret = -7609;
  12061. }
  12062. #ifdef WOLFSSL_ALT_NAMES
  12063. if (ret == 0) {
  12064. if(wc_SetAltNamesBuffer(&cert, der, derSz) != 0)
  12065. ret = -7610;
  12066. }
  12067. if (ret == 0) {
  12068. if(wc_SetAltNamesBuffer(NULL, der, derSz) != BAD_FUNC_ARG)
  12069. ret = -7611;
  12070. }
  12071. if (ret == 0) {
  12072. if(wc_SetDatesBuffer(&cert, der, derSz) != 0)
  12073. ret = -7612;
  12074. }
  12075. if (ret == 0) {
  12076. if(wc_SetDatesBuffer(NULL, der, derSz) != BAD_FUNC_ARG)
  12077. ret = -7613;
  12078. }
  12079. #endif
  12080. if (ret == 0) {
  12081. if(wc_SetAuthKeyIdFromCert(&cert, der, derSz) != 0)
  12082. ret = -7614;
  12083. }
  12084. if (ret == 0) {
  12085. if(wc_SetAuthKeyIdFromCert(NULL, der, derSz) != BAD_FUNC_ARG)
  12086. ret = -7615;
  12087. }
  12088. wc_SetCert_Free(&cert);
  12089. if (ret == 0) {
  12090. if(cert.decodedCert != NULL)
  12091. ret = -7616;
  12092. }
  12093. XFREE(der, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  12094. return ret;
  12095. }
  12096. #endif /* defined(WOLFSSL_CERT_GEN_CACHE) && defined(WOLFSSL_TEST_CERT) &&
  12097. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN) */
  12098. #define RSA_TEST_BYTES 512 /* up to 4096-bit key */
  12099. #if !defined(NO_ASN) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
  12100. !defined(WOLFSSL_RSA_VERIFY_ONLY)
  12101. static int rsa_flatten_test(RsaKey* key)
  12102. {
  12103. int ret;
  12104. byte e[RSA_TEST_BYTES];
  12105. byte n[RSA_TEST_BYTES];
  12106. word32 eSz = sizeof(e);
  12107. word32 nSz = sizeof(n);
  12108. /* Parameter Validation testing. */
  12109. ret = wc_RsaFlattenPublicKey(NULL, e, &eSz, n, &nSz);
  12110. #ifdef HAVE_USER_RSA
  12111. /* Implementation using IPP Libraries returns:
  12112. * -101 = USER_CRYPTO_ERROR
  12113. */
  12114. if (ret == 0)
  12115. #else
  12116. if (ret != BAD_FUNC_ARG)
  12117. #endif
  12118. return -7620;
  12119. ret = wc_RsaFlattenPublicKey(key, NULL, &eSz, n, &nSz);
  12120. #ifdef HAVE_USER_RSA
  12121. /* Implementation using IPP Libraries returns:
  12122. * -101 = USER_CRYPTO_ERROR
  12123. */
  12124. if (ret == 0)
  12125. #else
  12126. if (ret != BAD_FUNC_ARG)
  12127. #endif
  12128. return -7621;
  12129. ret = wc_RsaFlattenPublicKey(key, e, NULL, n, &nSz);
  12130. #ifdef HAVE_USER_RSA
  12131. /* Implementation using IPP Libraries returns:
  12132. * -101 = USER_CRYPTO_ERROR
  12133. */
  12134. if (ret == 0)
  12135. #else
  12136. if (ret != BAD_FUNC_ARG)
  12137. #endif
  12138. return -7622;
  12139. ret = wc_RsaFlattenPublicKey(key, e, &eSz, NULL, &nSz);
  12140. #ifdef HAVE_USER_RSA
  12141. /* Implementation using IPP Libraries returns:
  12142. * -101 = USER_CRYPTO_ERROR
  12143. */
  12144. if (ret == 0)
  12145. #else
  12146. if (ret != BAD_FUNC_ARG)
  12147. #endif
  12148. return -7623;
  12149. ret = wc_RsaFlattenPublicKey(key, e, &eSz, n, NULL);
  12150. #ifdef HAVE_USER_RSA
  12151. /* Implementation using IPP Libraries returns:
  12152. * -101 = USER_CRYPTO_ERROR
  12153. */
  12154. if (ret == 0)
  12155. #else
  12156. if (ret != BAD_FUNC_ARG)
  12157. #endif
  12158. return -7624;
  12159. ret = wc_RsaFlattenPublicKey(key, e, &eSz, n, &nSz);
  12160. if (ret != 0)
  12161. return -7625;
  12162. eSz = 0;
  12163. ret = wc_RsaFlattenPublicKey(key, e, &eSz, n, &nSz);
  12164. #ifdef HAVE_USER_RSA
  12165. /* Implementation using IPP Libraries returns:
  12166. * -101 = USER_CRYPTO_ERROR
  12167. */
  12168. if (ret == 0)
  12169. #elif defined(HAVE_FIPS) && \
  12170. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 2))
  12171. if (ret != 0)
  12172. #else
  12173. if (ret != RSA_BUFFER_E)
  12174. #endif
  12175. return -7626;
  12176. eSz = sizeof(e);
  12177. nSz = 0;
  12178. ret = wc_RsaFlattenPublicKey(key, e, &eSz, n, &nSz);
  12179. #ifdef HAVE_USER_RSA
  12180. /* Implementation using IPP Libraries returns:
  12181. * -101 = USER_CRYPTO_ERROR
  12182. */
  12183. if (ret == 0)
  12184. #else
  12185. if (ret != RSA_BUFFER_E)
  12186. #endif
  12187. return -7627;
  12188. return 0;
  12189. }
  12190. #endif /* NO_ASN */
  12191. #if !defined(HAVE_FIPS) && !defined(HAVE_USER_RSA) && !defined(NO_ASN) \
  12192. && !defined(WOLFSSL_RSA_VERIFY_ONLY)
  12193. static int rsa_export_key_test(RsaKey* key)
  12194. {
  12195. int ret;
  12196. byte e[3];
  12197. word32 eSz = sizeof(e);
  12198. byte n[RSA_TEST_BYTES];
  12199. word32 nSz = sizeof(n);
  12200. byte d[RSA_TEST_BYTES];
  12201. word32 dSz = sizeof(d);
  12202. byte p[RSA_TEST_BYTES/2];
  12203. word32 pSz = sizeof(p);
  12204. byte q[RSA_TEST_BYTES/2];
  12205. word32 qSz = sizeof(q);
  12206. word32 zero = 0;
  12207. ret = wc_RsaExportKey(NULL, e, &eSz, n, &nSz, d, &dSz, p, &pSz, q, &qSz);
  12208. if (ret != BAD_FUNC_ARG)
  12209. return -7630;
  12210. ret = wc_RsaExportKey(key, NULL, &eSz, n, &nSz, d, &dSz, p, &pSz, q, &qSz);
  12211. if (ret != BAD_FUNC_ARG)
  12212. return -7631;
  12213. ret = wc_RsaExportKey(key, e, NULL, n, &nSz, d, &dSz, p, &pSz, q, &qSz);
  12214. if (ret != BAD_FUNC_ARG)
  12215. return -7632;
  12216. ret = wc_RsaExportKey(key, e, &eSz, NULL, &nSz, d, &dSz, p, &pSz, q, &qSz);
  12217. if (ret != BAD_FUNC_ARG)
  12218. return -7633;
  12219. ret = wc_RsaExportKey(key, e, &eSz, n, NULL, d, &dSz, p, &pSz, q, &qSz);
  12220. if (ret != BAD_FUNC_ARG)
  12221. return -7634;
  12222. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, NULL, &dSz, p, &pSz, q, &qSz);
  12223. if (ret != BAD_FUNC_ARG)
  12224. return -7635;
  12225. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, NULL, p, &pSz, q, &qSz);
  12226. if (ret != BAD_FUNC_ARG)
  12227. return -7636;
  12228. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, NULL, &pSz, q, &qSz);
  12229. if (ret != BAD_FUNC_ARG)
  12230. return -7637;
  12231. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, NULL, q, &qSz);
  12232. if (ret != BAD_FUNC_ARG)
  12233. return -7638;
  12234. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, &pSz, NULL, &qSz);
  12235. if (ret != BAD_FUNC_ARG)
  12236. return -7639;
  12237. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, &pSz, q, NULL);
  12238. if (ret != BAD_FUNC_ARG)
  12239. return -7640;
  12240. ret = wc_RsaExportKey(key, e, &zero, n, &nSz, d, &dSz, p, &pSz, q, &qSz);
  12241. if (ret != RSA_BUFFER_E)
  12242. return -7641;
  12243. ret = wc_RsaExportKey(key, e, &eSz, n, &zero, d, &dSz, p, &pSz, q, &qSz);
  12244. if (ret != RSA_BUFFER_E)
  12245. return -7642;
  12246. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  12247. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &zero, p, &pSz, q, &qSz);
  12248. if (ret != RSA_BUFFER_E)
  12249. return -7643;
  12250. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, &zero, q, &qSz);
  12251. if (ret != RSA_BUFFER_E)
  12252. return -7644;
  12253. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, &pSz, q, &zero);
  12254. if (ret != RSA_BUFFER_E)
  12255. return -7645;
  12256. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  12257. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, &pSz, q, &qSz);
  12258. if (ret != 0)
  12259. return -7646;
  12260. return 0;
  12261. }
  12262. #endif /* !HAVE_FIPS && !USER_RSA && !NO_ASN */
  12263. #ifndef NO_SIG_WRAPPER
  12264. static int rsa_sig_test(RsaKey* key, word32 keyLen, int modLen, WC_RNG* rng)
  12265. {
  12266. int ret;
  12267. word32 sigSz;
  12268. WOLFSSL_SMALL_STACK_STATIC const byte in[] = TEST_STRING;
  12269. WOLFSSL_SMALL_STACK_STATIC const byte hash[] = {
  12270. 0xf2, 0x02, 0x95, 0x65, 0xcb, 0xf6, 0x2a, 0x59,
  12271. 0x39, 0x2c, 0x05, 0xff, 0x0e, 0x29, 0xaf, 0xfe,
  12272. 0x47, 0x33, 0x8c, 0x99, 0x8d, 0x58, 0x64, 0x83,
  12273. 0xa6, 0x58, 0x0a, 0x33, 0x0b, 0x84, 0x5f, 0x5f
  12274. };
  12275. WOLFSSL_SMALL_STACK_STATIC const byte hashEnc[] = {
  12276. 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
  12277. 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05,
  12278. 0x00, 0x04, 0x20,
  12279. 0xf2, 0x02, 0x95, 0x65, 0xcb, 0xf6, 0x2a, 0x59,
  12280. 0x39, 0x2c, 0x05, 0xff, 0x0e, 0x29, 0xaf, 0xfe,
  12281. 0x47, 0x33, 0x8c, 0x99, 0x8d, 0x58, 0x64, 0x83,
  12282. 0xa6, 0x58, 0x0a, 0x33, 0x0b, 0x84, 0x5f, 0x5f
  12283. };
  12284. word32 inLen = (word32)XSTRLEN((char*)in);
  12285. byte out[RSA_TEST_BYTES];
  12286. /* Parameter Validation testing. */
  12287. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_NONE, key, keyLen);
  12288. if (ret != BAD_FUNC_ARG)
  12289. return -7650;
  12290. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_RSA, key, 0);
  12291. if (ret != BAD_FUNC_ARG)
  12292. return -7651;
  12293. sigSz = (word32)modLen;
  12294. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, NULL,
  12295. inLen, out, &sigSz, key, keyLen, rng);
  12296. if (ret != BAD_FUNC_ARG)
  12297. return -7652;
  12298. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  12299. 0, out, &sigSz, key, keyLen, rng);
  12300. if (ret != BAD_FUNC_ARG)
  12301. return -7653;
  12302. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  12303. inLen, NULL, &sigSz, key, keyLen, rng);
  12304. if (ret != BAD_FUNC_ARG)
  12305. return -7654;
  12306. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  12307. inLen, out, NULL, key, keyLen, rng);
  12308. if (ret != BAD_FUNC_ARG)
  12309. return -7655;
  12310. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  12311. inLen, out, &sigSz, NULL, keyLen, rng);
  12312. if (ret != BAD_FUNC_ARG)
  12313. return -7656;
  12314. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  12315. inLen, out, &sigSz, key, 0, rng);
  12316. if (ret != BAD_FUNC_ARG)
  12317. return -7657;
  12318. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  12319. inLen, out, &sigSz, key, keyLen, NULL);
  12320. #ifdef HAVE_USER_RSA
  12321. /* Implementation using IPP Libraries returns:
  12322. * -101 = USER_CRYPTO_ERROR
  12323. */
  12324. if (ret == 0)
  12325. #elif defined(WOLFSSL_AFALG_XILINX_RSA) || defined(WOLFSSL_XILINX_CRYPT)
  12326. /* blinding / rng handled with hardware acceleration */
  12327. if (ret != 0)
  12328. #elif defined(WOLFSSL_ASYNC_CRYPT) || defined(WOLF_CRYPTO_CB)
  12329. /* async may not require RNG */
  12330. #if defined(WOLF_CRYPTO_CB_ONLY_RSA)
  12331. if (ret != NO_VALID_DEVID)
  12332. #else
  12333. if (ret != 0 && ret != MISSING_RNG_E)
  12334. #endif
  12335. #elif defined(HAVE_FIPS) || !defined(WC_RSA_BLINDING)
  12336. /* FIPS140 implementation does not do blinding */
  12337. if (ret != 0)
  12338. #elif defined(WOLFSSL_RSA_PUBLIC_ONLY) || defined(WOLFSSL_RSA_VERIFY_ONLY)
  12339. if (ret != SIG_TYPE_E)
  12340. #elif defined(WOLFSSL_CRYPTOCELL) || defined(WOLFSSL_SE050)
  12341. /* RNG is handled by hardware */
  12342. if (ret != 0)
  12343. #else
  12344. if (ret != MISSING_RNG_E)
  12345. #endif
  12346. return -7658;
  12347. sigSz = 0;
  12348. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  12349. inLen, out, &sigSz, key, keyLen, rng);
  12350. if (ret != BAD_FUNC_ARG)
  12351. return -7659;
  12352. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, NULL,
  12353. inLen, out, (word32)modLen, key, keyLen);
  12354. if (ret != BAD_FUNC_ARG)
  12355. return -7660;
  12356. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  12357. 0, out, (word32)modLen, key, keyLen);
  12358. if (ret != BAD_FUNC_ARG)
  12359. return -7661;
  12360. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  12361. inLen, NULL, (word32)modLen, key, keyLen);
  12362. if (ret != BAD_FUNC_ARG)
  12363. return -7662;
  12364. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  12365. inLen, out, 0, key, keyLen);
  12366. if (ret != BAD_FUNC_ARG)
  12367. return -7663;
  12368. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  12369. inLen, out, (word32)modLen, NULL, keyLen);
  12370. if (ret != BAD_FUNC_ARG)
  12371. return -7664;
  12372. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  12373. inLen, out, (word32)modLen, key, 0);
  12374. if (ret != BAD_FUNC_ARG)
  12375. return -7665;
  12376. #ifndef HAVE_ECC
  12377. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_ECC, key, keyLen);
  12378. if (ret != SIG_TYPE_E)
  12379. return -7666;
  12380. #endif
  12381. #if defined(WOLF_CRYPTO_CB_ONLY_RSA)
  12382. return 0;
  12383. #endif
  12384. /* Use APIs. */
  12385. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_RSA, key, keyLen);
  12386. if (ret != modLen)
  12387. return -7667;
  12388. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_RSA_W_ENC, key, keyLen);
  12389. if (ret != modLen)
  12390. return -7668;
  12391. sigSz = (word32)ret;
  12392. #if !defined(WOLFSSL_RSA_PUBLIC_ONLY) && !defined(WOLFSSL_RSA_VERIFY_ONLY)
  12393. XMEMSET(out, 0, sizeof(out));
  12394. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  12395. inLen, out, &sigSz, key, keyLen, rng);
  12396. if (ret != 0)
  12397. return -7669;
  12398. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  12399. inLen, out, (word32)modLen, key, keyLen);
  12400. if (ret != 0)
  12401. return -7670;
  12402. sigSz = (word32)sizeof(out);
  12403. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA_W_ENC,
  12404. in, inLen, out, &sigSz, key, keyLen, rng);
  12405. if (ret != 0)
  12406. return -7671;
  12407. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA_W_ENC,
  12408. in, inLen, out, (word32)modLen, key, keyLen);
  12409. if (ret != 0)
  12410. return -7672;
  12411. /* Wrong signature type. */
  12412. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  12413. inLen, out, (word32)modLen, key, keyLen);
  12414. if (ret == 0)
  12415. return -7673;
  12416. /* check hash functions */
  12417. sigSz = (word32)sizeof(out);
  12418. ret = wc_SignatureGenerateHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA,
  12419. hash, (int)sizeof(hash), out, &sigSz, key, keyLen, rng);
  12420. if (ret != 0)
  12421. return -7674;
  12422. ret = wc_SignatureVerifyHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA,
  12423. hash, (int)sizeof(hash), out, (word32)modLen, key, keyLen);
  12424. if (ret != 0)
  12425. return -7675;
  12426. sigSz = (word32)sizeof(out);
  12427. ret = wc_SignatureGenerateHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA_W_ENC,
  12428. hashEnc, (int)sizeof(hashEnc), out, &sigSz, key, keyLen, rng);
  12429. if (ret != 0)
  12430. return -7676;
  12431. ret = wc_SignatureVerifyHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA_W_ENC,
  12432. hashEnc, (int)sizeof(hashEnc), out, (word32)modLen, key, keyLen);
  12433. if (ret != 0)
  12434. return -7677;
  12435. #else
  12436. (void)hash;
  12437. (void)hashEnc;
  12438. #endif /* !WOLFSSL_RSA_PUBLIC_ONLY && !WOLFSSL_RSA_VERIFY_ONLY */
  12439. return 0;
  12440. }
  12441. #endif /* !NO_SIG_WRAPPER */
  12442. #ifdef WC_RSA_NONBLOCK
  12443. static int rsa_nb_test(RsaKey* key, const byte* in, word32 inLen, byte* out,
  12444. word32 outSz, byte* plain, word32 plainSz, WC_RNG* rng)
  12445. {
  12446. int ret = 0, count;
  12447. int signSz = 0;
  12448. RsaNb nb;
  12449. byte* inlinePlain = NULL;
  12450. /* Enable non-blocking RSA mode - provide context */
  12451. ret = wc_RsaSetNonBlock(key, &nb);
  12452. if (ret != 0)
  12453. return ret;
  12454. #ifdef WC_RSA_NONBLOCK_TIME
  12455. /* Enable time based RSA blocking. 8 microseconds max (3.1GHz) */
  12456. ret = wc_RsaSetNonBlockTime(key, 8, 3100);
  12457. if (ret != 0)
  12458. return ret;
  12459. #endif
  12460. count = 0;
  12461. do {
  12462. ret = wc_RsaSSL_Sign(in, inLen, out, outSz, key, rng);
  12463. count++; /* track number of would blocks */
  12464. if (ret == FP_WOULDBLOCK) {
  12465. /* do "other" work here */
  12466. }
  12467. } while (ret == FP_WOULDBLOCK);
  12468. if (ret < 0) {
  12469. return ret;
  12470. }
  12471. #ifdef DEBUG_WOLFSSL
  12472. printf("RSA non-block sign: %d times\n", count);
  12473. #endif
  12474. signSz = ret;
  12475. /* Test non-blocking verify */
  12476. XMEMSET(plain, 0, plainSz);
  12477. count = 0;
  12478. do {
  12479. ret = wc_RsaSSL_Verify(out, (word32)signSz, plain, plainSz, key);
  12480. count++; /* track number of would blocks */
  12481. if (ret == FP_WOULDBLOCK) {
  12482. /* do "other" work here */
  12483. }
  12484. } while (ret == FP_WOULDBLOCK);
  12485. if (ret < 0) {
  12486. return ret;
  12487. }
  12488. #ifdef DEBUG_WOLFSSL
  12489. printf("RSA non-block verify: %d times\n", count);
  12490. #endif
  12491. if (signSz == ret && XMEMCMP(plain, in, (size_t)ret)) {
  12492. return SIG_VERIFY_E;
  12493. }
  12494. /* Test inline non-blocking verify */
  12495. count = 0;
  12496. do {
  12497. ret = wc_RsaSSL_VerifyInline(out, (word32)signSz, &inlinePlain, key);
  12498. count++; /* track number of would blocks */
  12499. if (ret == FP_WOULDBLOCK) {
  12500. /* do "other" work here */
  12501. }
  12502. } while (ret == FP_WOULDBLOCK);
  12503. if (ret < 0) {
  12504. return ret;
  12505. }
  12506. #ifdef DEBUG_WOLFSSL
  12507. printf("RSA non-block inline verify: %d times\n", count);
  12508. #endif
  12509. if (signSz == ret && XMEMCMP(inlinePlain, in, (size_t)ret)) {
  12510. return SIG_VERIFY_E;
  12511. }
  12512. /* Disabling non-block RSA mode */
  12513. ret = wc_RsaSetNonBlock(key, NULL);
  12514. (void)count;
  12515. return 0;
  12516. }
  12517. #endif
  12518. #if !defined(HAVE_USER_RSA) && !defined(NO_ASN)
  12519. static int rsa_decode_test(RsaKey* keyPub)
  12520. {
  12521. int ret;
  12522. word32 inSz;
  12523. word32 inOutIdx;
  12524. WOLFSSL_SMALL_STACK_STATIC const byte n[2] = { 0x00, 0x23 };
  12525. WOLFSSL_SMALL_STACK_STATIC const byte e[2] = { 0x00, 0x03 };
  12526. WOLFSSL_SMALL_STACK_STATIC const byte good[] = { 0x30, 0x06, 0x02, 0x01, 0x23, 0x02, 0x1,
  12527. 0x03 };
  12528. WOLFSSL_SMALL_STACK_STATIC const byte goodAlgId[] = {
  12529. 0x30, 0x18, 0x30, 0x16,
  12530. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
  12531. 0x03, 0x09, 0x00, 0x30, 0x06, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  12532. WOLFSSL_SMALL_STACK_STATIC const byte goodAlgIdNull[] = {
  12533. 0x30, 0x1a, 0x30, 0x18,
  12534. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
  12535. 0x05, 0x00, 0x03, 0x09, 0x00, 0x30, 0x06, 0x02, 0x01, 0x23,
  12536. 0x02, 0x1, 0x03 };
  12537. WOLFSSL_SMALL_STACK_STATIC const byte badAlgIdNull[] = {
  12538. 0x30, 0x1b, 0x30, 0x19,
  12539. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
  12540. 0x05, 0x01, 0x00, 0x03, 0x09, 0x00, 0x30, 0x06, 0x02, 0x01, 0x23,
  12541. 0x02, 0x1, 0x03 };
  12542. WOLFSSL_SMALL_STACK_STATIC const byte badNotBitString[] = {
  12543. 0x30, 0x18, 0x30, 0x16,
  12544. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
  12545. 0x04, 0x09, 0x00, 0x30, 0x06, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  12546. WOLFSSL_SMALL_STACK_STATIC const byte badBitStringLen[] = {
  12547. 0x30, 0x18, 0x30, 0x16,
  12548. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
  12549. 0x03, 0x0a, 0x00, 0x30, 0x06, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  12550. WOLFSSL_SMALL_STACK_STATIC const byte badNoSeq[] = {
  12551. 0x30, 0x16, 0x30, 0x14,
  12552. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
  12553. 0x07, 0x00, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  12554. WOLFSSL_SMALL_STACK_STATIC const byte badNoObj[] = {
  12555. 0x30, 0x0f, 0x30, 0x0d, 0x05, 0x00, 0x03, 0x09, 0x00, 0x30, 0x06,
  12556. 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  12557. WOLFSSL_SMALL_STACK_STATIC const byte badIntN[] = {
  12558. 0x30, 0x06, 0x02, 0x05, 0x23, 0x02, 0x1, 0x03 };
  12559. WOLFSSL_SMALL_STACK_STATIC const byte badNotIntE[] = {
  12560. 0x30, 0x06, 0x02, 0x01, 0x23, 0x04, 0x1, 0x03 };
  12561. WOLFSSL_SMALL_STACK_STATIC const byte badLength[] = {
  12562. 0x30, 0x04, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  12563. WOLFSSL_SMALL_STACK_STATIC const byte badBitStrNoZero[] = {
  12564. 0x30, 0x17, 0x30, 0x15,
  12565. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
  12566. 0x03, 0x08, 0x30, 0x06, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  12567. ret = wc_InitRsaKey(keyPub, NULL);
  12568. if (ret != 0)
  12569. return -7690;
  12570. /* Parameter Validation testing. */
  12571. ret = wc_RsaPublicKeyDecodeRaw(NULL, sizeof(n), e, sizeof(e), keyPub);
  12572. if (ret != BAD_FUNC_ARG) {
  12573. ret = -7691;
  12574. goto done;
  12575. }
  12576. ret = wc_RsaPublicKeyDecodeRaw(n, sizeof(n), NULL, sizeof(e), keyPub);
  12577. if (ret != BAD_FUNC_ARG) {
  12578. ret = -7692;
  12579. goto done;
  12580. }
  12581. ret = wc_RsaPublicKeyDecodeRaw(n, sizeof(n), e, sizeof(e), NULL);
  12582. if (ret != BAD_FUNC_ARG) {
  12583. ret = -7693;
  12584. goto done;
  12585. }
  12586. ret = wc_RsaPublicKeyDecodeRaw(n, (word32)-1, e, sizeof(e), keyPub);
  12587. #if !defined(WOLFSSL_SP_MATH) & !defined(WOLFSSL_SP_MATH_ALL)
  12588. if (ret != 0) {
  12589. #else
  12590. if (ret != ASN_GETINT_E) {
  12591. #endif
  12592. ret = -7694;
  12593. goto done;
  12594. }
  12595. wc_FreeRsaKey(keyPub);
  12596. ret = wc_InitRsaKey(keyPub, NULL);
  12597. if (ret != 0)
  12598. return -7695;
  12599. ret = wc_RsaPublicKeyDecodeRaw(n, sizeof(n), e, (word32)-1, keyPub);
  12600. #if !defined(WOLFSSL_SP_MATH) & !defined(WOLFSSL_SP_MATH_ALL)
  12601. if (ret != 0) {
  12602. #else
  12603. if (ret != ASN_GETINT_E) {
  12604. #endif
  12605. ret = -7696;
  12606. goto done;
  12607. }
  12608. wc_FreeRsaKey(keyPub);
  12609. ret = wc_InitRsaKey(keyPub, NULL);
  12610. if (ret != 0)
  12611. return -7697;
  12612. /* Use API. */
  12613. ret = wc_RsaPublicKeyDecodeRaw(n, sizeof(n), e, sizeof(e), keyPub);
  12614. if (ret != 0) {
  12615. ret = -7698;
  12616. goto done;
  12617. }
  12618. wc_FreeRsaKey(keyPub);
  12619. ret = wc_InitRsaKey(keyPub, NULL);
  12620. if (ret != 0)
  12621. return -7699;
  12622. /* Parameter Validation testing. */
  12623. inSz = sizeof(good);
  12624. ret = wc_RsaPublicKeyDecode(NULL, &inOutIdx, keyPub, inSz);
  12625. if (ret != BAD_FUNC_ARG) {
  12626. ret = -7700;
  12627. goto done;
  12628. }
  12629. ret = wc_RsaPublicKeyDecode(good, NULL, keyPub, inSz);
  12630. if (ret != BAD_FUNC_ARG) {
  12631. ret = -7701;
  12632. goto done;
  12633. }
  12634. ret = wc_RsaPublicKeyDecode(good, &inOutIdx, NULL, inSz);
  12635. if (ret != BAD_FUNC_ARG) {
  12636. ret = -7702;
  12637. goto done;
  12638. }
  12639. /* Use good data and offset to bad data. */
  12640. inOutIdx = 2;
  12641. inSz = sizeof(good) - inOutIdx;
  12642. ret = wc_RsaPublicKeyDecode(good, &inOutIdx, keyPub, inSz);
  12643. if (ret != ASN_PARSE_E) {
  12644. ret = -7703;
  12645. goto done;
  12646. }
  12647. inOutIdx = 2;
  12648. inSz = sizeof(goodAlgId) - inOutIdx;
  12649. ret = wc_RsaPublicKeyDecode(goodAlgId, &inOutIdx, keyPub, inSz);
  12650. if (ret != ASN_PARSE_E) {
  12651. ret = -7704;
  12652. goto done;
  12653. }
  12654. inOutIdx = 2;
  12655. inSz = sizeof(goodAlgId);
  12656. ret = wc_RsaPublicKeyDecode(goodAlgId, &inOutIdx, keyPub, inSz);
  12657. #ifndef WOLFSSL_NO_DECODE_EXTRA
  12658. if (ret != ASN_PARSE_E)
  12659. #else
  12660. if (ret != ASN_RSA_KEY_E)
  12661. #endif
  12662. {
  12663. ret = -7705;
  12664. goto done;
  12665. }
  12666. /* Try different bad data. */
  12667. inSz = sizeof(badAlgIdNull);
  12668. inOutIdx = 0;
  12669. ret = wc_RsaPublicKeyDecode(badAlgIdNull, &inOutIdx, keyPub, inSz);
  12670. if (ret != ASN_EXPECT_0_E) {
  12671. ret = -7706;
  12672. goto done;
  12673. }
  12674. inSz = sizeof(badNotBitString);
  12675. inOutIdx = 0;
  12676. ret = wc_RsaPublicKeyDecode(badNotBitString, &inOutIdx, keyPub, inSz);
  12677. if (ret != ASN_BITSTR_E) {
  12678. ret = -7707;
  12679. goto done;
  12680. }
  12681. inSz = sizeof(badBitStringLen);
  12682. inOutIdx = 0;
  12683. ret = wc_RsaPublicKeyDecode(badBitStringLen, &inOutIdx, keyPub, inSz);
  12684. if (ret != ASN_PARSE_E) {
  12685. ret = -7708;
  12686. goto done;
  12687. }
  12688. inSz = sizeof(badNoSeq);
  12689. inOutIdx = 0;
  12690. ret = wc_RsaPublicKeyDecode(badNoSeq, &inOutIdx, keyPub, inSz);
  12691. if (ret != ASN_PARSE_E) {
  12692. ret = -7709;
  12693. goto done;
  12694. }
  12695. inSz = sizeof(badNoObj);
  12696. inOutIdx = 0;
  12697. ret = wc_RsaPublicKeyDecode(badNoObj, &inOutIdx, keyPub, inSz);
  12698. if (ret != ASN_PARSE_E && ret != ASN_OBJECT_ID_E) {
  12699. ret = -7710;
  12700. goto done;
  12701. }
  12702. inSz = sizeof(badIntN);
  12703. inOutIdx = 0;
  12704. ret = wc_RsaPublicKeyDecode(badIntN, &inOutIdx, keyPub, inSz);
  12705. if (ret != ASN_RSA_KEY_E && ret != ASN_PARSE_E) {
  12706. ret = -7711;
  12707. goto done;
  12708. }
  12709. inSz = sizeof(badNotIntE);
  12710. inOutIdx = 0;
  12711. ret = wc_RsaPublicKeyDecode(badNotIntE, &inOutIdx, keyPub, inSz);
  12712. if (ret != ASN_RSA_KEY_E && ret != ASN_PARSE_E) {
  12713. ret = -7712;
  12714. goto done;
  12715. }
  12716. /* TODO: Shouldn't pass as the sequence length is too small. */
  12717. inSz = sizeof(badLength);
  12718. inOutIdx = 0;
  12719. ret = wc_RsaPublicKeyDecode(badLength, &inOutIdx, keyPub, inSz);
  12720. #ifndef WOLFSSL_ASN_TEMPLATE
  12721. if (ret != 0)
  12722. #else
  12723. if (ret != ASN_PARSE_E)
  12724. #endif
  12725. {
  12726. ret = -7713;
  12727. goto done;
  12728. }
  12729. /* TODO: Shouldn't ignore object id's data. */
  12730. wc_FreeRsaKey(keyPub);
  12731. ret = wc_InitRsaKey(keyPub, NULL);
  12732. if (ret != 0)
  12733. return -7714;
  12734. inSz = sizeof(badBitStrNoZero);
  12735. inOutIdx = 0;
  12736. ret = wc_RsaPublicKeyDecode(badBitStrNoZero, &inOutIdx, keyPub, inSz);
  12737. if (ret != ASN_EXPECT_0_E && ret != ASN_PARSE_E) {
  12738. ret = -7715;
  12739. goto done;
  12740. }
  12741. wc_FreeRsaKey(keyPub);
  12742. ret = wc_InitRsaKey(keyPub, NULL);
  12743. if (ret != 0)
  12744. return -7716;
  12745. /* Valid data cases. */
  12746. inSz = sizeof(good);
  12747. inOutIdx = 0;
  12748. ret = wc_RsaPublicKeyDecode(good, &inOutIdx, keyPub, inSz);
  12749. if (ret != 0) {
  12750. ret = -7717;
  12751. goto done;
  12752. }
  12753. if (inOutIdx != inSz) {
  12754. ret = -7718;
  12755. goto done;
  12756. }
  12757. wc_FreeRsaKey(keyPub);
  12758. ret = wc_InitRsaKey(keyPub, NULL);
  12759. if (ret != 0)
  12760. return -7719;
  12761. inSz = sizeof(goodAlgId);
  12762. inOutIdx = 0;
  12763. ret = wc_RsaPublicKeyDecode(goodAlgId, &inOutIdx, keyPub, inSz);
  12764. if (ret != 0) {
  12765. ret = -7720;
  12766. goto done;
  12767. }
  12768. if (inOutIdx != inSz) {
  12769. ret = -7721;
  12770. goto done;
  12771. }
  12772. wc_FreeRsaKey(keyPub);
  12773. ret = wc_InitRsaKey(keyPub, NULL);
  12774. if (ret != 0)
  12775. return -7722;
  12776. inSz = sizeof(goodAlgIdNull);
  12777. inOutIdx = 0;
  12778. ret = wc_RsaPublicKeyDecode(goodAlgIdNull, &inOutIdx, keyPub, inSz);
  12779. if (ret != 0) {
  12780. ret = -7723;
  12781. goto done;
  12782. }
  12783. if (inOutIdx != inSz) {
  12784. ret = -7724;
  12785. goto done;
  12786. }
  12787. done:
  12788. wc_FreeRsaKey(keyPub);
  12789. return ret;
  12790. }
  12791. #endif
  12792. #if defined(WC_RSA_PSS) && !defined(HAVE_FIPS_VERSION) /* not supported with FIPSv1 */
  12793. /* Need to create known good signatures to test with this. */
  12794. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
  12795. !defined(WOLF_CRYPTO_CB_ONLY_RSA)
  12796. static int rsa_pss_test(WC_RNG* rng, RsaKey* key)
  12797. {
  12798. byte digest[WC_MAX_DIGEST_SIZE];
  12799. int ret = 0;
  12800. const char inStr[] = TEST_STRING;
  12801. word32 inLen = (word32)TEST_STRING_SZ;
  12802. word32 outSz;
  12803. word32 plainSz;
  12804. word32 digestSz;
  12805. int i, j;
  12806. #ifdef RSA_PSS_TEST_WRONG_PARAMS
  12807. int k, l;
  12808. #endif
  12809. #ifndef WOLFSSL_SE050
  12810. int len;
  12811. #endif
  12812. byte* plain;
  12813. int mgf[] = {
  12814. #ifndef NO_SHA
  12815. WC_MGF1SHA1,
  12816. #endif
  12817. #ifdef WOLFSSL_SHA224
  12818. WC_MGF1SHA224,
  12819. #endif
  12820. WC_MGF1SHA256,
  12821. #ifdef WOLFSSL_SHA384
  12822. WC_MGF1SHA384,
  12823. #endif
  12824. #ifdef WOLFSSL_SHA512
  12825. WC_MGF1SHA512
  12826. #endif
  12827. };
  12828. enum wc_HashType hash[] = {
  12829. #ifndef NO_SHA
  12830. WC_HASH_TYPE_SHA,
  12831. #endif
  12832. #ifdef WOLFSSL_SHA224
  12833. WC_HASH_TYPE_SHA224,
  12834. #endif
  12835. WC_HASH_TYPE_SHA256,
  12836. #ifdef WOLFSSL_SHA384
  12837. WC_HASH_TYPE_SHA384,
  12838. #endif
  12839. #ifdef WOLFSSL_SHA512
  12840. WC_HASH_TYPE_SHA512,
  12841. #endif
  12842. };
  12843. WC_DECLARE_VAR(in, byte, RSA_TEST_BYTES, HEAP_HINT);
  12844. WC_DECLARE_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
  12845. WC_DECLARE_VAR(sig, byte, RSA_TEST_BYTES, HEAP_HINT);
  12846. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  12847. if (in == NULL || out == NULL || sig == NULL)
  12848. ERROR_OUT(MEMORY_E, exit_rsa_pss);
  12849. #endif
  12850. XMEMCPY(in, inStr, inLen);
  12851. /* Test all combinations of hash and MGF. */
  12852. for (j = 0; j < (int)(sizeof(hash)/sizeof(*hash)); j++) {
  12853. /* Calculate hash of message. */
  12854. ret = wc_Hash(hash[j], in, inLen, digest, sizeof(digest));
  12855. if (ret != 0)
  12856. ERROR_OUT(-7730, exit_rsa_pss);
  12857. digestSz = wc_HashGetDigestSize(hash[j]);
  12858. #ifdef WOLFSSL_SE050
  12859. /* SE050 only supports MGF matched to same hash type */
  12860. i = j;
  12861. #else
  12862. for (i = 0; i < (int)(sizeof(mgf)/sizeof(*mgf)); i++) {
  12863. #endif
  12864. outSz = RSA_TEST_BYTES;
  12865. do {
  12866. #if defined(WOLFSSL_ASYNC_CRYPT)
  12867. ret = wc_AsyncWait(ret, &key->asyncDev,
  12868. WC_ASYNC_FLAG_CALL_AGAIN);
  12869. #endif
  12870. if (ret >= 0) {
  12871. ret = wc_RsaPSS_Sign_ex(digest, digestSz, out, outSz,
  12872. hash[j], mgf[i], -1, key, rng);
  12873. }
  12874. } while (ret == WC_PENDING_E);
  12875. if (ret <= 0)
  12876. ERROR_OUT(-7731, exit_rsa_pss);
  12877. outSz = ret;
  12878. XMEMCPY(sig, out, outSz);
  12879. plain = NULL;
  12880. TEST_SLEEP();
  12881. do {
  12882. #if defined(WOLFSSL_ASYNC_CRYPT)
  12883. ret = wc_AsyncWait(ret, &key->asyncDev,
  12884. WC_ASYNC_FLAG_CALL_AGAIN);
  12885. #endif
  12886. if (ret >= 0) {
  12887. ret = wc_RsaPSS_VerifyInline_ex(sig, outSz, &plain, hash[j],
  12888. mgf[i], -1, key);
  12889. }
  12890. } while (ret == WC_PENDING_E);
  12891. if (ret <= 0)
  12892. ERROR_OUT(-7732, exit_rsa_pss);
  12893. plainSz = ret;
  12894. TEST_SLEEP();
  12895. #if defined(HAVE_SELFTEST) && \
  12896. (!defined(HAVE_SELFTEST_VERSION) || (HAVE_SELFTEST_VERSION < 2))
  12897. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  12898. hash[j], -1);
  12899. #elif defined(HAVE_SELFTEST) && (HAVE_SELFTEST_VERSION == 2)
  12900. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  12901. hash[j], -1, 0);
  12902. #else
  12903. ret = wc_RsaPSS_CheckPadding_ex2(digest, digestSz, plain, plainSz,
  12904. hash[j], -1, wc_RsaEncryptSize(key)*8, HEAP_HINT);
  12905. #endif
  12906. if (ret != 0)
  12907. ERROR_OUT(-7733, exit_rsa_pss);
  12908. #ifdef RSA_PSS_TEST_WRONG_PARAMS
  12909. for (k = 0; k < (int)(sizeof(mgf)/sizeof(*mgf)); k++) {
  12910. for (l = 0; l < (int)(sizeof(hash)/sizeof(*hash)); l++) {
  12911. if (i == k && j == l)
  12912. continue;
  12913. XMEMCPY(sig, out, outSz);
  12914. do {
  12915. #if defined(WOLFSSL_ASYNC_CRYPT)
  12916. ret = wc_AsyncWait(ret, &key->asyncDev,
  12917. WC_ASYNC_FLAG_CALL_AGAIN);
  12918. #endif
  12919. if (ret >= 0) {
  12920. ret = wc_RsaPSS_VerifyInline_ex(sig, outSz,
  12921. (byte**)&plain, hash[l], mgf[k], -1, key);
  12922. }
  12923. } while (ret == WC_PENDING_E);
  12924. if (ret >= 0)
  12925. ERROR_OUT(-7734, exit_rsa_pss);
  12926. }
  12927. }
  12928. #endif
  12929. #ifndef WOLFSSL_SE050
  12930. } /* end mgf for loop */
  12931. #endif
  12932. }
  12933. /* SE050 generates salts internally only of hash length */
  12934. #ifndef WOLFSSL_SE050
  12935. /* Test that a salt length of zero works. */
  12936. digestSz = wc_HashGetDigestSize(hash[0]);
  12937. outSz = RSA_TEST_BYTES;
  12938. do {
  12939. #if defined(WOLFSSL_ASYNC_CRYPT)
  12940. ret = wc_AsyncWait(ret, &key->asyncDev,
  12941. WC_ASYNC_FLAG_CALL_AGAIN);
  12942. #endif
  12943. if (ret >= 0) {
  12944. ret = wc_RsaPSS_Sign_ex(digest, digestSz, out, outSz, hash[0],
  12945. mgf[0], 0, key, rng);
  12946. }
  12947. } while (ret == WC_PENDING_E);
  12948. if (ret <= 0)
  12949. ERROR_OUT(-7735, exit_rsa_pss);
  12950. outSz = ret;
  12951. TEST_SLEEP();
  12952. do {
  12953. #if defined(WOLFSSL_ASYNC_CRYPT)
  12954. ret = wc_AsyncWait(ret, &key->asyncDev,
  12955. WC_ASYNC_FLAG_CALL_AGAIN);
  12956. #endif
  12957. if (ret >= 0) {
  12958. ret = wc_RsaPSS_Verify_ex(out, outSz, sig, outSz, hash[0], mgf[0],
  12959. 0, key);
  12960. }
  12961. } while (ret == WC_PENDING_E);
  12962. if (ret <= 0)
  12963. ERROR_OUT(-7736, exit_rsa_pss);
  12964. plainSz = ret;
  12965. TEST_SLEEP();
  12966. do {
  12967. #if defined(WOLFSSL_ASYNC_CRYPT)
  12968. ret = wc_AsyncWait(ret, &key->asyncDev,
  12969. WC_ASYNC_FLAG_CALL_AGAIN);
  12970. #endif
  12971. if (ret >= 0) {
  12972. #if defined(HAVE_SELFTEST) && \
  12973. (!defined(HAVE_SELFTEST_VERSION) || (HAVE_SELFTEST_VERSION < 2))
  12974. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, sig, plainSz,
  12975. hash[0], 0);
  12976. #elif defined(HAVE_SELFTEST) && (HAVE_SELFTEST_VERSION == 2)
  12977. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, sig, plainSz,
  12978. hash[0], 0, 0);
  12979. #else
  12980. ret = wc_RsaPSS_CheckPadding_ex2(digest, digestSz, sig, plainSz,
  12981. hash[0], 0, 0, HEAP_HINT);
  12982. #endif
  12983. }
  12984. } while (ret == WC_PENDING_E);
  12985. if (ret != 0)
  12986. ERROR_OUT(-7737, exit_rsa_pss);
  12987. XMEMCPY(sig, out, outSz);
  12988. plain = NULL;
  12989. do {
  12990. #if defined(WOLFSSL_ASYNC_CRYPT)
  12991. ret = wc_AsyncWait(ret, &key->asyncDev,
  12992. WC_ASYNC_FLAG_CALL_AGAIN);
  12993. #endif
  12994. if (ret >= 0) {
  12995. ret = wc_RsaPSS_VerifyInline_ex(sig, outSz, &plain, hash[0], mgf[0],
  12996. 0, key);
  12997. }
  12998. } while (ret == WC_PENDING_E);
  12999. if (ret <= 0)
  13000. ERROR_OUT(-7738, exit_rsa_pss);
  13001. plainSz = ret;
  13002. TEST_SLEEP();
  13003. #if defined(HAVE_SELFTEST) && \
  13004. (!defined(HAVE_SELFTEST_VERSION) || (HAVE_SELFTEST_VERSION < 2))
  13005. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  13006. hash[0], 0);
  13007. #elif defined(HAVE_SELFTEST) && (HAVE_SELFTEST_VERSION == 2)
  13008. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  13009. hash[0], 0, 0);
  13010. #else
  13011. ret = wc_RsaPSS_CheckPadding_ex2(digest, digestSz, plain, plainSz, hash[0],
  13012. 0, 0, HEAP_HINT);
  13013. #endif
  13014. if (ret != 0)
  13015. ERROR_OUT(-7739, exit_rsa_pss);
  13016. /* Test bad salt lengths in various APIs. */
  13017. digestSz = wc_HashGetDigestSize(hash[0]);
  13018. outSz = RSA_TEST_BYTES;
  13019. #ifndef WOLFSSL_PSS_SALT_LEN_DISCOVER
  13020. len = -2;
  13021. #else
  13022. len = -3;
  13023. #endif
  13024. do {
  13025. #if defined(WOLFSSL_ASYNC_CRYPT)
  13026. ret = wc_AsyncWait(ret, &key->asyncDev,
  13027. WC_ASYNC_FLAG_CALL_AGAIN);
  13028. #endif
  13029. if (ret >= 0) {
  13030. ret = wc_RsaPSS_Sign_ex(digest, digestSz, out, outSz, hash[0],
  13031. mgf[0], len, key, rng);
  13032. }
  13033. } while (ret == WC_PENDING_E);
  13034. if (ret != PSS_SALTLEN_E)
  13035. ERROR_OUT(-7740, exit_rsa_pss);
  13036. do {
  13037. #if defined(WOLFSSL_ASYNC_CRYPT)
  13038. ret = wc_AsyncWait(ret, &key->asyncDev,
  13039. WC_ASYNC_FLAG_CALL_AGAIN);
  13040. #endif
  13041. if (ret >= 0) {
  13042. ret = wc_RsaPSS_Sign_ex(digest, digestSz, out, outSz, hash[0],
  13043. mgf[0], digestSz + 1, key, rng);
  13044. }
  13045. } while (ret == WC_PENDING_E);
  13046. if (ret != PSS_SALTLEN_E)
  13047. ERROR_OUT(-7741, exit_rsa_pss);
  13048. TEST_SLEEP();
  13049. do {
  13050. #if defined(WOLFSSL_ASYNC_CRYPT)
  13051. ret = wc_AsyncWait(ret, &key->asyncDev,
  13052. WC_ASYNC_FLAG_CALL_AGAIN);
  13053. #endif
  13054. if (ret >= 0) {
  13055. ret = wc_RsaPSS_VerifyInline_ex(sig, outSz, &plain, hash[0],
  13056. mgf[0], -2, key);
  13057. }
  13058. } while (ret == WC_PENDING_E);
  13059. if (ret != PSS_SALTLEN_E)
  13060. ERROR_OUT(-7742, exit_rsa_pss);
  13061. TEST_SLEEP();
  13062. do {
  13063. #if defined(WOLFSSL_ASYNC_CRYPT)
  13064. ret = wc_AsyncWait(ret, &key->asyncDev,
  13065. WC_ASYNC_FLAG_CALL_AGAIN);
  13066. #endif
  13067. if (ret >= 0) {
  13068. ret = wc_RsaPSS_VerifyInline_ex(sig, outSz, &plain, hash[0], mgf[0],
  13069. digestSz + 1, key);
  13070. }
  13071. } while (ret == WC_PENDING_E);
  13072. if (ret != PSS_SALTLEN_E)
  13073. ERROR_OUT(-7743, exit_rsa_pss);
  13074. TEST_SLEEP();
  13075. #ifndef WOLFSSL_PSS_SALT_LEN_DISCOVER
  13076. len = -2;
  13077. #else
  13078. len = -3;
  13079. #endif
  13080. #if defined(HAVE_SELFTEST) && \
  13081. (!defined(HAVE_SELFTEST_VERSION) || (HAVE_SELFTEST_VERSION < 2))
  13082. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  13083. hash[0], len);
  13084. #elif defined(HAVE_SELFTEST) && (HAVE_SELFTEST_VERSION == 2)
  13085. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  13086. hash[0], len, 0);
  13087. #else
  13088. ret = wc_RsaPSS_CheckPadding_ex2(digest, digestSz, plain, plainSz, hash[0],
  13089. len, 0, HEAP_HINT);
  13090. #endif
  13091. if (ret != PSS_SALTLEN_E)
  13092. ERROR_OUT(-7744, exit_rsa_pss);
  13093. #ifndef WOLFSSL_PSS_LONG_SALT
  13094. len = digestSz + 1;
  13095. #else
  13096. len = plainSz - digestSz - 1;
  13097. #endif
  13098. #if defined(HAVE_SELFTEST) && \
  13099. (!defined(HAVE_SELFTEST_VERSION) || (HAVE_SELFTEST_VERSION < 2))
  13100. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  13101. hash[0], len);
  13102. #elif defined(HAVE_SELFTEST) && (HAVE_SELFTEST_VERSION == 2)
  13103. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  13104. hash[0], len, 0);
  13105. #else
  13106. ret = wc_RsaPSS_CheckPadding_ex2(digest, digestSz, plain, plainSz, hash[0],
  13107. len, 0, HEAP_HINT);
  13108. #endif
  13109. if (ret != PSS_SALTLEN_E)
  13110. ERROR_OUT(-7745, exit_rsa_pss);
  13111. ret = 0;
  13112. #endif /* WOLFSSL_SE050 */
  13113. exit_rsa_pss:
  13114. WC_FREE_VAR(sig, HEAP_HINT);
  13115. WC_FREE_VAR(in, HEAP_HINT);
  13116. WC_FREE_VAR(out, HEAP_HINT);
  13117. return ret;
  13118. }
  13119. #endif /* !WOLFSSL_RSA_VERIFY_ONLY && !WOLFSSL_RSA_PUBLIC_ONLY */
  13120. #endif
  13121. #ifdef WC_RSA_NO_PADDING
  13122. WOLFSSL_TEST_SUBROUTINE int rsa_no_pad_test(void)
  13123. {
  13124. WC_RNG rng;
  13125. byte* tmp = NULL;
  13126. size_t bytes;
  13127. int ret;
  13128. word32 inLen = 0;
  13129. word32 idx = 0;
  13130. word32 outSz = RSA_TEST_BYTES;
  13131. word32 plainSz = RSA_TEST_BYTES;
  13132. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  13133. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096) && \
  13134. !defined(NO_FILESYSTEM)
  13135. XFILE file;
  13136. #endif
  13137. WC_DECLARE_VAR(key, RsaKey, 1, HEAP_HINT);
  13138. WC_DECLARE_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
  13139. WC_DECLARE_VAR(plain, byte, RSA_TEST_BYTES, HEAP_HINT);
  13140. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  13141. if (key == NULL || out == NULL || plain == NULL)
  13142. ERROR_OUT(MEMORY_E, exit_rsa_nopadding);
  13143. #endif
  13144. /* initialize stack structures */
  13145. XMEMSET(&rng, 0, sizeof(rng));
  13146. XMEMSET(key, 0, sizeof(RsaKey));
  13147. #ifdef USE_CERT_BUFFERS_1024
  13148. bytes = (size_t)sizeof_client_key_der_1024;
  13149. if (bytes < (size_t)sizeof_client_cert_der_1024)
  13150. bytes = (size_t)sizeof_client_cert_der_1024;
  13151. #elif defined(USE_CERT_BUFFERS_2048)
  13152. bytes = (size_t)sizeof_client_key_der_2048;
  13153. if (bytes < (size_t)sizeof_client_cert_der_2048)
  13154. bytes = (size_t)sizeof_client_cert_der_2048;
  13155. #else
  13156. bytes = FOURK_BUF;
  13157. #endif
  13158. tmp = (byte*)XMALLOC(bytes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13159. if (tmp == NULL
  13160. #ifdef WOLFSSL_ASYNC_CRYPT
  13161. || out == NULL || plain == NULL
  13162. #endif
  13163. ) {
  13164. ERROR_OUT(-7800, exit_rsa_nopadding);
  13165. }
  13166. #ifdef USE_CERT_BUFFERS_1024
  13167. XMEMCPY(tmp, client_key_der_1024, (size_t)sizeof_client_key_der_1024);
  13168. #elif defined(USE_CERT_BUFFERS_2048)
  13169. XMEMCPY(tmp, client_key_der_2048, (size_t)sizeof_client_key_der_2048);
  13170. #elif defined(USE_CERT_BUFFERS_3072)
  13171. XMEMCPY(tmp, client_key_der_3072, (size_t)sizeof_client_key_der_3072);
  13172. #elif defined(USE_CERT_BUFFERS_4096)
  13173. XMEMCPY(tmp, client_key_der_4096, (size_t)sizeof_client_key_der_4096);
  13174. #elif !defined(NO_FILESYSTEM)
  13175. file = XFOPEN(clientKey, "rb");
  13176. if (!file) {
  13177. err_sys("can't open clientKey, Please run from wolfSSL home dir", -40);
  13178. ERROR_OUT(-7801, exit_rsa_nopadding);
  13179. }
  13180. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  13181. XFCLOSE(file);
  13182. #else
  13183. /* No key to use. */
  13184. ERROR_OUT(-7802, exit_rsa_nopadding);
  13185. #endif /* USE_CERT_BUFFERS */
  13186. ret = wc_InitRsaKey_ex(key, HEAP_HINT, devId);
  13187. if (ret != 0) {
  13188. ERROR_OUT(-7803, exit_rsa_nopadding);
  13189. }
  13190. ret = wc_RsaPrivateKeyDecode(tmp, &idx, key, (word32)bytes);
  13191. if (ret != 0) {
  13192. ERROR_OUT(-7804, exit_rsa_nopadding);
  13193. }
  13194. /* after loading in key use tmp as the test buffer */
  13195. #ifndef HAVE_FIPS
  13196. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  13197. #else
  13198. ret = wc_InitRng(&rng);
  13199. #endif
  13200. if (ret != 0) {
  13201. ERROR_OUT(-7805, exit_rsa_nopadding);
  13202. }
  13203. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  13204. inLen = wc_RsaEncryptSize(key);
  13205. outSz = inLen;
  13206. plainSz = inLen;
  13207. XMEMSET(tmp, 7, inLen);
  13208. do {
  13209. #if defined(WOLFSSL_ASYNC_CRYPT)
  13210. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13211. #endif
  13212. if (ret >= 0) {
  13213. ret = wc_RsaDirect(tmp, inLen, out, &outSz, key,
  13214. RSA_PRIVATE_ENCRYPT, &rng);
  13215. }
  13216. } while (ret == WC_PENDING_E);
  13217. if (ret <= 0) {
  13218. ERROR_OUT(-7806, exit_rsa_nopadding);
  13219. }
  13220. /* encrypted result should not be the same as input */
  13221. if (XMEMCMP(out, tmp, inLen) == 0) {
  13222. ERROR_OUT(-7807, exit_rsa_nopadding);
  13223. }
  13224. TEST_SLEEP();
  13225. /* decrypt with public key and compare result */
  13226. do {
  13227. #if defined(WOLFSSL_ASYNC_CRYPT)
  13228. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13229. #endif
  13230. if (ret >= 0) {
  13231. ret = wc_RsaDirect(out, outSz, plain, &plainSz, key,
  13232. RSA_PUBLIC_DECRYPT, &rng);
  13233. }
  13234. } while (ret == WC_PENDING_E);
  13235. if (ret <= 0) {
  13236. ERROR_OUT(-7808, exit_rsa_nopadding);
  13237. }
  13238. if (XMEMCMP(plain, tmp, inLen) != 0) {
  13239. ERROR_OUT(-7809, exit_rsa_nopadding);
  13240. }
  13241. TEST_SLEEP();
  13242. #endif
  13243. #ifdef WC_RSA_BLINDING
  13244. ret = wc_RsaSetRNG(NULL, &rng);
  13245. if (ret != BAD_FUNC_ARG) {
  13246. ERROR_OUT(-7810, exit_rsa_nopadding);
  13247. }
  13248. ret = wc_RsaSetRNG(key, &rng);
  13249. if (ret < 0) {
  13250. ERROR_OUT(-7811, exit_rsa_nopadding);
  13251. }
  13252. #endif
  13253. /* test encrypt and decrypt using WC_RSA_NO_PAD */
  13254. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  13255. do {
  13256. #if defined(WOLFSSL_ASYNC_CRYPT)
  13257. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13258. #endif
  13259. if (ret >= 0) {
  13260. ret = wc_RsaPublicEncrypt_ex(tmp, inLen, out, (int)outSz, key, &rng,
  13261. WC_RSA_NO_PAD, WC_HASH_TYPE_NONE, WC_MGF1NONE, NULL, 0);
  13262. }
  13263. } while (ret == WC_PENDING_E);
  13264. if (ret < 0) {
  13265. ERROR_OUT(-7812, exit_rsa_nopadding);
  13266. }
  13267. TEST_SLEEP();
  13268. #endif /* WOLFSSL_RSA_VERIFY_ONLY */
  13269. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  13270. do {
  13271. #if defined(WOLFSSL_ASYNC_CRYPT)
  13272. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13273. #endif
  13274. if (ret >= 0) {
  13275. ret = wc_RsaPrivateDecrypt_ex(out, outSz, plain, (int)plainSz, key,
  13276. WC_RSA_NO_PAD, WC_HASH_TYPE_NONE, WC_MGF1NONE, NULL, 0);
  13277. }
  13278. } while (ret == WC_PENDING_E);
  13279. if (ret < 0) {
  13280. ERROR_OUT(-7813, exit_rsa_nopadding);
  13281. }
  13282. if (XMEMCMP(plain, tmp, inLen) != 0) {
  13283. ERROR_OUT(-7814, exit_rsa_nopadding);
  13284. }
  13285. TEST_SLEEP();
  13286. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  13287. /* test some bad arguments */
  13288. ret = wc_RsaDirect(out, outSz, plain, &plainSz, key, -1,
  13289. &rng);
  13290. if (ret != BAD_FUNC_ARG) {
  13291. ERROR_OUT(-7815, exit_rsa_nopadding);
  13292. }
  13293. ret = wc_RsaDirect(out, outSz, plain, &plainSz, NULL, RSA_PUBLIC_DECRYPT,
  13294. &rng);
  13295. if (ret != BAD_FUNC_ARG) {
  13296. ERROR_OUT(-7816, exit_rsa_nopadding);
  13297. }
  13298. ret = wc_RsaDirect(out, outSz, NULL, &plainSz, key, RSA_PUBLIC_DECRYPT,
  13299. &rng);
  13300. if (ret != LENGTH_ONLY_E || plainSz != inLen) {
  13301. ERROR_OUT(-7817, exit_rsa_nopadding);
  13302. }
  13303. ret = wc_RsaDirect(out, outSz - 10, plain, &plainSz, key,
  13304. RSA_PUBLIC_DECRYPT, &rng);
  13305. if (ret != BAD_FUNC_ARG) {
  13306. ERROR_OUT(-7818, exit_rsa_nopadding);
  13307. }
  13308. /* if making it to this point of code without hitting an ERROR_OUT then
  13309. * all tests have passed */
  13310. ret = 0;
  13311. exit_rsa_nopadding:
  13312. wc_FreeRsaKey(key);
  13313. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13314. WC_FREE_VAR(key, HEAP_HINT);
  13315. WC_FREE_VAR(out, HEAP_HINT);
  13316. WC_FREE_VAR(plain, HEAP_HINT);
  13317. wc_FreeRng(&rng);
  13318. return ret;
  13319. }
  13320. #endif /* WC_RSA_NO_PADDING */
  13321. #if defined(WOLFSSL_HAVE_SP_RSA) && defined(USE_FAST_MATH)
  13322. static int rsa_even_mod_test(WC_RNG* rng, RsaKey* key)
  13323. {
  13324. byte* tmp = NULL;
  13325. size_t bytes;
  13326. int ret;
  13327. word32 inLen = 0;
  13328. #ifndef NO_ASN
  13329. word32 idx = 0;
  13330. #endif
  13331. word32 outSz = RSA_TEST_BYTES;
  13332. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  13333. word32 plainSz = RSA_TEST_BYTES;
  13334. #endif
  13335. #if !defined(USE_CERT_BUFFERS_2048) && !defined(USE_CERT_BUFFERS_3072) && \
  13336. !defined(USE_CERT_BUFFERS_4096) && !defined(NO_FILESYSTEM)
  13337. XFILE file;
  13338. #endif
  13339. WC_DECLARE_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
  13340. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  13341. WC_DECLARE_VAR(plain, byte, RSA_TEST_BYTES, HEAP_HINT);
  13342. #endif
  13343. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  13344. if (out == NULL
  13345. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  13346. || plain == NULL
  13347. #endif
  13348. ) {
  13349. ERROR_OUT(MEMORY_E, exit_rsa_even_mod);
  13350. }
  13351. #endif
  13352. #if defined(USE_CERT_BUFFERS_2048)
  13353. bytes = (size_t)sizeof_client_key_der_2048;
  13354. if (bytes < (size_t)sizeof_client_cert_der_2048)
  13355. bytes = (size_t)sizeof_client_cert_der_2048;
  13356. #else
  13357. bytes = FOURK_BUF;
  13358. #endif
  13359. tmp = (byte*)XMALLOC(bytes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13360. if (tmp == NULL
  13361. #ifdef WOLFSSL_ASYNC_CRYPT
  13362. || out == NULL || plain == NULL
  13363. #endif
  13364. ) {
  13365. ERROR_OUT(-7800, exit_rsa_even_mod);
  13366. }
  13367. #if defined(USE_CERT_BUFFERS_2048)
  13368. XMEMCPY(tmp, client_key_der_2048, (size_t)sizeof_client_key_der_2048);
  13369. #elif defined(USE_CERT_BUFFERS_3072)
  13370. XMEMCPY(tmp, client_key_der_3072, (size_t)sizeof_client_key_der_3072);
  13371. #elif defined(USE_CERT_BUFFERS_4096)
  13372. XMEMCPY(tmp, client_key_der_4096, (size_t)sizeof_client_key_der_4096);
  13373. #elif !defined(NO_FILESYSTEM)
  13374. file = XFOPEN(clientKey, "rb");
  13375. if (!file) {
  13376. err_sys("can't open ./certs/client-key.der, "
  13377. "Please run from wolfSSL home dir", -40);
  13378. ERROR_OUT(-7801, exit_rsa_even_mod);
  13379. }
  13380. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  13381. XFCLOSE(file);
  13382. #else
  13383. /* No key to use. */
  13384. ERROR_OUT(-7802, exit_rsa_even_mod);
  13385. #endif /* USE_CERT_BUFFERS */
  13386. #ifndef NO_ASN
  13387. ret = wc_RsaPrivateKeyDecode(tmp, &idx, key, (word32)bytes);
  13388. if (ret != 0) {
  13389. ERROR_OUT(-7804, exit_rsa_even_mod);
  13390. }
  13391. #else
  13392. #ifdef USE_CERT_BUFFERS_2048
  13393. ret = mp_read_unsigned_bin(&key->n, &tmp[12], 256);
  13394. if (ret != 0) {
  13395. ERROR_OUT(-7804, exit_rsa_even_mod);
  13396. }
  13397. ret = mp_set_int(&key->e, WC_RSA_EXPONENT);
  13398. if (ret != 0) {
  13399. ERROR_OUT(-7804, exit_rsa_even_mod);
  13400. }
  13401. #ifndef NO_SIG_WRAPPER
  13402. modLen = 2048;
  13403. #endif
  13404. #else
  13405. #error Not supported yet!
  13406. #endif
  13407. #endif
  13408. key->n.dp[0] &= (mp_digit)-2;
  13409. if (ret != 0) {
  13410. ERROR_OUT(-7804, exit_rsa_even_mod);
  13411. }
  13412. /* after loading in key use tmp as the test buffer */
  13413. #if !(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION == 2) && \
  13414. (defined(WOLFSSL_SP_ARM64_ASM) || defined(WOLFSSL_SP_ARM32_ASM))) && \
  13415. !defined(WOLFSSL_XILINX_CRYPT)
  13416. /* The ARM64_ASM code that was FIPS validated did not return these expected
  13417. * failure codes. These tests cases were added after the assembly was
  13418. * in-lined in the module and validated, these tests will be available in
  13419. * the 140-3 module */
  13420. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_PUBLIC_ONLY)
  13421. inLen = 32;
  13422. outSz = wc_RsaEncryptSize(key);
  13423. XMEMSET(tmp, 7, plainSz);
  13424. ret = wc_RsaSSL_Sign(tmp, inLen, out, outSz, key, rng);
  13425. if (ret != MP_VAL && ret != MP_EXPTMOD_E && ret != MP_INVMOD_E) {
  13426. ERROR_OUT(-7806, exit_rsa_even_mod);
  13427. }
  13428. ret = wc_RsaSSL_Verify(out, outSz, tmp, inLen, key);
  13429. if (ret != MP_VAL && ret != MP_EXPTMOD_E) {
  13430. ERROR_OUT(-7808, exit_rsa_even_mod);
  13431. }
  13432. #endif
  13433. #ifdef WC_RSA_BLINDING
  13434. ret = wc_RsaSetRNG(key, rng);
  13435. if (ret < 0) {
  13436. ERROR_OUT(-7811, exit_rsa_even_mod);
  13437. }
  13438. #endif
  13439. /* test encrypt and decrypt using WC_RSA_NO_PAD */
  13440. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_PUBLIC_ONLY)
  13441. ret = wc_RsaPublicEncrypt(tmp, inLen, out, (int)outSz, key, rng);
  13442. if (ret != MP_VAL && ret != MP_EXPTMOD_E) {
  13443. ERROR_OUT(-7812, exit_rsa_even_mod);
  13444. }
  13445. #endif /* WOLFSSL_RSA_VERIFY_ONLY */
  13446. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  13447. ret = wc_RsaPrivateDecrypt(out, outSz, plain, (int)plainSz, key);
  13448. if (ret != MP_VAL && ret != MP_EXPTMOD_E && ret != MP_INVMOD_E) {
  13449. ERROR_OUT(-7813, exit_rsa_even_mod);
  13450. }
  13451. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  13452. #endif /* !(HAVE_FIPS_VERSION == 2 && WOLFSSL_SP_ARMxx_ASM) */
  13453. /* if making it to this point of code without hitting an ERROR_OUT then
  13454. * all tests have passed */
  13455. ret = 0;
  13456. exit_rsa_even_mod:
  13457. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13458. WC_FREE_VAR(out, HEAP_HINT);
  13459. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  13460. WC_FREE_VAR(plain, HEAP_HINT);
  13461. #endif
  13462. (void)out;
  13463. (void)outSz;
  13464. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  13465. (void)plain;
  13466. (void)plainSz;
  13467. #endif
  13468. (void)inLen;
  13469. (void)rng;
  13470. return ret;
  13471. }
  13472. #endif /* WOLFSSL_HAVE_SP_RSA */
  13473. #ifdef WOLFSSL_CERT_GEN
  13474. static int rsa_certgen_test(RsaKey* key, RsaKey* keypub, WC_RNG* rng, byte* tmp)
  13475. {
  13476. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  13477. RsaKey *caKey = (RsaKey *)XMALLOC(sizeof *caKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13478. #ifdef WOLFSSL_TEST_CERT
  13479. DecodedCert *decode = (DecodedCert *)XMALLOC(sizeof *decode, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13480. #endif
  13481. #else
  13482. RsaKey caKey[1];
  13483. #ifdef WOLFSSL_TEST_CERT
  13484. DecodedCert decode[1];
  13485. #endif
  13486. #endif
  13487. byte* der = NULL;
  13488. int ret;
  13489. Cert* myCert = NULL;
  13490. int certSz;
  13491. size_t bytes3;
  13492. word32 idx3 = 0;
  13493. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  13494. XFILE file3;
  13495. #endif
  13496. #if defined(WOLFSSL_ALT_NAMES) && !defined(NO_ASN_TIME)
  13497. struct tm beforeTime;
  13498. struct tm afterTime;
  13499. #endif
  13500. const byte mySerial[8] = {1,2,3,4,5,6,7,8};
  13501. (void)keypub;
  13502. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  13503. if (caKey == NULL)
  13504. ERROR_OUT(MEMORY_E, exit_rsa);
  13505. #ifdef WOLFSSL_TEST_CERT
  13506. if (decode == NULL)
  13507. ERROR_OUT(MEMORY_E, exit_rsa);
  13508. #endif
  13509. #endif
  13510. XMEMSET(caKey, 0, sizeof *caKey);
  13511. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13512. if (der == NULL) {
  13513. ERROR_OUT(-7820, exit_rsa);
  13514. }
  13515. myCert = (Cert*)XMALLOC(sizeof(Cert), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13516. if (myCert == NULL) {
  13517. ERROR_OUT(-7821, exit_rsa);
  13518. }
  13519. /* self signed */
  13520. if (wc_InitCert_ex(myCert, HEAP_HINT, devId)) {
  13521. ERROR_OUT(-7822, exit_rsa);
  13522. }
  13523. XMEMCPY(&myCert->subject, &certDefaultName, sizeof(CertName));
  13524. XMEMCPY(myCert->serial, mySerial, sizeof(mySerial));
  13525. myCert->serialSz = (int)sizeof(mySerial);
  13526. myCert->isCA = 1;
  13527. #ifndef NO_SHA256
  13528. myCert->sigType = CTC_SHA256wRSA;
  13529. #else
  13530. myCert->sigType = CTC_SHAwRSA;
  13531. #endif
  13532. #ifdef WOLFSSL_CERT_EXT
  13533. /* add Policies */
  13534. XSTRNCPY(myCert->certPolicies[0], "2.16.840.1.101.3.4.1.42",
  13535. CTC_MAX_CERTPOL_SZ);
  13536. XSTRNCPY(myCert->certPolicies[1], "1.2.840.113549.1.9.16.6.5",
  13537. CTC_MAX_CERTPOL_SZ);
  13538. myCert->certPoliciesNb = 2;
  13539. /* add SKID from the Public Key */
  13540. if (wc_SetSubjectKeyIdFromPublicKey(myCert, keypub, NULL) != 0) {
  13541. ERROR_OUT(-7823, exit_rsa);
  13542. }
  13543. /* add AKID from the Public Key */
  13544. if (wc_SetAuthKeyIdFromPublicKey(myCert, keypub, NULL) != 0) {
  13545. ERROR_OUT(-7824, exit_rsa);
  13546. }
  13547. /* add Key Usage */
  13548. if (wc_SetKeyUsage(myCert,"cRLSign,keyCertSign") != 0) {
  13549. ERROR_OUT(-7825, exit_rsa);
  13550. }
  13551. #ifdef WOLFSSL_EKU_OID
  13552. {
  13553. const char unique[] = "2.16.840.1.111111.100.1.10.1";
  13554. if (wc_SetExtKeyUsageOID(myCert, unique, sizeof(unique), 0,
  13555. HEAP_HINT) != 0) {
  13556. ERROR_OUT(-7826, exit_rsa);
  13557. }
  13558. }
  13559. #endif /* WOLFSSL_EKU_OID */
  13560. #endif /* WOLFSSL_CERT_EXT */
  13561. ret = 0;
  13562. do {
  13563. #if defined(WOLFSSL_ASYNC_CRYPT)
  13564. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13565. #endif
  13566. if (ret >= 0) {
  13567. ret = wc_MakeSelfCert(myCert, der, FOURK_BUF, key, rng);
  13568. }
  13569. } while (ret == WC_PENDING_E);
  13570. if (ret < 0) {
  13571. ERROR_OUT(-7827, exit_rsa);
  13572. }
  13573. certSz = ret;
  13574. #ifdef WOLFSSL_TEST_CERT
  13575. InitDecodedCert(decode, der, certSz, HEAP_HINT);
  13576. ret = ParseCert(decode, CERT_TYPE, NO_VERIFY, 0);
  13577. if (ret != 0) {
  13578. FreeDecodedCert(decode);
  13579. ERROR_OUT(-7828, exit_rsa);
  13580. }
  13581. FreeDecodedCert(decode);
  13582. #endif
  13583. ret = SaveDerAndPem(der, certSz, certDerFile, certPemFile,
  13584. CERT_TYPE, -5578);
  13585. if (ret != 0) {
  13586. goto exit_rsa;
  13587. }
  13588. /* Setup Certificate */
  13589. if (wc_InitCert_ex(myCert, HEAP_HINT, devId)) {
  13590. ERROR_OUT(-7829, exit_rsa);
  13591. }
  13592. #ifdef WOLFSSL_ALT_NAMES
  13593. /* Get CA Cert for testing */
  13594. #ifdef USE_CERT_BUFFERS_1024
  13595. XMEMCPY(tmp, ca_cert_der_1024, sizeof_ca_cert_der_1024);
  13596. bytes3 = sizeof_ca_cert_der_1024;
  13597. #elif defined(USE_CERT_BUFFERS_2048)
  13598. XMEMCPY(tmp, ca_cert_der_2048, sizeof_ca_cert_der_2048);
  13599. bytes3 = sizeof_ca_cert_der_2048;
  13600. #else
  13601. file3 = XFOPEN(rsaCaCertDerFile, "rb");
  13602. if (!file3) {
  13603. ERROR_OUT(-7830, exit_rsa);
  13604. }
  13605. bytes3 = XFREAD(tmp, 1, FOURK_BUF, file3);
  13606. XFCLOSE(file3);
  13607. #endif /* USE_CERT_BUFFERS */
  13608. #if !defined(NO_FILESYSTEM) && !defined(USE_CERT_BUFFERS_1024) && \
  13609. !defined(USE_CERT_BUFFERS_2048) && !defined(NO_ASN)
  13610. ret = wc_SetAltNames(myCert, rsaCaCertFile);
  13611. if (ret != 0) {
  13612. ERROR_OUT(-7831, exit_rsa);
  13613. }
  13614. #endif
  13615. /* get alt names from der */
  13616. ret = wc_SetAltNamesBuffer(myCert, tmp, (int)bytes3);
  13617. if (ret != 0) {
  13618. ERROR_OUT(-7832, exit_rsa);
  13619. }
  13620. /* get dates from der */
  13621. ret = wc_SetDatesBuffer(myCert, tmp, (int)bytes3);
  13622. if (ret != 0) {
  13623. ERROR_OUT(-7833, exit_rsa);
  13624. }
  13625. #ifndef NO_ASN_TIME
  13626. ret = wc_GetCertDates(myCert, &beforeTime, &afterTime);
  13627. if (ret < 0) {
  13628. ERROR_OUT(-7834, exit_rsa);
  13629. }
  13630. #endif
  13631. #endif /* WOLFSSL_ALT_NAMES */
  13632. /* Get CA Key */
  13633. #ifdef USE_CERT_BUFFERS_1024
  13634. XMEMCPY(tmp, ca_key_der_1024, sizeof_ca_key_der_1024);
  13635. bytes3 = sizeof_ca_key_der_1024;
  13636. #elif defined(USE_CERT_BUFFERS_2048)
  13637. XMEMCPY(tmp, ca_key_der_2048, sizeof_ca_key_der_2048);
  13638. bytes3 = sizeof_ca_key_der_2048;
  13639. #else
  13640. file3 = XFOPEN(rsaCaKeyFile, "rb");
  13641. if (!file3) {
  13642. ERROR_OUT(-7835, exit_rsa);
  13643. }
  13644. bytes3 = XFREAD(tmp, 1, FOURK_BUF, file3);
  13645. XFCLOSE(file3);
  13646. #endif /* USE_CERT_BUFFERS */
  13647. ret = wc_InitRsaKey(caKey, HEAP_HINT);
  13648. if (ret != 0) {
  13649. ERROR_OUT(-7836, exit_rsa);
  13650. }
  13651. ret = wc_RsaPrivateKeyDecode(tmp, &idx3, caKey, (word32)bytes3);
  13652. if (ret != 0) {
  13653. ERROR_OUT(-7837, exit_rsa);
  13654. }
  13655. #ifndef NO_SHA256
  13656. myCert->sigType = CTC_SHA256wRSA;
  13657. #else
  13658. myCert->sigType = CTC_SHAwRSA;
  13659. #endif
  13660. XMEMCPY(&myCert->subject, &certDefaultName, sizeof(CertName));
  13661. #ifdef WOLFSSL_CERT_EXT
  13662. /* add Policies */
  13663. XSTRNCPY(myCert->certPolicies[0], "2.16.840.1.101.3.4.1.42",
  13664. CTC_MAX_CERTPOL_SZ);
  13665. myCert->certPoliciesNb =1;
  13666. /* add SKID from the Public Key */
  13667. if (wc_SetSubjectKeyIdFromPublicKey(myCert, key, NULL) != 0) {
  13668. ERROR_OUT(-7838, exit_rsa);
  13669. }
  13670. /* add AKID from the CA certificate */
  13671. #if defined(USE_CERT_BUFFERS_2048)
  13672. ret = wc_SetAuthKeyIdFromCert(myCert, ca_cert_der_2048,
  13673. sizeof_ca_cert_der_2048);
  13674. #elif defined(USE_CERT_BUFFERS_1024)
  13675. ret = wc_SetAuthKeyIdFromCert(myCert, ca_cert_der_1024,
  13676. sizeof_ca_cert_der_1024);
  13677. #else
  13678. ret = wc_SetAuthKeyId(myCert, rsaCaCertFile);
  13679. #endif
  13680. if (ret != 0) {
  13681. ERROR_OUT(-7839, exit_rsa);
  13682. }
  13683. /* add Key Usage */
  13684. if (wc_SetKeyUsage(myCert,"keyEncipherment,keyAgreement") != 0) {
  13685. ERROR_OUT(-7840, exit_rsa);
  13686. }
  13687. #endif /* WOLFSSL_CERT_EXT */
  13688. #if defined(USE_CERT_BUFFERS_2048)
  13689. ret = wc_SetIssuerBuffer(myCert, ca_cert_der_2048,
  13690. sizeof_ca_cert_der_2048);
  13691. #elif defined(USE_CERT_BUFFERS_1024)
  13692. ret = wc_SetIssuerBuffer(myCert, ca_cert_der_1024,
  13693. sizeof_ca_cert_der_1024);
  13694. #else
  13695. ret = wc_SetIssuer(myCert, rsaCaCertFile);
  13696. #endif
  13697. if (ret < 0) {
  13698. ERROR_OUT(-7841, exit_rsa);
  13699. }
  13700. certSz = wc_MakeCert(myCert, der, FOURK_BUF, key, NULL, rng);
  13701. if (certSz < 0) {
  13702. ERROR_OUT(-7842, exit_rsa);
  13703. }
  13704. ret = 0;
  13705. do {
  13706. #if defined(WOLFSSL_ASYNC_CRYPT)
  13707. ret = wc_AsyncWait(ret, &caKey->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13708. #endif
  13709. if (ret >= 0) {
  13710. ret = wc_SignCert(myCert->bodySz, myCert->sigType, der, FOURK_BUF,
  13711. caKey, NULL, rng);
  13712. }
  13713. } while (ret == WC_PENDING_E);
  13714. if (ret < 0) {
  13715. ERROR_OUT(-7843, exit_rsa);
  13716. }
  13717. certSz = ret;
  13718. #ifdef WOLFSSL_TEST_CERT
  13719. InitDecodedCert(decode, der, certSz, HEAP_HINT);
  13720. ret = ParseCert(decode, CERT_TYPE, NO_VERIFY, 0);
  13721. if (ret != 0) {
  13722. FreeDecodedCert(decode);
  13723. ERROR_OUT(-7844, exit_rsa);
  13724. }
  13725. FreeDecodedCert(decode);
  13726. #endif
  13727. ret = SaveDerAndPem(der, certSz, otherCertDerFile, otherCertPemFile,
  13728. CERT_TYPE, -5598);
  13729. if (ret != 0) {
  13730. goto exit_rsa;
  13731. }
  13732. exit_rsa:
  13733. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  13734. if (caKey != NULL) {
  13735. wc_FreeRsaKey(caKey);
  13736. XFREE(caKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13737. }
  13738. #ifdef WOLFSSL_TEST_CERT
  13739. if (decode != NULL)
  13740. XFREE(decode, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13741. #endif
  13742. #else
  13743. wc_FreeRsaKey(caKey);
  13744. #endif
  13745. XFREE(myCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13746. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13747. return ret;
  13748. }
  13749. #endif
  13750. #if !defined(NO_RSA) && defined(HAVE_ECC) && defined(WOLFSSL_CERT_GEN)
  13751. /* Make Cert / Sign example for ECC cert and RSA CA */
  13752. static int rsa_ecc_certgen_test(WC_RNG* rng, byte* tmp)
  13753. {
  13754. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  13755. RsaKey *caKey = (RsaKey *)XMALLOC(sizeof *caKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13756. ecc_key *caEccKey = (ecc_key *)XMALLOC(sizeof *caEccKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13757. ecc_key *caEccKeyPub = (ecc_key *)XMALLOC(sizeof *caEccKeyPub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13758. #ifdef WOLFSSL_TEST_CERT
  13759. DecodedCert *decode = (DecodedCert *)XMALLOC(sizeof *decode, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13760. #endif
  13761. #else
  13762. RsaKey caKey[1];
  13763. ecc_key caEccKey[1];
  13764. ecc_key caEccKeyPub[1];
  13765. #ifdef WOLFSSL_TEST_CERT
  13766. DecodedCert decode[1];
  13767. #endif
  13768. #endif
  13769. byte* der = NULL;
  13770. Cert* myCert = NULL;
  13771. int certSz;
  13772. size_t bytes3;
  13773. word32 idx3 = 0;
  13774. #if (!defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)) \
  13775. || !defined(USE_CERT_BUFFERS_256)
  13776. XFILE file3;
  13777. #endif
  13778. int ret;
  13779. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  13780. if ((caKey == NULL) || (caEccKey == NULL) || (caEccKeyPub == NULL)
  13781. #ifdef WOLFSSL_TEST_CERT
  13782. || (decode == NULL)
  13783. #endif
  13784. )
  13785. ERROR_OUT(MEMORY_E, exit_rsa);
  13786. #endif
  13787. XMEMSET(caKey, 0, sizeof *caKey);
  13788. XMEMSET(caEccKey, 0, sizeof *caEccKey);
  13789. XMEMSET(caEccKeyPub, 0, sizeof *caEccKeyPub);
  13790. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13791. if (der == NULL) {
  13792. ERROR_OUT(-7850, exit_rsa);
  13793. }
  13794. myCert = (Cert*)XMALLOC(sizeof(Cert), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13795. if (myCert == NULL) {
  13796. ERROR_OUT(-7851, exit_rsa);
  13797. }
  13798. /* Get CA Key */
  13799. #ifdef USE_CERT_BUFFERS_1024
  13800. XMEMCPY(tmp, ca_key_der_1024, sizeof_ca_key_der_1024);
  13801. bytes3 = sizeof_ca_key_der_1024;
  13802. #elif defined(USE_CERT_BUFFERS_2048)
  13803. XMEMCPY(tmp, ca_key_der_2048, sizeof_ca_key_der_2048);
  13804. bytes3 = sizeof_ca_key_der_2048;
  13805. #else
  13806. file3 = XFOPEN(rsaCaKeyFile, "rb");
  13807. if (!file3) {
  13808. ERROR_OUT(-7852, exit_rsa);
  13809. }
  13810. bytes3 = XFREAD(tmp, 1, FOURK_BUF, file3);
  13811. XFCLOSE(file3);
  13812. #endif /* USE_CERT_BUFFERS */
  13813. ret = wc_InitRsaKey(caKey, HEAP_HINT);
  13814. if (ret != 0) {
  13815. ERROR_OUT(-7853, exit_rsa);
  13816. }
  13817. ret = wc_RsaPrivateKeyDecode(tmp, &idx3, caKey, (word32)bytes3);
  13818. if (ret != 0) {
  13819. ERROR_OUT(-7854, exit_rsa);
  13820. }
  13821. /* Get Cert Key */
  13822. #ifdef USE_CERT_BUFFERS_256
  13823. XMEMCPY(tmp, ecc_key_pub_der_256, sizeof_ecc_key_pub_der_256);
  13824. bytes3 = sizeof_ecc_key_pub_der_256;
  13825. #else
  13826. file3 = XFOPEN(eccKeyPubFileDer, "rb");
  13827. if (!file3) {
  13828. ERROR_OUT(-7855, exit_rsa);
  13829. }
  13830. bytes3 = XFREAD(tmp, 1, FOURK_BUF, file3);
  13831. XFCLOSE(file3);
  13832. #endif
  13833. ret = wc_ecc_init_ex(caEccKeyPub, HEAP_HINT, devId);
  13834. if (ret != 0) {
  13835. ERROR_OUT(-7856, exit_rsa);
  13836. }
  13837. idx3 = 0;
  13838. ret = wc_EccPublicKeyDecode(tmp, &idx3, caEccKeyPub, (word32)bytes3);
  13839. if (ret != 0) {
  13840. ERROR_OUT(-7857, exit_rsa);
  13841. }
  13842. /* Setup Certificate */
  13843. if (wc_InitCert_ex(myCert, HEAP_HINT, devId)) {
  13844. ERROR_OUT(-7858, exit_rsa);
  13845. }
  13846. #ifndef NO_SHA256
  13847. myCert->sigType = CTC_SHA256wRSA;
  13848. #else
  13849. myCert->sigType = CTC_SHAwRSA;
  13850. #endif
  13851. XMEMCPY(&myCert->subject, &certDefaultName, sizeof(CertName));
  13852. #ifdef WOLFSSL_CERT_EXT
  13853. /* add Policies */
  13854. XSTRNCPY(myCert->certPolicies[0], "2.4.589440.587.101.2.1.9632587.1",
  13855. CTC_MAX_CERTPOL_SZ);
  13856. XSTRNCPY(myCert->certPolicies[1], "1.2.13025.489.1.113549",
  13857. CTC_MAX_CERTPOL_SZ);
  13858. myCert->certPoliciesNb = 2;
  13859. /* add SKID from the Public Key */
  13860. if (wc_SetSubjectKeyIdFromPublicKey(myCert, NULL, caEccKeyPub) != 0) {
  13861. ERROR_OUT(-7859, exit_rsa);
  13862. }
  13863. /* add AKID from the CA certificate */
  13864. #if defined(USE_CERT_BUFFERS_2048)
  13865. ret = wc_SetAuthKeyIdFromCert(myCert, ca_cert_der_2048,
  13866. sizeof_ca_cert_der_2048);
  13867. #elif defined(USE_CERT_BUFFERS_1024)
  13868. ret = wc_SetAuthKeyIdFromCert(myCert, ca_cert_der_1024,
  13869. sizeof_ca_cert_der_1024);
  13870. #else
  13871. ret = wc_SetAuthKeyId(myCert, rsaCaCertFile);
  13872. #endif
  13873. if (ret != 0) {
  13874. ERROR_OUT(-7860, exit_rsa);
  13875. }
  13876. /* add Key Usage */
  13877. if (wc_SetKeyUsage(myCert, certKeyUsage) != 0) {
  13878. ERROR_OUT(-7861, exit_rsa);
  13879. }
  13880. #endif /* WOLFSSL_CERT_EXT */
  13881. #if defined(USE_CERT_BUFFERS_2048)
  13882. ret = wc_SetIssuerBuffer(myCert, ca_cert_der_2048,
  13883. sizeof_ca_cert_der_2048);
  13884. #elif defined(USE_CERT_BUFFERS_1024)
  13885. ret = wc_SetIssuerBuffer(myCert, ca_cert_der_1024,
  13886. sizeof_ca_cert_der_1024);
  13887. #else
  13888. ret = wc_SetIssuer(myCert, rsaCaCertFile);
  13889. #endif
  13890. if (ret < 0) {
  13891. ERROR_OUT(-7862, exit_rsa);
  13892. }
  13893. certSz = wc_MakeCert(myCert, der, FOURK_BUF, NULL, caEccKeyPub, rng);
  13894. if (certSz < 0) {
  13895. ERROR_OUT(-7863, exit_rsa);
  13896. }
  13897. ret = 0;
  13898. do {
  13899. #if defined(WOLFSSL_ASYNC_CRYPT)
  13900. ret = wc_AsyncWait(ret, &caEccKey->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13901. #endif
  13902. if (ret >= 0) {
  13903. ret = wc_SignCert(myCert->bodySz, myCert->sigType, der,
  13904. FOURK_BUF, caKey, NULL, rng);
  13905. }
  13906. } while (ret == WC_PENDING_E);
  13907. if (ret < 0) {
  13908. ERROR_OUT(-7864, exit_rsa);
  13909. }
  13910. certSz = ret;
  13911. #ifdef WOLFSSL_TEST_CERT
  13912. InitDecodedCert(decode, der, certSz, 0);
  13913. ret = ParseCert(decode, CERT_TYPE, NO_VERIFY, 0);
  13914. if (ret != 0) {
  13915. FreeDecodedCert(decode);
  13916. ERROR_OUT(-7865, exit_rsa);
  13917. }
  13918. FreeDecodedCert(decode);
  13919. #endif
  13920. ret = SaveDerAndPem(der, certSz, certEccRsaDerFile, certEccRsaPemFile,
  13921. CERT_TYPE, -5616);
  13922. if (ret != 0) {
  13923. goto exit_rsa;
  13924. }
  13925. exit_rsa:
  13926. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  13927. if (caKey != NULL) {
  13928. wc_FreeRsaKey(caKey);
  13929. XFREE(caKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13930. }
  13931. if (caEccKey != NULL) {
  13932. wc_ecc_free(caEccKey);
  13933. XFREE(caEccKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13934. }
  13935. if (caEccKeyPub != NULL) {
  13936. wc_ecc_free(caEccKeyPub);
  13937. XFREE(caEccKeyPub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13938. }
  13939. #ifdef WOLFSSL_TEST_CERT
  13940. if (decode != NULL)
  13941. XFREE(decode, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13942. #endif
  13943. #else
  13944. wc_FreeRsaKey(caKey);
  13945. wc_ecc_free(caEccKey);
  13946. wc_ecc_free(caEccKeyPub);
  13947. #endif
  13948. XFREE(myCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13949. myCert = NULL;
  13950. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13951. der = NULL;
  13952. if (ret >= 0)
  13953. ret = 0;
  13954. return ret;
  13955. }
  13956. #endif /* !NO_RSA && HAVE_ECC && WOLFSSL_CERT_GEN */
  13957. #ifdef WOLFSSL_KEY_GEN
  13958. static int rsa_keygen_test(WC_RNG* rng)
  13959. {
  13960. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  13961. RsaKey *genKey = (RsaKey *)XMALLOC(sizeof *genKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13962. #else
  13963. RsaKey genKey[1];
  13964. #endif
  13965. int ret;
  13966. byte* der = NULL;
  13967. #ifndef WOLFSSL_CRYPTOCELL
  13968. word32 idx = 0;
  13969. #endif
  13970. int derSz = 0;
  13971. #if !defined(WOLFSSL_SP_MATH) && !defined(HAVE_FIPS)
  13972. int keySz = 1024;
  13973. #else
  13974. int keySz = 2048;
  13975. #endif
  13976. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  13977. if (! genKey)
  13978. ERROR_OUT(MEMORY_E, exit_rsa);
  13979. #endif
  13980. XMEMSET(genKey, 0, sizeof *genKey);
  13981. ret = wc_InitRsaKey_ex(genKey, HEAP_HINT, devId);
  13982. if (ret != 0) {
  13983. ERROR_OUT(-7870, exit_rsa);
  13984. }
  13985. #ifdef HAVE_FIPS
  13986. for (;;) {
  13987. #endif
  13988. ret = wc_MakeRsaKey(genKey, keySz, WC_RSA_EXPONENT, rng);
  13989. #if defined(WOLFSSL_ASYNC_CRYPT)
  13990. ret = wc_AsyncWait(ret, &genKey->asyncDev, WC_ASYNC_FLAG_NONE);
  13991. #endif
  13992. #ifdef HAVE_FIPS
  13993. if (ret == PRIME_GEN_E)
  13994. continue;
  13995. break;
  13996. }
  13997. #endif
  13998. if (ret != 0) {
  13999. ERROR_OUT(-7871, exit_rsa);
  14000. }
  14001. TEST_SLEEP();
  14002. #ifdef WOLFSSL_RSA_KEY_CHECK
  14003. ret = wc_CheckRsaKey(genKey);
  14004. if (ret != 0) {
  14005. ERROR_OUT(-7872, exit_rsa);
  14006. }
  14007. #endif
  14008. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14009. if (der == NULL) {
  14010. ERROR_OUT(-7873, exit_rsa);
  14011. }
  14012. derSz = wc_RsaKeyToDer(genKey, der, FOURK_BUF);
  14013. if (derSz < 0) {
  14014. ERROR_OUT(-7874, exit_rsa);
  14015. }
  14016. ret = SaveDerAndPem(der, derSz, keyDerFile, keyPemFile,
  14017. PRIVATEKEY_TYPE, -5555);
  14018. if (ret != 0) {
  14019. goto exit_rsa;
  14020. }
  14021. wc_FreeRsaKey(genKey);
  14022. ret = wc_InitRsaKey(genKey, HEAP_HINT);
  14023. if (ret != 0) {
  14024. ERROR_OUT(-7875, exit_rsa);
  14025. }
  14026. #ifndef WOLFSSL_CRYPTOCELL
  14027. idx = 0;
  14028. /* The private key part of the key gen pairs from cryptocell can't be exported */
  14029. ret = wc_RsaPrivateKeyDecode(der, &idx, genKey, derSz);
  14030. if (ret != 0) {
  14031. ERROR_OUT(-7876, exit_rsa);
  14032. }
  14033. #endif /* WOLFSSL_CRYPTOCELL */
  14034. exit_rsa:
  14035. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  14036. if (genKey) {
  14037. wc_FreeRsaKey(genKey);
  14038. XFREE(genKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14039. }
  14040. #else
  14041. wc_FreeRsaKey(genKey);
  14042. #endif
  14043. if (der != NULL) {
  14044. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14045. der = NULL;
  14046. }
  14047. return ret;
  14048. }
  14049. #endif
  14050. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  14051. #if !defined(WC_NO_RSA_OAEP) && !defined(WC_NO_RNG) && \
  14052. !defined(HAVE_FAST_RSA) && !defined(HAVE_USER_RSA) && \
  14053. (!defined(HAVE_FIPS) || \
  14054. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))) \
  14055. && !defined(WOLF_CRYPTO_CB_ONLY_RSA)
  14056. static int rsa_oaep_padding_test(RsaKey* key, WC_RNG* rng)
  14057. {
  14058. int ret = 0;
  14059. word32 idx = 0;
  14060. const char inStr[] = TEST_STRING;
  14061. const word32 inLen = (word32)TEST_STRING_SZ;
  14062. const word32 outSz = RSA_TEST_BYTES;
  14063. const word32 plainSz = RSA_TEST_BYTES;
  14064. byte* res = NULL;
  14065. WC_DECLARE_VAR(in, byte, TEST_STRING_SZ, HEAP_HINT);
  14066. WC_DECLARE_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
  14067. WC_DECLARE_VAR(plain, byte, RSA_TEST_BYTES, HEAP_HINT);
  14068. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  14069. if (in == NULL || out == NULL || plain == NULL)
  14070. ERROR_OUT(MEMORY_E, exit_rsa);
  14071. #endif
  14072. XMEMCPY(in, inStr, inLen);
  14073. #ifndef NO_SHA
  14074. do {
  14075. #if defined(WOLFSSL_ASYNC_CRYPT)
  14076. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14077. #endif
  14078. if (ret >= 0) {
  14079. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, rng,
  14080. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA, WC_MGF1SHA1, NULL, 0);
  14081. }
  14082. } while (ret == WC_PENDING_E);
  14083. if (ret < 0) {
  14084. ERROR_OUT(-7918, exit_rsa);
  14085. }
  14086. TEST_SLEEP();
  14087. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  14088. idx = (word32)ret;
  14089. do {
  14090. #if defined(WOLFSSL_ASYNC_CRYPT)
  14091. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14092. #endif
  14093. if (ret >= 0) {
  14094. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  14095. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA, WC_MGF1SHA1, NULL, 0);
  14096. }
  14097. } while (ret == WC_PENDING_E);
  14098. if (ret < 0) {
  14099. ERROR_OUT(-7919, exit_rsa);
  14100. }
  14101. if (XMEMCMP(plain, in, inLen)) {
  14102. ERROR_OUT(-7920, exit_rsa);
  14103. }
  14104. TEST_SLEEP();
  14105. #endif /* NO_SHA */
  14106. #endif
  14107. #ifndef NO_SHA256
  14108. XMEMSET(plain, 0, plainSz);
  14109. do {
  14110. #if defined(WOLFSSL_ASYNC_CRYPT)
  14111. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14112. #endif
  14113. if (ret >= 0) {
  14114. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, rng,
  14115. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0);
  14116. }
  14117. } while (ret == WC_PENDING_E);
  14118. if (ret < 0) {
  14119. ERROR_OUT(-7921, exit_rsa);
  14120. }
  14121. TEST_SLEEP();
  14122. idx = (word32)ret;
  14123. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  14124. do {
  14125. #if defined(WOLFSSL_ASYNC_CRYPT)
  14126. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14127. #endif
  14128. if (ret >= 0) {
  14129. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  14130. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0);
  14131. }
  14132. } while (ret == WC_PENDING_E);
  14133. if (ret < 0) {
  14134. ERROR_OUT(-7922, exit_rsa);
  14135. }
  14136. if (XMEMCMP(plain, in, inLen)) {
  14137. ERROR_OUT(-7923, exit_rsa);
  14138. }
  14139. TEST_SLEEP();
  14140. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  14141. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  14142. do {
  14143. #if defined(WOLFSSL_ASYNC_CRYPT)
  14144. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14145. #endif
  14146. if (ret >= 0) {
  14147. ret = wc_RsaPrivateDecryptInline_ex(out, idx, &res, key,
  14148. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0);
  14149. }
  14150. } while (ret == WC_PENDING_E);
  14151. if (ret < 0) {
  14152. ERROR_OUT(-7924, exit_rsa);
  14153. }
  14154. if (ret != (int)inLen) {
  14155. ERROR_OUT(-7925, exit_rsa);
  14156. }
  14157. if (XMEMCMP(res, in, inLen)) {
  14158. ERROR_OUT(-7926, exit_rsa);
  14159. }
  14160. TEST_SLEEP();
  14161. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  14162. /* check fails if not using the same optional label */
  14163. XMEMSET(plain, 0, plainSz);
  14164. do {
  14165. #if defined(WOLFSSL_ASYNC_CRYPT)
  14166. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14167. #endif
  14168. if (ret >= 0) {
  14169. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, rng,
  14170. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0);
  14171. }
  14172. } while (ret == WC_PENDING_E);
  14173. if (ret < 0) {
  14174. ERROR_OUT(-7927, exit_rsa);
  14175. }
  14176. TEST_SLEEP();
  14177. /* TODO: investigate why Cavium Nitrox doesn't detect decrypt error here */
  14178. #if !defined(HAVE_CAVIUM) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
  14179. !defined(WOLFSSL_CRYPTOCELL) && !defined(WOLFSSL_SE050)
  14180. /* label is unused in cryptocell and SE050 so it won't detect decrypt error
  14181. * due to label */
  14182. idx = (word32)ret;
  14183. do {
  14184. #if defined(WOLFSSL_ASYNC_CRYPT)
  14185. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14186. #endif
  14187. if (ret >= 0) {
  14188. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  14189. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, in, inLen);
  14190. }
  14191. } while (ret == WC_PENDING_E);
  14192. if (ret > 0) { /* in this case decrypt should fail */
  14193. ERROR_OUT(-7928, exit_rsa);
  14194. }
  14195. ret = 0;
  14196. TEST_SLEEP();
  14197. #endif /* !HAVE_CAVIUM */
  14198. /* check using optional label with encrypt/decrypt */
  14199. XMEMSET(plain, 0, plainSz);
  14200. do {
  14201. #if defined(WOLFSSL_ASYNC_CRYPT)
  14202. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14203. #endif
  14204. if (ret >= 0) {
  14205. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, rng,
  14206. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, in, inLen);
  14207. }
  14208. } while (ret == WC_PENDING_E);
  14209. if (ret < 0) {
  14210. ERROR_OUT(-7929, exit_rsa);
  14211. }
  14212. TEST_SLEEP();
  14213. idx = (word32)ret;
  14214. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  14215. do {
  14216. #if defined(WOLFSSL_ASYNC_CRYPT)
  14217. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14218. #endif
  14219. if (ret >= 0) {
  14220. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  14221. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, in, inLen);
  14222. }
  14223. } while (ret == WC_PENDING_E);
  14224. if (ret < 0) {
  14225. ERROR_OUT(-7930, exit_rsa);
  14226. }
  14227. if (XMEMCMP(plain, in, inLen)) {
  14228. ERROR_OUT(-7931, exit_rsa);
  14229. }
  14230. TEST_SLEEP();
  14231. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  14232. #ifndef NO_SHA
  14233. /* check fail using mismatch hash algorithms */
  14234. XMEMSET(plain, 0, plainSz);
  14235. do {
  14236. #if defined(WOLFSSL_ASYNC_CRYPT)
  14237. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14238. #endif
  14239. if (ret >= 0) {
  14240. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, rng,
  14241. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA, WC_MGF1SHA1, in, inLen);
  14242. }
  14243. } while (ret == WC_PENDING_E);
  14244. if (ret < 0) {
  14245. ERROR_OUT(-7932, exit_rsa);
  14246. }
  14247. TEST_SLEEP();
  14248. /* TODO: investigate why Cavium Nitrox doesn't detect decrypt error here */
  14249. #if !defined(HAVE_CAVIUM) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
  14250. !defined(WOLFSSL_CRYPTOCELL) && !defined(WOLFSSL_SE050)
  14251. idx = (word32)ret;
  14252. do {
  14253. #if defined(WOLFSSL_ASYNC_CRYPT)
  14254. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14255. #endif
  14256. if (ret >= 0) {
  14257. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  14258. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256,
  14259. in, inLen);
  14260. }
  14261. } while (ret == WC_PENDING_E);
  14262. if (ret > 0) { /* should fail */
  14263. ERROR_OUT(-7933, exit_rsa);
  14264. }
  14265. ret = 0;
  14266. TEST_SLEEP();
  14267. #endif /* !HAVE_CAVIUM */
  14268. #endif /* NO_SHA */
  14269. #endif /* NO_SHA256 */
  14270. #ifdef WOLFSSL_SHA512
  14271. /* Check valid RSA key size is used while using hash length of SHA512
  14272. If key size is less than (hash length * 2) + 2 then is invalid use
  14273. and test, since OAEP padding requires this.
  14274. BAD_FUNC_ARG is returned when this case is not met */
  14275. if (wc_RsaEncryptSize(key) > ((int)WC_SHA512_DIGEST_SIZE * 2) + 2) {
  14276. XMEMSET(plain, 0, plainSz);
  14277. do {
  14278. #if defined(WOLFSSL_ASYNC_CRYPT)
  14279. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14280. #endif
  14281. if (ret >= 0) {
  14282. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, rng,
  14283. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA512, WC_MGF1SHA512, NULL, 0);
  14284. }
  14285. } while (ret == WC_PENDING_E);
  14286. if (ret < 0) {
  14287. ERROR_OUT(-7934, exit_rsa);
  14288. }
  14289. TEST_SLEEP();
  14290. idx = ret;
  14291. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  14292. do {
  14293. #if defined(WOLFSSL_ASYNC_CRYPT)
  14294. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14295. #endif
  14296. if (ret >= 0) {
  14297. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  14298. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA512, WC_MGF1SHA512, NULL, 0);
  14299. }
  14300. } while (ret == WC_PENDING_E);
  14301. if (ret < 0) {
  14302. ERROR_OUT(-7935, exit_rsa);
  14303. }
  14304. if (XMEMCMP(plain, in, inLen)) {
  14305. ERROR_OUT(-7936, exit_rsa);
  14306. }
  14307. TEST_SLEEP();
  14308. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  14309. }
  14310. #endif /* WOLFSSL_SHA512 */
  14311. /* check using pkcsv15 padding with _ex API */
  14312. XMEMSET(plain, 0, plainSz);
  14313. do {
  14314. #if defined(WOLFSSL_ASYNC_CRYPT)
  14315. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14316. #endif
  14317. if (ret >= 0) {
  14318. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, rng,
  14319. WC_RSA_PKCSV15_PAD, WC_HASH_TYPE_NONE, 0, NULL, 0);
  14320. }
  14321. } while (ret == WC_PENDING_E);
  14322. if (ret < 0) {
  14323. ERROR_OUT(-7937, exit_rsa);
  14324. }
  14325. TEST_SLEEP();
  14326. idx = (word32)ret;
  14327. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  14328. do {
  14329. #if defined(WOLFSSL_ASYNC_CRYPT)
  14330. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14331. #endif
  14332. if (ret >= 0) {
  14333. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  14334. WC_RSA_PKCSV15_PAD, WC_HASH_TYPE_NONE, 0, NULL, 0);
  14335. }
  14336. } while (ret == WC_PENDING_E);
  14337. if (ret < 0) {
  14338. ERROR_OUT(-7938, exit_rsa);
  14339. }
  14340. if (XMEMCMP(plain, in, inLen)) {
  14341. ERROR_OUT(-7939, exit_rsa);
  14342. }
  14343. TEST_SLEEP();
  14344. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  14345. exit_rsa:
  14346. WC_FREE_VAR(in, HEAP_HINT);
  14347. WC_FREE_VAR(out, HEAP_HINT);
  14348. WC_FREE_VAR(plain, HEAP_HINT);
  14349. (void)idx;
  14350. (void)inStr;
  14351. (void)res;
  14352. if (ret >= 0)
  14353. ret = 0;
  14354. return ret;
  14355. }
  14356. #endif
  14357. #endif
  14358. WOLFSSL_TEST_SUBROUTINE int rsa_test(void)
  14359. {
  14360. int ret;
  14361. size_t bytes;
  14362. WC_RNG rng;
  14363. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  14364. byte* tmp = NULL;
  14365. byte* der = NULL;
  14366. RsaKey *key = (RsaKey *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14367. #else
  14368. RsaKey key[1];
  14369. byte tmp[FOURK_BUF];
  14370. #endif
  14371. #if defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_CERT_GEN)
  14372. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  14373. RsaKey *keypub = (RsaKey *)XMALLOC(sizeof *keypub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14374. #else
  14375. RsaKey keypub[1];
  14376. #endif
  14377. #endif
  14378. word32 idx = 0;
  14379. const char inStr[] = TEST_STRING;
  14380. const word32 inLen = (word32)TEST_STRING_SZ;
  14381. const word32 outSz = RSA_TEST_BYTES;
  14382. const word32 plainSz = RSA_TEST_BYTES;
  14383. byte* res = NULL;
  14384. #ifndef NO_SIG_WRAPPER
  14385. int modLen;
  14386. #endif
  14387. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  14388. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096) && \
  14389. !defined(NO_FILESYSTEM)
  14390. XFILE file;
  14391. #ifdef WOLFSSL_TEST_CERT
  14392. XFILE file2;
  14393. #endif
  14394. #endif
  14395. #ifdef WOLFSSL_TEST_CERT
  14396. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  14397. DecodedCert *cert = (DecodedCert *)XMALLOC(sizeof *cert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14398. #else
  14399. DecodedCert cert[1];
  14400. #endif
  14401. #ifndef NO_ASN_TIME
  14402. struct tm timearg;
  14403. const byte* date;
  14404. byte dateFormat;
  14405. int dateLength;
  14406. #endif
  14407. #endif
  14408. WC_DECLARE_VAR(in, byte, TEST_STRING_SZ, HEAP_HINT);
  14409. WC_DECLARE_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
  14410. WC_DECLARE_VAR(plain, byte, RSA_TEST_BYTES, HEAP_HINT);
  14411. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  14412. if (in == NULL || out == NULL || plain == NULL)
  14413. ERROR_OUT(MEMORY_E, exit_rsa);
  14414. #endif
  14415. XMEMCPY(in, inStr, inLen);
  14416. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  14417. if (key == NULL)
  14418. ERROR_OUT(MEMORY_E, exit_rsa);
  14419. #if defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_CERT_GEN)
  14420. if (keypub == NULL)
  14421. ERROR_OUT(MEMORY_E, exit_rsa);
  14422. #endif
  14423. #ifdef WOLFSSL_TEST_CERT
  14424. if (cert == NULL)
  14425. ERROR_OUT(MEMORY_E, exit_rsa);
  14426. #endif
  14427. #endif /* WOLFSSL_SMALL_STACK && !WOLFSSL_NO_MALLOC */
  14428. /* initialize stack structures */
  14429. XMEMSET(&rng, 0, sizeof(rng));
  14430. XMEMSET(key, 0, sizeof *key);
  14431. #if defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_CERT_GEN)
  14432. XMEMSET(keypub, 0, sizeof *keypub);
  14433. #endif
  14434. #if !defined(HAVE_USER_RSA) && !defined(NO_ASN)
  14435. ret = rsa_decode_test(key);
  14436. if (ret != 0)
  14437. ERROR_OUT(ret, exit_rsa);
  14438. #endif
  14439. #ifdef USE_CERT_BUFFERS_1024
  14440. bytes = (size_t)sizeof_client_key_der_1024;
  14441. if (bytes < (size_t)sizeof_client_cert_der_1024)
  14442. bytes = (size_t)sizeof_client_cert_der_1024;
  14443. #elif defined(USE_CERT_BUFFERS_2048)
  14444. bytes = (size_t)sizeof_client_key_der_2048;
  14445. if (bytes < (size_t)sizeof_client_cert_der_2048)
  14446. bytes = (size_t)sizeof_client_cert_der_2048;
  14447. #elif defined(USE_CERT_BUFFERS_3072)
  14448. bytes = (size_t)sizeof_client_key_der_3072;
  14449. if (bytes < (size_t)sizeof_client_cert_der_3072)
  14450. bytes = (size_t)sizeof_client_cert_der_3072;
  14451. #elif defined(USE_CERT_BUFFERS_4096)
  14452. bytes = (size_t)sizeof_client_key_der_4096;
  14453. if (bytes < (size_t)sizeof_client_cert_der_4096)
  14454. bytes = (size_t)sizeof_client_cert_der_4096;
  14455. #else
  14456. bytes = FOURK_BUF;
  14457. #endif
  14458. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  14459. tmp = (byte*)XMALLOC(bytes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14460. if (tmp == NULL)
  14461. ERROR_OUT(-7900, exit_rsa);
  14462. #endif
  14463. #ifdef USE_CERT_BUFFERS_1024
  14464. XMEMCPY(tmp, client_key_der_1024, (size_t)sizeof_client_key_der_1024);
  14465. #elif defined(USE_CERT_BUFFERS_2048)
  14466. XMEMCPY(tmp, client_key_der_2048, (size_t)sizeof_client_key_der_2048);
  14467. #elif defined(USE_CERT_BUFFERS_3072)
  14468. XMEMCPY(tmp, client_key_der_3072, (size_t)sizeof_client_key_der_3072);
  14469. #elif defined(USE_CERT_BUFFERS_4096)
  14470. XMEMCPY(tmp, client_key_der_4096, (size_t)sizeof_client_key_der_4096);
  14471. #elif !defined(NO_FILESYSTEM)
  14472. file = XFOPEN(clientKey, "rb");
  14473. if (!file) {
  14474. err_sys("can't open ./certs/client-key.der, "
  14475. "Please run from wolfSSL home dir", -40);
  14476. ERROR_OUT(-7901, exit_rsa);
  14477. }
  14478. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  14479. XFCLOSE(file);
  14480. #else
  14481. /* No key to use. */
  14482. ERROR_OUT(-7902, exit_rsa);
  14483. #endif /* USE_CERT_BUFFERS */
  14484. ret = wc_InitRsaKey_ex(key, HEAP_HINT, devId);
  14485. if (ret != 0) {
  14486. ERROR_OUT(-7903, exit_rsa);
  14487. }
  14488. #ifndef NO_ASN
  14489. ret = wc_RsaPrivateKeyDecode(tmp, &idx, key, (word32)bytes);
  14490. if (ret != 0) {
  14491. ERROR_OUT(-7904, exit_rsa);
  14492. }
  14493. #ifndef NO_SIG_WRAPPER
  14494. modLen = wc_RsaEncryptSize(key);
  14495. #endif
  14496. #else
  14497. #ifdef USE_CERT_BUFFERS_2048
  14498. ret = mp_read_unsigned_bin(&key->n, &tmp[12], 256);
  14499. if (ret != 0) {
  14500. ERROR_OUT(-7905, exit_rsa);
  14501. }
  14502. ret = mp_set_int(&key->e, WC_RSA_EXPONENT);
  14503. if (ret != 0) {
  14504. ERROR_OUT(-7906, exit_rsa);
  14505. }
  14506. #ifndef NO_SIG_WRAPPER
  14507. modLen = 2048;
  14508. #endif
  14509. #else
  14510. #error Not supported yet!
  14511. #endif
  14512. #endif
  14513. #ifndef WC_NO_RNG
  14514. #ifndef HAVE_FIPS
  14515. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  14516. #else
  14517. ret = wc_InitRng(&rng);
  14518. #endif
  14519. if (ret != 0) {
  14520. ERROR_OUT(-7907, exit_rsa);
  14521. }
  14522. #endif
  14523. #ifndef NO_SIG_WRAPPER
  14524. ret = rsa_sig_test(key, sizeof *key, modLen, &rng);
  14525. if (ret != 0)
  14526. goto exit_rsa;
  14527. #endif
  14528. #ifdef WC_RSA_NONBLOCK
  14529. ret = rsa_nb_test(key, in, inLen, out, outSz, plain, plainSz, &rng);
  14530. if (ret != 0)
  14531. goto exit_rsa;
  14532. #endif
  14533. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
  14534. !defined(WC_NO_RNG) && !defined(WOLF_CRYPTO_CB_ONLY_RSA)
  14535. do {
  14536. #if defined(WOLFSSL_ASYNC_CRYPT)
  14537. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14538. #endif
  14539. if (ret >= 0) {
  14540. ret = wc_RsaPublicEncrypt(in, inLen, out, outSz, key, &rng);
  14541. }
  14542. } while (ret == WC_PENDING_E);
  14543. if (ret < 0) {
  14544. ERROR_OUT(-7908, exit_rsa);
  14545. }
  14546. TEST_SLEEP();
  14547. #ifdef WC_RSA_BLINDING
  14548. {
  14549. int tmpret = ret;
  14550. ret = wc_RsaSetRNG(key, &rng);
  14551. if (ret < 0) {
  14552. ERROR_OUT(-7909, exit_rsa);
  14553. }
  14554. ret = tmpret;
  14555. }
  14556. #endif
  14557. idx = (word32)ret; /* save off encrypted length */
  14558. do {
  14559. #if defined(WOLFSSL_ASYNC_CRYPT)
  14560. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14561. #endif
  14562. if (ret >= 0) {
  14563. ret = wc_RsaPrivateDecrypt(out, idx, plain, plainSz, key);
  14564. }
  14565. } while (ret == WC_PENDING_E);
  14566. if (ret < 0) {
  14567. ERROR_OUT(-7910, exit_rsa);
  14568. }
  14569. if (XMEMCMP(plain, in, inLen)) {
  14570. ERROR_OUT(-7911, exit_rsa);
  14571. }
  14572. TEST_SLEEP();
  14573. do {
  14574. #if defined(WOLFSSL_ASYNC_CRYPT)
  14575. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14576. #endif
  14577. if (ret >= 0) {
  14578. ret = wc_RsaPrivateDecryptInline(out, idx, &res, key);
  14579. }
  14580. } while (ret == WC_PENDING_E);
  14581. if (ret < 0) {
  14582. ERROR_OUT(-7912, exit_rsa);
  14583. }
  14584. if (ret != (int)inLen) {
  14585. ERROR_OUT(-7913, exit_rsa);
  14586. }
  14587. if (XMEMCMP(res, in, inLen)) {
  14588. ERROR_OUT(-7914, exit_rsa);
  14589. }
  14590. TEST_SLEEP();
  14591. do {
  14592. #if defined(WOLFSSL_ASYNC_CRYPT)
  14593. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14594. #endif
  14595. if (ret >= 0) {
  14596. ret = wc_RsaSSL_Sign(in, inLen, out, outSz, key, &rng);
  14597. }
  14598. } while (ret == WC_PENDING_E);
  14599. if (ret < 0) {
  14600. ERROR_OUT(-7915, exit_rsa);
  14601. }
  14602. TEST_SLEEP();
  14603. #elif defined(WOLFSSL_PUBLIC_MP)
  14604. {
  14605. static byte signature_2048[] = {
  14606. 0x07, 0x6f, 0xc9, 0x85, 0x73, 0x9e, 0x21, 0x79,
  14607. 0x47, 0xf1, 0xa3, 0xd7, 0xf4, 0x27, 0x29, 0xbe,
  14608. 0x99, 0x5d, 0xac, 0xb2, 0x10, 0x3f, 0x95, 0xda,
  14609. 0x89, 0x23, 0xb8, 0x96, 0x13, 0x57, 0x72, 0x30,
  14610. 0xa1, 0xfe, 0x5a, 0x68, 0x9c, 0x99, 0x9d, 0x1e,
  14611. 0x05, 0xa4, 0x80, 0xb0, 0xbb, 0xd9, 0xd9, 0xa1,
  14612. 0x69, 0x97, 0x74, 0xb3, 0x41, 0x21, 0x3b, 0x47,
  14613. 0xf5, 0x51, 0xb1, 0xfb, 0xc7, 0xaa, 0xcc, 0xdc,
  14614. 0xcd, 0x76, 0xa0, 0x28, 0x4d, 0x27, 0x14, 0xa4,
  14615. 0xb9, 0x41, 0x68, 0x7c, 0xb3, 0x66, 0xe6, 0x6f,
  14616. 0x40, 0x76, 0xe4, 0x12, 0xfd, 0xae, 0x29, 0xb5,
  14617. 0x63, 0x60, 0x87, 0xce, 0x49, 0x6b, 0xf3, 0x05,
  14618. 0x9a, 0x14, 0xb5, 0xcc, 0xcd, 0xf7, 0x30, 0x95,
  14619. 0xd2, 0x72, 0x52, 0x1d, 0x5b, 0x7e, 0xef, 0x4a,
  14620. 0x02, 0x96, 0x21, 0x6c, 0x55, 0xa5, 0x15, 0xb1,
  14621. 0x57, 0x63, 0x2c, 0xa3, 0x8e, 0x9d, 0x3d, 0x45,
  14622. 0xcc, 0xb8, 0xe6, 0xa1, 0xc8, 0x59, 0xcd, 0xf5,
  14623. 0xdc, 0x0a, 0x51, 0xb6, 0x9d, 0xfb, 0xf4, 0x6b,
  14624. 0xfd, 0x32, 0x71, 0x6e, 0xcf, 0xcb, 0xb3, 0xd9,
  14625. 0xe0, 0x4a, 0x77, 0x34, 0xd6, 0x61, 0xf5, 0x7c,
  14626. 0xf9, 0xa9, 0xa4, 0xb0, 0x8e, 0x3b, 0xd6, 0x04,
  14627. 0xe0, 0xde, 0x2b, 0x5b, 0x5a, 0xbf, 0xd9, 0xef,
  14628. 0x8d, 0xa3, 0xf5, 0xb1, 0x67, 0xf3, 0xb9, 0x72,
  14629. 0x0a, 0x37, 0x12, 0x35, 0x6c, 0x8e, 0x10, 0x8b,
  14630. 0x38, 0x06, 0x16, 0x4b, 0x20, 0x20, 0x13, 0x00,
  14631. 0x2e, 0x6d, 0xc2, 0x59, 0x23, 0x67, 0x4a, 0x6d,
  14632. 0xa1, 0x46, 0x8b, 0xee, 0xcf, 0x44, 0xb4, 0x3e,
  14633. 0x56, 0x75, 0x00, 0x68, 0xb5, 0x7d, 0x0f, 0x20,
  14634. 0x79, 0x5d, 0x7f, 0x12, 0x15, 0x32, 0x89, 0x61,
  14635. 0x6b, 0x29, 0xb7, 0x52, 0xf5, 0x25, 0xd8, 0x98,
  14636. 0xe8, 0x6f, 0xf9, 0x22, 0xb4, 0xbb, 0xe5, 0xff,
  14637. 0xd0, 0x92, 0x86, 0x9a, 0x88, 0xa2, 0xaf, 0x6b
  14638. };
  14639. ret = sizeof(signature_2048);
  14640. XMEMCPY(out, signature_2048, ret);
  14641. }
  14642. #endif
  14643. #if !defined(WC_NO_RNG) && !defined(WC_NO_RSA_OAEP) && \
  14644. ((!defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_PUBLIC_ONLY)) || \
  14645. defined(WOLFSSL_PUBLIC_MP)) && !defined(WOLF_CRYPTO_CB_ONLY_RSA)
  14646. idx = (word32)ret;
  14647. XMEMSET(plain, 0, plainSz);
  14648. do {
  14649. #if defined(WOLFSSL_ASYNC_CRYPT)
  14650. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14651. #endif
  14652. if (ret >= 0) {
  14653. #ifndef WOLFSSL_RSA_VERIFY_INLINE
  14654. #if defined(WOLFSSL_CRYPTOCELL)
  14655. /*
  14656. Cryptocell requires the input data and signature byte array to verify.
  14657. first argument must be the input data
  14658. second argument must be the length of input data
  14659. third argument must be the signature byte array or the output from
  14660. wc_RsaSSL_Sign()
  14661. fourth argument must be the length of the signature byte array
  14662. */
  14663. ret = wc_RsaSSL_Verify(in, inLen, out, outSz, key);
  14664. #else
  14665. ret = wc_RsaSSL_Verify(out, idx, plain, plainSz, key);
  14666. #endif /* WOLFSSL_CRYPTOCELL */
  14667. #else
  14668. byte* dec = NULL;
  14669. ret = wc_RsaSSL_VerifyInline(out, idx, &dec, key);
  14670. if (ret > 0) {
  14671. XMEMCPY(plain, dec, ret);
  14672. }
  14673. #endif
  14674. }
  14675. } while (ret == WC_PENDING_E);
  14676. if (ret < 0) {
  14677. ERROR_OUT(-7916, exit_rsa);
  14678. }
  14679. if (XMEMCMP(plain, in, (size_t)ret)) {
  14680. ERROR_OUT(-7917, exit_rsa);
  14681. }
  14682. TEST_SLEEP();
  14683. #endif
  14684. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  14685. #if !defined(WC_NO_RSA_OAEP) && !defined(WC_NO_RNG)
  14686. #if !defined(HAVE_FAST_RSA) && !defined(HAVE_USER_RSA) && \
  14687. (!defined(HAVE_FIPS) || \
  14688. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))) \
  14689. && !defined(WOLF_CRYPTO_CB_ONLY_RSA)
  14690. ret = rsa_oaep_padding_test(key, &rng);
  14691. if (ret != 0)
  14692. return ret;
  14693. #endif /* !HAVE_FAST_RSA && !HAVE_FIPS */
  14694. #endif /* WC_NO_RSA_OAEP && !WC_NO_RNG */
  14695. #endif /* WOLFSSL_RSA_VERIFY_ONLY */
  14696. #if !defined(HAVE_FIPS) && !defined(HAVE_USER_RSA) && !defined(NO_ASN) \
  14697. && !defined(WOLFSSL_RSA_VERIFY_ONLY)
  14698. ret = rsa_export_key_test(key);
  14699. if (ret != 0)
  14700. return ret;
  14701. #endif
  14702. #if !defined(NO_ASN) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
  14703. !defined(WOLFSSL_RSA_VERIFY_ONLY)
  14704. ret = rsa_flatten_test(key);
  14705. if (ret != 0)
  14706. return ret;
  14707. #endif
  14708. #if !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_ASN) && \
  14709. !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  14710. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096)
  14711. (void)clientCert;
  14712. #endif
  14713. #ifdef WOLFSSL_TEST_CERT
  14714. #if defined(WOLFSSL_MDK_ARM)
  14715. #define sizeof(s) XSTRLEN((char *)(s))
  14716. #endif
  14717. #ifdef USE_CERT_BUFFERS_1024
  14718. XMEMCPY(tmp, client_cert_der_1024, (size_t)sizeof_client_cert_der_1024);
  14719. bytes = (size_t)sizeof_client_cert_der_1024;
  14720. #elif defined(USE_CERT_BUFFERS_2048)
  14721. XMEMCPY(tmp, client_cert_der_2048, (size_t)sizeof_client_cert_der_2048);
  14722. bytes = (size_t)sizeof_client_cert_der_2048;
  14723. #elif defined(USE_CERT_BUFFERS_3072)
  14724. XMEMCPY(tmp, client_cert_der_3072, (size_t)sizeof_client_cert_der_3072);
  14725. bytes = (size_t)sizeof_client_cert_der_3072;
  14726. #elif defined(USE_CERT_BUFFERS_4096)
  14727. XMEMCPY(tmp, client_cert_der_4096, (size_t)sizeof_client_cert_der_4096);
  14728. bytes = (size_t)sizeof_client_cert_der_4096;
  14729. #elif !defined(NO_FILESYSTEM)
  14730. file2 = XFOPEN(clientCert, "rb");
  14731. if (!file2) {
  14732. ERROR_OUT(-7940, exit_rsa);
  14733. }
  14734. bytes = XFREAD(tmp, 1, FOURK_BUF, file2);
  14735. XFCLOSE(file2);
  14736. #else
  14737. /* No certificate to use. */
  14738. ERROR_OUT(-7941, exit_rsa);
  14739. #endif
  14740. #ifdef sizeof
  14741. #undef sizeof
  14742. #endif
  14743. InitDecodedCert(cert, tmp, (word32)bytes, NULL);
  14744. ret = ParseCert(cert, CERT_TYPE, NO_VERIFY, NULL);
  14745. if (ret != 0) {
  14746. FreeDecodedCert(cert);
  14747. ERROR_OUT(-7942, exit_rsa);
  14748. }
  14749. #ifndef NO_ASN_TIME
  14750. ret = wc_GetDateInfo(cert->afterDate, cert->afterDateLen, &date,
  14751. &dateFormat, &dateLength);
  14752. if (ret != 0) {
  14753. FreeDecodedCert(cert);
  14754. ERROR_OUT(-7943, exit_rsa);
  14755. }
  14756. ret = wc_GetDateAsCalendarTime(date, dateLength, dateFormat, &timearg);
  14757. if (ret != 0) {
  14758. FreeDecodedCert(cert);
  14759. ERROR_OUT(-7944, exit_rsa);
  14760. }
  14761. #endif
  14762. FreeDecodedCert(cert);
  14763. #endif /* WOLFSSL_TEST_CERT */
  14764. #ifdef WOLFSSL_CERT_EXT
  14765. #ifdef USE_CERT_BUFFERS_1024
  14766. XMEMCPY(tmp, client_keypub_der_1024, sizeof_client_keypub_der_1024);
  14767. bytes = sizeof_client_keypub_der_1024;
  14768. #elif defined(USE_CERT_BUFFERS_2048)
  14769. XMEMCPY(tmp, client_keypub_der_2048, sizeof_client_keypub_der_2048);
  14770. bytes = sizeof_client_keypub_der_2048;
  14771. #elif defined(USE_CERT_BUFFERS_3072)
  14772. XMEMCPY(tmp, client_keypub_der_3072, sizeof_client_keypub_der_3072);
  14773. bytes = sizeof_client_keypub_der_3072;
  14774. #elif defined(USE_CERT_BUFFERS_4096)
  14775. XMEMCPY(tmp, client_keypub_der_4096, sizeof_client_keypub_der_4096);
  14776. bytes = sizeof_client_keypub_der_4096;
  14777. #else
  14778. file = XFOPEN(clientKeyPub, "rb");
  14779. if (!file) {
  14780. err_sys("can't open ./certs/client-keyPub.der, "
  14781. "Please run from wolfSSL home dir", -40);
  14782. ERROR_OUT(-7945, exit_rsa);
  14783. }
  14784. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  14785. XFCLOSE(file);
  14786. #endif /* USE_CERT_BUFFERS */
  14787. ret = wc_InitRsaKey(keypub, HEAP_HINT);
  14788. if (ret != 0) {
  14789. ERROR_OUT(-7946, exit_rsa);
  14790. }
  14791. idx = 0;
  14792. ret = wc_RsaPublicKeyDecode(tmp, &idx, keypub, (word32)bytes);
  14793. if (ret != 0) {
  14794. ERROR_OUT(-7947, exit_rsa);
  14795. }
  14796. #endif /* WOLFSSL_CERT_EXT */
  14797. #ifdef WOLFSSL_KEY_GEN
  14798. ret = rsa_keygen_test(&rng);
  14799. if (ret != 0)
  14800. goto exit_rsa;
  14801. #endif
  14802. #ifdef WOLFSSL_CERT_GEN
  14803. /* Make Cert / Sign example for RSA cert and RSA CA */
  14804. ret = rsa_certgen_test(key, keypub, &rng, tmp);
  14805. if (ret != 0)
  14806. goto exit_rsa;
  14807. #if !defined(NO_RSA) && defined(HAVE_ECC)
  14808. ret = rsa_ecc_certgen_test(&rng, tmp);
  14809. if (ret != 0)
  14810. goto exit_rsa;
  14811. #endif
  14812. #if defined(WOLFSSL_CERT_REQ) && !defined(WOLFSSL_NO_MALLOC)
  14813. {
  14814. Cert *req;
  14815. int derSz;
  14816. #ifndef WOLFSSL_SMALL_STACK
  14817. byte* der = NULL;
  14818. #endif
  14819. req = (Cert *)XMALLOC(sizeof *req, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14820. if (! req)
  14821. ERROR_OUT(MEMORY_E, exit_rsa);
  14822. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,DYNAMIC_TYPE_TMP_BUFFER);
  14823. if (der == NULL) {
  14824. ERROR_OUT(-7964, exit_rsa);
  14825. }
  14826. if (wc_InitCert_ex(req, HEAP_HINT, devId)) {
  14827. ERROR_OUT(-7965, exit_rsa);
  14828. }
  14829. req->version = 0;
  14830. req->isCA = 1;
  14831. XSTRNCPY(req->challengePw, "wolf123", CTC_NAME_SIZE);
  14832. XMEMCPY(&req->subject, &certDefaultName, sizeof(CertName));
  14833. #ifndef NO_SHA256
  14834. req->sigType = CTC_SHA256wRSA;
  14835. #else
  14836. req->sigType = CTC_SHAwRSA;
  14837. #endif
  14838. #ifdef WOLFSSL_CERT_EXT
  14839. /* add SKID from the Public Key */
  14840. if (wc_SetSubjectKeyIdFromPublicKey(req, keypub, NULL) != 0) {
  14841. ERROR_OUT(-7966, exit_rsa);
  14842. }
  14843. /* add Key Usage */
  14844. if (wc_SetKeyUsage(req, certKeyUsage2) != 0) {
  14845. ERROR_OUT(-7967, exit_rsa);
  14846. }
  14847. /* add Extended Key Usage */
  14848. if (wc_SetExtKeyUsage(req, "serverAuth,clientAuth,codeSigning,"
  14849. "emailProtection,timeStamping,OCSPSigning") != 0) {
  14850. ERROR_OUT(-7968, exit_rsa);
  14851. }
  14852. #ifdef WOLFSSL_EKU_OID
  14853. {
  14854. WOLFSSL_SMALL_STACK_STATIC const char unique[] = "2.16.840.1.111111.100.1.10.1";
  14855. if (wc_SetExtKeyUsageOID(req, unique, sizeof(unique), 0,
  14856. HEAP_HINT) != 0) {
  14857. ERROR_OUT(-7969, exit_rsa);
  14858. }
  14859. }
  14860. #endif /* WOLFSSL_EKU_OID */
  14861. #endif /* WOLFSSL_CERT_EXT */
  14862. derSz = wc_MakeCertReq(req, der, FOURK_BUF, key, NULL);
  14863. if (derSz < 0) {
  14864. ERROR_OUT(-7970, exit_rsa);
  14865. }
  14866. #ifdef WOLFSSL_CERT_EXT
  14867. /* Try again with "any" flag set, will override all others */
  14868. if (wc_SetExtKeyUsage(req, "any") != 0) {
  14869. ERROR_OUT(-7971, exit_rsa);
  14870. }
  14871. derSz = wc_MakeCertReq(req, der, FOURK_BUF, key, NULL);
  14872. if (derSz < 0) {
  14873. ERROR_OUT(-7972, exit_rsa);
  14874. }
  14875. #endif /* WOLFSSL_CERT_EXT */
  14876. ret = 0;
  14877. do {
  14878. #if defined(WOLFSSL_ASYNC_CRYPT)
  14879. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14880. #endif
  14881. if (ret >= 0) {
  14882. ret = wc_SignCert(req->bodySz, req->sigType, der, FOURK_BUF,
  14883. key, NULL, &rng);
  14884. }
  14885. } while (ret == WC_PENDING_E);
  14886. if (ret < 0) {
  14887. ERROR_OUT(-7973, exit_rsa);
  14888. }
  14889. derSz = ret;
  14890. ret = SaveDerAndPem(der, derSz, certReqDerFile, certReqPemFile,
  14891. CERTREQ_TYPE, -5650);
  14892. if (ret != 0) {
  14893. goto exit_rsa;
  14894. }
  14895. derSz = wc_MakeCertReq_ex(req, der, FOURK_BUF, RSA_TYPE, key);
  14896. if (derSz < 0) {
  14897. ERROR_OUT(-7974, exit_rsa);
  14898. }
  14899. /* Test getting the size of the buffer without providing the buffer.
  14900. * derSz is set to the "largest buffer" we are willing to allocate. */
  14901. derSz = wc_MakeCertReq(req, NULL, 10000, key, NULL);
  14902. if (derSz < 0) {
  14903. ERROR_OUT(-7975, exit_rsa);
  14904. }
  14905. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14906. XFREE(req, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14907. der = NULL;
  14908. }
  14909. #endif /* WOLFSSL_CERT_REQ */
  14910. #endif /* WOLFSSL_CERT_GEN */
  14911. #if defined(WC_RSA_PSS) && !defined(HAVE_FIPS_VERSION) /* not supported with FIPSv1 */
  14912. /* Need to create known good signatures to test with this. */
  14913. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
  14914. !defined(WOLF_CRYPTO_CB_ONLY_RSA)
  14915. ret = rsa_pss_test(&rng, key);
  14916. if (ret != 0)
  14917. goto exit_rsa;
  14918. #endif
  14919. #endif
  14920. #if defined(WOLFSSL_HAVE_SP_RSA) && defined(USE_FAST_MATH)
  14921. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  14922. /* New key to be loaded in rsa_even_mod_test(). */
  14923. if (key != NULL)
  14924. #endif
  14925. wc_FreeRsaKey(key);
  14926. /* New key to be loaded in rsa_even_mod_test(). */
  14927. ret = rsa_even_mod_test(&rng, key);
  14928. #endif
  14929. exit_rsa:
  14930. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  14931. if (key != NULL) {
  14932. wc_FreeRsaKey(key);
  14933. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14934. }
  14935. #if defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_CERT_GEN)
  14936. if (keypub != NULL) {
  14937. wc_FreeRsaKey(keypub);
  14938. XFREE(keypub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14939. }
  14940. #endif
  14941. #ifdef WOLFSSL_TEST_CERT
  14942. if (cert != NULL)
  14943. XFREE(cert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14944. #endif
  14945. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14946. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14947. #else
  14948. wc_FreeRsaKey(key);
  14949. #if defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_CERT_GEN)
  14950. wc_FreeRsaKey(keypub);
  14951. #endif
  14952. #endif /* WOLFSSL_SMALL_STACK && !WOLFSSL_NO_MALLOC */
  14953. wc_FreeRng(&rng);
  14954. WC_FREE_VAR(in, HEAP_HINT);
  14955. WC_FREE_VAR(out, HEAP_HINT);
  14956. WC_FREE_VAR(plain, HEAP_HINT);
  14957. (void)res;
  14958. (void)bytes;
  14959. (void)idx;
  14960. (void)in;
  14961. (void)out;
  14962. (void)plain;
  14963. (void)idx;
  14964. (void)inStr;
  14965. (void)inLen;
  14966. (void)outSz;
  14967. (void)plainSz;
  14968. /* ret can be greater then 0 with certgen but all negative values should
  14969. * be returned and treated as an error */
  14970. if (ret >= 0) {
  14971. return 0;
  14972. }
  14973. else {
  14974. return ret;
  14975. }
  14976. }
  14977. #endif /* !NO_RSA */
  14978. #ifndef NO_DH
  14979. static int dh_fips_generate_test(WC_RNG *rng)
  14980. {
  14981. int ret = 0;
  14982. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  14983. DhKey *key = (DhKey *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14984. #else
  14985. DhKey key[1];
  14986. #endif
  14987. WOLFSSL_SMALL_STACK_STATIC const byte p[] = {
  14988. 0xc5, 0x7c, 0xa2, 0x4f, 0x4b, 0xd6, 0x8c, 0x3c,
  14989. 0xda, 0xc7, 0xba, 0xaa, 0xea, 0x2e, 0x5c, 0x1e,
  14990. 0x18, 0xb2, 0x7b, 0x8c, 0x55, 0x65, 0x9f, 0xea,
  14991. 0xe0, 0xa1, 0x36, 0x53, 0x2b, 0x36, 0xe0, 0x4e,
  14992. 0x3e, 0x64, 0xa9, 0xe4, 0xfc, 0x8f, 0x32, 0x62,
  14993. 0x97, 0xe4, 0xbe, 0xf7, 0xc1, 0xde, 0x07, 0x5a,
  14994. 0x89, 0x28, 0xf3, 0xfe, 0x4f, 0xfe, 0x68, 0xbc,
  14995. 0xfb, 0x0a, 0x7c, 0xa4, 0xb3, 0x14, 0x48, 0x89,
  14996. 0x9f, 0xaf, 0xb8, 0x43, 0xe2, 0xa0, 0x62, 0x5c,
  14997. 0xb4, 0x88, 0x3f, 0x06, 0x50, 0x11, 0xfe, 0x65,
  14998. 0x8d, 0x49, 0xd2, 0xf5, 0x4b, 0x74, 0x79, 0xdb,
  14999. 0x06, 0x62, 0x92, 0x89, 0xed, 0xda, 0xcb, 0x87,
  15000. 0x37, 0x16, 0xd2, 0xa1, 0x7a, 0xe8, 0xde, 0x92,
  15001. 0xee, 0x3e, 0x41, 0x4a, 0x91, 0x5e, 0xed, 0xf3,
  15002. 0x6c, 0x6b, 0x7e, 0xfd, 0x15, 0x92, 0x18, 0xfc,
  15003. 0xa7, 0xac, 0x42, 0x85, 0x57, 0xe9, 0xdc, 0xda,
  15004. 0x55, 0xc9, 0x8b, 0x28, 0x9e, 0xc1, 0xc4, 0x46,
  15005. 0x4d, 0x88, 0xed, 0x62, 0x8e, 0xdb, 0x3f, 0xb9,
  15006. 0xd7, 0xc8, 0xe3, 0xcf, 0xb8, 0x34, 0x2c, 0xd2,
  15007. 0x6f, 0x28, 0x06, 0x41, 0xe3, 0x66, 0x8c, 0xfc,
  15008. 0x72, 0xff, 0x26, 0x3b, 0x6b, 0x6c, 0x6f, 0x73,
  15009. 0xde, 0xf2, 0x90, 0x29, 0xe0, 0x61, 0x32, 0xc4,
  15010. 0x12, 0x74, 0x09, 0x52, 0xec, 0xf3, 0x1b, 0xa6,
  15011. 0x45, 0x98, 0xac, 0xf9, 0x1c, 0x65, 0x8e, 0x3a,
  15012. 0x91, 0x84, 0x4b, 0x23, 0x8a, 0xb2, 0x3c, 0xc9,
  15013. 0xfa, 0xea, 0xf1, 0x38, 0xce, 0xd8, 0x05, 0xe0,
  15014. 0xfa, 0x44, 0x68, 0x1f, 0xeb, 0xd9, 0x57, 0xb8,
  15015. 0x4a, 0x97, 0x5b, 0x88, 0xc5, 0xf1, 0xbb, 0xb0,
  15016. 0x49, 0xc3, 0x91, 0x7c, 0xd3, 0x13, 0xb9, 0x47,
  15017. 0xbb, 0x91, 0x8f, 0xe5, 0x26, 0x07, 0xab, 0xa9,
  15018. 0xc5, 0xd0, 0x3d, 0x95, 0x41, 0x26, 0x92, 0x9d,
  15019. 0x13, 0x67, 0xf2, 0x7e, 0x11, 0x88, 0xdc, 0x2d
  15020. };
  15021. WOLFSSL_SMALL_STACK_STATIC const byte g[] = {
  15022. 0x4a, 0x1a, 0xf3, 0xa4, 0x92, 0xe9, 0xee, 0x74,
  15023. 0x6e, 0x57, 0xd5, 0x8c, 0x2c, 0x5b, 0x41, 0x41,
  15024. 0x5e, 0xd4, 0x55, 0x19, 0xdc, 0xd9, 0x32, 0x91,
  15025. 0xf7, 0xfd, 0xc2, 0x57, 0xff, 0x03, 0x14, 0xdb,
  15026. 0xf1, 0xb7, 0x60, 0x0c, 0x43, 0x59, 0x3f, 0xff,
  15027. 0xac, 0xf1, 0x80, 0x9a, 0x15, 0x6f, 0xd8, 0x6e,
  15028. 0xb7, 0x85, 0x18, 0xc8, 0xec, 0x4e, 0x59, 0x4a,
  15029. 0xe2, 0x91, 0x43, 0x4c, 0xeb, 0x95, 0xb6, 0x2e,
  15030. 0x9a, 0xea, 0x53, 0x68, 0x80, 0x64, 0x69, 0x40,
  15031. 0xf9, 0xec, 0xbd, 0x85, 0x89, 0x26, 0x97, 0x67,
  15032. 0xaf, 0xb0, 0xad, 0x00, 0x1b, 0xd4, 0xfd, 0x94,
  15033. 0xd3, 0xe9, 0x92, 0xb1, 0xb4, 0xbc, 0x5a, 0xaa,
  15034. 0x92, 0x80, 0x89, 0x3b, 0x39, 0x05, 0x6c, 0x22,
  15035. 0x26, 0xfe, 0x5a, 0x28, 0x6c, 0x37, 0x50, 0x5a,
  15036. 0x38, 0x99, 0xcf, 0xf3, 0xc1, 0x96, 0x45, 0xdc,
  15037. 0x01, 0xcb, 0x20, 0x87, 0xa5, 0x00, 0x8c, 0xf5,
  15038. 0x4d, 0xc2, 0xef, 0xb8, 0x9b, 0xd1, 0x87, 0xbe,
  15039. 0xed, 0xd5, 0x0a, 0x29, 0x15, 0x34, 0x59, 0x4c,
  15040. 0x3a, 0x05, 0x22, 0x05, 0x44, 0x4f, 0x9f, 0xc8,
  15041. 0x47, 0x12, 0x24, 0x8e, 0xa8, 0x79, 0xe4, 0x67,
  15042. 0xba, 0x4d, 0x5b, 0x75, 0x56, 0x95, 0xeb, 0xe8,
  15043. 0x8a, 0xfa, 0x8e, 0x01, 0x8c, 0x1b, 0x74, 0x63,
  15044. 0xd9, 0x2f, 0xf7, 0xd3, 0x44, 0x8f, 0xa8, 0xf5,
  15045. 0xaf, 0x6c, 0x4f, 0xdb, 0xe7, 0xc9, 0x6c, 0x71,
  15046. 0x22, 0xa3, 0x1d, 0xf1, 0x40, 0xb2, 0xe0, 0x9a,
  15047. 0xb6, 0x72, 0xc9, 0xc0, 0x13, 0x16, 0xa2, 0x4a,
  15048. 0xe1, 0x92, 0xc7, 0x54, 0x23, 0xab, 0x9d, 0xa1,
  15049. 0xa1, 0xe5, 0x0b, 0xed, 0xba, 0xe8, 0x84, 0x37,
  15050. 0xb2, 0xe7, 0xfe, 0x32, 0x8d, 0xfa, 0x1c, 0x53,
  15051. 0x77, 0x97, 0xc7, 0xf3, 0x48, 0xc9, 0xdb, 0x2d,
  15052. 0x75, 0x52, 0x9d, 0x42, 0x51, 0x78, 0x62, 0x68,
  15053. 0x05, 0x45, 0x15, 0xf8, 0xa2, 0x4e, 0xf3, 0x0b
  15054. };
  15055. WOLFSSL_SMALL_STACK_STATIC const byte q[] = {
  15056. 0xe0, 0x35, 0x37, 0xaf, 0xb2, 0x50, 0x91, 0x8e,
  15057. 0xf2, 0x62, 0x2b, 0xd9, 0x9f, 0x6c, 0x11, 0x75,
  15058. 0xec, 0x24, 0x5d, 0x78, 0x59, 0xe7, 0x8d, 0xb5,
  15059. 0x40, 0x52, 0xed, 0x41
  15060. };
  15061. WOLFSSL_SMALL_STACK_STATIC const byte q0[] = {
  15062. 0x00,
  15063. 0xe0, 0x35, 0x37, 0xaf, 0xb2, 0x50, 0x91, 0x8e,
  15064. 0xf2, 0x62, 0x2b, 0xd9, 0x9f, 0x6c, 0x11, 0x75,
  15065. 0xec, 0x24, 0x5d, 0x78, 0x59, 0xe7, 0x8d, 0xb5,
  15066. 0x40, 0x52, 0xed, 0x41
  15067. };
  15068. byte priv[256];
  15069. byte pub[256];
  15070. word32 privSz = sizeof(priv);
  15071. word32 pubSz = sizeof(pub);
  15072. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15073. if (key == NULL)
  15074. ERROR_OUT(MEMORY_E, exit_gen_test);
  15075. #endif
  15076. /* Parameter Validation testing. */
  15077. ret = wc_DhGenerateKeyPair(NULL, rng, priv, &privSz, pub, &pubSz);
  15078. if (ret != BAD_FUNC_ARG)
  15079. ERROR_OUT(-7980, exit_gen_test);
  15080. ret = wc_DhGenerateKeyPair(key, NULL, priv, &privSz, pub, &pubSz);
  15081. if (ret != BAD_FUNC_ARG)
  15082. ERROR_OUT(-7981, exit_gen_test);
  15083. ret = wc_DhGenerateKeyPair(key, rng, NULL, &privSz, pub, &pubSz);
  15084. if (ret != BAD_FUNC_ARG)
  15085. ERROR_OUT(-7982, exit_gen_test);
  15086. ret = wc_DhGenerateKeyPair(key, rng, priv, NULL, pub, &pubSz);
  15087. if (ret != BAD_FUNC_ARG)
  15088. ERROR_OUT(-7983, exit_gen_test);
  15089. ret = wc_DhGenerateKeyPair(key, rng, priv, &privSz, NULL, &pubSz);
  15090. if (ret != BAD_FUNC_ARG)
  15091. ERROR_OUT(-7984, exit_gen_test);
  15092. ret = wc_DhGenerateKeyPair(key, rng, priv, &privSz, pub, NULL);
  15093. if (ret != BAD_FUNC_ARG)
  15094. ERROR_OUT(-7985, exit_gen_test);
  15095. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  15096. if (ret != 0)
  15097. ERROR_OUT(-7986, exit_gen_test);
  15098. ret = wc_DhSetKey_ex(key, p, sizeof(p), g, sizeof(g), q0, sizeof(q0));
  15099. if (ret != 0) {
  15100. ERROR_OUT(-7987, exit_gen_test);
  15101. }
  15102. wc_FreeDhKey(key);
  15103. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  15104. if (ret != 0)
  15105. ERROR_OUT(-7988, exit_gen_test);
  15106. ret = wc_DhSetKey_ex(key, p, sizeof(p), g, sizeof(g), q, sizeof(q));
  15107. if (ret != 0) {
  15108. ERROR_OUT(-7989, exit_gen_test);
  15109. }
  15110. /* Use API. */
  15111. ret = wc_DhGenerateKeyPair(key, rng, priv, &privSz, pub, &pubSz);
  15112. #if defined(WOLFSSL_ASYNC_CRYPT)
  15113. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  15114. #endif
  15115. if (ret != 0) {
  15116. ERROR_OUT(-7990, exit_gen_test);
  15117. }
  15118. ret = wc_DhCheckPubKey_ex(key, pub, pubSz, q0, sizeof(q0));
  15119. if (ret != 0) {
  15120. ERROR_OUT(-7991, exit_gen_test);
  15121. }
  15122. wc_FreeDhKey(key);
  15123. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  15124. if (ret != 0)
  15125. ERROR_OUT(-7992, exit_gen_test);
  15126. ret = wc_DhSetKey(key, p, sizeof(p), g, sizeof(g));
  15127. if (ret != 0) {
  15128. ERROR_OUT(-7993, exit_gen_test);
  15129. }
  15130. ret = wc_DhCheckPubKey_ex(key, pub, pubSz, q, sizeof(q));
  15131. if (ret != 0) {
  15132. ERROR_OUT(-7994, exit_gen_test);
  15133. }
  15134. #ifndef HAVE_SELFTEST
  15135. ret = wc_DhCheckKeyPair(key, pub, pubSz, priv, privSz);
  15136. if (ret != 0) {
  15137. ERROR_OUT(-7995, exit_gen_test);
  15138. }
  15139. /* Taint the public key so the check fails. */
  15140. pub[0]++;
  15141. ret = wc_DhCheckKeyPair(key, pub, pubSz, priv, privSz);
  15142. if (ret != MP_CMP_E) {
  15143. ERROR_OUT(-7996, exit_gen_test);
  15144. }
  15145. #ifdef WOLFSSL_KEY_GEN
  15146. wc_FreeDhKey(key);
  15147. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  15148. if (ret != 0)
  15149. ERROR_OUT(-7997, exit_gen_test);
  15150. ret = wc_DhGenerateParams(rng, 2048, key);
  15151. if (ret != 0) {
  15152. ERROR_OUT(-7998, exit_gen_test);
  15153. }
  15154. privSz = sizeof(priv);
  15155. pubSz = sizeof(pub);
  15156. ret = wc_DhGenerateKeyPair(key, rng, priv, &privSz, pub, &pubSz);
  15157. #if defined(WOLFSSL_ASYNC_CRYPT)
  15158. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  15159. #endif
  15160. if (ret != 0) {
  15161. ERROR_OUT(-7999, exit_gen_test);
  15162. }
  15163. #endif /* WOLFSSL_KEY_GEN */
  15164. #endif /* HAVE_SELFTEST */
  15165. ret = 0;
  15166. exit_gen_test:
  15167. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15168. if (key) {
  15169. wc_FreeDhKey(key);
  15170. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15171. }
  15172. #else
  15173. wc_FreeDhKey(key);
  15174. #endif
  15175. return ret;
  15176. }
  15177. static int dh_generate_test(WC_RNG *rng)
  15178. {
  15179. int ret = 0;
  15180. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15181. DhKey *smallKey = NULL;
  15182. #else
  15183. DhKey smallKey[1];
  15184. #endif
  15185. byte p[2] = { 1, 7 }; /* 263 in decimal */
  15186. byte g[2] = { 0, 2 };
  15187. #if !defined(WOLFSSL_SP_MATH) && !defined(HAVE_FFDHE)
  15188. #ifdef WOLFSSL_DH_CONST
  15189. /* the table for constant DH lookup will round to the lowest byte size 21 */
  15190. byte priv[21];
  15191. byte pub[21];
  15192. #else
  15193. byte priv[2];
  15194. byte pub[2];
  15195. #endif
  15196. word32 privSz = sizeof(priv);
  15197. word32 pubSz = sizeof(pub);
  15198. #endif
  15199. int smallKey_inited = 0;
  15200. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15201. if ((smallKey = (DhKey *)XMALLOC(sizeof(*smallKey), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER)) == NULL)
  15202. return -8019;
  15203. #endif
  15204. ret = wc_InitDhKey_ex(smallKey, HEAP_HINT, devId);
  15205. if (ret != 0)
  15206. ERROR_OUT(-8010, exit_gen_test);
  15207. smallKey_inited = 1;
  15208. /* Parameter Validation testing. */
  15209. ret = wc_InitDhKey_ex(NULL, HEAP_HINT, devId);
  15210. if (ret != BAD_FUNC_ARG)
  15211. return -8011;
  15212. wc_FreeDhKey(NULL);
  15213. ret = wc_DhSetKey(NULL, p, sizeof(p), g, sizeof(g));
  15214. if (ret != BAD_FUNC_ARG) {
  15215. ERROR_OUT(-8012, exit_gen_test);
  15216. }
  15217. ret = wc_DhSetKey(smallKey, NULL, sizeof(p), g, sizeof(g));
  15218. if (ret != BAD_FUNC_ARG) {
  15219. ERROR_OUT(-8013, exit_gen_test);
  15220. }
  15221. ret = wc_DhSetKey(smallKey, p, 0, g, sizeof(g));
  15222. if (ret != BAD_FUNC_ARG) {
  15223. ERROR_OUT(-8014, exit_gen_test);
  15224. }
  15225. ret = wc_DhSetKey(smallKey, p, sizeof(p), NULL, sizeof(g));
  15226. if (ret != BAD_FUNC_ARG) {
  15227. ERROR_OUT(-8015, exit_gen_test);
  15228. }
  15229. ret = wc_DhSetKey(smallKey, p, sizeof(p), g, 0);
  15230. if (ret != BAD_FUNC_ARG) {
  15231. ERROR_OUT(-8016, exit_gen_test);
  15232. }
  15233. ret = wc_DhSetKey(smallKey, p, sizeof(p), g, sizeof(g));
  15234. if (ret != 0) {
  15235. ERROR_OUT(-8017, exit_gen_test);
  15236. }
  15237. #if !defined(WOLFSSL_SP_MATH) && !defined(HAVE_FFDHE)
  15238. /* Use API. */
  15239. ret = wc_DhGenerateKeyPair(smallKey, rng, priv, &privSz, pub, &pubSz);
  15240. #if defined(WOLFSSL_ASYNC_CRYPT)
  15241. ret = wc_AsyncWait(ret, &smallKey->asyncDev, WC_ASYNC_FLAG_NONE);
  15242. #endif
  15243. if (ret != 0) {
  15244. ret = -8018;
  15245. }
  15246. #else
  15247. (void)rng;
  15248. #if defined(HAVE_FIPS) || !defined(WOLFSSL_NO_DH186)
  15249. ret = 0;
  15250. #endif
  15251. #endif
  15252. #if !defined(HAVE_FIPS) && defined(WOLFSSL_NO_DH186)
  15253. {
  15254. byte priv[260];
  15255. byte pub[260];
  15256. word32 privSz = sizeof(priv);
  15257. word32 pubSz = sizeof(pub);
  15258. /* test odd ball param generation with DH */
  15259. wc_FreeDhKey(smallKey);
  15260. ret = wc_InitDhKey_ex(smallKey, HEAP_HINT, devId);
  15261. if (ret != 0)
  15262. ERROR_OUT(-8019, exit_gen_test);
  15263. ret = wc_DhGenerateParams(rng, 2056, smallKey);
  15264. if (ret != 0) {
  15265. ERROR_OUT(-8020, exit_gen_test);
  15266. }
  15267. privSz = sizeof(priv);
  15268. pubSz = sizeof(pub);
  15269. ret = wc_DhGenerateKeyPair(smallKey, rng, priv, &privSz, pub, &pubSz);
  15270. #if defined(WOLFSSL_ASYNC_CRYPT)
  15271. ret = wc_AsyncWait(ret, &smallKey->asyncDev, WC_ASYNC_FLAG_NONE);
  15272. #endif
  15273. if (ret != 0) {
  15274. ERROR_OUT(-8021, exit_gen_test);
  15275. }
  15276. }
  15277. #endif /* !HAVE_FIPS and WOLFSSL_NO_DH186 */
  15278. exit_gen_test:
  15279. if (smallKey_inited)
  15280. wc_FreeDhKey(smallKey);
  15281. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15282. if (smallKey != NULL)
  15283. XFREE(smallKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15284. #endif
  15285. return ret;
  15286. }
  15287. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  15288. typedef struct dh_pubvalue_test {
  15289. const byte* data;
  15290. word32 len;
  15291. } dh_pubvalue_test;
  15292. static int dh_test_check_pubvalue(void)
  15293. {
  15294. int ret;
  15295. word32 i;
  15296. WOLFSSL_SMALL_STACK_STATIC const byte prime[] = {0x01, 0x00, 0x01};
  15297. WOLFSSL_SMALL_STACK_STATIC const byte pubValZero[] = { 0x00 };
  15298. WOLFSSL_SMALL_STACK_STATIC const byte pubValZeroLong[] = { 0x00, 0x00, 0x00 };
  15299. WOLFSSL_SMALL_STACK_STATIC const byte pubValOne[] = { 0x01 };
  15300. WOLFSSL_SMALL_STACK_STATIC const byte pubValOneLong[] = { 0x00, 0x00, 0x01 };
  15301. WOLFSSL_SMALL_STACK_STATIC const byte pubValPrimeMinusOne[] = { 0x01, 0x00, 0x00 };
  15302. WOLFSSL_SMALL_STACK_STATIC const byte pubValPrimeLong[] = {0x00, 0x01, 0x00, 0x01};
  15303. WOLFSSL_SMALL_STACK_STATIC const byte pubValPrimePlusOne[] = { 0x01, 0x00, 0x02 };
  15304. WOLFSSL_SMALL_STACK_STATIC const byte pubValTooBig0[] = { 0x02, 0x00, 0x01 };
  15305. WOLFSSL_SMALL_STACK_STATIC const byte pubValTooBig1[] = { 0x01, 0x01, 0x01 };
  15306. WOLFSSL_SMALL_STACK_STATIC const byte pubValTooLong[] = { 0x01, 0x00, 0x00, 0x01 };
  15307. const dh_pubvalue_test dh_pubval_fail[] = {
  15308. { prime, sizeof(prime) },
  15309. { pubValZero, sizeof(pubValZero) },
  15310. { pubValZeroLong, sizeof(pubValZeroLong) },
  15311. { pubValOne, sizeof(pubValOne) },
  15312. { pubValOneLong, sizeof(pubValOneLong) },
  15313. { pubValPrimeMinusOne, sizeof(pubValPrimeMinusOne) },
  15314. { pubValPrimeLong, sizeof(pubValPrimeLong) },
  15315. { pubValPrimePlusOne, sizeof(pubValPrimePlusOne) },
  15316. { pubValTooBig0, sizeof(pubValTooBig0) },
  15317. { pubValTooBig1, sizeof(pubValTooBig1) },
  15318. { pubValTooLong, sizeof(pubValTooLong) },
  15319. };
  15320. WOLFSSL_SMALL_STACK_STATIC const byte pubValTwo[] = { 0x02 };
  15321. WOLFSSL_SMALL_STACK_STATIC const byte pubValTwoLong[] = { 0x00, 0x00, 0x02 };
  15322. WOLFSSL_SMALL_STACK_STATIC const byte pubValGood[] = { 0x12, 0x34 };
  15323. WOLFSSL_SMALL_STACK_STATIC const byte pubValGoodLen[] = { 0x00, 0x12, 0x34 };
  15324. WOLFSSL_SMALL_STACK_STATIC const byte pubValGoodLong[] = { 0x00, 0x00, 0x12, 0x34 };
  15325. const dh_pubvalue_test dh_pubval_pass[] = {
  15326. { pubValTwo, sizeof(pubValTwo) },
  15327. { pubValTwoLong, sizeof(pubValTwoLong) },
  15328. { pubValGood, sizeof(pubValGood) },
  15329. { pubValGoodLen, sizeof(pubValGoodLen) },
  15330. { pubValGoodLong, sizeof(pubValGoodLong) },
  15331. };
  15332. for (i = 0; i < sizeof(dh_pubval_fail) / sizeof(*dh_pubval_fail); i++) {
  15333. ret = wc_DhCheckPubValue(prime, sizeof(prime), dh_pubval_fail[i].data,
  15334. dh_pubval_fail[i].len);
  15335. if (ret != MP_VAL)
  15336. return -8020 - (int)i;
  15337. }
  15338. for (i = 0; i < sizeof(dh_pubval_pass) / sizeof(*dh_pubval_pass); i++) {
  15339. ret = wc_DhCheckPubValue(prime, sizeof(prime), dh_pubval_pass[i].data,
  15340. dh_pubval_pass[i].len);
  15341. if (ret != 0)
  15342. return -8030 - (int)i;
  15343. }
  15344. return 0;
  15345. }
  15346. #endif
  15347. #if defined(HAVE_FFDHE)
  15348. #if defined(HAVE_FFDHE_4096)
  15349. #define MAX_DH_PRIV_SZ 39
  15350. #define MAX_DH_KEY_SZ 512
  15351. #elif defined(HAVE_FFDHE_3072)
  15352. #define MAX_DH_PRIV_SZ 34
  15353. #define MAX_DH_KEY_SZ 384
  15354. #else
  15355. #define MAX_DH_PRIV_SZ 29
  15356. #define MAX_DH_KEY_SZ 256
  15357. #endif
  15358. #ifndef WC_NO_RNG
  15359. #if !(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION == 2) && \
  15360. (defined(WOLFSSL_SP_ARM64_ASM) || defined(WOLFSSL_SP_ARM32_ASM)))
  15361. #ifdef HAVE_PUBLIC_FFDHE
  15362. static int dh_ffdhe_test(WC_RNG *rng, const DhParams* params)
  15363. #else
  15364. static int dh_ffdhe_test(WC_RNG *rng, int name)
  15365. #endif
  15366. {
  15367. int ret;
  15368. word32 privSz, pubSz, privSz2, pubSz2;
  15369. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15370. byte *priv = (byte*)XMALLOC(MAX_DH_PRIV_SZ, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15371. byte *pub = (byte*)XMALLOC(MAX_DH_KEY_SZ, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15372. byte *priv2 = (byte*)XMALLOC(MAX_DH_PRIV_SZ, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15373. byte *pub2 = (byte*)XMALLOC(MAX_DH_KEY_SZ, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15374. byte *agree = (byte*)XMALLOC(MAX_DH_KEY_SZ, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15375. byte *agree2 = (byte*)XMALLOC(MAX_DH_KEY_SZ, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15376. DhKey *key = (DhKey*)XMALLOC(sizeof(*key), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15377. DhKey *key2 = (DhKey*)XMALLOC(sizeof(*key2), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15378. #else
  15379. byte priv[MAX_DH_PRIV_SZ];
  15380. byte pub[MAX_DH_KEY_SZ];
  15381. byte priv2[MAX_DH_PRIV_SZ];
  15382. byte pub2[MAX_DH_KEY_SZ];
  15383. byte agree[MAX_DH_KEY_SZ];
  15384. byte agree2[MAX_DH_KEY_SZ];
  15385. DhKey key[1];
  15386. DhKey key2[1];
  15387. #endif
  15388. word32 agreeSz = MAX_DH_KEY_SZ;
  15389. word32 agreeSz2 = MAX_DH_KEY_SZ;
  15390. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15391. if ((priv == NULL) ||
  15392. (pub == NULL) ||
  15393. (priv2 == NULL) ||
  15394. (pub2 == NULL) ||
  15395. (agree == NULL) ||
  15396. (agree2 == NULL) ||
  15397. (key == NULL) ||
  15398. (key2 == NULL))
  15399. ERROR_OUT(-8050, done);
  15400. #endif
  15401. pubSz = MAX_DH_KEY_SZ;
  15402. pubSz2 = MAX_DH_KEY_SZ;
  15403. #ifdef HAVE_PUBLIC_FFDHE
  15404. privSz = MAX_DH_PRIV_SZ;
  15405. privSz2 = MAX_DH_PRIV_SZ;
  15406. #else
  15407. privSz = wc_DhGetNamedKeyMinSize(name);
  15408. privSz2 = privSz;
  15409. #endif
  15410. XMEMSET(key, 0, sizeof(*key));
  15411. XMEMSET(key2, 0, sizeof(*key2));
  15412. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  15413. if (ret != 0) {
  15414. ERROR_OUT(-8051, done);
  15415. }
  15416. ret = wc_InitDhKey_ex(key2, HEAP_HINT, devId);
  15417. if (ret != 0) {
  15418. ERROR_OUT(-8052, done);
  15419. }
  15420. #ifdef HAVE_PUBLIC_FFDHE
  15421. ret = wc_DhSetKey(key, params->p, params->p_len, params->g, params->g_len);
  15422. #else
  15423. ret = wc_DhSetNamedKey(key, name);
  15424. #endif
  15425. if (ret != 0) {
  15426. ERROR_OUT(-8053, done);
  15427. }
  15428. #ifdef HAVE_PUBLIC_FFDHE
  15429. ret = wc_DhSetKey(key2, params->p, params->p_len, params->g,
  15430. params->g_len);
  15431. #else
  15432. ret = wc_DhSetNamedKey(key2, name);
  15433. #endif
  15434. if (ret != 0) {
  15435. ERROR_OUT(-8054, done);
  15436. }
  15437. ret = wc_DhGenerateKeyPair(key, rng, priv, &privSz, pub, &pubSz);
  15438. #if defined(WOLFSSL_ASYNC_CRYPT)
  15439. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  15440. #endif
  15441. if (ret != 0) {
  15442. ERROR_OUT(-8055, done);
  15443. }
  15444. ret = wc_DhGenerateKeyPair(key2, rng, priv2, &privSz2, pub2, &pubSz2);
  15445. #if defined(WOLFSSL_ASYNC_CRYPT)
  15446. ret = wc_AsyncWait(ret, &key2->asyncDev, WC_ASYNC_FLAG_NONE);
  15447. #endif
  15448. if (ret != 0) {
  15449. ERROR_OUT(-8056, done);
  15450. }
  15451. ret = wc_DhAgree(key, agree, &agreeSz, priv, privSz, pub2, pubSz2);
  15452. #if defined(WOLFSSL_ASYNC_CRYPT)
  15453. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  15454. #endif
  15455. if (ret != 0) {
  15456. ERROR_OUT(-8057, done);
  15457. }
  15458. ret = wc_DhAgree(key2, agree2, &agreeSz2, priv2, privSz2, pub, pubSz);
  15459. #if defined(WOLFSSL_ASYNC_CRYPT)
  15460. ret = wc_AsyncWait(ret, &key2->asyncDev, WC_ASYNC_FLAG_NONE);
  15461. #endif
  15462. if (ret != 0) {
  15463. ERROR_OUT(-8058, done);
  15464. }
  15465. if (agreeSz != agreeSz2 || XMEMCMP(agree, agree2, agreeSz)) {
  15466. ERROR_OUT(-8059, done);
  15467. }
  15468. #if defined(WOLFSSL_HAVE_SP_DH) || defined(USE_FAST_MATH)
  15469. /* Make p even */
  15470. key->p.dp[0] &= (mp_digit)-2;
  15471. if (ret != 0) {
  15472. ERROR_OUT(-8058, done);
  15473. }
  15474. ret = wc_DhGenerateKeyPair(key, rng, priv, &privSz, pub, &pubSz);
  15475. #if defined(WOLFSSL_ASYNC_CRYPT)
  15476. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  15477. #endif
  15478. if (ret != MP_VAL && ret != MP_EXPTMOD_E) {
  15479. ERROR_OUT(-8058, done);
  15480. }
  15481. ret = wc_DhAgree(key, agree, &agreeSz, priv, privSz, pub2, pubSz2);
  15482. #if defined(WOLFSSL_ASYNC_CRYPT)
  15483. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  15484. #endif
  15485. if (ret != MP_VAL && ret != MP_EXPTMOD_E && ret != ASYNC_OP_E) {
  15486. ERROR_OUT(-8057, done);
  15487. }
  15488. ret = wc_DhCheckKeyPair(key, pub, pubSz, priv, privSz);
  15489. if (ret != MP_VAL && ret != MP_EXPTMOD_E && ret != MP_CMP_E &&
  15490. ret != ASYNC_OP_E) {
  15491. ERROR_OUT(-8057, done);
  15492. }
  15493. /* Getting here means success - set ret to 0. */
  15494. ret = 0;
  15495. #endif
  15496. done:
  15497. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC) && \
  15498. !defined(WC_NO_RNG)
  15499. if (priv)
  15500. XFREE(priv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15501. if (pub)
  15502. XFREE(pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15503. if (priv2)
  15504. XFREE(priv2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15505. if (pub2)
  15506. XFREE(pub2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15507. if (agree)
  15508. XFREE(agree, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15509. if (agree2)
  15510. XFREE(agree2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15511. if (key) {
  15512. wc_FreeDhKey(key);
  15513. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15514. }
  15515. if (key2) {
  15516. wc_FreeDhKey(key2);
  15517. XFREE(key2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15518. }
  15519. #else
  15520. wc_FreeDhKey(key);
  15521. wc_FreeDhKey(key2);
  15522. #endif
  15523. return ret;
  15524. }
  15525. #endif /* !(HAVE_FIPS_VERSION == 2 && WOLFSSL_SP_ARMxx_ASM) */
  15526. #endif /* !WC_NO_RNG */
  15527. #endif /* HAVE_FFDHE */
  15528. WOLFSSL_TEST_SUBROUTINE int dh_test(void)
  15529. {
  15530. int ret;
  15531. word32 bytes;
  15532. word32 idx = 0, privSz, pubSz, privSz2, pubSz2;
  15533. #ifndef WC_NO_RNG
  15534. WC_RNG rng;
  15535. int rngInit = 0;
  15536. #endif
  15537. int keyInit = 0, key2Init = 0;
  15538. #define DH_TEST_TMP_SIZE 1024
  15539. #if !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096)
  15540. #define DH_TEST_BUF_SIZE 256
  15541. #else
  15542. #define DH_TEST_BUF_SIZE 512
  15543. #endif
  15544. #ifndef WC_NO_RNG
  15545. word32 agreeSz = DH_TEST_BUF_SIZE;
  15546. word32 agreeSz2 = DH_TEST_BUF_SIZE;
  15547. #endif
  15548. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15549. DhKey *key = (DhKey *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15550. DhKey *key2 = (DhKey *)XMALLOC(sizeof *key2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15551. byte *tmp = (byte *)XMALLOC(DH_TEST_TMP_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15552. #else
  15553. DhKey key[1];
  15554. DhKey key2[1];
  15555. byte tmp[DH_TEST_TMP_SIZE];
  15556. #endif
  15557. #ifndef WC_NO_RNG
  15558. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15559. byte *priv = (byte *)XMALLOC(DH_TEST_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15560. byte *pub = (byte *)XMALLOC(DH_TEST_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15561. byte *priv2 = (byte *)XMALLOC(DH_TEST_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15562. byte *pub2 = (byte *)XMALLOC(DH_TEST_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15563. byte *agree = (byte *)XMALLOC(DH_TEST_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15564. byte *agree2 = (byte *)XMALLOC(DH_TEST_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15565. if (priv == NULL || pub == NULL || priv2 == NULL || pub2 == NULL ||
  15566. agree == NULL || agree2 == NULL) {
  15567. ERROR_OUT(-8100, done);
  15568. }
  15569. #else
  15570. byte priv[DH_TEST_BUF_SIZE];
  15571. byte pub[DH_TEST_BUF_SIZE];
  15572. byte priv2[DH_TEST_BUF_SIZE];
  15573. byte pub2[DH_TEST_BUF_SIZE];
  15574. byte agree[DH_TEST_BUF_SIZE];
  15575. byte agree2[DH_TEST_BUF_SIZE];
  15576. #endif
  15577. #endif /* !WC_NO_RNG */
  15578. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15579. if (key == NULL || key2 == NULL || tmp == NULL) {
  15580. ERROR_OUT(-8100, done);
  15581. }
  15582. #endif
  15583. #ifdef USE_CERT_BUFFERS_1024
  15584. XMEMCPY(tmp, dh_key_der_1024, (size_t)sizeof_dh_key_der_1024);
  15585. bytes = (size_t)sizeof_dh_key_der_1024;
  15586. #elif defined(USE_CERT_BUFFERS_2048)
  15587. XMEMCPY(tmp, dh_key_der_2048, (size_t)sizeof_dh_key_der_2048);
  15588. bytes = (size_t)sizeof_dh_key_der_2048;
  15589. #elif defined(USE_CERT_BUFFERS_3072)
  15590. XMEMCPY(tmp, dh_key_der_3072, (size_t)sizeof_dh_key_der_3072);
  15591. bytes = (size_t)sizeof_dh_key_der_3072;
  15592. #elif defined(USE_CERT_BUFFERS_4096)
  15593. XMEMCPY(tmp, dh_key_der_4096, (size_t)sizeof_dh_key_der_4096);
  15594. bytes = (size_t)sizeof_dh_key_der_4096;
  15595. #elif defined(NO_ASN)
  15596. /* don't use file, no DER parsing */
  15597. #elif !defined(NO_FILESYSTEM)
  15598. {
  15599. XFILE file = XFOPEN(dhParamsFile, "rb");
  15600. if (! file)
  15601. ERROR_OUT(-8101, done);
  15602. bytes = (word32) XFREAD(tmp, 1, DH_TEST_TMP_SIZE, file);
  15603. XFCLOSE(file);
  15604. }
  15605. #else
  15606. /* No DH key to use. */
  15607. ERROR_OUT(-8102, done);
  15608. #endif /* USE_CERT_BUFFERS */
  15609. (void)idx;
  15610. (void)tmp;
  15611. (void)bytes;
  15612. pubSz = DH_TEST_BUF_SIZE;
  15613. pubSz2 = DH_TEST_BUF_SIZE;
  15614. privSz = DH_TEST_BUF_SIZE;
  15615. privSz2 = DH_TEST_BUF_SIZE;
  15616. #ifndef WC_NO_RNG
  15617. XMEMSET(&rng, 0, sizeof(rng));
  15618. #endif
  15619. /* Use API for coverage. */
  15620. ret = wc_InitDhKey(key);
  15621. if (ret != 0) {
  15622. ERROR_OUT(-8103, done);
  15623. }
  15624. wc_FreeDhKey(key);
  15625. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  15626. if (ret != 0) {
  15627. ERROR_OUT(-8104, done);
  15628. }
  15629. keyInit = 1;
  15630. ret = wc_InitDhKey_ex(key2, HEAP_HINT, devId);
  15631. if (ret != 0) {
  15632. ERROR_OUT(-8105, done);
  15633. }
  15634. key2Init = 1;
  15635. #ifdef NO_ASN
  15636. #ifndef WOLFSSL_SP_MATH
  15637. ret = wc_DhSetKey(key, dh_p, sizeof(dh_p), dh_g, sizeof(dh_g));
  15638. if (ret != 0) {
  15639. ERROR_OUT(-8106, done);
  15640. }
  15641. ret = wc_DhSetKey(key2, dh_p, sizeof(dh_p), dh_g, sizeof(dh_g));
  15642. if (ret != 0) {
  15643. ERROR_OUT(-8107, done);
  15644. }
  15645. #else
  15646. ret = wc_DhSetKey(key, dh2048_p, sizeof(dh2048_p), dh2048_g,
  15647. sizeof(dh2048_g));
  15648. if (ret != 0) {
  15649. ERROR_OUT(-8106, done);
  15650. }
  15651. ret = wc_DhSetKey(key2, dh2048_p, sizeof(dh2048_p), dh2048_g,
  15652. sizeof(dh2048_g));
  15653. if (ret != 0) {
  15654. ERROR_OUT(-8107, done);
  15655. }
  15656. #endif
  15657. #else
  15658. ret = wc_DhKeyDecode(tmp, &idx, key, bytes);
  15659. if (ret != 0) {
  15660. ERROR_OUT(-8108, done);
  15661. }
  15662. idx = 0;
  15663. ret = wc_DhKeyDecode(tmp, &idx, key2, bytes);
  15664. if (ret != 0) {
  15665. ERROR_OUT(-8109, done);
  15666. }
  15667. #endif
  15668. #ifndef WC_NO_RNG
  15669. #ifndef HAVE_FIPS
  15670. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  15671. #else
  15672. ret = wc_InitRng(&rng);
  15673. #endif
  15674. if (ret != 0) {
  15675. ERROR_OUT(-8110, done);
  15676. }
  15677. rngInit = 1;
  15678. ret = wc_DhGenerateKeyPair(key, &rng, priv, &privSz, pub, &pubSz);
  15679. #if defined(WOLFSSL_ASYNC_CRYPT)
  15680. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  15681. #endif
  15682. if (ret != 0) {
  15683. ERROR_OUT(-8111, done);
  15684. }
  15685. ret = wc_DhGenerateKeyPair(key2, &rng, priv2, &privSz2, pub2, &pubSz2);
  15686. #if defined(WOLFSSL_ASYNC_CRYPT)
  15687. ret = wc_AsyncWait(ret, &key2->asyncDev, WC_ASYNC_FLAG_NONE);
  15688. #endif
  15689. if (ret != 0) {
  15690. ERROR_OUT(-8112, done);
  15691. }
  15692. ret = wc_DhAgree(key, agree, &agreeSz, priv, privSz, pub2, pubSz2);
  15693. #if defined(WOLFSSL_ASYNC_CRYPT)
  15694. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  15695. #endif
  15696. if (ret != 0) {
  15697. ERROR_OUT(-8113, done);
  15698. }
  15699. ret = wc_DhAgree(key2, agree2, &agreeSz2, priv2, privSz2, pub, pubSz);
  15700. #if defined(WOLFSSL_ASYNC_CRYPT)
  15701. ret = wc_AsyncWait(ret, &key2->asyncDev, WC_ASYNC_FLAG_NONE);
  15702. #endif
  15703. if (ret != 0) {
  15704. ERROR_OUT(-8114, done);
  15705. }
  15706. if (agreeSz != agreeSz2 || XMEMCMP(agree, agree2, agreeSz)) {
  15707. ERROR_OUT(-8115, done);
  15708. }
  15709. #endif /* !WC_NO_RNG */
  15710. #if defined(WOLFSSL_KEY_GEN) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  15711. if (wc_DhCheckPrivKey(NULL, NULL, 0) != BAD_FUNC_ARG)
  15712. ERROR_OUT(-8116, done);
  15713. if (wc_DhCheckPrivKey(key, priv, privSz) != 0)
  15714. ERROR_OUT(-8117, done);
  15715. if (wc_DhExportParamsRaw(NULL, NULL, NULL, NULL, NULL, NULL, NULL) != BAD_FUNC_ARG)
  15716. ERROR_OUT(-8118, done);
  15717. {
  15718. word32 pSz, qSz, gSz;
  15719. if (wc_DhExportParamsRaw(key, NULL, &pSz, NULL, &qSz, NULL, &gSz) != LENGTH_ONLY_E)
  15720. ERROR_OUT(-8119, done);
  15721. }
  15722. #endif
  15723. /* Test DH key import / export */
  15724. #if defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM) && \
  15725. (!defined(HAVE_FIPS) || \
  15726. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  15727. wc_FreeDhKey(key);
  15728. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  15729. if (ret != 0) {
  15730. ERROR_OUT(-8120, done);
  15731. }
  15732. #ifndef NO_ASN
  15733. {
  15734. /* DH Private - Key Export / Import */
  15735. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15736. byte *tmp2;
  15737. #else
  15738. byte tmp2[DH_TEST_TMP_SIZE];
  15739. #endif
  15740. XFILE file = XFOPEN(dhKeyFile, "rb");
  15741. if (!file)
  15742. ERROR_OUT(-8130, done);
  15743. bytes = (word32)XFREAD(tmp, 1, DH_TEST_TMP_SIZE, file);
  15744. XFCLOSE(file);
  15745. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15746. tmp2 = (byte*)XMALLOC(DH_TEST_TMP_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15747. if (tmp2 == NULL)
  15748. ERROR_OUT(-8131, done);
  15749. #endif
  15750. idx = 0;
  15751. XMEMSET(tmp2, 0, DH_TEST_TMP_SIZE);
  15752. /* Import DH Private key as DER */
  15753. ret = wc_DhKeyDecode(tmp, &idx, key, bytes);
  15754. if (ret == 0) {
  15755. /* Export as DER */
  15756. idx = DH_TEST_TMP_SIZE;
  15757. ret = wc_DhPrivKeyToDer(key, tmp2, &idx);
  15758. }
  15759. /* Verify export matches original */
  15760. if (ret <= 0 || bytes != idx || XMEMCMP(tmp, tmp2, bytes) != 0) {
  15761. ERROR_OUT(-8132, done);
  15762. }
  15763. /* DH Public Key - Export / Import */
  15764. file = XFOPEN(dhKeyPubFile, "rb");
  15765. if (!file)
  15766. ERROR_OUT(-8133, done);
  15767. bytes = (word32)XFREAD(tmp, 1, DH_TEST_TMP_SIZE, file);
  15768. XFCLOSE(file);
  15769. /* for HAVE_WOLF_BIGINT prevent leak */
  15770. wc_FreeDhKey(key);
  15771. (void)wc_InitDhKey_ex(key, HEAP_HINT, devId);
  15772. idx = 0;
  15773. XMEMSET(tmp2, 0, DH_TEST_TMP_SIZE);
  15774. /* Import DH Public key as DER */
  15775. ret = wc_DhKeyDecode(tmp, &idx, key, bytes);
  15776. if (ret == 0) {
  15777. /* Export as DER */
  15778. idx = DH_TEST_TMP_SIZE;
  15779. ret = wc_DhPubKeyToDer(key, tmp2, &idx);
  15780. }
  15781. /* Verify export matches original */
  15782. if (ret <= 0 || bytes != idx || XMEMCMP(tmp, tmp2, bytes) != 0) {
  15783. ERROR_OUT(-8134, done);
  15784. }
  15785. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15786. XFREE(tmp2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15787. #endif
  15788. }
  15789. #else
  15790. ret = wc_DhSetKey(key, dh_p, sizeof(dh_p), dh_g, sizeof(dh_g));
  15791. if (ret != 0) {
  15792. ERROR_OUT(-8121, done);
  15793. }
  15794. #endif /* !NO_ASN */
  15795. privSz = DH_TEST_BUF_SIZE;
  15796. pubSz = DH_TEST_BUF_SIZE;
  15797. ret = wc_DhExportKeyPair(key, priv, &privSz, pub, &pubSz);
  15798. if (ret != 0) {
  15799. ERROR_OUT(-8122, done);
  15800. }
  15801. ret = wc_DhImportKeyPair(key2, priv, privSz, pub, pubSz);
  15802. if (ret != 0) {
  15803. ERROR_OUT(-8125, done);
  15804. }
  15805. #endif /* WOLFSSL_DH_EXTRA && !NO_FILESYSTEM && !FIPS <= 2 */
  15806. #ifndef WC_NO_RNG
  15807. ret = dh_generate_test(&rng);
  15808. if (ret != 0)
  15809. ERROR_OUT(-8123, done);
  15810. ret = dh_fips_generate_test(&rng);
  15811. if (ret != 0)
  15812. ERROR_OUT(-8124, done);
  15813. #endif /* !WC_NO_RNG */
  15814. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  15815. ret = dh_test_check_pubvalue();
  15816. if (ret != 0)
  15817. ERROR_OUT(-8125, done);
  15818. #endif
  15819. #if !(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION == 2) && \
  15820. (defined(WOLFSSL_SP_ARM64_ASM) || defined(WOLFSSL_SP_ARM32_ASM)))
  15821. /* RNG with DH and SP_ASM code not supported in the in-lined FIPS ASM code,
  15822. * this will be available for testing in the 140-3 module */
  15823. #ifndef WC_NO_RNG
  15824. /* Specialized code for key gen when using FFDHE-2048, FFDHE-3072 and FFDHE-4096 */
  15825. #ifdef HAVE_FFDHE_2048
  15826. #ifdef HAVE_PUBLIC_FFDHE
  15827. ret = dh_ffdhe_test(&rng, wc_Dh_ffdhe2048_Get());
  15828. #else
  15829. ret = dh_ffdhe_test(&rng, WC_FFDHE_2048);
  15830. #endif
  15831. if (ret != 0)
  15832. ERROR_OUT(-8126, done);
  15833. #endif
  15834. #ifdef HAVE_FFDHE_3072
  15835. #ifdef HAVE_PUBLIC_FFDHE
  15836. ret = dh_ffdhe_test(&rng, wc_Dh_ffdhe3072_Get());
  15837. #else
  15838. ret = dh_ffdhe_test(&rng, WC_FFDHE_3072);
  15839. #endif
  15840. if (ret != 0)
  15841. ERROR_OUT(-8127, done);
  15842. #endif
  15843. #ifdef HAVE_FFDHE_4096
  15844. #ifdef HAVE_PUBLIC_FFDHE
  15845. ret = dh_ffdhe_test(&rng, wc_Dh_ffdhe4096_Get());
  15846. #else
  15847. ret = dh_ffdhe_test(&rng, WC_FFDHE_4096);
  15848. #endif
  15849. if (ret != 0)
  15850. ERROR_OUT(-8128, done);
  15851. #endif
  15852. #endif /* !WC_NO_RNG */
  15853. #endif /* HAVE_FIPS_VERSION == 2 && !WOLFSSL_SP_ARM64_ASM */
  15854. wc_FreeDhKey(key);
  15855. keyInit = 0;
  15856. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  15857. !defined(WOLFSSL_OLD_PRIME_CHECK) && !defined(WC_NO_RNG)
  15858. /* Test Check Key */
  15859. ret = wc_DhSetCheckKey(key, dh_p, sizeof(dh_p), dh_g, sizeof(dh_g),
  15860. NULL, 0, 0, &rng);
  15861. if (ret != 0)
  15862. ERROR_OUT(-8129, done);
  15863. keyInit = 1; /* DhSetCheckKey also initializes the key, free it */
  15864. #endif
  15865. done:
  15866. #ifndef WC_NO_RNG
  15867. if (rngInit)
  15868. wc_FreeRng(&rng);
  15869. #endif
  15870. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15871. if (key) {
  15872. if (keyInit)
  15873. wc_FreeDhKey(key);
  15874. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15875. }
  15876. if (key2) {
  15877. if (key2Init)
  15878. wc_FreeDhKey(key2);
  15879. XFREE(key2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15880. }
  15881. if (tmp)
  15882. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15883. if (priv)
  15884. XFREE(priv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15885. if (pub)
  15886. XFREE(pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15887. if (priv2)
  15888. XFREE(priv2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15889. if (pub2)
  15890. XFREE(pub2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15891. if (agree)
  15892. XFREE(agree, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15893. if (agree2)
  15894. XFREE(agree2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15895. #else
  15896. if (keyInit)
  15897. wc_FreeDhKey(key);
  15898. if (key2Init)
  15899. wc_FreeDhKey(key2);
  15900. #endif
  15901. (void)privSz;
  15902. (void)pubSz;
  15903. (void)pubSz2;
  15904. (void)privSz2;
  15905. return ret;
  15906. #undef DH_TEST_BUF_SIZE
  15907. #undef DH_TEST_TMP_SIZE
  15908. }
  15909. #endif /* NO_DH */
  15910. #ifndef NO_DSA
  15911. WOLFSSL_TEST_SUBROUTINE int dsa_test(void)
  15912. {
  15913. int ret = 0, answer;
  15914. word32 bytes;
  15915. word32 idx = 0;
  15916. WC_RNG rng;
  15917. wc_Sha sha;
  15918. byte hash[WC_SHA_DIGEST_SIZE];
  15919. byte signature[40];
  15920. #ifdef WOLFSSL_KEY_GEN
  15921. byte* der = 0;
  15922. #endif
  15923. #define DSA_TEST_TMP_SIZE 1024
  15924. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15925. byte *tmp = (byte *)XMALLOC(DSA_TEST_TMP_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15926. DsaKey *key = (DsaKey *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15927. #ifdef WOLFSSL_KEY_GEN
  15928. DsaKey *derIn = (DsaKey *)XMALLOC(sizeof *derIn, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15929. DsaKey *genKey = (DsaKey *)XMALLOC(sizeof *genKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15930. #endif
  15931. if ((tmp == NULL) ||
  15932. (key == NULL)
  15933. #ifdef WOLFSSL_KEY_GEN
  15934. || (derIn == NULL)
  15935. || (genKey == NULL)
  15936. #endif
  15937. ) {
  15938. ret = -8216;
  15939. goto out;
  15940. }
  15941. #else
  15942. byte tmp[1024];
  15943. DsaKey key[1];
  15944. #ifdef WOLFSSL_KEY_GEN
  15945. DsaKey derIn[1];
  15946. DsaKey genKey[1];
  15947. #endif
  15948. #endif
  15949. #ifdef USE_CERT_BUFFERS_1024
  15950. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  15951. bytes = sizeof_dsa_key_der_1024;
  15952. #elif defined(USE_CERT_BUFFERS_2048)
  15953. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  15954. bytes = sizeof_dsa_key_der_2048;
  15955. #else
  15956. {
  15957. XFILE file = XFOPEN(dsaKey, "rb");
  15958. if (!file)
  15959. ERROR_OUT(-8200, out);
  15960. bytes = (word32) XFREAD(tmp, 1, DSA_TEST_TMP_SIZE, file);
  15961. XFCLOSE(file);
  15962. }
  15963. #endif /* USE_CERT_BUFFERS */
  15964. ret = wc_InitSha_ex(&sha, HEAP_HINT, devId);
  15965. if (ret != 0)
  15966. ERROR_OUT(-8201, out);
  15967. wc_ShaUpdate(&sha, tmp, bytes);
  15968. wc_ShaFinal(&sha, hash);
  15969. wc_ShaFree(&sha);
  15970. ret = wc_InitDsaKey(key);
  15971. if (ret != 0)
  15972. ERROR_OUT(-8202, out);
  15973. ret = wc_DsaPrivateKeyDecode(tmp, &idx, key, bytes);
  15974. if (ret != 0)
  15975. ERROR_OUT(-8203, out);
  15976. #ifndef HAVE_FIPS
  15977. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  15978. #else
  15979. ret = wc_InitRng(&rng);
  15980. #endif
  15981. if (ret != 0)
  15982. ERROR_OUT(-8204, out);
  15983. ret = wc_DsaSign(hash, signature, key, &rng);
  15984. if (ret != 0)
  15985. ERROR_OUT(-8205, out);
  15986. ret = wc_DsaVerify(hash, signature, key, &answer);
  15987. if (ret != 0)
  15988. ERROR_OUT(-8206, out);
  15989. if (answer != 1)
  15990. ERROR_OUT(-8207, out);
  15991. wc_FreeDsaKey(key);
  15992. #ifdef WOLFSSL_KEY_GEN
  15993. {
  15994. int derSz = 0;
  15995. ret = wc_InitDsaKey(genKey);
  15996. if (ret != 0)
  15997. ERROR_OUT(-8208, out);
  15998. ret = wc_MakeDsaParameters(&rng, 1024, genKey);
  15999. if (ret != 0) {
  16000. wc_FreeDsaKey(genKey);
  16001. ERROR_OUT(-8209, out);
  16002. }
  16003. ret = wc_MakeDsaKey(&rng, genKey);
  16004. if (ret != 0) {
  16005. wc_FreeDsaKey(genKey);
  16006. ERROR_OUT(-8210, out);
  16007. }
  16008. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16009. if (der == NULL) {
  16010. wc_FreeDsaKey(genKey);
  16011. ERROR_OUT(-8211, out);
  16012. }
  16013. derSz = wc_DsaKeyToDer(genKey, der, FOURK_BUF);
  16014. if (derSz < 0) {
  16015. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16016. ERROR_OUT(-8212, out);
  16017. }
  16018. ret = SaveDerAndPem(der, derSz, keyDerFile, keyPemFile,
  16019. DSA_PRIVATEKEY_TYPE, -5814);
  16020. if (ret != 0) {
  16021. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16022. wc_FreeDsaKey(genKey);
  16023. goto out;
  16024. }
  16025. ret = wc_InitDsaKey(derIn);
  16026. if (ret != 0) {
  16027. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16028. wc_FreeDsaKey(genKey);
  16029. ERROR_OUT(-8213, out);
  16030. }
  16031. idx = 0;
  16032. ret = wc_DsaPrivateKeyDecode(der, &idx, derIn, derSz);
  16033. if (ret != 0) {
  16034. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16035. wc_FreeDsaKey(derIn);
  16036. wc_FreeDsaKey(genKey);
  16037. ERROR_OUT(-8214, out);
  16038. }
  16039. }
  16040. #endif /* WOLFSSL_KEY_GEN */
  16041. out:
  16042. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16043. if (key) {
  16044. #endif
  16045. if (wc_InitDsaKey_h(key, NULL) != 0)
  16046. ret = -8215;
  16047. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16048. }
  16049. #endif
  16050. #ifdef WOLFSSL_KEY_GEN
  16051. if (der)
  16052. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16053. #endif
  16054. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16055. if (tmp)
  16056. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16057. if (key)
  16058. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16059. #ifdef WOLFSSL_KEY_GEN
  16060. if (derIn) {
  16061. wc_FreeDsaKey(derIn);
  16062. XFREE(derIn, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16063. }
  16064. if (genKey) {
  16065. wc_FreeDsaKey(genKey);
  16066. XFREE(genKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16067. }
  16068. #endif
  16069. #else /* !WOLFSSL_SMALL_STACK || WOLFSSL_NO_MALLOC */
  16070. #ifdef WOLFSSL_KEY_GEN
  16071. wc_FreeDsaKey(derIn);
  16072. wc_FreeDsaKey(genKey);
  16073. #endif
  16074. #endif
  16075. wc_FreeRng(&rng);
  16076. return ret;
  16077. }
  16078. #endif /* NO_DSA */
  16079. #ifdef WOLFCRYPT_HAVE_SRP
  16080. static int generate_random_salt(byte *buf, word32 size)
  16081. {
  16082. int ret = -8220;
  16083. WC_RNG rng;
  16084. if(NULL == buf || !size)
  16085. return -8221;
  16086. if (buf && size && wc_InitRng_ex(&rng, HEAP_HINT, devId) == 0) {
  16087. ret = wc_RNG_GenerateBlock(&rng, (byte *)buf, size);
  16088. wc_FreeRng(&rng);
  16089. }
  16090. return ret;
  16091. }
  16092. static int srp_test_digest(SrpType dgstType)
  16093. {
  16094. int r;
  16095. byte clientPubKey[192]; /* A */
  16096. byte serverPubKey[192]; /* B */
  16097. word32 clientPubKeySz = 192;
  16098. word32 serverPubKeySz = 192;
  16099. byte username[] = "user";
  16100. word32 usernameSz = 4;
  16101. byte password[] = "password";
  16102. word32 passwordSz = 8;
  16103. WOLFSSL_SMALL_STACK_STATIC const byte N[] = {
  16104. 0xfc, 0x58, 0x7a, 0x8a, 0x70, 0xfb, 0x5a, 0x9a,
  16105. 0x5d, 0x39, 0x48, 0xbf, 0x1c, 0x46, 0xd8, 0x3b,
  16106. 0x7a, 0xe9, 0x1f, 0x85, 0x36, 0x18, 0xc4, 0x35,
  16107. 0x3f, 0xf8, 0x8a, 0x8f, 0x8c, 0x10, 0x2e, 0x01,
  16108. 0x58, 0x1d, 0x41, 0xcb, 0xc4, 0x47, 0xa8, 0xaf,
  16109. 0x9a, 0x6f, 0x58, 0x14, 0xa4, 0x68, 0xf0, 0x9c,
  16110. 0xa6, 0xe7, 0xbf, 0x0d, 0xe9, 0x62, 0x0b, 0xd7,
  16111. 0x26, 0x46, 0x5b, 0x27, 0xcb, 0x4c, 0xf9, 0x7e,
  16112. 0x1e, 0x8b, 0xe6, 0xdd, 0x29, 0xb7, 0xb7, 0x15,
  16113. 0x2e, 0xcf, 0x23, 0xa6, 0x4b, 0x97, 0x9f, 0x89,
  16114. 0xd4, 0x86, 0xc4, 0x90, 0x63, 0x92, 0xf4, 0x30,
  16115. 0x26, 0x69, 0x48, 0x9d, 0x7a, 0x4f, 0xad, 0xb5,
  16116. 0x6a, 0x51, 0xad, 0xeb, 0xf9, 0x90, 0x31, 0x77,
  16117. 0x53, 0x30, 0x2a, 0x85, 0xf7, 0x11, 0x21, 0x0c,
  16118. 0xb8, 0x4b, 0x56, 0x03, 0x5e, 0xbb, 0x25, 0x33,
  16119. 0x7c, 0xd9, 0x5a, 0xd1, 0x5c, 0xb2, 0xd4, 0x53,
  16120. 0xc5, 0x16, 0x68, 0xf0, 0xdf, 0x48, 0x55, 0x3e,
  16121. 0xd4, 0x59, 0x87, 0x64, 0x59, 0xaa, 0x39, 0x01,
  16122. 0x45, 0x89, 0x9c, 0x72, 0xff, 0xdd, 0x8f, 0x6d,
  16123. 0xa0, 0x42, 0xbc, 0x6f, 0x6e, 0x62, 0x18, 0x2d,
  16124. 0x50, 0xe8, 0x18, 0x97, 0x87, 0xfc, 0xef, 0x1f,
  16125. 0xf5, 0x53, 0x68, 0xe8, 0x49, 0xd1, 0xa2, 0xe8,
  16126. 0xb9, 0x26, 0x03, 0xba, 0xb5, 0x58, 0x6f, 0x6c,
  16127. 0x8b, 0x08, 0xa1, 0x7b, 0x6f, 0x42, 0xc9, 0x53
  16128. };
  16129. WOLFSSL_SMALL_STACK_STATIC const byte g[] = {
  16130. 0x02
  16131. };
  16132. byte salt[10];
  16133. byte verifier[192];
  16134. word32 v_size = sizeof(verifier);
  16135. word32 clientProofSz = SRP_MAX_DIGEST_SIZE;
  16136. word32 serverProofSz = SRP_MAX_DIGEST_SIZE;
  16137. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16138. Srp *cli = (Srp *)XMALLOC(sizeof *cli, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16139. Srp *srv = (Srp *)XMALLOC(sizeof *srv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16140. byte *clientProof = (byte *)XMALLOC(SRP_MAX_DIGEST_SIZE, HEAP_HINT,
  16141. DYNAMIC_TYPE_TMP_BUFFER); /* M1 */
  16142. byte *serverProof = (byte *)XMALLOC(SRP_MAX_DIGEST_SIZE, HEAP_HINT,
  16143. DYNAMIC_TYPE_TMP_BUFFER); /* M2 */
  16144. if ((cli == NULL) ||
  16145. (srv == NULL) ||
  16146. (clientProof == NULL) ||
  16147. (serverProof == NULL)) {
  16148. r = -8222;
  16149. goto out;
  16150. }
  16151. #else
  16152. Srp cli[1], srv[1];
  16153. byte clientProof[SRP_MAX_DIGEST_SIZE]; /* M1 */
  16154. byte serverProof[SRP_MAX_DIGEST_SIZE]; /* M2 */
  16155. #endif
  16156. /* set as 0's so if second init on srv not called SrpTerm is not on
  16157. * garbage values */
  16158. XMEMSET(srv, 0, sizeof *srv);
  16159. XMEMSET(cli, 0, sizeof *cli);
  16160. /* generating random salt */
  16161. r = generate_random_salt(salt, sizeof(salt));
  16162. /* client knows username and password. */
  16163. /* server knows N, g, salt and verifier. */
  16164. if (!r) r = wc_SrpInit_ex(cli, dgstType, SRP_CLIENT_SIDE, HEAP_HINT, devId);
  16165. if (!r) r = wc_SrpSetUsername(cli, username, usernameSz);
  16166. /* loading N, g and salt in advance to generate the verifier. */
  16167. if (!r) r = wc_SrpSetParams(cli, N, sizeof(N),
  16168. g, sizeof(g),
  16169. salt, sizeof(salt));
  16170. if (!r) r = wc_SrpSetPassword(cli, password, passwordSz);
  16171. if (!r) r = wc_SrpGetVerifier(cli, verifier, &v_size);
  16172. /* client sends username to server */
  16173. if (!r) r = wc_SrpInit_ex(srv, dgstType, SRP_SERVER_SIDE, HEAP_HINT, devId);
  16174. if (!r) r = wc_SrpSetUsername(srv, username, usernameSz);
  16175. if (!r) r = wc_SrpSetParams(srv, N, sizeof(N),
  16176. g, sizeof(g),
  16177. salt, sizeof(salt));
  16178. if (!r) r = wc_SrpSetVerifier(srv, verifier, v_size);
  16179. if (!r) r = wc_SrpGetPublic(srv, serverPubKey, &serverPubKeySz);
  16180. /* server sends N, g, salt and B to client */
  16181. if (!r) r = wc_SrpGetPublic(cli, clientPubKey, &clientPubKeySz);
  16182. if (!r) r = wc_SrpComputeKey(cli, clientPubKey, clientPubKeySz,
  16183. serverPubKey, serverPubKeySz);
  16184. if (!r) r = wc_SrpGetProof(cli, clientProof, &clientProofSz);
  16185. /* client sends A and M1 to server */
  16186. if (!r) r = wc_SrpComputeKey(srv, clientPubKey, clientPubKeySz,
  16187. serverPubKey, serverPubKeySz);
  16188. if (!r) r = wc_SrpVerifyPeersProof(srv, clientProof, clientProofSz);
  16189. if (!r) r = wc_SrpGetProof(srv, serverProof, &serverProofSz);
  16190. /* server sends M2 to client */
  16191. if (!r) r = wc_SrpVerifyPeersProof(cli, serverProof, serverProofSz);
  16192. wc_SrpTerm(cli);
  16193. wc_SrpTerm(srv);
  16194. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16195. out:
  16196. if (cli)
  16197. XFREE(cli, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16198. if (srv)
  16199. XFREE(srv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16200. if (clientProof)
  16201. XFREE(clientProof, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16202. if (serverProof)
  16203. XFREE(serverProof, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16204. #endif
  16205. return r;
  16206. }
  16207. WOLFSSL_TEST_SUBROUTINE int srp_test(void)
  16208. {
  16209. int ret;
  16210. #ifndef NO_SHA
  16211. ret = srp_test_digest(SRP_TYPE_SHA);
  16212. if (ret != 0)
  16213. return ret;
  16214. #endif
  16215. #ifndef NO_SHA256
  16216. ret = srp_test_digest(SRP_TYPE_SHA256);
  16217. if (ret != 0)
  16218. return ret;
  16219. #endif
  16220. #ifdef WOLFSSL_SHA384
  16221. ret = srp_test_digest(SRP_TYPE_SHA384);
  16222. if (ret != 0)
  16223. return ret;
  16224. #endif
  16225. #ifdef WOLFSSL_SHA512
  16226. ret = srp_test_digest(SRP_TYPE_SHA512);
  16227. if (ret != 0)
  16228. return ret;
  16229. #endif
  16230. return ret;
  16231. }
  16232. #endif /* WOLFCRYPT_HAVE_SRP */
  16233. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  16234. #if !defined(NO_AES) && !defined(WOLFCRYPT_ONLY)
  16235. static int openssl_aes_test(void)
  16236. {
  16237. #ifdef HAVE_AES_CBC
  16238. #ifdef WOLFSSL_AES_128
  16239. {
  16240. /* EVP_CipherUpdate test */
  16241. WOLFSSL_SMALL_STACK_STATIC const byte cbcPlain[] =
  16242. {
  16243. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  16244. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  16245. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  16246. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  16247. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  16248. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  16249. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  16250. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  16251. };
  16252. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  16253. "0123456789abcdef "; /* align */
  16254. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  16255. "1234567890abcdef "; /* align */
  16256. byte cipher[AES_BLOCK_SIZE * 4];
  16257. byte plain [AES_BLOCK_SIZE * 4];
  16258. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16259. EVP_CIPHER_CTX *en = wolfSSL_EVP_CIPHER_CTX_new();
  16260. EVP_CIPHER_CTX *de = wolfSSL_EVP_CIPHER_CTX_new();
  16261. #else
  16262. EVP_CIPHER_CTX en[1];
  16263. EVP_CIPHER_CTX de[1];
  16264. #endif
  16265. int outlen ;
  16266. int total = 0;
  16267. int i;
  16268. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16269. if ((en == NULL) || (de == NULL))
  16270. return MEMORY_E;
  16271. #endif
  16272. EVP_CIPHER_CTX_init(en);
  16273. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  16274. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  16275. return -8400;
  16276. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen,
  16277. (byte*)cbcPlain, 9) == 0)
  16278. return -8401;
  16279. if (outlen != 0)
  16280. return -8402;
  16281. total += outlen;
  16282. if (EVP_CipherUpdate(en, (byte*)&cipher[total], &outlen,
  16283. (byte*)&cbcPlain[9] , 9) == 0)
  16284. return -8403;
  16285. if (outlen != 16)
  16286. return -8404;
  16287. total += outlen;
  16288. if (EVP_CipherFinal(en, (byte*)&cipher[total], &outlen) == 0)
  16289. return -8405;
  16290. if (outlen != 16)
  16291. return -8406;
  16292. total += outlen;
  16293. if (total != 32)
  16294. return 3408;
  16295. total = 0;
  16296. EVP_CIPHER_CTX_init(de);
  16297. if (EVP_CipherInit(de, EVP_aes_128_cbc(),
  16298. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  16299. return -8407;
  16300. if (EVP_CipherUpdate(de, (byte*)plain, &outlen, (byte*)cipher, 6) == 0)
  16301. return -8408;
  16302. if (outlen != 0)
  16303. return -8409;
  16304. total += outlen;
  16305. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen,
  16306. (byte*)&cipher[6], 12) == 0)
  16307. return -8410;
  16308. if (outlen != 0)
  16309. total += outlen;
  16310. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen,
  16311. (byte*)&cipher[6+12], 14) == 0)
  16312. return -8411;
  16313. if (outlen != 16)
  16314. return -8412;
  16315. total += outlen;
  16316. if (EVP_CipherFinal(de, (byte*)&plain[total], &outlen) == 0)
  16317. return -8413;
  16318. if (outlen != 2)
  16319. return -8414;
  16320. total += outlen;
  16321. if (total != 18)
  16322. return 3427;
  16323. if (XMEMCMP(plain, cbcPlain, 18))
  16324. return -8415;
  16325. /* test with encrypting/decrypting more than 16 bytes at once */
  16326. total = 0;
  16327. EVP_CIPHER_CTX_init(en);
  16328. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  16329. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  16330. return -8416;
  16331. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen,
  16332. (byte*)cbcPlain, 17) == 0)
  16333. return -8417;
  16334. if (outlen != 16)
  16335. return -8418;
  16336. total += outlen;
  16337. if (EVP_CipherUpdate(en, (byte*)&cipher[total], &outlen,
  16338. (byte*)&cbcPlain[17] , 1) == 0)
  16339. return -8419;
  16340. if (outlen != 0)
  16341. return -8420;
  16342. total += outlen;
  16343. if (EVP_CipherFinal(en, (byte*)&cipher[total], &outlen) == 0)
  16344. return -8421;
  16345. if (outlen != 16)
  16346. return -8422;
  16347. total += outlen;
  16348. if (total != 32)
  16349. return -8423;
  16350. total = 0;
  16351. EVP_CIPHER_CTX_init(de);
  16352. if (EVP_CipherInit(de, EVP_aes_128_cbc(),
  16353. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  16354. return -8424;
  16355. if (EVP_CipherUpdate(de, (byte*)plain, &outlen, (byte*)cipher, 17) == 0)
  16356. return -8425;
  16357. if (outlen != 16)
  16358. return -8426;
  16359. total += outlen;
  16360. /* final call on non block size should fail */
  16361. if (EVP_CipherFinal(de, (byte*)&plain[total], &outlen) != 0)
  16362. return -8427;
  16363. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen,
  16364. (byte*)&cipher[17], 1) == 0)
  16365. return -8428;
  16366. if (outlen != 0)
  16367. total += outlen;
  16368. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen,
  16369. (byte*)&cipher[17+1], 14) == 0)
  16370. return -8429;
  16371. if (outlen != 0)
  16372. return -8430;
  16373. total += outlen;
  16374. if (EVP_CipherFinal(de, (byte*)&plain[total], &outlen) == 0)
  16375. return -8431;
  16376. if (outlen != 2)
  16377. return -8432;
  16378. total += outlen;
  16379. if (total != 18)
  16380. return -8433;
  16381. if (XMEMCMP(plain, cbcPlain, 18))
  16382. return -8434;
  16383. /* test byte by byte decrypt */
  16384. for (i = 0; i < AES_BLOCK_SIZE * 3; i++) {
  16385. plain[i] = i;
  16386. }
  16387. total = 0;
  16388. EVP_CIPHER_CTX_init(en);
  16389. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  16390. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  16391. return -8435;
  16392. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen,
  16393. (byte*)plain, AES_BLOCK_SIZE * 3) == 0)
  16394. return -8436;
  16395. if (outlen != AES_BLOCK_SIZE * 3)
  16396. return -8437;
  16397. total += outlen;
  16398. if (EVP_CipherFinal(en, (byte*)&cipher[total], &outlen) == 0)
  16399. return -8438;
  16400. if (outlen != AES_BLOCK_SIZE)
  16401. return -8439;
  16402. total += outlen;
  16403. if (total != sizeof(plain))
  16404. return -8440;
  16405. total = 0;
  16406. EVP_CIPHER_CTX_init(de);
  16407. if (EVP_CipherInit(de, EVP_aes_128_cbc(),
  16408. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  16409. return -8441;
  16410. for (i = 0; i < AES_BLOCK_SIZE * 4; i++) {
  16411. if (EVP_CipherUpdate(de, (byte*)plain + total, &outlen,
  16412. (byte*)cipher + i, 1) == 0)
  16413. return -8442;
  16414. if (outlen > 0) {
  16415. int j;
  16416. total += outlen;
  16417. for (j = 0; j < total; j++) {
  16418. if (plain[j] != j) {
  16419. return -8443;
  16420. }
  16421. }
  16422. }
  16423. }
  16424. if (EVP_CipherFinal(de, (byte*)&plain[total], &outlen) == 0)
  16425. return -8444;
  16426. total += outlen;
  16427. if (total != AES_BLOCK_SIZE * 3) {
  16428. return -8445;
  16429. }
  16430. for (i = 0; i < AES_BLOCK_SIZE * 3; i++) {
  16431. if (plain[i] != i) {
  16432. return -8446;
  16433. }
  16434. }
  16435. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16436. wolfSSL_EVP_CIPHER_CTX_free(en);
  16437. wolfSSL_EVP_CIPHER_CTX_free(de);
  16438. #endif
  16439. }
  16440. /* set buffers to be exact size to catch potential over read/write */
  16441. {
  16442. /* EVP_CipherUpdate test */
  16443. WOLFSSL_SMALL_STACK_STATIC const byte cbcPlain[] =
  16444. {
  16445. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  16446. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  16447. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  16448. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  16449. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  16450. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  16451. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  16452. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  16453. };
  16454. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  16455. "0123456789abcdef "; /* align */
  16456. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  16457. "1234567890abcdef "; /* align */
  16458. #define EVP_TEST_BUF_SZ 18
  16459. #define EVP_TEST_BUF_PAD 32
  16460. byte cipher[EVP_TEST_BUF_SZ];
  16461. byte plain [EVP_TEST_BUF_SZ];
  16462. byte padded[EVP_TEST_BUF_PAD];
  16463. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16464. EVP_CIPHER_CTX *en = wolfSSL_EVP_CIPHER_CTX_new();
  16465. EVP_CIPHER_CTX *de = wolfSSL_EVP_CIPHER_CTX_new();
  16466. #else
  16467. EVP_CIPHER_CTX en[1];
  16468. EVP_CIPHER_CTX de[1];
  16469. #endif
  16470. int outlen ;
  16471. int total = 0;
  16472. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16473. if ((en == NULL) || (de == NULL))
  16474. return MEMORY_E;
  16475. #endif
  16476. EVP_CIPHER_CTX_init(en);
  16477. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  16478. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  16479. return -8447;
  16480. if (EVP_CIPHER_CTX_set_padding(en, 0) != 1)
  16481. return -8448;
  16482. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen,
  16483. (byte*)cbcPlain, EVP_TEST_BUF_SZ) == 0)
  16484. return -8449;
  16485. if (outlen != 16)
  16486. return -8450;
  16487. total += outlen;
  16488. /* should fail here */
  16489. if (EVP_CipherFinal(en, (byte*)&cipher[total], &outlen) != 0)
  16490. return -8451;
  16491. /* turn padding back on and do successful encrypt */
  16492. total = 0;
  16493. EVP_CIPHER_CTX_init(en);
  16494. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  16495. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  16496. return -8452;
  16497. if (EVP_CIPHER_CTX_set_padding(en, 1) != 1)
  16498. return -8453;
  16499. if (EVP_CipherUpdate(en, (byte*)padded, &outlen,
  16500. (byte*)cbcPlain, EVP_TEST_BUF_SZ) == 0)
  16501. return -8454;
  16502. if (outlen != 16)
  16503. return -8455;
  16504. total += outlen;
  16505. if (EVP_CipherFinal(en, (byte*)&padded[total], &outlen) == 0)
  16506. return -8456;
  16507. total += outlen;
  16508. if (total != 32)
  16509. return -8457;
  16510. XMEMCPY(cipher, padded, EVP_TEST_BUF_SZ);
  16511. /* test out of bounds read on buffers w/o padding during decryption */
  16512. total = 0;
  16513. EVP_CIPHER_CTX_init(de);
  16514. if (EVP_CipherInit(de, EVP_aes_128_cbc(),
  16515. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  16516. return -8458;
  16517. if (EVP_CIPHER_CTX_set_padding(de, 0) != 1)
  16518. return -8459;
  16519. if (EVP_CipherUpdate(de, (byte*)plain, &outlen, (byte*)cipher,
  16520. EVP_TEST_BUF_SZ) == 0)
  16521. return -8460;
  16522. if (outlen != 16)
  16523. return -8461;
  16524. total += outlen;
  16525. /* should fail since not using padding */
  16526. if (EVP_CipherFinal(de, (byte*)&plain[total], &outlen) != 0)
  16527. return -8462;
  16528. total = 0;
  16529. EVP_CIPHER_CTX_init(de);
  16530. if (EVP_CipherInit(de, EVP_aes_128_cbc(),
  16531. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  16532. return -8463;
  16533. if (EVP_CIPHER_CTX_set_padding(de, 1) != 1)
  16534. return -8464;
  16535. if (EVP_CipherUpdate(de, (byte*)padded, &outlen, (byte*)padded,
  16536. EVP_TEST_BUF_PAD) == 0)
  16537. return -8465;
  16538. if (outlen != 16)
  16539. return -8466;
  16540. total += outlen;
  16541. if (EVP_CipherFinal(de, (byte*)&padded[total], &outlen) == 0)
  16542. return -8467;
  16543. if (XMEMCMP(padded, cbcPlain, EVP_TEST_BUF_SZ))
  16544. return -8468;
  16545. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16546. wolfSSL_EVP_CIPHER_CTX_free(en);
  16547. wolfSSL_EVP_CIPHER_CTX_free(de);
  16548. #endif
  16549. }
  16550. { /* evp_cipher test: EVP_aes_128_cbc */
  16551. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16552. EVP_CIPHER_CTX *ctx = wolfSSL_EVP_CIPHER_CTX_new();
  16553. #else
  16554. EVP_CIPHER_CTX ctx[1];
  16555. #endif
  16556. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = { /* "Now is the time for all " w/o trailing 0 */
  16557. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  16558. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  16559. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  16560. };
  16561. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  16562. {
  16563. 0x95,0x94,0x92,0x57,0x5f,0x42,0x81,0x53,
  16564. 0x2c,0xcc,0x9d,0x46,0x77,0xa2,0x33,0xcb
  16565. };
  16566. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  16567. "0123456789abcdef "; /* align */
  16568. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  16569. "1234567890abcdef "; /* align */
  16570. byte cipher[AES_BLOCK_SIZE * 4];
  16571. byte plain [AES_BLOCK_SIZE * 4];
  16572. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16573. if (ctx == NULL)
  16574. return MEMORY_E;
  16575. #endif
  16576. EVP_CIPHER_CTX_init(ctx);
  16577. if (EVP_CipherInit(ctx, EVP_aes_128_cbc(), key, iv, 1) == 0)
  16578. return -8469;
  16579. if (EVP_Cipher(ctx, cipher, (byte*)msg, 16) != 16)
  16580. return -8470;
  16581. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE))
  16582. return -8471;
  16583. EVP_CIPHER_CTX_init(ctx);
  16584. if (EVP_CipherInit(ctx, EVP_aes_128_cbc(), key, iv, 0) == 0)
  16585. return -8472;
  16586. if (EVP_Cipher(ctx, plain, cipher, 16) != 16)
  16587. return -8473;
  16588. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  16589. return -8474;
  16590. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16591. wolfSSL_EVP_CIPHER_CTX_free(ctx);
  16592. #endif
  16593. } /* end evp_cipher test: EVP_aes_128_cbc*/
  16594. #endif /* WOLFSSL_AES_128 */
  16595. #endif /* HAVE_AES_CBC */
  16596. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_256)
  16597. { /* evp_cipher test: EVP_aes_256_ecb*/
  16598. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16599. EVP_CIPHER_CTX *ctx = wolfSSL_EVP_CIPHER_CTX_new();
  16600. #else
  16601. EVP_CIPHER_CTX ctx[1];
  16602. #endif
  16603. WOLFSSL_SMALL_STACK_STATIC const byte msg[] =
  16604. {
  16605. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  16606. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  16607. };
  16608. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  16609. {
  16610. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  16611. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  16612. };
  16613. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  16614. {
  16615. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  16616. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  16617. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  16618. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  16619. };
  16620. byte cipher[AES_BLOCK_SIZE * 4];
  16621. byte plain [AES_BLOCK_SIZE * 4];
  16622. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16623. if (ctx == NULL)
  16624. return MEMORY_E;
  16625. #endif
  16626. EVP_CIPHER_CTX_init(ctx);
  16627. if (EVP_CipherInit(ctx, EVP_aes_256_ecb(), (unsigned char*)key, NULL, 1) == 0)
  16628. return -8475;
  16629. if (EVP_Cipher(ctx, cipher, (byte*)msg, 16) != 16)
  16630. return -8476;
  16631. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE))
  16632. return -8477;
  16633. EVP_CIPHER_CTX_init(ctx);
  16634. if (EVP_CipherInit(ctx, EVP_aes_256_ecb(), (unsigned char*)key, NULL, 0) == 0)
  16635. return -8478;
  16636. if (EVP_Cipher(ctx, plain, cipher, 16) != 16)
  16637. return -8479;
  16638. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  16639. return -8480;
  16640. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16641. wolfSSL_EVP_CIPHER_CTX_free(ctx);
  16642. #endif
  16643. } /* end evp_cipher test */
  16644. #endif /* HAVE_AES_ECB && WOLFSSL_AES_256 */
  16645. #if defined(WOLFSSL_AES_DIRECT) && defined(WOLFSSL_AES_256)
  16646. /* enable HAVE_AES_DECRYPT for AES_encrypt/decrypt */
  16647. {
  16648. /* Test: AES_encrypt/decrypt/set Key */
  16649. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16650. AES_KEY *enc = (AES_KEY *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  16651. #ifdef HAVE_AES_DECRYPT
  16652. AES_KEY *dec = (AES_KEY *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  16653. #endif
  16654. #else
  16655. AES_KEY enc[1];
  16656. #ifdef HAVE_AES_DECRYPT
  16657. AES_KEY dec[1];
  16658. #endif
  16659. #endif
  16660. WOLFSSL_SMALL_STACK_STATIC const byte msg[] =
  16661. {
  16662. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  16663. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  16664. };
  16665. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  16666. {
  16667. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  16668. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  16669. };
  16670. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  16671. {
  16672. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  16673. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  16674. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  16675. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  16676. };
  16677. byte plain[sizeof(msg)];
  16678. byte cipher[sizeof(msg)];
  16679. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16680. if (enc == NULL)
  16681. return MEMORY_E;
  16682. #ifdef HAVE_AES_DECRYPT
  16683. if (dec == NULL)
  16684. return MEMORY_E;
  16685. #endif
  16686. #endif
  16687. AES_set_encrypt_key(key, sizeof(key)*8, enc);
  16688. AES_set_decrypt_key(key, sizeof(key)*8, dec);
  16689. AES_encrypt(msg, cipher, enc);
  16690. #ifdef HAVE_AES_DECRYPT
  16691. AES_decrypt(cipher, plain, dec);
  16692. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  16693. return -8481;
  16694. #endif /* HAVE_AES_DECRYPT */
  16695. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE))
  16696. return -8482;
  16697. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16698. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  16699. #ifdef HAVE_AES_DECRYPT
  16700. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  16701. #endif
  16702. #endif
  16703. }
  16704. #endif /* WOLFSSL_AES_DIRECT && WOLFSSL_AES_256 */
  16705. /* EVP_Cipher with EVP_aes_xxx_ctr() */
  16706. #ifdef WOLFSSL_AES_COUNTER
  16707. {
  16708. byte plainBuff [64];
  16709. byte cipherBuff[64];
  16710. #ifdef WOLFSSL_AES_128
  16711. WOLFSSL_SMALL_STACK_STATIC const byte ctrKey[] =
  16712. {
  16713. 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,
  16714. 0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c
  16715. };
  16716. WOLFSSL_SMALL_STACK_STATIC const byte ctrIv[] =
  16717. {
  16718. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  16719. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  16720. };
  16721. WOLFSSL_SMALL_STACK_STATIC const byte ctrPlain[] =
  16722. {
  16723. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  16724. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  16725. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  16726. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  16727. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  16728. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  16729. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  16730. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  16731. };
  16732. WOLFSSL_SMALL_STACK_STATIC const byte ctrCipher[] =
  16733. {
  16734. 0x87,0x4d,0x61,0x91,0xb6,0x20,0xe3,0x26,
  16735. 0x1b,0xef,0x68,0x64,0x99,0x0d,0xb6,0xce,
  16736. 0x98,0x06,0xf6,0x6b,0x79,0x70,0xfd,0xff,
  16737. 0x86,0x17,0x18,0x7b,0xb9,0xff,0xfd,0xff,
  16738. 0x5a,0xe4,0xdf,0x3e,0xdb,0xd5,0xd3,0x5e,
  16739. 0x5b,0x4f,0x09,0x02,0x0d,0xb0,0x3e,0xab,
  16740. 0x1e,0x03,0x1d,0xda,0x2f,0xbe,0x03,0xd1,
  16741. 0x79,0x21,0x70,0xa0,0xf3,0x00,0x9c,0xee
  16742. };
  16743. WOLFSSL_SMALL_STACK_STATIC const byte oddCipher[] =
  16744. {
  16745. 0xb9,0xd7,0xcb,0x08,0xb0,0xe1,0x7b,0xa0,
  16746. 0xc2
  16747. };
  16748. #endif
  16749. /* test vector from "Recommendation for Block Cipher Modes of Operation"
  16750. * NIST Special Publication 800-38A */
  16751. #ifdef WOLFSSL_AES_192
  16752. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Key[] =
  16753. {
  16754. 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  16755. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  16756. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b
  16757. };
  16758. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Iv[] =
  16759. {
  16760. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  16761. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  16762. };
  16763. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Plain[] =
  16764. {
  16765. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  16766. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  16767. };
  16768. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Cipher[] =
  16769. {
  16770. 0x1a,0xbc,0x93,0x24,0x17,0x52,0x1c,0xa2,
  16771. 0x4f,0x2b,0x04,0x59,0xfe,0x7e,0x6e,0x0b
  16772. };
  16773. #endif /* WOLFSSL_AES_192 */
  16774. #ifdef WOLFSSL_AES_256
  16775. /* test vector from "Recommendation for Block Cipher Modes of Operation"
  16776. * NIST Special Publication 800-38A */
  16777. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Key[] =
  16778. {
  16779. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  16780. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  16781. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  16782. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  16783. };
  16784. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Iv[] =
  16785. {
  16786. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  16787. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  16788. };
  16789. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Plain[] =
  16790. {
  16791. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  16792. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  16793. };
  16794. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Cipher[] =
  16795. {
  16796. 0x60,0x1e,0xc3,0x13,0x77,0x57,0x89,0xa5,
  16797. 0xb7,0xa7,0xf5,0x04,0xbb,0xf3,0xd2,0x28
  16798. };
  16799. #endif /* WOLFSSL_AES_256 */
  16800. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16801. EVP_CIPHER_CTX *en = wolfSSL_EVP_CIPHER_CTX_new();
  16802. EVP_CIPHER_CTX *de = wolfSSL_EVP_CIPHER_CTX_new();
  16803. #else
  16804. EVP_CIPHER_CTX en[1];
  16805. EVP_CIPHER_CTX de[1];
  16806. #endif
  16807. #ifdef WOLFSSL_AES_128
  16808. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16809. EVP_CIPHER_CTX *p_en;
  16810. EVP_CIPHER_CTX *p_de;
  16811. #endif
  16812. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16813. if ((en == NULL) || (de == NULL))
  16814. return MEMORY_E;
  16815. #endif
  16816. EVP_CIPHER_CTX_init(en);
  16817. if (EVP_CipherInit(en, EVP_aes_128_ctr(),
  16818. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  16819. return -8483;
  16820. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctrPlain,
  16821. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  16822. return -8484;
  16823. EVP_CIPHER_CTX_init(de);
  16824. if (EVP_CipherInit(de, EVP_aes_128_ctr(),
  16825. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  16826. return -8485;
  16827. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff,
  16828. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  16829. return -8486;
  16830. if (XMEMCMP(cipherBuff, ctrCipher, AES_BLOCK_SIZE*4))
  16831. return -8487;
  16832. if (XMEMCMP(plainBuff, ctrPlain, AES_BLOCK_SIZE*4))
  16833. return -8488;
  16834. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16835. p_en = wolfSSL_EVP_CIPHER_CTX_new();
  16836. if (p_en == NULL)
  16837. return -8489;
  16838. p_de = wolfSSL_EVP_CIPHER_CTX_new();
  16839. if (p_de == NULL)
  16840. return -8490;
  16841. if (EVP_CipherInit(p_en, EVP_aes_128_ctr(),
  16842. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  16843. return -8491;
  16844. if (EVP_Cipher(p_en, (byte*)cipherBuff, (byte*)ctrPlain,
  16845. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  16846. return -8492;
  16847. if (EVP_CipherInit(p_de, EVP_aes_128_ctr(),
  16848. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  16849. return -8493;
  16850. if (EVP_Cipher(p_de, (byte*)plainBuff, (byte*)cipherBuff,
  16851. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  16852. return -8494;
  16853. wolfSSL_EVP_CIPHER_CTX_free(p_en);
  16854. wolfSSL_EVP_CIPHER_CTX_free(p_de);
  16855. #endif /* WOLFSSL_SMALL_STACK && !WOLFSSL_NO_MALLOC */
  16856. if (XMEMCMP(cipherBuff, ctrCipher, AES_BLOCK_SIZE*4))
  16857. return -8495;
  16858. if (XMEMCMP(plainBuff, ctrPlain, AES_BLOCK_SIZE*4))
  16859. return -8496;
  16860. EVP_CIPHER_CTX_init(en);
  16861. if (EVP_CipherInit(en, EVP_aes_128_ctr(),
  16862. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  16863. return -8497;
  16864. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctrPlain, 9) != 9)
  16865. return -8498;
  16866. EVP_CIPHER_CTX_init(de);
  16867. if (EVP_CipherInit(de, EVP_aes_128_ctr(),
  16868. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  16869. return -8499;
  16870. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff, 9) != 9)
  16871. return -8500;
  16872. if (XMEMCMP(plainBuff, ctrPlain, 9))
  16873. return -8501;
  16874. if (XMEMCMP(cipherBuff, ctrCipher, 9))
  16875. return -8502;
  16876. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctrPlain, 9) != 9)
  16877. return -8503;
  16878. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff, 9) != 9)
  16879. return -8504;
  16880. if (XMEMCMP(plainBuff, ctrPlain, 9))
  16881. return -8505;
  16882. if (XMEMCMP(cipherBuff, oddCipher, 9))
  16883. return -8506;
  16884. #endif /* WOLFSSL_AES_128 */
  16885. #ifdef WOLFSSL_AES_192
  16886. EVP_CIPHER_CTX_init(en);
  16887. if (EVP_CipherInit(en, EVP_aes_192_ctr(),
  16888. (unsigned char*)ctr192Key, (unsigned char*)ctr192Iv, 0) == 0)
  16889. return -8507;
  16890. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctr192Plain,
  16891. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  16892. return -8508;
  16893. EVP_CIPHER_CTX_init(de);
  16894. if (EVP_CipherInit(de, EVP_aes_192_ctr(),
  16895. (unsigned char*)ctr192Key, (unsigned char*)ctr192Iv, 0) == 0)
  16896. return -8509;
  16897. XMEMSET(plainBuff, 0, sizeof(plainBuff));
  16898. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff,
  16899. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  16900. return -8510;
  16901. if (XMEMCMP(plainBuff, ctr192Plain, sizeof(ctr192Plain)))
  16902. return -8511;
  16903. if (XMEMCMP(ctr192Cipher, cipherBuff, sizeof(ctr192Cipher)))
  16904. return -8512;
  16905. #endif /* WOLFSSL_AES_192 */
  16906. #ifdef WOLFSSL_AES_256
  16907. EVP_CIPHER_CTX_init(en);
  16908. if (EVP_CipherInit(en, EVP_aes_256_ctr(),
  16909. (unsigned char*)ctr256Key, (unsigned char*)ctr256Iv, 0) == 0)
  16910. return -8513;
  16911. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctr256Plain,
  16912. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  16913. return -8514;
  16914. EVP_CIPHER_CTX_init(de);
  16915. if (EVP_CipherInit(de, EVP_aes_256_ctr(),
  16916. (unsigned char*)ctr256Key, (unsigned char*)ctr256Iv, 0) == 0)
  16917. return -8515;
  16918. XMEMSET(plainBuff, 0, sizeof(plainBuff));
  16919. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff,
  16920. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  16921. return -8516;
  16922. if (XMEMCMP(plainBuff, ctr256Plain, sizeof(ctr256Plain)))
  16923. return -8517;
  16924. if (XMEMCMP(ctr256Cipher, cipherBuff, sizeof(ctr256Cipher)))
  16925. return -8518;
  16926. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16927. wolfSSL_EVP_CIPHER_CTX_free(en);
  16928. wolfSSL_EVP_CIPHER_CTX_free(de);
  16929. #endif
  16930. #endif /* WOLFSSL_AES_256 */
  16931. }
  16932. #endif /* HAVE_AES_COUNTER */
  16933. #if defined(WOLFSSL_AES_CFB) && defined(WOLFSSL_AES_128)
  16934. {
  16935. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16936. AES_KEY *enc = (AES_KEY *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  16937. AES_KEY *dec = (AES_KEY *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  16938. #else
  16939. AES_KEY enc[1];
  16940. AES_KEY dec[1];
  16941. #endif
  16942. WOLFSSL_SMALL_STACK_STATIC const byte setIv[] = {
  16943. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  16944. 0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f
  16945. };
  16946. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  16947. {
  16948. 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,
  16949. 0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c
  16950. };
  16951. WOLFSSL_SMALL_STACK_STATIC const byte cipher1[] =
  16952. {
  16953. 0x3b,0x3f,0xd9,0x2e,0xb7,0x2d,0xad,0x20,
  16954. 0x33,0x34,0x49,0xf8,0xe8,0x3c,0xfb,0x4a,
  16955. 0xc8,0xa6,0x45,0x37,0xa0,0xb3,0xa9,0x3f,
  16956. 0xcd,0xe3,0xcd,0xad,0x9f,0x1c,0xe5,0x8b
  16957. };
  16958. WOLFSSL_SMALL_STACK_STATIC const byte msg[] =
  16959. {
  16960. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  16961. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  16962. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  16963. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51
  16964. };
  16965. byte cipher[AES_BLOCK_SIZE * 2];
  16966. byte iv[AES_BLOCK_SIZE]; /* iv buffer is updeated by API */
  16967. int num = 0;
  16968. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16969. if ((enc == NULL) || (dec == NULL))
  16970. return MEMORY_E;
  16971. #endif
  16972. XMEMCPY(iv, setIv, sizeof(setIv));
  16973. wolfSSL_AES_set_encrypt_key(key, sizeof(key) * 8, enc);
  16974. wolfSSL_AES_set_encrypt_key(key, sizeof(key) * 8, dec);
  16975. wolfSSL_AES_cfb128_encrypt(msg, cipher, AES_BLOCK_SIZE - 1, enc, iv,
  16976. &num, AES_ENCRYPT);
  16977. if (XMEMCMP(cipher, cipher1, AES_BLOCK_SIZE - 1))
  16978. return -8519;
  16979. if (num != 15) /* should have used 15 of the 16 bytes */
  16980. return -8520;
  16981. wolfSSL_AES_cfb128_encrypt(msg + AES_BLOCK_SIZE - 1,
  16982. cipher + AES_BLOCK_SIZE - 1, AES_BLOCK_SIZE + 1, enc, iv,
  16983. &num, AES_ENCRYPT);
  16984. if (XMEMCMP(cipher, cipher1, AES_BLOCK_SIZE * 2))
  16985. return -8521;
  16986. if (num != 0)
  16987. return -8522;
  16988. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16989. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  16990. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  16991. #endif
  16992. }
  16993. #endif /* WOLFSSL_AES_CFB && WOLFSSL_AES_128 */
  16994. return 0;
  16995. }
  16996. #endif /* !defined(NO_AES) && !defined(WOLFCRYPT_ONLY) */
  16997. WOLFSSL_TEST_SUBROUTINE int openssl_test(void)
  16998. {
  16999. int ret;
  17000. EVP_MD_CTX md_ctx;
  17001. testVector a, b, c, d, e, f;
  17002. byte hash[WC_SHA256_DIGEST_SIZE*2]; /* max size */
  17003. a.inLen = 0;
  17004. b.inLen = c.inLen = d.inLen = e.inLen = f.inLen = a.inLen;
  17005. (void)a;
  17006. (void)b;
  17007. (void)c;
  17008. (void)d;
  17009. (void)e;
  17010. (void)f;
  17011. /* test malloc / free , 10 is an arbitrary amount of memory chosen */
  17012. {
  17013. byte* p;
  17014. p = (byte*)CRYPTO_malloc(10, "", 0);
  17015. if (p == NULL) {
  17016. return -8600;
  17017. }
  17018. XMEMSET(p, 0, 10);
  17019. CRYPTO_free(p, "", 0);
  17020. }
  17021. #ifndef NO_MD5
  17022. a.input = "1234567890123456789012345678901234567890123456789012345678"
  17023. "9012345678901234567890";
  17024. a.output = "\x57\xed\xf4\xa2\x2b\xe3\xc9\x55\xac\x49\xda\x2e\x21\x07\xb6"
  17025. "\x7a";
  17026. a.inLen = XSTRLEN(a.input);
  17027. a.outLen = WC_MD5_DIGEST_SIZE;
  17028. EVP_MD_CTX_init(&md_ctx);
  17029. ret = EVP_DigestInit(&md_ctx, EVP_md5());
  17030. if (ret == WOLFSSL_SUCCESS) {
  17031. ret = EVP_DigestUpdate(&md_ctx, a.input, (unsigned long)a.inLen);
  17032. }
  17033. if (ret == WOLFSSL_SUCCESS) {
  17034. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  17035. }
  17036. EVP_MD_CTX_cleanup(&md_ctx);
  17037. if (ret != WOLFSSL_SUCCESS)
  17038. return -18601;
  17039. if (XMEMCMP(hash, a.output, WC_MD5_DIGEST_SIZE) != 0)
  17040. return -8601;
  17041. #endif /* NO_MD5 */
  17042. #ifndef NO_SHA
  17043. b.input = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  17044. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  17045. "aaaaaaaaaa";
  17046. b.output = "\xAD\x5B\x3F\xDB\xCB\x52\x67\x78\xC2\x83\x9D\x2F\x15\x1E\xA7"
  17047. "\x53\x99\x5E\x26\xA0";
  17048. b.inLen = XSTRLEN(b.input);
  17049. b.outLen = WC_SHA_DIGEST_SIZE;
  17050. EVP_MD_CTX_init(&md_ctx);
  17051. ret = EVP_DigestInit(&md_ctx, EVP_sha1());
  17052. if (ret == WOLFSSL_SUCCESS) {
  17053. ret = EVP_DigestUpdate(&md_ctx, b.input, (unsigned long)b.inLen);
  17054. if (ret == WOLFSSL_SUCCESS)
  17055. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  17056. }
  17057. EVP_MD_CTX_cleanup(&md_ctx);
  17058. if (ret != WOLFSSL_SUCCESS)
  17059. return -18602;
  17060. if (XMEMCMP(hash, b.output, WC_SHA_DIGEST_SIZE) != 0)
  17061. return -8602;
  17062. #endif /* NO_SHA */
  17063. #ifdef WOLFSSL_SHA224
  17064. e.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  17065. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  17066. e.output = "\xc9\x7c\xa9\xa5\x59\x85\x0c\xe9\x7a\x04\xa9\x6d\xef\x6d\x99"
  17067. "\xa9\xe0\xe0\xe2\xab\x14\xe6\xb8\xdf\x26\x5f\xc0\xb3";
  17068. e.inLen = XSTRLEN(e.input);
  17069. e.outLen = WC_SHA224_DIGEST_SIZE;
  17070. EVP_MD_CTX_init(&md_ctx);
  17071. ret = EVP_DigestInit(&md_ctx, EVP_sha224());
  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_SHA224_DIGEST_SIZE) != 0) {
  17080. return -8603;
  17081. }
  17082. #endif /* WOLFSSL_SHA224 */
  17083. #ifndef NO_SHA256
  17084. d.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  17085. d.output = "\x24\x8D\x6A\x61\xD2\x06\x38\xB8\xE5\xC0\x26\x93\x0C\x3E\x60"
  17086. "\x39\xA3\x3C\xE4\x59\x64\xFF\x21\x67\xF6\xEC\xED\xD4\x19\xDB"
  17087. "\x06\xC1";
  17088. d.inLen = XSTRLEN(d.input);
  17089. d.outLen = WC_SHA256_DIGEST_SIZE;
  17090. EVP_MD_CTX_init(&md_ctx);
  17091. ret = EVP_DigestInit(&md_ctx, EVP_sha256());
  17092. if (ret == WOLFSSL_SUCCESS) {
  17093. ret = EVP_DigestUpdate(&md_ctx, d.input, (unsigned long)d.inLen);
  17094. if (ret == WOLFSSL_SUCCESS)
  17095. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  17096. }
  17097. EVP_MD_CTX_cleanup(&md_ctx);
  17098. if (ret != WOLFSSL_SUCCESS ||
  17099. XMEMCMP(hash, d.output, WC_SHA256_DIGEST_SIZE) != 0) {
  17100. return -8604;
  17101. }
  17102. #endif /* !NO_SHA256 */
  17103. #ifdef WOLFSSL_SHA384
  17104. e.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  17105. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  17106. e.output = "\x09\x33\x0c\x33\xf7\x11\x47\xe8\x3d\x19\x2f\xc7\x82\xcd\x1b"
  17107. "\x47\x53\x11\x1b\x17\x3b\x3b\x05\xd2\x2f\xa0\x80\x86\xe3\xb0"
  17108. "\xf7\x12\xfc\xc7\xc7\x1a\x55\x7e\x2d\xb9\x66\xc3\xe9\xfa\x91"
  17109. "\x74\x60\x39";
  17110. e.inLen = XSTRLEN(e.input);
  17111. e.outLen = WC_SHA384_DIGEST_SIZE;
  17112. EVP_MD_CTX_init(&md_ctx);
  17113. ret = EVP_DigestInit(&md_ctx, EVP_sha384());
  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_SHA384_DIGEST_SIZE) != 0) {
  17122. return -8605;
  17123. }
  17124. #endif /* WOLFSSL_SHA384 */
  17125. #ifdef WOLFSSL_SHA512
  17126. f.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  17127. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  17128. f.output = "\x8e\x95\x9b\x75\xda\xe3\x13\xda\x8c\xf4\xf7\x28\x14\xfc\x14"
  17129. "\x3f\x8f\x77\x79\xc6\xeb\x9f\x7f\xa1\x72\x99\xae\xad\xb6\x88"
  17130. "\x90\x18\x50\x1d\x28\x9e\x49\x00\xf7\xe4\x33\x1b\x99\xde\xc4"
  17131. "\xb5\x43\x3a\xc7\xd3\x29\xee\xb6\xdd\x26\x54\x5e\x96\xe5\x5b"
  17132. "\x87\x4b\xe9\x09";
  17133. f.inLen = XSTRLEN(f.input);
  17134. f.outLen = WC_SHA512_DIGEST_SIZE;
  17135. EVP_MD_CTX_init(&md_ctx);
  17136. ret = EVP_DigestInit(&md_ctx, EVP_sha512());
  17137. if (ret == WOLFSSL_SUCCESS) {
  17138. ret = EVP_DigestUpdate(&md_ctx, f.input, (unsigned long)f.inLen);
  17139. if (ret == WOLFSSL_SUCCESS)
  17140. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  17141. }
  17142. EVP_MD_CTX_cleanup(&md_ctx);
  17143. if (ret != WOLFSSL_SUCCESS ||
  17144. XMEMCMP(hash, f.output, WC_SHA512_DIGEST_SIZE) != 0) {
  17145. return -8606;
  17146. }
  17147. #endif /* WOLFSSL_SHA512 */
  17148. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  17149. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  17150. f.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  17151. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  17152. f.output = "\x23\xfe\xc5\xbb\x94\xd6\x0b\x23\x30\x81\x92\x64\x0b\x0c\x45"
  17153. "\x33\x35\xd6\x64\x73\x4f\xe4\x0e\x72\x68\x67\x4a\xf9";
  17154. f.inLen = XSTRLEN(f.input);
  17155. f.outLen = WC_SHA512_224_DIGEST_SIZE;
  17156. EVP_MD_CTX_init(&md_ctx);
  17157. ret = EVP_DigestInit(&md_ctx, EVP_sha512_224());
  17158. if (ret == WOLFSSL_SUCCESS) {
  17159. ret = EVP_DigestUpdate(&md_ctx, f.input, (unsigned long)f.inLen);
  17160. if (ret == WOLFSSL_SUCCESS)
  17161. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  17162. }
  17163. EVP_MD_CTX_cleanup(&md_ctx);
  17164. if (ret != WOLFSSL_SUCCESS ||
  17165. XMEMCMP(hash, f.output, WC_SHA512_224_DIGEST_SIZE) != 0) {
  17166. return -8722;
  17167. }
  17168. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_224 */
  17169. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  17170. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  17171. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  17172. f.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  17173. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  17174. f.output = "\x39\x28\xe1\x84\xfb\x86\x90\xf8\x40\xda\x39\x88\x12\x1d\x31"
  17175. "\xbe\x65\xcb\x9d\x3e\xf8\x3e\xe6\x14\x6f\xea\xc8\x61\xe1\x9b"
  17176. "\x56\x3a";
  17177. f.inLen = XSTRLEN(f.input);
  17178. f.outLen = WC_SHA512_256_DIGEST_SIZE;
  17179. EVP_MD_CTX_init(&md_ctx);
  17180. ret = EVP_DigestInit(&md_ctx, EVP_sha512_256());
  17181. if (ret == WOLFSSL_SUCCESS) {
  17182. ret = EVP_DigestUpdate(&md_ctx, f.input, (unsigned long)f.inLen);
  17183. if (ret == WOLFSSL_SUCCESS)
  17184. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  17185. }
  17186. EVP_MD_CTX_cleanup(&md_ctx);
  17187. if (ret != WOLFSSL_SUCCESS ||
  17188. XMEMCMP(hash, f.output, WC_SHA512_256_DIGEST_SIZE) != 0) {
  17189. return -8723;
  17190. }
  17191. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_224 */
  17192. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  17193. #ifdef WOLFSSL_SHA3
  17194. #ifndef WOLFSSL_NOSHA3_224
  17195. e.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  17196. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  17197. e.output = "\x54\x3e\x68\x68\xe1\x66\x6c\x1a\x64\x36\x30\xdf\x77\x36\x7a"
  17198. "\xe5\xa6\x2a\x85\x07\x0a\x51\xc1\x4c\xbf\x66\x5c\xbc";
  17199. e.inLen = XSTRLEN(e.input);
  17200. e.outLen = WC_SHA3_224_DIGEST_SIZE;
  17201. EVP_MD_CTX_init(&md_ctx);
  17202. ret = EVP_DigestInit(&md_ctx, EVP_sha3_224());
  17203. if (ret == WOLFSSL_SUCCESS) {
  17204. ret = EVP_DigestUpdate(&md_ctx, e.input, (unsigned long)e.inLen);
  17205. if (ret == WOLFSSL_SUCCESS)
  17206. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  17207. }
  17208. EVP_MD_CTX_cleanup(&md_ctx);
  17209. if (ret != WOLFSSL_SUCCESS ||
  17210. XMEMCMP(hash, e.output, WC_SHA3_224_DIGEST_SIZE) != 0) {
  17211. return -8607;
  17212. }
  17213. #endif /* WOLFSSL_NOSHA3_224 */
  17214. #ifndef WOLFSSL_NOSHA3_256
  17215. d.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  17216. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  17217. d.output = "\x91\x6f\x60\x61\xfe\x87\x97\x41\xca\x64\x69\xb4\x39\x71\xdf"
  17218. "\xdb\x28\xb1\xa3\x2d\xc3\x6c\xb3\x25\x4e\x81\x2b\xe2\x7a\xad"
  17219. "\x1d\x18";
  17220. d.inLen = XSTRLEN(d.input);
  17221. d.outLen = WC_SHA3_256_DIGEST_SIZE;
  17222. EVP_MD_CTX_init(&md_ctx);
  17223. ret = EVP_DigestInit(&md_ctx, EVP_sha3_256());
  17224. if (ret == WOLFSSL_SUCCESS) {
  17225. ret = EVP_DigestUpdate(&md_ctx, d.input, (unsigned long)d.inLen);
  17226. if (ret == WOLFSSL_SUCCESS)
  17227. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  17228. }
  17229. EVP_MD_CTX_cleanup(&md_ctx);
  17230. if (ret != WOLFSSL_SUCCESS ||
  17231. XMEMCMP(hash, d.output, WC_SHA3_256_DIGEST_SIZE) != 0) {
  17232. return -8608;
  17233. }
  17234. #endif /* WOLFSSL_NOSHA3_256 */
  17235. e.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  17236. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  17237. e.output = "\x79\x40\x7d\x3b\x59\x16\xb5\x9c\x3e\x30\xb0\x98\x22\x97\x47"
  17238. "\x91\xc3\x13\xfb\x9e\xcc\x84\x9e\x40\x6f\x23\x59\x2d\x04\xf6"
  17239. "\x25\xdc\x8c\x70\x9b\x98\xb4\x3b\x38\x52\xb3\x37\x21\x61\x79"
  17240. "\xaa\x7f\xc7";
  17241. e.inLen = XSTRLEN(e.input);
  17242. e.outLen = WC_SHA3_384_DIGEST_SIZE;
  17243. EVP_MD_CTX_init(&md_ctx);
  17244. ret = EVP_DigestInit(&md_ctx, EVP_sha3_384());
  17245. if (ret == WOLFSSL_SUCCESS) {
  17246. ret = EVP_DigestUpdate(&md_ctx, e.input, (unsigned long)e.inLen);
  17247. if (ret == WOLFSSL_SUCCESS)
  17248. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  17249. }
  17250. EVP_MD_CTX_cleanup(&md_ctx);
  17251. if (ret != WOLFSSL_SUCCESS ||
  17252. XMEMCMP(hash, e.output, WC_SHA3_384_DIGEST_SIZE) != 0) {
  17253. return -8609;
  17254. }
  17255. #ifndef WOLFSSL_NOSHA3_512
  17256. f.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  17257. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  17258. f.output = "\xaf\xeb\xb2\xef\x54\x2e\x65\x79\xc5\x0c\xad\x06\xd2\xe5\x78"
  17259. "\xf9\xf8\xdd\x68\x81\xd7\xdc\x82\x4d\x26\x36\x0f\xee\xbf\x18"
  17260. "\xa4\xfa\x73\xe3\x26\x11\x22\x94\x8e\xfc\xfd\x49\x2e\x74\xe8"
  17261. "\x2e\x21\x89\xed\x0f\xb4\x40\xd1\x87\xf3\x82\x27\x0c\xb4\x55"
  17262. "\xf2\x1d\xd1\x85";
  17263. f.inLen = XSTRLEN(f.input);
  17264. f.outLen = WC_SHA3_512_DIGEST_SIZE;
  17265. EVP_MD_CTX_init(&md_ctx);
  17266. ret = EVP_DigestInit(&md_ctx, EVP_sha3_512());
  17267. if (ret == WOLFSSL_SUCCESS) {
  17268. ret = EVP_DigestUpdate(&md_ctx, f.input, (unsigned long)f.inLen);
  17269. if (ret == WOLFSSL_SUCCESS)
  17270. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  17271. }
  17272. EVP_MD_CTX_cleanup(&md_ctx);
  17273. if (ret != WOLFSSL_SUCCESS ||
  17274. XMEMCMP(hash, f.output, WC_SHA3_512_DIGEST_SIZE) != 0) {
  17275. return -8610;
  17276. }
  17277. #endif /* WOLFSSL_NOSHA3_512 */
  17278. #endif /* WOLFSSL_SHA3 */
  17279. #ifndef WC_NO_RNG
  17280. if (RAND_bytes(hash, sizeof(hash)) != WOLFSSL_SUCCESS)
  17281. return -8611;
  17282. #endif
  17283. #ifndef NO_MD5
  17284. c.input = "what do ya want for nothing?";
  17285. c.output = "\x55\x78\xe8\x48\x4b\xcc\x93\x80\x93\xec\x53\xaf\x22\xd6\x14"
  17286. "\x76";
  17287. c.inLen = XSTRLEN(c.input);
  17288. c.outLen = WC_MD5_DIGEST_SIZE;
  17289. #if defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)
  17290. /* Expect failure with MD5 + HMAC when using FIPS 140-3. */
  17291. if (HMAC(EVP_md5(), "JefeJefeJefeJefe", 16, (byte*)c.input, (int)c.inLen,
  17292. hash, 0) != NULL)
  17293. #else
  17294. if (HMAC(EVP_md5(), "JefeJefeJefeJefe", 16, (byte*)c.input, (int)c.inLen,
  17295. hash, 0) == NULL ||
  17296. XMEMCMP(hash, c.output, WC_MD5_DIGEST_SIZE) != 0)
  17297. #endif
  17298. {
  17299. return -8612;
  17300. }
  17301. #endif /* NO_MD5 */
  17302. #ifndef NO_DES3
  17303. { /* des test */
  17304. WOLFSSL_SMALL_STACK_STATIC const byte vector[] = { /* "now is the time for all " w/o trailing 0 */
  17305. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  17306. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  17307. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  17308. };
  17309. byte plain[24];
  17310. byte cipher[24];
  17311. const_DES_cblock key = {
  17312. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
  17313. };
  17314. DES_cblock iv = {
  17315. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef
  17316. };
  17317. DES_key_schedule sched;
  17318. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  17319. 0x8b,0x7c,0x52,0xb0,0x01,0x2b,0x6c,0xb8,
  17320. 0x4f,0x0f,0xeb,0xf3,0xfb,0x5f,0x86,0x73,
  17321. 0x15,0x85,0xb3,0x22,0x4b,0x86,0x2b,0x4b
  17322. };
  17323. DES_key_sched(&key, &sched);
  17324. DES_cbc_encrypt(vector, cipher, sizeof(vector), &sched, &iv, DES_ENCRYPT);
  17325. DES_cbc_encrypt(cipher, plain, sizeof(vector), &sched, &iv, DES_DECRYPT);
  17326. if (XMEMCMP(plain, vector, sizeof(vector)) != 0)
  17327. return -8613;
  17328. if (XMEMCMP(cipher, verify, sizeof(verify)) != 0)
  17329. return -8614;
  17330. /* test changing iv */
  17331. DES_ncbc_encrypt(vector, cipher, 8, &sched, &iv, DES_ENCRYPT);
  17332. DES_ncbc_encrypt(vector + 8, cipher + 8, 16, &sched, &iv, DES_ENCRYPT);
  17333. if (XMEMCMP(cipher, verify, sizeof(verify)) != 0)
  17334. return -8615;
  17335. } /* end des test */
  17336. #endif /* NO_DES3 */
  17337. #if !defined(NO_AES) && !defined(WOLFCRYPT_ONLY)
  17338. if ((ret = openssl_aes_test()) != 0) {
  17339. return ret;
  17340. }
  17341. #if defined(WOLFSSL_AES_128) && defined(HAVE_AES_CBC)
  17342. { /* evp_cipher test: EVP_aes_128_cbc */
  17343. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17344. EVP_CIPHER_CTX *ctx = wolfSSL_EVP_CIPHER_CTX_new();
  17345. #else
  17346. EVP_CIPHER_CTX ctx[1];
  17347. #endif
  17348. int idx, cipherSz, plainSz;
  17349. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = { /* "Now is the time for all " w/o trailing 0 */
  17350. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  17351. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  17352. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  17353. };
  17354. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  17355. 0x95,0x94,0x92,0x57,0x5f,0x42,0x81,0x53,
  17356. 0x2c,0xcc,0x9d,0x46,0x77,0xa2,0x33,0xcb,
  17357. 0x3b,0x5d,0x41,0x97,0x94,0x25,0xa4,0xb4,
  17358. 0xae,0x7b,0x34,0xd0,0x3f,0x0c,0xbc,0x06
  17359. };
  17360. WOLFSSL_SMALL_STACK_STATIC const byte verify2[] = {
  17361. 0x95,0x94,0x92,0x57,0x5f,0x42,0x81,0x53,
  17362. 0x2c,0xcc,0x9d,0x46,0x77,0xa2,0x33,0xcb,
  17363. 0x7d,0x37,0x7b,0x0b,0x44,0xaa,0xb5,0xf0,
  17364. 0x5f,0x34,0xb4,0xde,0xb5,0xbd,0x2a,0xbb
  17365. };
  17366. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  17367. "0123456789abcdef "; /* align */
  17368. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  17369. "1234567890abcdef "; /* align */
  17370. byte cipher[AES_BLOCK_SIZE * 4];
  17371. byte plain [AES_BLOCK_SIZE * 4];
  17372. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17373. if (ctx == NULL)
  17374. return MEMORY_E;
  17375. #endif
  17376. cipherSz = 0;
  17377. EVP_CIPHER_CTX_init(ctx);
  17378. ret = EVP_CipherInit(ctx, EVP_aes_128_cbc(), key, iv, 1);
  17379. if (ret == WOLFSSL_SUCCESS) {
  17380. ret = EVP_CipherUpdate(ctx, cipher, &idx, (byte*)msg, sizeof(msg));
  17381. if (ret == WOLFSSL_SUCCESS)
  17382. cipherSz += idx;
  17383. }
  17384. if (ret == WOLFSSL_SUCCESS) {
  17385. ret = EVP_CipherFinal(ctx, cipher + cipherSz, &idx);
  17386. if (ret == WOLFSSL_SUCCESS)
  17387. cipherSz += idx;
  17388. }
  17389. EVP_CIPHER_CTX_cleanup(ctx);
  17390. if (ret != WOLFSSL_SUCCESS)
  17391. return -8617;
  17392. if (cipherSz != (int)sizeof(verify) || XMEMCMP(cipher, verify, cipherSz))
  17393. return -8618;
  17394. /* check partial decrypt (not enough padding for full block) */
  17395. plainSz = 0;
  17396. EVP_CIPHER_CTX_init(ctx);
  17397. ret = EVP_CipherInit(ctx, EVP_aes_128_cbc(), key, iv, 0);
  17398. if (ret == WOLFSSL_SUCCESS) {
  17399. ret = EVP_CipherUpdate(ctx, plain, &idx, cipher, 1);
  17400. if (ret == WOLFSSL_SUCCESS)
  17401. plainSz += idx;
  17402. }
  17403. if (ret == WOLFSSL_SUCCESS) {
  17404. /* this test should fail... not enough padding for full block */
  17405. ret = EVP_CipherFinal(ctx, plain + plainSz, &idx);
  17406. if (plainSz == 0 && ret != WOLFSSL_SUCCESS)
  17407. ret = WOLFSSL_SUCCESS;
  17408. else
  17409. ret = -8619;
  17410. }
  17411. else
  17412. ret = -8620;
  17413. EVP_CIPHER_CTX_cleanup(ctx);
  17414. if (ret != WOLFSSL_SUCCESS)
  17415. return ret;
  17416. plainSz = 0;
  17417. EVP_CIPHER_CTX_init(ctx);
  17418. ret = EVP_CipherInit(ctx, EVP_aes_128_cbc(), key, iv, 0);
  17419. if (ret == WOLFSSL_SUCCESS) {
  17420. ret = EVP_CipherUpdate(ctx, plain, &idx, cipher, cipherSz);
  17421. if (ret == WOLFSSL_SUCCESS)
  17422. plainSz += idx;
  17423. }
  17424. if (ret == WOLFSSL_SUCCESS) {
  17425. ret = EVP_CipherFinal(ctx, plain + plainSz, &idx);
  17426. if (ret == WOLFSSL_SUCCESS)
  17427. plainSz += idx;
  17428. }
  17429. EVP_CIPHER_CTX_cleanup(ctx);
  17430. if (ret != WOLFSSL_SUCCESS)
  17431. return -8621;
  17432. if (plainSz != (int)sizeof(msg) || XMEMCMP(plain, msg, sizeof(msg)))
  17433. return -8622;
  17434. cipherSz = 0;
  17435. EVP_CIPHER_CTX_init(ctx);
  17436. ret = EVP_CipherInit(ctx, EVP_aes_128_cbc(), key, iv, 1);
  17437. if (ret == WOLFSSL_SUCCESS) {
  17438. ret = EVP_CipherUpdate(ctx, cipher, &idx, msg, AES_BLOCK_SIZE);
  17439. if (ret == WOLFSSL_SUCCESS)
  17440. cipherSz += idx;
  17441. }
  17442. if (ret == WOLFSSL_SUCCESS) {
  17443. ret = EVP_CipherFinal(ctx, cipher + cipherSz, &idx);
  17444. if (ret == WOLFSSL_SUCCESS)
  17445. cipherSz += idx;
  17446. }
  17447. EVP_CIPHER_CTX_cleanup(ctx);
  17448. if (ret != WOLFSSL_SUCCESS)
  17449. return -8623;
  17450. if (cipherSz != (int)sizeof(verify2) || XMEMCMP(cipher, verify2, cipherSz))
  17451. return -8624;
  17452. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17453. wolfSSL_EVP_CIPHER_CTX_free(ctx);
  17454. #endif
  17455. } /* end evp_cipher test: EVP_aes_128_cbc*/
  17456. #endif /* WOLFSSL_AES_128 && HAVE_AES_CBC */
  17457. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_256)
  17458. { /* evp_cipher test: EVP_aes_256_ecb*/
  17459. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17460. EVP_CIPHER_CTX *ctx = wolfSSL_EVP_CIPHER_CTX_new();
  17461. #else
  17462. EVP_CIPHER_CTX ctx[1];
  17463. #endif
  17464. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = {
  17465. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  17466. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  17467. };
  17468. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  17469. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  17470. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  17471. };
  17472. WOLFSSL_SMALL_STACK_STATIC const byte key[] = {
  17473. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  17474. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  17475. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  17476. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  17477. };
  17478. byte cipher[AES_BLOCK_SIZE * 4];
  17479. byte plain [AES_BLOCK_SIZE * 4];
  17480. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17481. if (ctx == NULL)
  17482. return MEMORY_E;
  17483. #endif
  17484. EVP_CIPHER_CTX_init(ctx);
  17485. ret = EVP_CipherInit(ctx, EVP_aes_256_ecb(), (unsigned char*)key, NULL, 1);
  17486. if (ret == WOLFSSL_SUCCESS)
  17487. ret = EVP_Cipher(ctx, cipher, (byte*)msg, 16);
  17488. EVP_CIPHER_CTX_cleanup(ctx);
  17489. if (ret != 16)
  17490. return -8625;
  17491. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE))
  17492. return -8626;
  17493. EVP_CIPHER_CTX_init(ctx);
  17494. ret = EVP_CipherInit(ctx, EVP_aes_256_ecb(), (unsigned char*)key, NULL, 0);
  17495. if (ret == WOLFSSL_SUCCESS)
  17496. ret = EVP_Cipher(ctx, plain, cipher, 16);
  17497. EVP_CIPHER_CTX_cleanup(ctx);
  17498. if (ret != 16)
  17499. return -8627;
  17500. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  17501. return -8628;
  17502. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17503. wolfSSL_EVP_CIPHER_CTX_free(ctx);
  17504. #endif
  17505. } /* end evp_cipher test */
  17506. #endif /* HAVE_AES_ECB && WOLFSSL_AES_128 */
  17507. #define OPENSSL_TEST_ERROR (-10000)
  17508. #if defined(WOLFSSL_AES_DIRECT) && defined(WOLFSSL_AES_256)
  17509. /* enable HAVE_AES_DECRYPT for AES_encrypt/decrypt */
  17510. {
  17511. /* Test: AES_encrypt/decrypt/set Key */
  17512. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17513. AES_KEY *enc = (AES_KEY *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  17514. #ifdef HAVE_AES_DECRYPT
  17515. AES_KEY *dec = (AES_KEY *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  17516. #endif
  17517. #else
  17518. AES_KEY enc[1];
  17519. #ifdef HAVE_AES_DECRYPT
  17520. AES_KEY dec[1];
  17521. #endif
  17522. #endif
  17523. WOLFSSL_SMALL_STACK_STATIC const byte msg[] =
  17524. {
  17525. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  17526. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  17527. };
  17528. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  17529. {
  17530. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  17531. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  17532. };
  17533. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  17534. {
  17535. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  17536. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  17537. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  17538. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  17539. };
  17540. byte plain[sizeof(msg)];
  17541. byte cipher[sizeof(msg)];
  17542. printf("openSSL extra test\n") ;
  17543. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17544. if (enc == NULL)
  17545. return MEMORY_E;
  17546. #ifdef HAVE_AES_DECRYPT
  17547. if (dec == NULL)
  17548. return MEMORY_E;
  17549. #endif
  17550. #endif
  17551. AES_set_encrypt_key(key, sizeof(key)*8, enc);
  17552. AES_set_decrypt_key(key, sizeof(key)*8, dec);
  17553. AES_encrypt(msg, cipher, enc);
  17554. #ifdef HAVE_AES_DECRYPT
  17555. AES_decrypt(cipher, plain, dec);
  17556. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  17557. return OPENSSL_TEST_ERROR-60;
  17558. #endif /* HAVE_AES_DECRYPT */
  17559. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE))
  17560. return OPENSSL_TEST_ERROR-61;
  17561. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17562. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  17563. #ifdef HAVE_AES_DECRYPT
  17564. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  17565. #endif
  17566. #endif
  17567. }
  17568. #endif /* WOLFSSL_AES_DIRECT && WOLFSSL_AES_256 */
  17569. /* EVP_Cipher with EVP_aes_xxx_ctr() */
  17570. #ifdef WOLFSSL_AES_COUNTER
  17571. {
  17572. byte plainBuff [64];
  17573. byte cipherBuff[64];
  17574. #ifdef WOLFSSL_AES_128
  17575. WOLFSSL_SMALL_STACK_STATIC const byte ctrKey[] =
  17576. {
  17577. 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,
  17578. 0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c
  17579. };
  17580. WOLFSSL_SMALL_STACK_STATIC const byte ctrIv[] =
  17581. {
  17582. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  17583. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  17584. };
  17585. WOLFSSL_SMALL_STACK_STATIC const byte ctrPlain[] =
  17586. {
  17587. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  17588. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  17589. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  17590. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  17591. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  17592. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  17593. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  17594. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  17595. };
  17596. WOLFSSL_SMALL_STACK_STATIC const byte ctrCipher[] =
  17597. {
  17598. 0x87,0x4d,0x61,0x91,0xb6,0x20,0xe3,0x26,
  17599. 0x1b,0xef,0x68,0x64,0x99,0x0d,0xb6,0xce,
  17600. 0x98,0x06,0xf6,0x6b,0x79,0x70,0xfd,0xff,
  17601. 0x86,0x17,0x18,0x7b,0xb9,0xff,0xfd,0xff,
  17602. 0x5a,0xe4,0xdf,0x3e,0xdb,0xd5,0xd3,0x5e,
  17603. 0x5b,0x4f,0x09,0x02,0x0d,0xb0,0x3e,0xab,
  17604. 0x1e,0x03,0x1d,0xda,0x2f,0xbe,0x03,0xd1,
  17605. 0x79,0x21,0x70,0xa0,0xf3,0x00,0x9c,0xee
  17606. };
  17607. WOLFSSL_SMALL_STACK_STATIC const byte oddCipher[] =
  17608. {
  17609. 0xb9,0xd7,0xcb,0x08,0xb0,0xe1,0x7b,0xa0,
  17610. 0xc2
  17611. };
  17612. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17613. EVP_CIPHER_CTX *p_en;
  17614. EVP_CIPHER_CTX *p_de;
  17615. #endif
  17616. #endif /* WOLFSSL_AES_128 */
  17617. #ifdef WOLFSSL_AES_192
  17618. /* test vector from "Recommendation for Block Cipher Modes of Operation"
  17619. * NIST Special Publication 800-38A */
  17620. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Key[] =
  17621. {
  17622. 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  17623. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  17624. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b
  17625. };
  17626. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Iv[] =
  17627. {
  17628. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  17629. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  17630. };
  17631. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Plain[] =
  17632. {
  17633. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  17634. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  17635. };
  17636. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Cipher[] =
  17637. {
  17638. 0x1a,0xbc,0x93,0x24,0x17,0x52,0x1c,0xa2,
  17639. 0x4f,0x2b,0x04,0x59,0xfe,0x7e,0x6e,0x0b
  17640. };
  17641. #endif /* WOLFSSL_AES_192 */
  17642. #ifdef WOLFSSL_AES_256
  17643. /* test vector from "Recommendation for Block Cipher Modes of Operation"
  17644. * NIST Special Publication 800-38A */
  17645. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Key[] =
  17646. {
  17647. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  17648. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  17649. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  17650. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  17651. };
  17652. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Iv[] =
  17653. {
  17654. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  17655. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  17656. };
  17657. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Plain[] =
  17658. {
  17659. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  17660. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  17661. };
  17662. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Cipher[] =
  17663. {
  17664. 0x60,0x1e,0xc3,0x13,0x77,0x57,0x89,0xa5,
  17665. 0xb7,0xa7,0xf5,0x04,0xbb,0xf3,0xd2,0x28
  17666. };
  17667. #endif /* WOLFSSL_AES_256 */
  17668. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17669. EVP_CIPHER_CTX *en = wolfSSL_EVP_CIPHER_CTX_new();
  17670. EVP_CIPHER_CTX *de = wolfSSL_EVP_CIPHER_CTX_new();
  17671. if ((en == NULL) || (de == NULL))
  17672. return MEMORY_E;
  17673. #else
  17674. EVP_CIPHER_CTX en[1];
  17675. EVP_CIPHER_CTX de[1];
  17676. #endif
  17677. #ifdef WOLFSSL_AES_128
  17678. EVP_CIPHER_CTX_init(en);
  17679. if (EVP_CipherInit(en, EVP_aes_128_ctr(),
  17680. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  17681. return -8629;
  17682. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctrPlain,
  17683. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  17684. return -8630;
  17685. EVP_CIPHER_CTX_init(de);
  17686. if (EVP_CipherInit(de, EVP_aes_128_ctr(),
  17687. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  17688. return -8631;
  17689. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff,
  17690. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  17691. return -8632;
  17692. if (XMEMCMP(cipherBuff, ctrCipher, AES_BLOCK_SIZE*4))
  17693. return -8633;
  17694. if (XMEMCMP(plainBuff, ctrPlain, AES_BLOCK_SIZE*4))
  17695. return -8634;
  17696. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17697. p_en = wolfSSL_EVP_CIPHER_CTX_new();
  17698. if (p_en == NULL)
  17699. return -8635;
  17700. p_de = wolfSSL_EVP_CIPHER_CTX_new();
  17701. if (p_de == NULL)
  17702. return -8636;
  17703. if (EVP_CipherInit(p_en, EVP_aes_128_ctr(),
  17704. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  17705. return -8637;
  17706. if (EVP_Cipher(p_en, (byte*)cipherBuff, (byte*)ctrPlain,
  17707. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  17708. return -8638;
  17709. if (EVP_CipherInit(p_de, EVP_aes_128_ctr(),
  17710. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  17711. return -8639;
  17712. if (EVP_Cipher(p_de, (byte*)plainBuff, (byte*)cipherBuff,
  17713. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  17714. return -8640;
  17715. wolfSSL_EVP_CIPHER_CTX_free(p_en);
  17716. wolfSSL_EVP_CIPHER_CTX_free(p_de);
  17717. #endif /* WOLFSSL_SMALL_STACK && !WOLFSSL_NO_MALLOC */
  17718. if (XMEMCMP(cipherBuff, ctrCipher, AES_BLOCK_SIZE*4))
  17719. return -8641;
  17720. if (XMEMCMP(plainBuff, ctrPlain, AES_BLOCK_SIZE*4))
  17721. return -8642;
  17722. EVP_CIPHER_CTX_init(en);
  17723. if (EVP_CipherInit(en, EVP_aes_128_ctr(),
  17724. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  17725. return -8643;
  17726. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctrPlain, 9) != 9)
  17727. return -8644;
  17728. EVP_CIPHER_CTX_init(de);
  17729. if (EVP_CipherInit(de, EVP_aes_128_ctr(),
  17730. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  17731. return -8645;
  17732. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff, 9) != 9)
  17733. return -8646;
  17734. if (XMEMCMP(plainBuff, ctrPlain, 9))
  17735. return -8647;
  17736. if (XMEMCMP(cipherBuff, ctrCipher, 9))
  17737. return -8648;
  17738. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctrPlain, 9) != 9)
  17739. return -8649;
  17740. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff, 9) != 9)
  17741. return -8650;
  17742. if (XMEMCMP(plainBuff, ctrPlain, 9))
  17743. return -8651;
  17744. if (XMEMCMP(cipherBuff, oddCipher, 9))
  17745. return -8652;
  17746. #endif /* WOLFSSL_AES_128 */
  17747. #ifdef WOLFSSL_AES_192
  17748. EVP_CIPHER_CTX_init(en);
  17749. if (EVP_CipherInit(en, EVP_aes_192_ctr(),
  17750. (unsigned char*)ctr192Key, (unsigned char*)ctr192Iv, 0) == 0)
  17751. return -8653;
  17752. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctr192Plain,
  17753. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  17754. return -8654;
  17755. EVP_CIPHER_CTX_init(de);
  17756. if (EVP_CipherInit(de, EVP_aes_192_ctr(),
  17757. (unsigned char*)ctr192Key, (unsigned char*)ctr192Iv, 0) == 0)
  17758. return -8655;
  17759. XMEMSET(plainBuff, 0, sizeof(plainBuff));
  17760. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff,
  17761. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  17762. return -8656;
  17763. if (XMEMCMP(plainBuff, ctr192Plain, sizeof(ctr192Plain)))
  17764. return -8657;
  17765. if (XMEMCMP(ctr192Cipher, cipherBuff, sizeof(ctr192Cipher)))
  17766. return -8658;
  17767. #endif /* WOLFSSL_AES_192 */
  17768. #ifdef WOLFSSL_AES_256
  17769. EVP_CIPHER_CTX_init(en);
  17770. if (EVP_CipherInit(en, EVP_aes_256_ctr(),
  17771. (unsigned char*)ctr256Key, (unsigned char*)ctr256Iv, 0) == 0)
  17772. return -8659;
  17773. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctr256Plain,
  17774. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  17775. return -8660;
  17776. EVP_CIPHER_CTX_init(de);
  17777. if (EVP_CipherInit(de, EVP_aes_256_ctr(),
  17778. (unsigned char*)ctr256Key, (unsigned char*)ctr256Iv, 0) == 0)
  17779. return -8661;
  17780. XMEMSET(plainBuff, 0, sizeof(plainBuff));
  17781. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff,
  17782. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  17783. return -8662;
  17784. if (XMEMCMP(plainBuff, ctr256Plain, sizeof(ctr256Plain)))
  17785. return -8663;
  17786. if (XMEMCMP(ctr256Cipher, cipherBuff, sizeof(ctr256Cipher)))
  17787. return -8664;
  17788. #endif /* WOLFSSL_AES_256 */
  17789. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17790. wolfSSL_EVP_CIPHER_CTX_free(en);
  17791. wolfSSL_EVP_CIPHER_CTX_free(de);
  17792. #endif
  17793. }
  17794. #endif /* HAVE_AES_COUNTER */
  17795. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  17796. {
  17797. /* EVP_CipherUpdate test */
  17798. WOLFSSL_SMALL_STACK_STATIC const byte cbcPlain[] =
  17799. {
  17800. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  17801. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  17802. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  17803. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  17804. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  17805. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  17806. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  17807. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  17808. };
  17809. byte key[] = "0123456789abcdef "; /* align */
  17810. byte iv[] = "1234567890abcdef "; /* align */
  17811. byte cipher[AES_BLOCK_SIZE * 4];
  17812. byte plain [AES_BLOCK_SIZE * 4];
  17813. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17814. EVP_CIPHER_CTX *en = wolfSSL_EVP_CIPHER_CTX_new();
  17815. EVP_CIPHER_CTX *de = wolfSSL_EVP_CIPHER_CTX_new();
  17816. #else
  17817. EVP_CIPHER_CTX en[1];
  17818. EVP_CIPHER_CTX de[1];
  17819. #endif
  17820. int outlen ;
  17821. int total = 0;
  17822. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17823. if ((en == NULL) || (de == NULL))
  17824. return MEMORY_E;
  17825. #endif
  17826. EVP_CIPHER_CTX_init(en);
  17827. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  17828. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  17829. return -8665;
  17830. /* openSSL compatibility, if(inlen == 0)return 1; */
  17831. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen,
  17832. (byte*)cbcPlain, 0) != 1)
  17833. return -8666;
  17834. EVP_CIPHER_CTX_init(en);
  17835. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  17836. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  17837. return -8667;
  17838. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen,
  17839. (byte*)cbcPlain, 9) == 0)
  17840. return -8668;
  17841. if(outlen != 0)
  17842. return -8669;
  17843. total += outlen;
  17844. if (EVP_CipherUpdate(en, (byte*)&cipher[total], &outlen,
  17845. (byte*)&cbcPlain[9] , 9) == 0)
  17846. return -8670;
  17847. if(outlen != 16)
  17848. return -8671;
  17849. total += outlen;
  17850. if (EVP_CipherFinal(en, (byte*)&cipher[total], &outlen) == 0)
  17851. return -8672;
  17852. if(outlen != 16)
  17853. return -8673;
  17854. total += outlen;
  17855. if(total != 32)
  17856. return -8674;
  17857. total = 0;
  17858. EVP_CIPHER_CTX_init(de);
  17859. if (EVP_CipherInit(de, EVP_aes_128_cbc(),
  17860. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  17861. return -8675;
  17862. if (EVP_CipherUpdate(de, (byte*)plain, &outlen, (byte*)cipher, 6) == 0)
  17863. return -8676;
  17864. if(outlen != 0)
  17865. return -8677;
  17866. total += outlen;
  17867. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen,
  17868. (byte*)&cipher[6], 12) == 0)
  17869. return -8678;
  17870. if(outlen != 0)
  17871. total += outlen;
  17872. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen,
  17873. (byte*)&cipher[6+12], 14) == 0)
  17874. return -8679;
  17875. if(outlen != 16)
  17876. return -8680;
  17877. total += outlen;
  17878. if (EVP_CipherFinal(de, (byte*)&plain[total], &outlen) == 0)
  17879. return -8681;
  17880. if(outlen != 2)
  17881. return -8682;
  17882. total += outlen;
  17883. if(total != 18)
  17884. return -8683;
  17885. if (XMEMCMP(plain, cbcPlain, 18))
  17886. return -8684;
  17887. total = 0;
  17888. EVP_CIPHER_CTX_init(en);
  17889. if (EVP_EncryptInit(en, EVP_aes_128_cbc(),
  17890. (unsigned char*)key, (unsigned char*)iv) == 0)
  17891. return -8685;
  17892. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen, (byte*)cbcPlain, 9) == 0)
  17893. return -8686;
  17894. if(outlen != 0)
  17895. return -8687;
  17896. total += outlen;
  17897. if (EVP_CipherUpdate(en, (byte*)&cipher[total], &outlen, (byte*)&cbcPlain[9] , 9) == 0)
  17898. return -8688;
  17899. if(outlen != 16)
  17900. return -8689;
  17901. total += outlen;
  17902. if (EVP_EncryptFinal(en, (byte*)&cipher[total], &outlen) == 0)
  17903. return -8690;
  17904. if(outlen != 16)
  17905. return -8691;
  17906. total += outlen;
  17907. if(total != 32)
  17908. return 3438;
  17909. total = 0;
  17910. EVP_CIPHER_CTX_init(de);
  17911. if (EVP_DecryptInit(de, EVP_aes_128_cbc(),
  17912. (unsigned char*)key, (unsigned char*)iv) == 0)
  17913. return -8692;
  17914. if (EVP_CipherUpdate(de, (byte*)plain, &outlen, (byte*)cipher, 6) == 0)
  17915. return -8693;
  17916. if(outlen != 0)
  17917. return -8694;
  17918. total += outlen;
  17919. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen, (byte*)&cipher[6], 12) == 0)
  17920. return -8695;
  17921. if(outlen != 0)
  17922. total += outlen;
  17923. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen, (byte*)&cipher[6+12], 14) == 0)
  17924. return -8696;
  17925. if(outlen != 16)
  17926. return -8697;
  17927. total += outlen;
  17928. if (EVP_DecryptFinal(de, (byte*)&plain[total], &outlen) == 0)
  17929. return -8698;
  17930. if(outlen != 2)
  17931. return -8699;
  17932. total += outlen;
  17933. if(total != 18)
  17934. return 3447;
  17935. if (XMEMCMP(plain, cbcPlain, 18))
  17936. return -8700;
  17937. if (EVP_CIPHER_key_length(NULL) != 0)
  17938. return -8701;
  17939. if (EVP_CIPHER_key_length(EVP_aes_128_cbc()) != 16)
  17940. return -8702;
  17941. if (EVP_CIPHER_CTX_mode(NULL) != 0)
  17942. return -8703;
  17943. if (EVP_CIPHER_CTX_mode(en) != (en->flags & WOLFSSL_EVP_CIPH_MODE))
  17944. return -8704;
  17945. EVP_CIPHER_CTX_init(en);
  17946. if (EVP_CipherInit_ex(en, EVP_aes_128_cbc(), NULL,
  17947. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  17948. return -8705;
  17949. EVP_CIPHER_CTX_init(en);
  17950. if (EVP_EncryptInit_ex(en, EVP_aes_128_cbc(), NULL,
  17951. (unsigned char*)key, (unsigned char*)iv) == 0)
  17952. return -8706;
  17953. if (wolfSSL_EVP_EncryptFinal_ex(NULL, NULL, NULL) != WOLFSSL_FAILURE)
  17954. return -8707;
  17955. if (wolfSSL_EVP_EncryptFinal(NULL, NULL, NULL) != WOLFSSL_FAILURE)
  17956. return -8708;
  17957. EVP_CIPHER_CTX_init(de);
  17958. if (EVP_DecryptInit_ex(de, EVP_aes_128_cbc(), NULL,
  17959. (unsigned char*)key, (unsigned char*)iv) == 0)
  17960. return -8709;
  17961. if (wolfSSL_EVP_DecryptFinal(NULL, NULL, NULL) != WOLFSSL_FAILURE)
  17962. return -8710;
  17963. if (wolfSSL_EVP_DecryptFinal_ex(NULL, NULL, NULL) != WOLFSSL_FAILURE)
  17964. return -8711;
  17965. if (EVP_CIPHER_CTX_block_size(NULL) != BAD_FUNC_ARG)
  17966. return -8712;
  17967. EVP_CIPHER_CTX_init(en);
  17968. EVP_EncryptInit_ex(en, EVP_aes_128_cbc(), NULL,
  17969. (unsigned char*)key, (unsigned char*)iv);
  17970. if (EVP_CIPHER_CTX_block_size(en) != en->block_size)
  17971. return -8713;
  17972. if (EVP_CIPHER_block_size(NULL) != BAD_FUNC_ARG)
  17973. return -8714;
  17974. if (EVP_CIPHER_block_size(EVP_aes_128_cbc()) != AES_BLOCK_SIZE)
  17975. return -8715;
  17976. if (WOLFSSL_EVP_CIPHER_mode(NULL) != 0)
  17977. return -8716;
  17978. if (EVP_CIPHER_flags(EVP_aes_128_cbc()) != WOLFSSL_EVP_CIPH_CBC_MODE)
  17979. return -8717;
  17980. EVP_CIPHER_CTX_clear_flags(en, 0xFFFFFFFF);
  17981. EVP_CIPHER_CTX_set_flags(en, 42);
  17982. if (en->flags != 42)
  17983. return -8718;
  17984. if (EVP_CIPHER_CTX_set_padding(NULL, 0) != BAD_FUNC_ARG)
  17985. return -8719;
  17986. if (EVP_CIPHER_CTX_set_padding(en, 0) != WOLFSSL_SUCCESS)
  17987. return -8720;
  17988. if (EVP_CIPHER_CTX_set_padding(en, 1) != WOLFSSL_SUCCESS)
  17989. return -8721;
  17990. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17991. wolfSSL_EVP_CIPHER_CTX_free(en);
  17992. wolfSSL_EVP_CIPHER_CTX_free(de);
  17993. #endif
  17994. }
  17995. #endif /* WOLFSSL_AES_128 && HAVE_AES_CBC */
  17996. #endif /* ifndef NO_AES */
  17997. return 0;
  17998. }
  17999. WOLFSSL_TEST_SUBROUTINE int openSSL_evpMD_test(void)
  18000. {
  18001. int ret = 0;
  18002. #if !defined(NO_SHA256) && !defined(NO_SHA)
  18003. WOLFSSL_EVP_MD_CTX* ctx;
  18004. WOLFSSL_EVP_MD_CTX* ctx2;
  18005. ctx = EVP_MD_CTX_create();
  18006. ctx2 = EVP_MD_CTX_create();
  18007. ret = EVP_DigestInit(ctx, EVP_sha256());
  18008. if (ret != SSL_SUCCESS) {
  18009. ret = -8800;
  18010. goto openSSL_evpMD_test_done;
  18011. }
  18012. ret = EVP_MD_CTX_copy(ctx2, ctx);
  18013. if (ret != SSL_SUCCESS) {
  18014. ret = -8801;
  18015. goto openSSL_evpMD_test_done;
  18016. }
  18017. if (EVP_MD_type(EVP_sha256()) != EVP_MD_CTX_type(ctx2)) {
  18018. ret = -8802;
  18019. goto openSSL_evpMD_test_done;
  18020. }
  18021. ret = EVP_DigestInit(ctx, EVP_sha1());
  18022. if (ret != SSL_SUCCESS) {
  18023. ret = -8803;
  18024. goto openSSL_evpMD_test_done;
  18025. }
  18026. if (EVP_MD_type(EVP_sha256()) != EVP_MD_CTX_type(ctx2)) {
  18027. ret = -8804;
  18028. goto openSSL_evpMD_test_done;
  18029. }
  18030. ret = EVP_MD_CTX_copy_ex(ctx2, ctx);
  18031. if (ret != SSL_SUCCESS) {
  18032. ret = -8805;
  18033. goto openSSL_evpMD_test_done;
  18034. }
  18035. if (EVP_MD_type(EVP_sha256()) == EVP_MD_CTX_type(ctx2)) {
  18036. ret = -8806;
  18037. goto openSSL_evpMD_test_done;
  18038. }
  18039. if (EVP_MD_type(EVP_sha1()) != EVP_MD_CTX_type(ctx2)) {
  18040. ret = -8807;
  18041. goto openSSL_evpMD_test_done;
  18042. }
  18043. if (EVP_DigestInit_ex(ctx, EVP_sha1(), NULL) != SSL_SUCCESS) {
  18044. ret = -8808;
  18045. goto openSSL_evpMD_test_done;
  18046. }
  18047. if (EVP_add_digest(NULL) != 0) {
  18048. ret = -8809;
  18049. goto openSSL_evpMD_test_done;
  18050. }
  18051. if (wolfSSL_EVP_add_cipher(NULL) != 0) {
  18052. ret = -8810;
  18053. goto openSSL_evpMD_test_done;
  18054. }
  18055. ret = 0; /* got to success state without jumping to end with a fail */
  18056. openSSL_evpMD_test_done:
  18057. EVP_MD_CTX_destroy(ctx);
  18058. EVP_MD_CTX_destroy(ctx2);
  18059. #endif /* NO_SHA256 */
  18060. return ret;
  18061. }
  18062. #ifdef DEBUG_SIGN
  18063. static void show(const char *title, const char *p, unsigned int s) {
  18064. char* i;
  18065. printf("%s: ", title);
  18066. for (i = p;
  18067. i < p + s;
  18068. printf("%c", *i), i++);
  18069. printf("\n");
  18070. }
  18071. #else
  18072. #define show(a,b,c)
  18073. #endif
  18074. #define FOURK_BUFF 4096
  18075. #define ERR_BASE_PKEY (-5000)
  18076. WOLFSSL_TEST_SUBROUTINE int openssl_pkey0_test(void)
  18077. {
  18078. int ret = 0;
  18079. #if !defined(NO_RSA) && !defined(HAVE_USER_RSA) && !defined(NO_SHA)
  18080. byte* prvTmp;
  18081. byte* pubTmp;
  18082. int prvBytes;
  18083. int pubBytes;
  18084. RSA *prvRsa = NULL;
  18085. RSA *pubRsa = NULL;
  18086. EVP_PKEY *prvPkey = NULL;
  18087. EVP_PKEY *pubPkey = NULL;
  18088. EVP_PKEY_CTX *enc = NULL;
  18089. EVP_PKEY_CTX *dec = NULL;
  18090. byte in[] = TEST_STRING;
  18091. byte out[256];
  18092. size_t outlen;
  18093. size_t keySz;
  18094. byte plain[256];
  18095. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  18096. XFILE keyFile;
  18097. XFILE keypubFile;
  18098. char cliKey[] = "./certs/client-key.der";
  18099. char cliKeypub[] = "./certs/client-keyPub.der";
  18100. #endif
  18101. prvTmp = (byte*)XMALLOC(FOURK_BUFF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18102. if (prvTmp == NULL)
  18103. return ERR_BASE_PKEY-1;
  18104. pubTmp = (byte*)XMALLOC(FOURK_BUFF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18105. if (pubTmp == NULL) {
  18106. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18107. return ERR_BASE_PKEY-2;
  18108. }
  18109. #ifdef USE_CERT_BUFFERS_1024
  18110. XMEMCPY(prvTmp, client_key_der_1024, sizeof_client_key_der_1024);
  18111. prvBytes = sizeof_client_key_der_1024;
  18112. XMEMCPY(pubTmp, client_keypub_der_1024, sizeof_client_keypub_der_1024);
  18113. pubBytes = sizeof_client_keypub_der_1024;
  18114. #elif defined(USE_CERT_BUFFERS_2048)
  18115. XMEMCPY(prvTmp, client_key_der_2048, sizeof_client_key_der_2048);
  18116. prvBytes = sizeof_client_key_der_2048;
  18117. XMEMCPY(pubTmp, client_keypub_der_2048, sizeof_client_keypub_der_2048);
  18118. pubBytes = sizeof_client_keypub_der_2048;
  18119. #else
  18120. keyFile = XFOPEN(cliKey, "rb");
  18121. if (!keyFile) {
  18122. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18123. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18124. err_sys("can't open ./certs/client-key.der, "
  18125. "Please run from wolfSSL home dir", ERR_BASE_PKEY-3);
  18126. return ERR_BASE_PKEY-3;
  18127. }
  18128. prvBytes = (int)XFREAD(prvTmp, 1, (int)FOURK_BUFF, keyFile);
  18129. XFCLOSE(keyFile);
  18130. keypubFile = XFOPEN(cliKeypub, "rb");
  18131. if (!keypubFile) {
  18132. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18133. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18134. err_sys("can't open ./certs/client-cert.der, "
  18135. "Please run from wolfSSL home dir", -4);
  18136. return ERR_BASE_PKEY-4;
  18137. }
  18138. pubBytes = (int)XFREAD(pubTmp, 1, (int)FOURK_BUFF, keypubFile);
  18139. XFCLOSE(keypubFile);
  18140. #endif /* USE_CERT_BUFFERS */
  18141. prvRsa = wolfSSL_RSA_new();
  18142. pubRsa = wolfSSL_RSA_new();
  18143. if((prvRsa == NULL) || (pubRsa == NULL)){
  18144. printf("error with RSA_new\n");
  18145. ret = ERR_BASE_PKEY-10;
  18146. goto openssl_pkey0_test_done;
  18147. }
  18148. ret = wolfSSL_RSA_LoadDer_ex(prvRsa, prvTmp, prvBytes, WOLFSSL_RSA_LOAD_PRIVATE);
  18149. if(ret != SSL_SUCCESS){
  18150. printf("error with RSA_LoadDer_ex\n");
  18151. ret = ERR_BASE_PKEY-11;
  18152. goto openssl_pkey0_test_done;
  18153. }
  18154. ret = wolfSSL_RSA_LoadDer_ex(pubRsa, pubTmp, pubBytes, WOLFSSL_RSA_LOAD_PUBLIC);
  18155. if(ret != SSL_SUCCESS){
  18156. printf("error with RSA_LoadDer_ex\n");
  18157. ret = ERR_BASE_PKEY-12;
  18158. goto openssl_pkey0_test_done;
  18159. }
  18160. keySz = (size_t)RSA_size(pubRsa);
  18161. prvPkey = wolfSSL_EVP_PKEY_new();
  18162. pubPkey = wolfSSL_EVP_PKEY_new();
  18163. if((prvPkey == NULL) || (pubPkey == NULL)){
  18164. printf("error with PKEY_new\n");
  18165. ret = ERR_BASE_PKEY-13;
  18166. goto openssl_pkey0_test_done;
  18167. }
  18168. ret = wolfSSL_EVP_PKEY_set1_RSA(prvPkey, prvRsa);
  18169. ret += wolfSSL_EVP_PKEY_set1_RSA(pubPkey, pubRsa);
  18170. if(ret != 2){
  18171. printf("error with PKEY_set1_RSA\n");
  18172. ret = ERR_BASE_PKEY-14;
  18173. goto openssl_pkey0_test_done;
  18174. }
  18175. dec = EVP_PKEY_CTX_new(prvPkey, NULL);
  18176. enc = EVP_PKEY_CTX_new(pubPkey, NULL);
  18177. if((dec == NULL)||(enc==NULL)){
  18178. printf("error with EVP_PKEY_CTX_new\n");
  18179. ret = ERR_BASE_PKEY-15;
  18180. goto openssl_pkey0_test_done;
  18181. }
  18182. ret = EVP_PKEY_decrypt_init(dec);
  18183. if (ret != 1) {
  18184. printf("error with decrypt init\n");
  18185. ret = ERR_BASE_PKEY-16;
  18186. goto openssl_pkey0_test_done;
  18187. }
  18188. ret = EVP_PKEY_encrypt_init(enc);
  18189. if (ret != 1) {
  18190. printf("error with encrypt init\n");
  18191. ret = ERR_BASE_PKEY-17;
  18192. goto openssl_pkey0_test_done;
  18193. }
  18194. XMEMSET(out, 0, sizeof(out));
  18195. ret = EVP_PKEY_encrypt(enc, out, &outlen, in, sizeof(in));
  18196. if (ret != 1) {
  18197. printf("error encrypting msg\n");
  18198. ret = ERR_BASE_PKEY-18;
  18199. goto openssl_pkey0_test_done;
  18200. }
  18201. show("encrypted msg", out, outlen);
  18202. XMEMSET(plain, 0, sizeof(plain));
  18203. ret = EVP_PKEY_decrypt(dec, plain, &outlen, out, keySz);
  18204. if (ret != 1) {
  18205. printf("error decrypting msg\n");
  18206. ret = ERR_BASE_PKEY-19;
  18207. goto openssl_pkey0_test_done;
  18208. }
  18209. show("decrypted msg", plain, outlen);
  18210. /* RSA_PKCS1_OAEP_PADDING test */
  18211. ret = EVP_PKEY_decrypt_init(dec);
  18212. if (ret != 1) {
  18213. printf("error with decrypt init\n");
  18214. ret = ERR_BASE_PKEY-30;
  18215. goto openssl_pkey0_test_done;
  18216. }
  18217. ret = EVP_PKEY_encrypt_init(enc);
  18218. if (ret != 1) {
  18219. printf("error with encrypt init\n");
  18220. ret = ERR_BASE_PKEY-31;
  18221. goto openssl_pkey0_test_done;
  18222. }
  18223. if (EVP_PKEY_CTX_set_rsa_padding(dec, RSA_PKCS1_PADDING) <= 0) {
  18224. printf("first set rsa padding error\n");
  18225. ret = ERR_BASE_PKEY-32;
  18226. goto openssl_pkey0_test_done;
  18227. }
  18228. #ifndef HAVE_FIPS
  18229. if (EVP_PKEY_CTX_set_rsa_padding(dec, RSA_PKCS1_OAEP_PADDING) <= 0){
  18230. printf("second set rsa padding error\n");
  18231. ret = ERR_BASE_PKEY-33;
  18232. goto openssl_pkey0_test_done;
  18233. }
  18234. if (EVP_PKEY_CTX_set_rsa_padding(enc, RSA_PKCS1_OAEP_PADDING) <= 0) {
  18235. printf("third set rsa padding error\n");
  18236. ret = ERR_BASE_PKEY-34;
  18237. goto openssl_pkey0_test_done;
  18238. }
  18239. #endif
  18240. XMEMSET(out, 0, sizeof(out));
  18241. ret = EVP_PKEY_encrypt(enc, out, &outlen, in, sizeof(in));
  18242. if (ret != 1) {
  18243. printf("error encrypting msg\n");
  18244. ret = ERR_BASE_PKEY-35;
  18245. goto openssl_pkey0_test_done;
  18246. }
  18247. show("encrypted msg", out, outlen);
  18248. XMEMSET(plain, 0, sizeof(plain));
  18249. ret = EVP_PKEY_decrypt(dec, plain, &outlen, out, keySz);
  18250. if (ret != 1) {
  18251. printf("error decrypting msg\n");
  18252. ret = ERR_BASE_PKEY-36;
  18253. goto openssl_pkey0_test_done;
  18254. }
  18255. show("decrypted msg", plain, outlen);
  18256. ret = 0; /* made it to this point without error then set success */
  18257. openssl_pkey0_test_done:
  18258. wolfSSL_RSA_free(prvRsa);
  18259. wolfSSL_RSA_free(pubRsa);
  18260. EVP_PKEY_free(pubPkey);
  18261. EVP_PKEY_free(prvPkey);
  18262. EVP_PKEY_CTX_free(dec);
  18263. EVP_PKEY_CTX_free(enc);
  18264. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18265. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18266. #endif /* NO_RSA */
  18267. return ret;
  18268. }
  18269. WOLFSSL_TEST_SUBROUTINE int openssl_pkey1_test(void)
  18270. {
  18271. int ret = 0;
  18272. #if !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(HAVE_USER_RSA) && \
  18273. !defined(NO_SHA)
  18274. EVP_PKEY_CTX* dec = NULL;
  18275. EVP_PKEY_CTX* enc = NULL;
  18276. EVP_PKEY* pubKey = NULL;
  18277. EVP_PKEY* prvKey = NULL;
  18278. X509* x509 = NULL;
  18279. WOLFSSL_SMALL_STACK_STATIC const unsigned char msg[] = "sugar slapped";
  18280. const unsigned char* clikey;
  18281. long cliKeySz;
  18282. size_t outlen;
  18283. int keyLenBits = 2048;
  18284. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18285. unsigned char *tmp = (unsigned char *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18286. unsigned char *cipher = (unsigned char *)XMALLOC(RSA_TEST_BYTES, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18287. unsigned char *plain = (unsigned char *)XMALLOC(RSA_TEST_BYTES, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18288. if ((tmp == NULL) ||
  18289. (cipher == NULL) ||
  18290. (plain == NULL)) {
  18291. ret = -9015;
  18292. goto openssl_pkey1_test_done;
  18293. }
  18294. #else
  18295. unsigned char tmp[FOURK_BUF];
  18296. unsigned char cipher[RSA_TEST_BYTES];
  18297. unsigned char plain[RSA_TEST_BYTES];
  18298. #endif
  18299. #if defined(USE_CERT_BUFFERS_1024)
  18300. XMEMCPY(tmp, client_key_der_1024, sizeof_client_key_der_1024);
  18301. cliKeySz = (long)sizeof_client_key_der_1024;
  18302. x509 = wolfSSL_X509_load_certificate_buffer(client_cert_der_1024,
  18303. sizeof_client_cert_der_1024, SSL_FILETYPE_ASN1);
  18304. keyLenBits = 1024;
  18305. #elif defined(USE_CERT_BUFFERS_2048)
  18306. XMEMCPY(tmp, client_key_der_2048, sizeof_client_key_der_2048);
  18307. cliKeySz = (long)sizeof_client_key_der_2048;
  18308. x509 = wolfSSL_X509_load_certificate_buffer(client_cert_der_2048,
  18309. sizeof_client_cert_der_2048, SSL_FILETYPE_ASN1);
  18310. #elif defined(USE_CERT_BUFFERS_3072)
  18311. XMEMCPY(tmp, client_key_der_3072, sizeof_client_key_der_3072);
  18312. cliKeySz = (long)sizeof_client_key_der_3072;
  18313. x509 = wolfSSL_X509_load_certificate_buffer(client_cert_der_3072,
  18314. sizeof_client_cert_der_3072, SSL_FILETYPE_ASN1);
  18315. keyLenBits = 3072;
  18316. #elif defined(USE_CERT_BUFFERS_4096)
  18317. XMEMCPY(tmp, client_key_der_4096, sizeof_client_key_der_4096);
  18318. cliKeySz = (long)sizeof_client_key_der_4096;
  18319. x509 = wolfSSL_X509_load_certificate_buffer(client_cert_der_4096,
  18320. sizeof_client_cert_der_4096, SSL_FILETYPE_ASN1);
  18321. keyLenBits = 4096;
  18322. #else
  18323. {
  18324. XFILE f;
  18325. f = XFOPEN(clientKey, "rb");
  18326. if (!f) {
  18327. err_sys("can't open ./certs/client-key.der, "
  18328. "Please run from wolfSSL home dir", -41);
  18329. ret = -9000;
  18330. goto openssl_pkey1_test_done;
  18331. }
  18332. cliKeySz = (long)XFREAD(tmp, 1, FOURK_BUF, f);
  18333. XFCLOSE(f);
  18334. }
  18335. /* using existing wolfSSL api to get public and private key */
  18336. x509 = wolfSSL_X509_load_certificate_file(clientCert, SSL_FILETYPE_ASN1);
  18337. #endif /* USE_CERT_BUFFERS */
  18338. clikey = tmp;
  18339. if ((prvKey = EVP_PKEY_new()) == NULL) {
  18340. ret = -9001;
  18341. goto openssl_pkey1_test_done;
  18342. }
  18343. EVP_PKEY_free(prvKey);
  18344. prvKey = NULL;
  18345. if (x509 == NULL) {
  18346. ret = -9002;
  18347. goto openssl_pkey1_test_done;
  18348. }
  18349. pubKey = X509_get_pubkey(x509);
  18350. if (pubKey == NULL) {
  18351. ret = -9003;
  18352. goto openssl_pkey1_test_done;
  18353. }
  18354. prvKey = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &clikey, cliKeySz);
  18355. if (prvKey == NULL) {
  18356. ret = -9004;
  18357. goto openssl_pkey1_test_done;
  18358. }
  18359. /* phase 2 API to create EVP_PKEY_CTX and encrypt/decrypt */
  18360. if (EVP_PKEY_bits(prvKey) != keyLenBits) {
  18361. ret = -9005;
  18362. goto openssl_pkey1_test_done;
  18363. }
  18364. if (EVP_PKEY_size(prvKey) != keyLenBits/8) {
  18365. ret = -9006;
  18366. goto openssl_pkey1_test_done;
  18367. }
  18368. dec = EVP_PKEY_CTX_new(prvKey, NULL);
  18369. enc = EVP_PKEY_CTX_new(pubKey, NULL);
  18370. if (dec == NULL || enc == NULL) {
  18371. ret = -9007;
  18372. goto openssl_pkey1_test_done;
  18373. }
  18374. if (EVP_PKEY_decrypt_init(dec) != 1) {
  18375. ret = -9008;
  18376. goto openssl_pkey1_test_done;
  18377. }
  18378. if (EVP_PKEY_encrypt_init(enc) != 1) {
  18379. ret = -9009;
  18380. goto openssl_pkey1_test_done;
  18381. }
  18382. if (EVP_PKEY_CTX_set_rsa_padding(dec, RSA_PKCS1_PADDING) <= 0) {
  18383. ret = -9010;
  18384. goto openssl_pkey1_test_done;
  18385. }
  18386. #ifndef HAVE_FIPS
  18387. if (EVP_PKEY_CTX_set_rsa_padding(dec, RSA_PKCS1_OAEP_PADDING) <= 0){
  18388. ret = -9011;
  18389. goto openssl_pkey1_test_done;
  18390. }
  18391. if (EVP_PKEY_CTX_set_rsa_padding(enc, RSA_PKCS1_OAEP_PADDING) <= 0) {
  18392. ret = -9012;
  18393. goto openssl_pkey1_test_done;
  18394. }
  18395. #endif
  18396. XMEMSET(cipher, 0, RSA_TEST_BYTES);
  18397. outlen = keyLenBits/8;
  18398. if (EVP_PKEY_encrypt(enc, cipher, &outlen, msg, sizeof(msg)) < 0) {
  18399. ret = -9013;
  18400. goto openssl_pkey1_test_done;
  18401. }
  18402. XMEMSET(plain, 0, RSA_TEST_BYTES);
  18403. if (EVP_PKEY_decrypt(dec, plain, &outlen, cipher, outlen) != 1) {
  18404. ret = -9014;
  18405. goto openssl_pkey1_test_done;
  18406. }
  18407. openssl_pkey1_test_done:
  18408. if (pubKey != NULL) {
  18409. EVP_PKEY_free(pubKey);
  18410. }
  18411. if (prvKey != NULL) {
  18412. EVP_PKEY_free(prvKey);
  18413. }
  18414. if (dec != NULL) {
  18415. EVP_PKEY_CTX_free(dec);
  18416. }
  18417. if (enc != NULL) {
  18418. EVP_PKEY_CTX_free(enc);
  18419. }
  18420. if (x509 != NULL) {
  18421. X509_free(x509);
  18422. }
  18423. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18424. if (tmp != NULL)
  18425. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18426. if (cipher != NULL)
  18427. XFREE(cipher, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18428. if (plain != NULL)
  18429. XFREE(plain, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18430. #endif
  18431. #endif
  18432. return ret;
  18433. }
  18434. #define ERR_BASE_EVPSIG (-5100)
  18435. WOLFSSL_TEST_SUBROUTINE int openssl_evpSig_test(void)
  18436. {
  18437. #if !defined(NO_RSA) && !defined(NO_SHA) && !defined(HAVE_USER_RSA)
  18438. byte* prvTmp;
  18439. byte* pubTmp;
  18440. int prvBytes;
  18441. int pubBytes;
  18442. RSA *prvRsa;
  18443. RSA *pubRsa;
  18444. EVP_PKEY *prvPkey;
  18445. EVP_PKEY *pubPkey;
  18446. EVP_MD_CTX* sign;
  18447. EVP_MD_CTX* verf;
  18448. char msg[] = "see spot run";
  18449. unsigned char sig[256];
  18450. unsigned int sigSz;
  18451. const void* pt;
  18452. unsigned int count;
  18453. int ret, ret1, ret2;
  18454. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  18455. XFILE keyFile;
  18456. XFILE keypubFile;
  18457. char cliKey[] = "./certs/client-key.der";
  18458. char cliKeypub[] = "./certs/client-keyPub.der";
  18459. #endif
  18460. prvTmp = (byte*)XMALLOC(FOURK_BUFF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18461. if (prvTmp == NULL)
  18462. return ERR_BASE_EVPSIG-1;
  18463. pubTmp = (byte*)XMALLOC(FOURK_BUFF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18464. if (pubTmp == NULL) {
  18465. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18466. return ERR_BASE_EVPSIG-2;
  18467. }
  18468. #ifdef USE_CERT_BUFFERS_1024
  18469. XMEMCPY(prvTmp, client_key_der_1024, sizeof_client_key_der_1024);
  18470. prvBytes = sizeof_client_key_der_1024;
  18471. XMEMCPY(pubTmp, client_keypub_der_1024, sizeof_client_keypub_der_1024);
  18472. pubBytes = sizeof_client_keypub_der_1024;
  18473. #elif defined(USE_CERT_BUFFERS_2048)
  18474. XMEMCPY(prvTmp, client_key_der_2048, sizeof_client_key_der_2048);
  18475. prvBytes = sizeof_client_key_der_2048;
  18476. XMEMCPY(pubTmp, client_keypub_der_2048, sizeof_client_keypub_der_2048);
  18477. pubBytes = sizeof_client_keypub_der_2048;
  18478. #else
  18479. keyFile = XFOPEN(cliKey, "rb");
  18480. if (!keyFile) {
  18481. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18482. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18483. err_sys("can't open ./certs/client-key.der, "
  18484. "Please run from wolfSSL home dir", -40);
  18485. return ERR_BASE_EVPSIG-3;
  18486. }
  18487. prvBytes = (int)XFREAD(prvTmp, 1, (int)FOURK_BUFF, keyFile);
  18488. XFCLOSE(keyFile);
  18489. keypubFile = XFOPEN(cliKeypub, "rb");
  18490. if (!keypubFile) {
  18491. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18492. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18493. err_sys("can't open ./certs/client-cert.der, "
  18494. "Please run from wolfSSL home dir", -41);
  18495. return ERR_BASE_EVPSIG-4;
  18496. }
  18497. pubBytes = (int)XFREAD(pubTmp, 1, (int)FOURK_BUFF, keypubFile);
  18498. XFCLOSE(keypubFile);
  18499. #endif /* USE_CERT_BUFFERS */
  18500. prvRsa = wolfSSL_RSA_new();
  18501. pubRsa = wolfSSL_RSA_new();
  18502. if((prvRsa == NULL) || (pubRsa == NULL)){
  18503. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18504. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18505. err_sys("ERROR with RSA_new", -9100);
  18506. return ERR_BASE_EVPSIG-5;
  18507. }
  18508. ret1 = wolfSSL_RSA_LoadDer_ex(prvRsa, prvTmp, prvBytes, WOLFSSL_RSA_LOAD_PRIVATE);
  18509. ret2 = wolfSSL_RSA_LoadDer_ex(pubRsa, pubTmp, pubBytes, WOLFSSL_RSA_LOAD_PUBLIC);
  18510. if((ret1 != SSL_SUCCESS) || (ret2 != SSL_SUCCESS)){
  18511. printf("error with RSA_LoadDer_ex\n");
  18512. return ERR_BASE_EVPSIG-6;
  18513. }
  18514. prvPkey = wolfSSL_EVP_PKEY_new();
  18515. pubPkey = wolfSSL_EVP_PKEY_new();
  18516. if((prvPkey == NULL) || (pubPkey == NULL)){
  18517. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18518. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18519. printf("error with KEY_new\n");
  18520. return ERR_BASE_EVPSIG-7;
  18521. }
  18522. ret1 = wolfSSL_EVP_PKEY_set1_RSA(prvPkey, prvRsa);
  18523. ret2 = wolfSSL_EVP_PKEY_set1_RSA(pubPkey, pubRsa);
  18524. if((ret1 != 1) || (ret2 != 1)){
  18525. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18526. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18527. printf("error with EVP_PKEY_set1_RSA\n");
  18528. return ERR_BASE_EVPSIG-8;
  18529. }
  18530. /****************** sign and verify *******************/
  18531. sign = EVP_MD_CTX_create();
  18532. verf = EVP_MD_CTX_create();
  18533. if((sign == NULL)||(verf == NULL)){
  18534. printf("error with EVP_MD_CTX_create\n");
  18535. EVP_MD_CTX_destroy(sign);
  18536. EVP_MD_CTX_destroy(verf);
  18537. return ERR_BASE_EVPSIG-10;
  18538. }
  18539. ret = EVP_SignInit(sign, EVP_sha1());
  18540. if (ret != SSL_SUCCESS){
  18541. printf("error with EVP_SignInit\n");
  18542. EVP_MD_CTX_destroy(sign);
  18543. EVP_MD_CTX_destroy(verf);
  18544. return ERR_BASE_EVPSIG-11;
  18545. }
  18546. count = sizeof(msg);
  18547. show("message = ", (char *)msg, count);
  18548. /* sign */
  18549. XMEMSET(sig, 0, sizeof(sig));
  18550. pt = (const void*)msg;
  18551. ret1 = EVP_SignUpdate(sign, pt, count);
  18552. ret2 = EVP_SignFinal(sign, sig, &sigSz, prvPkey);
  18553. if((ret1 != SSL_SUCCESS) || (ret2 != SSL_SUCCESS)){
  18554. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18555. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18556. EVP_MD_CTX_destroy(sign);
  18557. EVP_MD_CTX_destroy(verf);
  18558. printf("error with EVP_MD_CTX_create\n");
  18559. return ERR_BASE_EVPSIG-12;
  18560. }
  18561. show("signature = ", (char *)sig, sigSz);
  18562. /* verify */
  18563. pt = (const void*)msg;
  18564. ret1 = EVP_VerifyInit(verf, EVP_sha1());
  18565. ret2 = EVP_VerifyUpdate(verf, pt, count);
  18566. if((ret1 != SSL_SUCCESS) || (ret2 != SSL_SUCCESS)){
  18567. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18568. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18569. EVP_MD_CTX_destroy(sign);
  18570. EVP_MD_CTX_destroy(verf);
  18571. printf("error with EVP_Verify\n");
  18572. return ERR_BASE_EVPSIG-13;
  18573. }
  18574. if (EVP_VerifyFinal(verf, sig, sigSz, pubPkey) != 1) {
  18575. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18576. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18577. EVP_MD_CTX_destroy(sign);
  18578. EVP_MD_CTX_destroy(verf);
  18579. printf("error with EVP_VerifyFinal\n");
  18580. return ERR_BASE_EVPSIG-14;
  18581. }
  18582. /* expect fail without update */
  18583. EVP_VerifyInit(verf, EVP_sha1());
  18584. if (EVP_VerifyFinal(verf, sig, sigSz, pubPkey) == 1) {
  18585. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18586. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18587. EVP_MD_CTX_destroy(sign);
  18588. EVP_MD_CTX_destroy(verf);
  18589. printf("EVP_VerifyInit without update not detected\n");
  18590. return ERR_BASE_EVPSIG-15;
  18591. }
  18592. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18593. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18594. EVP_MD_CTX_destroy(sign);
  18595. EVP_MD_CTX_destroy(verf);
  18596. wolfSSL_RSA_free(prvRsa);
  18597. wolfSSL_RSA_free(pubRsa);
  18598. EVP_PKEY_free(pubPkey);
  18599. EVP_PKEY_free(prvPkey);
  18600. #endif /* NO_RSA */
  18601. return 0;
  18602. }
  18603. #endif /* OPENSSL_EXTRA */
  18604. #ifndef NO_PWDBASED
  18605. #ifdef HAVE_SCRYPT
  18606. /* Test vectors taken from RFC 7914: scrypt PBKDF - Section 12. */
  18607. WOLFSSL_TEST_SUBROUTINE int scrypt_test(void)
  18608. {
  18609. #ifdef HAVE_FIPS
  18610. /* RFC 7914 test vector keys are too short for FIPS. */
  18611. #else
  18612. int ret;
  18613. byte derived[64];
  18614. WOLFSSL_SMALL_STACK_STATIC const byte verify1[] = {
  18615. 0x77, 0xd6, 0x57, 0x62, 0x38, 0x65, 0x7b, 0x20,
  18616. 0x3b, 0x19, 0xca, 0x42, 0xc1, 0x8a, 0x04, 0x97,
  18617. 0xf1, 0x6b, 0x48, 0x44, 0xe3, 0x07, 0x4a, 0xe8,
  18618. 0xdf, 0xdf, 0xfa, 0x3f, 0xed, 0xe2, 0x14, 0x42,
  18619. 0xfc, 0xd0, 0x06, 0x9d, 0xed, 0x09, 0x48, 0xf8,
  18620. 0x32, 0x6a, 0x75, 0x3a, 0x0f, 0xc8, 0x1f, 0x17,
  18621. 0xe8, 0xd3, 0xe0, 0xfb, 0x2e, 0x0d, 0x36, 0x28,
  18622. 0xcf, 0x35, 0xe2, 0x0c, 0x38, 0xd1, 0x89, 0x06
  18623. };
  18624. WOLFSSL_SMALL_STACK_STATIC const byte verify2[] = {
  18625. 0xfd, 0xba, 0xbe, 0x1c, 0x9d, 0x34, 0x72, 0x00,
  18626. 0x78, 0x56, 0xe7, 0x19, 0x0d, 0x01, 0xe9, 0xfe,
  18627. 0x7c, 0x6a, 0xd7, 0xcb, 0xc8, 0x23, 0x78, 0x30,
  18628. 0xe7, 0x73, 0x76, 0x63, 0x4b, 0x37, 0x31, 0x62,
  18629. 0x2e, 0xaf, 0x30, 0xd9, 0x2e, 0x22, 0xa3, 0x88,
  18630. 0x6f, 0xf1, 0x09, 0x27, 0x9d, 0x98, 0x30, 0xda,
  18631. 0xc7, 0x27, 0xaf, 0xb9, 0x4a, 0x83, 0xee, 0x6d,
  18632. 0x83, 0x60, 0xcb, 0xdf, 0xa2, 0xcc, 0x06, 0x40
  18633. };
  18634. #if !defined(BENCH_EMBEDDED) && !defined(WOLFSSL_LINUXKM) && !defined(HAVE_INTEL_QA)
  18635. WOLFSSL_SMALL_STACK_STATIC const byte verify3[] = {
  18636. 0x70, 0x23, 0xbd, 0xcb, 0x3a, 0xfd, 0x73, 0x48,
  18637. 0x46, 0x1c, 0x06, 0xcd, 0x81, 0xfd, 0x38, 0xeb,
  18638. 0xfd, 0xa8, 0xfb, 0xba, 0x90, 0x4f, 0x8e, 0x3e,
  18639. 0xa9, 0xb5, 0x43, 0xf6, 0x54, 0x5d, 0xa1, 0xf2,
  18640. 0xd5, 0x43, 0x29, 0x55, 0x61, 0x3f, 0x0f, 0xcf,
  18641. 0x62, 0xd4, 0x97, 0x05, 0x24, 0x2a, 0x9a, 0xf9,
  18642. 0xe6, 0x1e, 0x85, 0xdc, 0x0d, 0x65, 0x1e, 0x40,
  18643. 0xdf, 0xcf, 0x01, 0x7b, 0x45, 0x57, 0x58, 0x87
  18644. };
  18645. #endif
  18646. #ifdef SCRYPT_TEST_ALL
  18647. /* Test case is very slow.
  18648. * Use for confirmation after code change or new platform.
  18649. */
  18650. WOLFSSL_SMALL_STACK_STATIC const byte verify4[] = {
  18651. 0x21, 0x01, 0xcb, 0x9b, 0x6a, 0x51, 0x1a, 0xae,
  18652. 0xad, 0xdb, 0xbe, 0x09, 0xcf, 0x70, 0xf8, 0x81,
  18653. 0xec, 0x56, 0x8d, 0x57, 0x4a, 0x2f, 0xfd, 0x4d,
  18654. 0xab, 0xe5, 0xee, 0x98, 0x20, 0xad, 0xaa, 0x47,
  18655. 0x8e, 0x56, 0xfd, 0x8f, 0x4b, 0xa5, 0xd0, 0x9f,
  18656. 0xfa, 0x1c, 0x6d, 0x92, 0x7c, 0x40, 0xf4, 0xc3,
  18657. 0x37, 0x30, 0x40, 0x49, 0xe8, 0xa9, 0x52, 0xfb,
  18658. 0xcb, 0xf4, 0x5c, 0x6f, 0xa7, 0x7a, 0x41, 0xa4
  18659. };
  18660. #endif
  18661. ret = wc_scrypt(derived, NULL, 0, NULL, 0, 4, 1, 1, sizeof(verify1));
  18662. if (ret != 0)
  18663. return -9200;
  18664. if (XMEMCMP(derived, verify1, sizeof(verify1)) != 0)
  18665. return -9201;
  18666. ret = wc_scrypt(derived, (byte*)"password", 8, (byte*)"NaCl", 4, 10, 8, 16,
  18667. sizeof(verify2));
  18668. if (ret != 0)
  18669. return -9202;
  18670. if (XMEMCMP(derived, verify2, sizeof(verify2)) != 0)
  18671. return -9203;
  18672. /* Test case with parallel overflowing */
  18673. ret = wc_scrypt(derived, (byte*)"password", 16, (byte*)"NaCl", 16, 2, 4, 8388608,
  18674. sizeof(verify2));
  18675. if (ret != BAD_FUNC_ARG)
  18676. return -9210;
  18677. /* Don't run these test on embedded, since they use large mallocs */
  18678. #if !defined(BENCH_EMBEDDED) && !defined(WOLFSSL_LINUXKM) && !defined(HAVE_INTEL_QA)
  18679. ret = wc_scrypt(derived, (byte*)"pleaseletmein", 13,
  18680. (byte*)"SodiumChloride", 14, 14, 8, 1, sizeof(verify3));
  18681. if (ret != 0)
  18682. return -9204;
  18683. if (XMEMCMP(derived, verify3, sizeof(verify3)) != 0)
  18684. return -9205;
  18685. #ifdef SCRYPT_TEST_ALL
  18686. ret = wc_scrypt(derived, (byte*)"pleaseletmein", 13,
  18687. (byte*)"SodiumChloride", 14, 20, 8, 1, sizeof(verify4));
  18688. if (ret != 0)
  18689. return -9206;
  18690. if (XMEMCMP(derived, verify4, sizeof(verify4)) != 0)
  18691. return -9207;
  18692. #endif
  18693. #endif /* !BENCH_EMBEDDED && !defined(WOLFSSL_LINUXKM) && !HAVE_INTEL_QA */
  18694. ret = wc_scrypt_ex(derived, (byte*)"password", 8, (byte*)"NaCl", 4, 1<<10,
  18695. 8, 16, sizeof(verify2));
  18696. if (ret != 0)
  18697. return -9208;
  18698. if (XMEMCMP(derived, verify2, sizeof(verify2)) != 0)
  18699. return -9209;
  18700. #endif /* !HAVE_FIPS */
  18701. return 0;
  18702. }
  18703. #endif
  18704. #ifdef HAVE_PKCS12
  18705. WOLFSSL_TEST_SUBROUTINE int pkcs12_test(void)
  18706. {
  18707. WOLFSSL_SMALL_STACK_STATIC const byte passwd[] = { 0x00, 0x73, 0x00, 0x6d, 0x00, 0x65, 0x00, 0x67,
  18708. 0x00, 0x00 };
  18709. WOLFSSL_SMALL_STACK_STATIC const byte salt[] = { 0x0a, 0x58, 0xCF, 0x64, 0x53, 0x0d, 0x82, 0x3f };
  18710. WOLFSSL_SMALL_STACK_STATIC const byte passwd2[] = { 0x00, 0x71, 0x00, 0x75, 0x00, 0x65, 0x00, 0x65,
  18711. 0x00, 0x67, 0x00, 0x00 };
  18712. WOLFSSL_SMALL_STACK_STATIC const byte salt2[] = { 0x16, 0x82, 0xC0, 0xfC, 0x5b, 0x3f, 0x7e, 0xc5 };
  18713. byte derived[64];
  18714. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  18715. 0x27, 0xE9, 0x0D, 0x7E, 0xD5, 0xA1, 0xC4, 0x11,
  18716. 0xBA, 0x87, 0x8B, 0xC0, 0x90, 0xF5, 0xCE, 0xBE,
  18717. 0x5E, 0x9D, 0x5F, 0xE3, 0xD6, 0x2B, 0x73, 0xAA
  18718. };
  18719. WOLFSSL_SMALL_STACK_STATIC const byte verify2[] = {
  18720. 0x90, 0x1B, 0x49, 0x70, 0xF0, 0x94, 0xF0, 0xF8,
  18721. 0x45, 0xC0, 0xF3, 0xF3, 0x13, 0x59, 0x18, 0x6A,
  18722. 0x35, 0xE3, 0x67, 0xFE, 0xD3, 0x21, 0xFD, 0x7C
  18723. };
  18724. int id = 1;
  18725. int kLen = 24;
  18726. int iterations = 1;
  18727. int ret = wc_PKCS12_PBKDF(derived, passwd, sizeof(passwd), salt, 8,
  18728. iterations, kLen, WC_SHA256, id);
  18729. if (ret < 0)
  18730. return -9300;
  18731. if (XMEMCMP(derived, verify, kLen) != 0)
  18732. return -9301;
  18733. iterations = 1000;
  18734. ret = wc_PKCS12_PBKDF(derived, passwd2, sizeof(passwd2), salt2, 8,
  18735. iterations, kLen, WC_SHA256, id);
  18736. if (ret < 0)
  18737. return -9302;
  18738. ret = wc_PKCS12_PBKDF_ex(derived, passwd2, sizeof(passwd2), salt2, 8,
  18739. iterations, kLen, WC_SHA256, id, HEAP_HINT);
  18740. if (ret < 0)
  18741. return -9303;
  18742. if (XMEMCMP(derived, verify2, 24) != 0)
  18743. return -9304;
  18744. return 0;
  18745. }
  18746. #endif /* HAVE_PKCS12 */
  18747. #if defined(HAVE_PBKDF2) && !defined(NO_SHA256) && !defined(NO_HMAC)
  18748. WOLFSSL_TEST_SUBROUTINE int pbkdf2_test(void)
  18749. {
  18750. char passwd[] = "passwordpassword";
  18751. WOLFSSL_SMALL_STACK_STATIC const byte salt[] = { 0x78, 0x57, 0x8E, 0x5a, 0x5d, 0x63, 0xcb, 0x06 };
  18752. int iterations = 2048;
  18753. int kLen = 24;
  18754. byte derived[64];
  18755. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  18756. 0x43, 0x6d, 0xb5, 0xe8, 0xd0, 0xfb, 0x3f, 0x35, 0x42, 0x48, 0x39, 0xbc,
  18757. 0x2d, 0xd4, 0xf9, 0x37, 0xd4, 0x95, 0x16, 0xa7, 0x2a, 0x9a, 0x21, 0xd1
  18758. };
  18759. int ret = wc_PBKDF2_ex(derived, (byte*)passwd, (int)XSTRLEN(passwd), salt,
  18760. (int)sizeof(salt), iterations, kLen, WC_SHA256, HEAP_HINT, devId);
  18761. if (ret != 0)
  18762. return ret;
  18763. if (XMEMCMP(derived, verify, sizeof(verify)) != 0)
  18764. return -9400;
  18765. return 0;
  18766. }
  18767. #endif /* HAVE_PBKDF2 && !NO_SHA256 && !NO_HMAC */
  18768. #if defined(HAVE_PBKDF1) && !defined(NO_SHA)
  18769. WOLFSSL_TEST_SUBROUTINE int pbkdf1_test(void)
  18770. {
  18771. char passwd[] = "password";
  18772. WOLFSSL_SMALL_STACK_STATIC const byte salt[] = { 0x78, 0x57, 0x8E, 0x5a, 0x5d, 0x63, 0xcb, 0x06 };
  18773. int iterations = 1000;
  18774. int kLen = 16;
  18775. byte derived[16];
  18776. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  18777. 0xDC, 0x19, 0x84, 0x7E, 0x05, 0xC6, 0x4D, 0x2F,
  18778. 0xAF, 0x10, 0xEB, 0xFB, 0x4A, 0x3D, 0x2A, 0x20
  18779. };
  18780. int ret = wc_PBKDF1_ex(derived, kLen, NULL, 0, (byte*)passwd,
  18781. (int)XSTRLEN(passwd), salt, (int)sizeof(salt), iterations, WC_SHA,
  18782. HEAP_HINT);
  18783. if (ret != 0)
  18784. return ret;
  18785. if (XMEMCMP(derived, verify, sizeof(verify)) != 0)
  18786. return -9500;
  18787. return 0;
  18788. }
  18789. #endif /* HAVE_PBKDF2 && !NO_SHA */
  18790. WOLFSSL_TEST_SUBROUTINE int pwdbased_test(void)
  18791. {
  18792. int ret = 0;
  18793. #if defined(HAVE_PBKDF1) && !defined(NO_SHA)
  18794. ret = pbkdf1_test();
  18795. if (ret != 0)
  18796. return ret;
  18797. #endif
  18798. #if defined(HAVE_PBKDF2) && !defined(NO_SHA256) && !defined(NO_HMAC)
  18799. ret = pbkdf2_test();
  18800. if (ret != 0)
  18801. return ret;
  18802. #endif
  18803. #ifdef HAVE_PKCS12
  18804. ret = pkcs12_test();
  18805. if (ret != 0)
  18806. return ret;
  18807. #endif
  18808. #ifdef HAVE_SCRYPT
  18809. ret = scrypt_test();
  18810. if (ret != 0)
  18811. return ret;
  18812. #endif
  18813. return ret;
  18814. }
  18815. #endif /* NO_PWDBASED */
  18816. #if defined(HAVE_HKDF) && !defined(NO_HMAC)
  18817. /* hkdf_test has issue with WOLFSSL_TEST_SUBROUTINE set on Xilinx with afalg */
  18818. static int hkdf_test(void)
  18819. {
  18820. int ret = 0;
  18821. #if !defined(NO_SHA) || !defined(NO_SHA256)
  18822. int L;
  18823. byte okm1[42];
  18824. byte ikm1[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  18825. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  18826. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b };
  18827. #ifndef HAVE_FIPS
  18828. byte salt1[13] ={ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18829. 0x08, 0x09, 0x0a, 0x0b, 0x0c };
  18830. byte info1[10] ={ 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
  18831. 0xf8, 0xf9 };
  18832. #endif
  18833. #ifndef NO_SHA
  18834. byte res1[42] = { 0x0a, 0xc1, 0xaf, 0x70, 0x02, 0xb3, 0xd7, 0x61,
  18835. 0xd1, 0xe5, 0x52, 0x98, 0xda, 0x9d, 0x05, 0x06,
  18836. 0xb9, 0xae, 0x52, 0x05, 0x72, 0x20, 0xa3, 0x06,
  18837. 0xe0, 0x7b, 0x6b, 0x87, 0xe8, 0xdf, 0x21, 0xd0,
  18838. 0xea, 0x00, 0x03, 0x3d, 0xe0, 0x39, 0x84, 0xd3,
  18839. 0x49, 0x18 };
  18840. #ifndef HAVE_FIPS
  18841. byte res2[42] = { 0x08, 0x5a, 0x01, 0xea, 0x1b, 0x10, 0xf3, 0x69,
  18842. 0x33, 0x06, 0x8b, 0x56, 0xef, 0xa5, 0xad, 0x81,
  18843. 0xa4, 0xf1, 0x4b, 0x82, 0x2f, 0x5b, 0x09, 0x15,
  18844. 0x68, 0xa9, 0xcd, 0xd4, 0xf1, 0x55, 0xfd, 0xa2,
  18845. 0xc2, 0x2e, 0x42, 0x24, 0x78, 0xd3, 0x05, 0xf3,
  18846. 0xf8, 0x96 };
  18847. #endif
  18848. #endif /* !NO_SHA */
  18849. #ifndef NO_SHA256
  18850. byte res3[42] = { 0x8d, 0xa4, 0xe7, 0x75, 0xa5, 0x63, 0xc1, 0x8f,
  18851. 0x71, 0x5f, 0x80, 0x2a, 0x06, 0x3c, 0x5a, 0x31,
  18852. 0xb8, 0xa1, 0x1f, 0x5c, 0x5e, 0xe1, 0x87, 0x9e,
  18853. 0xc3, 0x45, 0x4e, 0x5f, 0x3c, 0x73, 0x8d, 0x2d,
  18854. 0x9d, 0x20, 0x13, 0x95, 0xfa, 0xa4, 0xb6, 0x1a,
  18855. 0x96, 0xc8 };
  18856. #ifndef HAVE_FIPS
  18857. byte res4[42] = { 0x3c, 0xb2, 0x5f, 0x25, 0xfa, 0xac, 0xd5, 0x7a,
  18858. 0x90, 0x43, 0x4f, 0x64, 0xd0, 0x36, 0x2f, 0x2a,
  18859. 0x2d, 0x2d, 0x0a, 0x90, 0xcf, 0x1a, 0x5a, 0x4c,
  18860. 0x5d, 0xb0, 0x2d, 0x56, 0xec, 0xc4, 0xc5, 0xbf,
  18861. 0x34, 0x00, 0x72, 0x08, 0xd5, 0xb8, 0x87, 0x18,
  18862. 0x58, 0x65 };
  18863. #endif
  18864. #endif /* !NO_SHA256 */
  18865. XMEMSET(okm1, 0, sizeof(okm1));
  18866. L = (int)sizeof(okm1);
  18867. #ifndef NO_SHA
  18868. ret = wc_HKDF(WC_SHA, ikm1, (word32)sizeof(ikm1), NULL, 0, NULL, 0,
  18869. okm1, L);
  18870. if (ret != 0)
  18871. return -9700;
  18872. if (XMEMCMP(okm1, res1, L) != 0)
  18873. return -9701;
  18874. #ifndef HAVE_FIPS
  18875. /* fips can't have key size under 14 bytes, salt is key too */
  18876. L = (int)sizeof(okm1);
  18877. ret = wc_HKDF(WC_SHA, ikm1, 11, salt1, (word32)sizeof(salt1),
  18878. info1, (word32)sizeof(info1), okm1, L);
  18879. if (ret != 0)
  18880. return -9702;
  18881. if (XMEMCMP(okm1, res2, L) != 0)
  18882. return -9703;
  18883. #endif /* HAVE_FIPS */
  18884. #endif /* !NO_SHA */
  18885. #ifndef NO_SHA256
  18886. ret = wc_HKDF(WC_SHA256, ikm1, (word32)sizeof(ikm1), NULL, 0, NULL, 0,
  18887. okm1, L);
  18888. if (ret != 0)
  18889. return -9704;
  18890. if (XMEMCMP(okm1, res3, L) != 0)
  18891. return -9705;
  18892. #ifndef HAVE_FIPS
  18893. /* fips can't have key size under 14 bytes, salt is key too */
  18894. ret = wc_HKDF(WC_SHA256, ikm1, (word32)sizeof(ikm1),
  18895. salt1, (word32)sizeof(salt1), info1, (word32)sizeof(info1), okm1, L);
  18896. if (ret != 0)
  18897. return -9706;
  18898. if (XMEMCMP(okm1, res4, L) != 0)
  18899. return -9707;
  18900. #endif /* HAVE_FIPS */
  18901. #endif /* !NO_SHA256 */
  18902. #endif /* !NO_SHA || !NO_SHA256 */
  18903. return ret;
  18904. }
  18905. #endif /* HAVE_HKDF */
  18906. #ifdef WOLFSSL_WOLFSSH
  18907. typedef struct {
  18908. byte hashId;
  18909. byte keyId;
  18910. const byte* k;
  18911. word32 kSz;
  18912. const byte* h;
  18913. word32 hSz;
  18914. const byte* sessionId;
  18915. word32 sessionIdSz;
  18916. const byte* expectedKey;
  18917. word32 expectedKeySz;
  18918. } SshKdfTestVector;
  18919. /** Test Vector Set #3: SHA-256 **/
  18920. static const byte sshKdfTvSet3k[] = {
  18921. 0x6A, 0xC3, 0x82, 0xEA, 0xAC, 0xA0, 0x93, 0xE1,
  18922. 0x25, 0xE2, 0x5C, 0x24, 0xBE, 0xBC, 0x84, 0x64,
  18923. 0x0C, 0x11, 0x98, 0x75, 0x07, 0x34, 0x4B, 0x5C,
  18924. 0x73, 0x9C, 0xEB, 0x84, 0xA9, 0xE0, 0xB2, 0x22,
  18925. 0xB9, 0xA8, 0xB5, 0x1C, 0x83, 0x9E, 0x5E, 0xBE,
  18926. 0x49, 0xCF, 0xAD, 0xBF, 0xB3, 0x95, 0x99, 0x76,
  18927. 0x4E, 0xD5, 0x22, 0x09, 0x9D, 0xC9, 0x12, 0x75,
  18928. 0x19, 0x50, 0xDC, 0x7D, 0xC9, 0x7F, 0xBD, 0xC0,
  18929. 0x63, 0x28, 0xB6, 0x8F, 0x22, 0x78, 0x1F, 0xD3,
  18930. 0x15, 0xAF, 0x56, 0x80, 0x09, 0xA5, 0x50, 0x9E,
  18931. 0x5B, 0x87, 0xA1, 0x1B, 0xF5, 0x27, 0xC0, 0x56,
  18932. 0xDA, 0xFF, 0xD8, 0x2A, 0xB6, 0xCB, 0xC2, 0x5C,
  18933. 0xCA, 0x37, 0x14, 0x34, 0x59, 0xE7, 0xBC, 0x63,
  18934. 0xBC, 0xDE, 0x52, 0x75, 0x7A, 0xDE, 0xB7, 0xDF,
  18935. 0x01, 0xCF, 0x12, 0x17, 0x3F, 0x1F, 0xEF, 0x81,
  18936. 0x02, 0xEC, 0x5A, 0xB1, 0x42, 0xC2, 0x13, 0xDD,
  18937. 0x9D, 0x30, 0x69, 0x62, 0x78, 0xA8, 0xD8, 0xBC,
  18938. 0x32, 0xDD, 0xE9, 0x59, 0x2D, 0x28, 0xC0, 0x78,
  18939. 0xC6, 0xD9, 0x2B, 0x94, 0x7D, 0x82, 0x5A, 0xCA,
  18940. 0xAB, 0x64, 0x94, 0x84, 0x6A, 0x49, 0xDE, 0x24,
  18941. 0xB9, 0x62, 0x3F, 0x48, 0x89, 0xE8, 0xAD, 0xC3,
  18942. 0x8E, 0x8C, 0x66, 0x9E, 0xFF, 0xEF, 0x17, 0x60,
  18943. 0x40, 0xAD, 0x94, 0x5E, 0x90, 0xA7, 0xD3, 0xEE,
  18944. 0xC1, 0x5E, 0xFE, 0xEE, 0x78, 0xAE, 0x71, 0x04,
  18945. 0x3C, 0x96, 0x51, 0x11, 0x03, 0xA1, 0x6B, 0xA7,
  18946. 0xCA, 0xF0, 0xAC, 0xD0, 0x64, 0x2E, 0xFD, 0xBE,
  18947. 0x80, 0x99, 0x34, 0xFA, 0xA1, 0xA5, 0xF1, 0xBD,
  18948. 0x11, 0x04, 0x36, 0x49, 0xB2, 0x5C, 0xCD, 0x1F,
  18949. 0xEE, 0x2E, 0x38, 0x81, 0x5D, 0x4D, 0x5F, 0x5F,
  18950. 0xC6, 0xB4, 0x10, 0x29, 0x69, 0xF2, 0x1C, 0x22,
  18951. 0xAE, 0x1B, 0x0E, 0x7D, 0x36, 0x03, 0xA5, 0x56,
  18952. 0xA1, 0x32, 0x62, 0xFF, 0x62, 0x8D, 0xE2, 0x22
  18953. };
  18954. static const byte sshKdfTvSet3h[] = {
  18955. 0x7B, 0x70, 0x01, 0x18, 0x5E, 0x25, 0x6D, 0x44,
  18956. 0x93, 0x44, 0x5F, 0x39, 0xA5, 0x5F, 0xB9, 0x05,
  18957. 0xE6, 0x32, 0x1F, 0x4B, 0x5D, 0xD8, 0xBB, 0xF3,
  18958. 0x10, 0x0D, 0x51, 0xBA, 0x0B, 0xDA, 0x3D, 0x2D
  18959. };
  18960. static const byte sshKdfTvSet3sid[] = {
  18961. 0x7B, 0x70, 0x01, 0x18, 0x5E, 0x25, 0x6D, 0x44,
  18962. 0x93, 0x44, 0x5F, 0x39, 0xA5, 0x5F, 0xB9, 0x05,
  18963. 0xE6, 0x32, 0x1F, 0x4B, 0x5D, 0xD8, 0xBB, 0xF3,
  18964. 0x10, 0x0D, 0x51, 0xBA, 0x0B, 0xDA, 0x3D, 0x2D
  18965. };
  18966. static const byte sshKdfTvSet3a[] = {
  18967. 0x81, 0xF0, 0x33, 0x0E, 0xF6, 0xF0, 0x53, 0x61,
  18968. 0xB3, 0x82, 0x3B, 0xFD, 0xED, 0x6E, 0x1D, 0xE9
  18969. };
  18970. static const byte sshKdfTvSet3b[] = {
  18971. 0x3F, 0x6F, 0xD2, 0x06, 0x5E, 0xEB, 0x2B, 0x0B,
  18972. 0x1D, 0x93, 0x19, 0x5A, 0x1F, 0xED, 0x48, 0xA5
  18973. };
  18974. static const byte sshKdfTvSet3c[] = {
  18975. 0xC3, 0x54, 0x71, 0x03, 0x4E, 0x6F, 0xD6, 0x54,
  18976. 0x76, 0x13, 0x17, 0x8E, 0x23, 0x43, 0x5F, 0x21
  18977. };
  18978. static const byte sshKdfTvSet3d[] = {
  18979. 0x7E, 0x9D, 0x79, 0x03, 0x20, 0x90, 0xD9, 0x9F,
  18980. 0x98, 0xB0, 0x15, 0x63, 0x4D, 0xD9, 0xF4, 0x62
  18981. };
  18982. static const byte sshKdfTvSet3e[] = {
  18983. 0x24, 0xEE, 0x55, 0x9A, 0xD7, 0xCE, 0x71, 0x2B,
  18984. 0x68, 0x5D, 0x0B, 0x22, 0x71, 0xE4, 0x43, 0xC1,
  18985. 0x7A, 0xB1, 0xD1, 0xDC, 0xEB, 0x5A, 0x36, 0x05,
  18986. 0x69, 0xD2, 0x5D, 0x5D, 0xC2, 0x43, 0x00, 0x2F
  18987. };
  18988. static const byte sshKdfTvSet3f[] = {
  18989. 0xC3, 0x41, 0x9C, 0x2B, 0x96, 0x62, 0x35, 0x86,
  18990. 0x9D, 0x71, 0x4B, 0xA5, 0xAC, 0x48, 0xDD, 0xB7,
  18991. 0xD9, 0xE3, 0x5C, 0x8C, 0x19, 0xAA, 0xC7, 0x34,
  18992. 0x22, 0x33, 0x7A, 0x37, 0x34, 0x53, 0x60, 0x7E
  18993. };
  18994. static const SshKdfTestVector sshKdfTestVectors[] = {
  18995. {WC_HASH_TYPE_SHA256, 'A',
  18996. sshKdfTvSet3k, sizeof(sshKdfTvSet3k),
  18997. sshKdfTvSet3h, sizeof(sshKdfTvSet3h),
  18998. sshKdfTvSet3sid, sizeof(sshKdfTvSet3sid),
  18999. sshKdfTvSet3a, sizeof(sshKdfTvSet3a)},
  19000. {WC_HASH_TYPE_SHA256, 'B',
  19001. sshKdfTvSet3k, sizeof(sshKdfTvSet3k),
  19002. sshKdfTvSet3h, sizeof(sshKdfTvSet3h),
  19003. sshKdfTvSet3sid, sizeof(sshKdfTvSet3sid),
  19004. sshKdfTvSet3b, sizeof(sshKdfTvSet3b)},
  19005. {WC_HASH_TYPE_SHA256, 'C',
  19006. sshKdfTvSet3k, sizeof(sshKdfTvSet3k),
  19007. sshKdfTvSet3h, sizeof(sshKdfTvSet3h),
  19008. sshKdfTvSet3sid, sizeof(sshKdfTvSet3sid),
  19009. sshKdfTvSet3c, sizeof(sshKdfTvSet3c)},
  19010. {WC_HASH_TYPE_SHA256, 'D',
  19011. sshKdfTvSet3k, sizeof(sshKdfTvSet3k),
  19012. sshKdfTvSet3h, sizeof(sshKdfTvSet3h),
  19013. sshKdfTvSet3sid, sizeof(sshKdfTvSet3sid),
  19014. sshKdfTvSet3d, sizeof(sshKdfTvSet3d)},
  19015. {WC_HASH_TYPE_SHA256, 'E',
  19016. sshKdfTvSet3k, sizeof(sshKdfTvSet3k),
  19017. sshKdfTvSet3h, sizeof(sshKdfTvSet3h),
  19018. sshKdfTvSet3sid, sizeof(sshKdfTvSet3sid),
  19019. sshKdfTvSet3e, sizeof(sshKdfTvSet3e)},
  19020. {WC_HASH_TYPE_SHA256, 'F',
  19021. sshKdfTvSet3k, sizeof(sshKdfTvSet3k),
  19022. sshKdfTvSet3h, sizeof(sshKdfTvSet3h),
  19023. sshKdfTvSet3sid, sizeof(sshKdfTvSet3sid),
  19024. sshKdfTvSet3f, sizeof(sshKdfTvSet3f)},
  19025. };
  19026. int sshkdf_test(void)
  19027. {
  19028. int result = 0;
  19029. word32 i;
  19030. word32 tc = sizeof(sshKdfTestVectors)/sizeof(SshKdfTestVector);
  19031. const SshKdfTestVector* tv = NULL;
  19032. byte cKey[32]; /* Greater of SHA256_DIGEST_SIZE and AES_BLOCK_SIZE */
  19033. /* sId - Session ID, eKey - Expected Key, cKey - Calculated Key */
  19034. for (i = 0, tv = sshKdfTestVectors; i < tc; i++, tv++) {
  19035. result = wc_SSH_KDF(tv->hashId, tv->keyId,
  19036. cKey, tv->expectedKeySz,
  19037. tv->k, tv->kSz, tv->h, tv->hSz,
  19038. tv->sessionId, tv->sessionIdSz);
  19039. if (result != 0) {
  19040. printf("KDF: Could not derive key.\n");
  19041. result = -101;
  19042. }
  19043. else {
  19044. if (XMEMCMP(cKey, tv->expectedKey, tv->expectedKeySz) != 0) {
  19045. printf("KDF: Calculated Key does not match Expected Key.\n");
  19046. result = -102;
  19047. }
  19048. }
  19049. if (result != 0) break;
  19050. }
  19051. return result;
  19052. }
  19053. #endif /* WOLFSSL_WOLFSSH */
  19054. #ifdef WOLFSSL_TLS13
  19055. #define TLSV13_PSK_DHE_SZ 40
  19056. typedef struct {
  19057. enum wc_HashType hashAlg;
  19058. word32 pskSz;
  19059. word32 dheSz;
  19060. byte psk[TLSV13_PSK_DHE_SZ];
  19061. byte dhe[TLSV13_PSK_DHE_SZ];
  19062. byte hashHello1[WC_MAX_DIGEST_SIZE];
  19063. byte hashHello2[WC_MAX_DIGEST_SIZE];
  19064. byte hashFinished1[WC_MAX_DIGEST_SIZE];
  19065. byte hashFinished2[WC_MAX_DIGEST_SIZE];
  19066. /* Expected */
  19067. byte clientEarlyTrafficSecret[WC_MAX_DIGEST_SIZE];
  19068. byte earlyExporterMasterSecret[WC_MAX_DIGEST_SIZE];
  19069. byte clientHandshakeTrafficSecret[WC_MAX_DIGEST_SIZE];
  19070. byte serverHandshakeTrafficSecret[WC_MAX_DIGEST_SIZE];
  19071. byte clientApplicationTrafficSecret[WC_MAX_DIGEST_SIZE];
  19072. byte serverApplicationTrafficSecret[WC_MAX_DIGEST_SIZE];
  19073. byte exporterMasterSecret[WC_MAX_DIGEST_SIZE];
  19074. byte resumptionMasterSecret[WC_MAX_DIGEST_SIZE];
  19075. } Tls13KdfTestVector;
  19076. /* The following tests come from the CAVP test vectors we used for
  19077. * our FIPS validation. The hash values used are the components from
  19078. * the test hashed together. hashHello1 is the hash of the
  19079. * clientHelloRandom value of the test vector. hashHello2 is the hash
  19080. * of the clientHelloRandom and serverHelloRandom values from the test
  19081. * vector. hashFinished1 is clientHelloRandom, serverHelloRandom, and
  19082. * serverFinishedRandom. hashFinished2 is clientHelloRandom,
  19083. * serverHelloRandom, serverFinishedRandom, and clietnFinishedRandom
  19084. * hashed together. */
  19085. static const Tls13KdfTestVector tls13KdfTestVectors[] = {
  19086. { /* 1 */
  19087. WC_HASH_TYPE_SHA256, 35, 35,
  19088. { /* PSK */
  19089. 0x7b, 0xf1, 0x05, 0x31, 0x36, 0xfa, 0x03, 0xdc,
  19090. 0x31, 0x97, 0x88, 0x04, 0x9c, 0xbc, 0xee, 0xf7,
  19091. 0x8d, 0x84, 0x95, 0x26, 0xaf, 0x1d, 0x68, 0xb0,
  19092. 0x60, 0x7a, 0xcc, 0x4f, 0xc1, 0xd3, 0xa1, 0x68,
  19093. 0x7f, 0x6d, 0xbe
  19094. },
  19095. { /* DHE */
  19096. 0x6e, 0xa1, 0x77, 0xab, 0x2f, 0x43, 0xd2, 0x4b,
  19097. 0xe5, 0xa1, 0x09, 0xe0, 0x7a, 0xd0, 0x01, 0x35,
  19098. 0x8d, 0xf8, 0xf2, 0x5c, 0x91, 0x02, 0xb0, 0x6c,
  19099. 0x3f, 0xeb, 0xee, 0xa4, 0x42, 0x19, 0xce, 0xdc,
  19100. 0x81, 0x26, 0x40
  19101. },
  19102. { /* Hello 1 */
  19103. 0xd9, 0x4b, 0xe4, 0x17, 0xef, 0x58, 0x73, 0x7d,
  19104. 0x28, 0x3d, 0xf0, 0xcc, 0x05, 0x03, 0xaf, 0xac,
  19105. 0x3d, 0x92, 0x79, 0x48, 0xe8, 0x8c, 0xdb, 0xce,
  19106. 0x95, 0x82, 0x21, 0x31, 0x7b, 0x61, 0xd7, 0xc6
  19107. },
  19108. { /* Hello 2 */
  19109. 0xb7, 0x7f, 0x29, 0x91, 0xa4, 0x8b, 0x34, 0xdb,
  19110. 0xbd, 0xc7, 0x54, 0x1c, 0x3b, 0x86, 0xa3, 0x69,
  19111. 0xfe, 0x26, 0xe4, 0x7b, 0xac, 0x57, 0x71, 0xb3,
  19112. 0x32, 0x97, 0xed, 0xd2, 0x0e, 0x95, 0xb8, 0x63
  19113. },
  19114. { /* Finished 1 */
  19115. 0x65, 0xdb, 0x6d, 0x71, 0x71, 0xd0, 0xd8, 0x49,
  19116. 0xd0, 0x3c, 0x8e, 0x2b, 0x24, 0xdf, 0xc2, 0xe9,
  19117. 0xd6, 0xfd, 0xea, 0x04, 0x95, 0x7c, 0xf0, 0x7e,
  19118. 0x57, 0x74, 0x7c, 0xdd, 0xa3, 0x0b, 0x2b, 0x36
  19119. },
  19120. { /* Finished 2 */
  19121. 0x28, 0xf2, 0xf2, 0x79, 0xcf, 0x20, 0x52, 0x90,
  19122. 0x1d, 0x91, 0x05, 0xad, 0x44, 0x26, 0x23, 0x96,
  19123. 0x32, 0xce, 0xec, 0x61, 0xd1, 0xbf, 0x00, 0x48,
  19124. 0x4a, 0xa5, 0x60, 0xcc, 0x28, 0xb5, 0x8d, 0x98
  19125. },
  19126. { /* Client Early Traffic Secret */
  19127. 0x07, 0x14, 0x6a, 0x26, 0x5b, 0x6c, 0x7f, 0x4d, 0x6b, 0x47, 0x3f, 0xd5,
  19128. 0x03, 0x1d, 0xd2, 0x23, 0x3d, 0x89, 0x3e, 0xc6, 0x51, 0xd1, 0xac, 0xf8,
  19129. 0x28, 0xae, 0x4b, 0x76, 0xc8, 0x10, 0x7e, 0xdd
  19130. },
  19131. { /* Early Exporter Master Secret */
  19132. 0xb8, 0xd3, 0x25, 0x7e, 0x2d, 0x41, 0x7b, 0xcb, 0x5e, 0x82, 0x49, 0xf5,
  19133. 0x51, 0x3d, 0xb7, 0x59, 0x32, 0xb3, 0xdf, 0x99, 0x4e, 0x04, 0x69, 0xc6,
  19134. 0x96, 0x8e, 0xe6, 0x3d, 0x91, 0xe4, 0x81, 0x11
  19135. },
  19136. { /* Client Handshake Traffic Secret */
  19137. 0xd9, 0x3b, 0x54, 0xe2, 0xb0, 0xd1, 0x85, 0xf0, 0xfd, 0xf3, 0x48, 0x4a,
  19138. 0xf8, 0x0b, 0xa5, 0xdc, 0x4c, 0x37, 0xcb, 0xd4, 0x20, 0xaf, 0x60, 0xc7,
  19139. 0xd5, 0x50, 0x5d, 0x0c, 0x77, 0x3b, 0x6f, 0xd2
  19140. },
  19141. { /* Server Handshake Traffic Secret */
  19142. 0x4d, 0x40, 0x2b, 0xd2, 0x8c, 0x33, 0x90, 0x39, 0x67, 0x67, 0x05, 0xf7,
  19143. 0x5d, 0x37, 0x1e, 0xdc, 0x4a, 0x70, 0x6b, 0x9e, 0xf8, 0x06, 0x61, 0x89,
  19144. 0x70, 0xe1, 0x3d, 0x36, 0xad, 0x88, 0x7e, 0x5b
  19145. },
  19146. { /* Client Application Traffic Secret */
  19147. 0x74, 0x6e, 0xa0, 0x13, 0x18, 0x34, 0x48, 0x4d, 0x23, 0x31, 0xf1, 0xf9,
  19148. 0xee, 0x44, 0x6d, 0xad, 0xc1, 0xad, 0x92, 0x73, 0xca, 0x27, 0x16, 0x91,
  19149. 0xa2, 0x50, 0x9a, 0xfc, 0xec, 0xf0, 0x6b, 0x24
  19150. },
  19151. { /* Server Application Traffic Secret */
  19152. 0x89, 0x18, 0x7e, 0x34, 0x8d, 0xfc, 0x14, 0xb1, 0x4f, 0x21, 0xd8, 0x29,
  19153. 0xdb, 0x9b, 0xfb, 0x55, 0xcf, 0xa1, 0x4f, 0x95, 0xf8, 0xe0, 0xb0, 0x83,
  19154. 0xd5, 0x34, 0x9e, 0x0b, 0x83, 0x37, 0x42, 0x93
  19155. },
  19156. { /* Exporter Master Secret */
  19157. 0x7d, 0xc8, 0x88, 0x46, 0xd5, 0x57, 0x15, 0xb6, 0x24, 0x25, 0x92, 0x61,
  19158. 0xb1, 0x18, 0x86, 0x2a, 0x6d, 0xa5, 0x84, 0xeb, 0x59, 0xdf, 0x13, 0xbd,
  19159. 0x73, 0xaa, 0x5d, 0x65, 0xab, 0xd9, 0xb4, 0x56
  19160. },
  19161. { /* Resumption Master Secret */
  19162. 0x20, 0xb7, 0xd0, 0xe3, 0x82, 0x01, 0xa1, 0x04, 0xb8, 0x13, 0x29, 0xed,
  19163. 0x35, 0xe4, 0x2f, 0xbf, 0x58, 0x23, 0x7f, 0x21, 0xdb, 0x9f, 0xf8, 0xe0,
  19164. 0xe8, 0xe4, 0xab, 0xc4, 0xa1, 0x61, 0xb9, 0xbb
  19165. }
  19166. },
  19167. { /* 6 */
  19168. WC_HASH_TYPE_SHA256, 0, 33,
  19169. { 0 }, /* PSK */
  19170. { /* DHE */
  19171. 0x7a, 0x46, 0x8c, 0x5a, 0xd1, 0x8e, 0x95, 0xba,
  19172. 0x61, 0xe6, 0x6f, 0xe6, 0x76, 0x0c, 0x20, 0x43,
  19173. 0x16, 0x82, 0x15, 0xfe, 0x54, 0xa3, 0xc7, 0xfd,
  19174. 0x3b, 0x2c, 0x88, 0xb4, 0xd3, 0x42, 0x70, 0x12,
  19175. 0x18
  19176. },
  19177. { /* Hello 1 */
  19178. 0x63, 0x83, 0x58, 0xab, 0x36, 0xcd, 0x0c, 0xf3,
  19179. 0x26, 0x07, 0xb5, 0x5f, 0x0b, 0x8b, 0x45, 0xd6,
  19180. 0x7d, 0x5b, 0x42, 0xdc, 0xa8, 0xaa, 0x06, 0xfb,
  19181. 0x20, 0xa5, 0xbb, 0x85, 0xdb, 0x54, 0xd8, 0x8b
  19182. },
  19183. { /* Hello 2 */
  19184. 0xea, 0xfe, 0x9e, 0x8e, 0xff, 0x1f, 0x6f, 0x43,
  19185. 0xf9, 0x5d, 0xfd, 0xbf, 0xe2, 0x5f, 0x02, 0x2f,
  19186. 0x6d, 0x47, 0x60, 0x9a, 0x48, 0x9a, 0x75, 0xfb,
  19187. 0xb5, 0x4a, 0xbf, 0x9c, 0x4e, 0xff, 0xbf, 0x0b
  19188. },
  19189. { /* Finished 1 */
  19190. 0xca, 0x25, 0xb3, 0x53, 0x8e, 0x6d, 0xc3, 0x36,
  19191. 0x17, 0x30, 0x07, 0xdf, 0x0d, 0xd7, 0x79, 0xb0,
  19192. 0x7f, 0xcb, 0xbe, 0x7a, 0xbc, 0x2d, 0x9f, 0x2d,
  19193. 0x94, 0x44, 0x94, 0xe6, 0xa4, 0xf3, 0xe8, 0x53
  19194. },
  19195. { /* Finished 2 */
  19196. 0x2e, 0xa6, 0x5a, 0xaf, 0xb5, 0xba, 0x9f, 0x2f,
  19197. 0x74, 0x83, 0x5d, 0xbf, 0x86, 0xa4, 0xa6, 0xf6,
  19198. 0xb9, 0x89, 0xdf, 0x17, 0xe1, 0xa8, 0x14, 0xc0,
  19199. 0xe1, 0x50, 0xfa, 0xec, 0xfa, 0xae, 0x8b, 0x7b
  19200. },
  19201. {
  19202. 0x20, 0x18, 0x72, 0x7c, 0xde, 0x3a, 0x85, 0x17, 0x72, 0xdc, 0xd7, 0x72,
  19203. 0xb0, 0xfc, 0x45, 0xd0, 0x62, 0xb9, 0xbb, 0x38, 0x69, 0x05, 0x7b, 0xb4,
  19204. 0x5e, 0x58, 0x5d, 0xed, 0xcd, 0x0b, 0x96, 0xd3
  19205. },
  19206. {
  19207. 0x68, 0x10, 0x20, 0xd1, 0x5e, 0xfc, 0x0c, 0x53, 0x85, 0xbb, 0xdb, 0x18,
  19208. 0xa8, 0x78, 0xf1, 0x2b, 0x13, 0xba, 0x64, 0x1d, 0xe7, 0x09, 0xbe, 0x13,
  19209. 0x49, 0x26, 0xf9, 0x98, 0x56, 0xf1, 0x43, 0xfb
  19210. },
  19211. {
  19212. 0x24, 0x35, 0x3e, 0x10, 0x6f, 0x39, 0x50, 0xd6, 0xa2, 0x12, 0x99, 0xf2,
  19213. 0xd5, 0xf5, 0x19, 0xf5, 0x84, 0xed, 0xee, 0x78, 0x2a, 0xa6, 0xfa, 0x3d,
  19214. 0x06, 0xa8, 0xa7, 0x5d, 0x97, 0x78, 0xd6, 0x58
  19215. },
  19216. {
  19217. 0xf4, 0x57, 0xac, 0x24, 0x7a, 0xfb, 0x7c, 0x3b, 0xb6, 0x39, 0x17, 0x14,
  19218. 0xd9, 0xd4, 0x58, 0x4d, 0x46, 0xd5, 0x1b, 0xde, 0xf7, 0x9d, 0x06, 0xee,
  19219. 0x8d, 0x1a, 0x2c, 0x25, 0x6d, 0x64, 0xde, 0x89
  19220. },
  19221. {
  19222. 0xb6, 0x00, 0xce, 0x63, 0xed, 0x65, 0x8b, 0x66, 0x66, 0x42, 0xc6, 0xbd,
  19223. 0x89, 0xc4, 0x71, 0x6f, 0xce, 0x28, 0xb2, 0xac, 0x97, 0x07, 0x5b, 0xea,
  19224. 0xb8, 0x1d, 0x4c, 0xeb, 0x9e, 0x71, 0x07, 0x8f
  19225. },
  19226. {
  19227. 0xf8, 0x92, 0xc8, 0xba, 0xe7, 0x83, 0xfe, 0x68, 0xe4, 0xd6, 0x5e, 0xcb,
  19228. 0xb3, 0xef, 0x49, 0xd0, 0xe7, 0xb1, 0xac, 0xcb, 0x39, 0x19, 0xfd, 0xa7,
  19229. 0xf7, 0xca, 0xab, 0x1e, 0x42, 0x14, 0xd8, 0xe7
  19230. },
  19231. {
  19232. 0x32, 0x4a, 0x1a, 0xad, 0xe2, 0xbb, 0x55, 0x8a, 0xdd, 0xe9, 0xa5, 0x2a,
  19233. 0x46, 0x5e, 0x6c, 0x83, 0x66, 0x27, 0x27, 0x94, 0xdd, 0x68, 0x59, 0xa0,
  19234. 0xbb, 0xe8, 0x31, 0x7c, 0x39, 0xd7, 0xfd, 0x6d
  19235. },
  19236. {
  19237. 0x58, 0xbc, 0x6c, 0x5b, 0x24, 0xad, 0x82, 0xb3, 0xcc, 0xc7, 0xd1, 0xa1,
  19238. 0xaa, 0x2b, 0x98, 0x9f, 0x2f, 0x7e, 0xa9, 0x63, 0xc2, 0x8e, 0xb6, 0x06,
  19239. 0xc2, 0x2b, 0x74, 0x4b, 0x79, 0x19, 0x7e, 0x2e
  19240. }
  19241. },
  19242. { /* 11 */
  19243. WC_HASH_TYPE_SHA256, 33, 0,
  19244. { /* PSK */
  19245. 0x3d, 0x39, 0x49, 0x36, 0x98, 0xc5, 0xfd, 0xcd,
  19246. 0xa0, 0x17, 0xbd, 0x65, 0x0a, 0xdb, 0xd4, 0x07,
  19247. 0x56, 0xa2, 0x7b, 0xb8, 0x2a, 0x7e, 0xfb, 0x26,
  19248. 0x74, 0xe1, 0xbc, 0x08, 0x4b, 0xf0, 0x30, 0x14,
  19249. 0x12
  19250. },
  19251. { 0 }, /* DHE */
  19252. { /* Hello 1 */
  19253. 0xb7, 0x44, 0x74, 0x6c, 0x57, 0x1f, 0xf3, 0x84,
  19254. 0x8f, 0x63, 0xfb, 0x8c, 0x94, 0x6c, 0x16, 0x68,
  19255. 0x4b, 0xe1, 0xb5, 0xb5, 0x2a, 0x4e, 0x5f, 0xdf,
  19256. 0x4b, 0x53, 0xb2, 0x35, 0xfc, 0x30, 0xf1, 0x36
  19257. },
  19258. { /* Hello 2 */
  19259. 0xe6, 0x4f, 0x3a, 0x4f, 0xd7, 0xe0, 0x64, 0xd4,
  19260. 0x69, 0x50, 0xe4, 0x8b, 0xba, 0xbc, 0x47, 0x74,
  19261. 0xa7, 0x9b, 0x40, 0x91, 0x8f, 0xa8, 0x72, 0x22,
  19262. 0x97, 0xad, 0x43, 0xa7, 0x11, 0x86, 0xb5, 0x72
  19263. },
  19264. { /* Finished 1 */
  19265. 0x5f, 0xa6, 0x10, 0xe2, 0xa3, 0x99, 0x0b, 0x5e,
  19266. 0x57, 0xee, 0xc3, 0x3a, 0x8e, 0x04, 0xf3, 0x0e,
  19267. 0x58, 0x02, 0x09, 0xb2, 0x7e, 0x2d, 0xc6, 0xd2,
  19268. 0x08, 0xae, 0x68, 0x0a, 0x55, 0xa5, 0xda, 0x51
  19269. },
  19270. { /* Finished 2 */
  19271. 0xfc, 0x5b, 0xc0, 0x7e, 0x1b, 0xaa, 0xc0, 0xb4,
  19272. 0x34, 0x85, 0x49, 0x8e, 0x16, 0x31, 0x98, 0xdf,
  19273. 0x10, 0x54, 0x22, 0xda, 0x1e, 0x6b, 0x51, 0xf6,
  19274. 0x97, 0x57, 0xa0, 0x7a, 0x92, 0xe7, 0x47, 0x52
  19275. },
  19276. {
  19277. 0x80, 0xfa, 0x36, 0x30, 0xb8, 0x65, 0xb3, 0x2a, 0x1d, 0x68, 0x91, 0x06,
  19278. 0x98, 0xa0, 0x17, 0x8f, 0xee, 0xb7, 0x9e, 0x3d, 0xd8, 0x84, 0x99, 0x30,
  19279. 0xb9, 0xd6, 0x09, 0x25, 0x5e, 0xfb, 0x8f, 0xd3 },
  19280. {
  19281. 0xa9, 0x89, 0x29, 0x70, 0xe4, 0x55, 0xec, 0x97, 0xfb, 0x24, 0x5b, 0xf9,
  19282. 0xf1, 0xa3, 0x19, 0x3d, 0xf1, 0x31, 0x14, 0xcd, 0x2a, 0xed, 0x21, 0xc8,
  19283. 0xb1, 0x53, 0xad, 0x11, 0x0b, 0x9e, 0x5a, 0xee },
  19284. {
  19285. 0x72, 0xad, 0x8d, 0x7f, 0xfc, 0xb7, 0x68, 0xda, 0x27, 0x60, 0x37, 0xa3,
  19286. 0x4a, 0x63, 0xe8, 0xa5, 0xc8, 0xcd, 0x36, 0x6a, 0x77, 0x99, 0x0d, 0xa9,
  19287. 0xb1, 0x5b, 0x2f, 0x47, 0x2e, 0x22, 0xa7, 0x5e },
  19288. {
  19289. 0x95, 0x6e, 0x85, 0x09, 0xe5, 0x04, 0x88, 0x14, 0x28, 0x8d, 0xdf, 0xe6,
  19290. 0x0d, 0x0f, 0x0d, 0x6b, 0x4e, 0x66, 0x1c, 0x03, 0xb9, 0xaa, 0x2d, 0x45,
  19291. 0x56, 0x67, 0x5c, 0x55, 0x29, 0xd6, 0x89, 0xd0 },
  19292. {
  19293. 0xe8, 0xf2, 0x14, 0xf9, 0x9b, 0x2b, 0x9f, 0x24, 0x2b, 0x37, 0xbe, 0x86,
  19294. 0xdb, 0x23, 0x4b, 0xbe, 0x39, 0x57, 0xe8, 0xa9, 0xa5, 0xee, 0x08, 0xf2,
  19295. 0x75, 0x58, 0xdb, 0xd9, 0x51, 0xc1, 0x46, 0x02 },
  19296. {
  19297. 0x3d, 0x19, 0xaf, 0xa3, 0x0b, 0x21, 0xf7, 0x3d, 0xe7, 0x37, 0x6e, 0x32,
  19298. 0x13, 0x48, 0x9d, 0xea, 0xe0, 0x90, 0xbf, 0x64, 0x48, 0xf7, 0x1e, 0xcc,
  19299. 0xf0, 0xbc, 0x92, 0xd7, 0x8a, 0x4a, 0xa8, 0xc1 },
  19300. {
  19301. 0x16, 0x35, 0xb1, 0x66, 0x28, 0xa3, 0x3e, 0x19, 0xf5, 0x2d, 0x92, 0x22,
  19302. 0x95, 0x48, 0xe8, 0x34, 0x7b, 0x30, 0x50, 0xa2, 0xa0, 0xd9, 0xc2, 0x59,
  19303. 0x39, 0xf9, 0x8c, 0x69, 0xf2, 0x2a, 0xb9, 0xff },
  19304. {
  19305. 0x32, 0x71, 0xa6, 0x87, 0x0c, 0x97, 0x42, 0x07, 0xdd, 0x5f, 0xc9, 0x44,
  19306. 0xa5, 0x7c, 0x50, 0x14, 0xfd, 0xe7, 0x5f, 0x8b, 0xd3, 0x2f, 0xdc, 0x9b,
  19307. 0xa9, 0x93, 0x22, 0x19, 0xe6, 0xf2, 0x0c, 0xd8 }
  19308. },
  19309. #ifdef WOLFSSL_SHA384
  19310. { /* 26 */
  19311. WC_HASH_TYPE_SHA384, 35, 35,
  19312. { /* PSK */
  19313. 0x62, 0x83, 0x25, 0xc7, 0xcc, 0x08, 0x5e, 0x63,
  19314. 0x64, 0x56, 0xf0, 0xc6, 0x88, 0x27, 0x5a, 0x5b,
  19315. 0x68, 0x59, 0x0b, 0x14, 0x55, 0x13, 0x2e, 0xfd,
  19316. 0x8f, 0x28, 0x5b, 0x3d, 0xe3, 0xad, 0x67, 0xe4,
  19317. 0x68, 0xba, 0xf9
  19318. },
  19319. { /* DHE */
  19320. 0xa8, 0xb1, 0xab, 0xd8, 0xc8, 0x5b, 0x52, 0xdf,
  19321. 0x7f, 0x49, 0x10, 0xf4, 0xa1, 0x31, 0xd1, 0x91,
  19322. 0x36, 0xc1, 0x87, 0x5d, 0x42, 0x2a, 0xe7, 0x1d,
  19323. 0x2c, 0x29, 0x3d, 0x40, 0x64, 0x61, 0x63, 0x76,
  19324. 0xd8, 0x66, 0xac
  19325. },
  19326. { /* Hello 1 */
  19327. 0x6f, 0xc6, 0x4c, 0xe1, 0xc6, 0x68, 0x34, 0x8c,
  19328. 0x0a, 0xe1, 0xf8, 0xb8, 0x3e, 0xd4, 0xf8, 0x0b,
  19329. 0x54, 0x50, 0xe4, 0xc5, 0x4a, 0x33, 0x7d, 0xbd,
  19330. 0x90, 0xd2, 0xa2, 0xb9, 0xb7, 0x92, 0xed, 0xab,
  19331. 0x14, 0xf1, 0xe4, 0x86, 0x22, 0x67, 0xd7, 0x44,
  19332. 0x03, 0x21, 0xdc, 0x51, 0x52, 0x7f, 0x35, 0x80
  19333. },
  19334. { /* Hello 2 */
  19335. 0x3e, 0xcf, 0x2f, 0xc3, 0x87, 0xba, 0xc5, 0xbd,
  19336. 0x7c, 0xe8, 0x35, 0x5b, 0x95, 0x51, 0x30, 0x3b,
  19337. 0x08, 0xcc, 0x2a, 0x7d, 0xb5, 0x74, 0x7c, 0x16,
  19338. 0xb3, 0x0b, 0xe7, 0x61, 0xa3, 0x7c, 0x6c, 0xbd,
  19339. 0x39, 0x74, 0xfd, 0x1e, 0x4c, 0xff, 0xc8, 0xcc,
  19340. 0xa0, 0xef, 0x29, 0x4d, 0x94, 0xaa, 0x55, 0x6f,
  19341. },
  19342. { /* Finished 1 */
  19343. 0x06, 0xc1, 0x47, 0x78, 0x66, 0x53, 0x6f, 0x24,
  19344. 0x94, 0x61, 0x69, 0xec, 0xd8, 0x60, 0x31, 0x2f,
  19345. 0xbf, 0xd6, 0x8a, 0x29, 0x17, 0xff, 0xa3, 0x88,
  19346. 0x13, 0x09, 0x8c, 0x9d, 0x6c, 0x64, 0x84, 0x48,
  19347. 0x44, 0xdd, 0x2d, 0x29, 0x4d, 0xe6, 0x98, 0x2b,
  19348. 0x45, 0x3b, 0x84, 0x33, 0x79, 0xb2, 0x75, 0x68
  19349. },
  19350. { /* Finished 2 */
  19351. 0x28, 0x1e, 0x18, 0xf7, 0x9c, 0x32, 0xa9, 0xbf,
  19352. 0x0c, 0x24, 0x58, 0x21, 0xce, 0xbc, 0xf2, 0x44,
  19353. 0xb1, 0x18, 0xaf, 0x9d, 0xd9, 0x20, 0xf9, 0xf4,
  19354. 0xed, 0xcc, 0x53, 0x82, 0x66, 0x5c, 0x46, 0x94,
  19355. 0x8c, 0x36, 0x5e, 0xca, 0x9f, 0xd8, 0x9a, 0xd3,
  19356. 0xf0, 0xe1, 0x53, 0x71, 0xdd, 0x19, 0x1e, 0x59
  19357. },
  19358. {
  19359. 0xd0, 0xef, 0xa8, 0xcb, 0x5b, 0x14, 0x0f, 0x0a, 0x62, 0xba, 0x5a, 0xb1,
  19360. 0xc5, 0xb5, 0x3f, 0x11, 0xda, 0xa1, 0x0c, 0x9c, 0xb4, 0x32, 0x48, 0x4e,
  19361. 0xfa, 0x84, 0x4f, 0xe4, 0xe7, 0x91, 0x8f, 0x42, 0x3f, 0xc7, 0x4e, 0xd3,
  19362. 0x83, 0x3d, 0x7f, 0x70, 0x12, 0xee, 0x9a, 0x37, 0x01, 0xbb, 0x14, 0xd3
  19363. },
  19364. {
  19365. 0x48, 0x6f, 0x77, 0x1d, 0x39, 0x1b, 0xa5, 0x9a, 0x76, 0xd9, 0x1d, 0x7d,
  19366. 0xb3, 0xd9, 0xb9, 0x78, 0x35, 0x0f, 0xd0, 0xe1, 0x07, 0x1f, 0x8d, 0xe5,
  19367. 0x75, 0x00, 0xda, 0xc0, 0x19, 0x01, 0xfb, 0x08, 0x35, 0xe7, 0x18, 0x8f,
  19368. 0xf0, 0x19, 0xfb, 0x46, 0xf6, 0xa5, 0x77, 0x0e, 0x90, 0x38, 0x8b, 0x15
  19369. },
  19370. {
  19371. 0x80, 0x8c, 0xa7, 0x24, 0x97, 0xf9, 0xd3, 0x52, 0xb0, 0x69, 0x9d, 0x4b,
  19372. 0xa4, 0x19, 0x4a, 0xb1, 0x46, 0x53, 0x3a, 0xc8, 0xe4, 0x02, 0x69, 0xf2,
  19373. 0xe7, 0xb6, 0x1d, 0x33, 0x51, 0xcc, 0x14, 0x40, 0x4a, 0xb0, 0xe7, 0x58,
  19374. 0x84, 0xba, 0xc2, 0x14, 0x58, 0x6b, 0xb9, 0xdc, 0x50, 0x98, 0x67, 0x01
  19375. },
  19376. {
  19377. 0xb1, 0xa8, 0xc0, 0x06, 0xb3, 0x2e, 0xa7, 0x8a, 0x6a, 0x12, 0x88, 0x00,
  19378. 0x65, 0x88, 0x9c, 0x5d, 0x35, 0xee, 0xe5, 0x51, 0x0b, 0x62, 0xf8, 0x67,
  19379. 0xe5, 0xef, 0x15, 0x1f, 0x23, 0x02, 0x74, 0x08, 0x9c, 0xc8, 0xba, 0x27,
  19380. 0x5d, 0x32, 0x19, 0x6f, 0x6d, 0x5d, 0x72, 0x5e, 0x15, 0xde, 0x30, 0xc3
  19381. },
  19382. {
  19383. 0xfd, 0xce, 0xf5, 0x65, 0x45, 0x84, 0xfb, 0x8c, 0x79, 0xa4, 0x6c, 0x1b,
  19384. 0x0e, 0x1b, 0xfd, 0x26, 0xa2, 0x53, 0xf4, 0x4e, 0x00, 0x4d, 0x4b, 0x0b,
  19385. 0x24, 0x6d, 0x35, 0x35, 0xd9, 0x97, 0x70, 0xc5, 0xf4, 0xee, 0xe3, 0xba,
  19386. 0x31, 0x1e, 0x2a, 0x42, 0xcb, 0xdf, 0x40, 0xb1, 0x14, 0xb8, 0x53, 0xce
  19387. },
  19388. {
  19389. 0xbb, 0xb3, 0x26, 0x7c, 0x22, 0x21, 0x9b, 0x72, 0x32, 0xa1, 0x97, 0xfb,
  19390. 0x78, 0x8c, 0xbe, 0x3d, 0x71, 0x45, 0xb8, 0xf5, 0x24, 0x8f, 0x0f, 0xac,
  19391. 0x42, 0x5b, 0x81, 0xe8, 0xd0, 0x71, 0x4a, 0xcb, 0x32, 0x3f, 0x03, 0xfb,
  19392. 0xec, 0x6a, 0x1f, 0x76, 0x80, 0x65, 0x01, 0x7a, 0x3d, 0xce, 0xc4, 0xdf
  19393. },
  19394. {
  19395. 0x3f, 0xcf, 0x2f, 0x63, 0x94, 0x94, 0x99, 0xfd, 0x04, 0x3a, 0x89, 0x83,
  19396. 0xcf, 0x06, 0x05, 0xec, 0x20, 0x3e, 0x5f, 0x51, 0x9d, 0x6e, 0x4a, 0xc6,
  19397. 0xf1, 0x2b, 0x37, 0x17, 0x34, 0x72, 0x6e, 0x1d, 0x2a, 0xfd, 0xc7, 0x73,
  19398. 0xb5, 0x07, 0x22, 0x81, 0x32, 0x2e, 0x21, 0x85, 0xaf, 0x10, 0xb2, 0x73
  19399. },
  19400. {
  19401. 0x52, 0x0c, 0x3d, 0x2e, 0x2d, 0x4a, 0x11, 0xae, 0x96, 0x78, 0xe9, 0x5b,
  19402. 0xd8, 0x0f, 0x6c, 0xf4, 0xbd, 0x96, 0x13, 0x55, 0x88, 0xdd, 0xa3, 0x67,
  19403. 0x36, 0x86, 0x1e, 0x0b, 0x36, 0x41, 0xec, 0xf6, 0x04, 0xb2, 0xc4, 0x16,
  19404. 0xbc, 0x2c, 0xdb, 0x30, 0x02, 0x94, 0xd4, 0x42, 0xbf, 0x38, 0xee, 0x9d
  19405. }
  19406. },
  19407. { /* 36 */
  19408. WC_HASH_TYPE_SHA384, 0, 33,
  19409. { 0 }, /* PSK */
  19410. { /* DHE */
  19411. 0xd3, 0x00, 0x72, 0x9a, 0xa8, 0xc5, 0xf3, 0xc4,
  19412. 0xf1, 0xa0, 0x26, 0x89, 0x65, 0x70, 0xc7, 0x0b,
  19413. 0x77, 0xbb, 0xe1, 0x4b, 0x2b, 0xa8, 0x4f, 0xa6,
  19414. 0x09, 0x4b, 0xba, 0x45, 0x36, 0x15, 0xee, 0x68,
  19415. 0xfd
  19416. },
  19417. { /* Hello 1 */
  19418. 0x10, 0x9d, 0x8b, 0xa2, 0x93, 0xe7, 0xd3, 0xb9,
  19419. 0xb4, 0x0f, 0xeb, 0x6a, 0xb9, 0x69, 0xcb, 0x39,
  19420. 0x16, 0x29, 0xcc, 0xd3, 0xcc, 0x1a, 0x4c, 0x1b,
  19421. 0x53, 0x7c, 0x33, 0x88, 0x06, 0xbc, 0x0a, 0x02,
  19422. 0xa0, 0xbe, 0x62, 0xc0, 0xe6, 0x5e, 0x97, 0x5b,
  19423. 0x6a, 0xa1, 0x98, 0xf3, 0xd2, 0x1e, 0xcd, 0xc5
  19424. },
  19425. { /* Hello 2 */
  19426. 0x74, 0xc0, 0x07, 0x2c, 0xc1, 0x63, 0xcc, 0x11,
  19427. 0xad, 0x1a, 0x55, 0x63, 0xbc, 0x20, 0x77, 0x96,
  19428. 0x30, 0x1c, 0x68, 0x45, 0x1e, 0x9b, 0xa7, 0xb4,
  19429. 0xf3, 0x04, 0x45, 0x16, 0x76, 0x55, 0xf9, 0xdf,
  19430. 0x4b, 0x2f, 0x1a, 0xdf, 0x5a, 0xb0, 0x93, 0xc9,
  19431. 0xab, 0xf5, 0x32, 0x47, 0x79, 0x9c, 0x01, 0xeb
  19432. },
  19433. { /* Finished 1 */
  19434. 0x27, 0x08, 0x8e, 0xa5, 0xf1, 0x30, 0xe1, 0xd6,
  19435. 0x4f, 0xa2, 0x9e, 0x3b, 0x03, 0x2d, 0x2e, 0xa3,
  19436. 0x84, 0x75, 0x51, 0x3a, 0xc3, 0xf6, 0xee, 0x2e,
  19437. 0x37, 0x0c, 0xe3, 0x28, 0x46, 0xa5, 0x2d, 0xc7,
  19438. 0xf0, 0x64, 0x78, 0x53, 0x66, 0x43, 0x02, 0xa4,
  19439. 0x7a, 0x43, 0x66, 0x4b, 0xa7, 0xcb, 0x97, 0x16
  19440. },
  19441. { /* Finished 2 */
  19442. 0x1d, 0x0d, 0xf8, 0xe1, 0x81, 0xa5, 0xbd, 0xa8,
  19443. 0x6f, 0x9d, 0x01, 0xa4, 0x9a, 0x92, 0xe2, 0xef,
  19444. 0x08, 0xab, 0xef, 0x3e, 0x2d, 0xd4, 0x82, 0xac,
  19445. 0x68, 0x9d, 0xe0, 0x54, 0x17, 0xde, 0x1a, 0xed,
  19446. 0x57, 0xcb, 0xd9, 0x2d, 0xc8, 0xbc, 0x93, 0xe6,
  19447. 0xa3, 0xec, 0xde, 0xee, 0xa1, 0x1c, 0x41, 0x85
  19448. },
  19449. {
  19450. 0x7f, 0x1f, 0xe6, 0x7b, 0xd8, 0xf5, 0x2b, 0x37, 0xbe, 0xb7, 0xd0, 0x37,
  19451. 0xce, 0x46, 0xad, 0x04, 0x2f, 0xc7, 0xdb, 0xc9, 0x9a, 0xb6, 0x00, 0x3f,
  19452. 0xc1, 0x97, 0xe9, 0x5c, 0x5e, 0x14, 0xd1, 0x38, 0x4d, 0x55, 0xe1, 0x07,
  19453. 0xb5, 0x85, 0x6d, 0xfa, 0xa7, 0x66, 0xad, 0xfa, 0xb6, 0xad, 0x29, 0x44
  19454. },
  19455. {
  19456. 0x4e, 0x6b, 0x20, 0x99, 0x55, 0x1b, 0x21, 0x89, 0xb6, 0x70, 0xdb, 0xe8,
  19457. 0xa7, 0x16, 0x55, 0xf2, 0x93, 0x13, 0x90, 0x7d, 0xfa, 0x62, 0x65, 0x53,
  19458. 0xa0, 0x97, 0xe9, 0xb4, 0xc0, 0xf1, 0xc9, 0x1a, 0x67, 0xdd, 0xca, 0x57,
  19459. 0xbc, 0xca, 0x39, 0xe6, 0x39, 0x6b, 0x63, 0x47, 0x25, 0x08, 0x3a, 0xd7
  19460. },
  19461. {
  19462. 0x35, 0x0d, 0xac, 0xd8, 0x10, 0x6a, 0x46, 0x50, 0x66, 0xae, 0x02, 0xc9,
  19463. 0xde, 0x13, 0x48, 0xce, 0x53, 0xd4, 0x92, 0x62, 0xc5, 0x65, 0x10, 0x08,
  19464. 0xc2, 0xc2, 0x82, 0xed, 0x9d, 0xc9, 0x6f, 0xa8, 0xc3, 0xc1, 0x0b, 0x7c,
  19465. 0xe1, 0x97, 0x85, 0xd6, 0x46, 0x29, 0x0e, 0x42, 0x51, 0xc1, 0x35, 0xcf
  19466. },
  19467. {
  19468. 0x3d, 0x5d, 0x84, 0xbd, 0x16, 0x46, 0x34, 0xb3, 0xf6, 0x31, 0x49, 0x3e,
  19469. 0x8d, 0xdc, 0xcb, 0x8c, 0x6a, 0x42, 0xf4, 0x88, 0xfc, 0x19, 0xfa, 0xa2,
  19470. 0x25, 0xc7, 0xa0, 0xa4, 0xca, 0xf0, 0xea, 0x2d, 0xe8, 0xc4, 0x02, 0x14,
  19471. 0x63, 0xfb, 0xd3, 0x7b, 0x51, 0x1c, 0xce, 0xca, 0xa3, 0xc3, 0xe4, 0xa5
  19472. },
  19473. {
  19474. 0x7c, 0x3a, 0x55, 0x92, 0x2e, 0xdd, 0x75, 0xdd, 0x76, 0x54, 0x4a, 0x9f,
  19475. 0xd0, 0xa2, 0x88, 0x83, 0xe9, 0x27, 0xda, 0x30, 0xe9, 0x96, 0x58, 0xc5,
  19476. 0xb7, 0x56, 0xfc, 0x4b, 0xb8, 0x5d, 0xee, 0x46, 0x70, 0x4e, 0x1b, 0x06,
  19477. 0x86, 0xaf, 0x48, 0x5c, 0x17, 0x35, 0xfa, 0x69, 0xc2, 0x4d, 0xfb, 0x09
  19478. },
  19479. {
  19480. 0x00, 0x0e, 0x28, 0x51, 0xc1, 0x7f, 0x41, 0x89, 0x6f, 0x9a, 0xca, 0x15,
  19481. 0xee, 0xed, 0x43, 0xca, 0x6d, 0x65, 0x6f, 0x51, 0x18, 0x6c, 0x08, 0x4b,
  19482. 0x77, 0xca, 0x75, 0xc4, 0xc3, 0xde, 0x29, 0x41, 0x8b, 0xaf, 0xa7, 0x1c,
  19483. 0x28, 0x37, 0xa0, 0xa0, 0x74, 0x8e, 0x09, 0x42, 0x7a, 0x1b, 0x68, 0xdb
  19484. },
  19485. {
  19486. 0x14, 0x8f, 0xab, 0x28, 0x64, 0xea, 0x45, 0x88, 0xdb, 0xc1, 0xc6, 0xa0,
  19487. 0x48, 0xdf, 0x15, 0xd0, 0x28, 0x07, 0x2d, 0x6c, 0xb8, 0x42, 0xbb, 0x60,
  19488. 0x02, 0x08, 0x9e, 0x29, 0x9b, 0x8d, 0xd6, 0x1c, 0xaf, 0xf2, 0x1a, 0xdc,
  19489. 0xf0, 0x78, 0x0b, 0x4d, 0x90, 0xa1, 0x0c, 0xb3, 0x13, 0xde, 0xca, 0x5a
  19490. },
  19491. {
  19492. 0x4d, 0x80, 0x7d, 0x0b, 0xb9, 0x00, 0x6f, 0x65, 0x51, 0x65, 0x23, 0xde,
  19493. 0x72, 0xdc, 0x4f, 0x04, 0xa5, 0xa2, 0x90, 0x45, 0x51, 0x9e, 0xd0, 0x3a,
  19494. 0xe4, 0xd7, 0x78, 0xa3, 0x0f, 0x2d, 0x65, 0x12, 0xad, 0xc8, 0x92, 0x30,
  19495. 0x79, 0x9d, 0x9d, 0x08, 0x7a, 0x9c, 0x9f, 0x83, 0xb1, 0xca, 0x59, 0x56
  19496. }
  19497. },
  19498. { /* 41 */
  19499. WC_HASH_TYPE_SHA384, 33, 0,
  19500. { /* PSK */
  19501. 0xa4, 0x8b, 0x1b, 0x5f, 0xd0, 0xea, 0x75, 0x62,
  19502. 0x06, 0x4d, 0x68, 0x40, 0x85, 0x20, 0x45, 0x95,
  19503. 0x4a, 0x00, 0xca, 0x05, 0xeb, 0xd4, 0x1d, 0x48,
  19504. 0x81, 0x89, 0xe8, 0x86, 0x43, 0xfa, 0x28, 0x17,
  19505. 0x12
  19506. },
  19507. { 0 }, /* DHE */
  19508. { /* Hello 1 */
  19509. 0x03, 0x7c, 0x33, 0x75, 0xdc, 0xc5, 0x46, 0x3a,
  19510. 0x0d, 0x56, 0xc6, 0xfb, 0xab, 0x1e, 0x1d, 0xda,
  19511. 0x59, 0xc2, 0xb2, 0xb1, 0x7c, 0x48, 0x9b, 0x06,
  19512. 0x0a, 0x5a, 0xbb, 0xf8, 0x98, 0x53, 0x78, 0x2d,
  19513. 0xd2, 0xcc, 0x87, 0x68, 0x25, 0xdd, 0x88, 0x22,
  19514. 0xcd, 0xb7, 0x74, 0x55, 0x21, 0xf9, 0x34, 0x98
  19515. },
  19516. { /* Hello 2 */
  19517. 0x03, 0xb4, 0xfb, 0xcc, 0x28, 0x2c, 0xc1, 0x70,
  19518. 0x42, 0x73, 0x57, 0xac, 0xdb, 0x47, 0x71, 0xf6,
  19519. 0x2e, 0x11, 0x8a, 0x5b, 0x47, 0x2f, 0x02, 0x54,
  19520. 0x95, 0x34, 0xed, 0x5f, 0x19, 0xc1, 0x75, 0xe0,
  19521. 0x76, 0xad, 0xb0, 0x90, 0x57, 0xcd, 0xfd, 0xd7,
  19522. 0x58, 0x1f, 0x0d, 0x6b, 0x9e, 0x51, 0x3c, 0x08
  19523. },
  19524. { /* Finished 1 */
  19525. 0x2b, 0x50, 0xd9, 0xa7, 0x43, 0x24, 0xda, 0x2c,
  19526. 0x7a, 0xaa, 0x0e, 0x37, 0xd7, 0x6b, 0x2c, 0xab,
  19527. 0x8e, 0xb2, 0xfe, 0x31, 0x1b, 0xa8, 0x12, 0x59,
  19528. 0x5b, 0x7b, 0xdc, 0x3e, 0xa7, 0x86, 0xa5, 0x48,
  19529. 0xe4, 0x46, 0x2b, 0x4c, 0xc1, 0x66, 0x4b, 0xf3,
  19530. 0x2a, 0x99, 0x93, 0x08, 0xbc, 0x3d, 0x08, 0x76
  19531. },
  19532. { /* Finished 2 */
  19533. 0x7c, 0x34, 0xc8, 0x56, 0x17, 0xf1, 0x62, 0x1c,
  19534. 0x9f, 0x0b, 0xeb, 0xfd, 0x69, 0x72, 0x51, 0xc5,
  19535. 0xfa, 0x74, 0x87, 0xc9, 0xbd, 0x50, 0xe9, 0x48,
  19536. 0xa7, 0x3c, 0x94, 0x3e, 0x06, 0x7d, 0xe8, 0x8e,
  19537. 0xc1, 0xd1, 0x08, 0x1f, 0x5d, 0x48, 0x8a, 0x25,
  19538. 0xfc, 0xea, 0xe7, 0xd9, 0xd4, 0xd0, 0xf9, 0xad
  19539. },
  19540. {
  19541. 0x4b, 0x0b, 0xed, 0xb9, 0xc8, 0xb8, 0xa8, 0x1e, 0xb0, 0x81, 0x76, 0xd5,
  19542. 0x33, 0x22, 0x71, 0x33, 0x3a, 0x85, 0x19, 0x67, 0x7e, 0x91, 0x37, 0xf2,
  19543. 0xa6, 0x11, 0x22, 0xdf, 0x41, 0x04, 0x3d, 0xa9, 0x13, 0xb9, 0xb2, 0xb1,
  19544. 0xbb, 0xd8, 0xef, 0x23, 0x7c, 0xc2, 0xab, 0x70, 0x1b, 0x51, 0x9f, 0xc9
  19545. },
  19546. {
  19547. 0xeb, 0x96, 0x10, 0x8c, 0x7d, 0x92, 0xea, 0x80, 0x86, 0xb2, 0xf8, 0x27,
  19548. 0xf2, 0x9a, 0x09, 0xc1, 0x7c, 0x09, 0x43, 0xbc, 0xfe, 0xc8, 0x75, 0xe0,
  19549. 0x97, 0xe7, 0x6d, 0xd5, 0xb2, 0x3c, 0xed, 0x12, 0xb7, 0x74, 0x0e, 0xe3,
  19550. 0xb6, 0xe0, 0xba, 0xe1, 0x8d, 0x89, 0xcf, 0x4f, 0x57, 0xf6, 0x6d, 0x90
  19551. },
  19552. {
  19553. 0x22, 0xb0, 0x39, 0x34, 0xb6, 0x6c, 0x2d, 0x7a, 0x97, 0x1c, 0x5d, 0xcc,
  19554. 0x78, 0x84, 0x71, 0xbb, 0xc6, 0x7b, 0xb6, 0xbc, 0xcc, 0x0b, 0xf8, 0xac,
  19555. 0x8e, 0xd7, 0x20, 0xbd, 0xbe, 0x32, 0xf0, 0xd6, 0xe9, 0x69, 0x13, 0xf2,
  19556. 0x9a, 0xce, 0xfe, 0x86, 0xd3, 0xee, 0xba, 0x69, 0x51, 0xb6, 0x77, 0x56
  19557. },
  19558. {
  19559. 0x16, 0xfd, 0xda, 0xf3, 0x5e, 0xb9, 0xa6, 0x17, 0x24, 0xb2, 0x16, 0x9f,
  19560. 0xb6, 0x59, 0x13, 0x0f, 0x25, 0x5a, 0xf1, 0x5b, 0x5f, 0xe4, 0x54, 0x2a,
  19561. 0xa7, 0xbf, 0x29, 0xaf, 0x5a, 0x77, 0xf4, 0x4f, 0x25, 0xba, 0x94, 0xad,
  19562. 0x6b, 0x91, 0x3b, 0xe7, 0xd5, 0x73, 0x0d, 0xff, 0xaa, 0xe3, 0x72, 0x2c
  19563. },
  19564. {
  19565. 0x22, 0xb4, 0x94, 0xc0, 0x53, 0xd7, 0x82, 0x06, 0x38, 0x9d, 0x4a, 0xa0,
  19566. 0x3f, 0xf1, 0x5f, 0x6e, 0x23, 0x8d, 0x09, 0x62, 0xbf, 0x6f, 0x7c, 0x84,
  19567. 0xc6, 0x3e, 0x15, 0xad, 0x18, 0x37, 0x76, 0x29, 0xc7, 0xd6, 0x68, 0x0c,
  19568. 0x1e, 0xc6, 0x93, 0x31, 0xef, 0x85, 0x69, 0x30, 0x68, 0xf0, 0x1e, 0x37
  19569. },
  19570. {
  19571. 0x6d, 0x4d, 0x20, 0xaf, 0x47, 0xe8, 0x1b, 0xfa, 0xd0, 0xb6, 0xc8, 0x97,
  19572. 0xd1, 0x03, 0xfc, 0x9d, 0x59, 0xa0, 0x68, 0x9d, 0xe9, 0x17, 0x8b, 0xce,
  19573. 0x48, 0x2c, 0x77, 0x8a, 0x22, 0x4b, 0x5c, 0x54, 0x22, 0xa1, 0x15, 0x12,
  19574. 0xe1, 0x07, 0x8e, 0x15, 0xd8, 0x7b, 0x16, 0x65, 0x99, 0x6b, 0xcb, 0x71
  19575. },
  19576. {
  19577. 0x79, 0x64, 0x79, 0xdd, 0x75, 0x5c, 0x6f, 0x98, 0xac, 0x03, 0xe0, 0xcd,
  19578. 0x92, 0xba, 0x0e, 0x2d, 0xb4, 0xd1, 0x8b, 0x97, 0xd0, 0x85, 0xbb, 0x2e,
  19579. 0x4f, 0x26, 0x93, 0xf5, 0x1d, 0xf3, 0xd2, 0x43, 0x4f, 0xd2, 0x47, 0xaa,
  19580. 0x91, 0x1e, 0xf3, 0x67, 0x10, 0x18, 0x2c, 0xb9, 0x01, 0xba, 0x10, 0x9f
  19581. },
  19582. {
  19583. 0x79, 0xb6, 0x9c, 0xbe, 0xf1, 0x6a, 0xb0, 0x92, 0xa0, 0x29, 0x52, 0x61,
  19584. 0xf1, 0xcd, 0x3a, 0x67, 0xe1, 0x6b, 0xb8, 0x9d, 0x0d, 0x95, 0xb6, 0x03,
  19585. 0x80, 0x1f, 0xd5, 0x75, 0xb6, 0x1d, 0x79, 0x02, 0x93, 0x43, 0x77, 0xa7,
  19586. 0x9d, 0x2f, 0xc3, 0x84, 0xc6, 0x83, 0x76, 0x16, 0x06, 0x98, 0x7b, 0x79
  19587. }
  19588. },
  19589. #endif /* WOLFSSL_SHA384 */
  19590. };
  19591. const char protocolLabel[] = "tls13 ";
  19592. const char ceTrafficLabel[] = "c e traffic";
  19593. const char eExpMasterLabel[] = "e exp master";
  19594. const char cHsTrafficLabel[] = "c hs traffic";
  19595. const char sHsTrafficLabel[] = "s hs traffic";
  19596. const char cAppTrafficLabel[] = "c ap traffic";
  19597. const char sAppTrafficLabel[] = "s ap traffic";
  19598. const char expMasterLabel[] = "exp master";
  19599. const char resMasterLabel[] = "res master";
  19600. const char derivedLabel[] = "derived";
  19601. WOLFSSL_TEST_SUBROUTINE int tls13_kdf_test(void)
  19602. {
  19603. int ret = 0;
  19604. word32 i;
  19605. word32 tc = sizeof(tls13KdfTestVectors)/sizeof(Tls13KdfTestVector);
  19606. const Tls13KdfTestVector* tv = NULL;
  19607. for (i = 0, tv = tls13KdfTestVectors; i < tc; i++, tv++) {
  19608. byte output[WC_MAX_DIGEST_SIZE];
  19609. byte secret[WC_MAX_DIGEST_SIZE];
  19610. byte salt[WC_MAX_DIGEST_SIZE];
  19611. byte zeroes[WC_MAX_DIGEST_SIZE];
  19612. byte hashZero[WC_MAX_DIGEST_SIZE];
  19613. int hashAlgSz;
  19614. XMEMSET(zeroes, 0, sizeof zeroes);
  19615. hashAlgSz = wc_HashGetDigestSize(tv->hashAlg);
  19616. if (hashAlgSz == BAD_FUNC_ARG) break;
  19617. ret = wc_Hash(tv->hashAlg, NULL, 0, hashZero, hashAlgSz);
  19618. if (ret != 0) break;
  19619. ret = wc_Tls13_HKDF_Extract(secret, NULL, 0,
  19620. (tv->pskSz == 0) ? zeroes : (byte*)tv->psk,
  19621. tv->pskSz, tv->hashAlg);
  19622. if (ret != 0) break;
  19623. ret = wc_Tls13_HKDF_Expand_Label(output, hashAlgSz,
  19624. secret, hashAlgSz,
  19625. (byte*)protocolLabel, (word32)strlen(protocolLabel),
  19626. (byte*)ceTrafficLabel, (word32)strlen(ceTrafficLabel),
  19627. tv->hashHello1, hashAlgSz, tv->hashAlg);
  19628. if (ret != 0) break;
  19629. ret = XMEMCMP(tv->clientEarlyTrafficSecret, output, hashAlgSz);
  19630. if (ret != 0) break;
  19631. ret = wc_Tls13_HKDF_Expand_Label(output, hashAlgSz,
  19632. secret, hashAlgSz,
  19633. (byte*)protocolLabel, (word32)strlen(protocolLabel),
  19634. (byte*)eExpMasterLabel, (word32)strlen(eExpMasterLabel),
  19635. tv->hashHello1, hashAlgSz, tv->hashAlg);
  19636. if (ret != 0) break;
  19637. ret = XMEMCMP(tv->earlyExporterMasterSecret, output, hashAlgSz);
  19638. if (ret != 0) break;
  19639. ret = wc_Tls13_HKDF_Expand_Label(salt, hashAlgSz,
  19640. secret, hashAlgSz,
  19641. (byte*)protocolLabel, (word32)strlen(protocolLabel),
  19642. (byte*)derivedLabel, (word32)strlen(derivedLabel),
  19643. hashZero, hashAlgSz, tv->hashAlg);
  19644. if (ret != 0) break;
  19645. ret = wc_Tls13_HKDF_Extract(secret, salt, hashAlgSz,
  19646. (tv->dheSz == 0) ? zeroes : (byte*)tv->dhe,
  19647. tv->dheSz, tv->hashAlg);
  19648. if (ret != 0) break;
  19649. ret = wc_Tls13_HKDF_Expand_Label(output, hashAlgSz,
  19650. secret, hashAlgSz,
  19651. (byte*)protocolLabel, (word32)strlen(protocolLabel),
  19652. (byte*)cHsTrafficLabel, (word32)strlen(cHsTrafficLabel),
  19653. tv->hashHello2, hashAlgSz, tv->hashAlg);
  19654. if (ret != 0) break;
  19655. ret = XMEMCMP(tv->clientHandshakeTrafficSecret,
  19656. output, hashAlgSz);
  19657. if (ret != 0) break;
  19658. ret = wc_Tls13_HKDF_Expand_Label(output, hashAlgSz,
  19659. secret, hashAlgSz,
  19660. (byte*)protocolLabel, (word32)strlen(protocolLabel),
  19661. (byte*)sHsTrafficLabel, (word32)strlen(sHsTrafficLabel),
  19662. tv->hashHello2, hashAlgSz, tv->hashAlg);
  19663. if (ret != 0) break;
  19664. ret = XMEMCMP(tv->serverHandshakeTrafficSecret, output, hashAlgSz);
  19665. if (ret != 0) break;
  19666. ret = wc_Tls13_HKDF_Expand_Label(salt, hashAlgSz,
  19667. secret, hashAlgSz,
  19668. (byte*)protocolLabel, (word32)strlen(protocolLabel),
  19669. (byte*)derivedLabel, (word32)strlen(derivedLabel),
  19670. hashZero, hashAlgSz, tv->hashAlg);
  19671. if (ret != 0) break;
  19672. ret = wc_Tls13_HKDF_Extract(secret, salt, hashAlgSz,
  19673. zeroes, hashAlgSz, tv->hashAlg);
  19674. if (ret != 0) break;
  19675. ret = wc_Tls13_HKDF_Expand_Label(output, hashAlgSz,
  19676. secret, hashAlgSz,
  19677. (byte*)protocolLabel, (word32)strlen(protocolLabel),
  19678. (byte*)cAppTrafficLabel, (word32)strlen(cAppTrafficLabel),
  19679. tv->hashFinished1, hashAlgSz, tv->hashAlg);
  19680. if (ret != 0) break;
  19681. ret = XMEMCMP(tv->clientApplicationTrafficSecret, output, hashAlgSz);
  19682. if (ret != 0) break;
  19683. ret = wc_Tls13_HKDF_Expand_Label(output, hashAlgSz,
  19684. secret, hashAlgSz,
  19685. (byte*)protocolLabel, (word32)strlen(protocolLabel),
  19686. (byte*)sAppTrafficLabel, (word32)strlen(sAppTrafficLabel),
  19687. tv->hashFinished1, hashAlgSz, tv->hashAlg);
  19688. if (ret != 0) break;
  19689. ret = XMEMCMP(tv->serverApplicationTrafficSecret, output, hashAlgSz);
  19690. if (ret != 0) break;
  19691. ret = wc_Tls13_HKDF_Expand_Label(output, hashAlgSz,
  19692. secret, hashAlgSz,
  19693. (byte*)protocolLabel, (word32)strlen(protocolLabel),
  19694. (byte*)expMasterLabel, (word32)strlen(expMasterLabel),
  19695. tv->hashFinished1, hashAlgSz, tv->hashAlg);
  19696. if (ret != 0) break;
  19697. ret = XMEMCMP(tv->exporterMasterSecret, output, hashAlgSz);
  19698. if (ret != 0) break;
  19699. ret = wc_Tls13_HKDF_Expand_Label(output, hashAlgSz,
  19700. secret, hashAlgSz,
  19701. (byte*)protocolLabel, (word32)strlen(protocolLabel),
  19702. (byte*)resMasterLabel, (word32)strlen(resMasterLabel),
  19703. tv->hashFinished2, hashAlgSz, tv->hashAlg);
  19704. if (ret != 0) break;
  19705. ret = XMEMCMP(tv->resumptionMasterSecret, output, hashAlgSz);
  19706. if (ret != 0) break;
  19707. }
  19708. return ret;
  19709. }
  19710. #endif /* WOLFSSL_TLS13 */
  19711. #if defined(HAVE_ECC) && defined(HAVE_X963_KDF)
  19712. WOLFSSL_TEST_SUBROUTINE int x963kdf_test(void)
  19713. {
  19714. int ret;
  19715. byte kek[128];
  19716. #ifndef NO_SHA
  19717. /* SHA-1, COUNT = 0
  19718. * shared secret length: 192
  19719. * SharedInfo length: 0
  19720. * key data length: 128
  19721. */
  19722. WOLFSSL_SMALL_STACK_STATIC const byte Z[] = {
  19723. 0x1c, 0x7d, 0x7b, 0x5f, 0x05, 0x97, 0xb0, 0x3d,
  19724. 0x06, 0xa0, 0x18, 0x46, 0x6e, 0xd1, 0xa9, 0x3e,
  19725. 0x30, 0xed, 0x4b, 0x04, 0xdc, 0x64, 0xcc, 0xdd
  19726. };
  19727. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  19728. 0xbf, 0x71, 0xdf, 0xfd, 0x8f, 0x4d, 0x99, 0x22,
  19729. 0x39, 0x36, 0xbe, 0xb4, 0x6f, 0xee, 0x8c, 0xcc
  19730. };
  19731. #endif
  19732. #ifndef NO_SHA256
  19733. /* SHA-256, COUNT = 3
  19734. * shared secret length: 192
  19735. * SharedInfo length: 0
  19736. * key data length: 128
  19737. */
  19738. WOLFSSL_SMALL_STACK_STATIC const byte Z2[] = {
  19739. 0xd3, 0x8b, 0xdb, 0xe5, 0xc4, 0xfc, 0x16, 0x4c,
  19740. 0xdd, 0x96, 0x7f, 0x63, 0xc0, 0x4f, 0xe0, 0x7b,
  19741. 0x60, 0xcd, 0xe8, 0x81, 0xc2, 0x46, 0x43, 0x8c
  19742. };
  19743. WOLFSSL_SMALL_STACK_STATIC const byte verify2[] = {
  19744. 0x5e, 0x67, 0x4d, 0xb9, 0x71, 0xba, 0xc2, 0x0a,
  19745. 0x80, 0xba, 0xd0, 0xd4, 0x51, 0x4d, 0xc4, 0x84
  19746. };
  19747. #endif
  19748. #ifdef WOLFSSL_SHA512
  19749. /* SHA-512, COUNT = 0
  19750. * shared secret length: 192
  19751. * SharedInfo length: 0
  19752. * key data length: 128
  19753. */
  19754. WOLFSSL_SMALL_STACK_STATIC const byte Z3[] = {
  19755. 0x87, 0xfc, 0x0d, 0x8c, 0x44, 0x77, 0x48, 0x5b,
  19756. 0xb5, 0x74, 0xf5, 0xfc, 0xea, 0x26, 0x4b, 0x30,
  19757. 0x88, 0x5d, 0xc8, 0xd9, 0x0a, 0xd8, 0x27, 0x82
  19758. };
  19759. WOLFSSL_SMALL_STACK_STATIC const byte verify3[] = {
  19760. 0x94, 0x76, 0x65, 0xfb, 0xb9, 0x15, 0x21, 0x53,
  19761. 0xef, 0x46, 0x02, 0x38, 0x50, 0x6a, 0x02, 0x45
  19762. };
  19763. /* SHA-512, COUNT = 0
  19764. * shared secret length: 521
  19765. * SharedInfo length: 128
  19766. * key data length: 1024
  19767. */
  19768. WOLFSSL_SMALL_STACK_STATIC const byte Z4[] = {
  19769. 0x00, 0xaa, 0x5b, 0xb7, 0x9b, 0x33, 0xe3, 0x89,
  19770. 0xfa, 0x58, 0xce, 0xad, 0xc0, 0x47, 0x19, 0x7f,
  19771. 0x14, 0xe7, 0x37, 0x12, 0xf4, 0x52, 0xca, 0xa9,
  19772. 0xfc, 0x4c, 0x9a, 0xdb, 0x36, 0x93, 0x48, 0xb8,
  19773. 0x15, 0x07, 0x39, 0x2f, 0x1a, 0x86, 0xdd, 0xfd,
  19774. 0xb7, 0xc4, 0xff, 0x82, 0x31, 0xc4, 0xbd, 0x0f,
  19775. 0x44, 0xe4, 0x4a, 0x1b, 0x55, 0xb1, 0x40, 0x47,
  19776. 0x47, 0xa9, 0xe2, 0xe7, 0x53, 0xf5, 0x5e, 0xf0,
  19777. 0x5a, 0x2d
  19778. };
  19779. WOLFSSL_SMALL_STACK_STATIC const byte info4[] = {
  19780. 0xe3, 0xb5, 0xb4, 0xc1, 0xb0, 0xd5, 0xcf, 0x1d,
  19781. 0x2b, 0x3a, 0x2f, 0x99, 0x37, 0x89, 0x5d, 0x31
  19782. };
  19783. WOLFSSL_SMALL_STACK_STATIC const byte verify4[] = {
  19784. 0x44, 0x63, 0xf8, 0x69, 0xf3, 0xcc, 0x18, 0x76,
  19785. 0x9b, 0x52, 0x26, 0x4b, 0x01, 0x12, 0xb5, 0x85,
  19786. 0x8f, 0x7a, 0xd3, 0x2a, 0x5a, 0x2d, 0x96, 0xd8,
  19787. 0xcf, 0xfa, 0xbf, 0x7f, 0xa7, 0x33, 0x63, 0x3d,
  19788. 0x6e, 0x4d, 0xd2, 0xa5, 0x99, 0xac, 0xce, 0xb3,
  19789. 0xea, 0x54, 0xa6, 0x21, 0x7c, 0xe0, 0xb5, 0x0e,
  19790. 0xef, 0x4f, 0x6b, 0x40, 0xa5, 0xc3, 0x02, 0x50,
  19791. 0xa5, 0xa8, 0xee, 0xee, 0x20, 0x80, 0x02, 0x26,
  19792. 0x70, 0x89, 0xdb, 0xf3, 0x51, 0xf3, 0xf5, 0x02,
  19793. 0x2a, 0xa9, 0x63, 0x8b, 0xf1, 0xee, 0x41, 0x9d,
  19794. 0xea, 0x9c, 0x4f, 0xf7, 0x45, 0xa2, 0x5a, 0xc2,
  19795. 0x7b, 0xda, 0x33, 0xca, 0x08, 0xbd, 0x56, 0xdd,
  19796. 0x1a, 0x59, 0xb4, 0x10, 0x6c, 0xf2, 0xdb, 0xbc,
  19797. 0x0a, 0xb2, 0xaa, 0x8e, 0x2e, 0xfa, 0x7b, 0x17,
  19798. 0x90, 0x2d, 0x34, 0x27, 0x69, 0x51, 0xce, 0xcc,
  19799. 0xab, 0x87, 0xf9, 0x66, 0x1c, 0x3e, 0x88, 0x16
  19800. };
  19801. #endif
  19802. #ifndef NO_SHA
  19803. ret = wc_X963_KDF(WC_HASH_TYPE_SHA, Z, sizeof(Z), NULL, 0,
  19804. kek, sizeof(verify));
  19805. if (ret != 0)
  19806. return -9800;
  19807. if (XMEMCMP(verify, kek, sizeof(verify)) != 0)
  19808. return -9801;
  19809. #endif
  19810. #ifndef NO_SHA256
  19811. ret = wc_X963_KDF(WC_HASH_TYPE_SHA256, Z2, sizeof(Z2), NULL, 0,
  19812. kek, sizeof(verify2));
  19813. if (ret != 0)
  19814. return -9802;
  19815. if (XMEMCMP(verify2, kek, sizeof(verify2)) != 0)
  19816. return -9803;
  19817. #endif
  19818. #ifdef WOLFSSL_SHA512
  19819. ret = wc_X963_KDF(WC_HASH_TYPE_SHA512, Z3, sizeof(Z3), NULL, 0,
  19820. kek, sizeof(verify3));
  19821. if (ret != 0)
  19822. return -9804;
  19823. if (XMEMCMP(verify3, kek, sizeof(verify3)) != 0)
  19824. return -9805;
  19825. ret = wc_X963_KDF(WC_HASH_TYPE_SHA512, Z4, sizeof(Z4), info4,
  19826. sizeof(info4), kek, sizeof(verify4));
  19827. if (ret != 0)
  19828. return -9806;
  19829. if (XMEMCMP(verify4, kek, sizeof(verify4)) != 0)
  19830. return -9807;
  19831. #endif
  19832. return 0;
  19833. }
  19834. #endif /* HAVE_X963_KDF */
  19835. #ifdef HAVE_ECC
  19836. /* size to use for ECC key gen tests */
  19837. #ifndef ECC_KEYGEN_SIZE
  19838. #ifndef NO_ECC256
  19839. #define ECC_KEYGEN_SIZE 32
  19840. #elif defined(HAVE_ECC384)
  19841. #define ECC_KEYGEN_SIZE 48
  19842. #elif defined(HAVE_ECC224)
  19843. #define ECC_KEYGEN_SIZE 28
  19844. #elif defined(HAVE_ECC521)
  19845. #define ECC_KEYGEN_SIZE 66
  19846. #else
  19847. #error No ECC keygen size defined for test
  19848. #endif
  19849. #endif
  19850. #ifdef BENCH_EMBEDDED
  19851. #define ECC_SHARED_SIZE 128
  19852. #else
  19853. #define ECC_SHARED_SIZE MAX_ECC_BYTES
  19854. #endif
  19855. #if defined(WOLFSSL_ECDSA_DETERMINISTIC_K) || defined(WOLFSSL_ECDSA_DETERMINISTIC_K_VARIANT)
  19856. #define HAVE_ECC_DETERMINISTIC_K
  19857. #define ECC_DIGEST_SIZE WC_SHA256_DIGEST_SIZE
  19858. #else
  19859. #define ECC_DIGEST_SIZE MAX_ECC_BYTES
  19860. #endif
  19861. #define ECC_SIG_SIZE ECC_MAX_SIG_SIZE
  19862. #ifndef NO_ECC_VECTOR_TEST
  19863. #if (defined(HAVE_ECC192) || defined(HAVE_ECC224) ||\
  19864. !defined(NO_ECC256) || defined(HAVE_ECC384) ||\
  19865. defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES))
  19866. #define HAVE_ECC_VECTOR_TEST
  19867. #endif
  19868. #endif
  19869. #ifdef HAVE_ECC_VECTOR_TEST
  19870. typedef struct eccVector {
  19871. const char* msg; /* SHA-1 Encoded Message */
  19872. const char* Qx;
  19873. const char* Qy;
  19874. const char* d; /* Private Key */
  19875. const char* R;
  19876. const char* S;
  19877. const char* curveName;
  19878. word32 msgLen;
  19879. word32 keySize;
  19880. #ifndef NO_ASN
  19881. const byte* r;
  19882. word32 rSz;
  19883. const byte* s;
  19884. word32 sSz;
  19885. #endif
  19886. } eccVector;
  19887. #if !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  19888. static int ecc_test_vector_item(const eccVector* vector)
  19889. {
  19890. int ret = 0, verify = 0;
  19891. word32 sigSz;
  19892. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  19893. ecc_key *userA = (ecc_key *)XMALLOC(sizeof *userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19894. #else
  19895. ecc_key userA[1];
  19896. #endif
  19897. WC_DECLARE_VAR(sig, byte, ECC_SIG_SIZE, HEAP_HINT);
  19898. #if !defined(NO_ASN) && !defined(HAVE_SELFTEST)
  19899. word32 sigRawSz, rSz = MAX_ECC_BYTES, sSz = MAX_ECC_BYTES;
  19900. WC_DECLARE_VAR(sigRaw, byte, ECC_SIG_SIZE, HEAP_HINT);
  19901. WC_DECLARE_VAR(r, byte, MAX_ECC_BYTES, HEAP_HINT);
  19902. WC_DECLARE_VAR(s, byte, MAX_ECC_BYTES, HEAP_HINT);
  19903. #endif
  19904. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  19905. if (sig == NULL)
  19906. ERROR_OUT(MEMORY_E, done);
  19907. #if !defined(NO_ASN) && !defined(HAVE_SELFTEST)
  19908. if (sigRaw == NULL || r == NULL || s == NULL)
  19909. ERROR_OUT(MEMORY_E, done);
  19910. #endif
  19911. #endif
  19912. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  19913. if (userA == NULL)
  19914. ERROR_OUT(MEMORY_E, done);
  19915. #endif
  19916. ret = wc_ecc_init_ex(userA, HEAP_HINT, devId);
  19917. if (ret != 0)
  19918. goto done;
  19919. ret = wc_ecc_import_raw(userA, vector->Qx, vector->Qy,
  19920. vector->d, vector->curveName);
  19921. if (ret != 0)
  19922. goto done;
  19923. XMEMSET(sig, 0, ECC_SIG_SIZE);
  19924. sigSz = ECC_SIG_SIZE;
  19925. ret = wc_ecc_rs_to_sig(vector->R, vector->S, sig, &sigSz);
  19926. if (ret != 0)
  19927. goto done;
  19928. #if !defined(NO_ASN) && !defined(HAVE_SELFTEST)
  19929. XMEMSET(sigRaw, 0, ECC_SIG_SIZE);
  19930. sigRawSz = ECC_SIG_SIZE;
  19931. ret = wc_ecc_rs_raw_to_sig(vector->r, vector->rSz, vector->s, vector->sSz,
  19932. sigRaw, &sigRawSz);
  19933. if (ret != 0)
  19934. goto done;
  19935. if (sigSz != sigRawSz || XMEMCMP(sig, sigRaw, sigSz) != 0) {
  19936. ret = -9810;
  19937. goto done;
  19938. }
  19939. ret = wc_ecc_sig_to_rs(sig, sigSz, r, &rSz, s, &sSz);
  19940. if (ret != 0)
  19941. goto done;
  19942. if (rSz != vector->rSz || XMEMCMP(r, vector->r, rSz) != 0 ||
  19943. sSz != vector->sSz || XMEMCMP(s, vector->s, sSz) != 0) {
  19944. ret = -9811;
  19945. goto done;
  19946. }
  19947. #endif
  19948. do {
  19949. #if defined(WOLFSSL_ASYNC_CRYPT)
  19950. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  19951. #endif
  19952. if (ret == 0)
  19953. ret = wc_ecc_verify_hash(sig, sigSz, (byte*)vector->msg,
  19954. vector->msgLen, &verify, userA);
  19955. } while (ret == WC_PENDING_E);
  19956. if (ret != 0)
  19957. goto done;
  19958. TEST_SLEEP();
  19959. if (verify != 1)
  19960. ret = -9812;
  19961. done:
  19962. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  19963. if (userA != NULL) {
  19964. wc_ecc_free(userA);
  19965. XFREE(userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19966. }
  19967. #else
  19968. wc_ecc_free(userA);
  19969. #endif
  19970. #if !defined(NO_ASN) && !defined(HAVE_SELFTEST)
  19971. WC_FREE_VAR(sigRaw, HEAP_HINT);
  19972. WC_FREE_VAR(r, HEAP_HINT);
  19973. WC_FREE_VAR(s, HEAP_HINT);
  19974. #endif
  19975. WC_FREE_VAR(sig, HEAP_HINT);
  19976. return ret;
  19977. }
  19978. static int ecc_test_vector(int keySize)
  19979. {
  19980. int ret;
  19981. eccVector vec;
  19982. XMEMSET(&vec, 0, sizeof(vec));
  19983. vec.keySize = (word32)keySize;
  19984. switch(keySize) {
  19985. #if defined(HAVE_ECC112) || defined(HAVE_ALL_CURVES)
  19986. case 14:
  19987. return 0;
  19988. #endif /* HAVE_ECC112 */
  19989. #if defined(HAVE_ECC128) || defined(HAVE_ALL_CURVES)
  19990. case 16:
  19991. return 0;
  19992. #endif /* HAVE_ECC128 */
  19993. #if defined(HAVE_ECC160) || defined(HAVE_ALL_CURVES)
  19994. case 20:
  19995. return 0;
  19996. #endif /* HAVE_ECC160 */
  19997. #if defined(HAVE_ECC192) || defined(HAVE_ALL_CURVES)
  19998. case 24:
  19999. /* first [P-192,SHA-1] vector from FIPS 186-3 NIST vectors */
  20000. #if 1
  20001. vec.msg = "\x60\x80\x79\x42\x3f\x12\x42\x1d\xe6\x16\xb7\x49\x3e\xbe\x55\x1c\xf4\xd6\x5b\x92";
  20002. vec.msgLen = 20;
  20003. #else
  20004. /* This is the raw message prior to SHA-1 */
  20005. vec.msg =
  20006. "\xeb\xf7\x48\xd7\x48\xeb\xbc\xa7\xd2\x9f\xb4\x73\x69\x8a\x6e\x6b"
  20007. "\x4f\xb1\x0c\x86\x5d\x4a\xf0\x24\xcc\x39\xae\x3d\xf3\x46\x4b\xa4"
  20008. "\xf1\xd6\xd4\x0f\x32\xbf\x96\x18\xa9\x1b\xb5\x98\x6f\xa1\xa2\xaf"
  20009. "\x04\x8a\x0e\x14\xdc\x51\xe5\x26\x7e\xb0\x5e\x12\x7d\x68\x9d\x0a"
  20010. "\xc6\xf1\xa7\xf1\x56\xce\x06\x63\x16\xb9\x71\xcc\x7a\x11\xd0\xfd"
  20011. "\x7a\x20\x93\xe2\x7c\xf2\xd0\x87\x27\xa4\xe6\x74\x8c\xc3\x2f\xd5"
  20012. "\x9c\x78\x10\xc5\xb9\x01\x9d\xf2\x1c\xdc\xc0\xbc\xa4\x32\xc0\xa3"
  20013. "\xee\xd0\x78\x53\x87\x50\x88\x77\x11\x43\x59\xce\xe4\xa0\x71\xcf";
  20014. vec.msgLen = 128;
  20015. #endif
  20016. vec.Qx = "07008ea40b08dbe76432096e80a2494c94982d2d5bcf98e6";
  20017. vec.Qy = "76fab681d00b414ea636ba215de26d98c41bd7f2e4d65477";
  20018. vec.d = "e14f37b3d1374ff8b03f41b9b3fdd2f0ebccf275d660d7f3";
  20019. vec.R = "6994d962bdd0d793ffddf855ec5bf2f91a9698b46258a63e";
  20020. vec.S = "02ba6465a234903744ab02bc8521405b73cf5fc00e1a9f41";
  20021. vec.curveName = "SECP192R1";
  20022. #ifndef NO_ASN
  20023. vec.r = (byte*)"\x69\x94\xd9\x62\xbd\xd0\xd7\x93\xff\xdd\xf8\x55"
  20024. "\xec\x5b\xf2\xf9\x1a\x96\x98\xb4\x62\x58\xa6\x3e";
  20025. vec.rSz = 24;
  20026. vec.s = (byte*)"\x02\xba\x64\x65\xa2\x34\x90\x37\x44\xab\x02\xbc"
  20027. "\x85\x21\x40\x5b\x73\xcf\x5f\xc0\x0e\x1a\x9f\x41";
  20028. vec.sSz = 24;
  20029. #endif
  20030. break;
  20031. #endif /* HAVE_ECC192 */
  20032. #if defined(HAVE_ECC224) || defined(HAVE_ALL_CURVES)
  20033. case 28:
  20034. /* first [P-224,SHA-1] vector from FIPS 186-3 NIST vectors */
  20035. #if 1
  20036. vec.msg = "\xb9\xa3\xb8\x6d\xb0\xba\x99\xfd\xc6\xd2\x94\x6b\xfe\xbe\x9c\xe8\x3f\x10\x74\xfc";
  20037. vec.msgLen = 20;
  20038. #else
  20039. /* This is the raw message prior to SHA-1 */
  20040. vec.msg =
  20041. "\x36\xc8\xb2\x29\x86\x48\x7f\x67\x7c\x18\xd0\x97\x2a\x9e\x20\x47"
  20042. "\xb3\xaf\xa5\x9e\xc1\x62\x76\x4e\xc3\x0b\x5b\x69\xe0\x63\x0f\x99"
  20043. "\x0d\x4e\x05\xc2\x73\xb0\xe5\xa9\xd4\x28\x27\xb6\x95\xfc\x2d\x64"
  20044. "\xd9\x13\x8b\x1c\xf4\xc1\x21\x55\x89\x4c\x42\x13\x21\xa7\xbb\x97"
  20045. "\x0b\xdc\xe0\xfb\xf0\xd2\xae\x85\x61\xaa\xd8\x71\x7f\x2e\x46\xdf"
  20046. "\xe3\xff\x8d\xea\xb4\xd7\x93\x23\x56\x03\x2c\x15\x13\x0d\x59\x9e"
  20047. "\x26\xc1\x0f\x2f\xec\x96\x30\x31\xac\x69\x38\xa1\x8d\x66\x45\x38"
  20048. "\xb9\x4d\xac\x55\x34\xef\x7b\x59\x94\x24\xd6\x9b\xe1\xf7\x1c\x20";
  20049. vec.msgLen = 128;
  20050. #endif
  20051. vec.Qx = "8a4dca35136c4b70e588e23554637ae251077d1365a6ba5db9585de7";
  20052. vec.Qy = "ad3dee06de0be8279d4af435d7245f14f3b4f82eb578e519ee0057b1";
  20053. vec.d = "97c4b796e1639dd1035b708fc00dc7ba1682cec44a1002a1a820619f";
  20054. vec.R = "147b33758321e722a0360a4719738af848449e2c1d08defebc1671a7";
  20055. vec.S = "24fc7ed7f1352ca3872aa0916191289e2e04d454935d50fe6af3ad5b";
  20056. vec.curveName = "SECP224R1";
  20057. #ifndef NO_ASN
  20058. vec.r = (byte*)"\x14\x7b\x33\x75\x83\x21\xe7\x22\xa0\x36\x0a\x47"
  20059. "\x19\x73\x8a\xf8\x48\x44\x9e\x2c\x1d\x08\xde\xfe"
  20060. "\xbc\x16\x71\xa7";
  20061. vec.rSz = 28;
  20062. vec.s = (byte*)"\x24\xfc\x7e\xd7\xf1\x35\x2c\xa3\x87\x2a\xa0\x91"
  20063. "\x61\x91\x28\x9e\x2e\x04\xd4\x54\x93\x5d\x50\xfe"
  20064. "\x6a\xf3\xad\x5b";
  20065. vec.sSz = 28;
  20066. #endif
  20067. break;
  20068. #endif /* HAVE_ECC224 */
  20069. #if defined(HAVE_ECC239) || defined(HAVE_ALL_CURVES)
  20070. case 30:
  20071. return 0;
  20072. #endif /* HAVE_ECC239 */
  20073. #if !defined(NO_ECC256) || defined(HAVE_ALL_CURVES)
  20074. case 32:
  20075. /* first [P-256,SHA-1] vector from FIPS 186-3 NIST vectors */
  20076. #if 1
  20077. vec.msg = "\xa3\xf9\x1a\xe2\x1b\xa6\xb3\x03\x98\x64\x47\x2f\x18\x41\x44\xc6\xaf\x62\xcd\x0e";
  20078. vec.msgLen = 20;
  20079. #else
  20080. /* This is the raw message prior to SHA-1 */
  20081. vec.msg =
  20082. "\xa2\x4b\x21\x76\x2e\x6e\xdb\x15\x3c\xc1\x14\x38\xdb\x0e\x92\xcd"
  20083. "\xf5\x2b\x86\xb0\x6c\xa9\x70\x16\x06\x27\x59\xc7\x0d\x36\xd1\x56"
  20084. "\x2c\xc9\x63\x0d\x7f\xc7\xc7\x74\xb2\x8b\x54\xe3\x1e\xf5\x58\x72"
  20085. "\xb2\xa6\x5d\xf1\xd7\xec\x26\xde\xbb\x33\xe7\xd9\x27\xef\xcc\xf4"
  20086. "\x6b\x63\xde\x52\xa4\xf4\x31\xea\xca\x59\xb0\x5d\x2e\xde\xc4\x84"
  20087. "\x5f\xff\xc0\xee\x15\x03\x94\xd6\x1f\x3d\xfe\xcb\xcd\xbf\x6f\x5a"
  20088. "\x73\x38\xd0\xbe\x3f\x2a\x77\x34\x51\x98\x3e\xba\xeb\x48\xf6\x73"
  20089. "\x8f\xc8\x95\xdf\x35\x7e\x1a\x48\xa6\x53\xbb\x35\x5a\x31\xa1\xb4"
  20090. vec.msgLen = 128;
  20091. #endif
  20092. vec.Qx = "fa2737fb93488d19caef11ae7faf6b7f4bcd67b286e3fc54e8a65c2b74aeccb0";
  20093. vec.Qy = "d4ccd6dae698208aa8c3a6f39e45510d03be09b2f124bfc067856c324f9b4d09";
  20094. vec.d = "be34baa8d040a3b991f9075b56ba292f755b90e4b6dc10dad36715c33cfdac25";
  20095. vec.R = "2b826f5d44e2d0b6de531ad96b51e8f0c56fdfead3c236892e4d84eacfc3b75c";
  20096. vec.S = "a2248b62c03db35a7cd63e8a120a3521a89d3d2f61ff99035a2148ae32e3a248";
  20097. #ifndef NO_ASN
  20098. vec.r = (byte*)"\x2b\x82\x6f\x5d\x44\xe2\xd0\xb6\xde\x53\x1a\xd9"
  20099. "\x6b\x51\xe8\xf0\xc5\x6f\xdf\xea\xd3\xc2\x36\x89"
  20100. "\x2e\x4d\x84\xea\xcf\xc3\xb7\x5c";
  20101. vec.rSz = 32;
  20102. vec.s = (byte*)"\xa2\x24\x8b\x62\xc0\x3d\xb3\x5a\x7c\xd6\x3e\x8a"
  20103. "\x12\x0a\x35\x21\xa8\x9d\x3d\x2f\x61\xff\x99\x03"
  20104. "\x5a\x21\x48\xae\x32\xe3\xa2\x48";
  20105. vec.sSz = 32;
  20106. #endif
  20107. vec.curveName = "SECP256R1";
  20108. break;
  20109. #endif /* !NO_ECC256 */
  20110. #if defined(HAVE_ECC320) || defined(HAVE_ALL_CURVES)
  20111. case 40:
  20112. return 0;
  20113. #endif /* HAVE_ECC320 */
  20114. #if defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)
  20115. case 48:
  20116. /* first [P-384,SHA-1] vector from FIPS 186-3 NIST vectors */
  20117. #if 1
  20118. vec.msg = "\x9b\x9f\x8c\x95\x35\xa5\xca\x26\x60\x5d\xb7\xf2\xfa\x57\x3b\xdf\xc3\x2e\xab\x8b";
  20119. vec.msgLen = 20;
  20120. #else
  20121. /* This is the raw message prior to SHA-1 */
  20122. vec.msg =
  20123. "\xab\xe1\x0a\xce\x13\xe7\xe1\xd9\x18\x6c\x48\xf7\x88\x9d\x51\x47"
  20124. "\x3d\x3a\x09\x61\x98\x4b\xc8\x72\xdf\x70\x8e\xcc\x3e\xd3\xb8\x16"
  20125. "\x9d\x01\xe3\xd9\x6f\xc4\xf1\xd5\xea\x00\xa0\x36\x92\xbc\xc5\xcf"
  20126. "\xfd\x53\x78\x7c\x88\xb9\x34\xaf\x40\x4c\x03\x9d\x32\x89\xb5\xba"
  20127. "\xc5\xae\x7d\xb1\x49\x68\x75\xb5\xdc\x73\xc3\x09\xf9\x25\xc1\x3d"
  20128. "\x1c\x01\xab\xda\xaf\xeb\xcd\xac\x2c\xee\x43\x39\x39\xce\x8d\x4a"
  20129. "\x0a\x5d\x57\xbb\x70\x5f\x3b\xf6\xec\x08\x47\x95\x11\xd4\xb4\xa3"
  20130. "\x21\x1f\x61\x64\x9a\xd6\x27\x43\x14\xbf\x0d\x43\x8a\x81\xe0\x60"
  20131. vec.msgLen = 128;
  20132. #endif
  20133. vec.Qx = "e55fee6c49d8d523f5ce7bf9c0425ce4ff650708b7de5cfb095901523979a7f042602db30854735369813b5c3f5ef868";
  20134. vec.Qy = "28f59cc5dc509892a988d38a8e2519de3d0c4fd0fbdb0993e38f18506c17606c5e24249246f1ce94983a5361c5be983e";
  20135. vec.d = "a492ce8fa90084c227e1a32f7974d39e9ff67a7e8705ec3419b35fb607582bebd461e0b1520ac76ec2dd4e9b63ebae71";
  20136. vec.R = "6820b8585204648aed63bdff47f6d9acebdea62944774a7d14f0e14aa0b9a5b99545b2daee6b3c74ebf606667a3f39b7";
  20137. vec.S = "491af1d0cccd56ddd520b233775d0bc6b40a6255cc55207d8e9356741f23c96c14714221078dbd5c17f4fdd89b32a907";
  20138. vec.curveName = "SECP384R1";
  20139. #ifndef NO_ASN
  20140. vec.r = (byte*)"\x68\x20\xb8\x58\x52\x04\x64\x8a\xed\x63\xbd\xff"
  20141. "\x47\xf6\xd9\xac\xeb\xde\xa6\x29\x44\x77\x4a\x7d"
  20142. "\x14\xf0\xe1\x4a\xa0\xb9\xa5\xb9\x95\x45\xb2\xda"
  20143. "\xee\x6b\x3c\x74\xeb\xf6\x06\x66\x7a\x3f\x39\xb7";
  20144. vec.rSz = 48;
  20145. vec.s = (byte*)"\x49\x1a\xf1\xd0\xcc\xcd\x56\xdd\xd5\x20\xb2\x33"
  20146. "\x77\x5d\x0b\xc6\xb4\x0a\x62\x55\xcc\x55\x20\x7d"
  20147. "\x8e\x93\x56\x74\x1f\x23\xc9\x6c\x14\x71\x42\x21"
  20148. "\x07\x8d\xbd\x5c\x17\xf4\xfd\xd8\x9b\x32\xa9\x07";
  20149. vec.sSz = 48;
  20150. #endif
  20151. break;
  20152. #endif /* HAVE_ECC384 */
  20153. #if defined(HAVE_ECC512) || defined(HAVE_ALL_CURVES)
  20154. case 64:
  20155. return 0;
  20156. #endif /* HAVE_ECC512 */
  20157. #if defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)
  20158. case 66:
  20159. /* first [P-521,SHA-1] vector from FIPS 186-3 NIST vectors */
  20160. #if 1
  20161. vec.msg = "\x1b\xf7\x03\x9c\xca\x23\x94\x27\x3f\x11\xa1\xd4\x8d\xcc\xb4\x46\x6f\x31\x61\xdf";
  20162. vec.msgLen = 20;
  20163. #else
  20164. /* This is the raw message prior to SHA-1 */
  20165. vec.msg =
  20166. "\x50\x3f\x79\x39\x34\x0a\xc7\x23\xcd\x4a\x2f\x4e\x6c\xcc\x27\x33"
  20167. "\x38\x3a\xca\x2f\xba\x90\x02\x19\x9d\x9e\x1f\x94\x8b\xe0\x41\x21"
  20168. "\x07\xa3\xfd\xd5\x14\xd9\x0c\xd4\xf3\x7c\xc3\xac\x62\xef\x00\x3a"
  20169. "\x2d\xb1\xd9\x65\x7a\xb7\x7f\xe7\x55\xbf\x71\xfa\x59\xe4\xd9\x6e"
  20170. "\xa7\x2a\xe7\xbf\x9d\xe8\x7d\x79\x34\x3b\xc1\xa4\xbb\x14\x4d\x16"
  20171. "\x28\xd1\xe9\xe9\xc8\xed\x80\x8b\x96\x2c\x54\xe5\xf9\x6d\x53\xda"
  20172. "\x14\x7a\x96\x38\xf9\x4a\x91\x75\xd8\xed\x61\x05\x5f\x0b\xa5\x73"
  20173. "\xa8\x2b\xb7\xe0\x18\xee\xda\xc4\xea\x7b\x36\x2e\xc8\x9c\x38\x2b"
  20174. vec.msgLen = 128;
  20175. #endif
  20176. vec.Qx = "12fbcaeffa6a51f3ee4d3d2b51c5dec6d7c726ca353fc014ea2bf7cfbb9b910d32cbfa6a00fe39b6cdb8946f22775398b2e233c0cf144d78c8a7742b5c7a3bb5d23";
  20177. vec.Qy = "09cdef823dd7bf9a79e8cceacd2e4527c231d0ae5967af0958e931d7ddccf2805a3e618dc3039fec9febbd33052fe4c0fee98f033106064982d88f4e03549d4a64d";
  20178. vec.d = "1bd56bd106118eda246155bd43b42b8e13f0a6e25dd3bb376026fab4dc92b6157bc6dfec2d15dd3d0cf2a39aa68494042af48ba9601118da82c6f2108a3a203ad74";
  20179. vec.R = "0bd117b4807710898f9dd7778056485777668f0e78e6ddf5b000356121eb7a220e9493c7f9a57c077947f89ac45d5acb6661bbcd17abb3faea149ba0aa3bb1521be";
  20180. vec.S = "019cd2c5c3f9870ecdeb9b323abdf3a98cd5e231d85c6ddc5b71ab190739f7f226e6b134ba1d5889ddeb2751dabd97911dff90c34684cdbe7bb669b6c3d22f2480c";
  20181. vec.curveName = "SECP521R1";
  20182. #ifndef NO_ASN
  20183. vec.r = (byte*)"\xbd\x11\x7b\x48\x07\x71\x08\x98\xf9\xdd\x77\x78"
  20184. "\x05\x64\x85\x77\x76\x68\xf0\xe7\x8e\x6d\xdf\x5b"
  20185. "\x00\x03\x56\x12\x1e\xb7\xa2\x20\xe9\x49\x3c\x7f"
  20186. "\x9a\x57\xc0\x77\x94\x7f\x89\xac\x45\xd5\xac\xb6"
  20187. "\x66\x1b\xbc\xd1\x7a\xbb\x3f\xae\xa1\x49\xba\x0a"
  20188. "\xa3\xbb\x15\x21\xbe";
  20189. vec.rSz = 65;
  20190. vec.s = (byte*)"\x19\xcd\x2c\x5c\x3f\x98\x70\xec\xde\xb9\xb3\x23"
  20191. "\xab\xdf\x3a\x98\xcd\x5e\x23\x1d\x85\xc6\xdd\xc5"
  20192. "\xb7\x1a\xb1\x90\x73\x9f\x7f\x22\x6e\x6b\x13\x4b"
  20193. "\xa1\xd5\x88\x9d\xde\xb2\x75\x1d\xab\xd9\x79\x11"
  20194. "\xdf\xf9\x0c\x34\x68\x4c\xdb\xe7\xbb\x66\x9b\x6c"
  20195. "\x3d\x22\xf2\x48\x0c";
  20196. vec.sSz = 65;
  20197. #endif
  20198. break;
  20199. #endif /* HAVE_ECC521 */
  20200. default:
  20201. return NOT_COMPILED_IN; /* Invalid key size / Not supported */
  20202. }; /* Switch */
  20203. ret = ecc_test_vector_item(&vec);
  20204. if (ret < 0) {
  20205. return ret;
  20206. }
  20207. return 0;
  20208. }
  20209. #endif /* WOLF_CRYPTO_CB_ONLY_ECC */
  20210. #if defined(HAVE_ECC_SIGN) && (defined(WOLFSSL_ECDSA_DETERMINISTIC_K) || \
  20211. defined(WOLFSSL_ECDSA_DETERMINISTIC_K_VARIANT)) \
  20212. && (!defined(FIPS_VERSION_GE) || FIPS_VERSION_GE(5,3))
  20213. #if defined(HAVE_ECC256)
  20214. static int ecc_test_deterministic_k(WC_RNG* rng)
  20215. {
  20216. int ret;
  20217. #ifdef WOLFSSL_SMALL_STACK
  20218. ecc_key *key = NULL;
  20219. #else
  20220. ecc_key key[1];
  20221. #endif
  20222. int key_inited = 0;
  20223. byte sig[72];
  20224. word32 sigSz;
  20225. WOLFSSL_SMALL_STACK_STATIC const unsigned char msg[] = "sample";
  20226. unsigned char hash[32];
  20227. WOLFSSL_SMALL_STACK_STATIC const char* dIUT =
  20228. "C9AFA9D845BA75166B5C215767B1D6934E50C3DB36E89B127B8A622B120F6721";
  20229. WOLFSSL_SMALL_STACK_STATIC const char* QIUTx =
  20230. "60FED4BA255A9D31C961EB74C6356D68C049B8923B61FA6CE669622E60F29FB6";
  20231. WOLFSSL_SMALL_STACK_STATIC const char* QIUTy =
  20232. "7903FE1008B8BC99A41AE9E95628BC64F2F1B20C2D7E9F5177A3C294D4462299";
  20233. WOLFSSL_SMALL_STACK_STATIC const byte expSig[] = {
  20234. 0x30, 0x46, 0x02, 0x21, 0x00, 0xEF, 0xD4, 0x8B,
  20235. 0x2A, 0xAC, 0xB6, 0xA8, 0xFD, 0x11, 0x40, 0xDD,
  20236. 0x9C, 0xD4, 0x5E, 0x81, 0xD6, 0x9D, 0x2C, 0x87,
  20237. 0x7B, 0x56, 0xAA, 0xF9, 0x91, 0xC3, 0x4D, 0x0E,
  20238. 0xA8, 0x4E, 0xAF, 0x37, 0x16, 0x02, 0x21, 0x00,
  20239. 0xF7, 0xCB, 0x1C, 0x94, 0x2D, 0x65, 0x7C, 0x41,
  20240. 0xD4, 0x36, 0xC7, 0xA1, 0xB6, 0xE2, 0x9F, 0x65,
  20241. 0xF3, 0xE9, 0x00, 0xDB, 0xB9, 0xAF, 0xF4, 0x06,
  20242. 0x4D, 0xC4, 0xAB, 0x2F, 0x84, 0x3A, 0xCD, 0xA8
  20243. };
  20244. #ifdef WOLFSSL_SMALL_STACK
  20245. key = (ecc_key *)XMALLOC(sizeof(*key), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20246. if (key == NULL)
  20247. return MEMORY_E;
  20248. #endif
  20249. ret = wc_ecc_init_ex(key, HEAP_HINT, devId);
  20250. if (ret != 0) {
  20251. goto done;
  20252. }
  20253. key_inited = 1;
  20254. ret = wc_ecc_import_raw(key, QIUTx, QIUTy, dIUT, "SECP256R1");
  20255. if (ret != 0) {
  20256. goto done;
  20257. }
  20258. ret = wc_Hash(WC_HASH_TYPE_SHA256, msg,
  20259. (word32)XSTRLEN((const char*)msg), hash, sizeof(hash));
  20260. if (ret != 0) {
  20261. goto done;
  20262. }
  20263. ret = wc_ecc_set_deterministic(key, 1);
  20264. if (ret != 0) {
  20265. goto done;
  20266. }
  20267. sigSz = sizeof(sig);
  20268. do {
  20269. #if defined(WOLFSSL_ASYNC_CRYPT)
  20270. ret = wc_AsyncWait(ret, key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  20271. #endif
  20272. if (ret == 0)
  20273. ret = wc_ecc_sign_hash(hash, sizeof(hash), sig, &sigSz, rng, key);
  20274. } while (ret == WC_PENDING_E);
  20275. if (ret != 0) {
  20276. goto done;
  20277. }
  20278. TEST_SLEEP();
  20279. if (sigSz != sizeof(expSig)) {
  20280. ret = -9830;
  20281. goto done;
  20282. }
  20283. if (XMEMCMP(sig, expSig, sigSz) != 0) {
  20284. ret = -9831;
  20285. goto done;
  20286. }
  20287. sigSz = sizeof(sig);
  20288. do {
  20289. #if defined(WOLFSSL_ASYNC_CRYPT)
  20290. ret = wc_AsyncWait(ret, key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  20291. #endif
  20292. if (ret == 0)
  20293. ret = wc_ecc_sign_hash(hash, sizeof(hash), sig, &sigSz, rng, key);
  20294. } while (ret == WC_PENDING_E);
  20295. if (ret != 0) {
  20296. goto done;
  20297. }
  20298. TEST_SLEEP();
  20299. done:
  20300. if (key_inited)
  20301. wc_ecc_free(key);
  20302. #ifdef WOLFSSL_SMALL_STACK
  20303. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20304. #endif
  20305. return ret;
  20306. }
  20307. #endif
  20308. #ifdef WOLFSSL_PUBLIC_MP
  20309. #if defined(HAVE_ECC384)
  20310. /* KAT from RFC6979 */
  20311. static int ecc384_test_deterministic_k(WC_RNG* rng)
  20312. {
  20313. int ret;
  20314. #ifdef WOLFSSL_SMALL_STACK
  20315. ecc_key *key;
  20316. mp_int *r, *s, *expR, *expS;
  20317. #else
  20318. ecc_key key[1];
  20319. mp_int r[1], s[1], expR[1], expS[1];
  20320. #endif
  20321. int key_inited = 0;
  20322. WOLFSSL_SMALL_STACK_STATIC const unsigned char msg[] = "sample";
  20323. unsigned char hash[32];
  20324. WOLFSSL_SMALL_STACK_STATIC const char* dIUT =
  20325. "6B9D3DAD2E1B8C1C05B19875B6659F4DE23C3B667BF297BA9AA47740787137D8"
  20326. "96D5724E4C70A825F872C9EA60D2EDF5";
  20327. WOLFSSL_SMALL_STACK_STATIC const char* QIUTx =
  20328. "EC3A4E415B4E19A4568618029F427FA5DA9A8BC4AE92E02E06AAE5286B300C64"
  20329. "DEF8F0EA9055866064A254515480BC13";
  20330. WOLFSSL_SMALL_STACK_STATIC const char* QIUTy =
  20331. "8015D9B72D7D57244EA8EF9AC0C621896708A59367F9DFB9F54CA84B3F1C9DB1"
  20332. "288B231C3AE0D4FE7344FD2533264720";
  20333. WOLFSSL_SMALL_STACK_STATIC const char* expRstr =
  20334. "21B13D1E013C7FA1392D03C5F99AF8B30C570C6F98D4EA8E354B63A21D3DAA33"
  20335. "BDE1E888E63355D92FA2B3C36D8FB2CD";
  20336. WOLFSSL_SMALL_STACK_STATIC const char* expSstr =
  20337. "F3AA443FB107745BF4BD77CB3891674632068A10CA67E3D45DB2266FA7D1FEEB"
  20338. "EFDC63ECCD1AC42EC0CB8668A4FA0AB0";
  20339. #ifdef WOLFSSL_SMALL_STACK
  20340. key = (ecc_key *)XMALLOC(sizeof(*key), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20341. r = (mp_int *)XMALLOC(sizeof(*r), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20342. s = (mp_int *)XMALLOC(sizeof(*s), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20343. expR = (mp_int *)XMALLOC(sizeof(*expR), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20344. expS = (mp_int *)XMALLOC(sizeof(*expS), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20345. if ((key == NULL) ||
  20346. (r == NULL) ||
  20347. (s == NULL) ||
  20348. (expR == NULL) ||
  20349. (expS == NULL))
  20350. {
  20351. ret = MEMORY_E;
  20352. goto done;
  20353. }
  20354. #endif
  20355. ret = mp_init_multi(r, s, expR, expS, NULL, NULL);
  20356. if (ret != MP_OKAY) {
  20357. goto done;
  20358. }
  20359. ret = wc_ecc_init_ex(key, HEAP_HINT, devId);
  20360. if (ret != 0) {
  20361. goto done;
  20362. }
  20363. key_inited = 1;
  20364. ret = wc_ecc_import_raw(key, QIUTx, QIUTy, dIUT, "SECP384R1");
  20365. if (ret != 0) {
  20366. goto done;
  20367. }
  20368. ret = wc_Hash(WC_HASH_TYPE_SHA256, msg,
  20369. (word32)XSTRLEN((const char*)msg), hash, sizeof(hash));
  20370. if (ret != 0) {
  20371. goto done;
  20372. }
  20373. ret = wc_ecc_set_deterministic(key, 1);
  20374. if (ret != 0) {
  20375. goto done;
  20376. }
  20377. do {
  20378. #if defined(WOLFSSL_ASYNC_CRYPT)
  20379. ret = wc_AsyncWait(ret, key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  20380. #endif
  20381. if (ret == 0)
  20382. ret = wc_ecc_sign_hash_ex(hash, sizeof(hash), rng, key, r, s);
  20383. } while (ret == WC_PENDING_E);
  20384. if (ret != 0) {
  20385. goto done;
  20386. }
  20387. TEST_SLEEP();
  20388. mp_read_radix(expR, expRstr, MP_RADIX_HEX);
  20389. mp_read_radix(expS, expSstr, MP_RADIX_HEX);
  20390. if (mp_cmp(r, expR) != MP_EQ) {
  20391. ret = -1;
  20392. }
  20393. done:
  20394. if (key_inited)
  20395. wc_ecc_free(key);
  20396. #ifdef WOLFSSL_SMALL_STACK
  20397. if (key != NULL)
  20398. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20399. if (r != NULL)
  20400. XFREE(r, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20401. if (s != NULL)
  20402. XFREE(s, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20403. if (expR != NULL)
  20404. XFREE(expR, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20405. if (expS != NULL)
  20406. XFREE(expS, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20407. #endif
  20408. return ret;
  20409. }
  20410. #endif /* HAVE_ECC384 */
  20411. #if defined(HAVE_ECC521)
  20412. /* KAT from RFC6979 */
  20413. static int ecc521_test_deterministic_k(WC_RNG* rng)
  20414. {
  20415. int ret;
  20416. #ifdef WOLFSSL_SMALL_STACK
  20417. ecc_key *key;
  20418. mp_int *r, *s, *expR, *expS;
  20419. #else
  20420. ecc_key key[1];
  20421. mp_int r[1], s[1], expR[1], expS[1];
  20422. #endif
  20423. int key_inited = 0;
  20424. WOLFSSL_SMALL_STACK_STATIC const unsigned char msg[] = "sample";
  20425. unsigned char hash[32];
  20426. WOLFSSL_SMALL_STACK_STATIC const char* dIUT =
  20427. "0FAD06DAA62BA3B25D2FB40133DA757205DE67F5BB0018FEE8C86E1B68C7E75C"
  20428. "AA896EB32F1F47C70855836A6D16FCC1466F6D8FBEC67DB89EC0C08B0E996B83"
  20429. "538";
  20430. WOLFSSL_SMALL_STACK_STATIC const char* QIUTx =
  20431. "1894550D0785932E00EAA23B694F213F8C3121F86DC97A04E5A7167DB4E5BCD3"
  20432. "71123D46E45DB6B5D5370A7F20FB633155D38FFA16D2BD761DCAC474B9A2F502"
  20433. "3A4";
  20434. WOLFSSL_SMALL_STACK_STATIC const char* QIUTy =
  20435. "0493101C962CD4D2FDDF782285E64584139C2F91B47F87FF82354D6630F746A2"
  20436. "8A0DB25741B5B34A828008B22ACC23F924FAAFBD4D33F81EA66956DFEAA2BFDF"
  20437. "CF5";
  20438. WOLFSSL_SMALL_STACK_STATIC const char* expRstr =
  20439. "1511BB4D675114FE266FC4372B87682BAECC01D3CC62CF2303C92B3526012659"
  20440. "D16876E25C7C1E57648F23B73564D67F61C6F14D527D54972810421E7D87589E"
  20441. "1A7";
  20442. WOLFSSL_SMALL_STACK_STATIC const char* expSstr =
  20443. "04A171143A83163D6DF460AAF61522695F207A58B95C0644D87E52AA1A347916"
  20444. "E4F7A72930B1BC06DBE22CE3F58264AFD23704CBB63B29B931F7DE6C9D949A7E"
  20445. "CFC";
  20446. #ifdef WOLFSSL_SMALL_STACK
  20447. key = (ecc_key *)XMALLOC(sizeof(*key), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20448. r = (mp_int *)XMALLOC(sizeof(*r), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20449. s = (mp_int *)XMALLOC(sizeof(*s), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20450. expR = (mp_int *)XMALLOC(sizeof(*expR), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20451. expS = (mp_int *)XMALLOC(sizeof(*expS), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20452. if ((key == NULL) ||
  20453. (r == NULL) ||
  20454. (s == NULL) ||
  20455. (expR == NULL) ||
  20456. (expS == NULL))
  20457. {
  20458. ret = MEMORY_E;
  20459. goto done;
  20460. }
  20461. #endif
  20462. ret = mp_init_multi(r, s, expR, expS, NULL, NULL);
  20463. if (ret != MP_OKAY) {
  20464. goto done;
  20465. }
  20466. ret = wc_ecc_init_ex(key, HEAP_HINT, devId);
  20467. if (ret != 0) {
  20468. return ret;
  20469. }
  20470. key_inited = 1;
  20471. ret = wc_ecc_import_raw(key, QIUTx, QIUTy, dIUT, "SECP521R1");
  20472. if (ret != 0) {
  20473. goto done;
  20474. }
  20475. ret = wc_Hash(WC_HASH_TYPE_SHA256, msg,
  20476. (word32)XSTRLEN((const char*)msg), hash, sizeof(hash));
  20477. if (ret != 0) {
  20478. goto done;
  20479. }
  20480. ret = wc_ecc_set_deterministic(key, 1);
  20481. if (ret != 0) {
  20482. goto done;
  20483. }
  20484. do {
  20485. #if defined(WOLFSSL_ASYNC_CRYPT)
  20486. ret = wc_AsyncWait(ret, key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  20487. #endif
  20488. if (ret == 0)
  20489. ret = wc_ecc_sign_hash_ex(hash, sizeof(hash), rng, key, r, s);
  20490. } while (ret == WC_PENDING_E);
  20491. if (ret != 0) {
  20492. goto done;
  20493. }
  20494. TEST_SLEEP();
  20495. mp_read_radix(expR, expRstr, MP_RADIX_HEX);
  20496. mp_read_radix(expS, expSstr, MP_RADIX_HEX);
  20497. if (mp_cmp(r, expR) != MP_EQ) {
  20498. ret = -1;
  20499. }
  20500. done:
  20501. if (key_inited)
  20502. wc_ecc_free(key);
  20503. #ifdef WOLFSSL_SMALL_STACK
  20504. if (key != NULL)
  20505. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20506. if (r != NULL)
  20507. XFREE(r, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20508. if (s != NULL)
  20509. XFREE(s, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20510. if (expR != NULL)
  20511. XFREE(expR, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20512. if (expS != NULL)
  20513. XFREE(expS, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20514. #endif
  20515. return ret;
  20516. }
  20517. #endif /* HAVE_ECC521 */
  20518. #endif /* WOLFSSL_PUBLIC_MP */
  20519. #endif /* HAVE_ECC_SIGN && (WOLFSSL_ECDSA_DETERMINISTIC_K ||
  20520. WOLFSSL_ECDSA_DETERMINISTIC_K_VARIANT)
  20521. && (!FIPS_VERSION_GE || FIPS_VERSION_GE(5,3)) */
  20522. #if defined(HAVE_ECC_SIGN) && defined(WOLFSSL_ECDSA_SET_K) && \
  20523. !defined(WOLFSSL_KCAPI_ECC)
  20524. static int ecc_test_sign_vectors(WC_RNG* rng)
  20525. {
  20526. int ret;
  20527. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20528. ecc_key *key = NULL;
  20529. #else
  20530. ecc_key key[1];
  20531. #endif
  20532. int key_inited = 0;
  20533. byte sig[72];
  20534. word32 sigSz;
  20535. WOLFSSL_SMALL_STACK_STATIC const unsigned char hash[32] = "test wolfSSL deterministic sign";
  20536. WOLFSSL_SMALL_STACK_STATIC const char* dIUT = "7d7dc5f71eb29ddaf80d6214632eeae03d9058af1fb6d22ed80badb62bc1a534";
  20537. WOLFSSL_SMALL_STACK_STATIC const char* QIUTx = "ead218590119e8876b29146ff89ca61770c4edbbf97d38ce385ed281d8a6b230";
  20538. WOLFSSL_SMALL_STACK_STATIC const char* QIUTy = "28af61281fd35e2fa7002523acc85a429cb06ee6648325389f59edfce1405141";
  20539. WOLFSSL_SMALL_STACK_STATIC const byte k[1] = { 0x02 };
  20540. WOLFSSL_SMALL_STACK_STATIC const byte expSig[71] = {
  20541. 0x30, 0x45, 0x02, 0x20, 0x7c, 0xf2, 0x7b, 0x18,
  20542. 0x8d, 0x03, 0x4f, 0x7e, 0x8a, 0x52, 0x38, 0x03,
  20543. 0x04, 0xb5, 0x1a, 0xc3, 0xc0, 0x89, 0x69, 0xe2,
  20544. 0x77, 0xf2, 0x1b, 0x35, 0xa6, 0x0b, 0x48, 0xfc,
  20545. 0x47, 0x66, 0x99, 0x78, 0x02, 0x21, 0x00, 0xa8,
  20546. 0x43, 0xa0, 0xce, 0x6c, 0x5e, 0x17, 0x8a, 0x53,
  20547. 0x4d, 0xaf, 0xd2, 0x95, 0x78, 0x9f, 0x84, 0x4f,
  20548. 0x94, 0xb8, 0x75, 0xa3, 0x19, 0xa5, 0xd4, 0xdf,
  20549. 0xe1, 0xd4, 0x5e, 0x9d, 0x97, 0xfe, 0x81
  20550. };
  20551. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20552. if ((key = (ecc_key *)XMALLOC(sizeof(*key), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER)) == NULL)
  20553. return MEMORY_E;
  20554. #endif
  20555. ret = wc_ecc_init_ex(key, HEAP_HINT, devId);
  20556. if (ret != 0) {
  20557. goto done;
  20558. }
  20559. key_inited = 1;
  20560. ret = wc_ecc_import_raw(key, QIUTx, QIUTy, dIUT, "SECP256R1");
  20561. if (ret != 0) {
  20562. goto done;
  20563. }
  20564. wc_ecc_set_flags(key, WC_ECC_FLAG_DEC_SIGN);
  20565. ret = wc_ecc_sign_set_k(k, sizeof(k), key);
  20566. if (ret != 0) {
  20567. goto done;
  20568. }
  20569. sigSz = sizeof(sig);
  20570. do {
  20571. #if defined(WOLFSSL_ASYNC_CRYPT)
  20572. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  20573. #endif
  20574. if (ret == 0)
  20575. ret = wc_ecc_sign_hash(hash, sizeof(hash), sig, &sigSz, rng, key);
  20576. } while (ret == WC_PENDING_E);
  20577. if (ret != 0) {
  20578. goto done;
  20579. }
  20580. TEST_SLEEP();
  20581. if (sigSz != sizeof(expSig)) {
  20582. ret = -9830;
  20583. goto done;
  20584. }
  20585. if (XMEMCMP(sig, expSig, sigSz) != 0) {
  20586. ret = -9831;
  20587. goto done;
  20588. }
  20589. sigSz = sizeof(sig);
  20590. do {
  20591. #if defined(WOLFSSL_ASYNC_CRYPT)
  20592. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  20593. #endif
  20594. if (ret == 0)
  20595. ret = wc_ecc_sign_hash(hash, sizeof(hash), sig, &sigSz, rng, key);
  20596. } while (ret == WC_PENDING_E);
  20597. if (ret != 0) {
  20598. goto done;
  20599. }
  20600. TEST_SLEEP();
  20601. done:
  20602. if (key_inited)
  20603. wc_ecc_free(key);
  20604. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20605. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20606. #endif
  20607. return ret;
  20608. }
  20609. #endif
  20610. #ifdef HAVE_ECC_CDH
  20611. static int ecc_test_cdh_vectors(WC_RNG* rng)
  20612. {
  20613. int ret;
  20614. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20615. ecc_key *pub_key = (ecc_key *)XMALLOC(sizeof *pub_key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20616. ecc_key *priv_key = (ecc_key *)XMALLOC(sizeof *priv_key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20617. #else
  20618. ecc_key pub_key[1], priv_key[1];
  20619. #endif
  20620. byte sharedA[32] = {0}, sharedB[32] = {0};
  20621. word32 x, z;
  20622. WOLFSSL_SMALL_STACK_STATIC const char* QCAVSx = "700c48f77f56584c5cc632ca65640db91b6bacce3a4df6b42ce7cc838833d287";
  20623. WOLFSSL_SMALL_STACK_STATIC const char* QCAVSy = "db71e509e3fd9b060ddb20ba5c51dcc5948d46fbf640dfe0441782cab85fa4ac";
  20624. WOLFSSL_SMALL_STACK_STATIC const char* dIUT = "7d7dc5f71eb29ddaf80d6214632eeae03d9058af1fb6d22ed80badb62bc1a534";
  20625. WOLFSSL_SMALL_STACK_STATIC const char* QIUTx = "ead218590119e8876b29146ff89ca61770c4edbbf97d38ce385ed281d8a6b230";
  20626. WOLFSSL_SMALL_STACK_STATIC const char* QIUTy = "28af61281fd35e2fa7002523acc85a429cb06ee6648325389f59edfce1405141";
  20627. WOLFSSL_SMALL_STACK_STATIC const char* ZIUT = "46fc62106420ff012e54a434fbdd2d25ccc5852060561e68040dd7778997bd7b";
  20628. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20629. if ((pub_key == NULL) ||
  20630. (priv_key == NULL)) {
  20631. ret = MEMORY_E;
  20632. goto done;
  20633. }
  20634. #endif
  20635. XMEMSET(pub_key, 0, sizeof *pub_key);
  20636. XMEMSET(priv_key, 0, sizeof *priv_key);
  20637. /* setup private and public keys */
  20638. ret = wc_ecc_init_ex(pub_key, HEAP_HINT, devId);
  20639. if (ret != 0)
  20640. goto done;
  20641. ret = wc_ecc_init_ex(priv_key, HEAP_HINT, devId);
  20642. if (ret != 0)
  20643. goto done;
  20644. wc_ecc_set_flags(pub_key, WC_ECC_FLAG_COFACTOR);
  20645. wc_ecc_set_flags(priv_key, WC_ECC_FLAG_COFACTOR);
  20646. ret = wc_ecc_import_raw(pub_key, QCAVSx, QCAVSy, NULL, "SECP256R1");
  20647. if (ret != 0)
  20648. goto done;
  20649. ret = wc_ecc_import_raw(priv_key, QIUTx, QIUTy, dIUT, "SECP256R1");
  20650. if (ret != 0)
  20651. goto done;
  20652. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  20653. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  20654. !defined(HAVE_SELFTEST)
  20655. ret = wc_ecc_set_rng(priv_key, rng);
  20656. if (ret != 0)
  20657. goto done;
  20658. #else
  20659. (void)rng;
  20660. #endif
  20661. /* compute ECC Cofactor shared secret */
  20662. x = sizeof(sharedA);
  20663. do {
  20664. #if defined(WOLFSSL_ASYNC_CRYPT)
  20665. ret = wc_AsyncWait(ret, &priv_key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  20666. #endif
  20667. if (ret == 0)
  20668. ret = wc_ecc_shared_secret(priv_key, pub_key, sharedA, &x);
  20669. } while (ret == WC_PENDING_E);
  20670. if (ret != 0) {
  20671. goto done;
  20672. }
  20673. TEST_SLEEP();
  20674. /* read in expected Z */
  20675. z = sizeof(sharedB);
  20676. ret = Base16_Decode((const byte*)ZIUT, (word32)XSTRLEN(ZIUT), sharedB, &z);
  20677. if (ret != 0)
  20678. goto done;
  20679. /* compare results */
  20680. if (x != z || XMEMCMP(sharedA, sharedB, x)) {
  20681. ERROR_OUT(-9840, done);
  20682. }
  20683. done:
  20684. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20685. if (priv_key) {
  20686. wc_ecc_free(priv_key);
  20687. XFREE(priv_key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20688. }
  20689. if (pub_key) {
  20690. wc_ecc_free(pub_key);
  20691. XFREE(pub_key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20692. }
  20693. #else
  20694. wc_ecc_free(priv_key);
  20695. wc_ecc_free(pub_key);
  20696. #endif
  20697. return ret;
  20698. }
  20699. #endif /* HAVE_ECC_CDH */
  20700. #endif /* HAVE_ECC_VECTOR_TEST */
  20701. #ifdef HAVE_ECC_KEY_IMPORT
  20702. /* returns 0 on success */
  20703. static int ecc_test_make_pub(WC_RNG* rng)
  20704. {
  20705. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20706. ecc_key *key = (ecc_key *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20707. #if defined(HAVE_ECC_DHE) && defined(HAVE_ECC_KEY_EXPORT)
  20708. ecc_key *pub = (ecc_key *)XMALLOC(sizeof *pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20709. #endif
  20710. byte *exportBuf = (byte *)XMALLOC(ECC_BUFSIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20711. byte *tmp = (byte *)XMALLOC(ECC_BUFSIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20712. #else
  20713. ecc_key key[1];
  20714. #if defined(HAVE_ECC_DHE) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG) && \
  20715. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  20716. ecc_key pub[1];
  20717. #endif
  20718. byte exportBuf[ECC_BUFSIZE];
  20719. byte tmp[ECC_BUFSIZE];
  20720. #endif
  20721. const byte* msg = (const byte*)"test wolfSSL ECC public gen";
  20722. word32 x;
  20723. word32 tmpSz;
  20724. int ret = 0;
  20725. ecc_point* pubPoint = NULL;
  20726. #ifdef HAVE_ECC_VERIFY
  20727. int verify = 0;
  20728. #endif
  20729. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20730. if ((key == NULL) ||
  20731. #if defined(HAVE_ECC_DHE) && defined(HAVE_ECC_KEY_EXPORT)
  20732. (pub == NULL) ||
  20733. #endif
  20734. (exportBuf == NULL) ||
  20735. (tmp == NULL))
  20736. ERROR_OUT(MEMORY_E, done);
  20737. #endif
  20738. (void)msg;
  20739. (void)verify;
  20740. (void)exportBuf;
  20741. (void)rng;
  20742. wc_ecc_init_ex(key, HEAP_HINT, devId);
  20743. #ifndef NO_ECC256
  20744. #ifdef USE_CERT_BUFFERS_256
  20745. XMEMCPY(tmp, ecc_key_der_256, (size_t)sizeof_ecc_key_der_256);
  20746. tmpSz = (size_t)sizeof_ecc_key_der_256;
  20747. #else
  20748. {
  20749. XFILE file = XFOPEN(eccKeyDerFile, "rb");
  20750. if (!file) {
  20751. ERROR_OUT(-9850, done);
  20752. }
  20753. tmpSz = (word32)XFREAD(tmp, 1, ECC_BUFSIZE, file);
  20754. XFCLOSE(file);
  20755. }
  20756. #endif /* USE_CERT_BUFFERS_256 */
  20757. /* import private only then test with */
  20758. ret = wc_ecc_import_private_key(tmp, tmpSz, NULL, 0, NULL);
  20759. if (ret == 0) {
  20760. ERROR_OUT(-9851, done);
  20761. }
  20762. ret = wc_ecc_import_private_key(NULL, tmpSz, NULL, 0, key);
  20763. if (ret == 0) {
  20764. ERROR_OUT(-9852, done);
  20765. }
  20766. x = 0;
  20767. ret = wc_EccPrivateKeyDecode(tmp, &x, key, tmpSz);
  20768. if (ret != 0) {
  20769. ERROR_OUT(-9853, done);
  20770. }
  20771. #ifdef HAVE_ECC_KEY_EXPORT
  20772. x = ECC_BUFSIZE;
  20773. ret = wc_ecc_export_private_only(key, exportBuf, &x);
  20774. if (ret != 0) {
  20775. ERROR_OUT(-9854, done);
  20776. }
  20777. /* make private only key */
  20778. wc_ecc_free(key);
  20779. wc_ecc_init_ex(key, HEAP_HINT, devId);
  20780. ret = wc_ecc_import_private_key(exportBuf, x, NULL, 0, key);
  20781. if (ret != 0) {
  20782. ERROR_OUT(-9855, done);
  20783. }
  20784. x = ECC_BUFSIZE;
  20785. ret = wc_ecc_export_x963_ex(key, exportBuf, &x, 0);
  20786. if (ret == 0) {
  20787. ERROR_OUT(-9856, done);
  20788. }
  20789. #endif /* HAVE_ECC_KEY_EXPORT */
  20790. ret = wc_ecc_make_pub(NULL, NULL);
  20791. if (ret == 0) {
  20792. ERROR_OUT(-9857, done);
  20793. }
  20794. TEST_SLEEP();
  20795. #ifndef WOLFSSL_NO_MALLOC
  20796. pubPoint = wc_ecc_new_point_h(HEAP_HINT);
  20797. if (pubPoint == NULL) {
  20798. ERROR_OUT(-9858, done);
  20799. }
  20800. #if !defined(WOLFSSL_CRYPTOCELL)
  20801. ret = wc_ecc_make_pub(key, pubPoint);
  20802. #if defined(WOLFSSL_ASYNC_CRYPT)
  20803. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  20804. #endif
  20805. if (ret != 0) {
  20806. ERROR_OUT(-9859, done);
  20807. }
  20808. #endif
  20809. TEST_SLEEP();
  20810. #ifdef HAVE_ECC_KEY_EXPORT
  20811. /* export should still fail, is private only key */
  20812. x = ECC_BUFSIZE;
  20813. ret = wc_ecc_export_x963_ex(key, exportBuf, &x, 0);
  20814. if (ret == 0) {
  20815. ERROR_OUT(-9860, done);
  20816. }
  20817. #endif /* HAVE_ECC_KEY_EXPORT */
  20818. #endif /* !WOLFSSL_NO_MALLOC */
  20819. #endif /* !NO_ECC256 */
  20820. /* create a new key since above test for loading key is not supported */
  20821. #if defined(WOLFSSL_CRYPTOCELL) || defined(NO_ECC256) || \
  20822. defined(WOLFSSL_QNX_CAAM) || defined(WOLFSSL_SE050) || \
  20823. defined(WOLFSSL_SECO_CAAM)
  20824. ret = wc_ecc_make_key(rng, ECC_KEYGEN_SIZE, key);
  20825. if (ret != 0) {
  20826. ERROR_OUT(-9861, done);
  20827. }
  20828. #endif
  20829. #if defined(HAVE_ECC_SIGN) && (!defined(ECC_TIMING_RESISTANT) || \
  20830. (defined(ECC_TIMING_RESISTANT) && !defined(WC_NO_RNG))) && \
  20831. !defined(WOLF_CRYPTO_CB_ONLY_ECC) && !defined(HAVE_ECC_DETERMINISTIC_K)
  20832. tmpSz = ECC_BUFSIZE;
  20833. ret = 0;
  20834. do {
  20835. #if defined(WOLFSSL_ASYNC_CRYPT)
  20836. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  20837. #endif
  20838. if (ret == 0) {
  20839. ret = wc_ecc_sign_hash(msg, (word32)XSTRLEN((const char* )msg), tmp,
  20840. &tmpSz, rng, key);
  20841. }
  20842. } while (ret == WC_PENDING_E);
  20843. if (ret != 0) {
  20844. ERROR_OUT(-9862, done);
  20845. }
  20846. TEST_SLEEP();
  20847. #ifdef HAVE_ECC_VERIFY
  20848. /* try verify with private only key */
  20849. ret = 0;
  20850. do {
  20851. #if defined(WOLFSSL_ASYNC_CRYPT)
  20852. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  20853. #endif
  20854. if (ret == 0) {
  20855. ret = wc_ecc_verify_hash(tmp, tmpSz, msg,
  20856. (word32)XSTRLEN((const char*)msg), &verify, key);
  20857. }
  20858. } while (ret == WC_PENDING_E);
  20859. if (ret != 0) {
  20860. ERROR_OUT(-9863, done);
  20861. }
  20862. if (verify != 1) {
  20863. ERROR_OUT(-9864, done);
  20864. }
  20865. TEST_SLEEP();
  20866. #ifdef HAVE_ECC_KEY_EXPORT
  20867. /* exporting the public part should now work */
  20868. x = ECC_BUFSIZE;
  20869. ret = wc_ecc_export_x963_ex(key, exportBuf, &x, 0);
  20870. if (ret != 0) {
  20871. ERROR_OUT(-9865, done);
  20872. }
  20873. #endif /* HAVE_ECC_KEY_EXPORT */
  20874. #endif /* HAVE_ECC_VERIFY */
  20875. #endif /* HAVE_ECC_SIGN */
  20876. #if defined(HAVE_ECC_DHE) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  20877. /* now test private only key with creating a shared secret */
  20878. x = ECC_BUFSIZE;
  20879. ret = wc_ecc_export_private_only(key, exportBuf, &x);
  20880. if (ret != 0) {
  20881. ERROR_OUT(-9866, done);
  20882. }
  20883. #if !defined(WOLFSSL_QNX_CAAM) && !defined(WOLFSSL_SE050)
  20884. /* make private only key */
  20885. wc_ecc_free(key);
  20886. wc_ecc_init_ex(key, HEAP_HINT, devId);
  20887. ret = wc_ecc_import_private_key(exportBuf, x, NULL, 0, key);
  20888. if (ret != 0) {
  20889. ERROR_OUT(-9867, done);
  20890. }
  20891. /* check that public export fails with private only key */
  20892. x = ECC_BUFSIZE;
  20893. ret = wc_ecc_export_x963_ex(key, exportBuf, &x, 0);
  20894. if (ret == 0) {
  20895. ERROR_OUT(-9868, done);
  20896. }
  20897. #endif /* WOLFSSL_QNX_CAAM */
  20898. #ifndef WOLF_CRYPTO_CB_ONLY_ECC
  20899. /* make public key for shared secret */
  20900. wc_ecc_init_ex(pub, HEAP_HINT, devId);
  20901. ret = wc_ecc_make_key(rng, ECC_KEYGEN_SIZE, pub);
  20902. #ifdef HAVE_ECC_CDH
  20903. wc_ecc_set_flags(key, WC_ECC_FLAG_COFACTOR);
  20904. #endif
  20905. #if defined(WOLFSSL_ASYNC_CRYPT)
  20906. ret = wc_AsyncWait(ret, &pub->asyncDev, WC_ASYNC_FLAG_NONE);
  20907. #endif
  20908. if (ret != 0) {
  20909. ERROR_OUT(-9869, done);
  20910. }
  20911. TEST_SLEEP();
  20912. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  20913. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  20914. !defined(HAVE_SELFTEST)
  20915. ret = wc_ecc_set_rng(key, rng);
  20916. if (ret != 0)
  20917. goto done;
  20918. #endif
  20919. x = ECC_BUFSIZE;
  20920. do {
  20921. #if defined(WOLFSSL_ASYNC_CRYPT)
  20922. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  20923. #endif
  20924. if (ret == 0) {
  20925. ret = wc_ecc_shared_secret(key, pub, exportBuf, &x);
  20926. }
  20927. } while (ret == WC_PENDING_E);
  20928. wc_ecc_free(pub);
  20929. if (ret != 0) {
  20930. ERROR_OUT(-9870, done);
  20931. }
  20932. TEST_SLEEP();
  20933. #endif /* HAVE_ECC_DHE && HAVE_ECC_KEY_EXPORT && !WC_NO_RNG */
  20934. #endif /* WOLF_CRYPTO_CB_ONLY_ECC */
  20935. ret = 0;
  20936. done:
  20937. wc_ecc_del_point_h(pubPoint, HEAP_HINT);
  20938. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20939. if (key != NULL) {
  20940. wc_ecc_free(key);
  20941. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20942. }
  20943. #if defined(HAVE_ECC_DHE) && defined(HAVE_ECC_KEY_EXPORT)
  20944. if (pub != NULL)
  20945. XFREE(pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20946. #endif
  20947. if (exportBuf != NULL)
  20948. XFREE(exportBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20949. if (tmp != NULL)
  20950. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20951. #else
  20952. wc_ecc_free(key);
  20953. #endif
  20954. return ret;
  20955. }
  20956. #if defined(HAVE_ECC_KEY_EXPORT) && !defined(NO_ASN_CRYPT) && \
  20957. !defined(WC_NO_RNG) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  20958. static int ecc_test_key_decode(WC_RNG* rng, int keySize)
  20959. {
  20960. int ret;
  20961. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20962. ecc_key *eccKey = (ecc_key *)XMALLOC(sizeof *eccKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20963. byte *tmpBuf = (byte *)XMALLOC(ECC_BUFSIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20964. #else
  20965. ecc_key eccKey[1];
  20966. byte tmpBuf[ECC_BUFSIZE];
  20967. #endif
  20968. word32 tmpSz;
  20969. word32 idx;
  20970. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20971. if ((eccKey == NULL) || (tmpBuf == NULL))
  20972. ERROR_OUT(MEMORY_E, done);
  20973. #endif
  20974. ret = wc_ecc_init(eccKey);
  20975. if (ret != 0) {
  20976. goto done;
  20977. }
  20978. ret = wc_ecc_make_key(rng, keySize, eccKey);
  20979. #if defined(WOLFSSL_ASYNC_CRYPT)
  20980. ret = wc_AsyncWait(ret, &eccKey->asyncDev, WC_ASYNC_FLAG_NONE);
  20981. #endif
  20982. if (ret != 0) {
  20983. goto done;
  20984. }
  20985. tmpSz = ECC_BUFSIZE;
  20986. ret = wc_EccKeyToDer(eccKey, tmpBuf, tmpSz);
  20987. wc_ecc_free(eccKey);
  20988. if (ret < 0) {
  20989. goto done;
  20990. }
  20991. tmpSz = ret;
  20992. ret = wc_ecc_init(eccKey);
  20993. if (ret != 0) {
  20994. goto done;
  20995. }
  20996. idx = 0;
  20997. ret = wc_EccPrivateKeyDecode(tmpBuf, &idx, eccKey, tmpSz);
  20998. if (ret != 0) {
  20999. goto done;
  21000. }
  21001. wc_ecc_free(eccKey);
  21002. ret = wc_ecc_init(eccKey);
  21003. if (ret != 0) {
  21004. goto done;
  21005. }
  21006. idx = 0;
  21007. ret = wc_EccPublicKeyDecode(tmpBuf, &idx, eccKey, tmpSz);
  21008. if (ret != 0) {
  21009. goto done;
  21010. }
  21011. ret = 0;
  21012. done:
  21013. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21014. if (eccKey != NULL) {
  21015. wc_ecc_free(eccKey);
  21016. XFREE(eccKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21017. }
  21018. if (tmpBuf != NULL)
  21019. XFREE(tmpBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21020. #else
  21021. wc_ecc_free(eccKey);
  21022. #endif
  21023. return ret;
  21024. }
  21025. #endif /* HAVE_ECC_KEY_EXPORT && !NO_ASN_CRYPT */
  21026. #endif /* HAVE_ECC_KEY_IMPORT */
  21027. #if defined(HAVE_ECC_KEY_EXPORT) && !defined(NO_ASN_CRYPT) && \
  21028. !defined(WC_NO_RNG) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  21029. static int ecc_test_key_gen(WC_RNG* rng, int keySize)
  21030. {
  21031. int ret = 0;
  21032. int derSz;
  21033. #ifdef HAVE_PKCS8
  21034. word32 pkcs8Sz;
  21035. #endif
  21036. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21037. byte *der = (byte *)XMALLOC(ECC_BUFSIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21038. ecc_key *userA = (ecc_key *)XMALLOC(sizeof *userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21039. #else
  21040. byte der[ECC_BUFSIZE];
  21041. ecc_key userA[1];
  21042. #endif
  21043. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21044. if ((der == NULL) || (userA == NULL))
  21045. ERROR_OUT(MEMORY_E, done);
  21046. #endif
  21047. ret = wc_ecc_init_ex(userA, HEAP_HINT, devId);
  21048. if (ret != 0)
  21049. goto done;
  21050. ret = wc_ecc_make_key(rng, keySize, userA);
  21051. #if defined(WOLFSSL_ASYNC_CRYPT)
  21052. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_NONE);
  21053. #endif
  21054. if (ret != 0)
  21055. goto done;
  21056. TEST_SLEEP();
  21057. ret = wc_ecc_check_key(userA);
  21058. if (ret != 0)
  21059. goto done;
  21060. TEST_SLEEP();
  21061. derSz = wc_EccKeyToDer(userA, der, ECC_BUFSIZE);
  21062. if (derSz < 0) {
  21063. ERROR_OUT(derSz, done);
  21064. }
  21065. ret = SaveDerAndPem(der, derSz, eccCaKeyTempFile, eccCaKeyPemFile,
  21066. ECC_PRIVATEKEY_TYPE, -8347);
  21067. if (ret != 0) {
  21068. goto done;
  21069. }
  21070. /* test export of public key */
  21071. derSz = wc_EccPublicKeyToDer(userA, der, ECC_BUFSIZE, 1);
  21072. if (derSz < 0) {
  21073. ERROR_OUT(derSz, done);
  21074. }
  21075. if (derSz == 0) {
  21076. ERROR_OUT(-9890, done);
  21077. }
  21078. #ifdef HAVE_COMP_KEY
  21079. /* test export of compressed public key */
  21080. derSz = wc_EccPublicKeyToDer_ex(userA, der, ECC_BUFSIZE, 1, 1);
  21081. if (derSz < 0) {
  21082. ERROR_OUT(derSz, done);
  21083. }
  21084. if (derSz == 0) {
  21085. ERROR_OUT(-9890, done);
  21086. }
  21087. #endif
  21088. ret = SaveDerAndPem(der, derSz, eccPubKeyDerFile, NULL, 0, -8348);
  21089. if (ret != 0) {
  21090. goto done;
  21091. }
  21092. #ifdef HAVE_PKCS8
  21093. /* test export of PKCS#8 unencrypted private key */
  21094. pkcs8Sz = FOURK_BUF;
  21095. derSz = wc_EccPrivateKeyToPKCS8(userA, der, &pkcs8Sz);
  21096. if (derSz < 0) {
  21097. ERROR_OUT(derSz, done);
  21098. }
  21099. if (derSz == 0) {
  21100. ERROR_OUT(-9891, done);
  21101. }
  21102. ret = SaveDerAndPem(der, derSz, eccPkcs8KeyDerFile, NULL, 0, -8349);
  21103. if (ret != 0) {
  21104. goto done;
  21105. }
  21106. #endif /* HAVE_PKCS8 */
  21107. done:
  21108. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21109. if (der != NULL)
  21110. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21111. if (userA != NULL) {
  21112. wc_ecc_free(userA);
  21113. XFREE(userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21114. }
  21115. #else
  21116. wc_ecc_free(userA);
  21117. #endif
  21118. return ret;
  21119. }
  21120. #endif /* HAVE_ECC_KEY_EXPORT && !NO_ASN_CRYPT */
  21121. static int ecc_test_curve_size(WC_RNG* rng, int keySize, int testVerifyCount,
  21122. int curve_id, const ecc_set_type* dp)
  21123. {
  21124. #if (defined(HAVE_ECC_DHE) || defined(HAVE_ECC_CDH)) && !defined(WC_NO_RNG) && \
  21125. !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A)
  21126. WC_DECLARE_VAR(sharedA, byte, ECC_SHARED_SIZE, HEAP_HINT);
  21127. WC_DECLARE_VAR(sharedB, byte, ECC_SHARED_SIZE, HEAP_HINT);
  21128. #endif
  21129. #ifdef HAVE_ECC_KEY_EXPORT
  21130. #define ECC_KEY_EXPORT_BUF_SIZE (MAX_ECC_BYTES * 2 + 32)
  21131. WC_DECLARE_VAR(exportBuf, byte, ECC_KEY_EXPORT_BUF_SIZE, HEAP_HINT);
  21132. #endif
  21133. word32 x = 0;
  21134. #if (defined(HAVE_ECC_DHE) || defined(HAVE_ECC_CDH)) && !defined(WC_NO_RNG) && \
  21135. !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A)
  21136. word32 y;
  21137. #endif
  21138. #if defined(HAVE_ECC_SIGN) && !defined(WOLFSSL_KCAPI_ECC)
  21139. WC_DECLARE_VAR(sig, byte, ECC_SIG_SIZE, HEAP_HINT);
  21140. WC_DECLARE_VAR(digest, byte, ECC_DIGEST_SIZE, HEAP_HINT);
  21141. int i;
  21142. #ifdef HAVE_ECC_VERIFY
  21143. int verify;
  21144. #endif /* HAVE_ECC_VERIFY */
  21145. #endif /* HAVE_ECC_SIGN */
  21146. int ret;
  21147. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21148. ecc_key *userA = (ecc_key *)XMALLOC(sizeof *userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21149. ecc_key *userB = (ecc_key *)XMALLOC(sizeof *userB, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21150. ecc_key *pubKey = (ecc_key *)XMALLOC(sizeof *pubKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21151. #else
  21152. ecc_key userA[1];
  21153. ecc_key userB[1];
  21154. ecc_key pubKey[1];
  21155. #endif
  21156. #ifndef WC_NO_RNG
  21157. int curveSize;
  21158. #endif
  21159. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  21160. #if (defined(HAVE_ECC_DHE) || defined(HAVE_ECC_CDH)) && !defined(WC_NO_RNG) && \
  21161. !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A)
  21162. if (sharedA == NULL || sharedB == NULL)
  21163. ERROR_OUT(-9900, done);
  21164. #endif
  21165. #ifdef HAVE_ECC_KEY_EXPORT
  21166. if (exportBuf == NULL)
  21167. ERROR_OUT(-9901, done);
  21168. #endif
  21169. #if defined(HAVE_ECC_SIGN) && !defined(WOLFSSL_KCAPI_ECC)
  21170. if (sig == NULL || digest == NULL)
  21171. ERROR_OUT(-9902, done);
  21172. #endif
  21173. #endif /* WOLFSSL_SMALL_STACK && !WOLFSSL_NO_MALLOC */
  21174. (void)testVerifyCount;
  21175. (void)dp;
  21176. (void)x;
  21177. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21178. if ((userA == NULL) ||
  21179. (userB == NULL) ||
  21180. (pubKey == NULL))
  21181. ERROR_OUT(-9903, done);
  21182. #endif
  21183. XMEMSET(userA, 0, sizeof *userA);
  21184. XMEMSET(userB, 0, sizeof *userB);
  21185. XMEMSET(pubKey, 0, sizeof *pubKey);
  21186. ret = wc_ecc_init_ex(userA, HEAP_HINT, devId);
  21187. if (ret != 0)
  21188. ERROR_OUT(-9904, done);
  21189. ret = wc_ecc_init_ex(userB, HEAP_HINT, devId);
  21190. if (ret != 0)
  21191. ERROR_OUT(-9905, done);
  21192. ret = wc_ecc_init_ex(pubKey, HEAP_HINT, devId);
  21193. if (ret != 0)
  21194. ERROR_OUT(-9906, done);
  21195. #ifdef WOLFSSL_CUSTOM_CURVES
  21196. if (dp != NULL) {
  21197. ret = wc_ecc_set_custom_curve(userA, dp);
  21198. if (ret != 0)
  21199. ERROR_OUT(-9907, done);
  21200. ret = wc_ecc_set_custom_curve(userB, dp);
  21201. if (ret != 0)
  21202. ERROR_OUT(-9908, done);
  21203. }
  21204. #endif
  21205. #ifndef WC_NO_RNG
  21206. ret = wc_ecc_make_key_ex(rng, keySize, userA, curve_id);
  21207. #if defined(WOLFSSL_ASYNC_CRYPT)
  21208. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_NONE);
  21209. #endif
  21210. #ifdef WOLF_CRYPTO_CB_ONLY_ECC
  21211. if (ret == NO_VALID_DEVID) {
  21212. ret = 0;
  21213. goto done; /* no software case */
  21214. }
  21215. #endif
  21216. if (ret == ECC_CURVE_OID_E)
  21217. goto done; /* catch case, where curve is not supported */
  21218. if (ret != 0)
  21219. ERROR_OUT(-9910, done);
  21220. TEST_SLEEP();
  21221. if (wc_ecc_get_curve_idx(curve_id) != -1) {
  21222. curveSize = wc_ecc_get_curve_size_from_id(userA->dp->id);
  21223. if (curveSize != userA->dp->size)
  21224. ERROR_OUT(-9911, done);
  21225. }
  21226. ret = wc_ecc_check_key(userA);
  21227. if (ret != 0)
  21228. ERROR_OUT(-9912, done);
  21229. TEST_SLEEP();
  21230. /* ATECC508/608 configuration may not support more than one ECDH key */
  21231. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A)
  21232. ret = wc_ecc_make_key_ex(rng, keySize, userB, curve_id);
  21233. #if defined(WOLFSSL_ASYNC_CRYPT)
  21234. ret = wc_AsyncWait(ret, &userB->asyncDev, WC_ASYNC_FLAG_NONE);
  21235. #endif
  21236. if (ret != 0)
  21237. ERROR_OUT(-9914, done);
  21238. TEST_SLEEP();
  21239. /* only perform the below tests if the key size matches */
  21240. if (dp == NULL && keySize > 0 && wc_ecc_size(userA) != keySize)
  21241. ERROR_OUT(ECC_CURVE_OID_E, done);
  21242. #ifdef HAVE_ECC_DHE
  21243. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  21244. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  21245. !defined(HAVE_SELFTEST)
  21246. ret = wc_ecc_set_rng(userA, rng);
  21247. if (ret != 0)
  21248. ERROR_OUT(-9915, done);
  21249. ret = wc_ecc_set_rng(userB, rng);
  21250. if (ret != 0)
  21251. ERROR_OUT(-9916, done);
  21252. #endif
  21253. x = ECC_SHARED_SIZE;
  21254. do {
  21255. #if defined(WOLFSSL_ASYNC_CRYPT)
  21256. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  21257. #endif
  21258. if (ret == 0)
  21259. ret = wc_ecc_shared_secret(userA, userB, sharedA, &x);
  21260. } while (ret == WC_PENDING_E);
  21261. if (ret != 0) {
  21262. ERROR_OUT(-9917, done);
  21263. }
  21264. TEST_SLEEP();
  21265. y = ECC_SHARED_SIZE;
  21266. do {
  21267. #if defined(WOLFSSL_ASYNC_CRYPT)
  21268. ret = wc_AsyncWait(ret, &userB->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  21269. #endif
  21270. if (ret == 0)
  21271. ret = wc_ecc_shared_secret(userB, userA, sharedB, &y);
  21272. } while (ret == WC_PENDING_E);
  21273. if (ret != 0)
  21274. ERROR_OUT(-9918, done);
  21275. if (y != x)
  21276. ERROR_OUT(-9919, done);
  21277. if (XMEMCMP(sharedA, sharedB, x))
  21278. ERROR_OUT(-9920, done);
  21279. TEST_SLEEP();
  21280. #endif /* HAVE_ECC_DHE */
  21281. #ifdef HAVE_ECC_CDH
  21282. /* add cofactor flag */
  21283. wc_ecc_set_flags(userA, WC_ECC_FLAG_COFACTOR);
  21284. wc_ecc_set_flags(userB, WC_ECC_FLAG_COFACTOR);
  21285. x = ECC_SHARED_SIZE;
  21286. do {
  21287. #if defined(WOLFSSL_ASYNC_CRYPT)
  21288. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  21289. #endif
  21290. if (ret == 0)
  21291. ret = wc_ecc_shared_secret(userA, userB, sharedA, &x);
  21292. } while (ret == WC_PENDING_E);
  21293. if (ret != 0)
  21294. ERROR_OUT(-9921, done);
  21295. TEST_SLEEP();
  21296. y = ECC_SHARED_SIZE;
  21297. do {
  21298. #if defined(WOLFSSL_ASYNC_CRYPT)
  21299. ret = wc_AsyncWait(ret, &userB->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  21300. #endif
  21301. if (ret == 0)
  21302. ret = wc_ecc_shared_secret(userB, userA, sharedB, &y);
  21303. } while (ret == WC_PENDING_E);
  21304. if (ret != 0)
  21305. ERROR_OUT(-9922, done);
  21306. if (y != x)
  21307. ERROR_OUT(-9923, done);
  21308. if (XMEMCMP(sharedA, sharedB, x))
  21309. ERROR_OUT(-9924, done);
  21310. TEST_SLEEP();
  21311. /* remove cofactor flag */
  21312. wc_ecc_set_flags(userA, 0);
  21313. wc_ecc_set_flags(userB, 0);
  21314. #endif /* HAVE_ECC_CDH */
  21315. #endif /* !WOLFSSL_ATECC508A && WOLFSSL_ATECC608A */
  21316. #ifdef HAVE_ECC_KEY_EXPORT
  21317. x = ECC_KEY_EXPORT_BUF_SIZE;
  21318. ret = wc_ecc_export_x963_ex(userA, exportBuf, &x, 0);
  21319. if (ret != 0)
  21320. ERROR_OUT(-9925, done);
  21321. #ifdef HAVE_ECC_KEY_IMPORT
  21322. #ifdef WOLFSSL_CUSTOM_CURVES
  21323. if (dp != NULL) {
  21324. ret = wc_ecc_set_custom_curve(pubKey, dp);
  21325. if (ret != 0)
  21326. ERROR_OUT(-9926, done);
  21327. }
  21328. #endif
  21329. ret = wc_ecc_import_x963_ex(exportBuf, x, pubKey, curve_id);
  21330. if (ret != 0)
  21331. ERROR_OUT(-9927, done);
  21332. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A)
  21333. #ifdef HAVE_ECC_DHE
  21334. y = ECC_SHARED_SIZE;
  21335. do {
  21336. #if defined(WOLFSSL_ASYNC_CRYPT)
  21337. ret = wc_AsyncWait(ret, &userB->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  21338. #endif
  21339. if (ret == 0)
  21340. ret = wc_ecc_shared_secret(userB, pubKey, sharedB, &y);
  21341. } while (ret == WC_PENDING_E);
  21342. if (ret != 0)
  21343. ERROR_OUT(-9928, done);
  21344. if (XMEMCMP(sharedA, sharedB, y))
  21345. ERROR_OUT(-9929, done);
  21346. TEST_SLEEP();
  21347. #endif /* HAVE_ECC_DHE */
  21348. #ifdef HAVE_COMP_KEY
  21349. /* try compressed export / import too */
  21350. x = ECC_KEY_EXPORT_BUF_SIZE;
  21351. ret = wc_ecc_export_x963_ex(userA, exportBuf, &x, 1);
  21352. if (ret != 0)
  21353. ERROR_OUT(-9930, done);
  21354. wc_ecc_free(pubKey);
  21355. ret = wc_ecc_init_ex(pubKey, HEAP_HINT, devId);
  21356. if (ret != 0)
  21357. ERROR_OUT(-9931, done);
  21358. #ifdef WOLFSSL_CUSTOM_CURVES
  21359. if (dp != NULL) {
  21360. ret = wc_ecc_set_custom_curve(pubKey, dp);
  21361. if (ret != 0)
  21362. ERROR_OUT(-9932, done);
  21363. }
  21364. #endif
  21365. ret = wc_ecc_import_x963_ex(exportBuf, x, pubKey, curve_id);
  21366. if (ret != 0)
  21367. ERROR_OUT(-9933, done);
  21368. #ifdef HAVE_ECC_DHE
  21369. y = ECC_SHARED_SIZE;
  21370. do {
  21371. #if defined(WOLFSSL_ASYNC_CRYPT)
  21372. ret = wc_AsyncWait(ret, &userB->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  21373. #endif
  21374. if (ret == 0)
  21375. ret = wc_ecc_shared_secret(userB, pubKey, sharedB, &y);
  21376. } while (ret == WC_PENDING_E);
  21377. if (ret != 0)
  21378. ERROR_OUT(-9934, done);
  21379. if (XMEMCMP(sharedA, sharedB, y))
  21380. ERROR_OUT(-9935, done);
  21381. TEST_SLEEP();
  21382. #endif /* HAVE_ECC_DHE */
  21383. #endif /* HAVE_COMP_KEY */
  21384. #endif /* !WOLFSSL_ATECC508A && !WOLFSSL_ATECC608A */
  21385. #endif /* !WC_NO_RNG */
  21386. #endif /* HAVE_ECC_KEY_IMPORT */
  21387. #endif /* HAVE_ECC_KEY_EXPORT */
  21388. /* For KCAPI cannot sign using generated ECDH key */
  21389. #if !defined(ECC_TIMING_RESISTANT) || (defined(ECC_TIMING_RESISTANT) && \
  21390. !defined(WC_NO_RNG) && !defined(WOLFSSL_KCAPI_ECC))
  21391. #ifdef HAVE_ECC_SIGN
  21392. /* ECC w/out Shamir has issue with all 0 digest */
  21393. /* WC_BIGINT doesn't have 0 len well on hardware */
  21394. /* Cryptocell has issues with all 0 digest */
  21395. #if defined(ECC_SHAMIR) && !defined(WOLFSSL_ASYNC_CRYPT) && \
  21396. !defined(WOLFSSL_CRYPTOCELL)
  21397. /* test DSA sign hash with zeros */
  21398. for (i = 0; i < (int)ECC_DIGEST_SIZE; i++) {
  21399. digest[i] = 0;
  21400. }
  21401. x = ECC_SIG_SIZE;
  21402. do {
  21403. #if defined(WOLFSSL_ASYNC_CRYPT)
  21404. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  21405. #endif
  21406. if (ret == 0)
  21407. ret = wc_ecc_sign_hash(digest, ECC_DIGEST_SIZE, sig, &x, rng,
  21408. userA);
  21409. } while (ret == WC_PENDING_E);
  21410. if (ret != 0)
  21411. ERROR_OUT(-9936, done);
  21412. TEST_SLEEP();
  21413. #ifdef HAVE_ECC_VERIFY
  21414. for (i=0; i<testVerifyCount; i++) {
  21415. verify = 0;
  21416. do {
  21417. #if defined(WOLFSSL_ASYNC_CRYPT)
  21418. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  21419. #endif
  21420. if (ret == 0)
  21421. ret = wc_ecc_verify_hash(sig, x, digest, ECC_DIGEST_SIZE,
  21422. &verify, userA);
  21423. } while (ret == WC_PENDING_E);
  21424. if (ret != 0)
  21425. ERROR_OUT(-9937, done);
  21426. if (verify != 1)
  21427. ERROR_OUT(-9938, done);
  21428. TEST_SLEEP();
  21429. }
  21430. #endif /* HAVE_ECC_VERIFY */
  21431. #endif /* ECC_SHAMIR && !WOLFSSL_ASYNC_CRYPT && !WOLFSSL_CRYPTOCELL */
  21432. /* test DSA sign hash with sequence (0,1,2,3,4,...) */
  21433. for (i = 0; i < (int)ECC_DIGEST_SIZE; i++) {
  21434. digest[i] = (byte)i;
  21435. }
  21436. x = ECC_SIG_SIZE;
  21437. do {
  21438. #if defined(WOLFSSL_ASYNC_CRYPT)
  21439. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  21440. #endif
  21441. if (ret == 0)
  21442. ret = wc_ecc_sign_hash(digest, ECC_DIGEST_SIZE, sig, &x, rng, userA);
  21443. } while (ret == WC_PENDING_E);
  21444. if (ret != 0)
  21445. ERROR_OUT(-9939, done);
  21446. TEST_SLEEP();
  21447. #ifdef HAVE_ECC_VERIFY
  21448. for (i=0; i<testVerifyCount; i++) {
  21449. verify = 0;
  21450. do {
  21451. #if defined(WOLFSSL_ASYNC_CRYPT)
  21452. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  21453. #endif
  21454. if (ret == 0)
  21455. ret = wc_ecc_verify_hash(sig, x, digest, ECC_DIGEST_SIZE, &verify, userA);
  21456. } while (ret == WC_PENDING_E);
  21457. if (ret != 0)
  21458. ERROR_OUT(-9940, done);
  21459. if (verify != 1)
  21460. ERROR_OUT(-9941, done);
  21461. TEST_SLEEP();
  21462. }
  21463. #endif /* HAVE_ECC_VERIFY */
  21464. #endif /* HAVE_ECC_SIGN */
  21465. #endif /* !ECC_TIMING_RESISTANT || (ECC_TIMING_RESISTANT &&
  21466. * !WC_NO_RNG && !WOLFSSL_KCAPI_ECC) */
  21467. #if defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG) && \
  21468. !defined(WOLFSSL_ATECC508) && !defined(WOLFSSL_ATECC608A) && \
  21469. !defined(WOLFSSL_KCAPI_ECC)
  21470. x = ECC_KEY_EXPORT_BUF_SIZE;
  21471. ret = wc_ecc_export_private_only(userA, exportBuf, &x);
  21472. if (ret != 0)
  21473. ERROR_OUT(-9942, done);
  21474. #elif defined(HAVE_ECC_KEY_EXPORT)
  21475. (void)exportBuf;
  21476. #endif /* HAVE_ECC_KEY_EXPORT */
  21477. done:
  21478. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21479. if (userA != NULL) {
  21480. wc_ecc_free(userA);
  21481. XFREE(userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21482. }
  21483. if (userB != NULL) {
  21484. wc_ecc_free(userB);
  21485. XFREE(userB, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21486. }
  21487. if (pubKey != NULL) {
  21488. wc_ecc_free(pubKey);
  21489. XFREE(pubKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21490. }
  21491. #else
  21492. wc_ecc_free(pubKey);
  21493. wc_ecc_free(userB);
  21494. wc_ecc_free(userA);
  21495. #endif
  21496. #if defined(HAVE_ECC_DHE) || defined(HAVE_ECC_CDH)
  21497. WC_FREE_VAR(sharedA, HEAP_HINT);
  21498. WC_FREE_VAR(sharedB, HEAP_HINT);
  21499. #endif
  21500. #ifdef HAVE_ECC_KEY_EXPORT
  21501. WC_FREE_VAR(exportBuf, HEAP_HINT);
  21502. #endif
  21503. #ifdef HAVE_ECC_SIGN
  21504. WC_FREE_VAR(sig, HEAP_HINT);
  21505. WC_FREE_VAR(digest, HEAP_HINT);
  21506. #endif
  21507. (void)keySize;
  21508. (void)curve_id;
  21509. (void)rng;
  21510. return ret;
  21511. }
  21512. #undef ECC_TEST_VERIFY_COUNT
  21513. #define ECC_TEST_VERIFY_COUNT 2
  21514. static int ecc_test_curve(WC_RNG* rng, int keySize)
  21515. {
  21516. int ret;
  21517. ret = ecc_test_curve_size(rng, keySize, ECC_TEST_VERIFY_COUNT,
  21518. ECC_CURVE_DEF, NULL);
  21519. if (ret < 0) {
  21520. if (ret == ECC_CURVE_OID_E) {
  21521. /* ignore error for curves not found */
  21522. /* some curve sizes are only available with:
  21523. HAVE_ECC_SECPR2, HAVE_ECC_SECPR3, HAVE_ECC_BRAINPOOL
  21524. and HAVE_ECC_KOBLITZ */
  21525. }
  21526. else {
  21527. printf("ecc_test_curve_size %d failed!: %d\n", keySize, ret);
  21528. return ret;
  21529. }
  21530. }
  21531. #ifndef WOLF_CRYPTO_CB_ONLY_ECC
  21532. #ifdef HAVE_ECC_VECTOR_TEST
  21533. ret = ecc_test_vector(keySize);
  21534. if (ret < 0) {
  21535. printf("ecc_test_vector %d failed!: %d\n", keySize, ret);
  21536. return ret;
  21537. }
  21538. #endif
  21539. #if defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT) && \
  21540. !defined(NO_ASN_CRYPT) && !defined(WC_NO_RNG)
  21541. ret = ecc_test_key_decode(rng, keySize);
  21542. if (ret < 0) {
  21543. if (ret == ECC_CURVE_OID_E) {
  21544. /* ignore error for curves not found */
  21545. }
  21546. else {
  21547. printf("ecc_test_key_decode %d failed!: %d\n", keySize, ret);
  21548. return ret;
  21549. }
  21550. }
  21551. #endif
  21552. #if defined(HAVE_ECC_KEY_EXPORT) && !defined(NO_ASN_CRYPT) && !defined(WC_NO_RNG)
  21553. ret = ecc_test_key_gen(rng, keySize);
  21554. if (ret < 0) {
  21555. if (ret == ECC_CURVE_OID_E) {
  21556. /* ignore error for curves not found */
  21557. }
  21558. else {
  21559. printf("ecc_test_key_gen %d failed!: %d\n", keySize, ret);
  21560. return ret;
  21561. }
  21562. }
  21563. #endif
  21564. #endif /* WOLF_CRYPTO_CB_ONLY_ECC */
  21565. return 0;
  21566. }
  21567. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  21568. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  21569. defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT) && \
  21570. !defined(WOLFSSL_NO_MALLOC) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  21571. static int ecc_point_test(void)
  21572. {
  21573. int ret;
  21574. ecc_point* point;
  21575. ecc_point* point2;
  21576. #ifdef HAVE_COMP_KEY
  21577. ecc_point* point3;
  21578. ecc_point* point4;
  21579. #endif
  21580. word32 outLen;
  21581. byte out[65];
  21582. byte der[] = { 0x04, /* = Uncompressed */
  21583. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  21584. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  21585. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  21586. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  21587. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  21588. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  21589. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  21590. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  21591. #if defined(HAVE_COMP_KEY) && (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) || \
  21592. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  21593. byte derComp0[] = { 0x02, /* = Compressed, y even */
  21594. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  21595. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  21596. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  21597. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  21598. byte derComp1[] = { 0x03, /* = Compressed, y odd */
  21599. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  21600. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  21601. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  21602. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  21603. #endif
  21604. byte altDer[] = { 0x04, /* = Uncompressed */
  21605. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  21606. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  21607. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  21608. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  21609. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  21610. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  21611. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  21612. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
  21613. int curve_idx = wc_ecc_get_curve_idx(ECC_SECP256R1);
  21614. /* if curve P256 is not enabled then test should not fail */
  21615. if (curve_idx == ECC_CURVE_INVALID)
  21616. return 0;
  21617. outLen = sizeof(out);
  21618. point = wc_ecc_new_point();
  21619. if (point == NULL)
  21620. return -10000;
  21621. point2 = wc_ecc_new_point();
  21622. if (point2 == NULL) {
  21623. wc_ecc_del_point(point);
  21624. return -10001;
  21625. }
  21626. #ifdef HAVE_COMP_KEY
  21627. point3 = wc_ecc_new_point();
  21628. if (point3 == NULL) {
  21629. wc_ecc_del_point(point2);
  21630. wc_ecc_del_point(point);
  21631. return -10002;
  21632. }
  21633. point4 = wc_ecc_new_point();
  21634. if (point4 == NULL) {
  21635. wc_ecc_del_point(point3);
  21636. wc_ecc_del_point(point2);
  21637. wc_ecc_del_point(point);
  21638. return -10003;
  21639. }
  21640. #endif
  21641. /* Parameter Validation testing. */
  21642. wc_ecc_del_point(NULL);
  21643. ret = wc_ecc_import_point_der(NULL, sizeof(der), curve_idx, point);
  21644. if (ret != ECC_BAD_ARG_E) {
  21645. ret = -10004;
  21646. goto done;
  21647. }
  21648. ret = wc_ecc_import_point_der(der, sizeof(der), ECC_CURVE_INVALID, point);
  21649. if (ret != ECC_BAD_ARG_E) {
  21650. ret = -10005;
  21651. goto done;
  21652. }
  21653. ret = wc_ecc_import_point_der(der, sizeof(der), curve_idx, NULL);
  21654. if (ret != ECC_BAD_ARG_E) {
  21655. ret = -10006;
  21656. goto done;
  21657. }
  21658. ret = wc_ecc_export_point_der(-1, point, out, &outLen);
  21659. if (ret != ECC_BAD_ARG_E) {
  21660. ret = -10007;
  21661. goto done;
  21662. }
  21663. ret = wc_ecc_export_point_der(curve_idx, NULL, out, &outLen);
  21664. if (ret != ECC_BAD_ARG_E) {
  21665. ret = -10008;
  21666. goto done;
  21667. }
  21668. ret = wc_ecc_export_point_der(curve_idx, point, NULL, &outLen);
  21669. if (ret != LENGTH_ONLY_E || outLen != sizeof(out)) {
  21670. ret = -10009;
  21671. goto done;
  21672. }
  21673. ret = wc_ecc_export_point_der(curve_idx, point, out, NULL);
  21674. if (ret != ECC_BAD_ARG_E) {
  21675. ret = -10010;
  21676. goto done;
  21677. }
  21678. outLen = 0;
  21679. ret = wc_ecc_export_point_der(curve_idx, point, out, &outLen);
  21680. if (ret != BUFFER_E) {
  21681. ret = -10011;
  21682. goto done;
  21683. }
  21684. ret = wc_ecc_copy_point(NULL, NULL);
  21685. if (ret != ECC_BAD_ARG_E) {
  21686. ret = -10012;
  21687. goto done;
  21688. }
  21689. ret = wc_ecc_copy_point(NULL, point2);
  21690. if (ret != ECC_BAD_ARG_E) {
  21691. ret = -10013;
  21692. goto done;
  21693. }
  21694. ret = wc_ecc_copy_point(point, NULL);
  21695. if (ret != ECC_BAD_ARG_E) {
  21696. ret = -10014;
  21697. goto done;
  21698. }
  21699. ret = wc_ecc_cmp_point(NULL, NULL);
  21700. if (ret != BAD_FUNC_ARG) {
  21701. ret = -10015;
  21702. goto done;
  21703. }
  21704. ret = wc_ecc_cmp_point(NULL, point2);
  21705. if (ret != BAD_FUNC_ARG) {
  21706. ret = -10016;
  21707. goto done;
  21708. }
  21709. ret = wc_ecc_cmp_point(point, NULL);
  21710. if (ret != BAD_FUNC_ARG) {
  21711. ret = -10017;
  21712. goto done;
  21713. }
  21714. /* Use API. */
  21715. ret = wc_ecc_import_point_der(der, sizeof(der), curve_idx, point);
  21716. if (ret != 0) {
  21717. ret = -10018;
  21718. goto done;
  21719. }
  21720. outLen = sizeof(out);
  21721. ret = wc_ecc_export_point_der(curve_idx, point, out, &outLen);
  21722. if (ret != 0) {
  21723. ret = -10019;
  21724. goto done;
  21725. }
  21726. if (outLen != sizeof(der)) {
  21727. ret = -10020;
  21728. goto done;
  21729. }
  21730. if (XMEMCMP(out, der, outLen) != 0) {
  21731. ret = -10021;
  21732. goto done;
  21733. }
  21734. ret = wc_ecc_copy_point(point2, point);
  21735. if (ret != MP_OKAY) {
  21736. ret = -10022;
  21737. goto done;
  21738. }
  21739. ret = wc_ecc_cmp_point(point2, point);
  21740. if (ret != MP_EQ) {
  21741. ret = -10023;
  21742. goto done;
  21743. }
  21744. ret = wc_ecc_import_point_der(altDer, sizeof(altDer), curve_idx, point2);
  21745. if (ret != 0) {
  21746. ret = -10024;
  21747. goto done;
  21748. }
  21749. ret = wc_ecc_cmp_point(point2, point);
  21750. if (ret != MP_GT) {
  21751. ret = -10025;
  21752. goto done;
  21753. }
  21754. #if defined(HAVE_COMP_KEY) && (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) || \
  21755. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  21756. ret = wc_ecc_import_point_der(derComp0, sizeof(derComp0)*2-1, curve_idx, point3);
  21757. if (ret != 0) {
  21758. ret = -10026;
  21759. goto done;
  21760. }
  21761. ret = wc_ecc_import_point_der_ex(derComp0, sizeof(derComp0), curve_idx, point4, 0);
  21762. if (ret != 0) {
  21763. ret = -10027;
  21764. goto done;
  21765. }
  21766. ret = wc_ecc_cmp_point(point3, point4);
  21767. if (ret != MP_EQ) {
  21768. ret = -10028;
  21769. goto done;
  21770. }
  21771. ret = wc_ecc_import_point_der(derComp1, sizeof(derComp1)*2-1, curve_idx, point3);
  21772. if (ret != 0) {
  21773. ret = -10029;
  21774. goto done;
  21775. }
  21776. ret = wc_ecc_import_point_der_ex(derComp1, sizeof(derComp1), curve_idx, point4, 0);
  21777. if (ret != 0) {
  21778. ret = -10030;
  21779. goto done;
  21780. }
  21781. ret = wc_ecc_cmp_point(point3, point4);
  21782. if (ret != MP_EQ) {
  21783. ret = -10031;
  21784. goto done;
  21785. }
  21786. #endif
  21787. done:
  21788. #ifdef HAVE_COMP_KEY
  21789. wc_ecc_del_point(point4);
  21790. wc_ecc_del_point(point3);
  21791. #endif
  21792. wc_ecc_del_point(point2);
  21793. wc_ecc_del_point(point);
  21794. return ret;
  21795. }
  21796. #endif /* !WOLFSSL_ATECC508A && HAVE_ECC_KEY_IMPORT && HAVE_ECC_KEY_EXPORT */
  21797. #if !defined(NO_SIG_WRAPPER) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  21798. static int ecc_sig_test(WC_RNG* rng, ecc_key* key)
  21799. {
  21800. int ret;
  21801. word32 sigSz;
  21802. int size;
  21803. byte out[ECC_MAX_SIG_SIZE];
  21804. byte in[] = TEST_STRING;
  21805. WOLFSSL_SMALL_STACK_STATIC const byte hash[] = {
  21806. 0xf2, 0x02, 0x95, 0x65, 0xcb, 0xf6, 0x2a, 0x59,
  21807. 0x39, 0x2c, 0x05, 0xff, 0x0e, 0x29, 0xaf, 0xfe,
  21808. 0x47, 0x33, 0x8c, 0x99, 0x8d, 0x58, 0x64, 0x83,
  21809. 0xa6, 0x58, 0x0a, 0x33, 0x0b, 0x84, 0x5f, 0x5f
  21810. };
  21811. word32 inLen = (word32)XSTRLEN((char*)in);
  21812. size = wc_ecc_sig_size(key);
  21813. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_ECC, key, sizeof(*key));
  21814. if (ret != size)
  21815. return -10040;
  21816. sigSz = (word32)ret;
  21817. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_ECC, in,
  21818. inLen, out, &sigSz, key, sizeof(*key), rng);
  21819. if (ret != 0)
  21820. return -10041;
  21821. TEST_SLEEP();
  21822. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_ECC, in,
  21823. inLen, out, sigSz, key, sizeof(*key));
  21824. if (ret != 0)
  21825. return -10042;
  21826. TEST_SLEEP();
  21827. sigSz = (word32)sizeof(out);
  21828. ret = wc_SignatureGenerateHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_ECC,
  21829. hash, (int)sizeof(hash), out, &sigSz, key, sizeof(*key), rng);
  21830. if (ret != 0)
  21831. return -10043;
  21832. TEST_SLEEP();
  21833. ret = wc_SignatureVerifyHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_ECC,
  21834. hash, (int)sizeof(hash), out, sigSz, key, sizeof(*key));
  21835. if (ret != 0)
  21836. return -10044;
  21837. TEST_SLEEP();
  21838. return 0;
  21839. }
  21840. #endif
  21841. #if defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT) && \
  21842. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  21843. static int ecc_exp_imp_test(ecc_key* key)
  21844. {
  21845. int ret;
  21846. int curve_id;
  21847. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21848. ecc_key *keyImp = (ecc_key *)XMALLOC(sizeof *keyImp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21849. #else
  21850. ecc_key keyImp[1];
  21851. #endif
  21852. byte priv[32];
  21853. word32 privLen;
  21854. byte pub[65*2];
  21855. word32 pubLen, pubLenX, pubLenY;
  21856. const char qx[] = "7a4e287890a1a47ad3457e52f2f76a83"
  21857. "ce46cbc947616d0cbaa82323818a793d";
  21858. const char qy[] = "eec4084f5b29ebf29c44cce3b3059610"
  21859. "922f8b30ea6e8811742ac7238fe87308";
  21860. const char d[] = "8c14b793cb19137e323a6d2e2a870bca"
  21861. "2e7a493ec1153b3a95feb8a4873f8d08";
  21862. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21863. if (keyImp == NULL)
  21864. ERROR_OUT(-10050, done);
  21865. #endif
  21866. wc_ecc_init_ex(keyImp, HEAP_HINT, devId);
  21867. privLen = sizeof(priv);
  21868. ret = wc_ecc_export_private_only(key, priv, &privLen);
  21869. if (ret != 0) {
  21870. ret = -10051;
  21871. goto done;
  21872. }
  21873. pubLen = sizeof(pub);
  21874. ret = wc_ecc_export_point_der(key->idx, &key->pubkey, pub, &pubLen);
  21875. if (ret != 0) {
  21876. ret = -10052;
  21877. goto done;
  21878. }
  21879. ret = wc_ecc_import_private_key(priv, privLen, pub, pubLen, keyImp);
  21880. if (ret != 0) {
  21881. ret = -10053;
  21882. goto done;
  21883. }
  21884. wc_ecc_free(keyImp);
  21885. wc_ecc_init_ex(keyImp, HEAP_HINT, devId);
  21886. ret = wc_ecc_import_raw_ex(keyImp, qx, qy, d, ECC_SECP256R1);
  21887. if (ret != 0) {
  21888. ret = -10054;
  21889. goto done;
  21890. }
  21891. wc_ecc_free(keyImp);
  21892. wc_ecc_init_ex(keyImp, HEAP_HINT, devId);
  21893. curve_id = wc_ecc_get_curve_id(key->idx);
  21894. if (curve_id < 0) {
  21895. ret = -10055;
  21896. goto done;
  21897. }
  21898. /* test import private only */
  21899. ret = wc_ecc_import_private_key_ex(priv, privLen, NULL, 0, keyImp,
  21900. curve_id);
  21901. if (ret != 0) {
  21902. ret = -10056;
  21903. goto done;
  21904. }
  21905. wc_ecc_free(keyImp);
  21906. wc_ecc_init_ex(keyImp, HEAP_HINT, devId);
  21907. /* test export public raw */
  21908. pubLenX = pubLenY = 32;
  21909. ret = wc_ecc_export_public_raw(key, pub, &pubLenX, &pub[32], &pubLenY);
  21910. if (ret != 0) {
  21911. ret = -10057;
  21912. goto done;
  21913. }
  21914. #ifndef HAVE_SELFTEST
  21915. /* test import of public */
  21916. ret = wc_ecc_import_unsigned(keyImp, pub, &pub[32], NULL, ECC_SECP256R1);
  21917. if (ret != 0) {
  21918. ret = -10058;
  21919. goto done;
  21920. }
  21921. #endif
  21922. wc_ecc_free(keyImp);
  21923. wc_ecc_init_ex(keyImp, HEAP_HINT, devId);
  21924. /* test export private and public raw */
  21925. pubLenX = pubLenY = privLen = 32;
  21926. ret = wc_ecc_export_private_raw(key, pub, &pubLenX, &pub[32], &pubLenY,
  21927. priv, &privLen);
  21928. if (ret != 0) {
  21929. ret = -10059;
  21930. goto done;
  21931. }
  21932. #ifndef HAVE_SELFTEST
  21933. /* test import of private and public */
  21934. ret = wc_ecc_import_unsigned(keyImp, pub, &pub[32], priv, ECC_SECP256R1);
  21935. if (ret != 0) {
  21936. ret = -10060;
  21937. goto done;
  21938. }
  21939. #endif
  21940. done:
  21941. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21942. if (keyImp != NULL) {
  21943. wc_ecc_free(keyImp);
  21944. XFREE(keyImp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21945. }
  21946. #else
  21947. wc_ecc_free(keyImp);
  21948. #endif
  21949. return ret;
  21950. }
  21951. #endif /* HAVE_ECC_KEY_IMPORT && HAVE_ECC_KEY_EXPORT */
  21952. #if defined(HAVE_ECC_KEY_IMPORT) && !defined(WOLFSSL_VALIDATE_ECC_IMPORT) && \
  21953. !defined(WOLFSSL_CRYPTOCELL) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  21954. static int ecc_mulmod_test(ecc_key* key1)
  21955. {
  21956. int ret;
  21957. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21958. ecc_key *key2 = (ecc_key *)XMALLOC(sizeof *key2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21959. ecc_key *key3 = (ecc_key *)XMALLOC(sizeof *key3, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21960. #else
  21961. ecc_key key2[1];
  21962. ecc_key key3[1];
  21963. #endif
  21964. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21965. if ((key2 == NULL) || (key3 == NULL))
  21966. ERROR_OUT(MEMORY_E, done);
  21967. #endif
  21968. wc_ecc_init_ex(key2, HEAP_HINT, devId);
  21969. wc_ecc_init_ex(key3, HEAP_HINT, devId);
  21970. /* TODO: Use test data, test with WOLFSSL_VALIDATE_ECC_IMPORT. */
  21971. /* Need base point (Gx,Gy) and parameter A - load them as the public and
  21972. * private key in key2.
  21973. */
  21974. ret = wc_ecc_import_raw_ex(key2, key1->dp->Gx, key1->dp->Gy, key1->dp->Af,
  21975. ECC_SECP256R1);
  21976. if (ret != 0)
  21977. goto done;
  21978. /* Need a point (Gx,Gy) and prime - load them as the public and private key
  21979. * in key3.
  21980. */
  21981. ret = wc_ecc_import_raw_ex(key3, key1->dp->Gx, key1->dp->Gy,
  21982. key1->dp->prime, ECC_SECP256R1);
  21983. if (ret != 0)
  21984. goto done;
  21985. ret = wc_ecc_mulmod(&key1->k, &key2->pubkey, &key3->pubkey, &key2->k, &key3->k,
  21986. 1);
  21987. if (ret != 0) {
  21988. ret = -10070;
  21989. goto done;
  21990. }
  21991. done:
  21992. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21993. if (key2 != NULL) {
  21994. wc_ecc_free(key2);
  21995. XFREE(key2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21996. }
  21997. if (key3 != NULL) {
  21998. wc_ecc_free(key3);
  21999. XFREE(key3, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22000. }
  22001. #else
  22002. wc_ecc_free(key3);
  22003. wc_ecc_free(key2);
  22004. #endif
  22005. return ret;
  22006. }
  22007. #endif
  22008. #if defined(HAVE_ECC_DHE) && !defined(WC_NO_RNG) && \
  22009. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  22010. static int ecc_ssh_test(ecc_key* key, WC_RNG* rng)
  22011. {
  22012. int ret;
  22013. byte out[128];
  22014. word32 outLen = sizeof(out);
  22015. /* Parameter Validation testing. */
  22016. ret = wc_ecc_shared_secret_ssh(NULL, &key->pubkey, out, &outLen);
  22017. if (ret != BAD_FUNC_ARG)
  22018. return -10080;
  22019. ret = wc_ecc_shared_secret_ssh(key, NULL, out, &outLen);
  22020. if (ret != BAD_FUNC_ARG)
  22021. return -10081;
  22022. ret = wc_ecc_shared_secret_ssh(key, &key->pubkey, NULL, &outLen);
  22023. if (ret != BAD_FUNC_ARG)
  22024. return -10082;
  22025. ret = wc_ecc_shared_secret_ssh(key, &key->pubkey, out, NULL);
  22026. if (ret != BAD_FUNC_ARG)
  22027. return -10083;
  22028. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  22029. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  22030. !defined(HAVE_SELFTEST)
  22031. ret = wc_ecc_set_rng(key, rng);
  22032. if (ret != 0)
  22033. return -10084;
  22034. #else
  22035. (void)rng;
  22036. #endif
  22037. /* Use API. */
  22038. ret = 0;
  22039. do {
  22040. #if defined(WOLFSSL_ASYNC_CRYPT)
  22041. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  22042. #endif
  22043. if (ret == 0)
  22044. ret = wc_ecc_shared_secret_ssh(key, &key->pubkey, out, &outLen);
  22045. } while (ret == WC_PENDING_E);
  22046. if (ret != 0)
  22047. return -10085;
  22048. TEST_SLEEP();
  22049. return 0;
  22050. }
  22051. #endif /* HAVE_ECC_DHE && !WC_NO_RNG */
  22052. static int ecc_def_curve_test(WC_RNG *rng)
  22053. {
  22054. int ret;
  22055. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22056. ecc_key *key = (ecc_key *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22057. #else
  22058. ecc_key key[1];
  22059. #endif
  22060. #if (defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT)) || \
  22061. (defined(HAVE_ECC_KEY_IMPORT) && !defined(WOLFSSL_VALIDATE_ECC_IMPORT))
  22062. word32 idx = 0;
  22063. #endif
  22064. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22065. if (key == NULL)
  22066. ERROR_OUT(MEMORY_E, done);
  22067. #endif
  22068. wc_ecc_init_ex(key, HEAP_HINT, devId);
  22069. /* Use API */
  22070. ret = wc_ecc_set_flags(NULL, 0);
  22071. if (ret != BAD_FUNC_ARG) {
  22072. ret = -10090;
  22073. goto done;
  22074. }
  22075. ret = wc_ecc_set_flags(key, 0);
  22076. if (ret != 0) {
  22077. ret = -10091;
  22078. goto done;
  22079. }
  22080. #ifndef WOLF_CRYPTO_CB_ONLY_ECC
  22081. #ifndef WC_NO_RNG
  22082. ret = wc_ecc_make_key(rng, ECC_KEYGEN_SIZE, key);
  22083. #if defined(WOLFSSL_ASYNC_CRYPT)
  22084. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  22085. #endif
  22086. if (ret != 0) {
  22087. goto done;
  22088. }
  22089. #ifndef NO_SIG_WRAPPER
  22090. ret = ecc_sig_test(rng, key);
  22091. if (ret < 0)
  22092. goto done;
  22093. #endif
  22094. TEST_SLEEP();
  22095. #if defined(HAVE_ECC_DHE) && !defined(WOLFSSL_CRYPTOCELL) && \
  22096. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  22097. ret = ecc_ssh_test(key, rng);
  22098. if (ret < 0)
  22099. goto done;
  22100. #endif
  22101. wc_ecc_free(key);
  22102. #else
  22103. (void)rng;
  22104. #endif /* !WC_NO_RNG */
  22105. #if (defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT)) || \
  22106. (defined(HAVE_ECC_KEY_IMPORT) && !defined(WOLFSSL_VALIDATE_ECC_IMPORT))
  22107. /* Use test ECC key - ensure real private "d" exists */
  22108. #ifdef USE_CERT_BUFFERS_256
  22109. ret = wc_EccPrivateKeyDecode(ecc_key_der_256, &idx, key,
  22110. sizeof_ecc_key_der_256);
  22111. #else
  22112. {
  22113. XFILE file = XFOPEN(eccKeyDerFile, "rb");
  22114. byte der[128];
  22115. word32 derSz;
  22116. if (!file) {
  22117. ERROR_OUT(-10093, done);
  22118. }
  22119. derSz = (word32)XFREAD(der, 1, sizeof(der), file);
  22120. XFCLOSE(file);
  22121. ret = wc_EccPrivateKeyDecode(der, &idx, key, derSz);
  22122. }
  22123. #endif
  22124. if (ret != 0) {
  22125. goto done;
  22126. }
  22127. #if defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT)
  22128. ret = ecc_exp_imp_test(key);
  22129. if (ret < 0)
  22130. goto done;
  22131. #endif
  22132. #if defined(HAVE_ECC_KEY_IMPORT) && !defined(WOLFSSL_VALIDATE_ECC_IMPORT) && \
  22133. !defined(WOLFSSL_CRYPTOCELL)
  22134. ret = ecc_mulmod_test(key);
  22135. if (ret < 0)
  22136. goto done;
  22137. #endif
  22138. #endif
  22139. #else
  22140. (void)rng;
  22141. (void)idx;
  22142. #endif /* WOLF_CRYPTO_CB_ONLY_ECC */
  22143. done:
  22144. wc_ecc_free(key);
  22145. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22146. if (key != NULL) {
  22147. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22148. }
  22149. #endif
  22150. return ret;
  22151. }
  22152. #endif /* !NO_ECC256 || HAVE_ALL_CURVES */
  22153. #if defined(WOLFSSL_CERT_EXT) && \
  22154. (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  22155. static int ecc_decode_test(void)
  22156. {
  22157. int ret;
  22158. word32 inSz;
  22159. word32 inOutIdx;
  22160. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22161. ecc_key *key = (ecc_key *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22162. #else
  22163. ecc_key key[1];
  22164. #endif
  22165. /* SECP256R1 OID: 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07 */
  22166. /* This is ecc_clikeypub_der_256. */
  22167. WOLFSSL_SMALL_STACK_STATIC const byte good[] = {
  22168. 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce,
  22169. 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d,
  22170. 0x03, 0x01, 0x07, 0x03, 0x42, 0x00, 0x04, 0x55, 0xbf, 0xf4,
  22171. 0x0f, 0x44, 0x50, 0x9a, 0x3d, 0xce, 0x9b, 0xb7, 0xf0, 0xc5,
  22172. 0x4d, 0xf5, 0x70, 0x7b, 0xd4, 0xec, 0x24, 0x8e, 0x19, 0x80,
  22173. 0xec, 0x5a, 0x4c, 0xa2, 0x24, 0x03, 0x62, 0x2c, 0x9b, 0xda,
  22174. 0xef, 0xa2, 0x35, 0x12, 0x43, 0x84, 0x76, 0x16, 0xc6, 0x56,
  22175. 0x95, 0x06, 0xcc, 0x01, 0xa9, 0xbd, 0xf6, 0x75, 0x1a, 0x42,
  22176. 0xf7, 0xbd, 0xa9, 0xb2, 0x36, 0x22, 0x5f, 0xc7, 0x5d, 0x7f,
  22177. 0xb4 };
  22178. WOLFSSL_SMALL_STACK_STATIC const byte badNoObjId[] = { 0x30, 0x08, 0x30, 0x06, 0x03, 0x04,
  22179. 0x00, 0x04, 0x01, 0x01 };
  22180. WOLFSSL_SMALL_STACK_STATIC const byte badOneObjId[] = { 0x30, 0x0a, 0x30, 0x08, 0x06, 0x00,
  22181. 0x03, 0x04, 0x00, 0x04, 0x01, 0x01 };
  22182. WOLFSSL_SMALL_STACK_STATIC const byte badObjId1Len[] = { 0x30, 0x0c, 0x30, 0x0a, 0x06, 0x09,
  22183. 0x06, 0x00, 0x03, 0x04, 0x00, 0x04, 0x01, 0x01 };
  22184. WOLFSSL_SMALL_STACK_STATIC const byte badObj2d1Len[] = { 0x30, 0x0c, 0x30, 0x0a, 0x06, 0x00,
  22185. 0x06, 0x07, 0x03, 0x04, 0x00, 0x04, 0x01, 0x01 };
  22186. WOLFSSL_SMALL_STACK_STATIC const byte badNotBitStr[] = { 0x30, 0x14, 0x30, 0x0b, 0x06, 0x00,
  22187. 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07,
  22188. 0x04, 0x04, 0x00, 0x04, 0x01, 0x01 };
  22189. WOLFSSL_SMALL_STACK_STATIC const byte badBitStrLen[] = { 0x30, 0x14, 0x30, 0x0b, 0x06, 0x00,
  22190. 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07,
  22191. 0x03, 0x05, 0x00, 0x04, 0x01, 0x01 };
  22192. WOLFSSL_SMALL_STACK_STATIC const byte badNoBitStrZero[] = { 0x30, 0x13, 0x30, 0x0a, 0x06, 0x00,
  22193. 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07,
  22194. 0x03, 0x03, 0x04, 0x01, 0x01 };
  22195. WOLFSSL_SMALL_STACK_STATIC const byte badPoint[] = { 0x30, 0x12, 0x30, 0x09, 0x06, 0x00,
  22196. 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07,
  22197. 0x03, 0x03, 0x00, 0x04, 0x01 };
  22198. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22199. if (key == NULL)
  22200. ERROR_OUT(MEMORY_E, done);
  22201. #endif
  22202. XMEMSET(key, 0, sizeof *key);
  22203. wc_ecc_init_ex(key, HEAP_HINT, devId);
  22204. inSz = sizeof(good);
  22205. ret = wc_EccPublicKeyDecode(NULL, &inOutIdx, key, inSz);
  22206. if (ret != BAD_FUNC_ARG) {
  22207. ret = -10100;
  22208. goto done;
  22209. }
  22210. ret = wc_EccPublicKeyDecode(good, NULL, key, inSz);
  22211. if (ret != BAD_FUNC_ARG) {
  22212. ret = -10101;
  22213. goto done;
  22214. }
  22215. ret = wc_EccPublicKeyDecode(good, &inOutIdx, NULL, inSz);
  22216. if (ret != BAD_FUNC_ARG) {
  22217. ret = -10102;
  22218. goto done;
  22219. }
  22220. ret = wc_EccPublicKeyDecode(good, &inOutIdx, key, 0);
  22221. if (ret != BAD_FUNC_ARG) {
  22222. ret = -10103;
  22223. goto done;
  22224. }
  22225. /* Change offset to produce bad input data. */
  22226. inOutIdx = 2;
  22227. inSz = sizeof(good) - inOutIdx;
  22228. ret = wc_EccPublicKeyDecode(good, &inOutIdx, key, inSz);
  22229. if (ret != ASN_PARSE_E) {
  22230. ret = -10104;
  22231. goto done;
  22232. }
  22233. inOutIdx = 4;
  22234. inSz = sizeof(good) - inOutIdx;
  22235. ret = wc_EccPublicKeyDecode(good, &inOutIdx, key, inSz);
  22236. if (ret != ASN_PARSE_E) {
  22237. ret = -10105;
  22238. goto done;
  22239. }
  22240. /* Bad data. */
  22241. inSz = sizeof(badNoObjId);
  22242. inOutIdx = 0;
  22243. ret = wc_EccPublicKeyDecode(badNoObjId, &inOutIdx, key, inSz);
  22244. if (ret != ASN_OBJECT_ID_E && ret != ASN_PARSE_E) {
  22245. ret = -10106;
  22246. goto done;
  22247. }
  22248. inSz = sizeof(badOneObjId);
  22249. inOutIdx = 0;
  22250. ret = wc_EccPublicKeyDecode(badOneObjId, &inOutIdx, key, inSz);
  22251. if (ret != ASN_OBJECT_ID_E && ret != ASN_PARSE_E) {
  22252. ret = -10107;
  22253. goto done;
  22254. }
  22255. inSz = sizeof(badObjId1Len);
  22256. inOutIdx = 0;
  22257. ret = wc_EccPublicKeyDecode(badObjId1Len, &inOutIdx, key, inSz);
  22258. if (ret != ASN_PARSE_E) {
  22259. ret = -10108;
  22260. goto done;
  22261. }
  22262. inSz = sizeof(badObj2d1Len);
  22263. inOutIdx = 0;
  22264. ret = wc_EccPublicKeyDecode(badObj2d1Len, &inOutIdx, key, inSz);
  22265. if (ret != ASN_PARSE_E) {
  22266. ret = -10109;
  22267. goto done;
  22268. }
  22269. inSz = sizeof(badNotBitStr);
  22270. inOutIdx = 0;
  22271. ret = wc_EccPublicKeyDecode(badNotBitStr, &inOutIdx, key, inSz);
  22272. if (ret != ASN_BITSTR_E && ret != ASN_PARSE_E) {
  22273. ret = -10110;
  22274. goto done;
  22275. }
  22276. inSz = sizeof(badBitStrLen);
  22277. inOutIdx = 0;
  22278. ret = wc_EccPublicKeyDecode(badBitStrLen, &inOutIdx, key, inSz);
  22279. if (ret != ASN_PARSE_E) {
  22280. ret = -10111;
  22281. goto done;
  22282. }
  22283. inSz = sizeof(badNoBitStrZero);
  22284. inOutIdx = 0;
  22285. ret = wc_EccPublicKeyDecode(badNoBitStrZero, &inOutIdx, key, inSz);
  22286. if (ret != ASN_EXPECT_0_E && ret != ASN_PARSE_E) {
  22287. ret = -10112;
  22288. goto done;
  22289. }
  22290. inSz = sizeof(badPoint);
  22291. inOutIdx = 0;
  22292. ret = wc_EccPublicKeyDecode(badPoint, &inOutIdx, key, inSz);
  22293. if (ret != ASN_ECC_KEY_E && ret != ASN_PARSE_E) {
  22294. ret = -10113;
  22295. goto done;
  22296. }
  22297. inSz = sizeof(good);
  22298. inOutIdx = 0;
  22299. ret = wc_EccPublicKeyDecode(good, &inOutIdx, key, inSz);
  22300. if (ret != 0) {
  22301. ret = -10114;
  22302. goto done;
  22303. }
  22304. done:
  22305. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22306. if (key != NULL) {
  22307. wc_ecc_free(key);
  22308. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22309. }
  22310. #else
  22311. wc_ecc_free(key);
  22312. #endif
  22313. return ret;
  22314. }
  22315. #endif /* WOLFSSL_CERT_EXT */
  22316. #ifdef WOLFSSL_CUSTOM_CURVES
  22317. static const byte eccKeyExplicitCurve[] = {
  22318. 0x30, 0x81, 0xf5, 0x30, 0x81, 0xae, 0x06, 0x07,
  22319. 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01, 0x30,
  22320. 0x81, 0xa2, 0x02, 0x01, 0x01, 0x30, 0x2c, 0x06,
  22321. 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x01, 0x01,
  22322. 0x02, 0x21, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff,
  22323. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  22324. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  22325. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0xff,
  22326. 0xff, 0xfc, 0x2f, 0x30, 0x06, 0x04, 0x01, 0x00,
  22327. 0x04, 0x01, 0x07, 0x04, 0x41, 0x04, 0x79, 0xbe,
  22328. 0x66, 0x7e, 0xf9, 0xdc, 0xbb, 0xac, 0x55, 0xa0,
  22329. 0x62, 0x95, 0xce, 0x87, 0x0b, 0x07, 0x02, 0x9b,
  22330. 0xfc, 0xdb, 0x2d, 0xce, 0x28, 0xd9, 0x59, 0xf2,
  22331. 0x81, 0x5b, 0x16, 0xf8, 0x17, 0x98, 0x48, 0x3a,
  22332. 0xda, 0x77, 0x26, 0xa3, 0xc4, 0x65, 0x5d, 0xa4,
  22333. 0xfb, 0xfc, 0x0e, 0x11, 0x08, 0xa8, 0xfd, 0x17,
  22334. 0xb4, 0x48, 0xa6, 0x85, 0x54, 0x19, 0x9c, 0x47,
  22335. 0xd0, 0x8f, 0xfb, 0x10, 0xd4, 0xb8, 0x02, 0x21,
  22336. 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  22337. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  22338. 0xfe, 0xba, 0xae, 0xdc, 0xe6, 0xaf, 0x48, 0xa0,
  22339. 0x3b, 0xbf, 0xd2, 0x5e, 0x8c, 0xd0, 0x36, 0x41,
  22340. 0x41, 0x02, 0x01, 0x01, 0x03, 0x42, 0x00, 0x04,
  22341. 0x3c, 0x4c, 0xc9, 0x5e, 0x2e, 0xa2, 0x3d, 0x49,
  22342. 0xcc, 0x5b, 0xff, 0x4f, 0xc9, 0x2e, 0x1d, 0x4a,
  22343. 0xc6, 0x21, 0xf6, 0xf3, 0xe6, 0x0b, 0x4f, 0xa9,
  22344. 0x9d, 0x74, 0x99, 0xdd, 0x97, 0xc7, 0x6e, 0xbe,
  22345. 0x14, 0x2b, 0x39, 0x9d, 0x63, 0xc7, 0x97, 0x0d,
  22346. 0x45, 0x25, 0x40, 0x30, 0x77, 0x05, 0x76, 0x88,
  22347. 0x38, 0x96, 0x29, 0x7d, 0x9c, 0xe1, 0x50, 0xbe,
  22348. 0xac, 0xf0, 0x1d, 0x86, 0xf4, 0x2f, 0x65, 0x0b
  22349. };
  22350. static int ecc_test_custom_curves(WC_RNG* rng)
  22351. {
  22352. int ret;
  22353. word32 inOutIdx;
  22354. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22355. ecc_key *key = (ecc_key *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22356. #else
  22357. ecc_key key[1];
  22358. #endif
  22359. /* test use of custom curve - using BRAINPOOLP256R1 for test */
  22360. #if defined(HAVE_ECC_BRAINPOOL) && !defined(HAVE_INTEL_QA)
  22361. #ifndef WOLFSSL_ECC_CURVE_STATIC
  22362. WOLFSSL_SMALL_STACK_STATIC const ecc_oid_t ecc_oid_brainpoolp256r1[] = {
  22363. 0x2B,0x24,0x03,0x03,0x02,0x08,0x01,0x01,0x07
  22364. };
  22365. #define ecc_oid_brainpoolp256r1_sz \
  22366. (sizeof(ecc_oid_brainpoolp256r1) / sizeof(ecc_oid_t))
  22367. #else
  22368. #define ecc_oid_brainpoolp256r1 { \
  22369. 0x2B,0x24,0x03,0x03,0x02,0x08,0x01,0x01,0x07 \
  22370. }
  22371. #define ecc_oid_brainpoolp256r1_sz 9
  22372. #endif
  22373. #define ecc_oid_brainpoolp256r1_sum 104
  22374. WOLFSSL_SMALL_STACK_STATIC const ecc_set_type ecc_dp_brainpool256r1 = {
  22375. 32, /* size/bytes */
  22376. ECC_CURVE_CUSTOM, /* ID */
  22377. "BRAINPOOLP256R1", /* curve name */
  22378. "A9FB57DBA1EEA9BC3E660A909D838D726E3BF623D52620282013481D1F6E5377", /* prime */
  22379. "7D5A0975FC2C3057EEF67530417AFFE7FB8055C126DC5C6CE94A4B44F330B5D9", /* A */
  22380. "26DC5C6CE94A4B44F330B5D9BBD77CBF958416295CF7E1CE6BCCDC18FF8C07B6", /* B */
  22381. "A9FB57DBA1EEA9BC3E660A909D838D718C397AA3B561A6F7901E0E82974856A7", /* order */
  22382. "8BD2AEB9CB7E57CB2C4B482FFC81B7AFB9DE27E1E3BD23C23A4453BD9ACE3262", /* Gx */
  22383. "547EF835C3DAC4FD97F8461A14611DC9C27745132DED8E545C1D54C72F046997", /* Gy */
  22384. ecc_oid_brainpoolp256r1, /* oid/oidSz */
  22385. ecc_oid_brainpoolp256r1_sz,
  22386. ecc_oid_brainpoolp256r1_sum, /* oid sum */
  22387. 1, /* cofactor */
  22388. };
  22389. #endif /* HAVE_ECC_BRAINPOOL */
  22390. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22391. if (! key) {
  22392. ret = MEMORY_E;
  22393. goto done;
  22394. }
  22395. #endif
  22396. XMEMSET(key, 0, sizeof *key);
  22397. #if defined(HAVE_ECC_BRAINPOOL) && !defined(HAVE_INTEL_QA)
  22398. ret = ecc_test_curve_size(rng, 0, ECC_TEST_VERIFY_COUNT, ECC_CURVE_DEF,
  22399. &ecc_dp_brainpool256r1);
  22400. if (ret != 0) {
  22401. printf("ECC test for custom curve failed! %d\n", ret);
  22402. goto done;
  22403. }
  22404. #endif
  22405. #if defined(HAVE_ECC_BRAINPOOL) || defined(HAVE_ECC_KOBLITZ)
  22406. {
  22407. int curve_id;
  22408. #ifdef HAVE_ECC_BRAINPOOL
  22409. curve_id = ECC_BRAINPOOLP256R1;
  22410. #else
  22411. curve_id = ECC_SECP256K1;
  22412. #endif
  22413. /* Test and demonstrate use of non-SECP curve */
  22414. ret = ecc_test_curve_size(rng, 0, ECC_TEST_VERIFY_COUNT, curve_id, NULL);
  22415. if (ret < 0) {
  22416. printf("ECC test for curve_id %d failed! %d\n", curve_id, ret);
  22417. goto done;
  22418. }
  22419. }
  22420. #endif
  22421. ret = wc_ecc_init_ex(key, HEAP_HINT, devId);
  22422. if (ret != 0) {
  22423. ret = -10120;
  22424. goto done;
  22425. }
  22426. inOutIdx = 0;
  22427. ret = wc_EccPublicKeyDecode(eccKeyExplicitCurve, &inOutIdx, key,
  22428. sizeof(eccKeyExplicitCurve));
  22429. if (ret != 0)
  22430. ret = -10121;
  22431. done:
  22432. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22433. if (key) {
  22434. wc_ecc_free(key);
  22435. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22436. }
  22437. #else
  22438. wc_ecc_free(key);
  22439. #endif
  22440. (void)rng;
  22441. return ret;
  22442. }
  22443. #endif /* WOLFSSL_CUSTOM_CURVES */
  22444. #ifdef WOLFSSL_CERT_GEN
  22445. /* Make Cert / Sign example for ECC cert and ECC CA */
  22446. static int ecc_test_cert_gen(WC_RNG* rng)
  22447. {
  22448. int ret;
  22449. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22450. Cert *myCert = (Cert *)XMALLOC(sizeof *myCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22451. #ifdef WOLFSSL_TEST_CERT
  22452. DecodedCert *decode = (DecodedCert *)XMALLOC(sizeof *decode, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22453. #endif
  22454. ecc_key *caEccKey = (ecc_key *)XMALLOC(sizeof *caEccKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22455. ecc_key *certPubKey = (ecc_key *)XMALLOC(sizeof *certPubKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22456. #else
  22457. Cert myCert[1];
  22458. #ifdef WOLFSSL_TEST_CERT
  22459. DecodedCert decode[1];
  22460. #endif
  22461. ecc_key caEccKey[1];
  22462. ecc_key certPubKey[1];
  22463. #endif
  22464. int certSz;
  22465. size_t bytes;
  22466. word32 idx = 0;
  22467. #ifndef USE_CERT_BUFFERS_256
  22468. XFILE file;
  22469. #endif
  22470. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22471. byte* der = NULL;
  22472. #else
  22473. byte der[FOURK_BUF];
  22474. #endif
  22475. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22476. if ((myCert == NULL)
  22477. #ifdef WOLFSSL_TEST_CERT
  22478. || (decode == NULL)
  22479. #endif
  22480. || (caEccKey == NULL) || (certPubKey == NULL))
  22481. ERROR_OUT(MEMORY_E, exit);
  22482. #endif
  22483. XMEMSET(caEccKey, 0, sizeof *caEccKey);
  22484. XMEMSET(certPubKey, 0, sizeof *certPubKey);
  22485. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22486. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22487. if (der == NULL) {
  22488. ERROR_OUT(-10130, exit);
  22489. }
  22490. #endif
  22491. /* Get cert private key */
  22492. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  22493. /* Get Cert Key 384 */
  22494. #ifdef USE_CERT_BUFFERS_256
  22495. XMEMCPY(der, ca_ecc_key_der_384, sizeof_ca_ecc_key_der_384);
  22496. bytes = sizeof_ca_ecc_key_der_384;
  22497. #else
  22498. file = XFOPEN(eccCaKey384File, "rb");
  22499. if (!file) {
  22500. ERROR_OUT(-10131, exit);
  22501. }
  22502. bytes = XFREAD(der, 1, FOURK_BUF, file);
  22503. XFCLOSE(file);
  22504. (void)eccCaKeyFile;
  22505. #endif /* USE_CERT_BUFFERS_256 */
  22506. #else
  22507. #ifdef USE_CERT_BUFFERS_256
  22508. XMEMCPY(der, ca_ecc_key_der_256, sizeof_ca_ecc_key_der_256);
  22509. bytes = sizeof_ca_ecc_key_der_256;
  22510. #else
  22511. file = XFOPEN(eccCaKeyFile, "rb");
  22512. if (!file) {
  22513. ERROR_OUT(-10132, exit);
  22514. }
  22515. bytes = XFREAD(der, 1, FOURK_BUF, file);
  22516. XFCLOSE(file);
  22517. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  22518. (void)eccCaKey384File;
  22519. #endif
  22520. #endif /* USE_CERT_BUFFERS_256 */
  22521. #endif /* ENABLE_ECC384_CERT_GEN_TEST */
  22522. /* Get CA Key */
  22523. ret = wc_ecc_init_ex(caEccKey, HEAP_HINT, devId);
  22524. if (ret != 0) {
  22525. ERROR_OUT(-10133, exit);
  22526. }
  22527. ret = wc_EccPrivateKeyDecode(der, &idx, caEccKey, (word32)bytes);
  22528. if (ret != 0) {
  22529. ERROR_OUT(-10134, exit);
  22530. }
  22531. /* Make a public key */
  22532. ret = wc_ecc_init_ex(certPubKey, HEAP_HINT, devId);
  22533. if (ret != 0) {
  22534. ERROR_OUT(-10135, exit);
  22535. }
  22536. ret = wc_ecc_make_key(rng, ECC_KEYGEN_SIZE, certPubKey);
  22537. #if defined(WOLFSSL_ASYNC_CRYPT)
  22538. ret = wc_AsyncWait(ret, &certPubKey->asyncDev, WC_ASYNC_FLAG_NONE);
  22539. #endif
  22540. if (ret != 0) {
  22541. ERROR_OUT(-10136, exit);
  22542. }
  22543. TEST_SLEEP();
  22544. /* Setup Certificate */
  22545. if (wc_InitCert_ex(myCert, HEAP_HINT, devId)) {
  22546. ERROR_OUT(-10137, exit);
  22547. }
  22548. #ifndef NO_SHA256
  22549. myCert->sigType = CTC_SHA256wECDSA;
  22550. #else
  22551. myCert->sigType = CTC_SHAwECDSA;
  22552. #endif
  22553. XMEMCPY(&myCert->subject, &certDefaultName, sizeof(CertName));
  22554. #ifdef WOLFSSL_CERT_EXT
  22555. /* add Policies */
  22556. XSTRNCPY(myCert->certPolicies[0], "2.4.589440.587.101.2.1.9632587.1",
  22557. CTC_MAX_CERTPOL_SZ);
  22558. XSTRNCPY(myCert->certPolicies[1], "1.2.13025.489.1.113549",
  22559. CTC_MAX_CERTPOL_SZ);
  22560. myCert->certPoliciesNb = 2;
  22561. /* add SKID from the Public Key */
  22562. if (wc_SetSubjectKeyIdFromPublicKey(myCert, NULL, certPubKey) != 0) {
  22563. ERROR_OUT(-10138, exit);
  22564. }
  22565. /* add AKID from the Public Key */
  22566. if (wc_SetAuthKeyIdFromPublicKey(myCert, NULL, caEccKey) != 0) {
  22567. ERROR_OUT(-10139, exit);
  22568. }
  22569. /* add Key Usage */
  22570. if (wc_SetKeyUsage(myCert, certKeyUsage) != 0) {
  22571. ERROR_OUT(-10140, exit);
  22572. }
  22573. #endif /* WOLFSSL_CERT_EXT */
  22574. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  22575. #if defined(USE_CERT_BUFFERS_256)
  22576. ret = wc_SetIssuerBuffer(myCert, ca_ecc_cert_der_384,
  22577. sizeof_ca_ecc_cert_der_384);
  22578. #else
  22579. ret = wc_SetIssuer(myCert, eccCaCert384File);
  22580. (void)eccCaCertFile;
  22581. #endif
  22582. #else
  22583. #if defined(USE_CERT_BUFFERS_256)
  22584. ret = wc_SetIssuerBuffer(myCert, ca_ecc_cert_der_256,
  22585. sizeof_ca_ecc_cert_der_256);
  22586. #else
  22587. ret = wc_SetIssuer(myCert, eccCaCertFile);
  22588. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  22589. (void)eccCaCert384File;
  22590. #endif
  22591. #endif
  22592. #endif /* ENABLE_ECC384_CERT_GEN_TEST */
  22593. if (ret < 0) {
  22594. ERROR_OUT(-10141, exit);
  22595. }
  22596. certSz = wc_MakeCert(myCert, der, FOURK_BUF, NULL, certPubKey, rng);
  22597. if (certSz < 0) {
  22598. ERROR_OUT(-10142, exit);
  22599. }
  22600. ret = 0;
  22601. do {
  22602. #if defined(WOLFSSL_ASYNC_CRYPT)
  22603. ret = wc_AsyncWait(ret, &caEccKey->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  22604. #endif
  22605. if (ret >= 0) {
  22606. ret = wc_SignCert(myCert->bodySz, myCert->sigType, der,
  22607. FOURK_BUF, NULL, caEccKey, rng);
  22608. }
  22609. } while (ret == WC_PENDING_E);
  22610. if (ret < 0) {
  22611. ERROR_OUT(-10143, exit);
  22612. }
  22613. certSz = ret;
  22614. TEST_SLEEP();
  22615. #ifdef WOLFSSL_TEST_CERT
  22616. InitDecodedCert(decode, der, certSz, HEAP_HINT);
  22617. ret = ParseCert(decode, CERT_TYPE, NO_VERIFY, 0);
  22618. if (ret != 0) {
  22619. FreeDecodedCert(decode);
  22620. ERROR_OUT(-10144, exit);
  22621. }
  22622. FreeDecodedCert(decode);
  22623. #endif
  22624. ret = SaveDerAndPem(der, certSz, certEccDerFile, certEccPemFile,
  22625. CERT_TYPE, -6735);
  22626. if (ret != 0) {
  22627. goto exit;
  22628. }
  22629. exit:
  22630. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22631. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22632. #endif
  22633. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22634. if (myCert != NULL)
  22635. XFREE(myCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22636. #ifdef WOLFSSL_TEST_CERT
  22637. if (decode != NULL)
  22638. XFREE(decode, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22639. #endif
  22640. if (caEccKey != NULL) {
  22641. wc_ecc_free(caEccKey);
  22642. XFREE(caEccKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22643. }
  22644. if (certPubKey != NULL) {
  22645. wc_ecc_free(certPubKey);
  22646. XFREE(certPubKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22647. }
  22648. #else
  22649. wc_ecc_free(certPubKey);
  22650. wc_ecc_free(caEccKey);
  22651. #endif
  22652. return ret;
  22653. }
  22654. #endif /* WOLFSSL_CERT_GEN */
  22655. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  22656. !defined(WOLFSSL_NO_MALLOC) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  22657. /* Test for the wc_ecc_key_new() and wc_ecc_key_free() functions. */
  22658. static int ecc_test_allocator(WC_RNG* rng)
  22659. {
  22660. int ret = 0;
  22661. ecc_key* key;
  22662. #ifdef WC_NO_RNG
  22663. word32 idx = 0;
  22664. #endif
  22665. key = wc_ecc_key_new(HEAP_HINT);
  22666. if (key == NULL) {
  22667. ERROR_OUT(-10150, exit);
  22668. }
  22669. #ifndef WC_NO_RNG
  22670. ret = wc_ecc_make_key(rng, ECC_KEYGEN_SIZE, key);
  22671. #if defined(WOLFSSL_ASYNC_CRYPT)
  22672. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  22673. #endif
  22674. if (ret != 0) {
  22675. ERROR_OUT(-10151, exit);
  22676. }
  22677. #else
  22678. /* use test ECC key */
  22679. ret = wc_EccPrivateKeyDecode(ecc_key_der_256, &idx, key,
  22680. (word32)sizeof_ecc_key_der_256);
  22681. (void)rng;
  22682. #endif
  22683. exit:
  22684. wc_ecc_key_free(key);
  22685. return ret;
  22686. }
  22687. #endif
  22688. /* ECC Non-blocking tests for Sign and Verify */
  22689. /* Requires SP math and supports P384 or P256 */
  22690. /* ./configure --enable-ecc=nonblock --enable-sp=yes,nonblock CFLAGS="-DWOLFSSL_PUBLIC_MP" */
  22691. #if defined(WC_ECC_NONBLOCK) && defined(WOLFSSL_HAVE_SP_ECC) && \
  22692. defined(WOLFSSL_PUBLIC_MP)
  22693. /* ECC Private Key "d" */
  22694. static const byte p256PrivKey[] = {
  22695. /* SECP256R1 */
  22696. /* d */
  22697. 0x1e, 0xe7, 0x70, 0x07, 0xd3, 0x30, 0x94, 0x39,
  22698. 0x28, 0x90, 0xdf, 0x23, 0x88, 0x2c, 0x4a, 0x34,
  22699. 0x15, 0xdb, 0x4c, 0x43, 0xcd, 0xfa, 0xe5, 0x1f,
  22700. 0x3d, 0x4c, 0x37, 0xfe, 0x59, 0x3b, 0x96, 0xd8
  22701. };
  22702. #ifdef HAVE_ECC384
  22703. static const byte p384PrivKey[] = {
  22704. /* SECP384R1 */
  22705. /* d */
  22706. 0xa4, 0xe5, 0x06, 0xe8, 0x06, 0x16, 0x3e, 0xab,
  22707. 0x89, 0xf8, 0x60, 0x43, 0xc0, 0x60, 0x25, 0xdb,
  22708. 0xba, 0x7b, 0xfe, 0x19, 0x35, 0x08, 0x55, 0x65,
  22709. 0x76, 0xe2, 0xdc, 0xe0, 0x01, 0x8b, 0x6b, 0x68,
  22710. 0xdf, 0xcf, 0x6f, 0x80, 0x12, 0xce, 0x79, 0x37,
  22711. 0xeb, 0x2b, 0x9c, 0x7b, 0xc4, 0x68, 0x1c, 0x74
  22712. };
  22713. #endif /* HAVE_ECC384 */
  22714. #ifdef HAVE_ECC521
  22715. static const byte p521PrivKey[] = {
  22716. /* SECP521R1 */
  22717. /* d */
  22718. 0x01, 0x68, 0x91, 0x33, 0x53, 0xe2, 0x90, 0x68,
  22719. 0x11, 0x8f, 0xaa, 0xa8, 0x76, 0x0c, 0xf7, 0x2a,
  22720. 0x07, 0x1b, 0x92, 0x2a, 0xa7, 0x82, 0x3d, 0xfa,
  22721. 0x83, 0xce, 0x70, 0xc8, 0xc2, 0x60, 0x82, 0xfe,
  22722. 0x18, 0x88, 0x68, 0xda, 0x6a, 0x83, 0x46, 0x78,
  22723. 0xe4, 0xe9, 0xe9, 0xcc, 0x51, 0x7f, 0xed, 0x81,
  22724. 0x02, 0x32, 0xee, 0x26, 0x87, 0xcc, 0xed, 0x63,
  22725. 0x3f, 0x39, 0x27, 0xf0, 0xd7, 0x17, 0x77, 0xa1,
  22726. 0xa4, 0x36
  22727. };
  22728. #endif /* HAVE_ECC521 */
  22729. /* ECC public key Qx/Qy */
  22730. static const byte p256PubKey[] = {
  22731. /* SECP256R1 */
  22732. /* Qx */
  22733. 0x96, 0x93, 0x1c, 0x53, 0x0b, 0x43, 0x6c, 0x42,
  22734. 0x0c, 0x52, 0x90, 0xe4, 0xa7, 0xec, 0x98, 0xb1,
  22735. 0xaf, 0xd4, 0x14, 0x49, 0xd8, 0xc1, 0x42, 0x82,
  22736. 0x04, 0x78, 0xd1, 0x90, 0xae, 0xa0, 0x6c, 0x07,
  22737. /* Qy */
  22738. 0xf2, 0x3a, 0xb5, 0x10, 0x32, 0x8d, 0xce, 0x9e,
  22739. 0x76, 0xa0, 0xd2, 0x8c, 0xf3, 0xfc, 0xa9, 0x94,
  22740. 0x43, 0x24, 0xe6, 0x82, 0x00, 0x40, 0xc6, 0xdb,
  22741. 0x1c, 0x2f, 0xcd, 0x38, 0x4b, 0x60, 0xdd, 0x61
  22742. };
  22743. #ifdef HAVE_ECC384
  22744. static const byte p384PubKey[] = {
  22745. /* SECP384R1 */
  22746. /* Qx */
  22747. 0xea, 0xcf, 0x93, 0x4f, 0x2c, 0x09, 0xbb, 0x39,
  22748. 0x14, 0x0f, 0x56, 0x64, 0xc3, 0x40, 0xb4, 0xdf,
  22749. 0x0e, 0x63, 0xae, 0xe5, 0x71, 0x4b, 0x00, 0xcc,
  22750. 0x04, 0x97, 0xff, 0xe1, 0xe9, 0x38, 0x96, 0xbb,
  22751. 0x5f, 0x91, 0xb2, 0x6a, 0xcc, 0xb5, 0x39, 0x5f,
  22752. 0x8f, 0x70, 0x59, 0xf1, 0x01, 0xf6, 0x5a, 0x2b,
  22753. /* Qy */
  22754. 0x01, 0x6c, 0x68, 0x0b, 0xcf, 0x55, 0x25, 0xaf,
  22755. 0x6d, 0x98, 0x48, 0x0a, 0xa8, 0x74, 0xc9, 0xa9,
  22756. 0x17, 0xa0, 0x0c, 0xc3, 0xfb, 0xd3, 0x23, 0x68,
  22757. 0xfe, 0x04, 0x3c, 0x63, 0x50, 0x88, 0x3b, 0xb9,
  22758. 0x4f, 0x7c, 0x67, 0x34, 0xf7, 0x3b, 0xa9, 0x73,
  22759. 0xe7, 0x1b, 0xc3, 0x51, 0x5e, 0x22, 0x18, 0xec
  22760. };
  22761. #endif
  22762. #ifdef HAVE_ECC521
  22763. static const byte p521PubKey[] = {
  22764. /* SECP521R1 */
  22765. /* Qx */
  22766. 0x01, 0x62, 0x6e, 0xf1, 0x00, 0xec, 0xd8, 0x99,
  22767. 0x58, 0x9b, 0x80, 0x6b, 0xfe, 0x2c, 0xf1, 0xb2,
  22768. 0xf0, 0xc8, 0x48, 0xdf, 0xac, 0xd2, 0x3b, 0x71,
  22769. 0x29, 0xab, 0xf0, 0x66, 0x63, 0xd8, 0x8e, 0xb5,
  22770. 0xc8, 0xc2, 0xfc, 0x99, 0x44, 0xe2, 0x45, 0xb1,
  22771. 0x5a, 0x7b, 0xb9, 0x73, 0x01, 0xda, 0x79, 0xec,
  22772. 0x9c, 0x26, 0x27, 0x34, 0x45, 0x26, 0xd5, 0x89,
  22773. 0x4b, 0x44, 0xfe, 0x69, 0x4e, 0x72, 0x14, 0xe3,
  22774. 0x8b, 0xbc,
  22775. /* Qy */
  22776. 0x00, 0x0f, 0x09, 0xa2, 0x03, 0xc3, 0x5a, 0xdc,
  22777. 0x95, 0x82, 0xf6, 0xf9, 0xf6, 0x9c, 0xff, 0xb5,
  22778. 0x6b, 0x75, 0x95, 0x4b, 0xa4, 0x28, 0x5d, 0x9e,
  22779. 0x90, 0x04, 0xd1, 0xc0, 0x1e, 0xd5, 0xfd, 0x43,
  22780. 0x9e, 0x1e, 0x83, 0xc0, 0x11, 0x2b, 0x2b, 0x07,
  22781. 0x6d, 0xa9, 0x7a, 0x10, 0xd7, 0x67, 0xe7, 0x51,
  22782. 0x37, 0x24, 0xd8, 0xbf, 0x03, 0x0d, 0x8b, 0xb5,
  22783. 0x40, 0x5c, 0x4f, 0xd6, 0x13, 0x73, 0x42, 0xbc,
  22784. 0x91, 0xd9
  22785. };
  22786. /* perform verify of signature and hash using public key */
  22787. /* key is public Qx + public Qy */
  22788. /* sig is r + s */
  22789. static int crypto_ecc_verify(const byte *key, uint32_t keySz,
  22790. const byte *hash, uint32_t hashSz, const byte *sig, uint32_t sigSz,
  22791. uint32_t curveSz, int curveId)
  22792. {
  22793. int ret, verify_res = 0, count = 0;
  22794. mp_int r, s;
  22795. ecc_key ecc;
  22796. ecc_nb_ctx_t nb_ctx;
  22797. /* validate arguments */
  22798. if (key == NULL || hash == NULL || sig == NULL || curveSz == 0 ||
  22799. hashSz == 0 || keySz < (curveSz*2) || sigSz < (curveSz*2))
  22800. {
  22801. return BAD_FUNC_ARG;
  22802. }
  22803. /* Setup the ECC key */
  22804. ret = wc_ecc_init(&ecc);
  22805. if (ret < 0) {
  22806. return ret;
  22807. }
  22808. ret = wc_ecc_set_nonblock(&ecc, &nb_ctx);
  22809. if (ret != MP_OKAY) {
  22810. wc_ecc_free(&ecc);
  22811. return ret;
  22812. }
  22813. /* Setup the signature r/s variables */
  22814. ret = mp_init(&r);
  22815. if (ret != MP_OKAY) {
  22816. wc_ecc_free(&ecc);
  22817. return ret;
  22818. }
  22819. ret = mp_init(&s);
  22820. if (ret != MP_OKAY) {
  22821. mp_clear(&r);
  22822. wc_ecc_free(&ecc);
  22823. return ret;
  22824. }
  22825. /* Import public key x/y */
  22826. ret = wc_ecc_import_unsigned(
  22827. &ecc,
  22828. (byte*)key, /* Public "x" Coordinate */
  22829. (byte*)(key + curveSz), /* Public "y" Coordinate */
  22830. NULL, /* Private "d" (optional) */
  22831. curveId /* ECC Curve Id */
  22832. );
  22833. /* Make sure it was a public key imported */
  22834. if (ret == 0 && ecc.type != ECC_PUBLICKEY) {
  22835. ret = ECC_BAD_ARG_E;
  22836. }
  22837. /* Import signature r/s */
  22838. if (ret == 0) {
  22839. ret = mp_read_unsigned_bin(&r, sig, curveSz);
  22840. }
  22841. if (ret == 0) {
  22842. ret = mp_read_unsigned_bin(&s, sig + curveSz, curveSz);
  22843. }
  22844. /* Verify ECC Signature */
  22845. if (ret == 0) {
  22846. do {
  22847. ret = wc_ecc_verify_hash_ex(
  22848. &r, &s, /* r/s as mp_int */
  22849. hash, hashSz, /* computed hash digest */
  22850. &verify_res, /* verification result 1=success */
  22851. &ecc
  22852. );
  22853. count++;
  22854. /* This is where real-time work could be called */
  22855. } while (ret == FP_WOULDBLOCK);
  22856. #ifdef DEBUG_WOLFSSL
  22857. printf("ECC non-block verify: %d times\n", count);
  22858. #endif
  22859. }
  22860. /* check verify result */
  22861. if (ret == 0 && verify_res == 0) {
  22862. ret = SIG_VERIFY_E;
  22863. }
  22864. mp_clear(&r);
  22865. mp_clear(&s);
  22866. wc_ecc_free(&ecc);
  22867. (void)count;
  22868. return ret;
  22869. }
  22870. /* perform signature operation against hash using private key */
  22871. static int crypto_ecc_sign(const byte *key, uint32_t keySz,
  22872. const byte *hash, uint32_t hashSz, byte *sig, uint32_t* sigSz,
  22873. uint32_t curveSz, int curveId, WC_RNG* rng)
  22874. {
  22875. int ret, count = 0;
  22876. mp_int r, s;
  22877. ecc_key ecc;
  22878. ecc_nb_ctx_t nb_ctx;
  22879. /* validate arguments */
  22880. if (key == NULL || hash == NULL || sig == NULL || sigSz == NULL ||
  22881. curveSz == 0 || hashSz == 0 || keySz < curveSz || *sigSz < (curveSz*2))
  22882. {
  22883. return BAD_FUNC_ARG;
  22884. }
  22885. /* Initialize signature result */
  22886. memset(sig, 0, curveSz*2);
  22887. /* Setup the ECC key */
  22888. ret = wc_ecc_init(&ecc);
  22889. if (ret < 0) {
  22890. return ret;
  22891. }
  22892. ret = wc_ecc_set_nonblock(&ecc, &nb_ctx);
  22893. if (ret != MP_OKAY) {
  22894. wc_ecc_free(&ecc);
  22895. return ret;
  22896. }
  22897. /* Setup the signature r/s variables */
  22898. ret = mp_init(&r);
  22899. if (ret != MP_OKAY) {
  22900. wc_ecc_free(&ecc);
  22901. return ret;
  22902. }
  22903. ret = mp_init(&s);
  22904. if (ret != MP_OKAY) {
  22905. mp_clear(&r);
  22906. wc_ecc_free(&ecc);
  22907. return ret;
  22908. }
  22909. /* Import private key "k" */
  22910. ret = wc_ecc_import_private_key_ex(
  22911. key, keySz, /* private key "d" */
  22912. NULL, 0, /* public (optional) */
  22913. &ecc,
  22914. curveId /* ECC Curve Id */
  22915. );
  22916. if (ret == 0) {
  22917. do {
  22918. /* Verify ECC Signature */
  22919. ret = wc_ecc_sign_hash_ex(
  22920. hash, hashSz, /* computed hash digest */
  22921. rng, &ecc, /* random and key context */
  22922. &r, &s /* r/s as mp_int */
  22923. );
  22924. count++;
  22925. /* This is where real-time work could be called */
  22926. } while (ret == FP_WOULDBLOCK);
  22927. #ifdef DEBUG_WOLFSSL
  22928. printf("ECC non-block sign: %d times\n", count);
  22929. #endif
  22930. }
  22931. if (ret == 0) {
  22932. /* export r/s */
  22933. mp_to_unsigned_bin_len(&r, sig, curveSz);
  22934. mp_to_unsigned_bin_len(&s, sig + curveSz, curveSz);
  22935. }
  22936. mp_clear(&r);
  22937. mp_clear(&s);
  22938. wc_ecc_free(&ecc);
  22939. (void)count;
  22940. return ret;
  22941. }
  22942. #endif /* HAVE_ECC_SIGN && HAVE_ECC_VERIFY */
  22943. /*
  22944. * This test doesn't work with WOLFSSL_VALIDATE_ECC_KEYGEN defined because we
  22945. * don't have non-blocking versions of the key checking functions, yet.
  22946. */
  22947. #if defined(HAVE_ECC_DHE) && !defined(WOLFSSL_VALIDATE_ECC_KEYGEN)
  22948. static int ecc_test_nonblock_dhe(int curveId, word32 curveSz,
  22949. const byte* privKey, const byte* pubKey, WC_RNG* rng)
  22950. {
  22951. int ret;
  22952. ecc_key keyA;
  22953. ecc_key keyB;
  22954. ecc_nb_ctx_t nbCtxA;
  22955. ecc_nb_ctx_t nbCtxB;
  22956. byte secretA[ECC_SHARED_SIZE];
  22957. byte secretB[ECC_SHARED_SIZE];
  22958. word32 secretSzA = ECC_SHARED_SIZE;
  22959. word32 secretSzB = ECC_SHARED_SIZE;
  22960. int count = 0;
  22961. ret = wc_ecc_init(&keyA);
  22962. if (ret == 0) {
  22963. ret = wc_ecc_init(&keyB);
  22964. }
  22965. if (ret == 0) {
  22966. ret = wc_ecc_set_nonblock(&keyA, &nbCtxA);
  22967. }
  22968. if (ret == 0) {
  22969. ret = wc_ecc_set_nonblock(&keyB, &nbCtxB);
  22970. }
  22971. if (ret == 0) {
  22972. do {
  22973. ret = wc_ecc_make_key_ex(rng, curveSz, &keyA, curveId);
  22974. count++;
  22975. } while (ret == FP_WOULDBLOCK);
  22976. }
  22977. #ifdef DEBUG_WOLFSSL
  22978. fprintf(stderr, "ECC non-block key gen: %d times\n", count);
  22979. #endif
  22980. if (ret == 0) {
  22981. ret = wc_ecc_check_key(&keyA);
  22982. }
  22983. if (ret == 0) {
  22984. ret = wc_ecc_import_unsigned(&keyB, pubKey, pubKey + curveSz,
  22985. privKey, curveId);
  22986. }
  22987. count = 0;
  22988. if (ret == 0) {
  22989. do {
  22990. ret = wc_ecc_shared_secret(&keyA, &keyB, secretA, &secretSzA);
  22991. count++;
  22992. } while (ret == FP_WOULDBLOCK);
  22993. }
  22994. #ifdef DEBUG_WOLFSSL
  22995. fprintf(stderr, "ECC non-block shared secret: %d times\n", count);
  22996. #endif
  22997. if (ret == 0) {
  22998. do {
  22999. ret = wc_ecc_shared_secret(&keyB, &keyA, secretB, &secretSzB);
  23000. } while (ret == FP_WOULDBLOCK);
  23001. }
  23002. if (ret == 0) {
  23003. if (secretSzA != secretSzB ||
  23004. XMEMCMP(secretA, secretB, secretSzA) != 0) {
  23005. ret = -1;
  23006. }
  23007. }
  23008. wc_ecc_free(&keyA);
  23009. wc_ecc_free(&keyB);
  23010. return ret;
  23011. }
  23012. #endif /* HAVE_ECC_DHE && !WOLFSSL_VALIDATE_ECC_KEYGEN */
  23013. #if defined(HAVE_ECC_SIGN) && defined(HAVE_ECC_VERIFY)
  23014. static int ecc_test_nonblock_ecdsa(int curveId, word32 curveSz,
  23015. const byte* privKey, word32 privKeySz, const byte* pubKey, word32 pubKeySz,
  23016. WC_RNG* rng)
  23017. {
  23018. int ret = 0;
  23019. byte* sig = NULL;
  23020. word32 sigSz = curveSz * 2;
  23021. static const byte hash[] = {
  23022. 0x8d, 0x28, 0xa3, 0x8b, 0x0b, 0xa9, 0xfe, 0xd4, 0x0e, 0x54, 0xc4, 0x17,
  23023. 0x3d, 0x54, 0x66, 0x34, 0xbf, 0x5d, 0x6f, 0x46, 0xc2, 0x20, 0xcb, 0xc3,
  23024. 0x22, 0xe9, 0xb0, 0xdf, 0xe7, 0x64, 0x3f, 0xd9
  23025. };
  23026. sig = (byte*)XMALLOC(sigSz, HEAP_HINT, DYNAMIC_TYPE_SIGNATURE);
  23027. if (sig == NULL) {
  23028. ret = -1;
  23029. }
  23030. if (ret == 0) {
  23031. /* Sign hash using private key */
  23032. /* Note: result of an ECC sign varies for each call even with same
  23033. private key and hash. This is because a new random public key is
  23034. used for each operation. */
  23035. ret = crypto_ecc_sign(privKey, privKeySz, hash, sizeof(hash), sig,
  23036. &sigSz, curveSz, curveId, rng);
  23037. }
  23038. if (ret == 0) {
  23039. /* Verify generated signature is valid */
  23040. ret = crypto_ecc_verify(pubKey, pubKeySz, hash, sizeof(hash), sig,
  23041. sigSz, curveSz, curveId);
  23042. }
  23043. if (sig != NULL) {
  23044. XFREE(sig, HEAP_HINT, DYNAMIC_TYPE_SIGNATURE);
  23045. }
  23046. return ret;
  23047. }
  23048. #endif /* HAVE_ECC_SIGN && HAVE_ECC_VERIFY */
  23049. static int ecc_test_nonblock(WC_RNG* rng)
  23050. {
  23051. int ret = 0;
  23052. word32 i;
  23053. int curveIds[3] = {0, 0, 0};
  23054. word32 curveSzs[3] = {0, 0, 0};
  23055. const byte* privKeys[3] = {NULL, NULL, NULL};
  23056. word32 privKeySzs[3] = {0, 0, 0};
  23057. const byte* pubKeys[3] = {NULL, NULL, NULL};
  23058. word32 pubKeySzs[3] = {0, 0, 0};
  23059. curveIds[0] = ECC_SECP256R1;
  23060. curveSzs[0] = 32;
  23061. privKeys[0] = p256PrivKey;
  23062. privKeySzs[0] = sizeof(p256PrivKey);
  23063. pubKeys[0] = p256PubKey;
  23064. pubKeySzs[0] = sizeof(p256PubKey);
  23065. #ifdef HAVE_ECC384
  23066. curveIds[1] = ECC_SECP384R1;
  23067. curveSzs[1] = 48;
  23068. privKeys[1] = p384PrivKey;
  23069. privKeySzs[1] = sizeof(p384PrivKey);
  23070. pubKeys[1] = p384PubKey;
  23071. pubKeySzs[1] = sizeof(p384PubKey);
  23072. #endif
  23073. #ifdef HAVE_ECC521
  23074. curveIds[2] = ECC_SECP521R1;
  23075. curveSzs[2] = 66;
  23076. privKeys[2] = p521PrivKey;
  23077. privKeySzs[2] = sizeof(p521PrivKey);
  23078. pubKeys[2] = p521PubKey;
  23079. pubKeySzs[2] = sizeof(p521PubKey);
  23080. #endif
  23081. for (i = 0; ret == 0 && i < sizeof(curveIds) / sizeof(curveIds[0]); ++i) {
  23082. if (curveIds[i] == 0) {
  23083. continue;
  23084. }
  23085. #if defined(HAVE_ECC_SIGN) && defined(HAVE_ECC_VERIFY)
  23086. ret = ecc_test_nonblock_ecdsa(curveIds[i], curveSzs[i], privKeys[i],
  23087. privKeySzs[i], pubKeys[i], pubKeySzs[i], rng);
  23088. #endif /* HAVE_ECC_SIGN && HAVE_ECC_VERIFY */
  23089. #if defined(HAVE_ECC_DHE) && !defined(WOLFSSL_VALIDATE_ECC_KEYGEN)
  23090. if (ret == 0) {
  23091. ret = ecc_test_nonblock_dhe(curveIds[i], curveSzs[i], privKeys[i],
  23092. pubKeys[i], rng);
  23093. }
  23094. #endif /* HAVE_ECC_DHE && !WOLFSSL_VALIDATE_ECC_KEYGEN */
  23095. }
  23096. return ret;
  23097. }
  23098. #endif /* WC_ECC_NONBLOCK && WOLFSSL_HAVE_SP_ECC && WOLFSSL_PUBLIC_MP */
  23099. WOLFSSL_TEST_SUBROUTINE int ecc_test(void)
  23100. {
  23101. int ret;
  23102. WC_RNG rng;
  23103. #if defined(WOLFSSL_CERT_EXT) && \
  23104. (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  23105. ret = ecc_decode_test();
  23106. if (ret < 0)
  23107. return ret;
  23108. #endif
  23109. #ifndef HAVE_FIPS
  23110. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  23111. #else
  23112. ret = wc_InitRng(&rng);
  23113. #endif
  23114. #ifndef WC_NO_RNG
  23115. if (ret != 0)
  23116. return -10300;
  23117. #else
  23118. (void)ret;
  23119. #endif
  23120. #if (defined(HAVE_ECC112) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 112
  23121. ret = ecc_test_curve(&rng, 14);
  23122. if (ret < 0) {
  23123. goto done;
  23124. }
  23125. #endif /* HAVE_ECC112 */
  23126. #if (defined(HAVE_ECC128) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 128
  23127. ret = ecc_test_curve(&rng, 16);
  23128. if (ret < 0) {
  23129. goto done;
  23130. }
  23131. #endif /* HAVE_ECC128 */
  23132. #if (defined(HAVE_ECC160) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 160
  23133. ret = ecc_test_curve(&rng, 20);
  23134. if (ret < 0) {
  23135. goto done;
  23136. }
  23137. #endif /* HAVE_ECC160 */
  23138. #if (defined(HAVE_ECC192) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 192
  23139. ret = ecc_test_curve(&rng, 24);
  23140. if (ret < 0) {
  23141. goto done;
  23142. }
  23143. #endif /* HAVE_ECC192 */
  23144. #if (defined(HAVE_ECC224) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 224
  23145. ret = ecc_test_curve(&rng, 28);
  23146. if (ret < 0) {
  23147. goto done;
  23148. }
  23149. #endif /* HAVE_ECC224 */
  23150. #if (defined(HAVE_ECC239) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 239
  23151. ret = ecc_test_curve(&rng, 30);
  23152. if (ret < 0) {
  23153. goto done;
  23154. }
  23155. #endif /* HAVE_ECC239 */
  23156. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  23157. ret = ecc_test_curve(&rng, 32);
  23158. if (ret < 0) {
  23159. goto done;
  23160. }
  23161. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  23162. defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT) && \
  23163. !defined(WOLFSSL_NO_MALLOC) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  23164. ret = ecc_point_test();
  23165. if (ret < 0) {
  23166. goto done;
  23167. }
  23168. #endif
  23169. ret = ecc_def_curve_test(&rng);
  23170. if (ret < 0) {
  23171. goto done;
  23172. }
  23173. #endif /* !NO_ECC256 */
  23174. #if (defined(HAVE_ECC320) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 320
  23175. ret = ecc_test_curve(&rng, 40);
  23176. if (ret < 0) {
  23177. goto done;
  23178. }
  23179. #endif /* HAVE_ECC320 */
  23180. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 384
  23181. ret = ecc_test_curve(&rng, 48);
  23182. if (ret < 0) {
  23183. goto done;
  23184. }
  23185. #endif /* HAVE_ECC384 */
  23186. #if (defined(HAVE_ECC512) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 512
  23187. ret = ecc_test_curve(&rng, 64);
  23188. if (ret < 0) {
  23189. goto done;
  23190. }
  23191. #endif /* HAVE_ECC512 */
  23192. #if (defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 521
  23193. ret = ecc_test_curve(&rng, 66);
  23194. if (ret < 0) {
  23195. goto done;
  23196. }
  23197. #endif /* HAVE_ECC521 */
  23198. #if defined(WOLFSSL_CUSTOM_CURVES)
  23199. ret = ecc_test_custom_curves(&rng);
  23200. if (ret != 0) {
  23201. goto done;
  23202. }
  23203. #endif
  23204. #if defined(HAVE_ECC_SIGN) && (defined(WOLFSSL_ECDSA_DETERMINISTIC_K) || \
  23205. defined(WOLFSSL_ECDSA_DETERMINISTIC_K_VARIANT)) \
  23206. && (!defined(FIPS_VERSION_GE) || FIPS_VERSION_GE(5,3))
  23207. #ifdef HAVE_ECC256
  23208. ret = ecc_test_deterministic_k(&rng);
  23209. if (ret != 0) {
  23210. printf("ecc_test_deterministic_k failed! %d\n", ret);
  23211. goto done;
  23212. }
  23213. #endif
  23214. #ifdef WOLFSSL_PUBLIC_MP
  23215. #if defined(HAVE_ECC384)
  23216. ret = ecc384_test_deterministic_k(&rng);
  23217. if (ret != 0) {
  23218. printf("ecc384_test_deterministic_k failed! %d\n", ret);
  23219. goto done;
  23220. }
  23221. #endif
  23222. #if defined(HAVE_ECC521)
  23223. ret = ecc521_test_deterministic_k(&rng);
  23224. if (ret != 0) {
  23225. printf("ecc512_test_deterministic_k failed! %d\n", ret);
  23226. goto done;
  23227. }
  23228. #endif
  23229. #endif
  23230. #endif
  23231. #if defined(HAVE_ECC_SIGN) && defined(WOLFSSL_ECDSA_SET_K) && \
  23232. !defined(WOLFSSL_KCAPI_ECC)
  23233. ret = ecc_test_sign_vectors(&rng);
  23234. if (ret != 0) {
  23235. printf("ecc_test_sign_vectors failed! %d\n", ret);
  23236. goto done;
  23237. }
  23238. #endif
  23239. #ifdef HAVE_ECC_CDH
  23240. ret = ecc_test_cdh_vectors(&rng);
  23241. if (ret != 0) {
  23242. printf("ecc_test_cdh_vectors failed! %d\n", ret);
  23243. goto done;
  23244. }
  23245. #endif
  23246. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  23247. !defined(WOLFSSL_STM32_PKA) && !defined(WOLFSSL_SILABS_SE_ACCEL) && \
  23248. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  23249. ret = ecc_test_make_pub(&rng);
  23250. if (ret != 0) {
  23251. printf("ecc_test_make_pub failed!: %d\n", ret);
  23252. goto done;
  23253. }
  23254. #elif defined(HAVE_ECC_KEY_IMPORT)
  23255. (void)ecc_test_make_pub; /* for compiler warning */
  23256. #endif
  23257. #ifdef WOLFSSL_CERT_GEN
  23258. ret = ecc_test_cert_gen(&rng);
  23259. if (ret != 0) {
  23260. printf("ecc_test_cert_gen failed!: %d\n", ret);
  23261. goto done;
  23262. }
  23263. #endif
  23264. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && !defined(WOLFSSL_NO_MALLOC) && \
  23265. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  23266. ret = ecc_test_allocator(&rng);
  23267. if (ret != 0) {
  23268. printf("ecc_test_allocator failed!: %d\n", ret);
  23269. goto done;
  23270. }
  23271. #endif
  23272. #if defined(WC_ECC_NONBLOCK) && defined(WOLFSSL_PUBLIC_MP) && \
  23273. defined(HAVE_ECC_SIGN) && defined(HAVE_ECC_VERIFY)
  23274. ret = ecc_test_nonblock(&rng);
  23275. if (ret != 0) {
  23276. printf("ecc_test_nonblock failed!: %d\n", ret);
  23277. goto done;
  23278. }
  23279. #endif
  23280. done:
  23281. wc_FreeRng(&rng);
  23282. return ret;
  23283. }
  23284. #if defined(HAVE_ECC_ENCRYPT) && defined(HAVE_AES_CBC) && \
  23285. (defined(WOLFSSL_AES_128) || defined(WOLFSSL_AES_256))
  23286. /* ecc_encrypt_e2e_test() uses wc_ecc_ctx_set_algo(), which was added in
  23287. * wolfFIPS 5.3.
  23288. * ecc_encrypt_kat() is used only by ecc_encrypt_e2e_test().
  23289. */
  23290. #if !defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3))
  23291. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && \
  23292. ECC_MIN_KEY_SZ <= 256 && defined(WOLFSSL_AES_128)
  23293. static int ecc_encrypt_kat(WC_RNG *rng)
  23294. {
  23295. int ret = 0;
  23296. #ifdef WOLFSSL_ECIES_OLD
  23297. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  23298. ecc_key* userA = NULL;
  23299. #else
  23300. ecc_key userA[1];
  23301. #endif
  23302. int userAInit = 0;
  23303. #endif
  23304. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  23305. ecc_key* userB = NULL;
  23306. #else
  23307. ecc_key userB[1];
  23308. #endif
  23309. int userBInit = 0;
  23310. ecc_key* tmpKey;
  23311. byte plain[48];
  23312. word32 plainSz = sizeof(plain);
  23313. WOLFSSL_SMALL_STACK_STATIC const byte privKey[] = {
  23314. 0x04, 0x80, 0xef, 0x1d, 0xbe, 0x02, 0x0c, 0x20,
  23315. 0x5b, 0xab, 0x80, 0x35, 0x5b, 0x2a, 0x0f, 0x6d,
  23316. 0xd3, 0xb0, 0x7f, 0x7e, 0x7f, 0x86, 0x8a, 0x49,
  23317. 0xee, 0xb4, 0xaa, 0x09, 0x2d, 0x1e, 0x1d, 0x02
  23318. };
  23319. #if defined(WOLFSSL_ECIES_OLD) || defined(WOLFSSL_QNX_CAAM)
  23320. WOLFSSL_SMALL_STACK_STATIC const byte pubKey[] = {
  23321. 0x04,
  23322. /* X */
  23323. 0x50, 0xf2, 0x93, 0xa2, 0x48, 0xa9, 0xc0, 0x5a,
  23324. 0x9a, 0xa7, 0x70, 0x34, 0xb7, 0x7f, 0x4c, 0x3a,
  23325. 0xad, 0xfc, 0xd8, 0xb6, 0x76, 0x0a, 0xe3, 0xc1,
  23326. 0x87, 0x17, 0x07, 0x2d, 0x8d, 0xa3, 0x63, 0xa0,
  23327. /* X */
  23328. 0xc1, 0x27, 0xb2, 0x97, 0x9b, 0x84, 0xe7, 0xcd,
  23329. 0x20, 0x65, 0x8d, 0x2b, 0x6a, 0x93, 0x75, 0xaa,
  23330. 0x8b, 0xe1, 0x3a, 0x7b, 0x24, 0x1a, 0xbe, 0xe8,
  23331. 0x36, 0xd2, 0xe6, 0x34, 0x8a, 0x7a, 0xb3, 0x28
  23332. };
  23333. #endif
  23334. WOLFSSL_SMALL_STACK_STATIC const byte enc_msg[] = {
  23335. #ifdef WOLFSSL_ECIES_OLD
  23336. 0x42, 0x70, 0xbf, 0xf9, 0xf4, 0x7e, 0x4b, 0x9b,
  23337. 0xb5, 0x4c, 0xcc, 0xc5, 0x94, 0xa7, 0xef, 0xaa,
  23338. 0xc3, 0x7c, 0x85, 0xa6, 0x51, 0x6e, 0xd3, 0xfa,
  23339. 0x56, 0xc9, 0x10, 0x4d, 0x14, 0x32, 0x61, 0xb8,
  23340. 0xbb, 0x66, 0x7a, 0xb5, 0xbc, 0x95, 0xf8, 0xca,
  23341. 0xd1, 0x2a, 0x19, 0x51, 0x44, 0xd8, 0x0e, 0x57,
  23342. 0x34, 0xed, 0x45, 0x89, 0x2e, 0x57, 0xbe, 0xd5,
  23343. 0x06, 0x22, 0xd7, 0x13, 0x0a, 0x0e, 0x40, 0x36,
  23344. 0x0d, 0x05, 0x0d, 0xb6, 0xae, 0x61, 0x37, 0x18,
  23345. 0x83, 0x90, 0x0a, 0x27, 0x95, 0x41, 0x8c, 0x45
  23346. #elif defined(WOLFSSL_ECIES_ISO18033)
  23347. 0x04, 0x50, 0xf2, 0x93, 0xa2, 0x48, 0xa9, 0xc0,
  23348. 0x5a, 0x9a, 0xa7, 0x70, 0x34, 0xb7, 0x7f, 0x4c,
  23349. 0x3a, 0xad, 0xfc, 0xd8, 0xb6, 0x76, 0x0a, 0xe3,
  23350. 0xc1, 0x87, 0x17, 0x07, 0x2d, 0x8d, 0xa3, 0x63,
  23351. 0xa0, 0xc1, 0x27, 0xb2, 0x97, 0x9b, 0x84, 0xe7,
  23352. 0xcd, 0x20, 0x65, 0x8d, 0x2b, 0x6a, 0x93, 0x75,
  23353. 0xaa, 0x8b, 0xe1, 0x3a, 0x7b, 0x24, 0x1a, 0xbe,
  23354. 0xe8, 0x36, 0xd2, 0xe6, 0x34, 0x8a, 0x7a, 0xb3,
  23355. 0x28, 0xbb, 0x9f, 0xa8, 0x2d, 0xe1, 0xf1, 0x67,
  23356. 0x45, 0x02, 0x19, 0xdc, 0xc8, 0x24, 0x8b, 0x20,
  23357. 0x02, 0xa0, 0x8f, 0x95, 0x12, 0x55, 0x51, 0xf8,
  23358. 0x03, 0xc4, 0x54, 0x13, 0x98, 0x2d, 0xf0, 0x31,
  23359. 0x51, 0x80, 0x45, 0x24, 0xcb, 0x8b, 0x48, 0xa6,
  23360. 0x8b, 0x8e, 0x97, 0x9c, 0x56, 0x4d, 0x70, 0x00,
  23361. 0x53, 0xd3, 0x47, 0x00, 0x5a, 0x23, 0x8c, 0xf9,
  23362. 0xfd, 0xd2, 0x33, 0x2c, 0x43, 0x6e, 0x9e, 0xb2,
  23363. 0xf4, 0x95, 0xd4, 0xcf, 0x30, 0xd6, 0xa2, 0xc5,
  23364. 0x35, 0x96, 0x6a, 0xd4, 0x36, 0x15, 0xa9, 0xbd,
  23365. 0x7f
  23366. #elif defined(WOLFSSL_ECIES_GEN_IV)
  23367. /* EC P-256 point */
  23368. 0x04,
  23369. /* X */
  23370. 0x50, 0xf2, 0x93, 0xa2, 0x48, 0xa9, 0xc0, 0x5a,
  23371. 0x9a, 0xa7, 0x70, 0x34, 0xb7, 0x7f, 0x4c, 0x3a,
  23372. 0xad, 0xfc, 0xd8, 0xb6, 0x76, 0x0a, 0xe3, 0xc1,
  23373. 0x87, 0x17, 0x07, 0x2d, 0x8d, 0xa3, 0x63, 0xa0,
  23374. /* Y */
  23375. 0xc1, 0x27, 0xb2, 0x97, 0x9b, 0x84, 0xe7, 0xcd,
  23376. 0x20, 0x65, 0x8d, 0x2b, 0x6a, 0x93, 0x75, 0xaa,
  23377. 0x8b, 0xe1, 0x3a, 0x7b, 0x24, 0x1a, 0xbe, 0xe8,
  23378. 0x36, 0xd2, 0xe6, 0x34, 0x8a, 0x7a, 0xb3, 0x28,
  23379. /* IV */
  23380. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  23381. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  23382. /* Encrypted Msg */
  23383. 0xe5, 0x17, 0xaf, 0x0d, 0x65, 0x4d, 0x3d, 0x50,
  23384. 0x96, 0x05, 0xc9, 0x63, 0x2c, 0xef, 0x1c, 0x1f,
  23385. 0x78, 0xc9, 0x90, 0x7a, 0x14, 0x00, 0xfc, 0x44,
  23386. 0x71, 0x6d, 0x57, 0x8c, 0xdf, 0x23, 0xca, 0x65,
  23387. 0xcf, 0x93, 0x06, 0xb6, 0x9a, 0xf4, 0x61, 0xbd,
  23388. 0x44, 0x1a, 0xeb, 0x52, 0x68, 0x0f, 0xd1, 0xde,
  23389. /* HMAC */
  23390. 0x5a, 0x22, 0xc1, 0x5d, 0x99, 0x66, 0x3f, 0x24,
  23391. 0x35, 0x96, 0xac, 0xf7, 0xf6, 0x28, 0x45, 0x16,
  23392. 0x52, 0x19, 0x0d, 0xe4, 0xb2, 0xca, 0x5b, 0x28,
  23393. 0x4e, 0xbb, 0xf3, 0x98, 0x57, 0xd7, 0x3b, 0xe2
  23394. #else
  23395. 0x04, 0x50, 0xf2, 0x93, 0xa2, 0x48, 0xa9, 0xc0,
  23396. 0x5a, 0x9a, 0xa7, 0x70, 0x34, 0xb7, 0x7f, 0x4c,
  23397. 0x3a, 0xad, 0xfc, 0xd8, 0xb6, 0x76, 0x0a, 0xe3,
  23398. 0xc1, 0x87, 0x17, 0x07, 0x2d, 0x8d, 0xa3, 0x63,
  23399. 0xa0, 0xc1, 0x27, 0xb2, 0x97, 0x9b, 0x84, 0xe7,
  23400. 0xcd, 0x20, 0x65, 0x8d, 0x2b, 0x6a, 0x93, 0x75,
  23401. 0xaa, 0x8b, 0xe1, 0x3a, 0x7b, 0x24, 0x1a, 0xbe,
  23402. 0xe8, 0x36, 0xd2, 0xe6, 0x34, 0x8a, 0x7a, 0xb3,
  23403. 0x28, 0xe5, 0x17, 0xaf, 0x0d, 0x65, 0x4d, 0x3d,
  23404. 0x50, 0x96, 0x05, 0xc9, 0x63, 0x2c, 0xef, 0x1c,
  23405. 0x1f, 0x78, 0xc9, 0x90, 0x7a, 0x14, 0x00, 0xfc,
  23406. 0x44, 0x71, 0x6d, 0x57, 0x8c, 0xdf, 0x23, 0xca,
  23407. 0x65, 0xcf, 0x93, 0x06, 0xb6, 0x9a, 0xf4, 0x61,
  23408. 0xbd, 0x44, 0x1a, 0xeb, 0x52, 0x68, 0x0f, 0xd1,
  23409. 0xde, 0xc7, 0x3f, 0x6f, 0xce, 0xbe, 0x49, 0x61,
  23410. 0x48, 0x01, 0x77, 0x41, 0xd0, 0xd8, 0x5b, 0x48,
  23411. 0xca, 0x4e, 0x47, 0x3e, 0x47, 0xbf, 0x1d, 0x28,
  23412. 0x4c, 0x18, 0x1a, 0xfb, 0x96, 0x95, 0xda, 0xde,
  23413. 0x55
  23414. #endif
  23415. };
  23416. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = {
  23417. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  23418. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  23419. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  23420. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
  23421. 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  23422. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f
  23423. };
  23424. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  23425. userB = (ecc_key *)XMALLOC(sizeof(*userB), HEAP_HINT,
  23426. DYNAMIC_TYPE_TMP_BUFFER);
  23427. if (userB == NULL) {
  23428. ret = -10451;
  23429. }
  23430. #ifdef WOLFSSL_ECIES_OLD
  23431. if (ret == 0) {
  23432. userA = (ecc_key *)XMALLOC(sizeof(*userA), HEAP_HINT,
  23433. DYNAMIC_TYPE_TMP_BUFFER);
  23434. if (userA == NULL) {
  23435. ret = -10450;
  23436. }
  23437. }
  23438. #endif
  23439. #endif
  23440. if (ret == 0) {
  23441. ret = wc_ecc_init_ex(userB, HEAP_HINT, devId);
  23442. if (ret != 0)
  23443. ret = -10453;
  23444. }
  23445. if (ret == 0) {
  23446. userBInit = 1;
  23447. #ifdef WOLFSSL_ECIES_OLD
  23448. ret = wc_ecc_init_ex(userA, HEAP_HINT, devId);
  23449. if (ret != 0)
  23450. ret = -10452;
  23451. }
  23452. if (ret == 0) {
  23453. userAInit = 1;
  23454. tmpKey = userA;
  23455. #else
  23456. tmpKey = NULL;
  23457. #endif
  23458. }
  23459. if (ret == 0) {
  23460. #ifdef WOLFSSL_QNX_CAAM
  23461. ret = wc_ecc_import_private_key_ex(privKey, sizeof(privKey), pubKey,
  23462. sizeof(pubKey), userB, ECC_SECP256R1);
  23463. #else
  23464. ret = wc_ecc_import_private_key_ex(privKey, sizeof(privKey), NULL, 0,
  23465. userB, ECC_SECP256R1);
  23466. #endif
  23467. if (ret != 0)
  23468. ret = -10454;
  23469. }
  23470. #ifdef WOLFSSL_ECIES_OLD
  23471. if (ret == 0) {
  23472. ret = wc_ecc_import_x963_ex(pubKey, sizeof(pubKey), userA,
  23473. ECC_SECP256R1);
  23474. if (ret != 0)
  23475. ret = -10455;
  23476. }
  23477. #endif
  23478. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  23479. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  23480. !defined(HAVE_SELFTEST)
  23481. if (ret == 0) {
  23482. ret = wc_ecc_set_rng(userB, rng);
  23483. if (ret != 0) {
  23484. ret = -10456;
  23485. }
  23486. }
  23487. #else
  23488. (void)rng;
  23489. #endif
  23490. if (ret == 0) {
  23491. ret = wc_ecc_decrypt(userB, tmpKey, enc_msg, sizeof(enc_msg), plain,
  23492. &plainSz, NULL);
  23493. if (ret != 0)
  23494. ret = -10457;
  23495. }
  23496. if (ret == 0) {
  23497. if (XMEMCMP(plain, msg, sizeof(msg)) != 0) {
  23498. ret = -10458;
  23499. }
  23500. }
  23501. if (userBInit)
  23502. wc_ecc_free(userB);
  23503. #ifdef WOLFSSL_ECIES_OLD
  23504. if (userAInit)
  23505. wc_ecc_free(userA);
  23506. #endif
  23507. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  23508. if (userB != NULL) {
  23509. XFREE(userB, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23510. }
  23511. #ifdef WOLFSSL_ECIES_OLD
  23512. if (userA != NULL) {
  23513. XFREE(userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23514. }
  23515. #endif
  23516. #endif
  23517. return ret;
  23518. }
  23519. #endif
  23520. static int ecc_encrypt_e2e_test(WC_RNG* rng, ecc_key* userA, ecc_key* userB,
  23521. byte encAlgo, byte kdfAlgo, byte macAlgo)
  23522. {
  23523. int ret = 0;
  23524. byte msg[48];
  23525. byte plain[48];
  23526. #ifdef WOLFSSL_ECIES_OLD
  23527. byte out[80];
  23528. #elif defined(WOLFSSL_ECIES_GEN_IV)
  23529. byte out[1 + ECC_KEYGEN_SIZE * 2 + 16 + 80];
  23530. #else
  23531. byte out[1 + ECC_KEYGEN_SIZE * 2 + 80];
  23532. #endif
  23533. word32 outSz = sizeof(out);
  23534. word32 plainSz = sizeof(plain);
  23535. int i;
  23536. ecEncCtx* cliCtx = NULL;
  23537. ecEncCtx* srvCtx = NULL;
  23538. byte cliSalt[EXCHANGE_SALT_SZ];
  23539. byte srvSalt[EXCHANGE_SALT_SZ];
  23540. const byte* tmpSalt;
  23541. byte msg2[48];
  23542. byte plain2[48];
  23543. #ifdef WOLFSSL_ECIES_OLD
  23544. byte out2[80];
  23545. #elif defined(WOLFSSL_ECIES_GEN_IV)
  23546. byte out2[1 + ECC_KEYGEN_SIZE * 2 + 16 + 80];
  23547. #else
  23548. byte out2[1 + ECC_KEYGEN_SIZE * 2 + 80];
  23549. #endif
  23550. word32 outSz2 = sizeof(out2);
  23551. word32 plainSz2 = sizeof(plain2);
  23552. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  23553. ecc_key *tmpKey = (ecc_key *)XMALLOC(sizeof(ecc_key), HEAP_HINT,
  23554. DYNAMIC_TYPE_TMP_BUFFER);
  23555. #else
  23556. ecc_key tmpKey[1];
  23557. #endif
  23558. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  23559. if (tmpKey == NULL) {
  23560. ERROR_OUT(MEMORY_E, done);
  23561. }
  23562. #endif
  23563. ret = wc_ecc_init_ex(tmpKey, HEAP_HINT, devId);
  23564. if (ret != 0)
  23565. goto done;
  23566. /* set message to incrementing 0,1,2,etc... */
  23567. for (i = 0; i < (int)sizeof(msg); i++)
  23568. msg[i] = i;
  23569. /* encrypt msg to B */
  23570. ret = wc_ecc_encrypt(userA, userB, msg, sizeof(msg), out, &outSz, NULL);
  23571. if (ret != 0) {
  23572. ret = -10405; goto done;
  23573. }
  23574. #ifdef WOLFSSL_ECIES_OLD
  23575. tmpKey->dp = userA->dp;
  23576. ret = wc_ecc_copy_point(&userA->pubkey, &tmpKey->pubkey);
  23577. if (ret != 0) {
  23578. ret = -10413; goto done;
  23579. }
  23580. #endif
  23581. /* decrypt msg from A */
  23582. ret = wc_ecc_decrypt(userB, tmpKey, out, outSz, plain, &plainSz, NULL);
  23583. if (ret != 0) {
  23584. ret = -10406; goto done;
  23585. }
  23586. if (XMEMCMP(plain, msg, sizeof(msg)) != 0) {
  23587. ret = -10407; goto done;
  23588. }
  23589. #ifndef WOLFSSL_ECIES_OLD
  23590. /* A decrypts msg (response) from B */
  23591. ret = wc_ecc_decrypt(userB, NULL, out, outSz, plain2, &plainSz2, NULL);
  23592. if (ret != 0)
  23593. goto done;
  23594. if (XMEMCMP(plain, msg, sizeof(msg)) != 0) {
  23595. ret = -10415; goto done;
  23596. }
  23597. #endif
  23598. /* let's verify message exchange works, A is client, B is server */
  23599. cliCtx = wc_ecc_ctx_new(REQ_RESP_CLIENT, rng);
  23600. srvCtx = wc_ecc_ctx_new(REQ_RESP_SERVER, rng);
  23601. if (cliCtx == NULL || srvCtx == NULL) {
  23602. ret = -10408; goto done;
  23603. }
  23604. ret = wc_ecc_ctx_set_algo(cliCtx, encAlgo, kdfAlgo, macAlgo);
  23605. if (ret != 0)
  23606. goto done;
  23607. ret = wc_ecc_ctx_set_algo(srvCtx, encAlgo, kdfAlgo, macAlgo);
  23608. if (ret != 0)
  23609. goto done;
  23610. /* get salt to send to peer */
  23611. tmpSalt = wc_ecc_ctx_get_own_salt(cliCtx);
  23612. if (tmpSalt == NULL) {
  23613. ret = -10409; goto done;
  23614. }
  23615. XMEMCPY(cliSalt, tmpSalt, EXCHANGE_SALT_SZ);
  23616. tmpSalt = wc_ecc_ctx_get_own_salt(srvCtx);
  23617. if (tmpSalt == NULL) {
  23618. ret = -10410; goto done;
  23619. }
  23620. XMEMCPY(srvSalt, tmpSalt, EXCHANGE_SALT_SZ);
  23621. /* in actual use, we'd get the peer's salt over the transport */
  23622. ret = wc_ecc_ctx_set_peer_salt(cliCtx, srvSalt);
  23623. if (ret != 0)
  23624. goto done;
  23625. ret = wc_ecc_ctx_set_peer_salt(srvCtx, cliSalt);
  23626. if (ret != 0)
  23627. goto done;
  23628. ret = wc_ecc_ctx_set_info(cliCtx, (byte*)"wolfSSL MSGE", 11);
  23629. if (ret != 0)
  23630. goto done;
  23631. ret = wc_ecc_ctx_set_info(srvCtx, (byte*)"wolfSSL MSGE", 11);
  23632. if (ret != 0)
  23633. goto done;
  23634. /* get encrypted msg (request) to send to B */
  23635. outSz = sizeof(out);
  23636. ret = wc_ecc_encrypt(userA, userB, msg, sizeof(msg), out, &outSz,cliCtx);
  23637. if (ret != 0)
  23638. goto done;
  23639. #ifndef WOLFSSL_ECIES_OLD
  23640. wc_ecc_free(tmpKey);
  23641. #endif
  23642. /* B decrypts msg (request) from A */
  23643. plainSz = sizeof(plain);
  23644. ret = wc_ecc_decrypt(userB, tmpKey, out, outSz, plain, &plainSz, srvCtx);
  23645. if (ret != 0)
  23646. goto done;
  23647. if (XMEMCMP(plain, msg, sizeof(msg)) != 0) {
  23648. ret = -10411; goto done;
  23649. }
  23650. /* msg2 (response) from B to A */
  23651. for (i = 0; i < (int)sizeof(msg2); i++)
  23652. msg2[i] = i + sizeof(msg2);
  23653. /* get encrypted msg (response) to send to B */
  23654. ret = wc_ecc_encrypt(userB, userA, msg2, sizeof(msg2), out2,
  23655. &outSz2, srvCtx);
  23656. if (ret != 0)
  23657. goto done;
  23658. #ifdef WOLFSSL_ECIES_OLD
  23659. tmpKey->dp = userB->dp;
  23660. ret = wc_ecc_copy_point(&userB->pubkey, &tmpKey->pubkey);
  23661. if (ret != 0) {
  23662. ret = -10414; goto done;
  23663. }
  23664. #else
  23665. wc_ecc_free(tmpKey);
  23666. #endif
  23667. /* A decrypts msg (response) from B */
  23668. ret = wc_ecc_decrypt(userA, tmpKey, out2, outSz2, plain2, &plainSz2,
  23669. cliCtx);
  23670. if (ret != 0)
  23671. goto done;
  23672. if (XMEMCMP(plain2, msg2, sizeof(msg2)) != 0) {
  23673. ret = -10412; goto done;
  23674. }
  23675. #if defined(HAVE_COMP_KEY) && \
  23676. (! defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3)))
  23677. /* Create new client and server contexts. */
  23678. wc_ecc_ctx_free(srvCtx);
  23679. wc_ecc_ctx_free(cliCtx);
  23680. /* let's verify message exchange works, A is client, B is server */
  23681. cliCtx = wc_ecc_ctx_new(REQ_RESP_CLIENT, rng);
  23682. srvCtx = wc_ecc_ctx_new(REQ_RESP_SERVER, rng);
  23683. if (cliCtx == NULL || srvCtx == NULL) {
  23684. ret = -10416; goto done;
  23685. }
  23686. ret = wc_ecc_ctx_set_algo(cliCtx, encAlgo, kdfAlgo, macAlgo);
  23687. if (ret != 0)
  23688. goto done;
  23689. ret = wc_ecc_ctx_set_algo(srvCtx, encAlgo, kdfAlgo, macAlgo);
  23690. if (ret != 0)
  23691. goto done;
  23692. /* get salt to send to peer */
  23693. tmpSalt = wc_ecc_ctx_get_own_salt(cliCtx);
  23694. if (tmpSalt == NULL) {
  23695. ret = -10417; goto done;
  23696. }
  23697. XMEMCPY(cliSalt, tmpSalt, EXCHANGE_SALT_SZ);
  23698. tmpSalt = wc_ecc_ctx_get_own_salt(srvCtx);
  23699. if (tmpSalt == NULL) {
  23700. ret = -10418; goto done;
  23701. }
  23702. XMEMCPY(srvSalt, tmpSalt, EXCHANGE_SALT_SZ);
  23703. /* in actual use, we'd get the peer's salt over the transport */
  23704. ret = wc_ecc_ctx_set_peer_salt(cliCtx, srvSalt);
  23705. if (ret != 0)
  23706. goto done;
  23707. ret = wc_ecc_ctx_set_peer_salt(srvCtx, cliSalt);
  23708. if (ret != 0)
  23709. goto done;
  23710. ret = wc_ecc_ctx_set_info(cliCtx, (byte*)"wolfSSL MSGE", 12);
  23711. if (ret != 0)
  23712. goto done;
  23713. ret = wc_ecc_ctx_set_info(srvCtx, (byte*)"wolfSSL MSGE", 12);
  23714. if (ret != 0)
  23715. goto done;
  23716. /* get encrypted msg (request) to send to B - compressed public key */
  23717. outSz = sizeof(out);
  23718. ret = wc_ecc_encrypt_ex(userA, userB, msg, sizeof(msg), out, &outSz, cliCtx,
  23719. 1);
  23720. if (ret != 0)
  23721. goto done;
  23722. #ifndef WOLFSSL_ECIES_OLD
  23723. wc_ecc_free(tmpKey);
  23724. #endif
  23725. /* B decrypts msg (request) from A - out has a compressed public key */
  23726. plainSz = sizeof(plain);
  23727. ret = wc_ecc_decrypt(userB, tmpKey, out, outSz, plain, &plainSz, srvCtx);
  23728. if (ret != 0)
  23729. goto done;
  23730. if (XMEMCMP(plain, msg, sizeof(msg)) != 0) {
  23731. ret = -10419; goto done;
  23732. }
  23733. #endif /* HAVE_COMP_KEY && (!FIPS || FIPS>=5.3) */
  23734. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && \
  23735. (ECC_MIN_KEY_SZ <= 256) && defined(WOLFSSL_AES_128)
  23736. ret = ecc_encrypt_kat(rng);
  23737. #endif
  23738. done:
  23739. /* cleanup */
  23740. wc_ecc_ctx_free(srvCtx);
  23741. wc_ecc_ctx_free(cliCtx);
  23742. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  23743. if (tmpKey != NULL) {
  23744. wc_ecc_free(tmpKey);
  23745. XFREE(tmpKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23746. }
  23747. #else
  23748. wc_ecc_free(tmpKey);
  23749. #endif
  23750. return ret;
  23751. }
  23752. #endif /* !HAVE_FIPS || FIPS_VERSION_GE(5,3) */
  23753. WOLFSSL_TEST_SUBROUTINE int ecc_encrypt_test(void)
  23754. {
  23755. WC_RNG rng;
  23756. int ret;
  23757. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  23758. ecc_key *userA;
  23759. ecc_key *userB;
  23760. #else
  23761. ecc_key userA[1];
  23762. ecc_key userB[1];
  23763. #endif
  23764. #ifndef HAVE_FIPS
  23765. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  23766. #else
  23767. ret = wc_InitRng(&rng);
  23768. #endif
  23769. if (ret != 0)
  23770. return -10400;
  23771. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  23772. userA = (ecc_key *)XMALLOC(sizeof *userA, HEAP_HINT,
  23773. DYNAMIC_TYPE_TMP_BUFFER);
  23774. userB = (ecc_key *)XMALLOC(sizeof *userB, HEAP_HINT,
  23775. DYNAMIC_TYPE_TMP_BUFFER);
  23776. if ((userA == NULL) || (userB == NULL)) {
  23777. ERROR_OUT(MEMORY_E, done);
  23778. }
  23779. #endif
  23780. XMEMSET(userA, 0, sizeof *userA);
  23781. XMEMSET(userB, 0, sizeof *userB);
  23782. ret = wc_ecc_init_ex(userA, HEAP_HINT, devId);
  23783. if (ret != 0)
  23784. goto done;
  23785. ret = wc_ecc_init_ex(userB, HEAP_HINT, devId);
  23786. if (ret != 0)
  23787. goto done;
  23788. ret = wc_ecc_make_key(&rng, ECC_KEYGEN_SIZE, userA);
  23789. #if defined(WOLFSSL_ASYNC_CRYPT)
  23790. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_NONE);
  23791. #endif
  23792. if (ret != 0){
  23793. ret = -10401; goto done;
  23794. }
  23795. ret = wc_ecc_make_key(&rng, ECC_KEYGEN_SIZE, userB);
  23796. #if defined(WOLFSSL_ASYNC_CRYPT)
  23797. ret = wc_AsyncWait(ret, &userB->asyncDev, WC_ASYNC_FLAG_NONE);
  23798. #endif
  23799. if (ret != 0){
  23800. ret = -10402; goto done;
  23801. }
  23802. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  23803. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  23804. !defined(HAVE_SELFTEST)
  23805. ret = wc_ecc_set_rng(userA, &rng);
  23806. if (ret != 0) {
  23807. ret = -10403; goto done;
  23808. }
  23809. ret = wc_ecc_set_rng(userB, &rng);
  23810. if (ret != 0) {
  23811. ret = -10404; goto done;
  23812. }
  23813. #endif
  23814. #if !defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3))
  23815. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  23816. #ifdef WOLFSSL_AES_128
  23817. if (ret == 0) {
  23818. ret = ecc_encrypt_e2e_test(&rng, userA, userB, ecAES_128_CBC,
  23819. ecHKDF_SHA256, ecHMAC_SHA256);
  23820. if (ret != 0) {
  23821. printf("ECIES: AES_128_CBC, HKDF_SHA256, HMAC_SHA256\n");
  23822. }
  23823. }
  23824. #endif
  23825. #ifdef WOLFSSL_AES_256
  23826. if (ret == 0) {
  23827. ret = ecc_encrypt_e2e_test(&rng, userA, userB, ecAES_256_CBC,
  23828. ecHKDF_SHA256, ecHMAC_SHA256);
  23829. if (ret != 0) {
  23830. printf("ECIES: AES_256_CBC, HKDF_SHA256, HMAC_SHA256\n");
  23831. }
  23832. }
  23833. #endif
  23834. #endif
  23835. #if !defined(NO_AES) && defined(WOLFSSL_AES_COUNTER)
  23836. #ifdef WOLFSSL_AES_128
  23837. if (ret == 0) {
  23838. ret = ecc_encrypt_e2e_test(&rng, userA, userB, ecAES_128_CTR,
  23839. ecHKDF_SHA256, ecHMAC_SHA256);
  23840. if (ret != 0) {
  23841. printf("ECIES: AES_128_CTR, HKDF_SHA256, HMAC_SHA256\n");
  23842. }
  23843. }
  23844. #endif
  23845. #ifdef WOLFSSL_AES_256
  23846. if (ret == 0) {
  23847. ret = ecc_encrypt_e2e_test(&rng, userA, userB, ecAES_256_CTR,
  23848. ecHKDF_SHA256, ecHMAC_SHA256);
  23849. if (ret != 0) {
  23850. printf("ECIES: AES_256_CTR, HKDF_SHA256, HMAC_SHA256\n");
  23851. }
  23852. }
  23853. #endif
  23854. #endif
  23855. #endif /* !HAVE_FIPS || FIPS_VERSION_GE(5,3) */
  23856. done:
  23857. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  23858. if (userA != NULL) {
  23859. wc_ecc_free(userA);
  23860. XFREE(userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23861. }
  23862. if (userB != NULL) {
  23863. wc_ecc_free(userB);
  23864. XFREE(userB, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23865. }
  23866. #else
  23867. wc_ecc_free(userB);
  23868. wc_ecc_free(userA);
  23869. #endif
  23870. wc_FreeRng(&rng);
  23871. return ret;
  23872. }
  23873. #endif /* HAVE_ECC_ENCRYPT && HAVE_AES_CBC && WOLFSSL_AES_128 */
  23874. #if defined(USE_CERT_BUFFERS_256) && !defined(WOLFSSL_ATECC508A) && \
  23875. !defined(WOLFSSL_ATECC608A) && !defined(NO_ECC256) && \
  23876. defined(HAVE_ECC_VERIFY) && defined(HAVE_ECC_SIGN) && \
  23877. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  23878. WOLFSSL_TEST_SUBROUTINE int ecc_test_buffers(void)
  23879. {
  23880. size_t bytes;
  23881. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  23882. ecc_key *cliKey = (ecc_key *)XMALLOC(sizeof *cliKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23883. ecc_key *servKey = (ecc_key *)XMALLOC(sizeof *servKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23884. ecc_key *tmpKey = (ecc_key *)XMALLOC(sizeof *tmpKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23885. #else
  23886. ecc_key cliKey[1];
  23887. ecc_key servKey[1];
  23888. ecc_key tmpKey[1];
  23889. #endif
  23890. WC_RNG rng;
  23891. word32 idx = 0;
  23892. int ret;
  23893. /* pad our test message to 32 bytes so evenly divisible by AES_BLOCK_SZ */
  23894. byte in[] = "Everyone gets Friday off. ecc p";
  23895. word32 inLen = (word32)XSTRLEN((char*)in);
  23896. byte out[256];
  23897. byte plain[256];
  23898. int verify = 0;
  23899. word32 x;
  23900. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  23901. if ((cliKey == NULL) || (servKey == NULL) || (tmpKey == NULL))
  23902. ERROR_OUT(MEMORY_E, done);
  23903. #endif
  23904. ret = wc_ecc_init_ex(cliKey, HEAP_HINT, devId);
  23905. if (ret != 0)
  23906. ERROR_OUT(-10420, done);
  23907. ret = wc_ecc_init_ex(servKey, HEAP_HINT, devId);
  23908. if (ret != 0)
  23909. ERROR_OUT(-10421, done);
  23910. ret = wc_ecc_init_ex(tmpKey, HEAP_HINT, devId);
  23911. if (ret != 0)
  23912. ERROR_OUT(-10421, done);
  23913. bytes = (size_t)sizeof_ecc_clikey_der_256;
  23914. /* place client key into ecc_key struct cliKey */
  23915. ret = wc_EccPrivateKeyDecode(ecc_clikey_der_256, &idx, cliKey,
  23916. (word32)bytes);
  23917. if (ret != 0)
  23918. ERROR_OUT(-10422, done);
  23919. idx = 0;
  23920. bytes = (size_t)sizeof_ecc_key_der_256;
  23921. /* place server key into ecc_key struct servKey */
  23922. ret = wc_EccPrivateKeyDecode(ecc_key_der_256, &idx, servKey,
  23923. (word32)bytes);
  23924. if (ret != 0)
  23925. ERROR_OUT(-10423, done);
  23926. #ifndef WC_NO_RNG
  23927. #ifndef HAVE_FIPS
  23928. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  23929. #else
  23930. ret = wc_InitRng(&rng);
  23931. #endif
  23932. if (ret != 0)
  23933. ERROR_OUT(-10424, done);
  23934. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  23935. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  23936. !defined(HAVE_SELFTEST)
  23937. ret = wc_ecc_set_rng(cliKey, &rng);
  23938. if (ret != 0) {
  23939. ERROR_OUT(-10425, done);
  23940. }
  23941. ret = wc_ecc_set_rng(servKey, &rng);
  23942. if (ret != 0) {
  23943. ERROR_OUT(-10425, done);
  23944. }
  23945. #endif
  23946. #endif /* !WC_NO_RNG */
  23947. #if defined(HAVE_ECC_ENCRYPT) && defined(HAVE_HKDF) && \
  23948. defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  23949. {
  23950. word32 y;
  23951. /* test encrypt and decrypt if they're available */
  23952. x = sizeof(out);
  23953. ret = wc_ecc_encrypt(cliKey, servKey, in, sizeof(in), out, &x, NULL);
  23954. if (ret < 0)
  23955. ERROR_OUT(-10426, done);
  23956. #ifdef WOLFSSL_ECIES_OLD
  23957. tmpKey->dp = cliKey->dp;
  23958. ret = wc_ecc_copy_point(&cliKey->pubkey, &tmpKey->pubkey);
  23959. if (ret != 0) {
  23960. ret = -10414; goto done;
  23961. }
  23962. #endif
  23963. y = sizeof(plain);
  23964. ret = wc_ecc_decrypt(servKey, tmpKey, out, x, plain, &y, NULL);
  23965. if (ret < 0)
  23966. ERROR_OUT(-10427, done);
  23967. if (XMEMCMP(plain, in, inLen))
  23968. ERROR_OUT(-10428, done);
  23969. }
  23970. #endif
  23971. x = sizeof(out);
  23972. do {
  23973. #if defined(WOLFSSL_ASYNC_CRYPT)
  23974. ret = wc_AsyncWait(ret, &cliKey->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  23975. #endif
  23976. if (ret == 0)
  23977. ret = wc_ecc_sign_hash(in, inLen, out, &x, &rng, cliKey);
  23978. } while (ret == WC_PENDING_E);
  23979. if (ret < 0)
  23980. ERROR_OUT(-10429, done);
  23981. TEST_SLEEP();
  23982. XMEMSET(plain, 0, sizeof(plain));
  23983. do {
  23984. #if defined(WOLFSSL_ASYNC_CRYPT)
  23985. ret = wc_AsyncWait(ret, &cliKey->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  23986. #endif
  23987. if (ret == 0)
  23988. ret = wc_ecc_verify_hash(out, x, in, inLen, &verify,
  23989. cliKey);
  23990. } while (ret == WC_PENDING_E);
  23991. if (ret < 0)
  23992. ERROR_OUT(-10430, done);
  23993. if (verify != 1)
  23994. ERROR_OUT(-10431, done);
  23995. TEST_SLEEP();
  23996. #ifdef WOLFSSL_CERT_EXT
  23997. idx = 0;
  23998. bytes = sizeof_ecc_clikeypub_der_256;
  23999. ret = wc_EccPublicKeyDecode(ecc_clikeypub_der_256, &idx, cliKey,
  24000. (word32) bytes);
  24001. if (ret != 0)
  24002. ERROR_OUT(-10432, done);
  24003. #endif
  24004. ret = 0;
  24005. done:
  24006. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  24007. if (cliKey != NULL) {
  24008. wc_ecc_free(cliKey);
  24009. XFREE(cliKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24010. }
  24011. if (servKey != NULL) {
  24012. wc_ecc_free(servKey);
  24013. XFREE(servKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24014. }
  24015. if (tmpKey != NULL) {
  24016. wc_ecc_free(tmpKey);
  24017. XFREE(tmpKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24018. }
  24019. #else
  24020. wc_ecc_free(cliKey);
  24021. wc_ecc_free(servKey);
  24022. wc_ecc_free(tmpKey);
  24023. #endif
  24024. wc_FreeRng(&rng);
  24025. return ret;
  24026. }
  24027. #endif /* USE_CERT_BUFFERS_256 && !WOLFSSL_ATECCX08A && !NO_ECC256 */
  24028. #endif /* HAVE_ECC */
  24029. #ifdef HAVE_CURVE25519
  24030. #if defined(HAVE_CURVE25519_SHARED_SECRET) && \
  24031. defined(HAVE_CURVE25519_KEY_IMPORT)
  24032. #ifdef CURVE25519_OVERFLOW_ALL_TESTS
  24033. #define X25519_TEST_CNT 5
  24034. #else
  24035. #define X25519_TEST_CNT 1
  24036. #endif
  24037. static int curve25519_overflow_test(void)
  24038. {
  24039. /* secret key for party a */
  24040. byte sa[X25519_TEST_CNT][32] = {
  24041. {
  24042. 0x8d,0xaf,0x6e,0x7a,0xc1,0xeb,0x8d,0x30,
  24043. 0x99,0x86,0xd3,0x90,0x47,0x96,0x21,0x3c,
  24044. 0x3a,0x75,0xc0,0x7b,0x75,0x01,0x75,0xa3,
  24045. 0x81,0x4b,0xff,0x5a,0xbc,0x96,0x87,0x28
  24046. },
  24047. #ifdef CURVE25519_OVERFLOW_ALL_TESTS
  24048. {
  24049. 0x9d,0x63,0x5f,0xce,0xe2,0xe8,0xd7,0xfb,
  24050. 0x68,0x77,0x0e,0x44,0xd1,0xad,0x87,0x2b,
  24051. 0xf4,0x65,0x06,0xb7,0xbb,0xdb,0xbe,0x6e,
  24052. 0x02,0x43,0x24,0xc7,0x3d,0x7b,0x88,0x60
  24053. },
  24054. {
  24055. 0x63,0xbf,0x76,0xa9,0x73,0xa0,0x09,0xb9,
  24056. 0xcc,0xc9,0x4d,0x47,0x2d,0x14,0x0e,0x52,
  24057. 0xa3,0x84,0x55,0xb8,0x7c,0xdb,0xce,0xb1,
  24058. 0xe4,0x5b,0x8a,0xb9,0x30,0xf1,0xa4,0xa0
  24059. },
  24060. {
  24061. 0x63,0xbf,0x76,0xa9,0x73,0xa0,0x09,0xb9,
  24062. 0xcc,0xc9,0x4d,0x47,0x2d,0x14,0x0e,0x52,
  24063. 0xa3,0x84,0x55,0xb8,0x7c,0xdb,0xce,0xb1,
  24064. 0xe4,0x5b,0x8a,0xb9,0x30,0xf1,0xa4,0xa0
  24065. },
  24066. {
  24067. 0x63,0xbf,0x76,0xa9,0x73,0xa0,0x09,0xb9,
  24068. 0xcc,0xc9,0x4d,0x47,0x2d,0x14,0x0e,0x52,
  24069. 0xa3,0x84,0x55,0xb8,0x7c,0xdb,0xce,0xb1,
  24070. 0xe4,0x5b,0x8a,0xb9,0x30,0xf1,0xa4,0xa0
  24071. }
  24072. #endif
  24073. };
  24074. /* public key for party b */
  24075. byte pb[X25519_TEST_CNT][32] = {
  24076. {
  24077. 0x7f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  24078. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  24079. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  24080. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xf0
  24081. },
  24082. #ifdef CURVE25519_OVERFLOW_ALL_TESTS
  24083. {
  24084. /* 0xff first byte in original - invalid! */
  24085. 0x7f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  24086. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  24087. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  24088. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xf0
  24089. },
  24090. {
  24091. 0x36,0x1a,0x74,0x87,0x28,0x59,0xe0,0xb6,
  24092. 0xe4,0x2b,0x17,0x9b,0x16,0xb0,0x3b,0xf8,
  24093. 0xb8,0x9f,0x2a,0x8f,0xc5,0x33,0x68,0x4f,
  24094. 0xde,0x4d,0xd8,0x80,0x63,0xe7,0xb4,0x0a
  24095. },
  24096. {
  24097. 0x00,0x80,0x38,0x59,0x19,0x3a,0x66,0x12,
  24098. 0xfd,0xa1,0xec,0x1c,0x40,0x84,0x40,0xbd,
  24099. 0x64,0x10,0x8b,0x53,0x81,0x21,0x03,0x2d,
  24100. 0x7d,0x33,0xb4,0x01,0x57,0x0d,0xe1,0x89
  24101. },
  24102. {
  24103. 0x1d,0xf8,0xf8,0x33,0x89,0x6c,0xb7,0xba,
  24104. 0x94,0x73,0xfa,0xc2,0x36,0xac,0xbe,0x49,
  24105. 0xaf,0x85,0x3e,0x93,0x5f,0xae,0xb2,0xc0,
  24106. 0xc8,0x80,0x8f,0x4a,0xaa,0xd3,0x55,0x2b
  24107. }
  24108. #endif
  24109. };
  24110. /* expected shared key */
  24111. byte ss[X25519_TEST_CNT][32] = {
  24112. {
  24113. 0x5c,0x4c,0x85,0x5f,0xfb,0x20,0x38,0xcc,
  24114. 0x55,0x16,0x5b,0x8a,0xa7,0xed,0x57,0x6e,
  24115. 0x35,0xaa,0x71,0x67,0x85,0x1f,0xb6,0x28,
  24116. 0x17,0x07,0x7b,0xda,0x76,0xdd,0xe0,0xb4
  24117. },
  24118. #ifdef CURVE25519_OVERFLOW_ALL_TESTS
  24119. {
  24120. 0x33,0xf6,0xc1,0x34,0x62,0x92,0x06,0x02,
  24121. 0x95,0xdb,0x91,0x4c,0x5d,0x52,0x54,0xc7,
  24122. 0xd2,0x5b,0x24,0xb5,0x4f,0x33,0x59,0x79,
  24123. 0x9f,0x6d,0x7e,0x4a,0x4c,0x30,0xd6,0x38
  24124. },
  24125. {
  24126. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24127. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24128. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24129. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x02
  24130. },
  24131. {
  24132. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24133. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24134. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24135. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x09
  24136. },
  24137. {
  24138. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24139. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24140. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24141. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x10
  24142. }
  24143. #endif
  24144. };
  24145. int ret = 0;
  24146. int i;
  24147. word32 y;
  24148. byte shared[32];
  24149. curve25519_key userA;
  24150. wc_curve25519_init_ex(&userA, HEAP_HINT, devId);
  24151. for (i = 0; i < X25519_TEST_CNT; i++) {
  24152. if (wc_curve25519_import_private_raw(sa[i], sizeof(sa[i]), pb[i],
  24153. sizeof(pb[i]), &userA) != 0) {
  24154. ret = -10500 - i; break;
  24155. }
  24156. /* test against known test vector */
  24157. XMEMSET(shared, 0, sizeof(shared));
  24158. y = sizeof(shared);
  24159. if (wc_curve25519_shared_secret(&userA, &userA, shared, &y) != 0) {
  24160. ret = -10510 - i; break;
  24161. }
  24162. if (XMEMCMP(ss[i], shared, y)) {
  24163. ret = -10520 - i; break;
  24164. }
  24165. }
  24166. wc_curve25519_free(&userA);
  24167. return ret;
  24168. }
  24169. /* Test the wc_curve25519_check_public API.
  24170. *
  24171. * returns 0 on success and -ve on failure.
  24172. */
  24173. static int curve25519_check_public_test(void)
  24174. {
  24175. /* Little-endian values that will fail */
  24176. byte fail_le[][CURVE25519_KEYSIZE] = {
  24177. {
  24178. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24179. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24180. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24181. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  24182. },
  24183. {
  24184. 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24185. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24186. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24187. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  24188. },
  24189. {
  24190. 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24191. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24192. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24193. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x81
  24194. },
  24195. };
  24196. /* Big-endian values that will fail */
  24197. byte fail_be[][CURVE25519_KEYSIZE] = {
  24198. {
  24199. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24200. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24201. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24202. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  24203. },
  24204. {
  24205. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24206. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24207. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24208. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  24209. },
  24210. {
  24211. 0x81,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24212. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24213. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24214. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  24215. },
  24216. };
  24217. /* Good or valid public value */
  24218. byte good[CURVE25519_KEYSIZE] = {
  24219. 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24220. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24221. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24222. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  24223. };
  24224. int i;
  24225. /* Parameter checks */
  24226. /* NULL pointer */
  24227. if (wc_curve25519_check_public(NULL, 0, EC25519_LITTLE_ENDIAN) !=
  24228. BAD_FUNC_ARG) {
  24229. return -10600;
  24230. }
  24231. if (wc_curve25519_check_public(NULL, 0, EC25519_BIG_ENDIAN) !=
  24232. BAD_FUNC_ARG) {
  24233. return -10601;
  24234. }
  24235. /* Length of 0 treated differently to other invalid lengths for TLS */
  24236. if (wc_curve25519_check_public(good, 0, EC25519_LITTLE_ENDIAN) != BUFFER_E)
  24237. return -10602;
  24238. if (wc_curve25519_check_public(good, 0, EC25519_BIG_ENDIAN) != BUFFER_E)
  24239. return -10603;
  24240. /* Length not CURVE25519_KEYSIZE */
  24241. for (i = 1; i < CURVE25519_KEYSIZE + 2; i++) {
  24242. if (i == CURVE25519_KEYSIZE)
  24243. continue;
  24244. if (wc_curve25519_check_public(good, i, EC25519_LITTLE_ENDIAN) !=
  24245. ECC_BAD_ARG_E) {
  24246. return -10604 - i;
  24247. }
  24248. if (wc_curve25519_check_public(good, i, EC25519_BIG_ENDIAN) !=
  24249. ECC_BAD_ARG_E) {
  24250. return -10614 - i;
  24251. }
  24252. }
  24253. /* Little-endian fail cases */
  24254. for (i = 0; i < (int)(sizeof(fail_le) / sizeof(*fail_le)); i++) {
  24255. if (wc_curve25519_check_public(fail_le[i], CURVE25519_KEYSIZE,
  24256. EC25519_LITTLE_ENDIAN) == 0) {
  24257. return -10624 - i;
  24258. }
  24259. }
  24260. /* Big-endian fail cases */
  24261. for (i = 0; i < (int)(sizeof(fail_be) / sizeof(*fail_be)); i++) {
  24262. if (wc_curve25519_check_public(fail_be[i], CURVE25519_KEYSIZE,
  24263. EC25519_BIG_ENDIAN) == 0) {
  24264. return -10634 - i;
  24265. }
  24266. }
  24267. /* Check a valid public value works! */
  24268. if (wc_curve25519_check_public(good, CURVE25519_KEYSIZE,
  24269. EC25519_LITTLE_ENDIAN) != 0) {
  24270. return -10644;
  24271. }
  24272. if (wc_curve25519_check_public(good, CURVE25519_KEYSIZE,
  24273. EC25519_BIG_ENDIAN) != 0) {
  24274. return -10645;
  24275. }
  24276. return 0;
  24277. }
  24278. #endif /* HAVE_CURVE25519_SHARED_SECRET && HAVE_CURVE25519_KEY_IMPORT */
  24279. #if !defined(NO_ASN) && defined(HAVE_CURVE25519_KEY_EXPORT) && \
  24280. defined(HAVE_CURVE25519_KEY_IMPORT)
  24281. static int curve255519_der_test(void)
  24282. {
  24283. int ret = 0;
  24284. /* certs/statickeys/x25519.der */
  24285. const byte kCurve25519PrivDer[] = {
  24286. 0x30, 0x2E, 0x02, 0x01, 0x00, 0x30, 0x05, 0x06, 0x03, 0x2B, 0x65, 0x6E,
  24287. 0x04, 0x22, 0x04, 0x20, 0x78, 0x8E, 0x31, 0x5C, 0x33, 0xA9, 0x19, 0xC0,
  24288. 0x5E, 0x36, 0x70, 0x1B, 0xA4, 0xE8, 0xEF, 0xC1, 0x89, 0x8C, 0xB3, 0x15,
  24289. 0xC6, 0x79, 0xD3, 0xAC, 0x22, 0x00, 0xAE, 0xFA, 0xB3, 0xB7, 0x0F, 0x78
  24290. };
  24291. /* certs/statickeys/x25519-pub.der */
  24292. const byte kCurve25519PubDer[] = {
  24293. 0x30, 0x2A, 0x30, 0x05, 0x06, 0x03, 0x2B, 0x65, 0x6E, 0x03, 0x21, 0x00,
  24294. 0x09, 0xBC, 0x8C, 0xC7, 0x45, 0x0D, 0xC1, 0xC2, 0x02, 0x57, 0x9A, 0x68,
  24295. 0x3A, 0xFD, 0x7A, 0xA8, 0xA5, 0x2F, 0xF0, 0x99, 0x39, 0x98, 0xEA, 0x26,
  24296. 0xA2, 0x5B, 0x38, 0xFD, 0x96, 0xDB, 0x2A, 0x26
  24297. };
  24298. curve25519_key key;
  24299. byte output[128];
  24300. word32 outputSz = 128;
  24301. word32 idx;
  24302. if (wc_curve25519_init_ex(&key, HEAP_HINT, devId) != 0) {
  24303. return -10723;
  24304. }
  24305. /* Test decode / encode of Curve25519 private key only */
  24306. if (ret == 0) {
  24307. idx = 0;
  24308. ret = wc_Curve25519PrivateKeyDecode(kCurve25519PrivDer, &idx, &key,
  24309. (word32)sizeof(kCurve25519PrivDer));
  24310. }
  24311. if (ret == 0) {
  24312. outputSz = (word32)sizeof(output);
  24313. ret = wc_Curve25519PrivateKeyToDer(&key, output, outputSz);
  24314. if (ret >= 0) {
  24315. outputSz = ret;
  24316. ret = 0;
  24317. }
  24318. else {
  24319. ret = -10724;
  24320. }
  24321. }
  24322. if (ret == 0 && (outputSz != (word32)sizeof(kCurve25519PrivDer) ||
  24323. XMEMCMP(output, kCurve25519PrivDer, outputSz) != 0)) {
  24324. ret = -10725;
  24325. }
  24326. /* Test decode / encode of Curve25519 public key only */
  24327. if (ret == 0) {
  24328. idx = 0;
  24329. ret = wc_Curve25519PublicKeyDecode(kCurve25519PubDer, &idx, &key,
  24330. (word32)sizeof(kCurve25519PubDer));
  24331. }
  24332. if (ret == 0) {
  24333. outputSz = (word32)sizeof(output);
  24334. ret = wc_Curve25519PublicKeyToDer(&key, output, outputSz, 1);
  24335. if (ret >= 0) {
  24336. outputSz = ret;
  24337. ret = 0;
  24338. }
  24339. else {
  24340. ret = -10726;
  24341. }
  24342. }
  24343. if (ret == 0 && (outputSz != (word32)sizeof(kCurve25519PubDer) ||
  24344. XMEMCMP(output, kCurve25519PubDer, outputSz) != 0)) {
  24345. ret = -10727;
  24346. }
  24347. wc_curve25519_free(&key);
  24348. return ret;
  24349. }
  24350. #endif /* !NO_ASN && HAVE_CURVE25519_KEY_EXPORT && HAVE_CURVE25519_KEY_IMPORT */
  24351. WOLFSSL_TEST_SUBROUTINE int curve25519_test(void)
  24352. {
  24353. WC_RNG rng;
  24354. int ret;
  24355. #ifdef HAVE_CURVE25519_SHARED_SECRET
  24356. byte sharedA[32];
  24357. byte sharedB[32];
  24358. word32 y;
  24359. #endif
  24360. #ifdef HAVE_CURVE25519_KEY_EXPORT
  24361. byte exportBuf[32];
  24362. #endif
  24363. word32 x = 0;
  24364. curve25519_key userA, userB, pubKey;
  24365. #if defined(HAVE_CURVE25519_SHARED_SECRET) && \
  24366. defined(HAVE_CURVE25519_KEY_IMPORT)
  24367. /* test vectors from
  24368. https://tools.ietf.org/html/draft-josefsson-tls-curve25519-03
  24369. */
  24370. /* secret key for party a */
  24371. byte sa[] = {
  24372. 0x5A,0xC9,0x9F,0x33,0x63,0x2E,0x5A,0x76,
  24373. 0x8D,0xE7,0xE8,0x1B,0xF8,0x54,0xC2,0x7C,
  24374. 0x46,0xE3,0xFB,0xF2,0xAB,0xBA,0xCD,0x29,
  24375. 0xEC,0x4A,0xFF,0x51,0x73,0x69,0xC6,0x60
  24376. };
  24377. /* public key for party a */
  24378. byte pa[] = {
  24379. 0x05,0x7E,0x23,0xEA,0x9F,0x1C,0xBE,0x8A,
  24380. 0x27,0x16,0x8F,0x6E,0x69,0x6A,0x79,0x1D,
  24381. 0xE6,0x1D,0xD3,0xAF,0x7A,0xCD,0x4E,0xEA,
  24382. 0xCC,0x6E,0x7B,0xA5,0x14,0xFD,0xA8,0x63
  24383. };
  24384. /* secret key for party b */
  24385. byte sb[] = {
  24386. 0x47,0xDC,0x3D,0x21,0x41,0x74,0x82,0x0E,
  24387. 0x11,0x54,0xB4,0x9B,0xC6,0xCD,0xB2,0xAB,
  24388. 0xD4,0x5E,0xE9,0x58,0x17,0x05,0x5D,0x25,
  24389. 0x5A,0xA3,0x58,0x31,0xB7,0x0D,0x32,0x60
  24390. };
  24391. /* public key for party b */
  24392. byte pb[] = {
  24393. 0x6E,0xB8,0x9D,0xA9,0x19,0x89,0xAE,0x37,
  24394. 0xC7,0xEA,0xC7,0x61,0x8D,0x9E,0x5C,0x49,
  24395. 0x51,0xDB,0xA1,0xD7,0x3C,0x28,0x5A,0xE1,
  24396. 0xCD,0x26,0xA8,0x55,0x02,0x0E,0xEF,0x04
  24397. };
  24398. /* expected shared key */
  24399. byte ss[] = {
  24400. 0x61,0x45,0x0C,0xD9,0x8E,0x36,0x01,0x6B,
  24401. 0x58,0x77,0x6A,0x89,0x7A,0x9F,0x0A,0xEF,
  24402. 0x73,0x8B,0x99,0xF0,0x94,0x68,0xB8,0xD6,
  24403. 0xB8,0x51,0x11,0x84,0xD5,0x34,0x94,0xAB
  24404. };
  24405. #endif /* HAVE_CURVE25519_SHARED_SECRET */
  24406. (void)x;
  24407. #ifndef HAVE_FIPS
  24408. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  24409. #else
  24410. ret = wc_InitRng(&rng);
  24411. #endif
  24412. if (ret != 0)
  24413. return -10700;
  24414. wc_curve25519_init_ex(&userA, HEAP_HINT, devId);
  24415. wc_curve25519_init_ex(&userB, HEAP_HINT, devId);
  24416. wc_curve25519_init_ex(&pubKey, HEAP_HINT, devId);
  24417. /* make curve25519 keys */
  24418. if (wc_curve25519_make_key(&rng, 32, &userA) != 0)
  24419. return -10701;
  24420. if (wc_curve25519_make_key(&rng, 32, &userB) != 0)
  24421. return -10702;
  24422. #ifdef HAVE_CURVE25519_SHARED_SECRET
  24423. /* find shared secret key */
  24424. x = sizeof(sharedA);
  24425. if ((ret = wc_curve25519_shared_secret(&userA, &userB, sharedA, &x)) != 0) {
  24426. printf("wc_curve25519_shared_secret 1 %d\n", ret);
  24427. return -10703;
  24428. }
  24429. y = sizeof(sharedB);
  24430. if ((ret = wc_curve25519_shared_secret(&userB, &userA, sharedB, &y)) != 0) {
  24431. printf("wc_curve25519_shared_secret 2 %d\n", ret);
  24432. return -10704;
  24433. }
  24434. /* compare shared secret keys to test they are the same */
  24435. if (y != x)
  24436. return -10705;
  24437. if (XMEMCMP(sharedA, sharedB, x))
  24438. return -10706;
  24439. #endif
  24440. #ifdef HAVE_CURVE25519_KEY_EXPORT
  24441. /* export a public key and import it for another user */
  24442. x = sizeof(exportBuf);
  24443. if (wc_curve25519_export_public(&userA, exportBuf, &x) != 0)
  24444. return -10707;
  24445. #ifdef HAVE_CURVE25519_KEY_IMPORT
  24446. if (wc_curve25519_import_public(exportBuf, x, &pubKey) != 0)
  24447. return -10708;
  24448. #endif
  24449. #endif
  24450. #if defined(HAVE_CURVE25519_SHARED_SECRET) && \
  24451. defined(HAVE_CURVE25519_KEY_IMPORT)
  24452. /* test shared key after importing a public key */
  24453. XMEMSET(sharedB, 0, sizeof(sharedB));
  24454. y = sizeof(sharedB);
  24455. if (wc_curve25519_shared_secret(&userB, &pubKey, sharedB, &y) != 0)
  24456. return -10709;
  24457. if (XMEMCMP(sharedA, sharedB, y))
  24458. return -10710;
  24459. /* import RFC test vectors and compare shared key */
  24460. if (wc_curve25519_import_private_raw(sa, sizeof(sa), pa, sizeof(pa), &userA)
  24461. != 0)
  24462. return -10711;
  24463. if (wc_curve25519_import_private_raw(sb, sizeof(sb), pb, sizeof(pb), &userB)
  24464. != 0)
  24465. return -10712;
  24466. /* test against known test vector */
  24467. XMEMSET(sharedB, 0, sizeof(sharedB));
  24468. y = sizeof(sharedB);
  24469. if (wc_curve25519_shared_secret(&userA, &userB, sharedB, &y) != 0)
  24470. return -10713;
  24471. if (XMEMCMP(ss, sharedB, y))
  24472. return -10714;
  24473. /* test swapping roles of keys and generating same shared key */
  24474. XMEMSET(sharedB, 0, sizeof(sharedB));
  24475. y = sizeof(sharedB);
  24476. if (wc_curve25519_shared_secret(&userB, &userA, sharedB, &y) != 0)
  24477. return -10715;
  24478. if (XMEMCMP(ss, sharedB, y))
  24479. return -10716;
  24480. /* test with 1 generated key and 1 from known test vector */
  24481. if (wc_curve25519_import_private_raw(sa, sizeof(sa), pa, sizeof(pa), &userA)
  24482. != 0)
  24483. return -10717;
  24484. wc_curve25519_free(&userB);
  24485. wc_curve25519_init_ex(&userB, HEAP_HINT, devId);
  24486. if (wc_curve25519_make_key(&rng, 32, &userB) != 0)
  24487. return -10718;
  24488. x = sizeof(sharedA);
  24489. if (wc_curve25519_shared_secret(&userA, &userB, sharedA, &x) != 0)
  24490. return -10719;
  24491. y = sizeof(sharedB);
  24492. if (wc_curve25519_shared_secret(&userB, &userA, sharedB, &y) != 0)
  24493. return -10720;
  24494. /* compare shared secret keys to test they are the same */
  24495. if (y != x)
  24496. return -10721;
  24497. if (XMEMCMP(sharedA, sharedB, x))
  24498. return -10722;
  24499. ret = curve25519_overflow_test();
  24500. if (ret != 0)
  24501. return ret;
  24502. ret = curve25519_check_public_test();
  24503. if (ret != 0)
  24504. return ret;
  24505. #endif /* HAVE_CURVE25519_SHARED_SECRET && HAVE_CURVE25519_KEY_IMPORT */
  24506. #if !defined(NO_ASN) && defined(HAVE_CURVE25519_KEY_EXPORT) && \
  24507. defined(HAVE_CURVE25519_KEY_IMPORT)
  24508. ret = curve255519_der_test();
  24509. if (ret != 0)
  24510. return ret;
  24511. #endif
  24512. /* clean up keys when done */
  24513. wc_curve25519_free(&pubKey);
  24514. wc_curve25519_free(&userB);
  24515. wc_curve25519_free(&userA);
  24516. wc_FreeRng(&rng);
  24517. return 0;
  24518. }
  24519. #endif /* HAVE_CURVE25519 */
  24520. #ifdef HAVE_ED25519
  24521. #ifdef WOLFSSL_TEST_CERT
  24522. static int ed25519_test_cert(void)
  24523. {
  24524. DecodedCert cert[2];
  24525. DecodedCert* serverCert = NULL;
  24526. DecodedCert* caCert = NULL;
  24527. #ifdef HAVE_ED25519_VERIFY
  24528. ed25519_key key;
  24529. ed25519_key* pubKey = NULL;
  24530. int verify;
  24531. #endif /* HAVE_ED25519_VERIFY */
  24532. int ret;
  24533. byte* tmp;
  24534. size_t bytes;
  24535. XFILE file;
  24536. tmp = (byte *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24537. if (tmp == NULL) {
  24538. ERROR_OUT(-10730, done);
  24539. }
  24540. #ifdef USE_CERT_BUFFERS_256
  24541. XMEMCPY(tmp, ca_ed25519_cert, sizeof_ca_ed25519_cert);
  24542. bytes = sizeof_ca_ed25519_cert;
  24543. #elif !defined(NO_FILESYSTEM)
  24544. file = XFOPEN(caEd25519Cert, "rb");
  24545. if (file == NULL) {
  24546. ERROR_OUT(-10731, done);
  24547. }
  24548. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  24549. XFCLOSE(file);
  24550. #else
  24551. /* No certificate to use. */
  24552. ERROR_OUT(-10732, done);
  24553. #endif
  24554. InitDecodedCert(&cert[0], tmp, (word32)bytes, 0);
  24555. caCert = &cert[0];
  24556. ret = ParseCert(caCert, CERT_TYPE, NO_VERIFY, NULL);
  24557. if (ret != 0) {
  24558. ERROR_OUT(-10733, done);
  24559. }
  24560. #ifdef USE_CERT_BUFFERS_256
  24561. XMEMCPY(tmp, server_ed25519_cert, sizeof_server_ed25519_cert);
  24562. bytes = sizeof_server_ed25519_cert;
  24563. #elif !defined(NO_FILESYSTEM)
  24564. file = XFOPEN(serverEd25519Cert, "rb");
  24565. if (file == NULL) {
  24566. ERROR_OUT(-10734, done);
  24567. }
  24568. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  24569. XFCLOSE(file);
  24570. #else
  24571. /* No certificate to use. */
  24572. ERROR_OUT(-10735, done);
  24573. #endif
  24574. InitDecodedCert(&cert[1], tmp, (word32)bytes, 0);
  24575. serverCert = &cert[1];
  24576. ret = ParseCert(serverCert, CERT_TYPE, NO_VERIFY, NULL);
  24577. if (ret != 0) {
  24578. ERROR_OUT(-10736, done);
  24579. }
  24580. #ifdef HAVE_ED25519_VERIFY
  24581. ret = wc_ed25519_init(&key);
  24582. if (ret < 0) {
  24583. ERROR_OUT(-10737, done);
  24584. }
  24585. pubKey = &key;
  24586. ret = wc_ed25519_import_public(caCert->publicKey, caCert->pubKeySize,
  24587. pubKey);
  24588. if (ret < 0) {
  24589. ERROR_OUT(-10738, done);
  24590. }
  24591. if (wc_ed25519_verify_msg(serverCert->signature, serverCert->sigLength,
  24592. serverCert->source + serverCert->certBegin,
  24593. serverCert->sigIndex - serverCert->certBegin,
  24594. &verify, pubKey) < 0 || verify != 1) {
  24595. ERROR_OUT(-10739, done);
  24596. }
  24597. #endif /* HAVE_ED25519_VERIFY */
  24598. done:
  24599. if (tmp != NULL)
  24600. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24601. #ifdef HAVE_ED25519_VERIFY
  24602. wc_ed25519_free(pubKey);
  24603. #endif /* HAVE_ED25519_VERIFY */
  24604. if (caCert != NULL)
  24605. FreeDecodedCert(caCert);
  24606. if (serverCert != NULL)
  24607. FreeDecodedCert(serverCert);
  24608. return ret;
  24609. }
  24610. static int ed25519_test_make_cert(void)
  24611. {
  24612. WC_RNG rng;
  24613. Cert cert;
  24614. DecodedCert decode;
  24615. ed25519_key key;
  24616. ed25519_key* privKey = NULL;
  24617. int ret = 0;
  24618. byte* tmp = NULL;
  24619. wc_InitCert_ex(&cert, HEAP_HINT, devId);
  24620. #ifndef HAVE_FIPS
  24621. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  24622. #else
  24623. ret = wc_InitRng(&rng);
  24624. #endif
  24625. if (ret != 0)
  24626. return -10750;
  24627. wc_ed25519_init(&key);
  24628. privKey = &key;
  24629. wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, privKey);
  24630. cert.daysValid = 365 * 2;
  24631. cert.selfSigned = 1;
  24632. XMEMCPY(&cert.issuer, &certDefaultName, sizeof(CertName));
  24633. XMEMCPY(&cert.subject, &certDefaultName, sizeof(CertName));
  24634. cert.isCA = 0;
  24635. #ifdef WOLFSSL_CERT_EXT
  24636. ret = wc_SetKeyUsage(&cert, certKeyUsage);
  24637. if (ret < 0) {
  24638. ERROR_OUT(-10751, done);
  24639. }
  24640. ret = wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ED25519_TYPE, privKey);
  24641. if (ret < 0) {
  24642. ERROR_OUT(-10752, done);
  24643. }
  24644. ret = wc_SetAuthKeyIdFromPublicKey_ex(&cert, ED25519_TYPE, privKey);
  24645. if (ret < 0) {
  24646. ERROR_OUT(-10753, done);
  24647. }
  24648. #endif
  24649. tmp = (byte *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24650. if (tmp == NULL) {
  24651. ERROR_OUT(-10754, done);
  24652. }
  24653. cert.sigType = CTC_ED25519;
  24654. ret = wc_MakeCert_ex(&cert, tmp, FOURK_BUF, ED25519_TYPE, privKey, &rng);
  24655. if (ret < 0) {
  24656. ERROR_OUT(-10755, done);
  24657. }
  24658. ret = wc_SignCert_ex(cert.bodySz, cert.sigType, tmp, FOURK_BUF,
  24659. ED25519_TYPE, privKey, &rng);
  24660. if (ret < 0) {
  24661. ERROR_OUT(-10756, done);
  24662. }
  24663. InitDecodedCert(&decode, tmp, ret, HEAP_HINT);
  24664. ret = ParseCert(&decode, CERT_TYPE, NO_VERIFY, 0);
  24665. FreeDecodedCert(&decode);
  24666. if (ret != 0) {
  24667. ERROR_OUT(-10757, done);
  24668. }
  24669. done:
  24670. if (tmp != NULL)
  24671. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24672. wc_ed25519_free(privKey);
  24673. wc_FreeRng(&rng);
  24674. return ret;
  24675. }
  24676. #endif /* WOLFSSL_TEST_CERT */
  24677. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_EXPORT) && \
  24678. defined(HAVE_ED25519_KEY_IMPORT)
  24679. static int ed25519ctx_test(void)
  24680. {
  24681. int ret;
  24682. byte out[ED25519_SIG_SIZE];
  24683. word32 outlen;
  24684. #ifdef HAVE_ED25519_VERIFY
  24685. int verify = 0;
  24686. #endif /* HAVE_ED25519_VERIFY */
  24687. ed25519_key key;
  24688. WOLFSSL_SMALL_STACK_STATIC const byte sKeyCtx[] = {
  24689. 0x03,0x05,0x33,0x4e,0x38,0x1a,0xf7,0x8f,
  24690. 0x14,0x1c,0xb6,0x66,0xf6,0x19,0x9f,0x57,
  24691. 0xbc,0x34,0x95,0x33,0x5a,0x25,0x6a,0x95,
  24692. 0xbd,0x2a,0x55,0xbf,0x54,0x66,0x63,0xf6
  24693. };
  24694. WOLFSSL_SMALL_STACK_STATIC const byte pKeyCtx[] = {
  24695. 0xdf,0xc9,0x42,0x5e,0x4f,0x96,0x8f,0x7f,
  24696. 0x0c,0x29,0xf0,0x25,0x9c,0xf5,0xf9,0xae,
  24697. 0xd6,0x85,0x1c,0x2b,0xb4,0xad,0x8b,0xfb,
  24698. 0x86,0x0c,0xfe,0xe0,0xab,0x24,0x82,0x92
  24699. };
  24700. WOLFSSL_SMALL_STACK_STATIC const byte sigCtx1[] = {
  24701. 0x55,0xa4,0xcc,0x2f,0x70,0xa5,0x4e,0x04,
  24702. 0x28,0x8c,0x5f,0x4c,0xd1,0xe4,0x5a,0x7b,
  24703. 0xb5,0x20,0xb3,0x62,0x92,0x91,0x18,0x76,
  24704. 0xca,0xda,0x73,0x23,0x19,0x8d,0xd8,0x7a,
  24705. 0x8b,0x36,0x95,0x0b,0x95,0x13,0x00,0x22,
  24706. 0x90,0x7a,0x7f,0xb7,0xc4,0xe9,0xb2,0xd5,
  24707. 0xf6,0xcc,0xa6,0x85,0xa5,0x87,0xb4,0xb2,
  24708. 0x1f,0x4b,0x88,0x8e,0x4e,0x7e,0xdb,0x0d
  24709. };
  24710. WOLFSSL_SMALL_STACK_STATIC const byte sigCtx2[] = {
  24711. 0xcc,0x5e,0x63,0xa2,0x7e,0x94,0xaf,0xd3,
  24712. 0x41,0x83,0x38,0xd2,0x48,0x6f,0xa9,0x2a,
  24713. 0xf9,0x91,0x7c,0x2d,0x98,0x9e,0x06,0xe5,
  24714. 0x02,0x77,0x72,0x1c,0x34,0x38,0x18,0xb4,
  24715. 0x21,0x96,0xbc,0x29,0x2e,0x68,0xf3,0x4d,
  24716. 0x85,0x9b,0xbe,0xad,0x17,0x9f,0x54,0x54,
  24717. 0x2d,0x4b,0x04,0xdc,0xfb,0xfa,0x4a,0x68,
  24718. 0x4e,0x39,0x50,0xfb,0x1c,0xcd,0x8d,0x0d
  24719. };
  24720. WOLFSSL_SMALL_STACK_STATIC const byte msgCtx[] = {
  24721. 0xf7,0x26,0x93,0x6d,0x19,0xc8,0x00,0x49,
  24722. 0x4e,0x3f,0xda,0xff,0x20,0xb2,0x76,0xa8
  24723. };
  24724. WOLFSSL_SMALL_STACK_STATIC const byte contextCtx[] = {
  24725. 0x66,0x6f,0x6f
  24726. };
  24727. outlen = sizeof(out);
  24728. XMEMSET(out, 0, sizeof(out));
  24729. ret = wc_ed25519_init_ex(&key, HEAP_HINT, devId);
  24730. if (ret != 0)
  24731. return 10800;
  24732. ret = wc_ed25519_import_private_key(sKeyCtx, ED25519_KEY_SIZE, pKeyCtx,
  24733. sizeof(pKeyCtx), &key);
  24734. if (ret == 0)
  24735. ret = wc_ed25519ctx_sign_msg(msgCtx, sizeof(msgCtx), out, &outlen, &key,
  24736. contextCtx, sizeof(contextCtx));
  24737. if (ret == 0 && XMEMCMP(out, sigCtx1, 64) != 0)
  24738. ret = -10801;
  24739. #if defined(HAVE_ED25519_VERIFY)
  24740. /* test verify on good msg */
  24741. if (ret == 0)
  24742. ret = wc_ed25519ctx_verify_msg(out, outlen, msgCtx, sizeof(msgCtx),
  24743. &verify, &key, contextCtx, sizeof(contextCtx));
  24744. if (ret == 0 && verify != 1)
  24745. ret = -10802;
  24746. #endif
  24747. if (ret == 0)
  24748. ret = wc_ed25519ctx_sign_msg(msgCtx, sizeof(msgCtx), out, &outlen, &key,
  24749. NULL, 0);
  24750. if (ret == 0 && XMEMCMP(out, sigCtx2, 64) != 0)
  24751. ret = -10803;
  24752. #if defined(HAVE_ED25519_VERIFY)
  24753. /* test verify on good msg */
  24754. if (ret == 0)
  24755. ret = wc_ed25519ctx_verify_msg(out, outlen, msgCtx, sizeof(msgCtx),
  24756. &verify, &key, NULL, 0);
  24757. if (ret == 0 && verify != 1)
  24758. ret = -10804;
  24759. #endif
  24760. wc_ed25519_free(&key);
  24761. return ret;
  24762. }
  24763. static int ed25519ph_test(void)
  24764. {
  24765. int ret = 0;
  24766. byte out[ED25519_SIG_SIZE];
  24767. word32 outlen;
  24768. #ifdef HAVE_ED25519_VERIFY
  24769. int verify = 0;
  24770. #endif /* HAVE_ED25519_VERIFY */
  24771. ed25519_key key;
  24772. WOLFSSL_SMALL_STACK_STATIC const byte sKeyPh[] = {
  24773. 0x83,0x3f,0xe6,0x24,0x09,0x23,0x7b,0x9d,
  24774. 0x62,0xec,0x77,0x58,0x75,0x20,0x91,0x1e,
  24775. 0x9a,0x75,0x9c,0xec,0x1d,0x19,0x75,0x5b,
  24776. 0x7d,0xa9,0x01,0xb9,0x6d,0xca,0x3d,0x42
  24777. };
  24778. WOLFSSL_SMALL_STACK_STATIC const byte pKeyPh[] = {
  24779. 0xec,0x17,0x2b,0x93,0xad,0x5e,0x56,0x3b,
  24780. 0xf4,0x93,0x2c,0x70,0xe1,0x24,0x50,0x34,
  24781. 0xc3,0x54,0x67,0xef,0x2e,0xfd,0x4d,0x64,
  24782. 0xeb,0xf8,0x19,0x68,0x34,0x67,0xe2,0xbf
  24783. };
  24784. WOLFSSL_SMALL_STACK_STATIC const byte sigPh1[] = {
  24785. 0x98,0xa7,0x02,0x22,0xf0,0xb8,0x12,0x1a,
  24786. 0xa9,0xd3,0x0f,0x81,0x3d,0x68,0x3f,0x80,
  24787. 0x9e,0x46,0x2b,0x46,0x9c,0x7f,0xf8,0x76,
  24788. 0x39,0x49,0x9b,0xb9,0x4e,0x6d,0xae,0x41,
  24789. 0x31,0xf8,0x50,0x42,0x46,0x3c,0x2a,0x35,
  24790. 0x5a,0x20,0x03,0xd0,0x62,0xad,0xf5,0xaa,
  24791. 0xa1,0x0b,0x8c,0x61,0xe6,0x36,0x06,0x2a,
  24792. 0xaa,0xd1,0x1c,0x2a,0x26,0x08,0x34,0x06
  24793. };
  24794. WOLFSSL_SMALL_STACK_STATIC const byte sigPh2[] = {
  24795. 0xe0,0x39,0x70,0x2b,0x4c,0x25,0x95,0xa6,
  24796. 0xa5,0x41,0xac,0x85,0x09,0x23,0x6e,0x29,
  24797. 0x90,0x47,0x47,0x95,0x33,0x0c,0x9b,0x34,
  24798. 0xa7,0x5f,0x58,0xa6,0x60,0x12,0x9e,0x08,
  24799. 0xfd,0x73,0x69,0x43,0xfb,0x19,0x43,0xa5,
  24800. 0x57,0x20,0xb9,0xe0,0x95,0x7b,0x1e,0xd6,
  24801. 0x73,0x48,0x16,0x61,0x9f,0x13,0x88,0xf4,
  24802. 0x3f,0x73,0xe6,0xe3,0xba,0xa8,0x1c,0x0e
  24803. };
  24804. WOLFSSL_SMALL_STACK_STATIC const byte msgPh[] = {
  24805. 0x61,0x62,0x63
  24806. };
  24807. /* SHA-512 hash of msgPh */
  24808. WOLFSSL_SMALL_STACK_STATIC const byte hashPh[] = {
  24809. 0xdd,0xaf,0x35,0xa1,0x93,0x61,0x7a,0xba,
  24810. 0xcc,0x41,0x73,0x49,0xae,0x20,0x41,0x31,
  24811. 0x12,0xe6,0xfa,0x4e,0x89,0xa9,0x7e,0xa2,
  24812. 0x0a,0x9e,0xee,0xe6,0x4b,0x55,0xd3,0x9a,
  24813. 0x21,0x92,0x99,0x2a,0x27,0x4f,0xc1,0xa8,
  24814. 0x36,0xba,0x3c,0x23,0xa3,0xfe,0xeb,0xbd,
  24815. 0x45,0x4d,0x44,0x23,0x64,0x3c,0xe8,0x0e,
  24816. 0x2a,0x9a,0xc9,0x4f,0xa5,0x4c,0xa4,0x9f
  24817. };
  24818. WOLFSSL_SMALL_STACK_STATIC const byte contextPh2[] = {
  24819. 0x66,0x6f,0x6f
  24820. };
  24821. outlen = sizeof(out);
  24822. XMEMSET(out, 0, sizeof(out));
  24823. ret = wc_ed25519_init_ex(&key, HEAP_HINT, devId);
  24824. if (ret != 0)
  24825. return -10900;
  24826. ret = wc_ed25519_import_private_key(sKeyPh, ED25519_KEY_SIZE, pKeyPh,
  24827. sizeof(pKeyPh), &key);
  24828. if (ret == 0)
  24829. ret = wc_ed25519ph_sign_msg(msgPh, sizeof(msgPh), out, &outlen, &key,
  24830. NULL, 0);
  24831. if (ret == 0 && XMEMCMP(out, sigPh1, 64) != 0)
  24832. ret = -10901;
  24833. #if defined(HAVE_ED25519_VERIFY)
  24834. /* test verify on good msg */
  24835. if (ret == 0)
  24836. ret = wc_ed25519ph_verify_msg(out, outlen, msgPh, sizeof(msgPh),
  24837. &verify, &key, NULL, 0);
  24838. if (ret == 0 && verify != 1)
  24839. ret = -10902;
  24840. #endif
  24841. if (ret == 0)
  24842. ret = wc_ed25519ph_sign_msg(msgPh, sizeof(msgPh), out, &outlen, &key,
  24843. contextPh2, sizeof(contextPh2));
  24844. if (ret == 0 && XMEMCMP(out, sigPh2, 64) != 0)
  24845. ret = -10903;
  24846. #if defined(HAVE_ED25519_VERIFY)
  24847. /* test verify on good msg */
  24848. if (ret == 0)
  24849. ret = wc_ed25519ph_verify_msg(out, outlen, msgPh, sizeof(msgPh), &verify,
  24850. &key, contextPh2, sizeof(contextPh2));
  24851. if (ret == 0 && verify != 1)
  24852. ret = -10904;
  24853. #endif
  24854. if (ret == 0)
  24855. ret = wc_ed25519ph_sign_hash(hashPh, sizeof(hashPh), out, &outlen, &key,
  24856. NULL, 0);
  24857. if (ret == 0 && XMEMCMP(out, sigPh1, 64) != 0)
  24858. ret = -10905;
  24859. #if defined(HAVE_ED25519_VERIFY)
  24860. if (ret == 0)
  24861. ret = wc_ed25519ph_verify_hash(out, outlen, hashPh, sizeof(hashPh),
  24862. &verify, &key, NULL, 0);
  24863. if (ret == 0 && verify != 1)
  24864. ret = -10906;
  24865. #endif
  24866. if (ret == 0)
  24867. ret = wc_ed25519ph_sign_hash(hashPh, sizeof(hashPh), out, &outlen, &key,
  24868. contextPh2, sizeof(contextPh2));
  24869. if (ret == 0 && XMEMCMP(out, sigPh2, 64) != 0)
  24870. ret = -10907;
  24871. #if defined(HAVE_ED25519_VERIFY)
  24872. if (ret == 0)
  24873. ret = wc_ed25519ph_verify_hash(out, outlen, hashPh, sizeof(hashPh), &verify,
  24874. &key, contextPh2, sizeof(contextPh2));
  24875. if (ret == 0 && verify != 1)
  24876. ret = -10908;
  24877. #endif
  24878. wc_ed25519_free(&key);
  24879. return ret;
  24880. }
  24881. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_EXPORT && HAVE_ED25519_KEY_IMPORT */
  24882. WOLFSSL_TEST_SUBROUTINE int ed25519_test(void)
  24883. {
  24884. int ret;
  24885. WC_RNG rng;
  24886. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_EXPORT) &&\
  24887. defined(HAVE_ED25519_KEY_IMPORT)
  24888. byte out[ED25519_SIG_SIZE];
  24889. byte exportPKey[ED25519_KEY_SIZE];
  24890. byte exportSKey[ED25519_KEY_SIZE];
  24891. word32 exportPSz;
  24892. word32 exportSSz;
  24893. int i;
  24894. word32 outlen;
  24895. #ifdef HAVE_ED25519_VERIFY
  24896. #ifdef WOLFSSL_ED25519_STREAMING_VERIFY
  24897. int j;
  24898. #endif
  24899. int verify;
  24900. #endif /* HAVE_ED25519_VERIFY */
  24901. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_EXPORT && HAVE_ED25519_KEY_IMPORT */
  24902. word32 keySz, sigSz;
  24903. ed25519_key key;
  24904. ed25519_key key2;
  24905. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_EXPORT) && \
  24906. defined(HAVE_ED25519_KEY_IMPORT)
  24907. /* test vectors from
  24908. https://tools.ietf.org/html/draft-josefsson-eddsa-ed25519-02
  24909. */
  24910. WOLFSSL_SMALL_STACK_STATIC const byte sKey1[] = {
  24911. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  24912. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  24913. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  24914. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60
  24915. };
  24916. WOLFSSL_SMALL_STACK_STATIC const byte sKey2[] = {
  24917. 0x4c,0xcd,0x08,0x9b,0x28,0xff,0x96,0xda,
  24918. 0x9d,0xb6,0xc3,0x46,0xec,0x11,0x4e,0x0f,
  24919. 0x5b,0x8a,0x31,0x9f,0x35,0xab,0xa6,0x24,
  24920. 0xda,0x8c,0xf6,0xed,0x4f,0xb8,0xa6,0xfb
  24921. };
  24922. WOLFSSL_SMALL_STACK_STATIC const byte sKey3[] = {
  24923. 0xc5,0xaa,0x8d,0xf4,0x3f,0x9f,0x83,0x7b,
  24924. 0xed,0xb7,0x44,0x2f,0x31,0xdc,0xb7,0xb1,
  24925. 0x66,0xd3,0x85,0x35,0x07,0x6f,0x09,0x4b,
  24926. 0x85,0xce,0x3a,0x2e,0x0b,0x44,0x58,0xf7
  24927. };
  24928. /* uncompressed test */
  24929. WOLFSSL_SMALL_STACK_STATIC const byte sKey4[] = {
  24930. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  24931. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  24932. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  24933. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60
  24934. };
  24935. /* compressed prefix test */
  24936. WOLFSSL_SMALL_STACK_STATIC const byte sKey5[] = {
  24937. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  24938. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  24939. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  24940. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60
  24941. };
  24942. WOLFSSL_SMALL_STACK_STATIC const byte sKey6[] = {
  24943. 0xf5,0xe5,0x76,0x7c,0xf1,0x53,0x31,0x95,
  24944. 0x17,0x63,0x0f,0x22,0x68,0x76,0xb8,0x6c,
  24945. 0x81,0x60,0xcc,0x58,0x3b,0xc0,0x13,0x74,
  24946. 0x4c,0x6b,0xf2,0x55,0xf5,0xcc,0x0e,0xe5
  24947. };
  24948. WOLFSSL_SMALL_STACK_STATIC const byte* sKeys[] = {sKey1, sKey2, sKey3, sKey4, sKey5, sKey6};
  24949. WOLFSSL_SMALL_STACK_STATIC const byte pKey1[] = {
  24950. 0xd7,0x5a,0x98,0x01,0x82,0xb1,0x0a,0xb7,
  24951. 0xd5,0x4b,0xfe,0xd3,0xc9,0x64,0x07,0x3a,
  24952. 0x0e,0xe1,0x72,0xf3,0xda,0xa6,0x23,0x25,
  24953. 0xaf,0x02,0x1a,0x68,0xf7,0x07,0x51,0x1a
  24954. };
  24955. WOLFSSL_SMALL_STACK_STATIC const byte pKey2[] = {
  24956. 0x3d,0x40,0x17,0xc3,0xe8,0x43,0x89,0x5a,
  24957. 0x92,0xb7,0x0a,0xa7,0x4d,0x1b,0x7e,0xbc,
  24958. 0x9c,0x98,0x2c,0xcf,0x2e,0xc4,0x96,0x8c,
  24959. 0xc0,0xcd,0x55,0xf1,0x2a,0xf4,0x66,0x0c
  24960. };
  24961. WOLFSSL_SMALL_STACK_STATIC const byte pKey3[] = {
  24962. 0xfc,0x51,0xcd,0x8e,0x62,0x18,0xa1,0xa3,
  24963. 0x8d,0xa4,0x7e,0xd0,0x02,0x30,0xf0,0x58,
  24964. 0x08,0x16,0xed,0x13,0xba,0x33,0x03,0xac,
  24965. 0x5d,0xeb,0x91,0x15,0x48,0x90,0x80,0x25
  24966. };
  24967. /* uncompressed test */
  24968. WOLFSSL_SMALL_STACK_STATIC const byte pKey4[] = {
  24969. 0x04,0x55,0xd0,0xe0,0x9a,0x2b,0x9d,0x34,
  24970. 0x29,0x22,0x97,0xe0,0x8d,0x60,0xd0,0xf6,
  24971. 0x20,0xc5,0x13,0xd4,0x72,0x53,0x18,0x7c,
  24972. 0x24,0xb1,0x27,0x86,0xbd,0x77,0x76,0x45,
  24973. 0xce,0x1a,0x51,0x07,0xf7,0x68,0x1a,0x02,
  24974. 0xaf,0x25,0x23,0xa6,0xda,0xf3,0x72,0xe1,
  24975. 0x0e,0x3a,0x07,0x64,0xc9,0xd3,0xfe,0x4b,
  24976. 0xd5,0xb7,0x0a,0xb1,0x82,0x01,0x98,0x5a,
  24977. 0xd7
  24978. };
  24979. /* compressed prefix */
  24980. WOLFSSL_SMALL_STACK_STATIC const byte pKey5[] = {
  24981. 0x40,0xd7,0x5a,0x98,0x01,0x82,0xb1,0x0a,0xb7,
  24982. 0xd5,0x4b,0xfe,0xd3,0xc9,0x64,0x07,0x3a,
  24983. 0x0e,0xe1,0x72,0xf3,0xda,0xa6,0x23,0x25,
  24984. 0xaf,0x02,0x1a,0x68,0xf7,0x07,0x51,0x1a
  24985. };
  24986. WOLFSSL_SMALL_STACK_STATIC const byte pKey6[] = {
  24987. 0x27,0x81,0x17,0xfc,0x14,0x4c,0x72,0x34,
  24988. 0x0f,0x67,0xd0,0xf2,0x31,0x6e,0x83,0x86,
  24989. 0xce,0xff,0xbf,0x2b,0x24,0x28,0xc9,0xc5,
  24990. 0x1f,0xef,0x7c,0x59,0x7f,0x1d,0x42,0x6e
  24991. };
  24992. WOLFSSL_SMALL_STACK_STATIC const byte* pKeys[] = {pKey1, pKey2, pKey3, pKey4, pKey5, pKey6};
  24993. WOLFSSL_SMALL_STACK_STATIC const byte pKeySz[] = {sizeof(pKey1), sizeof(pKey2), sizeof(pKey3),
  24994. sizeof(pKey4), sizeof(pKey5), sizeof(pKey6)};
  24995. WOLFSSL_SMALL_STACK_STATIC const byte sig1[] = {
  24996. 0xe5,0x56,0x43,0x00,0xc3,0x60,0xac,0x72,
  24997. 0x90,0x86,0xe2,0xcc,0x80,0x6e,0x82,0x8a,
  24998. 0x84,0x87,0x7f,0x1e,0xb8,0xe5,0xd9,0x74,
  24999. 0xd8,0x73,0xe0,0x65,0x22,0x49,0x01,0x55,
  25000. 0x5f,0xb8,0x82,0x15,0x90,0xa3,0x3b,0xac,
  25001. 0xc6,0x1e,0x39,0x70,0x1c,0xf9,0xb4,0x6b,
  25002. 0xd2,0x5b,0xf5,0xf0,0x59,0x5b,0xbe,0x24,
  25003. 0x65,0x51,0x41,0x43,0x8e,0x7a,0x10,0x0b
  25004. };
  25005. WOLFSSL_SMALL_STACK_STATIC const byte sig2[] = {
  25006. 0x92,0xa0,0x09,0xa9,0xf0,0xd4,0xca,0xb8,
  25007. 0x72,0x0e,0x82,0x0b,0x5f,0x64,0x25,0x40,
  25008. 0xa2,0xb2,0x7b,0x54,0x16,0x50,0x3f,0x8f,
  25009. 0xb3,0x76,0x22,0x23,0xeb,0xdb,0x69,0xda,
  25010. 0x08,0x5a,0xc1,0xe4,0x3e,0x15,0x99,0x6e,
  25011. 0x45,0x8f,0x36,0x13,0xd0,0xf1,0x1d,0x8c,
  25012. 0x38,0x7b,0x2e,0xae,0xb4,0x30,0x2a,0xee,
  25013. 0xb0,0x0d,0x29,0x16,0x12,0xbb,0x0c,0x00
  25014. };
  25015. WOLFSSL_SMALL_STACK_STATIC const byte sig3[] = {
  25016. 0x62,0x91,0xd6,0x57,0xde,0xec,0x24,0x02,
  25017. 0x48,0x27,0xe6,0x9c,0x3a,0xbe,0x01,0xa3,
  25018. 0x0c,0xe5,0x48,0xa2,0x84,0x74,0x3a,0x44,
  25019. 0x5e,0x36,0x80,0xd7,0xdb,0x5a,0xc3,0xac,
  25020. 0x18,0xff,0x9b,0x53,0x8d,0x16,0xf2,0x90,
  25021. 0xae,0x67,0xf7,0x60,0x98,0x4d,0xc6,0x59,
  25022. 0x4a,0x7c,0x15,0xe9,0x71,0x6e,0xd2,0x8d,
  25023. 0xc0,0x27,0xbe,0xce,0xea,0x1e,0xc4,0x0a
  25024. };
  25025. /* uncompressed test */
  25026. WOLFSSL_SMALL_STACK_STATIC const byte sig4[] = {
  25027. 0xe5,0x56,0x43,0x00,0xc3,0x60,0xac,0x72,
  25028. 0x90,0x86,0xe2,0xcc,0x80,0x6e,0x82,0x8a,
  25029. 0x84,0x87,0x7f,0x1e,0xb8,0xe5,0xd9,0x74,
  25030. 0xd8,0x73,0xe0,0x65,0x22,0x49,0x01,0x55,
  25031. 0x5f,0xb8,0x82,0x15,0x90,0xa3,0x3b,0xac,
  25032. 0xc6,0x1e,0x39,0x70,0x1c,0xf9,0xb4,0x6b,
  25033. 0xd2,0x5b,0xf5,0xf0,0x59,0x5b,0xbe,0x24,
  25034. 0x65,0x51,0x41,0x43,0x8e,0x7a,0x10,0x0b
  25035. };
  25036. /* compressed prefix */
  25037. WOLFSSL_SMALL_STACK_STATIC const byte sig5[] = {
  25038. 0xe5,0x56,0x43,0x00,0xc3,0x60,0xac,0x72,
  25039. 0x90,0x86,0xe2,0xcc,0x80,0x6e,0x82,0x8a,
  25040. 0x84,0x87,0x7f,0x1e,0xb8,0xe5,0xd9,0x74,
  25041. 0xd8,0x73,0xe0,0x65,0x22,0x49,0x01,0x55,
  25042. 0x5f,0xb8,0x82,0x15,0x90,0xa3,0x3b,0xac,
  25043. 0xc6,0x1e,0x39,0x70,0x1c,0xf9,0xb4,0x6b,
  25044. 0xd2,0x5b,0xf5,0xf0,0x59,0x5b,0xbe,0x24,
  25045. 0x65,0x51,0x41,0x43,0x8e,0x7a,0x10,0x0b
  25046. };
  25047. WOLFSSL_SMALL_STACK_STATIC const byte sig6[] = {
  25048. 0x0a,0xab,0x4c,0x90,0x05,0x01,0xb3,0xe2,
  25049. 0x4d,0x7c,0xdf,0x46,0x63,0x32,0x6a,0x3a,
  25050. 0x87,0xdf,0x5e,0x48,0x43,0xb2,0xcb,0xdb,
  25051. 0x67,0xcb,0xf6,0xe4,0x60,0xfe,0xc3,0x50,
  25052. 0xaa,0x53,0x71,0xb1,0x50,0x8f,0x9f,0x45,
  25053. 0x28,0xec,0xea,0x23,0xc4,0x36,0xd9,0x4b,
  25054. 0x5e,0x8f,0xcd,0x4f,0x68,0x1e,0x30,0xa6,
  25055. 0xac,0x00,0xa9,0x70,0x4a,0x18,0x8a,0x03
  25056. };
  25057. WOLFSSL_SMALL_STACK_STATIC const byte* sigs[] = {sig1, sig2, sig3, sig4, sig5, sig6};
  25058. WOLFSSL_SMALL_STACK_STATIC const byte msg1[] = {0x0 };
  25059. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] = {0x72};
  25060. WOLFSSL_SMALL_STACK_STATIC const byte msg3[] = {0xAF,0x82};
  25061. /* test of a 1024 byte long message */
  25062. WOLFSSL_SMALL_STACK_STATIC const byte msg4[] = {
  25063. 0x08,0xb8,0xb2,0xb7,0x33,0x42,0x42,0x43,
  25064. 0x76,0x0f,0xe4,0x26,0xa4,0xb5,0x49,0x08,
  25065. 0x63,0x21,0x10,0xa6,0x6c,0x2f,0x65,0x91,
  25066. 0xea,0xbd,0x33,0x45,0xe3,0xe4,0xeb,0x98,
  25067. 0xfa,0x6e,0x26,0x4b,0xf0,0x9e,0xfe,0x12,
  25068. 0xee,0x50,0xf8,0xf5,0x4e,0x9f,0x77,0xb1,
  25069. 0xe3,0x55,0xf6,0xc5,0x05,0x44,0xe2,0x3f,
  25070. 0xb1,0x43,0x3d,0xdf,0x73,0xbe,0x84,0xd8,
  25071. 0x79,0xde,0x7c,0x00,0x46,0xdc,0x49,0x96,
  25072. 0xd9,0xe7,0x73,0xf4,0xbc,0x9e,0xfe,0x57,
  25073. 0x38,0x82,0x9a,0xdb,0x26,0xc8,0x1b,0x37,
  25074. 0xc9,0x3a,0x1b,0x27,0x0b,0x20,0x32,0x9d,
  25075. 0x65,0x86,0x75,0xfc,0x6e,0xa5,0x34,0xe0,
  25076. 0x81,0x0a,0x44,0x32,0x82,0x6b,0xf5,0x8c,
  25077. 0x94,0x1e,0xfb,0x65,0xd5,0x7a,0x33,0x8b,
  25078. 0xbd,0x2e,0x26,0x64,0x0f,0x89,0xff,0xbc,
  25079. 0x1a,0x85,0x8e,0xfc,0xb8,0x55,0x0e,0xe3,
  25080. 0xa5,0xe1,0x99,0x8b,0xd1,0x77,0xe9,0x3a,
  25081. 0x73,0x63,0xc3,0x44,0xfe,0x6b,0x19,0x9e,
  25082. 0xe5,0xd0,0x2e,0x82,0xd5,0x22,0xc4,0xfe,
  25083. 0xba,0x15,0x45,0x2f,0x80,0x28,0x8a,0x82,
  25084. 0x1a,0x57,0x91,0x16,0xec,0x6d,0xad,0x2b,
  25085. 0x3b,0x31,0x0d,0xa9,0x03,0x40,0x1a,0xa6,
  25086. 0x21,0x00,0xab,0x5d,0x1a,0x36,0x55,0x3e,
  25087. 0x06,0x20,0x3b,0x33,0x89,0x0c,0xc9,0xb8,
  25088. 0x32,0xf7,0x9e,0xf8,0x05,0x60,0xcc,0xb9,
  25089. 0xa3,0x9c,0xe7,0x67,0x96,0x7e,0xd6,0x28,
  25090. 0xc6,0xad,0x57,0x3c,0xb1,0x16,0xdb,0xef,
  25091. 0xef,0xd7,0x54,0x99,0xda,0x96,0xbd,0x68,
  25092. 0xa8,0xa9,0x7b,0x92,0x8a,0x8b,0xbc,0x10,
  25093. 0x3b,0x66,0x21,0xfc,0xde,0x2b,0xec,0xa1,
  25094. 0x23,0x1d,0x20,0x6b,0xe6,0xcd,0x9e,0xc7,
  25095. 0xaf,0xf6,0xf6,0xc9,0x4f,0xcd,0x72,0x04,
  25096. 0xed,0x34,0x55,0xc6,0x8c,0x83,0xf4,0xa4,
  25097. 0x1d,0xa4,0xaf,0x2b,0x74,0xef,0x5c,0x53,
  25098. 0xf1,0xd8,0xac,0x70,0xbd,0xcb,0x7e,0xd1,
  25099. 0x85,0xce,0x81,0xbd,0x84,0x35,0x9d,0x44,
  25100. 0x25,0x4d,0x95,0x62,0x9e,0x98,0x55,0xa9,
  25101. 0x4a,0x7c,0x19,0x58,0xd1,0xf8,0xad,0xa5,
  25102. 0xd0,0x53,0x2e,0xd8,0xa5,0xaa,0x3f,0xb2,
  25103. 0xd1,0x7b,0xa7,0x0e,0xb6,0x24,0x8e,0x59,
  25104. 0x4e,0x1a,0x22,0x97,0xac,0xbb,0xb3,0x9d,
  25105. 0x50,0x2f,0x1a,0x8c,0x6e,0xb6,0xf1,0xce,
  25106. 0x22,0xb3,0xde,0x1a,0x1f,0x40,0xcc,0x24,
  25107. 0x55,0x41,0x19,0xa8,0x31,0xa9,0xaa,0xd6,
  25108. 0x07,0x9c,0xad,0x88,0x42,0x5d,0xe6,0xbd,
  25109. 0xe1,0xa9,0x18,0x7e,0xbb,0x60,0x92,0xcf,
  25110. 0x67,0xbf,0x2b,0x13,0xfd,0x65,0xf2,0x70,
  25111. 0x88,0xd7,0x8b,0x7e,0x88,0x3c,0x87,0x59,
  25112. 0xd2,0xc4,0xf5,0xc6,0x5a,0xdb,0x75,0x53,
  25113. 0x87,0x8a,0xd5,0x75,0xf9,0xfa,0xd8,0x78,
  25114. 0xe8,0x0a,0x0c,0x9b,0xa6,0x3b,0xcb,0xcc,
  25115. 0x27,0x32,0xe6,0x94,0x85,0xbb,0xc9,0xc9,
  25116. 0x0b,0xfb,0xd6,0x24,0x81,0xd9,0x08,0x9b,
  25117. 0xec,0xcf,0x80,0xcf,0xe2,0xdf,0x16,0xa2,
  25118. 0xcf,0x65,0xbd,0x92,0xdd,0x59,0x7b,0x07,
  25119. 0x07,0xe0,0x91,0x7a,0xf4,0x8b,0xbb,0x75,
  25120. 0xfe,0xd4,0x13,0xd2,0x38,0xf5,0x55,0x5a,
  25121. 0x7a,0x56,0x9d,0x80,0xc3,0x41,0x4a,0x8d,
  25122. 0x08,0x59,0xdc,0x65,0xa4,0x61,0x28,0xba,
  25123. 0xb2,0x7a,0xf8,0x7a,0x71,0x31,0x4f,0x31,
  25124. 0x8c,0x78,0x2b,0x23,0xeb,0xfe,0x80,0x8b,
  25125. 0x82,0xb0,0xce,0x26,0x40,0x1d,0x2e,0x22,
  25126. 0xf0,0x4d,0x83,0xd1,0x25,0x5d,0xc5,0x1a,
  25127. 0xdd,0xd3,0xb7,0x5a,0x2b,0x1a,0xe0,0x78,
  25128. 0x45,0x04,0xdf,0x54,0x3a,0xf8,0x96,0x9b,
  25129. 0xe3,0xea,0x70,0x82,0xff,0x7f,0xc9,0x88,
  25130. 0x8c,0x14,0x4d,0xa2,0xaf,0x58,0x42,0x9e,
  25131. 0xc9,0x60,0x31,0xdb,0xca,0xd3,0xda,0xd9,
  25132. 0xaf,0x0d,0xcb,0xaa,0xaf,0x26,0x8c,0xb8,
  25133. 0xfc,0xff,0xea,0xd9,0x4f,0x3c,0x7c,0xa4,
  25134. 0x95,0xe0,0x56,0xa9,0xb4,0x7a,0xcd,0xb7,
  25135. 0x51,0xfb,0x73,0xe6,0x66,0xc6,0xc6,0x55,
  25136. 0xad,0xe8,0x29,0x72,0x97,0xd0,0x7a,0xd1,
  25137. 0xba,0x5e,0x43,0xf1,0xbc,0xa3,0x23,0x01,
  25138. 0x65,0x13,0x39,0xe2,0x29,0x04,0xcc,0x8c,
  25139. 0x42,0xf5,0x8c,0x30,0xc0,0x4a,0xaf,0xdb,
  25140. 0x03,0x8d,0xda,0x08,0x47,0xdd,0x98,0x8d,
  25141. 0xcd,0xa6,0xf3,0xbf,0xd1,0x5c,0x4b,0x4c,
  25142. 0x45,0x25,0x00,0x4a,0xa0,0x6e,0xef,0xf8,
  25143. 0xca,0x61,0x78,0x3a,0xac,0xec,0x57,0xfb,
  25144. 0x3d,0x1f,0x92,0xb0,0xfe,0x2f,0xd1,0xa8,
  25145. 0x5f,0x67,0x24,0x51,0x7b,0x65,0xe6,0x14,
  25146. 0xad,0x68,0x08,0xd6,0xf6,0xee,0x34,0xdf,
  25147. 0xf7,0x31,0x0f,0xdc,0x82,0xae,0xbf,0xd9,
  25148. 0x04,0xb0,0x1e,0x1d,0xc5,0x4b,0x29,0x27,
  25149. 0x09,0x4b,0x2d,0xb6,0x8d,0x6f,0x90,0x3b,
  25150. 0x68,0x40,0x1a,0xde,0xbf,0x5a,0x7e,0x08,
  25151. 0xd7,0x8f,0xf4,0xef,0x5d,0x63,0x65,0x3a,
  25152. 0x65,0x04,0x0c,0xf9,0xbf,0xd4,0xac,0xa7,
  25153. 0x98,0x4a,0x74,0xd3,0x71,0x45,0x98,0x67,
  25154. 0x80,0xfc,0x0b,0x16,0xac,0x45,0x16,0x49,
  25155. 0xde,0x61,0x88,0xa7,0xdb,0xdf,0x19,0x1f,
  25156. 0x64,0xb5,0xfc,0x5e,0x2a,0xb4,0x7b,0x57,
  25157. 0xf7,0xf7,0x27,0x6c,0xd4,0x19,0xc1,0x7a,
  25158. 0x3c,0xa8,0xe1,0xb9,0x39,0xae,0x49,0xe4,
  25159. 0x88,0xac,0xba,0x6b,0x96,0x56,0x10,0xb5,
  25160. 0x48,0x01,0x09,0xc8,0xb1,0x7b,0x80,0xe1,
  25161. 0xb7,0xb7,0x50,0xdf,0xc7,0x59,0x8d,0x5d,
  25162. 0x50,0x11,0xfd,0x2d,0xcc,0x56,0x00,0xa3,
  25163. 0x2e,0xf5,0xb5,0x2a,0x1e,0xcc,0x82,0x0e,
  25164. 0x30,0x8a,0xa3,0x42,0x72,0x1a,0xac,0x09,
  25165. 0x43,0xbf,0x66,0x86,0xb6,0x4b,0x25,0x79,
  25166. 0x37,0x65,0x04,0xcc,0xc4,0x93,0xd9,0x7e,
  25167. 0x6a,0xed,0x3f,0xb0,0xf9,0xcd,0x71,0xa4,
  25168. 0x3d,0xd4,0x97,0xf0,0x1f,0x17,0xc0,0xe2,
  25169. 0xcb,0x37,0x97,0xaa,0x2a,0x2f,0x25,0x66,
  25170. 0x56,0x16,0x8e,0x6c,0x49,0x6a,0xfc,0x5f,
  25171. 0xb9,0x32,0x46,0xf6,0xb1,0x11,0x63,0x98,
  25172. 0xa3,0x46,0xf1,0xa6,0x41,0xf3,0xb0,0x41,
  25173. 0xe9,0x89,0xf7,0x91,0x4f,0x90,0xcc,0x2c,
  25174. 0x7f,0xff,0x35,0x78,0x76,0xe5,0x06,0xb5,
  25175. 0x0d,0x33,0x4b,0xa7,0x7c,0x22,0x5b,0xc3,
  25176. 0x07,0xba,0x53,0x71,0x52,0xf3,0xf1,0x61,
  25177. 0x0e,0x4e,0xaf,0xe5,0x95,0xf6,0xd9,0xd9,
  25178. 0x0d,0x11,0xfa,0xa9,0x33,0xa1,0x5e,0xf1,
  25179. 0x36,0x95,0x46,0x86,0x8a,0x7f,0x3a,0x45,
  25180. 0xa9,0x67,0x68,0xd4,0x0f,0xd9,0xd0,0x34,
  25181. 0x12,0xc0,0x91,0xc6,0x31,0x5c,0xf4,0xfd,
  25182. 0xe7,0xcb,0x68,0x60,0x69,0x37,0x38,0x0d,
  25183. 0xb2,0xea,0xaa,0x70,0x7b,0x4c,0x41,0x85,
  25184. 0xc3,0x2e,0xdd,0xcd,0xd3,0x06,0x70,0x5e,
  25185. 0x4d,0xc1,0xff,0xc8,0x72,0xee,0xee,0x47,
  25186. 0x5a,0x64,0xdf,0xac,0x86,0xab,0xa4,0x1c,
  25187. 0x06,0x18,0x98,0x3f,0x87,0x41,0xc5,0xef,
  25188. 0x68,0xd3,0xa1,0x01,0xe8,0xa3,0xb8,0xca,
  25189. 0xc6,0x0c,0x90,0x5c,0x15,0xfc,0x91,0x08,
  25190. 0x40,0xb9,0x4c,0x00,0xa0,0xb9,0xd0
  25191. };
  25192. WOLFSSL_SMALL_STACK_STATIC const byte* msgs[] = {msg1, msg2, msg3, msg1, msg1, msg4};
  25193. WOLFSSL_SMALL_STACK_STATIC const word16 msgSz[] = {0 /*sizeof(msg1)*/,
  25194. sizeof(msg2),
  25195. sizeof(msg3),
  25196. 0 /*sizeof(msg1)*/,
  25197. 0 /*sizeof(msg1)*/,
  25198. sizeof(msg4)
  25199. };
  25200. #ifndef NO_ASN
  25201. static byte privateEd25519[] = {
  25202. 0x30,0x2e,0x02,0x01,0x00,0x30,0x05,0x06,
  25203. 0x03,0x2b,0x65,0x70,0x04,0x22,0x04,0x20,
  25204. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  25205. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  25206. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  25207. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60
  25208. };
  25209. static byte badPrivateEd25519[] = {
  25210. 0x30,0x52,0x02,0x01,0x00,0x30,0x05,0x06,
  25211. 0x03,0x2b,0x65,0x70,0x04,0x22,0x04,0x20,
  25212. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  25213. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  25214. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  25215. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60,
  25216. 0xa1,0x22,0x04,0x21,0xd7,0x5a,0x98,0x01, /* octet len 0x20 -> 0x21 */
  25217. 0x82,0xb1,0x0a,0xb7,0xd5,0x4b,0xfe,0xd3,
  25218. 0xc9,0x64,0x07,0x3a,0x0e,0xe1,0x72,0xf3,
  25219. 0xda,0xa6,0x23,0x25,0xaf,0x02,0x1a,0x68,
  25220. 0xf7,0x07,0x51,0x1a,
  25221. 0x00 /* add additional bytes to make the pubkey bigger */
  25222. };
  25223. static byte publicEd25519[] = {
  25224. 0x30,0x2a,0x30,0x05,0x06,0x03,0x2b,0x65,
  25225. 0x70,0x03,0x21,0x00,0xd7,0x5a,0x98,0x01,
  25226. 0x82,0xb1,0x0a,0xb7,0xd5,0x4b,0xfe,0xd3,
  25227. 0xc9,0x64,0x07,0x3a,0x0e,0xe1,0x72,0xf3,
  25228. 0xda,0xa6,0x23,0x25,0xaf,0x02,0x1a,0x68,
  25229. 0xf7,0x07,0x51,0x1a
  25230. };
  25231. /* size has been altered to catch if sanity check is done */
  25232. static byte badPublicEd25519[] = {
  25233. 0x30,0x2a,0x30,0x05,0x06,0x03,0x2b,0x65,
  25234. 0x70,0x03,0x21,0x00,0xd7,0x5a,0x98,0x01,
  25235. 0x82,0xb1,0x0a,0xb7,0xd5,0x4b,0xfe,0xd3,
  25236. 0xc9,0x64,0x07,0x3a,0x0e,0xe1,0x72,0xf3,
  25237. 0xda,0xa6,0x23,0x25,0xaf,0x02,0x1a,0x68,
  25238. 0xf7,0x07,0x51,0x1a,
  25239. 0x00 /* add an additional byte to make the pubkey appear bigger */
  25240. };
  25241. static byte privPubEd25519[] = {
  25242. 0x30,0x50,0x02,0x01,0x00,0x30,0x05,0x06,
  25243. 0x03,0x2b,0x65,0x70,0x04,0x22,0x04,0x20,
  25244. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  25245. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  25246. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  25247. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60,
  25248. 0x81,0x20,0xd7,0x5a,0x98,0x01,0x82,0xb1,
  25249. 0x0a,0xb7,0xd5,0x4b,0xfe,0xd3,0xc9,0x64,
  25250. 0x07,0x3a,0x0e,0xe1,0x72,0xf3,0xda,0xa6,
  25251. 0x23,0x25,0xaf,0x02,0x1a,0x68,0xf7,0x07,
  25252. 0x51,0x1a
  25253. };
  25254. word32 idx;
  25255. #endif /* NO_ASN */
  25256. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_EXPORT && HAVE_ED25519_KEY_IMPORT */
  25257. #if !defined(NO_ASN) && defined(HAVE_ED25519_SIGN)
  25258. ed25519_key key3;
  25259. #endif
  25260. /* create ed25519 keys */
  25261. #ifndef HAVE_FIPS
  25262. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  25263. #else
  25264. ret = wc_InitRng(&rng);
  25265. #endif
  25266. if (ret != 0)
  25267. return -11000;
  25268. wc_ed25519_init_ex(&key, HEAP_HINT, devId);
  25269. wc_ed25519_init_ex(&key2, HEAP_HINT, devId);
  25270. #if !defined(NO_ASN) && defined(HAVE_ED25519_SIGN)
  25271. wc_ed25519_init_ex(&key3, HEAP_HINT, devId);
  25272. #endif
  25273. wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  25274. wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key2);
  25275. /* helper functions for signature and key size */
  25276. keySz = wc_ed25519_size(&key);
  25277. sigSz = wc_ed25519_sig_size(&key);
  25278. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_EXPORT) &&\
  25279. defined(HAVE_ED25519_KEY_IMPORT)
  25280. for (i = 0; i < 6; i++) {
  25281. outlen = sizeof(out);
  25282. XMEMSET(out, 0, sizeof(out));
  25283. if (wc_ed25519_import_private_key(sKeys[i], ED25519_KEY_SIZE, pKeys[i],
  25284. pKeySz[i], &key) != 0)
  25285. return -11001 - i;
  25286. if (wc_ed25519_sign_msg(msgs[i], msgSz[i], out, &outlen, &key) != 0)
  25287. return -11011 - i;
  25288. if (XMEMCMP(out, sigs[i], 64))
  25289. return -11021 - i;
  25290. #if defined(HAVE_ED25519_VERIFY)
  25291. /* test verify on good msg */
  25292. if (wc_ed25519_verify_msg(out, outlen, msgs[i], msgSz[i], &verify,
  25293. &key) != 0 || verify != 1)
  25294. return -11031 - i;
  25295. #ifdef WOLFSSL_ED25519_STREAMING_VERIFY
  25296. /* test verify on good msg using streaming interface directly */
  25297. if (wc_ed25519_verify_msg_init(out, outlen,
  25298. &key, (byte)Ed25519, NULL, 0) != 0)
  25299. return -11211 - i;
  25300. for (j = 0; j < msgSz[i]; j += i) {
  25301. if (wc_ed25519_verify_msg_update(msgs[i] + j, MIN(i, msgSz[i] - j), &key) != 0)
  25302. return -11221 - i;
  25303. }
  25304. if (wc_ed25519_verify_msg_final(out, outlen, &verify,
  25305. &key) != 0 || verify != 1)
  25306. return -11231 - i;
  25307. #endif /* WOLFSSL_ED25519_STREAMING_VERIFY */
  25308. /* test verify on bad msg */
  25309. out[outlen-1] = out[outlen-1] + 1;
  25310. if (wc_ed25519_verify_msg(out, outlen, msgs[i], msgSz[i], &verify,
  25311. &key) == 0 || verify == 1)
  25312. return -11041 - i;
  25313. #endif /* HAVE_ED25519_VERIFY */
  25314. /* test api for import/exporting keys */
  25315. exportPSz = sizeof(exportPKey);
  25316. exportSSz = sizeof(exportSKey);
  25317. if (wc_ed25519_export_public(&key, exportPKey, &exportPSz) != 0)
  25318. return -11051 - i;
  25319. if (wc_ed25519_import_public_ex(exportPKey, exportPSz, &key2, 1) != 0)
  25320. return -11061 - i;
  25321. if (wc_ed25519_export_private_only(&key, exportSKey, &exportSSz) != 0)
  25322. return -11071 - i;
  25323. if (wc_ed25519_import_private_key(exportSKey, exportSSz,
  25324. exportPKey, exportPSz, &key2) != 0)
  25325. return -11081 - i;
  25326. /* clear "out" buffer and test sign with imported keys */
  25327. outlen = sizeof(out);
  25328. XMEMSET(out, 0, sizeof(out));
  25329. if (wc_ed25519_sign_msg(msgs[i], msgSz[i], out, &outlen, &key2) != 0)
  25330. return -11091 - i;
  25331. #if defined(HAVE_ED25519_VERIFY)
  25332. if (wc_ed25519_verify_msg(out, outlen, msgs[i], msgSz[i], &verify,
  25333. &key2) != 0 || verify != 1)
  25334. return -11101 - i;
  25335. if (XMEMCMP(out, sigs[i], 64))
  25336. return -11111 - i;
  25337. #endif /* HAVE_ED25519_VERIFY */
  25338. }
  25339. ret = ed25519ctx_test();
  25340. if (ret != 0)
  25341. return ret;
  25342. ret = ed25519ph_test();
  25343. if (ret != 0)
  25344. return ret;
  25345. #ifndef NO_ASN
  25346. /* Try ASN.1 encoded private-only key and public key. */
  25347. idx = 0;
  25348. if (wc_Ed25519PrivateKeyDecode(privateEd25519, &idx, &key3,
  25349. sizeof(privateEd25519)) != 0)
  25350. return -11121;
  25351. idx = 0;
  25352. if (wc_Ed25519PrivateKeyDecode(badPrivateEd25519, &idx, &key3,
  25353. sizeof(badPrivateEd25519)) == 0)
  25354. return -11122;
  25355. if (wc_ed25519_sign_msg(msgs[0], msgSz[0], out, &outlen, &key3)
  25356. != BAD_FUNC_ARG)
  25357. return -11131;
  25358. /* try with a buffer size that is too large */
  25359. idx = 0;
  25360. if (wc_Ed25519PublicKeyDecode(badPublicEd25519, &idx, &key3,
  25361. sizeof(badPublicEd25519)) == 0)
  25362. return -11140;
  25363. idx = 0;
  25364. if (wc_Ed25519PublicKeyDecode(publicEd25519, &idx, &key3,
  25365. sizeof(publicEd25519)) != 0)
  25366. return -11141;
  25367. if (wc_ed25519_sign_msg(msgs[0], msgSz[0], out, &outlen, &key3) != 0)
  25368. return -11151;
  25369. if (XMEMCMP(out, sigs[0], 64))
  25370. return -11161;
  25371. #if defined(HAVE_ED25519_VERIFY)
  25372. /* test verify on good msg */
  25373. if (wc_ed25519_verify_msg(out, outlen, msgs[0], msgSz[0], &verify, &key3)
  25374. != 0 || verify != 1)
  25375. return -11171;
  25376. #endif /* HAVE_ED25519_VERIFY */
  25377. wc_ed25519_free(&key3);
  25378. wc_ed25519_init(&key3);
  25379. idx = 0;
  25380. if (wc_Ed25519PrivateKeyDecode(privPubEd25519, &idx, &key3,
  25381. sizeof(privPubEd25519)) != 0)
  25382. return -11181;
  25383. if (wc_ed25519_sign_msg(msgs[0], msgSz[0], out, &outlen, &key3) != 0)
  25384. return -11191;
  25385. if (XMEMCMP(out, sigs[0], 64))
  25386. return -11201;
  25387. wc_ed25519_free(&key3);
  25388. #endif /* NO_ASN */
  25389. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_EXPORT && HAVE_ED25519_KEY_IMPORT */
  25390. /* clean up keys when done */
  25391. wc_ed25519_free(&key);
  25392. wc_ed25519_free(&key2);
  25393. #if defined(HAVE_HASHDRBG) || defined(NO_RC4)
  25394. wc_FreeRng(&rng);
  25395. #endif
  25396. /* hush warnings of unused keySz and sigSz */
  25397. (void)keySz;
  25398. (void)sigSz;
  25399. #ifdef WOLFSSL_TEST_CERT
  25400. ret = ed25519_test_cert();
  25401. if (ret < 0)
  25402. return ret;
  25403. #ifdef WOLFSSL_CERT_GEN
  25404. ret = ed25519_test_make_cert();
  25405. if (ret < 0)
  25406. return ret;
  25407. #endif /* WOLFSSL_CERT_GEN */
  25408. #endif /* WOLFSSL_TEST_CERT */
  25409. return 0;
  25410. }
  25411. #endif /* HAVE_ED25519 */
  25412. #ifdef HAVE_CURVE448
  25413. #if defined(HAVE_CURVE448_SHARED_SECRET) && \
  25414. defined(HAVE_CURVE448_KEY_IMPORT)
  25415. /* Test the wc_curve448_check_public API.
  25416. *
  25417. * returns 0 on success and -ve on failure.
  25418. */
  25419. static int curve448_check_public_test(void)
  25420. {
  25421. /* Little-endian values that will fail */
  25422. byte fail_le[][CURVE448_KEY_SIZE] = {
  25423. {
  25424. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25425. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25426. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25427. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25428. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25429. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25430. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  25431. },
  25432. {
  25433. 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25434. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25435. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25436. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25437. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25438. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25439. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  25440. },
  25441. };
  25442. /* Big-endian values that will fail */
  25443. byte fail_be[][CURVE448_KEY_SIZE] = {
  25444. {
  25445. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25446. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25447. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25448. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25449. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25450. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25451. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  25452. },
  25453. {
  25454. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25455. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25456. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25457. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25458. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25459. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25460. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  25461. },
  25462. };
  25463. /* Good or valid public value */
  25464. byte good[CURVE448_KEY_SIZE] = {
  25465. 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25466. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25467. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25468. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25469. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25470. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25471. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  25472. };
  25473. int i;
  25474. /* Parameter checks */
  25475. /* NULL pointer */
  25476. if (wc_curve448_check_public(NULL, 0, EC448_LITTLE_ENDIAN) !=
  25477. BAD_FUNC_ARG) {
  25478. return -11300;
  25479. }
  25480. if (wc_curve448_check_public(NULL, 0, EC448_BIG_ENDIAN) != BAD_FUNC_ARG) {
  25481. return -11301;
  25482. }
  25483. /* Length of 0 treated differently to other invalid lengths for TLS */
  25484. if (wc_curve448_check_public(good, 0, EC448_LITTLE_ENDIAN) != BUFFER_E)
  25485. return -11302;
  25486. if (wc_curve448_check_public(good, 0, EC448_BIG_ENDIAN) != BUFFER_E)
  25487. return -11303;
  25488. /* Length not CURVE448_KEY_SIZE */
  25489. for (i = 1; i < CURVE448_KEY_SIZE + 2; i++) {
  25490. if (i == CURVE448_KEY_SIZE)
  25491. continue;
  25492. if (wc_curve448_check_public(good, i, EC448_LITTLE_ENDIAN) !=
  25493. ECC_BAD_ARG_E) {
  25494. return -11304 - i;
  25495. }
  25496. if (wc_curve448_check_public(good, i, EC448_BIG_ENDIAN) !=
  25497. ECC_BAD_ARG_E) {
  25498. return -11314 - i;
  25499. }
  25500. }
  25501. /* Little-endian fail cases */
  25502. for (i = 0; i < (int)(sizeof(fail_le) / sizeof(*fail_le)); i++) {
  25503. if (wc_curve448_check_public(fail_le[i], CURVE448_KEY_SIZE,
  25504. EC448_LITTLE_ENDIAN) == 0) {
  25505. return -11324 - i;
  25506. }
  25507. }
  25508. /* Big-endian fail cases */
  25509. for (i = 0; i < (int)(sizeof(fail_be) / sizeof(*fail_be)); i++) {
  25510. if (wc_curve448_check_public(fail_be[i], CURVE448_KEY_SIZE,
  25511. EC448_BIG_ENDIAN) == 0) {
  25512. return -11334 - i;
  25513. }
  25514. }
  25515. /* Check a valid public value works! */
  25516. if (wc_curve448_check_public(good, CURVE448_KEY_SIZE,
  25517. EC448_LITTLE_ENDIAN) != 0) {
  25518. return -11344;
  25519. }
  25520. if (wc_curve448_check_public(good, CURVE448_KEY_SIZE,
  25521. EC448_BIG_ENDIAN) != 0) {
  25522. return -11345;
  25523. }
  25524. return 0;
  25525. }
  25526. #endif /* HAVE_CURVE448_SHARED_SECRET && HAVE_CURVE448_KEY_IMPORT */
  25527. WOLFSSL_TEST_SUBROUTINE int curve448_test(void)
  25528. {
  25529. WC_RNG rng;
  25530. int ret;
  25531. #ifdef HAVE_CURVE448_SHARED_SECRET
  25532. byte sharedA[CURVE448_KEY_SIZE];
  25533. byte sharedB[CURVE448_KEY_SIZE];
  25534. word32 y;
  25535. #endif
  25536. #ifdef HAVE_CURVE448_KEY_EXPORT
  25537. byte exportBuf[CURVE448_KEY_SIZE];
  25538. #endif
  25539. word32 x;
  25540. curve448_key userA, userB, pubKey;
  25541. #if defined(HAVE_CURVE448_SHARED_SECRET) && \
  25542. defined(HAVE_CURVE448_KEY_IMPORT)
  25543. /* test vectors from
  25544. https://www.rfc-editor.org/rfc/rfc7748.html
  25545. */
  25546. /* secret key for party a */
  25547. byte sa[] = {
  25548. 0x6b, 0x72, 0x98, 0xa5, 0xc0, 0xd8, 0xc2, 0x9a,
  25549. 0x1d, 0xab, 0x27, 0xf1, 0xa6, 0x82, 0x63, 0x00,
  25550. 0x91, 0x73, 0x89, 0x44, 0x97, 0x41, 0xa9, 0x74,
  25551. 0xf5, 0xba, 0xc9, 0xd9, 0x8d, 0xc2, 0x98, 0xd4,
  25552. 0x65, 0x55, 0xbc, 0xe8, 0xba, 0xe8, 0x9e, 0xee,
  25553. 0xd4, 0x00, 0x58, 0x4b, 0xb0, 0x46, 0xcf, 0x75,
  25554. 0x57, 0x9f, 0x51, 0xd1, 0x25, 0x49, 0x8f, 0x9a,
  25555. };
  25556. /* public key for party a */
  25557. byte pa[] = {
  25558. 0xa0, 0x1f, 0xc4, 0x32, 0xe5, 0x80, 0x7f, 0x17,
  25559. 0x53, 0x0d, 0x12, 0x88, 0xda, 0x12, 0x5b, 0x0c,
  25560. 0xd4, 0x53, 0xd9, 0x41, 0x72, 0x64, 0x36, 0xc8,
  25561. 0xbb, 0xd9, 0xc5, 0x22, 0x2c, 0x3d, 0xa7, 0xfa,
  25562. 0x63, 0x9c, 0xe0, 0x3d, 0xb8, 0xd2, 0x3b, 0x27,
  25563. 0x4a, 0x07, 0x21, 0xa1, 0xae, 0xd5, 0x22, 0x7d,
  25564. 0xe6, 0xe3, 0xb7, 0x31, 0xcc, 0xf7, 0x08, 0x9b,
  25565. };
  25566. /* secret key for party b */
  25567. byte sb[] = {
  25568. 0x2d, 0x99, 0x73, 0x51, 0xb6, 0x10, 0x6f, 0x36,
  25569. 0xb0, 0xd1, 0x09, 0x1b, 0x92, 0x9c, 0x4c, 0x37,
  25570. 0x21, 0x3e, 0x0d, 0x2b, 0x97, 0xe8, 0x5e, 0xbb,
  25571. 0x20, 0xc1, 0x27, 0x69, 0x1d, 0x0d, 0xad, 0x8f,
  25572. 0x1d, 0x81, 0x75, 0xb0, 0x72, 0x37, 0x45, 0xe6,
  25573. 0x39, 0xa3, 0xcb, 0x70, 0x44, 0x29, 0x0b, 0x99,
  25574. 0xe0, 0xe2, 0xa0, 0xc2, 0x7a, 0x6a, 0x30, 0x1c,
  25575. };
  25576. /* public key for party b */
  25577. byte pb[] = {
  25578. 0x09, 0x36, 0xf3, 0x7b, 0xc6, 0xc1, 0xbd, 0x07,
  25579. 0xae, 0x3d, 0xec, 0x7a, 0xb5, 0xdc, 0x06, 0xa7,
  25580. 0x3c, 0xa1, 0x32, 0x42, 0xfb, 0x34, 0x3e, 0xfc,
  25581. 0x72, 0xb9, 0xd8, 0x27, 0x30, 0xb4, 0x45, 0xf3,
  25582. 0xd4, 0xb0, 0xbd, 0x07, 0x71, 0x62, 0xa4, 0x6d,
  25583. 0xcf, 0xec, 0x6f, 0x9b, 0x59, 0x0b, 0xfc, 0xbc,
  25584. 0xf5, 0x20, 0xcd, 0xb0, 0x29, 0xa8, 0xb7, 0x3e,
  25585. };
  25586. /* expected shared key */
  25587. byte ss[] = {
  25588. 0x9d, 0x87, 0x4a, 0x51, 0x37, 0x50, 0x9a, 0x44,
  25589. 0x9a, 0xd5, 0x85, 0x30, 0x40, 0x24, 0x1c, 0x52,
  25590. 0x36, 0x39, 0x54, 0x35, 0xc3, 0x64, 0x24, 0xfd,
  25591. 0x56, 0x0b, 0x0c, 0xb6, 0x2b, 0x28, 0x1d, 0x28,
  25592. 0x52, 0x75, 0xa7, 0x40, 0xce, 0x32, 0xa2, 0x2d,
  25593. 0xd1, 0x74, 0x0f, 0x4a, 0xa9, 0x16, 0x1c, 0xec,
  25594. 0x95, 0xcc, 0xc6, 0x1a, 0x18, 0xf4, 0xff, 0x07,
  25595. };
  25596. #endif /* HAVE_CURVE448_SHARED_SECRET */
  25597. (void)x;
  25598. #ifndef HAVE_FIPS
  25599. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  25600. #else
  25601. ret = wc_InitRng(&rng);
  25602. #endif
  25603. if (ret != 0)
  25604. return -11400;
  25605. wc_curve448_init(&userA);
  25606. wc_curve448_init(&userB);
  25607. wc_curve448_init(&pubKey);
  25608. /* make curve448 keys */
  25609. if (wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &userA) != 0)
  25610. return -11401;
  25611. if (wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &userB) != 0)
  25612. return -11402;
  25613. #ifdef HAVE_CURVE448_SHARED_SECRET
  25614. /* find shared secret key */
  25615. x = sizeof(sharedA);
  25616. if (wc_curve448_shared_secret(&userA, &userB, sharedA, &x) != 0)
  25617. return -11403;
  25618. y = sizeof(sharedB);
  25619. if (wc_curve448_shared_secret(&userB, &userA, sharedB, &y) != 0)
  25620. return -11404;
  25621. /* compare shared secret keys to test they are the same */
  25622. if (y != x)
  25623. return -11405;
  25624. if (XMEMCMP(sharedA, sharedB, x))
  25625. return -11406;
  25626. #endif
  25627. #ifdef HAVE_CURVE448_KEY_EXPORT
  25628. /* export a public key and import it for another user */
  25629. x = sizeof(exportBuf);
  25630. if (wc_curve448_export_public(&userA, exportBuf, &x) != 0)
  25631. return -11407;
  25632. #ifdef HAVE_CURVE448_KEY_IMPORT
  25633. if (wc_curve448_import_public(exportBuf, x, &pubKey) != 0)
  25634. return -11408;
  25635. #endif
  25636. #endif
  25637. #if defined(HAVE_CURVE448_SHARED_SECRET) && \
  25638. defined(HAVE_CURVE448_KEY_IMPORT)
  25639. /* test shared key after importing a public key */
  25640. XMEMSET(sharedB, 0, sizeof(sharedB));
  25641. y = sizeof(sharedB);
  25642. if (wc_curve448_shared_secret(&userB, &pubKey, sharedB, &y) != 0)
  25643. return -11409;
  25644. if (XMEMCMP(sharedA, sharedB, y))
  25645. return -11410;
  25646. /* import RFC test vectors and compare shared key */
  25647. if (wc_curve448_import_private_raw(sa, sizeof(sa), pa, sizeof(pa), &userA)
  25648. != 0)
  25649. return -11411;
  25650. if (wc_curve448_import_private_raw(sb, sizeof(sb), pb, sizeof(pb), &userB)
  25651. != 0)
  25652. return -11412;
  25653. /* test against known test vector */
  25654. XMEMSET(sharedB, 0, sizeof(sharedB));
  25655. y = sizeof(sharedB);
  25656. if (wc_curve448_shared_secret(&userA, &userB, sharedB, &y) != 0)
  25657. return -11413;
  25658. if (XMEMCMP(ss, sharedB, y))
  25659. return -11414;
  25660. /* test swapping roles of keys and generating same shared key */
  25661. XMEMSET(sharedB, 0, sizeof(sharedB));
  25662. y = sizeof(sharedB);
  25663. if (wc_curve448_shared_secret(&userB, &userA, sharedB, &y) != 0)
  25664. return -11415;
  25665. if (XMEMCMP(ss, sharedB, y))
  25666. return -11416;
  25667. /* test with 1 generated key and 1 from known test vector */
  25668. if (wc_curve448_import_private_raw(sa, sizeof(sa), pa, sizeof(pa), &userA)
  25669. != 0)
  25670. return -11417;
  25671. if (wc_curve448_make_key(&rng, 56, &userB) != 0)
  25672. return -11418;
  25673. x = sizeof(sharedA);
  25674. if (wc_curve448_shared_secret(&userA, &userB, sharedA, &x) != 0)
  25675. return -11419;
  25676. y = sizeof(sharedB);
  25677. if (wc_curve448_shared_secret(&userB, &userA, sharedB, &y) != 0)
  25678. return -11420;
  25679. /* compare shared secret keys to test they are the same */
  25680. if (y != x)
  25681. return -11421;
  25682. if (XMEMCMP(sharedA, sharedB, x))
  25683. return -11422;
  25684. ret = curve448_check_public_test();
  25685. if (ret != 0)
  25686. return ret;
  25687. #endif /* HAVE_CURVE448_SHARED_SECRET && HAVE_CURVE448_KEY_IMPORT */
  25688. /* clean up keys when done */
  25689. wc_curve448_free(&pubKey);
  25690. wc_curve448_free(&userB);
  25691. wc_curve448_free(&userA);
  25692. wc_FreeRng(&rng);
  25693. return 0;
  25694. }
  25695. #endif /* HAVE_CURVE448 */
  25696. #ifdef HAVE_ED448
  25697. #ifdef WOLFSSL_TEST_CERT
  25698. static int ed448_test_cert(void)
  25699. {
  25700. DecodedCert cert[2];
  25701. DecodedCert* serverCert = NULL;
  25702. DecodedCert* caCert = NULL;
  25703. #ifdef HAVE_ED448_VERIFY
  25704. ed448_key key;
  25705. ed448_key* pubKey = NULL;
  25706. int verify;
  25707. #endif /* HAVE_ED448_VERIFY */
  25708. int ret;
  25709. byte* tmp;
  25710. size_t bytes;
  25711. XFILE file;
  25712. tmp = (byte *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25713. if (tmp == NULL) {
  25714. ERROR_OUT(-11430, done);
  25715. }
  25716. #ifdef USE_CERT_BUFFERS_256
  25717. XMEMCPY(tmp, ca_ed448_cert, sizeof_ca_ed448_cert);
  25718. bytes = sizeof_ca_ed448_cert;
  25719. #elif !defined(NO_FILESYSTEM)
  25720. file = XFOPEN(caEd448Cert, "rb");
  25721. if (file == NULL) {
  25722. ERROR_OUT(-11431, done);
  25723. }
  25724. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  25725. XFCLOSE(file);
  25726. #else
  25727. /* No certificate to use. */
  25728. ERROR_OUT(-11432, done);
  25729. #endif
  25730. InitDecodedCert(&cert[0], tmp, (word32)bytes, 0);
  25731. caCert = &cert[0];
  25732. ret = ParseCert(caCert, CERT_TYPE, NO_VERIFY, NULL);
  25733. if (ret != 0) {
  25734. ERROR_OUT(-11433, done);
  25735. }
  25736. #ifdef USE_CERT_BUFFERS_256
  25737. XMEMCPY(tmp, server_ed448_cert, sizeof_server_ed448_cert);
  25738. bytes = sizeof_server_ed448_cert;
  25739. #elif !defined(NO_FILESYSTEM)
  25740. file = XFOPEN(serverEd448Cert, "rb");
  25741. if (file == NULL) {
  25742. ERROR_OUT(-11434, done);
  25743. }
  25744. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  25745. XFCLOSE(file);
  25746. #else
  25747. /* No certificate to use. */
  25748. ERROR_OUT(-11435, done);
  25749. #endif
  25750. InitDecodedCert(&cert[1], tmp, (word32)bytes, 0);
  25751. serverCert = &cert[1];
  25752. ret = ParseCert(serverCert, CERT_TYPE, NO_VERIFY, NULL);
  25753. if (ret != 0) {
  25754. ERROR_OUT(-11436, done);
  25755. }
  25756. #ifdef HAVE_ED448_VERIFY
  25757. ret = wc_ed448_init(&key);
  25758. if (ret < 0) {
  25759. ERROR_OUT(-11437, done);
  25760. }
  25761. pubKey = &key;
  25762. ret = wc_ed448_import_public(caCert->publicKey, caCert->pubKeySize, pubKey);
  25763. if (ret < 0) {
  25764. ERROR_OUT(-11438, done);
  25765. }
  25766. if (wc_ed448_verify_msg(serverCert->signature, serverCert->sigLength,
  25767. serverCert->source + serverCert->certBegin,
  25768. serverCert->sigIndex - serverCert->certBegin,
  25769. &verify, pubKey, NULL, 0) < 0 || verify != 1) {
  25770. ERROR_OUT(-11439, done);
  25771. }
  25772. #endif /* HAVE_ED448_VERIFY */
  25773. done:
  25774. if (tmp != NULL)
  25775. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25776. #ifdef HAVE_ED448_VERIFY
  25777. wc_ed448_free(pubKey);
  25778. #endif /* HAVE_ED448_VERIFY */
  25779. if (caCert != NULL)
  25780. FreeDecodedCert(caCert);
  25781. if (serverCert != NULL)
  25782. FreeDecodedCert(serverCert);
  25783. return ret;
  25784. }
  25785. static int ed448_test_make_cert(void)
  25786. {
  25787. WC_RNG rng;
  25788. Cert cert;
  25789. DecodedCert decode;
  25790. ed448_key key;
  25791. ed448_key* privKey = NULL;
  25792. int ret = 0;
  25793. byte* tmp = NULL;
  25794. wc_InitCert_ex(&cert, HEAP_HINT, devId);
  25795. #ifndef HAVE_FIPS
  25796. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  25797. #else
  25798. ret = wc_InitRng(&rng);
  25799. #endif
  25800. if (ret != 0)
  25801. return -11450;
  25802. wc_ed448_init(&key);
  25803. privKey = &key;
  25804. wc_ed448_make_key(&rng, ED448_KEY_SIZE, privKey);
  25805. cert.daysValid = 365 * 2;
  25806. cert.selfSigned = 1;
  25807. XMEMCPY(&cert.issuer, &certDefaultName, sizeof(CertName));
  25808. XMEMCPY(&cert.subject, &certDefaultName, sizeof(CertName));
  25809. cert.isCA = 0;
  25810. #ifdef WOLFSSL_CERT_EXT
  25811. ret = wc_SetKeyUsage(&cert, certKeyUsage);
  25812. if (ret < 0) {
  25813. ERROR_OUT(-11451, done);
  25814. }
  25815. ret = wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ED448_TYPE, privKey);
  25816. if (ret < 0) {
  25817. ERROR_OUT(-11452, done);
  25818. }
  25819. ret = wc_SetAuthKeyIdFromPublicKey_ex(&cert, ED448_TYPE, privKey);
  25820. if (ret < 0) {
  25821. ERROR_OUT(-11453, done);
  25822. }
  25823. #endif
  25824. tmp = (byte *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25825. if (tmp == NULL) {
  25826. ERROR_OUT(-11454, done);
  25827. }
  25828. cert.sigType = CTC_ED448;
  25829. ret = wc_MakeCert_ex(&cert, tmp, FOURK_BUF, ED448_TYPE, privKey, &rng);
  25830. if (ret < 0) {
  25831. ERROR_OUT(-11455, done);
  25832. }
  25833. ret = wc_SignCert_ex(cert.bodySz, cert.sigType, tmp, FOURK_BUF, ED448_TYPE,
  25834. privKey, &rng);
  25835. if (ret < 0) {
  25836. ERROR_OUT(-11456, done);
  25837. }
  25838. InitDecodedCert(&decode, tmp, ret, HEAP_HINT);
  25839. ret = ParseCert(&decode, CERT_TYPE, NO_VERIFY, 0);
  25840. FreeDecodedCert(&decode);
  25841. if (ret != 0) {
  25842. ERROR_OUT(-11457, done);
  25843. }
  25844. done:
  25845. if (tmp != NULL)
  25846. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25847. wc_ed448_free(privKey);
  25848. wc_FreeRng(&rng);
  25849. return ret;
  25850. }
  25851. #endif /* WOLFSSL_TEST_CERT */
  25852. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_EXPORT) && \
  25853. defined(HAVE_ED448_KEY_IMPORT)
  25854. static int ed448_ctx_test(void)
  25855. {
  25856. byte out[ED448_SIG_SIZE];
  25857. word32 outlen;
  25858. #ifdef HAVE_ED448_VERIFY
  25859. int verify;
  25860. #endif /* HAVE_ED448_VERIFY */
  25861. ed448_key key;
  25862. WOLFSSL_SMALL_STACK_STATIC const byte sKeyCtx[] = {
  25863. 0xc4, 0xea, 0xb0, 0x5d, 0x35, 0x70, 0x07, 0xc6,
  25864. 0x32, 0xf3, 0xdb, 0xb4, 0x84, 0x89, 0x92, 0x4d,
  25865. 0x55, 0x2b, 0x08, 0xfe, 0x0c, 0x35, 0x3a, 0x0d,
  25866. 0x4a, 0x1f, 0x00, 0xac, 0xda, 0x2c, 0x46, 0x3a,
  25867. 0xfb, 0xea, 0x67, 0xc5, 0xe8, 0xd2, 0x87, 0x7c,
  25868. 0x5e, 0x3b, 0xc3, 0x97, 0xa6, 0x59, 0x94, 0x9e,
  25869. 0xf8, 0x02, 0x1e, 0x95, 0x4e, 0x0a, 0x12, 0x27,
  25870. 0x4e
  25871. };
  25872. WOLFSSL_SMALL_STACK_STATIC const byte pKeyCtx[] = {
  25873. 0x43, 0xba, 0x28, 0xf4, 0x30, 0xcd, 0xff, 0x45,
  25874. 0x6a, 0xe5, 0x31, 0x54, 0x5f, 0x7e, 0xcd, 0x0a,
  25875. 0xc8, 0x34, 0xa5, 0x5d, 0x93, 0x58, 0xc0, 0x37,
  25876. 0x2b, 0xfa, 0x0c, 0x6c, 0x67, 0x98, 0xc0, 0x86,
  25877. 0x6a, 0xea, 0x01, 0xeb, 0x00, 0x74, 0x28, 0x02,
  25878. 0xb8, 0x43, 0x8e, 0xa4, 0xcb, 0x82, 0x16, 0x9c,
  25879. 0x23, 0x51, 0x60, 0x62, 0x7b, 0x4c, 0x3a, 0x94,
  25880. 0x80
  25881. };
  25882. WOLFSSL_SMALL_STACK_STATIC const byte sigCtx[] = {
  25883. 0xd4, 0xf8, 0xf6, 0x13, 0x17, 0x70, 0xdd, 0x46,
  25884. 0xf4, 0x08, 0x67, 0xd6, 0xfd, 0x5d, 0x50, 0x55,
  25885. 0xde, 0x43, 0x54, 0x1f, 0x8c, 0x5e, 0x35, 0xab,
  25886. 0xbc, 0xd0, 0x01, 0xb3, 0x2a, 0x89, 0xf7, 0xd2,
  25887. 0x15, 0x1f, 0x76, 0x47, 0xf1, 0x1d, 0x8c, 0xa2,
  25888. 0xae, 0x27, 0x9f, 0xb8, 0x42, 0xd6, 0x07, 0x21,
  25889. 0x7f, 0xce, 0x6e, 0x04, 0x2f, 0x68, 0x15, 0xea,
  25890. 0x00, 0x0c, 0x85, 0x74, 0x1d, 0xe5, 0xc8, 0xda,
  25891. 0x11, 0x44, 0xa6, 0xa1, 0xab, 0xa7, 0xf9, 0x6d,
  25892. 0xe4, 0x25, 0x05, 0xd7, 0xa7, 0x29, 0x85, 0x24,
  25893. 0xfd, 0xa5, 0x38, 0xfc, 0xcb, 0xbb, 0x75, 0x4f,
  25894. 0x57, 0x8c, 0x1c, 0xad, 0x10, 0xd5, 0x4d, 0x0d,
  25895. 0x54, 0x28, 0x40, 0x7e, 0x85, 0xdc, 0xbc, 0x98,
  25896. 0xa4, 0x91, 0x55, 0xc1, 0x37, 0x64, 0xe6, 0x6c,
  25897. 0x3c, 0x00
  25898. };
  25899. WOLFSSL_SMALL_STACK_STATIC const byte msgCtx[] = {
  25900. 0x03
  25901. };
  25902. WOLFSSL_SMALL_STACK_STATIC const byte contextCtx[] = {
  25903. 0x66,0x6f,0x6f
  25904. };
  25905. outlen = sizeof(out);
  25906. XMEMSET(out, 0, sizeof(out));
  25907. if (wc_ed448_init_ex(&key, HEAP_HINT, devId) != 0)
  25908. return -11500;
  25909. if (wc_ed448_import_private_key(sKeyCtx, ED448_KEY_SIZE, pKeyCtx,
  25910. sizeof(pKeyCtx), &key) != 0)
  25911. return -11501;
  25912. if (wc_ed448_sign_msg(msgCtx, sizeof(msgCtx), out, &outlen, &key,
  25913. contextCtx, sizeof(contextCtx)) != 0)
  25914. return -11502;
  25915. if (XMEMCMP(out, sigCtx, sizeof(sigCtx)))
  25916. return -11503;
  25917. #if defined(HAVE_ED448_VERIFY)
  25918. /* test verify on good msg */
  25919. if (wc_ed448_verify_msg(out, outlen, msgCtx, sizeof(msgCtx), &verify, &key,
  25920. contextCtx, sizeof(contextCtx)) != 0 || verify != 1)
  25921. return -11504;
  25922. #endif
  25923. wc_ed448_free(&key);
  25924. return 0;
  25925. }
  25926. static int ed448ph_test(void)
  25927. {
  25928. byte out[ED448_SIG_SIZE];
  25929. word32 outlen;
  25930. #ifdef HAVE_ED448_VERIFY
  25931. int verify;
  25932. #endif /* HAVE_ED448_VERIFY */
  25933. ed448_key key;
  25934. WOLFSSL_SMALL_STACK_STATIC const byte sKeyPh[] = {
  25935. 0x83, 0x3f, 0xe6, 0x24, 0x09, 0x23, 0x7b, 0x9d,
  25936. 0x62, 0xec, 0x77, 0x58, 0x75, 0x20, 0x91, 0x1e,
  25937. 0x9a, 0x75, 0x9c, 0xec, 0x1d, 0x19, 0x75, 0x5b,
  25938. 0x7d, 0xa9, 0x01, 0xb9, 0x6d, 0xca, 0x3d, 0x42,
  25939. 0xef, 0x78, 0x22, 0xe0, 0xd5, 0x10, 0x41, 0x27,
  25940. 0xdc, 0x05, 0xd6, 0xdb, 0xef, 0xde, 0x69, 0xe3,
  25941. 0xab, 0x2c, 0xec, 0x7c, 0x86, 0x7c, 0x6e, 0x2c,
  25942. 0x49
  25943. };
  25944. WOLFSSL_SMALL_STACK_STATIC const byte pKeyPh[] = {
  25945. 0x25, 0x9b, 0x71, 0xc1, 0x9f, 0x83, 0xef, 0x77,
  25946. 0xa7, 0xab, 0xd2, 0x65, 0x24, 0xcb, 0xdb, 0x31,
  25947. 0x61, 0xb5, 0x90, 0xa4, 0x8f, 0x7d, 0x17, 0xde,
  25948. 0x3e, 0xe0, 0xba, 0x9c, 0x52, 0xbe, 0xb7, 0x43,
  25949. 0xc0, 0x94, 0x28, 0xa1, 0x31, 0xd6, 0xb1, 0xb5,
  25950. 0x73, 0x03, 0xd9, 0x0d, 0x81, 0x32, 0xc2, 0x76,
  25951. 0xd5, 0xed, 0x3d, 0x5d, 0x01, 0xc0, 0xf5, 0x38,
  25952. 0x80
  25953. };
  25954. WOLFSSL_SMALL_STACK_STATIC const byte sigPh1[] = {
  25955. 0x82, 0x2f, 0x69, 0x01, 0xf7, 0x48, 0x0f, 0x3d,
  25956. 0x5f, 0x56, 0x2c, 0x59, 0x29, 0x94, 0xd9, 0x69,
  25957. 0x36, 0x02, 0x87, 0x56, 0x14, 0x48, 0x32, 0x56,
  25958. 0x50, 0x56, 0x00, 0xbb, 0xc2, 0x81, 0xae, 0x38,
  25959. 0x1f, 0x54, 0xd6, 0xbc, 0xe2, 0xea, 0x91, 0x15,
  25960. 0x74, 0x93, 0x2f, 0x52, 0xa4, 0xe6, 0xca, 0xdd,
  25961. 0x78, 0x76, 0x93, 0x75, 0xec, 0x3f, 0xfd, 0x1b,
  25962. 0x80, 0x1a, 0x0d, 0x9b, 0x3f, 0x40, 0x30, 0xcd,
  25963. 0x43, 0x39, 0x64, 0xb6, 0x45, 0x7e, 0xa3, 0x94,
  25964. 0x76, 0x51, 0x12, 0x14, 0xf9, 0x74, 0x69, 0xb5,
  25965. 0x7d, 0xd3, 0x2d, 0xbc, 0x56, 0x0a, 0x9a, 0x94,
  25966. 0xd0, 0x0b, 0xff, 0x07, 0x62, 0x04, 0x64, 0xa3,
  25967. 0xad, 0x20, 0x3d, 0xf7, 0xdc, 0x7c, 0xe3, 0x60,
  25968. 0xc3, 0xcd, 0x36, 0x96, 0xd9, 0xd9, 0xfa, 0xb9,
  25969. 0x0f, 0x00
  25970. };
  25971. WOLFSSL_SMALL_STACK_STATIC const byte sigPh2[] = {
  25972. 0xc3, 0x22, 0x99, 0xd4, 0x6e, 0xc8, 0xff, 0x02,
  25973. 0xb5, 0x45, 0x40, 0x98, 0x28, 0x14, 0xdc, 0xe9,
  25974. 0xa0, 0x58, 0x12, 0xf8, 0x19, 0x62, 0xb6, 0x49,
  25975. 0xd5, 0x28, 0x09, 0x59, 0x16, 0xa2, 0xaa, 0x48,
  25976. 0x10, 0x65, 0xb1, 0x58, 0x04, 0x23, 0xef, 0x92,
  25977. 0x7e, 0xcf, 0x0a, 0xf5, 0x88, 0x8f, 0x90, 0xda,
  25978. 0x0f, 0x6a, 0x9a, 0x85, 0xad, 0x5d, 0xc3, 0xf2,
  25979. 0x80, 0xd9, 0x12, 0x24, 0xba, 0x99, 0x11, 0xa3,
  25980. 0x65, 0x3d, 0x00, 0xe4, 0x84, 0xe2, 0xce, 0x23,
  25981. 0x25, 0x21, 0x48, 0x1c, 0x86, 0x58, 0xdf, 0x30,
  25982. 0x4b, 0xb7, 0x74, 0x5a, 0x73, 0x51, 0x4c, 0xdb,
  25983. 0x9b, 0xf3, 0xe1, 0x57, 0x84, 0xab, 0x71, 0x28,
  25984. 0x4f, 0x8d, 0x07, 0x04, 0xa6, 0x08, 0xc5, 0x4a,
  25985. 0x6b, 0x62, 0xd9, 0x7b, 0xeb, 0x51, 0x1d, 0x13,
  25986. 0x21, 0x00
  25987. };
  25988. WOLFSSL_SMALL_STACK_STATIC const byte msgPh[] = {
  25989. 0x61,0x62,0x63
  25990. };
  25991. /* SHA-512 hash of msgPh */
  25992. WOLFSSL_SMALL_STACK_STATIC const byte hashPh[] = {
  25993. 0x48, 0x33, 0x66, 0x60, 0x13, 0x60, 0xa8, 0x77,
  25994. 0x1c, 0x68, 0x63, 0x08, 0x0c, 0xc4, 0x11, 0x4d,
  25995. 0x8d, 0xb4, 0x45, 0x30, 0xf8, 0xf1, 0xe1, 0xee,
  25996. 0x4f, 0x94, 0xea, 0x37, 0xe7, 0x8b, 0x57, 0x39,
  25997. 0xd5, 0xa1, 0x5b, 0xef, 0x18, 0x6a, 0x53, 0x86,
  25998. 0xc7, 0x57, 0x44, 0xc0, 0x52, 0x7e, 0x1f, 0xaa,
  25999. 0x9f, 0x87, 0x26, 0xe4, 0x62, 0xa1, 0x2a, 0x4f,
  26000. 0xeb, 0x06, 0xbd, 0x88, 0x01, 0xe7, 0x51, 0xe4
  26001. };
  26002. WOLFSSL_SMALL_STACK_STATIC const byte contextPh2[] = {
  26003. 0x66,0x6f,0x6f
  26004. };
  26005. outlen = sizeof(out);
  26006. XMEMSET(out, 0, sizeof(out));
  26007. if (wc_ed448_init_ex(&key, HEAP_HINT, devId) != 0)
  26008. return -11600;
  26009. if (wc_ed448_import_private_key(sKeyPh, ED448_KEY_SIZE, pKeyPh,
  26010. sizeof(pKeyPh), &key) != 0) {
  26011. return -11601;
  26012. }
  26013. if (wc_ed448ph_sign_msg(msgPh, sizeof(msgPh), out, &outlen, &key, NULL,
  26014. 0) != 0) {
  26015. return -11602;
  26016. }
  26017. if (XMEMCMP(out, sigPh1, sizeof(sigPh1)))
  26018. return -11603;
  26019. #if defined(HAVE_ED448_VERIFY)
  26020. /* test verify on good msg */
  26021. if (wc_ed448ph_verify_msg(out, outlen, msgPh, sizeof(msgPh), &verify, &key,
  26022. NULL, 0) != 0 || verify != 1) {
  26023. return -11604;
  26024. }
  26025. #endif
  26026. if (wc_ed448ph_sign_msg(msgPh, sizeof(msgPh), out, &outlen, &key,
  26027. contextPh2, sizeof(contextPh2)) != 0) {
  26028. return -11605;
  26029. }
  26030. if (XMEMCMP(out, sigPh2, sizeof(sigPh2)))
  26031. return -11606;
  26032. #if defined(HAVE_ED448_VERIFY)
  26033. /* test verify on good msg */
  26034. if (wc_ed448ph_verify_msg(out, outlen, msgPh, sizeof(msgPh), &verify, &key,
  26035. contextPh2, sizeof(contextPh2)) != 0 ||
  26036. verify != 1) {
  26037. return -11607;
  26038. }
  26039. #endif
  26040. if (wc_ed448ph_sign_hash(hashPh, sizeof(hashPh), out, &outlen, &key, NULL,
  26041. 0) != 0) {
  26042. return -11608;
  26043. }
  26044. if (XMEMCMP(out, sigPh1, sizeof(sigPh1)))
  26045. return -11609;
  26046. #if defined(HAVE_ED448_VERIFY)
  26047. if (wc_ed448ph_verify_hash(out, outlen, hashPh, sizeof(hashPh), &verify,
  26048. &key, NULL, 0) != 0 || verify != 1) {
  26049. return -11610;
  26050. }
  26051. #endif
  26052. if (wc_ed448ph_sign_hash(hashPh, sizeof(hashPh), out, &outlen, &key,
  26053. contextPh2, sizeof(contextPh2)) != 0) {
  26054. return -11611;
  26055. }
  26056. if (XMEMCMP(out, sigPh2, sizeof(sigPh2)))
  26057. return -11612;
  26058. #if defined(HAVE_ED448_VERIFY)
  26059. if (wc_ed448ph_verify_hash(out, outlen, hashPh, sizeof(hashPh), &verify,
  26060. &key, contextPh2, sizeof(contextPh2)) != 0 ||
  26061. verify != 1) {
  26062. return -11613;
  26063. }
  26064. #endif
  26065. wc_ed448_free(&key);
  26066. return 0;
  26067. }
  26068. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_EXPORT && HAVE_ED448_KEY_IMPORT */
  26069. WOLFSSL_TEST_SUBROUTINE int ed448_test(void)
  26070. {
  26071. int ret;
  26072. WC_RNG rng;
  26073. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_EXPORT) &&\
  26074. defined(HAVE_ED448_KEY_IMPORT)
  26075. byte out[ED448_SIG_SIZE];
  26076. int i;
  26077. word32 outlen;
  26078. #ifdef HAVE_ED448_VERIFY
  26079. #ifdef WOLFSSL_ED448_STREAMING_VERIFY
  26080. int j;
  26081. #endif /* WOLFSSL_ED448_STREAMING_VERIFY */
  26082. int verify;
  26083. #endif /* HAVE_ED448_VERIFY */
  26084. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_EXPORT && HAVE_ED448_KEY_IMPORT */
  26085. word32 keySz, sigSz;
  26086. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  26087. ed448_key *key = NULL;
  26088. ed448_key *key2 = NULL;
  26089. #else
  26090. ed448_key key[1];
  26091. ed448_key key2[1];
  26092. #endif
  26093. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_EXPORT) && \
  26094. defined(HAVE_ED448_KEY_IMPORT)
  26095. /* test vectors from
  26096. https://tools.ietf.org/html/rfc8032
  26097. */
  26098. WOLFSSL_SMALL_STACK_STATIC const byte sKey1[] = {
  26099. 0x6c, 0x82, 0xa5, 0x62, 0xcb, 0x80, 0x8d, 0x10,
  26100. 0xd6, 0x32, 0xbe, 0x89, 0xc8, 0x51, 0x3e, 0xbf,
  26101. 0x6c, 0x92, 0x9f, 0x34, 0xdd, 0xfa, 0x8c, 0x9f,
  26102. 0x63, 0xc9, 0x96, 0x0e, 0xf6, 0xe3, 0x48, 0xa3,
  26103. 0x52, 0x8c, 0x8a, 0x3f, 0xcc, 0x2f, 0x04, 0x4e,
  26104. 0x39, 0xa3, 0xfc, 0x5b, 0x94, 0x49, 0x2f, 0x8f,
  26105. 0x03, 0x2e, 0x75, 0x49, 0xa2, 0x00, 0x98, 0xf9,
  26106. 0x5b
  26107. };
  26108. WOLFSSL_SMALL_STACK_STATIC const byte sKey2[] = {
  26109. 0xc4, 0xea, 0xb0, 0x5d, 0x35, 0x70, 0x07, 0xc6,
  26110. 0x32, 0xf3, 0xdb, 0xb4, 0x84, 0x89, 0x92, 0x4d,
  26111. 0x55, 0x2b, 0x08, 0xfe, 0x0c, 0x35, 0x3a, 0x0d,
  26112. 0x4a, 0x1f, 0x00, 0xac, 0xda, 0x2c, 0x46, 0x3a,
  26113. 0xfb, 0xea, 0x67, 0xc5, 0xe8, 0xd2, 0x87, 0x7c,
  26114. 0x5e, 0x3b, 0xc3, 0x97, 0xa6, 0x59, 0x94, 0x9e,
  26115. 0xf8, 0x02, 0x1e, 0x95, 0x4e, 0x0a, 0x12, 0x27,
  26116. 0x4e
  26117. };
  26118. WOLFSSL_SMALL_STACK_STATIC const byte sKey3[] = {
  26119. 0x25, 0x8c, 0xdd, 0x4a, 0xda, 0x32, 0xed, 0x9c,
  26120. 0x9f, 0xf5, 0x4e, 0x63, 0x75, 0x6a, 0xe5, 0x82,
  26121. 0xfb, 0x8f, 0xab, 0x2a, 0xc7, 0x21, 0xf2, 0xc8,
  26122. 0xe6, 0x76, 0xa7, 0x27, 0x68, 0x51, 0x3d, 0x93,
  26123. 0x9f, 0x63, 0xdd, 0xdb, 0x55, 0x60, 0x91, 0x33,
  26124. 0xf2, 0x9a, 0xdf, 0x86, 0xec, 0x99, 0x29, 0xdc,
  26125. 0xcb, 0x52, 0xc1, 0xc5, 0xfd, 0x2f, 0xf7, 0xe2,
  26126. 0x1b
  26127. };
  26128. /* uncompressed test */
  26129. WOLFSSL_SMALL_STACK_STATIC const byte sKey4[] = {
  26130. 0x6c, 0x82, 0xa5, 0x62, 0xcb, 0x80, 0x8d, 0x10,
  26131. 0xd6, 0x32, 0xbe, 0x89, 0xc8, 0x51, 0x3e, 0xbf,
  26132. 0x6c, 0x92, 0x9f, 0x34, 0xdd, 0xfa, 0x8c, 0x9f,
  26133. 0x63, 0xc9, 0x96, 0x0e, 0xf6, 0xe3, 0x48, 0xa3,
  26134. 0x52, 0x8c, 0x8a, 0x3f, 0xcc, 0x2f, 0x04, 0x4e,
  26135. 0x39, 0xa3, 0xfc, 0x5b, 0x94, 0x49, 0x2f, 0x8f,
  26136. 0x03, 0x2e, 0x75, 0x49, 0xa2, 0x00, 0x98, 0xf9,
  26137. 0x5b
  26138. };
  26139. /* compressed prefix test */
  26140. WOLFSSL_SMALL_STACK_STATIC const byte sKey5[] = {
  26141. 0x6c, 0x82, 0xa5, 0x62, 0xcb, 0x80, 0x8d, 0x10,
  26142. 0xd6, 0x32, 0xbe, 0x89, 0xc8, 0x51, 0x3e, 0xbf,
  26143. 0x6c, 0x92, 0x9f, 0x34, 0xdd, 0xfa, 0x8c, 0x9f,
  26144. 0x63, 0xc9, 0x96, 0x0e, 0xf6, 0xe3, 0x48, 0xa3,
  26145. 0x52, 0x8c, 0x8a, 0x3f, 0xcc, 0x2f, 0x04, 0x4e,
  26146. 0x39, 0xa3, 0xfc, 0x5b, 0x94, 0x49, 0x2f, 0x8f,
  26147. 0x03, 0x2e, 0x75, 0x49, 0xa2, 0x00, 0x98, 0xf9,
  26148. 0x5b
  26149. };
  26150. WOLFSSL_SMALL_STACK_STATIC const byte sKey6[] = {
  26151. 0x87, 0x2d, 0x09, 0x37, 0x80, 0xf5, 0xd3, 0x73,
  26152. 0x0d, 0xf7, 0xc2, 0x12, 0x66, 0x4b, 0x37, 0xb8,
  26153. 0xa0, 0xf2, 0x4f, 0x56, 0x81, 0x0d, 0xaa, 0x83,
  26154. 0x82, 0xcd, 0x4f, 0xa3, 0xf7, 0x76, 0x34, 0xec,
  26155. 0x44, 0xdc, 0x54, 0xf1, 0xc2, 0xed, 0x9b, 0xea,
  26156. 0x86, 0xfa, 0xfb, 0x76, 0x32, 0xd8, 0xbe, 0x19,
  26157. 0x9e, 0xa1, 0x65, 0xf5, 0xad, 0x55, 0xdd, 0x9c,
  26158. 0xe8
  26159. };
  26160. WOLFSSL_SMALL_STACK_STATIC const byte* sKeys[] = {sKey1, sKey2, sKey3, sKey4, sKey5, sKey6};
  26161. WOLFSSL_SMALL_STACK_STATIC const byte pKey1[] = {
  26162. 0x5f, 0xd7, 0x44, 0x9b, 0x59, 0xb4, 0x61, 0xfd,
  26163. 0x2c, 0xe7, 0x87, 0xec, 0x61, 0x6a, 0xd4, 0x6a,
  26164. 0x1d, 0xa1, 0x34, 0x24, 0x85, 0xa7, 0x0e, 0x1f,
  26165. 0x8a, 0x0e, 0xa7, 0x5d, 0x80, 0xe9, 0x67, 0x78,
  26166. 0xed, 0xf1, 0x24, 0x76, 0x9b, 0x46, 0xc7, 0x06,
  26167. 0x1b, 0xd6, 0x78, 0x3d, 0xf1, 0xe5, 0x0f, 0x6c,
  26168. 0xd1, 0xfa, 0x1a, 0xbe, 0xaf, 0xe8, 0x25, 0x61,
  26169. 0x80
  26170. };
  26171. WOLFSSL_SMALL_STACK_STATIC const byte pKey2[] = {
  26172. 0x43, 0xba, 0x28, 0xf4, 0x30, 0xcd, 0xff, 0x45,
  26173. 0x6a, 0xe5, 0x31, 0x54, 0x5f, 0x7e, 0xcd, 0x0a,
  26174. 0xc8, 0x34, 0xa5, 0x5d, 0x93, 0x58, 0xc0, 0x37,
  26175. 0x2b, 0xfa, 0x0c, 0x6c, 0x67, 0x98, 0xc0, 0x86,
  26176. 0x6a, 0xea, 0x01, 0xeb, 0x00, 0x74, 0x28, 0x02,
  26177. 0xb8, 0x43, 0x8e, 0xa4, 0xcb, 0x82, 0x16, 0x9c,
  26178. 0x23, 0x51, 0x60, 0x62, 0x7b, 0x4c, 0x3a, 0x94,
  26179. 0x80
  26180. };
  26181. WOLFSSL_SMALL_STACK_STATIC const byte pKey3[] = {
  26182. 0x3b, 0xa1, 0x6d, 0xa0, 0xc6, 0xf2, 0xcc, 0x1f,
  26183. 0x30, 0x18, 0x77, 0x40, 0x75, 0x6f, 0x5e, 0x79,
  26184. 0x8d, 0x6b, 0xc5, 0xfc, 0x01, 0x5d, 0x7c, 0x63,
  26185. 0xcc, 0x95, 0x10, 0xee, 0x3f, 0xd4, 0x4a, 0xdc,
  26186. 0x24, 0xd8, 0xe9, 0x68, 0xb6, 0xe4, 0x6e, 0x6f,
  26187. 0x94, 0xd1, 0x9b, 0x94, 0x53, 0x61, 0x72, 0x6b,
  26188. 0xd7, 0x5e, 0x14, 0x9e, 0xf0, 0x98, 0x17, 0xf5,
  26189. 0x80
  26190. };
  26191. /* uncompressed test */
  26192. WOLFSSL_SMALL_STACK_STATIC const byte pKey4[] = {
  26193. 0x5f, 0xd7, 0x44, 0x9b, 0x59, 0xb4, 0x61, 0xfd,
  26194. 0x2c, 0xe7, 0x87, 0xec, 0x61, 0x6a, 0xd4, 0x6a,
  26195. 0x1d, 0xa1, 0x34, 0x24, 0x85, 0xa7, 0x0e, 0x1f,
  26196. 0x8a, 0x0e, 0xa7, 0x5d, 0x80, 0xe9, 0x67, 0x78,
  26197. 0xed, 0xf1, 0x24, 0x76, 0x9b, 0x46, 0xc7, 0x06,
  26198. 0x1b, 0xd6, 0x78, 0x3d, 0xf1, 0xe5, 0x0f, 0x6c,
  26199. 0xd1, 0xfa, 0x1a, 0xbe, 0xaf, 0xe8, 0x25, 0x61,
  26200. 0x80
  26201. };
  26202. /* compressed prefix */
  26203. WOLFSSL_SMALL_STACK_STATIC const byte pKey5[] = {
  26204. 0x5f, 0xd7, 0x44, 0x9b, 0x59, 0xb4, 0x61, 0xfd,
  26205. 0x2c, 0xe7, 0x87, 0xec, 0x61, 0x6a, 0xd4, 0x6a,
  26206. 0x1d, 0xa1, 0x34, 0x24, 0x85, 0xa7, 0x0e, 0x1f,
  26207. 0x8a, 0x0e, 0xa7, 0x5d, 0x80, 0xe9, 0x67, 0x78,
  26208. 0xed, 0xf1, 0x24, 0x76, 0x9b, 0x46, 0xc7, 0x06,
  26209. 0x1b, 0xd6, 0x78, 0x3d, 0xf1, 0xe5, 0x0f, 0x6c,
  26210. 0xd1, 0xfa, 0x1a, 0xbe, 0xaf, 0xe8, 0x25, 0x61,
  26211. 0x80
  26212. };
  26213. WOLFSSL_SMALL_STACK_STATIC const byte pKey6[] = {
  26214. 0xa8, 0x1b, 0x2e, 0x8a, 0x70, 0xa5, 0xac, 0x94,
  26215. 0xff, 0xdb, 0xcc, 0x9b, 0xad, 0xfc, 0x3f, 0xeb,
  26216. 0x08, 0x01, 0xf2, 0x58, 0x57, 0x8b, 0xb1, 0x14,
  26217. 0xad, 0x44, 0xec, 0xe1, 0xec, 0x0e, 0x79, 0x9d,
  26218. 0xa0, 0x8e, 0xff, 0xb8, 0x1c, 0x5d, 0x68, 0x5c,
  26219. 0x0c, 0x56, 0xf6, 0x4e, 0xec, 0xae, 0xf8, 0xcd,
  26220. 0xf1, 0x1c, 0xc3, 0x87, 0x37, 0x83, 0x8c, 0xf4,
  26221. 0x00
  26222. };
  26223. WOLFSSL_SMALL_STACK_STATIC const byte* pKeys[] = {pKey1, pKey2, pKey3, pKey4, pKey5, pKey6};
  26224. WOLFSSL_SMALL_STACK_STATIC const byte pKeySz[] = {sizeof(pKey1), sizeof(pKey2), sizeof(pKey3),
  26225. sizeof(pKey4), sizeof(pKey5), sizeof(pKey6)};
  26226. WOLFSSL_SMALL_STACK_STATIC const byte sig1[] = {
  26227. 0x53, 0x3a, 0x37, 0xf6, 0xbb, 0xe4, 0x57, 0x25,
  26228. 0x1f, 0x02, 0x3c, 0x0d, 0x88, 0xf9, 0x76, 0xae,
  26229. 0x2d, 0xfb, 0x50, 0x4a, 0x84, 0x3e, 0x34, 0xd2,
  26230. 0x07, 0x4f, 0xd8, 0x23, 0xd4, 0x1a, 0x59, 0x1f,
  26231. 0x2b, 0x23, 0x3f, 0x03, 0x4f, 0x62, 0x82, 0x81,
  26232. 0xf2, 0xfd, 0x7a, 0x22, 0xdd, 0xd4, 0x7d, 0x78,
  26233. 0x28, 0xc5, 0x9b, 0xd0, 0xa2, 0x1b, 0xfd, 0x39,
  26234. 0x80, 0xff, 0x0d, 0x20, 0x28, 0xd4, 0xb1, 0x8a,
  26235. 0x9d, 0xf6, 0x3e, 0x00, 0x6c, 0x5d, 0x1c, 0x2d,
  26236. 0x34, 0x5b, 0x92, 0x5d, 0x8d, 0xc0, 0x0b, 0x41,
  26237. 0x04, 0x85, 0x2d, 0xb9, 0x9a, 0xc5, 0xc7, 0xcd,
  26238. 0xda, 0x85, 0x30, 0xa1, 0x13, 0xa0, 0xf4, 0xdb,
  26239. 0xb6, 0x11, 0x49, 0xf0, 0x5a, 0x73, 0x63, 0x26,
  26240. 0x8c, 0x71, 0xd9, 0x58, 0x08, 0xff, 0x2e, 0x65,
  26241. 0x26, 0x00
  26242. };
  26243. WOLFSSL_SMALL_STACK_STATIC const byte sig2[] = {
  26244. 0x26, 0xb8, 0xf9, 0x17, 0x27, 0xbd, 0x62, 0x89,
  26245. 0x7a, 0xf1, 0x5e, 0x41, 0xeb, 0x43, 0xc3, 0x77,
  26246. 0xef, 0xb9, 0xc6, 0x10, 0xd4, 0x8f, 0x23, 0x35,
  26247. 0xcb, 0x0b, 0xd0, 0x08, 0x78, 0x10, 0xf4, 0x35,
  26248. 0x25, 0x41, 0xb1, 0x43, 0xc4, 0xb9, 0x81, 0xb7,
  26249. 0xe1, 0x8f, 0x62, 0xde, 0x8c, 0xcd, 0xf6, 0x33,
  26250. 0xfc, 0x1b, 0xf0, 0x37, 0xab, 0x7c, 0xd7, 0x79,
  26251. 0x80, 0x5e, 0x0d, 0xbc, 0xc0, 0xaa, 0xe1, 0xcb,
  26252. 0xce, 0xe1, 0xaf, 0xb2, 0xe0, 0x27, 0xdf, 0x36,
  26253. 0xbc, 0x04, 0xdc, 0xec, 0xbf, 0x15, 0x43, 0x36,
  26254. 0xc1, 0x9f, 0x0a, 0xf7, 0xe0, 0xa6, 0x47, 0x29,
  26255. 0x05, 0xe7, 0x99, 0xf1, 0x95, 0x3d, 0x2a, 0x0f,
  26256. 0xf3, 0x34, 0x8a, 0xb2, 0x1a, 0xa4, 0xad, 0xaf,
  26257. 0xd1, 0xd2, 0x34, 0x44, 0x1c, 0xf8, 0x07, 0xc0,
  26258. 0x3a, 0x00
  26259. };
  26260. WOLFSSL_SMALL_STACK_STATIC const byte sig3[] = {
  26261. 0x7e, 0xee, 0xab, 0x7c, 0x4e, 0x50, 0xfb, 0x79,
  26262. 0x9b, 0x41, 0x8e, 0xe5, 0xe3, 0x19, 0x7f, 0xf6,
  26263. 0xbf, 0x15, 0xd4, 0x3a, 0x14, 0xc3, 0x43, 0x89,
  26264. 0xb5, 0x9d, 0xd1, 0xa7, 0xb1, 0xb8, 0x5b, 0x4a,
  26265. 0xe9, 0x04, 0x38, 0xac, 0xa6, 0x34, 0xbe, 0xa4,
  26266. 0x5e, 0x3a, 0x26, 0x95, 0xf1, 0x27, 0x0f, 0x07,
  26267. 0xfd, 0xcd, 0xf7, 0xc6, 0x2b, 0x8e, 0xfe, 0xaf,
  26268. 0x00, 0xb4, 0x5c, 0x2c, 0x96, 0xba, 0x45, 0x7e,
  26269. 0xb1, 0xa8, 0xbf, 0x07, 0x5a, 0x3d, 0xb2, 0x8e,
  26270. 0x5c, 0x24, 0xf6, 0xb9, 0x23, 0xed, 0x4a, 0xd7,
  26271. 0x47, 0xc3, 0xc9, 0xe0, 0x3c, 0x70, 0x79, 0xef,
  26272. 0xb8, 0x7c, 0xb1, 0x10, 0xd3, 0xa9, 0x98, 0x61,
  26273. 0xe7, 0x20, 0x03, 0xcb, 0xae, 0x6d, 0x6b, 0x8b,
  26274. 0x82, 0x7e, 0x4e, 0x6c, 0x14, 0x30, 0x64, 0xff,
  26275. 0x3c, 0x00
  26276. };
  26277. /* uncompressed test */
  26278. WOLFSSL_SMALL_STACK_STATIC const byte sig4[] = {
  26279. 0x53, 0x3a, 0x37, 0xf6, 0xbb, 0xe4, 0x57, 0x25,
  26280. 0x1f, 0x02, 0x3c, 0x0d, 0x88, 0xf9, 0x76, 0xae,
  26281. 0x2d, 0xfb, 0x50, 0x4a, 0x84, 0x3e, 0x34, 0xd2,
  26282. 0x07, 0x4f, 0xd8, 0x23, 0xd4, 0x1a, 0x59, 0x1f,
  26283. 0x2b, 0x23, 0x3f, 0x03, 0x4f, 0x62, 0x82, 0x81,
  26284. 0xf2, 0xfd, 0x7a, 0x22, 0xdd, 0xd4, 0x7d, 0x78,
  26285. 0x28, 0xc5, 0x9b, 0xd0, 0xa2, 0x1b, 0xfd, 0x39,
  26286. 0x80, 0xff, 0x0d, 0x20, 0x28, 0xd4, 0xb1, 0x8a,
  26287. 0x9d, 0xf6, 0x3e, 0x00, 0x6c, 0x5d, 0x1c, 0x2d,
  26288. 0x34, 0x5b, 0x92, 0x5d, 0x8d, 0xc0, 0x0b, 0x41,
  26289. 0x04, 0x85, 0x2d, 0xb9, 0x9a, 0xc5, 0xc7, 0xcd,
  26290. 0xda, 0x85, 0x30, 0xa1, 0x13, 0xa0, 0xf4, 0xdb,
  26291. 0xb6, 0x11, 0x49, 0xf0, 0x5a, 0x73, 0x63, 0x26,
  26292. 0x8c, 0x71, 0xd9, 0x58, 0x08, 0xff, 0x2e, 0x65,
  26293. 0x26, 0x00
  26294. };
  26295. /* compressed prefix */
  26296. WOLFSSL_SMALL_STACK_STATIC const byte sig5[] = {
  26297. 0x53, 0x3a, 0x37, 0xf6, 0xbb, 0xe4, 0x57, 0x25,
  26298. 0x1f, 0x02, 0x3c, 0x0d, 0x88, 0xf9, 0x76, 0xae,
  26299. 0x2d, 0xfb, 0x50, 0x4a, 0x84, 0x3e, 0x34, 0xd2,
  26300. 0x07, 0x4f, 0xd8, 0x23, 0xd4, 0x1a, 0x59, 0x1f,
  26301. 0x2b, 0x23, 0x3f, 0x03, 0x4f, 0x62, 0x82, 0x81,
  26302. 0xf2, 0xfd, 0x7a, 0x22, 0xdd, 0xd4, 0x7d, 0x78,
  26303. 0x28, 0xc5, 0x9b, 0xd0, 0xa2, 0x1b, 0xfd, 0x39,
  26304. 0x80, 0xff, 0x0d, 0x20, 0x28, 0xd4, 0xb1, 0x8a,
  26305. 0x9d, 0xf6, 0x3e, 0x00, 0x6c, 0x5d, 0x1c, 0x2d,
  26306. 0x34, 0x5b, 0x92, 0x5d, 0x8d, 0xc0, 0x0b, 0x41,
  26307. 0x04, 0x85, 0x2d, 0xb9, 0x9a, 0xc5, 0xc7, 0xcd,
  26308. 0xda, 0x85, 0x30, 0xa1, 0x13, 0xa0, 0xf4, 0xdb,
  26309. 0xb6, 0x11, 0x49, 0xf0, 0x5a, 0x73, 0x63, 0x26,
  26310. 0x8c, 0x71, 0xd9, 0x58, 0x08, 0xff, 0x2e, 0x65,
  26311. 0x26, 0x00
  26312. };
  26313. WOLFSSL_SMALL_STACK_STATIC const byte sig6[] = {
  26314. 0xe3, 0x01, 0x34, 0x5a, 0x41, 0xa3, 0x9a, 0x4d,
  26315. 0x72, 0xff, 0xf8, 0xdf, 0x69, 0xc9, 0x80, 0x75,
  26316. 0xa0, 0xcc, 0x08, 0x2b, 0x80, 0x2f, 0xc9, 0xb2,
  26317. 0xb6, 0xbc, 0x50, 0x3f, 0x92, 0x6b, 0x65, 0xbd,
  26318. 0xdf, 0x7f, 0x4c, 0x8f, 0x1c, 0xb4, 0x9f, 0x63,
  26319. 0x96, 0xaf, 0xc8, 0xa7, 0x0a, 0xbe, 0x6d, 0x8a,
  26320. 0xef, 0x0d, 0xb4, 0x78, 0xd4, 0xc6, 0xb2, 0x97,
  26321. 0x00, 0x76, 0xc6, 0xa0, 0x48, 0x4f, 0xe7, 0x6d,
  26322. 0x76, 0xb3, 0xa9, 0x76, 0x25, 0xd7, 0x9f, 0x1c,
  26323. 0xe2, 0x40, 0xe7, 0xc5, 0x76, 0x75, 0x0d, 0x29,
  26324. 0x55, 0x28, 0x28, 0x6f, 0x71, 0x9b, 0x41, 0x3d,
  26325. 0xe9, 0xad, 0xa3, 0xe8, 0xeb, 0x78, 0xed, 0x57,
  26326. 0x36, 0x03, 0xce, 0x30, 0xd8, 0xbb, 0x76, 0x17,
  26327. 0x85, 0xdc, 0x30, 0xdb, 0xc3, 0x20, 0x86, 0x9e,
  26328. 0x1a, 0x00
  26329. };
  26330. WOLFSSL_SMALL_STACK_STATIC const byte* sigs[] = {sig1, sig2, sig3, sig4, sig5, sig6};
  26331. #define SIGSZ sizeof(sig1)
  26332. PEDANTIC_EXTENSION WOLFSSL_SMALL_STACK_STATIC const byte msg1[] = { };
  26333. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] = { 0x03 };
  26334. WOLFSSL_SMALL_STACK_STATIC const byte msg3[] = { 0x64, 0xa6, 0x5f, 0x3c, 0xde, 0xdc, 0xdd,
  26335. 0x66, 0x81, 0x1e, 0x29, 0x15 };
  26336. /* test of a 1023 byte long message */
  26337. WOLFSSL_SMALL_STACK_STATIC const byte msg4[] = {
  26338. 0x6d, 0xdf, 0x80, 0x2e, 0x1a, 0xae, 0x49, 0x86,
  26339. 0x93, 0x5f, 0x7f, 0x98, 0x1b, 0xa3, 0xf0, 0x35,
  26340. 0x1d, 0x62, 0x73, 0xc0, 0xa0, 0xc2, 0x2c, 0x9c,
  26341. 0x0e, 0x83, 0x39, 0x16, 0x8e, 0x67, 0x54, 0x12,
  26342. 0xa3, 0xde, 0xbf, 0xaf, 0x43, 0x5e, 0xd6, 0x51,
  26343. 0x55, 0x80, 0x07, 0xdb, 0x43, 0x84, 0xb6, 0x50,
  26344. 0xfc, 0xc0, 0x7e, 0x3b, 0x58, 0x6a, 0x27, 0xa4,
  26345. 0xf7, 0xa0, 0x0a, 0xc8, 0xa6, 0xfe, 0xc2, 0xcd,
  26346. 0x86, 0xae, 0x4b, 0xf1, 0x57, 0x0c, 0x41, 0xe6,
  26347. 0xa4, 0x0c, 0x93, 0x1d, 0xb2, 0x7b, 0x2f, 0xaa,
  26348. 0x15, 0xa8, 0xce, 0xdd, 0x52, 0xcf, 0xf7, 0x36,
  26349. 0x2c, 0x4e, 0x6e, 0x23, 0xda, 0xec, 0x0f, 0xbc,
  26350. 0x3a, 0x79, 0xb6, 0x80, 0x6e, 0x31, 0x6e, 0xfc,
  26351. 0xc7, 0xb6, 0x81, 0x19, 0xbf, 0x46, 0xbc, 0x76,
  26352. 0xa2, 0x60, 0x67, 0xa5, 0x3f, 0x29, 0x6d, 0xaf,
  26353. 0xdb, 0xdc, 0x11, 0xc7, 0x7f, 0x77, 0x77, 0xe9,
  26354. 0x72, 0x66, 0x0c, 0xf4, 0xb6, 0xa9, 0xb3, 0x69,
  26355. 0xa6, 0x66, 0x5f, 0x02, 0xe0, 0xcc, 0x9b, 0x6e,
  26356. 0xdf, 0xad, 0x13, 0x6b, 0x4f, 0xab, 0xe7, 0x23,
  26357. 0xd2, 0x81, 0x3d, 0xb3, 0x13, 0x6c, 0xfd, 0xe9,
  26358. 0xb6, 0xd0, 0x44, 0x32, 0x2f, 0xee, 0x29, 0x47,
  26359. 0x95, 0x2e, 0x03, 0x1b, 0x73, 0xab, 0x5c, 0x60,
  26360. 0x33, 0x49, 0xb3, 0x07, 0xbd, 0xc2, 0x7b, 0xc6,
  26361. 0xcb, 0x8b, 0x8b, 0xbd, 0x7b, 0xd3, 0x23, 0x21,
  26362. 0x9b, 0x80, 0x33, 0xa5, 0x81, 0xb5, 0x9e, 0xad,
  26363. 0xeb, 0xb0, 0x9b, 0x3c, 0x4f, 0x3d, 0x22, 0x77,
  26364. 0xd4, 0xf0, 0x34, 0x36, 0x24, 0xac, 0xc8, 0x17,
  26365. 0x80, 0x47, 0x28, 0xb2, 0x5a, 0xb7, 0x97, 0x17,
  26366. 0x2b, 0x4c, 0x5c, 0x21, 0xa2, 0x2f, 0x9c, 0x78,
  26367. 0x39, 0xd6, 0x43, 0x00, 0x23, 0x2e, 0xb6, 0x6e,
  26368. 0x53, 0xf3, 0x1c, 0x72, 0x3f, 0xa3, 0x7f, 0xe3,
  26369. 0x87, 0xc7, 0xd3, 0xe5, 0x0b, 0xdf, 0x98, 0x13,
  26370. 0xa3, 0x0e, 0x5b, 0xb1, 0x2c, 0xf4, 0xcd, 0x93,
  26371. 0x0c, 0x40, 0xcf, 0xb4, 0xe1, 0xfc, 0x62, 0x25,
  26372. 0x92, 0xa4, 0x95, 0x88, 0x79, 0x44, 0x94, 0xd5,
  26373. 0x6d, 0x24, 0xea, 0x4b, 0x40, 0xc8, 0x9f, 0xc0,
  26374. 0x59, 0x6c, 0xc9, 0xeb, 0xb9, 0x61, 0xc8, 0xcb,
  26375. 0x10, 0xad, 0xde, 0x97, 0x6a, 0x5d, 0x60, 0x2b,
  26376. 0x1c, 0x3f, 0x85, 0xb9, 0xb9, 0xa0, 0x01, 0xed,
  26377. 0x3c, 0x6a, 0x4d, 0x3b, 0x14, 0x37, 0xf5, 0x20,
  26378. 0x96, 0xcd, 0x19, 0x56, 0xd0, 0x42, 0xa5, 0x97,
  26379. 0xd5, 0x61, 0xa5, 0x96, 0xec, 0xd3, 0xd1, 0x73,
  26380. 0x5a, 0x8d, 0x57, 0x0e, 0xa0, 0xec, 0x27, 0x22,
  26381. 0x5a, 0x2c, 0x4a, 0xaf, 0xf2, 0x63, 0x06, 0xd1,
  26382. 0x52, 0x6c, 0x1a, 0xf3, 0xca, 0x6d, 0x9c, 0xf5,
  26383. 0xa2, 0xc9, 0x8f, 0x47, 0xe1, 0xc4, 0x6d, 0xb9,
  26384. 0xa3, 0x32, 0x34, 0xcf, 0xd4, 0xd8, 0x1f, 0x2c,
  26385. 0x98, 0x53, 0x8a, 0x09, 0xeb, 0xe7, 0x69, 0x98,
  26386. 0xd0, 0xd8, 0xfd, 0x25, 0x99, 0x7c, 0x7d, 0x25,
  26387. 0x5c, 0x6d, 0x66, 0xec, 0xe6, 0xfa, 0x56, 0xf1,
  26388. 0x11, 0x44, 0x95, 0x0f, 0x02, 0x77, 0x95, 0xe6,
  26389. 0x53, 0x00, 0x8f, 0x4b, 0xd7, 0xca, 0x2d, 0xee,
  26390. 0x85, 0xd8, 0xe9, 0x0f, 0x3d, 0xc3, 0x15, 0x13,
  26391. 0x0c, 0xe2, 0xa0, 0x03, 0x75, 0xa3, 0x18, 0xc7,
  26392. 0xc3, 0xd9, 0x7b, 0xe2, 0xc8, 0xce, 0x5b, 0x6d,
  26393. 0xb4, 0x1a, 0x62, 0x54, 0xff, 0x26, 0x4f, 0xa6,
  26394. 0x15, 0x5b, 0xae, 0xe3, 0xb0, 0x77, 0x3c, 0x0f,
  26395. 0x49, 0x7c, 0x57, 0x3f, 0x19, 0xbb, 0x4f, 0x42,
  26396. 0x40, 0x28, 0x1f, 0x0b, 0x1f, 0x4f, 0x7b, 0xe8,
  26397. 0x57, 0xa4, 0xe5, 0x9d, 0x41, 0x6c, 0x06, 0xb4,
  26398. 0xc5, 0x0f, 0xa0, 0x9e, 0x18, 0x10, 0xdd, 0xc6,
  26399. 0xb1, 0x46, 0x7b, 0xae, 0xac, 0x5a, 0x36, 0x68,
  26400. 0xd1, 0x1b, 0x6e, 0xca, 0xa9, 0x01, 0x44, 0x00,
  26401. 0x16, 0xf3, 0x89, 0xf8, 0x0a, 0xcc, 0x4d, 0xb9,
  26402. 0x77, 0x02, 0x5e, 0x7f, 0x59, 0x24, 0x38, 0x8c,
  26403. 0x7e, 0x34, 0x0a, 0x73, 0x2e, 0x55, 0x44, 0x40,
  26404. 0xe7, 0x65, 0x70, 0xf8, 0xdd, 0x71, 0xb7, 0xd6,
  26405. 0x40, 0xb3, 0x45, 0x0d, 0x1f, 0xd5, 0xf0, 0x41,
  26406. 0x0a, 0x18, 0xf9, 0xa3, 0x49, 0x4f, 0x70, 0x7c,
  26407. 0x71, 0x7b, 0x79, 0xb4, 0xbf, 0x75, 0xc9, 0x84,
  26408. 0x00, 0xb0, 0x96, 0xb2, 0x16, 0x53, 0xb5, 0xd2,
  26409. 0x17, 0xcf, 0x35, 0x65, 0xc9, 0x59, 0x74, 0x56,
  26410. 0xf7, 0x07, 0x03, 0x49, 0x7a, 0x07, 0x87, 0x63,
  26411. 0x82, 0x9b, 0xc0, 0x1b, 0xb1, 0xcb, 0xc8, 0xfa,
  26412. 0x04, 0xea, 0xdc, 0x9a, 0x6e, 0x3f, 0x66, 0x99,
  26413. 0x58, 0x7a, 0x9e, 0x75, 0xc9, 0x4e, 0x5b, 0xab,
  26414. 0x00, 0x36, 0xe0, 0xb2, 0xe7, 0x11, 0x39, 0x2c,
  26415. 0xff, 0x00, 0x47, 0xd0, 0xd6, 0xb0, 0x5b, 0xd2,
  26416. 0xa5, 0x88, 0xbc, 0x10, 0x97, 0x18, 0x95, 0x42,
  26417. 0x59, 0xf1, 0xd8, 0x66, 0x78, 0xa5, 0x79, 0xa3,
  26418. 0x12, 0x0f, 0x19, 0xcf, 0xb2, 0x96, 0x3f, 0x17,
  26419. 0x7a, 0xeb, 0x70, 0xf2, 0xd4, 0x84, 0x48, 0x26,
  26420. 0x26, 0x2e, 0x51, 0xb8, 0x02, 0x71, 0x27, 0x20,
  26421. 0x68, 0xef, 0x5b, 0x38, 0x56, 0xfa, 0x85, 0x35,
  26422. 0xaa, 0x2a, 0x88, 0xb2, 0xd4, 0x1f, 0x2a, 0x0e,
  26423. 0x2f, 0xda, 0x76, 0x24, 0xc2, 0x85, 0x02, 0x72,
  26424. 0xac, 0x4a, 0x2f, 0x56, 0x1f, 0x8f, 0x2f, 0x7a,
  26425. 0x31, 0x8b, 0xfd, 0x5c, 0xaf, 0x96, 0x96, 0x14,
  26426. 0x9e, 0x4a, 0xc8, 0x24, 0xad, 0x34, 0x60, 0x53,
  26427. 0x8f, 0xdc, 0x25, 0x42, 0x1b, 0xee, 0xc2, 0xcc,
  26428. 0x68, 0x18, 0x16, 0x2d, 0x06, 0xbb, 0xed, 0x0c,
  26429. 0x40, 0xa3, 0x87, 0x19, 0x23, 0x49, 0xdb, 0x67,
  26430. 0xa1, 0x18, 0xba, 0xda, 0x6c, 0xd5, 0xab, 0x01,
  26431. 0x40, 0xee, 0x27, 0x32, 0x04, 0xf6, 0x28, 0xaa,
  26432. 0xd1, 0xc1, 0x35, 0xf7, 0x70, 0x27, 0x9a, 0x65,
  26433. 0x1e, 0x24, 0xd8, 0xc1, 0x4d, 0x75, 0xa6, 0x05,
  26434. 0x9d, 0x76, 0xb9, 0x6a, 0x6f, 0xd8, 0x57, 0xde,
  26435. 0xf5, 0xe0, 0xb3, 0x54, 0xb2, 0x7a, 0xb9, 0x37,
  26436. 0xa5, 0x81, 0x5d, 0x16, 0xb5, 0xfa, 0xe4, 0x07,
  26437. 0xff, 0x18, 0x22, 0x2c, 0x6d, 0x1e, 0xd2, 0x63,
  26438. 0xbe, 0x68, 0xc9, 0x5f, 0x32, 0xd9, 0x08, 0xbd,
  26439. 0x89, 0x5c, 0xd7, 0x62, 0x07, 0xae, 0x72, 0x64,
  26440. 0x87, 0x56, 0x7f, 0x9a, 0x67, 0xda, 0xd7, 0x9a,
  26441. 0xbe, 0xc3, 0x16, 0xf6, 0x83, 0xb1, 0x7f, 0x2d,
  26442. 0x02, 0xbf, 0x07, 0xe0, 0xac, 0x8b, 0x5b, 0xc6,
  26443. 0x16, 0x2c, 0xf9, 0x46, 0x97, 0xb3, 0xc2, 0x7c,
  26444. 0xd1, 0xfe, 0xa4, 0x9b, 0x27, 0xf2, 0x3b, 0xa2,
  26445. 0x90, 0x18, 0x71, 0x96, 0x25, 0x06, 0x52, 0x0c,
  26446. 0x39, 0x2d, 0xa8, 0xb6, 0xad, 0x0d, 0x99, 0xf7,
  26447. 0x01, 0x3f, 0xbc, 0x06, 0xc2, 0xc1, 0x7a, 0x56,
  26448. 0x95, 0x00, 0xc8, 0xa7, 0x69, 0x64, 0x81, 0xc1,
  26449. 0xcd, 0x33, 0xe9, 0xb1, 0x4e, 0x40, 0xb8, 0x2e,
  26450. 0x79, 0xa5, 0xf5, 0xdb, 0x82, 0x57, 0x1b, 0xa9,
  26451. 0x7b, 0xae, 0x3a, 0xd3, 0xe0, 0x47, 0x95, 0x15,
  26452. 0xbb, 0x0e, 0x2b, 0x0f, 0x3b, 0xfc, 0xd1, 0xfd,
  26453. 0x33, 0x03, 0x4e, 0xfc, 0x62, 0x45, 0xed, 0xdd,
  26454. 0x7e, 0xe2, 0x08, 0x6d, 0xda, 0xe2, 0x60, 0x0d,
  26455. 0x8c, 0xa7, 0x3e, 0x21, 0x4e, 0x8c, 0x2b, 0x0b,
  26456. 0xdb, 0x2b, 0x04, 0x7c, 0x6a, 0x46, 0x4a, 0x56,
  26457. 0x2e, 0xd7, 0x7b, 0x73, 0xd2, 0xd8, 0x41, 0xc4,
  26458. 0xb3, 0x49, 0x73, 0x55, 0x12, 0x57, 0x71, 0x3b,
  26459. 0x75, 0x36, 0x32, 0xef, 0xba, 0x34, 0x81, 0x69,
  26460. 0xab, 0xc9, 0x0a, 0x68, 0xf4, 0x26, 0x11, 0xa4,
  26461. 0x01, 0x26, 0xd7, 0xcb, 0x21, 0xb5, 0x86, 0x95,
  26462. 0x56, 0x81, 0x86, 0xf7, 0xe5, 0x69, 0xd2, 0xff,
  26463. 0x0f, 0x9e, 0x74, 0x5d, 0x04, 0x87, 0xdd, 0x2e,
  26464. 0xb9, 0x97, 0xca, 0xfc, 0x5a, 0xbf, 0x9d, 0xd1,
  26465. 0x02, 0xe6, 0x2f, 0xf6, 0x6c, 0xba, 0x87
  26466. };
  26467. WOLFSSL_SMALL_STACK_STATIC const byte* msgs[] = {msg1, msg2, msg3, msg1, msg1, msg4};
  26468. WOLFSSL_SMALL_STACK_STATIC const word16 msgSz[] = {0 /*sizeof(msg1)*/,
  26469. sizeof(msg2),
  26470. sizeof(msg3),
  26471. 0 /*sizeof(msg1)*/,
  26472. 0 /*sizeof(msg1)*/,
  26473. sizeof(msg4)
  26474. };
  26475. #ifndef NO_ASN
  26476. static const byte privateEd448[] = {
  26477. 0x30, 0x47, 0x02, 0x01, 0x00, 0x30, 0x05, 0x06,
  26478. 0x03, 0x2b, 0x65, 0x71, 0x04, 0x3b, 0x04, 0x39,
  26479. 0x6c, 0x82, 0xa5, 0x62, 0xcb, 0x80, 0x8d, 0x10,
  26480. 0xd6, 0x32, 0xbe, 0x89, 0xc8, 0x51, 0x3e, 0xbf,
  26481. 0x6c, 0x92, 0x9f, 0x34, 0xdd, 0xfa, 0x8c, 0x9f,
  26482. 0x63, 0xc9, 0x96, 0x0e, 0xf6, 0xe3, 0x48, 0xa3,
  26483. 0x52, 0x8c, 0x8a, 0x3f, 0xcc, 0x2f, 0x04, 0x4e,
  26484. 0x39, 0xa3, 0xfc, 0x5b, 0x94, 0x49, 0x2f, 0x8f,
  26485. 0x03, 0x2e, 0x75, 0x49, 0xa2, 0x00, 0x98, 0xf9,
  26486. 0x5b
  26487. };
  26488. static const byte publicEd448[] = {
  26489. 0x30, 0x43, 0x30, 0x05, 0x06, 0x03, 0x2b, 0x65,
  26490. 0x71, 0x03, 0x3a, 0x00, 0x5f, 0xd7, 0x44, 0x9b,
  26491. 0x59, 0xb4, 0x61, 0xfd, 0x2c, 0xe7, 0x87, 0xec,
  26492. 0x61, 0x6a, 0xd4, 0x6a, 0x1d, 0xa1, 0x34, 0x24,
  26493. 0x85, 0xa7, 0x0e, 0x1f, 0x8a, 0x0e, 0xa7, 0x5d,
  26494. 0x80, 0xe9, 0x67, 0x78, 0xed, 0xf1, 0x24, 0x76,
  26495. 0x9b, 0x46, 0xc7, 0x06, 0x1b, 0xd6, 0x78, 0x3d,
  26496. 0xf1, 0xe5, 0x0f, 0x6c, 0xd1, 0xfa, 0x1a, 0xbe,
  26497. 0xaf, 0xe8, 0x25, 0x61, 0x80
  26498. };
  26499. static const byte privPubEd448[] = {
  26500. 0x30, 0x81, 0x82, 0x02, 0x01, 0x00, 0x30, 0x05,
  26501. 0x06, 0x03, 0x2b, 0x65, 0x71, 0x04, 0x3b, 0x04,
  26502. 0x39, 0x6c, 0x82, 0xa5, 0x62, 0xcb, 0x80, 0x8d,
  26503. 0x10, 0xd6, 0x32, 0xbe, 0x89, 0xc8, 0x51, 0x3e,
  26504. 0xbf, 0x6c, 0x92, 0x9f, 0x34, 0xdd, 0xfa, 0x8c,
  26505. 0x9f, 0x63, 0xc9, 0x96, 0x0e, 0xf6, 0xe3, 0x48,
  26506. 0xa3, 0x52, 0x8c, 0x8a, 0x3f, 0xcc, 0x2f, 0x04,
  26507. 0x4e, 0x39, 0xa3, 0xfc, 0x5b, 0x94, 0x49, 0x2f,
  26508. 0x8f, 0x03, 0x2e, 0x75, 0x49, 0xa2, 0x00, 0x98,
  26509. 0xf9, 0x5b, 0x81, 0x39, 0x5f, 0xd7, 0x44, 0x9b,
  26510. 0x59, 0xb4, 0x61, 0xfd, 0x2c, 0xe7, 0x87, 0xec,
  26511. 0x61, 0x6a, 0xd4, 0x6a, 0x1d, 0xa1, 0x34, 0x24,
  26512. 0x85, 0xa7, 0x0e, 0x1f, 0x8a, 0x0e, 0xa7, 0x5d,
  26513. 0x80, 0xe9, 0x67, 0x78, 0xed, 0xf1, 0x24, 0x76,
  26514. 0x9b, 0x46, 0xc7, 0x06, 0x1b, 0xd6, 0x78, 0x3d,
  26515. 0xf1, 0xe5, 0x0f, 0x6c, 0xd1, 0xfa, 0x1a, 0xbe,
  26516. 0xaf, 0xe8, 0x25, 0x61, 0x80
  26517. };
  26518. word32 idx;
  26519. #endif /* NO_ASN */
  26520. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_EXPORT && HAVE_ED448_KEY_IMPORT */
  26521. #if !defined(NO_ASN) && defined(HAVE_ED448_SIGN)
  26522. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  26523. ed448_key *key3 = NULL;
  26524. #else
  26525. ed448_key key3[1];
  26526. #endif
  26527. #endif
  26528. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  26529. key = (ed448_key *)XMALLOC(sizeof(*key), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26530. key2 = (ed448_key *)XMALLOC(sizeof(*key2), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26531. #if !defined(NO_ASN) && defined(HAVE_ED448_SIGN)
  26532. key3 = (ed448_key *)XMALLOC(sizeof(*key3), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26533. #endif
  26534. #endif
  26535. /* create ed448 keys */
  26536. #ifndef HAVE_FIPS
  26537. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  26538. #else
  26539. ret = wc_InitRng(&rng);
  26540. #endif
  26541. if (ret != 0) {
  26542. XMEMSET(&rng, 0, sizeof(rng));
  26543. ERROR_OUT(-11700, out);
  26544. }
  26545. if (wc_ed448_init(key) < 0)
  26546. ERROR_OUT(-11903, out);
  26547. if (wc_ed448_init(key2) < 0)
  26548. ERROR_OUT(-11904, out);
  26549. #if !defined(NO_ASN) && defined(HAVE_ED448_SIGN)
  26550. if (wc_ed448_init(key3) < 0)
  26551. ERROR_OUT(-11905, out);
  26552. #endif
  26553. if (wc_ed448_make_key(&rng, ED448_KEY_SIZE, key) < 0)
  26554. ERROR_OUT(-11906, out);
  26555. if (wc_ed448_make_key(&rng, ED448_KEY_SIZE, key2) < 0)
  26556. ERROR_OUT(-11907, out);
  26557. /* helper functions for signature and key size */
  26558. keySz = wc_ed448_size(key);
  26559. sigSz = wc_ed448_sig_size(key);
  26560. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_EXPORT) &&\
  26561. defined(HAVE_ED448_KEY_IMPORT)
  26562. for (i = 0; i < 6; i++) {
  26563. outlen = sizeof(out);
  26564. XMEMSET(out, 0, sizeof(out));
  26565. if (wc_ed448_import_private_key(sKeys[i], ED448_KEY_SIZE, pKeys[i],
  26566. pKeySz[i], key) != 0)
  26567. ERROR_OUT(-11701 - i, out);
  26568. if (wc_ed448_sign_msg(msgs[i], msgSz[i], out, &outlen, key, NULL,
  26569. 0) != 0)
  26570. ERROR_OUT(-11711 - i, out);
  26571. if (XMEMCMP(out, sigs[i], 114))
  26572. ERROR_OUT(-11721 - i, out);
  26573. #if defined(HAVE_ED448_VERIFY)
  26574. /* test verify on good msg */
  26575. if (wc_ed448_verify_msg(out, outlen, msgs[i], msgSz[i], &verify, key,
  26576. NULL, 0) != 0 || verify != 1)
  26577. ERROR_OUT(-11731 - i, out);
  26578. #ifdef WOLFSSL_ED448_STREAMING_VERIFY
  26579. /* test verify on good msg using streaming interface directly */
  26580. if (wc_ed448_verify_msg_init(out, outlen,
  26581. key, (byte)Ed448, NULL, 0) != 0)
  26582. ERROR_OUT(-11911 - i, out);
  26583. for (j = 0; j < msgSz[i]; j += i) {
  26584. if (wc_ed448_verify_msg_update(msgs[i] + j, MIN(i, msgSz[i] - j), key) != 0)
  26585. ERROR_OUT(-11921 - i, out);
  26586. }
  26587. if (wc_ed448_verify_msg_final(out, outlen, &verify,
  26588. key) != 0 || verify != 1)
  26589. ERROR_OUT(-11931 - i, out);
  26590. #endif /* WOLFSSL_ED448_STREAMING_VERIFY */
  26591. /* test verify on bad msg */
  26592. out[outlen-2] = out[outlen-2] + 1;
  26593. if (wc_ed448_verify_msg(out, outlen, msgs[i], msgSz[i], &verify, key,
  26594. NULL, 0) == 0 || verify == 1)
  26595. ERROR_OUT(-11741 - i, out);
  26596. #endif /* HAVE_ED448_VERIFY */
  26597. /* test api for import/exporting keys */
  26598. {
  26599. byte *exportPKey = NULL;
  26600. byte *exportSKey = NULL;
  26601. word32 exportPSz = ED448_KEY_SIZE;
  26602. word32 exportSSz = ED448_KEY_SIZE;
  26603. exportPKey = (byte *)XMALLOC(exportPSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26604. exportSKey = (byte *)XMALLOC(exportSSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26605. if ((exportPKey == NULL) || (exportSKey == NULL))
  26606. ERROR_OUT(-11902, out);
  26607. ret = 0;
  26608. do {
  26609. if (wc_ed448_export_public(key, exportPKey, &exportPSz) != 0) {
  26610. ret = -11751 - i;
  26611. break;
  26612. }
  26613. if (wc_ed448_import_public_ex(exportPKey, exportPSz, key2, 1) != 0) {
  26614. ret = -11761 - i;
  26615. break;
  26616. }
  26617. if (wc_ed448_export_private_only(key, exportSKey, &exportSSz) != 0) {
  26618. ret = -11771 - i;
  26619. break;
  26620. }
  26621. if (wc_ed448_import_private_key(exportSKey, exportSSz,
  26622. exportPKey, exportPSz, key2) != 0) {
  26623. ret = -11781 - i;
  26624. break;
  26625. }
  26626. /* clear "out" buffer and test sign with imported keys */
  26627. outlen = sizeof(out);
  26628. XMEMSET(out, 0, sizeof(out));
  26629. if (wc_ed448_sign_msg(msgs[i], msgSz[i], out, &outlen, key2, NULL,
  26630. 0) != 0) {
  26631. ret = -11791 - i;
  26632. break;
  26633. }
  26634. } while(0);
  26635. XFREE(exportPKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26636. XFREE(exportSKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26637. if (ret != 0)
  26638. goto out;
  26639. }
  26640. #if defined(HAVE_ED448_VERIFY)
  26641. if (wc_ed448_verify_msg(out, outlen, msgs[i], msgSz[i], &verify, key2,
  26642. NULL, 0) != 0 || verify != 1)
  26643. ERROR_OUT(-11801 - i, out);
  26644. if (XMEMCMP(out, sigs[i], SIGSZ))
  26645. ERROR_OUT(-11811 - i, out);
  26646. #endif /* HAVE_ED448_VERIFY */
  26647. }
  26648. ret = ed448_ctx_test();
  26649. if (ret != 0)
  26650. goto out;
  26651. ret = ed448ph_test();
  26652. if (ret != 0)
  26653. goto out;
  26654. #ifndef NO_ASN
  26655. /* Try ASN.1 encoded private-only key and public key. */
  26656. idx = 0;
  26657. if (wc_Ed448PrivateKeyDecode(privateEd448, &idx, key3,
  26658. sizeof(privateEd448)) != 0)
  26659. ERROR_OUT(-11821, out);
  26660. if (wc_ed448_sign_msg(msgs[0], msgSz[0], out, &outlen, key3, NULL, 0)
  26661. != BAD_FUNC_ARG)
  26662. ERROR_OUT(-11831, out);
  26663. idx = 0;
  26664. if (wc_Ed448PublicKeyDecode(publicEd448, &idx, key3,
  26665. sizeof(publicEd448)) != 0)
  26666. ERROR_OUT(-11841, out);
  26667. if (wc_ed448_sign_msg(msgs[0], msgSz[0], out, &outlen, key3, NULL, 0) != 0)
  26668. ERROR_OUT(-11851, out);
  26669. if (XMEMCMP(out, sigs[0], SIGSZ))
  26670. ERROR_OUT(-11861, out);
  26671. #if defined(HAVE_ED448_VERIFY)
  26672. /* test verify on good msg */
  26673. if (wc_ed448_verify_msg(out, outlen, msgs[0], msgSz[0], &verify, key3,
  26674. NULL, 0) != 0 || verify != 1)
  26675. ERROR_OUT(-11871, out);
  26676. #endif /* HAVE_ED448_VERIFY */
  26677. wc_ed448_free(key3);
  26678. if (wc_ed448_init(key3) < 0)
  26679. ERROR_OUT(-11908, out);
  26680. idx = 0;
  26681. if (wc_Ed448PrivateKeyDecode(privPubEd448, &idx, key3,
  26682. sizeof(privPubEd448)) != 0)
  26683. ERROR_OUT(-11881, out);
  26684. if (wc_ed448_sign_msg(msgs[0], msgSz[0], out, &outlen, key3, NULL, 0) != 0)
  26685. ERROR_OUT(-11891, out);
  26686. if (XMEMCMP(out, sigs[0], SIGSZ))
  26687. ERROR_OUT(-11901, out);
  26688. #endif /* NO_ASN */
  26689. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_EXPORT && HAVE_ED448_KEY_IMPORT */
  26690. ret = 0;
  26691. out:
  26692. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  26693. if (key) {
  26694. wc_ed448_free(key);
  26695. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26696. }
  26697. if (key2) {
  26698. wc_ed448_free(key2);
  26699. XFREE(key2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26700. }
  26701. #if !defined(NO_ASN) && defined(HAVE_ED448_SIGN)
  26702. if (key3) {
  26703. wc_ed448_free(key3);
  26704. XFREE(key3, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26705. }
  26706. #endif
  26707. #else
  26708. wc_ed448_free(key);
  26709. wc_ed448_free(key2);
  26710. #if !defined(NO_ASN) && defined(HAVE_ED448_SIGN)
  26711. wc_ed448_free(key3);
  26712. #endif
  26713. #endif
  26714. #if defined(HAVE_HASHDRBG) || defined(NO_RC4)
  26715. wc_FreeRng(&rng);
  26716. #endif
  26717. if (ret < 0)
  26718. return ret;
  26719. /* hush warnings of unused keySz and sigSz */
  26720. (void)keySz;
  26721. (void)sigSz;
  26722. #ifdef WOLFSSL_TEST_CERT
  26723. ret = ed448_test_cert();
  26724. if (ret < 0)
  26725. return ret;
  26726. #ifdef WOLFSSL_CERT_GEN
  26727. ret = ed448_test_make_cert();
  26728. if (ret < 0)
  26729. return ret;
  26730. #endif /* WOLFSSL_CERT_GEN */
  26731. #endif /* WOLFSSL_TEST_CERT */
  26732. return 0;
  26733. }
  26734. #endif /* HAVE_ED448 */
  26735. #ifdef WOLFSSL_HAVE_KYBER
  26736. #ifdef WOLFSSL_WC_KYBER /* OQS and PQM4 do not support KATs */
  26737. #ifdef WOLFSSL_KYBER512
  26738. static int kyber512_kat(void)
  26739. {
  26740. KyberKey key;
  26741. int ret;
  26742. byte priv[KYBER512_PRIVATE_KEY_SIZE];
  26743. byte pub[KYBER512_PUBLIC_KEY_SIZE];
  26744. byte ct[KYBER512_CIPHER_TEXT_SIZE];
  26745. byte ss[KYBER_SS_SZ];
  26746. byte ss_dec[KYBER_SS_SZ];
  26747. const byte kyber512_rand[] = {
  26748. 0x7c, 0x99, 0x35, 0xa0, 0xb0, 0x76, 0x94, 0xaa,
  26749. 0x0c, 0x6d, 0x10, 0xe4, 0xdb, 0x6b, 0x1a, 0xdd,
  26750. 0x2f, 0xd8, 0x1a, 0x25, 0xcc, 0xb1, 0x48, 0x03,
  26751. 0x2d, 0xcd, 0x73, 0x99, 0x36, 0x73, 0x7f, 0x2d,
  26752. 0x86, 0x26, 0xED, 0x79, 0xD4, 0x51, 0x14, 0x08,
  26753. 0x00, 0xE0, 0x3B, 0x59, 0xB9, 0x56, 0xF8, 0x21,
  26754. 0x0E, 0x55, 0x60, 0x67, 0x40, 0x7D, 0x13, 0xDC,
  26755. 0x90, 0xFA, 0x9E, 0x8B, 0x87, 0x2B, 0xFB, 0x8F
  26756. };
  26757. const byte kyber512enc_rand[] = {
  26758. 0x14, 0x7c, 0x03, 0xf7, 0xa5, 0xbe, 0xbb, 0xa4,
  26759. 0x06, 0xc8, 0xfa, 0xe1, 0x87, 0x4d, 0x7f, 0x13,
  26760. 0xc8, 0x0e, 0xfe, 0x79, 0xa3, 0xa9, 0xa8, 0x74,
  26761. 0xcc, 0x09, 0xfe, 0x76, 0xf6, 0x99, 0x76, 0x15
  26762. };
  26763. #ifndef WOLFSSL_KYBER_90S
  26764. const byte kyber512_pk[] = {
  26765. 0x11, 0x5A, 0xCE, 0x0E, 0x64, 0x67, 0x7C, 0xBB,
  26766. 0x7D, 0xCF, 0xC9, 0x3C, 0x16, 0xD3, 0xA3, 0x05,
  26767. 0xF6, 0x76, 0x15, 0xA4, 0x88, 0xD7, 0x11, 0xAA,
  26768. 0x56, 0x69, 0x8C, 0x56, 0x63, 0xAB, 0x7A, 0xC9,
  26769. 0xCE, 0x66, 0xD5, 0x47, 0xC0, 0x59, 0x5F, 0x98,
  26770. 0xA4, 0x3F, 0x46, 0x50, 0xBB, 0xE0, 0x8C, 0x36,
  26771. 0x4D, 0x97, 0x67, 0x89, 0x11, 0x7D, 0x34, 0xF6,
  26772. 0xAE, 0x51, 0xAC, 0x06, 0x3C, 0xB5, 0x5C, 0x6C,
  26773. 0xA3, 0x25, 0x58, 0x22, 0x7D, 0xFE, 0xF8, 0x07,
  26774. 0xD1, 0x9C, 0x30, 0xDE, 0x41, 0x44, 0x24, 0x09,
  26775. 0x7F, 0x6A, 0xA2, 0x36, 0xA1, 0x05, 0x3B, 0x4A,
  26776. 0x07, 0xA7, 0x6B, 0xE3, 0x72, 0xA5, 0xC6, 0xB6,
  26777. 0x00, 0x27, 0x91, 0xEB, 0xE0, 0xAF, 0xDA, 0xF5,
  26778. 0x4E, 0x1C, 0xA2, 0x37, 0xFF, 0x54, 0x5B, 0xA6,
  26779. 0x83, 0x43, 0xE7, 0x45, 0xC0, 0x4A, 0xD1, 0x63,
  26780. 0x9D, 0xBC, 0x59, 0x03, 0x46, 0xB6, 0xB9, 0x56,
  26781. 0x9B, 0x56, 0xDB, 0xBF, 0xE5, 0x31, 0x51, 0x91,
  26782. 0x30, 0x66, 0xE5, 0xC8, 0x55, 0x27, 0xDC, 0x94,
  26783. 0x68, 0x11, 0x0A, 0x13, 0x6A, 0x41, 0x14, 0x97,
  26784. 0xC2, 0x27, 0xDC, 0xB8, 0xC9, 0xB2, 0x55, 0x70,
  26785. 0xB7, 0xA0, 0xE4, 0x2A, 0xAD, 0xA6, 0x70, 0x9F,
  26786. 0x23, 0x20, 0x8F, 0x5D, 0x49, 0x6E, 0xBA, 0xB7,
  26787. 0x84, 0x3F, 0x64, 0x83, 0xBF, 0x0C, 0x0C, 0x73,
  26788. 0xA4, 0x02, 0x96, 0xEC, 0x2C, 0x64, 0x40, 0x00,
  26789. 0x13, 0x94, 0xC9, 0x9C, 0xA1, 0x73, 0xD5, 0xC7,
  26790. 0x75, 0xB7, 0xF4, 0x15, 0xD0, 0x2A, 0x5A, 0x26,
  26791. 0xA0, 0x74, 0x07, 0x91, 0x85, 0x87, 0xC4, 0x11,
  26792. 0x69, 0xF2, 0xB7, 0x17, 0x87, 0x55, 0xAC, 0xC2,
  26793. 0x7F, 0xC8, 0xB1, 0x9C, 0x4C, 0x4B, 0x3F, 0xCD,
  26794. 0x41, 0x05, 0x3F, 0x2C, 0x74, 0xC8, 0xA1, 0x0A,
  26795. 0x83, 0x21, 0x24, 0x1B, 0x28, 0x02, 0x43, 0x28,
  26796. 0x75, 0xAE, 0x80, 0x8B, 0x9E, 0xF1, 0x36, 0x5C,
  26797. 0x7B, 0x8A, 0x52, 0x90, 0x2F, 0x13, 0x17, 0xBA,
  26798. 0x2F, 0xB0, 0x26, 0x9F, 0x47, 0x93, 0x06, 0x72,
  26799. 0x10, 0x7B, 0x47, 0x26, 0xFE, 0xF6, 0x45, 0x47,
  26800. 0x39, 0x4D, 0x33, 0x20, 0xC8, 0xF1, 0x20, 0xB3,
  26801. 0xC2, 0xF4, 0x72, 0x5B, 0x03, 0x05, 0xFA, 0xB8,
  26802. 0x8C, 0xC7, 0x98, 0x1F, 0xCB, 0x09, 0xA7, 0x6A,
  26803. 0x1C, 0xBF, 0x7F, 0x17, 0x9F, 0x43, 0xBB, 0x0A,
  26804. 0x4C, 0x8B, 0x05, 0x90, 0x85, 0x7F, 0x1E, 0x69,
  26805. 0x70, 0x84, 0x66, 0xC7, 0xF8, 0x60, 0x73, 0x91,
  26806. 0xE7, 0xBC, 0x52, 0x68, 0xBF, 0xD3, 0xD7, 0xA1,
  26807. 0xDF, 0xFC, 0xB4, 0xEC, 0xA2, 0xA1, 0xC9, 0xB5,
  26808. 0x97, 0x59, 0x30, 0x13, 0xD5, 0xFC, 0x42, 0x02,
  26809. 0xEC, 0x2B, 0x74, 0xE5, 0x7A, 0xB7, 0x6B, 0xBC,
  26810. 0xF3, 0x63, 0x2B, 0xBA, 0xF9, 0x7C, 0xDC, 0x41,
  26811. 0x8A, 0x6F, 0x16, 0x39, 0x28, 0x38, 0xCA, 0x9B,
  26812. 0xF4, 0x5D, 0xDF, 0x02, 0x37, 0x77, 0xB7, 0x56,
  26813. 0x18, 0x33, 0xC1, 0x05, 0x19, 0x0F, 0x94, 0xF3,
  26814. 0x02, 0xC5, 0x9B, 0x53, 0x19, 0x00, 0xBB, 0xC8,
  26815. 0x16, 0x36, 0x1F, 0xAA, 0x5B, 0x33, 0x80, 0xCA,
  26816. 0x3A, 0x89, 0x31, 0x04, 0xCA, 0x73, 0x88, 0xB1,
  26817. 0x85, 0x67, 0x1B, 0x3E, 0x5F, 0xE3, 0x79, 0x0E,
  26818. 0x9A, 0x62, 0x6E, 0xC4, 0x6D, 0x9B, 0x0B, 0x33,
  26819. 0xC7, 0xA4, 0x19, 0xAF, 0x7B, 0x32, 0xB6, 0x85,
  26820. 0x98, 0x94, 0xF5, 0x75, 0xD8, 0x2A, 0xC5, 0x45,
  26821. 0x6B, 0x54, 0x90, 0xA7, 0xAF, 0x8F, 0xE6, 0x10,
  26822. 0x46, 0x36, 0x05, 0x89, 0xEC, 0xBA, 0x72, 0x44,
  26823. 0x23, 0x6F, 0x41, 0x23, 0x11, 0x6B, 0x61, 0x74,
  26824. 0xAA, 0x17, 0x92, 0x49, 0xA4, 0x91, 0x95, 0xB3,
  26825. 0x56, 0xC7, 0x2F, 0xC6, 0x64, 0x1F, 0x02, 0x51,
  26826. 0x81, 0x2E, 0xAA, 0x98, 0x57, 0x0B, 0x04, 0x66,
  26827. 0x99, 0x07, 0x0E, 0x08, 0x19, 0xDC, 0x27, 0x13,
  26828. 0xF4, 0x69, 0x13, 0x7D, 0xFC, 0x6A, 0x3D, 0x7B,
  26829. 0x92, 0xB2, 0x98, 0x99, 0x5E, 0xE7, 0x80, 0x36,
  26830. 0x91, 0x53, 0xAC, 0x36, 0x6B, 0x06, 0xD7, 0x24,
  26831. 0x9C, 0xD0, 0x9E, 0x1B, 0x33, 0x78, 0xFB, 0x04,
  26832. 0x39, 0x9C, 0xEC, 0xB8, 0x65, 0x05, 0x81, 0xD6,
  26833. 0x37, 0xC7, 0x9A, 0xE6, 0x7D, 0x6F, 0x2C, 0xAF,
  26834. 0x6A, 0xBA, 0xCF, 0x59, 0x81, 0x59, 0xA7, 0x79,
  26835. 0x2C, 0xB3, 0xC9, 0x71, 0xD1, 0x49, 0x9D, 0x23,
  26836. 0x73, 0xAD, 0x20, 0xF6, 0x3F, 0x03, 0xBB, 0x59,
  26837. 0xED, 0x13, 0x73, 0x84, 0xAC, 0x61, 0xA7, 0x15,
  26838. 0x51, 0x43, 0xB8, 0xCA, 0x49, 0x32, 0x61, 0x2E,
  26839. 0xC9, 0x15, 0xE4, 0xCA, 0x34, 0x6A, 0x9B, 0xCE,
  26840. 0x5D, 0xD6, 0x04, 0x17, 0xC6, 0xB2, 0xA8, 0x9B,
  26841. 0x1C, 0xC4, 0x35, 0x64, 0x3F, 0x87, 0x5B, 0xDC,
  26842. 0x5A, 0x7E, 0x5B, 0x34, 0x81, 0xCF, 0x91, 0x9E,
  26843. 0xA0, 0x91, 0x72, 0xFE, 0xBC, 0x46, 0xD4, 0xFC,
  26844. 0x3F, 0xB0, 0xCB, 0x95, 0x91, 0x70, 0x4E, 0xE2,
  26845. 0xDB, 0xB6, 0x18, 0x44, 0xB2, 0xF3, 0x31, 0x4A,
  26846. 0x06, 0xBB, 0x6C, 0x6D, 0x34, 0x00, 0x5E, 0x48,
  26847. 0x5C, 0xE6, 0x67, 0xBD, 0xC7, 0xD0, 0x98, 0x58,
  26848. 0x69, 0x28, 0xD2, 0xD9, 0x13, 0x40, 0xF0, 0x04,
  26849. 0x19, 0xEA, 0x40, 0x13, 0x51, 0xA2, 0x40, 0xA0,
  26850. 0xB0, 0x41, 0x05, 0x8B, 0xEF, 0xB0, 0xC2, 0xFD,
  26851. 0x32, 0x64, 0x5B, 0x7A, 0x2D, 0xF8, 0xF5, 0xCB,
  26852. 0xFD, 0x87, 0x33, 0x27, 0xC9, 0x78, 0xD7, 0xB3,
  26853. 0x51, 0xA2, 0x80, 0x88, 0x43, 0x88, 0x37, 0x02,
  26854. 0x4C, 0x52, 0xB9, 0xC2, 0x95, 0xCD, 0x71, 0x36,
  26855. 0x46, 0xFB, 0x5D, 0x6C, 0x0C, 0xCF, 0xB4, 0x70,
  26856. 0x73, 0x4A, 0xC2, 0xB2, 0xBC, 0x81, 0x23, 0xC2,
  26857. 0xC1, 0x3D, 0xF6, 0x93, 0x8E, 0x92, 0x45, 0x5A,
  26858. 0x86, 0x26, 0x39, 0xFE, 0xB8, 0xA6, 0x4B, 0x85,
  26859. 0x16, 0x3E, 0x32, 0x70, 0x7E, 0x03, 0x7B, 0x38,
  26860. 0xD8, 0xAC, 0x39, 0x22, 0xB4, 0x51, 0x87, 0xBB,
  26861. 0x65, 0xEA, 0xFD, 0x46, 0x5F, 0xC6, 0x4A, 0x0C,
  26862. 0x5F, 0x8F, 0x3F, 0x90, 0x03, 0x48, 0x94, 0x15,
  26863. 0x89, 0x9D, 0x59, 0xA5, 0x43, 0xD8, 0x20, 0x8C,
  26864. 0x54, 0xA3, 0x16, 0x65, 0x29, 0xB5, 0x39, 0x22
  26865. };
  26866. const byte kyber512_sk[] = {
  26867. 0x6C, 0x89, 0x2B, 0x02, 0x97, 0xA9, 0xC7, 0x64,
  26868. 0x14, 0x93, 0xF8, 0x7D, 0xAF, 0x35, 0x33, 0xEE,
  26869. 0xD6, 0x1F, 0x07, 0xF4, 0x65, 0x20, 0x66, 0x33,
  26870. 0x7E, 0xD7, 0x40, 0x46, 0xDC, 0xC7, 0x1B, 0xA0,
  26871. 0x3F, 0x30, 0x96, 0x01, 0x03, 0x16, 0x1F, 0x7D,
  26872. 0xEB, 0x53, 0xA7, 0x1B, 0x11, 0x61, 0x72, 0x63,
  26873. 0xFE, 0x2A, 0x80, 0x97, 0x69, 0xCE, 0x6D, 0x70,
  26874. 0xA8, 0x5F, 0xE6, 0x00, 0xEC, 0xE2, 0x9D, 0x7F,
  26875. 0x36, 0xA1, 0x6D, 0x33, 0x1B, 0x8B, 0x2A, 0x9E,
  26876. 0x1D, 0xB8, 0xC0, 0x90, 0x74, 0x2D, 0xF0, 0x73,
  26877. 0x9F, 0xF0, 0x60, 0xCE, 0xB4, 0xEC, 0xC5, 0xAB,
  26878. 0x1C, 0x5E, 0x55, 0xAC, 0x97, 0xBB, 0x66, 0xA7,
  26879. 0xF8, 0x95, 0x10, 0x5D, 0x57, 0x78, 0x2B, 0x22,
  26880. 0x95, 0x38, 0xE3, 0x42, 0x15, 0x44, 0xA3, 0x42,
  26881. 0x14, 0x08, 0xDB, 0xF4, 0x49, 0x10, 0x93, 0x4C,
  26882. 0xC4, 0x23, 0x77, 0x4F, 0x16, 0x76, 0xFF, 0x1C,
  26883. 0x30, 0x6F, 0x97, 0x55, 0x5F, 0x57, 0xB4, 0xAE,
  26884. 0xD7, 0xA6, 0xBA, 0xB9, 0x50, 0xA8, 0x16, 0x3C,
  26885. 0x8D, 0x31, 0x8D, 0xEA, 0x62, 0x75, 0x1B, 0xD6,
  26886. 0xAB, 0xC5, 0x06, 0x9C, 0x06, 0xC8, 0x8F, 0x33,
  26887. 0x00, 0x26, 0xA1, 0x98, 0x06, 0xA0, 0x3B, 0x97,
  26888. 0xA7, 0x69, 0x6B, 0x56, 0xDA, 0x21, 0x82, 0x7B,
  26889. 0xB4, 0xE8, 0xDC, 0x03, 0x11, 0x52, 0xB4, 0x1B,
  26890. 0x89, 0x2A, 0x9E, 0x99, 0xAD, 0xF6, 0xE1, 0x96,
  26891. 0x3E, 0x96, 0x57, 0x88, 0x28, 0x15, 0x4F, 0x46,
  26892. 0x70, 0x33, 0x84, 0x69, 0x20, 0xFB, 0xB4, 0xB8,
  26893. 0x05, 0x44, 0xE7, 0xE8, 0xA8, 0x1A, 0xE9, 0x63,
  26894. 0xCF, 0x36, 0x8C, 0x9B, 0xA0, 0x37, 0xA8, 0xC2,
  26895. 0xAD, 0x62, 0xE3, 0x2B, 0x6E, 0x61, 0xC9, 0x1D,
  26896. 0x75, 0xCE, 0x00, 0x5A, 0xB3, 0x0F, 0x80, 0x99,
  26897. 0xA1, 0xF2, 0x9D, 0x7B, 0x63, 0x05, 0xB4, 0xDC,
  26898. 0x06, 0xE2, 0x56, 0x80, 0xBB, 0x00, 0x99, 0x2F,
  26899. 0x71, 0x7F, 0xE6, 0xC1, 0x15, 0xA8, 0x08, 0x42,
  26900. 0x31, 0xCC, 0x79, 0xDD, 0x70, 0x0E, 0xA6, 0x91,
  26901. 0x2A, 0xC7, 0xFA, 0x0D, 0x93, 0x7B, 0xB6, 0xA7,
  26902. 0x56, 0x66, 0x22, 0x30, 0x47, 0x0C, 0x18, 0x9B,
  26903. 0x5A, 0xA1, 0x65, 0x3D, 0xEB, 0x93, 0x7D, 0x5A,
  26904. 0x9C, 0x25, 0xA2, 0x1D, 0x93, 0xB1, 0x90, 0x74,
  26905. 0xFC, 0x23, 0x9D, 0x81, 0x53, 0x53, 0x97, 0x97,
  26906. 0xC7, 0xD4, 0xAB, 0x62, 0x64, 0x9D, 0x76, 0xAA,
  26907. 0x55, 0x37, 0x36, 0xA9, 0x49, 0x02, 0x2C, 0x22,
  26908. 0xC5, 0x2B, 0xAE, 0xEC, 0x60, 0x5B, 0x32, 0xCE,
  26909. 0x9E, 0x5B, 0x93, 0x84, 0x90, 0x35, 0x58, 0xCA,
  26910. 0x9D, 0x6A, 0x3A, 0xBA, 0x90, 0x42, 0x3E, 0xED,
  26911. 0xA0, 0x1C, 0x94, 0x19, 0x8B, 0x19, 0x2A, 0x8B,
  26912. 0xA9, 0x06, 0x34, 0x97, 0xA0, 0xC5, 0x01, 0x33,
  26913. 0x07, 0xDD, 0xD8, 0x63, 0x52, 0x64, 0x71, 0xA4,
  26914. 0xD9, 0x95, 0x23, 0xEB, 0x41, 0x7F, 0x29, 0x1A,
  26915. 0xAC, 0x0C, 0x3A, 0x58, 0x1B, 0x6D, 0xA0, 0x07,
  26916. 0x32, 0xE5, 0xE8, 0x1B, 0x1F, 0x7C, 0x87, 0x9B,
  26917. 0x16, 0x93, 0xC1, 0x3B, 0x6F, 0x9F, 0x79, 0x31,
  26918. 0x62, 0x24, 0x29, 0xE5, 0x42, 0xAF, 0x40, 0x69,
  26919. 0x22, 0x2F, 0x04, 0x55, 0x44, 0xE0, 0xCC, 0x4F,
  26920. 0xB2, 0x4D, 0x44, 0x48, 0xCF, 0x2C, 0x65, 0x96,
  26921. 0xF5, 0xCB, 0x08, 0x62, 0x4B, 0x11, 0x85, 0x01,
  26922. 0x3B, 0x6B, 0x02, 0x08, 0x92, 0xF9, 0x6B, 0xDF,
  26923. 0xD4, 0xAD, 0xA9, 0x17, 0x9D, 0xE7, 0x27, 0xB8,
  26924. 0xD9, 0x42, 0x6E, 0x09, 0x96, 0xB5, 0xD3, 0x49,
  26925. 0x48, 0xCE, 0x02, 0xD0, 0xC3, 0x69, 0xB3, 0x7C,
  26926. 0xBB, 0x54, 0xD3, 0x47, 0x9E, 0xD8, 0xB5, 0x82,
  26927. 0xE9, 0xE7, 0x28, 0x92, 0x9B, 0x4C, 0x71, 0xC9,
  26928. 0xBE, 0x11, 0xD4, 0x5B, 0x20, 0xC4, 0xBD, 0xC3,
  26929. 0xC7, 0x43, 0x13, 0x22, 0x3F, 0x58, 0x27, 0x4E,
  26930. 0x8B, 0xA5, 0x24, 0x44, 0x47, 0xC4, 0x95, 0x95,
  26931. 0x0B, 0x84, 0xCB, 0x0C, 0x3C, 0x27, 0x36, 0x40,
  26932. 0x10, 0x8A, 0x33, 0x97, 0x94, 0x45, 0x73, 0x27,
  26933. 0x93, 0x28, 0x99, 0x6C, 0xDC, 0x0C, 0x91, 0x3C,
  26934. 0x95, 0x8A, 0xD6, 0x20, 0xBA, 0x8B, 0x5E, 0x5E,
  26935. 0xCB, 0xBB, 0x7E, 0x13, 0xCB, 0x9C, 0x70, 0xBD,
  26936. 0x5A, 0xB3, 0x0E, 0xB7, 0x48, 0x8C, 0x97, 0x00,
  26937. 0x1C, 0x20, 0x49, 0x8F, 0x1D, 0x7C, 0xC0, 0x6D,
  26938. 0xA7, 0x6B, 0xF5, 0x20, 0xC6, 0x58, 0xCC, 0xAD,
  26939. 0xFA, 0x29, 0x56, 0x42, 0x45, 0x57, 0xAB, 0xEA,
  26940. 0x8A, 0xB8, 0x92, 0x39, 0xC1, 0x78, 0x33, 0xDC,
  26941. 0x3A, 0x49, 0xB3, 0x6A, 0x9A, 0xE9, 0xA4, 0x86,
  26942. 0x94, 0x05, 0x40, 0xEB, 0x44, 0x4F, 0x97, 0x15,
  26943. 0x23, 0x57, 0xE0, 0x20, 0x35, 0x93, 0x9D, 0x75,
  26944. 0xA3, 0xC0, 0x25, 0xF4, 0x1A, 0x40, 0x08, 0x23,
  26945. 0x82, 0xA0, 0x73, 0x3C, 0x39, 0xB0, 0x62, 0x2B,
  26946. 0x74, 0x0E, 0x40, 0x75, 0x92, 0xC6, 0x2E, 0xCA,
  26947. 0xEB, 0x14, 0x32, 0xC4, 0x45, 0xB3, 0x70, 0x3A,
  26948. 0x86, 0xF6, 0x98, 0x1A, 0x27, 0x81, 0x57, 0xEA,
  26949. 0x95, 0xA6, 0xE9, 0x2D, 0x55, 0xE4, 0xB9, 0x72,
  26950. 0xF9, 0x36, 0xC2, 0xF0, 0xA6, 0x58, 0x28, 0x0E,
  26951. 0xA2, 0xB0, 0x7A, 0x48, 0x99, 0x2D, 0xF8, 0x93,
  26952. 0x7E, 0x0A, 0x2A, 0xC1, 0xDC, 0xC9, 0x74, 0xFE,
  26953. 0x00, 0xAA, 0xE1, 0xF5, 0x61, 0xFA, 0x25, 0x8E,
  26954. 0x2D, 0x25, 0x9C, 0x3E, 0x86, 0x1D, 0xCE, 0x23,
  26955. 0x60, 0x39, 0x12, 0x76, 0x06, 0xFC, 0x1C, 0xE0,
  26956. 0x09, 0x00, 0x3A, 0x7B, 0xAC, 0x94, 0x21, 0x01,
  26957. 0xDC, 0xB8, 0x22, 0xB1, 0xF3, 0xC1, 0x2B, 0xF7,
  26958. 0x32, 0x38, 0xF5, 0x46, 0xE0, 0x1C, 0x36, 0xB5,
  26959. 0xA6, 0x93, 0x61, 0x92, 0x99, 0x5C, 0xC6, 0x9C,
  26960. 0x63, 0x23, 0x74, 0x09, 0xCB, 0x53, 0xC2, 0xE3,
  26961. 0x5D, 0x74, 0x89, 0x0D, 0x18, 0x88, 0x53, 0x76,
  26962. 0xFA, 0x55, 0x03, 0xB1, 0x07, 0xA2, 0xA3, 0x92,
  26963. 0x11, 0x5A, 0xCE, 0x0E, 0x64, 0x67, 0x7C, 0xBB,
  26964. 0x7D, 0xCF, 0xC9, 0x3C, 0x16, 0xD3, 0xA3, 0x05,
  26965. 0xF6, 0x76, 0x15, 0xA4, 0x88, 0xD7, 0x11, 0xAA,
  26966. 0x56, 0x69, 0x8C, 0x56, 0x63, 0xAB, 0x7A, 0xC9,
  26967. 0xCE, 0x66, 0xD5, 0x47, 0xC0, 0x59, 0x5F, 0x98,
  26968. 0xA4, 0x3F, 0x46, 0x50, 0xBB, 0xE0, 0x8C, 0x36,
  26969. 0x4D, 0x97, 0x67, 0x89, 0x11, 0x7D, 0x34, 0xF6,
  26970. 0xAE, 0x51, 0xAC, 0x06, 0x3C, 0xB5, 0x5C, 0x6C,
  26971. 0xA3, 0x25, 0x58, 0x22, 0x7D, 0xFE, 0xF8, 0x07,
  26972. 0xD1, 0x9C, 0x30, 0xDE, 0x41, 0x44, 0x24, 0x09,
  26973. 0x7F, 0x6A, 0xA2, 0x36, 0xA1, 0x05, 0x3B, 0x4A,
  26974. 0x07, 0xA7, 0x6B, 0xE3, 0x72, 0xA5, 0xC6, 0xB6,
  26975. 0x00, 0x27, 0x91, 0xEB, 0xE0, 0xAF, 0xDA, 0xF5,
  26976. 0x4E, 0x1C, 0xA2, 0x37, 0xFF, 0x54, 0x5B, 0xA6,
  26977. 0x83, 0x43, 0xE7, 0x45, 0xC0, 0x4A, 0xD1, 0x63,
  26978. 0x9D, 0xBC, 0x59, 0x03, 0x46, 0xB6, 0xB9, 0x56,
  26979. 0x9B, 0x56, 0xDB, 0xBF, 0xE5, 0x31, 0x51, 0x91,
  26980. 0x30, 0x66, 0xE5, 0xC8, 0x55, 0x27, 0xDC, 0x94,
  26981. 0x68, 0x11, 0x0A, 0x13, 0x6A, 0x41, 0x14, 0x97,
  26982. 0xC2, 0x27, 0xDC, 0xB8, 0xC9, 0xB2, 0x55, 0x70,
  26983. 0xB7, 0xA0, 0xE4, 0x2A, 0xAD, 0xA6, 0x70, 0x9F,
  26984. 0x23, 0x20, 0x8F, 0x5D, 0x49, 0x6E, 0xBA, 0xB7,
  26985. 0x84, 0x3F, 0x64, 0x83, 0xBF, 0x0C, 0x0C, 0x73,
  26986. 0xA4, 0x02, 0x96, 0xEC, 0x2C, 0x64, 0x40, 0x00,
  26987. 0x13, 0x94, 0xC9, 0x9C, 0xA1, 0x73, 0xD5, 0xC7,
  26988. 0x75, 0xB7, 0xF4, 0x15, 0xD0, 0x2A, 0x5A, 0x26,
  26989. 0xA0, 0x74, 0x07, 0x91, 0x85, 0x87, 0xC4, 0x11,
  26990. 0x69, 0xF2, 0xB7, 0x17, 0x87, 0x55, 0xAC, 0xC2,
  26991. 0x7F, 0xC8, 0xB1, 0x9C, 0x4C, 0x4B, 0x3F, 0xCD,
  26992. 0x41, 0x05, 0x3F, 0x2C, 0x74, 0xC8, 0xA1, 0x0A,
  26993. 0x83, 0x21, 0x24, 0x1B, 0x28, 0x02, 0x43, 0x28,
  26994. 0x75, 0xAE, 0x80, 0x8B, 0x9E, 0xF1, 0x36, 0x5C,
  26995. 0x7B, 0x8A, 0x52, 0x90, 0x2F, 0x13, 0x17, 0xBA,
  26996. 0x2F, 0xB0, 0x26, 0x9F, 0x47, 0x93, 0x06, 0x72,
  26997. 0x10, 0x7B, 0x47, 0x26, 0xFE, 0xF6, 0x45, 0x47,
  26998. 0x39, 0x4D, 0x33, 0x20, 0xC8, 0xF1, 0x20, 0xB3,
  26999. 0xC2, 0xF4, 0x72, 0x5B, 0x03, 0x05, 0xFA, 0xB8,
  27000. 0x8C, 0xC7, 0x98, 0x1F, 0xCB, 0x09, 0xA7, 0x6A,
  27001. 0x1C, 0xBF, 0x7F, 0x17, 0x9F, 0x43, 0xBB, 0x0A,
  27002. 0x4C, 0x8B, 0x05, 0x90, 0x85, 0x7F, 0x1E, 0x69,
  27003. 0x70, 0x84, 0x66, 0xC7, 0xF8, 0x60, 0x73, 0x91,
  27004. 0xE7, 0xBC, 0x52, 0x68, 0xBF, 0xD3, 0xD7, 0xA1,
  27005. 0xDF, 0xFC, 0xB4, 0xEC, 0xA2, 0xA1, 0xC9, 0xB5,
  27006. 0x97, 0x59, 0x30, 0x13, 0xD5, 0xFC, 0x42, 0x02,
  27007. 0xEC, 0x2B, 0x74, 0xE5, 0x7A, 0xB7, 0x6B, 0xBC,
  27008. 0xF3, 0x63, 0x2B, 0xBA, 0xF9, 0x7C, 0xDC, 0x41,
  27009. 0x8A, 0x6F, 0x16, 0x39, 0x28, 0x38, 0xCA, 0x9B,
  27010. 0xF4, 0x5D, 0xDF, 0x02, 0x37, 0x77, 0xB7, 0x56,
  27011. 0x18, 0x33, 0xC1, 0x05, 0x19, 0x0F, 0x94, 0xF3,
  27012. 0x02, 0xC5, 0x9B, 0x53, 0x19, 0x00, 0xBB, 0xC8,
  27013. 0x16, 0x36, 0x1F, 0xAA, 0x5B, 0x33, 0x80, 0xCA,
  27014. 0x3A, 0x89, 0x31, 0x04, 0xCA, 0x73, 0x88, 0xB1,
  27015. 0x85, 0x67, 0x1B, 0x3E, 0x5F, 0xE3, 0x79, 0x0E,
  27016. 0x9A, 0x62, 0x6E, 0xC4, 0x6D, 0x9B, 0x0B, 0x33,
  27017. 0xC7, 0xA4, 0x19, 0xAF, 0x7B, 0x32, 0xB6, 0x85,
  27018. 0x98, 0x94, 0xF5, 0x75, 0xD8, 0x2A, 0xC5, 0x45,
  27019. 0x6B, 0x54, 0x90, 0xA7, 0xAF, 0x8F, 0xE6, 0x10,
  27020. 0x46, 0x36, 0x05, 0x89, 0xEC, 0xBA, 0x72, 0x44,
  27021. 0x23, 0x6F, 0x41, 0x23, 0x11, 0x6B, 0x61, 0x74,
  27022. 0xAA, 0x17, 0x92, 0x49, 0xA4, 0x91, 0x95, 0xB3,
  27023. 0x56, 0xC7, 0x2F, 0xC6, 0x64, 0x1F, 0x02, 0x51,
  27024. 0x81, 0x2E, 0xAA, 0x98, 0x57, 0x0B, 0x04, 0x66,
  27025. 0x99, 0x07, 0x0E, 0x08, 0x19, 0xDC, 0x27, 0x13,
  27026. 0xF4, 0x69, 0x13, 0x7D, 0xFC, 0x6A, 0x3D, 0x7B,
  27027. 0x92, 0xB2, 0x98, 0x99, 0x5E, 0xE7, 0x80, 0x36,
  27028. 0x91, 0x53, 0xAC, 0x36, 0x6B, 0x06, 0xD7, 0x24,
  27029. 0x9C, 0xD0, 0x9E, 0x1B, 0x33, 0x78, 0xFB, 0x04,
  27030. 0x39, 0x9C, 0xEC, 0xB8, 0x65, 0x05, 0x81, 0xD6,
  27031. 0x37, 0xC7, 0x9A, 0xE6, 0x7D, 0x6F, 0x2C, 0xAF,
  27032. 0x6A, 0xBA, 0xCF, 0x59, 0x81, 0x59, 0xA7, 0x79,
  27033. 0x2C, 0xB3, 0xC9, 0x71, 0xD1, 0x49, 0x9D, 0x23,
  27034. 0x73, 0xAD, 0x20, 0xF6, 0x3F, 0x03, 0xBB, 0x59,
  27035. 0xED, 0x13, 0x73, 0x84, 0xAC, 0x61, 0xA7, 0x15,
  27036. 0x51, 0x43, 0xB8, 0xCA, 0x49, 0x32, 0x61, 0x2E,
  27037. 0xC9, 0x15, 0xE4, 0xCA, 0x34, 0x6A, 0x9B, 0xCE,
  27038. 0x5D, 0xD6, 0x04, 0x17, 0xC6, 0xB2, 0xA8, 0x9B,
  27039. 0x1C, 0xC4, 0x35, 0x64, 0x3F, 0x87, 0x5B, 0xDC,
  27040. 0x5A, 0x7E, 0x5B, 0x34, 0x81, 0xCF, 0x91, 0x9E,
  27041. 0xA0, 0x91, 0x72, 0xFE, 0xBC, 0x46, 0xD4, 0xFC,
  27042. 0x3F, 0xB0, 0xCB, 0x95, 0x91, 0x70, 0x4E, 0xE2,
  27043. 0xDB, 0xB6, 0x18, 0x44, 0xB2, 0xF3, 0x31, 0x4A,
  27044. 0x06, 0xBB, 0x6C, 0x6D, 0x34, 0x00, 0x5E, 0x48,
  27045. 0x5C, 0xE6, 0x67, 0xBD, 0xC7, 0xD0, 0x98, 0x58,
  27046. 0x69, 0x28, 0xD2, 0xD9, 0x13, 0x40, 0xF0, 0x04,
  27047. 0x19, 0xEA, 0x40, 0x13, 0x51, 0xA2, 0x40, 0xA0,
  27048. 0xB0, 0x41, 0x05, 0x8B, 0xEF, 0xB0, 0xC2, 0xFD,
  27049. 0x32, 0x64, 0x5B, 0x7A, 0x2D, 0xF8, 0xF5, 0xCB,
  27050. 0xFD, 0x87, 0x33, 0x27, 0xC9, 0x78, 0xD7, 0xB3,
  27051. 0x51, 0xA2, 0x80, 0x88, 0x43, 0x88, 0x37, 0x02,
  27052. 0x4C, 0x52, 0xB9, 0xC2, 0x95, 0xCD, 0x71, 0x36,
  27053. 0x46, 0xFB, 0x5D, 0x6C, 0x0C, 0xCF, 0xB4, 0x70,
  27054. 0x73, 0x4A, 0xC2, 0xB2, 0xBC, 0x81, 0x23, 0xC2,
  27055. 0xC1, 0x3D, 0xF6, 0x93, 0x8E, 0x92, 0x45, 0x5A,
  27056. 0x86, 0x26, 0x39, 0xFE, 0xB8, 0xA6, 0x4B, 0x85,
  27057. 0x16, 0x3E, 0x32, 0x70, 0x7E, 0x03, 0x7B, 0x38,
  27058. 0xD8, 0xAC, 0x39, 0x22, 0xB4, 0x51, 0x87, 0xBB,
  27059. 0x65, 0xEA, 0xFD, 0x46, 0x5F, 0xC6, 0x4A, 0x0C,
  27060. 0x5F, 0x8F, 0x3F, 0x90, 0x03, 0x48, 0x94, 0x15,
  27061. 0x89, 0x9D, 0x59, 0xA5, 0x43, 0xD8, 0x20, 0x8C,
  27062. 0x54, 0xA3, 0x16, 0x65, 0x29, 0xB5, 0x39, 0x22,
  27063. 0x7F, 0xFA, 0xD1, 0xBC, 0x8A, 0xF7, 0x3B, 0x7E,
  27064. 0x87, 0x49, 0x56, 0xB8, 0x1C, 0x2A, 0x2E, 0xF0,
  27065. 0xBF, 0xAB, 0xE8, 0xDC, 0x93, 0xD7, 0x7B, 0x2F,
  27066. 0xBC, 0x9E, 0x0C, 0x64, 0xEF, 0xA0, 0x1E, 0x84,
  27067. 0x86, 0x26, 0xED, 0x79, 0xD4, 0x51, 0x14, 0x08,
  27068. 0x00, 0xE0, 0x3B, 0x59, 0xB9, 0x56, 0xF8, 0x21,
  27069. 0x0E, 0x55, 0x60, 0x67, 0x40, 0x7D, 0x13, 0xDC,
  27070. 0x90, 0xFA, 0x9E, 0x8B, 0x87, 0x2B, 0xFB, 0x8F
  27071. };
  27072. const byte kyber512_ct[] = {
  27073. 0xED, 0xF2, 0x41, 0x45, 0xE4, 0x3B, 0x4F, 0x6D,
  27074. 0xC6, 0xBF, 0x83, 0x32, 0xF5, 0x4E, 0x02, 0xCA,
  27075. 0xB0, 0x2D, 0xBF, 0x3B, 0x56, 0x05, 0xDD, 0xC9,
  27076. 0x0A, 0x15, 0xC8, 0x86, 0xAD, 0x3E, 0xD4, 0x89,
  27077. 0x46, 0x26, 0x99, 0xE4, 0xAB, 0xED, 0x44, 0x35,
  27078. 0x0B, 0xC3, 0x75, 0x7E, 0x26, 0x96, 0xFB, 0xFB,
  27079. 0x25, 0x34, 0x41, 0x2E, 0x8D, 0xD2, 0x01, 0xF1,
  27080. 0xE4, 0x54, 0x0A, 0x39, 0x70, 0xB0, 0x55, 0xFE,
  27081. 0x3B, 0x0B, 0xEC, 0x3A, 0x71, 0xF9, 0xE1, 0x15,
  27082. 0xB3, 0xF9, 0xF3, 0x91, 0x02, 0x06, 0x5B, 0x1C,
  27083. 0xCA, 0x83, 0x14, 0xDC, 0xC7, 0x95, 0xE3, 0xC0,
  27084. 0xE8, 0xFA, 0x98, 0xEE, 0x83, 0xCA, 0x66, 0x28,
  27085. 0x45, 0x70, 0x28, 0xA4, 0xD0, 0x9E, 0x83, 0x9E,
  27086. 0x55, 0x48, 0x62, 0xCF, 0x0B, 0x7B, 0xF5, 0x6C,
  27087. 0x5C, 0x0A, 0x82, 0x9E, 0x86, 0x57, 0x94, 0x79,
  27088. 0x45, 0xFE, 0x9C, 0x22, 0x56, 0x4F, 0xBA, 0xEB,
  27089. 0xC1, 0xB3, 0xAF, 0x35, 0x0D, 0x79, 0x55, 0x50,
  27090. 0x8A, 0x26, 0xD8, 0xA8, 0xEB, 0x54, 0x7B, 0x8B,
  27091. 0x1A, 0x2C, 0xF0, 0x3C, 0xCA, 0x1A, 0xAB, 0xCE,
  27092. 0x6C, 0x34, 0x97, 0x78, 0x3B, 0x64, 0x65, 0xBA,
  27093. 0x0B, 0x6E, 0x7A, 0xCB, 0xA8, 0x21, 0x19, 0x51,
  27094. 0x24, 0xAE, 0xF0, 0x9E, 0x62, 0x83, 0x82, 0xA1,
  27095. 0xF9, 0x14, 0x04, 0x3B, 0xE7, 0x09, 0x6E, 0x95,
  27096. 0x2C, 0xBC, 0x4F, 0xB4, 0xAF, 0xED, 0x13, 0x60,
  27097. 0x90, 0x46, 0x11, 0x7C, 0x01, 0x1F, 0xD7, 0x41,
  27098. 0xEE, 0x28, 0x6C, 0x83, 0x77, 0x16, 0x90, 0xF0,
  27099. 0xAE, 0xB5, 0x0D, 0xA0, 0xD7, 0x12, 0x85, 0xA1,
  27100. 0x79, 0xB2, 0x15, 0xC6, 0x03, 0x6D, 0xEB, 0x78,
  27101. 0x0F, 0x4D, 0x16, 0x76, 0x9F, 0x72, 0xDE, 0x16,
  27102. 0xFD, 0xAD, 0xAC, 0x73, 0xBE, 0xFA, 0x5B, 0xEF,
  27103. 0x89, 0x43, 0x19, 0x7F, 0x44, 0xC5, 0x95, 0x89,
  27104. 0xDC, 0x9F, 0x49, 0x73, 0xDE, 0x14, 0x50, 0xBA,
  27105. 0x1D, 0x0C, 0x32, 0x90, 0xD6, 0xB1, 0xD6, 0x83,
  27106. 0xF2, 0x94, 0xE7, 0x59, 0xC9, 0x54, 0xAB, 0xE8,
  27107. 0xA7, 0xDA, 0x5B, 0x10, 0x54, 0xFD, 0x6D, 0x21,
  27108. 0x32, 0x9B, 0x8E, 0x73, 0xD3, 0x75, 0x6A, 0xFD,
  27109. 0xA0, 0xDC, 0xB1, 0xFC, 0x8B, 0x15, 0x82, 0xD1,
  27110. 0xF9, 0x0C, 0xF2, 0x75, 0xA1, 0x02, 0xAB, 0xC6,
  27111. 0xAC, 0x69, 0x9D, 0xF0, 0xC5, 0x87, 0x0E, 0x50,
  27112. 0xA1, 0xF9, 0x89, 0xE4, 0xE6, 0x24, 0x1B, 0x60,
  27113. 0xAA, 0xA2, 0xEC, 0xF9, 0xE8, 0xE3, 0x3E, 0x0F,
  27114. 0xFC, 0xF4, 0x0F, 0xE8, 0x31, 0xE8, 0xFD, 0xC2,
  27115. 0xE8, 0x3B, 0x52, 0xCA, 0x7A, 0xB6, 0xD9, 0x3F,
  27116. 0x14, 0x6D, 0x29, 0xDC, 0xA5, 0x3C, 0x7D, 0xA1,
  27117. 0xDB, 0x4A, 0xC4, 0xF2, 0xDB, 0x39, 0xEA, 0x12,
  27118. 0x0D, 0x90, 0xFA, 0x60, 0xF4, 0xD4, 0x37, 0xC6,
  27119. 0xD0, 0x0E, 0xF4, 0x83, 0xBC, 0x94, 0xA3, 0x17,
  27120. 0x5C, 0xDA, 0x16, 0x3F, 0xC1, 0xC2, 0x82, 0x8B,
  27121. 0xE4, 0xDB, 0xD6, 0x43, 0x05, 0x07, 0xB5, 0x84,
  27122. 0xBB, 0x51, 0x77, 0xE1, 0x71, 0xB8, 0xDD, 0xA9,
  27123. 0xA4, 0x29, 0x3C, 0x32, 0x00, 0x29, 0x5C, 0x80,
  27124. 0x3A, 0x86, 0x5D, 0x6D, 0x21, 0x66, 0xF6, 0x6B,
  27125. 0xA5, 0x40, 0x1F, 0xB7, 0xA0, 0xE8, 0x53, 0x16,
  27126. 0x86, 0x00, 0xA2, 0x94, 0x84, 0x37, 0xE0, 0x36,
  27127. 0xE3, 0xBF, 0x19, 0xE1, 0x2F, 0xD3, 0xF2, 0xA2,
  27128. 0xB8, 0xB3, 0x43, 0xF7, 0x84, 0x24, 0x8E, 0x8D,
  27129. 0x68, 0x5E, 0xB0, 0xAF, 0xDE, 0x63, 0x15, 0x33,
  27130. 0x87, 0x30, 0xE7, 0xA1, 0x00, 0x1C, 0x27, 0xD8,
  27131. 0xD2, 0xA7, 0x6F, 0xA6, 0x9D, 0x15, 0x7B, 0xA1,
  27132. 0xAC, 0x7A, 0xD5, 0x6D, 0xA5, 0xA8, 0xC7, 0x0F,
  27133. 0xE4, 0xB5, 0xB8, 0xD7, 0x86, 0xDC, 0x6F, 0xC0,
  27134. 0x56, 0x6B, 0xA8, 0xE1, 0xB8, 0x81, 0x63, 0x34,
  27135. 0xD3, 0x2A, 0x3F, 0xB1, 0xCE, 0x7D, 0x4D, 0x5E,
  27136. 0x4C, 0x33, 0x2A, 0xF7, 0xB0, 0x03, 0xD0, 0x91,
  27137. 0x74, 0x1A, 0x3D, 0x5C, 0x96, 0x52, 0x92, 0x25,
  27138. 0x5D, 0xFF, 0x8E, 0xD2, 0xBB, 0xF1, 0xF9, 0x11,
  27139. 0x6B, 0xE5, 0x0C, 0x17, 0xB8, 0xE5, 0x48, 0x74,
  27140. 0x8A, 0xD4, 0xB2, 0xE9, 0x57, 0xBB, 0xD1, 0x95,
  27141. 0x34, 0x82, 0xA2, 0xE1, 0x71, 0x8C, 0xEC, 0x66,
  27142. 0xCD, 0x2C, 0x81, 0xF5, 0x72, 0xD5, 0x52, 0xB7,
  27143. 0x18, 0x78, 0x85, 0xE6, 0xB8, 0x94, 0x3D, 0x64,
  27144. 0x31, 0x41, 0x3C, 0x59, 0xEB, 0xB7, 0xE0, 0x36,
  27145. 0x04, 0x84, 0x90, 0xBE, 0x52, 0x89, 0xE9, 0x5B,
  27146. 0x20, 0xA8, 0x9E, 0x8B, 0x15, 0x9F, 0x61, 0xA9,
  27147. 0xA9, 0x88, 0x6E, 0x14, 0x75, 0x68, 0xF4, 0xC9,
  27148. 0x02, 0x1F, 0x36, 0x2F, 0x02, 0x68, 0x8A, 0x1C,
  27149. 0x8C, 0x3B, 0xB0, 0xD2, 0x40, 0x86, 0x88, 0x0E,
  27150. 0x55, 0xB6, 0xED, 0xB4, 0x3F, 0x37, 0x45, 0xD2,
  27151. 0xC1, 0x66, 0xDC, 0x1C, 0xB7, 0x43, 0xC7, 0x6F,
  27152. 0xE6, 0xBE, 0x52, 0x3A, 0x89, 0x3C, 0xC7, 0x64,
  27153. 0xD1, 0x64, 0x35, 0xC3, 0x78, 0x51, 0x25, 0x2A,
  27154. 0x81, 0xE2, 0xFF, 0xBA, 0x0F, 0x18, 0x97, 0x1A,
  27155. 0x3D, 0xEE, 0x37, 0xD4, 0x87, 0x7C, 0xB9, 0x28,
  27156. 0xE3, 0x6E, 0x52, 0x35, 0x03, 0x7A, 0x6B, 0x20,
  27157. 0x57, 0x89, 0x7D, 0x51, 0x8A, 0x5F, 0x0E, 0x34,
  27158. 0x8E, 0x3A, 0xB6, 0xD5, 0xB5, 0x2D, 0xFC, 0x60,
  27159. 0x75, 0x7F, 0x3B, 0x41, 0xA4, 0xFE, 0xC7, 0x82,
  27160. 0x8F, 0x1D, 0xEE, 0xAF, 0x45, 0x87, 0xCC, 0xC8,
  27161. 0xEA, 0xDF, 0x64, 0x7F, 0x4D, 0x20, 0x3B, 0x2F,
  27162. 0xAA, 0x05, 0xA6, 0x49, 0xB5, 0x82, 0x34, 0x0C,
  27163. 0xB4, 0xCA, 0xCE, 0x57, 0xA3, 0x07, 0x11, 0xBE,
  27164. 0x75, 0x2F, 0xAC, 0xF0, 0x22, 0x7D, 0x0A, 0x80,
  27165. 0xC4, 0x12, 0x84, 0x42, 0xDD, 0xC5, 0x44, 0xBE,
  27166. 0x80, 0x5B, 0x9C, 0xFE, 0x8F, 0xE9, 0xB1, 0x23,
  27167. 0x7C, 0x80, 0xF9, 0x67, 0x87, 0xCD, 0x92, 0x81,
  27168. 0xCC, 0xF2, 0x70, 0xC1, 0xAF, 0xC0, 0x67, 0x0D
  27169. };
  27170. const byte kyber512_ss[] = {
  27171. 0x0A, 0x69, 0x25, 0x67, 0x6F, 0x24, 0xB2, 0x2C,
  27172. 0x28, 0x6F, 0x4C, 0x81, 0xA4, 0x22, 0x4C, 0xEC,
  27173. 0x50, 0x6C, 0x9B, 0x25, 0x7D, 0x48, 0x0E, 0x02,
  27174. 0xE3, 0xB4, 0x9F, 0x44, 0xCA, 0xA3, 0x23, 0x7F
  27175. };
  27176. #else
  27177. const byte kyber512_pk[] = {
  27178. 0x68, 0xD6, 0x83, 0xF6, 0x01, 0x8F, 0xCB, 0x38,
  27179. 0x71, 0xC0, 0x64, 0x0A, 0xD1, 0x7B, 0x7E, 0x80,
  27180. 0x02, 0x93, 0xC9, 0xF6, 0x36, 0xC1, 0xE2, 0x09,
  27181. 0x10, 0x34, 0x25, 0xAD, 0x72, 0x26, 0xDD, 0x13,
  27182. 0x90, 0x41, 0xFA, 0x84, 0x9F, 0x8C, 0x2F, 0xED,
  27183. 0x18, 0xAB, 0xEF, 0xF5, 0x2A, 0xE0, 0xB6, 0x06,
  27184. 0x86, 0x13, 0xBA, 0x05, 0x73, 0x09, 0x3D, 0xE8,
  27185. 0xAC, 0xC8, 0x56, 0x19, 0x80, 0x62, 0xCD, 0x38,
  27186. 0xDC, 0x0F, 0x80, 0x05, 0x6B, 0x8D, 0x1C, 0x42,
  27187. 0xDF, 0xA3, 0x2C, 0x4C, 0xA4, 0x0E, 0x8C, 0x57,
  27188. 0x03, 0xF3, 0x13, 0xBA, 0x5F, 0xD0, 0x5C, 0x1E,
  27189. 0x75, 0xC3, 0x78, 0xD7, 0x04, 0x77, 0x62, 0x1A,
  27190. 0xF3, 0xA0, 0x8E, 0x52, 0x0A, 0x16, 0x05, 0xC4,
  27191. 0x89, 0x42, 0x2C, 0x97, 0xD0, 0xD9, 0x2E, 0x43,
  27192. 0x12, 0xBB, 0xEF, 0xB6, 0xC0, 0x18, 0x7A, 0x45,
  27193. 0xF9, 0x24, 0x6B, 0xF8, 0xEB, 0xA6, 0x2C, 0x04,
  27194. 0x7F, 0x8C, 0xB2, 0x42, 0x17, 0xB2, 0x01, 0x78,
  27195. 0xBB, 0x09, 0x5E, 0x59, 0x63, 0x6B, 0xCB, 0xA9,
  27196. 0x13, 0x73, 0x52, 0x65, 0xC7, 0xA0, 0x90, 0x06,
  27197. 0x12, 0xEF, 0x16, 0x41, 0xC9, 0xE1, 0x4D, 0x7D,
  27198. 0x89, 0x15, 0xE3, 0xC8, 0x83, 0xB7, 0x50, 0x68,
  27199. 0xA9, 0xB1, 0x3B, 0x0C, 0xF0, 0xA1, 0x32, 0x8A,
  27200. 0x3E, 0xC5, 0xD6, 0x00, 0x1E, 0xE9, 0x6D, 0x01,
  27201. 0x05, 0x6C, 0x25, 0x09, 0xC2, 0x16, 0x51, 0x4D,
  27202. 0xA5, 0x24, 0x61, 0xF7, 0xCA, 0x6A, 0xC4, 0x1A,
  27203. 0x2C, 0x0E, 0x35, 0x2B, 0x53, 0x63, 0x68, 0x99,
  27204. 0xD0, 0x65, 0xBD, 0xF2, 0xBF, 0x14, 0xF5, 0x35,
  27205. 0xFC, 0xE9, 0x42, 0x93, 0x9B, 0x6B, 0x4B, 0x97,
  27206. 0x72, 0x04, 0xD6, 0x29, 0xA6, 0x24, 0x1F, 0xFD,
  27207. 0x99, 0x94, 0x6A, 0x70, 0xBC, 0xA8, 0x91, 0x2B,
  27208. 0x1D, 0xFA, 0x61, 0x4A, 0xE9, 0x91, 0x3C, 0xB3,
  27209. 0x65, 0x63, 0x04, 0x7F, 0x9F, 0xA0, 0xA6, 0x48,
  27210. 0x7B, 0xA4, 0x10, 0x19, 0x5F, 0x56, 0x17, 0x58,
  27211. 0x84, 0x41, 0xC6, 0xA1, 0xA1, 0x50, 0x78, 0xBA,
  27212. 0x60, 0x93, 0x36, 0x79, 0x64, 0xD2, 0x5F, 0x19,
  27213. 0xD7, 0x15, 0x1A, 0xE0, 0x9F, 0xAF, 0xA6, 0xA4,
  27214. 0xCB, 0x63, 0x09, 0x40, 0xC5, 0x8F, 0x88, 0xEB,
  27215. 0x77, 0x8B, 0xFA, 0x44, 0xD7, 0x3B, 0x30, 0x63,
  27216. 0x74, 0x46, 0x8A, 0xBB, 0x8B, 0x56, 0x16, 0x0D,
  27217. 0x49, 0xA2, 0x4E, 0x6F, 0x43, 0x2B, 0xBF, 0xE8,
  27218. 0x8D, 0xE0, 0xC5, 0x6E, 0x9A, 0xE2, 0x13, 0x23,
  27219. 0xF1, 0x2D, 0xEE, 0xFC, 0xAF, 0x32, 0xF6, 0x8C,
  27220. 0x46, 0x21, 0x2F, 0xE3, 0x68, 0x3F, 0xB0, 0x48,
  27221. 0xB7, 0xE2, 0x4C, 0xB8, 0x17, 0xF8, 0x7E, 0xC4,
  27222. 0xA4, 0x2F, 0xE1, 0x1B, 0x21, 0xEA, 0x54, 0x4E,
  27223. 0xB7, 0xD4, 0x82, 0x89, 0xAA, 0xB4, 0xB3, 0x9C,
  27224. 0x57, 0x8F, 0xF3, 0x21, 0xFF, 0xEA, 0x57, 0xE9,
  27225. 0xD8, 0x81, 0x04, 0x9A, 0x1F, 0x92, 0xB7, 0x4A,
  27226. 0xFC, 0xB5, 0x5B, 0xA5, 0x29, 0x1F, 0xF5, 0x25,
  27227. 0x5D, 0x91, 0x11, 0xAA, 0x01, 0x08, 0x52, 0x5A,
  27228. 0xA3, 0x65, 0x33, 0xB4, 0xC2, 0xF0, 0x0C, 0x49,
  27229. 0x73, 0x07, 0x75, 0x67, 0x5C, 0x17, 0x8F, 0xE2,
  27230. 0x80, 0x83, 0x0B, 0xBC, 0x0A, 0xDA, 0x08, 0xC3,
  27231. 0xB3, 0x55, 0x19, 0x90, 0x78, 0x4F, 0xD5, 0x5A,
  27232. 0x12, 0x8B, 0x6A, 0x29, 0xF9, 0xB7, 0xA0, 0xF1,
  27233. 0x68, 0xD5, 0xC2, 0x9D, 0xEC, 0xC9, 0x63, 0x82,
  27234. 0x83, 0x9A, 0xCD, 0xEA, 0x32, 0xA8, 0xD4, 0x90,
  27235. 0x48, 0xFC, 0x47, 0x1D, 0x9B, 0x23, 0xB3, 0xAC,
  27236. 0xBE, 0x7D, 0x16, 0x94, 0x4A, 0xF6, 0x5A, 0x0B,
  27237. 0x76, 0x2D, 0xFF, 0xBA, 0x03, 0xA5, 0x25, 0xCB,
  27238. 0x69, 0x9B, 0x03, 0xB0, 0x7A, 0x2C, 0xFE, 0x55,
  27239. 0x22, 0x1E, 0x52, 0x68, 0x8A, 0xA2, 0xBF, 0xCB,
  27240. 0x8A, 0xA7, 0x3D, 0x75, 0x22, 0x28, 0x29, 0x88,
  27241. 0x8F, 0xA7, 0xC9, 0x97, 0x2A, 0xCD, 0x04, 0x01,
  27242. 0x60, 0xC1, 0x43, 0x25, 0x07, 0x78, 0x65, 0x00,
  27243. 0x08, 0x8A, 0x0A, 0xD5, 0x09, 0xC4, 0x45, 0xBB,
  27244. 0x5B, 0xF1, 0xCF, 0x40, 0x92, 0xAA, 0x74, 0xE1,
  27245. 0x50, 0x34, 0x30, 0x07, 0xE9, 0x00, 0x16, 0xA2,
  27246. 0xF4, 0xBD, 0x6A, 0xE8, 0x6A, 0x71, 0x76, 0x37,
  27247. 0xED, 0x5C, 0x7C, 0x9E, 0x1C, 0xCD, 0x4C, 0x78,
  27248. 0xAF, 0xA2, 0x74, 0xA5, 0x66, 0xB1, 0x7E, 0x5F,
  27249. 0xC4, 0x33, 0x26, 0x7C, 0x40, 0xBD, 0x16, 0x40,
  27250. 0x3C, 0x53, 0x2C, 0x2B, 0x9A, 0xBF, 0xA0, 0xC6,
  27251. 0x25, 0x1B, 0x8A, 0xAD, 0x27, 0xB8, 0x97, 0x3D,
  27252. 0x54, 0x87, 0x0B, 0x50, 0xB9, 0xDE, 0x37, 0xAE,
  27253. 0x5F, 0xA5, 0x6F, 0x79, 0xF9, 0x75, 0x77, 0xE6,
  27254. 0x35, 0xC7, 0x03, 0xBC, 0xBB, 0x02, 0x82, 0x67,
  27255. 0xB5, 0x44, 0x87, 0x96, 0xA0, 0xDC, 0xE0, 0x34,
  27256. 0xE2, 0xCB, 0xBB, 0x09, 0x27, 0x6F, 0x49, 0x50,
  27257. 0xBE, 0x02, 0x23, 0xCC, 0x97, 0xC0, 0x0A, 0x66,
  27258. 0xBB, 0x32, 0x1B, 0x11, 0x4D, 0x5F, 0xB1, 0xBB,
  27259. 0x5A, 0x6C, 0x97, 0x3C, 0xEC, 0x1B, 0x53, 0xDB,
  27260. 0x26, 0x30, 0x81, 0x44, 0x74, 0x76, 0x99, 0x3E,
  27261. 0x68, 0x18, 0xA6, 0xD6, 0xCE, 0x4D, 0x05, 0x61,
  27262. 0xFD, 0xBA, 0x94, 0x1B, 0xD9, 0xBE, 0x17, 0x41,
  27263. 0x6B, 0xEA, 0xBB, 0x51, 0x1C, 0x61, 0xCD, 0xA4,
  27264. 0x12, 0x3D, 0x45, 0xB0, 0x09, 0xEA, 0x6C, 0x09,
  27265. 0xAD, 0x78, 0x09, 0xD4, 0x52, 0xBE, 0x93, 0x42,
  27266. 0x9E, 0x05, 0x39, 0x41, 0xBB, 0xA9, 0xB0, 0x65,
  27267. 0x99, 0x61, 0xBB, 0x02, 0x16, 0x60, 0x08, 0x7C,
  27268. 0xCD, 0x6C, 0x6E, 0x0D, 0xEA, 0xA6, 0xBE, 0xE5,
  27269. 0x87, 0x19, 0x3C, 0x4E, 0x1C, 0x13, 0x20, 0x04,
  27270. 0x5C, 0xB5, 0x09, 0xC7, 0xC0, 0x58, 0x4C, 0x43,
  27271. 0xCD, 0x65, 0x0F, 0xD7, 0x82, 0x65, 0x20, 0x24,
  27272. 0x36, 0xC7, 0xC9, 0x31, 0x75, 0xFB, 0xCB, 0x3B,
  27273. 0x55, 0x69, 0x4A, 0xB9, 0xB8, 0xAE, 0x99, 0xC2,
  27274. 0x69, 0x2D, 0x3C, 0x8E, 0x8C, 0x73, 0x9F, 0xB3,
  27275. 0x78, 0x27, 0x74, 0x15, 0x61, 0xD5, 0xE7, 0x66,
  27276. 0x67, 0x51, 0x82, 0xFF, 0x83, 0xC9, 0x1C, 0xF0,
  27277. 0x33, 0x65, 0x88, 0x23, 0xD8, 0xFA, 0x91, 0xE1
  27278. };
  27279. const byte kyber512_sk[] = {
  27280. 0xD3, 0xBA, 0x7A, 0x3B, 0x91, 0x56, 0x15, 0x32,
  27281. 0x1E, 0xD4, 0xE4, 0x19, 0x6B, 0xC7, 0x34, 0xD0,
  27282. 0x75, 0xB7, 0x40, 0x56, 0xC9, 0x7C, 0x83, 0x00,
  27283. 0x78, 0xF0, 0x28, 0xDD, 0xB3, 0x4E, 0xEF, 0x76,
  27284. 0x9A, 0x22, 0x31, 0xC6, 0x60, 0x05, 0x9A, 0x56,
  27285. 0xA0, 0x9A, 0x64, 0x10, 0x8C, 0x7C, 0x0B, 0xC4,
  27286. 0x8B, 0x0A, 0x5E, 0xD3, 0x47, 0xA4, 0xA8, 0xBC,
  27287. 0x7D, 0xA1, 0x70, 0x74, 0xF4, 0xE3, 0x3E, 0x8A,
  27288. 0x13, 0x0B, 0x2F, 0x67, 0x64, 0x74, 0x63, 0x50,
  27289. 0x0B, 0x50, 0x1E, 0x8C, 0x41, 0x42, 0x8C, 0x87,
  27290. 0x85, 0x63, 0x19, 0x6C, 0x99, 0x40, 0x2B, 0x1F,
  27291. 0x4A, 0x17, 0x9E, 0x18, 0xC2, 0xF8, 0x8B, 0xA5,
  27292. 0x75, 0xD1, 0x5C, 0xF2, 0xF0, 0xA8, 0xDC, 0x3C,
  27293. 0x8A, 0x28, 0x2B, 0x3E, 0xF1, 0x18, 0x1F, 0xAA,
  27294. 0x28, 0x9B, 0x81, 0xF0, 0xA9, 0xBE, 0x47, 0xC3,
  27295. 0xFA, 0xFC, 0xCB, 0x18, 0xAC, 0x08, 0xF8, 0x6C,
  27296. 0xA2, 0xF3, 0xBB, 0x06, 0x99, 0xE5, 0x7E, 0xAD,
  27297. 0x61, 0x0E, 0x6A, 0xA7, 0x02, 0xD1, 0x09, 0xC4,
  27298. 0xF5, 0x04, 0x33, 0xA4, 0x74, 0x2B, 0x74, 0xDC,
  27299. 0x75, 0x27, 0x8B, 0x22, 0x46, 0xA8, 0xC1, 0xE4,
  27300. 0xC3, 0x73, 0xDC, 0x03, 0x85, 0x17, 0xB4, 0x8A,
  27301. 0x0B, 0xDC, 0xB3, 0xB3, 0xB1, 0x9F, 0x5F, 0xFA,
  27302. 0xBD, 0x65, 0x11, 0xAA, 0xD5, 0x4C, 0xCE, 0xB6,
  27303. 0x3C, 0xB9, 0x40, 0xCC, 0xB3, 0xF9, 0x40, 0x75,
  27304. 0xDF, 0x03, 0x19, 0x7D, 0x81, 0xAD, 0x2D, 0x8A,
  27305. 0x8D, 0x35, 0xBC, 0x01, 0x1C, 0xCA, 0xCF, 0xE5,
  27306. 0xB0, 0xA8, 0x21, 0xC1, 0xAD, 0x04, 0x6B, 0xC2,
  27307. 0x0B, 0xD4, 0x48, 0xAB, 0x98, 0xAE, 0x49, 0x46,
  27308. 0x7F, 0xA7, 0xA2, 0xCB, 0x2B, 0xF1, 0x30, 0x32,
  27309. 0xA1, 0x3E, 0x98, 0x65, 0x08, 0x10, 0x0A, 0x22,
  27310. 0x1D, 0x55, 0xAD, 0x97, 0x91, 0xAE, 0x47, 0x37,
  27311. 0x91, 0xCA, 0xA3, 0x18, 0x91, 0x5B, 0x58, 0x3F,
  27312. 0xD4, 0x73, 0x91, 0xC2, 0x31, 0xD9, 0x0B, 0xB7,
  27313. 0x29, 0xB2, 0x06, 0x2A, 0x0A, 0xC4, 0x30, 0x91,
  27314. 0xAF, 0x14, 0x05, 0x02, 0xB0, 0x62, 0x42, 0xFD,
  27315. 0x65, 0x04, 0xFB, 0x74, 0x9C, 0x20, 0x93, 0xCD,
  27316. 0x78, 0x00, 0x63, 0x4B, 0x48, 0xBA, 0xFC, 0xE0,
  27317. 0xB1, 0xDC, 0x22, 0x2A, 0x42, 0xB9, 0x70, 0xB7,
  27318. 0x34, 0x26, 0x02, 0x1B, 0xC0, 0x8F, 0xE7, 0x10,
  27319. 0x98, 0x6B, 0x3E, 0xD6, 0x47, 0x9C, 0x21, 0xEB,
  27320. 0xBB, 0x9D, 0x94, 0x6D, 0x13, 0x3A, 0x97, 0x55,
  27321. 0xB7, 0x33, 0xB0, 0xBA, 0x79, 0x5D, 0x34, 0xAA,
  27322. 0x2C, 0x43, 0x6E, 0xB3, 0x04, 0x87, 0xFE, 0x19,
  27323. 0x3C, 0xDE, 0x86, 0xC7, 0x54, 0x00, 0x14, 0x85,
  27324. 0x48, 0x4C, 0x4D, 0x28, 0x89, 0xD9, 0x67, 0x66,
  27325. 0x7E, 0xD1, 0x72, 0xAB, 0xE4, 0x52, 0x5C, 0x05,
  27326. 0xA7, 0xE9, 0x53, 0x8E, 0x1B, 0x0C, 0x38, 0xBE,
  27327. 0x40, 0x7D, 0x3F, 0x09, 0x72, 0xEA, 0x82, 0xB9,
  27328. 0xA7, 0xB1, 0x55, 0x0B, 0x2A, 0x20, 0x68, 0xFA,
  27329. 0x80, 0x56, 0x86, 0xB2, 0x7F, 0x50, 0xAD, 0xCC,
  27330. 0x47, 0x30, 0xCA, 0xF9, 0x04, 0x28, 0x74, 0x11,
  27331. 0xED, 0xB4, 0x82, 0x46, 0x36, 0x6C, 0x4B, 0x02,
  27332. 0xB7, 0x95, 0x96, 0x69, 0x81, 0x56, 0xA0, 0xE4,
  27333. 0x89, 0x65, 0x2D, 0xA5, 0xC2, 0x2B, 0x51, 0x24,
  27334. 0xD7, 0x90, 0x33, 0xD9, 0x84, 0x0D, 0x51, 0x93,
  27335. 0x39, 0xF2, 0xA9, 0x90, 0x72, 0x1C, 0x73, 0x1B,
  27336. 0x02, 0xC2, 0x36, 0xBC, 0x72, 0x8C, 0x22, 0x61,
  27337. 0xFF, 0x18, 0x18, 0xA3, 0x8A, 0x4D, 0x39, 0x54,
  27338. 0x61, 0xAE, 0x20, 0xC4, 0x2E, 0x12, 0xB0, 0x1C,
  27339. 0xBC, 0x85, 0xFE, 0xC8, 0x9E, 0x9F, 0x75, 0x9D,
  27340. 0x20, 0xE3, 0x87, 0x4F, 0x37, 0x16, 0x0E, 0x19,
  27341. 0x5C, 0x4C, 0x61, 0x47, 0x54, 0xC5, 0x93, 0x03,
  27342. 0xD0, 0x9B, 0x27, 0x76, 0xAE, 0xF6, 0x8C, 0x5B,
  27343. 0xF6, 0x1C, 0x4B, 0x50, 0x47, 0x42, 0x41, 0x3A,
  27344. 0xC3, 0xD7, 0xC4, 0x95, 0x7C, 0x47, 0x0B, 0xC2,
  27345. 0x9A, 0x72, 0x2C, 0xC3, 0x7F, 0xB2, 0xB2, 0x30,
  27346. 0x45, 0x7A, 0x3A, 0x98, 0x8A, 0xBE, 0x48, 0x96,
  27347. 0x71, 0x70, 0x54, 0x4F, 0xD2, 0x57, 0xBB, 0x89,
  27348. 0xEC, 0xC3, 0x13, 0xF1, 0xBD, 0x33, 0x10, 0xCA,
  27349. 0x37, 0xB0, 0x2D, 0x6E, 0x97, 0x07, 0xCA, 0x91,
  27350. 0xB6, 0x25, 0x97, 0x7B, 0xED, 0xB3, 0x1B, 0xA8,
  27351. 0x27, 0x48, 0xEE, 0x31, 0xAF, 0x2B, 0xC7, 0x62,
  27352. 0x92, 0xA7, 0x2B, 0xCF, 0xA0, 0x46, 0xE9, 0x31,
  27353. 0x48, 0xBE, 0x8C, 0x83, 0xB2, 0x58, 0x50, 0x7D,
  27354. 0x96, 0x6F, 0x14, 0xD2, 0x02, 0x81, 0xA3, 0x33,
  27355. 0x3E, 0x69, 0xAB, 0x78, 0x60, 0x0D, 0x3F, 0x3B,
  27356. 0x6B, 0xAF, 0x7A, 0xC9, 0xAC, 0xEA, 0xB5, 0x5B,
  27357. 0xB2, 0x0C, 0xE7, 0x5C, 0x13, 0x47, 0x4A, 0x67,
  27358. 0x8E, 0x7C, 0x60, 0xC0, 0x81, 0x87, 0x1E, 0xF3,
  27359. 0x3C, 0xA9, 0xEC, 0x6C, 0xD6, 0xA3, 0x64, 0x3B,
  27360. 0x6A, 0x41, 0x60, 0xF8, 0x3A, 0x27, 0x8C, 0x21,
  27361. 0x0C, 0xA2, 0xBC, 0xB9, 0xE0, 0x12, 0x27, 0x01,
  27362. 0xC9, 0xAD, 0xF0, 0x45, 0x8E, 0xF4, 0x71, 0x9F,
  27363. 0xB5, 0xB4, 0x01, 0xB3, 0xB5, 0x16, 0xE1, 0xAD,
  27364. 0x25, 0xD7, 0xA4, 0x84, 0xA6, 0x3E, 0xE4, 0x1A,
  27365. 0x81, 0x44, 0xE1, 0xAA, 0x38, 0x8A, 0xC8, 0xAE,
  27366. 0x69, 0x00, 0xCE, 0x87, 0x9E, 0x9F, 0xD7, 0x4F,
  27367. 0x47, 0xC7, 0x30, 0x78, 0xCC, 0x6E, 0x85, 0xC7,
  27368. 0x64, 0x15, 0x0C, 0x00, 0x3A, 0x58, 0x70, 0x56,
  27369. 0x97, 0x33, 0xDA, 0x66, 0x8B, 0xDA, 0x15, 0xC5,
  27370. 0xA2, 0x69, 0x9D, 0x38, 0x25, 0x82, 0x6B, 0x36,
  27371. 0x98, 0x5A, 0x51, 0x08, 0x51, 0x0B, 0x6F, 0xC3,
  27372. 0x65, 0x4B, 0x17, 0x51, 0x02, 0xA4, 0xD9, 0x06,
  27373. 0x14, 0x47, 0xA7, 0x8B, 0xA9, 0x39, 0x40, 0x8B,
  27374. 0x9A, 0x94, 0x57, 0x61, 0xEC, 0x0C, 0x24, 0x6F,
  27375. 0x8A, 0x64, 0x18, 0xF6, 0x2F, 0x1E, 0xA3, 0x00,
  27376. 0x68, 0xD6, 0x83, 0xF6, 0x01, 0x8F, 0xCB, 0x38,
  27377. 0x71, 0xC0, 0x64, 0x0A, 0xD1, 0x7B, 0x7E, 0x80,
  27378. 0x02, 0x93, 0xC9, 0xF6, 0x36, 0xC1, 0xE2, 0x09,
  27379. 0x10, 0x34, 0x25, 0xAD, 0x72, 0x26, 0xDD, 0x13,
  27380. 0x90, 0x41, 0xFA, 0x84, 0x9F, 0x8C, 0x2F, 0xED,
  27381. 0x18, 0xAB, 0xEF, 0xF5, 0x2A, 0xE0, 0xB6, 0x06,
  27382. 0x86, 0x13, 0xBA, 0x05, 0x73, 0x09, 0x3D, 0xE8,
  27383. 0xAC, 0xC8, 0x56, 0x19, 0x80, 0x62, 0xCD, 0x38,
  27384. 0xDC, 0x0F, 0x80, 0x05, 0x6B, 0x8D, 0x1C, 0x42,
  27385. 0xDF, 0xA3, 0x2C, 0x4C, 0xA4, 0x0E, 0x8C, 0x57,
  27386. 0x03, 0xF3, 0x13, 0xBA, 0x5F, 0xD0, 0x5C, 0x1E,
  27387. 0x75, 0xC3, 0x78, 0xD7, 0x04, 0x77, 0x62, 0x1A,
  27388. 0xF3, 0xA0, 0x8E, 0x52, 0x0A, 0x16, 0x05, 0xC4,
  27389. 0x89, 0x42, 0x2C, 0x97, 0xD0, 0xD9, 0x2E, 0x43,
  27390. 0x12, 0xBB, 0xEF, 0xB6, 0xC0, 0x18, 0x7A, 0x45,
  27391. 0xF9, 0x24, 0x6B, 0xF8, 0xEB, 0xA6, 0x2C, 0x04,
  27392. 0x7F, 0x8C, 0xB2, 0x42, 0x17, 0xB2, 0x01, 0x78,
  27393. 0xBB, 0x09, 0x5E, 0x59, 0x63, 0x6B, 0xCB, 0xA9,
  27394. 0x13, 0x73, 0x52, 0x65, 0xC7, 0xA0, 0x90, 0x06,
  27395. 0x12, 0xEF, 0x16, 0x41, 0xC9, 0xE1, 0x4D, 0x7D,
  27396. 0x89, 0x15, 0xE3, 0xC8, 0x83, 0xB7, 0x50, 0x68,
  27397. 0xA9, 0xB1, 0x3B, 0x0C, 0xF0, 0xA1, 0x32, 0x8A,
  27398. 0x3E, 0xC5, 0xD6, 0x00, 0x1E, 0xE9, 0x6D, 0x01,
  27399. 0x05, 0x6C, 0x25, 0x09, 0xC2, 0x16, 0x51, 0x4D,
  27400. 0xA5, 0x24, 0x61, 0xF7, 0xCA, 0x6A, 0xC4, 0x1A,
  27401. 0x2C, 0x0E, 0x35, 0x2B, 0x53, 0x63, 0x68, 0x99,
  27402. 0xD0, 0x65, 0xBD, 0xF2, 0xBF, 0x14, 0xF5, 0x35,
  27403. 0xFC, 0xE9, 0x42, 0x93, 0x9B, 0x6B, 0x4B, 0x97,
  27404. 0x72, 0x04, 0xD6, 0x29, 0xA6, 0x24, 0x1F, 0xFD,
  27405. 0x99, 0x94, 0x6A, 0x70, 0xBC, 0xA8, 0x91, 0x2B,
  27406. 0x1D, 0xFA, 0x61, 0x4A, 0xE9, 0x91, 0x3C, 0xB3,
  27407. 0x65, 0x63, 0x04, 0x7F, 0x9F, 0xA0, 0xA6, 0x48,
  27408. 0x7B, 0xA4, 0x10, 0x19, 0x5F, 0x56, 0x17, 0x58,
  27409. 0x84, 0x41, 0xC6, 0xA1, 0xA1, 0x50, 0x78, 0xBA,
  27410. 0x60, 0x93, 0x36, 0x79, 0x64, 0xD2, 0x5F, 0x19,
  27411. 0xD7, 0x15, 0x1A, 0xE0, 0x9F, 0xAF, 0xA6, 0xA4,
  27412. 0xCB, 0x63, 0x09, 0x40, 0xC5, 0x8F, 0x88, 0xEB,
  27413. 0x77, 0x8B, 0xFA, 0x44, 0xD7, 0x3B, 0x30, 0x63,
  27414. 0x74, 0x46, 0x8A, 0xBB, 0x8B, 0x56, 0x16, 0x0D,
  27415. 0x49, 0xA2, 0x4E, 0x6F, 0x43, 0x2B, 0xBF, 0xE8,
  27416. 0x8D, 0xE0, 0xC5, 0x6E, 0x9A, 0xE2, 0x13, 0x23,
  27417. 0xF1, 0x2D, 0xEE, 0xFC, 0xAF, 0x32, 0xF6, 0x8C,
  27418. 0x46, 0x21, 0x2F, 0xE3, 0x68, 0x3F, 0xB0, 0x48,
  27419. 0xB7, 0xE2, 0x4C, 0xB8, 0x17, 0xF8, 0x7E, 0xC4,
  27420. 0xA4, 0x2F, 0xE1, 0x1B, 0x21, 0xEA, 0x54, 0x4E,
  27421. 0xB7, 0xD4, 0x82, 0x89, 0xAA, 0xB4, 0xB3, 0x9C,
  27422. 0x57, 0x8F, 0xF3, 0x21, 0xFF, 0xEA, 0x57, 0xE9,
  27423. 0xD8, 0x81, 0x04, 0x9A, 0x1F, 0x92, 0xB7, 0x4A,
  27424. 0xFC, 0xB5, 0x5B, 0xA5, 0x29, 0x1F, 0xF5, 0x25,
  27425. 0x5D, 0x91, 0x11, 0xAA, 0x01, 0x08, 0x52, 0x5A,
  27426. 0xA3, 0x65, 0x33, 0xB4, 0xC2, 0xF0, 0x0C, 0x49,
  27427. 0x73, 0x07, 0x75, 0x67, 0x5C, 0x17, 0x8F, 0xE2,
  27428. 0x80, 0x83, 0x0B, 0xBC, 0x0A, 0xDA, 0x08, 0xC3,
  27429. 0xB3, 0x55, 0x19, 0x90, 0x78, 0x4F, 0xD5, 0x5A,
  27430. 0x12, 0x8B, 0x6A, 0x29, 0xF9, 0xB7, 0xA0, 0xF1,
  27431. 0x68, 0xD5, 0xC2, 0x9D, 0xEC, 0xC9, 0x63, 0x82,
  27432. 0x83, 0x9A, 0xCD, 0xEA, 0x32, 0xA8, 0xD4, 0x90,
  27433. 0x48, 0xFC, 0x47, 0x1D, 0x9B, 0x23, 0xB3, 0xAC,
  27434. 0xBE, 0x7D, 0x16, 0x94, 0x4A, 0xF6, 0x5A, 0x0B,
  27435. 0x76, 0x2D, 0xFF, 0xBA, 0x03, 0xA5, 0x25, 0xCB,
  27436. 0x69, 0x9B, 0x03, 0xB0, 0x7A, 0x2C, 0xFE, 0x55,
  27437. 0x22, 0x1E, 0x52, 0x68, 0x8A, 0xA2, 0xBF, 0xCB,
  27438. 0x8A, 0xA7, 0x3D, 0x75, 0x22, 0x28, 0x29, 0x88,
  27439. 0x8F, 0xA7, 0xC9, 0x97, 0x2A, 0xCD, 0x04, 0x01,
  27440. 0x60, 0xC1, 0x43, 0x25, 0x07, 0x78, 0x65, 0x00,
  27441. 0x08, 0x8A, 0x0A, 0xD5, 0x09, 0xC4, 0x45, 0xBB,
  27442. 0x5B, 0xF1, 0xCF, 0x40, 0x92, 0xAA, 0x74, 0xE1,
  27443. 0x50, 0x34, 0x30, 0x07, 0xE9, 0x00, 0x16, 0xA2,
  27444. 0xF4, 0xBD, 0x6A, 0xE8, 0x6A, 0x71, 0x76, 0x37,
  27445. 0xED, 0x5C, 0x7C, 0x9E, 0x1C, 0xCD, 0x4C, 0x78,
  27446. 0xAF, 0xA2, 0x74, 0xA5, 0x66, 0xB1, 0x7E, 0x5F,
  27447. 0xC4, 0x33, 0x26, 0x7C, 0x40, 0xBD, 0x16, 0x40,
  27448. 0x3C, 0x53, 0x2C, 0x2B, 0x9A, 0xBF, 0xA0, 0xC6,
  27449. 0x25, 0x1B, 0x8A, 0xAD, 0x27, 0xB8, 0x97, 0x3D,
  27450. 0x54, 0x87, 0x0B, 0x50, 0xB9, 0xDE, 0x37, 0xAE,
  27451. 0x5F, 0xA5, 0x6F, 0x79, 0xF9, 0x75, 0x77, 0xE6,
  27452. 0x35, 0xC7, 0x03, 0xBC, 0xBB, 0x02, 0x82, 0x67,
  27453. 0xB5, 0x44, 0x87, 0x96, 0xA0, 0xDC, 0xE0, 0x34,
  27454. 0xE2, 0xCB, 0xBB, 0x09, 0x27, 0x6F, 0x49, 0x50,
  27455. 0xBE, 0x02, 0x23, 0xCC, 0x97, 0xC0, 0x0A, 0x66,
  27456. 0xBB, 0x32, 0x1B, 0x11, 0x4D, 0x5F, 0xB1, 0xBB,
  27457. 0x5A, 0x6C, 0x97, 0x3C, 0xEC, 0x1B, 0x53, 0xDB,
  27458. 0x26, 0x30, 0x81, 0x44, 0x74, 0x76, 0x99, 0x3E,
  27459. 0x68, 0x18, 0xA6, 0xD6, 0xCE, 0x4D, 0x05, 0x61,
  27460. 0xFD, 0xBA, 0x94, 0x1B, 0xD9, 0xBE, 0x17, 0x41,
  27461. 0x6B, 0xEA, 0xBB, 0x51, 0x1C, 0x61, 0xCD, 0xA4,
  27462. 0x12, 0x3D, 0x45, 0xB0, 0x09, 0xEA, 0x6C, 0x09,
  27463. 0xAD, 0x78, 0x09, 0xD4, 0x52, 0xBE, 0x93, 0x42,
  27464. 0x9E, 0x05, 0x39, 0x41, 0xBB, 0xA9, 0xB0, 0x65,
  27465. 0x99, 0x61, 0xBB, 0x02, 0x16, 0x60, 0x08, 0x7C,
  27466. 0xCD, 0x6C, 0x6E, 0x0D, 0xEA, 0xA6, 0xBE, 0xE5,
  27467. 0x87, 0x19, 0x3C, 0x4E, 0x1C, 0x13, 0x20, 0x04,
  27468. 0x5C, 0xB5, 0x09, 0xC7, 0xC0, 0x58, 0x4C, 0x43,
  27469. 0xCD, 0x65, 0x0F, 0xD7, 0x82, 0x65, 0x20, 0x24,
  27470. 0x36, 0xC7, 0xC9, 0x31, 0x75, 0xFB, 0xCB, 0x3B,
  27471. 0x55, 0x69, 0x4A, 0xB9, 0xB8, 0xAE, 0x99, 0xC2,
  27472. 0x69, 0x2D, 0x3C, 0x8E, 0x8C, 0x73, 0x9F, 0xB3,
  27473. 0x78, 0x27, 0x74, 0x15, 0x61, 0xD5, 0xE7, 0x66,
  27474. 0x67, 0x51, 0x82, 0xFF, 0x83, 0xC9, 0x1C, 0xF0,
  27475. 0x33, 0x65, 0x88, 0x23, 0xD8, 0xFA, 0x91, 0xE1,
  27476. 0xC1, 0xC0, 0x8E, 0xFF, 0x2A, 0xB6, 0xE2, 0xAF,
  27477. 0x9F, 0x29, 0xA6, 0x32, 0xC5, 0x93, 0x32, 0xE9,
  27478. 0xA6, 0x1B, 0x63, 0x14, 0x6E, 0x8A, 0xC3, 0x5E,
  27479. 0xF3, 0xD3, 0xC4, 0x5A, 0x10, 0x10, 0xAC, 0xE2,
  27480. 0x86, 0x26, 0xED, 0x79, 0xD4, 0x51, 0x14, 0x08,
  27481. 0x00, 0xE0, 0x3B, 0x59, 0xB9, 0x56, 0xF8, 0x21,
  27482. 0x0E, 0x55, 0x60, 0x67, 0x40, 0x7D, 0x13, 0xDC,
  27483. 0x90, 0xFA, 0x9E, 0x8B, 0x87, 0x2B, 0xFB, 0x8F
  27484. };
  27485. const byte kyber512_ct[] = {
  27486. 0x58, 0x56, 0xAE, 0x75, 0x76, 0x21, 0xFD, 0x94,
  27487. 0x9B, 0xA5, 0x49, 0xD2, 0x49, 0x70, 0x52, 0x5F,
  27488. 0x17, 0x95, 0x60, 0xE3, 0x24, 0xF7, 0x1B, 0x3C,
  27489. 0x1F, 0xDC, 0xFC, 0xAF, 0x92, 0xFD, 0x7F, 0xED,
  27490. 0x6B, 0x35, 0x1F, 0x0D, 0xA4, 0x1C, 0x98, 0x51,
  27491. 0x24, 0x9F, 0x6E, 0x63, 0xB7, 0xBC, 0xE3, 0x4F,
  27492. 0xD4, 0x9C, 0x97, 0x70, 0x58, 0x8B, 0x94, 0x2D,
  27493. 0x1B, 0x51, 0x69, 0x57, 0xDB, 0x3A, 0xE9, 0x7C,
  27494. 0x5F, 0x94, 0x51, 0x8D, 0x40, 0xF5, 0x4B, 0x5B,
  27495. 0x78, 0xE1, 0xF3, 0x8F, 0x8D, 0x61, 0x27, 0xA5,
  27496. 0xD0, 0xD5, 0xD4, 0x24, 0x29, 0xEA, 0x79, 0x62,
  27497. 0x13, 0xC0, 0x77, 0x2D, 0x5C, 0x9B, 0x99, 0x2C,
  27498. 0xEC, 0x72, 0x1B, 0x52, 0x17, 0xB6, 0x91, 0x7F,
  27499. 0xF8, 0xC0, 0xCC, 0xBB, 0xCC, 0xFE, 0x13, 0x4B,
  27500. 0xD8, 0x9A, 0x99, 0x48, 0x0B, 0x95, 0x66, 0xE4,
  27501. 0x69, 0x60, 0xBD, 0x21, 0x8C, 0xAC, 0x2D, 0xDD,
  27502. 0x58, 0x00, 0xB0, 0x83, 0x01, 0x9F, 0x09, 0x42,
  27503. 0x0E, 0x8B, 0xA2, 0x8E, 0x1E, 0x4D, 0xCC, 0x51,
  27504. 0xD4, 0xF5, 0xF9, 0x57, 0xD4, 0x57, 0x5B, 0xC3,
  27505. 0x2C, 0xF3, 0xDD, 0x08, 0x15, 0xD7, 0xE1, 0xEC,
  27506. 0xDA, 0x47, 0x44, 0x3B, 0x34, 0xCD, 0x88, 0x68,
  27507. 0xE7, 0x39, 0xD1, 0x57, 0x90, 0x29, 0x37, 0x12,
  27508. 0xBF, 0xF0, 0x89, 0x19, 0x8D, 0xFF, 0xA8, 0x42,
  27509. 0xD9, 0x5C, 0x03, 0x68, 0x49, 0x6C, 0x3E, 0x7D,
  27510. 0xFA, 0x06, 0xC8, 0x99, 0x9B, 0x9A, 0xEA, 0x3B,
  27511. 0x09, 0xC6, 0xD3, 0x62, 0x80, 0xF7, 0x6D, 0xD9,
  27512. 0xC8, 0xC1, 0x58, 0x53, 0xB4, 0x5E, 0xBC, 0xCA,
  27513. 0x5E, 0xC8, 0xF9, 0xB1, 0xF5, 0x7C, 0xFA, 0x6A,
  27514. 0x60, 0x19, 0xA0, 0x52, 0xCD, 0x9D, 0xD9, 0x5F,
  27515. 0x6F, 0x25, 0x97, 0x3D, 0xFA, 0xB9, 0x79, 0x7B,
  27516. 0x3F, 0xDB, 0xDC, 0x2C, 0xEF, 0xCE, 0x8B, 0x0B,
  27517. 0x35, 0x33, 0xA3, 0xA8, 0x54, 0x96, 0xA4, 0x11,
  27518. 0x09, 0xF6, 0x14, 0xB0, 0x53, 0xB0, 0x8E, 0x48,
  27519. 0xFE, 0xA9, 0x2F, 0xA5, 0x61, 0x07, 0xB5, 0x24,
  27520. 0xDD, 0x28, 0x77, 0x93, 0xA0, 0x24, 0x50, 0x03,
  27521. 0xEA, 0x35, 0xA0, 0x4E, 0x03, 0xEA, 0x59, 0x3D,
  27522. 0xAA, 0xFE, 0x31, 0x24, 0xB1, 0x7D, 0x0F, 0x2D,
  27523. 0xC3, 0xBE, 0xAE, 0xD3, 0x48, 0xD8, 0x96, 0xD1,
  27524. 0x65, 0xE5, 0x24, 0x08, 0x67, 0xA2, 0xCB, 0x72,
  27525. 0x3A, 0xA3, 0x88, 0x62, 0xC0, 0x47, 0x45, 0x75,
  27526. 0x40, 0xB4, 0xA4, 0xC0, 0x06, 0xF8, 0x22, 0xBE,
  27527. 0xEF, 0xB6, 0xF2, 0x75, 0xF5, 0x82, 0x8D, 0x3B,
  27528. 0x79, 0xC5, 0x3F, 0x87, 0x5E, 0x3C, 0x5B, 0xD4,
  27529. 0xA7, 0x66, 0x5C, 0xD1, 0x0C, 0xFA, 0x25, 0x1B,
  27530. 0x79, 0xE7, 0x6D, 0x26, 0xC5, 0xDB, 0x24, 0x25,
  27531. 0x72, 0xE5, 0xD8, 0x11, 0xC4, 0x60, 0x04, 0xB0,
  27532. 0xCC, 0xAB, 0x1C, 0xE5, 0x00, 0x2C, 0xA1, 0xDE,
  27533. 0x61, 0x4F, 0x31, 0x0B, 0x30, 0x9D, 0xFA, 0x75,
  27534. 0x63, 0xD0, 0x66, 0x23, 0x03, 0x6C, 0x7C, 0x26,
  27535. 0x17, 0x96, 0x40, 0x26, 0x45, 0x03, 0x60, 0xEF,
  27536. 0x52, 0x5B, 0x1B, 0xE7, 0xD8, 0x16, 0xEA, 0x1D,
  27537. 0xDF, 0xA1, 0x07, 0x15, 0xA1, 0x14, 0x86, 0x9E,
  27538. 0x62, 0x70, 0xC0, 0x3C, 0xDC, 0x58, 0x52, 0xD7,
  27539. 0x20, 0x46, 0x81, 0xB0, 0xF4, 0xD1, 0xB2, 0xA3,
  27540. 0x88, 0xAC, 0x06, 0x1B, 0x99, 0xAE, 0x0C, 0x01,
  27541. 0x1A, 0xFF, 0x0D, 0x3D, 0x89, 0x6B, 0xFF, 0xE6,
  27542. 0x27, 0x2D, 0xCF, 0x30, 0x9B, 0xB4, 0xF8, 0x6E,
  27543. 0xF4, 0xB5, 0x8B, 0xA8, 0xF4, 0xA1, 0x41, 0x15,
  27544. 0x38, 0x83, 0x4B, 0xAE, 0x55, 0x22, 0xBB, 0x51,
  27545. 0x3B, 0xF3, 0x56, 0xA5, 0x20, 0x3F, 0xE8, 0xAA,
  27546. 0x57, 0x5A, 0xCC, 0xD0, 0x94, 0xC9, 0x55, 0x2C,
  27547. 0xC7, 0xB6, 0x40, 0x75, 0x2C, 0xDE, 0x80, 0xD1,
  27548. 0x5F, 0x7F, 0x6B, 0xE0, 0x88, 0xA1, 0x2C, 0x5C,
  27549. 0x58, 0x47, 0xDD, 0x56, 0xCA, 0x66, 0x26, 0x7C,
  27550. 0x08, 0xDC, 0xD5, 0x16, 0x70, 0x6F, 0x3A, 0xE9,
  27551. 0x0F, 0x17, 0xA7, 0x42, 0x6A, 0x1B, 0x24, 0x44,
  27552. 0x05, 0x6E, 0x89, 0x5B, 0x77, 0x6C, 0x1E, 0x7A,
  27553. 0xE1, 0x65, 0xE4, 0xE9, 0x5D, 0x62, 0x0B, 0x20,
  27554. 0x54, 0xE5, 0x00, 0x5C, 0x62, 0x2C, 0x97, 0x03,
  27555. 0x4F, 0x96, 0x39, 0x14, 0x28, 0x0A, 0x0A, 0x06,
  27556. 0x2C, 0x2A, 0x63, 0x0E, 0x65, 0xCF, 0x64, 0xF2,
  27557. 0xD7, 0x0C, 0xD6, 0x0F, 0xE1, 0x57, 0xE3, 0x20,
  27558. 0xB7, 0xE2, 0xD6, 0x33, 0x2C, 0xDB, 0x73, 0x5E,
  27559. 0xA9, 0x8B, 0xEB, 0x1E, 0x2E, 0x01, 0x5C, 0x0A,
  27560. 0x2B, 0x33, 0xD7, 0x0F, 0xE7, 0x68, 0xF1, 0x77,
  27561. 0xE6, 0x23, 0x91, 0x04, 0x88, 0xE8, 0xBB, 0x2F,
  27562. 0x83, 0x83, 0xE3, 0xE4, 0xA6, 0xE0, 0xFC, 0xA1,
  27563. 0x11, 0xF0, 0xA0, 0x2E, 0x57, 0x3B, 0xFB, 0x38,
  27564. 0xBC, 0x34, 0x73, 0x47, 0x08, 0x11, 0x62, 0x08,
  27565. 0xCA, 0x3E, 0x82, 0xEF, 0xE5, 0xC2, 0x15, 0x44,
  27566. 0x3D, 0x3F, 0xE9, 0x46, 0x3B, 0x61, 0x4D, 0xA1,
  27567. 0x7A, 0x09, 0x29, 0x37, 0xFB, 0x9C, 0xCB, 0xCE,
  27568. 0xE9, 0xC8, 0x84, 0x01, 0xCD, 0xC3, 0x55, 0x7E,
  27569. 0xF5, 0xBF, 0xFE, 0x89, 0x45, 0x2C, 0x42, 0x1A,
  27570. 0x6E, 0x8C, 0xB0, 0x0D, 0xA5, 0x00, 0xDB, 0x90,
  27571. 0x94, 0xA9, 0x92, 0xD5, 0x76, 0xAA, 0xCB, 0x2D,
  27572. 0x17, 0xB5, 0xF5, 0xEA, 0x5B, 0xC4, 0x58, 0x8E,
  27573. 0x2B, 0x2E, 0x08, 0x76, 0x9D, 0x62, 0x6C, 0x00,
  27574. 0x98, 0x0D, 0x51, 0x63, 0x9A, 0x43, 0xA0, 0xD0,
  27575. 0xEA, 0x5C, 0xCC, 0x26, 0x9E, 0x86, 0x8C, 0x1E,
  27576. 0xB1, 0xEE, 0xC8, 0x08, 0xCE, 0x64, 0x20, 0x8C,
  27577. 0x6B, 0xA0, 0x73, 0x5A, 0x0B, 0x42, 0x06, 0xB1,
  27578. 0x08, 0x28, 0xCE, 0x9B, 0x5E, 0x51, 0xCD, 0x5F,
  27579. 0x0E, 0xA5, 0x77, 0x7D, 0x0B, 0x7A, 0xCF, 0x14,
  27580. 0x42, 0xC9, 0xAA, 0xCC, 0x3E, 0x80, 0x87, 0x8C,
  27581. 0x4D, 0x7D, 0x3B, 0x6F, 0xDC, 0x56, 0xEF, 0x17
  27582. };
  27583. const byte kyber512_ss[] = {
  27584. 0x0C, 0x92, 0x39, 0xC7, 0x70, 0x5D, 0x63, 0x91,
  27585. 0x51, 0xAD, 0x1B, 0xCA, 0xDF, 0x58, 0xBD, 0x99,
  27586. 0x91, 0x0B, 0x7A, 0x12, 0x44, 0x99, 0x17, 0x21,
  27587. 0x13, 0x22, 0x8B, 0x4C, 0x75, 0xF8, 0x22, 0xE1
  27588. };
  27589. #endif
  27590. ret = wc_KyberKey_Init(KYBER512, &key, HEAP_HINT, INVALID_DEVID);
  27591. if (ret != 0)
  27592. return -20101;
  27593. ret = wc_KyberKey_MakeKeyWithRandom(&key, kyber512_rand,
  27594. sizeof(kyber512_rand));
  27595. if (ret != 0)
  27596. return -20102;
  27597. ret = wc_KyberKey_EncodePublicKey(&key, pub, sizeof(pub));
  27598. if (ret != 0)
  27599. return -20103;
  27600. ret = wc_KyberKey_EncodePrivateKey(&key, priv, sizeof(priv));
  27601. if (ret != 0)
  27602. return -20104;
  27603. if (XMEMCMP(pub, kyber512_pk, sizeof(kyber512_pk)) != 0)
  27604. return -20105;
  27605. if (XMEMCMP(priv, kyber512_sk, sizeof(kyber512_sk)) != 0)
  27606. return -20106;
  27607. ret = wc_KyberKey_EncapsulateWithRandom(&key, ct, ss, kyber512enc_rand,
  27608. sizeof(kyber512enc_rand));
  27609. if (ret != 0)
  27610. return -20107;
  27611. if (XMEMCMP(ct, kyber512_ct, sizeof(kyber512_ct)) != 0)
  27612. return -20108;
  27613. if (XMEMCMP(ss, kyber512_ss, sizeof(kyber512_ss)) != 0)
  27614. return -20109;
  27615. ret = wc_KyberKey_Decapsulate(&key, ss_dec, ct, sizeof(kyber512_ct));
  27616. if (ret != 0)
  27617. return -20110;
  27618. if (XMEMCMP(ss_dec, kyber512_ss, sizeof(kyber512_ss)) != 0)
  27619. return -20111;
  27620. wc_KyberKey_Free(&key);
  27621. return 0;
  27622. }
  27623. #endif /* WOLFSSL_KYBER512 */
  27624. #ifdef WOLFSSL_KYBER768
  27625. static int kyber768_kat(void)
  27626. {
  27627. KyberKey key;
  27628. int ret;
  27629. byte priv[KYBER768_PRIVATE_KEY_SIZE];
  27630. byte pub[KYBER768_PUBLIC_KEY_SIZE];
  27631. byte ct[KYBER768_CIPHER_TEXT_SIZE];
  27632. byte ss[KYBER_SS_SZ];
  27633. byte ss_dec[KYBER_SS_SZ];
  27634. const byte kyber768_rand[] = {
  27635. 0x7c, 0x99, 0x35, 0xa0, 0xb0, 0x76, 0x94, 0xaa,
  27636. 0x0c, 0x6d, 0x10, 0xe4, 0xdb, 0x6b, 0x1a, 0xdd,
  27637. 0x2f, 0xd8, 0x1a, 0x25, 0xcc, 0xb1, 0x48, 0x03,
  27638. 0x2d, 0xcd, 0x73, 0x99, 0x36, 0x73, 0x7f, 0x2d,
  27639. 0x86, 0x26, 0xED, 0x79, 0xD4, 0x51, 0x14, 0x08,
  27640. 0x00, 0xE0, 0x3B, 0x59, 0xB9, 0x56, 0xF8, 0x21,
  27641. 0x0E, 0x55, 0x60, 0x67, 0x40, 0x7D, 0x13, 0xDC,
  27642. 0x90, 0xFA, 0x9E, 0x8B, 0x87, 0x2B, 0xFB, 0x8F
  27643. };
  27644. const byte kyber768enc_rand[] = {
  27645. 0x14, 0x7c, 0x03, 0xf7, 0xa5, 0xbe, 0xbb, 0xa4,
  27646. 0x06, 0xc8, 0xfa, 0xe1, 0x87, 0x4d, 0x7f, 0x13,
  27647. 0xc8, 0x0e, 0xfe, 0x79, 0xa3, 0xa9, 0xa8, 0x74,
  27648. 0xcc, 0x09, 0xfe, 0x76, 0xf6, 0x99, 0x76, 0x15
  27649. };
  27650. #ifndef WOLFSSL_KYBER_90S
  27651. const byte kyber768_pk[] = {
  27652. 0xA7, 0x2C, 0x2D, 0x9C, 0x84, 0x3E, 0xE9, 0xF8,
  27653. 0x31, 0x3E, 0xCC, 0x7F, 0x86, 0xD6, 0x29, 0x4D,
  27654. 0x59, 0x15, 0x9D, 0x9A, 0x87, 0x9A, 0x54, 0x2E,
  27655. 0x26, 0x09, 0x22, 0xAD, 0xF9, 0x99, 0x05, 0x1C,
  27656. 0xC4, 0x52, 0x00, 0xC9, 0xFF, 0xDB, 0x60, 0x44,
  27657. 0x9C, 0x49, 0x46, 0x59, 0x79, 0x27, 0x23, 0x67,
  27658. 0xC0, 0x83, 0xA7, 0xD6, 0x26, 0x7A, 0x3E, 0xD7,
  27659. 0xA7, 0xFD, 0x47, 0x95, 0x7C, 0x21, 0x93, 0x27,
  27660. 0xF7, 0xCA, 0x73, 0xA4, 0x00, 0x7E, 0x16, 0x27,
  27661. 0xF0, 0x0B, 0x11, 0xCC, 0x80, 0x57, 0x3C, 0x15,
  27662. 0xAE, 0xE6, 0x64, 0x0F, 0xB8, 0x56, 0x2D, 0xFA,
  27663. 0x6B, 0x24, 0x0C, 0xA0, 0xAD, 0x35, 0x1A, 0xC4,
  27664. 0xAC, 0x15, 0x5B, 0x96, 0xC1, 0x4C, 0x8A, 0xB1,
  27665. 0x3D, 0xD2, 0x62, 0xCD, 0xFD, 0x51, 0xC4, 0xBB,
  27666. 0x55, 0x72, 0xFD, 0x61, 0x65, 0x53, 0xD1, 0x7B,
  27667. 0xDD, 0x43, 0x0A, 0xCB, 0xEA, 0x3E, 0x95, 0xF0,
  27668. 0xB6, 0x98, 0xD6, 0x69, 0x90, 0xAB, 0x51, 0xE5,
  27669. 0xD0, 0x37, 0x83, 0xA8, 0xB3, 0xD2, 0x78, 0xA5,
  27670. 0x72, 0x04, 0x54, 0xCF, 0x96, 0x95, 0xCF, 0xDC,
  27671. 0xA0, 0x84, 0x85, 0xBA, 0x09, 0x9C, 0x51, 0xCD,
  27672. 0x92, 0xA7, 0xEA, 0x75, 0x87, 0xC1, 0xD1, 0x5C,
  27673. 0x28, 0xE6, 0x09, 0xA8, 0x18, 0x52, 0x60, 0x1B,
  27674. 0x06, 0x04, 0x01, 0x06, 0x79, 0xAA, 0x48, 0x2D,
  27675. 0x51, 0x26, 0x1E, 0xC3, 0x6E, 0x36, 0xB8, 0x71,
  27676. 0x96, 0x76, 0x21, 0x7F, 0xD7, 0x4C, 0x54, 0x78,
  27677. 0x64, 0x88, 0xF4, 0xB4, 0x96, 0x9C, 0x05, 0xA8,
  27678. 0xBA, 0x27, 0xCA, 0x3A, 0x77, 0xCC, 0xE7, 0x3B,
  27679. 0x96, 0x59, 0x23, 0xCA, 0x55, 0x4E, 0x42, 0x2B,
  27680. 0x9B, 0x61, 0xF4, 0x75, 0x46, 0x41, 0x60, 0x8A,
  27681. 0xC1, 0x6C, 0x9B, 0x85, 0x87, 0xA3, 0x2C, 0x1C,
  27682. 0x5D, 0xD7, 0x88, 0xF8, 0x8B, 0x36, 0xB7, 0x17,
  27683. 0xA4, 0x69, 0x65, 0x63, 0x5D, 0xEB, 0x67, 0xF4,
  27684. 0x5B, 0x12, 0x9B, 0x99, 0x07, 0x09, 0x09, 0xC9,
  27685. 0x3E, 0xB8, 0x0B, 0x42, 0xC2, 0xB3, 0xF3, 0xF7,
  27686. 0x03, 0x43, 0xA7, 0xCF, 0x37, 0xE8, 0x52, 0x0E,
  27687. 0x7B, 0xCF, 0xC4, 0x16, 0xAC, 0xA4, 0xF1, 0x8C,
  27688. 0x79, 0x81, 0x26, 0x2B, 0xA2, 0xBF, 0xC7, 0x56,
  27689. 0xAE, 0x03, 0x27, 0x8F, 0x0E, 0xC6, 0x6D, 0xC2,
  27690. 0x05, 0x76, 0x96, 0x82, 0x4B, 0xA6, 0x76, 0x98,
  27691. 0x65, 0xA6, 0x01, 0xD7, 0x14, 0x8E, 0xF6, 0xF5,
  27692. 0x4E, 0x5A, 0xF5, 0x68, 0x6A, 0xA2, 0x90, 0x6F,
  27693. 0x99, 0x4C, 0xE3, 0x8A, 0x5E, 0x0B, 0x93, 0x8F,
  27694. 0x23, 0x90, 0x07, 0x00, 0x30, 0x22, 0xC0, 0x33,
  27695. 0x92, 0xDF, 0x34, 0x01, 0xB1, 0xE4, 0xA3, 0xA7,
  27696. 0xEB, 0xC6, 0x16, 0x14, 0x49, 0xF7, 0x33, 0x74,
  27697. 0xC8, 0xB0, 0x14, 0x03, 0x69, 0x34, 0x3D, 0x92,
  27698. 0x95, 0xFD, 0xF5, 0x11, 0x84, 0x5C, 0x4A, 0x46,
  27699. 0xEB, 0xAA, 0xB6, 0xCA, 0x54, 0x92, 0xF6, 0x80,
  27700. 0x0B, 0x98, 0xC0, 0xCC, 0x80, 0x36, 0x53, 0xA4,
  27701. 0xB1, 0xD6, 0xE6, 0xAA, 0xED, 0x19, 0x32, 0xBA,
  27702. 0xCC, 0x5F, 0xEF, 0xAA, 0x81, 0x8B, 0xA5, 0x02,
  27703. 0x85, 0x9B, 0xA5, 0x49, 0x4C, 0x5F, 0x54, 0x02,
  27704. 0xC8, 0x53, 0x6A, 0x9C, 0x4C, 0x18, 0x88, 0x15,
  27705. 0x06, 0x17, 0xF8, 0x00, 0x98, 0xF6, 0xB2, 0xA9,
  27706. 0x9C, 0x39, 0xBC, 0x5D, 0xC7, 0xCF, 0x3B, 0x59,
  27707. 0x00, 0xA2, 0x13, 0x29, 0xAB, 0x59, 0x05, 0x3A,
  27708. 0xBA, 0xA6, 0x4E, 0xD1, 0x63, 0xE8, 0x59, 0xA8,
  27709. 0xB3, 0xB3, 0xCA, 0x33, 0x59, 0xB7, 0x50, 0xCC,
  27710. 0xC3, 0xE7, 0x10, 0xC7, 0xAC, 0x43, 0xC8, 0x19,
  27711. 0x1C, 0xB5, 0xD6, 0x88, 0x70, 0xC0, 0x63, 0x91,
  27712. 0xC0, 0xCB, 0x8A, 0xEC, 0x72, 0xB8, 0x97, 0xAC,
  27713. 0x6B, 0xE7, 0xFB, 0xAA, 0xCC, 0x67, 0x6E, 0xD6,
  27714. 0x63, 0x14, 0xC8, 0x36, 0x30, 0xE8, 0x94, 0x48,
  27715. 0xC8, 0x8A, 0x1D, 0xF0, 0x4A, 0xCE, 0xB2, 0x3A,
  27716. 0xBF, 0x2E, 0x40, 0x9E, 0xF3, 0x33, 0xC6, 0x22,
  27717. 0x28, 0x9C, 0x18, 0xA2, 0x13, 0x4E, 0x65, 0x0C,
  27718. 0x45, 0x25, 0x7E, 0x47, 0x47, 0x5F, 0xA3, 0x3A,
  27719. 0xA5, 0x37, 0xA5, 0xA8, 0xF7, 0x68, 0x02, 0x14,
  27720. 0x71, 0x6C, 0x50, 0xD4, 0x70, 0xE3, 0x28, 0x49,
  27721. 0x63, 0xCA, 0x64, 0xF5, 0x46, 0x77, 0xAE, 0xC5,
  27722. 0x4B, 0x52, 0x72, 0x16, 0x2B, 0xF5, 0x2B, 0xC8,
  27723. 0x14, 0x2E, 0x1D, 0x41, 0x83, 0xFC, 0x01, 0x74,
  27724. 0x54, 0xA6, 0xB5, 0xA4, 0x96, 0x83, 0x17, 0x59,
  27725. 0x06, 0x40, 0x24, 0x74, 0x59, 0x78, 0xCB, 0xD5,
  27726. 0x1A, 0x6C, 0xED, 0xC8, 0x95, 0x5D, 0xE4, 0xCC,
  27727. 0x6D, 0x36, 0x36, 0x70, 0xA4, 0x74, 0x66, 0xE8,
  27728. 0x2B, 0xE5, 0xC2, 0x36, 0x03, 0xA1, 0x7B, 0xF2,
  27729. 0x2A, 0xCD, 0xB7, 0xCC, 0x98, 0x4A, 0xF0, 0x8C,
  27730. 0x87, 0xE1, 0x4E, 0x27, 0x75, 0x3C, 0xF5, 0x87,
  27731. 0xA8, 0xEC, 0x34, 0x47, 0xE6, 0x2C, 0x64, 0x9E,
  27732. 0x88, 0x7A, 0x67, 0xC3, 0x6C, 0x9C, 0xE9, 0x87,
  27733. 0x21, 0xB6, 0x97, 0x21, 0x32, 0x75, 0x64, 0x6B,
  27734. 0x19, 0x4F, 0x36, 0x75, 0x86, 0x73, 0xA8, 0xED,
  27735. 0x11, 0x28, 0x44, 0x55, 0xAF, 0xC7, 0xA8, 0x52,
  27736. 0x9F, 0x69, 0xC9, 0x7A, 0x3C, 0x2D, 0x7B, 0x8C,
  27737. 0x63, 0x6C, 0x0B, 0xA5, 0x56, 0x14, 0xB7, 0x68,
  27738. 0xE6, 0x24, 0xE7, 0x12, 0x93, 0x0F, 0x77, 0x61,
  27739. 0x69, 0xB0, 0x17, 0x15, 0x72, 0x53, 0x51, 0xBC,
  27740. 0x74, 0xB4, 0x73, 0x95, 0xED, 0x52, 0xB2, 0x5A,
  27741. 0x13, 0x13, 0xC9, 0x51, 0x64, 0x81, 0x4C, 0x34,
  27742. 0xC9, 0x79, 0xCB, 0xDF, 0xAB, 0x85, 0x95, 0x46,
  27743. 0x62, 0xCA, 0xB4, 0x85, 0xE7, 0x50, 0x87, 0xA9,
  27744. 0x8C, 0xC7, 0x4B, 0xB8, 0x2C, 0xA2, 0xD1, 0xB5,
  27745. 0xBF, 0x28, 0x03, 0x23, 0x84, 0x80, 0x63, 0x8C,
  27746. 0x40, 0xE9, 0x0B, 0x43, 0xC7, 0x46, 0x0E, 0x7A,
  27747. 0xA9, 0x17, 0xF0, 0x10, 0x15, 0x1F, 0xAB, 0x11,
  27748. 0x69, 0x98, 0x7B, 0x37, 0x2A, 0xBB, 0x59, 0x27,
  27749. 0x1F, 0x70, 0x06, 0xC2, 0x4E, 0x60, 0x23, 0x6B,
  27750. 0x84, 0xB9, 0xDD, 0xD6, 0x00, 0x62, 0x37, 0x04,
  27751. 0x25, 0x46, 0x17, 0xFB, 0x49, 0x8D, 0x89, 0xE5,
  27752. 0x8B, 0x03, 0x68, 0xBC, 0xB2, 0x10, 0x3E, 0x79,
  27753. 0x35, 0x3E, 0xB5, 0x87, 0x86, 0x0C, 0x14, 0x22,
  27754. 0xE4, 0x76, 0x16, 0x2E, 0x42, 0x5B, 0xC2, 0x38,
  27755. 0x1D, 0xB8, 0x2C, 0x65, 0x92, 0x73, 0x7E, 0x1D,
  27756. 0xD6, 0x02, 0x86, 0x4B, 0x01, 0x67, 0xA7, 0x1E,
  27757. 0xC1, 0xF2, 0x23, 0x30, 0x5C, 0x02, 0xFE, 0x25,
  27758. 0x05, 0x2A, 0xF2, 0xB3, 0xB5, 0xA5, 0x5A, 0x0D,
  27759. 0x7A, 0x20, 0x22, 0xD9, 0xA7, 0x98, 0xDC, 0x0C,
  27760. 0x58, 0x74, 0xA9, 0x87, 0x02, 0xAA, 0xF4, 0x05,
  27761. 0x4C, 0x5D, 0x80, 0x33, 0x8A, 0x52, 0x48, 0xB5,
  27762. 0xB7, 0xBD, 0x09, 0xC5, 0x3B, 0x5E, 0x2A, 0x08,
  27763. 0x4B, 0x04, 0x7D, 0x27, 0x7A, 0x86, 0x1B, 0x1A,
  27764. 0x73, 0xBB, 0x51, 0x48, 0x8D, 0xE0, 0x4E, 0xF5,
  27765. 0x73, 0xC8, 0x52, 0x30, 0xA0, 0x47, 0x0B, 0x73,
  27766. 0x17, 0x5C, 0x9F, 0xA5, 0x05, 0x94, 0xF6, 0x6A,
  27767. 0x5F, 0x50, 0xB4, 0x15, 0x00, 0x54, 0xC9, 0x3B,
  27768. 0x68, 0x18, 0x6F, 0x8B, 0x5C, 0xBC, 0x49, 0x31,
  27769. 0x6C, 0x85, 0x48, 0xA6, 0x42, 0xB2, 0xB3, 0x6A,
  27770. 0x1D, 0x45, 0x4C, 0x74, 0x89, 0xAC, 0x33, 0xB2,
  27771. 0xD2, 0xCE, 0x66, 0x68, 0x09, 0x67, 0x82, 0xA2,
  27772. 0xC1, 0xE0, 0x86, 0x6D, 0x21, 0xA6, 0x5E, 0x16,
  27773. 0xB5, 0x85, 0xE7, 0xAF, 0x86, 0x18, 0xBD, 0xF3,
  27774. 0x18, 0x4C, 0x19, 0x86, 0x87, 0x85, 0x08, 0x91,
  27775. 0x72, 0x77, 0xB9, 0x3E, 0x10, 0x70, 0x6B, 0x16,
  27776. 0x14, 0x97, 0x2B, 0x2A, 0x94, 0xC7, 0x31, 0x0F,
  27777. 0xE9, 0xC7, 0x08, 0xC2, 0x31, 0xA1, 0xA8, 0xAC,
  27778. 0x8D, 0x93, 0x14, 0xA5, 0x29, 0xA9, 0x7F, 0x46,
  27779. 0x9B, 0xF6, 0x49, 0x62, 0xD8, 0x20, 0x64, 0x84,
  27780. 0x43, 0x09, 0x9A, 0x07, 0x6D, 0x55, 0xD4, 0xCE,
  27781. 0xA8, 0x24, 0xA5, 0x83, 0x04, 0x84, 0x4F, 0x99,
  27782. 0x49, 0x7C, 0x10, 0xA2, 0x51, 0x48, 0x61, 0x8A,
  27783. 0x31, 0x5D, 0x72, 0xCA, 0x85, 0x7D, 0x1B, 0x04,
  27784. 0xD5, 0x75, 0xB9, 0x4F, 0x85, 0xC0, 0x1D, 0x19,
  27785. 0xBE, 0xF2, 0x11, 0xBF, 0x0A, 0xA3, 0x36, 0x2E,
  27786. 0x70, 0x41, 0xFD, 0x16, 0x59, 0x6D, 0x80, 0x8E,
  27787. 0x86, 0x7B, 0x44, 0xC4, 0xC0, 0x0D, 0x1C, 0xDA,
  27788. 0x34, 0x18, 0x96, 0x77, 0x17, 0xF1, 0x47, 0xD0,
  27789. 0xEB, 0x21, 0xB4, 0x2A, 0xAE, 0xE7, 0x4A, 0xC3,
  27790. 0x5D, 0x0B, 0x92, 0x41, 0x4B, 0x95, 0x85, 0x31,
  27791. 0xAA, 0xDF, 0x46, 0x3E, 0xC6, 0x30, 0x5A, 0xE5,
  27792. 0xEC, 0xAF, 0x79, 0x17, 0x40, 0x02, 0xF2, 0x6D,
  27793. 0xDE, 0xCC, 0x81, 0x3B, 0xF3, 0x26, 0x72, 0xE8,
  27794. 0x52, 0x9D, 0x95, 0xA4, 0xE7, 0x30, 0xA7, 0xAB,
  27795. 0x4A, 0x3E, 0x8F, 0x8A, 0x8A, 0xF9, 0x79, 0xA6,
  27796. 0x65, 0xEA, 0xFD, 0x46, 0x5F, 0xC6, 0x4A, 0x0C,
  27797. 0x5F, 0x8F, 0x3F, 0x90, 0x03, 0x48, 0x94, 0x15,
  27798. 0x89, 0x9D, 0x59, 0xA5, 0x43, 0xD8, 0x20, 0x8C,
  27799. 0x54, 0xA3, 0x16, 0x65, 0x29, 0xB5, 0x39, 0x22
  27800. };
  27801. const byte kyber768_sk[] = {
  27802. 0x07, 0x63, 0x8F, 0xB6, 0x98, 0x68, 0xF3, 0xD3,
  27803. 0x20, 0xE5, 0x86, 0x2B, 0xD9, 0x69, 0x33, 0xFE,
  27804. 0xB3, 0x11, 0xB3, 0x62, 0x09, 0x3C, 0x9B, 0x5D,
  27805. 0x50, 0x17, 0x0B, 0xCE, 0xD4, 0x3F, 0x1B, 0x53,
  27806. 0x6D, 0x9A, 0x20, 0x4B, 0xB1, 0xF2, 0x26, 0x95,
  27807. 0x95, 0x0B, 0xA1, 0xF2, 0xA9, 0xE8, 0xEB, 0x82,
  27808. 0x8B, 0x28, 0x44, 0x88, 0x76, 0x0B, 0x3F, 0xC8,
  27809. 0x4F, 0xAB, 0xA0, 0x42, 0x75, 0xD5, 0x62, 0x8E,
  27810. 0x39, 0xC5, 0xB2, 0x47, 0x13, 0x74, 0x28, 0x3C,
  27811. 0x50, 0x32, 0x99, 0xC0, 0xAB, 0x49, 0xB6, 0x6B,
  27812. 0x8B, 0xBB, 0x56, 0xA4, 0x18, 0x66, 0x24, 0xF9,
  27813. 0x19, 0xA2, 0xBA, 0x59, 0xBB, 0x08, 0xD8, 0x55,
  27814. 0x18, 0x80, 0xC2, 0xBE, 0xFC, 0x4F, 0x87, 0xF2,
  27815. 0x5F, 0x59, 0xAB, 0x58, 0x7A, 0x79, 0xC3, 0x27,
  27816. 0xD7, 0x92, 0xD5, 0x4C, 0x97, 0x4A, 0x69, 0x26,
  27817. 0x2F, 0xF8, 0xA7, 0x89, 0x38, 0x28, 0x9E, 0x9A,
  27818. 0x87, 0xB6, 0x88, 0xB0, 0x83, 0xE0, 0x59, 0x5F,
  27819. 0xE2, 0x18, 0xB6, 0xBB, 0x15, 0x05, 0x94, 0x1C,
  27820. 0xE2, 0xE8, 0x1A, 0x5A, 0x64, 0xC5, 0xAA, 0xC6,
  27821. 0x04, 0x17, 0x25, 0x69, 0x85, 0x34, 0x9E, 0xE4,
  27822. 0x7A, 0x52, 0x42, 0x0A, 0x5F, 0x97, 0x47, 0x7B,
  27823. 0x72, 0x36, 0xAC, 0x76, 0xBC, 0x70, 0xE8, 0x28,
  27824. 0x87, 0x29, 0x28, 0x7E, 0xE3, 0xE3, 0x4A, 0x3D,
  27825. 0xBC, 0x36, 0x83, 0xC0, 0xB7, 0xB1, 0x00, 0x29,
  27826. 0xFC, 0x20, 0x34, 0x18, 0x53, 0x7E, 0x74, 0x66,
  27827. 0xBA, 0x63, 0x85, 0xA8, 0xFF, 0x30, 0x1E, 0xE1,
  27828. 0x27, 0x08, 0xF8, 0x2A, 0xAA, 0x1E, 0x38, 0x0F,
  27829. 0xC7, 0xA8, 0x8F, 0x8F, 0x20, 0x5A, 0xB7, 0xE8,
  27830. 0x8D, 0x7E, 0x95, 0x95, 0x2A, 0x55, 0xBA, 0x20,
  27831. 0xD0, 0x9B, 0x79, 0xA4, 0x71, 0x41, 0xD6, 0x2B,
  27832. 0xF6, 0xEB, 0x7D, 0xD3, 0x07, 0xB0, 0x8E, 0xCA,
  27833. 0x13, 0xA5, 0xBC, 0x5F, 0x6B, 0x68, 0x58, 0x1C,
  27834. 0x68, 0x65, 0xB2, 0x7B, 0xBC, 0xDD, 0xAB, 0x14,
  27835. 0x2F, 0x4B, 0x2C, 0xBF, 0xF4, 0x88, 0xC8, 0xA2,
  27836. 0x27, 0x05, 0xFA, 0xA9, 0x8A, 0x2B, 0x9E, 0xEA,
  27837. 0x35, 0x30, 0xC7, 0x66, 0x62, 0x33, 0x5C, 0xC7,
  27838. 0xEA, 0x3A, 0x00, 0x77, 0x77, 0x25, 0xEB, 0xCC,
  27839. 0xCD, 0x2A, 0x46, 0x36, 0xB2, 0xD9, 0x12, 0x2F,
  27840. 0xF3, 0xAB, 0x77, 0x12, 0x3C, 0xE0, 0x88, 0x3C,
  27841. 0x19, 0x11, 0x11, 0x5E, 0x50, 0xC9, 0xE8, 0xA9,
  27842. 0x41, 0x94, 0xE4, 0x8D, 0xD0, 0xD0, 0x9C, 0xFF,
  27843. 0xB3, 0xAD, 0xCD, 0x2C, 0x1E, 0x92, 0x43, 0x09,
  27844. 0x03, 0xD0, 0x7A, 0xDB, 0xF0, 0x05, 0x32, 0x03,
  27845. 0x15, 0x75, 0xAA, 0x7F, 0x9E, 0x7B, 0x5A, 0x1F,
  27846. 0x33, 0x62, 0xDE, 0xC9, 0x36, 0xD4, 0x04, 0x3C,
  27847. 0x05, 0xF2, 0x47, 0x6C, 0x07, 0x57, 0x8B, 0xC9,
  27848. 0xCB, 0xAF, 0x2A, 0xB4, 0xE3, 0x82, 0x72, 0x7A,
  27849. 0xD4, 0x16, 0x86, 0xA9, 0x6B, 0x25, 0x48, 0x82,
  27850. 0x0B, 0xB0, 0x3B, 0x32, 0xF1, 0x1B, 0x28, 0x11,
  27851. 0xAD, 0x62, 0xF4, 0x89, 0xE9, 0x51, 0x63, 0x2A,
  27852. 0xBA, 0x0D, 0x1D, 0xF8, 0x96, 0x80, 0xCC, 0x8A,
  27853. 0x8B, 0x53, 0xB4, 0x81, 0xD9, 0x2A, 0x68, 0xD7,
  27854. 0x0B, 0x4E, 0xA1, 0xC3, 0xA6, 0xA5, 0x61, 0xC0,
  27855. 0x69, 0x28, 0x82, 0xB5, 0xCA, 0x8C, 0xC9, 0x42,
  27856. 0xA8, 0xD4, 0x95, 0xAF, 0xCB, 0x06, 0xDE, 0x89,
  27857. 0x49, 0x8F, 0xB9, 0x35, 0xB7, 0x75, 0x90, 0x8F,
  27858. 0xE7, 0xA0, 0x3E, 0x32, 0x4D, 0x54, 0xCC, 0x19,
  27859. 0xD4, 0xE1, 0xAA, 0xBD, 0x35, 0x93, 0xB3, 0x8B,
  27860. 0x19, 0xEE, 0x13, 0x88, 0xFE, 0x49, 0x2B, 0x43,
  27861. 0x12, 0x7E, 0x5A, 0x50, 0x42, 0x53, 0x78, 0x6A,
  27862. 0x0D, 0x69, 0xAD, 0x32, 0x60, 0x1C, 0x28, 0xE2,
  27863. 0xC8, 0x85, 0x04, 0xA5, 0xBA, 0x59, 0x97, 0x06,
  27864. 0x02, 0x3A, 0x61, 0x36, 0x3E, 0x17, 0xC6, 0xB9,
  27865. 0xBB, 0x59, 0xBD, 0xC6, 0x97, 0x45, 0x2C, 0xD0,
  27866. 0x59, 0x45, 0x19, 0x83, 0xD7, 0x38, 0xCA, 0x3F,
  27867. 0xD0, 0x34, 0xE3, 0xF5, 0x98, 0x88, 0x54, 0xCA,
  27868. 0x05, 0x03, 0x1D, 0xB0, 0x96, 0x11, 0x49, 0x89,
  27869. 0x88, 0x19, 0x7C, 0x6B, 0x30, 0xD2, 0x58, 0xDF,
  27870. 0xE2, 0x62, 0x65, 0x54, 0x1C, 0x89, 0xA4, 0xB3,
  27871. 0x1D, 0x68, 0x64, 0xE9, 0x38, 0x9B, 0x03, 0xCB,
  27872. 0x74, 0xF7, 0xEC, 0x43, 0x23, 0xFB, 0x94, 0x21,
  27873. 0xA4, 0xB9, 0x79, 0x0A, 0x26, 0xD1, 0x7B, 0x03,
  27874. 0x98, 0xA2, 0x67, 0x67, 0x35, 0x09, 0x09, 0xF8,
  27875. 0x4D, 0x57, 0xB6, 0x69, 0x4D, 0xF8, 0x30, 0x66,
  27876. 0x4C, 0xA8, 0xB3, 0xC3, 0xC0, 0x3E, 0xD2, 0xAE,
  27877. 0x67, 0xB8, 0x90, 0x06, 0x86, 0x8A, 0x68, 0x52,
  27878. 0x7C, 0xCD, 0x66, 0x64, 0x59, 0xAB, 0x7F, 0x05,
  27879. 0x66, 0x71, 0x00, 0x0C, 0x61, 0x64, 0xD3, 0xA7,
  27880. 0xF2, 0x66, 0xA1, 0x4D, 0x97, 0xCB, 0xD7, 0x00,
  27881. 0x4D, 0x6C, 0x92, 0xCA, 0xCA, 0x77, 0x0B, 0x84,
  27882. 0x4A, 0x4F, 0xA9, 0xB1, 0x82, 0xE7, 0xB1, 0x8C,
  27883. 0xA8, 0x85, 0x08, 0x2A, 0xC5, 0x64, 0x6F, 0xCB,
  27884. 0x4A, 0x14, 0xE1, 0x68, 0x5F, 0xEB, 0x0C, 0x9C,
  27885. 0xE3, 0x37, 0x2A, 0xB9, 0x53, 0x65, 0xC0, 0x4F,
  27886. 0xD8, 0x30, 0x84, 0xF8, 0x0A, 0x23, 0xFF, 0x10,
  27887. 0xA0, 0x5B, 0xF1, 0x5F, 0x7F, 0xA5, 0xAC, 0xC6,
  27888. 0xC0, 0xCB, 0x46, 0x2C, 0x33, 0xCA, 0x52, 0x4F,
  27889. 0xA6, 0xB8, 0xBB, 0x35, 0x90, 0x43, 0xBA, 0x68,
  27890. 0x60, 0x9E, 0xAA, 0x25, 0x36, 0xE8, 0x1D, 0x08,
  27891. 0x46, 0x3B, 0x19, 0x65, 0x3B, 0x54, 0x35, 0xBA,
  27892. 0x94, 0x6C, 0x9A, 0xDD, 0xEB, 0x20, 0x2B, 0x04,
  27893. 0xB0, 0x31, 0xCC, 0x96, 0x0D, 0xCC, 0x12, 0xE4,
  27894. 0x51, 0x8D, 0x42, 0x8B, 0x32, 0xB2, 0x57, 0xA4,
  27895. 0xFC, 0x73, 0x13, 0xD3, 0xA7, 0x98, 0x0D, 0x80,
  27896. 0x08, 0x2E, 0x93, 0x4F, 0x9D, 0x95, 0xC3, 0x2B,
  27897. 0x0A, 0x01, 0x91, 0xA2, 0x36, 0x04, 0x38, 0x4D,
  27898. 0xD9, 0xE0, 0x79, 0xBB, 0xBA, 0xA2, 0x66, 0xD1,
  27899. 0x4C, 0x3F, 0x75, 0x6B, 0x9F, 0x21, 0x33, 0x10,
  27900. 0x74, 0x33, 0xA4, 0xE8, 0x3F, 0xA7, 0x18, 0x72,
  27901. 0x82, 0xA8, 0x09, 0x20, 0x3A, 0x4F, 0xAF, 0x84,
  27902. 0x18, 0x51, 0x83, 0x3D, 0x12, 0x1A, 0xC3, 0x83,
  27903. 0x84, 0x3A, 0x5E, 0x55, 0xBC, 0x23, 0x81, 0x42,
  27904. 0x5E, 0x16, 0xC7, 0xDB, 0x4C, 0xC9, 0xAB, 0x5C,
  27905. 0x1B, 0x0D, 0x91, 0xA4, 0x7E, 0x2B, 0x8D, 0xE0,
  27906. 0xE5, 0x82, 0xC8, 0x6B, 0x6B, 0x0D, 0x90, 0x7B,
  27907. 0xB3, 0x60, 0xB9, 0x7F, 0x40, 0xAB, 0x5D, 0x03,
  27908. 0x8F, 0x6B, 0x75, 0xC8, 0x14, 0xB2, 0x7D, 0x9B,
  27909. 0x96, 0x8D, 0x41, 0x98, 0x32, 0xBC, 0x8C, 0x2B,
  27910. 0xEE, 0x60, 0x5E, 0xF6, 0xE5, 0x05, 0x9D, 0x33,
  27911. 0x10, 0x0D, 0x90, 0x48, 0x5D, 0x37, 0x84, 0x50,
  27912. 0x01, 0x42, 0x21, 0x73, 0x6C, 0x07, 0x40, 0x7C,
  27913. 0xAC, 0x26, 0x04, 0x08, 0xAA, 0x64, 0x92, 0x66,
  27914. 0x19, 0x78, 0x8B, 0x86, 0x01, 0xC2, 0xA7, 0x52,
  27915. 0xD1, 0xA6, 0xCB, 0xF8, 0x20, 0xD7, 0xC7, 0xA0,
  27916. 0x47, 0x16, 0x20, 0x32, 0x25, 0xB3, 0x89, 0x5B,
  27917. 0x93, 0x42, 0xD1, 0x47, 0xA8, 0x18, 0x5C, 0xFC,
  27918. 0x1B, 0xB6, 0x5B, 0xA0, 0x6B, 0x41, 0x42, 0x33,
  27919. 0x99, 0x03, 0xC0, 0xAC, 0x46, 0x51, 0x38, 0x5B,
  27920. 0x45, 0xD9, 0x8A, 0x8B, 0x19, 0xD2, 0x8C, 0xD6,
  27921. 0xBA, 0xB0, 0x88, 0x78, 0x7F, 0x7E, 0xE1, 0xB1,
  27922. 0x24, 0x61, 0x76, 0x6B, 0x43, 0xCB, 0xCC, 0xB9,
  27923. 0x64, 0x34, 0x42, 0x7D, 0x93, 0xC0, 0x65, 0x55,
  27924. 0x06, 0x88, 0xF6, 0x94, 0x8E, 0xD1, 0xB5, 0x47,
  27925. 0x5A, 0x42, 0x5F, 0x1B, 0x85, 0x20, 0x9D, 0x06,
  27926. 0x1C, 0x08, 0xB5, 0x6C, 0x1C, 0xC0, 0x69, 0xF6,
  27927. 0xC0, 0xA7, 0xC6, 0xF2, 0x93, 0x58, 0xCA, 0xB9,
  27928. 0x11, 0x08, 0x77, 0x32, 0xA6, 0x49, 0xD2, 0x7C,
  27929. 0x9B, 0x98, 0xF9, 0xA4, 0x88, 0x79, 0x38, 0x7D,
  27930. 0x9B, 0x00, 0xC2, 0x59, 0x59, 0xA7, 0x16, 0x54,
  27931. 0xD6, 0xF6, 0xA9, 0x46, 0x16, 0x45, 0x13, 0xE4,
  27932. 0x7A, 0x75, 0xD0, 0x05, 0x98, 0x6C, 0x23, 0x63,
  27933. 0xC0, 0x9F, 0x6B, 0x53, 0x7E, 0xCA, 0x78, 0xB9,
  27934. 0x30, 0x3A, 0x5F, 0xA4, 0x57, 0x60, 0x8A, 0x58,
  27935. 0x6A, 0x65, 0x3A, 0x34, 0x7D, 0xB0, 0x4D, 0xFC,
  27936. 0xC1, 0x91, 0x75, 0xB3, 0xA3, 0x01, 0x17, 0x25,
  27937. 0x36, 0x06, 0x2A, 0x65, 0x8A, 0x95, 0x27, 0x75,
  27938. 0x70, 0xC8, 0x85, 0x2C, 0xA8, 0x97, 0x3F, 0x4A,
  27939. 0xE1, 0x23, 0xA3, 0x34, 0x04, 0x7D, 0xD7, 0x11,
  27940. 0xC8, 0x92, 0x7A, 0x63, 0x4A, 0x03, 0x38, 0x8A,
  27941. 0x52, 0x7B, 0x03, 0x4B, 0xF7, 0xA8, 0x17, 0x0F,
  27942. 0xA7, 0x02, 0xC1, 0xF7, 0xC2, 0x3E, 0xC3, 0x2D,
  27943. 0x18, 0xA2, 0x37, 0x48, 0x90, 0xBE, 0x9C, 0x78,
  27944. 0x7A, 0x94, 0x09, 0xC8, 0x2D, 0x19, 0x2C, 0x4B,
  27945. 0xB7, 0x05, 0xA2, 0xF9, 0x96, 0xCE, 0x40, 0x5D,
  27946. 0xA7, 0x2C, 0x2D, 0x9C, 0x84, 0x3E, 0xE9, 0xF8,
  27947. 0x31, 0x3E, 0xCC, 0x7F, 0x86, 0xD6, 0x29, 0x4D,
  27948. 0x59, 0x15, 0x9D, 0x9A, 0x87, 0x9A, 0x54, 0x2E,
  27949. 0x26, 0x09, 0x22, 0xAD, 0xF9, 0x99, 0x05, 0x1C,
  27950. 0xC4, 0x52, 0x00, 0xC9, 0xFF, 0xDB, 0x60, 0x44,
  27951. 0x9C, 0x49, 0x46, 0x59, 0x79, 0x27, 0x23, 0x67,
  27952. 0xC0, 0x83, 0xA7, 0xD6, 0x26, 0x7A, 0x3E, 0xD7,
  27953. 0xA7, 0xFD, 0x47, 0x95, 0x7C, 0x21, 0x93, 0x27,
  27954. 0xF7, 0xCA, 0x73, 0xA4, 0x00, 0x7E, 0x16, 0x27,
  27955. 0xF0, 0x0B, 0x11, 0xCC, 0x80, 0x57, 0x3C, 0x15,
  27956. 0xAE, 0xE6, 0x64, 0x0F, 0xB8, 0x56, 0x2D, 0xFA,
  27957. 0x6B, 0x24, 0x0C, 0xA0, 0xAD, 0x35, 0x1A, 0xC4,
  27958. 0xAC, 0x15, 0x5B, 0x96, 0xC1, 0x4C, 0x8A, 0xB1,
  27959. 0x3D, 0xD2, 0x62, 0xCD, 0xFD, 0x51, 0xC4, 0xBB,
  27960. 0x55, 0x72, 0xFD, 0x61, 0x65, 0x53, 0xD1, 0x7B,
  27961. 0xDD, 0x43, 0x0A, 0xCB, 0xEA, 0x3E, 0x95, 0xF0,
  27962. 0xB6, 0x98, 0xD6, 0x69, 0x90, 0xAB, 0x51, 0xE5,
  27963. 0xD0, 0x37, 0x83, 0xA8, 0xB3, 0xD2, 0x78, 0xA5,
  27964. 0x72, 0x04, 0x54, 0xCF, 0x96, 0x95, 0xCF, 0xDC,
  27965. 0xA0, 0x84, 0x85, 0xBA, 0x09, 0x9C, 0x51, 0xCD,
  27966. 0x92, 0xA7, 0xEA, 0x75, 0x87, 0xC1, 0xD1, 0x5C,
  27967. 0x28, 0xE6, 0x09, 0xA8, 0x18, 0x52, 0x60, 0x1B,
  27968. 0x06, 0x04, 0x01, 0x06, 0x79, 0xAA, 0x48, 0x2D,
  27969. 0x51, 0x26, 0x1E, 0xC3, 0x6E, 0x36, 0xB8, 0x71,
  27970. 0x96, 0x76, 0x21, 0x7F, 0xD7, 0x4C, 0x54, 0x78,
  27971. 0x64, 0x88, 0xF4, 0xB4, 0x96, 0x9C, 0x05, 0xA8,
  27972. 0xBA, 0x27, 0xCA, 0x3A, 0x77, 0xCC, 0xE7, 0x3B,
  27973. 0x96, 0x59, 0x23, 0xCA, 0x55, 0x4E, 0x42, 0x2B,
  27974. 0x9B, 0x61, 0xF4, 0x75, 0x46, 0x41, 0x60, 0x8A,
  27975. 0xC1, 0x6C, 0x9B, 0x85, 0x87, 0xA3, 0x2C, 0x1C,
  27976. 0x5D, 0xD7, 0x88, 0xF8, 0x8B, 0x36, 0xB7, 0x17,
  27977. 0xA4, 0x69, 0x65, 0x63, 0x5D, 0xEB, 0x67, 0xF4,
  27978. 0x5B, 0x12, 0x9B, 0x99, 0x07, 0x09, 0x09, 0xC9,
  27979. 0x3E, 0xB8, 0x0B, 0x42, 0xC2, 0xB3, 0xF3, 0xF7,
  27980. 0x03, 0x43, 0xA7, 0xCF, 0x37, 0xE8, 0x52, 0x0E,
  27981. 0x7B, 0xCF, 0xC4, 0x16, 0xAC, 0xA4, 0xF1, 0x8C,
  27982. 0x79, 0x81, 0x26, 0x2B, 0xA2, 0xBF, 0xC7, 0x56,
  27983. 0xAE, 0x03, 0x27, 0x8F, 0x0E, 0xC6, 0x6D, 0xC2,
  27984. 0x05, 0x76, 0x96, 0x82, 0x4B, 0xA6, 0x76, 0x98,
  27985. 0x65, 0xA6, 0x01, 0xD7, 0x14, 0x8E, 0xF6, 0xF5,
  27986. 0x4E, 0x5A, 0xF5, 0x68, 0x6A, 0xA2, 0x90, 0x6F,
  27987. 0x99, 0x4C, 0xE3, 0x8A, 0x5E, 0x0B, 0x93, 0x8F,
  27988. 0x23, 0x90, 0x07, 0x00, 0x30, 0x22, 0xC0, 0x33,
  27989. 0x92, 0xDF, 0x34, 0x01, 0xB1, 0xE4, 0xA3, 0xA7,
  27990. 0xEB, 0xC6, 0x16, 0x14, 0x49, 0xF7, 0x33, 0x74,
  27991. 0xC8, 0xB0, 0x14, 0x03, 0x69, 0x34, 0x3D, 0x92,
  27992. 0x95, 0xFD, 0xF5, 0x11, 0x84, 0x5C, 0x4A, 0x46,
  27993. 0xEB, 0xAA, 0xB6, 0xCA, 0x54, 0x92, 0xF6, 0x80,
  27994. 0x0B, 0x98, 0xC0, 0xCC, 0x80, 0x36, 0x53, 0xA4,
  27995. 0xB1, 0xD6, 0xE6, 0xAA, 0xED, 0x19, 0x32, 0xBA,
  27996. 0xCC, 0x5F, 0xEF, 0xAA, 0x81, 0x8B, 0xA5, 0x02,
  27997. 0x85, 0x9B, 0xA5, 0x49, 0x4C, 0x5F, 0x54, 0x02,
  27998. 0xC8, 0x53, 0x6A, 0x9C, 0x4C, 0x18, 0x88, 0x15,
  27999. 0x06, 0x17, 0xF8, 0x00, 0x98, 0xF6, 0xB2, 0xA9,
  28000. 0x9C, 0x39, 0xBC, 0x5D, 0xC7, 0xCF, 0x3B, 0x59,
  28001. 0x00, 0xA2, 0x13, 0x29, 0xAB, 0x59, 0x05, 0x3A,
  28002. 0xBA, 0xA6, 0x4E, 0xD1, 0x63, 0xE8, 0x59, 0xA8,
  28003. 0xB3, 0xB3, 0xCA, 0x33, 0x59, 0xB7, 0x50, 0xCC,
  28004. 0xC3, 0xE7, 0x10, 0xC7, 0xAC, 0x43, 0xC8, 0x19,
  28005. 0x1C, 0xB5, 0xD6, 0x88, 0x70, 0xC0, 0x63, 0x91,
  28006. 0xC0, 0xCB, 0x8A, 0xEC, 0x72, 0xB8, 0x97, 0xAC,
  28007. 0x6B, 0xE7, 0xFB, 0xAA, 0xCC, 0x67, 0x6E, 0xD6,
  28008. 0x63, 0x14, 0xC8, 0x36, 0x30, 0xE8, 0x94, 0x48,
  28009. 0xC8, 0x8A, 0x1D, 0xF0, 0x4A, 0xCE, 0xB2, 0x3A,
  28010. 0xBF, 0x2E, 0x40, 0x9E, 0xF3, 0x33, 0xC6, 0x22,
  28011. 0x28, 0x9C, 0x18, 0xA2, 0x13, 0x4E, 0x65, 0x0C,
  28012. 0x45, 0x25, 0x7E, 0x47, 0x47, 0x5F, 0xA3, 0x3A,
  28013. 0xA5, 0x37, 0xA5, 0xA8, 0xF7, 0x68, 0x02, 0x14,
  28014. 0x71, 0x6C, 0x50, 0xD4, 0x70, 0xE3, 0x28, 0x49,
  28015. 0x63, 0xCA, 0x64, 0xF5, 0x46, 0x77, 0xAE, 0xC5,
  28016. 0x4B, 0x52, 0x72, 0x16, 0x2B, 0xF5, 0x2B, 0xC8,
  28017. 0x14, 0x2E, 0x1D, 0x41, 0x83, 0xFC, 0x01, 0x74,
  28018. 0x54, 0xA6, 0xB5, 0xA4, 0x96, 0x83, 0x17, 0x59,
  28019. 0x06, 0x40, 0x24, 0x74, 0x59, 0x78, 0xCB, 0xD5,
  28020. 0x1A, 0x6C, 0xED, 0xC8, 0x95, 0x5D, 0xE4, 0xCC,
  28021. 0x6D, 0x36, 0x36, 0x70, 0xA4, 0x74, 0x66, 0xE8,
  28022. 0x2B, 0xE5, 0xC2, 0x36, 0x03, 0xA1, 0x7B, 0xF2,
  28023. 0x2A, 0xCD, 0xB7, 0xCC, 0x98, 0x4A, 0xF0, 0x8C,
  28024. 0x87, 0xE1, 0x4E, 0x27, 0x75, 0x3C, 0xF5, 0x87,
  28025. 0xA8, 0xEC, 0x34, 0x47, 0xE6, 0x2C, 0x64, 0x9E,
  28026. 0x88, 0x7A, 0x67, 0xC3, 0x6C, 0x9C, 0xE9, 0x87,
  28027. 0x21, 0xB6, 0x97, 0x21, 0x32, 0x75, 0x64, 0x6B,
  28028. 0x19, 0x4F, 0x36, 0x75, 0x86, 0x73, 0xA8, 0xED,
  28029. 0x11, 0x28, 0x44, 0x55, 0xAF, 0xC7, 0xA8, 0x52,
  28030. 0x9F, 0x69, 0xC9, 0x7A, 0x3C, 0x2D, 0x7B, 0x8C,
  28031. 0x63, 0x6C, 0x0B, 0xA5, 0x56, 0x14, 0xB7, 0x68,
  28032. 0xE6, 0x24, 0xE7, 0x12, 0x93, 0x0F, 0x77, 0x61,
  28033. 0x69, 0xB0, 0x17, 0x15, 0x72, 0x53, 0x51, 0xBC,
  28034. 0x74, 0xB4, 0x73, 0x95, 0xED, 0x52, 0xB2, 0x5A,
  28035. 0x13, 0x13, 0xC9, 0x51, 0x64, 0x81, 0x4C, 0x34,
  28036. 0xC9, 0x79, 0xCB, 0xDF, 0xAB, 0x85, 0x95, 0x46,
  28037. 0x62, 0xCA, 0xB4, 0x85, 0xE7, 0x50, 0x87, 0xA9,
  28038. 0x8C, 0xC7, 0x4B, 0xB8, 0x2C, 0xA2, 0xD1, 0xB5,
  28039. 0xBF, 0x28, 0x03, 0x23, 0x84, 0x80, 0x63, 0x8C,
  28040. 0x40, 0xE9, 0x0B, 0x43, 0xC7, 0x46, 0x0E, 0x7A,
  28041. 0xA9, 0x17, 0xF0, 0x10, 0x15, 0x1F, 0xAB, 0x11,
  28042. 0x69, 0x98, 0x7B, 0x37, 0x2A, 0xBB, 0x59, 0x27,
  28043. 0x1F, 0x70, 0x06, 0xC2, 0x4E, 0x60, 0x23, 0x6B,
  28044. 0x84, 0xB9, 0xDD, 0xD6, 0x00, 0x62, 0x37, 0x04,
  28045. 0x25, 0x46, 0x17, 0xFB, 0x49, 0x8D, 0x89, 0xE5,
  28046. 0x8B, 0x03, 0x68, 0xBC, 0xB2, 0x10, 0x3E, 0x79,
  28047. 0x35, 0x3E, 0xB5, 0x87, 0x86, 0x0C, 0x14, 0x22,
  28048. 0xE4, 0x76, 0x16, 0x2E, 0x42, 0x5B, 0xC2, 0x38,
  28049. 0x1D, 0xB8, 0x2C, 0x65, 0x92, 0x73, 0x7E, 0x1D,
  28050. 0xD6, 0x02, 0x86, 0x4B, 0x01, 0x67, 0xA7, 0x1E,
  28051. 0xC1, 0xF2, 0x23, 0x30, 0x5C, 0x02, 0xFE, 0x25,
  28052. 0x05, 0x2A, 0xF2, 0xB3, 0xB5, 0xA5, 0x5A, 0x0D,
  28053. 0x7A, 0x20, 0x22, 0xD9, 0xA7, 0x98, 0xDC, 0x0C,
  28054. 0x58, 0x74, 0xA9, 0x87, 0x02, 0xAA, 0xF4, 0x05,
  28055. 0x4C, 0x5D, 0x80, 0x33, 0x8A, 0x52, 0x48, 0xB5,
  28056. 0xB7, 0xBD, 0x09, 0xC5, 0x3B, 0x5E, 0x2A, 0x08,
  28057. 0x4B, 0x04, 0x7D, 0x27, 0x7A, 0x86, 0x1B, 0x1A,
  28058. 0x73, 0xBB, 0x51, 0x48, 0x8D, 0xE0, 0x4E, 0xF5,
  28059. 0x73, 0xC8, 0x52, 0x30, 0xA0, 0x47, 0x0B, 0x73,
  28060. 0x17, 0x5C, 0x9F, 0xA5, 0x05, 0x94, 0xF6, 0x6A,
  28061. 0x5F, 0x50, 0xB4, 0x15, 0x00, 0x54, 0xC9, 0x3B,
  28062. 0x68, 0x18, 0x6F, 0x8B, 0x5C, 0xBC, 0x49, 0x31,
  28063. 0x6C, 0x85, 0x48, 0xA6, 0x42, 0xB2, 0xB3, 0x6A,
  28064. 0x1D, 0x45, 0x4C, 0x74, 0x89, 0xAC, 0x33, 0xB2,
  28065. 0xD2, 0xCE, 0x66, 0x68, 0x09, 0x67, 0x82, 0xA2,
  28066. 0xC1, 0xE0, 0x86, 0x6D, 0x21, 0xA6, 0x5E, 0x16,
  28067. 0xB5, 0x85, 0xE7, 0xAF, 0x86, 0x18, 0xBD, 0xF3,
  28068. 0x18, 0x4C, 0x19, 0x86, 0x87, 0x85, 0x08, 0x91,
  28069. 0x72, 0x77, 0xB9, 0x3E, 0x10, 0x70, 0x6B, 0x16,
  28070. 0x14, 0x97, 0x2B, 0x2A, 0x94, 0xC7, 0x31, 0x0F,
  28071. 0xE9, 0xC7, 0x08, 0xC2, 0x31, 0xA1, 0xA8, 0xAC,
  28072. 0x8D, 0x93, 0x14, 0xA5, 0x29, 0xA9, 0x7F, 0x46,
  28073. 0x9B, 0xF6, 0x49, 0x62, 0xD8, 0x20, 0x64, 0x84,
  28074. 0x43, 0x09, 0x9A, 0x07, 0x6D, 0x55, 0xD4, 0xCE,
  28075. 0xA8, 0x24, 0xA5, 0x83, 0x04, 0x84, 0x4F, 0x99,
  28076. 0x49, 0x7C, 0x10, 0xA2, 0x51, 0x48, 0x61, 0x8A,
  28077. 0x31, 0x5D, 0x72, 0xCA, 0x85, 0x7D, 0x1B, 0x04,
  28078. 0xD5, 0x75, 0xB9, 0x4F, 0x85, 0xC0, 0x1D, 0x19,
  28079. 0xBE, 0xF2, 0x11, 0xBF, 0x0A, 0xA3, 0x36, 0x2E,
  28080. 0x70, 0x41, 0xFD, 0x16, 0x59, 0x6D, 0x80, 0x8E,
  28081. 0x86, 0x7B, 0x44, 0xC4, 0xC0, 0x0D, 0x1C, 0xDA,
  28082. 0x34, 0x18, 0x96, 0x77, 0x17, 0xF1, 0x47, 0xD0,
  28083. 0xEB, 0x21, 0xB4, 0x2A, 0xAE, 0xE7, 0x4A, 0xC3,
  28084. 0x5D, 0x0B, 0x92, 0x41, 0x4B, 0x95, 0x85, 0x31,
  28085. 0xAA, 0xDF, 0x46, 0x3E, 0xC6, 0x30, 0x5A, 0xE5,
  28086. 0xEC, 0xAF, 0x79, 0x17, 0x40, 0x02, 0xF2, 0x6D,
  28087. 0xDE, 0xCC, 0x81, 0x3B, 0xF3, 0x26, 0x72, 0xE8,
  28088. 0x52, 0x9D, 0x95, 0xA4, 0xE7, 0x30, 0xA7, 0xAB,
  28089. 0x4A, 0x3E, 0x8F, 0x8A, 0x8A, 0xF9, 0x79, 0xA6,
  28090. 0x65, 0xEA, 0xFD, 0x46, 0x5F, 0xC6, 0x4A, 0x0C,
  28091. 0x5F, 0x8F, 0x3F, 0x90, 0x03, 0x48, 0x94, 0x15,
  28092. 0x89, 0x9D, 0x59, 0xA5, 0x43, 0xD8, 0x20, 0x8C,
  28093. 0x54, 0xA3, 0x16, 0x65, 0x29, 0xB5, 0x39, 0x22,
  28094. 0xD4, 0xEC, 0x14, 0x3B, 0x50, 0xF0, 0x14, 0x23,
  28095. 0xB1, 0x77, 0x89, 0x5E, 0xDE, 0xE2, 0x2B, 0xB7,
  28096. 0x39, 0xF6, 0x47, 0xEC, 0xF8, 0x5F, 0x50, 0xBC,
  28097. 0x25, 0xEF, 0x7B, 0x5A, 0x72, 0x5D, 0xEE, 0x86,
  28098. 0x86, 0x26, 0xED, 0x79, 0xD4, 0x51, 0x14, 0x08,
  28099. 0x00, 0xE0, 0x3B, 0x59, 0xB9, 0x56, 0xF8, 0x21,
  28100. 0x0E, 0x55, 0x60, 0x67, 0x40, 0x7D, 0x13, 0xDC,
  28101. 0x90, 0xFA, 0x9E, 0x8B, 0x87, 0x2B, 0xFB, 0x8F
  28102. };
  28103. const byte kyber768_ct[] = {
  28104. 0xB5, 0x2C, 0x56, 0xB9, 0x2A, 0x4B, 0x7C, 0xE9,
  28105. 0xE4, 0xCB, 0x7C, 0x5B, 0x1B, 0x16, 0x31, 0x67,
  28106. 0xA8, 0xA1, 0x67, 0x5B, 0x2F, 0xDE, 0xF8, 0x4A,
  28107. 0x5B, 0x67, 0xCA, 0x15, 0xDB, 0x69, 0x4C, 0x9F,
  28108. 0x11, 0xBD, 0x02, 0x7C, 0x30, 0xAE, 0x22, 0xEC,
  28109. 0x92, 0x1A, 0x1D, 0x91, 0x15, 0x99, 0xAF, 0x05,
  28110. 0x85, 0xE4, 0x8D, 0x20, 0xDA, 0x70, 0xDF, 0x9F,
  28111. 0x39, 0xE3, 0x2E, 0xF9, 0x5D, 0x4C, 0x8F, 0x44,
  28112. 0xBF, 0xEF, 0xDA, 0xA5, 0xDA, 0x64, 0xF1, 0x05,
  28113. 0x46, 0x31, 0xD0, 0x4D, 0x6D, 0x3C, 0xFD, 0x0A,
  28114. 0x54, 0x0D, 0xD7, 0xBA, 0x38, 0x86, 0xE4, 0xB5,
  28115. 0xF1, 0x3E, 0x87, 0x87, 0x88, 0x60, 0x4C, 0x95,
  28116. 0xC0, 0x96, 0xEA, 0xB3, 0x91, 0x9F, 0x42, 0x75,
  28117. 0x21, 0x41, 0x9A, 0x94, 0x6C, 0x26, 0xCC, 0x04,
  28118. 0x14, 0x75, 0xD7, 0x12, 0x4C, 0xDC, 0x01, 0xD0,
  28119. 0x37, 0x3E, 0x5B, 0x09, 0xC7, 0xA7, 0x06, 0x03,
  28120. 0xCF, 0xDB, 0x4F, 0xB3, 0x40, 0x50, 0x23, 0xF2,
  28121. 0x26, 0x4D, 0xC3, 0xF9, 0x83, 0xC4, 0xFC, 0x02,
  28122. 0xA2, 0xD1, 0xB2, 0x68, 0xF2, 0x20, 0x8A, 0x1F,
  28123. 0x6E, 0x2A, 0x62, 0x09, 0xBF, 0xF1, 0x2F, 0x6F,
  28124. 0x46, 0x5F, 0x0B, 0x06, 0x9C, 0x3A, 0x7F, 0x84,
  28125. 0xF6, 0x06, 0xD8, 0xA9, 0x40, 0x64, 0x00, 0x3D,
  28126. 0x6E, 0xC1, 0x14, 0xC8, 0xE8, 0x08, 0xD3, 0x05,
  28127. 0x38, 0x84, 0xC1, 0xD5, 0xA1, 0x42, 0xFB, 0xF2,
  28128. 0x01, 0x12, 0xEB, 0x36, 0x0F, 0xDA, 0x3F, 0x0F,
  28129. 0x28, 0xB1, 0x72, 0xAE, 0x50, 0xF5, 0xE7, 0xD8,
  28130. 0x38, 0x01, 0xFB, 0x3F, 0x00, 0x64, 0xB6, 0x87,
  28131. 0x18, 0x70, 0x74, 0xBD, 0x7F, 0xE3, 0x0E, 0xDD,
  28132. 0xAA, 0x33, 0x4C, 0xF8, 0xFC, 0x04, 0xFA, 0x8C,
  28133. 0xED, 0x89, 0x9C, 0xEA, 0xDE, 0x4B, 0x4F, 0x28,
  28134. 0xB6, 0x83, 0x72, 0xBA, 0xF9, 0x8F, 0xF4, 0x82,
  28135. 0xA4, 0x15, 0xB7, 0x31, 0x15, 0x5B, 0x75, 0xCE,
  28136. 0xB9, 0x76, 0xBE, 0x0E, 0xA0, 0x28, 0x5B, 0xA0,
  28137. 0x1A, 0x27, 0xF1, 0x85, 0x7A, 0x8F, 0xB3, 0x77,
  28138. 0xA3, 0xAE, 0x0C, 0x23, 0xB2, 0xAA, 0x9A, 0x07,
  28139. 0x9B, 0xFA, 0xBF, 0xF0, 0xD5, 0xB2, 0xF1, 0xCD,
  28140. 0x9B, 0x71, 0x8B, 0xEA, 0x03, 0xC4, 0x2F, 0x34,
  28141. 0x3A, 0x39, 0xB4, 0xF1, 0x42, 0xD0, 0x1A, 0xD8,
  28142. 0xAC, 0xBB, 0x50, 0xE3, 0x88, 0x53, 0xCF, 0x9A,
  28143. 0x50, 0xC8, 0xB4, 0x4C, 0x3C, 0xF6, 0x71, 0xA4,
  28144. 0xA9, 0x04, 0x3B, 0x26, 0xDD, 0xBB, 0x24, 0x95,
  28145. 0x9A, 0xD6, 0x71, 0x5C, 0x08, 0x52, 0x18, 0x55,
  28146. 0xC7, 0x9A, 0x23, 0xB9, 0xC3, 0xD6, 0x47, 0x17,
  28147. 0x49, 0xC4, 0x07, 0x25, 0xBD, 0xD5, 0xC2, 0x77,
  28148. 0x6D, 0x43, 0xAE, 0xD2, 0x02, 0x04, 0xBA, 0xA1,
  28149. 0x41, 0xEF, 0xB3, 0x30, 0x49, 0x17, 0x47, 0x4B,
  28150. 0x7F, 0x9F, 0x7A, 0x4B, 0x08, 0xB1, 0xA9, 0x3D,
  28151. 0xAE, 0xD9, 0x8C, 0x67, 0x49, 0x53, 0x59, 0xD3,
  28152. 0x7D, 0x67, 0xF7, 0x43, 0x8B, 0xEE, 0x5E, 0x43,
  28153. 0x58, 0x56, 0x34, 0xB2, 0x6C, 0x6B, 0x38, 0x10,
  28154. 0xD7, 0xCD, 0xCB, 0xC0, 0xF6, 0xEB, 0x87, 0x7A,
  28155. 0x60, 0x87, 0xE6, 0x8A, 0xCB, 0x84, 0x80, 0xD3,
  28156. 0xA8, 0xCF, 0x69, 0x00, 0x44, 0x7E, 0x49, 0xB4,
  28157. 0x17, 0xF1, 0x5A, 0x53, 0xB6, 0x07, 0xA0, 0xE2,
  28158. 0x16, 0xB8, 0x55, 0x97, 0x0D, 0x37, 0x40, 0x68,
  28159. 0x70, 0xB4, 0x56, 0x87, 0x22, 0xDA, 0x77, 0xA4,
  28160. 0x08, 0x47, 0x03, 0x81, 0x67, 0x84, 0xE2, 0xF1,
  28161. 0x6B, 0xED, 0x18, 0x99, 0x65, 0x32, 0xC5, 0xD8,
  28162. 0xB7, 0xF5, 0xD2, 0x14, 0x46, 0x4E, 0x5F, 0x3F,
  28163. 0x6E, 0x90, 0x58, 0x67, 0xB0, 0xCE, 0x11, 0x9E,
  28164. 0x25, 0x2A, 0x66, 0x71, 0x32, 0x53, 0x54, 0x46,
  28165. 0x85, 0xD2, 0x08, 0xE1, 0x72, 0x39, 0x08, 0xA0,
  28166. 0xCE, 0x97, 0x83, 0x46, 0x52, 0xE0, 0x8A, 0xE7,
  28167. 0xBD, 0xC8, 0x81, 0xA1, 0x31, 0xB7, 0x3C, 0x71,
  28168. 0xE8, 0x4D, 0x20, 0xD6, 0x8F, 0xDE, 0xFF, 0x4F,
  28169. 0x5D, 0x70, 0xCD, 0x1A, 0xF5, 0x7B, 0x78, 0xE3,
  28170. 0x49, 0x1A, 0x98, 0x65, 0x94, 0x23, 0x21, 0x80,
  28171. 0x0A, 0x20, 0x3C, 0x05, 0xED, 0x1F, 0xEE, 0xB5,
  28172. 0xA2, 0x8E, 0x58, 0x4E, 0x19, 0xF6, 0x53, 0x5E,
  28173. 0x7F, 0x84, 0xE4, 0xA2, 0x4F, 0x84, 0xA7, 0x2D,
  28174. 0xCA, 0xF5, 0x64, 0x8B, 0x4A, 0x42, 0x35, 0xDD,
  28175. 0x66, 0x44, 0x64, 0x48, 0x2F, 0x03, 0x17, 0x6E,
  28176. 0x88, 0x8C, 0x28, 0xBF, 0xC6, 0xC1, 0xCB, 0x23,
  28177. 0x8C, 0xFF, 0xA3, 0x5A, 0x32, 0x1E, 0x71, 0x79,
  28178. 0x1D, 0x9E, 0xA8, 0xED, 0x08, 0x78, 0xC6, 0x11,
  28179. 0x21, 0xBF, 0x8D, 0x2A, 0x4A, 0xB2, 0xC1, 0xA5,
  28180. 0xE1, 0x20, 0xBC, 0x40, 0xAB, 0xB1, 0x89, 0x2D,
  28181. 0x17, 0x15, 0x09, 0x0A, 0x0E, 0xE4, 0x82, 0x52,
  28182. 0xCA, 0x29, 0x7A, 0x99, 0xAA, 0x0E, 0x51, 0x0C,
  28183. 0xF2, 0x6B, 0x1A, 0xDD, 0x06, 0xCA, 0x54, 0x3E,
  28184. 0x1C, 0x5D, 0x6B, 0xDC, 0xD3, 0xB9, 0xC5, 0x85,
  28185. 0xC8, 0x53, 0x80, 0x45, 0xDB, 0x5C, 0x25, 0x2E,
  28186. 0xC3, 0xC8, 0xC3, 0xC9, 0x54, 0xD9, 0xBE, 0x59,
  28187. 0x07, 0x09, 0x4A, 0x89, 0x4E, 0x60, 0xEA, 0xB4,
  28188. 0x35, 0x38, 0xCF, 0xEE, 0x82, 0xE8, 0xFF, 0xC0,
  28189. 0x79, 0x1B, 0x0D, 0x0F, 0x43, 0xAC, 0x16, 0x27,
  28190. 0x83, 0x0A, 0x61, 0xD5, 0x6D, 0xAD, 0x96, 0xC6,
  28191. 0x29, 0x58, 0xB0, 0xDE, 0x78, 0x0B, 0x78, 0xBD,
  28192. 0x47, 0xA6, 0x04, 0x55, 0x0D, 0xAB, 0x83, 0xFF,
  28193. 0xF2, 0x27, 0xC3, 0x24, 0x04, 0x94, 0x71, 0xF3,
  28194. 0x52, 0x48, 0xCF, 0xB8, 0x49, 0xB2, 0x57, 0x24,
  28195. 0xFF, 0x70, 0x4D, 0x52, 0x77, 0xAA, 0x35, 0x2D,
  28196. 0x55, 0x09, 0x58, 0xBE, 0x3B, 0x23, 0x7D, 0xFF,
  28197. 0x47, 0x3E, 0xC2, 0xAD, 0xBA, 0xEA, 0x48, 0xCA,
  28198. 0x26, 0x58, 0xAE, 0xFC, 0xC7, 0x7B, 0xBD, 0x42,
  28199. 0x64, 0xAB, 0x37, 0x4D, 0x70, 0xEA, 0xE5, 0xB9,
  28200. 0x64, 0x41, 0x6C, 0xE8, 0x22, 0x6A, 0x7E, 0x32,
  28201. 0x55, 0xA0, 0xF8, 0xD7, 0xE2, 0xAD, 0xCA, 0x06,
  28202. 0x2B, 0xCD, 0x6D, 0x78, 0xD6, 0x0D, 0x1B, 0x32,
  28203. 0xE1, 0x14, 0x05, 0xBE, 0x54, 0xB6, 0x6E, 0xF0,
  28204. 0xFD, 0xDD, 0x56, 0x77, 0x02, 0xA3, 0xBC, 0xCF,
  28205. 0xED, 0xE3, 0xC5, 0x84, 0x70, 0x12, 0x69, 0xED,
  28206. 0x14, 0x80, 0x9F, 0x06, 0xF8, 0x96, 0x83, 0x56,
  28207. 0xBB, 0x92, 0x67, 0xFE, 0x86, 0xE5, 0x14, 0x25,
  28208. 0x2E, 0x88, 0xBB, 0x5C, 0x30, 0xA7, 0xEC, 0xB3,
  28209. 0xD0, 0xE6, 0x21, 0x02, 0x1E, 0xE0, 0xFB, 0xF7,
  28210. 0x87, 0x1B, 0x09, 0x34, 0x2B, 0xF8, 0x4F, 0x55,
  28211. 0xC9, 0x7E, 0xAF, 0x86, 0xC4, 0x81, 0x89, 0xC7,
  28212. 0xFF, 0x4D, 0xF3, 0x89, 0xF0, 0x77, 0xE2, 0x80,
  28213. 0x6E, 0x5F, 0xA7, 0x3B, 0x3E, 0x94, 0x58, 0xA1,
  28214. 0x6C, 0x7E, 0x27, 0x5F, 0x4F, 0x60, 0x22, 0x75,
  28215. 0x58, 0x0E, 0xB7, 0xB7, 0x13, 0x5F, 0xB5, 0x37,
  28216. 0xFA, 0x0C, 0xD9, 0x5D, 0x6E, 0xA5, 0x8C, 0x10,
  28217. 0x8C, 0xD8, 0x94, 0x3D, 0x70, 0xC1, 0x64, 0x31,
  28218. 0x11, 0xF4, 0xF0, 0x1C, 0xA8, 0xA8, 0x27, 0x6A,
  28219. 0x90, 0x26, 0x66, 0xED, 0x81, 0xB7, 0x8D, 0x16,
  28220. 0x8B, 0x00, 0x6F, 0x16, 0xAA, 0xA3, 0xD8, 0xE4,
  28221. 0xCE, 0x4F, 0x4D, 0x0F, 0xB0, 0x99, 0x7E, 0x41,
  28222. 0xAE, 0xFF, 0xB5, 0xB3, 0xDA, 0xA8, 0x38, 0x73,
  28223. 0x2F, 0x35, 0x73, 0x49, 0x44, 0x7F, 0x38, 0x77,
  28224. 0x76, 0xC7, 0x93, 0xC0, 0x47, 0x9D, 0xE9, 0xE9,
  28225. 0x94, 0x98, 0xCC, 0x35, 0x6F, 0xDB, 0x00, 0x75,
  28226. 0xA7, 0x03, 0xF2, 0x3C, 0x55, 0xD4, 0x7B, 0x55,
  28227. 0x0E, 0xC8, 0x9B, 0x02, 0xAD, 0xE8, 0x93, 0x29,
  28228. 0x08, 0x6A, 0x50, 0x84, 0x34, 0x56, 0xFE, 0xDC,
  28229. 0x37, 0x88, 0xAC, 0x8D, 0x97, 0x23, 0x3C, 0x54,
  28230. 0x56, 0x04, 0x67, 0xEE, 0x1D, 0x0F, 0x02, 0x4B,
  28231. 0x18, 0x42, 0x8F, 0x0D, 0x73, 0xB3, 0x0E, 0x19,
  28232. 0xF5, 0xC6, 0x3B, 0x9A, 0xBF, 0x11, 0x41, 0x5B,
  28233. 0xEA, 0x4D, 0x01, 0x70, 0x13, 0x0B, 0xAA, 0xBD,
  28234. 0x33, 0xC0, 0x5E, 0x65, 0x24, 0xE5, 0xFB, 0x55,
  28235. 0x81, 0xB2, 0x2B, 0x04, 0x33, 0x34, 0x22, 0x48,
  28236. 0x26, 0x6D, 0x0F, 0x10, 0x53, 0xB2, 0x45, 0xCC,
  28237. 0x24, 0x62, 0xDC, 0x44, 0xD3, 0x49, 0x65, 0x10,
  28238. 0x24, 0x82, 0xA8, 0xED, 0x9E, 0x4E, 0x96, 0x4D,
  28239. 0x56, 0x83, 0xE5, 0xD4, 0x5D, 0x0C, 0x82, 0x69
  28240. };
  28241. const byte kyber768_ss[] = {
  28242. 0x91, 0x4C, 0xB6, 0x7F, 0xE5, 0xC3, 0x8E, 0x73,
  28243. 0xBF, 0x74, 0x18, 0x1C, 0x0A, 0xC5, 0x04, 0x28,
  28244. 0xDE, 0xDF, 0x77, 0x50, 0xA9, 0x80, 0x58, 0xF7,
  28245. 0xD5, 0x36, 0x70, 0x87, 0x74, 0x53, 0x5B, 0x29
  28246. };
  28247. #else
  28248. const byte kyber768_pk[] = {
  28249. 0x86, 0x1A, 0xCF, 0x24, 0x57, 0x9D, 0x8E, 0xE8,
  28250. 0x94, 0x63, 0xB4, 0x78, 0xE2, 0xE9, 0x53, 0xC7,
  28251. 0xD5, 0xCD, 0xA4, 0xD3, 0x07, 0xD1, 0x68, 0xCF,
  28252. 0xC6, 0x79, 0xB1, 0xA3, 0x79, 0x1F, 0x05, 0x83,
  28253. 0x82, 0x86, 0x0B, 0x1C, 0x77, 0x52, 0x87, 0x89,
  28254. 0xC6, 0x96, 0x4B, 0xCC, 0x40, 0x8A, 0x21, 0xCA,
  28255. 0x4E, 0x42, 0x8F, 0xE7, 0x33, 0x9F, 0x7C, 0x08,
  28256. 0x76, 0xA8, 0x58, 0xAB, 0xA9, 0x44, 0xB7, 0x3F,
  28257. 0x9C, 0xC9, 0xB8, 0xAC, 0xC4, 0xB7, 0x9A, 0x9F,
  28258. 0x72, 0xF7, 0xA0, 0x47, 0x72, 0x57, 0xB9, 0xF3,
  28259. 0x26, 0xE4, 0x4C, 0x2C, 0x5F, 0x8B, 0x8A, 0x17,
  28260. 0x58, 0x81, 0x42, 0xB0, 0x91, 0xA3, 0x12, 0x31,
  28261. 0xD0, 0x98, 0xB0, 0x57, 0x30, 0x45, 0x62, 0x90,
  28262. 0x6E, 0x96, 0x05, 0x47, 0x3D, 0x28, 0xB7, 0xB4,
  28263. 0x33, 0x2A, 0xB4, 0x49, 0x82, 0x48, 0x7B, 0x04,
  28264. 0x9E, 0x41, 0x93, 0x74, 0xA4, 0x8E, 0xAC, 0x6A,
  28265. 0xB8, 0xCB, 0x42, 0x6D, 0x13, 0xA4, 0x86, 0x28,
  28266. 0x28, 0x43, 0x02, 0xFB, 0x72, 0x71, 0xD1, 0x54,
  28267. 0xCA, 0x09, 0x06, 0xD8, 0xE2, 0x0F, 0x1D, 0x97,
  28268. 0xBE, 0x9C, 0x82, 0x2E, 0x78, 0x83, 0x79, 0x32,
  28269. 0xEB, 0x28, 0xEE, 0xB0, 0x83, 0x71, 0xBB, 0xC6,
  28270. 0x3A, 0x7A, 0x96, 0x6C, 0xAC, 0x7A, 0x05, 0xB5,
  28271. 0x50, 0x3C, 0x55, 0x1A, 0xEF, 0x80, 0x15, 0x14,
  28272. 0xE1, 0x6A, 0xF6, 0x81, 0x02, 0x64, 0x6C, 0x66,
  28273. 0x9D, 0xD8, 0xA7, 0xE8, 0xA4, 0x11, 0x39, 0x54,
  28274. 0x62, 0xC7, 0xD4, 0x83, 0x8B, 0x91, 0x96, 0x96,
  28275. 0x54, 0x16, 0x59, 0x2B, 0xCF, 0xAC, 0x97, 0x87,
  28276. 0x0C, 0x23, 0x58, 0xA8, 0x59, 0x5B, 0x2F, 0x43,
  28277. 0x55, 0xF0, 0xFA, 0x10, 0x43, 0xA1, 0x1F, 0x62,
  28278. 0x86, 0x2F, 0xD5, 0xD9, 0x7A, 0x9D, 0xE2, 0x1B,
  28279. 0xD3, 0x39, 0x85, 0x5E, 0x17, 0x8C, 0x97, 0xD3,
  28280. 0x77, 0xD1, 0xD1, 0x46, 0xEC, 0x6A, 0x96, 0xE4,
  28281. 0xFB, 0xB7, 0x15, 0xC4, 0xC0, 0x22, 0x84, 0x55,
  28282. 0xBB, 0x64, 0xA3, 0x88, 0xDC, 0xC4, 0xD3, 0x91,
  28283. 0x3A, 0x76, 0x09, 0x57, 0xD1, 0xA6, 0x9F, 0xEA,
  28284. 0xAA, 0x08, 0x92, 0x29, 0xB8, 0xCA, 0xD0, 0xBC,
  28285. 0xC9, 0x38, 0xC7, 0x80, 0x69, 0x8A, 0x5C, 0xE1,
  28286. 0x9B, 0xC5, 0x21, 0x44, 0xDB, 0x46, 0x3B, 0x6D,
  28287. 0x55, 0x4F, 0xC4, 0xEC, 0x5E, 0x18, 0x64, 0xC4,
  28288. 0xC6, 0x88, 0x75, 0x97, 0x50, 0x0F, 0x40, 0x94,
  28289. 0x18, 0xB3, 0xA3, 0x3F, 0x2C, 0x88, 0xB2, 0x6E,
  28290. 0xD9, 0x83, 0x1D, 0xB4, 0x65, 0x08, 0xE8, 0x9F,
  28291. 0x8C, 0x33, 0x5B, 0xE1, 0x94, 0x56, 0x12, 0x05,
  28292. 0x22, 0xA8, 0xB4, 0x43, 0xFE, 0xF2, 0x1A, 0x7B,
  28293. 0xC3, 0x05, 0x9A, 0xFB, 0x4A, 0xAA, 0xD5, 0x0E,
  28294. 0x6C, 0x72, 0x8B, 0xEA, 0x8A, 0xC5, 0xDC, 0x7C,
  28295. 0x6D, 0x06, 0x94, 0x76, 0x03, 0x51, 0x0A, 0x84,
  28296. 0x90, 0xAF, 0xCE, 0x30, 0x4D, 0x57, 0xA7, 0x25,
  28297. 0xC4, 0x3B, 0x5A, 0xA3, 0x71, 0x3B, 0xA0, 0x44,
  28298. 0x98, 0x75, 0xE5, 0x67, 0x55, 0x13, 0x22, 0x60,
  28299. 0x54, 0x63, 0x6C, 0xF8, 0x9E, 0x59, 0x5A, 0xC5,
  28300. 0xAE, 0xE5, 0x66, 0x78, 0x4B, 0x80, 0xFA, 0x39,
  28301. 0x64, 0x0B, 0x74, 0x0F, 0x47, 0x12, 0x22, 0x60,
  28302. 0x25, 0x11, 0x00, 0xFA, 0x8E, 0xE0, 0xF1, 0x65,
  28303. 0x27, 0x11, 0x40, 0x0C, 0x68, 0x16, 0x38, 0x3C,
  28304. 0xC5, 0xE6, 0xDA, 0xB7, 0x9C, 0xDC, 0x3E, 0x41,
  28305. 0x98, 0x37, 0x48, 0x15, 0xB2, 0xBB, 0xA2, 0x0E,
  28306. 0xFC, 0x14, 0x25, 0xCE, 0xD1, 0x9B, 0x60, 0x19,
  28307. 0x77, 0x08, 0x08, 0x00, 0xCF, 0x54, 0x9C, 0xC4,
  28308. 0x80, 0x7D, 0xE6, 0xCA, 0x84, 0xE5, 0xD3, 0xC2,
  28309. 0xCF, 0x8B, 0x11, 0xEB, 0x68, 0x56, 0xE7, 0x1C,
  28310. 0x6B, 0xA5, 0xD2, 0x96, 0x45, 0x46, 0xAA, 0x0C,
  28311. 0x82, 0x53, 0x10, 0xF7, 0x79, 0xED, 0x69, 0x53,
  28312. 0xD9, 0x22, 0x5E, 0x11, 0xF0, 0x57, 0x93, 0x48,
  28313. 0x4C, 0x6B, 0x47, 0xB6, 0xE3, 0xD0, 0x4A, 0x4B,
  28314. 0x27, 0xB7, 0x86, 0x71, 0x46, 0xF6, 0xA0, 0x64,
  28315. 0xB9, 0x8B, 0x8B, 0xDD, 0x2B, 0x69, 0x94, 0xBA,
  28316. 0x20, 0xF8, 0x14, 0x95, 0xB3, 0x74, 0x1C, 0xE1,
  28317. 0xD3, 0x07, 0xE5, 0xEC, 0x0A, 0x7C, 0x20, 0x20,
  28318. 0x2B, 0x81, 0xA1, 0x04, 0x02, 0x83, 0x4D, 0x23,
  28319. 0x83, 0xEC, 0xB2, 0x33, 0x3B, 0x2C, 0x93, 0xD6,
  28320. 0xC4, 0x37, 0xC3, 0x17, 0x58, 0xDF, 0x92, 0x21,
  28321. 0xA3, 0xFA, 0x60, 0x24, 0x81, 0x29, 0x29, 0x62,
  28322. 0x81, 0x17, 0xCB, 0xC4, 0x79, 0x22, 0x76, 0xDA,
  28323. 0x84, 0x0F, 0xAB, 0x37, 0x03, 0x54, 0x65, 0x23,
  28324. 0x0C, 0x7A, 0x79, 0xEB, 0xB1, 0xA8, 0xC2, 0xB4,
  28325. 0x49, 0x0C, 0x2C, 0x1C, 0xB8, 0xE8, 0xB5, 0x73,
  28326. 0x5B, 0xC0, 0x72, 0xFC, 0x4D, 0x0F, 0x02, 0xB5,
  28327. 0x5B, 0x01, 0x3A, 0x53, 0x86, 0x4B, 0x82, 0x5A,
  28328. 0x85, 0xE9, 0x52, 0x03, 0x76, 0xC4, 0x4A, 0x9D,
  28329. 0xBA, 0x2C, 0xE9, 0x06, 0x8F, 0x25, 0x7A, 0x11,
  28330. 0xCF, 0xB6, 0x46, 0x6A, 0xC5, 0xAB, 0xBC, 0x85,
  28331. 0xCE, 0x80, 0x0A, 0x13, 0xB9, 0x64, 0x4D, 0x6E,
  28332. 0x04, 0x70, 0x1D, 0x23, 0xA9, 0x9A, 0x7C, 0x1A,
  28333. 0x5A, 0xEA, 0xBE, 0x2D, 0xB1, 0x34, 0x16, 0xD0,
  28334. 0xC3, 0x5C, 0x6C, 0x04, 0x98, 0x8B, 0x39, 0x91,
  28335. 0x6C, 0x46, 0x67, 0x6C, 0x66, 0x51, 0xF3, 0x5E,
  28336. 0xC2, 0x28, 0x13, 0xCF, 0xE3, 0x84, 0x40, 0x69,
  28337. 0x2F, 0x43, 0x29, 0xC5, 0x85, 0x03, 0x27, 0x30,
  28338. 0xC6, 0x94, 0xF2, 0xB2, 0x41, 0x03, 0xFB, 0x78,
  28339. 0xE4, 0x3A, 0x49, 0x52, 0x70, 0x03, 0xF4, 0xFA,
  28340. 0x5B, 0x73, 0x49, 0x2A, 0x50, 0x71, 0x83, 0x55,
  28341. 0x36, 0x26, 0x78, 0x26, 0x97, 0x37, 0x65, 0x7B,
  28342. 0x7A, 0x24, 0x03, 0xD4, 0x15, 0x09, 0xD6, 0xEC,
  28343. 0x9E, 0x51, 0x65, 0x51, 0x95, 0x56, 0xCB, 0x94,
  28344. 0x92, 0xCD, 0x6D, 0x04, 0x6F, 0x2B, 0x4C, 0x91,
  28345. 0x4A, 0xF6, 0x3C, 0xBF, 0xCC, 0xA0, 0xB4, 0xCC,
  28346. 0x44, 0x6A, 0x54, 0x69, 0x54, 0x59, 0x41, 0x66,
  28347. 0x8A, 0xBD, 0x9A, 0x26, 0x7A, 0xF2, 0x41, 0x28,
  28348. 0x33, 0xD9, 0x67, 0xB8, 0xAB, 0xB4, 0x34, 0xD7,
  28349. 0xA0, 0xAF, 0x5C, 0xAB, 0x11, 0x34, 0xB0, 0x9E,
  28350. 0xDB, 0x7A, 0xA1, 0xC1, 0x60, 0x0A, 0xF1, 0x5E,
  28351. 0xF6, 0x15, 0x27, 0xC8, 0xC1, 0xA1, 0x59, 0x40,
  28352. 0xCA, 0xB1, 0x60, 0x35, 0xF0, 0x5C, 0x71, 0x77,
  28353. 0x15, 0x90, 0x9D, 0x73, 0x2D, 0x1E, 0x9B, 0x4C,
  28354. 0x59, 0x2B, 0xB3, 0x86, 0xA5, 0xC1, 0xF3, 0xB9,
  28355. 0x89, 0x17, 0x58, 0x82, 0x05, 0xD2, 0xA8, 0xB5,
  28356. 0x03, 0x8A, 0x52, 0x4B, 0x99, 0x52, 0xD5, 0x9D,
  28357. 0x85, 0xDB, 0x27, 0x3C, 0xA1, 0xA7, 0x87, 0xF0,
  28358. 0x1F, 0x9B, 0xCB, 0x07, 0xDA, 0xC3, 0x3B, 0xC4,
  28359. 0xF0, 0xB4, 0x33, 0x36, 0x38, 0xCD, 0x3A, 0x60,
  28360. 0x1D, 0x35, 0x63, 0xF3, 0x53, 0xB5, 0xDF, 0xAA,
  28361. 0xA6, 0x2C, 0x68, 0xBF, 0x92, 0xA4, 0x79, 0xD8,
  28362. 0xB9, 0x4F, 0x81, 0x54, 0x31, 0x72, 0xB8, 0x36,
  28363. 0x26, 0xCC, 0x3B, 0x48, 0x72, 0x76, 0xAA, 0x5C,
  28364. 0xCB, 0x4D, 0x10, 0xBB, 0x18, 0xC9, 0x9F, 0x48,
  28365. 0x2A, 0x88, 0x26, 0x1C, 0x21, 0xA7, 0x90, 0x0F,
  28366. 0x60, 0x73, 0xB7, 0x06, 0xAC, 0xC5, 0xD2, 0xD7,
  28367. 0x54, 0x10, 0xB3, 0xA8, 0x84, 0x06, 0x95, 0x87,
  28368. 0x2B, 0xAC, 0xC8, 0xAA, 0x36, 0x60, 0xA4, 0x7D,
  28369. 0x6B, 0x7A, 0x5A, 0xA7, 0x9A, 0x5D, 0x28, 0xB2,
  28370. 0x6B, 0xFF, 0x80, 0x63, 0x14, 0xE6, 0x92, 0x93,
  28371. 0x30, 0x4A, 0xD0, 0x13, 0xB4, 0xE6, 0x75, 0xAE,
  28372. 0x62, 0x0B, 0x52, 0xEF, 0x30, 0x79, 0x00, 0x33,
  28373. 0xCC, 0x42, 0x78, 0xC0, 0x38, 0x50, 0xB4, 0xE6,
  28374. 0x0B, 0x39, 0x63, 0x46, 0x11, 0xDC, 0x3A, 0x5F,
  28375. 0x4C, 0x79, 0x66, 0xA9, 0xF4, 0x92, 0x0B, 0xF7,
  28376. 0x08, 0xBA, 0x8A, 0x01, 0x49, 0x1C, 0x7C, 0xEC,
  28377. 0x97, 0x40, 0xFA, 0x97, 0x6E, 0x8E, 0x76, 0x13,
  28378. 0x00, 0x60, 0x5F, 0x75, 0x20, 0x88, 0x56, 0x56,
  28379. 0x5F, 0x4F, 0x55, 0x60, 0x8F, 0x61, 0x37, 0x92,
  28380. 0x99, 0xA6, 0x4C, 0x96, 0x6B, 0xAD, 0x4A, 0x0E,
  28381. 0x87, 0x92, 0xAD, 0x3D, 0x46, 0x86, 0x41, 0x28,
  28382. 0x37, 0x61, 0xA5, 0x39, 0x99, 0x25, 0x86, 0x63,
  28383. 0x30, 0xB5, 0xBA, 0x37, 0x07, 0xE8, 0x54, 0xA6,
  28384. 0x3F, 0xE7, 0xAA, 0x7F, 0x81, 0x1A, 0xA4, 0x57,
  28385. 0x54, 0x08, 0xC8, 0xC2, 0x06, 0xBB, 0x39, 0x53,
  28386. 0xA7, 0x66, 0x29, 0x9A, 0x5C, 0x26, 0xC6, 0x7A,
  28387. 0x4E, 0x58, 0x21, 0x05, 0x79, 0xB3, 0x10, 0xF7,
  28388. 0x99, 0xB0, 0x97, 0x9B, 0xB9, 0x45, 0x8A, 0xEC,
  28389. 0x21, 0x50, 0xAD, 0x27, 0x3F, 0xEB, 0xCB, 0x3E,
  28390. 0xF3, 0xB6, 0x0B, 0x38, 0x56, 0xA7, 0x16, 0xB9,
  28391. 0x78, 0x3D, 0xF8, 0x08, 0xC6, 0x9C, 0x06, 0x12,
  28392. 0xA0, 0x9E, 0xA5, 0x93, 0x16, 0x64, 0x65, 0x87,
  28393. 0x69, 0x2D, 0x3C, 0x8E, 0x8C, 0x73, 0x9F, 0xB3,
  28394. 0x78, 0x27, 0x74, 0x15, 0x61, 0xD5, 0xE7, 0x66,
  28395. 0x67, 0x51, 0x82, 0xFF, 0x83, 0xC9, 0x1C, 0xF0,
  28396. 0x33, 0x65, 0x88, 0x23, 0xD8, 0xFA, 0x91, 0xE1
  28397. };
  28398. const byte kyber768_sk[] = {
  28399. 0x1B, 0xD1, 0x97, 0x95, 0x10, 0xBC, 0xE5, 0x03,
  28400. 0x1C, 0x0F, 0x99, 0x1D, 0xE0, 0x82, 0xC3, 0x89,
  28401. 0xA1, 0xBD, 0x15, 0x57, 0x19, 0x1D, 0x81, 0x03,
  28402. 0xBD, 0xCC, 0x04, 0xC1, 0x98, 0xAE, 0x3E, 0x95,
  28403. 0x95, 0xFD, 0xB6, 0x57, 0xB5, 0xC6, 0x39, 0x97,
  28404. 0x95, 0x79, 0xAE, 0x13, 0x5B, 0x11, 0xE2, 0x4D,
  28405. 0x36, 0xE5, 0x49, 0x44, 0x67, 0x83, 0x2E, 0x6A,
  28406. 0x8A, 0x46, 0xAA, 0x8F, 0x9F, 0xF9, 0x87, 0x4A,
  28407. 0x16, 0x00, 0x27, 0x84, 0x07, 0x71, 0x56, 0x2D,
  28408. 0x74, 0x61, 0xB1, 0x6C, 0x61, 0x87, 0xF3, 0xC2,
  28409. 0xC1, 0x9C, 0x9C, 0x8F, 0xE6, 0x41, 0x49, 0x7D,
  28410. 0x8A, 0x15, 0x5C, 0x51, 0x15, 0x7D, 0x91, 0x35,
  28411. 0xD9, 0xB4, 0x5D, 0x3C, 0x7C, 0x4C, 0xB1, 0x82,
  28412. 0x31, 0x8C, 0x11, 0xAD, 0x08, 0xB3, 0x9F, 0xAC,
  28413. 0x04, 0x15, 0xE6, 0xE2, 0x6D, 0xB3, 0x43, 0x91,
  28414. 0xDE, 0xC2, 0x47, 0x1B, 0x30, 0x3C, 0x05, 0x05,
  28415. 0xB6, 0xFE, 0x45, 0x6E, 0x81, 0x50, 0x21, 0xE8,
  28416. 0x2B, 0x5F, 0x2D, 0x16, 0x6F, 0xDF, 0xB5, 0xB9,
  28417. 0x23, 0xC8, 0x3A, 0x61, 0x67, 0x30, 0x28, 0xF0,
  28418. 0xBA, 0xE0, 0x28, 0x08, 0x9F, 0x47, 0x61, 0x67,
  28419. 0xE5, 0x75, 0x6C, 0xAA, 0x3E, 0xE0, 0xF1, 0x89,
  28420. 0xC6, 0x47, 0x81, 0xC7, 0x49, 0x27, 0xBC, 0xDB,
  28421. 0x67, 0xE7, 0x75, 0xCC, 0xA8, 0x5B, 0x44, 0xB8,
  28422. 0x8B, 0x10, 0x61, 0xD8, 0x96, 0x3F, 0xE4, 0x0B,
  28423. 0x1F, 0xB4, 0x34, 0x68, 0x20, 0x09, 0x0E, 0x5C,
  28424. 0x0B, 0x7C, 0x49, 0x08, 0x89, 0x33, 0xAD, 0xE3,
  28425. 0x82, 0x96, 0x88, 0x6C, 0x43, 0x45, 0x65, 0x7C,
  28426. 0x3F, 0x07, 0xCF, 0x1A, 0xE9, 0x76, 0xE6, 0xBA,
  28427. 0x77, 0x7E, 0xF0, 0x1F, 0xB9, 0xC3, 0xB0, 0xDC,
  28428. 0xF0, 0x58, 0x3A, 0xE5, 0x44, 0xCD, 0x80, 0x3A,
  28429. 0xC1, 0x39, 0xCA, 0x61, 0x9B, 0x8D, 0xCA, 0x13,
  28430. 0x1C, 0xD2, 0x73, 0xCE, 0xCB, 0x65, 0xBC, 0xEF,
  28431. 0x00, 0x29, 0x95, 0x12, 0xB0, 0x21, 0xBA, 0x1B,
  28432. 0x2F, 0xF2, 0x08, 0xEE, 0xBB, 0x08, 0xA6, 0x28,
  28433. 0x7E, 0xC4, 0x4C, 0x21, 0x7D, 0x94, 0x76, 0x40,
  28434. 0x42, 0x5C, 0x0E, 0x97, 0x13, 0x44, 0x5B, 0xC5,
  28435. 0x2B, 0xC3, 0xCD, 0x66, 0x01, 0x2C, 0x8C, 0xB7,
  28436. 0x10, 0xA2, 0xE8, 0xAD, 0x5F, 0xC5, 0x1C, 0x5B,
  28437. 0x71, 0xCE, 0x69, 0xE9, 0x6A, 0x22, 0x06, 0x6F,
  28438. 0x74, 0x4B, 0xBA, 0xB6, 0x47, 0x2F, 0xEF, 0x3A,
  28439. 0x5B, 0xCF, 0xD6, 0x1D, 0xF1, 0xF8, 0x6A, 0x95,
  28440. 0x64, 0x80, 0x03, 0x1A, 0x99, 0x0E, 0xE0, 0x7D,
  28441. 0xE5, 0x04, 0x5E, 0xBC, 0x49, 0x8F, 0x2D, 0x3A,
  28442. 0xBD, 0xE3, 0x28, 0xC4, 0x2A, 0xCB, 0x21, 0xAD,
  28443. 0x62, 0x5D, 0x7B, 0xCB, 0x85, 0x5A, 0x8A, 0x0A,
  28444. 0xCB, 0xA2, 0x85, 0x16, 0xD3, 0x5A, 0xAF, 0x92,
  28445. 0xBE, 0xB3, 0x9C, 0x46, 0xD8, 0xF4, 0xA0, 0x1F,
  28446. 0x73, 0x20, 0x40, 0xF2, 0x7C, 0x40, 0x9B, 0xA4,
  28447. 0x9A, 0x80, 0xCF, 0x95, 0x55, 0x7C, 0x73, 0x7C,
  28448. 0x4A, 0x45, 0x11, 0x2C, 0x52, 0x2C, 0x78, 0x04,
  28449. 0xC7, 0x7A, 0x3D, 0x44, 0x53, 0xBA, 0x82, 0xC1,
  28450. 0x49, 0x08, 0xBC, 0xC0, 0x47, 0x13, 0x7E, 0x5A,
  28451. 0x7B, 0x23, 0x92, 0xC8, 0x31, 0x25, 0x4C, 0x1A,
  28452. 0x59, 0x54, 0xC8, 0x8C, 0xCF, 0xA5, 0xD8, 0x8B,
  28453. 0x3B, 0x93, 0x31, 0xBE, 0xF4, 0x62, 0x55, 0x09,
  28454. 0x14, 0xD1, 0x84, 0x17, 0x59, 0x40, 0xB4, 0xE9,
  28455. 0xB7, 0xAD, 0x3D, 0x44, 0x2E, 0xA9, 0x68, 0xB6,
  28456. 0xA4, 0x0A, 0x19, 0x72, 0x71, 0x5B, 0xCB, 0x72,
  28457. 0x82, 0x14, 0xA1, 0xBE, 0x8E, 0x04, 0x1D, 0xF5,
  28458. 0x72, 0xCC, 0x2D, 0x01, 0x6F, 0xFD, 0x6B, 0x0A,
  28459. 0xFA, 0xC8, 0x02, 0xBA, 0x97, 0x5A, 0x4A, 0x31,
  28460. 0xA4, 0x18, 0x40, 0x2E, 0x6B, 0x01, 0x41, 0xFA,
  28461. 0xD6, 0x48, 0x49, 0xE1, 0x6C, 0x80, 0x37, 0x23,
  28462. 0xA7, 0x74, 0x2B, 0x08, 0x2A, 0x7A, 0x56, 0x26,
  28463. 0x4E, 0x02, 0x60, 0xA2, 0x4A, 0x66, 0x42, 0x3D,
  28464. 0x52, 0x64, 0x82, 0x07, 0x04, 0x06, 0x5C, 0xAC,
  28465. 0x41, 0x10, 0x72, 0x48, 0x64, 0x37, 0xCA, 0xA5,
  28466. 0x47, 0x2D, 0x73, 0x3C, 0x64, 0x14, 0x5F, 0x10,
  28467. 0x0B, 0x0B, 0x16, 0xC0, 0xAB, 0x0D, 0xB8, 0x7C,
  28468. 0xC5, 0x64, 0x26, 0xC1, 0x1A, 0x69, 0x45, 0x25,
  28469. 0x95, 0x8D, 0xB8, 0x31, 0x59, 0x31, 0xC5, 0x09,
  28470. 0xD6, 0x3C, 0x48, 0x10, 0xBB, 0xA7, 0x68, 0xCA,
  28471. 0xCB, 0xF9, 0xCD, 0x85, 0x7B, 0x0D, 0x66, 0xD6,
  28472. 0x5A, 0xF7, 0xEB, 0xAC, 0x18, 0xB7, 0x9C, 0xBE,
  28473. 0xBC, 0x0A, 0x49, 0xB5, 0xC0, 0x5A, 0xA8, 0x7F,
  28474. 0x8D, 0xE8, 0x25, 0xA6, 0xA1, 0x13, 0x5F, 0xE8,
  28475. 0x64, 0xCF, 0x6C, 0x99, 0x8D, 0xDC, 0x37, 0xFC,
  28476. 0x01, 0x56, 0xBB, 0x52, 0xA7, 0x8E, 0xC2, 0x99,
  28477. 0xCA, 0x53, 0x1C, 0xF8, 0x75, 0xBE, 0xFA, 0x48,
  28478. 0x75, 0x72, 0x60, 0x28, 0xED, 0x37, 0x91, 0xBA,
  28479. 0x31, 0xC0, 0x0E, 0xC7, 0x40, 0x20, 0xCA, 0xA1,
  28480. 0x7F, 0x91, 0x19, 0x63, 0x96, 0xCF, 0xE5, 0x88,
  28481. 0x6E, 0x51, 0x1A, 0x52, 0xD0, 0xCA, 0x8D, 0xEE,
  28482. 0x25, 0xBF, 0x4E, 0x77, 0xCB, 0x92, 0xE6, 0x36,
  28483. 0x40, 0x64, 0x6D, 0xE2, 0x58, 0x6D, 0xCD, 0x00,
  28484. 0x14, 0x16, 0xCC, 0xC1, 0xEC, 0x30, 0x7C, 0xA2,
  28485. 0x75, 0x6D, 0xBE, 0xA8, 0x26, 0x36, 0x14, 0x51,
  28486. 0x3C, 0xA0, 0x5E, 0x70, 0xC7, 0x54, 0xF5, 0x6A,
  28487. 0x2C, 0x3F, 0xF7, 0x42, 0x94, 0x8C, 0x34, 0xAF,
  28488. 0x49, 0x34, 0x2F, 0x55, 0x03, 0x4C, 0x1C, 0x70,
  28489. 0xE0, 0x79, 0x6D, 0x26, 0xB2, 0x30, 0x35, 0x6B,
  28490. 0x1B, 0x74, 0xA1, 0x6C, 0x0B, 0x28, 0x37, 0x4B,
  28491. 0x79, 0xC1, 0x6B, 0x04, 0x96, 0xCA, 0x20, 0x12,
  28492. 0x3E, 0x6C, 0x67, 0x62, 0xD9, 0x01, 0xE2, 0x76,
  28493. 0x08, 0x5D, 0x81, 0x2A, 0xE9, 0xC3, 0x45, 0xAD,
  28494. 0xF5, 0x09, 0xDD, 0xC7, 0x87, 0x61, 0x62, 0xCE,
  28495. 0x34, 0xA6, 0x00, 0x3F, 0xB4, 0x5C, 0xD8, 0x66,
  28496. 0x0F, 0xDD, 0x46, 0x71, 0x6E, 0x82, 0x55, 0x06,
  28497. 0x62, 0x37, 0x5D, 0xB5, 0x5E, 0x63, 0xA2, 0x0F,
  28498. 0x01, 0x2A, 0x71, 0xE3, 0xD9, 0x72, 0x25, 0xC5,
  28499. 0x8F, 0x52, 0x34, 0xC6, 0xB5, 0x3A, 0x8A, 0xA0,
  28500. 0x33, 0x9C, 0xEF, 0xD4, 0x84, 0xE4, 0x7A, 0x3D,
  28501. 0xE6, 0x49, 0x88, 0x05, 0xC2, 0x82, 0xE0, 0x76,
  28502. 0x8E, 0x35, 0xC7, 0x5C, 0x20, 0x07, 0x76, 0x5C,
  28503. 0xA1, 0x7F, 0x01, 0xD2, 0x85, 0xA7, 0xC3, 0x04,
  28504. 0x8B, 0xC7, 0x2B, 0xA3, 0xAA, 0x57, 0x7C, 0x1C,
  28505. 0x43, 0x2A, 0x3C, 0x76, 0x9D, 0x21, 0xB8, 0x6C,
  28506. 0x01, 0x8F, 0x81, 0x7A, 0x26, 0x67, 0x35, 0x1C,
  28507. 0x2B, 0xC8, 0x5F, 0xA5, 0x53, 0x6F, 0xA7, 0xA6,
  28508. 0xCD, 0xEF, 0xCC, 0x0D, 0xD6, 0xF9, 0x1B, 0xDD,
  28509. 0xE8, 0x9D, 0x4E, 0x51, 0x6C, 0x8B, 0x75, 0x40,
  28510. 0xA7, 0xDC, 0x3F, 0xC4, 0x89, 0x8A, 0xB3, 0x30,
  28511. 0x08, 0xBC, 0xC1, 0x9F, 0x9E, 0x46, 0x3D, 0xE9,
  28512. 0x36, 0x26, 0x99, 0x4C, 0xC9, 0x27, 0xA3, 0x4A,
  28513. 0x9D, 0x70, 0x40, 0xC1, 0x31, 0xC8, 0x1F, 0x07,
  28514. 0x83, 0xDD, 0x68, 0x84, 0x29, 0x43, 0xC5, 0x5D,
  28515. 0xC5, 0x77, 0xD3, 0x67, 0xA4, 0x01, 0x83, 0x94,
  28516. 0xBB, 0x11, 0x60, 0x72, 0xAC, 0x65, 0xF2, 0xEB,
  28517. 0x9B, 0x4F, 0xF5, 0x8F, 0x41, 0x15, 0x40, 0x1B,
  28518. 0x11, 0x7D, 0x91, 0x3B, 0x75, 0x75, 0x57, 0x57,
  28519. 0x19, 0x39, 0x0C, 0x9E, 0x80, 0x06, 0x97, 0xA8,
  28520. 0x28, 0xB1, 0x25, 0x83, 0xF9, 0x4A, 0x2F, 0x8F,
  28521. 0x72, 0x0A, 0x6E, 0x87, 0x00, 0xFD, 0x40, 0x7D,
  28522. 0x73, 0xA4, 0xC7, 0x97, 0x55, 0x2B, 0x98, 0x75,
  28523. 0x7A, 0x5F, 0x57, 0x82, 0xA9, 0x50, 0x95, 0xDF,
  28524. 0xA5, 0xAC, 0xB7, 0xD9, 0x3A, 0x80, 0x5C, 0x30,
  28525. 0x18, 0x30, 0x4E, 0x5D, 0x22, 0xB7, 0x6F, 0xA5,
  28526. 0xAA, 0x51, 0x96, 0xB7, 0xEB, 0xDB, 0x7A, 0xA0,
  28527. 0xA5, 0x2D, 0x06, 0xAC, 0x4B, 0x9E, 0x4C, 0x00,
  28528. 0xC0, 0xF8, 0x5C, 0xEC, 0xB0, 0xBD, 0x95, 0x56,
  28529. 0x2D, 0x56, 0x23, 0x30, 0x78, 0x51, 0x43, 0xC7,
  28530. 0xF1, 0x29, 0xFA, 0xF9, 0x19, 0x48, 0xB0, 0xC8,
  28531. 0x8C, 0x60, 0x82, 0xE0, 0xF3, 0x03, 0x54, 0x74,
  28532. 0x12, 0x6E, 0x23, 0x6B, 0x80, 0xD2, 0x22, 0xD2,
  28533. 0x81, 0x4F, 0x6A, 0xF2, 0x4B, 0xD5, 0xD1, 0x19,
  28534. 0x86, 0x33, 0x1F, 0xAF, 0xC2, 0x1E, 0xC8, 0x4C,
  28535. 0x93, 0xA8, 0x81, 0x24, 0x72, 0x51, 0x10, 0x5A,
  28536. 0x6A, 0xB4, 0x35, 0xD7, 0x6E, 0xFD, 0x88, 0x9D,
  28537. 0x27, 0xC0, 0x0C, 0x2A, 0x14, 0xBD, 0x04, 0xA6,
  28538. 0xBD, 0x0B, 0x20, 0x07, 0xB5, 0x51, 0x5A, 0x75,
  28539. 0xF1, 0x14, 0xD1, 0x92, 0x10, 0xBC, 0x70, 0x50,
  28540. 0x85, 0x33, 0x53, 0x2F, 0x1B, 0x0A, 0x30, 0x0C,
  28541. 0x8B, 0x12, 0xF0, 0x0A, 0xC4, 0xFC, 0x46, 0x0F,
  28542. 0xA8, 0x4A, 0x2D, 0x15, 0x21, 0xBD, 0x0B, 0x42,
  28543. 0x86, 0x1A, 0xCF, 0x24, 0x57, 0x9D, 0x8E, 0xE8,
  28544. 0x94, 0x63, 0xB4, 0x78, 0xE2, 0xE9, 0x53, 0xC7,
  28545. 0xD5, 0xCD, 0xA4, 0xD3, 0x07, 0xD1, 0x68, 0xCF,
  28546. 0xC6, 0x79, 0xB1, 0xA3, 0x79, 0x1F, 0x05, 0x83,
  28547. 0x82, 0x86, 0x0B, 0x1C, 0x77, 0x52, 0x87, 0x89,
  28548. 0xC6, 0x96, 0x4B, 0xCC, 0x40, 0x8A, 0x21, 0xCA,
  28549. 0x4E, 0x42, 0x8F, 0xE7, 0x33, 0x9F, 0x7C, 0x08,
  28550. 0x76, 0xA8, 0x58, 0xAB, 0xA9, 0x44, 0xB7, 0x3F,
  28551. 0x9C, 0xC9, 0xB8, 0xAC, 0xC4, 0xB7, 0x9A, 0x9F,
  28552. 0x72, 0xF7, 0xA0, 0x47, 0x72, 0x57, 0xB9, 0xF3,
  28553. 0x26, 0xE4, 0x4C, 0x2C, 0x5F, 0x8B, 0x8A, 0x17,
  28554. 0x58, 0x81, 0x42, 0xB0, 0x91, 0xA3, 0x12, 0x31,
  28555. 0xD0, 0x98, 0xB0, 0x57, 0x30, 0x45, 0x62, 0x90,
  28556. 0x6E, 0x96, 0x05, 0x47, 0x3D, 0x28, 0xB7, 0xB4,
  28557. 0x33, 0x2A, 0xB4, 0x49, 0x82, 0x48, 0x7B, 0x04,
  28558. 0x9E, 0x41, 0x93, 0x74, 0xA4, 0x8E, 0xAC, 0x6A,
  28559. 0xB8, 0xCB, 0x42, 0x6D, 0x13, 0xA4, 0x86, 0x28,
  28560. 0x28, 0x43, 0x02, 0xFB, 0x72, 0x71, 0xD1, 0x54,
  28561. 0xCA, 0x09, 0x06, 0xD8, 0xE2, 0x0F, 0x1D, 0x97,
  28562. 0xBE, 0x9C, 0x82, 0x2E, 0x78, 0x83, 0x79, 0x32,
  28563. 0xEB, 0x28, 0xEE, 0xB0, 0x83, 0x71, 0xBB, 0xC6,
  28564. 0x3A, 0x7A, 0x96, 0x6C, 0xAC, 0x7A, 0x05, 0xB5,
  28565. 0x50, 0x3C, 0x55, 0x1A, 0xEF, 0x80, 0x15, 0x14,
  28566. 0xE1, 0x6A, 0xF6, 0x81, 0x02, 0x64, 0x6C, 0x66,
  28567. 0x9D, 0xD8, 0xA7, 0xE8, 0xA4, 0x11, 0x39, 0x54,
  28568. 0x62, 0xC7, 0xD4, 0x83, 0x8B, 0x91, 0x96, 0x96,
  28569. 0x54, 0x16, 0x59, 0x2B, 0xCF, 0xAC, 0x97, 0x87,
  28570. 0x0C, 0x23, 0x58, 0xA8, 0x59, 0x5B, 0x2F, 0x43,
  28571. 0x55, 0xF0, 0xFA, 0x10, 0x43, 0xA1, 0x1F, 0x62,
  28572. 0x86, 0x2F, 0xD5, 0xD9, 0x7A, 0x9D, 0xE2, 0x1B,
  28573. 0xD3, 0x39, 0x85, 0x5E, 0x17, 0x8C, 0x97, 0xD3,
  28574. 0x77, 0xD1, 0xD1, 0x46, 0xEC, 0x6A, 0x96, 0xE4,
  28575. 0xFB, 0xB7, 0x15, 0xC4, 0xC0, 0x22, 0x84, 0x55,
  28576. 0xBB, 0x64, 0xA3, 0x88, 0xDC, 0xC4, 0xD3, 0x91,
  28577. 0x3A, 0x76, 0x09, 0x57, 0xD1, 0xA6, 0x9F, 0xEA,
  28578. 0xAA, 0x08, 0x92, 0x29, 0xB8, 0xCA, 0xD0, 0xBC,
  28579. 0xC9, 0x38, 0xC7, 0x80, 0x69, 0x8A, 0x5C, 0xE1,
  28580. 0x9B, 0xC5, 0x21, 0x44, 0xDB, 0x46, 0x3B, 0x6D,
  28581. 0x55, 0x4F, 0xC4, 0xEC, 0x5E, 0x18, 0x64, 0xC4,
  28582. 0xC6, 0x88, 0x75, 0x97, 0x50, 0x0F, 0x40, 0x94,
  28583. 0x18, 0xB3, 0xA3, 0x3F, 0x2C, 0x88, 0xB2, 0x6E,
  28584. 0xD9, 0x83, 0x1D, 0xB4, 0x65, 0x08, 0xE8, 0x9F,
  28585. 0x8C, 0x33, 0x5B, 0xE1, 0x94, 0x56, 0x12, 0x05,
  28586. 0x22, 0xA8, 0xB4, 0x43, 0xFE, 0xF2, 0x1A, 0x7B,
  28587. 0xC3, 0x05, 0x9A, 0xFB, 0x4A, 0xAA, 0xD5, 0x0E,
  28588. 0x6C, 0x72, 0x8B, 0xEA, 0x8A, 0xC5, 0xDC, 0x7C,
  28589. 0x6D, 0x06, 0x94, 0x76, 0x03, 0x51, 0x0A, 0x84,
  28590. 0x90, 0xAF, 0xCE, 0x30, 0x4D, 0x57, 0xA7, 0x25,
  28591. 0xC4, 0x3B, 0x5A, 0xA3, 0x71, 0x3B, 0xA0, 0x44,
  28592. 0x98, 0x75, 0xE5, 0x67, 0x55, 0x13, 0x22, 0x60,
  28593. 0x54, 0x63, 0x6C, 0xF8, 0x9E, 0x59, 0x5A, 0xC5,
  28594. 0xAE, 0xE5, 0x66, 0x78, 0x4B, 0x80, 0xFA, 0x39,
  28595. 0x64, 0x0B, 0x74, 0x0F, 0x47, 0x12, 0x22, 0x60,
  28596. 0x25, 0x11, 0x00, 0xFA, 0x8E, 0xE0, 0xF1, 0x65,
  28597. 0x27, 0x11, 0x40, 0x0C, 0x68, 0x16, 0x38, 0x3C,
  28598. 0xC5, 0xE6, 0xDA, 0xB7, 0x9C, 0xDC, 0x3E, 0x41,
  28599. 0x98, 0x37, 0x48, 0x15, 0xB2, 0xBB, 0xA2, 0x0E,
  28600. 0xFC, 0x14, 0x25, 0xCE, 0xD1, 0x9B, 0x60, 0x19,
  28601. 0x77, 0x08, 0x08, 0x00, 0xCF, 0x54, 0x9C, 0xC4,
  28602. 0x80, 0x7D, 0xE6, 0xCA, 0x84, 0xE5, 0xD3, 0xC2,
  28603. 0xCF, 0x8B, 0x11, 0xEB, 0x68, 0x56, 0xE7, 0x1C,
  28604. 0x6B, 0xA5, 0xD2, 0x96, 0x45, 0x46, 0xAA, 0x0C,
  28605. 0x82, 0x53, 0x10, 0xF7, 0x79, 0xED, 0x69, 0x53,
  28606. 0xD9, 0x22, 0x5E, 0x11, 0xF0, 0x57, 0x93, 0x48,
  28607. 0x4C, 0x6B, 0x47, 0xB6, 0xE3, 0xD0, 0x4A, 0x4B,
  28608. 0x27, 0xB7, 0x86, 0x71, 0x46, 0xF6, 0xA0, 0x64,
  28609. 0xB9, 0x8B, 0x8B, 0xDD, 0x2B, 0x69, 0x94, 0xBA,
  28610. 0x20, 0xF8, 0x14, 0x95, 0xB3, 0x74, 0x1C, 0xE1,
  28611. 0xD3, 0x07, 0xE5, 0xEC, 0x0A, 0x7C, 0x20, 0x20,
  28612. 0x2B, 0x81, 0xA1, 0x04, 0x02, 0x83, 0x4D, 0x23,
  28613. 0x83, 0xEC, 0xB2, 0x33, 0x3B, 0x2C, 0x93, 0xD6,
  28614. 0xC4, 0x37, 0xC3, 0x17, 0x58, 0xDF, 0x92, 0x21,
  28615. 0xA3, 0xFA, 0x60, 0x24, 0x81, 0x29, 0x29, 0x62,
  28616. 0x81, 0x17, 0xCB, 0xC4, 0x79, 0x22, 0x76, 0xDA,
  28617. 0x84, 0x0F, 0xAB, 0x37, 0x03, 0x54, 0x65, 0x23,
  28618. 0x0C, 0x7A, 0x79, 0xEB, 0xB1, 0xA8, 0xC2, 0xB4,
  28619. 0x49, 0x0C, 0x2C, 0x1C, 0xB8, 0xE8, 0xB5, 0x73,
  28620. 0x5B, 0xC0, 0x72, 0xFC, 0x4D, 0x0F, 0x02, 0xB5,
  28621. 0x5B, 0x01, 0x3A, 0x53, 0x86, 0x4B, 0x82, 0x5A,
  28622. 0x85, 0xE9, 0x52, 0x03, 0x76, 0xC4, 0x4A, 0x9D,
  28623. 0xBA, 0x2C, 0xE9, 0x06, 0x8F, 0x25, 0x7A, 0x11,
  28624. 0xCF, 0xB6, 0x46, 0x6A, 0xC5, 0xAB, 0xBC, 0x85,
  28625. 0xCE, 0x80, 0x0A, 0x13, 0xB9, 0x64, 0x4D, 0x6E,
  28626. 0x04, 0x70, 0x1D, 0x23, 0xA9, 0x9A, 0x7C, 0x1A,
  28627. 0x5A, 0xEA, 0xBE, 0x2D, 0xB1, 0x34, 0x16, 0xD0,
  28628. 0xC3, 0x5C, 0x6C, 0x04, 0x98, 0x8B, 0x39, 0x91,
  28629. 0x6C, 0x46, 0x67, 0x6C, 0x66, 0x51, 0xF3, 0x5E,
  28630. 0xC2, 0x28, 0x13, 0xCF, 0xE3, 0x84, 0x40, 0x69,
  28631. 0x2F, 0x43, 0x29, 0xC5, 0x85, 0x03, 0x27, 0x30,
  28632. 0xC6, 0x94, 0xF2, 0xB2, 0x41, 0x03, 0xFB, 0x78,
  28633. 0xE4, 0x3A, 0x49, 0x52, 0x70, 0x03, 0xF4, 0xFA,
  28634. 0x5B, 0x73, 0x49, 0x2A, 0x50, 0x71, 0x83, 0x55,
  28635. 0x36, 0x26, 0x78, 0x26, 0x97, 0x37, 0x65, 0x7B,
  28636. 0x7A, 0x24, 0x03, 0xD4, 0x15, 0x09, 0xD6, 0xEC,
  28637. 0x9E, 0x51, 0x65, 0x51, 0x95, 0x56, 0xCB, 0x94,
  28638. 0x92, 0xCD, 0x6D, 0x04, 0x6F, 0x2B, 0x4C, 0x91,
  28639. 0x4A, 0xF6, 0x3C, 0xBF, 0xCC, 0xA0, 0xB4, 0xCC,
  28640. 0x44, 0x6A, 0x54, 0x69, 0x54, 0x59, 0x41, 0x66,
  28641. 0x8A, 0xBD, 0x9A, 0x26, 0x7A, 0xF2, 0x41, 0x28,
  28642. 0x33, 0xD9, 0x67, 0xB8, 0xAB, 0xB4, 0x34, 0xD7,
  28643. 0xA0, 0xAF, 0x5C, 0xAB, 0x11, 0x34, 0xB0, 0x9E,
  28644. 0xDB, 0x7A, 0xA1, 0xC1, 0x60, 0x0A, 0xF1, 0x5E,
  28645. 0xF6, 0x15, 0x27, 0xC8, 0xC1, 0xA1, 0x59, 0x40,
  28646. 0xCA, 0xB1, 0x60, 0x35, 0xF0, 0x5C, 0x71, 0x77,
  28647. 0x15, 0x90, 0x9D, 0x73, 0x2D, 0x1E, 0x9B, 0x4C,
  28648. 0x59, 0x2B, 0xB3, 0x86, 0xA5, 0xC1, 0xF3, 0xB9,
  28649. 0x89, 0x17, 0x58, 0x82, 0x05, 0xD2, 0xA8, 0xB5,
  28650. 0x03, 0x8A, 0x52, 0x4B, 0x99, 0x52, 0xD5, 0x9D,
  28651. 0x85, 0xDB, 0x27, 0x3C, 0xA1, 0xA7, 0x87, 0xF0,
  28652. 0x1F, 0x9B, 0xCB, 0x07, 0xDA, 0xC3, 0x3B, 0xC4,
  28653. 0xF0, 0xB4, 0x33, 0x36, 0x38, 0xCD, 0x3A, 0x60,
  28654. 0x1D, 0x35, 0x63, 0xF3, 0x53, 0xB5, 0xDF, 0xAA,
  28655. 0xA6, 0x2C, 0x68, 0xBF, 0x92, 0xA4, 0x79, 0xD8,
  28656. 0xB9, 0x4F, 0x81, 0x54, 0x31, 0x72, 0xB8, 0x36,
  28657. 0x26, 0xCC, 0x3B, 0x48, 0x72, 0x76, 0xAA, 0x5C,
  28658. 0xCB, 0x4D, 0x10, 0xBB, 0x18, 0xC9, 0x9F, 0x48,
  28659. 0x2A, 0x88, 0x26, 0x1C, 0x21, 0xA7, 0x90, 0x0F,
  28660. 0x60, 0x73, 0xB7, 0x06, 0xAC, 0xC5, 0xD2, 0xD7,
  28661. 0x54, 0x10, 0xB3, 0xA8, 0x84, 0x06, 0x95, 0x87,
  28662. 0x2B, 0xAC, 0xC8, 0xAA, 0x36, 0x60, 0xA4, 0x7D,
  28663. 0x6B, 0x7A, 0x5A, 0xA7, 0x9A, 0x5D, 0x28, 0xB2,
  28664. 0x6B, 0xFF, 0x80, 0x63, 0x14, 0xE6, 0x92, 0x93,
  28665. 0x30, 0x4A, 0xD0, 0x13, 0xB4, 0xE6, 0x75, 0xAE,
  28666. 0x62, 0x0B, 0x52, 0xEF, 0x30, 0x79, 0x00, 0x33,
  28667. 0xCC, 0x42, 0x78, 0xC0, 0x38, 0x50, 0xB4, 0xE6,
  28668. 0x0B, 0x39, 0x63, 0x46, 0x11, 0xDC, 0x3A, 0x5F,
  28669. 0x4C, 0x79, 0x66, 0xA9, 0xF4, 0x92, 0x0B, 0xF7,
  28670. 0x08, 0xBA, 0x8A, 0x01, 0x49, 0x1C, 0x7C, 0xEC,
  28671. 0x97, 0x40, 0xFA, 0x97, 0x6E, 0x8E, 0x76, 0x13,
  28672. 0x00, 0x60, 0x5F, 0x75, 0x20, 0x88, 0x56, 0x56,
  28673. 0x5F, 0x4F, 0x55, 0x60, 0x8F, 0x61, 0x37, 0x92,
  28674. 0x99, 0xA6, 0x4C, 0x96, 0x6B, 0xAD, 0x4A, 0x0E,
  28675. 0x87, 0x92, 0xAD, 0x3D, 0x46, 0x86, 0x41, 0x28,
  28676. 0x37, 0x61, 0xA5, 0x39, 0x99, 0x25, 0x86, 0x63,
  28677. 0x30, 0xB5, 0xBA, 0x37, 0x07, 0xE8, 0x54, 0xA6,
  28678. 0x3F, 0xE7, 0xAA, 0x7F, 0x81, 0x1A, 0xA4, 0x57,
  28679. 0x54, 0x08, 0xC8, 0xC2, 0x06, 0xBB, 0x39, 0x53,
  28680. 0xA7, 0x66, 0x29, 0x9A, 0x5C, 0x26, 0xC6, 0x7A,
  28681. 0x4E, 0x58, 0x21, 0x05, 0x79, 0xB3, 0x10, 0xF7,
  28682. 0x99, 0xB0, 0x97, 0x9B, 0xB9, 0x45, 0x8A, 0xEC,
  28683. 0x21, 0x50, 0xAD, 0x27, 0x3F, 0xEB, 0xCB, 0x3E,
  28684. 0xF3, 0xB6, 0x0B, 0x38, 0x56, 0xA7, 0x16, 0xB9,
  28685. 0x78, 0x3D, 0xF8, 0x08, 0xC6, 0x9C, 0x06, 0x12,
  28686. 0xA0, 0x9E, 0xA5, 0x93, 0x16, 0x64, 0x65, 0x87,
  28687. 0x69, 0x2D, 0x3C, 0x8E, 0x8C, 0x73, 0x9F, 0xB3,
  28688. 0x78, 0x27, 0x74, 0x15, 0x61, 0xD5, 0xE7, 0x66,
  28689. 0x67, 0x51, 0x82, 0xFF, 0x83, 0xC9, 0x1C, 0xF0,
  28690. 0x33, 0x65, 0x88, 0x23, 0xD8, 0xFA, 0x91, 0xE1,
  28691. 0x05, 0xFC, 0x14, 0xDA, 0x24, 0xF1, 0xC4, 0x1E,
  28692. 0xB3, 0xCC, 0xE3, 0xC7, 0x47, 0xEE, 0x30, 0x0C,
  28693. 0x31, 0x1F, 0x27, 0x85, 0x47, 0x7A, 0x01, 0x33,
  28694. 0xF7, 0x13, 0xB0, 0x28, 0x04, 0x67, 0x4E, 0x7A,
  28695. 0x86, 0x26, 0xED, 0x79, 0xD4, 0x51, 0x14, 0x08,
  28696. 0x00, 0xE0, 0x3B, 0x59, 0xB9, 0x56, 0xF8, 0x21,
  28697. 0x0E, 0x55, 0x60, 0x67, 0x40, 0x7D, 0x13, 0xDC,
  28698. 0x90, 0xFA, 0x9E, 0x8B, 0x87, 0x2B, 0xFB, 0x8F
  28699. };
  28700. const byte kyber768_ct[] = {
  28701. 0xB9, 0x54, 0x84, 0x84, 0xA4, 0x87, 0x3B, 0x1F,
  28702. 0x2C, 0xFF, 0x66, 0xA3, 0x23, 0x09, 0x34, 0x60,
  28703. 0x05, 0xEB, 0x7C, 0x04, 0x44, 0x9E, 0xAD, 0xC4,
  28704. 0x66, 0xBC, 0x47, 0x0E, 0x71, 0xED, 0x6C, 0x77,
  28705. 0x1C, 0x2C, 0xF3, 0x92, 0x5E, 0xEB, 0x78, 0x91,
  28706. 0x56, 0xAC, 0xF5, 0x8D, 0x13, 0x21, 0xA5, 0x23,
  28707. 0xE4, 0xDD, 0xB1, 0x58, 0x4C, 0x6E, 0x3F, 0x4A,
  28708. 0x3E, 0xE5, 0xCC, 0x9C, 0x00, 0x7A, 0x00, 0xFF,
  28709. 0xA2, 0xBF, 0xF2, 0xD3, 0x92, 0xD2, 0x9D, 0xCB,
  28710. 0xEF, 0x44, 0x6D, 0x16, 0x51, 0xC9, 0xB3, 0x63,
  28711. 0x8B, 0x0F, 0xB7, 0xEB, 0xEC, 0xE5, 0xB0, 0x59,
  28712. 0xC9, 0x76, 0x21, 0xFF, 0x7A, 0x1E, 0x1C, 0xE7,
  28713. 0x7A, 0xD5, 0x46, 0x1C, 0x51, 0xE1, 0x3A, 0xF7,
  28714. 0x07, 0x89, 0xE4, 0x88, 0x58, 0x98, 0xEA, 0x20,
  28715. 0xFA, 0x17, 0x85, 0xA0, 0x3E, 0x3E, 0xF8, 0xA2,
  28716. 0x98, 0x33, 0xDA, 0x8D, 0x4D, 0x28, 0x32, 0x53,
  28717. 0x74, 0xE4, 0xF4, 0x67, 0x1B, 0xDA, 0x5B, 0x8F,
  28718. 0xAE, 0x9C, 0x9F, 0x68, 0x88, 0xAD, 0x5B, 0xCD,
  28719. 0x0B, 0x91, 0x16, 0x47, 0x35, 0x73, 0xFB, 0x78,
  28720. 0xDD, 0xA7, 0x9A, 0x27, 0x8D, 0x0D, 0x6A, 0xFF,
  28721. 0x71, 0xD0, 0x37, 0x7F, 0xB5, 0x0C, 0x47, 0xEC,
  28722. 0xBD, 0xB4, 0xB1, 0x42, 0x7D, 0xF8, 0xD4, 0x55,
  28723. 0xCC, 0x34, 0x29, 0xCC, 0x84, 0xA1, 0x4A, 0x99,
  28724. 0xFB, 0xE4, 0xAD, 0x1E, 0xB3, 0xE8, 0x07, 0x48,
  28725. 0x3E, 0x62, 0x26, 0xC7, 0x59, 0xAA, 0x0A, 0x6C,
  28726. 0x96, 0x04, 0x41, 0x19, 0x76, 0x8A, 0xB5, 0xA9,
  28727. 0x9B, 0x6F, 0xCD, 0x5B, 0x00, 0x7A, 0x18, 0x82,
  28728. 0xF4, 0xEC, 0xD3, 0x71, 0x39, 0x64, 0x33, 0x32,
  28729. 0x74, 0x4D, 0x08, 0x77, 0xB4, 0x47, 0x01, 0xB0,
  28730. 0xE3, 0xB5, 0xD1, 0x22, 0x8A, 0xB1, 0xE3, 0xA7,
  28731. 0xF9, 0x94, 0xA2, 0xC0, 0xC3, 0xC1, 0x6C, 0x1E,
  28732. 0x6E, 0x02, 0xDF, 0xBF, 0x44, 0x44, 0x4F, 0x6A,
  28733. 0x9D, 0x58, 0x25, 0x49, 0x2A, 0x10, 0x88, 0xD1,
  28734. 0x91, 0xB4, 0x62, 0x22, 0x76, 0x14, 0x30, 0xDD,
  28735. 0x50, 0xEB, 0xF1, 0x1C, 0xAB, 0xE9, 0x95, 0x5C,
  28736. 0x5A, 0x10, 0x79, 0xC4, 0xB7, 0xF2, 0xF4, 0x8F,
  28737. 0xB6, 0xF3, 0x89, 0x3A, 0xBE, 0x38, 0xDD, 0xB0,
  28738. 0x4A, 0xB1, 0x8C, 0xAE, 0x8D, 0x50, 0x70, 0xBD,
  28739. 0x9E, 0x05, 0x71, 0x0A, 0x7B, 0x4C, 0xBE, 0xE2,
  28740. 0xAE, 0x3F, 0xEC, 0xCF, 0xFA, 0x26, 0x65, 0xBE,
  28741. 0xC9, 0xD0, 0xBC, 0x6B, 0x57, 0x34, 0xF2, 0x4C,
  28742. 0x29, 0xB5, 0x9E, 0xF8, 0x21, 0x99, 0x77, 0x58,
  28743. 0x73, 0x2B, 0x04, 0x72, 0xFE, 0x63, 0x81, 0x63,
  28744. 0xC0, 0xF8, 0x67, 0x64, 0x48, 0x12, 0xBB, 0x95,
  28745. 0x83, 0xEE, 0xB7, 0xED, 0x64, 0x73, 0xEE, 0x97,
  28746. 0x23, 0x35, 0xA5, 0x06, 0xCE, 0x64, 0x94, 0x90,
  28747. 0x49, 0x26, 0x3E, 0x32, 0x77, 0x92, 0x88, 0xF8,
  28748. 0xD0, 0x16, 0x5E, 0xE5, 0x22, 0x79, 0xF7, 0x98,
  28749. 0xE0, 0x2F, 0x8B, 0xDE, 0xA2, 0x5F, 0x67, 0x8E,
  28750. 0x2A, 0xB9, 0xE8, 0x8A, 0x77, 0xD2, 0x89, 0x2B,
  28751. 0x79, 0x69, 0x22, 0x1B, 0x9D, 0x81, 0xFD, 0xA7,
  28752. 0x6B, 0x91, 0xD1, 0x8F, 0xE6, 0xBF, 0x14, 0xA1,
  28753. 0xC2, 0x3A, 0xAF, 0x42, 0xCB, 0x95, 0x53, 0x44,
  28754. 0x82, 0xAC, 0x2C, 0x56, 0x9C, 0x01, 0xE0, 0xC2,
  28755. 0x0E, 0x65, 0x8A, 0x1E, 0x17, 0xC1, 0xD8, 0xB3,
  28756. 0x8F, 0xF6, 0x44, 0xD5, 0x3F, 0x41, 0x10, 0x65,
  28757. 0x55, 0x14, 0x09, 0xB7, 0x4A, 0x7A, 0x26, 0x04,
  28758. 0x42, 0xE1, 0x96, 0xE1, 0x2B, 0x48, 0x33, 0x96,
  28759. 0x86, 0x3E, 0x6D, 0x67, 0xDC, 0x4E, 0x54, 0x07,
  28760. 0x4C, 0x73, 0x97, 0x9E, 0x69, 0x83, 0x07, 0xCF,
  28761. 0xA6, 0x37, 0xA3, 0xFD, 0xBB, 0xD0, 0xAA, 0xA0,
  28762. 0xA7, 0x97, 0x0B, 0x38, 0xEE, 0x38, 0xB1, 0x85,
  28763. 0xA6, 0x68, 0x71, 0x29, 0xE6, 0xA2, 0xFF, 0x62,
  28764. 0x4F, 0x65, 0x6C, 0x53, 0x80, 0x5B, 0x72, 0xF4,
  28765. 0xD2, 0x41, 0x50, 0x59, 0x01, 0x22, 0xC8, 0x25,
  28766. 0x96, 0x5C, 0xE1, 0x24, 0x7D, 0x3B, 0x4A, 0x10,
  28767. 0xEE, 0x82, 0x47, 0xDF, 0xB5, 0x60, 0x25, 0x4F,
  28768. 0x79, 0x91, 0x09, 0x82, 0xED, 0x30, 0xE7, 0xF3,
  28769. 0xE6, 0x7A, 0x9F, 0xD1, 0xF0, 0xA7, 0x15, 0x60,
  28770. 0xB9, 0xDF, 0xAB, 0x4A, 0xCC, 0x0A, 0x78, 0xC9,
  28771. 0x9A, 0x7F, 0x15, 0x26, 0x81, 0x2C, 0x16, 0xA7,
  28772. 0xDC, 0x4A, 0xA8, 0xBB, 0x9F, 0xA6, 0xCA, 0xA6,
  28773. 0x80, 0xE3, 0x60, 0x2D, 0x13, 0x3C, 0xCA, 0xE9,
  28774. 0xD1, 0xAD, 0x39, 0xFD, 0xA6, 0xA3, 0xDA, 0x7F,
  28775. 0xF1, 0x0A, 0x25, 0xBD, 0x8F, 0x05, 0xBC, 0x17,
  28776. 0x98, 0xCD, 0x01, 0x2F, 0x63, 0x16, 0xCB, 0xF1,
  28777. 0xC9, 0x9F, 0x75, 0x98, 0x62, 0x43, 0x3B, 0xB7,
  28778. 0x55, 0xCC, 0x5C, 0x40, 0xF3, 0x87, 0x55, 0xEA,
  28779. 0x03, 0xA5, 0x00, 0xB4, 0x7F, 0xDD, 0x2D, 0xBD,
  28780. 0xFE, 0x05, 0x60, 0x69, 0x42, 0xB5, 0x99, 0x1E,
  28781. 0x0E, 0xA8, 0xEC, 0x04, 0x7E, 0x6F, 0x77, 0xD4,
  28782. 0x92, 0x8F, 0xC7, 0xE0, 0xB5, 0x06, 0x05, 0x76,
  28783. 0xFA, 0x45, 0x25, 0xB1, 0xD1, 0xF4, 0x01, 0x3A,
  28784. 0x49, 0xA9, 0x93, 0x77, 0x4B, 0xEE, 0xA2, 0xE8,
  28785. 0xAA, 0x39, 0xBD, 0xD9, 0x90, 0xEB, 0x0F, 0xB5,
  28786. 0xFA, 0xB6, 0xC4, 0x10, 0x79, 0xF5, 0x6A, 0x32,
  28787. 0xFD, 0x9C, 0x38, 0xAC, 0xEC, 0x84, 0xF1, 0x62,
  28788. 0xC6, 0x71, 0xE9, 0xDC, 0x55, 0xD8, 0x40, 0x1D,
  28789. 0x29, 0x8A, 0x97, 0x02, 0x08, 0x44, 0x23, 0x27,
  28790. 0x51, 0x13, 0xC3, 0x46, 0x62, 0x4A, 0x8F, 0x0E,
  28791. 0x79, 0xFC, 0xC3, 0x59, 0x33, 0xC7, 0x75, 0x7F,
  28792. 0x24, 0xE0, 0x7A, 0x25, 0x7A, 0xBF, 0xEA, 0x28,
  28793. 0xDA, 0x2B, 0xDE, 0x70, 0x73, 0xAE, 0xC1, 0x33,
  28794. 0xE6, 0x55, 0xC5, 0xA4, 0xB8, 0x94, 0xC5, 0x95,
  28795. 0x10, 0x2E, 0x7D, 0x13, 0x80, 0x96, 0xCD, 0x6F,
  28796. 0x97, 0xF5, 0x62, 0x27, 0x62, 0x4D, 0xCC, 0xAB,
  28797. 0xFB, 0x9D, 0xCF, 0xE9, 0x0E, 0x26, 0x5A, 0x04,
  28798. 0x9B, 0x90, 0xA3, 0x2E, 0x54, 0xD4, 0x81, 0xE2,
  28799. 0xF3, 0x7B, 0x00, 0x2C, 0xFE, 0x9C, 0xC9, 0xCD,
  28800. 0x24, 0x3F, 0x83, 0xE3, 0x62, 0x09, 0x4C, 0xFB,
  28801. 0xF7, 0x0E, 0x1A, 0x94, 0x79, 0xC5, 0x7E, 0x30,
  28802. 0xB4, 0xB9, 0xEB, 0x38, 0x31, 0x6F, 0xA9, 0xE9,
  28803. 0xBE, 0xE1, 0x90, 0xC6, 0xE8, 0xA8, 0xD4, 0x5C,
  28804. 0xA3, 0x40, 0x76, 0xF4, 0x84, 0x0D, 0xBC, 0x9F,
  28805. 0x42, 0x5C, 0xD5, 0x70, 0x98, 0x8E, 0x58, 0x86,
  28806. 0xC2, 0x7B, 0x64, 0xC2, 0x28, 0x15, 0x8D, 0xA7,
  28807. 0xE2, 0x82, 0xF6, 0xC0, 0x09, 0xC1, 0x61, 0xBA,
  28808. 0xC8, 0xE4, 0xDE, 0x67, 0xCD, 0x46, 0xAB, 0x9D,
  28809. 0x5A, 0xDC, 0xF7, 0xF5, 0xF6, 0xA0, 0x6D, 0x36,
  28810. 0xB5, 0xEC, 0xA3, 0x41, 0x7C, 0x40, 0xDA, 0xAC,
  28811. 0x80, 0x55, 0xFF, 0x3D, 0x77, 0x3D, 0x14, 0x1F,
  28812. 0x50, 0x40, 0x58, 0x74, 0x48, 0xF0, 0xA9, 0x10,
  28813. 0x7C, 0x44, 0xED, 0x67, 0x1A, 0x34, 0xF2, 0x5C,
  28814. 0x4F, 0x86, 0x6F, 0xCA, 0xA6, 0xC1, 0x42, 0x4F,
  28815. 0xE9, 0x59, 0x59, 0x9E, 0xFE, 0x31, 0x63, 0x56,
  28816. 0xF9, 0x05, 0xDD, 0xD6, 0x8B, 0x27, 0xA4, 0xA0,
  28817. 0xBB, 0x17, 0x28, 0x52, 0x0D, 0x5D, 0x36, 0x77,
  28818. 0x7E, 0x22, 0xF1, 0xFE, 0xDE, 0x19, 0x3B, 0x07,
  28819. 0xB3, 0xA2, 0xD2, 0x09, 0x9C, 0x41, 0x93, 0x1E,
  28820. 0xB4, 0xE0, 0xB0, 0xFA, 0x3F, 0xA7, 0xC6, 0x66,
  28821. 0xD8, 0xA7, 0xFD, 0x90, 0xCD, 0x49, 0xA2, 0xEE,
  28822. 0x25, 0x66, 0xCE, 0x31, 0x9E, 0x03, 0x35, 0x23,
  28823. 0x32, 0x2E, 0xDA, 0x7C, 0xD3, 0x14, 0x98, 0x88,
  28824. 0xBA, 0x9B, 0xEF, 0x0C, 0x5F, 0x82, 0x6F, 0x3C,
  28825. 0x5A, 0xB8, 0x6D, 0x2D, 0xD0, 0x84, 0xA3, 0xC3,
  28826. 0x16, 0xDE, 0x98, 0xCB, 0x42, 0x68, 0x4E, 0x0D,
  28827. 0x64, 0x41, 0xE0, 0x73, 0xA5, 0x9F, 0x27, 0x67,
  28828. 0x55, 0x1E, 0x56, 0x48, 0xBF, 0xBE, 0x49, 0x8E,
  28829. 0xF3, 0xC0, 0x3B, 0x65, 0xD2, 0x9F, 0x5F, 0xA8,
  28830. 0x4D, 0x99, 0x79, 0x9F, 0x1D, 0x32, 0xC2, 0x26,
  28831. 0xFB, 0xF7, 0x39, 0x65, 0x7F, 0x02, 0x61, 0xE8,
  28832. 0x96, 0x02, 0xF7, 0xBD, 0x07, 0x73, 0x41, 0x6D,
  28833. 0xFE, 0xEA, 0x80, 0x1C, 0xE1, 0xA7, 0x55, 0x3F,
  28834. 0x1E, 0xF9, 0x34, 0x1A, 0x5F, 0x74, 0x09, 0x25,
  28835. 0x80, 0x75, 0xBE, 0xF5, 0x82, 0x83, 0x35, 0x66,
  28836. 0x1A, 0x8C, 0xB1, 0x50, 0xD5, 0x37, 0x0B, 0xEC
  28837. };
  28838. const byte kyber768_ss[] = {
  28839. 0x44, 0xF6, 0x94, 0xE4, 0x78, 0xEB, 0xAC, 0x4A,
  28840. 0x55, 0x6A, 0x38, 0xA2, 0x5C, 0x95, 0x9B, 0x62,
  28841. 0xAC, 0xC7, 0x2E, 0x17, 0xCF, 0x04, 0xB4, 0xD4,
  28842. 0x7E, 0x54, 0xB0, 0xB7, 0xFE, 0xAC, 0xEB, 0x56
  28843. };
  28844. #endif
  28845. ret = wc_KyberKey_Init(KYBER768, &key, HEAP_HINT, INVALID_DEVID);
  28846. if (ret != 0)
  28847. return -20201;
  28848. ret = wc_KyberKey_MakeKeyWithRandom(&key, kyber768_rand,
  28849. sizeof(kyber768_rand));
  28850. if (ret != 0)
  28851. return -20202;
  28852. ret = wc_KyberKey_EncodePublicKey(&key, pub, sizeof(pub));
  28853. if (ret != 0)
  28854. return -20203;
  28855. ret = wc_KyberKey_EncodePrivateKey(&key, priv, sizeof(priv));
  28856. if (ret != 0)
  28857. return -20204;
  28858. if (XMEMCMP(pub, kyber768_pk, sizeof(kyber768_pk)) != 0)
  28859. return -20205;
  28860. if (XMEMCMP(priv, kyber768_sk, sizeof(kyber768_sk)) != 0)
  28861. return -20206;
  28862. ret = wc_KyberKey_EncapsulateWithRandom(&key, ct, ss, kyber768enc_rand,
  28863. sizeof(kyber768enc_rand));
  28864. if (ret != 0)
  28865. return -20207;
  28866. if (XMEMCMP(ct, kyber768_ct, sizeof(kyber768_ct)) != 0)
  28867. return -20208;
  28868. if (XMEMCMP(ss, kyber768_ss, sizeof(kyber768_ss)) != 0)
  28869. return -20209;
  28870. ret = wc_KyberKey_Decapsulate(&key, ss_dec, ct, sizeof(kyber768_ct));
  28871. if (ret != 0)
  28872. return -20210;
  28873. if (XMEMCMP(ss_dec, kyber768_ss, sizeof(kyber768_ss)) != 0)
  28874. return -20211;
  28875. wc_KyberKey_Free(&key);
  28876. return 0;
  28877. }
  28878. #endif /* WOLFSSL_KYBER768 */
  28879. #ifdef WOLFSSL_KYBER1024
  28880. static int kyber1024_kat(void)
  28881. {
  28882. KyberKey key;
  28883. int ret;
  28884. byte priv[KYBER1024_PRIVATE_KEY_SIZE];
  28885. byte pub[KYBER1024_PUBLIC_KEY_SIZE];
  28886. byte ct[KYBER1024_CIPHER_TEXT_SIZE];
  28887. byte ss[KYBER_SS_SZ];
  28888. byte ss_dec[KYBER_SS_SZ];
  28889. const byte kyber1024_rand[] = {
  28890. 0x7c, 0x99, 0x35, 0xa0, 0xb0, 0x76, 0x94, 0xaa,
  28891. 0x0c, 0x6d, 0x10, 0xe4, 0xdb, 0x6b, 0x1a, 0xdd,
  28892. 0x2f, 0xd8, 0x1a, 0x25, 0xcc, 0xb1, 0x48, 0x03,
  28893. 0x2d, 0xcd, 0x73, 0x99, 0x36, 0x73, 0x7f, 0x2d,
  28894. 0x86, 0x26, 0xED, 0x79, 0xD4, 0x51, 0x14, 0x08,
  28895. 0x00, 0xE0, 0x3B, 0x59, 0xB9, 0x56, 0xF8, 0x21,
  28896. 0x0E, 0x55, 0x60, 0x67, 0x40, 0x7D, 0x13, 0xDC,
  28897. 0x90, 0xFA, 0x9E, 0x8B, 0x87, 0x2B, 0xFB, 0x8F
  28898. };
  28899. const byte kyber1024enc_rand[] = {
  28900. 0x14, 0x7c, 0x03, 0xf7, 0xa5, 0xbe, 0xbb, 0xa4,
  28901. 0x06, 0xc8, 0xfa, 0xe1, 0x87, 0x4d, 0x7f, 0x13,
  28902. 0xc8, 0x0e, 0xfe, 0x79, 0xa3, 0xa9, 0xa8, 0x74,
  28903. 0xcc, 0x09, 0xfe, 0x76, 0xf6, 0x99, 0x76, 0x15
  28904. };
  28905. #ifndef WOLFSSL_KYBER_90S
  28906. const byte kyber1024_pk[] = {
  28907. 0xD2, 0x23, 0x02, 0xCB, 0xD3, 0x39, 0x9F, 0xAC,
  28908. 0xC6, 0x30, 0x99, 0x1F, 0xC8, 0xF2, 0x8B, 0xDB,
  28909. 0x43, 0x54, 0x76, 0x25, 0x41, 0x52, 0x76, 0x78,
  28910. 0xBC, 0xF6, 0x1F, 0x65, 0xC2, 0x41, 0x14, 0x6C,
  28911. 0x42, 0x6D, 0x23, 0xB9, 0xBF, 0xAA, 0x6B, 0x7D,
  28912. 0xF1, 0x8C, 0x97, 0xF2, 0x0C, 0x1B, 0x61, 0x25,
  28913. 0xBF, 0x87, 0x4B, 0x1D, 0x89, 0x47, 0x58, 0x52,
  28914. 0xC4, 0x48, 0x21, 0x5D, 0xB0, 0xEB, 0x77, 0x37,
  28915. 0xF9, 0x14, 0x80, 0xE8, 0xCE, 0xBD, 0x9A, 0x08,
  28916. 0x71, 0x57, 0x4F, 0x5A, 0xB6, 0x2D, 0x90, 0x20,
  28917. 0x17, 0x5E, 0xC6, 0x92, 0x7C, 0xA0, 0xB5, 0x4C,
  28918. 0x09, 0x81, 0x8E, 0x42, 0xCF, 0x92, 0xA3, 0x83,
  28919. 0x17, 0x24, 0x22, 0xC7, 0xDC, 0x18, 0x31, 0xD6,
  28920. 0x3B, 0x0C, 0x29, 0x5D, 0xE7, 0x51, 0x59, 0xDB,
  28921. 0x80, 0x34, 0xE9, 0xE0, 0x7F, 0x7B, 0x0B, 0x91,
  28922. 0x0C, 0x3C, 0x1E, 0x5F, 0xB6, 0x6B, 0x3D, 0xC5,
  28923. 0x23, 0xF1, 0xFA, 0x6E, 0xB4, 0x91, 0x0C, 0xB8,
  28924. 0x9A, 0x6C, 0x17, 0x56, 0x2C, 0x83, 0xAB, 0x4C,
  28925. 0x18, 0xD0, 0xCD, 0x7E, 0x07, 0x96, 0x59, 0x2A,
  28926. 0x37, 0x2A, 0xA4, 0x09, 0xB1, 0xC5, 0x57, 0x34,
  28927. 0x7C, 0xCA, 0xCD, 0xC4, 0x64, 0x4A, 0x11, 0x90,
  28928. 0x64, 0xD0, 0x6D, 0xD4, 0x74, 0x92, 0x9D, 0x1C,
  28929. 0x6F, 0xB4, 0xD6, 0x86, 0xE5, 0x49, 0x1C, 0xE4,
  28930. 0xBC, 0x89, 0xA3, 0x0B, 0xB4, 0xB8, 0xC4, 0x1B,
  28931. 0xCE, 0x51, 0x57, 0xDF, 0xC1, 0x36, 0x08, 0x23,
  28932. 0xB1, 0xAB, 0x61, 0x8C, 0x14, 0xB1, 0x0F, 0x98,
  28933. 0xC2, 0x50, 0x67, 0x39, 0x8E, 0xA7, 0x01, 0x8C,
  28934. 0x27, 0x8A, 0x4B, 0x3D, 0xF3, 0x13, 0x34, 0xD6,
  28935. 0x03, 0xB2, 0x04, 0x4E, 0xF1, 0x87, 0xCD, 0x9B,
  28936. 0xC6, 0xCE, 0x42, 0x72, 0x5B, 0xD9, 0x62, 0xC2,
  28937. 0x64, 0x98, 0x3E, 0x9E, 0x18, 0x15, 0x5A, 0x8B,
  28938. 0x9C, 0x47, 0x14, 0x3D, 0x70, 0x46, 0x0A, 0x26,
  28939. 0xA5, 0x6F, 0xE7, 0x65, 0x8C, 0x1F, 0x15, 0x03,
  28940. 0x48, 0xC6, 0x08, 0x7E, 0xF7, 0x58, 0xAD, 0x16,
  28941. 0x78, 0x87, 0x86, 0x0A, 0x00, 0x7A, 0x5F, 0xC3,
  28942. 0x73, 0x58, 0xD4, 0x3B, 0x5E, 0xBE, 0xE8, 0x20,
  28943. 0xAC, 0xEA, 0x47, 0x4F, 0x0A, 0xC0, 0x7B, 0x76,
  28944. 0x80, 0x28, 0x66, 0x19, 0x9C, 0x61, 0x23, 0x1D,
  28945. 0x5C, 0x74, 0x7C, 0x93, 0x77, 0x4D, 0x2C, 0x1E,
  28946. 0x0C, 0x1C, 0x67, 0xE6, 0xC8, 0x1B, 0x82, 0x75,
  28947. 0x21, 0x73, 0xE1, 0x25, 0xBA, 0xF3, 0x9B, 0x4F,
  28948. 0xD1, 0x9A, 0x4F, 0x45, 0x3D, 0xC5, 0x79, 0x76,
  28949. 0xB1, 0xD9, 0x7F, 0xE6, 0x99, 0x69, 0x92, 0xBB,
  28950. 0xB6, 0x5B, 0x7C, 0xB2, 0x5D, 0x07, 0x7B, 0xBA,
  28951. 0xA6, 0xA1, 0x33, 0x22, 0x89, 0x9A, 0xF6, 0x59,
  28952. 0xCF, 0x1B, 0x35, 0x58, 0xC1, 0xB5, 0x00, 0x11,
  28953. 0x54, 0xB6, 0x25, 0x80, 0x9E, 0xD8, 0x9A, 0xEE,
  28954. 0xBB, 0x89, 0xE6, 0xEA, 0x7D, 0x67, 0xF7, 0x23,
  28955. 0xD0, 0x45, 0xAB, 0x05, 0x71, 0x5C, 0x42, 0x35,
  28956. 0x5D, 0xA6, 0xA5, 0xC8, 0xDD, 0x39, 0xC8, 0xAB,
  28957. 0xE3, 0x03, 0x77, 0x51, 0xA0, 0x1E, 0xD1, 0xC7,
  28958. 0x37, 0x49, 0x19, 0xF3, 0x12, 0x1B, 0x5A, 0x52,
  28959. 0xC5, 0x3D, 0x14, 0x87, 0x31, 0x67, 0x69, 0xF8,
  28960. 0x07, 0x21, 0xDE, 0xEA, 0xAA, 0xD3, 0xC9, 0x0F,
  28961. 0x76, 0xE7, 0xAE, 0x9E, 0x12, 0xBA, 0x92, 0xB3,
  28962. 0x2B, 0x5F, 0xD4, 0x57, 0xE3, 0xC7, 0x52, 0xC2,
  28963. 0x65, 0x0D, 0xFB, 0x88, 0x57, 0x71, 0xCB, 0x77,
  28964. 0xAC, 0x3C, 0x78, 0x5A, 0x8C, 0x56, 0x2E, 0x6A,
  28965. 0x1C, 0x63, 0xC2, 0xA5, 0x5E, 0xA4, 0x7C, 0xF8,
  28966. 0xB9, 0x0E, 0xB8, 0x22, 0x5C, 0x12, 0x3C, 0x34,
  28967. 0x64, 0x52, 0x56, 0x62, 0x35, 0xB2, 0xF3, 0x18,
  28968. 0x23, 0xA3, 0x35, 0x21, 0xE0, 0x87, 0x93, 0x7A,
  28969. 0x34, 0x5D, 0x8D, 0x66, 0x3E, 0xEA, 0xA0, 0x56,
  28970. 0x58, 0x91, 0x7B, 0xBA, 0xA0, 0x08, 0xC2, 0xE3,
  28971. 0x35, 0xF8, 0x85, 0x0A, 0x90, 0xA3, 0x26, 0xD0,
  28972. 0xE6, 0x64, 0x32, 0xF4, 0x4C, 0xEB, 0x82, 0x89,
  28973. 0xE4, 0xEC, 0xB2, 0xD1, 0x29, 0x58, 0xE9, 0x84,
  28974. 0x07, 0x2E, 0xCA, 0xCB, 0x88, 0xE1, 0x34, 0x8F,
  28975. 0xF0, 0xB5, 0x56, 0x54, 0xAC, 0xBA, 0x5B, 0x54,
  28976. 0x97, 0x1C, 0xBA, 0xEB, 0xA8, 0x8E, 0xC4, 0xB9,
  28977. 0x1A, 0x94, 0xC3, 0x71, 0x92, 0xFA, 0x98, 0x2B,
  28978. 0xEC, 0xB9, 0xF3, 0xDA, 0x42, 0x16, 0x03, 0xB6,
  28979. 0x1A, 0x51, 0xBC, 0x8E, 0x36, 0xCB, 0xD0, 0x53,
  28980. 0x85, 0x1C, 0x77, 0xB1, 0xB9, 0x26, 0xB1, 0x7A,
  28981. 0x27, 0x2A, 0xA9, 0x02, 0x32, 0x46, 0xB0, 0x2B,
  28982. 0x3E, 0xD4, 0x7F, 0x66, 0xA0, 0x0B, 0xD5, 0x68,
  28983. 0x48, 0x23, 0x63, 0x4E, 0x7C, 0xE5, 0x8C, 0xF8,
  28984. 0xF3, 0x06, 0xE3, 0x5B, 0x1E, 0x53, 0x22, 0x82,
  28985. 0x4D, 0x90, 0x48, 0x01, 0xF0, 0xA2, 0xFA, 0x7C,
  28986. 0x2B, 0xC9, 0xC2, 0x52, 0xB0, 0xA5, 0x6B, 0x7B,
  28987. 0xA2, 0xAB, 0x0F, 0x63, 0x60, 0x21, 0x74, 0x5A,
  28988. 0x70, 0xA9, 0xA4, 0x3E, 0x2B, 0x0A, 0x8D, 0x61,
  28989. 0x59, 0x70, 0xB6, 0x53, 0x09, 0x62, 0x4B, 0x51,
  28990. 0x84, 0xBC, 0xC3, 0x0B, 0x91, 0x16, 0x79, 0xAE,
  28991. 0xDD, 0x76, 0x02, 0x5F, 0xE3, 0x90, 0x8F, 0xD6,
  28992. 0x78, 0x97, 0xB0, 0xCF, 0x4B, 0xE5, 0xA6, 0xF5,
  28993. 0x41, 0x3D, 0x7D, 0xD9, 0x85, 0x64, 0xB2, 0x3E,
  28994. 0x42, 0xA9, 0x3E, 0x4A, 0xA8, 0x82, 0x1C, 0xD4,
  28995. 0x50, 0x54, 0xC6, 0x43, 0xED, 0xC1, 0x15, 0x8D,
  28996. 0xB6, 0xB3, 0xDE, 0xB1, 0x3F, 0xB5, 0xA5, 0x1E,
  28997. 0xBD, 0x1A, 0x8A, 0x78, 0xB8, 0x72, 0x25, 0xA7,
  28998. 0x33, 0x8E, 0x10, 0x11, 0x04, 0xC4, 0xA2, 0x20,
  28999. 0xD9, 0xBD, 0xED, 0xD4, 0x8C, 0x85, 0xA1, 0xC2,
  29000. 0xDA, 0xE7, 0x81, 0xA8, 0x0C, 0x40, 0xE1, 0x3B,
  29001. 0x87, 0xEA, 0xC7, 0x3A, 0x76, 0x42, 0x01, 0xC9,
  29002. 0xB7, 0x60, 0xCC, 0xFB, 0x1A, 0xE3, 0x92, 0x69,
  29003. 0x9C, 0x70, 0x39, 0xD2, 0x7C, 0x39, 0x36, 0x2B,
  29004. 0x27, 0xB8, 0xFC, 0x6F, 0x07, 0xA8, 0xA3, 0xD4,
  29005. 0x41, 0x0F, 0x15, 0x47, 0xC4, 0x8A, 0x99, 0x97,
  29006. 0xF6, 0x2C, 0x61, 0x07, 0x44, 0x52, 0xEF, 0x15,
  29007. 0x15, 0xF8, 0xA6, 0x49, 0xEB, 0xCA, 0x94, 0x37,
  29008. 0x20, 0x5A, 0x4E, 0x8A, 0x61, 0x60, 0x6B, 0x41,
  29009. 0xDA, 0xF6, 0x83, 0x4D, 0x67, 0x1F, 0x4D, 0x85,
  29010. 0x2C, 0x0C, 0x9C, 0x40, 0x96, 0x61, 0x16, 0x48,
  29011. 0xC6, 0xA3, 0x17, 0x06, 0x78, 0xB1, 0x53, 0x7C,
  29012. 0xC1, 0x82, 0x8D, 0x93, 0x58, 0x0C, 0x9E, 0x58,
  29013. 0x49, 0xA9, 0x65, 0x31, 0x75, 0xAC, 0xB7, 0x53,
  29014. 0xF2, 0xBE, 0x74, 0x37, 0xBE, 0x45, 0xF6, 0xC6,
  29015. 0x03, 0xE4, 0x85, 0xF2, 0xEC, 0x30, 0x1B, 0xB4,
  29016. 0x2B, 0x6C, 0x37, 0xC2, 0x25, 0xD7, 0x49, 0x5A,
  29017. 0x58, 0x4A, 0xE2, 0x31, 0x89, 0x0A, 0xB5, 0xC8,
  29018. 0xC3, 0x5C, 0x26, 0x8C, 0xF4, 0xBB, 0xB0, 0x21,
  29019. 0x3C, 0x09, 0x60, 0x19, 0x31, 0x95, 0x61, 0xA8,
  29020. 0xA6, 0x94, 0x76, 0x37, 0xAA, 0x40, 0xD0, 0x06,
  29021. 0xB4, 0x15, 0xBB, 0x2C, 0xFA, 0x22, 0x37, 0xE0,
  29022. 0x89, 0x0B, 0x6A, 0x3B, 0xC1, 0x34, 0xAB, 0xF8,
  29023. 0xF6, 0x58, 0x5E, 0x10, 0x8D, 0x15, 0x94, 0x0F,
  29024. 0x91, 0xF4, 0xBF, 0x5B, 0x0C, 0x81, 0x80, 0x55,
  29025. 0xB2, 0x1D, 0xEA, 0x6E, 0x63, 0xB5, 0x53, 0x98,
  29026. 0x8C, 0x47, 0xF4, 0xB9, 0x4E, 0x7C, 0xF8, 0x00,
  29027. 0xA4, 0x93, 0xB4, 0x73, 0x47, 0x05, 0xED, 0xC5,
  29028. 0x6A, 0x4B, 0x60, 0x21, 0xC6, 0x29, 0x50, 0x06,
  29029. 0x75, 0x87, 0x68, 0x04, 0xCF, 0x0B, 0x95, 0x1F,
  29030. 0x03, 0x8A, 0x5C, 0x7F, 0xE5, 0x8E, 0x89, 0x77,
  29031. 0x4E, 0xF2, 0x99, 0x2F, 0xD7, 0xC6, 0x30, 0x99,
  29032. 0xD3, 0x52, 0xA7, 0xD2, 0x15, 0x60, 0xB7, 0x88,
  29033. 0xB4, 0x05, 0x70, 0x98, 0x61, 0x81, 0x7E, 0x59,
  29034. 0xA9, 0x6B, 0x3A, 0x3A, 0x83, 0xCB, 0xA8, 0x03,
  29035. 0xB1, 0x69, 0x34, 0x33, 0x10, 0x71, 0x90, 0x5B,
  29036. 0xBE, 0xC6, 0x53, 0x29, 0x00, 0x15, 0x5D, 0x8A,
  29037. 0xC8, 0x8C, 0xB3, 0x2E, 0x4E, 0x21, 0xA3, 0xBD,
  29038. 0x3A, 0x03, 0xFD, 0xEC, 0x32, 0x5A, 0x51, 0xCD,
  29039. 0x27, 0x73, 0x96, 0x4E, 0x67, 0x84, 0xFC, 0xF1,
  29040. 0x85, 0x37, 0x37, 0xAA, 0x64, 0xEB, 0x67, 0x56,
  29041. 0x47, 0x27, 0x27, 0x26, 0x61, 0xAB, 0xF8, 0x43,
  29042. 0x13, 0xA5, 0x7A, 0x44, 0xB1, 0x23, 0xC6, 0x55,
  29043. 0x09, 0xCF, 0xB7, 0xA6, 0xF6, 0x64, 0x1C, 0xDC,
  29044. 0xC3, 0xB5, 0x7F, 0xE6, 0x28, 0xC7, 0xB8, 0x19,
  29045. 0x2D, 0xB4, 0x4F, 0xFB, 0xF5, 0x79, 0x6A, 0x86,
  29046. 0x13, 0xB1, 0xFA, 0x12, 0x6F, 0x60, 0x76, 0x88,
  29047. 0x3C, 0x78, 0x3D, 0xC2, 0x4E, 0x2A, 0x44, 0x64,
  29048. 0xC4, 0x0B, 0x3A, 0x41, 0xCA, 0x70, 0xAE, 0x87,
  29049. 0x62, 0x08, 0x66, 0xCF, 0x4F, 0xCB, 0x2B, 0xD2,
  29050. 0x04, 0xBF, 0x5C, 0x28, 0x38, 0x12, 0xBA, 0x05,
  29051. 0x6A, 0xC0, 0xC3, 0x45, 0xE3, 0x79, 0xC4, 0xBA,
  29052. 0x24, 0xD7, 0x50, 0x90, 0x12, 0x79, 0xBB, 0x2F,
  29053. 0x3A, 0x16, 0xF6, 0x12, 0xBF, 0xAD, 0xB3, 0x57,
  29054. 0x03, 0x33, 0x2C, 0x7C, 0x13, 0x6F, 0x68, 0xEA,
  29055. 0xB6, 0x75, 0x5C, 0x66, 0xB6, 0xA4, 0xAD, 0x1A,
  29056. 0xAB, 0xA7, 0xB7, 0x68, 0xA5, 0x8A, 0xCA, 0xAC,
  29057. 0xC1, 0x0A, 0x45, 0x9A, 0x1C, 0xC8, 0xEF, 0x29,
  29058. 0x37, 0x7B, 0xC2, 0x00, 0xE4, 0xD3, 0x15, 0xA3,
  29059. 0x0A, 0x6B, 0xCC, 0x32, 0x56, 0xF9, 0x73, 0x4D,
  29060. 0x06, 0xE9, 0x77, 0x9C, 0xAA, 0x54, 0x42, 0xA9,
  29061. 0xA1, 0x60, 0x69, 0x08, 0x13, 0x77, 0xC7, 0x6E,
  29062. 0x75, 0x15, 0x43, 0x68, 0x07, 0x2D, 0xC4, 0x46,
  29063. 0xED, 0x6C, 0x8B, 0x8E, 0x62, 0x2A, 0x21, 0xE3,
  29064. 0x83, 0xCF, 0x9B, 0xA1, 0xFB, 0x43, 0x4E, 0x2E,
  29065. 0xCC, 0x81, 0xE7, 0xB7, 0x8C, 0xEE, 0x98, 0x6B,
  29066. 0x8F, 0xF7, 0x98, 0xAB, 0x18, 0xCF, 0x96, 0x34,
  29067. 0x54, 0x35, 0x46, 0x28, 0x4E, 0xDA, 0x2A, 0x26,
  29068. 0xB4, 0x7F, 0x05, 0xB7, 0x35, 0xBC, 0xDB, 0x12,
  29069. 0x02, 0x22, 0x00, 0x76, 0xDC, 0x8B, 0x4E, 0x4B,
  29070. 0x9F, 0x85, 0x35, 0x33, 0xC8, 0xF6, 0xC7, 0xFF,
  29071. 0x38, 0x81, 0x7B, 0xA4, 0x97, 0x12, 0x83, 0x57,
  29072. 0x85, 0xF1, 0x7F, 0x14, 0xCA, 0x01, 0xD0, 0xC1,
  29073. 0xC1, 0xE9, 0x88, 0x10, 0xFE, 0x0B, 0x36, 0xE5,
  29074. 0xB4, 0x27, 0x15, 0x7B, 0x94, 0x18, 0x44, 0x9C,
  29075. 0xED, 0xD6, 0x41, 0xA4, 0x29, 0x3C, 0x85, 0xC3,
  29076. 0x27, 0x00, 0x10, 0x2A, 0xCE, 0xC2, 0x2E, 0xBA,
  29077. 0xD9, 0x8E, 0xD1, 0x60, 0xA5, 0xF0, 0x27, 0xBD,
  29078. 0x4C, 0xDA, 0x57, 0xF1, 0xF3, 0x72, 0x0A, 0x12,
  29079. 0xC1, 0x34, 0x65, 0x4D, 0xD5, 0xE7, 0x3F, 0x82,
  29080. 0x96, 0x76, 0x49, 0x53, 0x90, 0xD0, 0xE7, 0x92,
  29081. 0x9D, 0x60, 0x34, 0xE9, 0xC5, 0x5F, 0x7D, 0x55,
  29082. 0xBA, 0x65, 0x8B, 0xC5, 0x87, 0x98, 0x8E, 0x8A,
  29083. 0xF9, 0x49, 0x60, 0xF6, 0xCF, 0xB8, 0xD5, 0xAF,
  29084. 0x7A, 0x00, 0x21, 0x53, 0x5A, 0x6E, 0x25, 0xE4,
  29085. 0x37, 0xD4, 0x9A, 0x78, 0x06, 0x98, 0xBE, 0x22,
  29086. 0xAC, 0x99, 0x53, 0x94, 0x9F, 0x57, 0x1B, 0x85,
  29087. 0xA6, 0x85, 0x72, 0x5F, 0x82, 0x07, 0xA2, 0xB0,
  29088. 0xAE, 0x84, 0x9B, 0x60, 0x1A, 0xB9, 0x1B, 0x15,
  29089. 0x9B, 0x3D, 0xF4, 0xA1, 0x54, 0xC2, 0x04, 0x1E,
  29090. 0x77, 0x60, 0x70, 0xAF, 0xC4, 0x29, 0x69, 0x32,
  29091. 0x23, 0x80, 0x91, 0x7C, 0x97, 0x51, 0x07, 0x99,
  29092. 0xF3, 0x14, 0x91, 0x31, 0x47, 0x7E, 0x16, 0x66,
  29093. 0x3D, 0x31, 0x74, 0xC7, 0xC1, 0xCA, 0xEA, 0x78,
  29094. 0x85, 0x35, 0xC6, 0xC0, 0x05, 0xA6, 0x4F, 0x28,
  29095. 0x68, 0x63, 0x1B, 0x31, 0xB6, 0x6E, 0x20, 0x5F,
  29096. 0xD3, 0x8C, 0x1D, 0x84, 0x54, 0x2D, 0x0F, 0x1B,
  29097. 0x57, 0x8F, 0x58, 0xC9, 0xBF, 0x5A, 0x0F, 0xAE,
  29098. 0xAB, 0x6A, 0xB6, 0x49, 0x48, 0x93, 0x05, 0x31,
  29099. 0x65, 0xEA, 0xFD, 0x46, 0x5F, 0xC6, 0x4A, 0x0C,
  29100. 0x5F, 0x8F, 0x3F, 0x90, 0x03, 0x48, 0x94, 0x15,
  29101. 0x89, 0x9D, 0x59, 0xA5, 0x43, 0xD8, 0x20, 0x8C,
  29102. 0x54, 0xA3, 0x16, 0x65, 0x29, 0xB5, 0x39, 0x22
  29103. };
  29104. const byte kyber1024_sk[] = {
  29105. 0x07, 0x63, 0x8F, 0xB6, 0x98, 0x68, 0xF3, 0xD3,
  29106. 0x20, 0xE5, 0x86, 0x2B, 0xD9, 0x69, 0x33, 0xFE,
  29107. 0xB3, 0x11, 0xB3, 0x62, 0x09, 0x3C, 0x9B, 0x5D,
  29108. 0x50, 0x17, 0x0B, 0xCE, 0xD4, 0x3F, 0x1B, 0x53,
  29109. 0x6D, 0x9A, 0x20, 0x4B, 0xB1, 0xF2, 0x26, 0x95,
  29110. 0x95, 0x0B, 0xA1, 0xF2, 0xA9, 0xE8, 0xEB, 0x82,
  29111. 0x8B, 0x28, 0x44, 0x88, 0x76, 0x0B, 0x3F, 0xC8,
  29112. 0x4F, 0xAB, 0xA0, 0x42, 0x75, 0xD5, 0x62, 0x8E,
  29113. 0x39, 0xC5, 0xB2, 0x47, 0x13, 0x74, 0x28, 0x3C,
  29114. 0x50, 0x32, 0x99, 0xC0, 0xAB, 0x49, 0xB6, 0x6B,
  29115. 0x8B, 0xBB, 0x56, 0xA4, 0x18, 0x66, 0x24, 0xF9,
  29116. 0x19, 0xA2, 0xBA, 0x59, 0xBB, 0x08, 0xD8, 0x55,
  29117. 0x18, 0x80, 0xC2, 0xBE, 0xFC, 0x4F, 0x87, 0xF2,
  29118. 0x5F, 0x59, 0xAB, 0x58, 0x7A, 0x79, 0xC3, 0x27,
  29119. 0xD7, 0x92, 0xD5, 0x4C, 0x97, 0x4A, 0x69, 0x26,
  29120. 0x2F, 0xF8, 0xA7, 0x89, 0x38, 0x28, 0x9E, 0x9A,
  29121. 0x87, 0xB6, 0x88, 0xB0, 0x83, 0xE0, 0x59, 0x5F,
  29122. 0xE2, 0x18, 0xB6, 0xBB, 0x15, 0x05, 0x94, 0x1C,
  29123. 0xE2, 0xE8, 0x1A, 0x5A, 0x64, 0xC5, 0xAA, 0xC6,
  29124. 0x04, 0x17, 0x25, 0x69, 0x85, 0x34, 0x9E, 0xE4,
  29125. 0x7A, 0x52, 0x42, 0x0A, 0x5F, 0x97, 0x47, 0x7B,
  29126. 0x72, 0x36, 0xAC, 0x76, 0xBC, 0x70, 0xE8, 0x28,
  29127. 0x87, 0x29, 0x28, 0x7E, 0xE3, 0xE3, 0x4A, 0x3D,
  29128. 0xBC, 0x36, 0x83, 0xC0, 0xB7, 0xB1, 0x00, 0x29,
  29129. 0xFC, 0x20, 0x34, 0x18, 0x53, 0x7E, 0x74, 0x66,
  29130. 0xBA, 0x63, 0x85, 0xA8, 0xFF, 0x30, 0x1E, 0xE1,
  29131. 0x27, 0x08, 0xF8, 0x2A, 0xAA, 0x1E, 0x38, 0x0F,
  29132. 0xC7, 0xA8, 0x8F, 0x8F, 0x20, 0x5A, 0xB7, 0xE8,
  29133. 0x8D, 0x7E, 0x95, 0x95, 0x2A, 0x55, 0xBA, 0x20,
  29134. 0xD0, 0x9B, 0x79, 0xA4, 0x71, 0x41, 0xD6, 0x2B,
  29135. 0xF6, 0xEB, 0x7D, 0xD3, 0x07, 0xB0, 0x8E, 0xCA,
  29136. 0x13, 0xA5, 0xBC, 0x5F, 0x6B, 0x68, 0x58, 0x1C,
  29137. 0x68, 0x65, 0xB2, 0x7B, 0xBC, 0xDD, 0xAB, 0x14,
  29138. 0x2F, 0x4B, 0x2C, 0xBF, 0xF4, 0x88, 0xC8, 0xA2,
  29139. 0x27, 0x05, 0xFA, 0xA9, 0x8A, 0x2B, 0x9E, 0xEA,
  29140. 0x35, 0x30, 0xC7, 0x66, 0x62, 0x33, 0x5C, 0xC7,
  29141. 0xEA, 0x3A, 0x00, 0x77, 0x77, 0x25, 0xEB, 0xCC,
  29142. 0xCD, 0x2A, 0x46, 0x36, 0xB2, 0xD9, 0x12, 0x2F,
  29143. 0xF3, 0xAB, 0x77, 0x12, 0x3C, 0xE0, 0x88, 0x3C,
  29144. 0x19, 0x11, 0x11, 0x5E, 0x50, 0xC9, 0xE8, 0xA9,
  29145. 0x41, 0x94, 0xE4, 0x8D, 0xD0, 0xD0, 0x9C, 0xFF,
  29146. 0xB3, 0xAD, 0xCD, 0x2C, 0x1E, 0x92, 0x43, 0x09,
  29147. 0x03, 0xD0, 0x7A, 0xDB, 0xF0, 0x05, 0x32, 0x03,
  29148. 0x15, 0x75, 0xAA, 0x7F, 0x9E, 0x7B, 0x5A, 0x1F,
  29149. 0x33, 0x62, 0xDE, 0xC9, 0x36, 0xD4, 0x04, 0x3C,
  29150. 0x05, 0xF2, 0x47, 0x6C, 0x07, 0x57, 0x8B, 0xC9,
  29151. 0xCB, 0xAF, 0x2A, 0xB4, 0xE3, 0x82, 0x72, 0x7A,
  29152. 0xD4, 0x16, 0x86, 0xA9, 0x6B, 0x25, 0x48, 0x82,
  29153. 0x0B, 0xB0, 0x3B, 0x32, 0xF1, 0x1B, 0x28, 0x11,
  29154. 0xAD, 0x62, 0xF4, 0x89, 0xE9, 0x51, 0x63, 0x2A,
  29155. 0xBA, 0x0D, 0x1D, 0xF8, 0x96, 0x80, 0xCC, 0x8A,
  29156. 0x8B, 0x53, 0xB4, 0x81, 0xD9, 0x2A, 0x68, 0xD7,
  29157. 0x0B, 0x4E, 0xA1, 0xC3, 0xA6, 0xA5, 0x61, 0xC0,
  29158. 0x69, 0x28, 0x82, 0xB5, 0xCA, 0x8C, 0xC9, 0x42,
  29159. 0xA8, 0xD4, 0x95, 0xAF, 0xCB, 0x06, 0xDE, 0x89,
  29160. 0x49, 0x8F, 0xB9, 0x35, 0xB7, 0x75, 0x90, 0x8F,
  29161. 0xE7, 0xA0, 0x3E, 0x32, 0x4D, 0x54, 0xCC, 0x19,
  29162. 0xD4, 0xE1, 0xAA, 0xBD, 0x35, 0x93, 0xB3, 0x8B,
  29163. 0x19, 0xEE, 0x13, 0x88, 0xFE, 0x49, 0x2B, 0x43,
  29164. 0x12, 0x7E, 0x5A, 0x50, 0x42, 0x53, 0x78, 0x6A,
  29165. 0x0D, 0x69, 0xAD, 0x32, 0x60, 0x1C, 0x28, 0xE2,
  29166. 0xC8, 0x85, 0x04, 0xA5, 0xBA, 0x59, 0x97, 0x06,
  29167. 0x02, 0x3A, 0x61, 0x36, 0x3E, 0x17, 0xC6, 0xB9,
  29168. 0xBB, 0x59, 0xBD, 0xC6, 0x97, 0x45, 0x2C, 0xD0,
  29169. 0x59, 0x45, 0x19, 0x83, 0xD7, 0x38, 0xCA, 0x3F,
  29170. 0xD0, 0x34, 0xE3, 0xF5, 0x98, 0x88, 0x54, 0xCA,
  29171. 0x05, 0x03, 0x1D, 0xB0, 0x96, 0x11, 0x49, 0x89,
  29172. 0x88, 0x19, 0x7C, 0x6B, 0x30, 0xD2, 0x58, 0xDF,
  29173. 0xE2, 0x62, 0x65, 0x54, 0x1C, 0x89, 0xA4, 0xB3,
  29174. 0x1D, 0x68, 0x64, 0xE9, 0x38, 0x9B, 0x03, 0xCB,
  29175. 0x74, 0xF7, 0xEC, 0x43, 0x23, 0xFB, 0x94, 0x21,
  29176. 0xA4, 0xB9, 0x79, 0x0A, 0x26, 0xD1, 0x7B, 0x03,
  29177. 0x98, 0xA2, 0x67, 0x67, 0x35, 0x09, 0x09, 0xF8,
  29178. 0x4D, 0x57, 0xB6, 0x69, 0x4D, 0xF8, 0x30, 0x66,
  29179. 0x4C, 0xA8, 0xB3, 0xC3, 0xC0, 0x3E, 0xD2, 0xAE,
  29180. 0x67, 0xB8, 0x90, 0x06, 0x86, 0x8A, 0x68, 0x52,
  29181. 0x7C, 0xCD, 0x66, 0x64, 0x59, 0xAB, 0x7F, 0x05,
  29182. 0x66, 0x71, 0x00, 0x0C, 0x61, 0x64, 0xD3, 0xA7,
  29183. 0xF2, 0x66, 0xA1, 0x4D, 0x97, 0xCB, 0xD7, 0x00,
  29184. 0x4D, 0x6C, 0x92, 0xCA, 0xCA, 0x77, 0x0B, 0x84,
  29185. 0x4A, 0x4F, 0xA9, 0xB1, 0x82, 0xE7, 0xB1, 0x8C,
  29186. 0xA8, 0x85, 0x08, 0x2A, 0xC5, 0x64, 0x6F, 0xCB,
  29187. 0x4A, 0x14, 0xE1, 0x68, 0x5F, 0xEB, 0x0C, 0x9C,
  29188. 0xE3, 0x37, 0x2A, 0xB9, 0x53, 0x65, 0xC0, 0x4F,
  29189. 0xD8, 0x30, 0x84, 0xF8, 0x0A, 0x23, 0xFF, 0x10,
  29190. 0xA0, 0x5B, 0xF1, 0x5F, 0x7F, 0xA5, 0xAC, 0xC6,
  29191. 0xC0, 0xCB, 0x46, 0x2C, 0x33, 0xCA, 0x52, 0x4F,
  29192. 0xA6, 0xB8, 0xBB, 0x35, 0x90, 0x43, 0xBA, 0x68,
  29193. 0x60, 0x9E, 0xAA, 0x25, 0x36, 0xE8, 0x1D, 0x08,
  29194. 0x46, 0x3B, 0x19, 0x65, 0x3B, 0x54, 0x35, 0xBA,
  29195. 0x94, 0x6C, 0x9A, 0xDD, 0xEB, 0x20, 0x2B, 0x04,
  29196. 0xB0, 0x31, 0xCC, 0x96, 0x0D, 0xCC, 0x12, 0xE4,
  29197. 0x51, 0x8D, 0x42, 0x8B, 0x32, 0xB2, 0x57, 0xA4,
  29198. 0xFC, 0x73, 0x13, 0xD3, 0xA7, 0x98, 0x0D, 0x80,
  29199. 0x08, 0x2E, 0x93, 0x4F, 0x9D, 0x95, 0xC3, 0x2B,
  29200. 0x0A, 0x01, 0x91, 0xA2, 0x36, 0x04, 0x38, 0x4D,
  29201. 0xD9, 0xE0, 0x79, 0xBB, 0xBA, 0xA2, 0x66, 0xD1,
  29202. 0x4C, 0x3F, 0x75, 0x6B, 0x9F, 0x21, 0x33, 0x10,
  29203. 0x74, 0x33, 0xA4, 0xE8, 0x3F, 0xA7, 0x18, 0x72,
  29204. 0x82, 0xA8, 0x09, 0x20, 0x3A, 0x4F, 0xAF, 0x84,
  29205. 0x18, 0x51, 0x83, 0x3D, 0x12, 0x1A, 0xC3, 0x83,
  29206. 0x84, 0x3A, 0x5E, 0x55, 0xBC, 0x23, 0x81, 0x42,
  29207. 0x5E, 0x16, 0xC7, 0xDB, 0x4C, 0xC9, 0xAB, 0x5C,
  29208. 0x1B, 0x0D, 0x91, 0xA4, 0x7E, 0x2B, 0x8D, 0xE0,
  29209. 0xE5, 0x82, 0xC8, 0x6B, 0x6B, 0x0D, 0x90, 0x7B,
  29210. 0xB3, 0x60, 0xB9, 0x7F, 0x40, 0xAB, 0x5D, 0x03,
  29211. 0x8F, 0x6B, 0x75, 0xC8, 0x14, 0xB2, 0x7D, 0x9B,
  29212. 0x96, 0x8D, 0x41, 0x98, 0x32, 0xBC, 0x8C, 0x2B,
  29213. 0xEE, 0x60, 0x5E, 0xF6, 0xE5, 0x05, 0x9D, 0x33,
  29214. 0x10, 0x0D, 0x90, 0x48, 0x5D, 0x37, 0x84, 0x50,
  29215. 0x01, 0x42, 0x21, 0x73, 0x6C, 0x07, 0x40, 0x7C,
  29216. 0xAC, 0x26, 0x04, 0x08, 0xAA, 0x64, 0x92, 0x66,
  29217. 0x19, 0x78, 0x8B, 0x86, 0x01, 0xC2, 0xA7, 0x52,
  29218. 0xD1, 0xA6, 0xCB, 0xF8, 0x20, 0xD7, 0xC7, 0xA0,
  29219. 0x47, 0x16, 0x20, 0x32, 0x25, 0xB3, 0x89, 0x5B,
  29220. 0x93, 0x42, 0xD1, 0x47, 0xA8, 0x18, 0x5C, 0xFC,
  29221. 0x1B, 0xB6, 0x5B, 0xA0, 0x6B, 0x41, 0x42, 0x33,
  29222. 0x99, 0x03, 0xC0, 0xAC, 0x46, 0x51, 0x38, 0x5B,
  29223. 0x45, 0xD9, 0x8A, 0x8B, 0x19, 0xD2, 0x8C, 0xD6,
  29224. 0xBA, 0xB0, 0x88, 0x78, 0x7F, 0x7E, 0xE1, 0xB1,
  29225. 0x24, 0x61, 0x76, 0x6B, 0x43, 0xCB, 0xCC, 0xB9,
  29226. 0x64, 0x34, 0x42, 0x7D, 0x93, 0xC0, 0x65, 0x55,
  29227. 0x06, 0x88, 0xF6, 0x94, 0x8E, 0xD1, 0xB5, 0x47,
  29228. 0x5A, 0x42, 0x5F, 0x1B, 0x85, 0x20, 0x9D, 0x06,
  29229. 0x1C, 0x08, 0xB5, 0x6C, 0x1C, 0xC0, 0x69, 0xF6,
  29230. 0xC0, 0xA7, 0xC6, 0xF2, 0x93, 0x58, 0xCA, 0xB9,
  29231. 0x11, 0x08, 0x77, 0x32, 0xA6, 0x49, 0xD2, 0x7C,
  29232. 0x9B, 0x98, 0xF9, 0xA4, 0x88, 0x79, 0x38, 0x7D,
  29233. 0x9B, 0x00, 0xC2, 0x59, 0x59, 0xA7, 0x16, 0x54,
  29234. 0xD6, 0xF6, 0xA9, 0x46, 0x16, 0x45, 0x13, 0xE4,
  29235. 0x7A, 0x75, 0xD0, 0x05, 0x98, 0x6C, 0x23, 0x63,
  29236. 0xC0, 0x9F, 0x6B, 0x53, 0x7E, 0xCA, 0x78, 0xB9,
  29237. 0x30, 0x3A, 0x5F, 0xA4, 0x57, 0x60, 0x8A, 0x58,
  29238. 0x6A, 0x65, 0x3A, 0x34, 0x7D, 0xB0, 0x4D, 0xFC,
  29239. 0xC1, 0x91, 0x75, 0xB3, 0xA3, 0x01, 0x17, 0x25,
  29240. 0x36, 0x06, 0x2A, 0x65, 0x8A, 0x95, 0x27, 0x75,
  29241. 0x70, 0xC8, 0x85, 0x2C, 0xA8, 0x97, 0x3F, 0x4A,
  29242. 0xE1, 0x23, 0xA3, 0x34, 0x04, 0x7D, 0xD7, 0x11,
  29243. 0xC8, 0x92, 0x7A, 0x63, 0x4A, 0x03, 0x38, 0x8A,
  29244. 0x52, 0x7B, 0x03, 0x4B, 0xF7, 0xA8, 0x17, 0x0F,
  29245. 0xA7, 0x02, 0xC1, 0xF7, 0xC2, 0x3E, 0xC3, 0x2D,
  29246. 0x18, 0xA2, 0x37, 0x48, 0x90, 0xBE, 0x9C, 0x78,
  29247. 0x7A, 0x94, 0x09, 0xC8, 0x2D, 0x19, 0x2C, 0x4B,
  29248. 0xB7, 0x05, 0xA2, 0xF9, 0x96, 0xCE, 0x40, 0x5D,
  29249. 0x85, 0xA4, 0xC1, 0xA1, 0xAB, 0x9B, 0x6A, 0xEB,
  29250. 0x49, 0xCC, 0xE1, 0xC2, 0xF8, 0xA9, 0x7C, 0x35,
  29251. 0x16, 0xC7, 0x2A, 0x00, 0xA4, 0x62, 0x63, 0xBA,
  29252. 0xA6, 0x96, 0xBF, 0x25, 0x72, 0x77, 0x19, 0xC3,
  29253. 0x21, 0x64, 0x23, 0x61, 0x8F, 0xF3, 0x33, 0x80,
  29254. 0x93, 0x4A, 0x6C, 0x10, 0x54, 0x5C, 0x4C, 0x5C,
  29255. 0x51, 0x55, 0xB1, 0x24, 0x86, 0x18, 0x1F, 0xC7,
  29256. 0xA2, 0x31, 0x98, 0x73, 0x97, 0x8B, 0x6A, 0x2A,
  29257. 0x67, 0x49, 0x0F, 0x82, 0x56, 0xBD, 0x21, 0x96,
  29258. 0xFE, 0x17, 0x92, 0xA4, 0xC0, 0x00, 0x77, 0xB8,
  29259. 0x12, 0xEA, 0xE8, 0xBE, 0xD3, 0x57, 0x24, 0x99,
  29260. 0x68, 0x4A, 0xB3, 0x37, 0x18, 0x76, 0x76, 0x1E,
  29261. 0x45, 0x0C, 0x9F, 0x9D, 0x27, 0x68, 0xA3, 0x68,
  29262. 0x06, 0xD7, 0xAB, 0x20, 0x46, 0xC9, 0x1F, 0x17,
  29263. 0x59, 0x9E, 0x9A, 0xC5, 0x92, 0x99, 0x08, 0x08,
  29264. 0xDC, 0xD7, 0xB4, 0xD0, 0x91, 0x90, 0x72, 0xF1,
  29265. 0x4E, 0xC3, 0x61, 0x77, 0x3B, 0x72, 0x52, 0x44,
  29266. 0x4C, 0x32, 0x3C, 0x30, 0x83, 0x26, 0xF4, 0xA3,
  29267. 0x0F, 0x86, 0x80, 0xD2, 0xF7, 0x48, 0xF5, 0x6A,
  29268. 0x13, 0x2B, 0x82, 0x67, 0x4E, 0xD0, 0x18, 0x46,
  29269. 0x20, 0xB8, 0x2A, 0xD2, 0xCB, 0x18, 0x2C, 0x97,
  29270. 0xB4, 0x81, 0x62, 0x66, 0x47, 0x49, 0x12, 0x90,
  29271. 0xA0, 0x11, 0xCC, 0x73, 0x82, 0x86, 0x85, 0xA8,
  29272. 0xC3, 0x67, 0xA5, 0xB9, 0xCF, 0x8D, 0x62, 0x1B,
  29273. 0x0D, 0x5C, 0x1E, 0xFF, 0x03, 0x17, 0x27, 0x58,
  29274. 0xBD, 0x00, 0x49, 0x78, 0xC2, 0x51, 0xCD, 0x51,
  29275. 0x34, 0x22, 0x28, 0x98, 0x9C, 0xAE, 0x63, 0x32,
  29276. 0xAC, 0x48, 0x64, 0x37, 0xCB, 0x5C, 0x57, 0xD4,
  29277. 0x30, 0x74, 0x62, 0x86, 0x52, 0x53, 0xBE, 0x21,
  29278. 0x7B, 0x35, 0x15, 0xC7, 0x3D, 0xF4, 0x05, 0xB7,
  29279. 0xF2, 0x82, 0x17, 0xAD, 0x0B, 0x8C, 0xF6, 0x0C,
  29280. 0x2F, 0xFF, 0xAA, 0x0A, 0x00, 0x48, 0xB1, 0xFB,
  29281. 0x4A, 0xCD, 0xCD, 0xC3, 0x8B, 0x52, 0x50, 0xCF,
  29282. 0xEC, 0x35, 0x6A, 0x6D, 0xE2, 0x6C, 0xFA, 0x7A,
  29283. 0x58, 0x8F, 0xDC, 0x86, 0xF9, 0x8C, 0x85, 0x4A,
  29284. 0xC6, 0x4C, 0x7B, 0xFA, 0xA9, 0x6F, 0x5A, 0x32,
  29285. 0xCC, 0x06, 0x10, 0x93, 0x4B, 0xAA, 0x6A, 0x58,
  29286. 0x6B, 0x9A, 0x20, 0x54, 0xF1, 0x3B, 0xA2, 0x74,
  29287. 0x17, 0x4A, 0xA0, 0xD2, 0xB3, 0xA8, 0x1B, 0x96,
  29288. 0xA9, 0x40, 0x66, 0x6F, 0x78, 0x9B, 0x5A, 0x6B,
  29289. 0xCD, 0xC0, 0xA6, 0xA0, 0x17, 0x8A, 0x0C, 0x9A,
  29290. 0x02, 0x57, 0x8A, 0x49, 0x3F, 0x6E, 0xEA, 0x0D,
  29291. 0x2E, 0x6C, 0x13, 0x95, 0x1C, 0x9F, 0x24, 0x9A,
  29292. 0x5E, 0x8D, 0xD7, 0x1D, 0xD4, 0x9A, 0x74, 0x2D,
  29293. 0x45, 0x1F, 0x1A, 0xBB, 0xA1, 0x9A, 0xF8, 0xC5,
  29294. 0x47, 0x85, 0x5E, 0x0A, 0xFC, 0x72, 0x8E, 0x90,
  29295. 0xAB, 0xB4, 0x99, 0xC9, 0xBE, 0xEB, 0x76, 0x6F,
  29296. 0x47, 0x29, 0xCD, 0xA2, 0x22, 0x63, 0xE3, 0x24,
  29297. 0xD2, 0x23, 0x02, 0xCB, 0xD3, 0x39, 0x9F, 0xAC,
  29298. 0xC6, 0x30, 0x99, 0x1F, 0xC8, 0xF2, 0x8B, 0xDB,
  29299. 0x43, 0x54, 0x76, 0x25, 0x41, 0x52, 0x76, 0x78,
  29300. 0xBC, 0xF6, 0x1F, 0x65, 0xC2, 0x41, 0x14, 0x6C,
  29301. 0x42, 0x6D, 0x23, 0xB9, 0xBF, 0xAA, 0x6B, 0x7D,
  29302. 0xF1, 0x8C, 0x97, 0xF2, 0x0C, 0x1B, 0x61, 0x25,
  29303. 0xBF, 0x87, 0x4B, 0x1D, 0x89, 0x47, 0x58, 0x52,
  29304. 0xC4, 0x48, 0x21, 0x5D, 0xB0, 0xEB, 0x77, 0x37,
  29305. 0xF9, 0x14, 0x80, 0xE8, 0xCE, 0xBD, 0x9A, 0x08,
  29306. 0x71, 0x57, 0x4F, 0x5A, 0xB6, 0x2D, 0x90, 0x20,
  29307. 0x17, 0x5E, 0xC6, 0x92, 0x7C, 0xA0, 0xB5, 0x4C,
  29308. 0x09, 0x81, 0x8E, 0x42, 0xCF, 0x92, 0xA3, 0x83,
  29309. 0x17, 0x24, 0x22, 0xC7, 0xDC, 0x18, 0x31, 0xD6,
  29310. 0x3B, 0x0C, 0x29, 0x5D, 0xE7, 0x51, 0x59, 0xDB,
  29311. 0x80, 0x34, 0xE9, 0xE0, 0x7F, 0x7B, 0x0B, 0x91,
  29312. 0x0C, 0x3C, 0x1E, 0x5F, 0xB6, 0x6B, 0x3D, 0xC5,
  29313. 0x23, 0xF1, 0xFA, 0x6E, 0xB4, 0x91, 0x0C, 0xB8,
  29314. 0x9A, 0x6C, 0x17, 0x56, 0x2C, 0x83, 0xAB, 0x4C,
  29315. 0x18, 0xD0, 0xCD, 0x7E, 0x07, 0x96, 0x59, 0x2A,
  29316. 0x37, 0x2A, 0xA4, 0x09, 0xB1, 0xC5, 0x57, 0x34,
  29317. 0x7C, 0xCA, 0xCD, 0xC4, 0x64, 0x4A, 0x11, 0x90,
  29318. 0x64, 0xD0, 0x6D, 0xD4, 0x74, 0x92, 0x9D, 0x1C,
  29319. 0x6F, 0xB4, 0xD6, 0x86, 0xE5, 0x49, 0x1C, 0xE4,
  29320. 0xBC, 0x89, 0xA3, 0x0B, 0xB4, 0xB8, 0xC4, 0x1B,
  29321. 0xCE, 0x51, 0x57, 0xDF, 0xC1, 0x36, 0x08, 0x23,
  29322. 0xB1, 0xAB, 0x61, 0x8C, 0x14, 0xB1, 0x0F, 0x98,
  29323. 0xC2, 0x50, 0x67, 0x39, 0x8E, 0xA7, 0x01, 0x8C,
  29324. 0x27, 0x8A, 0x4B, 0x3D, 0xF3, 0x13, 0x34, 0xD6,
  29325. 0x03, 0xB2, 0x04, 0x4E, 0xF1, 0x87, 0xCD, 0x9B,
  29326. 0xC6, 0xCE, 0x42, 0x72, 0x5B, 0xD9, 0x62, 0xC2,
  29327. 0x64, 0x98, 0x3E, 0x9E, 0x18, 0x15, 0x5A, 0x8B,
  29328. 0x9C, 0x47, 0x14, 0x3D, 0x70, 0x46, 0x0A, 0x26,
  29329. 0xA5, 0x6F, 0xE7, 0x65, 0x8C, 0x1F, 0x15, 0x03,
  29330. 0x48, 0xC6, 0x08, 0x7E, 0xF7, 0x58, 0xAD, 0x16,
  29331. 0x78, 0x87, 0x86, 0x0A, 0x00, 0x7A, 0x5F, 0xC3,
  29332. 0x73, 0x58, 0xD4, 0x3B, 0x5E, 0xBE, 0xE8, 0x20,
  29333. 0xAC, 0xEA, 0x47, 0x4F, 0x0A, 0xC0, 0x7B, 0x76,
  29334. 0x80, 0x28, 0x66, 0x19, 0x9C, 0x61, 0x23, 0x1D,
  29335. 0x5C, 0x74, 0x7C, 0x93, 0x77, 0x4D, 0x2C, 0x1E,
  29336. 0x0C, 0x1C, 0x67, 0xE6, 0xC8, 0x1B, 0x82, 0x75,
  29337. 0x21, 0x73, 0xE1, 0x25, 0xBA, 0xF3, 0x9B, 0x4F,
  29338. 0xD1, 0x9A, 0x4F, 0x45, 0x3D, 0xC5, 0x79, 0x76,
  29339. 0xB1, 0xD9, 0x7F, 0xE6, 0x99, 0x69, 0x92, 0xBB,
  29340. 0xB6, 0x5B, 0x7C, 0xB2, 0x5D, 0x07, 0x7B, 0xBA,
  29341. 0xA6, 0xA1, 0x33, 0x22, 0x89, 0x9A, 0xF6, 0x59,
  29342. 0xCF, 0x1B, 0x35, 0x58, 0xC1, 0xB5, 0x00, 0x11,
  29343. 0x54, 0xB6, 0x25, 0x80, 0x9E, 0xD8, 0x9A, 0xEE,
  29344. 0xBB, 0x89, 0xE6, 0xEA, 0x7D, 0x67, 0xF7, 0x23,
  29345. 0xD0, 0x45, 0xAB, 0x05, 0x71, 0x5C, 0x42, 0x35,
  29346. 0x5D, 0xA6, 0xA5, 0xC8, 0xDD, 0x39, 0xC8, 0xAB,
  29347. 0xE3, 0x03, 0x77, 0x51, 0xA0, 0x1E, 0xD1, 0xC7,
  29348. 0x37, 0x49, 0x19, 0xF3, 0x12, 0x1B, 0x5A, 0x52,
  29349. 0xC5, 0x3D, 0x14, 0x87, 0x31, 0x67, 0x69, 0xF8,
  29350. 0x07, 0x21, 0xDE, 0xEA, 0xAA, 0xD3, 0xC9, 0x0F,
  29351. 0x76, 0xE7, 0xAE, 0x9E, 0x12, 0xBA, 0x92, 0xB3,
  29352. 0x2B, 0x5F, 0xD4, 0x57, 0xE3, 0xC7, 0x52, 0xC2,
  29353. 0x65, 0x0D, 0xFB, 0x88, 0x57, 0x71, 0xCB, 0x77,
  29354. 0xAC, 0x3C, 0x78, 0x5A, 0x8C, 0x56, 0x2E, 0x6A,
  29355. 0x1C, 0x63, 0xC2, 0xA5, 0x5E, 0xA4, 0x7C, 0xF8,
  29356. 0xB9, 0x0E, 0xB8, 0x22, 0x5C, 0x12, 0x3C, 0x34,
  29357. 0x64, 0x52, 0x56, 0x62, 0x35, 0xB2, 0xF3, 0x18,
  29358. 0x23, 0xA3, 0x35, 0x21, 0xE0, 0x87, 0x93, 0x7A,
  29359. 0x34, 0x5D, 0x8D, 0x66, 0x3E, 0xEA, 0xA0, 0x56,
  29360. 0x58, 0x91, 0x7B, 0xBA, 0xA0, 0x08, 0xC2, 0xE3,
  29361. 0x35, 0xF8, 0x85, 0x0A, 0x90, 0xA3, 0x26, 0xD0,
  29362. 0xE6, 0x64, 0x32, 0xF4, 0x4C, 0xEB, 0x82, 0x89,
  29363. 0xE4, 0xEC, 0xB2, 0xD1, 0x29, 0x58, 0xE9, 0x84,
  29364. 0x07, 0x2E, 0xCA, 0xCB, 0x88, 0xE1, 0x34, 0x8F,
  29365. 0xF0, 0xB5, 0x56, 0x54, 0xAC, 0xBA, 0x5B, 0x54,
  29366. 0x97, 0x1C, 0xBA, 0xEB, 0xA8, 0x8E, 0xC4, 0xB9,
  29367. 0x1A, 0x94, 0xC3, 0x71, 0x92, 0xFA, 0x98, 0x2B,
  29368. 0xEC, 0xB9, 0xF3, 0xDA, 0x42, 0x16, 0x03, 0xB6,
  29369. 0x1A, 0x51, 0xBC, 0x8E, 0x36, 0xCB, 0xD0, 0x53,
  29370. 0x85, 0x1C, 0x77, 0xB1, 0xB9, 0x26, 0xB1, 0x7A,
  29371. 0x27, 0x2A, 0xA9, 0x02, 0x32, 0x46, 0xB0, 0x2B,
  29372. 0x3E, 0xD4, 0x7F, 0x66, 0xA0, 0x0B, 0xD5, 0x68,
  29373. 0x48, 0x23, 0x63, 0x4E, 0x7C, 0xE5, 0x8C, 0xF8,
  29374. 0xF3, 0x06, 0xE3, 0x5B, 0x1E, 0x53, 0x22, 0x82,
  29375. 0x4D, 0x90, 0x48, 0x01, 0xF0, 0xA2, 0xFA, 0x7C,
  29376. 0x2B, 0xC9, 0xC2, 0x52, 0xB0, 0xA5, 0x6B, 0x7B,
  29377. 0xA2, 0xAB, 0x0F, 0x63, 0x60, 0x21, 0x74, 0x5A,
  29378. 0x70, 0xA9, 0xA4, 0x3E, 0x2B, 0x0A, 0x8D, 0x61,
  29379. 0x59, 0x70, 0xB6, 0x53, 0x09, 0x62, 0x4B, 0x51,
  29380. 0x84, 0xBC, 0xC3, 0x0B, 0x91, 0x16, 0x79, 0xAE,
  29381. 0xDD, 0x76, 0x02, 0x5F, 0xE3, 0x90, 0x8F, 0xD6,
  29382. 0x78, 0x97, 0xB0, 0xCF, 0x4B, 0xE5, 0xA6, 0xF5,
  29383. 0x41, 0x3D, 0x7D, 0xD9, 0x85, 0x64, 0xB2, 0x3E,
  29384. 0x42, 0xA9, 0x3E, 0x4A, 0xA8, 0x82, 0x1C, 0xD4,
  29385. 0x50, 0x54, 0xC6, 0x43, 0xED, 0xC1, 0x15, 0x8D,
  29386. 0xB6, 0xB3, 0xDE, 0xB1, 0x3F, 0xB5, 0xA5, 0x1E,
  29387. 0xBD, 0x1A, 0x8A, 0x78, 0xB8, 0x72, 0x25, 0xA7,
  29388. 0x33, 0x8E, 0x10, 0x11, 0x04, 0xC4, 0xA2, 0x20,
  29389. 0xD9, 0xBD, 0xED, 0xD4, 0x8C, 0x85, 0xA1, 0xC2,
  29390. 0xDA, 0xE7, 0x81, 0xA8, 0x0C, 0x40, 0xE1, 0x3B,
  29391. 0x87, 0xEA, 0xC7, 0x3A, 0x76, 0x42, 0x01, 0xC9,
  29392. 0xB7, 0x60, 0xCC, 0xFB, 0x1A, 0xE3, 0x92, 0x69,
  29393. 0x9C, 0x70, 0x39, 0xD2, 0x7C, 0x39, 0x36, 0x2B,
  29394. 0x27, 0xB8, 0xFC, 0x6F, 0x07, 0xA8, 0xA3, 0xD4,
  29395. 0x41, 0x0F, 0x15, 0x47, 0xC4, 0x8A, 0x99, 0x97,
  29396. 0xF6, 0x2C, 0x61, 0x07, 0x44, 0x52, 0xEF, 0x15,
  29397. 0x15, 0xF8, 0xA6, 0x49, 0xEB, 0xCA, 0x94, 0x37,
  29398. 0x20, 0x5A, 0x4E, 0x8A, 0x61, 0x60, 0x6B, 0x41,
  29399. 0xDA, 0xF6, 0x83, 0x4D, 0x67, 0x1F, 0x4D, 0x85,
  29400. 0x2C, 0x0C, 0x9C, 0x40, 0x96, 0x61, 0x16, 0x48,
  29401. 0xC6, 0xA3, 0x17, 0x06, 0x78, 0xB1, 0x53, 0x7C,
  29402. 0xC1, 0x82, 0x8D, 0x93, 0x58, 0x0C, 0x9E, 0x58,
  29403. 0x49, 0xA9, 0x65, 0x31, 0x75, 0xAC, 0xB7, 0x53,
  29404. 0xF2, 0xBE, 0x74, 0x37, 0xBE, 0x45, 0xF6, 0xC6,
  29405. 0x03, 0xE4, 0x85, 0xF2, 0xEC, 0x30, 0x1B, 0xB4,
  29406. 0x2B, 0x6C, 0x37, 0xC2, 0x25, 0xD7, 0x49, 0x5A,
  29407. 0x58, 0x4A, 0xE2, 0x31, 0x89, 0x0A, 0xB5, 0xC8,
  29408. 0xC3, 0x5C, 0x26, 0x8C, 0xF4, 0xBB, 0xB0, 0x21,
  29409. 0x3C, 0x09, 0x60, 0x19, 0x31, 0x95, 0x61, 0xA8,
  29410. 0xA6, 0x94, 0x76, 0x37, 0xAA, 0x40, 0xD0, 0x06,
  29411. 0xB4, 0x15, 0xBB, 0x2C, 0xFA, 0x22, 0x37, 0xE0,
  29412. 0x89, 0x0B, 0x6A, 0x3B, 0xC1, 0x34, 0xAB, 0xF8,
  29413. 0xF6, 0x58, 0x5E, 0x10, 0x8D, 0x15, 0x94, 0x0F,
  29414. 0x91, 0xF4, 0xBF, 0x5B, 0x0C, 0x81, 0x80, 0x55,
  29415. 0xB2, 0x1D, 0xEA, 0x6E, 0x63, 0xB5, 0x53, 0x98,
  29416. 0x8C, 0x47, 0xF4, 0xB9, 0x4E, 0x7C, 0xF8, 0x00,
  29417. 0xA4, 0x93, 0xB4, 0x73, 0x47, 0x05, 0xED, 0xC5,
  29418. 0x6A, 0x4B, 0x60, 0x21, 0xC6, 0x29, 0x50, 0x06,
  29419. 0x75, 0x87, 0x68, 0x04, 0xCF, 0x0B, 0x95, 0x1F,
  29420. 0x03, 0x8A, 0x5C, 0x7F, 0xE5, 0x8E, 0x89, 0x77,
  29421. 0x4E, 0xF2, 0x99, 0x2F, 0xD7, 0xC6, 0x30, 0x99,
  29422. 0xD3, 0x52, 0xA7, 0xD2, 0x15, 0x60, 0xB7, 0x88,
  29423. 0xB4, 0x05, 0x70, 0x98, 0x61, 0x81, 0x7E, 0x59,
  29424. 0xA9, 0x6B, 0x3A, 0x3A, 0x83, 0xCB, 0xA8, 0x03,
  29425. 0xB1, 0x69, 0x34, 0x33, 0x10, 0x71, 0x90, 0x5B,
  29426. 0xBE, 0xC6, 0x53, 0x29, 0x00, 0x15, 0x5D, 0x8A,
  29427. 0xC8, 0x8C, 0xB3, 0x2E, 0x4E, 0x21, 0xA3, 0xBD,
  29428. 0x3A, 0x03, 0xFD, 0xEC, 0x32, 0x5A, 0x51, 0xCD,
  29429. 0x27, 0x73, 0x96, 0x4E, 0x67, 0x84, 0xFC, 0xF1,
  29430. 0x85, 0x37, 0x37, 0xAA, 0x64, 0xEB, 0x67, 0x56,
  29431. 0x47, 0x27, 0x27, 0x26, 0x61, 0xAB, 0xF8, 0x43,
  29432. 0x13, 0xA5, 0x7A, 0x44, 0xB1, 0x23, 0xC6, 0x55,
  29433. 0x09, 0xCF, 0xB7, 0xA6, 0xF6, 0x64, 0x1C, 0xDC,
  29434. 0xC3, 0xB5, 0x7F, 0xE6, 0x28, 0xC7, 0xB8, 0x19,
  29435. 0x2D, 0xB4, 0x4F, 0xFB, 0xF5, 0x79, 0x6A, 0x86,
  29436. 0x13, 0xB1, 0xFA, 0x12, 0x6F, 0x60, 0x76, 0x88,
  29437. 0x3C, 0x78, 0x3D, 0xC2, 0x4E, 0x2A, 0x44, 0x64,
  29438. 0xC4, 0x0B, 0x3A, 0x41, 0xCA, 0x70, 0xAE, 0x87,
  29439. 0x62, 0x08, 0x66, 0xCF, 0x4F, 0xCB, 0x2B, 0xD2,
  29440. 0x04, 0xBF, 0x5C, 0x28, 0x38, 0x12, 0xBA, 0x05,
  29441. 0x6A, 0xC0, 0xC3, 0x45, 0xE3, 0x79, 0xC4, 0xBA,
  29442. 0x24, 0xD7, 0x50, 0x90, 0x12, 0x79, 0xBB, 0x2F,
  29443. 0x3A, 0x16, 0xF6, 0x12, 0xBF, 0xAD, 0xB3, 0x57,
  29444. 0x03, 0x33, 0x2C, 0x7C, 0x13, 0x6F, 0x68, 0xEA,
  29445. 0xB6, 0x75, 0x5C, 0x66, 0xB6, 0xA4, 0xAD, 0x1A,
  29446. 0xAB, 0xA7, 0xB7, 0x68, 0xA5, 0x8A, 0xCA, 0xAC,
  29447. 0xC1, 0x0A, 0x45, 0x9A, 0x1C, 0xC8, 0xEF, 0x29,
  29448. 0x37, 0x7B, 0xC2, 0x00, 0xE4, 0xD3, 0x15, 0xA3,
  29449. 0x0A, 0x6B, 0xCC, 0x32, 0x56, 0xF9, 0x73, 0x4D,
  29450. 0x06, 0xE9, 0x77, 0x9C, 0xAA, 0x54, 0x42, 0xA9,
  29451. 0xA1, 0x60, 0x69, 0x08, 0x13, 0x77, 0xC7, 0x6E,
  29452. 0x75, 0x15, 0x43, 0x68, 0x07, 0x2D, 0xC4, 0x46,
  29453. 0xED, 0x6C, 0x8B, 0x8E, 0x62, 0x2A, 0x21, 0xE3,
  29454. 0x83, 0xCF, 0x9B, 0xA1, 0xFB, 0x43, 0x4E, 0x2E,
  29455. 0xCC, 0x81, 0xE7, 0xB7, 0x8C, 0xEE, 0x98, 0x6B,
  29456. 0x8F, 0xF7, 0x98, 0xAB, 0x18, 0xCF, 0x96, 0x34,
  29457. 0x54, 0x35, 0x46, 0x28, 0x4E, 0xDA, 0x2A, 0x26,
  29458. 0xB4, 0x7F, 0x05, 0xB7, 0x35, 0xBC, 0xDB, 0x12,
  29459. 0x02, 0x22, 0x00, 0x76, 0xDC, 0x8B, 0x4E, 0x4B,
  29460. 0x9F, 0x85, 0x35, 0x33, 0xC8, 0xF6, 0xC7, 0xFF,
  29461. 0x38, 0x81, 0x7B, 0xA4, 0x97, 0x12, 0x83, 0x57,
  29462. 0x85, 0xF1, 0x7F, 0x14, 0xCA, 0x01, 0xD0, 0xC1,
  29463. 0xC1, 0xE9, 0x88, 0x10, 0xFE, 0x0B, 0x36, 0xE5,
  29464. 0xB4, 0x27, 0x15, 0x7B, 0x94, 0x18, 0x44, 0x9C,
  29465. 0xED, 0xD6, 0x41, 0xA4, 0x29, 0x3C, 0x85, 0xC3,
  29466. 0x27, 0x00, 0x10, 0x2A, 0xCE, 0xC2, 0x2E, 0xBA,
  29467. 0xD9, 0x8E, 0xD1, 0x60, 0xA5, 0xF0, 0x27, 0xBD,
  29468. 0x4C, 0xDA, 0x57, 0xF1, 0xF3, 0x72, 0x0A, 0x12,
  29469. 0xC1, 0x34, 0x65, 0x4D, 0xD5, 0xE7, 0x3F, 0x82,
  29470. 0x96, 0x76, 0x49, 0x53, 0x90, 0xD0, 0xE7, 0x92,
  29471. 0x9D, 0x60, 0x34, 0xE9, 0xC5, 0x5F, 0x7D, 0x55,
  29472. 0xBA, 0x65, 0x8B, 0xC5, 0x87, 0x98, 0x8E, 0x8A,
  29473. 0xF9, 0x49, 0x60, 0xF6, 0xCF, 0xB8, 0xD5, 0xAF,
  29474. 0x7A, 0x00, 0x21, 0x53, 0x5A, 0x6E, 0x25, 0xE4,
  29475. 0x37, 0xD4, 0x9A, 0x78, 0x06, 0x98, 0xBE, 0x22,
  29476. 0xAC, 0x99, 0x53, 0x94, 0x9F, 0x57, 0x1B, 0x85,
  29477. 0xA6, 0x85, 0x72, 0x5F, 0x82, 0x07, 0xA2, 0xB0,
  29478. 0xAE, 0x84, 0x9B, 0x60, 0x1A, 0xB9, 0x1B, 0x15,
  29479. 0x9B, 0x3D, 0xF4, 0xA1, 0x54, 0xC2, 0x04, 0x1E,
  29480. 0x77, 0x60, 0x70, 0xAF, 0xC4, 0x29, 0x69, 0x32,
  29481. 0x23, 0x80, 0x91, 0x7C, 0x97, 0x51, 0x07, 0x99,
  29482. 0xF3, 0x14, 0x91, 0x31, 0x47, 0x7E, 0x16, 0x66,
  29483. 0x3D, 0x31, 0x74, 0xC7, 0xC1, 0xCA, 0xEA, 0x78,
  29484. 0x85, 0x35, 0xC6, 0xC0, 0x05, 0xA6, 0x4F, 0x28,
  29485. 0x68, 0x63, 0x1B, 0x31, 0xB6, 0x6E, 0x20, 0x5F,
  29486. 0xD3, 0x8C, 0x1D, 0x84, 0x54, 0x2D, 0x0F, 0x1B,
  29487. 0x57, 0x8F, 0x58, 0xC9, 0xBF, 0x5A, 0x0F, 0xAE,
  29488. 0xAB, 0x6A, 0xB6, 0x49, 0x48, 0x93, 0x05, 0x31,
  29489. 0x65, 0xEA, 0xFD, 0x46, 0x5F, 0xC6, 0x4A, 0x0C,
  29490. 0x5F, 0x8F, 0x3F, 0x90, 0x03, 0x48, 0x94, 0x15,
  29491. 0x89, 0x9D, 0x59, 0xA5, 0x43, 0xD8, 0x20, 0x8C,
  29492. 0x54, 0xA3, 0x16, 0x65, 0x29, 0xB5, 0x39, 0x22,
  29493. 0x8A, 0x39, 0xE8, 0x7D, 0x53, 0x1F, 0x35, 0x27,
  29494. 0xC2, 0x07, 0xED, 0xCC, 0x1D, 0xB7, 0xFA, 0xDD,
  29495. 0xCF, 0x96, 0x28, 0x39, 0x18, 0x79, 0xB3, 0x35,
  29496. 0xC7, 0x07, 0x83, 0x9A, 0x0D, 0xB0, 0x51, 0xA8,
  29497. 0x86, 0x26, 0xED, 0x79, 0xD4, 0x51, 0x14, 0x08,
  29498. 0x00, 0xE0, 0x3B, 0x59, 0xB9, 0x56, 0xF8, 0x21,
  29499. 0x0E, 0x55, 0x60, 0x67, 0x40, 0x7D, 0x13, 0xDC,
  29500. 0x90, 0xFA, 0x9E, 0x8B, 0x87, 0x2B, 0xFB, 0x8F
  29501. };
  29502. const byte kyber1024_ct[] = {
  29503. 0xA6, 0xAF, 0x29, 0xD5, 0xF5, 0xB8, 0x0B, 0xD1,
  29504. 0x30, 0xF5, 0x18, 0xBA, 0xDD, 0xD6, 0xC8, 0xF1,
  29505. 0x75, 0x45, 0x41, 0x3D, 0x86, 0x0F, 0xB3, 0xDE,
  29506. 0x45, 0x19, 0x79, 0xEB, 0xFA, 0x5E, 0x4E, 0x31,
  29507. 0x12, 0xC7, 0xC0, 0xAD, 0xF9, 0x98, 0x24, 0xBB,
  29508. 0x52, 0x6F, 0x2C, 0x35, 0x50, 0x74, 0x8E, 0xD0,
  29509. 0xE1, 0x34, 0xF0, 0x45, 0x7A, 0x7C, 0x61, 0xF9,
  29510. 0xF5, 0x26, 0xF0, 0x02, 0xBA, 0xAD, 0xC0, 0x3F,
  29511. 0xC1, 0x3E, 0x38, 0x13, 0x12, 0x19, 0x51, 0x3C,
  29512. 0x3E, 0xDE, 0x06, 0x16, 0x61, 0xE7, 0x4F, 0x60,
  29513. 0x3C, 0x4F, 0xCF, 0x79, 0x51, 0xC8, 0xE5, 0x2C,
  29514. 0x9C, 0x21, 0x3B, 0x0D, 0x22, 0xD9, 0x29, 0x36,
  29515. 0x63, 0xD6, 0x69, 0xA6, 0xB5, 0x8E, 0xD8, 0xFC,
  29516. 0xEF, 0xCF, 0x82, 0x49, 0xD7, 0xBB, 0x52, 0x98,
  29517. 0xF5, 0x57, 0x61, 0x44, 0x5B, 0x2B, 0x83, 0xCE,
  29518. 0x7F, 0x00, 0x5C, 0xB0, 0x42, 0x48, 0xAE, 0xC8,
  29519. 0xBD, 0xA2, 0x2F, 0xD2, 0xD4, 0x2A, 0xA7, 0x66,
  29520. 0x32, 0x20, 0x14, 0xEA, 0x03, 0x8C, 0xC3, 0x2C,
  29521. 0x55, 0xC8, 0xE4, 0xB9, 0xE2, 0x8E, 0xC9, 0x11,
  29522. 0x9F, 0x52, 0x73, 0x41, 0xE4, 0xF6, 0x6A, 0x03,
  29523. 0x51, 0x21, 0x07, 0x3B, 0x85, 0xDE, 0x67, 0x06,
  29524. 0xDA, 0x19, 0xE0, 0x83, 0x8A, 0x9F, 0x33, 0xB7,
  29525. 0x19, 0xA6, 0x8F, 0x03, 0x9B, 0x66, 0x4D, 0xC0,
  29526. 0x02, 0x65, 0x9E, 0xAB, 0xFC, 0x39, 0x86, 0x79,
  29527. 0xAA, 0x70, 0x09, 0xCE, 0x0C, 0xD0, 0x1C, 0xDA,
  29528. 0xFB, 0x6C, 0xD2, 0xA2, 0x6F, 0xE4, 0x10, 0x16,
  29529. 0x72, 0xC9, 0x8F, 0xF5, 0x8F, 0x7C, 0x47, 0xD5,
  29530. 0xBD, 0xA2, 0x90, 0x66, 0x53, 0xB3, 0xA6, 0xF9,
  29531. 0x65, 0x1F, 0x7A, 0x12, 0x1E, 0xA7, 0x7E, 0xA7,
  29532. 0x47, 0x23, 0xFA, 0xE5, 0xB8, 0x73, 0xF9, 0xBB,
  29533. 0x7B, 0x66, 0x4F, 0x0C, 0x8A, 0x93, 0x83, 0x1E,
  29534. 0xF9, 0xD5, 0x1C, 0x7C, 0xC1, 0xEF, 0x44, 0xAC,
  29535. 0x0E, 0x55, 0xA5, 0x5C, 0xA7, 0x6D, 0x13, 0x7F,
  29536. 0xE9, 0xB7, 0x5F, 0x40, 0x50, 0x9C, 0xEF, 0x15,
  29537. 0x6E, 0x5A, 0xD1, 0x8F, 0x9F, 0xB9, 0x99, 0x68,
  29538. 0x00, 0x08, 0xE5, 0x47, 0xD5, 0x5E, 0xEC, 0xD5,
  29539. 0xB4, 0xD1, 0xCB, 0x1D, 0x9F, 0x07, 0x6C, 0xEC,
  29540. 0x21, 0x50, 0x1C, 0x74, 0x02, 0x50, 0x9E, 0xCB,
  29541. 0x77, 0xAF, 0xB2, 0xCB, 0x9A, 0x61, 0x34, 0x0A,
  29542. 0x8B, 0xD1, 0x51, 0x4C, 0x6E, 0x71, 0xB4, 0xAA,
  29543. 0x45, 0xE4, 0x7E, 0xC3, 0x75, 0x12, 0x27, 0x1B,
  29544. 0x91, 0x1F, 0x8F, 0xB4, 0x6C, 0x90, 0x82, 0xC9,
  29545. 0xDF, 0x07, 0x20, 0x4A, 0xBB, 0x5A, 0x50, 0xE6,
  29546. 0xE3, 0x64, 0x7A, 0x8A, 0xD4, 0xD8, 0xD5, 0xD7,
  29547. 0xBF, 0xF1, 0x9C, 0x8A, 0x50, 0x93, 0x08, 0xBC,
  29548. 0xFB, 0x89, 0x55, 0x36, 0xD0, 0x45, 0xCA, 0x2B,
  29549. 0x97, 0xCB, 0x16, 0xA2, 0x9B, 0xB7, 0x18, 0x1C,
  29550. 0xAD, 0x05, 0x09, 0xDD, 0xB9, 0x17, 0x35, 0x02,
  29551. 0x8E, 0xBA, 0x8C, 0x31, 0xD7, 0x4B, 0xD2, 0x75,
  29552. 0xEA, 0xA6, 0x5B, 0x53, 0x40, 0xB3, 0xA4, 0x3F,
  29553. 0xBF, 0xE0, 0xB3, 0x06, 0x1D, 0x6B, 0xAE, 0x7E,
  29554. 0x75, 0xB7, 0x09, 0x8C, 0xDA, 0xBE, 0x91, 0xD4,
  29555. 0xB3, 0x1E, 0x36, 0xC9, 0xAA, 0x7A, 0x82, 0x98,
  29556. 0x86, 0x2A, 0xD6, 0x3C, 0x8F, 0xD2, 0x82, 0xE0,
  29557. 0x3B, 0x46, 0x0B, 0x3A, 0xB4, 0x64, 0xCE, 0x0F,
  29558. 0x27, 0xB1, 0xC3, 0xD1, 0x11, 0x55, 0xAC, 0xAA,
  29559. 0x01, 0x1E, 0xB9, 0xE2, 0xAE, 0x3E, 0x6D, 0xDA,
  29560. 0x07, 0xD6, 0xF4, 0x91, 0x73, 0x7C, 0xBC, 0xE9,
  29561. 0xB0, 0x5F, 0x9B, 0xC5, 0x6B, 0xE2, 0x0E, 0x8D,
  29562. 0x32, 0x6B, 0xA1, 0x32, 0xC5, 0x7F, 0xB2, 0x35,
  29563. 0x16, 0x11, 0x44, 0x51, 0x9C, 0xDF, 0x40, 0x56,
  29564. 0x0F, 0xBE, 0x27, 0x9B, 0xDE, 0x41, 0x1E, 0x11,
  29565. 0x25, 0x31, 0xF8, 0x26, 0xD6, 0xAB, 0x10, 0xD4,
  29566. 0x54, 0x73, 0x50, 0xAD, 0xD2, 0xA9, 0xDE, 0x8D,
  29567. 0x62, 0xC2, 0xAC, 0x82, 0xCA, 0xBE, 0x68, 0x15,
  29568. 0x64, 0x6F, 0x4D, 0xC9, 0x74, 0x2B, 0xB0, 0xC2,
  29569. 0xA3, 0xF7, 0x7E, 0xC7, 0xB4, 0x6C, 0x6B, 0x53,
  29570. 0x76, 0x05, 0xFA, 0x31, 0x79, 0x8C, 0xD8, 0x92,
  29571. 0x81, 0x22, 0x1A, 0x33, 0xDF, 0xB9, 0x79, 0x6E,
  29572. 0x64, 0x43, 0x05, 0x63, 0x03, 0x32, 0xC2, 0xCB,
  29573. 0x93, 0x14, 0x08, 0xAB, 0x48, 0x1A, 0x16, 0xD9,
  29574. 0x53, 0xF6, 0xBE, 0xAE, 0x38, 0x91, 0xD6, 0xD9,
  29575. 0xAC, 0x1F, 0xAB, 0x38, 0x22, 0x2D, 0x92, 0x71,
  29576. 0x87, 0x2D, 0x9D, 0x0C, 0xAD, 0xB9, 0x1A, 0xBE,
  29577. 0x9B, 0x4E, 0x26, 0x5F, 0x75, 0xC6, 0xE5, 0xE8,
  29578. 0x29, 0xE1, 0x46, 0xC3, 0xD8, 0xCE, 0x1E, 0x9D,
  29579. 0x12, 0xE0, 0xD1, 0x29, 0x80, 0x19, 0x57, 0xF4,
  29580. 0x6B, 0x0D, 0x2D, 0xBE, 0x1F, 0x74, 0x9B, 0x1D,
  29581. 0x08, 0xE2, 0x34, 0x5F, 0x62, 0x39, 0xA7, 0x31,
  29582. 0x34, 0x2E, 0xB7, 0x5B, 0x0C, 0xF1, 0xBF, 0x41,
  29583. 0x17, 0x49, 0xBC, 0x2C, 0xAF, 0x28, 0x10, 0xB7,
  29584. 0x88, 0xC6, 0xB7, 0x23, 0x8B, 0x4D, 0x3D, 0xA2,
  29585. 0xD6, 0x31, 0x5C, 0xE9, 0x54, 0x2E, 0x24, 0x40,
  29586. 0x4F, 0x14, 0x57, 0x55, 0xA3, 0x0A, 0xB8, 0x51,
  29587. 0xE4, 0x44, 0x58, 0x41, 0xBD, 0x33, 0xF7, 0x16,
  29588. 0xA5, 0x86, 0x88, 0x48, 0x88, 0xEC, 0xC6, 0xBC,
  29589. 0x64, 0x98, 0xAA, 0x32, 0x91, 0x9A, 0xE8, 0x1D,
  29590. 0x20, 0xC2, 0x69, 0x73, 0xC2, 0xBD, 0x54, 0x58,
  29591. 0x2A, 0x0F, 0x6A, 0xD9, 0x8A, 0xBF, 0xD2, 0x62,
  29592. 0x7E, 0x15, 0x69, 0x0A, 0x72, 0x7E, 0x69, 0xF5,
  29593. 0x81, 0xDD, 0x2A, 0x71, 0x27, 0x98, 0x2A, 0x90,
  29594. 0xE3, 0x3E, 0x2D, 0x4A, 0x03, 0xFE, 0x33, 0x91,
  29595. 0x42, 0xC7, 0xE4, 0x4C, 0x32, 0x6A, 0xC4, 0x6E,
  29596. 0xD3, 0x95, 0xA2, 0x25, 0xD3, 0x03, 0x33, 0x89,
  29597. 0x91, 0x73, 0x28, 0xB4, 0x53, 0x16, 0xB1, 0x58,
  29598. 0x5A, 0x01, 0xB2, 0xC3, 0x04, 0xB2, 0x94, 0x4E,
  29599. 0x90, 0x3A, 0xBB, 0xB3, 0xEC, 0x56, 0x19, 0x44,
  29600. 0x1C, 0xFC, 0x89, 0x65, 0xA4, 0x46, 0xDF, 0x75,
  29601. 0xDE, 0xFA, 0x80, 0xC6, 0xE1, 0x5A, 0xDB, 0xD5,
  29602. 0x06, 0xB7, 0xAB, 0x2D, 0xE1, 0x2D, 0xDA, 0x9B,
  29603. 0xC8, 0x14, 0x41, 0xCF, 0xC8, 0x90, 0x52, 0xE2,
  29604. 0xE5, 0x80, 0x8F, 0x71, 0x26, 0xC6, 0xFD, 0x3A,
  29605. 0xC6, 0xAC, 0x80, 0x81, 0x25, 0x8A, 0x84, 0xA0,
  29606. 0x9A, 0xE5, 0x0F, 0x6C, 0xD7, 0xCC, 0x0F, 0x4A,
  29607. 0xF3, 0x36, 0xFD, 0x1D, 0x64, 0x3E, 0x99, 0x07,
  29608. 0x99, 0x96, 0x26, 0x8C, 0x2D, 0x32, 0xD9, 0x09,
  29609. 0xF2, 0x2E, 0x35, 0x04, 0xF0, 0x7F, 0xBB, 0x56,
  29610. 0x31, 0x96, 0xD4, 0x31, 0x2F, 0xDD, 0xB9, 0x33,
  29611. 0x5D, 0x5C, 0x1D, 0x36, 0xE8, 0xC5, 0xEE, 0xA2,
  29612. 0x27, 0x8D, 0xBA, 0x23, 0xB9, 0x4D, 0x19, 0x3C,
  29613. 0x94, 0x7C, 0xC4, 0x1C, 0xA9, 0x93, 0xDC, 0x7D,
  29614. 0xB1, 0x39, 0x63, 0x40, 0xAD, 0x9C, 0x4F, 0xE6,
  29615. 0x87, 0xDD, 0x7B, 0x8D, 0x0C, 0x7A, 0x51, 0x20,
  29616. 0xAE, 0x02, 0x04, 0xF2, 0xC6, 0x65, 0xBD, 0x5F,
  29617. 0x47, 0x3D, 0x64, 0x4C, 0x7F, 0xF2, 0x6B, 0xFF,
  29618. 0xBA, 0x7A, 0x36, 0x98, 0x08, 0x30, 0x70, 0x21,
  29619. 0x28, 0xA7, 0xE6, 0x61, 0xD6, 0x77, 0xA0, 0x92,
  29620. 0xA3, 0x6E, 0x74, 0x28, 0xA4, 0x13, 0x9F, 0xB2,
  29621. 0x9B, 0x00, 0x95, 0xCC, 0x11, 0x08, 0x6F, 0x44,
  29622. 0x7D, 0x2A, 0x9E, 0xF6, 0xC9, 0xB1, 0x61, 0xF1,
  29623. 0x89, 0xC6, 0x29, 0x9E, 0x08, 0x4C, 0xB7, 0xAA,
  29624. 0x00, 0xFA, 0xF7, 0x87, 0x79, 0x7B, 0xFB, 0x06,
  29625. 0x9F, 0xBC, 0x08, 0x7F, 0xDE, 0x26, 0x25, 0x2A,
  29626. 0x16, 0x64, 0xF1, 0x9C, 0x5A, 0x8A, 0x22, 0xEC,
  29627. 0x5E, 0xE1, 0xAE, 0xB0, 0x76, 0x35, 0x7B, 0x7D,
  29628. 0xC3, 0x7E, 0x6B, 0x0F, 0x15, 0x20, 0xF9, 0x58,
  29629. 0xF7, 0x85, 0x1B, 0xAC, 0xB9, 0x2C, 0x89, 0xFD,
  29630. 0x11, 0x4A, 0x72, 0xFE, 0xAC, 0x54, 0x65, 0x2D,
  29631. 0x45, 0xB0, 0x9E, 0x1A, 0xE7, 0x65, 0x1A, 0xBD,
  29632. 0x16, 0x4B, 0xCD, 0x53, 0x7D, 0x58, 0xFA, 0x39,
  29633. 0xD3, 0xEC, 0x8A, 0xCD, 0xCD, 0xF9, 0x84, 0x25,
  29634. 0x00, 0x58, 0x62, 0xFA, 0x59, 0x69, 0x2D, 0xE1,
  29635. 0x62, 0xB7, 0x7E, 0x62, 0x97, 0xC6, 0x62, 0x33,
  29636. 0x34, 0x84, 0x08, 0xA8, 0xAB, 0x69, 0x5C, 0xE2,
  29637. 0xF2, 0x72, 0x8D, 0xB9, 0xFB, 0xE2, 0x7E, 0x95,
  29638. 0x89, 0x67, 0xEC, 0x59, 0x74, 0x76, 0x7C, 0x5A,
  29639. 0x66, 0x02, 0x30, 0x74, 0xB4, 0xA7, 0x1A, 0xFD,
  29640. 0x26, 0x4A, 0xD2, 0x89, 0x0E, 0x97, 0x0A, 0x1F,
  29641. 0x31, 0xD6, 0xE3, 0x31, 0x1B, 0x73, 0x6F, 0x9F,
  29642. 0x94, 0x88, 0x79, 0x3D, 0xDC, 0x88, 0xF2, 0x34,
  29643. 0x58, 0x06, 0x42, 0x54, 0xC8, 0x2A, 0x1D, 0x9E,
  29644. 0x59, 0xEA, 0xD2, 0xFC, 0xEC, 0x40, 0xB4, 0x30,
  29645. 0x68, 0x7C, 0x4B, 0x7E, 0x28, 0x96, 0x09, 0x26,
  29646. 0xAF, 0xCA, 0xCC, 0x9B, 0xD7, 0x56, 0xA7, 0x10,
  29647. 0x88, 0xC7, 0x84, 0x50, 0xE2, 0x0A, 0x2E, 0x98,
  29648. 0x0A, 0xED, 0xE9, 0xEB, 0xED, 0xFE, 0x7F, 0xAB,
  29649. 0xD6, 0xAB, 0xFE, 0x96, 0xF9, 0x34, 0xC4, 0xB0,
  29650. 0x2C, 0x01, 0xCA, 0x19, 0x4D, 0x01, 0xB7, 0x3C,
  29651. 0x25, 0xD5, 0x99, 0x70, 0x39, 0xD3, 0xFC, 0xD0,
  29652. 0xF0, 0x99, 0x52, 0x1F, 0x70, 0xCA, 0xEE, 0x69,
  29653. 0x11, 0x0A, 0xC1, 0xFC, 0x5A, 0x99, 0x91, 0x7A,
  29654. 0xD7, 0x52, 0xFC, 0x96, 0xAD, 0xFA, 0xD7, 0x18,
  29655. 0x6D, 0x0A, 0x7C, 0x9C, 0xFE, 0x56, 0x01, 0xC0,
  29656. 0x75, 0x14, 0xEA, 0x64, 0x48, 0xD6, 0x61, 0xC5,
  29657. 0x7A, 0xA2, 0x02, 0x42, 0x10, 0x3C, 0x42, 0x76,
  29658. 0xA0, 0x70, 0xA4, 0x89, 0xA4, 0xCB, 0x6B, 0xCA,
  29659. 0x0F, 0x9E, 0xCC, 0x43, 0x79, 0xFB, 0x22, 0x02,
  29660. 0x15, 0xFD, 0x91, 0xF8, 0x10, 0x19, 0xD5, 0xB0,
  29661. 0xAE, 0x61, 0x93, 0x58, 0xB5, 0x24, 0x68, 0xF2,
  29662. 0x72, 0xC1, 0x78, 0xE3, 0xA7, 0x4C, 0xF6, 0x77,
  29663. 0x5A, 0xA9, 0x24, 0xFE, 0x32, 0x9C, 0x31, 0x75,
  29664. 0xD9, 0xE4, 0xC3, 0xE2, 0x1A, 0xB9, 0xEC, 0x83,
  29665. 0x6E, 0xDC, 0x3A, 0xCA, 0xB2, 0xE3, 0x89, 0x1E,
  29666. 0xE8, 0xDE, 0xDA, 0x51, 0x5D, 0x39, 0xAF, 0x9B,
  29667. 0x8D, 0xDD, 0x0E, 0xE7, 0xB0, 0x16, 0x4F, 0x80,
  29668. 0x5C, 0x38, 0x35, 0xF6, 0xD2, 0xBA, 0xBD, 0xB3,
  29669. 0x0E, 0xAB, 0x47, 0x56, 0xE7, 0xEC, 0x7F, 0x82,
  29670. 0x9E, 0xCE, 0x01, 0xE8, 0xEA, 0xDF, 0xBB, 0xED,
  29671. 0x12, 0xFC, 0x28, 0x3B, 0x3D, 0x4C, 0x69, 0xF5,
  29672. 0x75, 0xE7, 0xF8, 0x04, 0x17, 0x68, 0x9F, 0xDF,
  29673. 0xCF, 0xC7, 0xBE, 0x27, 0xEE, 0x3B, 0x8C, 0xDF,
  29674. 0x57, 0xAA, 0xEB, 0xEC, 0x4A, 0x95, 0xB7, 0xE5,
  29675. 0xBB, 0x58, 0x5B, 0x85, 0x22, 0x7F, 0x7C, 0x32,
  29676. 0xBE, 0x30, 0xDB, 0x3E, 0x65, 0xE4, 0x2E, 0x30,
  29677. 0xDC, 0xF5, 0xA5, 0xFA, 0x07, 0x3D, 0xBA, 0x39,
  29678. 0x9D, 0x94, 0x2F, 0x22, 0x22, 0xAD, 0xB9, 0xB9,
  29679. 0x89, 0x81, 0x02, 0xAF, 0xE5, 0x43, 0x2E, 0xDC,
  29680. 0x7F, 0x04, 0xAE, 0x34, 0xA8, 0xFE, 0xC2, 0xD8,
  29681. 0x1C, 0xB4, 0x9A, 0x9A, 0x9B, 0x43, 0x81, 0x4C,
  29682. 0xE7, 0x1D, 0x97, 0xF7, 0x26, 0xE2, 0xB1, 0xE8,
  29683. 0xF6, 0x4B, 0x50, 0xE6, 0x5D, 0xFB, 0x48, 0x16,
  29684. 0xE1, 0x2E, 0x82, 0xA3, 0x19, 0x74, 0x84, 0xA4,
  29685. 0xE9, 0xBB, 0xA4, 0xD2, 0xD6, 0x9E, 0x3F, 0x19,
  29686. 0xD0, 0xB7, 0x5C, 0x21, 0xE2, 0xBF, 0xFE, 0x9F,
  29687. 0xC0, 0xC9, 0x8C, 0xF4, 0x8A, 0x3A, 0xAF, 0x08,
  29688. 0xD4, 0x67, 0xF7, 0x26, 0x87, 0xDF, 0x01, 0x78,
  29689. 0x17, 0x4B, 0x78, 0x97, 0xF7, 0x34, 0x34, 0x9B,
  29690. 0x18, 0x1E, 0xCA, 0x86, 0xA5, 0x98, 0xA0, 0xC5,
  29691. 0xE8, 0xC2, 0x59, 0x46, 0xF2, 0x4D, 0xC5, 0x57,
  29692. 0x2B, 0xD3, 0x24, 0xA4, 0x04, 0x58, 0xA7, 0x88,
  29693. 0xE5, 0x13, 0x7F, 0x3C, 0x7A, 0x7C, 0x97, 0xFC,
  29694. 0x9F, 0x12, 0xA3, 0xC4, 0x63, 0xA8, 0xFE, 0x94,
  29695. 0x49, 0x10, 0x1C, 0xCE, 0x96, 0x6D, 0x7C, 0x00,
  29696. 0x93, 0x23, 0x93, 0x29, 0x98, 0xD5, 0x6E, 0xF4,
  29697. 0x30, 0xC7, 0x3B, 0xC2, 0x4F, 0x5D, 0x95, 0xF7,
  29698. 0x37, 0x85, 0x8D, 0xDC, 0x4F, 0x32, 0xC0, 0x13
  29699. };
  29700. const byte kyber1024_ss[] = {
  29701. 0xB1, 0x0F, 0x73, 0x94, 0x92, 0x6A, 0xD3, 0xB4,
  29702. 0x9C, 0x5D, 0x62, 0xD5, 0xAE, 0xB5, 0x31, 0xD5,
  29703. 0x75, 0x75, 0x38, 0xBC, 0xC0, 0xDA, 0x9E, 0x55,
  29704. 0x0D, 0x43, 0x8F, 0x1B, 0x61, 0xBD, 0x74, 0x19
  29705. };
  29706. #else
  29707. const byte kyber1024_pk[] = {
  29708. 0x68, 0xE6, 0xBC, 0x26, 0x8B, 0x9E, 0x36, 0x1B,
  29709. 0x8F, 0x6E, 0x0A, 0xBC, 0xFE, 0x88, 0x93, 0x37,
  29710. 0x87, 0x53, 0x48, 0xE4, 0x16, 0x4D, 0x66, 0x15,
  29711. 0xF3, 0x82, 0xC6, 0xC7, 0xC4, 0x17, 0x8F, 0x75,
  29712. 0x1F, 0x72, 0x2B, 0x21, 0x27, 0x74, 0x64, 0xFB,
  29713. 0x83, 0x1F, 0x8A, 0xA9, 0xA0, 0x18, 0xC1, 0x9A,
  29714. 0x51, 0xB0, 0x7F, 0xA0, 0x93, 0x9B, 0xA5, 0xC2,
  29715. 0x06, 0x68, 0x63, 0x23, 0xAF, 0xF6, 0xCA, 0x83,
  29716. 0x0B, 0x76, 0xE3, 0x80, 0x02, 0x63, 0x54, 0x47,
  29717. 0x7A, 0xAC, 0x58, 0xD8, 0x93, 0x1D, 0x80, 0x3B,
  29718. 0x6F, 0xAB, 0xD7, 0x12, 0xD1, 0x72, 0x81, 0x4A,
  29719. 0x54, 0xB8, 0x12, 0xE7, 0x92, 0x47, 0x94, 0xAA,
  29720. 0x40, 0x02, 0x49, 0xD5, 0xC0, 0x46, 0x8E, 0xA3,
  29721. 0x2F, 0xB2, 0x15, 0x54, 0xC5, 0x93, 0x67, 0x90,
  29722. 0x3C, 0x60, 0xA8, 0xB4, 0x8E, 0x23, 0x40, 0x71,
  29723. 0x2F, 0x7A, 0x04, 0x5A, 0x67, 0xC2, 0x3C, 0x72,
  29724. 0x41, 0x3A, 0x64, 0x69, 0xFC, 0x83, 0x69, 0xA2,
  29725. 0x91, 0x03, 0x3C, 0x46, 0xCE, 0xA3, 0x96, 0x76,
  29726. 0x65, 0x6B, 0x6C, 0xD8, 0x57, 0x99, 0x7C, 0x04,
  29727. 0x5C, 0x5A, 0xF0, 0x34, 0xE1, 0xE1, 0x14, 0x8A,
  29728. 0x8B, 0x40, 0xCD, 0x16, 0x68, 0x31, 0x93, 0xA3,
  29729. 0x60, 0x0C, 0x84, 0xFC, 0xE3, 0xB9, 0x65, 0x10,
  29730. 0x88, 0xDD, 0x09, 0x44, 0x57, 0x6C, 0x7E, 0xA4,
  29731. 0x07, 0x2D, 0x25, 0xF0, 0x67, 0xD2, 0x42, 0x7E,
  29732. 0x26, 0xD1, 0x86, 0xF9, 0xB7, 0x7A, 0x32, 0x79,
  29733. 0x05, 0x03, 0x3B, 0xA9, 0x40, 0x85, 0xA8, 0xAB,
  29734. 0x6A, 0x8A, 0x2B, 0x86, 0x08, 0x0C, 0x45, 0x2B,
  29735. 0x10, 0x90, 0xAC, 0xC9, 0x5A, 0x2B, 0x9F, 0x5A,
  29736. 0x6E, 0x61, 0x84, 0x6D, 0x63, 0x45, 0x66, 0xFA,
  29737. 0xC0, 0xB0, 0xFF, 0x35, 0x11, 0x18, 0x06, 0x18,
  29738. 0xAF, 0x98, 0x34, 0x87, 0x12, 0x2C, 0xFB, 0x63,
  29739. 0x61, 0xFF, 0x66, 0x0D, 0x5C, 0xA0, 0x0B, 0x9D,
  29740. 0x88, 0xB1, 0xDA, 0x18, 0x36, 0xC9, 0x78, 0x6E,
  29741. 0x55, 0x27, 0x6C, 0x88, 0x53, 0x8C, 0xC1, 0xD3,
  29742. 0x05, 0xFA, 0xAA, 0xB7, 0x79, 0x9A, 0x35, 0x46,
  29743. 0xF0, 0xBE, 0x6C, 0xC7, 0x00, 0xB0, 0x65, 0xB9,
  29744. 0x03, 0x4A, 0x91, 0x63, 0x31, 0x74, 0x66, 0xD9,
  29745. 0x83, 0xDD, 0xC2, 0xAA, 0x5B, 0xB0, 0x3F, 0x5B,
  29746. 0x67, 0x33, 0x48, 0xD5, 0x76, 0x02, 0x8A, 0x02,
  29747. 0x55, 0x15, 0x6C, 0x6A, 0x29, 0x74, 0x46, 0x98,
  29748. 0x34, 0xFB, 0x15, 0x73, 0x23, 0x74, 0x60, 0x54,
  29749. 0xC4, 0xBA, 0x96, 0x40, 0x30, 0x4A, 0x11, 0x63,
  29750. 0x04, 0xF0, 0x18, 0x21, 0x72, 0xBA, 0x8D, 0x03,
  29751. 0xAE, 0xDA, 0x39, 0x91, 0x6A, 0xF1, 0x18, 0xB9,
  29752. 0x84, 0x22, 0xB7, 0x16, 0x3A, 0xB6, 0xE2, 0x47,
  29753. 0xFC, 0xAA, 0x9D, 0x7C, 0x7C, 0xAE, 0x96, 0xBB,
  29754. 0x60, 0xB1, 0xD7, 0x86, 0x0E, 0x4C, 0x7F, 0x1D,
  29755. 0x92, 0x0D, 0x31, 0xA5, 0x41, 0x38, 0x82, 0x29,
  29756. 0xEE, 0x84, 0x2C, 0xA1, 0x59, 0x43, 0x89, 0x4A,
  29757. 0x56, 0xE5, 0x14, 0x0D, 0x29, 0x59, 0xC3, 0x6E,
  29758. 0xA2, 0x6E, 0x2D, 0xE0, 0x51, 0x21, 0x36, 0x6B,
  29759. 0xE4, 0x14, 0x02, 0x7B, 0xD1, 0x47, 0x81, 0x47,
  29760. 0x2E, 0xDD, 0xA4, 0x71, 0x10, 0xD0, 0xB3, 0xAE,
  29761. 0x2A, 0xA2, 0x55, 0xAB, 0x6D, 0x6F, 0xE0, 0x1C,
  29762. 0x26, 0xC2, 0x87, 0x63, 0x97, 0x6B, 0xBF, 0xA9,
  29763. 0x2F, 0x4F, 0x09, 0xA5, 0xB7, 0x3C, 0x47, 0x1F,
  29764. 0x8C, 0x9E, 0x53, 0x26, 0x92, 0x55, 0xDA, 0xC1,
  29765. 0xE0, 0x71, 0x5E, 0x87, 0x00, 0x06, 0x09, 0x70,
  29766. 0x60, 0xFF, 0x23, 0xC3, 0xAB, 0x43, 0xC4, 0x19,
  29767. 0xE4, 0x48, 0xC7, 0x30, 0x40, 0x10, 0xC4, 0x0F,
  29768. 0x3D, 0xC0, 0xB7, 0x26, 0x39, 0x2E, 0x46, 0x03,
  29769. 0x7E, 0xB9, 0x49, 0x04, 0x55, 0xE3, 0x49, 0xA4,
  29770. 0x35, 0xCD, 0xA9, 0x0A, 0xA3, 0xB7, 0x47, 0x8C,
  29771. 0x32, 0xE8, 0x72, 0xB2, 0xB8, 0x5C, 0xEC, 0xF0,
  29772. 0x3D, 0xDC, 0xD6, 0x16, 0x33, 0x78, 0xBF, 0x73,
  29773. 0xB1, 0x3B, 0x2E, 0x1A, 0x7B, 0x6E, 0xBC, 0x39,
  29774. 0x29, 0xEC, 0x08, 0x63, 0x89, 0x3F, 0x73, 0x8C,
  29775. 0x38, 0x9F, 0x3B, 0x3B, 0x2B, 0xB5, 0x4F, 0xC1,
  29776. 0x5B, 0xB7, 0x8A, 0x25, 0x31, 0xEE, 0xBC, 0xB7,
  29777. 0xBC, 0x10, 0x81, 0x07, 0x96, 0x59, 0x81, 0xD2,
  29778. 0x0A, 0x7D, 0x1A, 0x87, 0x21, 0xBA, 0x36, 0x40,
  29779. 0x03, 0x13, 0x2F, 0x03, 0x6C, 0xE4, 0x0C, 0x9E,
  29780. 0x75, 0x5B, 0x2A, 0xB4, 0x86, 0x1D, 0x04, 0xC1,
  29781. 0xBE, 0x0D, 0x53, 0x97, 0x91, 0x4B, 0x6C, 0xD4,
  29782. 0xAA, 0x40, 0x8C, 0x62, 0x2B, 0x21, 0xB1, 0x1B,
  29783. 0xA0, 0xEB, 0xA6, 0x17, 0x29, 0x59, 0x6F, 0x8B,
  29784. 0x0E, 0x82, 0x14, 0xAA, 0x4A, 0xA6, 0x01, 0x86,
  29785. 0x51, 0x07, 0xA2, 0xA7, 0x6A, 0xD7, 0xCC, 0x2F,
  29786. 0x97, 0x43, 0x00, 0x18, 0x28, 0xB8, 0x0B, 0x24,
  29787. 0x80, 0x2B, 0x98, 0x9D, 0x98, 0x42, 0x5E, 0x14,
  29788. 0x7C, 0x6D, 0x93, 0x32, 0xBA, 0x08, 0x4B, 0x8F,
  29789. 0xFB, 0x96, 0x27, 0xE3, 0xFB, 0xC4, 0xBE, 0x65,
  29790. 0x38, 0xEB, 0xDA, 0x1D, 0x9D, 0xB9, 0xB6, 0x75,
  29791. 0xB0, 0x16, 0x66, 0x57, 0x23, 0xD4, 0x11, 0x71,
  29792. 0x54, 0xF8, 0x13, 0x3E, 0xA9, 0x00, 0xCB, 0x47,
  29793. 0x0B, 0x1D, 0xC5, 0xB5, 0x7A, 0x78, 0x11, 0x16,
  29794. 0x72, 0x9E, 0xD6, 0x29, 0x45, 0x6E, 0x1A, 0x7D,
  29795. 0x6A, 0x37, 0x9F, 0x2E, 0x01, 0x0A, 0xCB, 0xEC,
  29796. 0x21, 0xFC, 0xD1, 0xC2, 0xB0, 0xBB, 0x61, 0xC3,
  29797. 0xDA, 0x6C, 0x73, 0x70, 0x5F, 0xD7, 0xB0, 0xC3,
  29798. 0x61, 0x2A, 0xC3, 0xB4, 0xA1, 0x2E, 0xC5, 0xD3,
  29799. 0x7F, 0x4C, 0x1A, 0x4A, 0x4D, 0xF8, 0x16, 0x6E,
  29800. 0xF6, 0x68, 0x9C, 0x06, 0xB5, 0xD4, 0xA6, 0x1A,
  29801. 0x52, 0x14, 0xAE, 0x5E, 0x5C, 0x28, 0x07, 0x4C,
  29802. 0xC5, 0xFE, 0x2A, 0x41, 0x2A, 0xC5, 0x14, 0x15,
  29803. 0x94, 0x28, 0x54, 0x85, 0x15, 0x6E, 0xCA, 0x65,
  29804. 0x64, 0x28, 0x37, 0x20, 0xB0, 0xBA, 0xD1, 0xD9,
  29805. 0x74, 0x5B, 0xA5, 0x77, 0x71, 0x1A, 0xB6, 0xF2,
  29806. 0x14, 0xC5, 0x2F, 0xB5, 0xBA, 0xAF, 0x8A, 0x1F,
  29807. 0x85, 0x33, 0x5A, 0xEB, 0x44, 0x37, 0x0B, 0x39,
  29808. 0xC5, 0x6C, 0x4C, 0x3D, 0x32, 0x75, 0x55, 0x60,
  29809. 0xCB, 0x2C, 0xA9, 0x45, 0x87, 0x51, 0xF1, 0x65,
  29810. 0x4B, 0x82, 0xC5, 0xE7, 0xFB, 0x0D, 0xB5, 0x16,
  29811. 0xBE, 0x1E, 0x42, 0x43, 0x3A, 0xD4, 0x7B, 0x6B,
  29812. 0xCC, 0xB3, 0x63, 0x61, 0xC4, 0x34, 0xEB, 0x59,
  29813. 0xCF, 0xF3, 0x38, 0x49, 0x54, 0xBB, 0x76, 0xF1,
  29814. 0x44, 0x14, 0x91, 0x67, 0x91, 0x3B, 0x85, 0x0F,
  29815. 0x75, 0x28, 0xBD, 0x76, 0x87, 0xB8, 0x8B, 0x64,
  29816. 0x84, 0xA6, 0x82, 0x68, 0x39, 0x97, 0x08, 0x42,
  29817. 0xA5, 0x93, 0xBB, 0x93, 0x45, 0x5A, 0x73, 0xBC,
  29818. 0x2C, 0x74, 0x62, 0x55, 0xC1, 0x3B, 0xC8, 0x52,
  29819. 0x1C, 0x73, 0x54, 0xE9, 0x8B, 0x98, 0x76, 0xA9,
  29820. 0x58, 0x6F, 0xC3, 0x7C, 0x5F, 0x24, 0x8D, 0xA0,
  29821. 0x55, 0x75, 0x8B, 0xD9, 0x61, 0x5E, 0x68, 0xBF,
  29822. 0x93, 0xEA, 0x0B, 0xA1, 0xD0, 0x22, 0xE2, 0xD2,
  29823. 0x22, 0x04, 0xA4, 0x45, 0x8C, 0xB6, 0x12, 0xB0,
  29824. 0x29, 0x41, 0xE8, 0x7A, 0xAC, 0xF1, 0xA3, 0x29,
  29825. 0x07, 0xD9, 0x8B, 0x4B, 0x13, 0xC6, 0xC9, 0xB4,
  29826. 0xAB, 0xD6, 0xDC, 0xCF, 0x66, 0x21, 0x06, 0xF5,
  29827. 0xD5, 0x8C, 0x2F, 0xC7, 0x51, 0x97, 0x35, 0x5E,
  29828. 0xB8, 0xCC, 0x41, 0x74, 0x2C, 0x6D, 0xA0, 0x03,
  29829. 0x40, 0x6D, 0x13, 0x56, 0xF8, 0xA0, 0x7A, 0x37,
  29830. 0x97, 0x88, 0x00, 0x60, 0x67, 0xCB, 0xF5, 0x2F,
  29831. 0x22, 0xD5, 0x3D, 0x68, 0x71, 0x22, 0x41, 0x09,
  29832. 0x34, 0x6E, 0xA8, 0x3B, 0x45, 0xCA, 0x60, 0xAF,
  29833. 0xEC, 0xCA, 0x0F, 0xA5, 0x67, 0x2B, 0x36, 0x64,
  29834. 0x89, 0xEB, 0x8D, 0xD1, 0xF7, 0x20, 0x4C, 0xDB,
  29835. 0x7D, 0x9B, 0xE1, 0x87, 0xF4, 0x39, 0x7B, 0x9B,
  29836. 0x4C, 0xA0, 0x31, 0x34, 0x75, 0x0D, 0xD5, 0x7D,
  29837. 0xAD, 0xE1, 0x88, 0x91, 0xCC, 0xA9, 0x47, 0x4B,
  29838. 0x65, 0x32, 0x60, 0x52, 0x50, 0xBC, 0xBB, 0x6E,
  29839. 0x02, 0x94, 0xEF, 0x95, 0x32, 0x6D, 0x25, 0x9B,
  29840. 0x08, 0xB2, 0x93, 0x8D, 0x8C, 0x59, 0xE2, 0x58,
  29841. 0x4B, 0x69, 0x10, 0x71, 0x85, 0xF3, 0x45, 0x30,
  29842. 0x24, 0x07, 0x35, 0x17, 0xC9, 0x89, 0x00, 0x9E,
  29843. 0xBB, 0x6B, 0x22, 0xED, 0xE9, 0x70, 0x43, 0x66,
  29844. 0xC6, 0x23, 0x2A, 0x62, 0x82, 0xF3, 0xBB, 0x09,
  29845. 0x08, 0x74, 0xA6, 0xFB, 0x0D, 0x84, 0x53, 0x6D,
  29846. 0x3F, 0x61, 0x3A, 0x21, 0xD1, 0x9D, 0x98, 0x44,
  29847. 0x17, 0xFA, 0xF0, 0x84, 0x6C, 0xB4, 0x28, 0x7B,
  29848. 0x3C, 0x70, 0x9F, 0x22, 0x62, 0x26, 0xA0, 0x9E,
  29849. 0x94, 0xFA, 0x89, 0x85, 0x43, 0x87, 0x4A, 0xE2,
  29850. 0x08, 0x11, 0xE3, 0x90, 0x80, 0xE6, 0xB2, 0xCE,
  29851. 0x49, 0x92, 0x26, 0xF5, 0x13, 0x8C, 0x14, 0x38,
  29852. 0x72, 0xDB, 0x86, 0xFB, 0xC2, 0x9C, 0xFD, 0xA2,
  29853. 0x9F, 0xD2, 0x26, 0x0E, 0xC7, 0xB9, 0xA2, 0x37,
  29854. 0xD1, 0x9A, 0xAB, 0x1B, 0x93, 0xE1, 0xDA, 0x4E,
  29855. 0xBC, 0x58, 0x9D, 0x82, 0x4C, 0x58, 0x90, 0x96,
  29856. 0x99, 0xD3, 0xE8, 0xBC, 0x7F, 0x53, 0xB3, 0x55,
  29857. 0xFA, 0x31, 0xE9, 0x6B, 0x1E, 0x35, 0x4A, 0xA4,
  29858. 0xB3, 0xC8, 0x4D, 0xF3, 0xC4, 0x7F, 0x80, 0xF9,
  29859. 0x32, 0xEA, 0x68, 0x5D, 0x96, 0xC5, 0xA5, 0x96,
  29860. 0x08, 0x22, 0xC6, 0x43, 0x64, 0x5F, 0x1A, 0x63,
  29861. 0xDD, 0x60, 0x8A, 0x09, 0x71, 0xC7, 0xF2, 0x15,
  29862. 0x7C, 0xA5, 0x24, 0x4D, 0x49, 0x2C, 0x01, 0x0B,
  29863. 0x71, 0xB7, 0x74, 0xC1, 0x0C, 0x07, 0xD3, 0xC8,
  29864. 0x4C, 0xA5, 0x4D, 0xE5, 0x00, 0x1F, 0xE0, 0xAC,
  29865. 0x4E, 0x69, 0x52, 0x81, 0x4E, 0xC1, 0x0F, 0xF9,
  29866. 0x8B, 0x19, 0xE7, 0x89, 0xC1, 0xAC, 0x68, 0x45,
  29867. 0xC7, 0x62, 0x80, 0x55, 0x27, 0x5C, 0x33, 0x34,
  29868. 0x41, 0x87, 0x1B, 0x8C, 0x30, 0x99, 0x9B, 0x05,
  29869. 0x31, 0x34, 0xC0, 0xE7, 0xB1, 0xAC, 0x6A, 0xA6,
  29870. 0x63, 0x12, 0xC1, 0xA5, 0xE2, 0x97, 0x92, 0x97,
  29871. 0x02, 0x56, 0x88, 0x14, 0x38, 0xDC, 0x46, 0x29,
  29872. 0x37, 0x4D, 0xFB, 0x9A, 0x91, 0x48, 0x62, 0x9D,
  29873. 0x14, 0x8C, 0x60, 0x1D, 0x4B, 0x6A, 0xA2, 0xC2,
  29874. 0x9C, 0xCE, 0xE8, 0x94, 0x7A, 0x27, 0x87, 0x0E,
  29875. 0x4C, 0x65, 0xE4, 0x12, 0x46, 0xD6, 0x25, 0x3D,
  29876. 0xC4, 0xF1, 0x3A, 0xEB, 0x31, 0xCD, 0x7E, 0x36,
  29877. 0x2F, 0x0F, 0x28, 0x84, 0xB9, 0xF7, 0x3B, 0xA9,
  29878. 0x93, 0x50, 0xAA, 0xA4, 0x02, 0x4A, 0x73, 0x14,
  29879. 0xBE, 0x69, 0x3A, 0xE4, 0x13, 0xA5, 0xCE, 0xA0,
  29880. 0x9A, 0x27, 0x27, 0x3A, 0x0B, 0xF6, 0xA4, 0x53,
  29881. 0xF8, 0x63, 0x9B, 0x48, 0x26, 0x8D, 0x21, 0xC9,
  29882. 0xAE, 0xC2, 0x62, 0xBE, 0x28, 0xC0, 0x7E, 0xA4,
  29883. 0xB9, 0x35, 0xA8, 0x69, 0x63, 0x26, 0xAA, 0x18,
  29884. 0xA5, 0x91, 0xCE, 0xE9, 0xC9, 0xF2, 0xA7, 0x29,
  29885. 0xBC, 0x50, 0x33, 0xDD, 0xE6, 0x37, 0x27, 0x38,
  29886. 0x85, 0x92, 0x1B, 0x73, 0xB8, 0x58, 0xA1, 0x88,
  29887. 0x33, 0xA7, 0xB8, 0x3A, 0x82, 0xFE, 0x02, 0xCC,
  29888. 0x33, 0x54, 0xC3, 0x0B, 0x0C, 0x4D, 0x0D, 0x3B,
  29889. 0xA1, 0x33, 0x1C, 0x6E, 0xC7, 0xA7, 0x0B, 0x7E,
  29890. 0x50, 0x3F, 0x76, 0x47, 0xA9, 0x52, 0xB0, 0x03,
  29891. 0x86, 0xCC, 0x90, 0xB3, 0x4A, 0x51, 0x4F, 0x99,
  29892. 0x5A, 0x56, 0x92, 0x05, 0xEB, 0x2A, 0x3F, 0xA7,
  29893. 0x9A, 0x78, 0x65, 0x21, 0x18, 0xD4, 0xBC, 0x48,
  29894. 0x46, 0x27, 0xCD, 0x02, 0x33, 0xBC, 0x5A, 0x35,
  29895. 0x16, 0x15, 0x0B, 0xAF, 0x6E, 0x10, 0x66, 0x5D,
  29896. 0xA7, 0x61, 0x8F, 0x28, 0x41, 0xB5, 0x02, 0x61,
  29897. 0x72, 0x48, 0x89, 0x3B, 0xAA, 0xB7, 0xE9, 0x03,
  29898. 0x89, 0x0D, 0xF0, 0xCA, 0xC2, 0x84, 0xCD, 0x11,
  29899. 0x65, 0xB4, 0x64, 0xC1, 0x8F, 0x4C, 0xFA, 0x8F,
  29900. 0x69, 0x2D, 0x3C, 0x8E, 0x8C, 0x73, 0x9F, 0xB3,
  29901. 0x78, 0x27, 0x74, 0x15, 0x61, 0xD5, 0xE7, 0x66,
  29902. 0x67, 0x51, 0x82, 0xFF, 0x83, 0xC9, 0x1C, 0xF0,
  29903. 0x33, 0x65, 0x88, 0x23, 0xD8, 0xFA, 0x91, 0xE1
  29904. };
  29905. const byte kyber1024_sk[] = {
  29906. 0x1B, 0xD1, 0x97, 0x95, 0x10, 0xBC, 0xE5, 0x03,
  29907. 0x1C, 0x0F, 0x99, 0x1D, 0xE0, 0x82, 0xC3, 0x89,
  29908. 0xA1, 0xBD, 0x15, 0x57, 0x19, 0x1D, 0x81, 0x03,
  29909. 0xBD, 0xCC, 0x04, 0xC1, 0x98, 0xAE, 0x3E, 0x95,
  29910. 0x95, 0xFD, 0xB6, 0x57, 0xB5, 0xC6, 0x39, 0x97,
  29911. 0x95, 0x79, 0xAE, 0x13, 0x5B, 0x11, 0xE2, 0x4D,
  29912. 0x36, 0xE5, 0x49, 0x44, 0x67, 0x83, 0x2E, 0x6A,
  29913. 0x8A, 0x46, 0xAA, 0x8F, 0x9F, 0xF9, 0x87, 0x4A,
  29914. 0x16, 0x00, 0x27, 0x84, 0x07, 0x71, 0x56, 0x2D,
  29915. 0x74, 0x61, 0xB1, 0x6C, 0x61, 0x87, 0xF3, 0xC2,
  29916. 0xC1, 0x9C, 0x9C, 0x8F, 0xE6, 0x41, 0x49, 0x7D,
  29917. 0x8A, 0x15, 0x5C, 0x51, 0x15, 0x7D, 0x91, 0x35,
  29918. 0xD9, 0xB4, 0x5D, 0x3C, 0x7C, 0x4C, 0xB1, 0x82,
  29919. 0x31, 0x8C, 0x11, 0xAD, 0x08, 0xB3, 0x9F, 0xAC,
  29920. 0x04, 0x15, 0xE6, 0xE2, 0x6D, 0xB3, 0x43, 0x91,
  29921. 0xDE, 0xC2, 0x47, 0x1B, 0x30, 0x3C, 0x05, 0x05,
  29922. 0xB6, 0xFE, 0x45, 0x6E, 0x81, 0x50, 0x21, 0xE8,
  29923. 0x2B, 0x5F, 0x2D, 0x16, 0x6F, 0xDF, 0xB5, 0xB9,
  29924. 0x23, 0xC8, 0x3A, 0x61, 0x67, 0x30, 0x28, 0xF0,
  29925. 0xBA, 0xE0, 0x28, 0x08, 0x9F, 0x47, 0x61, 0x67,
  29926. 0xE5, 0x75, 0x6C, 0xAA, 0x3E, 0xE0, 0xF1, 0x89,
  29927. 0xC6, 0x47, 0x81, 0xC7, 0x49, 0x27, 0xBC, 0xDB,
  29928. 0x67, 0xE7, 0x75, 0xCC, 0xA8, 0x5B, 0x44, 0xB8,
  29929. 0x8B, 0x10, 0x61, 0xD8, 0x96, 0x3F, 0xE4, 0x0B,
  29930. 0x1F, 0xB4, 0x34, 0x68, 0x20, 0x09, 0x0E, 0x5C,
  29931. 0x0B, 0x7C, 0x49, 0x08, 0x89, 0x33, 0xAD, 0xE3,
  29932. 0x82, 0x96, 0x88, 0x6C, 0x43, 0x45, 0x65, 0x7C,
  29933. 0x3F, 0x07, 0xCF, 0x1A, 0xE9, 0x76, 0xE6, 0xBA,
  29934. 0x77, 0x7E, 0xF0, 0x1F, 0xB9, 0xC3, 0xB0, 0xDC,
  29935. 0xF0, 0x58, 0x3A, 0xE5, 0x44, 0xCD, 0x80, 0x3A,
  29936. 0xC1, 0x39, 0xCA, 0x61, 0x9B, 0x8D, 0xCA, 0x13,
  29937. 0x1C, 0xD2, 0x73, 0xCE, 0xCB, 0x65, 0xBC, 0xEF,
  29938. 0x00, 0x29, 0x95, 0x12, 0xB0, 0x21, 0xBA, 0x1B,
  29939. 0x2F, 0xF2, 0x08, 0xEE, 0xBB, 0x08, 0xA6, 0x28,
  29940. 0x7E, 0xC4, 0x4C, 0x21, 0x7D, 0x94, 0x76, 0x40,
  29941. 0x42, 0x5C, 0x0E, 0x97, 0x13, 0x44, 0x5B, 0xC5,
  29942. 0x2B, 0xC3, 0xCD, 0x66, 0x01, 0x2C, 0x8C, 0xB7,
  29943. 0x10, 0xA2, 0xE8, 0xAD, 0x5F, 0xC5, 0x1C, 0x5B,
  29944. 0x71, 0xCE, 0x69, 0xE9, 0x6A, 0x22, 0x06, 0x6F,
  29945. 0x74, 0x4B, 0xBA, 0xB6, 0x47, 0x2F, 0xEF, 0x3A,
  29946. 0x5B, 0xCF, 0xD6, 0x1D, 0xF1, 0xF8, 0x6A, 0x95,
  29947. 0x64, 0x80, 0x03, 0x1A, 0x99, 0x0E, 0xE0, 0x7D,
  29948. 0xE5, 0x04, 0x5E, 0xBC, 0x49, 0x8F, 0x2D, 0x3A,
  29949. 0xBD, 0xE3, 0x28, 0xC4, 0x2A, 0xCB, 0x21, 0xAD,
  29950. 0x62, 0x5D, 0x7B, 0xCB, 0x85, 0x5A, 0x8A, 0x0A,
  29951. 0xCB, 0xA2, 0x85, 0x16, 0xD3, 0x5A, 0xAF, 0x92,
  29952. 0xBE, 0xB3, 0x9C, 0x46, 0xD8, 0xF4, 0xA0, 0x1F,
  29953. 0x73, 0x20, 0x40, 0xF2, 0x7C, 0x40, 0x9B, 0xA4,
  29954. 0x9A, 0x80, 0xCF, 0x95, 0x55, 0x7C, 0x73, 0x7C,
  29955. 0x4A, 0x45, 0x11, 0x2C, 0x52, 0x2C, 0x78, 0x04,
  29956. 0xC7, 0x7A, 0x3D, 0x44, 0x53, 0xBA, 0x82, 0xC1,
  29957. 0x49, 0x08, 0xBC, 0xC0, 0x47, 0x13, 0x7E, 0x5A,
  29958. 0x7B, 0x23, 0x92, 0xC8, 0x31, 0x25, 0x4C, 0x1A,
  29959. 0x59, 0x54, 0xC8, 0x8C, 0xCF, 0xA5, 0xD8, 0x8B,
  29960. 0x3B, 0x93, 0x31, 0xBE, 0xF4, 0x62, 0x55, 0x09,
  29961. 0x14, 0xD1, 0x84, 0x17, 0x59, 0x40, 0xB4, 0xE9,
  29962. 0xB7, 0xAD, 0x3D, 0x44, 0x2E, 0xA9, 0x68, 0xB6,
  29963. 0xA4, 0x0A, 0x19, 0x72, 0x71, 0x5B, 0xCB, 0x72,
  29964. 0x82, 0x14, 0xA1, 0xBE, 0x8E, 0x04, 0x1D, 0xF5,
  29965. 0x72, 0xCC, 0x2D, 0x01, 0x6F, 0xFD, 0x6B, 0x0A,
  29966. 0xFA, 0xC8, 0x02, 0xBA, 0x97, 0x5A, 0x4A, 0x31,
  29967. 0xA4, 0x18, 0x40, 0x2E, 0x6B, 0x01, 0x41, 0xFA,
  29968. 0xD6, 0x48, 0x49, 0xE1, 0x6C, 0x80, 0x37, 0x23,
  29969. 0xA7, 0x74, 0x2B, 0x08, 0x2A, 0x7A, 0x56, 0x26,
  29970. 0x4E, 0x02, 0x60, 0xA2, 0x4A, 0x66, 0x42, 0x3D,
  29971. 0x52, 0x64, 0x82, 0x07, 0x04, 0x06, 0x5C, 0xAC,
  29972. 0x41, 0x10, 0x72, 0x48, 0x64, 0x37, 0xCA, 0xA5,
  29973. 0x47, 0x2D, 0x73, 0x3C, 0x64, 0x14, 0x5F, 0x10,
  29974. 0x0B, 0x0B, 0x16, 0xC0, 0xAB, 0x0D, 0xB8, 0x7C,
  29975. 0xC5, 0x64, 0x26, 0xC1, 0x1A, 0x69, 0x45, 0x25,
  29976. 0x95, 0x8D, 0xB8, 0x31, 0x59, 0x31, 0xC5, 0x09,
  29977. 0xD6, 0x3C, 0x48, 0x10, 0xBB, 0xA7, 0x68, 0xCA,
  29978. 0xCB, 0xF9, 0xCD, 0x85, 0x7B, 0x0D, 0x66, 0xD6,
  29979. 0x5A, 0xF7, 0xEB, 0xAC, 0x18, 0xB7, 0x9C, 0xBE,
  29980. 0xBC, 0x0A, 0x49, 0xB5, 0xC0, 0x5A, 0xA8, 0x7F,
  29981. 0x8D, 0xE8, 0x25, 0xA6, 0xA1, 0x13, 0x5F, 0xE8,
  29982. 0x64, 0xCF, 0x6C, 0x99, 0x8D, 0xDC, 0x37, 0xFC,
  29983. 0x01, 0x56, 0xBB, 0x52, 0xA7, 0x8E, 0xC2, 0x99,
  29984. 0xCA, 0x53, 0x1C, 0xF8, 0x75, 0xBE, 0xFA, 0x48,
  29985. 0x75, 0x72, 0x60, 0x28, 0xED, 0x37, 0x91, 0xBA,
  29986. 0x31, 0xC0, 0x0E, 0xC7, 0x40, 0x20, 0xCA, 0xA1,
  29987. 0x7F, 0x91, 0x19, 0x63, 0x96, 0xCF, 0xE5, 0x88,
  29988. 0x6E, 0x51, 0x1A, 0x52, 0xD0, 0xCA, 0x8D, 0xEE,
  29989. 0x25, 0xBF, 0x4E, 0x77, 0xCB, 0x92, 0xE6, 0x36,
  29990. 0x40, 0x64, 0x6D, 0xE2, 0x58, 0x6D, 0xCD, 0x00,
  29991. 0x14, 0x16, 0xCC, 0xC1, 0xEC, 0x30, 0x7C, 0xA2,
  29992. 0x75, 0x6D, 0xBE, 0xA8, 0x26, 0x36, 0x14, 0x51,
  29993. 0x3C, 0xA0, 0x5E, 0x70, 0xC7, 0x54, 0xF5, 0x6A,
  29994. 0x2C, 0x3F, 0xF7, 0x42, 0x94, 0x8C, 0x34, 0xAF,
  29995. 0x49, 0x34, 0x2F, 0x55, 0x03, 0x4C, 0x1C, 0x70,
  29996. 0xE0, 0x79, 0x6D, 0x26, 0xB2, 0x30, 0x35, 0x6B,
  29997. 0x1B, 0x74, 0xA1, 0x6C, 0x0B, 0x28, 0x37, 0x4B,
  29998. 0x79, 0xC1, 0x6B, 0x04, 0x96, 0xCA, 0x20, 0x12,
  29999. 0x3E, 0x6C, 0x67, 0x62, 0xD9, 0x01, 0xE2, 0x76,
  30000. 0x08, 0x5D, 0x81, 0x2A, 0xE9, 0xC3, 0x45, 0xAD,
  30001. 0xF5, 0x09, 0xDD, 0xC7, 0x87, 0x61, 0x62, 0xCE,
  30002. 0x34, 0xA6, 0x00, 0x3F, 0xB4, 0x5C, 0xD8, 0x66,
  30003. 0x0F, 0xDD, 0x46, 0x71, 0x6E, 0x82, 0x55, 0x06,
  30004. 0x62, 0x37, 0x5D, 0xB5, 0x5E, 0x63, 0xA2, 0x0F,
  30005. 0x01, 0x2A, 0x71, 0xE3, 0xD9, 0x72, 0x25, 0xC5,
  30006. 0x8F, 0x52, 0x34, 0xC6, 0xB5, 0x3A, 0x8A, 0xA0,
  30007. 0x33, 0x9C, 0xEF, 0xD4, 0x84, 0xE4, 0x7A, 0x3D,
  30008. 0xE6, 0x49, 0x88, 0x05, 0xC2, 0x82, 0xE0, 0x76,
  30009. 0x8E, 0x35, 0xC7, 0x5C, 0x20, 0x07, 0x76, 0x5C,
  30010. 0xA1, 0x7F, 0x01, 0xD2, 0x85, 0xA7, 0xC3, 0x04,
  30011. 0x8B, 0xC7, 0x2B, 0xA3, 0xAA, 0x57, 0x7C, 0x1C,
  30012. 0x43, 0x2A, 0x3C, 0x76, 0x9D, 0x21, 0xB8, 0x6C,
  30013. 0x01, 0x8F, 0x81, 0x7A, 0x26, 0x67, 0x35, 0x1C,
  30014. 0x2B, 0xC8, 0x5F, 0xA5, 0x53, 0x6F, 0xA7, 0xA6,
  30015. 0xCD, 0xEF, 0xCC, 0x0D, 0xD6, 0xF9, 0x1B, 0xDD,
  30016. 0xE8, 0x9D, 0x4E, 0x51, 0x6C, 0x8B, 0x75, 0x40,
  30017. 0xA7, 0xDC, 0x3F, 0xC4, 0x89, 0x8A, 0xB3, 0x30,
  30018. 0x08, 0xBC, 0xC1, 0x9F, 0x9E, 0x46, 0x3D, 0xE9,
  30019. 0x36, 0x26, 0x99, 0x4C, 0xC9, 0x27, 0xA3, 0x4A,
  30020. 0x9D, 0x70, 0x40, 0xC1, 0x31, 0xC8, 0x1F, 0x07,
  30021. 0x83, 0xDD, 0x68, 0x84, 0x29, 0x43, 0xC5, 0x5D,
  30022. 0xC5, 0x77, 0xD3, 0x67, 0xA4, 0x01, 0x83, 0x94,
  30023. 0xBB, 0x11, 0x60, 0x72, 0xAC, 0x65, 0xF2, 0xEB,
  30024. 0x9B, 0x4F, 0xF5, 0x8F, 0x41, 0x15, 0x40, 0x1B,
  30025. 0x11, 0x7D, 0x91, 0x3B, 0x75, 0x75, 0x57, 0x57,
  30026. 0x19, 0x39, 0x0C, 0x9E, 0x80, 0x06, 0x97, 0xA8,
  30027. 0x28, 0xB1, 0x25, 0x83, 0xF9, 0x4A, 0x2F, 0x8F,
  30028. 0x72, 0x0A, 0x6E, 0x87, 0x00, 0xFD, 0x40, 0x7D,
  30029. 0x73, 0xA4, 0xC7, 0x97, 0x55, 0x2B, 0x98, 0x75,
  30030. 0x7A, 0x5F, 0x57, 0x82, 0xA9, 0x50, 0x95, 0xDF,
  30031. 0xA5, 0xAC, 0xB7, 0xD9, 0x3A, 0x80, 0x5C, 0x30,
  30032. 0x18, 0x30, 0x4E, 0x5D, 0x22, 0xB7, 0x6F, 0xA5,
  30033. 0xAA, 0x51, 0x96, 0xB7, 0xEB, 0xDB, 0x7A, 0xA0,
  30034. 0xA5, 0x2D, 0x06, 0xAC, 0x4B, 0x9E, 0x4C, 0x00,
  30035. 0xC0, 0xF8, 0x5C, 0xEC, 0xB0, 0xBD, 0x95, 0x56,
  30036. 0x2D, 0x56, 0x23, 0x30, 0x78, 0x51, 0x43, 0xC7,
  30037. 0xF1, 0x29, 0xFA, 0xF9, 0x19, 0x48, 0xB0, 0xC8,
  30038. 0x8C, 0x60, 0x82, 0xE0, 0xF3, 0x03, 0x54, 0x74,
  30039. 0x12, 0x6E, 0x23, 0x6B, 0x80, 0xD2, 0x22, 0xD2,
  30040. 0x81, 0x4F, 0x6A, 0xF2, 0x4B, 0xD5, 0xD1, 0x19,
  30041. 0x86, 0x33, 0x1F, 0xAF, 0xC2, 0x1E, 0xC8, 0x4C,
  30042. 0x93, 0xA8, 0x81, 0x24, 0x72, 0x51, 0x10, 0x5A,
  30043. 0x6A, 0xB4, 0x35, 0xD7, 0x6E, 0xFD, 0x88, 0x9D,
  30044. 0x27, 0xC0, 0x0C, 0x2A, 0x14, 0xBD, 0x04, 0xA6,
  30045. 0xBD, 0x0B, 0x20, 0x07, 0xB5, 0x51, 0x5A, 0x75,
  30046. 0xF1, 0x14, 0xD1, 0x92, 0x10, 0xBC, 0x70, 0x50,
  30047. 0x85, 0x33, 0x53, 0x2F, 0x1B, 0x0A, 0x30, 0x0C,
  30048. 0x8B, 0x12, 0xF0, 0x0A, 0xC4, 0xFC, 0x46, 0x0F,
  30049. 0xA8, 0x4A, 0x2D, 0x15, 0x21, 0xBD, 0x0B, 0x42,
  30050. 0x88, 0xD9, 0x2D, 0xD1, 0x2B, 0x17, 0x3E, 0x28,
  30051. 0x8B, 0x8A, 0x30, 0xC4, 0x9C, 0xA3, 0x56, 0x21,
  30052. 0x3C, 0x22, 0x4A, 0x03, 0x64, 0xDC, 0xF1, 0x8F,
  30053. 0xF0, 0x70, 0x0E, 0xAE, 0xBB, 0xB6, 0xD2, 0x02,
  30054. 0x03, 0x71, 0x14, 0x5A, 0x8A, 0xE8, 0x79, 0x59,
  30055. 0x72, 0x2D, 0x34, 0xF3, 0x3F, 0xAE, 0x00, 0x57,
  30056. 0x2D, 0xB3, 0xAC, 0x9A, 0x7B, 0xBD, 0xD4, 0x0B,
  30057. 0x75, 0x3C, 0x57, 0xA7, 0x3B, 0xA1, 0xB5, 0x43,
  30058. 0x12, 0x8A, 0x20, 0xDC, 0x98, 0x31, 0x69, 0x39,
  30059. 0xFE, 0xB8, 0x14, 0xCB, 0x17, 0x6C, 0x41, 0x64,
  30060. 0x77, 0x01, 0x2A, 0x97, 0x23, 0x67, 0x83, 0x46,
  30061. 0x7C, 0x0E, 0x15, 0x97, 0x33, 0xC5, 0xA0, 0x31,
  30062. 0x3C, 0xB2, 0x78, 0xD2, 0x31, 0x41, 0x2C, 0xB0,
  30063. 0x42, 0xC8, 0xE1, 0x16, 0xD6, 0x65, 0xA3, 0x7E,
  30064. 0xA4, 0x86, 0xEE, 0x07, 0x75, 0x44, 0xFC, 0x09,
  30065. 0xD5, 0x71, 0x56, 0x46, 0x3C, 0x1B, 0x10, 0xDC,
  30066. 0x49, 0x41, 0x0A, 0x3C, 0x0F, 0xE4, 0xB2, 0xEF,
  30067. 0x57, 0x1F, 0xAA, 0x56, 0x7A, 0xB5, 0x01, 0x59,
  30068. 0x58, 0xDB, 0xCF, 0x2C, 0xFA, 0x2C, 0xA7, 0x33,
  30069. 0x96, 0x3C, 0x34, 0x45, 0x7F, 0x85, 0x99, 0xCA,
  30070. 0x3A, 0xC8, 0xD2, 0xCB, 0x99, 0xA4, 0x57, 0x11,
  30071. 0x4E, 0x55, 0x9A, 0x9F, 0x82, 0xC4, 0x28, 0x47,
  30072. 0x99, 0xDE, 0x66, 0x15, 0x0A, 0x26, 0x56, 0x8F,
  30073. 0x95, 0xAF, 0x70, 0x65, 0x78, 0x8E, 0x62, 0x86,
  30074. 0x84, 0x60, 0x8C, 0x74, 0xDA, 0x57, 0x17, 0xB8,
  30075. 0x1F, 0x5D, 0x5C, 0x2F, 0x7D, 0x1A, 0x6C, 0xC0,
  30076. 0x71, 0x7C, 0xAB, 0xD2, 0xBA, 0xD2, 0x49, 0xB0,
  30077. 0x87, 0x58, 0x8F, 0x75, 0x65, 0x23, 0xFD, 0xF0,
  30078. 0x4E, 0x7F, 0x31, 0x76, 0x1B, 0x01, 0xAC, 0xB7,
  30079. 0xF6, 0x7E, 0xBF, 0x87, 0x62, 0xF5, 0xCB, 0x0B,
  30080. 0xC3, 0x72, 0xB4, 0xDD, 0x08, 0x3B, 0x06, 0x66,
  30081. 0x74, 0x02, 0x20, 0x4D, 0x08, 0x73, 0x9E, 0xB7,
  30082. 0x95, 0x20, 0xA9, 0x2C, 0x87, 0xC7, 0xE2, 0x2C,
  30083. 0x78, 0x77, 0x97, 0x3A, 0x0B, 0x79, 0xB0, 0xC7,
  30084. 0xAC, 0xCB, 0xBB, 0xAF, 0x88, 0xFB, 0xB0, 0x3D,
  30085. 0x4B, 0x6F, 0x5B, 0xB9, 0x87, 0x8A, 0xAB, 0x81,
  30086. 0x02, 0xF6, 0xB9, 0x1A, 0x27, 0x00, 0xD0, 0xEA,
  30087. 0xC3, 0x1D, 0x26, 0x03, 0x5B, 0xC1, 0x8A, 0x66,
  30088. 0xCB, 0x90, 0xEA, 0x24, 0x3F, 0xCF, 0x1A, 0x9A,
  30089. 0x1D, 0x24, 0x0C, 0x29, 0x0A, 0x54, 0x82, 0xA1,
  30090. 0x55, 0xC8, 0x00, 0x91, 0x13, 0xA0, 0x82, 0xDA,
  30091. 0xD5, 0xCE, 0x5C, 0xD2, 0x53, 0x03, 0x37, 0x02,
  30092. 0x51, 0x59, 0x3C, 0x12, 0x11, 0xAA, 0xB6, 0xC8,
  30093. 0x44, 0xC2, 0xC4, 0xB7, 0x99, 0xD4, 0x92, 0x80,
  30094. 0x00, 0x74, 0x63, 0x89, 0x48, 0xCB, 0x64, 0x31,
  30095. 0x0F, 0x38, 0xB4, 0x88, 0x51, 0xA6, 0x9E, 0x22,
  30096. 0x28, 0x70, 0x3C, 0x97, 0xFC, 0x20, 0x4E, 0x54,
  30097. 0x37, 0x21, 0x79, 0xAA, 0x84, 0xDE, 0xC1, 0xAC,
  30098. 0x68, 0xE6, 0xBC, 0x26, 0x8B, 0x9E, 0x36, 0x1B,
  30099. 0x8F, 0x6E, 0x0A, 0xBC, 0xFE, 0x88, 0x93, 0x37,
  30100. 0x87, 0x53, 0x48, 0xE4, 0x16, 0x4D, 0x66, 0x15,
  30101. 0xF3, 0x82, 0xC6, 0xC7, 0xC4, 0x17, 0x8F, 0x75,
  30102. 0x1F, 0x72, 0x2B, 0x21, 0x27, 0x74, 0x64, 0xFB,
  30103. 0x83, 0x1F, 0x8A, 0xA9, 0xA0, 0x18, 0xC1, 0x9A,
  30104. 0x51, 0xB0, 0x7F, 0xA0, 0x93, 0x9B, 0xA5, 0xC2,
  30105. 0x06, 0x68, 0x63, 0x23, 0xAF, 0xF6, 0xCA, 0x83,
  30106. 0x0B, 0x76, 0xE3, 0x80, 0x02, 0x63, 0x54, 0x47,
  30107. 0x7A, 0xAC, 0x58, 0xD8, 0x93, 0x1D, 0x80, 0x3B,
  30108. 0x6F, 0xAB, 0xD7, 0x12, 0xD1, 0x72, 0x81, 0x4A,
  30109. 0x54, 0xB8, 0x12, 0xE7, 0x92, 0x47, 0x94, 0xAA,
  30110. 0x40, 0x02, 0x49, 0xD5, 0xC0, 0x46, 0x8E, 0xA3,
  30111. 0x2F, 0xB2, 0x15, 0x54, 0xC5, 0x93, 0x67, 0x90,
  30112. 0x3C, 0x60, 0xA8, 0xB4, 0x8E, 0x23, 0x40, 0x71,
  30113. 0x2F, 0x7A, 0x04, 0x5A, 0x67, 0xC2, 0x3C, 0x72,
  30114. 0x41, 0x3A, 0x64, 0x69, 0xFC, 0x83, 0x69, 0xA2,
  30115. 0x91, 0x03, 0x3C, 0x46, 0xCE, 0xA3, 0x96, 0x76,
  30116. 0x65, 0x6B, 0x6C, 0xD8, 0x57, 0x99, 0x7C, 0x04,
  30117. 0x5C, 0x5A, 0xF0, 0x34, 0xE1, 0xE1, 0x14, 0x8A,
  30118. 0x8B, 0x40, 0xCD, 0x16, 0x68, 0x31, 0x93, 0xA3,
  30119. 0x60, 0x0C, 0x84, 0xFC, 0xE3, 0xB9, 0x65, 0x10,
  30120. 0x88, 0xDD, 0x09, 0x44, 0x57, 0x6C, 0x7E, 0xA4,
  30121. 0x07, 0x2D, 0x25, 0xF0, 0x67, 0xD2, 0x42, 0x7E,
  30122. 0x26, 0xD1, 0x86, 0xF9, 0xB7, 0x7A, 0x32, 0x79,
  30123. 0x05, 0x03, 0x3B, 0xA9, 0x40, 0x85, 0xA8, 0xAB,
  30124. 0x6A, 0x8A, 0x2B, 0x86, 0x08, 0x0C, 0x45, 0x2B,
  30125. 0x10, 0x90, 0xAC, 0xC9, 0x5A, 0x2B, 0x9F, 0x5A,
  30126. 0x6E, 0x61, 0x84, 0x6D, 0x63, 0x45, 0x66, 0xFA,
  30127. 0xC0, 0xB0, 0xFF, 0x35, 0x11, 0x18, 0x06, 0x18,
  30128. 0xAF, 0x98, 0x34, 0x87, 0x12, 0x2C, 0xFB, 0x63,
  30129. 0x61, 0xFF, 0x66, 0x0D, 0x5C, 0xA0, 0x0B, 0x9D,
  30130. 0x88, 0xB1, 0xDA, 0x18, 0x36, 0xC9, 0x78, 0x6E,
  30131. 0x55, 0x27, 0x6C, 0x88, 0x53, 0x8C, 0xC1, 0xD3,
  30132. 0x05, 0xFA, 0xAA, 0xB7, 0x79, 0x9A, 0x35, 0x46,
  30133. 0xF0, 0xBE, 0x6C, 0xC7, 0x00, 0xB0, 0x65, 0xB9,
  30134. 0x03, 0x4A, 0x91, 0x63, 0x31, 0x74, 0x66, 0xD9,
  30135. 0x83, 0xDD, 0xC2, 0xAA, 0x5B, 0xB0, 0x3F, 0x5B,
  30136. 0x67, 0x33, 0x48, 0xD5, 0x76, 0x02, 0x8A, 0x02,
  30137. 0x55, 0x15, 0x6C, 0x6A, 0x29, 0x74, 0x46, 0x98,
  30138. 0x34, 0xFB, 0x15, 0x73, 0x23, 0x74, 0x60, 0x54,
  30139. 0xC4, 0xBA, 0x96, 0x40, 0x30, 0x4A, 0x11, 0x63,
  30140. 0x04, 0xF0, 0x18, 0x21, 0x72, 0xBA, 0x8D, 0x03,
  30141. 0xAE, 0xDA, 0x39, 0x91, 0x6A, 0xF1, 0x18, 0xB9,
  30142. 0x84, 0x22, 0xB7, 0x16, 0x3A, 0xB6, 0xE2, 0x47,
  30143. 0xFC, 0xAA, 0x9D, 0x7C, 0x7C, 0xAE, 0x96, 0xBB,
  30144. 0x60, 0xB1, 0xD7, 0x86, 0x0E, 0x4C, 0x7F, 0x1D,
  30145. 0x92, 0x0D, 0x31, 0xA5, 0x41, 0x38, 0x82, 0x29,
  30146. 0xEE, 0x84, 0x2C, 0xA1, 0x59, 0x43, 0x89, 0x4A,
  30147. 0x56, 0xE5, 0x14, 0x0D, 0x29, 0x59, 0xC3, 0x6E,
  30148. 0xA2, 0x6E, 0x2D, 0xE0, 0x51, 0x21, 0x36, 0x6B,
  30149. 0xE4, 0x14, 0x02, 0x7B, 0xD1, 0x47, 0x81, 0x47,
  30150. 0x2E, 0xDD, 0xA4, 0x71, 0x10, 0xD0, 0xB3, 0xAE,
  30151. 0x2A, 0xA2, 0x55, 0xAB, 0x6D, 0x6F, 0xE0, 0x1C,
  30152. 0x26, 0xC2, 0x87, 0x63, 0x97, 0x6B, 0xBF, 0xA9,
  30153. 0x2F, 0x4F, 0x09, 0xA5, 0xB7, 0x3C, 0x47, 0x1F,
  30154. 0x8C, 0x9E, 0x53, 0x26, 0x92, 0x55, 0xDA, 0xC1,
  30155. 0xE0, 0x71, 0x5E, 0x87, 0x00, 0x06, 0x09, 0x70,
  30156. 0x60, 0xFF, 0x23, 0xC3, 0xAB, 0x43, 0xC4, 0x19,
  30157. 0xE4, 0x48, 0xC7, 0x30, 0x40, 0x10, 0xC4, 0x0F,
  30158. 0x3D, 0xC0, 0xB7, 0x26, 0x39, 0x2E, 0x46, 0x03,
  30159. 0x7E, 0xB9, 0x49, 0x04, 0x55, 0xE3, 0x49, 0xA4,
  30160. 0x35, 0xCD, 0xA9, 0x0A, 0xA3, 0xB7, 0x47, 0x8C,
  30161. 0x32, 0xE8, 0x72, 0xB2, 0xB8, 0x5C, 0xEC, 0xF0,
  30162. 0x3D, 0xDC, 0xD6, 0x16, 0x33, 0x78, 0xBF, 0x73,
  30163. 0xB1, 0x3B, 0x2E, 0x1A, 0x7B, 0x6E, 0xBC, 0x39,
  30164. 0x29, 0xEC, 0x08, 0x63, 0x89, 0x3F, 0x73, 0x8C,
  30165. 0x38, 0x9F, 0x3B, 0x3B, 0x2B, 0xB5, 0x4F, 0xC1,
  30166. 0x5B, 0xB7, 0x8A, 0x25, 0x31, 0xEE, 0xBC, 0xB7,
  30167. 0xBC, 0x10, 0x81, 0x07, 0x96, 0x59, 0x81, 0xD2,
  30168. 0x0A, 0x7D, 0x1A, 0x87, 0x21, 0xBA, 0x36, 0x40,
  30169. 0x03, 0x13, 0x2F, 0x03, 0x6C, 0xE4, 0x0C, 0x9E,
  30170. 0x75, 0x5B, 0x2A, 0xB4, 0x86, 0x1D, 0x04, 0xC1,
  30171. 0xBE, 0x0D, 0x53, 0x97, 0x91, 0x4B, 0x6C, 0xD4,
  30172. 0xAA, 0x40, 0x8C, 0x62, 0x2B, 0x21, 0xB1, 0x1B,
  30173. 0xA0, 0xEB, 0xA6, 0x17, 0x29, 0x59, 0x6F, 0x8B,
  30174. 0x0E, 0x82, 0x14, 0xAA, 0x4A, 0xA6, 0x01, 0x86,
  30175. 0x51, 0x07, 0xA2, 0xA7, 0x6A, 0xD7, 0xCC, 0x2F,
  30176. 0x97, 0x43, 0x00, 0x18, 0x28, 0xB8, 0x0B, 0x24,
  30177. 0x80, 0x2B, 0x98, 0x9D, 0x98, 0x42, 0x5E, 0x14,
  30178. 0x7C, 0x6D, 0x93, 0x32, 0xBA, 0x08, 0x4B, 0x8F,
  30179. 0xFB, 0x96, 0x27, 0xE3, 0xFB, 0xC4, 0xBE, 0x65,
  30180. 0x38, 0xEB, 0xDA, 0x1D, 0x9D, 0xB9, 0xB6, 0x75,
  30181. 0xB0, 0x16, 0x66, 0x57, 0x23, 0xD4, 0x11, 0x71,
  30182. 0x54, 0xF8, 0x13, 0x3E, 0xA9, 0x00, 0xCB, 0x47,
  30183. 0x0B, 0x1D, 0xC5, 0xB5, 0x7A, 0x78, 0x11, 0x16,
  30184. 0x72, 0x9E, 0xD6, 0x29, 0x45, 0x6E, 0x1A, 0x7D,
  30185. 0x6A, 0x37, 0x9F, 0x2E, 0x01, 0x0A, 0xCB, 0xEC,
  30186. 0x21, 0xFC, 0xD1, 0xC2, 0xB0, 0xBB, 0x61, 0xC3,
  30187. 0xDA, 0x6C, 0x73, 0x70, 0x5F, 0xD7, 0xB0, 0xC3,
  30188. 0x61, 0x2A, 0xC3, 0xB4, 0xA1, 0x2E, 0xC5, 0xD3,
  30189. 0x7F, 0x4C, 0x1A, 0x4A, 0x4D, 0xF8, 0x16, 0x6E,
  30190. 0xF6, 0x68, 0x9C, 0x06, 0xB5, 0xD4, 0xA6, 0x1A,
  30191. 0x52, 0x14, 0xAE, 0x5E, 0x5C, 0x28, 0x07, 0x4C,
  30192. 0xC5, 0xFE, 0x2A, 0x41, 0x2A, 0xC5, 0x14, 0x15,
  30193. 0x94, 0x28, 0x54, 0x85, 0x15, 0x6E, 0xCA, 0x65,
  30194. 0x64, 0x28, 0x37, 0x20, 0xB0, 0xBA, 0xD1, 0xD9,
  30195. 0x74, 0x5B, 0xA5, 0x77, 0x71, 0x1A, 0xB6, 0xF2,
  30196. 0x14, 0xC5, 0x2F, 0xB5, 0xBA, 0xAF, 0x8A, 0x1F,
  30197. 0x85, 0x33, 0x5A, 0xEB, 0x44, 0x37, 0x0B, 0x39,
  30198. 0xC5, 0x6C, 0x4C, 0x3D, 0x32, 0x75, 0x55, 0x60,
  30199. 0xCB, 0x2C, 0xA9, 0x45, 0x87, 0x51, 0xF1, 0x65,
  30200. 0x4B, 0x82, 0xC5, 0xE7, 0xFB, 0x0D, 0xB5, 0x16,
  30201. 0xBE, 0x1E, 0x42, 0x43, 0x3A, 0xD4, 0x7B, 0x6B,
  30202. 0xCC, 0xB3, 0x63, 0x61, 0xC4, 0x34, 0xEB, 0x59,
  30203. 0xCF, 0xF3, 0x38, 0x49, 0x54, 0xBB, 0x76, 0xF1,
  30204. 0x44, 0x14, 0x91, 0x67, 0x91, 0x3B, 0x85, 0x0F,
  30205. 0x75, 0x28, 0xBD, 0x76, 0x87, 0xB8, 0x8B, 0x64,
  30206. 0x84, 0xA6, 0x82, 0x68, 0x39, 0x97, 0x08, 0x42,
  30207. 0xA5, 0x93, 0xBB, 0x93, 0x45, 0x5A, 0x73, 0xBC,
  30208. 0x2C, 0x74, 0x62, 0x55, 0xC1, 0x3B, 0xC8, 0x52,
  30209. 0x1C, 0x73, 0x54, 0xE9, 0x8B, 0x98, 0x76, 0xA9,
  30210. 0x58, 0x6F, 0xC3, 0x7C, 0x5F, 0x24, 0x8D, 0xA0,
  30211. 0x55, 0x75, 0x8B, 0xD9, 0x61, 0x5E, 0x68, 0xBF,
  30212. 0x93, 0xEA, 0x0B, 0xA1, 0xD0, 0x22, 0xE2, 0xD2,
  30213. 0x22, 0x04, 0xA4, 0x45, 0x8C, 0xB6, 0x12, 0xB0,
  30214. 0x29, 0x41, 0xE8, 0x7A, 0xAC, 0xF1, 0xA3, 0x29,
  30215. 0x07, 0xD9, 0x8B, 0x4B, 0x13, 0xC6, 0xC9, 0xB4,
  30216. 0xAB, 0xD6, 0xDC, 0xCF, 0x66, 0x21, 0x06, 0xF5,
  30217. 0xD5, 0x8C, 0x2F, 0xC7, 0x51, 0x97, 0x35, 0x5E,
  30218. 0xB8, 0xCC, 0x41, 0x74, 0x2C, 0x6D, 0xA0, 0x03,
  30219. 0x40, 0x6D, 0x13, 0x56, 0xF8, 0xA0, 0x7A, 0x37,
  30220. 0x97, 0x88, 0x00, 0x60, 0x67, 0xCB, 0xF5, 0x2F,
  30221. 0x22, 0xD5, 0x3D, 0x68, 0x71, 0x22, 0x41, 0x09,
  30222. 0x34, 0x6E, 0xA8, 0x3B, 0x45, 0xCA, 0x60, 0xAF,
  30223. 0xEC, 0xCA, 0x0F, 0xA5, 0x67, 0x2B, 0x36, 0x64,
  30224. 0x89, 0xEB, 0x8D, 0xD1, 0xF7, 0x20, 0x4C, 0xDB,
  30225. 0x7D, 0x9B, 0xE1, 0x87, 0xF4, 0x39, 0x7B, 0x9B,
  30226. 0x4C, 0xA0, 0x31, 0x34, 0x75, 0x0D, 0xD5, 0x7D,
  30227. 0xAD, 0xE1, 0x88, 0x91, 0xCC, 0xA9, 0x47, 0x4B,
  30228. 0x65, 0x32, 0x60, 0x52, 0x50, 0xBC, 0xBB, 0x6E,
  30229. 0x02, 0x94, 0xEF, 0x95, 0x32, 0x6D, 0x25, 0x9B,
  30230. 0x08, 0xB2, 0x93, 0x8D, 0x8C, 0x59, 0xE2, 0x58,
  30231. 0x4B, 0x69, 0x10, 0x71, 0x85, 0xF3, 0x45, 0x30,
  30232. 0x24, 0x07, 0x35, 0x17, 0xC9, 0x89, 0x00, 0x9E,
  30233. 0xBB, 0x6B, 0x22, 0xED, 0xE9, 0x70, 0x43, 0x66,
  30234. 0xC6, 0x23, 0x2A, 0x62, 0x82, 0xF3, 0xBB, 0x09,
  30235. 0x08, 0x74, 0xA6, 0xFB, 0x0D, 0x84, 0x53, 0x6D,
  30236. 0x3F, 0x61, 0x3A, 0x21, 0xD1, 0x9D, 0x98, 0x44,
  30237. 0x17, 0xFA, 0xF0, 0x84, 0x6C, 0xB4, 0x28, 0x7B,
  30238. 0x3C, 0x70, 0x9F, 0x22, 0x62, 0x26, 0xA0, 0x9E,
  30239. 0x94, 0xFA, 0x89, 0x85, 0x43, 0x87, 0x4A, 0xE2,
  30240. 0x08, 0x11, 0xE3, 0x90, 0x80, 0xE6, 0xB2, 0xCE,
  30241. 0x49, 0x92, 0x26, 0xF5, 0x13, 0x8C, 0x14, 0x38,
  30242. 0x72, 0xDB, 0x86, 0xFB, 0xC2, 0x9C, 0xFD, 0xA2,
  30243. 0x9F, 0xD2, 0x26, 0x0E, 0xC7, 0xB9, 0xA2, 0x37,
  30244. 0xD1, 0x9A, 0xAB, 0x1B, 0x93, 0xE1, 0xDA, 0x4E,
  30245. 0xBC, 0x58, 0x9D, 0x82, 0x4C, 0x58, 0x90, 0x96,
  30246. 0x99, 0xD3, 0xE8, 0xBC, 0x7F, 0x53, 0xB3, 0x55,
  30247. 0xFA, 0x31, 0xE9, 0x6B, 0x1E, 0x35, 0x4A, 0xA4,
  30248. 0xB3, 0xC8, 0x4D, 0xF3, 0xC4, 0x7F, 0x80, 0xF9,
  30249. 0x32, 0xEA, 0x68, 0x5D, 0x96, 0xC5, 0xA5, 0x96,
  30250. 0x08, 0x22, 0xC6, 0x43, 0x64, 0x5F, 0x1A, 0x63,
  30251. 0xDD, 0x60, 0x8A, 0x09, 0x71, 0xC7, 0xF2, 0x15,
  30252. 0x7C, 0xA5, 0x24, 0x4D, 0x49, 0x2C, 0x01, 0x0B,
  30253. 0x71, 0xB7, 0x74, 0xC1, 0x0C, 0x07, 0xD3, 0xC8,
  30254. 0x4C, 0xA5, 0x4D, 0xE5, 0x00, 0x1F, 0xE0, 0xAC,
  30255. 0x4E, 0x69, 0x52, 0x81, 0x4E, 0xC1, 0x0F, 0xF9,
  30256. 0x8B, 0x19, 0xE7, 0x89, 0xC1, 0xAC, 0x68, 0x45,
  30257. 0xC7, 0x62, 0x80, 0x55, 0x27, 0x5C, 0x33, 0x34,
  30258. 0x41, 0x87, 0x1B, 0x8C, 0x30, 0x99, 0x9B, 0x05,
  30259. 0x31, 0x34, 0xC0, 0xE7, 0xB1, 0xAC, 0x6A, 0xA6,
  30260. 0x63, 0x12, 0xC1, 0xA5, 0xE2, 0x97, 0x92, 0x97,
  30261. 0x02, 0x56, 0x88, 0x14, 0x38, 0xDC, 0x46, 0x29,
  30262. 0x37, 0x4D, 0xFB, 0x9A, 0x91, 0x48, 0x62, 0x9D,
  30263. 0x14, 0x8C, 0x60, 0x1D, 0x4B, 0x6A, 0xA2, 0xC2,
  30264. 0x9C, 0xCE, 0xE8, 0x94, 0x7A, 0x27, 0x87, 0x0E,
  30265. 0x4C, 0x65, 0xE4, 0x12, 0x46, 0xD6, 0x25, 0x3D,
  30266. 0xC4, 0xF1, 0x3A, 0xEB, 0x31, 0xCD, 0x7E, 0x36,
  30267. 0x2F, 0x0F, 0x28, 0x84, 0xB9, 0xF7, 0x3B, 0xA9,
  30268. 0x93, 0x50, 0xAA, 0xA4, 0x02, 0x4A, 0x73, 0x14,
  30269. 0xBE, 0x69, 0x3A, 0xE4, 0x13, 0xA5, 0xCE, 0xA0,
  30270. 0x9A, 0x27, 0x27, 0x3A, 0x0B, 0xF6, 0xA4, 0x53,
  30271. 0xF8, 0x63, 0x9B, 0x48, 0x26, 0x8D, 0x21, 0xC9,
  30272. 0xAE, 0xC2, 0x62, 0xBE, 0x28, 0xC0, 0x7E, 0xA4,
  30273. 0xB9, 0x35, 0xA8, 0x69, 0x63, 0x26, 0xAA, 0x18,
  30274. 0xA5, 0x91, 0xCE, 0xE9, 0xC9, 0xF2, 0xA7, 0x29,
  30275. 0xBC, 0x50, 0x33, 0xDD, 0xE6, 0x37, 0x27, 0x38,
  30276. 0x85, 0x92, 0x1B, 0x73, 0xB8, 0x58, 0xA1, 0x88,
  30277. 0x33, 0xA7, 0xB8, 0x3A, 0x82, 0xFE, 0x02, 0xCC,
  30278. 0x33, 0x54, 0xC3, 0x0B, 0x0C, 0x4D, 0x0D, 0x3B,
  30279. 0xA1, 0x33, 0x1C, 0x6E, 0xC7, 0xA7, 0x0B, 0x7E,
  30280. 0x50, 0x3F, 0x76, 0x47, 0xA9, 0x52, 0xB0, 0x03,
  30281. 0x86, 0xCC, 0x90, 0xB3, 0x4A, 0x51, 0x4F, 0x99,
  30282. 0x5A, 0x56, 0x92, 0x05, 0xEB, 0x2A, 0x3F, 0xA7,
  30283. 0x9A, 0x78, 0x65, 0x21, 0x18, 0xD4, 0xBC, 0x48,
  30284. 0x46, 0x27, 0xCD, 0x02, 0x33, 0xBC, 0x5A, 0x35,
  30285. 0x16, 0x15, 0x0B, 0xAF, 0x6E, 0x10, 0x66, 0x5D,
  30286. 0xA7, 0x61, 0x8F, 0x28, 0x41, 0xB5, 0x02, 0x61,
  30287. 0x72, 0x48, 0x89, 0x3B, 0xAA, 0xB7, 0xE9, 0x03,
  30288. 0x89, 0x0D, 0xF0, 0xCA, 0xC2, 0x84, 0xCD, 0x11,
  30289. 0x65, 0xB4, 0x64, 0xC1, 0x8F, 0x4C, 0xFA, 0x8F,
  30290. 0x69, 0x2D, 0x3C, 0x8E, 0x8C, 0x73, 0x9F, 0xB3,
  30291. 0x78, 0x27, 0x74, 0x15, 0x61, 0xD5, 0xE7, 0x66,
  30292. 0x67, 0x51, 0x82, 0xFF, 0x83, 0xC9, 0x1C, 0xF0,
  30293. 0x33, 0x65, 0x88, 0x23, 0xD8, 0xFA, 0x91, 0xE1,
  30294. 0xE8, 0xD6, 0x62, 0xEA, 0xD3, 0x75, 0x0D, 0x71,
  30295. 0x6B, 0x91, 0x8D, 0x26, 0x78, 0x26, 0x59, 0xA5,
  30296. 0xB3, 0xD7, 0x99, 0xE4, 0x26, 0x58, 0xE1, 0x49,
  30297. 0x5F, 0x90, 0x84, 0xFF, 0xD4, 0xD1, 0x00, 0xB8,
  30298. 0x86, 0x26, 0xED, 0x79, 0xD4, 0x51, 0x14, 0x08,
  30299. 0x00, 0xE0, 0x3B, 0x59, 0xB9, 0x56, 0xF8, 0x21,
  30300. 0x0E, 0x55, 0x60, 0x67, 0x40, 0x7D, 0x13, 0xDC,
  30301. 0x90, 0xFA, 0x9E, 0x8B, 0x87, 0x2B, 0xFB, 0x8F
  30302. };
  30303. const byte kyber1024_ct[] = {
  30304. 0x17, 0x50, 0x59, 0x78, 0xD5, 0xCA, 0x04, 0x62,
  30305. 0x81, 0xC9, 0x7C, 0xA1, 0xFE, 0x89, 0xBA, 0xB3,
  30306. 0x99, 0xC1, 0xC9, 0xF8, 0xED, 0x27, 0x1D, 0xC5,
  30307. 0x1B, 0x30, 0x09, 0x43, 0xDB, 0x2E, 0xA9, 0x49,
  30308. 0x08, 0xEE, 0x94, 0x64, 0x61, 0x96, 0x02, 0xA7,
  30309. 0xF8, 0xCD, 0x38, 0xB5, 0xA3, 0x12, 0x5F, 0x46,
  30310. 0x7B, 0xF1, 0xA7, 0xA9, 0x67, 0xEE, 0xE0, 0x84,
  30311. 0x20, 0x98, 0x80, 0x14, 0x33, 0x88, 0x38, 0x92,
  30312. 0xAD, 0x0A, 0xD2, 0x99, 0x6F, 0x76, 0xCE, 0x49,
  30313. 0xA1, 0x79, 0x0A, 0x7A, 0x23, 0x0E, 0xB0, 0xDA,
  30314. 0x07, 0x72, 0xC6, 0xFC, 0x06, 0x0E, 0x75, 0x51,
  30315. 0x8C, 0x94, 0xA1, 0xA6, 0x52, 0xD8, 0x66, 0x0F,
  30316. 0xBA, 0x70, 0x1E, 0xF4, 0x82, 0xF4, 0xFA, 0x26,
  30317. 0xA7, 0xC4, 0xDB, 0x86, 0x02, 0xB7, 0x89, 0x49,
  30318. 0xB2, 0x8D, 0x99, 0xC7, 0x3B, 0x84, 0xD5, 0x5C,
  30319. 0xF6, 0x80, 0xA5, 0x90, 0x89, 0x3C, 0x63, 0xEA,
  30320. 0x8F, 0x85, 0x45, 0x8B, 0x85, 0xF7, 0x39, 0x2C,
  30321. 0x11, 0xB8, 0x90, 0x02, 0x02, 0x73, 0xB7, 0x48,
  30322. 0x33, 0xA8, 0x0C, 0xD6, 0xC7, 0x8C, 0x75, 0x51,
  30323. 0xD7, 0xCC, 0x8A, 0xA1, 0xB4, 0xC5, 0x42, 0x8D,
  30324. 0x2A, 0xA3, 0xE7, 0x43, 0x5E, 0x23, 0xDD, 0x9B,
  30325. 0x4F, 0xFA, 0xB0, 0xD0, 0x21, 0x90, 0xBB, 0xA1,
  30326. 0x36, 0x89, 0xF0, 0x15, 0xC1, 0x9A, 0x3A, 0xD9,
  30327. 0xD6, 0x62, 0x69, 0x77, 0xD1, 0xA3, 0x35, 0x58,
  30328. 0xAB, 0x6C, 0xF4, 0x6F, 0x76, 0xBD, 0x5A, 0x30,
  30329. 0xED, 0x94, 0x6D, 0x04, 0x03, 0x38, 0x8B, 0x13,
  30330. 0xA5, 0xB4, 0x95, 0x83, 0xCF, 0xB9, 0x3D, 0xEB,
  30331. 0x30, 0xC4, 0x63, 0x82, 0xAF, 0xC1, 0x28, 0x62,
  30332. 0xF0, 0x69, 0xF1, 0x36, 0xD5, 0xCF, 0x5F, 0x30,
  30333. 0x06, 0x4D, 0x90, 0xDE, 0x12, 0xF7, 0xD7, 0x6E,
  30334. 0xD7, 0xF4, 0xA5, 0x93, 0x13, 0x9C, 0xB6, 0x77,
  30335. 0x93, 0xF2, 0xAD, 0x94, 0xDB, 0xE1, 0xBA, 0xD2,
  30336. 0x3B, 0xC1, 0x7C, 0x72, 0x66, 0x9C, 0x25, 0x14,
  30337. 0x9A, 0xC8, 0xA2, 0x5C, 0x50, 0xE0, 0xE0, 0x8F,
  30338. 0x5D, 0x76, 0x4B, 0x30, 0xF2, 0x87, 0x07, 0x47,
  30339. 0x0C, 0x5E, 0xC0, 0x9E, 0xDD, 0x21, 0x4C, 0x8A,
  30340. 0x0A, 0x82, 0x8C, 0x2C, 0x8B, 0x6F, 0xCF, 0x74,
  30341. 0xE9, 0x47, 0x36, 0x62, 0xFC, 0x61, 0xC7, 0x28,
  30342. 0x1E, 0x9B, 0xA7, 0x23, 0x04, 0x6A, 0x0A, 0xE7,
  30343. 0x0D, 0x7F, 0xEB, 0x86, 0xFC, 0xFF, 0x4D, 0x87,
  30344. 0x6B, 0x10, 0xEF, 0x2B, 0x67, 0x73, 0xE2, 0x6C,
  30345. 0xB2, 0x52, 0xDF, 0x75, 0x73, 0x07, 0x68, 0x0E,
  30346. 0xCC, 0x47, 0xD8, 0xEC, 0x07, 0xB6, 0xA5, 0x09,
  30347. 0xE2, 0x74, 0x51, 0x31, 0x77, 0xC6, 0xEE, 0x1B,
  30348. 0x91, 0x7B, 0x0C, 0x3E, 0x77, 0xCC, 0xF6, 0x2F,
  30349. 0x41, 0x2A, 0x95, 0x69, 0x57, 0x3B, 0xEB, 0xA1,
  30350. 0x54, 0xB1, 0x51, 0x47, 0xEF, 0x2E, 0x52, 0xA3,
  30351. 0x9C, 0xD0, 0xC6, 0x4B, 0xFD, 0x81, 0xEA, 0xA2,
  30352. 0x1B, 0x50, 0xAB, 0x9A, 0x11, 0x56, 0xBD, 0x41,
  30353. 0xDD, 0xB9, 0x93, 0x87, 0x24, 0xD1, 0x71, 0x39,
  30354. 0x39, 0x3F, 0x2D, 0x36, 0x1A, 0xAE, 0xE6, 0x76,
  30355. 0xB7, 0xF5, 0xA2, 0xBB, 0x45, 0x6B, 0xB5, 0x9C,
  30356. 0xF0, 0x4A, 0xC1, 0x7F, 0x0B, 0xD3, 0x14, 0x83,
  30357. 0xCD, 0x45, 0xC7, 0x5B, 0xF7, 0xD5, 0xD2, 0x7D,
  30358. 0x9A, 0x3A, 0x5D, 0x4E, 0x49, 0x1F, 0x1E, 0x3C,
  30359. 0x73, 0xE9, 0xD5, 0xC3, 0x46, 0x02, 0x3F, 0x5E,
  30360. 0xAC, 0x39, 0x1B, 0xBE, 0xE3, 0x2A, 0xD8, 0x01,
  30361. 0xA5, 0x9D, 0xFC, 0x03, 0x58, 0x26, 0x51, 0x11,
  30362. 0xE0, 0x3D, 0xD6, 0x01, 0xA8, 0x66, 0x5C, 0xE3,
  30363. 0xEC, 0xE5, 0x23, 0x1F, 0x1F, 0x5F, 0x5B, 0x47,
  30364. 0xEF, 0x38, 0x1B, 0x47, 0xBC, 0x18, 0xE4, 0xDE,
  30365. 0x73, 0xD0, 0xD5, 0x68, 0x10, 0x91, 0x33, 0x5E,
  30366. 0x8A, 0xF7, 0xE1, 0x2C, 0x0E, 0xA8, 0x92, 0x24,
  30367. 0xA2, 0x0F, 0x5F, 0x18, 0x66, 0x64, 0x31, 0x9B,
  30368. 0xFB, 0x55, 0x5F, 0x5F, 0x5D, 0x07, 0x41, 0x33,
  30369. 0x77, 0xB3, 0x41, 0x6F, 0x91, 0x35, 0x66, 0xB8,
  30370. 0xFF, 0x31, 0x48, 0x11, 0xD3, 0x3D, 0x42, 0xE4,
  30371. 0xFE, 0xE5, 0x17, 0xF3, 0xD2, 0xB9, 0x82, 0x8F,
  30372. 0x76, 0x81, 0x18, 0x03, 0x67, 0x3E, 0x86, 0x5A,
  30373. 0xFB, 0xA6, 0x64, 0xB7, 0x5D, 0x4A, 0x8B, 0xC7,
  30374. 0xA1, 0x36, 0xA6, 0x8C, 0x81, 0x39, 0x1E, 0x43,
  30375. 0x20, 0x87, 0xDE, 0x28, 0x51, 0xE5, 0xCB, 0x26,
  30376. 0xBF, 0xF6, 0x5D, 0x70, 0xBC, 0x0C, 0xDC, 0x35,
  30377. 0x9F, 0xE5, 0xFA, 0x0A, 0x45, 0x94, 0x80, 0x9C,
  30378. 0xFC, 0x0F, 0xC6, 0x4F, 0x0A, 0x46, 0x98, 0x2E,
  30379. 0xA1, 0xEA, 0x78, 0xA1, 0x8E, 0x1D, 0xCE, 0x00,
  30380. 0xCD, 0x2F, 0xE0, 0x7D, 0xC4, 0xE6, 0xC7, 0x04,
  30381. 0x50, 0x4B, 0x08, 0x33, 0xAA, 0x16, 0xD3, 0xB1,
  30382. 0x5D, 0xA5, 0x68, 0x26, 0x35, 0x70, 0x46, 0x66,
  30383. 0x0D, 0x47, 0x2C, 0x66, 0xAB, 0xEB, 0xD7, 0xFD,
  30384. 0x48, 0xFD, 0x62, 0x0D, 0x37, 0x9F, 0x2A, 0xEF,
  30385. 0x93, 0x99, 0x3B, 0xB8, 0xAA, 0x56, 0x7D, 0x9A,
  30386. 0x5A, 0x12, 0x94, 0xB4, 0x69, 0x80, 0xC0, 0x02,
  30387. 0x3B, 0xB8, 0x94, 0xD3, 0x70, 0x94, 0xF2, 0x95,
  30388. 0x6C, 0x8D, 0x1D, 0x8F, 0xC3, 0xF1, 0x98, 0x09,
  30389. 0x93, 0x54, 0x7E, 0xA4, 0x8F, 0xD4, 0xCB, 0xDB,
  30390. 0xD0, 0x97, 0xB3, 0xEE, 0xFA, 0xF8, 0x65, 0x75,
  30391. 0xEB, 0xBD, 0x23, 0xDB, 0xB2, 0x63, 0x04, 0xA0,
  30392. 0x9B, 0x1D, 0xE0, 0x55, 0xB7, 0x8A, 0x5F, 0xC4,
  30393. 0xBD, 0x41, 0x13, 0x27, 0x41, 0xD3, 0x87, 0xAC,
  30394. 0x3A, 0x58, 0x22, 0x19, 0xFE, 0x39, 0xC5, 0x33,
  30395. 0x2F, 0x8A, 0xD8, 0x8E, 0xB4, 0x9F, 0x9E, 0xEA,
  30396. 0xCB, 0x7C, 0xD7, 0xD5, 0x34, 0x47, 0xAB, 0x62,
  30397. 0x48, 0xBA, 0xA8, 0x22, 0x7F, 0xA1, 0xB2, 0x4D,
  30398. 0x60, 0xE8, 0xBA, 0xAC, 0xB1, 0x9D, 0x00, 0x40,
  30399. 0x24, 0xAF, 0x18, 0x6C, 0xAD, 0x86, 0xAC, 0xA0,
  30400. 0xD6, 0xAA, 0x8E, 0x8C, 0x36, 0x4E, 0xBD, 0xCB,
  30401. 0x43, 0x27, 0xC3, 0x6D, 0x30, 0x88, 0xDC, 0x80,
  30402. 0xCE, 0x04, 0xF3, 0xA0, 0xA7, 0x9C, 0x32, 0xAB,
  30403. 0x6C, 0x3F, 0x14, 0xF4, 0xD6, 0x29, 0x14, 0x69,
  30404. 0x2E, 0x4A, 0x9A, 0xFF, 0xD1, 0x96, 0x09, 0x14,
  30405. 0x05, 0x62, 0x33, 0x3A, 0x05, 0x65, 0x96, 0x2B,
  30406. 0xD8, 0xAC, 0xD2, 0x59, 0x50, 0xF5, 0x6C, 0x94,
  30407. 0x2C, 0xE1, 0x5C, 0x38, 0x1C, 0x95, 0x03, 0xD7,
  30408. 0x1C, 0xF1, 0x7C, 0x5E, 0x97, 0xA6, 0xAE, 0xFB,
  30409. 0x3F, 0xCF, 0x48, 0x80, 0x4E, 0x3F, 0x07, 0x5D,
  30410. 0x71, 0xD5, 0x6F, 0x44, 0x49, 0x93, 0x9A, 0xF0,
  30411. 0x47, 0x86, 0xD6, 0x2D, 0x12, 0xAD, 0x67, 0x06,
  30412. 0x65, 0x3E, 0xE9, 0x68, 0x84, 0x0E, 0xFC, 0xCE,
  30413. 0x28, 0x72, 0x57, 0x64, 0xE3, 0x07, 0xC0, 0x0F,
  30414. 0x0C, 0xBA, 0x57, 0x5A, 0x33, 0xC9, 0x6C, 0x6F,
  30415. 0x5A, 0x74, 0x9E, 0x59, 0x94, 0x5B, 0x92, 0xB3,
  30416. 0x0F, 0xC8, 0x99, 0xDE, 0x61, 0xD1, 0x1C, 0xCF,
  30417. 0xBF, 0xEF, 0x98, 0x3B, 0xF8, 0x54, 0xC8, 0xF5,
  30418. 0x41, 0x06, 0xB3, 0x03, 0x62, 0x4D, 0x79, 0xF0,
  30419. 0x66, 0x58, 0xFB, 0x4B, 0xDC, 0x69, 0x6C, 0xE0,
  30420. 0x59, 0x77, 0xDE, 0x4A, 0x11, 0x5D, 0x83, 0x8D,
  30421. 0x83, 0x50, 0xF3, 0xC2, 0xBE, 0x35, 0xA6, 0x54,
  30422. 0xCA, 0xBB, 0x1E, 0xBD, 0x46, 0xA9, 0xF0, 0x79,
  30423. 0xBA, 0xE4, 0x6B, 0x31, 0xEE, 0x45, 0x74, 0x26,
  30424. 0xF3, 0x97, 0x9E, 0x36, 0x61, 0x1F, 0xAF, 0x16,
  30425. 0x87, 0x0A, 0xBE, 0x9D, 0x8F, 0x93, 0xB5, 0x0A,
  30426. 0xC1, 0x49, 0x98, 0xE1, 0xA4, 0x95, 0xF5, 0x1A,
  30427. 0x1B, 0x02, 0x05, 0x6A, 0x2F, 0xFB, 0x17, 0x11,
  30428. 0x4C, 0xA7, 0x21, 0xAC, 0x24, 0x6C, 0x29, 0x20,
  30429. 0xBC, 0xEA, 0x3A, 0x72, 0x36, 0x7A, 0x0D, 0xA5,
  30430. 0x02, 0xE3, 0xF2, 0x5F, 0xFB, 0x3A, 0x58, 0x10,
  30431. 0x01, 0xAC, 0xFC, 0xDD, 0x91, 0x7E, 0xA7, 0x89,
  30432. 0xE4, 0xEF, 0x88, 0x43, 0x4D, 0xFE, 0x20, 0xBC,
  30433. 0x54, 0x78, 0x42, 0x8D, 0xD0, 0x80, 0xA0, 0xCA,
  30434. 0xDD, 0xC6, 0x1F, 0x02, 0x32, 0x03, 0x15, 0x1C,
  30435. 0x5A, 0x82, 0xD4, 0x95, 0x21, 0x8F, 0xF3, 0xFB,
  30436. 0x1E, 0x22, 0x7D, 0xBF, 0xCA, 0x1B, 0x5A, 0xBC,
  30437. 0xE6, 0x39, 0xC4, 0x9C, 0xD8, 0xEC, 0x12, 0x95,
  30438. 0x83, 0xC5, 0x73, 0xC5, 0x17, 0x02, 0xA1, 0xB5,
  30439. 0xC1, 0x01, 0x72, 0x13, 0x7C, 0x36, 0xDD, 0x66,
  30440. 0xD1, 0x84, 0x99, 0xB0, 0x78, 0x7D, 0xAB, 0xA9,
  30441. 0x78, 0xAC, 0x76, 0xC1, 0x14, 0xD0, 0x17, 0x8C,
  30442. 0xCB, 0xF7, 0xE6, 0xE1, 0x31, 0xBD, 0x9E, 0x53,
  30443. 0x3A, 0x13, 0x33, 0xD2, 0x2B, 0xD1, 0x7B, 0x99,
  30444. 0x6F, 0x76, 0x22, 0x57, 0x5C, 0xF9, 0x8B, 0x37,
  30445. 0x0D, 0x77, 0x17, 0xB7, 0x24, 0x48, 0x7B, 0xFB,
  30446. 0x27, 0x74, 0x62, 0xC8, 0x3C, 0x1C, 0xAA, 0xF6,
  30447. 0x28, 0x85, 0x7E, 0x71, 0xD6, 0x16, 0xDD, 0x5D,
  30448. 0x52, 0x87, 0xBD, 0xF3, 0x7B, 0x19, 0xDA, 0xAA,
  30449. 0xE4, 0xD2, 0x4C, 0xCC, 0xEC, 0xB1, 0x26, 0xAE,
  30450. 0x74, 0x25, 0x19, 0x7A, 0x21, 0x51, 0x13, 0x91,
  30451. 0xBA, 0x1E, 0x7D, 0x13, 0x08, 0x49, 0x82, 0xED,
  30452. 0x29, 0x1D, 0xDD, 0x51, 0x6A, 0x60, 0xED, 0xCD,
  30453. 0x86, 0x26, 0xF0, 0x0E, 0xEF, 0x6A, 0xA4, 0xD3,
  30454. 0x76, 0xE1, 0x53, 0x90, 0x64, 0x88, 0x46, 0x64,
  30455. 0x09, 0xCC, 0x26, 0x50, 0x82, 0xAA, 0xB0, 0x4E,
  30456. 0x5E, 0xDC, 0x8B, 0xE2, 0x45, 0xF5, 0x38, 0xC3,
  30457. 0x6E, 0x7E, 0x0A, 0xE3, 0xE7, 0x10, 0x9E, 0x25,
  30458. 0xDA, 0xF8, 0x09, 0x19, 0xCE, 0xD7, 0x30, 0xA6,
  30459. 0x93, 0xFF, 0xF8, 0xD2, 0x71, 0x6F, 0xB2, 0x27,
  30460. 0x15, 0x93, 0x17, 0xE0, 0x7D, 0x9D, 0xAE, 0x5B,
  30461. 0xDA, 0x9B, 0x01, 0x05, 0xB1, 0x7B, 0xAE, 0x55,
  30462. 0xA7, 0x37, 0x56, 0x6D, 0xC4, 0x4E, 0x63, 0xA4,
  30463. 0x7E, 0xBC, 0xA5, 0xA4, 0xE9, 0xB2, 0xB8, 0x06,
  30464. 0x8C, 0xB8, 0x13, 0xBD, 0xD2, 0xC3, 0xFD, 0x73,
  30465. 0x90, 0x81, 0x0C, 0xCC, 0x2C, 0xA1, 0x48, 0xEF,
  30466. 0xAF, 0x14, 0x59, 0xD7, 0x39, 0xD2, 0x2A, 0x12,
  30467. 0x14, 0x83, 0x92, 0xEA, 0x0D, 0x9A, 0xC3, 0x76,
  30468. 0x10, 0xD5, 0x61, 0x29, 0x1E, 0xE3, 0x88, 0x06,
  30469. 0x09, 0x42, 0xB0, 0xD0, 0x12, 0xB1, 0xBC, 0x52,
  30470. 0x34, 0xF5, 0x77, 0xF5, 0xF3, 0xF3, 0x4A, 0xFA,
  30471. 0x77, 0x9C, 0x15, 0x5E, 0x72, 0x9A, 0x57, 0xD6,
  30472. 0x45, 0xF8, 0x8E, 0x88, 0x23, 0xC1, 0xE7, 0xC5,
  30473. 0x5B, 0x87, 0xB7, 0xEE, 0x06, 0x89, 0x0B, 0x9E,
  30474. 0xD8, 0x13, 0x04, 0x33, 0xC7, 0x57, 0x93, 0x2C,
  30475. 0x4A, 0xAA, 0x8E, 0x7A, 0x26, 0x3E, 0x59, 0x8C,
  30476. 0x87, 0xEE, 0x0D, 0xFE, 0x7E, 0x12, 0x0E, 0x60,
  30477. 0x7C, 0x0F, 0xED, 0x41, 0x72, 0x11, 0xC5, 0x3B,
  30478. 0x10, 0xA7, 0x12, 0x24, 0x61, 0x56, 0xCA, 0xB4,
  30479. 0xBB, 0xCB, 0x46, 0xF2, 0xA4, 0x1F, 0xBB, 0x6B,
  30480. 0xD3, 0x67, 0x3D, 0x4E, 0x89, 0xDA, 0xE8, 0x88,
  30481. 0xD0, 0x35, 0xF8, 0xE5, 0x2A, 0xEA, 0x4D, 0x58,
  30482. 0x41, 0x1D, 0x5E, 0x24, 0xFA, 0x6C, 0x4A, 0x72,
  30483. 0x17, 0x4A, 0xDA, 0xA0, 0xD8, 0x85, 0xFA, 0x64,
  30484. 0xB1, 0x2B, 0x16, 0xEF, 0x85, 0x65, 0xDD, 0xE9,
  30485. 0x84, 0x56, 0xD6, 0x72, 0xBB, 0x52, 0xF6, 0x00,
  30486. 0x7B, 0x23, 0xF4, 0x74, 0x8F, 0x63, 0xA2, 0x48,
  30487. 0xAF, 0x1A, 0xB1, 0x86, 0xEA, 0xAC, 0xBD, 0xA1,
  30488. 0xC2, 0xB0, 0xB4, 0x77, 0xC6, 0x25, 0xA0, 0x0C,
  30489. 0xFF, 0x14, 0xFA, 0xD3, 0xCF, 0xC9, 0xFC, 0x2C,
  30490. 0xF2, 0xAE, 0x7E, 0x4B, 0x55, 0xE0, 0x41, 0xF8,
  30491. 0x5E, 0x2D, 0x03, 0xD3, 0x60, 0x25, 0xAB, 0x4D,
  30492. 0xF6, 0x65, 0x12, 0xBC, 0xE9, 0x66, 0x7F, 0x2A,
  30493. 0x2D, 0xF9, 0xC9, 0xAA, 0xE2, 0x80, 0x39, 0x7F,
  30494. 0xEF, 0x0C, 0x03, 0x8B, 0x54, 0x7A, 0xE5, 0x87,
  30495. 0xE7, 0xBE, 0x80, 0x22, 0x37, 0x96, 0xE3, 0x9A,
  30496. 0x93, 0x99, 0x99, 0x9A, 0xE5, 0x7A, 0xD6, 0x80,
  30497. 0x26, 0xF1, 0x3F, 0xCA, 0x9E, 0xE6, 0x33, 0x79,
  30498. 0xC9, 0x09, 0x9A, 0xCB, 0x2A, 0x61, 0x19, 0x04,
  30499. 0x74, 0x40, 0x5B, 0x57, 0xE3, 0x48, 0x4A, 0x52
  30500. };
  30501. const byte kyber1024_ss[] = {
  30502. 0xC1, 0x4E, 0xBD, 0x6E, 0x37, 0x88, 0xA6, 0x41,
  30503. 0xD1, 0x75, 0x5B, 0x4C, 0x86, 0x9C, 0x46, 0x99,
  30504. 0x4F, 0x75, 0xAF, 0x16, 0xB4, 0x0F, 0x8F, 0x64,
  30505. 0x92, 0xCC, 0xBE, 0xC7, 0xA3, 0xDA, 0x9B, 0xFE
  30506. };
  30507. #endif
  30508. ret = wc_KyberKey_Init(KYBER1024, &key, HEAP_HINT, INVALID_DEVID);
  30509. if (ret != 0)
  30510. return -20301;
  30511. ret = wc_KyberKey_MakeKeyWithRandom(&key, kyber1024_rand,
  30512. sizeof(kyber1024_rand));
  30513. if (ret != 0)
  30514. return -20302;
  30515. ret = wc_KyberKey_EncodePublicKey(&key, pub, sizeof(pub));
  30516. if (ret != 0)
  30517. return -20303;
  30518. ret = wc_KyberKey_EncodePrivateKey(&key, priv, sizeof(priv));
  30519. if (ret != 0)
  30520. return -20304;
  30521. if (XMEMCMP(pub, kyber1024_pk, sizeof(kyber1024_pk)) != 0)
  30522. return -20305;
  30523. if (XMEMCMP(priv, kyber1024_sk, sizeof(kyber1024_sk)) != 0)
  30524. return -20306;
  30525. ret = wc_KyberKey_EncapsulateWithRandom(&key, ct, ss, kyber1024enc_rand,
  30526. sizeof(kyber1024enc_rand));
  30527. if (ret != 0)
  30528. return -20307;
  30529. if (XMEMCMP(ct, kyber1024_ct, sizeof(kyber1024_ct)) != 0)
  30530. return -20308;
  30531. if (XMEMCMP(ss, kyber1024_ss, sizeof(kyber1024_ss)) != 0)
  30532. return -20309;
  30533. ret = wc_KyberKey_Decapsulate(&key, ss_dec, ct, sizeof(kyber1024_ct));
  30534. if (ret != 0)
  30535. return -20310;
  30536. if (XMEMCMP(ss_dec, kyber1024_ss, sizeof(kyber1024_ss)) != 0)
  30537. return -20311;
  30538. wc_KyberKey_Free(&key);
  30539. return 0;
  30540. }
  30541. #endif /* WOLFSSL_KYBER1024 */
  30542. #endif /* WOLFSSL_WC_KYBER */
  30543. WOLFSSL_TEST_SUBROUTINE int kyber_test(void)
  30544. {
  30545. int ret;
  30546. KyberKey key;
  30547. WC_RNG rng;
  30548. int i;
  30549. byte priv[KYBER_MAX_PRIVATE_KEY_SIZE];
  30550. byte pub[KYBER_MAX_PUBLIC_KEY_SIZE];
  30551. byte priv2[KYBER_MAX_PRIVATE_KEY_SIZE];
  30552. byte pub2[KYBER_MAX_PUBLIC_KEY_SIZE];
  30553. byte ct[KYBER_MAX_CIPHER_TEXT_SIZE];
  30554. byte ss[KYBER_SS_SZ];
  30555. byte ss_dec[KYBER_SS_SZ];
  30556. int testData[][4] = {
  30557. #ifdef WOLFSSL_KYBER512
  30558. { KYBER512, KYBER512_PRIVATE_KEY_SIZE, KYBER512_PUBLIC_KEY_SIZE,
  30559. KYBER512_CIPHER_TEXT_SIZE },
  30560. #endif
  30561. #ifdef WOLFSSL_KYBER768
  30562. { KYBER768, KYBER768_PRIVATE_KEY_SIZE, KYBER768_PUBLIC_KEY_SIZE,
  30563. KYBER768_CIPHER_TEXT_SIZE },
  30564. #endif
  30565. #ifdef WOLFSSL_KYBER1024
  30566. { KYBER1024, KYBER1024_PRIVATE_KEY_SIZE, KYBER1024_PUBLIC_KEY_SIZE,
  30567. KYBER1024_CIPHER_TEXT_SIZE },
  30568. #endif
  30569. };
  30570. #ifndef HAVE_FIPS
  30571. ret = wc_InitRng_ex(&rng, HEAP_HINT, INVALID_DEVID);
  30572. #else
  30573. ret = wc_InitRng(&rng);
  30574. #endif
  30575. if (ret != 0)
  30576. return -20000;
  30577. for (i = 0; i < (int)(sizeof(testData) / sizeof(*testData)); i++) {
  30578. ret = wc_KyberKey_Init(testData[i][0], &key, HEAP_HINT, INVALID_DEVID);
  30579. if (ret != 0)
  30580. return -20001 - i * 20;
  30581. ret = wc_KyberKey_MakeKey(&key, &rng);
  30582. if (ret != 0)
  30583. return -20002 - i * 20;
  30584. ret = wc_KyberKey_EncodePublicKey(&key, pub, testData[i][2]);
  30585. if (ret != 0)
  30586. return -20003 - i * 20;
  30587. ret = wc_KyberKey_EncodePrivateKey(&key, priv, testData[i][1]);
  30588. if (ret != 0)
  30589. return -20004 - i * 20;
  30590. ret = wc_KyberKey_Init(testData[i][0], &key, HEAP_HINT, INVALID_DEVID);
  30591. if (ret != 0)
  30592. return -20005 - i * 20;
  30593. ret = wc_KyberKey_DecodePublicKey(&key, pub, testData[i][2]);
  30594. if (ret != 0)
  30595. return -20006 - i * 20;
  30596. ret = wc_KyberKey_Encapsulate(&key, ct, ss, &rng);
  30597. if (ret != 0)
  30598. return -20007 - i * 20;
  30599. ret = wc_KyberKey_EncodePublicKey(&key, pub2, testData[i][2]);
  30600. if (ret != 0)
  30601. return -20008 - i * 20;
  30602. if (XMEMCMP(pub, pub2, testData[i][2]) != 0)
  30603. return -20009 - i * 20;
  30604. ret = wc_KyberKey_Init(testData[i][0], &key, HEAP_HINT, INVALID_DEVID);
  30605. if (ret != 0)
  30606. return -20010 - i * 20;
  30607. ret = wc_KyberKey_DecodePrivateKey(&key, priv, testData[i][1]);
  30608. if (ret != 0)
  30609. return -20011 - i * 20;
  30610. ret = wc_KyberKey_Decapsulate(&key, ss_dec, ct, testData[i][3]);
  30611. if (ret != 0)
  30612. return -20012 - i * 20;
  30613. if (XMEMCMP(ss, ss_dec, sizeof(ss)) != 0)
  30614. return -20013 - i * 20;
  30615. ret = wc_KyberKey_EncodePrivateKey(&key, priv2, testData[i][1]);
  30616. if (ret != 0)
  30617. return -20014 - i * 20;
  30618. if (XMEMCMP(priv, priv2, testData[i][2]) != 0)
  30619. return -20015 - i * 20;
  30620. wc_KyberKey_Free(&key);
  30621. }
  30622. wc_FreeRng(&rng);
  30623. #ifdef WOLFSSL_WC_KYBER
  30624. #ifdef WOLFSSL_KYBER512
  30625. ret = kyber512_kat();
  30626. if (ret != 0)
  30627. return ret;
  30628. #endif
  30629. #ifdef WOLFSSL_KYBER768
  30630. ret = kyber768_kat();
  30631. if (ret != 0)
  30632. return ret;
  30633. #endif
  30634. #ifdef WOLFSSL_KYBER1024
  30635. ret = kyber1024_kat();
  30636. if (ret != 0)
  30637. return ret;
  30638. #endif
  30639. #endif /* WOLFSSL_WC_KYBER */
  30640. return 0;
  30641. }
  30642. #endif /* WOLFSSL_HAVE_KYBER */
  30643. #ifdef WOLFCRYPT_HAVE_ECCSI
  30644. static int eccsi_api_test(WC_RNG* rng, EccsiKey* key, mp_int* ssk,
  30645. ecc_point* pvt)
  30646. {
  30647. int ret;
  30648. byte id[1] = { 0x00 };
  30649. int valid;
  30650. word32 sz;
  30651. byte data[256];
  30652. byte hash[WC_MAX_DIGEST_SIZE];
  30653. byte hashSz;
  30654. byte sig[257];
  30655. word32 sigSz;
  30656. ret = wc_InitEccsiKey_ex(NULL, 32, ECC_SECP256R1, HEAP_HINT, INVALID_DEVID);
  30657. if (ret != BAD_FUNC_ARG)
  30658. return -10023;
  30659. ret = wc_InitEccsiKey_ex(NULL, 32, ECC_SECP256R1, HEAP_HINT, INVALID_DEVID);
  30660. if (ret != BAD_FUNC_ARG)
  30661. return -10024;
  30662. ret = wc_InitEccsiKey(NULL, NULL, INVALID_DEVID);
  30663. if (ret != BAD_FUNC_ARG)
  30664. return -10025;
  30665. ret = wc_InitEccsiKey(NULL, HEAP_HINT, INVALID_DEVID);
  30666. if (ret != BAD_FUNC_ARG)
  30667. return -10026;
  30668. wc_FreeEccsiKey(NULL);
  30669. /* Create a valid key. */
  30670. ret = wc_InitEccsiKey(key, NULL, INVALID_DEVID);
  30671. if (ret != 0)
  30672. return -10027;
  30673. ret = wc_MakeEccsiKey(NULL, NULL);
  30674. if (ret != BAD_FUNC_ARG)
  30675. return -10028;
  30676. ret = wc_MakeEccsiKey(key, NULL);
  30677. if (ret != BAD_FUNC_ARG)
  30678. return -10029;
  30679. ret = wc_MakeEccsiKey(NULL, rng);
  30680. if (ret != BAD_FUNC_ARG)
  30681. return -10030;
  30682. ret = wc_MakeEccsiPair(NULL, NULL, WC_HASH_TYPE_SHA256, NULL, 1, NULL,
  30683. NULL);
  30684. if (ret != BAD_FUNC_ARG)
  30685. return -10031;
  30686. ret = wc_MakeEccsiPair(key, rng, WC_HASH_TYPE_SHA256, id, 1, ssk, NULL);
  30687. if (ret != BAD_FUNC_ARG)
  30688. return -10032;
  30689. ret = wc_MakeEccsiPair(key, rng, WC_HASH_TYPE_SHA256, id, 1, NULL, pvt);
  30690. if (ret != BAD_FUNC_ARG)
  30691. return -10033;
  30692. ret = wc_MakeEccsiPair(key, rng, WC_HASH_TYPE_SHA256, NULL, 1, ssk, pvt);
  30693. if (ret != BAD_FUNC_ARG)
  30694. return -10034;
  30695. ret = wc_MakeEccsiPair(key, NULL, WC_HASH_TYPE_SHA256, id, 1, ssk, pvt);
  30696. if (ret != BAD_FUNC_ARG)
  30697. return -10035;
  30698. ret = wc_MakeEccsiPair(NULL, rng, WC_HASH_TYPE_SHA256, id, 1, ssk, pvt);
  30699. if (ret != BAD_FUNC_ARG)
  30700. return -10036;
  30701. /* No key set */
  30702. ret = wc_MakeEccsiPair(key, rng, WC_HASH_TYPE_SHA256, id, 1, ssk, pvt);
  30703. if (ret != BAD_STATE_E)
  30704. return -10037;
  30705. ret = wc_ValidateEccsiPair(NULL, WC_HASH_TYPE_SHA256, NULL, 1, NULL, NULL,
  30706. NULL);
  30707. if (ret != BAD_FUNC_ARG)
  30708. return -10038;
  30709. ret = wc_ValidateEccsiPair(key, WC_HASH_TYPE_SHA256, id, 1, ssk, pvt,
  30710. NULL);
  30711. if (ret != BAD_FUNC_ARG)
  30712. return -10039;
  30713. ret = wc_ValidateEccsiPair(key, WC_HASH_TYPE_SHA256, id, 1, ssk, NULL,
  30714. &valid);
  30715. if (ret != BAD_FUNC_ARG)
  30716. return -10040;
  30717. ret = wc_ValidateEccsiPair(key, WC_HASH_TYPE_SHA256, id, 1, NULL, pvt,
  30718. &valid);
  30719. if (ret != BAD_FUNC_ARG)
  30720. return -10041;
  30721. ret = wc_ValidateEccsiPair(key, WC_HASH_TYPE_SHA256, NULL, 1, ssk, pvt,
  30722. &valid);
  30723. if (ret != BAD_FUNC_ARG)
  30724. return -10042;
  30725. ret = wc_ValidateEccsiPair(NULL, WC_HASH_TYPE_SHA256, id, 1, ssk, pvt,
  30726. &valid);
  30727. if (ret != BAD_FUNC_ARG)
  30728. return -10043;
  30729. /* No key set */
  30730. ret = wc_ValidateEccsiPair(key, WC_HASH_TYPE_SHA256, id, 1, ssk, pvt,
  30731. &valid);
  30732. if (ret != BAD_STATE_E)
  30733. return -10044;
  30734. ret = wc_ValidateEccsiPvt(NULL, NULL, NULL);
  30735. if (ret != BAD_FUNC_ARG)
  30736. return -10045;
  30737. ret = wc_ValidateEccsiPvt(key, NULL, NULL);
  30738. if (ret != BAD_FUNC_ARG)
  30739. return -10046;
  30740. ret = wc_ValidateEccsiPvt(NULL, pvt, NULL);
  30741. if (ret != BAD_FUNC_ARG)
  30742. return -10047;
  30743. ret = wc_ValidateEccsiPvt(NULL, NULL, &valid);
  30744. if (ret != BAD_FUNC_ARG)
  30745. return -10048;
  30746. ret = wc_ValidateEccsiPvt(key, pvt, NULL);
  30747. if (ret != BAD_FUNC_ARG)
  30748. return -10049;
  30749. ret = wc_ValidateEccsiPvt(key, NULL, &valid);
  30750. if (ret != BAD_FUNC_ARG)
  30751. return -10050;
  30752. ret = wc_ValidateEccsiPvt(NULL, pvt, &valid);
  30753. if (ret != BAD_FUNC_ARG)
  30754. return -10051;
  30755. ret = wc_EncodeEccsiPair(NULL, NULL, NULL, data, NULL);
  30756. if (ret != BAD_FUNC_ARG)
  30757. return -10052;
  30758. ret = wc_EncodeEccsiPair(key, ssk, pvt, data, NULL);
  30759. if (ret != BAD_FUNC_ARG)
  30760. return -10053;
  30761. ret = wc_EncodeEccsiPair(key, ssk, NULL, data, &sz);
  30762. if (ret != BAD_FUNC_ARG)
  30763. return -10054;
  30764. ret = wc_EncodeEccsiPair(key, NULL, pvt, data, &sz);
  30765. if (ret != BAD_FUNC_ARG)
  30766. return -10055;
  30767. ret = wc_EncodeEccsiPair(NULL, ssk, pvt, data, &sz);
  30768. if (ret != BAD_FUNC_ARG)
  30769. return -10056;
  30770. /* No key created so no curve information. */
  30771. ret = wc_EncodeEccsiPair(key, ssk, pvt, NULL, &sz);
  30772. if (ret != LENGTH_ONLY_E)
  30773. return -10057;
  30774. ret = wc_EncodeEccsiSsk(NULL, NULL, data, NULL);
  30775. if (ret != BAD_FUNC_ARG)
  30776. return -10058;
  30777. ret = wc_EncodeEccsiSsk(key, ssk, data, NULL);
  30778. if (ret != BAD_FUNC_ARG)
  30779. return -10059;
  30780. ret = wc_EncodeEccsiSsk(key, NULL, data, &sz);
  30781. if (ret != BAD_FUNC_ARG)
  30782. return -10060;
  30783. ret = wc_EncodeEccsiSsk(NULL, ssk, data, &sz);
  30784. if (ret != BAD_FUNC_ARG)
  30785. return -10061;
  30786. ret = wc_EncodeEccsiPvt(NULL, NULL, data, NULL, 1);
  30787. if (ret != BAD_FUNC_ARG)
  30788. return -10058;
  30789. ret = wc_EncodeEccsiPvt(key, pvt, data, NULL, 1);
  30790. if (ret != BAD_FUNC_ARG)
  30791. return -10059;
  30792. ret = wc_EncodeEccsiPvt(key, NULL, data, &sz, 1);
  30793. if (ret != BAD_FUNC_ARG)
  30794. return -10060;
  30795. ret = wc_EncodeEccsiPvt(NULL, pvt, data, &sz, 1);
  30796. if (ret != BAD_FUNC_ARG)
  30797. return -10061;
  30798. ret = wc_DecodeEccsiPair(NULL, NULL, 0, NULL, NULL);
  30799. if (ret != BAD_FUNC_ARG)
  30800. return -10062;
  30801. ret = wc_DecodeEccsiPair(key, data, 0, ssk, NULL);
  30802. if (ret != BAD_FUNC_ARG)
  30803. return -10063;
  30804. ret = wc_DecodeEccsiPair(key, data, 0, NULL, pvt);
  30805. if (ret != BAD_FUNC_ARG)
  30806. return -10064;
  30807. ret = wc_DecodeEccsiPair(key, NULL, 0, ssk, pvt);
  30808. if (ret != BAD_FUNC_ARG)
  30809. return -10065;
  30810. ret = wc_DecodeEccsiPair(NULL, data, 0, ssk, pvt);
  30811. if (ret != BAD_FUNC_ARG)
  30812. return -10066;
  30813. ret = wc_DecodeEccsiSsk(NULL, NULL, 0, NULL);
  30814. if (ret != BAD_FUNC_ARG)
  30815. return -10067;
  30816. ret = wc_DecodeEccsiSsk(key, data, 0, NULL);
  30817. if (ret != BAD_FUNC_ARG)
  30818. return -10068;
  30819. ret = wc_DecodeEccsiSsk(key, NULL, 0, ssk);
  30820. if (ret != BAD_FUNC_ARG)
  30821. return -10069;
  30822. ret = wc_DecodeEccsiSsk(NULL, data, 0, ssk);
  30823. if (ret != BAD_FUNC_ARG)
  30824. return -10070;
  30825. ret = wc_DecodeEccsiPvt(NULL, NULL, 0, NULL);
  30826. if (ret != BAD_FUNC_ARG)
  30827. return -10067;
  30828. ret = wc_DecodeEccsiPvt(key, data, 0, NULL);
  30829. if (ret != BAD_FUNC_ARG)
  30830. return -10068;
  30831. ret = wc_DecodeEccsiPvt(key, NULL, 0, pvt);
  30832. if (ret != BAD_FUNC_ARG)
  30833. return -10069;
  30834. ret = wc_DecodeEccsiPvt(NULL, data, 0, pvt);
  30835. if (ret != BAD_FUNC_ARG)
  30836. return -10070;
  30837. ret = wc_DecodeEccsiPvtFromSig(NULL, NULL, 0, NULL);
  30838. if (ret != BAD_FUNC_ARG)
  30839. return -10067;
  30840. ret = wc_DecodeEccsiPvtFromSig(key, data, 0, NULL);
  30841. if (ret != BAD_FUNC_ARG)
  30842. return -10068;
  30843. ret = wc_DecodeEccsiPvtFromSig(key, NULL, 0, pvt);
  30844. if (ret != BAD_FUNC_ARG)
  30845. return -10069;
  30846. ret = wc_DecodeEccsiPvtFromSig(NULL, data, 0, pvt);
  30847. if (ret != BAD_FUNC_ARG)
  30848. return -10070;
  30849. ret = wc_ExportEccsiKey(NULL, data, NULL);
  30850. if (ret != BAD_FUNC_ARG)
  30851. return -10071;
  30852. ret = wc_ExportEccsiKey(key, data, NULL);
  30853. if (ret != BAD_FUNC_ARG)
  30854. return -10072;
  30855. ret = wc_ExportEccsiKey(NULL, data, &sz);
  30856. if (ret != BAD_FUNC_ARG)
  30857. return -10073;
  30858. /* No key to export */
  30859. ret = wc_ExportEccsiKey(key, NULL, &sz);
  30860. if (ret != BAD_STATE_E)
  30861. return -10074;
  30862. ret = wc_ImportEccsiKey(NULL, NULL, 0);
  30863. if (ret != BAD_FUNC_ARG)
  30864. return -10075;
  30865. ret = wc_ImportEccsiKey(key, NULL, 0);
  30866. if (ret != BAD_FUNC_ARG)
  30867. return -10076;
  30868. ret = wc_ImportEccsiKey(NULL, data, 0);
  30869. if (ret != BAD_FUNC_ARG)
  30870. return -10077;
  30871. ret = wc_ExportEccsiPrivateKey(NULL, data, NULL);
  30872. if (ret != BAD_FUNC_ARG)
  30873. return -10071;
  30874. ret = wc_ExportEccsiPrivateKey(key, data, NULL);
  30875. if (ret != BAD_FUNC_ARG)
  30876. return -10072;
  30877. ret = wc_ExportEccsiPrivateKey(NULL, data, &sz);
  30878. if (ret != BAD_FUNC_ARG)
  30879. return -10073;
  30880. /* No key to export */
  30881. ret = wc_ExportEccsiPrivateKey(key, NULL, &sz);
  30882. if (ret != BAD_STATE_E)
  30883. return -10074;
  30884. ret = wc_ImportEccsiPrivateKey(NULL, NULL, 0);
  30885. if (ret != BAD_FUNC_ARG)
  30886. return -10075;
  30887. ret = wc_ImportEccsiPrivateKey(key, NULL, 0);
  30888. if (ret != BAD_FUNC_ARG)
  30889. return -10076;
  30890. ret = wc_ImportEccsiPrivateKey(NULL, data, 0);
  30891. if (ret != BAD_FUNC_ARG)
  30892. return -10077;
  30893. ret = wc_ExportEccsiPublicKey(NULL, data, NULL, 1);
  30894. if (ret != BAD_FUNC_ARG)
  30895. return -10078;
  30896. ret = wc_ExportEccsiPublicKey(key, data, NULL, 1);
  30897. if (ret != BAD_FUNC_ARG)
  30898. return -10079;
  30899. ret = wc_ExportEccsiPublicKey(NULL, data, &sz, 1);
  30900. if (ret != BAD_FUNC_ARG)
  30901. return -10080;
  30902. /* No key to export */
  30903. ret = wc_ExportEccsiPublicKey(key, data, &sz, 1);
  30904. if (ret != BAD_STATE_E)
  30905. return -10081;
  30906. ret = wc_ImportEccsiPublicKey(NULL, NULL, 0, 1);
  30907. if (ret != BAD_FUNC_ARG)
  30908. return -10082;
  30909. ret = wc_ImportEccsiPublicKey(key, NULL, 0, 1);
  30910. if (ret != BAD_FUNC_ARG)
  30911. return -10083;
  30912. ret = wc_ImportEccsiPublicKey(NULL, data, 0, 1);
  30913. if (ret != BAD_FUNC_ARG)
  30914. return -10084;
  30915. ret = wc_HashEccsiId(NULL, WC_HASH_TYPE_SHA256, NULL, 1, NULL, NULL, NULL);
  30916. if (ret != BAD_FUNC_ARG)
  30917. return -10085;
  30918. ret = wc_HashEccsiId(key, WC_HASH_TYPE_SHA256, id, 1, pvt, hash, NULL);
  30919. if (ret != BAD_FUNC_ARG)
  30920. return -10086;
  30921. ret = wc_HashEccsiId(key, WC_HASH_TYPE_SHA256, id, 1, pvt, NULL, &hashSz);
  30922. if (ret != BAD_FUNC_ARG)
  30923. return -10087;
  30924. ret = wc_HashEccsiId(key, WC_HASH_TYPE_SHA256, id, 1, NULL, hash, &hashSz);
  30925. if (ret != BAD_FUNC_ARG)
  30926. return -10088;
  30927. ret = wc_HashEccsiId(key, WC_HASH_TYPE_SHA256, NULL, 1, pvt, hash,
  30928. &hashSz);
  30929. if (ret != BAD_FUNC_ARG)
  30930. return -10089;
  30931. ret = wc_HashEccsiId(NULL, WC_HASH_TYPE_SHA256, id, 1, pvt, hash, &hashSz);
  30932. if (ret != BAD_FUNC_ARG)
  30933. return -10090;
  30934. ret = wc_HashEccsiId(key, WC_HASH_TYPE_SHA256, id, 1, pvt, hash, &hashSz);
  30935. if (ret != BAD_STATE_E)
  30936. return -10091;
  30937. ret = wc_SetEccsiHash(NULL, NULL, 1);
  30938. if (ret != BAD_FUNC_ARG)
  30939. return -10090;
  30940. ret = wc_SetEccsiHash(key, NULL, 1);
  30941. if (ret != BAD_FUNC_ARG)
  30942. return -10090;
  30943. ret = wc_SetEccsiHash(NULL, hash, 1);
  30944. if (ret != BAD_FUNC_ARG)
  30945. return -10090;
  30946. ret = wc_SetEccsiPair(NULL, NULL, NULL);
  30947. if (ret != BAD_FUNC_ARG)
  30948. return -10090;
  30949. ret = wc_SetEccsiPair(key, NULL, NULL);
  30950. if (ret != BAD_FUNC_ARG)
  30951. return -10090;
  30952. ret = wc_SetEccsiPair(NULL, ssk, NULL);
  30953. if (ret != BAD_FUNC_ARG)
  30954. return -10090;
  30955. ret = wc_SetEccsiPair(NULL, NULL, pvt);
  30956. if (ret != BAD_FUNC_ARG)
  30957. return -10090;
  30958. ret = wc_SetEccsiPair(key, ssk, NULL);
  30959. if (ret != BAD_FUNC_ARG)
  30960. return -10090;
  30961. ret = wc_SetEccsiPair(key, NULL, pvt);
  30962. if (ret != BAD_FUNC_ARG)
  30963. return -10090;
  30964. ret = wc_SetEccsiPair(NULL, ssk, pvt);
  30965. if (ret != BAD_FUNC_ARG)
  30966. return -10090;
  30967. ret = wc_SignEccsiHash(NULL, NULL, WC_HASH_TYPE_SHA256, NULL, 0, sig, NULL);
  30968. if (ret != BAD_FUNC_ARG)
  30969. return -10092;
  30970. ret = wc_SignEccsiHash(key, rng, WC_HASH_TYPE_SHA256, data, 0, sig, NULL);
  30971. if (ret != BAD_FUNC_ARG)
  30972. return -10093;
  30973. ret = wc_SignEccsiHash(key, rng, WC_HASH_TYPE_SHA256, NULL, 0, sig,
  30974. &sigSz);
  30975. if (ret != BAD_FUNC_ARG)
  30976. return -10096;
  30977. ret = wc_SignEccsiHash(key, NULL, WC_HASH_TYPE_SHA256, data, 0, sig,
  30978. &sigSz);
  30979. if (ret != BAD_FUNC_ARG)
  30980. return -10098;
  30981. ret = wc_SignEccsiHash(NULL, rng, WC_HASH_TYPE_SHA256, data, 0, sig,
  30982. &sigSz);
  30983. if (ret != BAD_FUNC_ARG)
  30984. return -10099;
  30985. /* Key not set. */
  30986. ret = wc_SignEccsiHash(key, rng, WC_HASH_TYPE_SHA256, data, 0, NULL,
  30987. &sigSz);
  30988. if (ret != BAD_STATE_E)
  30989. return -10100;
  30990. ret = wc_VerifyEccsiHash(NULL, WC_HASH_TYPE_SHA256, NULL, 0, NULL, 0, NULL);
  30991. if (ret != BAD_FUNC_ARG)
  30992. return -10101;
  30993. ret = wc_VerifyEccsiHash(key, WC_HASH_TYPE_SHA256, NULL, 0, NULL, 0, NULL);
  30994. if (ret != BAD_FUNC_ARG)
  30995. return -10101;
  30996. ret = wc_VerifyEccsiHash(NULL, WC_HASH_TYPE_SHA256, data, 0, NULL, 0, NULL);
  30997. if (ret != BAD_FUNC_ARG)
  30998. return -10101;
  30999. ret = wc_VerifyEccsiHash(NULL, WC_HASH_TYPE_SHA256, NULL, 0, sig, 0, NULL);
  31000. if (ret != BAD_FUNC_ARG)
  31001. return -10101;
  31002. ret = wc_VerifyEccsiHash(NULL, WC_HASH_TYPE_SHA256, NULL, 0, NULL, 0,
  31003. &valid);
  31004. if (ret != BAD_FUNC_ARG)
  31005. return -10101;
  31006. ret = wc_VerifyEccsiHash(key, WC_HASH_TYPE_SHA256, data, 0, sig, 0, NULL);
  31007. if (ret != BAD_FUNC_ARG)
  31008. return -10102;
  31009. ret = wc_VerifyEccsiHash(key, WC_HASH_TYPE_SHA256, data, 0, NULL, 0,
  31010. &valid);
  31011. if (ret != BAD_FUNC_ARG)
  31012. return -10103;
  31013. ret = wc_VerifyEccsiHash(key, WC_HASH_TYPE_SHA256, NULL, 0, sig, 0,
  31014. &valid);
  31015. if (ret != BAD_FUNC_ARG)
  31016. return -10104;
  31017. ret = wc_VerifyEccsiHash(NULL, WC_HASH_TYPE_SHA256, data, 0, sig, 0,
  31018. &valid);
  31019. if (ret != BAD_FUNC_ARG)
  31020. return -10106;
  31021. ret = wc_VerifyEccsiHash(key, WC_HASH_TYPE_SHA256, data, 0, sig, 0,
  31022. &valid);
  31023. if (ret != BAD_STATE_E)
  31024. return -10106;
  31025. ret = wc_SetEccsiPair(key, ssk, pvt);
  31026. if (ret != 0)
  31027. return -10107;
  31028. /* Identity hash not set. */
  31029. ret = wc_SignEccsiHash(key, rng, WC_HASH_TYPE_SHA256, data, 0, NULL,
  31030. &sigSz);
  31031. if (ret != BAD_STATE_E)
  31032. return -10108;
  31033. wc_FreeEccsiKey(key);
  31034. return 0;
  31035. }
  31036. /* RFC 6507: Appendix A */
  31037. static int eccsi_kat_verify_test(EccsiKey* key, ecc_point* pvt)
  31038. {
  31039. int ret;
  31040. int verified;
  31041. const byte msg[] = { 0x6D, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x00 };
  31042. word32 msgSz = sizeof(msg);
  31043. byte hash[WC_SHA256_DIGEST_SIZE];
  31044. byte hashSz = WC_SHA256_DIGEST_SIZE;
  31045. static const byte id[] = {
  31046. 0x32, 0x30, 0x31, 0x31, 0x2D, 0x30, 0x32, 0x00,
  31047. 0x74, 0x65, 0x6C, 0x3A, 0x2B, 0x34, 0x34, 0x37,
  31048. 0x37, 0x30, 0x30, 0x39, 0x30, 0x30, 0x31, 0x32,
  31049. 0x33, 0x00
  31050. };
  31051. word32 idSz = sizeof(id);
  31052. static const byte sig[] = {
  31053. 0x26, 0x9D, 0x4C, 0x8F, 0xDE, 0xB6, 0x6A, 0x74,
  31054. 0xE4, 0xEF, 0x8C, 0x0D, 0x5D, 0xCC, 0x59, 0x7D,
  31055. 0xDF, 0xE6, 0x02, 0x9C, 0x2A, 0xFF, 0xC4, 0x93,
  31056. 0x60, 0x08, 0xCD, 0x2C, 0xC1, 0x04, 0x5D, 0x81,
  31057. 0xE0, 0x9B, 0x52, 0x8D, 0x0E, 0xF8, 0xD6, 0xDF,
  31058. 0x1A, 0xA3, 0xEC, 0xBF, 0x80, 0x11, 0x0C, 0xFC,
  31059. 0xEC, 0x9F, 0xC6, 0x82, 0x52, 0xCE, 0xBB, 0x67,
  31060. 0x9F, 0x41, 0x34, 0x84, 0x69, 0x40, 0xCC, 0xFD,
  31061. 0x04,
  31062. 0x75, 0x8A, 0x14, 0x27, 0x79, 0xBE, 0x89, 0xE8,
  31063. 0x29, 0xE7, 0x19, 0x84, 0xCB, 0x40, 0xEF, 0x75,
  31064. 0x8C, 0xC4, 0xAD, 0x77, 0x5F, 0xC5, 0xB9, 0xA3,
  31065. 0xE1, 0xC8, 0xED, 0x52, 0xF6, 0xFA, 0x36, 0xD9,
  31066. 0xA7, 0x9D, 0x24, 0x76, 0x92, 0xF4, 0xED, 0xA3,
  31067. 0xA6, 0xBD, 0xAB, 0x77, 0xD6, 0xAA, 0x64, 0x74,
  31068. 0xA4, 0x64, 0xAE, 0x49, 0x34, 0x66, 0x3C, 0x52,
  31069. 0x65, 0xBA, 0x70, 0x18, 0xBA, 0x09, 0x1F, 0x79
  31070. };
  31071. word32 sigSz = sizeof(sig);
  31072. static const byte pubData[] = {
  31073. 0x50, 0xD4, 0x67, 0x0B, 0xDE, 0x75, 0x24, 0x4F,
  31074. 0x28, 0xD2, 0x83, 0x8A, 0x0D, 0x25, 0x55, 0x8A,
  31075. 0x7A, 0x72, 0x68, 0x6D, 0x45, 0x22, 0xD4, 0xC8,
  31076. 0x27, 0x3F, 0xB6, 0x44, 0x2A, 0xEB, 0xFA, 0x93,
  31077. 0xDB, 0xDD, 0x37, 0x55, 0x1A, 0xFD, 0x26, 0x3B,
  31078. 0x5D, 0xFD, 0x61, 0x7F, 0x39, 0x60, 0xC6, 0x5A,
  31079. 0x8C, 0x29, 0x88, 0x50, 0xFF, 0x99, 0xF2, 0x03,
  31080. 0x66, 0xDC, 0xE7, 0xD4, 0x36, 0x72, 0x17, 0xF4
  31081. };
  31082. static const byte expHash[] = {
  31083. 0x49, 0x0f, 0x3f, 0xeb, 0xbc, 0x1c, 0x90, 0x2f,
  31084. 0x62, 0x89, 0x72, 0x3d, 0x7f, 0x8c, 0xbf, 0x79,
  31085. 0xdb, 0x88, 0x93, 0x08, 0x49, 0xd1, 0x9f, 0x38,
  31086. 0xf0, 0x29, 0x5b, 0x5c, 0x27, 0x6c, 0x14, 0xd1
  31087. };
  31088. ret = wc_ImportEccsiPublicKey(key, pubData, sizeof(pubData), 0);
  31089. if (ret != 0)
  31090. return -10108;
  31091. ret = wc_DecodeEccsiPvtFromSig(key, sig, sigSz, pvt);
  31092. if (ret != 0)
  31093. return -10109;
  31094. ret = wc_HashEccsiId(key, WC_HASH_TYPE_SHA256, id, idSz, pvt, hash,
  31095. &hashSz);
  31096. if (ret != 0)
  31097. return -10112;
  31098. if (hashSz != sizeof(expHash))
  31099. return -10113;
  31100. if (XMEMCMP(hash, expHash, hashSz) != 0)
  31101. return -10114;
  31102. ret = wc_SetEccsiHash(key, hash, hashSz);
  31103. if (ret != 0)
  31104. return -10112;
  31105. ret = wc_VerifyEccsiHash(key, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  31106. &verified);
  31107. if (ret != 0)
  31108. return -10115;
  31109. if (!verified)
  31110. return -10116;
  31111. return 0;
  31112. }
  31113. static int eccsi_enc_dec_pair_test(EccsiKey* priv, mp_int* ssk, ecc_point* pvt)
  31114. {
  31115. int ret;
  31116. byte data[32 * 3];
  31117. word32 sz;
  31118. ecc_point* decPvt = NULL;
  31119. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  31120. mp_int *decSsk = (mp_int *)XMALLOC(sizeof(*decSsk), HEAP_HINT,
  31121. DYNAMIC_TYPE_TMP_BUFFER);
  31122. if (decSsk == NULL)
  31123. return -10173;
  31124. #else
  31125. mp_int decSsk[1];
  31126. #endif
  31127. ret = mp_init(decSsk);
  31128. if (ret != 0)
  31129. ERROR_OUT(-10117, out);
  31130. decPvt = wc_ecc_new_point();
  31131. if (decPvt == NULL)
  31132. ERROR_OUT(-10118, out);
  31133. ret = wc_EncodeEccsiPair(priv, ssk, pvt, NULL, &sz);
  31134. if (ret != LENGTH_ONLY_E)
  31135. ERROR_OUT(-10119, out);
  31136. if (sz != 32 * 3)
  31137. ERROR_OUT(-10120, out);
  31138. ret = wc_EncodeEccsiPair(priv, ssk, pvt, data, &sz);
  31139. if (ret != 0)
  31140. ERROR_OUT(-10121, out);
  31141. if (sz != 32* 3)
  31142. ERROR_OUT(-10122, out);
  31143. ret = wc_DecodeEccsiPair(priv, data, sz, decSsk, decPvt);
  31144. if (ret != 0)
  31145. ERROR_OUT(-10123, out);
  31146. if (mp_cmp(ssk, decSsk) != MP_EQ)
  31147. ERROR_OUT(-10124, out);
  31148. if (wc_ecc_cmp_point(pvt, decPvt) != MP_EQ)
  31149. ERROR_OUT(-10125, out);
  31150. ret = wc_EncodeEccsiSsk(priv, ssk, NULL, &sz);
  31151. if (ret != LENGTH_ONLY_E)
  31152. ERROR_OUT(-10119, out);
  31153. if (sz != 32)
  31154. ERROR_OUT(-10120, out);
  31155. ret = wc_EncodeEccsiSsk(priv, ssk, data, &sz);
  31156. if (ret != 0)
  31157. ERROR_OUT(-10121, out);
  31158. if (sz != 32)
  31159. ERROR_OUT(-10122, out);
  31160. ret = wc_DecodeEccsiSsk(priv, data, sz, decSsk);
  31161. if (ret != 0)
  31162. ERROR_OUT(-10123, out);
  31163. if (mp_cmp(ssk, decSsk) != MP_EQ)
  31164. ERROR_OUT(-10124, out);
  31165. ret = wc_EncodeEccsiPvt(priv, pvt, NULL, &sz, 1);
  31166. if (ret != LENGTH_ONLY_E)
  31167. ERROR_OUT(-10126, out);
  31168. if (sz != 32 * 2)
  31169. ERROR_OUT(-10127, out);
  31170. ret = wc_EncodeEccsiPvt(priv, pvt, data, &sz, 1);
  31171. if (ret != 0)
  31172. ERROR_OUT(-10128, out);
  31173. if (sz != 32 * 2)
  31174. ERROR_OUT(-10129, out);
  31175. ret = wc_DecodeEccsiPvt(priv, data, sz, decPvt);
  31176. if (ret != 0)
  31177. ERROR_OUT(-10130, out);
  31178. if (wc_ecc_cmp_point(pvt, decPvt) != MP_EQ)
  31179. ERROR_OUT(-10131, out);
  31180. sz = sizeof(data);
  31181. ret = wc_EncodeEccsiPvt(priv, pvt, data, &sz, 0);
  31182. if (ret != 0)
  31183. ERROR_OUT(-10128, out);
  31184. if (sz != 32 * 2 + 1)
  31185. ERROR_OUT(-10129, out);
  31186. ret = wc_DecodeEccsiPvt(priv, data, sz, decPvt);
  31187. if (ret != 0)
  31188. ERROR_OUT(-10130, out);
  31189. if (wc_ecc_cmp_point(pvt, decPvt) != MP_EQ)
  31190. ERROR_OUT(-10131, out);
  31191. wc_ecc_del_point(decPvt);
  31192. out:
  31193. mp_free(decSsk);
  31194. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  31195. XFREE(decSsk, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  31196. #endif
  31197. return ret;
  31198. }
  31199. static int eccsi_imp_exp_key_test(EccsiKey* priv)
  31200. {
  31201. int ret;
  31202. byte data[32 * 3];
  31203. byte out[32 * 3];
  31204. word32 sz;
  31205. ret = wc_ExportEccsiKey(priv, NULL, &sz);
  31206. if (ret != LENGTH_ONLY_E)
  31207. return -10132;
  31208. if (sz != 32 * 3)
  31209. return -10133;
  31210. ret = wc_ExportEccsiKey(priv, data, &sz);
  31211. if (ret != 0)
  31212. return -10134;
  31213. ret = wc_ImportEccsiKey(priv, data, sz);
  31214. if (ret != 0)
  31215. return -10135;
  31216. ret = wc_ExportEccsiKey(priv, NULL, &sz);
  31217. if (ret != LENGTH_ONLY_E)
  31218. return -10132;
  31219. if (sz != 32 * 3)
  31220. return -10143;
  31221. ret = wc_ExportEccsiKey(priv, out, &sz);
  31222. if (ret != 0)
  31223. return -10144;
  31224. if (sz != 32 * 3)
  31225. return -10145;
  31226. if (XMEMCMP(data, out, sz) != 0)
  31227. return -10146;
  31228. ret = wc_ExportEccsiPrivateKey(priv, NULL, &sz);
  31229. if (ret != LENGTH_ONLY_E)
  31230. return -10156;
  31231. if (sz != 32)
  31232. return -10157;
  31233. ret = wc_ExportEccsiPrivateKey(priv, data, &sz);
  31234. if (ret != 0)
  31235. return -10158;
  31236. ret = wc_ImportEccsiPrivateKey(priv, data, sz);
  31237. if (ret != 0)
  31238. return -10159;
  31239. ret = wc_ExportEccsiPrivateKey(priv, NULL, &sz);
  31240. if (ret != LENGTH_ONLY_E)
  31241. return -10152;
  31242. if (sz != 32)
  31243. return -10163;
  31244. ret = wc_ExportEccsiPrivateKey(priv, out, &sz);
  31245. if (ret != 0)
  31246. return -10164;
  31247. if (sz != 32)
  31248. return -10165;
  31249. if (XMEMCMP(data, out, sz) != 0)
  31250. return -10166;
  31251. return 0;
  31252. }
  31253. static int eccsi_imp_exp_pubkey_test(EccsiKey* key1, EccsiKey* key2)
  31254. {
  31255. int ret;
  31256. byte data[32 * 2 + 1];
  31257. byte pubData[32 * 2 + 1];
  31258. word32 sz;
  31259. ret = wc_ExportEccsiPublicKey(key1, NULL, &sz, 1);
  31260. if (ret != LENGTH_ONLY_E)
  31261. return -10136;
  31262. if (sz != 32 * 2)
  31263. return -10137;
  31264. ret = wc_ExportEccsiPublicKey(key1, data, &sz, 1);
  31265. if (ret != 0)
  31266. return -10138;
  31267. ret = wc_ImportEccsiPublicKey(key2, data, sz, 1);
  31268. if (ret != 0)
  31269. return -10139;
  31270. sz = sizeof(pubData);
  31271. ret = wc_ExportEccsiPublicKey(key2, pubData, &sz, 1);
  31272. if (ret != 0)
  31273. return -10140;
  31274. if (sz != 32 * 2)
  31275. return -10141;
  31276. if (XMEMCMP(data, pubData, sz) != 0)
  31277. return -10142;
  31278. sz = sizeof(pubData);
  31279. ret = wc_ExportEccsiPublicKey(key2, pubData, &sz, 0);
  31280. if (ret != 0)
  31281. return -10140;
  31282. if (sz != 32 * 2 + 1)
  31283. return -10141;
  31284. if (pubData[0] != 0x04)
  31285. return -10140;
  31286. if (XMEMCMP(pubData + 1, data, sz - 1) != 0)
  31287. return -10142;
  31288. ret = wc_ImportEccsiPublicKey(key2, pubData, sz, 0);
  31289. if (ret != 0)
  31290. return -10139;
  31291. return 0;
  31292. }
  31293. static int eccsi_make_key_test(EccsiKey* priv, EccsiKey* pub, WC_RNG* rng,
  31294. mp_int* ssk, ecc_point* pvt)
  31295. {
  31296. int ret;
  31297. char mail[] = "test@wolfssl.com";
  31298. byte* id = (byte*)mail;
  31299. word32 idSz = (word32) XSTRLEN(mail);
  31300. int valid;
  31301. ret = wc_MakeEccsiKey(priv, rng);
  31302. if (ret != 0)
  31303. return -10143;
  31304. ret = eccsi_imp_exp_key_test(priv);
  31305. if (ret < 0)
  31306. return ret;
  31307. ret = eccsi_imp_exp_pubkey_test(priv, pub);
  31308. if (ret < 0)
  31309. return ret;
  31310. ret = wc_MakeEccsiPair(priv, rng, WC_HASH_TYPE_SHA256, id, idSz, ssk, pvt);
  31311. if (ret != 0)
  31312. return -10144;
  31313. ret = wc_ValidateEccsiPair(pub, WC_HASH_TYPE_SHA256, id, idSz, ssk, pvt,
  31314. &valid);
  31315. if (ret != 0)
  31316. return -10145;
  31317. if (!valid)
  31318. return -10146;
  31319. ret = eccsi_enc_dec_pair_test(priv, ssk, pvt);
  31320. if (ret != 0)
  31321. return ret;
  31322. return 0;
  31323. }
  31324. static int eccsi_sign_verify_test(EccsiKey* priv, EccsiKey* pub, WC_RNG* rng,
  31325. mp_int* ssk, ecc_point* pvt)
  31326. {
  31327. int ret;
  31328. byte hashPriv[WC_MAX_DIGEST_SIZE];
  31329. byte hashPub[WC_MAX_DIGEST_SIZE];
  31330. byte hashSz;
  31331. byte sig[144];
  31332. word32 sigSz;
  31333. int verified, valid;
  31334. char mail[] = "test@wolfssl.com";
  31335. byte* id = (byte*)mail;
  31336. word32 idSz = (word32) XSTRLEN(mail);
  31337. byte msg[] = { 0x00 };
  31338. word32 msgSz = sizeof(msg);
  31339. ret = wc_HashEccsiId(priv, WC_HASH_TYPE_SHA256, id, idSz, pvt, hashPriv,
  31340. &hashSz);
  31341. if (ret != 0)
  31342. return -10147;
  31343. if (hashSz != 32)
  31344. return -10148;
  31345. ret = wc_HashEccsiId(priv, WC_HASH_TYPE_SHA256, id, idSz, pvt, hashPub,
  31346. &hashSz);
  31347. if (ret != 0)
  31348. return -10149;
  31349. if (hashSz != 32)
  31350. return -10150;
  31351. if (XMEMCMP(hashPriv, hashPub, hashSz) != 0)
  31352. return -10151;
  31353. ret = wc_SetEccsiHash(priv, hashPriv, hashSz);
  31354. if (ret != 0)
  31355. return -10149;
  31356. ret = wc_SetEccsiPair(priv, ssk, pvt);
  31357. if (ret != 0)
  31358. return -10149;
  31359. ret = wc_SignEccsiHash(priv, rng, WC_HASH_TYPE_SHA256, msg, msgSz, NULL,
  31360. &sigSz);
  31361. if (ret != LENGTH_ONLY_E)
  31362. return -10152;
  31363. if (sigSz != 129)
  31364. return -10153;
  31365. ret = wc_SignEccsiHash(priv, rng, WC_HASH_TYPE_SHA256, msg, msgSz, sig,
  31366. &sigSz);
  31367. if (ret != 0)
  31368. return -10154;
  31369. ret = wc_SetEccsiHash(pub, hashPub, hashSz);
  31370. if (ret != 0)
  31371. return -10149;
  31372. ret = wc_VerifyEccsiHash(pub, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  31373. &verified);
  31374. if (ret != 0)
  31375. return -10155;
  31376. if (!verified)
  31377. return -10156;
  31378. /* Check that changing HS results in verification failure. */
  31379. hashPub[0] ^= 0x80;
  31380. ret = wc_SetEccsiHash(pub, hashPub, hashSz);
  31381. if (ret != 0)
  31382. return -10149;
  31383. ret = wc_VerifyEccsiHash(pub, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  31384. &verified);
  31385. if (ret != 0)
  31386. return -10157;
  31387. if (verified)
  31388. return -10158;
  31389. hashPub[0] ^= 0x80;
  31390. ret = wc_SetEccsiHash(pub, hashPub, hashSz);
  31391. if (ret != 0)
  31392. return -10149;
  31393. /* Check that changing msg results in verification failure. */
  31394. msg[0] ^= 0x80;
  31395. ret = wc_VerifyEccsiHash(pub, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  31396. &verified);
  31397. if (ret != 0)
  31398. return -10159;
  31399. if (verified)
  31400. return -10160;
  31401. msg[0] ^= 0x80;
  31402. /* Check that changing signature results in verification failure. */
  31403. sig[0] ^= 0x80;
  31404. ret = wc_VerifyEccsiHash(pub, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  31405. &verified);
  31406. if (ret != 0)
  31407. return -10161;
  31408. if (verified)
  31409. return -10162;
  31410. sig[0] ^= 0x80;
  31411. /* Check that key state hasn't been invalidated. */
  31412. ret = wc_VerifyEccsiHash(pub, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  31413. &verified);
  31414. if (ret != 0)
  31415. return -10163;
  31416. if (!verified)
  31417. return -10164;
  31418. /* Check that verifying with the private key works. */
  31419. ret = wc_VerifyEccsiHash(priv, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  31420. &verified);
  31421. if (ret != 0)
  31422. return -10165;
  31423. if (!verified)
  31424. return -10166;
  31425. /* Check that the KPAK is converted from montgomery form. */
  31426. ret = eccsi_imp_exp_key_test(priv);
  31427. if (ret != 0)
  31428. return ret;
  31429. /* Check that KPAK can converted to Montgomery form again. */
  31430. ret = wc_VerifyEccsiHash(priv, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  31431. &verified);
  31432. if (ret != 0)
  31433. return -10167;
  31434. if (!verified)
  31435. return -10168;
  31436. /* Check that the KPAK is converted from montgomery form. */
  31437. ret = wc_ValidateEccsiPair(pub, WC_HASH_TYPE_SHA256, id, idSz, ssk, pvt,
  31438. &valid);
  31439. if (ret != 0)
  31440. return -10169;
  31441. if (!valid)
  31442. return -10170;
  31443. /* Check that KPAK can converted to Montgomery form again. */
  31444. ret = wc_VerifyEccsiHash(priv, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  31445. &verified);
  31446. if (ret != 0)
  31447. return -10171;
  31448. if (!verified)
  31449. return -10172;
  31450. /* Check that the KPAK is converted from montgomery form. */
  31451. ret = eccsi_imp_exp_pubkey_test(priv, pub);
  31452. if (ret != 0)
  31453. return ret;
  31454. return 0;
  31455. }
  31456. int eccsi_test(void)
  31457. {
  31458. int ret = 0;
  31459. WC_RNG rng;
  31460. int rng_inited = 0;
  31461. EccsiKey* priv = NULL;
  31462. EccsiKey* pub = NULL;
  31463. mp_int* ssk = NULL;
  31464. ecc_point* pvt = NULL;
  31465. priv = (EccsiKey*)XMALLOC(sizeof(EccsiKey), HEAP_HINT,
  31466. DYNAMIC_TYPE_TMP_BUFFER);
  31467. if (priv == NULL)
  31468. ret = -10205;
  31469. else
  31470. XMEMSET(priv, 0, sizeof(*priv));
  31471. if (ret == 0) {
  31472. pub = (EccsiKey*)XMALLOC(sizeof(EccsiKey), HEAP_HINT,
  31473. DYNAMIC_TYPE_TMP_BUFFER);
  31474. if (pub == NULL)
  31475. ret = -10206;
  31476. else
  31477. XMEMSET(pub, 0, sizeof(*pub));
  31478. }
  31479. if (ret == 0) {
  31480. ssk = (mp_int*)XMALLOC(sizeof(mp_int), HEAP_HINT,
  31481. DYNAMIC_TYPE_TMP_BUFFER);
  31482. if (ssk == NULL)
  31483. ret = -10207;
  31484. else
  31485. XMEMSET(ssk, 0, sizeof(*ssk));
  31486. }
  31487. if (ret == 0) {
  31488. #ifndef HAVE_FIPS
  31489. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  31490. #else
  31491. ret = wc_InitRng(&rng);
  31492. #endif
  31493. if (ret != 0)
  31494. ret = -10200;
  31495. else
  31496. rng_inited = 1;
  31497. }
  31498. if (ret == 0) {
  31499. pvt = wc_ecc_new_point();
  31500. if (pvt == NULL)
  31501. ret = -10201;
  31502. }
  31503. if (ret == 0) {
  31504. ret = mp_init(ssk);
  31505. if (ret != 0)
  31506. ret = -10202;
  31507. }
  31508. if (ret == 0) {
  31509. ret = eccsi_api_test(&rng, priv, ssk, pvt);
  31510. }
  31511. if (ret == 0) {
  31512. ret = wc_InitEccsiKey(pub, HEAP_HINT, INVALID_DEVID);
  31513. if (ret != 0)
  31514. ret = -10203;
  31515. }
  31516. if (ret == 0) {
  31517. ret = wc_InitEccsiKey(priv, HEAP_HINT, INVALID_DEVID);
  31518. if (ret != 0)
  31519. ret = -10204;
  31520. }
  31521. if (ret == 0) {
  31522. ret = eccsi_kat_verify_test(pub, pvt);
  31523. }
  31524. if (ret == 0) {
  31525. ret = eccsi_make_key_test(priv, pub, &rng, ssk, pvt);
  31526. }
  31527. if (ret == 0) {
  31528. ret = eccsi_sign_verify_test(priv, pub, &rng, ssk, pvt);
  31529. }
  31530. if (pvt != NULL)
  31531. wc_ecc_del_point(pvt);
  31532. if (rng_inited)
  31533. wc_FreeRng(&rng);
  31534. if (ssk != NULL) {
  31535. mp_free(ssk);
  31536. XFREE(ssk, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  31537. }
  31538. if (pub != NULL) {
  31539. wc_FreeEccsiKey(pub);
  31540. XFREE(pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  31541. }
  31542. if (priv != NULL) {
  31543. wc_FreeEccsiKey(priv);
  31544. XFREE(priv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  31545. }
  31546. return ret;
  31547. }
  31548. #endif /* WOLFCRYPT_HAVE_ECCSI */
  31549. #ifdef WOLFCRYPT_HAVE_SAKKE
  31550. static int sakke_api_test(WC_RNG* rng, SakkeKey* key, ecc_point* rsk)
  31551. {
  31552. int ret;
  31553. byte id[1] = { 0x00 };
  31554. int valid;
  31555. byte data[256];
  31556. word32 sz;
  31557. byte auth[257];
  31558. word16 authSz;
  31559. byte ssv[256];
  31560. word16 ssvSz;
  31561. word32 len;
  31562. ret = wc_InitSakkeKey_ex(NULL, 128, ECC_SAKKE_1, NULL, INVALID_DEVID);
  31563. if (ret != BAD_FUNC_ARG)
  31564. return -10205;
  31565. ret = wc_InitSakkeKey_ex(NULL, 128, ECC_SAKKE_1, HEAP_HINT, INVALID_DEVID);
  31566. if (ret != BAD_FUNC_ARG)
  31567. return -10206;
  31568. wc_FreeSakkeKey(NULL);
  31569. XMEMSET(key, 0, sizeof(*key));
  31570. wc_FreeSakkeKey(key);
  31571. ret = wc_InitSakkeKey_ex(key, 128, ECC_SAKKE_1, HEAP_HINT, INVALID_DEVID);
  31572. if (ret != 0)
  31573. return -10207;
  31574. ret = wc_MakeSakkeKey(NULL, NULL);
  31575. if (ret != BAD_FUNC_ARG)
  31576. return -10208;
  31577. ret = wc_MakeSakkeKey(key, NULL);
  31578. if (ret != BAD_FUNC_ARG)
  31579. return -10209;
  31580. ret = wc_MakeSakkeKey(NULL, rng);
  31581. if (ret != BAD_FUNC_ARG)
  31582. return -10210;
  31583. ret = wc_MakeSakkePublicKey(NULL, NULL);
  31584. if (ret != BAD_FUNC_ARG)
  31585. return -10211;
  31586. ret = wc_MakeSakkePublicKey(key, NULL);
  31587. if (ret != BAD_FUNC_ARG)
  31588. return -10212;
  31589. ret = wc_MakeSakkePublicKey(NULL, rsk);
  31590. if (ret != BAD_FUNC_ARG)
  31591. return -10213;
  31592. ret = wc_MakeSakkeRsk(NULL, NULL, 1, NULL);
  31593. if (ret != BAD_FUNC_ARG)
  31594. return -10214;
  31595. ret = wc_MakeSakkeRsk(key, id, 1, NULL);
  31596. if (ret != BAD_FUNC_ARG)
  31597. return -10215;
  31598. ret = wc_MakeSakkeRsk(key, NULL, 1, rsk);
  31599. if (ret != BAD_FUNC_ARG)
  31600. return -10216;
  31601. ret = wc_MakeSakkeRsk(NULL, id, 1, rsk);
  31602. if (ret != BAD_FUNC_ARG)
  31603. return -10217;
  31604. ret = wc_ValidateSakkeRsk(NULL, NULL, 1, NULL, NULL);
  31605. if (ret != BAD_FUNC_ARG)
  31606. return -10218;
  31607. ret = wc_ValidateSakkeRsk(key, id, 1, rsk, NULL);
  31608. if (ret != BAD_FUNC_ARG)
  31609. return -10219;
  31610. ret = wc_ValidateSakkeRsk(NULL, id, 1, rsk, &valid);
  31611. if (ret != BAD_FUNC_ARG)
  31612. return -10220;
  31613. ret = wc_ExportSakkeKey(NULL, NULL, NULL);
  31614. if (ret != BAD_FUNC_ARG)
  31615. return -10221;
  31616. ret = wc_ExportSakkeKey(key, data, NULL);
  31617. if (ret != BAD_FUNC_ARG)
  31618. return -10222;
  31619. ret = wc_ExportSakkeKey(NULL, data, &sz);
  31620. if (ret != BAD_FUNC_ARG)
  31621. return -10223;
  31622. ret = wc_ImportSakkeKey(NULL, NULL, 1);
  31623. if (ret != BAD_FUNC_ARG)
  31624. return -10224;
  31625. ret = wc_ImportSakkeKey(key, NULL, 1);
  31626. if (ret != BAD_FUNC_ARG)
  31627. return -10225;
  31628. ret = wc_ImportSakkeKey(NULL, data, 1);
  31629. if (ret != BAD_FUNC_ARG)
  31630. return -10226;
  31631. ret = wc_ExportSakkePrivateKey(NULL, NULL, NULL);
  31632. if (ret != BAD_FUNC_ARG)
  31633. return -10227;
  31634. ret = wc_ExportSakkePrivateKey(key, data, NULL);
  31635. if (ret != BAD_FUNC_ARG)
  31636. return -10228;
  31637. ret = wc_ExportSakkePrivateKey(NULL, data, &sz);
  31638. if (ret != BAD_FUNC_ARG)
  31639. return -10229;
  31640. ret = wc_ImportSakkePrivateKey(NULL, NULL, 1);
  31641. if (ret != BAD_FUNC_ARG)
  31642. return -10230;
  31643. ret = wc_ImportSakkePrivateKey(key, NULL, 1);
  31644. if (ret != BAD_FUNC_ARG)
  31645. return -10231;
  31646. ret = wc_ImportSakkePrivateKey(NULL, data, 1);
  31647. if (ret != BAD_FUNC_ARG)
  31648. return -10232;
  31649. sz = sizeof(data);
  31650. ret = wc_EncodeSakkeRsk(NULL, NULL, data, NULL, 1);
  31651. if (ret != BAD_FUNC_ARG)
  31652. return -10233;
  31653. ret = wc_EncodeSakkeRsk(key, rsk, data, NULL, 1);
  31654. if (ret != BAD_FUNC_ARG)
  31655. return -10234;
  31656. ret = wc_EncodeSakkeRsk(key, NULL, data, &sz, 1);
  31657. if (ret != BAD_FUNC_ARG)
  31658. return -10235;
  31659. ret = wc_EncodeSakkeRsk(NULL, rsk, data, &sz, 1);
  31660. if (ret != BAD_FUNC_ARG)
  31661. return -10236;
  31662. ret = wc_DecodeSakkeRsk(NULL, NULL, sz, NULL);
  31663. if (ret != BAD_FUNC_ARG)
  31664. return -10237;
  31665. ret = wc_DecodeSakkeRsk(key, data, sz, NULL);
  31666. if (ret != BAD_FUNC_ARG)
  31667. return -10238;
  31668. ret = wc_DecodeSakkeRsk(key, NULL, sz, rsk);
  31669. if (ret != BAD_FUNC_ARG)
  31670. return -10239;
  31671. ret = wc_DecodeSakkeRsk(NULL, data, sz, rsk);
  31672. if (ret != BAD_FUNC_ARG)
  31673. return -10240;
  31674. ret = wc_ImportSakkeRsk(NULL, NULL, sz);
  31675. if (ret != BAD_FUNC_ARG)
  31676. return -10237;
  31677. ret = wc_ImportSakkeRsk(key, NULL, sz);
  31678. if (ret != BAD_FUNC_ARG)
  31679. return -10237;
  31680. ret = wc_ImportSakkeRsk(NULL, data, sz);
  31681. if (ret != BAD_FUNC_ARG)
  31682. return -10237;
  31683. ret = wc_ImportSakkeRsk(key, data, 1);
  31684. if (ret != BUFFER_E)
  31685. return -10237;
  31686. ret = wc_GenerateSakkeRskTable(NULL, NULL, data, NULL);
  31687. if (ret != BAD_FUNC_ARG)
  31688. return -10241;
  31689. ret = wc_GenerateSakkeRskTable(key, NULL, data, NULL);
  31690. if (ret != BAD_FUNC_ARG)
  31691. return -10242;
  31692. ret = wc_GenerateSakkeRskTable(NULL, rsk, data, NULL);
  31693. if (ret != BAD_FUNC_ARG)
  31694. return -10243;
  31695. ret = wc_GenerateSakkeRskTable(NULL, NULL, data, &len);
  31696. if (ret != BAD_FUNC_ARG)
  31697. return -10244;
  31698. ret = wc_GenerateSakkeRskTable(key, rsk, data, NULL);
  31699. if (ret != BAD_FUNC_ARG)
  31700. return -10245;
  31701. ret = wc_GenerateSakkeRskTable(key, NULL, data, &len);
  31702. if (ret != BAD_FUNC_ARG)
  31703. return -10246;
  31704. ret = wc_GenerateSakkeRskTable(NULL, rsk, data, &len);
  31705. if (ret != BAD_FUNC_ARG)
  31706. return -10247;
  31707. ret = wc_GenerateSakkeRskTable(key, rsk, NULL, &len);
  31708. if (ret != LENGTH_ONLY_E)
  31709. return -10248;
  31710. len--;
  31711. ret = wc_GenerateSakkeRskTable(key, rsk, data, &len);
  31712. if (ret != BUFFER_E)
  31713. return -10249;
  31714. ret = wc_ExportSakkePublicKey(NULL, data, NULL, 1);
  31715. if (ret != BAD_FUNC_ARG)
  31716. return -10250;
  31717. ret = wc_ExportSakkePublicKey(key, data, NULL, 1);
  31718. if (ret != BAD_FUNC_ARG)
  31719. return -10251;
  31720. ret = wc_ExportSakkePublicKey(NULL, data, &sz, 1);
  31721. if (ret != BAD_FUNC_ARG)
  31722. return -10252;
  31723. ret = wc_ImportSakkePublicKey(NULL, NULL, sz, 1);
  31724. if (ret != BAD_FUNC_ARG)
  31725. return -10253;
  31726. ret = wc_ImportSakkePublicKey(key, NULL, sz, 1);
  31727. if (ret != BAD_FUNC_ARG)
  31728. return -10254;
  31729. ret = wc_ImportSakkePublicKey(NULL, data, sz, 1);
  31730. if (ret != BAD_FUNC_ARG)
  31731. return -10255;
  31732. ret = wc_GetSakkeAuthSize(NULL, NULL);
  31733. if (ret != BAD_FUNC_ARG)
  31734. return -10256;
  31735. ret = wc_GetSakkeAuthSize(key, NULL);
  31736. if (ret != BAD_FUNC_ARG)
  31737. return -10257;
  31738. ret = wc_GetSakkeAuthSize(NULL, &authSz);
  31739. if (ret != BAD_FUNC_ARG)
  31740. return -10258;
  31741. ret = wc_MakeSakkePointI(NULL, NULL, SAKKE_ID_MAX_SIZE + 1);
  31742. if (ret != BAD_FUNC_ARG)
  31743. return -10259;
  31744. ret = wc_MakeSakkePointI(key, NULL, SAKKE_ID_MAX_SIZE + 1);
  31745. if (ret != BAD_FUNC_ARG)
  31746. return -10260;
  31747. ret = wc_MakeSakkePointI(NULL, id, 1);
  31748. if (ret != BAD_FUNC_ARG)
  31749. return -10261;
  31750. ret = wc_MakeSakkePointI(NULL, NULL, 1);
  31751. if (ret != BAD_FUNC_ARG)
  31752. return -10262;
  31753. ret = wc_MakeSakkePointI(key, id, SAKKE_ID_MAX_SIZE + 1);
  31754. if (ret != BAD_FUNC_ARG)
  31755. return -10263;
  31756. ret = wc_MakeSakkePointI(key, NULL, 1);
  31757. if (ret != BAD_FUNC_ARG)
  31758. return -10264;
  31759. ret = wc_MakeSakkePointI(NULL, id, 1);
  31760. if (ret != BAD_FUNC_ARG)
  31761. return -10265;
  31762. ret = wc_GenerateSakkePointITable(NULL, data, NULL);
  31763. if (ret != BAD_FUNC_ARG)
  31764. return -10266;
  31765. ret = wc_GenerateSakkePointITable(key, data, NULL);
  31766. if (ret != BAD_FUNC_ARG)
  31767. return -10267;
  31768. ret = wc_GenerateSakkePointITable(NULL, data, &len);
  31769. if (ret != BAD_FUNC_ARG)
  31770. return -10268;
  31771. ret = wc_GenerateSakkePointITable(key, NULL, &len);
  31772. if (ret != LENGTH_ONLY_E)
  31773. return -10269;
  31774. len--;
  31775. ret = wc_GenerateSakkePointITable(key, data, &len);
  31776. if (ret != BUFFER_E)
  31777. return -10270;
  31778. ret = wc_SetSakkePointITable(NULL, NULL, 1);
  31779. if (ret != BAD_FUNC_ARG)
  31780. return -10271;
  31781. ret = wc_SetSakkePointITable(key, NULL, 1);
  31782. if (ret != BAD_FUNC_ARG)
  31783. return -10272;
  31784. ret = wc_SetSakkePointITable(NULL, data, 1);
  31785. if (ret != BAD_FUNC_ARG)
  31786. return -10273;
  31787. ret = wc_SetSakkePointITable(key, data, 1);
  31788. if (ret != BUFFER_E)
  31789. return -10274;
  31790. ret = wc_ClearSakkePointITable(NULL);
  31791. if (ret != BAD_FUNC_ARG)
  31792. return -10275;
  31793. ret = wc_GetSakkePointI(NULL, data, NULL);
  31794. if (ret != BAD_FUNC_ARG)
  31795. return -10276;
  31796. ret = wc_GetSakkePointI(key, data, NULL);
  31797. if (ret != BAD_FUNC_ARG)
  31798. return -10277;
  31799. ret = wc_GetSakkePointI(NULL, data, &sz);
  31800. if (ret != BAD_FUNC_ARG)
  31801. return -10278;
  31802. sz = 1;
  31803. ret = wc_GetSakkePointI(key, data, &sz);
  31804. if (ret != BUFFER_E)
  31805. return -10279;
  31806. sz = 256;
  31807. ret = wc_SetSakkePointI(NULL, NULL, 1, NULL, sz);
  31808. if (ret != BAD_FUNC_ARG)
  31809. return -10280;
  31810. ret = wc_SetSakkePointI(key, NULL, 1, NULL, sz);
  31811. if (ret != BAD_FUNC_ARG)
  31812. return -10281;
  31813. ret = wc_SetSakkePointI(NULL, id, 1, NULL, sz);
  31814. if (ret != BAD_FUNC_ARG)
  31815. return -10282;
  31816. ret = wc_SetSakkePointI(NULL, NULL, 1, data, sz);
  31817. if (ret != BAD_FUNC_ARG)
  31818. return -10283;
  31819. ret = wc_SetSakkePointI(key, id, 1, NULL, sz);
  31820. if (ret != BAD_FUNC_ARG)
  31821. return -10284;
  31822. ret = wc_SetSakkePointI(key, NULL, 1, data, sz);
  31823. if (ret != BAD_FUNC_ARG)
  31824. return -10285;
  31825. ret = wc_SetSakkePointI(NULL, id, 1, data, sz);
  31826. if (ret != BAD_FUNC_ARG)
  31827. return -10286;
  31828. ret = wc_SetSakkePointI(key, id, SAKKE_ID_MAX_SIZE + 1, data, sz);
  31829. if (ret != BUFFER_E)
  31830. return -10287;
  31831. ret = wc_SetSakkePointI(key, id, 1, data, sz - 1);
  31832. if (ret != BUFFER_E)
  31833. return -10288;
  31834. ret = wc_SetSakkeIdentity(NULL, NULL, 1);
  31835. if (ret != BAD_FUNC_ARG)
  31836. return -10286;
  31837. ret = wc_SetSakkeIdentity(key, NULL, 1);
  31838. if (ret != BAD_FUNC_ARG)
  31839. return -10286;
  31840. ret = wc_SetSakkeIdentity(NULL, id, 1);
  31841. if (ret != BAD_FUNC_ARG)
  31842. return -10286;
  31843. ssvSz = sizeof(ssv);
  31844. ret = wc_MakeSakkeEncapsulatedSSV(NULL, WC_HASH_TYPE_SHA256, NULL, ssvSz,
  31845. auth, NULL);
  31846. if (ret != BAD_FUNC_ARG)
  31847. return -10289;
  31848. ret = wc_MakeSakkeEncapsulatedSSV(key, WC_HASH_TYPE_SHA256, NULL, ssvSz,
  31849. auth, NULL);
  31850. if (ret != BAD_FUNC_ARG)
  31851. return -10290;
  31852. ret = wc_MakeSakkeEncapsulatedSSV(NULL, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  31853. auth, NULL);
  31854. if (ret != BAD_FUNC_ARG)
  31855. return -10291;
  31856. ret = wc_MakeSakkeEncapsulatedSSV(NULL, WC_HASH_TYPE_SHA256, NULL, ssvSz,
  31857. auth, &authSz);
  31858. if (ret != BAD_FUNC_ARG)
  31859. return -10292;
  31860. ret = wc_MakeSakkeEncapsulatedSSV(key, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  31861. auth, NULL);
  31862. if (ret != BAD_FUNC_ARG)
  31863. return -10293;
  31864. ret = wc_MakeSakkeEncapsulatedSSV(key, WC_HASH_TYPE_SHA256, NULL, ssvSz,
  31865. auth, &authSz);
  31866. if (ret != BAD_FUNC_ARG)
  31867. return -10294;
  31868. ret = wc_MakeSakkeEncapsulatedSSV(NULL, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  31869. auth, &authSz);
  31870. if (ret != BAD_FUNC_ARG)
  31871. return -10295;
  31872. ret = wc_MakeSakkeEncapsulatedSSV(key, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  31873. auth, &authSz);
  31874. if (ret != BAD_STATE_E)
  31875. return -10295;
  31876. ret = wc_GenerateSakkeSSV(NULL, NULL, data, NULL);
  31877. if (ret != BAD_FUNC_ARG)
  31878. return -10296;
  31879. ret = wc_GenerateSakkeSSV(key, rng, data, NULL);
  31880. if (ret != BAD_FUNC_ARG)
  31881. return -10297;
  31882. ret = wc_GenerateSakkeSSV(key, NULL, data, &ssvSz);
  31883. if (ret != BAD_FUNC_ARG)
  31884. return -10298;
  31885. ret = wc_GenerateSakkeSSV(NULL, rng, data, &ssvSz);
  31886. if (ret != BAD_FUNC_ARG)
  31887. return -10299;
  31888. ret = wc_SetSakkeRsk(NULL, NULL, data, 1);
  31889. if (ret != BAD_FUNC_ARG)
  31890. return -10286;
  31891. ret = wc_SetSakkeRsk(key, NULL, data, 1);
  31892. if (ret != BAD_FUNC_ARG)
  31893. return -10286;
  31894. ret = wc_SetSakkeRsk(NULL, rsk, data, 1);
  31895. if (ret != BAD_FUNC_ARG)
  31896. return -10286;
  31897. ssvSz = sizeof(ssv);
  31898. authSz = sizeof(auth);
  31899. ret = wc_DeriveSakkeSSV(NULL, WC_HASH_TYPE_SHA256, NULL, ssvSz, NULL,
  31900. authSz);
  31901. if (ret != BAD_FUNC_ARG)
  31902. return -10300;
  31903. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, NULL, ssvSz, NULL,
  31904. authSz);
  31905. if (ret != BAD_FUNC_ARG)
  31906. return -10300;
  31907. ret = wc_DeriveSakkeSSV(NULL, WC_HASH_TYPE_SHA256, ssv, ssvSz, NULL,
  31908. authSz);
  31909. if (ret != BAD_FUNC_ARG)
  31910. return -10300;
  31911. ret = wc_DeriveSakkeSSV(NULL, WC_HASH_TYPE_SHA256, NULL, ssvSz, auth,
  31912. authSz);
  31913. if (ret != BAD_FUNC_ARG)
  31914. return -10300;
  31915. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, ssv, ssvSz, NULL,
  31916. authSz);
  31917. if (ret != BAD_FUNC_ARG)
  31918. return -10300;
  31919. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, NULL, ssvSz, auth,
  31920. authSz);
  31921. if (ret != BAD_FUNC_ARG)
  31922. return -10300;
  31923. ret = wc_DeriveSakkeSSV(NULL, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  31924. authSz);
  31925. if (ret != BAD_FUNC_ARG)
  31926. return -10300;
  31927. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  31928. authSz);
  31929. if (ret != BAD_STATE_E)
  31930. return -10300;
  31931. ret = wc_SetSakkeIdentity(key, id, 1);
  31932. if (ret != 0)
  31933. return -10286;
  31934. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  31935. authSz);
  31936. if (ret != BAD_STATE_E)
  31937. return -10300;
  31938. ret = wc_SetSakkeIdentity(key, id, 0);
  31939. if (ret != 0)
  31940. return -10286;
  31941. ret = wc_SetSakkeRsk(key, rsk, data, 1);
  31942. if (ret != 0)
  31943. return -10286;
  31944. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  31945. authSz);
  31946. if (ret != BAD_STATE_E)
  31947. return -10300;
  31948. wc_FreeSakkeKey(key);
  31949. return 0;
  31950. }
  31951. static int sakke_kat_derive_test(SakkeKey* key, ecc_point* rsk)
  31952. {
  31953. WOLFSSL_SMALL_STACK_STATIC const byte pubData[] = {
  31954. 0x59, 0x58, 0xEF, 0x1B, 0x16, 0x79, 0xBF, 0x09,
  31955. 0x9B, 0x3A, 0x03, 0x0D, 0xF2, 0x55, 0xAA, 0x6A,
  31956. 0x23, 0xC1, 0xD8, 0xF1, 0x43, 0xD4, 0xD2, 0x3F,
  31957. 0x75, 0x3E, 0x69, 0xBD, 0x27, 0xA8, 0x32, 0xF3,
  31958. 0x8C, 0xB4, 0xAD, 0x53, 0xDD, 0xEF, 0x42, 0x60,
  31959. 0xB0, 0xFE, 0x8B, 0xB4, 0x5C, 0x4C, 0x1F, 0xF5,
  31960. 0x10, 0xEF, 0xFE, 0x30, 0x03, 0x67, 0xA3, 0x7B,
  31961. 0x61, 0xF7, 0x01, 0xD9, 0x14, 0xAE, 0xF0, 0x97,
  31962. 0x24, 0x82, 0x5F, 0xA0, 0x70, 0x7D, 0x61, 0xA6,
  31963. 0xDF, 0xF4, 0xFB, 0xD7, 0x27, 0x35, 0x66, 0xCD,
  31964. 0xDE, 0x35, 0x2A, 0x0B, 0x04, 0xB7, 0xC1, 0x6A,
  31965. 0x78, 0x30, 0x9B, 0xE6, 0x40, 0x69, 0x7D, 0xE7,
  31966. 0x47, 0x61, 0x3A, 0x5F, 0xC1, 0x95, 0xE8, 0xB9,
  31967. 0xF3, 0x28, 0x85, 0x2A, 0x57, 0x9D, 0xB8, 0xF9,
  31968. 0x9B, 0x1D, 0x00, 0x34, 0x47, 0x9E, 0xA9, 0xC5,
  31969. 0x59, 0x5F, 0x47, 0xC4, 0xB2, 0xF5, 0x4F, 0xF2,
  31970. 0x15, 0x08, 0xD3, 0x75, 0x14, 0xDC, 0xF7, 0xA8,
  31971. 0xE1, 0x43, 0xA6, 0x05, 0x8C, 0x09, 0xA6, 0xBF,
  31972. 0x2C, 0x98, 0x58, 0xCA, 0x37, 0xC2, 0x58, 0x06,
  31973. 0x5A, 0xE6, 0xBF, 0x75, 0x32, 0xBC, 0x8B, 0x5B,
  31974. 0x63, 0x38, 0x38, 0x66, 0xE0, 0x75, 0x3C, 0x5A,
  31975. 0xC0, 0xE7, 0x27, 0x09, 0xF8, 0x44, 0x5F, 0x2E,
  31976. 0x61, 0x78, 0xE0, 0x65, 0x85, 0x7E, 0x0E, 0xDA,
  31977. 0x10, 0xF6, 0x82, 0x06, 0xB6, 0x35, 0x05, 0xED,
  31978. 0x87, 0xE5, 0x34, 0xFB, 0x28, 0x31, 0xFF, 0x95,
  31979. 0x7F, 0xB7, 0xDC, 0x61, 0x9D, 0xAE, 0x61, 0x30,
  31980. 0x1E, 0xEA, 0xCC, 0x2F, 0xDA, 0x36, 0x80, 0xEA,
  31981. 0x49, 0x99, 0x25, 0x8A, 0x83, 0x3C, 0xEA, 0x8F,
  31982. 0xC6, 0x7C, 0x6D, 0x19, 0x48, 0x7F, 0xB4, 0x49,
  31983. 0x05, 0x9F, 0x26, 0xCC, 0x8A, 0xAB, 0x65, 0x5A,
  31984. 0xB5, 0x8B, 0x7C, 0xC7, 0x96, 0xE2, 0x4E, 0x9A,
  31985. 0x39, 0x40, 0x95, 0x75, 0x4F, 0x5F, 0x8B, 0xAE
  31986. };
  31987. WOLFSSL_SMALL_STACK_STATIC const byte rskData[] = {
  31988. 0x93, 0xAF, 0x67, 0xE5, 0x00, 0x7B, 0xA6, 0xE6,
  31989. 0xA8, 0x0D, 0xA7, 0x93, 0xDA, 0x30, 0x0F, 0xA4,
  31990. 0xB5, 0x2D, 0x0A, 0x74, 0xE2, 0x5E, 0x6E, 0x7B,
  31991. 0x2B, 0x3D, 0x6E, 0xE9, 0xD1, 0x8A, 0x9B, 0x5C,
  31992. 0x50, 0x23, 0x59, 0x7B, 0xD8, 0x2D, 0x80, 0x62,
  31993. 0xD3, 0x40, 0x19, 0x56, 0x3B, 0xA1, 0xD2, 0x5C,
  31994. 0x0D, 0xC5, 0x6B, 0x7B, 0x97, 0x9D, 0x74, 0xAA,
  31995. 0x50, 0xF2, 0x9F, 0xBF, 0x11, 0xCC, 0x2C, 0x93,
  31996. 0xF5, 0xDF, 0xCA, 0x61, 0x5E, 0x60, 0x92, 0x79,
  31997. 0xF6, 0x17, 0x5C, 0xEA, 0xDB, 0x00, 0xB5, 0x8C,
  31998. 0x6B, 0xEE, 0x1E, 0x7A, 0x2A, 0x47, 0xC4, 0xF0,
  31999. 0xC4, 0x56, 0xF0, 0x52, 0x59, 0xA6, 0xFA, 0x94,
  32000. 0xA6, 0x34, 0xA4, 0x0D, 0xAE, 0x1D, 0xF5, 0x93,
  32001. 0xD4, 0xFE, 0xCF, 0x68, 0x8D, 0x5F, 0xC6, 0x78,
  32002. 0xBE, 0x7E, 0xFC, 0x6D, 0xF3, 0xD6, 0x83, 0x53,
  32003. 0x25, 0xB8, 0x3B, 0x2C, 0x6E, 0x69, 0x03, 0x6B,
  32004. 0x15, 0x5F, 0x0A, 0x27, 0x24, 0x10, 0x94, 0xB0,
  32005. 0x4B, 0xFB, 0x0B, 0xDF, 0xAC, 0x6C, 0x67, 0x0A,
  32006. 0x65, 0xC3, 0x25, 0xD3, 0x9A, 0x06, 0x9F, 0x03,
  32007. 0x65, 0x9D, 0x44, 0xCA, 0x27, 0xD3, 0xBE, 0x8D,
  32008. 0xF3, 0x11, 0x17, 0x2B, 0x55, 0x41, 0x60, 0x18,
  32009. 0x1C, 0xBE, 0x94, 0xA2, 0xA7, 0x83, 0x32, 0x0C,
  32010. 0xED, 0x59, 0x0B, 0xC4, 0x26, 0x44, 0x70, 0x2C,
  32011. 0xF3, 0x71, 0x27, 0x1E, 0x49, 0x6B, 0xF2, 0x0F,
  32012. 0x58, 0x8B, 0x78, 0xA1, 0xBC, 0x01, 0xEC, 0xBB,
  32013. 0x65, 0x59, 0x93, 0x4B, 0xDD, 0x2F, 0xB6, 0x5D,
  32014. 0x28, 0x84, 0x31, 0x8A, 0x33, 0xD1, 0xA4, 0x2A,
  32015. 0xDF, 0x5E, 0x33, 0xCC, 0x58, 0x00, 0x28, 0x0B,
  32016. 0x28, 0x35, 0x64, 0x97, 0xF8, 0x71, 0x35, 0xBA,
  32017. 0xB9, 0x61, 0x2A, 0x17, 0x26, 0x04, 0x24, 0x40,
  32018. 0x9A, 0xC1, 0x5F, 0xEE, 0x99, 0x6B, 0x74, 0x4C,
  32019. 0x33, 0x21, 0x51, 0x23, 0x5D, 0xEC, 0xB0, 0xF5
  32020. };
  32021. WOLFSSL_SMALL_STACK_STATIC const byte id[] = {
  32022. 0x32, 0x30, 0x31, 0x31, 0x2D, 0x30, 0x32, 0x00,
  32023. 0x74, 0x65, 0x6C, 0x3A, 0x2B, 0x34, 0x34, 0x37,
  32024. 0x37, 0x30, 0x30, 0x39, 0x30, 0x30, 0x31, 0x32,
  32025. 0x33, 0x00
  32026. };
  32027. WOLFSSL_SMALL_STACK_STATIC const byte ssv[] = {
  32028. 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0,
  32029. 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0
  32030. };
  32031. WOLFSSL_SMALL_STACK_STATIC const byte auth[] = {
  32032. 0x04,
  32033. 0x44, 0xE8, 0xAD, 0x44, 0xAB, 0x85, 0x92, 0xA6,
  32034. 0xA5, 0xA3, 0xDD, 0xCA, 0x5C, 0xF8, 0x96, 0xC7,
  32035. 0x18, 0x04, 0x36, 0x06, 0xA0, 0x1D, 0x65, 0x0D,
  32036. 0xEF, 0x37, 0xA0, 0x1F, 0x37, 0xC2, 0x28, 0xC3,
  32037. 0x32, 0xFC, 0x31, 0x73, 0x54, 0xE2, 0xC2, 0x74,
  32038. 0xD4, 0xDA, 0xF8, 0xAD, 0x00, 0x10, 0x54, 0xC7,
  32039. 0x6C, 0xE5, 0x79, 0x71, 0xC6, 0xF4, 0x48, 0x6D,
  32040. 0x57, 0x23, 0x04, 0x32, 0x61, 0xC5, 0x06, 0xEB,
  32041. 0xF5, 0xBE, 0x43, 0x8F, 0x53, 0xDE, 0x04, 0xF0,
  32042. 0x67, 0xC7, 0x76, 0xE0, 0xDD, 0x3B, 0x71, 0xA6,
  32043. 0x29, 0x01, 0x33, 0x28, 0x37, 0x25, 0xA5, 0x32,
  32044. 0xF2, 0x1A, 0xF1, 0x45, 0x12, 0x6D, 0xC1, 0xD7,
  32045. 0x77, 0xEC, 0xC2, 0x7B, 0xE5, 0x08, 0x35, 0xBD,
  32046. 0x28, 0x09, 0x8B, 0x8A, 0x73, 0xD9, 0xF8, 0x01,
  32047. 0xD8, 0x93, 0x79, 0x3A, 0x41, 0xFF, 0x5C, 0x49,
  32048. 0xB8, 0x7E, 0x79, 0xF2, 0xBE, 0x4D, 0x56, 0xCE,
  32049. 0x55, 0x7E, 0x13, 0x4A, 0xD8, 0x5B, 0xB1, 0xD4,
  32050. 0xB9, 0xCE, 0x4F, 0x8B, 0xE4, 0xB0, 0x8A, 0x12,
  32051. 0xBA, 0xBF, 0x55, 0xB1, 0xD6, 0xF1, 0xD7, 0xA6,
  32052. 0x38, 0x01, 0x9E, 0xA2, 0x8E, 0x15, 0xAB, 0x1C,
  32053. 0x9F, 0x76, 0x37, 0x5F, 0xDD, 0x12, 0x10, 0xD4,
  32054. 0xF4, 0x35, 0x1B, 0x9A, 0x00, 0x94, 0x86, 0xB7,
  32055. 0xF3, 0xED, 0x46, 0xC9, 0x65, 0xDE, 0xD2, 0xD8,
  32056. 0x0D, 0xAD, 0xE4, 0xF3, 0x8C, 0x67, 0x21, 0xD5,
  32057. 0x2C, 0x3A, 0xD1, 0x03, 0xA1, 0x0E, 0xBD, 0x29,
  32058. 0x59, 0x24, 0x8B, 0x4E, 0xF0, 0x06, 0x83, 0x6B,
  32059. 0xF0, 0x97, 0x44, 0x8E, 0x61, 0x07, 0xC9, 0xED,
  32060. 0xEE, 0x9F, 0xB7, 0x04, 0x82, 0x3D, 0xF1, 0x99,
  32061. 0xF8, 0x32, 0xC9, 0x05, 0xAE, 0x45, 0xF8, 0xA2,
  32062. 0x47, 0xA0, 0x72, 0xD8, 0xEF, 0x72, 0x9E, 0xAB,
  32063. 0xC5, 0xE2, 0x75, 0x74, 0xB0, 0x77, 0x39, 0xB3,
  32064. 0x4B, 0xE7, 0x4A, 0x53, 0x2F, 0x74, 0x7B, 0x86
  32065. };
  32066. WOLFSSL_SMALL_STACK_STATIC const byte encSsv[] = {
  32067. 0x89, 0xE0, 0xBC, 0x66, 0x1A, 0xA1, 0xE9, 0x16,
  32068. 0x38, 0xE6, 0xAC, 0xC8, 0x4E, 0x49, 0x65, 0x07
  32069. };
  32070. int ret;
  32071. int valid;
  32072. byte pubKey[sizeof(pubData) + 1];
  32073. word32 sz = sizeof(pubKey);
  32074. byte tmpSsv[sizeof(encSsv)];
  32075. byte* iTable = NULL;
  32076. word32 iTableLen;
  32077. byte* table = NULL;
  32078. word32 len;
  32079. ret = wc_ImportSakkePublicKey(key, pubData, sizeof(pubData), 0);
  32080. if (ret != 0)
  32081. return -10315;
  32082. ret = wc_DecodeSakkeRsk(key, rskData, sizeof(rskData), rsk);
  32083. if (ret != 0)
  32084. return -10316;
  32085. ret = wc_ValidateSakkeRsk(key, id, sizeof(id), rsk, &valid);
  32086. if (ret != 0)
  32087. return -10317;
  32088. if (valid != 1)
  32089. return -10318;
  32090. ret = wc_SetSakkeRsk(key, rsk, NULL, 0);
  32091. if (ret != 0)
  32092. return -10319;
  32093. ret = wc_SetSakkeIdentity(key, id, sizeof(id));
  32094. if (ret != 0)
  32095. return -10319;
  32096. XMEMCPY(tmpSsv, encSsv, sizeof(encSsv));
  32097. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, tmpSsv, sizeof(tmpSsv),
  32098. auth, sizeof(auth));
  32099. if (ret != 0)
  32100. return -10322;
  32101. if (XMEMCMP(tmpSsv, ssv, sizeof(ssv)) != 0)
  32102. return -10320;
  32103. ret = wc_MakeSakkePointI(key, id, sizeof(id));
  32104. if (ret != 0)
  32105. return -10321;
  32106. iTableLen = 0;
  32107. ret = wc_GenerateSakkePointITable(key, NULL, &iTableLen);
  32108. if (ret != LENGTH_ONLY_E)
  32109. return -10322;
  32110. if (iTableLen != 0) {
  32111. iTable = (byte*)XMALLOC(iTableLen, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32112. if (iTable == NULL)
  32113. return -10323;
  32114. ret = wc_GenerateSakkePointITable(key, iTable, &iTableLen);
  32115. if (ret != 0)
  32116. return -10324;
  32117. }
  32118. len = 0;
  32119. ret = wc_GenerateSakkeRskTable(key, rsk, NULL, &len);
  32120. if (ret != LENGTH_ONLY_E)
  32121. return -10325;
  32122. if (len > 0) {
  32123. table = (byte*)XMALLOC(len, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32124. if (table == NULL)
  32125. return -10326;
  32126. ret = wc_GenerateSakkeRskTable(key, rsk, table, &len);
  32127. if (ret != 0)
  32128. return -10327;
  32129. }
  32130. ret = wc_SetSakkeRsk(key, rsk, table, len);
  32131. if (ret != 0)
  32132. return -10319;
  32133. XMEMCPY(tmpSsv, encSsv, sizeof(encSsv));
  32134. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, tmpSsv, sizeof(tmpSsv),
  32135. auth, sizeof(auth));
  32136. if (ret != 0)
  32137. return -10328;
  32138. if (XMEMCMP(tmpSsv, ssv, sizeof(ssv)) != 0)
  32139. return -10329;
  32140. /* Don't reference table that is about to be freed. */
  32141. ret = wc_ClearSakkePointITable(key);
  32142. if (ret != 0)
  32143. return -10330;
  32144. /* Dispose of tables */
  32145. if (iTable != NULL)
  32146. XFREE(iTable, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32147. if (table != NULL)
  32148. XFREE(table, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32149. /* Make sure the key public key is exportable - convert to Montgomery form
  32150. * in Validation.
  32151. */
  32152. ret = wc_ExportSakkePublicKey(key, pubKey, &sz, 1);
  32153. if (ret != 0)
  32154. return -10331;
  32155. if (sz != sizeof(pubData))
  32156. return -10332;
  32157. if (XMEMCMP(pubKey, pubData, sizeof(pubData)) != 0)
  32158. return -10333;
  32159. sz = sizeof(pubData) + 1;
  32160. ret = wc_ExportSakkePublicKey(key, pubKey, &sz, 0);
  32161. if (ret != 0)
  32162. return -10334;
  32163. if (sz != sizeof(pubData) + 1)
  32164. return -10335;
  32165. if (pubKey[0] != 0x04)
  32166. return -10336;
  32167. if (XMEMCMP(pubKey + 1, pubData, sizeof(pubData)) != 0)
  32168. return -10337;
  32169. return 0;
  32170. }
  32171. static int sakke_kat_encapsulate_test(SakkeKey* key)
  32172. {
  32173. static const byte pubData[] = {
  32174. 0x59, 0x58, 0xEF, 0x1B, 0x16, 0x79, 0xBF, 0x09,
  32175. 0x9B, 0x3A, 0x03, 0x0D, 0xF2, 0x55, 0xAA, 0x6A,
  32176. 0x23, 0xC1, 0xD8, 0xF1, 0x43, 0xD4, 0xD2, 0x3F,
  32177. 0x75, 0x3E, 0x69, 0xBD, 0x27, 0xA8, 0x32, 0xF3,
  32178. 0x8C, 0xB4, 0xAD, 0x53, 0xDD, 0xEF, 0x42, 0x60,
  32179. 0xB0, 0xFE, 0x8B, 0xB4, 0x5C, 0x4C, 0x1F, 0xF5,
  32180. 0x10, 0xEF, 0xFE, 0x30, 0x03, 0x67, 0xA3, 0x7B,
  32181. 0x61, 0xF7, 0x01, 0xD9, 0x14, 0xAE, 0xF0, 0x97,
  32182. 0x24, 0x82, 0x5F, 0xA0, 0x70, 0x7D, 0x61, 0xA6,
  32183. 0xDF, 0xF4, 0xFB, 0xD7, 0x27, 0x35, 0x66, 0xCD,
  32184. 0xDE, 0x35, 0x2A, 0x0B, 0x04, 0xB7, 0xC1, 0x6A,
  32185. 0x78, 0x30, 0x9B, 0xE6, 0x40, 0x69, 0x7D, 0xE7,
  32186. 0x47, 0x61, 0x3A, 0x5F, 0xC1, 0x95, 0xE8, 0xB9,
  32187. 0xF3, 0x28, 0x85, 0x2A, 0x57, 0x9D, 0xB8, 0xF9,
  32188. 0x9B, 0x1D, 0x00, 0x34, 0x47, 0x9E, 0xA9, 0xC5,
  32189. 0x59, 0x5F, 0x47, 0xC4, 0xB2, 0xF5, 0x4F, 0xF2,
  32190. 0x15, 0x08, 0xD3, 0x75, 0x14, 0xDC, 0xF7, 0xA8,
  32191. 0xE1, 0x43, 0xA6, 0x05, 0x8C, 0x09, 0xA6, 0xBF,
  32192. 0x2C, 0x98, 0x58, 0xCA, 0x37, 0xC2, 0x58, 0x06,
  32193. 0x5A, 0xE6, 0xBF, 0x75, 0x32, 0xBC, 0x8B, 0x5B,
  32194. 0x63, 0x38, 0x38, 0x66, 0xE0, 0x75, 0x3C, 0x5A,
  32195. 0xC0, 0xE7, 0x27, 0x09, 0xF8, 0x44, 0x5F, 0x2E,
  32196. 0x61, 0x78, 0xE0, 0x65, 0x85, 0x7E, 0x0E, 0xDA,
  32197. 0x10, 0xF6, 0x82, 0x06, 0xB6, 0x35, 0x05, 0xED,
  32198. 0x87, 0xE5, 0x34, 0xFB, 0x28, 0x31, 0xFF, 0x95,
  32199. 0x7F, 0xB7, 0xDC, 0x61, 0x9D, 0xAE, 0x61, 0x30,
  32200. 0x1E, 0xEA, 0xCC, 0x2F, 0xDA, 0x36, 0x80, 0xEA,
  32201. 0x49, 0x99, 0x25, 0x8A, 0x83, 0x3C, 0xEA, 0x8F,
  32202. 0xC6, 0x7C, 0x6D, 0x19, 0x48, 0x7F, 0xB4, 0x49,
  32203. 0x05, 0x9F, 0x26, 0xCC, 0x8A, 0xAB, 0x65, 0x5A,
  32204. 0xB5, 0x8B, 0x7C, 0xC7, 0x96, 0xE2, 0x4E, 0x9A,
  32205. 0x39, 0x40, 0x95, 0x75, 0x4F, 0x5F, 0x8B, 0xAE
  32206. };
  32207. static const byte id[] = {
  32208. 0x32, 0x30, 0x31, 0x31, 0x2D, 0x30, 0x32, 0x00,
  32209. 0x74, 0x65, 0x6C, 0x3A, 0x2B, 0x34, 0x34, 0x37,
  32210. 0x37, 0x30, 0x30, 0x39, 0x30, 0x30, 0x31, 0x32,
  32211. 0x33, 0x00
  32212. };
  32213. static word32 idSz = sizeof(id);
  32214. byte ssv[] = {
  32215. 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0,
  32216. 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0
  32217. };
  32218. static word16 ssvSz = sizeof(ssv);
  32219. static const byte expAuth[] = {
  32220. 0x04,
  32221. 0x44, 0xE8, 0xAD, 0x44, 0xAB, 0x85, 0x92, 0xA6,
  32222. 0xA5, 0xA3, 0xDD, 0xCA, 0x5C, 0xF8, 0x96, 0xC7,
  32223. 0x18, 0x04, 0x36, 0x06, 0xA0, 0x1D, 0x65, 0x0D,
  32224. 0xEF, 0x37, 0xA0, 0x1F, 0x37, 0xC2, 0x28, 0xC3,
  32225. 0x32, 0xFC, 0x31, 0x73, 0x54, 0xE2, 0xC2, 0x74,
  32226. 0xD4, 0xDA, 0xF8, 0xAD, 0x00, 0x10, 0x54, 0xC7,
  32227. 0x6C, 0xE5, 0x79, 0x71, 0xC6, 0xF4, 0x48, 0x6D,
  32228. 0x57, 0x23, 0x04, 0x32, 0x61, 0xC5, 0x06, 0xEB,
  32229. 0xF5, 0xBE, 0x43, 0x8F, 0x53, 0xDE, 0x04, 0xF0,
  32230. 0x67, 0xC7, 0x76, 0xE0, 0xDD, 0x3B, 0x71, 0xA6,
  32231. 0x29, 0x01, 0x33, 0x28, 0x37, 0x25, 0xA5, 0x32,
  32232. 0xF2, 0x1A, 0xF1, 0x45, 0x12, 0x6D, 0xC1, 0xD7,
  32233. 0x77, 0xEC, 0xC2, 0x7B, 0xE5, 0x08, 0x35, 0xBD,
  32234. 0x28, 0x09, 0x8B, 0x8A, 0x73, 0xD9, 0xF8, 0x01,
  32235. 0xD8, 0x93, 0x79, 0x3A, 0x41, 0xFF, 0x5C, 0x49,
  32236. 0xB8, 0x7E, 0x79, 0xF2, 0xBE, 0x4D, 0x56, 0xCE,
  32237. 0x55, 0x7E, 0x13, 0x4A, 0xD8, 0x5B, 0xB1, 0xD4,
  32238. 0xB9, 0xCE, 0x4F, 0x8B, 0xE4, 0xB0, 0x8A, 0x12,
  32239. 0xBA, 0xBF, 0x55, 0xB1, 0xD6, 0xF1, 0xD7, 0xA6,
  32240. 0x38, 0x01, 0x9E, 0xA2, 0x8E, 0x15, 0xAB, 0x1C,
  32241. 0x9F, 0x76, 0x37, 0x5F, 0xDD, 0x12, 0x10, 0xD4,
  32242. 0xF4, 0x35, 0x1B, 0x9A, 0x00, 0x94, 0x86, 0xB7,
  32243. 0xF3, 0xED, 0x46, 0xC9, 0x65, 0xDE, 0xD2, 0xD8,
  32244. 0x0D, 0xAD, 0xE4, 0xF3, 0x8C, 0x67, 0x21, 0xD5,
  32245. 0x2C, 0x3A, 0xD1, 0x03, 0xA1, 0x0E, 0xBD, 0x29,
  32246. 0x59, 0x24, 0x8B, 0x4E, 0xF0, 0x06, 0x83, 0x6B,
  32247. 0xF0, 0x97, 0x44, 0x8E, 0x61, 0x07, 0xC9, 0xED,
  32248. 0xEE, 0x9F, 0xB7, 0x04, 0x82, 0x3D, 0xF1, 0x99,
  32249. 0xF8, 0x32, 0xC9, 0x05, 0xAE, 0x45, 0xF8, 0xA2,
  32250. 0x47, 0xA0, 0x72, 0xD8, 0xEF, 0x72, 0x9E, 0xAB,
  32251. 0xC5, 0xE2, 0x75, 0x74, 0xB0, 0x77, 0x39, 0xB3,
  32252. 0x4B, 0xE7, 0x4A, 0x53, 0x2F, 0x74, 0x7B, 0x86
  32253. };
  32254. static const byte encSsv[] = {
  32255. 0x89, 0xE0, 0xBC, 0x66, 0x1A, 0xA1, 0xE9, 0x16,
  32256. 0x38, 0xE6, 0xAC, 0xC8, 0x4E, 0x49, 0x65, 0x07
  32257. };
  32258. int ret;
  32259. byte auth[257];
  32260. word16 authSz = sizeof(auth);
  32261. ret = wc_ImportSakkePublicKey(key, pubData, sizeof(pubData), 0);
  32262. if (ret != 0)
  32263. return -10334;
  32264. ret = wc_SetSakkeIdentity(key, id, idSz);
  32265. if (ret != 0)
  32266. return -10335;
  32267. ret = wc_MakeSakkeEncapsulatedSSV(key, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  32268. auth, &authSz);
  32269. if (ret != 0)
  32270. return -10336;
  32271. if (authSz != 257)
  32272. return -10337;
  32273. if (XMEMCMP(ssv, encSsv, ssvSz) != 0)
  32274. return -10338;
  32275. if (XMEMCMP(auth, expAuth, authSz) != 0)
  32276. return -10339;
  32277. return 0;
  32278. }
  32279. static int sakke_make_key_test(SakkeKey* priv, SakkeKey* pub, SakkeKey* key,
  32280. WC_RNG* rng, ecc_point* rsk)
  32281. {
  32282. int ret;
  32283. byte data[440];
  32284. byte pubData[257];
  32285. word32 sz;
  32286. char mail[] = "test@wolfssl.com";
  32287. byte* id = (byte*)mail;
  32288. word32 idSz = (word32)XSTRLEN(mail);
  32289. int valid;
  32290. ecc_point* pubKey = rsk;
  32291. ret = wc_InitSakkeKey_ex(key, 128, ECC_SAKKE_1, NULL, INVALID_DEVID);
  32292. if (ret != 0)
  32293. return -10339;
  32294. ret = wc_MakeSakkeKey(priv, rng);
  32295. if (ret != 0)
  32296. return -10340;
  32297. ret = wc_ExportSakkeKey(priv, NULL, &sz);
  32298. if (ret != LENGTH_ONLY_E)
  32299. return -10341;
  32300. if (sz != 384)
  32301. return -10342;
  32302. sz--;
  32303. ret = wc_ExportSakkeKey(priv, data, &sz);
  32304. if (ret == 0)
  32305. return -10343;
  32306. sz++;
  32307. ret = wc_ExportSakkeKey(priv, data, &sz);
  32308. if (ret != 0)
  32309. return -10344;
  32310. if (sz != 384)
  32311. return -10345;
  32312. ret = wc_ImportSakkeKey(key, data, sz - 1);
  32313. if (ret == 0)
  32314. return -10346;
  32315. ret = wc_ImportSakkeKey(key, data, sz);
  32316. if (ret != 0)
  32317. return -10347;
  32318. wc_FreeSakkeKey(key);
  32319. ret = wc_InitSakkeKey_ex(key, 128, ECC_SAKKE_1, NULL, INVALID_DEVID);
  32320. if (ret != 0)
  32321. return -10348;
  32322. ret = wc_ExportSakkePrivateKey(priv, NULL, &sz);
  32323. if (ret != LENGTH_ONLY_E)
  32324. return -10349;
  32325. if (sz != 128)
  32326. return -10350;
  32327. sz--;
  32328. ret = wc_ExportSakkePrivateKey(priv, data, &sz);
  32329. if (ret == 0)
  32330. return -10351;
  32331. sz++;
  32332. ret = wc_ExportSakkePrivateKey(priv, data, &sz);
  32333. if (ret != 0)
  32334. return -10352;
  32335. if (sz != 128)
  32336. return -10353;
  32337. ret = wc_ImportSakkePrivateKey(key, data, sz - 1);
  32338. if (ret == 0)
  32339. return -10354;
  32340. ret = wc_ImportSakkePrivateKey(key, data, sz);
  32341. if (ret != 0)
  32342. return -10355;
  32343. ret = wc_MakeSakkePublicKey(key, pubKey);
  32344. if (ret != 0)
  32345. return -10356;
  32346. ret = wc_ExportSakkePublicKey(priv, NULL, &sz, 1);
  32347. if (ret != LENGTH_ONLY_E)
  32348. return -10357;
  32349. if (sz != 256)
  32350. return -10358;
  32351. sz--;
  32352. ret = wc_ExportSakkePublicKey(priv, data, &sz, 1);
  32353. if (ret == 0)
  32354. return -10359;
  32355. sz++;
  32356. ret = wc_ExportSakkePublicKey(priv, data, &sz, 1);
  32357. if (ret != 0)
  32358. return -10360;
  32359. if (sz != 256)
  32360. return -10361;
  32361. ret = wc_ImportSakkePublicKey(pub, data, sz - 1, 1);
  32362. if (ret == 0)
  32363. return -10362;
  32364. ret = wc_ImportSakkePublicKey(pub, data, sz, 1);
  32365. if (ret != 0)
  32366. return -10363;
  32367. ret = wc_ExportSakkePublicKey(pub, pubData, &sz, 1);
  32368. if (ret != 0)
  32369. return -10364;
  32370. if (sz != 256)
  32371. return -10365;
  32372. if (XMEMCMP(data, pubData, sz) != 0)
  32373. return -10366;
  32374. ret = wc_MakeSakkeRsk(priv, id, idSz, rsk);
  32375. if (ret != 0)
  32376. return -10367;
  32377. ret = wc_ValidateSakkeRsk(priv, id, idSz, rsk, &valid);
  32378. if (ret != 0)
  32379. return -10368;
  32380. if (valid != 1)
  32381. return -10369;
  32382. ret = wc_ValidateSakkeRsk(pub, id, idSz, rsk, &valid);
  32383. if (ret != 0)
  32384. return -10370;
  32385. if (valid != 1)
  32386. return -10371;
  32387. sz = sizeof(data);
  32388. ret = wc_EncodeSakkeRsk(priv, rsk, data, &sz, 1);
  32389. if (ret != 0)
  32390. return -10372;
  32391. if (sz != 256)
  32392. return -10373;
  32393. ret = wc_DecodeSakkeRsk(priv, data, sz, rsk);
  32394. if (ret != 0)
  32395. return -10374;
  32396. sz = sizeof(pubData);
  32397. ret = wc_EncodeSakkeRsk(priv, rsk, pubData, &sz, 0);
  32398. if (ret != 0)
  32399. return -10375;
  32400. if (sz != sizeof(pubData))
  32401. return -10376;
  32402. ret = wc_DecodeSakkeRsk(priv, pubData, sz, rsk);
  32403. if (ret != 0)
  32404. return -10377;
  32405. wc_FreeSakkeKey(key);
  32406. return 0;
  32407. }
  32408. static int sakke_op_test(SakkeKey* priv, SakkeKey* pub, WC_RNG* rng,
  32409. ecc_point* rsk)
  32410. {
  32411. int ret;
  32412. byte ssv[16];
  32413. word16 ssvSz;
  32414. byte auth[257];
  32415. word16 authSz;
  32416. char mail[] = "test@wolfssl.com";
  32417. byte* id = (byte*)mail;
  32418. word32 idSz = (word32)XSTRLEN(mail);
  32419. byte pointI[256];
  32420. word32 sz;
  32421. ret = wc_GenerateSakkeSSV(pub, rng, NULL, &ssvSz);
  32422. if (ret != LENGTH_ONLY_E)
  32423. return -10375;
  32424. if (ssvSz != 16)
  32425. return -10376;
  32426. ssvSz += 128;
  32427. ret = wc_GenerateSakkeSSV(pub, rng, ssv, &ssvSz);
  32428. if (ret == 0)
  32429. return -10377;
  32430. ssvSz -= 128;
  32431. ret = wc_GenerateSakkeSSV(pub, rng, ssv, &ssvSz);
  32432. if (ret != 0)
  32433. return -10378;
  32434. if (ssvSz != 16)
  32435. return -10379;
  32436. ret = wc_GetSakkeAuthSize(pub, &authSz);
  32437. if (ret != 0)
  32438. return -10380;
  32439. ret = wc_SetSakkeIdentity(pub, id, idSz);
  32440. if (ret != 0)
  32441. return -10380;
  32442. ret = wc_MakeSakkeEncapsulatedSSV(pub, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  32443. NULL, &authSz);
  32444. if (ret != LENGTH_ONLY_E)
  32445. return -10381;
  32446. if (authSz != 257)
  32447. return -10382;
  32448. authSz--;
  32449. ret = wc_MakeSakkeEncapsulatedSSV(pub, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  32450. auth, &authSz);
  32451. if (ret == 0)
  32452. return -10383;
  32453. authSz++;
  32454. ret = wc_MakeSakkeEncapsulatedSSV(pub, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  32455. auth, &authSz);
  32456. if (ret != 0)
  32457. return -10384;
  32458. if (authSz != 257)
  32459. return -10385;
  32460. ret = wc_GetSakkePointI(pub, NULL, &sz);
  32461. if (ret != LENGTH_ONLY_E)
  32462. return -10386;
  32463. if (sz != 256)
  32464. return -10387;
  32465. ret = wc_GetSakkePointI(pub, pointI, &sz);
  32466. if (ret != 0)
  32467. return -10388;
  32468. if (sz != 256)
  32469. return -10389;
  32470. /* Bogus identity - make it check and regenerate I. */
  32471. ret = wc_MakeSakkePointI(pub, ssv, ssvSz);
  32472. if (ret != 0)
  32473. return -10391;
  32474. ret = wc_MakeSakkeEncapsulatedSSV(pub, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  32475. auth, &authSz);
  32476. if (ret != 0)
  32477. return -10392;
  32478. if (authSz != 257)
  32479. return -10393;
  32480. ret = wc_SetSakkeRsk(priv, rsk, NULL, 0);
  32481. if (ret != 0)
  32482. return -10392;
  32483. ret = wc_SetSakkeIdentity(priv, id, idSz);
  32484. if (ret != 0)
  32485. return -10392;
  32486. authSz--;
  32487. ret = wc_DeriveSakkeSSV(priv, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  32488. authSz);
  32489. if (ret == 0)
  32490. return -10394;
  32491. authSz++;
  32492. ret = wc_DeriveSakkeSSV(priv, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  32493. authSz);
  32494. if (ret != 0)
  32495. return -10395;
  32496. ssv[0] ^= 0x80;
  32497. ret = wc_DeriveSakkeSSV(priv, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  32498. authSz);
  32499. if (ret != SAKKE_VERIFY_FAIL_E)
  32500. return -10396;
  32501. ssv[0] ^= 0x80;
  32502. /* Bogus identity - make it check and regenerate I. */
  32503. ret = wc_MakeSakkePointI(pub, ssv, idSz);
  32504. if (ret != 0)
  32505. return -10397;
  32506. ret = wc_DeriveSakkeSSV(priv, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  32507. authSz);
  32508. if (ret != 0)
  32509. return -10398;
  32510. return 0;
  32511. }
  32512. int sakke_test(void)
  32513. {
  32514. int ret = 0;
  32515. WC_RNG rng;
  32516. int rng_inited = 0;
  32517. SakkeKey* priv = NULL;
  32518. SakkeKey* pub = NULL;
  32519. SakkeKey* key = NULL;
  32520. ecc_point* rsk = NULL;
  32521. priv = (SakkeKey*)XMALLOC(sizeof(SakkeKey), HEAP_HINT,
  32522. DYNAMIC_TYPE_TMP_BUFFER);
  32523. if (priv == NULL)
  32524. ret = -10404;
  32525. else
  32526. XMEMSET(priv, 0, sizeof(*priv));
  32527. if (ret == 0) {
  32528. pub = (SakkeKey*)XMALLOC(sizeof(SakkeKey), HEAP_HINT,
  32529. DYNAMIC_TYPE_TMP_BUFFER);
  32530. if (pub == NULL)
  32531. ret = -10405;
  32532. else
  32533. XMEMSET(pub, 0, sizeof(*pub));
  32534. }
  32535. if (ret == 0) {
  32536. key = (SakkeKey*)XMALLOC(sizeof(SakkeKey), HEAP_HINT,
  32537. DYNAMIC_TYPE_TMP_BUFFER);
  32538. if (key == NULL)
  32539. ret = -10406;
  32540. else
  32541. XMEMSET(key, 0, sizeof(*key));
  32542. }
  32543. if (ret == 0) {
  32544. #ifndef HAVE_FIPS
  32545. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  32546. #else
  32547. ret = wc_InitRng(&rng);
  32548. #endif
  32549. if (ret == 0)
  32550. rng_inited = 1;
  32551. else
  32552. ret = -10400;
  32553. }
  32554. if (ret == 0) {
  32555. rsk = wc_ecc_new_point();
  32556. if (rsk == NULL)
  32557. ret = -10401;
  32558. }
  32559. if (ret == 0) {
  32560. ret = wc_InitSakkeKey(pub, HEAP_HINT, INVALID_DEVID);
  32561. if (ret != 0)
  32562. ret = -10402;
  32563. }
  32564. if (ret == 0) {
  32565. ret = wc_InitSakkeKey(priv, HEAP_HINT, INVALID_DEVID);
  32566. if (ret != 0)
  32567. ret = -10403;
  32568. }
  32569. if (ret == 0) {
  32570. ret = sakke_api_test(&rng, key, rsk);
  32571. }
  32572. if (ret == 0) {
  32573. ret = sakke_kat_derive_test(pub, rsk);
  32574. }
  32575. if (ret == 0) {
  32576. ret = sakke_kat_encapsulate_test(pub);
  32577. }
  32578. if (ret == 0) {
  32579. ret = sakke_make_key_test(priv, pub, key, &rng, rsk);
  32580. }
  32581. if (ret == 0) {
  32582. ret = sakke_op_test(priv, pub, &rng, rsk);
  32583. }
  32584. if (rsk != NULL) {
  32585. wc_ecc_forcezero_point(rsk);
  32586. wc_ecc_del_point(rsk);
  32587. }
  32588. if (rng_inited)
  32589. wc_FreeRng(&rng);
  32590. if (key != NULL)
  32591. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32592. if (pub != NULL) {
  32593. wc_FreeSakkeKey(pub);
  32594. XFREE(pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32595. }
  32596. if (priv != NULL) {
  32597. wc_FreeSakkeKey(priv);
  32598. XFREE(priv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32599. }
  32600. return ret;
  32601. }
  32602. #endif /* WOLFCRYPT_HAVE_SAKKE */
  32603. #if defined(WOLFSSL_CMAC) && !defined(NO_AES)
  32604. typedef struct CMAC_Test_Case {
  32605. int type;
  32606. int partial;
  32607. const byte* m;
  32608. word32 mSz;
  32609. const byte* k;
  32610. word32 kSz;
  32611. const byte* t;
  32612. word32 tSz;
  32613. } CMAC_Test_Case;
  32614. WOLFSSL_TEST_SUBROUTINE int cmac_test(void)
  32615. {
  32616. #ifdef WOLFSSL_AES_128
  32617. WOLFSSL_SMALL_STACK_STATIC const byte k128[] =
  32618. {
  32619. 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
  32620. 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c
  32621. };
  32622. #define KLEN_128 (sizeof(k128))
  32623. #endif
  32624. #ifdef WOLFSSL_AES_192
  32625. WOLFSSL_SMALL_STACK_STATIC const byte k192[] =
  32626. {
  32627. 0x8e, 0x73, 0xb0, 0xf7, 0xda, 0x0e, 0x64, 0x52,
  32628. 0xc8, 0x10, 0xf3, 0x2b, 0x80, 0x90, 0x79, 0xe5,
  32629. 0x62, 0xf8, 0xea, 0xd2, 0x52, 0x2c, 0x6b, 0x7b
  32630. };
  32631. #define KLEN_192 (sizeof(k192))
  32632. #endif
  32633. #ifdef WOLFSSL_AES_256
  32634. WOLFSSL_SMALL_STACK_STATIC const byte k256[] =
  32635. {
  32636. 0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe,
  32637. 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
  32638. 0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7,
  32639. 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4
  32640. };
  32641. #define KLEN_256 (sizeof(k256))
  32642. #endif
  32643. WOLFSSL_SMALL_STACK_STATIC const byte m[] =
  32644. {
  32645. 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
  32646. 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
  32647. 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c,
  32648. 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51,
  32649. 0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11,
  32650. 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef,
  32651. 0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17,
  32652. 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10
  32653. };
  32654. #define MLEN_0 (0)
  32655. #define MLEN_128 (128/8)
  32656. #define MLEN_320 (320/8)
  32657. #define MLEN_319 (MLEN_320 - 1)
  32658. #define MLEN_512 (512/8)
  32659. #ifdef WOLFSSL_AES_128
  32660. WOLFSSL_SMALL_STACK_STATIC const byte t128_0[] =
  32661. {
  32662. 0xbb, 0x1d, 0x69, 0x29, 0xe9, 0x59, 0x37, 0x28,
  32663. 0x7f, 0xa3, 0x7d, 0x12, 0x9b, 0x75, 0x67, 0x46
  32664. };
  32665. WOLFSSL_SMALL_STACK_STATIC const byte t128_128[] =
  32666. {
  32667. 0x07, 0x0a, 0x16, 0xb4, 0x6b, 0x4d, 0x41, 0x44,
  32668. 0xf7, 0x9b, 0xdd, 0x9d, 0xd0, 0x4a, 0x28, 0x7c
  32669. };
  32670. WOLFSSL_SMALL_STACK_STATIC const byte t128_319[] =
  32671. {
  32672. 0x2c, 0x17, 0x84, 0x4c, 0x93, 0x1c, 0x07, 0x95,
  32673. 0x15, 0x92, 0x73, 0x0a, 0x34, 0xd0, 0xd9, 0xd2
  32674. };
  32675. WOLFSSL_SMALL_STACK_STATIC const byte t128_320[] =
  32676. {
  32677. 0xdf, 0xa6, 0x67, 0x47, 0xde, 0x9a, 0xe6, 0x30,
  32678. 0x30, 0xca, 0x32, 0x61, 0x14, 0x97, 0xc8, 0x27
  32679. };
  32680. WOLFSSL_SMALL_STACK_STATIC const byte t128_512[] =
  32681. {
  32682. 0x51, 0xf0, 0xbe, 0xbf, 0x7e, 0x3b, 0x9d, 0x92,
  32683. 0xfc, 0x49, 0x74, 0x17, 0x79, 0x36, 0x3c, 0xfe
  32684. };
  32685. #endif
  32686. #ifdef WOLFSSL_AES_192
  32687. WOLFSSL_SMALL_STACK_STATIC const byte t192_0[] =
  32688. {
  32689. 0xd1, 0x7d, 0xdf, 0x46, 0xad, 0xaa, 0xcd, 0xe5,
  32690. 0x31, 0xca, 0xc4, 0x83, 0xde, 0x7a, 0x93, 0x67
  32691. };
  32692. WOLFSSL_SMALL_STACK_STATIC const byte t192_128[] =
  32693. {
  32694. 0x9e, 0x99, 0xa7, 0xbf, 0x31, 0xe7, 0x10, 0x90,
  32695. 0x06, 0x62, 0xf6, 0x5e, 0x61, 0x7c, 0x51, 0x84
  32696. };
  32697. WOLFSSL_SMALL_STACK_STATIC const byte t192_320[] =
  32698. {
  32699. 0x8a, 0x1d, 0xe5, 0xbe, 0x2e, 0xb3, 0x1a, 0xad,
  32700. 0x08, 0x9a, 0x82, 0xe6, 0xee, 0x90, 0x8b, 0x0e
  32701. };
  32702. WOLFSSL_SMALL_STACK_STATIC const byte t192_512[] =
  32703. {
  32704. 0xa1, 0xd5, 0xdf, 0x0e, 0xed, 0x79, 0x0f, 0x79,
  32705. 0x4d, 0x77, 0x58, 0x96, 0x59, 0xf3, 0x9a, 0x11
  32706. };
  32707. #endif
  32708. #ifdef WOLFSSL_AES_256
  32709. WOLFSSL_SMALL_STACK_STATIC const byte t256_0[] =
  32710. {
  32711. 0x02, 0x89, 0x62, 0xf6, 0x1b, 0x7b, 0xf8, 0x9e,
  32712. 0xfc, 0x6b, 0x55, 0x1f, 0x46, 0x67, 0xd9, 0x83
  32713. };
  32714. WOLFSSL_SMALL_STACK_STATIC const byte t256_128[] =
  32715. {
  32716. 0x28, 0xa7, 0x02, 0x3f, 0x45, 0x2e, 0x8f, 0x82,
  32717. 0xbd, 0x4b, 0xf2, 0x8d, 0x8c, 0x37, 0xc3, 0x5c
  32718. };
  32719. WOLFSSL_SMALL_STACK_STATIC const byte t256_320[] =
  32720. {
  32721. 0xaa, 0xf3, 0xd8, 0xf1, 0xde, 0x56, 0x40, 0xc2,
  32722. 0x32, 0xf5, 0xb1, 0x69, 0xb9, 0xc9, 0x11, 0xe6
  32723. };
  32724. WOLFSSL_SMALL_STACK_STATIC const byte t256_512[] =
  32725. {
  32726. 0xe1, 0x99, 0x21, 0x90, 0x54, 0x9f, 0x6e, 0xd5,
  32727. 0x69, 0x6a, 0x2c, 0x05, 0x6c, 0x31, 0x54, 0x10
  32728. };
  32729. #endif
  32730. const CMAC_Test_Case testCases[] =
  32731. {
  32732. #ifdef WOLFSSL_AES_128
  32733. {WC_CMAC_AES, 0, m, MLEN_0, k128, KLEN_128, t128_0, AES_BLOCK_SIZE},
  32734. {WC_CMAC_AES, 0, m, MLEN_128, k128, KLEN_128, t128_128, AES_BLOCK_SIZE},
  32735. {WC_CMAC_AES, 0, m, MLEN_320, k128, KLEN_128, t128_320, AES_BLOCK_SIZE},
  32736. {WC_CMAC_AES, 0, m, MLEN_512, k128, KLEN_128, t128_512, AES_BLOCK_SIZE},
  32737. {WC_CMAC_AES, 5, m, MLEN_512, k128, KLEN_128, t128_512, AES_BLOCK_SIZE},
  32738. #endif
  32739. #ifdef WOLFSSL_AES_192
  32740. {WC_CMAC_AES, 0, m, MLEN_0, k192, KLEN_192, t192_0, AES_BLOCK_SIZE},
  32741. {WC_CMAC_AES, 0, m, MLEN_128, k192, KLEN_192, t192_128, AES_BLOCK_SIZE},
  32742. {WC_CMAC_AES, 0, m, MLEN_320, k192, KLEN_192, t192_320, AES_BLOCK_SIZE},
  32743. {WC_CMAC_AES, 0, m, MLEN_512, k192, KLEN_192, t192_512, AES_BLOCK_SIZE},
  32744. #endif
  32745. #ifdef WOLFSSL_AES_256
  32746. {WC_CMAC_AES, 0, m, MLEN_0, k256, KLEN_256, t256_0, AES_BLOCK_SIZE},
  32747. {WC_CMAC_AES, 0, m, MLEN_128, k256, KLEN_256, t256_128, AES_BLOCK_SIZE},
  32748. {WC_CMAC_AES, 0, m, MLEN_320, k256, KLEN_256, t256_320, AES_BLOCK_SIZE},
  32749. {WC_CMAC_AES, 0, m, MLEN_512, k256, KLEN_256, t256_512, AES_BLOCK_SIZE},
  32750. #endif
  32751. #ifdef WOLFSSL_AES_128
  32752. {WC_CMAC_AES, 0, m, MLEN_319, k128, KLEN_128, t128_319, AES_BLOCK_SIZE}
  32753. #endif
  32754. };
  32755. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  32756. Cmac *cmac;
  32757. #else
  32758. Cmac cmac[1];
  32759. #endif
  32760. byte tag[AES_BLOCK_SIZE];
  32761. const CMAC_Test_Case* tc;
  32762. word32 i, tagSz;
  32763. int ret;
  32764. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  32765. if ((cmac = (Cmac *)XMALLOC(sizeof *cmac, HEAP_HINT, DYNAMIC_TYPE_CMAC)) == NULL)
  32766. ERROR_OUT(-12009, out);
  32767. #endif
  32768. for (i = 0, tc = testCases;
  32769. i < sizeof(testCases)/sizeof(CMAC_Test_Case);
  32770. i++, tc++) {
  32771. XMEMSET(tag, 0, sizeof(tag));
  32772. tagSz = AES_BLOCK_SIZE;
  32773. #if !defined(HAVE_FIPS) || \
  32774. defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3)
  32775. if (wc_InitCmac_ex(cmac, tc->k, tc->kSz, tc->type, NULL, HEAP_HINT, devId) != 0)
  32776. #else
  32777. if (wc_InitCmac(cmac, tc->k, tc->kSz, tc->type, NULL) != 0)
  32778. #endif
  32779. {
  32780. ERROR_OUT(-12000, out);
  32781. }
  32782. if (tc->partial) {
  32783. if (wc_CmacUpdate(cmac, tc->m,
  32784. tc->mSz/2 - tc->partial) != 0)
  32785. ERROR_OUT(-12001, out);
  32786. if (wc_CmacUpdate(cmac, tc->m + tc->mSz/2 - tc->partial,
  32787. tc->mSz/2 + tc->partial) != 0)
  32788. ERROR_OUT(-12002, out);
  32789. }
  32790. else {
  32791. if (wc_CmacUpdate(cmac, tc->m, tc->mSz) != 0)
  32792. ERROR_OUT(-12003, out);
  32793. }
  32794. if (wc_CmacFinal(cmac, tag, &tagSz) != 0)
  32795. ERROR_OUT(-12004, out);
  32796. if (XMEMCMP(tag, tc->t, AES_BLOCK_SIZE) != 0)
  32797. ERROR_OUT(-12005, out);
  32798. XMEMSET(tag, 0, sizeof(tag));
  32799. tagSz = sizeof(tag);
  32800. if (wc_AesCmacGenerate(tag, &tagSz, tc->m, tc->mSz,
  32801. tc->k, tc->kSz) != 0)
  32802. ERROR_OUT(-12006, out);
  32803. if (XMEMCMP(tag, tc->t, AES_BLOCK_SIZE) != 0)
  32804. ERROR_OUT(-12007, out);
  32805. if (wc_AesCmacVerify(tc->t, tc->tSz, tc->m, tc->mSz,
  32806. tc->k, tc->kSz) != 0)
  32807. ERROR_OUT(-12008, out);
  32808. }
  32809. ret = 0;
  32810. out:
  32811. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  32812. if (cmac)
  32813. XFREE(cmac, HEAP_HINT, DYNAMIC_TYPE_CMAC);
  32814. #endif
  32815. return ret;
  32816. }
  32817. #endif /* NO_AES && WOLFSSL_CMAC */
  32818. #if defined(WOLFSSL_SIPHASH)
  32819. #if WOLFSSL_SIPHASH_CROUNDS == 2 && WOLFSSL_SIPHASH_DROUNDS == 4
  32820. /* Test vectors from:
  32821. * https://github.com/veorq/SipHash/blob/master/vectors.h
  32822. */
  32823. static const unsigned char siphash_key[SIPHASH_KEY_SIZE] = {
  32824. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  32825. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
  32826. };
  32827. static const unsigned char siphash_msg[64] = {
  32828. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  32829. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  32830. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  32831. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
  32832. 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  32833. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
  32834. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  32835. 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f
  32836. };
  32837. static const unsigned char siphash_r8[64][SIPHASH_MAC_SIZE_8] = {
  32838. { 0x31, 0x0e, 0x0e, 0xdd, 0x47, 0xdb, 0x6f, 0x72, },
  32839. { 0xfd, 0x67, 0xdc, 0x93, 0xc5, 0x39, 0xf8, 0x74, },
  32840. { 0x5a, 0x4f, 0xa9, 0xd9, 0x09, 0x80, 0x6c, 0x0d, },
  32841. { 0x2d, 0x7e, 0xfb, 0xd7, 0x96, 0x66, 0x67, 0x85, },
  32842. { 0xb7, 0x87, 0x71, 0x27, 0xe0, 0x94, 0x27, 0xcf, },
  32843. { 0x8d, 0xa6, 0x99, 0xcd, 0x64, 0x55, 0x76, 0x18, },
  32844. { 0xce, 0xe3, 0xfe, 0x58, 0x6e, 0x46, 0xc9, 0xcb, },
  32845. { 0x37, 0xd1, 0x01, 0x8b, 0xf5, 0x00, 0x02, 0xab, },
  32846. { 0x62, 0x24, 0x93, 0x9a, 0x79, 0xf5, 0xf5, 0x93, },
  32847. { 0xb0, 0xe4, 0xa9, 0x0b, 0xdf, 0x82, 0x00, 0x9e, },
  32848. { 0xf3, 0xb9, 0xdd, 0x94, 0xc5, 0xbb, 0x5d, 0x7a, },
  32849. { 0xa7, 0xad, 0x6b, 0x22, 0x46, 0x2f, 0xb3, 0xf4, },
  32850. { 0xfb, 0xe5, 0x0e, 0x86, 0xbc, 0x8f, 0x1e, 0x75, },
  32851. { 0x90, 0x3d, 0x84, 0xc0, 0x27, 0x56, 0xea, 0x14, },
  32852. { 0xee, 0xf2, 0x7a, 0x8e, 0x90, 0xca, 0x23, 0xf7, },
  32853. { 0xe5, 0x45, 0xbe, 0x49, 0x61, 0xca, 0x29, 0xa1, },
  32854. { 0xdb, 0x9b, 0xc2, 0x57, 0x7f, 0xcc, 0x2a, 0x3f, },
  32855. { 0x94, 0x47, 0xbe, 0x2c, 0xf5, 0xe9, 0x9a, 0x69, },
  32856. { 0x9c, 0xd3, 0x8d, 0x96, 0xf0, 0xb3, 0xc1, 0x4b, },
  32857. { 0xbd, 0x61, 0x79, 0xa7, 0x1d, 0xc9, 0x6d, 0xbb, },
  32858. { 0x98, 0xee, 0xa2, 0x1a, 0xf2, 0x5c, 0xd6, 0xbe, },
  32859. { 0xc7, 0x67, 0x3b, 0x2e, 0xb0, 0xcb, 0xf2, 0xd0, },
  32860. { 0x88, 0x3e, 0xa3, 0xe3, 0x95, 0x67, 0x53, 0x93, },
  32861. { 0xc8, 0xce, 0x5c, 0xcd, 0x8c, 0x03, 0x0c, 0xa8, },
  32862. { 0x94, 0xaf, 0x49, 0xf6, 0xc6, 0x50, 0xad, 0xb8, },
  32863. { 0xea, 0xb8, 0x85, 0x8a, 0xde, 0x92, 0xe1, 0xbc, },
  32864. { 0xf3, 0x15, 0xbb, 0x5b, 0xb8, 0x35, 0xd8, 0x17, },
  32865. { 0xad, 0xcf, 0x6b, 0x07, 0x63, 0x61, 0x2e, 0x2f, },
  32866. { 0xa5, 0xc9, 0x1d, 0xa7, 0xac, 0xaa, 0x4d, 0xde, },
  32867. { 0x71, 0x65, 0x95, 0x87, 0x66, 0x50, 0xa2, 0xa6, },
  32868. { 0x28, 0xef, 0x49, 0x5c, 0x53, 0xa3, 0x87, 0xad, },
  32869. { 0x42, 0xc3, 0x41, 0xd8, 0xfa, 0x92, 0xd8, 0x32, },
  32870. { 0xce, 0x7c, 0xf2, 0x72, 0x2f, 0x51, 0x27, 0x71, },
  32871. { 0xe3, 0x78, 0x59, 0xf9, 0x46, 0x23, 0xf3, 0xa7, },
  32872. { 0x38, 0x12, 0x05, 0xbb, 0x1a, 0xb0, 0xe0, 0x12, },
  32873. { 0xae, 0x97, 0xa1, 0x0f, 0xd4, 0x34, 0xe0, 0x15, },
  32874. { 0xb4, 0xa3, 0x15, 0x08, 0xbe, 0xff, 0x4d, 0x31, },
  32875. { 0x81, 0x39, 0x62, 0x29, 0xf0, 0x90, 0x79, 0x02, },
  32876. { 0x4d, 0x0c, 0xf4, 0x9e, 0xe5, 0xd4, 0xdc, 0xca, },
  32877. { 0x5c, 0x73, 0x33, 0x6a, 0x76, 0xd8, 0xbf, 0x9a, },
  32878. { 0xd0, 0xa7, 0x04, 0x53, 0x6b, 0xa9, 0x3e, 0x0e, },
  32879. { 0x92, 0x59, 0x58, 0xfc, 0xd6, 0x42, 0x0c, 0xad, },
  32880. { 0xa9, 0x15, 0xc2, 0x9b, 0xc8, 0x06, 0x73, 0x18, },
  32881. { 0x95, 0x2b, 0x79, 0xf3, 0xbc, 0x0a, 0xa6, 0xd4, },
  32882. { 0xf2, 0x1d, 0xf2, 0xe4, 0x1d, 0x45, 0x35, 0xf9, },
  32883. { 0x87, 0x57, 0x75, 0x19, 0x04, 0x8f, 0x53, 0xa9, },
  32884. { 0x10, 0xa5, 0x6c, 0xf5, 0xdf, 0xcd, 0x9a, 0xdb, },
  32885. { 0xeb, 0x75, 0x09, 0x5c, 0xcd, 0x98, 0x6c, 0xd0, },
  32886. { 0x51, 0xa9, 0xcb, 0x9e, 0xcb, 0xa3, 0x12, 0xe6, },
  32887. { 0x96, 0xaf, 0xad, 0xfc, 0x2c, 0xe6, 0x66, 0xc7, },
  32888. { 0x72, 0xfe, 0x52, 0x97, 0x5a, 0x43, 0x64, 0xee, },
  32889. { 0x5a, 0x16, 0x45, 0xb2, 0x76, 0xd5, 0x92, 0xa1, },
  32890. { 0xb2, 0x74, 0xcb, 0x8e, 0xbf, 0x87, 0x87, 0x0a, },
  32891. { 0x6f, 0x9b, 0xb4, 0x20, 0x3d, 0xe7, 0xb3, 0x81, },
  32892. { 0xea, 0xec, 0xb2, 0xa3, 0x0b, 0x22, 0xa8, 0x7f, },
  32893. { 0x99, 0x24, 0xa4, 0x3c, 0xc1, 0x31, 0x57, 0x24, },
  32894. { 0xbd, 0x83, 0x8d, 0x3a, 0xaf, 0xbf, 0x8d, 0xb7, },
  32895. { 0x0b, 0x1a, 0x2a, 0x32, 0x65, 0xd5, 0x1a, 0xea, },
  32896. { 0x13, 0x50, 0x79, 0xa3, 0x23, 0x1c, 0xe6, 0x60, },
  32897. { 0x93, 0x2b, 0x28, 0x46, 0xe4, 0xd7, 0x06, 0x66, },
  32898. { 0xe1, 0x91, 0x5f, 0x5c, 0xb1, 0xec, 0xa4, 0x6c, },
  32899. { 0xf3, 0x25, 0x96, 0x5c, 0xa1, 0x6d, 0x62, 0x9f, },
  32900. { 0x57, 0x5f, 0xf2, 0x8e, 0x60, 0x38, 0x1b, 0xe5, },
  32901. { 0x72, 0x45, 0x06, 0xeb, 0x4c, 0x32, 0x8a, 0x95, },
  32902. };
  32903. static const unsigned char siphash_r16[64][SIPHASH_MAC_SIZE_16] = {
  32904. { 0xa3, 0x81, 0x7f, 0x04, 0xba, 0x25, 0xa8, 0xe6,
  32905. 0x6d, 0xf6, 0x72, 0x14, 0xc7, 0x55, 0x02, 0x93, },
  32906. { 0xda, 0x87, 0xc1, 0xd8, 0x6b, 0x99, 0xaf, 0x44,
  32907. 0x34, 0x76, 0x59, 0x11, 0x9b, 0x22, 0xfc, 0x45, },
  32908. { 0x81, 0x77, 0x22, 0x8d, 0xa4, 0xa4, 0x5d, 0xc7,
  32909. 0xfc, 0xa3, 0x8b, 0xde, 0xf6, 0x0a, 0xff, 0xe4, },
  32910. { 0x9c, 0x70, 0xb6, 0x0c, 0x52, 0x67, 0xa9, 0x4e,
  32911. 0x5f, 0x33, 0xb6, 0xb0, 0x29, 0x85, 0xed, 0x51, },
  32912. { 0xf8, 0x81, 0x64, 0xc1, 0x2d, 0x9c, 0x8f, 0xaf,
  32913. 0x7d, 0x0f, 0x6e, 0x7c, 0x7b, 0xcd, 0x55, 0x79, },
  32914. { 0x13, 0x68, 0x87, 0x59, 0x80, 0x77, 0x6f, 0x88,
  32915. 0x54, 0x52, 0x7a, 0x07, 0x69, 0x0e, 0x96, 0x27, },
  32916. { 0x14, 0xee, 0xca, 0x33, 0x8b, 0x20, 0x86, 0x13,
  32917. 0x48, 0x5e, 0xa0, 0x30, 0x8f, 0xd7, 0xa1, 0x5e, },
  32918. { 0xa1, 0xf1, 0xeb, 0xbe, 0xd8, 0xdb, 0xc1, 0x53,
  32919. 0xc0, 0xb8, 0x4a, 0xa6, 0x1f, 0xf0, 0x82, 0x39, },
  32920. { 0x3b, 0x62, 0xa9, 0xba, 0x62, 0x58, 0xf5, 0x61,
  32921. 0x0f, 0x83, 0xe2, 0x64, 0xf3, 0x14, 0x97, 0xb4, },
  32922. { 0x26, 0x44, 0x99, 0x06, 0x0a, 0xd9, 0xba, 0xab,
  32923. 0xc4, 0x7f, 0x8b, 0x02, 0xbb, 0x6d, 0x71, 0xed, },
  32924. { 0x00, 0x11, 0x0d, 0xc3, 0x78, 0x14, 0x69, 0x56,
  32925. 0xc9, 0x54, 0x47, 0xd3, 0xf3, 0xd0, 0xfb, 0xba, },
  32926. { 0x01, 0x51, 0xc5, 0x68, 0x38, 0x6b, 0x66, 0x77,
  32927. 0xa2, 0xb4, 0xdc, 0x6f, 0x81, 0xe5, 0xdc, 0x18, },
  32928. { 0xd6, 0x26, 0xb2, 0x66, 0x90, 0x5e, 0xf3, 0x58,
  32929. 0x82, 0x63, 0x4d, 0xf6, 0x85, 0x32, 0xc1, 0x25, },
  32930. { 0x98, 0x69, 0xe2, 0x47, 0xe9, 0xc0, 0x8b, 0x10,
  32931. 0xd0, 0x29, 0x93, 0x4f, 0xc4, 0xb9, 0x52, 0xf7, },
  32932. { 0x31, 0xfc, 0xef, 0xac, 0x66, 0xd7, 0xde, 0x9c,
  32933. 0x7e, 0xc7, 0x48, 0x5f, 0xe4, 0x49, 0x49, 0x02, },
  32934. { 0x54, 0x93, 0xe9, 0x99, 0x33, 0xb0, 0xa8, 0x11,
  32935. 0x7e, 0x08, 0xec, 0x0f, 0x97, 0xcf, 0xc3, 0xd9, },
  32936. { 0x6e, 0xe2, 0xa4, 0xca, 0x67, 0xb0, 0x54, 0xbb,
  32937. 0xfd, 0x33, 0x15, 0xbf, 0x85, 0x23, 0x05, 0x77, },
  32938. { 0x47, 0x3d, 0x06, 0xe8, 0x73, 0x8d, 0xb8, 0x98,
  32939. 0x54, 0xc0, 0x66, 0xc4, 0x7a, 0xe4, 0x77, 0x40, },
  32940. { 0xa4, 0x26, 0xe5, 0xe4, 0x23, 0xbf, 0x48, 0x85,
  32941. 0x29, 0x4d, 0xa4, 0x81, 0xfe, 0xae, 0xf7, 0x23, },
  32942. { 0x78, 0x01, 0x77, 0x31, 0xcf, 0x65, 0xfa, 0xb0,
  32943. 0x74, 0xd5, 0x20, 0x89, 0x52, 0x51, 0x2e, 0xb1, },
  32944. { 0x9e, 0x25, 0xfc, 0x83, 0x3f, 0x22, 0x90, 0x73,
  32945. 0x3e, 0x93, 0x44, 0xa5, 0xe8, 0x38, 0x39, 0xeb, },
  32946. { 0x56, 0x8e, 0x49, 0x5a, 0xbe, 0x52, 0x5a, 0x21,
  32947. 0x8a, 0x22, 0x14, 0xcd, 0x3e, 0x07, 0x1d, 0x12, },
  32948. { 0x4a, 0x29, 0xb5, 0x45, 0x52, 0xd1, 0x6b, 0x9a,
  32949. 0x46, 0x9c, 0x10, 0x52, 0x8e, 0xff, 0x0a, 0xae, },
  32950. { 0xc9, 0xd1, 0x84, 0xdd, 0xd5, 0xa9, 0xf5, 0xe0,
  32951. 0xcf, 0x8c, 0xe2, 0x9a, 0x9a, 0xbf, 0x69, 0x1c, },
  32952. { 0x2d, 0xb4, 0x79, 0xae, 0x78, 0xbd, 0x50, 0xd8,
  32953. 0x88, 0x2a, 0x8a, 0x17, 0x8a, 0x61, 0x32, 0xad, },
  32954. { 0x8e, 0xce, 0x5f, 0x04, 0x2d, 0x5e, 0x44, 0x7b,
  32955. 0x50, 0x51, 0xb9, 0xea, 0xcb, 0x8d, 0x8f, 0x6f, },
  32956. { 0x9c, 0x0b, 0x53, 0xb4, 0xb3, 0xc3, 0x07, 0xe8,
  32957. 0x7e, 0xae, 0xe0, 0x86, 0x78, 0x14, 0x1f, 0x66, },
  32958. { 0xab, 0xf2, 0x48, 0xaf, 0x69, 0xa6, 0xea, 0xe4,
  32959. 0xbf, 0xd3, 0xeb, 0x2f, 0x12, 0x9e, 0xeb, 0x94, },
  32960. { 0x06, 0x64, 0xda, 0x16, 0x68, 0x57, 0x4b, 0x88,
  32961. 0xb9, 0x35, 0xf3, 0x02, 0x73, 0x58, 0xae, 0xf4, },
  32962. { 0xaa, 0x4b, 0x9d, 0xc4, 0xbf, 0x33, 0x7d, 0xe9,
  32963. 0x0c, 0xd4, 0xfd, 0x3c, 0x46, 0x7c, 0x6a, 0xb7, },
  32964. { 0xea, 0x5c, 0x7f, 0x47, 0x1f, 0xaf, 0x6b, 0xde,
  32965. 0x2b, 0x1a, 0xd7, 0xd4, 0x68, 0x6d, 0x22, 0x87, },
  32966. { 0x29, 0x39, 0xb0, 0x18, 0x32, 0x23, 0xfa, 0xfc,
  32967. 0x17, 0x23, 0xde, 0x4f, 0x52, 0xc4, 0x3d, 0x35, },
  32968. { 0x7c, 0x39, 0x56, 0xca, 0x5e, 0xea, 0xfc, 0x3e,
  32969. 0x36, 0x3e, 0x9d, 0x55, 0x65, 0x46, 0xeb, 0x68, },
  32970. { 0x77, 0xc6, 0x07, 0x71, 0x46, 0xf0, 0x1c, 0x32,
  32971. 0xb6, 0xb6, 0x9d, 0x5f, 0x4e, 0xa9, 0xff, 0xcf, },
  32972. { 0x37, 0xa6, 0x98, 0x6c, 0xb8, 0x84, 0x7e, 0xdf,
  32973. 0x09, 0x25, 0xf0, 0xf1, 0x30, 0x9b, 0x54, 0xde, },
  32974. { 0xa7, 0x05, 0xf0, 0xe6, 0x9d, 0xa9, 0xa8, 0xf9,
  32975. 0x07, 0x24, 0x1a, 0x2e, 0x92, 0x3c, 0x8c, 0xc8, },
  32976. { 0x3d, 0xc4, 0x7d, 0x1f, 0x29, 0xc4, 0x48, 0x46,
  32977. 0x1e, 0x9e, 0x76, 0xed, 0x90, 0x4f, 0x67, 0x11, },
  32978. { 0x0d, 0x62, 0xbf, 0x01, 0xe6, 0xfc, 0x0e, 0x1a,
  32979. 0x0d, 0x3c, 0x47, 0x51, 0xc5, 0xd3, 0x69, 0x2b, },
  32980. { 0x8c, 0x03, 0x46, 0x8b, 0xca, 0x7c, 0x66, 0x9e,
  32981. 0xe4, 0xfd, 0x5e, 0x08, 0x4b, 0xbe, 0xe7, 0xb5, },
  32982. { 0x52, 0x8a, 0x5b, 0xb9, 0x3b, 0xaf, 0x2c, 0x9c,
  32983. 0x44, 0x73, 0xcc, 0xe5, 0xd0, 0xd2, 0x2b, 0xd9, },
  32984. { 0xdf, 0x6a, 0x30, 0x1e, 0x95, 0xc9, 0x5d, 0xad,
  32985. 0x97, 0xae, 0x0c, 0xc8, 0xc6, 0x91, 0x3b, 0xd8, },
  32986. { 0x80, 0x11, 0x89, 0x90, 0x2c, 0x85, 0x7f, 0x39,
  32987. 0xe7, 0x35, 0x91, 0x28, 0x5e, 0x70, 0xb6, 0xdb, },
  32988. { 0xe6, 0x17, 0x34, 0x6a, 0xc9, 0xc2, 0x31, 0xbb,
  32989. 0x36, 0x50, 0xae, 0x34, 0xcc, 0xca, 0x0c, 0x5b, },
  32990. { 0x27, 0xd9, 0x34, 0x37, 0xef, 0xb7, 0x21, 0xaa,
  32991. 0x40, 0x18, 0x21, 0xdc, 0xec, 0x5a, 0xdf, 0x89, },
  32992. { 0x89, 0x23, 0x7d, 0x9d, 0xed, 0x9c, 0x5e, 0x78,
  32993. 0xd8, 0xb1, 0xc9, 0xb1, 0x66, 0xcc, 0x73, 0x42, },
  32994. { 0x4a, 0x6d, 0x80, 0x91, 0xbf, 0x5e, 0x7d, 0x65,
  32995. 0x11, 0x89, 0xfa, 0x94, 0xa2, 0x50, 0xb1, 0x4c, },
  32996. { 0x0e, 0x33, 0xf9, 0x60, 0x55, 0xe7, 0xae, 0x89,
  32997. 0x3f, 0xfc, 0x0e, 0x3d, 0xcf, 0x49, 0x29, 0x02, },
  32998. { 0xe6, 0x1c, 0x43, 0x2b, 0x72, 0x0b, 0x19, 0xd1,
  32999. 0x8e, 0xc8, 0xd8, 0x4b, 0xdc, 0x63, 0x15, 0x1b, },
  33000. { 0xf7, 0xe5, 0xae, 0xf5, 0x49, 0xf7, 0x82, 0xcf,
  33001. 0x37, 0x90, 0x55, 0xa6, 0x08, 0x26, 0x9b, 0x16, },
  33002. { 0x43, 0x8d, 0x03, 0x0f, 0xd0, 0xb7, 0xa5, 0x4f,
  33003. 0xa8, 0x37, 0xf2, 0xad, 0x20, 0x1a, 0x64, 0x03, },
  33004. { 0xa5, 0x90, 0xd3, 0xee, 0x4f, 0xbf, 0x04, 0xe3,
  33005. 0x24, 0x7e, 0x0d, 0x27, 0xf2, 0x86, 0x42, 0x3f, },
  33006. { 0x5f, 0xe2, 0xc1, 0xa1, 0x72, 0xfe, 0x93, 0xc4,
  33007. 0xb1, 0x5c, 0xd3, 0x7c, 0xae, 0xf9, 0xf5, 0x38, },
  33008. { 0x2c, 0x97, 0x32, 0x5c, 0xbd, 0x06, 0xb3, 0x6e,
  33009. 0xb2, 0x13, 0x3d, 0xd0, 0x8b, 0x3a, 0x01, 0x7c, },
  33010. { 0x92, 0xc8, 0x14, 0x22, 0x7a, 0x6b, 0xca, 0x94,
  33011. 0x9f, 0xf0, 0x65, 0x9f, 0x00, 0x2a, 0xd3, 0x9e, },
  33012. { 0xdc, 0xe8, 0x50, 0x11, 0x0b, 0xd8, 0x32, 0x8c,
  33013. 0xfb, 0xd5, 0x08, 0x41, 0xd6, 0x91, 0x1d, 0x87, },
  33014. { 0x67, 0xf1, 0x49, 0x84, 0xc7, 0xda, 0x79, 0x12,
  33015. 0x48, 0xe3, 0x2b, 0xb5, 0x92, 0x25, 0x83, 0xda, },
  33016. { 0x19, 0x38, 0xf2, 0xcf, 0x72, 0xd5, 0x4e, 0xe9,
  33017. 0x7e, 0x94, 0x16, 0x6f, 0xa9, 0x1d, 0x2a, 0x36, },
  33018. { 0x74, 0x48, 0x1e, 0x96, 0x46, 0xed, 0x49, 0xfe,
  33019. 0x0f, 0x62, 0x24, 0x30, 0x16, 0x04, 0x69, 0x8e, },
  33020. { 0x57, 0xfc, 0xa5, 0xde, 0x98, 0xa9, 0xd6, 0xd8,
  33021. 0x00, 0x64, 0x38, 0xd0, 0x58, 0x3d, 0x8a, 0x1d, },
  33022. { 0x9f, 0xec, 0xde, 0x1c, 0xef, 0xdc, 0x1c, 0xbe,
  33023. 0xd4, 0x76, 0x36, 0x74, 0xd9, 0x57, 0x53, 0x59, },
  33024. { 0xe3, 0x04, 0x0c, 0x00, 0xeb, 0x28, 0xf1, 0x53,
  33025. 0x66, 0xca, 0x73, 0xcb, 0xd8, 0x72, 0xe7, 0x40, },
  33026. { 0x76, 0x97, 0x00, 0x9a, 0x6a, 0x83, 0x1d, 0xfe,
  33027. 0xcc, 0xa9, 0x1c, 0x59, 0x93, 0x67, 0x0f, 0x7a, },
  33028. { 0x58, 0x53, 0x54, 0x23, 0x21, 0xf5, 0x67, 0xa0,
  33029. 0x05, 0xd5, 0x47, 0xa4, 0xf0, 0x47, 0x59, 0xbd, },
  33030. { 0x51, 0x50, 0xd1, 0x77, 0x2f, 0x50, 0x83, 0x4a,
  33031. 0x50, 0x3e, 0x06, 0x9a, 0x97, 0x3f, 0xbd, 0x7c, },
  33032. };
  33033. #endif
  33034. WOLFSSL_TEST_SUBROUTINE int siphash_test(void)
  33035. {
  33036. int ret = 0;
  33037. int i;
  33038. #if WOLFSSL_SIPHASH_CROUNDS == 2 && WOLFSSL_SIPHASH_DROUNDS == 4
  33039. unsigned char res[SIPHASH_MAC_SIZE_16];
  33040. SipHash siphash;
  33041. for (i = 0; i < 64; i++) {
  33042. ret = wc_InitSipHash(&siphash, siphash_key, SIPHASH_MAC_SIZE_8);
  33043. if (ret != 0)
  33044. return -12100 - i;
  33045. ret = wc_SipHashUpdate(&siphash, siphash_msg, i);
  33046. if (ret != 0)
  33047. return -12200 - i;
  33048. ret = wc_SipHashFinal(&siphash, res, SIPHASH_MAC_SIZE_8);
  33049. if (ret != 0)
  33050. return -12300 - i;
  33051. if (XMEMCMP(res, siphash_r8[i], SIPHASH_MAC_SIZE_8) != 0)
  33052. return -12400 - i;
  33053. ret = wc_SipHash(siphash_key, siphash_msg, i, res, SIPHASH_MAC_SIZE_8);
  33054. if (ret != 0)
  33055. return -12500 - i;
  33056. if (XMEMCMP(res, siphash_r8[i], SIPHASH_MAC_SIZE_8) != 0)
  33057. return -12600 - i;
  33058. }
  33059. for (i = 0; i < 64; i++) {
  33060. ret = wc_InitSipHash(&siphash, siphash_key, SIPHASH_MAC_SIZE_16);
  33061. if (ret != 0)
  33062. return -12700 - i;
  33063. ret = wc_SipHashUpdate(&siphash, siphash_msg, i);
  33064. if (ret != 0)
  33065. return -12800 - i;
  33066. ret = wc_SipHashFinal(&siphash, res, SIPHASH_MAC_SIZE_16);
  33067. if (ret != 0)
  33068. return -12900 - i;
  33069. if (XMEMCMP(res, siphash_r16[i], SIPHASH_MAC_SIZE_16) != 0)
  33070. return -13000 - i;
  33071. ret = wc_SipHash(siphash_key, siphash_msg, i, res, SIPHASH_MAC_SIZE_16);
  33072. if (ret != 0)
  33073. return -13100 - i;
  33074. if (XMEMCMP(res, siphash_r16[i], SIPHASH_MAC_SIZE_16) != 0)
  33075. return -13200 - i;
  33076. }
  33077. #endif
  33078. /* Testing bad parameters. */
  33079. ret = wc_InitSipHash(NULL, NULL, SIPHASH_MAC_SIZE_8);
  33080. if (ret != BAD_FUNC_ARG)
  33081. return -13300;
  33082. ret = wc_InitSipHash(NULL, siphash_key, SIPHASH_MAC_SIZE_8);
  33083. if (ret != BAD_FUNC_ARG)
  33084. return -13301;
  33085. ret = wc_InitSipHash(&siphash, NULL, SIPHASH_MAC_SIZE_8);
  33086. if (ret != BAD_FUNC_ARG)
  33087. return -13302;
  33088. ret = wc_InitSipHash(&siphash, siphash_key, 7);
  33089. if (ret != BAD_FUNC_ARG)
  33090. return -13303;
  33091. ret = wc_InitSipHash(&siphash, siphash_key, SIPHASH_MAC_SIZE_8);
  33092. if (ret != 0)
  33093. return -13304;
  33094. ret = wc_SipHashUpdate(NULL, NULL, 0);
  33095. if (ret != BAD_FUNC_ARG)
  33096. return -13305;
  33097. ret = wc_SipHashUpdate(&siphash, NULL, 1);
  33098. if (ret != BAD_FUNC_ARG)
  33099. return -13306;
  33100. ret = wc_SipHashFinal(NULL, NULL, SIPHASH_MAC_SIZE_8);
  33101. if (ret != BAD_FUNC_ARG)
  33102. return -13307;
  33103. ret = wc_SipHashFinal(&siphash, NULL, SIPHASH_MAC_SIZE_8);
  33104. if (ret != BAD_FUNC_ARG)
  33105. return -13308;
  33106. ret = wc_SipHashFinal(NULL, res, SIPHASH_MAC_SIZE_8);
  33107. if (ret != BAD_FUNC_ARG)
  33108. return -13309;
  33109. ret = wc_SipHashFinal(&siphash, res, SIPHASH_MAC_SIZE_16);
  33110. if (ret != BAD_FUNC_ARG)
  33111. return -13310;
  33112. ret = wc_SipHash(NULL, NULL, 0, NULL, SIPHASH_MAC_SIZE_16);
  33113. if (ret != BAD_FUNC_ARG)
  33114. return -13311;
  33115. ret = wc_SipHash(siphash_key, NULL, 0, NULL, SIPHASH_MAC_SIZE_16);
  33116. if (ret != BAD_FUNC_ARG)
  33117. return -13312;
  33118. ret = wc_SipHash(NULL, NULL, 0, res, SIPHASH_MAC_SIZE_16);
  33119. if (ret != BAD_FUNC_ARG)
  33120. return -13313;
  33121. ret = wc_SipHash(siphash_key, NULL, 0, res, 15);
  33122. if (ret != BAD_FUNC_ARG)
  33123. return -13314;
  33124. ret = wc_SipHash(siphash_key, NULL, 1, res, SIPHASH_MAC_SIZE_16);
  33125. if (ret != BAD_FUNC_ARG)
  33126. return -13315;
  33127. return 0;
  33128. }
  33129. #endif /* WOLFSSL_SIPHASH */
  33130. #ifdef HAVE_LIBZ
  33131. static const byte sample_text[] =
  33132. "Biodiesel cupidatat marfa, cliche aute put a bird on it incididunt elit\n"
  33133. "polaroid. Sunt tattooed bespoke reprehenderit. Sint twee organic id\n"
  33134. "marfa. Commodo veniam ad esse gastropub. 3 wolf moon sartorial vero,\n"
  33135. "plaid delectus biodiesel squid +1 vice. Post-ironic keffiyeh leggings\n"
  33136. "selfies cray fap hoodie, forage anim. Carles cupidatat shoreditch, VHS\n"
  33137. "small batch meggings kogi dolore food truck bespoke gastropub.\n"
  33138. "\n"
  33139. "Terry richardson adipisicing actually typewriter tumblr, twee whatever\n"
  33140. "four loko you probably haven't heard of them high life. Messenger bag\n"
  33141. "whatever tattooed deep v mlkshk. Brooklyn pinterest assumenda chillwave\n"
  33142. "et, banksy ullamco messenger bag umami pariatur direct trade forage.\n"
  33143. "Typewriter culpa try-hard, pariatur sint brooklyn meggings. Gentrify\n"
  33144. "food truck next level, tousled irony non semiotics PBR ethical anim cred\n"
  33145. "readymade. Mumblecore brunch lomo odd future, portland organic terry\n"
  33146. "richardson elit leggings adipisicing ennui raw denim banjo hella. Godard\n"
  33147. "mixtape polaroid, pork belly readymade organic cray typewriter helvetica\n"
  33148. "four loko whatever street art yr farm-to-table.\n"
  33149. "\n"
  33150. "Vinyl keytar vice tofu. Locavore you probably haven't heard of them pug\n"
  33151. "pickled, hella tonx labore truffaut DIY mlkshk elit cosby sweater sint\n"
  33152. "et mumblecore. Elit swag semiotics, reprehenderit DIY sartorial nisi ugh\n"
  33153. "nesciunt pug pork belly wayfarers selfies delectus. Ethical hoodie\n"
  33154. "seitan fingerstache kale chips. Terry richardson artisan williamsburg,\n"
  33155. "eiusmod fanny pack irony tonx ennui lo-fi incididunt tofu YOLO\n"
  33156. "readymade. 8-bit sed ethnic beard officia. Pour-over iphone DIY butcher,\n"
  33157. "ethnic art party qui letterpress nisi proident jean shorts mlkshk\n"
  33158. "locavore.\n"
  33159. "\n"
  33160. "Narwhal flexitarian letterpress, do gluten-free voluptate next level\n"
  33161. "banh mi tonx incididunt carles DIY. Odd future nulla 8-bit beard ut\n"
  33162. "cillum pickled velit, YOLO officia you probably haven't heard of them\n"
  33163. "trust fund gastropub. Nisi adipisicing tattooed, Austin mlkshk 90's\n"
  33164. "small batch american apparel. Put a bird on it cosby sweater before they\n"
  33165. "sold out pork belly kogi hella. Street art mollit sustainable polaroid,\n"
  33166. "DIY ethnic ea pug beard dreamcatcher cosby sweater magna scenester nisi.\n"
  33167. "Sed pork belly skateboard mollit, labore proident eiusmod. Sriracha\n"
  33168. "excepteur cosby sweater, anim deserunt laborum eu aliquip ethical et\n"
  33169. "neutra PBR selvage.\n"
  33170. "\n"
  33171. "Raw denim pork belly truffaut, irony plaid sustainable put a bird on it\n"
  33172. "next level jean shorts exercitation. Hashtag keytar whatever, nihil\n"
  33173. "authentic aliquip disrupt laborum. Tattooed selfies deserunt trust fund\n"
  33174. "wayfarers. 3 wolf moon synth church-key sartorial, gastropub leggings\n"
  33175. "tattooed. Labore high life commodo, meggings raw denim fingerstache pug\n"
  33176. "trust fund leggings seitan forage. Nostrud ullamco duis, reprehenderit\n"
  33177. "incididunt flannel sustainable helvetica pork belly pug banksy you\n"
  33178. "probably haven't heard of them nesciunt farm-to-table. Disrupt nostrud\n"
  33179. "mollit magna, sriracha sartorial helvetica.\n"
  33180. "\n"
  33181. "Nulla kogi reprehenderit, skateboard sustainable duis adipisicing viral\n"
  33182. "ad fanny pack salvia. Fanny pack trust fund you probably haven't heard\n"
  33183. "of them YOLO vice nihil. Keffiyeh cray lo-fi pinterest cardigan aliqua,\n"
  33184. "reprehenderit aute. Culpa tousled williamsburg, marfa lomo actually anim\n"
  33185. "skateboard. Iphone aliqua ugh, semiotics pariatur vero readymade\n"
  33186. "organic. Marfa squid nulla, in laborum disrupt laboris irure gastropub.\n"
  33187. "Veniam sunt food truck leggings, sint vinyl fap.\n"
  33188. "\n"
  33189. "Hella dolore pork belly, truffaut carles you probably haven't heard of\n"
  33190. "them PBR helvetica in sapiente. Fashion axe ugh bushwick american\n"
  33191. "apparel. Fingerstache sed iphone, jean shorts blue bottle nisi bushwick\n"
  33192. "flexitarian officia veniam plaid bespoke fap YOLO lo-fi. Blog\n"
  33193. "letterpress mumblecore, food truck id cray brooklyn cillum ad sed.\n"
  33194. "Assumenda chambray wayfarers vinyl mixtape sustainable. VHS vinyl\n"
  33195. "delectus, culpa williamsburg polaroid cliche swag church-key synth kogi\n"
  33196. "magna pop-up literally. Swag thundercats ennui shoreditch vegan\n"
  33197. "pitchfork neutra truffaut etsy, sed single-origin coffee craft beer.\n"
  33198. "\n"
  33199. "Odio letterpress brooklyn elit. Nulla single-origin coffee in occaecat\n"
  33200. "meggings. Irony meggings 8-bit, chillwave lo-fi adipisicing cred\n"
  33201. "dreamcatcher veniam. Put a bird on it irony umami, trust fund bushwick\n"
  33202. "locavore kale chips. Sriracha swag thundercats, chillwave disrupt\n"
  33203. "tousled beard mollit mustache leggings portland next level. Nihil esse\n"
  33204. "est, skateboard art party etsy thundercats sed dreamcatcher ut iphone\n"
  33205. "swag consectetur et. Irure skateboard banjo, nulla deserunt messenger\n"
  33206. "bag dolor terry richardson sapiente.\n";
  33207. static const byte sample_text_gz[] = {
  33208. 0x1F, 0x8B, 0x08, 0x08, 0xC5, 0x49, 0xB5, 0x5B, 0x00, 0x03, 0x63, 0x69, 0x70,
  33209. 0x68, 0x65, 0x72, 0x74, 0x65, 0x78, 0x74, 0x2E, 0x74, 0x78, 0x74, 0x00, 0x8D,
  33210. 0x58, 0xCB, 0x92, 0xE4, 0xB6, 0x11, 0xBC, 0xE3, 0x2B, 0xEA, 0xA6, 0x83, 0xD9,
  33211. 0x1D, 0x72, 0xF8, 0x22, 0x1F, 0xB5, 0x96, 0xA5, 0xDD, 0x90, 0xBC, 0xAB, 0xD0,
  33212. 0x28, 0x36, 0x42, 0x47, 0x90, 0x2C, 0x36, 0xA1, 0x06, 0x09, 0x0A, 0x8F, 0xEE,
  33213. 0xE1, 0xDF, 0x3B, 0x0B, 0xE0, 0x73, 0x2C, 0x4B, 0xBA, 0xCD, 0xCE, 0x80, 0x78,
  33214. 0x64, 0x65, 0x65, 0x66, 0xED, 0x3B, 0xE3, 0x5A, 0xC3, 0x81, 0x2D, 0x35, 0x69,
  33215. 0x32, 0xAD, 0x8E, 0x3A, 0xD2, 0xA0, 0x7D, 0xA7, 0x2B, 0x6A, 0xAC, 0x69, 0x7A,
  33216. 0x26, 0x9D, 0x22, 0xD3, 0x94, 0x22, 0x69, 0xAA, 0x8D, 0x6F, 0xC9, 0x8D, 0x64,
  33217. 0x22, 0x99, 0xB1, 0x31, 0xAD, 0x69, 0xD3, 0x18, 0x89, 0xAD, 0x89, 0x6A, 0x72,
  33218. 0x56, 0x7B, 0x67, 0xDA, 0x2B, 0xBD, 0xC8, 0xEF, 0xB0, 0x4D, 0x74, 0x8E, 0x5B,
  33219. 0xAA, 0x39, 0x4C, 0xEE, 0xCE, 0xE4, 0x79, 0xF2, 0xDC, 0xF3, 0xD8, 0xB2, 0x37,
  33220. 0x11, 0x8B, 0x8C, 0x2C, 0x7A, 0x32, 0x93, 0xF3, 0x37, 0x3D, 0x9A, 0x86, 0x4C,
  33221. 0xAB, 0xF2, 0xB9, 0x57, 0xFA, 0x97, 0x1B, 0x06, 0xD7, 0x3A, 0x7A, 0xF0, 0x68,
  33222. 0xF4, 0x40, 0xBA, 0x25, 0x0E, 0x81, 0xE9, 0xA6, 0x43, 0xF4, 0x6E, 0x4A, 0xF5,
  33223. 0x95, 0xFE, 0x41, 0x4F, 0x67, 0x3B, 0x1A, 0x1C, 0xEE, 0x12, 0xB4, 0x8F, 0xCE,
  33224. 0x1B, 0x6D, 0xB1, 0xDE, 0xBB, 0x4A, 0x4D, 0x56, 0x9B, 0x96, 0x5A, 0xB6, 0xDC,
  33225. 0xC4, 0x14, 0x70, 0xE5, 0xF5, 0x7D, 0xE1, 0xB7, 0x84, 0x3F, 0xFC, 0xED, 0xEF,
  33226. 0xF4, 0x30, 0x0D, 0x5F, 0xE9, 0x47, 0x17, 0xE2, 0xC5, 0x78, 0x27, 0x67, 0xDF,
  33227. 0xB9, 0xEB, 0xCC, 0xCC, 0x3D, 0x59, 0xBE, 0xDD, 0xCC, 0x78, 0x0B, 0x0A, 0x1F,
  33228. 0x74, 0xF8, 0x8C, 0x1A, 0xAF, 0x67, 0xEA, 0xF4, 0x44, 0xBD, 0x93, 0x7D, 0x2A,
  33229. 0xEA, 0x9C, 0xD7, 0x37, 0x80, 0x32, 0x9A, 0x01, 0x37, 0xD5, 0xDE, 0xCA, 0xA2,
  33230. 0x0D, 0xB9, 0xD0, 0x3B, 0xCF, 0xAD, 0x89, 0x4D, 0x5F, 0xD1, 0xE7, 0xF7, 0x2F,
  33231. 0x2A, 0x0C, 0xDA, 0x5A, 0xAA, 0x35, 0x7E, 0x41, 0xC3, 0xB2, 0x37, 0xDD, 0xDD,
  33232. 0xCD, 0x50, 0xEB, 0x2C, 0x96, 0x62, 0x3B, 0xD7, 0x52, 0xF4, 0xA9, 0xB9, 0x6F,
  33233. 0x48, 0xED, 0xEF, 0x54, 0xEA, 0x67, 0xF6, 0x7E, 0x26, 0x8F, 0x3A, 0x68, 0xDF,
  33234. 0x06, 0xBC, 0x56, 0xB7, 0x66, 0x32, 0xC1, 0x34, 0xD8, 0x88, 0x34, 0x1E, 0x88,
  33235. 0xED, 0x67, 0x8A, 0xF3, 0xC4, 0x4F, 0xC0, 0xCA, 0x9E, 0x62, 0x1A, 0x6A, 0xEB,
  33236. 0xAB, 0x02, 0xED, 0xB3, 0xD7, 0x91, 0x81, 0x8A, 0xEA, 0x5C, 0xF2, 0x64, 0xDD,
  33237. 0xDD, 0xD1, 0xEC, 0x12, 0x4D, 0xDE, 0xD5, 0xBA, 0xC6, 0x77, 0xBD, 0x06, 0xC4,
  33238. 0x5F, 0x44, 0xEA, 0x59, 0x4B, 0x5D, 0x3B, 0x8A, 0x3D, 0x0F, 0xD4, 0x9B, 0x1B,
  33239. 0x80, 0x30, 0x1D, 0x30, 0xFA, 0x8F, 0x00, 0x3F, 0xDE, 0xB0, 0x6F, 0xAD, 0x6F,
  33240. 0x6A, 0xDD, 0x6E, 0x2F, 0x6E, 0xCB, 0x3C, 0xD1, 0x83, 0x06, 0x7B, 0x0F, 0xFD,
  33241. 0xFD, 0x4A, 0xEF, 0xBC, 0x73, 0x77, 0x3B, 0x8F, 0x34, 0xA1, 0xBA, 0xEC, 0x39,
  33242. 0x80, 0x33, 0x21, 0xA4, 0x01, 0x55, 0xD7, 0xD4, 0xF4, 0xC6, 0xDA, 0x27, 0x4E,
  33243. 0x54, 0x1C, 0x2B, 0xEC, 0x37, 0xDE, 0xC3, 0x4C, 0xC9, 0x5A, 0x3D, 0x34, 0x0E,
  33244. 0xD8, 0x1C, 0x0E, 0xA2, 0x34, 0xE8, 0xC1, 0xD0, 0xA4, 0x51, 0xD5, 0x88, 0x8B,
  33245. 0xB7, 0xC6, 0xA3, 0x96, 0x40, 0x49, 0xB7, 0xBC, 0xE0, 0x7F, 0x55, 0x3F, 0xEF,
  33246. 0x6F, 0x6E, 0x92, 0x9D, 0x34, 0xFE, 0x3C, 0x5F, 0x04, 0xA5, 0x6A, 0xFF, 0x30,
  33247. 0x08, 0xC9, 0xEA, 0xF5, 0x52, 0x2B, 0xFE, 0x57, 0xFA, 0x8E, 0xC7, 0xE8, 0x4D,
  33248. 0x37, 0xAB, 0x03, 0xFA, 0x23, 0xBF, 0x46, 0x94, 0xFF, 0xC1, 0x16, 0xE0, 0xB9,
  33249. 0x14, 0x2C, 0x9E, 0x27, 0xEC, 0x98, 0x69, 0x14, 0x92, 0xF1, 0x60, 0x5C, 0x34,
  33250. 0x4D, 0xA0, 0x1F, 0xDF, 0xFD, 0x44, 0x1C, 0x7B, 0xD3, 0x80, 0x70, 0x42, 0x02,
  33251. 0x30, 0x84, 0x5B, 0xE5, 0x59, 0xB7, 0xF3, 0x80, 0xFB, 0x01, 0x33, 0xA9, 0x00,
  33252. 0x37, 0x52, 0xDC, 0xDA, 0xA7, 0x11, 0x85, 0xB7, 0x6E, 0x70, 0xE4, 0xDA, 0x96,
  33253. 0xBA, 0x84, 0x5B, 0x81, 0x43, 0x93, 0xF3, 0xD1, 0xEA, 0xB1, 0xDD, 0xB8, 0x1F,
  33254. 0xA5, 0xCC, 0xEA, 0x50, 0x66, 0x69, 0xA9, 0x8D, 0x8C, 0xA7, 0xA2, 0xF3, 0x38,
  33255. 0x26, 0x43, 0x5E, 0x3F, 0x01, 0xBE, 0x1C, 0x0F, 0x20, 0x7F, 0x75, 0xA8, 0x20,
  33256. 0x80, 0xC4, 0xC3, 0x5C, 0x8B, 0x0D, 0xD4, 0x60, 0x5E, 0xA3, 0x9E, 0xD0, 0xB4,
  33257. 0x4B, 0x4F, 0xE6, 0x13, 0x85, 0x60, 0x42, 0x96, 0xED, 0xAA, 0xDB, 0xE9, 0x99,
  33258. 0xE3, 0x07, 0x0E, 0x61, 0xB3, 0x07, 0xE3, 0xB1, 0xFA, 0xC0, 0x9B, 0xAD, 0xF6,
  33259. 0xE0, 0x26, 0x33, 0xEA, 0xEA, 0x23, 0xCD, 0x1E, 0x9D, 0xE1, 0x87, 0x4B, 0x74,
  33260. 0x97, 0x08, 0x3E, 0xA1, 0x28, 0xEA, 0xB3, 0x19, 0x67, 0x8B, 0x76, 0x9A, 0xA3,
  33261. 0xF6, 0xB9, 0xCF, 0x80, 0x65, 0x97, 0xAE, 0xF4, 0x83, 0x6B, 0xF4, 0x43, 0x20,
  33262. 0xF9, 0x0B, 0xFC, 0x9B, 0xD2, 0x4D, 0x4D, 0xA6, 0xB9, 0xA3, 0x02, 0x55, 0x79,
  33263. 0x18, 0x36, 0x19, 0x5F, 0xC9, 0xEA, 0x5A, 0x76, 0x40, 0xB9, 0xBA, 0x0E, 0x9A,
  33264. 0x44, 0xDF, 0x7C, 0xF8, 0x65, 0x61, 0x5E, 0x81, 0xAB, 0x71, 0xA1, 0x9E, 0x29,
  33265. 0x3C, 0x59, 0xCB, 0x23, 0xA4, 0xF6, 0x60, 0x1A, 0x0D, 0x5B, 0x39, 0xAE, 0xF4,
  33266. 0x6F, 0x59, 0x16, 0x9E, 0x60, 0xD8, 0x56, 0xCF, 0xEA, 0x2C, 0x4C, 0x79, 0xD3,
  33267. 0x5D, 0x51, 0x46, 0xA0, 0x4E, 0xE9, 0xD6, 0xAB, 0x91, 0x43, 0x63, 0x44, 0xD7,
  33268. 0x70, 0xB9, 0x23, 0x98, 0x4F, 0x3D, 0x03, 0x02, 0xF6, 0x81, 0x56, 0xC1, 0x58,
  33269. 0x85, 0x07, 0xA7, 0x2D, 0x2C, 0x29, 0xCA, 0x01, 0x45, 0x31, 0x51, 0x8F, 0xD4,
  33270. 0x19, 0xA1, 0x79, 0x88, 0x5A, 0xA4, 0xF5, 0xAE, 0x2D, 0x4B, 0x63, 0x4C, 0x58,
  33271. 0xFE, 0xBF, 0xAD, 0xEE, 0xA3, 0x09, 0xF8, 0xE2, 0x89, 0xBE, 0x81, 0x0E, 0x86,
  33272. 0x3A, 0xF9, 0x5B, 0xA5, 0xD8, 0xA4, 0x00, 0x75, 0x04, 0xF2, 0x23, 0xB8, 0x39,
  33273. 0x69, 0x50, 0xB7, 0xD0, 0x34, 0x63, 0x54, 0xD8, 0x61, 0xDD, 0xA5, 0x33, 0x47,
  33274. 0x85, 0x96, 0x22, 0xD0, 0x2F, 0x9F, 0x7E, 0xF8, 0x74, 0x24, 0xEA, 0x57, 0x97,
  33275. 0x5A, 0xE0, 0x00, 0xCF, 0xC1, 0x67, 0xE1, 0x41, 0xBD, 0x94, 0xA1, 0x03, 0xD3,
  33276. 0xB4, 0x08, 0x64, 0xF2, 0x17, 0x27, 0x35, 0x37, 0x53, 0xEF, 0x46, 0xCE, 0xD8,
  33277. 0xD4, 0x09, 0x52, 0xC6, 0x1E, 0xF7, 0x28, 0xDF, 0x08, 0x0F, 0xD0, 0x6F, 0x71,
  33278. 0xA6, 0xDF, 0xE4, 0x60, 0x8E, 0xC0, 0x1E, 0x78, 0x86, 0x50, 0xB0, 0x9B, 0x84,
  33279. 0x7E, 0xE8, 0x36, 0xFA, 0x95, 0xF1, 0x12, 0x51, 0xC7, 0x18, 0x96, 0xA2, 0x29,
  33280. 0xBB, 0x70, 0x02, 0xB4, 0xF9, 0xA8, 0x3D, 0x08, 0x66, 0xA9, 0xB3, 0xFC, 0x0A,
  33281. 0x94, 0x80, 0xFD, 0x78, 0xDC, 0xAB, 0x82, 0x5A, 0xD2, 0xCD, 0xC2, 0x87, 0xC6,
  33282. 0x4B, 0x07, 0xFA, 0xD1, 0xC3, 0xD9, 0x34, 0x41, 0x85, 0xF8, 0xD0, 0xB6, 0x0A,
  33283. 0x9D, 0x00, 0x91, 0x35, 0x05, 0x88, 0xC3, 0xE3, 0x9B, 0x22, 0xD2, 0xB8, 0xFD,
  33284. 0x95, 0x3E, 0x6D, 0x5D, 0x48, 0xA3, 0x68, 0xCF, 0x02, 0x42, 0x79, 0x79, 0x8A,
  33285. 0xAA, 0x01, 0xD6, 0x09, 0x14, 0x2C, 0xF4, 0x83, 0xA3, 0x80, 0x31, 0x55, 0x46,
  33286. 0x6E, 0xC5, 0xE5, 0x2F, 0x30, 0x58, 0x81, 0xA2, 0x90, 0xBE, 0x2E, 0xA1, 0xC3,
  33287. 0x0F, 0xA6, 0xF5, 0x51, 0x00, 0x39, 0xB6, 0xF2, 0x2A, 0xA3, 0x15, 0x7D, 0x8D,
  33288. 0xF5, 0x66, 0x5C, 0xD9, 0xFC, 0xCF, 0x2F, 0xBF, 0x08, 0x27, 0xE7, 0xD0, 0x03,
  33289. 0xB8, 0xD9, 0x00, 0x13, 0x3D, 0x01, 0x6B, 0xB6, 0xA8, 0xCD, 0x5B, 0x3B, 0x3E,
  33290. 0x93, 0xBF, 0xE6, 0x2E, 0xB7, 0x4A, 0xCF, 0xB3, 0x0A, 0xCE, 0x62, 0x11, 0xD6,
  33291. 0x1F, 0x68, 0x9B, 0x1D, 0x68, 0xD1, 0x8C, 0x97, 0xBD, 0xA1, 0x07, 0x67, 0x73,
  33292. 0x87, 0xE0, 0x36, 0xDA, 0x8C, 0xD2, 0xD2, 0xBB, 0x84, 0x28, 0xA9, 0xFE, 0x52,
  33293. 0x74, 0xD6, 0xB9, 0x0F, 0x0A, 0x6A, 0x2D, 0x28, 0x35, 0x34, 0x3A, 0xD3, 0xE2,
  33294. 0xCD, 0x35, 0x06, 0x7D, 0x1B, 0x35, 0x85, 0x86, 0xD1, 0x3E, 0xF2, 0x6F, 0xA1,
  33295. 0xC4, 0x55, 0xBD, 0x00, 0xD8, 0xC3, 0x5D, 0xC2, 0x1D, 0x6B, 0x6B, 0x27, 0x5B,
  33296. 0x95, 0xF3, 0xAB, 0xB5, 0xD3, 0x37, 0xF2, 0x2C, 0x9C, 0xC7, 0x5D, 0xBD, 0xF1,
  33297. 0x68, 0x1C, 0xAD, 0xF8, 0xB5, 0xE1, 0x29, 0x72, 0x7A, 0x73, 0x62, 0x55, 0x24,
  33298. 0xB9, 0x85, 0xDF, 0x7B, 0x29, 0x7D, 0xDE, 0x08, 0xF5, 0xE4, 0x44, 0xDA, 0x1A,
  33299. 0x30, 0x74, 0xDA, 0xB4, 0x9B, 0x23, 0x9A, 0x3A, 0xC1, 0x53, 0xB2, 0xA2, 0xA3,
  33300. 0x7B, 0x1F, 0xD9, 0x56, 0xD4, 0x4F, 0x9B, 0xB2, 0x1E, 0xEE, 0xB8, 0x6A, 0x4E,
  33301. 0xB5, 0xF4, 0x5A, 0xC9, 0x18, 0x27, 0x9C, 0xDE, 0x14, 0x44, 0xED, 0xC4, 0x3C,
  33302. 0x71, 0x9F, 0x5F, 0xD9, 0x37, 0xA0, 0x78, 0x34, 0x6E, 0xBC, 0xD2, 0x7B, 0x1D,
  33303. 0xFA, 0x08, 0x39, 0x5A, 0x04, 0x73, 0x15, 0xD9, 0x0A, 0x48, 0xC1, 0x2D, 0x15,
  33304. 0x4E, 0x84, 0x30, 0x45, 0x69, 0xB3, 0xE5, 0xF6, 0xAD, 0x09, 0x1E, 0xCC, 0x5F,
  33305. 0x1F, 0x06, 0xD5, 0x58, 0xAD, 0x78, 0xD7, 0x9F, 0xE5, 0xED, 0x3B, 0x09, 0xD5,
  33306. 0xA6, 0x52, 0x6F, 0x92, 0xD3, 0x3C, 0xC6, 0x1E, 0xF2, 0x93, 0x7C, 0xD3, 0x5F,
  33307. 0x70, 0x85, 0x5D, 0xF8, 0xAA, 0x9D, 0xB7, 0x7B, 0x24, 0x5A, 0xE9, 0x0A, 0x35,
  33308. 0x2F, 0xF5, 0xD9, 0x82, 0x02, 0x8A, 0x90, 0x13, 0x5B, 0xB5, 0x67, 0x9C, 0xDD,
  33309. 0xA0, 0x4E, 0x82, 0x27, 0xDA, 0x7E, 0xE8, 0x8E, 0xCD, 0xE1, 0x56, 0x71, 0x2C,
  33310. 0xE6, 0x4E, 0x1F, 0x91, 0xCD, 0x7C, 0x6A, 0xB7, 0x78, 0xD0, 0x26, 0xF3, 0x56,
  33311. 0xA9, 0xD5, 0xA1, 0xC3, 0x3B, 0x98, 0xE9, 0x28, 0x09, 0xEF, 0x50, 0x90, 0xCD,
  33312. 0xC4, 0x8E, 0x75, 0xCC, 0xAC, 0x2D, 0xC9, 0x03, 0x6D, 0xAC, 0xFE, 0xC4, 0x88,
  33313. 0x36, 0xD1, 0x3F, 0xBB, 0x1C, 0x7D, 0xB3, 0x14, 0x61, 0x2C, 0xB7, 0x54, 0x4B,
  33314. 0xDB, 0x64, 0xB6, 0x57, 0x14, 0x16, 0x8E, 0x1E, 0x6C, 0x64, 0xBB, 0x8B, 0x48,
  33315. 0x5D, 0x96, 0x9D, 0xDC, 0x80, 0xA7, 0xF7, 0x54, 0xC7, 0x46, 0x38, 0x3E, 0x44,
  33316. 0xDE, 0x7E, 0x92, 0x8D, 0x07, 0xF6, 0x07, 0x37, 0x4E, 0x16, 0x10, 0xB4, 0x7D,
  33317. 0x88, 0x66, 0x7F, 0xBB, 0xFF, 0xEA, 0x00, 0xF3, 0xFF, 0x97, 0x2C, 0xB5, 0xBE,
  33318. 0x35, 0x4B, 0x5C, 0x36, 0xEC, 0x4C, 0xBD, 0x2B, 0x7D, 0xBF, 0x46, 0xE2, 0x9C,
  33319. 0x0E, 0x8A, 0xA3, 0xEC, 0xB1, 0x0E, 0x9A, 0xDA, 0x9A, 0x9B, 0x28, 0x92, 0x10,
  33320. 0x53, 0x57, 0xEA, 0xEC, 0xA2, 0x32, 0x32, 0x20, 0x1D, 0x97, 0x5C, 0xB6, 0x84,
  33321. 0xA9, 0x93, 0x8D, 0x95, 0x11, 0xA3, 0x24, 0xA3, 0x2D, 0xC6, 0x4A, 0xEF, 0xAA,
  33322. 0x1D, 0x85, 0x2B, 0x7D, 0x28, 0xBE, 0x53, 0xCE, 0x10, 0x1F, 0xAE, 0x0E, 0x41,
  33323. 0x6C, 0x4B, 0x79, 0x12, 0xFB, 0xF7, 0x54, 0xA3, 0x96, 0x54, 0x83, 0x20, 0x96,
  33324. 0x8F, 0x28, 0xA9, 0x3F, 0x8B, 0x3D, 0xBA, 0x77, 0xDC, 0x24, 0xE1, 0xD4, 0x49,
  33325. 0x40, 0xD8, 0x78, 0x31, 0x85, 0x43, 0xF6, 0xFE, 0x5C, 0xA6, 0x8F, 0x90, 0x09,
  33326. 0xB0, 0xE7, 0xC4, 0x95, 0xB2, 0x55, 0x49, 0x97, 0x8F, 0x1C, 0x78, 0x30, 0x20,
  33327. 0xA0, 0xB4, 0xEF, 0x73, 0x56, 0x59, 0x82, 0xFD, 0xCE, 0xBA, 0x6A, 0x8F, 0x2C,
  33328. 0x8B, 0x15, 0xFD, 0xA1, 0x85, 0xA8, 0x5C, 0x0F, 0x11, 0xA5, 0x9D, 0xC2, 0x46,
  33329. 0xC6, 0x9C, 0xC9, 0x40, 0x0B, 0x58, 0x6A, 0x1C, 0x7A, 0x23, 0xF9, 0xE0, 0x95,
  33330. 0x05, 0x13, 0x58, 0x72, 0xE8, 0x9F, 0x30, 0xAC, 0xCD, 0x26, 0xD4, 0x66, 0x13,
  33331. 0xDF, 0x1E, 0x7B, 0x4F, 0x9C, 0xBE, 0x38, 0x79, 0x75, 0x92, 0xA4, 0xDA, 0x26,
  33332. 0x44, 0x55, 0x17, 0xA3, 0xE5, 0x62, 0xDA, 0xEB, 0x86, 0xEA, 0x68, 0xC7, 0xAB,
  33333. 0xFD, 0x2D, 0x43, 0x59, 0x51, 0xC0, 0x75, 0x64, 0x91, 0x01, 0x29, 0x33, 0x28,
  33334. 0xF3, 0x04, 0x83, 0x80, 0x75, 0x37, 0x75, 0x0C, 0x03, 0x7B, 0x0A, 0xAB, 0x8E,
  33335. 0x60, 0x62, 0x8B, 0x4C, 0xAF, 0x2D, 0xA3, 0x2F, 0xFE, 0xAB, 0x45, 0xCF, 0xDA,
  33336. 0xAB, 0xFA, 0xFA, 0x30, 0x3D, 0xE8, 0xA1, 0x96, 0xA5, 0x7B, 0xE2, 0x2A, 0xD0,
  33337. 0xAF, 0x59, 0xF7, 0xD0, 0x32, 0x57, 0x19, 0xBD, 0xCA, 0x9F, 0xD5, 0x1A, 0xC7,
  33338. 0xAA, 0x65, 0x4A, 0x38, 0xB2, 0x70, 0x33, 0xB7, 0x75, 0xD2, 0xCD, 0xD1, 0xF0,
  33339. 0xA8, 0x87, 0x59, 0x20, 0xA5, 0x57, 0x55, 0xB1, 0xB2, 0xC9, 0x4D, 0x97, 0x34,
  33340. 0x41, 0xF3, 0xF0, 0x30, 0xA1, 0x2C, 0x1C, 0x49, 0x3E, 0x89, 0x7D, 0x12, 0xE2,
  33341. 0xC3, 0x04, 0xC3, 0x92, 0xC0, 0xF6, 0x39, 0x10, 0x80, 0x81, 0x8F, 0x08, 0xB4,
  33342. 0xF8, 0xB9, 0x13, 0x4E, 0x2C, 0xAE, 0xB3, 0x71, 0x82, 0x63, 0x98, 0xAB, 0x5C,
  33343. 0x1C, 0x10, 0xEA, 0x66, 0xF9, 0x02, 0x3A, 0x82, 0x61, 0xD0, 0xD4, 0xAE, 0x43,
  33344. 0xD4, 0x01, 0x3E, 0x9D, 0x04, 0x14, 0xF6, 0x60, 0xD8, 0xA7, 0xD6, 0xB8, 0x53,
  33345. 0xC8, 0xDA, 0x80, 0x93, 0xA0, 0x02, 0xDD, 0xCC, 0xE2, 0xF2, 0xBB, 0xFB, 0xE0,
  33346. 0x27, 0xD7, 0x34, 0x9A, 0x71, 0x49, 0xB5, 0x4F, 0x42, 0x1F, 0xB2, 0x9D, 0x6D,
  33347. 0xAA, 0x9D, 0xD3, 0x50, 0xB5, 0x8F, 0x6A, 0x4B, 0xDF, 0x1F, 0xD5, 0x27, 0x8F,
  33348. 0x3B, 0x27, 0xCF, 0x2F, 0x8C, 0xF8, 0x9D, 0x4C, 0x52, 0xBC, 0x32, 0x0F, 0x73,
  33349. 0xD5, 0x51, 0x8E, 0x36, 0x7E, 0xAD, 0x09, 0xF0, 0x94, 0x83, 0x5F, 0x36, 0xFD,
  33350. 0x7C, 0x03, 0xED, 0xF1, 0x5E, 0x4B, 0xF7, 0xAA, 0x55, 0x5C, 0x4A, 0x14, 0x59,
  33351. 0x85, 0x38, 0x2D, 0x8C, 0xDF, 0xEC, 0x65, 0x1B, 0xB8, 0x76, 0x57, 0x96, 0x3C,
  33352. 0x86, 0xED, 0xF2, 0x7F, 0x2D, 0x28, 0x48, 0xDA, 0x49, 0x7F, 0xF7, 0x54, 0x2B,
  33353. 0xD5, 0x39, 0xD5, 0x57, 0x0A, 0x75, 0x7A, 0x3E, 0x5E, 0x5D, 0xBA, 0x4A, 0x15,
  33354. 0xFA, 0xB8, 0x31, 0x80, 0x71, 0x2C, 0xCA, 0xC4, 0x51, 0x10, 0x16, 0x5D, 0x39,
  33355. 0xEC, 0x9D, 0x07, 0xB6, 0x6A, 0x89, 0x9F, 0x9B, 0x5B, 0x6F, 0x03, 0xB0, 0x92,
  33356. 0x01, 0x38, 0x6B, 0x48, 0x99, 0x0A, 0x8F, 0x13, 0xC1, 0xA6, 0x01, 0xEA, 0xBF,
  33357. 0x6F, 0x86, 0x43, 0x51, 0xB6, 0x11, 0x00, 0x00
  33358. };
  33359. WOLFSSL_TEST_SUBROUTINE int compress_test(void)
  33360. {
  33361. int ret = 0;
  33362. word32 dSz = sizeof(sample_text);
  33363. word32 cSz = (dSz + (word32)(dSz * 0.001) + 12);
  33364. byte *c;
  33365. byte *d;
  33366. c = (byte *)XMALLOC(cSz * sizeof(byte), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33367. d = (byte *)XMALLOC(dSz * sizeof(byte), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33368. if (c == NULL || d == NULL) {
  33369. ERROR_OUT(-12100, exit);
  33370. }
  33371. /* follow calloc and initialize to 0 */
  33372. XMEMSET(c, 0, cSz);
  33373. XMEMSET(d, 0, dSz);
  33374. if ((ret = wc_Compress(c, cSz, sample_text, dSz, 0)) < 0) {
  33375. ERROR_OUT(-12101, exit);
  33376. }
  33377. cSz = (word32)ret;
  33378. if ((ret = wc_DeCompress(d, dSz, c, cSz)) != (int)dSz) {
  33379. ERROR_OUT(-12102, exit);
  33380. }
  33381. dSz = (word32)ret;
  33382. if (XMEMCMP(d, sample_text, dSz) != 0) {
  33383. ERROR_OUT(-12103, exit);
  33384. }
  33385. /* GZIP tests */
  33386. cSz = (dSz + (word32)(dSz * 0.001) + 12); /* reset cSz */
  33387. XMEMSET(c, 0, cSz);
  33388. XMEMSET(d, 0, dSz);
  33389. ret = wc_Compress_ex(c, cSz, sample_text, dSz, 0, LIBZ_WINBITS_GZIP);
  33390. if (ret < 0) {
  33391. ERROR_OUT(-12104, exit);
  33392. }
  33393. cSz = (word32)ret;
  33394. ret = wc_DeCompress_ex(d, dSz, c, cSz, LIBZ_WINBITS_GZIP);
  33395. if (ret < 0) {
  33396. ERROR_OUT(-12105, exit);
  33397. }
  33398. if (XMEMCMP(d, sample_text, dSz) != 0) {
  33399. ERROR_OUT(-12106, exit);
  33400. }
  33401. /* Try with gzip generated output */
  33402. XMEMSET(d, 0, dSz);
  33403. ret = wc_DeCompress_ex(d, dSz, sample_text_gz, sizeof(sample_text_gz),
  33404. LIBZ_WINBITS_GZIP);
  33405. if (ret < 0) {
  33406. ERROR_OUT(-12107, exit);
  33407. }
  33408. dSz = (word32)ret;
  33409. if (XMEMCMP(d, sample_text, dSz) != 0) {
  33410. ERROR_OUT(-12108, exit);
  33411. }
  33412. ret = 0; /* success */
  33413. exit:
  33414. if (c) XFREE(c, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33415. if (d) XFREE(d, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33416. return ret;
  33417. }
  33418. #endif /* HAVE_LIBZ */
  33419. #ifdef HAVE_PKCS7
  33420. /* External Debugging/Testing Note:
  33421. *
  33422. * PKCS#7 test functions can output generated PKCS#7/CMS bundles for
  33423. * additional testing. To dump bundles to files DER encoded files, please
  33424. * define:
  33425. *
  33426. * #define PKCS7_OUTPUT_TEST_BUNDLES
  33427. */
  33428. /* Loads certs and keys for use with PKCS7 tests, from either files
  33429. * or buffers.
  33430. *
  33431. * rsaClientCertBuf - output buffer for RSA client cert
  33432. * rsaClientCertBufSz - IN/OUT size of output buffer, size of RSA client cert
  33433. * rsaClientPrivKeyBuf - output buffer for RSA client private key
  33434. * rsaClientPrivKeyBufSz - IN/OUT size of output buffer, size of RSA client key
  33435. *
  33436. * rsaServerCertBuf - output buffer for RSA server cert
  33437. * rsaServerCertBufSz - IN/OUT size of output buffer, size of RSA server cert
  33438. * rsaServerPrivKeyBuf - output buffer for RSA server private key
  33439. * rsaServerPrivKeyBufSz - IN/OUT size of output buffer, size of RSA server key
  33440. *
  33441. * rsaCaCertBuf - output buffer for RSA CA cert
  33442. * rsaCaCertBufSz - IN/OUT size of output buffer, size of RSA ca cert
  33443. * rsaCaPrivKeyBuf - output buffer for RSA CA private key
  33444. * rsaCaPrivKeyBufSz - IN/OUT size of output buffer, size of RSA CA key
  33445. *
  33446. * eccClientCertBuf - output buffer for ECC cert
  33447. * eccClientCertBufSz - IN/OUT size of output buffer, size of ECC cert
  33448. * eccClientPrivKeyBuf - output buffer for ECC private key
  33449. * eccClientPrivKeyBufSz - IN/OUT size of output buffer, size of ECC private key
  33450. *
  33451. * Returns 0 on success, negative on error
  33452. */
  33453. static int pkcs7_load_certs_keys(
  33454. byte* rsaClientCertBuf, word32* rsaClientCertBufSz,
  33455. byte* rsaClientPrivKeyBuf, word32* rsaClientPrivKeyBufSz,
  33456. byte* rsaServerCertBuf, word32* rsaServerCertBufSz,
  33457. byte* rsaServerPrivKeyBuf, word32* rsaServerPrivKeyBufSz,
  33458. byte* rsaCaCertBuf, word32* rsaCaCertBufSz,
  33459. byte* rsaCaPrivKeyBuf, word32* rsaCaPrivKeyBufSz,
  33460. byte* eccClientCertBuf, word32* eccClientCertBufSz,
  33461. byte* eccClientPrivKeyBuf, word32* eccClientPrivKeyBufSz)
  33462. {
  33463. #ifndef NO_FILESYSTEM
  33464. XFILE certFile;
  33465. XFILE keyFile;
  33466. (void)certFile;
  33467. (void)keyFile;
  33468. #endif
  33469. #ifndef NO_RSA
  33470. if (rsaClientCertBuf == NULL || rsaClientCertBufSz == NULL ||
  33471. rsaClientPrivKeyBuf == NULL || rsaClientPrivKeyBufSz == NULL)
  33472. return BAD_FUNC_ARG;
  33473. #endif
  33474. #ifdef HAVE_ECC
  33475. if (eccClientCertBuf == NULL || eccClientCertBufSz == NULL ||
  33476. eccClientPrivKeyBuf == NULL || eccClientPrivKeyBufSz == NULL)
  33477. return BAD_FUNC_ARG;
  33478. #endif
  33479. /* RSA */
  33480. #ifndef NO_RSA
  33481. #ifdef USE_CERT_BUFFERS_1024
  33482. if (*rsaClientCertBufSz < (word32)sizeof_client_cert_der_1024)
  33483. return -12110;
  33484. XMEMCPY(rsaClientCertBuf, client_cert_der_1024,
  33485. sizeof_client_cert_der_1024);
  33486. *rsaClientCertBufSz = sizeof_client_cert_der_1024;
  33487. if (rsaServerCertBuf != NULL) {
  33488. if (*rsaServerCertBufSz < (word32)sizeof_server_cert_der_1024)
  33489. return -12111;
  33490. XMEMCPY(rsaServerCertBuf, server_cert_der_1024,
  33491. sizeof_server_cert_der_1024);
  33492. *rsaServerCertBufSz = sizeof_server_cert_der_1024;
  33493. }
  33494. if (rsaCaCertBuf != NULL) {
  33495. if (*rsaCaCertBufSz < (word32)sizeof_ca_cert_der_1024)
  33496. return -12112;
  33497. XMEMCPY(rsaCaCertBuf, ca_cert_der_1024, sizeof_ca_cert_der_1024);
  33498. *rsaCaCertBufSz = sizeof_ca_cert_der_1024;
  33499. }
  33500. #elif defined(USE_CERT_BUFFERS_2048)
  33501. if (*rsaClientCertBufSz < (word32)sizeof_client_cert_der_2048)
  33502. return -12113;
  33503. XMEMCPY(rsaClientCertBuf, client_cert_der_2048,
  33504. sizeof_client_cert_der_2048);
  33505. *rsaClientCertBufSz = sizeof_client_cert_der_2048;
  33506. if (rsaServerCertBuf != NULL) {
  33507. if (*rsaServerCertBufSz < (word32)sizeof_server_cert_der_2048)
  33508. return -12114;
  33509. XMEMCPY(rsaServerCertBuf, server_cert_der_2048,
  33510. sizeof_server_cert_der_2048);
  33511. *rsaServerCertBufSz = sizeof_server_cert_der_2048;
  33512. }
  33513. if (rsaCaCertBuf != NULL) {
  33514. if (*rsaCaCertBufSz < (word32)sizeof_ca_cert_der_2048)
  33515. return -12115;
  33516. XMEMCPY(rsaCaCertBuf, ca_cert_der_2048, sizeof_ca_cert_der_2048);
  33517. *rsaCaCertBufSz = sizeof_ca_cert_der_2048;
  33518. }
  33519. #else
  33520. certFile = XFOPEN(clientCert, "rb");
  33521. if (!certFile)
  33522. return -12116;
  33523. *rsaClientCertBufSz = (word32)XFREAD(rsaClientCertBuf, 1,
  33524. *rsaClientCertBufSz, certFile);
  33525. XFCLOSE(certFile);
  33526. if (rsaServerCertBuf != NULL) {
  33527. certFile = XFOPEN(rsaServerCertDerFile, "rb");
  33528. if (!certFile)
  33529. return -12117;
  33530. *rsaServerCertBufSz = (word32)XFREAD(rsaServerCertBuf, 1,
  33531. *rsaServerCertBufSz, certFile);
  33532. XFCLOSE(certFile);
  33533. }
  33534. if (rsaCaCertBuf != NULL) {
  33535. certFile = XFOPEN(rsaCaCertDerFile, "rb");
  33536. if (!certFile)
  33537. return -12118;
  33538. *rsaCaCertBufSz = (word32)XFREAD(rsaCaCertBuf, 1, *rsaCaCertBufSz,
  33539. certFile);
  33540. XFCLOSE(certFile);
  33541. }
  33542. #endif
  33543. #ifdef USE_CERT_BUFFERS_1024
  33544. if (*rsaClientPrivKeyBufSz < (word32)sizeof_client_key_der_1024)
  33545. return -12119;
  33546. XMEMCPY(rsaClientPrivKeyBuf, client_key_der_1024,
  33547. sizeof_client_key_der_1024);
  33548. *rsaClientPrivKeyBufSz = sizeof_client_key_der_1024;
  33549. if (rsaServerPrivKeyBuf != NULL) {
  33550. if (*rsaServerPrivKeyBufSz < (word32)sizeof_server_key_der_1024)
  33551. return -12120;
  33552. XMEMCPY(rsaServerPrivKeyBuf, server_key_der_1024,
  33553. sizeof_server_key_der_1024);
  33554. *rsaServerPrivKeyBufSz = sizeof_server_key_der_1024;
  33555. }
  33556. if (rsaCaPrivKeyBuf != NULL) {
  33557. if (*rsaCaPrivKeyBufSz < (word32)sizeof_ca_key_der_1024)
  33558. return -12121;
  33559. XMEMCPY(rsaCaPrivKeyBuf, ca_key_der_1024, sizeof_ca_key_der_1024);
  33560. *rsaCaPrivKeyBufSz = sizeof_ca_key_der_1024;
  33561. }
  33562. #elif defined(USE_CERT_BUFFERS_2048)
  33563. if (*rsaClientPrivKeyBufSz < (word32)sizeof_client_key_der_2048)
  33564. return -12122;
  33565. XMEMCPY(rsaClientPrivKeyBuf, client_key_der_2048,
  33566. sizeof_client_key_der_2048);
  33567. *rsaClientPrivKeyBufSz = sizeof_client_key_der_2048;
  33568. if (rsaServerPrivKeyBuf != NULL) {
  33569. if (*rsaServerPrivKeyBufSz < (word32)sizeof_server_key_der_2048)
  33570. return -12123;
  33571. XMEMCPY(rsaServerPrivKeyBuf, server_key_der_2048,
  33572. sizeof_server_key_der_2048);
  33573. *rsaServerPrivKeyBufSz = sizeof_server_key_der_2048;
  33574. }
  33575. if (rsaCaPrivKeyBuf != NULL) {
  33576. if (*rsaCaPrivKeyBufSz < (word32)sizeof_ca_key_der_2048)
  33577. return -12124;
  33578. XMEMCPY(rsaCaPrivKeyBuf, ca_key_der_2048, sizeof_ca_key_der_2048);
  33579. *rsaCaPrivKeyBufSz = sizeof_ca_key_der_2048;
  33580. }
  33581. #else
  33582. keyFile = XFOPEN(clientKey, "rb");
  33583. if (!keyFile)
  33584. return -12125;
  33585. *rsaClientPrivKeyBufSz = (word32)XFREAD(rsaClientPrivKeyBuf, 1,
  33586. *rsaClientPrivKeyBufSz, keyFile);
  33587. XFCLOSE(keyFile);
  33588. if (rsaServerPrivKeyBuf != NULL) {
  33589. keyFile = XFOPEN(rsaServerKeyDerFile, "rb");
  33590. if (!keyFile)
  33591. return -12126;
  33592. *rsaServerPrivKeyBufSz = (word32)XFREAD(rsaServerPrivKeyBuf, 1,
  33593. *rsaServerPrivKeyBufSz, keyFile);
  33594. XFCLOSE(keyFile);
  33595. }
  33596. if (rsaCaPrivKeyBuf != NULL) {
  33597. keyFile = XFOPEN(rsaCaKeyFile, "rb");
  33598. if (!keyFile)
  33599. return -12127;
  33600. *rsaCaPrivKeyBufSz = (word32)XFREAD(rsaCaPrivKeyBuf, 1,
  33601. *rsaCaPrivKeyBufSz, keyFile);
  33602. XFCLOSE(keyFile);
  33603. }
  33604. #endif /* USE_CERT_BUFFERS */
  33605. #endif /* NO_RSA */
  33606. /* ECC */
  33607. #ifdef HAVE_ECC
  33608. #ifdef USE_CERT_BUFFERS_256
  33609. if (*eccClientCertBufSz < (word32)sizeof_cliecc_cert_der_256)
  33610. return -12128;
  33611. XMEMCPY(eccClientCertBuf, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  33612. *eccClientCertBufSz = sizeof_cliecc_cert_der_256;
  33613. #else
  33614. certFile = XFOPEN(eccClientCert, "rb");
  33615. if (!certFile)
  33616. return -12129;
  33617. *eccClientCertBufSz = (word32)XFREAD(eccClientCertBuf, 1,
  33618. *eccClientCertBufSz, certFile);
  33619. XFCLOSE(certFile);
  33620. #endif /* USE_CERT_BUFFERS_256 */
  33621. #ifdef USE_CERT_BUFFERS_256
  33622. if (*eccClientPrivKeyBufSz < (word32)sizeof_ecc_clikey_der_256)
  33623. return -12130;
  33624. XMEMCPY(eccClientPrivKeyBuf, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  33625. *eccClientPrivKeyBufSz = sizeof_ecc_clikey_der_256;
  33626. #else
  33627. keyFile = XFOPEN(eccClientKey, "rb");
  33628. if (!keyFile)
  33629. return -12131;
  33630. *eccClientPrivKeyBufSz = (word32)XFREAD(eccClientPrivKeyBuf, 1,
  33631. *eccClientPrivKeyBufSz, keyFile);
  33632. XFCLOSE(keyFile);
  33633. #endif /* USE_CERT_BUFFERS_256 */
  33634. #endif /* HAVE_ECC */
  33635. #ifdef NO_RSA
  33636. (void)rsaClientCertBuf;
  33637. (void)rsaClientCertBufSz;
  33638. (void)rsaClientPrivKeyBuf;
  33639. (void)rsaClientPrivKeyBufSz;
  33640. (void)rsaServerCertBuf;
  33641. (void)rsaServerCertBufSz;
  33642. (void)rsaServerPrivKeyBuf;
  33643. (void)rsaServerPrivKeyBufSz;
  33644. (void)rsaCaCertBuf;
  33645. (void)rsaCaCertBufSz;
  33646. (void)rsaCaPrivKeyBuf;
  33647. (void)rsaCaPrivKeyBufSz;
  33648. #endif
  33649. #ifndef HAVE_ECC
  33650. (void)eccClientCertBuf;
  33651. (void)eccClientCertBufSz;
  33652. (void)eccClientPrivKeyBuf;
  33653. (void)eccClientPrivKeyBufSz;
  33654. #endif
  33655. #ifndef NO_FILESYSTEM
  33656. (void)certFile;
  33657. (void)keyFile;
  33658. #endif
  33659. return 0;
  33660. }
  33661. typedef struct {
  33662. const byte* content;
  33663. word32 contentSz;
  33664. int contentOID;
  33665. int encryptOID;
  33666. int keyWrapOID;
  33667. int keyAgreeOID;
  33668. byte* cert;
  33669. size_t certSz;
  33670. byte* privateKey;
  33671. word32 privateKeySz;
  33672. byte* optionalUkm;
  33673. word32 optionalUkmSz;
  33674. int ktriOptions; /* KTRI options flags */
  33675. int kariOptions; /* KARI options flags */
  33676. /* KEKRI specific */
  33677. const byte* secretKey; /* key, only for kekri RecipientInfo types */
  33678. word32 secretKeySz; /* size of secretKey, bytes */
  33679. const byte* secretKeyId; /* key identifier */
  33680. word32 secretKeyIdSz; /* size of key identifier, bytes */
  33681. void* timePtr; /* time_t pointer */
  33682. byte* otherAttrOID; /* OPTIONAL, other attribute OID */
  33683. word32 otherAttrOIDSz; /* size of otherAttrOID, bytes */
  33684. byte* otherAttr; /* OPTIONAL, other attribute, ASN.1 encoded */
  33685. word32 otherAttrSz; /* size of otherAttr, bytes */
  33686. int kekriOptions; /* KEKRI options flags */
  33687. /* PWRI specific */
  33688. const char* password;
  33689. word32 passwordSz;
  33690. const byte* salt;
  33691. word32 saltSz;
  33692. int kdfOID;
  33693. int hashOID;
  33694. int kdfIterations;
  33695. int pwriOptions; /* PWRI options flags */
  33696. /* ORI specific */
  33697. int isOri;
  33698. int oriOptions; /* ORI options flags */
  33699. const char* outFileName;
  33700. } pkcs7EnvelopedVector;
  33701. static const byte asnDataOid[] = {
  33702. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x07, 0x01
  33703. };
  33704. /* ORI encrypt callback, responsible for encrypting content-encryption key (CEK)
  33705. * and giving wolfCrypt the value for oriOID and oriValue to place in
  33706. * OtherRecipientInfo.
  33707. *
  33708. * Returns 0 on success, negative upon error. */
  33709. static int myOriEncryptCb(PKCS7* pkcs7, byte* cek, word32 cekSz, byte* oriType,
  33710. word32* oriTypeSz, byte* oriValue, word32* oriValueSz,
  33711. void* ctx)
  33712. {
  33713. int i;
  33714. /* make sure buffers are large enough */
  33715. if ((*oriValueSz < (2 + cekSz)) || (*oriTypeSz < sizeof(oriType)))
  33716. return -12140;
  33717. /* our simple encryption algorithm will be take the bitwise complement */
  33718. oriValue[0] = 0x04; /*ASN OCTET STRING */
  33719. oriValue[1] = (byte)cekSz; /* length */
  33720. for (i = 0; i < (int)cekSz; i++) {
  33721. oriValue[2 + i] = ~cek[i];
  33722. }
  33723. *oriValueSz = 2 + cekSz;
  33724. /* set oriType to ASN.1 encoded data OID */
  33725. XMEMCPY(oriType, asnDataOid, sizeof(asnDataOid));
  33726. *oriTypeSz = sizeof(asnDataOid);
  33727. (void)pkcs7;
  33728. (void)ctx;
  33729. return 0;
  33730. }
  33731. /* ORI decrypt callback, responsible for providing a decrypted content
  33732. * encryption key (CEK) placed into decryptedKey and size placed into
  33733. * decryptedKeySz. oriOID and oriValue are given to the callback to help
  33734. * in decrypting the encrypted CEK.
  33735. *
  33736. * Returns 0 on success, negative upon error. */
  33737. static int myOriDecryptCb(PKCS7* pkcs7, byte* oriType, word32 oriTypeSz,
  33738. byte* oriValue, word32 oriValueSz, byte* decryptedKey,
  33739. word32* decryptedKeySz, void* ctx)
  33740. {
  33741. int i;
  33742. /* make sure oriType matches what we expect */
  33743. if (oriTypeSz != sizeof(asnDataOid))
  33744. return -12150;
  33745. if (XMEMCMP(oriType, asnDataOid, sizeof(asnDataOid)) != 0)
  33746. return -12151;
  33747. /* make sure decrypted buffer is large enough */
  33748. if (*decryptedKeySz < oriValueSz)
  33749. return -12152;
  33750. /* decrypt encrypted CEK using simple bitwise complement,
  33751. only for example */
  33752. for (i = 0; i < (int)oriValueSz - 2; i++) {
  33753. decryptedKey[i] = ~oriValue[2 + i];
  33754. }
  33755. *decryptedKeySz = oriValueSz - 2;
  33756. (void)pkcs7;
  33757. (void)ctx;
  33758. return 0;
  33759. }
  33760. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  33761. /* returns 0 on success */
  33762. static int myDecryptionFunc(PKCS7* pkcs7, int encryptOID, byte* iv, int ivSz,
  33763. byte* aad, word32 aadSz, byte* authTag, word32 authTagSz,
  33764. byte* in, int inSz, byte* out, void* usrCtx)
  33765. {
  33766. int keyId = -1, ret, keySz;
  33767. word32 keyIdSz = 8;
  33768. const byte* key;
  33769. byte keyIdRaw[8];
  33770. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  33771. Aes *aes;
  33772. #else
  33773. Aes aes[1];
  33774. #endif
  33775. /* looking for KEY ID
  33776. * fwDecryptKeyID OID "1.2.840.113549.1.9.16.2.37
  33777. */
  33778. WOLFSSL_SMALL_STACK_STATIC const unsigned char OID[] = {
  33779. /* 0x06, 0x0B do not pass in tag and length */
  33780. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  33781. 0x01, 0x09, 0x10, 0x02, 0x25
  33782. };
  33783. WOLFSSL_SMALL_STACK_STATIC const byte defKey[] = {
  33784. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  33785. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  33786. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  33787. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  33788. };
  33789. WOLFSSL_SMALL_STACK_STATIC const byte altKey[] = {
  33790. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  33791. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  33792. };
  33793. /* test user context passed in */
  33794. if (usrCtx == NULL || *(int*)usrCtx != 1) {
  33795. return -12160;
  33796. }
  33797. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  33798. if ((aes = (Aes *)XMALLOC(sizeof *aes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER)) == NULL)
  33799. return -12164;
  33800. #endif
  33801. /* if needing to find keyIdSz can call with NULL */
  33802. ret = wc_PKCS7_GetAttributeValue(pkcs7, OID, sizeof(OID), NULL,
  33803. &keyIdSz);
  33804. if (ret != LENGTH_ONLY_E) {
  33805. printf("Unexpected error %d when getting keyIdSz\n", ret);
  33806. printf("Possibly no KEY ID attribute set\n");
  33807. ERROR_OUT(-12161, out);
  33808. }
  33809. else {
  33810. XMEMSET(keyIdRaw, 0, sizeof(keyIdRaw));
  33811. ret = wc_PKCS7_GetAttributeValue(pkcs7, OID, sizeof(OID), keyIdRaw,
  33812. &keyIdSz);
  33813. if (ret < 0) {
  33814. goto out;
  33815. }
  33816. if (keyIdSz < 3) {
  33817. printf("keyIdSz is smaller than expected\n");
  33818. ERROR_OUT(-12162, out);
  33819. }
  33820. if (keyIdSz > 2 + sizeof(int)) {
  33821. printf("example case was only expecting a keyId of int size\n");
  33822. ERROR_OUT(-12163, out);
  33823. }
  33824. /* keyIdRaw[0] OCTET TAG */
  33825. /* keyIdRaw[1] Length */
  33826. #ifdef BIG_ENDIAN_ORDER
  33827. if (keyIdRaw[1] == 0x01) {
  33828. keyId = 1;
  33829. }
  33830. #else
  33831. XMEMCPY(&keyId, keyIdRaw + 2, sizeof(keyId));
  33832. #endif
  33833. }
  33834. /* Use keyID here if found to select key and decrypt in HSM or in this
  33835. * example just select key and do software decryption */
  33836. if (keyId == 1) {
  33837. key = altKey;
  33838. keySz = sizeof(altKey);
  33839. }
  33840. else {
  33841. key = defKey;
  33842. keySz = sizeof(defKey);
  33843. }
  33844. switch (encryptOID) {
  33845. #ifdef WOLFSSL_AES_256
  33846. case AES256CBCb:
  33847. if ((keySz != 32 ) || (ivSz != AES_BLOCK_SIZE))
  33848. ERROR_OUT(BAD_FUNC_ARG, out);
  33849. break;
  33850. #endif
  33851. #ifdef WOLFSSL_AES_128
  33852. case AES128CBCb:
  33853. if ((keySz != 16 ) || (ivSz != AES_BLOCK_SIZE))
  33854. ERROR_OUT(BAD_FUNC_ARG, out);
  33855. break;
  33856. #endif
  33857. default:
  33858. printf("Unsupported content cipher type for example");
  33859. ERROR_OUT(ALGO_ID_E, out);
  33860. };
  33861. ret = wc_AesInit(aes, HEAP_HINT, INVALID_DEVID);
  33862. if (ret == 0) {
  33863. ret = wc_AesSetKey(aes, key, keySz, iv, AES_DECRYPTION);
  33864. if (ret == 0)
  33865. ret = wc_AesCbcDecrypt(aes, out, in, inSz);
  33866. wc_AesFree(aes);
  33867. }
  33868. out:
  33869. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  33870. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33871. #endif
  33872. (void)aad;
  33873. (void)aadSz;
  33874. (void)authTag;
  33875. (void)authTagSz;
  33876. return ret;
  33877. }
  33878. #endif /* !NO_AES && HAVE_AES_CBC */
  33879. #define PKCS7_BUF_SIZE 2048
  33880. static int pkcs7enveloped_run_vectors(byte* rsaCert, word32 rsaCertSz,
  33881. byte* rsaPrivKey, word32 rsaPrivKeySz,
  33882. byte* eccCert, word32 eccCertSz,
  33883. byte* eccPrivKey, word32 eccPrivKeySz)
  33884. {
  33885. int ret = 0, testSz = 0, i;
  33886. int envelopedSz, decodedSz;
  33887. byte *enveloped = NULL;
  33888. byte *decoded = NULL;
  33889. PKCS7* pkcs7 = NULL;
  33890. #ifdef ECC_TIMING_RESISTANT
  33891. WC_RNG rng;
  33892. #endif
  33893. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  33894. XFILE pkcs7File;
  33895. #endif
  33896. WOLFSSL_SMALL_STACK_STATIC const byte data[] = { /* Hello World */
  33897. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  33898. 0x72,0x6c,0x64
  33899. };
  33900. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_256) && \
  33901. defined(HAVE_ECC) && defined(WOLFSSL_SHA512)
  33902. byte optionalUkm[] = {
  33903. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07
  33904. };
  33905. #endif /* NO_AES */
  33906. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128) && \
  33907. !defined(NO_SHA)
  33908. /* encryption key for kekri recipient types */
  33909. WOLFSSL_SMALL_STACK_STATIC const byte secretKey[] = {
  33910. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  33911. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07
  33912. };
  33913. /* encryption key identifier */
  33914. WOLFSSL_SMALL_STACK_STATIC const byte secretKeyId[] = {
  33915. 0x02,0x02,0x03,0x04
  33916. };
  33917. #endif
  33918. #if !defined(NO_PWDBASED) && !defined(NO_SHA) && \
  33919. !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  33920. #ifndef HAVE_FIPS
  33921. WOLFSSL_SMALL_STACK_STATIC const char password[] = "password"; /* NOTE: Password is too short for FIPS */
  33922. #else
  33923. WOLFSSL_SMALL_STACK_STATIC const char password[] = "passwordFIPS_MODE";
  33924. #endif
  33925. WOLFSSL_SMALL_STACK_STATIC const byte salt[] = {
  33926. 0x12, 0x34, 0x56, 0x78, 0x78, 0x56, 0x34, 0x12
  33927. };
  33928. #endif
  33929. #define MAX_TESTVECTORS_LEN 13
  33930. #define ADD_PKCS7ENVELOPEDVECTOR(...) { \
  33931. pkcs7EnvelopedVector _this_vector = { __VA_ARGS__ }; \
  33932. if (testSz == MAX_TESTVECTORS_LEN) { \
  33933. ret = -12534; \
  33934. goto out; \
  33935. } \
  33936. XMEMCPY(&testVectors[testSz++], &_this_vector, sizeof _this_vector);\
  33937. }
  33938. pkcs7EnvelopedVector *testVectors = NULL;
  33939. #ifdef ECC_TIMING_RESISTANT
  33940. XMEMSET(&rng, 0, sizeof(rng));
  33941. #endif
  33942. testVectors = (pkcs7EnvelopedVector *)XMALLOC(MAX_TESTVECTORS_LEN * sizeof(*testVectors),
  33943. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33944. if (testVectors == NULL) {
  33945. ret = -12534;
  33946. goto out;
  33947. }
  33948. {
  33949. /* key transport key encryption technique */
  33950. #ifndef NO_RSA
  33951. #ifndef NO_DES3
  33952. ADD_PKCS7ENVELOPEDVECTOR(
  33953. data, (word32)sizeof(data), DATA, DES3b, 0, 0, rsaCert, rsaCertSz,
  33954. rsaPrivKey, rsaPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL,
  33955. 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  33956. "pkcs7envelopedDataDES3.der");
  33957. #endif
  33958. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  33959. #ifdef WOLFSSL_AES_128
  33960. ADD_PKCS7ENVELOPEDVECTOR(
  33961. data, (word32)sizeof(data), DATA, AES128CBCb, 0, 0, rsaCert, rsaCertSz,
  33962. rsaPrivKey, rsaPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL,
  33963. 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  33964. "pkcs7envelopedDataAES128CBC.der");
  33965. #endif
  33966. #ifdef WOLFSSL_AES_192
  33967. ADD_PKCS7ENVELOPEDVECTOR(
  33968. data, (word32)sizeof(data), DATA, AES192CBCb, 0, 0, rsaCert, rsaCertSz,
  33969. rsaPrivKey, rsaPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL,
  33970. 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  33971. "pkcs7envelopedDataAES192CBC.der");
  33972. #endif
  33973. #ifdef WOLFSSL_AES_256
  33974. ADD_PKCS7ENVELOPEDVECTOR(
  33975. data, (word32)sizeof(data), DATA, AES256CBCb, 0, 0, rsaCert, rsaCertSz,
  33976. rsaPrivKey, rsaPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL,
  33977. 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  33978. "pkcs7envelopedDataAES256CBC.der");
  33979. /* explicitly using SKID for SubjectKeyIdentifier */
  33980. ADD_PKCS7ENVELOPEDVECTOR(
  33981. data, (word32)sizeof(data), DATA, AES256CBCb, 0, 0, rsaCert, rsaCertSz,
  33982. rsaPrivKey, rsaPrivKeySz, NULL, 0, CMS_SKID, 0, NULL, 0, NULL, 0, NULL,
  33983. NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  33984. "pkcs7envelopedDataAES256CBC_SKID.der");
  33985. /* explicitly using IssuerAndSerialNumber for SubjectKeyIdentifier */
  33986. ADD_PKCS7ENVELOPEDVECTOR(
  33987. data, (word32)sizeof(data), DATA, AES256CBCb, 0, 0, rsaCert, rsaCertSz,
  33988. rsaPrivKey, rsaPrivKeySz, NULL, 0, CMS_ISSUER_AND_SERIAL_NUMBER, 0,
  33989. NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0,
  33990. 0, 0, 0, 0, "pkcs7envelopedDataAES256CBC_IANDS.der");
  33991. #endif
  33992. #endif /* !NO_AES && HAVE_AES_CBC */
  33993. #endif
  33994. /* key agreement key encryption technique*/
  33995. #ifdef HAVE_ECC
  33996. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  33997. #if !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  33998. ADD_PKCS7ENVELOPEDVECTOR(
  33999. data, (word32)sizeof(data), DATA, AES128CBCb, AES128_WRAP,
  34000. dhSinglePass_stdDH_sha1kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  34001. eccPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0,
  34002. 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  34003. "pkcs7envelopedDataAES128CBC_ECDH_SHA1KDF.der");
  34004. #endif
  34005. #if !defined(NO_SHA256) && defined(WOLFSSL_AES_256)
  34006. ADD_PKCS7ENVELOPEDVECTOR(
  34007. data, (word32)sizeof(data), DATA, AES256CBCb, AES256_WRAP,
  34008. dhSinglePass_stdDH_sha256kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  34009. eccPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0,
  34010. 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  34011. "pkcs7envelopedDataAES256CBC_ECDH_SHA256KDF.der");
  34012. #endif /* NO_SHA256 && WOLFSSL_AES_256 */
  34013. #if defined(WOLFSSL_SHA512) && defined(WOLFSSL_AES_256)
  34014. ADD_PKCS7ENVELOPEDVECTOR(
  34015. data, (word32)sizeof(data), DATA, AES256CBCb, AES256_WRAP,
  34016. dhSinglePass_stdDH_sha512kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  34017. eccPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0,
  34018. 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  34019. "pkcs7envelopedDataAES256CBC_ECDH_SHA512KDF.der");
  34020. /* with optional user keying material (ukm) */
  34021. ADD_PKCS7ENVELOPEDVECTOR(
  34022. data, (word32)sizeof(data), DATA, AES256CBCb, AES256_WRAP,
  34023. dhSinglePass_stdDH_sha512kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  34024. eccPrivKeySz, optionalUkm, sizeof(optionalUkm), 0, 0, NULL, 0,
  34025. NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  34026. "pkcs7envelopedDataAES256CBC_ECDH_SHA512KDF_ukm.der");
  34027. #endif /* WOLFSSL_SHA512 && WOLFSSL_AES_256 */
  34028. #endif /* !NO_AES && HAVE_AES_CBC */
  34029. #endif
  34030. /* kekri (KEKRecipientInfo) recipient types */
  34031. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  34032. #if !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  34033. ADD_PKCS7ENVELOPEDVECTOR(
  34034. data, (word32)sizeof(data), DATA, AES128CBCb, AES128_WRAP, 0,
  34035. NULL, 0, NULL, 0, NULL, 0, 0, 0, secretKey, sizeof(secretKey),
  34036. secretKeyId, sizeof(secretKeyId), NULL, NULL, 0, NULL, 0,
  34037. 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  34038. "pkcs7envelopedDataAES128CBC_KEKRI.der");
  34039. #endif
  34040. #endif /* !NO_AES && HAVE_AES_CBC */
  34041. /* pwri (PasswordRecipientInfo) recipient types */
  34042. #if !defined(NO_PWDBASED) && !defined(NO_AES) && defined(HAVE_AES_CBC)
  34043. #if !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  34044. ADD_PKCS7ENVELOPEDVECTOR(
  34045. data, (word32)sizeof(data), DATA, AES128CBCb, 0, 0,
  34046. NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0,
  34047. NULL, 0, NULL, NULL, 0, NULL, 0, 0, password,
  34048. (word32)XSTRLEN(password), salt, sizeof(salt), PBKDF2_OID, WC_SHA, 5,
  34049. 0, 0, 0, "pkcs7envelopedDataAES128CBC_PWRI.der");
  34050. #endif
  34051. #endif
  34052. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && !defined(NO_AES_128)
  34053. /* ori (OtherRecipientInfo) recipient types */
  34054. ADD_PKCS7ENVELOPEDVECTOR(
  34055. data, (word32)sizeof(data), DATA, AES128CBCb, 0, 0, NULL, 0, NULL, 0,
  34056. NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0,
  34057. NULL, 0, 0, 0, 0, 0, 1, 0, "pkcs7envelopedDataAES128CBC_ORI.der");
  34058. #endif
  34059. };
  34060. #undef MAX_TESTVECTORS_LEN
  34061. #undef ADD_PKCS7ENVELOPEDVECTOR
  34062. enveloped = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34063. decoded = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34064. if ((! enveloped) || (! decoded)) {
  34065. ERROR_OUT(-12170, out);
  34066. }
  34067. #ifdef ECC_TIMING_RESISTANT
  34068. #ifndef HAVE_FIPS
  34069. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  34070. #else
  34071. ret = wc_InitRng(&rng);
  34072. #endif
  34073. if (ret != 0) {
  34074. ERROR_OUT(-12171, out);
  34075. }
  34076. #endif
  34077. for (i = 0; i < testSz; i++) {
  34078. pkcs7 = wc_PKCS7_New(HEAP_HINT,
  34079. #ifdef WOLFSSL_ASYNC_CRYPT
  34080. INVALID_DEVID /* async PKCS7 is not supported */
  34081. #else
  34082. devId
  34083. #endif
  34084. );
  34085. if (pkcs7 == NULL) {
  34086. ERROR_OUT(-12172, out);
  34087. }
  34088. if (testVectors[i].secretKey != NULL) {
  34089. /* KEKRI recipient type */
  34090. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  34091. if (ret != 0) {
  34092. ERROR_OUT(-12173, out);
  34093. }
  34094. pkcs7->content = (byte*)testVectors[i].content;
  34095. pkcs7->contentSz = testVectors[i].contentSz;
  34096. pkcs7->contentOID = testVectors[i].contentOID;
  34097. pkcs7->encryptOID = testVectors[i].encryptOID;
  34098. pkcs7->ukm = testVectors[i].optionalUkm;
  34099. pkcs7->ukmSz = testVectors[i].optionalUkmSz;
  34100. ret = wc_PKCS7_AddRecipient_KEKRI(pkcs7, testVectors[i].keyWrapOID,
  34101. (byte *)testVectors[i].secretKey, testVectors[i].secretKeySz,
  34102. (byte *)testVectors[i].secretKeyId, testVectors[i].secretKeyIdSz,
  34103. testVectors[i].timePtr, testVectors[i].otherAttrOID,
  34104. testVectors[i].otherAttrOIDSz, testVectors[i].otherAttr,
  34105. testVectors[i].otherAttrSz, testVectors[i].kekriOptions);
  34106. if (ret < 0) {
  34107. wc_PKCS7_Free(pkcs7);
  34108. ERROR_OUT(-12174, out);
  34109. }
  34110. /* set key, for decryption */
  34111. ret = wc_PKCS7_SetKey(pkcs7, (byte *)testVectors[i].secretKey,
  34112. testVectors[i].secretKeySz);
  34113. if (ret != 0) {
  34114. wc_PKCS7_Free(pkcs7);
  34115. ERROR_OUT(-12175, out);
  34116. }
  34117. } else if (testVectors[i].password != NULL) {
  34118. #if !defined(NO_PWDBASED) && !defined(NO_SHA)
  34119. /* PWRI recipient type */
  34120. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  34121. if (ret != 0) {
  34122. ERROR_OUT(-12176, out);
  34123. }
  34124. pkcs7->content = (byte*)testVectors[i].content;
  34125. pkcs7->contentSz = testVectors[i].contentSz;
  34126. pkcs7->contentOID = testVectors[i].contentOID;
  34127. pkcs7->encryptOID = testVectors[i].encryptOID;
  34128. pkcs7->ukm = testVectors[i].optionalUkm;
  34129. pkcs7->ukmSz = testVectors[i].optionalUkmSz;
  34130. ret = wc_PKCS7_AddRecipient_PWRI(pkcs7,
  34131. (byte *)testVectors[i].password, testVectors[i].passwordSz,
  34132. (byte *)testVectors[i].salt, testVectors[i].saltSz,
  34133. testVectors[i].kdfOID,
  34134. testVectors[i].hashOID, testVectors[i].kdfIterations,
  34135. testVectors[i].encryptOID, testVectors[i].pwriOptions);
  34136. if (ret < 0) {
  34137. wc_PKCS7_Free(pkcs7);
  34138. ERROR_OUT(-12177, out);
  34139. }
  34140. /* set password, for decryption */
  34141. ret = wc_PKCS7_SetPassword(pkcs7, (byte*)testVectors[i].password,
  34142. testVectors[i].passwordSz);
  34143. if (ret < 0) {
  34144. wc_PKCS7_Free(pkcs7);
  34145. ERROR_OUT(-12178, out);
  34146. }
  34147. #endif /* ! NO_PWDBASED && ! NO_SHA */
  34148. } else if (testVectors[i].isOri == 1) {
  34149. /* ORI recipient type */
  34150. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  34151. if (ret != 0) {
  34152. ERROR_OUT(-12179, out);
  34153. }
  34154. pkcs7->content = (byte*)testVectors[i].content;
  34155. pkcs7->contentSz = testVectors[i].contentSz;
  34156. pkcs7->contentOID = testVectors[i].contentOID;
  34157. pkcs7->encryptOID = testVectors[i].encryptOID;
  34158. ret = wc_PKCS7_AddRecipient_ORI(pkcs7, myOriEncryptCb,
  34159. testVectors[i].oriOptions);
  34160. if (ret < 0) {
  34161. wc_PKCS7_Free(pkcs7);
  34162. ERROR_OUT(-12180, out);
  34163. }
  34164. /* set decrypt callback for decryption */
  34165. ret = wc_PKCS7_SetOriDecryptCb(pkcs7, myOriDecryptCb);
  34166. if (ret < 0) {
  34167. wc_PKCS7_Free(pkcs7);
  34168. ERROR_OUT(-12181, out);
  34169. }
  34170. } else {
  34171. /* KTRI or KARI recipient types */
  34172. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  34173. if (ret != 0) {
  34174. ERROR_OUT(-12182, out);
  34175. }
  34176. ret = wc_PKCS7_InitWithCert(pkcs7, testVectors[i].cert,
  34177. (word32)testVectors[i].certSz);
  34178. if (ret != 0) {
  34179. wc_PKCS7_Free(pkcs7);
  34180. ERROR_OUT(-12183, out);
  34181. }
  34182. pkcs7->keyWrapOID = testVectors[i].keyWrapOID;
  34183. pkcs7->keyAgreeOID = testVectors[i].keyAgreeOID;
  34184. pkcs7->privateKey = testVectors[i].privateKey;
  34185. pkcs7->privateKeySz = testVectors[i].privateKeySz;
  34186. pkcs7->content = (byte*)testVectors[i].content;
  34187. pkcs7->contentSz = testVectors[i].contentSz;
  34188. pkcs7->contentOID = testVectors[i].contentOID;
  34189. pkcs7->encryptOID = testVectors[i].encryptOID;
  34190. pkcs7->ukm = testVectors[i].optionalUkm;
  34191. pkcs7->ukmSz = testVectors[i].optionalUkmSz;
  34192. /* set SubjectIdentifier type for KTRI types */
  34193. if (testVectors[i].ktriOptions & CMS_SKID) {
  34194. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID);
  34195. if (ret != 0) {
  34196. wc_PKCS7_Free(pkcs7);
  34197. ERROR_OUT(-12184, out);
  34198. }
  34199. } else if (testVectors[i].ktriOptions &
  34200. CMS_ISSUER_AND_SERIAL_NUMBER) {
  34201. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7,
  34202. CMS_ISSUER_AND_SERIAL_NUMBER);
  34203. if (ret != 0) {
  34204. wc_PKCS7_Free(pkcs7);
  34205. ERROR_OUT(-12185, out);
  34206. }
  34207. }
  34208. }
  34209. #ifdef ECC_TIMING_RESISTANT
  34210. pkcs7->rng = &rng;
  34211. #endif
  34212. /* encode envelopedData */
  34213. envelopedSz = wc_PKCS7_EncodeEnvelopedData(pkcs7, enveloped,
  34214. PKCS7_BUF_SIZE);
  34215. if (envelopedSz <= 0) {
  34216. wc_PKCS7_Free(pkcs7);
  34217. ERROR_OUT(-12186, out);
  34218. }
  34219. /* decode envelopedData */
  34220. pkcs7->contentOID = 0;
  34221. decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, enveloped, envelopedSz,
  34222. decoded, PKCS7_BUF_SIZE);
  34223. if (pkcs7->contentOID != testVectors[i].contentOID ||
  34224. decodedSz <= 0) {
  34225. wc_PKCS7_Free(pkcs7);
  34226. ERROR_OUT(-12187, out);
  34227. }
  34228. /* test decode result */
  34229. if (XMEMCMP(decoded, data, sizeof(data)) != 0){
  34230. wc_PKCS7_Free(pkcs7);
  34231. ERROR_OUT(-12188, out);
  34232. }
  34233. #ifndef NO_PKCS7_STREAM
  34234. { /* test reading byte by byte */
  34235. int z;
  34236. for (z = 0; z < envelopedSz; z++) {
  34237. decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, enveloped + z, 1,
  34238. decoded, PKCS7_BUF_SIZE);
  34239. if (decodedSz <= 0 && decodedSz != WC_PKCS7_WANT_READ_E) {
  34240. printf("unexpected error %d\n", decodedSz);
  34241. ERROR_OUT(-12189, out);
  34242. }
  34243. }
  34244. /* test decode result */
  34245. if (XMEMCMP(decoded, data, sizeof(data)) != 0) {
  34246. printf("stream read compare failed\n");
  34247. wc_PKCS7_Free(pkcs7);
  34248. ERROR_OUT(-12190, out);
  34249. }
  34250. }
  34251. #endif
  34252. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  34253. /* output pkcs7 envelopedData for external testing */
  34254. pkcs7File = XFOPEN(testVectors[i].outFileName, "wb");
  34255. if (!pkcs7File) {
  34256. wc_PKCS7_Free(pkcs7);
  34257. ERROR_OUT(-12191, out);
  34258. }
  34259. ret = (int)XFWRITE(enveloped, 1, envelopedSz, pkcs7File);
  34260. XFCLOSE(pkcs7File);
  34261. if (ret != envelopedSz) {
  34262. wc_PKCS7_Free(pkcs7);
  34263. ERROR_OUT(-12192, out);
  34264. } else {
  34265. /* reset ret to 0 for success */
  34266. ret = 0;
  34267. }
  34268. #endif /* PKCS7_OUTPUT_TEST_BUNDLES */
  34269. wc_PKCS7_Free(pkcs7);
  34270. pkcs7 = NULL;
  34271. }
  34272. #ifdef ECC_TIMING_RESISTANT
  34273. wc_FreeRng(&rng);
  34274. #endif
  34275. (void)eccCert;
  34276. (void)eccCertSz;
  34277. (void)eccPrivKey;
  34278. (void)eccPrivKeySz;
  34279. (void)rsaCert;
  34280. (void)rsaCertSz;
  34281. (void)rsaPrivKey;
  34282. (void)rsaPrivKeySz;
  34283. out:
  34284. if (testVectors)
  34285. XFREE(testVectors, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34286. if (enveloped)
  34287. XFREE(enveloped, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34288. if (decoded)
  34289. XFREE(decoded, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34290. return ret;
  34291. }
  34292. WOLFSSL_TEST_SUBROUTINE int pkcs7enveloped_test(void)
  34293. {
  34294. int ret = 0;
  34295. byte* rsaCert = NULL;
  34296. byte* rsaPrivKey = NULL;
  34297. word32 rsaCertSz = 0;
  34298. word32 rsaPrivKeySz = 0;
  34299. byte* eccCert = NULL;
  34300. byte* eccPrivKey = NULL;
  34301. word32 eccCertSz = 0;
  34302. word32 eccPrivKeySz = 0;
  34303. #ifndef NO_RSA
  34304. /* read client RSA cert and key in DER format */
  34305. rsaCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34306. if (rsaCert == NULL)
  34307. return -12200;
  34308. rsaPrivKey = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34309. if (rsaPrivKey == NULL) {
  34310. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34311. return -12201;
  34312. }
  34313. rsaCertSz = FOURK_BUF;
  34314. rsaPrivKeySz = FOURK_BUF;
  34315. #endif /* NO_RSA */
  34316. #ifdef HAVE_ECC
  34317. /* read client ECC cert and key in DER format */
  34318. eccCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34319. if (eccCert == NULL) {
  34320. #ifndef NO_RSA
  34321. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34322. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34323. #endif
  34324. return -12202;
  34325. }
  34326. eccPrivKey =(byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34327. if (eccPrivKey == NULL) {
  34328. #ifndef NO_RSA
  34329. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34330. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34331. #endif
  34332. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34333. return -12203;
  34334. }
  34335. eccCertSz = FOURK_BUF;
  34336. eccPrivKeySz = FOURK_BUF;
  34337. #endif /* HAVE_ECC */
  34338. ret = pkcs7_load_certs_keys(rsaCert, &rsaCertSz, rsaPrivKey,
  34339. &rsaPrivKeySz, NULL, NULL, NULL, NULL,
  34340. NULL, NULL, NULL, NULL, eccCert, &eccCertSz,
  34341. eccPrivKey, &eccPrivKeySz);
  34342. if (ret < 0) {
  34343. #ifndef NO_RSA
  34344. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34345. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34346. #endif
  34347. #ifdef HAVE_ECC
  34348. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34349. XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34350. #endif
  34351. return -12204;
  34352. }
  34353. ret = pkcs7enveloped_run_vectors(rsaCert, (word32)rsaCertSz,
  34354. rsaPrivKey, (word32)rsaPrivKeySz,
  34355. eccCert, (word32)eccCertSz,
  34356. eccPrivKey, (word32)eccPrivKeySz);
  34357. #ifndef NO_RSA
  34358. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34359. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34360. #endif
  34361. #ifdef HAVE_ECC
  34362. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34363. XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34364. #endif
  34365. return ret;
  34366. }
  34367. #if defined(HAVE_AESGCM) || defined(HAVE_AESCCM)
  34368. typedef struct {
  34369. const byte* content;
  34370. word32 contentSz;
  34371. int contentOID;
  34372. int encryptOID;
  34373. int keyWrapOID;
  34374. int keyAgreeOID;
  34375. byte* cert;
  34376. size_t certSz;
  34377. byte* privateKey;
  34378. word32 privateKeySz;
  34379. PKCS7Attrib* authAttribs;
  34380. word32 authAttribsSz;
  34381. PKCS7Attrib* unauthAttribs;
  34382. word32 unauthAttribsSz;
  34383. /* KARI / KTRI specific */
  34384. byte* optionalUkm;
  34385. word32 optionalUkmSz;
  34386. int ktriOptions; /* KTRI options flags */
  34387. int kariOptions; /* KARI options flags */
  34388. /* KEKRI specific */
  34389. byte* secretKey; /* key, only for kekri RecipientInfo types */
  34390. word32 secretKeySz; /* size of secretKey, bytes */
  34391. byte* secretKeyId; /* key identifier */
  34392. word32 secretKeyIdSz; /* size of key identifier, bytes */
  34393. void* timePtr; /* time_t pointer */
  34394. byte* otherAttrOID; /* OPTIONAL, other attribute OID */
  34395. word32 otherAttrOIDSz; /* size of otherAttrOID, bytes */
  34396. byte* otherAttr; /* OPTIONAL, other attribute, ASN.1 encoded */
  34397. word32 otherAttrSz; /* size of otherAttr, bytes */
  34398. int kekriOptions; /* KEKRI options flags */
  34399. /* PWRI specific */
  34400. char* password; /* password */
  34401. word32 passwordSz; /* password size, bytes */
  34402. byte* salt; /* KDF salt */
  34403. word32 saltSz; /* KDF salt size, bytes */
  34404. int kdfOID; /* KDF OID */
  34405. int hashOID; /* KDF hash algorithm OID */
  34406. int kdfIterations; /* KDF iterations */
  34407. int kekEncryptOID; /* KEK encryption algorithm OID */
  34408. int pwriOptions; /* PWRI options flags */
  34409. /* ORI specific */
  34410. int isOri;
  34411. int oriOptions; /* ORI options flags */
  34412. const char* outFileName;
  34413. } pkcs7AuthEnvelopedVector;
  34414. static int pkcs7authenveloped_run_vectors(byte* rsaCert, word32 rsaCertSz,
  34415. byte* rsaPrivKey, word32 rsaPrivKeySz,
  34416. byte* eccCert, word32 eccCertSz,
  34417. byte* eccPrivKey, word32 eccPrivKeySz)
  34418. {
  34419. int ret = 0, testSz = 0, i;
  34420. int envelopedSz, decodedSz;
  34421. byte *enveloped = NULL;
  34422. byte *decoded = NULL;
  34423. WC_RNG rng;
  34424. PKCS7* pkcs7;
  34425. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  34426. XFILE pkcs7File;
  34427. #endif
  34428. WOLFSSL_SMALL_STACK_STATIC const byte data[] = { /* Hello World */
  34429. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  34430. 0x72,0x6c,0x64
  34431. };
  34432. byte senderNonce[PKCS7_NONCE_SZ + 2];
  34433. #ifdef HAVE_ECC
  34434. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  34435. #if !defined(NO_SHA256) && defined(WOLFSSL_AES_256)
  34436. WOLFSSL_SMALL_STACK_STATIC const byte senderNonceOid[] =
  34437. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  34438. 0x09, 0x05 };
  34439. PKCS7Attrib attribs[] =
  34440. {
  34441. { senderNonceOid, sizeof(senderNonceOid), senderNonce,
  34442. sizeof(senderNonce) }
  34443. };
  34444. #endif
  34445. #endif
  34446. #endif
  34447. #if !defined(NO_AES) && defined(WOLFSSL_AES_256) && defined(HAVE_ECC) && \
  34448. defined(WOLFSSL_SHA512)
  34449. WOLFSSL_SMALL_STACK_STATIC const byte optionalUkm[] = {
  34450. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07
  34451. };
  34452. #endif /* NO_AES */
  34453. #if !defined(NO_AES) && !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  34454. /* encryption key for kekri recipient types */
  34455. WOLFSSL_SMALL_STACK_STATIC const byte secretKey[] = {
  34456. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  34457. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07
  34458. };
  34459. /* encryption key identifier */
  34460. WOLFSSL_SMALL_STACK_STATIC const byte secretKeyId[] = {
  34461. 0x02,0x02,0x03,0x04
  34462. };
  34463. #endif
  34464. #if !defined(NO_PWDBASED) && !defined(NO_AES) && defined(HAVE_AESGCM) && \
  34465. !defined(NO_SHA) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  34466. #ifndef HAVE_FIPS
  34467. WOLFSSL_SMALL_STACK_STATIC const char password[] = "password";
  34468. #else
  34469. WOLFSSL_SMALL_STACK_STATIC const char password[] = "passwordFIPS_MODE";
  34470. #endif
  34471. WOLFSSL_SMALL_STACK_STATIC const byte salt[] = {
  34472. 0x12, 0x34, 0x56, 0x78, 0x78, 0x56, 0x34, 0x12
  34473. };
  34474. #endif
  34475. #define MAX_TESTVECTORS_LEN 20
  34476. #define ADD_PKCS7AUTHENVELOPEDVECTOR(...) { \
  34477. pkcs7AuthEnvelopedVector _this_vector = { __VA_ARGS__ }; \
  34478. if (testSz == MAX_TESTVECTORS_LEN) { \
  34479. ret = -12534; \
  34480. goto out; \
  34481. } \
  34482. XMEMCPY(&testVectors[testSz++], &_this_vector, sizeof _this_vector);\
  34483. }
  34484. pkcs7AuthEnvelopedVector *testVectors = NULL;
  34485. XMEMSET(&rng, 0, sizeof(rng));
  34486. testVectors = (pkcs7AuthEnvelopedVector *)XMALLOC(MAX_TESTVECTORS_LEN * sizeof(*testVectors),
  34487. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34488. if (testVectors == NULL) {
  34489. ret = -12534;
  34490. goto out;
  34491. }
  34492. {
  34493. /* key transport key encryption technique */
  34494. #ifndef NO_RSA
  34495. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  34496. #ifdef WOLFSSL_AES_128
  34497. ADD_PKCS7AUTHENVELOPEDVECTOR(
  34498. data, (word32)sizeof(data), DATA, AES128GCMb, 0, 0, rsaCert, rsaCertSz,
  34499. rsaPrivKey, rsaPrivKeySz, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0,
  34500. NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0,
  34501. 0, 0, "pkcs7authEnvelopedDataAES128GCM.der");
  34502. #endif
  34503. #ifdef WOLFSSL_AES_192
  34504. ADD_PKCS7AUTHENVELOPEDVECTOR(
  34505. data, (word32)sizeof(data), DATA, AES192GCMb, 0, 0, rsaCert, rsaCertSz,
  34506. rsaPrivKey, rsaPrivKeySz, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0,
  34507. NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0,
  34508. 0, 0, "pkcs7authEnvelopedDataAES192GCM.der");
  34509. #endif
  34510. #ifdef WOLFSSL_AES_256
  34511. ADD_PKCS7AUTHENVELOPEDVECTOR(
  34512. data, (word32)sizeof(data), DATA, AES256GCMb, 0, 0, rsaCert, rsaCertSz,
  34513. rsaPrivKey, rsaPrivKeySz, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0,
  34514. NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0,
  34515. 0, 0, "pkcs7authEnvelopedDataAES256GCM.der");
  34516. /* test with contentType set to FirmwarePkgData */
  34517. ADD_PKCS7AUTHENVELOPEDVECTOR(
  34518. data, (word32)sizeof(data), FIRMWARE_PKG_DATA, AES256GCMb, 0, 0,
  34519. rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz, NULL, 0, NULL, 0, NULL,
  34520. 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL,
  34521. 0, 0, 0, 0, 0, 0, 0, 0,
  34522. "pkcs7authEnvelopedDataAES256GCM_firmwarePkgData.der");
  34523. /* explicitly using SKID for SubjectKeyIdentifier */
  34524. ADD_PKCS7AUTHENVELOPEDVECTOR(
  34525. data, (word32)sizeof(data), DATA, AES256GCMb, 0, 0, rsaCert, rsaCertSz,
  34526. rsaPrivKey, rsaPrivKeySz, NULL, 0, NULL, 0, NULL, 0, CMS_SKID, 0,
  34527. NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0,
  34528. 0, 0, 0, 0, 0, "pkcs7authEnvelopedDataAES256GCM_SKID.der");
  34529. /* explicitly using IssuerAndSerialNumber for SubjectKeyIdentifier */
  34530. ADD_PKCS7AUTHENVELOPEDVECTOR(
  34531. data, (word32)sizeof(data), DATA, AES256GCMb, 0, 0, rsaCert, rsaCertSz,
  34532. rsaPrivKey, rsaPrivKeySz, NULL, 0, NULL, 0, NULL, 0,
  34533. CMS_ISSUER_AND_SERIAL_NUMBER, 0, NULL, 0, NULL, 0, NULL, NULL, 0,
  34534. NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0, 0,
  34535. "pkcs7authEnvelopedDataAES256GCM_IANDS.der");
  34536. #endif
  34537. #endif /* NO_AES */
  34538. #endif
  34539. /* key agreement key encryption technique*/
  34540. #ifdef HAVE_ECC
  34541. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  34542. #if !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  34543. ADD_PKCS7AUTHENVELOPEDVECTOR(
  34544. data, (word32)sizeof(data), DATA, AES128GCMb, AES128_WRAP,
  34545. dhSinglePass_stdDH_sha1kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  34546. eccPrivKeySz, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0, NULL, 0,
  34547. NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0, 0,
  34548. "pkcs7authEnvelopedDataAES128GCM_ECDH_SHA1KDF.der");
  34549. #endif
  34550. #if !defined(NO_SHA256) && defined(WOLFSSL_AES_256)
  34551. ADD_PKCS7AUTHENVELOPEDVECTOR(
  34552. data, (word32)sizeof(data), DATA, AES256GCMb, AES256_WRAP,
  34553. dhSinglePass_stdDH_sha256kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  34554. eccPrivKeySz, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0, NULL, 0,
  34555. NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0, 0,
  34556. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA256KDF.der");
  34557. /* with authenticated attributes */
  34558. ADD_PKCS7AUTHENVELOPEDVECTOR(
  34559. data, (word32)sizeof(data), DATA, AES256GCMb, AES256_WRAP,
  34560. dhSinglePass_stdDH_sha256kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  34561. eccPrivKeySz, attribs, (sizeof(attribs) / sizeof(PKCS7Attrib)),
  34562. NULL, 0, NULL, 0, 0, 0, NULL, 0,
  34563. NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0,
  34564. 0, 0, 0,
  34565. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA256KDF_authAttribs.der");
  34566. /* with unauthenticated attributes */
  34567. ADD_PKCS7AUTHENVELOPEDVECTOR(
  34568. data, (word32)sizeof(data), DATA, AES256GCMb, AES256_WRAP,
  34569. dhSinglePass_stdDH_sha256kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  34570. eccPrivKeySz, NULL, 0, attribs,
  34571. (sizeof(attribs) / sizeof(PKCS7Attrib)), NULL, 0, 0, 0, NULL, 0,
  34572. NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0,
  34573. 0, 0, 0,
  34574. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA256KDF_unauthAttribs.der");
  34575. /* with authenticated AND unauthenticated attributes */
  34576. ADD_PKCS7AUTHENVELOPEDVECTOR(
  34577. data, (word32)sizeof(data), DATA, AES256GCMb, AES256_WRAP,
  34578. dhSinglePass_stdDH_sha256kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  34579. eccPrivKeySz, attribs, (sizeof(attribs) / sizeof(PKCS7Attrib)),
  34580. attribs, (sizeof(attribs) / sizeof(PKCS7Attrib)), NULL, 0, 0, 0,
  34581. NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0,
  34582. 0, 0, 0, 0, 0, 0,
  34583. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA256KDF_bothAttribs.der");
  34584. /* with authenticated AND unauthenticated attributes AND
  34585. * contentType of FirmwarePkgData */
  34586. ADD_PKCS7AUTHENVELOPEDVECTOR(
  34587. data, (word32)sizeof(data), FIRMWARE_PKG_DATA, AES256GCMb, AES256_WRAP,
  34588. dhSinglePass_stdDH_sha256kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  34589. eccPrivKeySz, attribs, (sizeof(attribs) / sizeof(PKCS7Attrib)),
  34590. attribs, (sizeof(attribs) / sizeof(PKCS7Attrib)), NULL, 0, 0, 0,
  34591. NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0,
  34592. 0, 0, 0, 0, 0, 0,
  34593. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA256KDF_fw_bothAttribs.der");
  34594. #endif /* NO_SHA256 && WOLFSSL_AES_256 */
  34595. #if defined(WOLFSSL_SHA512) && defined(WOLFSSL_AES_256)
  34596. ADD_PKCS7AUTHENVELOPEDVECTOR(
  34597. data, (word32)sizeof(data), DATA, AES256GCMb, AES256_WRAP,
  34598. dhSinglePass_stdDH_sha512kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  34599. eccPrivKeySz, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL,
  34600. NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0, 0,
  34601. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA512KDF.der");
  34602. /* with optional user keying material (ukm) */
  34603. ADD_PKCS7AUTHENVELOPEDVECTOR(
  34604. data, (word32)sizeof(data), DATA, AES256GCMb, AES256_WRAP,
  34605. dhSinglePass_stdDH_sha512kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  34606. eccPrivKeySz, NULL, 0, NULL, 0, (byte *)optionalUkm, sizeof(optionalUkm), 0,
  34607. 0, NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0,
  34608. 0, 0, 0, 0, 0, 0,
  34609. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA512KDF_ukm.der");
  34610. #endif /* WOLFSSL_SHA512 && WOLFSSL_AES_256 */
  34611. #endif /* NO_AES */
  34612. #endif
  34613. /* kekri (KEKRecipientInfo) recipient types */
  34614. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  34615. #if !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  34616. ADD_PKCS7AUTHENVELOPEDVECTOR(
  34617. data, (word32)sizeof(data), DATA, AES128GCMb, AES128_WRAP, 0,
  34618. NULL, 0, NULL, 0, NULL, 0, NULL, 0, NULL, 0, 0, 0,
  34619. (byte *)secretKey, sizeof(secretKey), (byte *)secretKeyId, sizeof(secretKeyId),
  34620. NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0, 0,
  34621. "pkcs7authEnvelopedDataAES128GCM_KEKRI.der");
  34622. #endif
  34623. #endif
  34624. /* pwri (PasswordRecipientInfo) recipient types */
  34625. #if !defined(NO_PWDBASED) && !defined(NO_AES) && defined(HAVE_AESGCM)
  34626. #if !defined(NO_SHA) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  34627. ADD_PKCS7AUTHENVELOPEDVECTOR(
  34628. data, (word32)sizeof(data), DATA, AES128GCMb, 0, 0,
  34629. NULL, 0, NULL, 0, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0,
  34630. NULL, 0, NULL, NULL, 0, NULL, 0, 0, (char *)password,
  34631. (word32)XSTRLEN(password), (byte *)salt, sizeof(salt), PBKDF2_OID, WC_SHA, 5,
  34632. AES128CBCb, 0, 0, 0, "pkcs7authEnvelopedDataAES128GCM_PWRI.der");
  34633. #endif
  34634. #endif
  34635. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  34636. #ifdef WOLFSSL_AES_128
  34637. /* ori (OtherRecipientInfo) recipient types */
  34638. ADD_PKCS7AUTHENVELOPEDVECTOR(
  34639. data, (word32)sizeof(data), DATA, AES128GCMb, 0, 0, NULL, 0, NULL, 0,
  34640. NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL, 0,
  34641. NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 1, 0,
  34642. "pkcs7authEnvelopedDataAES128GCM_ORI.der");
  34643. #endif
  34644. #endif
  34645. }
  34646. #undef MAX_TESTVECTORS_LEN
  34647. #undef ADD_PKCS7AUTHENVELOPEDVECTOR
  34648. enveloped = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34649. decoded = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34650. if ((! enveloped) || (! decoded)) {
  34651. ERROR_OUT(-12210, out);
  34652. }
  34653. /* generate senderNonce */
  34654. {
  34655. #ifndef HAVE_FIPS
  34656. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  34657. #else
  34658. ret = wc_InitRng(&rng);
  34659. #endif
  34660. if (ret != 0) {
  34661. ERROR_OUT(-12211, out);
  34662. }
  34663. senderNonce[0] = 0x04;
  34664. senderNonce[1] = PKCS7_NONCE_SZ;
  34665. ret = wc_RNG_GenerateBlock(&rng, &senderNonce[2], PKCS7_NONCE_SZ);
  34666. if (ret != 0) {
  34667. wc_FreeRng(&rng);
  34668. ERROR_OUT(-12212, out);
  34669. }
  34670. }
  34671. for (i = 0; i < testSz; i++) {
  34672. pkcs7 = wc_PKCS7_New(HEAP_HINT,
  34673. #ifdef WOLFSSL_ASYNC_CRYPT
  34674. INVALID_DEVID /* async PKCS7 is not supported */
  34675. #else
  34676. devId
  34677. #endif
  34678. );
  34679. if (pkcs7 == NULL) {
  34680. ERROR_OUT(-12213, out);
  34681. }
  34682. if (testVectors[i].secretKey != NULL) {
  34683. /* KEKRI recipient type */
  34684. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  34685. if (ret != 0) {
  34686. ERROR_OUT(-12214, out);
  34687. }
  34688. pkcs7->content = (byte*)testVectors[i].content;
  34689. pkcs7->contentSz = testVectors[i].contentSz;
  34690. pkcs7->contentOID = testVectors[i].contentOID;
  34691. pkcs7->encryptOID = testVectors[i].encryptOID;
  34692. pkcs7->ukm = testVectors[i].optionalUkm;
  34693. pkcs7->ukmSz = testVectors[i].optionalUkmSz;
  34694. pkcs7->authAttribs = testVectors[i].authAttribs;
  34695. pkcs7->authAttribsSz = testVectors[i].authAttribsSz;
  34696. pkcs7->unauthAttribs = testVectors[i].unauthAttribs;
  34697. pkcs7->unauthAttribsSz = testVectors[i].unauthAttribsSz;
  34698. ret = wc_PKCS7_AddRecipient_KEKRI(pkcs7, testVectors[i].keyWrapOID,
  34699. testVectors[i].secretKey, testVectors[i].secretKeySz,
  34700. testVectors[i].secretKeyId, testVectors[i].secretKeyIdSz,
  34701. testVectors[i].timePtr, testVectors[i].otherAttrOID,
  34702. testVectors[i].otherAttrOIDSz, testVectors[i].otherAttr,
  34703. testVectors[i].otherAttrSz, testVectors[i].kekriOptions);
  34704. if (ret < 0) {
  34705. wc_PKCS7_Free(pkcs7);
  34706. ERROR_OUT(-12215, out);
  34707. }
  34708. /* set key, for decryption */
  34709. ret = wc_PKCS7_SetKey(pkcs7, testVectors[i].secretKey,
  34710. testVectors[i].secretKeySz);
  34711. if (ret != 0) {
  34712. wc_PKCS7_Free(pkcs7);
  34713. ERROR_OUT(-12216, out);
  34714. }
  34715. } else if (testVectors[i].password != NULL) {
  34716. #if !defined(NO_PWDBASED) && !defined(NO_SHA)
  34717. /* PWRI recipient type */
  34718. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  34719. if (ret != 0) {
  34720. ERROR_OUT(-12217, out);
  34721. }
  34722. pkcs7->content = (byte*)testVectors[i].content;
  34723. pkcs7->contentSz = testVectors[i].contentSz;
  34724. pkcs7->contentOID = testVectors[i].contentOID;
  34725. pkcs7->encryptOID = testVectors[i].encryptOID;
  34726. pkcs7->ukm = testVectors[i].optionalUkm;
  34727. pkcs7->ukmSz = testVectors[i].optionalUkmSz;
  34728. pkcs7->authAttribs = testVectors[i].authAttribs;
  34729. pkcs7->authAttribsSz = testVectors[i].authAttribsSz;
  34730. pkcs7->unauthAttribs = testVectors[i].unauthAttribs;
  34731. pkcs7->unauthAttribsSz = testVectors[i].unauthAttribsSz;
  34732. ret = wc_PKCS7_AddRecipient_PWRI(pkcs7,
  34733. (byte*)testVectors[i].password,
  34734. testVectors[i].passwordSz, testVectors[i].salt,
  34735. testVectors[i].saltSz, testVectors[i].kdfOID,
  34736. testVectors[i].hashOID, testVectors[i].kdfIterations,
  34737. testVectors[i].kekEncryptOID, testVectors[i].pwriOptions);
  34738. if (ret < 0) {
  34739. wc_PKCS7_Free(pkcs7);
  34740. ERROR_OUT(-12218, out);
  34741. }
  34742. /* set password, for decryption */
  34743. ret = wc_PKCS7_SetPassword(pkcs7, (byte*)testVectors[i].password,
  34744. testVectors[i].passwordSz);
  34745. if (ret < 0) {
  34746. wc_PKCS7_Free(pkcs7);
  34747. ERROR_OUT(-12219, out);
  34748. }
  34749. #endif /* ! NO_PWDBASED && ! NO_SHA */
  34750. } else if (testVectors[i].isOri == 1) {
  34751. /* ORI recipient type */
  34752. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  34753. if (ret != 0) {
  34754. ERROR_OUT(-12220, out);
  34755. }
  34756. pkcs7->content = (byte*)testVectors[i].content;
  34757. pkcs7->contentSz = testVectors[i].contentSz;
  34758. pkcs7->contentOID = testVectors[i].contentOID;
  34759. pkcs7->encryptOID = testVectors[i].encryptOID;
  34760. pkcs7->authAttribs = testVectors[i].authAttribs;
  34761. pkcs7->authAttribsSz = testVectors[i].authAttribsSz;
  34762. pkcs7->unauthAttribs = testVectors[i].unauthAttribs;
  34763. pkcs7->unauthAttribsSz = testVectors[i].unauthAttribsSz;
  34764. ret = wc_PKCS7_AddRecipient_ORI(pkcs7, myOriEncryptCb,
  34765. testVectors[i].oriOptions);
  34766. if (ret < 0) {
  34767. wc_PKCS7_Free(pkcs7);
  34768. ERROR_OUT(-12221, out);
  34769. }
  34770. /* set decrypt callback for decryption */
  34771. ret = wc_PKCS7_SetOriDecryptCb(pkcs7, myOriDecryptCb);
  34772. if (ret < 0) {
  34773. wc_PKCS7_Free(pkcs7);
  34774. ERROR_OUT(-12222, out);
  34775. }
  34776. } else {
  34777. /* KTRI or KARI recipient types */
  34778. ret = wc_PKCS7_InitWithCert(pkcs7, testVectors[i].cert,
  34779. (word32)testVectors[i].certSz);
  34780. if (ret != 0) {
  34781. wc_PKCS7_Free(pkcs7);
  34782. ERROR_OUT(-12223, out);
  34783. }
  34784. pkcs7->keyWrapOID = testVectors[i].keyWrapOID;
  34785. pkcs7->keyAgreeOID = testVectors[i].keyAgreeOID;
  34786. pkcs7->privateKey = testVectors[i].privateKey;
  34787. pkcs7->privateKeySz = testVectors[i].privateKeySz;
  34788. pkcs7->content = (byte*)testVectors[i].content;
  34789. pkcs7->contentSz = testVectors[i].contentSz;
  34790. pkcs7->contentOID = testVectors[i].contentOID;
  34791. pkcs7->encryptOID = testVectors[i].encryptOID;
  34792. pkcs7->ukm = testVectors[i].optionalUkm;
  34793. pkcs7->ukmSz = testVectors[i].optionalUkmSz;
  34794. pkcs7->authAttribs = testVectors[i].authAttribs;
  34795. pkcs7->authAttribsSz = testVectors[i].authAttribsSz;
  34796. pkcs7->unauthAttribs = testVectors[i].unauthAttribs;
  34797. pkcs7->unauthAttribsSz = testVectors[i].unauthAttribsSz;
  34798. /* set SubjectIdentifier type for KTRI types */
  34799. if (testVectors[i].ktriOptions & CMS_SKID) {
  34800. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID);
  34801. if (ret != 0) {
  34802. wc_PKCS7_Free(pkcs7);
  34803. ERROR_OUT(-12224, out);
  34804. }
  34805. } else if (testVectors[i].ktriOptions &
  34806. CMS_ISSUER_AND_SERIAL_NUMBER) {
  34807. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7,
  34808. CMS_ISSUER_AND_SERIAL_NUMBER);
  34809. if (ret != 0) {
  34810. wc_PKCS7_Free(pkcs7);
  34811. ERROR_OUT(-12225, out);
  34812. }
  34813. }
  34814. }
  34815. #ifdef ECC_TIMING_RESISTANT
  34816. pkcs7->rng = &rng;
  34817. #endif
  34818. /* encode envelopedData */
  34819. envelopedSz = wc_PKCS7_EncodeAuthEnvelopedData(pkcs7, enveloped,
  34820. PKCS7_BUF_SIZE);
  34821. if (envelopedSz <= 0) {
  34822. wc_PKCS7_Free(pkcs7);
  34823. ERROR_OUT(-12226, out);
  34824. }
  34825. #ifndef NO_PKCS7_STREAM
  34826. { /* test reading byte by byte */
  34827. int z;
  34828. for (z = 0; z < envelopedSz; z++) {
  34829. decodedSz = wc_PKCS7_DecodeAuthEnvelopedData(pkcs7,
  34830. enveloped + z, 1, decoded, PKCS7_BUF_SIZE);
  34831. if (decodedSz <= 0 && decodedSz != WC_PKCS7_WANT_READ_E) {
  34832. printf("unexpected error %d\n", decodedSz);
  34833. ERROR_OUT(-12227, out);
  34834. }
  34835. }
  34836. /* test decode result */
  34837. if (XMEMCMP(decoded, data, sizeof(data)) != 0) {
  34838. printf("stream read compare failed\n");
  34839. wc_PKCS7_Free(pkcs7);
  34840. ERROR_OUT(-12228, out);
  34841. }
  34842. }
  34843. #endif
  34844. /* decode envelopedData */
  34845. decodedSz = wc_PKCS7_DecodeAuthEnvelopedData(pkcs7, enveloped,
  34846. envelopedSz, decoded,
  34847. PKCS7_BUF_SIZE);
  34848. if (decodedSz <= 0) {
  34849. wc_PKCS7_Free(pkcs7);
  34850. ERROR_OUT(-12229, out);
  34851. }
  34852. /* test decode result */
  34853. if (XMEMCMP(decoded, data, sizeof(data)) != 0){
  34854. wc_PKCS7_Free(pkcs7);
  34855. ERROR_OUT(-12230, out);
  34856. }
  34857. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  34858. /* output pkcs7 envelopedData for external testing */
  34859. pkcs7File = XFOPEN(testVectors[i].outFileName, "wb");
  34860. if (!pkcs7File) {
  34861. wc_PKCS7_Free(pkcs7);
  34862. ERROR_OUT(-12231, out);
  34863. }
  34864. ret = (int)XFWRITE(enveloped, 1, envelopedSz, pkcs7File);
  34865. XFCLOSE(pkcs7File);
  34866. if (ret != envelopedSz) {
  34867. wc_PKCS7_Free(pkcs7);
  34868. ERROR_OUT(-12232, out);
  34869. } else {
  34870. /* reset ret to 0 for success */
  34871. ret = 0;
  34872. }
  34873. #endif /* PKCS7_OUTPUT_TEST_BUNDLES */
  34874. wc_PKCS7_Free(pkcs7);
  34875. pkcs7 = NULL;
  34876. }
  34877. wc_FreeRng(&rng);
  34878. (void)eccCert;
  34879. (void)eccCertSz;
  34880. (void)eccPrivKey;
  34881. (void)eccPrivKeySz;
  34882. #if !defined(NO_AES) && !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  34883. (void)secretKey;
  34884. (void)secretKeyId;
  34885. #endif
  34886. #ifdef NO_RSA
  34887. (void)rsaCert;
  34888. (void)rsaCertSz;
  34889. (void)rsaPrivKey;
  34890. (void)rsaPrivKeySz;
  34891. #endif
  34892. out:
  34893. if (testVectors)
  34894. XFREE(testVectors, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34895. if (enveloped)
  34896. XFREE(enveloped, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34897. if (decoded)
  34898. XFREE(decoded, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34899. return ret;
  34900. }
  34901. WOLFSSL_TEST_SUBROUTINE int pkcs7authenveloped_test(void)
  34902. {
  34903. int ret = 0;
  34904. byte* rsaCert = NULL;
  34905. byte* rsaPrivKey = NULL;
  34906. word32 rsaCertSz = 0;
  34907. word32 rsaPrivKeySz = 0;
  34908. byte* eccCert = NULL;
  34909. byte* eccPrivKey = NULL;
  34910. word32 eccCertSz = 0;
  34911. word32 eccPrivKeySz = 0;
  34912. #ifndef NO_RSA
  34913. /* read client RSA cert and key in DER format */
  34914. rsaCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34915. if (rsaCert == NULL)
  34916. return -12300;
  34917. rsaPrivKey = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34918. if (rsaPrivKey == NULL) {
  34919. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34920. return -12301;
  34921. }
  34922. rsaCertSz = FOURK_BUF;
  34923. rsaPrivKeySz = FOURK_BUF;
  34924. #endif /* NO_RSA */
  34925. #ifdef HAVE_ECC
  34926. /* read client ECC cert and key in DER format */
  34927. eccCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34928. if (eccCert == NULL) {
  34929. #ifndef NO_RSA
  34930. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34931. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34932. #endif
  34933. return -12302;
  34934. }
  34935. eccPrivKey =(byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34936. if (eccPrivKey == NULL) {
  34937. #ifndef NO_RSA
  34938. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34939. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34940. #endif
  34941. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34942. return -12303;
  34943. }
  34944. eccCertSz = FOURK_BUF;
  34945. eccPrivKeySz = FOURK_BUF;
  34946. #endif /* HAVE_ECC */
  34947. ret = pkcs7_load_certs_keys(rsaCert, &rsaCertSz, rsaPrivKey,
  34948. &rsaPrivKeySz, NULL, NULL, NULL, NULL,
  34949. NULL, NULL, NULL, NULL, eccCert, &eccCertSz,
  34950. eccPrivKey, &eccPrivKeySz);
  34951. if (ret < 0) {
  34952. #ifndef NO_RSA
  34953. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34954. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34955. #endif
  34956. #ifdef HAVE_ECC
  34957. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34958. XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34959. #endif
  34960. return -12304;
  34961. }
  34962. ret = pkcs7authenveloped_run_vectors(rsaCert, (word32)rsaCertSz,
  34963. rsaPrivKey, (word32)rsaPrivKeySz,
  34964. eccCert, (word32)eccCertSz,
  34965. eccPrivKey, (word32)eccPrivKeySz);
  34966. #ifndef NO_RSA
  34967. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34968. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34969. #endif
  34970. #ifdef HAVE_ECC
  34971. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34972. XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34973. #endif
  34974. return ret;
  34975. }
  34976. #endif /* HAVE_AESGCM || HAVE_AESCCM */
  34977. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  34978. static const byte p7DefKey[] = {
  34979. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  34980. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  34981. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  34982. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  34983. };
  34984. static const byte p7AltKey[] = {
  34985. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  34986. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  34987. };
  34988. static int myCEKwrapFunc(PKCS7* pkcs7, byte* cek, word32 cekSz, byte* keyId,
  34989. word32 keyIdSz, byte* orginKey, word32 orginKeySz,
  34990. byte* out, word32 outSz, int keyWrapAlgo, int type, int direction)
  34991. {
  34992. int ret;
  34993. if (cek == NULL || out == NULL)
  34994. return BAD_FUNC_ARG;
  34995. /* test case sanity checks */
  34996. if (keyIdSz != 1) {
  34997. return -12310;
  34998. }
  34999. if (keyId[0] != 0x00) {
  35000. return -12311;
  35001. }
  35002. if (type != (int)PKCS7_KEKRI) {
  35003. return -12312;
  35004. }
  35005. switch (keyWrapAlgo) {
  35006. case AES256_WRAP:
  35007. ret = wc_AesKeyUnWrap(p7DefKey, sizeof(p7DefKey), cek, cekSz,
  35008. out, outSz, NULL);
  35009. if (ret <= 0)
  35010. return ret;
  35011. break;
  35012. default:
  35013. WOLFSSL_MSG("Unsupported key wrap algorithm in example");
  35014. return BAD_KEYWRAP_ALG_E;
  35015. };
  35016. (void)pkcs7;
  35017. (void)direction;
  35018. (void)orginKey; /* used with KAKRI */
  35019. (void)orginKeySz;
  35020. return ret;
  35021. }
  35022. /* returns key size on success */
  35023. static int getFirmwareKey(PKCS7* pkcs7, byte* key, word32 keySz)
  35024. {
  35025. int ret;
  35026. word32 atrSz;
  35027. byte atr[256];
  35028. /* Additionally can look for fwWrappedFirmwareKey
  35029. * 1.2.840.113529.1.9.16.1.16 */
  35030. const unsigned char fwWrappedFirmwareKey[] = {
  35031. /* 0x06, 0x0B */
  35032. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  35033. 0x01, 0x09, 0x10, 0x02, 0x27
  35034. };
  35035. /* find keyID in fwWrappedFirmwareKey */
  35036. ret = wc_PKCS7_GetAttributeValue(pkcs7, fwWrappedFirmwareKey,
  35037. sizeof(fwWrappedFirmwareKey), NULL, &atrSz);
  35038. if (ret == LENGTH_ONLY_E) {
  35039. XMEMSET(atr, 0, sizeof(atr));
  35040. ret = wc_PKCS7_GetAttributeValue(pkcs7, fwWrappedFirmwareKey,
  35041. sizeof(fwWrappedFirmwareKey), atr, &atrSz);
  35042. /* keyIdRaw[0] OCTET TAG */
  35043. /* keyIdRaw[1] Length */
  35044. if (ret > 0) {
  35045. PKCS7* envPkcs7;
  35046. envPkcs7 = wc_PKCS7_New(NULL, 0);
  35047. if (envPkcs7 == NULL) {
  35048. return MEMORY_E;
  35049. }
  35050. wc_PKCS7_Init(envPkcs7, NULL, 0);
  35051. ret = wc_PKCS7_SetWrapCEKCb(envPkcs7, myCEKwrapFunc);
  35052. if (ret == 0) {
  35053. /* expecting FIRMWARE_PKG_DATA content */
  35054. envPkcs7->contentOID = FIRMWARE_PKG_DATA;
  35055. ret = wc_PKCS7_DecodeEnvelopedData(envPkcs7, atr, atrSz,
  35056. key, keySz);
  35057. if (envPkcs7->contentOID != FIRMWARE_PKG_DATA) {
  35058. /* the contentOID should have been set to the inner
  35059. * FIRMWARE_PKG_DATA content */
  35060. ret = BAD_STATE_E;
  35061. }
  35062. }
  35063. wc_PKCS7_Free(envPkcs7);
  35064. }
  35065. }
  35066. return ret;
  35067. }
  35068. /* create a KEKRI enveloped data
  35069. * return size on success */
  35070. static int envelopedData_encrypt(byte* in, word32 inSz, byte* out,
  35071. word32 outSz)
  35072. {
  35073. int ret;
  35074. PKCS7* pkcs7;
  35075. WOLFSSL_SMALL_STACK_STATIC const byte keyId[] = { 0x00 };
  35076. pkcs7 = wc_PKCS7_New(NULL, INVALID_DEVID);
  35077. if (pkcs7 == NULL)
  35078. return -12330;
  35079. pkcs7->content = in;
  35080. pkcs7->contentSz = inSz;
  35081. pkcs7->contentOID = FIRMWARE_PKG_DATA;
  35082. pkcs7->encryptOID = AES256CBCb;
  35083. pkcs7->ukm = NULL;
  35084. pkcs7->ukmSz = 0;
  35085. /* add recipient (KEKRI type) */
  35086. ret = wc_PKCS7_AddRecipient_KEKRI(pkcs7, AES256_WRAP, (byte*)p7DefKey,
  35087. sizeof(p7DefKey), (byte*)keyId,
  35088. sizeof(keyId), NULL, NULL, 0, NULL, 0, 0);
  35089. if (ret < 0) {
  35090. printf("wc_PKCS7_AddRecipient_KEKRI() failed, ret = %d\n", ret);
  35091. wc_PKCS7_Free(pkcs7);
  35092. return -12331;
  35093. }
  35094. /* encode envelopedData, returns size */
  35095. ret = wc_PKCS7_EncodeEnvelopedData(pkcs7, out, outSz);
  35096. if (ret <= 0) {
  35097. printf("wc_PKCS7_EncodeEnvelopedData() failed, ret = %d\n", ret);
  35098. wc_PKCS7_Free(pkcs7);
  35099. return -12332;
  35100. }
  35101. wc_PKCS7_Free(pkcs7);
  35102. return ret;
  35103. }
  35104. /*
  35105. * keyHint is the KeyID to be set in the fwDecryptKeyID attribute
  35106. * returns size of buffer output on success
  35107. */
  35108. static int generateBundle(byte* out, word32 *outSz, const byte* encryptKey,
  35109. word32 encryptKeySz, byte keyHint, byte* cert, word32 certSz,
  35110. byte* key, word32 keySz)
  35111. {
  35112. int ret, attribNum = 1;
  35113. PKCS7* pkcs7;
  35114. /* KEY ID
  35115. * fwDecryptKeyID OID 1.2.840.113549.1.9.16.2.37
  35116. */
  35117. const unsigned char fwDecryptKeyID[] = {
  35118. 0x06, 0x0B,
  35119. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  35120. 0x01, 0x09, 0x10, 0x02, 0x25
  35121. };
  35122. /* fwWrappedFirmwareKey 1.2.840.113529.1.9.16.1.16 */
  35123. const unsigned char fwWrappedFirmwareKey[] = {
  35124. 0x06, 0x0B, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  35125. 0x01, 0x09, 0x10, 0x02, 0x27
  35126. };
  35127. byte keyID[] = { 0x04, 0x01, 0x00 };
  35128. byte env[256];
  35129. char data[] = "Test of wolfSSL PKCS7 decrypt callback";
  35130. PKCS7Attrib attribs[] =
  35131. {
  35132. { fwDecryptKeyID, sizeof(fwDecryptKeyID), keyID, sizeof(keyID) },
  35133. { fwWrappedFirmwareKey, sizeof(fwWrappedFirmwareKey), env, 0 }
  35134. };
  35135. keyID[2] = keyHint;
  35136. /* If using keyHint 0 then create a bundle with fwWrappedFirmwareKey */
  35137. if (keyHint == 0) {
  35138. ret = envelopedData_encrypt((byte*)p7DefKey, sizeof(p7DefKey), env,
  35139. sizeof(env));
  35140. if (ret <= 0) {
  35141. return ret;
  35142. }
  35143. attribs[1].valueSz = ret;
  35144. attribNum++;
  35145. }
  35146. /* init PKCS7 */
  35147. pkcs7 = wc_PKCS7_New(NULL, INVALID_DEVID);
  35148. if (pkcs7 == NULL)
  35149. return -12340;
  35150. ret = wc_PKCS7_InitWithCert(pkcs7, cert, certSz);
  35151. if (ret != 0) {
  35152. printf("ERROR: wc_PKCS7_InitWithCert() failed, ret = %d\n", ret);
  35153. wc_PKCS7_Free(pkcs7);
  35154. return -12341;
  35155. }
  35156. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID);
  35157. if (ret != 0) {
  35158. wc_PKCS7_Free(pkcs7);
  35159. return -12342;
  35160. }
  35161. /* encode Signed Encrypted FirmwarePkgData */
  35162. if (encryptKeySz == 16) {
  35163. ret = wc_PKCS7_EncodeSignedEncryptedFPD(pkcs7, (byte*)encryptKey,
  35164. encryptKeySz, key, keySz, AES128CBCb, RSAk, SHA256h,
  35165. (byte*)data, sizeof(data), NULL, 0,
  35166. attribs, attribNum, out, *outSz);
  35167. }
  35168. else {
  35169. ret = wc_PKCS7_EncodeSignedEncryptedFPD(pkcs7, (byte*)encryptKey,
  35170. encryptKeySz, key, keySz, AES256CBCb, RSAk, SHA256h,
  35171. (byte*)data, sizeof(data), NULL, 0,
  35172. attribs, attribNum, out, *outSz);
  35173. }
  35174. if (ret <= 0) {
  35175. printf("ERROR: wc_PKCS7_EncodeSignedEncryptedFPD() failed, "
  35176. "ret = %d\n", ret);
  35177. wc_PKCS7_Free(pkcs7);
  35178. return -12343;
  35179. } else {
  35180. *outSz = ret;
  35181. }
  35182. wc_PKCS7_Free(pkcs7);
  35183. return ret;
  35184. }
  35185. /* test verification and decryption of PKCS7 bundle
  35186. * return 0 on success
  35187. */
  35188. static int verifyBundle(byte* derBuf, word32 derSz, int keyHint)
  35189. {
  35190. int ret = 0;
  35191. int usrCtx = 1; /* test value to pass as user context to callback */
  35192. PKCS7* pkcs7 = NULL;
  35193. byte* sid = NULL;
  35194. word32 sidSz;
  35195. byte key[256];
  35196. word32 keySz = sizeof(key);
  35197. byte *decoded = NULL;
  35198. int decodedSz = FOURK_BUF/2;
  35199. WOLFSSL_SMALL_STACK_STATIC const byte expectedSid[] = {
  35200. #ifdef USE_CERT_BUFFERS_1024
  35201. 0x81, 0x69, 0x0f, 0xf8, 0xdf, 0xdd, 0xcf, 0x34,
  35202. 0x29, 0xd5, 0x67, 0x75, 0x71, 0x85, 0xc7, 0x75,
  35203. 0x10, 0x69, 0x59, 0xec,
  35204. #else
  35205. 0x33, 0xD8, 0x45, 0x66, 0xD7, 0x68, 0x87, 0x18,
  35206. 0x7E, 0x54, 0x0D, 0x70, 0x27, 0x91, 0xC7, 0x26,
  35207. 0xD7, 0x85, 0x65, 0xC0
  35208. #endif
  35209. };
  35210. decoded = (byte *)XMALLOC(decodedSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35211. if (decoded == NULL) {
  35212. ret = MEMORY_E;
  35213. goto out;
  35214. }
  35215. pkcs7 = wc_PKCS7_New(HEAP_HINT, INVALID_DEVID);
  35216. if (pkcs7 == NULL) {
  35217. ret = MEMORY_E;
  35218. goto out;
  35219. }
  35220. /* Test verify */
  35221. ret = wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID);
  35222. if (ret != 0)
  35223. goto out;
  35224. ret = wc_PKCS7_InitWithCert(pkcs7, NULL, 0);
  35225. if (ret != 0)
  35226. goto out;
  35227. ret = wc_PKCS7_VerifySignedData(pkcs7, derBuf, derSz);
  35228. if (ret != 0)
  35229. goto out;
  35230. /* Get size of SID and print it out */
  35231. ret = wc_PKCS7_GetSignerSID(pkcs7, NULL, &sidSz);
  35232. if (ret != LENGTH_ONLY_E)
  35233. goto out;
  35234. sid = (byte*)XMALLOC(sidSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35235. if (sid == NULL) {
  35236. ret = MEMORY_E;
  35237. goto out;
  35238. }
  35239. ret = wc_PKCS7_GetSignerSID(pkcs7, sid, &sidSz);
  35240. if (ret != 0)
  35241. goto out;
  35242. ret = XMEMCMP(sid, expectedSid, sidSz);
  35243. if (ret != 0) {
  35244. ret = PKCS7_NO_SIGNER_E; /* close enough */
  35245. goto out;
  35246. }
  35247. /* get expected fwWrappedFirmwareKey */
  35248. if (keyHint == 0) {
  35249. ret = getFirmwareKey(pkcs7, key, keySz);
  35250. if (ret < 0)
  35251. goto out;
  35252. pkcs7->encryptionKey = key;
  35253. pkcs7->encryptionKeySz = ret;
  35254. }
  35255. else {
  35256. decodedSz = PKCS7_BUF_SIZE;
  35257. ret = wc_PKCS7_SetDecodeEncryptedCb(pkcs7, myDecryptionFunc);
  35258. if (ret != 0)
  35259. goto out;
  35260. ret = wc_PKCS7_SetDecodeEncryptedCtx(pkcs7, (void*)&usrCtx);
  35261. if (ret != 0)
  35262. goto out;
  35263. }
  35264. decodedSz = wc_PKCS7_DecodeEncryptedData(pkcs7, pkcs7->content,
  35265. pkcs7->contentSz, decoded, decodedSz);
  35266. if (decodedSz < 0) {
  35267. ret = decodedSz;
  35268. goto out;
  35269. }
  35270. ret = 0;
  35271. out:
  35272. if (decoded)
  35273. XFREE(decoded, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35274. if (pkcs7)
  35275. wc_PKCS7_Free(pkcs7);
  35276. if (sid)
  35277. XFREE(sid, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35278. return ret;
  35279. }
  35280. WOLFSSL_TEST_SUBROUTINE int pkcs7callback_test(byte* cert, word32 certSz, byte* key, word32 keySz)
  35281. {
  35282. int ret = 0;
  35283. word32 derSz;
  35284. byte *derBuf = (byte *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35285. if (! derBuf)
  35286. ERROR_OUT(-12360, out);
  35287. /* Doing default generation and verify */
  35288. derSz = FOURK_BUF;
  35289. ret = generateBundle(derBuf, &derSz, p7DefKey, sizeof(p7DefKey), 0, cert,
  35290. certSz, key, keySz);
  35291. if (ret <= 0) {
  35292. ERROR_OUT(-12361, out);
  35293. }
  35294. ret = verifyBundle(derBuf, derSz, 0);
  35295. if (ret != 0) {
  35296. ERROR_OUT(-12362, out);
  35297. }
  35298. /* test choosing other key with keyID */
  35299. derSz = FOURK_BUF;
  35300. ret = generateBundle(derBuf, &derSz, p7AltKey, sizeof(p7AltKey), 1,
  35301. cert, certSz, key, keySz);
  35302. if (ret <= 0) {
  35303. ERROR_OUT(-12363, out);
  35304. }
  35305. ret = verifyBundle(derBuf, derSz, 1);
  35306. if (ret != 0) {
  35307. ERROR_OUT(-12364, out);
  35308. }
  35309. /* test fail case with wrong keyID */
  35310. derSz = FOURK_BUF;
  35311. ret = generateBundle(derBuf, &derSz, p7DefKey, sizeof(p7DefKey), 1,
  35312. cert, certSz, key, keySz);
  35313. if (ret <= 0) {
  35314. ERROR_OUT(-12365, out);
  35315. }
  35316. ret = verifyBundle(derBuf, derSz, 1);
  35317. if (ret == 0) {
  35318. ERROR_OUT(-12366, out);
  35319. }
  35320. ret = 0;
  35321. out:
  35322. if (derBuf)
  35323. XFREE(derBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35324. return ret;
  35325. }
  35326. #endif /* !NO_AES && HAVE_AES_CBC */
  35327. #ifndef NO_PKCS7_ENCRYPTED_DATA
  35328. typedef struct {
  35329. const byte* content;
  35330. word32 contentSz;
  35331. int contentOID;
  35332. int encryptOID;
  35333. byte* encryptionKey;
  35334. word32 encryptionKeySz;
  35335. PKCS7Attrib* attribs;
  35336. word32 attribsSz;
  35337. const char* outFileName;
  35338. } pkcs7EncryptedVector;
  35339. WOLFSSL_TEST_SUBROUTINE int pkcs7encrypted_test(void)
  35340. {
  35341. int ret = 0;
  35342. int i, testSz;
  35343. int encryptedSz, decodedSz, attribIdx;
  35344. PKCS7* pkcs7;
  35345. byte *encrypted;
  35346. byte *decoded;
  35347. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  35348. XFILE pkcs7File;
  35349. #endif
  35350. PKCS7Attrib* expectedAttrib;
  35351. PKCS7DecodedAttrib* decodedAttrib;
  35352. WOLFSSL_SMALL_STACK_STATIC const byte data[] = { /* Hello World */
  35353. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  35354. 0x72,0x6c,0x64
  35355. };
  35356. #ifndef NO_DES3
  35357. byte desKey[] = {
  35358. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
  35359. };
  35360. byte des3Key[] = {
  35361. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  35362. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  35363. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  35364. };
  35365. #endif
  35366. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  35367. #ifdef WOLFSSL_AES_128
  35368. byte aes128Key[] = {
  35369. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  35370. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  35371. };
  35372. #endif
  35373. #ifdef WOLFSSL_AES_192
  35374. byte aes192Key[] = {
  35375. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  35376. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  35377. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  35378. };
  35379. #endif
  35380. #ifdef WOLFSSL_AES_256
  35381. byte aes256Key[] = {
  35382. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  35383. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  35384. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  35385. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  35386. };
  35387. #endif
  35388. #ifdef WOLFSSL_AES_256
  35389. /* Attribute example from RFC 4134, Section 7.2
  35390. * OID = 1.2.5555
  35391. * OCTET STRING = 'This is a test General ASN Attribute, number 1.' */
  35392. static byte genAttrOid[] = { 0x06, 0x03, 0x2a, 0xab, 0x33 };
  35393. static byte genAttr[] = { 0x04, 47,
  35394. 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20,
  35395. 0x61, 0x20, 0x74, 0x65, 0x73, 0x74, 0x20, 0x47,
  35396. 0x65, 0x6e, 0x65, 0x72, 0x61, 0x6c, 0x20, 0x41,
  35397. 0x53, 0x4e, 0x20, 0x41, 0x74, 0x74, 0x72, 0x69,
  35398. 0x62, 0x75, 0x74, 0x65, 0x2c, 0x20, 0x6e, 0x75,
  35399. 0x6d, 0x62, 0x65, 0x72, 0x20, 0x31, 0x2e };
  35400. static byte genAttrOid2[] = { 0x06, 0x03, 0x2a, 0xab, 0x34 };
  35401. static byte genAttr2[] = { 0x04, 47,
  35402. 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20,
  35403. 0x61, 0x20, 0x74, 0x65, 0x73, 0x74, 0x20, 0x47,
  35404. 0x65, 0x6e, 0x65, 0x72, 0x61, 0x6c, 0x20, 0x41,
  35405. 0x53, 0x4e, 0x20, 0x41, 0x74, 0x74, 0x72, 0x69,
  35406. 0x62, 0x75, 0x74, 0x65, 0x2c, 0x20, 0x6e, 0x75,
  35407. 0x6d, 0x62, 0x65, 0x72, 0x20, 0x32, 0x2e };
  35408. PKCS7Attrib attribs[] =
  35409. {
  35410. { genAttrOid, sizeof(genAttrOid), genAttr, sizeof(genAttr) }
  35411. };
  35412. PKCS7Attrib multiAttribs[] =
  35413. {
  35414. { genAttrOid, sizeof(genAttrOid), genAttr, sizeof(genAttr) },
  35415. { genAttrOid2, sizeof(genAttrOid2), genAttr2, sizeof(genAttr2) }
  35416. };
  35417. #endif
  35418. #endif /* NO_AES */
  35419. const pkcs7EncryptedVector testVectors[] =
  35420. {
  35421. #ifndef NO_DES3
  35422. {data, (word32)sizeof(data), DATA, DES3b, des3Key, sizeof(des3Key),
  35423. NULL, 0, "pkcs7encryptedDataDES3.der"},
  35424. {data, (word32)sizeof(data), DATA, DESb, desKey, sizeof(desKey),
  35425. NULL, 0, "pkcs7encryptedDataDES.der"},
  35426. #endif /* NO_DES3 */
  35427. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  35428. #ifdef WOLFSSL_AES_128
  35429. {data, (word32)sizeof(data), DATA, AES128CBCb, aes128Key,
  35430. sizeof(aes128Key), NULL, 0, "pkcs7encryptedDataAES128CBC.der"},
  35431. #endif
  35432. #ifdef WOLFSSL_AES_192
  35433. {data, (word32)sizeof(data), DATA, AES192CBCb, aes192Key,
  35434. sizeof(aes192Key), NULL, 0, "pkcs7encryptedDataAES192CBC.der"},
  35435. #endif
  35436. #ifdef WOLFSSL_AES_256
  35437. {data, (word32)sizeof(data), DATA, AES256CBCb, aes256Key,
  35438. sizeof(aes256Key), NULL, 0, "pkcs7encryptedDataAES256CBC.der"},
  35439. /* test with optional unprotected attributes */
  35440. {data, (word32)sizeof(data), DATA, AES256CBCb, aes256Key,
  35441. sizeof(aes256Key), attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  35442. "pkcs7encryptedDataAES256CBC_attribs.der"},
  35443. /* test with multiple optional unprotected attributes */
  35444. {data, (word32)sizeof(data), DATA, AES256CBCb, aes256Key,
  35445. sizeof(aes256Key), multiAttribs,
  35446. (sizeof(multiAttribs)/sizeof(PKCS7Attrib)),
  35447. "pkcs7encryptedDataAES256CBC_multi_attribs.der"},
  35448. /* test with contentType set to FirmwarePkgData */
  35449. {data, (word32)sizeof(data), FIRMWARE_PKG_DATA, AES256CBCb, aes256Key,
  35450. sizeof(aes256Key), NULL, 0,
  35451. "pkcs7encryptedDataAES256CBC_firmwarePkgData.der"},
  35452. #endif
  35453. #endif /* !NO_AES && HAVE_AES_CBC */
  35454. };
  35455. encrypted = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35456. decoded = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35457. if ((! encrypted) || (! decoded)) {
  35458. ERROR_OUT(MEMORY_E, out);
  35459. }
  35460. testSz = sizeof(testVectors) / sizeof(pkcs7EncryptedVector);
  35461. for (i = 0; i < testSz; i++) {
  35462. pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
  35463. if (pkcs7 == NULL) {
  35464. ERROR_OUT(-12400, out);
  35465. }
  35466. pkcs7->content = (byte*)testVectors[i].content;
  35467. pkcs7->contentSz = testVectors[i].contentSz;
  35468. pkcs7->contentOID = testVectors[i].contentOID;
  35469. pkcs7->encryptOID = testVectors[i].encryptOID;
  35470. pkcs7->encryptionKey = testVectors[i].encryptionKey;
  35471. pkcs7->encryptionKeySz = testVectors[i].encryptionKeySz;
  35472. pkcs7->unprotectedAttribs = testVectors[i].attribs;
  35473. pkcs7->unprotectedAttribsSz = testVectors[i].attribsSz;
  35474. /* encode encryptedData */
  35475. encryptedSz = wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  35476. PKCS7_BUF_SIZE);
  35477. if (encryptedSz <= 0) {
  35478. wc_PKCS7_Free(pkcs7);
  35479. ERROR_OUT(-12401, out);
  35480. }
  35481. /* decode encryptedData */
  35482. #ifndef NO_PKCS7_STREAM
  35483. { /* test reading byte by byte */
  35484. int z;
  35485. for (z = 0; z < encryptedSz; z++) {
  35486. decodedSz = wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted + z, 1,
  35487. decoded, PKCS7_BUF_SIZE);
  35488. if (decodedSz <= 0 && decodedSz != WC_PKCS7_WANT_READ_E) {
  35489. printf("unexpected error %d\n", decodedSz);
  35490. ERROR_OUT(-12402, out);
  35491. }
  35492. }
  35493. /* test decode result */
  35494. if (XMEMCMP(decoded, data, sizeof(data)) != 0) {
  35495. printf("stream read failed\n");
  35496. wc_PKCS7_Free(pkcs7);
  35497. ERROR_OUT(-12403, out);
  35498. }
  35499. }
  35500. #endif
  35501. decodedSz = wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  35502. decoded, PKCS7_BUF_SIZE);
  35503. if (decodedSz <= 0){
  35504. wc_PKCS7_Free(pkcs7);
  35505. ERROR_OUT(-12404, out);
  35506. }
  35507. /* test decode result */
  35508. if (XMEMCMP(decoded, data, sizeof(data)) != 0) {
  35509. wc_PKCS7_Free(pkcs7);
  35510. ERROR_OUT(-12405, out);
  35511. }
  35512. /* verify decoded unprotected attributes */
  35513. if (pkcs7->decodedAttrib != NULL) {
  35514. decodedAttrib = pkcs7->decodedAttrib;
  35515. attribIdx = 1;
  35516. while (decodedAttrib != NULL) {
  35517. /* expected attribute, stored list is reversed */
  35518. expectedAttrib = &(pkcs7->unprotectedAttribs
  35519. [pkcs7->unprotectedAttribsSz - attribIdx]);
  35520. /* verify oid */
  35521. if (XMEMCMP(decodedAttrib->oid, expectedAttrib->oid,
  35522. decodedAttrib->oidSz) != 0) {
  35523. wc_PKCS7_Free(pkcs7);
  35524. ERROR_OUT(-12406, out);
  35525. }
  35526. /* verify value */
  35527. if (XMEMCMP(decodedAttrib->value, expectedAttrib->value,
  35528. decodedAttrib->valueSz) != 0) {
  35529. wc_PKCS7_Free(pkcs7);
  35530. ERROR_OUT(-12407, out);
  35531. }
  35532. decodedAttrib = decodedAttrib->next;
  35533. attribIdx++;
  35534. }
  35535. }
  35536. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  35537. /* output pkcs7 envelopedData for external testing */
  35538. pkcs7File = XFOPEN(testVectors[i].outFileName, "wb");
  35539. if (!pkcs7File) {
  35540. wc_PKCS7_Free(pkcs7);
  35541. ERROR_OUT(-12408, out);
  35542. }
  35543. ret = (int)XFWRITE(encrypted, encryptedSz, 1, pkcs7File);
  35544. XFCLOSE(pkcs7File);
  35545. if (ret > 0)
  35546. ret = 0;
  35547. #endif
  35548. wc_PKCS7_Free(pkcs7);
  35549. }
  35550. out:
  35551. if (encrypted)
  35552. XFREE(encrypted, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35553. if (decoded)
  35554. XFREE(decoded, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35555. return ret;
  35556. }
  35557. #endif /* NO_PKCS7_ENCRYPTED_DATA */
  35558. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  35559. typedef struct {
  35560. const byte* content;
  35561. word32 contentSz;
  35562. int contentOID;
  35563. const char* outFileName;
  35564. } pkcs7CompressedVector;
  35565. WOLFSSL_TEST_SUBROUTINE int pkcs7compressed_test(void)
  35566. {
  35567. int ret = 0;
  35568. int i, testSz;
  35569. int compressedSz, decodedSz;
  35570. PKCS7* pkcs7;
  35571. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  35572. byte *compressed;
  35573. byte *decoded;
  35574. #else
  35575. byte compressed[PKCS7_BUF_SIZE];
  35576. byte decoded[PKCS7_BUF_SIZE];
  35577. #endif
  35578. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  35579. XFILE pkcs7File;
  35580. #endif
  35581. WOLFSSL_SMALL_STACK_STATIC const byte data[] = { /* Hello World */
  35582. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  35583. 0x72,0x6c,0x64
  35584. };
  35585. const pkcs7CompressedVector testVectors[] =
  35586. {
  35587. {data, (word32)sizeof(data), DATA,
  35588. "pkcs7compressedData_data_zlib.der"},
  35589. {data, (word32)sizeof(data), FIRMWARE_PKG_DATA,
  35590. "pkcs7compressedData_firmwarePkgData_zlib.der"},
  35591. };
  35592. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  35593. compressed = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35594. decoded = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35595. if ((! compressed) || (! decoded)) {
  35596. ERROR_OUT(MEMORY_E, out);
  35597. }
  35598. #endif
  35599. testSz = sizeof(testVectors) / sizeof(pkcs7CompressedVector);
  35600. for (i = 0; i < testSz; i++) {
  35601. pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
  35602. if (pkcs7 == NULL) {
  35603. ERROR_OUT(-12500, out);
  35604. }
  35605. pkcs7->content = (byte*)testVectors[i].content;
  35606. pkcs7->contentSz = testVectors[i].contentSz;
  35607. pkcs7->contentOID = testVectors[i].contentOID;
  35608. /* encode compressedData */
  35609. compressedSz = wc_PKCS7_EncodeCompressedData(pkcs7, compressed,
  35610. PKCS7_BUF_SIZE);
  35611. if (compressedSz <= 0) {
  35612. wc_PKCS7_Free(pkcs7);
  35613. ERROR_OUT(-12501, out);
  35614. }
  35615. /* decode compressedData */
  35616. decodedSz = wc_PKCS7_DecodeCompressedData(pkcs7, compressed,
  35617. compressedSz, decoded,
  35618. PKCS7_BUF_SIZE);
  35619. if (decodedSz <= 0){
  35620. wc_PKCS7_Free(pkcs7);
  35621. ERROR_OUT(-12502, out);
  35622. }
  35623. /* test decode result */
  35624. if (XMEMCMP(decoded, testVectors[i].content,
  35625. testVectors[i].contentSz) != 0) {
  35626. wc_PKCS7_Free(pkcs7);
  35627. ERROR_OUT(-12503, out);
  35628. }
  35629. /* make sure content type is the same */
  35630. if (testVectors[i].contentOID != pkcs7->contentOID) {
  35631. ERROR_OUT(-12504, out);
  35632. }
  35633. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  35634. /* output pkcs7 compressedData for external testing */
  35635. pkcs7File = XFOPEN(testVectors[i].outFileName, "wb");
  35636. if (!pkcs7File) {
  35637. wc_PKCS7_Free(pkcs7);
  35638. ERROR_OUT(-12505, out);
  35639. }
  35640. ret = (int)XFWRITE(compressed, compressedSz, 1, pkcs7File);
  35641. XFCLOSE(pkcs7File);
  35642. if (ret > 0)
  35643. ret = 0;
  35644. #endif
  35645. wc_PKCS7_Free(pkcs7);
  35646. }
  35647. out:
  35648. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  35649. if (compressed)
  35650. XFREE(compressed, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35651. if (decoded)
  35652. XFREE(decoded, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35653. #endif
  35654. return ret;
  35655. } /* pkcs7compressed_test() */
  35656. #undef PKCS7_BUF_SIZE
  35657. #endif /* HAVE_LIBZ */
  35658. typedef struct {
  35659. const byte* content;
  35660. word32 contentSz;
  35661. int hashOID;
  35662. int signOID;
  35663. byte* privateKey;
  35664. word32 privateKeySz;
  35665. byte* cert;
  35666. size_t certSz;
  35667. byte* caCert;
  35668. size_t caCertSz;
  35669. PKCS7Attrib* signedAttribs;
  35670. word32 signedAttribsSz;
  35671. const char* outFileName;
  35672. int contentOID;
  35673. byte* contentType;
  35674. word32 contentTypeSz;
  35675. int sidType;
  35676. int encryptOID; /* for single-shot encrypt alg OID */
  35677. int encCompFlag; /* for single-shot. 1 = enc, 2 = comp, 3 = both*/
  35678. byte* encryptKey; /* for single-shot, encryptedData */
  35679. word32 encryptKeySz; /* for single-shot, encryptedData */
  35680. PKCS7Attrib* unprotectedAttribs; /* for single-shot, encryptedData */
  35681. word32 unprotectedAttribsSz; /* for single-shot, encryptedData */
  35682. word16 detachedSignature; /* generate detached signature (0:1) */
  35683. } pkcs7SignedVector;
  35684. static int pkcs7signed_run_vectors(
  35685. byte* rsaClientCertBuf, word32 rsaClientCertBufSz,
  35686. byte* rsaClientPrivKeyBuf, word32 rsaClientPrivKeyBufSz,
  35687. byte* rsaServerCertBuf, word32 rsaServerCertBufSz,
  35688. byte* rsaServerPrivKeyBuf, word32 rsaServerPrivKeyBufSz,
  35689. byte* rsaCaCertBuf, word32 rsaCaCertBufSz,
  35690. byte* rsaCaPrivKeyBuf, word32 rsaCaPrivKeyBufSz,
  35691. byte* eccClientCertBuf, word32 eccClientCertBufSz,
  35692. byte* eccClientPrivKeyBuf, word32 eccClientPrivKeyBufSz)
  35693. {
  35694. int ret, testSz = 0, i;
  35695. int encodedSz;
  35696. byte* out = NULL;
  35697. word32 outSz;
  35698. WC_RNG rng;
  35699. PKCS7* pkcs7 = NULL;
  35700. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  35701. XFILE file;
  35702. #endif
  35703. WOLFSSL_SMALL_STACK_STATIC const byte data[] = { /* Hello World */
  35704. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  35705. 0x72,0x6c,0x64
  35706. };
  35707. static byte transIdOid[] =
  35708. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  35709. 0x09, 0x07 };
  35710. static byte messageTypeOid[] =
  35711. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  35712. 0x09, 0x02 };
  35713. static byte senderNonceOid[] =
  35714. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  35715. 0x09, 0x05 };
  35716. #ifndef NO_SHA
  35717. static byte transId[(WC_SHA_DIGEST_SIZE + 1) * 2 + 1];
  35718. #else
  35719. static byte transId[(WC_SHA256_DIGEST_SIZE + 1) * 2 + 1];
  35720. #endif
  35721. static byte messageType[] = { 0x13, 2, '1', '9' };
  35722. static byte senderNonce[PKCS7_NONCE_SZ + 2];
  35723. static PKCS7Attrib attribs[] =
  35724. {
  35725. { transIdOid, sizeof(transIdOid), transId,
  35726. sizeof(transId) - 1 }, /* take off the null */
  35727. { messageTypeOid, sizeof(messageTypeOid), messageType,
  35728. sizeof(messageType) },
  35729. { senderNonceOid, sizeof(senderNonceOid), senderNonce,
  35730. sizeof(senderNonce) }
  35731. };
  35732. /* for testing custom contentType, FirmwarePkgData */
  35733. static byte customContentType[] = { 0x06, 0x0B, 0x2A, 0x86,
  35734. 0x48, 0x86, 0xF7, 0x0D,
  35735. 0x01, 0x09, 0x10, 0x01, 0x10 };
  35736. #define MAX_TESTVECTORS_LEN 20
  35737. #define ADD_PKCS7SIGNEDVECTOR(...) { \
  35738. pkcs7SignedVector _this_vector = { __VA_ARGS__ }; \
  35739. if (testSz == MAX_TESTVECTORS_LEN) { \
  35740. ret = -12534; \
  35741. goto out; \
  35742. } \
  35743. XMEMCPY(&testVectors[testSz++], &_this_vector, sizeof _this_vector);\
  35744. }
  35745. pkcs7SignedVector *testVectors = NULL;
  35746. XMEMSET(&rng, 0, sizeof(rng));
  35747. testVectors = (pkcs7SignedVector *)XMALLOC(MAX_TESTVECTORS_LEN * sizeof(*testVectors),
  35748. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35749. if (testVectors == NULL) {
  35750. ret = -12567;
  35751. goto out;
  35752. }
  35753. {
  35754. #ifndef NO_RSA
  35755. #ifndef NO_SHA
  35756. /* RSA with SHA */
  35757. ADD_PKCS7SIGNEDVECTOR(
  35758. data, (word32)sizeof(data), SHAh, RSAk, rsaClientPrivKeyBuf,
  35759. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  35760. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  35761. "pkcs7signedData_RSA_SHA.der", 0, NULL, 0, 0, 0, 0, NULL, 0, NULL,
  35762. 0, 0);
  35763. /* RSA with SHA, no signed attributes */
  35764. ADD_PKCS7SIGNEDVECTOR(
  35765. data, (word32)sizeof(data), SHAh, RSAk, rsaClientPrivKeyBuf,
  35766. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz,
  35767. NULL, 0, NULL, 0,
  35768. "pkcs7signedData_RSA_SHA_noattr.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  35769. NULL, 0, 0);
  35770. #endif
  35771. #ifdef WOLFSSL_SHA224
  35772. /* RSA with SHA224 */
  35773. ADD_PKCS7SIGNEDVECTOR(
  35774. data, (word32)sizeof(data), SHA224h, RSAk, rsaClientPrivKeyBuf,
  35775. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  35776. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  35777. "pkcs7signedData_RSA_SHA224.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  35778. NULL, 0, 0);
  35779. #endif
  35780. #ifndef NO_SHA256
  35781. /* RSA with SHA256 */
  35782. ADD_PKCS7SIGNEDVECTOR(
  35783. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  35784. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  35785. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  35786. "pkcs7signedData_RSA_SHA256.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  35787. NULL, 0, 0);
  35788. /* RSA with SHA256, detached signature */
  35789. ADD_PKCS7SIGNEDVECTOR(
  35790. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  35791. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  35792. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  35793. "pkcs7signedData_RSA_SHA256_detachedSig.der", 0, NULL, 0, 0, 0, 0,
  35794. NULL, 0, NULL, 0, 1);
  35795. /* RSA with SHA256 and SubjectKeyIdentifier in SignerIdentifier */
  35796. ADD_PKCS7SIGNEDVECTOR(
  35797. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  35798. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  35799. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  35800. "pkcs7signedData_RSA_SHA256_SKID.der", 0, NULL, 0, CMS_SKID, 0, 0,
  35801. NULL, 0, NULL, 0, 0);
  35802. /* RSA with SHA256 and custom contentType */
  35803. ADD_PKCS7SIGNEDVECTOR(
  35804. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  35805. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  35806. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  35807. "pkcs7signedData_RSA_SHA256_custom_contentType.der", 0,
  35808. customContentType, sizeof(customContentType), 0, 0, 0, NULL, 0,
  35809. NULL, 0, 0);
  35810. /* RSA with SHA256 and FirmwarePkgData contentType */
  35811. ADD_PKCS7SIGNEDVECTOR(
  35812. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  35813. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  35814. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  35815. "pkcs7signedData_RSA_SHA256_firmwarePkgData.der",
  35816. FIRMWARE_PKG_DATA, NULL, 0, 0, 0, 0, NULL, 0, NULL, 0, 0);
  35817. /* RSA with SHA256 using server cert and ca cert */
  35818. ADD_PKCS7SIGNEDVECTOR(
  35819. data, (word32)sizeof(data), SHA256h, RSAk, rsaServerPrivKeyBuf,
  35820. rsaServerPrivKeyBufSz, rsaServerCertBuf, rsaServerCertBufSz,
  35821. rsaCaCertBuf, rsaCaCertBufSz,
  35822. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  35823. "pkcs7signedData_RSA_SHA256_with_ca_cert.der", 0, NULL, 0, 0, 0, 0,
  35824. NULL, 0, NULL, 0, 0);
  35825. #endif
  35826. #if defined(WOLFSSL_SHA384)
  35827. /* RSA with SHA384 */
  35828. ADD_PKCS7SIGNEDVECTOR(
  35829. data, (word32)sizeof(data), SHA384h, RSAk, rsaClientPrivKeyBuf,
  35830. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  35831. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  35832. "pkcs7signedData_RSA_SHA384.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  35833. NULL, 0, 0);
  35834. #endif
  35835. #if defined(WOLFSSL_SHA512)
  35836. /* RSA with SHA512 */
  35837. ADD_PKCS7SIGNEDVECTOR(
  35838. data, (word32)sizeof(data), SHA512h, RSAk, rsaClientPrivKeyBuf,
  35839. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  35840. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  35841. "pkcs7signedData_RSA_SHA512.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  35842. NULL, 0, 0);
  35843. #endif
  35844. #endif /* NO_RSA */
  35845. #ifdef HAVE_ECC
  35846. #ifndef NO_SHA
  35847. /* ECDSA with SHA */
  35848. ADD_PKCS7SIGNEDVECTOR(
  35849. data, (word32)sizeof(data), SHAh, ECDSAk, eccClientPrivKeyBuf,
  35850. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  35851. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  35852. "pkcs7signedData_ECDSA_SHA.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  35853. NULL, 0, 0);
  35854. /* ECDSA with SHA, no signed attributes */
  35855. ADD_PKCS7SIGNEDVECTOR(
  35856. data, (word32)sizeof(data), SHAh, ECDSAk, eccClientPrivKeyBuf,
  35857. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz,
  35858. NULL, 0, NULL, 0,
  35859. "pkcs7signedData_ECDSA_SHA_noattr.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  35860. NULL, 0, 0);
  35861. #endif
  35862. #ifdef WOLFSSL_SHA224
  35863. /* ECDSA with SHA224 */
  35864. ADD_PKCS7SIGNEDVECTOR(
  35865. data, (word32)sizeof(data), SHA224h, ECDSAk, eccClientPrivKeyBuf,
  35866. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  35867. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  35868. "pkcs7signedData_ECDSA_SHA224.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  35869. NULL, 0, 0);
  35870. #endif
  35871. #ifndef NO_SHA256
  35872. /* ECDSA with SHA256 */
  35873. ADD_PKCS7SIGNEDVECTOR(
  35874. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  35875. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  35876. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  35877. "pkcs7signedData_ECDSA_SHA256.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  35878. NULL, 0, 0);
  35879. /* ECDSA with SHA256 and SubjectKeyIdentifier in SigherIdentifier */
  35880. ADD_PKCS7SIGNEDVECTOR(
  35881. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  35882. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  35883. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  35884. "pkcs7signedData_ECDSA_SHA256_SKID.der", 0, NULL, 0, CMS_SKID, 0, 0,
  35885. NULL, 0, NULL, 0, 0);
  35886. /* ECDSA with SHA256 and custom contentType */
  35887. ADD_PKCS7SIGNEDVECTOR(
  35888. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  35889. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  35890. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  35891. "pkcs7signedData_ECDSA_SHA256_custom_contentType.der", 0,
  35892. customContentType, sizeof(customContentType), 0, 0, 0, NULL, 0,
  35893. NULL, 0, 0);
  35894. /* ECDSA with SHA256 and FirmwarePkgData contentType */
  35895. ADD_PKCS7SIGNEDVECTOR(
  35896. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  35897. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  35898. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  35899. "pkcs7signedData_ECDSA_SHA256_firmwarePkgData.der",
  35900. FIRMWARE_PKG_DATA, NULL, 0, 0, 0, 0, NULL, 0, NULL, 0, 0);
  35901. #endif
  35902. #ifdef WOLFSSL_SHA384
  35903. /* ECDSA with SHA384 */
  35904. ADD_PKCS7SIGNEDVECTOR(
  35905. data, (word32)sizeof(data), SHA384h, ECDSAk, eccClientPrivKeyBuf,
  35906. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  35907. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  35908. "pkcs7signedData_ECDSA_SHA384.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  35909. NULL, 0, 0);
  35910. #endif
  35911. #ifdef WOLFSSL_SHA512
  35912. /* ECDSA with SHA512 */
  35913. ADD_PKCS7SIGNEDVECTOR(
  35914. data, (word32)sizeof(data), SHA512h, ECDSAk, eccClientPrivKeyBuf,
  35915. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  35916. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  35917. "pkcs7signedData_ECDSA_SHA512.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  35918. NULL, 0, 0);
  35919. #endif
  35920. #endif /* HAVE_ECC */
  35921. };
  35922. #undef MAX_TESTVECTORS_LEN
  35923. #undef ADD_PKCS7SIGNEDVECTOR
  35924. outSz = FOURK_BUF;
  35925. out = (byte*)XMALLOC(outSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35926. if (out == NULL)
  35927. ERROR_OUT(-12510, out);
  35928. XMEMSET(out, 0, outSz);
  35929. ret = wc_PKCS7_PadData((byte*)data, sizeof(data), out, outSz, 16);
  35930. if (ret < 0)
  35931. ERROR_OUT(-12511, out);
  35932. #ifndef HAVE_FIPS
  35933. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  35934. #else
  35935. ret = wc_InitRng(&rng);
  35936. #endif
  35937. if (ret != 0)
  35938. ERROR_OUT(-12512, out);
  35939. for (i = 0; i < testSz; i++) {
  35940. if (pkcs7)
  35941. wc_PKCS7_Free(pkcs7);
  35942. pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
  35943. if (pkcs7 == NULL)
  35944. ERROR_OUT(-12513, out);
  35945. ret = wc_PKCS7_InitWithCert(pkcs7, testVectors[i].cert,
  35946. (word32)testVectors[i].certSz);
  35947. if (ret != 0)
  35948. ERROR_OUT(-12514, out);
  35949. /* load CA certificate, if present */
  35950. if (testVectors[i].caCert != NULL) {
  35951. ret = wc_PKCS7_AddCertificate(pkcs7, testVectors[i].caCert,
  35952. (word32)testVectors[i].caCertSz);
  35953. if (ret != 0)
  35954. ERROR_OUT(-12515, out);
  35955. }
  35956. pkcs7->rng = &rng;
  35957. pkcs7->content = (byte*)testVectors[i].content;
  35958. pkcs7->contentSz = testVectors[i].contentSz;
  35959. pkcs7->contentOID = testVectors[i].contentOID;
  35960. pkcs7->hashOID = testVectors[i].hashOID;
  35961. pkcs7->encryptOID = testVectors[i].signOID;
  35962. pkcs7->privateKey = testVectors[i].privateKey;
  35963. pkcs7->privateKeySz = testVectors[i].privateKeySz;
  35964. pkcs7->signedAttribs = testVectors[i].signedAttribs;
  35965. pkcs7->signedAttribsSz = testVectors[i].signedAttribsSz;
  35966. /* optional custom contentType, default is DATA,
  35967. overrides contentOID if set */
  35968. if (testVectors[i].contentType != NULL) {
  35969. ret = wc_PKCS7_SetContentType(pkcs7, testVectors[i].contentType,
  35970. testVectors[i].contentTypeSz);
  35971. if (ret != 0)
  35972. ERROR_OUT(-12516, out);
  35973. }
  35974. /* set SignerIdentifier to use SubjectKeyIdentifier if desired,
  35975. default is IssuerAndSerialNumber */
  35976. if (testVectors[i].sidType == CMS_SKID) {
  35977. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID);
  35978. if (ret != 0)
  35979. ERROR_OUT(-12517, out);
  35980. }
  35981. /* generate senderNonce */
  35982. {
  35983. senderNonce[0] = 0x04;
  35984. senderNonce[1] = PKCS7_NONCE_SZ;
  35985. ret = wc_RNG_GenerateBlock(&rng, &senderNonce[2], PKCS7_NONCE_SZ);
  35986. if (ret != 0)
  35987. ERROR_OUT(-12518, out);
  35988. }
  35989. /* generate transactionID (used with SCEP) */
  35990. {
  35991. #ifndef NO_SHA
  35992. wc_Sha sha;
  35993. byte digest[WC_SHA_DIGEST_SIZE];
  35994. #else
  35995. wc_Sha256 sha;
  35996. byte digest[WC_SHA256_DIGEST_SIZE];
  35997. #endif
  35998. int j,k;
  35999. transId[0] = 0x13;
  36000. transId[1] = sizeof(digest) * 2;
  36001. #ifndef NO_SHA
  36002. ret = wc_InitSha_ex(&sha, HEAP_HINT, devId);
  36003. if (ret != 0)
  36004. ERROR_OUT(-12519, out);
  36005. wc_ShaUpdate(&sha, pkcs7->publicKey, pkcs7->publicKeySz);
  36006. wc_ShaFinal(&sha, digest);
  36007. wc_ShaFree(&sha);
  36008. #else
  36009. ret = wc_InitSha256_ex(&sha, HEAP_HINT, devId);
  36010. if (ret != 0)
  36011. ERROR_OUT(-12520, out);
  36012. wc_Sha256Update(&sha, pkcs7->publicKey, pkcs7->publicKeySz);
  36013. wc_Sha256Final(&sha, digest);
  36014. wc_Sha256Free(&sha);
  36015. #endif
  36016. for (j = 0, k = 2; j < (int)sizeof(digest); j++, k += 2) {
  36017. #if defined(WOLF_C89)
  36018. XSPRINTF((char*)&transId[k], "%02x", digest[j]);
  36019. #else
  36020. (void)XSNPRINTF((char*)&transId[k], 3, "%02x", digest[j]);
  36021. #endif
  36022. }
  36023. }
  36024. /* enable detached signature generation, if set */
  36025. if (testVectors[i].detachedSignature == 1) {
  36026. ret = wc_PKCS7_SetDetached(pkcs7, 1);
  36027. if (ret != 0)
  36028. ERROR_OUT(-12521, out);
  36029. }
  36030. encodedSz = wc_PKCS7_EncodeSignedData(pkcs7, out, outSz);
  36031. if (encodedSz < 0)
  36032. ERROR_OUT(-12522, out);
  36033. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  36034. /* write PKCS#7 to output file for more testing */
  36035. file = XFOPEN(testVectors[i].outFileName, "wb");
  36036. if (!file) {
  36037. ERROR_OUT(-12523, out);
  36038. }
  36039. ret = (int)XFWRITE(out, 1, encodedSz, file);
  36040. XFCLOSE(file);
  36041. if (ret != (int)encodedSz)
  36042. ERROR_OUT(-12524, out);
  36043. #endif /* PKCS7_OUTPUT_TEST_BUNDLES */
  36044. wc_PKCS7_Free(pkcs7);
  36045. pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
  36046. if (pkcs7 == NULL)
  36047. ERROR_OUT(-12525, out);
  36048. wc_PKCS7_InitWithCert(pkcs7, NULL, 0);
  36049. if (testVectors[i].detachedSignature == 1) {
  36050. /* set content for verifying detached signatures */
  36051. pkcs7->content = (byte*)testVectors[i].content;
  36052. pkcs7->contentSz = testVectors[i].contentSz;
  36053. }
  36054. ret = wc_PKCS7_VerifySignedData(pkcs7, out, outSz);
  36055. if (ret < 0)
  36056. ERROR_OUT(-12526, out);
  36057. /* verify contentType extracted successfully for custom content types */
  36058. if (testVectors[i].contentTypeSz > 0) {
  36059. if (pkcs7->contentTypeSz != testVectors[i].contentTypeSz) {
  36060. ERROR_OUT(-12527, out);
  36061. } else if (XMEMCMP(pkcs7->contentType, testVectors[i].contentType,
  36062. pkcs7->contentTypeSz) != 0) {
  36063. ERROR_OUT(-12528, out);
  36064. }
  36065. }
  36066. if (pkcs7->singleCert == NULL || pkcs7->singleCertSz == 0)
  36067. ERROR_OUT(-12529, out);
  36068. {
  36069. /* check getting signed attributes */
  36070. #ifndef NO_SHA
  36071. byte buf[(WC_SHA_DIGEST_SIZE + 1) * 2 + 1];
  36072. #else
  36073. byte buf[(WC_SHA256_DIGEST_SIZE + 1) * 2 + 1];
  36074. #endif
  36075. byte* oidPt = transIdOid + 2; /* skip object id tag and size */
  36076. int oidSz = (int)sizeof(transIdOid) - 2;
  36077. int bufSz = 0;
  36078. if (testVectors[i].signedAttribs != NULL &&
  36079. wc_PKCS7_GetAttributeValue(pkcs7, oidPt, oidSz,
  36080. NULL, (word32*)&bufSz) != LENGTH_ONLY_E)
  36081. ERROR_OUT(-12530, out);
  36082. if (bufSz > (int)sizeof(buf))
  36083. ERROR_OUT(-12531, out);
  36084. bufSz = wc_PKCS7_GetAttributeValue(pkcs7, oidPt, oidSz,
  36085. buf, (word32*)&bufSz);
  36086. if ((testVectors[i].signedAttribs != NULL && bufSz < 0) ||
  36087. (testVectors[i].signedAttribs == NULL && bufSz > 0))
  36088. ERROR_OUT(-12532, out);
  36089. }
  36090. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  36091. file = XFOPEN("./pkcs7cert.der", "wb");
  36092. if (!file)
  36093. ERROR_OUT(-12533, out);
  36094. ret = (int)XFWRITE(pkcs7->singleCert, 1, pkcs7->singleCertSz, file);
  36095. XFCLOSE(file);
  36096. #endif /* PKCS7_OUTPUT_TEST_BUNDLES */
  36097. }
  36098. out:
  36099. if (pkcs7 != NULL)
  36100. wc_PKCS7_Free(pkcs7);
  36101. if (out != NULL)
  36102. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  36103. if (testVectors != NULL)
  36104. XFREE(testVectors, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  36105. wc_FreeRng(&rng);
  36106. if (ret > 0)
  36107. return 0;
  36108. (void)rsaClientCertBuf;
  36109. (void)rsaClientCertBufSz;
  36110. (void)rsaClientPrivKeyBuf;
  36111. (void)rsaClientPrivKeyBufSz;
  36112. (void)rsaServerCertBuf;
  36113. (void)rsaServerCertBufSz;
  36114. (void)rsaServerPrivKeyBuf;
  36115. (void)rsaServerPrivKeyBufSz;
  36116. (void)rsaCaCertBuf;
  36117. (void)rsaCaCertBufSz;
  36118. (void)rsaCaPrivKeyBuf;
  36119. (void)rsaCaPrivKeyBufSz;
  36120. (void)eccClientCertBuf;
  36121. (void)eccClientCertBufSz;
  36122. (void)eccClientPrivKeyBuf;
  36123. (void)eccClientPrivKeyBufSz;
  36124. return ret;
  36125. }
  36126. static int pkcs7signed_run_SingleShotVectors(
  36127. byte* rsaClientCertBuf, word32 rsaClientCertBufSz,
  36128. byte* rsaClientPrivKeyBuf, word32 rsaClientPrivKeyBufSz,
  36129. byte* rsaServerCertBuf, word32 rsaServerCertBufSz,
  36130. byte* rsaServerPrivKeyBuf, word32 rsaServerPrivKeyBufSz,
  36131. byte* rsaCaCertBuf, word32 rsaCaCertBufSz,
  36132. byte* rsaCaPrivKeyBuf, word32 rsaCaPrivKeyBufSz,
  36133. byte* eccClientCertBuf, word32 eccClientCertBufSz,
  36134. byte* eccClientPrivKeyBuf, word32 eccClientPrivKeyBufSz)
  36135. {
  36136. int ret, testSz = 0, i;
  36137. int encodedSz;
  36138. byte* out = NULL;
  36139. word32 outSz;
  36140. WC_RNG rng;
  36141. PKCS7* pkcs7 = NULL;
  36142. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  36143. XFILE file;
  36144. #endif
  36145. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA) && \
  36146. !defined(NO_PKCS7_ENCRYPTED_DATA)
  36147. byte* encryptedTmp = NULL;
  36148. int encryptedTmpSz;
  36149. #endif
  36150. WOLFSSL_SMALL_STACK_STATIC const byte data[] = { /* Hello World */
  36151. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  36152. 0x72,0x6c,0x64
  36153. };
  36154. #if !defined(NO_PKCS7_ENCRYPTED_DATA) && \
  36155. defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_256)
  36156. static byte aes256Key[] = {
  36157. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  36158. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  36159. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  36160. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  36161. };
  36162. #endif
  36163. static byte messageTypeOid[] =
  36164. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  36165. 0x09, 0x02 };
  36166. static byte messageType[] = { 0x13, 2, '1', '9' };
  36167. PKCS7Attrib attribs[] =
  36168. {
  36169. { messageTypeOid, sizeof(messageTypeOid), messageType,
  36170. sizeof(messageType) },
  36171. };
  36172. #define MAX_TESTVECTORS_LEN 19
  36173. #define ADD_PKCS7SIGNEDVECTOR(...) { \
  36174. pkcs7SignedVector _this_vector = { __VA_ARGS__ }; \
  36175. if (testSz == MAX_TESTVECTORS_LEN) { \
  36176. ret = -12568; \
  36177. goto out; \
  36178. } \
  36179. XMEMCPY(&testVectors[testSz++], &_this_vector, sizeof _this_vector);\
  36180. }
  36181. pkcs7SignedVector *testVectors = NULL;
  36182. XMEMSET(&rng, 0, sizeof(rng));
  36183. testVectors = (pkcs7SignedVector *)XMALLOC(MAX_TESTVECTORS_LEN * sizeof(*testVectors),
  36184. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  36185. if (testVectors == NULL) {
  36186. ret = -12567;
  36187. goto out;
  36188. }
  36189. {
  36190. #ifndef NO_RSA
  36191. #ifndef NO_SHA256
  36192. /* Signed FirmwarePkgData, RSA, SHA256, no attribs */
  36193. ADD_PKCS7SIGNEDVECTOR(
  36194. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  36195. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  36196. NULL, 0,
  36197. "pkcs7signedFirmwarePkgData_RSA_SHA256_noattr.der", 0, NULL, 0, 0,
  36198. 0, 0, NULL, 0, NULL, 0, 0);
  36199. /* Signed FirmwarePkgData, RSA, SHA256, attrs */
  36200. ADD_PKCS7SIGNEDVECTOR(
  36201. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  36202. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  36203. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  36204. "pkcs7signedFirmwarePkgData_RSA_SHA256.der", 0, NULL, 0, 0, 0, 0,
  36205. NULL, 0, NULL, 0, 0);
  36206. /* Signed FirmwarePkgData, RSA, SHA256, SubjectKeyIdentifier, attrs */
  36207. ADD_PKCS7SIGNEDVECTOR(
  36208. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  36209. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  36210. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  36211. "pkcs7signedFirmwarePkgData_RSA_SHA256_SKID.der", 0, NULL,
  36212. 0, CMS_SKID, 0, 0, NULL, 0, NULL, 0, 0);
  36213. /* Signed FirmwraePkgData, RSA, SHA256, server cert and ca cert, attr */
  36214. ADD_PKCS7SIGNEDVECTOR(
  36215. data, (word32)sizeof(data), SHA256h, RSAk, rsaServerPrivKeyBuf,
  36216. rsaServerPrivKeyBufSz, rsaServerCertBuf, rsaServerCertBufSz,
  36217. rsaCaCertBuf, rsaCaCertBufSz,
  36218. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  36219. "pkcs7signedFirmwarePkgData_RSA_SHA256_with_ca_cert.der", 0, NULL,
  36220. 0, 0, 0, 0, NULL, 0, NULL, 0, 0);
  36221. #if !defined(NO_PKCS7_ENCRYPTED_DATA) && \
  36222. defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_256)
  36223. /* Signed Encrypted FirmwarePkgData, RSA, SHA256, no attribs */
  36224. ADD_PKCS7SIGNEDVECTOR(
  36225. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  36226. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  36227. NULL, 0,
  36228. "pkcs7signedEncryptedFirmwarePkgData_RSA_SHA256_noattr.der", 0,
  36229. NULL, 0, 0, AES256CBCb, 1, aes256Key, sizeof(aes256Key), NULL, 0, 0);
  36230. /* Signed Encrypted FirmwarePkgData, RSA, SHA256, attribs */
  36231. ADD_PKCS7SIGNEDVECTOR(
  36232. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  36233. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  36234. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  36235. "pkcs7signedEncryptedFirmwarePkgData_RSA_SHA256.der", 0,
  36236. NULL, 0, 0, AES256CBCb, 1, aes256Key, sizeof(aes256Key),
  36237. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)), 0);
  36238. #endif /* WOLFSSL_AES_256 && !NO_PKCS7_ENCRYPTED_DATA */
  36239. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  36240. /* Signed Compressed FirmwarePkgData, RSA, SHA256, no attribs */
  36241. ADD_PKCS7SIGNEDVECTOR(
  36242. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  36243. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  36244. NULL, 0,
  36245. "pkcs7signedCompressedFirmwarePkgData_RSA_SHA256_noattr.der", 0,
  36246. NULL, 0, 0, 0, 2, NULL, 0, NULL, 0, 0);
  36247. /* Signed Compressed FirmwarePkgData, RSA, SHA256, attribs */
  36248. ADD_PKCS7SIGNEDVECTOR(
  36249. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  36250. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  36251. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  36252. "pkcs7signedCompressedFirmwarePkgData_RSA_SHA256.der", 0,
  36253. NULL, 0, 0, 0, 2, NULL, 0, NULL, 0, 0);
  36254. #ifndef NO_PKCS7_ENCRYPTED_DATA
  36255. /* Signed Encrypted Compressed FirmwarePkgData, RSA, SHA256,
  36256. no attribs */
  36257. ADD_PKCS7SIGNEDVECTOR(
  36258. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  36259. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  36260. NULL, 0,
  36261. "pkcs7signedEncryptedCompressedFirmwarePkgData_RSA_SHA256_noattr.der",
  36262. 0, NULL, 0, 0, AES256CBCb, 3, aes256Key, sizeof(aes256Key), NULL,
  36263. 0, 0);
  36264. /* Signed Encrypted Compressed FirmwarePkgData, RSA, SHA256,
  36265. attribs */
  36266. ADD_PKCS7SIGNEDVECTOR(
  36267. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  36268. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  36269. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  36270. "pkcs7signedEncryptedCompressedFirmwarePkgData_RSA_SHA256.der",
  36271. 0, NULL, 0, 0, AES256CBCb, 3, aes256Key, sizeof(aes256Key),
  36272. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)), 0);
  36273. #endif /* !NO_PKCS7_ENCRYPTED_DATA */
  36274. #endif /* HAVE_LIBZ && !NO_PKCS7_COMPRESSED_DATA */
  36275. #endif /* NO_SHA256 */
  36276. #endif /* NO_RSA */
  36277. #ifdef HAVE_ECC
  36278. #ifndef NO_SHA256
  36279. /* Signed FirmwarePkgData, ECDSA, SHA256, no attribs */
  36280. ADD_PKCS7SIGNEDVECTOR(
  36281. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  36282. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  36283. NULL, 0,
  36284. "pkcs7signedFirmwarePkgData_ECDSA_SHA256_noattr.der", 0, NULL,
  36285. 0, 0, 0, 0, NULL, 0, NULL, 0, 0);
  36286. /* Signed FirmwarePkgData, ECDSA, SHA256, attribs */
  36287. ADD_PKCS7SIGNEDVECTOR(
  36288. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  36289. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  36290. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  36291. "pkcs7signedFirmwarePkgData_ECDSA_SHA256.der", 0, NULL,
  36292. 0, 0, 0, 0, NULL, 0, NULL, 0, 0);
  36293. /* Signed FirmwarePkgData, ECDSA, SHA256, SubjectKeyIdentifier, attr */
  36294. ADD_PKCS7SIGNEDVECTOR(
  36295. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  36296. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  36297. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  36298. "pkcs7signedFirmwarePkgData_ECDSA_SHA256_SKID.der", 0, NULL,
  36299. 0, CMS_SKID, 0, 0, NULL, 0, NULL, 0, 0);
  36300. #if !defined(NO_PKCS7_ENCRYPTED_DATA) && \
  36301. defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_256)
  36302. /* Signed Encrypted FirmwarePkgData, ECDSA, SHA256, no attribs */
  36303. ADD_PKCS7SIGNEDVECTOR(
  36304. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  36305. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  36306. NULL, 0,
  36307. "pkcs7signedEncryptedFirmwarePkgData_ECDSA_SHA256_noattr.der", 0, NULL,
  36308. 0, 0, AES256CBCb, 1, aes256Key, sizeof(aes256Key), NULL, 0, 0);
  36309. /* Signed Encrypted FirmwarePkgData, ECDSA, SHA256, attribs */
  36310. ADD_PKCS7SIGNEDVECTOR(
  36311. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  36312. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  36313. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  36314. "pkcs7signedEncryptedFirmwarePkgData_ECDSA_SHA256.der", 0, NULL,
  36315. 0, 0, AES256CBCb, 1, aes256Key, sizeof(aes256Key),
  36316. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)), 0);
  36317. #endif /* WOLFSSL_AES_256 && !NO_PKCS7_ENCRYPTED_DATA */
  36318. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  36319. /* Signed Compressed FirmwarePkgData, ECDSA, SHA256, no attribs */
  36320. ADD_PKCS7SIGNEDVECTOR(
  36321. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  36322. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  36323. NULL, 0,
  36324. "pkcs7signedCompressedFirmwarePkgData_ECDSA_SHA256_noattr.der", 0, NULL,
  36325. 0, 0, 0, 2, NULL, 0, NULL, 0, 0);
  36326. /* Signed Compressed FirmwarePkgData, ECDSA, SHA256, attrib */
  36327. ADD_PKCS7SIGNEDVECTOR(
  36328. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  36329. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  36330. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  36331. "pkcs7signedCompressedFirmwarePkgData_ECDSA_SHA256.der", 0, NULL,
  36332. 0, 0, 0, 2, NULL, 0, NULL, 0, 0);
  36333. #ifndef NO_PKCS7_ENCRYPTED_DATA
  36334. /* Signed Encrypted Compressed FirmwarePkgData, ECDSA, SHA256,
  36335. no attribs */
  36336. ADD_PKCS7SIGNEDVECTOR(
  36337. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  36338. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  36339. NULL, 0,
  36340. "pkcs7signedEncryptedCompressedFirmwarePkgData_ECDSA_SHA256_noattr.der",
  36341. 0, NULL, 0, 0, AES256CBCb, 3, aes256Key, sizeof(aes256Key), NULL,
  36342. 0, 0);
  36343. /* Signed Encrypted Compressed FirmwarePkgData, ECDSA, SHA256,
  36344. attribs */
  36345. ADD_PKCS7SIGNEDVECTOR(
  36346. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  36347. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  36348. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  36349. "pkcs7signedEncryptedCompressedFirmwarePkgData_ECDSA_SHA256.der",
  36350. 0, NULL, 0, 0, AES256CBCb, 3, aes256Key, sizeof(aes256Key),
  36351. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)), 0);
  36352. #endif /* !NO_PKCS7_ENCRYPTED_DATA */
  36353. #endif /* HAVE_LIBZ && !NO_PKCS7_COMPRESSED_DATA */
  36354. #endif /* NO_SHA256 */
  36355. #endif /* HAVE_ECC */
  36356. };
  36357. #undef MAX_TESTVECTORS_LEN
  36358. #undef ADD_PKCS7SIGNEDVECTOR
  36359. outSz = FOURK_BUF;
  36360. out = (byte*)XMALLOC(outSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  36361. if (out == NULL)
  36362. ERROR_OUT(-12540, out);
  36363. XMEMSET(out, 0, outSz);
  36364. ret = wc_PKCS7_PadData((byte*)data, sizeof(data), out, outSz, 16);
  36365. if (ret < 0)
  36366. ERROR_OUT(-12541, out);
  36367. #ifndef HAVE_FIPS
  36368. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  36369. #else
  36370. ret = wc_InitRng(&rng);
  36371. #endif
  36372. if (ret != 0)
  36373. ERROR_OUT(-12542, out);
  36374. for (i = 0; i < testSz; i++) {
  36375. if (pkcs7)
  36376. wc_PKCS7_Free(pkcs7);
  36377. pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
  36378. if (pkcs7 == NULL)
  36379. ERROR_OUT(-12543, out);
  36380. ret = wc_PKCS7_InitWithCert(pkcs7, testVectors[i].cert,
  36381. (word32)testVectors[i].certSz);
  36382. if (ret != 0)
  36383. ERROR_OUT(-12544, out);
  36384. /* load CA certificate, if present */
  36385. if (testVectors[i].caCert != NULL) {
  36386. ret = wc_PKCS7_AddCertificate(pkcs7, testVectors[i].caCert,
  36387. (word32)testVectors[i].caCertSz);
  36388. if (ret != 0)
  36389. ERROR_OUT(-12545, out);
  36390. }
  36391. /* set SignerIdentifier to use SubjectKeyIdentifier if desired,
  36392. default is IssuerAndSerialNumber */
  36393. if (testVectors[i].sidType == CMS_SKID) {
  36394. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID);
  36395. if (ret != 0)
  36396. ERROR_OUT(-12546, out);
  36397. }
  36398. if (testVectors[i].encCompFlag == 0) {
  36399. /* encode Signed FirmwarePkgData */
  36400. encodedSz = wc_PKCS7_EncodeSignedFPD(pkcs7,
  36401. testVectors[i].privateKey, testVectors[i].privateKeySz,
  36402. testVectors[i].signOID, testVectors[i].hashOID,
  36403. (byte*)testVectors[i].content, testVectors[i].contentSz,
  36404. testVectors[i].signedAttribs,
  36405. testVectors[i].signedAttribsSz, out, outSz);
  36406. if (encodedSz < 0)
  36407. ERROR_OUT(-12547, out);
  36408. #ifndef NO_PKCS7_ENCRYPTED_DATA
  36409. } else if (testVectors[i].encCompFlag == 1) {
  36410. /* encode Signed Encrypted FirmwarePkgData */
  36411. encodedSz = wc_PKCS7_EncodeSignedEncryptedFPD(pkcs7,
  36412. testVectors[i].encryptKey, testVectors[i].encryptKeySz,
  36413. testVectors[i].privateKey, testVectors[i].privateKeySz,
  36414. testVectors[i].encryptOID, testVectors[i].signOID,
  36415. testVectors[i].hashOID, (byte*)testVectors[i].content,
  36416. testVectors[i].contentSz, testVectors[i].unprotectedAttribs,
  36417. testVectors[i].unprotectedAttribsSz,
  36418. testVectors[i].signedAttribs,
  36419. testVectors[i].signedAttribsSz, out, outSz);
  36420. if (encodedSz <= 0)
  36421. ERROR_OUT(-12548, out);
  36422. #endif
  36423. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  36424. } else if (testVectors[i].encCompFlag == 2) {
  36425. /* encode Signed Compressed FirmwarePkgData */
  36426. encodedSz = wc_PKCS7_EncodeSignedCompressedFPD(pkcs7,
  36427. testVectors[i].privateKey, testVectors[i].privateKeySz,
  36428. testVectors[i].signOID, testVectors[i].hashOID,
  36429. (byte*)testVectors[i].content, testVectors[i].contentSz,
  36430. testVectors[i].signedAttribs,
  36431. testVectors[i].signedAttribsSz, out, outSz);
  36432. if (encodedSz <= 0)
  36433. ERROR_OUT(-12549, out);
  36434. #ifndef NO_PKCS7_ENCRYPTED_DATA
  36435. } else if (testVectors[i].encCompFlag == 3) {
  36436. /* encode Signed Encrypted Compressed FirmwarePkgData */
  36437. encodedSz = wc_PKCS7_EncodeSignedEncryptedCompressedFPD(pkcs7,
  36438. testVectors[i].encryptKey, testVectors[i].encryptKeySz,
  36439. testVectors[i].privateKey, testVectors[i].privateKeySz,
  36440. testVectors[i].encryptOID, testVectors[i].signOID,
  36441. testVectors[i].hashOID, (byte*)testVectors[i].content,
  36442. testVectors[i].contentSz, testVectors[i].unprotectedAttribs,
  36443. testVectors[i].unprotectedAttribsSz,
  36444. testVectors[i].signedAttribs,
  36445. testVectors[i].signedAttribsSz, out, outSz);
  36446. if (encodedSz <= 0)
  36447. ERROR_OUT(-12550, out);
  36448. #endif /* NO_PKCS7_ENCRYPTED_DATA */
  36449. #endif /* HAVE_LIBZ && !NO_PKCS7_COMPRESSED_DATA */
  36450. } else {
  36451. /* unsupported SignedData single-shot combination */
  36452. ERROR_OUT(-12551, out);
  36453. }
  36454. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  36455. /* write PKCS#7 to output file for more testing */
  36456. file = XFOPEN(testVectors[i].outFileName, "wb");
  36457. if (!file)
  36458. ERROR_OUT(-12552, out);
  36459. ret = (int)XFWRITE(out, 1, encodedSz, file);
  36460. XFCLOSE(file);
  36461. file = NULL;
  36462. if (ret != (int)encodedSz)
  36463. ERROR_OUT(-12553, out);
  36464. #endif /* PKCS7_OUTPUT_TEST_BUNDLES */
  36465. wc_PKCS7_Free(pkcs7);
  36466. pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
  36467. if (pkcs7 == NULL)
  36468. ERROR_OUT(-12554, out);
  36469. wc_PKCS7_InitWithCert(pkcs7, NULL, 0);
  36470. ret = wc_PKCS7_VerifySignedData(pkcs7, out, outSz);
  36471. if (ret < 0)
  36472. ERROR_OUT(-12555, out);
  36473. #ifndef NO_PKCS7_STREAM
  36474. {
  36475. word32 z;
  36476. for (z = 0; z < outSz && ret != 0; z++) {
  36477. ret = wc_PKCS7_VerifySignedData(pkcs7, out + z, 1);
  36478. if (ret < 0 && ret != WC_PKCS7_WANT_READ_E) {
  36479. printf("unexpected error %d\n", ret);
  36480. ERROR_OUT(-12556, out);
  36481. }
  36482. }
  36483. }
  36484. #endif
  36485. if (pkcs7->singleCert == NULL || pkcs7->singleCertSz == 0)
  36486. ERROR_OUT(-12557, out);
  36487. if (testVectors[i].encCompFlag == 0) {
  36488. /* verify decoded content matches expected */
  36489. if ((pkcs7->contentSz != testVectors[i].contentSz) ||
  36490. XMEMCMP(pkcs7->content, testVectors[i].content,
  36491. pkcs7->contentSz)) {
  36492. ERROR_OUT(-12558, out);
  36493. }
  36494. }
  36495. #ifndef NO_PKCS7_ENCRYPTED_DATA
  36496. else if (testVectors[i].encCompFlag == 1) {
  36497. /* decrypt inner encryptedData */
  36498. pkcs7->encryptionKey = testVectors[i].encryptKey;
  36499. pkcs7->encryptionKeySz = testVectors[i].encryptKeySz;
  36500. ret = wc_PKCS7_DecodeEncryptedData(pkcs7, pkcs7->content,
  36501. pkcs7->contentSz, out, outSz);
  36502. if (ret < 0)
  36503. ERROR_OUT(-12559, out);
  36504. /* compare decrypted to expected */
  36505. if (((word32)ret != testVectors[i].contentSz) ||
  36506. XMEMCMP(out, testVectors[i].content, ret))
  36507. ERROR_OUT(-12560, out);
  36508. }
  36509. #endif
  36510. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  36511. else if (testVectors[i].encCompFlag == 2) {
  36512. /* decompress inner compressedData */
  36513. ret = wc_PKCS7_DecodeCompressedData(pkcs7, pkcs7->content,
  36514. pkcs7->contentSz, out, outSz);
  36515. if (ret < 0)
  36516. ERROR_OUT(-12561, out);
  36517. /* compare decompressed to expected */
  36518. if (((word32)ret != testVectors[i].contentSz) ||
  36519. XMEMCMP(out, testVectors[i].content, ret))
  36520. ERROR_OUT(-12562, out);
  36521. }
  36522. #ifndef NO_PKCS7_ENCRYPTED_DATA
  36523. else if (testVectors[i].encCompFlag == 3) {
  36524. encryptedTmpSz = FOURK_BUF;
  36525. encryptedTmp = (byte*)XMALLOC(encryptedTmpSz, HEAP_HINT,
  36526. DYNAMIC_TYPE_TMP_BUFFER);
  36527. if (encryptedTmp == NULL)
  36528. ERROR_OUT(-12563, out);
  36529. XMEMSET(encryptedTmp, 0, encryptedTmpSz);
  36530. /* decrypt inner encryptedData */
  36531. pkcs7->encryptionKey = testVectors[i].encryptKey;
  36532. pkcs7->encryptionKeySz = testVectors[i].encryptKeySz;
  36533. encryptedTmpSz = wc_PKCS7_DecodeEncryptedData(pkcs7, pkcs7->content,
  36534. pkcs7->contentSz, encryptedTmp,
  36535. encryptedTmpSz);
  36536. if (encryptedTmpSz < 0 || pkcs7->contentOID != COMPRESSED_DATA)
  36537. ERROR_OUT(-12564, out);
  36538. /* decompress inner compressedData */
  36539. ret = wc_PKCS7_DecodeCompressedData(pkcs7, encryptedTmp,
  36540. encryptedTmpSz, out, outSz);
  36541. if (ret < 0)
  36542. ERROR_OUT(-12565, out);
  36543. /* compare decompressed to expected */
  36544. if (((word32)ret != testVectors[i].contentSz) ||
  36545. XMEMCMP(out, testVectors[i].content, ret))
  36546. ERROR_OUT(-12566, out);
  36547. }
  36548. #endif /* NO_PKCS7_ENCRYPTED_DATA */
  36549. #endif /* HAVE_LIBZ && !NO_PKCS7_COMPRESSED_DATA */
  36550. }
  36551. out:
  36552. if (pkcs7 != NULL)
  36553. wc_PKCS7_Free(pkcs7);
  36554. if (out != NULL)
  36555. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  36556. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA) && \
  36557. !defined(NO_PKCS7_ENCRYPTED_DATA)
  36558. if (encryptedTmp != NULL)
  36559. XFREE(encryptedTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  36560. #endif
  36561. if (testVectors != NULL)
  36562. XFREE(testVectors, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  36563. wc_FreeRng(&rng);
  36564. if (ret > 0)
  36565. return 0;
  36566. (void)eccClientCertBuf;
  36567. (void)eccClientCertBufSz;
  36568. (void)eccClientPrivKeyBuf;
  36569. (void)eccClientPrivKeyBufSz;
  36570. (void)rsaClientCertBuf;
  36571. (void)rsaClientCertBufSz;
  36572. (void)rsaClientPrivKeyBuf;
  36573. (void)rsaClientPrivKeyBufSz;
  36574. (void)rsaServerCertBuf;
  36575. (void)rsaServerCertBufSz;
  36576. (void)rsaServerPrivKeyBuf;
  36577. (void)rsaServerPrivKeyBufSz;
  36578. (void)rsaCaCertBuf;
  36579. (void)rsaCaCertBufSz;
  36580. (void)rsaCaPrivKeyBuf;
  36581. (void)rsaCaPrivKeyBufSz;
  36582. return ret;
  36583. }
  36584. WOLFSSL_TEST_SUBROUTINE int pkcs7signed_test(void)
  36585. {
  36586. int ret = 0;
  36587. byte* rsaClientCertBuf = NULL;
  36588. byte* rsaServerCertBuf = NULL;
  36589. byte* rsaCaCertBuf = NULL;
  36590. byte* eccClientCertBuf = NULL;
  36591. byte* rsaClientPrivKeyBuf = NULL;
  36592. byte* rsaServerPrivKeyBuf = NULL;
  36593. byte* rsaCaPrivKeyBuf = NULL;
  36594. byte* eccClientPrivKeyBuf = NULL;
  36595. word32 rsaClientCertBufSz = 0;
  36596. word32 rsaServerCertBufSz = 0;
  36597. word32 rsaCaCertBufSz = 0;
  36598. word32 eccClientCertBufSz = 0;
  36599. word32 rsaClientPrivKeyBufSz = 0;
  36600. word32 rsaServerPrivKeyBufSz = 0;
  36601. word32 rsaCaPrivKeyBufSz = 0;
  36602. word32 eccClientPrivKeyBufSz = 0;
  36603. #ifndef NO_RSA
  36604. /* read client RSA cert and key in DER format */
  36605. rsaClientCertBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  36606. DYNAMIC_TYPE_TMP_BUFFER);
  36607. if (rsaClientCertBuf == NULL)
  36608. ret = -12600;
  36609. rsaClientPrivKeyBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  36610. DYNAMIC_TYPE_TMP_BUFFER);
  36611. if (ret == 0 && rsaClientPrivKeyBuf == NULL) {
  36612. ret = -12601;
  36613. }
  36614. rsaClientCertBufSz = FOURK_BUF;
  36615. rsaClientPrivKeyBufSz = FOURK_BUF;
  36616. /* read server RSA cert and key in DER format */
  36617. rsaServerCertBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  36618. DYNAMIC_TYPE_TMP_BUFFER);
  36619. if (ret == 0 && rsaServerCertBuf == NULL)
  36620. ret = -12602;
  36621. rsaServerPrivKeyBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  36622. DYNAMIC_TYPE_TMP_BUFFER);
  36623. if (ret == 0 && rsaServerPrivKeyBuf == NULL) {
  36624. ret = -12603;
  36625. }
  36626. rsaServerCertBufSz = FOURK_BUF;
  36627. rsaServerPrivKeyBufSz = FOURK_BUF;
  36628. /* read CA RSA cert and key in DER format, for use with server cert */
  36629. rsaCaCertBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  36630. DYNAMIC_TYPE_TMP_BUFFER);
  36631. if (ret == 0 && rsaCaCertBuf == NULL)
  36632. ret = -12604;
  36633. rsaCaPrivKeyBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  36634. DYNAMIC_TYPE_TMP_BUFFER);
  36635. if (ret == 0 && rsaCaPrivKeyBuf == NULL) {
  36636. ret = -12605;
  36637. }
  36638. rsaCaCertBufSz = FOURK_BUF;
  36639. rsaCaPrivKeyBufSz = FOURK_BUF;
  36640. #endif /* NO_RSA */
  36641. #ifdef HAVE_ECC
  36642. /* read client ECC cert and key in DER format */
  36643. eccClientCertBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  36644. DYNAMIC_TYPE_TMP_BUFFER);
  36645. if (ret == 0 && eccClientCertBuf == NULL) {
  36646. ret = -12606;
  36647. }
  36648. eccClientPrivKeyBuf =(byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  36649. DYNAMIC_TYPE_TMP_BUFFER);
  36650. if (ret == 0 && eccClientPrivKeyBuf == NULL) {
  36651. ret = -12607;
  36652. }
  36653. eccClientCertBufSz = FOURK_BUF;
  36654. eccClientPrivKeyBufSz = FOURK_BUF;
  36655. #endif /* HAVE_ECC */
  36656. if (ret >= 0)
  36657. ret = pkcs7_load_certs_keys(rsaClientCertBuf, &rsaClientCertBufSz,
  36658. rsaClientPrivKeyBuf, &rsaClientPrivKeyBufSz,
  36659. rsaServerCertBuf, &rsaServerCertBufSz,
  36660. rsaServerPrivKeyBuf, &rsaServerPrivKeyBufSz,
  36661. rsaCaCertBuf, &rsaCaCertBufSz,
  36662. rsaCaPrivKeyBuf, &rsaCaPrivKeyBufSz,
  36663. eccClientCertBuf, &eccClientCertBufSz,
  36664. eccClientPrivKeyBuf, &eccClientPrivKeyBufSz);
  36665. if (ret < 0) {
  36666. ret = -12608;
  36667. }
  36668. if (ret >= 0)
  36669. ret = pkcs7signed_run_vectors(rsaClientCertBuf, (word32)rsaClientCertBufSz,
  36670. rsaClientPrivKeyBuf, (word32)rsaClientPrivKeyBufSz,
  36671. rsaServerCertBuf, (word32)rsaServerCertBufSz,
  36672. rsaServerPrivKeyBuf, (word32)rsaServerPrivKeyBufSz,
  36673. rsaCaCertBuf, (word32)rsaCaCertBufSz,
  36674. rsaCaPrivKeyBuf, (word32)rsaCaPrivKeyBufSz,
  36675. eccClientCertBuf, (word32)eccClientCertBufSz,
  36676. eccClientPrivKeyBuf, (word32)eccClientPrivKeyBufSz);
  36677. if (ret >= 0)
  36678. ret = pkcs7signed_run_SingleShotVectors(
  36679. rsaClientCertBuf, (word32)rsaClientCertBufSz,
  36680. rsaClientPrivKeyBuf, (word32)rsaClientPrivKeyBufSz,
  36681. rsaServerCertBuf, (word32)rsaServerCertBufSz,
  36682. rsaServerPrivKeyBuf, (word32)rsaServerPrivKeyBufSz,
  36683. rsaCaCertBuf, (word32)rsaCaCertBufSz,
  36684. rsaCaPrivKeyBuf, (word32)rsaCaPrivKeyBufSz,
  36685. eccClientCertBuf, (word32)eccClientCertBufSz,
  36686. eccClientPrivKeyBuf, (word32)eccClientPrivKeyBufSz);
  36687. #if !defined(NO_RSA) && !defined(NO_AES) && defined(HAVE_AES_CBC)
  36688. if (ret >= 0)
  36689. ret = pkcs7callback_test(
  36690. rsaClientCertBuf, (word32)rsaClientCertBufSz,
  36691. rsaClientPrivKeyBuf, (word32)rsaClientPrivKeyBufSz);
  36692. #endif
  36693. XFREE(rsaClientCertBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  36694. XFREE(rsaClientPrivKeyBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  36695. XFREE(rsaServerCertBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  36696. XFREE(rsaServerPrivKeyBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  36697. XFREE(rsaCaCertBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  36698. XFREE(rsaCaPrivKeyBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  36699. XFREE(eccClientCertBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  36700. XFREE(eccClientPrivKeyBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  36701. return ret;
  36702. }
  36703. #endif /* HAVE_PKCS7 */
  36704. #ifdef HAVE_VALGRIND
  36705. /* Need a static build to have access to symbols. */
  36706. /* Maximum number of bytes in a number to test. */
  36707. #define MP_MAX_TEST_BYTE_LEN 32
  36708. static int randNum(mp_int* n, int len, WC_RNG* rng, void* heap)
  36709. {
  36710. byte d[MP_MAX_TEST_BYTE_LEN];
  36711. int ret;
  36712. (void)heap;
  36713. do {
  36714. ret = wc_RNG_GenerateBlock(rng, d, len);
  36715. if (ret != 0)
  36716. return ret;
  36717. ret = mp_read_unsigned_bin(n, d, len);
  36718. if (ret != 0)
  36719. return ret;
  36720. } while (mp_iszero(n));
  36721. return 0;
  36722. }
  36723. #if defined(WOLFSSL_SP_MATH_ALL) || !defined(USE_FAST_MATH)
  36724. static int mp_test_div_3(mp_int* a, mp_int* r, WC_RNG* rng)
  36725. {
  36726. int i, j;
  36727. mp_digit rem;
  36728. mp_digit rem2;
  36729. #if (defined(WOLFSSL_SP_MATH_ALL) && !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
  36730. defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  36731. for (i = 0; i < 10; i++) {
  36732. for (j = 1; j < 10; j++) {
  36733. if (randNum(a, j, rng, NULL) != 0)
  36734. return -12620;
  36735. if (mp_div_3(a, r, &rem) != 0)
  36736. return -12621;
  36737. if (mp_mul_d(r, 3, r) != 0)
  36738. return -12622;
  36739. if (mp_add_d(r, rem, r) != 0)
  36740. return -12623;
  36741. if (mp_cmp(r, a) != MP_EQ)
  36742. return -12624;
  36743. }
  36744. }
  36745. if (mp_div_3(a, r, &rem) != 0)
  36746. return -12625;
  36747. if (mp_div_3(a, a, NULL) != 0)
  36748. return -12626;
  36749. if (mp_cmp(r, a) != MP_EQ)
  36750. return -12627;
  36751. #endif
  36752. #if defined(WOLFSSL_SP_MATH_ALL)
  36753. if (mp_div_d(a, 10, r, &rem) != 0)
  36754. return -12628;
  36755. if (mp_div_d(a, 10, a, NULL) != 0)
  36756. return -12629;
  36757. if (mp_cmp(r, a) != MP_EQ)
  36758. return -12630;
  36759. if (mp_div_d(a, 12, r, &rem) != 0)
  36760. return -12631;
  36761. if (mp_div_d(a, 12, a, NULL) != 0)
  36762. return -12632;
  36763. if (mp_cmp(r, a) != MP_EQ)
  36764. return -12633;
  36765. if (mp_div_d(a, (mp_digit)1 << (DIGIT_BIT / 2), r, &rem) != 0)
  36766. return -12634;
  36767. if (mp_div_d(a, (mp_digit)1 << (DIGIT_BIT / 2), NULL, &rem2) != 0)
  36768. return -12635;
  36769. if (mp_div_d(a, (mp_digit)1 << (DIGIT_BIT / 2), a, NULL) != 0)
  36770. return -12636;
  36771. if (mp_cmp(r, a) != MP_EQ)
  36772. return -12637;
  36773. if (rem != rem2)
  36774. return -12638;
  36775. #endif
  36776. (void)a;
  36777. (void)r;
  36778. (void)rng;
  36779. (void)i;
  36780. (void)j;
  36781. (void)rem;
  36782. (void)rem2;
  36783. return 0;
  36784. }
  36785. #endif /* WOLFSSL_SP_MATH || !USE_FAST_MATH */
  36786. #if (defined(WOLFSSL_SP_MATH_ALL) && !defined(NO_RSA) && \
  36787. !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
  36788. (!defined WOLFSSL_SP_MATH && !defined(WOLFSSL_SP_MATH_ALL) && \
  36789. (defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)))
  36790. static int mp_test_radix_10(mp_int* a, mp_int* r, WC_RNG* rng)
  36791. {
  36792. int ret;
  36793. int i, j;
  36794. int size;
  36795. char str[30];
  36796. WOLFSSL_SMALL_STACK_STATIC const char* badStr1 = "A";
  36797. WOLFSSL_SMALL_STACK_STATIC const char* badStr2 = "a";
  36798. WOLFSSL_SMALL_STACK_STATIC const char* badStr3 = " ";
  36799. WOLFSSL_SMALL_STACK_STATIC const char* zeros = "000";
  36800. WOLFSSL_SMALL_STACK_STATIC const char* empty = "";
  36801. for (i = 0; i < 10; i++) {
  36802. for (j = 2; j < 12; j++) {
  36803. if (randNum(a, j, rng, NULL) != 0)
  36804. return -12640;
  36805. if (mp_radix_size(a, MP_RADIX_DEC, &size) != MP_OKAY)
  36806. return -12641;
  36807. if (mp_toradix(a, str, MP_RADIX_DEC) != MP_OKAY)
  36808. return -12660;
  36809. if ((int)XSTRLEN(str) != size - 1)
  36810. return -12642;
  36811. if (mp_read_radix(r, str, MP_RADIX_DEC) != MP_OKAY)
  36812. return -12661;
  36813. if (mp_cmp(a, r) != MP_EQ)
  36814. return -12643;
  36815. }
  36816. }
  36817. if (mp_read_radix(r, badStr1, MP_RADIX_DEC) != MP_VAL)
  36818. return -12644;
  36819. if (mp_read_radix(r, badStr2, MP_RADIX_DEC) != MP_VAL)
  36820. return -12645;
  36821. if (mp_read_radix(r, badStr3, MP_RADIX_DEC) != MP_VAL)
  36822. return -12646;
  36823. if (mp_read_radix(r, zeros, MP_RADIX_DEC) != MP_OKAY)
  36824. return -12647;
  36825. if (!mp_iszero(r))
  36826. return -12648;
  36827. mp_set(r, 1);
  36828. if (mp_read_radix(r, empty, MP_RADIX_DEC) != MP_OKAY)
  36829. return -12649;
  36830. if (!mp_iszero(r))
  36831. return -12650;
  36832. mp_zero(a);
  36833. ret = mp_radix_size(a, MP_RADIX_DEC, &size);
  36834. if (ret != 0)
  36835. return -12651;
  36836. if (size != 2)
  36837. return -12652;
  36838. ret = mp_toradix(a, str, MP_RADIX_DEC);
  36839. if (ret != 0)
  36840. return -12653;
  36841. if ((int)XSTRLEN(str) != size - 1)
  36842. return -12654;
  36843. ret = mp_read_radix(r, str, MP_RADIX_DEC);
  36844. if (ret != 0)
  36845. return -12655;
  36846. if (!mp_iszero(r))
  36847. return -12656;
  36848. return 0;
  36849. }
  36850. #endif
  36851. #if defined(WOLFSSL_SP_MATH_ALL) || (!defined WOLFSSL_SP_MATH && \
  36852. defined(HAVE_ECC))
  36853. static int mp_test_radix_16(mp_int* a, mp_int* r, WC_RNG* rng)
  36854. {
  36855. int ret;
  36856. int i, j;
  36857. int size;
  36858. char str[30];
  36859. #if defined(WOLFSSL_SP_MATH) || defined(USE_FAST_MATH)
  36860. static char longStr[2 * sizeof(a->dp) + 2];
  36861. #endif
  36862. WOLFSSL_SMALL_STACK_STATIC const char* badStr1 = " ";
  36863. WOLFSSL_SMALL_STACK_STATIC const char* badStr2 = "}";
  36864. WOLFSSL_SMALL_STACK_STATIC const char* empty = "";
  36865. for (i = 0; i < 10; i++) {
  36866. for (j = 2; j < 12; j++) {
  36867. if (randNum(a, j, rng, NULL) != 0)
  36868. return -12660;
  36869. mp_radix_size(a, MP_RADIX_HEX, &size);
  36870. mp_toradix(a, str, MP_RADIX_HEX);
  36871. if ((int)XSTRLEN(str) != size - 1)
  36872. return -12661;
  36873. mp_read_radix(r, str, MP_RADIX_HEX);
  36874. if (mp_cmp(a, r) != MP_EQ)
  36875. return -12662;
  36876. }
  36877. }
  36878. if (mp_read_radix(r, badStr1, MP_RADIX_HEX) != MP_VAL)
  36879. return -12663;
  36880. if (mp_read_radix(r, badStr2, MP_RADIX_HEX) != MP_VAL)
  36881. return -12664;
  36882. mp_set(r, 1);
  36883. if (mp_read_radix(r, empty, MP_RADIX_HEX) != MP_OKAY)
  36884. return -12665;
  36885. if (!mp_iszero(r))
  36886. return -12666;
  36887. #if defined(WOLFSSL_SP_MATH) || defined(USE_FAST_MATH)
  36888. /* Fixed MP data size - string can be too long. */
  36889. longStr[0] = '8';
  36890. XMEMSET(longStr+1, '0', sizeof(longStr) - 2);
  36891. longStr[sizeof(longStr)-1] = '\0';
  36892. if (mp_read_radix(r, longStr, MP_RADIX_HEX) != MP_VAL)
  36893. return -12667;
  36894. #endif
  36895. mp_zero(a);
  36896. ret = mp_radix_size(a, MP_RADIX_HEX, &size);
  36897. if (ret != 0)
  36898. return -12668;
  36899. #ifndef WC_DISABLE_RADIX_ZERO_PAD
  36900. if (size != 3)
  36901. #else
  36902. if (size != 2)
  36903. #endif
  36904. return -12669;
  36905. ret = mp_toradix(a, str, MP_RADIX_HEX);
  36906. if (ret != 0)
  36907. return -12670;
  36908. if ((int)XSTRLEN(str) != size - 1)
  36909. return -12671;
  36910. ret = mp_read_radix(r, str, MP_RADIX_HEX);
  36911. if (ret != 0)
  36912. return -12672;
  36913. if (!mp_iszero(r))
  36914. return -12673;
  36915. #ifdef WOLFSSL_SP_MATH
  36916. ret = mp_toradix(a, str, 8);
  36917. if (ret != MP_VAL)
  36918. return -12674;
  36919. ret = mp_radix_size(a, 8, &size);
  36920. if (ret != MP_VAL)
  36921. return -12675;
  36922. #endif
  36923. return 0;
  36924. }
  36925. #endif
  36926. static int mp_test_shift(mp_int* a, mp_int* r1, WC_RNG* rng)
  36927. {
  36928. int i;
  36929. if (randNum(a, 4, rng, NULL) != 0)
  36930. return -12680;
  36931. for (i = 0; i < 4; i++) {
  36932. mp_copy(r1, a);
  36933. if (mp_lshd(r1, i) != MP_OKAY)
  36934. return -12681;
  36935. mp_rshd(r1, i);
  36936. if (mp_cmp(a, r1) != MP_EQ)
  36937. return -12682;
  36938. }
  36939. #ifndef WOLFSSL_SP_MATH
  36940. for (i = 0; i < DIGIT_BIT+1; i++) {
  36941. if (mp_mul_2d(a, i, r1) != MP_OKAY)
  36942. return -12683;
  36943. mp_rshb(r1, i);
  36944. if (mp_cmp(a, r1) != MP_EQ)
  36945. return -12684;
  36946. }
  36947. #endif
  36948. return 0;
  36949. }
  36950. static int mp_test_add_sub_d(mp_int* a, mp_int* r1)
  36951. {
  36952. int i, j;
  36953. for (i = 0; i <= DIGIT_BIT * 2; i++) {
  36954. mp_zero(a);
  36955. mp_set_bit(a, i);
  36956. if (a->used != (i + DIGIT_BIT) / DIGIT_BIT)
  36957. return -12690;
  36958. for (j = 0; j < i && j < DIGIT_BIT; j++) {
  36959. mp_zero(r1);
  36960. mp_set_bit(r1, i);
  36961. if (mp_sub_d(r1, (mp_digit)1 << j, r1) != MP_OKAY)
  36962. return -12691;
  36963. if (mp_add_d(r1, (mp_digit)1 << j, r1) != MP_OKAY)
  36964. return -12692;
  36965. if (mp_cmp(a, r1) != MP_EQ)
  36966. return -12693;
  36967. }
  36968. }
  36969. mp_zero(r1);
  36970. if (mp_add_d(r1, 1, r1) != MP_OKAY)
  36971. return -12694;
  36972. if (r1->used != 1)
  36973. return -12695;
  36974. if (mp_sub_d(r1, 1, r1) != MP_OKAY)
  36975. return -12696;
  36976. if (r1->used != 0)
  36977. return -12697;
  36978. return 0;
  36979. }
  36980. static int mp_test_read_to_bin(mp_int* a)
  36981. {
  36982. WOLFSSL_SMALL_STACK_STATIC const byte in[16] = {
  36983. 0x91, 0xa2, 0xb3, 0xc4, 0xd5, 0xe6, 0xf7, 0x08,
  36984. 0x93, 0xa4, 0xb4, 0xc5, 0xd6, 0xe7, 0xf8, 0x09
  36985. };
  36986. byte out[24];
  36987. int i, j, k;
  36988. const byte* p;
  36989. int ret;
  36990. for (i = 0; i < (int)sizeof(in); i++) {
  36991. p = in + sizeof(in) - i;
  36992. ret = mp_read_unsigned_bin(a, p, i);
  36993. if (ret != 0)
  36994. return -12710;
  36995. for (j = i; j < (int)sizeof(out); j++) {
  36996. XMEMSET(out, 0xff, sizeof(out));
  36997. ret = mp_to_unsigned_bin_len(a, out, j);
  36998. if (ret != 0)
  36999. return -12711;
  37000. for (k = 0; k < j - i; k++) {
  37001. if (out[k] != 0)
  37002. return -12712;
  37003. }
  37004. for (; k < j; k++) {
  37005. if (out[k] != p[k - (j - i)])
  37006. return -12713;
  37007. }
  37008. }
  37009. }
  37010. ret = mp_read_unsigned_bin(a, NULL, 0);
  37011. if (ret != 0)
  37012. return -12714;
  37013. if (!mp_iszero(a))
  37014. return -12715;
  37015. return 0;
  37016. }
  37017. #if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  37018. static int mp_test_set_int(mp_int* a)
  37019. {
  37020. #if SP_ULONG_BITS == 64
  37021. unsigned long n = 0xfedcba9876543210UL;
  37022. byte exp[8] = { 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10 };
  37023. byte out[8] = { 0 };
  37024. #elif SP_ULONG_BITS == 32
  37025. unsigned long n = 0xfedcba98UL;
  37026. byte exp[4] = { 0xfe, 0xdc, 0xba, 0x98 };
  37027. byte out[4] = { 0 };
  37028. #elif SP_ULONG_BITS == 16
  37029. unsigned long n = 0xfedc;
  37030. byte exp[2] = { 0xfe, 0xdc };
  37031. byte out[2] = { 0 };
  37032. #elif SP_ULONG_BITS == 8
  37033. unsigned long n = 0xfe;
  37034. byte exp[1] = { 0xfe };
  37035. byte out[1] = { 0 };
  37036. #endif
  37037. int ret;
  37038. ret = mp_set_int(a, n);
  37039. if (ret != 0)
  37040. return -12720;
  37041. ret = mp_unsigned_bin_size(a);
  37042. if (ret != sizeof(exp))
  37043. return -12721;
  37044. ret = mp_to_unsigned_bin(a, out);
  37045. if (ret != 0)
  37046. return -12722;
  37047. if (XMEMCMP(exp, out, sizeof(exp)) != 0)
  37048. return -12723;
  37049. return 0;
  37050. }
  37051. #endif
  37052. #if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  37053. static int mp_test_param(mp_int* a, mp_int* b, mp_int* r, WC_RNG* rng)
  37054. {
  37055. byte buffer[16];
  37056. #if defined(HAVE_ECC) || defined(WOLFSSL_SP_MATH_ALL)
  37057. char hexStr[] = "abcdef0123456789";
  37058. #ifndef WOLFSSL_SP_INT_NEGATIVE
  37059. char negStr[] = "-1234";
  37060. #endif
  37061. #endif
  37062. #if !defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_KEY_GEN) || \
  37063. defined(HAVE_COMP_KEY)
  37064. char decStr[] = "0987654321";
  37065. #endif
  37066. int ret;
  37067. #ifdef WOLFSSL_SP_MATH_ALL
  37068. mp_digit rho;
  37069. int size;
  37070. #endif
  37071. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH)
  37072. int result;
  37073. #endif
  37074. #if (defined(HAVE_ECC) && defined(HAVE_COMP_KEY)) || \
  37075. (defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN))
  37076. mp_digit rd;
  37077. #endif
  37078. (void)rng;
  37079. (void)r;
  37080. ret = mp_init(NULL);
  37081. if (ret != MP_VAL)
  37082. return -12730;
  37083. #if !defined(WOLFSSL_RSA_PUBLIC_ONLY) || (!defined(NO_DH) || defined(HAVE_ECC))
  37084. ret = mp_init_multi(NULL, NULL, NULL, NULL, NULL, NULL);
  37085. if (ret != MP_OKAY)
  37086. return -12731;
  37087. #endif
  37088. mp_free(NULL);
  37089. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) || !defined(NO_DH) || defined(HAVE_ECC)
  37090. ret = mp_grow(NULL, 1);
  37091. if (ret != MP_VAL)
  37092. return -12732;
  37093. #ifdef WOLFSSL_SP_MATH
  37094. ret = mp_grow(a, SP_INT_DIGITS + 1);
  37095. if (ret != MP_MEM)
  37096. return -12733;
  37097. #endif
  37098. #endif
  37099. mp_clear(NULL);
  37100. ret = mp_abs(NULL, NULL);
  37101. if (ret != MP_VAL)
  37102. return -12734;
  37103. ret = mp_abs(a, NULL);
  37104. if (ret != MP_VAL)
  37105. return -12735;
  37106. ret = mp_abs(NULL, b);
  37107. if (ret != MP_VAL)
  37108. return -12736;
  37109. ret = mp_unsigned_bin_size(NULL);
  37110. if (ret != 0)
  37111. return -12737;
  37112. ret = mp_read_unsigned_bin(NULL, NULL, sizeof(buffer));
  37113. if (ret != MP_VAL)
  37114. return -12738;
  37115. ret = mp_read_unsigned_bin(NULL, buffer, sizeof(buffer));
  37116. if (ret != MP_VAL)
  37117. return -12739;
  37118. ret = mp_read_unsigned_bin(a, NULL, sizeof(buffer));
  37119. if (ret != MP_VAL)
  37120. return -12740;
  37121. ret = mp_read_unsigned_bin(a, buffer, SP_INT_DIGITS * SP_WORD_SIZEOF + 1);
  37122. if (ret != MP_VAL)
  37123. return -12741;
  37124. #if defined(HAVE_ECC) || defined(WOLFSSL_SP_MATH_ALL)
  37125. ret = mp_read_radix(NULL, NULL, 16);
  37126. if (ret != MP_VAL)
  37127. return -12742;
  37128. ret = mp_read_radix(a, NULL, 16);
  37129. if (ret != MP_VAL)
  37130. return -12743;
  37131. ret = mp_read_radix(NULL, hexStr, 16);
  37132. if (ret != MP_VAL)
  37133. return -12744;
  37134. #ifndef WOLFSSL_SP_INT_NEGATIVE
  37135. ret = mp_read_radix(a, negStr, 16);
  37136. if (ret != MP_VAL)
  37137. return -12745;
  37138. #ifdef WOLFSSL_SP_MATH_ALL
  37139. ret = mp_read_radix(a, negStr, 10);
  37140. if (ret != MP_VAL)
  37141. return -12746;
  37142. #endif /* WOLFSSL_SP_MATH_ALL */
  37143. #endif /* WOLFSSL_SP_INT_NEGATIVE */
  37144. #endif
  37145. #ifndef WOLFSSL_SP_MATH_ALL
  37146. /* Radix 10 only supported with ALL. */
  37147. ret = mp_read_radix(a, decStr, 10);
  37148. if (ret != MP_VAL)
  37149. return -12747;
  37150. #endif
  37151. /* Radix 8 not supported SP_INT. */
  37152. ret = mp_read_radix(a, "0123", 8);
  37153. if (ret != MP_VAL)
  37154. return -12748;
  37155. ret = mp_count_bits(NULL);
  37156. if (ret != 0)
  37157. return -12749;
  37158. ret = mp_is_bit_set(NULL, 0);
  37159. if (ret != 0)
  37160. return -12750;
  37161. ret = mp_leading_bit(NULL);
  37162. if (ret != 0)
  37163. return -12751;
  37164. mp_zero(a);
  37165. ret = mp_leading_bit(a);
  37166. if (ret != 0)
  37167. return -12752;
  37168. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH) || \
  37169. defined(HAVE_ECC) || defined(WOLFSSL_KEY_GEN) || defined(OPENSSL_EXTRA) || \
  37170. !defined(NO_RSA)
  37171. ret = mp_set_bit(NULL, 1);
  37172. if (ret != MP_VAL)
  37173. return -12753;
  37174. #endif
  37175. #if !defined(NO_DH) || defined(HAVE_ECC) || defined(WC_RSA_BLINDING) || \
  37176. !defined(WOLFSSL_RSA_VERIFY_ONLY)
  37177. ret = mp_to_unsigned_bin(NULL, NULL);
  37178. if (ret != MP_VAL)
  37179. return -12754;
  37180. ret = mp_to_unsigned_bin(a, NULL);
  37181. if (ret != MP_VAL)
  37182. return -12755;
  37183. ret = mp_to_unsigned_bin(NULL, buffer);
  37184. if (ret != MP_VAL)
  37185. return -12756;
  37186. #endif
  37187. ret = mp_to_unsigned_bin_len(NULL, NULL, 1);
  37188. if (ret != MP_VAL)
  37189. return -12757;
  37190. ret = mp_to_unsigned_bin_len(a, NULL, 1);
  37191. if (ret != MP_VAL)
  37192. return -12758;
  37193. ret = mp_to_unsigned_bin_len(NULL, buffer, 1);
  37194. if (ret != MP_VAL)
  37195. return -12759;
  37196. #if defined(WOLFSSL_SP_MATH_ALL) && !defined(NO_RSA) && \
  37197. !defined(WOLFSSL_RSA_VERIFY_ONLY)
  37198. ret = mp_to_unsigned_bin_at_pos(0, NULL, NULL);
  37199. if (ret != MP_VAL)
  37200. return -12760;
  37201. ret = mp_to_unsigned_bin_at_pos(0, a, NULL);
  37202. if (ret != MP_VAL)
  37203. return -12761;
  37204. ret = mp_to_unsigned_bin_at_pos(0, NULL, buffer);
  37205. if (ret != MP_VAL)
  37206. return -12762;
  37207. ret = mp_to_unsigned_bin_at_pos(0, a, buffer);
  37208. if (ret != MP_OKAY)
  37209. return -12763;
  37210. #endif
  37211. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) || (!defined(NO_DH) || defined(HAVE_ECC))
  37212. ret = mp_copy(NULL, NULL);
  37213. if (ret != MP_VAL)
  37214. return -12764;
  37215. ret = mp_copy(a, NULL);
  37216. if (ret != MP_VAL)
  37217. return -12765;
  37218. ret = mp_copy(NULL, b);
  37219. if (ret != MP_VAL)
  37220. return -12766;
  37221. #endif
  37222. #if defined(WOLFSSL_KEY_GEN) || !defined(NO_DH)
  37223. ret = sp_2expt(NULL, 1);
  37224. if (ret != MP_VAL)
  37225. return -12767;
  37226. #endif
  37227. ret = mp_set(NULL, 0);
  37228. if (ret != MP_VAL)
  37229. return -12768;
  37230. ret = mp_cmp_d(NULL, 0);
  37231. if (ret != MP_LT)
  37232. return -12769;
  37233. ret = mp_cmp(NULL, NULL);
  37234. if (ret != MP_EQ)
  37235. return -12770;
  37236. ret = mp_cmp(a, NULL);
  37237. if (ret != MP_GT)
  37238. return -12771;
  37239. ret = mp_cmp(NULL, b);
  37240. if (ret != MP_LT)
  37241. return -12772;
  37242. #if !defined(NO_DH) || defined(HAVE_ECC) || !defined(WOLFSSL_RSA_VERIFY_ONLY)
  37243. mp_rshd(NULL, 1);
  37244. #endif
  37245. mp_zero(NULL);
  37246. #if !defined(NO_DH) || defined(HAVE_ECC) || defined(WC_RSA_BLINDING) || \
  37247. !defined(WOLFSSL_RSA_VERIFY_ONLY)
  37248. ret = mp_lshd(NULL, 0);
  37249. if (ret != MP_VAL)
  37250. return -12773;
  37251. ret = mp_lshd(a, SP_INT_DIGITS + 1);
  37252. if (ret != MP_VAL)
  37253. return -12774;
  37254. #endif
  37255. #if defined(WOLFSSL_SP_MATH_ALL)
  37256. ret = mp_div(NULL, NULL, a, b);
  37257. if (ret != MP_VAL)
  37258. return -12775;
  37259. ret = mp_div(a, NULL, a, b);
  37260. if (ret != MP_VAL)
  37261. return -12776;
  37262. ret = mp_div(NULL, b, a, b);
  37263. if (ret != MP_VAL)
  37264. return -12777;
  37265. ret = mp_div(a, b, NULL, NULL);
  37266. if (ret != MP_VAL)
  37267. return -12778;
  37268. #endif
  37269. #if defined(WOLFSSL_SP_MATH_ALL) || !defined(NO_DH) || defined(HAVE_ECC) || \
  37270. (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY))
  37271. ret = mp_mod(NULL, NULL, NULL);
  37272. if (ret != MP_VAL)
  37273. return -12779;
  37274. ret = mp_mod(a, NULL, NULL);
  37275. if (ret != MP_VAL)
  37276. return -12780;
  37277. ret = mp_mod(NULL, b, NULL);
  37278. if (ret != MP_VAL)
  37279. return -12781;
  37280. ret = mp_mod(NULL, NULL, r);
  37281. if (ret != MP_VAL)
  37282. return -12782;
  37283. ret = mp_mod(a, b, NULL);
  37284. if (ret != MP_VAL)
  37285. return -12783;
  37286. ret = mp_mod(a, NULL, r);
  37287. if (ret != MP_VAL)
  37288. return -12784;
  37289. ret = mp_mod(NULL, b, r);
  37290. if (ret != MP_VAL)
  37291. return -12785;
  37292. #endif
  37293. #if !defined(NO_RSA) || defined(WOLFSSL_SP_MATH_ALL)
  37294. ret = mp_set_int(NULL, 0);
  37295. if (ret != MP_VAL)
  37296. return -12786;
  37297. #endif
  37298. #if !defined(NO_RSA) || !defined(NO_DSA) || !defined(NO_DH) || \
  37299. (defined(HAVE_ECC) && defined(HAVE_COMP_KEY)) || defined(OPENSSL_EXTRA)
  37300. ret = mp_exptmod_ex(NULL, NULL, 1, NULL, NULL);
  37301. if (ret != MP_VAL)
  37302. return 9950;
  37303. ret = mp_exptmod_ex(a, NULL, 1, NULL, NULL);
  37304. if (ret != MP_VAL)
  37305. return 9951;
  37306. ret = mp_exptmod_ex(NULL, a, 1, NULL, NULL);
  37307. if (ret != MP_VAL)
  37308. return 9952;
  37309. ret = mp_exptmod_ex(NULL, NULL, 1, a, NULL);
  37310. if (ret != MP_VAL)
  37311. return 9953;
  37312. ret = mp_exptmod_ex(NULL, NULL, 1, NULL, a);
  37313. if (ret != MP_VAL)
  37314. return 9954;
  37315. ret = mp_exptmod_ex(a, a, 1, a, NULL);
  37316. if (ret != MP_VAL)
  37317. return 9955;
  37318. ret = mp_exptmod_ex(a, a, 1, NULL, a);
  37319. if (ret != MP_VAL)
  37320. return 9956;
  37321. ret = mp_exptmod_ex(a, NULL, 1, a, a);
  37322. if (ret != MP_VAL)
  37323. return 9957;
  37324. ret = mp_exptmod_ex(NULL, a, 1, a, a);
  37325. if (ret != MP_VAL)
  37326. return 9958;
  37327. ret = mp_exptmod_nct(NULL, NULL, NULL, NULL);
  37328. if (ret != MP_VAL)
  37329. return 9960;
  37330. ret = mp_exptmod_nct(a, NULL, NULL, NULL);
  37331. if (ret != MP_VAL)
  37332. return 9961;
  37333. ret = mp_exptmod_nct(NULL, a, NULL, NULL);
  37334. if (ret != MP_VAL)
  37335. return 9962;
  37336. ret = mp_exptmod_nct(NULL, NULL, a, NULL);
  37337. if (ret != MP_VAL)
  37338. return 9963;
  37339. ret = mp_exptmod_nct(NULL, NULL, NULL, a);
  37340. if (ret != MP_VAL)
  37341. return 9964;
  37342. ret = mp_exptmod_nct(a, a, a, NULL);
  37343. if (ret != MP_VAL)
  37344. return 9965;
  37345. ret = mp_exptmod_nct(a, a, NULL, a);
  37346. if (ret != MP_VAL)
  37347. return 9966;
  37348. ret = mp_exptmod_nct(a, NULL, a, a);
  37349. if (ret != MP_VAL)
  37350. return 9967;
  37351. ret = mp_exptmod_nct(NULL, a, a, a);
  37352. if (ret != MP_VAL)
  37353. return 9968;
  37354. #endif
  37355. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_DH) || !defined(NO_DSA)) && \
  37356. !defined(WC_NO_RNG)
  37357. ret = mp_rand_prime(NULL, 32, NULL, NULL);
  37358. if (ret != MP_VAL)
  37359. return -12787;
  37360. ret = mp_rand_prime(a, 32, NULL, NULL);
  37361. if (ret != MP_VAL)
  37362. return -12788;
  37363. ret = mp_rand_prime(NULL, 32, rng, NULL);
  37364. if (ret != MP_VAL)
  37365. return -12789;
  37366. ret = mp_rand_prime(a, 0, rng, NULL);
  37367. if (ret != MP_VAL)
  37368. return -9969;
  37369. #endif
  37370. #if defined(WOLFSSL_SP_MATH_ALL) && !defined(WOLFSSL_RSA_VERIFY_ONLY)
  37371. ret = mp_mul(NULL, NULL, NULL);
  37372. if (ret != MP_VAL)
  37373. return -12790;
  37374. ret = mp_mul(a, NULL, NULL);
  37375. if (ret != MP_VAL)
  37376. return -12791;
  37377. ret = mp_mul(NULL, b, NULL);
  37378. if (ret != MP_VAL)
  37379. return -12792;
  37380. ret = mp_mul(NULL, NULL, r);
  37381. if (ret != MP_VAL)
  37382. return -12793;
  37383. ret = mp_mul(a, b, NULL);
  37384. if (ret != MP_VAL)
  37385. return -12794;
  37386. ret = mp_mul(a, NULL, r);
  37387. if (ret != MP_VAL)
  37388. return -12795;
  37389. ret = mp_mul(NULL, b, r);
  37390. if (ret != MP_VAL)
  37391. return -12796;
  37392. #endif
  37393. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH) || \
  37394. defined(HAVE_ECC) || (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY))
  37395. ret = mp_sqr(NULL, NULL);
  37396. if (ret != MP_VAL)
  37397. return -12797;
  37398. ret = mp_sqr(a, NULL);
  37399. if (ret != MP_VAL)
  37400. return -12798;
  37401. ret = mp_sqr(NULL, r);
  37402. if (ret != MP_VAL)
  37403. return -12799;
  37404. #endif
  37405. #if !defined(WOLFSSL_RSA_VERIFY_ONLY)
  37406. ret = mp_sqrmod(NULL, NULL, NULL);
  37407. if (ret != MP_VAL)
  37408. return -12800;
  37409. ret = mp_sqrmod(a, NULL, NULL);
  37410. if (ret != MP_VAL)
  37411. return -12801;
  37412. ret = mp_sqrmod(NULL, a, NULL);
  37413. if (ret != MP_VAL)
  37414. return -12802;
  37415. ret = mp_sqrmod(NULL, NULL, a);
  37416. if (ret != MP_VAL)
  37417. return -12803;
  37418. ret = mp_sqrmod(a, b, NULL);
  37419. if (ret != MP_VAL)
  37420. return -12804;
  37421. ret = mp_sqrmod(a, NULL, b);
  37422. if (ret != MP_VAL)
  37423. return -12805;
  37424. ret = mp_sqrmod(NULL, a, b);
  37425. if (ret != MP_VAL)
  37426. return -12806;
  37427. ret = mp_mulmod(NULL, NULL, NULL, NULL);
  37428. if (ret != MP_VAL)
  37429. return -12807;
  37430. ret = mp_mulmod(a, NULL, NULL, NULL);
  37431. if (ret != MP_VAL)
  37432. return -12808;
  37433. ret = mp_mulmod(NULL, a, NULL, NULL);
  37434. if (ret != MP_VAL)
  37435. return -12809;
  37436. ret = mp_mulmod(NULL, NULL, a, NULL);
  37437. if (ret != MP_VAL)
  37438. return -12810;
  37439. ret = mp_mulmod(NULL, NULL, NULL, a);
  37440. if (ret != MP_VAL)
  37441. return -12811;
  37442. ret = mp_mulmod(a, b, b, NULL);
  37443. if (ret != MP_VAL)
  37444. return -12812;
  37445. ret = mp_mulmod(a, b, NULL, a);
  37446. if (ret != MP_VAL)
  37447. return -12813;
  37448. ret = mp_mulmod(a, NULL, b, a);
  37449. if (ret != MP_VAL)
  37450. return -12814;
  37451. ret = mp_mulmod(NULL, b, b, a);
  37452. if (ret != MP_VAL)
  37453. return -12815;
  37454. #endif
  37455. #if !defined(NO_PWDBASED) || defined(WOLFSSL_KEY_GEN) || !defined(NO_DH) || \
  37456. !defined(NO_RSA) || !defined(NO_DSA)
  37457. ret = mp_add_d(NULL, 1, NULL);
  37458. if (ret != MP_VAL)
  37459. return -12816;
  37460. ret = mp_add_d(a, 1, NULL);
  37461. if (ret != MP_VAL)
  37462. return -12817;
  37463. ret = mp_add_d(NULL, 1, b);
  37464. if (ret != MP_VAL)
  37465. return -12818;
  37466. #endif
  37467. #if (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
  37468. !defined(NO_DH) || defined(HAVE_ECC) || !defined(NO_DSA)
  37469. ret = mp_sub_d(NULL, 1, NULL);
  37470. if (ret != MP_VAL)
  37471. return -12819;
  37472. ret = mp_sub_d(a, 1, NULL);
  37473. if (ret != MP_VAL)
  37474. return -12820;
  37475. ret = mp_sub_d(NULL, 1, b);
  37476. if (ret != MP_VAL)
  37477. return -12821;
  37478. #endif
  37479. #if (defined(WOLFSSL_SP_MATH_ALL) && !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
  37480. defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  37481. ret = mp_div_d(NULL, 0, NULL, NULL);
  37482. if (ret != MP_VAL)
  37483. return -12822;
  37484. ret = mp_div_d(a, 0, NULL, NULL);
  37485. if (ret != MP_VAL)
  37486. return -12823;
  37487. ret = mp_div_d(NULL, 1, NULL, NULL);
  37488. if (ret != MP_VAL)
  37489. return -12824;
  37490. #endif
  37491. #if (defined(HAVE_ECC) && defined(HAVE_COMP_KEY)) || \
  37492. (defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN))
  37493. ret = mp_mod_d(NULL, 0, NULL);
  37494. if (ret != MP_VAL)
  37495. return -12825;
  37496. ret = mp_mod_d(a, 0, NULL);
  37497. if (ret != MP_VAL)
  37498. return -12826;
  37499. ret = mp_mod_d(NULL, 0, &rd);
  37500. if (ret != MP_VAL)
  37501. return -12827;
  37502. #endif
  37503. #if defined(WOLFSSL_SP_MATH_ALL) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  37504. ret = mp_gcd(NULL, NULL, NULL);
  37505. if (ret != MP_VAL)
  37506. return -12828;
  37507. ret = mp_gcd(a, NULL, NULL);
  37508. if (ret != MP_VAL)
  37509. return -12829;
  37510. ret = mp_gcd(NULL, a, NULL);
  37511. if (ret != MP_VAL)
  37512. return -12830;
  37513. ret = mp_gcd(NULL, NULL, a);
  37514. if (ret != MP_VAL)
  37515. return -12831;
  37516. ret = mp_gcd(a, b, NULL);
  37517. if (ret != MP_VAL)
  37518. return -12832;
  37519. ret = mp_gcd(a, NULL, b);
  37520. if (ret != MP_VAL)
  37521. return -12833;
  37522. ret = mp_gcd(NULL, a, b);
  37523. if (ret != MP_VAL)
  37524. return -12834;
  37525. #endif
  37526. #if !defined(WOLFSSL_SP_MATH) && defined(HAVE_ECC)
  37527. ret = mp_div_2_mod_ct(NULL, NULL, NULL);
  37528. if (ret != MP_VAL)
  37529. return -12835;
  37530. ret = mp_div_2_mod_ct(a, NULL, NULL);
  37531. if (ret != MP_VAL)
  37532. return -12836;
  37533. ret = mp_div_2_mod_ct(NULL, b, NULL);
  37534. if (ret != MP_VAL)
  37535. return -12837;
  37536. ret = mp_div_2_mod_ct(NULL, NULL, a);
  37537. if (ret != MP_VAL)
  37538. return -12838;
  37539. ret = mp_div_2_mod_ct(a, b, NULL);
  37540. if (ret != MP_VAL)
  37541. return -12839;
  37542. ret = mp_div_2_mod_ct(a, b, NULL);
  37543. if (ret != MP_VAL)
  37544. return -12840;
  37545. ret = mp_div_2_mod_ct(NULL, b, a);
  37546. if (ret != MP_VAL)
  37547. return -12841;
  37548. ret = mp_div_2(NULL, NULL);
  37549. if (ret != MP_VAL)
  37550. return -12842;
  37551. ret = mp_div_2(a, NULL);
  37552. if (ret != MP_VAL)
  37553. return -12843;
  37554. ret = mp_div_2(NULL, a);
  37555. if (ret != MP_VAL)
  37556. return -12844;
  37557. #endif
  37558. #if (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
  37559. defined(HAVE_ECC) || !defined(NO_DSA) || defined(OPENSSL_EXTRA)
  37560. ret = mp_invmod(NULL, NULL, NULL);
  37561. if (ret != MP_VAL)
  37562. return -12845;
  37563. ret = mp_invmod(a, NULL, NULL);
  37564. if (ret != MP_VAL)
  37565. return -12846;
  37566. ret = mp_invmod(NULL, b, NULL);
  37567. if (ret != MP_VAL)
  37568. return -12847;
  37569. ret = mp_invmod(NULL, NULL, a);
  37570. if (ret != MP_VAL)
  37571. return -12848;
  37572. ret = mp_invmod(a, b, NULL);
  37573. if (ret != MP_VAL)
  37574. return -12849;
  37575. ret = mp_invmod(a, NULL, a);
  37576. if (ret != MP_VAL)
  37577. return -12850;
  37578. ret = mp_invmod(NULL, b, a);
  37579. if (ret != MP_VAL)
  37580. return -12851;
  37581. #endif
  37582. #if !defined(WOLFSSL_SP_MATH) && defined(HAVE_ECC)
  37583. ret = mp_invmod_mont_ct(NULL, NULL, NULL, 1);
  37584. if (ret != MP_VAL)
  37585. return -12852;
  37586. ret = mp_invmod_mont_ct(a, NULL, NULL, 1);
  37587. if (ret != MP_VAL)
  37588. return -12853;
  37589. ret = mp_invmod_mont_ct(NULL, b, NULL, 1);
  37590. if (ret != MP_VAL)
  37591. return -12854;
  37592. ret = mp_invmod_mont_ct(NULL, NULL, a, 1);
  37593. if (ret != MP_VAL)
  37594. return -12855;
  37595. ret = mp_invmod_mont_ct(a, b, NULL, 1);
  37596. if (ret != MP_VAL)
  37597. return -12856;
  37598. ret = mp_invmod_mont_ct(a, NULL, a, 1);
  37599. if (ret != MP_VAL)
  37600. return -12857;
  37601. ret = mp_invmod_mont_ct(NULL, b, a, 1);
  37602. if (ret != MP_VAL)
  37603. return -12858;
  37604. #endif
  37605. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(WC_RSA_BLINDING)
  37606. ret = mp_lcm(NULL, NULL, NULL);
  37607. if (ret != MP_VAL)
  37608. return -12859;
  37609. ret = mp_lcm(a, NULL, NULL);
  37610. if (ret != MP_VAL)
  37611. return -12860;
  37612. ret = mp_lcm(NULL, b, NULL);
  37613. if (ret != MP_VAL)
  37614. return -12861;
  37615. ret = mp_lcm(NULL, NULL, a);
  37616. if (ret != MP_VAL)
  37617. return -12862;
  37618. ret = mp_lcm(a, b, NULL);
  37619. if (ret != MP_VAL)
  37620. return -12863;
  37621. ret = mp_lcm(a, NULL, a);
  37622. if (ret != MP_VAL)
  37623. return -12864;
  37624. ret = mp_lcm(NULL, b, a);
  37625. if (ret != MP_VAL)
  37626. return -12865;
  37627. #endif
  37628. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH)
  37629. ret = mp_exptmod_ex(NULL, NULL, 1, NULL, NULL);
  37630. if (ret != MP_VAL)
  37631. return -12866;
  37632. ret = mp_exptmod_ex(a, NULL, 1, NULL, NULL);
  37633. if (ret != MP_VAL)
  37634. return -12867;
  37635. ret = mp_exptmod_ex(NULL, b, 1, NULL, NULL);
  37636. if (ret != MP_VAL)
  37637. return -12868;
  37638. ret = mp_exptmod_ex(NULL, NULL, 1, b, NULL);
  37639. if (ret != MP_VAL)
  37640. return -12869;
  37641. ret = mp_exptmod_ex(NULL, NULL, 1, NULL, a);
  37642. if (ret != MP_VAL)
  37643. return -12870;
  37644. ret = mp_exptmod_ex(a, b, 1, b, NULL);
  37645. if (ret != MP_VAL)
  37646. return -12871;
  37647. ret = mp_exptmod_ex(a, b, 1, NULL, a);
  37648. if (ret != MP_VAL)
  37649. return -12872;
  37650. ret = mp_exptmod_ex(a, NULL, 1, b, a);
  37651. if (ret != MP_VAL)
  37652. return -12873;
  37653. ret = mp_exptmod_ex(NULL, b, 1, b, a);
  37654. if (ret != MP_VAL)
  37655. return -12874;
  37656. ret = mp_exptmod(NULL, NULL, NULL, NULL);
  37657. if (ret != MP_VAL)
  37658. return -12875;
  37659. ret = mp_exptmod(a, NULL, NULL, NULL);
  37660. if (ret != MP_VAL)
  37661. return -12876;
  37662. ret = mp_exptmod(NULL, b, NULL, NULL);
  37663. if (ret != MP_VAL)
  37664. return -12877;
  37665. ret = mp_exptmod(NULL, NULL, b, NULL);
  37666. if (ret != MP_VAL)
  37667. return -12878;
  37668. ret = mp_exptmod(NULL, NULL, NULL, a);
  37669. if (ret != MP_VAL)
  37670. return -12879;
  37671. ret = mp_exptmod(a, b, b, NULL);
  37672. if (ret != MP_VAL)
  37673. return -12880;
  37674. ret = mp_exptmod(a, b, NULL, a);
  37675. if (ret != MP_VAL)
  37676. return -12881;
  37677. ret = mp_exptmod(a, NULL, b, a);
  37678. if (ret != MP_VAL)
  37679. return -12882;
  37680. ret = mp_exptmod(NULL, b, b, a);
  37681. if (ret != MP_VAL)
  37682. return -12883;
  37683. ret = mp_exptmod_nct(NULL, NULL, NULL, NULL);
  37684. if (ret != MP_VAL)
  37685. return -12884;
  37686. ret = mp_exptmod_nct(a, NULL, NULL, NULL);
  37687. if (ret != MP_VAL)
  37688. return -12885;
  37689. ret = mp_exptmod_nct(NULL, b, NULL, NULL);
  37690. if (ret != MP_VAL)
  37691. return -12886;
  37692. ret = mp_exptmod_nct(NULL, NULL, b, NULL);
  37693. if (ret != MP_VAL)
  37694. return -12887;
  37695. ret = mp_exptmod_nct(NULL, NULL, NULL, a);
  37696. if (ret != MP_VAL)
  37697. return -12888;
  37698. ret = mp_exptmod_nct(a, b, b, NULL);
  37699. if (ret != MP_VAL)
  37700. return -12889;
  37701. ret = mp_exptmod_nct(a, b, NULL, a);
  37702. if (ret != MP_VAL)
  37703. return -12890;
  37704. ret = mp_exptmod_nct(a, NULL, b, a);
  37705. if (ret != MP_VAL)
  37706. return -12891;
  37707. ret = mp_exptmod_nct(NULL, b, b, a);
  37708. if (ret != MP_VAL)
  37709. return -12892;
  37710. #endif
  37711. #if defined(HAVE_ECC) && defined(HAVE_COMP_KEY)
  37712. ret = mp_cnt_lsb(NULL);
  37713. if (ret != 0)
  37714. return -12893;
  37715. #endif
  37716. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH)
  37717. ret = mp_prime_is_prime(NULL, 1, NULL);
  37718. if (ret != MP_VAL)
  37719. return -12894;
  37720. ret = mp_prime_is_prime(a, 1, NULL);
  37721. if (ret != MP_VAL)
  37722. return -12895;
  37723. ret = mp_prime_is_prime(NULL, 1, &result);
  37724. if (ret != MP_VAL)
  37725. return -12896;
  37726. ret = mp_prime_is_prime(a, 0, &result);
  37727. if (ret != MP_VAL)
  37728. return -12897;
  37729. ret = mp_prime_is_prime(a, 1024, &result);
  37730. if (ret != MP_VAL)
  37731. return -12898;
  37732. ret = mp_prime_is_prime_ex(NULL, 1, NULL, NULL);
  37733. if (ret != MP_VAL)
  37734. return -12899;
  37735. ret = mp_prime_is_prime_ex(a, 1, NULL, NULL);
  37736. if (ret != MP_VAL)
  37737. return -12900;
  37738. ret = mp_prime_is_prime_ex(NULL, 1, &result, NULL);
  37739. if (ret != MP_VAL)
  37740. return -12901;
  37741. ret = mp_prime_is_prime_ex(NULL, 1, NULL, rng);
  37742. if (ret != MP_VAL)
  37743. return -12902;
  37744. ret = mp_prime_is_prime_ex(a, 1, &result, NULL);
  37745. if (ret != MP_VAL)
  37746. return -12903;
  37747. ret = mp_prime_is_prime_ex(a, 1, NULL, rng);
  37748. if (ret != MP_VAL)
  37749. return -12904;
  37750. ret = mp_prime_is_prime_ex(NULL, 1, &result, rng);
  37751. if (ret != MP_VAL)
  37752. return -12905;
  37753. #endif
  37754. #if defined(WOLFSSL_SP_MATH_ALL) || !defined(NO_DH) || !defined(NO_DSA)
  37755. ret = mp_exch(NULL, NULL);
  37756. if (ret != MP_VAL)
  37757. return -12906;
  37758. ret = mp_exch(a, NULL);
  37759. if (ret != MP_VAL)
  37760. return -12907;
  37761. ret = mp_exch(NULL, b);
  37762. if (ret != MP_VAL)
  37763. return -12908;
  37764. #endif
  37765. #if (defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA)) || \
  37766. defined(WOLFSSL_SP_MATH_ALL)
  37767. ret = mp_mul_d(NULL, 1, NULL);
  37768. if (ret != MP_VAL)
  37769. return -12909;
  37770. ret = mp_mul_d(a, 1, NULL);
  37771. if (ret != MP_VAL)
  37772. return -12910;
  37773. ret = mp_mul_d(NULL, 1, b);
  37774. if (ret != MP_VAL)
  37775. return -12911;
  37776. #endif
  37777. #if !defined(WOLFSSL_RSA_VERIFY_ONLY)
  37778. ret = mp_add(NULL, NULL, NULL);
  37779. if (ret != MP_VAL)
  37780. return -12912;
  37781. ret = mp_add(a, NULL, NULL);
  37782. if (ret != MP_VAL)
  37783. return -12913;
  37784. ret = mp_add(NULL, b, NULL);
  37785. if (ret != MP_VAL)
  37786. return -12914;
  37787. ret = mp_add(NULL, NULL, r);
  37788. if (ret != MP_VAL)
  37789. return -12915;
  37790. ret = mp_add(a, b, NULL);
  37791. if (ret != MP_VAL)
  37792. return -12916;
  37793. ret = mp_add(a, NULL, r);
  37794. if (ret != MP_VAL)
  37795. return -12917;
  37796. ret = mp_add(NULL, b, r);
  37797. if (ret != MP_VAL)
  37798. return -12918;
  37799. #endif
  37800. #if defined(WOLFSSL_SP_MATH_ALL) || !defined(NO_DH) || defined(HAVE_ECC) || \
  37801. (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY))
  37802. ret = mp_sub(NULL, NULL, NULL);
  37803. if (ret != MP_VAL)
  37804. return -12919;
  37805. ret = mp_sub(a, NULL, NULL);
  37806. if (ret != MP_VAL)
  37807. return -12920;
  37808. ret = mp_sub(NULL, b, NULL);
  37809. if (ret != MP_VAL)
  37810. return -12921;
  37811. ret = mp_sub(NULL, NULL, r);
  37812. if (ret != MP_VAL)
  37813. return -12922;
  37814. ret = mp_sub(a, b, NULL);
  37815. if (ret != MP_VAL)
  37816. return -12923;
  37817. ret = mp_sub(a, NULL, r);
  37818. if (ret != MP_VAL)
  37819. return -12924;
  37820. ret = mp_sub(NULL, b, r);
  37821. if (ret != MP_VAL)
  37822. return -12925;
  37823. #endif
  37824. #if defined(WOLFSSL_SP_MATH_ALL) || (!defined(WOLFSSL_SP_MATH) && \
  37825. defined(WOLFSSL_CUSTOM_CURVES))
  37826. ret = mp_addmod(NULL, NULL, NULL, NULL);
  37827. if (ret != MP_VAL)
  37828. return -12926;
  37829. ret = mp_addmod(a, NULL, NULL, NULL);
  37830. if (ret != MP_VAL)
  37831. return -12927;
  37832. ret = mp_addmod(NULL, b, NULL, NULL);
  37833. if (ret != MP_VAL)
  37834. return -12928;
  37835. ret = mp_addmod(NULL, NULL, b, NULL);
  37836. if (ret != MP_VAL)
  37837. return -12929;
  37838. ret = mp_addmod(NULL, NULL, NULL, a);
  37839. if (ret != MP_VAL)
  37840. return -12930;
  37841. ret = mp_addmod(a, b, b, NULL);
  37842. if (ret != MP_VAL)
  37843. return -12931;
  37844. ret = mp_addmod(a, b, NULL, a);
  37845. if (ret != MP_VAL)
  37846. return -12932;
  37847. ret = mp_addmod(a, NULL, b, a);
  37848. if (ret != MP_VAL)
  37849. return -12933;
  37850. ret = mp_addmod(NULL, b, b, a);
  37851. if (ret != MP_VAL)
  37852. return -12934;
  37853. #endif
  37854. #ifdef WOLFSSL_SP_MATH_ALL
  37855. ret = mp_submod(NULL, NULL, NULL, NULL);
  37856. if (ret != MP_VAL)
  37857. return -12935;
  37858. ret = mp_submod(a, NULL, NULL, NULL);
  37859. if (ret != MP_VAL)
  37860. return -12936;
  37861. ret = mp_submod(NULL, b, NULL, NULL);
  37862. if (ret != MP_VAL)
  37863. return -12937;
  37864. ret = mp_submod(NULL, NULL, b, NULL);
  37865. if (ret != MP_VAL)
  37866. return -12938;
  37867. ret = mp_submod(NULL, NULL, NULL, a);
  37868. if (ret != MP_VAL)
  37869. return -12939;
  37870. ret = mp_submod(a, b, b, NULL);
  37871. if (ret != MP_VAL)
  37872. return -12940;
  37873. ret = mp_submod(a, b, NULL, a);
  37874. if (ret != MP_VAL)
  37875. return -12941;
  37876. ret = mp_submod(a, NULL, b, a);
  37877. if (ret != MP_VAL)
  37878. return -12942;
  37879. ret = mp_submod(NULL, b, b, a);
  37880. if (ret != MP_VAL)
  37881. return -12943;
  37882. #endif
  37883. #ifdef WOLFSSL_SP_MATH_ALL
  37884. ret = mp_div_2d(NULL, 1, a, b);
  37885. if (ret != MP_VAL)
  37886. return -12944;
  37887. ret = mp_mod_2d(NULL, 1, NULL);
  37888. if (ret != MP_VAL)
  37889. return -12945;
  37890. ret = mp_mod_2d(a, 1, NULL);
  37891. if (ret != MP_VAL)
  37892. return -12946;
  37893. ret = mp_mod_2d(NULL, 1, b);
  37894. if (ret != MP_VAL)
  37895. return -12947;
  37896. ret = mp_mul_2d(NULL, 1, NULL);
  37897. if (ret != MP_VAL)
  37898. return -12948;
  37899. ret = mp_mul_2d(a, 1, NULL);
  37900. if (ret != MP_VAL)
  37901. return -12949;
  37902. ret = mp_mul_2d(NULL, 1, b);
  37903. if (ret != MP_VAL)
  37904. return -12950;
  37905. #endif
  37906. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH) || \
  37907. defined(HAVE_ECC) || (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY))
  37908. ret = mp_montgomery_reduce(NULL, NULL, 1);
  37909. if (ret != MP_VAL)
  37910. return -12951;
  37911. ret = mp_montgomery_reduce(a, NULL, 1);
  37912. if (ret != MP_VAL)
  37913. return -12952;
  37914. ret = mp_montgomery_reduce(NULL, b, 1);
  37915. if (ret != MP_VAL)
  37916. return -12953;
  37917. mp_zero(b);
  37918. ret = mp_montgomery_reduce(a, b, 1);
  37919. if (ret != MP_VAL)
  37920. return -12954;
  37921. #endif
  37922. #ifdef WOLFSSL_SP_MATH_ALL
  37923. ret = mp_montgomery_setup(NULL, NULL);
  37924. if (ret != MP_VAL)
  37925. return -12955;
  37926. ret = mp_montgomery_setup(a, NULL);
  37927. if (ret != MP_VAL)
  37928. return -12956;
  37929. ret = mp_montgomery_setup(NULL, &rho);
  37930. if (ret != MP_VAL)
  37931. return -12957;
  37932. ret = mp_montgomery_calc_normalization(NULL, NULL);
  37933. if (ret != MP_VAL)
  37934. return -12958;
  37935. ret = mp_montgomery_calc_normalization(a, NULL);
  37936. if (ret != MP_VAL)
  37937. return -12959;
  37938. ret = mp_montgomery_calc_normalization(NULL, b);
  37939. if (ret != MP_VAL)
  37940. return -12960;
  37941. #endif
  37942. ret = mp_unsigned_bin_size(NULL);
  37943. if (ret != 0)
  37944. return -12961;
  37945. #if defined(WC_MP_TO_RADIX) || defined(WOLFSSL_SP_MATH_ALL)
  37946. ret = mp_tohex(NULL, NULL);
  37947. if (ret != MP_VAL)
  37948. return -12962;
  37949. ret = mp_tohex(a, NULL);
  37950. if (ret != MP_VAL)
  37951. return -12963;
  37952. ret = mp_tohex(NULL, hexStr);
  37953. if (ret != MP_VAL)
  37954. return -12964;
  37955. #endif
  37956. #if defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  37957. ret = mp_todecimal(NULL, NULL);
  37958. if (ret != MP_VAL)
  37959. return -12965;
  37960. ret = mp_todecimal(a, NULL);
  37961. if (ret != MP_VAL)
  37962. return -12966;
  37963. ret = mp_todecimal(NULL, decStr);
  37964. if (ret != MP_VAL)
  37965. return -12967;
  37966. #endif
  37967. #ifdef WOLFSSL_SP_MATH_ALL
  37968. ret = mp_toradix(NULL, NULL, MP_RADIX_HEX);
  37969. if (ret != MP_VAL)
  37970. return -12968;
  37971. ret = mp_toradix(a, NULL, MP_RADIX_HEX);
  37972. if (ret != MP_VAL)
  37973. return -12969;
  37974. ret = mp_toradix(NULL, hexStr, MP_RADIX_HEX);
  37975. if (ret != MP_VAL)
  37976. return -12970;
  37977. ret = mp_toradix(a, hexStr, 3);
  37978. if (ret != MP_VAL)
  37979. return -12971;
  37980. ret = mp_radix_size(NULL, MP_RADIX_HEX, NULL);
  37981. if (ret != MP_VAL)
  37982. return -12972;
  37983. ret = mp_radix_size(a, MP_RADIX_HEX, NULL);
  37984. if (ret != MP_VAL)
  37985. return -12973;
  37986. ret = mp_radix_size(NULL, MP_RADIX_HEX, &size);
  37987. if (ret != MP_VAL)
  37988. return -12974;
  37989. ret = mp_radix_size(a, 3, &size);
  37990. if (ret != MP_VAL)
  37991. return -12975;
  37992. #endif
  37993. return 0;
  37994. }
  37995. #endif
  37996. #if !defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  37997. static int mp_test_set_is_bit(mp_int* a)
  37998. {
  37999. int i, j;
  38000. mp_zero(a);
  38001. for (i = 0; i <= DIGIT_BIT * 2; i++) {
  38002. if (mp_is_bit_set(a, i))
  38003. return -12980;
  38004. for (j = 0; j < i; j++) {
  38005. if (!mp_is_bit_set(a, j))
  38006. return -12981;
  38007. }
  38008. if (mp_set_bit(a, i) != 0)
  38009. return -12982;
  38010. if (!mp_is_bit_set(a, i))
  38011. return -12983;
  38012. }
  38013. mp_zero(a);
  38014. for (i = 0; i <= DIGIT_BIT * 2; i++) {
  38015. if (mp_is_bit_set(a, i))
  38016. return -12984;
  38017. }
  38018. for (i = 0; i <= DIGIT_BIT * 2; i++) {
  38019. mp_zero(a);
  38020. if (mp_set_bit(a, i) != 0)
  38021. return -12985;
  38022. for (j = 0; j < i; j++) {
  38023. if (mp_is_bit_set(a, j))
  38024. return -12986;
  38025. }
  38026. if (!mp_is_bit_set(a, i))
  38027. return -12987;
  38028. }
  38029. #ifdef WOLFSSL_KEY_GEN
  38030. for (i = 0; i < DIGIT_BIT * 2; i++) {
  38031. mp_set(a, 1);
  38032. if (mp_2expt(a, i) != 0)
  38033. return -12988;
  38034. for (j = 0; j < i; j++) {
  38035. if (mp_is_bit_set(a, j))
  38036. return -12989;
  38037. }
  38038. if (!mp_is_bit_set(a, i))
  38039. return -12990;
  38040. }
  38041. #endif
  38042. #ifdef WOLFSSL_SP_MATH
  38043. mp_zero(a);
  38044. for (j = 1; j <= 3; j++) {
  38045. i = SP_INT_MAX_BITS - j;
  38046. if (mp_is_bit_set(a, i))
  38047. return -12991;
  38048. if (mp_set_bit(a, i) != 0)
  38049. return -12992;
  38050. if (!mp_is_bit_set(a, i))
  38051. return -12993;
  38052. #ifdef WOLFSSL_KEY_GEN
  38053. if (mp_2expt(a, i) != 0)
  38054. return -12994;
  38055. if (!mp_is_bit_set(a, i))
  38056. return -12995;
  38057. #endif
  38058. }
  38059. mp_zero(a);
  38060. for (j = 0; j <= 3; j++) {
  38061. i = SP_INT_MAX_BITS + j;
  38062. if (mp_is_bit_set(a, i))
  38063. return -12996;
  38064. if (mp_set_bit(a, i) != MP_VAL)
  38065. return -12997;
  38066. #ifdef WOLFSSL_KEY_GEN
  38067. if (mp_2expt(a, i) != MP_VAL)
  38068. return -12998;
  38069. #endif
  38070. }
  38071. #endif
  38072. return 0;
  38073. }
  38074. #endif /* !WOLFSSL_SP_MATH || WOLFSSL_SP_MATH_ALL */
  38075. static int mp_test_cmp(mp_int* a, mp_int* b)
  38076. {
  38077. int ret;
  38078. mp_zero(a);
  38079. mp_zero(b);
  38080. ret = mp_cmp_d(a, 0);
  38081. if (ret != MP_EQ)
  38082. return -13000;
  38083. ret = mp_cmp_d(a, 1);
  38084. if (ret != MP_LT)
  38085. return -13001;
  38086. ret = mp_cmp(a, b);
  38087. if (ret != MP_EQ)
  38088. return -13002;
  38089. mp_set(a, 1);
  38090. ret = mp_cmp_d(a, 0);
  38091. if (ret != MP_GT)
  38092. return -13003;
  38093. ret = mp_cmp_d(a, 1);
  38094. if (ret != MP_EQ)
  38095. return -13004;
  38096. ret = mp_cmp_d(a, 2);
  38097. if (ret != MP_LT)
  38098. return -13005;
  38099. ret = mp_cmp(a, b);
  38100. if (ret != MP_GT)
  38101. return -13006;
  38102. mp_read_radix(b, "1234567890123456789", MP_RADIX_HEX);
  38103. ret = mp_cmp_d(b, -1);
  38104. if (ret != MP_GT)
  38105. return -13007;
  38106. ret = mp_cmp(a, b);
  38107. if (ret != MP_LT)
  38108. return -13008;
  38109. ret = mp_cmp(b, a);
  38110. if (ret != MP_GT)
  38111. return -13009;
  38112. ret = mp_cmp(b, b);
  38113. if (ret != MP_EQ)
  38114. return -13010;
  38115. #if (!defined(WOLFSSL_SP_MATH) && !defined(WOLFSSL_SP_MATH_ALL)) || \
  38116. defined(WOLFSSL_SP_INT_NEGATIVE)
  38117. mp_read_radix(a, "-1", MP_RADIX_HEX);
  38118. mp_read_radix(a, "1", MP_RADIX_HEX);
  38119. ret = mp_cmp(a, b);
  38120. if (ret != MP_LT)
  38121. return -13011;
  38122. ret = mp_cmp(b, a);
  38123. if (ret != MP_GT)
  38124. return -13012;
  38125. mp_read_radix(b, "-2", MP_RADIX_HEX);
  38126. ret = mp_cmp(a, b);
  38127. if (ret != MP_GT)
  38128. return -13013;
  38129. ret = mp_cmp(b, a);
  38130. if (ret != MP_LT)
  38131. return -13014;
  38132. mp_read_radix(a, "-2", MP_RADIX_HEX);
  38133. ret = mp_cmp(a, b);
  38134. if (ret != MP_EQ)
  38135. return -13015;
  38136. #endif
  38137. return 0;
  38138. }
  38139. #if !defined(NO_DH) || defined(HAVE_ECC) || !defined(WOLFSSL_RSA_VERIFY_ONLY)
  38140. static int mp_test_shbd(mp_int* a, mp_int* b, WC_RNG* rng)
  38141. {
  38142. int ret;
  38143. int i, j, k;
  38144. #ifndef WOLFSSL_SP_MATH
  38145. for (i = 0; i < 10; i++) {
  38146. for (j = 1; j < (DIGIT_BIT + 7) / 8 * 3; j++) {
  38147. ret = randNum(a, j, rng, NULL);
  38148. if (ret != MP_OKAY)
  38149. return -13020;
  38150. mp_copy(a, b);
  38151. for (k = 0; k <= DIGIT_BIT * 2; k++) {
  38152. ret = mp_mul_2d(a, k, a);
  38153. if (ret != MP_OKAY)
  38154. return -13021;
  38155. mp_rshb(a, k);
  38156. if (mp_cmp(a, b) != MP_EQ)
  38157. return -13022;
  38158. }
  38159. }
  38160. }
  38161. #endif
  38162. for (i = 0; i < 10; i++) {
  38163. for (j = 1; j < (DIGIT_BIT + 7) / 8 * 3; j++) {
  38164. ret = randNum(a, j, rng, NULL);
  38165. if (ret != MP_OKAY)
  38166. return -13023;
  38167. mp_copy(a, b);
  38168. for (k = 0; k < 10; k++) {
  38169. ret = mp_lshd(a, k);
  38170. if (ret != MP_OKAY)
  38171. return -13024;
  38172. mp_rshd(a, k);
  38173. if (mp_cmp(a, b) != MP_EQ)
  38174. return -13025;
  38175. }
  38176. }
  38177. }
  38178. mp_zero(a);
  38179. mp_rshd(a, 1);
  38180. if (!mp_iszero(a))
  38181. return -13026;
  38182. mp_set(a, 1);
  38183. mp_rshd(a, 1);
  38184. if (!mp_iszero(a))
  38185. return -13027;
  38186. mp_set(a, 1);
  38187. mp_rshd(a, 2);
  38188. if (!mp_iszero(a))
  38189. return -13028;
  38190. return 0;
  38191. }
  38192. #endif
  38193. #if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  38194. static int mp_test_div(mp_int* a, mp_int* d, mp_int* r, mp_int* rem,
  38195. WC_RNG* rng)
  38196. {
  38197. int ret;
  38198. int i, j, k;
  38199. mp_zero(a);
  38200. mp_zero(d);
  38201. ret = mp_div(a, d, r, rem);
  38202. if (ret != MP_VAL)
  38203. return -13030;
  38204. mp_set(d, 1);
  38205. ret = mp_div(a, d, r, rem);
  38206. if (ret != MP_OKAY)
  38207. return -13031;
  38208. if (!mp_iszero(r))
  38209. return -13032;
  38210. if (!mp_iszero(rem))
  38211. return -13033;
  38212. mp_set(a, 1);
  38213. ret = mp_div(a, d, r, rem);
  38214. if (ret != MP_OKAY)
  38215. return -13034;
  38216. if (!mp_isone(r))
  38217. return -13035;
  38218. if (!mp_iszero(rem))
  38219. return -13036;
  38220. for (i = 0; i < 100; i++) {
  38221. for (j = 1; j < (DIGIT_BIT + 7) / 8 * 2; j++) {
  38222. ret = randNum(d, j, rng, NULL);
  38223. if (ret != MP_OKAY)
  38224. return -13037;
  38225. for (k = 1; k < (DIGIT_BIT + 7) / 8 * 2 + 1; k++) {
  38226. ret = randNum(a, k, rng, NULL);
  38227. if (ret != MP_OKAY)
  38228. return -13038;
  38229. ret = mp_div(a, d, NULL, rem);
  38230. if (ret != MP_OKAY)
  38231. return -13039;
  38232. ret = mp_div(a, d, r, NULL);
  38233. if (ret != MP_OKAY)
  38234. return -13040;
  38235. ret = mp_div(a, d, r, rem);
  38236. if (ret != MP_OKAY)
  38237. return -13041;
  38238. mp_mul(r, d, r);
  38239. mp_add(r, rem, r);
  38240. if (mp_cmp(r, a) != MP_EQ)
  38241. return -13042;
  38242. }
  38243. }
  38244. }
  38245. ret = randNum(d, (DIGIT_BIT + 7) / 8 * 2, rng, NULL);
  38246. if (ret != MP_OKAY)
  38247. return -13043;
  38248. mp_add(d, d, a);
  38249. mp_set(rem, 1);
  38250. mp_div(a, d, NULL, rem);
  38251. if (ret != MP_OKAY)
  38252. return -13044;
  38253. if (!mp_iszero(rem))
  38254. return -13045;
  38255. mp_set(r, 1);
  38256. mp_div(a, d, r, NULL);
  38257. if (ret != MP_OKAY)
  38258. return -13046;
  38259. if (mp_cmp_d(r, 2) != MP_EQ)
  38260. return -13047;
  38261. mp_set(r, 1);
  38262. mp_set(rem, 1);
  38263. mp_div(a, d, r, rem);
  38264. if (ret != MP_OKAY)
  38265. return -13048;
  38266. if (mp_cmp_d(r, 2) != MP_EQ)
  38267. return -13049;
  38268. if (!mp_iszero(rem))
  38269. return -13050;
  38270. mp_set(a, 0xfe);
  38271. mp_lshd(a, 3);
  38272. mp_add_d(a, 0xff, a);
  38273. mp_set(d, 0xfe);
  38274. mp_lshd(d, 2);
  38275. ret = mp_div(a, d, r, rem);
  38276. if (ret != MP_OKAY)
  38277. return -13051;
  38278. mp_mul(r, d, d);
  38279. mp_add(rem, d, d);
  38280. if (mp_cmp(a, d) != MP_EQ)
  38281. return -13052;
  38282. /* Force (hi | lo) / d to be (d | 0) / d which will would not fit in
  38283. * a digit. So mp_div must detect and handle.
  38284. * For example: 0x800000 / 0x8001, DIGIT_BIT = 8
  38285. */
  38286. mp_set(a, 1);
  38287. mp_mul_2d(a, DIGIT_BIT * 3 - 1, a);
  38288. mp_set(d, 1);
  38289. mp_mul_2d(d, DIGIT_BIT * 2 - 1, d);
  38290. mp_add_d(d, 1, d);
  38291. ret = mp_div(a, d, r, rem);
  38292. if (ret != MP_OKAY)
  38293. return -13053;
  38294. /* Make sure [d | d] / d is handled. */
  38295. mp_zero(a);
  38296. mp_set_bit(a, DIGIT_BIT * 2 - 1);
  38297. mp_set_bit(a, DIGIT_BIT * 1 - 1);
  38298. mp_zero(d);
  38299. mp_set_bit(d, DIGIT_BIT - 1);
  38300. ret = mp_div(a, d, r, rem);
  38301. if (ret != MP_OKAY)
  38302. return -13054;
  38303. mp_zero(a);
  38304. mp_set_bit(a, DIGIT_BIT);
  38305. mp_set_bit(a, 0);
  38306. mp_zero(d);
  38307. if (mp_cmp(r, a) != MP_EQ)
  38308. return -13055;
  38309. if (mp_cmp(rem, d) != MP_EQ)
  38310. return -13056;
  38311. return 0;
  38312. }
  38313. #endif
  38314. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_DH) || !defined(NO_DSA)) && \
  38315. !defined(WC_NO_RNG)
  38316. static int mp_test_prime(mp_int* a, WC_RNG* rng)
  38317. {
  38318. int ret;
  38319. int res;
  38320. ret = mp_rand_prime(a, 1, rng, NULL);
  38321. #if defined(WOLFSSL_SP_MATH_ALL)
  38322. if (ret != 0)
  38323. return -13060;
  38324. #else
  38325. if (ret != MP_VAL)
  38326. return -13060;
  38327. #endif
  38328. #ifndef WOLFSSL_SP_MATH
  38329. ret = mp_rand_prime(a, -5, rng, NULL);
  38330. if (ret != 0 || (a->dp[0] & 3) != 3)
  38331. return -13061;
  38332. #endif
  38333. ret = mp_prime_is_prime(a, 1, &res);
  38334. if (ret != MP_OKAY)
  38335. return -13062;
  38336. #ifndef WOLFSSL_SP_MATH
  38337. if (res != MP_YES)
  38338. return -13063;
  38339. #else
  38340. if (res != MP_NO)
  38341. return -13063;
  38342. #endif
  38343. ret = mp_prime_is_prime(a, 0, &res);
  38344. if (ret != MP_VAL)
  38345. return -13064;
  38346. ret = mp_prime_is_prime(a, -1, &res);
  38347. if (ret != MP_VAL)
  38348. return -13065;
  38349. ret = mp_prime_is_prime(a, 257, &res);
  38350. if (ret != MP_VAL)
  38351. return -13066;
  38352. mp_set(a, 1);
  38353. ret = mp_prime_is_prime(a, 1, &res);
  38354. if (ret != MP_OKAY)
  38355. return -13067;
  38356. if (res != MP_NO)
  38357. return -13068;
  38358. ret = mp_prime_is_prime_ex(a, 1, &res, rng);
  38359. if (ret != MP_OKAY)
  38360. return -13069;
  38361. if (res != MP_NO)
  38362. return -13070;
  38363. mp_set(a, 2);
  38364. ret = mp_prime_is_prime(a, 1, &res);
  38365. if (ret != MP_OKAY)
  38366. return -13071;
  38367. if (res != MP_YES)
  38368. return -13072;
  38369. ret = mp_prime_is_prime_ex(a, 1, &res, rng);
  38370. if (ret != MP_OKAY)
  38371. return -13073;
  38372. if (res != MP_YES)
  38373. return -13074;
  38374. mp_set(a, 0xfb);
  38375. ret = mp_prime_is_prime(a, 1, &res);
  38376. if (ret != MP_OKAY)
  38377. return -13075;
  38378. if (res != MP_YES)
  38379. return -13076;
  38380. ret = mp_prime_is_prime_ex(a, 1, &res, rng);
  38381. if (ret != MP_OKAY)
  38382. return -13077;
  38383. if (res != MP_YES)
  38384. return -13078;
  38385. mp_set(a, 0x6);
  38386. ret = mp_prime_is_prime(a, 1, &res);
  38387. if (ret != MP_OKAY)
  38388. return -13079;
  38389. if (res != MP_NO)
  38390. return -13080;
  38391. ret = mp_prime_is_prime_ex(a, 1, &res, rng);
  38392. if (ret != MP_OKAY)
  38393. return -13081;
  38394. if (res != MP_NO)
  38395. return -13082;
  38396. mp_set_int(a, 0x655 * 0x65b);
  38397. ret = mp_prime_is_prime(a, 10, &res);
  38398. if (ret != MP_OKAY)
  38399. return -13083;
  38400. if (res != MP_NO)
  38401. return -13084;
  38402. ret = mp_prime_is_prime_ex(a, 10, &res, rng);
  38403. if (ret != MP_OKAY)
  38404. return -13085;
  38405. if (res != MP_NO)
  38406. return -13086;
  38407. return 0;
  38408. }
  38409. #endif
  38410. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(WC_RSA_BLINDING)
  38411. static int mp_test_lcm_gcd(mp_int* a, mp_int* b, mp_int* r, mp_int* exp,
  38412. WC_RNG* rng)
  38413. {
  38414. int ret;
  38415. int i;
  38416. WOLFSSL_SMALL_STACK_STATIC const int kat[][3] = {
  38417. { 1, 1, 1 }, { 2, 1, 2 }, { 1, 2, 2 }, { 2, 4, 4 }, { 4, 2, 4 },
  38418. { 12, 56, 168 }, { 56, 12, 168 }
  38419. };
  38420. (void)exp;
  38421. mp_set(a, 0);
  38422. mp_set(b, 1);
  38423. ret = mp_lcm(a, a, r);
  38424. if (ret != MP_VAL)
  38425. return -13090;
  38426. ret = mp_lcm(a, b, r);
  38427. if (ret != MP_VAL)
  38428. return -13091;
  38429. ret = mp_lcm(b, a, r);
  38430. if (ret != MP_VAL)
  38431. return -13092;
  38432. for (i = 0; i < (int)(sizeof(kat) / sizeof(*kat)); i++) {
  38433. mp_set(a, kat[i][0]);
  38434. mp_set(b, kat[i][1]);
  38435. ret = mp_lcm(a, b, r);
  38436. if (ret != MP_OKAY)
  38437. return -13093;
  38438. mp_set(exp, kat[i][2]);
  38439. if (mp_cmp(r, exp) != MP_EQ)
  38440. return -13094;
  38441. }
  38442. (void)rng;
  38443. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_DH) || !defined(NO_DSA)) && \
  38444. !defined(WC_NO_RNG)
  38445. if (mp_rand_prime(a, 20, rng, NULL) != MP_OKAY)
  38446. return -13095;
  38447. if (mp_rand_prime(b, 20, rng, NULL) != MP_OKAY)
  38448. return -13096;
  38449. if (mp_mul(a, b, exp) != MP_OKAY)
  38450. return -13097;
  38451. ret = mp_lcm(a, b, r);
  38452. if (ret != MP_OKAY)
  38453. return -13098;
  38454. if (mp_cmp(r, exp) != MP_EQ)
  38455. return -13099;
  38456. ret = mp_lcm(b, a, r);
  38457. if (ret != MP_OKAY)
  38458. return -13100;
  38459. if (mp_cmp(r, exp) != MP_EQ)
  38460. return -13101;
  38461. #endif
  38462. mp_set(a, 11);
  38463. mp_zero(b);
  38464. ret = mp_gcd(a, b, r);
  38465. if (ret != MP_OKAY)
  38466. return -13102;
  38467. if (mp_cmp_d(r, 11) != MP_EQ)
  38468. return -13103;
  38469. ret = mp_gcd(b, a, r);
  38470. if (ret != MP_OKAY)
  38471. return -13104;
  38472. if (mp_cmp_d(r, 11) != MP_EQ)
  38473. return -13105;
  38474. ret = mp_gcd(b, b, r);
  38475. if (ret != MP_VAL)
  38476. return -13106;
  38477. return 0;
  38478. }
  38479. #endif
  38480. #if (!defined(WOLFSSL_SP_MATH) && !defined(USE_FAST_MATH)) || \
  38481. defined(WOLFSSL_SP_MATH_ALL)
  38482. static int mp_test_mod_2d(mp_int* a, mp_int* r, mp_int* t, WC_RNG* rng)
  38483. {
  38484. int ret;
  38485. int i;
  38486. int j;
  38487. mp_set(a, 10);
  38488. ret = mp_mod_2d(a, 0, r);
  38489. if (ret != MP_OKAY)
  38490. return -13110;
  38491. if (!mp_iszero(r))
  38492. return -13111;
  38493. ret = mp_mod_2d(a, 1, r);
  38494. if (ret != MP_OKAY)
  38495. return -13112;
  38496. if (!mp_iszero(r))
  38497. return -13113;
  38498. ret = mp_mod_2d(a, 2, r);
  38499. if (ret != MP_OKAY)
  38500. return -13114;
  38501. if (mp_cmp_d(r, 2))
  38502. return -13115;
  38503. for (i = 2; i < 20; i++) {
  38504. ret = randNum(a, i, rng, NULL);
  38505. if (ret != 0)
  38506. return -13116;
  38507. for (j = 1; j <= mp_count_bits(a); j++) {
  38508. /* Get top part */
  38509. ret = mp_div_2d(a, j, t, NULL);
  38510. if (ret != 0)
  38511. return -13117;
  38512. ret = mp_mul_2d(t, j, t);
  38513. if (ret != 0)
  38514. return -13118;
  38515. /* Get bottom part */
  38516. ret = mp_mod_2d(a, j, r);
  38517. if (ret != 0)
  38518. return -13119;
  38519. /* Reassemble */
  38520. ret = mp_add(t, r, r);
  38521. if (ret != 0)
  38522. return -13120;
  38523. if (mp_cmp(a, r) != MP_EQ)
  38524. return -13121;
  38525. }
  38526. }
  38527. #if !defined(WOLFSSL_SP_MATH) && defined(WOLFSSL_SP_INT_NEGATIVE)
  38528. /* Test negative value being moded. */
  38529. for (j = 0; j < 20; j++) {
  38530. ret = randNum(a, 2, rng, NULL);
  38531. if (ret != 0)
  38532. return -13122;
  38533. a->sign = MP_NEG;
  38534. for (i = 1; i < DIGIT_BIT * 3 + 1; i++) {
  38535. ret = mp_mod_2d(a, i, r);
  38536. if (ret != 0)
  38537. return -13124;
  38538. mp_zero(t);
  38539. ret = mp_set_bit(t, i);
  38540. if (ret != 0)
  38541. return -13125;
  38542. ret = mp_mod(a, t, t);
  38543. if (ret != 0)
  38544. return -13126;
  38545. if (mp_cmp(r, t) != MP_EQ)
  38546. return -13127;
  38547. }
  38548. }
  38549. #endif
  38550. return 0;
  38551. }
  38552. #endif
  38553. #if (defined(HAVE_ECC) && defined(HAVE_COMP_KEY)) || \
  38554. (defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN))
  38555. static int mp_test_mod_d(mp_int* a)
  38556. {
  38557. int ret;
  38558. mp_digit r;
  38559. if (mp_set(a, 1) != MP_OKAY)
  38560. return -13130;
  38561. ret = mp_mod_d(a, 0, &r);
  38562. if (ret != MP_VAL)
  38563. return -13131;
  38564. mp_zero(a);
  38565. ret = mp_mod_d(a, 1, &r);
  38566. if (ret != MP_OKAY)
  38567. return -13132;
  38568. ret = mp_mod_d(a, 3, &r);
  38569. if (ret != MP_OKAY)
  38570. return -13133;
  38571. ret = mp_mod_d(a, 5, &r);
  38572. if (ret != MP_OKAY)
  38573. return -13134;
  38574. return 0;
  38575. }
  38576. #endif
  38577. static int mp_test_mul_sqr(mp_int* a, mp_int* b, mp_int* r1, mp_int* r2,
  38578. WC_RNG* rng)
  38579. {
  38580. int ret;
  38581. int i;
  38582. for (i = 1; i < 16; i++) {
  38583. ret = randNum(a, i, rng, NULL);
  38584. if (ret != 0)
  38585. return -13140;
  38586. ret = mp_mul(a, a, r1);
  38587. if (ret != 0)
  38588. return -13141;
  38589. ret = mp_sqr(a, r2);
  38590. if (ret != 0)
  38591. return -13142;
  38592. if (mp_cmp(r1, r2) != MP_EQ)
  38593. return -13143;
  38594. }
  38595. ret = mp_set(b, 0);
  38596. if (ret != MP_OKAY)
  38597. return -13144;
  38598. ret = mp_mul(a, b, r1);
  38599. if (ret != MP_OKAY)
  38600. return -13145;
  38601. if (!mp_iszero(r1))
  38602. return -13146;
  38603. ret = mp_sqr(b, r1);
  38604. if (ret != MP_OKAY)
  38605. return -13147;
  38606. if (!mp_iszero(r1))
  38607. return -13148;
  38608. #ifdef WOLFSSL_SP_MATH_ALL
  38609. ret = mp_set(a, 1);
  38610. if (ret != MP_OKAY)
  38611. return -13149;
  38612. i = (SP_INT_DIGITS / 2) + 1;
  38613. ret = mp_mul_2d(a, i * SP_WORD_SIZE - 1, a);
  38614. if (ret != MP_OKAY)
  38615. return -13150;
  38616. ret = mp_set(b, 1);
  38617. if (ret != MP_OKAY)
  38618. return -13151;
  38619. ret = mp_mul_2d(b, (SP_INT_DIGITS - 1 - i) * SP_WORD_SIZE - 1, b);
  38620. if (ret != MP_OKAY)
  38621. return -13152;
  38622. ret = mp_mul(a, b, r1);
  38623. if (ret != MP_OKAY)
  38624. return -13153;
  38625. ret = mp_mul(a, a, r1);
  38626. if (ret == MP_OKAY)
  38627. return -13154;
  38628. ret = mp_sqr(a, r1);
  38629. if (ret == MP_OKAY)
  38630. return -13155;
  38631. ret = mp_sqr(b, r1);
  38632. if (ret != MP_OKAY)
  38633. return -13156;
  38634. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH) || \
  38635. (defined(HAVE_ECC) && defined(FP_ECC))
  38636. ret = mp_mulmod(a, b, b, r1);
  38637. if (ret != MP_OKAY)
  38638. return -13157;
  38639. ret = mp_mulmod(a, a, b, r1);
  38640. if (ret == MP_OKAY)
  38641. return -13158;
  38642. #if defined(HAVE_ECC) && (defined(ECC_SHAMIR) || defined(FP_ECC))
  38643. ret = mp_sqrmod(a, b, r1);
  38644. if (ret == MP_OKAY)
  38645. return -13159;
  38646. ret = mp_sqrmod(b, a, r1);
  38647. if (ret != MP_OKAY)
  38648. return -13160;
  38649. #endif /* HAVE_ECC && (ECC_SHAMIR || FP_ECC) */
  38650. #endif /* WOLFSSL_SP_MATH_ALL || WOLFSSL_HAVE_SP_DH || (HAVE_ECC && FP_ECC) */
  38651. #endif /* WOLFSSL_SP_MATH_ALL */
  38652. return 0;
  38653. }
  38654. #if !defined(NO_RSA) || defined(HAVE_ECC) || !defined(NO_DSA) || \
  38655. defined(OPENSSL_EXTRA)
  38656. static int mp_test_invmod(mp_int* a, mp_int* m, mp_int* r)
  38657. {
  38658. int ret;
  38659. mp_set(a, 0);
  38660. mp_set(m, 1);
  38661. ret = mp_invmod(a, m, r);
  38662. if (ret != MP_VAL)
  38663. return -13170;
  38664. ret = mp_invmod(m, a, r);
  38665. if (ret != MP_VAL)
  38666. return -13171;
  38667. mp_set(a, 2);
  38668. mp_set(m, 4);
  38669. ret = mp_invmod(a, m, r);
  38670. if (ret != MP_VAL)
  38671. return -13172;
  38672. mp_set(a, 1);
  38673. mp_set(m, 4);
  38674. ret = mp_invmod(a, m, r);
  38675. if (ret != MP_OKAY)
  38676. return -13173;
  38677. if (!mp_isone(r))
  38678. return -13174;
  38679. mp_set(a, 3);
  38680. mp_set(m, 4);
  38681. ret = mp_invmod(a, m, r);
  38682. if (ret != MP_OKAY)
  38683. return -13175;
  38684. if (mp_cmp_d(r, 3))
  38685. return -13176;
  38686. mp_set(a, 3);
  38687. mp_set(m, 5);
  38688. ret = mp_invmod(a, m, r);
  38689. if (ret != MP_OKAY)
  38690. return -13177;
  38691. #if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  38692. /* Maximum 'a' */
  38693. mp_set(a, 0);
  38694. mp_set_bit(a, (r->size / 2)* SP_WORD_SIZE - 1);
  38695. mp_sub_d(a, 1, a);
  38696. /* Modulus too big. */
  38697. mp_set(m, 0);
  38698. mp_set_bit(m, (r->size / 2) * SP_WORD_SIZE);
  38699. ret = mp_invmod(a, m, r);
  38700. if (ret != MP_VAL)
  38701. return -13178;
  38702. /* Maximum modulus - even. */
  38703. mp_set(m, 0);
  38704. mp_set_bit(m, (r->size / 2) * SP_WORD_SIZE - 1);
  38705. ret = mp_invmod(a, m, r);
  38706. if (ret != MP_OKAY)
  38707. return -13179;
  38708. #endif
  38709. #if !defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_INT_NEGATIVE)
  38710. mp_read_radix(a, "-3", 16);
  38711. ret = mp_invmod(a, m, r);
  38712. if (ret != MP_OKAY)
  38713. return -13180;
  38714. #endif
  38715. #if defined(WOLFSSL_SP_MATH_ALL) && defined(HAVE_ECC)
  38716. mp_set(a, 0);
  38717. mp_set(m, 3);
  38718. ret = mp_invmod_mont_ct(a, m, r, 1);
  38719. if (ret != MP_VAL)
  38720. return -13190;
  38721. mp_set(a, 1);
  38722. mp_set(m, 0);
  38723. ret = mp_invmod_mont_ct(a, m, r, 1);
  38724. if (ret != MP_VAL)
  38725. return -13191;
  38726. mp_set(a, 1);
  38727. mp_set(m, 1);
  38728. ret = mp_invmod_mont_ct(a, m, r, 1);
  38729. if (ret != MP_VAL)
  38730. return -13192;
  38731. mp_set(a, 1);
  38732. mp_set(m, 2);
  38733. ret = mp_invmod_mont_ct(a, m, r, 1);
  38734. if (ret != MP_VAL)
  38735. return -13193;
  38736. mp_set(a, 1);
  38737. mp_set(m, 3);
  38738. ret = mp_invmod_mont_ct(a, m, r, 1);
  38739. if (ret != MP_OKAY)
  38740. return -13194;
  38741. #endif
  38742. return 0;
  38743. }
  38744. #endif /* !NO_RSA || HAVE_ECC || !NO_DSA || OPENSSL_EXTRA */
  38745. #if !defined(NO_RSA) || !defined(NO_DSA) || !defined(NO_DH) || \
  38746. (defined(HAVE_ECC) && defined(HAVE_COMP_KEY)) || defined(OPENSSL_EXTRA)
  38747. static int mp_test_exptmod(mp_int* b, mp_int* e, mp_int* m, mp_int* r)
  38748. {
  38749. int ret;
  38750. mp_set(b, 0x2);
  38751. mp_set(e, 0x3);
  38752. mp_set(m, 0x0);
  38753. ret = mp_exptmod_ex(b, e, 1, m, r);
  38754. if (ret != MP_VAL)
  38755. return -13190;
  38756. ret = mp_exptmod_nct(b, e, m, r);
  38757. if (ret != MP_VAL)
  38758. return -13191;
  38759. mp_set(b, 0x2);
  38760. mp_set(e, 0x3);
  38761. mp_set(m, 0x1);
  38762. ret = mp_exptmod_ex(b, e, 1, m, r);
  38763. if (ret != MP_OKAY)
  38764. return -13192;
  38765. if (!mp_iszero(r))
  38766. return -13193;
  38767. ret = mp_exptmod_nct(b, e, m, r);
  38768. if (ret != MP_OKAY)
  38769. return -13194;
  38770. if (!mp_iszero(r))
  38771. return -13195;
  38772. mp_set(b, 0x2);
  38773. mp_set(e, 0x0);
  38774. mp_set(m, 0x7);
  38775. ret = mp_exptmod_ex(b, e, 1, m, r);
  38776. if (ret != MP_OKAY)
  38777. return -13196;
  38778. if (!mp_isone(r))
  38779. return -13197;
  38780. ret = mp_exptmod_nct(b, e, m, r);
  38781. if (ret != MP_OKAY)
  38782. return -13198;
  38783. if (!mp_isone(r))
  38784. return -13199;
  38785. mp_set(b, 0x0);
  38786. mp_set(e, 0x3);
  38787. mp_set(m, 0x7);
  38788. ret = mp_exptmod_ex(b, e, 1, m, r);
  38789. if (ret != MP_OKAY)
  38790. return -13200;
  38791. if (!mp_iszero(r))
  38792. return -13201;
  38793. ret = mp_exptmod_nct(b, e, m, r);
  38794. if (ret != MP_OKAY)
  38795. return -13202;
  38796. if (!mp_iszero(r))
  38797. return -13203;
  38798. mp_set(b, 0x10);
  38799. mp_set(e, 0x3);
  38800. mp_set(m, 0x7);
  38801. ret = mp_exptmod_ex(b, e, 1, m, r);
  38802. if (ret != MP_OKAY)
  38803. return -13204;
  38804. ret = mp_exptmod_nct(b, e, m, r);
  38805. if (ret != MP_OKAY)
  38806. return -13205;
  38807. mp_set(b, 0x7);
  38808. mp_set(e, 0x3);
  38809. mp_set(m, 0x7);
  38810. ret = mp_exptmod_ex(b, e, 1, m, r);
  38811. if (ret != MP_OKAY)
  38812. return -13206;
  38813. if (!mp_iszero(r))
  38814. return -13207;
  38815. ret = mp_exptmod_nct(b, e, m, r);
  38816. if (ret != MP_OKAY)
  38817. return -13208;
  38818. if (!mp_iszero(r))
  38819. return -13209;
  38820. #ifndef WOLFSSL_SP_MATH
  38821. mp_set(b, 0x01);
  38822. mp_mul_2d(b, DIGIT_BIT, b);
  38823. mp_add_d(b, 1, b);
  38824. mp_set(e, 0x3);
  38825. mp_copy(b, m);
  38826. ret = mp_exptmod_ex(b, e, 1, m, r);
  38827. if (ret != MP_OKAY)
  38828. return -13210;
  38829. if (!mp_iszero(r))
  38830. return -13211;
  38831. ret = mp_exptmod_nct(b, e, m, r);
  38832. if (ret != MP_OKAY)
  38833. return -13212;
  38834. if (!mp_iszero(r))
  38835. return -13213;
  38836. #endif
  38837. mp_set(b, 0x2);
  38838. mp_set(e, 0x3);
  38839. mp_set(m, 0x7);
  38840. ret = mp_exptmod_ex(b, e, 1, m, r);
  38841. if (ret != MP_OKAY)
  38842. return -13214;
  38843. ret = mp_exptmod_nct(b, e, m, r);
  38844. if (ret != MP_OKAY)
  38845. return -13215;
  38846. #ifdef WOLFSSL_SP_MATH_ALL
  38847. mp_set(b, 0x2);
  38848. mp_set(e, 0x3);
  38849. mp_set(m, 0x01);
  38850. mp_mul_2d(m, SP_WORD_SIZE * SP_INT_DIGITS / 2, m);
  38851. mp_add_d(m, 0x01, m);
  38852. ret = mp_exptmod_ex(b, e, 1, m, r);
  38853. if (ret != MP_VAL)
  38854. return -13216;
  38855. ret = mp_exptmod_nct(b, e, m, r);
  38856. if (ret != MP_VAL)
  38857. return -13217;
  38858. #endif
  38859. return 0;
  38860. }
  38861. #endif /* !NO_RSA || !NO_DSA || !NO_DH || (HAVE_ECC && HAVE_COMP_KEY) ||
  38862. * OPENSSL_EXTRA */
  38863. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH) || \
  38864. defined(HAVE_ECC) || (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY))
  38865. static int mp_test_mont(mp_int* a, mp_int* m, mp_int* n, mp_int* r, WC_RNG* rng)
  38866. {
  38867. int ret;
  38868. mp_digit mp;
  38869. static int exp[] = { 7, 8, 16, 27, 32, 64,
  38870. 127, 128, 255, 256,
  38871. #if defined(SP_WORD_SIZE) && SP_WORD_SIZE > 8
  38872. 383, 384, 2033, 2048
  38873. #endif
  38874. };
  38875. static mp_digit sub[] = { 0x01, 0x05, 0x0f, 0x27, 0x05, 0x3b,
  38876. 0x01, 0x9f, 0x13, 0xbd,
  38877. #if defined(SP_WORD_SIZE) && SP_WORD_SIZE > 8
  38878. 0x1f, 0x13d, 0x45, 0x615
  38879. #endif
  38880. };
  38881. int bits[] = { 256, 384,
  38882. #if defined(SP_INT_MAX_BITS) && SP_INT_MAX_BITS > 4096
  38883. 2048,
  38884. #endif
  38885. #if defined(SP_INT_MAX_BITS) && SP_INT_MAX_BITS > 6144
  38886. 3072
  38887. #endif
  38888. };
  38889. int i;
  38890. int j;
  38891. for (i = 0; i < (int)(sizeof(exp) / sizeof(*exp)); i++) {
  38892. if (exp[i] >= DIGIT_BIT)
  38893. continue;
  38894. mp_zero(m);
  38895. ret = mp_set_bit(m, exp[i]);
  38896. if (ret != MP_OKAY)
  38897. return -13220;
  38898. ret = mp_sub_d(m, sub[i], m);
  38899. if (ret != MP_OKAY)
  38900. return -13221;
  38901. ret = mp_montgomery_setup(m, &mp);
  38902. if (ret != MP_OKAY)
  38903. return -13222;
  38904. ret = mp_montgomery_calc_normalization(n, m);
  38905. if (ret != MP_OKAY)
  38906. return -13223;
  38907. for (j = 0; j < 10; j++) {
  38908. ret = randNum(a, (exp[i] + DIGIT_BIT - 1) / DIGIT_BIT, rng, NULL);
  38909. if (ret != 0)
  38910. return -13224;
  38911. ret = mp_mod(a, m, a);
  38912. if (ret != 0)
  38913. return -13225;
  38914. /* r = a * a */
  38915. ret = mp_sqrmod(a, m, r);
  38916. if (ret != MP_OKAY)
  38917. return -13226;
  38918. /* Convert to Montgomery form = a*n */
  38919. ret = mp_mulmod(a, n, m, a);
  38920. if (ret != MP_OKAY)
  38921. return -13227;
  38922. /* a*a mod m == ((a*n) * (a*n)) / n / n */
  38923. ret = mp_sqr(a, a);
  38924. if (ret != MP_OKAY)
  38925. return -13228;
  38926. ret = mp_montgomery_reduce(a, m, mp);
  38927. if (ret != MP_OKAY)
  38928. return -13229;
  38929. ret = mp_montgomery_reduce(a, m, mp);
  38930. if (ret != MP_OKAY)
  38931. return -13230;
  38932. if (mp_cmp(a, r) != MP_EQ)
  38933. return -13231;
  38934. }
  38935. }
  38936. /* Force carries. */
  38937. for (i = 0; i < (int)(sizeof(bits) / sizeof(*bits)); i++) {
  38938. /* a = 2^(bits*2) - 1 */
  38939. mp_zero(a);
  38940. mp_set_bit(a, bits[i] * 2);
  38941. mp_sub_d(a, 1, a);
  38942. /* m = 2^(bits) - 1 */
  38943. mp_zero(m);
  38944. mp_set_bit(m, bits[i]);
  38945. mp_sub_d(m, 1, m);
  38946. mp = 1;
  38947. /* result = r = 2^(bits) - 1 */
  38948. mp_zero(r);
  38949. mp_set_bit(r, bits[i]);
  38950. mp_sub_d(r, 1, r);
  38951. ret = mp_montgomery_reduce(a, m, mp);
  38952. if (ret != MP_OKAY)
  38953. return -13240;
  38954. /* Result is m or 0 if reduced to range of modulus. */
  38955. if (mp_cmp(a, r) != MP_EQ && mp_iszero(a) != MP_YES)
  38956. return -13241;
  38957. }
  38958. return 0;
  38959. }
  38960. #endif
  38961. WOLFSSL_TEST_SUBROUTINE int mp_test(void)
  38962. {
  38963. WC_RNG rng;
  38964. int ret;
  38965. #if defined(HAVE_ECC) || defined(WOLFSSL_KEY_GEN)
  38966. int i, j;
  38967. #ifndef WOLFSSL_SP_MATH
  38968. int k;
  38969. #endif
  38970. mp_digit d;
  38971. #endif
  38972. mp_int a, b, r1, r2, p;
  38973. ret = mp_init_multi(&a, &b, &r1, &r2, NULL, NULL);
  38974. if (ret != 0)
  38975. return -13300;
  38976. #ifdef WOLFSSL_SP_MATH_ALL
  38977. mp_init_copy(&p, &a);
  38978. #else
  38979. ret = mp_init(&p);
  38980. if (ret != 0)
  38981. return -13301;
  38982. #endif
  38983. #ifndef HAVE_FIPS
  38984. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  38985. #else
  38986. ret = wc_InitRng(&rng);
  38987. #endif
  38988. if (ret != 0)
  38989. goto done;
  38990. #if defined(HAVE_ECC) || defined(WOLFSSL_KEY_GEN)
  38991. mp_set_int(&a, 0);
  38992. if (a.used != 0 || a.dp[0] != 0)
  38993. return -13302;
  38994. for (j = 1; j <= MP_MAX_TEST_BYTE_LEN; j++) {
  38995. for (i = 0; i < 4 * j; i++) {
  38996. /* New values to use. */
  38997. ret = randNum(&p, j, &rng, NULL);
  38998. if (ret != 0)
  38999. return -13303;
  39000. ret = randNum(&a, j, &rng, NULL);
  39001. if (ret != 0)
  39002. return -13304;
  39003. ret = randNum(&b, j, &rng, NULL);
  39004. if (ret != 0)
  39005. return -13305;
  39006. ret = wc_RNG_GenerateBlock(&rng, (byte*)&d, sizeof(d));
  39007. if (ret != 0)
  39008. return -13306;
  39009. d &= MP_MASK;
  39010. #if !defined(WOLFSSL_SP_MATH) || (defined(HAVE_ECC) && \
  39011. (defined(ECC_SHAMIR) || defined(FP_ECC)))
  39012. /* Ensure sqrmod produce same result as mulmod. */
  39013. ret = mp_sqrmod(&a, &p, &r1);
  39014. if (ret != 0)
  39015. return -13307;
  39016. ret = mp_mulmod(&a, &a, &p, &r2);
  39017. if (ret != 0)
  39018. return -13308;
  39019. if (mp_cmp(&r1, &r2) != 0)
  39020. return -13309;
  39021. #endif
  39022. #if defined(WOLFSSL_SP_MATH_ALL) && !defined(WOLFSSL_RSA_VERIFY_ONLY)
  39023. #if defined(WOLFSSL_SP_MATH) || (defined(WOLFSSL_SP_MATH_ALL) && \
  39024. !defined(WOLFSSL_SP_INT_NEGATIVE))
  39025. ret = mp_addmod(&a, &b, &p, &r1);
  39026. if (ret != 0)
  39027. return -13310;
  39028. ret = mp_submod(&r1, &b, &p, &r2);
  39029. if (ret != 0)
  39030. return -13311;
  39031. ret = mp_mod(&a, &p, &r1);
  39032. if (ret != 0)
  39033. return -13312;
  39034. if (mp_cmp(&r1, &r2) != MP_EQ)
  39035. return -13313;
  39036. #else
  39037. /* Ensure add with mod produce same result as sub with mod. */
  39038. ret = mp_addmod(&a, &b, &p, &r1);
  39039. if (ret != 0)
  39040. return -13314;
  39041. b.sign ^= 1;
  39042. ret = mp_submod(&a, &b, &p, &r2);
  39043. if (ret != 0)
  39044. return -13315;
  39045. if (mp_cmp(&r1, &r2) != 0)
  39046. return -13316;
  39047. #endif
  39048. #endif
  39049. /* Ensure add digit produce same result as sub digit. */
  39050. ret = mp_add_d(&a, d, &r1);
  39051. if (ret != 0)
  39052. return -13317;
  39053. ret = mp_sub_d(&r1, d, &r2);
  39054. if (ret != 0)
  39055. return -13318;
  39056. if (mp_cmp(&a, &r2) != 0)
  39057. return -13319;
  39058. /* Invert - if p is even it will use the slow impl.
  39059. * - if p and a are even it will fail.
  39060. */
  39061. ret = mp_invmod(&a, &p, &r1);
  39062. if (ret != 0 && ret != MP_VAL)
  39063. return -13320;
  39064. ret = 0;
  39065. #ifndef WOLFSSL_SP_MATH
  39066. /* Shift up and down number all bits in a digit. */
  39067. for (k = 0; k < DIGIT_BIT; k++) {
  39068. mp_mul_2d(&a, k, &r1);
  39069. mp_div_2d(&r1, k, &r2, &p);
  39070. if (mp_cmp(&a, &r2) != 0)
  39071. return -13321;
  39072. if (!mp_iszero(&p))
  39073. return -13322;
  39074. mp_rshb(&r1, k);
  39075. if (mp_cmp(&a, &r1) != 0)
  39076. return -13323;
  39077. }
  39078. #endif
  39079. }
  39080. }
  39081. /* Test adding and subtracting zero from zero. */
  39082. mp_zero(&a);
  39083. ret = mp_add_d(&a, 0, &r1);
  39084. if (ret != 0)
  39085. return -13329;
  39086. if (!mp_iszero(&r1)) {
  39087. return -13330;
  39088. }
  39089. ret = mp_sub_d(&a, 0, &r2);
  39090. if (ret != 0)
  39091. return -13331;
  39092. if (!mp_iszero(&r2)) {
  39093. return -13332;
  39094. }
  39095. #if DIGIT_BIT >= 32
  39096. /* Check that setting a 32-bit digit works. */
  39097. d &= 0xffffffffU;
  39098. mp_set_int(&a, d);
  39099. if (a.used != 1 || a.dp[0] != d)
  39100. return -13324;
  39101. #endif
  39102. /* Check setting a bit and testing a bit works. */
  39103. for (i = 0; i < MP_MAX_TEST_BYTE_LEN * 8; i++) {
  39104. mp_zero(&a);
  39105. mp_set_bit(&a, i);
  39106. if (!mp_is_bit_set(&a, i))
  39107. return -13325;
  39108. }
  39109. #endif
  39110. #if defined(HAVE_ECC) && defined(HAVE_COMP_KEY)
  39111. mp_zero(&a);
  39112. i = mp_cnt_lsb(&a);
  39113. if (i != 0)
  39114. return -13326;
  39115. mp_set(&a, 1);
  39116. i = mp_cnt_lsb(&a);
  39117. if (i != 0)
  39118. return -13327;
  39119. mp_set(&a, 32);
  39120. i = mp_cnt_lsb(&a);
  39121. if (i != 5)
  39122. return -13328;
  39123. mp_zero(&a);
  39124. mp_set_bit(&a, 129);
  39125. i = mp_cnt_lsb(&a);
  39126. if (i != 129)
  39127. return -13328;
  39128. #endif
  39129. #if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  39130. if ((ret = mp_test_param(&a, &b, &r1, &rng)) != 0)
  39131. return ret;
  39132. #endif
  39133. #if defined(WOLFSSL_SP_MATH_ALL) || !defined(USE_FAST_MATH)
  39134. if ((ret = mp_test_div_3(&a, &r1, &rng)) != 0)
  39135. return ret;
  39136. #endif
  39137. #if (defined(WOLFSSL_SP_MATH_ALL) && !defined(NO_RSA) && \
  39138. !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
  39139. (!defined WOLFSSL_SP_MATH && !defined(WOLFSSL_SP_MATH_ALL) && \
  39140. (defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)))
  39141. if ((ret = mp_test_radix_10(&a, &r1, &rng)) != 0)
  39142. return ret;
  39143. #endif
  39144. #if defined(WOLFSSL_SP_MATH_ALL) || (!defined WOLFSSL_SP_MATH && \
  39145. defined(HAVE_ECC))
  39146. if ((ret = mp_test_radix_16(&a, &r1, &rng)) != 0)
  39147. return ret;
  39148. #endif
  39149. if ((ret = mp_test_shift(&a, &r1, &rng)) != 0)
  39150. return ret;
  39151. if ((ret = mp_test_add_sub_d(&a, &r1)) != 0)
  39152. return ret;
  39153. if ((ret = mp_test_read_to_bin(&a)) != 0)
  39154. return ret;
  39155. #if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  39156. if ((ret = mp_test_set_int(&a)) != 0)
  39157. return ret;
  39158. #endif
  39159. if ((ret = mp_test_cmp(&a, &r1)) != 0)
  39160. return ret;
  39161. #if !defined(NO_DH) || defined(HAVE_ECC) || !defined(WOLFSSL_RSA_VERIFY_ONLY)
  39162. if ((ret = mp_test_shbd(&a, &b, &rng)) != 0)
  39163. return ret;
  39164. #endif
  39165. #if !defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  39166. if ((ret = mp_test_set_is_bit(&a)) != 0)
  39167. return ret;
  39168. #endif
  39169. #if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  39170. if ((ret = mp_test_div(&a, &b, &r1, &r2, &rng)) != 0)
  39171. return ret;
  39172. #endif
  39173. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_DH) || !defined(NO_DSA)) && \
  39174. !defined(WC_NO_RNG)
  39175. if ((ret = mp_test_prime(&a, &rng)) != 0)
  39176. return ret;
  39177. #endif
  39178. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(WC_RSA_BLINDING)
  39179. if ((ret = mp_test_lcm_gcd(&a, &b, &r1, &r2, &rng)) != 0)
  39180. return ret;
  39181. #endif
  39182. #if (!defined(WOLFSSL_SP_MATH) && !defined(USE_FAST_MATH)) || \
  39183. defined(WOLFSSL_SP_MATH_ALL)
  39184. if ((ret = mp_test_mod_2d(&a, &r1, &p, &rng)) != 0)
  39185. return ret;
  39186. #endif
  39187. #if (defined(HAVE_ECC) && defined(HAVE_COMP_KEY)) || \
  39188. (defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN))
  39189. if ((ret = mp_test_mod_d(&a)) != 0)
  39190. return ret;
  39191. #endif
  39192. if ((ret = mp_test_mul_sqr(&a, &b, &r1, &r2, &rng)) != 0)
  39193. return ret;
  39194. #if !defined(NO_RSA) || defined(HAVE_ECC) || !defined(NO_DSA) || \
  39195. defined(OPENSSL_EXTRA)
  39196. if ((ret = mp_test_invmod(&a, &b, &r1)) != 0)
  39197. return ret;
  39198. #endif
  39199. #if !defined(NO_RSA) || !defined(NO_DSA) || !defined(NO_DH) || \
  39200. (defined(HAVE_ECC) && defined(HAVE_COMP_KEY)) || defined(OPENSSL_EXTRA)
  39201. if ((ret = mp_test_exptmod(&a, &b, &r1, &r2)) != 0)
  39202. return ret;
  39203. #endif
  39204. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH) || \
  39205. defined(HAVE_ECC) || (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY))
  39206. if ((ret = mp_test_mont(&a, &b, &r1, &r2, &rng)) != 0)
  39207. return ret;
  39208. #endif
  39209. done:
  39210. mp_clear(&p);
  39211. mp_clear(&r2);
  39212. mp_clear(&r1);
  39213. mp_clear(&b);
  39214. mp_clear(&a);
  39215. wc_FreeRng(&rng);
  39216. return ret;
  39217. }
  39218. #endif
  39219. #if defined(WOLFSSL_PUBLIC_MP) && defined(WOLFSSL_KEY_GEN)
  39220. typedef struct pairs_t {
  39221. const unsigned char* coeff;
  39222. int coeffSz;
  39223. int exp;
  39224. } pairs_t;
  39225. /*
  39226. n =p1p2p3, where pi = ki(p1-1)+1 with (k2,k3) = (173,293)
  39227. p1 = 2^192 * 0x000000000000e24fd4f6d6363200bf2323ec46285cac1d3a
  39228. + 2^0 * 0x0b2488b0c29d96c5e67f8bec15b54b189ae5636efe89b45b
  39229. */
  39230. static const unsigned char c192a[] =
  39231. {
  39232. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe2, 0x4f,
  39233. 0xd4, 0xf6, 0xd6, 0x36, 0x32, 0x00, 0xbf, 0x23,
  39234. 0x23, 0xec, 0x46, 0x28, 0x5c, 0xac, 0x1d, 0x3a
  39235. };
  39236. static const unsigned char c0a[] =
  39237. {
  39238. 0x0b, 0x24, 0x88, 0xb0, 0xc2, 0x9d, 0x96, 0xc5,
  39239. 0xe6, 0x7f, 0x8b, 0xec, 0x15, 0xb5, 0x4b, 0x18,
  39240. 0x9a, 0xe5, 0x63, 0x6e, 0xfe, 0x89, 0xb4, 0x5b
  39241. };
  39242. static const pairs_t ecPairsA[] =
  39243. {
  39244. {c192a, sizeof(c192a), 192},
  39245. {c0a, sizeof(c0a), 0}
  39246. };
  39247. static const int kA[] = {173, 293};
  39248. static const unsigned char controlPrime[] = {
  39249. 0xe1, 0x76, 0x45, 0x80, 0x59, 0xb6, 0xd3, 0x49,
  39250. 0xdf, 0x0a, 0xef, 0x12, 0xd6, 0x0f, 0xf0, 0xb7,
  39251. 0xcb, 0x2a, 0x37, 0xbf, 0xa7, 0xf8, 0xb5, 0x4d,
  39252. 0xf5, 0x31, 0x35, 0xad, 0xe4, 0xa3, 0x94, 0xa1,
  39253. 0xdb, 0xf1, 0x96, 0xad, 0xb5, 0x05, 0x64, 0x85,
  39254. 0x83, 0xfc, 0x1b, 0x5b, 0x29, 0xaa, 0xbe, 0xf8,
  39255. 0x26, 0x3f, 0x76, 0x7e, 0xad, 0x1c, 0xf0, 0xcb,
  39256. 0xd7, 0x26, 0xb4, 0x1b, 0x05, 0x8e, 0x56, 0x86,
  39257. 0x7e, 0x08, 0x62, 0x21, 0xc1, 0x86, 0xd6, 0x47,
  39258. 0x79, 0x3e, 0xb7, 0x5d, 0xa4, 0xc6, 0x3a, 0xd7,
  39259. 0xb1, 0x74, 0x20, 0xf6, 0x50, 0x97, 0x41, 0x04,
  39260. 0x53, 0xed, 0x3f, 0x26, 0xd6, 0x6f, 0x91, 0xfa,
  39261. 0x68, 0x26, 0xec, 0x2a, 0xdc, 0x9a, 0xf1, 0xe7,
  39262. 0xdc, 0xfb, 0x73, 0xf0, 0x79, 0x43, 0x1b, 0x21,
  39263. 0xa3, 0x59, 0x04, 0x63, 0x52, 0x07, 0xc9, 0xd7,
  39264. 0xe6, 0xd1, 0x1b, 0x5d, 0x5e, 0x96, 0xfa, 0x53
  39265. };
  39266. static const unsigned char testOne[] = { 1 };
  39267. static int GenerateNextP(mp_int* p1, mp_int* p2, int k)
  39268. {
  39269. int ret;
  39270. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  39271. mp_int *ki = (mp_int *)XMALLOC(sizeof(*ki), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  39272. if (ki == NULL)
  39273. return MEMORY_E;
  39274. #else
  39275. mp_int ki[1];
  39276. #endif
  39277. ret = mp_init(ki);
  39278. if (ret == 0)
  39279. ret = mp_set(ki, k);
  39280. if (ret == 0)
  39281. ret = mp_sub_d(p1, 1, p2);
  39282. if (ret == 0)
  39283. ret = mp_mul(p2, ki, p2);
  39284. if (ret == 0)
  39285. ret = mp_add_d(p2, 1, p2);
  39286. mp_clear(ki);
  39287. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  39288. XFREE(ki, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  39289. #endif
  39290. return ret;
  39291. }
  39292. static int GenerateP(mp_int* p1, mp_int* p2, mp_int* p3,
  39293. const pairs_t* ecPairs, int ecPairsSz,
  39294. const int* k)
  39295. {
  39296. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  39297. mp_int *x = NULL, *y = NULL;
  39298. #else
  39299. mp_int x[1], y[1];
  39300. #endif
  39301. int ret, i;
  39302. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  39303. if (((x = (mp_int *)XMALLOC(sizeof(*x), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER)) == NULL) ||
  39304. ((y = (mp_int *)XMALLOC(sizeof(*x), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER)) == NULL)) {
  39305. ret = MEMORY_E;
  39306. goto out;
  39307. }
  39308. #endif
  39309. ret = mp_init_multi(x, y, NULL, NULL, NULL, NULL);
  39310. if (ret != 0) {
  39311. ret = MP_MEM;
  39312. goto out;
  39313. }
  39314. for (i = 0; ret == 0 && i < ecPairsSz; i++) {
  39315. ret = mp_read_unsigned_bin(x, ecPairs[i].coeff, ecPairs[i].coeffSz);
  39316. /* p1 = 2^exp */
  39317. if (ret == 0)
  39318. ret = mp_2expt(y, ecPairs[i].exp);
  39319. /* p1 = p1 * m */
  39320. if (ret == 0)
  39321. ret = mp_mul(x, y, x);
  39322. /* p1 += */
  39323. if (ret == 0)
  39324. ret = mp_add(p1, x, p1);
  39325. mp_zero(x);
  39326. mp_zero(y);
  39327. }
  39328. if (ret == 0)
  39329. ret = GenerateNextP(p1, p2, k[0]);
  39330. if (ret == 0)
  39331. ret = GenerateNextP(p1, p3, k[1]);
  39332. out:
  39333. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  39334. if (x != NULL) {
  39335. mp_clear(x);
  39336. XFREE(x, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  39337. }
  39338. if (y != NULL) {
  39339. mp_clear(y);
  39340. XFREE(y, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  39341. }
  39342. #else
  39343. mp_clear(x);
  39344. mp_clear(y);
  39345. #endif
  39346. return ret;
  39347. }
  39348. WOLFSSL_TEST_SUBROUTINE int prime_test(void)
  39349. {
  39350. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  39351. mp_int *n = (mp_int *)XMALLOC(sizeof *n, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER),
  39352. *p1 = (mp_int *)XMALLOC(sizeof *p1, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER),
  39353. *p2 = (mp_int *)XMALLOC(sizeof *p2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER),
  39354. *p3 = (mp_int *)XMALLOC(sizeof *p3, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  39355. #else
  39356. mp_int n[1],
  39357. p1[1],
  39358. p2[1],
  39359. p3[1];
  39360. #endif
  39361. int ret, isPrime = 0;
  39362. WC_RNG rng;
  39363. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  39364. if ((n == NULL) ||
  39365. (p1 == NULL) ||
  39366. (p2 == NULL) ||
  39367. (p3 == NULL))
  39368. ERROR_OUT(MEMORY_E, out);
  39369. #endif
  39370. ret = wc_InitRng(&rng);
  39371. if (ret == 0)
  39372. ret = mp_init_multi(n, p1, p2, p3, NULL, NULL);
  39373. if (ret == 0)
  39374. ret = GenerateP(p1, p2, p3,
  39375. ecPairsA, sizeof(ecPairsA) / sizeof(ecPairsA[0]), kA);
  39376. if (ret == 0)
  39377. ret = mp_mul(p1, p2, n);
  39378. if (ret == 0)
  39379. ret = mp_mul(n, p3, n);
  39380. if (ret != 0)
  39381. ERROR_OUT(-13400, out);
  39382. /* Check the old prime test using the number that false positives.
  39383. * This test result should indicate as not prime. */
  39384. ret = mp_prime_is_prime(n, 40, &isPrime);
  39385. if (ret != 0)
  39386. ERROR_OUT(-13401, out);
  39387. if (isPrime)
  39388. ERROR_OUT(-13402, out);
  39389. /* This test result should fail. It should indicate the value as prime. */
  39390. ret = mp_prime_is_prime(n, 8, &isPrime);
  39391. if (ret != 0)
  39392. ERROR_OUT(-13403, out);
  39393. if (!isPrime)
  39394. ERROR_OUT(-13404, out);
  39395. /* This test result should indicate the value as not prime. */
  39396. ret = mp_prime_is_prime_ex(n, 8, &isPrime, &rng);
  39397. if (ret != 0)
  39398. ERROR_OUT(-13405, out);
  39399. if (isPrime)
  39400. ERROR_OUT(-13406, out);
  39401. ret = mp_read_unsigned_bin(n, controlPrime, sizeof(controlPrime));
  39402. if (ret != 0)
  39403. ERROR_OUT(-13407, out);
  39404. /* This test result should indicate the value as prime. */
  39405. ret = mp_prime_is_prime_ex(n, 8, &isPrime, &rng);
  39406. if (ret != 0)
  39407. ERROR_OUT(-13408, out);
  39408. if (!isPrime)
  39409. ERROR_OUT(-13409, out);
  39410. /* This test result should indicate the value as prime. */
  39411. isPrime = -1;
  39412. ret = mp_prime_is_prime(n, 8, &isPrime);
  39413. if (ret != 0)
  39414. ERROR_OUT(-13410, out);
  39415. if (!isPrime)
  39416. ERROR_OUT(-13411, out);
  39417. ret = mp_read_unsigned_bin(n, testOne, sizeof(testOne));
  39418. if (ret != 0)
  39419. ERROR_OUT(-13412, out);
  39420. /* This test result should indicate the value as not prime. */
  39421. ret = mp_prime_is_prime_ex(n, 8, &isPrime, &rng);
  39422. if (ret != 0)
  39423. ERROR_OUT(-13413, out);
  39424. if (isPrime)
  39425. ERROR_OUT(-13414, out);
  39426. ret = mp_prime_is_prime(n, 8, &isPrime);
  39427. if (ret != 0)
  39428. ERROR_OUT(-13415, out);
  39429. if (isPrime)
  39430. ERROR_OUT(-13416, out);
  39431. ret = 0;
  39432. out:
  39433. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  39434. if (n != NULL) {
  39435. mp_clear(n);
  39436. XFREE(n, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  39437. }
  39438. if (p1 != NULL) {
  39439. mp_clear(p1);
  39440. XFREE(p1, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  39441. }
  39442. if (p2 != NULL) {
  39443. mp_clear(p2);
  39444. XFREE(p2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  39445. }
  39446. if (p3 != NULL) {
  39447. mp_clear(p3);
  39448. XFREE(p3, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  39449. }
  39450. #else
  39451. mp_clear(p3);
  39452. mp_clear(p2);
  39453. mp_clear(p1);
  39454. mp_clear(n);
  39455. #endif
  39456. wc_FreeRng(&rng);
  39457. return ret;
  39458. }
  39459. #endif /* WOLFSSL_PUBLIC_MP */
  39460. #if defined(ASN_BER_TO_DER) && \
  39461. (defined(WOLFSSL_TEST_CERT) || defined(OPENSSL_EXTRA) || \
  39462. defined(OPENSSL_EXTRA_X509_SMALL))
  39463. /* wc_BerToDer is only public facing in the case of test cert or opensslextra */
  39464. typedef struct berDerTestData {
  39465. const byte *in;
  39466. word32 inSz;
  39467. const byte *out;
  39468. word32 outSz;
  39469. } berDerTestData;
  39470. WOLFSSL_TEST_SUBROUTINE int berder_test(void)
  39471. {
  39472. int ret;
  39473. int i;
  39474. word32 len = 0, l;
  39475. byte out[32];
  39476. WOLFSSL_SMALL_STACK_STATIC const byte good1_in[] = { 0x30, 0x80, 0x00, 0x00 };
  39477. WOLFSSL_SMALL_STACK_STATIC const byte good1_out[] = { 0x30, 0x00 };
  39478. WOLFSSL_SMALL_STACK_STATIC const byte good2_in[] = { 0x30, 0x80, 0x02, 0x01, 0x01, 0x00, 0x00 };
  39479. WOLFSSL_SMALL_STACK_STATIC const byte good2_out[] = { 0x30, 0x03, 0x02, 0x01, 0x01 };
  39480. WOLFSSL_SMALL_STACK_STATIC const byte good3_in[] = {
  39481. 0x24, 0x80, 0x04, 0x01, 0x01, 0x00, 0x00
  39482. };
  39483. WOLFSSL_SMALL_STACK_STATIC const byte good3_out[] = { 0x04, 0x1, 0x01 };
  39484. WOLFSSL_SMALL_STACK_STATIC const byte good4_in[] = {
  39485. 0x30, 0x80,
  39486. 0x02, 0x01, 0x01,
  39487. 0x30, 0x80,
  39488. 0x24, 0x80,
  39489. 0x04, 0x01, 0x01,
  39490. 0x04, 0x02, 0x02, 0x03,
  39491. 0x00, 0x00,
  39492. 0x06, 0x01, 0x01,
  39493. 0x00, 0x00,
  39494. 0x31, 0x80,
  39495. 0x06, 0x01, 0x01,
  39496. 0x00, 0x00,
  39497. 0x00, 0x00,
  39498. };
  39499. WOLFSSL_SMALL_STACK_STATIC const byte good4_out[] = {
  39500. 0x30, 0x12,
  39501. 0x02, 0x01, 0x01,
  39502. 0x30, 0x08,
  39503. 0x04, 0x03, 0x01, 0x02, 0x03,
  39504. 0x06, 0x01, 0x01,
  39505. 0x31, 0x03,
  39506. 0x06, 0x01, 0x01
  39507. };
  39508. WOLFSSL_SMALL_STACK_STATIC const byte good5_in[] = { 0x30, 0x03, 0x02, 0x01, 0x01 };
  39509. berDerTestData testData[] = {
  39510. { good1_in, sizeof(good1_in), good1_out, sizeof(good1_out) },
  39511. { good2_in, sizeof(good2_in), good2_out, sizeof(good2_out) },
  39512. { good3_in, sizeof(good3_in), good3_out, sizeof(good3_out) },
  39513. { good4_in, sizeof(good4_in), good4_out, sizeof(good4_out) },
  39514. { good5_in, sizeof(good5_in), good5_in , sizeof(good5_in ) },
  39515. };
  39516. for (i = 0; i < (int)(sizeof(testData) / sizeof(*testData)); i++) {
  39517. ret = wc_BerToDer(testData[i].in, testData[i].inSz, NULL, &len);
  39518. if (ret != LENGTH_ONLY_E)
  39519. return -13500 - i;
  39520. if (len != testData[i].outSz)
  39521. return -13510 - i;
  39522. len = testData[i].outSz;
  39523. ret = wc_BerToDer(testData[i].in, testData[i].inSz, out, &len);
  39524. if (ret != 0)
  39525. return -13520 - i;
  39526. if (XMEMCMP(out, testData[i].out, len) != 0)
  39527. return -13530 - i;
  39528. for (l = 1; l < testData[i].inSz; l++) {
  39529. ret = wc_BerToDer(testData[i].in, l, NULL, &len);
  39530. if (ret != ASN_PARSE_E)
  39531. return -13540;
  39532. len = testData[i].outSz;
  39533. ret = wc_BerToDer(testData[i].in, l, out, &len);
  39534. if (ret != ASN_PARSE_E)
  39535. return -13541;
  39536. }
  39537. for (l = 0; l < testData[i].outSz-1; l++) {
  39538. ret = wc_BerToDer(testData[i].in, testData[i].inSz, out, &l);
  39539. if (ret != BUFFER_E)
  39540. return -13542;
  39541. }
  39542. }
  39543. ret = wc_BerToDer(NULL, 4, NULL, NULL);
  39544. if (ret != BAD_FUNC_ARG)
  39545. return -13543;
  39546. ret = wc_BerToDer(out, 4, NULL, NULL);
  39547. if (ret != BAD_FUNC_ARG)
  39548. return -13544;
  39549. ret = wc_BerToDer(NULL, 4, NULL, &len);
  39550. if (ret != BAD_FUNC_ARG)
  39551. return -13545;
  39552. ret = wc_BerToDer(NULL, 4, out, NULL);
  39553. if (ret != BAD_FUNC_ARG)
  39554. return -13546;
  39555. ret = wc_BerToDer(out, 4, out, NULL);
  39556. if (ret != BAD_FUNC_ARG)
  39557. return -13547;
  39558. ret = wc_BerToDer(NULL, 4, out, &len);
  39559. if (ret != BAD_FUNC_ARG)
  39560. return -13548;
  39561. for (l = 1; l < sizeof(good4_out); l++) {
  39562. len = l;
  39563. ret = wc_BerToDer(good4_in, sizeof(good4_in), out, &len);
  39564. if (ret != BUFFER_E)
  39565. return -13549;
  39566. }
  39567. return 0;
  39568. }
  39569. #endif
  39570. #ifdef DEBUG_WOLFSSL
  39571. static THREAD_LS_T int log_cnt = 0;
  39572. static void my_Logging_cb(const int logLevel, const char *const logMessage)
  39573. {
  39574. (void)logLevel;
  39575. (void)logMessage;
  39576. log_cnt++;
  39577. }
  39578. #endif /* DEBUG_WOLFSSL */
  39579. WOLFSSL_TEST_SUBROUTINE int logging_test(void)
  39580. {
  39581. #ifdef DEBUG_WOLFSSL
  39582. const char* msg = "Testing, testing. 1, 2, 3, 4 ...";
  39583. byte a[8] = { 1, 2, 3, 4, 5, 6, 7, 8 };
  39584. byte b[256];
  39585. int i;
  39586. for (i = 0; i < (int)sizeof(b); i++)
  39587. b[i] = i;
  39588. if (wolfSSL_Debugging_ON() != 0)
  39589. return -13600;
  39590. if (wolfSSL_SetLoggingCb(my_Logging_cb) != 0)
  39591. return -13601;
  39592. WOLFSSL_MSG(msg);
  39593. WOLFSSL_BUFFER(a, sizeof(a));
  39594. WOLFSSL_BUFFER(b, sizeof(b));
  39595. WOLFSSL_BUFFER(NULL, 0);
  39596. WOLFSSL_ERROR(MEMORY_E);
  39597. WOLFSSL_ERROR_MSG(msg);
  39598. /* turn off logs */
  39599. wolfSSL_Debugging_OFF();
  39600. /* capture log count */
  39601. i = log_cnt;
  39602. /* validate no logs are output when disabled */
  39603. WOLFSSL_MSG(msg);
  39604. WOLFSSL_BUFFER(a, sizeof(a));
  39605. WOLFSSL_BUFFER(b, sizeof(b));
  39606. WOLFSSL_BUFFER(NULL, 0);
  39607. WOLFSSL_ERROR(MEMORY_E);
  39608. WOLFSSL_ERROR_MSG(msg);
  39609. /* check the logs were disabled */
  39610. if (i != log_cnt)
  39611. return -13602;
  39612. /* restore callback and leave logging enabled */
  39613. wolfSSL_SetLoggingCb(NULL);
  39614. wolfSSL_Debugging_ON();
  39615. /* suppress unused args */
  39616. (void)a;
  39617. (void)b;
  39618. #else
  39619. if (wolfSSL_Debugging_ON() != NOT_COMPILED_IN)
  39620. return -13603;
  39621. wolfSSL_Debugging_OFF();
  39622. if (wolfSSL_SetLoggingCb(NULL) != NOT_COMPILED_IN)
  39623. return -13604;
  39624. #endif /* DEBUG_WOLFSSL */
  39625. return 0;
  39626. }
  39627. WOLFSSL_TEST_SUBROUTINE int mutex_test(void)
  39628. {
  39629. #ifdef WOLFSSL_PTHREADS
  39630. wolfSSL_Mutex m;
  39631. #endif
  39632. #if !defined(WOLFSSL_NO_MALLOC) && !defined(WOLFSSL_USER_MUTEX)
  39633. wolfSSL_Mutex *mm = wc_InitAndAllocMutex();
  39634. if (mm == NULL)
  39635. return -13700;
  39636. wc_FreeMutex(mm);
  39637. XFREE(mm, HEAP_HINT, DYNAMIC_TYPE_MUTEX);
  39638. #endif
  39639. /* Can optionally enable advanced pthread tests using "ENABLE_PTHREAD_LOCKFREE_TESTS" */
  39640. #ifdef WOLFSSL_PTHREADS
  39641. if (wc_InitMutex(&m) != 0)
  39642. return -13701;
  39643. if (wc_LockMutex(&m) != 0)
  39644. return -13702;
  39645. #if !defined(WOLFSSL_SOLARIS) && defined(ENABLE_PTHREAD_LOCKFREE_TESTS)
  39646. /* trying to free a locked mutex is not portable behavior with pthread */
  39647. /* Attempting to destroy a locked mutex results in undefined behavior */
  39648. if (wc_FreeMutex(&m) != BAD_MUTEX_E)
  39649. return -13703;
  39650. #endif
  39651. if (wc_UnLockMutex(&m) != 0)
  39652. return -13704;
  39653. if (wc_FreeMutex(&m) != 0)
  39654. return -13705;
  39655. #if !defined(WOLFSSL_SOLARIS) && defined(ENABLE_PTHREAD_LOCKFREE_TESTS)
  39656. /* Trying to use a pthread after free'ing is not portable behavior */
  39657. if (wc_LockMutex(&m) != BAD_MUTEX_E)
  39658. return -13706;
  39659. if (wc_UnLockMutex(&m) != BAD_MUTEX_E)
  39660. return -13707;
  39661. #endif
  39662. #endif
  39663. return 0;
  39664. }
  39665. #if defined(USE_WOLFSSL_MEMORY) && !defined(FREERTOS)
  39666. #if !defined(WOLFSSL_NO_MALLOC) && !defined(WOLFSSL_LINUXKM) && \
  39667. !defined(WOLFSSL_STATIC_MEMORY)
  39668. static int malloc_cnt = 0;
  39669. static int realloc_cnt = 0;
  39670. static int free_cnt = 0;
  39671. #ifdef WOLFSSL_DEBUG_MEMORY
  39672. static void *my_Malloc_cb(size_t size, const char* func, unsigned int line)
  39673. {
  39674. (void) func;
  39675. (void) line;
  39676. #else
  39677. static void *my_Malloc_cb(size_t size)
  39678. {
  39679. #endif
  39680. malloc_cnt++;
  39681. #ifndef WOLFSSL_NO_MALLOC
  39682. return malloc(size);
  39683. #else
  39684. WOLFSSL_MSG("No malloc available");
  39685. (void)size;
  39686. return NULL;
  39687. #endif
  39688. }
  39689. #ifdef WOLFSSL_DEBUG_MEMORY
  39690. static void my_Free_cb(void *ptr, const char* func, unsigned int line)
  39691. {
  39692. (void) func;
  39693. (void) line;
  39694. #else
  39695. static void my_Free_cb(void *ptr)
  39696. {
  39697. #endif
  39698. free_cnt++;
  39699. #ifndef WOLFSSL_NO_MALLOC
  39700. free(ptr);
  39701. #else
  39702. WOLFSSL_MSG("No free available");
  39703. (void)ptr;
  39704. #endif
  39705. }
  39706. #ifdef WOLFSSL_DEBUG_MEMORY
  39707. static void *my_Realloc_cb(void *ptr, size_t size, const char* func, unsigned int line)
  39708. {
  39709. (void) func;
  39710. (void) line;
  39711. #else
  39712. static void *my_Realloc_cb(void *ptr, size_t size)
  39713. {
  39714. #endif
  39715. realloc_cnt++;
  39716. #ifndef WOLFSSL_NO_MALLOC
  39717. return realloc(ptr, size);
  39718. #else
  39719. WOLFSSL_MSG("No realloc available");
  39720. (void)ptr;
  39721. (void)size;
  39722. return NULL;
  39723. #endif
  39724. }
  39725. #endif /* !WOLFSSL_NO_MALLOC */
  39726. WOLFSSL_TEST_SUBROUTINE int memcb_test(void)
  39727. {
  39728. int ret = 0;
  39729. #if !defined(WOLFSSL_NO_MALLOC) && !defined(WOLFSSL_LINUXKM) && \
  39730. !defined(WOLFSSL_STATIC_MEMORY)
  39731. byte* b = NULL;
  39732. #endif
  39733. wolfSSL_Malloc_cb mc;
  39734. wolfSSL_Free_cb fc;
  39735. wolfSSL_Realloc_cb rc;
  39736. /* Save existing memory callbacks */
  39737. if (wolfSSL_GetAllocators(&mc, &fc, &rc) != 0)
  39738. return -13800;
  39739. #if !defined(WOLFSSL_NO_MALLOC) && !defined(WOLFSSL_LINUXKM) && \
  39740. !defined(WOLFSSL_STATIC_MEMORY)
  39741. /* test realloc */
  39742. b = (byte*)XREALLOC(b, 1024, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  39743. if (b == NULL) {
  39744. ERROR_OUT(-13801, exit_memcb);
  39745. }
  39746. XFREE(b, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  39747. b = NULL;
  39748. /* Use API. */
  39749. if (wolfSSL_SetAllocators((wolfSSL_Malloc_cb)my_Malloc_cb,
  39750. (wolfSSL_Free_cb)my_Free_cb,
  39751. (wolfSSL_Realloc_cb)my_Realloc_cb) != 0) {
  39752. ERROR_OUT(-13802, exit_memcb);
  39753. }
  39754. b = (byte*)XMALLOC(1024, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  39755. b = (byte*)XREALLOC(b, 1024, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  39756. XFREE(b, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  39757. #ifndef WOLFSSL_STATIC_MEMORY
  39758. #ifndef WOLFSSL_CHECK_MEM_ZERO
  39759. if (malloc_cnt != 1 || free_cnt != 1 || realloc_cnt != 1)
  39760. #else
  39761. /* Checking zeroized memory means realloc is a malloc and free. */
  39762. if (malloc_cnt != 2 || free_cnt != 2 || realloc_cnt != 0)
  39763. #endif
  39764. #else
  39765. if (malloc_cnt != 0 || free_cnt != 0 || realloc_cnt != 0)
  39766. #endif
  39767. ret = -13803;
  39768. #endif /* !WOLFSSL_NO_MALLOC */
  39769. #if !defined(WOLFSSL_NO_MALLOC) && !defined(WOLFSSL_LINUXKM) && \
  39770. !defined(WOLFSSL_STATIC_MEMORY)
  39771. exit_memcb:
  39772. /* reset malloc/free/realloc counts */
  39773. malloc_cnt = 0;
  39774. free_cnt = 0;
  39775. realloc_cnt = 0;
  39776. #endif
  39777. /* restore memory callbacks */
  39778. wolfSSL_SetAllocators(mc, fc, rc);
  39779. return ret;
  39780. }
  39781. #endif /* USE_WOLFSSL_MEMORY && !WOLFSSL_NO_MALLOC */
  39782. #ifdef WOLFSSL_IMX6_CAAM_BLOB
  39783. WOLFSSL_TEST_SUBROUTINE int blob_test(void)
  39784. {
  39785. int ret = 0;
  39786. byte out[112];
  39787. byte blob[112];
  39788. word32 outSz;
  39789. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  39790. {
  39791. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  39792. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  39793. };
  39794. WOLFSSL_SMALL_STACK_STATIC const byte text[] =
  39795. {
  39796. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  39797. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  39798. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  39799. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  39800. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  39801. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  39802. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  39803. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  39804. };
  39805. XMEMSET(blob, 0, sizeof(blob));
  39806. XMEMSET(out, 0, sizeof(out));
  39807. outSz = sizeof(blob);
  39808. ret = wc_caamCreateBlob((byte*)iv, sizeof(iv), blob, &outSz);
  39809. if (ret != 0) {
  39810. ERROR_OUT(-13900, exit_blob);
  39811. }
  39812. blob[outSz - 2] += 1;
  39813. ret = wc_caamOpenBlob(blob, outSz, out, &outSz);
  39814. if (ret == 0) { /* should fail with altered blob */
  39815. ERROR_OUT(-13901, exit_blob);
  39816. }
  39817. XMEMSET(blob, 0, sizeof(blob));
  39818. outSz = sizeof(blob);
  39819. ret = wc_caamCreateBlob((byte*)iv, sizeof(iv), blob, &outSz);
  39820. if (ret != 0) {
  39821. ERROR_OUT(-13902, exit_blob);
  39822. }
  39823. ret = wc_caamOpenBlob(blob, outSz, out, &outSz);
  39824. if (ret != 0) {
  39825. ERROR_OUT(-13903, exit_blob);
  39826. }
  39827. if (XMEMCMP(out, iv, sizeof(iv))) {
  39828. ERROR_OUT(-13904, exit_blob);
  39829. }
  39830. XMEMSET(blob, 0, sizeof(blob));
  39831. outSz = sizeof(blob);
  39832. ret = wc_caamCreateBlob((byte*)text, sizeof(text), blob, &outSz);
  39833. if (ret != 0) {
  39834. ERROR_OUT(-13905, exit_blob);
  39835. }
  39836. ret = wc_caamOpenBlob(blob, outSz, out, &outSz);
  39837. if (ret != 0) {
  39838. ERROR_OUT(-13906, exit_blob);
  39839. }
  39840. if (XMEMCMP(out, text, sizeof(text))) {
  39841. ERROR_OUT(-13907, exit_blob);
  39842. }
  39843. exit_blob:
  39844. return ret;
  39845. }
  39846. #endif /* WOLFSSL_IMX6_CAAM_BLOB */
  39847. #ifdef WOLF_CRYPTO_CB
  39848. /* Example custom context for crypto callback */
  39849. typedef struct {
  39850. int exampleVar; /* flag for testing if only crypt is enabled. */
  39851. } myCryptoDevCtx;
  39852. #ifdef WOLF_CRYPTO_CB_ONLY_RSA
  39853. /* Testing rsa cb when CB_ONLY_RSA is enabled
  39854. * When CB_ONLY_RSA is enabled, software imple. is not available.
  39855. *
  39856. * ctx callback ctx
  39857. * returen 0 on success, otherwise return -8000 - -8007
  39858. */
  39859. static int rsa_onlycb_test(myCryptoDevCtx *ctx)
  39860. {
  39861. int ret = 0;
  39862. #if !defined(NO_RSA)
  39863. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  39864. RsaKey *key = (RsaKey *)XMALLOC(sizeof *key,
  39865. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  39866. byte* tmp = NULL;
  39867. #else
  39868. RsaKey key[1];
  39869. byte tmp[FOURK_BUF];
  39870. #endif
  39871. size_t bytes;
  39872. const word32 inLen = (word32)TEST_STRING_SZ;
  39873. word32 idx = 0;
  39874. word32 sigSz;
  39875. WOLFSSL_SMALL_STACK_STATIC const byte in[] = TEST_STRING;
  39876. byte out[RSA_TEST_BYTES];
  39877. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  39878. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096) && \
  39879. !defined(NO_FILESYSTEM)
  39880. XFILE file;
  39881. #endif
  39882. #ifdef WOLFSSL_KEY_GEN
  39883. WC_RNG rng;
  39884. #endif
  39885. #ifdef USE_CERT_BUFFERS_1024
  39886. bytes = (size_t)sizeof_client_key_der_1024;
  39887. if (bytes < (size_t)sizeof_client_cert_der_1024)
  39888. bytes = (size_t)sizeof_client_cert_der_1024;
  39889. #elif defined(USE_CERT_BUFFERS_2048)
  39890. bytes = (size_t)sizeof_client_key_der_2048;
  39891. if (bytes < (size_t)sizeof_client_cert_der_2048)
  39892. bytes = (size_t)sizeof_client_cert_der_2048;
  39893. #elif defined(USE_CERT_BUFFERS_3072)
  39894. bytes = (size_t)sizeof_client_key_der_3072;
  39895. if (bytes < (size_t)sizeof_client_cert_der_3072)
  39896. bytes = (size_t)sizeof_client_cert_der_3072;
  39897. #elif defined(USE_CERT_BUFFERS_4096)
  39898. bytes = (size_t)sizeof_client_key_der_4096;
  39899. if (bytes < (size_t)sizeof_client_cert_der_4096)
  39900. bytes = (size_t)sizeof_client_cert_der_4096;
  39901. #else
  39902. bytes = FOURK_BUF;
  39903. #endif
  39904. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  39905. tmp = (byte*)XMALLOC(bytes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  39906. if (tmp == NULL)
  39907. ERROR_OUT(-8000, exit_onlycb);
  39908. #endif
  39909. #ifdef USE_CERT_BUFFERS_1024
  39910. XMEMCPY(tmp, client_key_der_1024, (size_t)sizeof_client_key_der_1024);
  39911. #elif defined(USE_CERT_BUFFERS_2048)
  39912. XMEMCPY(tmp, client_key_der_2048, (size_t)sizeof_client_key_der_2048);
  39913. #elif defined(USE_CERT_BUFFERS_3072)
  39914. XMEMCPY(tmp, client_key_der_3072, (size_t)sizeof_client_key_der_3072);
  39915. #elif defined(USE_CERT_BUFFERS_4096)
  39916. XMEMCPY(tmp, client_key_der_4096, (size_t)sizeof_client_key_der_4096);
  39917. #elif !defined(NO_FILESYSTEM)
  39918. file = XFOPEN(clientKey, "rb");
  39919. if (!file) {
  39920. err_sys("can't open ./certs/client-key.der, "
  39921. "Please run from wolfSSL home dir", -40);
  39922. ERROR_OUT(-8001, exit_onlycb);
  39923. }
  39924. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  39925. XFCLOSE(file);
  39926. #endif
  39927. #ifdef WOLFSSL_KEY_GEN
  39928. /* wc_CryptoCb_MakeRsaKey cb test, no actual making key
  39929. * wc_MakeRsaKey() -> rsa cb ->
  39930. * myCryptoDevCb -> wc_MakeRsaKey(CBONLY_TEST_DEVID)
  39931. * wc_MakeRsaKey(CBONLY_TEST_DEVID) expects to return 0(success)
  39932. */
  39933. ctx->exampleVar = 99;
  39934. ret = wc_MakeRsaKey(key, keySz, WC_RSA_EXPONENT, rng);
  39935. if (ret != 0) {
  39936. ERROR_OUT(-8002, exit_onlycb);
  39937. }
  39938. /* wc_MakeRsaKey() -> rsa cb ->
  39939. * myCryptoDevCb -> wc_MakeRsaKey(INVALID_DEVID)
  39940. * wc_MakeRsaKey(CBONLY_TEST_DEVID) expects to return NO_VALID_DEVID(failure)
  39941. */
  39942. ctx->exampleVar = 1;
  39943. ret = wc_MakeRsaKey(key, keySz, WC_RSA_EXPONENT, rng);
  39944. if (ret != NO_VALID_DEVID) {
  39945. ERROR_OUT(-8003, exit_onlycb);
  39946. } else
  39947. /* reset return code */
  39948. ret = 0;
  39949. #endif
  39950. ret = wc_InitRsaKey_ex(key, HEAP_HINT, devId);
  39951. if (ret != 0) {
  39952. ERROR_OUT(-8004, exit_onlycb);
  39953. }
  39954. ret = wc_RsaPrivateKeyDecode(tmp, &idx, key, (word32)bytes);
  39955. if (ret != 0) {
  39956. ERROR_OUT(-8005, exit_onlycb);
  39957. }
  39958. sigSz = (word32)wc_RsaEncryptSize(key);
  39959. /* wc_CryptoCb_Rsa cb test, no actual rsa operation */
  39960. if (ret == 0) {
  39961. /* wc_SignatureGenerate() -> rsa cb ->
  39962. * myCryptoDevCb -> wc_RsaFunction(CBONLY_TEST_DEVID)
  39963. * wc_RsaFunction(CBONLY_TEST_DEVID) expects to return 0(success)
  39964. */
  39965. ctx->exampleVar = 99;
  39966. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA,
  39967. in, inLen, out, &sigSz, key, sizeof(*key), NULL);
  39968. if (ret != 0) {
  39969. ERROR_OUT(-8006, exit_onlycb);
  39970. }
  39971. }
  39972. if (ret == 0) {
  39973. /* wc_SignatureGenerate() -> rsa cb ->
  39974. * myCryptoDevCb -> wc_RsaFunction(INVALID_DEVID)
  39975. * wc_SignatureGenerate(INVALID_DEVID) expects to
  39976. * return NO_VALID_DEVID(failure)
  39977. */
  39978. ctx->exampleVar = 1;
  39979. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA,
  39980. in, inLen, out, &sigSz, key, sizeof(*key), NULL);
  39981. if (ret != NO_VALID_DEVID) {
  39982. ERROR_OUT(-8007, exit_onlycb);
  39983. } else
  39984. /* reset return code */
  39985. ret = 0;
  39986. }
  39987. exit_onlycb:
  39988. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  39989. if (key != NULL) {
  39990. wc_FreeRsaKey(key);
  39991. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  39992. }
  39993. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  39994. #else
  39995. wc_FreeRsaKey(key);
  39996. #endif
  39997. #endif
  39998. return ret;
  39999. }
  40000. #endif
  40001. #ifdef WOLF_CRYPTO_CB_ONLY_ECC
  40002. /* Testing rsa cb when CB_ONLY_ECC is enabled
  40003. * When CB_ONLY_ECC is enabled, software imple. is not available.
  40004. *
  40005. * ctx callback ctx
  40006. * returen 0 on success, otherwise return -8008 - -8018
  40007. */
  40008. static int ecc_onlycb_test(myCryptoDevCtx *ctx)
  40009. {
  40010. int ret = 0;
  40011. #if defined(HAVE_ECC)
  40012. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  40013. ecc_key* key = (ecc_key *)XMALLOC(sizeof *key,
  40014. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40015. ecc_key* pub = (ecc_key *)XMALLOC(sizeof *pub,
  40016. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40017. byte* out = (byte*)XMALLOC(sizeof(byte),
  40018. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40019. #ifdef OPENSSL_EXTRA
  40020. byte* check = (byte*)XMALLOC(sizeof(byte)*(256), HEAP_HINT,
  40021. DYNAMIC_TYPE_TMP_BUFFER);
  40022. #endif
  40023. #else
  40024. ecc_key key[1];
  40025. ecc_key pub[1];
  40026. byte out[256];
  40027. #ifdef OPENSSL_EXTRA
  40028. unsigned char check[256];
  40029. #endif
  40030. #endif
  40031. #ifdef OPENSSL_EXTRA
  40032. EVP_PKEY* privKey = NULL;
  40033. EVP_PKEY* pubKey = NULL;
  40034. ecc_key* pkey;
  40035. EVP_MD_CTX mdCtx;
  40036. const char testData[] = "Hi There";
  40037. size_t checkSz = -1;
  40038. const unsigned char* cp;
  40039. const unsigned char* p;
  40040. const unsigned char check_v[256] = {
  40041. 0x30,0x45,0x02,0x20,0x1b,0x5c,0x2a,0xf0,0x18,0x09,
  40042. 0x74,0x65,0xa1,0x04,0x76,0x3a,0xce,0xcc,0xe5,0x34,
  40043. 0x5e,0x89,0xed,0x40,0x1e,0x5a,0xb1,0x53,0xb4,0xff,
  40044. 0xc7,0x18,0xfe,0x0f,0xc7,0xa6,0x02,0x21,0x00,0xe5,
  40045. 0x70,0x21,0xfc,0xf9,0x63,0x36,0xfd,0x16,0x18,0x08,
  40046. 0x9a,0x63,0x61,0x0f,0xe7,0x7c,0xa3,0xc9,0x14,0xa3,
  40047. 0x30,0x87,0xf7,0xf5,0x70,0x19,0xaf,0x56,0x96,0x9b,
  40048. 0xd8,0x64,0xcd,0xd9,0xff,0x7b,0x2a,0x55,0x52,0xca,
  40049. 0x41,0xb2,0xa6,0xa4,0x8a,0x3b,0x02,0x20,0x8c,0xc5,
  40050. 0xf9,0xc1,0x7d,0x2a,0x65,0x6c,0xe6,0x5a,0xe3,0x76,
  40051. 0x9b,0xab,0x0b,0x9f,0xaf,0x62,0x5d,0xb2,0x60,0xd7,
  40052. 0xeb,0xb4,0x1b,0x73,0xdc,0x01,0x7d,0x7b,0xab,0xc1,
  40053. 0x0c,0x74,0x96,0x41,0xe6,0x3f,0xc5,0x86,0xe6,0x7d,
  40054. 0x2b,0x9d,0x54,0x6b,0xcd,0x31,0x35,0x1f,0xdb,0x49,
  40055. 0x1f,0x32,0x34,0xf8,0x57,0x12,0x86,0x5c,0x0e,0x80,
  40056. 0x55,0x8d,0xff,0xd8,0xbd,0xdf,0x32,0x26,0x62,0x42,
  40057. 0x09,0xda,0xf7,0x74,0xf2,0x3f,0xe6,0xf1,0x77,0x82,
  40058. 0xce,0xe4,0xbb,0x61,0xa6,0xc0,0x17,0x0c,0x6c,0x47,
  40059. 0x2a,0x40,0x1c,0x2b,0xe0,0x98,0x3b,0xbf,0xc6,0xf8,
  40060. 0x6d,0xfd,0xd0,0xfa,0xc1,0x02,0xfb,0x5f,0xfb,0xb0,
  40061. 0xcb,0xd9,0xa3,0x59,0x94,0xe9,0x0f,0x74,0xbb,0x3f,
  40062. 0x64,0xa3,0x83,0xc4,0x2b,0xf7,0xd2,0x97,0xbf,0x3b,
  40063. 0xcf,0xbb,0x60,0x81,0x33,0x94,0xfa,0x0d,0x35,0xd2,
  40064. 0x3d,0xb9,0x99,0xe3,0x12,0xf8,0xf4,0xa3,0x74,0xf4,
  40065. 0x94,0x1d,0x7a,0x66,0xf8,0xd1,0x1d,0xcf,0xb0,0x48,
  40066. 0xef,0x8c,0x94,0x6f,0xdd,0x62,
  40067. };
  40068. #endif
  40069. WC_RNG rng;
  40070. EncryptedInfo encInfo;
  40071. int keyFormat = 0;
  40072. word32 keyIdx = 0;
  40073. byte in[] = "Everyone gets Friday off. ecc p";
  40074. word32 inLen = (word32)XSTRLEN((char*)in);
  40075. word32 outLen;
  40076. int verify;
  40077. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  40078. if (key == NULL || pub == NULL) {
  40079. ERROR_OUT(-8008, exit_onlycb);
  40080. }
  40081. #endif
  40082. ret = wc_ecc_init_ex(key, HEAP_HINT, devId);
  40083. if (ret != 0) {
  40084. ERROR_OUT(-8009, exit_onlycb);
  40085. }
  40086. /* wc_CryptoCb_MakeEccKey cb test, , no actual testing */
  40087. ctx->exampleVar = 99;
  40088. ret = wc_ecc_make_key(&rng, ECC_KEYGEN_SIZE, key);
  40089. if (ret != 0) {
  40090. ERROR_OUT(-8010, exit_onlycb);
  40091. }
  40092. ctx->exampleVar = 1;
  40093. ret = wc_ecc_make_key(&rng, ECC_KEYGEN_SIZE, key);
  40094. if (ret != NO_VALID_DEVID) {
  40095. ERROR_OUT(-8011, exit_onlycb);
  40096. } else
  40097. /* reset return code */
  40098. ret = 0;
  40099. #ifdef USE_CERT_BUFFERS_256
  40100. if (ret == 0) {
  40101. /* load ECC private key and perform private transform */
  40102. ret = wc_EccPrivateKeyDecode(ecc_key_der_256, &keyIdx,
  40103. key, sizeof_ecc_key_der_256);
  40104. }
  40105. if (ret != 0) {
  40106. ERROR_OUT(-8012, exit_onlycb);
  40107. }
  40108. /* wc_CryptoCb_EccSign cb test, no actual testing */
  40109. ctx->exampleVar = 99;
  40110. if (ret == 0) {
  40111. ret = wc_ecc_sign_hash(in, inLen, out, &outLen, &rng, key);
  40112. }
  40113. if (ret != 0) {
  40114. ERROR_OUT(-8013, exit_onlycb);
  40115. }
  40116. ctx->exampleVar = 1;
  40117. if (ret == 0) {
  40118. ret = wc_ecc_sign_hash(in, inLen, out, &outLen, &rng, key);
  40119. }
  40120. if (ret != NO_VALID_DEVID) {
  40121. ERROR_OUT(-8014, exit_onlycb);
  40122. }
  40123. else
  40124. ret = 0;
  40125. /* wc_CryptoCb_EccVerify cb test, no actual testing */
  40126. ctx->exampleVar = 99;
  40127. if (ret == 0) {
  40128. ret = wc_ecc_verify_hash(in, inLen, out, outLen, &verify, key);
  40129. }
  40130. if (ret != 0) {
  40131. ERROR_OUT(-8015, exit_onlycb);
  40132. }
  40133. ctx->exampleVar = 1;
  40134. if (ret == 0) {
  40135. ret = wc_ecc_verify_hash(in, inLen, out, outLen, &verify, key);
  40136. }
  40137. if (ret != NO_VALID_DEVID) {
  40138. ERROR_OUT(-8016, exit_onlycb);
  40139. }
  40140. else
  40141. ret = 0;
  40142. /* wc_CryptoCb_Ecdh cb test, no actual testing */
  40143. /* make public key for shared secret */
  40144. wc_ecc_init_ex(pub, HEAP_HINT, devId);
  40145. ctx->exampleVar = 99;
  40146. if (ret == 0) {
  40147. ret = wc_ecc_shared_secret(key, pub, out, &outLen);
  40148. }
  40149. if (ret != 0) {
  40150. ERROR_OUT(-8017, exit_onlycb);
  40151. }
  40152. ctx->exampleVar = 1;
  40153. if (ret == 0) {
  40154. ret = wc_ecc_shared_secret(key, pub, out, &outLen);
  40155. }
  40156. if (ret != NO_VALID_DEVID) {
  40157. ERROR_OUT(-8018, exit_onlycb);
  40158. }
  40159. else
  40160. ret = 0;
  40161. #ifdef OPENSSL_EXTRA
  40162. (void)pkey;
  40163. cp = ecc_clikey_der_256;
  40164. privKey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &cp,
  40165. sizeof_ecc_clikey_der_256);
  40166. if (privKey == NULL) {
  40167. ERROR_OUT(-8019, exit_onlycb);
  40168. }
  40169. pkey = (ecc_key*)privKey->ecc->internal;
  40170. pkey->devId = devId;
  40171. p = ecc_clikeypub_der_256;
  40172. pubKey = d2i_PUBKEY(NULL, &p, sizeof_ecc_clikeypub_der_256);
  40173. if (pubKey == NULL) {
  40174. ERROR_OUT(-8020, exit_onlycb);
  40175. }
  40176. pkey = (ecc_key*)pubKey->ecc->internal;
  40177. pkey->devId = devId;
  40178. /* sign */
  40179. EVP_MD_CTX_init(&mdCtx);
  40180. ret = EVP_DigestSignInit(&mdCtx, NULL, EVP_sha256(), NULL, privKey);
  40181. if (ret != WOLFSSL_SUCCESS) {
  40182. ERROR_OUT(-8021, exit_onlycb);
  40183. }
  40184. ret = EVP_DigestSignUpdate(&mdCtx, testData,
  40185. (unsigned int)XSTRLEN(testData));
  40186. if (ret != WOLFSSL_SUCCESS) {
  40187. ERROR_OUT(-8022, exit_onlycb);
  40188. }
  40189. ret = EVP_DigestSignFinal(&mdCtx, NULL, &checkSz);
  40190. if (ret != WOLFSSL_SUCCESS) {
  40191. ERROR_OUT(-8023, exit_onlycb);
  40192. }
  40193. ctx->exampleVar = 99;
  40194. ret = EVP_DigestSignFinal(&mdCtx, check, &checkSz);
  40195. /* just called crypt callback as dummy
  40196. * EVP_DigestSignFinal returns 0 internally.
  40197. */
  40198. if (ret != 0) {
  40199. ERROR_OUT(-8024, exit_onlycb);
  40200. }
  40201. ctx->exampleVar = 1;
  40202. ret = EVP_DigestSignFinal(&mdCtx, check, &checkSz);
  40203. /* just called crypt callback as dummy
  40204. * EVP_DigestSignFinal returns 0 internally.
  40205. */
  40206. if (ret != 0) {
  40207. ERROR_OUT(-8025, exit_onlycb);
  40208. }
  40209. /* restore checkSz for verify */
  40210. checkSz = 71;
  40211. ret = EVP_MD_CTX_cleanup(&mdCtx);
  40212. if (ret != SSL_SUCCESS) {
  40213. ERROR_OUT(-8026, exit_onlycb);
  40214. }
  40215. /* verify */
  40216. EVP_MD_CTX_init(&mdCtx);
  40217. if (ret == SSL_SUCCESS) {
  40218. ret = EVP_DigestVerifyInit(&mdCtx, NULL, EVP_sha256(), NULL, pubKey);
  40219. }
  40220. if (ret != WOLFSSL_SUCCESS) {
  40221. ERROR_OUT(-8027, exit_onlycb);
  40222. }
  40223. if (ret == WOLFSSL_SUCCESS) {
  40224. ret = EVP_DigestVerifyUpdate(&mdCtx, testData,
  40225. (unsigned int)XSTRLEN(testData));
  40226. }
  40227. if (ret != WOLFSSL_SUCCESS) {
  40228. ERROR_OUT(-8028, exit_onlycb);
  40229. }
  40230. ctx->exampleVar = 99;
  40231. ret = EVP_DigestVerifyFinal(&mdCtx, check_v, checkSz);
  40232. /* just called crypt callback as dummy
  40233. * EVP_DigestSignFinal returns 0 internally.
  40234. */
  40235. if (ret != 0) {
  40236. ERROR_OUT(-8029, exit_onlycb);
  40237. }
  40238. ctx->exampleVar = 1;
  40239. ret = EVP_DigestVerifyFinal(&mdCtx, check_v, checkSz);
  40240. /* just called crypt callback as dummy
  40241. * EVP_DigestVerifyFinal returns -1 internally rather than NO_VALID_DEVID.
  40242. */
  40243. if (ret != -1) {
  40244. ERROR_OUT(-8030, exit_onlycb);
  40245. }
  40246. ret = EVP_MD_CTX_cleanup(&mdCtx);
  40247. if (ret != SSL_SUCCESS) {
  40248. ERROR_OUT(-8031, exit_onlycb);
  40249. } else
  40250. ret = 0;
  40251. #endif
  40252. #else
  40253. (void)verify;
  40254. (void)outLen;
  40255. (void)inLen;
  40256. (void)out;
  40257. (void)pub;
  40258. #ifdef OPENSSL_EXTRA
  40259. (void)privKey;
  40260. (void)pubKey;
  40261. (void)mdCtx;
  40262. (void)check;
  40263. (void)checkSz;
  40264. (void)p;
  40265. #endif
  40266. #endif
  40267. (void)keyFormat;
  40268. (void)encInfo;
  40269. exit_onlycb:
  40270. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  40271. if (key != NULL) {
  40272. wc_ecc_free(key);
  40273. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40274. }
  40275. if (pub != NULL) {
  40276. XFREE(pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40277. }
  40278. if (out != NULL) {
  40279. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40280. }
  40281. #ifdef OPENSSL_EXTRA
  40282. if (check) {
  40283. FREE(check, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40284. }
  40285. #endif
  40286. #else
  40287. wc_ecc_free(key);
  40288. #ifdef OPENSSL_EXTRA
  40289. if (privKey)
  40290. EVP_PKEY_free(privKey);
  40291. if (pubKey)
  40292. EVP_PKEY_free(pubKey);
  40293. #endif
  40294. #endif
  40295. #endif /* HAVE_ECC */
  40296. return ret;
  40297. }
  40298. #endif
  40299. /* Example crypto dev callback function that calls software version */
  40300. static int myCryptoDevCb(int devIdArg, wc_CryptoInfo* info, void* ctx)
  40301. {
  40302. int ret = NOT_COMPILED_IN; /* return this to bypass HW and use SW */
  40303. myCryptoDevCtx* myCtx = (myCryptoDevCtx*)ctx;
  40304. if (info == NULL)
  40305. return BAD_FUNC_ARG;
  40306. #ifdef DEBUG_WOLFSSL
  40307. printf("CryptoDevCb: Algo Type %d\n", info->algo_type);
  40308. #endif
  40309. if (info->algo_type == WC_ALGO_TYPE_RNG) {
  40310. #ifndef WC_NO_RNG
  40311. /* set devId to invalid, so software is used */
  40312. info->rng.rng->devId = INVALID_DEVID;
  40313. ret = wc_RNG_GenerateBlock(info->rng.rng,
  40314. info->rng.out, info->rng.sz);
  40315. /* reset devId */
  40316. info->rng.rng->devId = devIdArg;
  40317. #endif
  40318. }
  40319. else if (info->algo_type == WC_ALGO_TYPE_SEED) {
  40320. #ifndef WC_NO_RNG
  40321. ALIGN32 static byte seed[sizeof(word32)] = { 0x00, 0x00, 0x00, 0x01 };
  40322. word32* seedWord32 = (word32*)seed;
  40323. word32 len;
  40324. /* wc_GenerateSeed is a local symbol so we need to fake the entropy. */
  40325. while (info->seed.sz > 0) {
  40326. len = (word32)sizeof(seed);
  40327. if (info->seed.sz < len)
  40328. len = info->seed.sz;
  40329. XMEMCPY(info->seed.seed, seed, sizeof(seed));
  40330. info->seed.seed += len;
  40331. info->seed.sz -= len;
  40332. (*seedWord32)++;
  40333. }
  40334. ret = 0;
  40335. #endif
  40336. }
  40337. else if (info->algo_type == WC_ALGO_TYPE_PK) {
  40338. #ifdef DEBUG_WOLFSSL
  40339. printf("CryptoDevCb: Pk Type %d\n", info->pk.type);
  40340. #endif
  40341. #ifndef NO_RSA
  40342. if (info->pk.type == WC_PK_TYPE_RSA) {
  40343. /* set devId to invalid, so software is used */
  40344. info->pk.rsa.key->devId = INVALID_DEVID;
  40345. #if defined(WOLF_CRYPTO_CB_ONLY_RSA)
  40346. #ifdef DEBUG_WOLFSSL
  40347. printf("CryptoDevCb: exampleVar %d\n", myCtx->exampleVar);
  40348. #endif
  40349. if (myCtx->exampleVar == 99) {
  40350. info->pk.rsa.key->devId = devIdArg;
  40351. return 0;
  40352. }
  40353. #endif
  40354. switch (info->pk.rsa.type) {
  40355. case RSA_PUBLIC_ENCRYPT:
  40356. case RSA_PUBLIC_DECRYPT:
  40357. /* perform software based RSA public op */
  40358. ret = wc_RsaFunction(
  40359. info->pk.rsa.in, info->pk.rsa.inLen,
  40360. info->pk.rsa.out, info->pk.rsa.outLen,
  40361. info->pk.rsa.type, info->pk.rsa.key, info->pk.rsa.rng);
  40362. break;
  40363. case RSA_PRIVATE_ENCRYPT:
  40364. case RSA_PRIVATE_DECRYPT:
  40365. /* perform software based RSA private op */
  40366. ret = wc_RsaFunction(
  40367. info->pk.rsa.in, info->pk.rsa.inLen,
  40368. info->pk.rsa.out, info->pk.rsa.outLen,
  40369. info->pk.rsa.type, info->pk.rsa.key, info->pk.rsa.rng);
  40370. break;
  40371. }
  40372. /* reset devId */
  40373. info->pk.rsa.key->devId = devIdArg;
  40374. }
  40375. #ifdef WOLFSSL_KEY_GEN
  40376. else if (info->pk.type == WC_PK_TYPE_RSA_KEYGEN) {
  40377. info->pk.rsakg.key->devId = INVALID_DEVID;
  40378. #if defined(WOLF_CRYPTO_CB_ONLY_RSA)
  40379. #ifdef DEBUG_WOLFSSL
  40380. printf("CryptoDevCb: exampleVar %d\n", myCtx->exampleVar);
  40381. #endif
  40382. if (myCtx->exampleVar == 99) {
  40383. info->pk.rsakg.key->devId = devIdArg;
  40384. return 0;
  40385. }
  40386. #endif
  40387. #ifdef HAVE_FIPS
  40388. for (;;) {
  40389. #endif
  40390. ret = wc_MakeRsaKey(info->pk.rsakg.key, info->pk.rsakg.size,
  40391. info->pk.rsakg.e, info->pk.rsakg.rng);
  40392. #ifdef HAVE_FIPS
  40393. if (ret == PRIME_GEN_E)
  40394. continue;
  40395. break;
  40396. }
  40397. #endif
  40398. /* reset devId */
  40399. info->pk.rsakg.key->devId = devIdArg;
  40400. }
  40401. #endif
  40402. #endif /* !NO_RSA */
  40403. #ifdef HAVE_ECC
  40404. if (info->pk.type == WC_PK_TYPE_EC_KEYGEN) {
  40405. /* set devId to invalid, so software is used */
  40406. info->pk.eckg.key->devId = INVALID_DEVID;
  40407. #if defined(WOLF_CRYPTO_CB_ONLY_ECC)
  40408. #ifdef DEBUG_WOLFSSL
  40409. printf("CryptoDevCb: exampleVar %d\n", myCtx->exampleVar);
  40410. #endif
  40411. if (myCtx->exampleVar == 99) {
  40412. info->pk.eckg.key->devId = devIdArg;
  40413. return 0;
  40414. }
  40415. #endif
  40416. ret = wc_ecc_make_key_ex(info->pk.eckg.rng, info->pk.eckg.size,
  40417. info->pk.eckg.key, info->pk.eckg.curveId);
  40418. /* reset devId */
  40419. info->pk.eckg.key->devId = devIdArg;
  40420. }
  40421. else if (info->pk.type == WC_PK_TYPE_ECDSA_SIGN) {
  40422. /* set devId to invalid, so software is used */
  40423. info->pk.eccsign.key->devId = INVALID_DEVID;
  40424. #if defined(WOLF_CRYPTO_CB_ONLY_ECC)
  40425. #ifdef DEBUG_WOLFSSL
  40426. printf("CryptoDevCb: exampleVar %d\n", myCtx->exampleVar);
  40427. #endif
  40428. if (myCtx->exampleVar == 99) {
  40429. info->pk.eccsign.key->devId = devIdArg;
  40430. return 0;
  40431. }
  40432. #endif
  40433. ret = wc_ecc_sign_hash(
  40434. info->pk.eccsign.in, info->pk.eccsign.inlen,
  40435. info->pk.eccsign.out, info->pk.eccsign.outlen,
  40436. info->pk.eccsign.rng, info->pk.eccsign.key);
  40437. /* reset devId */
  40438. info->pk.eccsign.key->devId = devIdArg;
  40439. }
  40440. else if (info->pk.type == WC_PK_TYPE_ECDSA_VERIFY) {
  40441. /* set devId to invalid, so software is used */
  40442. info->pk.eccverify.key->devId = INVALID_DEVID;
  40443. #if defined(WOLF_CRYPTO_CB_ONLY_ECC)
  40444. #ifdef DEBUG_WOLFSSL
  40445. printf("CryptoDevCb: exampleVar %d\n", myCtx->exampleVar);
  40446. #endif
  40447. if (myCtx->exampleVar == 99) {
  40448. info->pk.eccverify.key->devId = devIdArg;
  40449. return 0;
  40450. }
  40451. #endif
  40452. ret = wc_ecc_verify_hash(
  40453. info->pk.eccverify.sig, info->pk.eccverify.siglen,
  40454. info->pk.eccverify.hash, info->pk.eccverify.hashlen,
  40455. info->pk.eccverify.res, info->pk.eccverify.key);
  40456. /* reset devId */
  40457. info->pk.eccverify.key->devId = devIdArg;
  40458. }
  40459. else if (info->pk.type == WC_PK_TYPE_ECDH) {
  40460. /* set devId to invalid, so software is used */
  40461. info->pk.ecdh.private_key->devId = INVALID_DEVID;
  40462. #if defined(WOLF_CRYPTO_CB_ONLY_ECC)
  40463. #ifdef DEBUG_WOLFSSL
  40464. printf("CryptoDevCb: exampleVar %d\n", myCtx->exampleVar);
  40465. #endif
  40466. if (myCtx->exampleVar == 99) {
  40467. info->pk.ecdh.private_key->devId = devIdArg;
  40468. return 0;
  40469. }
  40470. #endif
  40471. ret = wc_ecc_shared_secret(
  40472. info->pk.ecdh.private_key, info->pk.ecdh.public_key,
  40473. info->pk.ecdh.out, info->pk.ecdh.outlen);
  40474. /* reset devId */
  40475. info->pk.ecdh.private_key->devId = devIdArg;
  40476. }
  40477. #endif /* HAVE_ECC */
  40478. #ifdef HAVE_CURVE25519
  40479. if (info->pk.type == WC_PK_TYPE_CURVE25519_KEYGEN) {
  40480. /* set devId to invalid, so software is used */
  40481. info->pk.curve25519kg.key->devId = INVALID_DEVID;
  40482. ret = wc_curve25519_make_key(info->pk.curve25519kg.rng,
  40483. info->pk.curve25519kg.size, info->pk.curve25519kg.key);
  40484. /* reset devId */
  40485. info->pk.curve25519kg.key->devId = devIdArg;
  40486. }
  40487. else if (info->pk.type == WC_PK_TYPE_CURVE25519) {
  40488. /* set devId to invalid, so software is used */
  40489. info->pk.curve25519.private_key->devId = INVALID_DEVID;
  40490. ret = wc_curve25519_shared_secret_ex(
  40491. info->pk.curve25519.private_key, info->pk.curve25519.public_key,
  40492. info->pk.curve25519.out, info->pk.curve25519.outlen,
  40493. info->pk.curve25519.endian);
  40494. /* reset devId */
  40495. info->pk.curve25519.private_key->devId = devIdArg;
  40496. }
  40497. #endif /* HAVE_CURVE25519 */
  40498. #ifdef HAVE_ED25519
  40499. if (info->pk.type == WC_PK_TYPE_ED25519_KEYGEN) {
  40500. /* set devId to invalid, so software is used */
  40501. info->pk.ed25519kg.key->devId = INVALID_DEVID;
  40502. ret = wc_ed25519_make_key(info->pk.ed25519kg.rng,
  40503. info->pk.ed25519kg.size, info->pk.ed25519kg.key);
  40504. /* reset devId */
  40505. info->pk.ed25519kg.key->devId = devIdArg;
  40506. }
  40507. #ifdef HAVE_ED25519_SIGN
  40508. else if (info->pk.type == WC_PK_TYPE_ED25519_SIGN) {
  40509. /* set devId to invalid, so software is used */
  40510. info->pk.ed25519sign.key->devId = INVALID_DEVID;
  40511. ret = wc_ed25519_sign_msg_ex(
  40512. info->pk.ed25519sign.in, info->pk.ed25519sign.inLen,
  40513. info->pk.ed25519sign.out, info->pk.ed25519sign.outLen,
  40514. info->pk.ed25519sign.key, info->pk.ed25519sign.type,
  40515. info->pk.ed25519sign.context, info->pk.ed25519sign.contextLen);
  40516. /* reset devId */
  40517. info->pk.ed25519sign.key->devId = devIdArg;
  40518. }
  40519. #endif
  40520. #ifdef HAVE_ED25519_VERIFY
  40521. else if (info->pk.type == WC_PK_TYPE_ED25519_VERIFY) {
  40522. /* set devId to invalid, so software is used */
  40523. info->pk.ed25519verify.key->devId = INVALID_DEVID;
  40524. ret = wc_ed25519_verify_msg_ex(
  40525. info->pk.ed25519verify.sig, info->pk.ed25519verify.sigLen,
  40526. info->pk.ed25519verify.msg, info->pk.ed25519verify.msgLen,
  40527. info->pk.ed25519verify.res, info->pk.ed25519verify.key,
  40528. info->pk.ed25519verify.type, info->pk.ed25519verify.context,
  40529. info->pk.ed25519verify.contextLen);
  40530. /* reset devId */
  40531. info->pk.ed25519verify.key->devId = devIdArg;
  40532. }
  40533. #endif
  40534. #endif /* HAVE_ED25519 */
  40535. }
  40536. else if (info->algo_type == WC_ALGO_TYPE_CIPHER) {
  40537. #if !defined(NO_AES) || !defined(NO_DES3)
  40538. #ifdef HAVE_AESGCM
  40539. if (info->cipher.type == WC_CIPHER_AES_GCM) {
  40540. if (info->cipher.enc) {
  40541. /* set devId to invalid, so software is used */
  40542. info->cipher.aesgcm_enc.aes->devId = INVALID_DEVID;
  40543. ret = wc_AesGcmEncrypt(
  40544. info->cipher.aesgcm_enc.aes,
  40545. info->cipher.aesgcm_enc.out,
  40546. info->cipher.aesgcm_enc.in,
  40547. info->cipher.aesgcm_enc.sz,
  40548. info->cipher.aesgcm_enc.iv,
  40549. info->cipher.aesgcm_enc.ivSz,
  40550. info->cipher.aesgcm_enc.authTag,
  40551. info->cipher.aesgcm_enc.authTagSz,
  40552. info->cipher.aesgcm_enc.authIn,
  40553. info->cipher.aesgcm_enc.authInSz);
  40554. /* reset devId */
  40555. info->cipher.aesgcm_enc.aes->devId = devIdArg;
  40556. }
  40557. else {
  40558. /* set devId to invalid, so software is used */
  40559. info->cipher.aesgcm_dec.aes->devId = INVALID_DEVID;
  40560. ret = wc_AesGcmDecrypt(
  40561. info->cipher.aesgcm_dec.aes,
  40562. info->cipher.aesgcm_dec.out,
  40563. info->cipher.aesgcm_dec.in,
  40564. info->cipher.aesgcm_dec.sz,
  40565. info->cipher.aesgcm_dec.iv,
  40566. info->cipher.aesgcm_dec.ivSz,
  40567. info->cipher.aesgcm_dec.authTag,
  40568. info->cipher.aesgcm_dec.authTagSz,
  40569. info->cipher.aesgcm_dec.authIn,
  40570. info->cipher.aesgcm_dec.authInSz);
  40571. /* reset devId */
  40572. info->cipher.aesgcm_dec.aes->devId = devIdArg;
  40573. }
  40574. }
  40575. #endif /* HAVE_AESGCM */
  40576. #ifdef HAVE_AES_CBC
  40577. if (info->cipher.type == WC_CIPHER_AES_CBC) {
  40578. if (info->cipher.enc) {
  40579. /* set devId to invalid, so software is used */
  40580. info->cipher.aescbc.aes->devId = INVALID_DEVID;
  40581. ret = wc_AesCbcEncrypt(
  40582. info->cipher.aescbc.aes,
  40583. info->cipher.aescbc.out,
  40584. info->cipher.aescbc.in,
  40585. info->cipher.aescbc.sz);
  40586. /* reset devId */
  40587. info->cipher.aescbc.aes->devId = devIdArg;
  40588. }
  40589. else {
  40590. /* set devId to invalid, so software is used */
  40591. info->cipher.aescbc.aes->devId = INVALID_DEVID;
  40592. ret = wc_AesCbcDecrypt(
  40593. info->cipher.aescbc.aes,
  40594. info->cipher.aescbc.out,
  40595. info->cipher.aescbc.in,
  40596. info->cipher.aescbc.sz);
  40597. /* reset devId */
  40598. info->cipher.aescbc.aes->devId = devIdArg;
  40599. }
  40600. }
  40601. #endif /* HAVE_AES_CBC */
  40602. #if defined(HAVE_AES_ECB) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  40603. if (info->cipher.type == WC_CIPHER_AES_ECB) {
  40604. if (info->cipher.enc) {
  40605. /* set devId to invalid, so software is used */
  40606. info->cipher.aesecb.aes->devId = INVALID_DEVID;
  40607. ret = wc_AesEcbEncrypt(
  40608. info->cipher.aesecb.aes,
  40609. info->cipher.aesecb.out,
  40610. info->cipher.aesecb.in,
  40611. info->cipher.aesecb.sz);
  40612. /* reset devId */
  40613. info->cipher.aesecb.aes->devId = devIdArg;
  40614. }
  40615. else {
  40616. /* set devId to invalid, so software is used */
  40617. info->cipher.aesecb.aes->devId = INVALID_DEVID;
  40618. ret = wc_AesEcbDecrypt(
  40619. info->cipher.aesecb.aes,
  40620. info->cipher.aesecb.out,
  40621. info->cipher.aesecb.in,
  40622. info->cipher.aesecb.sz);
  40623. /* reset devId */
  40624. info->cipher.aesecb.aes->devId = devIdArg;
  40625. }
  40626. }
  40627. #endif /* HAVE_AES_ECB */
  40628. #if defined(WOLFSSL_AES_COUNTER) && !defined(HAVE_FIPS) && \
  40629. !defined(HAVE_SELFTEST)
  40630. if (info->cipher.type == WC_CIPHER_AES_CTR) {
  40631. /* set devId to invalid, so software is used */
  40632. info->cipher.aesctr.aes->devId = INVALID_DEVID;
  40633. ret = wc_AesCtrEncrypt(
  40634. info->cipher.aesctr.aes,
  40635. info->cipher.aesctr.out,
  40636. info->cipher.aesctr.in,
  40637. info->cipher.aesctr.sz);
  40638. /* reset devId */
  40639. info->cipher.aesctr.aes->devId = devIdArg;
  40640. }
  40641. #endif /* WOLFSSL_AES_COUNTER */
  40642. #if defined(HAVE_AESCCM) && defined(WOLFSSL_AES_128)
  40643. if (info->cipher.type == WC_CIPHER_AES_CCM) {
  40644. if (info->cipher.enc) {
  40645. /* set devId to invalid, so software is used */
  40646. info->cipher.aesccm_enc.aes->devId = INVALID_DEVID;
  40647. ret = wc_AesCcmEncrypt(
  40648. info->cipher.aesccm_enc.aes,
  40649. info->cipher.aesccm_enc.out,
  40650. info->cipher.aesccm_enc.in,
  40651. info->cipher.aesccm_enc.sz,
  40652. info->cipher.aesccm_enc.nonce,
  40653. info->cipher.aesccm_enc.nonceSz,
  40654. info->cipher.aesccm_enc.authTag,
  40655. info->cipher.aesccm_enc.authTagSz,
  40656. info->cipher.aesccm_enc.authIn,
  40657. info->cipher.aesccm_enc.authInSz);
  40658. /* reset devId */
  40659. info->cipher.aesccm_enc.aes->devId = devIdArg;
  40660. }
  40661. else {
  40662. /* set devId to invalid, so software is used */
  40663. info->cipher.aesccm_dec.aes->devId = INVALID_DEVID;
  40664. ret = wc_AesCcmDecrypt(
  40665. info->cipher.aesccm_dec.aes,
  40666. info->cipher.aesccm_dec.out,
  40667. info->cipher.aesccm_dec.in,
  40668. info->cipher.aesccm_dec.sz,
  40669. info->cipher.aesccm_dec.nonce,
  40670. info->cipher.aesccm_dec.nonceSz,
  40671. info->cipher.aesccm_dec.authTag,
  40672. info->cipher.aesccm_dec.authTagSz,
  40673. info->cipher.aesccm_dec.authIn,
  40674. info->cipher.aesccm_dec.authInSz);
  40675. /* reset devId */
  40676. info->cipher.aesccm_dec.aes->devId = devIdArg;
  40677. }
  40678. }
  40679. #endif
  40680. #ifndef NO_DES3
  40681. if (info->cipher.type == WC_CIPHER_DES3) {
  40682. if (info->cipher.enc) {
  40683. /* set devId to invalid, so software is used */
  40684. info->cipher.des3.des->devId = INVALID_DEVID;
  40685. ret = wc_Des3_CbcEncrypt(
  40686. info->cipher.des3.des,
  40687. info->cipher.des3.out,
  40688. info->cipher.des3.in,
  40689. info->cipher.des3.sz);
  40690. /* reset devId */
  40691. info->cipher.des3.des->devId = devIdArg;
  40692. }
  40693. else {
  40694. /* set devId to invalid, so software is used */
  40695. info->cipher.des3.des->devId = INVALID_DEVID;
  40696. ret = wc_Des3_CbcDecrypt(
  40697. info->cipher.des3.des,
  40698. info->cipher.des3.out,
  40699. info->cipher.des3.in,
  40700. info->cipher.des3.sz);
  40701. /* reset devId */
  40702. info->cipher.des3.des->devId = devIdArg;
  40703. }
  40704. }
  40705. #endif /* !NO_DES3 */
  40706. #endif /* !NO_AES || !NO_DES3 */
  40707. }
  40708. #if !defined(NO_SHA) || !defined(NO_SHA256) || \
  40709. defined(WOLFSSL_SHA384) || defined(WOLFSSL_SHA512)
  40710. else if (info->algo_type == WC_ALGO_TYPE_HASH) {
  40711. #if !defined(NO_SHA)
  40712. if (info->hash.type == WC_HASH_TYPE_SHA) {
  40713. if (info->hash.sha1 == NULL)
  40714. return NOT_COMPILED_IN;
  40715. /* set devId to invalid, so software is used */
  40716. info->hash.sha1->devId = INVALID_DEVID;
  40717. if (info->hash.in != NULL) {
  40718. ret = wc_ShaUpdate(
  40719. info->hash.sha1,
  40720. info->hash.in,
  40721. info->hash.inSz);
  40722. }
  40723. if (info->hash.digest != NULL) {
  40724. ret = wc_ShaFinal(
  40725. info->hash.sha1,
  40726. info->hash.digest);
  40727. }
  40728. /* reset devId */
  40729. info->hash.sha1->devId = devIdArg;
  40730. }
  40731. else
  40732. #endif
  40733. #if !defined(NO_SHA256)
  40734. if (info->hash.type == WC_HASH_TYPE_SHA256) {
  40735. if (info->hash.sha256 == NULL)
  40736. return NOT_COMPILED_IN;
  40737. /* set devId to invalid, so software is used */
  40738. info->hash.sha256->devId = INVALID_DEVID;
  40739. if (info->hash.in != NULL) {
  40740. ret = wc_Sha256Update(
  40741. info->hash.sha256,
  40742. info->hash.in,
  40743. info->hash.inSz);
  40744. }
  40745. if (info->hash.digest != NULL) {
  40746. ret = wc_Sha256Final(
  40747. info->hash.sha256,
  40748. info->hash.digest);
  40749. }
  40750. /* reset devId */
  40751. info->hash.sha256->devId = devIdArg;
  40752. }
  40753. else
  40754. #endif
  40755. #ifdef WOLFSSL_SHA384
  40756. if (info->hash.type == WC_HASH_TYPE_SHA384) {
  40757. if (info->hash.sha384 == NULL)
  40758. return NOT_COMPILED_IN;
  40759. #ifndef NO_SHA2_CRYPTO_CB
  40760. /* set devId to invalid, so software is used */
  40761. info->hash.sha384->devId = INVALID_DEVID;
  40762. #endif
  40763. if (info->hash.in != NULL) {
  40764. ret = wc_Sha384Update(
  40765. info->hash.sha384,
  40766. info->hash.in,
  40767. info->hash.inSz);
  40768. }
  40769. if (info->hash.digest != NULL) {
  40770. ret = wc_Sha384Final(
  40771. info->hash.sha384,
  40772. info->hash.digest);
  40773. }
  40774. #ifndef NO_SHA2_CRYPTO_CB
  40775. /* reset devId */
  40776. info->hash.sha384->devId = devIdArg;
  40777. #endif
  40778. }
  40779. else
  40780. #endif
  40781. #ifdef WOLFSSL_SHA512
  40782. if (info->hash.type == WC_HASH_TYPE_SHA512) {
  40783. if (info->hash.sha512 == NULL)
  40784. return NOT_COMPILED_IN;
  40785. #ifndef NO_SHA2_CRYPTO_CB
  40786. /* set devId to invalid, so software is used */
  40787. info->hash.sha512->devId = INVALID_DEVID;
  40788. #endif
  40789. if (info->hash.in != NULL) {
  40790. ret = wc_Sha512Update(
  40791. info->hash.sha512,
  40792. info->hash.in,
  40793. info->hash.inSz);
  40794. }
  40795. if (info->hash.digest != NULL) {
  40796. ret = wc_Sha512Final(
  40797. info->hash.sha512,
  40798. info->hash.digest);
  40799. }
  40800. #ifndef NO_SHA2_CRYPTO_CB
  40801. /* reset devId */
  40802. info->hash.sha512->devId = devIdArg;
  40803. #endif
  40804. }
  40805. else
  40806. #endif
  40807. {
  40808. }
  40809. }
  40810. #endif /* !NO_SHA || !NO_SHA256 */
  40811. #ifndef NO_HMAC
  40812. else if (info->algo_type == WC_ALGO_TYPE_HMAC) {
  40813. if (info->hmac.hmac == NULL)
  40814. return NOT_COMPILED_IN;
  40815. /* set devId to invalid, so software is used */
  40816. info->hmac.hmac->devId = INVALID_DEVID;
  40817. if (info->hash.in != NULL) {
  40818. ret = wc_HmacUpdate(
  40819. info->hmac.hmac,
  40820. info->hmac.in,
  40821. info->hmac.inSz);
  40822. }
  40823. else if (info->hash.digest != NULL) {
  40824. ret = wc_HmacFinal(
  40825. info->hmac.hmac,
  40826. info->hmac.digest);
  40827. }
  40828. /* reset devId */
  40829. info->hmac.hmac->devId = devIdArg;
  40830. }
  40831. #endif
  40832. (void)devIdArg;
  40833. (void)myCtx;
  40834. return ret;
  40835. }
  40836. WOLFSSL_TEST_SUBROUTINE int cryptocb_test(void)
  40837. {
  40838. int ret = 0;
  40839. myCryptoDevCtx myCtx;
  40840. /* example data for callback */
  40841. myCtx.exampleVar = 1;
  40842. /* set devId to something other than INVALID_DEVID */
  40843. devId = 1;
  40844. ret = wc_CryptoCb_RegisterDevice(devId, myCryptoDevCb, &myCtx);
  40845. #ifndef WC_NO_RNG
  40846. if (ret == 0)
  40847. ret = random_test();
  40848. #endif /* WC_NO_RNG */
  40849. #if !defined(NO_RSA)
  40850. PRIVATE_KEY_UNLOCK();
  40851. if (ret == 0)
  40852. ret = rsa_test();
  40853. PRIVATE_KEY_LOCK();
  40854. #endif
  40855. #if defined(WOLF_CRYPTO_CB_ONLY_RSA)
  40856. PRIVATE_KEY_UNLOCK();
  40857. if (ret == 0)
  40858. ret = rsa_onlycb_test(&myCtx);
  40859. PRIVATE_KEY_LOCK();
  40860. #endif
  40861. #if defined(HAVE_ECC)
  40862. PRIVATE_KEY_UNLOCK();
  40863. if (ret == 0)
  40864. ret = ecc_test();
  40865. PRIVATE_KEY_LOCK();
  40866. #endif
  40867. #if defined(WOLF_CRYPTO_CB_ONLY_ECC)
  40868. PRIVATE_KEY_UNLOCK();
  40869. if (ret == 0)
  40870. ret = ecc_onlycb_test(&myCtx);
  40871. PRIVATE_KEY_LOCK();
  40872. #endif
  40873. #ifdef HAVE_ED25519
  40874. if (ret == 0)
  40875. ret = ed25519_test();
  40876. #endif
  40877. #ifdef HAVE_CURVE25519
  40878. if (ret == 0)
  40879. ret = curve25519_test();
  40880. #endif
  40881. #ifndef NO_AES
  40882. #ifdef HAVE_AESGCM
  40883. if (ret == 0)
  40884. ret = aesgcm_test();
  40885. #endif
  40886. #ifdef HAVE_AES_CBC
  40887. if (ret == 0)
  40888. ret = aes_test();
  40889. #endif
  40890. #if defined(HAVE_AESCCM) && defined(WOLFSSL_AES_128)
  40891. if (ret == 0)
  40892. ret = aesccm_test();
  40893. #endif
  40894. #endif /* !NO_AES */
  40895. #ifndef NO_DES3
  40896. if (ret == 0)
  40897. ret = des3_test();
  40898. #endif /* !NO_DES3 */
  40899. #ifndef NO_SHA
  40900. if (ret == 0)
  40901. ret = sha_test();
  40902. #endif
  40903. #ifndef NO_SHA256
  40904. if (ret == 0)
  40905. ret = sha256_test();
  40906. #endif
  40907. #ifdef WOLFSSL_SHA384
  40908. if (ret == 0)
  40909. ret = sha384_test();
  40910. #endif
  40911. #ifdef WOLFSSL_SHA512
  40912. if (ret == 0)
  40913. ret = sha512_test();
  40914. #endif
  40915. #ifndef NO_HMAC
  40916. #ifndef NO_SHA
  40917. if (ret == 0)
  40918. ret = hmac_sha_test();
  40919. #endif
  40920. #ifndef NO_SHA256
  40921. if (ret == 0)
  40922. ret = hmac_sha256_test();
  40923. #endif
  40924. #endif
  40925. #ifndef NO_PWDBASED
  40926. #if defined(HAVE_PBKDF2) && !defined(NO_SHA256)
  40927. if (ret == 0)
  40928. ret = pbkdf2_test();
  40929. #endif
  40930. #endif
  40931. #if defined(WOLFSSL_CMAC) && !defined(NO_AES)
  40932. if (ret == 0)
  40933. ret = cmac_test();
  40934. #endif
  40935. /* reset devId */
  40936. devId = INVALID_DEVID;
  40937. return ret;
  40938. }
  40939. #endif /* WOLF_CRYPTO_CB */
  40940. #ifdef WOLFSSL_CERT_PIV
  40941. WOLFSSL_TEST_SUBROUTINE int certpiv_test(void)
  40942. {
  40943. int ret;
  40944. wc_CertPIV piv;
  40945. /* Template for Identiv PIV cert, nonce and signature */
  40946. WOLFSSL_SMALL_STACK_STATIC const byte pivCertIdentiv[] = {
  40947. 0x0A, 0x0B,
  40948. 0x53, 0x09, /* NIST PIV Cert */
  40949. 0x70, 0x02, /* Certificate */
  40950. 0x30, 0x00,
  40951. 0x71, 0x01, 0x05, /* Cert Info */
  40952. 0xFE, 0x00, /* Error Detection */
  40953. 0x0B, 0x01, 0x00, /* Nonce */
  40954. 0x0C, 0x01, 0x00, /* Signed Nonce */
  40955. };
  40956. /* PIV certificate data including certificate, info and error dectection. */
  40957. WOLFSSL_SMALL_STACK_STATIC const byte pivCert[] = {
  40958. 0x53, 0x09, /* NIST PIV Cert */
  40959. 0x70, 0x02, /* Certificate */
  40960. 0x30, 0x00,
  40961. 0x71, 0x01, 0x04, /* Cert Info */
  40962. 0xFE, 0x00, /* Error Detection */
  40963. };
  40964. XMEMSET(&piv, 0, sizeof(piv));
  40965. /* Test with Identiv 0x0A, 0x0B and 0x0C markers */
  40966. ret = wc_ParseCertPIV(&piv, pivCertIdentiv, sizeof(pivCertIdentiv));
  40967. if (ret != 0) {
  40968. return -14000;
  40969. }
  40970. if (!piv.isIdentiv) {
  40971. return -14001;
  40972. }
  40973. if ((piv.cert == NULL) || (piv.certSz != 2)) {
  40974. return -14002;
  40975. }
  40976. if ((piv.certErrDet == NULL) || (piv.certErrDetSz != 0)) {
  40977. return -14003;
  40978. }
  40979. if ((piv.compression != ASN_PIV_CERT_INFO_GZIP)) {
  40980. return -14004;
  40981. }
  40982. if (!piv.isX509) {
  40983. return -14005;
  40984. }
  40985. if ((piv.nonce == NULL) || (piv.nonceSz != 1)) {
  40986. return -14006;
  40987. }
  40988. if ((piv.signedNonce == NULL) || (piv.signedNonceSz != 1)) {
  40989. return -14007;
  40990. }
  40991. XMEMSET(&piv, 0, sizeof(piv));
  40992. /* Test with NIST PIV format */
  40993. ret = wc_ParseCertPIV(&piv, pivCert, sizeof(pivCert));
  40994. if (ret != 0) {
  40995. return -14010;
  40996. }
  40997. if (piv.isIdentiv) {
  40998. return -14011;
  40999. }
  41000. if ((piv.cert == NULL) || (piv.certSz != 2)) {
  41001. return -14012;
  41002. }
  41003. if ((piv.certErrDet == NULL) || (piv.certErrDetSz != 0)) {
  41004. return -14013;
  41005. }
  41006. if ((piv.compression != 0)) {
  41007. return -14014;
  41008. }
  41009. if (!piv.isX509) {
  41010. return -14015;
  41011. }
  41012. return ret;
  41013. }
  41014. #endif /* WOLFSSL_CERT_PIV */
  41015. #if !defined(NO_ASN) && !defined(NO_ASN_TIME)
  41016. static time_t time_cb(time_t* t)
  41017. {
  41018. if (t != NULL) {
  41019. *t = 99;
  41020. }
  41021. return 99;
  41022. }
  41023. WOLFSSL_TEST_SUBROUTINE int time_test(void)
  41024. {
  41025. time_t t;
  41026. if (wc_SetTimeCb(time_cb) != 0)
  41027. return -15000;
  41028. t = wc_Time(NULL);
  41029. if (t != 99)
  41030. return -15001;
  41031. if (wc_GetTime(&t, sizeof(time_t)) != 0)
  41032. return -15002;
  41033. if (t != 99)
  41034. return -15003;
  41035. if (wc_SetTimeCb(NULL) != 0)
  41036. return -15004;
  41037. return 0;
  41038. }
  41039. #endif
  41040. #ifdef WOLFSSL_AES_SIV
  41041. typedef struct {
  41042. const byte key[33];
  41043. word32 keySz;
  41044. const byte nonce[49];
  41045. word32 nonceSz;
  41046. const byte assoc[81];
  41047. word32 assocSz;
  41048. const byte plaintext[83];
  41049. word32 plaintextSz;
  41050. const byte siv[AES_BLOCK_SIZE+1];
  41051. const byte ciphertext[82];
  41052. word32 ciphertextSz;
  41053. } AesSivTestVector;
  41054. #define AES_SIV_TEST_VECTORS 7
  41055. WOLFSSL_TEST_SUBROUTINE int aes_siv_test(void)
  41056. {
  41057. /* These test vectors come from chrony 4.1's SIV unit tests. */
  41058. WOLFSSL_SMALL_STACK_STATIC const AesSivTestVector testVectors[AES_SIV_TEST_VECTORS] = {
  41059. { "\x01\x23\x45\x67\x89\xab\xcd\xef\xf0\x12\x34\x56\x78\x9a\xbc\xde"
  41060. "\xef\x01\x23\x45\x67\x89\xab\xcd\xde\xf0\x12\x34\x56\x78\x9a\xbc", 32,
  41061. "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f", 16,
  41062. "", 0,
  41063. "", 0,
  41064. "\x22\x3e\xb5\x94\xe0\xe0\x25\x4b\x00\x25\x8e\x21\x9a\x1c\xa4\x21",
  41065. "", 0
  41066. },
  41067. { "\x01\x23\x45\x67\x89\xab\xcd\xef\xf0\x12\x34\x56\x78\x9a\xbc\xde"
  41068. "\xef\x01\x23\x45\x67\x89\xab\xcd\xde\xf0\x12\x34\x56\x78\x9a\xbc", 32,
  41069. "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f", 16,
  41070. "\x4c\x9d\x4f\xca\xed\x8a\xe2\xba\xad\x3f\x3e\xa6\xe9\x3c\x8c\x8b", 16,
  41071. "", 0,
  41072. "\xd7\x20\x19\x89\xc6\xdb\xc6\xd6\x61\xfc\x62\xbc\x86\x5e\xee\xef",
  41073. "", 0
  41074. },
  41075. { "\x01\x23\x45\x67\x89\xab\xcd\xef\xf0\x12\x34\x56\x78\x9a\xbc\xde"
  41076. "\xef\x01\x23\x45\x67\x89\xab\xcd\xde\xf0\x12\x34\x56\x78\x9a\xbc", 32,
  41077. "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f", 16,
  41078. "", 0,
  41079. "\x4c\x9d\x4f\xca\xed\x8a\xe2\xba\xad\x3f\x3e\xa6\xe9\x3c\x8c\x8b", 16,
  41080. "\xb6\xc1\x60\xe9\xc2\xfd\x2a\xe8\xde\xc5\x36\x8b\x2a\x33\xed\xe1",
  41081. "\x14\xff\xb3\x97\x34\x5c\xcb\xe4\x4a\xa4\xde\xac\xd9\x36\x90\x46", 16
  41082. },
  41083. { "\x01\x23\x45\x67\x89\xab\xcd\xef\xf0\x12\x34\x56\x78\x9a\xbc\xde"
  41084. "\xef\x01\x23\x45\x67\x89\xab\xcd\xde\xf0\x12\x34\x56\x78\x9a\xbc", 32,
  41085. "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e", 15,
  41086. "\x4c\x9d\x4f\xca\xed\x8a\xe2\xba\xad\x3f\x3e\xa6\xe9\x3c\x8c", 15,
  41087. "\xba\x99\x79\x31\x23\x7e\x3c\x53\x58\x7e\xd4\x93\x02\xab\xe4", 15,
  41088. "\x03\x8c\x41\x51\xba\x7a\x8f\x77\x6e\x56\x31\x99\x42\x0b\xc7\x03",
  41089. "\xe7\x6c\x67\xc9\xda\xb7\x0d\x5b\x44\x06\x26\x5a\xd0\xd2\x3b", 15
  41090. },
  41091. { "\x01\x23\x45\x67\x89\xab\xcd\xef\xf0\x12\x34\x56\x78\x9a\xbc\xde"
  41092. "\xef\x01\x23\x45\x67\x89\xab\xcd\xde\xf0\x12\x34\x56\x78\x9a\xbc", 32,
  41093. "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f", 16,
  41094. "\x4c\x9d\x4f\xca\xed\x8a\xe2\xba\xad\x3f\x3e\xa6\xe9\x3c\x8c\x8b", 16,
  41095. "\xba\x99\x79\x31\x23\x7e\x3c\x53\x58\x7e\xd4\x93\x02\xab\xe4\xa7", 16,
  41096. "\x5c\x05\x23\x65\xf4\x57\x0a\xa0\xfb\x38\x3e\xce\x9b\x75\x85\xeb",
  41097. "\x68\x85\x19\x36\x0c\x7c\x48\x11\x40\xcb\x9b\x57\x9a\x0e\x65\x32", 16
  41098. },
  41099. { "\x01\x23\x45\x67\x89\xab\xcd\xef\xf0\x12\x34\x56\x78\x9a\xbc\xde"
  41100. "\xef\x01\x23\x45\x67\x89\xab\xcd\xde\xf0\x12\x34\x56\x78\x9a\xbc", 32,
  41101. "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f"
  41102. "\xd5", 17,
  41103. "\x4c\x9d\x4f\xca\xed\x8a\xe2\xba\xad\x3f\x3e\xa6\xe9\x3c\x8c\x8b"
  41104. "\xa0", 17,
  41105. "\xba\x99\x79\x31\x23\x7e\x3c\x53\x58\x7e\xd4\x93\x02\xab\xe4\xa7"
  41106. "\x08", 17,
  41107. "\xaf\x58\x4b\xe7\x82\x1e\x96\x19\x29\x91\x25\xe0\xdd\x80\x3b\x49",
  41108. "\xa5\x11\xcd\xb6\x08\xf3\x76\xa0\xb6\xfa\x15\x82\xf3\x95\xe1\xeb"
  41109. "\xbd", 17
  41110. },
  41111. { "\x01\x23\x45\x67\x89\xab\xcd\xef\xf0\x12\x34\x56\x78\x9a\xbc\xde"
  41112. "\xef\x01\x23\x45\x67\x89\xab\xcd\xde\xf0\x12\x34\x56\x78\x9a\xbc", 32,
  41113. "\xb0\x5a\x1b\xc7\x56\xe7\xb6\x2c\xb4\x85\xe5\x56\xa5\x28\xc0\x6c"
  41114. "\x2f\x3b\x0b\x9d\x1a\x0c\xdf\x69\x47\xe0\xcc\xc0\x87\xaa\x5c\x09"
  41115. "\x98\x48\x8d\x6a\x8e\x1e\x05\xd7\x8b\x68\x74\x83\xb5\x1d\xf1\x2c", 48,
  41116. "\xe5\x8b\xd2\x6a\x30\xc5\xc5\x61\xcc\xbd\x7c\x27\xbf\xfe\xf9\x06"
  41117. "\x00\x5b\xd7\xfc\x11\x0b\xcf\x16\x61\xef\xac\x05\xa7\xaf\xec\x27"
  41118. "\x41\xc8\x5e\x9e\x0d\xf9\x2f\xaf\x20\x79\x17\xe5\x17\x91\x2a\x27"
  41119. "\x34\x1c\xbc\xaf\xeb\xef\x7f\x52\xe7\x1e\x4c\x2a\xca\xbd\x2b\xbe"
  41120. "\x34\xd6\xfb\x69\xd3\x3e\x49\x59\x60\xb4\x26\xc9\xb8\xce\xba", 79,
  41121. "\x6c\xe7\xcf\x7e\xab\x7b\xa0\xe1\xa7\x22\xcb\x88\xde\x5e\x42\xd2"
  41122. "\xec\x79\xe0\xa2\xcf\x5f\x0f\x6f\x6b\x89\x57\xcd\xae\x17\xd4\xc2"
  41123. "\xf3\x1b\xa2\xa8\x13\x78\x23\x2f\x83\xa8\xd4\x0c\xc0\xd2\xf3\x99"
  41124. "\xae\x81\xa1\xca\x5b\x5f\x45\xa6\x6f\x0c\x8a\xf3\xd4\x67\x40\x81"
  41125. "\x26\xe2\x01\x86\xe8\x5a\xd5\xf8\x58\x80\x9f\x56\xaa\x76\x96\xbf"
  41126. "\x31", 81,
  41127. "\x9a\x06\x33\xe0\xee\x00\x6a\x9b\xc8\x20\xd5\xe2\xc2\xed\xb5\x75",
  41128. "\xfa\x9e\x42\x2a\x31\x6b\xda\xca\xaa\x7d\x31\x8b\x84\x7a\xb8\xd7"
  41129. "\x8a\x81\x25\x64\xed\x41\x9b\xa9\x77\x10\xbd\x05\x0c\x4e\xc5\x31"
  41130. "\x0c\xa2\x86\xec\x8a\x94\xc8\x24\x23\x3c\x13\xee\xa5\x51\xc9\xdf"
  41131. "\x48\xc9\x55\xc5\x2f\x40\x73\x3f\x98\xbb\x8d\x69\x78\x46\x64\x17"
  41132. "\x8d\x49\x2f\x14\x62\xa4\x7c\x2a\x57\x38\x87\xce\xc6\x72\xd3\x5c"
  41133. "\xa1", 81
  41134. }};
  41135. int i;
  41136. byte computedCiphertext[82];
  41137. byte computedPlaintext[82];
  41138. byte siv[AES_BLOCK_SIZE];
  41139. int rc = 0;
  41140. for (i = 0; i < AES_SIV_TEST_VECTORS; ++i) {
  41141. rc = wc_AesSivEncrypt(testVectors[i].key, testVectors[i].keySz,
  41142. testVectors[i].assoc, testVectors[i].assocSz,
  41143. testVectors[i].nonce, testVectors[i].nonceSz,
  41144. testVectors[i].plaintext,
  41145. testVectors[i].plaintextSz, siv,
  41146. computedCiphertext);
  41147. if (rc != 0) {
  41148. return -16000;
  41149. }
  41150. rc = XMEMCMP(siv, testVectors[i].siv, AES_BLOCK_SIZE);
  41151. if (rc != 0) {
  41152. return -16001;
  41153. }
  41154. rc = XMEMCMP(computedCiphertext, testVectors[i].ciphertext,
  41155. testVectors[i].ciphertextSz);
  41156. if (rc != 0) {
  41157. return -16002;
  41158. }
  41159. rc = wc_AesSivDecrypt(testVectors[i].key, testVectors[i].keySz,
  41160. testVectors[i].assoc, testVectors[i].assocSz,
  41161. testVectors[i].nonce, testVectors[i].nonceSz,
  41162. computedCiphertext, testVectors[i].plaintextSz,
  41163. siv, computedPlaintext);
  41164. if (rc != 0) {
  41165. return -16003;
  41166. }
  41167. rc = XMEMCMP(computedPlaintext, testVectors[i].plaintext,
  41168. testVectors[i].plaintextSz);
  41169. if (rc != 0) {
  41170. return -16004;
  41171. }
  41172. }
  41173. return 0;
  41174. }
  41175. #endif
  41176. #undef ERROR_OUT
  41177. #else
  41178. #ifndef NO_MAIN_DRIVER
  41179. int main() { return 0; }
  41180. #endif
  41181. #endif /* NO_CRYPT_TEST */