test.c 1.5 MB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629196301963119632196331963419635196361963719638196391964019641196421964319644196451964619647196481964919650196511965219653196541965519656196571965819659196601966119662196631966419665196661966719668196691967019671196721967319674196751967619677196781967919680196811968219683196841968519686196871968819689196901969119692196931969419695196961969719698196991970019701197021970319704197051970619707197081970919710197111971219713197141971519716197171971819719197201972119722197231972419725197261972719728197291973019731197321973319734197351973619737197381973919740197411974219743197441974519746197471974819749197501975119752197531975419755197561975719758197591976019761197621976319764197651976619767197681976919770197711977219773197741977519776197771977819779197801978119782197831978419785197861978719788197891979019791197921979319794197951979619797197981979919800198011980219803198041980519806198071980819809198101981119812198131981419815198161981719818198191982019821198221982319824198251982619827198281982919830198311983219833198341983519836198371983819839198401984119842198431984419845198461984719848198491985019851198521985319854198551985619857198581985919860198611986219863198641986519866198671986819869198701987119872198731987419875198761987719878198791988019881198821988319884198851988619887198881988919890198911989219893198941989519896198971989819899199001990119902199031990419905199061990719908199091991019911199121991319914199151991619917199181991919920199211992219923199241992519926199271992819929199301993119932199331993419935199361993719938199391994019941199421994319944199451994619947199481994919950199511995219953199541995519956199571995819959199601996119962199631996419965199661996719968199691997019971199721997319974199751997619977199781997919980199811998219983199841998519986199871998819989199901999119992199931999419995199961999719998199992000020001200022000320004200052000620007200082000920010200112001220013200142001520016200172001820019200202002120022200232002420025200262002720028200292003020031200322003320034200352003620037200382003920040200412004220043200442004520046200472004820049200502005120052200532005420055200562005720058200592006020061200622006320064200652006620067200682006920070200712007220073200742007520076200772007820079200802008120082200832008420085200862008720088200892009020091200922009320094200952009620097200982009920100201012010220103201042010520106201072010820109201102011120112201132011420115201162011720118201192012020121201222012320124201252012620127201282012920130201312013220133201342013520136201372013820139201402014120142201432014420145201462014720148201492015020151201522015320154201552015620157201582015920160201612016220163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022520226202272022820229202302023120232202332023420235202362023720238202392024020241202422024320244202452024620247202482024920250202512025220253202542025520256202572025820259202602026120262202632026420265202662026720268202692027020271202722027320274202752027620277202782027920280202812028220283202842028520286202872028820289202902029120292202932029420295202962029720298202992030020301203022030320304203052030620307203082030920310203112031220313203142031520316203172031820319203202032120322203232032420325203262032720328203292033020331203322033320334203352033620337203382033920340203412034220343203442034520346203472034820349203502035120352203532035420355203562035720358203592036020361203622036320364203652036620367203682036920370203712037220373203742037520376203772037820379203802038120382203832038420385203862038720388203892039020391203922039320394203952039620397203982039920400204012040220403204042040520406204072040820409204102041120412204132041420415204162041720418204192042020421204222042320424204252042620427204282042920430204312043220433204342043520436204372043820439204402044120442204432044420445204462044720448204492045020451204522045320454204552045620457204582045920460204612046220463204642046520466204672046820469204702047120472204732047420475204762047720478204792048020481204822048320484204852048620487204882048920490204912049220493204942049520496204972049820499205002050120502205032050420505205062050720508205092051020511205122051320514205152051620517205182051920520205212052220523205242052520526205272052820529205302053120532205332053420535205362053720538205392054020541205422054320544205452054620547205482054920550205512055220553205542055520556205572055820559205602056120562205632056420565205662056720568205692057020571205722057320574205752057620577205782057920580205812058220583205842058520586205872058820589205902059120592205932059420595205962059720598205992060020601206022060320604206052060620607206082060920610206112061220613206142061520616206172061820619206202062120622206232062420625206262062720628206292063020631206322063320634206352063620637206382063920640206412064220643206442064520646206472064820649206502065120652206532065420655206562065720658206592066020661206622066320664206652066620667206682066920670206712067220673206742067520676206772067820679206802068120682206832068420685206862068720688206892069020691206922069320694206952069620697206982069920700207012070220703207042070520706207072070820709207102071120712207132071420715207162071720718207192072020721207222072320724207252072620727207282072920730207312073220733207342073520736207372073820739207402074120742207432074420745207462074720748207492075020751207522075320754207552075620757207582075920760207612076220763207642076520766207672076820769207702077120772207732077420775207762077720778207792078020781207822078320784207852078620787207882078920790207912079220793207942079520796207972079820799208002080120802208032080420805208062080720808208092081020811208122081320814208152081620817208182081920820208212082220823208242082520826208272082820829208302083120832208332083420835208362083720838208392084020841208422084320844208452084620847208482084920850208512085220853208542085520856208572085820859208602086120862208632086420865208662086720868208692087020871208722087320874208752087620877208782087920880208812088220883208842088520886208872088820889208902089120892208932089420895208962089720898208992090020901209022090320904209052090620907209082090920910209112091220913209142091520916209172091820919209202092120922209232092420925209262092720928209292093020931209322093320934209352093620937209382093920940209412094220943209442094520946209472094820949209502095120952209532095420955209562095720958209592096020961209622096320964209652096620967209682096920970209712097220973209742097520976209772097820979209802098120982209832098420985209862098720988209892099020991209922099320994209952099620997209982099921000210012100221003210042100521006210072100821009210102101121012210132101421015210162101721018210192102021021210222102321024210252102621027210282102921030210312103221033210342103521036210372103821039210402104121042210432104421045210462104721048210492105021051210522105321054210552105621057210582105921060210612106221063210642106521066210672106821069210702107121072210732107421075210762107721078210792108021081210822108321084210852108621087210882108921090210912109221093210942109521096210972109821099211002110121102211032110421105211062110721108211092111021111211122111321114211152111621117211182111921120211212112221123211242112521126211272112821129211302113121132211332113421135211362113721138211392114021141211422114321144211452114621147211482114921150211512115221153211542115521156211572115821159211602116121162211632116421165211662116721168211692117021171211722117321174211752117621177211782117921180211812118221183211842118521186211872118821189211902119121192211932119421195211962119721198211992120021201212022120321204212052120621207212082120921210212112121221213212142121521216212172121821219212202122121222212232122421225212262122721228212292123021231212322123321234212352123621237212382123921240212412124221243212442124521246212472124821249212502125121252212532125421255212562125721258212592126021261212622126321264212652126621267212682126921270212712127221273212742127521276212772127821279212802128121282212832128421285212862128721288212892129021291212922129321294212952129621297212982129921300213012130221303213042130521306213072130821309213102131121312213132131421315213162131721318213192132021321213222132321324213252132621327213282132921330213312133221333213342133521336213372133821339213402134121342213432134421345213462134721348213492135021351213522135321354213552135621357213582135921360213612136221363213642136521366213672136821369213702137121372213732137421375213762137721378213792138021381213822138321384213852138621387213882138921390213912139221393213942139521396213972139821399214002140121402214032140421405214062140721408214092141021411214122141321414214152141621417214182141921420214212142221423214242142521426214272142821429214302143121432214332143421435214362143721438214392144021441214422144321444214452144621447214482144921450214512145221453214542145521456214572145821459214602146121462214632146421465214662146721468214692147021471214722147321474214752147621477214782147921480214812148221483214842148521486214872148821489214902149121492214932149421495214962149721498214992150021501215022150321504215052150621507215082150921510215112151221513215142151521516215172151821519215202152121522215232152421525215262152721528215292153021531215322153321534215352153621537215382153921540215412154221543215442154521546215472154821549215502155121552215532155421555215562155721558215592156021561215622156321564215652156621567215682156921570215712157221573215742157521576215772157821579215802158121582215832158421585215862158721588215892159021591215922159321594215952159621597215982159921600216012160221603216042160521606216072160821609216102161121612216132161421615216162161721618216192162021621216222162321624216252162621627216282162921630216312163221633216342163521636216372163821639216402164121642216432164421645216462164721648216492165021651216522165321654216552165621657216582165921660216612166221663216642166521666216672166821669216702167121672216732167421675216762167721678216792168021681216822168321684216852168621687216882168921690216912169221693216942169521696216972169821699217002170121702217032170421705217062170721708217092171021711217122171321714217152171621717217182171921720217212172221723217242172521726217272172821729217302173121732217332173421735217362173721738217392174021741217422174321744217452174621747217482174921750217512175221753217542175521756217572175821759217602176121762217632176421765217662176721768217692177021771217722177321774217752177621777217782177921780217812178221783217842178521786217872178821789217902179121792217932179421795217962179721798217992180021801218022180321804218052180621807218082180921810218112181221813218142181521816218172181821819218202182121822218232182421825218262182721828218292183021831218322183321834218352183621837218382183921840218412184221843218442184521846218472184821849218502185121852218532185421855218562185721858218592186021861218622186321864218652186621867218682186921870218712187221873218742187521876218772187821879218802188121882218832188421885218862188721888218892189021891218922189321894218952189621897218982189921900219012190221903219042190521906219072190821909219102191121912219132191421915219162191721918219192192021921219222192321924219252192621927219282192921930219312193221933219342193521936219372193821939219402194121942219432194421945219462194721948219492195021951219522195321954219552195621957219582195921960219612196221963219642196521966219672196821969219702197121972219732197421975219762197721978219792198021981219822198321984219852198621987219882198921990219912199221993219942199521996219972199821999220002200122002220032200422005220062200722008220092201022011220122201322014220152201622017220182201922020220212202222023220242202522026220272202822029220302203122032220332203422035220362203722038220392204022041220422204322044220452204622047220482204922050220512205222053220542205522056220572205822059220602206122062220632206422065220662206722068220692207022071220722207322074220752207622077220782207922080220812208222083220842208522086220872208822089220902209122092220932209422095220962209722098220992210022101221022210322104221052210622107221082210922110221112211222113221142211522116221172211822119221202212122122221232212422125221262212722128221292213022131221322213322134221352213622137221382213922140221412214222143221442214522146221472214822149221502215122152221532215422155221562215722158221592216022161221622216322164221652216622167221682216922170221712217222173221742217522176221772217822179221802218122182221832218422185221862218722188221892219022191221922219322194221952219622197221982219922200222012220222203222042220522206222072220822209222102221122212222132221422215222162221722218222192222022221222222222322224222252222622227222282222922230222312223222233222342223522236222372223822239222402224122242222432224422245222462224722248222492225022251222522225322254222552225622257222582225922260222612226222263222642226522266222672226822269222702227122272222732227422275222762227722278222792228022281222822228322284222852228622287222882228922290222912229222293222942229522296222972229822299223002230122302223032230422305223062230722308223092231022311223122231322314223152231622317223182231922320223212232222323223242232522326223272232822329223302233122332223332233422335223362233722338223392234022341223422234322344223452234622347223482234922350223512235222353223542235522356223572235822359223602236122362223632236422365223662236722368223692237022371223722237322374223752237622377223782237922380223812238222383223842238522386223872238822389223902239122392223932239422395223962239722398223992240022401224022240322404224052240622407224082240922410224112241222413224142241522416224172241822419224202242122422224232242422425224262242722428224292243022431224322243322434224352243622437224382243922440224412244222443224442244522446224472244822449224502245122452224532245422455224562245722458224592246022461224622246322464224652246622467224682246922470224712247222473224742247522476224772247822479224802248122482224832248422485224862248722488224892249022491224922249322494224952249622497224982249922500225012250222503225042250522506225072250822509225102251122512225132251422515225162251722518225192252022521225222252322524225252252622527225282252922530225312253222533225342253522536225372253822539225402254122542225432254422545225462254722548225492255022551225522255322554225552255622557225582255922560225612256222563225642256522566225672256822569225702257122572225732257422575225762257722578225792258022581225822258322584225852258622587225882258922590225912259222593225942259522596225972259822599226002260122602226032260422605226062260722608226092261022611226122261322614226152261622617226182261922620226212262222623226242262522626226272262822629226302263122632226332263422635226362263722638226392264022641226422264322644226452264622647226482264922650226512265222653226542265522656226572265822659226602266122662226632266422665226662266722668226692267022671226722267322674226752267622677226782267922680226812268222683226842268522686226872268822689226902269122692226932269422695226962269722698226992270022701227022270322704227052270622707227082270922710227112271222713227142271522716227172271822719227202272122722227232272422725227262272722728227292273022731227322273322734227352273622737227382273922740227412274222743227442274522746227472274822749227502275122752227532275422755227562275722758227592276022761227622276322764227652276622767227682276922770227712277222773227742277522776227772277822779227802278122782227832278422785227862278722788227892279022791227922279322794227952279622797227982279922800228012280222803228042280522806228072280822809228102281122812228132281422815228162281722818228192282022821228222282322824228252282622827228282282922830228312283222833228342283522836228372283822839228402284122842228432284422845228462284722848228492285022851228522285322854228552285622857228582285922860228612286222863228642286522866228672286822869228702287122872228732287422875228762287722878228792288022881228822288322884228852288622887228882288922890228912289222893228942289522896228972289822899229002290122902229032290422905229062290722908229092291022911229122291322914229152291622917229182291922920229212292222923229242292522926229272292822929229302293122932229332293422935229362293722938229392294022941229422294322944229452294622947229482294922950229512295222953229542295522956229572295822959229602296122962229632296422965229662296722968229692297022971229722297322974229752297622977229782297922980229812298222983229842298522986229872298822989229902299122992229932299422995229962299722998229992300023001230022300323004230052300623007230082300923010230112301223013230142301523016230172301823019230202302123022230232302423025230262302723028230292303023031230322303323034230352303623037230382303923040230412304223043230442304523046230472304823049230502305123052230532305423055230562305723058230592306023061230622306323064230652306623067230682306923070230712307223073230742307523076230772307823079230802308123082230832308423085230862308723088230892309023091230922309323094230952309623097230982309923100231012310223103231042310523106231072310823109231102311123112231132311423115231162311723118231192312023121231222312323124231252312623127231282312923130231312313223133231342313523136231372313823139231402314123142231432314423145231462314723148231492315023151231522315323154231552315623157231582315923160231612316223163231642316523166231672316823169231702317123172231732317423175231762317723178231792318023181231822318323184231852318623187231882318923190231912319223193231942319523196231972319823199232002320123202232032320423205232062320723208232092321023211232122321323214232152321623217232182321923220232212322223223232242322523226232272322823229232302323123232232332323423235232362323723238232392324023241232422324323244232452324623247232482324923250232512325223253232542325523256232572325823259232602326123262232632326423265232662326723268232692327023271232722327323274232752327623277232782327923280232812328223283232842328523286232872328823289232902329123292232932329423295232962329723298232992330023301233022330323304233052330623307233082330923310233112331223313233142331523316233172331823319233202332123322233232332423325233262332723328233292333023331233322333323334233352333623337233382333923340233412334223343233442334523346233472334823349233502335123352233532335423355233562335723358233592336023361233622336323364233652336623367233682336923370233712337223373233742337523376233772337823379233802338123382233832338423385233862338723388233892339023391233922339323394233952339623397233982339923400234012340223403234042340523406234072340823409234102341123412234132341423415234162341723418234192342023421234222342323424234252342623427234282342923430234312343223433234342343523436234372343823439234402344123442234432344423445234462344723448234492345023451234522345323454234552345623457234582345923460234612346223463234642346523466234672346823469234702347123472234732347423475234762347723478234792348023481234822348323484234852348623487234882348923490234912349223493234942349523496234972349823499235002350123502235032350423505235062350723508235092351023511235122351323514235152351623517235182351923520235212352223523235242352523526235272352823529235302353123532235332353423535235362353723538235392354023541235422354323544235452354623547235482354923550235512355223553235542355523556235572355823559235602356123562235632356423565235662356723568235692357023571235722357323574235752357623577235782357923580235812358223583235842358523586235872358823589235902359123592235932359423595235962359723598235992360023601236022360323604236052360623607236082360923610236112361223613236142361523616236172361823619236202362123622236232362423625236262362723628236292363023631236322363323634236352363623637236382363923640236412364223643236442364523646236472364823649236502365123652236532365423655236562365723658236592366023661236622366323664236652366623667236682366923670236712367223673236742367523676236772367823679236802368123682236832368423685236862368723688236892369023691236922369323694236952369623697236982369923700237012370223703237042370523706237072370823709237102371123712237132371423715237162371723718237192372023721237222372323724237252372623727237282372923730237312373223733237342373523736237372373823739237402374123742237432374423745237462374723748237492375023751237522375323754237552375623757237582375923760237612376223763237642376523766237672376823769237702377123772237732377423775237762377723778237792378023781237822378323784237852378623787237882378923790237912379223793237942379523796237972379823799238002380123802238032380423805238062380723808238092381023811238122381323814238152381623817238182381923820238212382223823238242382523826238272382823829238302383123832238332383423835238362383723838238392384023841238422384323844238452384623847238482384923850238512385223853238542385523856238572385823859238602386123862238632386423865238662386723868238692387023871238722387323874238752387623877238782387923880238812388223883238842388523886238872388823889238902389123892238932389423895238962389723898238992390023901239022390323904239052390623907239082390923910239112391223913239142391523916239172391823919239202392123922239232392423925239262392723928239292393023931239322393323934239352393623937239382393923940239412394223943239442394523946239472394823949239502395123952239532395423955239562395723958239592396023961239622396323964239652396623967239682396923970239712397223973239742397523976239772397823979239802398123982239832398423985239862398723988239892399023991239922399323994239952399623997239982399924000240012400224003240042400524006240072400824009240102401124012240132401424015240162401724018240192402024021240222402324024240252402624027240282402924030240312403224033240342403524036240372403824039240402404124042240432404424045240462404724048240492405024051240522405324054240552405624057240582405924060240612406224063240642406524066240672406824069240702407124072240732407424075240762407724078240792408024081240822408324084240852408624087240882408924090240912409224093240942409524096240972409824099241002410124102241032410424105241062410724108241092411024111241122411324114241152411624117241182411924120241212412224123241242412524126241272412824129241302413124132241332413424135241362413724138241392414024141241422414324144241452414624147241482414924150241512415224153241542415524156241572415824159241602416124162241632416424165241662416724168241692417024171241722417324174241752417624177241782417924180241812418224183241842418524186241872418824189241902419124192241932419424195241962419724198241992420024201242022420324204242052420624207242082420924210242112421224213242142421524216242172421824219242202422124222242232422424225242262422724228242292423024231242322423324234242352423624237242382423924240242412424224243242442424524246242472424824249242502425124252242532425424255242562425724258242592426024261242622426324264242652426624267242682426924270242712427224273242742427524276242772427824279242802428124282242832428424285242862428724288242892429024291242922429324294242952429624297242982429924300243012430224303243042430524306243072430824309243102431124312243132431424315243162431724318243192432024321243222432324324243252432624327243282432924330243312433224333243342433524336243372433824339243402434124342243432434424345243462434724348243492435024351243522435324354243552435624357243582435924360243612436224363243642436524366243672436824369243702437124372243732437424375243762437724378243792438024381243822438324384243852438624387243882438924390243912439224393243942439524396243972439824399244002440124402244032440424405244062440724408244092441024411244122441324414244152441624417244182441924420244212442224423244242442524426244272442824429244302443124432244332443424435244362443724438244392444024441244422444324444244452444624447244482444924450244512445224453244542445524456244572445824459244602446124462244632446424465244662446724468244692447024471244722447324474244752447624477244782447924480244812448224483244842448524486244872448824489244902449124492244932449424495244962449724498244992450024501245022450324504245052450624507245082450924510245112451224513245142451524516245172451824519245202452124522245232452424525245262452724528245292453024531245322453324534245352453624537245382453924540245412454224543245442454524546245472454824549245502455124552245532455424555245562455724558245592456024561245622456324564245652456624567245682456924570245712457224573245742457524576245772457824579245802458124582245832458424585245862458724588245892459024591245922459324594245952459624597245982459924600246012460224603246042460524606246072460824609246102461124612246132461424615246162461724618246192462024621246222462324624246252462624627246282462924630246312463224633246342463524636246372463824639246402464124642246432464424645246462464724648246492465024651246522465324654246552465624657246582465924660246612466224663246642466524666246672466824669246702467124672246732467424675246762467724678246792468024681246822468324684246852468624687246882468924690246912469224693246942469524696246972469824699247002470124702247032470424705247062470724708247092471024711247122471324714247152471624717247182471924720247212472224723247242472524726247272472824729247302473124732247332473424735247362473724738247392474024741247422474324744247452474624747247482474924750247512475224753247542475524756247572475824759247602476124762247632476424765247662476724768247692477024771247722477324774247752477624777247782477924780247812478224783247842478524786247872478824789247902479124792247932479424795247962479724798247992480024801248022480324804248052480624807248082480924810248112481224813248142481524816248172481824819248202482124822248232482424825248262482724828248292483024831248322483324834248352483624837248382483924840248412484224843248442484524846248472484824849248502485124852248532485424855248562485724858248592486024861248622486324864248652486624867248682486924870248712487224873248742487524876248772487824879248802488124882248832488424885248862488724888248892489024891248922489324894248952489624897248982489924900249012490224903249042490524906249072490824909249102491124912249132491424915249162491724918249192492024921249222492324924249252492624927249282492924930249312493224933249342493524936249372493824939249402494124942249432494424945249462494724948249492495024951249522495324954249552495624957249582495924960249612496224963249642496524966249672496824969249702497124972249732497424975249762497724978249792498024981249822498324984249852498624987249882498924990249912499224993249942499524996249972499824999250002500125002250032500425005250062500725008250092501025011250122501325014250152501625017250182501925020250212502225023250242502525026250272502825029250302503125032250332503425035250362503725038250392504025041250422504325044250452504625047250482504925050250512505225053250542505525056250572505825059250602506125062250632506425065250662506725068250692507025071250722507325074250752507625077250782507925080250812508225083250842508525086250872508825089250902509125092250932509425095250962509725098250992510025101251022510325104251052510625107251082510925110251112511225113251142511525116251172511825119251202512125122251232512425125251262512725128251292513025131251322513325134251352513625137251382513925140251412514225143251442514525146251472514825149251502515125152251532515425155251562515725158251592516025161251622516325164251652516625167251682516925170251712517225173251742517525176251772517825179251802518125182251832518425185251862518725188251892519025191251922519325194251952519625197251982519925200252012520225203252042520525206252072520825209252102521125212252132521425215252162521725218252192522025221252222522325224252252522625227252282522925230252312523225233252342523525236252372523825239252402524125242252432524425245252462524725248252492525025251252522525325254252552525625257252582525925260252612526225263252642526525266252672526825269252702527125272252732527425275252762527725278252792528025281252822528325284252852528625287252882528925290252912529225293252942529525296252972529825299253002530125302253032530425305253062530725308253092531025311253122531325314253152531625317253182531925320253212532225323253242532525326253272532825329253302533125332253332533425335253362533725338253392534025341253422534325344253452534625347253482534925350253512535225353253542535525356253572535825359253602536125362253632536425365253662536725368253692537025371253722537325374253752537625377253782537925380253812538225383253842538525386253872538825389253902539125392253932539425395253962539725398253992540025401254022540325404254052540625407254082540925410254112541225413254142541525416254172541825419254202542125422254232542425425254262542725428254292543025431254322543325434254352543625437254382543925440254412544225443254442544525446254472544825449254502545125452254532545425455254562545725458254592546025461254622546325464254652546625467254682546925470254712547225473254742547525476254772547825479254802548125482254832548425485254862548725488254892549025491254922549325494254952549625497254982549925500255012550225503255042550525506255072550825509255102551125512255132551425515255162551725518255192552025521255222552325524255252552625527255282552925530255312553225533255342553525536255372553825539255402554125542255432554425545255462554725548255492555025551255522555325554255552555625557255582555925560255612556225563255642556525566255672556825569255702557125572255732557425575255762557725578255792558025581255822558325584255852558625587255882558925590255912559225593255942559525596255972559825599256002560125602256032560425605256062560725608256092561025611256122561325614256152561625617256182561925620256212562225623256242562525626256272562825629256302563125632256332563425635256362563725638256392564025641256422564325644256452564625647256482564925650256512565225653256542565525656256572565825659256602566125662256632566425665256662566725668256692567025671256722567325674256752567625677256782567925680256812568225683256842568525686256872568825689256902569125692256932569425695256962569725698256992570025701257022570325704257052570625707257082570925710257112571225713257142571525716257172571825719257202572125722257232572425725257262572725728257292573025731257322573325734257352573625737257382573925740257412574225743257442574525746257472574825749257502575125752257532575425755257562575725758257592576025761257622576325764257652576625767257682576925770257712577225773257742577525776257772577825779257802578125782257832578425785257862578725788257892579025791257922579325794257952579625797257982579925800258012580225803258042580525806258072580825809258102581125812258132581425815258162581725818258192582025821258222582325824258252582625827258282582925830258312583225833258342583525836258372583825839258402584125842258432584425845258462584725848258492585025851258522585325854258552585625857258582585925860258612586225863258642586525866258672586825869258702587125872258732587425875258762587725878258792588025881258822588325884258852588625887258882588925890258912589225893258942589525896258972589825899259002590125902259032590425905259062590725908259092591025911259122591325914259152591625917259182591925920259212592225923259242592525926259272592825929259302593125932259332593425935259362593725938259392594025941259422594325944259452594625947259482594925950259512595225953259542595525956259572595825959259602596125962259632596425965259662596725968259692597025971259722597325974259752597625977259782597925980259812598225983259842598525986259872598825989259902599125992259932599425995259962599725998259992600026001260022600326004260052600626007260082600926010260112601226013260142601526016260172601826019260202602126022260232602426025260262602726028260292603026031260322603326034260352603626037260382603926040260412604226043260442604526046260472604826049260502605126052260532605426055260562605726058260592606026061260622606326064260652606626067260682606926070260712607226073260742607526076260772607826079260802608126082260832608426085260862608726088260892609026091260922609326094260952609626097260982609926100261012610226103261042610526106261072610826109261102611126112261132611426115261162611726118261192612026121261222612326124261252612626127261282612926130261312613226133261342613526136261372613826139261402614126142261432614426145261462614726148261492615026151261522615326154261552615626157261582615926160261612616226163261642616526166261672616826169261702617126172261732617426175261762617726178261792618026181261822618326184261852618626187261882618926190261912619226193261942619526196261972619826199262002620126202262032620426205262062620726208262092621026211262122621326214262152621626217262182621926220262212622226223262242622526226262272622826229262302623126232262332623426235262362623726238262392624026241262422624326244262452624626247262482624926250262512625226253262542625526256262572625826259262602626126262262632626426265262662626726268262692627026271262722627326274262752627626277262782627926280262812628226283262842628526286262872628826289262902629126292262932629426295262962629726298262992630026301263022630326304263052630626307263082630926310263112631226313263142631526316263172631826319263202632126322263232632426325263262632726328263292633026331263322633326334263352633626337263382633926340263412634226343263442634526346263472634826349263502635126352263532635426355263562635726358263592636026361263622636326364263652636626367263682636926370263712637226373263742637526376263772637826379263802638126382263832638426385263862638726388263892639026391263922639326394263952639626397263982639926400264012640226403264042640526406264072640826409264102641126412264132641426415264162641726418264192642026421264222642326424264252642626427264282642926430264312643226433264342643526436264372643826439264402644126442264432644426445264462644726448264492645026451264522645326454264552645626457264582645926460264612646226463264642646526466264672646826469264702647126472264732647426475264762647726478264792648026481264822648326484264852648626487264882648926490264912649226493264942649526496264972649826499265002650126502265032650426505265062650726508265092651026511265122651326514265152651626517265182651926520265212652226523265242652526526265272652826529265302653126532265332653426535265362653726538265392654026541265422654326544265452654626547265482654926550265512655226553265542655526556265572655826559265602656126562265632656426565265662656726568265692657026571265722657326574265752657626577265782657926580265812658226583265842658526586265872658826589265902659126592265932659426595265962659726598265992660026601266022660326604266052660626607266082660926610266112661226613266142661526616266172661826619266202662126622266232662426625266262662726628266292663026631266322663326634266352663626637266382663926640266412664226643266442664526646266472664826649266502665126652266532665426655266562665726658266592666026661266622666326664266652666626667266682666926670266712667226673266742667526676266772667826679266802668126682266832668426685266862668726688266892669026691266922669326694266952669626697266982669926700267012670226703267042670526706267072670826709267102671126712267132671426715267162671726718267192672026721267222672326724267252672626727267282672926730267312673226733267342673526736267372673826739267402674126742267432674426745267462674726748267492675026751267522675326754267552675626757267582675926760267612676226763267642676526766267672676826769267702677126772267732677426775267762677726778267792678026781267822678326784267852678626787267882678926790267912679226793267942679526796267972679826799268002680126802268032680426805268062680726808268092681026811268122681326814268152681626817268182681926820268212682226823268242682526826268272682826829268302683126832268332683426835268362683726838268392684026841268422684326844268452684626847268482684926850268512685226853268542685526856268572685826859268602686126862268632686426865268662686726868268692687026871268722687326874268752687626877268782687926880268812688226883268842688526886268872688826889268902689126892268932689426895268962689726898268992690026901269022690326904269052690626907269082690926910269112691226913269142691526916269172691826919269202692126922269232692426925269262692726928269292693026931269322693326934269352693626937269382693926940269412694226943269442694526946269472694826949269502695126952269532695426955269562695726958269592696026961269622696326964269652696626967269682696926970269712697226973269742697526976269772697826979269802698126982269832698426985269862698726988269892699026991269922699326994269952699626997269982699927000270012700227003270042700527006270072700827009270102701127012270132701427015270162701727018270192702027021270222702327024270252702627027270282702927030270312703227033270342703527036270372703827039270402704127042270432704427045270462704727048270492705027051270522705327054270552705627057270582705927060270612706227063270642706527066270672706827069270702707127072270732707427075270762707727078270792708027081270822708327084270852708627087270882708927090270912709227093270942709527096270972709827099271002710127102271032710427105271062710727108271092711027111271122711327114271152711627117271182711927120271212712227123271242712527126271272712827129271302713127132271332713427135271362713727138271392714027141271422714327144271452714627147271482714927150271512715227153271542715527156271572715827159271602716127162271632716427165271662716727168271692717027171271722717327174271752717627177271782717927180271812718227183271842718527186271872718827189271902719127192271932719427195271962719727198271992720027201272022720327204272052720627207272082720927210272112721227213272142721527216272172721827219272202722127222272232722427225272262722727228272292723027231272322723327234272352723627237272382723927240272412724227243272442724527246272472724827249272502725127252272532725427255272562725727258272592726027261272622726327264272652726627267272682726927270272712727227273272742727527276272772727827279272802728127282272832728427285272862728727288272892729027291272922729327294272952729627297272982729927300273012730227303273042730527306273072730827309273102731127312273132731427315273162731727318273192732027321273222732327324273252732627327273282732927330273312733227333273342733527336273372733827339273402734127342273432734427345273462734727348273492735027351273522735327354273552735627357273582735927360273612736227363273642736527366273672736827369273702737127372273732737427375273762737727378273792738027381273822738327384273852738627387273882738927390273912739227393273942739527396273972739827399274002740127402274032740427405274062740727408274092741027411274122741327414274152741627417274182741927420274212742227423274242742527426274272742827429274302743127432274332743427435274362743727438274392744027441274422744327444274452744627447274482744927450274512745227453274542745527456274572745827459274602746127462274632746427465274662746727468274692747027471274722747327474274752747627477274782747927480274812748227483274842748527486274872748827489274902749127492274932749427495274962749727498274992750027501275022750327504275052750627507275082750927510275112751227513275142751527516275172751827519275202752127522275232752427525275262752727528275292753027531275322753327534275352753627537275382753927540275412754227543275442754527546275472754827549275502755127552275532755427555275562755727558275592756027561275622756327564275652756627567275682756927570275712757227573275742757527576275772757827579275802758127582275832758427585275862758727588275892759027591275922759327594275952759627597275982759927600276012760227603276042760527606276072760827609276102761127612276132761427615276162761727618276192762027621276222762327624276252762627627276282762927630276312763227633276342763527636276372763827639276402764127642276432764427645276462764727648276492765027651276522765327654276552765627657276582765927660276612766227663276642766527666276672766827669276702767127672276732767427675276762767727678276792768027681276822768327684276852768627687276882768927690276912769227693276942769527696276972769827699277002770127702277032770427705277062770727708277092771027711277122771327714277152771627717277182771927720277212772227723277242772527726277272772827729277302773127732277332773427735277362773727738277392774027741277422774327744277452774627747277482774927750277512775227753277542775527756277572775827759277602776127762277632776427765277662776727768277692777027771277722777327774277752777627777277782777927780277812778227783277842778527786277872778827789277902779127792277932779427795277962779727798277992780027801278022780327804278052780627807278082780927810278112781227813278142781527816278172781827819278202782127822278232782427825278262782727828278292783027831278322783327834278352783627837278382783927840278412784227843278442784527846278472784827849278502785127852278532785427855278562785727858278592786027861278622786327864278652786627867278682786927870278712787227873278742787527876278772787827879278802788127882278832788427885278862788727888278892789027891278922789327894278952789627897278982789927900279012790227903279042790527906279072790827909279102791127912279132791427915279162791727918279192792027921279222792327924279252792627927279282792927930279312793227933279342793527936279372793827939279402794127942279432794427945279462794727948279492795027951279522795327954279552795627957279582795927960279612796227963279642796527966279672796827969279702797127972279732797427975279762797727978279792798027981279822798327984279852798627987279882798927990279912799227993279942799527996279972799827999280002800128002280032800428005280062800728008280092801028011280122801328014280152801628017280182801928020280212802228023280242802528026280272802828029280302803128032280332803428035280362803728038280392804028041280422804328044280452804628047280482804928050280512805228053280542805528056280572805828059280602806128062280632806428065280662806728068280692807028071280722807328074280752807628077280782807928080280812808228083280842808528086280872808828089280902809128092280932809428095280962809728098280992810028101281022810328104281052810628107281082810928110281112811228113281142811528116281172811828119281202812128122281232812428125281262812728128281292813028131281322813328134281352813628137281382813928140281412814228143281442814528146281472814828149281502815128152281532815428155281562815728158281592816028161281622816328164281652816628167281682816928170281712817228173281742817528176281772817828179281802818128182281832818428185281862818728188281892819028191281922819328194281952819628197281982819928200282012820228203282042820528206282072820828209282102821128212282132821428215282162821728218282192822028221282222822328224282252822628227282282822928230282312823228233282342823528236282372823828239282402824128242282432824428245282462824728248282492825028251282522825328254282552825628257282582825928260282612826228263282642826528266282672826828269282702827128272282732827428275282762827728278282792828028281282822828328284282852828628287282882828928290282912829228293282942829528296282972829828299283002830128302283032830428305283062830728308283092831028311283122831328314283152831628317283182831928320283212832228323283242832528326283272832828329283302833128332283332833428335283362833728338283392834028341283422834328344283452834628347283482834928350283512835228353283542835528356283572835828359283602836128362283632836428365283662836728368283692837028371283722837328374283752837628377283782837928380283812838228383283842838528386283872838828389283902839128392283932839428395283962839728398283992840028401284022840328404284052840628407284082840928410284112841228413284142841528416284172841828419284202842128422284232842428425284262842728428284292843028431284322843328434284352843628437284382843928440284412844228443284442844528446284472844828449284502845128452284532845428455284562845728458284592846028461284622846328464284652846628467284682846928470284712847228473284742847528476284772847828479284802848128482284832848428485284862848728488284892849028491284922849328494284952849628497284982849928500285012850228503285042850528506285072850828509285102851128512285132851428515285162851728518285192852028521285222852328524285252852628527285282852928530285312853228533285342853528536285372853828539285402854128542285432854428545285462854728548285492855028551285522855328554285552855628557285582855928560285612856228563285642856528566285672856828569285702857128572285732857428575285762857728578285792858028581285822858328584285852858628587285882858928590285912859228593285942859528596285972859828599286002860128602286032860428605286062860728608286092861028611286122861328614286152861628617286182861928620286212862228623286242862528626286272862828629286302863128632286332863428635286362863728638286392864028641286422864328644286452864628647286482864928650286512865228653286542865528656286572865828659286602866128662286632866428665286662866728668286692867028671286722867328674286752867628677286782867928680286812868228683286842868528686286872868828689286902869128692286932869428695286962869728698286992870028701287022870328704287052870628707287082870928710287112871228713287142871528716287172871828719287202872128722287232872428725287262872728728287292873028731287322873328734287352873628737287382873928740287412874228743287442874528746287472874828749287502875128752287532875428755287562875728758287592876028761287622876328764287652876628767287682876928770287712877228773287742877528776287772877828779287802878128782287832878428785287862878728788287892879028791287922879328794287952879628797287982879928800288012880228803288042880528806288072880828809288102881128812288132881428815288162881728818288192882028821288222882328824288252882628827288282882928830288312883228833288342883528836288372883828839288402884128842288432884428845288462884728848288492885028851288522885328854288552885628857288582885928860288612886228863288642886528866288672886828869288702887128872288732887428875288762887728878288792888028881288822888328884288852888628887288882888928890288912889228893288942889528896288972889828899289002890128902289032890428905289062890728908289092891028911289122891328914289152891628917289182891928920289212892228923289242892528926289272892828929289302893128932289332893428935289362893728938289392894028941289422894328944289452894628947289482894928950289512895228953289542895528956289572895828959289602896128962289632896428965289662896728968289692897028971289722897328974289752897628977289782897928980289812898228983289842898528986289872898828989289902899128992289932899428995289962899728998289992900029001290022900329004290052900629007290082900929010290112901229013290142901529016290172901829019290202902129022290232902429025290262902729028290292903029031290322903329034290352903629037290382903929040290412904229043290442904529046290472904829049290502905129052290532905429055290562905729058290592906029061290622906329064290652906629067290682906929070290712907229073290742907529076290772907829079290802908129082290832908429085290862908729088290892909029091290922909329094290952909629097290982909929100291012910229103291042910529106291072910829109291102911129112291132911429115291162911729118291192912029121291222912329124291252912629127291282912929130291312913229133291342913529136291372913829139291402914129142291432914429145291462914729148291492915029151291522915329154291552915629157291582915929160291612916229163291642916529166291672916829169291702917129172291732917429175291762917729178291792918029181291822918329184291852918629187291882918929190291912919229193291942919529196291972919829199292002920129202292032920429205292062920729208292092921029211292122921329214292152921629217292182921929220292212922229223292242922529226292272922829229292302923129232292332923429235292362923729238292392924029241292422924329244292452924629247292482924929250292512925229253292542925529256292572925829259292602926129262292632926429265292662926729268292692927029271292722927329274292752927629277292782927929280292812928229283292842928529286292872928829289292902929129292292932929429295292962929729298292992930029301293022930329304293052930629307293082930929310293112931229313293142931529316293172931829319293202932129322293232932429325293262932729328293292933029331293322933329334293352933629337293382933929340293412934229343293442934529346293472934829349293502935129352293532935429355293562935729358293592936029361293622936329364293652936629367293682936929370293712937229373293742937529376293772937829379293802938129382293832938429385293862938729388293892939029391293922939329394293952939629397293982939929400294012940229403294042940529406294072940829409294102941129412294132941429415294162941729418294192942029421294222942329424294252942629427294282942929430294312943229433294342943529436294372943829439294402944129442294432944429445294462944729448294492945029451294522945329454294552945629457294582945929460294612946229463294642946529466294672946829469294702947129472294732947429475294762947729478294792948029481294822948329484294852948629487294882948929490294912949229493294942949529496294972949829499295002950129502295032950429505295062950729508295092951029511295122951329514295152951629517295182951929520295212952229523295242952529526295272952829529295302953129532295332953429535295362953729538295392954029541295422954329544295452954629547295482954929550295512955229553295542955529556295572955829559295602956129562295632956429565295662956729568295692957029571295722957329574295752957629577295782957929580295812958229583295842958529586295872958829589295902959129592295932959429595295962959729598295992960029601296022960329604296052960629607296082960929610296112961229613296142961529616296172961829619296202962129622296232962429625296262962729628296292963029631296322963329634296352963629637296382963929640296412964229643296442964529646296472964829649296502965129652296532965429655296562965729658296592966029661296622966329664296652966629667296682966929670296712967229673296742967529676296772967829679296802968129682296832968429685296862968729688296892969029691296922969329694296952969629697296982969929700297012970229703297042970529706297072970829709297102971129712297132971429715297162971729718297192972029721297222972329724297252972629727297282972929730297312973229733297342973529736297372973829739297402974129742297432974429745297462974729748297492975029751297522975329754297552975629757297582975929760297612976229763297642976529766297672976829769297702977129772297732977429775297762977729778297792978029781297822978329784297852978629787297882978929790297912979229793297942979529796297972979829799298002980129802298032980429805298062980729808298092981029811298122981329814298152981629817298182981929820298212982229823298242982529826298272982829829298302983129832298332983429835298362983729838298392984029841298422984329844298452984629847298482984929850298512985229853298542985529856298572985829859298602986129862298632986429865298662986729868298692987029871298722987329874298752987629877298782987929880298812988229883298842988529886298872988829889298902989129892298932989429895298962989729898298992990029901299022990329904299052990629907299082990929910299112991229913299142991529916299172991829919299202992129922299232992429925299262992729928299292993029931299322993329934299352993629937299382993929940299412994229943299442994529946299472994829949299502995129952299532995429955299562995729958299592996029961299622996329964299652996629967299682996929970299712997229973299742997529976299772997829979299802998129982299832998429985299862998729988299892999029991299922999329994299952999629997299982999930000300013000230003300043000530006300073000830009300103001130012300133001430015300163001730018300193002030021300223002330024300253002630027300283002930030300313003230033300343003530036300373003830039300403004130042300433004430045300463004730048300493005030051300523005330054300553005630057300583005930060300613006230063300643006530066300673006830069300703007130072300733007430075300763007730078300793008030081300823008330084300853008630087300883008930090300913009230093300943009530096300973009830099301003010130102301033010430105301063010730108301093011030111301123011330114301153011630117301183011930120301213012230123301243012530126301273012830129301303013130132301333013430135301363013730138301393014030141301423014330144301453014630147301483014930150301513015230153301543015530156301573015830159301603016130162301633016430165301663016730168301693017030171301723017330174301753017630177301783017930180301813018230183301843018530186301873018830189301903019130192301933019430195301963019730198301993020030201302023020330204302053020630207302083020930210302113021230213302143021530216302173021830219302203022130222302233022430225302263022730228302293023030231302323023330234302353023630237302383023930240302413024230243302443024530246302473024830249302503025130252302533025430255302563025730258302593026030261302623026330264302653026630267302683026930270302713027230273302743027530276302773027830279302803028130282302833028430285302863028730288302893029030291302923029330294302953029630297302983029930300303013030230303303043030530306303073030830309303103031130312303133031430315303163031730318303193032030321303223032330324303253032630327303283032930330303313033230333303343033530336303373033830339303403034130342303433034430345303463034730348303493035030351303523035330354303553035630357303583035930360303613036230363303643036530366303673036830369303703037130372303733037430375303763037730378303793038030381303823038330384303853038630387303883038930390303913039230393303943039530396303973039830399304003040130402304033040430405304063040730408304093041030411304123041330414304153041630417304183041930420304213042230423304243042530426304273042830429304303043130432304333043430435304363043730438304393044030441304423044330444304453044630447304483044930450304513045230453304543045530456304573045830459304603046130462304633046430465304663046730468304693047030471304723047330474304753047630477304783047930480304813048230483304843048530486304873048830489304903049130492304933049430495304963049730498304993050030501305023050330504305053050630507305083050930510305113051230513305143051530516305173051830519305203052130522305233052430525305263052730528305293053030531305323053330534305353053630537305383053930540305413054230543305443054530546305473054830549305503055130552305533055430555305563055730558305593056030561305623056330564305653056630567305683056930570305713057230573305743057530576305773057830579305803058130582305833058430585305863058730588305893059030591305923059330594305953059630597305983059930600306013060230603306043060530606306073060830609306103061130612306133061430615306163061730618306193062030621306223062330624306253062630627306283062930630306313063230633306343063530636306373063830639306403064130642306433064430645306463064730648306493065030651306523065330654306553065630657306583065930660306613066230663306643066530666306673066830669306703067130672306733067430675306763067730678306793068030681306823068330684306853068630687306883068930690306913069230693306943069530696306973069830699307003070130702307033070430705307063070730708307093071030711307123071330714307153071630717307183071930720307213072230723307243072530726307273072830729307303073130732307333073430735307363073730738307393074030741307423074330744307453074630747307483074930750307513075230753307543075530756307573075830759307603076130762307633076430765307663076730768307693077030771307723077330774307753077630777307783077930780307813078230783307843078530786307873078830789307903079130792307933079430795307963079730798307993080030801308023080330804308053080630807308083080930810308113081230813308143081530816308173081830819308203082130822308233082430825308263082730828308293083030831308323083330834308353083630837308383083930840308413084230843308443084530846308473084830849308503085130852308533085430855308563085730858308593086030861308623086330864308653086630867308683086930870308713087230873308743087530876308773087830879308803088130882308833088430885308863088730888308893089030891308923089330894308953089630897308983089930900309013090230903309043090530906309073090830909309103091130912309133091430915309163091730918309193092030921309223092330924309253092630927309283092930930309313093230933309343093530936309373093830939309403094130942309433094430945309463094730948309493095030951309523095330954309553095630957309583095930960309613096230963309643096530966309673096830969309703097130972309733097430975309763097730978309793098030981309823098330984309853098630987309883098930990309913099230993309943099530996309973099830999310003100131002310033100431005310063100731008310093101031011310123101331014310153101631017310183101931020310213102231023310243102531026310273102831029310303103131032310333103431035310363103731038310393104031041310423104331044310453104631047310483104931050310513105231053310543105531056310573105831059310603106131062310633106431065310663106731068310693107031071310723107331074310753107631077310783107931080310813108231083310843108531086310873108831089310903109131092310933109431095310963109731098310993110031101311023110331104311053110631107311083110931110311113111231113311143111531116311173111831119311203112131122311233112431125311263112731128311293113031131311323113331134311353113631137311383113931140311413114231143311443114531146311473114831149311503115131152311533115431155311563115731158311593116031161311623116331164311653116631167311683116931170311713117231173311743117531176311773117831179311803118131182311833118431185311863118731188311893119031191311923119331194311953119631197311983119931200312013120231203312043120531206312073120831209312103121131212312133121431215312163121731218312193122031221312223122331224312253122631227312283122931230312313123231233312343123531236312373123831239312403124131242312433124431245312463124731248312493125031251312523125331254312553125631257312583125931260312613126231263312643126531266312673126831269312703127131272312733127431275312763127731278312793128031281312823128331284312853128631287312883128931290312913129231293312943129531296312973129831299313003130131302313033130431305313063130731308313093131031311313123131331314313153131631317313183131931320313213132231323313243132531326313273132831329313303133131332313333133431335313363133731338313393134031341313423134331344313453134631347313483134931350313513135231353313543135531356313573135831359313603136131362313633136431365313663136731368313693137031371313723137331374313753137631377313783137931380313813138231383313843138531386313873138831389313903139131392313933139431395313963139731398313993140031401314023140331404314053140631407314083140931410314113141231413314143141531416314173141831419314203142131422314233142431425314263142731428314293143031431314323143331434314353143631437314383143931440314413144231443314443144531446314473144831449314503145131452314533145431455314563145731458314593146031461314623146331464314653146631467314683146931470314713147231473314743147531476314773147831479314803148131482314833148431485314863148731488314893149031491314923149331494314953149631497314983149931500315013150231503315043150531506315073150831509315103151131512315133151431515315163151731518315193152031521315223152331524315253152631527315283152931530315313153231533315343153531536315373153831539315403154131542315433154431545315463154731548315493155031551315523155331554315553155631557315583155931560315613156231563315643156531566315673156831569315703157131572315733157431575315763157731578315793158031581315823158331584315853158631587315883158931590315913159231593315943159531596315973159831599316003160131602316033160431605316063160731608316093161031611316123161331614316153161631617316183161931620316213162231623316243162531626316273162831629316303163131632316333163431635316363163731638316393164031641316423164331644316453164631647316483164931650316513165231653316543165531656316573165831659316603166131662316633166431665316663166731668316693167031671316723167331674316753167631677316783167931680316813168231683316843168531686316873168831689316903169131692316933169431695316963169731698316993170031701317023170331704317053170631707317083170931710317113171231713317143171531716317173171831719317203172131722317233172431725317263172731728317293173031731317323173331734317353173631737317383173931740317413174231743317443174531746317473174831749317503175131752317533175431755317563175731758317593176031761317623176331764317653176631767317683176931770317713177231773317743177531776317773177831779317803178131782317833178431785317863178731788317893179031791317923179331794317953179631797317983179931800318013180231803318043180531806318073180831809318103181131812318133181431815318163181731818318193182031821318223182331824318253182631827318283182931830318313183231833318343183531836318373183831839318403184131842318433184431845318463184731848318493185031851318523185331854318553185631857318583185931860318613186231863318643186531866318673186831869318703187131872318733187431875318763187731878318793188031881318823188331884318853188631887318883188931890318913189231893318943189531896318973189831899319003190131902319033190431905319063190731908319093191031911319123191331914319153191631917319183191931920319213192231923319243192531926319273192831929319303193131932319333193431935319363193731938319393194031941319423194331944319453194631947319483194931950319513195231953319543195531956319573195831959319603196131962319633196431965319663196731968319693197031971319723197331974319753197631977319783197931980319813198231983319843198531986319873198831989319903199131992319933199431995319963199731998319993200032001320023200332004320053200632007320083200932010320113201232013320143201532016320173201832019320203202132022320233202432025320263202732028320293203032031320323203332034320353203632037320383203932040320413204232043320443204532046320473204832049320503205132052320533205432055320563205732058320593206032061320623206332064320653206632067320683206932070320713207232073320743207532076320773207832079320803208132082320833208432085320863208732088320893209032091320923209332094320953209632097320983209932100321013210232103321043210532106321073210832109321103211132112321133211432115321163211732118321193212032121321223212332124321253212632127321283212932130321313213232133321343213532136321373213832139321403214132142321433214432145321463214732148321493215032151321523215332154321553215632157321583215932160321613216232163321643216532166321673216832169321703217132172321733217432175321763217732178321793218032181321823218332184321853218632187321883218932190321913219232193321943219532196321973219832199322003220132202322033220432205322063220732208322093221032211322123221332214322153221632217322183221932220322213222232223322243222532226322273222832229322303223132232322333223432235322363223732238322393224032241322423224332244322453224632247322483224932250322513225232253322543225532256322573225832259322603226132262322633226432265322663226732268322693227032271322723227332274322753227632277322783227932280322813228232283322843228532286322873228832289322903229132292322933229432295322963229732298322993230032301323023230332304323053230632307323083230932310323113231232313323143231532316323173231832319323203232132322323233232432325323263232732328323293233032331323323233332334323353233632337323383233932340323413234232343323443234532346323473234832349323503235132352323533235432355323563235732358323593236032361323623236332364323653236632367323683236932370323713237232373323743237532376323773237832379323803238132382323833238432385323863238732388323893239032391323923239332394323953239632397323983239932400324013240232403324043240532406324073240832409324103241132412324133241432415324163241732418324193242032421324223242332424324253242632427324283242932430324313243232433324343243532436324373243832439324403244132442324433244432445324463244732448324493245032451324523245332454324553245632457324583245932460324613246232463324643246532466324673246832469324703247132472324733247432475324763247732478324793248032481324823248332484324853248632487324883248932490324913249232493324943249532496324973249832499325003250132502325033250432505325063250732508325093251032511325123251332514325153251632517325183251932520325213252232523325243252532526325273252832529325303253132532325333253432535325363253732538325393254032541325423254332544325453254632547325483254932550325513255232553325543255532556325573255832559325603256132562325633256432565325663256732568325693257032571325723257332574325753257632577325783257932580325813258232583325843258532586325873258832589325903259132592325933259432595325963259732598325993260032601326023260332604326053260632607326083260932610326113261232613326143261532616326173261832619326203262132622326233262432625326263262732628326293263032631326323263332634326353263632637326383263932640326413264232643326443264532646326473264832649326503265132652326533265432655326563265732658326593266032661326623266332664326653266632667326683266932670326713267232673326743267532676326773267832679326803268132682326833268432685326863268732688326893269032691326923269332694326953269632697326983269932700327013270232703327043270532706327073270832709327103271132712327133271432715327163271732718327193272032721327223272332724327253272632727327283272932730327313273232733327343273532736327373273832739327403274132742327433274432745327463274732748327493275032751327523275332754327553275632757327583275932760327613276232763327643276532766327673276832769327703277132772327733277432775327763277732778327793278032781327823278332784327853278632787327883278932790327913279232793327943279532796327973279832799328003280132802328033280432805328063280732808328093281032811328123281332814328153281632817328183281932820328213282232823328243282532826328273282832829328303283132832328333283432835328363283732838328393284032841328423284332844328453284632847328483284932850328513285232853328543285532856328573285832859328603286132862328633286432865328663286732868328693287032871328723287332874328753287632877328783287932880328813288232883328843288532886328873288832889328903289132892328933289432895328963289732898328993290032901329023290332904329053290632907329083290932910329113291232913329143291532916329173291832919329203292132922329233292432925329263292732928329293293032931329323293332934329353293632937329383293932940329413294232943329443294532946329473294832949329503295132952329533295432955329563295732958329593296032961329623296332964329653296632967329683296932970329713297232973329743297532976329773297832979329803298132982329833298432985329863298732988329893299032991329923299332994329953299632997329983299933000330013300233003330043300533006330073300833009330103301133012330133301433015330163301733018330193302033021330223302333024330253302633027330283302933030330313303233033330343303533036330373303833039330403304133042330433304433045330463304733048330493305033051330523305333054330553305633057330583305933060330613306233063330643306533066330673306833069330703307133072330733307433075330763307733078330793308033081330823308333084330853308633087330883308933090330913309233093330943309533096330973309833099331003310133102331033310433105331063310733108331093311033111331123311333114331153311633117331183311933120331213312233123331243312533126331273312833129331303313133132331333313433135331363313733138331393314033141331423314333144331453314633147331483314933150331513315233153331543315533156331573315833159331603316133162331633316433165331663316733168331693317033171331723317333174331753317633177331783317933180331813318233183331843318533186331873318833189331903319133192331933319433195331963319733198331993320033201332023320333204332053320633207332083320933210332113321233213332143321533216332173321833219332203322133222332233322433225332263322733228332293323033231332323323333234332353323633237332383323933240332413324233243332443324533246332473324833249332503325133252332533325433255332563325733258332593326033261332623326333264332653326633267332683326933270332713327233273332743327533276332773327833279332803328133282332833328433285332863328733288332893329033291332923329333294332953329633297332983329933300333013330233303333043330533306333073330833309333103331133312333133331433315333163331733318333193332033321333223332333324333253332633327333283332933330333313333233333333343333533336333373333833339333403334133342333433334433345333463334733348333493335033351333523335333354333553335633357333583335933360333613336233363333643336533366333673336833369333703337133372333733337433375333763337733378333793338033381333823338333384333853338633387333883338933390333913339233393333943339533396333973339833399334003340133402334033340433405334063340733408334093341033411334123341333414334153341633417334183341933420334213342233423334243342533426334273342833429334303343133432334333343433435334363343733438334393344033441334423344333444334453344633447334483344933450334513345233453334543345533456334573345833459334603346133462334633346433465334663346733468334693347033471334723347333474334753347633477334783347933480334813348233483334843348533486334873348833489334903349133492334933349433495334963349733498334993350033501335023350333504335053350633507335083350933510335113351233513335143351533516335173351833519335203352133522335233352433525335263352733528335293353033531335323353333534335353353633537335383353933540335413354233543335443354533546335473354833549335503355133552335533355433555335563355733558335593356033561335623356333564335653356633567335683356933570335713357233573335743357533576335773357833579335803358133582335833358433585335863358733588335893359033591335923359333594335953359633597335983359933600336013360233603336043360533606336073360833609336103361133612336133361433615336163361733618336193362033621336223362333624336253362633627336283362933630336313363233633336343363533636336373363833639336403364133642336433364433645336463364733648336493365033651336523365333654336553365633657336583365933660336613366233663336643366533666336673366833669336703367133672336733367433675336763367733678336793368033681336823368333684336853368633687336883368933690336913369233693336943369533696336973369833699337003370133702337033370433705337063370733708337093371033711337123371333714337153371633717337183371933720337213372233723337243372533726337273372833729337303373133732337333373433735337363373733738337393374033741337423374333744337453374633747337483374933750337513375233753337543375533756337573375833759337603376133762337633376433765337663376733768337693377033771337723377333774337753377633777337783377933780337813378233783337843378533786337873378833789337903379133792337933379433795337963379733798337993380033801338023380333804338053380633807338083380933810338113381233813338143381533816338173381833819338203382133822338233382433825338263382733828338293383033831338323383333834338353383633837338383383933840338413384233843338443384533846338473384833849338503385133852338533385433855338563385733858338593386033861338623386333864338653386633867338683386933870338713387233873338743387533876338773387833879338803388133882338833388433885338863388733888338893389033891338923389333894338953389633897338983389933900339013390233903339043390533906339073390833909339103391133912339133391433915339163391733918339193392033921339223392333924339253392633927339283392933930339313393233933339343393533936339373393833939339403394133942339433394433945339463394733948339493395033951339523395333954339553395633957339583395933960339613396233963339643396533966339673396833969339703397133972339733397433975339763397733978339793398033981339823398333984339853398633987339883398933990339913399233993339943399533996339973399833999340003400134002340033400434005340063400734008340093401034011340123401334014340153401634017340183401934020340213402234023340243402534026340273402834029340303403134032340333403434035340363403734038340393404034041340423404334044340453404634047340483404934050340513405234053340543405534056340573405834059340603406134062340633406434065340663406734068340693407034071340723407334074340753407634077340783407934080340813408234083340843408534086340873408834089340903409134092340933409434095340963409734098340993410034101341023410334104341053410634107341083410934110341113411234113341143411534116341173411834119341203412134122341233412434125341263412734128341293413034131341323413334134341353413634137341383413934140341413414234143341443414534146341473414834149341503415134152341533415434155341563415734158341593416034161341623416334164341653416634167341683416934170341713417234173341743417534176341773417834179341803418134182341833418434185341863418734188341893419034191341923419334194341953419634197341983419934200342013420234203342043420534206342073420834209342103421134212342133421434215342163421734218342193422034221342223422334224342253422634227342283422934230342313423234233342343423534236342373423834239342403424134242342433424434245342463424734248342493425034251342523425334254342553425634257342583425934260342613426234263342643426534266342673426834269342703427134272342733427434275342763427734278342793428034281342823428334284342853428634287342883428934290342913429234293342943429534296342973429834299343003430134302343033430434305343063430734308343093431034311343123431334314343153431634317343183431934320343213432234323343243432534326343273432834329343303433134332343333433434335343363433734338343393434034341343423434334344343453434634347343483434934350343513435234353343543435534356343573435834359343603436134362343633436434365343663436734368343693437034371343723437334374343753437634377343783437934380343813438234383343843438534386343873438834389343903439134392343933439434395343963439734398343993440034401344023440334404344053440634407344083440934410344113441234413344143441534416344173441834419344203442134422344233442434425344263442734428344293443034431344323443334434344353443634437344383443934440344413444234443344443444534446344473444834449344503445134452344533445434455344563445734458344593446034461344623446334464344653446634467344683446934470344713447234473344743447534476344773447834479344803448134482344833448434485344863448734488344893449034491344923449334494344953449634497344983449934500345013450234503345043450534506345073450834509345103451134512345133451434515345163451734518345193452034521345223452334524345253452634527345283452934530345313453234533345343453534536345373453834539345403454134542345433454434545345463454734548345493455034551345523455334554345553455634557345583455934560345613456234563345643456534566345673456834569345703457134572345733457434575345763457734578345793458034581345823458334584345853458634587345883458934590345913459234593345943459534596345973459834599346003460134602346033460434605346063460734608346093461034611346123461334614346153461634617346183461934620346213462234623346243462534626346273462834629346303463134632346333463434635346363463734638346393464034641346423464334644346453464634647346483464934650346513465234653346543465534656346573465834659346603466134662346633466434665346663466734668346693467034671346723467334674346753467634677346783467934680346813468234683346843468534686346873468834689346903469134692346933469434695346963469734698346993470034701347023470334704347053470634707347083470934710347113471234713347143471534716347173471834719347203472134722347233472434725347263472734728347293473034731347323473334734347353473634737347383473934740347413474234743347443474534746347473474834749347503475134752347533475434755347563475734758347593476034761347623476334764347653476634767347683476934770347713477234773347743477534776347773477834779347803478134782347833478434785347863478734788347893479034791347923479334794347953479634797347983479934800348013480234803348043480534806348073480834809348103481134812348133481434815348163481734818348193482034821348223482334824348253482634827348283482934830348313483234833348343483534836348373483834839348403484134842348433484434845348463484734848348493485034851348523485334854348553485634857348583485934860348613486234863348643486534866348673486834869348703487134872348733487434875348763487734878348793488034881348823488334884348853488634887348883488934890348913489234893348943489534896348973489834899349003490134902349033490434905349063490734908349093491034911349123491334914349153491634917349183491934920349213492234923349243492534926349273492834929349303493134932349333493434935349363493734938349393494034941349423494334944349453494634947349483494934950349513495234953349543495534956349573495834959349603496134962349633496434965349663496734968349693497034971349723497334974349753497634977349783497934980349813498234983349843498534986349873498834989349903499134992349933499434995349963499734998349993500035001350023500335004350053500635007350083500935010350113501235013350143501535016350173501835019350203502135022350233502435025350263502735028350293503035031350323503335034350353503635037350383503935040350413504235043350443504535046350473504835049350503505135052350533505435055350563505735058350593506035061350623506335064350653506635067350683506935070350713507235073350743507535076350773507835079350803508135082350833508435085350863508735088350893509035091350923509335094350953509635097350983509935100351013510235103351043510535106351073510835109351103511135112351133511435115351163511735118351193512035121351223512335124351253512635127351283512935130351313513235133351343513535136351373513835139351403514135142351433514435145351463514735148351493515035151351523515335154351553515635157351583515935160351613516235163351643516535166351673516835169351703517135172351733517435175351763517735178351793518035181351823518335184351853518635187351883518935190351913519235193351943519535196351973519835199352003520135202352033520435205352063520735208352093521035211352123521335214352153521635217352183521935220352213522235223352243522535226352273522835229352303523135232352333523435235352363523735238352393524035241352423524335244352453524635247352483524935250352513525235253352543525535256352573525835259352603526135262352633526435265352663526735268352693527035271352723527335274352753527635277352783527935280352813528235283352843528535286352873528835289352903529135292352933529435295352963529735298352993530035301353023530335304353053530635307353083530935310353113531235313353143531535316353173531835319353203532135322353233532435325353263532735328353293533035331353323533335334353353533635337353383533935340353413534235343353443534535346353473534835349353503535135352353533535435355353563535735358353593536035361353623536335364353653536635367353683536935370353713537235373353743537535376353773537835379353803538135382353833538435385353863538735388353893539035391353923539335394353953539635397353983539935400354013540235403354043540535406354073540835409354103541135412354133541435415354163541735418354193542035421354223542335424354253542635427354283542935430354313543235433354343543535436354373543835439354403544135442354433544435445354463544735448354493545035451354523545335454354553545635457354583545935460354613546235463354643546535466354673546835469354703547135472354733547435475354763547735478354793548035481354823548335484354853548635487354883548935490354913549235493354943549535496354973549835499355003550135502355033550435505355063550735508355093551035511355123551335514355153551635517355183551935520355213552235523355243552535526355273552835529355303553135532355333553435535355363553735538355393554035541355423554335544355453554635547355483554935550355513555235553355543555535556355573555835559355603556135562355633556435565355663556735568355693557035571355723557335574355753557635577355783557935580355813558235583355843558535586355873558835589355903559135592355933559435595355963559735598355993560035601356023560335604356053560635607356083560935610356113561235613356143561535616356173561835619356203562135622356233562435625356263562735628356293563035631356323563335634356353563635637356383563935640356413564235643356443564535646356473564835649356503565135652356533565435655356563565735658356593566035661356623566335664356653566635667356683566935670356713567235673356743567535676356773567835679356803568135682356833568435685356863568735688356893569035691356923569335694356953569635697356983569935700357013570235703357043570535706357073570835709357103571135712357133571435715357163571735718357193572035721357223572335724357253572635727357283572935730357313573235733357343573535736357373573835739357403574135742357433574435745357463574735748357493575035751357523575335754357553575635757357583575935760357613576235763357643576535766357673576835769357703577135772357733577435775357763577735778357793578035781357823578335784357853578635787357883578935790357913579235793357943579535796357973579835799358003580135802358033580435805358063580735808358093581035811358123581335814358153581635817358183581935820358213582235823358243582535826358273582835829358303583135832358333583435835358363583735838358393584035841358423584335844358453584635847358483584935850358513585235853358543585535856358573585835859358603586135862358633586435865358663586735868358693587035871358723587335874358753587635877358783587935880358813588235883358843588535886358873588835889358903589135892358933589435895358963589735898358993590035901359023590335904359053590635907359083590935910359113591235913359143591535916359173591835919359203592135922359233592435925359263592735928359293593035931359323593335934359353593635937359383593935940359413594235943359443594535946359473594835949359503595135952359533595435955359563595735958359593596035961359623596335964359653596635967359683596935970359713597235973359743597535976359773597835979359803598135982359833598435985359863598735988359893599035991359923599335994359953599635997359983599936000360013600236003360043600536006360073600836009360103601136012360133601436015360163601736018360193602036021360223602336024360253602636027360283602936030360313603236033360343603536036360373603836039360403604136042360433604436045360463604736048360493605036051360523605336054360553605636057360583605936060360613606236063360643606536066360673606836069360703607136072360733607436075360763607736078360793608036081360823608336084360853608636087360883608936090360913609236093360943609536096360973609836099361003610136102361033610436105361063610736108361093611036111361123611336114361153611636117361183611936120361213612236123361243612536126361273612836129361303613136132361333613436135361363613736138361393614036141361423614336144361453614636147361483614936150361513615236153361543615536156361573615836159361603616136162361633616436165361663616736168361693617036171361723617336174361753617636177361783617936180361813618236183361843618536186361873618836189361903619136192361933619436195361963619736198361993620036201362023620336204362053620636207362083620936210362113621236213362143621536216362173621836219362203622136222362233622436225362263622736228362293623036231362323623336234362353623636237362383623936240362413624236243362443624536246362473624836249362503625136252362533625436255362563625736258362593626036261362623626336264362653626636267362683626936270362713627236273362743627536276362773627836279362803628136282362833628436285362863628736288362893629036291362923629336294362953629636297362983629936300363013630236303363043630536306363073630836309363103631136312363133631436315363163631736318363193632036321363223632336324363253632636327363283632936330363313633236333363343633536336363373633836339363403634136342363433634436345363463634736348363493635036351363523635336354363553635636357363583635936360363613636236363363643636536366363673636836369363703637136372363733637436375363763637736378363793638036381363823638336384363853638636387363883638936390363913639236393363943639536396363973639836399364003640136402364033640436405364063640736408364093641036411364123641336414364153641636417364183641936420364213642236423364243642536426364273642836429364303643136432364333643436435364363643736438364393644036441364423644336444364453644636447364483644936450364513645236453364543645536456364573645836459364603646136462364633646436465364663646736468364693647036471364723647336474364753647636477364783647936480364813648236483364843648536486364873648836489364903649136492364933649436495364963649736498364993650036501365023650336504365053650636507365083650936510365113651236513365143651536516365173651836519365203652136522365233652436525365263652736528365293653036531365323653336534365353653636537365383653936540365413654236543365443654536546365473654836549365503655136552365533655436555365563655736558365593656036561365623656336564365653656636567365683656936570365713657236573365743657536576365773657836579365803658136582365833658436585365863658736588365893659036591365923659336594365953659636597365983659936600366013660236603366043660536606366073660836609366103661136612366133661436615366163661736618366193662036621366223662336624366253662636627366283662936630366313663236633366343663536636366373663836639366403664136642366433664436645366463664736648366493665036651366523665336654366553665636657366583665936660366613666236663366643666536666366673666836669366703667136672366733667436675366763667736678366793668036681366823668336684366853668636687366883668936690366913669236693366943669536696366973669836699367003670136702367033670436705367063670736708367093671036711367123671336714367153671636717367183671936720367213672236723367243672536726367273672836729367303673136732367333673436735367363673736738367393674036741367423674336744367453674636747367483674936750367513675236753367543675536756367573675836759367603676136762367633676436765367663676736768367693677036771367723677336774367753677636777367783677936780367813678236783367843678536786367873678836789367903679136792367933679436795367963679736798367993680036801368023680336804368053680636807368083680936810368113681236813368143681536816368173681836819368203682136822368233682436825368263682736828368293683036831368323683336834368353683636837368383683936840368413684236843368443684536846368473684836849368503685136852368533685436855368563685736858368593686036861368623686336864368653686636867368683686936870368713687236873368743687536876368773687836879368803688136882368833688436885368863688736888368893689036891368923689336894368953689636897368983689936900369013690236903369043690536906369073690836909369103691136912369133691436915369163691736918369193692036921369223692336924369253692636927369283692936930369313693236933369343693536936369373693836939369403694136942369433694436945369463694736948369493695036951369523695336954369553695636957369583695936960369613696236963369643696536966369673696836969369703697136972369733697436975369763697736978369793698036981369823698336984369853698636987369883698936990369913699236993369943699536996369973699836999370003700137002370033700437005370063700737008370093701037011370123701337014370153701637017370183701937020370213702237023370243702537026370273702837029370303703137032370333703437035370363703737038370393704037041370423704337044370453704637047370483704937050370513705237053370543705537056370573705837059370603706137062370633706437065370663706737068370693707037071370723707337074370753707637077370783707937080370813708237083370843708537086370873708837089370903709137092370933709437095370963709737098370993710037101371023710337104371053710637107371083710937110371113711237113371143711537116371173711837119371203712137122371233712437125371263712737128371293713037131371323713337134371353713637137371383713937140371413714237143371443714537146371473714837149371503715137152371533715437155371563715737158371593716037161371623716337164371653716637167371683716937170371713717237173371743717537176371773717837179371803718137182371833718437185371863718737188371893719037191371923719337194371953719637197371983719937200372013720237203372043720537206372073720837209372103721137212372133721437215372163721737218372193722037221372223722337224372253722637227372283722937230372313723237233372343723537236372373723837239372403724137242372433724437245372463724737248372493725037251372523725337254372553725637257372583725937260372613726237263372643726537266372673726837269372703727137272372733727437275372763727737278372793728037281372823728337284372853728637287372883728937290372913729237293372943729537296372973729837299373003730137302373033730437305373063730737308373093731037311373123731337314373153731637317373183731937320373213732237323373243732537326373273732837329373303733137332373333733437335373363733737338373393734037341373423734337344373453734637347373483734937350373513735237353373543735537356373573735837359373603736137362373633736437365373663736737368373693737037371373723737337374373753737637377373783737937380373813738237383373843738537386373873738837389373903739137392373933739437395373963739737398373993740037401374023740337404374053740637407374083740937410374113741237413374143741537416374173741837419374203742137422374233742437425374263742737428374293743037431374323743337434374353743637437374383743937440374413744237443374443744537446374473744837449374503745137452374533745437455374563745737458374593746037461374623746337464374653746637467374683746937470374713747237473374743747537476374773747837479374803748137482374833748437485374863748737488374893749037491374923749337494374953749637497374983749937500375013750237503375043750537506375073750837509375103751137512375133751437515375163751737518375193752037521375223752337524375253752637527375283752937530375313753237533375343753537536375373753837539375403754137542375433754437545375463754737548375493755037551375523755337554375553755637557375583755937560375613756237563375643756537566375673756837569375703757137572375733757437575375763757737578375793758037581375823758337584375853758637587375883758937590375913759237593375943759537596375973759837599376003760137602376033760437605376063760737608376093761037611376123761337614376153761637617376183761937620376213762237623376243762537626376273762837629376303763137632376333763437635376363763737638376393764037641376423764337644376453764637647376483764937650376513765237653376543765537656376573765837659376603766137662376633766437665376663766737668376693767037671376723767337674376753767637677376783767937680376813768237683376843768537686376873768837689376903769137692376933769437695376963769737698376993770037701377023770337704377053770637707377083770937710377113771237713377143771537716377173771837719377203772137722377233772437725377263772737728377293773037731377323773337734377353773637737377383773937740377413774237743377443774537746377473774837749377503775137752377533775437755377563775737758377593776037761377623776337764377653776637767377683776937770377713777237773377743777537776377773777837779377803778137782377833778437785377863778737788377893779037791377923779337794377953779637797377983779937800378013780237803378043780537806378073780837809378103781137812378133781437815378163781737818378193782037821378223782337824378253782637827378283782937830378313783237833378343783537836378373783837839378403784137842378433784437845378463784737848378493785037851378523785337854378553785637857378583785937860378613786237863378643786537866378673786837869378703787137872378733787437875378763787737878378793788037881378823788337884378853788637887378883788937890378913789237893378943789537896378973789837899379003790137902379033790437905379063790737908379093791037911379123791337914379153791637917379183791937920379213792237923379243792537926379273792837929379303793137932379333793437935379363793737938379393794037941379423794337944379453794637947379483794937950379513795237953379543795537956379573795837959379603796137962379633796437965379663796737968379693797037971379723797337974379753797637977379783797937980379813798237983379843798537986379873798837989379903799137992379933799437995379963799737998379993800038001380023800338004380053800638007380083800938010380113801238013380143801538016380173801838019380203802138022380233802438025380263802738028380293803038031380323803338034380353803638037380383803938040380413804238043380443804538046380473804838049380503805138052380533805438055380563805738058380593806038061380623806338064380653806638067380683806938070380713807238073380743807538076380773807838079380803808138082380833808438085380863808738088380893809038091380923809338094380953809638097380983809938100381013810238103381043810538106381073810838109381103811138112381133811438115381163811738118381193812038121381223812338124381253812638127381283812938130381313813238133381343813538136381373813838139381403814138142381433814438145381463814738148381493815038151381523815338154381553815638157381583815938160381613816238163381643816538166381673816838169381703817138172381733817438175381763817738178381793818038181381823818338184381853818638187381883818938190381913819238193381943819538196381973819838199382003820138202382033820438205382063820738208382093821038211382123821338214382153821638217382183821938220382213822238223382243822538226382273822838229382303823138232382333823438235382363823738238382393824038241382423824338244382453824638247382483824938250382513825238253382543825538256382573825838259382603826138262382633826438265382663826738268382693827038271382723827338274382753827638277382783827938280382813828238283382843828538286382873828838289382903829138292382933829438295382963829738298382993830038301383023830338304383053830638307383083830938310383113831238313383143831538316383173831838319383203832138322383233832438325383263832738328383293833038331383323833338334383353833638337383383833938340383413834238343383443834538346383473834838349383503835138352383533835438355383563835738358383593836038361383623836338364383653836638367383683836938370383713837238373383743837538376383773837838379383803838138382383833838438385383863838738388383893839038391383923839338394383953839638397383983839938400384013840238403384043840538406384073840838409384103841138412384133841438415384163841738418384193842038421384223842338424384253842638427384283842938430384313843238433384343843538436384373843838439384403844138442384433844438445384463844738448384493845038451384523845338454384553845638457384583845938460384613846238463384643846538466384673846838469384703847138472384733847438475384763847738478384793848038481384823848338484384853848638487384883848938490384913849238493384943849538496384973849838499385003850138502385033850438505385063850738508385093851038511385123851338514385153851638517385183851938520385213852238523385243852538526385273852838529385303853138532385333853438535385363853738538385393854038541385423854338544385453854638547385483854938550385513855238553385543855538556385573855838559385603856138562385633856438565385663856738568385693857038571385723857338574385753857638577385783857938580385813858238583385843858538586385873858838589385903859138592385933859438595385963859738598385993860038601386023860338604386053860638607386083860938610386113861238613386143861538616386173861838619386203862138622386233862438625386263862738628386293863038631386323863338634386353863638637386383863938640386413864238643386443864538646386473864838649386503865138652386533865438655386563865738658386593866038661386623866338664386653866638667386683866938670386713867238673386743867538676386773867838679386803868138682386833868438685386863868738688386893869038691386923869338694386953869638697386983869938700387013870238703387043870538706387073870838709387103871138712387133871438715387163871738718387193872038721387223872338724387253872638727387283872938730387313873238733387343873538736387373873838739387403874138742387433874438745387463874738748387493875038751387523875338754387553875638757387583875938760387613876238763387643876538766387673876838769387703877138772387733877438775387763877738778387793878038781387823878338784387853878638787387883878938790387913879238793387943879538796387973879838799388003880138802388033880438805388063880738808388093881038811388123881338814388153881638817388183881938820388213882238823388243882538826388273882838829388303883138832388333883438835388363883738838388393884038841388423884338844388453884638847388483884938850388513885238853388543885538856388573885838859388603886138862388633886438865388663886738868388693887038871388723887338874388753887638877388783887938880388813888238883388843888538886388873888838889388903889138892388933889438895388963889738898388993890038901389023890338904389053890638907389083890938910389113891238913389143891538916389173891838919389203892138922389233892438925389263892738928389293893038931389323893338934389353893638937389383893938940389413894238943389443894538946389473894838949389503895138952389533895438955389563895738958389593896038961389623896338964389653896638967389683896938970389713897238973389743897538976389773897838979389803898138982389833898438985389863898738988389893899038991389923899338994389953899638997389983899939000390013900239003390043900539006390073900839009390103901139012390133901439015390163901739018390193902039021390223902339024390253902639027390283902939030390313903239033390343903539036390373903839039390403904139042390433904439045390463904739048390493905039051390523905339054390553905639057390583905939060390613906239063390643906539066390673906839069390703907139072390733907439075390763907739078390793908039081390823908339084390853908639087390883908939090390913909239093390943909539096390973909839099391003910139102391033910439105391063910739108391093911039111391123911339114391153911639117391183911939120391213912239123391243912539126391273912839129391303913139132391333913439135391363913739138391393914039141391423914339144391453914639147391483914939150391513915239153391543915539156391573915839159391603916139162391633916439165391663916739168391693917039171391723917339174391753917639177391783917939180391813918239183391843918539186391873918839189391903919139192391933919439195391963919739198391993920039201392023920339204392053920639207392083920939210392113921239213392143921539216392173921839219392203922139222392233922439225392263922739228392293923039231392323923339234392353923639237392383923939240392413924239243392443924539246392473924839249392503925139252392533925439255392563925739258392593926039261392623926339264392653926639267392683926939270392713927239273392743927539276392773927839279392803928139282392833928439285392863928739288392893929039291392923929339294392953929639297392983929939300393013930239303393043930539306393073930839309393103931139312393133931439315393163931739318393193932039321393223932339324393253932639327393283932939330393313933239333393343933539336393373933839339393403934139342393433934439345393463934739348393493935039351393523935339354393553935639357393583935939360393613936239363393643936539366393673936839369393703937139372393733937439375393763937739378393793938039381393823938339384393853938639387393883938939390393913939239393393943939539396393973939839399394003940139402394033940439405394063940739408394093941039411394123941339414394153941639417394183941939420394213942239423394243942539426394273942839429394303943139432394333943439435394363943739438394393944039441394423944339444394453944639447394483944939450394513945239453394543945539456394573945839459394603946139462394633946439465394663946739468394693947039471394723947339474394753947639477394783947939480394813948239483394843948539486394873948839489394903949139492394933949439495394963949739498394993950039501395023950339504395053950639507395083950939510395113951239513395143951539516395173951839519395203952139522395233952439525395263952739528395293953039531395323953339534395353953639537395383953939540395413954239543395443954539546395473954839549395503955139552395533955439555395563955739558395593956039561395623956339564395653956639567395683956939570395713957239573395743957539576395773957839579395803958139582395833958439585395863958739588395893959039591395923959339594395953959639597395983959939600396013960239603396043960539606396073960839609396103961139612396133961439615396163961739618396193962039621396223962339624396253962639627396283962939630396313963239633396343963539636396373963839639396403964139642396433964439645396463964739648396493965039651396523965339654396553965639657396583965939660396613966239663396643966539666396673966839669396703967139672396733967439675396763967739678396793968039681396823968339684396853968639687396883968939690396913969239693396943969539696396973969839699397003970139702397033970439705397063970739708397093971039711397123971339714397153971639717397183971939720397213972239723397243972539726397273972839729397303973139732397333973439735397363973739738397393974039741397423974339744397453974639747397483974939750397513975239753397543975539756397573975839759397603976139762397633976439765397663976739768397693977039771397723977339774397753977639777397783977939780397813978239783397843978539786397873978839789397903979139792397933979439795397963979739798397993980039801398023980339804398053980639807398083980939810398113981239813398143981539816398173981839819398203982139822398233982439825398263982739828398293983039831398323983339834398353983639837398383983939840398413984239843398443984539846398473984839849398503985139852398533985439855398563985739858398593986039861398623986339864398653986639867398683986939870398713987239873398743987539876398773987839879398803988139882398833988439885398863988739888398893989039891398923989339894398953989639897398983989939900399013990239903399043990539906399073990839909399103991139912399133991439915399163991739918399193992039921399223992339924399253992639927399283992939930399313993239933399343993539936399373993839939399403994139942399433994439945399463994739948399493995039951399523995339954399553995639957399583995939960399613996239963399643996539966399673996839969399703997139972399733997439975399763997739978399793998039981399823998339984399853998639987399883998939990399913999239993399943999539996399973999839999400004000140002400034000440005400064000740008400094001040011400124001340014400154001640017400184001940020400214002240023400244002540026400274002840029400304003140032400334003440035400364003740038400394004040041400424004340044400454004640047400484004940050400514005240053400544005540056400574005840059400604006140062400634006440065400664006740068400694007040071400724007340074400754007640077400784007940080400814008240083400844008540086400874008840089400904009140092400934009440095400964009740098400994010040101401024010340104401054010640107401084010940110401114011240113401144011540116401174011840119401204012140122401234012440125401264012740128401294013040131401324013340134401354013640137401384013940140401414014240143401444014540146401474014840149401504015140152401534015440155401564015740158401594016040161401624016340164401654016640167401684016940170401714017240173401744017540176401774017840179401804018140182401834018440185401864018740188401894019040191401924019340194401954019640197401984019940200402014020240203402044020540206402074020840209402104021140212402134021440215402164021740218402194022040221402224022340224402254022640227402284022940230402314023240233402344023540236402374023840239402404024140242402434024440245402464024740248402494025040251402524025340254402554025640257402584025940260402614026240263402644026540266402674026840269402704027140272402734027440275402764027740278402794028040281402824028340284402854028640287402884028940290402914029240293402944029540296402974029840299403004030140302403034030440305403064030740308403094031040311403124031340314403154031640317403184031940320403214032240323403244032540326403274032840329403304033140332403334033440335403364033740338403394034040341403424034340344403454034640347403484034940350403514035240353403544035540356403574035840359403604036140362403634036440365403664036740368403694037040371403724037340374403754037640377403784037940380403814038240383403844038540386403874038840389403904039140392403934039440395403964039740398403994040040401404024040340404404054040640407404084040940410404114041240413404144041540416404174041840419404204042140422404234042440425404264042740428404294043040431404324043340434404354043640437404384043940440404414044240443404444044540446404474044840449404504045140452404534045440455404564045740458404594046040461404624046340464404654046640467404684046940470404714047240473404744047540476404774047840479404804048140482404834048440485404864048740488404894049040491404924049340494404954049640497404984049940500405014050240503405044050540506405074050840509405104051140512405134051440515405164051740518405194052040521405224052340524405254052640527405284052940530405314053240533405344053540536405374053840539405404054140542405434054440545405464054740548405494055040551405524055340554405554055640557405584055940560405614056240563405644056540566405674056840569405704057140572405734057440575405764057740578405794058040581405824058340584405854058640587405884058940590405914059240593405944059540596405974059840599406004060140602406034060440605406064060740608406094061040611406124061340614406154061640617406184061940620406214062240623406244062540626406274062840629406304063140632406334063440635406364063740638406394064040641406424064340644406454064640647406484064940650406514065240653406544065540656406574065840659406604066140662406634066440665406664066740668406694067040671406724067340674406754067640677406784067940680406814068240683406844068540686406874068840689406904069140692406934069440695406964069740698406994070040701407024070340704407054070640707407084070940710407114071240713407144071540716407174071840719407204072140722407234072440725407264072740728407294073040731407324073340734407354073640737407384073940740407414074240743407444074540746407474074840749407504075140752407534075440755407564075740758407594076040761407624076340764407654076640767407684076940770407714077240773407744077540776407774077840779407804078140782407834078440785407864078740788407894079040791407924079340794407954079640797407984079940800408014080240803408044080540806408074080840809408104081140812408134081440815408164081740818408194082040821408224082340824408254082640827408284082940830408314083240833408344083540836408374083840839408404084140842408434084440845408464084740848408494085040851408524085340854408554085640857408584085940860408614086240863408644086540866408674086840869408704087140872408734087440875408764087740878408794088040881408824088340884408854088640887408884088940890408914089240893408944089540896408974089840899409004090140902409034090440905409064090740908409094091040911409124091340914409154091640917409184091940920409214092240923409244092540926409274092840929409304093140932409334093440935409364093740938409394094040941409424094340944409454094640947409484094940950409514095240953409544095540956409574095840959409604096140962409634096440965409664096740968409694097040971409724097340974409754097640977409784097940980409814098240983409844098540986409874098840989409904099140992409934099440995409964099740998409994100041001410024100341004410054100641007410084100941010410114101241013410144101541016410174101841019410204102141022410234102441025410264102741028410294103041031410324103341034410354103641037410384103941040410414104241043410444104541046410474104841049410504105141052410534105441055410564105741058410594106041061410624106341064410654106641067410684106941070410714107241073410744107541076410774107841079410804108141082410834108441085410864108741088410894109041091410924109341094410954109641097410984109941100411014110241103411044110541106411074110841109411104111141112411134111441115411164111741118411194112041121411224112341124411254112641127411284112941130411314113241133411344113541136411374113841139411404114141142411434114441145411464114741148411494115041151411524115341154411554115641157411584115941160411614116241163411644116541166411674116841169411704117141172411734117441175411764117741178411794118041181411824118341184411854118641187411884118941190411914119241193411944119541196411974119841199412004120141202412034120441205412064120741208412094121041211412124121341214412154121641217412184121941220412214122241223412244122541226412274122841229412304123141232412334123441235412364123741238412394124041241412424124341244412454124641247412484124941250412514125241253412544125541256412574125841259412604126141262412634126441265412664126741268412694127041271412724127341274412754127641277412784127941280412814128241283412844128541286412874128841289412904129141292412934129441295412964129741298412994130041301413024130341304413054130641307413084130941310413114131241313413144131541316413174131841319413204132141322413234132441325413264132741328413294133041331413324133341334413354133641337413384133941340413414134241343413444134541346413474134841349413504135141352413534135441355413564135741358413594136041361413624136341364413654136641367413684136941370413714137241373413744137541376413774137841379413804138141382413834138441385413864138741388413894139041391413924139341394413954139641397413984139941400414014140241403414044140541406414074140841409414104141141412414134141441415414164141741418414194142041421414224142341424414254142641427414284142941430414314143241433414344143541436414374143841439414404144141442414434144441445414464144741448414494145041451414524145341454414554145641457414584145941460414614146241463414644146541466414674146841469414704147141472414734147441475414764147741478414794148041481414824148341484414854148641487414884148941490414914149241493414944149541496414974149841499415004150141502415034150441505415064150741508415094151041511415124151341514415154151641517415184151941520415214152241523415244152541526415274152841529415304153141532415334153441535415364153741538415394154041541415424154341544415454154641547415484154941550415514155241553415544155541556415574155841559415604156141562415634156441565415664156741568415694157041571415724157341574415754157641577415784157941580415814158241583415844158541586415874158841589415904159141592415934159441595415964159741598415994160041601416024160341604416054160641607416084160941610416114161241613416144161541616416174161841619416204162141622416234162441625416264162741628416294163041631416324163341634416354163641637416384163941640416414164241643416444164541646416474164841649416504165141652416534165441655416564165741658416594166041661416624166341664416654166641667416684166941670416714167241673416744167541676416774167841679416804168141682416834168441685416864168741688416894169041691416924169341694416954169641697416984169941700417014170241703417044170541706417074170841709417104171141712417134171441715417164171741718417194172041721417224172341724417254172641727417284172941730417314173241733417344173541736417374173841739417404174141742417434174441745417464174741748417494175041751417524175341754417554175641757417584175941760417614176241763417644176541766417674176841769417704177141772417734177441775417764177741778417794178041781417824178341784417854178641787417884178941790417914179241793417944179541796417974179841799418004180141802418034180441805418064180741808418094181041811418124181341814418154181641817418184181941820418214182241823418244182541826418274182841829418304183141832418334183441835418364183741838418394184041841418424184341844418454184641847418484184941850418514185241853418544185541856418574185841859418604186141862418634186441865418664186741868418694187041871418724187341874418754187641877418784187941880418814188241883418844188541886418874188841889418904189141892418934189441895418964189741898418994190041901419024190341904419054190641907419084190941910419114191241913419144191541916419174191841919419204192141922419234192441925419264192741928419294193041931419324193341934419354193641937419384193941940419414194241943419444194541946419474194841949419504195141952419534195441955419564195741958419594196041961419624196341964419654196641967419684196941970419714197241973419744197541976419774197841979419804198141982419834198441985419864198741988419894199041991419924199341994419954199641997419984199942000420014200242003420044200542006420074200842009420104201142012420134201442015420164201742018420194202042021420224202342024420254202642027420284202942030420314203242033420344203542036420374203842039420404204142042420434204442045420464204742048420494205042051420524205342054420554205642057420584205942060420614206242063420644206542066420674206842069420704207142072420734207442075420764207742078420794208042081420824208342084420854208642087420884208942090420914209242093420944209542096420974209842099421004210142102421034210442105421064210742108421094211042111421124211342114421154211642117421184211942120421214212242123421244212542126421274212842129421304213142132421334213442135421364213742138421394214042141421424214342144421454214642147421484214942150421514215242153421544215542156421574215842159421604216142162421634216442165421664216742168421694217042171421724217342174421754217642177421784217942180421814218242183421844218542186421874218842189421904219142192421934219442195421964219742198421994220042201422024220342204422054220642207422084220942210422114221242213422144221542216422174221842219422204222142222422234222442225422264222742228422294223042231422324223342234422354223642237422384223942240422414224242243422444224542246422474224842249422504225142252422534225442255422564225742258422594226042261422624226342264422654226642267422684226942270422714227242273422744227542276422774227842279422804228142282422834228442285422864228742288422894229042291422924229342294422954229642297422984229942300423014230242303423044230542306423074230842309423104231142312423134231442315423164231742318423194232042321423224232342324423254232642327423284232942330423314233242333423344233542336423374233842339423404234142342423434234442345423464234742348423494235042351423524235342354423554235642357423584235942360423614236242363423644236542366423674236842369423704237142372423734237442375423764237742378423794238042381423824238342384423854238642387423884238942390423914239242393423944239542396423974239842399424004240142402424034240442405424064240742408424094241042411424124241342414424154241642417424184241942420424214242242423424244242542426424274242842429424304243142432424334243442435424364243742438424394244042441424424244342444424454244642447424484244942450424514245242453424544245542456424574245842459424604246142462424634246442465424664246742468424694247042471424724247342474424754247642477424784247942480424814248242483424844248542486424874248842489424904249142492424934249442495424964249742498424994250042501425024250342504425054250642507425084250942510425114251242513425144251542516425174251842519425204252142522425234252442525425264252742528425294253042531425324253342534425354253642537425384253942540425414254242543425444254542546425474254842549425504255142552425534255442555425564255742558425594256042561425624256342564425654256642567425684256942570425714257242573425744257542576425774257842579425804258142582425834258442585425864258742588425894259042591425924259342594425954259642597425984259942600426014260242603426044260542606426074260842609426104261142612426134261442615426164261742618426194262042621426224262342624426254262642627426284262942630426314263242633426344263542636426374263842639426404264142642426434264442645426464264742648426494265042651426524265342654426554265642657426584265942660426614266242663426644266542666426674266842669426704267142672426734267442675426764267742678426794268042681426824268342684426854268642687426884268942690426914269242693426944269542696426974269842699427004270142702427034270442705427064270742708427094271042711427124271342714427154271642717427184271942720427214272242723427244272542726427274272842729427304273142732427334273442735427364273742738427394274042741427424274342744427454274642747427484274942750427514275242753427544275542756427574275842759427604276142762427634276442765427664276742768427694277042771427724277342774427754277642777427784277942780427814278242783427844278542786427874278842789427904279142792427934279442795427964279742798427994280042801428024280342804428054280642807428084280942810428114281242813428144281542816428174281842819428204282142822428234282442825428264282742828428294283042831428324283342834428354283642837428384283942840428414284242843428444284542846428474284842849428504285142852428534285442855428564285742858428594286042861428624286342864428654286642867428684286942870428714287242873428744287542876428774287842879428804288142882428834288442885428864288742888428894289042891428924289342894428954289642897428984289942900429014290242903429044290542906429074290842909429104291142912429134291442915429164291742918429194292042921429224292342924429254292642927429284292942930429314293242933429344293542936429374293842939429404294142942429434294442945429464294742948429494295042951429524295342954429554295642957429584295942960429614296242963429644296542966429674296842969429704297142972429734297442975429764297742978429794298042981429824298342984429854298642987429884298942990429914299242993429944299542996429974299842999430004300143002430034300443005430064300743008430094301043011430124301343014430154301643017430184301943020430214302243023430244302543026430274302843029430304303143032430334303443035430364303743038430394304043041430424304343044430454304643047430484304943050430514305243053430544305543056430574305843059430604306143062430634306443065430664306743068430694307043071430724307343074430754307643077430784307943080430814308243083430844308543086430874308843089430904309143092430934309443095430964309743098430994310043101431024310343104431054310643107431084310943110431114311243113431144311543116431174311843119431204312143122431234312443125431264312743128431294313043131431324313343134431354313643137431384313943140431414314243143431444314543146431474314843149431504315143152431534315443155431564315743158431594316043161431624316343164431654316643167431684316943170431714317243173431744317543176431774317843179431804318143182431834318443185431864318743188431894319043191431924319343194431954319643197431984319943200432014320243203432044320543206432074320843209432104321143212432134321443215432164321743218432194322043221432224322343224432254322643227432284322943230432314323243233432344323543236432374323843239432404324143242432434324443245432464324743248432494325043251432524325343254432554325643257432584325943260432614326243263432644326543266432674326843269432704327143272432734327443275432764327743278432794328043281432824328343284432854328643287432884328943290432914329243293432944329543296432974329843299433004330143302433034330443305433064330743308433094331043311433124331343314433154331643317433184331943320433214332243323433244332543326433274332843329433304333143332433334333443335433364333743338433394334043341433424334343344433454334643347433484334943350433514335243353433544335543356433574335843359433604336143362433634336443365433664336743368433694337043371433724337343374433754337643377433784337943380433814338243383433844338543386433874338843389433904339143392433934339443395433964339743398433994340043401434024340343404434054340643407434084340943410434114341243413434144341543416434174341843419434204342143422434234342443425434264342743428434294343043431434324343343434434354343643437434384343943440434414344243443434444344543446434474344843449434504345143452434534345443455434564345743458434594346043461434624346343464434654346643467434684346943470434714347243473434744347543476434774347843479434804348143482434834348443485434864348743488434894349043491434924349343494434954349643497434984349943500435014350243503435044350543506435074350843509435104351143512435134351443515435164351743518435194352043521435224352343524435254352643527435284352943530435314353243533435344353543536435374353843539435404354143542435434354443545435464354743548435494355043551435524355343554435554355643557435584355943560435614356243563435644356543566435674356843569435704357143572435734357443575435764357743578435794358043581435824358343584435854358643587435884358943590435914359243593435944359543596435974359843599436004360143602436034360443605436064360743608436094361043611436124361343614436154361643617436184361943620436214362243623436244362543626436274362843629436304363143632436334363443635436364363743638436394364043641436424364343644436454364643647436484364943650436514365243653436544365543656436574365843659436604366143662436634366443665436664366743668436694367043671436724367343674436754367643677436784367943680436814368243683436844368543686436874368843689436904369143692436934369443695436964369743698436994370043701437024370343704437054370643707437084370943710437114371243713437144371543716437174371843719437204372143722437234372443725437264372743728437294373043731437324373343734437354373643737437384373943740437414374243743437444374543746437474374843749437504375143752437534375443755437564375743758437594376043761437624376343764437654376643767437684376943770437714377243773437744377543776437774377843779437804378143782437834378443785437864378743788437894379043791437924379343794437954379643797437984379943800438014380243803438044380543806438074380843809438104381143812438134381443815438164381743818438194382043821438224382343824438254382643827438284382943830438314383243833438344383543836438374383843839438404384143842438434384443845438464384743848438494385043851438524385343854438554385643857438584385943860438614386243863438644386543866438674386843869438704387143872438734387443875438764387743878438794388043881438824388343884438854388643887438884388943890438914389243893438944389543896438974389843899439004390143902439034390443905439064390743908439094391043911439124391343914439154391643917439184391943920439214392243923439244392543926439274392843929439304393143932439334393443935439364393743938439394394043941439424394343944439454394643947439484394943950439514395243953439544395543956439574395843959439604396143962439634396443965439664396743968439694397043971439724397343974439754397643977439784397943980439814398243983439844398543986439874398843989439904399143992439934399443995439964399743998439994400044001440024400344004440054400644007440084400944010440114401244013440144401544016440174401844019440204402144022440234402444025440264402744028440294403044031440324403344034440354403644037440384403944040440414404244043440444404544046440474404844049440504405144052440534405444055440564405744058440594406044061440624406344064440654406644067440684406944070440714407244073440744407544076440774407844079440804408144082440834408444085440864408744088440894409044091440924409344094440954409644097440984409944100441014410244103441044410544106441074410844109441104411144112441134411444115441164411744118441194412044121441224412344124441254412644127441284412944130441314413244133441344413544136441374413844139441404414144142441434414444145441464414744148441494415044151441524415344154441554415644157441584415944160441614416244163441644416544166441674416844169441704417144172441734417444175441764417744178441794418044181441824418344184441854418644187441884418944190441914419244193441944419544196441974419844199442004420144202442034420444205442064420744208442094421044211442124421344214442154421644217442184421944220442214422244223442244422544226442274422844229442304423144232442334423444235442364423744238442394424044241442424424344244442454424644247442484424944250442514425244253442544425544256442574425844259442604426144262442634426444265442664426744268442694427044271442724427344274442754427644277442784427944280442814428244283442844428544286442874428844289442904429144292442934429444295442964429744298442994430044301443024430344304443054430644307443084430944310443114431244313443144431544316443174431844319443204432144322443234432444325443264432744328443294433044331443324433344334443354433644337443384433944340443414434244343443444434544346443474434844349443504435144352443534435444355443564435744358443594436044361443624436344364443654436644367443684436944370443714437244373443744437544376443774437844379443804438144382443834438444385443864438744388443894439044391443924439344394443954439644397443984439944400444014440244403444044440544406444074440844409444104441144412444134441444415444164441744418444194442044421444224442344424444254442644427444284442944430444314443244433444344443544436444374443844439444404444144442444434444444445444464444744448444494445044451444524445344454444554445644457444584445944460444614446244463444644446544466444674446844469444704447144472444734447444475444764447744478444794448044481444824448344484444854448644487444884448944490444914449244493444944449544496444974449844499445004450144502445034450444505445064450744508445094451044511445124451344514445154451644517445184451944520445214452244523445244452544526445274452844529445304453144532445334453444535445364453744538445394454044541445424454344544445454454644547445484454944550445514455244553445544455544556445574455844559445604456144562445634456444565445664456744568445694457044571445724457344574445754457644577445784457944580445814458244583445844458544586445874458844589445904459144592445934459444595445964459744598445994460044601446024460344604446054460644607446084460944610446114461244613446144461544616446174461844619446204462144622446234462444625446264462744628446294463044631446324463344634446354463644637446384463944640446414464244643446444464544646446474464844649446504465144652446534465444655446564465744658446594466044661446624466344664446654466644667446684466944670446714467244673446744467544676446774467844679446804468144682446834468444685446864468744688446894469044691446924469344694446954469644697446984469944700447014470244703447044470544706447074470844709447104471144712447134471444715447164471744718447194472044721447224472344724447254472644727447284472944730447314473244733447344473544736447374473844739447404474144742447434474444745447464474744748447494475044751447524475344754447554475644757447584475944760447614476244763447644476544766447674476844769447704477144772447734477444775447764477744778447794478044781447824478344784447854478644787447884478944790447914479244793447944479544796447974479844799448004480144802448034480444805448064480744808448094481044811448124481344814448154481644817448184481944820448214482244823448244482544826448274482844829448304483144832448334483444835448364483744838448394484044841448424484344844448454484644847448484484944850448514485244853448544485544856448574485844859448604486144862448634486444865448664486744868448694487044871448724487344874448754487644877448784487944880448814488244883448844488544886448874488844889448904489144892448934489444895448964489744898448994490044901449024490344904449054490644907449084490944910449114491244913449144491544916449174491844919449204492144922449234492444925449264492744928449294493044931449324493344934449354493644937449384493944940449414494244943449444494544946449474494844949449504495144952449534495444955449564495744958449594496044961449624496344964449654496644967449684496944970449714497244973449744497544976449774497844979449804498144982449834498444985449864498744988449894499044991449924499344994449954499644997449984499945000450014500245003450044500545006450074500845009450104501145012450134501445015450164501745018450194502045021450224502345024450254502645027450284502945030450314503245033450344503545036450374503845039450404504145042450434504445045450464504745048450494505045051450524505345054450554505645057450584505945060450614506245063450644506545066450674506845069450704507145072450734507445075450764507745078450794508045081450824508345084450854508645087450884508945090450914509245093450944509545096450974509845099451004510145102451034510445105451064510745108451094511045111451124511345114451154511645117451184511945120451214512245123451244512545126451274512845129451304513145132451334513445135451364513745138451394514045141451424514345144451454514645147451484514945150451514515245153451544515545156451574515845159451604516145162451634516445165451664516745168451694517045171451724517345174451754517645177451784517945180451814518245183451844518545186451874518845189451904519145192451934519445195451964519745198451994520045201452024520345204452054520645207452084520945210452114521245213452144521545216452174521845219452204522145222452234522445225452264522745228452294523045231452324523345234452354523645237452384523945240452414524245243452444524545246452474524845249452504525145252452534525445255452564525745258452594526045261452624526345264452654526645267452684526945270452714527245273452744527545276452774527845279452804528145282452834528445285452864528745288452894529045291452924529345294452954529645297452984529945300453014530245303453044530545306453074530845309453104531145312453134531445315453164531745318453194532045321453224532345324453254532645327453284532945330453314533245333453344533545336453374533845339453404534145342453434534445345453464534745348453494535045351453524535345354453554535645357453584535945360453614536245363453644536545366453674536845369453704537145372453734537445375453764537745378453794538045381453824538345384453854538645387453884538945390453914539245393453944539545396453974539845399454004540145402454034540445405454064540745408454094541045411454124541345414454154541645417454184541945420454214542245423454244542545426454274542845429454304543145432454334543445435454364543745438454394544045441454424544345444454454544645447454484544945450454514545245453454544545545456454574545845459454604546145462454634546445465454664546745468454694547045471454724547345474454754547645477454784547945480454814548245483454844548545486454874548845489454904549145492454934549445495454964549745498454994550045501455024550345504455054550645507455084550945510455114551245513455144551545516455174551845519455204552145522455234552445525455264552745528455294553045531455324553345534455354553645537455384553945540455414554245543455444554545546455474554845549455504555145552455534555445555455564555745558455594556045561455624556345564455654556645567455684556945570455714557245573455744557545576455774557845579455804558145582455834558445585455864558745588455894559045591455924559345594455954559645597455984559945600456014560245603456044560545606456074560845609456104561145612456134561445615456164561745618456194562045621456224562345624456254562645627456284562945630456314563245633456344563545636456374563845639456404564145642456434564445645456464564745648456494565045651456524565345654456554565645657456584565945660456614566245663456644566545666456674566845669456704567145672456734567445675456764567745678456794568045681456824568345684456854568645687456884568945690456914569245693456944569545696456974569845699457004570145702457034570445705457064570745708457094571045711457124571345714457154571645717457184571945720457214572245723457244572545726457274572845729457304573145732457334573445735457364573745738457394574045741457424574345744457454574645747457484574945750457514575245753457544575545756457574575845759457604576145762457634576445765457664576745768457694577045771457724577345774457754577645777457784577945780457814578245783457844578545786457874578845789457904579145792457934579445795457964579745798457994580045801458024580345804458054580645807458084580945810458114581245813458144581545816458174581845819458204582145822458234582445825458264582745828458294583045831458324583345834458354583645837458384583945840458414584245843458444584545846458474584845849458504585145852458534585445855458564585745858458594586045861458624586345864458654586645867458684586945870458714587245873458744587545876458774587845879458804588145882458834588445885458864588745888458894589045891458924589345894458954589645897458984589945900459014590245903459044590545906459074590845909459104591145912459134591445915459164591745918459194592045921459224592345924459254592645927459284592945930459314593245933459344593545936459374593845939459404594145942459434594445945459464594745948459494595045951459524595345954459554595645957459584595945960459614596245963459644596545966459674596845969459704597145972459734597445975459764597745978459794598045981459824598345984459854598645987459884598945990459914599245993459944599545996459974599845999460004600146002460034600446005
  1. /* test.c
  2. *
  3. * Copyright (C) 2006-2022 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL.
  6. *
  7. * wolfSSL is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * wolfSSL is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  20. */
  21. #ifdef HAVE_CONFIG_H
  22. #include <config.h>
  23. #endif
  24. #ifndef WOLFSSL_USER_SETTINGS
  25. #include <wolfssl/options.h>
  26. #endif
  27. #include <wolfssl/wolfcrypt/settings.h>
  28. #ifndef NO_CRYPT_TEST
  29. #include <wolfssl/version.h>
  30. #include <wolfssl/wolfcrypt/types.h>
  31. #include <wolfssl/wolfcrypt/wc_port.h>
  32. #include <wolfssl/wolfcrypt/mem_track.h>
  33. #if defined(HAVE_WOLFCRYPT_TEST_OPTIONS)
  34. #include <wolfssl/ssl.h>
  35. #define err_sys err_sys_remap /* remap err_sys */
  36. #include <wolfssl/test.h>
  37. #undef err_sys
  38. #endif
  39. #if defined(WC_ECC_NONBLOCK) && defined(WOLFSSL_PUBLIC_MP) && \
  40. defined(HAVE_ECC_SIGN) && defined(HAVE_ECC_VERIFY)
  41. #include <stdint.h>
  42. #endif
  43. #ifdef HAVE_STACK_SIZE_VERBOSE
  44. #ifdef WOLFSSL_TEST_MAX_RELATIVE_STACK_BYTES
  45. static ssize_t max_relative_stack = WOLFSSL_TEST_MAX_RELATIVE_STACK_BYTES;
  46. #else
  47. static ssize_t max_relative_stack = -1;
  48. #endif
  49. #endif
  50. #ifdef WOLFSSL_TRACK_MEMORY_VERBOSE
  51. #ifdef WOLFSSL_TEST_MAX_RELATIVE_HEAP_ALLOCS
  52. static ssize_t max_relative_heap_allocs = WOLFSSL_TEST_MAX_RELATIVE_HEAP_ALLOCS;
  53. #else
  54. static ssize_t max_relative_heap_allocs = -1;
  55. #endif
  56. #ifdef WOLFSSL_TEST_MAX_RELATIVE_HEAP_BYTES
  57. static ssize_t max_relative_heap_bytes = WOLFSSL_TEST_MAX_RELATIVE_HEAP_BYTES;
  58. #else
  59. static ssize_t max_relative_heap_bytes = -1;
  60. #endif
  61. #define PRINT_HEAP_CHECKPOINT() { \
  62. const ssize_t _rha = wolfCrypt_heap_peakAllocs_checkpoint() - heap_baselineAllocs; \
  63. const ssize_t _rhb = wolfCrypt_heap_peakBytes_checkpoint() - heap_baselineBytes; \
  64. printf(" relative heap peak usage: %ld alloc%s, %ld bytes\n", \
  65. (long int)_rha, \
  66. _rha == 1 ? "" : "s", \
  67. (long int)_rhb); \
  68. if ((max_relative_heap_allocs > 0) && (_rha > max_relative_heap_allocs)) \
  69. return err_sys("heap allocs exceed designated max.", -1); \
  70. if ((max_relative_heap_bytes > 0) && (_rhb > max_relative_heap_bytes)) \
  71. return err_sys("heap bytes exceed designated max.", -1); \
  72. heap_baselineAllocs = wolfCrypt_heap_peakAllocs_checkpoint(); \
  73. heap_baselineBytes = wolfCrypt_heap_peakBytes_checkpoint(); \
  74. }
  75. #else
  76. #define PRINT_HEAP_CHECKPOINT()
  77. #endif
  78. #ifdef USE_FLAT_TEST_H
  79. #ifdef HAVE_CONFIG_H
  80. #include "test_paths.h"
  81. #endif
  82. #include "test.h"
  83. #else
  84. #ifdef HAVE_CONFIG_H
  85. #include "wolfcrypt/test/test_paths.h"
  86. #endif
  87. #include "wolfcrypt/test/test.h"
  88. #endif
  89. /* printf mappings */
  90. #ifndef WOLFSSL_LOG_PRINTF
  91. #if defined(FREESCALE_MQX) || defined(FREESCALE_KSDK_MQX)
  92. #include <mqx.h>
  93. #include <stdlib.h>
  94. /* see wc_port.h for fio.h and nio.h includes */
  95. #elif defined(FREESCALE_KSDK_BM)
  96. #include "fsl_debug_console.h"
  97. #undef printf
  98. #define printf PRINTF
  99. #elif defined(WOLFSSL_APACHE_MYNEWT)
  100. #include <assert.h>
  101. #include <string.h>
  102. #include "sysinit/sysinit.h"
  103. #include "os/os.h"
  104. #ifdef ARCH_sim
  105. #include "mcu/mcu_sim.h"
  106. #endif
  107. #include "os/os_time.h"
  108. #elif defined(WOLFSSL_ESPIDF)
  109. #include <time.h>
  110. #include <sys/time.h>
  111. #elif defined(WOLFSSL_ZEPHYR)
  112. #include <stdio.h>
  113. #define printf printk
  114. #elif defined(MICRIUM)
  115. #include <os.h>
  116. #if (OS_VERSION < 50000)
  117. #include <bsp_ser.h>
  118. void BSP_Ser_Printf (CPU_CHAR* format, ...);
  119. #undef printf
  120. #define printf BSP_Ser_Printf
  121. #else
  122. #include <stdio.h>
  123. #endif
  124. #elif defined(WOLFSSL_PB)
  125. #include <stdarg.h>
  126. int wolfssl_pb_print(const char*, ...);
  127. #undef printf
  128. #define printf wolfssl_pb_print
  129. #elif defined(WOLFSSL_TELIT_M2MB)
  130. #include "wolfssl/wolfcrypt/wc_port.h" /* for m2mb headers */
  131. #include "m2m_log.h" /* for M2M_LOG_INFO - not standard API */
  132. /* remap printf */
  133. #undef printf
  134. #define printf M2M_LOG_INFO
  135. /* OS requires occasional sleep() */
  136. #ifndef TEST_SLEEP_MS
  137. #define TEST_SLEEP_MS 50
  138. #endif
  139. #define TEST_SLEEP() m2mb_os_taskSleep(M2MB_OS_MS2TICKS(TEST_SLEEP_MS))
  140. /* don't use file system for these tests, since ./certs dir isn't loaded */
  141. #undef NO_FILESYSTEM
  142. #define NO_FILESYSTEM
  143. #elif defined(THREADX) && !defined(WOLFSSL_WICED) && \
  144. !defined(THREADX_NO_DC_PRINTF)
  145. #ifndef NETOS
  146. /* since just testing, use THREADX log printf instead (NETOS prototypes
  147. * this elsewhere) */
  148. int dc_log_printf(char*, ...);
  149. #endif
  150. #undef printf
  151. #define printf dc_log_printf
  152. #elif defined(ANDROID)
  153. #ifdef XMALLOC_USER
  154. #include <stdlib.h> /* we're using malloc / free direct here */
  155. #endif
  156. #ifndef STRING_USER
  157. #include <stdio.h>
  158. #endif
  159. #include <android/log.h>
  160. #ifdef ANDROID_V454 /* See fips/android/wolfCrypt_v454_android */
  161. #ifndef NO_FILESYSTEM
  162. #define NO_FILESYSTEM /* Turn off tests that want to call SaveDerAndPem() */
  163. #endif
  164. #else
  165. #define printf(...) \
  166. __android_log_print(ANDROID_LOG_DEBUG, "[WOLFCRYPT]", __VA_ARGS__)
  167. #define fprintf(fp, ...) \
  168. __android_log_print(ANDROID_LOG_DEBUG, "[WOLFCRYPT]", __VA_ARGS__)
  169. #endif
  170. #elif defined(WOLFSSL_DEOS)
  171. #include <printx.h>
  172. #undef printf
  173. #define printf printx
  174. #else
  175. #ifdef XMALLOC_USER
  176. #include <stdlib.h> /* we're using malloc / free direct here */
  177. #endif
  178. #if !defined(STRING_USER) && !defined(WOLFSSL_LINUXKM)
  179. #include <stdio.h>
  180. #endif
  181. #if defined(WOLFSSL_LINUXKM) && !defined(WOLFSSL_LINUXKM_VERBOSE_DEBUG)
  182. #undef printf
  183. #define printf(...) ({})
  184. #endif
  185. /* enable way for customer to override test/bench printf */
  186. #ifdef XPRINTF
  187. #undef printf
  188. #define printf XPRINTF
  189. #elif !defined(printf)
  190. /* arrange for printf() to flush after every message -- this assures
  191. * redirected output (to a log file) records progress right up to the
  192. * moment of a crash/abort(); otherwise anything queued in stdout would
  193. * be lost.
  194. */
  195. #define printf(...) ( printf(__VA_ARGS__), fflush(stdout) )
  196. #endif
  197. #endif
  198. #endif /* !WOLFSSL_LOG_PRINTF */
  199. #include <wolfssl/wolfcrypt/memory.h>
  200. #include <wolfssl/wolfcrypt/wc_port.h>
  201. #include <wolfssl/wolfcrypt/logging.h>
  202. #include <wolfssl/wolfcrypt/types.h>
  203. #include <wolfssl/wolfcrypt/asn.h>
  204. #include <wolfssl/wolfcrypt/md2.h>
  205. #include <wolfssl/wolfcrypt/md5.h>
  206. #include <wolfssl/wolfcrypt/md4.h>
  207. #include <wolfssl/wolfcrypt/sha.h>
  208. #include <wolfssl/wolfcrypt/sha256.h>
  209. #include <wolfssl/wolfcrypt/sha512.h>
  210. #include <wolfssl/wolfcrypt/rc2.h>
  211. #include <wolfssl/wolfcrypt/arc4.h>
  212. #if defined(WC_NO_RNG)
  213. #include <wolfssl/wolfcrypt/integer.h>
  214. #else
  215. #include <wolfssl/wolfcrypt/random.h>
  216. #endif
  217. #include <wolfssl/wolfcrypt/coding.h>
  218. #include <wolfssl/wolfcrypt/signature.h>
  219. #include <wolfssl/wolfcrypt/rsa.h>
  220. #include <wolfssl/wolfcrypt/des3.h>
  221. #include <wolfssl/wolfcrypt/aes.h>
  222. #include <wolfssl/wolfcrypt/wc_encrypt.h>
  223. #include <wolfssl/wolfcrypt/cmac.h>
  224. #include <wolfssl/wolfcrypt/siphash.h>
  225. #include <wolfssl/wolfcrypt/poly1305.h>
  226. #include <wolfssl/wolfcrypt/camellia.h>
  227. #include <wolfssl/wolfcrypt/hmac.h>
  228. #include <wolfssl/wolfcrypt/kdf.h>
  229. #include <wolfssl/wolfcrypt/dh.h>
  230. #include <wolfssl/wolfcrypt/dsa.h>
  231. #include <wolfssl/wolfcrypt/srp.h>
  232. #include <wolfssl/wolfcrypt/chacha.h>
  233. #include <wolfssl/wolfcrypt/chacha20_poly1305.h>
  234. #include <wolfssl/wolfcrypt/pwdbased.h>
  235. #include <wolfssl/wolfcrypt/ripemd.h>
  236. #include <wolfssl/wolfcrypt/error-crypt.h>
  237. #ifdef HAVE_ECC
  238. #include <wolfssl/wolfcrypt/ecc.h>
  239. #endif
  240. #ifdef HAVE_CURVE25519
  241. #include <wolfssl/wolfcrypt/curve25519.h>
  242. #endif
  243. #ifdef HAVE_ED25519
  244. #include <wolfssl/wolfcrypt/ed25519.h>
  245. #endif
  246. #ifdef HAVE_CURVE448
  247. #include <wolfssl/wolfcrypt/curve448.h>
  248. #endif
  249. #ifdef HAVE_ED448
  250. #include <wolfssl/wolfcrypt/ed448.h>
  251. #endif
  252. #ifdef WOLFSSL_HAVE_KYBER
  253. #include <wolfssl/wolfcrypt/kyber.h>
  254. #ifdef WOLFSSL_WC_KYBER
  255. #include <wolfssl/wolfcrypt/wc_kyber.h>
  256. #endif
  257. #if defined(HAVE_LIBOQS) || defined(HAVE_PQM4)
  258. #include <wolfssl/wolfcrypt/ext_kyber.h>
  259. #endif
  260. #endif
  261. #ifdef WOLFCRYPT_HAVE_ECCSI
  262. #include <wolfssl/wolfcrypt/eccsi.h>
  263. #endif
  264. #ifdef WOLFCRYPT_HAVE_SAKKE
  265. #include <wolfssl/wolfcrypt/sakke.h>
  266. #endif
  267. #if defined(HAVE_BLAKE2) || defined(HAVE_BLAKE2S)
  268. #include <wolfssl/wolfcrypt/blake2.h>
  269. #endif
  270. #ifdef WOLFSSL_SHA3
  271. #include <wolfssl/wolfcrypt/sha3.h>
  272. #endif
  273. #ifdef HAVE_LIBZ
  274. #include <wolfssl/wolfcrypt/compress.h>
  275. #endif
  276. #ifdef HAVE_PKCS7
  277. #include <wolfssl/wolfcrypt/pkcs7.h>
  278. #endif
  279. #ifdef HAVE_FIPS
  280. #include <wolfssl/wolfcrypt/fips_test.h>
  281. #endif
  282. #ifdef HAVE_SELFTEST
  283. #include <wolfssl/wolfcrypt/selftest.h>
  284. #endif
  285. #ifdef WOLFSSL_ASYNC_CRYPT
  286. #include <wolfssl/wolfcrypt/async.h>
  287. #endif
  288. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  289. #include <wolfssl/wolfcrypt/logging.h>
  290. #endif
  291. #ifdef WOLFSSL_CAAM
  292. #include <wolfssl/wolfcrypt/port/caam/wolfcaam.h>
  293. #endif
  294. #ifdef WOLF_CRYPTO_CB
  295. #include <wolfssl/wolfcrypt/cryptocb.h>
  296. #ifdef HAVE_INTEL_QA_SYNC
  297. #include <wolfssl/wolfcrypt/port/intel/quickassist_sync.h>
  298. #endif
  299. #ifdef HAVE_CAVIUM_OCTEON_SYNC
  300. #include <wolfssl/wolfcrypt/port/cavium/cavium_octeon_sync.h>
  301. #endif
  302. #endif
  303. #ifdef _MSC_VER
  304. /* 4996 warning to use MS extensions e.g., strcpy_s instead of strncpy */
  305. #pragma warning(disable: 4996)
  306. #endif
  307. #ifdef OPENSSL_EXTRA
  308. #ifndef WOLFCRYPT_ONLY
  309. #include <wolfssl/openssl/evp.h>
  310. #include <wolfssl/openssl/hmac.h>
  311. #endif
  312. #include <wolfssl/openssl/rand.h>
  313. #include <wolfssl/openssl/aes.h>
  314. #include <wolfssl/openssl/des.h>
  315. #endif
  316. #if defined(NO_FILESYSTEM) || defined(WC_NO_RNG)
  317. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  318. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096)
  319. #define USE_CERT_BUFFERS_2048
  320. #endif
  321. #if !defined(USE_CERT_BUFFERS_256)
  322. #define USE_CERT_BUFFERS_256
  323. #endif
  324. #endif
  325. #if defined(WOLFSSL_CERT_GEN) && (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES))
  326. #define ENABLE_ECC384_CERT_GEN_TEST
  327. #endif
  328. #include <wolfssl/certs_test.h>
  329. #ifdef DEVKITPRO
  330. #include <wiiuse/wpad.h>
  331. #endif
  332. #ifdef WOLFSSL_STATIC_MEMORY
  333. static WOLFSSL_HEAP_HINT* HEAP_HINT;
  334. #else
  335. #define HEAP_HINT NULL
  336. #endif /* WOLFSSL_STATIC_MEMORY */
  337. /* these cases do not have intermediate hashing support */
  338. #if (defined(WOLFSSL_AFALG_XILINX_SHA3) && !defined(WOLFSSL_AFALG_HASH_KEEP)) \
  339. && !defined(WOLFSSL_XILINX_CRYPT) || defined(WOLFSSL_XILINX_CRYPT_VERSAL)
  340. #define NO_INTM_HASH_TEST
  341. #endif
  342. #if defined(WOLFSSL_RENESAS_TSIP) || defined(WOLFSSL_RENESAS_SCEPROTECT) || \
  343. defined(WOLFSSL_SECO_CAAM)
  344. #define HASH_SIZE_LIMIT
  345. #endif
  346. #if defined(WOLFSSL_CERT_GEN) && (!defined(NO_RSA) || defined(HAVE_ECC)) || \
  347. (defined(WOLFSSL_TEST_CERT) && (defined(HAVE_ED25519) || defined(HAVE_ED448)))
  348. static void initDefaultName(void);
  349. #endif
  350. /* for async devices */
  351. #ifdef WOLFSSL_CAAM_DEVID
  352. static int devId = WOLFSSL_CAAM_DEVID;
  353. #else
  354. static int devId = INVALID_DEVID;
  355. #endif
  356. #ifdef HAVE_WNR
  357. const char* wnrConfigFile = "wnr-example.conf";
  358. #endif
  359. #define TEST_STRING "Everyone gets Friday off."
  360. #define TEST_STRING_SZ 25
  361. typedef struct testVector {
  362. const char* input;
  363. const char* output;
  364. size_t inLen;
  365. size_t outLen;
  366. } testVector;
  367. #ifndef WOLFSSL_TEST_SUBROUTINE
  368. #define WOLFSSL_TEST_SUBROUTINE
  369. #endif
  370. PRAGMA_GCC("GCC diagnostic ignored \"-Wunused-function\"")
  371. PRAGMA_CLANG("clang diagnostic ignored \"-Wunused-function\"")
  372. WOLFSSL_TEST_SUBROUTINE int error_test(void);
  373. WOLFSSL_TEST_SUBROUTINE int base64_test(void);
  374. WOLFSSL_TEST_SUBROUTINE int base16_test(void);
  375. WOLFSSL_TEST_SUBROUTINE int asn_test(void);
  376. WOLFSSL_TEST_SUBROUTINE int md2_test(void);
  377. WOLFSSL_TEST_SUBROUTINE int md5_test(void);
  378. WOLFSSL_TEST_SUBROUTINE int md4_test(void);
  379. WOLFSSL_TEST_SUBROUTINE int sha_test(void);
  380. WOLFSSL_TEST_SUBROUTINE int sha224_test(void);
  381. WOLFSSL_TEST_SUBROUTINE int sha256_test(void);
  382. WOLFSSL_TEST_SUBROUTINE int sha512_test(void);
  383. WOLFSSL_TEST_SUBROUTINE int sha384_test(void);
  384. WOLFSSL_TEST_SUBROUTINE int sha3_test(void);
  385. WOLFSSL_TEST_SUBROUTINE int shake128_test(void);
  386. WOLFSSL_TEST_SUBROUTINE int shake256_test(void);
  387. WOLFSSL_TEST_SUBROUTINE int hash_test(void);
  388. WOLFSSL_TEST_SUBROUTINE int hmac_md5_test(void);
  389. WOLFSSL_TEST_SUBROUTINE int hmac_sha_test(void);
  390. WOLFSSL_TEST_SUBROUTINE int hmac_sha224_test(void);
  391. WOLFSSL_TEST_SUBROUTINE int hmac_sha256_test(void);
  392. WOLFSSL_TEST_SUBROUTINE int hmac_sha384_test(void);
  393. WOLFSSL_TEST_SUBROUTINE int hmac_sha512_test(void);
  394. WOLFSSL_TEST_SUBROUTINE int hmac_sha3_test(void);
  395. #if defined(HAVE_HKDF) && !defined(NO_HMAC)
  396. /* hkdf_test has issue with WOLFSSL_TEST_SUBROUTINE set on Xilinx with afalg */
  397. static int hkdf_test(void);
  398. #endif
  399. WOLFSSL_TEST_SUBROUTINE int sshkdf_test(void);
  400. #ifdef WOLFSSL_TLS13
  401. WOLFSSL_TEST_SUBROUTINE int tls13_kdf_test(void);
  402. #endif
  403. WOLFSSL_TEST_SUBROUTINE int x963kdf_test(void);
  404. WOLFSSL_TEST_SUBROUTINE int arc4_test(void);
  405. #ifdef WC_RC2
  406. WOLFSSL_TEST_SUBROUTINE int rc2_test(void);
  407. #endif
  408. WOLFSSL_TEST_SUBROUTINE int chacha_test(void);
  409. WOLFSSL_TEST_SUBROUTINE int XChaCha_test(void);
  410. WOLFSSL_TEST_SUBROUTINE int chacha20_poly1305_aead_test(void);
  411. WOLFSSL_TEST_SUBROUTINE int XChaCha20Poly1305_test(void);
  412. WOLFSSL_TEST_SUBROUTINE int des_test(void);
  413. WOLFSSL_TEST_SUBROUTINE int des3_test(void);
  414. WOLFSSL_TEST_SUBROUTINE int aes_test(void);
  415. WOLFSSL_TEST_SUBROUTINE int aes192_test(void);
  416. WOLFSSL_TEST_SUBROUTINE int aes256_test(void);
  417. WOLFSSL_TEST_SUBROUTINE int aesofb_test(void);
  418. WOLFSSL_TEST_SUBROUTINE int cmac_test(void);
  419. #if defined(WOLFSSL_SIPHASH)
  420. WOLFSSL_TEST_SUBROUTINE int siphash_test(void);
  421. #endif
  422. WOLFSSL_TEST_SUBROUTINE int poly1305_test(void);
  423. WOLFSSL_TEST_SUBROUTINE int aesgcm_test(void);
  424. WOLFSSL_TEST_SUBROUTINE int aesgcm_default_test(void);
  425. WOLFSSL_TEST_SUBROUTINE int gmac_test(void);
  426. WOLFSSL_TEST_SUBROUTINE int aesccm_test(void);
  427. WOLFSSL_TEST_SUBROUTINE int aeskeywrap_test(void);
  428. WOLFSSL_TEST_SUBROUTINE int camellia_test(void);
  429. #ifdef WC_RSA_NO_PADDING
  430. WOLFSSL_TEST_SUBROUTINE int rsa_no_pad_test(void);
  431. #endif
  432. WOLFSSL_TEST_SUBROUTINE int rsa_test(void);
  433. WOLFSSL_TEST_SUBROUTINE int dh_test(void);
  434. WOLFSSL_TEST_SUBROUTINE int dsa_test(void);
  435. WOLFSSL_TEST_SUBROUTINE int srp_test(void);
  436. #ifndef WC_NO_RNG
  437. WOLFSSL_TEST_SUBROUTINE int random_test(void);
  438. #endif /* WC_NO_RNG */
  439. WOLFSSL_TEST_SUBROUTINE int pwdbased_test(void);
  440. WOLFSSL_TEST_SUBROUTINE int ripemd_test(void);
  441. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  442. WOLFSSL_TEST_SUBROUTINE int openssl_test(void); /* test mini api */
  443. WOLFSSL_TEST_SUBROUTINE int openssl_pkey0_test(void);
  444. WOLFSSL_TEST_SUBROUTINE int openssl_pkey1_test(void);
  445. WOLFSSL_TEST_SUBROUTINE int openSSL_evpMD_test(void);
  446. WOLFSSL_TEST_SUBROUTINE int openssl_evpSig_test(void);
  447. #endif
  448. WOLFSSL_TEST_SUBROUTINE int pbkdf1_test(void);
  449. WOLFSSL_TEST_SUBROUTINE int pkcs12_test(void);
  450. WOLFSSL_TEST_SUBROUTINE int pbkdf2_test(void);
  451. WOLFSSL_TEST_SUBROUTINE int scrypt_test(void);
  452. #ifdef HAVE_ECC
  453. WOLFSSL_TEST_SUBROUTINE int ecc_test(void);
  454. #if defined(HAVE_ECC_ENCRYPT) && defined(HAVE_AES_CBC) && \
  455. (defined(WOLFSSL_AES_128) || defined(WOLFSSL_AES_256))
  456. WOLFSSL_TEST_SUBROUTINE int ecc_encrypt_test(void);
  457. #endif
  458. #if defined(USE_CERT_BUFFERS_256) && !defined(WOLFSSL_ATECC508A) && \
  459. !defined(WOLFSSL_ATECC608A) && !defined(NO_ECC256) && \
  460. defined(HAVE_ECC_VERIFY) && defined(HAVE_ECC_SIGN) && \
  461. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  462. /* skip for ATECC508/608A, cannot import private key buffers */
  463. WOLFSSL_TEST_SUBROUTINE int ecc_test_buffers(void);
  464. #endif
  465. #endif
  466. #ifdef HAVE_CURVE25519
  467. WOLFSSL_TEST_SUBROUTINE int curve25519_test(void);
  468. #endif
  469. #ifdef HAVE_ED25519
  470. WOLFSSL_TEST_SUBROUTINE int ed25519_test(void);
  471. #endif
  472. #ifdef HAVE_CURVE448
  473. WOLFSSL_TEST_SUBROUTINE int curve448_test(void);
  474. #endif
  475. #ifdef HAVE_ED448
  476. WOLFSSL_TEST_SUBROUTINE int ed448_test(void);
  477. #endif
  478. #ifdef WOLFSSL_HAVE_KYBER
  479. WOLFSSL_TEST_SUBROUTINE int kyber_test(void);
  480. #endif
  481. #ifdef WOLFCRYPT_HAVE_ECCSI
  482. WOLFSSL_TEST_SUBROUTINE int eccsi_test(void);
  483. #endif
  484. #ifdef WOLFCRYPT_HAVE_SAKKE
  485. WOLFSSL_TEST_SUBROUTINE int sakke_test(void);
  486. #endif
  487. #ifdef HAVE_BLAKE2
  488. WOLFSSL_TEST_SUBROUTINE int blake2b_test(void);
  489. #endif
  490. #ifdef HAVE_BLAKE2S
  491. WOLFSSL_TEST_SUBROUTINE int blake2s_test(void);
  492. #endif
  493. #ifdef HAVE_LIBZ
  494. WOLFSSL_TEST_SUBROUTINE int compress_test(void);
  495. #endif
  496. #ifdef HAVE_PKCS7
  497. #ifndef NO_PKCS7_ENCRYPTED_DATA
  498. WOLFSSL_TEST_SUBROUTINE int pkcs7encrypted_test(void);
  499. #endif
  500. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  501. WOLFSSL_TEST_SUBROUTINE int pkcs7compressed_test(void);
  502. #endif
  503. WOLFSSL_TEST_SUBROUTINE int pkcs7signed_test(void);
  504. WOLFSSL_TEST_SUBROUTINE int pkcs7enveloped_test(void);
  505. #if defined(HAVE_AESGCM) || defined(HAVE_AESCCM)
  506. WOLFSSL_TEST_SUBROUTINE int pkcs7authenveloped_test(void);
  507. #endif
  508. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  509. WOLFSSL_TEST_SUBROUTINE int pkcs7callback_test(byte* cert, word32 certSz, byte* key,
  510. word32 keySz);
  511. #endif
  512. #endif
  513. #if !defined(NO_ASN_TIME) && !defined(NO_RSA) && defined(WOLFSSL_TEST_CERT) && \
  514. !defined(NO_FILESYSTEM)
  515. WOLFSSL_TEST_SUBROUTINE int cert_test(void);
  516. #endif
  517. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_TEST_CERT) && \
  518. !defined(NO_FILESYSTEM) && defined(WOLFSSL_CERT_GEN)
  519. WOLFSSL_TEST_SUBROUTINE int certext_test(void);
  520. #endif
  521. #if defined(WOLFSSL_CERT_GEN_CACHE) && defined(WOLFSSL_TEST_CERT) && \
  522. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  523. WOLFSSL_TEST_SUBROUTINE int decodedCertCache_test(void);
  524. #endif
  525. WOLFSSL_TEST_SUBROUTINE int memory_test(void);
  526. #ifdef HAVE_VALGRIND
  527. WOLFSSL_TEST_SUBROUTINE int mp_test(void);
  528. #endif
  529. #if defined(WOLFSSL_PUBLIC_MP) && defined(WOLFSSL_KEY_GEN)
  530. WOLFSSL_TEST_SUBROUTINE int prime_test(void);
  531. #endif
  532. #if defined(ASN_BER_TO_DER) && \
  533. (defined(WOLFSSL_TEST_CERT) || defined(OPENSSL_EXTRA) || \
  534. defined(OPENSSL_EXTRA_X509_SMALL))
  535. WOLFSSL_TEST_SUBROUTINE int berder_test(void);
  536. #endif
  537. WOLFSSL_TEST_SUBROUTINE int logging_test(void);
  538. #if !defined(NO_ASN) && !defined(NO_ASN_TIME)
  539. WOLFSSL_TEST_SUBROUTINE int time_test(void);
  540. #endif
  541. WOLFSSL_TEST_SUBROUTINE int mutex_test(void);
  542. #if defined(USE_WOLFSSL_MEMORY) && !defined(FREERTOS)
  543. WOLFSSL_TEST_SUBROUTINE int memcb_test(void);
  544. #endif
  545. #ifdef WOLFSSL_IMX6_CAAM_BLOB
  546. WOLFSSL_TEST_SUBROUTINE int blob_test(void);
  547. #endif
  548. #ifdef WOLF_CRYPTO_CB
  549. WOLFSSL_TEST_SUBROUTINE int cryptocb_test(void);
  550. #endif
  551. #ifdef WOLFSSL_CERT_PIV
  552. WOLFSSL_TEST_SUBROUTINE int certpiv_test(void);
  553. #endif
  554. #ifdef WOLFSSL_AES_SIV
  555. WOLFSSL_TEST_SUBROUTINE int aes_siv_test(void);
  556. #endif
  557. /* General big buffer size for many tests. */
  558. #define FOURK_BUF 4096
  559. #define ERROR_OUT(err, eLabel) do { ret = (err); goto eLabel; } while (0)
  560. #ifdef HAVE_STACK_SIZE
  561. static THREAD_RETURN err_sys(const char* msg, int es)
  562. #else
  563. static int err_sys(const char* msg, int es)
  564. #endif
  565. {
  566. (void)msg;
  567. (void)es;
  568. #ifdef WOLFSSL_LINUXKM
  569. lkm_printf("%s error = %d\n", msg, es);
  570. EXIT_TEST(es);
  571. #else
  572. printf("%s error = %d\n", msg, es);
  573. EXIT_TEST(-1);
  574. #endif
  575. }
  576. #ifndef HAVE_WOLFCRYPT_TEST_OPTIONS
  577. /* func_args from test.h, so don't have to pull in other stuff */
  578. typedef struct func_args {
  579. int argc;
  580. char** argv;
  581. int return_code;
  582. } func_args;
  583. #endif /* !HAVE_WOLFCRYPT_TEST_OPTIONS */
  584. #if defined(HAVE_FIPS) && !defined(WOLFSSL_LINUXKM)
  585. static void myFipsCb(int ok, int err, const char* hash)
  586. {
  587. printf("in my Fips callback, ok = %d, err = %d\n", ok, err);
  588. printf("message = %s\n", wc_GetErrorString(err));
  589. printf("hash = %s\n", hash);
  590. if (err == IN_CORE_FIPS_E) {
  591. printf("In core integrity hash check failure, copy above hash\n");
  592. printf("into verifyCore[] in fips_test.c and rebuild\n");
  593. }
  594. }
  595. #endif /* HAVE_FIPS && !WOLFSSL_LINUXKM */
  596. #ifdef WOLFSSL_STATIC_MEMORY
  597. #if defined(WOLFSSL_STATIC_MEMORY_TEST_SZ)
  598. static byte gTestMemory[WOLFSSL_STATIC_MEMORY_TEST_SZ];
  599. #elif defined(BENCH_EMBEDDED)
  600. static byte gTestMemory[14000];
  601. #elif defined(WOLFSSL_CERT_EXT)
  602. static byte gTestMemory[140000];
  603. #elif (defined(WOLFSSL_SP_MATH_ALL) || defined(USE_FAST_MATH)) && \
  604. !defined(ALT_ECC_SIZE)
  605. static byte gTestMemory[160000];
  606. #else
  607. static byte gTestMemory[80000];
  608. #endif
  609. #endif
  610. #ifdef WOLFSSL_PB
  611. static int wolfssl_pb_print(const char* msg, ...)
  612. {
  613. int ret;
  614. va_list args;
  615. char tmpBuf[80];
  616. va_start(args, msg);
  617. ret = vsprint(tmpBuf, msg, args);
  618. va_end(args);
  619. fnDumpStringToSystemLog(tmpBuf);
  620. return ret;
  621. }
  622. #endif /* WOLFSSL_PB */
  623. /* optional macro to add sleep between tests */
  624. #ifndef TEST_SLEEP
  625. #define TEST_SLEEP()
  626. #else
  627. #define TEST_PASS test_pass
  628. #include <stdarg.h> /* for var args */
  629. static WC_INLINE void test_pass(const char* fmt, ...)
  630. {
  631. va_list args;
  632. va_start(args, fmt);
  633. STACK_SIZE_CHECKPOINT_WITH_MAX_CHECK(max_relative_stack, vprintf(fmt, args));
  634. va_end(args);
  635. PRINT_HEAP_CHECKPOINT();
  636. TEST_SLEEP();
  637. ASSERT_RESTORED_VECTOR_REGISTERS(exit(1););
  638. }
  639. #endif
  640. /* set test pass output to printf if not overriden */
  641. #ifndef TEST_PASS
  642. /* redirect to printf */
  643. #define TEST_PASS(...) { \
  644. if (STACK_SIZE_CHECKPOINT_WITH_MAX_CHECK \
  645. (max_relative_stack, printf(__VA_ARGS__)) < 0) { \
  646. return err_sys("post-test check failed", -1); \
  647. } \
  648. PRINT_HEAP_CHECKPOINT(); \
  649. ASSERT_RESTORED_VECTOR_REGISTERS(exit(1);); \
  650. }
  651. #endif
  652. #ifdef HAVE_STACK_SIZE
  653. THREAD_RETURN WOLFSSL_THREAD wolfcrypt_test(void* args)
  654. #else
  655. int wolfcrypt_test(void* args)
  656. #endif
  657. {
  658. int ret;
  659. #ifdef WOLFSSL_TRACK_MEMORY_VERBOSE
  660. long heap_baselineAllocs, heap_baselineBytes;
  661. #endif
  662. STACK_SIZE_INIT();
  663. #ifdef WOLFSSL_TRACK_MEMORY_VERBOSE
  664. (void)wolfCrypt_heap_peakAllocs_checkpoint();
  665. heap_baselineAllocs = wolfCrypt_heap_peakAllocs_checkpoint();
  666. (void)wolfCrypt_heap_peakBytes_checkpoint();
  667. heap_baselineBytes = wolfCrypt_heap_peakBytes_checkpoint();
  668. #endif
  669. printf("------------------------------------------------------------------------------\n");
  670. printf(" wolfSSL version %s\n", LIBWOLFSSL_VERSION_STRING);
  671. printf("------------------------------------------------------------------------------\n");
  672. if (args) {
  673. #ifdef HAVE_WOLFCRYPT_TEST_OPTIONS
  674. int ch;
  675. #endif
  676. ((func_args*)args)->return_code = -1; /* error state */
  677. #ifdef HAVE_WOLFCRYPT_TEST_OPTIONS
  678. while ((ch = mygetopt(((func_args*)args)->argc, ((func_args*)args)->argv, "s:m:a:h")) != -1) {
  679. switch(ch) {
  680. case 's':
  681. #ifdef HAVE_STACK_SIZE_VERBOSE
  682. max_relative_stack = (ssize_t)atoi(myoptarg);
  683. break;
  684. #else
  685. return err_sys("-s (max relative stack bytes) requires HAVE_STACK_SIZE_VERBOSE (--enable-stacksize=verbose).", -1);
  686. #endif
  687. case 'm':
  688. #ifdef WOLFSSL_TRACK_MEMORY_VERBOSE
  689. max_relative_heap_bytes = (ssize_t)atoi(myoptarg);
  690. break;
  691. #else
  692. return err_sys("-m (max relative heap memory bytes) requires WOLFSSL_TRACK_MEMORY_VERBOSE (--enable-trackmemory=verbose).", -1);
  693. #endif
  694. case 'a':
  695. #ifdef WOLFSSL_TRACK_MEMORY_VERBOSE
  696. max_relative_heap_allocs = (ssize_t)atoi(myoptarg);
  697. break;
  698. #else
  699. return err_sys("-a (max relative heap allocs) requires WOLFSSL_TRACK_MEMORY_VERBOSE (--enable-trackmemory=verbose).", -1);
  700. #endif
  701. case 'h':
  702. return err_sys("\
  703. options: [-s max_relative_stack_bytes] [-m max_relative_heap_memory_bytes]\n\
  704. [-a max_relative_heap_allocs] [-h]\n", 0);
  705. default:
  706. return err_sys("unknown test option. try -h.", -1);
  707. }
  708. }
  709. #endif
  710. }
  711. #ifdef WOLFSSL_STATIC_MEMORY
  712. if (wc_LoadStaticMemory(&HEAP_HINT, gTestMemory, sizeof(gTestMemory),
  713. WOLFMEM_GENERAL, 1) != 0) {
  714. printf("unable to load static memory.\n");
  715. return(EXIT_FAILURE);
  716. }
  717. #endif
  718. #if defined(DEBUG_WOLFSSL) && !defined(HAVE_VALGRIND)
  719. wolfSSL_Debugging_ON();
  720. #endif
  721. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  722. wc_SetLoggingHeap(HEAP_HINT);
  723. #endif
  724. #if defined(HAVE_FIPS) && !defined(WOLFSSL_LINUXKM)
  725. wolfCrypt_SetCb_fips(myFipsCb);
  726. #endif
  727. #if !defined(NO_BIG_INT)
  728. if (CheckCtcSettings() != 1) {
  729. printf("Sizeof mismatch (build) %x != (run) %lx\n",
  730. CTC_SETTINGS, (unsigned long)CheckRunTimeSettings());
  731. return err_sys("Build vs runtime math mismatch\n", -1000);
  732. }
  733. #if defined(USE_FAST_MATH) && \
  734. (!defined(NO_RSA) || !defined(NO_DH) || defined(HAVE_ECC))
  735. if (CheckFastMathSettings() != 1)
  736. return err_sys("Build vs runtime fastmath FP_MAX_BITS mismatch\n",
  737. -1001);
  738. #endif /* USE_FAST_MATH */
  739. #endif /* !NO_BIG_INT */
  740. #if defined(WOLFSSL_CERT_GEN) && (!defined(NO_RSA) || defined(HAVE_ECC)) || \
  741. (defined(WOLFSSL_TEST_CERT) && (defined(HAVE_ED25519) || defined(HAVE_ED448)))
  742. initDefaultName();
  743. #endif
  744. #ifdef WOLFSSL_ASYNC_CRYPT
  745. ret = wolfAsync_DevOpen(&devId);
  746. if (ret < 0) {
  747. printf("Async device open failed\nRunning without async\n");
  748. }
  749. #else
  750. (void)devId;
  751. #endif /* WOLFSSL_ASYNC_CRYPT */
  752. #ifdef WOLF_CRYPTO_CB
  753. #ifdef HAVE_INTEL_QA_SYNC
  754. devId = wc_CryptoCb_InitIntelQa();
  755. if (INVALID_DEVID == devId) {
  756. printf("Couldn't init the Intel QA\n");
  757. }
  758. #endif
  759. #ifdef HAVE_CAVIUM_OCTEON_SYNC
  760. devId = wc_CryptoCb_InitOcteon();
  761. if (INVALID_DEVID == devId) {
  762. printf("Couldn't init the Cavium Octeon\n");
  763. }
  764. #endif
  765. #endif
  766. #ifdef HAVE_SELFTEST
  767. if ( (ret = wolfCrypt_SelfTest()) != 0)
  768. return err_sys("CAVP selftest failed!\n", ret);
  769. else
  770. TEST_PASS("CAVP selftest passed!\n");
  771. #endif
  772. if ( (ret = error_test()) != 0)
  773. return err_sys("error test failed!\n", ret);
  774. else
  775. TEST_PASS("error test passed!\n");
  776. if ( (ret = memory_test()) != 0)
  777. return err_sys("MEMORY test failed!\n", ret);
  778. else
  779. TEST_PASS("MEMORY test passed!\n");
  780. #ifndef NO_CODING
  781. if ( (ret = base64_test()) != 0)
  782. return err_sys("base64 test failed!\n", ret);
  783. else
  784. TEST_PASS("base64 test passed!\n");
  785. #ifdef WOLFSSL_BASE16
  786. if ( (ret = base16_test()) != 0)
  787. return err_sys("base16 test failed!\n", ret);
  788. else
  789. TEST_PASS("base16 test passed!\n");
  790. #endif
  791. #endif /* !NO_CODING */
  792. #ifndef NO_ASN
  793. if ( (ret = asn_test()) != 0)
  794. return err_sys("asn test failed!\n", ret);
  795. else
  796. TEST_PASS("asn test passed!\n");
  797. #endif
  798. #ifndef WC_NO_RNG
  799. if ( (ret = random_test()) != 0)
  800. return err_sys("RANDOM test failed!\n", ret);
  801. else
  802. TEST_PASS("RANDOM test passed!\n");
  803. #endif /* WC_NO_RNG */
  804. #ifndef NO_MD5
  805. if ( (ret = md5_test()) != 0)
  806. return err_sys("MD5 test failed!\n", ret);
  807. else
  808. TEST_PASS("MD5 test passed!\n");
  809. #endif
  810. #ifdef WOLFSSL_MD2
  811. if ( (ret = md2_test()) != 0)
  812. return err_sys("MD2 test failed!\n", ret);
  813. else
  814. TEST_PASS("MD2 test passed!\n");
  815. #endif
  816. #ifndef NO_MD4
  817. if ( (ret = md4_test()) != 0)
  818. return err_sys("MD4 test failed!\n", ret);
  819. else
  820. TEST_PASS("MD4 test passed!\n");
  821. #endif
  822. #ifndef NO_SHA
  823. if ( (ret = sha_test()) != 0)
  824. return err_sys("SHA test failed!\n", ret);
  825. else
  826. TEST_PASS("SHA test passed!\n");
  827. #endif
  828. #ifdef WOLFSSL_SHA224
  829. if ( (ret = sha224_test()) != 0)
  830. return err_sys("SHA-224 test failed!\n", ret);
  831. else
  832. TEST_PASS("SHA-224 test passed!\n");
  833. #endif
  834. #ifndef NO_SHA256
  835. if ( (ret = sha256_test()) != 0)
  836. return err_sys("SHA-256 test failed!\n", ret);
  837. else
  838. TEST_PASS("SHA-256 test passed!\n");
  839. #endif
  840. #ifdef WOLFSSL_SHA384
  841. if ( (ret = sha384_test()) != 0)
  842. return err_sys("SHA-384 test failed!\n", ret);
  843. else
  844. TEST_PASS("SHA-384 test passed!\n");
  845. #endif
  846. #ifdef WOLFSSL_SHA512
  847. if ( (ret = sha512_test()) != 0)
  848. return err_sys("SHA-512 test failed!\n", ret);
  849. else
  850. TEST_PASS("SHA-512 test passed!\n");
  851. #endif
  852. #ifdef WOLFSSL_SHA3
  853. if ( (ret = sha3_test()) != 0)
  854. return err_sys("SHA-3 test failed!\n", ret);
  855. else
  856. TEST_PASS("SHA-3 test passed!\n");
  857. #endif
  858. #ifdef WOLFSSL_SHAKE128
  859. if ( (ret = shake128_test()) != 0)
  860. return err_sys("SHAKE128 test failed!\n", ret);
  861. else
  862. TEST_PASS("SHAKE128 test passed!\n");
  863. #endif
  864. #ifdef WOLFSSL_SHAKE256
  865. if ( (ret = shake256_test()) != 0)
  866. return err_sys("SHAKE256 test failed!\n", ret);
  867. else
  868. TEST_PASS("SHAKE256 test passed!\n");
  869. #endif
  870. #ifndef NO_HASH_WRAPPER
  871. if ( (ret = hash_test()) != 0)
  872. return err_sys("Hash test failed!\n", ret);
  873. else
  874. TEST_PASS("Hash test passed!\n");
  875. #endif
  876. #ifdef WOLFSSL_RIPEMD
  877. if ( (ret = ripemd_test()) != 0)
  878. return err_sys("RIPEMD test failed!\n", ret);
  879. else
  880. TEST_PASS("RIPEMD test passed!\n");
  881. #endif
  882. #ifdef HAVE_BLAKE2
  883. if ( (ret = blake2b_test()) != 0)
  884. return err_sys("BLAKE2b test failed!\n", ret);
  885. else
  886. TEST_PASS("BLAKE2b test passed!\n");
  887. #endif
  888. #ifdef HAVE_BLAKE2S
  889. if ( (ret = blake2s_test()) != 0)
  890. return err_sys("BLAKE2s test failed!\n", ret);
  891. else
  892. TEST_PASS("BLAKE2s test passed!\n");
  893. #endif
  894. #ifndef NO_HMAC
  895. #if !defined(NO_MD5) && !(defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) \
  896. && (HAVE_FIPS_VERSION >= 5))
  897. if ( (ret = hmac_md5_test()) != 0)
  898. return err_sys("HMAC-MD5 test failed!\n", ret);
  899. else
  900. TEST_PASS("HMAC-MD5 test passed!\n");
  901. #endif
  902. #ifndef NO_SHA
  903. if ( (ret = hmac_sha_test()) != 0)
  904. return err_sys("HMAC-SHA test failed!\n", ret);
  905. else
  906. TEST_PASS("HMAC-SHA test passed!\n");
  907. #endif
  908. #ifdef WOLFSSL_SHA224
  909. if ( (ret = hmac_sha224_test()) != 0)
  910. return err_sys("HMAC-SHA224 test failed!\n", ret);
  911. else
  912. TEST_PASS("HMAC-SHA224 test passed!\n");
  913. #endif
  914. #ifndef NO_SHA256
  915. if ( (ret = hmac_sha256_test()) != 0)
  916. return err_sys("HMAC-SHA256 test failed!\n", ret);
  917. else
  918. TEST_PASS("HMAC-SHA256 test passed!\n");
  919. #endif
  920. #ifdef WOLFSSL_SHA384
  921. if ( (ret = hmac_sha384_test()) != 0)
  922. return err_sys("HMAC-SHA384 test failed!\n", ret);
  923. else
  924. TEST_PASS("HMAC-SHA384 test passed!\n");
  925. #endif
  926. #ifdef WOLFSSL_SHA512
  927. if ( (ret = hmac_sha512_test()) != 0)
  928. return err_sys("HMAC-SHA512 test failed!\n", ret);
  929. else
  930. TEST_PASS("HMAC-SHA512 test passed!\n");
  931. #endif
  932. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA3) && \
  933. !defined(WOLFSSL_NOSHA3_224) && !defined(WOLFSSL_NOSHA3_256) && \
  934. !defined(WOLFSSL_NOSHA3_384) && !defined(WOLFSSL_NOSHA3_512)
  935. if ( (ret = hmac_sha3_test()) != 0)
  936. return err_sys("HMAC-SHA3 test failed!\n", ret);
  937. else
  938. TEST_PASS("HMAC-SHA3 test passed!\n");
  939. #endif
  940. #if defined(HAVE_HKDF) && !defined(NO_HMAC)
  941. PRIVATE_KEY_UNLOCK();
  942. if ( (ret = hkdf_test()) != 0)
  943. return err_sys("HMAC-KDF test failed!\n", ret);
  944. else
  945. TEST_PASS("HMAC-KDF test passed!\n");
  946. PRIVATE_KEY_LOCK();
  947. #endif
  948. #endif /* !NO_HMAC */
  949. #ifdef WOLFSSL_WOLFSSH
  950. PRIVATE_KEY_UNLOCK();
  951. if ( (ret = sshkdf_test()) != 0)
  952. return err_sys("SSH-KDF test failed!\n", ret);
  953. else
  954. TEST_PASS("SSH-KDF test passed!\n");
  955. PRIVATE_KEY_LOCK();
  956. #endif /* WOLFSSL_WOLFSSH */
  957. #ifdef WOLFSSL_TLS13
  958. PRIVATE_KEY_UNLOCK();
  959. if ( (ret = tls13_kdf_test()) != 0)
  960. return err_sys("TLSv1.3 KDF test failed!\n", ret);
  961. else
  962. TEST_PASS("TLSv1.3 KDF test passed!\n");
  963. PRIVATE_KEY_LOCK();
  964. #endif /* WOLFSSL_TLS13 */
  965. #if defined(HAVE_X963_KDF) && defined(HAVE_ECC)
  966. if ( (ret = x963kdf_test()) != 0)
  967. return err_sys("X963-KDF test failed!\n", ret);
  968. else
  969. TEST_PASS("X963-KDF test passed!\n");
  970. #endif
  971. #if defined(HAVE_AESGCM) && defined(WOLFSSL_AES_128) && \
  972. !defined(WOLFSSL_AFALG_XILINX_AES) && !defined(WOLFSSL_XILINX_CRYPT)
  973. if ( (ret = gmac_test()) != 0)
  974. return err_sys("GMAC test failed!\n", ret);
  975. else
  976. TEST_PASS("GMAC test passed!\n");
  977. #endif
  978. #ifdef WC_RC2
  979. if ( (ret = rc2_test()) != 0)
  980. return err_sys("RC2 test failed!\n", ret);
  981. else
  982. TEST_PASS("RC2 test passed!\n");
  983. #endif
  984. #ifndef NO_RC4
  985. if ( (ret = arc4_test()) != 0)
  986. return err_sys("ARC4 test failed!\n", ret);
  987. else
  988. TEST_PASS("ARC4 test passed!\n");
  989. #endif
  990. #ifdef HAVE_CHACHA
  991. if ( (ret = chacha_test()) != 0)
  992. return err_sys("Chacha test failed!\n", ret);
  993. else
  994. TEST_PASS("Chacha test passed!\n");
  995. #endif
  996. #ifdef HAVE_XCHACHA
  997. if ( (ret = XChaCha_test()) != 0)
  998. return err_sys("XChacha test failed!\n", ret);
  999. else
  1000. TEST_PASS("XChacha test passed!\n");
  1001. #endif
  1002. #ifdef HAVE_POLY1305
  1003. if ( (ret = poly1305_test()) != 0)
  1004. return err_sys("POLY1305 test failed!\n", ret);
  1005. else
  1006. TEST_PASS("POLY1305 test passed!\n");
  1007. #endif
  1008. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  1009. if ( (ret = chacha20_poly1305_aead_test()) != 0)
  1010. return err_sys("ChaCha20-Poly1305 AEAD test failed!\n", ret);
  1011. else
  1012. TEST_PASS("ChaCha20-Poly1305 AEAD test passed!\n");
  1013. #endif
  1014. #if defined(HAVE_XCHACHA) && defined(HAVE_POLY1305)
  1015. if ( (ret = XChaCha20Poly1305_test()) != 0)
  1016. return err_sys("XChaCha20-Poly1305 AEAD test failed!\n", ret);
  1017. else
  1018. TEST_PASS("XChaCha20-Poly1305 AEAD test passed!\n");
  1019. #endif
  1020. #ifndef NO_DES3
  1021. if ( (ret = des_test()) != 0)
  1022. return err_sys("DES test failed!\n", ret);
  1023. else
  1024. TEST_PASS("DES test passed!\n");
  1025. #endif
  1026. #ifndef NO_DES3
  1027. if ( (ret = des3_test()) != 0)
  1028. return err_sys("DES3 test failed!\n", ret);
  1029. else
  1030. TEST_PASS("DES3 test passed!\n");
  1031. #endif
  1032. #ifndef NO_AES
  1033. if ( (ret = aes_test()) != 0)
  1034. return err_sys("AES test failed!\n", ret);
  1035. else
  1036. TEST_PASS("AES test passed!\n");
  1037. #ifdef WOLFSSL_AES_192
  1038. if ( (ret = aes192_test()) != 0)
  1039. return err_sys("AES192 test failed!\n", ret);
  1040. else
  1041. TEST_PASS("AES192 test passed!\n");
  1042. #endif
  1043. #ifdef WOLFSSL_AES_256
  1044. if ( (ret = aes256_test()) != 0)
  1045. return err_sys("AES256 test failed!\n", ret);
  1046. else
  1047. TEST_PASS("AES256 test passed!\n");
  1048. #endif
  1049. #ifdef WOLFSSL_AES_OFB
  1050. if ( (ret = aesofb_test()) != 0)
  1051. return err_sys("AES-OFB test failed!\n", ret);
  1052. else
  1053. TEST_PASS("AESOFB test passed!\n");
  1054. #endif
  1055. #ifdef HAVE_AESGCM
  1056. #if !defined(WOLFSSL_AFALG) && !defined(WOLFSSL_DEVCRYPTO)
  1057. if ( (ret = aesgcm_test()) != 0)
  1058. return err_sys("AES-GCM test failed!\n", ret);
  1059. #endif
  1060. #if !defined(WOLFSSL_AFALG_XILINX_AES) && !defined(WOLFSSL_XILINX_CRYPT) && \
  1061. !defined(WOLFSSL_KCAPI_AES) && !(defined(WOLF_CRYPTO_CB) && \
  1062. (defined(HAVE_INTEL_QA_SYNC) || defined(HAVE_CAVIUM_OCTEON_SYNC)))
  1063. if ((ret = aesgcm_default_test()) != 0) {
  1064. return err_sys("AES-GCM test failed!\n", ret);
  1065. }
  1066. #endif
  1067. if (ret == 0) {
  1068. TEST_PASS("AES-GCM test passed!\n");
  1069. }
  1070. #endif
  1071. #if defined(HAVE_AESCCM) && defined(WOLFSSL_AES_128)
  1072. if ( (ret = aesccm_test()) != 0)
  1073. return err_sys("AES-CCM test failed!\n", ret);
  1074. else
  1075. TEST_PASS("AES-CCM test passed!\n");
  1076. #endif
  1077. #ifdef HAVE_AES_KEYWRAP
  1078. if ( (ret = aeskeywrap_test()) != 0)
  1079. return err_sys("AES Key Wrap test failed!\n", ret);
  1080. else
  1081. TEST_PASS("AES Key Wrap test passed!\n");
  1082. #endif
  1083. #ifdef WOLFSSL_AES_SIV
  1084. if ( (ret = aes_siv_test()) != 0)
  1085. return err_sys("AES-SIV test failed!\n", ret);
  1086. else
  1087. TEST_PASS("AES-SIV test passed!\n");
  1088. #endif
  1089. #endif
  1090. #ifdef HAVE_CAMELLIA
  1091. if ( (ret = camellia_test()) != 0)
  1092. return err_sys("CAMELLIA test failed!\n", ret);
  1093. else
  1094. TEST_PASS("CAMELLIA test passed!\n");
  1095. #endif
  1096. #if !defined(NO_RSA)
  1097. #ifdef WC_RSA_NO_PADDING
  1098. if ( (ret = rsa_no_pad_test()) != 0)
  1099. return err_sys("RSA NOPAD test failed!\n", ret);
  1100. else
  1101. TEST_PASS("RSA NOPAD test passed!\n");
  1102. #endif
  1103. if ( (ret = rsa_test()) != 0)
  1104. return err_sys("RSA test failed!\n", ret);
  1105. else
  1106. TEST_PASS("RSA test passed!\n");
  1107. #endif
  1108. #ifndef NO_DH
  1109. PRIVATE_KEY_UNLOCK();
  1110. if ( (ret = dh_test()) != 0)
  1111. return err_sys("DH test failed!\n", ret);
  1112. else
  1113. TEST_PASS("DH test passed!\n");
  1114. PRIVATE_KEY_LOCK();
  1115. #endif
  1116. #ifndef NO_DSA
  1117. if ( (ret = dsa_test()) != 0)
  1118. return err_sys("DSA test failed!\n", ret);
  1119. else
  1120. TEST_PASS("DSA test passed!\n");
  1121. #endif
  1122. #ifdef WOLFCRYPT_HAVE_SRP
  1123. if ( (ret = srp_test()) != 0)
  1124. return err_sys("SRP test failed!\n", ret);
  1125. else
  1126. TEST_PASS("SRP test passed!\n");
  1127. #endif
  1128. #ifndef NO_PWDBASED
  1129. if ( (ret = pwdbased_test()) != 0)
  1130. return err_sys("PWDBASED test failed!\n", ret);
  1131. else
  1132. TEST_PASS("PWDBASED test passed!\n");
  1133. #endif
  1134. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  1135. if ( (ret = openssl_test()) != 0)
  1136. return err_sys("OPENSSL test failed!\n", ret);
  1137. else
  1138. TEST_PASS("OPENSSL test passed!\n");
  1139. if ( (ret = openSSL_evpMD_test()) != 0)
  1140. return err_sys("OPENSSL (EVP MD) test failed!\n", ret);
  1141. else
  1142. TEST_PASS("OPENSSL (EVP MD) passed!\n");
  1143. if ( (ret = openssl_pkey0_test()) != 0)
  1144. return err_sys("OPENSSL (PKEY0) test failed!\n", ret);
  1145. else
  1146. TEST_PASS("OPENSSL (PKEY0) passed!\n");
  1147. if ( (ret = openssl_pkey1_test()) != 0)
  1148. return err_sys("OPENSSL (PKEY1) test failed!\n", ret);
  1149. else
  1150. TEST_PASS("OPENSSL (PKEY1) passed!\n");
  1151. #if !defined(WOLF_CRYPTO_CB_ONLY_RSA) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  1152. if ( (ret = openssl_evpSig_test()) != 0)
  1153. return err_sys("OPENSSL (EVP Sign/Verify) test failed!\n", ret);
  1154. else
  1155. TEST_PASS("OPENSSL (EVP Sign/Verify) passed!\n");
  1156. #endif
  1157. #endif
  1158. #if defined(HAVE_ECC)
  1159. PRIVATE_KEY_UNLOCK();
  1160. if ( (ret = ecc_test()) != 0)
  1161. return err_sys("ECC test failed!\n", ret);
  1162. else
  1163. TEST_PASS("ECC test passed!\n");
  1164. PRIVATE_KEY_LOCK();
  1165. #if defined(HAVE_ECC_ENCRYPT) && defined(HAVE_AES_CBC) && \
  1166. (defined(WOLFSSL_AES_128) || defined(WOLFSSL_AES_256))
  1167. if ( (ret = ecc_encrypt_test()) != 0)
  1168. return err_sys("ECC Enc test failed!\n", ret);
  1169. else
  1170. TEST_PASS("ECC Enc test passed!\n");
  1171. #endif
  1172. #if defined(USE_CERT_BUFFERS_256) && !defined(WOLFSSL_ATECC508A) && \
  1173. !defined(WOLFSSL_ATECC608A) && !defined(NO_ECC256) && \
  1174. defined(HAVE_ECC_VERIFY) && defined(HAVE_ECC_SIGN) && \
  1175. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  1176. /* skip for ATECC508/608A, cannot import private key buffers */
  1177. if ( (ret = ecc_test_buffers()) != 0)
  1178. return err_sys("ECC buffer test failed!\n", ret);
  1179. else
  1180. TEST_PASS("ECC buffer test passed!\n");
  1181. #endif
  1182. #endif
  1183. #if !defined(NO_ASN_TIME) && !defined(NO_RSA) && defined(WOLFSSL_TEST_CERT) && \
  1184. !defined(NO_FILESYSTEM)
  1185. if ( (ret = cert_test()) != 0)
  1186. return err_sys("CERT test failed!\n", ret);
  1187. else
  1188. TEST_PASS("CERT test passed!\n");
  1189. #endif
  1190. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_TEST_CERT) && \
  1191. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && defined(WOLFSSL_GEN_CERT)
  1192. if ( (ret = certext_test()) != 0)
  1193. return err_sys("CERT EXT test failed!\n", ret);
  1194. else
  1195. TEST_PASS("CERT EXT test passed!\n");
  1196. #endif
  1197. #if defined(WOLFSSL_CERT_GEN_CACHE) && defined(WOLFSSL_TEST_CERT) && \
  1198. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  1199. if ( (ret = decodedCertCache_test()) != 0)
  1200. return err_sys("DECODED CERT CACHE test failed!\n", ret);
  1201. else
  1202. TEST_PASS("DECODED CERT CACHE test passed!\n");
  1203. #endif
  1204. #ifdef HAVE_CURVE25519
  1205. if ( (ret = curve25519_test()) != 0)
  1206. return err_sys("CURVE25519 test failed!\n", ret);
  1207. else
  1208. TEST_PASS("CURVE25519 test passed!\n");
  1209. #endif
  1210. #ifdef HAVE_ED25519
  1211. if ( (ret = ed25519_test()) != 0)
  1212. return err_sys("ED25519 test failed!\n", ret);
  1213. else
  1214. TEST_PASS("ED25519 test passed!\n");
  1215. #endif
  1216. #ifdef HAVE_CURVE448
  1217. if ( (ret = curve448_test()) != 0)
  1218. return err_sys("CURVE448 test failed!\n", ret);
  1219. else
  1220. TEST_PASS("CURVE448 test passed!\n");
  1221. #endif
  1222. #ifdef HAVE_ED448
  1223. if ( (ret = ed448_test()) != 0)
  1224. return err_sys("ED448 test failed!\n", ret);
  1225. else
  1226. TEST_PASS("ED448 test passed!\n");
  1227. #endif
  1228. #ifdef WOLFSSL_HAVE_KYBER
  1229. if ( (ret = kyber_test()) != 0)
  1230. return err_sys("KYBER test failed!\n", ret);
  1231. else
  1232. TEST_PASS("KYBER test passed!\n");
  1233. #endif
  1234. #ifdef WOLFCRYPT_HAVE_ECCSI
  1235. if ( (ret = eccsi_test()) != 0)
  1236. return err_sys("ECCSI test failed!\n", ret);
  1237. else
  1238. TEST_PASS("ECCSI test passed!\n");
  1239. #endif
  1240. #ifdef WOLFCRYPT_HAVE_SAKKE
  1241. if ( (ret = sakke_test()) != 0)
  1242. return err_sys("SAKKE test failed!\n", ret);
  1243. else
  1244. TEST_PASS("SAKKE test passed!\n");
  1245. #endif
  1246. #if defined(WOLFSSL_CMAC) && !defined(NO_AES)
  1247. if ( (ret = cmac_test()) != 0)
  1248. return err_sys("CMAC test failed!\n", ret);
  1249. else
  1250. TEST_PASS("CMAC test passed!\n");
  1251. #endif
  1252. #if defined(WOLFSSL_SIPHASH)
  1253. if ( (ret = siphash_test()) != 0)
  1254. return err_sys("SipHash test failed!\n", ret);
  1255. else
  1256. TEST_PASS("SipHash test passed!\n");
  1257. #endif
  1258. #ifdef HAVE_LIBZ
  1259. if ( (ret = compress_test()) != 0)
  1260. return err_sys("COMPRESS test failed!\n", ret);
  1261. else
  1262. TEST_PASS("COMPRESS test passed!\n");
  1263. #endif
  1264. #ifdef HAVE_PKCS7
  1265. #ifndef NO_PKCS7_ENCRYPTED_DATA
  1266. if ( (ret = pkcs7encrypted_test()) != 0)
  1267. return err_sys("PKCS7encrypted test failed!\n", ret);
  1268. else
  1269. TEST_PASS("PKCS7encrypted test passed!\n");
  1270. #endif
  1271. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  1272. if ( (ret = pkcs7compressed_test()) != 0)
  1273. return err_sys("PKCS7compressed test failed!\n", ret);
  1274. else
  1275. TEST_PASS("PKCS7compressed test passed!\n");
  1276. #endif
  1277. if ( (ret = pkcs7signed_test()) != 0)
  1278. return err_sys("PKCS7signed test failed!\n", ret);
  1279. else
  1280. TEST_PASS("PKCS7signed test passed!\n");
  1281. if ( (ret = pkcs7enveloped_test()) != 0)
  1282. return err_sys("PKCS7enveloped test failed!\n", ret);
  1283. else
  1284. TEST_PASS("PKCS7enveloped test passed!\n");
  1285. #if defined(HAVE_AESGCM) || defined(HAVE_AESCCM)
  1286. if ( (ret = pkcs7authenveloped_test()) != 0)
  1287. return err_sys("PKCS7authenveloped test failed!\n", ret);
  1288. else
  1289. TEST_PASS("PKCS7authenveloped test passed!\n");
  1290. #endif
  1291. #endif
  1292. #ifdef HAVE_VALGRIND
  1293. if ( (ret = mp_test()) != 0)
  1294. return err_sys("mp test failed!\n", ret);
  1295. else
  1296. TEST_PASS("mp test passed!\n");
  1297. #endif
  1298. #if defined(WOLFSSL_PUBLIC_MP) && defined(WOLFSSL_KEY_GEN)
  1299. if ( (ret = prime_test()) != 0)
  1300. return err_sys("prime test failed!\n", ret);
  1301. else
  1302. TEST_PASS("prime test passed!\n");
  1303. #endif
  1304. #if defined(ASN_BER_TO_DER) && \
  1305. (defined(WOLFSSL_TEST_CERT) || defined(OPENSSL_EXTRA) || \
  1306. defined(OPENSSL_EXTRA_X509_SMALL))
  1307. if ( (ret = berder_test()) != 0)
  1308. return err_sys("ber-der test failed!\n", ret);
  1309. else
  1310. TEST_PASS("ber-der test passed!\n");
  1311. #endif
  1312. if ( (ret = logging_test()) != 0)
  1313. return err_sys("logging test failed!\n", ret);
  1314. else
  1315. TEST_PASS("logging test passed!\n");
  1316. #if !defined(NO_ASN) && !defined(NO_ASN_TIME)
  1317. if ( (ret = time_test()) != 0)
  1318. return err_sys("time test failed!\n", ret);
  1319. else
  1320. TEST_PASS("time test passed!\n");
  1321. #endif
  1322. if ( (ret = mutex_test()) != 0)
  1323. return err_sys("mutex test failed!\n", ret);
  1324. else
  1325. TEST_PASS("mutex test passed!\n");
  1326. #if defined(USE_WOLFSSL_MEMORY) && !defined(FREERTOS)
  1327. if ( (ret = memcb_test()) != 0)
  1328. return err_sys("memcb test failed!\n", ret);
  1329. else
  1330. TEST_PASS("memcb test passed!\n");
  1331. #endif
  1332. #ifdef WOLFSSL_IMX6_CAAM_BLOB
  1333. if ( (ret = blob_test()) != 0)
  1334. return err_sys("blob test failed!\n", ret);
  1335. else
  1336. TEST_PASS("blob test passed!\n");
  1337. #endif
  1338. #if defined(WOLF_CRYPTO_CB) && \
  1339. !(defined(HAVE_INTEL_QAT_SYNC) || defined(HAVE_CAVIUM_OCTEON_SYNC) || \
  1340. defined(WOLFSSL_QNX_CAAM))
  1341. if ( (ret = cryptocb_test()) != 0)
  1342. return err_sys("crypto callback test failed!\n", ret);
  1343. else
  1344. TEST_PASS("crypto callback test passed!\n");
  1345. #endif
  1346. #ifdef WOLFSSL_CERT_PIV
  1347. if ( (ret = certpiv_test()) != 0)
  1348. return err_sys("cert piv test failed!\n", ret);
  1349. else
  1350. TEST_PASS("cert piv test passed!\n");
  1351. #endif
  1352. #ifdef WOLF_CRYPTO_CB
  1353. #ifdef HAVE_INTEL_QA_SYNC
  1354. wc_CryptoCb_CleanupIntelQa(&devId);
  1355. #endif
  1356. #ifdef HAVE_CAVIUM_OCTEON_SYNC
  1357. wc_CryptoCb_CleanupOcteon(&devId);
  1358. #endif
  1359. #endif
  1360. #ifdef WOLFSSL_ASYNC_CRYPT
  1361. wolfAsync_DevClose(&devId);
  1362. #endif
  1363. /* cleanup the thread if fixed point cache is enabled and have thread local */
  1364. #if defined(HAVE_THREAD_LS) && defined(HAVE_ECC) && defined(FP_ECC)
  1365. wc_ecc_fp_free();
  1366. #endif
  1367. if (args)
  1368. ((func_args*)args)->return_code = ret;
  1369. TEST_PASS("Test complete\n");
  1370. EXIT_TEST(ret);
  1371. }
  1372. #ifndef NO_MAIN_DRIVER
  1373. #ifdef HAVE_WOLFCRYPT_TEST_OPTIONS
  1374. int myoptind = 0;
  1375. char* myoptarg = NULL;
  1376. #endif
  1377. /* so overall tests can pull in test function */
  1378. #if defined(WOLFSSL_ESPIDF) || defined(_WIN32_WCE)
  1379. int wolf_test_task(void)
  1380. #else
  1381. #ifndef NO_MAIN_FUNCTION
  1382. int main(int argc, char** argv)
  1383. {
  1384. return wolfcrypt_test_main(argc, argv);
  1385. }
  1386. #endif
  1387. int wolfcrypt_test_main(int argc, char** argv)
  1388. #endif
  1389. {
  1390. int ret;
  1391. func_args args;
  1392. #if defined(WOLFSSL_ESPIDF) || defined(WOLFSSL_SE050)
  1393. /* set dummy wallclock time. */
  1394. struct timeval utctime;
  1395. struct timezone tz;
  1396. utctime.tv_sec = 1521725159; /* dummy time: 2018-03-22T13:25:59+00:00 */
  1397. utctime.tv_usec = 0;
  1398. tz.tz_minuteswest = 0;
  1399. tz.tz_dsttime = 0;
  1400. settimeofday(&utctime, &tz);
  1401. #endif
  1402. #ifdef WOLFSSL_APACHE_MYNEWT
  1403. #ifdef ARCH_sim
  1404. mcu_sim_parse_args(argc, argv);
  1405. #endif
  1406. sysinit();
  1407. /* set dummy wallclock time. */
  1408. struct os_timeval utctime;
  1409. struct os_timezone tz;
  1410. utctime.tv_sec = 1521725159; /* dummy time: 2018-03-22T13:25:59+00:00 */
  1411. utctime.tv_usec = 0;
  1412. tz.tz_minuteswest = 0;
  1413. tz.tz_dsttime = 0;
  1414. os_settimeofday(&utctime, &tz);
  1415. #endif
  1416. #ifdef WOLFSSL_ZEPHYR
  1417. /* set dummy wallclock time. */
  1418. struct timespec utctime;
  1419. utctime.tv_sec = 1521725159; /* dummy time: 2018-03-22T13:25:59+00:00 */
  1420. utctime.tv_nsec = 0;
  1421. clock_settime(CLOCK_REALTIME, &utctime);
  1422. #endif
  1423. #ifdef DEVKITPRO
  1424. void *framebuffer;
  1425. GXRModeObj *rmode = NULL;
  1426. VIDEO_Init();
  1427. WPAD_Init();
  1428. rmode = VIDEO_GetPreferredMode(NULL);
  1429. #pragma GCC diagnostic ignored "-Wbad-function-cast"
  1430. framebuffer = MEM_K0_TO_K1(SYS_AllocateFramebuffer(rmode));
  1431. #pragma GCC diagnostic pop
  1432. console_init(framebuffer,20,20,rmode->fbWidth,rmode->xfbHeight,rmode->fbWidth*VI_DISPLAY_PIX_SZ);
  1433. VIDEO_Configure(rmode);
  1434. VIDEO_SetNextFramebuffer(framebuffer);
  1435. VIDEO_SetBlack(FALSE);
  1436. VIDEO_Flush();
  1437. VIDEO_WaitVSync();
  1438. if(rmode->viTVMode&VI_NON_INTERLACE) VIDEO_WaitVSync();
  1439. #endif
  1440. #ifdef HAVE_WNR
  1441. if (wc_InitNetRandom(wnrConfigFile, NULL, 5000) != 0) {
  1442. err_sys("Whitewood netRandom global config failed", -1001);
  1443. return -1002;
  1444. }
  1445. #endif
  1446. #ifndef WOLFSSL_ESPIDF
  1447. args.argc = argc;
  1448. args.argv = argv;
  1449. #endif
  1450. if ((ret = wolfCrypt_Init()) != 0) {
  1451. printf("wolfCrypt_Init failed %d\n", ret);
  1452. err_sys("Error with wolfCrypt_Init!\n", -1003);
  1453. }
  1454. #ifdef WC_RNG_SEED_CB
  1455. wc_SetSeed_Cb(wc_GenerateSeed);
  1456. #endif
  1457. #ifdef HAVE_STACK_SIZE
  1458. StackSizeCheck(&args, wolfcrypt_test);
  1459. #else
  1460. wolfcrypt_test(&args);
  1461. #endif
  1462. if ((ret = wolfCrypt_Cleanup()) != 0) {
  1463. printf("wolfCrypt_Cleanup failed %d\n", ret);
  1464. err_sys("Error with wolfCrypt_Cleanup!\n", -1004);
  1465. }
  1466. #ifdef HAVE_WNR
  1467. if (wc_FreeNetRandom() < 0)
  1468. err_sys("Failed to free netRandom context", -1005);
  1469. #endif /* HAVE_WNR */
  1470. #ifdef DOLPHIN_EMULATOR
  1471. /* Returning from main panics the emulator. Just hang
  1472. * and let the user force quit the emulator window. */
  1473. printf("args.return_code: %d\n", args.return_code);
  1474. printf("Testing complete. You may close the window now\n");
  1475. while (1);
  1476. #endif
  1477. #ifndef WOLFSSL_ESPIDF
  1478. printf("Exiting main with return code: %d\n", args.return_code);
  1479. return args.return_code;
  1480. #endif
  1481. }
  1482. #endif /* NO_MAIN_DRIVER */
  1483. /* helper to save DER, convert to PEM and save PEM */
  1484. #if !defined(NO_ASN) && (defined(HAVE_ECC) || !defined(NO_DSA) || \
  1485. (!defined(NO_RSA) && (defined(WOLFSSL_KEY_GEN) || defined(WOLFSSL_CERT_GEN)))) \
  1486. && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  1487. #if !defined(NO_FILESYSTEM) && !defined(NO_WRITE_TEMP_FILES)
  1488. #define SaveDerAndPem(d, dSz, fD, fP, pT, eB) _SaveDerAndPem(d, dSz, fD, fP, pT, eB)
  1489. #else
  1490. #define SaveDerAndPem(d, dSz, fD, fP, pT, eB) _SaveDerAndPem(d, dSz, NULL, NULL, pT, eB)
  1491. #endif
  1492. static int _SaveDerAndPem(const byte* der, int derSz,
  1493. const char* fileDer, const char* filePem, int pemType, int errBase)
  1494. {
  1495. #if !defined(NO_FILESYSTEM) && !defined(NO_WRITE_TEMP_FILES)
  1496. int ret;
  1497. XFILE derFile;
  1498. derFile = XFOPEN(fileDer, "wb");
  1499. if (!derFile) {
  1500. return errBase + 0;
  1501. }
  1502. ret = (int)XFWRITE(der, 1, derSz, derFile);
  1503. XFCLOSE(derFile);
  1504. if (ret != derSz) {
  1505. return errBase + 1;
  1506. }
  1507. #endif
  1508. #ifdef WOLFSSL_DER_TO_PEM
  1509. if (filePem) {
  1510. #if !defined(NO_FILESYSTEM) && !defined(NO_WRITE_TEMP_FILES)
  1511. XFILE pemFile;
  1512. #endif
  1513. byte* pem;
  1514. int pemSz;
  1515. /* calculate PEM size */
  1516. pemSz = wc_DerToPem(der, derSz, NULL, 0, pemType);
  1517. if (pemSz < 0) {
  1518. return pemSz;
  1519. }
  1520. pem = (byte*)XMALLOC(pemSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  1521. if (pem == NULL) {
  1522. return MEMORY_E;
  1523. }
  1524. /* Convert to PEM */
  1525. pemSz = wc_DerToPem(der, derSz, pem, pemSz, pemType);
  1526. if (pemSz < 0) {
  1527. XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  1528. return errBase + 2;
  1529. }
  1530. #if !defined(NO_FILESYSTEM) && !defined(NO_WRITE_TEMP_FILES)
  1531. pemFile = XFOPEN(filePem, "wb");
  1532. if (!pemFile) {
  1533. XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  1534. return errBase + 3;
  1535. }
  1536. ret = (int)XFWRITE(pem, 1, pemSz, pemFile);
  1537. XFCLOSE(pemFile);
  1538. if (ret != pemSz) {
  1539. XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  1540. return errBase + 4;
  1541. }
  1542. #endif
  1543. XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  1544. }
  1545. #endif /* WOLFSSL_DER_TO_PEM */
  1546. /* suppress unused variable warnings */
  1547. (void)der;
  1548. (void)derSz;
  1549. (void)filePem;
  1550. (void)fileDer;
  1551. (void)pemType;
  1552. (void)errBase;
  1553. return 0;
  1554. }
  1555. #endif /* WOLFSSL_KEY_GEN || WOLFSSL_CERT_GEN */
  1556. WOLFSSL_TEST_SUBROUTINE int error_test(void)
  1557. {
  1558. const char* errStr;
  1559. char out[WOLFSSL_MAX_ERROR_SZ];
  1560. const char* unknownStr = wc_GetErrorString(0);
  1561. #ifdef NO_ERROR_STRINGS
  1562. /* Ensure a valid error code's string matches an invalid code's.
  1563. * The string is that error strings are not available.
  1564. */
  1565. errStr = wc_GetErrorString(OPEN_RAN_E);
  1566. wc_ErrorString(OPEN_RAN_E, out);
  1567. if (XSTRCMP(errStr, unknownStr) != 0)
  1568. return -1100;
  1569. if (XSTRCMP(out, unknownStr) != 0)
  1570. return -1101;
  1571. #else
  1572. int i;
  1573. int j = 0;
  1574. /* Values that are not or no longer error codes. */
  1575. int missing[] = { -122, -123, -124, -127, -128, -129, -159,
  1576. -163, -164, -165, -166, -167, -168, -169, -233,
  1577. 0 };
  1578. /* Check that all errors have a string and it's the same through the two
  1579. * APIs. Check that the values that are not errors map to the unknown
  1580. * string.
  1581. */
  1582. for (i = MAX_CODE_E-1; i >= WC_LAST_E; i--) {
  1583. errStr = wc_GetErrorString(i);
  1584. wc_ErrorString(i, out);
  1585. if (i != missing[j]) {
  1586. if (XSTRCMP(errStr, unknownStr) == 0)
  1587. return -1102;
  1588. if (XSTRCMP(out, unknownStr) == 0)
  1589. return -1103;
  1590. if (XSTRCMP(errStr, out) != 0)
  1591. return -1104;
  1592. if (XSTRLEN(errStr) >= WOLFSSL_MAX_ERROR_SZ)
  1593. return -1105;
  1594. }
  1595. else {
  1596. j++;
  1597. if (XSTRCMP(errStr, unknownStr) != 0)
  1598. return -1106;
  1599. if (XSTRCMP(out, unknownStr) != 0)
  1600. return -1107;
  1601. }
  1602. }
  1603. /* Check if the next possible value has been given a string. */
  1604. errStr = wc_GetErrorString(i);
  1605. wc_ErrorString(i, out);
  1606. if (XSTRCMP(errStr, unknownStr) != 0)
  1607. return -1108;
  1608. if (XSTRCMP(out, unknownStr) != 0)
  1609. return -1109;
  1610. #endif
  1611. return 0;
  1612. }
  1613. #ifndef NO_CODING
  1614. WOLFSSL_TEST_SUBROUTINE int base64_test(void)
  1615. {
  1616. int ret;
  1617. WOLFSSL_SMALL_STACK_STATIC const byte good[] = "A+Gd\0\0\0";
  1618. WOLFSSL_SMALL_STACK_STATIC const byte goodEnd[] = "A+Gd \r\n";
  1619. WOLFSSL_SMALL_STACK_STATIC const byte good_spaces[] = " A + G d \0";
  1620. byte out[128];
  1621. word32 outLen;
  1622. #ifdef WOLFSSL_BASE64_ENCODE
  1623. byte data[3];
  1624. word32 dataLen;
  1625. byte longData[79] = { 0 };
  1626. WOLFSSL_SMALL_STACK_STATIC const byte symbols[] = "+/A=";
  1627. #endif
  1628. WOLFSSL_SMALL_STACK_STATIC const byte badSmall[] = "AAA!Gdj=";
  1629. WOLFSSL_SMALL_STACK_STATIC const byte badLarge[] = "AAA~Gdj=";
  1630. WOLFSSL_SMALL_STACK_STATIC const byte badEOL[] = "A+Gd!AA";
  1631. WOLFSSL_SMALL_STACK_STATIC const byte badPadding[] = "AA=A";
  1632. WOLFSSL_SMALL_STACK_STATIC const byte badChar[] = ",-.:;<=>?@[\\]^_`";
  1633. byte goodChar[] =
  1634. "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  1635. "abcdefghijklmnopqrstuvwxyz"
  1636. "0123456789+/;";
  1637. byte charTest[] = "A+Gd\0\0\0";
  1638. int i;
  1639. /* Good Base64 encodings. */
  1640. outLen = sizeof(out);
  1641. ret = Base64_Decode(good, sizeof(good), out, &outLen);
  1642. if (ret != 0)
  1643. return -1200;
  1644. outLen = sizeof(out);
  1645. ret = Base64_Decode(goodEnd, sizeof(goodEnd), out, &outLen);
  1646. if (ret != 0)
  1647. return -1201;
  1648. outLen = sizeof(goodChar);
  1649. ret = Base64_Decode(goodChar, sizeof(goodChar), goodChar, &outLen);
  1650. if (ret != 0)
  1651. return -1235;
  1652. if (outLen != 64 / 4 * 3)
  1653. return -1236;
  1654. outLen = sizeof(out);
  1655. ret = Base64_Decode(good_spaces, sizeof(good_spaces), out, &outLen);
  1656. if (ret != 0)
  1657. return -1201;
  1658. /* Bad parameters. */
  1659. outLen = 1;
  1660. ret = Base64_Decode(good, sizeof(good), out, &outLen);
  1661. if (ret != BAD_FUNC_ARG)
  1662. return -1202;
  1663. outLen = sizeof(out);
  1664. ret = Base64_Decode(badEOL, sizeof(badEOL), out, &outLen);
  1665. if (ret != ASN_INPUT_E)
  1666. return -1203;
  1667. outLen = sizeof(out);
  1668. ret = Base64_Decode(badPadding, sizeof(badPadding), out, &outLen);
  1669. if (ret != ASN_INPUT_E)
  1670. return -1203;
  1671. /* Bad character at each offset 0-3. */
  1672. for (i = 0; i < 4; i++) {
  1673. outLen = sizeof(out);
  1674. ret = Base64_Decode(badSmall + i, 4, out, &outLen);
  1675. if (ret != ASN_INPUT_E)
  1676. return -1204 - i;
  1677. ret = Base64_Decode(badLarge + i, 4, out, &outLen);
  1678. if (ret != ASN_INPUT_E)
  1679. return -1214 - i;
  1680. }
  1681. /* Invalid character less than 0x2b */
  1682. for (i = 1; i < 0x2b; i++) {
  1683. outLen = sizeof(out);
  1684. charTest[0] = (byte)i;
  1685. ret = Base64_Decode(charTest, sizeof(charTest), out, &outLen);
  1686. if (ret != ASN_INPUT_E)
  1687. return -1240 - i;
  1688. }
  1689. /* Bad characters in range 0x2b - 0x7a. */
  1690. for (i = 0; i < (int)sizeof(badChar) - 1; i++) {
  1691. outLen = sizeof(out);
  1692. charTest[0] = badChar[i];
  1693. ret = Base64_Decode(charTest, sizeof(charTest), out, &outLen);
  1694. if (ret != ASN_INPUT_E)
  1695. return -1270 - i;
  1696. }
  1697. /* Invalid character greater than 0x7a */
  1698. for (i = 0x7b; i < 0x100; i++) {
  1699. outLen = sizeof(out);
  1700. charTest[0] = (byte)i;
  1701. ret = Base64_Decode(charTest, sizeof(charTest), out, &outLen);
  1702. if (ret != ASN_INPUT_E)
  1703. return -1290 - i;
  1704. }
  1705. #ifdef WOLFSSL_BASE64_ENCODE
  1706. /* Decode and encode all symbols - non-alphanumeric. */
  1707. dataLen = sizeof(data);
  1708. ret = Base64_Decode(symbols, sizeof(symbols), data, &dataLen);
  1709. if (ret != 0)
  1710. return -1224;
  1711. outLen = sizeof(out);
  1712. ret = Base64_Encode(data, dataLen, NULL, &outLen);
  1713. if (ret != LENGTH_ONLY_E)
  1714. return -1225;
  1715. outLen = sizeof(out);
  1716. ret = Base64_Encode(data, dataLen, out, &outLen);
  1717. if (ret != 0)
  1718. return -1226;
  1719. outLen = 7;
  1720. ret = Base64_EncodeEsc(data, dataLen, out, &outLen);
  1721. if (ret != BUFFER_E)
  1722. return -1227;
  1723. outLen = sizeof(out);
  1724. ret = Base64_EncodeEsc(data, dataLen, NULL, &outLen);
  1725. if (ret != LENGTH_ONLY_E)
  1726. return -1228;
  1727. outLen = sizeof(out);
  1728. ret = Base64_EncodeEsc(data, dataLen, out, &outLen);
  1729. if (ret != 0)
  1730. return -1229;
  1731. outLen = sizeof(out);
  1732. ret = Base64_Encode_NoNl(data, dataLen, out, &outLen);
  1733. if (ret != 0)
  1734. return -1230;
  1735. /* Data that results in an encoding longer than one line. */
  1736. outLen = sizeof(out);
  1737. dataLen = sizeof(longData);
  1738. ret = Base64_Encode(longData, dataLen, out, &outLen);
  1739. if (ret != 0)
  1740. return -1231;
  1741. outLen = sizeof(out);
  1742. ret = Base64_EncodeEsc(longData, dataLen, out, &outLen);
  1743. if (ret != 0)
  1744. return -1232;
  1745. outLen = sizeof(out);
  1746. ret = Base64_Encode_NoNl(longData, dataLen, out, &outLen);
  1747. if (ret != 0)
  1748. return -1233;
  1749. #endif
  1750. return 0;
  1751. }
  1752. #ifdef WOLFSSL_BASE16
  1753. WOLFSSL_TEST_SUBROUTINE int base16_test(void)
  1754. {
  1755. int ret;
  1756. WOLFSSL_SMALL_STACK_STATIC const byte testData[] = "SomeDataToEncode\n";
  1757. WOLFSSL_SMALL_STACK_STATIC const byte encodedTestData[] = "536F6D6544617461546F456E636F64650A00";
  1758. byte encoded[40];
  1759. word32 encodedLen;
  1760. byte plain[40];
  1761. word32 len;
  1762. /* length returned includes null termination */
  1763. encodedLen = sizeof(encoded);
  1764. ret = Base16_Encode(testData, sizeof(testData), encoded, &encodedLen);
  1765. if (ret != 0)
  1766. return -1300;
  1767. len = (word32)XSTRLEN((char*)encoded);
  1768. if (len != encodedLen - 1)
  1769. return -1301;
  1770. len = sizeof(plain);
  1771. ret = Base16_Decode(encoded, encodedLen - 1, plain, &len);
  1772. if (ret != 0)
  1773. return -1302;
  1774. if (len != sizeof(testData) || XMEMCMP(testData, plain, len) != 0)
  1775. return -1303;
  1776. if (encodedLen != sizeof(encodedTestData) ||
  1777. XMEMCMP(encoded, encodedTestData, encodedLen) != 0) {
  1778. return -1304;
  1779. }
  1780. return 0;
  1781. }
  1782. #endif /* WOLFSSL_BASE16 */
  1783. #endif /* !NO_CODING */
  1784. #ifndef NO_ASN
  1785. WOLFSSL_TEST_SUBROUTINE int asn_test(void)
  1786. {
  1787. int ret;
  1788. /* ASN1 encoded date buffer */
  1789. WOLFSSL_SMALL_STACK_STATIC const byte dateBuf[] = {0x17, 0x0d, 0x31, 0x36, 0x30, 0x38, 0x31, 0x31,
  1790. 0x32, 0x30, 0x30, 0x37, 0x33, 0x37, 0x5a};
  1791. byte format;
  1792. int length;
  1793. const byte* datePart;
  1794. #ifndef NO_ASN_TIME
  1795. struct tm timearg;
  1796. time_t now;
  1797. #endif
  1798. ret = wc_GetDateInfo(dateBuf, (int)sizeof(dateBuf), &datePart, &format,
  1799. &length);
  1800. if (ret != 0)
  1801. return -1400;
  1802. #ifndef NO_ASN_TIME
  1803. /* Parameter Validation tests. */
  1804. if (wc_GetTime(NULL, sizeof(now)) != BAD_FUNC_ARG)
  1805. return -1401;
  1806. if (wc_GetTime(&now, 0) != BUFFER_E)
  1807. return -1402;
  1808. now = 0;
  1809. if (wc_GetTime(&now, sizeof(now)) != 0) {
  1810. return -1403;
  1811. }
  1812. if (now == 0) {
  1813. printf("RTC/Time not set!\n");
  1814. return -1404;
  1815. }
  1816. ret = wc_GetDateAsCalendarTime(datePart, length, format, &timearg);
  1817. if (ret != 0)
  1818. return -1405;
  1819. #endif /* !NO_ASN_TIME */
  1820. return 0;
  1821. }
  1822. #endif /* !NO_ASN */
  1823. #ifdef WOLFSSL_MD2
  1824. WOLFSSL_TEST_SUBROUTINE int md2_test(void)
  1825. {
  1826. int ret = 0;
  1827. Md2 md2;
  1828. byte hash[MD2_DIGEST_SIZE];
  1829. testVector a, b, c, d, e, f, g;
  1830. testVector test_md2[7];
  1831. int times = sizeof(test_md2) / sizeof(testVector), i;
  1832. a.input = "";
  1833. a.output = "\x83\x50\xe5\xa3\xe2\x4c\x15\x3d\xf2\x27\x5c\x9f\x80\x69"
  1834. "\x27\x73";
  1835. a.inLen = XSTRLEN(a.input);
  1836. a.outLen = MD2_DIGEST_SIZE;
  1837. b.input = "a";
  1838. b.output = "\x32\xec\x01\xec\x4a\x6d\xac\x72\xc0\xab\x96\xfb\x34\xc0"
  1839. "\xb5\xd1";
  1840. b.inLen = XSTRLEN(b.input);
  1841. b.outLen = MD2_DIGEST_SIZE;
  1842. c.input = "abc";
  1843. c.output = "\xda\x85\x3b\x0d\x3f\x88\xd9\x9b\x30\x28\x3a\x69\xe6\xde"
  1844. "\xd6\xbb";
  1845. c.inLen = XSTRLEN(c.input);
  1846. c.outLen = MD2_DIGEST_SIZE;
  1847. d.input = "message digest";
  1848. d.output = "\xab\x4f\x49\x6b\xfb\x2a\x53\x0b\x21\x9f\xf3\x30\x31\xfe"
  1849. "\x06\xb0";
  1850. d.inLen = XSTRLEN(d.input);
  1851. d.outLen = MD2_DIGEST_SIZE;
  1852. e.input = "abcdefghijklmnopqrstuvwxyz";
  1853. e.output = "\x4e\x8d\xdf\xf3\x65\x02\x92\xab\x5a\x41\x08\xc3\xaa\x47"
  1854. "\x94\x0b";
  1855. e.inLen = XSTRLEN(e.input);
  1856. e.outLen = MD2_DIGEST_SIZE;
  1857. f.input = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345"
  1858. "6789";
  1859. f.output = "\xda\x33\xde\xf2\xa4\x2d\xf1\x39\x75\x35\x28\x46\xc3\x03"
  1860. "\x38\xcd";
  1861. f.inLen = XSTRLEN(f.input);
  1862. f.outLen = MD2_DIGEST_SIZE;
  1863. g.input = "1234567890123456789012345678901234567890123456789012345678"
  1864. "9012345678901234567890";
  1865. g.output = "\xd5\x97\x6f\x79\xd8\x3d\x3a\x0d\xc9\x80\x6c\x3c\x66\xf3"
  1866. "\xef\xd8";
  1867. g.inLen = XSTRLEN(g.input);
  1868. g.outLen = MD2_DIGEST_SIZE;
  1869. test_md2[0] = a;
  1870. test_md2[1] = b;
  1871. test_md2[2] = c;
  1872. test_md2[3] = d;
  1873. test_md2[4] = e;
  1874. test_md2[5] = f;
  1875. test_md2[6] = g;
  1876. wc_InitMd2(&md2);
  1877. for (i = 0; i < times; ++i) {
  1878. wc_Md2Update(&md2, (byte*)test_md2[i].input, (word32)test_md2[i].inLen);
  1879. wc_Md2Final(&md2, hash);
  1880. if (XMEMCMP(hash, test_md2[i].output, MD2_DIGEST_SIZE) != 0)
  1881. return -1500 - i;
  1882. }
  1883. for (i = 0; i < times; ++i) {
  1884. ret = wc_Md2Hash((byte*)test_md2[i].input, (word32)test_md2[i].inLen, hash);
  1885. if (ret != 0) {
  1886. return -1507 - i;
  1887. }
  1888. if (XMEMCMP(hash, test_md2[i].output, MD2_DIGEST_SIZE) != 0) {
  1889. return -1507 - i;
  1890. }
  1891. }
  1892. return 0;
  1893. }
  1894. #endif
  1895. #ifndef NO_MD5
  1896. WOLFSSL_TEST_SUBROUTINE int md5_test(void)
  1897. {
  1898. int ret = 0;
  1899. wc_Md5 md5, md5Copy;
  1900. byte hash[WC_MD5_DIGEST_SIZE];
  1901. byte hashcopy[WC_MD5_DIGEST_SIZE];
  1902. testVector a, b, c, d, e, f;
  1903. testVector test_md5[6];
  1904. int times = sizeof(test_md5) / sizeof(testVector), i;
  1905. a.input = "";
  1906. a.output = "\xd4\x1d\x8c\xd9\x8f\x00\xb2\x04\xe9\x80\x09\x98\xec\xf8\x42"
  1907. "\x7e";
  1908. a.inLen = XSTRLEN(a.input);
  1909. a.outLen = WC_MD5_DIGEST_SIZE;
  1910. b.input = "abc";
  1911. b.output = "\x90\x01\x50\x98\x3c\xd2\x4f\xb0\xd6\x96\x3f\x7d\x28\xe1\x7f"
  1912. "\x72";
  1913. b.inLen = XSTRLEN(b.input);
  1914. b.outLen = WC_MD5_DIGEST_SIZE;
  1915. c.input = "message digest";
  1916. c.output = "\xf9\x6b\x69\x7d\x7c\xb7\x93\x8d\x52\x5a\x2f\x31\xaa\xf1\x61"
  1917. "\xd0";
  1918. c.inLen = XSTRLEN(c.input);
  1919. c.outLen = WC_MD5_DIGEST_SIZE;
  1920. d.input = "abcdefghijklmnopqrstuvwxyz";
  1921. d.output = "\xc3\xfc\xd3\xd7\x61\x92\xe4\x00\x7d\xfb\x49\x6c\xca\x67\xe1"
  1922. "\x3b";
  1923. d.inLen = XSTRLEN(d.input);
  1924. d.outLen = WC_MD5_DIGEST_SIZE;
  1925. e.input = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345"
  1926. "6789";
  1927. e.output = "\xd1\x74\xab\x98\xd2\x77\xd9\xf5\xa5\x61\x1c\x2c\x9f\x41\x9d"
  1928. "\x9f";
  1929. e.inLen = XSTRLEN(e.input);
  1930. e.outLen = WC_MD5_DIGEST_SIZE;
  1931. f.input = "1234567890123456789012345678901234567890123456789012345678"
  1932. "9012345678901234567890";
  1933. f.output = "\x57\xed\xf4\xa2\x2b\xe3\xc9\x55\xac\x49\xda\x2e\x21\x07\xb6"
  1934. "\x7a";
  1935. f.inLen = XSTRLEN(f.input);
  1936. f.outLen = WC_MD5_DIGEST_SIZE;
  1937. test_md5[0] = a;
  1938. test_md5[1] = b;
  1939. test_md5[2] = c;
  1940. test_md5[3] = d;
  1941. test_md5[4] = e;
  1942. test_md5[5] = f;
  1943. ret = wc_InitMd5_ex(&md5, HEAP_HINT, devId);
  1944. if (ret != 0)
  1945. return -1600;
  1946. ret = wc_InitMd5_ex(&md5Copy, HEAP_HINT, devId);
  1947. if (ret != 0) {
  1948. wc_Md5Free(&md5);
  1949. return -1601;
  1950. }
  1951. for (i = 0; i < times; ++i) {
  1952. ret = wc_Md5Update(&md5, (byte*)test_md5[i].input,
  1953. (word32)test_md5[i].inLen);
  1954. if (ret != 0)
  1955. ERROR_OUT(-1602 - i, exit);
  1956. ret = wc_Md5GetHash(&md5, hashcopy);
  1957. if (ret != 0)
  1958. ERROR_OUT(-1603 - i, exit);
  1959. ret = wc_Md5Copy(&md5, &md5Copy);
  1960. if (ret != 0)
  1961. ERROR_OUT(-1604 - i, exit);
  1962. ret = wc_Md5Final(&md5, hash);
  1963. if (ret != 0)
  1964. ERROR_OUT(-1605 - i, exit);
  1965. wc_Md5Free(&md5Copy);
  1966. if (XMEMCMP(hash, test_md5[i].output, WC_MD5_DIGEST_SIZE) != 0)
  1967. ERROR_OUT(-1606 - i, exit);
  1968. if (XMEMCMP(hash, hashcopy, WC_MD5_DIGEST_SIZE) != 0)
  1969. ERROR_OUT(-1607 - i, exit);
  1970. }
  1971. #ifndef NO_LARGE_HASH_TEST
  1972. /* BEGIN LARGE HASH TEST */ {
  1973. byte large_input[1024];
  1974. const char* large_digest =
  1975. "\x44\xd0\x88\xce\xf1\x36\xd1\x78\xe9\xc8\xba\x84\xc3\xfd\xf6\xca";
  1976. for (i = 0; i < (int)sizeof(large_input); i++) {
  1977. large_input[i] = (byte)(i & 0xFF);
  1978. }
  1979. times = 100;
  1980. #ifdef WOLFSSL_PIC32MZ_HASH
  1981. wc_Md5SizeSet(&md5, times * sizeof(large_input));
  1982. #endif
  1983. for (i = 0; i < times; ++i) {
  1984. ret = wc_Md5Update(&md5, (byte*)large_input,
  1985. (word32)sizeof(large_input));
  1986. if (ret != 0)
  1987. ERROR_OUT(-1608, exit);
  1988. }
  1989. ret = wc_Md5Final(&md5, hash);
  1990. if (ret != 0)
  1991. ERROR_OUT(-1609, exit);
  1992. if (XMEMCMP(hash, large_digest, WC_MD5_DIGEST_SIZE) != 0)
  1993. ERROR_OUT(-1610, exit);
  1994. } /* END LARGE HASH TEST */
  1995. #endif /* NO_LARGE_HASH_TEST */
  1996. exit:
  1997. wc_Md5Free(&md5);
  1998. wc_Md5Free(&md5Copy);
  1999. return ret;
  2000. }
  2001. #endif /* NO_MD5 */
  2002. #ifndef NO_MD4
  2003. WOLFSSL_TEST_SUBROUTINE int md4_test(void)
  2004. {
  2005. Md4 md4;
  2006. byte hash[MD4_DIGEST_SIZE];
  2007. testVector a, b, c, d, e, f, g;
  2008. testVector test_md4[7];
  2009. int times = sizeof(test_md4) / sizeof(testVector), i;
  2010. a.input = "";
  2011. a.output = "\x31\xd6\xcf\xe0\xd1\x6a\xe9\x31\xb7\x3c\x59\xd7\xe0\xc0\x89"
  2012. "\xc0";
  2013. a.inLen = XSTRLEN(a.input);
  2014. a.outLen = MD4_DIGEST_SIZE;
  2015. b.input = "a";
  2016. b.output = "\xbd\xe5\x2c\xb3\x1d\xe3\x3e\x46\x24\x5e\x05\xfb\xdb\xd6\xfb"
  2017. "\x24";
  2018. b.inLen = XSTRLEN(b.input);
  2019. b.outLen = MD4_DIGEST_SIZE;
  2020. c.input = "abc";
  2021. c.output = "\xa4\x48\x01\x7a\xaf\x21\xd8\x52\x5f\xc1\x0a\xe8\x7a\xa6\x72"
  2022. "\x9d";
  2023. c.inLen = XSTRLEN(c.input);
  2024. c.outLen = MD4_DIGEST_SIZE;
  2025. d.input = "message digest";
  2026. d.output = "\xd9\x13\x0a\x81\x64\x54\x9f\xe8\x18\x87\x48\x06\xe1\xc7\x01"
  2027. "\x4b";
  2028. d.inLen = XSTRLEN(d.input);
  2029. d.outLen = MD4_DIGEST_SIZE;
  2030. e.input = "abcdefghijklmnopqrstuvwxyz";
  2031. e.output = "\xd7\x9e\x1c\x30\x8a\xa5\xbb\xcd\xee\xa8\xed\x63\xdf\x41\x2d"
  2032. "\xa9";
  2033. e.inLen = XSTRLEN(e.input);
  2034. e.outLen = MD4_DIGEST_SIZE;
  2035. f.input = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345"
  2036. "6789";
  2037. f.output = "\x04\x3f\x85\x82\xf2\x41\xdb\x35\x1c\xe6\x27\xe1\x53\xe7\xf0"
  2038. "\xe4";
  2039. f.inLen = XSTRLEN(f.input);
  2040. f.outLen = MD4_DIGEST_SIZE;
  2041. g.input = "1234567890123456789012345678901234567890123456789012345678"
  2042. "9012345678901234567890";
  2043. g.output = "\xe3\x3b\x4d\xdc\x9c\x38\xf2\x19\x9c\x3e\x7b\x16\x4f\xcc\x05"
  2044. "\x36";
  2045. g.inLen = XSTRLEN(g.input);
  2046. g.outLen = MD4_DIGEST_SIZE;
  2047. test_md4[0] = a;
  2048. test_md4[1] = b;
  2049. test_md4[2] = c;
  2050. test_md4[3] = d;
  2051. test_md4[4] = e;
  2052. test_md4[5] = f;
  2053. test_md4[6] = g;
  2054. wc_InitMd4(&md4);
  2055. for (i = 0; i < times; ++i) {
  2056. wc_Md4Update(&md4, (byte*)test_md4[i].input, (word32)test_md4[i].inLen);
  2057. wc_Md4Final(&md4, hash);
  2058. if (XMEMCMP(hash, test_md4[i].output, MD4_DIGEST_SIZE) != 0)
  2059. return -1700 - i;
  2060. }
  2061. return 0;
  2062. }
  2063. #endif /* NO_MD4 */
  2064. #ifndef NO_SHA
  2065. WOLFSSL_TEST_SUBROUTINE int sha_test(void)
  2066. {
  2067. int ret = 0;
  2068. wc_Sha sha, shaCopy;
  2069. byte hash[WC_SHA_DIGEST_SIZE];
  2070. byte hashcopy[WC_SHA_DIGEST_SIZE];
  2071. testVector a, b, c, d, e;
  2072. testVector test_sha[5];
  2073. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2074. a.input = "";
  2075. a.output = "\xda\x39\xa3\xee\x5e\x6b\x4b\x0d\x32\x55\xbf\xef\x95\x60\x18"
  2076. "\x90\xaf\xd8\x07\x09";
  2077. a.inLen = XSTRLEN(a.input);
  2078. a.outLen = WC_SHA_DIGEST_SIZE;
  2079. b.input = "abc";
  2080. b.output = "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E\x25\x71\x78\x50\xC2"
  2081. "\x6C\x9C\xD0\xD8\x9D";
  2082. b.inLen = XSTRLEN(b.input);
  2083. b.outLen = WC_SHA_DIGEST_SIZE;
  2084. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  2085. c.output = "\x84\x98\x3E\x44\x1C\x3B\xD2\x6E\xBA\xAE\x4A\xA1\xF9\x51\x29"
  2086. "\xE5\xE5\x46\x70\xF1";
  2087. c.inLen = XSTRLEN(c.input);
  2088. c.outLen = WC_SHA_DIGEST_SIZE;
  2089. d.input = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  2090. "aaaaaa";
  2091. d.output = "\x00\x98\xBA\x82\x4B\x5C\x16\x42\x7B\xD7\xA1\x12\x2A\x5A\x44"
  2092. "\x2A\x25\xEC\x64\x4D";
  2093. d.inLen = XSTRLEN(d.input);
  2094. d.outLen = WC_SHA_DIGEST_SIZE;
  2095. e.input = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  2096. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  2097. "aaaaaaaaaa";
  2098. e.output = "\xAD\x5B\x3F\xDB\xCB\x52\x67\x78\xC2\x83\x9D\x2F\x15\x1E\xA7"
  2099. "\x53\x99\x5E\x26\xA0";
  2100. e.inLen = XSTRLEN(e.input);
  2101. e.outLen = WC_SHA_DIGEST_SIZE;
  2102. test_sha[0] = a;
  2103. test_sha[1] = b;
  2104. test_sha[2] = c;
  2105. test_sha[3] = d;
  2106. test_sha[4] = e;
  2107. ret = wc_InitSha_ex(&sha, HEAP_HINT, devId);
  2108. if (ret != 0)
  2109. return -1800;
  2110. ret = wc_InitSha_ex(&shaCopy, HEAP_HINT, devId);
  2111. if (ret != 0) {
  2112. wc_ShaFree(&sha);
  2113. return -1801;
  2114. }
  2115. for (i = 0; i < times; ++i) {
  2116. ret = wc_ShaUpdate(&sha, (byte*)test_sha[i].input,
  2117. (word32)test_sha[i].inLen);
  2118. if (ret != 0)
  2119. ERROR_OUT(-1802 - i, exit);
  2120. ret = wc_ShaGetHash(&sha, hashcopy);
  2121. if (ret != 0)
  2122. ERROR_OUT(-1803 - i, exit);
  2123. ret = wc_ShaCopy(&sha, &shaCopy);
  2124. if (ret != 0)
  2125. ERROR_OUT(-1804 - i, exit);
  2126. ret = wc_ShaFinal(&sha, hash);
  2127. if (ret != 0)
  2128. ERROR_OUT(-1805 - i, exit);
  2129. wc_ShaFree(&shaCopy);
  2130. if (XMEMCMP(hash, test_sha[i].output, WC_SHA_DIGEST_SIZE) != 0)
  2131. ERROR_OUT(-1806 - i, exit);
  2132. if (XMEMCMP(hash, hashcopy, WC_SHA_DIGEST_SIZE) != 0)
  2133. ERROR_OUT(-1807 - i, exit);
  2134. }
  2135. #ifndef NO_LARGE_HASH_TEST
  2136. /* BEGIN LARGE HASH TEST */ {
  2137. byte large_input[1024];
  2138. #if defined(WOLFSSL_RENESAS_TSIP) || defined(WOLFSSL_RENESAS_SCEPROTECT) || \
  2139. defined(HASH_SIZE_LIMIT)
  2140. const char* large_digest =
  2141. "\x1d\x6a\x5a\xf6\xe5\x7c\x86\xce\x7f\x7c\xaf\xd5\xdb\x08\xcd\x59"
  2142. "\x15\x8c\x6d\xb6";
  2143. #else
  2144. const char* large_digest =
  2145. "\x8b\x77\x02\x48\x39\xe8\xdb\xd3\x9a\xf4\x05\x24\x66\x12\x2d\x9e"
  2146. "\xc5\xd9\x0a\xac";
  2147. #endif
  2148. for (i = 0; i < (int)sizeof(large_input); i++) {
  2149. large_input[i] = (byte)(i & 0xFF);
  2150. }
  2151. #if defined(WOLFSSL_RENESAS_TSIP) || defined(WOLFSSL_RENESAS_SCEPROTECT) || \
  2152. defined(HASH_SIZE_LIMIT)
  2153. times = 20;
  2154. #else
  2155. times = 100;
  2156. #endif
  2157. #ifdef WOLFSSL_PIC32MZ_HASH
  2158. wc_ShaSizeSet(&sha, times * sizeof(large_input));
  2159. #endif
  2160. for (i = 0; i < times; ++i) {
  2161. ret = wc_ShaUpdate(&sha, (byte*)large_input,
  2162. (word32)sizeof(large_input));
  2163. if (ret != 0)
  2164. ERROR_OUT(-1808, exit);
  2165. }
  2166. ret = wc_ShaFinal(&sha, hash);
  2167. if (ret != 0)
  2168. ERROR_OUT(-1809, exit);
  2169. if (XMEMCMP(hash, large_digest, WC_SHA_DIGEST_SIZE) != 0)
  2170. ERROR_OUT(-1810, exit);
  2171. } /* END LARGE HASH TEST */
  2172. #endif /* NO_LARGE_HASH_TEST */
  2173. exit:
  2174. wc_ShaFree(&sha);
  2175. wc_ShaFree(&shaCopy);
  2176. return ret;
  2177. }
  2178. #endif /* NO_SHA */
  2179. #ifdef WOLFSSL_RIPEMD
  2180. WOLFSSL_TEST_SUBROUTINE int ripemd_test(void)
  2181. {
  2182. RipeMd ripemd;
  2183. int ret;
  2184. byte hash[RIPEMD_DIGEST_SIZE];
  2185. testVector a, b, c, d;
  2186. testVector test_ripemd[4];
  2187. int times = sizeof(test_ripemd) / sizeof(struct testVector), i;
  2188. a.input = "abc";
  2189. a.output = "\x8e\xb2\x08\xf7\xe0\x5d\x98\x7a\x9b\x04\x4a\x8e\x98\xc6"
  2190. "\xb0\x87\xf1\x5a\x0b\xfc";
  2191. a.inLen = XSTRLEN(a.input);
  2192. a.outLen = RIPEMD_DIGEST_SIZE;
  2193. b.input = "message digest";
  2194. b.output = "\x5d\x06\x89\xef\x49\xd2\xfa\xe5\x72\xb8\x81\xb1\x23\xa8"
  2195. "\x5f\xfa\x21\x59\x5f\x36";
  2196. b.inLen = XSTRLEN(b.input);
  2197. b.outLen = RIPEMD_DIGEST_SIZE;
  2198. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  2199. c.output = "\x12\xa0\x53\x38\x4a\x9c\x0c\x88\xe4\x05\xa0\x6c\x27\xdc"
  2200. "\xf4\x9a\xda\x62\xeb\x2b";
  2201. c.inLen = XSTRLEN(c.input);
  2202. c.outLen = RIPEMD_DIGEST_SIZE;
  2203. d.input = "12345678901234567890123456789012345678901234567890123456"
  2204. "789012345678901234567890";
  2205. d.output = "\x9b\x75\x2e\x45\x57\x3d\x4b\x39\xf4\xdb\xd3\x32\x3c\xab"
  2206. "\x82\xbf\x63\x32\x6b\xfb";
  2207. d.inLen = XSTRLEN(d.input);
  2208. d.outLen = RIPEMD_DIGEST_SIZE;
  2209. test_ripemd[0] = a;
  2210. test_ripemd[1] = b;
  2211. test_ripemd[2] = c;
  2212. test_ripemd[3] = d;
  2213. ret = wc_InitRipeMd(&ripemd);
  2214. if (ret != 0) {
  2215. return -1900;
  2216. }
  2217. for (i = 0; i < times; ++i) {
  2218. ret = wc_RipeMdUpdate(&ripemd, (byte*)test_ripemd[i].input,
  2219. (word32)test_ripemd[i].inLen);
  2220. if (ret != 0) {
  2221. return -1901 - i;
  2222. }
  2223. ret = wc_RipeMdFinal(&ripemd, hash);
  2224. if (ret != 0) {
  2225. return -1911 - i;
  2226. }
  2227. if (XMEMCMP(hash, test_ripemd[i].output, RIPEMD_DIGEST_SIZE) != 0)
  2228. return -1921 - i;
  2229. }
  2230. return 0;
  2231. }
  2232. #endif /* WOLFSSL_RIPEMD */
  2233. #ifdef HAVE_BLAKE2
  2234. #define BLAKE2B_TESTS 3
  2235. static const byte blake2b_vec[BLAKE2B_TESTS][BLAKE2B_OUTBYTES] =
  2236. {
  2237. {
  2238. 0x78, 0x6A, 0x02, 0xF7, 0x42, 0x01, 0x59, 0x03,
  2239. 0xC6, 0xC6, 0xFD, 0x85, 0x25, 0x52, 0xD2, 0x72,
  2240. 0x91, 0x2F, 0x47, 0x40, 0xE1, 0x58, 0x47, 0x61,
  2241. 0x8A, 0x86, 0xE2, 0x17, 0xF7, 0x1F, 0x54, 0x19,
  2242. 0xD2, 0x5E, 0x10, 0x31, 0xAF, 0xEE, 0x58, 0x53,
  2243. 0x13, 0x89, 0x64, 0x44, 0x93, 0x4E, 0xB0, 0x4B,
  2244. 0x90, 0x3A, 0x68, 0x5B, 0x14, 0x48, 0xB7, 0x55,
  2245. 0xD5, 0x6F, 0x70, 0x1A, 0xFE, 0x9B, 0xE2, 0xCE
  2246. },
  2247. {
  2248. 0x2F, 0xA3, 0xF6, 0x86, 0xDF, 0x87, 0x69, 0x95,
  2249. 0x16, 0x7E, 0x7C, 0x2E, 0x5D, 0x74, 0xC4, 0xC7,
  2250. 0xB6, 0xE4, 0x8F, 0x80, 0x68, 0xFE, 0x0E, 0x44,
  2251. 0x20, 0x83, 0x44, 0xD4, 0x80, 0xF7, 0x90, 0x4C,
  2252. 0x36, 0x96, 0x3E, 0x44, 0x11, 0x5F, 0xE3, 0xEB,
  2253. 0x2A, 0x3A, 0xC8, 0x69, 0x4C, 0x28, 0xBC, 0xB4,
  2254. 0xF5, 0xA0, 0xF3, 0x27, 0x6F, 0x2E, 0x79, 0x48,
  2255. 0x7D, 0x82, 0x19, 0x05, 0x7A, 0x50, 0x6E, 0x4B
  2256. },
  2257. {
  2258. 0x1C, 0x08, 0x79, 0x8D, 0xC6, 0x41, 0xAB, 0xA9,
  2259. 0xDE, 0xE4, 0x35, 0xE2, 0x25, 0x19, 0xA4, 0x72,
  2260. 0x9A, 0x09, 0xB2, 0xBF, 0xE0, 0xFF, 0x00, 0xEF,
  2261. 0x2D, 0xCD, 0x8E, 0xD6, 0xF8, 0xA0, 0x7D, 0x15,
  2262. 0xEA, 0xF4, 0xAE, 0xE5, 0x2B, 0xBF, 0x18, 0xAB,
  2263. 0x56, 0x08, 0xA6, 0x19, 0x0F, 0x70, 0xB9, 0x04,
  2264. 0x86, 0xC8, 0xA7, 0xD4, 0x87, 0x37, 0x10, 0xB1,
  2265. 0x11, 0x5D, 0x3D, 0xEB, 0xBB, 0x43, 0x27, 0xB5
  2266. }
  2267. };
  2268. WOLFSSL_TEST_SUBROUTINE int blake2b_test(void)
  2269. {
  2270. Blake2b b2b;
  2271. byte digest[64];
  2272. byte input[64];
  2273. int i, ret;
  2274. for (i = 0; i < (int)sizeof(input); i++)
  2275. input[i] = (byte)i;
  2276. for (i = 0; i < BLAKE2B_TESTS; i++) {
  2277. ret = wc_InitBlake2b(&b2b, 64);
  2278. if (ret != 0)
  2279. return -2000 - i;
  2280. ret = wc_Blake2bUpdate(&b2b, input, i);
  2281. if (ret != 0)
  2282. return -2010 - 1;
  2283. ret = wc_Blake2bFinal(&b2b, digest, 64);
  2284. if (ret != 0)
  2285. return -2020 - i;
  2286. if (XMEMCMP(digest, blake2b_vec[i], 64) != 0) {
  2287. return -2030 - i;
  2288. }
  2289. }
  2290. return 0;
  2291. }
  2292. #endif /* HAVE_BLAKE2 */
  2293. #ifdef HAVE_BLAKE2S
  2294. #define BLAKE2S_TESTS 3
  2295. static const byte blake2s_vec[BLAKE2S_TESTS][BLAKE2S_OUTBYTES] =
  2296. {
  2297. {
  2298. 0x69, 0x21, 0x7a, 0x30, 0x79, 0x90, 0x80, 0x94,
  2299. 0xe1, 0x11, 0x21, 0xd0, 0x42, 0x35, 0x4a, 0x7c,
  2300. 0x1f, 0x55, 0xb6, 0x48, 0x2c, 0xa1, 0xa5, 0x1e,
  2301. 0x1b, 0x25, 0x0d, 0xfd, 0x1e, 0xd0, 0xee, 0xf9,
  2302. },
  2303. {
  2304. 0xe3, 0x4d, 0x74, 0xdb, 0xaf, 0x4f, 0xf4, 0xc6,
  2305. 0xab, 0xd8, 0x71, 0xcc, 0x22, 0x04, 0x51, 0xd2,
  2306. 0xea, 0x26, 0x48, 0x84, 0x6c, 0x77, 0x57, 0xfb,
  2307. 0xaa, 0xc8, 0x2f, 0xe5, 0x1a, 0xd6, 0x4b, 0xea,
  2308. },
  2309. {
  2310. 0xdd, 0xad, 0x9a, 0xb1, 0x5d, 0xac, 0x45, 0x49,
  2311. 0xba, 0x42, 0xf4, 0x9d, 0x26, 0x24, 0x96, 0xbe,
  2312. 0xf6, 0xc0, 0xba, 0xe1, 0xdd, 0x34, 0x2a, 0x88,
  2313. 0x08, 0xf8, 0xea, 0x26, 0x7c, 0x6e, 0x21, 0x0c,
  2314. }
  2315. };
  2316. WOLFSSL_TEST_SUBROUTINE int blake2s_test(void)
  2317. {
  2318. Blake2s b2s;
  2319. byte digest[32];
  2320. byte input[64];
  2321. int i, ret;
  2322. for (i = 0; i < (int)sizeof(input); i++)
  2323. input[i] = (byte)i;
  2324. for (i = 0; i < BLAKE2S_TESTS; i++) {
  2325. ret = wc_InitBlake2s(&b2s, 32);
  2326. if (ret != 0)
  2327. return -2100 - i;
  2328. ret = wc_Blake2sUpdate(&b2s, input, i);
  2329. if (ret != 0)
  2330. return -2110 - 1;
  2331. ret = wc_Blake2sFinal(&b2s, digest, 32);
  2332. if (ret != 0)
  2333. return -2120 - i;
  2334. if (XMEMCMP(digest, blake2s_vec[i], 32) != 0) {
  2335. return -2130 - i;
  2336. }
  2337. }
  2338. return 0;
  2339. }
  2340. #endif /* HAVE_BLAKE2S */
  2341. #ifdef WOLFSSL_SHA224
  2342. WOLFSSL_TEST_SUBROUTINE int sha224_test(void)
  2343. {
  2344. wc_Sha224 sha, shaCopy;
  2345. byte hash[WC_SHA224_DIGEST_SIZE];
  2346. byte hashcopy[WC_SHA224_DIGEST_SIZE];
  2347. int ret = 0;
  2348. testVector a, b, c;
  2349. testVector test_sha[3];
  2350. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2351. a.input = "";
  2352. a.output = "\xd1\x4a\x02\x8c\x2a\x3a\x2b\xc9\x47\x61\x02\xbb\x28\x82\x34"
  2353. "\xc4\x15\xa2\xb0\x1f\x82\x8e\xa6\x2a\xc5\xb3\xe4\x2f";
  2354. a.inLen = XSTRLEN(a.input);
  2355. a.outLen = WC_SHA224_DIGEST_SIZE;
  2356. b.input = "abc";
  2357. b.output = "\x23\x09\x7d\x22\x34\x05\xd8\x22\x86\x42\xa4\x77\xbd\xa2\x55"
  2358. "\xb3\x2a\xad\xbc\xe4\xbd\xa0\xb3\xf7\xe3\x6c\x9d\xa7";
  2359. b.inLen = XSTRLEN(b.input);
  2360. b.outLen = WC_SHA224_DIGEST_SIZE;
  2361. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  2362. c.output = "\x75\x38\x8b\x16\x51\x27\x76\xcc\x5d\xba\x5d\xa1\xfd\x89\x01"
  2363. "\x50\xb0\xc6\x45\x5c\xb4\xf5\x8b\x19\x52\x52\x25\x25";
  2364. c.inLen = XSTRLEN(c.input);
  2365. c.outLen = WC_SHA224_DIGEST_SIZE;
  2366. test_sha[0] = a;
  2367. test_sha[1] = b;
  2368. test_sha[2] = c;
  2369. ret = wc_InitSha224_ex(&sha, HEAP_HINT, devId);
  2370. if (ret != 0)
  2371. return -2200;
  2372. ret = wc_InitSha224_ex(&shaCopy, HEAP_HINT, devId);
  2373. if (ret != 0) {
  2374. wc_Sha224Free(&sha);
  2375. return -2201;
  2376. }
  2377. for (i = 0; i < times; ++i) {
  2378. ret = wc_Sha224Update(&sha, (byte*)test_sha[i].input,
  2379. (word32)test_sha[i].inLen);
  2380. if (ret != 0)
  2381. ERROR_OUT(-2202 - i, exit);
  2382. ret = wc_Sha224GetHash(&sha, hashcopy);
  2383. if (ret != 0)
  2384. ERROR_OUT(-2203 - i, exit);
  2385. ret = wc_Sha224Copy(&sha, &shaCopy);
  2386. if (ret != 0)
  2387. ERROR_OUT(-2204 - i, exit);
  2388. ret = wc_Sha224Final(&sha, hash);
  2389. if (ret != 0)
  2390. ERROR_OUT(-2205 - i, exit);
  2391. wc_Sha224Free(&shaCopy);
  2392. if (XMEMCMP(hash, test_sha[i].output, WC_SHA224_DIGEST_SIZE) != 0)
  2393. ERROR_OUT(-2206 - i, exit);
  2394. if (XMEMCMP(hash, hashcopy, WC_SHA224_DIGEST_SIZE) != 0)
  2395. ERROR_OUT(-2207 - i, exit);
  2396. }
  2397. exit:
  2398. wc_Sha224Free(&sha);
  2399. wc_Sha224Free(&shaCopy);
  2400. return ret;
  2401. }
  2402. #endif
  2403. #ifndef NO_SHA256
  2404. WOLFSSL_TEST_SUBROUTINE int sha256_test(void)
  2405. {
  2406. wc_Sha256 sha, shaCopy;
  2407. byte hash[WC_SHA256_DIGEST_SIZE];
  2408. byte hashcopy[WC_SHA256_DIGEST_SIZE];
  2409. int ret = 0;
  2410. testVector a, b, c;
  2411. testVector test_sha[3];
  2412. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2413. a.input = "";
  2414. a.output = "\xe3\xb0\xc4\x42\x98\xfc\x1c\x14\x9a\xfb\xf4\xc8\x99\x6f\xb9"
  2415. "\x24\x27\xae\x41\xe4\x64\x9b\x93\x4c\xa4\x95\x99\x1b\x78\x52"
  2416. "\xb8\x55";
  2417. a.inLen = XSTRLEN(a.input);
  2418. a.outLen = WC_SHA256_DIGEST_SIZE;
  2419. b.input = "abc";
  2420. b.output = "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  2421. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  2422. "\x15\xAD";
  2423. b.inLen = XSTRLEN(b.input);
  2424. b.outLen = WC_SHA256_DIGEST_SIZE;
  2425. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  2426. c.output = "\x24\x8D\x6A\x61\xD2\x06\x38\xB8\xE5\xC0\x26\x93\x0C\x3E\x60"
  2427. "\x39\xA3\x3C\xE4\x59\x64\xFF\x21\x67\xF6\xEC\xED\xD4\x19\xDB"
  2428. "\x06\xC1";
  2429. c.inLen = XSTRLEN(c.input);
  2430. c.outLen = WC_SHA256_DIGEST_SIZE;
  2431. test_sha[0] = a;
  2432. test_sha[1] = b;
  2433. test_sha[2] = c;
  2434. ret = wc_InitSha256_ex(&sha, HEAP_HINT, devId);
  2435. if (ret != 0)
  2436. return -2300;
  2437. ret = wc_InitSha256_ex(&shaCopy, HEAP_HINT, devId);
  2438. if (ret != 0) {
  2439. wc_Sha256Free(&sha);
  2440. return -2301;
  2441. }
  2442. for (i = 0; i < times; ++i) {
  2443. ret = wc_Sha256Update(&sha, (byte*)test_sha[i].input,
  2444. (word32)test_sha[i].inLen);
  2445. if (ret != 0) {
  2446. ERROR_OUT(-2302 - i, exit);
  2447. }
  2448. ret = wc_Sha256GetHash(&sha, hashcopy);
  2449. if (ret != 0)
  2450. ERROR_OUT(-2303 - i, exit);
  2451. ret = wc_Sha256Copy(&sha, &shaCopy);
  2452. if (ret != 0)
  2453. ERROR_OUT(-2304 - i, exit);
  2454. ret = wc_Sha256Final(&sha, hash);
  2455. if (ret != 0)
  2456. ERROR_OUT(-2305 - i, exit);
  2457. wc_Sha256Free(&shaCopy);
  2458. if (XMEMCMP(hash, test_sha[i].output, WC_SHA256_DIGEST_SIZE) != 0)
  2459. ERROR_OUT(-2306 - i, exit);
  2460. if (XMEMCMP(hash, hashcopy, WC_SHA256_DIGEST_SIZE) != 0)
  2461. ERROR_OUT(-2307 - i, exit);
  2462. }
  2463. #ifndef NO_LARGE_HASH_TEST
  2464. /* BEGIN LARGE HASH TEST */ {
  2465. byte large_input[1024];
  2466. #ifdef HASH_SIZE_LIMIT
  2467. const char* large_digest =
  2468. "\xa4\x75\x9e\x7a\xa2\x03\x38\x32\x88\x66\xa2\xea\x17\xea\xf8\xc7"
  2469. "\xfe\x4e\xc6\xbb\xe3\xbb\x71\xce\xe7\xdf\x7c\x04\x61\xb3\xc2\x2f";
  2470. #else
  2471. const char* large_digest =
  2472. "\x27\x78\x3e\x87\x96\x3a\x4e\xfb\x68\x29\xb5\x31\xc9\xba\x57\xb4"
  2473. "\x4f\x45\x79\x7f\x67\x70\xbd\x63\x7f\xbf\x0d\x80\x7c\xbd\xba\xe0";
  2474. #endif
  2475. for (i = 0; i < (int)sizeof(large_input); i++) {
  2476. large_input[i] = (byte)(i & 0xFF);
  2477. }
  2478. #ifdef HASH_SIZE_LIMIT
  2479. times = 20;
  2480. #else
  2481. times = 100;
  2482. #endif
  2483. #ifdef WOLFSSL_PIC32MZ_HASH
  2484. wc_Sha256SizeSet(&sha, times * sizeof(large_input));
  2485. #endif
  2486. for (i = 0; i < times; ++i) {
  2487. ret = wc_Sha256Update(&sha, (byte*)large_input,
  2488. (word32)sizeof(large_input));
  2489. if (ret != 0)
  2490. ERROR_OUT(-2308, exit);
  2491. }
  2492. ret = wc_Sha256Final(&sha, hash);
  2493. if (ret != 0)
  2494. ERROR_OUT(-2309, exit);
  2495. if (XMEMCMP(hash, large_digest, WC_SHA256_DIGEST_SIZE) != 0)
  2496. ERROR_OUT(-2310, exit);
  2497. } /* END LARGE HASH TEST */
  2498. #endif /* NO_LARGE_HASH_TEST */
  2499. exit:
  2500. wc_Sha256Free(&sha);
  2501. wc_Sha256Free(&shaCopy);
  2502. return ret;
  2503. }
  2504. #endif
  2505. #ifdef WOLFSSL_SHA512
  2506. WOLFSSL_TEST_SUBROUTINE int sha512_test(void)
  2507. {
  2508. wc_Sha512 sha, shaCopy;
  2509. byte hash[WC_SHA512_DIGEST_SIZE];
  2510. byte hashcopy[WC_SHA512_DIGEST_SIZE];
  2511. int ret = 0;
  2512. testVector a, b, c;
  2513. testVector test_sha[3];
  2514. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2515. a.input = "";
  2516. a.output = "\xcf\x83\xe1\x35\x7e\xef\xb8\xbd\xf1\x54\x28\x50\xd6\x6d\x80"
  2517. "\x07\xd6\x20\xe4\x05\x0b\x57\x15\xdc\x83\xf4\xa9\x21\xd3\x6c"
  2518. "\xe9\xce\x47\xd0\xd1\x3c\x5d\x85\xf2\xb0\xff\x83\x18\xd2\x87"
  2519. "\x7e\xec\x2f\x63\xb9\x31\xbd\x47\x41\x7a\x81\xa5\x38\x32\x7a"
  2520. "\xf9\x27\xda\x3e";
  2521. a.inLen = XSTRLEN(a.input);
  2522. a.outLen = WC_SHA512_DIGEST_SIZE;
  2523. b.input = "abc";
  2524. b.output = "\xdd\xaf\x35\xa1\x93\x61\x7a\xba\xcc\x41\x73\x49\xae\x20\x41"
  2525. "\x31\x12\xe6\xfa\x4e\x89\xa9\x7e\xa2\x0a\x9e\xee\xe6\x4b\x55"
  2526. "\xd3\x9a\x21\x92\x99\x2a\x27\x4f\xc1\xa8\x36\xba\x3c\x23\xa3"
  2527. "\xfe\xeb\xbd\x45\x4d\x44\x23\x64\x3c\xe8\x0e\x2a\x9a\xc9\x4f"
  2528. "\xa5\x4c\xa4\x9f";
  2529. b.inLen = XSTRLEN(b.input);
  2530. b.outLen = WC_SHA512_DIGEST_SIZE;
  2531. c.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  2532. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  2533. c.output = "\x8e\x95\x9b\x75\xda\xe3\x13\xda\x8c\xf4\xf7\x28\x14\xfc\x14"
  2534. "\x3f\x8f\x77\x79\xc6\xeb\x9f\x7f\xa1\x72\x99\xae\xad\xb6\x88"
  2535. "\x90\x18\x50\x1d\x28\x9e\x49\x00\xf7\xe4\x33\x1b\x99\xde\xc4"
  2536. "\xb5\x43\x3a\xc7\xd3\x29\xee\xb6\xdd\x26\x54\x5e\x96\xe5\x5b"
  2537. "\x87\x4b\xe9\x09";
  2538. c.inLen = XSTRLEN(c.input);
  2539. c.outLen = WC_SHA512_DIGEST_SIZE;
  2540. test_sha[0] = a;
  2541. test_sha[1] = b;
  2542. test_sha[2] = c;
  2543. ret = wc_InitSha512_ex(&sha, HEAP_HINT, devId);
  2544. if (ret != 0)
  2545. return -2400;
  2546. ret = wc_InitSha512_ex(&shaCopy, HEAP_HINT, devId);
  2547. if (ret != 0) {
  2548. wc_Sha512Free(&sha);
  2549. return -2401;
  2550. }
  2551. for (i = 0; i < times; ++i) {
  2552. ret = wc_Sha512Update(&sha, (byte*)test_sha[i].input,
  2553. (word32)test_sha[i].inLen);
  2554. if (ret != 0)
  2555. ERROR_OUT(-2402 - i, exit);
  2556. ret = wc_Sha512GetHash(&sha, hashcopy);
  2557. if (ret != 0)
  2558. ERROR_OUT(-2403 - i, exit);
  2559. ret = wc_Sha512Copy(&sha, &shaCopy);
  2560. if (ret != 0)
  2561. ERROR_OUT(-2404 - i, exit);
  2562. ret = wc_Sha512Final(&sha, hash);
  2563. if (ret != 0)
  2564. ERROR_OUT(-2405 - i, exit);
  2565. wc_Sha512Free(&shaCopy);
  2566. if (XMEMCMP(hash, test_sha[i].output, WC_SHA512_DIGEST_SIZE) != 0)
  2567. ERROR_OUT(-2406 - i, exit);
  2568. if (XMEMCMP(hash, hashcopy, WC_SHA512_DIGEST_SIZE) != 0)
  2569. ERROR_OUT(-2407 - i, exit);
  2570. }
  2571. #ifndef NO_LARGE_HASH_TEST
  2572. /* BEGIN LARGE HASH TEST */ {
  2573. byte large_input[1024];
  2574. #ifdef HASH_SIZE_LIMIT
  2575. const char* large_digest =
  2576. "\x30\x9B\x96\xA6\xE9\x43\x78\x30\xA3\x71\x51\x61\xC1\xEB\xE1\xBE"
  2577. "\xC8\xA5\xF9\x13\x5A\xD6\x6D\x9E\x46\x31\x31\x67\x8D\xE2\xC0\x0B"
  2578. "\x2A\x1A\x03\xE1\xF3\x48\xA7\x33\xBD\x49\xF8\xFF\xF1\xC2\xC2\x95"
  2579. "\xCB\xF0\xAF\x87\x61\x85\x58\x63\x6A\xCA\x70\x9C\x8B\x83\x3F\x5D";
  2580. #else
  2581. const char* large_digest =
  2582. "\x5a\x1f\x73\x90\xbd\x8c\xe4\x63\x54\xce\xa0\x9b\xef\x32\x78\x2d"
  2583. "\x2e\xe7\x0d\x5e\x2f\x9d\x15\x1b\xdd\x2d\xde\x65\x0c\x7b\xfa\x83"
  2584. "\x5e\x80\x02\x13\x84\xb8\x3f\xff\x71\x62\xb5\x09\x89\x63\xe1\xdc"
  2585. "\xa5\xdc\xfc\xfa\x9d\x1a\x4d\xc0\xfa\x3a\x14\xf6\x01\x51\x90\xa4";
  2586. #endif
  2587. for (i = 0; i < (int)sizeof(large_input); i++) {
  2588. large_input[i] = (byte)(i & 0xFF);
  2589. }
  2590. #ifdef HASH_SIZE_LIMIT
  2591. times = 20;
  2592. #else
  2593. times = 100;
  2594. #endif
  2595. for (i = 0; i < times; ++i) {
  2596. ret = wc_Sha512Update(&sha, (byte*)large_input,
  2597. (word32)sizeof(large_input));
  2598. if (ret != 0)
  2599. ERROR_OUT(-2408, exit);
  2600. }
  2601. ret = wc_Sha512Final(&sha, hash);
  2602. if (ret != 0)
  2603. ERROR_OUT(-2409, exit);
  2604. if (XMEMCMP(hash, large_digest, WC_SHA512_DIGEST_SIZE) != 0)
  2605. ERROR_OUT(-2410, exit);
  2606. #ifndef NO_UNALIGNED_MEMORY_TEST
  2607. /* Unaligned memory access test */
  2608. for (i = 1; i < 16; i++) {
  2609. ret = wc_Sha512Update(&sha, (byte*)large_input + i,
  2610. (word32)sizeof(large_input) - i);
  2611. if (ret != 0)
  2612. ERROR_OUT(-2411, exit);
  2613. ret = wc_Sha512Final(&sha, hash);
  2614. }
  2615. #endif
  2616. } /* END LARGE HASH TEST */
  2617. #endif /* NO_LARGE_HASH_TEST */
  2618. exit:
  2619. wc_Sha512Free(&sha);
  2620. wc_Sha512Free(&shaCopy);
  2621. return ret;
  2622. }
  2623. #endif
  2624. #ifdef WOLFSSL_SHA384
  2625. WOLFSSL_TEST_SUBROUTINE int sha384_test(void)
  2626. {
  2627. wc_Sha384 sha, shaCopy;
  2628. byte hash[WC_SHA384_DIGEST_SIZE];
  2629. byte hashcopy[WC_SHA384_DIGEST_SIZE];
  2630. int ret = 0;
  2631. testVector a, b, c;
  2632. testVector test_sha[3];
  2633. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2634. a.input = "";
  2635. a.output = "\x38\xb0\x60\xa7\x51\xac\x96\x38\x4c\xd9\x32\x7e\xb1\xb1\xe3"
  2636. "\x6a\x21\xfd\xb7\x11\x14\xbe\x07\x43\x4c\x0c\xc7\xbf\x63\xf6"
  2637. "\xe1\xda\x27\x4e\xde\xbf\xe7\x6f\x65\xfb\xd5\x1a\xd2\xf1\x48"
  2638. "\x98\xb9\x5b";
  2639. a.inLen = XSTRLEN(a.input);
  2640. a.outLen = WC_SHA384_DIGEST_SIZE;
  2641. b.input = "abc";
  2642. b.output = "\xcb\x00\x75\x3f\x45\xa3\x5e\x8b\xb5\xa0\x3d\x69\x9a\xc6\x50"
  2643. "\x07\x27\x2c\x32\xab\x0e\xde\xd1\x63\x1a\x8b\x60\x5a\x43\xff"
  2644. "\x5b\xed\x80\x86\x07\x2b\xa1\xe7\xcc\x23\x58\xba\xec\xa1\x34"
  2645. "\xc8\x25\xa7";
  2646. b.inLen = XSTRLEN(b.input);
  2647. b.outLen = WC_SHA384_DIGEST_SIZE;
  2648. c.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  2649. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  2650. c.output = "\x09\x33\x0c\x33\xf7\x11\x47\xe8\x3d\x19\x2f\xc7\x82\xcd\x1b"
  2651. "\x47\x53\x11\x1b\x17\x3b\x3b\x05\xd2\x2f\xa0\x80\x86\xe3\xb0"
  2652. "\xf7\x12\xfc\xc7\xc7\x1a\x55\x7e\x2d\xb9\x66\xc3\xe9\xfa\x91"
  2653. "\x74\x60\x39";
  2654. c.inLen = XSTRLEN(c.input);
  2655. c.outLen = WC_SHA384_DIGEST_SIZE;
  2656. test_sha[0] = a;
  2657. test_sha[1] = b;
  2658. test_sha[2] = c;
  2659. ret = wc_InitSha384_ex(&sha, HEAP_HINT, devId);
  2660. if (ret != 0)
  2661. return -2500;
  2662. ret = wc_InitSha384_ex(&shaCopy, HEAP_HINT, devId);
  2663. if (ret != 0) {
  2664. wc_Sha384Free(&sha);
  2665. return -2501;
  2666. }
  2667. for (i = 0; i < times; ++i) {
  2668. ret = wc_Sha384Update(&sha, (byte*)test_sha[i].input,
  2669. (word32)test_sha[i].inLen);
  2670. if (ret != 0)
  2671. ERROR_OUT(-2502 - i, exit);
  2672. ret = wc_Sha384GetHash(&sha, hashcopy);
  2673. if (ret != 0)
  2674. ERROR_OUT(-2503 - i, exit);
  2675. ret = wc_Sha384Copy(&sha, &shaCopy);
  2676. if (ret != 0)
  2677. ERROR_OUT(-2504 - i, exit);
  2678. ret = wc_Sha384Final(&sha, hash);
  2679. if (ret != 0)
  2680. ERROR_OUT(-2505 - i, exit);
  2681. wc_Sha384Free(&shaCopy);
  2682. if (XMEMCMP(hash, test_sha[i].output, WC_SHA384_DIGEST_SIZE) != 0)
  2683. ERROR_OUT(-2506 - i, exit);
  2684. if (XMEMCMP(hash, hashcopy, WC_SHA384_DIGEST_SIZE) != 0)
  2685. ERROR_OUT(-2507 - i, exit);
  2686. }
  2687. #ifndef NO_LARGE_HASH_TEST
  2688. /* BEGIN LARGE HASH TEST */ {
  2689. byte large_input[1024];
  2690. #ifdef HASH_SIZE_LIMIT
  2691. const char* large_digest =
  2692. "\xB5\xAD\x66\x6F\xD9\x58\x5E\x68\xDD\x5E\x30\xD3\x95\x72\x33\xA4"
  2693. "\xE9\x4B\x99\x3A\xEF\xF8\xE1\xBF\x1F\x05\x32\xAA\x16\x00\x82\xEC"
  2694. "\x15\xDA\xF2\x75\xEE\xE9\x06\xAF\x52\x8A\x5C\xEF\x72\x81\x80\xD6";
  2695. #else
  2696. const char* large_digest =
  2697. "\x37\x01\xdb\xff\x1e\x40\x4f\xe1\xe2\xea\x0b\x40\xbb\x3b\x39\x9a"
  2698. "\xcc\xe8\x44\x8e\x7e\xe5\x64\xb5\x6b\x7f\x56\x64\xa7\x2b\x84\xe3"
  2699. "\xc5\xd7\x79\x03\x25\x90\xf7\xa4\x58\xcb\x97\xa8\x8b\xb1\xa4\x81";
  2700. #endif
  2701. for (i = 0; i < (int)sizeof(large_input); i++) {
  2702. large_input[i] = (byte)(i & 0xFF);
  2703. }
  2704. #ifdef HASH_SIZE_LIMIT
  2705. times = 20;
  2706. #else
  2707. times = 100;
  2708. #endif
  2709. for (i = 0; i < times; ++i) {
  2710. ret = wc_Sha384Update(&sha, (byte*)large_input,
  2711. (word32)sizeof(large_input));
  2712. if (ret != 0)
  2713. ERROR_OUT(-2508, exit);
  2714. }
  2715. ret = wc_Sha384Final(&sha, hash);
  2716. if (ret != 0)
  2717. ERROR_OUT(-2509, exit);
  2718. if (XMEMCMP(hash, large_digest, WC_SHA384_DIGEST_SIZE) != 0)
  2719. ERROR_OUT(-2510, exit);
  2720. } /* END LARGE HASH TEST */
  2721. #endif /* NO_LARGE_HASH_TEST */
  2722. exit:
  2723. wc_Sha384Free(&sha);
  2724. wc_Sha384Free(&shaCopy);
  2725. return ret;
  2726. }
  2727. #endif /* WOLFSSL_SHA384 */
  2728. #ifdef WOLFSSL_SHA3
  2729. #ifndef WOLFSSL_NOSHA3_224
  2730. static int sha3_224_test(void)
  2731. {
  2732. wc_Sha3 sha;
  2733. byte hash[WC_SHA3_224_DIGEST_SIZE];
  2734. byte hashcopy[WC_SHA3_224_DIGEST_SIZE];
  2735. testVector a, b, c;
  2736. testVector test_sha[3];
  2737. int ret = 0;
  2738. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2739. a.input = "";
  2740. a.output = "\x6b\x4e\x03\x42\x36\x67\xdb\xb7\x3b\x6e\x15\x45\x4f\x0e\xb1"
  2741. "\xab\xd4\x59\x7f\x9a\x1b\x07\x8e\x3f\x5b\x5a\x6b\xc7";
  2742. a.inLen = XSTRLEN(a.input);
  2743. a.outLen = WC_SHA3_224_DIGEST_SIZE;
  2744. b.input = "abc";
  2745. b.output = "\xe6\x42\x82\x4c\x3f\x8c\xf2\x4a\xd0\x92\x34\xee\x7d\x3c\x76"
  2746. "\x6f\xc9\xa3\xa5\x16\x8d\x0c\x94\xad\x73\xb4\x6f\xdf";
  2747. b.inLen = XSTRLEN(b.input);
  2748. b.outLen = WC_SHA3_224_DIGEST_SIZE;
  2749. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  2750. c.output = "\x8a\x24\x10\x8b\x15\x4a\xda\x21\xc9\xfd\x55\x74\x49\x44\x79"
  2751. "\xba\x5c\x7e\x7a\xb7\x6e\xf2\x64\xea\xd0\xfc\xce\x33";
  2752. c.inLen = XSTRLEN(c.input);
  2753. c.outLen = WC_SHA3_224_DIGEST_SIZE;
  2754. test_sha[0] = a;
  2755. test_sha[1] = b;
  2756. test_sha[2] = c;
  2757. ret = wc_InitSha3_224(&sha, HEAP_HINT, devId);
  2758. if (ret != 0)
  2759. return -2600;
  2760. for (i = 0; i < times; ++i) {
  2761. ret = wc_Sha3_224_Update(&sha, (byte*)test_sha[i].input,
  2762. (word32)test_sha[i].inLen);
  2763. if (ret != 0)
  2764. ERROR_OUT(-2601 - i, exit);
  2765. ret = wc_Sha3_224_GetHash(&sha, hashcopy);
  2766. if (ret != 0)
  2767. ERROR_OUT(-2602 - i, exit);
  2768. ret = wc_Sha3_224_Final(&sha, hash);
  2769. if (ret != 0)
  2770. ERROR_OUT(-2603 - i, exit);
  2771. if (XMEMCMP(hash, test_sha[i].output, WC_SHA3_224_DIGEST_SIZE) != 0)
  2772. ERROR_OUT(-2604 - i, exit);
  2773. if (XMEMCMP(hash, hashcopy, WC_SHA3_224_DIGEST_SIZE) != 0)
  2774. ERROR_OUT(-2605 - i, exit);
  2775. }
  2776. #ifndef NO_LARGE_HASH_TEST
  2777. /* BEGIN LARGE HASH TEST */ {
  2778. byte large_input[1024];
  2779. const char* large_digest =
  2780. "\x13\xe5\xd3\x98\x7b\x94\xda\x41\x12\xc7\x1e\x92\x3a\x19"
  2781. "\x21\x20\x86\x6f\x24\xbf\x0a\x31\xbc\xfd\xd6\x70\x36\xf3";
  2782. for (i = 0; i < (int)sizeof(large_input); i++) {
  2783. large_input[i] = (byte)(i & 0xFF);
  2784. }
  2785. times = 100;
  2786. for (i = 0; i < times; ++i) {
  2787. ret = wc_Sha3_224_Update(&sha, (byte*)large_input,
  2788. (word32)sizeof(large_input));
  2789. if (ret != 0)
  2790. ERROR_OUT(-2606, exit);
  2791. }
  2792. ret = wc_Sha3_224_Final(&sha, hash);
  2793. if (ret != 0)
  2794. ERROR_OUT(-2607, exit);
  2795. if (XMEMCMP(hash, large_digest, WC_SHA3_224_DIGEST_SIZE) != 0)
  2796. ERROR_OUT(-2608, exit);
  2797. } /* END LARGE HASH TEST */
  2798. #endif /* NO_LARGE_HASH_TEST */
  2799. exit:
  2800. wc_Sha3_224_Free(&sha);
  2801. return ret;
  2802. }
  2803. #endif /* WOLFSSL_NOSHA3_224 */
  2804. #ifndef WOLFSSL_NOSHA3_256
  2805. static int sha3_256_test(void)
  2806. {
  2807. wc_Sha3 sha;
  2808. byte hash[WC_SHA3_256_DIGEST_SIZE];
  2809. byte hashcopy[WC_SHA3_256_DIGEST_SIZE];
  2810. testVector a, b, c;
  2811. testVector test_sha[3];
  2812. int ret = 0;
  2813. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2814. byte large_input[1024];
  2815. const char* large_digest =
  2816. "\xdc\x90\xc0\xb1\x25\xdb\x2c\x34\x81\xa3\xff\xbc\x1e\x2e\x87\xeb"
  2817. "\x6d\x70\x85\x61\xe0\xe9\x63\x61\xff\xe5\x84\x4b\x1f\x68\x05\x15";
  2818. #if defined(WOLFSSL_HASH_FLAGS) && !defined(WOLFSSL_ASYNC_CRYPT)
  2819. /* test vector with hash of empty string */
  2820. const char* Keccak256EmptyOut =
  2821. "\xc5\xd2\x46\x01\x86\xf7\x23\x3c\x92\x7e\x7d\xb2\xdc\xc7\x03\xc0"
  2822. "\xe5\x00\xb6\x53\xca\x82\x27\x3b\x7b\xfa\xd8\x04\x5d\x85\xa4\x70";
  2823. #endif
  2824. a.input = "";
  2825. a.output = "\xa7\xff\xc6\xf8\xbf\x1e\xd7\x66\x51\xc1\x47\x56\xa0\x61\xd6"
  2826. "\x62\xf5\x80\xff\x4d\xe4\x3b\x49\xfa\x82\xd8\x0a\x4b\x80\xf8"
  2827. "\x43\x4a";
  2828. a.inLen = XSTRLEN(a.input);
  2829. a.outLen = WC_SHA3_256_DIGEST_SIZE;
  2830. b.input = "abc";
  2831. b.output = "\x3a\x98\x5d\xa7\x4f\xe2\x25\xb2\x04\x5c\x17\x2d\x6b\xd3\x90"
  2832. "\xbd\x85\x5f\x08\x6e\x3e\x9d\x52\x5b\x46\xbf\xe2\x45\x11\x43"
  2833. "\x15\x32";
  2834. b.inLen = XSTRLEN(b.input);
  2835. b.outLen = WC_SHA3_256_DIGEST_SIZE;
  2836. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  2837. c.output = "\x41\xc0\xdb\xa2\xa9\xd6\x24\x08\x49\x10\x03\x76\xa8\x23\x5e"
  2838. "\x2c\x82\xe1\xb9\x99\x8a\x99\x9e\x21\xdb\x32\xdd\x97\x49\x6d"
  2839. "\x33\x76";
  2840. c.inLen = XSTRLEN(c.input);
  2841. c.outLen = WC_SHA3_256_DIGEST_SIZE;
  2842. test_sha[0] = a;
  2843. test_sha[1] = b;
  2844. test_sha[2] = c;
  2845. ret = wc_InitSha3_256(&sha, HEAP_HINT, devId);
  2846. if (ret != 0)
  2847. return -2700;
  2848. for (i = 0; i < times; ++i) {
  2849. ret = wc_Sha3_256_Update(&sha, (byte*)test_sha[i].input,
  2850. (word32)test_sha[i].inLen);
  2851. if (ret != 0)
  2852. ERROR_OUT(-2701 - i, exit);
  2853. ret = wc_Sha3_256_GetHash(&sha, hashcopy);
  2854. if (ret != 0)
  2855. ERROR_OUT(-2702 - i, exit);
  2856. ret = wc_Sha3_256_Final(&sha, hash);
  2857. if (ret != 0)
  2858. ERROR_OUT(-2703 - i, exit);
  2859. if (XMEMCMP(hash, test_sha[i].output, WC_SHA3_256_DIGEST_SIZE) != 0)
  2860. ERROR_OUT(-2704 - i, exit);
  2861. if (XMEMCMP(hash, hashcopy, WC_SHA3_256_DIGEST_SIZE) != 0)
  2862. ERROR_OUT(-2705 - i, exit);
  2863. }
  2864. #ifndef NO_LARGE_HASH_TEST
  2865. /* BEGIN LARGE HASH TEST */ {
  2866. for (i = 0; i < (int)sizeof(large_input); i++) {
  2867. large_input[i] = (byte)(i & 0xFF);
  2868. }
  2869. times = 100;
  2870. for (i = 0; i < times; ++i) {
  2871. ret = wc_Sha3_256_Update(&sha, (byte*)large_input,
  2872. (word32)sizeof(large_input));
  2873. if (ret != 0)
  2874. ERROR_OUT(-2706, exit);
  2875. }
  2876. ret = wc_Sha3_256_Final(&sha, hash);
  2877. if (ret != 0)
  2878. ERROR_OUT(-2707, exit);
  2879. if (XMEMCMP(hash, large_digest, WC_SHA3_256_DIGEST_SIZE) != 0)
  2880. ERROR_OUT(-2708, exit);
  2881. } /* END LARGE HASH TEST */
  2882. #endif /* NO_LARGE_HASH_TEST */
  2883. /* this is a software only variant of SHA3 not supported by external hardware devices */
  2884. #if defined(WOLFSSL_HASH_FLAGS) && !defined(WOLFSSL_ASYNC_CRYPT)
  2885. /* Test for Keccak256 */
  2886. ret = wc_Sha3_SetFlags(&sha, WC_HASH_SHA3_KECCAK256);
  2887. if (ret != 0) {
  2888. ERROR_OUT(-2709, exit);
  2889. }
  2890. ret = wc_Sha3_256_Update(&sha, (byte*)"", 0);
  2891. if (ret != 0) {
  2892. ERROR_OUT(-2710, exit);
  2893. }
  2894. ret = wc_Sha3_256_Final(&sha, hash);
  2895. if (ret != 0) {
  2896. ERROR_OUT(-2711, exit);
  2897. }
  2898. if (XMEMCMP(hash, Keccak256EmptyOut, WC_SHA3_256_DIGEST_SIZE) != 0) {
  2899. ERROR_OUT(-2712, exit);
  2900. }
  2901. #endif /* WOLFSSL_HASH_FLAGS && !WOLFSSL_ASYNC_CRYPT */
  2902. exit:
  2903. wc_Sha3_256_Free(&sha);
  2904. return ret;
  2905. }
  2906. #endif /* WOLFSSL_NOSHA3_256 */
  2907. #ifndef WOLFSSL_NOSHA3_384
  2908. static int sha3_384_test(void)
  2909. {
  2910. wc_Sha3 sha;
  2911. byte hash[WC_SHA3_384_DIGEST_SIZE];
  2912. byte buf[64];
  2913. #ifndef NO_INTM_HASH_TEST
  2914. byte hashcopy[WC_SHA3_384_DIGEST_SIZE];
  2915. #endif
  2916. testVector a, b, c;
  2917. testVector test_sha[3];
  2918. int ret;
  2919. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2920. a.input = "";
  2921. a.output = "\x0c\x63\xa7\x5b\x84\x5e\x4f\x7d\x01\x10\x7d\x85\x2e\x4c\x24"
  2922. "\x85\xc5\x1a\x50\xaa\xaa\x94\xfc\x61\x99\x5e\x71\xbb\xee\x98"
  2923. "\x3a\x2a\xc3\x71\x38\x31\x26\x4a\xdb\x47\xfb\x6b\xd1\xe0\x58"
  2924. "\xd5\xf0\x04";
  2925. a.inLen = XSTRLEN(a.input);
  2926. a.outLen = WC_SHA3_384_DIGEST_SIZE;
  2927. #if defined(WOLFSSL_AFALG_XILINX_SHA3) || defined(WOLFSSL_XILINX_CRYPT) && !defined(WOLFSSL_XILINX_CRYPT_VERSAL)
  2928. /* NIST test vector with a length that is a multiple of 4 */
  2929. b.input = "\x7d\x80\xb1\x60\xc4\xb5\x36\xa3\xbe\xb7\x99\x80\x59\x93\x44"
  2930. "\x04\x7c\x5f\x82\xa1\xdf\xc3\xee\xd4";
  2931. b.output = "\x04\x1c\xc5\x86\x1b\xa3\x34\x56\x3c\x61\xd4\xef\x97\x10\xd4"
  2932. "\x89\x6c\x31\x1c\x92\xed\xbe\x0d\x7c\xd5\x3e\x80\x3b\xf2\xf4"
  2933. "\xeb\x60\x57\x23\x55\x70\x77\x0c\xe8\x7c\x55\x20\xd7\xec\x14"
  2934. "\x19\x87\x22";
  2935. b.inLen = XSTRLEN(b.input);
  2936. b.outLen = WC_SHA3_384_DIGEST_SIZE;
  2937. #else
  2938. b.input = "abc";
  2939. b.output = "\xec\x01\x49\x82\x88\x51\x6f\xc9\x26\x45\x9f\x58\xe2\xc6\xad"
  2940. "\x8d\xf9\xb4\x73\xcb\x0f\xc0\x8c\x25\x96\xda\x7c\xf0\xe4\x9b"
  2941. "\xe4\xb2\x98\xd8\x8c\xea\x92\x7a\xc7\xf5\x39\xf1\xed\xf2\x28"
  2942. "\x37\x6d\x25";
  2943. b.inLen = XSTRLEN(b.input);
  2944. b.outLen = WC_SHA3_384_DIGEST_SIZE;
  2945. #endif
  2946. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  2947. c.output = "\x99\x1c\x66\x57\x55\xeb\x3a\x4b\x6b\xbd\xfb\x75\xc7\x8a\x49"
  2948. "\x2e\x8c\x56\xa2\x2c\x5c\x4d\x7e\x42\x9b\xfd\xbc\x32\xb9\xd4"
  2949. "\xad\x5a\xa0\x4a\x1f\x07\x6e\x62\xfe\xa1\x9e\xef\x51\xac\xd0"
  2950. "\x65\x7c\x22";
  2951. c.inLen = XSTRLEN(c.input);
  2952. c.outLen = WC_SHA3_384_DIGEST_SIZE;
  2953. #if defined(WOLFSSL_XILINX_CRYPT) && !defined(WOLFSSL_XILINX_CRYPT_VERSAL)
  2954. test_sha[0] = b; /* hardware acc. pre-Versal can not handle "" string */
  2955. #else
  2956. test_sha[0] = a;
  2957. #endif
  2958. test_sha[1] = b;
  2959. test_sha[2] = c;
  2960. ret = wc_InitSha3_384(&sha, HEAP_HINT, devId);
  2961. if (ret != 0)
  2962. return -2800;
  2963. for (i = 0; i < times; ++i) {
  2964. XMEMCPY(buf, test_sha[i].input, test_sha[i].inLen);
  2965. ret = wc_Sha3_384_Update(&sha, buf,
  2966. (word32)test_sha[i].inLen);
  2967. if (ret != 0)
  2968. ERROR_OUT(-2801 - (i * 10), exit);
  2969. #ifndef NO_INTM_HASH_TEST
  2970. ret = wc_Sha3_384_GetHash(&sha, hashcopy);
  2971. if (ret != 0)
  2972. ERROR_OUT(-2802 - (i * 10), exit);
  2973. #endif
  2974. ret = wc_Sha3_384_Final(&sha, hash);
  2975. if (ret != 0)
  2976. ERROR_OUT(-2803 - (i * 10), exit);
  2977. if (XMEMCMP(hash, test_sha[i].output, WC_SHA3_384_DIGEST_SIZE) != 0)
  2978. ERROR_OUT(-2804 - (i * 10), exit);
  2979. #ifndef NO_INTM_HASH_TEST
  2980. if (XMEMCMP(hash, hashcopy, WC_SHA3_384_DIGEST_SIZE) != 0)
  2981. ERROR_OUT(-2805 - (i * 10), exit);
  2982. #endif
  2983. }
  2984. #ifndef NO_LARGE_HASH_TEST
  2985. /* BEGIN LARGE HASH TEST */ {
  2986. byte large_input[1024];
  2987. const char* large_digest =
  2988. "\x30\x44\xec\x17\xef\x47\x9f\x55\x36\x11\xd6\x3f\x8a\x31\x5a\x71"
  2989. "\x8a\x71\xa7\x1d\x8e\x84\xe8\x6c\x24\x02\x2f\x7a\x08\x4e\xea\xd7"
  2990. "\x42\x36\x5d\xa8\xc2\xb7\x42\xad\xec\x19\xfb\xca\xc6\x64\xb3\xa4";
  2991. for (i = 0; i < (int)sizeof(large_input); i++) {
  2992. large_input[i] = (byte)(i & 0xFF);
  2993. }
  2994. times = 100;
  2995. for (i = 0; i < times; ++i) {
  2996. ret = wc_Sha3_384_Update(&sha, (byte*)large_input,
  2997. (word32)sizeof(large_input));
  2998. if (ret != 0)
  2999. ERROR_OUT(-2806, exit);
  3000. }
  3001. ret = wc_Sha3_384_Final(&sha, hash);
  3002. if (ret != 0)
  3003. ERROR_OUT(-2807, exit);
  3004. if (XMEMCMP(hash, large_digest, WC_SHA3_384_DIGEST_SIZE) != 0)
  3005. ERROR_OUT(-2808, exit);
  3006. } /* END LARGE HASH TEST */
  3007. #endif /* NO_LARGE_HASH_TEST */
  3008. exit:
  3009. wc_Sha3_384_Free(&sha);
  3010. return ret;
  3011. }
  3012. #endif /* WOLFSSL_NOSHA3_384 */
  3013. #ifndef WOLFSSL_NOSHA3_512
  3014. static int sha3_512_test(void)
  3015. {
  3016. wc_Sha3 sha;
  3017. byte hash[WC_SHA3_512_DIGEST_SIZE];
  3018. byte hashcopy[WC_SHA3_512_DIGEST_SIZE];
  3019. testVector a, b, c;
  3020. testVector test_sha[3];
  3021. int ret;
  3022. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  3023. a.input = "";
  3024. a.output = "\xa6\x9f\x73\xcc\xa2\x3a\x9a\xc5\xc8\xb5\x67\xdc\x18\x5a\x75"
  3025. "\x6e\x97\xc9\x82\x16\x4f\xe2\x58\x59\xe0\xd1\xdc\xc1\x47\x5c"
  3026. "\x80\xa6\x15\xb2\x12\x3a\xf1\xf5\xf9\x4c\x11\xe3\xe9\x40\x2c"
  3027. "\x3a\xc5\x58\xf5\x00\x19\x9d\x95\xb6\xd3\xe3\x01\x75\x85\x86"
  3028. "\x28\x1d\xcd\x26";
  3029. a.inLen = XSTRLEN(a.input);
  3030. a.outLen = WC_SHA3_512_DIGEST_SIZE;
  3031. b.input = "abc";
  3032. b.output = "\xb7\x51\x85\x0b\x1a\x57\x16\x8a\x56\x93\xcd\x92\x4b\x6b\x09"
  3033. "\x6e\x08\xf6\x21\x82\x74\x44\xf7\x0d\x88\x4f\x5d\x02\x40\xd2"
  3034. "\x71\x2e\x10\xe1\x16\xe9\x19\x2a\xf3\xc9\x1a\x7e\xc5\x76\x47"
  3035. "\xe3\x93\x40\x57\x34\x0b\x4c\xf4\x08\xd5\xa5\x65\x92\xf8\x27"
  3036. "\x4e\xec\x53\xf0";
  3037. b.inLen = XSTRLEN(b.input);
  3038. b.outLen = WC_SHA3_512_DIGEST_SIZE;
  3039. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  3040. c.output = "\x04\xa3\x71\xe8\x4e\xcf\xb5\xb8\xb7\x7c\xb4\x86\x10\xfc\xa8"
  3041. "\x18\x2d\xd4\x57\xce\x6f\x32\x6a\x0f\xd3\xd7\xec\x2f\x1e\x91"
  3042. "\x63\x6d\xee\x69\x1f\xbe\x0c\x98\x53\x02\xba\x1b\x0d\x8d\xc7"
  3043. "\x8c\x08\x63\x46\xb5\x33\xb4\x9c\x03\x0d\x99\xa2\x7d\xaf\x11"
  3044. "\x39\xd6\xe7\x5e";
  3045. c.inLen = XSTRLEN(c.input);
  3046. c.outLen = WC_SHA3_512_DIGEST_SIZE;
  3047. test_sha[0] = a;
  3048. test_sha[1] = b;
  3049. test_sha[2] = c;
  3050. ret = wc_InitSha3_512(&sha, HEAP_HINT, devId);
  3051. if (ret != 0)
  3052. return -2900;
  3053. for (i = 0; i < times; ++i) {
  3054. ret = wc_Sha3_512_Update(&sha, (byte*)test_sha[i].input,
  3055. (word32)test_sha[i].inLen);
  3056. if (ret != 0)
  3057. ERROR_OUT(-2901 - i, exit);
  3058. ret = wc_Sha3_512_GetHash(&sha, hashcopy);
  3059. if (ret != 0)
  3060. ERROR_OUT(-2902 - i, exit);
  3061. ret = wc_Sha3_512_Final(&sha, hash);
  3062. if (ret != 0)
  3063. ERROR_OUT(-2903 - i, exit);
  3064. if (XMEMCMP(hash, test_sha[i].output, WC_SHA3_512_DIGEST_SIZE) != 0)
  3065. ERROR_OUT(-2904 - i, exit);
  3066. if (XMEMCMP(hash, hashcopy, WC_SHA3_512_DIGEST_SIZE) != 0)
  3067. ERROR_OUT(-2905 - i, exit);
  3068. }
  3069. #ifndef NO_LARGE_HASH_TEST
  3070. /* BEGIN LARGE HASH TEST */ {
  3071. byte large_input[1024];
  3072. const char* large_digest =
  3073. "\x9c\x13\x26\xb6\x26\xb2\x94\x31\xbc\xf4\x34\xe9\x6f\xf2\xd6\x29"
  3074. "\x9a\xd0\x9b\x32\x63\x2f\x18\xa7\x5f\x23\xc9\x60\xc2\x32\x0c\xbc"
  3075. "\x57\x77\x33\xf1\x83\x81\x8a\xd3\x15\x7c\x93\xdc\x80\x9f\xed\x61"
  3076. "\x41\xa7\x5b\xfd\x32\x0e\x38\x15\xb0\x46\x3b\x7a\x4f\xfd\x44\x88";
  3077. for (i = 0; i < (int)sizeof(large_input); i++) {
  3078. large_input[i] = (byte)(i & 0xFF);
  3079. }
  3080. times = 100;
  3081. for (i = 0; i < times; ++i) {
  3082. ret = wc_Sha3_512_Update(&sha, (byte*)large_input,
  3083. (word32)sizeof(large_input));
  3084. if (ret != 0)
  3085. ERROR_OUT(-2906, exit);
  3086. }
  3087. ret = wc_Sha3_512_Final(&sha, hash);
  3088. if (ret != 0)
  3089. ERROR_OUT(-2907, exit);
  3090. if (XMEMCMP(hash, large_digest, WC_SHA3_512_DIGEST_SIZE) != 0)
  3091. ERROR_OUT(-2908, exit);
  3092. } /* END LARGE HASH TEST */
  3093. #endif /* NO_LARGE_HASH_TEST */
  3094. exit:
  3095. wc_Sha3_512_Free(&sha);
  3096. return ret;
  3097. }
  3098. #endif /* WOLFSSL_NOSHA3_512 */
  3099. WOLFSSL_TEST_SUBROUTINE int sha3_test(void)
  3100. {
  3101. int ret;
  3102. (void)ret;
  3103. #ifndef WOLFSSL_NOSHA3_224
  3104. if ((ret = sha3_224_test()) != 0)
  3105. return ret;
  3106. #endif
  3107. #ifndef WOLFSSL_NOSHA3_256
  3108. if ((ret = sha3_256_test()) != 0)
  3109. return ret;
  3110. #endif
  3111. #ifndef WOLFSSL_NOSHA3_384
  3112. if ((ret = sha3_384_test()) != 0)
  3113. return ret;
  3114. #endif
  3115. #ifndef WOLFSSL_NOSHA3_512
  3116. if ((ret = sha3_512_test()) != 0)
  3117. return ret;
  3118. #endif
  3119. return 0;
  3120. }
  3121. #endif /* WOLFSSL_SHA3 */
  3122. #ifdef WOLFSSL_SHAKE128
  3123. static int shake128_absorb_test(wc_Shake* sha)
  3124. {
  3125. byte hash[WC_SHA3_128_BLOCK_SIZE*2];
  3126. testVector a, b, c, d, e;
  3127. testVector test_sha[5];
  3128. int ret = 0;
  3129. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  3130. byte large_input[1024];
  3131. const char* large_digest =
  3132. "\x2b\xd1\x69\x9f\xb3\x75\x40\x74\xb8\xb2\xd2\x0b\x92\x47\x9b\xfe"
  3133. "\xc9\x91\x48\xbe\xda\xa4\x09\xd7\x61\x35\x18\x05\x07\x71\xa5\x61"
  3134. "\x4d\xc4\x94\xad\xbe\x04\x7d\xad\x95\x2f\xeb\x2c\xc0\x10\x67\x43"
  3135. "\x40\xf1\x4a\x58\x1c\x54\xfa\x24\x1c\x1a\x4e\x8d\x9b\xbc\xea\xa7"
  3136. "\x32\xf2\x4c\xc7\x86\x05\x36\xdc\xb4\x42\xd8\x35\xd1\xb4\xa2\x79"
  3137. "\xa2\xe6\xee\x67\x4f\xbf\x2a\x93\x41\x88\x25\x56\x29\x90\x1a\x06"
  3138. "\xba\xfe\x9f\xa6\x1a\x74\xe8\x7e\x85\x4a\xc8\x58\x60\xb1\x7b\x18"
  3139. "\xdf\x77\x59\x46\x04\xc1\xff\x4b\x9b\xcb\xad\xfe\x91\x28\xf0\x01"
  3140. "\xc1\x33\xd0\x99\x99\x2e\x0c\x86\x84\x67\x4d\x37\xa4\x42\x45\x10"
  3141. "\xdc\x8f\xdb\x6f\xa6\x9b\xee\x8a\x60\xa5\x1f\x95\x3f\x8f\xf5\x31"
  3142. "\x4b\x1d\x48\x1e\x45\xff\x79\x5c\xbe\x72\xfc\x56\xed\x6d\x1a\x99"
  3143. "\x7f\x23\x7c\xd1\xa5\x50\x9e\xb0\x4d\x61\x37\xa5\xcb\x24\x71\x3b"
  3144. "\xa3\x60\x51\x2e\x80\x83\x8b\xe0\x55\x50\xa7\x1e\xcc\x9f\xac\x41"
  3145. "\x77\x2c\x79\x22\x30\x09\x1b\x1a\x83\x5b\x2c\x48\xdc\x09\x7d\x59"
  3146. "\x0d\xf0\x54\x17\xfb\x5e\x38\x68\xde\xdb\xc5\x93\xab\x17\x5f\x4b"
  3147. "\x4d\x6d\xf2\xc7\x4e\x15\x1e\x10\x76\xc4\xcb\x87\xd8\xb7\x9d\xa8"
  3148. "\xbf\xc5\x2e\x5e\xfc\xd3\x6c\x45\xd4\x5d\x72\x0f\x66\xeb\x67\x86"
  3149. "\xfa\x6c\xd6\x80\xa4\x23\xcb\x5d\xed\x3c\xde\xdc\x5b\x3d\xca\x95"
  3150. "\x43\x4b\xdc\xe8\x49\xd3\xe1\x01\xd4\xf1\xe4\x47\xcf\x56\xba\x71"
  3151. "\xb4\x69\xed\xe7\xdb\x0f\x89\xd6\xbb\xcd\x1a\xff\xb4\xbe\x72\x26"
  3152. "\xdc\x76\x79\xb3\x1a\x4b\xe6\x8d\x9b\x8e\xd9\xe9\xe6\xf9\xff\xa5";
  3153. a.input = "";
  3154. a.output = "\x7f\x9c\x2b\xa4\xe8\x8f\x82\x7d\x61\x60\x45\x50\x76\x05\x85"
  3155. "\x3e\xd7\x3b\x80\x93\xf6\xef\xbc\x88\xeb\x1a\x6e\xac\xfa\x66"
  3156. "\xef\x26\x3c\xb1\xee\xa9\x88\x00\x4b\x93\x10\x3c\xfb\x0a\xee"
  3157. "\xfd\x2a\x68\x6e\x01\xfa\x4a\x58\xe8\xa3\x63\x9c\xa8\xa1\xe3"
  3158. "\xf9\xae\x57\xe2\x35\xb8\xcc\x87\x3c\x23\xdc\x62\xb8\xd2\x60"
  3159. "\x16\x9a\xfa\x2f\x75\xab\x91\x6a\x58\xd9\x74\x91\x88\x35\xd2"
  3160. "\x5e\x6a\x43\x50\x85\xb2\xba\xdf\xd6\xdf\xaa\xc3\x59\xa5\xef"
  3161. "\xbb\x7b\xcc\x4b\x59\xd5\x38\xdf\x9a\x04\x30\x2e\x10\xc8\xbc"
  3162. "\x1c\xbf\x1a\x0b\x3a\x51\x20\xea\x17\xcd\xa7\xcf\xad\x76\x5f"
  3163. "\x56\x23\x47\x4d\x36\x8c\xcc\xa8\xaf\x00\x07\xcd\x9f\x5e\x4c"
  3164. "\x84\x9f\x16\x7a\x58\x0b\x14\xaa\xbd\xef\xae\xe7\xee\xf4\x7c"
  3165. "\xb0\xfc\xa9";
  3166. a.inLen = XSTRLEN(a.input);
  3167. a.outLen = WC_SHA3_128_BLOCK_SIZE;
  3168. b.input = "abc";
  3169. b.output = "\x58\x81\x09\x2d\xd8\x18\xbf\x5c\xf8\xa3\xdd\xb7\x93\xfb\xcb"
  3170. "\xa7\x40\x97\xd5\xc5\x26\xa6\xd3\x5f\x97\xb8\x33\x51\x94\x0f"
  3171. "\x2c\xc8\x44\xc5\x0a\xf3\x2a\xcd\x3f\x2c\xdd\x06\x65\x68\x70"
  3172. "\x6f\x50\x9b\xc1\xbd\xde\x58\x29\x5d\xae\x3f\x89\x1a\x9a\x0f"
  3173. "\xca\x57\x83\x78\x9a\x41\xf8\x61\x12\x14\xce\x61\x23\x94\xdf"
  3174. "\x28\x6a\x62\xd1\xa2\x25\x2a\xa9\x4d\xb9\xc5\x38\x95\x6c\x71"
  3175. "\x7d\xc2\xbe\xd4\xf2\x32\xa0\x29\x4c\x85\x7c\x73\x0a\xa1\x60"
  3176. "\x67\xac\x10\x62\xf1\x20\x1f\xb0\xd3\x77\xcf\xb9\xcd\xe4\xc6"
  3177. "\x35\x99\xb2\x7f\x34\x62\xbb\xa4\xa0\xed\x29\x6c\x80\x1f\x9f"
  3178. "\xf7\xf5\x73\x02\xbb\x30\x76\xee\x14\x5f\x97\xa3\x2a\xe6\x8e"
  3179. "\x76\xab\x66\xc4\x8d\x51\x67\x5b\xd4\x9a\xcc\x29\x08\x2f\x56"
  3180. "\x47\x58\x4e";
  3181. b.inLen = XSTRLEN(b.input);
  3182. b.outLen = WC_SHA3_128_BLOCK_SIZE;
  3183. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  3184. c.output = "\x1a\x96\x18\x2b\x50\xfb\x8c\x7e\x74\xe0\xa7\x07\x78\x8f\x55"
  3185. "\xe9\x82\x09\xb8\xd9\x1f\xad\xe8\xf3\x2f\x8d\xd5\xcf\xf7\xbf"
  3186. "\x21\xf5\x4e\xe5\xf1\x95\x50\x82\x5a\x6e\x07\x00\x30\x51\x9e"
  3187. "\x94\x42\x63\xac\x1c\x67\x65\x28\x70\x65\x62\x1f\x9f\xcb\x32"
  3188. "\x01\x72\x3e\x32\x23\xb6\x3a\x46\xc2\x93\x8a\xa9\x53\xba\x84"
  3189. "\x01\xd0\xea\x77\xb8\xd2\x64\x90\x77\x55\x66\x40\x7b\x95\x67"
  3190. "\x3c\x0f\x4c\xc1\xce\x9f\xd9\x66\x14\x8d\x7e\xfd\xff\x26\xbb"
  3191. "\xf9\xf4\x8a\x21\xc6\xda\x35\xbf\xaa\x54\x56\x54\xf7\x0a\xe5"
  3192. "\x86\xff\x10\x13\x14\x20\x77\x14\x83\xec\x92\xed\xab\x40\x8c"
  3193. "\x76\x7b\xf4\xc5\xb4\xff\xfa\xa8\x0c\x8c\xa2\x14\xd8\x4c\x4d"
  3194. "\xc7\x00\xd0\xc5\x06\x30\xb2\xff\xc3\x79\x3e\xa4\xd8\x72\x58"
  3195. "\xb4\xc9\x54";
  3196. c.inLen = XSTRLEN(c.input);
  3197. c.outLen = WC_SHA3_128_BLOCK_SIZE;
  3198. /* Taken from NIST CAVP test vectors - full rate output. */
  3199. d.input = "\xdc\x88\x6d\xf3\xf6\x9c\x49\x51\x3d\xe3\x62\x7e\x94\x81\xdb"
  3200. "\x58\x71\xe8\xee\x88\xeb\x9f\x99\x61\x15\x41\x93\x0a\x8b\xc8"
  3201. "\x85\xe0";
  3202. d.output = "\x93\x68\xf0\x15\x10\x92\x44\xeb\x02\x47\xfa\x3a\x0e\x57\xf5"
  3203. "\x2e\xa7\xd9\xeb\xa2\x3d\xae\x7a\x19\x7f\x0a\x29\xe9\x22\x55"
  3204. "\x06\x05\x98\x16\xb7\x84\x48\xb6\x49\x7a\x76\xeb\x96\x2d\xb3"
  3205. "\xf8\x4d\x37\x60\xf1\xfe\xb4\xbd\xc1\xfd\x4a\xc9\x4e\x91\x7a"
  3206. "\xc2\xea\x5e\x4f\x38\x37\x4a\xa5\x6e\x4f\x47\x67\xb8\xd7\x83"
  3207. "\x1b\x2d\x51\x49\x5a\xb8\xea\xb7\xc9\x82\x20\xaf\x13\x41\x5a"
  3208. "\x59\xbb\x7c\x17\x7a\xcd\x62\x8e\xf0\xff\xe3\x6c\xeb\x18\x59"
  3209. "\x5d\x14\x4c\xbf\x25\xef\xc0\x6c\xd9\x56\xa5\x78\x20\x6e\xa8"
  3210. "\xf9\x14\x5e\xf9\xce\x19\x50\x6a\x9d\x04\x4e\xc7\x00\x79\x9f"
  3211. "\xa1\x41\x9b\xaf\x60\x52\xc0\xc1\xb4\x45\xf8\x35\x17\x57\xb0"
  3212. "\xd0\x22\x87\x21\x89\xe2\xc0\x27\x3f\x82\xd9\x69\x69\x66\x3e"
  3213. "\x55\x4d\x09";
  3214. d.inLen = 32;
  3215. d.outLen = WC_SHA3_128_BLOCK_SIZE;
  3216. /* Taken from NIST CAVP test vectors - more than one output block. */
  3217. e.input = "\x8d\x80\x01\xe2\xc0\x96\xf1\xb8\x8e\x7c\x92\x24\xa0\x86\xef"
  3218. "\xd4\x79\x7f\xbf\x74\xa8\x03\x3a\x2d\x42\x2a\x2b\x6b\x8f\x67"
  3219. "\x47\xe4";
  3220. e.output = "\xe1\x7e\xab\x0d\xa4\x04\xf9\xb6\xac\xc0\x84\x97\x2f\xc5\x79"
  3221. "\xe8\x6d\xaa\x76\x10\xa5\xe1\x7c\x23\x2f\x79\x19\x83\x96\xfd"
  3222. "\x01\xc2\x4c\x34\xbb\x54\xf4\xb0\x1e\xf7\x40\xb4\x25\x33\x4a"
  3223. "\x55\xdd\x24\x81\x3d\xc8\xea\x86\xf5\x6e\xf7\x27\x67\x26\x2b"
  3224. "\xf2\x25\x74\x8c\xcc\x3d\x9f\x48\x6f\xfb\x72\x8f\x4e\xad\x29"
  3225. "\x60\xc9\x6c\x3e\x44\x63\x86\xea\xce\x21\x9c\x84\x28\x16\x11"
  3226. "\x63\x58\xb0\xf4\x2d\x7d\xff\xf7\xdd\x24\x11\xfa\x2a\x56\x79"
  3227. "\xfd\x7a\x94\x77\x45\x75\xba\xf9\xfc\xad\x68\xa1\x9e\x30\xd1"
  3228. "\x49\xb0\x59\xb5\x9c\x44\x6c\x4e\xdc\xa5\x9b\xc5\xa4\x79\x9d"
  3229. "\xc4\x65\xaa\x9e\x78\x2c\xed\x9f\x21\xc5\x5d\xe2\x42\xdd\x25"
  3230. "\xd0\xd9\xde\x60\xd0\x9f\xf8\x6a\xba\xf3\xa0\x3a\x76\x71\xb3"
  3231. "\x05\x42\xdf\xbe\x72\xfc\x56\xed\x6d\x1a\x99\x7f\x23\x7c\xd1"
  3232. "\xa5\x50\x9e\xb0\x4d\x61\x37\xa5\xcb\x24\x71\x3b\xa3\x60\x51"
  3233. "\x2e\x80\x83\x8b\xe0\x55\x50\xa7\x1e\xcc\x9f\xac\x41\x77\x2c"
  3234. "\x79\x22\x30\x09\x1b\x1a\x83\x5b\x2c\x48\xdc\x09\x7d\x59\x0d"
  3235. "\xf0\x54\x17\xfb\x5e\x38\x68\xde\xdb\xc5\x93\xab\x17\x5f\x4b"
  3236. "\x4d\x6d\xf2\xc7\x4e\x15\x1e\x10\x76\xc4\xcb\x87\xd8\xb7\x9d"
  3237. "\xa8\xbf\xc5\x2e\x5e\xfc\xd3\x6c\x45\xd4\x5d\x72\x0f\x66\xeb"
  3238. "\x67\x86\xfa\x6c\xd6\x80\xa4\x23\xcb\x5d\xed\x3c\xde\xdc\x5b"
  3239. "\x3d\xca\x95\x43\x4b\xdc\xe8\x49\xd3\xe1\x01\xd4\xf1\xe4\x47"
  3240. "\xcf\x56\xba\x71\xb4\x69\xed\xe7\xdb\x0f\x89\xd6\xbb\xcd\x1a"
  3241. "\xff\xb4\xbe\x72\x26\xdc\x76\x79\xb3\x1a\x4b\xe6\x8d\x9b\x8e"
  3242. "\xd9\xe9\xe6\xf9\xff\xa5";
  3243. e.inLen = 32;
  3244. e.outLen = 2 * WC_SHA3_128_BLOCK_SIZE;
  3245. test_sha[0] = a;
  3246. test_sha[1] = b;
  3247. test_sha[2] = c;
  3248. test_sha[3] = d;
  3249. test_sha[4] = e;
  3250. for (i = 0; i < times; ++i) {
  3251. ret = wc_InitShake128(sha, HEAP_HINT, devId);
  3252. if (ret != 0)
  3253. ERROR_OUT(-3100 - i, exit);
  3254. ret = wc_Shake128_Absorb(sha, (byte*)test_sha[i].input,
  3255. (word32)test_sha[i].inLen);
  3256. if (ret != 0)
  3257. ERROR_OUT(-3101 - i, exit);
  3258. ret = wc_Shake128_SqueezeBlocks(sha, hash,
  3259. (word32)test_sha[i].outLen / WC_SHA3_128_BLOCK_SIZE);
  3260. if (ret != 0)
  3261. ERROR_OUT(-3102 - i, exit);
  3262. if (XMEMCMP(hash, test_sha[i].output, (word32)test_sha[i].outLen) != 0)
  3263. ERROR_OUT(-3103 - i, exit);
  3264. }
  3265. #ifndef NO_LARGE_HASH_TEST
  3266. /* BEGIN LARGE HASH TEST */ {
  3267. for (i = 0; i < (int)sizeof(large_input); i++) {
  3268. large_input[i] = (byte)(i & 0xFF);
  3269. }
  3270. ret = wc_InitShake128(sha, HEAP_HINT, devId);
  3271. if (ret != 0)
  3272. ERROR_OUT(-3104, exit);
  3273. /* Absorb is non-incremental. */
  3274. ret = wc_Shake128_Absorb(sha, (byte*)large_input,
  3275. (word32)sizeof(large_input));
  3276. if (ret != 0)
  3277. ERROR_OUT(-3105, exit);
  3278. /* Able to squeeze out blocks incrementally. */
  3279. ret = wc_Shake128_SqueezeBlocks(sha, hash, 1);
  3280. if (ret != 0)
  3281. ERROR_OUT(-3106, exit);
  3282. ret = wc_Shake128_SqueezeBlocks(sha, hash,
  3283. ((word32)sizeof(hash) / WC_SHA3_128_BLOCK_SIZE) - 1);
  3284. if (ret != 0)
  3285. ERROR_OUT(-3106, exit);
  3286. if (XMEMCMP(hash, large_digest, sizeof(hash)) != 0)
  3287. ERROR_OUT(-3107, exit);
  3288. } /* END LARGE HASH TEST */
  3289. #endif /* NO_LARGE_HASH_TEST */
  3290. exit:
  3291. return ret;
  3292. }
  3293. WOLFSSL_TEST_SUBROUTINE int shake128_test(void)
  3294. {
  3295. wc_Shake sha;
  3296. byte hash[250];
  3297. testVector a, b, c, d, e;
  3298. testVector test_sha[5];
  3299. int ret = 0;
  3300. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  3301. byte large_input[1024];
  3302. const char* large_digest =
  3303. "\x88\xd7\x0e\x86\x46\x72\x6b\x3d\x7d\x22\xe1\xa9\x2d\x02\xdb\x35"
  3304. "\x92\x4f\x1b\x03\x90\xee\xa3\xce\xd1\x3a\x08\x3a\xd7\x4e\x10\xdf"
  3305. "\x09\x67\x33\x35\x4f\xdd\x38\x50\x5b\xcb\x75\xc7\xba\x65\xe5\xe8"
  3306. "\xb8\x76\xde\xc5\xee\xd7\xf1\x65\x93\x4e\x5e\xc4\xb1\xd7\x6b\xee"
  3307. "\x4b\x57\x48\xf5\x38\x49\x9e\x45\xa0\xf7\x32\xe9\x05\x26\x6a\x10"
  3308. "\x70\xd4\x7c\x19\x01\x1f\x6d\x37\xba\x7b\x74\xc2\xbc\xb6\xbc\x74"
  3309. "\xa3\x66\x6c\x9b\x11\x84\x9d\x4a\x36\xbc\x8a\x0d\x4c\xe3\x39\xfa"
  3310. "\xfa\x1b";
  3311. a.input = "";
  3312. a.output = "\x7f\x9c\x2b\xa4\xe8\x8f\x82\x7d\x61\x60\x45\x50\x76\x05\x85"
  3313. "\x3e\xd7\x3b\x80\x93\xf6\xef\xbc\x88\xeb\x1a\x6e\xac\xfa\x66"
  3314. "\xef\x26\x3c\xb1\xee\xa9\x88\x00\x4b\x93\x10\x3c\xfb\x0a\xee"
  3315. "\xfd\x2a\x68\x6e\x01\xfa\x4a\x58\xe8\xa3\x63\x9c\xa8\xa1\xe3"
  3316. "\xf9\xae\x57\xe2\x35\xb8\xcc\x87\x3c\x23\xdc\x62\xb8\xd2\x60"
  3317. "\x16\x9a\xfa\x2f\x75\xab\x91\x6a\x58\xd9\x74\x91\x88\x35\xd2"
  3318. "\x5e\x6a\x43\x50\x85\xb2\xba\xdf\xd6\xdf\xaa\xc3\x59\xa5\xef"
  3319. "\xbb\x7b\xcc\x4b\x59\xd5\x38\xdf\x9a";
  3320. a.inLen = XSTRLEN(a.input);
  3321. a.outLen = 114;
  3322. b.input = "abc";
  3323. b.output = "\x58\x81\x09\x2d\xd8\x18\xbf\x5c\xf8\xa3\xdd\xb7\x93\xfb\xcb"
  3324. "\xa7\x40\x97\xd5\xc5\x26\xa6\xd3\x5f\x97\xb8\x33\x51\x94\x0f"
  3325. "\x2c\xc8\x44\xc5\x0a\xf3\x2a\xcd\x3f\x2c\xdd\x06\x65\x68\x70"
  3326. "\x6f\x50\x9b\xc1\xbd\xde\x58\x29\x5d\xae\x3f\x89\x1a\x9a\x0f"
  3327. "\xca\x57\x83\x78\x9a\x41\xf8\x61\x12\x14\xce\x61\x23\x94\xdf"
  3328. "\x28\x6a\x62\xd1\xa2\x25\x2a\xa9\x4d\xb9\xc5\x38\x95\x6c\x71"
  3329. "\x7d\xc2\xbe\xd4\xf2\x32\xa0\x29\x4c\x85\x7c\x73\x0a\xa1\x60"
  3330. "\x67\xac\x10\x62\xf1\x20\x1f\xb0\xd3";
  3331. b.inLen = XSTRLEN(b.input);
  3332. b.outLen = 114;
  3333. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  3334. c.output = "\x1a\x96\x18\x2b\x50\xfb\x8c\x7e\x74\xe0\xa7\x07\x78\x8f\x55"
  3335. "\xe9\x82\x09\xb8\xd9\x1f\xad\xe8\xf3\x2f\x8d\xd5\xcf\xf7\xbf"
  3336. "\x21\xf5\x4e\xe5\xf1\x95\x50\x82\x5a\x6e\x07\x00\x30\x51\x9e"
  3337. "\x94\x42\x63\xac\x1c\x67\x65\x28\x70\x65\x62\x1f\x9f\xcb\x32"
  3338. "\x01\x72\x3e\x32\x23\xb6\x3a\x46\xc2\x93\x8a\xa9\x53\xba\x84"
  3339. "\x01\xd0\xea\x77\xb8\xd2\x64\x90\x77\x55\x66\x40\x7b\x95\x67"
  3340. "\x3c\x0f\x4c\xc1\xce\x9f\xd9\x66\x14\x8d\x7e\xfd\xff\x26\xbb"
  3341. "\xf9\xf4\x8a\x21\xc6\xda\x35\xbf\xaa";
  3342. c.inLen = XSTRLEN(c.input);
  3343. c.outLen = 114;
  3344. /* Taken from NIST CAVP test vectors - full rate output. */
  3345. d.input = "\xdc\x88\x6d\xf3\xf6\x9c\x49\x51\x3d\xe3\x62\x7e\x94\x81\xdb"
  3346. "\x58\x71\xe8\xee\x88\xeb\x9f\x99\x61\x15\x41\x93\x0a\x8b\xc8"
  3347. "\x85\xe0";
  3348. d.output = "\x93\x68\xf0\x15\x10\x92\x44\xeb\x02\x47\xfa\x3a\x0e\x57\xf5"
  3349. "\x2e\xa7\xd9\xeb\xa2\x3d\xae\x7a\x19\x7f\x0a\x29\xe9\x22\x55"
  3350. "\x06\x05\x98\x16\xb7\x84\x48\xb6\x49\x7a\x76\xeb\x96\x2d\xb3"
  3351. "\xf8\x4d\x37\x60\xf1\xfe\xb4\xbd\xc1\xfd\x4a\xc9\x4e\x91\x7a"
  3352. "\xc2\xea\x5e\x4f\x38\x37\x4a\xa5\x6e\x4f\x47\x67\xb8\xd7\x83"
  3353. "\x1b\x2d\x51\x49\x5a\xb8\xea\xb7\xc9\x82\x20\xaf\x13\x41\x5a"
  3354. "\x59\xbb\x7c\x17\x7a\xcd\x62\x8e\xf0\xff\xe3\x6c\xeb\x18\x59"
  3355. "\x5d\x14\x4c\xbf\x25\xef\xc0\x6c\xd9\x56\xa5\x78\x20\x6e\xa8"
  3356. "\xf9\x14\x5e\xf9\xce\x19\x50\x6a\x9d\x04\x4e\xc7\x00\x79\x9f"
  3357. "\xa1";
  3358. d.inLen = 32;
  3359. d.outLen = 136;
  3360. /* Taken from NIST CAVP test vectors - more than one output block. */
  3361. e.input = "\x8d\x80\x01\xe2\xc0\x96\xf1\xb8\x8e\x7c\x92\x24\xa0\x86\xef"
  3362. "\xd4\x79\x7f\xbf\x74\xa8\x03\x3a\x2d\x42\x2a\x2b\x6b\x8f\x67"
  3363. "\x47\xe4";
  3364. e.output = "\xe1\x7e\xab\x0d\xa4\x04\xf9\xb6\xac\xc0\x84\x97\x2f\xc5\x79"
  3365. "\xe8\x6d\xaa\x76\x10\xa5\xe1\x7c\x23\x2f\x79\x19\x83\x96\xfd"
  3366. "\x01\xc2\x4c\x34\xbb\x54\xf4\xb0\x1e\xf7\x40\xb4\x25\x33\x4a"
  3367. "\x55\xdd\x24\x81\x3d\xc8\xea\x86\xf5\x6e\xf7\x27\x67\x26\x2b"
  3368. "\xf2\x25\x74\x8c\xcc\x3d\x9f\x48\x6f\xfb\x72\x8f\x4e\xad\x29"
  3369. "\x60\xc9\x6c\x3e\x44\x63\x86\xea\xce\x21\x9c\x84\x28\x16\x11"
  3370. "\x63\x58\xb0\xf4\x2d\x7d\xff\xf7\xdd\x24\x11\xfa\x2a\x56\x79"
  3371. "\xfd\x7a\x94\x77\x45\x75\xba\xf9\xfc\xad\x68\xa1\x9e\x30\xd1"
  3372. "\x49\xb0\x59\xb5\x9c\x44\x6c\x4e\xdc\xa5\x9b\xc5\xa4\x79\x9d"
  3373. "\xc4\x65\xaa\x9e\x78\x2c\xed\x9f\x21\xc5\x5d\xe2\x42\xdd\x25"
  3374. "\xd0\xd9\xde\x60\xd0\x9f\xf8\x6a\xba\xf3\xa0\x3a\x76\x71\xb3"
  3375. "\x05\x42\xdf\xbe\x72\xfc\x56\xed\x6d\x1a\x99\x7f\x23\x7c\xd1"
  3376. "\xa5\x50\x9e\xb0\x4d\x61\x37\xa5\xcb\x24\x71\x3b\xa3\x60\x51"
  3377. "\x2e\x80\x83\x8b\xe0\x55\x50\xa7\x1e\xcc\x9f\xac\x41\x77\x2c"
  3378. "\x79\x22\x30\x09\x1b\x1a\x83\x5b\x2c\x48\xdc\x09\x7d\x59\x0d"
  3379. "\xf0\x54\x17\xfb\x5e\x38\x68\xde\xdb\xc5\x93\xab\x17\x5f\x4b"
  3380. "\x4d\x6d\xf2\xc7\x4e\x15\x1e\x10\x76\xc4";
  3381. e.inLen = 32;
  3382. e.outLen = 250;
  3383. test_sha[0] = a;
  3384. test_sha[1] = b;
  3385. test_sha[2] = c;
  3386. test_sha[3] = d;
  3387. test_sha[4] = e;
  3388. ret = wc_InitShake128(&sha, HEAP_HINT, devId);
  3389. if (ret != 0)
  3390. return -3100;
  3391. for (i = 0; i < times; ++i) {
  3392. ret = wc_Shake128_Update(&sha, (byte*)test_sha[i].input,
  3393. (word32)test_sha[i].inLen);
  3394. if (ret != 0)
  3395. ERROR_OUT(-3101 - i, exit);
  3396. ret = wc_Shake128_Final(&sha, hash, (word32)test_sha[i].outLen);
  3397. if (ret != 0)
  3398. ERROR_OUT(-3102 - i, exit);
  3399. if (XMEMCMP(hash, test_sha[i].output, test_sha[i].outLen) != 0)
  3400. ERROR_OUT(-3103 - i, exit);
  3401. }
  3402. #ifndef NO_LARGE_HASH_TEST
  3403. /* BEGIN LARGE HASH TEST */ {
  3404. for (i = 0; i < (int)sizeof(large_input); i++) {
  3405. large_input[i] = (byte)(i & 0xFF);
  3406. }
  3407. times = 100;
  3408. for (i = 0; i < times; ++i) {
  3409. ret = wc_Shake128_Update(&sha, (byte*)large_input,
  3410. (word32)sizeof(large_input));
  3411. if (ret != 0)
  3412. ERROR_OUT(-3104, exit);
  3413. }
  3414. ret = wc_Shake128_Final(&sha, hash, (word32)sizeof(hash));
  3415. if (ret != 0)
  3416. ERROR_OUT(-3105, exit);
  3417. if (XMEMCMP(hash, large_digest, 114) != 0)
  3418. ERROR_OUT(-3106, exit);
  3419. } /* END LARGE HASH TEST */
  3420. #endif /* NO_LARGE_HASH_TEST */
  3421. ret = shake128_absorb_test(&sha);
  3422. exit:
  3423. wc_Shake128_Free(&sha);
  3424. return ret;
  3425. }
  3426. #endif
  3427. #ifdef WOLFSSL_SHAKE256
  3428. static int shake256_absorb_test(wc_Shake* sha, byte *large_input_buf,
  3429. size_t large_input_buf_size)
  3430. {
  3431. byte hash[WC_SHA3_256_BLOCK_SIZE*2];
  3432. testVector a, b, c, d, e;
  3433. testVector test_sha[5];
  3434. int ret = 0;
  3435. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  3436. const char* large_digest =
  3437. "\x21\x25\x8e\xae\x6e\x4f\xa7\xe1\xb9\x6d\xa7\xc9\x7d\x46\x03\x69"
  3438. "\x29\x0d\x81\x49\xba\x5d\xaf\x37\xfd\xeb\x25\x52\x1d\xd9\xbd\x65"
  3439. "\xfa\x99\xb9\xd1\x70\x6b\xeb\xd4\xc1\x2c\xea\x24\x20\x27\xa7\xcd"
  3440. "\xfa\xe1\x81\xd9\xd5\xc1\x1c\xc7\xe9\x70\xc3\xc7\x21\x6f\x32\x22"
  3441. "\xe3\x27\xdb\x58\x5e\xea\x18\x2d\x63\x4d\x14\x6c\x94\xcf\x2b\x7e"
  3442. "\x6e\x2a\x74\xf3\xe0\xac\xb3\xb2\xcc\xef\x38\xe9\xe7\x35\xb3\xc5"
  3443. "\x77\x9d\xff\xe3\x08\x8e\xf8\x2c\x89\xbb\x45\x22\x16\x99\x91\xc0"
  3444. "\xe7\x71\x57\x75\xc5\xb1\xc6\xaf\x27\xcb\x64\x8c\xc4\xee\x3d\x5f"
  3445. "\x4c\x35\xfb\x1c\xf3\xf8\x0e\xfd\x5e\xfc\x07\xd8\x4d\x55\x32\x49"
  3446. "\x45\x0d\xab\x4a\x49\xc4\x83\xde\xd2\x50\xc9\x33\x8f\x85\xcd\x93"
  3447. "\x7a\xe6\x6b\xb4\x36\xf3\xb4\x02\x6e\x85\x9f\xda\x1c\xa5\x71\x43"
  3448. "\x2f\x3b\xfc\x09\xe7\xc0\x3c\xa4\xd1\x83\xb7\x41\x11\x1c\xa0\x48"
  3449. "\x3d\x0e\xda\xbc\x03\xfe\xb2\x3b\x17\xee\x48\xe8\x44\xba\x24\x08"
  3450. "\xd9\xdc\xfd\x01\x39\xd2\xe8\xc7\x31\x01\x25\xae\xe8\x01\xc6\x1a"
  3451. "\xb7\x90\x0d\x1e\xfc\x47\xc0\x78\x28\x17\x66\xf3\x61\xc5\xe6\x11"
  3452. "\x13\x46\x23\x5e\x1d\xc3\x83\x25\x66\x6c\x68\x1b\x30\xdd\xc4\xe6"
  3453. "\x83\x8b\x0f\x23\x58\x7e\x06\x5f\x4a\x2b\xed\xc9\x6c\x97\x68\x44";
  3454. a.input = "";
  3455. a.output = "\x46\xb9\xdd\x2b\x0b\xa8\x8d\x13\x23\x3b\x3f\xeb\x74\x3e\xeb"
  3456. "\x24\x3f\xcd\x52\xea\x62\xb8\x1b\x82\xb5\x0c\x27\x64\x6e\xd5"
  3457. "\x76\x2f\xd7\x5d\xc4\xdd\xd8\xc0\xf2\x00\xcb\x05\x01\x9d\x67"
  3458. "\xb5\x92\xf6\xfc\x82\x1c\x49\x47\x9a\xb4\x86\x40\x29\x2e\xac"
  3459. "\xb3\xb7\xc4\xbe\x14\x1e\x96\x61\x6f\xb1\x39\x57\x69\x2c\xc7"
  3460. "\xed\xd0\xb4\x5a\xe3\xdc\x07\x22\x3c\x8e\x92\x93\x7b\xef\x84"
  3461. "\xbc\x0e\xab\x86\x28\x53\x34\x9e\xc7\x55\x46\xf5\x8f\xb7\xc2"
  3462. "\x77\x5c\x38\x46\x2c\x50\x10\xd8\x46\xc1\x85\xc1\x51\x11\xe5"
  3463. "\x95\x52\x2a\x6b\xcd\x16\xcf\x86\xf3\xd1\x22\x10\x9e\x3b\x1f"
  3464. "\xdd";
  3465. a.inLen = XSTRLEN(a.input);
  3466. a.outLen = WC_SHA3_256_BLOCK_SIZE;
  3467. b.input = "abc";
  3468. b.output = "\x48\x33\x66\x60\x13\x60\xa8\x77\x1c\x68\x63\x08\x0c\xc4\x11"
  3469. "\x4d\x8d\xb4\x45\x30\xf8\xf1\xe1\xee\x4f\x94\xea\x37\xe7\x8b"
  3470. "\x57\x39\xd5\xa1\x5b\xef\x18\x6a\x53\x86\xc7\x57\x44\xc0\x52"
  3471. "\x7e\x1f\xaa\x9f\x87\x26\xe4\x62\xa1\x2a\x4f\xeb\x06\xbd\x88"
  3472. "\x01\xe7\x51\xe4\x13\x85\x14\x12\x04\xf3\x29\x97\x9f\xd3\x04"
  3473. "\x7a\x13\xc5\x65\x77\x24\xad\xa6\x4d\x24\x70\x15\x7b\x3c\xdc"
  3474. "\x28\x86\x20\x94\x4d\x78\xdb\xcd\xdb\xd9\x12\x99\x3f\x09\x13"
  3475. "\xf1\x64\xfb\x2c\xe9\x51\x31\xa2\xd0\x9a\x3e\x6d\x51\xcb\xfc"
  3476. "\x62\x27\x20\xd7\xa7\x5c\x63\x34\xe8\xa2\xd7\xec\x71\xa7\xcc"
  3477. "\x29";
  3478. b.inLen = XSTRLEN(b.input);
  3479. b.outLen = WC_SHA3_256_BLOCK_SIZE;
  3480. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  3481. c.output = "\x4d\x8c\x2d\xd2\x43\x5a\x01\x28\xee\xfb\xb8\xc3\x6f\x6f\x87"
  3482. "\x13\x3a\x79\x11\xe1\x8d\x97\x9e\xe1\xae\x6b\xe5\xd4\xfd\x2e"
  3483. "\x33\x29\x40\xd8\x68\x8a\x4e\x6a\x59\xaa\x80\x60\xf1\xf9\xbc"
  3484. "\x99\x6c\x05\xac\xa3\xc6\x96\xa8\xb6\x62\x79\xdc\x67\x2c\x74"
  3485. "\x0b\xb2\x24\xec\x37\xa9\x2b\x65\xdb\x05\x39\xc0\x20\x34\x55"
  3486. "\xf5\x1d\x97\xcc\xe4\xcf\xc4\x91\x27\xd7\x26\x0a\xfc\x67\x3a"
  3487. "\xf2\x08\xba\xf1\x9b\xe2\x12\x33\xf3\xde\xbe\x78\xd0\x67\x60"
  3488. "\xcf\xa5\x51\xee\x1e\x07\x91\x41\xd4\x9d\xd3\xef\x7e\x18\x2b"
  3489. "\x15\x24\xdf\x82\xea\x1c\xef\xe1\xc6\xc3\x96\x61\x75\xf0\x22"
  3490. "\x8d";
  3491. c.inLen = XSTRLEN(c.input);
  3492. c.outLen = WC_SHA3_256_BLOCK_SIZE;
  3493. /* Taken from NIST CAVP test vectors - full rate output. */
  3494. d.input = "\xdc\x88\x6d\xf3\xf6\x9c\x49\x51\x3d\xe3\x62\x7e\x94\x81\xdb"
  3495. "\x58\x71\xe8\xee\x88\xeb\x9f\x99\x61\x15\x41\x93\x0a\x8b\xc8"
  3496. "\x85\xe0";
  3497. d.output = "\x00\x64\x8a\xfb\xc5\xe6\x51\x64\x9d\xb1\xfd\x82\x93\x6b\x00"
  3498. "\xdb\xbc\x12\x2f\xb4\xc8\x77\x86\x0d\x38\x5c\x49\x50\xd5\x6d"
  3499. "\xe7\xe0\x96\xd6\x13\xd7\xa3\xf2\x7e\xd8\xf2\x63\x34\xb0\xcc"
  3500. "\xc1\x40\x7b\x41\xdc\xcb\x23\xdf\xaa\x52\x98\x18\xd1\x12\x5c"
  3501. "\xd5\x34\x80\x92\x52\x43\x66\xb8\x5f\xab\xb9\x7c\x6c\xd1\xe6"
  3502. "\x06\x6f\x45\x9b\xcc\x56\x6d\xa8\x7e\xc9\xb7\xba\x36\x79\x2d"
  3503. "\x11\x8a\xc3\x9a\x4c\xce\xf6\x19\x2b\xbf\x3a\x54\xaf\x18\xe5"
  3504. "\x7b\x0c\x14\x61\x01\xf6\xae\xaa\x82\x2b\xc4\xb4\xc9\x70\x8b"
  3505. "\x09\xf0\xb3\xba\xb4\x1b\xcc\xe9\x64\xd9\x99\xd1\x10\x7b\xd7"
  3506. "\xc2";
  3507. d.inLen = 32;
  3508. d.outLen = WC_SHA3_256_BLOCK_SIZE;
  3509. /* Taken from NIST CAVP test vectors - more than one output block. */
  3510. e.input = "\x8d\x80\x01\xe2\xc0\x96\xf1\xb8\x8e\x7c\x92\x24\xa0\x86\xef"
  3511. "\xd4\x79\x7f\xbf\x74\xa8\x03\x3a\x2d\x42\x2a\x2b\x6b\x8f\x67"
  3512. "\x47\xe4";
  3513. e.output = "\x2e\x97\x5f\x6a\x8a\x14\xf0\x70\x4d\x51\xb1\x36\x67\xd8\x19"
  3514. "\x5c\x21\x9f\x71\xe6\x34\x56\x96\xc4\x9f\xa4\xb9\xd0\x8e\x92"
  3515. "\x25\xd3\xd3\x93\x93\x42\x51\x52\xc9\x7e\x71\xdd\x24\x60\x1c"
  3516. "\x11\xab\xcf\xa0\xf1\x2f\x53\xc6\x80\xbd\x3a\xe7\x57\xb8\x13"
  3517. "\x4a\x9c\x10\xd4\x29\x61\x58\x69\x21\x7f\xdd\x58\x85\xc4\xdb"
  3518. "\x17\x49\x85\x70\x3a\x6d\x6d\xe9\x4a\x66\x7e\xac\x30\x23\x44"
  3519. "\x3a\x83\x37\xae\x1b\xc6\x01\xb7\x6d\x7d\x38\xec\x3c\x34\x46"
  3520. "\x31\x05\xf0\xd3\x94\x9d\x78\xe5\x62\xa0\x39\xe4\x46\x95\x48"
  3521. "\xb6\x09\x39\x5d\xe5\xa4\xfd\x43\xc4\x6c\xa9\xfd\x6e\xe2\x9a"
  3522. "\xda\x5e\xfc\x07\xd8\x4d\x55\x32\x49\x45\x0d\xab\x4a\x49\xc4"
  3523. "\x83\xde\xd2\x50\xc9\x33\x8f\x85\xcd\x93\x7a\xe6\x6b\xb4\x36"
  3524. "\xf3\xb4\x02\x6e\x85\x9f\xda\x1c\xa5\x71\x43\x2f\x3b\xfc\x09"
  3525. "\xe7\xc0\x3c\xa4\xd1\x83\xb7\x41\x11\x1c\xa0\x48\x3d\x0e\xda"
  3526. "\xbc\x03\xfe\xb2\x3b\x17\xee\x48\xe8\x44\xba\x24\x08\xd9\xdc"
  3527. "\xfd\x01\x39\xd2\xe8\xc7\x31\x01\x25\xae\xe8\x01\xc6\x1a\xb7"
  3528. "\x90\x0d\x1e\xfc\x47\xc0\x78\x28\x17\x66\xf3\x61\xc5\xe6\x11"
  3529. "\x13\x46\x23\x5e\x1d\xc3\x83\x25\x66\x6c\x68\x1b\x30\xdd\xc4"
  3530. "\xe6\x83\x8b\x0f\x23\x58\x7e\x06\x5f\x4a\x2b\xed\xc9\x6c\x97"
  3531. "\x68\x44";
  3532. e.inLen = 32;
  3533. e.outLen = 2 * WC_SHA3_256_BLOCK_SIZE;
  3534. test_sha[0] = a;
  3535. test_sha[1] = b;
  3536. test_sha[2] = c;
  3537. test_sha[3] = d;
  3538. test_sha[4] = e;
  3539. for (i = 0; i < times; ++i) {
  3540. ret = wc_InitShake256(sha, HEAP_HINT, devId);
  3541. if (ret != 0)
  3542. ERROR_OUT(-3100 - i, exit);
  3543. ret = wc_Shake256_Absorb(sha, (byte*)test_sha[i].input,
  3544. (word32)test_sha[i].inLen);
  3545. if (ret != 0)
  3546. ERROR_OUT(-3101 - i, exit);
  3547. ret = wc_Shake256_SqueezeBlocks(sha, hash,
  3548. (word32)test_sha[i].outLen / WC_SHA3_256_BLOCK_SIZE);
  3549. if (ret != 0)
  3550. ERROR_OUT(-3102 - i, exit);
  3551. if (XMEMCMP(hash, test_sha[i].output, (word32)test_sha[i].outLen) != 0)
  3552. ERROR_OUT(-3103 - i, exit);
  3553. }
  3554. #ifndef NO_LARGE_HASH_TEST
  3555. /* BEGIN LARGE HASH TEST */ {
  3556. for (i = 0; i < (int)large_input_buf_size; i++) {
  3557. large_input_buf[i] = (byte)(i & 0xFF);
  3558. }
  3559. ret = wc_InitShake256(sha, HEAP_HINT, devId);
  3560. if (ret != 0)
  3561. ERROR_OUT(-3104, exit);
  3562. /* Absorb is non-incremental. */
  3563. ret = wc_Shake256_Absorb(sha, large_input_buf,
  3564. (word32)large_input_buf_size);
  3565. if (ret != 0)
  3566. ERROR_OUT(-3105, exit);
  3567. /* Able to squeeze out blocks incrementally. */
  3568. ret = wc_Shake256_SqueezeBlocks(sha, hash, 1);
  3569. if (ret != 0)
  3570. ERROR_OUT(-3106, exit);
  3571. ret = wc_Shake256_SqueezeBlocks(sha, hash,
  3572. ((word32)sizeof(hash) / WC_SHA3_256_BLOCK_SIZE) - 1);
  3573. if (ret != 0)
  3574. ERROR_OUT(-3106, exit);
  3575. if (XMEMCMP(hash, large_digest, sizeof(hash)) != 0)
  3576. ERROR_OUT(-3107, exit);
  3577. } /* END LARGE HASH TEST */
  3578. #endif /* NO_LARGE_HASH_TEST */
  3579. exit:
  3580. return ret;
  3581. }
  3582. WOLFSSL_TEST_SUBROUTINE int shake256_test(void)
  3583. {
  3584. wc_Shake sha;
  3585. byte hash[250];
  3586. testVector a, b, c, d, e;
  3587. testVector test_sha[5];
  3588. int ret = 0;
  3589. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  3590. #define SHAKE256_LARGE_INPUT_BUFSIZ 1024
  3591. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  3592. byte *large_input = NULL;
  3593. #else
  3594. byte large_input[SHAKE256_LARGE_INPUT_BUFSIZ];
  3595. #endif
  3596. const char* large_digest =
  3597. "\x90\x32\x4a\xcc\xd1\xdf\xb8\x0b\x79\x1f\xb8\xc8\x5b\x54\xc8\xe7"
  3598. "\x45\xf5\x60\x6b\x38\x26\xb2\x0a\xee\x38\x01\xf3\xd9\xfa\x96\x9f"
  3599. "\x6a\xd7\x15\xdf\xb6\xc2\xf4\x20\x33\x44\x55\xe8\x2a\x09\x2b\x68"
  3600. "\x2e\x18\x65\x5e\x65\x93\x28\xbc\xb1\x9e\xe2\xb1\x92\xea\x98\xac"
  3601. "\x21\xef\x4c\xe1\xb4\xb7\xbe\x81\x5c\x1d\xd3\xb7\x17\xe5\xbb\xc5"
  3602. "\x8c\x68\xb7\xfb\xac\x55\x8a\x9b\x4d\x91\xe4\x9f\x72\xbb\x6e\x38"
  3603. "\xaf\x21\x7d\x21\xaa\x98\x4e\x75\xc4\xb4\x1c\x7c\x50\x45\x54\xf9"
  3604. "\xea\x26";
  3605. a.input = "";
  3606. a.output = "\x46\xb9\xdd\x2b\x0b\xa8\x8d\x13\x23\x3b\x3f\xeb\x74\x3e\xeb"
  3607. "\x24\x3f\xcd\x52\xea\x62\xb8\x1b\x82\xb5\x0c\x27\x64\x6e\xd5"
  3608. "\x76\x2f\xd7\x5d\xc4\xdd\xd8\xc0\xf2\x00\xcb\x05\x01\x9d\x67"
  3609. "\xb5\x92\xf6\xfc\x82\x1c\x49\x47\x9a\xb4\x86\x40\x29\x2e\xac"
  3610. "\xb3\xb7\xc4\xbe\x14\x1e\x96\x61\x6f\xb1\x39\x57\x69\x2c\xc7"
  3611. "\xed\xd0\xb4\x5a\xe3\xdc\x07\x22\x3c\x8e\x92\x93\x7b\xef\x84"
  3612. "\xbc\x0e\xab\x86\x28\x53\x34\x9e\xc7\x55\x46\xf5\x8f\xb7\xc2"
  3613. "\x77\x5c\x38\x46\x2c\x50\x10\xd8\x46";
  3614. a.inLen = XSTRLEN(a.input);
  3615. a.outLen = 114;
  3616. b.input = "abc";
  3617. b.output = "\x48\x33\x66\x60\x13\x60\xa8\x77\x1c\x68\x63\x08\x0c\xc4\x11"
  3618. "\x4d\x8d\xb4\x45\x30\xf8\xf1\xe1\xee\x4f\x94\xea\x37\xe7\x8b"
  3619. "\x57\x39\xd5\xa1\x5b\xef\x18\x6a\x53\x86\xc7\x57\x44\xc0\x52"
  3620. "\x7e\x1f\xaa\x9f\x87\x26\xe4\x62\xa1\x2a\x4f\xeb\x06\xbd\x88"
  3621. "\x01\xe7\x51\xe4\x13\x85\x14\x12\x04\xf3\x29\x97\x9f\xd3\x04"
  3622. "\x7a\x13\xc5\x65\x77\x24\xad\xa6\x4d\x24\x70\x15\x7b\x3c\xdc"
  3623. "\x28\x86\x20\x94\x4d\x78\xdb\xcd\xdb\xd9\x12\x99\x3f\x09\x13"
  3624. "\xf1\x64\xfb\x2c\xe9\x51\x31\xa2\xd0";
  3625. b.inLen = XSTRLEN(b.input);
  3626. b.outLen = 114;
  3627. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  3628. c.output = "\x4d\x8c\x2d\xd2\x43\x5a\x01\x28\xee\xfb\xb8\xc3\x6f\x6f\x87"
  3629. "\x13\x3a\x79\x11\xe1\x8d\x97\x9e\xe1\xae\x6b\xe5\xd4\xfd\x2e"
  3630. "\x33\x29\x40\xd8\x68\x8a\x4e\x6a\x59\xaa\x80\x60\xf1\xf9\xbc"
  3631. "\x99\x6c\x05\xac\xa3\xc6\x96\xa8\xb6\x62\x79\xdc\x67\x2c\x74"
  3632. "\x0b\xb2\x24\xec\x37\xa9\x2b\x65\xdb\x05\x39\xc0\x20\x34\x55"
  3633. "\xf5\x1d\x97\xcc\xe4\xcf\xc4\x91\x27\xd7\x26\x0a\xfc\x67\x3a"
  3634. "\xf2\x08\xba\xf1\x9b\xe2\x12\x33\xf3\xde\xbe\x78\xd0\x67\x60"
  3635. "\xcf\xa5\x51\xee\x1e\x07\x91\x41\xd4";
  3636. c.inLen = XSTRLEN(c.input);
  3637. c.outLen = 114;
  3638. /* Taken from NIST CAVP test vectors - full rate output. */
  3639. d.input = "\xdc\x88\x6d\xf3\xf6\x9c\x49\x51\x3d\xe3\x62\x7e\x94\x81\xdb"
  3640. "\x58\x71\xe8\xee\x88\xeb\x9f\x99\x61\x15\x41\x93\x0a\x8b\xc8"
  3641. "\x85\xe0";
  3642. d.output = "\x00\x64\x8a\xfb\xc5\xe6\x51\x64\x9d\xb1\xfd\x82\x93\x6b\x00"
  3643. "\xdb\xbc\x12\x2f\xb4\xc8\x77\x86\x0d\x38\x5c\x49\x50\xd5\x6d"
  3644. "\xe7\xe0\x96\xd6\x13\xd7\xa3\xf2\x7e\xd8\xf2\x63\x34\xb0\xcc"
  3645. "\xc1\x40\x7b\x41\xdc\xcb\x23\xdf\xaa\x52\x98\x18\xd1\x12\x5c"
  3646. "\xd5\x34\x80\x92\x52\x43\x66\xb8\x5f\xab\xb9\x7c\x6c\xd1\xe6"
  3647. "\x06\x6f\x45\x9b\xcc\x56\x6d\xa8\x7e\xc9\xb7\xba\x36\x79\x2d"
  3648. "\x11\x8a\xc3\x9a\x4c\xce\xf6\x19\x2b\xbf\x3a\x54\xaf\x18\xe5"
  3649. "\x7b\x0c\x14\x61\x01\xf6\xae\xaa\x82\x2b\xc4\xb4\xc9\x70\x8b"
  3650. "\x09\xf0\xb3\xba\xb4\x1b\xcc\xe9\x64\xd9\x99\xd1\x10\x7b\xd7"
  3651. "\xc2";
  3652. d.inLen = 32;
  3653. d.outLen = 136;
  3654. /* Taken from NIST CAVP test vectors - more than one output block. */
  3655. e.input = "\x8d\x80\x01\xe2\xc0\x96\xf1\xb8\x8e\x7c\x92\x24\xa0\x86\xef"
  3656. "\xd4\x79\x7f\xbf\x74\xa8\x03\x3a\x2d\x42\x2a\x2b\x6b\x8f\x67"
  3657. "\x47\xe4";
  3658. e.output = "\x2e\x97\x5f\x6a\x8a\x14\xf0\x70\x4d\x51\xb1\x36\x67\xd8\x19"
  3659. "\x5c\x21\x9f\x71\xe6\x34\x56\x96\xc4\x9f\xa4\xb9\xd0\x8e\x92"
  3660. "\x25\xd3\xd3\x93\x93\x42\x51\x52\xc9\x7e\x71\xdd\x24\x60\x1c"
  3661. "\x11\xab\xcf\xa0\xf1\x2f\x53\xc6\x80\xbd\x3a\xe7\x57\xb8\x13"
  3662. "\x4a\x9c\x10\xd4\x29\x61\x58\x69\x21\x7f\xdd\x58\x85\xc4\xdb"
  3663. "\x17\x49\x85\x70\x3a\x6d\x6d\xe9\x4a\x66\x7e\xac\x30\x23\x44"
  3664. "\x3a\x83\x37\xae\x1b\xc6\x01\xb7\x6d\x7d\x38\xec\x3c\x34\x46"
  3665. "\x31\x05\xf0\xd3\x94\x9d\x78\xe5\x62\xa0\x39\xe4\x46\x95\x48"
  3666. "\xb6\x09\x39\x5d\xe5\xa4\xfd\x43\xc4\x6c\xa9\xfd\x6e\xe2\x9a"
  3667. "\xda\x5e\xfc\x07\xd8\x4d\x55\x32\x49\x45\x0d\xab\x4a\x49\xc4"
  3668. "\x83\xde\xd2\x50\xc9\x33\x8f\x85\xcd\x93\x7a\xe6\x6b\xb4\x36"
  3669. "\xf3\xb4\x02\x6e\x85\x9f\xda\x1c\xa5\x71\x43\x2f\x3b\xfc\x09"
  3670. "\xe7\xc0\x3c\xa4\xd1\x83\xb7\x41\x11\x1c\xa0\x48\x3d\x0e\xda"
  3671. "\xbc\x03\xfe\xb2\x3b\x17\xee\x48\xe8\x44\xba\x24\x08\xd9\xdc"
  3672. "\xfd\x01\x39\xd2\xe8\xc7\x31\x01\x25\xae\xe8\x01\xc6\x1a\xb7"
  3673. "\x90\x0d\x1e\xfc\x47\xc0\x78\x28\x17\x66\xf3\x61\xc5\xe6\x11"
  3674. "\x13\x46\x23\x5e\x1d\xc3\x83\x25\x66\x6c";
  3675. e.inLen = 32;
  3676. e.outLen = 250;
  3677. test_sha[0] = a;
  3678. test_sha[1] = b;
  3679. test_sha[2] = c;
  3680. test_sha[3] = d;
  3681. test_sha[4] = e;
  3682. ret = wc_InitShake256(&sha, HEAP_HINT, devId);
  3683. if (ret != 0)
  3684. return -3100;
  3685. for (i = 0; i < times; ++i) {
  3686. ret = wc_Shake256_Update(&sha, (byte*)test_sha[i].input,
  3687. (word32)test_sha[i].inLen);
  3688. if (ret != 0)
  3689. ERROR_OUT(-3101 - i, exit);
  3690. ret = wc_Shake256_Final(&sha, hash, (word32)test_sha[i].outLen);
  3691. if (ret != 0)
  3692. ERROR_OUT(-3102 - i, exit);
  3693. if (XMEMCMP(hash, test_sha[i].output, test_sha[i].outLen) != 0)
  3694. ERROR_OUT(-3103 - i, exit);
  3695. }
  3696. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  3697. large_input = (byte *)XMALLOC(SHAKE256_LARGE_INPUT_BUFSIZ, NULL,
  3698. DYNAMIC_TYPE_TMP_BUFFER);
  3699. if (large_input == NULL)
  3700. ERROR_OUT(-3107, exit);
  3701. #endif
  3702. #ifndef NO_LARGE_HASH_TEST
  3703. /* BEGIN LARGE HASH TEST */ {
  3704. for (i = 0; i < SHAKE256_LARGE_INPUT_BUFSIZ; i++) {
  3705. large_input[i] = (byte)(i & 0xFF);
  3706. }
  3707. times = 100;
  3708. for (i = 0; i < times; ++i) {
  3709. ret = wc_Shake256_Update(&sha, (byte*)large_input,
  3710. SHAKE256_LARGE_INPUT_BUFSIZ);
  3711. if (ret != 0)
  3712. ERROR_OUT(-3104, exit);
  3713. }
  3714. ret = wc_Shake256_Final(&sha, hash, (word32)sizeof(hash));
  3715. if (ret != 0)
  3716. ERROR_OUT(-3105, exit);
  3717. if (XMEMCMP(hash, large_digest, 114) != 0)
  3718. ERROR_OUT(-3106, exit);
  3719. } /* END LARGE HASH TEST */
  3720. #endif /* NO_LARGE_HASH_TEST */
  3721. ret = shake256_absorb_test(&sha, large_input, SHAKE256_LARGE_INPUT_BUFSIZ);
  3722. exit:
  3723. wc_Shake256_Free(&sha);
  3724. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  3725. if (large_input != NULL)
  3726. XFREE(large_input, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  3727. #endif
  3728. return ret;
  3729. }
  3730. #endif
  3731. #ifndef NO_HASH_WRAPPER
  3732. WOLFSSL_TEST_SUBROUTINE int hash_test(void)
  3733. {
  3734. wc_HashAlg hash;
  3735. int ret, exp_ret;
  3736. int i, j;
  3737. int digestSz;
  3738. byte data[] = "0123456789abcdef0123456789abcdef0123456";
  3739. byte out[WC_MAX_DIGEST_SIZE];
  3740. byte hashOut[WC_MAX_DIGEST_SIZE];
  3741. #if !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC)
  3742. enum wc_HashType hashType;
  3743. #endif
  3744. enum wc_HashType typesGood[] = { WC_HASH_TYPE_MD5, WC_HASH_TYPE_SHA,
  3745. WC_HASH_TYPE_SHA224, WC_HASH_TYPE_SHA256,
  3746. WC_HASH_TYPE_SHA384, WC_HASH_TYPE_SHA512,
  3747. WC_HASH_TYPE_SHA3_224,
  3748. WC_HASH_TYPE_SHA3_256,
  3749. WC_HASH_TYPE_SHA3_384,
  3750. WC_HASH_TYPE_SHA3_512 };
  3751. enum wc_HashType typesNoImpl[] = {
  3752. #ifdef NO_MD5
  3753. WC_HASH_TYPE_MD5,
  3754. #endif
  3755. #ifdef NO_SHA
  3756. WC_HASH_TYPE_SHA,
  3757. #endif
  3758. #ifndef WOLFSSL_SHA224
  3759. WC_HASH_TYPE_SHA224,
  3760. #endif
  3761. #ifdef NO_SHA256
  3762. WC_HASH_TYPE_SHA256,
  3763. #endif
  3764. #ifndef WOLFSSL_SHA384
  3765. WC_HASH_TYPE_SHA384,
  3766. #endif
  3767. #ifndef WOLFSSL_SHA512
  3768. WC_HASH_TYPE_SHA512,
  3769. #endif
  3770. #if !defined(WOLFSSL_SHA3) || defined(WOLFSSL_NOSHA3_224)
  3771. WC_HASH_TYPE_SHA3_224,
  3772. #endif
  3773. #if !defined(WOLFSSL_SHA3) || defined(WOLFSSL_NOSHA3_256)
  3774. WC_HASH_TYPE_SHA3_256,
  3775. #endif
  3776. #if !defined(WOLFSSL_SHA3) || defined(WOLFSSL_NOSHA3_384)
  3777. WC_HASH_TYPE_SHA3_384,
  3778. #endif
  3779. #if !defined(WOLFSSL_SHA3) || defined(WOLFSSL_NOSHA3_512)
  3780. WC_HASH_TYPE_SHA3_512,
  3781. #endif
  3782. WC_HASH_TYPE_NONE
  3783. };
  3784. enum wc_HashType typesBad[] = { WC_HASH_TYPE_NONE, WC_HASH_TYPE_MD5_SHA,
  3785. WC_HASH_TYPE_MD2, WC_HASH_TYPE_MD4 };
  3786. enum wc_HashType typesHashBad[] = { WC_HASH_TYPE_MD2, WC_HASH_TYPE_MD4,
  3787. WC_HASH_TYPE_BLAKE2B,
  3788. WC_HASH_TYPE_NONE };
  3789. /* Parameter Validation testing. */
  3790. ret = wc_HashInit(NULL, WC_HASH_TYPE_SHA256);
  3791. if (ret != BAD_FUNC_ARG)
  3792. return -3200;
  3793. ret = wc_HashUpdate(NULL, WC_HASH_TYPE_SHA256, NULL, sizeof(data));
  3794. if (ret != BAD_FUNC_ARG)
  3795. return -3201;
  3796. ret = wc_HashUpdate(&hash, WC_HASH_TYPE_SHA256, NULL, sizeof(data));
  3797. if (ret != BAD_FUNC_ARG)
  3798. return -3202;
  3799. ret = wc_HashUpdate(NULL, WC_HASH_TYPE_SHA256, data, sizeof(data));
  3800. if (ret != BAD_FUNC_ARG)
  3801. return -3203;
  3802. ret = wc_HashFinal(NULL, WC_HASH_TYPE_SHA256, NULL);
  3803. if (ret != BAD_FUNC_ARG)
  3804. return -3204;
  3805. ret = wc_HashFinal(&hash, WC_HASH_TYPE_SHA256, NULL);
  3806. if (ret != BAD_FUNC_ARG)
  3807. return -3205;
  3808. ret = wc_HashFinal(NULL, WC_HASH_TYPE_SHA256, out);
  3809. if (ret != BAD_FUNC_ARG)
  3810. return -3206;
  3811. /* Try invalid hash algorithms. */
  3812. for (i = 0; i < (int)(sizeof(typesBad)/sizeof(*typesBad)); i++) {
  3813. ret = wc_HashInit(&hash, typesBad[i]);
  3814. if (ret != BAD_FUNC_ARG)
  3815. return -3207 - i;
  3816. ret = wc_HashUpdate(&hash, typesBad[i], data, sizeof(data));
  3817. if (ret != BAD_FUNC_ARG)
  3818. return -3217 - i;
  3819. ret = wc_HashFinal(&hash, typesBad[i], out);
  3820. if (ret != BAD_FUNC_ARG)
  3821. return -3227 - i;
  3822. wc_HashFree(&hash, typesBad[i]);
  3823. }
  3824. /* Try valid hash algorithms. */
  3825. for (i = 0, j = 0; i < (int)(sizeof(typesGood)/sizeof(*typesGood)); i++) {
  3826. exp_ret = 0;
  3827. if (typesGood[i] == typesNoImpl[j]) {
  3828. /* Recognized but no implementation compiled in. */
  3829. exp_ret = HASH_TYPE_E;
  3830. j++;
  3831. }
  3832. ret = wc_HashInit(&hash, typesGood[i]);
  3833. if (ret != exp_ret)
  3834. return -3237 - i;
  3835. ret = wc_HashUpdate(&hash, typesGood[i], data, sizeof(data));
  3836. if (ret != exp_ret)
  3837. return -3247 - i;
  3838. ret = wc_HashFinal(&hash, typesGood[i], out);
  3839. if (ret != exp_ret)
  3840. return -3257 - i;
  3841. wc_HashFree(&hash, typesGood[i]);
  3842. digestSz = wc_HashGetDigestSize(typesGood[i]);
  3843. if (exp_ret < 0 && digestSz != exp_ret)
  3844. return -3267 - i;
  3845. if (exp_ret == 0 && digestSz < 0)
  3846. return -3277 - i;
  3847. if (exp_ret == 0) {
  3848. ret = wc_Hash(typesGood[i], data, sizeof(data), hashOut,
  3849. digestSz - 1);
  3850. if (ret != BUFFER_E)
  3851. return -3287 - i;
  3852. }
  3853. ret = wc_Hash(typesGood[i], data, sizeof(data), hashOut, digestSz);
  3854. if (ret != exp_ret)
  3855. return -3297 - i;
  3856. if (exp_ret == 0 && XMEMCMP(out, hashOut, digestSz) != 0)
  3857. return -3307 -i;
  3858. ret = wc_HashGetBlockSize(typesGood[i]);
  3859. if (exp_ret < 0 && ret != exp_ret)
  3860. return -3308 - i;
  3861. if (exp_ret == 0 && ret < 0)
  3862. return -3318 - i;
  3863. #if !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC)
  3864. ret = wc_HashGetOID(typesGood[i]);
  3865. if (ret == BAD_FUNC_ARG ||
  3866. (exp_ret == 0 && ret == HASH_TYPE_E) ||
  3867. (exp_ret != 0 && ret != HASH_TYPE_E)) {
  3868. return -3328 - i;
  3869. }
  3870. hashType = wc_OidGetHash(ret);
  3871. if (exp_ret == 0 && hashType != typesGood[i])
  3872. return -3338 - i;
  3873. #endif /* !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC) */
  3874. }
  3875. for (i = 0; i < (int)(sizeof(typesHashBad)/sizeof(*typesHashBad)); i++) {
  3876. ret = wc_Hash(typesHashBad[i], data, sizeof(data), out, sizeof(out));
  3877. if (ret != BAD_FUNC_ARG && ret != BUFFER_E)
  3878. return -3348 - i;
  3879. }
  3880. #if !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC)
  3881. ret = wc_HashGetOID(WC_HASH_TYPE_MD2);
  3882. #ifdef WOLFSSL_MD2
  3883. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  3884. return -3358;
  3885. #else
  3886. if (ret != HASH_TYPE_E)
  3887. return -3359;
  3888. #endif
  3889. hashType = wc_OidGetHash(646); /* Md2h */
  3890. #ifdef WOLFSSL_MD2
  3891. if (hashType != WC_HASH_TYPE_MD2)
  3892. return -3360;
  3893. #else
  3894. if (hashType != WC_HASH_TYPE_NONE)
  3895. return -3361;
  3896. #endif
  3897. ret = wc_HashGetOID(WC_HASH_TYPE_MD5_SHA);
  3898. #ifndef NO_MD5
  3899. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  3900. return -3362;
  3901. #else
  3902. if (ret != HASH_TYPE_E)
  3903. return -3363;
  3904. #endif
  3905. ret = wc_HashGetOID(WC_HASH_TYPE_MD4);
  3906. if (ret != BAD_FUNC_ARG)
  3907. return -3364;
  3908. ret = wc_HashGetOID(WC_HASH_TYPE_NONE);
  3909. if (ret != BAD_FUNC_ARG)
  3910. return -3365;
  3911. hashType = wc_OidGetHash(0);
  3912. if (hashType != WC_HASH_TYPE_NONE)
  3913. return -3366;
  3914. #endif /* !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC) */
  3915. ret = wc_HashGetBlockSize(WC_HASH_TYPE_MD2);
  3916. #ifdef WOLFSSL_MD2
  3917. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  3918. return -3367;
  3919. #else
  3920. if (ret != HASH_TYPE_E)
  3921. return -3368;
  3922. #endif
  3923. ret = wc_HashGetDigestSize(WC_HASH_TYPE_MD2);
  3924. #ifdef WOLFSSL_MD2
  3925. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  3926. return -3369;
  3927. #else
  3928. if (ret != HASH_TYPE_E)
  3929. return -3370;
  3930. #endif
  3931. ret = wc_HashGetBlockSize(WC_HASH_TYPE_MD4);
  3932. #ifndef NO_MD4
  3933. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  3934. return -3371;
  3935. #else
  3936. if (ret != HASH_TYPE_E)
  3937. return -3372;
  3938. #endif
  3939. ret = wc_HashGetDigestSize(WC_HASH_TYPE_MD4);
  3940. #ifndef NO_MD4
  3941. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  3942. return -3373;
  3943. #else
  3944. if (ret != HASH_TYPE_E)
  3945. return -3374;
  3946. #endif
  3947. ret = wc_HashGetBlockSize(WC_HASH_TYPE_MD5_SHA);
  3948. #if !defined(NO_MD5) && !defined(NO_SHA)
  3949. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  3950. return -3375;
  3951. #else
  3952. if (ret != HASH_TYPE_E)
  3953. return -3376;
  3954. #endif
  3955. ret = wc_HashGetBlockSize(WC_HASH_TYPE_BLAKE2B);
  3956. #if defined(HAVE_BLAKE2) || defined(HAVE_BLAKE2S)
  3957. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  3958. return -3377;
  3959. #else
  3960. if (ret != HASH_TYPE_E)
  3961. return -3378;
  3962. #endif
  3963. ret = wc_HashGetDigestSize(WC_HASH_TYPE_BLAKE2B);
  3964. #if defined(HAVE_BLAKE2) || defined(HAVE_BLAKE2S)
  3965. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  3966. return -3379;
  3967. #else
  3968. if (ret != HASH_TYPE_E)
  3969. return -3380;
  3970. #endif
  3971. ret = wc_HashGetBlockSize(WC_HASH_TYPE_NONE);
  3972. if (ret != BAD_FUNC_ARG)
  3973. return -3381;
  3974. ret = wc_HashGetDigestSize(WC_HASH_TYPE_NONE);
  3975. if (ret != BAD_FUNC_ARG)
  3976. return -3382;
  3977. #if !defined(NO_CERTS) && !defined(NO_ASN)
  3978. #if defined(WOLFSSL_MD2) && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  3979. ret = wc_GetCTC_HashOID(MD2);
  3980. if (ret == 0)
  3981. return -3383;
  3982. #endif
  3983. #ifndef NO_MD5
  3984. ret = wc_GetCTC_HashOID(WC_MD5);
  3985. if (ret == 0)
  3986. return -3384;
  3987. #endif
  3988. #ifndef NO_SHA
  3989. ret = wc_GetCTC_HashOID(WC_SHA);
  3990. if (ret == 0)
  3991. return -3385;
  3992. #endif
  3993. #ifdef WOLFSSL_SHA224
  3994. ret = wc_GetCTC_HashOID(WC_SHA224);
  3995. if (ret == 0)
  3996. return -3386;
  3997. #endif
  3998. #ifndef NO_SHA256
  3999. ret = wc_GetCTC_HashOID(WC_SHA256);
  4000. if (ret == 0)
  4001. return -3387;
  4002. #endif
  4003. #ifdef WOLFSSL_SHA384
  4004. ret = wc_GetCTC_HashOID(WC_SHA384);
  4005. if (ret == 0)
  4006. return -3388;
  4007. #endif
  4008. #ifdef WOLFSSL_SHA512
  4009. ret = wc_GetCTC_HashOID(WC_SHA512);
  4010. if (ret == 0)
  4011. return -3389;
  4012. #endif
  4013. ret = wc_GetCTC_HashOID(-1);
  4014. if (ret != 0)
  4015. return -3390;
  4016. #endif
  4017. return 0;
  4018. }
  4019. #endif /* !NO_HASH_WRAPPER */
  4020. #if !defined(NO_HMAC) && !defined(NO_MD5) && !(defined(HAVE_FIPS) && \
  4021. defined(HAVE_FIPS_VERSION) && \
  4022. (HAVE_FIPS_VERSION >= 5))
  4023. WOLFSSL_TEST_SUBROUTINE int hmac_md5_test(void)
  4024. {
  4025. Hmac hmac;
  4026. byte hash[WC_MD5_DIGEST_SIZE];
  4027. const char* keys[]=
  4028. {
  4029. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
  4030. "Jefe",
  4031. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  4032. };
  4033. testVector a, b, c;
  4034. testVector test_hmac[3];
  4035. int ret;
  4036. int times = sizeof(test_hmac) / sizeof(testVector), i;
  4037. a.input = "Hi There";
  4038. a.output = "\x92\x94\x72\x7a\x36\x38\xbb\x1c\x13\xf4\x8e\xf8\x15\x8b\xfc"
  4039. "\x9d";
  4040. a.inLen = XSTRLEN(a.input);
  4041. a.outLen = WC_MD5_DIGEST_SIZE;
  4042. b.input = "what do ya want for nothing?";
  4043. b.output = "\x75\x0c\x78\x3e\x6a\xb0\xb5\x03\xea\xa8\x6e\x31\x0a\x5d\xb7"
  4044. "\x38";
  4045. b.inLen = XSTRLEN(b.input);
  4046. b.outLen = WC_MD5_DIGEST_SIZE;
  4047. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4048. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4049. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4050. "\xDD\xDD\xDD\xDD\xDD\xDD";
  4051. c.output = "\x56\xbe\x34\x52\x1d\x14\x4c\x88\xdb\xb8\xc7\x33\xf0\xe8\xb3"
  4052. "\xf6";
  4053. c.inLen = XSTRLEN(c.input);
  4054. c.outLen = WC_MD5_DIGEST_SIZE;
  4055. test_hmac[0] = a;
  4056. test_hmac[1] = b;
  4057. test_hmac[2] = c;
  4058. for (i = 0; i < times; ++i) {
  4059. #if defined(HAVE_FIPS) || defined(HAVE_CAVIUM)
  4060. if (i == 1) {
  4061. continue; /* cavium can't handle short keys, fips not allowed */
  4062. }
  4063. #endif
  4064. if (wc_HmacInit(&hmac, HEAP_HINT, devId) != 0) {
  4065. return -3400;
  4066. }
  4067. ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys[i],
  4068. (word32)XSTRLEN(keys[i]));
  4069. if (ret != 0)
  4070. return -3401;
  4071. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  4072. (word32)test_hmac[i].inLen);
  4073. if (ret != 0)
  4074. return -3402;
  4075. ret = wc_HmacFinal(&hmac, hash);
  4076. if (ret != 0)
  4077. return -3403;
  4078. if (XMEMCMP(hash, test_hmac[i].output, WC_MD5_DIGEST_SIZE) != 0)
  4079. return -3404 - i;
  4080. wc_HmacFree(&hmac);
  4081. }
  4082. #ifndef HAVE_FIPS
  4083. if (wc_HmacSizeByType(WC_MD5) != WC_MD5_DIGEST_SIZE)
  4084. return -3414;
  4085. #endif
  4086. return 0;
  4087. }
  4088. #endif /* !NO_HMAC && !NO_MD5 && (!HAVE_FIPS || (HAVE_FIPS_VERSION < 5)) */
  4089. #if !defined(NO_HMAC) && !defined(NO_SHA)
  4090. WOLFSSL_TEST_SUBROUTINE int hmac_sha_test(void)
  4091. {
  4092. Hmac hmac;
  4093. byte hash[WC_SHA_DIGEST_SIZE];
  4094. const char* keys[]=
  4095. {
  4096. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  4097. "\x0b\x0b\x0b",
  4098. "Jefe",
  4099. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  4100. "\xAA\xAA\xAA"
  4101. };
  4102. testVector a, b, c;
  4103. testVector test_hmac[3];
  4104. int ret;
  4105. int times = sizeof(test_hmac) / sizeof(testVector), i;
  4106. a.input = "Hi There";
  4107. a.output = "\xb6\x17\x31\x86\x55\x05\x72\x64\xe2\x8b\xc0\xb6\xfb\x37\x8c"
  4108. "\x8e\xf1\x46\xbe\x00";
  4109. a.inLen = XSTRLEN(a.input);
  4110. a.outLen = WC_SHA_DIGEST_SIZE;
  4111. b.input = "what do ya want for nothing?";
  4112. b.output = "\xef\xfc\xdf\x6a\xe5\xeb\x2f\xa2\xd2\x74\x16\xd5\xf1\x84\xdf"
  4113. "\x9c\x25\x9a\x7c\x79";
  4114. b.inLen = XSTRLEN(b.input);
  4115. b.outLen = WC_SHA_DIGEST_SIZE;
  4116. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4117. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4118. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4119. "\xDD\xDD\xDD\xDD\xDD\xDD";
  4120. c.output = "\x12\x5d\x73\x42\xb9\xac\x11\xcd\x91\xa3\x9a\xf4\x8a\xa1\x7b"
  4121. "\x4f\x63\xf1\x75\xd3";
  4122. c.inLen = XSTRLEN(c.input);
  4123. c.outLen = WC_SHA_DIGEST_SIZE;
  4124. test_hmac[0] = a;
  4125. test_hmac[1] = b;
  4126. test_hmac[2] = c;
  4127. for (i = 0; i < times; ++i) {
  4128. #if defined(HAVE_FIPS) || defined(HAVE_CAVIUM)
  4129. if (i == 1)
  4130. continue; /* cavium can't handle short keys, fips not allowed */
  4131. #endif
  4132. if (wc_HmacInit(&hmac, HEAP_HINT, devId) != 0)
  4133. return -3500;
  4134. ret = wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys[i],
  4135. (word32)XSTRLEN(keys[i]));
  4136. if (ret != 0)
  4137. return -3501;
  4138. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  4139. (word32)test_hmac[i].inLen);
  4140. if (ret != 0)
  4141. return -3502;
  4142. ret = wc_HmacFinal(&hmac, hash);
  4143. if (ret != 0)
  4144. return -3503;
  4145. if (XMEMCMP(hash, test_hmac[i].output, WC_SHA_DIGEST_SIZE) != 0)
  4146. return -3504 - i;
  4147. wc_HmacFree(&hmac);
  4148. }
  4149. #ifndef HAVE_FIPS
  4150. if (wc_HmacSizeByType(WC_SHA) != WC_SHA_DIGEST_SIZE)
  4151. return -3514;
  4152. #endif
  4153. return 0;
  4154. }
  4155. #endif
  4156. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
  4157. WOLFSSL_TEST_SUBROUTINE int hmac_sha224_test(void)
  4158. {
  4159. Hmac hmac;
  4160. byte hash[WC_SHA224_DIGEST_SIZE];
  4161. const char* keys[]=
  4162. {
  4163. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  4164. "\x0b\x0b\x0b",
  4165. "Jefe",
  4166. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  4167. "\xAA\xAA\xAA",
  4168. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4169. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4170. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4171. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4172. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4173. };
  4174. testVector a, b, c, d;
  4175. testVector test_hmac[4];
  4176. int ret;
  4177. int times = sizeof(test_hmac) / sizeof(testVector), i;
  4178. a.input = "Hi There";
  4179. a.output = "\x89\x6f\xb1\x12\x8a\xbb\xdf\x19\x68\x32\x10\x7c\xd4\x9d\xf3"
  4180. "\x3f\x47\xb4\xb1\x16\x99\x12\xba\x4f\x53\x68\x4b\x22";
  4181. a.inLen = XSTRLEN(a.input);
  4182. a.outLen = WC_SHA224_DIGEST_SIZE;
  4183. b.input = "what do ya want for nothing?";
  4184. b.output = "\xa3\x0e\x01\x09\x8b\xc6\xdb\xbf\x45\x69\x0f\x3a\x7e\x9e\x6d"
  4185. "\x0f\x8b\xbe\xa2\xa3\x9e\x61\x48\x00\x8f\xd0\x5e\x44";
  4186. b.inLen = XSTRLEN(b.input);
  4187. b.outLen = WC_SHA224_DIGEST_SIZE;
  4188. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4189. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4190. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4191. "\xDD\xDD\xDD\xDD\xDD\xDD";
  4192. c.output = "\x7f\xb3\xcb\x35\x88\xc6\xc1\xf6\xff\xa9\x69\x4d\x7d\x6a\xd2"
  4193. "\x64\x93\x65\xb0\xc1\xf6\x5d\x69\xd1\xec\x83\x33\xea";
  4194. c.inLen = XSTRLEN(c.input);
  4195. c.outLen = WC_SHA224_DIGEST_SIZE;
  4196. d.input = "Big Key Input";
  4197. d.output = "\xe7\x4e\x2b\x8a\xa9\xf0\x37\x2f\xed\xae\x70\x0c\x49\x47\xf1"
  4198. "\x46\x54\xa7\x32\x6b\x55\x01\x87\xd2\xc8\x02\x0e\x3a";
  4199. d.inLen = XSTRLEN(d.input);
  4200. d.outLen = WC_SHA224_DIGEST_SIZE;
  4201. test_hmac[0] = a;
  4202. test_hmac[1] = b;
  4203. test_hmac[2] = c;
  4204. test_hmac[3] = d;
  4205. for (i = 0; i < times; ++i) {
  4206. #if defined(HAVE_FIPS) || defined(HAVE_CAVIUM)
  4207. if (i == 1)
  4208. continue; /* cavium can't handle short keys, fips not allowed */
  4209. #endif
  4210. if (wc_HmacInit(&hmac, HEAP_HINT, devId) != 0)
  4211. return -3600;
  4212. ret = wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys[i],
  4213. (word32)XSTRLEN(keys[i]));
  4214. if (ret != 0)
  4215. return -3601;
  4216. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  4217. (word32)test_hmac[i].inLen);
  4218. if (ret != 0)
  4219. return -3602;
  4220. ret = wc_HmacFinal(&hmac, hash);
  4221. if (ret != 0)
  4222. return -3603;
  4223. if (XMEMCMP(hash, test_hmac[i].output, WC_SHA224_DIGEST_SIZE) != 0)
  4224. return -3604 - i;
  4225. wc_HmacFree(&hmac);
  4226. }
  4227. #ifndef HAVE_FIPS
  4228. if (wc_HmacSizeByType(WC_SHA224) != WC_SHA224_DIGEST_SIZE)
  4229. return -3614;
  4230. #endif
  4231. return 0;
  4232. }
  4233. #endif
  4234. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  4235. WOLFSSL_TEST_SUBROUTINE int hmac_sha256_test(void)
  4236. {
  4237. Hmac hmac;
  4238. byte hash[WC_SHA256_DIGEST_SIZE];
  4239. const char* keys[]=
  4240. {
  4241. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  4242. "\x0b\x0b\x0b",
  4243. "Jefe",
  4244. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  4245. "\xAA\xAA\xAA",
  4246. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  4247. "\xAA\xAA\xAA",
  4248. };
  4249. testVector a, b, c, d;
  4250. testVector test_hmac[4];
  4251. int ret;
  4252. int times = sizeof(test_hmac) / sizeof(testVector), i;
  4253. a.input = "Hi There";
  4254. a.output = "\xb0\x34\x4c\x61\xd8\xdb\x38\x53\x5c\xa8\xaf\xce\xaf\x0b\xf1"
  4255. "\x2b\x88\x1d\xc2\x00\xc9\x83\x3d\xa7\x26\xe9\x37\x6c\x2e\x32"
  4256. "\xcf\xf7";
  4257. a.inLen = XSTRLEN(a.input);
  4258. a.outLen = WC_SHA256_DIGEST_SIZE;
  4259. b.input = "what do ya want for nothing?";
  4260. b.output = "\x5b\xdc\xc1\x46\xbf\x60\x75\x4e\x6a\x04\x24\x26\x08\x95\x75"
  4261. "\xc7\x5a\x00\x3f\x08\x9d\x27\x39\x83\x9d\xec\x58\xb9\x64\xec"
  4262. "\x38\x43";
  4263. b.inLen = XSTRLEN(b.input);
  4264. b.outLen = WC_SHA256_DIGEST_SIZE;
  4265. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4266. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4267. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4268. "\xDD\xDD\xDD\xDD\xDD\xDD";
  4269. c.output = "\x77\x3e\xa9\x1e\x36\x80\x0e\x46\x85\x4d\xb8\xeb\xd0\x91\x81"
  4270. "\xa7\x29\x59\x09\x8b\x3e\xf8\xc1\x22\xd9\x63\x55\x14\xce\xd5"
  4271. "\x65\xfe";
  4272. c.inLen = XSTRLEN(c.input);
  4273. c.outLen = WC_SHA256_DIGEST_SIZE;
  4274. d.input = 0;
  4275. d.output = "\x86\xe5\x4f\xd4\x48\x72\x5d\x7e\x5d\xcf\xe2\x23\x53\xc8\x28"
  4276. "\xaf\x48\x78\x1e\xb4\x8c\xae\x81\x06\xa7\xe1\xd4\x98\x94\x9f"
  4277. "\x3e\x46";
  4278. d.inLen = 0;
  4279. d.outLen = WC_SHA256_DIGEST_SIZE;
  4280. test_hmac[0] = a;
  4281. test_hmac[1] = b;
  4282. test_hmac[2] = c;
  4283. test_hmac[3] = d;
  4284. for (i = 0; i < times; ++i) {
  4285. #if defined(HAVE_FIPS) || defined(HAVE_CAVIUM)
  4286. if (i == 1)
  4287. continue; /* cavium can't handle short keys, fips not allowed */
  4288. #endif
  4289. #if defined(HAVE_INTEL_QA) || defined(HAVE_CAVIUM)
  4290. if (i == 3)
  4291. continue; /* QuickAssist can't handle empty HMAC */
  4292. #endif
  4293. if (wc_HmacInit(&hmac, HEAP_HINT, devId) != 0)
  4294. return -3700 - i;
  4295. ret = wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys[i],
  4296. (word32)XSTRLEN(keys[i]));
  4297. if (ret != 0)
  4298. return -3710 - i;
  4299. if (test_hmac[i].input != NULL) {
  4300. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  4301. (word32)test_hmac[i].inLen);
  4302. if (ret != 0)
  4303. return -3720 - i;
  4304. }
  4305. ret = wc_HmacFinal(&hmac, hash);
  4306. if (ret != 0)
  4307. return -3730 - i;
  4308. if (XMEMCMP(hash, test_hmac[i].output, WC_SHA256_DIGEST_SIZE) != 0)
  4309. return -3740 - i;
  4310. wc_HmacFree(&hmac);
  4311. }
  4312. #ifndef HAVE_FIPS
  4313. if (wc_HmacSizeByType(WC_SHA256) != WC_SHA256_DIGEST_SIZE)
  4314. return -3750;
  4315. if (wc_HmacSizeByType(20) != BAD_FUNC_ARG)
  4316. return -3751;
  4317. #endif
  4318. if (wolfSSL_GetHmacMaxSize() != WC_MAX_DIGEST_SIZE)
  4319. return -3752;
  4320. return 0;
  4321. }
  4322. #endif
  4323. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  4324. WOLFSSL_TEST_SUBROUTINE int hmac_sha384_test(void)
  4325. {
  4326. Hmac hmac;
  4327. byte hash[WC_SHA384_DIGEST_SIZE];
  4328. const char* keys[]=
  4329. {
  4330. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  4331. "\x0b\x0b\x0b",
  4332. "Jefe",
  4333. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  4334. "\xAA\xAA\xAA",
  4335. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4336. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4337. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4338. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4339. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4340. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4341. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  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. };
  4345. testVector a, b, c, d;
  4346. testVector test_hmac[4];
  4347. int ret;
  4348. int times = sizeof(test_hmac) / sizeof(testVector), i;
  4349. a.input = "Hi There";
  4350. a.output = "\xaf\xd0\x39\x44\xd8\x48\x95\x62\x6b\x08\x25\xf4\xab\x46\x90"
  4351. "\x7f\x15\xf9\xda\xdb\xe4\x10\x1e\xc6\x82\xaa\x03\x4c\x7c\xeb"
  4352. "\xc5\x9c\xfa\xea\x9e\xa9\x07\x6e\xde\x7f\x4a\xf1\x52\xe8\xb2"
  4353. "\xfa\x9c\xb6";
  4354. a.inLen = XSTRLEN(a.input);
  4355. a.outLen = WC_SHA384_DIGEST_SIZE;
  4356. b.input = "what do ya want for nothing?";
  4357. b.output = "\xaf\x45\xd2\xe3\x76\x48\x40\x31\x61\x7f\x78\xd2\xb5\x8a\x6b"
  4358. "\x1b\x9c\x7e\xf4\x64\xf5\xa0\x1b\x47\xe4\x2e\xc3\x73\x63\x22"
  4359. "\x44\x5e\x8e\x22\x40\xca\x5e\x69\xe2\xc7\x8b\x32\x39\xec\xfa"
  4360. "\xb2\x16\x49";
  4361. b.inLen = XSTRLEN(b.input);
  4362. b.outLen = WC_SHA384_DIGEST_SIZE;
  4363. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4364. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4365. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4366. "\xDD\xDD\xDD\xDD\xDD\xDD";
  4367. c.output = "\x88\x06\x26\x08\xd3\xe6\xad\x8a\x0a\xa2\xac\xe0\x14\xc8\xa8"
  4368. "\x6f\x0a\xa6\x35\xd9\x47\xac\x9f\xeb\xe8\x3e\xf4\xe5\x59\x66"
  4369. "\x14\x4b\x2a\x5a\xb3\x9d\xc1\x38\x14\xb9\x4e\x3a\xb6\xe1\x01"
  4370. "\xa3\x4f\x27";
  4371. c.inLen = XSTRLEN(c.input);
  4372. c.outLen = WC_SHA384_DIGEST_SIZE;
  4373. d.input = "Big Key Input";
  4374. d.output = "\xd2\x3d\x29\x6e\xf5\x1e\x23\x23\x49\x18\xb3\xbf\x4c\x38\x7b"
  4375. "\x31\x21\x17\xbb\x09\x73\x27\xf8\x12\x9d\xe9\xc6\x5d\xf9\x54"
  4376. "\xd6\x38\x5a\x68\x53\x14\xee\xe0\xa6\x4f\x36\x7e\xb2\xf3\x1a"
  4377. "\x57\x41\x69";
  4378. d.inLen = XSTRLEN(d.input);
  4379. d.outLen = WC_SHA384_DIGEST_SIZE;
  4380. test_hmac[0] = a;
  4381. test_hmac[1] = b;
  4382. test_hmac[2] = c;
  4383. test_hmac[3] = d;
  4384. for (i = 0; i < times; ++i) {
  4385. #if defined(HAVE_FIPS)
  4386. if (i == 1)
  4387. continue; /* fips not allowed */
  4388. #endif
  4389. if (wc_HmacInit(&hmac, HEAP_HINT, devId) != 0)
  4390. return -3800;
  4391. ret = wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys[i],
  4392. (word32)XSTRLEN(keys[i]));
  4393. if (ret != 0)
  4394. return -3801;
  4395. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  4396. (word32)test_hmac[i].inLen);
  4397. if (ret != 0)
  4398. return -3802;
  4399. ret = wc_HmacFinal(&hmac, hash);
  4400. if (ret != 0)
  4401. return -3803;
  4402. if (XMEMCMP(hash, test_hmac[i].output, WC_SHA384_DIGEST_SIZE) != 0)
  4403. return -3804 - i;
  4404. wc_HmacFree(&hmac);
  4405. }
  4406. #ifndef HAVE_FIPS
  4407. if (wc_HmacSizeByType(WC_SHA384) != WC_SHA384_DIGEST_SIZE)
  4408. return -3814;
  4409. #endif
  4410. return 0;
  4411. }
  4412. #endif
  4413. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA512)
  4414. WOLFSSL_TEST_SUBROUTINE int hmac_sha512_test(void)
  4415. {
  4416. Hmac hmac;
  4417. byte hash[WC_SHA512_DIGEST_SIZE];
  4418. const char* keys[]=
  4419. {
  4420. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  4421. "\x0b\x0b\x0b",
  4422. "Jefe",
  4423. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  4424. "\xAA\xAA\xAA",
  4425. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4426. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4427. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4428. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4429. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4430. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4431. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  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. };
  4435. testVector a, b, c, d;
  4436. testVector test_hmac[4];
  4437. int ret;
  4438. int times = sizeof(test_hmac) / sizeof(testVector), i;
  4439. a.input = "Hi There";
  4440. a.output = "\x87\xaa\x7c\xde\xa5\xef\x61\x9d\x4f\xf0\xb4\x24\x1a\x1d\x6c"
  4441. "\xb0\x23\x79\xf4\xe2\xce\x4e\xc2\x78\x7a\xd0\xb3\x05\x45\xe1"
  4442. "\x7c\xde\xda\xa8\x33\xb7\xd6\xb8\xa7\x02\x03\x8b\x27\x4e\xae"
  4443. "\xa3\xf4\xe4\xbe\x9d\x91\x4e\xeb\x61\xf1\x70\x2e\x69\x6c\x20"
  4444. "\x3a\x12\x68\x54";
  4445. a.inLen = XSTRLEN(a.input);
  4446. a.outLen = WC_SHA512_DIGEST_SIZE;
  4447. b.input = "what do ya want for nothing?";
  4448. b.output = "\x16\x4b\x7a\x7b\xfc\xf8\x19\xe2\xe3\x95\xfb\xe7\x3b\x56\xe0"
  4449. "\xa3\x87\xbd\x64\x22\x2e\x83\x1f\xd6\x10\x27\x0c\xd7\xea\x25"
  4450. "\x05\x54\x97\x58\xbf\x75\xc0\x5a\x99\x4a\x6d\x03\x4f\x65\xf8"
  4451. "\xf0\xe6\xfd\xca\xea\xb1\xa3\x4d\x4a\x6b\x4b\x63\x6e\x07\x0a"
  4452. "\x38\xbc\xe7\x37";
  4453. b.inLen = XSTRLEN(b.input);
  4454. b.outLen = WC_SHA512_DIGEST_SIZE;
  4455. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4456. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4457. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4458. "\xDD\xDD\xDD\xDD\xDD\xDD";
  4459. c.output = "\xfa\x73\xb0\x08\x9d\x56\xa2\x84\xef\xb0\xf0\x75\x6c\x89\x0b"
  4460. "\xe9\xb1\xb5\xdb\xdd\x8e\xe8\x1a\x36\x55\xf8\x3e\x33\xb2\x27"
  4461. "\x9d\x39\xbf\x3e\x84\x82\x79\xa7\x22\xc8\x06\xb4\x85\xa4\x7e"
  4462. "\x67\xc8\x07\xb9\x46\xa3\x37\xbe\xe8\x94\x26\x74\x27\x88\x59"
  4463. "\xe1\x32\x92\xfb";
  4464. c.inLen = XSTRLEN(c.input);
  4465. c.outLen = WC_SHA512_DIGEST_SIZE;
  4466. d.input = "Big Key Input";
  4467. d.output = "\x3f\xa9\xc9\xe1\xbd\xbb\x04\x55\x1f\xef\xcc\x92\x33\x08\xeb"
  4468. "\xcf\xc1\x9a\x5b\x5b\xc0\x7c\x86\x84\xae\x8c\x40\xaf\xb1\x27"
  4469. "\x87\x38\x92\x04\xa8\xed\xd7\xd7\x07\xa9\x85\xa0\xc2\xcd\x30"
  4470. "\xc0\x56\x14\x49\xbc\x2f\x69\x15\x6a\x97\xd8\x79\x2f\xb3\x3b"
  4471. "\x1e\x18\xfe\xfa";
  4472. d.inLen = XSTRLEN(d.input);
  4473. d.outLen = WC_SHA512_DIGEST_SIZE;
  4474. test_hmac[0] = a;
  4475. test_hmac[1] = b;
  4476. test_hmac[2] = c;
  4477. test_hmac[3] = d;
  4478. for (i = 0; i < times; ++i) {
  4479. #if defined(HAVE_FIPS)
  4480. if (i == 1)
  4481. continue; /* fips not allowed */
  4482. #endif
  4483. if (wc_HmacInit(&hmac, HEAP_HINT, devId) != 0)
  4484. return -3900;
  4485. ret = wc_HmacSetKey(&hmac, WC_SHA512, (byte*)keys[i],
  4486. (word32)XSTRLEN(keys[i]));
  4487. if (ret != 0)
  4488. return -3901;
  4489. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  4490. (word32)test_hmac[i].inLen);
  4491. if (ret != 0)
  4492. return -3902;
  4493. ret = wc_HmacFinal(&hmac, hash);
  4494. if (ret != 0)
  4495. return -3903;
  4496. if (XMEMCMP(hash, test_hmac[i].output, WC_SHA512_DIGEST_SIZE) != 0)
  4497. return -3904 - i;
  4498. wc_HmacFree(&hmac);
  4499. }
  4500. #ifndef HAVE_FIPS
  4501. if (wc_HmacSizeByType(WC_SHA512) != WC_SHA512_DIGEST_SIZE)
  4502. return -3914;
  4503. #endif
  4504. return 0;
  4505. }
  4506. #endif
  4507. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA3) && \
  4508. !defined(WOLFSSL_NOSHA3_224) && !defined(WOLFSSL_NOSHA3_256) && \
  4509. !defined(WOLFSSL_NOSHA3_384) && !defined(WOLFSSL_NOSHA3_512)
  4510. WOLFSSL_TEST_SUBROUTINE int hmac_sha3_test(void)
  4511. {
  4512. Hmac hmac;
  4513. byte hash[WC_SHA3_512_DIGEST_SIZE];
  4514. const char* key[4] =
  4515. {
  4516. "Jefe",
  4517. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  4518. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
  4519. "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
  4520. "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa",
  4521. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4522. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4523. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4524. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4525. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4526. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4527. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  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. };
  4532. const char* input[4] =
  4533. {
  4534. "what do ya want for nothing?",
  4535. "Hi There",
  4536. "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
  4537. "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
  4538. "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
  4539. "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
  4540. "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd",
  4541. "Big Key Input"
  4542. };
  4543. const int hashType[4] =
  4544. {
  4545. WC_SHA3_224, WC_SHA3_256, WC_SHA3_384, WC_SHA3_512
  4546. };
  4547. const int hashSz[4] =
  4548. {
  4549. WC_SHA3_224_DIGEST_SIZE, WC_SHA3_256_DIGEST_SIZE,
  4550. WC_SHA3_384_DIGEST_SIZE, WC_SHA3_512_DIGEST_SIZE
  4551. };
  4552. const char* output[16] =
  4553. {
  4554. /* key = jefe, input = what do ya want for nothing? */
  4555. /* HMAC-SHA3-224 */
  4556. "\x7f\xdb\x8d\xd8\x8b\xd2\xf6\x0d\x1b\x79\x86\x34\xad\x38\x68\x11"
  4557. "\xc2\xcf\xc8\x5b\xfa\xf5\xd5\x2b\xba\xce\x5e\x66",
  4558. /* HMAC-SHA3-256 */
  4559. "\xc7\xd4\x07\x2e\x78\x88\x77\xae\x35\x96\xbb\xb0\xda\x73\xb8\x87"
  4560. "\xc9\x17\x1f\x93\x09\x5b\x29\x4a\xe8\x57\xfb\xe2\x64\x5e\x1b\xa5",
  4561. /* HMAC-SHA3-384 */
  4562. "\xf1\x10\x1f\x8c\xbf\x97\x66\xfd\x67\x64\xd2\xed\x61\x90\x3f\x21"
  4563. "\xca\x9b\x18\xf5\x7c\xf3\xe1\xa2\x3c\xa1\x35\x08\xa9\x32\x43\xce"
  4564. "\x48\xc0\x45\xdc\x00\x7f\x26\xa2\x1b\x3f\x5e\x0e\x9d\xf4\xc2\x0a",
  4565. /* HMAC-SHA3-512 */
  4566. "\x5a\x4b\xfe\xab\x61\x66\x42\x7c\x7a\x36\x47\xb7\x47\x29\x2b\x83"
  4567. "\x84\x53\x7c\xdb\x89\xaf\xb3\xbf\x56\x65\xe4\xc5\xe7\x09\x35\x0b"
  4568. "\x28\x7b\xae\xc9\x21\xfd\x7c\xa0\xee\x7a\x0c\x31\xd0\x22\xa9\x5e"
  4569. "\x1f\xc9\x2b\xa9\xd7\x7d\xf8\x83\x96\x02\x75\xbe\xb4\xe6\x20\x24",
  4570. /* key = 0b..., input = Hi There */
  4571. /* HMAC-SHA3-224 */
  4572. "\x3b\x16\x54\x6b\xbc\x7b\xe2\x70\x6a\x03\x1d\xca\xfd\x56\x37\x3d"
  4573. "\x98\x84\x36\x76\x41\xd8\xc5\x9a\xf3\xc8\x60\xf7",
  4574. /* HMAC-SHA3-256 */
  4575. "\xba\x85\x19\x23\x10\xdf\xfa\x96\xe2\xa3\xa4\x0e\x69\x77\x43\x51"
  4576. "\x14\x0b\xb7\x18\x5e\x12\x02\xcd\xcc\x91\x75\x89\xf9\x5e\x16\xbb",
  4577. /* HMAC-SHA3-384 */
  4578. "\x68\xd2\xdc\xf7\xfd\x4d\xdd\x0a\x22\x40\xc8\xa4\x37\x30\x5f\x61"
  4579. "\xfb\x73\x34\xcf\xb5\xd0\x22\x6e\x1b\xc2\x7d\xc1\x0a\x2e\x72\x3a"
  4580. "\x20\xd3\x70\xb4\x77\x43\x13\x0e\x26\xac\x7e\x3d\x53\x28\x86\xbd",
  4581. /* HMAC-SHA3-512 */
  4582. "\xeb\x3f\xbd\x4b\x2e\xaa\xb8\xf5\xc5\x04\xbd\x3a\x41\x46\x5a\xac"
  4583. "\xec\x15\x77\x0a\x7c\xab\xac\x53\x1e\x48\x2f\x86\x0b\x5e\xc7\xba"
  4584. "\x47\xcc\xb2\xc6\xf2\xaf\xce\x8f\x88\xd2\x2b\x6d\xc6\x13\x80\xf2"
  4585. "\x3a\x66\x8f\xd3\x88\x8b\xb8\x05\x37\xc0\xa0\xb8\x64\x07\x68\x9e",
  4586. /* key = aa..., output = dd... */
  4587. /* HMAC-SHA3-224 */
  4588. "\x67\x6c\xfc\x7d\x16\x15\x36\x38\x78\x03\x90\x69\x2b\xe1\x42\xd2"
  4589. "\xdf\x7c\xe9\x24\xb9\x09\xc0\xc0\x8d\xbf\xdc\x1a",
  4590. /* HMAC-SHA3-256 */
  4591. "\x84\xec\x79\x12\x4a\x27\x10\x78\x65\xce\xdd\x8b\xd8\x2d\xa9\x96"
  4592. "\x5e\x5e\xd8\xc3\x7b\x0a\xc9\x80\x05\xa7\xf3\x9e\xd5\x8a\x42\x07",
  4593. /* HMAC-SHA3-384 */
  4594. "\x27\x5c\xd0\xe6\x61\xbb\x8b\x15\x1c\x64\xd2\x88\xf1\xf7\x82\xfb"
  4595. "\x91\xa8\xab\xd5\x68\x58\xd7\x2b\xab\xb2\xd4\x76\xf0\x45\x83\x73"
  4596. "\xb4\x1b\x6a\xb5\xbf\x17\x4b\xec\x42\x2e\x53\xfc\x31\x35\xac\x6e",
  4597. /* HMAC-SHA3-512 */
  4598. "\x30\x9e\x99\xf9\xec\x07\x5e\xc6\xc6\xd4\x75\xed\xa1\x18\x06\x87"
  4599. "\xfc\xf1\x53\x11\x95\x80\x2a\x99\xb5\x67\x74\x49\xa8\x62\x51\x82"
  4600. "\x85\x1c\xb3\x32\xaf\xb6\xa8\x9c\x41\x13\x25\xfb\xcb\xcd\x42\xaf"
  4601. "\xcb\x7b\x6e\x5a\xab\x7e\xa4\x2c\x66\x0f\x97\xfd\x85\x84\xbf\x03",
  4602. /* key = big key, input = Big Key Input */
  4603. /* HMAC-SHA3-224 */
  4604. "\x29\xe0\x5e\x46\xc4\xa4\x5e\x46\x74\xbf\xd7\x2d\x1a\xd8\x66\xdb"
  4605. "\x2d\x0d\x10\x4e\x2b\xfa\xad\x53\x7d\x15\x69\x8b",
  4606. /* HMAC-SHA3-256 */
  4607. "\xb5\x5b\x8d\x64\xb6\x9c\x21\xd0\xbf\x20\x5c\xa2\xf7\xb9\xb1\x4e"
  4608. "\x88\x21\x61\x2c\x66\xc3\x91\xae\x6c\x95\x16\x85\x83\xe6\xf4\x9b",
  4609. /* HMAC-SHA3-384 */
  4610. "\xaa\x91\xb3\xa6\x2f\x56\xa1\xbe\x8c\x3e\x74\x38\xdb\x58\xd9\xd3"
  4611. "\x34\xde\xa0\x60\x6d\x8d\x46\xe0\xec\xa9\xf6\x06\x35\x14\xe6\xed"
  4612. "\x83\xe6\x7c\x77\x24\x6c\x11\xb5\x90\x82\xb5\x75\xda\x7b\x83\x2d",
  4613. /* HMAC-SHA3-512 */
  4614. "\x1c\xc3\xa9\x24\x4a\x4a\x3f\xbd\xc7\x20\x00\x16\x9b\x79\x47\x03"
  4615. "\x78\x75\x2c\xb5\xf1\x2e\x62\x7c\xbe\xef\x4e\x8f\x0b\x11\x2b\x32"
  4616. "\xa0\xee\xc9\xd0\x4d\x64\x64\x0b\x37\xf4\xdd\x66\xf7\x8b\xb3\xad"
  4617. "\x52\x52\x6b\x65\x12\xde\x0d\x7c\xc0\x8b\x60\x01\x6c\x37\xd7\xa8"
  4618. };
  4619. int i = 0, iMax = sizeof(input) / sizeof(input[0]),
  4620. j, jMax = sizeof(hashType) / sizeof(hashType[0]),
  4621. ret;
  4622. #ifdef HAVE_FIPS
  4623. /* FIPS requires a minimum length for HMAC keys, and "Jefe" is too
  4624. * short. Skip it in FIPS builds. */
  4625. i = 1;
  4626. #endif
  4627. for (; i < iMax; i++) {
  4628. for (j = 0; j < jMax; j++) {
  4629. if (wc_HmacInit(&hmac, HEAP_HINT, devId) != 0)
  4630. return -4000;
  4631. ret = wc_HmacSetKey(&hmac, hashType[j], (byte*)key[i],
  4632. (word32)XSTRLEN(key[i]));
  4633. if (ret != 0)
  4634. return -4001;
  4635. ret = wc_HmacUpdate(&hmac, (byte*)input[i],
  4636. (word32)XSTRLEN(input[i]));
  4637. if (ret != 0)
  4638. return -4002;
  4639. ret = wc_HmacFinal(&hmac, hash);
  4640. if (ret != 0)
  4641. return -4003;
  4642. if (XMEMCMP(hash, output[(i*jMax) + j], hashSz[j]) != 0)
  4643. return -4004;
  4644. wc_HmacFree(&hmac);
  4645. if (i > 0)
  4646. continue;
  4647. #ifndef HAVE_FIPS
  4648. ret = wc_HmacSizeByType(hashType[j]);
  4649. if (ret != hashSz[j])
  4650. return -4005;
  4651. #endif
  4652. }
  4653. }
  4654. return 0;
  4655. }
  4656. #endif
  4657. #ifdef WC_RC2
  4658. typedef struct rc2TestVector {
  4659. const char* input;
  4660. const char* output;
  4661. const char* key; /* Key, variable up to 128 bytes */
  4662. const char* iv; /* IV, 8-bytes */
  4663. int inLen;
  4664. int outLen;
  4665. int keyLen;
  4666. int effectiveKeyBits; /* Up to 1024 bits supported */
  4667. } rc2TestVector;
  4668. static int rc2_ecb_test(void)
  4669. {
  4670. int ret = 0;
  4671. byte cipher[RC2_BLOCK_SIZE];
  4672. byte plain[RC2_BLOCK_SIZE];
  4673. rc2TestVector a, b, c, d, e, f, g, h;
  4674. rc2TestVector test_rc2[8];
  4675. int times = sizeof(test_rc2) / sizeof(rc2TestVector), i;
  4676. a.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4677. a.output = "\xeb\xb7\x73\xf9\x93\x27\x8e\xff";
  4678. a.key = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4679. a.inLen = RC2_BLOCK_SIZE;
  4680. a.outLen = RC2_BLOCK_SIZE;
  4681. a.keyLen = 8;
  4682. a.effectiveKeyBits = 63;
  4683. b.input = "\xff\xff\xff\xff\xff\xff\xff\xff";
  4684. b.output = "\x27\x8b\x27\xe4\x2e\x2f\x0d\x49";
  4685. b.key = "\xff\xff\xff\xff\xff\xff\xff\xff";
  4686. b.inLen = RC2_BLOCK_SIZE;
  4687. b.outLen = RC2_BLOCK_SIZE;
  4688. b.keyLen = 8;
  4689. b.effectiveKeyBits = 64;
  4690. c.input = "\x10\x00\x00\x00\x00\x00\x00\x01";
  4691. c.output = "\x30\x64\x9e\xdf\x9b\xe7\xd2\xc2";
  4692. c.key = "\x30\x00\x00\x00\x00\x00\x00\x00";
  4693. c.inLen = RC2_BLOCK_SIZE;
  4694. c.outLen = RC2_BLOCK_SIZE;
  4695. c.keyLen = 8;
  4696. c.effectiveKeyBits = 64;
  4697. d.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4698. d.output = "\x61\xa8\xa2\x44\xad\xac\xcc\xf0";
  4699. d.key = "\x88";
  4700. d.inLen = RC2_BLOCK_SIZE;
  4701. d.outLen = RC2_BLOCK_SIZE;
  4702. d.keyLen = 1;
  4703. d.effectiveKeyBits = 64;
  4704. e.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4705. e.output = "\x6c\xcf\x43\x08\x97\x4c\x26\x7f";
  4706. e.key = "\x88\xbc\xa9\x0e\x90\x87\x5a";
  4707. e.inLen = RC2_BLOCK_SIZE;
  4708. e.outLen = RC2_BLOCK_SIZE;
  4709. e.keyLen = 7;
  4710. e.effectiveKeyBits = 64;
  4711. f.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4712. f.output = "\x1a\x80\x7d\x27\x2b\xbe\x5d\xb1";
  4713. f.key = "\x88\xbc\xa9\x0e\x90\x87\x5a\x7f"
  4714. "\x0f\x79\xc3\x84\x62\x7b\xaf\xb2";
  4715. f.inLen = RC2_BLOCK_SIZE;
  4716. f.outLen = RC2_BLOCK_SIZE;
  4717. f.keyLen = 16;
  4718. f.effectiveKeyBits = 64;
  4719. g.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4720. g.output = "\x22\x69\x55\x2a\xb0\xf8\x5c\xa6";
  4721. g.key = "\x88\xbc\xa9\x0e\x90\x87\x5a\x7f"
  4722. "\x0f\x79\xc3\x84\x62\x7b\xaf\xb2";
  4723. g.inLen = RC2_BLOCK_SIZE;
  4724. g.outLen = RC2_BLOCK_SIZE;
  4725. g.keyLen = 16;
  4726. g.effectiveKeyBits = 128;
  4727. h.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4728. h.output = "\x5b\x78\xd3\xa4\x3d\xff\xf1\xf1";
  4729. h.key = "\x88\xbc\xa9\x0e\x90\x87\x5a\x7f"
  4730. "\x0f\x79\xc3\x84\x62\x7b\xaf\xb2"
  4731. "\x16\xf8\x0a\x6f\x85\x92\x05\x84"
  4732. "\xc4\x2f\xce\xb0\xbe\x25\x5d\xaf"
  4733. "\x1e";
  4734. h.inLen = RC2_BLOCK_SIZE;
  4735. h.outLen = RC2_BLOCK_SIZE;
  4736. h.keyLen = 33;
  4737. h.effectiveKeyBits = 129;
  4738. a.iv = b.iv = c.iv = d.iv = e.iv = f.iv = g.iv = h.iv = NULL;
  4739. test_rc2[0] = a;
  4740. test_rc2[1] = b;
  4741. test_rc2[2] = c;
  4742. test_rc2[3] = d;
  4743. test_rc2[4] = e;
  4744. test_rc2[5] = f;
  4745. test_rc2[6] = g;
  4746. test_rc2[7] = h;
  4747. for (i = 0; i < times; ++i) {
  4748. Rc2 enc;
  4749. XMEMSET(cipher, 0, RC2_BLOCK_SIZE);
  4750. XMEMSET(plain, 0, RC2_BLOCK_SIZE);
  4751. ret = wc_Rc2SetKey(&enc, (byte*)test_rc2[i].key, test_rc2[i].keyLen,
  4752. NULL, test_rc2[i].effectiveKeyBits);
  4753. if (ret != 0) {
  4754. return -4100;
  4755. }
  4756. /* ECB encrypt */
  4757. ret = wc_Rc2EcbEncrypt(&enc, cipher, (byte*)test_rc2[i].input,
  4758. (word32)test_rc2[i].outLen);
  4759. if (ret != 0) {
  4760. return -4101;
  4761. }
  4762. if (XMEMCMP(cipher, test_rc2[i].output, test_rc2[i].outLen)) {
  4763. return -4102;
  4764. }
  4765. /* ECB decrypt */
  4766. ret = wc_Rc2EcbDecrypt(&enc, plain, cipher, RC2_BLOCK_SIZE);
  4767. if (ret != 0) {
  4768. return -4103;
  4769. }
  4770. if (XMEMCMP(plain, test_rc2[i].input, RC2_BLOCK_SIZE)) {
  4771. return -4104;
  4772. }
  4773. }
  4774. return 0;
  4775. }
  4776. static int rc2_cbc_test(void)
  4777. {
  4778. int ret = 0;
  4779. byte cipher[128];
  4780. byte plain[128];
  4781. rc2TestVector a, b, c, d, e, f, g, h, i;
  4782. rc2TestVector test_rc2[9];
  4783. int times = sizeof(test_rc2) / sizeof(rc2TestVector), j;
  4784. /* key length = 7, effective key bits = 63 */
  4785. a.input = "\x00\x00\x00\x00\x00\x00\x00\x00"
  4786. "\x00\x00\x00\x00\x00\x00\x00\x00";
  4787. a.output = "\xEB\xB7\x73\xF9\x93\x27\x8E\xFF"
  4788. "\xF0\x51\x77\x8B\x65\xDB\x13\x57";
  4789. a.key = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4790. a.iv = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4791. a.inLen = RC2_BLOCK_SIZE*2;
  4792. a.outLen = RC2_BLOCK_SIZE*2;
  4793. a.keyLen = 8;
  4794. a.effectiveKeyBits = 63;
  4795. /* key length = 8, effective key bits = 64, all 0xFF */
  4796. b.input = "\xff\xff\xff\xff\xff\xff\xff\xff"
  4797. "\xff\xff\xff\xff\xff\xff\xff\xff";
  4798. b.output = "\xA3\xA1\x12\x65\x4F\x81\xC5\xCD"
  4799. "\xB6\x94\x3E\xEA\x3E\x8B\x9D\x1F";
  4800. b.key = "\xff\xff\xff\xff\xff\xff\xff\xff";
  4801. b.iv = "\xff\xff\xff\xff\xff\xff\xff\xff";
  4802. b.inLen = RC2_BLOCK_SIZE*2;
  4803. b.outLen = RC2_BLOCK_SIZE*2;
  4804. b.keyLen = 8;
  4805. b.effectiveKeyBits = 64;
  4806. /* key length = 8, effective key bits = 64 */
  4807. c.input = "\x10\x00\x00\x00\x00\x00\x00\x01"
  4808. "\x10\x00\x00\x00\x00\x00\x00\x01";
  4809. c.output = "\xB5\x70\x14\xA2\x5F\x40\xE3\x6D"
  4810. "\x81\x99\x8D\xE0\xB5\xD5\x3A\x05";
  4811. c.key = "\x30\x00\x00\x00\x00\x00\x00\x00";
  4812. c.iv = "\x30\x00\x00\x00\x00\x00\x00\x00";
  4813. c.inLen = RC2_BLOCK_SIZE*2;
  4814. c.outLen = RC2_BLOCK_SIZE*2;
  4815. c.keyLen = 8;
  4816. c.effectiveKeyBits = 64;
  4817. /* key length = 1, effective key bits = 64 */
  4818. d.input = "\x00\x00\x00\x00\x00\x00\x00\x00"
  4819. "\x00\x00\x00\x00\x00\x00\x00\x00";
  4820. d.output = "\x61\xA8\xA2\x44\xAD\xAC\xCC\xF0"
  4821. "\x6D\x19\xE8\xF1\xFC\xE7\x38\x87";
  4822. d.key = "\x88";
  4823. d.iv = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4824. d.inLen = RC2_BLOCK_SIZE*2;
  4825. d.outLen = RC2_BLOCK_SIZE*2;
  4826. d.keyLen = 1;
  4827. d.effectiveKeyBits = 64;
  4828. /* key length = 7, effective key bits = 64 */
  4829. e.input = "\x00\x00\x00\x00\x00\x00\x00\x00"
  4830. "\x00\x00\x00\x00\x00\x00\x00\x00";
  4831. e.output = "\x6C\xCF\x43\x08\x97\x4C\x26\x7F"
  4832. "\xCC\x3C\x53\x57\x7C\xA1\xA4\x4B";
  4833. e.key = "\x88\xbc\xa9\x0e\x90\x87\x5a";
  4834. e.iv = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4835. e.inLen = RC2_BLOCK_SIZE*2;
  4836. e.outLen = RC2_BLOCK_SIZE*2;
  4837. e.keyLen = 7;
  4838. e.effectiveKeyBits = 64;
  4839. /* key length = 16, effective key bits = 64 */
  4840. f.input = "\x00\x00\x00\x00\x00\x00\x00\x00"
  4841. "\x00\x00\x00\x00\x00\x00\x00\x00";
  4842. f.output = "\x1A\x80\x7D\x27\x2B\xBE\x5D\xB1"
  4843. "\x64\xEF\xE1\xC3\xB8\xAD\xFB\xBA";
  4844. f.key = "\x88\xbc\xa9\x0e\x90\x87\x5a\x7f"
  4845. "\x0f\x79\xc3\x84\x62\x7b\xaf\xb2";
  4846. f.iv = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4847. f.inLen = RC2_BLOCK_SIZE*2;
  4848. f.outLen = RC2_BLOCK_SIZE*2;
  4849. f.keyLen = 16;
  4850. f.effectiveKeyBits = 64;
  4851. /* key length = 16, effective bits = 128 */
  4852. g.input = "\x00\x00\x00\x00\x00\x00\x00\x00"
  4853. "\x00\x00\x00\x00\x00\x00\x00\x00";
  4854. g.output = "\x22\x69\x55\x2A\xB0\xF8\x5C\xA6"
  4855. "\x53\x6E\xFD\x2D\x89\xE1\x2A\x73";
  4856. g.key = "\x88\xbc\xa9\x0e\x90\x87\x5a\x7f"
  4857. "\x0f\x79\xc3\x84\x62\x7b\xaf\xb2";
  4858. g.iv = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4859. g.inLen = RC2_BLOCK_SIZE*2;
  4860. g.outLen = RC2_BLOCK_SIZE*2;
  4861. g.keyLen = 16;
  4862. g.effectiveKeyBits = 128;
  4863. /* key length = 33, effective bits = 129 */
  4864. h.input = "\x00\x00\x00\x00\x00\x00\x00\x00"
  4865. "\x00\x00\x00\x00\x00\x00\x00\x00";
  4866. h.output = "\x5B\x78\xD3\xA4\x3D\xFF\xF1\xF1"
  4867. "\x45\x30\xA8\xD5\xC7\x7C\x46\x19";
  4868. h.key = "\x88\xbc\xa9\x0e\x90\x87\x5a\x7f"
  4869. "\x0f\x79\xc3\x84\x62\x7b\xaf\xb2"
  4870. "\x16\xf8\x0a\x6f\x85\x92\x05\x84"
  4871. "\xc4\x2f\xce\xb0\xbe\x25\x5d\xaf"
  4872. "\x1e";
  4873. h.iv = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4874. h.inLen = RC2_BLOCK_SIZE*2;
  4875. h.outLen = RC2_BLOCK_SIZE*2;
  4876. h.keyLen = 33;
  4877. h.effectiveKeyBits = 129;
  4878. /* key length = 10, effective bits = 40 */
  4879. i.input = "\x11\x22\x33\x44\x55\x66\x77\x88"
  4880. "\x99\xAA\xBB\xCC\xDD\xEE\xFF\x00"
  4881. "\x11\x22\x33\x44\x55\x66\x77\x88"
  4882. "\x99\xAA\xBB\xCC\xDD\xEE\xFF\x00";
  4883. i.output = "\x71\x2D\x11\x99\xC9\xA0\x78\x4F"
  4884. "\xCD\xF1\x1E\x3D\xFD\x21\x7E\xDB"
  4885. "\xB2\x6E\x0D\xA4\x72\xBC\x31\x51"
  4886. "\x48\xEF\x4E\x68\x3B\xDC\xCD\x7D";
  4887. i.key = "\x26\x1E\x57\x8E\xC9\x62\xBF\xB8"
  4888. "\x3E\x96";
  4889. i.iv = "\x01\x02\x03\x04\x05\x06\x07\x08";
  4890. i.inLen = RC2_BLOCK_SIZE*4;
  4891. i.outLen = RC2_BLOCK_SIZE*4;
  4892. i.keyLen = 10;
  4893. i.effectiveKeyBits = 40;
  4894. test_rc2[0] = a;
  4895. test_rc2[1] = b;
  4896. test_rc2[2] = c;
  4897. test_rc2[3] = d;
  4898. test_rc2[4] = e;
  4899. test_rc2[5] = f;
  4900. test_rc2[6] = g;
  4901. test_rc2[7] = h;
  4902. test_rc2[8] = i;
  4903. for (j = 0; j < times; ++j) {
  4904. Rc2 rc2;
  4905. XMEMSET(cipher, 0, sizeof(cipher));
  4906. XMEMSET(plain, 0, sizeof(plain));
  4907. ret = wc_Rc2SetKey(&rc2, (byte*)test_rc2[j].key, test_rc2[j].keyLen,
  4908. (byte*)test_rc2[j].iv, test_rc2[j].effectiveKeyBits);
  4909. if (ret != 0) {
  4910. return -4200;
  4911. }
  4912. ret = wc_Rc2CbcEncrypt(&rc2, cipher, (byte*)test_rc2[j].input,
  4913. test_rc2[j].inLen);
  4914. if (ret != 0) {
  4915. return -4201;
  4916. }
  4917. if (XMEMCMP(cipher, (byte*)test_rc2[j].output, test_rc2[j].outLen)) {
  4918. return -4202;
  4919. }
  4920. /* reset IV for decrypt, since overriden by encrypt operation */
  4921. ret = wc_Rc2SetIV(&rc2, (byte*)test_rc2[j].iv);
  4922. if (ret != 0) {
  4923. return -4203;
  4924. }
  4925. ret = wc_Rc2CbcDecrypt(&rc2, plain, cipher, test_rc2[j].outLen);
  4926. if (ret != 0) {
  4927. return -4204;
  4928. }
  4929. if (XMEMCMP(plain, (byte*)test_rc2[j].input, test_rc2[j].inLen)) {
  4930. return -4205;
  4931. }
  4932. }
  4933. return 0;
  4934. }
  4935. WOLFSSL_TEST_SUBROUTINE int rc2_test(void)
  4936. {
  4937. int ret = 0;
  4938. ret = rc2_ecb_test();
  4939. if (ret != 0) {
  4940. return ret;
  4941. }
  4942. return rc2_cbc_test();
  4943. }
  4944. #endif
  4945. #ifndef NO_RC4
  4946. WOLFSSL_TEST_SUBROUTINE int arc4_test(void)
  4947. {
  4948. byte cipher[16];
  4949. byte plain[16];
  4950. const char* keys[] =
  4951. {
  4952. "\x01\x23\x45\x67\x89\xab\xcd\xef",
  4953. "\x01\x23\x45\x67\x89\xab\xcd\xef",
  4954. "\x00\x00\x00\x00\x00\x00\x00\x00",
  4955. "\xef\x01\x23\x45"
  4956. };
  4957. testVector a, b, c, d;
  4958. testVector test_arc4[4];
  4959. int times = sizeof(test_arc4) / sizeof(testVector), i;
  4960. a.input = "\x01\x23\x45\x67\x89\xab\xcd\xef";
  4961. a.output = "\x75\xb7\x87\x80\x99\xe0\xc5\x96";
  4962. a.inLen = 8;
  4963. a.outLen = 8;
  4964. b.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4965. b.output = "\x74\x94\xc2\xe7\x10\x4b\x08\x79";
  4966. b.inLen = 8;
  4967. b.outLen = 8;
  4968. c.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4969. c.output = "\xde\x18\x89\x41\xa3\x37\x5d\x3a";
  4970. c.inLen = 8;
  4971. c.outLen = 8;
  4972. d.input = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";
  4973. d.output = "\xd6\xa1\x41\xa7\xec\x3c\x38\xdf\xbd\x61";
  4974. d.inLen = 10;
  4975. d.outLen = 10;
  4976. test_arc4[0] = a;
  4977. test_arc4[1] = b;
  4978. test_arc4[2] = c;
  4979. test_arc4[3] = d;
  4980. for (i = 0; i < times; ++i) {
  4981. Arc4 enc;
  4982. Arc4 dec;
  4983. int keylen = 8; /* XSTRLEN with key 0x00 not good */
  4984. if (i == 3)
  4985. keylen = 4;
  4986. if (wc_Arc4Init(&enc, HEAP_HINT, devId) != 0)
  4987. return -4400;
  4988. if (wc_Arc4Init(&dec, HEAP_HINT, devId) != 0)
  4989. return -4401;
  4990. wc_Arc4SetKey(&enc, (byte*)keys[i], keylen);
  4991. wc_Arc4SetKey(&dec, (byte*)keys[i], keylen);
  4992. wc_Arc4Process(&enc, cipher, (byte*)test_arc4[i].input,
  4993. (word32)test_arc4[i].outLen);
  4994. wc_Arc4Process(&dec, plain, cipher, (word32)test_arc4[i].outLen);
  4995. if (XMEMCMP(plain, test_arc4[i].input, test_arc4[i].outLen))
  4996. return -4402 - i;
  4997. if (XMEMCMP(cipher, test_arc4[i].output, test_arc4[i].outLen))
  4998. return -4412 - i;
  4999. wc_Arc4Free(&enc);
  5000. wc_Arc4Free(&dec);
  5001. }
  5002. return 0;
  5003. }
  5004. #endif
  5005. #ifdef HAVE_CHACHA
  5006. WOLFSSL_TEST_SUBROUTINE int chacha_test(void)
  5007. {
  5008. ChaCha enc;
  5009. ChaCha dec;
  5010. byte cipher[128];
  5011. byte plain[128];
  5012. byte sliver[64];
  5013. byte input[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  5014. word32 keySz = 32;
  5015. int ret = 0;
  5016. int i;
  5017. int times = 4;
  5018. WOLFSSL_SMALL_STACK_STATIC const byte key1[] =
  5019. {
  5020. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5021. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5022. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5023. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  5024. };
  5025. WOLFSSL_SMALL_STACK_STATIC const byte key2[] =
  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,0x01
  5031. };
  5032. WOLFSSL_SMALL_STACK_STATIC const byte key3[] =
  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,0x00
  5038. };
  5039. /* 128 bit key */
  5040. WOLFSSL_SMALL_STACK_STATIC const byte key4[] =
  5041. {
  5042. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5043. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  5044. };
  5045. const byte* keys[] = {key1, key2, key3, key4};
  5046. WOLFSSL_SMALL_STACK_STATIC const byte ivs1[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
  5047. WOLFSSL_SMALL_STACK_STATIC const byte ivs2[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
  5048. WOLFSSL_SMALL_STACK_STATIC const byte ivs3[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00};
  5049. WOLFSSL_SMALL_STACK_STATIC const byte ivs4[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
  5050. const byte* ivs[] = {ivs1, ivs2, ivs3, ivs4};
  5051. #ifndef BENCH_EMBEDDED
  5052. WOLFSSL_SMALL_STACK_STATIC const byte cipher_big_result[] = {
  5053. 0x06, 0xa6, 0x5d, 0x31, 0x21, 0x6c, 0xdb, 0x37, 0x48, 0x7c, 0x01, 0x9d,
  5054. 0x72, 0xdf, 0x0a, 0x5b, 0x64, 0x74, 0x20, 0xba, 0x9e, 0xe0, 0x26, 0x7a,
  5055. 0xbf, 0xdf, 0x83, 0x34, 0x3b, 0x4f, 0x94, 0x3f, 0x37, 0x89, 0xaf, 0x00,
  5056. 0xdf, 0x0f, 0x2e, 0x75, 0x16, 0x41, 0xf6, 0x7a, 0x86, 0x94, 0x9d, 0x32,
  5057. 0x56, 0xf0, 0x79, 0x71, 0x68, 0x6f, 0xa6, 0x6b, 0xc6, 0x59, 0x49, 0xf6,
  5058. 0x10, 0x34, 0x03, 0x03, 0x16, 0x53, 0x9a, 0x98, 0x2a, 0x46, 0xde, 0x17,
  5059. 0x06, 0x65, 0x70, 0xca, 0x0a, 0x1f, 0xab, 0x80, 0x26, 0x96, 0x3f, 0x3e,
  5060. 0x7a, 0x3c, 0xa8, 0x87, 0xbb, 0x65, 0xdd, 0x5e, 0x07, 0x7b, 0x34, 0xe0,
  5061. 0x56, 0xda, 0x32, 0x13, 0x30, 0xc9, 0x0c, 0xd7, 0xba, 0xe4, 0x1f, 0xa6,
  5062. 0x91, 0x4f, 0x72, 0x9f, 0xd9, 0x5c, 0x62, 0x7d, 0xa6, 0xc2, 0xbc, 0x87,
  5063. 0xae, 0x64, 0x11, 0x94, 0x3b, 0xbc, 0x6c, 0x23, 0xbd, 0x7d, 0x00, 0xb4,
  5064. 0x99, 0xf2, 0x68, 0xb5, 0x59, 0x70, 0x93, 0xad, 0x69, 0xd0, 0xb1, 0x28,
  5065. 0x70, 0x92, 0xeb, 0xec, 0x39, 0x80, 0x82, 0xde, 0x44, 0xe2, 0x8a, 0x26,
  5066. 0xb3, 0xe9, 0x45, 0xcf, 0x83, 0x76, 0x9f, 0x6a, 0xa0, 0x46, 0x4a, 0x3d,
  5067. 0x26, 0x56, 0xaf, 0x49, 0x41, 0x26, 0x1b, 0x6a, 0x41, 0x37, 0x65, 0x91,
  5068. 0x72, 0xc4, 0xe7, 0x3c, 0x17, 0x31, 0xae, 0x2e, 0x2b, 0x31, 0x45, 0xe4,
  5069. 0x93, 0xd3, 0x10, 0xaa, 0xc5, 0x62, 0xd5, 0x11, 0x4b, 0x57, 0x1d, 0xad,
  5070. 0x48, 0x06, 0xd0, 0x0d, 0x98, 0xa5, 0xc6, 0x5b, 0xd0, 0x9e, 0x22, 0xc0,
  5071. 0x00, 0x32, 0x5a, 0xf5, 0x1c, 0x89, 0x6d, 0x54, 0x97, 0x55, 0x6b, 0x46,
  5072. 0xc5, 0xc7, 0xc4, 0x48, 0x9c, 0xbf, 0x47, 0xdc, 0x03, 0xc4, 0x1b, 0xcb,
  5073. 0x65, 0xa6, 0x91, 0x9d, 0x6d, 0xf1, 0xb0, 0x7a, 0x4d, 0x3b, 0x03, 0x95,
  5074. 0xf4, 0x8b, 0x0b, 0xae, 0x39, 0xff, 0x3f, 0xf6, 0xc0, 0x14, 0x18, 0x8a,
  5075. 0xe5, 0x19, 0xbd, 0xc1, 0xb4, 0x05, 0x4e, 0x29, 0x2f, 0x0b, 0x33, 0x76,
  5076. 0x28, 0x16, 0xa4, 0xa6, 0x93, 0x04, 0xb5, 0x55, 0x6b, 0x89, 0x3d, 0xa5,
  5077. 0x0f, 0xd3, 0xad, 0xfa, 0xd9, 0xfd, 0x05, 0x5d, 0x48, 0x94, 0x25, 0x5a,
  5078. 0x2c, 0x9a, 0x94, 0x80, 0xb0, 0xe7, 0xcb, 0x4d, 0x77, 0xbf, 0xca, 0xd8,
  5079. 0x55, 0x48, 0xbd, 0x66, 0xb1, 0x85, 0x81, 0xb1, 0x37, 0x79, 0xab, 0x52,
  5080. 0x08, 0x14, 0x12, 0xac, 0xcd, 0x45, 0x4d, 0x53, 0x6b, 0xca, 0x96, 0xc7,
  5081. 0x3b, 0x2f, 0x73, 0xb1, 0x5a, 0x23, 0xbd, 0x65, 0xd5, 0xea, 0x17, 0xb3,
  5082. 0xdc, 0xa1, 0x17, 0x1b, 0x2d, 0xb3, 0x9c, 0xd0, 0xdb, 0x41, 0x77, 0xef,
  5083. 0x93, 0x20, 0x52, 0x3e, 0x9d, 0xf5, 0xbf, 0x33, 0xf7, 0x52, 0xc1, 0x90,
  5084. 0xa0, 0x15, 0x17, 0xce, 0xf7, 0xf7, 0xd0, 0x3a, 0x3b, 0xd1, 0x72, 0x56,
  5085. 0x31, 0x81, 0xae, 0x60, 0xab, 0x40, 0xc1, 0xd1, 0x28, 0x77, 0x53, 0xac,
  5086. 0x9f, 0x11, 0x0a, 0x88, 0x36, 0x4b, 0xda, 0x57, 0xa7, 0x28, 0x5c, 0x85,
  5087. 0xd3, 0x85, 0x9b, 0x79, 0xad, 0x05, 0x1c, 0x37, 0x14, 0x5e, 0x0d, 0xd0,
  5088. 0x23, 0x03, 0x42, 0x1d, 0x48, 0x5d, 0xc5, 0x3c, 0x5a, 0x08, 0xa9, 0x0d,
  5089. 0x6e, 0x82, 0x7c, 0x2e, 0x3c, 0x41, 0xcc, 0x96, 0x8e, 0xad, 0xee, 0x2a,
  5090. 0x61, 0x0b, 0x16, 0x0f, 0xa9, 0x24, 0x40, 0x85, 0xbc, 0x9f, 0x28, 0x8d,
  5091. 0xe6, 0x68, 0x4d, 0x8f, 0x30, 0x48, 0xd9, 0x73, 0x73, 0x6c, 0x9a, 0x7f,
  5092. 0x67, 0xf7, 0xde, 0x4c, 0x0a, 0x8b, 0xe4, 0xb3, 0x08, 0x2a, 0x52, 0xda,
  5093. 0x54, 0xee, 0xcd, 0xb5, 0x62, 0x4a, 0x26, 0x20, 0xfb, 0x40, 0xbb, 0x39,
  5094. 0x3a, 0x0f, 0x09, 0xe8, 0x00, 0xd1, 0x24, 0x97, 0x60, 0xe9, 0x83, 0x83,
  5095. 0xfe, 0x9f, 0x9c, 0x15, 0xcf, 0x69, 0x03, 0x9f, 0x03, 0xe1, 0xe8, 0x6e,
  5096. 0xbd, 0x87, 0x58, 0x68, 0xee, 0xec, 0xd8, 0x29, 0x46, 0x23, 0x49, 0x92,
  5097. 0x72, 0x95, 0x5b, 0x49, 0xca, 0xe0, 0x45, 0x59, 0xb2, 0xca, 0xf4, 0xfc,
  5098. 0xb7, 0x59, 0x37, 0x49, 0x28, 0xbc, 0xf3, 0xd7, 0x61, 0xbc, 0x4b, 0xf3,
  5099. 0xa9, 0x4b, 0x2f, 0x05, 0xa8, 0x01, 0xa5, 0xdc, 0x00, 0x6e, 0x01, 0xb6,
  5100. 0x45, 0x3c, 0xd5, 0x49, 0x7d, 0x5c, 0x25, 0xe8, 0x31, 0x87, 0xb2, 0xb9,
  5101. 0xbf, 0xb3, 0x01, 0x62, 0x0c, 0xd0, 0x48, 0x77, 0xa2, 0x34, 0x0f, 0x16,
  5102. 0x22, 0x28, 0xee, 0x54, 0x08, 0x93, 0x3b, 0xe4, 0xde, 0x7e, 0x63, 0xf7,
  5103. 0x97, 0x16, 0x5d, 0x71, 0x58, 0xc2, 0x2e, 0xf2, 0x36, 0xa6, 0x12, 0x65,
  5104. 0x94, 0x17, 0xac, 0x66, 0x23, 0x7e, 0xc6, 0x72, 0x79, 0x24, 0xce, 0x8f,
  5105. 0x55, 0x19, 0x97, 0x44, 0xfc, 0x55, 0xec, 0x85, 0x26, 0x27, 0xdb, 0x38,
  5106. 0xb1, 0x42, 0x0a, 0xdd, 0x05, 0x99, 0x28, 0xeb, 0x03, 0x6c, 0x9a, 0xe9,
  5107. 0x17, 0xf6, 0x2c, 0xb0, 0xfe, 0xe7, 0xa4, 0xa7, 0x31, 0xda, 0x4d, 0xb0,
  5108. 0x29, 0xdb, 0xdd, 0x8d, 0x12, 0x13, 0x9c, 0xb4, 0xcc, 0x83, 0x97, 0xfb,
  5109. 0x1a, 0xdc, 0x08, 0xd6, 0x30, 0x62, 0xe8, 0xeb, 0x8b, 0x61, 0xcb, 0x1d,
  5110. 0x06, 0xe3, 0xa5, 0x4d, 0x35, 0xdb, 0x59, 0xa8, 0x2d, 0x87, 0x27, 0x44,
  5111. 0x6f, 0xc0, 0x38, 0x97, 0xe4, 0x85, 0x00, 0x02, 0x09, 0xf6, 0x69, 0x3a,
  5112. 0xcf, 0x08, 0x1b, 0x21, 0xbb, 0x79, 0xb1, 0xa1, 0x34, 0x09, 0xe0, 0x80,
  5113. 0xca, 0xb0, 0x78, 0x8a, 0x11, 0x97, 0xd4, 0x07, 0xbe, 0x1b, 0x6a, 0x5d,
  5114. 0xdb, 0xd6, 0x1f, 0x76, 0x6b, 0x16, 0xf0, 0x58, 0x84, 0x5f, 0x59, 0xce,
  5115. 0x62, 0x34, 0xc3, 0xdf, 0x94, 0xb8, 0x2f, 0x84, 0x68, 0xf0, 0xb8, 0x51,
  5116. 0xd9, 0x6d, 0x8e, 0x4a, 0x1d, 0xe6, 0x5c, 0xd8, 0x86, 0x25, 0xe3, 0x24,
  5117. 0xfd, 0x21, 0x61, 0x13, 0x48, 0x3e, 0xf6, 0x7d, 0xa6, 0x71, 0x9b, 0xd2,
  5118. 0x6e, 0xe6, 0xd2, 0x08, 0x94, 0x62, 0x6c, 0x98, 0xfe, 0x2f, 0x9c, 0x88,
  5119. 0x7e, 0x78, 0x15, 0x02, 0x00, 0xf0, 0xba, 0x24, 0x91, 0xf2, 0xdc, 0x47,
  5120. 0x51, 0x4d, 0x15, 0x5e, 0x91, 0x5f, 0x57, 0x5b, 0x1d, 0x35, 0x24, 0x45,
  5121. 0x75, 0x9b, 0x88, 0x75, 0xf1, 0x2f, 0x85, 0xe7, 0x89, 0xd1, 0x01, 0xb4,
  5122. 0xc8, 0x18, 0xb7, 0x97, 0xef, 0x4b, 0x90, 0xf4, 0xbf, 0x10, 0x27, 0x3c,
  5123. 0x60, 0xff, 0xc4, 0x94, 0x20, 0x2f, 0x93, 0x4b, 0x4d, 0xe3, 0x80, 0xf7,
  5124. 0x2c, 0x71, 0xd9, 0xe3, 0x68, 0xb4, 0x77, 0x2b, 0xc7, 0x0d, 0x39, 0x92,
  5125. 0xef, 0x91, 0x0d, 0xb2, 0x11, 0x50, 0x0e, 0xe8, 0xad, 0x3b, 0xf6, 0xb5,
  5126. 0xc6, 0x14, 0x4d, 0x33, 0x53, 0xa7, 0x60, 0x15, 0xc7, 0x27, 0x51, 0xdc,
  5127. 0x54, 0x29, 0xa7, 0x0d, 0x6a, 0x7b, 0x72, 0x13, 0xad, 0x7d, 0x41, 0x19,
  5128. 0x4e, 0x42, 0x49, 0xcc, 0x42, 0xe4, 0xbd, 0x99, 0x13, 0xd9, 0x7f, 0xf3,
  5129. 0x38, 0xa4, 0xb6, 0x33, 0xed, 0x07, 0x48, 0x7e, 0x8e, 0x82, 0xfe, 0x3a,
  5130. 0x9d, 0x75, 0x93, 0xba, 0x25, 0x4e, 0x37, 0x3c, 0x0c, 0xd5, 0x69, 0xa9,
  5131. 0x2d, 0x9e, 0xfd, 0xe8, 0xbb, 0xf5, 0x0c, 0xe2, 0x86, 0xb9, 0x5e, 0x6f,
  5132. 0x28, 0xe4, 0x19, 0xb3, 0x0b, 0xa4, 0x86, 0xd7, 0x24, 0xd0, 0xb8, 0x89,
  5133. 0x7b, 0x76, 0xec, 0x05, 0x10, 0x5b, 0x68, 0xe9, 0x58, 0x66, 0xa3, 0xc5,
  5134. 0xb6, 0x63, 0x20, 0x0e, 0x0e, 0xea, 0x3d, 0x61, 0x5e, 0xda, 0x3d, 0x3c,
  5135. 0xf9, 0xfd, 0xed, 0xa9, 0xdb, 0x52, 0x94, 0x8a, 0x00, 0xca, 0x3c, 0x8d,
  5136. 0x66, 0x8f, 0xb0, 0xf0, 0x5a, 0xca, 0x3f, 0x63, 0x71, 0xbf, 0xca, 0x99,
  5137. 0x37, 0x9b, 0x75, 0x97, 0x89, 0x10, 0x6e, 0xcf, 0xf2, 0xf5, 0xe3, 0xd5,
  5138. 0x45, 0x9b, 0xad, 0x10, 0x71, 0x6c, 0x5f, 0x6f, 0x7f, 0x22, 0x77, 0x18,
  5139. 0x2f, 0xf9, 0x99, 0xc5, 0x69, 0x58, 0x03, 0x12, 0x86, 0x82, 0x3e, 0xbf,
  5140. 0xc2, 0x12, 0x35, 0x43, 0xa3, 0xd9, 0x18, 0x4f, 0x41, 0x11, 0x6b, 0xf3,
  5141. 0x67, 0xaf, 0x3d, 0x78, 0xe4, 0x22, 0x2d, 0xb3, 0x48, 0x43, 0x31, 0x1d,
  5142. 0xef, 0xa8, 0xba, 0x49, 0x8e, 0xa9, 0xa7, 0xb6, 0x18, 0x77, 0x84, 0xca,
  5143. 0xbd, 0xa2, 0x02, 0x1b, 0x6a, 0xf8, 0x5f, 0xda, 0xff, 0xcf, 0x01, 0x6a,
  5144. 0x86, 0x69, 0xa9, 0xe9, 0xcb, 0x60, 0x1e, 0x15, 0xdc, 0x8f, 0x5d, 0x39,
  5145. 0xb5, 0xce, 0x55, 0x5f, 0x47, 0x97, 0xb1, 0x19, 0x6e, 0x21, 0xd6, 0x13,
  5146. 0x39, 0xb2, 0x24, 0xe0, 0x62, 0x82, 0x9f, 0xed, 0x12, 0x81, 0xed, 0xee,
  5147. 0xab, 0xd0, 0x2f, 0x19, 0x89, 0x3f, 0x57, 0x2e, 0xc2, 0xe2, 0x67, 0xe8,
  5148. 0xae, 0x03, 0x56, 0xba, 0xd4, 0xd0, 0xa4, 0x89, 0x03, 0x06, 0x5b, 0xcc,
  5149. 0xf2, 0x22, 0xb8, 0x0e, 0x76, 0x79, 0x4a, 0x42, 0x1d, 0x37, 0x51, 0x5a,
  5150. 0xaa, 0x46, 0x6c, 0x2a, 0xdd, 0x66, 0xfe, 0xc6, 0x68, 0xc3, 0x38, 0xa2,
  5151. 0xae, 0x5b, 0x98, 0x24, 0x5d, 0x43, 0x05, 0x82, 0x38, 0x12, 0xd3, 0xd1,
  5152. 0x75, 0x2d, 0x4f, 0x61, 0xbd, 0xb9, 0x10, 0x87, 0x44, 0x2a, 0x78, 0x07,
  5153. 0xff, 0xf4, 0x0f, 0xa1, 0xf3, 0x68, 0x9f, 0xbe, 0xae, 0xa2, 0x91, 0xf0,
  5154. 0xc7, 0x55, 0x7a, 0x52, 0xd5, 0xa3, 0x8d, 0x6f, 0xe4, 0x90, 0x5c, 0xf3,
  5155. 0x5f, 0xce, 0x3d, 0x23, 0xf9, 0x8e, 0xae, 0x14, 0xfb, 0x82, 0x9a, 0xa3,
  5156. 0x04, 0x5f, 0xbf, 0xad, 0x3e, 0xf2, 0x97, 0x0a, 0x60, 0x40, 0x70, 0x19,
  5157. 0x72, 0xad, 0x66, 0xfb, 0x78, 0x1b, 0x84, 0x6c, 0x98, 0xbc, 0x8c, 0xf8,
  5158. 0x4f, 0xcb, 0xb5, 0xf6, 0xaf, 0x7a, 0xb7, 0x93, 0xef, 0x67, 0x48, 0x02,
  5159. 0x2c, 0xcb, 0xe6, 0x77, 0x0f, 0x7b, 0xc1, 0xee, 0xc5, 0xb6, 0x2d, 0x7e,
  5160. 0x62, 0xa0, 0xc0, 0xa7, 0xa5, 0x80, 0x31, 0x92, 0x50, 0xa1, 0x28, 0x22,
  5161. 0x95, 0x03, 0x17, 0xd1, 0x0f, 0xf6, 0x08, 0xe5, 0xec
  5162. };
  5163. #define CHACHA_BIG_TEST_SIZE 1305
  5164. #if !defined(WOLFSSL_SMALL_STACK) || defined(WOLFSSL_NO_MALLOC)
  5165. byte cipher_big[CHACHA_BIG_TEST_SIZE] = {0};
  5166. byte plain_big[CHACHA_BIG_TEST_SIZE] = {0};
  5167. byte input_big[CHACHA_BIG_TEST_SIZE] = {0};
  5168. #else
  5169. byte* cipher_big;
  5170. byte* plain_big;
  5171. byte* input_big;
  5172. #endif /* WOLFSSL_SMALL_STACK && !WOLFSSL_NO_MALLOC */
  5173. int block_size;
  5174. #endif /* BENCH_EMBEDDED */
  5175. byte a[] = {0x76,0xb8,0xe0,0xad,0xa0,0xf1,0x3d,0x90};
  5176. byte b[] = {0x45,0x40,0xf0,0x5a,0x9f,0x1f,0xb2,0x96};
  5177. byte c[] = {0xde,0x9c,0xba,0x7b,0xf3,0xd6,0x9e,0xf5};
  5178. byte d[] = {0x89,0x67,0x09,0x52,0x60,0x83,0x64,0xfd};
  5179. byte* test_chacha[4];
  5180. test_chacha[0] = a;
  5181. test_chacha[1] = b;
  5182. test_chacha[2] = c;
  5183. test_chacha[3] = d;
  5184. #ifndef BENCH_EMBEDDED
  5185. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  5186. cipher_big = (byte*)XMALLOC(CHACHA_BIG_TEST_SIZE, HEAP_HINT,
  5187. DYNAMIC_TYPE_TMP_BUFFER);
  5188. if (cipher_big == NULL) {
  5189. return MEMORY_E;
  5190. }
  5191. plain_big = (byte*)XMALLOC(CHACHA_BIG_TEST_SIZE, HEAP_HINT,
  5192. DYNAMIC_TYPE_TMP_BUFFER);
  5193. if (plain_big == NULL) {
  5194. return MEMORY_E;
  5195. }
  5196. input_big = (byte*)XMALLOC(CHACHA_BIG_TEST_SIZE, HEAP_HINT,
  5197. DYNAMIC_TYPE_TMP_BUFFER);
  5198. if (input_big == NULL) {
  5199. return MEMORY_E;
  5200. }
  5201. XMEMSET(cipher_big, 0, CHACHA_BIG_TEST_SIZE);
  5202. XMEMSET(plain_big, 0, CHACHA_BIG_TEST_SIZE);
  5203. XMEMSET(input_big, 0, CHACHA_BIG_TEST_SIZE);
  5204. #endif /* WOLFSSL_SMALL_STACK && !WOLFSSL_NO_MALLOC */
  5205. #endif /* BENCH_EMBEDDED */
  5206. for (i = 0; i < times; ++i) {
  5207. if (i < 3) {
  5208. keySz = 32;
  5209. }
  5210. else {
  5211. keySz = 16;
  5212. }
  5213. XMEMCPY(plain, keys[i], keySz);
  5214. XMEMSET(cipher, 0, 32);
  5215. XMEMCPY(cipher + 4, ivs[i], 8);
  5216. ret |= wc_Chacha_SetKey(&enc, keys[i], keySz);
  5217. ret |= wc_Chacha_SetKey(&dec, keys[i], keySz);
  5218. if (ret != 0)
  5219. return ret;
  5220. ret |= wc_Chacha_SetIV(&enc, cipher, 0);
  5221. ret |= wc_Chacha_SetIV(&dec, cipher, 0);
  5222. if (ret != 0)
  5223. return ret;
  5224. XMEMCPY(plain, input, 8);
  5225. ret |= wc_Chacha_Process(&enc, cipher, plain, (word32)8);
  5226. ret |= wc_Chacha_Process(&dec, plain, cipher, (word32)8);
  5227. if (ret != 0)
  5228. return ret;
  5229. if (XMEMCMP(test_chacha[i], cipher, 8))
  5230. return -4700 - i;
  5231. if (XMEMCMP(plain, input, 8))
  5232. return -4710 - i;
  5233. }
  5234. /* test of starting at a different counter
  5235. encrypts all of the information and decrypts starting at 2nd chunk */
  5236. XMEMSET(plain, 0, sizeof(plain));
  5237. XMEMSET(sliver, 1, sizeof(sliver)); /* set as 1's to not match plain */
  5238. XMEMSET(cipher, 0, sizeof(cipher));
  5239. XMEMCPY(cipher + 4, ivs[0], 8);
  5240. ret |= wc_Chacha_SetKey(&enc, keys[0], keySz);
  5241. ret |= wc_Chacha_SetKey(&dec, keys[0], keySz);
  5242. if (ret != 0)
  5243. return ret;
  5244. ret |= wc_Chacha_SetIV(&enc, cipher, 0);
  5245. ret |= wc_Chacha_SetIV(&dec, cipher, 1);
  5246. if (ret != 0)
  5247. return ret;
  5248. ret |= wc_Chacha_Process(&enc, cipher, plain, sizeof(plain));
  5249. ret |= wc_Chacha_Process(&dec, sliver, cipher + 64, sizeof(sliver));
  5250. if (ret != 0)
  5251. return ret;
  5252. if (XMEMCMP(plain + 64, sliver, 64))
  5253. return -4720;
  5254. #ifndef BENCH_EMBEDDED
  5255. /* test of encrypting more data */
  5256. keySz = 32;
  5257. ret |= wc_Chacha_SetKey(&enc, keys[0], keySz);
  5258. ret |= wc_Chacha_SetKey(&dec, keys[0], keySz);
  5259. if (ret != 0)
  5260. return ret;
  5261. ret |= wc_Chacha_SetIV(&enc, ivs[2], 0);
  5262. ret |= wc_Chacha_SetIV(&dec, ivs[2], 0);
  5263. if (ret != 0)
  5264. return ret;
  5265. ret |= wc_Chacha_Process(&enc, cipher_big, plain_big, CHACHA_BIG_TEST_SIZE);
  5266. ret |= wc_Chacha_Process(&dec, plain_big, cipher_big,
  5267. CHACHA_BIG_TEST_SIZE);
  5268. if (ret != 0)
  5269. return ret;
  5270. if (XMEMCMP(plain_big, input_big, CHACHA_BIG_TEST_SIZE))
  5271. return -4721;
  5272. if (XMEMCMP(cipher_big, cipher_big_result, CHACHA_BIG_TEST_SIZE))
  5273. return -4722;
  5274. for (i = 0; i < 18; ++i) {
  5275. /* this will test all paths
  5276. * block sizes: 1 3 7 15 31 63 127 255 511 (i = 0- 8)
  5277. * 2 4 8 16 32 64 128 256 512 (i = 9-17)
  5278. */
  5279. block_size = (2 << (i%9)) - (i<9?1:0);
  5280. keySz = 32;
  5281. ret |= wc_Chacha_SetKey(&enc, keys[0], keySz);
  5282. ret |= wc_Chacha_SetKey(&dec, keys[0], keySz);
  5283. if (ret != 0)
  5284. return ret;
  5285. ret |= wc_Chacha_SetIV(&enc, ivs[2], 0);
  5286. ret |= wc_Chacha_SetIV(&dec, ivs[2], 0);
  5287. if (ret != 0)
  5288. return ret;
  5289. ret |= wc_Chacha_Process(&enc, cipher_big, plain_big , block_size);
  5290. ret |= wc_Chacha_Process(&dec, plain_big , cipher_big, block_size);
  5291. if (ret != 0)
  5292. return ret;
  5293. if (XMEMCMP(plain_big, input_big, block_size))
  5294. return -4740-i*2;
  5295. if (XMEMCMP(cipher_big, cipher_big_result, block_size))
  5296. return -4741-i*2;
  5297. }
  5298. /* Streaming test */
  5299. for (i = 1; i <= (int)CHACHA_CHUNK_BYTES + 1; i++) {
  5300. int j, rem;
  5301. ret = wc_Chacha_SetKey(&enc, keys[0], keySz);
  5302. if (ret != 0)
  5303. return -4725;
  5304. ret = wc_Chacha_SetKey(&dec, keys[0], keySz);
  5305. if (ret != 0)
  5306. return -4726;
  5307. ret = wc_Chacha_SetIV(&enc, ivs[2], 0);
  5308. if (ret != 0)
  5309. return -4727;
  5310. ret = wc_Chacha_SetIV(&dec, ivs[2], 0);
  5311. if (ret != 0)
  5312. return -4728;
  5313. for (j = 0; j < CHACHA_BIG_TEST_SIZE - i; j+= i) {
  5314. ret = wc_Chacha_Process(&enc, cipher_big + j, plain_big + j, i);
  5315. if (ret != 0)
  5316. return -4729;
  5317. ret = wc_Chacha_Process(&dec, plain_big + j, cipher_big + j, i);
  5318. if (ret != 0)
  5319. return -4730;
  5320. }
  5321. rem = CHACHA_BIG_TEST_SIZE - j;
  5322. ret = wc_Chacha_Process(&enc, cipher_big + j, plain_big + j, rem);
  5323. if (ret != 0)
  5324. return -4731;
  5325. ret = wc_Chacha_Process(&dec, plain_big + j, cipher_big + j, rem);
  5326. if (ret != 0)
  5327. return -4732;
  5328. if (XMEMCMP(plain_big, input_big, CHACHA_BIG_TEST_SIZE))
  5329. return -4733;
  5330. if (XMEMCMP(cipher_big, cipher_big_result, CHACHA_BIG_TEST_SIZE))
  5331. return -4734;
  5332. }
  5333. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  5334. XFREE(cipher_big, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  5335. XFREE(plain_big, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  5336. XFREE(input_big, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  5337. #endif /* WOLFSSL_SMALL_STACK && !WOLFSSL_NO_MALLOC */
  5338. #endif /* BENCH_EMBEDDED */
  5339. return 0;
  5340. }
  5341. #endif /* HAVE_CHACHA */
  5342. #ifdef HAVE_POLY1305
  5343. WOLFSSL_TEST_SUBROUTINE int poly1305_test(void)
  5344. {
  5345. int ret = 0;
  5346. int i;
  5347. byte tag[16];
  5348. Poly1305 enc;
  5349. WOLFSSL_SMALL_STACK_STATIC const byte msg1[] =
  5350. {
  5351. 0x43,0x72,0x79,0x70,0x74,0x6f,0x67,0x72,
  5352. 0x61,0x70,0x68,0x69,0x63,0x20,0x46,0x6f,
  5353. 0x72,0x75,0x6d,0x20,0x52,0x65,0x73,0x65,
  5354. 0x61,0x72,0x63,0x68,0x20,0x47,0x72,0x6f,
  5355. 0x75,0x70
  5356. };
  5357. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] =
  5358. {
  5359. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x77,0x6f,0x72,
  5360. 0x6c,0x64,0x21
  5361. };
  5362. WOLFSSL_SMALL_STACK_STATIC const byte msg3[] =
  5363. {
  5364. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5365. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5366. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5367. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  5368. };
  5369. WOLFSSL_SMALL_STACK_STATIC const byte msg4[] =
  5370. {
  5371. 0xd3,0x1a,0x8d,0x34,0x64,0x8e,0x60,0xdb,
  5372. 0x7b,0x86,0xaf,0xbc,0x53,0xef,0x7e,0xc2,
  5373. 0xa4,0xad,0xed,0x51,0x29,0x6e,0x08,0xfe,
  5374. 0xa9,0xe2,0xb5,0xa7,0x36,0xee,0x62,0xd6,
  5375. 0x3d,0xbe,0xa4,0x5e,0x8c,0xa9,0x67,0x12,
  5376. 0x82,0xfa,0xfb,0x69,0xda,0x92,0x72,0x8b,
  5377. 0x1a,0x71,0xde,0x0a,0x9e,0x06,0x0b,0x29,
  5378. 0x05,0xd6,0xa5,0xb6,0x7e,0xcd,0x3b,0x36,
  5379. 0x92,0xdd,0xbd,0x7f,0x2d,0x77,0x8b,0x8c,
  5380. 0x98,0x03,0xae,0xe3,0x28,0x09,0x1b,0x58,
  5381. 0xfa,0xb3,0x24,0xe4,0xfa,0xd6,0x75,0x94,
  5382. 0x55,0x85,0x80,0x8b,0x48,0x31,0xd7,0xbc,
  5383. 0x3f,0xf4,0xde,0xf0,0x8e,0x4b,0x7a,0x9d,
  5384. 0xe5,0x76,0xd2,0x65,0x86,0xce,0xc6,0x4b,
  5385. 0x61,0x16
  5386. };
  5387. WOLFSSL_SMALL_STACK_STATIC const byte msg5[] =
  5388. {
  5389. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  5390. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  5391. };
  5392. WOLFSSL_SMALL_STACK_STATIC const byte msg6[] =
  5393. {
  5394. 0xd3,0x1a,0x8d,0x34,0x64,0x8e,0x60,0xdb,
  5395. 0x7b,0x86,0xaf,0xbc,0x53,0xef,0x7e,0xc2,
  5396. 0xa4,0xad,0xed,0x51,0x29,0x6e,0x08,0xfe,
  5397. 0xa9,0xe2,0xb5,0xa7,0x36,0xee,0x62,0xd6,
  5398. 0x3d,0xbe,0xa4,0x5e,0x8c,0xa9,0x67,0x12,
  5399. 0x82,0xfa,0xfb,0x69,0xda,0x92,0x72,0x8b,
  5400. 0xfa,0xb3,0x24,0xe4,0xfa,0xd6,0x75,0x94,
  5401. 0x1a,0x71,0xde,0x0a,0x9e,0x06,0x0b,0x29,
  5402. 0xa9,0xe2,0xb5,0xa7,0x36,0xee,0x62,0xd6,
  5403. 0x3d,0xbe,0xa4,0x5e,0x8c,0xa9,0x67,0x12,
  5404. 0xfa,0xb3,0x24,0xe4,0xfa,0xd6,0x75,0x94,
  5405. 0x05,0xd6,0xa5,0xb6,0x7e,0xcd,0x3b,0x36,
  5406. 0x92,0xdd,0xbd,0x7f,0x2d,0x77,0x8b,0x8c,
  5407. 0x7b,0x86,0xaf,0xbc,0x53,0xef,0x7e,0xc2,
  5408. 0x98,0x03,0xae,0xe3,0x28,0x09,0x1b,0x58,
  5409. 0xfa,0xb3,0x24,0xe4,0xfa,0xd6,0x75,0x94,
  5410. 0x55,0x85,0x80,0x8b,0x48,0x31,0xd7,0xbc,
  5411. 0x3f,0xf4,0xde,0xf0,0x8e,0x4b,0x7a,0x9d,
  5412. 0xe5,0x76,0xd2,0x65,0x86,0xce,0xc6,0x4b,
  5413. 0x61,0x16
  5414. };
  5415. byte additional[] =
  5416. {
  5417. 0x50,0x51,0x52,0x53,0xc0,0xc1,0xc2,0xc3,
  5418. 0xc4,0xc5,0xc6,0xc7
  5419. };
  5420. WOLFSSL_SMALL_STACK_STATIC const byte correct0[] =
  5421. {
  5422. 0x01,0x03,0x80,0x8a,0xfb,0x0d,0xb2,0xfd,
  5423. 0x4a,0xbf,0xf6,0xaf,0x41,0x49,0xf5,0x1b
  5424. };
  5425. WOLFSSL_SMALL_STACK_STATIC const byte correct1[] =
  5426. {
  5427. 0xa8,0x06,0x1d,0xc1,0x30,0x51,0x36,0xc6,
  5428. 0xc2,0x2b,0x8b,0xaf,0x0c,0x01,0x27,0xa9
  5429. };
  5430. WOLFSSL_SMALL_STACK_STATIC const byte correct2[] =
  5431. {
  5432. 0xa6,0xf7,0x45,0x00,0x8f,0x81,0xc9,0x16,
  5433. 0xa2,0x0d,0xcc,0x74,0xee,0xf2,0xb2,0xf0
  5434. };
  5435. WOLFSSL_SMALL_STACK_STATIC const byte correct3[] =
  5436. {
  5437. 0x49,0xec,0x78,0x09,0x0e,0x48,0x1e,0xc6,
  5438. 0xc2,0x6b,0x33,0xb9,0x1c,0xcc,0x03,0x07
  5439. };
  5440. WOLFSSL_SMALL_STACK_STATIC const byte correct4[] =
  5441. {
  5442. 0x1a,0xe1,0x0b,0x59,0x4f,0x09,0xe2,0x6a,
  5443. 0x7e,0x90,0x2e,0xcb,0xd0,0x60,0x06,0x91
  5444. };
  5445. WOLFSSL_SMALL_STACK_STATIC const byte correct5[] =
  5446. {
  5447. 0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5448. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5449. };
  5450. WOLFSSL_SMALL_STACK_STATIC const byte correct6[] =
  5451. {
  5452. 0xea,0x11,0x5c,0x4f,0xd0,0xc0,0x10,0xae,
  5453. 0xf7,0xdf,0xda,0x77,0xa2,0xe9,0xaf,0xca
  5454. };
  5455. WOLFSSL_SMALL_STACK_STATIC const byte key[] = {
  5456. 0x85,0xd6,0xbe,0x78,0x57,0x55,0x6d,0x33,
  5457. 0x7f,0x44,0x52,0xfe,0x42,0xd5,0x06,0xa8,
  5458. 0x01,0x03,0x80,0x8a,0xfb,0x0d,0xb2,0xfd,
  5459. 0x4a,0xbf,0xf6,0xaf,0x41,0x49,0xf5,0x1b
  5460. };
  5461. WOLFSSL_SMALL_STACK_STATIC const byte key2[] = {
  5462. 0x74,0x68,0x69,0x73,0x20,0x69,0x73,0x20,
  5463. 0x33,0x32,0x2d,0x62,0x79,0x74,0x65,0x20,
  5464. 0x6b,0x65,0x79,0x20,0x66,0x6f,0x72,0x20,
  5465. 0x50,0x6f,0x6c,0x79,0x31,0x33,0x30,0x35
  5466. };
  5467. WOLFSSL_SMALL_STACK_STATIC const byte key4[] = {
  5468. 0x7b,0xac,0x2b,0x25,0x2d,0xb4,0x47,0xaf,
  5469. 0x09,0xb6,0x7a,0x55,0xa4,0xe9,0x55,0x84,
  5470. 0x0a,0xe1,0xd6,0x73,0x10,0x75,0xd9,0xeb,
  5471. 0x2a,0x93,0x75,0x78,0x3e,0xd5,0x53,0xff
  5472. };
  5473. WOLFSSL_SMALL_STACK_STATIC const byte key5[] = {
  5474. 0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5475. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5476. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5477. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  5478. };
  5479. const byte* msgs[] = {NULL, msg1, msg2, msg3, msg5, msg6};
  5480. word32 szm[] = {0, sizeof(msg1), sizeof(msg2),
  5481. sizeof(msg3), sizeof(msg5), sizeof(msg6)};
  5482. const byte* keys[] = {key, key, key2, key2, key5, key};
  5483. const byte* tests[] = {correct0, correct1, correct2, correct3, correct5,
  5484. correct6};
  5485. for (i = 0; i < 6; i++) {
  5486. ret = wc_Poly1305SetKey(&enc, keys[i], 32);
  5487. if (ret != 0)
  5488. return -4800 - i;
  5489. ret = wc_Poly1305Update(&enc, msgs[i], szm[i]);
  5490. if (ret != 0)
  5491. return -4810 - i;
  5492. ret = wc_Poly1305Final(&enc, tag);
  5493. if (ret != 0)
  5494. return -4820 - i;
  5495. if (XMEMCMP(tag, tests[i], sizeof(tag)))
  5496. return -4830 - i;
  5497. }
  5498. /* Check TLS MAC function from 2.8.2 https://tools.ietf.org/html/rfc7539 */
  5499. XMEMSET(tag, 0, sizeof(tag));
  5500. ret = wc_Poly1305SetKey(&enc, key4, sizeof(key4));
  5501. if (ret != 0)
  5502. return -4840;
  5503. ret = wc_Poly1305_MAC(&enc, additional, sizeof(additional),
  5504. (byte*)msg4, sizeof(msg4), tag, sizeof(tag));
  5505. if (ret != 0)
  5506. return -4841;
  5507. if (XMEMCMP(tag, correct4, sizeof(tag)))
  5508. return -4842;
  5509. /* Check fail of TLS MAC function if altering additional data */
  5510. XMEMSET(tag, 0, sizeof(tag));
  5511. additional[0]++;
  5512. ret = wc_Poly1305_MAC(&enc, additional, sizeof(additional),
  5513. (byte*)msg4, sizeof(msg4), tag, sizeof(tag));
  5514. if (ret != 0)
  5515. return -4843;
  5516. if (XMEMCMP(tag, correct4, sizeof(tag)) == 0)
  5517. return -4844;
  5518. return 0;
  5519. }
  5520. #endif /* HAVE_POLY1305 */
  5521. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  5522. WOLFSSL_TEST_SUBROUTINE int chacha20_poly1305_aead_test(void)
  5523. {
  5524. /* Test #1 from Section 2.8.2 of draft-irtf-cfrg-chacha20-poly1305-10 */
  5525. /* https://tools.ietf.org/html/draft-irtf-cfrg-chacha20-poly1305-10 */
  5526. WOLFSSL_SMALL_STACK_STATIC const byte key1[] = {
  5527. 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
  5528. 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  5529. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
  5530. 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f
  5531. };
  5532. WOLFSSL_SMALL_STACK_STATIC const byte plaintext1[] = {
  5533. 0x4c, 0x61, 0x64, 0x69, 0x65, 0x73, 0x20, 0x61,
  5534. 0x6e, 0x64, 0x20, 0x47, 0x65, 0x6e, 0x74, 0x6c,
  5535. 0x65, 0x6d, 0x65, 0x6e, 0x20, 0x6f, 0x66, 0x20,
  5536. 0x74, 0x68, 0x65, 0x20, 0x63, 0x6c, 0x61, 0x73,
  5537. 0x73, 0x20, 0x6f, 0x66, 0x20, 0x27, 0x39, 0x39,
  5538. 0x3a, 0x20, 0x49, 0x66, 0x20, 0x49, 0x20, 0x63,
  5539. 0x6f, 0x75, 0x6c, 0x64, 0x20, 0x6f, 0x66, 0x66,
  5540. 0x65, 0x72, 0x20, 0x79, 0x6f, 0x75, 0x20, 0x6f,
  5541. 0x6e, 0x6c, 0x79, 0x20, 0x6f, 0x6e, 0x65, 0x20,
  5542. 0x74, 0x69, 0x70, 0x20, 0x66, 0x6f, 0x72, 0x20,
  5543. 0x74, 0x68, 0x65, 0x20, 0x66, 0x75, 0x74, 0x75,
  5544. 0x72, 0x65, 0x2c, 0x20, 0x73, 0x75, 0x6e, 0x73,
  5545. 0x63, 0x72, 0x65, 0x65, 0x6e, 0x20, 0x77, 0x6f,
  5546. 0x75, 0x6c, 0x64, 0x20, 0x62, 0x65, 0x20, 0x69,
  5547. 0x74, 0x2e
  5548. };
  5549. WOLFSSL_SMALL_STACK_STATIC const byte iv1[] = {
  5550. 0x07, 0x00, 0x00, 0x00, 0x40, 0x41, 0x42, 0x43,
  5551. 0x44, 0x45, 0x46, 0x47
  5552. };
  5553. WOLFSSL_SMALL_STACK_STATIC const byte aad1[] = { /* additional data */
  5554. 0x50, 0x51, 0x52, 0x53, 0xc0, 0xc1, 0xc2, 0xc3,
  5555. 0xc4, 0xc5, 0xc6, 0xc7
  5556. };
  5557. WOLFSSL_SMALL_STACK_STATIC const byte cipher1[] = { /* expected output from operation */
  5558. 0xd3, 0x1a, 0x8d, 0x34, 0x64, 0x8e, 0x60, 0xdb,
  5559. 0x7b, 0x86, 0xaf, 0xbc, 0x53, 0xef, 0x7e, 0xc2,
  5560. 0xa4, 0xad, 0xed, 0x51, 0x29, 0x6e, 0x08, 0xfe,
  5561. 0xa9, 0xe2, 0xb5, 0xa7, 0x36, 0xee, 0x62, 0xd6,
  5562. 0x3d, 0xbe, 0xa4, 0x5e, 0x8c, 0xa9, 0x67, 0x12,
  5563. 0x82, 0xfa, 0xfb, 0x69, 0xda, 0x92, 0x72, 0x8b,
  5564. 0x1a, 0x71, 0xde, 0x0a, 0x9e, 0x06, 0x0b, 0x29,
  5565. 0x05, 0xd6, 0xa5, 0xb6, 0x7e, 0xcd, 0x3b, 0x36,
  5566. 0x92, 0xdd, 0xbd, 0x7f, 0x2d, 0x77, 0x8b, 0x8c,
  5567. 0x98, 0x03, 0xae, 0xe3, 0x28, 0x09, 0x1b, 0x58,
  5568. 0xfa, 0xb3, 0x24, 0xe4, 0xfa, 0xd6, 0x75, 0x94,
  5569. 0x55, 0x85, 0x80, 0x8b, 0x48, 0x31, 0xd7, 0xbc,
  5570. 0x3f, 0xf4, 0xde, 0xf0, 0x8e, 0x4b, 0x7a, 0x9d,
  5571. 0xe5, 0x76, 0xd2, 0x65, 0x86, 0xce, 0xc6, 0x4b,
  5572. 0x61, 0x16
  5573. };
  5574. WOLFSSL_SMALL_STACK_STATIC const byte authTag1[] = { /* expected output from operation */
  5575. 0x1a, 0xe1, 0x0b, 0x59, 0x4f, 0x09, 0xe2, 0x6a,
  5576. 0x7e, 0x90, 0x2e, 0xcb, 0xd0, 0x60, 0x06, 0x91
  5577. };
  5578. /* Test #2 from Appendix A.2 in draft-irtf-cfrg-chacha20-poly1305-10 */
  5579. /* https://tools.ietf.org/html/draft-irtf-cfrg-chacha20-poly1305-10 */
  5580. WOLFSSL_SMALL_STACK_STATIC const byte key2[] = {
  5581. 0x1c, 0x92, 0x40, 0xa5, 0xeb, 0x55, 0xd3, 0x8a,
  5582. 0xf3, 0x33, 0x88, 0x86, 0x04, 0xf6, 0xb5, 0xf0,
  5583. 0x47, 0x39, 0x17, 0xc1, 0x40, 0x2b, 0x80, 0x09,
  5584. 0x9d, 0xca, 0x5c, 0xbc, 0x20, 0x70, 0x75, 0xc0
  5585. };
  5586. WOLFSSL_SMALL_STACK_STATIC const byte plaintext2[] = {
  5587. 0x49, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x65, 0x74,
  5588. 0x2d, 0x44, 0x72, 0x61, 0x66, 0x74, 0x73, 0x20,
  5589. 0x61, 0x72, 0x65, 0x20, 0x64, 0x72, 0x61, 0x66,
  5590. 0x74, 0x20, 0x64, 0x6f, 0x63, 0x75, 0x6d, 0x65,
  5591. 0x6e, 0x74, 0x73, 0x20, 0x76, 0x61, 0x6c, 0x69,
  5592. 0x64, 0x20, 0x66, 0x6f, 0x72, 0x20, 0x61, 0x20,
  5593. 0x6d, 0x61, 0x78, 0x69, 0x6d, 0x75, 0x6d, 0x20,
  5594. 0x6f, 0x66, 0x20, 0x73, 0x69, 0x78, 0x20, 0x6d,
  5595. 0x6f, 0x6e, 0x74, 0x68, 0x73, 0x20, 0x61, 0x6e,
  5596. 0x64, 0x20, 0x6d, 0x61, 0x79, 0x20, 0x62, 0x65,
  5597. 0x20, 0x75, 0x70, 0x64, 0x61, 0x74, 0x65, 0x64,
  5598. 0x2c, 0x20, 0x72, 0x65, 0x70, 0x6c, 0x61, 0x63,
  5599. 0x65, 0x64, 0x2c, 0x20, 0x6f, 0x72, 0x20, 0x6f,
  5600. 0x62, 0x73, 0x6f, 0x6c, 0x65, 0x74, 0x65, 0x64,
  5601. 0x20, 0x62, 0x79, 0x20, 0x6f, 0x74, 0x68, 0x65,
  5602. 0x72, 0x20, 0x64, 0x6f, 0x63, 0x75, 0x6d, 0x65,
  5603. 0x6e, 0x74, 0x73, 0x20, 0x61, 0x74, 0x20, 0x61,
  5604. 0x6e, 0x79, 0x20, 0x74, 0x69, 0x6d, 0x65, 0x2e,
  5605. 0x20, 0x49, 0x74, 0x20, 0x69, 0x73, 0x20, 0x69,
  5606. 0x6e, 0x61, 0x70, 0x70, 0x72, 0x6f, 0x70, 0x72,
  5607. 0x69, 0x61, 0x74, 0x65, 0x20, 0x74, 0x6f, 0x20,
  5608. 0x75, 0x73, 0x65, 0x20, 0x49, 0x6e, 0x74, 0x65,
  5609. 0x72, 0x6e, 0x65, 0x74, 0x2d, 0x44, 0x72, 0x61,
  5610. 0x66, 0x74, 0x73, 0x20, 0x61, 0x73, 0x20, 0x72,
  5611. 0x65, 0x66, 0x65, 0x72, 0x65, 0x6e, 0x63, 0x65,
  5612. 0x20, 0x6d, 0x61, 0x74, 0x65, 0x72, 0x69, 0x61,
  5613. 0x6c, 0x20, 0x6f, 0x72, 0x20, 0x74, 0x6f, 0x20,
  5614. 0x63, 0x69, 0x74, 0x65, 0x20, 0x74, 0x68, 0x65,
  5615. 0x6d, 0x20, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20,
  5616. 0x74, 0x68, 0x61, 0x6e, 0x20, 0x61, 0x73, 0x20,
  5617. 0x2f, 0xe2, 0x80, 0x9c, 0x77, 0x6f, 0x72, 0x6b,
  5618. 0x20, 0x69, 0x6e, 0x20, 0x70, 0x72, 0x6f, 0x67,
  5619. 0x72, 0x65, 0x73, 0x73, 0x2e, 0x2f, 0xe2, 0x80,
  5620. 0x9d
  5621. };
  5622. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] = {
  5623. 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04,
  5624. 0x05, 0x06, 0x07, 0x08
  5625. };
  5626. WOLFSSL_SMALL_STACK_STATIC const byte aad2[] = { /* additional data */
  5627. 0xf3, 0x33, 0x88, 0x86, 0x00, 0x00, 0x00, 0x00,
  5628. 0x00, 0x00, 0x4e, 0x91
  5629. };
  5630. WOLFSSL_SMALL_STACK_STATIC const byte cipher2[] = { /* expected output from operation */
  5631. 0x64, 0xa0, 0x86, 0x15, 0x75, 0x86, 0x1a, 0xf4,
  5632. 0x60, 0xf0, 0x62, 0xc7, 0x9b, 0xe6, 0x43, 0xbd,
  5633. 0x5e, 0x80, 0x5c, 0xfd, 0x34, 0x5c, 0xf3, 0x89,
  5634. 0xf1, 0x08, 0x67, 0x0a, 0xc7, 0x6c, 0x8c, 0xb2,
  5635. 0x4c, 0x6c, 0xfc, 0x18, 0x75, 0x5d, 0x43, 0xee,
  5636. 0xa0, 0x9e, 0xe9, 0x4e, 0x38, 0x2d, 0x26, 0xb0,
  5637. 0xbd, 0xb7, 0xb7, 0x3c, 0x32, 0x1b, 0x01, 0x00,
  5638. 0xd4, 0xf0, 0x3b, 0x7f, 0x35, 0x58, 0x94, 0xcf,
  5639. 0x33, 0x2f, 0x83, 0x0e, 0x71, 0x0b, 0x97, 0xce,
  5640. 0x98, 0xc8, 0xa8, 0x4a, 0xbd, 0x0b, 0x94, 0x81,
  5641. 0x14, 0xad, 0x17, 0x6e, 0x00, 0x8d, 0x33, 0xbd,
  5642. 0x60, 0xf9, 0x82, 0xb1, 0xff, 0x37, 0xc8, 0x55,
  5643. 0x97, 0x97, 0xa0, 0x6e, 0xf4, 0xf0, 0xef, 0x61,
  5644. 0xc1, 0x86, 0x32, 0x4e, 0x2b, 0x35, 0x06, 0x38,
  5645. 0x36, 0x06, 0x90, 0x7b, 0x6a, 0x7c, 0x02, 0xb0,
  5646. 0xf9, 0xf6, 0x15, 0x7b, 0x53, 0xc8, 0x67, 0xe4,
  5647. 0xb9, 0x16, 0x6c, 0x76, 0x7b, 0x80, 0x4d, 0x46,
  5648. 0xa5, 0x9b, 0x52, 0x16, 0xcd, 0xe7, 0xa4, 0xe9,
  5649. 0x90, 0x40, 0xc5, 0xa4, 0x04, 0x33, 0x22, 0x5e,
  5650. 0xe2, 0x82, 0xa1, 0xb0, 0xa0, 0x6c, 0x52, 0x3e,
  5651. 0xaf, 0x45, 0x34, 0xd7, 0xf8, 0x3f, 0xa1, 0x15,
  5652. 0x5b, 0x00, 0x47, 0x71, 0x8c, 0xbc, 0x54, 0x6a,
  5653. 0x0d, 0x07, 0x2b, 0x04, 0xb3, 0x56, 0x4e, 0xea,
  5654. 0x1b, 0x42, 0x22, 0x73, 0xf5, 0x48, 0x27, 0x1a,
  5655. 0x0b, 0xb2, 0x31, 0x60, 0x53, 0xfa, 0x76, 0x99,
  5656. 0x19, 0x55, 0xeb, 0xd6, 0x31, 0x59, 0x43, 0x4e,
  5657. 0xce, 0xbb, 0x4e, 0x46, 0x6d, 0xae, 0x5a, 0x10,
  5658. 0x73, 0xa6, 0x72, 0x76, 0x27, 0x09, 0x7a, 0x10,
  5659. 0x49, 0xe6, 0x17, 0xd9, 0x1d, 0x36, 0x10, 0x94,
  5660. 0xfa, 0x68, 0xf0, 0xff, 0x77, 0x98, 0x71, 0x30,
  5661. 0x30, 0x5b, 0xea, 0xba, 0x2e, 0xda, 0x04, 0xdf,
  5662. 0x99, 0x7b, 0x71, 0x4d, 0x6c, 0x6f, 0x2c, 0x29,
  5663. 0xa6, 0xad, 0x5c, 0xb4, 0x02, 0x2b, 0x02, 0x70,
  5664. 0x9b
  5665. };
  5666. WOLFSSL_SMALL_STACK_STATIC const byte authTag2[] = { /* expected output from operation */
  5667. 0xee, 0xad, 0x9d, 0x67, 0x89, 0x0c, 0xbb, 0x22,
  5668. 0x39, 0x23, 0x36, 0xfe, 0xa1, 0x85, 0x1f, 0x38
  5669. };
  5670. byte generatedCiphertext[265]; /* max plaintext2/cipher2 */
  5671. byte generatedPlaintext[265]; /* max plaintext2/cipher2 */
  5672. byte generatedAuthTag[CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE];
  5673. int err;
  5674. ChaChaPoly_Aead aead;
  5675. #if !defined(USE_INTEL_CHACHA_SPEEDUP) && !defined(WOLFSSL_ARMASM)
  5676. #define TEST_SMALL_CHACHA_CHUNKS 32
  5677. #else
  5678. #define TEST_SMALL_CHACHA_CHUNKS 64
  5679. #endif
  5680. #ifdef TEST_SMALL_CHACHA_CHUNKS
  5681. word32 testLen;
  5682. #endif
  5683. XMEMSET(generatedCiphertext, 0, sizeof(generatedCiphertext));
  5684. XMEMSET(generatedAuthTag, 0, sizeof(generatedAuthTag));
  5685. XMEMSET(generatedPlaintext, 0, sizeof(generatedPlaintext));
  5686. /* Parameter Validation testing */
  5687. /* Encrypt */
  5688. err = wc_ChaCha20Poly1305_Encrypt(NULL, iv1, aad1, sizeof(aad1), plaintext1,
  5689. sizeof(plaintext1), generatedCiphertext, generatedAuthTag);
  5690. if (err != BAD_FUNC_ARG)
  5691. return -4900;
  5692. err = wc_ChaCha20Poly1305_Encrypt(key1, NULL, aad1, sizeof(aad1),
  5693. plaintext1, sizeof(plaintext1), generatedCiphertext,
  5694. generatedAuthTag);
  5695. if (err != BAD_FUNC_ARG)
  5696. return -4901;
  5697. err = wc_ChaCha20Poly1305_Encrypt(key1, iv1, aad1, sizeof(aad1), NULL,
  5698. sizeof(plaintext1), generatedCiphertext, generatedAuthTag);
  5699. if (err != BAD_FUNC_ARG)
  5700. return -4902;
  5701. err = wc_ChaCha20Poly1305_Encrypt(key1, iv1, aad1, sizeof(aad1), plaintext1,
  5702. sizeof(plaintext1), NULL, generatedAuthTag);
  5703. if (err != BAD_FUNC_ARG)
  5704. return -4903;
  5705. err = wc_ChaCha20Poly1305_Encrypt(key1, iv1, aad1, sizeof(aad1), plaintext1,
  5706. sizeof(plaintext1), generatedCiphertext, NULL);
  5707. if (err != BAD_FUNC_ARG)
  5708. return -4904;
  5709. err = wc_ChaCha20Poly1305_Encrypt(key1, iv1, aad1, sizeof(aad1), NULL,
  5710. sizeof(plaintext1), generatedCiphertext, generatedAuthTag);
  5711. if (err != BAD_FUNC_ARG)
  5712. return -4905;
  5713. /* Decrypt */
  5714. err = wc_ChaCha20Poly1305_Decrypt(NULL, iv2, aad2, sizeof(aad2), cipher2,
  5715. sizeof(cipher2), authTag2, generatedPlaintext);
  5716. if (err != BAD_FUNC_ARG)
  5717. return -4906;
  5718. err = wc_ChaCha20Poly1305_Decrypt(key2, NULL, aad2, sizeof(aad2), cipher2,
  5719. sizeof(cipher2), authTag2, generatedPlaintext);
  5720. if (err != BAD_FUNC_ARG)
  5721. return -4907;
  5722. err = wc_ChaCha20Poly1305_Decrypt(key2, iv2, aad2, sizeof(aad2), NULL,
  5723. sizeof(cipher2), authTag2, generatedPlaintext);
  5724. if (err != BAD_FUNC_ARG)
  5725. return -4908;
  5726. err = wc_ChaCha20Poly1305_Decrypt(key2, iv2, aad2, sizeof(aad2), cipher2,
  5727. sizeof(cipher2), NULL, generatedPlaintext);
  5728. if (err != BAD_FUNC_ARG)
  5729. return -4909;
  5730. err = wc_ChaCha20Poly1305_Decrypt(key2, iv2, aad2, sizeof(aad2), cipher2,
  5731. sizeof(cipher2), authTag2, NULL);
  5732. if (err != BAD_FUNC_ARG)
  5733. return -4910;
  5734. err = wc_ChaCha20Poly1305_Decrypt(key2, iv2, aad2, sizeof(aad2), NULL,
  5735. sizeof(cipher2), authTag2, generatedPlaintext);
  5736. if (err != BAD_FUNC_ARG)
  5737. return -4911;
  5738. /* Test #1 */
  5739. err = wc_ChaCha20Poly1305_Encrypt(key1, iv1,
  5740. aad1, sizeof(aad1),
  5741. plaintext1, sizeof(plaintext1),
  5742. generatedCiphertext, generatedAuthTag);
  5743. if (err) {
  5744. return err;
  5745. }
  5746. /* -- Check the ciphertext and authtag */
  5747. if (XMEMCMP(generatedCiphertext, cipher1, sizeof(cipher1))) {
  5748. return -4912;
  5749. }
  5750. if (XMEMCMP(generatedAuthTag, authTag1, sizeof(authTag1))) {
  5751. return -4913;
  5752. }
  5753. /* -- Verify decryption works */
  5754. err = wc_ChaCha20Poly1305_Decrypt(key1, iv1,
  5755. aad1, sizeof(aad1),
  5756. cipher1, sizeof(cipher1),
  5757. authTag1, generatedPlaintext);
  5758. if (err) {
  5759. return err;
  5760. }
  5761. if (XMEMCMP(generatedPlaintext, plaintext1, sizeof(plaintext1))) {
  5762. return -4914;
  5763. }
  5764. XMEMSET(generatedCiphertext, 0, sizeof(generatedCiphertext));
  5765. XMEMSET(generatedAuthTag, 0, sizeof(generatedAuthTag));
  5766. XMEMSET(generatedPlaintext, 0, sizeof(generatedPlaintext));
  5767. /* Test #2 */
  5768. err = wc_ChaCha20Poly1305_Encrypt(key2, iv2,
  5769. aad2, sizeof(aad2),
  5770. plaintext2, sizeof(plaintext2),
  5771. generatedCiphertext, generatedAuthTag);
  5772. if (err) {
  5773. return err;
  5774. }
  5775. /* -- Check the ciphertext and authtag */
  5776. if (XMEMCMP(generatedCiphertext, cipher2, sizeof(cipher2))) {
  5777. return -4915;
  5778. }
  5779. if (XMEMCMP(generatedAuthTag, authTag2, sizeof(authTag2))) {
  5780. return -4916;
  5781. }
  5782. /* -- Verify decryption works */
  5783. err = wc_ChaCha20Poly1305_Decrypt(key2, iv2,
  5784. aad2, sizeof(aad2),
  5785. cipher2, sizeof(cipher2),
  5786. authTag2, generatedPlaintext);
  5787. if (err) {
  5788. return err;
  5789. }
  5790. if (XMEMCMP(generatedPlaintext, plaintext2, sizeof(plaintext2))) {
  5791. return -4917;
  5792. }
  5793. /* AEAD init/update/final - bad argument tests */
  5794. err = wc_ChaCha20Poly1305_Init(NULL, key1, iv1,
  5795. CHACHA20_POLY1305_AEAD_DECRYPT);
  5796. if (err != BAD_FUNC_ARG)
  5797. return -4918;
  5798. err = wc_ChaCha20Poly1305_Init(&aead, NULL, iv1,
  5799. CHACHA20_POLY1305_AEAD_DECRYPT);
  5800. if (err != BAD_FUNC_ARG)
  5801. return -4919;
  5802. err = wc_ChaCha20Poly1305_Init(&aead, key1, NULL,
  5803. CHACHA20_POLY1305_AEAD_DECRYPT);
  5804. if (err != BAD_FUNC_ARG)
  5805. return -4920;
  5806. err = wc_ChaCha20Poly1305_UpdateAad(NULL, aad1, sizeof(aad1));
  5807. if (err != BAD_FUNC_ARG)
  5808. return -4921;
  5809. err = wc_ChaCha20Poly1305_UpdateAad(&aead, NULL, sizeof(aad1));
  5810. if (err != BAD_FUNC_ARG)
  5811. return -4922;
  5812. err = wc_ChaCha20Poly1305_UpdateData(NULL, generatedPlaintext,
  5813. generatedPlaintext, sizeof(plaintext1));
  5814. if (err != BAD_FUNC_ARG)
  5815. return -4923;
  5816. err = wc_ChaCha20Poly1305_UpdateData(&aead, generatedPlaintext, NULL,
  5817. sizeof(plaintext1));
  5818. if (err != BAD_FUNC_ARG)
  5819. return -4924;
  5820. err = wc_ChaCha20Poly1305_UpdateData(&aead, NULL, generatedPlaintext,
  5821. sizeof(plaintext1));
  5822. if (err != BAD_FUNC_ARG)
  5823. return -4925;
  5824. err = wc_ChaCha20Poly1305_Final(NULL, generatedAuthTag);
  5825. if (err != BAD_FUNC_ARG)
  5826. return -4926;
  5827. err = wc_ChaCha20Poly1305_Final(&aead, NULL);
  5828. if (err != BAD_FUNC_ARG)
  5829. return -4927;
  5830. /* AEAD init/update/final - bad state tests */
  5831. /* clear struct - make valgrind happy to resolve
  5832. "Conditional jump or move depends on uninitialised value(s)".
  5833. The enum is "int" size and aead.state is "byte" */
  5834. /* The wc_ChaCha20Poly1305_Init function does this normally */
  5835. XMEMSET(&aead, 0, sizeof(aead));
  5836. aead.state = CHACHA20_POLY1305_STATE_INIT;
  5837. err = wc_ChaCha20Poly1305_UpdateAad(&aead, aad1, sizeof(aad1));
  5838. if (err != BAD_STATE_E)
  5839. return -4928;
  5840. aead.state = CHACHA20_POLY1305_STATE_DATA;
  5841. err = wc_ChaCha20Poly1305_UpdateAad(&aead, aad1, sizeof(aad1));
  5842. if (err != BAD_STATE_E)
  5843. return -4929;
  5844. aead.state = CHACHA20_POLY1305_STATE_INIT;
  5845. err = wc_ChaCha20Poly1305_UpdateData(&aead, generatedPlaintext,
  5846. generatedPlaintext, sizeof(plaintext1));
  5847. if (err != BAD_STATE_E)
  5848. return -4930;
  5849. aead.state = CHACHA20_POLY1305_STATE_INIT;
  5850. err = wc_ChaCha20Poly1305_Final(&aead, generatedAuthTag);
  5851. if (err != BAD_STATE_E)
  5852. return -4931;
  5853. aead.state = CHACHA20_POLY1305_STATE_READY;
  5854. err = wc_ChaCha20Poly1305_Final(&aead, generatedAuthTag);
  5855. if (err != BAD_STATE_E)
  5856. return -4932;
  5857. XMEMSET(generatedCiphertext, 0, sizeof(generatedCiphertext));
  5858. XMEMSET(generatedAuthTag, 0, sizeof(generatedAuthTag));
  5859. XMEMSET(generatedPlaintext, 0, sizeof(generatedPlaintext));
  5860. /* Test 1 - Encrypt */
  5861. err = wc_ChaCha20Poly1305_Init(&aead, key1, iv1,
  5862. CHACHA20_POLY1305_AEAD_ENCRYPT);
  5863. if (err != 0)
  5864. return -4933;
  5865. err = wc_ChaCha20Poly1305_UpdateAad(&aead, aad1, sizeof(aad1));
  5866. if (err != 0)
  5867. return -4934;
  5868. #ifdef TEST_SMALL_CHACHA_CHUNKS
  5869. /* test doing data in smaller chunks */
  5870. for (testLen=0; testLen<sizeof(plaintext1); ) {
  5871. word32 dataLen = sizeof(plaintext1) - testLen;
  5872. if (dataLen > TEST_SMALL_CHACHA_CHUNKS)
  5873. dataLen = TEST_SMALL_CHACHA_CHUNKS;
  5874. err = wc_ChaCha20Poly1305_UpdateData(&aead, &plaintext1[testLen],
  5875. &generatedCiphertext[testLen], dataLen);
  5876. if (err != 0)
  5877. return -4935;
  5878. testLen += dataLen;
  5879. }
  5880. #else
  5881. err = wc_ChaCha20Poly1305_UpdateData(&aead, plaintext1,
  5882. generatedCiphertext, sizeof(plaintext1));
  5883. #endif
  5884. err = wc_ChaCha20Poly1305_Final(&aead, generatedAuthTag);
  5885. if (err != 0)
  5886. return -4936;
  5887. err = wc_ChaCha20Poly1305_CheckTag(generatedAuthTag, authTag1);
  5888. if (err != 0)
  5889. return -4937;
  5890. if (XMEMCMP(generatedCiphertext, cipher1, sizeof(cipher1))) {
  5891. return -4938;
  5892. }
  5893. /* Test 1 - Decrypt */
  5894. err = wc_ChaCha20Poly1305_Init(&aead, key1, iv1,
  5895. CHACHA20_POLY1305_AEAD_DECRYPT);
  5896. if (err != 0)
  5897. return -4939;
  5898. err = wc_ChaCha20Poly1305_UpdateAad(&aead, aad1, sizeof(aad1));
  5899. if (err != 0)
  5900. return -4940;
  5901. #ifdef TEST_SMALL_CHACHA_CHUNKS
  5902. /* test doing data in smaller chunks */
  5903. for (testLen=0; testLen<sizeof(plaintext1); ) {
  5904. word32 dataLen = sizeof(plaintext1) - testLen;
  5905. if (dataLen > TEST_SMALL_CHACHA_CHUNKS)
  5906. dataLen = TEST_SMALL_CHACHA_CHUNKS;
  5907. err = wc_ChaCha20Poly1305_UpdateData(&aead,
  5908. &generatedCiphertext[testLen], &generatedPlaintext[testLen],
  5909. dataLen);
  5910. if (err != 0)
  5911. return -4941;
  5912. testLen += dataLen;
  5913. }
  5914. #else
  5915. err = wc_ChaCha20Poly1305_UpdateData(&aead, generatedCiphertext,
  5916. generatedPlaintext, sizeof(cipher1));
  5917. #endif
  5918. err = wc_ChaCha20Poly1305_Final(&aead, generatedAuthTag);
  5919. if (err != 0)
  5920. return -4942;
  5921. err = wc_ChaCha20Poly1305_CheckTag(generatedAuthTag, authTag1);
  5922. if (err != 0)
  5923. return -4943;
  5924. if (XMEMCMP(generatedPlaintext, plaintext1, sizeof(plaintext1))) {
  5925. return -4944;
  5926. }
  5927. XMEMSET(generatedCiphertext, 0, sizeof(generatedCiphertext));
  5928. XMEMSET(generatedAuthTag, 0, sizeof(generatedAuthTag));
  5929. XMEMSET(generatedPlaintext, 0, sizeof(generatedPlaintext));
  5930. /* Test 2 - Encrypt */
  5931. err = wc_ChaCha20Poly1305_Init(&aead, key2, iv2,
  5932. CHACHA20_POLY1305_AEAD_ENCRYPT);
  5933. if (err != 0)
  5934. return -4945;
  5935. err = wc_ChaCha20Poly1305_UpdateAad(&aead, aad2, sizeof(aad2));
  5936. if (err != 0)
  5937. return -4946;
  5938. #ifdef TEST_SMALL_CHACHA_CHUNKS
  5939. /* test doing data in smaller chunks */
  5940. for (testLen=0; testLen<sizeof(plaintext2); ) {
  5941. word32 dataLen = sizeof(plaintext2) - testLen;
  5942. if (dataLen > TEST_SMALL_CHACHA_CHUNKS)
  5943. dataLen = TEST_SMALL_CHACHA_CHUNKS;
  5944. err = wc_ChaCha20Poly1305_UpdateData(&aead, &plaintext2[testLen],
  5945. &generatedCiphertext[testLen], dataLen);
  5946. if (err != 0)
  5947. return -4947;
  5948. testLen += dataLen;
  5949. }
  5950. #else
  5951. err = wc_ChaCha20Poly1305_UpdateData(&aead, plaintext2, generatedCiphertext,
  5952. sizeof(plaintext2));
  5953. #endif
  5954. err = wc_ChaCha20Poly1305_Final(&aead, generatedAuthTag);
  5955. if (err != 0)
  5956. return -4948;
  5957. err = wc_ChaCha20Poly1305_CheckTag(generatedAuthTag, authTag2);
  5958. if (err != 0)
  5959. return -4949;
  5960. if (XMEMCMP(generatedCiphertext, cipher2, sizeof(cipher2))) {
  5961. return -4950;
  5962. }
  5963. /* Test 2 - Decrypt */
  5964. err = wc_ChaCha20Poly1305_Init(&aead, key2, iv2,
  5965. CHACHA20_POLY1305_AEAD_DECRYPT);
  5966. if (err != 0)
  5967. return -4951;
  5968. err = wc_ChaCha20Poly1305_UpdateAad(&aead, aad2, sizeof(aad2));
  5969. if (err != 0)
  5970. return -4952;
  5971. #ifdef TEST_SMALL_CHACHA_CHUNKS
  5972. /* test doing data in smaller chunks */
  5973. for (testLen=0; testLen<sizeof(plaintext2); ) {
  5974. word32 dataLen = sizeof(plaintext2) - testLen;
  5975. if (dataLen > TEST_SMALL_CHACHA_CHUNKS)
  5976. dataLen = TEST_SMALL_CHACHA_CHUNKS;
  5977. err = wc_ChaCha20Poly1305_UpdateData(&aead,
  5978. &generatedCiphertext[testLen], &generatedPlaintext[testLen],
  5979. dataLen);
  5980. if (err != 0)
  5981. return -4953;
  5982. testLen += dataLen;
  5983. }
  5984. #else
  5985. err = wc_ChaCha20Poly1305_UpdateData(&aead, generatedCiphertext,
  5986. generatedPlaintext, sizeof(cipher2));
  5987. #endif
  5988. err = wc_ChaCha20Poly1305_Final(&aead, generatedAuthTag);
  5989. if (err != 0)
  5990. return -4954;
  5991. err = wc_ChaCha20Poly1305_CheckTag(generatedAuthTag, authTag2);
  5992. if (err != 0)
  5993. return -4955;
  5994. if (XMEMCMP(generatedPlaintext, plaintext2, sizeof(plaintext2))) {
  5995. return -4956;
  5996. }
  5997. return err;
  5998. }
  5999. #endif /* HAVE_CHACHA && HAVE_POLY1305 */
  6000. #ifndef NO_DES3
  6001. WOLFSSL_TEST_SUBROUTINE int des_test(void)
  6002. {
  6003. WOLFSSL_SMALL_STACK_STATIC const byte vector[] = { /* "now is the time for all " w/o trailing 0 */
  6004. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  6005. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  6006. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  6007. };
  6008. byte plain[24];
  6009. byte cipher[24];
  6010. Des enc;
  6011. Des dec;
  6012. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  6013. {
  6014. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
  6015. };
  6016. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  6017. {
  6018. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef
  6019. };
  6020. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  6021. {
  6022. 0x8b,0x7c,0x52,0xb0,0x01,0x2b,0x6c,0xb8,
  6023. 0x4f,0x0f,0xeb,0xf3,0xfb,0x5f,0x86,0x73,
  6024. 0x15,0x85,0xb3,0x22,0x4b,0x86,0x2b,0x4b
  6025. };
  6026. int ret;
  6027. ret = wc_Des_SetKey(&enc, key, iv, DES_ENCRYPTION);
  6028. if (ret != 0)
  6029. return -5000;
  6030. ret = wc_Des_CbcEncrypt(&enc, cipher, vector, sizeof(vector));
  6031. if (ret != 0)
  6032. return -5001;
  6033. ret = wc_Des_SetKey(&dec, key, iv, DES_DECRYPTION);
  6034. if (ret != 0)
  6035. return -5002;
  6036. ret = wc_Des_CbcDecrypt(&dec, plain, cipher, sizeof(cipher));
  6037. if (ret != 0)
  6038. return -5003;
  6039. if (XMEMCMP(plain, vector, sizeof(plain)))
  6040. return -5004;
  6041. if (XMEMCMP(cipher, verify, sizeof(cipher)))
  6042. return -5005;
  6043. ret = wc_Des_CbcEncryptWithKey(cipher, vector, sizeof(vector), key, iv);
  6044. if (ret != 0)
  6045. return -5006;
  6046. #if defined(WOLFSSL_ENCRYPTED_KEYS) && !defined(NO_SHA)
  6047. {
  6048. EncryptedInfo info;
  6049. XMEMSET(&info, 0, sizeof(EncryptedInfo));
  6050. XMEMCPY(info.iv, iv, sizeof(iv));
  6051. info.ivSz = sizeof(iv);
  6052. info.keySz = sizeof(key);
  6053. info.cipherType = WC_CIPHER_DES;
  6054. ret = wc_BufferKeyEncrypt(&info, cipher, sizeof(cipher), key,
  6055. sizeof(key), WC_HASH_TYPE_SHA);
  6056. if (ret != 0)
  6057. return -5007;
  6058. /* Test invalid info ptr */
  6059. ret = wc_BufferKeyEncrypt(NULL, cipher, sizeof(cipher), key,
  6060. sizeof(key), WC_HASH_TYPE_SHA);
  6061. if (ret != BAD_FUNC_ARG)
  6062. return -5008;
  6063. #ifndef NO_PWDBASED
  6064. /* Test invalid hash type - only applies to wc_PBKDF1 call */
  6065. ret = wc_BufferKeyEncrypt(&info, cipher, sizeof(cipher), key,
  6066. sizeof(key), WC_HASH_TYPE_NONE);
  6067. if (ret == 0)
  6068. return -5009;
  6069. #endif /* !NO_PWDBASED */
  6070. }
  6071. #endif
  6072. return 0;
  6073. }
  6074. #endif /* !NO_DES3 */
  6075. #ifndef NO_DES3
  6076. WOLFSSL_TEST_SUBROUTINE int des3_test(void)
  6077. {
  6078. WOLFSSL_SMALL_STACK_STATIC const byte vector[] = { /* "Now is the time for all " w/o trailing 0 */
  6079. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  6080. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  6081. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  6082. };
  6083. byte plain[24];
  6084. byte cipher[24];
  6085. Des3 enc;
  6086. Des3 dec;
  6087. WOLFSSL_SMALL_STACK_STATIC const byte key3[] =
  6088. {
  6089. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  6090. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  6091. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  6092. };
  6093. WOLFSSL_SMALL_STACK_STATIC const byte iv3[] =
  6094. {
  6095. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  6096. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  6097. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  6098. };
  6099. WOLFSSL_SMALL_STACK_STATIC const byte verify3[] =
  6100. {
  6101. 0x43,0xa0,0x29,0x7e,0xd1,0x84,0xf8,0x0e,
  6102. 0x89,0x64,0x84,0x32,0x12,0xd5,0x08,0x98,
  6103. 0x18,0x94,0x15,0x74,0x87,0x12,0x7d,0xb0
  6104. };
  6105. int ret;
  6106. if (wc_Des3Init(&enc, HEAP_HINT, devId) != 0)
  6107. return -5100;
  6108. if (wc_Des3Init(&dec, HEAP_HINT, devId) != 0)
  6109. return -5101;
  6110. ret = wc_Des3_SetKey(&enc, key3, iv3, DES_ENCRYPTION);
  6111. if (ret != 0)
  6112. return -5102;
  6113. ret = wc_Des3_SetKey(&dec, key3, iv3, DES_DECRYPTION);
  6114. if (ret != 0)
  6115. return -5103;
  6116. ret = wc_Des3_CbcEncrypt(&enc, cipher, vector, sizeof(vector));
  6117. #if defined(WOLFSSL_ASYNC_CRYPT)
  6118. ret = wc_AsyncWait(ret, &enc.asyncDev, WC_ASYNC_FLAG_NONE);
  6119. #endif
  6120. if (ret != 0)
  6121. return -5104;
  6122. ret = wc_Des3_CbcDecrypt(&dec, plain, cipher, sizeof(cipher));
  6123. #if defined(WOLFSSL_ASYNC_CRYPT)
  6124. ret = wc_AsyncWait(ret, &dec.asyncDev, WC_ASYNC_FLAG_NONE);
  6125. #endif
  6126. if (ret != 0)
  6127. return -5105;
  6128. if (XMEMCMP(plain, vector, sizeof(plain)))
  6129. return -5106;
  6130. if (XMEMCMP(cipher, verify3, sizeof(cipher)))
  6131. return -5107;
  6132. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  6133. /* test the same vectors with using compatibility layer */
  6134. {
  6135. DES_key_schedule ks1;
  6136. DES_key_schedule ks2;
  6137. DES_key_schedule ks3;
  6138. DES_cblock iv4;
  6139. XMEMCPY(ks1, key3, sizeof(DES_key_schedule));
  6140. XMEMCPY(ks2, key3 + 8, sizeof(DES_key_schedule));
  6141. XMEMCPY(ks3, key3 + 16, sizeof(DES_key_schedule));
  6142. XMEMCPY(iv4, iv3, sizeof(DES_cblock));
  6143. XMEMSET(plain, 0, sizeof(plain));
  6144. XMEMSET(cipher, 0, sizeof(cipher));
  6145. DES_ede3_cbc_encrypt(vector, cipher, sizeof(vector), &ks1, &ks2, &ks3,
  6146. &iv4, DES_ENCRYPT);
  6147. DES_ede3_cbc_encrypt(cipher, plain, sizeof(cipher), &ks1, &ks2, &ks3,
  6148. &iv4, DES_DECRYPT);
  6149. if (XMEMCMP(plain, vector, sizeof(plain)))
  6150. return -5108;
  6151. if (XMEMCMP(cipher, verify3, sizeof(cipher)))
  6152. return -5109;
  6153. }
  6154. #endif /* OPENSSL_EXTRA */
  6155. wc_Des3Free(&enc);
  6156. wc_Des3Free(&dec);
  6157. #if defined(WOLFSSL_ENCRYPTED_KEYS) && !defined(NO_SHA)
  6158. {
  6159. EncryptedInfo info;
  6160. XMEMSET(&info, 0, sizeof(EncryptedInfo));
  6161. XMEMCPY(info.iv, iv3, sizeof(iv3));
  6162. info.ivSz = sizeof(iv3);
  6163. info.keySz = sizeof(key3);
  6164. info.cipherType = WC_CIPHER_DES3;
  6165. ret = wc_BufferKeyEncrypt(&info, cipher, sizeof(cipher), key3,
  6166. sizeof(key3), WC_HASH_TYPE_SHA);
  6167. if (ret != 0)
  6168. return -5110;
  6169. }
  6170. #endif
  6171. return 0;
  6172. }
  6173. #endif /* NO_DES3 */
  6174. #ifndef NO_AES
  6175. #if defined(WOLFSSL_AES_OFB) || defined(WOLFSSL_AES_CFB) || \
  6176. defined(WOLFSSL_AES_XTS)
  6177. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  6178. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  6179. /* pass in the function, key, iv, plain text and expected and this function
  6180. * tests that the encryption and decryption is successful */
  6181. static int EVP_test(const WOLFSSL_EVP_CIPHER* type, const byte* key,
  6182. const byte* iv, const byte* plain, int plainSz,
  6183. const byte* expected, int expectedSz)
  6184. {
  6185. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  6186. EVP_CIPHER_CTX *ctx = NULL;
  6187. #else
  6188. EVP_CIPHER_CTX ctx[1];
  6189. #endif
  6190. int idx, ret = 0, cipherSz;
  6191. byte* cipher;
  6192. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  6193. if ((ctx = wolfSSL_EVP_CIPHER_CTX_new()) == NULL)
  6194. return MEMORY_E;
  6195. #endif
  6196. cipher = (byte*)XMALLOC(plainSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  6197. if (cipher == NULL) {
  6198. ret = -5120;
  6199. goto EVP_TEST_END;
  6200. }
  6201. /* test encrypt */
  6202. EVP_CIPHER_CTX_init(ctx);
  6203. if (EVP_CipherInit(ctx, type, key, iv, 1) == 0) {
  6204. ret = -5121;
  6205. goto EVP_TEST_END;
  6206. }
  6207. if (EVP_CipherUpdate(ctx, cipher, &idx, plain, expectedSz) == 0) {
  6208. ret = -5122;
  6209. goto EVP_TEST_END;
  6210. }
  6211. cipherSz = idx;
  6212. if (EVP_CipherFinal(ctx, cipher + cipherSz, &idx) == 0) {
  6213. ret = -5123;
  6214. goto EVP_TEST_END;
  6215. }
  6216. cipherSz += idx;
  6217. if (XMEMCMP(cipher, expected, plainSz)) {
  6218. ret = -5124;
  6219. goto EVP_TEST_END;
  6220. }
  6221. /* test decrypt */
  6222. EVP_CIPHER_CTX_init(ctx);
  6223. if (EVP_CipherInit(ctx, type, key, iv, 0) == 0) {
  6224. ret = -5125;
  6225. goto EVP_TEST_END;
  6226. }
  6227. if (EVP_CipherUpdate(ctx, cipher, &idx, cipher, expectedSz) == 0) {
  6228. ret = -5126;
  6229. goto EVP_TEST_END;
  6230. }
  6231. cipherSz = idx;
  6232. if (EVP_CipherFinal(ctx, cipher + cipherSz, &idx) == 0) {
  6233. ret = -5127;
  6234. goto EVP_TEST_END;
  6235. }
  6236. cipherSz += idx;
  6237. if ((expectedSz != cipherSz) || XMEMCMP(plain, cipher, plainSz)) {
  6238. ret = -5128;
  6239. goto EVP_TEST_END;
  6240. }
  6241. EVP_TEST_END:
  6242. if (cipher)
  6243. XFREE(cipher, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  6244. (void)cipherSz;
  6245. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  6246. wolfSSL_EVP_CIPHER_CTX_free(ctx);
  6247. #endif
  6248. return ret;
  6249. }
  6250. #endif /* OPENSSL_EXTRA */
  6251. #endif /* WOLFSSL_AES_OFB || WOLFSSL_AES_CFB */
  6252. #ifdef WOLFSSL_AES_OFB
  6253. /* test vector from https://csrc.nist.gov/Projects/cryptographic-algorithm-validation-program/Block-Ciphers */
  6254. WOLFSSL_TEST_SUBROUTINE int aesofb_test(void)
  6255. {
  6256. #ifdef WOLFSSL_AES_256
  6257. WOLFSSL_SMALL_STACK_STATIC const byte key1[] =
  6258. {
  6259. 0xc4,0xc7,0xfa,0xd6,0x53,0x5c,0xb8,0x71,
  6260. 0x4a,0x5c,0x40,0x77,0x9a,0x8b,0xa1,0xd2,
  6261. 0x53,0x3e,0x23,0xb4,0xb2,0x58,0x73,0x2a,
  6262. 0x5b,0x78,0x01,0xf4,0xe3,0x71,0xa7,0x94
  6263. };
  6264. WOLFSSL_SMALL_STACK_STATIC const byte iv1[] =
  6265. {
  6266. 0x5e,0xb9,0x33,0x13,0xb8,0x71,0xff,0x16,
  6267. 0xb9,0x8a,0x9b,0xcb,0x43,0x33,0x0d,0x6f
  6268. };
  6269. WOLFSSL_SMALL_STACK_STATIC const byte plain1[] =
  6270. {
  6271. 0x6d,0x0b,0xb0,0x79,0x63,0x84,0x71,0xe9,
  6272. 0x39,0xd4,0x53,0x14,0x86,0xc1,0x4c,0x25,
  6273. 0x9a,0xee,0xc6,0xf3,0xc0,0x0d,0xfd,0xd6,
  6274. 0xc0,0x50,0xa8,0xba,0xa8,0x20,0xdb,0x71,
  6275. 0xcc,0x12,0x2c,0x4e,0x0c,0x17,0x15,0xef,
  6276. 0x55,0xf3,0x99,0x5a,0x6b,0xf0,0x2a,0x4c
  6277. };
  6278. WOLFSSL_SMALL_STACK_STATIC const byte cipher1[] =
  6279. {
  6280. 0x0f,0x54,0x61,0x71,0x59,0xd0,0x3f,0xfc,
  6281. 0x1b,0xfa,0xfb,0x60,0x29,0x30,0xd7,0x00,
  6282. 0xf4,0xa4,0xa8,0xe6,0xdd,0x93,0x94,0x46,
  6283. 0x64,0xd2,0x19,0xc4,0xc5,0x4d,0xde,0x1b,
  6284. 0x04,0x53,0xe1,0x73,0xf5,0x18,0x74,0xae,
  6285. 0xfd,0x64,0xa2,0xe1,0xe2,0x76,0x13,0xb0
  6286. };
  6287. #endif /* WOLFSSL_AES_256 */
  6288. #ifdef WOLFSSL_AES_128
  6289. WOLFSSL_SMALL_STACK_STATIC const byte key2[] =
  6290. {
  6291. 0x10,0xa5,0x88,0x69,0xd7,0x4b,0xe5,0xa3,
  6292. 0x74,0xcf,0x86,0x7c,0xfb,0x47,0x38,0x59
  6293. };
  6294. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] =
  6295. {
  6296. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6297. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  6298. };
  6299. WOLFSSL_SMALL_STACK_STATIC const byte plain2[] =
  6300. {
  6301. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6302. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  6303. };
  6304. WOLFSSL_SMALL_STACK_STATIC const byte cipher2[] =
  6305. {
  6306. 0x6d,0x25,0x1e,0x69,0x44,0xb0,0x51,0xe0,
  6307. 0x4e,0xaa,0x6f,0xb4,0xdb,0xf7,0x84,0x65
  6308. };
  6309. #endif /* WOLFSSL_AES_128 */
  6310. #ifdef WOLFSSL_AES_192
  6311. WOLFSSL_SMALL_STACK_STATIC const byte key3[] = {
  6312. 0xd0,0x77,0xa0,0x3b,0xd8,0xa3,0x89,0x73,
  6313. 0x92,0x8c,0xca,0xfe,0x4a,0x9d,0x2f,0x45,
  6314. 0x51,0x30,0xbd,0x0a,0xf5,0xae,0x46,0xa9
  6315. };
  6316. WOLFSSL_SMALL_STACK_STATIC const byte iv3[] =
  6317. {
  6318. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6319. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  6320. };
  6321. WOLFSSL_SMALL_STACK_STATIC const byte cipher3[] =
  6322. {
  6323. 0xab,0xc7,0x86,0xfb,0x1e,0xdb,0x50,0x45,
  6324. 0x80,0xc4,0xd8,0x82,0xef,0x29,0xa0,0xc7
  6325. };
  6326. WOLFSSL_SMALL_STACK_STATIC const byte plain3[] =
  6327. {
  6328. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6329. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  6330. };
  6331. #endif /* WOLFSSL_AES_192 */
  6332. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  6333. Aes *enc = NULL;
  6334. #else
  6335. Aes enc[1];
  6336. #endif
  6337. byte cipher[AES_BLOCK_SIZE * 4];
  6338. #ifdef HAVE_AES_DECRYPT
  6339. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  6340. Aes *dec = NULL;
  6341. #else
  6342. Aes dec[1];
  6343. #endif
  6344. byte plain [AES_BLOCK_SIZE * 4];
  6345. #endif
  6346. int ret = 0;
  6347. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  6348. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  6349. ERROR_OUT(-1, out);
  6350. #ifdef HAVE_AES_DECRYPT
  6351. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  6352. ERROR_OUT(-1, out);
  6353. #endif
  6354. #endif
  6355. XMEMSET(enc, 0, sizeof *enc);
  6356. #ifdef HAVE_AES_DECRYPT
  6357. XMEMSET(dec, 0, sizeof *dec);
  6358. #endif
  6359. #ifdef WOLFSSL_AES_128
  6360. /* 128 key size test */
  6361. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  6362. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  6363. ret = EVP_test(EVP_aes_128_ofb(), key2, iv2, plain2, sizeof(plain2),
  6364. cipher2, sizeof(cipher2));
  6365. if (ret != 0) {
  6366. goto out;
  6367. }
  6368. #endif
  6369. ret = wc_AesSetKey(enc, key2, sizeof(key2), iv2, AES_ENCRYPTION);
  6370. if (ret != 0)
  6371. ERROR_OUT(-5129, out);
  6372. #ifdef HAVE_AES_DECRYPT
  6373. /* decrypt uses AES_ENCRYPTION */
  6374. ret = wc_AesSetKey(dec, key2, sizeof(key2), iv2, AES_ENCRYPTION);
  6375. if (ret != 0)
  6376. ERROR_OUT(-5130, out);
  6377. #endif
  6378. XMEMSET(cipher, 0, sizeof(cipher));
  6379. ret = wc_AesOfbEncrypt(enc, cipher, plain2, AES_BLOCK_SIZE);
  6380. if (ret != 0)
  6381. ERROR_OUT(-5131, out);
  6382. if (XMEMCMP(cipher, cipher2, AES_BLOCK_SIZE))
  6383. ERROR_OUT(-5132, out);
  6384. #ifdef HAVE_AES_DECRYPT
  6385. ret = wc_AesOfbDecrypt(dec, plain, cipher2, AES_BLOCK_SIZE);
  6386. if (ret != 0)
  6387. ERROR_OUT(-5133, out);
  6388. if (XMEMCMP(plain, plain2, AES_BLOCK_SIZE))
  6389. ERROR_OUT(-5134, out);
  6390. #endif /* HAVE_AES_DECRYPT */
  6391. #endif /* WOLFSSL_AES_128 */
  6392. #ifdef WOLFSSL_AES_192
  6393. /* 192 key size test */
  6394. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  6395. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  6396. ret = EVP_test(EVP_aes_192_ofb(), key3, iv3, plain3, sizeof(plain3),
  6397. cipher3, sizeof(cipher3));
  6398. if (ret != 0) {
  6399. goto out;
  6400. }
  6401. #endif
  6402. ret = wc_AesSetKey(enc, key3, sizeof(key3), iv3, AES_ENCRYPTION);
  6403. if (ret != 0)
  6404. ERROR_OUT(-5135, out);
  6405. #ifdef HAVE_AES_DECRYPT
  6406. /* decrypt uses AES_ENCRYPTION */
  6407. ret = wc_AesSetKey(dec, key3, sizeof(key3), iv3, AES_ENCRYPTION);
  6408. if (ret != 0)
  6409. ERROR_OUT(-5136, out);
  6410. #endif
  6411. XMEMSET(cipher, 0, sizeof(cipher));
  6412. ret = wc_AesOfbEncrypt(enc, cipher, plain3, AES_BLOCK_SIZE);
  6413. if (ret != 0)
  6414. ERROR_OUT(-5137, out);
  6415. if (XMEMCMP(cipher, cipher3, AES_BLOCK_SIZE))
  6416. ERROR_OUT(-5138, out);
  6417. #ifdef HAVE_AES_DECRYPT
  6418. ret = wc_AesOfbDecrypt(dec, plain, cipher3, AES_BLOCK_SIZE);
  6419. if (ret != 0)
  6420. ERROR_OUT(-5139, out);
  6421. if (XMEMCMP(plain, plain3, AES_BLOCK_SIZE))
  6422. ERROR_OUT(-5140, out);
  6423. #endif /* HAVE_AES_DECRYPT */
  6424. #endif /* WOLFSSL_AES_192 */
  6425. #ifdef WOLFSSL_AES_256
  6426. /* 256 key size test */
  6427. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  6428. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  6429. ret = EVP_test(EVP_aes_256_ofb(), key1, iv1, plain1, sizeof(plain1),
  6430. cipher1, sizeof(cipher1));
  6431. if (ret != 0) {
  6432. goto out;
  6433. }
  6434. #endif
  6435. ret = wc_AesSetKey(enc, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  6436. if (ret != 0)
  6437. ERROR_OUT(-5141, out);
  6438. #ifdef HAVE_AES_DECRYPT
  6439. /* decrypt uses AES_ENCRYPTION */
  6440. ret = wc_AesSetKey(dec, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  6441. if (ret != 0)
  6442. ERROR_OUT(-5142, out);
  6443. #endif
  6444. XMEMSET(cipher, 0, sizeof(cipher));
  6445. ret = wc_AesOfbEncrypt(enc, cipher, plain1, AES_BLOCK_SIZE);
  6446. if (ret != 0)
  6447. ERROR_OUT(-5143, out);
  6448. if (XMEMCMP(cipher, cipher1, AES_BLOCK_SIZE))
  6449. ERROR_OUT(-5144, out);
  6450. ret = wc_AesOfbEncrypt(enc, cipher + AES_BLOCK_SIZE,
  6451. plain1 + AES_BLOCK_SIZE, AES_BLOCK_SIZE);
  6452. if (ret != 0)
  6453. ERROR_OUT(-5145, out);
  6454. if (XMEMCMP(cipher + AES_BLOCK_SIZE, cipher1 + AES_BLOCK_SIZE,
  6455. AES_BLOCK_SIZE))
  6456. ERROR_OUT(-5146, out);
  6457. #ifdef HAVE_AES_DECRYPT
  6458. ret = wc_AesOfbDecrypt(dec, plain, cipher1, AES_BLOCK_SIZE);
  6459. if (ret != 0)
  6460. ERROR_OUT(-5147, out);
  6461. if (XMEMCMP(plain, plain1, AES_BLOCK_SIZE))
  6462. ERROR_OUT(-5148, out);
  6463. ret = wc_AesOfbDecrypt(dec, plain + AES_BLOCK_SIZE,
  6464. cipher1 + AES_BLOCK_SIZE, AES_BLOCK_SIZE);
  6465. if (ret != 0)
  6466. ERROR_OUT(-5149, out);
  6467. if (XMEMCMP(plain + AES_BLOCK_SIZE, plain1 + AES_BLOCK_SIZE,
  6468. AES_BLOCK_SIZE))
  6469. ERROR_OUT(-5150, out);
  6470. #endif /* HAVE_AES_DECRYPT */
  6471. /* multiple blocks at once */
  6472. ret = wc_AesSetKey(enc, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  6473. if (ret != 0)
  6474. ERROR_OUT(-5151, out);
  6475. #ifdef HAVE_AES_DECRYPT
  6476. /* decrypt uses AES_ENCRYPTION */
  6477. ret = wc_AesSetKey(dec, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  6478. if (ret != 0)
  6479. ERROR_OUT(-5152, out);
  6480. #endif
  6481. XMEMSET(cipher, 0, sizeof(cipher));
  6482. ret = wc_AesOfbEncrypt(enc, cipher, plain1, AES_BLOCK_SIZE * 3);
  6483. if (ret != 0)
  6484. ERROR_OUT(-5153, out);
  6485. if (XMEMCMP(cipher, cipher1, AES_BLOCK_SIZE * 3))
  6486. ERROR_OUT(-5154, out);
  6487. #ifdef HAVE_AES_DECRYPT
  6488. ret = wc_AesOfbDecrypt(dec, plain, cipher1, AES_BLOCK_SIZE * 3);
  6489. if (ret != 0)
  6490. ERROR_OUT(-5155, out);
  6491. if (XMEMCMP(plain, plain1, AES_BLOCK_SIZE * 3))
  6492. ERROR_OUT(-5156, out);
  6493. #endif /* HAVE_AES_DECRYPT */
  6494. /* inline decrypt/encrypt*/
  6495. ret = wc_AesSetKey(enc, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  6496. if (ret != 0)
  6497. ERROR_OUT(-5157, out);
  6498. #ifdef HAVE_AES_DECRYPT
  6499. /* decrypt uses AES_ENCRYPTION */
  6500. ret = wc_AesSetKey(dec, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  6501. if (ret != 0)
  6502. ERROR_OUT(-5158, out);
  6503. #endif
  6504. XMEMCPY(cipher, plain1, AES_BLOCK_SIZE * 2);
  6505. ret = wc_AesOfbEncrypt(enc, cipher, cipher, AES_BLOCK_SIZE * 2);
  6506. if (ret != 0)
  6507. ERROR_OUT(-5159, out);
  6508. if (XMEMCMP(cipher, cipher1, AES_BLOCK_SIZE * 2))
  6509. ERROR_OUT(-5160, out);
  6510. #ifdef HAVE_AES_DECRYPT
  6511. ret = wc_AesOfbDecrypt(dec, cipher, cipher, AES_BLOCK_SIZE * 2);
  6512. if (ret != 0)
  6513. ERROR_OUT(-5161, out);
  6514. if (XMEMCMP(cipher, plain1, AES_BLOCK_SIZE * 2))
  6515. ERROR_OUT(-5162, out);
  6516. #endif /* HAVE_AES_DECRYPT */
  6517. /* 256 key size test leftover support */
  6518. ret = wc_AesSetKey(enc, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  6519. if (ret != 0)
  6520. ERROR_OUT(-5163, out);
  6521. #ifdef HAVE_AES_DECRYPT
  6522. /* decrypt uses AES_ENCRYPTION */
  6523. ret = wc_AesSetKey(dec, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  6524. if (ret != 0)
  6525. ERROR_OUT(-5164, out);
  6526. #endif
  6527. XMEMSET(cipher, 0, sizeof(cipher));
  6528. ret = wc_AesOfbEncrypt(enc, cipher, plain1, 3);
  6529. if (ret != 0)
  6530. ERROR_OUT(-5165, out);
  6531. if (XMEMCMP(cipher, cipher1, 3))
  6532. ERROR_OUT(-5166, out);
  6533. ret = wc_AesOfbEncrypt(enc, cipher + 3, plain1 + 3, AES_BLOCK_SIZE);
  6534. if (ret != 0)
  6535. ERROR_OUT(-5167, out);
  6536. if (XMEMCMP(cipher + 3, cipher1 + 3, AES_BLOCK_SIZE))
  6537. ERROR_OUT(-5168, out);
  6538. #ifdef HAVE_AES_DECRYPT
  6539. ret = wc_AesOfbDecrypt(dec, plain, cipher1, 6);
  6540. if (ret != 0)
  6541. ERROR_OUT(-5169, out);
  6542. if (XMEMCMP(plain, plain1, 6))
  6543. ERROR_OUT(-5170, out);
  6544. ret = wc_AesOfbDecrypt(dec, plain + 6, cipher1 + 6, AES_BLOCK_SIZE);
  6545. if (ret != 0)
  6546. ERROR_OUT(-5171, out);
  6547. if (XMEMCMP(plain + 6, plain1 + 6, AES_BLOCK_SIZE))
  6548. ERROR_OUT(-5172, out);
  6549. #endif /* HAVE_AES_DECRYPT */
  6550. out:
  6551. wc_AesFree(enc);
  6552. wc_AesFree(dec);
  6553. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  6554. if (enc)
  6555. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  6556. #ifdef HAVE_AES_DECRYPT
  6557. if (dec)
  6558. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  6559. #endif
  6560. #endif
  6561. #endif /* WOLFSSL_AES_256 */
  6562. return ret;
  6563. }
  6564. #endif /* WOLFSSL_AES_OFB */
  6565. #if defined(WOLFSSL_AES_CFB)
  6566. /* Test cases from NIST SP 800-38A, Recommendation for Block Cipher Modes of Operation Methods an*/
  6567. static int aescfb_test(void)
  6568. {
  6569. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  6570. Aes *enc = NULL;
  6571. #else
  6572. Aes enc[1];
  6573. #endif
  6574. int enc_inited = 0;
  6575. byte cipher[AES_BLOCK_SIZE * 4];
  6576. #ifdef HAVE_AES_DECRYPT
  6577. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  6578. Aes *dec = NULL;
  6579. #else
  6580. Aes dec[1];
  6581. #endif
  6582. int dec_inited = 0;
  6583. byte plain [AES_BLOCK_SIZE * 4];
  6584. #endif
  6585. int ret = 0;
  6586. WOLFSSL_SMALL_STACK_STATIC const byte iv[] = {
  6587. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  6588. 0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f
  6589. };
  6590. #ifdef WOLFSSL_AES_128
  6591. WOLFSSL_SMALL_STACK_STATIC const byte key1[] =
  6592. {
  6593. 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,
  6594. 0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c
  6595. };
  6596. WOLFSSL_SMALL_STACK_STATIC const byte cipher1[] =
  6597. {
  6598. 0x3b,0x3f,0xd9,0x2e,0xb7,0x2d,0xad,0x20,
  6599. 0x33,0x34,0x49,0xf8,0xe8,0x3c,0xfb,0x4a,
  6600. 0xc8,0xa6,0x45,0x37,0xa0,0xb3,0xa9,0x3f,
  6601. 0xcd,0xe3,0xcd,0xad,0x9f,0x1c,0xe5,0x8b,
  6602. 0x26,0x75,0x1f,0x67,0xa3,0xcb,0xb1,0x40,
  6603. 0xb1,0x80,0x8c,0xf1,0x87,0xa4,0xf4,0xdf
  6604. };
  6605. WOLFSSL_SMALL_STACK_STATIC const byte msg1[] =
  6606. {
  6607. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  6608. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  6609. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  6610. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  6611. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  6612. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef
  6613. };
  6614. #endif /* WOLFSSL_AES_128 */
  6615. #ifdef WOLFSSL_AES_192
  6616. /* 192 size key test */
  6617. WOLFSSL_SMALL_STACK_STATIC const byte key2[] =
  6618. {
  6619. 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  6620. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  6621. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b
  6622. };
  6623. WOLFSSL_SMALL_STACK_STATIC const byte cipher2[] =
  6624. {
  6625. 0xcd,0xc8,0x0d,0x6f,0xdd,0xf1,0x8c,0xab,
  6626. 0x34,0xc2,0x59,0x09,0xc9,0x9a,0x41,0x74,
  6627. 0x67,0xce,0x7f,0x7f,0x81,0x17,0x36,0x21,
  6628. 0x96,0x1a,0x2b,0x70,0x17,0x1d,0x3d,0x7a,
  6629. 0x2e,0x1e,0x8a,0x1d,0xd5,0x9b,0x88,0xb1,
  6630. 0xc8,0xe6,0x0f,0xed,0x1e,0xfa,0xc4,0xc9,
  6631. 0xc0,0x5f,0x9f,0x9c,0xa9,0x83,0x4f,0xa0,
  6632. 0x42,0xae,0x8f,0xba,0x58,0x4b,0x09,0xff
  6633. };
  6634. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] =
  6635. {
  6636. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  6637. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  6638. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  6639. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  6640. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  6641. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  6642. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  6643. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  6644. };
  6645. #endif /* WOLFSSL_AES_192 */
  6646. #ifdef WOLFSSL_AES_256
  6647. /* 256 size key simple test */
  6648. WOLFSSL_SMALL_STACK_STATIC const byte key3[] =
  6649. {
  6650. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  6651. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  6652. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  6653. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  6654. };
  6655. WOLFSSL_SMALL_STACK_STATIC const byte cipher3[] =
  6656. {
  6657. 0xdc,0x7e,0x84,0xbf,0xda,0x79,0x16,0x4b,
  6658. 0x7e,0xcd,0x84,0x86,0x98,0x5d,0x38,0x60,
  6659. 0x39,0xff,0xed,0x14,0x3b,0x28,0xb1,0xc8,
  6660. 0x32,0x11,0x3c,0x63,0x31,0xe5,0x40,0x7b,
  6661. 0xdf,0x10,0x13,0x24,0x15,0xe5,0x4b,0x92,
  6662. 0xa1,0x3e,0xd0,0xa8,0x26,0x7a,0xe2,0xf9,
  6663. 0x75,0xa3,0x85,0x74,0x1a,0xb9,0xce,0xf8,
  6664. 0x20,0x31,0x62,0x3d,0x55,0xb1,0xe4,0x71
  6665. };
  6666. WOLFSSL_SMALL_STACK_STATIC const byte msg3[] =
  6667. {
  6668. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  6669. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  6670. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  6671. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  6672. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  6673. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  6674. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  6675. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  6676. };
  6677. #endif /* WOLFSSL_AES_256 */
  6678. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  6679. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  6680. ERROR_OUT(-1, out);
  6681. #ifdef HAVE_AES_DECRYPT
  6682. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  6683. ERROR_OUT(-1, out);
  6684. #endif
  6685. #endif
  6686. if (wc_AesInit(enc, HEAP_HINT, devId) != 0)
  6687. ERROR_OUT(-5173, out);
  6688. else
  6689. enc_inited = 1;
  6690. #ifdef HAVE_AES_DECRYPT
  6691. if (wc_AesInit(dec, HEAP_HINT, devId) != 0)
  6692. ERROR_OUT(-5174, out);
  6693. else
  6694. dec_inited = 1;
  6695. #endif
  6696. #ifdef WOLFSSL_AES_128
  6697. /* 128 key tests */
  6698. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  6699. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  6700. ret = EVP_test(EVP_aes_128_cfb128(), key1, iv, msg1, sizeof(msg1),
  6701. cipher1, sizeof(cipher1));
  6702. if (ret != 0) {
  6703. return ret;
  6704. }
  6705. #endif
  6706. ret = wc_AesSetKey(enc, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  6707. if (ret != 0)
  6708. ERROR_OUT(-5175, out);
  6709. #ifdef HAVE_AES_DECRYPT
  6710. /* decrypt uses AES_ENCRYPTION */
  6711. ret = wc_AesSetKey(dec, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  6712. if (ret != 0)
  6713. ERROR_OUT(-5176, out);
  6714. #endif
  6715. XMEMSET(cipher, 0, sizeof(cipher));
  6716. ret = wc_AesCfbEncrypt(enc, cipher, msg1, AES_BLOCK_SIZE * 2);
  6717. if (ret != 0)
  6718. ERROR_OUT(-5177, out);
  6719. if (XMEMCMP(cipher, cipher1, AES_BLOCK_SIZE * 2))
  6720. ERROR_OUT(-5178, out);
  6721. /* test restarting encryption process */
  6722. ret = wc_AesCfbEncrypt(enc, cipher + (AES_BLOCK_SIZE * 2),
  6723. msg1 + (AES_BLOCK_SIZE * 2), AES_BLOCK_SIZE);
  6724. if (ret != 0)
  6725. ERROR_OUT(-5179, out);
  6726. if (XMEMCMP(cipher + (AES_BLOCK_SIZE * 2),
  6727. cipher1 + (AES_BLOCK_SIZE * 2), AES_BLOCK_SIZE))
  6728. ERROR_OUT(-5180, out);
  6729. #ifdef HAVE_AES_DECRYPT
  6730. ret = wc_AesCfbDecrypt(dec, plain, cipher, AES_BLOCK_SIZE * 3);
  6731. if (ret != 0)
  6732. ERROR_OUT(-5181, out);
  6733. if (XMEMCMP(plain, msg1, AES_BLOCK_SIZE * 3))
  6734. ERROR_OUT(-5182, out);
  6735. #endif /* HAVE_AES_DECRYPT */
  6736. #endif /* WOLFSSL_AES_128 */
  6737. #ifdef WOLFSSL_AES_192
  6738. /* 192 key size test */
  6739. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  6740. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  6741. ret = EVP_test(EVP_aes_192_cfb128(), key2, iv, msg2, sizeof(msg2),
  6742. cipher2, sizeof(cipher2));
  6743. if (ret != 0) {
  6744. return ret;
  6745. }
  6746. #endif
  6747. ret = wc_AesSetKey(enc, key2, sizeof(key2), iv, AES_ENCRYPTION);
  6748. if (ret != 0)
  6749. ERROR_OUT(-5183, out);
  6750. #ifdef HAVE_AES_DECRYPT
  6751. /* decrypt uses AES_ENCRYPTION */
  6752. ret = wc_AesSetKey(dec, key2, sizeof(key2), iv, AES_ENCRYPTION);
  6753. if (ret != 0)
  6754. ERROR_OUT(-5184, out);
  6755. #endif
  6756. XMEMSET(cipher, 0, sizeof(cipher));
  6757. ret = wc_AesCfbEncrypt(enc, cipher, msg2, AES_BLOCK_SIZE * 4);
  6758. if (ret != 0)
  6759. ERROR_OUT(-5185, out);
  6760. if (XMEMCMP(cipher, cipher2, AES_BLOCK_SIZE * 4))
  6761. ERROR_OUT(-5186, out);
  6762. #ifdef HAVE_AES_DECRYPT
  6763. ret = wc_AesCfbDecrypt(dec, plain, cipher, AES_BLOCK_SIZE * 4);
  6764. if (ret != 0)
  6765. ERROR_OUT(-5187, out);
  6766. if (XMEMCMP(plain, msg2, AES_BLOCK_SIZE * 4))
  6767. ERROR_OUT(-5188, out);
  6768. #endif /* HAVE_AES_DECRYPT */
  6769. #endif /* WOLFSSL_AES_192 */
  6770. #ifdef WOLFSSL_AES_256
  6771. /* 256 key size test */
  6772. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  6773. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  6774. ret = EVP_test(EVP_aes_256_cfb128(), key3, iv, msg3, sizeof(msg3),
  6775. cipher3, sizeof(cipher3));
  6776. if (ret != 0) {
  6777. return ret;
  6778. }
  6779. #endif
  6780. ret = wc_AesSetKey(enc, key3, sizeof(key3), iv, AES_ENCRYPTION);
  6781. if (ret != 0)
  6782. ERROR_OUT(-5189, out);
  6783. #ifdef HAVE_AES_DECRYPT
  6784. /* decrypt uses AES_ENCRYPTION */
  6785. ret = wc_AesSetKey(dec, key3, sizeof(key3), iv, AES_ENCRYPTION);
  6786. if (ret != 0)
  6787. ERROR_OUT(-5190, out);
  6788. #endif
  6789. /* test with data left overs, magic lengths are checking near edges */
  6790. XMEMSET(cipher, 0, sizeof(cipher));
  6791. ret = wc_AesCfbEncrypt(enc, cipher, msg3, 4);
  6792. if (ret != 0)
  6793. ERROR_OUT(-5191, out);
  6794. if (XMEMCMP(cipher, cipher3, 4))
  6795. ERROR_OUT(-5192, out);
  6796. ret = wc_AesCfbEncrypt(enc, cipher + 4, msg3 + 4, 27);
  6797. if (ret != 0)
  6798. ERROR_OUT(-5193, out);
  6799. if (XMEMCMP(cipher + 4, cipher3 + 4, 27))
  6800. ERROR_OUT(-5194, out);
  6801. ret = wc_AesCfbEncrypt(enc, cipher + 31, msg3 + 31,
  6802. (AES_BLOCK_SIZE * 4) - 31);
  6803. if (ret != 0)
  6804. ERROR_OUT(-5195, out);
  6805. if (XMEMCMP(cipher, cipher3, AES_BLOCK_SIZE * 4))
  6806. ERROR_OUT(-5196, out);
  6807. #ifdef HAVE_AES_DECRYPT
  6808. ret = wc_AesCfbDecrypt(dec, plain, cipher, 4);
  6809. if (ret != 0)
  6810. ERROR_OUT(-5197, out);
  6811. if (XMEMCMP(plain, msg3, 4))
  6812. ERROR_OUT(-5198, out);
  6813. ret = wc_AesCfbDecrypt(dec, plain + 4, cipher + 4, 4);
  6814. if (ret != 0)
  6815. ERROR_OUT(-5199, out);
  6816. ret = wc_AesCfbDecrypt(dec, plain + 8, cipher + 8, 23);
  6817. if (ret != 0)
  6818. ERROR_OUT(-5200, out);
  6819. if (XMEMCMP(plain + 4, msg3 + 4, 27))
  6820. ERROR_OUT(-5201, out);
  6821. ret = wc_AesCfbDecrypt(dec, plain + 31, cipher + 31,
  6822. (AES_BLOCK_SIZE * 4) - 31);
  6823. if (ret != 0)
  6824. ERROR_OUT(-5202, out);
  6825. if (XMEMCMP(plain, msg3, AES_BLOCK_SIZE * 4))
  6826. ERROR_OUT(-5203, out);
  6827. #endif /* HAVE_AES_DECRYPT */
  6828. #endif /* WOLFSSL_AES_256 */
  6829. out:
  6830. if (enc_inited)
  6831. wc_AesFree(enc);
  6832. if (dec_inited)
  6833. wc_AesFree(dec);
  6834. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  6835. if (enc)
  6836. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  6837. #ifdef HAVE_AES_DECRYPT
  6838. if (dec)
  6839. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  6840. #endif
  6841. #endif
  6842. return ret;
  6843. }
  6844. #if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  6845. static int aescfb1_test(void)
  6846. {
  6847. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  6848. Aes *enc = NULL;
  6849. #else
  6850. Aes enc[1];
  6851. #endif
  6852. int enc_inited = 0;
  6853. byte cipher[AES_BLOCK_SIZE];
  6854. #ifdef HAVE_AES_DECRYPT
  6855. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  6856. Aes *dec = NULL;
  6857. #else
  6858. Aes dec[1];
  6859. #endif
  6860. int dec_inited = 0;
  6861. byte plain [AES_BLOCK_SIZE];
  6862. #endif
  6863. int ret = 0;
  6864. #ifdef WOLFSSL_AES_128
  6865. WOLFSSL_SMALL_STACK_STATIC const byte iv[] = {
  6866. 0x4d,0xbb,0xdc,0xaa,0x59,0xf3,0x63,0xc9,
  6867. 0x2a,0x3b,0x98,0x43,0xad,0x20,0xe2,0xb7
  6868. };
  6869. WOLFSSL_SMALL_STACK_STATIC const byte key1[] =
  6870. {
  6871. 0xcd,0xef,0x9d,0x06,0x61,0xba,0xe4,0x73,
  6872. 0x8d,0x1a,0x58,0xa2,0xa6,0x22,0x8b,0x66
  6873. };
  6874. WOLFSSL_SMALL_STACK_STATIC const byte cipher1[] =
  6875. {
  6876. 0x00
  6877. };
  6878. WOLFSSL_SMALL_STACK_STATIC const byte msg1[] =
  6879. {
  6880. 0xC0
  6881. };
  6882. #endif /* WOLFSSL_AES_128 */
  6883. #ifdef WOLFSSL_AES_192
  6884. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] = {
  6885. 0x57,0xc6,0x89,0x7c,0x99,0x52,0x28,0x13,
  6886. 0xbf,0x67,0x9c,0xe1,0x13,0x70,0xaf,0x5e
  6887. };
  6888. WOLFSSL_SMALL_STACK_STATIC const byte key2[] =
  6889. {
  6890. 0xba,0xa1,0x58,0xa1,0x6b,0x50,0x4a,0x10,
  6891. 0x8e,0xd4,0x33,0x2e,0xe7,0xf2,0x9b,0xf6,
  6892. 0xd1,0xac,0x46,0xa8,0xde,0x5a,0xfe,0x7a
  6893. };
  6894. WOLFSSL_SMALL_STACK_STATIC const byte cipher2[] =
  6895. {
  6896. 0x30
  6897. };
  6898. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] =
  6899. {
  6900. 0x80
  6901. };
  6902. #endif /* WOLFSSL_AES_192 */
  6903. #ifdef WOLFSSL_AES_256
  6904. WOLFSSL_SMALL_STACK_STATIC const byte iv3[] = {
  6905. 0x63,0x2e,0x9f,0x83,0x1f,0xa3,0x80,0x5e,
  6906. 0x52,0x02,0xbc,0xe0,0x6d,0x04,0xf9,0xa0
  6907. };
  6908. WOLFSSL_SMALL_STACK_STATIC const byte key3[] =
  6909. {
  6910. 0xf6,0xfa,0xe4,0xf1,0x5d,0x91,0xfc,0x50,
  6911. 0x88,0x78,0x4f,0x84,0xa5,0x37,0x12,0x7e,
  6912. 0x32,0x63,0x55,0x9c,0x62,0x73,0x88,0x20,
  6913. 0xc2,0xcf,0x3d,0xe1,0x1c,0x2a,0x30,0x40
  6914. };
  6915. WOLFSSL_SMALL_STACK_STATIC const byte cipher3[] =
  6916. {
  6917. 0xF7, 0x00
  6918. };
  6919. WOLFSSL_SMALL_STACK_STATIC const byte msg3[] =
  6920. {
  6921. 0x41, 0xC0
  6922. };
  6923. #endif /* WOLFSSL_AES_256 */
  6924. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  6925. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  6926. ERROR_OUT(-1, out);
  6927. #ifdef HAVE_AES_DECRYPT
  6928. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  6929. ERROR_OUT(-1, out);
  6930. #endif
  6931. #endif
  6932. if (wc_AesInit(enc, HEAP_HINT, devId) != 0)
  6933. ERROR_OUT(-5204, out);
  6934. else
  6935. enc_inited = 1;
  6936. #ifdef HAVE_AES_DECRYPT
  6937. if (wc_AesInit(dec, HEAP_HINT, devId) != 0)
  6938. ERROR_OUT(-5205, out);
  6939. else
  6940. dec_inited = 1;
  6941. #endif
  6942. #ifdef WOLFSSL_AES_128
  6943. /* 128 key tests */
  6944. ret = wc_AesSetKey(enc, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  6945. if (ret != 0)
  6946. ERROR_OUT(-5206, out);
  6947. #ifdef HAVE_AES_DECRYPT
  6948. /* decrypt uses AES_ENCRYPTION */
  6949. ret = wc_AesSetKey(dec, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  6950. if (ret != 0)
  6951. ERROR_OUT(-5207, out);
  6952. #endif
  6953. XMEMSET(cipher, 0, sizeof(cipher));
  6954. ret = wc_AesCfb1Encrypt(enc, cipher, msg1, 2);
  6955. if (ret != 0)
  6956. ERROR_OUT(-5208, out);
  6957. if (cipher[0] != cipher1[0])
  6958. ERROR_OUT(-5209, out);
  6959. #ifdef HAVE_AES_DECRYPT
  6960. ret = wc_AesCfb1Decrypt(dec, plain, cipher, 2);
  6961. if (ret != 0)
  6962. ERROR_OUT(-5210, out);
  6963. if (plain[0] != msg1[0])
  6964. ERROR_OUT(-5211, out);
  6965. #endif /* HAVE_AES_DECRYPT */
  6966. #ifdef OPENSSL_EXTRA
  6967. ret = wc_AesSetKey(enc, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  6968. if (ret != 0)
  6969. ERROR_OUT(-5212, out);
  6970. XMEMSET(cipher, 0, sizeof(cipher));
  6971. ret = wc_AesCfb1Encrypt(enc, cipher, msg1,
  6972. sizeof(msg1) * WOLFSSL_BIT_SIZE);
  6973. if (ret != 0)
  6974. ERROR_OUT(-5213, out);
  6975. #ifndef WOLFCRYPT_ONLY
  6976. ret = EVP_test(EVP_aes_128_cfb1(), key1, iv, msg1, sizeof(msg1),
  6977. cipher, sizeof(msg1));
  6978. if (ret != 0) {
  6979. goto out;
  6980. }
  6981. #endif
  6982. #endif
  6983. #endif /* WOLFSSL_AES_128 */
  6984. #ifdef WOLFSSL_AES_192
  6985. /* 192 key tests */
  6986. ret = wc_AesSetKey(enc, key2, sizeof(key2), iv2, AES_ENCRYPTION);
  6987. if (ret != 0)
  6988. ERROR_OUT(-5214, out);
  6989. XMEMSET(cipher, 0, sizeof(cipher));
  6990. ret = wc_AesCfb1Encrypt(enc, cipher, msg2, 4);
  6991. if (ret != 0)
  6992. ERROR_OUT(-5215, out);
  6993. if (XMEMCMP(cipher, cipher2, sizeof(cipher2)) != 0)
  6994. ERROR_OUT(-5216, out);
  6995. #ifdef OPENSSL_EXTRA
  6996. ret = wc_AesSetKey(enc, key2, sizeof(key2), iv2, AES_ENCRYPTION);
  6997. if (ret != 0)
  6998. ERROR_OUT(-5217, out);
  6999. XMEMSET(cipher, 0, sizeof(cipher));
  7000. ret = wc_AesCfb1Encrypt(enc, cipher, msg2,
  7001. sizeof(msg2) * WOLFSSL_BIT_SIZE);
  7002. if (ret != 0)
  7003. ERROR_OUT(-5218, out);
  7004. #ifndef WOLFCRYPT_ONLY
  7005. ret = EVP_test(EVP_aes_192_cfb1(), key2, iv2, msg2, sizeof(msg2),
  7006. cipher, sizeof(msg2));
  7007. if (ret != 0) {
  7008. goto out;
  7009. }
  7010. #endif
  7011. #endif
  7012. #endif /* WOLFSSL_AES_192 */
  7013. #ifdef WOLFSSL_AES_256
  7014. /* 256 key tests */
  7015. ret = wc_AesSetKey(enc, key3, sizeof(key3), iv3, AES_ENCRYPTION);
  7016. if (ret != 0)
  7017. ERROR_OUT(-5219, out);
  7018. XMEMSET(cipher, 0, sizeof(cipher));
  7019. ret = wc_AesCfb1Encrypt(enc, cipher, msg3, 10);
  7020. if (ret != 0)
  7021. ERROR_OUT(-5220, out);
  7022. if (XMEMCMP(cipher, cipher3, sizeof(cipher3)) != 0)
  7023. ERROR_OUT(-5221, out);
  7024. #ifdef OPENSSL_EXTRA
  7025. ret = wc_AesSetKey(enc, key3, sizeof(key3), iv3, AES_ENCRYPTION);
  7026. if (ret != 0)
  7027. ERROR_OUT(-5222, out);
  7028. XMEMSET(cipher, 0, sizeof(cipher));
  7029. ret = wc_AesCfb1Encrypt(enc, cipher, msg3,
  7030. sizeof(msg3) * WOLFSSL_BIT_SIZE);
  7031. if (ret != 0)
  7032. ERROR_OUT(-5223, out);
  7033. #ifndef WOLFCRYPT_ONLY
  7034. ret = EVP_test(EVP_aes_256_cfb1(), key3, iv3, msg3, sizeof(msg3),
  7035. cipher, sizeof(msg3));
  7036. if (ret != 0) {
  7037. goto out;
  7038. }
  7039. #endif
  7040. #endif
  7041. out:
  7042. if (enc_inited)
  7043. wc_AesFree(enc);
  7044. #ifdef HAVE_AES_DECRYPT
  7045. if (dec_inited)
  7046. wc_AesFree(dec);
  7047. #endif
  7048. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7049. if (enc)
  7050. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  7051. #ifdef HAVE_AES_DECRYPT
  7052. if (dec)
  7053. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  7054. #endif
  7055. #endif
  7056. #endif /* WOLFSSL_AES_256 */
  7057. return ret;
  7058. }
  7059. static int aescfb8_test(void)
  7060. {
  7061. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7062. Aes *enc = NULL;
  7063. #else
  7064. Aes enc[1];
  7065. #endif
  7066. int enc_inited = 0;
  7067. byte cipher[AES_BLOCK_SIZE];
  7068. #ifdef HAVE_AES_DECRYPT
  7069. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7070. Aes *dec = NULL;
  7071. #else
  7072. Aes dec[1];
  7073. #endif
  7074. int dec_inited = 0;
  7075. byte plain [AES_BLOCK_SIZE];
  7076. #endif
  7077. int ret = 0;
  7078. #ifdef WOLFSSL_AES_128
  7079. WOLFSSL_SMALL_STACK_STATIC const byte iv[] = {
  7080. 0xf4,0x75,0xc6,0x49,0x91,0xb2,0x0e,0xae,
  7081. 0xe1,0x83,0xa2,0x26,0x29,0xe2,0x1e,0x22
  7082. };
  7083. WOLFSSL_SMALL_STACK_STATIC const byte key1[] =
  7084. {
  7085. 0xc8,0xfe,0x9b,0xf7,0x7b,0x93,0x0f,0x46,
  7086. 0xd2,0x07,0x8b,0x8c,0x0e,0x65,0x7c,0xd4
  7087. };
  7088. WOLFSSL_SMALL_STACK_STATIC const byte cipher1[] =
  7089. {
  7090. 0xd2,0x76,0x91
  7091. };
  7092. WOLFSSL_SMALL_STACK_STATIC const byte msg1[] =
  7093. {
  7094. 0xc9,0x06,0x35
  7095. };
  7096. #endif /* WOLFSSL_AES_128 */
  7097. #ifdef WOLFSSL_AES_192
  7098. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] = {
  7099. 0x0a,0x02,0x84,0x6b,0x62,0xab,0xb6,0x93,
  7100. 0xef,0x31,0xd7,0x54,0x84,0x2e,0xed,0x29
  7101. };
  7102. WOLFSSL_SMALL_STACK_STATIC const byte key2[] =
  7103. {
  7104. 0xba,0xf0,0x8b,0x76,0x31,0x7a,0x65,0xc5,
  7105. 0xf0,0x7a,0xe6,0xf5,0x7e,0xb0,0xe6,0x54,
  7106. 0x88,0x65,0x93,0x24,0xd2,0x97,0x09,0xe3
  7107. };
  7108. WOLFSSL_SMALL_STACK_STATIC const byte cipher2[] =
  7109. {
  7110. 0x72,0x9c,0x0b,0x6d,0xeb,0x75,0xfa,0x6e,
  7111. 0xb5,0xe8
  7112. };
  7113. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] =
  7114. {
  7115. 0x98,0x95,0x93,0x24,0x02,0x39,0x3d,0xc3,
  7116. 0x3a,0x60
  7117. };
  7118. #endif
  7119. #ifdef WOLFSSL_AES_256
  7120. WOLFSSL_SMALL_STACK_STATIC const byte iv3[] = {
  7121. 0x33,0x8c,0x55,0x2f,0xf1,0xec,0xa1,0x44,
  7122. 0x08,0xe0,0x5d,0x8c,0xf9,0xf3,0xb3,0x1b
  7123. };
  7124. WOLFSSL_SMALL_STACK_STATIC const byte key3[] =
  7125. {
  7126. 0x06,0x48,0x74,0x09,0x2f,0x7a,0x13,0xcc,
  7127. 0x44,0x62,0x24,0x7a,0xd4,0x23,0xd0,0xe9,
  7128. 0x6e,0xdf,0x42,0xe8,0xb6,0x7a,0x5a,0x23,
  7129. 0xb7,0xa0,0xa6,0x47,0x7b,0x09,0x8e,0x66
  7130. };
  7131. WOLFSSL_SMALL_STACK_STATIC const byte cipher3[] =
  7132. {
  7133. 0x1c,0xff,0x95
  7134. };
  7135. WOLFSSL_SMALL_STACK_STATIC const byte msg3[] =
  7136. {
  7137. 0xb9,0x74,0xfa
  7138. };
  7139. #endif
  7140. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7141. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  7142. ERROR_OUT(-5238, out);
  7143. #ifdef HAVE_AES_DECRYPT
  7144. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  7145. ERROR_OUT(-5239, out);
  7146. #endif
  7147. #endif
  7148. if (wc_AesInit(enc, HEAP_HINT, devId) != 0)
  7149. ERROR_OUT(-5224, out);
  7150. else
  7151. enc_inited = 1;
  7152. #ifdef HAVE_AES_DECRYPT
  7153. if (wc_AesInit(dec, HEAP_HINT, devId) != 0)
  7154. ERROR_OUT(-5225, out);
  7155. else
  7156. dec_inited = 1;
  7157. #endif
  7158. #ifdef WOLFSSL_AES_128
  7159. /* 128 key tests */
  7160. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  7161. ret = EVP_test(EVP_aes_128_cfb8(), key1, iv, msg1, sizeof(msg1),
  7162. cipher1, sizeof(cipher1));
  7163. if (ret != 0) {
  7164. return ret;
  7165. }
  7166. #endif
  7167. ret = wc_AesSetKey(enc, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  7168. if (ret != 0)
  7169. ERROR_OUT(-5226, out);
  7170. #ifdef HAVE_AES_DECRYPT
  7171. /* decrypt uses AES_ENCRYPTION */
  7172. ret = wc_AesSetKey(dec, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  7173. if (ret != 0)
  7174. ERROR_OUT(-5227, out);
  7175. #endif
  7176. XMEMSET(cipher, 0, sizeof(cipher));
  7177. ret = wc_AesCfb8Encrypt(enc, cipher, msg1, sizeof(msg1));
  7178. if (ret != 0)
  7179. ERROR_OUT(-5228, out);
  7180. if (XMEMCMP(cipher, cipher1, sizeof(cipher1)) != 0)
  7181. ERROR_OUT(-5229, out);
  7182. #ifdef HAVE_AES_DECRYPT
  7183. ret = wc_AesCfb8Decrypt(dec, plain, cipher, sizeof(msg1));
  7184. if (ret != 0)
  7185. ERROR_OUT(-5230, out);
  7186. if (XMEMCMP(plain, msg1, sizeof(msg1)) != 0)
  7187. ERROR_OUT(-5231, out);
  7188. #endif /* HAVE_AES_DECRYPT */
  7189. #endif /* WOLFSSL_AES_128 */
  7190. #ifdef WOLFSSL_AES_192
  7191. /* 192 key tests */
  7192. ret = wc_AesSetKey(enc, key2, sizeof(key2), iv2, AES_ENCRYPTION);
  7193. if (ret != 0)
  7194. ERROR_OUT(-5232, out);
  7195. XMEMSET(cipher, 0, sizeof(cipher));
  7196. ret = wc_AesCfb8Encrypt(enc, cipher, msg2, sizeof(msg2));
  7197. if (ret != 0)
  7198. ERROR_OUT(-5233, out);
  7199. if (XMEMCMP(cipher, cipher2, sizeof(msg2)) != 0)
  7200. ERROR_OUT(-5234, out);
  7201. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  7202. ret = EVP_test(EVP_aes_192_cfb8(), key2, iv2, msg2, sizeof(msg2),
  7203. cipher2, sizeof(msg2));
  7204. if (ret != 0) {
  7205. return ret;
  7206. }
  7207. #endif
  7208. #endif /* WOLFSSL_AES_192 */
  7209. #ifdef WOLFSSL_AES_256
  7210. /* 256 key tests */
  7211. ret = wc_AesSetKey(enc, key3, sizeof(key3), iv3, AES_ENCRYPTION);
  7212. if (ret != 0)
  7213. ERROR_OUT(-5235, out);
  7214. XMEMSET(cipher, 0, sizeof(cipher));
  7215. ret = wc_AesCfb8Encrypt(enc, cipher, msg3, sizeof(msg3));
  7216. if (ret != 0)
  7217. ERROR_OUT(-5236, out);
  7218. if (XMEMCMP(cipher, cipher3, sizeof(cipher3)) != 0)
  7219. ERROR_OUT(-5237, out);
  7220. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  7221. ret = EVP_test(EVP_aes_256_cfb8(), key3, iv3, msg3, sizeof(msg3),
  7222. cipher3, sizeof(msg3));
  7223. if (ret != 0) {
  7224. goto out;
  7225. }
  7226. #endif
  7227. out:
  7228. if (enc_inited)
  7229. wc_AesFree(enc);
  7230. #ifdef HAVE_AES_DECRYPT
  7231. if (dec_inited)
  7232. wc_AesFree(dec);
  7233. #endif
  7234. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7235. if (enc)
  7236. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  7237. #ifdef HAVE_AES_DECRYPT
  7238. if (dec)
  7239. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  7240. #endif
  7241. #endif
  7242. #endif /* WOLFSSL_AES_256 */
  7243. return ret;
  7244. }
  7245. #endif /* !HAVE_SELFTEST && !HAVE_FIPS */
  7246. #endif /* WOLFSSL_AES_CFB */
  7247. static int aes_key_size_test(void)
  7248. {
  7249. int ret;
  7250. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7251. Aes *aes;
  7252. #else
  7253. Aes aes[1];
  7254. #endif
  7255. byte key16[] = { 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  7256. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66 };
  7257. #ifndef WOLFSSL_CRYPTOCELL
  7258. byte key24[] = { 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  7259. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  7260. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37 };
  7261. #endif
  7262. byte key32[] = { 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  7263. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  7264. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  7265. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66 };
  7266. byte iv[] = "1234567890abcdef";
  7267. #ifndef HAVE_FIPS
  7268. word32 keySize;
  7269. #endif
  7270. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7271. if ((aes = (Aes *)XMALLOC(sizeof *aes, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  7272. return -5315;
  7273. #endif
  7274. #if !defined(HAVE_FIPS) || \
  7275. defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)
  7276. /* w/ FIPS v1 (cert 2425) wc_AesInit just returns 0 always as it's not
  7277. * supported with that FIPS version */
  7278. ret = wc_AesInit(NULL, HEAP_HINT, devId);
  7279. if (ret != BAD_FUNC_ARG)
  7280. ERROR_OUT(-5300, out);
  7281. #endif
  7282. ret = wc_AesInit(aes, HEAP_HINT, devId);
  7283. /* 0 check OK for FIPSv1 */
  7284. if (ret != 0)
  7285. ERROR_OUT(-5301, out);
  7286. #ifndef HAVE_FIPS
  7287. /* Parameter Validation testing. */
  7288. ret = wc_AesGetKeySize(NULL, NULL);
  7289. if (ret != BAD_FUNC_ARG)
  7290. ERROR_OUT(-5302, out);
  7291. ret = wc_AesGetKeySize(aes, NULL);
  7292. if (ret != BAD_FUNC_ARG)
  7293. ERROR_OUT(-5303, out);
  7294. ret = wc_AesGetKeySize(NULL, &keySize);
  7295. if (ret != BAD_FUNC_ARG)
  7296. ERROR_OUT(-5304, out);
  7297. /* Crashes in FIPS */
  7298. ret = wc_AesSetKey(NULL, key16, sizeof(key16), iv, AES_ENCRYPTION);
  7299. if (ret != BAD_FUNC_ARG)
  7300. ERROR_OUT(-5305, out);
  7301. #endif
  7302. /* NULL IV indicates to use all zeros IV. */
  7303. ret = wc_AesSetKey(aes, key16, sizeof(key16), NULL, AES_ENCRYPTION);
  7304. #ifdef WOLFSSL_AES_128
  7305. if (ret != 0)
  7306. #else
  7307. if (ret != BAD_FUNC_ARG)
  7308. #endif
  7309. ERROR_OUT(-5306, out);
  7310. ret = wc_AesSetKey(aes, key32, sizeof(key32) - 1, iv, AES_ENCRYPTION);
  7311. if (ret != BAD_FUNC_ARG)
  7312. ERROR_OUT(-5307, out);
  7313. /* CryptoCell handles rounds internally */
  7314. #if !defined(HAVE_FIPS) && !defined(WOLFSSL_CRYPTOCELL)
  7315. /* PSA don't use aes->rounds */
  7316. #if !defined(WOLFSSL_HAVE_PSA) || defined(WOLFSSL_PSA_NO_AES)
  7317. /* Force invalid rounds */
  7318. aes->rounds = 16;
  7319. ret = wc_AesGetKeySize(aes, &keySize);
  7320. if (ret != BAD_FUNC_ARG)
  7321. ERROR_OUT(-5308, out);
  7322. #endif
  7323. #endif
  7324. ret = wc_AesSetKey(aes, key16, sizeof(key16), iv, AES_ENCRYPTION);
  7325. #ifdef WOLFSSL_AES_128
  7326. if (ret != 0)
  7327. #else
  7328. if (ret != BAD_FUNC_ARG)
  7329. #endif
  7330. ERROR_OUT(-5309, out);
  7331. #if !defined(HAVE_FIPS) && defined(WOLFSSL_AES_128)
  7332. ret = wc_AesGetKeySize(aes, &keySize);
  7333. if (ret != 0 || keySize != sizeof(key16))
  7334. ERROR_OUT(-5310, out);
  7335. #endif
  7336. #ifndef WOLFSSL_CRYPTOCELL
  7337. /* Cryptocell only supports AES-128 key size */
  7338. ret = wc_AesSetKey(aes, key24, sizeof(key24), iv, AES_ENCRYPTION);
  7339. #ifdef WOLFSSL_AES_192
  7340. if (ret != 0)
  7341. #else
  7342. if (ret != BAD_FUNC_ARG)
  7343. #endif
  7344. ERROR_OUT(-5311, out);
  7345. #if !defined(HAVE_FIPS) && defined(WOLFSSL_AES_192)
  7346. ret = wc_AesGetKeySize(aes, &keySize);
  7347. if (ret != 0 || keySize != sizeof(key24))
  7348. ERROR_OUT(-5312, out);
  7349. #endif
  7350. ret = wc_AesSetKey(aes, key32, sizeof(key32), iv, AES_ENCRYPTION);
  7351. #ifdef WOLFSSL_AES_256
  7352. if (ret != 0)
  7353. #else
  7354. if (ret != BAD_FUNC_ARG)
  7355. #endif
  7356. ERROR_OUT(-5313, out);
  7357. #if !defined(HAVE_FIPS) && defined(WOLFSSL_AES_256)
  7358. ret = wc_AesGetKeySize(aes, &keySize);
  7359. if (ret != 0 || keySize != sizeof(key32))
  7360. ERROR_OUT(-5314, out);
  7361. #endif
  7362. #endif /* !WOLFSSL_CRYPTOCELL */
  7363. ret = 0; /* success */
  7364. out:
  7365. wc_AesFree(aes);
  7366. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7367. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_AES);
  7368. #endif
  7369. return ret;
  7370. }
  7371. #if defined(WOLFSSL_AES_XTS)
  7372. /* test vectors from http://csrc.nist.gov/groups/STM/cavp/block-cipher-modes.html */
  7373. #ifdef WOLFSSL_AES_128
  7374. static int aes_xts_128_test(void)
  7375. {
  7376. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7377. XtsAes *aes = NULL;
  7378. #else
  7379. XtsAes aes[1];
  7380. #endif
  7381. int aes_inited = 0;
  7382. int ret = 0;
  7383. unsigned char buf[AES_BLOCK_SIZE * 2];
  7384. unsigned char cipher[AES_BLOCK_SIZE * 2];
  7385. /* 128 key tests */
  7386. WOLFSSL_SMALL_STACK_STATIC unsigned char k1[] = {
  7387. 0xa1, 0xb9, 0x0c, 0xba, 0x3f, 0x06, 0xac, 0x35,
  7388. 0x3b, 0x2c, 0x34, 0x38, 0x76, 0x08, 0x17, 0x62,
  7389. 0x09, 0x09, 0x23, 0x02, 0x6e, 0x91, 0x77, 0x18,
  7390. 0x15, 0xf2, 0x9d, 0xab, 0x01, 0x93, 0x2f, 0x2f
  7391. };
  7392. WOLFSSL_SMALL_STACK_STATIC unsigned char i1[] = {
  7393. 0x4f, 0xae, 0xf7, 0x11, 0x7c, 0xda, 0x59, 0xc6,
  7394. 0x6e, 0x4b, 0x92, 0x01, 0x3e, 0x76, 0x8a, 0xd5
  7395. };
  7396. WOLFSSL_SMALL_STACK_STATIC unsigned char p1[] = {
  7397. 0xeb, 0xab, 0xce, 0x95, 0xb1, 0x4d, 0x3c, 0x8d,
  7398. 0x6f, 0xb3, 0x50, 0x39, 0x07, 0x90, 0x31, 0x1c
  7399. };
  7400. /* plain text test of partial block is not from NIST test vector list */
  7401. WOLFSSL_SMALL_STACK_STATIC unsigned char pp[] = {
  7402. 0xeb, 0xab, 0xce, 0x95, 0xb1, 0x4d, 0x3c, 0x8d,
  7403. 0x6f, 0xb3, 0x50, 0x39, 0x07, 0x90, 0x31, 0x1c,
  7404. 0x6e, 0x4b, 0x92, 0x01, 0x3e, 0x76, 0x8a, 0xd5
  7405. };
  7406. WOLFSSL_SMALL_STACK_STATIC unsigned char c1[] = {
  7407. 0x77, 0x8a, 0xe8, 0xb4, 0x3c, 0xb9, 0x8d, 0x5a,
  7408. 0x82, 0x50, 0x81, 0xd5, 0xbe, 0x47, 0x1c, 0x63
  7409. };
  7410. WOLFSSL_SMALL_STACK_STATIC unsigned char k2[] = {
  7411. 0x39, 0x25, 0x79, 0x05, 0xdf, 0xcc, 0x77, 0x76,
  7412. 0x6c, 0x87, 0x0a, 0x80, 0x6a, 0x60, 0xe3, 0xc0,
  7413. 0x93, 0xd1, 0x2a, 0xcf, 0xcb, 0x51, 0x42, 0xfa,
  7414. 0x09, 0x69, 0x89, 0x62, 0x5b, 0x60, 0xdb, 0x16
  7415. };
  7416. WOLFSSL_SMALL_STACK_STATIC unsigned char i2[] = {
  7417. 0x5c, 0xf7, 0x9d, 0xb6, 0xc5, 0xcd, 0x99, 0x1a,
  7418. 0x1c, 0x78, 0x81, 0x42, 0x24, 0x95, 0x1e, 0x84
  7419. };
  7420. WOLFSSL_SMALL_STACK_STATIC unsigned char p2[] = {
  7421. 0xbd, 0xc5, 0x46, 0x8f, 0xbc, 0x8d, 0x50, 0xa1,
  7422. 0x0d, 0x1c, 0x85, 0x7f, 0x79, 0x1c, 0x5c, 0xba,
  7423. 0xb3, 0x81, 0x0d, 0x0d, 0x73, 0xcf, 0x8f, 0x20,
  7424. 0x46, 0xb1, 0xd1, 0x9e, 0x7d, 0x5d, 0x8a, 0x56
  7425. };
  7426. WOLFSSL_SMALL_STACK_STATIC unsigned char c2[] = {
  7427. 0xd6, 0xbe, 0x04, 0x6d, 0x41, 0xf2, 0x3b, 0x5e,
  7428. 0xd7, 0x0b, 0x6b, 0x3d, 0x5c, 0x8e, 0x66, 0x23,
  7429. 0x2b, 0xe6, 0xb8, 0x07, 0xd4, 0xdc, 0xc6, 0x0e,
  7430. 0xff, 0x8d, 0xbc, 0x1d, 0x9f, 0x7f, 0xc8, 0x22
  7431. };
  7432. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7433. if ((aes = (XtsAes *)XMALLOC(sizeof *aes, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  7434. ERROR_OUT(-5417, out);
  7435. #endif
  7436. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  7437. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  7438. ret = EVP_test(EVP_aes_128_xts(), k2, i2, p2, sizeof(p2), c2, sizeof(c2));
  7439. if (ret != 0) {
  7440. printf("EVP_aes_128_xts failed!\n");
  7441. goto out;
  7442. }
  7443. #endif
  7444. XMEMSET(buf, 0, sizeof(buf));
  7445. if (wc_AesXtsSetKey(aes, k2, sizeof(k2), AES_ENCRYPTION,
  7446. HEAP_HINT, devId) != 0)
  7447. ERROR_OUT(-5400, out);
  7448. else
  7449. aes_inited = 1;
  7450. ret = wc_AesXtsEncrypt(aes, buf, p2, sizeof(p2), i2, sizeof(i2));
  7451. #if defined(WOLFSSL_ASYNC_CRYPT)
  7452. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7453. #endif
  7454. if (ret != 0)
  7455. ERROR_OUT(-5401, out);
  7456. if (XMEMCMP(c2, buf, sizeof(c2)))
  7457. ERROR_OUT(-5402, out);
  7458. XMEMSET(buf, 0, sizeof(buf));
  7459. wc_AesXtsFree(aes);
  7460. if (wc_AesXtsSetKey(aes, k1, sizeof(k1), AES_ENCRYPTION,
  7461. HEAP_HINT, devId) != 0)
  7462. ERROR_OUT(-5403, out);
  7463. ret = wc_AesXtsEncrypt(aes, buf, p1, sizeof(p1), i1, sizeof(i1));
  7464. #if defined(WOLFSSL_ASYNC_CRYPT)
  7465. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7466. #endif
  7467. if (ret != 0)
  7468. ERROR_OUT(-5404, out);
  7469. if (XMEMCMP(c1, buf, AES_BLOCK_SIZE))
  7470. ERROR_OUT(-5405, out);
  7471. /* partial block encryption test */
  7472. XMEMSET(cipher, 0, sizeof(cipher));
  7473. ret = wc_AesXtsEncrypt(aes, cipher, pp, sizeof(pp), i1, sizeof(i1));
  7474. #if defined(WOLFSSL_ASYNC_CRYPT)
  7475. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7476. #endif
  7477. if (ret != 0)
  7478. ERROR_OUT(-5406, out);
  7479. wc_AesXtsFree(aes);
  7480. /* partial block decrypt test */
  7481. XMEMSET(buf, 0, sizeof(buf));
  7482. if (wc_AesXtsSetKey(aes, k1, sizeof(k1), AES_DECRYPTION,
  7483. HEAP_HINT, devId) != 0)
  7484. ERROR_OUT(-5407, out);
  7485. ret = wc_AesXtsDecrypt(aes, buf, cipher, sizeof(pp), i1, sizeof(i1));
  7486. #if defined(WOLFSSL_ASYNC_CRYPT)
  7487. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7488. #endif
  7489. if (ret != 0)
  7490. ERROR_OUT(-5408, out);
  7491. if (XMEMCMP(pp, buf, sizeof(pp)))
  7492. ERROR_OUT(-5409, out);
  7493. /* NIST decrypt test vector */
  7494. XMEMSET(buf, 0, sizeof(buf));
  7495. ret = wc_AesXtsDecrypt(aes, buf, c1, sizeof(c1), i1, sizeof(i1));
  7496. #if defined(WOLFSSL_ASYNC_CRYPT)
  7497. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7498. #endif
  7499. if (ret != 0)
  7500. ERROR_OUT(-5410, out);
  7501. if (XMEMCMP(p1, buf, AES_BLOCK_SIZE))
  7502. ERROR_OUT(-5411, out);
  7503. /* fail case with decrypting using wrong key */
  7504. XMEMSET(buf, 0, sizeof(buf));
  7505. ret = wc_AesXtsDecrypt(aes, buf, c2, sizeof(c2), i2, sizeof(i2));
  7506. #if defined(WOLFSSL_ASYNC_CRYPT)
  7507. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7508. #endif
  7509. if (ret != 0)
  7510. ERROR_OUT(-5412, out);
  7511. if (XMEMCMP(p2, buf, sizeof(p2)) == 0) /* fail case with wrong key */
  7512. ERROR_OUT(-5413, out);
  7513. wc_AesXtsFree(aes);
  7514. /* set correct key and retest */
  7515. XMEMSET(buf, 0, sizeof(buf));
  7516. if (wc_AesXtsSetKey(aes, k2, sizeof(k2), AES_DECRYPTION,
  7517. HEAP_HINT, devId) != 0)
  7518. ERROR_OUT(-5414, out);
  7519. ret = wc_AesXtsDecrypt(aes, buf, c2, sizeof(c2), i2, sizeof(i2));
  7520. #if defined(WOLFSSL_ASYNC_CRYPT)
  7521. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7522. #endif
  7523. if (ret != 0)
  7524. ERROR_OUT(-5415, out);
  7525. if (XMEMCMP(p2, buf, sizeof(p2)))
  7526. ERROR_OUT(-5416, out);
  7527. out:
  7528. if (aes_inited)
  7529. wc_AesXtsFree(aes);
  7530. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7531. if (aes)
  7532. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_AES);
  7533. #endif
  7534. return ret;
  7535. }
  7536. #endif /* WOLFSSL_AES_128 */
  7537. #ifdef WOLFSSL_AES_256
  7538. static int aes_xts_256_test(void)
  7539. {
  7540. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7541. XtsAes *aes = NULL;
  7542. #else
  7543. XtsAes aes[1];
  7544. #endif
  7545. int aes_inited = 0;
  7546. int ret = 0;
  7547. unsigned char buf[AES_BLOCK_SIZE * 3];
  7548. unsigned char cipher[AES_BLOCK_SIZE * 3];
  7549. /* 256 key tests */
  7550. WOLFSSL_SMALL_STACK_STATIC unsigned char k1[] = {
  7551. 0x1e, 0xa6, 0x61, 0xc5, 0x8d, 0x94, 0x3a, 0x0e,
  7552. 0x48, 0x01, 0xe4, 0x2f, 0x4b, 0x09, 0x47, 0x14,
  7553. 0x9e, 0x7f, 0x9f, 0x8e, 0x3e, 0x68, 0xd0, 0xc7,
  7554. 0x50, 0x52, 0x10, 0xbd, 0x31, 0x1a, 0x0e, 0x7c,
  7555. 0xd6, 0xe1, 0x3f, 0xfd, 0xf2, 0x41, 0x8d, 0x8d,
  7556. 0x19, 0x11, 0xc0, 0x04, 0xcd, 0xa5, 0x8d, 0xa3,
  7557. 0xd6, 0x19, 0xb7, 0xe2, 0xb9, 0x14, 0x1e, 0x58,
  7558. 0x31, 0x8e, 0xea, 0x39, 0x2c, 0xf4, 0x1b, 0x08
  7559. };
  7560. WOLFSSL_SMALL_STACK_STATIC unsigned char i1[] = {
  7561. 0xad, 0xf8, 0xd9, 0x26, 0x27, 0x46, 0x4a, 0xd2,
  7562. 0xf0, 0x42, 0x8e, 0x84, 0xa9, 0xf8, 0x75, 0x64
  7563. };
  7564. WOLFSSL_SMALL_STACK_STATIC unsigned char p1[] = {
  7565. 0x2e, 0xed, 0xea, 0x52, 0xcd, 0x82, 0x15, 0xe1,
  7566. 0xac, 0xc6, 0x47, 0xe8, 0x10, 0xbb, 0xc3, 0x64,
  7567. 0x2e, 0x87, 0x28, 0x7f, 0x8d, 0x2e, 0x57, 0xe3,
  7568. 0x6c, 0x0a, 0x24, 0xfb, 0xc1, 0x2a, 0x20, 0x2e
  7569. };
  7570. /* plain text test of partial block is not from NIST test vector list */
  7571. WOLFSSL_SMALL_STACK_STATIC unsigned char pp[] = {
  7572. 0xeb, 0xab, 0xce, 0x95, 0xb1, 0x4d, 0x3c, 0x8d,
  7573. 0x6f, 0xb3, 0x50, 0x39, 0x07, 0x90, 0x31, 0x1c,
  7574. 0x6e, 0x4b, 0x92, 0x01, 0x3e, 0x76, 0x8a, 0xd5
  7575. };
  7576. WOLFSSL_SMALL_STACK_STATIC unsigned char c1[] = {
  7577. 0xcb, 0xaa, 0xd0, 0xe2, 0xf6, 0xce, 0xa3, 0xf5,
  7578. 0x0b, 0x37, 0xf9, 0x34, 0xd4, 0x6a, 0x9b, 0x13,
  7579. 0x0b, 0x9d, 0x54, 0xf0, 0x7e, 0x34, 0xf3, 0x6a,
  7580. 0xf7, 0x93, 0xe8, 0x6f, 0x73, 0xc6, 0xd7, 0xdb
  7581. };
  7582. WOLFSSL_SMALL_STACK_STATIC unsigned char k2[] = {
  7583. 0xad, 0x50, 0x4b, 0x85, 0xd7, 0x51, 0xbf, 0xba,
  7584. 0x69, 0x13, 0xb4, 0xcc, 0x79, 0xb6, 0x5a, 0x62,
  7585. 0xf7, 0xf3, 0x9d, 0x36, 0x0f, 0x35, 0xb5, 0xec,
  7586. 0x4a, 0x7e, 0x95, 0xbd, 0x9b, 0xa5, 0xf2, 0xec,
  7587. 0xc1, 0xd7, 0x7e, 0xa3, 0xc3, 0x74, 0xbd, 0x4b,
  7588. 0x13, 0x1b, 0x07, 0x83, 0x87, 0xdd, 0x55, 0x5a,
  7589. 0xb5, 0xb0, 0xc7, 0xe5, 0x2d, 0xb5, 0x06, 0x12,
  7590. 0xd2, 0xb5, 0x3a, 0xcb, 0x47, 0x8a, 0x53, 0xb4
  7591. };
  7592. WOLFSSL_SMALL_STACK_STATIC unsigned char i2[] = {
  7593. 0xe6, 0x42, 0x19, 0xed, 0xe0, 0xe1, 0xc2, 0xa0,
  7594. 0x0e, 0xf5, 0x58, 0x6a, 0xc4, 0x9b, 0xeb, 0x6f
  7595. };
  7596. WOLFSSL_SMALL_STACK_STATIC unsigned char p2[] = {
  7597. 0x24, 0xcb, 0x76, 0x22, 0x55, 0xb5, 0xa8, 0x00,
  7598. 0xf4, 0x6e, 0x80, 0x60, 0x56, 0x9e, 0x05, 0x53,
  7599. 0xbc, 0xfe, 0x86, 0x55, 0x3b, 0xca, 0xd5, 0x89,
  7600. 0xc7, 0x54, 0x1a, 0x73, 0xac, 0xc3, 0x9a, 0xbd,
  7601. 0x53, 0xc4, 0x07, 0x76, 0xd8, 0xe8, 0x22, 0x61,
  7602. 0x9e, 0xa9, 0xad, 0x77, 0xa0, 0x13, 0x4c, 0xfc
  7603. };
  7604. WOLFSSL_SMALL_STACK_STATIC unsigned char c2[] = {
  7605. 0xa3, 0xc6, 0xf3, 0xf3, 0x82, 0x79, 0x5b, 0x10,
  7606. 0x87, 0xd7, 0x02, 0x50, 0xdb, 0x2c, 0xd3, 0xb1,
  7607. 0xa1, 0x62, 0xa8, 0xb6, 0xdc, 0x12, 0x60, 0x61,
  7608. 0xc1, 0x0a, 0x84, 0xa5, 0x85, 0x3f, 0x3a, 0x89,
  7609. 0xe6, 0x6c, 0xdb, 0xb7, 0x9a, 0xb4, 0x28, 0x9b,
  7610. 0xc3, 0xea, 0xd8, 0x10, 0xe9, 0xc0, 0xaf, 0x92
  7611. };
  7612. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7613. if ((aes = (XtsAes *)XMALLOC(sizeof *aes, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  7614. ERROR_OUT(-5515, out);
  7615. #endif
  7616. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  7617. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  7618. ret = EVP_test(EVP_aes_256_xts(), k2, i2, p2, sizeof(p2), c2, sizeof(c2));
  7619. if (ret != 0) {
  7620. printf("EVP_aes_256_xts failed\n");
  7621. goto out;
  7622. }
  7623. #endif
  7624. XMEMSET(buf, 0, sizeof(buf));
  7625. if (wc_AesXtsSetKey(aes, k2, sizeof(k2), AES_ENCRYPTION,
  7626. HEAP_HINT, devId) != 0)
  7627. ERROR_OUT(-5500, out);
  7628. else
  7629. aes_inited = 1;
  7630. ret = wc_AesXtsEncrypt(aes, buf, p2, sizeof(p2), i2, sizeof(i2));
  7631. #if defined(WOLFSSL_ASYNC_CRYPT)
  7632. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7633. #endif
  7634. if (ret != 0)
  7635. ERROR_OUT(-5501, out);
  7636. if (XMEMCMP(c2, buf, sizeof(c2)))
  7637. ERROR_OUT(-5502, out);
  7638. wc_AesXtsFree(aes);
  7639. XMEMSET(buf, 0, sizeof(buf));
  7640. if (wc_AesXtsSetKey(aes, k1, sizeof(k1), AES_ENCRYPTION,
  7641. HEAP_HINT, devId) != 0)
  7642. ERROR_OUT(-5503, out);
  7643. ret = wc_AesXtsEncrypt(aes, buf, p1, sizeof(p1), i1, sizeof(i1));
  7644. #if defined(WOLFSSL_ASYNC_CRYPT)
  7645. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7646. #endif
  7647. if (ret != 0)
  7648. ERROR_OUT(-5504, out);
  7649. if (XMEMCMP(c1, buf, AES_BLOCK_SIZE))
  7650. ERROR_OUT(-5505, out);
  7651. /* partial block encryption test */
  7652. XMEMSET(cipher, 0, sizeof(cipher));
  7653. ret = wc_AesXtsEncrypt(aes, cipher, pp, sizeof(pp), i1, sizeof(i1));
  7654. #if defined(WOLFSSL_ASYNC_CRYPT)
  7655. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7656. #endif
  7657. if (ret != 0)
  7658. ERROR_OUT(-5506, out);
  7659. wc_AesXtsFree(aes);
  7660. /* partial block decrypt test */
  7661. XMEMSET(buf, 0, sizeof(buf));
  7662. if (wc_AesXtsSetKey(aes, k1, sizeof(k1), AES_DECRYPTION,
  7663. HEAP_HINT, devId) != 0)
  7664. ERROR_OUT(-5507, out);
  7665. ret = wc_AesXtsDecrypt(aes, buf, cipher, sizeof(pp), i1, sizeof(i1));
  7666. #if defined(WOLFSSL_ASYNC_CRYPT)
  7667. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7668. #endif
  7669. if (ret != 0)
  7670. ERROR_OUT(-5508, out);
  7671. if (XMEMCMP(pp, buf, sizeof(pp)))
  7672. ERROR_OUT(-5509, out);
  7673. /* NIST decrypt test vector */
  7674. XMEMSET(buf, 0, sizeof(buf));
  7675. ret = wc_AesXtsDecrypt(aes, buf, c1, sizeof(c1), i1, sizeof(i1));
  7676. #if defined(WOLFSSL_ASYNC_CRYPT)
  7677. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7678. #endif
  7679. if (ret != 0)
  7680. ERROR_OUT(-5510, out);
  7681. if (XMEMCMP(p1, buf, AES_BLOCK_SIZE))
  7682. ERROR_OUT(-5511, out);
  7683. wc_AesXtsFree(aes);
  7684. XMEMSET(buf, 0, sizeof(buf));
  7685. if (wc_AesXtsSetKey(aes, k2, sizeof(k2), AES_DECRYPTION,
  7686. HEAP_HINT, devId) != 0)
  7687. ERROR_OUT(-5512, out);
  7688. ret = wc_AesXtsDecrypt(aes, buf, c2, sizeof(c2), i2, sizeof(i2));
  7689. #if defined(WOLFSSL_ASYNC_CRYPT)
  7690. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7691. #endif
  7692. if (ret != 0)
  7693. ERROR_OUT(-5513, out);
  7694. if (XMEMCMP(p2, buf, sizeof(p2)))
  7695. ERROR_OUT(-5514, out);
  7696. out:
  7697. if (aes_inited)
  7698. wc_AesXtsFree(aes);
  7699. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7700. if (aes)
  7701. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_AES);
  7702. #endif
  7703. return ret;
  7704. }
  7705. #endif /* WOLFSSL_AES_256 */
  7706. #if defined(WOLFSSL_AES_128) && defined(WOLFSSL_AES_256)
  7707. /* both 128 and 256 bit key test */
  7708. static int aes_xts_sector_test(void)
  7709. {
  7710. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7711. XtsAes *aes = NULL;
  7712. #else
  7713. XtsAes aes[1];
  7714. #endif
  7715. int aes_inited = 0;
  7716. int ret = 0;
  7717. unsigned char buf[AES_BLOCK_SIZE * 2];
  7718. /* 128 key tests */
  7719. WOLFSSL_SMALL_STACK_STATIC unsigned char k1[] = {
  7720. 0xa3, 0xe4, 0x0d, 0x5b, 0xd4, 0xb6, 0xbb, 0xed,
  7721. 0xb2, 0xd1, 0x8c, 0x70, 0x0a, 0xd2, 0xdb, 0x22,
  7722. 0x10, 0xc8, 0x11, 0x90, 0x64, 0x6d, 0x67, 0x3c,
  7723. 0xbc, 0xa5, 0x3f, 0x13, 0x3e, 0xab, 0x37, 0x3c
  7724. };
  7725. WOLFSSL_SMALL_STACK_STATIC unsigned char p1[] = {
  7726. 0x20, 0xe0, 0x71, 0x94, 0x05, 0x99, 0x3f, 0x09,
  7727. 0xa6, 0x6a, 0xe5, 0xbb, 0x50, 0x0e, 0x56, 0x2c
  7728. };
  7729. WOLFSSL_SMALL_STACK_STATIC unsigned char c1[] = {
  7730. 0x74, 0x62, 0x35, 0x51, 0x21, 0x02, 0x16, 0xac,
  7731. 0x92, 0x6b, 0x96, 0x50, 0xb6, 0xd3, 0xfa, 0x52
  7732. };
  7733. word64 s1 = 141;
  7734. /* 256 key tests */
  7735. WOLFSSL_SMALL_STACK_STATIC unsigned char k2[] = {
  7736. 0xef, 0x01, 0x0c, 0xa1, 0xa3, 0x66, 0x3e, 0x32,
  7737. 0x53, 0x43, 0x49, 0xbc, 0x0b, 0xae, 0x62, 0x23,
  7738. 0x2a, 0x15, 0x73, 0x34, 0x85, 0x68, 0xfb, 0x9e,
  7739. 0xf4, 0x17, 0x68, 0xa7, 0x67, 0x4f, 0x50, 0x7a,
  7740. 0x72, 0x7f, 0x98, 0x75, 0x53, 0x97, 0xd0, 0xe0,
  7741. 0xaa, 0x32, 0xf8, 0x30, 0x33, 0x8c, 0xc7, 0xa9,
  7742. 0x26, 0xc7, 0x73, 0xf0, 0x9e, 0x57, 0xb3, 0x57,
  7743. 0xcd, 0x15, 0x6a, 0xfb, 0xca, 0x46, 0xe1, 0xa0
  7744. };
  7745. WOLFSSL_SMALL_STACK_STATIC unsigned char p2[] = {
  7746. 0xed, 0x98, 0xe0, 0x17, 0x70, 0xa8, 0x53, 0xb4,
  7747. 0x9d, 0xb9, 0xe6, 0xaa, 0xf8, 0x8f, 0x0a, 0x41,
  7748. 0xb9, 0xb5, 0x6e, 0x91, 0xa5, 0xa2, 0xb1, 0x1d,
  7749. 0x40, 0x52, 0x92, 0x54, 0xf5, 0x52, 0x3e, 0x75
  7750. };
  7751. WOLFSSL_SMALL_STACK_STATIC unsigned char c2[] = {
  7752. 0xca, 0x20, 0xc5, 0x5e, 0x8d, 0xc1, 0x49, 0x68,
  7753. 0x7d, 0x25, 0x41, 0xde, 0x39, 0xc3, 0xdf, 0x63,
  7754. 0x00, 0xbb, 0x5a, 0x16, 0x3c, 0x10, 0xce, 0xd3,
  7755. 0x66, 0x6b, 0x13, 0x57, 0xdb, 0x8b, 0xd3, 0x9d
  7756. };
  7757. word64 s2 = 187;
  7758. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7759. if ((aes = (XtsAes *)XMALLOC(sizeof *aes, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  7760. ERROR_OUT(-5612, out);
  7761. #endif
  7762. XMEMSET(buf, 0, sizeof(buf));
  7763. if (wc_AesXtsSetKey(aes, k1, sizeof(k1), AES_ENCRYPTION,
  7764. HEAP_HINT, devId) != 0)
  7765. ERROR_OUT(-5600, out);
  7766. else
  7767. aes_inited = 1;
  7768. ret = wc_AesXtsEncryptSector(aes, buf, p1, sizeof(p1), s1);
  7769. #if defined(WOLFSSL_ASYNC_CRYPT)
  7770. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7771. #endif
  7772. if (ret != 0)
  7773. ERROR_OUT(-5601, out);
  7774. if (XMEMCMP(c1, buf, AES_BLOCK_SIZE))
  7775. ERROR_OUT(-5602, out);
  7776. wc_AesXtsFree(aes);
  7777. /* decrypt test */
  7778. XMEMSET(buf, 0, sizeof(buf));
  7779. if (wc_AesXtsSetKey(aes, k1, sizeof(k1), AES_DECRYPTION,
  7780. HEAP_HINT, devId) != 0)
  7781. ERROR_OUT(-5603, out);
  7782. ret = wc_AesXtsDecryptSector(aes, buf, c1, sizeof(c1), s1);
  7783. #if defined(WOLFSSL_ASYNC_CRYPT)
  7784. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7785. #endif
  7786. if (ret != 0)
  7787. ERROR_OUT(-5604, out);
  7788. if (XMEMCMP(p1, buf, AES_BLOCK_SIZE))
  7789. ERROR_OUT(-5605, out);
  7790. wc_AesXtsFree(aes);
  7791. /* 256 bit key tests */
  7792. XMEMSET(buf, 0, sizeof(buf));
  7793. if (wc_AesXtsSetKey(aes, k2, sizeof(k2), AES_ENCRYPTION,
  7794. HEAP_HINT, devId) != 0)
  7795. ERROR_OUT(-5606, out);
  7796. ret = wc_AesXtsEncryptSector(aes, buf, p2, sizeof(p2), s2);
  7797. #if defined(WOLFSSL_ASYNC_CRYPT)
  7798. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7799. #endif
  7800. if (ret != 0)
  7801. ERROR_OUT(-5607, out);
  7802. if (XMEMCMP(c2, buf, sizeof(c2)))
  7803. ERROR_OUT(-5608, out);
  7804. wc_AesXtsFree(aes);
  7805. /* decrypt test */
  7806. XMEMSET(buf, 0, sizeof(buf));
  7807. if (wc_AesXtsSetKey(aes, k2, sizeof(k2), AES_DECRYPTION,
  7808. HEAP_HINT, devId) != 0)
  7809. ERROR_OUT(-5609, out);
  7810. ret = wc_AesXtsDecryptSector(aes, buf, c2, sizeof(c2), s2);
  7811. #if defined(WOLFSSL_ASYNC_CRYPT)
  7812. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7813. #endif
  7814. if (ret != 0)
  7815. ERROR_OUT(-5610, out);
  7816. if (XMEMCMP(p2, buf, sizeof(p2)))
  7817. ERROR_OUT(-5611, out);
  7818. out:
  7819. if (aes_inited)
  7820. wc_AesXtsFree(aes);
  7821. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7822. if (aes)
  7823. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_AES);
  7824. #endif
  7825. return ret;
  7826. }
  7827. #endif /* WOLFSSL_AES_128 && WOLFSSL_AES_256 */
  7828. #ifdef WOLFSSL_AES_128
  7829. /* testing of bad arguments */
  7830. static int aes_xts_args_test(void)
  7831. {
  7832. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7833. XtsAes *aes = NULL;
  7834. #else
  7835. XtsAes aes[1];
  7836. #endif
  7837. int aes_inited = 0;
  7838. int ret;
  7839. unsigned char buf[AES_BLOCK_SIZE * 2];
  7840. /* 128 key tests */
  7841. WOLFSSL_SMALL_STACK_STATIC unsigned char k1[] = {
  7842. 0xa3, 0xe4, 0x0d, 0x5b, 0xd4, 0xb6, 0xbb, 0xed,
  7843. 0xb2, 0xd1, 0x8c, 0x70, 0x0a, 0xd2, 0xdb, 0x22,
  7844. 0x10, 0xc8, 0x11, 0x90, 0x64, 0x6d, 0x67, 0x3c,
  7845. 0xbc, 0xa5, 0x3f, 0x13, 0x3e, 0xab, 0x37, 0x3c
  7846. };
  7847. WOLFSSL_SMALL_STACK_STATIC unsigned char p1[] = {
  7848. 0x20, 0xe0, 0x71, 0x94, 0x05, 0x99, 0x3f, 0x09,
  7849. 0xa6, 0x6a, 0xe5, 0xbb, 0x50, 0x0e, 0x56, 0x2c
  7850. };
  7851. WOLFSSL_SMALL_STACK_STATIC unsigned char c1[] = {
  7852. 0x74, 0x62, 0x35, 0x51, 0x21, 0x02, 0x16, 0xac,
  7853. 0x92, 0x6b, 0x96, 0x50, 0xb6, 0xd3, 0xfa, 0x52
  7854. };
  7855. word64 s1 = 141;
  7856. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7857. if ((aes = (XtsAes *)XMALLOC(sizeof *aes, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  7858. ERROR_OUT(-5708, out);
  7859. #endif
  7860. if (wc_AesXtsSetKey(NULL, k1, sizeof(k1), AES_ENCRYPTION,
  7861. HEAP_HINT, devId) == 0)
  7862. ERROR_OUT(-5700, out);
  7863. if (wc_AesXtsSetKey(aes, NULL, sizeof(k1), AES_ENCRYPTION,
  7864. HEAP_HINT, devId) == 0)
  7865. ERROR_OUT(-5701, out);
  7866. /* encryption operations */
  7867. if (wc_AesXtsSetKey(aes, k1, sizeof(k1), AES_ENCRYPTION,
  7868. HEAP_HINT, devId) != 0)
  7869. ERROR_OUT(-5702, out);
  7870. else
  7871. aes_inited = 1;
  7872. ret = wc_AesXtsEncryptSector(NULL, buf, p1, sizeof(p1), s1);
  7873. #if defined(WOLFSSL_ASYNC_CRYPT)
  7874. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7875. #endif
  7876. if (ret == 0)
  7877. ERROR_OUT(-5703, out);
  7878. ret = wc_AesXtsEncryptSector(aes, NULL, p1, sizeof(p1), s1);
  7879. #if defined(WOLFSSL_ASYNC_CRYPT)
  7880. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7881. #endif
  7882. if (ret == 0)
  7883. ERROR_OUT(-5704, out);
  7884. wc_AesXtsFree(aes);
  7885. /* decryption operations */
  7886. if (wc_AesXtsSetKey(aes, k1, sizeof(k1), AES_DECRYPTION,
  7887. HEAP_HINT, devId) != 0)
  7888. ERROR_OUT(-5705, out);
  7889. ret = wc_AesXtsDecryptSector(NULL, buf, c1, sizeof(c1), s1);
  7890. #if defined(WOLFSSL_ASYNC_CRYPT)
  7891. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7892. #endif
  7893. if (ret == 0)
  7894. ERROR_OUT(-5706, out);
  7895. ret = wc_AesXtsDecryptSector(aes, NULL, c1, sizeof(c1), s1);
  7896. #if defined(WOLFSSL_ASYNC_CRYPT)
  7897. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7898. #endif
  7899. if (ret == 0)
  7900. ERROR_OUT(-5707, out);
  7901. ret = 0;
  7902. out:
  7903. if (aes_inited)
  7904. wc_AesXtsFree(aes);
  7905. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7906. if (aes)
  7907. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_AES);
  7908. #endif
  7909. return ret;
  7910. }
  7911. #endif /* WOLFSSL_AES_128 */
  7912. #endif /* WOLFSSL_AES_XTS */
  7913. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  7914. static int aes_cbc_test(void)
  7915. {
  7916. byte cipher[AES_BLOCK_SIZE];
  7917. byte plain[AES_BLOCK_SIZE];
  7918. int ret;
  7919. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = { /* "Now is the time for all " w/o trailing 0 */
  7920. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  7921. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  7922. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  7923. };
  7924. byte key[] = "0123456789abcdef "; /* align */
  7925. byte iv[] = "1234567890abcdef "; /* align */
  7926. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  7927. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  7928. /* Parameter Validation testing. */
  7929. ret = wc_AesCbcEncryptWithKey(cipher, msg, AES_BLOCK_SIZE, key, 17, NULL);
  7930. if (ret != BAD_FUNC_ARG)
  7931. return -5800;
  7932. #ifdef HAVE_AES_DECRYPT
  7933. ret = wc_AesCbcDecryptWithKey(plain, cipher, AES_BLOCK_SIZE, key, 17, NULL);
  7934. if (ret != BAD_FUNC_ARG)
  7935. return -5801;
  7936. #endif
  7937. ret = wc_AesCbcEncryptWithKey(cipher, msg, AES_BLOCK_SIZE, key,
  7938. AES_BLOCK_SIZE, iv);
  7939. if (ret != 0)
  7940. return -5802;
  7941. #ifdef HAVE_AES_DECRYPT
  7942. ret = wc_AesCbcDecryptWithKey(plain, cipher, AES_BLOCK_SIZE, key,
  7943. AES_BLOCK_SIZE, iv);
  7944. if (ret != 0)
  7945. return -5803;
  7946. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE) != 0)
  7947. return -5804;
  7948. #endif /* HAVE_AES_DECRYPT */
  7949. (void)plain;
  7950. return 0;
  7951. }
  7952. #endif
  7953. #if defined(HAVE_AES_ECB) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  7954. static int aesecb_test(void)
  7955. {
  7956. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7957. Aes *enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  7958. #else
  7959. Aes enc[1];
  7960. #endif
  7961. byte cipher[AES_BLOCK_SIZE * 4];
  7962. #ifdef HAVE_AES_DECRYPT
  7963. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7964. Aes *dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  7965. #else
  7966. Aes dec[1];
  7967. #endif
  7968. byte plain [AES_BLOCK_SIZE * 4];
  7969. #endif /* HAVE_AES_DECRYPT */
  7970. int ret = 0;
  7971. #if defined(WOLFSSL_AES_256)
  7972. {
  7973. WOLFSSL_SMALL_STACK_STATIC const byte niPlain[] =
  7974. {
  7975. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  7976. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  7977. };
  7978. WOLFSSL_SMALL_STACK_STATIC const byte niCipher[] =
  7979. {
  7980. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  7981. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  7982. };
  7983. WOLFSSL_SMALL_STACK_STATIC const byte niKey[] =
  7984. {
  7985. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  7986. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  7987. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  7988. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  7989. };
  7990. if (wc_AesInit(enc, HEAP_HINT, devId) != 0)
  7991. ERROR_OUT(-5900, out);
  7992. #if defined(HAVE_AES_DECRYPT)
  7993. if (wc_AesInit(dec, HEAP_HINT, devId) != 0)
  7994. ERROR_OUT(-5901, out);
  7995. #endif
  7996. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  7997. ret = wc_AesSetKey(enc, niKey, sizeof(niKey), cipher, AES_ENCRYPTION);
  7998. if (ret != 0)
  7999. ERROR_OUT(-5923, out);
  8000. if (wc_AesEcbEncrypt(enc, cipher, niPlain, AES_BLOCK_SIZE) != 0)
  8001. ERROR_OUT(-5924, out);
  8002. if (XMEMCMP(cipher, niCipher, AES_BLOCK_SIZE) != 0)
  8003. ERROR_OUT(-5925, out);
  8004. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  8005. ret = wc_AesSetKey(dec, niKey, sizeof(niKey), plain, AES_DECRYPTION);
  8006. if (ret != 0)
  8007. ERROR_OUT(-5926, out);
  8008. if (wc_AesEcbDecrypt(dec, plain, niCipher, AES_BLOCK_SIZE) != 0)
  8009. ERROR_OUT(-5927, out);
  8010. wc_AesEcbDecrypt(dec, plain, niCipher, AES_BLOCK_SIZE);
  8011. if (XMEMCMP(plain, niPlain, AES_BLOCK_SIZE) != 0)
  8012. ERROR_OUT(-5928, out);
  8013. }
  8014. wc_AesFree(enc);
  8015. #ifdef HAVE_AES_DECRYPT
  8016. wc_AesFree(dec);
  8017. #endif
  8018. out:
  8019. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8020. if (enc)
  8021. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  8022. #ifdef HAVE_AES_DECRYPT
  8023. if (dec)
  8024. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  8025. #endif
  8026. #endif
  8027. #endif /* WOLFSSL_AES_256 */
  8028. return ret;
  8029. }
  8030. #endif /* HAVE_AES_ECB */
  8031. #ifdef WOLFSSL_AES_COUNTER
  8032. static int aesctr_test(Aes* enc, Aes* dec, byte* cipher, byte* plain)
  8033. {
  8034. int ret;
  8035. /* test vectors from "Recommendation for Block Cipher Modes of
  8036. * Operation" NIST Special Publication 800-38A */
  8037. WOLFSSL_SMALL_STACK_STATIC const byte ctrIv[] =
  8038. {
  8039. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  8040. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  8041. };
  8042. WOLFSSL_SMALL_STACK_STATIC const byte ctrPlain[] =
  8043. {
  8044. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  8045. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  8046. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  8047. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  8048. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  8049. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  8050. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  8051. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  8052. };
  8053. #ifdef WOLFSSL_ARMASM
  8054. WOLFSSL_SMALL_STACK_STATIC const byte ctrIvWrap32[] =
  8055. {
  8056. 0xff,0xff,0xff,0xff,0x0f,0xff,0xff,0xff,
  8057. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff
  8058. };
  8059. WOLFSSL_SMALL_STACK_STATIC const byte ctrIvWrap32_2[] =
  8060. {
  8061. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  8062. 0xff,0xff,0xff,0xff,0x0f,0xff,0xff,0xfe
  8063. };
  8064. WOLFSSL_SMALL_STACK_STATIC const byte ctrIvWrap64[] =
  8065. {
  8066. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  8067. 0x0f,0xff,0xff,0xff,0xff,0xff,0xff,0xff
  8068. };
  8069. WOLFSSL_SMALL_STACK_STATIC const byte ctrIvWrap64_2[] =
  8070. {
  8071. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xf0,
  8072. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfe
  8073. };
  8074. WOLFSSL_SMALL_STACK_STATIC const byte ctrIvWrap96[] =
  8075. {
  8076. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  8077. 0xff,0xff,0xff,0xff,0x0f,0xff,0xff,0xff
  8078. };
  8079. WOLFSSL_SMALL_STACK_STATIC const byte ctrIvWrap96_2[] =
  8080. {
  8081. 0xff,0xff,0xff,0xf0,0xff,0xff,0xff,0xff,
  8082. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfe
  8083. };
  8084. #endif
  8085. WOLFSSL_SMALL_STACK_STATIC const byte ctrIvWrap128[] =
  8086. {
  8087. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  8088. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff
  8089. };
  8090. #ifdef WOLFSSL_ARMASM
  8091. WOLFSSL_SMALL_STACK_STATIC const byte ctrIvWrap128_2[] =
  8092. {
  8093. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  8094. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfe
  8095. };
  8096. #endif
  8097. #ifdef WOLFSSL_AES_128
  8098. WOLFSSL_SMALL_STACK_STATIC const byte oddCipher[] =
  8099. {
  8100. 0xb9,0xd7,0xcb,0x08,0xb0,0xe1,0x7b,0xa0,
  8101. 0xc2
  8102. };
  8103. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Key[] =
  8104. {
  8105. 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,
  8106. 0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c
  8107. };
  8108. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Cipher[] =
  8109. {
  8110. 0x87,0x4d,0x61,0x91,0xb6,0x20,0xe3,0x26,
  8111. 0x1b,0xef,0x68,0x64,0x99,0x0d,0xb6,0xce,
  8112. 0x98,0x06,0xf6,0x6b,0x79,0x70,0xfd,0xff,
  8113. 0x86,0x17,0x18,0x7b,0xb9,0xff,0xfd,0xff,
  8114. 0x5a,0xe4,0xdf,0x3e,0xdb,0xd5,0xd3,0x5e,
  8115. 0x5b,0x4f,0x09,0x02,0x0d,0xb0,0x3e,0xab,
  8116. 0x1e,0x03,0x1d,0xda,0x2f,0xbe,0x03,0xd1,
  8117. 0x79,0x21,0x70,0xa0,0xf3,0x00,0x9c,0xee
  8118. };
  8119. #ifdef WOLFSSL_ARMASM
  8120. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap32Cipher[] =
  8121. {
  8122. 0xb3,0x8b,0x58,0xbc,0xce,0xf4,0x71,0x78,
  8123. 0xf6,0x7c,0xdb,0xb4,0x27,0x2b,0x0a,0xbf,
  8124. 0x7e,0xad,0xea,0x5c,0xd1
  8125. };
  8126. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap32CipherLong[] =
  8127. {
  8128. 0xb3,0x8b,0x58,0xbc,0xce,0xf4,0x71,0x78,
  8129. 0xf6,0x7c,0xdb,0xb4,0x27,0x2b,0x0a,0xbf,
  8130. 0x7e,0xad,0xea,0x5c,0xd1,0xb7,0x98,0xf0,
  8131. 0x22,0x20,0xfe,0x67,0xb0,0x02,0x23,0x50
  8132. };
  8133. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap32_2CipherLong[] =
  8134. {
  8135. 0x6e,0xa1,0x27,0x4d,0xea,0x20,0x5f,0x39,
  8136. 0x68,0xc8,0xb6,0x78,0xde,0xfc,0x53,0x5c,
  8137. 0x90,0xc8,0xf6,0xc6,0xfa,0xe0,0x7b,0x09,
  8138. 0x7c,0xf8,0x9c,0x6a,0x5a,0xa5,0x17,0x7f,
  8139. 0x03,0x92,0x5f,0x4e,0x85,0xea,0x26,0xc9,
  8140. 0x5a,0xc2,0x74,0xe2,0xbf,0xe4,0x1b,0xd4
  8141. };
  8142. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap64Cipher[] =
  8143. {
  8144. 0xdd,0x17,0x10,0x7c,0x45,0x04,0xac,0x43,
  8145. 0xef,0xa8,0xcc,0x32,0x34,0x87,0x88,0xd7,
  8146. 0xae,0x74,0x94,0x72,0x8e
  8147. };
  8148. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap64CipherLong[] =
  8149. {
  8150. 0xdd,0x17,0x10,0x7c,0x45,0x04,0xac,0x43,
  8151. 0xef,0xa8,0xcc,0x32,0x34,0x87,0x88,0xd7,
  8152. 0xae,0x74,0x94,0x72,0x8e,0xd0,0x71,0xc0,
  8153. 0x89,0x8a,0xa1,0xb0,0x29,0xa0,0x10,0x9e
  8154. };
  8155. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap64_2CipherLong[] =
  8156. {
  8157. 0x3f,0xe7,0xd5,0xf3,0xfa,0x09,0xfe,0x40,
  8158. 0xa6,0xa1,0x32,0x8b,0x57,0x12,0xb9,0xfa,
  8159. 0xf2,0x2d,0xe4,0x3c,0x66,0x1d,0x0a,0x8e,
  8160. 0x46,0xf8,0x2e,0x33,0xce,0x8d,0x4e,0x3b,
  8161. 0x17,0x67,0x9e,0x9f,0x76,0x9e,0xc2,0x99,
  8162. 0xd5,0xd4,0x71,0xed,0xb4,0x33,0xb2,0xcd
  8163. };
  8164. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap96Cipher[] =
  8165. {
  8166. 0x55,0x24,0xc2,0x73,0xca,0xa3,0x48,0x03,
  8167. 0x0b,0x72,0x8d,0xd7,0x6c,0x99,0x8e,0x04,
  8168. 0x9d,0x77,0xc9,0x5f,0x38
  8169. };
  8170. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap96CipherLong[] =
  8171. {
  8172. 0x55,0x24,0xc2,0x73,0xca,0xa3,0x48,0x03,
  8173. 0x0b,0x72,0x8d,0xd7,0x6c,0x99,0x8e,0x04,
  8174. 0x9d,0x77,0xc9,0x5f,0x38,0xb5,0x6e,0x44,
  8175. 0x21,0x8e,0xda,0x57,0xe0,0x41,0xc7,0x6a
  8176. };
  8177. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap96_2CipherLong[] =
  8178. {
  8179. 0xc8,0x81,0x1a,0xbe,0xc7,0x5b,0x93,0x6f,
  8180. 0xe6,0x52,0xe4,0xb1,0x2d,0x1c,0x39,0xbc,
  8181. 0xeb,0x82,0x27,0x0a,0x7e,0xa5,0x0e,0x2d,
  8182. 0x32,0xda,0xbe,0x10,0x7a,0x10,0xcc,0xd3,
  8183. 0x6f,0xc6,0x83,0x28,0x05,0x57,0x8a,0x24,
  8184. 0x44,0x76,0x17,0x81,0xb9,0x5c,0x94,0x81
  8185. };
  8186. #endif
  8187. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap128Cipher[] =
  8188. {
  8189. 0xe1,0x33,0x38,0xe3,0x6c,0xb7,0x19,0x62,
  8190. 0xe0,0x0d,0x02,0x0b,0x4c,0xed,0xbd,0x86,
  8191. 0xd3,0xda,0xe1,0x5b,0x04
  8192. };
  8193. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap128CipherLong[] =
  8194. {
  8195. 0xe1,0x33,0x38,0xe3,0x6c,0xb7,0x19,0x62,
  8196. 0xe0,0x0d,0x02,0x0b,0x4c,0xed,0xbd,0x86,
  8197. 0xd3,0xda,0xe1,0x5b,0x04,0xbb,0x35,0x2f,
  8198. 0xa0,0xf5,0x9f,0xeb,0xfc,0xb4,0xda,0x3e
  8199. };
  8200. #ifdef WOLFSSL_ARMASM
  8201. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap128_2CipherLong[] =
  8202. {
  8203. 0xba,0x76,0xaa,0x54,0xd5,0xb5,0x60,0x67,
  8204. 0xc1,0xa7,0x90,0x3b,0x3f,0xdd,0xfa,0x89,
  8205. 0x24,0xdf,0x0c,0x56,0x5c,0xf4,0x2a,0x68,
  8206. 0x97,0x87,0x13,0xb6,0x7a,0xd1,0x24,0xfd,
  8207. 0x4d,0x3f,0x77,0x4a,0xb9,0xe4,0x7d,0xa2,
  8208. 0xdb,0xb9,0x31,0x5e,0xa3,0x11,0x06,0x80
  8209. };
  8210. #endif
  8211. #endif /* WOLFSSL_AES_128 */
  8212. #ifdef WOLFSSL_AES_192
  8213. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Key[] =
  8214. {
  8215. 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  8216. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  8217. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b
  8218. };
  8219. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Cipher[] =
  8220. {
  8221. 0x1a,0xbc,0x93,0x24,0x17,0x52,0x1c,0xa2,
  8222. 0x4f,0x2b,0x04,0x59,0xfe,0x7e,0x6e,0x0b,
  8223. 0x09,0x03,0x39,0xec,0x0a,0xa6,0xfa,0xef,
  8224. 0xd5,0xcc,0xc2,0xc6,0xf4,0xce,0x8e,0x94,
  8225. 0x1e,0x36,0xb2,0x6b,0xd1,0xeb,0xc6,0x70,
  8226. 0xd1,0xbd,0x1d,0x66,0x56,0x20,0xab,0xf7,
  8227. 0x4f,0x78,0xa7,0xf6,0xd2,0x98,0x09,0x58,
  8228. 0x5a,0x97,0xda,0xec,0x58,0xc6,0xb0,0x50
  8229. };
  8230. #ifdef WOLFSSL_ARMASM
  8231. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap32Cipher[] =
  8232. {
  8233. 0x28,0xaa,0xfa,0x90,0x72,0x74,0x86,0xaf,
  8234. 0x72,0x73,0x35,0x17,0x70,0x4e,0x7d,0xca,
  8235. 0x0c,0x33,0x97,0x06,0xc0
  8236. };
  8237. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap32CipherLong[] =
  8238. {
  8239. 0x28,0xaa,0xfa,0x90,0x72,0x74,0x86,0xaf,
  8240. 0x72,0x73,0x35,0x17,0x70,0x4e,0x7d,0xca,
  8241. 0x0c,0x33,0x97,0x06,0xc0,0xbe,0x83,0x87,
  8242. 0xdd,0xd3,0xff,0xd8,0xe4,0x6a,0x5b,0x84
  8243. };
  8244. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap32_2CipherLong[] =
  8245. {
  8246. 0xf5,0x00,0xa2,0x91,0x54,0xa3,0x76,0xa2,
  8247. 0xdd,0xad,0x16,0x89,0xe5,0xf0,0x1d,0x40,
  8248. 0x84,0xcd,0x74,0x84,0xcb,0x8b,0x9e,0x29,
  8249. 0xa9,0x8a,0x12,0x65,0xa0,0x79,0x5e,0xce,
  8250. 0xd9,0x50,0x65,0x21,0x86,0xb0,0x85,0x0d,
  8251. 0x98,0x2d,0x9a,0x5a,0x11,0xbe,0xa5,0x7f
  8252. };
  8253. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap64Cipher[] =
  8254. {
  8255. 0xfe,0x39,0x27,0x97,0xac,0xe5,0xb8,0x74,
  8256. 0xb9,0x8c,0xbf,0x58,0x71,0xa4,0x80,0x33,
  8257. 0x3d,0xf7,0xb4,0xfd,0x8c
  8258. };
  8259. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap64CipherLong[] =
  8260. {
  8261. 0xfe,0x39,0x27,0x97,0xac,0xe5,0xb8,0x74,
  8262. 0xb9,0x8c,0xbf,0x58,0x71,0xa4,0x80,0x33,
  8263. 0x3d,0xf7,0xb4,0xfd,0x8c,0x55,0x47,0x10,
  8264. 0xd5,0x91,0x35,0xbe,0xd8,0x0d,0xa5,0x9e
  8265. };
  8266. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap64_2CipherLong[] =
  8267. {
  8268. 0x59,0xf1,0xed,0x70,0x62,0x42,0xa8,0x06,
  8269. 0x07,0x36,0xe1,0xc5,0x04,0x79,0xc3,0x9b,
  8270. 0xd1,0x14,0x5c,0xcc,0x6f,0x81,0x5f,0x2f,
  8271. 0xa0,0xde,0xcf,0x61,0x55,0x18,0x7a,0xac,
  8272. 0xb0,0x59,0x37,0x90,0x53,0xb3,0x00,0x88,
  8273. 0xb4,0x49,0x90,0x7b,0x96,0xcd,0xcc,0xc3
  8274. };
  8275. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap96Cipher[] =
  8276. {
  8277. 0x41,0x21,0x40,0x31,0xfb,0xc8,0xad,0x23,
  8278. 0xde,0x00,0x03,0xd8,0x96,0x45,0xc7,0xb5,
  8279. 0x47,0xb5,0xf3,0x30,0x3b
  8280. };
  8281. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap96CipherLong[] =
  8282. {
  8283. 0x41,0x21,0x40,0x31,0xfb,0xc8,0xad,0x23,
  8284. 0xde,0x00,0x03,0xd8,0x96,0x45,0xc7,0xb5,
  8285. 0x47,0xb5,0xf3,0x30,0x3b,0xef,0xcd,0x80,
  8286. 0xe3,0x61,0x34,0xef,0x4e,0x1b,0x79,0xc1
  8287. };
  8288. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap96_2CipherLong[] =
  8289. {
  8290. 0x3c,0xb2,0xff,0xc0,0x24,0xe1,0xf5,0xc4,
  8291. 0x0f,0xd1,0x0a,0x1b,0xbe,0x1f,0x23,0xa1,
  8292. 0x8e,0xbf,0x2b,0x96,0xb6,0x37,0xc8,0x25,
  8293. 0x06,0x90,0xe2,0xca,0x71,0x24,0x52,0x95,
  8294. 0xaa,0x8c,0x80,0xdf,0xb7,0xd7,0x30,0xb0,
  8295. 0xcc,0x06,0x4f,0x28,0xa2,0x74,0x27,0xf8
  8296. };
  8297. #endif
  8298. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap128Cipher[] =
  8299. {
  8300. 0x5c,0xc3,0x8f,0xab,0x30,0xb6,0xac,0x67,
  8301. 0xdc,0xc2,0x1e,0x7b,0x01,0x2e,0xcf,0x98,
  8302. 0x8c,0x68,0xa7,0xd9,0x57
  8303. };
  8304. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap128CipherLong[] =
  8305. {
  8306. 0x5c,0xc3,0x8f,0xab,0x30,0xb6,0xac,0x67,
  8307. 0xdc,0xc2,0x1e,0x7b,0x01,0x2e,0xcf,0x98,
  8308. 0x8c,0x68,0xa7,0xd9,0x57,0xab,0x09,0x0f,
  8309. 0x01,0xc4,0x4e,0x62,0xaf,0xc2,0xdf,0x1a
  8310. };
  8311. #ifdef WOLFSSL_ARMASM
  8312. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap128_2CipherLong[] =
  8313. {
  8314. 0x88,0x0a,0x26,0x4e,0xa8,0x26,0x21,0xe0,
  8315. 0xfc,0xbc,0x63,0xdc,0xd9,0x60,0x52,0xb2,
  8316. 0x99,0x2f,0xbb,0x1e,0x00,0xf5,0x9f,0x6d,
  8317. 0xab,0x48,0x0f,0xc6,0x37,0x12,0x56,0xe3,
  8318. 0x12,0x8d,0x31,0xc8,0xea,0xf4,0x41,0x82,
  8319. 0x7a,0x88,0xe0,0xd7,0xf0,0x67,0x03,0xa4
  8320. };
  8321. #endif
  8322. #endif
  8323. #ifdef WOLFSSL_AES_256
  8324. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Key[] =
  8325. {
  8326. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  8327. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  8328. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  8329. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  8330. };
  8331. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Cipher[] =
  8332. {
  8333. 0x60,0x1e,0xc3,0x13,0x77,0x57,0x89,0xa5,
  8334. 0xb7,0xa7,0xf5,0x04,0xbb,0xf3,0xd2,0x28,
  8335. 0xf4,0x43,0xe3,0xca,0x4d,0x62,0xb5,0x9a,
  8336. 0xca,0x84,0xe9,0x90,0xca,0xca,0xf5,0xc5,
  8337. 0x2b,0x09,0x30,0xda,0xa2,0x3d,0xe9,0x4c,
  8338. 0xe8,0x70,0x17,0xba,0x2d,0x84,0x98,0x8d,
  8339. 0xdf,0xc9,0xc5,0x8d,0xb6,0x7a,0xad,0xa6,
  8340. 0x13,0xc2,0xdd,0x08,0x45,0x79,0x41,0xa6
  8341. };
  8342. #ifdef WOLFSSL_ARMASM
  8343. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap32Cipher[] =
  8344. {
  8345. 0xb0,0xa8,0xc0,0x65,0x85,0x20,0x0d,0x5c,
  8346. 0x25,0xcf,0xe7,0x58,0x63,0xc8,0xd4,0xea,
  8347. 0xa2,0x13,0x47,0x74,0xda
  8348. };
  8349. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap32CipherLong[] =
  8350. {
  8351. 0xb0,0xa8,0xc0,0x65,0x85,0x20,0x0d,0x5c,
  8352. 0x25,0xcf,0xe7,0x58,0x63,0xc8,0xd4,0xea,
  8353. 0xa2,0x13,0x47,0x74,0xda,0x89,0x77,0x40,
  8354. 0x28,0x9c,0xe8,0x19,0x26,0x32,0xd8,0x1f
  8355. };
  8356. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap32_2CipherLong[] =
  8357. {
  8358. 0xf6,0xd9,0x22,0xc6,0x80,0x29,0xaf,0x14,
  8359. 0x54,0x6c,0x0a,0xce,0x42,0xea,0x3c,0xa1,
  8360. 0x7c,0xeb,0x36,0x0d,0x8e,0xd7,0x8c,0x59,
  8361. 0xa8,0x09,0x9f,0x9e,0xba,0x5b,0x95,0xfa,
  8362. 0x26,0x8c,0x37,0x59,0xf8,0xae,0x8e,0xaa,
  8363. 0x4d,0xe4,0x1c,0xfe,0x51,0xc7,0xb7,0xcc
  8364. };
  8365. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap64Cipher[] =
  8366. {
  8367. 0x74,0x1a,0x52,0x41,0x76,0xb4,0x11,0x8f,
  8368. 0xfd,0x57,0x31,0xfd,0x3d,0x76,0x8f,0x07,
  8369. 0xd4,0x94,0x4c,0xcd,0x4d
  8370. };
  8371. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap64CipherLong[] =
  8372. {
  8373. 0x74,0x1a,0x52,0x41,0x76,0xb4,0x11,0x8f,
  8374. 0xfd,0x57,0x31,0xfd,0x3d,0x76,0x8f,0x07,
  8375. 0xd4,0x94,0x4c,0xcd,0x4d,0x47,0x5a,0x92,
  8376. 0x26,0x49,0x81,0x7a,0xda,0x36,0x27,0x01
  8377. };
  8378. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap64_2CipherLong[] =
  8379. {
  8380. 0xf7,0x9c,0xbf,0xf6,0xa2,0xaa,0x8a,0x0a,
  8381. 0x63,0x8a,0x20,0x2f,0x12,0xf1,0x8e,0x49,
  8382. 0x30,0xc0,0x8d,0x5c,0x5f,0x8b,0xbc,0x16,
  8383. 0xdd,0x71,0xee,0x13,0x14,0x7b,0xe1,0x25,
  8384. 0xcb,0x87,0x8a,0xc6,0xdc,0x1d,0x54,0x7a,
  8385. 0xe1,0xe4,0x6f,0x0d,0x95,0x1b,0xd1,0x8b
  8386. };
  8387. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap96Cipher[] =
  8388. {
  8389. 0xb9,0x07,0x02,0xb8,0xbe,0x94,0xbf,0x53,
  8390. 0xdf,0x83,0x8e,0x23,0x8c,0x67,0x0c,0x81,
  8391. 0xb8,0x69,0xa1,0x48,0x45
  8392. };
  8393. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap96CipherLong[] =
  8394. {
  8395. 0xb9,0x07,0x02,0xb8,0xbe,0x94,0xbf,0x53,
  8396. 0xdf,0x83,0x8e,0x23,0x8c,0x67,0x0c,0x81,
  8397. 0xb8,0x69,0xa1,0x48,0x45,0xf1,0xc6,0x27,
  8398. 0x36,0xa8,0xb2,0x4b,0x0e,0x62,0x6b,0x72
  8399. };
  8400. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap96_2CipherLong[] =
  8401. {
  8402. 0xd5,0x56,0x73,0xaa,0xb8,0xe4,0x06,0xf6,
  8403. 0x83,0x45,0x3a,0xb4,0xb9,0x63,0xec,0xad,
  8404. 0x73,0xc5,0xab,0x78,0xb1,0x21,0xab,0xef,
  8405. 0x69,0x15,0xb7,0x0c,0xe9,0xb4,0x3a,0xe7,
  8406. 0xbc,0xc4,0x22,0xbd,0x93,0xba,0x52,0xe0,
  8407. 0x91,0x2f,0x5e,0x8d,0x6d,0x59,0xf7,0xc2
  8408. };
  8409. #endif
  8410. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap128Cipher[] =
  8411. {
  8412. 0x50,0xfd,0x97,0xc3,0xe6,0x1a,0xbb,0x48,
  8413. 0x73,0xfb,0x78,0xdf,0x1e,0x8e,0x77,0xe6,
  8414. 0x4b,0x45,0x7c,0xd6,0x8a
  8415. };
  8416. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap128CipherLong[] =
  8417. {
  8418. 0x50,0xfd,0x97,0xc3,0xe6,0x1a,0xbb,0x48,
  8419. 0x73,0xfb,0x78,0xdf,0x1e,0x8e,0x77,0xe6,
  8420. 0x4b,0x45,0x7c,0xd6,0x8a,0xcc,0xda,0x4a,
  8421. 0x89,0xfa,0x23,0x6c,0x06,0xbf,0x26,0x05
  8422. };
  8423. #ifdef WOLFSSL_ARMASM
  8424. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap128_2CipherLong[] =
  8425. {
  8426. 0x24,0x5c,0x09,0xa0,0x3b,0x1a,0x5a,0x94,
  8427. 0x2b,0x93,0x56,0x13,0x48,0xa0,0x21,0xce,
  8428. 0x95,0x11,0xa3,0x76,0xd6,0x59,0x88,0x42,
  8429. 0x04,0x71,0x69,0x62,0x28,0xb2,0xee,0x9d,
  8430. 0xd5,0xa0,0xea,0xc7,0x37,0x93,0x92,0xc7,
  8431. 0xf2,0xb6,0x8d,0xd9,0x59,0x1a,0xfa,0xbb
  8432. };
  8433. #endif
  8434. #endif
  8435. int i;
  8436. struct {
  8437. const byte* key;
  8438. int keySz;
  8439. const byte* iv;
  8440. const byte* plain;
  8441. int len;
  8442. const byte* cipher;
  8443. } testVec[] = {
  8444. #ifdef WOLFSSL_AES_128
  8445. { ctr128Key, (int)sizeof(ctr128Key), ctrIv,
  8446. ctrPlain, (int)sizeof(ctrPlain), ctr128Cipher },
  8447. /* let's try with just 9 bytes, non block size test */
  8448. { ctr128Key, (int)sizeof(ctr128Key), ctrIv,
  8449. ctrPlain, (int)sizeof(oddCipher), ctr128Cipher },
  8450. /* and an additional 9 bytes to reuse tmp left buffer */
  8451. { NULL, 0, NULL, ctrPlain, (int)sizeof(oddCipher), oddCipher },
  8452. /* Counter wrapping */
  8453. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap128,
  8454. ctrPlain, (int)sizeof(ctr128Wrap128Cipher), ctr128Wrap128Cipher },
  8455. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap128,
  8456. ctrPlain, (int)sizeof(ctr128Wrap128CipherLong),
  8457. ctr128Wrap128CipherLong },
  8458. #ifdef WOLFSSL_ARMASM
  8459. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap128_2,
  8460. ctrPlain, (int)sizeof(ctr128Wrap128_2CipherLong),
  8461. ctr128Wrap128_2CipherLong },
  8462. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap96,
  8463. ctrPlain, (int)sizeof(ctr128Wrap96Cipher), ctr128Wrap96Cipher },
  8464. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap96,
  8465. ctrPlain, (int)sizeof(ctr128Wrap96CipherLong),
  8466. ctr128Wrap96CipherLong },
  8467. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap96_2,
  8468. ctrPlain, (int)sizeof(ctr128Wrap96_2CipherLong),
  8469. ctr128Wrap96_2CipherLong },
  8470. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap64,
  8471. ctrPlain, (int)sizeof(ctr128Wrap64Cipher), ctr128Wrap64Cipher },
  8472. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap64,
  8473. ctrPlain, (int)sizeof(ctr128Wrap64CipherLong),
  8474. ctr128Wrap64CipherLong },
  8475. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap64_2,
  8476. ctrPlain, (int)sizeof(ctr128Wrap64_2CipherLong),
  8477. ctr128Wrap64_2CipherLong },
  8478. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap32,
  8479. ctrPlain, (int)sizeof(ctr128Wrap32Cipher), ctr128Wrap32Cipher },
  8480. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap32,
  8481. ctrPlain, (int)sizeof(ctr128Wrap32CipherLong),
  8482. ctr128Wrap32CipherLong },
  8483. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap32_2,
  8484. ctrPlain, (int)sizeof(ctr128Wrap32_2CipherLong),
  8485. ctr128Wrap32_2CipherLong },
  8486. #endif
  8487. #endif
  8488. #ifdef WOLFSSL_AES_192
  8489. { ctr192Key, (int)sizeof(ctr192Key), ctrIv,
  8490. ctrPlain, (int)sizeof(ctrPlain), ctr192Cipher },
  8491. /* let's try with just 9 bytes, non block size test */
  8492. { ctr192Key, (int)sizeof(ctr192Key), ctrIv,
  8493. ctrPlain, (int)sizeof(oddCipher), ctr192Cipher },
  8494. /* Counter wrapping */
  8495. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap128,
  8496. ctrPlain, (int)sizeof(ctr192Wrap128Cipher), ctr192Wrap128Cipher },
  8497. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap128,
  8498. ctrPlain, (int)sizeof(ctr192Wrap128CipherLong),
  8499. ctr192Wrap128CipherLong },
  8500. #ifdef WOLFSSL_ARMASM
  8501. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap128_2,
  8502. ctrPlain, (int)sizeof(ctr192Wrap128_2CipherLong),
  8503. ctr192Wrap128_2CipherLong },
  8504. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap96,
  8505. ctrPlain, (int)sizeof(ctr192Wrap96Cipher), ctr192Wrap96Cipher },
  8506. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap96,
  8507. ctrPlain, (int)sizeof(ctr192Wrap96CipherLong),
  8508. ctr192Wrap96CipherLong },
  8509. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap96_2,
  8510. ctrPlain, (int)sizeof(ctr192Wrap96_2CipherLong),
  8511. ctr192Wrap96_2CipherLong },
  8512. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap64,
  8513. ctrPlain, (int)sizeof(ctr192Wrap64Cipher), ctr192Wrap64Cipher },
  8514. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap64,
  8515. ctrPlain, (int)sizeof(ctr192Wrap64CipherLong),
  8516. ctr192Wrap64CipherLong },
  8517. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap64_2,
  8518. ctrPlain, (int)sizeof(ctr192Wrap64_2CipherLong),
  8519. ctr192Wrap64_2CipherLong },
  8520. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap32,
  8521. ctrPlain, (int)sizeof(ctr192Wrap32Cipher), ctr192Wrap32Cipher },
  8522. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap32,
  8523. ctrPlain, (int)sizeof(ctr192Wrap32CipherLong),
  8524. ctr192Wrap32CipherLong },
  8525. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap32_2,
  8526. ctrPlain, (int)sizeof(ctr192Wrap32_2CipherLong),
  8527. ctr192Wrap32_2CipherLong },
  8528. #endif
  8529. #endif
  8530. #ifdef WOLFSSL_AES_256
  8531. { ctr256Key, (int)sizeof(ctr256Key), ctrIv,
  8532. ctrPlain, (int)sizeof(ctrPlain), ctr256Cipher },
  8533. /* let's try with just 9 bytes, non block size test */
  8534. { ctr256Key, (int)sizeof(ctr256Key), ctrIv,
  8535. ctrPlain, (int)sizeof(oddCipher), ctr256Cipher },
  8536. /* Counter wrapping */
  8537. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap128,
  8538. ctrPlain, (int)sizeof(ctr256Wrap128Cipher), ctr256Wrap128Cipher },
  8539. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap128,
  8540. ctrPlain, (int)sizeof(ctr256Wrap128CipherLong),
  8541. ctr256Wrap128CipherLong },
  8542. #ifdef WOLFSSL_ARMASM
  8543. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap128_2,
  8544. ctrPlain, (int)sizeof(ctr256Wrap128_2CipherLong),
  8545. ctr256Wrap128_2CipherLong },
  8546. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap96,
  8547. ctrPlain, (int)sizeof(ctr256Wrap96Cipher), ctr256Wrap96Cipher },
  8548. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap96,
  8549. ctrPlain, (int)sizeof(ctr256Wrap96CipherLong),
  8550. ctr256Wrap96CipherLong },
  8551. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap96_2,
  8552. ctrPlain, (int)sizeof(ctr256Wrap96_2CipherLong),
  8553. ctr256Wrap96_2CipherLong },
  8554. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap64,
  8555. ctrPlain, (int)sizeof(ctr256Wrap64Cipher), ctr256Wrap64Cipher },
  8556. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap64,
  8557. ctrPlain, (int)sizeof(ctr256Wrap64CipherLong),
  8558. ctr256Wrap64CipherLong },
  8559. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap64_2,
  8560. ctrPlain, (int)sizeof(ctr256Wrap64_2CipherLong),
  8561. ctr256Wrap64_2CipherLong },
  8562. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap32,
  8563. ctrPlain, (int)sizeof(ctr256Wrap32Cipher), ctr256Wrap32Cipher },
  8564. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap32,
  8565. ctrPlain, (int)sizeof(ctr256Wrap32CipherLong),
  8566. ctr256Wrap32CipherLong },
  8567. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap32_2,
  8568. ctrPlain, (int)sizeof(ctr256Wrap32_2CipherLong),
  8569. ctr256Wrap32_2CipherLong },
  8570. #endif
  8571. #endif
  8572. };
  8573. #define AES_CTR_TEST_LEN (int)(sizeof(testVec) / sizeof(*testVec))
  8574. for (i = 0; i < AES_CTR_TEST_LEN; i++) {
  8575. if (testVec[i].key != NULL) {
  8576. ret = wc_AesSetKeyDirect(enc, testVec[i].key, testVec[i].keySz,
  8577. testVec[i].iv, AES_ENCRYPTION);
  8578. if (ret != 0) {
  8579. ERROR_OUT(-5930 - i * 10, out);
  8580. }
  8581. /* Ctr only uses encrypt, even on key setup */
  8582. ret = wc_AesSetKeyDirect(dec, testVec[i].key, testVec[i].keySz,
  8583. testVec[i].iv, AES_ENCRYPTION);
  8584. if (ret != 0) {
  8585. ERROR_OUT(-5931 - i * 10, out);
  8586. }
  8587. }
  8588. ret = wc_AesCtrEncrypt(enc, cipher, testVec[i].plain, testVec[i].len);
  8589. if (ret != 0) {
  8590. ERROR_OUT(-5932 - i * 10, out);
  8591. }
  8592. ret = wc_AesCtrEncrypt(dec, plain, cipher, testVec[i].len);
  8593. if (ret != 0) {
  8594. ERROR_OUT(-5933 - i * 10, out);
  8595. }
  8596. if (XMEMCMP(plain, ctrPlain, testVec[i].len)) {
  8597. ERROR_OUT(-5934 - i * 10, out);
  8598. }
  8599. #if !(FIPS_VERSION_EQ(2,0) && defined(WOLFSSL_ARMASM))
  8600. if (XMEMCMP(cipher, testVec[i].cipher, testVec[i].len)) {
  8601. ERROR_OUT(-5935 - i * 10, out);
  8602. }
  8603. #endif
  8604. }
  8605. out:
  8606. return ret;
  8607. }
  8608. #endif /* WOLFSSL_AES_COUNTER */
  8609. WOLFSSL_TEST_SUBROUTINE int aes_test(void)
  8610. {
  8611. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_COUNTER) || defined(WOLFSSL_AES_DIRECT)
  8612. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8613. Aes *enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  8614. #else
  8615. Aes enc[1];
  8616. #endif
  8617. byte cipher[AES_BLOCK_SIZE * 4];
  8618. #ifdef HAVE_AES_DECRYPT
  8619. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8620. Aes *dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  8621. #else
  8622. Aes dec[1];
  8623. #endif
  8624. byte plain [AES_BLOCK_SIZE * 4];
  8625. #endif /* HAVE_AES_DECRYPT */
  8626. #endif /* HAVE_AES_CBC || WOLFSSL_AES_COUNTER || WOLFSSL_AES_DIRECT */
  8627. int ret = 0;
  8628. #ifdef HAVE_AES_CBC
  8629. #ifdef WOLFSSL_AES_128
  8630. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = { /* "Now is the time for all " w/o trailing 0 */
  8631. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  8632. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  8633. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  8634. };
  8635. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  8636. {
  8637. 0x95,0x94,0x92,0x57,0x5f,0x42,0x81,0x53,
  8638. 0x2c,0xcc,0x9d,0x46,0x77,0xa2,0x33,0xcb
  8639. };
  8640. WOLFSSL_SMALL_STACK_STATIC const byte key[] = "0123456789abcdef "; /* align */
  8641. WOLFSSL_SMALL_STACK_STATIC const byte iv[] = "1234567890abcdef "; /* align */
  8642. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8643. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_COUNTER) || defined(WOLFSSL_AES_DIRECT)
  8644. if (enc == NULL)
  8645. ERROR_OUT(-5990, out);
  8646. #endif
  8647. #if defined(HAVE_AES_DECRYPT) || defined(WOLFSSL_AES_COUNTER) || defined(WOLFSSL_AES_DIRECT)
  8648. if (dec == NULL)
  8649. ERROR_OUT(-5991, out);
  8650. #endif
  8651. #endif
  8652. if (wc_AesInit(enc, HEAP_HINT, devId) != 0)
  8653. ERROR_OUT(-5900, out); /* note this error code is used programmatically in cleanup. */
  8654. #if defined(HAVE_AES_DECRYPT) || defined(WOLFSSL_AES_COUNTER)
  8655. if (wc_AesInit(dec, HEAP_HINT, devId) != 0)
  8656. ERROR_OUT(-5901, out); /* note this error code is used programmatically in cleanup. */
  8657. #endif
  8658. ret = wc_AesSetKey(enc, key, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  8659. if (ret != 0)
  8660. ERROR_OUT(-5902, out);
  8661. #if defined(HAVE_AES_DECRYPT) || defined(WOLFSSL_AES_COUNTER)
  8662. ret = wc_AesSetKey(dec, key, AES_BLOCK_SIZE, iv, AES_DECRYPTION);
  8663. if (ret != 0)
  8664. ERROR_OUT(-5903, out);
  8665. #endif
  8666. XMEMSET(cipher, 0, AES_BLOCK_SIZE * 4);
  8667. ret = wc_AesCbcEncrypt(enc, cipher, msg, AES_BLOCK_SIZE);
  8668. #if defined(WOLFSSL_ASYNC_CRYPT)
  8669. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  8670. #endif
  8671. if (ret != 0)
  8672. ERROR_OUT(-5904, out);
  8673. #ifdef HAVE_AES_DECRYPT
  8674. XMEMSET(plain, 0, AES_BLOCK_SIZE * 4);
  8675. ret = wc_AesCbcDecrypt(dec, plain, cipher, AES_BLOCK_SIZE);
  8676. #if defined(WOLFSSL_ASYNC_CRYPT)
  8677. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  8678. #endif
  8679. if (ret != 0)
  8680. ERROR_OUT(-5905, out);
  8681. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  8682. ERROR_OUT(-5906, out);
  8683. #endif /* HAVE_AES_DECRYPT */
  8684. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE))
  8685. ERROR_OUT(-5907, out);
  8686. #endif /* WOLFSSL_AES_128 */
  8687. #if defined(WOLFSSL_AESNI) && defined(HAVE_AES_DECRYPT)
  8688. {
  8689. WOLFSSL_SMALL_STACK_STATIC const byte bigMsg[] = {
  8690. /* "All work and no play makes Jack a dull boy. " */
  8691. 0x41,0x6c,0x6c,0x20,0x77,0x6f,0x72,0x6b,
  8692. 0x20,0x61,0x6e,0x64,0x20,0x6e,0x6f,0x20,
  8693. 0x70,0x6c,0x61,0x79,0x20,0x6d,0x61,0x6b,
  8694. 0x65,0x73,0x20,0x4a,0x61,0x63,0x6b,0x20,
  8695. 0x61,0x20,0x64,0x75,0x6c,0x6c,0x20,0x62,
  8696. 0x6f,0x79,0x2e,0x20,0x41,0x6c,0x6c,0x20,
  8697. 0x77,0x6f,0x72,0x6b,0x20,0x61,0x6e,0x64,
  8698. 0x20,0x6e,0x6f,0x20,0x70,0x6c,0x61,0x79,
  8699. 0x20,0x6d,0x61,0x6b,0x65,0x73,0x20,0x4a,
  8700. 0x61,0x63,0x6b,0x20,0x61,0x20,0x64,0x75,
  8701. 0x6c,0x6c,0x20,0x62,0x6f,0x79,0x2e,0x20,
  8702. 0x41,0x6c,0x6c,0x20,0x77,0x6f,0x72,0x6b,
  8703. 0x20,0x61,0x6e,0x64,0x20,0x6e,0x6f,0x20,
  8704. 0x70,0x6c,0x61,0x79,0x20,0x6d,0x61,0x6b,
  8705. 0x65,0x73,0x20,0x4a,0x61,0x63,0x6b,0x20,
  8706. 0x61,0x20,0x64,0x75,0x6c,0x6c,0x20,0x62,
  8707. 0x6f,0x79,0x2e,0x20,0x41,0x6c,0x6c,0x20,
  8708. 0x77,0x6f,0x72,0x6b,0x20,0x61,0x6e,0x64,
  8709. 0x20,0x6e,0x6f,0x20,0x70,0x6c,0x61,0x79,
  8710. 0x20,0x6d,0x61,0x6b,0x65,0x73,0x20,0x4a,
  8711. 0x61,0x63,0x6b,0x20,0x61,0x20,0x64,0x75,
  8712. 0x6c,0x6c,0x20,0x62,0x6f,0x79,0x2e,0x20,
  8713. 0x41,0x6c,0x6c,0x20,0x77,0x6f,0x72,0x6b,
  8714. 0x20,0x61,0x6e,0x64,0x20,0x6e,0x6f,0x20,
  8715. 0x70,0x6c,0x61,0x79,0x20,0x6d,0x61,0x6b,
  8716. 0x65,0x73,0x20,0x4a,0x61,0x63,0x6b,0x20,
  8717. 0x61,0x20,0x64,0x75,0x6c,0x6c,0x20,0x62,
  8718. 0x6f,0x79,0x2e,0x20,0x41,0x6c,0x6c,0x20,
  8719. 0x77,0x6f,0x72,0x6b,0x20,0x61,0x6e,0x64,
  8720. 0x20,0x6e,0x6f,0x20,0x70,0x6c,0x61,0x79,
  8721. 0x20,0x6d,0x61,0x6b,0x65,0x73,0x20,0x4a,
  8722. 0x61,0x63,0x6b,0x20,0x61,0x20,0x64,0x75,
  8723. 0x6c,0x6c,0x20,0x62,0x6f,0x79,0x2e,0x20,
  8724. 0x41,0x6c,0x6c,0x20,0x77,0x6f,0x72,0x6b,
  8725. 0x20,0x61,0x6e,0x64,0x20,0x6e,0x6f,0x20,
  8726. 0x70,0x6c,0x61,0x79,0x20,0x6d,0x61,0x6b,
  8727. 0x65,0x73,0x20,0x4a,0x61,0x63,0x6b,0x20,
  8728. 0x61,0x20,0x64,0x75,0x6c,0x6c,0x20,0x62,
  8729. 0x6f,0x79,0x2e,0x20,0x41,0x6c,0x6c,0x20,
  8730. 0x77,0x6f,0x72,0x6b,0x20,0x61,0x6e,0x64,
  8731. 0x20,0x6e,0x6f,0x20,0x70,0x6c,0x61,0x79,
  8732. 0x20,0x6d,0x61,0x6b,0x65,0x73,0x20,0x4a,
  8733. 0x61,0x63,0x6b,0x20,0x61,0x20,0x64,0x75,
  8734. 0x6c,0x6c,0x20,0x62,0x6f,0x79,0x2e,0x20,
  8735. 0x41,0x6c,0x6c,0x20,0x77,0x6f,0x72,0x6b,
  8736. 0x20,0x61,0x6e,0x64,0x20,0x6e,0x6f,0x20,
  8737. 0x70,0x6c,0x61,0x79,0x20,0x6d,0x61,0x6b,
  8738. 0x65,0x73,0x20,0x4a,0x61,0x63,0x6b,0x20
  8739. };
  8740. WOLFSSL_SMALL_STACK_STATIC const byte bigKey[] = "0123456789abcdeffedcba9876543210";
  8741. word32 keySz, msgSz;
  8742. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8743. byte *bigCipher = (byte *)XMALLOC(sizeof(bigMsg), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8744. byte *bigPlain = (byte *)XMALLOC(sizeof(bigMsg), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8745. if ((bigCipher == NULL) ||
  8746. (bigPlain == NULL)) {
  8747. if (bigCipher != NULL)
  8748. XFREE(bigCipher, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8749. ERROR_OUT(-5992, out);
  8750. }
  8751. #else
  8752. byte bigCipher[sizeof(bigMsg)];
  8753. byte bigPlain[sizeof(bigMsg)];
  8754. #endif
  8755. /* Iterate from one AES_BLOCK_SIZE of bigMsg through the whole
  8756. * message by AES_BLOCK_SIZE for each size of AES key. */
  8757. for (keySz = 16; keySz <= 32; keySz += 8) {
  8758. for (msgSz = AES_BLOCK_SIZE;
  8759. msgSz <= sizeof(bigMsg);
  8760. msgSz += AES_BLOCK_SIZE) {
  8761. XMEMSET(bigCipher, 0, sizeof(bigMsg));
  8762. XMEMSET(bigPlain, 0, sizeof(bigMsg));
  8763. ret = wc_AesSetKey(enc, bigKey, keySz, iv, AES_ENCRYPTION);
  8764. if (ret != 0) {
  8765. ret = -5908;
  8766. break;
  8767. }
  8768. ret = wc_AesSetKey(dec, bigKey, keySz, iv, AES_DECRYPTION);
  8769. if (ret != 0) {
  8770. ret = -5909;
  8771. break;
  8772. }
  8773. ret = wc_AesCbcEncrypt(enc, bigCipher, bigMsg, msgSz);
  8774. #if defined(WOLFSSL_ASYNC_CRYPT)
  8775. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  8776. #endif
  8777. if (ret != 0) {
  8778. ret = -5910;
  8779. break;
  8780. }
  8781. ret = wc_AesCbcDecrypt(dec, bigPlain, bigCipher, msgSz);
  8782. #if defined(WOLFSSL_ASYNC_CRYPT)
  8783. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  8784. #endif
  8785. if (ret != 0) {
  8786. ret = -5911;
  8787. break;
  8788. }
  8789. if (XMEMCMP(bigPlain, bigMsg, msgSz)) {
  8790. ret = -5912;
  8791. break;
  8792. }
  8793. }
  8794. if (ret != 0)
  8795. break;
  8796. }
  8797. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8798. XFREE(bigCipher, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8799. XFREE(bigPlain, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8800. #endif
  8801. if (ret != 0)
  8802. goto out;
  8803. }
  8804. #endif /* WOLFSSL_AESNI && HAVE_AES_DECRYPT */
  8805. /* Test of AES IV state with encrypt/decrypt */
  8806. #ifdef WOLFSSL_AES_128
  8807. {
  8808. /* Test Vector from "NIST Special Publication 800-38A, 2001 Edition"
  8809. * https://nvlpubs.nist.gov/nistpubs/legacy/sp/nistspecialpublication800-38a.pdf
  8810. */
  8811. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] =
  8812. {
  8813. 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
  8814. 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
  8815. 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c,
  8816. 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51
  8817. };
  8818. WOLFSSL_SMALL_STACK_STATIC const byte verify2[] =
  8819. {
  8820. 0x76, 0x49, 0xab, 0xac, 0x81, 0x19, 0xb2, 0x46,
  8821. 0xce, 0xe9, 0x8e, 0x9b, 0x12, 0xe9, 0x19, 0x7d,
  8822. 0x50, 0x86, 0xcb, 0x9b, 0x50, 0x72, 0x19, 0xee,
  8823. 0x95, 0xdb, 0x11, 0x3a, 0x91, 0x76, 0x78, 0xb2
  8824. };
  8825. WOLFSSL_SMALL_STACK_STATIC const byte key2[] = {
  8826. 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
  8827. 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c
  8828. };
  8829. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] = {
  8830. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  8831. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
  8832. };
  8833. ret = wc_AesSetKey(enc, key2, sizeof(key2), iv2, AES_ENCRYPTION);
  8834. if (ret != 0)
  8835. ERROR_OUT(-5913, out);
  8836. XMEMSET(cipher, 0, AES_BLOCK_SIZE * 2);
  8837. ret = wc_AesCbcEncrypt(enc, cipher, msg2, AES_BLOCK_SIZE);
  8838. #if defined(WOLFSSL_ASYNC_CRYPT)
  8839. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  8840. #endif
  8841. if (ret != 0)
  8842. ERROR_OUT(-5914, out);
  8843. if (XMEMCMP(cipher, verify2, AES_BLOCK_SIZE))
  8844. ERROR_OUT(-5915, out);
  8845. ret = wc_AesCbcEncrypt(enc, cipher + AES_BLOCK_SIZE,
  8846. msg2 + AES_BLOCK_SIZE, AES_BLOCK_SIZE);
  8847. #if defined(WOLFSSL_ASYNC_CRYPT)
  8848. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  8849. #endif
  8850. if (ret != 0)
  8851. ERROR_OUT(-5916, out);
  8852. if (XMEMCMP(cipher + AES_BLOCK_SIZE, verify2 + AES_BLOCK_SIZE,
  8853. AES_BLOCK_SIZE))
  8854. ERROR_OUT(-5917, out);
  8855. #if defined(HAVE_AES_DECRYPT)
  8856. ret = wc_AesSetKey(dec, key2, sizeof(key2), iv2, AES_DECRYPTION);
  8857. if (ret != 0)
  8858. ERROR_OUT(-5918, out);
  8859. XMEMSET(plain, 0, AES_BLOCK_SIZE * 2);
  8860. ret = wc_AesCbcDecrypt(dec, plain, verify2, AES_BLOCK_SIZE);
  8861. #if defined(WOLFSSL_ASYNC_CRYPT)
  8862. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  8863. #endif
  8864. if (ret != 0)
  8865. ERROR_OUT(-5919, out);
  8866. if (XMEMCMP(plain, msg2, AES_BLOCK_SIZE))
  8867. ERROR_OUT(-5920, out);
  8868. ret = wc_AesCbcDecrypt(dec, plain + AES_BLOCK_SIZE,
  8869. verify2 + AES_BLOCK_SIZE, AES_BLOCK_SIZE);
  8870. #if defined(WOLFSSL_ASYNC_CRYPT)
  8871. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  8872. #endif
  8873. if (ret != 0)
  8874. ERROR_OUT(-5921, out);
  8875. if (XMEMCMP(plain + AES_BLOCK_SIZE, msg2 + AES_BLOCK_SIZE,
  8876. AES_BLOCK_SIZE))
  8877. ERROR_OUT(-5922, out);
  8878. #endif /* HAVE_AES_DECRYPT */
  8879. }
  8880. #endif /* WOLFSSL_AES_128 */
  8881. #endif /* HAVE_AES_CBC */
  8882. #ifdef WOLFSSL_AES_COUNTER
  8883. ret = aesctr_test(enc, dec, cipher, plain);
  8884. if (ret != 0)
  8885. return ret;
  8886. #endif
  8887. #if defined(WOLFSSL_AES_DIRECT) && defined(WOLFSSL_AES_256)
  8888. {
  8889. WOLFSSL_SMALL_STACK_STATIC const byte niPlain[] =
  8890. {
  8891. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  8892. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  8893. };
  8894. WOLFSSL_SMALL_STACK_STATIC const byte niCipher[] =
  8895. {
  8896. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  8897. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  8898. };
  8899. WOLFSSL_SMALL_STACK_STATIC const byte niKey[] =
  8900. {
  8901. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  8902. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  8903. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  8904. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  8905. };
  8906. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  8907. ret = wc_AesSetKey(enc, niKey, sizeof(niKey), cipher, AES_ENCRYPTION);
  8908. if (ret != 0)
  8909. ERROR_OUT(-5994, out);
  8910. #if !defined(HAVE_SELFTEST) && \
  8911. (defined(WOLFSSL_LINUXKM) || \
  8912. !defined(HAVE_FIPS) || \
  8913. (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3)))
  8914. if (wc_AesEncryptDirect(enc, cipher, niPlain) != 0)
  8915. ERROR_OUT(-5995, out);
  8916. #else
  8917. wc_AesEncryptDirect(enc, cipher, niPlain);
  8918. #endif
  8919. if (XMEMCMP(cipher, niCipher, AES_BLOCK_SIZE) != 0)
  8920. ERROR_OUT(-5996, out);
  8921. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  8922. ret = wc_AesSetKey(dec, niKey, sizeof(niKey), plain, AES_DECRYPTION);
  8923. if (ret != 0)
  8924. ERROR_OUT(-5997, out);
  8925. #if !defined(HAVE_SELFTEST) && \
  8926. (defined(WOLFSSL_LINUXKM) || \
  8927. !defined(HAVE_FIPS) || \
  8928. (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3)))
  8929. if (wc_AesDecryptDirect(dec, plain, niCipher) != 0)
  8930. ERROR_OUT(-5998, out);
  8931. #else
  8932. wc_AesDecryptDirect(dec, plain, niCipher);
  8933. #endif
  8934. if (XMEMCMP(plain, niPlain, AES_BLOCK_SIZE) != 0)
  8935. ERROR_OUT(-5999, out);
  8936. }
  8937. #endif /* WOLFSSL_AES_DIRECT && WOLFSSL_AES_256 */
  8938. ret = aes_key_size_test();
  8939. if (ret != 0)
  8940. goto out;
  8941. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  8942. ret = aes_cbc_test();
  8943. if (ret != 0)
  8944. goto out;
  8945. #endif
  8946. #if defined(WOLFSSL_AES_XTS)
  8947. #ifdef WOLFSSL_AES_128
  8948. ret = aes_xts_128_test();
  8949. if (ret != 0)
  8950. goto out;
  8951. #endif
  8952. #ifdef WOLFSSL_AES_256
  8953. ret = aes_xts_256_test();
  8954. if (ret != 0)
  8955. goto out;
  8956. #endif
  8957. #if defined(WOLFSSL_AES_128) && defined(WOLFSSL_AES_256)
  8958. ret = aes_xts_sector_test();
  8959. if (ret != 0)
  8960. goto out;
  8961. #endif
  8962. #ifdef WOLFSSL_AES_128
  8963. ret = aes_xts_args_test();
  8964. if (ret != 0)
  8965. goto out;
  8966. #endif
  8967. #endif
  8968. #if defined(WOLFSSL_AES_CFB)
  8969. ret = aescfb_test();
  8970. if (ret != 0)
  8971. goto out;
  8972. #if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  8973. ret = aescfb1_test();
  8974. if (ret != 0)
  8975. goto out;
  8976. ret = aescfb8_test();
  8977. if (ret != 0)
  8978. goto out;
  8979. #endif
  8980. #endif
  8981. #if defined(HAVE_AES_ECB) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  8982. ret = aesecb_test();
  8983. if (ret != 0)
  8984. goto out;
  8985. #endif
  8986. out:
  8987. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_COUNTER) || defined(WOLFSSL_AES_DIRECT)
  8988. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8989. if (enc) {
  8990. if (ret != -5900) /* note this must match ERRROR_OUT() code
  8991. * for wc_AesInit(enc, ...) failure above.
  8992. */
  8993. wc_AesFree(enc);
  8994. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  8995. }
  8996. #else
  8997. if (ret != -5900)
  8998. wc_AesFree(enc);
  8999. #endif
  9000. (void)cipher;
  9001. #ifdef HAVE_AES_DECRYPT
  9002. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9003. if (dec) {
  9004. if ((ret != -5900) && (ret != -5901))
  9005. /* note these codes must match the ERRROR_OUT() codes for
  9006. * wc_AesInit() failures above.
  9007. */
  9008. wc_AesFree(dec);
  9009. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  9010. }
  9011. #else
  9012. if ((ret != -5900) && (ret != -5901))
  9013. wc_AesFree(dec);
  9014. #endif
  9015. (void)plain;
  9016. #endif /* HAVE_AES_DECRYPT */
  9017. #endif /* HAVE_AES_CBC || WOLFSSL_AES_COUNTER || WOLFSSL_AES_DIRECT */
  9018. return ret;
  9019. }
  9020. #ifdef WOLFSSL_AES_192
  9021. WOLFSSL_TEST_SUBROUTINE int aes192_test(void)
  9022. {
  9023. #ifdef HAVE_AES_CBC
  9024. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9025. Aes *enc = NULL;
  9026. #else
  9027. Aes enc[1];
  9028. #endif
  9029. byte cipher[AES_BLOCK_SIZE];
  9030. #ifdef HAVE_AES_DECRYPT
  9031. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9032. Aes *dec = NULL;
  9033. #else
  9034. Aes dec[1];
  9035. #endif
  9036. byte plain[AES_BLOCK_SIZE];
  9037. #endif
  9038. #endif /* HAVE_AES_CBC */
  9039. int ret = 0;
  9040. #ifdef HAVE_AES_CBC
  9041. /* Test vectors from NIST Special Publication 800-38A, 2001 Edition
  9042. * Appendix F.2.3 */
  9043. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = {
  9044. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  9045. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  9046. };
  9047. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  9048. {
  9049. 0x4f,0x02,0x1d,0xb2,0x43,0xbc,0x63,0x3d,
  9050. 0x71,0x78,0x18,0x3a,0x9f,0xa0,0x71,0xe8
  9051. };
  9052. WOLFSSL_SMALL_STACK_STATIC byte key[] = {
  9053. 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  9054. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  9055. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b
  9056. };
  9057. WOLFSSL_SMALL_STACK_STATIC byte iv[] = {
  9058. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  9059. 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F
  9060. };
  9061. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9062. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  9063. ERROR_OUT(-6008, out);
  9064. #ifdef HAVE_AES_DECRYPT
  9065. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  9066. ERROR_OUT(-6009, out);
  9067. #endif
  9068. #endif
  9069. if (wc_AesInit(enc, HEAP_HINT, devId) != 0)
  9070. ERROR_OUT(-6000, out);
  9071. #ifdef HAVE_AES_DECRYPT
  9072. if (wc_AesInit(dec, HEAP_HINT, devId) != 0)
  9073. ERROR_OUT(-6001, out);
  9074. #endif
  9075. ret = wc_AesSetKey(enc, key, (int) sizeof(key), iv, AES_ENCRYPTION);
  9076. if (ret != 0)
  9077. ERROR_OUT(-6002, out);
  9078. #ifdef HAVE_AES_DECRYPT
  9079. ret = wc_AesSetKey(dec, key, (int) sizeof(key), iv, AES_DECRYPTION);
  9080. if (ret != 0)
  9081. ERROR_OUT(-6003, out);
  9082. #endif
  9083. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  9084. ret = wc_AesCbcEncrypt(enc, cipher, msg, (int) sizeof(msg));
  9085. #if defined(WOLFSSL_ASYNC_CRYPT)
  9086. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  9087. #endif
  9088. if (ret != 0)
  9089. ERROR_OUT(-6004, out);
  9090. #ifdef HAVE_AES_DECRYPT
  9091. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  9092. ret = wc_AesCbcDecrypt(dec, plain, cipher, (int) sizeof(cipher));
  9093. #if defined(WOLFSSL_ASYNC_CRYPT)
  9094. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  9095. #endif
  9096. if (ret != 0)
  9097. ERROR_OUT(-6005, out);
  9098. if (XMEMCMP(plain, msg, (int) sizeof(plain))) {
  9099. ERROR_OUT(-6006, out);
  9100. }
  9101. #endif
  9102. if (XMEMCMP(cipher, verify, (int) sizeof(cipher)))
  9103. ERROR_OUT(-6007, out);
  9104. wc_AesFree(enc);
  9105. #ifdef HAVE_AES_DECRYPT
  9106. wc_AesFree(dec);
  9107. #endif
  9108. out:
  9109. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9110. if (enc)
  9111. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  9112. #ifdef HAVE_AES_DECRYPT
  9113. if (dec)
  9114. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  9115. #endif
  9116. #endif
  9117. #endif /* HAVE_AES_CBC */
  9118. return ret;
  9119. }
  9120. #endif /* WOLFSSL_AES_192 */
  9121. #ifdef WOLFSSL_AES_256
  9122. WOLFSSL_TEST_SUBROUTINE int aes256_test(void)
  9123. {
  9124. #ifdef HAVE_AES_CBC
  9125. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9126. Aes *enc = NULL;
  9127. #else
  9128. Aes enc[1];
  9129. #endif
  9130. byte cipher[AES_BLOCK_SIZE];
  9131. #ifdef HAVE_AES_DECRYPT
  9132. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9133. Aes *dec = NULL;
  9134. #else
  9135. Aes dec[1];
  9136. #endif
  9137. byte plain[AES_BLOCK_SIZE];
  9138. #endif
  9139. #endif /* HAVE_AES_CBC */
  9140. int ret = 0;
  9141. #ifdef HAVE_AES_CBC
  9142. /* Test vectors from NIST Special Publication 800-38A, 2001 Edition,
  9143. * Appendix F.2.5 */
  9144. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = {
  9145. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  9146. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  9147. };
  9148. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  9149. {
  9150. 0xf5,0x8c,0x4c,0x04,0xd6,0xe5,0xf1,0xba,
  9151. 0x77,0x9e,0xab,0xfb,0x5f,0x7b,0xfb,0xd6
  9152. };
  9153. WOLFSSL_SMALL_STACK_STATIC byte key[] = {
  9154. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  9155. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  9156. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  9157. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  9158. };
  9159. WOLFSSL_SMALL_STACK_STATIC byte iv[] = {
  9160. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  9161. 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F
  9162. };
  9163. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9164. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  9165. ERROR_OUT(-6108, out);
  9166. #ifdef HAVE_AES_DECRYPT
  9167. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  9168. ERROR_OUT(-6109, out);
  9169. #endif
  9170. #endif
  9171. if (wc_AesInit(enc, HEAP_HINT, devId) != 0)
  9172. ERROR_OUT(-6100, out);
  9173. #ifdef HAVE_AES_DECRYPT
  9174. if (wc_AesInit(dec, HEAP_HINT, devId) != 0)
  9175. ERROR_OUT(-6101, out);
  9176. #endif
  9177. ret = wc_AesSetKey(enc, key, (int) sizeof(key), iv, AES_ENCRYPTION);
  9178. if (ret != 0)
  9179. ERROR_OUT(-6102, out);
  9180. #ifdef HAVE_AES_DECRYPT
  9181. ret = wc_AesSetKey(dec, key, (int) sizeof(key), iv, AES_DECRYPTION);
  9182. if (ret != 0)
  9183. ERROR_OUT(-6103, out);
  9184. #endif
  9185. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  9186. ret = wc_AesCbcEncrypt(enc, cipher, msg, (int) sizeof(msg));
  9187. #if defined(WOLFSSL_ASYNC_CRYPT)
  9188. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  9189. #endif
  9190. if (ret != 0)
  9191. ERROR_OUT(-6104, out);
  9192. #ifdef HAVE_AES_DECRYPT
  9193. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  9194. ret = wc_AesCbcDecrypt(dec, plain, cipher, (int) sizeof(cipher));
  9195. #if defined(WOLFSSL_ASYNC_CRYPT)
  9196. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  9197. #endif
  9198. if (ret != 0)
  9199. ERROR_OUT(-6105, out);
  9200. if (XMEMCMP(plain, msg, (int) sizeof(plain))) {
  9201. ERROR_OUT(-6106, out);
  9202. }
  9203. #endif
  9204. if (XMEMCMP(cipher, verify, (int) sizeof(cipher)))
  9205. ERROR_OUT(-6107, out);
  9206. wc_AesFree(enc);
  9207. #ifdef HAVE_AES_DECRYPT
  9208. wc_AesFree(dec);
  9209. #endif
  9210. out:
  9211. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9212. if (enc)
  9213. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  9214. #ifdef HAVE_AES_DECRYPT
  9215. if (dec)
  9216. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  9217. #endif
  9218. #endif
  9219. #endif /* HAVE_AES_CBC */
  9220. return ret;
  9221. }
  9222. #endif /* WOLFSSL_AES_256 */
  9223. #ifdef HAVE_AESGCM
  9224. #ifdef WOLFSSL_AES_128
  9225. static int aesgcm_default_test_helper(byte* key, int keySz, byte* iv, int ivSz,
  9226. byte* plain, int plainSz, byte* cipher, int cipherSz,
  9227. byte* aad, int aadSz, byte* tag, int tagSz)
  9228. {
  9229. int ret, enc_inited = 0, dec_inited = 0;
  9230. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9231. Aes *enc = NULL;
  9232. Aes *dec = NULL;
  9233. #else
  9234. Aes enc[1];
  9235. Aes dec[1];
  9236. #endif
  9237. byte resultT[AES_BLOCK_SIZE];
  9238. byte resultP[AES_BLOCK_SIZE * 3];
  9239. byte resultC[AES_BLOCK_SIZE * 3];
  9240. int result;
  9241. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9242. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  9243. ERROR_OUT(-6118, out);
  9244. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  9245. ERROR_OUT(-6119, out);
  9246. #endif
  9247. XMEMSET(resultT, 0, sizeof(resultT));
  9248. XMEMSET(resultC, 0, sizeof(resultC));
  9249. XMEMSET(resultP, 0, sizeof(resultP));
  9250. if (wc_AesInit(enc, HEAP_HINT, devId) != 0)
  9251. ERROR_OUT(-6110, out);
  9252. else
  9253. enc_inited = 1;
  9254. if (wc_AesInit(dec, HEAP_HINT, devId) != 0)
  9255. ERROR_OUT(-6111, out);
  9256. else
  9257. dec_inited = 1;
  9258. result = wc_AesGcmSetKey(enc, key, keySz);
  9259. if (result != 0)
  9260. ERROR_OUT(-6112, out);
  9261. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  9262. result = wc_AesGcmEncrypt(enc, resultC, plain, plainSz, iv, ivSz,
  9263. resultT, tagSz, aad, aadSz);
  9264. #if defined(WOLFSSL_ASYNC_CRYPT)
  9265. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  9266. #endif
  9267. if (result != 0)
  9268. ERROR_OUT(-6113, out);
  9269. if (cipher != NULL) {
  9270. if (XMEMCMP(cipher, resultC, cipherSz))
  9271. ERROR_OUT(-6114, out);
  9272. }
  9273. if (XMEMCMP(tag, resultT, tagSz))
  9274. ERROR_OUT(-6115, out);
  9275. #ifdef HAVE_AES_DECRYPT
  9276. result = wc_AesGcmSetKey(dec, key, keySz);
  9277. if (result != 0)
  9278. ERROR_OUT(-6116, out);
  9279. result = wc_AesGcmDecrypt(dec, resultP, resultC, cipherSz,
  9280. iv, ivSz, resultT, tagSz, aad, aadSz);
  9281. #if defined(WOLFSSL_ASYNC_CRYPT)
  9282. result = wc_AsyncWait(result, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  9283. #endif
  9284. if (result != 0)
  9285. ERROR_OUT(-6117, out);
  9286. if (plain != NULL) {
  9287. if (XMEMCMP(plain, resultP, plainSz))
  9288. ERROR_OUT(-6118, out);
  9289. }
  9290. #endif /* HAVE_AES_DECRYPT */
  9291. ret = 0;
  9292. out:
  9293. if (enc_inited)
  9294. wc_AesFree(enc);
  9295. if (dec_inited)
  9296. wc_AesFree(dec);
  9297. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9298. if (enc)
  9299. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  9300. if (dec)
  9301. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  9302. #endif
  9303. return ret;
  9304. }
  9305. #endif
  9306. /* tests that only use 12 byte IV and 16 or less byte AAD
  9307. * test vectors are from NIST SP 800-38D
  9308. * https://csrc.nist.gov/Projects/Cryptographic-Algorithm-Validation-Program/CAVP-TESTING-BLOCK-CIPHER-MODES*/
  9309. WOLFSSL_TEST_SUBROUTINE int aesgcm_default_test(void)
  9310. {
  9311. #ifdef WOLFSSL_AES_128
  9312. byte key1[] = {
  9313. 0x29, 0x8e, 0xfa, 0x1c, 0xcf, 0x29, 0xcf, 0x62,
  9314. 0xae, 0x68, 0x24, 0xbf, 0xc1, 0x95, 0x57, 0xfc
  9315. };
  9316. byte iv1[] = {
  9317. 0x6f, 0x58, 0xa9, 0x3f, 0xe1, 0xd2, 0x07, 0xfa,
  9318. 0xe4, 0xed, 0x2f, 0x6d
  9319. };
  9320. ALIGN64 byte plain1[] = {
  9321. 0xcc, 0x38, 0xbc, 0xcd, 0x6b, 0xc5, 0x36, 0xad,
  9322. 0x91, 0x9b, 0x13, 0x95, 0xf5, 0xd6, 0x38, 0x01,
  9323. 0xf9, 0x9f, 0x80, 0x68, 0xd6, 0x5c, 0xa5, 0xac,
  9324. 0x63, 0x87, 0x2d, 0xaf, 0x16, 0xb9, 0x39, 0x01
  9325. };
  9326. byte aad1[] = {
  9327. 0x02, 0x1f, 0xaf, 0xd2, 0x38, 0x46, 0x39, 0x73,
  9328. 0xff, 0xe8, 0x02, 0x56, 0xe5, 0xb1, 0xc6, 0xb1
  9329. };
  9330. ALIGN64 byte cipher1[] = {
  9331. 0xdf, 0xce, 0x4e, 0x9c, 0xd2, 0x91, 0x10, 0x3d,
  9332. 0x7f, 0xe4, 0xe6, 0x33, 0x51, 0xd9, 0xe7, 0x9d,
  9333. 0x3d, 0xfd, 0x39, 0x1e, 0x32, 0x67, 0x10, 0x46,
  9334. 0x58, 0x21, 0x2d, 0xa9, 0x65, 0x21, 0xb7, 0xdb
  9335. };
  9336. byte tag1[] = {
  9337. 0x54, 0x24, 0x65, 0xef, 0x59, 0x93, 0x16, 0xf7,
  9338. 0x3a, 0x7a, 0x56, 0x05, 0x09, 0xa2, 0xd9, 0xf2
  9339. };
  9340. byte key2[] = {
  9341. 0x01, 0x6d, 0xbb, 0x38, 0xda, 0xa7, 0x6d, 0xfe,
  9342. 0x7d, 0xa3, 0x84, 0xeb, 0xf1, 0x24, 0x03, 0x64
  9343. };
  9344. byte iv2[] = {
  9345. 0x07, 0x93, 0xef, 0x3a, 0xda, 0x78, 0x2f, 0x78,
  9346. 0xc9, 0x8a, 0xff, 0xe3
  9347. };
  9348. ALIGN64 byte plain2[] = {
  9349. 0x4b, 0x34, 0xa9, 0xec, 0x57, 0x63, 0x52, 0x4b,
  9350. 0x19, 0x1d, 0x56, 0x16, 0xc5, 0x47, 0xf6, 0xb7
  9351. };
  9352. ALIGN64 byte cipher2[] = {
  9353. 0x60, 0x9a, 0xa3, 0xf4, 0x54, 0x1b, 0xc0, 0xfe,
  9354. 0x99, 0x31, 0xda, 0xad, 0x2e, 0xe1, 0x5d, 0x0c
  9355. };
  9356. byte tag2[] = {
  9357. 0x33, 0xaf, 0xec, 0x59, 0xc4, 0x5b, 0xaf, 0x68,
  9358. 0x9a, 0x5e, 0x1b, 0x13, 0xae, 0x42, 0x36, 0x19
  9359. };
  9360. byte key3[] = {
  9361. 0xb0, 0x1e, 0x45, 0xcc, 0x30, 0x88, 0xaa, 0xba,
  9362. 0x9f, 0xa4, 0x3d, 0x81, 0xd4, 0x81, 0x82, 0x3f
  9363. };
  9364. byte iv3[] = {
  9365. 0x5a, 0x2c, 0x4a, 0x66, 0x46, 0x87, 0x13, 0x45,
  9366. 0x6a, 0x4b, 0xd5, 0xe1
  9367. };
  9368. byte tag3[] = {
  9369. 0x01, 0x42, 0x80, 0xf9, 0x44, 0xf5, 0x3c, 0x68,
  9370. 0x11, 0x64, 0xb2, 0xff
  9371. };
  9372. int ret;
  9373. ret = aesgcm_default_test_helper(key1, sizeof(key1), iv1, sizeof(iv1),
  9374. plain1, sizeof(plain1), cipher1, sizeof(cipher1),
  9375. aad1, sizeof(aad1), tag1, sizeof(tag1));
  9376. if (ret != 0) {
  9377. return ret;
  9378. }
  9379. ret = aesgcm_default_test_helper(key2, sizeof(key2), iv2, sizeof(iv2),
  9380. plain2, sizeof(plain2), cipher2, sizeof(cipher2),
  9381. NULL, 0, tag2, sizeof(tag2));
  9382. if (ret != 0) {
  9383. return ret;
  9384. }
  9385. ret = aesgcm_default_test_helper(key3, sizeof(key3), iv3, sizeof(iv3),
  9386. NULL, 0, NULL, 0,
  9387. NULL, 0, tag3, sizeof(tag3));
  9388. if (ret != 0) {
  9389. return ret;
  9390. }
  9391. #endif
  9392. return 0;
  9393. }
  9394. WOLFSSL_TEST_SUBROUTINE int aesgcm_test(void)
  9395. {
  9396. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9397. Aes *enc = NULL;
  9398. Aes *dec = NULL;
  9399. #else
  9400. Aes enc[1];
  9401. Aes dec[1];
  9402. #endif
  9403. /*
  9404. * This is Test Case 16 from the document Galois/
  9405. * Counter Mode of Operation (GCM) by McGrew and
  9406. * Viega.
  9407. */
  9408. WOLFSSL_SMALL_STACK_STATIC const byte p[] =
  9409. {
  9410. 0xd9, 0x31, 0x32, 0x25, 0xf8, 0x84, 0x06, 0xe5,
  9411. 0xa5, 0x59, 0x09, 0xc5, 0xaf, 0xf5, 0x26, 0x9a,
  9412. 0x86, 0xa7, 0xa9, 0x53, 0x15, 0x34, 0xf7, 0xda,
  9413. 0x2e, 0x4c, 0x30, 0x3d, 0x8a, 0x31, 0x8a, 0x72,
  9414. 0x1c, 0x3c, 0x0c, 0x95, 0x95, 0x68, 0x09, 0x53,
  9415. 0x2f, 0xcf, 0x0e, 0x24, 0x49, 0xa6, 0xb5, 0x25,
  9416. 0xb1, 0x6a, 0xed, 0xf5, 0xaa, 0x0d, 0xe6, 0x57,
  9417. 0xba, 0x63, 0x7b, 0x39
  9418. };
  9419. #if defined(WOLFSSL_AES_256) || defined(WOLFSSL_AES_192)
  9420. WOLFSSL_SMALL_STACK_STATIC const byte a[] =
  9421. {
  9422. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  9423. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  9424. 0xab, 0xad, 0xda, 0xd2
  9425. };
  9426. #endif
  9427. #ifdef WOLFSSL_AES_256
  9428. WOLFSSL_SMALL_STACK_STATIC const byte k1[] =
  9429. {
  9430. 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c,
  9431. 0x6d, 0x6a, 0x8f, 0x94, 0x67, 0x30, 0x83, 0x08,
  9432. 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c,
  9433. 0x6d, 0x6a, 0x8f, 0x94, 0x67, 0x30, 0x83, 0x08
  9434. };
  9435. WOLFSSL_SMALL_STACK_STATIC const byte iv1[] =
  9436. {
  9437. 0xca, 0xfe, 0xba, 0xbe, 0xfa, 0xce, 0xdb, 0xad,
  9438. 0xde, 0xca, 0xf8, 0x88
  9439. };
  9440. #endif /* WOLFSSL_AES_256 */
  9441. #if defined(WOLFSSL_AES_256) || defined(WOLFSSL_AES_192)
  9442. WOLFSSL_SMALL_STACK_STATIC const byte c1[] =
  9443. {
  9444. 0x52, 0x2d, 0xc1, 0xf0, 0x99, 0x56, 0x7d, 0x07,
  9445. 0xf4, 0x7f, 0x37, 0xa3, 0x2a, 0x84, 0x42, 0x7d,
  9446. 0x64, 0x3a, 0x8c, 0xdc, 0xbf, 0xe5, 0xc0, 0xc9,
  9447. 0x75, 0x98, 0xa2, 0xbd, 0x25, 0x55, 0xd1, 0xaa,
  9448. 0x8c, 0xb0, 0x8e, 0x48, 0x59, 0x0d, 0xbb, 0x3d,
  9449. 0xa7, 0xb0, 0x8b, 0x10, 0x56, 0x82, 0x88, 0x38,
  9450. 0xc5, 0xf6, 0x1e, 0x63, 0x93, 0xba, 0x7a, 0x0a,
  9451. 0xbc, 0xc9, 0xf6, 0x62
  9452. };
  9453. #endif /* WOLFSSL_AES_256 || WOLFSSL_AES_192 */
  9454. WOLFSSL_SMALL_STACK_STATIC const byte t1[] =
  9455. {
  9456. 0x76, 0xfc, 0x6e, 0xce, 0x0f, 0x4e, 0x17, 0x68,
  9457. 0xcd, 0xdf, 0x88, 0x53, 0xbb, 0x2d, 0x55, 0x1b
  9458. };
  9459. /* FIPS, QAT and PIC32MZ HW Crypto only support 12-byte IV */
  9460. #if !defined(HAVE_FIPS) && \
  9461. !defined(WOLFSSL_PIC32MZ_CRYPT) && \
  9462. !defined(FREESCALE_LTC) && !defined(FREESCALE_MMCAU) && \
  9463. !defined(WOLFSSL_XILINX_CRYPT) && !defined(WOLFSSL_AFALG_XILINX_AES) && \
  9464. !defined(WOLFSSL_SILABS_SE_ACCEL) && !defined(WOLFSSL_KCAPI_AES) && \
  9465. !(defined(WOLF_CRYPTO_CB) && \
  9466. (defined(HAVE_INTEL_QA_SYNC) || defined(HAVE_CAVIUM_OCTEON_SYNC)))
  9467. #define ENABLE_NON_12BYTE_IV_TEST
  9468. #ifdef WOLFSSL_AES_192
  9469. /* Test Case 12, uses same plaintext and AAD data. */
  9470. WOLFSSL_SMALL_STACK_STATIC const byte k2[] =
  9471. {
  9472. 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c,
  9473. 0x6d, 0x6a, 0x8f, 0x94, 0x67, 0x30, 0x83, 0x08,
  9474. 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c
  9475. };
  9476. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] =
  9477. {
  9478. 0x93, 0x13, 0x22, 0x5d, 0xf8, 0x84, 0x06, 0xe5,
  9479. 0x55, 0x90, 0x9c, 0x5a, 0xff, 0x52, 0x69, 0xaa,
  9480. 0x6a, 0x7a, 0x95, 0x38, 0x53, 0x4f, 0x7d, 0xa1,
  9481. 0xe4, 0xc3, 0x03, 0xd2, 0xa3, 0x18, 0xa7, 0x28,
  9482. 0xc3, 0xc0, 0xc9, 0x51, 0x56, 0x80, 0x95, 0x39,
  9483. 0xfc, 0xf0, 0xe2, 0x42, 0x9a, 0x6b, 0x52, 0x54,
  9484. 0x16, 0xae, 0xdb, 0xf5, 0xa0, 0xde, 0x6a, 0x57,
  9485. 0xa6, 0x37, 0xb3, 0x9b
  9486. };
  9487. WOLFSSL_SMALL_STACK_STATIC const byte c2[] =
  9488. {
  9489. 0xd2, 0x7e, 0x88, 0x68, 0x1c, 0xe3, 0x24, 0x3c,
  9490. 0x48, 0x30, 0x16, 0x5a, 0x8f, 0xdc, 0xf9, 0xff,
  9491. 0x1d, 0xe9, 0xa1, 0xd8, 0xe6, 0xb4, 0x47, 0xef,
  9492. 0x6e, 0xf7, 0xb7, 0x98, 0x28, 0x66, 0x6e, 0x45,
  9493. 0x81, 0xe7, 0x90, 0x12, 0xaf, 0x34, 0xdd, 0xd9,
  9494. 0xe2, 0xf0, 0x37, 0x58, 0x9b, 0x29, 0x2d, 0xb3,
  9495. 0xe6, 0x7c, 0x03, 0x67, 0x45, 0xfa, 0x22, 0xe7,
  9496. 0xe9, 0xb7, 0x37, 0x3b
  9497. };
  9498. WOLFSSL_SMALL_STACK_STATIC const byte t2[] =
  9499. {
  9500. 0xdc, 0xf5, 0x66, 0xff, 0x29, 0x1c, 0x25, 0xbb,
  9501. 0xb8, 0x56, 0x8f, 0xc3, 0xd3, 0x76, 0xa6, 0xd9
  9502. };
  9503. #endif /* WOLFSSL_AES_192 */
  9504. #ifdef WOLFSSL_AES_128
  9505. /* The following is an interesting test case from the example
  9506. * FIPS test vectors for AES-GCM. IVlen = 1 byte */
  9507. WOLFSSL_SMALL_STACK_STATIC const byte p3[] =
  9508. {
  9509. 0x57, 0xce, 0x45, 0x1f, 0xa5, 0xe2, 0x35, 0xa5,
  9510. 0x8e, 0x1a, 0xa2, 0x3b, 0x77, 0xcb, 0xaf, 0xe2
  9511. };
  9512. WOLFSSL_SMALL_STACK_STATIC const byte k3[] =
  9513. {
  9514. 0xbb, 0x01, 0xd7, 0x03, 0x81, 0x1c, 0x10, 0x1a,
  9515. 0x35, 0xe0, 0xff, 0xd2, 0x91, 0xba, 0xf2, 0x4b
  9516. };
  9517. WOLFSSL_SMALL_STACK_STATIC const byte iv3[] =
  9518. {
  9519. 0xca
  9520. };
  9521. WOLFSSL_SMALL_STACK_STATIC const byte c3[] =
  9522. {
  9523. 0x6b, 0x5f, 0xb3, 0x9d, 0xc1, 0xc5, 0x7a, 0x4f,
  9524. 0xf3, 0x51, 0x4d, 0xc2, 0xd5, 0xf0, 0xd0, 0x07
  9525. };
  9526. WOLFSSL_SMALL_STACK_STATIC const byte a3[] =
  9527. {
  9528. 0x40, 0xfc, 0xdc, 0xd7, 0x4a, 0xd7, 0x8b, 0xf1,
  9529. 0x3e, 0x7c, 0x60, 0x55, 0x50, 0x51, 0xdd, 0x54
  9530. };
  9531. WOLFSSL_SMALL_STACK_STATIC const byte t3[] =
  9532. {
  9533. 0x06, 0x90, 0xed, 0x01, 0x34, 0xdd, 0xc6, 0x95,
  9534. 0x31, 0x2e, 0x2a, 0xf9, 0x57, 0x7a, 0x1e, 0xa6
  9535. };
  9536. #endif /* WOLFSSL_AES_128 */
  9537. #ifdef WOLFSSL_AES_256
  9538. int ivlen;
  9539. #endif
  9540. #endif
  9541. byte resultT[sizeof(t1) + AES_BLOCK_SIZE];
  9542. byte resultP[sizeof(p) + AES_BLOCK_SIZE];
  9543. byte resultC[sizeof(p) + AES_BLOCK_SIZE];
  9544. int result = 0;
  9545. int ret;
  9546. #ifdef WOLFSSL_AES_256
  9547. #if !(defined(WOLF_CRYPTO_CB) && defined(HAVE_INTEL_QA_SYNC))
  9548. int alen;
  9549. #endif
  9550. #if !defined(WOLFSSL_AFALG_XILINX_AES) && !defined(WOLFSSL_XILINX_CRYPT)
  9551. int plen;
  9552. #endif
  9553. #endif
  9554. #if defined(WOLFSSL_XILINX_CRYPT_VERSAL)
  9555. byte buf[sizeof(p) + AES_BLOCK_SIZE];
  9556. byte bufA[sizeof(a) + 1];
  9557. byte *large_aad = (byte*)XMALLOC((size_t)1024 + 16, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9558. #endif
  9559. #if !defined(BENCH_EMBEDDED) && !defined(HAVE_CAVIUM)
  9560. #if !defined(BENCH_AESGCM_LARGE)
  9561. #define BENCH_AESGCM_LARGE 1024
  9562. #endif
  9563. #ifndef WOLFSSL_NO_MALLOC
  9564. byte *large_input = (byte *)XMALLOC(BENCH_AESGCM_LARGE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9565. byte *large_output = (byte *)XMALLOC(BENCH_AESGCM_LARGE + AES_BLOCK_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9566. byte *large_outdec = (byte *)XMALLOC(BENCH_AESGCM_LARGE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9567. if ((! large_input) || (! large_output) || (! large_outdec))
  9568. ERROR_OUT(MEMORY_E, out);
  9569. #else
  9570. byte large_input[BENCH_AESGCM_LARGE];
  9571. byte large_output[BENCH_AESGCM_LARGE];
  9572. byte large_outdec[BENCH_AESGCM_LARGE];
  9573. #endif
  9574. XMEMSET(large_input, 0, BENCH_AESGCM_LARGE);
  9575. XMEMSET(large_output, 0, BENCH_AESGCM_LARGE + AES_BLOCK_SIZE);
  9576. XMEMSET(large_outdec, 0, BENCH_AESGCM_LARGE);
  9577. #endif
  9578. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9579. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  9580. ERROR_OUT(-6342, out);
  9581. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  9582. ERROR_OUT(-6343, out);
  9583. #endif
  9584. (void)result;
  9585. XMEMSET(resultT, 0, sizeof(resultT));
  9586. XMEMSET(resultC, 0, sizeof(resultC));
  9587. XMEMSET(resultP, 0, sizeof(resultP));
  9588. if (wc_AesInit(enc, HEAP_HINT, devId) != 0) {
  9589. ERROR_OUT(-6300, out);
  9590. }
  9591. if (wc_AesInit(dec, HEAP_HINT, devId) != 0) {
  9592. ERROR_OUT(-6301, out);
  9593. }
  9594. #ifdef WOLFSSL_AES_256
  9595. result = wc_AesGcmSetKey(enc, k1, sizeof(k1));
  9596. if (result != 0)
  9597. ERROR_OUT(-6302, out);
  9598. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  9599. result = wc_AesGcmEncrypt(enc, resultC, p, sizeof(p), iv1, sizeof(iv1),
  9600. resultT, sizeof(t1), a, sizeof(a));
  9601. #if defined(WOLFSSL_ASYNC_CRYPT)
  9602. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  9603. #endif
  9604. if (result != 0)
  9605. ERROR_OUT(-6303, out);
  9606. if (XMEMCMP(c1, resultC, sizeof(c1)))
  9607. ERROR_OUT(-6304, out);
  9608. if (XMEMCMP(t1, resultT, sizeof(t1)))
  9609. ERROR_OUT(-6305, out);
  9610. #ifdef HAVE_AES_DECRYPT
  9611. result = wc_AesGcmSetKey(dec, k1, sizeof(k1));
  9612. if (result != 0)
  9613. ERROR_OUT(-6306, out);
  9614. result = wc_AesGcmDecrypt(dec, resultP, resultC, sizeof(c1),
  9615. iv1, sizeof(iv1), resultT, sizeof(t1), a, sizeof(a));
  9616. #if defined(WOLFSSL_ASYNC_CRYPT)
  9617. result = wc_AsyncWait(result, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  9618. #endif
  9619. if (result != 0)
  9620. ERROR_OUT(-6307, out);
  9621. if (XMEMCMP(p, resultP, sizeof(p)))
  9622. ERROR_OUT(-6308, out);
  9623. #endif /* HAVE_AES_DECRYPT */
  9624. /* Large buffer test */
  9625. #ifdef BENCH_AESGCM_LARGE
  9626. /* setup test buffer */
  9627. for (alen=0; alen<BENCH_AESGCM_LARGE; alen++)
  9628. large_input[alen] = (byte)alen;
  9629. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  9630. result = wc_AesGcmEncrypt(enc, large_output, large_input,
  9631. BENCH_AESGCM_LARGE, iv1, sizeof(iv1),
  9632. resultT, sizeof(t1), a, sizeof(a));
  9633. #if defined(WOLFSSL_ASYNC_CRYPT)
  9634. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  9635. #endif
  9636. if (result != 0)
  9637. ERROR_OUT(-6309, out);
  9638. #ifdef HAVE_AES_DECRYPT
  9639. result = wc_AesGcmDecrypt(dec, large_outdec, large_output,
  9640. BENCH_AESGCM_LARGE, iv1, sizeof(iv1), resultT,
  9641. sizeof(t1), a, sizeof(a));
  9642. #if defined(WOLFSSL_ASYNC_CRYPT)
  9643. result = wc_AsyncWait(result, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  9644. #endif
  9645. if (result != 0)
  9646. ERROR_OUT(-6310, out);
  9647. if (XMEMCMP(large_input, large_outdec, BENCH_AESGCM_LARGE))
  9648. ERROR_OUT(-6311, out);
  9649. #endif /* HAVE_AES_DECRYPT */
  9650. #endif /* BENCH_AESGCM_LARGE */
  9651. #if defined(ENABLE_NON_12BYTE_IV_TEST) && defined(WOLFSSL_AES_256)
  9652. /* Variable IV length test */
  9653. for (ivlen=1; ivlen<(int)sizeof(k1); ivlen++) {
  9654. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  9655. result = wc_AesGcmEncrypt(enc, resultC, p, sizeof(p), k1,
  9656. (word32)ivlen, resultT, sizeof(t1), a, sizeof(a));
  9657. #if defined(WOLFSSL_ASYNC_CRYPT)
  9658. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  9659. #endif
  9660. if (result != 0)
  9661. ERROR_OUT(-6312, out);
  9662. #ifdef HAVE_AES_DECRYPT
  9663. result = wc_AesGcmDecrypt(dec, resultP, resultC, sizeof(c1), k1,
  9664. (word32)ivlen, resultT, sizeof(t1), a, sizeof(a));
  9665. #if defined(WOLFSSL_ASYNC_CRYPT)
  9666. result = wc_AsyncWait(result, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  9667. #endif
  9668. if (result != 0)
  9669. ERROR_OUT(-6313, out);
  9670. #endif /* HAVE_AES_DECRYPT */
  9671. }
  9672. #endif
  9673. #if !(defined(WOLF_CRYPTO_CB) && defined(HAVE_INTEL_QA_SYNC))
  9674. /* Variable authenticated data length test */
  9675. for (alen=0; alen<(int)sizeof(p); alen++) {
  9676. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  9677. result = wc_AesGcmEncrypt(enc, resultC, p, sizeof(p), iv1,
  9678. sizeof(iv1), resultT, sizeof(t1), p, (word32)alen);
  9679. #if defined(WOLFSSL_ASYNC_CRYPT)
  9680. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  9681. #endif
  9682. if (result != 0)
  9683. ERROR_OUT(-6314, out);
  9684. #ifdef HAVE_AES_DECRYPT
  9685. result = wc_AesGcmDecrypt(dec, resultP, resultC, sizeof(c1), iv1,
  9686. sizeof(iv1), resultT, sizeof(t1), p, (word32)alen);
  9687. #if defined(WOLFSSL_ASYNC_CRYPT)
  9688. result = wc_AsyncWait(result, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  9689. #endif
  9690. if (result != 0)
  9691. ERROR_OUT(-6315, out);
  9692. #endif /* HAVE_AES_DECRYPT */
  9693. }
  9694. #if defined(WOLFSSL_XILINX_CRYPT_VERSAL)
  9695. if (! large_aad)
  9696. ERROR_OUT(MEMORY_E, out);
  9697. XMEMSET(large_aad, 0, 1024+16);
  9698. /* Variable authenticated data length test */
  9699. for (alen=0; alen<=1024; alen+=16) {
  9700. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  9701. result = wc_AesGcmEncrypt(enc, resultC, p, sizeof(p), iv1,
  9702. sizeof(iv1), resultT, sizeof(t1), large_aad, (word32)alen);
  9703. if (result != 0)
  9704. ERROR_OUT(-6316, out);
  9705. #ifdef HAVE_AES_DECRYPT
  9706. result = wc_AesGcmDecrypt(dec, resultP, resultC, sizeof(c1), iv1,
  9707. sizeof(iv1), resultT, sizeof(t1), large_aad, (word32)alen);
  9708. if (result != 0)
  9709. ERROR_OUT(-6317, out);
  9710. #endif /* HAVE_AES_DECRYPT */
  9711. }
  9712. /* Test unaligned memory of all potential arguments */
  9713. result = wc_AesGcmSetKey(enc, k1, sizeof(k1));
  9714. if (result != 0)
  9715. ERROR_OUT(-6318, out);
  9716. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  9717. XMEMCPY(&buf[1], p, sizeof(p));
  9718. XMEMCPY(&bufA[1], a, sizeof(a));
  9719. result = wc_AesGcmEncrypt(enc, &resultC[1], &buf[1], sizeof(p), iv1, sizeof(iv1),
  9720. &resultT[1], sizeof(t1), &bufA[1], sizeof(a));
  9721. if (result != 0)
  9722. ERROR_OUT(-6319, out);
  9723. if (XMEMCMP(c1, &resultC[1], sizeof(c1)))
  9724. ERROR_OUT(-6320, out);
  9725. if (XMEMCMP(t1, &resultT[1], sizeof(t1)))
  9726. ERROR_OUT(-6321, out);
  9727. #ifdef HAVE_AES_DECRYPT
  9728. result = wc_AesGcmSetKey(dec, k1, sizeof(k1));
  9729. if (result != 0)
  9730. ERROR_OUT(-6322, out);
  9731. result = wc_AesGcmDecrypt(dec, &resultP[1], &resultC[1], sizeof(c1),
  9732. iv1, sizeof(iv1), &resultT[1], sizeof(t1), &bufA[1], sizeof(a));
  9733. if (result != 0)
  9734. ERROR_OUT(-6323, out);
  9735. if (XMEMCMP(p, &resultP[1], sizeof(p)))
  9736. ERROR_OUT(-6324, out);
  9737. #endif /* HAVE_AES_DECRYPT */
  9738. #endif /* Xilinx Versal */
  9739. #endif
  9740. #if !defined(WOLFSSL_AFALG_XILINX_AES) && !defined(WOLFSSL_XILINX_CRYPT)
  9741. #ifdef BENCH_AESGCM_LARGE
  9742. /* Variable plain text length test */
  9743. for (plen=1; plen<BENCH_AESGCM_LARGE; plen++) {
  9744. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  9745. result = wc_AesGcmEncrypt(enc, large_output, large_input,
  9746. plen, iv1, sizeof(iv1), resultT,
  9747. sizeof(t1), a, sizeof(a));
  9748. #if defined(WOLFSSL_ASYNC_CRYPT)
  9749. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  9750. #endif
  9751. if (result != 0)
  9752. ERROR_OUT(-6316, out);
  9753. #ifdef HAVE_AES_DECRYPT
  9754. result = wc_AesGcmDecrypt(dec, large_outdec, large_output,
  9755. plen, iv1, sizeof(iv1), resultT,
  9756. sizeof(t1), a, sizeof(a));
  9757. #if defined(WOLFSSL_ASYNC_CRYPT)
  9758. result = wc_AsyncWait(result, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  9759. #endif
  9760. if (result != 0)
  9761. ERROR_OUT(-6317, out);
  9762. #endif /* HAVE_AES_DECRYPT */
  9763. }
  9764. #else /* BENCH_AESGCM_LARGE */
  9765. /* Variable plain text length test */
  9766. for (plen=1; plen<(int)sizeof(p); plen++) {
  9767. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  9768. result = wc_AesGcmEncrypt(enc, resultC, p, (word32)plen, iv1,
  9769. sizeof(iv1), resultT, sizeof(t1), a, sizeof(a));
  9770. #if defined(WOLFSSL_ASYNC_CRYPT)
  9771. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  9772. #endif
  9773. if (result != 0)
  9774. ERROR_OUT(-6318, out);
  9775. #ifdef HAVE_AES_DECRYPT
  9776. result = wc_AesGcmDecrypt(dec, resultP, resultC, (word32)plen, iv1,
  9777. sizeof(iv1), resultT, sizeof(t1), a, sizeof(a));
  9778. #if defined(WOLFSSL_ASYNC_CRYPT)
  9779. result = wc_AsyncWait(result, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  9780. #endif
  9781. if (result != 0)
  9782. ERROR_OUT(-6319, out);
  9783. #endif /* HAVE_AES_DECRYPT */
  9784. }
  9785. #endif /* BENCH_AESGCM_LARGE */
  9786. #endif
  9787. #endif /* WOLFSSL_AES_256 */
  9788. /* test with IV != 12 bytes */
  9789. #ifdef ENABLE_NON_12BYTE_IV_TEST
  9790. XMEMSET(resultT, 0, sizeof(resultT));
  9791. XMEMSET(resultC, 0, sizeof(resultC));
  9792. XMEMSET(resultP, 0, sizeof(resultP));
  9793. #ifdef WOLFSSL_AES_192
  9794. wc_AesGcmSetKey(enc, k2, sizeof(k2));
  9795. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  9796. result = wc_AesGcmEncrypt(enc, resultC, p, sizeof(p), iv2, sizeof(iv2),
  9797. resultT, sizeof(t1), a, sizeof(a));
  9798. #if defined(WOLFSSL_ASYNC_CRYPT)
  9799. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  9800. #endif
  9801. if (result != 0)
  9802. ERROR_OUT(-6320, out);
  9803. if (XMEMCMP(c2, resultC, sizeof(c2)))
  9804. ERROR_OUT(-6321, out);
  9805. if (XMEMCMP(t2, resultT, sizeof(t1)))
  9806. ERROR_OUT(-6322, out);
  9807. #ifdef HAVE_AES_DECRYPT
  9808. result = wc_AesGcmDecrypt(enc, resultP, resultC, sizeof(c1),
  9809. iv2, sizeof(iv2), resultT, sizeof(t1), a, sizeof(a));
  9810. #if defined(WOLFSSL_ASYNC_CRYPT)
  9811. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  9812. #endif
  9813. if (result != 0)
  9814. ERROR_OUT(-6323, out);
  9815. if (XMEMCMP(p, resultP, sizeof(p)))
  9816. ERROR_OUT(-6324, out);
  9817. #endif /* HAVE_AES_DECRYPT */
  9818. XMEMSET(resultT, 0, sizeof(resultT));
  9819. XMEMSET(resultC, 0, sizeof(resultC));
  9820. XMEMSET(resultP, 0, sizeof(resultP));
  9821. #endif /* WOLFSSL_AES_192 */
  9822. #ifdef WOLFSSL_AES_128
  9823. wc_AesGcmSetKey(enc, k3, sizeof(k3));
  9824. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  9825. result = wc_AesGcmEncrypt(enc, resultC, p3, sizeof(p3), iv3, sizeof(iv3),
  9826. resultT, sizeof(t3), a3, sizeof(a3));
  9827. #if defined(WOLFSSL_ASYNC_CRYPT)
  9828. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  9829. #endif
  9830. if (result != 0)
  9831. ERROR_OUT(-6325, out);
  9832. if (XMEMCMP(c3, resultC, sizeof(c3)))
  9833. ERROR_OUT(-6326, out);
  9834. if (XMEMCMP(t3, resultT, sizeof(t3)))
  9835. ERROR_OUT(-6327, out);
  9836. #ifdef HAVE_AES_DECRYPT
  9837. result = wc_AesGcmDecrypt(enc, resultP, resultC, sizeof(c3),
  9838. iv3, sizeof(iv3), resultT, sizeof(t3), a3, sizeof(a3));
  9839. #if defined(WOLFSSL_ASYNC_CRYPT)
  9840. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  9841. #endif
  9842. if (result != 0)
  9843. ERROR_OUT(-6328, out);
  9844. if (XMEMCMP(p3, resultP, sizeof(p3)))
  9845. ERROR_OUT(-6329, out);
  9846. #endif /* HAVE_AES_DECRYPT */
  9847. #endif /* WOLFSSL_AES_128 */
  9848. #endif /* ENABLE_NON_12BYTE_IV_TEST */
  9849. #if defined(WOLFSSL_AES_256) && !defined(WOLFSSL_AFALG_XILINX_AES) && \
  9850. !defined(WOLFSSL_XILINX_CRYPT) && \
  9851. !(defined(WOLF_CRYPTO_CB) && \
  9852. defined(HAVE_INTEL_QA_SYNC) || defined(HAVE_CAVIUM_OCTEON_SYNC))
  9853. XMEMSET(resultT, 0, sizeof(resultT));
  9854. XMEMSET(resultC, 0, sizeof(resultC));
  9855. XMEMSET(resultP, 0, sizeof(resultP));
  9856. wc_AesGcmSetKey(enc, k1, sizeof(k1));
  9857. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  9858. result = wc_AesGcmEncrypt(enc, resultC, p, sizeof(p), iv1, sizeof(iv1),
  9859. resultT + 1, sizeof(t1) - 1, a, sizeof(a));
  9860. #if defined(WOLFSSL_ASYNC_CRYPT)
  9861. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  9862. #endif
  9863. if (result != 0)
  9864. ERROR_OUT(-6330, out);
  9865. if (XMEMCMP(c1, resultC, sizeof(c1)))
  9866. ERROR_OUT(-6331, out);
  9867. if (XMEMCMP(t1, resultT + 1, sizeof(t1) - 1))
  9868. ERROR_OUT(-6332, out);
  9869. #ifdef HAVE_AES_DECRYPT
  9870. result = wc_AesGcmDecrypt(enc, resultP, resultC, sizeof(p),
  9871. iv1, sizeof(iv1), resultT + 1, sizeof(t1) - 1, a, sizeof(a));
  9872. #if defined(WOLFSSL_ASYNC_CRYPT)
  9873. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  9874. #endif
  9875. if (result != 0)
  9876. ERROR_OUT(-6333, out);
  9877. if (XMEMCMP(p, resultP, sizeof(p)))
  9878. ERROR_OUT(-6334, out);
  9879. #endif /* HAVE_AES_DECRYPT */
  9880. #endif /* WOLFSSL_AES_256 */
  9881. #if !defined(HAVE_FIPS) || \
  9882. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))
  9883. /* Test encrypt with internally generated IV */
  9884. #if defined(WOLFSSL_AES_256) && !(defined(WC_NO_RNG) || defined(HAVE_SELFTEST)) \
  9885. && !(defined(WOLF_CRYPTO_CB) && defined(HAVE_CAVIUM_OCTEON_SYNC))
  9886. {
  9887. WC_RNG rng;
  9888. byte randIV[12];
  9889. result = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  9890. if (result != 0)
  9891. ERROR_OUT(-6335, out);
  9892. XMEMSET(randIV, 0, sizeof(randIV));
  9893. XMEMSET(resultT, 0, sizeof(resultT));
  9894. XMEMSET(resultC, 0, sizeof(resultC));
  9895. XMEMSET(resultP, 0, sizeof(resultP));
  9896. wc_AesGcmSetKey(enc, k1, sizeof(k1));
  9897. result = wc_AesGcmSetIV(enc, sizeof(randIV), NULL, 0, &rng);
  9898. if (result != 0)
  9899. ERROR_OUT(-6336, out);
  9900. result = wc_AesGcmEncrypt_ex(enc,
  9901. resultC, p, sizeof(p),
  9902. randIV, sizeof(randIV),
  9903. resultT, sizeof(t1),
  9904. a, sizeof(a));
  9905. #if defined(WOLFSSL_ASYNC_CRYPT)
  9906. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  9907. #endif
  9908. if (result != 0)
  9909. ERROR_OUT(-6337, out);
  9910. /* Check the IV has been set. */
  9911. {
  9912. word32 i, ivSum = 0;
  9913. for (i = 0; i < sizeof(randIV); i++)
  9914. ivSum += randIV[i];
  9915. if (ivSum == 0)
  9916. ERROR_OUT(-6338, out);
  9917. }
  9918. #ifdef HAVE_AES_DECRYPT
  9919. wc_AesGcmSetKey(dec, k1, sizeof(k1));
  9920. result = wc_AesGcmSetIV(dec, sizeof(randIV), NULL, 0, &rng);
  9921. if (result != 0)
  9922. ERROR_OUT(-6339, out);
  9923. result = wc_AesGcmDecrypt(dec,
  9924. resultP, resultC, sizeof(c1),
  9925. randIV, sizeof(randIV),
  9926. resultT, sizeof(t1),
  9927. a, sizeof(a));
  9928. #if defined(WOLFSSL_ASYNC_CRYPT)
  9929. result = wc_AsyncWait(result, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  9930. #endif
  9931. if (result != 0)
  9932. ERROR_OUT(-6340, out);
  9933. if (XMEMCMP(p, resultP, sizeof(p)))
  9934. ERROR_OUT(-6341, out);
  9935. #endif /* HAVE_AES_DECRYPT */
  9936. wc_FreeRng(&rng);
  9937. }
  9938. #endif /* WOLFSSL_AES_256 && !(WC_NO_RNG || HAVE_SELFTEST) */
  9939. #endif /* HAVE_FIPS_VERSION >= 2 */
  9940. #if !defined(WOLFSSL_AFALG_XILINX_AES) && !defined(WOLFSSL_XILINX_CRYPT)
  9941. #ifdef WOLFSSL_AES_256
  9942. #ifdef WOLFSSL_AESGCM_STREAM
  9943. result = wc_AesGcmEncryptInit(enc, k1, sizeof(k1), iv1, sizeof(iv1));
  9944. if (result != 0)
  9945. ERROR_OUT(-6360, out);
  9946. result = wc_AesGcmEncryptUpdate(enc, resultC, p, sizeof(p), a, sizeof(a));
  9947. if (result != 0)
  9948. ERROR_OUT(-6361, out);
  9949. result = wc_AesGcmEncryptFinal(enc, resultT, sizeof(t1));
  9950. if (result != 0)
  9951. ERROR_OUT(-6362, out);
  9952. if (XMEMCMP(resultC, c1, sizeof(c1)) != 0)
  9953. ERROR_OUT(-6363, out);
  9954. if (XMEMCMP(resultT, t1, sizeof(t1)) != 0)
  9955. ERROR_OUT(-6364, out);
  9956. #ifdef HAVE_AES_DECRYPT
  9957. result = wc_AesGcmDecryptInit(enc, k1, sizeof(k1), iv1, sizeof(iv1));
  9958. if (result != 0)
  9959. ERROR_OUT(-6370, out);
  9960. result = wc_AesGcmDecryptUpdate(enc, resultP, c1, sizeof(c1), a, sizeof(a));
  9961. if (result != 0)
  9962. ERROR_OUT(-6371, out);
  9963. result = wc_AesGcmDecryptFinal(enc, t1, sizeof(t1));
  9964. if (result != 0)
  9965. ERROR_OUT(-6372, out);
  9966. if (XMEMCMP(resultP, p, sizeof(p)) != 0)
  9967. ERROR_OUT(-6373, out);
  9968. #endif
  9969. /* alen is the size to pass in with each update. */
  9970. for (alen = 1; alen < AES_BLOCK_SIZE + 1; alen++) {
  9971. result = wc_AesGcmEncryptInit(enc, k1, sizeof(k1), iv1, sizeof(iv1));
  9972. if (result != 0)
  9973. ERROR_OUT(-6380, out);
  9974. /* plen is the offset into AAD to update with. */
  9975. for (plen = 0; plen < (int)sizeof(a); plen += alen) {
  9976. int len = sizeof(a) - plen;
  9977. if (len > alen) len = alen;
  9978. result = wc_AesGcmEncryptUpdate(enc, NULL, NULL, 0, a + plen, len);
  9979. if (result != 0)
  9980. ERROR_OUT(-6381, out);
  9981. }
  9982. /* plen is the offset into plaintext to update with. */
  9983. for (plen = 0; plen < (int)sizeof(p); plen += alen) {
  9984. int len = sizeof(p) - plen;
  9985. if (len > alen) len = alen;
  9986. result = wc_AesGcmEncryptUpdate(enc, resultC + plen, p + plen, len,
  9987. NULL, 0);
  9988. if (result != 0)
  9989. ERROR_OUT(-6382, out);
  9990. }
  9991. result = wc_AesGcmEncryptFinal(enc, resultT, sizeof(t1));
  9992. if (result != 0)
  9993. ERROR_OUT(-6383, out);
  9994. if (XMEMCMP(resultC, c1, sizeof(c1)) != 0)
  9995. ERROR_OUT(-6384, out);
  9996. if (XMEMCMP(resultT, t1, sizeof(t1)) != 0)
  9997. ERROR_OUT(-6385, out);
  9998. }
  9999. #ifdef HAVE_AES_DECRYPT
  10000. for (alen = 1; alen < AES_BLOCK_SIZE + 1; alen++) {
  10001. result = wc_AesGcmDecryptInit(enc, k1, sizeof(k1), iv1, sizeof(iv1));
  10002. if (result != 0)
  10003. ERROR_OUT(-6390, out);
  10004. /* plen is the offset into AAD to update with. */
  10005. for (plen = 0; plen < (int)sizeof(a); plen += alen) {
  10006. int len = sizeof(a) - plen;
  10007. if (len > alen) len = alen;
  10008. result = wc_AesGcmDecryptUpdate(enc, NULL, NULL, 0, a + plen, len);
  10009. if (result != 0)
  10010. ERROR_OUT(-6391, out);
  10011. }
  10012. /* plen is the offset into cipher text to update with. */
  10013. for (plen = 0; plen < (int)sizeof(c1); plen += alen) {
  10014. int len = sizeof(c1) - plen;
  10015. if (len > alen) len = alen;
  10016. result = wc_AesGcmDecryptUpdate(enc, resultP + plen, c1 + plen, len,
  10017. NULL, 0);
  10018. if (result != 0)
  10019. ERROR_OUT(-6392, out);
  10020. }
  10021. result = wc_AesGcmDecryptFinal(enc, t1, sizeof(t1));
  10022. if (result != 0)
  10023. ERROR_OUT(-6393, out);
  10024. if (XMEMCMP(resultP, p, sizeof(p)) != 0)
  10025. ERROR_OUT(-6394, out);
  10026. }
  10027. #endif /* HAVE_AES_DECRYPT */
  10028. #ifdef BENCH_AESGCM_LARGE
  10029. /* setup test buffer */
  10030. result = wc_AesGcmEncryptInit(enc, k1, sizeof(k1), iv1, sizeof(iv1));
  10031. if (result != 0)
  10032. ERROR_OUT(-6360, out);
  10033. result = wc_AesGcmEncryptUpdate(enc, large_output, large_input,
  10034. BENCH_AESGCM_LARGE, a, sizeof(a));
  10035. if (result != 0)
  10036. ERROR_OUT(-6361, out);
  10037. result = wc_AesGcmEncryptFinal(enc, resultT, sizeof(t1));
  10038. if (result != 0)
  10039. ERROR_OUT(-6362, out);
  10040. #ifdef HAVE_AES_DECRYPT
  10041. result = wc_AesGcmDecryptInit(enc, k1, sizeof(k1), iv1, sizeof(iv1));
  10042. if (result != 0)
  10043. ERROR_OUT(-6363, out);
  10044. result = wc_AesGcmDecryptUpdate(enc, large_outdec, large_output,
  10045. BENCH_AESGCM_LARGE, a, sizeof(a));
  10046. if (result != 0)
  10047. ERROR_OUT(-6364, out);
  10048. result = wc_AesGcmDecryptFinal(enc, resultT, sizeof(t1));
  10049. if (result != 0)
  10050. ERROR_OUT(-6365, out);
  10051. if (XMEMCMP(large_input, large_outdec, BENCH_AESGCM_LARGE))
  10052. ERROR_OUT(-6366, out);
  10053. #endif /* HAVE_AES_DECRYPT */
  10054. #endif /* BENCH_AESGCM_LARGE */
  10055. #endif /* WOLFSSL_AESGCM_STREAM */
  10056. #endif /* WOLFSSL_AES_256 */
  10057. #endif /* !WOLFSSL_AFALG_XILINX_AES && !WOLFSSL_XILINX_CRYPT */
  10058. wc_AesFree(enc);
  10059. wc_AesFree(dec);
  10060. ret = 0;
  10061. out:
  10062. #if !defined(BENCH_EMBEDDED) && !defined(HAVE_CAVIUM) && \
  10063. !defined(WOLFSSL_NO_MALLOC)
  10064. if (large_input)
  10065. XFREE(large_input, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10066. if (large_output)
  10067. XFREE(large_output, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10068. if (large_outdec)
  10069. XFREE(large_outdec, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10070. #endif
  10071. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10072. if (enc)
  10073. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  10074. if (dec)
  10075. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  10076. #endif
  10077. return ret;
  10078. }
  10079. #ifdef WOLFSSL_AES_128
  10080. WOLFSSL_TEST_SUBROUTINE int gmac_test(void)
  10081. {
  10082. int ret;
  10083. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10084. Gmac *gmac;
  10085. #else
  10086. Gmac gmac[1];
  10087. #endif
  10088. WOLFSSL_SMALL_STACK_STATIC const byte k1[] =
  10089. {
  10090. 0x89, 0xc9, 0x49, 0xe9, 0xc8, 0x04, 0xaf, 0x01,
  10091. 0x4d, 0x56, 0x04, 0xb3, 0x94, 0x59, 0xf2, 0xc8
  10092. };
  10093. WOLFSSL_SMALL_STACK_STATIC const byte iv1[] =
  10094. {
  10095. 0xd1, 0xb1, 0x04, 0xc8, 0x15, 0xbf, 0x1e, 0x94,
  10096. 0xe2, 0x8c, 0x8f, 0x16
  10097. };
  10098. WOLFSSL_SMALL_STACK_STATIC const byte a1[] =
  10099. {
  10100. 0x82, 0xad, 0xcd, 0x63, 0x8d, 0x3f, 0xa9, 0xd9,
  10101. 0xf3, 0xe8, 0x41, 0x00, 0xd6, 0x1e, 0x07, 0x77
  10102. };
  10103. WOLFSSL_SMALL_STACK_STATIC const byte t1[] =
  10104. {
  10105. 0x88, 0xdb, 0x9d, 0x62, 0x17, 0x2e, 0xd0, 0x43,
  10106. 0xaa, 0x10, 0xf1, 0x6d, 0x22, 0x7d, 0xc4, 0x1b
  10107. };
  10108. #if (!defined(HAVE_FIPS) || \
  10109. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)))
  10110. /* FIPS builds only allow 16-byte auth tags. */
  10111. /* This sample uses a 15-byte auth tag. */
  10112. WOLFSSL_SMALL_STACK_STATIC const byte k2[] =
  10113. {
  10114. 0x40, 0xf7, 0xec, 0xb2, 0x52, 0x6d, 0xaa, 0xd4,
  10115. 0x74, 0x25, 0x1d, 0xf4, 0x88, 0x9e, 0xf6, 0x5b
  10116. };
  10117. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] =
  10118. {
  10119. 0xee, 0x9c, 0x6e, 0x06, 0x15, 0x45, 0x45, 0x03,
  10120. 0x1a, 0x60, 0x24, 0xa7
  10121. };
  10122. WOLFSSL_SMALL_STACK_STATIC const byte a2[] =
  10123. {
  10124. 0x94, 0x81, 0x2c, 0x87, 0x07, 0x4e, 0x15, 0x18,
  10125. 0x34, 0xb8, 0x35, 0xaf, 0x1c, 0xa5, 0x7e, 0x56
  10126. };
  10127. WOLFSSL_SMALL_STACK_STATIC const byte t2[] =
  10128. {
  10129. 0xc6, 0x81, 0x79, 0x8e, 0x3d, 0xda, 0xb0, 0x9f,
  10130. 0x8d, 0x83, 0xb0, 0xbb, 0x14, 0xb6, 0x91
  10131. };
  10132. #endif
  10133. byte tag[16];
  10134. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10135. if ((gmac = (Gmac *)XMALLOC(sizeof *gmac, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  10136. return -6409;
  10137. #endif
  10138. XMEMSET(gmac, 0, sizeof *gmac); /* clear context */
  10139. (void)wc_AesInit(&gmac->aes, HEAP_HINT, INVALID_DEVID); /* Make sure devId updated */
  10140. XMEMSET(tag, 0, sizeof(tag));
  10141. wc_GmacSetKey(gmac, k1, sizeof(k1));
  10142. wc_GmacUpdate(gmac, iv1, sizeof(iv1), a1, sizeof(a1), tag, sizeof(t1));
  10143. if (XMEMCMP(t1, tag, sizeof(t1)) != 0)
  10144. ERROR_OUT(-6400, out);
  10145. #if (!defined(HAVE_FIPS) || \
  10146. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)) )
  10147. XMEMSET(tag, 0, sizeof(tag));
  10148. wc_GmacSetKey(gmac, k2, sizeof(k2));
  10149. wc_GmacUpdate(gmac, iv2, sizeof(iv2), a2, sizeof(a2), tag, sizeof(t2));
  10150. if (XMEMCMP(t2, tag, sizeof(t2)) != 0)
  10151. ERROR_OUT(-6401, out);
  10152. #if !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && !defined(NO_AES_DECRYPT)
  10153. {
  10154. WOLFSSL_SMALL_STACK_STATIC const byte badT[] =
  10155. {
  10156. 0xde, 0xad, 0xbe, 0xef, 0x17, 0x2e, 0xd0, 0x43,
  10157. 0xaa, 0x10, 0xf1, 0x6d, 0x22, 0x7d, 0xc4, 0x1b
  10158. };
  10159. WC_RNG rng;
  10160. byte iv[12];
  10161. #ifndef HAVE_FIPS
  10162. if (wc_InitRng_ex(&rng, HEAP_HINT, devId) != 0)
  10163. ERROR_OUT(-6402, out);
  10164. #else
  10165. if (wc_InitRng(&rng) != 0)
  10166. ERROR_OUT(-6403, out);
  10167. #endif
  10168. if (wc_GmacVerify(k1, sizeof(k1), iv1, sizeof(iv1), a1, sizeof(a1),
  10169. t1, sizeof(t1)) != 0)
  10170. ERROR_OUT(-6404, out);
  10171. if (wc_GmacVerify(k1, sizeof(k1), iv1, sizeof(iv1), a1, sizeof(a1),
  10172. badT, sizeof(badT)) != AES_GCM_AUTH_E)
  10173. ERROR_OUT(-6405, out);
  10174. if (wc_GmacVerify(k2, sizeof(k2), iv2, sizeof(iv2), a2, sizeof(a2),
  10175. t2, sizeof(t2)) != 0)
  10176. ERROR_OUT(-6406, out);
  10177. XMEMSET(tag, 0, sizeof(tag));
  10178. XMEMSET(iv, 0, sizeof(iv));
  10179. if (wc_Gmac(k1, sizeof(k1), iv, sizeof(iv), a1, sizeof(a1),
  10180. tag, sizeof(tag), &rng) != 0)
  10181. ERROR_OUT(-6407, out);
  10182. if (wc_GmacVerify(k1, sizeof(k1), iv, sizeof(iv), a1, sizeof(a1),
  10183. tag, sizeof(tag)) != 0)
  10184. ERROR_OUT(-6408, out);
  10185. wc_FreeRng(&rng);
  10186. }
  10187. #endif /* !WC_NO_RNG && !HAVE_SELFTEST && !NO_AES_DECRYPT */
  10188. #endif /* HAVE_FIPS */
  10189. ret = 0;
  10190. out:
  10191. wc_AesFree(&gmac->aes);
  10192. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10193. XFREE(gmac, HEAP_HINT, DYNAMIC_TYPE_AES);
  10194. #endif
  10195. return ret;
  10196. }
  10197. #endif /* WOLFSSL_AES_128 */
  10198. #endif /* HAVE_AESGCM */
  10199. #if defined(HAVE_AESCCM)
  10200. #if defined(WOLFSSL_AES_256)
  10201. static int aesccm_256_test(void)
  10202. {
  10203. int ret;
  10204. /* Test vectors from NIST AES CCM 256-bit CAST Example #1 */
  10205. WOLFSSL_SMALL_STACK_STATIC const byte in_key[32] = {
  10206. 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
  10207. 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F,
  10208. 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  10209. 0x58, 0x59, 0x5A, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F
  10210. };
  10211. WOLFSSL_SMALL_STACK_STATIC const byte in_nonce[7] = {
  10212. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16};
  10213. WOLFSSL_SMALL_STACK_STATIC const byte in_auth[8] = {
  10214. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
  10215. WOLFSSL_SMALL_STACK_STATIC const byte in_plaintext[4] = {
  10216. 0x20, 0x21, 0x22, 0x23};
  10217. WOLFSSL_SMALL_STACK_STATIC const byte exp_ciphertext[4] = {
  10218. 0x8A, 0xB1, 0xA8, 0x74};
  10219. WOLFSSL_SMALL_STACK_STATIC const byte exp_tag[4] = {
  10220. 0x95, 0xFC, 0x08, 0x20};
  10221. byte output[sizeof(in_plaintext)];
  10222. byte atag[sizeof(exp_tag)];
  10223. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10224. Aes* aes = (Aes*)XMALLOC(sizeof(Aes), HEAP_HINT, DYNAMIC_TYPE_AES);
  10225. if (aes == NULL) {
  10226. return MEMORY_E;
  10227. }
  10228. #else
  10229. Aes aes[1];
  10230. #endif
  10231. ret = wc_AesInit(aes, HEAP_HINT, devId);
  10232. if (ret == 0) {
  10233. ret = wc_AesCcmSetKey(aes, in_key, sizeof(in_key));
  10234. }
  10235. if (ret == 0) {
  10236. ret = wc_AesCcmEncrypt(aes, output, in_plaintext, sizeof(in_plaintext),
  10237. in_nonce, sizeof(in_nonce),
  10238. atag, sizeof(atag),
  10239. in_auth, sizeof(in_auth));
  10240. }
  10241. /* Verify we produce the proper ciphertext and tag */
  10242. if (ret == 0 &&
  10243. (XMEMCMP(output, exp_ciphertext, sizeof(output)) ||
  10244. XMEMCMP(atag, exp_tag, sizeof(atag)))) {
  10245. ret = -1;
  10246. }
  10247. if (ret == 0) {
  10248. /* decrypt inline */
  10249. ret = wc_AesCcmDecrypt(aes, output, output, sizeof(output),
  10250. in_nonce, sizeof(in_nonce),
  10251. atag, sizeof(atag),
  10252. in_auth, sizeof(in_auth));
  10253. }
  10254. /* Verify decryption was successful */
  10255. if (ret == 0 &&
  10256. XMEMCMP(output, in_plaintext, sizeof(output))) {
  10257. ret = -1;
  10258. }
  10259. wc_AesFree(aes);
  10260. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10261. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_AES);
  10262. #endif
  10263. return ret;
  10264. }
  10265. #endif /* WOLFSSL_AES_256 */
  10266. #if defined(WOLFSSL_AES_128)
  10267. static int aesccm_128_test(void)
  10268. {
  10269. int ret;
  10270. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10271. Aes *enc;
  10272. #else
  10273. Aes enc[1];
  10274. #endif
  10275. /* key */
  10276. WOLFSSL_SMALL_STACK_STATIC const byte k[] =
  10277. {
  10278. 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
  10279. 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf
  10280. };
  10281. /* nonce */
  10282. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  10283. {
  10284. 0x00, 0x00, 0x00, 0x03, 0x02, 0x01, 0x00, 0xa0,
  10285. 0xa1, 0xa2, 0xa3, 0xa4, 0xa5
  10286. };
  10287. /* plaintext */
  10288. WOLFSSL_SMALL_STACK_STATIC const byte p[] =
  10289. {
  10290. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  10291. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  10292. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e
  10293. };
  10294. /* plaintext - long */
  10295. WOLFSSL_SMALL_STACK_STATIC const byte pl[] =
  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, 0x1f,
  10300. 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  10301. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
  10302. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  10303. 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
  10304. 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
  10305. 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
  10306. 0x50
  10307. };
  10308. WOLFSSL_SMALL_STACK_STATIC const byte a[] =
  10309. {
  10310. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
  10311. };
  10312. /* ciphertext */
  10313. WOLFSSL_SMALL_STACK_STATIC const byte c[] =
  10314. {
  10315. 0x58, 0x8c, 0x97, 0x9a, 0x61, 0xc6, 0x63, 0xd2,
  10316. 0xf0, 0x66, 0xd0, 0xc2, 0xc0, 0xf9, 0x89, 0x80,
  10317. 0x6d, 0x5f, 0x6b, 0x61, 0xda, 0xc3, 0x84
  10318. };
  10319. /* tag - authentication */
  10320. WOLFSSL_SMALL_STACK_STATIC const byte t[] =
  10321. {
  10322. 0x17, 0xe8, 0xd1, 0x2c, 0xfd, 0xf9, 0x26, 0xe0
  10323. };
  10324. /* ciphertext - long */
  10325. WOLFSSL_SMALL_STACK_STATIC const byte cl[] =
  10326. {
  10327. 0x58, 0x8c, 0x97, 0x9a, 0x61, 0xc6, 0x63, 0xd2,
  10328. 0xf0, 0x66, 0xd0, 0xc2, 0xc0, 0xf9, 0x89, 0x80,
  10329. 0x6d, 0x5f, 0x6b, 0x61, 0xda, 0xc3, 0x84, 0xe0,
  10330. 0x44, 0x2d, 0xbe, 0x25, 0xfa, 0x48, 0x2b, 0xa8,
  10331. 0x36, 0x0b, 0xbf, 0x01, 0xc0, 0x12, 0x45, 0xa4,
  10332. 0x82, 0x9f, 0x20, 0x6c, 0xc3, 0xd6, 0xae, 0x5b,
  10333. 0x54, 0x8d, 0xd0, 0xb1, 0x69, 0x2c, 0xec, 0x5e,
  10334. 0x95, 0xa5, 0x6b, 0x48, 0xc3, 0xc6, 0xc8, 0x9e,
  10335. 0xc7, 0x92, 0x98, 0x9d, 0x26, 0x7d, 0x2a, 0x10,
  10336. 0x0b
  10337. };
  10338. /* tag - authentication - long */
  10339. WOLFSSL_SMALL_STACK_STATIC const byte tl[] =
  10340. {
  10341. 0x89, 0xd8, 0xd2, 0x02, 0xc5, 0xcf, 0xae, 0xf4
  10342. };
  10343. /* tag - authentication - empty plaintext */
  10344. WOLFSSL_SMALL_STACK_STATIC const byte t_empty[] =
  10345. {
  10346. 0xe4, 0x28, 0x8a, 0xc3, 0x78, 0x00, 0x0f, 0xf5
  10347. };
  10348. byte t2[sizeof(t)];
  10349. byte p2[sizeof(p)];
  10350. byte c2[sizeof(c)];
  10351. byte iv2[sizeof(iv)];
  10352. byte pl2[sizeof(pl)];
  10353. byte cl2[sizeof(cl)];
  10354. byte tl2[sizeof(tl)];
  10355. byte t_empty2[sizeof(t_empty)];
  10356. int result;
  10357. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10358. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  10359. return -6521;
  10360. #endif
  10361. XMEMSET(enc, 0, sizeof *enc); /* clear context */
  10362. XMEMSET(t2, 0, sizeof(t2));
  10363. XMEMSET(c2, 0, sizeof(c2));
  10364. XMEMSET(p2, 0, sizeof(p2));
  10365. result = wc_AesInit(enc, HEAP_HINT, devId);
  10366. if (result != 0)
  10367. ERROR_OUT(-6499, out);
  10368. result = wc_AesCcmSetKey(enc, k, sizeof(k));
  10369. if (result != 0)
  10370. ERROR_OUT(-6500, out);
  10371. /* AES-CCM encrypt and decrypt both use AES encrypt internally */
  10372. result = wc_AesCcmEncrypt(enc, c2, p, sizeof(c2), iv, sizeof(iv),
  10373. t2, sizeof(t2), a, sizeof(a));
  10374. if (result != 0)
  10375. ERROR_OUT(-6501, out);
  10376. if (XMEMCMP(c, c2, sizeof(c2)))
  10377. ERROR_OUT(-6502, out);
  10378. if (XMEMCMP(t, t2, sizeof(t2)))
  10379. ERROR_OUT(-6503, out);
  10380. result = wc_AesCcmDecrypt(enc, p2, c2, sizeof(p2), iv, sizeof(iv),
  10381. t2, sizeof(t2), a, sizeof(a));
  10382. if (result != 0)
  10383. ERROR_OUT(-6504, out);
  10384. if (XMEMCMP(p, p2, sizeof(p2)))
  10385. ERROR_OUT(-6505, out);
  10386. /* Test the authentication failure */
  10387. t2[0]++; /* Corrupt the authentication tag. */
  10388. result = wc_AesCcmDecrypt(enc, p2, c, sizeof(p2), iv, sizeof(iv),
  10389. t2, sizeof(t2), a, sizeof(a));
  10390. if (result == 0)
  10391. ERROR_OUT(-6506, out);
  10392. /* Clear c2 to compare against p2. p2 should be set to zero in case of
  10393. * authentication fail. */
  10394. XMEMSET(c2, 0, sizeof(c2));
  10395. if (XMEMCMP(p2, c2, sizeof(p2)))
  10396. ERROR_OUT(-6507, out);
  10397. wc_AesFree(enc);
  10398. XMEMSET(enc, 0, sizeof(Aes)); /* clear context */
  10399. XMEMSET(t2, 0, sizeof(t2));
  10400. XMEMSET(c2, 0, sizeof(c2));
  10401. XMEMSET(p2, 0, sizeof(p2));
  10402. XMEMSET(iv2, 0, sizeof(iv2));
  10403. #ifndef HAVE_SELFTEST
  10404. /* selftest build does not have wc_AesCcmSetNonce() or
  10405. * wc_AesCcmEncrypt_ex() */
  10406. if (wc_AesCcmSetKey(enc, k, sizeof(k)) != 0)
  10407. ERROR_OUT(-6508, out);
  10408. if (wc_AesCcmSetNonce(enc, iv, sizeof(iv)) != 0)
  10409. ERROR_OUT(-6509, out);
  10410. if (wc_AesCcmEncrypt_ex(enc, c2, p, sizeof(c2), iv2, sizeof(iv2),
  10411. t2, sizeof(t2), a, sizeof(a)) != 0)
  10412. ERROR_OUT(-6510, out);
  10413. if (XMEMCMP(iv, iv2, sizeof(iv2)))
  10414. ERROR_OUT(-6511, out);
  10415. if (XMEMCMP(c, c2, sizeof(c2)))
  10416. ERROR_OUT(-6512, out);
  10417. if (XMEMCMP(t, t2, sizeof(t2)))
  10418. ERROR_OUT(-6513, out);
  10419. #endif
  10420. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  10421. /* test fail on invalid IV sizes */
  10422. result = wc_AesCcmSetKey(enc, k, sizeof(k));
  10423. if (result != 0)
  10424. ERROR_OUT(-6514, out);
  10425. /* AES-CCM encrypt and decrypt both use AES encrypt internally */
  10426. result = wc_AesCcmEncrypt(enc, c2, p, sizeof(c2), iv, sizeof(iv),
  10427. t2, 1, a, sizeof(a));
  10428. if (result == 0) {
  10429. ERROR_OUT(-6515, out);
  10430. }
  10431. #endif
  10432. /* AES-CCM encrypt and decrypt both use AES encrypt internally */
  10433. result = wc_AesCcmEncrypt(enc, cl2, pl, sizeof(cl2), iv, sizeof(iv),
  10434. tl2, sizeof(tl2), a, sizeof(a));
  10435. if (result != 0)
  10436. ERROR_OUT(-6516, out);
  10437. if (XMEMCMP(cl, cl2, sizeof(cl2)))
  10438. ERROR_OUT(-6517, out);
  10439. if (XMEMCMP(tl, tl2, sizeof(tl2)))
  10440. ERROR_OUT(-6518, out);
  10441. result = wc_AesCcmDecrypt(enc, pl2, cl2, sizeof(pl2), iv, sizeof(iv),
  10442. tl2, sizeof(tl2), a, sizeof(a));
  10443. if (result != 0)
  10444. ERROR_OUT(-6519, out);
  10445. if (XMEMCMP(pl, pl2, sizeof(pl2)))
  10446. ERROR_OUT(-6520, out);
  10447. /* test empty message as null input or output with nonzero inSz. */
  10448. result = wc_AesCcmEncrypt(enc, pl2 /* out */, NULL /* in */, 1 /* inSz */,
  10449. iv, sizeof(iv), t_empty2, sizeof(t_empty2),
  10450. a, sizeof(a));
  10451. if (result != BAD_FUNC_ARG)
  10452. ERROR_OUT(-6527, out);
  10453. result = wc_AesCcmEncrypt(enc, NULL /* out */, (const byte *)"" /* in */, 1 /* inSz */,
  10454. iv, sizeof(iv), t_empty2, sizeof(t_empty2),
  10455. a, sizeof(a));
  10456. if (result != BAD_FUNC_ARG)
  10457. ERROR_OUT(-6528, out);
  10458. result = wc_AesCcmDecrypt(enc, pl2, NULL /* in */, 1 /* inSz */,
  10459. iv, sizeof(iv), t_empty2, sizeof(t_empty2), a,
  10460. sizeof(a));
  10461. if (result != BAD_FUNC_ARG)
  10462. ERROR_OUT(-6529, out);
  10463. result = wc_AesCcmDecrypt(enc, NULL /* out */, (const byte *)"" /* in */, 1 /* inSz */,
  10464. iv, sizeof(iv), t_empty2, sizeof(t_empty2), a,
  10465. sizeof(a));
  10466. if (result != BAD_FUNC_ARG)
  10467. ERROR_OUT(-6530, out);
  10468. /* test empty message as null input and output with zero inSz --
  10469. * must either succeed, or fail early with BAD_FUNC_ARG.
  10470. */
  10471. result = wc_AesCcmEncrypt(enc, NULL /* out */, NULL /* in */, 0 /* inSz */,
  10472. iv, sizeof(iv), t_empty2, sizeof(t_empty2),
  10473. a, sizeof(a));
  10474. if (result != BAD_FUNC_ARG) {
  10475. if (result != 0)
  10476. ERROR_OUT(-6521, out);
  10477. if (XMEMCMP(t_empty, t_empty2, sizeof(t_empty2)))
  10478. ERROR_OUT(-6522, out);
  10479. result = wc_AesCcmDecrypt(enc, NULL /* out */, NULL /* in */,
  10480. 0 /* inSz */, iv, sizeof(iv), t_empty2,
  10481. sizeof(t_empty2), a, sizeof(a));
  10482. if (result != 0)
  10483. ERROR_OUT(-6523, out);
  10484. }
  10485. /* test empty message as zero-length string -- must work. */
  10486. result = wc_AesCcmEncrypt(enc, pl2, (const byte *)"", 0 /* inSz */, iv,
  10487. sizeof(iv), t_empty2, sizeof(t_empty2), a,
  10488. sizeof(a));
  10489. if (result != 0)
  10490. ERROR_OUT(-6524, out);
  10491. if (XMEMCMP(t_empty, t_empty2, sizeof(t_empty2)))
  10492. ERROR_OUT(-6525, out);
  10493. result = wc_AesCcmDecrypt(enc, pl2, (const byte *)"", 0 /* inSz */,
  10494. iv, sizeof(iv), t_empty2, sizeof(t_empty2), a,
  10495. sizeof(a));
  10496. if (result != 0)
  10497. ERROR_OUT(-6526, out);
  10498. wc_AesFree(enc);
  10499. ret = 0;
  10500. out:
  10501. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10502. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  10503. #endif
  10504. return ret;
  10505. }
  10506. #endif /* WOLFSSL_AES_128 */
  10507. WOLFSSL_TEST_SUBROUTINE int aesccm_test(void)
  10508. {
  10509. int ret = 0;
  10510. #ifdef WOLFSSL_AES_128
  10511. if (ret == 0)
  10512. ret = aesccm_128_test();
  10513. #endif
  10514. #ifdef WOLFSSL_AES_256
  10515. if (ret == 0)
  10516. ret = aesccm_256_test();
  10517. #endif
  10518. return ret;
  10519. }
  10520. #endif /* HAVE_AESCCM */
  10521. #ifdef HAVE_AES_KEYWRAP
  10522. #define MAX_KEYWRAP_TEST_OUTLEN 40
  10523. #define MAX_KEYWRAP_TEST_PLAINLEN 32
  10524. typedef struct keywrapVector {
  10525. const byte* kek;
  10526. const byte* data;
  10527. const byte* verify;
  10528. word32 kekLen;
  10529. word32 dataLen;
  10530. word32 verifyLen;
  10531. } keywrapVector;
  10532. WOLFSSL_TEST_SUBROUTINE int aeskeywrap_test(void)
  10533. {
  10534. int wrapSz, plainSz, testSz, i;
  10535. /* test vectors from RFC 3394 (kek, data, verify) */
  10536. #ifdef WOLFSSL_AES_128
  10537. /* Wrap 128 bits of Key Data with a 128-bit KEK */
  10538. WOLFSSL_SMALL_STACK_STATIC const byte k1[] = {
  10539. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  10540. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  10541. };
  10542. WOLFSSL_SMALL_STACK_STATIC const byte d1[] = {
  10543. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  10544. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF
  10545. };
  10546. WOLFSSL_SMALL_STACK_STATIC const byte v1[] = {
  10547. 0x1F, 0xA6, 0x8B, 0x0A, 0x81, 0x12, 0xB4, 0x47,
  10548. 0xAE, 0xF3, 0x4B, 0xD8, 0xFB, 0x5A, 0x7B, 0x82,
  10549. 0x9D, 0x3E, 0x86, 0x23, 0x71, 0xD2, 0xCF, 0xE5
  10550. };
  10551. #endif /* WOLFSSL_AES_128 */
  10552. #ifdef WOLFSSL_AES_192
  10553. /* Wrap 128 bits of Key Data with a 192-bit KEK */
  10554. WOLFSSL_SMALL_STACK_STATIC const byte k2[] = {
  10555. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  10556. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  10557. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17
  10558. };
  10559. WOLFSSL_SMALL_STACK_STATIC const byte d2[] = {
  10560. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  10561. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF
  10562. };
  10563. WOLFSSL_SMALL_STACK_STATIC const byte v2[] = {
  10564. 0x96, 0x77, 0x8B, 0x25, 0xAE, 0x6C, 0xA4, 0x35,
  10565. 0xF9, 0x2B, 0x5B, 0x97, 0xC0, 0x50, 0xAE, 0xD2,
  10566. 0x46, 0x8A, 0xB8, 0xA1, 0x7A, 0xD8, 0x4E, 0x5D
  10567. };
  10568. #endif
  10569. #ifdef WOLFSSL_AES_256
  10570. /* Wrap 128 bits of Key Data with a 256-bit KEK */
  10571. WOLFSSL_SMALL_STACK_STATIC const byte k3[] = {
  10572. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  10573. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  10574. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  10575. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F
  10576. };
  10577. WOLFSSL_SMALL_STACK_STATIC const byte d3[] = {
  10578. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  10579. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF
  10580. };
  10581. WOLFSSL_SMALL_STACK_STATIC const byte v3[] = {
  10582. 0x64, 0xE8, 0xC3, 0xF9, 0xCE, 0x0F, 0x5B, 0xA2,
  10583. 0x63, 0xE9, 0x77, 0x79, 0x05, 0x81, 0x8A, 0x2A,
  10584. 0x93, 0xC8, 0x19, 0x1E, 0x7D, 0x6E, 0x8A, 0xE7
  10585. };
  10586. #endif
  10587. #ifdef WOLFSSL_AES_192
  10588. /* Wrap 192 bits of Key Data with a 192-bit KEK */
  10589. WOLFSSL_SMALL_STACK_STATIC const byte k4[] = {
  10590. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  10591. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  10592. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17
  10593. };
  10594. WOLFSSL_SMALL_STACK_STATIC const byte d4[] = {
  10595. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  10596. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF,
  10597. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
  10598. };
  10599. WOLFSSL_SMALL_STACK_STATIC const byte v4[] = {
  10600. 0x03, 0x1D, 0x33, 0x26, 0x4E, 0x15, 0xD3, 0x32,
  10601. 0x68, 0xF2, 0x4E, 0xC2, 0x60, 0x74, 0x3E, 0xDC,
  10602. 0xE1, 0xC6, 0xC7, 0xDD, 0xEE, 0x72, 0x5A, 0x93,
  10603. 0x6B, 0xA8, 0x14, 0x91, 0x5C, 0x67, 0x62, 0xD2
  10604. };
  10605. #endif
  10606. #ifdef WOLFSSL_AES_256
  10607. /* Wrap 192 bits of Key Data with a 256-bit KEK */
  10608. WOLFSSL_SMALL_STACK_STATIC const byte k5[] = {
  10609. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  10610. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  10611. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  10612. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F
  10613. };
  10614. WOLFSSL_SMALL_STACK_STATIC const byte d5[] = {
  10615. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  10616. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF,
  10617. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
  10618. };
  10619. WOLFSSL_SMALL_STACK_STATIC const byte v5[] = {
  10620. 0xA8, 0xF9, 0xBC, 0x16, 0x12, 0xC6, 0x8B, 0x3F,
  10621. 0xF6, 0xE6, 0xF4, 0xFB, 0xE3, 0x0E, 0x71, 0xE4,
  10622. 0x76, 0x9C, 0x8B, 0x80, 0xA3, 0x2C, 0xB8, 0x95,
  10623. 0x8C, 0xD5, 0xD1, 0x7D, 0x6B, 0x25, 0x4D, 0xA1
  10624. };
  10625. /* Wrap 256 bits of Key Data with a 256-bit KEK */
  10626. WOLFSSL_SMALL_STACK_STATIC const byte k6[] = {
  10627. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  10628. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  10629. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  10630. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F
  10631. };
  10632. WOLFSSL_SMALL_STACK_STATIC const byte d6[] = {
  10633. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  10634. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF,
  10635. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  10636. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  10637. };
  10638. WOLFSSL_SMALL_STACK_STATIC const byte v6[] = {
  10639. 0x28, 0xC9, 0xF4, 0x04, 0xC4, 0xB8, 0x10, 0xF4,
  10640. 0xCB, 0xCC, 0xB3, 0x5C, 0xFB, 0x87, 0xF8, 0x26,
  10641. 0x3F, 0x57, 0x86, 0xE2, 0xD8, 0x0E, 0xD3, 0x26,
  10642. 0xCB, 0xC7, 0xF0, 0xE7, 0x1A, 0x99, 0xF4, 0x3B,
  10643. 0xFB, 0x98, 0x8B, 0x9B, 0x7A, 0x02, 0xDD, 0x21
  10644. };
  10645. #endif /* WOLFSSL_AES_256 */
  10646. byte output[MAX_KEYWRAP_TEST_OUTLEN];
  10647. byte plain [MAX_KEYWRAP_TEST_PLAINLEN];
  10648. const keywrapVector test_wrap[] =
  10649. {
  10650. #ifdef WOLFSSL_AES_128
  10651. {k1, d1, v1, sizeof(k1), sizeof(d1), sizeof(v1)},
  10652. #endif
  10653. #ifdef WOLFSSL_AES_192
  10654. {k2, d2, v2, sizeof(k2), sizeof(d2), sizeof(v2)},
  10655. #endif
  10656. #ifdef WOLFSSL_AES_256
  10657. {k3, d3, v3, sizeof(k3), sizeof(d3), sizeof(v3)},
  10658. #endif
  10659. #ifdef WOLFSSL_AES_192
  10660. {k4, d4, v4, sizeof(k4), sizeof(d4), sizeof(v4)},
  10661. #endif
  10662. #ifdef WOLFSSL_AES_256
  10663. {k5, d5, v5, sizeof(k5), sizeof(d5), sizeof(v5)},
  10664. {k6, d6, v6, sizeof(k6), sizeof(d6), sizeof(v6)}
  10665. #endif
  10666. };
  10667. testSz = sizeof(test_wrap) / sizeof(keywrapVector);
  10668. XMEMSET(output, 0, sizeof(output));
  10669. XMEMSET(plain, 0, sizeof(plain));
  10670. for (i = 0; i < testSz; i++) {
  10671. wrapSz = wc_AesKeyWrap(test_wrap[i].kek, test_wrap[i].kekLen,
  10672. test_wrap[i].data, test_wrap[i].dataLen,
  10673. output, sizeof(output), NULL);
  10674. if ( (wrapSz < 0) || (wrapSz != (int)test_wrap[i].verifyLen) )
  10675. return -6600;
  10676. if (XMEMCMP(output, test_wrap[i].verify, test_wrap[i].verifyLen) != 0)
  10677. return -6601;
  10678. plainSz = wc_AesKeyUnWrap((byte*)test_wrap[i].kek, test_wrap[i].kekLen,
  10679. output, wrapSz,
  10680. plain, sizeof(plain), NULL);
  10681. if ( (plainSz < 0) || (plainSz != (int)test_wrap[i].dataLen) )
  10682. return -6602;
  10683. if (XMEMCMP(plain, test_wrap[i].data, test_wrap[i].dataLen) != 0)
  10684. return -6603 - i;
  10685. }
  10686. return 0;
  10687. }
  10688. #endif /* HAVE_AES_KEYWRAP */
  10689. #endif /* NO_AES */
  10690. #ifdef HAVE_CAMELLIA
  10691. enum {
  10692. CAM_ECB_ENC, CAM_ECB_DEC, CAM_CBC_ENC, CAM_CBC_DEC
  10693. };
  10694. typedef struct {
  10695. int type;
  10696. const byte* plaintext;
  10697. const byte* iv;
  10698. const byte* ciphertext;
  10699. const byte* key;
  10700. word32 keySz;
  10701. int errorCode;
  10702. } test_vector_t;
  10703. WOLFSSL_TEST_SUBROUTINE int camellia_test(void)
  10704. {
  10705. /* Camellia ECB Test Plaintext */
  10706. WOLFSSL_SMALL_STACK_STATIC const byte pte[] =
  10707. {
  10708. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  10709. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
  10710. };
  10711. /* Camellia ECB Test Initialization Vector */
  10712. WOLFSSL_SMALL_STACK_STATIC const byte ive[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  10713. /* Test 1: Camellia ECB 128-bit key */
  10714. WOLFSSL_SMALL_STACK_STATIC const byte k1[] =
  10715. {
  10716. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  10717. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
  10718. };
  10719. WOLFSSL_SMALL_STACK_STATIC const byte c1[] =
  10720. {
  10721. 0x67, 0x67, 0x31, 0x38, 0x54, 0x96, 0x69, 0x73,
  10722. 0x08, 0x57, 0x06, 0x56, 0x48, 0xea, 0xbe, 0x43
  10723. };
  10724. /* Test 2: Camellia ECB 192-bit key */
  10725. WOLFSSL_SMALL_STACK_STATIC const byte k2[] =
  10726. {
  10727. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  10728. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  10729. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  10730. };
  10731. WOLFSSL_SMALL_STACK_STATIC const byte c2[] =
  10732. {
  10733. 0xb4, 0x99, 0x34, 0x01, 0xb3, 0xe9, 0x96, 0xf8,
  10734. 0x4e, 0xe5, 0xce, 0xe7, 0xd7, 0x9b, 0x09, 0xb9
  10735. };
  10736. /* Test 3: Camellia ECB 256-bit key */
  10737. WOLFSSL_SMALL_STACK_STATIC const byte k3[] =
  10738. {
  10739. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  10740. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  10741. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  10742. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff
  10743. };
  10744. WOLFSSL_SMALL_STACK_STATIC const byte c3[] =
  10745. {
  10746. 0x9a, 0xcc, 0x23, 0x7d, 0xff, 0x16, 0xd7, 0x6c,
  10747. 0x20, 0xef, 0x7c, 0x91, 0x9e, 0x3a, 0x75, 0x09
  10748. };
  10749. /* Camellia CBC Test Plaintext */
  10750. WOLFSSL_SMALL_STACK_STATIC const byte ptc[] =
  10751. {
  10752. 0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96,
  10753. 0xE9, 0x3D, 0x7E, 0x11, 0x73, 0x93, 0x17, 0x2A
  10754. };
  10755. /* Camellia CBC Test Initialization Vector */
  10756. WOLFSSL_SMALL_STACK_STATIC const byte ivc[] =
  10757. {
  10758. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  10759. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  10760. };
  10761. /* Test 4: Camellia-CBC 128-bit key */
  10762. WOLFSSL_SMALL_STACK_STATIC const byte k4[] =
  10763. {
  10764. 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6,
  10765. 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C
  10766. };
  10767. WOLFSSL_SMALL_STACK_STATIC const byte c4[] =
  10768. {
  10769. 0x16, 0x07, 0xCF, 0x49, 0x4B, 0x36, 0xBB, 0xF0,
  10770. 0x0D, 0xAE, 0xB0, 0xB5, 0x03, 0xC8, 0x31, 0xAB
  10771. };
  10772. /* Test 5: Camellia-CBC 192-bit key */
  10773. WOLFSSL_SMALL_STACK_STATIC const byte k5[] =
  10774. {
  10775. 0x8E, 0x73, 0xB0, 0xF7, 0xDA, 0x0E, 0x64, 0x52,
  10776. 0xC8, 0x10, 0xF3, 0x2B, 0x80, 0x90, 0x79, 0xE5,
  10777. 0x62, 0xF8, 0xEA, 0xD2, 0x52, 0x2C, 0x6B, 0x7B
  10778. };
  10779. WOLFSSL_SMALL_STACK_STATIC const byte c5[] =
  10780. {
  10781. 0x2A, 0x48, 0x30, 0xAB, 0x5A, 0xC4, 0xA1, 0xA2,
  10782. 0x40, 0x59, 0x55, 0xFD, 0x21, 0x95, 0xCF, 0x93
  10783. };
  10784. /* Test 6: CBC 256-bit key */
  10785. WOLFSSL_SMALL_STACK_STATIC const byte k6[] =
  10786. {
  10787. 0x60, 0x3D, 0xEB, 0x10, 0x15, 0xCA, 0x71, 0xBE,
  10788. 0x2B, 0x73, 0xAE, 0xF0, 0x85, 0x7D, 0x77, 0x81,
  10789. 0x1F, 0x35, 0x2C, 0x07, 0x3B, 0x61, 0x08, 0xD7,
  10790. 0x2D, 0x98, 0x10, 0xA3, 0x09, 0x14, 0xDF, 0xF4
  10791. };
  10792. WOLFSSL_SMALL_STACK_STATIC const byte c6[] =
  10793. {
  10794. 0xE6, 0xCF, 0xA3, 0x5F, 0xC0, 0x2B, 0x13, 0x4A,
  10795. 0x4D, 0x2C, 0x0B, 0x67, 0x37, 0xAC, 0x3E, 0xDA
  10796. };
  10797. byte out[CAMELLIA_BLOCK_SIZE];
  10798. Camellia cam;
  10799. int i, testsSz, ret;
  10800. WOLFSSL_SMALL_STACK_STATIC const test_vector_t testVectors[] =
  10801. {
  10802. {CAM_ECB_ENC, pte, ive, c1, k1, sizeof(k1), -114},
  10803. {CAM_ECB_ENC, pte, ive, c2, k2, sizeof(k2), -115},
  10804. {CAM_ECB_ENC, pte, ive, c3, k3, sizeof(k3), -116},
  10805. {CAM_ECB_DEC, pte, ive, c1, k1, sizeof(k1), -117},
  10806. {CAM_ECB_DEC, pte, ive, c2, k2, sizeof(k2), -118},
  10807. {CAM_ECB_DEC, pte, ive, c3, k3, sizeof(k3), -119},
  10808. {CAM_CBC_ENC, ptc, ivc, c4, k4, sizeof(k4), -120},
  10809. {CAM_CBC_ENC, ptc, ivc, c5, k5, sizeof(k5), -121},
  10810. {CAM_CBC_ENC, ptc, ivc, c6, k6, sizeof(k6), -122},
  10811. {CAM_CBC_DEC, ptc, ivc, c4, k4, sizeof(k4), -123},
  10812. {CAM_CBC_DEC, ptc, ivc, c5, k5, sizeof(k5), -124},
  10813. {CAM_CBC_DEC, ptc, ivc, c6, k6, sizeof(k6), -125}
  10814. };
  10815. testsSz = sizeof(testVectors)/sizeof(test_vector_t);
  10816. for (i = 0; i < testsSz; i++) {
  10817. if (wc_CamelliaSetKey(&cam, testVectors[i].key, testVectors[i].keySz,
  10818. testVectors[i].iv) != 0)
  10819. return testVectors[i].errorCode;
  10820. switch (testVectors[i].type) {
  10821. case CAM_ECB_ENC:
  10822. ret = wc_CamelliaEncryptDirect(&cam, out,
  10823. testVectors[i].plaintext);
  10824. if (ret != 0 || XMEMCMP(out, testVectors[i].ciphertext,
  10825. CAMELLIA_BLOCK_SIZE))
  10826. return testVectors[i].errorCode;
  10827. break;
  10828. case CAM_ECB_DEC:
  10829. ret = wc_CamelliaDecryptDirect(&cam, out,
  10830. testVectors[i].ciphertext);
  10831. if (ret != 0 || XMEMCMP(out, testVectors[i].plaintext,
  10832. CAMELLIA_BLOCK_SIZE))
  10833. return testVectors[i].errorCode;
  10834. break;
  10835. case CAM_CBC_ENC:
  10836. ret = wc_CamelliaCbcEncrypt(&cam, out, testVectors[i].plaintext,
  10837. CAMELLIA_BLOCK_SIZE);
  10838. if (ret != 0 || XMEMCMP(out, testVectors[i].ciphertext,
  10839. CAMELLIA_BLOCK_SIZE))
  10840. return testVectors[i].errorCode;
  10841. break;
  10842. case CAM_CBC_DEC:
  10843. ret = wc_CamelliaCbcDecrypt(&cam, out,
  10844. testVectors[i].ciphertext, CAMELLIA_BLOCK_SIZE);
  10845. if (ret != 0 || XMEMCMP(out, testVectors[i].plaintext,
  10846. CAMELLIA_BLOCK_SIZE))
  10847. return testVectors[i].errorCode;
  10848. break;
  10849. default:
  10850. break;
  10851. }
  10852. }
  10853. /* Setting the IV and checking it was actually set. */
  10854. ret = wc_CamelliaSetIV(&cam, ivc);
  10855. if (ret != 0 || XMEMCMP(cam.reg, ivc, CAMELLIA_BLOCK_SIZE))
  10856. return -6700;
  10857. /* Setting the IV to NULL should be same as all zeros IV */
  10858. if (wc_CamelliaSetIV(&cam, NULL) != 0 ||
  10859. XMEMCMP(cam.reg, ive, CAMELLIA_BLOCK_SIZE))
  10860. return -6701;
  10861. /* First parameter should never be null */
  10862. if (wc_CamelliaSetIV(NULL, NULL) == 0)
  10863. return -6702;
  10864. /* First parameter should never be null, check it fails */
  10865. if (wc_CamelliaSetKey(NULL, k1, sizeof(k1), NULL) == 0)
  10866. return -6703;
  10867. /* Key should have a size of 16, 24, or 32 */
  10868. if (wc_CamelliaSetKey(&cam, k1, 0, NULL) == 0)
  10869. return -6704;
  10870. return 0;
  10871. }
  10872. #endif /* HAVE_CAMELLIA */
  10873. #ifdef HAVE_XCHACHA
  10874. WOLFSSL_TEST_SUBROUTINE int XChaCha_test(void) {
  10875. int ret = -6830;
  10876. WOLFSSL_SMALL_STACK_STATIC const byte Plaintext[] = {
  10877. 0x54, 0x68, 0x65, 0x20, 0x64, 0x68, 0x6f, 0x6c, 0x65, 0x20, 0x28, 0x70, 0x72, 0x6f, 0x6e, 0x6f, /* The dhole (prono */
  10878. 0x75, 0x6e, 0x63, 0x65, 0x64, 0x20, 0x22, 0x64, 0x6f, 0x6c, 0x65, 0x22, 0x29, 0x20, 0x69, 0x73, /* unced "dole") is */
  10879. 0x20, 0x61, 0x6c, 0x73, 0x6f, 0x20, 0x6b, 0x6e, 0x6f, 0x77, 0x6e, 0x20, 0x61, 0x73, 0x20, 0x74, /* also known as t */
  10880. 0x68, 0x65, 0x20, 0x41, 0x73, 0x69, 0x61, 0x74, 0x69, 0x63, 0x20, 0x77, 0x69, 0x6c, 0x64, 0x20, /* he Asiatic wild */
  10881. 0x64, 0x6f, 0x67, 0x2c, 0x20, 0x72, 0x65, 0x64, 0x20, 0x64, 0x6f, 0x67, 0x2c, 0x20, 0x61, 0x6e, /* dog, red dog, an */
  10882. 0x64, 0x20, 0x77, 0x68, 0x69, 0x73, 0x74, 0x6c, 0x69, 0x6e, 0x67, 0x20, 0x64, 0x6f, 0x67, 0x2e, /* d whistling dog. */
  10883. 0x20, 0x49, 0x74, 0x20, 0x69, 0x73, 0x20, 0x61, 0x62, 0x6f, 0x75, 0x74, 0x20, 0x74, 0x68, 0x65, /* It is about the */
  10884. 0x20, 0x73, 0x69, 0x7a, 0x65, 0x20, 0x6f, 0x66, 0x20, 0x61, 0x20, 0x47, 0x65, 0x72, 0x6d, 0x61, /* size of a Germa */
  10885. 0x6e, 0x20, 0x73, 0x68, 0x65, 0x70, 0x68, 0x65, 0x72, 0x64, 0x20, 0x62, 0x75, 0x74, 0x20, 0x6c, /* n shepherd but l */
  10886. 0x6f, 0x6f, 0x6b, 0x73, 0x20, 0x6d, 0x6f, 0x72, 0x65, 0x20, 0x6c, 0x69, 0x6b, 0x65, 0x20, 0x61, /* ooks more like a */
  10887. 0x20, 0x6c, 0x6f, 0x6e, 0x67, 0x2d, 0x6c, 0x65, 0x67, 0x67, 0x65, 0x64, 0x20, 0x66, 0x6f, 0x78, /* long-legged fox */
  10888. 0x2e, 0x20, 0x54, 0x68, 0x69, 0x73, 0x20, 0x68, 0x69, 0x67, 0x68, 0x6c, 0x79, 0x20, 0x65, 0x6c, /* . This highly el */
  10889. 0x75, 0x73, 0x69, 0x76, 0x65, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x73, 0x6b, 0x69, 0x6c, 0x6c, 0x65, /* usive and skille */
  10890. 0x64, 0x20, 0x6a, 0x75, 0x6d, 0x70, 0x65, 0x72, 0x20, 0x69, 0x73, 0x20, 0x63, 0x6c, 0x61, 0x73, /* d jumper is clas */
  10891. 0x73, 0x69, 0x66, 0x69, 0x65, 0x64, 0x20, 0x77, 0x69, 0x74, 0x68, 0x20, 0x77, 0x6f, 0x6c, 0x76, /* sified with wolv */
  10892. 0x65, 0x73, 0x2c, 0x20, 0x63, 0x6f, 0x79, 0x6f, 0x74, 0x65, 0x73, 0x2c, 0x20, 0x6a, 0x61, 0x63, /* es, coyotes, jac */
  10893. 0x6b, 0x61, 0x6c, 0x73, 0x2c, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x66, 0x6f, 0x78, 0x65, 0x73, 0x20, /* kals, and foxes */
  10894. 0x69, 0x6e, 0x20, 0x74, 0x68, 0x65, 0x20, 0x74, 0x61, 0x78, 0x6f, 0x6e, 0x6f, 0x6d, 0x69, 0x63, /* in the taxonomic */
  10895. 0x20, 0x66, 0x61, 0x6d, 0x69, 0x6c, 0x79, 0x20, 0x43, 0x61, 0x6e, 0x69, 0x64, 0x61, 0x65, 0x2e /* family Canidae. */
  10896. };
  10897. WOLFSSL_SMALL_STACK_STATIC const byte Key[] = {
  10898. 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  10899. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f
  10900. };
  10901. WOLFSSL_SMALL_STACK_STATIC const byte IV[] = {
  10902. 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, /* @ABCDEFGHIJKLMNO */
  10903. 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x58 }; /* PQRSTUVW */
  10904. WOLFSSL_SMALL_STACK_STATIC const byte Ciphertext[] = {
  10905. 0x45, 0x59, 0xab, 0xba, 0x4e, 0x48, 0xc1, 0x61, 0x02, 0xe8, 0xbb, 0x2c, 0x05, 0xe6, 0x94, 0x7f,
  10906. 0x50, 0xa7, 0x86, 0xde, 0x16, 0x2f, 0x9b, 0x0b, 0x7e, 0x59, 0x2a, 0x9b, 0x53, 0xd0, 0xd4, 0xe9,
  10907. 0x8d, 0x8d, 0x64, 0x10, 0xd5, 0x40, 0xa1, 0xa6, 0x37, 0x5b, 0x26, 0xd8, 0x0d, 0xac, 0xe4, 0xfa,
  10908. 0xb5, 0x23, 0x84, 0xc7, 0x31, 0xac, 0xbf, 0x16, 0xa5, 0x92, 0x3c, 0x0c, 0x48, 0xd3, 0x57, 0x5d,
  10909. 0x4d, 0x0d, 0x2c, 0x67, 0x3b, 0x66, 0x6f, 0xaa, 0x73, 0x10, 0x61, 0x27, 0x77, 0x01, 0x09, 0x3a,
  10910. 0x6b, 0xf7, 0xa1, 0x58, 0xa8, 0x86, 0x42, 0x92, 0xa4, 0x1c, 0x48, 0xe3, 0xa9, 0xb4, 0xc0, 0xda,
  10911. 0xec, 0xe0, 0xf8, 0xd9, 0x8d, 0x0d, 0x7e, 0x05, 0xb3, 0x7a, 0x30, 0x7b, 0xbb, 0x66, 0x33, 0x31,
  10912. 0x64, 0xec, 0x9e, 0x1b, 0x24, 0xea, 0x0d, 0x6c, 0x3f, 0xfd, 0xdc, 0xec, 0x4f, 0x68, 0xe7, 0x44,
  10913. 0x30, 0x56, 0x19, 0x3a, 0x03, 0xc8, 0x10, 0xe1, 0x13, 0x44, 0xca, 0x06, 0xd8, 0xed, 0x8a, 0x2b,
  10914. 0xfb, 0x1e, 0x8d, 0x48, 0xcf, 0xa6, 0xbc, 0x0e, 0xb4, 0xe2, 0x46, 0x4b, 0x74, 0x81, 0x42, 0x40,
  10915. 0x7c, 0x9f, 0x43, 0x1a, 0xee, 0x76, 0x99, 0x60, 0xe1, 0x5b, 0xa8, 0xb9, 0x68, 0x90, 0x46, 0x6e,
  10916. 0xf2, 0x45, 0x75, 0x99, 0x85, 0x23, 0x85, 0xc6, 0x61, 0xf7, 0x52, 0xce, 0x20, 0xf9, 0xda, 0x0c,
  10917. 0x09, 0xab, 0x6b, 0x19, 0xdf, 0x74, 0xe7, 0x6a, 0x95, 0x96, 0x74, 0x46, 0xf8, 0xd0, 0xfd, 0x41,
  10918. 0x5e, 0x7b, 0xee, 0x2a, 0x12, 0xa1, 0x14, 0xc2, 0x0e, 0xb5, 0x29, 0x2a, 0xe7, 0xa3, 0x49, 0xae,
  10919. 0x57, 0x78, 0x20, 0xd5, 0x52, 0x0a, 0x1f, 0x3f, 0xb6, 0x2a, 0x17, 0xce, 0x6a, 0x7e, 0x68, 0xfa,
  10920. 0x7c, 0x79, 0x11, 0x1d, 0x88, 0x60, 0x92, 0x0b, 0xc0, 0x48, 0xef, 0x43, 0xfe, 0x84, 0x48, 0x6c,
  10921. 0xcb, 0x87, 0xc2, 0x5f, 0x0a, 0xe0, 0x45, 0xf0, 0xcc, 0xe1, 0xe7, 0x98, 0x9a, 0x9a, 0xa2, 0x20,
  10922. 0xa2, 0x8b, 0xdd, 0x48, 0x27, 0xe7, 0x51, 0xa2, 0x4a, 0x6d, 0x5c, 0x62, 0xd7, 0x90, 0xa6, 0x63,
  10923. 0x93, 0xb9, 0x31, 0x11, 0xc1, 0xa5, 0x5d, 0xd7, 0x42, 0x1a, 0x10, 0x18, 0x49, 0x74, 0xc7, 0xc5
  10924. };
  10925. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10926. struct ChaCha *chacha = (struct ChaCha *)XMALLOC(sizeof *chacha, HEAP_HINT, DYNAMIC_TYPE_CIPHER);
  10927. byte *buf1 = (byte *)XMALLOC(sizeof Plaintext, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10928. byte *buf2 = (byte *)XMALLOC(sizeof Plaintext, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10929. if ((chacha == NULL) || (buf1 == NULL) || (buf2 == NULL))
  10930. ERROR_OUT(MEMORY_E, out);
  10931. #else
  10932. struct ChaCha chacha[1];
  10933. byte buf1[sizeof Plaintext];
  10934. byte buf2[sizeof Plaintext];
  10935. #endif
  10936. ret = wc_XChacha_SetKey(chacha, Key, sizeof Key, IV, sizeof IV, 0);
  10937. if (ret < 0)
  10938. ERROR_OUT(-6831, out);
  10939. ret = wc_Chacha_Process(chacha, buf1, Plaintext, sizeof Plaintext);
  10940. if (ret < 0)
  10941. ERROR_OUT(-6832, out);
  10942. if (XMEMCMP(buf1, Ciphertext, sizeof Plaintext))
  10943. ERROR_OUT(-6833, out);
  10944. ret = wc_XChacha_SetKey(chacha, Key, sizeof Key, IV, sizeof IV, 0);
  10945. if (ret < 0)
  10946. ERROR_OUT(-6834, out);
  10947. ret = wc_Chacha_Process(chacha, buf2, buf1, sizeof Plaintext);
  10948. if (ret < 0)
  10949. ERROR_OUT(-6835, out);
  10950. if (XMEMCMP(buf2, Plaintext, sizeof Plaintext))
  10951. ERROR_OUT(-6836, out);
  10952. out:
  10953. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10954. if (chacha)
  10955. XFREE(chacha, HEAP_HINT, DYNAMIC_TYPE_CIPHER);
  10956. if (buf1)
  10957. XFREE(buf1, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10958. if (buf2)
  10959. XFREE(buf2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10960. #endif
  10961. return ret;
  10962. }
  10963. #endif /* HAVE_XCHACHA */
  10964. #if defined(HAVE_XCHACHA) && defined(HAVE_POLY1305)
  10965. WOLFSSL_TEST_SUBROUTINE int XChaCha20Poly1305_test(void) {
  10966. int ret;
  10967. WOLFSSL_SMALL_STACK_STATIC const byte Plaintext[] = {
  10968. 0x4c, 0x61, 0x64, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x47, 0x65, 0x6e, 0x74, 0x6c, /* Ladies and Gentl */
  10969. 0x65, 0x6d, 0x65, 0x6e, 0x20, 0x6f, 0x66, 0x20, 0x74, 0x68, 0x65, 0x20, 0x63, 0x6c, 0x61, 0x73, /* emen of the clas */
  10970. 0x73, 0x20, 0x6f, 0x66, 0x20, 0x27, 0x39, 0x39, 0x3a, 0x20, 0x49, 0x66, 0x20, 0x49, 0x20, 0x63, /* s of '99: If I c */
  10971. 0x6f, 0x75, 0x6c, 0x64, 0x20, 0x6f, 0x66, 0x66, 0x65, 0x72, 0x20, 0x79, 0x6f, 0x75, 0x20, 0x6f, /* ould offer you o */
  10972. 0x6e, 0x6c, 0x79, 0x20, 0x6f, 0x6e, 0x65, 0x20, 0x74, 0x69, 0x70, 0x20, 0x66, 0x6f, 0x72, 0x20, /* nly one tip for */
  10973. 0x74, 0x68, 0x65, 0x20, 0x66, 0x75, 0x74, 0x75, 0x72, 0x65, 0x2c, 0x20, 0x73, 0x75, 0x6e, 0x73, /* the future, suns */
  10974. 0x63, 0x72, 0x65, 0x65, 0x6e, 0x20, 0x77, 0x6f, 0x75, 0x6c, 0x64, 0x20, 0x62, 0x65, 0x20, 0x69, /* creen would be i */
  10975. 0x74, 0x2e }; /* t. */
  10976. WOLFSSL_SMALL_STACK_STATIC const byte AAD[] = { 0x50, 0x51, 0x52, 0x53, 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7 }; /* PQRS........ */
  10977. WOLFSSL_SMALL_STACK_STATIC const byte Key[] = {
  10978. 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  10979. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f
  10980. };
  10981. WOLFSSL_SMALL_STACK_STATIC const byte IV[] = {
  10982. 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, /* @ABCDEFGHIJKLMNO */
  10983. 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57 }; /* PQRSTUVW */
  10984. WOLFSSL_SMALL_STACK_STATIC const byte Ciphertext[] = {
  10985. 0xbd, 0x6d, 0x17, 0x9d, 0x3e, 0x83, 0xd4, 0x3b, 0x95, 0x76, 0x57, 0x94, 0x93, 0xc0, 0xe9, 0x39,
  10986. 0x57, 0x2a, 0x17, 0x00, 0x25, 0x2b, 0xfa, 0xcc, 0xbe, 0xd2, 0x90, 0x2c, 0x21, 0x39, 0x6c, 0xbb,
  10987. 0x73, 0x1c, 0x7f, 0x1b, 0x0b, 0x4a, 0xa6, 0x44, 0x0b, 0xf3, 0xa8, 0x2f, 0x4e, 0xda, 0x7e, 0x39,
  10988. 0xae, 0x64, 0xc6, 0x70, 0x8c, 0x54, 0xc2, 0x16, 0xcb, 0x96, 0xb7, 0x2e, 0x12, 0x13, 0xb4, 0x52,
  10989. 0x2f, 0x8c, 0x9b, 0xa4, 0x0d, 0xb5, 0xd9, 0x45, 0xb1, 0x1b, 0x69, 0xb9, 0x82, 0xc1, 0xbb, 0x9e,
  10990. 0x3f, 0x3f, 0xac, 0x2b, 0xc3, 0x69, 0x48, 0x8f, 0x76, 0xb2, 0x38, 0x35, 0x65, 0xd3, 0xff, 0xf9,
  10991. 0x21, 0xf9, 0x66, 0x4c, 0x97, 0x63, 0x7d, 0xa9, 0x76, 0x88, 0x12, 0xf6, 0x15, 0xc6, 0x8b, 0x13,
  10992. 0xb5, 0x2e };
  10993. WOLFSSL_SMALL_STACK_STATIC const byte Tag[] = {
  10994. 0xc0, 0x87, 0x59, 0x24, 0xc1, 0xc7, 0x98, 0x79, 0x47, 0xde, 0xaf, 0xd8, 0x78, 0x0a, 0xcf, 0x49
  10995. };
  10996. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10997. byte *buf1 = (byte *)XMALLOC(sizeof Ciphertext + sizeof Tag, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10998. byte *buf2 = (byte *)XMALLOC(sizeof Plaintext, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10999. if ((buf1 == NULL) || (buf2 == NULL))
  11000. ERROR_OUT(-6480, out);
  11001. #else
  11002. byte buf1[sizeof Ciphertext + sizeof Tag];
  11003. byte buf2[sizeof Plaintext];
  11004. #endif
  11005. ret = wc_XChaCha20Poly1305_Encrypt(buf1, sizeof Ciphertext + sizeof Tag,
  11006. Plaintext, sizeof Plaintext,
  11007. AAD, sizeof AAD,
  11008. IV, sizeof IV,
  11009. Key, sizeof Key);
  11010. if (ret < 0)
  11011. ERROR_OUT(-6841, out);
  11012. if (XMEMCMP(buf1, Ciphertext, sizeof Ciphertext))
  11013. ERROR_OUT(-6842, out);
  11014. if (XMEMCMP(buf1 + sizeof Ciphertext, Tag, CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE))
  11015. ERROR_OUT(-6843, out);
  11016. ret = wc_XChaCha20Poly1305_Decrypt(buf2, sizeof Plaintext,
  11017. buf1, sizeof Ciphertext + sizeof Tag,
  11018. AAD, sizeof AAD,
  11019. IV, sizeof IV,
  11020. Key, sizeof Key);
  11021. if (ret < 0)
  11022. ERROR_OUT(-6844, out);
  11023. if (XMEMCMP(buf2, Plaintext, sizeof Plaintext))
  11024. ERROR_OUT(-6845, out);
  11025. out:
  11026. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  11027. if (buf1 != NULL)
  11028. XFREE(buf1, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11029. if (buf2 != NULL)
  11030. XFREE(buf2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11031. #endif
  11032. return ret;
  11033. }
  11034. #endif /* defined(HAVE_XCHACHA) && defined(HAVE_POLY1305) */
  11035. #ifndef WC_NO_RNG
  11036. static int _rng_test(WC_RNG* rng, int errorOffset)
  11037. {
  11038. byte block[32];
  11039. int ret, i;
  11040. XMEMSET(block, 0, sizeof(block));
  11041. ret = wc_RNG_GenerateBlock(rng, block, sizeof(block));
  11042. if (ret != 0) {
  11043. ret = -6850;
  11044. goto exit;
  11045. }
  11046. /* Check for 0's */
  11047. for (i=0; i<(int)sizeof(block); i++) {
  11048. if (block[i] == 0) {
  11049. ret++;
  11050. }
  11051. }
  11052. /* All zeros count check */
  11053. if (ret >= (int)sizeof(block)) {
  11054. ret = -6851;
  11055. goto exit;
  11056. }
  11057. ret = wc_RNG_GenerateByte(rng, block);
  11058. if (ret != 0) {
  11059. ret = -6852;
  11060. goto exit;
  11061. }
  11062. /* Parameter validation testing. */
  11063. ret = wc_RNG_GenerateBlock(NULL, block, sizeof(block));
  11064. if (ret != BAD_FUNC_ARG) {
  11065. ret = -6853;
  11066. goto exit;
  11067. }
  11068. ret = wc_RNG_GenerateBlock(rng, NULL, sizeof(block));
  11069. if (ret != BAD_FUNC_ARG) {
  11070. ret = -6854;
  11071. goto exit;
  11072. }
  11073. ret = wc_RNG_GenerateByte(NULL, block);
  11074. if (ret != BAD_FUNC_ARG) {
  11075. ret = -6855;
  11076. goto exit;
  11077. }
  11078. ret = wc_RNG_GenerateByte(rng, NULL);
  11079. if (ret != BAD_FUNC_ARG) {
  11080. ret = -6856;
  11081. goto exit;
  11082. }
  11083. ret = 0;
  11084. exit:
  11085. if (ret != 0)
  11086. ret += errorOffset;
  11087. return ret;
  11088. }
  11089. static int random_rng_test(void)
  11090. {
  11091. WC_RNG localRng;
  11092. WC_RNG* rng;
  11093. int ret;
  11094. rng = &localRng;
  11095. /* Test stack based RNG. */
  11096. #ifndef HAVE_FIPS
  11097. ret = wc_InitRng_ex(rng, HEAP_HINT, devId);
  11098. #else
  11099. ret = wc_InitRng(rng);
  11100. #endif
  11101. if (ret != 0) return -6900;
  11102. ret = _rng_test(rng, -6300);
  11103. /* Make sure and free RNG */
  11104. wc_FreeRng(rng);
  11105. if (ret != 0) return ret;
  11106. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && !defined(WOLFSSL_NO_MALLOC)
  11107. {
  11108. byte nonce[8] = { 0 };
  11109. /* Test dynamic RNG. */
  11110. rng = wc_rng_new(nonce, (word32)sizeof(nonce), HEAP_HINT);
  11111. if (rng == NULL) return -6901;
  11112. ret = _rng_test(rng, -6310);
  11113. wc_rng_free(rng);
  11114. }
  11115. #endif
  11116. return ret;
  11117. }
  11118. #if defined(HAVE_HASHDRBG) && !defined(CUSTOM_RAND_GENERATE_BLOCK)
  11119. #ifdef WC_RNG_SEED_CB
  11120. static int seed_cb(OS_Seed* os, byte* output, word32 sz)
  11121. {
  11122. word32 i;
  11123. (void)os;
  11124. /* Known answer test. Set the seed to the same value every time. */
  11125. for (i = 0; i < sz; i++)
  11126. output[i] = (byte)i;
  11127. return 0;
  11128. }
  11129. static int rng_seed_test(void)
  11130. {
  11131. #ifndef HAVE_FIPS
  11132. WOLFSSL_SMALL_STACK_STATIC const byte check[] =
  11133. {
  11134. 0x83, 0x46, 0x65, 0x2f, 0x5c, 0x44, 0x16, 0x5f,
  11135. 0xb3, 0x89, 0x26, 0xde, 0x0b, 0x6b, 0xa2, 0x06,
  11136. 0x7e, 0xa7, 0x9a, 0x55, 0x22, 0x01, 0xb0, 0x22,
  11137. 0xf4, 0x7e, 0xa2, 0x66, 0xc4, 0x08, 0x6f, 0xba
  11138. };
  11139. #else
  11140. /* FIPS uses a longer seed, so different check value. */
  11141. WOLFSSL_SMALL_STACK_STATIC const byte check[] =
  11142. {
  11143. 0xaf, 0x31, 0xcc, 0xef, 0xa9, 0x29, 0x4c, 0x24,
  11144. 0xbd, 0xa5, 0xa3, 0x52, 0x69, 0xf3, 0xb9, 0xb2,
  11145. 0x1e, 0xd4, 0x52, 0x3b, 0x9a, 0x96, 0x06, 0x20,
  11146. 0xc0, 0x5f, 0x44, 0x06, 0x1f, 0x80, 0xdf, 0xe0
  11147. };
  11148. #endif
  11149. byte output[WC_SHA256_DIGEST_SIZE];
  11150. WC_RNG rng;
  11151. int ret;
  11152. ret = wc_SetSeed_Cb(seed_cb);
  11153. if (ret != 0) {
  11154. ret = -7007;
  11155. goto exit;
  11156. }
  11157. ret = wc_InitRng(&rng);
  11158. if (ret != 0) {
  11159. ret = -7008;
  11160. goto exit;
  11161. }
  11162. ret = wc_RNG_GenerateBlock(&rng, output, sizeof(output));
  11163. if (ret != 0) {
  11164. ret = -7009;
  11165. goto exit;
  11166. }
  11167. ret = XMEMCMP(output, check, sizeof(output));
  11168. if (ret != 0) {
  11169. ret = -7010;
  11170. goto exit;
  11171. }
  11172. ret = wc_FreeRng(&rng);
  11173. if (ret != 0) {
  11174. ret = -7011;
  11175. goto exit;
  11176. }
  11177. ret = wc_SetSeed_Cb(wc_GenerateSeed);
  11178. if (ret != 0) {
  11179. ret = -7012;
  11180. }
  11181. exit:
  11182. return ret;
  11183. }
  11184. #endif
  11185. WOLFSSL_TEST_SUBROUTINE int random_test(void)
  11186. {
  11187. WOLFSSL_SMALL_STACK_STATIC const byte test1Entropy[] =
  11188. {
  11189. 0xa6, 0x5a, 0xd0, 0xf3, 0x45, 0xdb, 0x4e, 0x0e, 0xff, 0xe8, 0x75, 0xc3,
  11190. 0xa2, 0xe7, 0x1f, 0x42, 0xc7, 0x12, 0x9d, 0x62, 0x0f, 0xf5, 0xc1, 0x19,
  11191. 0xa9, 0xef, 0x55, 0xf0, 0x51, 0x85, 0xe0, 0xfb, 0x85, 0x81, 0xf9, 0x31,
  11192. 0x75, 0x17, 0x27, 0x6e, 0x06, 0xe9, 0x60, 0x7d, 0xdb, 0xcb, 0xcc, 0x2e
  11193. };
  11194. WOLFSSL_SMALL_STACK_STATIC const byte test1Output[] =
  11195. {
  11196. 0xd3, 0xe1, 0x60, 0xc3, 0x5b, 0x99, 0xf3, 0x40, 0xb2, 0x62, 0x82, 0x64,
  11197. 0xd1, 0x75, 0x10, 0x60, 0xe0, 0x04, 0x5d, 0xa3, 0x83, 0xff, 0x57, 0xa5,
  11198. 0x7d, 0x73, 0xa6, 0x73, 0xd2, 0xb8, 0xd8, 0x0d, 0xaa, 0xf6, 0xa6, 0xc3,
  11199. 0x5a, 0x91, 0xbb, 0x45, 0x79, 0xd7, 0x3f, 0xd0, 0xc8, 0xfe, 0xd1, 0x11,
  11200. 0xb0, 0x39, 0x13, 0x06, 0x82, 0x8a, 0xdf, 0xed, 0x52, 0x8f, 0x01, 0x81,
  11201. 0x21, 0xb3, 0xfe, 0xbd, 0xc3, 0x43, 0xe7, 0x97, 0xb8, 0x7d, 0xbb, 0x63,
  11202. 0xdb, 0x13, 0x33, 0xde, 0xd9, 0xd1, 0xec, 0xe1, 0x77, 0xcf, 0xa6, 0xb7,
  11203. 0x1f, 0xe8, 0xab, 0x1d, 0xa4, 0x66, 0x24, 0xed, 0x64, 0x15, 0xe5, 0x1c,
  11204. 0xcd, 0xe2, 0xc7, 0xca, 0x86, 0xe2, 0x83, 0x99, 0x0e, 0xea, 0xeb, 0x91,
  11205. 0x12, 0x04, 0x15, 0x52, 0x8b, 0x22, 0x95, 0x91, 0x02, 0x81, 0xb0, 0x2d,
  11206. 0xd4, 0x31, 0xf4, 0xc9, 0xf7, 0x04, 0x27, 0xdf
  11207. };
  11208. WOLFSSL_SMALL_STACK_STATIC const byte test2EntropyA[] =
  11209. {
  11210. 0x63, 0x36, 0x33, 0x77, 0xe4, 0x1e, 0x86, 0x46, 0x8d, 0xeb, 0x0a, 0xb4,
  11211. 0xa8, 0xed, 0x68, 0x3f, 0x6a, 0x13, 0x4e, 0x47, 0xe0, 0x14, 0xc7, 0x00,
  11212. 0x45, 0x4e, 0x81, 0xe9, 0x53, 0x58, 0xa5, 0x69, 0x80, 0x8a, 0xa3, 0x8f,
  11213. 0x2a, 0x72, 0xa6, 0x23, 0x59, 0x91, 0x5a, 0x9f, 0x8a, 0x04, 0xca, 0x68
  11214. };
  11215. WOLFSSL_SMALL_STACK_STATIC const byte test2EntropyB[] =
  11216. {
  11217. 0xe6, 0x2b, 0x8a, 0x8e, 0xe8, 0xf1, 0x41, 0xb6, 0x98, 0x05, 0x66, 0xe3,
  11218. 0xbf, 0xe3, 0xc0, 0x49, 0x03, 0xda, 0xd4, 0xac, 0x2c, 0xdf, 0x9f, 0x22,
  11219. 0x80, 0x01, 0x0a, 0x67, 0x39, 0xbc, 0x83, 0xd3
  11220. };
  11221. WOLFSSL_SMALL_STACK_STATIC const byte test2Output[] =
  11222. {
  11223. 0x04, 0xee, 0xc6, 0x3b, 0xb2, 0x31, 0xdf, 0x2c, 0x63, 0x0a, 0x1a, 0xfb,
  11224. 0xe7, 0x24, 0x94, 0x9d, 0x00, 0x5a, 0x58, 0x78, 0x51, 0xe1, 0xaa, 0x79,
  11225. 0x5e, 0x47, 0x73, 0x47, 0xc8, 0xb0, 0x56, 0x62, 0x1c, 0x18, 0xbd, 0xdc,
  11226. 0xdd, 0x8d, 0x99, 0xfc, 0x5f, 0xc2, 0xb9, 0x20, 0x53, 0xd8, 0xcf, 0xac,
  11227. 0xfb, 0x0b, 0xb8, 0x83, 0x12, 0x05, 0xfa, 0xd1, 0xdd, 0xd6, 0xc0, 0x71,
  11228. 0x31, 0x8a, 0x60, 0x18, 0xf0, 0x3b, 0x73, 0xf5, 0xed, 0xe4, 0xd4, 0xd0,
  11229. 0x71, 0xf9, 0xde, 0x03, 0xfd, 0x7a, 0xea, 0x10, 0x5d, 0x92, 0x99, 0xb8,
  11230. 0xaf, 0x99, 0xaa, 0x07, 0x5b, 0xdb, 0x4d, 0xb9, 0xaa, 0x28, 0xc1, 0x8d,
  11231. 0x17, 0x4b, 0x56, 0xee, 0x2a, 0x01, 0x4d, 0x09, 0x88, 0x96, 0xff, 0x22,
  11232. 0x82, 0xc9, 0x55, 0xa8, 0x19, 0x69, 0xe0, 0x69, 0xfa, 0x8c, 0xe0, 0x07,
  11233. 0xa1, 0x80, 0x18, 0x3a, 0x07, 0xdf, 0xae, 0x17
  11234. };
  11235. byte output[WC_SHA256_DIGEST_SIZE * 4];
  11236. int ret;
  11237. ret = wc_RNG_HealthTest(0, test1Entropy, sizeof(test1Entropy), NULL, 0,
  11238. output, sizeof(output));
  11239. if (ret != 0)
  11240. return -7000;
  11241. if (XMEMCMP(test1Output, output, sizeof(output)) != 0)
  11242. return -7001;
  11243. ret = wc_RNG_HealthTest(1, test2EntropyA, sizeof(test2EntropyA),
  11244. test2EntropyB, sizeof(test2EntropyB),
  11245. output, sizeof(output));
  11246. if (ret != 0)
  11247. return -7002;
  11248. if (XMEMCMP(test2Output, output, sizeof(output)) != 0)
  11249. return -7003;
  11250. /* Basic RNG generate block test */
  11251. if ((ret = random_rng_test()) != 0)
  11252. return ret;
  11253. /* Test the seed check function. */
  11254. #if !(defined(HAVE_FIPS) || defined(HAVE_SELFTEST)) || \
  11255. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))
  11256. {
  11257. word32 i, outputSz;
  11258. /* Repeat the same byte over and over. Should fail. */
  11259. outputSz = sizeof(output);
  11260. XMEMSET(output, 1, outputSz);
  11261. ret = wc_RNG_TestSeed(output, outputSz);
  11262. if (ret == 0)
  11263. return -7004;
  11264. /* Every byte of the entropy scratch is different,
  11265. * entropy is a single byte that shouldn't match. */
  11266. outputSz = (sizeof(output) / 2) + 1;
  11267. for (i = 0; i < outputSz; i++)
  11268. output[i] = (byte)i;
  11269. ret = wc_RNG_TestSeed(output, outputSz);
  11270. if (ret != 0)
  11271. return -7005;
  11272. outputSz = sizeof(output);
  11273. for (i = 0; i < outputSz; i++)
  11274. output[i] = (byte)i;
  11275. ret = wc_RNG_TestSeed(output, outputSz);
  11276. if (ret != 0)
  11277. return -7006;
  11278. }
  11279. #endif
  11280. /* Test the seed callback. */
  11281. #ifdef WC_RNG_SEED_CB
  11282. if ((ret = rng_seed_test()) != 0)
  11283. return ret;
  11284. #endif
  11285. return 0;
  11286. }
  11287. #else
  11288. WOLFSSL_TEST_SUBROUTINE int random_test(void)
  11289. {
  11290. /* Basic RNG generate block test */
  11291. return random_rng_test();
  11292. }
  11293. #endif /* HAVE_HASHDRBG && !CUSTOM_RAND_GENERATE_BLOCK */
  11294. #endif /* WC_NO_RNG */
  11295. #ifndef MEM_TEST_SZ
  11296. #define MEM_TEST_SZ 1024
  11297. #endif
  11298. #if defined(WOLFSSL_STATIC_MEMORY) || !defined(WOLFSSL_NO_MALLOC)
  11299. static int simple_mem_test(int sz)
  11300. {
  11301. int ret = 0;
  11302. byte* b;
  11303. int i;
  11304. b = (byte*)XMALLOC(sz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11305. if (b == NULL) {
  11306. return -7110;
  11307. }
  11308. /* utilize memory */
  11309. for (i = 0; i < sz; i++) {
  11310. b[i] = (byte)i;
  11311. }
  11312. /* read back and verify */
  11313. for (i = 0; i < sz; i++) {
  11314. if (b[i] != (byte)i) {
  11315. ret = -7111;
  11316. break;
  11317. }
  11318. }
  11319. XFREE(b, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11320. return ret;
  11321. }
  11322. #endif
  11323. WOLFSSL_TEST_SUBROUTINE int memory_test(void)
  11324. {
  11325. int ret = 0;
  11326. #if defined(COMPLEX_MEM_TEST) || defined(WOLFSSL_STATIC_MEMORY)
  11327. int i;
  11328. #endif
  11329. #ifdef WOLFSSL_STATIC_MEMORY
  11330. word32 size[] = { WOLFMEM_BUCKETS };
  11331. word32 dist[] = { WOLFMEM_DIST };
  11332. byte buffer[30000]; /* make large enough to involve many bucket sizes */
  11333. int pad = -(int)((wc_ptr_t)buffer) & (WOLFSSL_STATIC_ALIGN - 1);
  11334. /* pad to account for if head of buffer is not at set memory
  11335. * alignment when tests are ran */
  11336. #endif
  11337. #ifdef WOLFSSL_STATIC_MEMORY
  11338. /* check macro settings */
  11339. if (sizeof(size)/sizeof(word32) != WOLFMEM_MAX_BUCKETS) {
  11340. return -7200;
  11341. }
  11342. if (sizeof(dist)/sizeof(word32) != WOLFMEM_MAX_BUCKETS) {
  11343. return -7201;
  11344. }
  11345. for (i = 0; i < WOLFMEM_MAX_BUCKETS; i++) {
  11346. if ((size[i] % WOLFSSL_STATIC_ALIGN) != 0) {
  11347. /* each element in array should be divisible by alignment size */
  11348. return -7202;
  11349. }
  11350. }
  11351. for (i = 1; i < WOLFMEM_MAX_BUCKETS; i++) {
  11352. if (size[i - 1] >= size[i]) {
  11353. return -7203; /* sizes should be in increasing order */
  11354. }
  11355. }
  11356. /* check that padding size returned is possible */
  11357. if (wolfSSL_MemoryPaddingSz() < WOLFSSL_STATIC_ALIGN) {
  11358. return -7204; /* no room for wc_Memory struct */
  11359. }
  11360. if (wolfSSL_MemoryPaddingSz() < 0) {
  11361. return -7205;
  11362. }
  11363. if (wolfSSL_MemoryPaddingSz() % WOLFSSL_STATIC_ALIGN != 0) {
  11364. return -7206; /* not aligned! */
  11365. }
  11366. /* check function to return optimum buffer size (rounded down) */
  11367. ret = wolfSSL_StaticBufferSz(buffer, sizeof(buffer), WOLFMEM_GENERAL);
  11368. if ((ret - pad) % WOLFSSL_STATIC_ALIGN != 0) {
  11369. return -7207; /* not aligned! */
  11370. }
  11371. if (ret < 0) {
  11372. return -7208;
  11373. }
  11374. if ((unsigned int)ret > sizeof(buffer)) {
  11375. return -7209; /* did not round down as expected */
  11376. }
  11377. if (ret != wolfSSL_StaticBufferSz(buffer, ret, WOLFMEM_GENERAL)) {
  11378. return -7210; /* return value changed when using suggested value */
  11379. }
  11380. ret = wolfSSL_MemoryPaddingSz();
  11381. ret += pad; /* add space that is going to be needed if buffer not aligned */
  11382. if (wolfSSL_StaticBufferSz(buffer, size[0] + ret + 1, WOLFMEM_GENERAL) !=
  11383. (ret + (int)size[0])) {
  11384. return -7211; /* did not round down to nearest bucket value */
  11385. }
  11386. ret = wolfSSL_StaticBufferSz(buffer, sizeof(buffer), WOLFMEM_IO_POOL);
  11387. if ((ret - pad) < 0) {
  11388. return -7212;
  11389. }
  11390. if (((ret - pad) % (WOLFMEM_IO_SZ + wolfSSL_MemoryPaddingSz())) != 0) {
  11391. return -7213; /* not even chunks of memory for IO size */
  11392. }
  11393. if (((ret - pad) % WOLFSSL_STATIC_ALIGN) != 0) {
  11394. return -7214; /* memory not aligned */
  11395. }
  11396. /* check for passing bad or unknown arguments to functions */
  11397. if (wolfSSL_StaticBufferSz(NULL, 1, WOLFMEM_GENERAL) > 0) {
  11398. return -7215;
  11399. }
  11400. if (wolfSSL_StaticBufferSz(buffer, 1, WOLFMEM_GENERAL) != 0) {
  11401. return -7216; /* should round to 0 since struct + bucket will not fit */
  11402. }
  11403. (void)dist; /* avoid static analysis warning of variable not used */
  11404. #endif
  11405. #if defined(WOLFSSL_STATIC_MEMORY) || !defined(WOLFSSL_NO_MALLOC)
  11406. /* simple test */
  11407. ret = simple_mem_test(MEM_TEST_SZ);
  11408. if (ret != 0)
  11409. return ret;
  11410. #endif
  11411. #ifdef COMPLEX_MEM_TEST
  11412. /* test various size blocks */
  11413. for (i = 1; i < MEM_TEST_SZ; i*=2) {
  11414. ret = simple_mem_test(i);
  11415. if (ret != 0)
  11416. return ret;
  11417. }
  11418. #endif
  11419. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_NO_MALLOC)
  11420. /* realloc test */
  11421. {
  11422. byte *c = NULL;
  11423. byte *b = (byte*)XMALLOC(MEM_TEST_SZ, HEAP_HINT,
  11424. DYNAMIC_TYPE_TMP_BUFFER);
  11425. if (b) {
  11426. c = (byte*)XREALLOC(b, MEM_TEST_SZ+sizeof(word32), HEAP_HINT,
  11427. DYNAMIC_TYPE_TMP_BUFFER);
  11428. if (c)
  11429. b = c;
  11430. }
  11431. if (b)
  11432. XFREE(b, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11433. if ((b == NULL) || (c == NULL)) {
  11434. return -7217;
  11435. }
  11436. }
  11437. #endif
  11438. return ret;
  11439. }
  11440. #ifndef NO_FILESYSTEM
  11441. /* Cert Paths */
  11442. #ifdef FREESCALE_MQX
  11443. #define CERT_PREFIX "a:\\"
  11444. #define CERT_PATH_SEP "\\"
  11445. #elif defined(WOLFSSL_uTKERNEL2)
  11446. #define CERT_PREFIX "/uda/"
  11447. #define CERT_PATH_SEP "/"
  11448. #elif defined(_WIN32_WCE)
  11449. #define CERT_PREFIX "\\windows\\"
  11450. #define CERT_PATH_SEP "\\"
  11451. #endif
  11452. #ifndef CERT_PREFIX
  11453. #define CERT_PREFIX "./"
  11454. #endif
  11455. #ifndef CERT_PATH_SEP
  11456. #define CERT_PATH_SEP "/"
  11457. #endif
  11458. #ifndef CERT_WRITE_TEMP_DIR
  11459. #define CERT_WRITE_TEMP_DIR CERT_PREFIX
  11460. #endif
  11461. #define CERT_ROOT CERT_PREFIX "certs" CERT_PATH_SEP
  11462. /* Generated Test Certs */
  11463. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  11464. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096)
  11465. #if !defined(NO_RSA) && !defined(NO_ASN)
  11466. static const char* clientKey = CERT_ROOT "client-key.der";
  11467. static const char* clientCert = CERT_ROOT "client-cert.der";
  11468. #ifdef WOLFSSL_CERT_EXT
  11469. static const char* clientKeyPub = CERT_ROOT "client-keyPub.der";
  11470. #endif
  11471. #endif /* !NO_RSA && !NO_ASN */
  11472. #endif
  11473. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  11474. #if !defined(NO_RSA) && !defined(NO_ASN)
  11475. #if defined(WOLFSSL_CERT_GEN) || defined(HAVE_PKCS7)
  11476. static const char* rsaCaKeyFile = CERT_ROOT "ca-key.der";
  11477. #ifdef WOLFSSL_CERT_GEN
  11478. static const char* rsaCaCertFile = CERT_ROOT "ca-cert.pem";
  11479. #endif
  11480. #if defined(WOLFSSL_ALT_NAMES) || defined(HAVE_PKCS7)
  11481. static const char* rsaCaCertDerFile = CERT_ROOT "ca-cert.der";
  11482. #endif
  11483. #ifdef HAVE_PKCS7
  11484. static const char* rsaServerCertDerFile =
  11485. CERT_ROOT "server-cert.der";
  11486. static const char* rsaServerKeyDerFile =
  11487. CERT_ROOT "server-key.der";
  11488. #endif
  11489. #endif
  11490. #endif /* !NO_RSA && !NO_ASN */
  11491. #endif /* !USE_CERT_BUFFER_* */
  11492. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  11493. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096)
  11494. #if !defined(NO_ASN) && !defined(NO_DH)
  11495. static const char* dhParamsFile = CERT_ROOT "dh2048.der";
  11496. #endif
  11497. #endif
  11498. #if !defined(NO_ASN) && !defined(NO_DH)
  11499. #if defined(WOLFSSL_DH_EXTRA) && (!defined(HAVE_FIPS) || \
  11500. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  11501. static const char* dhKeyFile = CERT_ROOT "statickeys/dh-ffdhe2048.der";
  11502. static const char* dhKeyPubFile = CERT_ROOT "statickeys/dh-ffdhe2048-pub.der";
  11503. #endif
  11504. #endif
  11505. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  11506. #ifndef NO_DSA
  11507. static const char* dsaKey = CERT_ROOT "dsa2048.der";
  11508. #endif
  11509. #endif /* !USE_CERT_BUFFER_* */
  11510. #if !defined(USE_CERT_BUFFERS_256) && !defined(NO_ECC256)
  11511. #ifdef HAVE_ECC
  11512. /* cert files to be used in rsa cert gen test, check if RSA enabled */
  11513. #ifdef HAVE_ECC_KEY_IMPORT
  11514. static const char* eccKeyDerFile = CERT_ROOT "ecc-key.der";
  11515. #endif
  11516. #endif
  11517. #if !defined(USE_CERT_BUFFERS_256) && !defined(NO_ASN)
  11518. #if defined(HAVE_ECC) && defined(WOLFSSL_CERT_GEN)
  11519. #ifndef NO_RSA
  11520. static const char* eccKeyPubFileDer = CERT_ROOT "ecc-keyPub.der";
  11521. #endif
  11522. static const char* eccCaKeyFile = CERT_ROOT "ca-ecc-key.der";
  11523. static const char* eccCaCertFile = CERT_ROOT "ca-ecc-cert.pem";
  11524. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  11525. static const char* eccCaKey384File =
  11526. CERT_ROOT "ca-ecc384-key.der";
  11527. static const char* eccCaCert384File =
  11528. CERT_ROOT "ca-ecc384-cert.pem";
  11529. #endif
  11530. #endif
  11531. #if defined(HAVE_PKCS7) && defined(HAVE_ECC)
  11532. static const char* eccClientKey = CERT_ROOT "ecc-client-key.der";
  11533. static const char* eccClientCert = CERT_ROOT "client-ecc-cert.der";
  11534. #endif
  11535. #endif /* HAVE_ECC */
  11536. #ifdef HAVE_ED25519
  11537. #ifdef WOLFSSL_TEST_CERT
  11538. static const char* serverEd25519Cert =
  11539. CERT_ROOT "ed25519/server-ed25519.der";
  11540. static const char* caEd25519Cert =
  11541. CERT_ROOT "ed25519/ca-ed25519.der";
  11542. #endif
  11543. #endif
  11544. #ifdef HAVE_ED448
  11545. #ifdef WOLFSSL_TEST_CERT
  11546. static const char* serverEd448Cert =
  11547. CERT_ROOT "ed448/server-ed448.der";
  11548. static const char* caEd448Cert = CERT_ROOT "ed448/ca-ed448.der";
  11549. #endif
  11550. #endif
  11551. #endif /* !USE_CERT_BUFFER_* */
  11552. #if !defined(NO_ASN_TIME) && !defined(NO_RSA) && defined(WOLFSSL_TEST_CERT) && \
  11553. !defined(NO_FILESYSTEM)
  11554. static const char* certExtNc =
  11555. CERT_ROOT "test" CERT_PATH_SEP "cert-ext-nc.der";
  11556. static const char* certExtIa =
  11557. CERT_ROOT "test" CERT_PATH_SEP "cert-ext-ia.der";
  11558. static const char* certExtNct =
  11559. CERT_ROOT "test" CERT_PATH_SEP "cert-ext-nct.der";
  11560. #endif
  11561. #ifndef NO_WRITE_TEMP_FILES
  11562. #ifdef HAVE_ECC
  11563. #ifdef WOLFSSL_CERT_GEN
  11564. static const char* certEccPemFile = CERT_WRITE_TEMP_DIR "certecc.pem";
  11565. static const char* certEccDerFile = CERT_WRITE_TEMP_DIR "certecc.der";
  11566. #endif
  11567. #if defined(WOLFSSL_CERT_GEN) && !defined(NO_RSA)
  11568. static const char* certEccRsaPemFile = CERT_WRITE_TEMP_DIR "certeccrsa.pem";
  11569. static const char* certEccRsaDerFile = CERT_WRITE_TEMP_DIR "certeccrsa.der";
  11570. #endif
  11571. #if defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG) && \
  11572. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  11573. static const char* eccCaKeyPemFile = CERT_WRITE_TEMP_DIR "ecc-key.pem";
  11574. static const char* eccPubKeyDerFile = CERT_WRITE_TEMP_DIR "ecc-public-key.der";
  11575. static const char* eccCaKeyTempFile = CERT_WRITE_TEMP_DIR "ecc-key.der";
  11576. #if defined(HAVE_PKCS8) && !defined(WC_NO_RNG) && \
  11577. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  11578. static const char* eccPkcs8KeyDerFile = CERT_WRITE_TEMP_DIR "ecc-key-pkcs8.der";
  11579. #endif
  11580. #endif /* HAVE_ECC_KEY_EXPORT */
  11581. #endif /* HAVE_ECC */
  11582. #ifndef NO_RSA
  11583. #ifdef WOLFSSL_CERT_GEN
  11584. static const char* otherCertDerFile = CERT_WRITE_TEMP_DIR "othercert.der";
  11585. static const char* certDerFile = CERT_WRITE_TEMP_DIR "cert.der";
  11586. static const char* otherCertPemFile = CERT_WRITE_TEMP_DIR "othercert.pem";
  11587. static const char* certPemFile = CERT_WRITE_TEMP_DIR "cert.pem";
  11588. #if defined(WOLFSSL_CERT_REQ) && !defined(WOLFSSL_NO_MALLOC)
  11589. static const char* certReqDerFile = CERT_WRITE_TEMP_DIR "certreq.der";
  11590. static const char* certReqPemFile = CERT_WRITE_TEMP_DIR "certreq.pem";
  11591. #endif
  11592. #endif
  11593. #endif /* !NO_RSA */
  11594. #if !defined(NO_RSA) || !defined(NO_DSA)
  11595. #ifdef WOLFSSL_KEY_GEN
  11596. static const char* keyDerFile = CERT_WRITE_TEMP_DIR "key.der";
  11597. static const char* keyPemFile = CERT_WRITE_TEMP_DIR "key.pem";
  11598. #endif
  11599. #endif
  11600. #endif /* !NO_WRITE_TEMP_FILES */
  11601. #endif /* !NO_FILESYSTEM */
  11602. #if defined(WOLFSSL_CERT_GEN) && (!defined(NO_RSA) || defined(HAVE_ECC)) || \
  11603. (defined(WOLFSSL_TEST_CERT) && (defined(HAVE_ED25519) || defined(HAVE_ED448)))
  11604. static CertName certDefaultName;
  11605. static void initDefaultName(void)
  11606. {
  11607. #if defined(WOLFSSL_MULTI_ATTRIB) && defined(WOLFSSL_TEST_CERT)
  11608. NameAttrib* n;
  11609. #endif
  11610. XMEMCPY(certDefaultName.country, "US", sizeof("US"));
  11611. certDefaultName.countryEnc = CTC_PRINTABLE;
  11612. XMEMCPY(certDefaultName.state, "Oregon", sizeof("Oregon"));
  11613. certDefaultName.stateEnc = CTC_UTF8;
  11614. XMEMCPY(certDefaultName.street, "Main St", sizeof("Main St"));
  11615. certDefaultName.streetEnc = CTC_UTF8;
  11616. XMEMCPY(certDefaultName.locality, "Portland", sizeof("Portland"));
  11617. certDefaultName.localityEnc = CTC_UTF8;
  11618. XMEMCPY(certDefaultName.sur, "Test", sizeof("Test"));
  11619. certDefaultName.surEnc = CTC_UTF8;
  11620. XMEMCPY(certDefaultName.org, "wolfSSL", sizeof("wolfSSL"));
  11621. certDefaultName.orgEnc = CTC_UTF8;
  11622. XMEMCPY(certDefaultName.unit, "Development", sizeof("Development"));
  11623. certDefaultName.unitEnc = CTC_UTF8;
  11624. XMEMCPY(certDefaultName.commonName, "www.wolfssl.com", sizeof("www.wolfssl.com"));
  11625. certDefaultName.commonNameEnc = CTC_UTF8;
  11626. XMEMCPY(certDefaultName.serialDev, "wolfSSL12345", sizeof("wolfSSL12345"));
  11627. certDefaultName.serialDevEnc = CTC_PRINTABLE;
  11628. XMEMCPY(certDefaultName.postalCode, "12-456", sizeof("12-456"));
  11629. certDefaultName.postalCodeEnc = CTC_PRINTABLE;
  11630. #ifdef WOLFSSL_CERT_EXT
  11631. XMEMCPY(certDefaultName.busCat, "Private Organization", sizeof("Private Organization"));
  11632. certDefaultName.busCatEnc = CTC_UTF8;
  11633. XMEMCPY(certDefaultName.joiSt, "US", sizeof("US"));
  11634. certDefaultName.joiStEnc = CTC_PRINTABLE;
  11635. XMEMCPY(certDefaultName.joiC, "Oregon", sizeof("Oregon"));
  11636. certDefaultName.joiCEnc = CTC_PRINTABLE;
  11637. #endif
  11638. XMEMCPY(certDefaultName.email, "info@wolfssl.com", sizeof("info@wolfssl.com"));
  11639. XMEMCPY(certDefaultName.userId, "TestUserID", sizeof("TestUserID"));
  11640. certDefaultName.userIdEnc = CTC_PRINTABLE;
  11641. #if defined(WOLFSSL_MULTI_ATTRIB) && defined(WOLFSSL_TEST_CERT)
  11642. /* test having additional OUs and setting DC */
  11643. n = &certDefaultName.name[0];
  11644. n->id = ASN_ORGUNIT_NAME;
  11645. n->type = CTC_UTF8;
  11646. n->sz = sizeof("Development-2");
  11647. XMEMCPY(n->value, "Development-2", sizeof("Development-2"));
  11648. #if CTC_MAX_ATTRIB > 3
  11649. n = &certDefaultName.name[1];
  11650. n->id = ASN_DOMAIN_COMPONENT;
  11651. n->type = CTC_UTF8;
  11652. n->sz = sizeof("com");
  11653. XMEMCPY(n->value, "com", sizeof("com"));
  11654. n = &certDefaultName.name[2];
  11655. n->id = ASN_DOMAIN_COMPONENT;
  11656. n->type = CTC_UTF8;
  11657. n->sz = sizeof("wolfssl");
  11658. XMEMCPY(n->value, "wolfssl", sizeof("wolfssl"));
  11659. #endif
  11660. #endif /* WOLFSSL_MULTI_ATTRIB && WOLFSSL_TEST_CERT */
  11661. #ifdef WOLFSSL_CUSTOM_OID
  11662. /* TODO: Add test case for custom OID's */
  11663. #endif
  11664. }
  11665. #ifdef WOLFSSL_CERT_EXT
  11666. #if ((defined(HAVE_ED25519) || defined(HAVE_ED448)) && \
  11667. defined(WOLFSSL_TEST_CERT)) || defined(HAVE_ECC)
  11668. WOLFSSL_SMALL_STACK_STATIC const char certKeyUsage[] =
  11669. "digitalSignature,nonRepudiation";
  11670. #endif
  11671. #if defined(WOLFSSL_CERT_REQ) && !defined(NO_RSA)
  11672. WOLFSSL_SMALL_STACK_STATIC const char certKeyUsage2[] =
  11673. "digitalSignature,nonRepudiation,keyEncipherment,keyAgreement";
  11674. #endif
  11675. #endif /* WOLFSSL_CERT_EXT */
  11676. #endif /* WOLFSSL_CERT_GEN */
  11677. #ifndef NO_RSA
  11678. #if !defined(NO_ASN_TIME) && !defined(NO_RSA) && defined(WOLFSSL_TEST_CERT) && \
  11679. !defined(NO_FILESYSTEM)
  11680. static byte minSerial[] = { 0x02, 0x01, 0x01 };
  11681. static byte minName[] = { 0x30, 0x00 };
  11682. static byte nameBad[] = {
  11683. 0x30, 0x08,
  11684. 0x31, 0x06,
  11685. 0x30, 0x04,
  11686. 0x06, 0x02,
  11687. 0x55, 0x04,
  11688. };
  11689. static byte minDates[] = {
  11690. 0x30, 0x1e,
  11691. 0x17, 0x0d,
  11692. 0x31, 0x38, 0x30, 0x34, 0x31, 0x33, 0x31, 0x35,
  11693. 0x32, 0x33, 0x31, 0x30, 0x5a,
  11694. 0x17, 0x0d,
  11695. 0x32, 0x31, 0x30, 0x31, 0x30, 0x37, 0x31, 0x35,
  11696. 0x32, 0x33, 0x31, 0x30, 0x5a
  11697. };
  11698. static byte minPubKey[] = {
  11699. 0x30, 0x1c,
  11700. 0x30, 0x0d,
  11701. 0x06, 0x09,
  11702. 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01,
  11703. 0x01,
  11704. 0x05, 0x00,
  11705. 0x03, 0x0b,
  11706. 0x00, 0x30, 0x08,
  11707. 0x02, 0x01,
  11708. 0x03,
  11709. 0x02, 0x03,
  11710. 0x01, 0x00, 0x01
  11711. };
  11712. static byte minSigAlg[] = {
  11713. 0x30, 0x0d,
  11714. 0x06, 0x09,
  11715. 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01,
  11716. 0x0b,
  11717. 0x05, 0x00
  11718. };
  11719. static byte minSig[] = {
  11720. 0x03, 0x01,
  11721. 0x00
  11722. };
  11723. static int add_seq(byte* certData, int offset, byte* data, byte length)
  11724. {
  11725. XMEMMOVE(certData + offset + 2, data, length);
  11726. certData[offset++] = 0x30;
  11727. certData[offset++] = length;
  11728. return offset + length;
  11729. }
  11730. static int add_data(byte* certData, int offset, byte* data, byte length)
  11731. {
  11732. XMEMCPY(certData + offset, data, length);
  11733. return offset + length;
  11734. }
  11735. static int cert_asn1_test(void)
  11736. {
  11737. int ret;
  11738. int len[3];
  11739. DecodedCert cert;
  11740. byte certData[114];
  11741. byte* badCert = NULL;
  11742. len[2] = add_data(certData, 0, minSerial, (byte)sizeof(minSerial));
  11743. len[2] = add_data(certData, len[2], minSigAlg, (byte)sizeof(minSigAlg));
  11744. len[2] = add_data(certData, len[2], minName, (byte)sizeof(minName));
  11745. len[2] = add_data(certData, len[2], minDates, (byte)sizeof(minDates));
  11746. len[2] = add_data(certData, len[2], minName, (byte)sizeof(minName));
  11747. len[2] = add_data(certData, len[2], minPubKey, (byte)sizeof(minPubKey));
  11748. len[1] = add_seq(certData, 0, certData, len[2]);
  11749. len[1] = add_data(certData, len[1], minSigAlg, (byte)sizeof(minSigAlg));
  11750. len[1] = add_data(certData, len[1], minSig, (byte)sizeof(minSig));
  11751. len[0] = add_seq(certData, 0, certData, len[1]);
  11752. /* Minimal good certificate */
  11753. InitDecodedCert(&cert, certData, len[0], 0);
  11754. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL);
  11755. FreeDecodedCert(&cert);
  11756. if (ret != 0) {
  11757. ERROR_OUT(-7300, done);
  11758. }
  11759. /* Bad issuer name */
  11760. len[2] = add_data(certData, 0, minSerial, (byte)sizeof(minSerial));
  11761. len[2] = add_data(certData, len[2], minSigAlg, (byte)sizeof(minSigAlg));
  11762. len[2] = add_data(certData, len[2], nameBad, (byte)sizeof(nameBad));
  11763. len[2] = add_data(certData, len[2], minDates, (byte)sizeof(minDates));
  11764. len[2] = add_data(certData, len[2], minName, (byte)sizeof(minName));
  11765. len[2] = add_data(certData, len[2], minPubKey, (byte)sizeof(minPubKey));
  11766. len[1] = add_seq(certData, 0, certData, len[2]);
  11767. len[1] = add_data(certData, len[1], minSigAlg, (byte)sizeof(minSigAlg));
  11768. len[1] = add_data(certData, len[1], minSig, (byte)sizeof(minSig));
  11769. len[0] = add_seq(certData, 0, certData, len[1]);
  11770. /* Put data into allocated buffer to allow access error checking. */
  11771. badCert = (byte*)XMALLOC(len[0], HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11772. XMEMCPY(badCert, certData, len[0]);
  11773. InitDecodedCert(&cert, badCert, len[0], 0);
  11774. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL);
  11775. FreeDecodedCert(&cert);
  11776. if (ret != ASN_PARSE_E) {
  11777. ERROR_OUT(-7301, done);
  11778. }
  11779. XFREE(badCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11780. badCert = NULL;
  11781. ret = 0;
  11782. done:
  11783. if (badCert != NULL)
  11784. XFREE(badCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11785. return ret;
  11786. }
  11787. WOLFSSL_TEST_SUBROUTINE int cert_test(void)
  11788. {
  11789. #if !defined(NO_FILESYSTEM)
  11790. DecodedCert cert;
  11791. byte* tmp;
  11792. size_t bytes;
  11793. XFILE file;
  11794. int ret;
  11795. tmp = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11796. if (tmp == NULL)
  11797. return -7400;
  11798. /* Certificate with Name Constraints extension. */
  11799. file = XFOPEN(certExtNc, "rb");
  11800. if (!file) {
  11801. ERROR_OUT(-7401, done);
  11802. }
  11803. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  11804. XFCLOSE(file);
  11805. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  11806. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL);
  11807. if (ret != 0) {
  11808. ERROR_OUT(-7402, done);
  11809. }
  11810. FreeDecodedCert(&cert);
  11811. /* Certificate with Inhibit Any Policy extension. */
  11812. file = XFOPEN(certExtIa, "rb");
  11813. if (!file) {
  11814. ERROR_OUT(-7403, done);
  11815. }
  11816. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  11817. XFCLOSE(file);
  11818. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  11819. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL);
  11820. if (ret != 0) {
  11821. ERROR_OUT(-7404, done);
  11822. }
  11823. FreeDecodedCert(&cert);
  11824. /* Certificate with Netscape Certificate Type extension. */
  11825. file = XFOPEN(certExtNct, "rb");
  11826. if (!file) {
  11827. ERROR_OUT(-7405, done);
  11828. }
  11829. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  11830. XFCLOSE(file);
  11831. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  11832. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL);
  11833. #ifndef IGNORE_NETSCAPE_CERT_TYPE
  11834. if (ret != 0) {
  11835. ERROR_OUT(-7406, done);
  11836. }
  11837. #else
  11838. if (ret != ASN_CRIT_EXT_E) {
  11839. ERROR_OUT(-7407, done);
  11840. }
  11841. ret = 0;
  11842. #endif
  11843. done:
  11844. FreeDecodedCert(&cert);
  11845. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11846. #endif /* !NO_FILESYSTEM */
  11847. if (ret == 0)
  11848. ret = cert_asn1_test();
  11849. return ret;
  11850. }
  11851. #endif /* WOLFSSL_TEST_CERT */
  11852. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_TEST_CERT) && \
  11853. !defined(NO_FILESYSTEM) && defined(WOLFSSL_CERT_GEN)
  11854. WOLFSSL_TEST_SUBROUTINE int certext_test(void)
  11855. {
  11856. DecodedCert cert;
  11857. byte* tmp;
  11858. size_t bytes;
  11859. XFILE file;
  11860. int ret;
  11861. /* created from rsa_test : othercert.der */
  11862. byte skid_rsa[] = "\x33\xD8\x45\x66\xD7\x68\x87\x18\x7E\x54"
  11863. "\x0D\x70\x27\x91\xC7\x26\xD7\x85\x65\xC0";
  11864. /* created from rsa_test : othercert.der */
  11865. byte akid_rsa[] = "\x27\x8E\x67\x11\x74\xC3\x26\x1D\x3F\xED"
  11866. "\x33\x63\xB3\xA4\xD8\x1D\x30\xE5\xE8\xD5";
  11867. #ifdef HAVE_ECC
  11868. /* created from ecc_test_cert_gen : certecc.der */
  11869. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  11870. /* Authority key id from ./certs/ca-ecc384-cert.pem */
  11871. byte akid_ecc[] = "\xAB\xE0\xC3\x26\x4C\x18\xD4\x72\xBB\xD2"
  11872. "\x84\x8C\x9C\x0A\x05\x92\x80\x12\x53\x52";
  11873. #else
  11874. /* Authority key id from ./certs/ca-ecc-cert.pem */
  11875. byte akid_ecc[] = "\x56\x8E\x9A\xC3\xF0\x42\xDE\x18\xB9\x45"
  11876. "\x55\x6E\xF9\x93\xCF\xEA\xC3\xF3\xA5\x21";
  11877. #endif
  11878. #endif /* HAVE_ECC */
  11879. /* created from rsa_test : cert.der */
  11880. byte kid_ca[] = "\x33\xD8\x45\x66\xD7\x68\x87\x18\x7E\x54"
  11881. "\x0D\x70\x27\x91\xC7\x26\xD7\x85\x65\xC0";
  11882. tmp = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11883. if (tmp == NULL)
  11884. return -7500;
  11885. /* load othercert.der (Cert signed by an authority) */
  11886. file = XFOPEN(otherCertDerFile, "rb");
  11887. if (!file) {
  11888. XFREE(tmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  11889. return -7501;
  11890. }
  11891. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  11892. XFCLOSE(file);
  11893. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  11894. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, 0);
  11895. if (ret != 0)
  11896. return -7502;
  11897. /* check the SKID from a RSA certificate */
  11898. if (XMEMCMP(skid_rsa, cert.extSubjKeyId, sizeof(cert.extSubjKeyId)))
  11899. return -7503;
  11900. /* check the AKID from an RSA certificate */
  11901. if (XMEMCMP(akid_rsa, cert.extAuthKeyId, sizeof(cert.extAuthKeyId)))
  11902. return -7504;
  11903. /* check the Key Usage from an RSA certificate */
  11904. if (!cert.extKeyUsageSet)
  11905. return -7505;
  11906. if (cert.extKeyUsage != (KEYUSE_KEY_ENCIPHER|KEYUSE_KEY_AGREE))
  11907. return -7506;
  11908. /* check the CA Basic Constraints from an RSA certificate */
  11909. if (cert.isCA)
  11910. return -7507;
  11911. #ifndef WOLFSSL_SEP /* test only if not using SEP policies */
  11912. /* check the Certificate Policies Id */
  11913. if (cert.extCertPoliciesNb != 1)
  11914. return -7508;
  11915. if (strncmp(cert.extCertPolicies[0], "2.16.840.1.101.3.4.1.42", 23))
  11916. return -7509;
  11917. #endif
  11918. FreeDecodedCert(&cert);
  11919. #ifdef HAVE_ECC
  11920. /* load certecc.der (Cert signed by our ECC CA test in ecc_test_cert_gen) */
  11921. file = XFOPEN(certEccDerFile, "rb");
  11922. if (!file) {
  11923. XFREE(tmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  11924. return -7510;
  11925. }
  11926. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  11927. XFCLOSE(file);
  11928. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  11929. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, 0);
  11930. if (ret != 0)
  11931. return -7511;
  11932. /* check the SKID from a ECC certificate - generated dynamically */
  11933. /* check the AKID from an ECC certificate */
  11934. if (XMEMCMP(akid_ecc, cert.extAuthKeyId, sizeof(cert.extAuthKeyId)))
  11935. return -7512;
  11936. /* check the Key Usage from an ECC certificate */
  11937. if (!cert.extKeyUsageSet)
  11938. return -7513;
  11939. if (cert.extKeyUsage != (KEYUSE_DIGITAL_SIG|KEYUSE_CONTENT_COMMIT))
  11940. return -7514;
  11941. /* check the CA Basic Constraints from an ECC certificate */
  11942. if (cert.isCA)
  11943. return -7515;
  11944. #ifndef WOLFSSL_SEP /* test only if not using SEP policies */
  11945. /* check the Certificate Policies Id */
  11946. if (cert.extCertPoliciesNb != 2)
  11947. return -7516;
  11948. if (strncmp(cert.extCertPolicies[0], "2.4.589440.587.101.2.1.9632587.1", 32))
  11949. return -7517;
  11950. if (strncmp(cert.extCertPolicies[1], "1.2.13025.489.1.113549", 22))
  11951. return -7518;
  11952. #endif
  11953. FreeDecodedCert(&cert);
  11954. #endif /* HAVE_ECC */
  11955. /* load cert.der (self signed certificate) */
  11956. file = XFOPEN(certDerFile, "rb");
  11957. if (!file) {
  11958. XFREE(tmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  11959. return -7519;
  11960. }
  11961. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  11962. XFCLOSE(file);
  11963. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  11964. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, 0);
  11965. if (ret != 0)
  11966. return -7520;
  11967. /* check the SKID from a CA certificate */
  11968. if (XMEMCMP(kid_ca, cert.extSubjKeyId, sizeof(cert.extSubjKeyId)))
  11969. return -7521;
  11970. /* check the AKID from an CA certificate */
  11971. if (XMEMCMP(kid_ca, cert.extAuthKeyId, sizeof(cert.extAuthKeyId)))
  11972. return -7522;
  11973. /* check the Key Usage from CA certificate */
  11974. if (!cert.extKeyUsageSet)
  11975. return -7523;
  11976. if (cert.extKeyUsage != (KEYUSE_KEY_CERT_SIGN|KEYUSE_CRL_SIGN))
  11977. return -7524;
  11978. /* check the CA Basic Constraints CA certificate */
  11979. if (!cert.isCA)
  11980. return -7525;
  11981. #ifndef WOLFSSL_SEP /* test only if not using SEP policies */
  11982. /* check the Certificate Policies Id */
  11983. if (cert.extCertPoliciesNb != 2)
  11984. return -7526;
  11985. if (strncmp(cert.extCertPolicies[0], "2.16.840.1.101.3.4.1.42", 23))
  11986. return -7527;
  11987. if (strncmp(cert.extCertPolicies[1], "1.2.840.113549.1.9.16.6.5", 25))
  11988. return -7528;
  11989. #endif
  11990. FreeDecodedCert(&cert);
  11991. XFREE(tmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  11992. return 0;
  11993. }
  11994. #endif /* WOLFSSL_CERT_EXT && WOLFSSL_TEST_CERT &&
  11995. !NO_FILESYSTEM && WOLFSSL_CERT_GEN */
  11996. #if defined(WOLFSSL_CERT_GEN_CACHE) && defined(WOLFSSL_TEST_CERT) && \
  11997. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  11998. WOLFSSL_TEST_SUBROUTINE int decodedCertCache_test(void)
  11999. {
  12000. int ret = 0;
  12001. Cert cert;
  12002. FILE* file;
  12003. byte* der;
  12004. word32 derSz;
  12005. derSz = FOURK_BUF;
  12006. der = (byte *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12007. if (der == NULL)
  12008. ret = -7600;
  12009. if (ret == 0) {
  12010. /* load cert.der */
  12011. file = XFOPEN(certDerFile, "rb");
  12012. if (file != NULL) {
  12013. derSz = (word32)XFREAD(der, 1, FOURK_BUF, file);
  12014. XFCLOSE(file);
  12015. }
  12016. else
  12017. ret = -7601;
  12018. }
  12019. if (ret == 0) {
  12020. if (wc_InitCert_ex(&cert, HEAP_HINT, devId)) {
  12021. ret = -7602;
  12022. }
  12023. }
  12024. if (ret == 0) {
  12025. ret = wc_SetSubjectBuffer(&cert, der, derSz);
  12026. }
  12027. if (ret == 0) {
  12028. if(wc_SetSubjectBuffer(NULL, der, derSz) != BAD_FUNC_ARG)
  12029. ret = -7603;
  12030. }
  12031. if (ret == 0) {
  12032. if (wc_SetSubjectRaw(&cert, der, derSz) != 0)
  12033. ret = -7604;
  12034. }
  12035. if (ret == 0) {
  12036. if(wc_SetSubjectRaw(NULL, der, derSz) != BAD_FUNC_ARG)
  12037. ret = -7605;
  12038. }
  12039. if (ret == 0) {
  12040. if(wc_SetIssuerBuffer(&cert, der, derSz) != 0)
  12041. ret = -7606;
  12042. }
  12043. if (ret == 0) {
  12044. if(wc_SetIssuerBuffer(NULL, der, derSz) != BAD_FUNC_ARG)
  12045. ret = -7607;
  12046. }
  12047. if (ret == 0) {
  12048. if(wc_SetIssuerRaw(&cert, der, derSz) != 0)
  12049. ret = -7608;
  12050. }
  12051. if (ret == 0) {
  12052. if(wc_SetIssuerRaw(NULL, der, derSz) != BAD_FUNC_ARG)
  12053. ret = -7609;
  12054. }
  12055. #ifdef WOLFSSL_ALT_NAMES
  12056. if (ret == 0) {
  12057. if(wc_SetAltNamesBuffer(&cert, der, derSz) != 0)
  12058. ret = -7610;
  12059. }
  12060. if (ret == 0) {
  12061. if(wc_SetAltNamesBuffer(NULL, der, derSz) != BAD_FUNC_ARG)
  12062. ret = -7611;
  12063. }
  12064. if (ret == 0) {
  12065. if(wc_SetDatesBuffer(&cert, der, derSz) != 0)
  12066. ret = -7612;
  12067. }
  12068. if (ret == 0) {
  12069. if(wc_SetDatesBuffer(NULL, der, derSz) != BAD_FUNC_ARG)
  12070. ret = -7613;
  12071. }
  12072. #endif
  12073. if (ret == 0) {
  12074. if(wc_SetAuthKeyIdFromCert(&cert, der, derSz) != 0)
  12075. ret = -7614;
  12076. }
  12077. if (ret == 0) {
  12078. if(wc_SetAuthKeyIdFromCert(NULL, der, derSz) != BAD_FUNC_ARG)
  12079. ret = -7615;
  12080. }
  12081. wc_SetCert_Free(&cert);
  12082. if (ret == 0) {
  12083. if(cert.decodedCert != NULL)
  12084. ret = -7616;
  12085. }
  12086. XFREE(der, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  12087. return ret;
  12088. }
  12089. #endif /* defined(WOLFSSL_CERT_GEN_CACHE) && defined(WOLFSSL_TEST_CERT) &&
  12090. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN) */
  12091. #define RSA_TEST_BYTES 512 /* up to 4096-bit key */
  12092. #if !defined(NO_ASN) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
  12093. !defined(WOLFSSL_RSA_VERIFY_ONLY)
  12094. static int rsa_flatten_test(RsaKey* key)
  12095. {
  12096. int ret;
  12097. byte e[RSA_TEST_BYTES];
  12098. byte n[RSA_TEST_BYTES];
  12099. word32 eSz = sizeof(e);
  12100. word32 nSz = sizeof(n);
  12101. /* Parameter Validation testing. */
  12102. ret = wc_RsaFlattenPublicKey(NULL, e, &eSz, n, &nSz);
  12103. #ifdef HAVE_USER_RSA
  12104. /* Implementation using IPP Libraries returns:
  12105. * -101 = USER_CRYPTO_ERROR
  12106. */
  12107. if (ret == 0)
  12108. #else
  12109. if (ret != BAD_FUNC_ARG)
  12110. #endif
  12111. return -7620;
  12112. ret = wc_RsaFlattenPublicKey(key, NULL, &eSz, n, &nSz);
  12113. #ifdef HAVE_USER_RSA
  12114. /* Implementation using IPP Libraries returns:
  12115. * -101 = USER_CRYPTO_ERROR
  12116. */
  12117. if (ret == 0)
  12118. #else
  12119. if (ret != BAD_FUNC_ARG)
  12120. #endif
  12121. return -7621;
  12122. ret = wc_RsaFlattenPublicKey(key, e, NULL, n, &nSz);
  12123. #ifdef HAVE_USER_RSA
  12124. /* Implementation using IPP Libraries returns:
  12125. * -101 = USER_CRYPTO_ERROR
  12126. */
  12127. if (ret == 0)
  12128. #else
  12129. if (ret != BAD_FUNC_ARG)
  12130. #endif
  12131. return -7622;
  12132. ret = wc_RsaFlattenPublicKey(key, e, &eSz, NULL, &nSz);
  12133. #ifdef HAVE_USER_RSA
  12134. /* Implementation using IPP Libraries returns:
  12135. * -101 = USER_CRYPTO_ERROR
  12136. */
  12137. if (ret == 0)
  12138. #else
  12139. if (ret != BAD_FUNC_ARG)
  12140. #endif
  12141. return -7623;
  12142. ret = wc_RsaFlattenPublicKey(key, e, &eSz, n, NULL);
  12143. #ifdef HAVE_USER_RSA
  12144. /* Implementation using IPP Libraries returns:
  12145. * -101 = USER_CRYPTO_ERROR
  12146. */
  12147. if (ret == 0)
  12148. #else
  12149. if (ret != BAD_FUNC_ARG)
  12150. #endif
  12151. return -7624;
  12152. ret = wc_RsaFlattenPublicKey(key, e, &eSz, n, &nSz);
  12153. if (ret != 0)
  12154. return -7625;
  12155. eSz = 0;
  12156. ret = wc_RsaFlattenPublicKey(key, e, &eSz, n, &nSz);
  12157. #ifdef HAVE_USER_RSA
  12158. /* Implementation using IPP Libraries returns:
  12159. * -101 = USER_CRYPTO_ERROR
  12160. */
  12161. if (ret == 0)
  12162. #elif defined(HAVE_FIPS) && \
  12163. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 2))
  12164. if (ret != 0)
  12165. #else
  12166. if (ret != RSA_BUFFER_E)
  12167. #endif
  12168. return -7626;
  12169. eSz = sizeof(e);
  12170. nSz = 0;
  12171. ret = wc_RsaFlattenPublicKey(key, e, &eSz, n, &nSz);
  12172. #ifdef HAVE_USER_RSA
  12173. /* Implementation using IPP Libraries returns:
  12174. * -101 = USER_CRYPTO_ERROR
  12175. */
  12176. if (ret == 0)
  12177. #else
  12178. if (ret != RSA_BUFFER_E)
  12179. #endif
  12180. return -7627;
  12181. return 0;
  12182. }
  12183. #endif /* NO_ASN */
  12184. #if !defined(HAVE_FIPS) && !defined(HAVE_USER_RSA) && !defined(NO_ASN) \
  12185. && !defined(WOLFSSL_RSA_VERIFY_ONLY)
  12186. static int rsa_export_key_test(RsaKey* key)
  12187. {
  12188. int ret;
  12189. byte e[3];
  12190. word32 eSz = sizeof(e);
  12191. byte n[RSA_TEST_BYTES];
  12192. word32 nSz = sizeof(n);
  12193. byte d[RSA_TEST_BYTES];
  12194. word32 dSz = sizeof(d);
  12195. byte p[RSA_TEST_BYTES/2];
  12196. word32 pSz = sizeof(p);
  12197. byte q[RSA_TEST_BYTES/2];
  12198. word32 qSz = sizeof(q);
  12199. word32 zero = 0;
  12200. ret = wc_RsaExportKey(NULL, e, &eSz, n, &nSz, d, &dSz, p, &pSz, q, &qSz);
  12201. if (ret != BAD_FUNC_ARG)
  12202. return -7630;
  12203. ret = wc_RsaExportKey(key, NULL, &eSz, n, &nSz, d, &dSz, p, &pSz, q, &qSz);
  12204. if (ret != BAD_FUNC_ARG)
  12205. return -7631;
  12206. ret = wc_RsaExportKey(key, e, NULL, n, &nSz, d, &dSz, p, &pSz, q, &qSz);
  12207. if (ret != BAD_FUNC_ARG)
  12208. return -7632;
  12209. ret = wc_RsaExportKey(key, e, &eSz, NULL, &nSz, d, &dSz, p, &pSz, q, &qSz);
  12210. if (ret != BAD_FUNC_ARG)
  12211. return -7633;
  12212. ret = wc_RsaExportKey(key, e, &eSz, n, NULL, d, &dSz, p, &pSz, q, &qSz);
  12213. if (ret != BAD_FUNC_ARG)
  12214. return -7634;
  12215. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, NULL, &dSz, p, &pSz, q, &qSz);
  12216. if (ret != BAD_FUNC_ARG)
  12217. return -7635;
  12218. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, NULL, p, &pSz, q, &qSz);
  12219. if (ret != BAD_FUNC_ARG)
  12220. return -7636;
  12221. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, NULL, &pSz, q, &qSz);
  12222. if (ret != BAD_FUNC_ARG)
  12223. return -7637;
  12224. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, NULL, q, &qSz);
  12225. if (ret != BAD_FUNC_ARG)
  12226. return -7638;
  12227. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, &pSz, NULL, &qSz);
  12228. if (ret != BAD_FUNC_ARG)
  12229. return -7639;
  12230. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, &pSz, q, NULL);
  12231. if (ret != BAD_FUNC_ARG)
  12232. return -7640;
  12233. ret = wc_RsaExportKey(key, e, &zero, n, &nSz, d, &dSz, p, &pSz, q, &qSz);
  12234. if (ret != RSA_BUFFER_E)
  12235. return -7641;
  12236. ret = wc_RsaExportKey(key, e, &eSz, n, &zero, d, &dSz, p, &pSz, q, &qSz);
  12237. if (ret != RSA_BUFFER_E)
  12238. return -7642;
  12239. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  12240. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &zero, p, &pSz, q, &qSz);
  12241. if (ret != RSA_BUFFER_E)
  12242. return -7643;
  12243. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, &zero, q, &qSz);
  12244. if (ret != RSA_BUFFER_E)
  12245. return -7644;
  12246. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, &pSz, q, &zero);
  12247. if (ret != RSA_BUFFER_E)
  12248. return -7645;
  12249. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  12250. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, &pSz, q, &qSz);
  12251. if (ret != 0)
  12252. return -7646;
  12253. return 0;
  12254. }
  12255. #endif /* !HAVE_FIPS && !USER_RSA && !NO_ASN */
  12256. #ifndef NO_SIG_WRAPPER
  12257. static int rsa_sig_test(RsaKey* key, word32 keyLen, int modLen, WC_RNG* rng)
  12258. {
  12259. int ret;
  12260. word32 sigSz;
  12261. WOLFSSL_SMALL_STACK_STATIC const byte in[] = TEST_STRING;
  12262. WOLFSSL_SMALL_STACK_STATIC const byte hash[] = {
  12263. 0xf2, 0x02, 0x95, 0x65, 0xcb, 0xf6, 0x2a, 0x59,
  12264. 0x39, 0x2c, 0x05, 0xff, 0x0e, 0x29, 0xaf, 0xfe,
  12265. 0x47, 0x33, 0x8c, 0x99, 0x8d, 0x58, 0x64, 0x83,
  12266. 0xa6, 0x58, 0x0a, 0x33, 0x0b, 0x84, 0x5f, 0x5f
  12267. };
  12268. WOLFSSL_SMALL_STACK_STATIC const byte hashEnc[] = {
  12269. 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
  12270. 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05,
  12271. 0x00, 0x04, 0x20,
  12272. 0xf2, 0x02, 0x95, 0x65, 0xcb, 0xf6, 0x2a, 0x59,
  12273. 0x39, 0x2c, 0x05, 0xff, 0x0e, 0x29, 0xaf, 0xfe,
  12274. 0x47, 0x33, 0x8c, 0x99, 0x8d, 0x58, 0x64, 0x83,
  12275. 0xa6, 0x58, 0x0a, 0x33, 0x0b, 0x84, 0x5f, 0x5f
  12276. };
  12277. word32 inLen = (word32)XSTRLEN((char*)in);
  12278. byte out[RSA_TEST_BYTES];
  12279. /* Parameter Validation testing. */
  12280. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_NONE, key, keyLen);
  12281. if (ret != BAD_FUNC_ARG)
  12282. return -7650;
  12283. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_RSA, key, 0);
  12284. if (ret != BAD_FUNC_ARG)
  12285. return -7651;
  12286. sigSz = (word32)modLen;
  12287. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, NULL,
  12288. inLen, out, &sigSz, key, keyLen, rng);
  12289. if (ret != BAD_FUNC_ARG)
  12290. return -7652;
  12291. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  12292. 0, out, &sigSz, key, keyLen, rng);
  12293. if (ret != BAD_FUNC_ARG)
  12294. return -7653;
  12295. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  12296. inLen, NULL, &sigSz, key, keyLen, rng);
  12297. if (ret != BAD_FUNC_ARG)
  12298. return -7654;
  12299. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  12300. inLen, out, NULL, key, keyLen, rng);
  12301. if (ret != BAD_FUNC_ARG)
  12302. return -7655;
  12303. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  12304. inLen, out, &sigSz, NULL, keyLen, rng);
  12305. if (ret != BAD_FUNC_ARG)
  12306. return -7656;
  12307. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  12308. inLen, out, &sigSz, key, 0, rng);
  12309. if (ret != BAD_FUNC_ARG)
  12310. return -7657;
  12311. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  12312. inLen, out, &sigSz, key, keyLen, NULL);
  12313. #ifdef HAVE_USER_RSA
  12314. /* Implementation using IPP Libraries returns:
  12315. * -101 = USER_CRYPTO_ERROR
  12316. */
  12317. if (ret == 0)
  12318. #elif defined(WOLFSSL_AFALG_XILINX_RSA) || defined(WOLFSSL_XILINX_CRYPT)
  12319. /* blinding / rng handled with hardware acceleration */
  12320. if (ret != 0)
  12321. #elif defined(WOLFSSL_ASYNC_CRYPT) || defined(WOLF_CRYPTO_CB)
  12322. /* async may not require RNG */
  12323. #if defined(WOLF_CRYPTO_CB_ONLY_RSA)
  12324. if (ret != NO_VALID_DEVID)
  12325. #else
  12326. if (ret != 0 && ret != MISSING_RNG_E)
  12327. #endif
  12328. #elif defined(HAVE_FIPS) || !defined(WC_RSA_BLINDING)
  12329. /* FIPS140 implementation does not do blinding */
  12330. if (ret != 0)
  12331. #elif defined(WOLFSSL_RSA_PUBLIC_ONLY) || defined(WOLFSSL_RSA_VERIFY_ONLY)
  12332. if (ret != SIG_TYPE_E)
  12333. #elif defined(WOLFSSL_CRYPTOCELL) || defined(WOLFSSL_SE050)
  12334. /* RNG is handled by hardware */
  12335. if (ret != 0)
  12336. #else
  12337. if (ret != MISSING_RNG_E)
  12338. #endif
  12339. return -7658;
  12340. sigSz = 0;
  12341. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  12342. inLen, out, &sigSz, key, keyLen, rng);
  12343. if (ret != BAD_FUNC_ARG)
  12344. return -7659;
  12345. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, NULL,
  12346. inLen, out, (word32)modLen, key, keyLen);
  12347. if (ret != BAD_FUNC_ARG)
  12348. return -7660;
  12349. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  12350. 0, out, (word32)modLen, key, keyLen);
  12351. if (ret != BAD_FUNC_ARG)
  12352. return -7661;
  12353. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  12354. inLen, NULL, (word32)modLen, key, keyLen);
  12355. if (ret != BAD_FUNC_ARG)
  12356. return -7662;
  12357. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  12358. inLen, out, 0, key, keyLen);
  12359. if (ret != BAD_FUNC_ARG)
  12360. return -7663;
  12361. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  12362. inLen, out, (word32)modLen, NULL, keyLen);
  12363. if (ret != BAD_FUNC_ARG)
  12364. return -7664;
  12365. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  12366. inLen, out, (word32)modLen, key, 0);
  12367. if (ret != BAD_FUNC_ARG)
  12368. return -7665;
  12369. #ifndef HAVE_ECC
  12370. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_ECC, key, keyLen);
  12371. if (ret != SIG_TYPE_E)
  12372. return -7666;
  12373. #endif
  12374. #if defined(WOLF_CRYPTO_CB_ONLY_RSA)
  12375. return 0;
  12376. #endif
  12377. /* Use APIs. */
  12378. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_RSA, key, keyLen);
  12379. if (ret != modLen)
  12380. return -7667;
  12381. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_RSA_W_ENC, key, keyLen);
  12382. if (ret != modLen)
  12383. return -7668;
  12384. sigSz = (word32)ret;
  12385. #if !defined(WOLFSSL_RSA_PUBLIC_ONLY) && !defined(WOLFSSL_RSA_VERIFY_ONLY)
  12386. XMEMSET(out, 0, sizeof(out));
  12387. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  12388. inLen, out, &sigSz, key, keyLen, rng);
  12389. if (ret != 0)
  12390. return -7669;
  12391. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  12392. inLen, out, (word32)modLen, key, keyLen);
  12393. if (ret != 0)
  12394. return -7670;
  12395. sigSz = (word32)sizeof(out);
  12396. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA_W_ENC,
  12397. in, inLen, out, &sigSz, key, keyLen, rng);
  12398. if (ret != 0)
  12399. return -7671;
  12400. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA_W_ENC,
  12401. in, inLen, out, (word32)modLen, key, keyLen);
  12402. if (ret != 0)
  12403. return -7672;
  12404. /* Wrong signature type. */
  12405. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  12406. inLen, out, (word32)modLen, key, keyLen);
  12407. if (ret == 0)
  12408. return -7673;
  12409. /* check hash functions */
  12410. sigSz = (word32)sizeof(out);
  12411. ret = wc_SignatureGenerateHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA,
  12412. hash, (int)sizeof(hash), out, &sigSz, key, keyLen, rng);
  12413. if (ret != 0)
  12414. return -7674;
  12415. ret = wc_SignatureVerifyHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA,
  12416. hash, (int)sizeof(hash), out, (word32)modLen, key, keyLen);
  12417. if (ret != 0)
  12418. return -7675;
  12419. sigSz = (word32)sizeof(out);
  12420. ret = wc_SignatureGenerateHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA_W_ENC,
  12421. hashEnc, (int)sizeof(hashEnc), out, &sigSz, key, keyLen, rng);
  12422. if (ret != 0)
  12423. return -7676;
  12424. ret = wc_SignatureVerifyHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA_W_ENC,
  12425. hashEnc, (int)sizeof(hashEnc), out, (word32)modLen, key, keyLen);
  12426. if (ret != 0)
  12427. return -7677;
  12428. #else
  12429. (void)hash;
  12430. (void)hashEnc;
  12431. #endif /* !WOLFSSL_RSA_PUBLIC_ONLY && !WOLFSSL_RSA_VERIFY_ONLY */
  12432. return 0;
  12433. }
  12434. #endif /* !NO_SIG_WRAPPER */
  12435. #ifdef WC_RSA_NONBLOCK
  12436. static int rsa_nb_test(RsaKey* key, const byte* in, word32 inLen, byte* out,
  12437. word32 outSz, byte* plain, word32 plainSz, WC_RNG* rng)
  12438. {
  12439. int ret = 0, count;
  12440. int signSz = 0;
  12441. RsaNb nb;
  12442. byte* inlinePlain = NULL;
  12443. /* Enable non-blocking RSA mode - provide context */
  12444. ret = wc_RsaSetNonBlock(key, &nb);
  12445. if (ret != 0)
  12446. return ret;
  12447. #ifdef WC_RSA_NONBLOCK_TIME
  12448. /* Enable time based RSA blocking. 8 microseconds max (3.1GHz) */
  12449. ret = wc_RsaSetNonBlockTime(key, 8, 3100);
  12450. if (ret != 0)
  12451. return ret;
  12452. #endif
  12453. count = 0;
  12454. do {
  12455. ret = wc_RsaSSL_Sign(in, inLen, out, outSz, key, rng);
  12456. count++; /* track number of would blocks */
  12457. if (ret == FP_WOULDBLOCK) {
  12458. /* do "other" work here */
  12459. }
  12460. } while (ret == FP_WOULDBLOCK);
  12461. if (ret < 0) {
  12462. return ret;
  12463. }
  12464. #ifdef DEBUG_WOLFSSL
  12465. printf("RSA non-block sign: %d times\n", count);
  12466. #endif
  12467. signSz = ret;
  12468. /* Test non-blocking verify */
  12469. XMEMSET(plain, 0, plainSz);
  12470. count = 0;
  12471. do {
  12472. ret = wc_RsaSSL_Verify(out, (word32)signSz, plain, plainSz, key);
  12473. count++; /* track number of would blocks */
  12474. if (ret == FP_WOULDBLOCK) {
  12475. /* do "other" work here */
  12476. }
  12477. } while (ret == FP_WOULDBLOCK);
  12478. if (ret < 0) {
  12479. return ret;
  12480. }
  12481. #ifdef DEBUG_WOLFSSL
  12482. printf("RSA non-block verify: %d times\n", count);
  12483. #endif
  12484. if (signSz == ret && XMEMCMP(plain, in, (size_t)ret)) {
  12485. return SIG_VERIFY_E;
  12486. }
  12487. /* Test inline non-blocking verify */
  12488. count = 0;
  12489. do {
  12490. ret = wc_RsaSSL_VerifyInline(out, (word32)signSz, &inlinePlain, key);
  12491. count++; /* track number of would blocks */
  12492. if (ret == FP_WOULDBLOCK) {
  12493. /* do "other" work here */
  12494. }
  12495. } while (ret == FP_WOULDBLOCK);
  12496. if (ret < 0) {
  12497. return ret;
  12498. }
  12499. #ifdef DEBUG_WOLFSSL
  12500. printf("RSA non-block inline verify: %d times\n", count);
  12501. #endif
  12502. if (signSz == ret && XMEMCMP(inlinePlain, in, (size_t)ret)) {
  12503. return SIG_VERIFY_E;
  12504. }
  12505. /* Disabling non-block RSA mode */
  12506. ret = wc_RsaSetNonBlock(key, NULL);
  12507. (void)count;
  12508. return 0;
  12509. }
  12510. #endif
  12511. #if !defined(HAVE_USER_RSA) && !defined(NO_ASN)
  12512. static int rsa_decode_test(RsaKey* keyPub)
  12513. {
  12514. int ret;
  12515. word32 inSz;
  12516. word32 inOutIdx;
  12517. WOLFSSL_SMALL_STACK_STATIC const byte n[2] = { 0x00, 0x23 };
  12518. WOLFSSL_SMALL_STACK_STATIC const byte e[2] = { 0x00, 0x03 };
  12519. WOLFSSL_SMALL_STACK_STATIC const byte good[] = { 0x30, 0x06, 0x02, 0x01, 0x23, 0x02, 0x1,
  12520. 0x03 };
  12521. WOLFSSL_SMALL_STACK_STATIC const byte goodAlgId[] = {
  12522. 0x30, 0x18, 0x30, 0x16,
  12523. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
  12524. 0x03, 0x09, 0x00, 0x30, 0x06, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  12525. WOLFSSL_SMALL_STACK_STATIC const byte goodAlgIdNull[] = {
  12526. 0x30, 0x1a, 0x30, 0x18,
  12527. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
  12528. 0x05, 0x00, 0x03, 0x09, 0x00, 0x30, 0x06, 0x02, 0x01, 0x23,
  12529. 0x02, 0x1, 0x03 };
  12530. WOLFSSL_SMALL_STACK_STATIC const byte badAlgIdNull[] = {
  12531. 0x30, 0x1b, 0x30, 0x19,
  12532. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
  12533. 0x05, 0x01, 0x00, 0x03, 0x09, 0x00, 0x30, 0x06, 0x02, 0x01, 0x23,
  12534. 0x02, 0x1, 0x03 };
  12535. WOLFSSL_SMALL_STACK_STATIC const byte badNotBitString[] = {
  12536. 0x30, 0x18, 0x30, 0x16,
  12537. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
  12538. 0x04, 0x09, 0x00, 0x30, 0x06, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  12539. WOLFSSL_SMALL_STACK_STATIC const byte badBitStringLen[] = {
  12540. 0x30, 0x18, 0x30, 0x16,
  12541. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
  12542. 0x03, 0x0a, 0x00, 0x30, 0x06, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  12543. WOLFSSL_SMALL_STACK_STATIC const byte badNoSeq[] = {
  12544. 0x30, 0x16, 0x30, 0x14,
  12545. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
  12546. 0x07, 0x00, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  12547. WOLFSSL_SMALL_STACK_STATIC const byte badNoObj[] = {
  12548. 0x30, 0x0f, 0x30, 0x0d, 0x05, 0x00, 0x03, 0x09, 0x00, 0x30, 0x06,
  12549. 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  12550. WOLFSSL_SMALL_STACK_STATIC const byte badIntN[] = {
  12551. 0x30, 0x06, 0x02, 0x05, 0x23, 0x02, 0x1, 0x03 };
  12552. WOLFSSL_SMALL_STACK_STATIC const byte badNotIntE[] = {
  12553. 0x30, 0x06, 0x02, 0x01, 0x23, 0x04, 0x1, 0x03 };
  12554. WOLFSSL_SMALL_STACK_STATIC const byte badLength[] = {
  12555. 0x30, 0x04, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  12556. WOLFSSL_SMALL_STACK_STATIC const byte badBitStrNoZero[] = {
  12557. 0x30, 0x17, 0x30, 0x15,
  12558. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
  12559. 0x03, 0x08, 0x30, 0x06, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  12560. ret = wc_InitRsaKey(keyPub, NULL);
  12561. if (ret != 0)
  12562. return -7690;
  12563. /* Parameter Validation testing. */
  12564. ret = wc_RsaPublicKeyDecodeRaw(NULL, sizeof(n), e, sizeof(e), keyPub);
  12565. if (ret != BAD_FUNC_ARG) {
  12566. ret = -7691;
  12567. goto done;
  12568. }
  12569. ret = wc_RsaPublicKeyDecodeRaw(n, sizeof(n), NULL, sizeof(e), keyPub);
  12570. if (ret != BAD_FUNC_ARG) {
  12571. ret = -7692;
  12572. goto done;
  12573. }
  12574. ret = wc_RsaPublicKeyDecodeRaw(n, sizeof(n), e, sizeof(e), NULL);
  12575. if (ret != BAD_FUNC_ARG) {
  12576. ret = -7693;
  12577. goto done;
  12578. }
  12579. ret = wc_RsaPublicKeyDecodeRaw(n, (word32)-1, e, sizeof(e), keyPub);
  12580. #if !defined(WOLFSSL_SP_MATH) & !defined(WOLFSSL_SP_MATH_ALL)
  12581. if (ret != 0) {
  12582. #else
  12583. if (ret != ASN_GETINT_E) {
  12584. #endif
  12585. ret = -7694;
  12586. goto done;
  12587. }
  12588. wc_FreeRsaKey(keyPub);
  12589. ret = wc_InitRsaKey(keyPub, NULL);
  12590. if (ret != 0)
  12591. return -7695;
  12592. ret = wc_RsaPublicKeyDecodeRaw(n, sizeof(n), e, (word32)-1, keyPub);
  12593. #if !defined(WOLFSSL_SP_MATH) & !defined(WOLFSSL_SP_MATH_ALL)
  12594. if (ret != 0) {
  12595. #else
  12596. if (ret != ASN_GETINT_E) {
  12597. #endif
  12598. ret = -7696;
  12599. goto done;
  12600. }
  12601. wc_FreeRsaKey(keyPub);
  12602. ret = wc_InitRsaKey(keyPub, NULL);
  12603. if (ret != 0)
  12604. return -7697;
  12605. /* Use API. */
  12606. ret = wc_RsaPublicKeyDecodeRaw(n, sizeof(n), e, sizeof(e), keyPub);
  12607. if (ret != 0) {
  12608. ret = -7698;
  12609. goto done;
  12610. }
  12611. wc_FreeRsaKey(keyPub);
  12612. ret = wc_InitRsaKey(keyPub, NULL);
  12613. if (ret != 0)
  12614. return -7699;
  12615. /* Parameter Validation testing. */
  12616. inSz = sizeof(good);
  12617. ret = wc_RsaPublicKeyDecode(NULL, &inOutIdx, keyPub, inSz);
  12618. if (ret != BAD_FUNC_ARG) {
  12619. ret = -7700;
  12620. goto done;
  12621. }
  12622. ret = wc_RsaPublicKeyDecode(good, NULL, keyPub, inSz);
  12623. if (ret != BAD_FUNC_ARG) {
  12624. ret = -7701;
  12625. goto done;
  12626. }
  12627. ret = wc_RsaPublicKeyDecode(good, &inOutIdx, NULL, inSz);
  12628. if (ret != BAD_FUNC_ARG) {
  12629. ret = -7702;
  12630. goto done;
  12631. }
  12632. /* Use good data and offset to bad data. */
  12633. inOutIdx = 2;
  12634. inSz = sizeof(good) - inOutIdx;
  12635. ret = wc_RsaPublicKeyDecode(good, &inOutIdx, keyPub, inSz);
  12636. if (ret != ASN_PARSE_E) {
  12637. ret = -7703;
  12638. goto done;
  12639. }
  12640. inOutIdx = 2;
  12641. inSz = sizeof(goodAlgId) - inOutIdx;
  12642. ret = wc_RsaPublicKeyDecode(goodAlgId, &inOutIdx, keyPub, inSz);
  12643. if (ret != ASN_PARSE_E) {
  12644. ret = -7704;
  12645. goto done;
  12646. }
  12647. inOutIdx = 2;
  12648. inSz = sizeof(goodAlgId);
  12649. ret = wc_RsaPublicKeyDecode(goodAlgId, &inOutIdx, keyPub, inSz);
  12650. #ifndef WOLFSSL_NO_DECODE_EXTRA
  12651. if (ret != ASN_PARSE_E)
  12652. #else
  12653. if (ret != ASN_RSA_KEY_E)
  12654. #endif
  12655. {
  12656. ret = -7705;
  12657. goto done;
  12658. }
  12659. /* Try different bad data. */
  12660. inSz = sizeof(badAlgIdNull);
  12661. inOutIdx = 0;
  12662. ret = wc_RsaPublicKeyDecode(badAlgIdNull, &inOutIdx, keyPub, inSz);
  12663. if (ret != ASN_EXPECT_0_E) {
  12664. ret = -7706;
  12665. goto done;
  12666. }
  12667. inSz = sizeof(badNotBitString);
  12668. inOutIdx = 0;
  12669. ret = wc_RsaPublicKeyDecode(badNotBitString, &inOutIdx, keyPub, inSz);
  12670. if (ret != ASN_BITSTR_E) {
  12671. ret = -7707;
  12672. goto done;
  12673. }
  12674. inSz = sizeof(badBitStringLen);
  12675. inOutIdx = 0;
  12676. ret = wc_RsaPublicKeyDecode(badBitStringLen, &inOutIdx, keyPub, inSz);
  12677. if (ret != ASN_PARSE_E) {
  12678. ret = -7708;
  12679. goto done;
  12680. }
  12681. inSz = sizeof(badNoSeq);
  12682. inOutIdx = 0;
  12683. ret = wc_RsaPublicKeyDecode(badNoSeq, &inOutIdx, keyPub, inSz);
  12684. if (ret != ASN_PARSE_E) {
  12685. ret = -7709;
  12686. goto done;
  12687. }
  12688. inSz = sizeof(badNoObj);
  12689. inOutIdx = 0;
  12690. ret = wc_RsaPublicKeyDecode(badNoObj, &inOutIdx, keyPub, inSz);
  12691. if (ret != ASN_PARSE_E && ret != ASN_OBJECT_ID_E) {
  12692. ret = -7710;
  12693. goto done;
  12694. }
  12695. inSz = sizeof(badIntN);
  12696. inOutIdx = 0;
  12697. ret = wc_RsaPublicKeyDecode(badIntN, &inOutIdx, keyPub, inSz);
  12698. if (ret != ASN_RSA_KEY_E && ret != ASN_PARSE_E) {
  12699. ret = -7711;
  12700. goto done;
  12701. }
  12702. inSz = sizeof(badNotIntE);
  12703. inOutIdx = 0;
  12704. ret = wc_RsaPublicKeyDecode(badNotIntE, &inOutIdx, keyPub, inSz);
  12705. if (ret != ASN_RSA_KEY_E && ret != ASN_PARSE_E) {
  12706. ret = -7712;
  12707. goto done;
  12708. }
  12709. /* TODO: Shouldn't pass as the sequence length is too small. */
  12710. inSz = sizeof(badLength);
  12711. inOutIdx = 0;
  12712. ret = wc_RsaPublicKeyDecode(badLength, &inOutIdx, keyPub, inSz);
  12713. #ifndef WOLFSSL_ASN_TEMPLATE
  12714. if (ret != 0)
  12715. #else
  12716. if (ret != ASN_PARSE_E)
  12717. #endif
  12718. {
  12719. ret = -7713;
  12720. goto done;
  12721. }
  12722. /* TODO: Shouldn't ignore object id's data. */
  12723. wc_FreeRsaKey(keyPub);
  12724. ret = wc_InitRsaKey(keyPub, NULL);
  12725. if (ret != 0)
  12726. return -7714;
  12727. inSz = sizeof(badBitStrNoZero);
  12728. inOutIdx = 0;
  12729. ret = wc_RsaPublicKeyDecode(badBitStrNoZero, &inOutIdx, keyPub, inSz);
  12730. if (ret != ASN_EXPECT_0_E && ret != ASN_PARSE_E) {
  12731. ret = -7715;
  12732. goto done;
  12733. }
  12734. wc_FreeRsaKey(keyPub);
  12735. ret = wc_InitRsaKey(keyPub, NULL);
  12736. if (ret != 0)
  12737. return -7716;
  12738. /* Valid data cases. */
  12739. inSz = sizeof(good);
  12740. inOutIdx = 0;
  12741. ret = wc_RsaPublicKeyDecode(good, &inOutIdx, keyPub, inSz);
  12742. if (ret != 0) {
  12743. ret = -7717;
  12744. goto done;
  12745. }
  12746. if (inOutIdx != inSz) {
  12747. ret = -7718;
  12748. goto done;
  12749. }
  12750. wc_FreeRsaKey(keyPub);
  12751. ret = wc_InitRsaKey(keyPub, NULL);
  12752. if (ret != 0)
  12753. return -7719;
  12754. inSz = sizeof(goodAlgId);
  12755. inOutIdx = 0;
  12756. ret = wc_RsaPublicKeyDecode(goodAlgId, &inOutIdx, keyPub, inSz);
  12757. if (ret != 0) {
  12758. ret = -7720;
  12759. goto done;
  12760. }
  12761. if (inOutIdx != inSz) {
  12762. ret = -7721;
  12763. goto done;
  12764. }
  12765. wc_FreeRsaKey(keyPub);
  12766. ret = wc_InitRsaKey(keyPub, NULL);
  12767. if (ret != 0)
  12768. return -7722;
  12769. inSz = sizeof(goodAlgIdNull);
  12770. inOutIdx = 0;
  12771. ret = wc_RsaPublicKeyDecode(goodAlgIdNull, &inOutIdx, keyPub, inSz);
  12772. if (ret != 0) {
  12773. ret = -7723;
  12774. goto done;
  12775. }
  12776. if (inOutIdx != inSz) {
  12777. ret = -7724;
  12778. goto done;
  12779. }
  12780. done:
  12781. wc_FreeRsaKey(keyPub);
  12782. return ret;
  12783. }
  12784. #endif
  12785. #if defined(WC_RSA_PSS) && !defined(HAVE_FIPS_VERSION) /* not supported with FIPSv1 */
  12786. /* Need to create known good signatures to test with this. */
  12787. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
  12788. !defined(WOLF_CRYPTO_CB_ONLY_RSA)
  12789. static int rsa_pss_test(WC_RNG* rng, RsaKey* key)
  12790. {
  12791. byte digest[WC_MAX_DIGEST_SIZE];
  12792. int ret = 0;
  12793. const char inStr[] = TEST_STRING;
  12794. word32 inLen = (word32)TEST_STRING_SZ;
  12795. word32 outSz;
  12796. word32 plainSz;
  12797. word32 digestSz;
  12798. int i, j;
  12799. #ifdef RSA_PSS_TEST_WRONG_PARAMS
  12800. int k, l;
  12801. #endif
  12802. #ifndef WOLFSSL_SE050
  12803. int len;
  12804. #endif
  12805. byte* plain;
  12806. int mgf[] = {
  12807. #ifndef NO_SHA
  12808. WC_MGF1SHA1,
  12809. #endif
  12810. #ifdef WOLFSSL_SHA224
  12811. WC_MGF1SHA224,
  12812. #endif
  12813. WC_MGF1SHA256,
  12814. #ifdef WOLFSSL_SHA384
  12815. WC_MGF1SHA384,
  12816. #endif
  12817. #ifdef WOLFSSL_SHA512
  12818. WC_MGF1SHA512
  12819. #endif
  12820. };
  12821. enum wc_HashType hash[] = {
  12822. #ifndef NO_SHA
  12823. WC_HASH_TYPE_SHA,
  12824. #endif
  12825. #ifdef WOLFSSL_SHA224
  12826. WC_HASH_TYPE_SHA224,
  12827. #endif
  12828. WC_HASH_TYPE_SHA256,
  12829. #ifdef WOLFSSL_SHA384
  12830. WC_HASH_TYPE_SHA384,
  12831. #endif
  12832. #ifdef WOLFSSL_SHA512
  12833. WC_HASH_TYPE_SHA512,
  12834. #endif
  12835. };
  12836. WC_DECLARE_VAR(in, byte, RSA_TEST_BYTES, HEAP_HINT);
  12837. WC_DECLARE_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
  12838. WC_DECLARE_VAR(sig, byte, RSA_TEST_BYTES, HEAP_HINT);
  12839. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  12840. if (in == NULL || out == NULL || sig == NULL)
  12841. ERROR_OUT(MEMORY_E, exit_rsa_pss);
  12842. #endif
  12843. XMEMCPY(in, inStr, inLen);
  12844. /* Test all combinations of hash and MGF. */
  12845. for (j = 0; j < (int)(sizeof(hash)/sizeof(*hash)); j++) {
  12846. /* Calculate hash of message. */
  12847. ret = wc_Hash(hash[j], in, inLen, digest, sizeof(digest));
  12848. if (ret != 0)
  12849. ERROR_OUT(-7730, exit_rsa_pss);
  12850. digestSz = wc_HashGetDigestSize(hash[j]);
  12851. #ifdef WOLFSSL_SE050
  12852. /* SE050 only supports MGF matched to same hash type */
  12853. i = j;
  12854. #else
  12855. for (i = 0; i < (int)(sizeof(mgf)/sizeof(*mgf)); i++) {
  12856. #endif
  12857. outSz = RSA_TEST_BYTES;
  12858. do {
  12859. #if defined(WOLFSSL_ASYNC_CRYPT)
  12860. ret = wc_AsyncWait(ret, &key->asyncDev,
  12861. WC_ASYNC_FLAG_CALL_AGAIN);
  12862. #endif
  12863. if (ret >= 0) {
  12864. ret = wc_RsaPSS_Sign_ex(digest, digestSz, out, outSz,
  12865. hash[j], mgf[i], -1, key, rng);
  12866. }
  12867. } while (ret == WC_PENDING_E);
  12868. if (ret <= 0)
  12869. ERROR_OUT(-7731, exit_rsa_pss);
  12870. outSz = ret;
  12871. XMEMCPY(sig, out, outSz);
  12872. plain = NULL;
  12873. TEST_SLEEP();
  12874. do {
  12875. #if defined(WOLFSSL_ASYNC_CRYPT)
  12876. ret = wc_AsyncWait(ret, &key->asyncDev,
  12877. WC_ASYNC_FLAG_CALL_AGAIN);
  12878. #endif
  12879. if (ret >= 0) {
  12880. ret = wc_RsaPSS_VerifyInline_ex(sig, outSz, &plain, hash[j],
  12881. mgf[i], -1, key);
  12882. }
  12883. } while (ret == WC_PENDING_E);
  12884. if (ret <= 0)
  12885. ERROR_OUT(-7732, exit_rsa_pss);
  12886. plainSz = ret;
  12887. TEST_SLEEP();
  12888. #if defined(HAVE_SELFTEST) && \
  12889. (!defined(HAVE_SELFTEST_VERSION) || (HAVE_SELFTEST_VERSION < 2))
  12890. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  12891. hash[j], -1);
  12892. #elif defined(HAVE_SELFTEST) && (HAVE_SELFTEST_VERSION == 2)
  12893. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  12894. hash[j], -1, 0);
  12895. #else
  12896. ret = wc_RsaPSS_CheckPadding_ex2(digest, digestSz, plain, plainSz,
  12897. hash[j], -1, wc_RsaEncryptSize(key)*8, HEAP_HINT);
  12898. #endif
  12899. if (ret != 0)
  12900. ERROR_OUT(-7733, exit_rsa_pss);
  12901. #ifdef RSA_PSS_TEST_WRONG_PARAMS
  12902. for (k = 0; k < (int)(sizeof(mgf)/sizeof(*mgf)); k++) {
  12903. for (l = 0; l < (int)(sizeof(hash)/sizeof(*hash)); l++) {
  12904. if (i == k && j == l)
  12905. continue;
  12906. XMEMCPY(sig, out, outSz);
  12907. do {
  12908. #if defined(WOLFSSL_ASYNC_CRYPT)
  12909. ret = wc_AsyncWait(ret, &key->asyncDev,
  12910. WC_ASYNC_FLAG_CALL_AGAIN);
  12911. #endif
  12912. if (ret >= 0) {
  12913. ret = wc_RsaPSS_VerifyInline_ex(sig, outSz,
  12914. (byte**)&plain, hash[l], mgf[k], -1, key);
  12915. }
  12916. } while (ret == WC_PENDING_E);
  12917. if (ret >= 0)
  12918. ERROR_OUT(-7734, exit_rsa_pss);
  12919. }
  12920. }
  12921. #endif
  12922. #ifndef WOLFSSL_SE050
  12923. } /* end mgf for loop */
  12924. #endif
  12925. }
  12926. /* SE050 generates salts internally only of hash length */
  12927. #ifndef WOLFSSL_SE050
  12928. /* Test that a salt length of zero works. */
  12929. digestSz = wc_HashGetDigestSize(hash[0]);
  12930. outSz = RSA_TEST_BYTES;
  12931. do {
  12932. #if defined(WOLFSSL_ASYNC_CRYPT)
  12933. ret = wc_AsyncWait(ret, &key->asyncDev,
  12934. WC_ASYNC_FLAG_CALL_AGAIN);
  12935. #endif
  12936. if (ret >= 0) {
  12937. ret = wc_RsaPSS_Sign_ex(digest, digestSz, out, outSz, hash[0],
  12938. mgf[0], 0, key, rng);
  12939. }
  12940. } while (ret == WC_PENDING_E);
  12941. if (ret <= 0)
  12942. ERROR_OUT(-7735, exit_rsa_pss);
  12943. outSz = ret;
  12944. TEST_SLEEP();
  12945. do {
  12946. #if defined(WOLFSSL_ASYNC_CRYPT)
  12947. ret = wc_AsyncWait(ret, &key->asyncDev,
  12948. WC_ASYNC_FLAG_CALL_AGAIN);
  12949. #endif
  12950. if (ret >= 0) {
  12951. ret = wc_RsaPSS_Verify_ex(out, outSz, sig, outSz, hash[0], mgf[0],
  12952. 0, key);
  12953. }
  12954. } while (ret == WC_PENDING_E);
  12955. if (ret <= 0)
  12956. ERROR_OUT(-7736, exit_rsa_pss);
  12957. plainSz = ret;
  12958. TEST_SLEEP();
  12959. do {
  12960. #if defined(WOLFSSL_ASYNC_CRYPT)
  12961. ret = wc_AsyncWait(ret, &key->asyncDev,
  12962. WC_ASYNC_FLAG_CALL_AGAIN);
  12963. #endif
  12964. if (ret >= 0) {
  12965. #if defined(HAVE_SELFTEST) && \
  12966. (!defined(HAVE_SELFTEST_VERSION) || (HAVE_SELFTEST_VERSION < 2))
  12967. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, sig, plainSz,
  12968. hash[0], 0);
  12969. #elif defined(HAVE_SELFTEST) && (HAVE_SELFTEST_VERSION == 2)
  12970. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, sig, plainSz,
  12971. hash[0], 0, 0);
  12972. #else
  12973. ret = wc_RsaPSS_CheckPadding_ex2(digest, digestSz, sig, plainSz,
  12974. hash[0], 0, 0, HEAP_HINT);
  12975. #endif
  12976. }
  12977. } while (ret == WC_PENDING_E);
  12978. if (ret != 0)
  12979. ERROR_OUT(-7737, exit_rsa_pss);
  12980. XMEMCPY(sig, out, outSz);
  12981. plain = NULL;
  12982. do {
  12983. #if defined(WOLFSSL_ASYNC_CRYPT)
  12984. ret = wc_AsyncWait(ret, &key->asyncDev,
  12985. WC_ASYNC_FLAG_CALL_AGAIN);
  12986. #endif
  12987. if (ret >= 0) {
  12988. ret = wc_RsaPSS_VerifyInline_ex(sig, outSz, &plain, hash[0], mgf[0],
  12989. 0, key);
  12990. }
  12991. } while (ret == WC_PENDING_E);
  12992. if (ret <= 0)
  12993. ERROR_OUT(-7738, exit_rsa_pss);
  12994. plainSz = ret;
  12995. TEST_SLEEP();
  12996. #if defined(HAVE_SELFTEST) && \
  12997. (!defined(HAVE_SELFTEST_VERSION) || (HAVE_SELFTEST_VERSION < 2))
  12998. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  12999. hash[0], 0);
  13000. #elif defined(HAVE_SELFTEST) && (HAVE_SELFTEST_VERSION == 2)
  13001. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  13002. hash[0], 0, 0);
  13003. #else
  13004. ret = wc_RsaPSS_CheckPadding_ex2(digest, digestSz, plain, plainSz, hash[0],
  13005. 0, 0, HEAP_HINT);
  13006. #endif
  13007. if (ret != 0)
  13008. ERROR_OUT(-7739, exit_rsa_pss);
  13009. /* Test bad salt lengths in various APIs. */
  13010. digestSz = wc_HashGetDigestSize(hash[0]);
  13011. outSz = RSA_TEST_BYTES;
  13012. #ifndef WOLFSSL_PSS_SALT_LEN_DISCOVER
  13013. len = -2;
  13014. #else
  13015. len = -3;
  13016. #endif
  13017. do {
  13018. #if defined(WOLFSSL_ASYNC_CRYPT)
  13019. ret = wc_AsyncWait(ret, &key->asyncDev,
  13020. WC_ASYNC_FLAG_CALL_AGAIN);
  13021. #endif
  13022. if (ret >= 0) {
  13023. ret = wc_RsaPSS_Sign_ex(digest, digestSz, out, outSz, hash[0],
  13024. mgf[0], len, key, rng);
  13025. }
  13026. } while (ret == WC_PENDING_E);
  13027. if (ret != PSS_SALTLEN_E)
  13028. ERROR_OUT(-7740, exit_rsa_pss);
  13029. do {
  13030. #if defined(WOLFSSL_ASYNC_CRYPT)
  13031. ret = wc_AsyncWait(ret, &key->asyncDev,
  13032. WC_ASYNC_FLAG_CALL_AGAIN);
  13033. #endif
  13034. if (ret >= 0) {
  13035. ret = wc_RsaPSS_Sign_ex(digest, digestSz, out, outSz, hash[0],
  13036. mgf[0], digestSz + 1, key, rng);
  13037. }
  13038. } while (ret == WC_PENDING_E);
  13039. if (ret != PSS_SALTLEN_E)
  13040. ERROR_OUT(-7741, exit_rsa_pss);
  13041. TEST_SLEEP();
  13042. do {
  13043. #if defined(WOLFSSL_ASYNC_CRYPT)
  13044. ret = wc_AsyncWait(ret, &key->asyncDev,
  13045. WC_ASYNC_FLAG_CALL_AGAIN);
  13046. #endif
  13047. if (ret >= 0) {
  13048. ret = wc_RsaPSS_VerifyInline_ex(sig, outSz, &plain, hash[0],
  13049. mgf[0], -2, key);
  13050. }
  13051. } while (ret == WC_PENDING_E);
  13052. if (ret != PSS_SALTLEN_E)
  13053. ERROR_OUT(-7742, exit_rsa_pss);
  13054. TEST_SLEEP();
  13055. do {
  13056. #if defined(WOLFSSL_ASYNC_CRYPT)
  13057. ret = wc_AsyncWait(ret, &key->asyncDev,
  13058. WC_ASYNC_FLAG_CALL_AGAIN);
  13059. #endif
  13060. if (ret >= 0) {
  13061. ret = wc_RsaPSS_VerifyInline_ex(sig, outSz, &plain, hash[0], mgf[0],
  13062. digestSz + 1, key);
  13063. }
  13064. } while (ret == WC_PENDING_E);
  13065. if (ret != PSS_SALTLEN_E)
  13066. ERROR_OUT(-7743, exit_rsa_pss);
  13067. TEST_SLEEP();
  13068. #ifndef WOLFSSL_PSS_SALT_LEN_DISCOVER
  13069. len = -2;
  13070. #else
  13071. len = -3;
  13072. #endif
  13073. #if defined(HAVE_SELFTEST) && \
  13074. (!defined(HAVE_SELFTEST_VERSION) || (HAVE_SELFTEST_VERSION < 2))
  13075. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  13076. hash[0], len);
  13077. #elif defined(HAVE_SELFTEST) && (HAVE_SELFTEST_VERSION == 2)
  13078. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  13079. hash[0], len, 0);
  13080. #else
  13081. ret = wc_RsaPSS_CheckPadding_ex2(digest, digestSz, plain, plainSz, hash[0],
  13082. len, 0, HEAP_HINT);
  13083. #endif
  13084. if (ret != PSS_SALTLEN_E)
  13085. ERROR_OUT(-7744, exit_rsa_pss);
  13086. #ifndef WOLFSSL_PSS_LONG_SALT
  13087. len = digestSz + 1;
  13088. #else
  13089. len = plainSz - digestSz - 1;
  13090. #endif
  13091. #if defined(HAVE_SELFTEST) && \
  13092. (!defined(HAVE_SELFTEST_VERSION) || (HAVE_SELFTEST_VERSION < 2))
  13093. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  13094. hash[0], len);
  13095. #elif defined(HAVE_SELFTEST) && (HAVE_SELFTEST_VERSION == 2)
  13096. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  13097. hash[0], len, 0);
  13098. #else
  13099. ret = wc_RsaPSS_CheckPadding_ex2(digest, digestSz, plain, plainSz, hash[0],
  13100. len, 0, HEAP_HINT);
  13101. #endif
  13102. if (ret != PSS_SALTLEN_E)
  13103. ERROR_OUT(-7745, exit_rsa_pss);
  13104. ret = 0;
  13105. #endif /* WOLFSSL_SE050 */
  13106. exit_rsa_pss:
  13107. WC_FREE_VAR(sig, HEAP_HINT);
  13108. WC_FREE_VAR(in, HEAP_HINT);
  13109. WC_FREE_VAR(out, HEAP_HINT);
  13110. return ret;
  13111. }
  13112. #endif /* !WOLFSSL_RSA_VERIFY_ONLY && !WOLFSSL_RSA_PUBLIC_ONLY */
  13113. #endif
  13114. #ifdef WC_RSA_NO_PADDING
  13115. WOLFSSL_TEST_SUBROUTINE int rsa_no_pad_test(void)
  13116. {
  13117. WC_RNG rng;
  13118. byte* tmp = NULL;
  13119. size_t bytes;
  13120. int ret;
  13121. word32 inLen = 0;
  13122. word32 idx = 0;
  13123. word32 outSz = RSA_TEST_BYTES;
  13124. word32 plainSz = RSA_TEST_BYTES;
  13125. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  13126. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096) && \
  13127. !defined(NO_FILESYSTEM)
  13128. XFILE file;
  13129. #endif
  13130. WC_DECLARE_VAR(key, RsaKey, 1, HEAP_HINT);
  13131. WC_DECLARE_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
  13132. WC_DECLARE_VAR(plain, byte, RSA_TEST_BYTES, HEAP_HINT);
  13133. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  13134. if (key == NULL || out == NULL || plain == NULL)
  13135. ERROR_OUT(MEMORY_E, exit_rsa_nopadding);
  13136. #endif
  13137. /* initialize stack structures */
  13138. XMEMSET(&rng, 0, sizeof(rng));
  13139. XMEMSET(key, 0, sizeof(RsaKey));
  13140. #ifdef USE_CERT_BUFFERS_1024
  13141. bytes = (size_t)sizeof_client_key_der_1024;
  13142. if (bytes < (size_t)sizeof_client_cert_der_1024)
  13143. bytes = (size_t)sizeof_client_cert_der_1024;
  13144. #elif defined(USE_CERT_BUFFERS_2048)
  13145. bytes = (size_t)sizeof_client_key_der_2048;
  13146. if (bytes < (size_t)sizeof_client_cert_der_2048)
  13147. bytes = (size_t)sizeof_client_cert_der_2048;
  13148. #else
  13149. bytes = FOURK_BUF;
  13150. #endif
  13151. tmp = (byte*)XMALLOC(bytes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13152. if (tmp == NULL
  13153. #ifdef WOLFSSL_ASYNC_CRYPT
  13154. || out == NULL || plain == NULL
  13155. #endif
  13156. ) {
  13157. ERROR_OUT(-7800, exit_rsa_nopadding);
  13158. }
  13159. #ifdef USE_CERT_BUFFERS_1024
  13160. XMEMCPY(tmp, client_key_der_1024, (size_t)sizeof_client_key_der_1024);
  13161. #elif defined(USE_CERT_BUFFERS_2048)
  13162. XMEMCPY(tmp, client_key_der_2048, (size_t)sizeof_client_key_der_2048);
  13163. #elif defined(USE_CERT_BUFFERS_3072)
  13164. XMEMCPY(tmp, client_key_der_3072, (size_t)sizeof_client_key_der_3072);
  13165. #elif defined(USE_CERT_BUFFERS_4096)
  13166. XMEMCPY(tmp, client_key_der_4096, (size_t)sizeof_client_key_der_4096);
  13167. #elif !defined(NO_FILESYSTEM)
  13168. file = XFOPEN(clientKey, "rb");
  13169. if (!file) {
  13170. err_sys("can't open clientKey, Please run from wolfSSL home dir", -40);
  13171. ERROR_OUT(-7801, exit_rsa_nopadding);
  13172. }
  13173. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  13174. XFCLOSE(file);
  13175. #else
  13176. /* No key to use. */
  13177. ERROR_OUT(-7802, exit_rsa_nopadding);
  13178. #endif /* USE_CERT_BUFFERS */
  13179. ret = wc_InitRsaKey_ex(key, HEAP_HINT, devId);
  13180. if (ret != 0) {
  13181. ERROR_OUT(-7803, exit_rsa_nopadding);
  13182. }
  13183. ret = wc_RsaPrivateKeyDecode(tmp, &idx, key, (word32)bytes);
  13184. if (ret != 0) {
  13185. ERROR_OUT(-7804, exit_rsa_nopadding);
  13186. }
  13187. /* after loading in key use tmp as the test buffer */
  13188. #ifndef HAVE_FIPS
  13189. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  13190. #else
  13191. ret = wc_InitRng(&rng);
  13192. #endif
  13193. if (ret != 0) {
  13194. ERROR_OUT(-7805, exit_rsa_nopadding);
  13195. }
  13196. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  13197. inLen = wc_RsaEncryptSize(key);
  13198. outSz = inLen;
  13199. plainSz = inLen;
  13200. XMEMSET(tmp, 7, inLen);
  13201. do {
  13202. #if defined(WOLFSSL_ASYNC_CRYPT)
  13203. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13204. #endif
  13205. if (ret >= 0) {
  13206. ret = wc_RsaDirect(tmp, inLen, out, &outSz, key,
  13207. RSA_PRIVATE_ENCRYPT, &rng);
  13208. }
  13209. } while (ret == WC_PENDING_E);
  13210. if (ret <= 0) {
  13211. ERROR_OUT(-7806, exit_rsa_nopadding);
  13212. }
  13213. /* encrypted result should not be the same as input */
  13214. if (XMEMCMP(out, tmp, inLen) == 0) {
  13215. ERROR_OUT(-7807, exit_rsa_nopadding);
  13216. }
  13217. TEST_SLEEP();
  13218. /* decrypt with public key and compare result */
  13219. do {
  13220. #if defined(WOLFSSL_ASYNC_CRYPT)
  13221. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13222. #endif
  13223. if (ret >= 0) {
  13224. ret = wc_RsaDirect(out, outSz, plain, &plainSz, key,
  13225. RSA_PUBLIC_DECRYPT, &rng);
  13226. }
  13227. } while (ret == WC_PENDING_E);
  13228. if (ret <= 0) {
  13229. ERROR_OUT(-7808, exit_rsa_nopadding);
  13230. }
  13231. if (XMEMCMP(plain, tmp, inLen) != 0) {
  13232. ERROR_OUT(-7809, exit_rsa_nopadding);
  13233. }
  13234. TEST_SLEEP();
  13235. #endif
  13236. #ifdef WC_RSA_BLINDING
  13237. ret = wc_RsaSetRNG(NULL, &rng);
  13238. if (ret != BAD_FUNC_ARG) {
  13239. ERROR_OUT(-7810, exit_rsa_nopadding);
  13240. }
  13241. ret = wc_RsaSetRNG(key, &rng);
  13242. if (ret < 0) {
  13243. ERROR_OUT(-7811, exit_rsa_nopadding);
  13244. }
  13245. #endif
  13246. /* test encrypt and decrypt using WC_RSA_NO_PAD */
  13247. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  13248. do {
  13249. #if defined(WOLFSSL_ASYNC_CRYPT)
  13250. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13251. #endif
  13252. if (ret >= 0) {
  13253. ret = wc_RsaPublicEncrypt_ex(tmp, inLen, out, (int)outSz, key, &rng,
  13254. WC_RSA_NO_PAD, WC_HASH_TYPE_NONE, WC_MGF1NONE, NULL, 0);
  13255. }
  13256. } while (ret == WC_PENDING_E);
  13257. if (ret < 0) {
  13258. ERROR_OUT(-7812, exit_rsa_nopadding);
  13259. }
  13260. TEST_SLEEP();
  13261. #endif /* WOLFSSL_RSA_VERIFY_ONLY */
  13262. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  13263. do {
  13264. #if defined(WOLFSSL_ASYNC_CRYPT)
  13265. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13266. #endif
  13267. if (ret >= 0) {
  13268. ret = wc_RsaPrivateDecrypt_ex(out, outSz, plain, (int)plainSz, key,
  13269. WC_RSA_NO_PAD, WC_HASH_TYPE_NONE, WC_MGF1NONE, NULL, 0);
  13270. }
  13271. } while (ret == WC_PENDING_E);
  13272. if (ret < 0) {
  13273. ERROR_OUT(-7813, exit_rsa_nopadding);
  13274. }
  13275. if (XMEMCMP(plain, tmp, inLen) != 0) {
  13276. ERROR_OUT(-7814, exit_rsa_nopadding);
  13277. }
  13278. TEST_SLEEP();
  13279. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  13280. /* test some bad arguments */
  13281. ret = wc_RsaDirect(out, outSz, plain, &plainSz, key, -1,
  13282. &rng);
  13283. if (ret != BAD_FUNC_ARG) {
  13284. ERROR_OUT(-7815, exit_rsa_nopadding);
  13285. }
  13286. ret = wc_RsaDirect(out, outSz, plain, &plainSz, NULL, RSA_PUBLIC_DECRYPT,
  13287. &rng);
  13288. if (ret != BAD_FUNC_ARG) {
  13289. ERROR_OUT(-7816, exit_rsa_nopadding);
  13290. }
  13291. ret = wc_RsaDirect(out, outSz, NULL, &plainSz, key, RSA_PUBLIC_DECRYPT,
  13292. &rng);
  13293. if (ret != LENGTH_ONLY_E || plainSz != inLen) {
  13294. ERROR_OUT(-7817, exit_rsa_nopadding);
  13295. }
  13296. ret = wc_RsaDirect(out, outSz - 10, plain, &plainSz, key,
  13297. RSA_PUBLIC_DECRYPT, &rng);
  13298. if (ret != BAD_FUNC_ARG) {
  13299. ERROR_OUT(-7818, exit_rsa_nopadding);
  13300. }
  13301. /* if making it to this point of code without hitting an ERROR_OUT then
  13302. * all tests have passed */
  13303. ret = 0;
  13304. exit_rsa_nopadding:
  13305. wc_FreeRsaKey(key);
  13306. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13307. WC_FREE_VAR(key, HEAP_HINT);
  13308. WC_FREE_VAR(out, HEAP_HINT);
  13309. WC_FREE_VAR(plain, HEAP_HINT);
  13310. wc_FreeRng(&rng);
  13311. return ret;
  13312. }
  13313. #endif /* WC_RSA_NO_PADDING */
  13314. #if defined(WOLFSSL_HAVE_SP_RSA) && defined(USE_FAST_MATH)
  13315. static int rsa_even_mod_test(WC_RNG* rng, RsaKey* key)
  13316. {
  13317. byte* tmp = NULL;
  13318. size_t bytes;
  13319. int ret;
  13320. word32 inLen = 0;
  13321. #ifndef NO_ASN
  13322. word32 idx = 0;
  13323. #endif
  13324. word32 outSz = RSA_TEST_BYTES;
  13325. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  13326. word32 plainSz = RSA_TEST_BYTES;
  13327. #endif
  13328. #if !defined(USE_CERT_BUFFERS_2048) && !defined(USE_CERT_BUFFERS_3072) && \
  13329. !defined(USE_CERT_BUFFERS_4096) && !defined(NO_FILESYSTEM)
  13330. XFILE file;
  13331. #endif
  13332. WC_DECLARE_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
  13333. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  13334. WC_DECLARE_VAR(plain, byte, RSA_TEST_BYTES, HEAP_HINT);
  13335. #endif
  13336. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  13337. if (out == NULL
  13338. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  13339. || plain == NULL
  13340. #endif
  13341. ) {
  13342. ERROR_OUT(MEMORY_E, exit_rsa_even_mod);
  13343. }
  13344. #endif
  13345. #if defined(USE_CERT_BUFFERS_2048)
  13346. bytes = (size_t)sizeof_client_key_der_2048;
  13347. if (bytes < (size_t)sizeof_client_cert_der_2048)
  13348. bytes = (size_t)sizeof_client_cert_der_2048;
  13349. #else
  13350. bytes = FOURK_BUF;
  13351. #endif
  13352. tmp = (byte*)XMALLOC(bytes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13353. if (tmp == NULL
  13354. #ifdef WOLFSSL_ASYNC_CRYPT
  13355. || out == NULL || plain == NULL
  13356. #endif
  13357. ) {
  13358. ERROR_OUT(-7800, exit_rsa_even_mod);
  13359. }
  13360. #if defined(USE_CERT_BUFFERS_2048)
  13361. XMEMCPY(tmp, client_key_der_2048, (size_t)sizeof_client_key_der_2048);
  13362. #elif defined(USE_CERT_BUFFERS_3072)
  13363. XMEMCPY(tmp, client_key_der_3072, (size_t)sizeof_client_key_der_3072);
  13364. #elif defined(USE_CERT_BUFFERS_4096)
  13365. XMEMCPY(tmp, client_key_der_4096, (size_t)sizeof_client_key_der_4096);
  13366. #elif !defined(NO_FILESYSTEM)
  13367. file = XFOPEN(clientKey, "rb");
  13368. if (!file) {
  13369. err_sys("can't open ./certs/client-key.der, "
  13370. "Please run from wolfSSL home dir", -40);
  13371. ERROR_OUT(-7801, exit_rsa_even_mod);
  13372. }
  13373. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  13374. XFCLOSE(file);
  13375. #else
  13376. /* No key to use. */
  13377. ERROR_OUT(-7802, exit_rsa_even_mod);
  13378. #endif /* USE_CERT_BUFFERS */
  13379. #ifndef NO_ASN
  13380. ret = wc_RsaPrivateKeyDecode(tmp, &idx, key, (word32)bytes);
  13381. if (ret != 0) {
  13382. ERROR_OUT(-7804, exit_rsa_even_mod);
  13383. }
  13384. #else
  13385. #ifdef USE_CERT_BUFFERS_2048
  13386. ret = mp_read_unsigned_bin(&key->n, &tmp[12], 256);
  13387. if (ret != 0) {
  13388. ERROR_OUT(-7804, exit_rsa_even_mod);
  13389. }
  13390. ret = mp_set_int(&key->e, WC_RSA_EXPONENT);
  13391. if (ret != 0) {
  13392. ERROR_OUT(-7804, exit_rsa_even_mod);
  13393. }
  13394. #ifndef NO_SIG_WRAPPER
  13395. modLen = 2048;
  13396. #endif
  13397. #else
  13398. #error Not supported yet!
  13399. #endif
  13400. #endif
  13401. key->n.dp[0] &= (mp_digit)-2;
  13402. if (ret != 0) {
  13403. ERROR_OUT(-7804, exit_rsa_even_mod);
  13404. }
  13405. /* after loading in key use tmp as the test buffer */
  13406. #if !(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION == 2) && \
  13407. (defined(WOLFSSL_SP_ARM64_ASM) || defined(WOLFSSL_SP_ARM32_ASM))) && \
  13408. !defined(WOLFSSL_XILINX_CRYPT)
  13409. /* The ARM64_ASM code that was FIPS validated did not return these expected
  13410. * failure codes. These tests cases were added after the assembly was
  13411. * in-lined in the module and validated, these tests will be available in
  13412. * the 140-3 module */
  13413. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_PUBLIC_ONLY)
  13414. inLen = 32;
  13415. outSz = wc_RsaEncryptSize(key);
  13416. XMEMSET(tmp, 7, plainSz);
  13417. ret = wc_RsaSSL_Sign(tmp, inLen, out, outSz, key, rng);
  13418. if (ret != MP_VAL && ret != MP_EXPTMOD_E && ret != MP_INVMOD_E) {
  13419. ERROR_OUT(-7806, exit_rsa_even_mod);
  13420. }
  13421. ret = wc_RsaSSL_Verify(out, outSz, tmp, inLen, key);
  13422. if (ret != MP_VAL && ret != MP_EXPTMOD_E) {
  13423. ERROR_OUT(-7808, exit_rsa_even_mod);
  13424. }
  13425. #endif
  13426. #ifdef WC_RSA_BLINDING
  13427. ret = wc_RsaSetRNG(key, rng);
  13428. if (ret < 0) {
  13429. ERROR_OUT(-7811, exit_rsa_even_mod);
  13430. }
  13431. #endif
  13432. /* test encrypt and decrypt using WC_RSA_NO_PAD */
  13433. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_PUBLIC_ONLY)
  13434. ret = wc_RsaPublicEncrypt(tmp, inLen, out, (int)outSz, key, rng);
  13435. if (ret != MP_VAL && ret != MP_EXPTMOD_E) {
  13436. ERROR_OUT(-7812, exit_rsa_even_mod);
  13437. }
  13438. #endif /* WOLFSSL_RSA_VERIFY_ONLY */
  13439. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  13440. ret = wc_RsaPrivateDecrypt(out, outSz, plain, (int)plainSz, key);
  13441. if (ret != MP_VAL && ret != MP_EXPTMOD_E && ret != MP_INVMOD_E) {
  13442. ERROR_OUT(-7813, exit_rsa_even_mod);
  13443. }
  13444. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  13445. #endif /* !(HAVE_FIPS_VERSION == 2 && WOLFSSL_SP_ARMxx_ASM) */
  13446. /* if making it to this point of code without hitting an ERROR_OUT then
  13447. * all tests have passed */
  13448. ret = 0;
  13449. exit_rsa_even_mod:
  13450. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13451. WC_FREE_VAR(out, HEAP_HINT);
  13452. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  13453. WC_FREE_VAR(plain, HEAP_HINT);
  13454. #endif
  13455. (void)out;
  13456. (void)outSz;
  13457. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  13458. (void)plain;
  13459. (void)plainSz;
  13460. #endif
  13461. (void)inLen;
  13462. (void)rng;
  13463. return ret;
  13464. }
  13465. #endif /* WOLFSSL_HAVE_SP_RSA */
  13466. #ifdef WOLFSSL_CERT_GEN
  13467. static int rsa_certgen_test(RsaKey* key, RsaKey* keypub, WC_RNG* rng, byte* tmp)
  13468. {
  13469. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  13470. RsaKey *caKey = (RsaKey *)XMALLOC(sizeof *caKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13471. #ifdef WOLFSSL_TEST_CERT
  13472. DecodedCert *decode = (DecodedCert *)XMALLOC(sizeof *decode, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13473. #endif
  13474. #else
  13475. RsaKey caKey[1];
  13476. #ifdef WOLFSSL_TEST_CERT
  13477. DecodedCert decode[1];
  13478. #endif
  13479. #endif
  13480. byte* der = NULL;
  13481. int ret;
  13482. Cert* myCert = NULL;
  13483. int certSz;
  13484. size_t bytes3;
  13485. word32 idx3 = 0;
  13486. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  13487. XFILE file3;
  13488. #endif
  13489. #if defined(WOLFSSL_ALT_NAMES) && !defined(NO_ASN_TIME)
  13490. struct tm beforeTime;
  13491. struct tm afterTime;
  13492. #endif
  13493. const byte mySerial[8] = {1,2,3,4,5,6,7,8};
  13494. (void)keypub;
  13495. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  13496. if (caKey == NULL)
  13497. ERROR_OUT(MEMORY_E, exit_rsa);
  13498. #ifdef WOLFSSL_TEST_CERT
  13499. if (decode == NULL)
  13500. ERROR_OUT(MEMORY_E, exit_rsa);
  13501. #endif
  13502. #endif
  13503. XMEMSET(caKey, 0, sizeof *caKey);
  13504. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13505. if (der == NULL) {
  13506. ERROR_OUT(-7820, exit_rsa);
  13507. }
  13508. myCert = (Cert*)XMALLOC(sizeof(Cert), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13509. if (myCert == NULL) {
  13510. ERROR_OUT(-7821, exit_rsa);
  13511. }
  13512. /* self signed */
  13513. if (wc_InitCert_ex(myCert, HEAP_HINT, devId)) {
  13514. ERROR_OUT(-7822, exit_rsa);
  13515. }
  13516. XMEMCPY(&myCert->subject, &certDefaultName, sizeof(CertName));
  13517. XMEMCPY(myCert->serial, mySerial, sizeof(mySerial));
  13518. myCert->serialSz = (int)sizeof(mySerial);
  13519. myCert->isCA = 1;
  13520. #ifndef NO_SHA256
  13521. myCert->sigType = CTC_SHA256wRSA;
  13522. #else
  13523. myCert->sigType = CTC_SHAwRSA;
  13524. #endif
  13525. #ifdef WOLFSSL_CERT_EXT
  13526. /* add Policies */
  13527. XSTRNCPY(myCert->certPolicies[0], "2.16.840.1.101.3.4.1.42",
  13528. CTC_MAX_CERTPOL_SZ);
  13529. XSTRNCPY(myCert->certPolicies[1], "1.2.840.113549.1.9.16.6.5",
  13530. CTC_MAX_CERTPOL_SZ);
  13531. myCert->certPoliciesNb = 2;
  13532. /* add SKID from the Public Key */
  13533. if (wc_SetSubjectKeyIdFromPublicKey(myCert, keypub, NULL) != 0) {
  13534. ERROR_OUT(-7823, exit_rsa);
  13535. }
  13536. /* add AKID from the Public Key */
  13537. if (wc_SetAuthKeyIdFromPublicKey(myCert, keypub, NULL) != 0) {
  13538. ERROR_OUT(-7824, exit_rsa);
  13539. }
  13540. /* add Key Usage */
  13541. if (wc_SetKeyUsage(myCert,"cRLSign,keyCertSign") != 0) {
  13542. ERROR_OUT(-7825, exit_rsa);
  13543. }
  13544. #ifdef WOLFSSL_EKU_OID
  13545. {
  13546. const char unique[] = "2.16.840.1.111111.100.1.10.1";
  13547. if (wc_SetExtKeyUsageOID(myCert, unique, sizeof(unique), 0,
  13548. HEAP_HINT) != 0) {
  13549. ERROR_OUT(-7826, exit_rsa);
  13550. }
  13551. }
  13552. #endif /* WOLFSSL_EKU_OID */
  13553. #endif /* WOLFSSL_CERT_EXT */
  13554. ret = 0;
  13555. do {
  13556. #if defined(WOLFSSL_ASYNC_CRYPT)
  13557. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13558. #endif
  13559. if (ret >= 0) {
  13560. ret = wc_MakeSelfCert(myCert, der, FOURK_BUF, key, rng);
  13561. }
  13562. } while (ret == WC_PENDING_E);
  13563. if (ret < 0) {
  13564. ERROR_OUT(-7827, exit_rsa);
  13565. }
  13566. certSz = ret;
  13567. #ifdef WOLFSSL_TEST_CERT
  13568. InitDecodedCert(decode, der, certSz, HEAP_HINT);
  13569. ret = ParseCert(decode, CERT_TYPE, NO_VERIFY, 0);
  13570. if (ret != 0) {
  13571. FreeDecodedCert(decode);
  13572. ERROR_OUT(-7828, exit_rsa);
  13573. }
  13574. FreeDecodedCert(decode);
  13575. #endif
  13576. ret = SaveDerAndPem(der, certSz, certDerFile, certPemFile,
  13577. CERT_TYPE, -5578);
  13578. if (ret != 0) {
  13579. goto exit_rsa;
  13580. }
  13581. /* Setup Certificate */
  13582. if (wc_InitCert_ex(myCert, HEAP_HINT, devId)) {
  13583. ERROR_OUT(-7829, exit_rsa);
  13584. }
  13585. #ifdef WOLFSSL_ALT_NAMES
  13586. /* Get CA Cert for testing */
  13587. #ifdef USE_CERT_BUFFERS_1024
  13588. XMEMCPY(tmp, ca_cert_der_1024, sizeof_ca_cert_der_1024);
  13589. bytes3 = sizeof_ca_cert_der_1024;
  13590. #elif defined(USE_CERT_BUFFERS_2048)
  13591. XMEMCPY(tmp, ca_cert_der_2048, sizeof_ca_cert_der_2048);
  13592. bytes3 = sizeof_ca_cert_der_2048;
  13593. #else
  13594. file3 = XFOPEN(rsaCaCertDerFile, "rb");
  13595. if (!file3) {
  13596. ERROR_OUT(-7830, exit_rsa);
  13597. }
  13598. bytes3 = XFREAD(tmp, 1, FOURK_BUF, file3);
  13599. XFCLOSE(file3);
  13600. #endif /* USE_CERT_BUFFERS */
  13601. #if !defined(NO_FILESYSTEM) && !defined(USE_CERT_BUFFERS_1024) && \
  13602. !defined(USE_CERT_BUFFERS_2048) && !defined(NO_ASN)
  13603. ret = wc_SetAltNames(myCert, rsaCaCertFile);
  13604. if (ret != 0) {
  13605. ERROR_OUT(-7831, exit_rsa);
  13606. }
  13607. #endif
  13608. /* get alt names from der */
  13609. ret = wc_SetAltNamesBuffer(myCert, tmp, (int)bytes3);
  13610. if (ret != 0) {
  13611. ERROR_OUT(-7832, exit_rsa);
  13612. }
  13613. /* get dates from der */
  13614. ret = wc_SetDatesBuffer(myCert, tmp, (int)bytes3);
  13615. if (ret != 0) {
  13616. ERROR_OUT(-7833, exit_rsa);
  13617. }
  13618. #ifndef NO_ASN_TIME
  13619. ret = wc_GetCertDates(myCert, &beforeTime, &afterTime);
  13620. if (ret < 0) {
  13621. ERROR_OUT(-7834, exit_rsa);
  13622. }
  13623. #endif
  13624. #endif /* WOLFSSL_ALT_NAMES */
  13625. /* Get CA Key */
  13626. #ifdef USE_CERT_BUFFERS_1024
  13627. XMEMCPY(tmp, ca_key_der_1024, sizeof_ca_key_der_1024);
  13628. bytes3 = sizeof_ca_key_der_1024;
  13629. #elif defined(USE_CERT_BUFFERS_2048)
  13630. XMEMCPY(tmp, ca_key_der_2048, sizeof_ca_key_der_2048);
  13631. bytes3 = sizeof_ca_key_der_2048;
  13632. #else
  13633. file3 = XFOPEN(rsaCaKeyFile, "rb");
  13634. if (!file3) {
  13635. ERROR_OUT(-7835, exit_rsa);
  13636. }
  13637. bytes3 = XFREAD(tmp, 1, FOURK_BUF, file3);
  13638. XFCLOSE(file3);
  13639. #endif /* USE_CERT_BUFFERS */
  13640. ret = wc_InitRsaKey(caKey, HEAP_HINT);
  13641. if (ret != 0) {
  13642. ERROR_OUT(-7836, exit_rsa);
  13643. }
  13644. ret = wc_RsaPrivateKeyDecode(tmp, &idx3, caKey, (word32)bytes3);
  13645. if (ret != 0) {
  13646. ERROR_OUT(-7837, exit_rsa);
  13647. }
  13648. #ifndef NO_SHA256
  13649. myCert->sigType = CTC_SHA256wRSA;
  13650. #else
  13651. myCert->sigType = CTC_SHAwRSA;
  13652. #endif
  13653. XMEMCPY(&myCert->subject, &certDefaultName, sizeof(CertName));
  13654. #ifdef WOLFSSL_CERT_EXT
  13655. /* add Policies */
  13656. XSTRNCPY(myCert->certPolicies[0], "2.16.840.1.101.3.4.1.42",
  13657. CTC_MAX_CERTPOL_SZ);
  13658. myCert->certPoliciesNb =1;
  13659. /* add SKID from the Public Key */
  13660. if (wc_SetSubjectKeyIdFromPublicKey(myCert, key, NULL) != 0) {
  13661. ERROR_OUT(-7838, exit_rsa);
  13662. }
  13663. /* add AKID from the CA certificate */
  13664. #if defined(USE_CERT_BUFFERS_2048)
  13665. ret = wc_SetAuthKeyIdFromCert(myCert, ca_cert_der_2048,
  13666. sizeof_ca_cert_der_2048);
  13667. #elif defined(USE_CERT_BUFFERS_1024)
  13668. ret = wc_SetAuthKeyIdFromCert(myCert, ca_cert_der_1024,
  13669. sizeof_ca_cert_der_1024);
  13670. #else
  13671. ret = wc_SetAuthKeyId(myCert, rsaCaCertFile);
  13672. #endif
  13673. if (ret != 0) {
  13674. ERROR_OUT(-7839, exit_rsa);
  13675. }
  13676. /* add Key Usage */
  13677. if (wc_SetKeyUsage(myCert,"keyEncipherment,keyAgreement") != 0) {
  13678. ERROR_OUT(-7840, exit_rsa);
  13679. }
  13680. #endif /* WOLFSSL_CERT_EXT */
  13681. #if defined(USE_CERT_BUFFERS_2048)
  13682. ret = wc_SetIssuerBuffer(myCert, ca_cert_der_2048,
  13683. sizeof_ca_cert_der_2048);
  13684. #elif defined(USE_CERT_BUFFERS_1024)
  13685. ret = wc_SetIssuerBuffer(myCert, ca_cert_der_1024,
  13686. sizeof_ca_cert_der_1024);
  13687. #else
  13688. ret = wc_SetIssuer(myCert, rsaCaCertFile);
  13689. #endif
  13690. if (ret < 0) {
  13691. ERROR_OUT(-7841, exit_rsa);
  13692. }
  13693. certSz = wc_MakeCert(myCert, der, FOURK_BUF, key, NULL, rng);
  13694. if (certSz < 0) {
  13695. ERROR_OUT(-7842, exit_rsa);
  13696. }
  13697. ret = 0;
  13698. do {
  13699. #if defined(WOLFSSL_ASYNC_CRYPT)
  13700. ret = wc_AsyncWait(ret, &caKey->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13701. #endif
  13702. if (ret >= 0) {
  13703. ret = wc_SignCert(myCert->bodySz, myCert->sigType, der, FOURK_BUF,
  13704. caKey, NULL, rng);
  13705. }
  13706. } while (ret == WC_PENDING_E);
  13707. if (ret < 0) {
  13708. ERROR_OUT(-7843, exit_rsa);
  13709. }
  13710. certSz = ret;
  13711. #ifdef WOLFSSL_TEST_CERT
  13712. InitDecodedCert(decode, der, certSz, HEAP_HINT);
  13713. ret = ParseCert(decode, CERT_TYPE, NO_VERIFY, 0);
  13714. if (ret != 0) {
  13715. FreeDecodedCert(decode);
  13716. ERROR_OUT(-7844, exit_rsa);
  13717. }
  13718. FreeDecodedCert(decode);
  13719. #endif
  13720. ret = SaveDerAndPem(der, certSz, otherCertDerFile, otherCertPemFile,
  13721. CERT_TYPE, -5598);
  13722. if (ret != 0) {
  13723. goto exit_rsa;
  13724. }
  13725. exit_rsa:
  13726. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  13727. if (caKey != NULL) {
  13728. wc_FreeRsaKey(caKey);
  13729. XFREE(caKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13730. }
  13731. #ifdef WOLFSSL_TEST_CERT
  13732. if (decode != NULL)
  13733. XFREE(decode, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13734. #endif
  13735. #else
  13736. wc_FreeRsaKey(caKey);
  13737. #endif
  13738. XFREE(myCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13739. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13740. return ret;
  13741. }
  13742. #endif
  13743. #if !defined(NO_RSA) && defined(HAVE_ECC) && defined(WOLFSSL_CERT_GEN)
  13744. /* Make Cert / Sign example for ECC cert and RSA CA */
  13745. static int rsa_ecc_certgen_test(WC_RNG* rng, byte* tmp)
  13746. {
  13747. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  13748. RsaKey *caKey = (RsaKey *)XMALLOC(sizeof *caKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13749. ecc_key *caEccKey = (ecc_key *)XMALLOC(sizeof *caEccKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13750. ecc_key *caEccKeyPub = (ecc_key *)XMALLOC(sizeof *caEccKeyPub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13751. #ifdef WOLFSSL_TEST_CERT
  13752. DecodedCert *decode = (DecodedCert *)XMALLOC(sizeof *decode, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13753. #endif
  13754. #else
  13755. RsaKey caKey[1];
  13756. ecc_key caEccKey[1];
  13757. ecc_key caEccKeyPub[1];
  13758. #ifdef WOLFSSL_TEST_CERT
  13759. DecodedCert decode[1];
  13760. #endif
  13761. #endif
  13762. byte* der = NULL;
  13763. Cert* myCert = NULL;
  13764. int certSz;
  13765. size_t bytes3;
  13766. word32 idx3 = 0;
  13767. #if (!defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)) \
  13768. || !defined(USE_CERT_BUFFERS_256)
  13769. XFILE file3;
  13770. #endif
  13771. int ret;
  13772. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  13773. if ((caKey == NULL) || (caEccKey == NULL) || (caEccKeyPub == NULL)
  13774. #ifdef WOLFSSL_TEST_CERT
  13775. || (decode == NULL)
  13776. #endif
  13777. )
  13778. ERROR_OUT(MEMORY_E, exit_rsa);
  13779. #endif
  13780. XMEMSET(caKey, 0, sizeof *caKey);
  13781. XMEMSET(caEccKey, 0, sizeof *caEccKey);
  13782. XMEMSET(caEccKeyPub, 0, sizeof *caEccKeyPub);
  13783. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13784. if (der == NULL) {
  13785. ERROR_OUT(-7850, exit_rsa);
  13786. }
  13787. myCert = (Cert*)XMALLOC(sizeof(Cert), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13788. if (myCert == NULL) {
  13789. ERROR_OUT(-7851, exit_rsa);
  13790. }
  13791. /* Get CA Key */
  13792. #ifdef USE_CERT_BUFFERS_1024
  13793. XMEMCPY(tmp, ca_key_der_1024, sizeof_ca_key_der_1024);
  13794. bytes3 = sizeof_ca_key_der_1024;
  13795. #elif defined(USE_CERT_BUFFERS_2048)
  13796. XMEMCPY(tmp, ca_key_der_2048, sizeof_ca_key_der_2048);
  13797. bytes3 = sizeof_ca_key_der_2048;
  13798. #else
  13799. file3 = XFOPEN(rsaCaKeyFile, "rb");
  13800. if (!file3) {
  13801. ERROR_OUT(-7852, exit_rsa);
  13802. }
  13803. bytes3 = XFREAD(tmp, 1, FOURK_BUF, file3);
  13804. XFCLOSE(file3);
  13805. #endif /* USE_CERT_BUFFERS */
  13806. ret = wc_InitRsaKey(caKey, HEAP_HINT);
  13807. if (ret != 0) {
  13808. ERROR_OUT(-7853, exit_rsa);
  13809. }
  13810. ret = wc_RsaPrivateKeyDecode(tmp, &idx3, caKey, (word32)bytes3);
  13811. if (ret != 0) {
  13812. ERROR_OUT(-7854, exit_rsa);
  13813. }
  13814. /* Get Cert Key */
  13815. #ifdef USE_CERT_BUFFERS_256
  13816. XMEMCPY(tmp, ecc_key_pub_der_256, sizeof_ecc_key_pub_der_256);
  13817. bytes3 = sizeof_ecc_key_pub_der_256;
  13818. #else
  13819. file3 = XFOPEN(eccKeyPubFileDer, "rb");
  13820. if (!file3) {
  13821. ERROR_OUT(-7855, exit_rsa);
  13822. }
  13823. bytes3 = XFREAD(tmp, 1, FOURK_BUF, file3);
  13824. XFCLOSE(file3);
  13825. #endif
  13826. ret = wc_ecc_init_ex(caEccKeyPub, HEAP_HINT, devId);
  13827. if (ret != 0) {
  13828. ERROR_OUT(-7856, exit_rsa);
  13829. }
  13830. idx3 = 0;
  13831. ret = wc_EccPublicKeyDecode(tmp, &idx3, caEccKeyPub, (word32)bytes3);
  13832. if (ret != 0) {
  13833. ERROR_OUT(-7857, exit_rsa);
  13834. }
  13835. /* Setup Certificate */
  13836. if (wc_InitCert_ex(myCert, HEAP_HINT, devId)) {
  13837. ERROR_OUT(-7858, exit_rsa);
  13838. }
  13839. #ifndef NO_SHA256
  13840. myCert->sigType = CTC_SHA256wRSA;
  13841. #else
  13842. myCert->sigType = CTC_SHAwRSA;
  13843. #endif
  13844. XMEMCPY(&myCert->subject, &certDefaultName, sizeof(CertName));
  13845. #ifdef WOLFSSL_CERT_EXT
  13846. /* add Policies */
  13847. XSTRNCPY(myCert->certPolicies[0], "2.4.589440.587.101.2.1.9632587.1",
  13848. CTC_MAX_CERTPOL_SZ);
  13849. XSTRNCPY(myCert->certPolicies[1], "1.2.13025.489.1.113549",
  13850. CTC_MAX_CERTPOL_SZ);
  13851. myCert->certPoliciesNb = 2;
  13852. /* add SKID from the Public Key */
  13853. if (wc_SetSubjectKeyIdFromPublicKey(myCert, NULL, caEccKeyPub) != 0) {
  13854. ERROR_OUT(-7859, exit_rsa);
  13855. }
  13856. /* add AKID from the CA certificate */
  13857. #if defined(USE_CERT_BUFFERS_2048)
  13858. ret = wc_SetAuthKeyIdFromCert(myCert, ca_cert_der_2048,
  13859. sizeof_ca_cert_der_2048);
  13860. #elif defined(USE_CERT_BUFFERS_1024)
  13861. ret = wc_SetAuthKeyIdFromCert(myCert, ca_cert_der_1024,
  13862. sizeof_ca_cert_der_1024);
  13863. #else
  13864. ret = wc_SetAuthKeyId(myCert, rsaCaCertFile);
  13865. #endif
  13866. if (ret != 0) {
  13867. ERROR_OUT(-7860, exit_rsa);
  13868. }
  13869. /* add Key Usage */
  13870. if (wc_SetKeyUsage(myCert, certKeyUsage) != 0) {
  13871. ERROR_OUT(-7861, exit_rsa);
  13872. }
  13873. #endif /* WOLFSSL_CERT_EXT */
  13874. #if defined(USE_CERT_BUFFERS_2048)
  13875. ret = wc_SetIssuerBuffer(myCert, ca_cert_der_2048,
  13876. sizeof_ca_cert_der_2048);
  13877. #elif defined(USE_CERT_BUFFERS_1024)
  13878. ret = wc_SetIssuerBuffer(myCert, ca_cert_der_1024,
  13879. sizeof_ca_cert_der_1024);
  13880. #else
  13881. ret = wc_SetIssuer(myCert, rsaCaCertFile);
  13882. #endif
  13883. if (ret < 0) {
  13884. ERROR_OUT(-7862, exit_rsa);
  13885. }
  13886. certSz = wc_MakeCert(myCert, der, FOURK_BUF, NULL, caEccKeyPub, rng);
  13887. if (certSz < 0) {
  13888. ERROR_OUT(-7863, exit_rsa);
  13889. }
  13890. ret = 0;
  13891. do {
  13892. #if defined(WOLFSSL_ASYNC_CRYPT)
  13893. ret = wc_AsyncWait(ret, &caEccKey->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13894. #endif
  13895. if (ret >= 0) {
  13896. ret = wc_SignCert(myCert->bodySz, myCert->sigType, der,
  13897. FOURK_BUF, caKey, NULL, rng);
  13898. }
  13899. } while (ret == WC_PENDING_E);
  13900. if (ret < 0) {
  13901. ERROR_OUT(-7864, exit_rsa);
  13902. }
  13903. certSz = ret;
  13904. #ifdef WOLFSSL_TEST_CERT
  13905. InitDecodedCert(decode, der, certSz, 0);
  13906. ret = ParseCert(decode, CERT_TYPE, NO_VERIFY, 0);
  13907. if (ret != 0) {
  13908. FreeDecodedCert(decode);
  13909. ERROR_OUT(-7865, exit_rsa);
  13910. }
  13911. FreeDecodedCert(decode);
  13912. #endif
  13913. ret = SaveDerAndPem(der, certSz, certEccRsaDerFile, certEccRsaPemFile,
  13914. CERT_TYPE, -5616);
  13915. if (ret != 0) {
  13916. goto exit_rsa;
  13917. }
  13918. exit_rsa:
  13919. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  13920. if (caKey != NULL) {
  13921. wc_FreeRsaKey(caKey);
  13922. XFREE(caKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13923. }
  13924. if (caEccKey != NULL) {
  13925. wc_ecc_free(caEccKey);
  13926. XFREE(caEccKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13927. }
  13928. if (caEccKeyPub != NULL) {
  13929. wc_ecc_free(caEccKeyPub);
  13930. XFREE(caEccKeyPub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13931. }
  13932. #ifdef WOLFSSL_TEST_CERT
  13933. if (decode != NULL)
  13934. XFREE(decode, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13935. #endif
  13936. #else
  13937. wc_FreeRsaKey(caKey);
  13938. wc_ecc_free(caEccKey);
  13939. wc_ecc_free(caEccKeyPub);
  13940. #endif
  13941. XFREE(myCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13942. myCert = NULL;
  13943. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13944. der = NULL;
  13945. if (ret >= 0)
  13946. ret = 0;
  13947. return ret;
  13948. }
  13949. #endif /* !NO_RSA && HAVE_ECC && WOLFSSL_CERT_GEN */
  13950. #ifdef WOLFSSL_KEY_GEN
  13951. static int rsa_keygen_test(WC_RNG* rng)
  13952. {
  13953. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  13954. RsaKey *genKey = (RsaKey *)XMALLOC(sizeof *genKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13955. #else
  13956. RsaKey genKey[1];
  13957. #endif
  13958. int ret;
  13959. byte* der = NULL;
  13960. #ifndef WOLFSSL_CRYPTOCELL
  13961. word32 idx = 0;
  13962. #endif
  13963. int derSz = 0;
  13964. #if !defined(WOLFSSL_SP_MATH) && !defined(HAVE_FIPS)
  13965. int keySz = 1024;
  13966. #else
  13967. int keySz = 2048;
  13968. #endif
  13969. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  13970. if (! genKey)
  13971. ERROR_OUT(MEMORY_E, exit_rsa);
  13972. #endif
  13973. XMEMSET(genKey, 0, sizeof *genKey);
  13974. ret = wc_InitRsaKey_ex(genKey, HEAP_HINT, devId);
  13975. if (ret != 0) {
  13976. ERROR_OUT(-7870, exit_rsa);
  13977. }
  13978. #ifdef HAVE_FIPS
  13979. for (;;) {
  13980. #endif
  13981. ret = wc_MakeRsaKey(genKey, keySz, WC_RSA_EXPONENT, rng);
  13982. #if defined(WOLFSSL_ASYNC_CRYPT)
  13983. ret = wc_AsyncWait(ret, &genKey->asyncDev, WC_ASYNC_FLAG_NONE);
  13984. #endif
  13985. #ifdef HAVE_FIPS
  13986. if (ret == PRIME_GEN_E)
  13987. continue;
  13988. break;
  13989. }
  13990. #endif
  13991. if (ret != 0) {
  13992. ERROR_OUT(-7871, exit_rsa);
  13993. }
  13994. TEST_SLEEP();
  13995. #ifdef WOLFSSL_RSA_KEY_CHECK
  13996. ret = wc_CheckRsaKey(genKey);
  13997. if (ret != 0) {
  13998. ERROR_OUT(-7872, exit_rsa);
  13999. }
  14000. #endif
  14001. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14002. if (der == NULL) {
  14003. ERROR_OUT(-7873, exit_rsa);
  14004. }
  14005. derSz = wc_RsaKeyToDer(genKey, der, FOURK_BUF);
  14006. if (derSz < 0) {
  14007. ERROR_OUT(-7874, exit_rsa);
  14008. }
  14009. ret = SaveDerAndPem(der, derSz, keyDerFile, keyPemFile,
  14010. PRIVATEKEY_TYPE, -5555);
  14011. if (ret != 0) {
  14012. goto exit_rsa;
  14013. }
  14014. wc_FreeRsaKey(genKey);
  14015. ret = wc_InitRsaKey(genKey, HEAP_HINT);
  14016. if (ret != 0) {
  14017. ERROR_OUT(-7875, exit_rsa);
  14018. }
  14019. #ifndef WOLFSSL_CRYPTOCELL
  14020. idx = 0;
  14021. /* The private key part of the key gen pairs from cryptocell can't be exported */
  14022. ret = wc_RsaPrivateKeyDecode(der, &idx, genKey, derSz);
  14023. if (ret != 0) {
  14024. ERROR_OUT(-7876, exit_rsa);
  14025. }
  14026. #endif /* WOLFSSL_CRYPTOCELL */
  14027. exit_rsa:
  14028. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  14029. if (genKey) {
  14030. wc_FreeRsaKey(genKey);
  14031. XFREE(genKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14032. }
  14033. #else
  14034. wc_FreeRsaKey(genKey);
  14035. #endif
  14036. if (der != NULL) {
  14037. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14038. der = NULL;
  14039. }
  14040. return ret;
  14041. }
  14042. #endif
  14043. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  14044. #if !defined(WC_NO_RSA_OAEP) && !defined(WC_NO_RNG) && \
  14045. !defined(HAVE_FAST_RSA) && !defined(HAVE_USER_RSA) && \
  14046. (!defined(HAVE_FIPS) || \
  14047. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))) \
  14048. && !defined(WOLF_CRYPTO_CB_ONLY_RSA)
  14049. static int rsa_oaep_padding_test(RsaKey* key, WC_RNG* rng)
  14050. {
  14051. int ret = 0;
  14052. word32 idx = 0;
  14053. const char inStr[] = TEST_STRING;
  14054. const word32 inLen = (word32)TEST_STRING_SZ;
  14055. const word32 outSz = RSA_TEST_BYTES;
  14056. const word32 plainSz = RSA_TEST_BYTES;
  14057. byte* res = NULL;
  14058. WC_DECLARE_VAR(in, byte, TEST_STRING_SZ, HEAP_HINT);
  14059. WC_DECLARE_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
  14060. WC_DECLARE_VAR(plain, byte, RSA_TEST_BYTES, HEAP_HINT);
  14061. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  14062. if (in == NULL || out == NULL || plain == NULL)
  14063. ERROR_OUT(MEMORY_E, exit_rsa);
  14064. #endif
  14065. XMEMCPY(in, inStr, inLen);
  14066. #ifndef NO_SHA
  14067. do {
  14068. #if defined(WOLFSSL_ASYNC_CRYPT)
  14069. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14070. #endif
  14071. if (ret >= 0) {
  14072. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, rng,
  14073. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA, WC_MGF1SHA1, NULL, 0);
  14074. }
  14075. } while (ret == WC_PENDING_E);
  14076. if (ret < 0) {
  14077. ERROR_OUT(-7918, exit_rsa);
  14078. }
  14079. TEST_SLEEP();
  14080. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  14081. idx = (word32)ret;
  14082. do {
  14083. #if defined(WOLFSSL_ASYNC_CRYPT)
  14084. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14085. #endif
  14086. if (ret >= 0) {
  14087. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  14088. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA, WC_MGF1SHA1, NULL, 0);
  14089. }
  14090. } while (ret == WC_PENDING_E);
  14091. if (ret < 0) {
  14092. ERROR_OUT(-7919, exit_rsa);
  14093. }
  14094. if (XMEMCMP(plain, in, inLen)) {
  14095. ERROR_OUT(-7920, exit_rsa);
  14096. }
  14097. TEST_SLEEP();
  14098. #endif /* NO_SHA */
  14099. #endif
  14100. #ifndef NO_SHA256
  14101. XMEMSET(plain, 0, plainSz);
  14102. do {
  14103. #if defined(WOLFSSL_ASYNC_CRYPT)
  14104. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14105. #endif
  14106. if (ret >= 0) {
  14107. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, rng,
  14108. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0);
  14109. }
  14110. } while (ret == WC_PENDING_E);
  14111. if (ret < 0) {
  14112. ERROR_OUT(-7921, exit_rsa);
  14113. }
  14114. TEST_SLEEP();
  14115. idx = (word32)ret;
  14116. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  14117. do {
  14118. #if defined(WOLFSSL_ASYNC_CRYPT)
  14119. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14120. #endif
  14121. if (ret >= 0) {
  14122. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  14123. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0);
  14124. }
  14125. } while (ret == WC_PENDING_E);
  14126. if (ret < 0) {
  14127. ERROR_OUT(-7922, exit_rsa);
  14128. }
  14129. if (XMEMCMP(plain, in, inLen)) {
  14130. ERROR_OUT(-7923, exit_rsa);
  14131. }
  14132. TEST_SLEEP();
  14133. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  14134. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  14135. do {
  14136. #if defined(WOLFSSL_ASYNC_CRYPT)
  14137. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14138. #endif
  14139. if (ret >= 0) {
  14140. ret = wc_RsaPrivateDecryptInline_ex(out, idx, &res, key,
  14141. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0);
  14142. }
  14143. } while (ret == WC_PENDING_E);
  14144. if (ret < 0) {
  14145. ERROR_OUT(-7924, exit_rsa);
  14146. }
  14147. if (ret != (int)inLen) {
  14148. ERROR_OUT(-7925, exit_rsa);
  14149. }
  14150. if (XMEMCMP(res, in, inLen)) {
  14151. ERROR_OUT(-7926, exit_rsa);
  14152. }
  14153. TEST_SLEEP();
  14154. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  14155. /* check fails if not using the same optional label */
  14156. XMEMSET(plain, 0, plainSz);
  14157. do {
  14158. #if defined(WOLFSSL_ASYNC_CRYPT)
  14159. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14160. #endif
  14161. if (ret >= 0) {
  14162. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, rng,
  14163. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0);
  14164. }
  14165. } while (ret == WC_PENDING_E);
  14166. if (ret < 0) {
  14167. ERROR_OUT(-7927, exit_rsa);
  14168. }
  14169. TEST_SLEEP();
  14170. /* TODO: investigate why Cavium Nitrox doesn't detect decrypt error here */
  14171. #if !defined(HAVE_CAVIUM) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
  14172. !defined(WOLFSSL_CRYPTOCELL) && !defined(WOLFSSL_SE050)
  14173. /* label is unused in cryptocell and SE050 so it won't detect decrypt error
  14174. * due to label */
  14175. idx = (word32)ret;
  14176. do {
  14177. #if defined(WOLFSSL_ASYNC_CRYPT)
  14178. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14179. #endif
  14180. if (ret >= 0) {
  14181. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  14182. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, in, inLen);
  14183. }
  14184. } while (ret == WC_PENDING_E);
  14185. if (ret > 0) { /* in this case decrypt should fail */
  14186. ERROR_OUT(-7928, exit_rsa);
  14187. }
  14188. ret = 0;
  14189. TEST_SLEEP();
  14190. #endif /* !HAVE_CAVIUM */
  14191. /* check using optional label with encrypt/decrypt */
  14192. XMEMSET(plain, 0, plainSz);
  14193. do {
  14194. #if defined(WOLFSSL_ASYNC_CRYPT)
  14195. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14196. #endif
  14197. if (ret >= 0) {
  14198. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, rng,
  14199. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, in, inLen);
  14200. }
  14201. } while (ret == WC_PENDING_E);
  14202. if (ret < 0) {
  14203. ERROR_OUT(-7929, exit_rsa);
  14204. }
  14205. TEST_SLEEP();
  14206. idx = (word32)ret;
  14207. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  14208. do {
  14209. #if defined(WOLFSSL_ASYNC_CRYPT)
  14210. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14211. #endif
  14212. if (ret >= 0) {
  14213. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  14214. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, in, inLen);
  14215. }
  14216. } while (ret == WC_PENDING_E);
  14217. if (ret < 0) {
  14218. ERROR_OUT(-7930, exit_rsa);
  14219. }
  14220. if (XMEMCMP(plain, in, inLen)) {
  14221. ERROR_OUT(-7931, exit_rsa);
  14222. }
  14223. TEST_SLEEP();
  14224. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  14225. #ifndef NO_SHA
  14226. /* check fail using mismatch hash algorithms */
  14227. XMEMSET(plain, 0, plainSz);
  14228. do {
  14229. #if defined(WOLFSSL_ASYNC_CRYPT)
  14230. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14231. #endif
  14232. if (ret >= 0) {
  14233. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, rng,
  14234. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA, WC_MGF1SHA1, in, inLen);
  14235. }
  14236. } while (ret == WC_PENDING_E);
  14237. if (ret < 0) {
  14238. ERROR_OUT(-7932, exit_rsa);
  14239. }
  14240. TEST_SLEEP();
  14241. /* TODO: investigate why Cavium Nitrox doesn't detect decrypt error here */
  14242. #if !defined(HAVE_CAVIUM) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
  14243. !defined(WOLFSSL_CRYPTOCELL) && !defined(WOLFSSL_SE050)
  14244. idx = (word32)ret;
  14245. do {
  14246. #if defined(WOLFSSL_ASYNC_CRYPT)
  14247. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14248. #endif
  14249. if (ret >= 0) {
  14250. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  14251. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256,
  14252. in, inLen);
  14253. }
  14254. } while (ret == WC_PENDING_E);
  14255. if (ret > 0) { /* should fail */
  14256. ERROR_OUT(-7933, exit_rsa);
  14257. }
  14258. ret = 0;
  14259. TEST_SLEEP();
  14260. #endif /* !HAVE_CAVIUM */
  14261. #endif /* NO_SHA */
  14262. #endif /* NO_SHA256 */
  14263. #ifdef WOLFSSL_SHA512
  14264. /* Check valid RSA key size is used while using hash length of SHA512
  14265. If key size is less than (hash length * 2) + 2 then is invalid use
  14266. and test, since OAEP padding requires this.
  14267. BAD_FUNC_ARG is returned when this case is not met */
  14268. if (wc_RsaEncryptSize(key) > ((int)WC_SHA512_DIGEST_SIZE * 2) + 2) {
  14269. XMEMSET(plain, 0, plainSz);
  14270. do {
  14271. #if defined(WOLFSSL_ASYNC_CRYPT)
  14272. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14273. #endif
  14274. if (ret >= 0) {
  14275. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, rng,
  14276. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA512, WC_MGF1SHA512, NULL, 0);
  14277. }
  14278. } while (ret == WC_PENDING_E);
  14279. if (ret < 0) {
  14280. ERROR_OUT(-7934, exit_rsa);
  14281. }
  14282. TEST_SLEEP();
  14283. idx = ret;
  14284. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  14285. do {
  14286. #if defined(WOLFSSL_ASYNC_CRYPT)
  14287. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14288. #endif
  14289. if (ret >= 0) {
  14290. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  14291. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA512, WC_MGF1SHA512, NULL, 0);
  14292. }
  14293. } while (ret == WC_PENDING_E);
  14294. if (ret < 0) {
  14295. ERROR_OUT(-7935, exit_rsa);
  14296. }
  14297. if (XMEMCMP(plain, in, inLen)) {
  14298. ERROR_OUT(-7936, exit_rsa);
  14299. }
  14300. TEST_SLEEP();
  14301. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  14302. }
  14303. #endif /* WOLFSSL_SHA512 */
  14304. /* check using pkcsv15 padding with _ex API */
  14305. XMEMSET(plain, 0, plainSz);
  14306. do {
  14307. #if defined(WOLFSSL_ASYNC_CRYPT)
  14308. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14309. #endif
  14310. if (ret >= 0) {
  14311. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, rng,
  14312. WC_RSA_PKCSV15_PAD, WC_HASH_TYPE_NONE, 0, NULL, 0);
  14313. }
  14314. } while (ret == WC_PENDING_E);
  14315. if (ret < 0) {
  14316. ERROR_OUT(-7937, exit_rsa);
  14317. }
  14318. TEST_SLEEP();
  14319. idx = (word32)ret;
  14320. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  14321. do {
  14322. #if defined(WOLFSSL_ASYNC_CRYPT)
  14323. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14324. #endif
  14325. if (ret >= 0) {
  14326. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  14327. WC_RSA_PKCSV15_PAD, WC_HASH_TYPE_NONE, 0, NULL, 0);
  14328. }
  14329. } while (ret == WC_PENDING_E);
  14330. if (ret < 0) {
  14331. ERROR_OUT(-7938, exit_rsa);
  14332. }
  14333. if (XMEMCMP(plain, in, inLen)) {
  14334. ERROR_OUT(-7939, exit_rsa);
  14335. }
  14336. TEST_SLEEP();
  14337. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  14338. exit_rsa:
  14339. WC_FREE_VAR(in, HEAP_HINT);
  14340. WC_FREE_VAR(out, HEAP_HINT);
  14341. WC_FREE_VAR(plain, HEAP_HINT);
  14342. (void)idx;
  14343. (void)inStr;
  14344. (void)res;
  14345. if (ret >= 0)
  14346. ret = 0;
  14347. return ret;
  14348. }
  14349. #endif
  14350. #endif
  14351. WOLFSSL_TEST_SUBROUTINE int rsa_test(void)
  14352. {
  14353. int ret;
  14354. size_t bytes;
  14355. WC_RNG rng;
  14356. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  14357. byte* tmp = NULL;
  14358. byte* der = NULL;
  14359. RsaKey *key = (RsaKey *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14360. #else
  14361. RsaKey key[1];
  14362. byte tmp[FOURK_BUF];
  14363. #endif
  14364. #if defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_CERT_GEN)
  14365. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  14366. RsaKey *keypub = (RsaKey *)XMALLOC(sizeof *keypub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14367. #else
  14368. RsaKey keypub[1];
  14369. #endif
  14370. #endif
  14371. word32 idx = 0;
  14372. const char inStr[] = TEST_STRING;
  14373. const word32 inLen = (word32)TEST_STRING_SZ;
  14374. const word32 outSz = RSA_TEST_BYTES;
  14375. const word32 plainSz = RSA_TEST_BYTES;
  14376. byte* res = NULL;
  14377. #ifndef NO_SIG_WRAPPER
  14378. int modLen;
  14379. #endif
  14380. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  14381. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096) && \
  14382. !defined(NO_FILESYSTEM)
  14383. XFILE file;
  14384. #ifdef WOLFSSL_TEST_CERT
  14385. XFILE file2;
  14386. #endif
  14387. #endif
  14388. #ifdef WOLFSSL_TEST_CERT
  14389. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  14390. DecodedCert *cert = (DecodedCert *)XMALLOC(sizeof *cert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14391. #else
  14392. DecodedCert cert[1];
  14393. #endif
  14394. #ifndef NO_ASN_TIME
  14395. struct tm timearg;
  14396. const byte* date;
  14397. byte dateFormat;
  14398. int dateLength;
  14399. #endif
  14400. #endif
  14401. WC_DECLARE_VAR(in, byte, TEST_STRING_SZ, HEAP_HINT);
  14402. WC_DECLARE_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
  14403. WC_DECLARE_VAR(plain, byte, RSA_TEST_BYTES, HEAP_HINT);
  14404. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  14405. if (in == NULL || out == NULL || plain == NULL)
  14406. ERROR_OUT(MEMORY_E, exit_rsa);
  14407. #endif
  14408. XMEMCPY(in, inStr, inLen);
  14409. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  14410. if (key == NULL)
  14411. ERROR_OUT(MEMORY_E, exit_rsa);
  14412. #if defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_CERT_GEN)
  14413. if (keypub == NULL)
  14414. ERROR_OUT(MEMORY_E, exit_rsa);
  14415. #endif
  14416. #ifdef WOLFSSL_TEST_CERT
  14417. if (cert == NULL)
  14418. ERROR_OUT(MEMORY_E, exit_rsa);
  14419. #endif
  14420. #endif /* WOLFSSL_SMALL_STACK && !WOLFSSL_NO_MALLOC */
  14421. /* initialize stack structures */
  14422. XMEMSET(&rng, 0, sizeof(rng));
  14423. XMEMSET(key, 0, sizeof *key);
  14424. #if defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_CERT_GEN)
  14425. XMEMSET(keypub, 0, sizeof *keypub);
  14426. #endif
  14427. #if !defined(HAVE_USER_RSA) && !defined(NO_ASN)
  14428. ret = rsa_decode_test(key);
  14429. if (ret != 0)
  14430. ERROR_OUT(ret, exit_rsa);
  14431. #endif
  14432. #ifdef USE_CERT_BUFFERS_1024
  14433. bytes = (size_t)sizeof_client_key_der_1024;
  14434. if (bytes < (size_t)sizeof_client_cert_der_1024)
  14435. bytes = (size_t)sizeof_client_cert_der_1024;
  14436. #elif defined(USE_CERT_BUFFERS_2048)
  14437. bytes = (size_t)sizeof_client_key_der_2048;
  14438. if (bytes < (size_t)sizeof_client_cert_der_2048)
  14439. bytes = (size_t)sizeof_client_cert_der_2048;
  14440. #elif defined(USE_CERT_BUFFERS_3072)
  14441. bytes = (size_t)sizeof_client_key_der_3072;
  14442. if (bytes < (size_t)sizeof_client_cert_der_3072)
  14443. bytes = (size_t)sizeof_client_cert_der_3072;
  14444. #elif defined(USE_CERT_BUFFERS_4096)
  14445. bytes = (size_t)sizeof_client_key_der_4096;
  14446. if (bytes < (size_t)sizeof_client_cert_der_4096)
  14447. bytes = (size_t)sizeof_client_cert_der_4096;
  14448. #else
  14449. bytes = FOURK_BUF;
  14450. #endif
  14451. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  14452. tmp = (byte*)XMALLOC(bytes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14453. if (tmp == NULL)
  14454. ERROR_OUT(-7900, exit_rsa);
  14455. #endif
  14456. #ifdef USE_CERT_BUFFERS_1024
  14457. XMEMCPY(tmp, client_key_der_1024, (size_t)sizeof_client_key_der_1024);
  14458. #elif defined(USE_CERT_BUFFERS_2048)
  14459. XMEMCPY(tmp, client_key_der_2048, (size_t)sizeof_client_key_der_2048);
  14460. #elif defined(USE_CERT_BUFFERS_3072)
  14461. XMEMCPY(tmp, client_key_der_3072, (size_t)sizeof_client_key_der_3072);
  14462. #elif defined(USE_CERT_BUFFERS_4096)
  14463. XMEMCPY(tmp, client_key_der_4096, (size_t)sizeof_client_key_der_4096);
  14464. #elif !defined(NO_FILESYSTEM)
  14465. file = XFOPEN(clientKey, "rb");
  14466. if (!file) {
  14467. err_sys("can't open ./certs/client-key.der, "
  14468. "Please run from wolfSSL home dir", -40);
  14469. ERROR_OUT(-7901, exit_rsa);
  14470. }
  14471. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  14472. XFCLOSE(file);
  14473. #else
  14474. /* No key to use. */
  14475. ERROR_OUT(-7902, exit_rsa);
  14476. #endif /* USE_CERT_BUFFERS */
  14477. ret = wc_InitRsaKey_ex(key, HEAP_HINT, devId);
  14478. if (ret != 0) {
  14479. ERROR_OUT(-7903, exit_rsa);
  14480. }
  14481. #ifndef NO_ASN
  14482. ret = wc_RsaPrivateKeyDecode(tmp, &idx, key, (word32)bytes);
  14483. if (ret != 0) {
  14484. ERROR_OUT(-7904, exit_rsa);
  14485. }
  14486. #ifndef NO_SIG_WRAPPER
  14487. modLen = wc_RsaEncryptSize(key);
  14488. #endif
  14489. #else
  14490. #ifdef USE_CERT_BUFFERS_2048
  14491. ret = mp_read_unsigned_bin(&key->n, &tmp[12], 256);
  14492. if (ret != 0) {
  14493. ERROR_OUT(-7905, exit_rsa);
  14494. }
  14495. ret = mp_set_int(&key->e, WC_RSA_EXPONENT);
  14496. if (ret != 0) {
  14497. ERROR_OUT(-7906, exit_rsa);
  14498. }
  14499. #ifndef NO_SIG_WRAPPER
  14500. modLen = 2048;
  14501. #endif
  14502. #else
  14503. #error Not supported yet!
  14504. #endif
  14505. #endif
  14506. #ifndef WC_NO_RNG
  14507. #ifndef HAVE_FIPS
  14508. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  14509. #else
  14510. ret = wc_InitRng(&rng);
  14511. #endif
  14512. if (ret != 0) {
  14513. ERROR_OUT(-7907, exit_rsa);
  14514. }
  14515. #endif
  14516. #ifndef NO_SIG_WRAPPER
  14517. ret = rsa_sig_test(key, sizeof *key, modLen, &rng);
  14518. if (ret != 0)
  14519. goto exit_rsa;
  14520. #endif
  14521. #ifdef WC_RSA_NONBLOCK
  14522. ret = rsa_nb_test(key, in, inLen, out, outSz, plain, plainSz, &rng);
  14523. if (ret != 0)
  14524. goto exit_rsa;
  14525. #endif
  14526. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
  14527. !defined(WC_NO_RNG) && !defined(WOLF_CRYPTO_CB_ONLY_RSA)
  14528. do {
  14529. #if defined(WOLFSSL_ASYNC_CRYPT)
  14530. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14531. #endif
  14532. if (ret >= 0) {
  14533. ret = wc_RsaPublicEncrypt(in, inLen, out, outSz, key, &rng);
  14534. }
  14535. } while (ret == WC_PENDING_E);
  14536. if (ret < 0) {
  14537. ERROR_OUT(-7908, exit_rsa);
  14538. }
  14539. TEST_SLEEP();
  14540. #ifdef WC_RSA_BLINDING
  14541. {
  14542. int tmpret = ret;
  14543. ret = wc_RsaSetRNG(key, &rng);
  14544. if (ret < 0) {
  14545. ERROR_OUT(-7909, exit_rsa);
  14546. }
  14547. ret = tmpret;
  14548. }
  14549. #endif
  14550. idx = (word32)ret; /* save off encrypted length */
  14551. do {
  14552. #if defined(WOLFSSL_ASYNC_CRYPT)
  14553. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14554. #endif
  14555. if (ret >= 0) {
  14556. ret = wc_RsaPrivateDecrypt(out, idx, plain, plainSz, key);
  14557. }
  14558. } while (ret == WC_PENDING_E);
  14559. if (ret < 0) {
  14560. ERROR_OUT(-7910, exit_rsa);
  14561. }
  14562. if (XMEMCMP(plain, in, inLen)) {
  14563. ERROR_OUT(-7911, exit_rsa);
  14564. }
  14565. TEST_SLEEP();
  14566. do {
  14567. #if defined(WOLFSSL_ASYNC_CRYPT)
  14568. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14569. #endif
  14570. if (ret >= 0) {
  14571. ret = wc_RsaPrivateDecryptInline(out, idx, &res, key);
  14572. }
  14573. } while (ret == WC_PENDING_E);
  14574. if (ret < 0) {
  14575. ERROR_OUT(-7912, exit_rsa);
  14576. }
  14577. if (ret != (int)inLen) {
  14578. ERROR_OUT(-7913, exit_rsa);
  14579. }
  14580. if (XMEMCMP(res, in, inLen)) {
  14581. ERROR_OUT(-7914, exit_rsa);
  14582. }
  14583. TEST_SLEEP();
  14584. do {
  14585. #if defined(WOLFSSL_ASYNC_CRYPT)
  14586. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14587. #endif
  14588. if (ret >= 0) {
  14589. ret = wc_RsaSSL_Sign(in, inLen, out, outSz, key, &rng);
  14590. }
  14591. } while (ret == WC_PENDING_E);
  14592. if (ret < 0) {
  14593. ERROR_OUT(-7915, exit_rsa);
  14594. }
  14595. TEST_SLEEP();
  14596. #elif defined(WOLFSSL_PUBLIC_MP)
  14597. {
  14598. static byte signature_2048[] = {
  14599. 0x07, 0x6f, 0xc9, 0x85, 0x73, 0x9e, 0x21, 0x79,
  14600. 0x47, 0xf1, 0xa3, 0xd7, 0xf4, 0x27, 0x29, 0xbe,
  14601. 0x99, 0x5d, 0xac, 0xb2, 0x10, 0x3f, 0x95, 0xda,
  14602. 0x89, 0x23, 0xb8, 0x96, 0x13, 0x57, 0x72, 0x30,
  14603. 0xa1, 0xfe, 0x5a, 0x68, 0x9c, 0x99, 0x9d, 0x1e,
  14604. 0x05, 0xa4, 0x80, 0xb0, 0xbb, 0xd9, 0xd9, 0xa1,
  14605. 0x69, 0x97, 0x74, 0xb3, 0x41, 0x21, 0x3b, 0x47,
  14606. 0xf5, 0x51, 0xb1, 0xfb, 0xc7, 0xaa, 0xcc, 0xdc,
  14607. 0xcd, 0x76, 0xa0, 0x28, 0x4d, 0x27, 0x14, 0xa4,
  14608. 0xb9, 0x41, 0x68, 0x7c, 0xb3, 0x66, 0xe6, 0x6f,
  14609. 0x40, 0x76, 0xe4, 0x12, 0xfd, 0xae, 0x29, 0xb5,
  14610. 0x63, 0x60, 0x87, 0xce, 0x49, 0x6b, 0xf3, 0x05,
  14611. 0x9a, 0x14, 0xb5, 0xcc, 0xcd, 0xf7, 0x30, 0x95,
  14612. 0xd2, 0x72, 0x52, 0x1d, 0x5b, 0x7e, 0xef, 0x4a,
  14613. 0x02, 0x96, 0x21, 0x6c, 0x55, 0xa5, 0x15, 0xb1,
  14614. 0x57, 0x63, 0x2c, 0xa3, 0x8e, 0x9d, 0x3d, 0x45,
  14615. 0xcc, 0xb8, 0xe6, 0xa1, 0xc8, 0x59, 0xcd, 0xf5,
  14616. 0xdc, 0x0a, 0x51, 0xb6, 0x9d, 0xfb, 0xf4, 0x6b,
  14617. 0xfd, 0x32, 0x71, 0x6e, 0xcf, 0xcb, 0xb3, 0xd9,
  14618. 0xe0, 0x4a, 0x77, 0x34, 0xd6, 0x61, 0xf5, 0x7c,
  14619. 0xf9, 0xa9, 0xa4, 0xb0, 0x8e, 0x3b, 0xd6, 0x04,
  14620. 0xe0, 0xde, 0x2b, 0x5b, 0x5a, 0xbf, 0xd9, 0xef,
  14621. 0x8d, 0xa3, 0xf5, 0xb1, 0x67, 0xf3, 0xb9, 0x72,
  14622. 0x0a, 0x37, 0x12, 0x35, 0x6c, 0x8e, 0x10, 0x8b,
  14623. 0x38, 0x06, 0x16, 0x4b, 0x20, 0x20, 0x13, 0x00,
  14624. 0x2e, 0x6d, 0xc2, 0x59, 0x23, 0x67, 0x4a, 0x6d,
  14625. 0xa1, 0x46, 0x8b, 0xee, 0xcf, 0x44, 0xb4, 0x3e,
  14626. 0x56, 0x75, 0x00, 0x68, 0xb5, 0x7d, 0x0f, 0x20,
  14627. 0x79, 0x5d, 0x7f, 0x12, 0x15, 0x32, 0x89, 0x61,
  14628. 0x6b, 0x29, 0xb7, 0x52, 0xf5, 0x25, 0xd8, 0x98,
  14629. 0xe8, 0x6f, 0xf9, 0x22, 0xb4, 0xbb, 0xe5, 0xff,
  14630. 0xd0, 0x92, 0x86, 0x9a, 0x88, 0xa2, 0xaf, 0x6b
  14631. };
  14632. ret = sizeof(signature_2048);
  14633. XMEMCPY(out, signature_2048, ret);
  14634. }
  14635. #endif
  14636. #if !defined(WC_NO_RNG) && !defined(WC_NO_RSA_OAEP) && \
  14637. ((!defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_PUBLIC_ONLY)) || \
  14638. defined(WOLFSSL_PUBLIC_MP)) && !defined(WOLF_CRYPTO_CB_ONLY_RSA)
  14639. idx = (word32)ret;
  14640. XMEMSET(plain, 0, plainSz);
  14641. do {
  14642. #if defined(WOLFSSL_ASYNC_CRYPT)
  14643. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14644. #endif
  14645. if (ret >= 0) {
  14646. #ifndef WOLFSSL_RSA_VERIFY_INLINE
  14647. #if defined(WOLFSSL_CRYPTOCELL)
  14648. /*
  14649. Cryptocell requires the input data and signature byte array to verify.
  14650. first argument must be the input data
  14651. second argument must be the length of input data
  14652. third argument must be the signature byte array or the output from
  14653. wc_RsaSSL_Sign()
  14654. fourth argument must be the length of the signature byte array
  14655. */
  14656. ret = wc_RsaSSL_Verify(in, inLen, out, outSz, key);
  14657. #else
  14658. ret = wc_RsaSSL_Verify(out, idx, plain, plainSz, key);
  14659. #endif /* WOLFSSL_CRYPTOCELL */
  14660. #else
  14661. byte* dec = NULL;
  14662. ret = wc_RsaSSL_VerifyInline(out, idx, &dec, key);
  14663. if (ret > 0) {
  14664. XMEMCPY(plain, dec, ret);
  14665. }
  14666. #endif
  14667. }
  14668. } while (ret == WC_PENDING_E);
  14669. if (ret < 0) {
  14670. ERROR_OUT(-7916, exit_rsa);
  14671. }
  14672. if (XMEMCMP(plain, in, (size_t)ret)) {
  14673. ERROR_OUT(-7917, exit_rsa);
  14674. }
  14675. TEST_SLEEP();
  14676. #endif
  14677. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  14678. #if !defined(WC_NO_RSA_OAEP) && !defined(WC_NO_RNG)
  14679. #if !defined(HAVE_FAST_RSA) && !defined(HAVE_USER_RSA) && \
  14680. (!defined(HAVE_FIPS) || \
  14681. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))) \
  14682. && !defined(WOLF_CRYPTO_CB_ONLY_RSA)
  14683. ret = rsa_oaep_padding_test(key, &rng);
  14684. if (ret != 0)
  14685. return ret;
  14686. #endif /* !HAVE_FAST_RSA && !HAVE_FIPS */
  14687. #endif /* WC_NO_RSA_OAEP && !WC_NO_RNG */
  14688. #endif /* WOLFSSL_RSA_VERIFY_ONLY */
  14689. #if !defined(HAVE_FIPS) && !defined(HAVE_USER_RSA) && !defined(NO_ASN) \
  14690. && !defined(WOLFSSL_RSA_VERIFY_ONLY)
  14691. ret = rsa_export_key_test(key);
  14692. if (ret != 0)
  14693. return ret;
  14694. #endif
  14695. #if !defined(NO_ASN) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
  14696. !defined(WOLFSSL_RSA_VERIFY_ONLY)
  14697. ret = rsa_flatten_test(key);
  14698. if (ret != 0)
  14699. return ret;
  14700. #endif
  14701. #if !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_ASN) && \
  14702. !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  14703. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096)
  14704. (void)clientCert;
  14705. #endif
  14706. #ifdef WOLFSSL_TEST_CERT
  14707. #if defined(WOLFSSL_MDK_ARM)
  14708. #define sizeof(s) XSTRLEN((char *)(s))
  14709. #endif
  14710. #ifdef USE_CERT_BUFFERS_1024
  14711. XMEMCPY(tmp, client_cert_der_1024, (size_t)sizeof_client_cert_der_1024);
  14712. bytes = (size_t)sizeof_client_cert_der_1024;
  14713. #elif defined(USE_CERT_BUFFERS_2048)
  14714. XMEMCPY(tmp, client_cert_der_2048, (size_t)sizeof_client_cert_der_2048);
  14715. bytes = (size_t)sizeof_client_cert_der_2048;
  14716. #elif defined(USE_CERT_BUFFERS_3072)
  14717. XMEMCPY(tmp, client_cert_der_3072, (size_t)sizeof_client_cert_der_3072);
  14718. bytes = (size_t)sizeof_client_cert_der_3072;
  14719. #elif defined(USE_CERT_BUFFERS_4096)
  14720. XMEMCPY(tmp, client_cert_der_4096, (size_t)sizeof_client_cert_der_4096);
  14721. bytes = (size_t)sizeof_client_cert_der_4096;
  14722. #elif !defined(NO_FILESYSTEM)
  14723. file2 = XFOPEN(clientCert, "rb");
  14724. if (!file2) {
  14725. ERROR_OUT(-7940, exit_rsa);
  14726. }
  14727. bytes = XFREAD(tmp, 1, FOURK_BUF, file2);
  14728. XFCLOSE(file2);
  14729. #else
  14730. /* No certificate to use. */
  14731. ERROR_OUT(-7941, exit_rsa);
  14732. #endif
  14733. #ifdef sizeof
  14734. #undef sizeof
  14735. #endif
  14736. InitDecodedCert(cert, tmp, (word32)bytes, NULL);
  14737. ret = ParseCert(cert, CERT_TYPE, NO_VERIFY, NULL);
  14738. if (ret != 0) {
  14739. FreeDecodedCert(cert);
  14740. ERROR_OUT(-7942, exit_rsa);
  14741. }
  14742. #ifndef NO_ASN_TIME
  14743. ret = wc_GetDateInfo(cert->afterDate, cert->afterDateLen, &date,
  14744. &dateFormat, &dateLength);
  14745. if (ret != 0) {
  14746. FreeDecodedCert(cert);
  14747. ERROR_OUT(-7943, exit_rsa);
  14748. }
  14749. ret = wc_GetDateAsCalendarTime(date, dateLength, dateFormat, &timearg);
  14750. if (ret != 0) {
  14751. FreeDecodedCert(cert);
  14752. ERROR_OUT(-7944, exit_rsa);
  14753. }
  14754. #endif
  14755. FreeDecodedCert(cert);
  14756. #endif /* WOLFSSL_TEST_CERT */
  14757. #ifdef WOLFSSL_CERT_EXT
  14758. #ifdef USE_CERT_BUFFERS_1024
  14759. XMEMCPY(tmp, client_keypub_der_1024, sizeof_client_keypub_der_1024);
  14760. bytes = sizeof_client_keypub_der_1024;
  14761. #elif defined(USE_CERT_BUFFERS_2048)
  14762. XMEMCPY(tmp, client_keypub_der_2048, sizeof_client_keypub_der_2048);
  14763. bytes = sizeof_client_keypub_der_2048;
  14764. #elif defined(USE_CERT_BUFFERS_3072)
  14765. XMEMCPY(tmp, client_keypub_der_3072, sizeof_client_keypub_der_3072);
  14766. bytes = sizeof_client_keypub_der_3072;
  14767. #elif defined(USE_CERT_BUFFERS_4096)
  14768. XMEMCPY(tmp, client_keypub_der_4096, sizeof_client_keypub_der_4096);
  14769. bytes = sizeof_client_keypub_der_4096;
  14770. #else
  14771. file = XFOPEN(clientKeyPub, "rb");
  14772. if (!file) {
  14773. err_sys("can't open ./certs/client-keyPub.der, "
  14774. "Please run from wolfSSL home dir", -40);
  14775. ERROR_OUT(-7945, exit_rsa);
  14776. }
  14777. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  14778. XFCLOSE(file);
  14779. #endif /* USE_CERT_BUFFERS */
  14780. ret = wc_InitRsaKey(keypub, HEAP_HINT);
  14781. if (ret != 0) {
  14782. ERROR_OUT(-7946, exit_rsa);
  14783. }
  14784. idx = 0;
  14785. ret = wc_RsaPublicKeyDecode(tmp, &idx, keypub, (word32)bytes);
  14786. if (ret != 0) {
  14787. ERROR_OUT(-7947, exit_rsa);
  14788. }
  14789. #endif /* WOLFSSL_CERT_EXT */
  14790. #ifdef WOLFSSL_KEY_GEN
  14791. ret = rsa_keygen_test(&rng);
  14792. if (ret != 0)
  14793. goto exit_rsa;
  14794. #endif
  14795. #ifdef WOLFSSL_CERT_GEN
  14796. /* Make Cert / Sign example for RSA cert and RSA CA */
  14797. ret = rsa_certgen_test(key, keypub, &rng, tmp);
  14798. if (ret != 0)
  14799. goto exit_rsa;
  14800. #if !defined(NO_RSA) && defined(HAVE_ECC)
  14801. ret = rsa_ecc_certgen_test(&rng, tmp);
  14802. if (ret != 0)
  14803. goto exit_rsa;
  14804. #endif
  14805. #if defined(WOLFSSL_CERT_REQ) && !defined(WOLFSSL_NO_MALLOC)
  14806. {
  14807. Cert *req;
  14808. int derSz;
  14809. #ifndef WOLFSSL_SMALL_STACK
  14810. byte* der = NULL;
  14811. #endif
  14812. req = (Cert *)XMALLOC(sizeof *req, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14813. if (! req)
  14814. ERROR_OUT(MEMORY_E, exit_rsa);
  14815. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,DYNAMIC_TYPE_TMP_BUFFER);
  14816. if (der == NULL) {
  14817. ERROR_OUT(-7964, exit_rsa);
  14818. }
  14819. if (wc_InitCert_ex(req, HEAP_HINT, devId)) {
  14820. ERROR_OUT(-7965, exit_rsa);
  14821. }
  14822. req->version = 0;
  14823. req->isCA = 1;
  14824. XSTRNCPY(req->challengePw, "wolf123", CTC_NAME_SIZE);
  14825. XMEMCPY(&req->subject, &certDefaultName, sizeof(CertName));
  14826. #ifndef NO_SHA256
  14827. req->sigType = CTC_SHA256wRSA;
  14828. #else
  14829. req->sigType = CTC_SHAwRSA;
  14830. #endif
  14831. #ifdef WOLFSSL_CERT_EXT
  14832. /* add SKID from the Public Key */
  14833. if (wc_SetSubjectKeyIdFromPublicKey(req, keypub, NULL) != 0) {
  14834. ERROR_OUT(-7966, exit_rsa);
  14835. }
  14836. /* add Key Usage */
  14837. if (wc_SetKeyUsage(req, certKeyUsage2) != 0) {
  14838. ERROR_OUT(-7967, exit_rsa);
  14839. }
  14840. /* add Extended Key Usage */
  14841. if (wc_SetExtKeyUsage(req, "serverAuth,clientAuth,codeSigning,"
  14842. "emailProtection,timeStamping,OCSPSigning") != 0) {
  14843. ERROR_OUT(-7968, exit_rsa);
  14844. }
  14845. #ifdef WOLFSSL_EKU_OID
  14846. {
  14847. WOLFSSL_SMALL_STACK_STATIC const char unique[] = "2.16.840.1.111111.100.1.10.1";
  14848. if (wc_SetExtKeyUsageOID(req, unique, sizeof(unique), 0,
  14849. HEAP_HINT) != 0) {
  14850. ERROR_OUT(-7969, exit_rsa);
  14851. }
  14852. }
  14853. #endif /* WOLFSSL_EKU_OID */
  14854. #endif /* WOLFSSL_CERT_EXT */
  14855. derSz = wc_MakeCertReq(req, der, FOURK_BUF, key, NULL);
  14856. if (derSz < 0) {
  14857. ERROR_OUT(-7970, exit_rsa);
  14858. }
  14859. #ifdef WOLFSSL_CERT_EXT
  14860. /* Try again with "any" flag set, will override all others */
  14861. if (wc_SetExtKeyUsage(req, "any") != 0) {
  14862. ERROR_OUT(-7971, exit_rsa);
  14863. }
  14864. derSz = wc_MakeCertReq(req, der, FOURK_BUF, key, NULL);
  14865. if (derSz < 0) {
  14866. ERROR_OUT(-7972, exit_rsa);
  14867. }
  14868. #endif /* WOLFSSL_CERT_EXT */
  14869. ret = 0;
  14870. do {
  14871. #if defined(WOLFSSL_ASYNC_CRYPT)
  14872. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14873. #endif
  14874. if (ret >= 0) {
  14875. ret = wc_SignCert(req->bodySz, req->sigType, der, FOURK_BUF,
  14876. key, NULL, &rng);
  14877. }
  14878. } while (ret == WC_PENDING_E);
  14879. if (ret < 0) {
  14880. ERROR_OUT(-7973, exit_rsa);
  14881. }
  14882. derSz = ret;
  14883. ret = SaveDerAndPem(der, derSz, certReqDerFile, certReqPemFile,
  14884. CERTREQ_TYPE, -5650);
  14885. if (ret != 0) {
  14886. goto exit_rsa;
  14887. }
  14888. derSz = wc_MakeCertReq_ex(req, der, FOURK_BUF, RSA_TYPE, key);
  14889. if (derSz < 0) {
  14890. ERROR_OUT(-7974, exit_rsa);
  14891. }
  14892. /* Test getting the size of the buffer without providing the buffer.
  14893. * derSz is set to the "largest buffer" we are willing to allocate. */
  14894. derSz = wc_MakeCertReq(req, NULL, 10000, key, NULL);
  14895. if (derSz < 0) {
  14896. ERROR_OUT(-7975, exit_rsa);
  14897. }
  14898. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14899. XFREE(req, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14900. der = NULL;
  14901. }
  14902. #endif /* WOLFSSL_CERT_REQ */
  14903. #endif /* WOLFSSL_CERT_GEN */
  14904. #if defined(WC_RSA_PSS) && !defined(HAVE_FIPS_VERSION) /* not supported with FIPSv1 */
  14905. /* Need to create known good signatures to test with this. */
  14906. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
  14907. !defined(WOLF_CRYPTO_CB_ONLY_RSA)
  14908. ret = rsa_pss_test(&rng, key);
  14909. if (ret != 0)
  14910. goto exit_rsa;
  14911. #endif
  14912. #endif
  14913. #if defined(WOLFSSL_HAVE_SP_RSA) && defined(USE_FAST_MATH)
  14914. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  14915. /* New key to be loaded in rsa_even_mod_test(). */
  14916. if (key != NULL)
  14917. #endif
  14918. wc_FreeRsaKey(key);
  14919. /* New key to be loaded in rsa_even_mod_test(). */
  14920. ret = rsa_even_mod_test(&rng, key);
  14921. #endif
  14922. exit_rsa:
  14923. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  14924. if (key != NULL) {
  14925. wc_FreeRsaKey(key);
  14926. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14927. }
  14928. #if defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_CERT_GEN)
  14929. if (keypub != NULL) {
  14930. wc_FreeRsaKey(keypub);
  14931. XFREE(keypub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14932. }
  14933. #endif
  14934. #ifdef WOLFSSL_TEST_CERT
  14935. if (cert != NULL)
  14936. XFREE(cert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14937. #endif
  14938. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14939. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14940. #else
  14941. wc_FreeRsaKey(key);
  14942. #if defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_CERT_GEN)
  14943. wc_FreeRsaKey(keypub);
  14944. #endif
  14945. #endif /* WOLFSSL_SMALL_STACK && !WOLFSSL_NO_MALLOC */
  14946. wc_FreeRng(&rng);
  14947. WC_FREE_VAR(in, HEAP_HINT);
  14948. WC_FREE_VAR(out, HEAP_HINT);
  14949. WC_FREE_VAR(plain, HEAP_HINT);
  14950. (void)res;
  14951. (void)bytes;
  14952. (void)idx;
  14953. (void)in;
  14954. (void)out;
  14955. (void)plain;
  14956. (void)idx;
  14957. (void)inStr;
  14958. (void)inLen;
  14959. (void)outSz;
  14960. (void)plainSz;
  14961. /* ret can be greater then 0 with certgen but all negative values should
  14962. * be returned and treated as an error */
  14963. if (ret >= 0) {
  14964. return 0;
  14965. }
  14966. else {
  14967. return ret;
  14968. }
  14969. }
  14970. #endif /* !NO_RSA */
  14971. #ifndef NO_DH
  14972. static int dh_fips_generate_test(WC_RNG *rng)
  14973. {
  14974. int ret = 0;
  14975. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  14976. DhKey *key = (DhKey *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14977. #else
  14978. DhKey key[1];
  14979. #endif
  14980. WOLFSSL_SMALL_STACK_STATIC const byte p[] = {
  14981. 0xc5, 0x7c, 0xa2, 0x4f, 0x4b, 0xd6, 0x8c, 0x3c,
  14982. 0xda, 0xc7, 0xba, 0xaa, 0xea, 0x2e, 0x5c, 0x1e,
  14983. 0x18, 0xb2, 0x7b, 0x8c, 0x55, 0x65, 0x9f, 0xea,
  14984. 0xe0, 0xa1, 0x36, 0x53, 0x2b, 0x36, 0xe0, 0x4e,
  14985. 0x3e, 0x64, 0xa9, 0xe4, 0xfc, 0x8f, 0x32, 0x62,
  14986. 0x97, 0xe4, 0xbe, 0xf7, 0xc1, 0xde, 0x07, 0x5a,
  14987. 0x89, 0x28, 0xf3, 0xfe, 0x4f, 0xfe, 0x68, 0xbc,
  14988. 0xfb, 0x0a, 0x7c, 0xa4, 0xb3, 0x14, 0x48, 0x89,
  14989. 0x9f, 0xaf, 0xb8, 0x43, 0xe2, 0xa0, 0x62, 0x5c,
  14990. 0xb4, 0x88, 0x3f, 0x06, 0x50, 0x11, 0xfe, 0x65,
  14991. 0x8d, 0x49, 0xd2, 0xf5, 0x4b, 0x74, 0x79, 0xdb,
  14992. 0x06, 0x62, 0x92, 0x89, 0xed, 0xda, 0xcb, 0x87,
  14993. 0x37, 0x16, 0xd2, 0xa1, 0x7a, 0xe8, 0xde, 0x92,
  14994. 0xee, 0x3e, 0x41, 0x4a, 0x91, 0x5e, 0xed, 0xf3,
  14995. 0x6c, 0x6b, 0x7e, 0xfd, 0x15, 0x92, 0x18, 0xfc,
  14996. 0xa7, 0xac, 0x42, 0x85, 0x57, 0xe9, 0xdc, 0xda,
  14997. 0x55, 0xc9, 0x8b, 0x28, 0x9e, 0xc1, 0xc4, 0x46,
  14998. 0x4d, 0x88, 0xed, 0x62, 0x8e, 0xdb, 0x3f, 0xb9,
  14999. 0xd7, 0xc8, 0xe3, 0xcf, 0xb8, 0x34, 0x2c, 0xd2,
  15000. 0x6f, 0x28, 0x06, 0x41, 0xe3, 0x66, 0x8c, 0xfc,
  15001. 0x72, 0xff, 0x26, 0x3b, 0x6b, 0x6c, 0x6f, 0x73,
  15002. 0xde, 0xf2, 0x90, 0x29, 0xe0, 0x61, 0x32, 0xc4,
  15003. 0x12, 0x74, 0x09, 0x52, 0xec, 0xf3, 0x1b, 0xa6,
  15004. 0x45, 0x98, 0xac, 0xf9, 0x1c, 0x65, 0x8e, 0x3a,
  15005. 0x91, 0x84, 0x4b, 0x23, 0x8a, 0xb2, 0x3c, 0xc9,
  15006. 0xfa, 0xea, 0xf1, 0x38, 0xce, 0xd8, 0x05, 0xe0,
  15007. 0xfa, 0x44, 0x68, 0x1f, 0xeb, 0xd9, 0x57, 0xb8,
  15008. 0x4a, 0x97, 0x5b, 0x88, 0xc5, 0xf1, 0xbb, 0xb0,
  15009. 0x49, 0xc3, 0x91, 0x7c, 0xd3, 0x13, 0xb9, 0x47,
  15010. 0xbb, 0x91, 0x8f, 0xe5, 0x26, 0x07, 0xab, 0xa9,
  15011. 0xc5, 0xd0, 0x3d, 0x95, 0x41, 0x26, 0x92, 0x9d,
  15012. 0x13, 0x67, 0xf2, 0x7e, 0x11, 0x88, 0xdc, 0x2d
  15013. };
  15014. WOLFSSL_SMALL_STACK_STATIC const byte g[] = {
  15015. 0x4a, 0x1a, 0xf3, 0xa4, 0x92, 0xe9, 0xee, 0x74,
  15016. 0x6e, 0x57, 0xd5, 0x8c, 0x2c, 0x5b, 0x41, 0x41,
  15017. 0x5e, 0xd4, 0x55, 0x19, 0xdc, 0xd9, 0x32, 0x91,
  15018. 0xf7, 0xfd, 0xc2, 0x57, 0xff, 0x03, 0x14, 0xdb,
  15019. 0xf1, 0xb7, 0x60, 0x0c, 0x43, 0x59, 0x3f, 0xff,
  15020. 0xac, 0xf1, 0x80, 0x9a, 0x15, 0x6f, 0xd8, 0x6e,
  15021. 0xb7, 0x85, 0x18, 0xc8, 0xec, 0x4e, 0x59, 0x4a,
  15022. 0xe2, 0x91, 0x43, 0x4c, 0xeb, 0x95, 0xb6, 0x2e,
  15023. 0x9a, 0xea, 0x53, 0x68, 0x80, 0x64, 0x69, 0x40,
  15024. 0xf9, 0xec, 0xbd, 0x85, 0x89, 0x26, 0x97, 0x67,
  15025. 0xaf, 0xb0, 0xad, 0x00, 0x1b, 0xd4, 0xfd, 0x94,
  15026. 0xd3, 0xe9, 0x92, 0xb1, 0xb4, 0xbc, 0x5a, 0xaa,
  15027. 0x92, 0x80, 0x89, 0x3b, 0x39, 0x05, 0x6c, 0x22,
  15028. 0x26, 0xfe, 0x5a, 0x28, 0x6c, 0x37, 0x50, 0x5a,
  15029. 0x38, 0x99, 0xcf, 0xf3, 0xc1, 0x96, 0x45, 0xdc,
  15030. 0x01, 0xcb, 0x20, 0x87, 0xa5, 0x00, 0x8c, 0xf5,
  15031. 0x4d, 0xc2, 0xef, 0xb8, 0x9b, 0xd1, 0x87, 0xbe,
  15032. 0xed, 0xd5, 0x0a, 0x29, 0x15, 0x34, 0x59, 0x4c,
  15033. 0x3a, 0x05, 0x22, 0x05, 0x44, 0x4f, 0x9f, 0xc8,
  15034. 0x47, 0x12, 0x24, 0x8e, 0xa8, 0x79, 0xe4, 0x67,
  15035. 0xba, 0x4d, 0x5b, 0x75, 0x56, 0x95, 0xeb, 0xe8,
  15036. 0x8a, 0xfa, 0x8e, 0x01, 0x8c, 0x1b, 0x74, 0x63,
  15037. 0xd9, 0x2f, 0xf7, 0xd3, 0x44, 0x8f, 0xa8, 0xf5,
  15038. 0xaf, 0x6c, 0x4f, 0xdb, 0xe7, 0xc9, 0x6c, 0x71,
  15039. 0x22, 0xa3, 0x1d, 0xf1, 0x40, 0xb2, 0xe0, 0x9a,
  15040. 0xb6, 0x72, 0xc9, 0xc0, 0x13, 0x16, 0xa2, 0x4a,
  15041. 0xe1, 0x92, 0xc7, 0x54, 0x23, 0xab, 0x9d, 0xa1,
  15042. 0xa1, 0xe5, 0x0b, 0xed, 0xba, 0xe8, 0x84, 0x37,
  15043. 0xb2, 0xe7, 0xfe, 0x32, 0x8d, 0xfa, 0x1c, 0x53,
  15044. 0x77, 0x97, 0xc7, 0xf3, 0x48, 0xc9, 0xdb, 0x2d,
  15045. 0x75, 0x52, 0x9d, 0x42, 0x51, 0x78, 0x62, 0x68,
  15046. 0x05, 0x45, 0x15, 0xf8, 0xa2, 0x4e, 0xf3, 0x0b
  15047. };
  15048. WOLFSSL_SMALL_STACK_STATIC const byte q[] = {
  15049. 0xe0, 0x35, 0x37, 0xaf, 0xb2, 0x50, 0x91, 0x8e,
  15050. 0xf2, 0x62, 0x2b, 0xd9, 0x9f, 0x6c, 0x11, 0x75,
  15051. 0xec, 0x24, 0x5d, 0x78, 0x59, 0xe7, 0x8d, 0xb5,
  15052. 0x40, 0x52, 0xed, 0x41
  15053. };
  15054. WOLFSSL_SMALL_STACK_STATIC const byte q0[] = {
  15055. 0x00,
  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. byte priv[256];
  15062. byte pub[256];
  15063. word32 privSz = sizeof(priv);
  15064. word32 pubSz = sizeof(pub);
  15065. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15066. if (key == NULL)
  15067. ERROR_OUT(MEMORY_E, exit_gen_test);
  15068. #endif
  15069. /* Parameter Validation testing. */
  15070. ret = wc_DhGenerateKeyPair(NULL, rng, priv, &privSz, pub, &pubSz);
  15071. if (ret != BAD_FUNC_ARG)
  15072. ERROR_OUT(-7980, exit_gen_test);
  15073. ret = wc_DhGenerateKeyPair(key, NULL, priv, &privSz, pub, &pubSz);
  15074. if (ret != BAD_FUNC_ARG)
  15075. ERROR_OUT(-7981, exit_gen_test);
  15076. ret = wc_DhGenerateKeyPair(key, rng, NULL, &privSz, pub, &pubSz);
  15077. if (ret != BAD_FUNC_ARG)
  15078. ERROR_OUT(-7982, exit_gen_test);
  15079. ret = wc_DhGenerateKeyPair(key, rng, priv, NULL, pub, &pubSz);
  15080. if (ret != BAD_FUNC_ARG)
  15081. ERROR_OUT(-7983, exit_gen_test);
  15082. ret = wc_DhGenerateKeyPair(key, rng, priv, &privSz, NULL, &pubSz);
  15083. if (ret != BAD_FUNC_ARG)
  15084. ERROR_OUT(-7984, exit_gen_test);
  15085. ret = wc_DhGenerateKeyPair(key, rng, priv, &privSz, pub, NULL);
  15086. if (ret != BAD_FUNC_ARG)
  15087. ERROR_OUT(-7985, exit_gen_test);
  15088. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  15089. if (ret != 0)
  15090. ERROR_OUT(-7986, exit_gen_test);
  15091. ret = wc_DhSetKey_ex(key, p, sizeof(p), g, sizeof(g), q0, sizeof(q0));
  15092. if (ret != 0) {
  15093. ERROR_OUT(-7987, exit_gen_test);
  15094. }
  15095. wc_FreeDhKey(key);
  15096. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  15097. if (ret != 0)
  15098. ERROR_OUT(-7988, exit_gen_test);
  15099. ret = wc_DhSetKey_ex(key, p, sizeof(p), g, sizeof(g), q, sizeof(q));
  15100. if (ret != 0) {
  15101. ERROR_OUT(-7989, exit_gen_test);
  15102. }
  15103. /* Use API. */
  15104. ret = wc_DhGenerateKeyPair(key, rng, priv, &privSz, pub, &pubSz);
  15105. #if defined(WOLFSSL_ASYNC_CRYPT)
  15106. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  15107. #endif
  15108. if (ret != 0) {
  15109. ERROR_OUT(-7990, exit_gen_test);
  15110. }
  15111. ret = wc_DhCheckPubKey_ex(key, pub, pubSz, q0, sizeof(q0));
  15112. if (ret != 0) {
  15113. ERROR_OUT(-7991, exit_gen_test);
  15114. }
  15115. wc_FreeDhKey(key);
  15116. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  15117. if (ret != 0)
  15118. ERROR_OUT(-7992, exit_gen_test);
  15119. ret = wc_DhSetKey(key, p, sizeof(p), g, sizeof(g));
  15120. if (ret != 0) {
  15121. ERROR_OUT(-7993, exit_gen_test);
  15122. }
  15123. ret = wc_DhCheckPubKey_ex(key, pub, pubSz, q, sizeof(q));
  15124. if (ret != 0) {
  15125. ERROR_OUT(-7994, exit_gen_test);
  15126. }
  15127. #ifndef HAVE_SELFTEST
  15128. ret = wc_DhCheckKeyPair(key, pub, pubSz, priv, privSz);
  15129. if (ret != 0) {
  15130. ERROR_OUT(-7995, exit_gen_test);
  15131. }
  15132. /* Taint the public key so the check fails. */
  15133. pub[0]++;
  15134. ret = wc_DhCheckKeyPair(key, pub, pubSz, priv, privSz);
  15135. if (ret != MP_CMP_E) {
  15136. ERROR_OUT(-7996, exit_gen_test);
  15137. }
  15138. #ifdef WOLFSSL_KEY_GEN
  15139. wc_FreeDhKey(key);
  15140. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  15141. if (ret != 0)
  15142. ERROR_OUT(-7997, exit_gen_test);
  15143. ret = wc_DhGenerateParams(rng, 2048, key);
  15144. if (ret != 0) {
  15145. ERROR_OUT(-7998, exit_gen_test);
  15146. }
  15147. privSz = sizeof(priv);
  15148. pubSz = sizeof(pub);
  15149. ret = wc_DhGenerateKeyPair(key, rng, priv, &privSz, pub, &pubSz);
  15150. #if defined(WOLFSSL_ASYNC_CRYPT)
  15151. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  15152. #endif
  15153. if (ret != 0) {
  15154. ERROR_OUT(-7999, exit_gen_test);
  15155. }
  15156. #endif /* WOLFSSL_KEY_GEN */
  15157. #endif /* HAVE_SELFTEST */
  15158. ret = 0;
  15159. exit_gen_test:
  15160. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15161. if (key) {
  15162. wc_FreeDhKey(key);
  15163. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15164. }
  15165. #else
  15166. wc_FreeDhKey(key);
  15167. #endif
  15168. return ret;
  15169. }
  15170. static int dh_generate_test(WC_RNG *rng)
  15171. {
  15172. int ret = 0;
  15173. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15174. DhKey *smallKey = NULL;
  15175. #else
  15176. DhKey smallKey[1];
  15177. #endif
  15178. byte p[2] = { 1, 7 }; /* 263 in decimal */
  15179. byte g[2] = { 0, 2 };
  15180. #if !defined(WOLFSSL_SP_MATH) && !defined(HAVE_FFDHE)
  15181. #ifdef WOLFSSL_DH_CONST
  15182. /* the table for constant DH lookup will round to the lowest byte size 21 */
  15183. byte priv[21];
  15184. byte pub[21];
  15185. #else
  15186. byte priv[2];
  15187. byte pub[2];
  15188. #endif
  15189. word32 privSz = sizeof(priv);
  15190. word32 pubSz = sizeof(pub);
  15191. #endif
  15192. int smallKey_inited = 0;
  15193. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15194. if ((smallKey = (DhKey *)XMALLOC(sizeof(*smallKey), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER)) == NULL)
  15195. return -8019;
  15196. #endif
  15197. ret = wc_InitDhKey_ex(smallKey, HEAP_HINT, devId);
  15198. if (ret != 0)
  15199. ERROR_OUT(-8010, exit_gen_test);
  15200. smallKey_inited = 1;
  15201. /* Parameter Validation testing. */
  15202. ret = wc_InitDhKey_ex(NULL, HEAP_HINT, devId);
  15203. if (ret != BAD_FUNC_ARG)
  15204. return -8011;
  15205. wc_FreeDhKey(NULL);
  15206. ret = wc_DhSetKey(NULL, p, sizeof(p), g, sizeof(g));
  15207. if (ret != BAD_FUNC_ARG) {
  15208. ERROR_OUT(-8012, exit_gen_test);
  15209. }
  15210. ret = wc_DhSetKey(smallKey, NULL, sizeof(p), g, sizeof(g));
  15211. if (ret != BAD_FUNC_ARG) {
  15212. ERROR_OUT(-8013, exit_gen_test);
  15213. }
  15214. ret = wc_DhSetKey(smallKey, p, 0, g, sizeof(g));
  15215. if (ret != BAD_FUNC_ARG) {
  15216. ERROR_OUT(-8014, exit_gen_test);
  15217. }
  15218. ret = wc_DhSetKey(smallKey, p, sizeof(p), NULL, sizeof(g));
  15219. if (ret != BAD_FUNC_ARG) {
  15220. ERROR_OUT(-8015, exit_gen_test);
  15221. }
  15222. ret = wc_DhSetKey(smallKey, p, sizeof(p), g, 0);
  15223. if (ret != BAD_FUNC_ARG) {
  15224. ERROR_OUT(-8016, exit_gen_test);
  15225. }
  15226. ret = wc_DhSetKey(smallKey, p, sizeof(p), g, sizeof(g));
  15227. if (ret != 0) {
  15228. ERROR_OUT(-8017, exit_gen_test);
  15229. }
  15230. #if !defined(WOLFSSL_SP_MATH) && !defined(HAVE_FFDHE)
  15231. /* Use API. */
  15232. ret = wc_DhGenerateKeyPair(smallKey, rng, priv, &privSz, pub, &pubSz);
  15233. #if defined(WOLFSSL_ASYNC_CRYPT)
  15234. ret = wc_AsyncWait(ret, &smallKey->asyncDev, WC_ASYNC_FLAG_NONE);
  15235. #endif
  15236. if (ret != 0) {
  15237. ret = -8018;
  15238. }
  15239. #else
  15240. (void)rng;
  15241. #if defined(HAVE_FIPS) || !defined(WOLFSSL_NO_DH186)
  15242. ret = 0;
  15243. #endif
  15244. #endif
  15245. #if !defined(HAVE_FIPS) && defined(WOLFSSL_NO_DH186)
  15246. {
  15247. byte priv[260];
  15248. byte pub[260];
  15249. word32 privSz = sizeof(priv);
  15250. word32 pubSz = sizeof(pub);
  15251. /* test odd ball param generation with DH */
  15252. wc_FreeDhKey(smallKey);
  15253. ret = wc_InitDhKey_ex(smallKey, HEAP_HINT, devId);
  15254. if (ret != 0)
  15255. ERROR_OUT(-8019, exit_gen_test);
  15256. ret = wc_DhGenerateParams(rng, 2056, smallKey);
  15257. if (ret != 0) {
  15258. ERROR_OUT(-8020, exit_gen_test);
  15259. }
  15260. privSz = sizeof(priv);
  15261. pubSz = sizeof(pub);
  15262. ret = wc_DhGenerateKeyPair(smallKey, rng, priv, &privSz, pub, &pubSz);
  15263. #if defined(WOLFSSL_ASYNC_CRYPT)
  15264. ret = wc_AsyncWait(ret, &smallKey->asyncDev, WC_ASYNC_FLAG_NONE);
  15265. #endif
  15266. if (ret != 0) {
  15267. ERROR_OUT(-8021, exit_gen_test);
  15268. }
  15269. }
  15270. #endif /* !HAVE_FIPS and WOLFSSL_NO_DH186 */
  15271. exit_gen_test:
  15272. if (smallKey_inited)
  15273. wc_FreeDhKey(smallKey);
  15274. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15275. if (smallKey != NULL)
  15276. XFREE(smallKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15277. #endif
  15278. return ret;
  15279. }
  15280. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  15281. typedef struct dh_pubvalue_test {
  15282. const byte* data;
  15283. word32 len;
  15284. } dh_pubvalue_test;
  15285. static int dh_test_check_pubvalue(void)
  15286. {
  15287. int ret;
  15288. word32 i;
  15289. WOLFSSL_SMALL_STACK_STATIC const byte prime[] = {0x01, 0x00, 0x01};
  15290. WOLFSSL_SMALL_STACK_STATIC const byte pubValZero[] = { 0x00 };
  15291. WOLFSSL_SMALL_STACK_STATIC const byte pubValZeroLong[] = { 0x00, 0x00, 0x00 };
  15292. WOLFSSL_SMALL_STACK_STATIC const byte pubValOne[] = { 0x01 };
  15293. WOLFSSL_SMALL_STACK_STATIC const byte pubValOneLong[] = { 0x00, 0x00, 0x01 };
  15294. WOLFSSL_SMALL_STACK_STATIC const byte pubValPrimeMinusOne[] = { 0x01, 0x00, 0x00 };
  15295. WOLFSSL_SMALL_STACK_STATIC const byte pubValPrimeLong[] = {0x00, 0x01, 0x00, 0x01};
  15296. WOLFSSL_SMALL_STACK_STATIC const byte pubValPrimePlusOne[] = { 0x01, 0x00, 0x02 };
  15297. WOLFSSL_SMALL_STACK_STATIC const byte pubValTooBig0[] = { 0x02, 0x00, 0x01 };
  15298. WOLFSSL_SMALL_STACK_STATIC const byte pubValTooBig1[] = { 0x01, 0x01, 0x01 };
  15299. WOLFSSL_SMALL_STACK_STATIC const byte pubValTooLong[] = { 0x01, 0x00, 0x00, 0x01 };
  15300. const dh_pubvalue_test dh_pubval_fail[] = {
  15301. { prime, sizeof(prime) },
  15302. { pubValZero, sizeof(pubValZero) },
  15303. { pubValZeroLong, sizeof(pubValZeroLong) },
  15304. { pubValOne, sizeof(pubValOne) },
  15305. { pubValOneLong, sizeof(pubValOneLong) },
  15306. { pubValPrimeMinusOne, sizeof(pubValPrimeMinusOne) },
  15307. { pubValPrimeLong, sizeof(pubValPrimeLong) },
  15308. { pubValPrimePlusOne, sizeof(pubValPrimePlusOne) },
  15309. { pubValTooBig0, sizeof(pubValTooBig0) },
  15310. { pubValTooBig1, sizeof(pubValTooBig1) },
  15311. { pubValTooLong, sizeof(pubValTooLong) },
  15312. };
  15313. WOLFSSL_SMALL_STACK_STATIC const byte pubValTwo[] = { 0x02 };
  15314. WOLFSSL_SMALL_STACK_STATIC const byte pubValTwoLong[] = { 0x00, 0x00, 0x02 };
  15315. WOLFSSL_SMALL_STACK_STATIC const byte pubValGood[] = { 0x12, 0x34 };
  15316. WOLFSSL_SMALL_STACK_STATIC const byte pubValGoodLen[] = { 0x00, 0x12, 0x34 };
  15317. WOLFSSL_SMALL_STACK_STATIC const byte pubValGoodLong[] = { 0x00, 0x00, 0x12, 0x34 };
  15318. const dh_pubvalue_test dh_pubval_pass[] = {
  15319. { pubValTwo, sizeof(pubValTwo) },
  15320. { pubValTwoLong, sizeof(pubValTwoLong) },
  15321. { pubValGood, sizeof(pubValGood) },
  15322. { pubValGoodLen, sizeof(pubValGoodLen) },
  15323. { pubValGoodLong, sizeof(pubValGoodLong) },
  15324. };
  15325. for (i = 0; i < sizeof(dh_pubval_fail) / sizeof(*dh_pubval_fail); i++) {
  15326. ret = wc_DhCheckPubValue(prime, sizeof(prime), dh_pubval_fail[i].data,
  15327. dh_pubval_fail[i].len);
  15328. if (ret != MP_VAL)
  15329. return -8020 - (int)i;
  15330. }
  15331. for (i = 0; i < sizeof(dh_pubval_pass) / sizeof(*dh_pubval_pass); i++) {
  15332. ret = wc_DhCheckPubValue(prime, sizeof(prime), dh_pubval_pass[i].data,
  15333. dh_pubval_pass[i].len);
  15334. if (ret != 0)
  15335. return -8030 - (int)i;
  15336. }
  15337. return 0;
  15338. }
  15339. #endif
  15340. #if defined(HAVE_FFDHE)
  15341. #if defined(HAVE_FFDHE_4096)
  15342. #define MAX_DH_PRIV_SZ 39
  15343. #define MAX_DH_KEY_SZ 512
  15344. #elif defined(HAVE_FFDHE_3072)
  15345. #define MAX_DH_PRIV_SZ 34
  15346. #define MAX_DH_KEY_SZ 384
  15347. #else
  15348. #define MAX_DH_PRIV_SZ 29
  15349. #define MAX_DH_KEY_SZ 256
  15350. #endif
  15351. #ifndef WC_NO_RNG
  15352. #if !(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION == 2) && \
  15353. (defined(WOLFSSL_SP_ARM64_ASM) || defined(WOLFSSL_SP_ARM32_ASM)))
  15354. #ifdef HAVE_PUBLIC_FFDHE
  15355. static int dh_ffdhe_test(WC_RNG *rng, const DhParams* params)
  15356. #else
  15357. static int dh_ffdhe_test(WC_RNG *rng, int name)
  15358. #endif
  15359. {
  15360. int ret;
  15361. word32 privSz, pubSz, privSz2, pubSz2;
  15362. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15363. byte *priv = (byte*)XMALLOC(MAX_DH_PRIV_SZ, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15364. byte *pub = (byte*)XMALLOC(MAX_DH_KEY_SZ, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15365. byte *priv2 = (byte*)XMALLOC(MAX_DH_PRIV_SZ, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15366. byte *pub2 = (byte*)XMALLOC(MAX_DH_KEY_SZ, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15367. byte *agree = (byte*)XMALLOC(MAX_DH_KEY_SZ, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15368. byte *agree2 = (byte*)XMALLOC(MAX_DH_KEY_SZ, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15369. DhKey *key = (DhKey*)XMALLOC(sizeof(*key), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15370. DhKey *key2 = (DhKey*)XMALLOC(sizeof(*key2), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15371. #else
  15372. byte priv[MAX_DH_PRIV_SZ];
  15373. byte pub[MAX_DH_KEY_SZ];
  15374. byte priv2[MAX_DH_PRIV_SZ];
  15375. byte pub2[MAX_DH_KEY_SZ];
  15376. byte agree[MAX_DH_KEY_SZ];
  15377. byte agree2[MAX_DH_KEY_SZ];
  15378. DhKey key[1];
  15379. DhKey key2[1];
  15380. #endif
  15381. word32 agreeSz = MAX_DH_KEY_SZ;
  15382. word32 agreeSz2 = MAX_DH_KEY_SZ;
  15383. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15384. if ((priv == NULL) ||
  15385. (pub == NULL) ||
  15386. (priv2 == NULL) ||
  15387. (pub2 == NULL) ||
  15388. (agree == NULL) ||
  15389. (agree2 == NULL) ||
  15390. (key == NULL) ||
  15391. (key2 == NULL))
  15392. ERROR_OUT(-8050, done);
  15393. #endif
  15394. pubSz = MAX_DH_KEY_SZ;
  15395. pubSz2 = MAX_DH_KEY_SZ;
  15396. #ifdef HAVE_PUBLIC_FFDHE
  15397. privSz = MAX_DH_PRIV_SZ;
  15398. privSz2 = MAX_DH_PRIV_SZ;
  15399. #else
  15400. privSz = wc_DhGetNamedKeyMinSize(name);
  15401. privSz2 = privSz;
  15402. #endif
  15403. XMEMSET(key, 0, sizeof(*key));
  15404. XMEMSET(key2, 0, sizeof(*key2));
  15405. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  15406. if (ret != 0) {
  15407. ERROR_OUT(-8051, done);
  15408. }
  15409. ret = wc_InitDhKey_ex(key2, HEAP_HINT, devId);
  15410. if (ret != 0) {
  15411. ERROR_OUT(-8052, done);
  15412. }
  15413. #ifdef HAVE_PUBLIC_FFDHE
  15414. ret = wc_DhSetKey(key, params->p, params->p_len, params->g, params->g_len);
  15415. #else
  15416. ret = wc_DhSetNamedKey(key, name);
  15417. #endif
  15418. if (ret != 0) {
  15419. ERROR_OUT(-8053, done);
  15420. }
  15421. #ifdef HAVE_PUBLIC_FFDHE
  15422. ret = wc_DhSetKey(key2, params->p, params->p_len, params->g,
  15423. params->g_len);
  15424. #else
  15425. ret = wc_DhSetNamedKey(key2, name);
  15426. #endif
  15427. if (ret != 0) {
  15428. ERROR_OUT(-8054, done);
  15429. }
  15430. ret = wc_DhGenerateKeyPair(key, rng, priv, &privSz, pub, &pubSz);
  15431. #if defined(WOLFSSL_ASYNC_CRYPT)
  15432. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  15433. #endif
  15434. if (ret != 0) {
  15435. ERROR_OUT(-8055, done);
  15436. }
  15437. ret = wc_DhGenerateKeyPair(key2, rng, priv2, &privSz2, pub2, &pubSz2);
  15438. #if defined(WOLFSSL_ASYNC_CRYPT)
  15439. ret = wc_AsyncWait(ret, &key2->asyncDev, WC_ASYNC_FLAG_NONE);
  15440. #endif
  15441. if (ret != 0) {
  15442. ERROR_OUT(-8056, done);
  15443. }
  15444. ret = wc_DhAgree(key, agree, &agreeSz, priv, privSz, pub2, pubSz2);
  15445. #if defined(WOLFSSL_ASYNC_CRYPT)
  15446. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  15447. #endif
  15448. if (ret != 0) {
  15449. ERROR_OUT(-8057, done);
  15450. }
  15451. ret = wc_DhAgree(key2, agree2, &agreeSz2, priv2, privSz2, pub, pubSz);
  15452. #if defined(WOLFSSL_ASYNC_CRYPT)
  15453. ret = wc_AsyncWait(ret, &key2->asyncDev, WC_ASYNC_FLAG_NONE);
  15454. #endif
  15455. if (ret != 0) {
  15456. ERROR_OUT(-8058, done);
  15457. }
  15458. if (agreeSz != agreeSz2 || XMEMCMP(agree, agree2, agreeSz)) {
  15459. ERROR_OUT(-8059, done);
  15460. }
  15461. #if defined(WOLFSSL_HAVE_SP_DH) || defined(USE_FAST_MATH)
  15462. /* Make p even */
  15463. key->p.dp[0] &= (mp_digit)-2;
  15464. if (ret != 0) {
  15465. ERROR_OUT(-8058, done);
  15466. }
  15467. ret = wc_DhGenerateKeyPair(key, rng, priv, &privSz, pub, &pubSz);
  15468. #if defined(WOLFSSL_ASYNC_CRYPT)
  15469. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  15470. #endif
  15471. if (ret != MP_VAL && ret != MP_EXPTMOD_E) {
  15472. ERROR_OUT(-8058, done);
  15473. }
  15474. ret = wc_DhAgree(key, agree, &agreeSz, priv, privSz, pub2, pubSz2);
  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 && ret != ASYNC_OP_E) {
  15479. ERROR_OUT(-8057, done);
  15480. }
  15481. ret = wc_DhCheckKeyPair(key, pub, pubSz, priv, privSz);
  15482. if (ret != MP_VAL && ret != MP_EXPTMOD_E && ret != MP_CMP_E &&
  15483. ret != ASYNC_OP_E) {
  15484. ERROR_OUT(-8057, done);
  15485. }
  15486. /* Getting here means success - set ret to 0. */
  15487. ret = 0;
  15488. #endif
  15489. done:
  15490. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC) && \
  15491. !defined(WC_NO_RNG)
  15492. if (priv)
  15493. XFREE(priv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15494. if (pub)
  15495. XFREE(pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15496. if (priv2)
  15497. XFREE(priv2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15498. if (pub2)
  15499. XFREE(pub2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15500. if (agree)
  15501. XFREE(agree, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15502. if (agree2)
  15503. XFREE(agree2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15504. if (key) {
  15505. wc_FreeDhKey(key);
  15506. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15507. }
  15508. if (key2) {
  15509. wc_FreeDhKey(key2);
  15510. XFREE(key2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15511. }
  15512. #else
  15513. wc_FreeDhKey(key);
  15514. wc_FreeDhKey(key2);
  15515. #endif
  15516. return ret;
  15517. }
  15518. #endif /* !(HAVE_FIPS_VERSION == 2 && WOLFSSL_SP_ARMxx_ASM) */
  15519. #endif /* !WC_NO_RNG */
  15520. #endif /* HAVE_FFDHE */
  15521. WOLFSSL_TEST_SUBROUTINE int dh_test(void)
  15522. {
  15523. int ret;
  15524. word32 bytes;
  15525. word32 idx = 0, privSz, pubSz, privSz2, pubSz2;
  15526. #ifndef WC_NO_RNG
  15527. WC_RNG rng;
  15528. int rngInit = 0;
  15529. #endif
  15530. int keyInit = 0, key2Init = 0;
  15531. #define DH_TEST_TMP_SIZE 1024
  15532. #if !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096)
  15533. #define DH_TEST_BUF_SIZE 256
  15534. #else
  15535. #define DH_TEST_BUF_SIZE 512
  15536. #endif
  15537. #ifndef WC_NO_RNG
  15538. word32 agreeSz = DH_TEST_BUF_SIZE;
  15539. word32 agreeSz2 = DH_TEST_BUF_SIZE;
  15540. #endif
  15541. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15542. DhKey *key = (DhKey *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15543. DhKey *key2 = (DhKey *)XMALLOC(sizeof *key2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15544. byte *tmp = (byte *)XMALLOC(DH_TEST_TMP_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15545. #else
  15546. DhKey key[1];
  15547. DhKey key2[1];
  15548. byte tmp[DH_TEST_TMP_SIZE];
  15549. #endif
  15550. #ifndef WC_NO_RNG
  15551. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15552. byte *priv = (byte *)XMALLOC(DH_TEST_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15553. byte *pub = (byte *)XMALLOC(DH_TEST_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15554. byte *priv2 = (byte *)XMALLOC(DH_TEST_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15555. byte *pub2 = (byte *)XMALLOC(DH_TEST_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15556. byte *agree = (byte *)XMALLOC(DH_TEST_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15557. byte *agree2 = (byte *)XMALLOC(DH_TEST_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15558. if (priv == NULL || pub == NULL || priv2 == NULL || pub2 == NULL ||
  15559. agree == NULL || agree2 == NULL) {
  15560. ERROR_OUT(-8100, done);
  15561. }
  15562. #else
  15563. byte priv[DH_TEST_BUF_SIZE];
  15564. byte pub[DH_TEST_BUF_SIZE];
  15565. byte priv2[DH_TEST_BUF_SIZE];
  15566. byte pub2[DH_TEST_BUF_SIZE];
  15567. byte agree[DH_TEST_BUF_SIZE];
  15568. byte agree2[DH_TEST_BUF_SIZE];
  15569. #endif
  15570. #endif /* !WC_NO_RNG */
  15571. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15572. if (key == NULL || key2 == NULL || tmp == NULL) {
  15573. ERROR_OUT(-8100, done);
  15574. }
  15575. #endif
  15576. #ifdef USE_CERT_BUFFERS_1024
  15577. XMEMCPY(tmp, dh_key_der_1024, (size_t)sizeof_dh_key_der_1024);
  15578. bytes = (size_t)sizeof_dh_key_der_1024;
  15579. #elif defined(USE_CERT_BUFFERS_2048)
  15580. XMEMCPY(tmp, dh_key_der_2048, (size_t)sizeof_dh_key_der_2048);
  15581. bytes = (size_t)sizeof_dh_key_der_2048;
  15582. #elif defined(USE_CERT_BUFFERS_3072)
  15583. XMEMCPY(tmp, dh_key_der_3072, (size_t)sizeof_dh_key_der_3072);
  15584. bytes = (size_t)sizeof_dh_key_der_3072;
  15585. #elif defined(USE_CERT_BUFFERS_4096)
  15586. XMEMCPY(tmp, dh_key_der_4096, (size_t)sizeof_dh_key_der_4096);
  15587. bytes = (size_t)sizeof_dh_key_der_4096;
  15588. #elif defined(NO_ASN)
  15589. /* don't use file, no DER parsing */
  15590. #elif !defined(NO_FILESYSTEM)
  15591. {
  15592. XFILE file = XFOPEN(dhParamsFile, "rb");
  15593. if (! file)
  15594. ERROR_OUT(-8101, done);
  15595. bytes = (word32) XFREAD(tmp, 1, DH_TEST_TMP_SIZE, file);
  15596. XFCLOSE(file);
  15597. }
  15598. #else
  15599. /* No DH key to use. */
  15600. ERROR_OUT(-8102, done);
  15601. #endif /* USE_CERT_BUFFERS */
  15602. (void)idx;
  15603. (void)tmp;
  15604. (void)bytes;
  15605. pubSz = DH_TEST_BUF_SIZE;
  15606. pubSz2 = DH_TEST_BUF_SIZE;
  15607. privSz = DH_TEST_BUF_SIZE;
  15608. privSz2 = DH_TEST_BUF_SIZE;
  15609. #ifndef WC_NO_RNG
  15610. XMEMSET(&rng, 0, sizeof(rng));
  15611. #endif
  15612. /* Use API for coverage. */
  15613. ret = wc_InitDhKey(key);
  15614. if (ret != 0) {
  15615. ERROR_OUT(-8103, done);
  15616. }
  15617. wc_FreeDhKey(key);
  15618. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  15619. if (ret != 0) {
  15620. ERROR_OUT(-8104, done);
  15621. }
  15622. keyInit = 1;
  15623. ret = wc_InitDhKey_ex(key2, HEAP_HINT, devId);
  15624. if (ret != 0) {
  15625. ERROR_OUT(-8105, done);
  15626. }
  15627. key2Init = 1;
  15628. #ifdef NO_ASN
  15629. #ifndef WOLFSSL_SP_MATH
  15630. ret = wc_DhSetKey(key, dh_p, sizeof(dh_p), dh_g, sizeof(dh_g));
  15631. if (ret != 0) {
  15632. ERROR_OUT(-8106, done);
  15633. }
  15634. ret = wc_DhSetKey(key2, dh_p, sizeof(dh_p), dh_g, sizeof(dh_g));
  15635. if (ret != 0) {
  15636. ERROR_OUT(-8107, done);
  15637. }
  15638. #else
  15639. ret = wc_DhSetKey(key, dh2048_p, sizeof(dh2048_p), dh2048_g,
  15640. sizeof(dh2048_g));
  15641. if (ret != 0) {
  15642. ERROR_OUT(-8106, done);
  15643. }
  15644. ret = wc_DhSetKey(key2, dh2048_p, sizeof(dh2048_p), dh2048_g,
  15645. sizeof(dh2048_g));
  15646. if (ret != 0) {
  15647. ERROR_OUT(-8107, done);
  15648. }
  15649. #endif
  15650. #else
  15651. ret = wc_DhKeyDecode(tmp, &idx, key, bytes);
  15652. if (ret != 0) {
  15653. ERROR_OUT(-8108, done);
  15654. }
  15655. idx = 0;
  15656. ret = wc_DhKeyDecode(tmp, &idx, key2, bytes);
  15657. if (ret != 0) {
  15658. ERROR_OUT(-8109, done);
  15659. }
  15660. #endif
  15661. #ifndef WC_NO_RNG
  15662. #ifndef HAVE_FIPS
  15663. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  15664. #else
  15665. ret = wc_InitRng(&rng);
  15666. #endif
  15667. if (ret != 0) {
  15668. ERROR_OUT(-8110, done);
  15669. }
  15670. rngInit = 1;
  15671. ret = wc_DhGenerateKeyPair(key, &rng, priv, &privSz, pub, &pubSz);
  15672. #if defined(WOLFSSL_ASYNC_CRYPT)
  15673. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  15674. #endif
  15675. if (ret != 0) {
  15676. ERROR_OUT(-8111, done);
  15677. }
  15678. ret = wc_DhGenerateKeyPair(key2, &rng, priv2, &privSz2, pub2, &pubSz2);
  15679. #if defined(WOLFSSL_ASYNC_CRYPT)
  15680. ret = wc_AsyncWait(ret, &key2->asyncDev, WC_ASYNC_FLAG_NONE);
  15681. #endif
  15682. if (ret != 0) {
  15683. ERROR_OUT(-8112, done);
  15684. }
  15685. ret = wc_DhAgree(key, agree, &agreeSz, priv, privSz, pub2, pubSz2);
  15686. #if defined(WOLFSSL_ASYNC_CRYPT)
  15687. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  15688. #endif
  15689. if (ret != 0) {
  15690. ERROR_OUT(-8113, done);
  15691. }
  15692. ret = wc_DhAgree(key2, agree2, &agreeSz2, priv2, privSz2, pub, pubSz);
  15693. #if defined(WOLFSSL_ASYNC_CRYPT)
  15694. ret = wc_AsyncWait(ret, &key2->asyncDev, WC_ASYNC_FLAG_NONE);
  15695. #endif
  15696. if (ret != 0) {
  15697. ERROR_OUT(-8114, done);
  15698. }
  15699. if (agreeSz != agreeSz2 || XMEMCMP(agree, agree2, agreeSz)) {
  15700. ERROR_OUT(-8115, done);
  15701. }
  15702. #endif /* !WC_NO_RNG */
  15703. #if defined(WOLFSSL_KEY_GEN) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  15704. if (wc_DhCheckPrivKey(NULL, NULL, 0) != BAD_FUNC_ARG)
  15705. ERROR_OUT(-8116, done);
  15706. if (wc_DhCheckPrivKey(key, priv, privSz) != 0)
  15707. ERROR_OUT(-8117, done);
  15708. if (wc_DhExportParamsRaw(NULL, NULL, NULL, NULL, NULL, NULL, NULL) != BAD_FUNC_ARG)
  15709. ERROR_OUT(-8118, done);
  15710. {
  15711. word32 pSz, qSz, gSz;
  15712. if (wc_DhExportParamsRaw(key, NULL, &pSz, NULL, &qSz, NULL, &gSz) != LENGTH_ONLY_E)
  15713. ERROR_OUT(-8119, done);
  15714. }
  15715. #endif
  15716. /* Test DH key import / export */
  15717. #if defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM) && \
  15718. (!defined(HAVE_FIPS) || \
  15719. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  15720. wc_FreeDhKey(key);
  15721. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  15722. if (ret != 0) {
  15723. ERROR_OUT(-8120, done);
  15724. }
  15725. #ifndef NO_ASN
  15726. {
  15727. /* DH Private - Key Export / Import */
  15728. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15729. byte *tmp2;
  15730. #else
  15731. byte tmp2[DH_TEST_TMP_SIZE];
  15732. #endif
  15733. XFILE file = XFOPEN(dhKeyFile, "rb");
  15734. if (!file)
  15735. ERROR_OUT(-8130, done);
  15736. bytes = (word32)XFREAD(tmp, 1, DH_TEST_TMP_SIZE, file);
  15737. XFCLOSE(file);
  15738. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15739. tmp2 = (byte*)XMALLOC(DH_TEST_TMP_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15740. if (tmp2 == NULL)
  15741. ERROR_OUT(-8131, done);
  15742. #endif
  15743. idx = 0;
  15744. XMEMSET(tmp2, 0, DH_TEST_TMP_SIZE);
  15745. /* Import DH Private key as DER */
  15746. ret = wc_DhKeyDecode(tmp, &idx, key, bytes);
  15747. if (ret == 0) {
  15748. /* Export as DER */
  15749. idx = DH_TEST_TMP_SIZE;
  15750. ret = wc_DhPrivKeyToDer(key, tmp2, &idx);
  15751. }
  15752. /* Verify export matches original */
  15753. if (ret <= 0 || bytes != idx || XMEMCMP(tmp, tmp2, bytes) != 0) {
  15754. ERROR_OUT(-8132, done);
  15755. }
  15756. /* DH Public Key - Export / Import */
  15757. file = XFOPEN(dhKeyPubFile, "rb");
  15758. if (!file)
  15759. ERROR_OUT(-8133, done);
  15760. bytes = (word32)XFREAD(tmp, 1, DH_TEST_TMP_SIZE, file);
  15761. XFCLOSE(file);
  15762. /* for HAVE_WOLF_BIGINT prevent leak */
  15763. wc_FreeDhKey(key);
  15764. (void)wc_InitDhKey_ex(key, HEAP_HINT, devId);
  15765. idx = 0;
  15766. XMEMSET(tmp2, 0, DH_TEST_TMP_SIZE);
  15767. /* Import DH Public key as DER */
  15768. ret = wc_DhKeyDecode(tmp, &idx, key, bytes);
  15769. if (ret == 0) {
  15770. /* Export as DER */
  15771. idx = DH_TEST_TMP_SIZE;
  15772. ret = wc_DhPubKeyToDer(key, tmp2, &idx);
  15773. }
  15774. /* Verify export matches original */
  15775. if (ret <= 0 || bytes != idx || XMEMCMP(tmp, tmp2, bytes) != 0) {
  15776. ERROR_OUT(-8134, done);
  15777. }
  15778. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15779. XFREE(tmp2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15780. #endif
  15781. }
  15782. #else
  15783. ret = wc_DhSetKey(key, dh_p, sizeof(dh_p), dh_g, sizeof(dh_g));
  15784. if (ret != 0) {
  15785. ERROR_OUT(-8121, done);
  15786. }
  15787. #endif /* !NO_ASN */
  15788. privSz = DH_TEST_BUF_SIZE;
  15789. pubSz = DH_TEST_BUF_SIZE;
  15790. ret = wc_DhExportKeyPair(key, priv, &privSz, pub, &pubSz);
  15791. if (ret != 0) {
  15792. ERROR_OUT(-8122, done);
  15793. }
  15794. ret = wc_DhImportKeyPair(key2, priv, privSz, pub, pubSz);
  15795. if (ret != 0) {
  15796. ERROR_OUT(-8125, done);
  15797. }
  15798. #endif /* WOLFSSL_DH_EXTRA && !NO_FILESYSTEM && !FIPS <= 2 */
  15799. #ifndef WC_NO_RNG
  15800. ret = dh_generate_test(&rng);
  15801. if (ret != 0)
  15802. ERROR_OUT(-8123, done);
  15803. ret = dh_fips_generate_test(&rng);
  15804. if (ret != 0)
  15805. ERROR_OUT(-8124, done);
  15806. #endif /* !WC_NO_RNG */
  15807. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  15808. ret = dh_test_check_pubvalue();
  15809. if (ret != 0)
  15810. ERROR_OUT(-8125, done);
  15811. #endif
  15812. #if !(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION == 2) && \
  15813. (defined(WOLFSSL_SP_ARM64_ASM) || defined(WOLFSSL_SP_ARM32_ASM)))
  15814. /* RNG with DH and SP_ASM code not supported in the in-lined FIPS ASM code,
  15815. * this will be available for testing in the 140-3 module */
  15816. #ifndef WC_NO_RNG
  15817. /* Specialized code for key gen when using FFDHE-2048, FFDHE-3072 and FFDHE-4096 */
  15818. #ifdef HAVE_FFDHE_2048
  15819. #ifdef HAVE_PUBLIC_FFDHE
  15820. ret = dh_ffdhe_test(&rng, wc_Dh_ffdhe2048_Get());
  15821. #else
  15822. ret = dh_ffdhe_test(&rng, WC_FFDHE_2048);
  15823. #endif
  15824. if (ret != 0)
  15825. ERROR_OUT(-8126, done);
  15826. #endif
  15827. #ifdef HAVE_FFDHE_3072
  15828. #ifdef HAVE_PUBLIC_FFDHE
  15829. ret = dh_ffdhe_test(&rng, wc_Dh_ffdhe3072_Get());
  15830. #else
  15831. ret = dh_ffdhe_test(&rng, WC_FFDHE_3072);
  15832. #endif
  15833. if (ret != 0)
  15834. ERROR_OUT(-8127, done);
  15835. #endif
  15836. #ifdef HAVE_FFDHE_4096
  15837. #ifdef HAVE_PUBLIC_FFDHE
  15838. ret = dh_ffdhe_test(&rng, wc_Dh_ffdhe4096_Get());
  15839. #else
  15840. ret = dh_ffdhe_test(&rng, WC_FFDHE_4096);
  15841. #endif
  15842. if (ret != 0)
  15843. ERROR_OUT(-8128, done);
  15844. #endif
  15845. #endif /* !WC_NO_RNG */
  15846. #endif /* HAVE_FIPS_VERSION == 2 && !WOLFSSL_SP_ARM64_ASM */
  15847. wc_FreeDhKey(key);
  15848. keyInit = 0;
  15849. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  15850. !defined(WOLFSSL_OLD_PRIME_CHECK) && !defined(WC_NO_RNG)
  15851. /* Test Check Key */
  15852. ret = wc_DhSetCheckKey(key, dh_p, sizeof(dh_p), dh_g, sizeof(dh_g),
  15853. NULL, 0, 0, &rng);
  15854. if (ret != 0)
  15855. ERROR_OUT(-8129, done);
  15856. keyInit = 1; /* DhSetCheckKey also initializes the key, free it */
  15857. #endif
  15858. done:
  15859. #ifndef WC_NO_RNG
  15860. if (rngInit)
  15861. wc_FreeRng(&rng);
  15862. #endif
  15863. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15864. if (key) {
  15865. if (keyInit)
  15866. wc_FreeDhKey(key);
  15867. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15868. }
  15869. if (key2) {
  15870. if (key2Init)
  15871. wc_FreeDhKey(key2);
  15872. XFREE(key2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15873. }
  15874. if (tmp)
  15875. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15876. if (priv)
  15877. XFREE(priv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15878. if (pub)
  15879. XFREE(pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15880. if (priv2)
  15881. XFREE(priv2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15882. if (pub2)
  15883. XFREE(pub2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15884. if (agree)
  15885. XFREE(agree, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15886. if (agree2)
  15887. XFREE(agree2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15888. #else
  15889. if (keyInit)
  15890. wc_FreeDhKey(key);
  15891. if (key2Init)
  15892. wc_FreeDhKey(key2);
  15893. #endif
  15894. (void)privSz;
  15895. (void)pubSz;
  15896. (void)pubSz2;
  15897. (void)privSz2;
  15898. return ret;
  15899. #undef DH_TEST_BUF_SIZE
  15900. #undef DH_TEST_TMP_SIZE
  15901. }
  15902. #endif /* NO_DH */
  15903. #ifndef NO_DSA
  15904. WOLFSSL_TEST_SUBROUTINE int dsa_test(void)
  15905. {
  15906. int ret = 0, answer;
  15907. word32 bytes;
  15908. word32 idx = 0;
  15909. WC_RNG rng;
  15910. wc_Sha sha;
  15911. byte hash[WC_SHA_DIGEST_SIZE];
  15912. byte signature[40];
  15913. #ifdef WOLFSSL_KEY_GEN
  15914. byte* der = 0;
  15915. #endif
  15916. #define DSA_TEST_TMP_SIZE 1024
  15917. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15918. byte *tmp = (byte *)XMALLOC(DSA_TEST_TMP_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15919. DsaKey *key = (DsaKey *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15920. #ifdef WOLFSSL_KEY_GEN
  15921. DsaKey *derIn = (DsaKey *)XMALLOC(sizeof *derIn, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15922. DsaKey *genKey = (DsaKey *)XMALLOC(sizeof *genKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15923. #endif
  15924. if ((tmp == NULL) ||
  15925. (key == NULL)
  15926. #ifdef WOLFSSL_KEY_GEN
  15927. || (derIn == NULL)
  15928. || (genKey == NULL)
  15929. #endif
  15930. ) {
  15931. ret = -8216;
  15932. goto out;
  15933. }
  15934. #else
  15935. byte tmp[1024];
  15936. DsaKey key[1];
  15937. #ifdef WOLFSSL_KEY_GEN
  15938. DsaKey derIn[1];
  15939. DsaKey genKey[1];
  15940. #endif
  15941. #endif
  15942. #ifdef USE_CERT_BUFFERS_1024
  15943. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  15944. bytes = sizeof_dsa_key_der_1024;
  15945. #elif defined(USE_CERT_BUFFERS_2048)
  15946. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  15947. bytes = sizeof_dsa_key_der_2048;
  15948. #else
  15949. {
  15950. XFILE file = XFOPEN(dsaKey, "rb");
  15951. if (!file)
  15952. ERROR_OUT(-8200, out);
  15953. bytes = (word32) XFREAD(tmp, 1, DSA_TEST_TMP_SIZE, file);
  15954. XFCLOSE(file);
  15955. }
  15956. #endif /* USE_CERT_BUFFERS */
  15957. ret = wc_InitSha_ex(&sha, HEAP_HINT, devId);
  15958. if (ret != 0)
  15959. ERROR_OUT(-8201, out);
  15960. wc_ShaUpdate(&sha, tmp, bytes);
  15961. wc_ShaFinal(&sha, hash);
  15962. wc_ShaFree(&sha);
  15963. ret = wc_InitDsaKey(key);
  15964. if (ret != 0)
  15965. ERROR_OUT(-8202, out);
  15966. ret = wc_DsaPrivateKeyDecode(tmp, &idx, key, bytes);
  15967. if (ret != 0)
  15968. ERROR_OUT(-8203, out);
  15969. #ifndef HAVE_FIPS
  15970. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  15971. #else
  15972. ret = wc_InitRng(&rng);
  15973. #endif
  15974. if (ret != 0)
  15975. ERROR_OUT(-8204, out);
  15976. ret = wc_DsaSign(hash, signature, key, &rng);
  15977. if (ret != 0)
  15978. ERROR_OUT(-8205, out);
  15979. ret = wc_DsaVerify(hash, signature, key, &answer);
  15980. if (ret != 0)
  15981. ERROR_OUT(-8206, out);
  15982. if (answer != 1)
  15983. ERROR_OUT(-8207, out);
  15984. wc_FreeDsaKey(key);
  15985. #ifdef WOLFSSL_KEY_GEN
  15986. {
  15987. int derSz = 0;
  15988. ret = wc_InitDsaKey(genKey);
  15989. if (ret != 0)
  15990. ERROR_OUT(-8208, out);
  15991. ret = wc_MakeDsaParameters(&rng, 1024, genKey);
  15992. if (ret != 0) {
  15993. wc_FreeDsaKey(genKey);
  15994. ERROR_OUT(-8209, out);
  15995. }
  15996. ret = wc_MakeDsaKey(&rng, genKey);
  15997. if (ret != 0) {
  15998. wc_FreeDsaKey(genKey);
  15999. ERROR_OUT(-8210, out);
  16000. }
  16001. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16002. if (der == NULL) {
  16003. wc_FreeDsaKey(genKey);
  16004. ERROR_OUT(-8211, out);
  16005. }
  16006. derSz = wc_DsaKeyToDer(genKey, der, FOURK_BUF);
  16007. if (derSz < 0) {
  16008. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16009. ERROR_OUT(-8212, out);
  16010. }
  16011. ret = SaveDerAndPem(der, derSz, keyDerFile, keyPemFile,
  16012. DSA_PRIVATEKEY_TYPE, -5814);
  16013. if (ret != 0) {
  16014. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16015. wc_FreeDsaKey(genKey);
  16016. goto out;
  16017. }
  16018. ret = wc_InitDsaKey(derIn);
  16019. if (ret != 0) {
  16020. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16021. wc_FreeDsaKey(genKey);
  16022. ERROR_OUT(-8213, out);
  16023. }
  16024. idx = 0;
  16025. ret = wc_DsaPrivateKeyDecode(der, &idx, derIn, derSz);
  16026. if (ret != 0) {
  16027. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16028. wc_FreeDsaKey(derIn);
  16029. wc_FreeDsaKey(genKey);
  16030. ERROR_OUT(-8214, out);
  16031. }
  16032. }
  16033. #endif /* WOLFSSL_KEY_GEN */
  16034. out:
  16035. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16036. if (key) {
  16037. #endif
  16038. if (wc_InitDsaKey_h(key, NULL) != 0)
  16039. ret = -8215;
  16040. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16041. }
  16042. #endif
  16043. #ifdef WOLFSSL_KEY_GEN
  16044. if (der)
  16045. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16046. #endif
  16047. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16048. if (tmp)
  16049. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16050. if (key)
  16051. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16052. #ifdef WOLFSSL_KEY_GEN
  16053. if (derIn) {
  16054. wc_FreeDsaKey(derIn);
  16055. XFREE(derIn, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16056. }
  16057. if (genKey) {
  16058. wc_FreeDsaKey(genKey);
  16059. XFREE(genKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16060. }
  16061. #endif
  16062. #else /* !WOLFSSL_SMALL_STACK || WOLFSSL_NO_MALLOC */
  16063. #ifdef WOLFSSL_KEY_GEN
  16064. wc_FreeDsaKey(derIn);
  16065. wc_FreeDsaKey(genKey);
  16066. #endif
  16067. #endif
  16068. wc_FreeRng(&rng);
  16069. return ret;
  16070. }
  16071. #endif /* NO_DSA */
  16072. #ifdef WOLFCRYPT_HAVE_SRP
  16073. static int generate_random_salt(byte *buf, word32 size)
  16074. {
  16075. int ret = -8220;
  16076. WC_RNG rng;
  16077. if(NULL == buf || !size)
  16078. return -8221;
  16079. if (buf && size && wc_InitRng_ex(&rng, HEAP_HINT, devId) == 0) {
  16080. ret = wc_RNG_GenerateBlock(&rng, (byte *)buf, size);
  16081. wc_FreeRng(&rng);
  16082. }
  16083. return ret;
  16084. }
  16085. static int srp_test_digest(SrpType dgstType)
  16086. {
  16087. int r;
  16088. byte clientPubKey[192]; /* A */
  16089. byte serverPubKey[192]; /* B */
  16090. word32 clientPubKeySz = 192;
  16091. word32 serverPubKeySz = 192;
  16092. byte username[] = "user";
  16093. word32 usernameSz = 4;
  16094. byte password[] = "password";
  16095. word32 passwordSz = 8;
  16096. WOLFSSL_SMALL_STACK_STATIC const byte N[] = {
  16097. 0xfc, 0x58, 0x7a, 0x8a, 0x70, 0xfb, 0x5a, 0x9a,
  16098. 0x5d, 0x39, 0x48, 0xbf, 0x1c, 0x46, 0xd8, 0x3b,
  16099. 0x7a, 0xe9, 0x1f, 0x85, 0x36, 0x18, 0xc4, 0x35,
  16100. 0x3f, 0xf8, 0x8a, 0x8f, 0x8c, 0x10, 0x2e, 0x01,
  16101. 0x58, 0x1d, 0x41, 0xcb, 0xc4, 0x47, 0xa8, 0xaf,
  16102. 0x9a, 0x6f, 0x58, 0x14, 0xa4, 0x68, 0xf0, 0x9c,
  16103. 0xa6, 0xe7, 0xbf, 0x0d, 0xe9, 0x62, 0x0b, 0xd7,
  16104. 0x26, 0x46, 0x5b, 0x27, 0xcb, 0x4c, 0xf9, 0x7e,
  16105. 0x1e, 0x8b, 0xe6, 0xdd, 0x29, 0xb7, 0xb7, 0x15,
  16106. 0x2e, 0xcf, 0x23, 0xa6, 0x4b, 0x97, 0x9f, 0x89,
  16107. 0xd4, 0x86, 0xc4, 0x90, 0x63, 0x92, 0xf4, 0x30,
  16108. 0x26, 0x69, 0x48, 0x9d, 0x7a, 0x4f, 0xad, 0xb5,
  16109. 0x6a, 0x51, 0xad, 0xeb, 0xf9, 0x90, 0x31, 0x77,
  16110. 0x53, 0x30, 0x2a, 0x85, 0xf7, 0x11, 0x21, 0x0c,
  16111. 0xb8, 0x4b, 0x56, 0x03, 0x5e, 0xbb, 0x25, 0x33,
  16112. 0x7c, 0xd9, 0x5a, 0xd1, 0x5c, 0xb2, 0xd4, 0x53,
  16113. 0xc5, 0x16, 0x68, 0xf0, 0xdf, 0x48, 0x55, 0x3e,
  16114. 0xd4, 0x59, 0x87, 0x64, 0x59, 0xaa, 0x39, 0x01,
  16115. 0x45, 0x89, 0x9c, 0x72, 0xff, 0xdd, 0x8f, 0x6d,
  16116. 0xa0, 0x42, 0xbc, 0x6f, 0x6e, 0x62, 0x18, 0x2d,
  16117. 0x50, 0xe8, 0x18, 0x97, 0x87, 0xfc, 0xef, 0x1f,
  16118. 0xf5, 0x53, 0x68, 0xe8, 0x49, 0xd1, 0xa2, 0xe8,
  16119. 0xb9, 0x26, 0x03, 0xba, 0xb5, 0x58, 0x6f, 0x6c,
  16120. 0x8b, 0x08, 0xa1, 0x7b, 0x6f, 0x42, 0xc9, 0x53
  16121. };
  16122. WOLFSSL_SMALL_STACK_STATIC const byte g[] = {
  16123. 0x02
  16124. };
  16125. byte salt[10];
  16126. byte verifier[192];
  16127. word32 v_size = sizeof(verifier);
  16128. word32 clientProofSz = SRP_MAX_DIGEST_SIZE;
  16129. word32 serverProofSz = SRP_MAX_DIGEST_SIZE;
  16130. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16131. Srp *cli = (Srp *)XMALLOC(sizeof *cli, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16132. Srp *srv = (Srp *)XMALLOC(sizeof *srv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16133. byte *clientProof = (byte *)XMALLOC(SRP_MAX_DIGEST_SIZE, HEAP_HINT,
  16134. DYNAMIC_TYPE_TMP_BUFFER); /* M1 */
  16135. byte *serverProof = (byte *)XMALLOC(SRP_MAX_DIGEST_SIZE, HEAP_HINT,
  16136. DYNAMIC_TYPE_TMP_BUFFER); /* M2 */
  16137. if ((cli == NULL) ||
  16138. (srv == NULL) ||
  16139. (clientProof == NULL) ||
  16140. (serverProof == NULL)) {
  16141. r = -8222;
  16142. goto out;
  16143. }
  16144. #else
  16145. Srp cli[1], srv[1];
  16146. byte clientProof[SRP_MAX_DIGEST_SIZE]; /* M1 */
  16147. byte serverProof[SRP_MAX_DIGEST_SIZE]; /* M2 */
  16148. #endif
  16149. /* set as 0's so if second init on srv not called SrpTerm is not on
  16150. * garbage values */
  16151. XMEMSET(srv, 0, sizeof *srv);
  16152. XMEMSET(cli, 0, sizeof *cli);
  16153. /* generating random salt */
  16154. r = generate_random_salt(salt, sizeof(salt));
  16155. /* client knows username and password. */
  16156. /* server knows N, g, salt and verifier. */
  16157. if (!r) r = wc_SrpInit_ex(cli, dgstType, SRP_CLIENT_SIDE, HEAP_HINT, devId);
  16158. if (!r) r = wc_SrpSetUsername(cli, username, usernameSz);
  16159. /* loading N, g and salt in advance to generate the verifier. */
  16160. if (!r) r = wc_SrpSetParams(cli, N, sizeof(N),
  16161. g, sizeof(g),
  16162. salt, sizeof(salt));
  16163. if (!r) r = wc_SrpSetPassword(cli, password, passwordSz);
  16164. if (!r) r = wc_SrpGetVerifier(cli, verifier, &v_size);
  16165. /* client sends username to server */
  16166. if (!r) r = wc_SrpInit_ex(srv, dgstType, SRP_SERVER_SIDE, HEAP_HINT, devId);
  16167. if (!r) r = wc_SrpSetUsername(srv, username, usernameSz);
  16168. if (!r) r = wc_SrpSetParams(srv, N, sizeof(N),
  16169. g, sizeof(g),
  16170. salt, sizeof(salt));
  16171. if (!r) r = wc_SrpSetVerifier(srv, verifier, v_size);
  16172. if (!r) r = wc_SrpGetPublic(srv, serverPubKey, &serverPubKeySz);
  16173. /* server sends N, g, salt and B to client */
  16174. if (!r) r = wc_SrpGetPublic(cli, clientPubKey, &clientPubKeySz);
  16175. if (!r) r = wc_SrpComputeKey(cli, clientPubKey, clientPubKeySz,
  16176. serverPubKey, serverPubKeySz);
  16177. if (!r) r = wc_SrpGetProof(cli, clientProof, &clientProofSz);
  16178. /* client sends A and M1 to server */
  16179. if (!r) r = wc_SrpComputeKey(srv, clientPubKey, clientPubKeySz,
  16180. serverPubKey, serverPubKeySz);
  16181. if (!r) r = wc_SrpVerifyPeersProof(srv, clientProof, clientProofSz);
  16182. if (!r) r = wc_SrpGetProof(srv, serverProof, &serverProofSz);
  16183. /* server sends M2 to client */
  16184. if (!r) r = wc_SrpVerifyPeersProof(cli, serverProof, serverProofSz);
  16185. wc_SrpTerm(cli);
  16186. wc_SrpTerm(srv);
  16187. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16188. out:
  16189. if (cli)
  16190. XFREE(cli, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16191. if (srv)
  16192. XFREE(srv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16193. if (clientProof)
  16194. XFREE(clientProof, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16195. if (serverProof)
  16196. XFREE(serverProof, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16197. #endif
  16198. return r;
  16199. }
  16200. WOLFSSL_TEST_SUBROUTINE int srp_test(void)
  16201. {
  16202. int ret;
  16203. #ifndef NO_SHA
  16204. ret = srp_test_digest(SRP_TYPE_SHA);
  16205. if (ret != 0)
  16206. return ret;
  16207. #endif
  16208. #ifndef NO_SHA256
  16209. ret = srp_test_digest(SRP_TYPE_SHA256);
  16210. if (ret != 0)
  16211. return ret;
  16212. #endif
  16213. #ifdef WOLFSSL_SHA384
  16214. ret = srp_test_digest(SRP_TYPE_SHA384);
  16215. if (ret != 0)
  16216. return ret;
  16217. #endif
  16218. #ifdef WOLFSSL_SHA512
  16219. ret = srp_test_digest(SRP_TYPE_SHA512);
  16220. if (ret != 0)
  16221. return ret;
  16222. #endif
  16223. return ret;
  16224. }
  16225. #endif /* WOLFCRYPT_HAVE_SRP */
  16226. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  16227. #if !defined(NO_AES) && !defined(WOLFCRYPT_ONLY)
  16228. static int openssl_aes_test(void)
  16229. {
  16230. #ifdef HAVE_AES_CBC
  16231. #ifdef WOLFSSL_AES_128
  16232. {
  16233. /* EVP_CipherUpdate test */
  16234. WOLFSSL_SMALL_STACK_STATIC const byte cbcPlain[] =
  16235. {
  16236. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  16237. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  16238. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  16239. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  16240. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  16241. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  16242. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  16243. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  16244. };
  16245. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  16246. "0123456789abcdef "; /* align */
  16247. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  16248. "1234567890abcdef "; /* align */
  16249. byte cipher[AES_BLOCK_SIZE * 4];
  16250. byte plain [AES_BLOCK_SIZE * 4];
  16251. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16252. EVP_CIPHER_CTX *en = wolfSSL_EVP_CIPHER_CTX_new();
  16253. EVP_CIPHER_CTX *de = wolfSSL_EVP_CIPHER_CTX_new();
  16254. #else
  16255. EVP_CIPHER_CTX en[1];
  16256. EVP_CIPHER_CTX de[1];
  16257. #endif
  16258. int outlen ;
  16259. int total = 0;
  16260. int i;
  16261. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16262. if ((en == NULL) || (de == NULL))
  16263. return MEMORY_E;
  16264. #endif
  16265. EVP_CIPHER_CTX_init(en);
  16266. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  16267. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  16268. return -8400;
  16269. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen,
  16270. (byte*)cbcPlain, 9) == 0)
  16271. return -8401;
  16272. if (outlen != 0)
  16273. return -8402;
  16274. total += outlen;
  16275. if (EVP_CipherUpdate(en, (byte*)&cipher[total], &outlen,
  16276. (byte*)&cbcPlain[9] , 9) == 0)
  16277. return -8403;
  16278. if (outlen != 16)
  16279. return -8404;
  16280. total += outlen;
  16281. if (EVP_CipherFinal(en, (byte*)&cipher[total], &outlen) == 0)
  16282. return -8405;
  16283. if (outlen != 16)
  16284. return -8406;
  16285. total += outlen;
  16286. if (total != 32)
  16287. return 3408;
  16288. total = 0;
  16289. EVP_CIPHER_CTX_init(de);
  16290. if (EVP_CipherInit(de, EVP_aes_128_cbc(),
  16291. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  16292. return -8407;
  16293. if (EVP_CipherUpdate(de, (byte*)plain, &outlen, (byte*)cipher, 6) == 0)
  16294. return -8408;
  16295. if (outlen != 0)
  16296. return -8409;
  16297. total += outlen;
  16298. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen,
  16299. (byte*)&cipher[6], 12) == 0)
  16300. return -8410;
  16301. if (outlen != 0)
  16302. total += outlen;
  16303. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen,
  16304. (byte*)&cipher[6+12], 14) == 0)
  16305. return -8411;
  16306. if (outlen != 16)
  16307. return -8412;
  16308. total += outlen;
  16309. if (EVP_CipherFinal(de, (byte*)&plain[total], &outlen) == 0)
  16310. return -8413;
  16311. if (outlen != 2)
  16312. return -8414;
  16313. total += outlen;
  16314. if (total != 18)
  16315. return 3427;
  16316. if (XMEMCMP(plain, cbcPlain, 18))
  16317. return -8415;
  16318. /* test with encrypting/decrypting more than 16 bytes at once */
  16319. total = 0;
  16320. EVP_CIPHER_CTX_init(en);
  16321. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  16322. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  16323. return -8416;
  16324. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen,
  16325. (byte*)cbcPlain, 17) == 0)
  16326. return -8417;
  16327. if (outlen != 16)
  16328. return -8418;
  16329. total += outlen;
  16330. if (EVP_CipherUpdate(en, (byte*)&cipher[total], &outlen,
  16331. (byte*)&cbcPlain[17] , 1) == 0)
  16332. return -8419;
  16333. if (outlen != 0)
  16334. return -8420;
  16335. total += outlen;
  16336. if (EVP_CipherFinal(en, (byte*)&cipher[total], &outlen) == 0)
  16337. return -8421;
  16338. if (outlen != 16)
  16339. return -8422;
  16340. total += outlen;
  16341. if (total != 32)
  16342. return -8423;
  16343. total = 0;
  16344. EVP_CIPHER_CTX_init(de);
  16345. if (EVP_CipherInit(de, EVP_aes_128_cbc(),
  16346. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  16347. return -8424;
  16348. if (EVP_CipherUpdate(de, (byte*)plain, &outlen, (byte*)cipher, 17) == 0)
  16349. return -8425;
  16350. if (outlen != 16)
  16351. return -8426;
  16352. total += outlen;
  16353. /* final call on non block size should fail */
  16354. if (EVP_CipherFinal(de, (byte*)&plain[total], &outlen) != 0)
  16355. return -8427;
  16356. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen,
  16357. (byte*)&cipher[17], 1) == 0)
  16358. return -8428;
  16359. if (outlen != 0)
  16360. total += outlen;
  16361. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen,
  16362. (byte*)&cipher[17+1], 14) == 0)
  16363. return -8429;
  16364. if (outlen != 0)
  16365. return -8430;
  16366. total += outlen;
  16367. if (EVP_CipherFinal(de, (byte*)&plain[total], &outlen) == 0)
  16368. return -8431;
  16369. if (outlen != 2)
  16370. return -8432;
  16371. total += outlen;
  16372. if (total != 18)
  16373. return -8433;
  16374. if (XMEMCMP(plain, cbcPlain, 18))
  16375. return -8434;
  16376. /* test byte by byte decrypt */
  16377. for (i = 0; i < AES_BLOCK_SIZE * 3; i++) {
  16378. plain[i] = i;
  16379. }
  16380. total = 0;
  16381. EVP_CIPHER_CTX_init(en);
  16382. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  16383. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  16384. return -8435;
  16385. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen,
  16386. (byte*)plain, AES_BLOCK_SIZE * 3) == 0)
  16387. return -8436;
  16388. if (outlen != AES_BLOCK_SIZE * 3)
  16389. return -8437;
  16390. total += outlen;
  16391. if (EVP_CipherFinal(en, (byte*)&cipher[total], &outlen) == 0)
  16392. return -8438;
  16393. if (outlen != AES_BLOCK_SIZE)
  16394. return -8439;
  16395. total += outlen;
  16396. if (total != sizeof(plain))
  16397. return -8440;
  16398. total = 0;
  16399. EVP_CIPHER_CTX_init(de);
  16400. if (EVP_CipherInit(de, EVP_aes_128_cbc(),
  16401. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  16402. return -8441;
  16403. for (i = 0; i < AES_BLOCK_SIZE * 4; i++) {
  16404. if (EVP_CipherUpdate(de, (byte*)plain + total, &outlen,
  16405. (byte*)cipher + i, 1) == 0)
  16406. return -8442;
  16407. if (outlen > 0) {
  16408. int j;
  16409. total += outlen;
  16410. for (j = 0; j < total; j++) {
  16411. if (plain[j] != j) {
  16412. return -8443;
  16413. }
  16414. }
  16415. }
  16416. }
  16417. if (EVP_CipherFinal(de, (byte*)&plain[total], &outlen) == 0)
  16418. return -8444;
  16419. total += outlen;
  16420. if (total != AES_BLOCK_SIZE * 3) {
  16421. return -8445;
  16422. }
  16423. for (i = 0; i < AES_BLOCK_SIZE * 3; i++) {
  16424. if (plain[i] != i) {
  16425. return -8446;
  16426. }
  16427. }
  16428. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16429. wolfSSL_EVP_CIPHER_CTX_free(en);
  16430. wolfSSL_EVP_CIPHER_CTX_free(de);
  16431. #endif
  16432. }
  16433. /* set buffers to be exact size to catch potential over read/write */
  16434. {
  16435. /* EVP_CipherUpdate test */
  16436. WOLFSSL_SMALL_STACK_STATIC const byte cbcPlain[] =
  16437. {
  16438. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  16439. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  16440. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  16441. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  16442. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  16443. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  16444. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  16445. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  16446. };
  16447. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  16448. "0123456789abcdef "; /* align */
  16449. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  16450. "1234567890abcdef "; /* align */
  16451. #define EVP_TEST_BUF_SZ 18
  16452. #define EVP_TEST_BUF_PAD 32
  16453. byte cipher[EVP_TEST_BUF_SZ];
  16454. byte plain [EVP_TEST_BUF_SZ];
  16455. byte padded[EVP_TEST_BUF_PAD];
  16456. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16457. EVP_CIPHER_CTX *en = wolfSSL_EVP_CIPHER_CTX_new();
  16458. EVP_CIPHER_CTX *de = wolfSSL_EVP_CIPHER_CTX_new();
  16459. #else
  16460. EVP_CIPHER_CTX en[1];
  16461. EVP_CIPHER_CTX de[1];
  16462. #endif
  16463. int outlen ;
  16464. int total = 0;
  16465. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16466. if ((en == NULL) || (de == NULL))
  16467. return MEMORY_E;
  16468. #endif
  16469. EVP_CIPHER_CTX_init(en);
  16470. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  16471. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  16472. return -8447;
  16473. if (EVP_CIPHER_CTX_set_padding(en, 0) != 1)
  16474. return -8448;
  16475. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen,
  16476. (byte*)cbcPlain, EVP_TEST_BUF_SZ) == 0)
  16477. return -8449;
  16478. if (outlen != 16)
  16479. return -8450;
  16480. total += outlen;
  16481. /* should fail here */
  16482. if (EVP_CipherFinal(en, (byte*)&cipher[total], &outlen) != 0)
  16483. return -8451;
  16484. /* turn padding back on and do successful encrypt */
  16485. total = 0;
  16486. EVP_CIPHER_CTX_init(en);
  16487. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  16488. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  16489. return -8452;
  16490. if (EVP_CIPHER_CTX_set_padding(en, 1) != 1)
  16491. return -8453;
  16492. if (EVP_CipherUpdate(en, (byte*)padded, &outlen,
  16493. (byte*)cbcPlain, EVP_TEST_BUF_SZ) == 0)
  16494. return -8454;
  16495. if (outlen != 16)
  16496. return -8455;
  16497. total += outlen;
  16498. if (EVP_CipherFinal(en, (byte*)&padded[total], &outlen) == 0)
  16499. return -8456;
  16500. total += outlen;
  16501. if (total != 32)
  16502. return -8457;
  16503. XMEMCPY(cipher, padded, EVP_TEST_BUF_SZ);
  16504. /* test out of bounds read on buffers w/o padding during decryption */
  16505. total = 0;
  16506. EVP_CIPHER_CTX_init(de);
  16507. if (EVP_CipherInit(de, EVP_aes_128_cbc(),
  16508. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  16509. return -8458;
  16510. if (EVP_CIPHER_CTX_set_padding(de, 0) != 1)
  16511. return -8459;
  16512. if (EVP_CipherUpdate(de, (byte*)plain, &outlen, (byte*)cipher,
  16513. EVP_TEST_BUF_SZ) == 0)
  16514. return -8460;
  16515. if (outlen != 16)
  16516. return -8461;
  16517. total += outlen;
  16518. /* should fail since not using padding */
  16519. if (EVP_CipherFinal(de, (byte*)&plain[total], &outlen) != 0)
  16520. return -8462;
  16521. total = 0;
  16522. EVP_CIPHER_CTX_init(de);
  16523. if (EVP_CipherInit(de, EVP_aes_128_cbc(),
  16524. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  16525. return -8463;
  16526. if (EVP_CIPHER_CTX_set_padding(de, 1) != 1)
  16527. return -8464;
  16528. if (EVP_CipherUpdate(de, (byte*)padded, &outlen, (byte*)padded,
  16529. EVP_TEST_BUF_PAD) == 0)
  16530. return -8465;
  16531. if (outlen != 16)
  16532. return -8466;
  16533. total += outlen;
  16534. if (EVP_CipherFinal(de, (byte*)&padded[total], &outlen) == 0)
  16535. return -8467;
  16536. if (XMEMCMP(padded, cbcPlain, EVP_TEST_BUF_SZ))
  16537. return -8468;
  16538. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16539. wolfSSL_EVP_CIPHER_CTX_free(en);
  16540. wolfSSL_EVP_CIPHER_CTX_free(de);
  16541. #endif
  16542. }
  16543. { /* evp_cipher test: EVP_aes_128_cbc */
  16544. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16545. EVP_CIPHER_CTX *ctx = wolfSSL_EVP_CIPHER_CTX_new();
  16546. #else
  16547. EVP_CIPHER_CTX ctx[1];
  16548. #endif
  16549. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = { /* "Now is the time for all " w/o trailing 0 */
  16550. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  16551. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  16552. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  16553. };
  16554. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  16555. {
  16556. 0x95,0x94,0x92,0x57,0x5f,0x42,0x81,0x53,
  16557. 0x2c,0xcc,0x9d,0x46,0x77,0xa2,0x33,0xcb
  16558. };
  16559. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  16560. "0123456789abcdef "; /* align */
  16561. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  16562. "1234567890abcdef "; /* align */
  16563. byte cipher[AES_BLOCK_SIZE * 4];
  16564. byte plain [AES_BLOCK_SIZE * 4];
  16565. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16566. if (ctx == NULL)
  16567. return MEMORY_E;
  16568. #endif
  16569. EVP_CIPHER_CTX_init(ctx);
  16570. if (EVP_CipherInit(ctx, EVP_aes_128_cbc(), key, iv, 1) == 0)
  16571. return -8469;
  16572. if (EVP_Cipher(ctx, cipher, (byte*)msg, 16) != 16)
  16573. return -8470;
  16574. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE))
  16575. return -8471;
  16576. EVP_CIPHER_CTX_init(ctx);
  16577. if (EVP_CipherInit(ctx, EVP_aes_128_cbc(), key, iv, 0) == 0)
  16578. return -8472;
  16579. if (EVP_Cipher(ctx, plain, cipher, 16) != 16)
  16580. return -8473;
  16581. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  16582. return -8474;
  16583. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16584. wolfSSL_EVP_CIPHER_CTX_free(ctx);
  16585. #endif
  16586. } /* end evp_cipher test: EVP_aes_128_cbc*/
  16587. #endif /* WOLFSSL_AES_128 */
  16588. #endif /* HAVE_AES_CBC */
  16589. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_256)
  16590. { /* evp_cipher test: EVP_aes_256_ecb*/
  16591. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16592. EVP_CIPHER_CTX *ctx = wolfSSL_EVP_CIPHER_CTX_new();
  16593. #else
  16594. EVP_CIPHER_CTX ctx[1];
  16595. #endif
  16596. WOLFSSL_SMALL_STACK_STATIC const byte msg[] =
  16597. {
  16598. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  16599. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  16600. };
  16601. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  16602. {
  16603. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  16604. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  16605. };
  16606. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  16607. {
  16608. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  16609. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  16610. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  16611. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  16612. };
  16613. byte cipher[AES_BLOCK_SIZE * 4];
  16614. byte plain [AES_BLOCK_SIZE * 4];
  16615. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16616. if (ctx == NULL)
  16617. return MEMORY_E;
  16618. #endif
  16619. EVP_CIPHER_CTX_init(ctx);
  16620. if (EVP_CipherInit(ctx, EVP_aes_256_ecb(), (unsigned char*)key, NULL, 1) == 0)
  16621. return -8475;
  16622. if (EVP_Cipher(ctx, cipher, (byte*)msg, 16) != 16)
  16623. return -8476;
  16624. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE))
  16625. return -8477;
  16626. EVP_CIPHER_CTX_init(ctx);
  16627. if (EVP_CipherInit(ctx, EVP_aes_256_ecb(), (unsigned char*)key, NULL, 0) == 0)
  16628. return -8478;
  16629. if (EVP_Cipher(ctx, plain, cipher, 16) != 16)
  16630. return -8479;
  16631. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  16632. return -8480;
  16633. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16634. wolfSSL_EVP_CIPHER_CTX_free(ctx);
  16635. #endif
  16636. } /* end evp_cipher test */
  16637. #endif /* HAVE_AES_ECB && WOLFSSL_AES_256 */
  16638. #if defined(WOLFSSL_AES_DIRECT) && defined(WOLFSSL_AES_256)
  16639. /* enable HAVE_AES_DECRYPT for AES_encrypt/decrypt */
  16640. {
  16641. /* Test: AES_encrypt/decrypt/set Key */
  16642. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16643. AES_KEY *enc = (AES_KEY *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  16644. #ifdef HAVE_AES_DECRYPT
  16645. AES_KEY *dec = (AES_KEY *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  16646. #endif
  16647. #else
  16648. AES_KEY enc[1];
  16649. #ifdef HAVE_AES_DECRYPT
  16650. AES_KEY dec[1];
  16651. #endif
  16652. #endif
  16653. WOLFSSL_SMALL_STACK_STATIC const byte msg[] =
  16654. {
  16655. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  16656. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  16657. };
  16658. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  16659. {
  16660. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  16661. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  16662. };
  16663. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  16664. {
  16665. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  16666. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  16667. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  16668. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  16669. };
  16670. byte plain[sizeof(msg)];
  16671. byte cipher[sizeof(msg)];
  16672. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16673. if (enc == NULL)
  16674. return MEMORY_E;
  16675. #ifdef HAVE_AES_DECRYPT
  16676. if (dec == NULL)
  16677. return MEMORY_E;
  16678. #endif
  16679. #endif
  16680. AES_set_encrypt_key(key, sizeof(key)*8, enc);
  16681. AES_set_decrypt_key(key, sizeof(key)*8, dec);
  16682. AES_encrypt(msg, cipher, enc);
  16683. #ifdef HAVE_AES_DECRYPT
  16684. AES_decrypt(cipher, plain, dec);
  16685. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  16686. return -8481;
  16687. #endif /* HAVE_AES_DECRYPT */
  16688. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE))
  16689. return -8482;
  16690. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16691. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  16692. #ifdef HAVE_AES_DECRYPT
  16693. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  16694. #endif
  16695. #endif
  16696. }
  16697. #endif /* WOLFSSL_AES_DIRECT && WOLFSSL_AES_256 */
  16698. /* EVP_Cipher with EVP_aes_xxx_ctr() */
  16699. #ifdef WOLFSSL_AES_COUNTER
  16700. {
  16701. byte plainBuff [64];
  16702. byte cipherBuff[64];
  16703. #ifdef WOLFSSL_AES_128
  16704. WOLFSSL_SMALL_STACK_STATIC const byte ctrKey[] =
  16705. {
  16706. 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,
  16707. 0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c
  16708. };
  16709. WOLFSSL_SMALL_STACK_STATIC const byte ctrIv[] =
  16710. {
  16711. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  16712. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  16713. };
  16714. WOLFSSL_SMALL_STACK_STATIC const byte ctrPlain[] =
  16715. {
  16716. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  16717. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  16718. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  16719. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  16720. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  16721. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  16722. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  16723. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  16724. };
  16725. WOLFSSL_SMALL_STACK_STATIC const byte ctrCipher[] =
  16726. {
  16727. 0x87,0x4d,0x61,0x91,0xb6,0x20,0xe3,0x26,
  16728. 0x1b,0xef,0x68,0x64,0x99,0x0d,0xb6,0xce,
  16729. 0x98,0x06,0xf6,0x6b,0x79,0x70,0xfd,0xff,
  16730. 0x86,0x17,0x18,0x7b,0xb9,0xff,0xfd,0xff,
  16731. 0x5a,0xe4,0xdf,0x3e,0xdb,0xd5,0xd3,0x5e,
  16732. 0x5b,0x4f,0x09,0x02,0x0d,0xb0,0x3e,0xab,
  16733. 0x1e,0x03,0x1d,0xda,0x2f,0xbe,0x03,0xd1,
  16734. 0x79,0x21,0x70,0xa0,0xf3,0x00,0x9c,0xee
  16735. };
  16736. WOLFSSL_SMALL_STACK_STATIC const byte oddCipher[] =
  16737. {
  16738. 0xb9,0xd7,0xcb,0x08,0xb0,0xe1,0x7b,0xa0,
  16739. 0xc2
  16740. };
  16741. #endif
  16742. /* test vector from "Recommendation for Block Cipher Modes of Operation"
  16743. * NIST Special Publication 800-38A */
  16744. #ifdef WOLFSSL_AES_192
  16745. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Key[] =
  16746. {
  16747. 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  16748. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  16749. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b
  16750. };
  16751. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Iv[] =
  16752. {
  16753. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  16754. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  16755. };
  16756. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Plain[] =
  16757. {
  16758. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  16759. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  16760. };
  16761. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Cipher[] =
  16762. {
  16763. 0x1a,0xbc,0x93,0x24,0x17,0x52,0x1c,0xa2,
  16764. 0x4f,0x2b,0x04,0x59,0xfe,0x7e,0x6e,0x0b
  16765. };
  16766. #endif /* WOLFSSL_AES_192 */
  16767. #ifdef WOLFSSL_AES_256
  16768. /* test vector from "Recommendation for Block Cipher Modes of Operation"
  16769. * NIST Special Publication 800-38A */
  16770. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Key[] =
  16771. {
  16772. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  16773. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  16774. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  16775. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  16776. };
  16777. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Iv[] =
  16778. {
  16779. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  16780. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  16781. };
  16782. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Plain[] =
  16783. {
  16784. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  16785. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  16786. };
  16787. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Cipher[] =
  16788. {
  16789. 0x60,0x1e,0xc3,0x13,0x77,0x57,0x89,0xa5,
  16790. 0xb7,0xa7,0xf5,0x04,0xbb,0xf3,0xd2,0x28
  16791. };
  16792. #endif /* WOLFSSL_AES_256 */
  16793. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16794. EVP_CIPHER_CTX *en = wolfSSL_EVP_CIPHER_CTX_new();
  16795. EVP_CIPHER_CTX *de = wolfSSL_EVP_CIPHER_CTX_new();
  16796. #else
  16797. EVP_CIPHER_CTX en[1];
  16798. EVP_CIPHER_CTX de[1];
  16799. #endif
  16800. #ifdef WOLFSSL_AES_128
  16801. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16802. EVP_CIPHER_CTX *p_en;
  16803. EVP_CIPHER_CTX *p_de;
  16804. #endif
  16805. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16806. if ((en == NULL) || (de == NULL))
  16807. return MEMORY_E;
  16808. #endif
  16809. EVP_CIPHER_CTX_init(en);
  16810. if (EVP_CipherInit(en, EVP_aes_128_ctr(),
  16811. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  16812. return -8483;
  16813. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctrPlain,
  16814. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  16815. return -8484;
  16816. EVP_CIPHER_CTX_init(de);
  16817. if (EVP_CipherInit(de, EVP_aes_128_ctr(),
  16818. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  16819. return -8485;
  16820. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff,
  16821. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  16822. return -8486;
  16823. if (XMEMCMP(cipherBuff, ctrCipher, AES_BLOCK_SIZE*4))
  16824. return -8487;
  16825. if (XMEMCMP(plainBuff, ctrPlain, AES_BLOCK_SIZE*4))
  16826. return -8488;
  16827. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16828. p_en = wolfSSL_EVP_CIPHER_CTX_new();
  16829. if (p_en == NULL)
  16830. return -8489;
  16831. p_de = wolfSSL_EVP_CIPHER_CTX_new();
  16832. if (p_de == NULL)
  16833. return -8490;
  16834. if (EVP_CipherInit(p_en, EVP_aes_128_ctr(),
  16835. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  16836. return -8491;
  16837. if (EVP_Cipher(p_en, (byte*)cipherBuff, (byte*)ctrPlain,
  16838. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  16839. return -8492;
  16840. if (EVP_CipherInit(p_de, EVP_aes_128_ctr(),
  16841. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  16842. return -8493;
  16843. if (EVP_Cipher(p_de, (byte*)plainBuff, (byte*)cipherBuff,
  16844. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  16845. return -8494;
  16846. wolfSSL_EVP_CIPHER_CTX_free(p_en);
  16847. wolfSSL_EVP_CIPHER_CTX_free(p_de);
  16848. #endif /* WOLFSSL_SMALL_STACK && !WOLFSSL_NO_MALLOC */
  16849. if (XMEMCMP(cipherBuff, ctrCipher, AES_BLOCK_SIZE*4))
  16850. return -8495;
  16851. if (XMEMCMP(plainBuff, ctrPlain, AES_BLOCK_SIZE*4))
  16852. return -8496;
  16853. EVP_CIPHER_CTX_init(en);
  16854. if (EVP_CipherInit(en, EVP_aes_128_ctr(),
  16855. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  16856. return -8497;
  16857. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctrPlain, 9) != 9)
  16858. return -8498;
  16859. EVP_CIPHER_CTX_init(de);
  16860. if (EVP_CipherInit(de, EVP_aes_128_ctr(),
  16861. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  16862. return -8499;
  16863. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff, 9) != 9)
  16864. return -8500;
  16865. if (XMEMCMP(plainBuff, ctrPlain, 9))
  16866. return -8501;
  16867. if (XMEMCMP(cipherBuff, ctrCipher, 9))
  16868. return -8502;
  16869. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctrPlain, 9) != 9)
  16870. return -8503;
  16871. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff, 9) != 9)
  16872. return -8504;
  16873. if (XMEMCMP(plainBuff, ctrPlain, 9))
  16874. return -8505;
  16875. if (XMEMCMP(cipherBuff, oddCipher, 9))
  16876. return -8506;
  16877. #endif /* WOLFSSL_AES_128 */
  16878. #ifdef WOLFSSL_AES_192
  16879. EVP_CIPHER_CTX_init(en);
  16880. if (EVP_CipherInit(en, EVP_aes_192_ctr(),
  16881. (unsigned char*)ctr192Key, (unsigned char*)ctr192Iv, 0) == 0)
  16882. return -8507;
  16883. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctr192Plain,
  16884. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  16885. return -8508;
  16886. EVP_CIPHER_CTX_init(de);
  16887. if (EVP_CipherInit(de, EVP_aes_192_ctr(),
  16888. (unsigned char*)ctr192Key, (unsigned char*)ctr192Iv, 0) == 0)
  16889. return -8509;
  16890. XMEMSET(plainBuff, 0, sizeof(plainBuff));
  16891. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff,
  16892. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  16893. return -8510;
  16894. if (XMEMCMP(plainBuff, ctr192Plain, sizeof(ctr192Plain)))
  16895. return -8511;
  16896. if (XMEMCMP(ctr192Cipher, cipherBuff, sizeof(ctr192Cipher)))
  16897. return -8512;
  16898. #endif /* WOLFSSL_AES_192 */
  16899. #ifdef WOLFSSL_AES_256
  16900. EVP_CIPHER_CTX_init(en);
  16901. if (EVP_CipherInit(en, EVP_aes_256_ctr(),
  16902. (unsigned char*)ctr256Key, (unsigned char*)ctr256Iv, 0) == 0)
  16903. return -8513;
  16904. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctr256Plain,
  16905. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  16906. return -8514;
  16907. EVP_CIPHER_CTX_init(de);
  16908. if (EVP_CipherInit(de, EVP_aes_256_ctr(),
  16909. (unsigned char*)ctr256Key, (unsigned char*)ctr256Iv, 0) == 0)
  16910. return -8515;
  16911. XMEMSET(plainBuff, 0, sizeof(plainBuff));
  16912. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff,
  16913. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  16914. return -8516;
  16915. if (XMEMCMP(plainBuff, ctr256Plain, sizeof(ctr256Plain)))
  16916. return -8517;
  16917. if (XMEMCMP(ctr256Cipher, cipherBuff, sizeof(ctr256Cipher)))
  16918. return -8518;
  16919. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16920. wolfSSL_EVP_CIPHER_CTX_free(en);
  16921. wolfSSL_EVP_CIPHER_CTX_free(de);
  16922. #endif
  16923. #endif /* WOLFSSL_AES_256 */
  16924. }
  16925. #endif /* HAVE_AES_COUNTER */
  16926. #if defined(WOLFSSL_AES_CFB) && defined(WOLFSSL_AES_128)
  16927. {
  16928. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16929. AES_KEY *enc = (AES_KEY *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  16930. AES_KEY *dec = (AES_KEY *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  16931. #else
  16932. AES_KEY enc[1];
  16933. AES_KEY dec[1];
  16934. #endif
  16935. WOLFSSL_SMALL_STACK_STATIC const byte setIv[] = {
  16936. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  16937. 0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f
  16938. };
  16939. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  16940. {
  16941. 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,
  16942. 0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c
  16943. };
  16944. WOLFSSL_SMALL_STACK_STATIC const byte cipher1[] =
  16945. {
  16946. 0x3b,0x3f,0xd9,0x2e,0xb7,0x2d,0xad,0x20,
  16947. 0x33,0x34,0x49,0xf8,0xe8,0x3c,0xfb,0x4a,
  16948. 0xc8,0xa6,0x45,0x37,0xa0,0xb3,0xa9,0x3f,
  16949. 0xcd,0xe3,0xcd,0xad,0x9f,0x1c,0xe5,0x8b
  16950. };
  16951. WOLFSSL_SMALL_STACK_STATIC const byte msg[] =
  16952. {
  16953. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  16954. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  16955. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  16956. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51
  16957. };
  16958. byte cipher[AES_BLOCK_SIZE * 2];
  16959. byte iv[AES_BLOCK_SIZE]; /* iv buffer is updeated by API */
  16960. int num = 0;
  16961. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16962. if ((enc == NULL) || (dec == NULL))
  16963. return MEMORY_E;
  16964. #endif
  16965. XMEMCPY(iv, setIv, sizeof(setIv));
  16966. wolfSSL_AES_set_encrypt_key(key, sizeof(key) * 8, enc);
  16967. wolfSSL_AES_set_encrypt_key(key, sizeof(key) * 8, dec);
  16968. wolfSSL_AES_cfb128_encrypt(msg, cipher, AES_BLOCK_SIZE - 1, enc, iv,
  16969. &num, AES_ENCRYPT);
  16970. if (XMEMCMP(cipher, cipher1, AES_BLOCK_SIZE - 1))
  16971. return -8519;
  16972. if (num != 15) /* should have used 15 of the 16 bytes */
  16973. return -8520;
  16974. wolfSSL_AES_cfb128_encrypt(msg + AES_BLOCK_SIZE - 1,
  16975. cipher + AES_BLOCK_SIZE - 1, AES_BLOCK_SIZE + 1, enc, iv,
  16976. &num, AES_ENCRYPT);
  16977. if (XMEMCMP(cipher, cipher1, AES_BLOCK_SIZE * 2))
  16978. return -8521;
  16979. if (num != 0)
  16980. return -8522;
  16981. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16982. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  16983. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  16984. #endif
  16985. }
  16986. #endif /* WOLFSSL_AES_CFB && WOLFSSL_AES_128 */
  16987. return 0;
  16988. }
  16989. #endif /* !defined(NO_AES) && !defined(WOLFCRYPT_ONLY) */
  16990. WOLFSSL_TEST_SUBROUTINE int openssl_test(void)
  16991. {
  16992. int ret;
  16993. EVP_MD_CTX md_ctx;
  16994. testVector a, b, c, d, e, f;
  16995. byte hash[WC_SHA256_DIGEST_SIZE*2]; /* max size */
  16996. a.inLen = 0;
  16997. b.inLen = c.inLen = d.inLen = e.inLen = f.inLen = a.inLen;
  16998. (void)a;
  16999. (void)b;
  17000. (void)c;
  17001. (void)d;
  17002. (void)e;
  17003. (void)f;
  17004. /* test malloc / free , 10 is an arbitrary amount of memory chosen */
  17005. {
  17006. byte* p;
  17007. p = (byte*)CRYPTO_malloc(10, "", 0);
  17008. if (p == NULL) {
  17009. return -8600;
  17010. }
  17011. XMEMSET(p, 0, 10);
  17012. CRYPTO_free(p, "", 0);
  17013. }
  17014. #ifndef NO_MD5
  17015. a.input = "1234567890123456789012345678901234567890123456789012345678"
  17016. "9012345678901234567890";
  17017. a.output = "\x57\xed\xf4\xa2\x2b\xe3\xc9\x55\xac\x49\xda\x2e\x21\x07\xb6"
  17018. "\x7a";
  17019. a.inLen = XSTRLEN(a.input);
  17020. a.outLen = WC_MD5_DIGEST_SIZE;
  17021. EVP_MD_CTX_init(&md_ctx);
  17022. ret = EVP_DigestInit(&md_ctx, EVP_md5());
  17023. if (ret == WOLFSSL_SUCCESS) {
  17024. ret = EVP_DigestUpdate(&md_ctx, a.input, (unsigned long)a.inLen);
  17025. }
  17026. if (ret == WOLFSSL_SUCCESS) {
  17027. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  17028. }
  17029. EVP_MD_CTX_cleanup(&md_ctx);
  17030. if (ret != WOLFSSL_SUCCESS)
  17031. return -18601;
  17032. if (XMEMCMP(hash, a.output, WC_MD5_DIGEST_SIZE) != 0)
  17033. return -8601;
  17034. #endif /* NO_MD5 */
  17035. #ifndef NO_SHA
  17036. b.input = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  17037. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  17038. "aaaaaaaaaa";
  17039. b.output = "\xAD\x5B\x3F\xDB\xCB\x52\x67\x78\xC2\x83\x9D\x2F\x15\x1E\xA7"
  17040. "\x53\x99\x5E\x26\xA0";
  17041. b.inLen = XSTRLEN(b.input);
  17042. b.outLen = WC_SHA_DIGEST_SIZE;
  17043. EVP_MD_CTX_init(&md_ctx);
  17044. ret = EVP_DigestInit(&md_ctx, EVP_sha1());
  17045. if (ret == WOLFSSL_SUCCESS) {
  17046. ret = EVP_DigestUpdate(&md_ctx, b.input, (unsigned long)b.inLen);
  17047. if (ret == WOLFSSL_SUCCESS)
  17048. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  17049. }
  17050. EVP_MD_CTX_cleanup(&md_ctx);
  17051. if (ret != WOLFSSL_SUCCESS)
  17052. return -18602;
  17053. if (XMEMCMP(hash, b.output, WC_SHA_DIGEST_SIZE) != 0)
  17054. return -8602;
  17055. #endif /* NO_SHA */
  17056. #ifdef WOLFSSL_SHA224
  17057. e.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  17058. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  17059. e.output = "\xc9\x7c\xa9\xa5\x59\x85\x0c\xe9\x7a\x04\xa9\x6d\xef\x6d\x99"
  17060. "\xa9\xe0\xe0\xe2\xab\x14\xe6\xb8\xdf\x26\x5f\xc0\xb3";
  17061. e.inLen = XSTRLEN(e.input);
  17062. e.outLen = WC_SHA224_DIGEST_SIZE;
  17063. EVP_MD_CTX_init(&md_ctx);
  17064. ret = EVP_DigestInit(&md_ctx, EVP_sha224());
  17065. if (ret == WOLFSSL_SUCCESS) {
  17066. ret = EVP_DigestUpdate(&md_ctx, e.input, (unsigned long)e.inLen);
  17067. if (ret == WOLFSSL_SUCCESS)
  17068. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  17069. }
  17070. EVP_MD_CTX_cleanup(&md_ctx);
  17071. if (ret != WOLFSSL_SUCCESS ||
  17072. XMEMCMP(hash, e.output, WC_SHA224_DIGEST_SIZE) != 0) {
  17073. return -8603;
  17074. }
  17075. #endif /* WOLFSSL_SHA224 */
  17076. #ifndef NO_SHA256
  17077. d.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  17078. d.output = "\x24\x8D\x6A\x61\xD2\x06\x38\xB8\xE5\xC0\x26\x93\x0C\x3E\x60"
  17079. "\x39\xA3\x3C\xE4\x59\x64\xFF\x21\x67\xF6\xEC\xED\xD4\x19\xDB"
  17080. "\x06\xC1";
  17081. d.inLen = XSTRLEN(d.input);
  17082. d.outLen = WC_SHA256_DIGEST_SIZE;
  17083. EVP_MD_CTX_init(&md_ctx);
  17084. ret = EVP_DigestInit(&md_ctx, EVP_sha256());
  17085. if (ret == WOLFSSL_SUCCESS) {
  17086. ret = EVP_DigestUpdate(&md_ctx, d.input, (unsigned long)d.inLen);
  17087. if (ret == WOLFSSL_SUCCESS)
  17088. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  17089. }
  17090. EVP_MD_CTX_cleanup(&md_ctx);
  17091. if (ret != WOLFSSL_SUCCESS ||
  17092. XMEMCMP(hash, d.output, WC_SHA256_DIGEST_SIZE) != 0) {
  17093. return -8604;
  17094. }
  17095. #endif /* !NO_SHA256 */
  17096. #ifdef WOLFSSL_SHA384
  17097. e.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  17098. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  17099. e.output = "\x09\x33\x0c\x33\xf7\x11\x47\xe8\x3d\x19\x2f\xc7\x82\xcd\x1b"
  17100. "\x47\x53\x11\x1b\x17\x3b\x3b\x05\xd2\x2f\xa0\x80\x86\xe3\xb0"
  17101. "\xf7\x12\xfc\xc7\xc7\x1a\x55\x7e\x2d\xb9\x66\xc3\xe9\xfa\x91"
  17102. "\x74\x60\x39";
  17103. e.inLen = XSTRLEN(e.input);
  17104. e.outLen = WC_SHA384_DIGEST_SIZE;
  17105. EVP_MD_CTX_init(&md_ctx);
  17106. ret = EVP_DigestInit(&md_ctx, EVP_sha384());
  17107. if (ret == WOLFSSL_SUCCESS) {
  17108. ret = EVP_DigestUpdate(&md_ctx, e.input, (unsigned long)e.inLen);
  17109. if (ret == WOLFSSL_SUCCESS)
  17110. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  17111. }
  17112. EVP_MD_CTX_cleanup(&md_ctx);
  17113. if (ret != WOLFSSL_SUCCESS ||
  17114. XMEMCMP(hash, e.output, WC_SHA384_DIGEST_SIZE) != 0) {
  17115. return -8605;
  17116. }
  17117. #endif /* WOLFSSL_SHA384 */
  17118. #ifdef WOLFSSL_SHA512
  17119. f.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  17120. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  17121. f.output = "\x8e\x95\x9b\x75\xda\xe3\x13\xda\x8c\xf4\xf7\x28\x14\xfc\x14"
  17122. "\x3f\x8f\x77\x79\xc6\xeb\x9f\x7f\xa1\x72\x99\xae\xad\xb6\x88"
  17123. "\x90\x18\x50\x1d\x28\x9e\x49\x00\xf7\xe4\x33\x1b\x99\xde\xc4"
  17124. "\xb5\x43\x3a\xc7\xd3\x29\xee\xb6\xdd\x26\x54\x5e\x96\xe5\x5b"
  17125. "\x87\x4b\xe9\x09";
  17126. f.inLen = XSTRLEN(f.input);
  17127. f.outLen = WC_SHA512_DIGEST_SIZE;
  17128. EVP_MD_CTX_init(&md_ctx);
  17129. ret = EVP_DigestInit(&md_ctx, EVP_sha512());
  17130. if (ret == WOLFSSL_SUCCESS) {
  17131. ret = EVP_DigestUpdate(&md_ctx, f.input, (unsigned long)f.inLen);
  17132. if (ret == WOLFSSL_SUCCESS)
  17133. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  17134. }
  17135. EVP_MD_CTX_cleanup(&md_ctx);
  17136. if (ret != WOLFSSL_SUCCESS ||
  17137. XMEMCMP(hash, f.output, WC_SHA512_DIGEST_SIZE) != 0) {
  17138. return -8606;
  17139. }
  17140. #endif /* WOLFSSL_SHA512 */
  17141. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  17142. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  17143. f.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  17144. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  17145. f.output = "\x23\xfe\xc5\xbb\x94\xd6\x0b\x23\x30\x81\x92\x64\x0b\x0c\x45"
  17146. "\x33\x35\xd6\x64\x73\x4f\xe4\x0e\x72\x68\x67\x4a\xf9";
  17147. f.inLen = XSTRLEN(f.input);
  17148. f.outLen = WC_SHA512_224_DIGEST_SIZE;
  17149. EVP_MD_CTX_init(&md_ctx);
  17150. ret = EVP_DigestInit(&md_ctx, EVP_sha512_224());
  17151. if (ret == WOLFSSL_SUCCESS) {
  17152. ret = EVP_DigestUpdate(&md_ctx, f.input, (unsigned long)f.inLen);
  17153. if (ret == WOLFSSL_SUCCESS)
  17154. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  17155. }
  17156. EVP_MD_CTX_cleanup(&md_ctx);
  17157. if (ret != WOLFSSL_SUCCESS ||
  17158. XMEMCMP(hash, f.output, WC_SHA512_224_DIGEST_SIZE) != 0) {
  17159. return -8722;
  17160. }
  17161. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_224 */
  17162. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  17163. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  17164. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  17165. f.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  17166. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  17167. f.output = "\x39\x28\xe1\x84\xfb\x86\x90\xf8\x40\xda\x39\x88\x12\x1d\x31"
  17168. "\xbe\x65\xcb\x9d\x3e\xf8\x3e\xe6\x14\x6f\xea\xc8\x61\xe1\x9b"
  17169. "\x56\x3a";
  17170. f.inLen = XSTRLEN(f.input);
  17171. f.outLen = WC_SHA512_256_DIGEST_SIZE;
  17172. EVP_MD_CTX_init(&md_ctx);
  17173. ret = EVP_DigestInit(&md_ctx, EVP_sha512_256());
  17174. if (ret == WOLFSSL_SUCCESS) {
  17175. ret = EVP_DigestUpdate(&md_ctx, f.input, (unsigned long)f.inLen);
  17176. if (ret == WOLFSSL_SUCCESS)
  17177. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  17178. }
  17179. EVP_MD_CTX_cleanup(&md_ctx);
  17180. if (ret != WOLFSSL_SUCCESS ||
  17181. XMEMCMP(hash, f.output, WC_SHA512_256_DIGEST_SIZE) != 0) {
  17182. return -8723;
  17183. }
  17184. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_224 */
  17185. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  17186. #ifdef WOLFSSL_SHA3
  17187. #ifndef WOLFSSL_NOSHA3_224
  17188. e.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  17189. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  17190. e.output = "\x54\x3e\x68\x68\xe1\x66\x6c\x1a\x64\x36\x30\xdf\x77\x36\x7a"
  17191. "\xe5\xa6\x2a\x85\x07\x0a\x51\xc1\x4c\xbf\x66\x5c\xbc";
  17192. e.inLen = XSTRLEN(e.input);
  17193. e.outLen = WC_SHA3_224_DIGEST_SIZE;
  17194. EVP_MD_CTX_init(&md_ctx);
  17195. ret = EVP_DigestInit(&md_ctx, EVP_sha3_224());
  17196. if (ret == WOLFSSL_SUCCESS) {
  17197. ret = EVP_DigestUpdate(&md_ctx, e.input, (unsigned long)e.inLen);
  17198. if (ret == WOLFSSL_SUCCESS)
  17199. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  17200. }
  17201. EVP_MD_CTX_cleanup(&md_ctx);
  17202. if (ret != WOLFSSL_SUCCESS ||
  17203. XMEMCMP(hash, e.output, WC_SHA3_224_DIGEST_SIZE) != 0) {
  17204. return -8607;
  17205. }
  17206. #endif /* WOLFSSL_NOSHA3_224 */
  17207. #ifndef WOLFSSL_NOSHA3_256
  17208. d.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  17209. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  17210. d.output = "\x91\x6f\x60\x61\xfe\x87\x97\x41\xca\x64\x69\xb4\x39\x71\xdf"
  17211. "\xdb\x28\xb1\xa3\x2d\xc3\x6c\xb3\x25\x4e\x81\x2b\xe2\x7a\xad"
  17212. "\x1d\x18";
  17213. d.inLen = XSTRLEN(d.input);
  17214. d.outLen = WC_SHA3_256_DIGEST_SIZE;
  17215. EVP_MD_CTX_init(&md_ctx);
  17216. ret = EVP_DigestInit(&md_ctx, EVP_sha3_256());
  17217. if (ret == WOLFSSL_SUCCESS) {
  17218. ret = EVP_DigestUpdate(&md_ctx, d.input, (unsigned long)d.inLen);
  17219. if (ret == WOLFSSL_SUCCESS)
  17220. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  17221. }
  17222. EVP_MD_CTX_cleanup(&md_ctx);
  17223. if (ret != WOLFSSL_SUCCESS ||
  17224. XMEMCMP(hash, d.output, WC_SHA3_256_DIGEST_SIZE) != 0) {
  17225. return -8608;
  17226. }
  17227. #endif /* WOLFSSL_NOSHA3_256 */
  17228. e.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  17229. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  17230. e.output = "\x79\x40\x7d\x3b\x59\x16\xb5\x9c\x3e\x30\xb0\x98\x22\x97\x47"
  17231. "\x91\xc3\x13\xfb\x9e\xcc\x84\x9e\x40\x6f\x23\x59\x2d\x04\xf6"
  17232. "\x25\xdc\x8c\x70\x9b\x98\xb4\x3b\x38\x52\xb3\x37\x21\x61\x79"
  17233. "\xaa\x7f\xc7";
  17234. e.inLen = XSTRLEN(e.input);
  17235. e.outLen = WC_SHA3_384_DIGEST_SIZE;
  17236. EVP_MD_CTX_init(&md_ctx);
  17237. ret = EVP_DigestInit(&md_ctx, EVP_sha3_384());
  17238. if (ret == WOLFSSL_SUCCESS) {
  17239. ret = EVP_DigestUpdate(&md_ctx, e.input, (unsigned long)e.inLen);
  17240. if (ret == WOLFSSL_SUCCESS)
  17241. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  17242. }
  17243. EVP_MD_CTX_cleanup(&md_ctx);
  17244. if (ret != WOLFSSL_SUCCESS ||
  17245. XMEMCMP(hash, e.output, WC_SHA3_384_DIGEST_SIZE) != 0) {
  17246. return -8609;
  17247. }
  17248. #ifndef WOLFSSL_NOSHA3_512
  17249. f.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  17250. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  17251. f.output = "\xaf\xeb\xb2\xef\x54\x2e\x65\x79\xc5\x0c\xad\x06\xd2\xe5\x78"
  17252. "\xf9\xf8\xdd\x68\x81\xd7\xdc\x82\x4d\x26\x36\x0f\xee\xbf\x18"
  17253. "\xa4\xfa\x73\xe3\x26\x11\x22\x94\x8e\xfc\xfd\x49\x2e\x74\xe8"
  17254. "\x2e\x21\x89\xed\x0f\xb4\x40\xd1\x87\xf3\x82\x27\x0c\xb4\x55"
  17255. "\xf2\x1d\xd1\x85";
  17256. f.inLen = XSTRLEN(f.input);
  17257. f.outLen = WC_SHA3_512_DIGEST_SIZE;
  17258. EVP_MD_CTX_init(&md_ctx);
  17259. ret = EVP_DigestInit(&md_ctx, EVP_sha3_512());
  17260. if (ret == WOLFSSL_SUCCESS) {
  17261. ret = EVP_DigestUpdate(&md_ctx, f.input, (unsigned long)f.inLen);
  17262. if (ret == WOLFSSL_SUCCESS)
  17263. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  17264. }
  17265. EVP_MD_CTX_cleanup(&md_ctx);
  17266. if (ret != WOLFSSL_SUCCESS ||
  17267. XMEMCMP(hash, f.output, WC_SHA3_512_DIGEST_SIZE) != 0) {
  17268. return -8610;
  17269. }
  17270. #endif /* WOLFSSL_NOSHA3_512 */
  17271. #endif /* WOLFSSL_SHA3 */
  17272. #ifndef WC_NO_RNG
  17273. if (RAND_bytes(hash, sizeof(hash)) != WOLFSSL_SUCCESS)
  17274. return -8611;
  17275. #endif
  17276. #ifndef NO_MD5
  17277. c.input = "what do ya want for nothing?";
  17278. c.output = "\x55\x78\xe8\x48\x4b\xcc\x93\x80\x93\xec\x53\xaf\x22\xd6\x14"
  17279. "\x76";
  17280. c.inLen = XSTRLEN(c.input);
  17281. c.outLen = WC_MD5_DIGEST_SIZE;
  17282. #if defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)
  17283. /* Expect failure with MD5 + HMAC when using FIPS 140-3. */
  17284. if (HMAC(EVP_md5(), "JefeJefeJefeJefe", 16, (byte*)c.input, (int)c.inLen,
  17285. hash, 0) != NULL)
  17286. #else
  17287. if (HMAC(EVP_md5(), "JefeJefeJefeJefe", 16, (byte*)c.input, (int)c.inLen,
  17288. hash, 0) == NULL ||
  17289. XMEMCMP(hash, c.output, WC_MD5_DIGEST_SIZE) != 0)
  17290. #endif
  17291. {
  17292. return -8612;
  17293. }
  17294. #endif /* NO_MD5 */
  17295. #ifndef NO_DES3
  17296. { /* des test */
  17297. WOLFSSL_SMALL_STACK_STATIC const byte vector[] = { /* "now is the time for all " w/o trailing 0 */
  17298. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  17299. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  17300. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  17301. };
  17302. byte plain[24];
  17303. byte cipher[24];
  17304. const_DES_cblock key = {
  17305. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
  17306. };
  17307. DES_cblock iv = {
  17308. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef
  17309. };
  17310. DES_key_schedule sched;
  17311. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  17312. 0x8b,0x7c,0x52,0xb0,0x01,0x2b,0x6c,0xb8,
  17313. 0x4f,0x0f,0xeb,0xf3,0xfb,0x5f,0x86,0x73,
  17314. 0x15,0x85,0xb3,0x22,0x4b,0x86,0x2b,0x4b
  17315. };
  17316. DES_key_sched(&key, &sched);
  17317. DES_cbc_encrypt(vector, cipher, sizeof(vector), &sched, &iv, DES_ENCRYPT);
  17318. DES_cbc_encrypt(cipher, plain, sizeof(vector), &sched, &iv, DES_DECRYPT);
  17319. if (XMEMCMP(plain, vector, sizeof(vector)) != 0)
  17320. return -8613;
  17321. if (XMEMCMP(cipher, verify, sizeof(verify)) != 0)
  17322. return -8614;
  17323. /* test changing iv */
  17324. DES_ncbc_encrypt(vector, cipher, 8, &sched, &iv, DES_ENCRYPT);
  17325. DES_ncbc_encrypt(vector + 8, cipher + 8, 16, &sched, &iv, DES_ENCRYPT);
  17326. if (XMEMCMP(cipher, verify, sizeof(verify)) != 0)
  17327. return -8615;
  17328. } /* end des test */
  17329. #endif /* NO_DES3 */
  17330. #if !defined(NO_AES) && !defined(WOLFCRYPT_ONLY)
  17331. if ((ret = openssl_aes_test()) != 0) {
  17332. return ret;
  17333. }
  17334. #if defined(WOLFSSL_AES_128) && defined(HAVE_AES_CBC)
  17335. { /* evp_cipher test: EVP_aes_128_cbc */
  17336. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17337. EVP_CIPHER_CTX *ctx = wolfSSL_EVP_CIPHER_CTX_new();
  17338. #else
  17339. EVP_CIPHER_CTX ctx[1];
  17340. #endif
  17341. int idx, cipherSz, plainSz;
  17342. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = { /* "Now is the time for all " w/o trailing 0 */
  17343. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  17344. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  17345. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  17346. };
  17347. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  17348. 0x95,0x94,0x92,0x57,0x5f,0x42,0x81,0x53,
  17349. 0x2c,0xcc,0x9d,0x46,0x77,0xa2,0x33,0xcb,
  17350. 0x3b,0x5d,0x41,0x97,0x94,0x25,0xa4,0xb4,
  17351. 0xae,0x7b,0x34,0xd0,0x3f,0x0c,0xbc,0x06
  17352. };
  17353. WOLFSSL_SMALL_STACK_STATIC const byte verify2[] = {
  17354. 0x95,0x94,0x92,0x57,0x5f,0x42,0x81,0x53,
  17355. 0x2c,0xcc,0x9d,0x46,0x77,0xa2,0x33,0xcb,
  17356. 0x7d,0x37,0x7b,0x0b,0x44,0xaa,0xb5,0xf0,
  17357. 0x5f,0x34,0xb4,0xde,0xb5,0xbd,0x2a,0xbb
  17358. };
  17359. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  17360. "0123456789abcdef "; /* align */
  17361. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  17362. "1234567890abcdef "; /* align */
  17363. byte cipher[AES_BLOCK_SIZE * 4];
  17364. byte plain [AES_BLOCK_SIZE * 4];
  17365. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17366. if (ctx == NULL)
  17367. return MEMORY_E;
  17368. #endif
  17369. cipherSz = 0;
  17370. EVP_CIPHER_CTX_init(ctx);
  17371. ret = EVP_CipherInit(ctx, EVP_aes_128_cbc(), key, iv, 1);
  17372. if (ret == WOLFSSL_SUCCESS) {
  17373. ret = EVP_CipherUpdate(ctx, cipher, &idx, (byte*)msg, sizeof(msg));
  17374. if (ret == WOLFSSL_SUCCESS)
  17375. cipherSz += idx;
  17376. }
  17377. if (ret == WOLFSSL_SUCCESS) {
  17378. ret = EVP_CipherFinal(ctx, cipher + cipherSz, &idx);
  17379. if (ret == WOLFSSL_SUCCESS)
  17380. cipherSz += idx;
  17381. }
  17382. EVP_CIPHER_CTX_cleanup(ctx);
  17383. if (ret != WOLFSSL_SUCCESS)
  17384. return -8617;
  17385. if (cipherSz != (int)sizeof(verify) || XMEMCMP(cipher, verify, cipherSz))
  17386. return -8618;
  17387. /* check partial decrypt (not enough padding for full block) */
  17388. plainSz = 0;
  17389. EVP_CIPHER_CTX_init(ctx);
  17390. ret = EVP_CipherInit(ctx, EVP_aes_128_cbc(), key, iv, 0);
  17391. if (ret == WOLFSSL_SUCCESS) {
  17392. ret = EVP_CipherUpdate(ctx, plain, &idx, cipher, 1);
  17393. if (ret == WOLFSSL_SUCCESS)
  17394. plainSz += idx;
  17395. }
  17396. if (ret == WOLFSSL_SUCCESS) {
  17397. /* this test should fail... not enough padding for full block */
  17398. ret = EVP_CipherFinal(ctx, plain + plainSz, &idx);
  17399. if (plainSz == 0 && ret != WOLFSSL_SUCCESS)
  17400. ret = WOLFSSL_SUCCESS;
  17401. else
  17402. ret = -8619;
  17403. }
  17404. else
  17405. ret = -8620;
  17406. EVP_CIPHER_CTX_cleanup(ctx);
  17407. if (ret != WOLFSSL_SUCCESS)
  17408. return ret;
  17409. plainSz = 0;
  17410. EVP_CIPHER_CTX_init(ctx);
  17411. ret = EVP_CipherInit(ctx, EVP_aes_128_cbc(), key, iv, 0);
  17412. if (ret == WOLFSSL_SUCCESS) {
  17413. ret = EVP_CipherUpdate(ctx, plain, &idx, cipher, cipherSz);
  17414. if (ret == WOLFSSL_SUCCESS)
  17415. plainSz += idx;
  17416. }
  17417. if (ret == WOLFSSL_SUCCESS) {
  17418. ret = EVP_CipherFinal(ctx, plain + plainSz, &idx);
  17419. if (ret == WOLFSSL_SUCCESS)
  17420. plainSz += idx;
  17421. }
  17422. EVP_CIPHER_CTX_cleanup(ctx);
  17423. if (ret != WOLFSSL_SUCCESS)
  17424. return -8621;
  17425. if (plainSz != (int)sizeof(msg) || XMEMCMP(plain, msg, sizeof(msg)))
  17426. return -8622;
  17427. cipherSz = 0;
  17428. EVP_CIPHER_CTX_init(ctx);
  17429. ret = EVP_CipherInit(ctx, EVP_aes_128_cbc(), key, iv, 1);
  17430. if (ret == WOLFSSL_SUCCESS) {
  17431. ret = EVP_CipherUpdate(ctx, cipher, &idx, msg, AES_BLOCK_SIZE);
  17432. if (ret == WOLFSSL_SUCCESS)
  17433. cipherSz += idx;
  17434. }
  17435. if (ret == WOLFSSL_SUCCESS) {
  17436. ret = EVP_CipherFinal(ctx, cipher + cipherSz, &idx);
  17437. if (ret == WOLFSSL_SUCCESS)
  17438. cipherSz += idx;
  17439. }
  17440. EVP_CIPHER_CTX_cleanup(ctx);
  17441. if (ret != WOLFSSL_SUCCESS)
  17442. return -8623;
  17443. if (cipherSz != (int)sizeof(verify2) || XMEMCMP(cipher, verify2, cipherSz))
  17444. return -8624;
  17445. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17446. wolfSSL_EVP_CIPHER_CTX_free(ctx);
  17447. #endif
  17448. } /* end evp_cipher test: EVP_aes_128_cbc*/
  17449. #endif /* WOLFSSL_AES_128 && HAVE_AES_CBC */
  17450. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_256)
  17451. { /* evp_cipher test: EVP_aes_256_ecb*/
  17452. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17453. EVP_CIPHER_CTX *ctx = wolfSSL_EVP_CIPHER_CTX_new();
  17454. #else
  17455. EVP_CIPHER_CTX ctx[1];
  17456. #endif
  17457. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = {
  17458. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  17459. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  17460. };
  17461. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  17462. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  17463. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  17464. };
  17465. WOLFSSL_SMALL_STACK_STATIC const byte key[] = {
  17466. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  17467. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  17468. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  17469. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  17470. };
  17471. byte cipher[AES_BLOCK_SIZE * 4];
  17472. byte plain [AES_BLOCK_SIZE * 4];
  17473. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17474. if (ctx == NULL)
  17475. return MEMORY_E;
  17476. #endif
  17477. EVP_CIPHER_CTX_init(ctx);
  17478. ret = EVP_CipherInit(ctx, EVP_aes_256_ecb(), (unsigned char*)key, NULL, 1);
  17479. if (ret == WOLFSSL_SUCCESS)
  17480. ret = EVP_Cipher(ctx, cipher, (byte*)msg, 16);
  17481. EVP_CIPHER_CTX_cleanup(ctx);
  17482. if (ret != 16)
  17483. return -8625;
  17484. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE))
  17485. return -8626;
  17486. EVP_CIPHER_CTX_init(ctx);
  17487. ret = EVP_CipherInit(ctx, EVP_aes_256_ecb(), (unsigned char*)key, NULL, 0);
  17488. if (ret == WOLFSSL_SUCCESS)
  17489. ret = EVP_Cipher(ctx, plain, cipher, 16);
  17490. EVP_CIPHER_CTX_cleanup(ctx);
  17491. if (ret != 16)
  17492. return -8627;
  17493. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  17494. return -8628;
  17495. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17496. wolfSSL_EVP_CIPHER_CTX_free(ctx);
  17497. #endif
  17498. } /* end evp_cipher test */
  17499. #endif /* HAVE_AES_ECB && WOLFSSL_AES_128 */
  17500. #define OPENSSL_TEST_ERROR (-10000)
  17501. #if defined(WOLFSSL_AES_DIRECT) && defined(WOLFSSL_AES_256)
  17502. /* enable HAVE_AES_DECRYPT for AES_encrypt/decrypt */
  17503. {
  17504. /* Test: AES_encrypt/decrypt/set Key */
  17505. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17506. AES_KEY *enc = (AES_KEY *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  17507. #ifdef HAVE_AES_DECRYPT
  17508. AES_KEY *dec = (AES_KEY *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  17509. #endif
  17510. #else
  17511. AES_KEY enc[1];
  17512. #ifdef HAVE_AES_DECRYPT
  17513. AES_KEY dec[1];
  17514. #endif
  17515. #endif
  17516. WOLFSSL_SMALL_STACK_STATIC const byte msg[] =
  17517. {
  17518. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  17519. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  17520. };
  17521. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  17522. {
  17523. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  17524. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  17525. };
  17526. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  17527. {
  17528. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  17529. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  17530. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  17531. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  17532. };
  17533. byte plain[sizeof(msg)];
  17534. byte cipher[sizeof(msg)];
  17535. printf("openSSL extra test\n") ;
  17536. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17537. if (enc == NULL)
  17538. return MEMORY_E;
  17539. #ifdef HAVE_AES_DECRYPT
  17540. if (dec == NULL)
  17541. return MEMORY_E;
  17542. #endif
  17543. #endif
  17544. AES_set_encrypt_key(key, sizeof(key)*8, enc);
  17545. AES_set_decrypt_key(key, sizeof(key)*8, dec);
  17546. AES_encrypt(msg, cipher, enc);
  17547. #ifdef HAVE_AES_DECRYPT
  17548. AES_decrypt(cipher, plain, dec);
  17549. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  17550. return OPENSSL_TEST_ERROR-60;
  17551. #endif /* HAVE_AES_DECRYPT */
  17552. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE))
  17553. return OPENSSL_TEST_ERROR-61;
  17554. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17555. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  17556. #ifdef HAVE_AES_DECRYPT
  17557. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  17558. #endif
  17559. #endif
  17560. }
  17561. #endif /* WOLFSSL_AES_DIRECT && WOLFSSL_AES_256 */
  17562. /* EVP_Cipher with EVP_aes_xxx_ctr() */
  17563. #ifdef WOLFSSL_AES_COUNTER
  17564. {
  17565. byte plainBuff [64];
  17566. byte cipherBuff[64];
  17567. #ifdef WOLFSSL_AES_128
  17568. WOLFSSL_SMALL_STACK_STATIC const byte ctrKey[] =
  17569. {
  17570. 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,
  17571. 0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c
  17572. };
  17573. WOLFSSL_SMALL_STACK_STATIC const byte ctrIv[] =
  17574. {
  17575. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  17576. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  17577. };
  17578. WOLFSSL_SMALL_STACK_STATIC const byte ctrPlain[] =
  17579. {
  17580. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  17581. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  17582. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  17583. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  17584. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  17585. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  17586. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  17587. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  17588. };
  17589. WOLFSSL_SMALL_STACK_STATIC const byte ctrCipher[] =
  17590. {
  17591. 0x87,0x4d,0x61,0x91,0xb6,0x20,0xe3,0x26,
  17592. 0x1b,0xef,0x68,0x64,0x99,0x0d,0xb6,0xce,
  17593. 0x98,0x06,0xf6,0x6b,0x79,0x70,0xfd,0xff,
  17594. 0x86,0x17,0x18,0x7b,0xb9,0xff,0xfd,0xff,
  17595. 0x5a,0xe4,0xdf,0x3e,0xdb,0xd5,0xd3,0x5e,
  17596. 0x5b,0x4f,0x09,0x02,0x0d,0xb0,0x3e,0xab,
  17597. 0x1e,0x03,0x1d,0xda,0x2f,0xbe,0x03,0xd1,
  17598. 0x79,0x21,0x70,0xa0,0xf3,0x00,0x9c,0xee
  17599. };
  17600. WOLFSSL_SMALL_STACK_STATIC const byte oddCipher[] =
  17601. {
  17602. 0xb9,0xd7,0xcb,0x08,0xb0,0xe1,0x7b,0xa0,
  17603. 0xc2
  17604. };
  17605. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17606. EVP_CIPHER_CTX *p_en;
  17607. EVP_CIPHER_CTX *p_de;
  17608. #endif
  17609. #endif /* WOLFSSL_AES_128 */
  17610. #ifdef WOLFSSL_AES_192
  17611. /* test vector from "Recommendation for Block Cipher Modes of Operation"
  17612. * NIST Special Publication 800-38A */
  17613. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Key[] =
  17614. {
  17615. 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  17616. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  17617. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b
  17618. };
  17619. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Iv[] =
  17620. {
  17621. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  17622. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  17623. };
  17624. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Plain[] =
  17625. {
  17626. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  17627. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  17628. };
  17629. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Cipher[] =
  17630. {
  17631. 0x1a,0xbc,0x93,0x24,0x17,0x52,0x1c,0xa2,
  17632. 0x4f,0x2b,0x04,0x59,0xfe,0x7e,0x6e,0x0b
  17633. };
  17634. #endif /* WOLFSSL_AES_192 */
  17635. #ifdef WOLFSSL_AES_256
  17636. /* test vector from "Recommendation for Block Cipher Modes of Operation"
  17637. * NIST Special Publication 800-38A */
  17638. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Key[] =
  17639. {
  17640. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  17641. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  17642. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  17643. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  17644. };
  17645. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Iv[] =
  17646. {
  17647. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  17648. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  17649. };
  17650. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Plain[] =
  17651. {
  17652. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  17653. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  17654. };
  17655. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Cipher[] =
  17656. {
  17657. 0x60,0x1e,0xc3,0x13,0x77,0x57,0x89,0xa5,
  17658. 0xb7,0xa7,0xf5,0x04,0xbb,0xf3,0xd2,0x28
  17659. };
  17660. #endif /* WOLFSSL_AES_256 */
  17661. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17662. EVP_CIPHER_CTX *en = wolfSSL_EVP_CIPHER_CTX_new();
  17663. EVP_CIPHER_CTX *de = wolfSSL_EVP_CIPHER_CTX_new();
  17664. if ((en == NULL) || (de == NULL))
  17665. return MEMORY_E;
  17666. #else
  17667. EVP_CIPHER_CTX en[1];
  17668. EVP_CIPHER_CTX de[1];
  17669. #endif
  17670. #ifdef WOLFSSL_AES_128
  17671. EVP_CIPHER_CTX_init(en);
  17672. if (EVP_CipherInit(en, EVP_aes_128_ctr(),
  17673. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  17674. return -8629;
  17675. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctrPlain,
  17676. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  17677. return -8630;
  17678. EVP_CIPHER_CTX_init(de);
  17679. if (EVP_CipherInit(de, EVP_aes_128_ctr(),
  17680. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  17681. return -8631;
  17682. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff,
  17683. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  17684. return -8632;
  17685. if (XMEMCMP(cipherBuff, ctrCipher, AES_BLOCK_SIZE*4))
  17686. return -8633;
  17687. if (XMEMCMP(plainBuff, ctrPlain, AES_BLOCK_SIZE*4))
  17688. return -8634;
  17689. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17690. p_en = wolfSSL_EVP_CIPHER_CTX_new();
  17691. if (p_en == NULL)
  17692. return -8635;
  17693. p_de = wolfSSL_EVP_CIPHER_CTX_new();
  17694. if (p_de == NULL)
  17695. return -8636;
  17696. if (EVP_CipherInit(p_en, EVP_aes_128_ctr(),
  17697. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  17698. return -8637;
  17699. if (EVP_Cipher(p_en, (byte*)cipherBuff, (byte*)ctrPlain,
  17700. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  17701. return -8638;
  17702. if (EVP_CipherInit(p_de, EVP_aes_128_ctr(),
  17703. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  17704. return -8639;
  17705. if (EVP_Cipher(p_de, (byte*)plainBuff, (byte*)cipherBuff,
  17706. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  17707. return -8640;
  17708. wolfSSL_EVP_CIPHER_CTX_free(p_en);
  17709. wolfSSL_EVP_CIPHER_CTX_free(p_de);
  17710. #endif /* WOLFSSL_SMALL_STACK && !WOLFSSL_NO_MALLOC */
  17711. if (XMEMCMP(cipherBuff, ctrCipher, AES_BLOCK_SIZE*4))
  17712. return -8641;
  17713. if (XMEMCMP(plainBuff, ctrPlain, AES_BLOCK_SIZE*4))
  17714. return -8642;
  17715. EVP_CIPHER_CTX_init(en);
  17716. if (EVP_CipherInit(en, EVP_aes_128_ctr(),
  17717. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  17718. return -8643;
  17719. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctrPlain, 9) != 9)
  17720. return -8644;
  17721. EVP_CIPHER_CTX_init(de);
  17722. if (EVP_CipherInit(de, EVP_aes_128_ctr(),
  17723. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  17724. return -8645;
  17725. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff, 9) != 9)
  17726. return -8646;
  17727. if (XMEMCMP(plainBuff, ctrPlain, 9))
  17728. return -8647;
  17729. if (XMEMCMP(cipherBuff, ctrCipher, 9))
  17730. return -8648;
  17731. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctrPlain, 9) != 9)
  17732. return -8649;
  17733. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff, 9) != 9)
  17734. return -8650;
  17735. if (XMEMCMP(plainBuff, ctrPlain, 9))
  17736. return -8651;
  17737. if (XMEMCMP(cipherBuff, oddCipher, 9))
  17738. return -8652;
  17739. #endif /* WOLFSSL_AES_128 */
  17740. #ifdef WOLFSSL_AES_192
  17741. EVP_CIPHER_CTX_init(en);
  17742. if (EVP_CipherInit(en, EVP_aes_192_ctr(),
  17743. (unsigned char*)ctr192Key, (unsigned char*)ctr192Iv, 0) == 0)
  17744. return -8653;
  17745. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctr192Plain,
  17746. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  17747. return -8654;
  17748. EVP_CIPHER_CTX_init(de);
  17749. if (EVP_CipherInit(de, EVP_aes_192_ctr(),
  17750. (unsigned char*)ctr192Key, (unsigned char*)ctr192Iv, 0) == 0)
  17751. return -8655;
  17752. XMEMSET(plainBuff, 0, sizeof(plainBuff));
  17753. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff,
  17754. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  17755. return -8656;
  17756. if (XMEMCMP(plainBuff, ctr192Plain, sizeof(ctr192Plain)))
  17757. return -8657;
  17758. if (XMEMCMP(ctr192Cipher, cipherBuff, sizeof(ctr192Cipher)))
  17759. return -8658;
  17760. #endif /* WOLFSSL_AES_192 */
  17761. #ifdef WOLFSSL_AES_256
  17762. EVP_CIPHER_CTX_init(en);
  17763. if (EVP_CipherInit(en, EVP_aes_256_ctr(),
  17764. (unsigned char*)ctr256Key, (unsigned char*)ctr256Iv, 0) == 0)
  17765. return -8659;
  17766. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctr256Plain,
  17767. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  17768. return -8660;
  17769. EVP_CIPHER_CTX_init(de);
  17770. if (EVP_CipherInit(de, EVP_aes_256_ctr(),
  17771. (unsigned char*)ctr256Key, (unsigned char*)ctr256Iv, 0) == 0)
  17772. return -8661;
  17773. XMEMSET(plainBuff, 0, sizeof(plainBuff));
  17774. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff,
  17775. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  17776. return -8662;
  17777. if (XMEMCMP(plainBuff, ctr256Plain, sizeof(ctr256Plain)))
  17778. return -8663;
  17779. if (XMEMCMP(ctr256Cipher, cipherBuff, sizeof(ctr256Cipher)))
  17780. return -8664;
  17781. #endif /* WOLFSSL_AES_256 */
  17782. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17783. wolfSSL_EVP_CIPHER_CTX_free(en);
  17784. wolfSSL_EVP_CIPHER_CTX_free(de);
  17785. #endif
  17786. }
  17787. #endif /* HAVE_AES_COUNTER */
  17788. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  17789. {
  17790. /* EVP_CipherUpdate test */
  17791. WOLFSSL_SMALL_STACK_STATIC const byte cbcPlain[] =
  17792. {
  17793. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  17794. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  17795. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  17796. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  17797. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  17798. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  17799. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  17800. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  17801. };
  17802. byte key[] = "0123456789abcdef "; /* align */
  17803. byte iv[] = "1234567890abcdef "; /* align */
  17804. byte cipher[AES_BLOCK_SIZE * 4];
  17805. byte plain [AES_BLOCK_SIZE * 4];
  17806. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17807. EVP_CIPHER_CTX *en = wolfSSL_EVP_CIPHER_CTX_new();
  17808. EVP_CIPHER_CTX *de = wolfSSL_EVP_CIPHER_CTX_new();
  17809. #else
  17810. EVP_CIPHER_CTX en[1];
  17811. EVP_CIPHER_CTX de[1];
  17812. #endif
  17813. int outlen ;
  17814. int total = 0;
  17815. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17816. if ((en == NULL) || (de == NULL))
  17817. return MEMORY_E;
  17818. #endif
  17819. EVP_CIPHER_CTX_init(en);
  17820. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  17821. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  17822. return -8665;
  17823. /* openSSL compatibility, if(inlen == 0)return 1; */
  17824. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen,
  17825. (byte*)cbcPlain, 0) != 1)
  17826. return -8666;
  17827. EVP_CIPHER_CTX_init(en);
  17828. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  17829. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  17830. return -8667;
  17831. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen,
  17832. (byte*)cbcPlain, 9) == 0)
  17833. return -8668;
  17834. if(outlen != 0)
  17835. return -8669;
  17836. total += outlen;
  17837. if (EVP_CipherUpdate(en, (byte*)&cipher[total], &outlen,
  17838. (byte*)&cbcPlain[9] , 9) == 0)
  17839. return -8670;
  17840. if(outlen != 16)
  17841. return -8671;
  17842. total += outlen;
  17843. if (EVP_CipherFinal(en, (byte*)&cipher[total], &outlen) == 0)
  17844. return -8672;
  17845. if(outlen != 16)
  17846. return -8673;
  17847. total += outlen;
  17848. if(total != 32)
  17849. return -8674;
  17850. total = 0;
  17851. EVP_CIPHER_CTX_init(de);
  17852. if (EVP_CipherInit(de, EVP_aes_128_cbc(),
  17853. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  17854. return -8675;
  17855. if (EVP_CipherUpdate(de, (byte*)plain, &outlen, (byte*)cipher, 6) == 0)
  17856. return -8676;
  17857. if(outlen != 0)
  17858. return -8677;
  17859. total += outlen;
  17860. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen,
  17861. (byte*)&cipher[6], 12) == 0)
  17862. return -8678;
  17863. if(outlen != 0)
  17864. total += outlen;
  17865. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen,
  17866. (byte*)&cipher[6+12], 14) == 0)
  17867. return -8679;
  17868. if(outlen != 16)
  17869. return -8680;
  17870. total += outlen;
  17871. if (EVP_CipherFinal(de, (byte*)&plain[total], &outlen) == 0)
  17872. return -8681;
  17873. if(outlen != 2)
  17874. return -8682;
  17875. total += outlen;
  17876. if(total != 18)
  17877. return -8683;
  17878. if (XMEMCMP(plain, cbcPlain, 18))
  17879. return -8684;
  17880. total = 0;
  17881. EVP_CIPHER_CTX_init(en);
  17882. if (EVP_EncryptInit(en, EVP_aes_128_cbc(),
  17883. (unsigned char*)key, (unsigned char*)iv) == 0)
  17884. return -8685;
  17885. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen, (byte*)cbcPlain, 9) == 0)
  17886. return -8686;
  17887. if(outlen != 0)
  17888. return -8687;
  17889. total += outlen;
  17890. if (EVP_CipherUpdate(en, (byte*)&cipher[total], &outlen, (byte*)&cbcPlain[9] , 9) == 0)
  17891. return -8688;
  17892. if(outlen != 16)
  17893. return -8689;
  17894. total += outlen;
  17895. if (EVP_EncryptFinal(en, (byte*)&cipher[total], &outlen) == 0)
  17896. return -8690;
  17897. if(outlen != 16)
  17898. return -8691;
  17899. total += outlen;
  17900. if(total != 32)
  17901. return 3438;
  17902. total = 0;
  17903. EVP_CIPHER_CTX_init(de);
  17904. if (EVP_DecryptInit(de, EVP_aes_128_cbc(),
  17905. (unsigned char*)key, (unsigned char*)iv) == 0)
  17906. return -8692;
  17907. if (EVP_CipherUpdate(de, (byte*)plain, &outlen, (byte*)cipher, 6) == 0)
  17908. return -8693;
  17909. if(outlen != 0)
  17910. return -8694;
  17911. total += outlen;
  17912. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen, (byte*)&cipher[6], 12) == 0)
  17913. return -8695;
  17914. if(outlen != 0)
  17915. total += outlen;
  17916. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen, (byte*)&cipher[6+12], 14) == 0)
  17917. return -8696;
  17918. if(outlen != 16)
  17919. return -8697;
  17920. total += outlen;
  17921. if (EVP_DecryptFinal(de, (byte*)&plain[total], &outlen) == 0)
  17922. return -8698;
  17923. if(outlen != 2)
  17924. return -8699;
  17925. total += outlen;
  17926. if(total != 18)
  17927. return 3447;
  17928. if (XMEMCMP(plain, cbcPlain, 18))
  17929. return -8700;
  17930. if (EVP_CIPHER_key_length(NULL) != 0)
  17931. return -8701;
  17932. if (EVP_CIPHER_key_length(EVP_aes_128_cbc()) != 16)
  17933. return -8702;
  17934. if (EVP_CIPHER_CTX_mode(NULL) != 0)
  17935. return -8703;
  17936. if (EVP_CIPHER_CTX_mode(en) != (en->flags & WOLFSSL_EVP_CIPH_MODE))
  17937. return -8704;
  17938. EVP_CIPHER_CTX_init(en);
  17939. if (EVP_CipherInit_ex(en, EVP_aes_128_cbc(), NULL,
  17940. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  17941. return -8705;
  17942. EVP_CIPHER_CTX_init(en);
  17943. if (EVP_EncryptInit_ex(en, EVP_aes_128_cbc(), NULL,
  17944. (unsigned char*)key, (unsigned char*)iv) == 0)
  17945. return -8706;
  17946. if (wolfSSL_EVP_EncryptFinal_ex(NULL, NULL, NULL) != WOLFSSL_FAILURE)
  17947. return -8707;
  17948. if (wolfSSL_EVP_EncryptFinal(NULL, NULL, NULL) != WOLFSSL_FAILURE)
  17949. return -8708;
  17950. EVP_CIPHER_CTX_init(de);
  17951. if (EVP_DecryptInit_ex(de, EVP_aes_128_cbc(), NULL,
  17952. (unsigned char*)key, (unsigned char*)iv) == 0)
  17953. return -8709;
  17954. if (wolfSSL_EVP_DecryptFinal(NULL, NULL, NULL) != WOLFSSL_FAILURE)
  17955. return -8710;
  17956. if (wolfSSL_EVP_DecryptFinal_ex(NULL, NULL, NULL) != WOLFSSL_FAILURE)
  17957. return -8711;
  17958. if (EVP_CIPHER_CTX_block_size(NULL) != BAD_FUNC_ARG)
  17959. return -8712;
  17960. EVP_CIPHER_CTX_init(en);
  17961. EVP_EncryptInit_ex(en, EVP_aes_128_cbc(), NULL,
  17962. (unsigned char*)key, (unsigned char*)iv);
  17963. if (EVP_CIPHER_CTX_block_size(en) != en->block_size)
  17964. return -8713;
  17965. if (EVP_CIPHER_block_size(NULL) != BAD_FUNC_ARG)
  17966. return -8714;
  17967. if (EVP_CIPHER_block_size(EVP_aes_128_cbc()) != AES_BLOCK_SIZE)
  17968. return -8715;
  17969. if (WOLFSSL_EVP_CIPHER_mode(NULL) != 0)
  17970. return -8716;
  17971. if (EVP_CIPHER_flags(EVP_aes_128_cbc()) != WOLFSSL_EVP_CIPH_CBC_MODE)
  17972. return -8717;
  17973. EVP_CIPHER_CTX_clear_flags(en, 0xFFFFFFFF);
  17974. EVP_CIPHER_CTX_set_flags(en, 42);
  17975. if (en->flags != 42)
  17976. return -8718;
  17977. if (EVP_CIPHER_CTX_set_padding(NULL, 0) != BAD_FUNC_ARG)
  17978. return -8719;
  17979. if (EVP_CIPHER_CTX_set_padding(en, 0) != WOLFSSL_SUCCESS)
  17980. return -8720;
  17981. if (EVP_CIPHER_CTX_set_padding(en, 1) != WOLFSSL_SUCCESS)
  17982. return -8721;
  17983. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17984. wolfSSL_EVP_CIPHER_CTX_free(en);
  17985. wolfSSL_EVP_CIPHER_CTX_free(de);
  17986. #endif
  17987. }
  17988. #endif /* WOLFSSL_AES_128 && HAVE_AES_CBC */
  17989. #endif /* ifndef NO_AES */
  17990. return 0;
  17991. }
  17992. WOLFSSL_TEST_SUBROUTINE int openSSL_evpMD_test(void)
  17993. {
  17994. int ret = 0;
  17995. #if !defined(NO_SHA256) && !defined(NO_SHA)
  17996. WOLFSSL_EVP_MD_CTX* ctx;
  17997. WOLFSSL_EVP_MD_CTX* ctx2;
  17998. ctx = EVP_MD_CTX_create();
  17999. ctx2 = EVP_MD_CTX_create();
  18000. ret = EVP_DigestInit(ctx, EVP_sha256());
  18001. if (ret != SSL_SUCCESS) {
  18002. ret = -8800;
  18003. goto openSSL_evpMD_test_done;
  18004. }
  18005. ret = EVP_MD_CTX_copy(ctx2, ctx);
  18006. if (ret != SSL_SUCCESS) {
  18007. ret = -8801;
  18008. goto openSSL_evpMD_test_done;
  18009. }
  18010. if (EVP_MD_type(EVP_sha256()) != EVP_MD_CTX_type(ctx2)) {
  18011. ret = -8802;
  18012. goto openSSL_evpMD_test_done;
  18013. }
  18014. ret = EVP_DigestInit(ctx, EVP_sha1());
  18015. if (ret != SSL_SUCCESS) {
  18016. ret = -8803;
  18017. goto openSSL_evpMD_test_done;
  18018. }
  18019. if (EVP_MD_type(EVP_sha256()) != EVP_MD_CTX_type(ctx2)) {
  18020. ret = -8804;
  18021. goto openSSL_evpMD_test_done;
  18022. }
  18023. ret = EVP_MD_CTX_copy_ex(ctx2, ctx);
  18024. if (ret != SSL_SUCCESS) {
  18025. ret = -8805;
  18026. goto openSSL_evpMD_test_done;
  18027. }
  18028. if (EVP_MD_type(EVP_sha256()) == EVP_MD_CTX_type(ctx2)) {
  18029. ret = -8806;
  18030. goto openSSL_evpMD_test_done;
  18031. }
  18032. if (EVP_MD_type(EVP_sha1()) != EVP_MD_CTX_type(ctx2)) {
  18033. ret = -8807;
  18034. goto openSSL_evpMD_test_done;
  18035. }
  18036. if (EVP_DigestInit_ex(ctx, EVP_sha1(), NULL) != SSL_SUCCESS) {
  18037. ret = -8808;
  18038. goto openSSL_evpMD_test_done;
  18039. }
  18040. if (EVP_add_digest(NULL) != 0) {
  18041. ret = -8809;
  18042. goto openSSL_evpMD_test_done;
  18043. }
  18044. if (wolfSSL_EVP_add_cipher(NULL) != 0) {
  18045. ret = -8810;
  18046. goto openSSL_evpMD_test_done;
  18047. }
  18048. ret = 0; /* got to success state without jumping to end with a fail */
  18049. openSSL_evpMD_test_done:
  18050. EVP_MD_CTX_destroy(ctx);
  18051. EVP_MD_CTX_destroy(ctx2);
  18052. #endif /* NO_SHA256 */
  18053. return ret;
  18054. }
  18055. #ifdef DEBUG_SIGN
  18056. static void show(const char *title, const char *p, unsigned int s) {
  18057. char* i;
  18058. printf("%s: ", title);
  18059. for (i = p;
  18060. i < p + s;
  18061. printf("%c", *i), i++);
  18062. printf("\n");
  18063. }
  18064. #else
  18065. #define show(a,b,c)
  18066. #endif
  18067. #define FOURK_BUFF 4096
  18068. #define ERR_BASE_PKEY (-5000)
  18069. WOLFSSL_TEST_SUBROUTINE int openssl_pkey0_test(void)
  18070. {
  18071. int ret = 0;
  18072. #if !defined(NO_RSA) && !defined(HAVE_USER_RSA) && !defined(NO_SHA)
  18073. byte* prvTmp;
  18074. byte* pubTmp;
  18075. int prvBytes;
  18076. int pubBytes;
  18077. RSA *prvRsa = NULL;
  18078. RSA *pubRsa = NULL;
  18079. EVP_PKEY *prvPkey = NULL;
  18080. EVP_PKEY *pubPkey = NULL;
  18081. EVP_PKEY_CTX *enc = NULL;
  18082. EVP_PKEY_CTX *dec = NULL;
  18083. byte in[] = TEST_STRING;
  18084. byte out[256];
  18085. size_t outlen;
  18086. size_t keySz;
  18087. byte plain[256];
  18088. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  18089. XFILE keyFile;
  18090. XFILE keypubFile;
  18091. char cliKey[] = "./certs/client-key.der";
  18092. char cliKeypub[] = "./certs/client-keyPub.der";
  18093. #endif
  18094. prvTmp = (byte*)XMALLOC(FOURK_BUFF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18095. if (prvTmp == NULL)
  18096. return ERR_BASE_PKEY-1;
  18097. pubTmp = (byte*)XMALLOC(FOURK_BUFF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18098. if (pubTmp == NULL) {
  18099. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18100. return ERR_BASE_PKEY-2;
  18101. }
  18102. #ifdef USE_CERT_BUFFERS_1024
  18103. XMEMCPY(prvTmp, client_key_der_1024, sizeof_client_key_der_1024);
  18104. prvBytes = sizeof_client_key_der_1024;
  18105. XMEMCPY(pubTmp, client_keypub_der_1024, sizeof_client_keypub_der_1024);
  18106. pubBytes = sizeof_client_keypub_der_1024;
  18107. #elif defined(USE_CERT_BUFFERS_2048)
  18108. XMEMCPY(prvTmp, client_key_der_2048, sizeof_client_key_der_2048);
  18109. prvBytes = sizeof_client_key_der_2048;
  18110. XMEMCPY(pubTmp, client_keypub_der_2048, sizeof_client_keypub_der_2048);
  18111. pubBytes = sizeof_client_keypub_der_2048;
  18112. #else
  18113. keyFile = XFOPEN(cliKey, "rb");
  18114. if (!keyFile) {
  18115. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18116. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18117. err_sys("can't open ./certs/client-key.der, "
  18118. "Please run from wolfSSL home dir", ERR_BASE_PKEY-3);
  18119. return ERR_BASE_PKEY-3;
  18120. }
  18121. prvBytes = (int)XFREAD(prvTmp, 1, (int)FOURK_BUFF, keyFile);
  18122. XFCLOSE(keyFile);
  18123. keypubFile = XFOPEN(cliKeypub, "rb");
  18124. if (!keypubFile) {
  18125. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18126. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18127. err_sys("can't open ./certs/client-cert.der, "
  18128. "Please run from wolfSSL home dir", -4);
  18129. return ERR_BASE_PKEY-4;
  18130. }
  18131. pubBytes = (int)XFREAD(pubTmp, 1, (int)FOURK_BUFF, keypubFile);
  18132. XFCLOSE(keypubFile);
  18133. #endif /* USE_CERT_BUFFERS */
  18134. prvRsa = wolfSSL_RSA_new();
  18135. pubRsa = wolfSSL_RSA_new();
  18136. if((prvRsa == NULL) || (pubRsa == NULL)){
  18137. printf("error with RSA_new\n");
  18138. ret = ERR_BASE_PKEY-10;
  18139. goto openssl_pkey0_test_done;
  18140. }
  18141. ret = wolfSSL_RSA_LoadDer_ex(prvRsa, prvTmp, prvBytes, WOLFSSL_RSA_LOAD_PRIVATE);
  18142. if(ret != SSL_SUCCESS){
  18143. printf("error with RSA_LoadDer_ex\n");
  18144. ret = ERR_BASE_PKEY-11;
  18145. goto openssl_pkey0_test_done;
  18146. }
  18147. ret = wolfSSL_RSA_LoadDer_ex(pubRsa, pubTmp, pubBytes, WOLFSSL_RSA_LOAD_PUBLIC);
  18148. if(ret != SSL_SUCCESS){
  18149. printf("error with RSA_LoadDer_ex\n");
  18150. ret = ERR_BASE_PKEY-12;
  18151. goto openssl_pkey0_test_done;
  18152. }
  18153. keySz = (size_t)RSA_size(pubRsa);
  18154. prvPkey = wolfSSL_EVP_PKEY_new();
  18155. pubPkey = wolfSSL_EVP_PKEY_new();
  18156. if((prvPkey == NULL) || (pubPkey == NULL)){
  18157. printf("error with PKEY_new\n");
  18158. ret = ERR_BASE_PKEY-13;
  18159. goto openssl_pkey0_test_done;
  18160. }
  18161. ret = wolfSSL_EVP_PKEY_set1_RSA(prvPkey, prvRsa);
  18162. ret += wolfSSL_EVP_PKEY_set1_RSA(pubPkey, pubRsa);
  18163. if(ret != 2){
  18164. printf("error with PKEY_set1_RSA\n");
  18165. ret = ERR_BASE_PKEY-14;
  18166. goto openssl_pkey0_test_done;
  18167. }
  18168. dec = EVP_PKEY_CTX_new(prvPkey, NULL);
  18169. enc = EVP_PKEY_CTX_new(pubPkey, NULL);
  18170. if((dec == NULL)||(enc==NULL)){
  18171. printf("error with EVP_PKEY_CTX_new\n");
  18172. ret = ERR_BASE_PKEY-15;
  18173. goto openssl_pkey0_test_done;
  18174. }
  18175. ret = EVP_PKEY_decrypt_init(dec);
  18176. if (ret != 1) {
  18177. printf("error with decrypt init\n");
  18178. ret = ERR_BASE_PKEY-16;
  18179. goto openssl_pkey0_test_done;
  18180. }
  18181. ret = EVP_PKEY_encrypt_init(enc);
  18182. if (ret != 1) {
  18183. printf("error with encrypt init\n");
  18184. ret = ERR_BASE_PKEY-17;
  18185. goto openssl_pkey0_test_done;
  18186. }
  18187. XMEMSET(out, 0, sizeof(out));
  18188. ret = EVP_PKEY_encrypt(enc, out, &outlen, in, sizeof(in));
  18189. if (ret != 1) {
  18190. printf("error encrypting msg\n");
  18191. ret = ERR_BASE_PKEY-18;
  18192. goto openssl_pkey0_test_done;
  18193. }
  18194. show("encrypted msg", out, outlen);
  18195. XMEMSET(plain, 0, sizeof(plain));
  18196. ret = EVP_PKEY_decrypt(dec, plain, &outlen, out, keySz);
  18197. if (ret != 1) {
  18198. printf("error decrypting msg\n");
  18199. ret = ERR_BASE_PKEY-19;
  18200. goto openssl_pkey0_test_done;
  18201. }
  18202. show("decrypted msg", plain, outlen);
  18203. /* RSA_PKCS1_OAEP_PADDING test */
  18204. ret = EVP_PKEY_decrypt_init(dec);
  18205. if (ret != 1) {
  18206. printf("error with decrypt init\n");
  18207. ret = ERR_BASE_PKEY-30;
  18208. goto openssl_pkey0_test_done;
  18209. }
  18210. ret = EVP_PKEY_encrypt_init(enc);
  18211. if (ret != 1) {
  18212. printf("error with encrypt init\n");
  18213. ret = ERR_BASE_PKEY-31;
  18214. goto openssl_pkey0_test_done;
  18215. }
  18216. if (EVP_PKEY_CTX_set_rsa_padding(dec, RSA_PKCS1_PADDING) <= 0) {
  18217. printf("first set rsa padding error\n");
  18218. ret = ERR_BASE_PKEY-32;
  18219. goto openssl_pkey0_test_done;
  18220. }
  18221. #ifndef HAVE_FIPS
  18222. if (EVP_PKEY_CTX_set_rsa_padding(dec, RSA_PKCS1_OAEP_PADDING) <= 0){
  18223. printf("second set rsa padding error\n");
  18224. ret = ERR_BASE_PKEY-33;
  18225. goto openssl_pkey0_test_done;
  18226. }
  18227. if (EVP_PKEY_CTX_set_rsa_padding(enc, RSA_PKCS1_OAEP_PADDING) <= 0) {
  18228. printf("third set rsa padding error\n");
  18229. ret = ERR_BASE_PKEY-34;
  18230. goto openssl_pkey0_test_done;
  18231. }
  18232. #endif
  18233. XMEMSET(out, 0, sizeof(out));
  18234. ret = EVP_PKEY_encrypt(enc, out, &outlen, in, sizeof(in));
  18235. if (ret != 1) {
  18236. printf("error encrypting msg\n");
  18237. ret = ERR_BASE_PKEY-35;
  18238. goto openssl_pkey0_test_done;
  18239. }
  18240. show("encrypted msg", out, outlen);
  18241. XMEMSET(plain, 0, sizeof(plain));
  18242. ret = EVP_PKEY_decrypt(dec, plain, &outlen, out, keySz);
  18243. if (ret != 1) {
  18244. printf("error decrypting msg\n");
  18245. ret = ERR_BASE_PKEY-36;
  18246. goto openssl_pkey0_test_done;
  18247. }
  18248. show("decrypted msg", plain, outlen);
  18249. ret = 0; /* made it to this point without error then set success */
  18250. openssl_pkey0_test_done:
  18251. wolfSSL_RSA_free(prvRsa);
  18252. wolfSSL_RSA_free(pubRsa);
  18253. EVP_PKEY_free(pubPkey);
  18254. EVP_PKEY_free(prvPkey);
  18255. EVP_PKEY_CTX_free(dec);
  18256. EVP_PKEY_CTX_free(enc);
  18257. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18258. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18259. #endif /* NO_RSA */
  18260. return ret;
  18261. }
  18262. WOLFSSL_TEST_SUBROUTINE int openssl_pkey1_test(void)
  18263. {
  18264. int ret = 0;
  18265. #if !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(HAVE_USER_RSA) && \
  18266. !defined(NO_SHA)
  18267. EVP_PKEY_CTX* dec = NULL;
  18268. EVP_PKEY_CTX* enc = NULL;
  18269. EVP_PKEY* pubKey = NULL;
  18270. EVP_PKEY* prvKey = NULL;
  18271. X509* x509 = NULL;
  18272. WOLFSSL_SMALL_STACK_STATIC const unsigned char msg[] = "sugar slapped";
  18273. const unsigned char* clikey;
  18274. long cliKeySz;
  18275. size_t outlen;
  18276. int keyLenBits = 2048;
  18277. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18278. unsigned char *tmp = (unsigned char *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18279. unsigned char *cipher = (unsigned char *)XMALLOC(RSA_TEST_BYTES, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18280. unsigned char *plain = (unsigned char *)XMALLOC(RSA_TEST_BYTES, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18281. if ((tmp == NULL) ||
  18282. (cipher == NULL) ||
  18283. (plain == NULL)) {
  18284. ret = -9015;
  18285. goto openssl_pkey1_test_done;
  18286. }
  18287. #else
  18288. unsigned char tmp[FOURK_BUF];
  18289. unsigned char cipher[RSA_TEST_BYTES];
  18290. unsigned char plain[RSA_TEST_BYTES];
  18291. #endif
  18292. #if defined(USE_CERT_BUFFERS_1024)
  18293. XMEMCPY(tmp, client_key_der_1024, sizeof_client_key_der_1024);
  18294. cliKeySz = (long)sizeof_client_key_der_1024;
  18295. x509 = wolfSSL_X509_load_certificate_buffer(client_cert_der_1024,
  18296. sizeof_client_cert_der_1024, SSL_FILETYPE_ASN1);
  18297. keyLenBits = 1024;
  18298. #elif defined(USE_CERT_BUFFERS_2048)
  18299. XMEMCPY(tmp, client_key_der_2048, sizeof_client_key_der_2048);
  18300. cliKeySz = (long)sizeof_client_key_der_2048;
  18301. x509 = wolfSSL_X509_load_certificate_buffer(client_cert_der_2048,
  18302. sizeof_client_cert_der_2048, SSL_FILETYPE_ASN1);
  18303. #elif defined(USE_CERT_BUFFERS_3072)
  18304. XMEMCPY(tmp, client_key_der_3072, sizeof_client_key_der_3072);
  18305. cliKeySz = (long)sizeof_client_key_der_3072;
  18306. x509 = wolfSSL_X509_load_certificate_buffer(client_cert_der_3072,
  18307. sizeof_client_cert_der_3072, SSL_FILETYPE_ASN1);
  18308. keyLenBits = 3072;
  18309. #elif defined(USE_CERT_BUFFERS_4096)
  18310. XMEMCPY(tmp, client_key_der_4096, sizeof_client_key_der_4096);
  18311. cliKeySz = (long)sizeof_client_key_der_4096;
  18312. x509 = wolfSSL_X509_load_certificate_buffer(client_cert_der_4096,
  18313. sizeof_client_cert_der_4096, SSL_FILETYPE_ASN1);
  18314. keyLenBits = 4096;
  18315. #else
  18316. {
  18317. XFILE f;
  18318. f = XFOPEN(clientKey, "rb");
  18319. if (!f) {
  18320. err_sys("can't open ./certs/client-key.der, "
  18321. "Please run from wolfSSL home dir", -41);
  18322. ret = -9000;
  18323. goto openssl_pkey1_test_done;
  18324. }
  18325. cliKeySz = (long)XFREAD(tmp, 1, FOURK_BUF, f);
  18326. XFCLOSE(f);
  18327. }
  18328. /* using existing wolfSSL api to get public and private key */
  18329. x509 = wolfSSL_X509_load_certificate_file(clientCert, SSL_FILETYPE_ASN1);
  18330. #endif /* USE_CERT_BUFFERS */
  18331. clikey = tmp;
  18332. if ((prvKey = EVP_PKEY_new()) == NULL) {
  18333. ret = -9001;
  18334. goto openssl_pkey1_test_done;
  18335. }
  18336. EVP_PKEY_free(prvKey);
  18337. prvKey = NULL;
  18338. if (x509 == NULL) {
  18339. ret = -9002;
  18340. goto openssl_pkey1_test_done;
  18341. }
  18342. pubKey = X509_get_pubkey(x509);
  18343. if (pubKey == NULL) {
  18344. ret = -9003;
  18345. goto openssl_pkey1_test_done;
  18346. }
  18347. prvKey = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &clikey, cliKeySz);
  18348. if (prvKey == NULL) {
  18349. ret = -9004;
  18350. goto openssl_pkey1_test_done;
  18351. }
  18352. /* phase 2 API to create EVP_PKEY_CTX and encrypt/decrypt */
  18353. if (EVP_PKEY_bits(prvKey) != keyLenBits) {
  18354. ret = -9005;
  18355. goto openssl_pkey1_test_done;
  18356. }
  18357. if (EVP_PKEY_size(prvKey) != keyLenBits/8) {
  18358. ret = -9006;
  18359. goto openssl_pkey1_test_done;
  18360. }
  18361. dec = EVP_PKEY_CTX_new(prvKey, NULL);
  18362. enc = EVP_PKEY_CTX_new(pubKey, NULL);
  18363. if (dec == NULL || enc == NULL) {
  18364. ret = -9007;
  18365. goto openssl_pkey1_test_done;
  18366. }
  18367. if (EVP_PKEY_decrypt_init(dec) != 1) {
  18368. ret = -9008;
  18369. goto openssl_pkey1_test_done;
  18370. }
  18371. if (EVP_PKEY_encrypt_init(enc) != 1) {
  18372. ret = -9009;
  18373. goto openssl_pkey1_test_done;
  18374. }
  18375. if (EVP_PKEY_CTX_set_rsa_padding(dec, RSA_PKCS1_PADDING) <= 0) {
  18376. ret = -9010;
  18377. goto openssl_pkey1_test_done;
  18378. }
  18379. #ifndef HAVE_FIPS
  18380. if (EVP_PKEY_CTX_set_rsa_padding(dec, RSA_PKCS1_OAEP_PADDING) <= 0){
  18381. ret = -9011;
  18382. goto openssl_pkey1_test_done;
  18383. }
  18384. if (EVP_PKEY_CTX_set_rsa_padding(enc, RSA_PKCS1_OAEP_PADDING) <= 0) {
  18385. ret = -9012;
  18386. goto openssl_pkey1_test_done;
  18387. }
  18388. #endif
  18389. XMEMSET(cipher, 0, RSA_TEST_BYTES);
  18390. outlen = keyLenBits/8;
  18391. if (EVP_PKEY_encrypt(enc, cipher, &outlen, msg, sizeof(msg)) < 0) {
  18392. ret = -9013;
  18393. goto openssl_pkey1_test_done;
  18394. }
  18395. XMEMSET(plain, 0, RSA_TEST_BYTES);
  18396. if (EVP_PKEY_decrypt(dec, plain, &outlen, cipher, outlen) != 1) {
  18397. ret = -9014;
  18398. goto openssl_pkey1_test_done;
  18399. }
  18400. openssl_pkey1_test_done:
  18401. if (pubKey != NULL) {
  18402. EVP_PKEY_free(pubKey);
  18403. }
  18404. if (prvKey != NULL) {
  18405. EVP_PKEY_free(prvKey);
  18406. }
  18407. if (dec != NULL) {
  18408. EVP_PKEY_CTX_free(dec);
  18409. }
  18410. if (enc != NULL) {
  18411. EVP_PKEY_CTX_free(enc);
  18412. }
  18413. if (x509 != NULL) {
  18414. X509_free(x509);
  18415. }
  18416. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18417. if (tmp != NULL)
  18418. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18419. if (cipher != NULL)
  18420. XFREE(cipher, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18421. if (plain != NULL)
  18422. XFREE(plain, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18423. #endif
  18424. #endif
  18425. return ret;
  18426. }
  18427. #define ERR_BASE_EVPSIG (-5100)
  18428. WOLFSSL_TEST_SUBROUTINE int openssl_evpSig_test(void)
  18429. {
  18430. #if !defined(NO_RSA) && !defined(NO_SHA) && !defined(HAVE_USER_RSA)
  18431. byte* prvTmp;
  18432. byte* pubTmp;
  18433. int prvBytes;
  18434. int pubBytes;
  18435. RSA *prvRsa;
  18436. RSA *pubRsa;
  18437. EVP_PKEY *prvPkey;
  18438. EVP_PKEY *pubPkey;
  18439. EVP_MD_CTX* sign;
  18440. EVP_MD_CTX* verf;
  18441. char msg[] = "see spot run";
  18442. unsigned char sig[256];
  18443. unsigned int sigSz;
  18444. const void* pt;
  18445. unsigned int count;
  18446. int ret, ret1, ret2;
  18447. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  18448. XFILE keyFile;
  18449. XFILE keypubFile;
  18450. char cliKey[] = "./certs/client-key.der";
  18451. char cliKeypub[] = "./certs/client-keyPub.der";
  18452. #endif
  18453. prvTmp = (byte*)XMALLOC(FOURK_BUFF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18454. if (prvTmp == NULL)
  18455. return ERR_BASE_EVPSIG-1;
  18456. pubTmp = (byte*)XMALLOC(FOURK_BUFF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18457. if (pubTmp == NULL) {
  18458. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18459. return ERR_BASE_EVPSIG-2;
  18460. }
  18461. #ifdef USE_CERT_BUFFERS_1024
  18462. XMEMCPY(prvTmp, client_key_der_1024, sizeof_client_key_der_1024);
  18463. prvBytes = sizeof_client_key_der_1024;
  18464. XMEMCPY(pubTmp, client_keypub_der_1024, sizeof_client_keypub_der_1024);
  18465. pubBytes = sizeof_client_keypub_der_1024;
  18466. #elif defined(USE_CERT_BUFFERS_2048)
  18467. XMEMCPY(prvTmp, client_key_der_2048, sizeof_client_key_der_2048);
  18468. prvBytes = sizeof_client_key_der_2048;
  18469. XMEMCPY(pubTmp, client_keypub_der_2048, sizeof_client_keypub_der_2048);
  18470. pubBytes = sizeof_client_keypub_der_2048;
  18471. #else
  18472. keyFile = XFOPEN(cliKey, "rb");
  18473. if (!keyFile) {
  18474. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18475. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18476. err_sys("can't open ./certs/client-key.der, "
  18477. "Please run from wolfSSL home dir", -40);
  18478. return ERR_BASE_EVPSIG-3;
  18479. }
  18480. prvBytes = (int)XFREAD(prvTmp, 1, (int)FOURK_BUFF, keyFile);
  18481. XFCLOSE(keyFile);
  18482. keypubFile = XFOPEN(cliKeypub, "rb");
  18483. if (!keypubFile) {
  18484. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18485. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18486. err_sys("can't open ./certs/client-cert.der, "
  18487. "Please run from wolfSSL home dir", -41);
  18488. return ERR_BASE_EVPSIG-4;
  18489. }
  18490. pubBytes = (int)XFREAD(pubTmp, 1, (int)FOURK_BUFF, keypubFile);
  18491. XFCLOSE(keypubFile);
  18492. #endif /* USE_CERT_BUFFERS */
  18493. prvRsa = wolfSSL_RSA_new();
  18494. pubRsa = wolfSSL_RSA_new();
  18495. if((prvRsa == NULL) || (pubRsa == NULL)){
  18496. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18497. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18498. err_sys("ERROR with RSA_new", -9100);
  18499. return ERR_BASE_EVPSIG-5;
  18500. }
  18501. ret1 = wolfSSL_RSA_LoadDer_ex(prvRsa, prvTmp, prvBytes, WOLFSSL_RSA_LOAD_PRIVATE);
  18502. ret2 = wolfSSL_RSA_LoadDer_ex(pubRsa, pubTmp, pubBytes, WOLFSSL_RSA_LOAD_PUBLIC);
  18503. if((ret1 != SSL_SUCCESS) || (ret2 != SSL_SUCCESS)){
  18504. printf("error with RSA_LoadDer_ex\n");
  18505. return ERR_BASE_EVPSIG-6;
  18506. }
  18507. prvPkey = wolfSSL_EVP_PKEY_new();
  18508. pubPkey = wolfSSL_EVP_PKEY_new();
  18509. if((prvPkey == NULL) || (pubPkey == NULL)){
  18510. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18511. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18512. printf("error with KEY_new\n");
  18513. return ERR_BASE_EVPSIG-7;
  18514. }
  18515. ret1 = wolfSSL_EVP_PKEY_set1_RSA(prvPkey, prvRsa);
  18516. ret2 = wolfSSL_EVP_PKEY_set1_RSA(pubPkey, pubRsa);
  18517. if((ret1 != 1) || (ret2 != 1)){
  18518. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18519. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18520. printf("error with EVP_PKEY_set1_RSA\n");
  18521. return ERR_BASE_EVPSIG-8;
  18522. }
  18523. /****************** sign and verify *******************/
  18524. sign = EVP_MD_CTX_create();
  18525. verf = EVP_MD_CTX_create();
  18526. if((sign == NULL)||(verf == NULL)){
  18527. printf("error with EVP_MD_CTX_create\n");
  18528. EVP_MD_CTX_destroy(sign);
  18529. EVP_MD_CTX_destroy(verf);
  18530. return ERR_BASE_EVPSIG-10;
  18531. }
  18532. ret = EVP_SignInit(sign, EVP_sha1());
  18533. if (ret != SSL_SUCCESS){
  18534. printf("error with EVP_SignInit\n");
  18535. EVP_MD_CTX_destroy(sign);
  18536. EVP_MD_CTX_destroy(verf);
  18537. return ERR_BASE_EVPSIG-11;
  18538. }
  18539. count = sizeof(msg);
  18540. show("message = ", (char *)msg, count);
  18541. /* sign */
  18542. XMEMSET(sig, 0, sizeof(sig));
  18543. pt = (const void*)msg;
  18544. ret1 = EVP_SignUpdate(sign, pt, count);
  18545. ret2 = EVP_SignFinal(sign, sig, &sigSz, prvPkey);
  18546. if((ret1 != SSL_SUCCESS) || (ret2 != SSL_SUCCESS)){
  18547. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18548. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18549. EVP_MD_CTX_destroy(sign);
  18550. EVP_MD_CTX_destroy(verf);
  18551. printf("error with EVP_MD_CTX_create\n");
  18552. return ERR_BASE_EVPSIG-12;
  18553. }
  18554. show("signature = ", (char *)sig, sigSz);
  18555. /* verify */
  18556. pt = (const void*)msg;
  18557. ret1 = EVP_VerifyInit(verf, EVP_sha1());
  18558. ret2 = EVP_VerifyUpdate(verf, pt, count);
  18559. if((ret1 != SSL_SUCCESS) || (ret2 != SSL_SUCCESS)){
  18560. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18561. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18562. EVP_MD_CTX_destroy(sign);
  18563. EVP_MD_CTX_destroy(verf);
  18564. printf("error with EVP_Verify\n");
  18565. return ERR_BASE_EVPSIG-13;
  18566. }
  18567. if (EVP_VerifyFinal(verf, sig, sigSz, pubPkey) != 1) {
  18568. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18569. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18570. EVP_MD_CTX_destroy(sign);
  18571. EVP_MD_CTX_destroy(verf);
  18572. printf("error with EVP_VerifyFinal\n");
  18573. return ERR_BASE_EVPSIG-14;
  18574. }
  18575. /* expect fail without update */
  18576. EVP_VerifyInit(verf, EVP_sha1());
  18577. if (EVP_VerifyFinal(verf, sig, sigSz, pubPkey) == 1) {
  18578. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18579. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18580. EVP_MD_CTX_destroy(sign);
  18581. EVP_MD_CTX_destroy(verf);
  18582. printf("EVP_VerifyInit without update not detected\n");
  18583. return ERR_BASE_EVPSIG-15;
  18584. }
  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. wolfSSL_RSA_free(prvRsa);
  18590. wolfSSL_RSA_free(pubRsa);
  18591. EVP_PKEY_free(pubPkey);
  18592. EVP_PKEY_free(prvPkey);
  18593. #endif /* NO_RSA */
  18594. return 0;
  18595. }
  18596. #endif /* OPENSSL_EXTRA */
  18597. #ifndef NO_PWDBASED
  18598. #ifdef HAVE_SCRYPT
  18599. /* Test vectors taken from RFC 7914: scrypt PBKDF - Section 12. */
  18600. WOLFSSL_TEST_SUBROUTINE int scrypt_test(void)
  18601. {
  18602. #ifdef HAVE_FIPS
  18603. /* RFC 7914 test vector keys are too short for FIPS. */
  18604. #else
  18605. int ret;
  18606. byte derived[64];
  18607. WOLFSSL_SMALL_STACK_STATIC const byte verify1[] = {
  18608. 0x77, 0xd6, 0x57, 0x62, 0x38, 0x65, 0x7b, 0x20,
  18609. 0x3b, 0x19, 0xca, 0x42, 0xc1, 0x8a, 0x04, 0x97,
  18610. 0xf1, 0x6b, 0x48, 0x44, 0xe3, 0x07, 0x4a, 0xe8,
  18611. 0xdf, 0xdf, 0xfa, 0x3f, 0xed, 0xe2, 0x14, 0x42,
  18612. 0xfc, 0xd0, 0x06, 0x9d, 0xed, 0x09, 0x48, 0xf8,
  18613. 0x32, 0x6a, 0x75, 0x3a, 0x0f, 0xc8, 0x1f, 0x17,
  18614. 0xe8, 0xd3, 0xe0, 0xfb, 0x2e, 0x0d, 0x36, 0x28,
  18615. 0xcf, 0x35, 0xe2, 0x0c, 0x38, 0xd1, 0x89, 0x06
  18616. };
  18617. WOLFSSL_SMALL_STACK_STATIC const byte verify2[] = {
  18618. 0xfd, 0xba, 0xbe, 0x1c, 0x9d, 0x34, 0x72, 0x00,
  18619. 0x78, 0x56, 0xe7, 0x19, 0x0d, 0x01, 0xe9, 0xfe,
  18620. 0x7c, 0x6a, 0xd7, 0xcb, 0xc8, 0x23, 0x78, 0x30,
  18621. 0xe7, 0x73, 0x76, 0x63, 0x4b, 0x37, 0x31, 0x62,
  18622. 0x2e, 0xaf, 0x30, 0xd9, 0x2e, 0x22, 0xa3, 0x88,
  18623. 0x6f, 0xf1, 0x09, 0x27, 0x9d, 0x98, 0x30, 0xda,
  18624. 0xc7, 0x27, 0xaf, 0xb9, 0x4a, 0x83, 0xee, 0x6d,
  18625. 0x83, 0x60, 0xcb, 0xdf, 0xa2, 0xcc, 0x06, 0x40
  18626. };
  18627. #if !defined(BENCH_EMBEDDED) && !defined(WOLFSSL_LINUXKM) && !defined(HAVE_INTEL_QA)
  18628. WOLFSSL_SMALL_STACK_STATIC const byte verify3[] = {
  18629. 0x70, 0x23, 0xbd, 0xcb, 0x3a, 0xfd, 0x73, 0x48,
  18630. 0x46, 0x1c, 0x06, 0xcd, 0x81, 0xfd, 0x38, 0xeb,
  18631. 0xfd, 0xa8, 0xfb, 0xba, 0x90, 0x4f, 0x8e, 0x3e,
  18632. 0xa9, 0xb5, 0x43, 0xf6, 0x54, 0x5d, 0xa1, 0xf2,
  18633. 0xd5, 0x43, 0x29, 0x55, 0x61, 0x3f, 0x0f, 0xcf,
  18634. 0x62, 0xd4, 0x97, 0x05, 0x24, 0x2a, 0x9a, 0xf9,
  18635. 0xe6, 0x1e, 0x85, 0xdc, 0x0d, 0x65, 0x1e, 0x40,
  18636. 0xdf, 0xcf, 0x01, 0x7b, 0x45, 0x57, 0x58, 0x87
  18637. };
  18638. #endif
  18639. #ifdef SCRYPT_TEST_ALL
  18640. /* Test case is very slow.
  18641. * Use for confirmation after code change or new platform.
  18642. */
  18643. WOLFSSL_SMALL_STACK_STATIC const byte verify4[] = {
  18644. 0x21, 0x01, 0xcb, 0x9b, 0x6a, 0x51, 0x1a, 0xae,
  18645. 0xad, 0xdb, 0xbe, 0x09, 0xcf, 0x70, 0xf8, 0x81,
  18646. 0xec, 0x56, 0x8d, 0x57, 0x4a, 0x2f, 0xfd, 0x4d,
  18647. 0xab, 0xe5, 0xee, 0x98, 0x20, 0xad, 0xaa, 0x47,
  18648. 0x8e, 0x56, 0xfd, 0x8f, 0x4b, 0xa5, 0xd0, 0x9f,
  18649. 0xfa, 0x1c, 0x6d, 0x92, 0x7c, 0x40, 0xf4, 0xc3,
  18650. 0x37, 0x30, 0x40, 0x49, 0xe8, 0xa9, 0x52, 0xfb,
  18651. 0xcb, 0xf4, 0x5c, 0x6f, 0xa7, 0x7a, 0x41, 0xa4
  18652. };
  18653. #endif
  18654. ret = wc_scrypt(derived, NULL, 0, NULL, 0, 4, 1, 1, sizeof(verify1));
  18655. if (ret != 0)
  18656. return -9200;
  18657. if (XMEMCMP(derived, verify1, sizeof(verify1)) != 0)
  18658. return -9201;
  18659. ret = wc_scrypt(derived, (byte*)"password", 8, (byte*)"NaCl", 4, 10, 8, 16,
  18660. sizeof(verify2));
  18661. if (ret != 0)
  18662. return -9202;
  18663. if (XMEMCMP(derived, verify2, sizeof(verify2)) != 0)
  18664. return -9203;
  18665. /* Test case with parallel overflowing */
  18666. ret = wc_scrypt(derived, (byte*)"password", 16, (byte*)"NaCl", 16, 2, 4, 8388608,
  18667. sizeof(verify2));
  18668. if (ret != BAD_FUNC_ARG)
  18669. return -9210;
  18670. /* Don't run these test on embedded, since they use large mallocs */
  18671. #if !defined(BENCH_EMBEDDED) && !defined(WOLFSSL_LINUXKM) && !defined(HAVE_INTEL_QA)
  18672. ret = wc_scrypt(derived, (byte*)"pleaseletmein", 13,
  18673. (byte*)"SodiumChloride", 14, 14, 8, 1, sizeof(verify3));
  18674. if (ret != 0)
  18675. return -9204;
  18676. if (XMEMCMP(derived, verify3, sizeof(verify3)) != 0)
  18677. return -9205;
  18678. #ifdef SCRYPT_TEST_ALL
  18679. ret = wc_scrypt(derived, (byte*)"pleaseletmein", 13,
  18680. (byte*)"SodiumChloride", 14, 20, 8, 1, sizeof(verify4));
  18681. if (ret != 0)
  18682. return -9206;
  18683. if (XMEMCMP(derived, verify4, sizeof(verify4)) != 0)
  18684. return -9207;
  18685. #endif
  18686. #endif /* !BENCH_EMBEDDED && !defined(WOLFSSL_LINUXKM) && !HAVE_INTEL_QA */
  18687. ret = wc_scrypt_ex(derived, (byte*)"password", 8, (byte*)"NaCl", 4, 1<<10,
  18688. 8, 16, sizeof(verify2));
  18689. if (ret != 0)
  18690. return -9208;
  18691. if (XMEMCMP(derived, verify2, sizeof(verify2)) != 0)
  18692. return -9209;
  18693. #endif /* !HAVE_FIPS */
  18694. return 0;
  18695. }
  18696. #endif
  18697. #ifdef HAVE_PKCS12
  18698. WOLFSSL_TEST_SUBROUTINE int pkcs12_test(void)
  18699. {
  18700. WOLFSSL_SMALL_STACK_STATIC const byte passwd[] = { 0x00, 0x73, 0x00, 0x6d, 0x00, 0x65, 0x00, 0x67,
  18701. 0x00, 0x00 };
  18702. WOLFSSL_SMALL_STACK_STATIC const byte salt[] = { 0x0a, 0x58, 0xCF, 0x64, 0x53, 0x0d, 0x82, 0x3f };
  18703. WOLFSSL_SMALL_STACK_STATIC const byte passwd2[] = { 0x00, 0x71, 0x00, 0x75, 0x00, 0x65, 0x00, 0x65,
  18704. 0x00, 0x67, 0x00, 0x00 };
  18705. WOLFSSL_SMALL_STACK_STATIC const byte salt2[] = { 0x16, 0x82, 0xC0, 0xfC, 0x5b, 0x3f, 0x7e, 0xc5 };
  18706. byte derived[64];
  18707. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  18708. 0x27, 0xE9, 0x0D, 0x7E, 0xD5, 0xA1, 0xC4, 0x11,
  18709. 0xBA, 0x87, 0x8B, 0xC0, 0x90, 0xF5, 0xCE, 0xBE,
  18710. 0x5E, 0x9D, 0x5F, 0xE3, 0xD6, 0x2B, 0x73, 0xAA
  18711. };
  18712. WOLFSSL_SMALL_STACK_STATIC const byte verify2[] = {
  18713. 0x90, 0x1B, 0x49, 0x70, 0xF0, 0x94, 0xF0, 0xF8,
  18714. 0x45, 0xC0, 0xF3, 0xF3, 0x13, 0x59, 0x18, 0x6A,
  18715. 0x35, 0xE3, 0x67, 0xFE, 0xD3, 0x21, 0xFD, 0x7C
  18716. };
  18717. int id = 1;
  18718. int kLen = 24;
  18719. int iterations = 1;
  18720. int ret = wc_PKCS12_PBKDF(derived, passwd, sizeof(passwd), salt, 8,
  18721. iterations, kLen, WC_SHA256, id);
  18722. if (ret < 0)
  18723. return -9300;
  18724. if (XMEMCMP(derived, verify, kLen) != 0)
  18725. return -9301;
  18726. iterations = 1000;
  18727. ret = wc_PKCS12_PBKDF(derived, passwd2, sizeof(passwd2), salt2, 8,
  18728. iterations, kLen, WC_SHA256, id);
  18729. if (ret < 0)
  18730. return -9302;
  18731. ret = wc_PKCS12_PBKDF_ex(derived, passwd2, sizeof(passwd2), salt2, 8,
  18732. iterations, kLen, WC_SHA256, id, HEAP_HINT);
  18733. if (ret < 0)
  18734. return -9303;
  18735. if (XMEMCMP(derived, verify2, 24) != 0)
  18736. return -9304;
  18737. return 0;
  18738. }
  18739. #endif /* HAVE_PKCS12 */
  18740. #if defined(HAVE_PBKDF2) && !defined(NO_SHA256) && !defined(NO_HMAC)
  18741. WOLFSSL_TEST_SUBROUTINE int pbkdf2_test(void)
  18742. {
  18743. char passwd[] = "passwordpassword";
  18744. WOLFSSL_SMALL_STACK_STATIC const byte salt[] = { 0x78, 0x57, 0x8E, 0x5a, 0x5d, 0x63, 0xcb, 0x06 };
  18745. int iterations = 2048;
  18746. int kLen = 24;
  18747. byte derived[64];
  18748. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  18749. 0x43, 0x6d, 0xb5, 0xe8, 0xd0, 0xfb, 0x3f, 0x35, 0x42, 0x48, 0x39, 0xbc,
  18750. 0x2d, 0xd4, 0xf9, 0x37, 0xd4, 0x95, 0x16, 0xa7, 0x2a, 0x9a, 0x21, 0xd1
  18751. };
  18752. int ret = wc_PBKDF2_ex(derived, (byte*)passwd, (int)XSTRLEN(passwd), salt,
  18753. (int)sizeof(salt), iterations, kLen, WC_SHA256, HEAP_HINT, devId);
  18754. if (ret != 0)
  18755. return ret;
  18756. if (XMEMCMP(derived, verify, sizeof(verify)) != 0)
  18757. return -9400;
  18758. return 0;
  18759. }
  18760. #endif /* HAVE_PBKDF2 && !NO_SHA256 && !NO_HMAC */
  18761. #if defined(HAVE_PBKDF1) && !defined(NO_SHA)
  18762. WOLFSSL_TEST_SUBROUTINE int pbkdf1_test(void)
  18763. {
  18764. char passwd[] = "password";
  18765. WOLFSSL_SMALL_STACK_STATIC const byte salt[] = { 0x78, 0x57, 0x8E, 0x5a, 0x5d, 0x63, 0xcb, 0x06 };
  18766. int iterations = 1000;
  18767. int kLen = 16;
  18768. byte derived[16];
  18769. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  18770. 0xDC, 0x19, 0x84, 0x7E, 0x05, 0xC6, 0x4D, 0x2F,
  18771. 0xAF, 0x10, 0xEB, 0xFB, 0x4A, 0x3D, 0x2A, 0x20
  18772. };
  18773. int ret = wc_PBKDF1_ex(derived, kLen, NULL, 0, (byte*)passwd,
  18774. (int)XSTRLEN(passwd), salt, (int)sizeof(salt), iterations, WC_SHA,
  18775. HEAP_HINT);
  18776. if (ret != 0)
  18777. return ret;
  18778. if (XMEMCMP(derived, verify, sizeof(verify)) != 0)
  18779. return -9500;
  18780. return 0;
  18781. }
  18782. #endif /* HAVE_PBKDF2 && !NO_SHA */
  18783. WOLFSSL_TEST_SUBROUTINE int pwdbased_test(void)
  18784. {
  18785. int ret = 0;
  18786. #if defined(HAVE_PBKDF1) && !defined(NO_SHA)
  18787. ret = pbkdf1_test();
  18788. if (ret != 0)
  18789. return ret;
  18790. #endif
  18791. #if defined(HAVE_PBKDF2) && !defined(NO_SHA256) && !defined(NO_HMAC)
  18792. ret = pbkdf2_test();
  18793. if (ret != 0)
  18794. return ret;
  18795. #endif
  18796. #ifdef HAVE_PKCS12
  18797. ret = pkcs12_test();
  18798. if (ret != 0)
  18799. return ret;
  18800. #endif
  18801. #ifdef HAVE_SCRYPT
  18802. ret = scrypt_test();
  18803. if (ret != 0)
  18804. return ret;
  18805. #endif
  18806. return ret;
  18807. }
  18808. #endif /* NO_PWDBASED */
  18809. #if defined(HAVE_HKDF) && !defined(NO_HMAC)
  18810. /* hkdf_test has issue with WOLFSSL_TEST_SUBROUTINE set on Xilinx with afalg */
  18811. static int hkdf_test(void)
  18812. {
  18813. int ret = 0;
  18814. #if !defined(NO_SHA) || !defined(NO_SHA256)
  18815. int L;
  18816. byte okm1[42];
  18817. byte ikm1[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  18818. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  18819. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b };
  18820. #ifndef HAVE_FIPS
  18821. byte salt1[13] ={ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18822. 0x08, 0x09, 0x0a, 0x0b, 0x0c };
  18823. byte info1[10] ={ 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
  18824. 0xf8, 0xf9 };
  18825. #endif
  18826. #ifndef NO_SHA
  18827. byte res1[42] = { 0x0a, 0xc1, 0xaf, 0x70, 0x02, 0xb3, 0xd7, 0x61,
  18828. 0xd1, 0xe5, 0x52, 0x98, 0xda, 0x9d, 0x05, 0x06,
  18829. 0xb9, 0xae, 0x52, 0x05, 0x72, 0x20, 0xa3, 0x06,
  18830. 0xe0, 0x7b, 0x6b, 0x87, 0xe8, 0xdf, 0x21, 0xd0,
  18831. 0xea, 0x00, 0x03, 0x3d, 0xe0, 0x39, 0x84, 0xd3,
  18832. 0x49, 0x18 };
  18833. #ifndef HAVE_FIPS
  18834. byte res2[42] = { 0x08, 0x5a, 0x01, 0xea, 0x1b, 0x10, 0xf3, 0x69,
  18835. 0x33, 0x06, 0x8b, 0x56, 0xef, 0xa5, 0xad, 0x81,
  18836. 0xa4, 0xf1, 0x4b, 0x82, 0x2f, 0x5b, 0x09, 0x15,
  18837. 0x68, 0xa9, 0xcd, 0xd4, 0xf1, 0x55, 0xfd, 0xa2,
  18838. 0xc2, 0x2e, 0x42, 0x24, 0x78, 0xd3, 0x05, 0xf3,
  18839. 0xf8, 0x96 };
  18840. #endif
  18841. #endif /* !NO_SHA */
  18842. #ifndef NO_SHA256
  18843. byte res3[42] = { 0x8d, 0xa4, 0xe7, 0x75, 0xa5, 0x63, 0xc1, 0x8f,
  18844. 0x71, 0x5f, 0x80, 0x2a, 0x06, 0x3c, 0x5a, 0x31,
  18845. 0xb8, 0xa1, 0x1f, 0x5c, 0x5e, 0xe1, 0x87, 0x9e,
  18846. 0xc3, 0x45, 0x4e, 0x5f, 0x3c, 0x73, 0x8d, 0x2d,
  18847. 0x9d, 0x20, 0x13, 0x95, 0xfa, 0xa4, 0xb6, 0x1a,
  18848. 0x96, 0xc8 };
  18849. #ifndef HAVE_FIPS
  18850. byte res4[42] = { 0x3c, 0xb2, 0x5f, 0x25, 0xfa, 0xac, 0xd5, 0x7a,
  18851. 0x90, 0x43, 0x4f, 0x64, 0xd0, 0x36, 0x2f, 0x2a,
  18852. 0x2d, 0x2d, 0x0a, 0x90, 0xcf, 0x1a, 0x5a, 0x4c,
  18853. 0x5d, 0xb0, 0x2d, 0x56, 0xec, 0xc4, 0xc5, 0xbf,
  18854. 0x34, 0x00, 0x72, 0x08, 0xd5, 0xb8, 0x87, 0x18,
  18855. 0x58, 0x65 };
  18856. #endif
  18857. #endif /* !NO_SHA256 */
  18858. XMEMSET(okm1, 0, sizeof(okm1));
  18859. L = (int)sizeof(okm1);
  18860. #ifndef NO_SHA
  18861. ret = wc_HKDF(WC_SHA, ikm1, (word32)sizeof(ikm1), NULL, 0, NULL, 0,
  18862. okm1, L);
  18863. if (ret != 0)
  18864. return -9700;
  18865. if (XMEMCMP(okm1, res1, L) != 0)
  18866. return -9701;
  18867. #ifndef HAVE_FIPS
  18868. /* fips can't have key size under 14 bytes, salt is key too */
  18869. L = (int)sizeof(okm1);
  18870. ret = wc_HKDF(WC_SHA, ikm1, 11, salt1, (word32)sizeof(salt1),
  18871. info1, (word32)sizeof(info1), okm1, L);
  18872. if (ret != 0)
  18873. return -9702;
  18874. if (XMEMCMP(okm1, res2, L) != 0)
  18875. return -9703;
  18876. #endif /* HAVE_FIPS */
  18877. #endif /* !NO_SHA */
  18878. #ifndef NO_SHA256
  18879. ret = wc_HKDF(WC_SHA256, ikm1, (word32)sizeof(ikm1), NULL, 0, NULL, 0,
  18880. okm1, L);
  18881. if (ret != 0)
  18882. return -9704;
  18883. if (XMEMCMP(okm1, res3, L) != 0)
  18884. return -9705;
  18885. #ifndef HAVE_FIPS
  18886. /* fips can't have key size under 14 bytes, salt is key too */
  18887. ret = wc_HKDF(WC_SHA256, ikm1, (word32)sizeof(ikm1),
  18888. salt1, (word32)sizeof(salt1), info1, (word32)sizeof(info1), okm1, L);
  18889. if (ret != 0)
  18890. return -9706;
  18891. if (XMEMCMP(okm1, res4, L) != 0)
  18892. return -9707;
  18893. #endif /* HAVE_FIPS */
  18894. #endif /* !NO_SHA256 */
  18895. #endif /* !NO_SHA || !NO_SHA256 */
  18896. return ret;
  18897. }
  18898. #endif /* HAVE_HKDF */
  18899. #ifdef WOLFSSL_WOLFSSH
  18900. typedef struct {
  18901. byte hashId;
  18902. byte keyId;
  18903. const byte* k;
  18904. word32 kSz;
  18905. const byte* h;
  18906. word32 hSz;
  18907. const byte* sessionId;
  18908. word32 sessionIdSz;
  18909. const byte* expectedKey;
  18910. word32 expectedKeySz;
  18911. } SshKdfTestVector;
  18912. /** Test Vector Set #3: SHA-256 **/
  18913. static const byte sshKdfTvSet3k[] = {
  18914. 0x6A, 0xC3, 0x82, 0xEA, 0xAC, 0xA0, 0x93, 0xE1,
  18915. 0x25, 0xE2, 0x5C, 0x24, 0xBE, 0xBC, 0x84, 0x64,
  18916. 0x0C, 0x11, 0x98, 0x75, 0x07, 0x34, 0x4B, 0x5C,
  18917. 0x73, 0x9C, 0xEB, 0x84, 0xA9, 0xE0, 0xB2, 0x22,
  18918. 0xB9, 0xA8, 0xB5, 0x1C, 0x83, 0x9E, 0x5E, 0xBE,
  18919. 0x49, 0xCF, 0xAD, 0xBF, 0xB3, 0x95, 0x99, 0x76,
  18920. 0x4E, 0xD5, 0x22, 0x09, 0x9D, 0xC9, 0x12, 0x75,
  18921. 0x19, 0x50, 0xDC, 0x7D, 0xC9, 0x7F, 0xBD, 0xC0,
  18922. 0x63, 0x28, 0xB6, 0x8F, 0x22, 0x78, 0x1F, 0xD3,
  18923. 0x15, 0xAF, 0x56, 0x80, 0x09, 0xA5, 0x50, 0x9E,
  18924. 0x5B, 0x87, 0xA1, 0x1B, 0xF5, 0x27, 0xC0, 0x56,
  18925. 0xDA, 0xFF, 0xD8, 0x2A, 0xB6, 0xCB, 0xC2, 0x5C,
  18926. 0xCA, 0x37, 0x14, 0x34, 0x59, 0xE7, 0xBC, 0x63,
  18927. 0xBC, 0xDE, 0x52, 0x75, 0x7A, 0xDE, 0xB7, 0xDF,
  18928. 0x01, 0xCF, 0x12, 0x17, 0x3F, 0x1F, 0xEF, 0x81,
  18929. 0x02, 0xEC, 0x5A, 0xB1, 0x42, 0xC2, 0x13, 0xDD,
  18930. 0x9D, 0x30, 0x69, 0x62, 0x78, 0xA8, 0xD8, 0xBC,
  18931. 0x32, 0xDD, 0xE9, 0x59, 0x2D, 0x28, 0xC0, 0x78,
  18932. 0xC6, 0xD9, 0x2B, 0x94, 0x7D, 0x82, 0x5A, 0xCA,
  18933. 0xAB, 0x64, 0x94, 0x84, 0x6A, 0x49, 0xDE, 0x24,
  18934. 0xB9, 0x62, 0x3F, 0x48, 0x89, 0xE8, 0xAD, 0xC3,
  18935. 0x8E, 0x8C, 0x66, 0x9E, 0xFF, 0xEF, 0x17, 0x60,
  18936. 0x40, 0xAD, 0x94, 0x5E, 0x90, 0xA7, 0xD3, 0xEE,
  18937. 0xC1, 0x5E, 0xFE, 0xEE, 0x78, 0xAE, 0x71, 0x04,
  18938. 0x3C, 0x96, 0x51, 0x11, 0x03, 0xA1, 0x6B, 0xA7,
  18939. 0xCA, 0xF0, 0xAC, 0xD0, 0x64, 0x2E, 0xFD, 0xBE,
  18940. 0x80, 0x99, 0x34, 0xFA, 0xA1, 0xA5, 0xF1, 0xBD,
  18941. 0x11, 0x04, 0x36, 0x49, 0xB2, 0x5C, 0xCD, 0x1F,
  18942. 0xEE, 0x2E, 0x38, 0x81, 0x5D, 0x4D, 0x5F, 0x5F,
  18943. 0xC6, 0xB4, 0x10, 0x29, 0x69, 0xF2, 0x1C, 0x22,
  18944. 0xAE, 0x1B, 0x0E, 0x7D, 0x36, 0x03, 0xA5, 0x56,
  18945. 0xA1, 0x32, 0x62, 0xFF, 0x62, 0x8D, 0xE2, 0x22
  18946. };
  18947. static const byte sshKdfTvSet3h[] = {
  18948. 0x7B, 0x70, 0x01, 0x18, 0x5E, 0x25, 0x6D, 0x44,
  18949. 0x93, 0x44, 0x5F, 0x39, 0xA5, 0x5F, 0xB9, 0x05,
  18950. 0xE6, 0x32, 0x1F, 0x4B, 0x5D, 0xD8, 0xBB, 0xF3,
  18951. 0x10, 0x0D, 0x51, 0xBA, 0x0B, 0xDA, 0x3D, 0x2D
  18952. };
  18953. static const byte sshKdfTvSet3sid[] = {
  18954. 0x7B, 0x70, 0x01, 0x18, 0x5E, 0x25, 0x6D, 0x44,
  18955. 0x93, 0x44, 0x5F, 0x39, 0xA5, 0x5F, 0xB9, 0x05,
  18956. 0xE6, 0x32, 0x1F, 0x4B, 0x5D, 0xD8, 0xBB, 0xF3,
  18957. 0x10, 0x0D, 0x51, 0xBA, 0x0B, 0xDA, 0x3D, 0x2D
  18958. };
  18959. static const byte sshKdfTvSet3a[] = {
  18960. 0x81, 0xF0, 0x33, 0x0E, 0xF6, 0xF0, 0x53, 0x61,
  18961. 0xB3, 0x82, 0x3B, 0xFD, 0xED, 0x6E, 0x1D, 0xE9
  18962. };
  18963. static const byte sshKdfTvSet3b[] = {
  18964. 0x3F, 0x6F, 0xD2, 0x06, 0x5E, 0xEB, 0x2B, 0x0B,
  18965. 0x1D, 0x93, 0x19, 0x5A, 0x1F, 0xED, 0x48, 0xA5
  18966. };
  18967. static const byte sshKdfTvSet3c[] = {
  18968. 0xC3, 0x54, 0x71, 0x03, 0x4E, 0x6F, 0xD6, 0x54,
  18969. 0x76, 0x13, 0x17, 0x8E, 0x23, 0x43, 0x5F, 0x21
  18970. };
  18971. static const byte sshKdfTvSet3d[] = {
  18972. 0x7E, 0x9D, 0x79, 0x03, 0x20, 0x90, 0xD9, 0x9F,
  18973. 0x98, 0xB0, 0x15, 0x63, 0x4D, 0xD9, 0xF4, 0x62
  18974. };
  18975. static const byte sshKdfTvSet3e[] = {
  18976. 0x24, 0xEE, 0x55, 0x9A, 0xD7, 0xCE, 0x71, 0x2B,
  18977. 0x68, 0x5D, 0x0B, 0x22, 0x71, 0xE4, 0x43, 0xC1,
  18978. 0x7A, 0xB1, 0xD1, 0xDC, 0xEB, 0x5A, 0x36, 0x05,
  18979. 0x69, 0xD2, 0x5D, 0x5D, 0xC2, 0x43, 0x00, 0x2F
  18980. };
  18981. static const byte sshKdfTvSet3f[] = {
  18982. 0xC3, 0x41, 0x9C, 0x2B, 0x96, 0x62, 0x35, 0x86,
  18983. 0x9D, 0x71, 0x4B, 0xA5, 0xAC, 0x48, 0xDD, 0xB7,
  18984. 0xD9, 0xE3, 0x5C, 0x8C, 0x19, 0xAA, 0xC7, 0x34,
  18985. 0x22, 0x33, 0x7A, 0x37, 0x34, 0x53, 0x60, 0x7E
  18986. };
  18987. static const SshKdfTestVector sshKdfTestVectors[] = {
  18988. {WC_HASH_TYPE_SHA256, 'A',
  18989. sshKdfTvSet3k, sizeof(sshKdfTvSet3k),
  18990. sshKdfTvSet3h, sizeof(sshKdfTvSet3h),
  18991. sshKdfTvSet3sid, sizeof(sshKdfTvSet3sid),
  18992. sshKdfTvSet3a, sizeof(sshKdfTvSet3a)},
  18993. {WC_HASH_TYPE_SHA256, 'B',
  18994. sshKdfTvSet3k, sizeof(sshKdfTvSet3k),
  18995. sshKdfTvSet3h, sizeof(sshKdfTvSet3h),
  18996. sshKdfTvSet3sid, sizeof(sshKdfTvSet3sid),
  18997. sshKdfTvSet3b, sizeof(sshKdfTvSet3b)},
  18998. {WC_HASH_TYPE_SHA256, 'C',
  18999. sshKdfTvSet3k, sizeof(sshKdfTvSet3k),
  19000. sshKdfTvSet3h, sizeof(sshKdfTvSet3h),
  19001. sshKdfTvSet3sid, sizeof(sshKdfTvSet3sid),
  19002. sshKdfTvSet3c, sizeof(sshKdfTvSet3c)},
  19003. {WC_HASH_TYPE_SHA256, 'D',
  19004. sshKdfTvSet3k, sizeof(sshKdfTvSet3k),
  19005. sshKdfTvSet3h, sizeof(sshKdfTvSet3h),
  19006. sshKdfTvSet3sid, sizeof(sshKdfTvSet3sid),
  19007. sshKdfTvSet3d, sizeof(sshKdfTvSet3d)},
  19008. {WC_HASH_TYPE_SHA256, 'E',
  19009. sshKdfTvSet3k, sizeof(sshKdfTvSet3k),
  19010. sshKdfTvSet3h, sizeof(sshKdfTvSet3h),
  19011. sshKdfTvSet3sid, sizeof(sshKdfTvSet3sid),
  19012. sshKdfTvSet3e, sizeof(sshKdfTvSet3e)},
  19013. {WC_HASH_TYPE_SHA256, 'F',
  19014. sshKdfTvSet3k, sizeof(sshKdfTvSet3k),
  19015. sshKdfTvSet3h, sizeof(sshKdfTvSet3h),
  19016. sshKdfTvSet3sid, sizeof(sshKdfTvSet3sid),
  19017. sshKdfTvSet3f, sizeof(sshKdfTvSet3f)},
  19018. };
  19019. int sshkdf_test(void)
  19020. {
  19021. int result = 0;
  19022. word32 i;
  19023. word32 tc = sizeof(sshKdfTestVectors)/sizeof(SshKdfTestVector);
  19024. const SshKdfTestVector* tv = NULL;
  19025. byte cKey[32]; /* Greater of SHA256_DIGEST_SIZE and AES_BLOCK_SIZE */
  19026. /* sId - Session ID, eKey - Expected Key, cKey - Calculated Key */
  19027. for (i = 0, tv = sshKdfTestVectors; i < tc; i++, tv++) {
  19028. result = wc_SSH_KDF(tv->hashId, tv->keyId,
  19029. cKey, tv->expectedKeySz,
  19030. tv->k, tv->kSz, tv->h, tv->hSz,
  19031. tv->sessionId, tv->sessionIdSz);
  19032. if (result != 0) {
  19033. printf("KDF: Could not derive key.\n");
  19034. result = -101;
  19035. }
  19036. else {
  19037. if (XMEMCMP(cKey, tv->expectedKey, tv->expectedKeySz) != 0) {
  19038. printf("KDF: Calculated Key does not match Expected Key.\n");
  19039. result = -102;
  19040. }
  19041. }
  19042. if (result != 0) break;
  19043. }
  19044. return result;
  19045. }
  19046. #endif /* WOLFSSL_WOLFSSH */
  19047. #ifdef WOLFSSL_TLS13
  19048. #define TLSV13_PSK_DHE_SZ 40
  19049. typedef struct {
  19050. enum wc_HashType hashAlg;
  19051. word32 pskSz;
  19052. word32 dheSz;
  19053. byte psk[TLSV13_PSK_DHE_SZ];
  19054. byte dhe[TLSV13_PSK_DHE_SZ];
  19055. byte hashHello1[WC_MAX_DIGEST_SIZE];
  19056. byte hashHello2[WC_MAX_DIGEST_SIZE];
  19057. byte hashFinished1[WC_MAX_DIGEST_SIZE];
  19058. byte hashFinished2[WC_MAX_DIGEST_SIZE];
  19059. /* Expected */
  19060. byte clientEarlyTrafficSecret[WC_MAX_DIGEST_SIZE];
  19061. byte earlyExporterMasterSecret[WC_MAX_DIGEST_SIZE];
  19062. byte clientHandshakeTrafficSecret[WC_MAX_DIGEST_SIZE];
  19063. byte serverHandshakeTrafficSecret[WC_MAX_DIGEST_SIZE];
  19064. byte clientApplicationTrafficSecret[WC_MAX_DIGEST_SIZE];
  19065. byte serverApplicationTrafficSecret[WC_MAX_DIGEST_SIZE];
  19066. byte exporterMasterSecret[WC_MAX_DIGEST_SIZE];
  19067. byte resumptionMasterSecret[WC_MAX_DIGEST_SIZE];
  19068. } Tls13KdfTestVector;
  19069. /* The following tests come from the CAVP test vectors we used for
  19070. * our FIPS validation. The hash values used are the components from
  19071. * the test hashed together. hashHello1 is the hash of the
  19072. * clientHelloRandom value of the test vector. hashHello2 is the hash
  19073. * of the clientHelloRandom and serverHelloRandom values from the test
  19074. * vector. hashFinished1 is clientHelloRandom, serverHelloRandom, and
  19075. * serverFinishedRandom. hashFinished2 is clientHelloRandom,
  19076. * serverHelloRandom, serverFinishedRandom, and clietnFinishedRandom
  19077. * hashed together. */
  19078. static const Tls13KdfTestVector tls13KdfTestVectors[] = {
  19079. { /* 1 */
  19080. WC_HASH_TYPE_SHA256, 35, 35,
  19081. { /* PSK */
  19082. 0x7b, 0xf1, 0x05, 0x31, 0x36, 0xfa, 0x03, 0xdc,
  19083. 0x31, 0x97, 0x88, 0x04, 0x9c, 0xbc, 0xee, 0xf7,
  19084. 0x8d, 0x84, 0x95, 0x26, 0xaf, 0x1d, 0x68, 0xb0,
  19085. 0x60, 0x7a, 0xcc, 0x4f, 0xc1, 0xd3, 0xa1, 0x68,
  19086. 0x7f, 0x6d, 0xbe
  19087. },
  19088. { /* DHE */
  19089. 0x6e, 0xa1, 0x77, 0xab, 0x2f, 0x43, 0xd2, 0x4b,
  19090. 0xe5, 0xa1, 0x09, 0xe0, 0x7a, 0xd0, 0x01, 0x35,
  19091. 0x8d, 0xf8, 0xf2, 0x5c, 0x91, 0x02, 0xb0, 0x6c,
  19092. 0x3f, 0xeb, 0xee, 0xa4, 0x42, 0x19, 0xce, 0xdc,
  19093. 0x81, 0x26, 0x40
  19094. },
  19095. { /* Hello 1 */
  19096. 0xd9, 0x4b, 0xe4, 0x17, 0xef, 0x58, 0x73, 0x7d,
  19097. 0x28, 0x3d, 0xf0, 0xcc, 0x05, 0x03, 0xaf, 0xac,
  19098. 0x3d, 0x92, 0x79, 0x48, 0xe8, 0x8c, 0xdb, 0xce,
  19099. 0x95, 0x82, 0x21, 0x31, 0x7b, 0x61, 0xd7, 0xc6
  19100. },
  19101. { /* Hello 2 */
  19102. 0xb7, 0x7f, 0x29, 0x91, 0xa4, 0x8b, 0x34, 0xdb,
  19103. 0xbd, 0xc7, 0x54, 0x1c, 0x3b, 0x86, 0xa3, 0x69,
  19104. 0xfe, 0x26, 0xe4, 0x7b, 0xac, 0x57, 0x71, 0xb3,
  19105. 0x32, 0x97, 0xed, 0xd2, 0x0e, 0x95, 0xb8, 0x63
  19106. },
  19107. { /* Finished 1 */
  19108. 0x65, 0xdb, 0x6d, 0x71, 0x71, 0xd0, 0xd8, 0x49,
  19109. 0xd0, 0x3c, 0x8e, 0x2b, 0x24, 0xdf, 0xc2, 0xe9,
  19110. 0xd6, 0xfd, 0xea, 0x04, 0x95, 0x7c, 0xf0, 0x7e,
  19111. 0x57, 0x74, 0x7c, 0xdd, 0xa3, 0x0b, 0x2b, 0x36
  19112. },
  19113. { /* Finished 2 */
  19114. 0x28, 0xf2, 0xf2, 0x79, 0xcf, 0x20, 0x52, 0x90,
  19115. 0x1d, 0x91, 0x05, 0xad, 0x44, 0x26, 0x23, 0x96,
  19116. 0x32, 0xce, 0xec, 0x61, 0xd1, 0xbf, 0x00, 0x48,
  19117. 0x4a, 0xa5, 0x60, 0xcc, 0x28, 0xb5, 0x8d, 0x98
  19118. },
  19119. { /* Client Early Traffic Secret */
  19120. 0x07, 0x14, 0x6a, 0x26, 0x5b, 0x6c, 0x7f, 0x4d, 0x6b, 0x47, 0x3f, 0xd5,
  19121. 0x03, 0x1d, 0xd2, 0x23, 0x3d, 0x89, 0x3e, 0xc6, 0x51, 0xd1, 0xac, 0xf8,
  19122. 0x28, 0xae, 0x4b, 0x76, 0xc8, 0x10, 0x7e, 0xdd
  19123. },
  19124. { /* Early Exporter Master Secret */
  19125. 0xb8, 0xd3, 0x25, 0x7e, 0x2d, 0x41, 0x7b, 0xcb, 0x5e, 0x82, 0x49, 0xf5,
  19126. 0x51, 0x3d, 0xb7, 0x59, 0x32, 0xb3, 0xdf, 0x99, 0x4e, 0x04, 0x69, 0xc6,
  19127. 0x96, 0x8e, 0xe6, 0x3d, 0x91, 0xe4, 0x81, 0x11
  19128. },
  19129. { /* Client Handshake Traffic Secret */
  19130. 0xd9, 0x3b, 0x54, 0xe2, 0xb0, 0xd1, 0x85, 0xf0, 0xfd, 0xf3, 0x48, 0x4a,
  19131. 0xf8, 0x0b, 0xa5, 0xdc, 0x4c, 0x37, 0xcb, 0xd4, 0x20, 0xaf, 0x60, 0xc7,
  19132. 0xd5, 0x50, 0x5d, 0x0c, 0x77, 0x3b, 0x6f, 0xd2
  19133. },
  19134. { /* Server Handshake Traffic Secret */
  19135. 0x4d, 0x40, 0x2b, 0xd2, 0x8c, 0x33, 0x90, 0x39, 0x67, 0x67, 0x05, 0xf7,
  19136. 0x5d, 0x37, 0x1e, 0xdc, 0x4a, 0x70, 0x6b, 0x9e, 0xf8, 0x06, 0x61, 0x89,
  19137. 0x70, 0xe1, 0x3d, 0x36, 0xad, 0x88, 0x7e, 0x5b
  19138. },
  19139. { /* Client Application Traffic Secret */
  19140. 0x74, 0x6e, 0xa0, 0x13, 0x18, 0x34, 0x48, 0x4d, 0x23, 0x31, 0xf1, 0xf9,
  19141. 0xee, 0x44, 0x6d, 0xad, 0xc1, 0xad, 0x92, 0x73, 0xca, 0x27, 0x16, 0x91,
  19142. 0xa2, 0x50, 0x9a, 0xfc, 0xec, 0xf0, 0x6b, 0x24
  19143. },
  19144. { /* Server Application Traffic Secret */
  19145. 0x89, 0x18, 0x7e, 0x34, 0x8d, 0xfc, 0x14, 0xb1, 0x4f, 0x21, 0xd8, 0x29,
  19146. 0xdb, 0x9b, 0xfb, 0x55, 0xcf, 0xa1, 0x4f, 0x95, 0xf8, 0xe0, 0xb0, 0x83,
  19147. 0xd5, 0x34, 0x9e, 0x0b, 0x83, 0x37, 0x42, 0x93
  19148. },
  19149. { /* Exporter Master Secret */
  19150. 0x7d, 0xc8, 0x88, 0x46, 0xd5, 0x57, 0x15, 0xb6, 0x24, 0x25, 0x92, 0x61,
  19151. 0xb1, 0x18, 0x86, 0x2a, 0x6d, 0xa5, 0x84, 0xeb, 0x59, 0xdf, 0x13, 0xbd,
  19152. 0x73, 0xaa, 0x5d, 0x65, 0xab, 0xd9, 0xb4, 0x56
  19153. },
  19154. { /* Resumption Master Secret */
  19155. 0x20, 0xb7, 0xd0, 0xe3, 0x82, 0x01, 0xa1, 0x04, 0xb8, 0x13, 0x29, 0xed,
  19156. 0x35, 0xe4, 0x2f, 0xbf, 0x58, 0x23, 0x7f, 0x21, 0xdb, 0x9f, 0xf8, 0xe0,
  19157. 0xe8, 0xe4, 0xab, 0xc4, 0xa1, 0x61, 0xb9, 0xbb
  19158. }
  19159. },
  19160. { /* 6 */
  19161. WC_HASH_TYPE_SHA256, 0, 33,
  19162. { 0 }, /* PSK */
  19163. { /* DHE */
  19164. 0x7a, 0x46, 0x8c, 0x5a, 0xd1, 0x8e, 0x95, 0xba,
  19165. 0x61, 0xe6, 0x6f, 0xe6, 0x76, 0x0c, 0x20, 0x43,
  19166. 0x16, 0x82, 0x15, 0xfe, 0x54, 0xa3, 0xc7, 0xfd,
  19167. 0x3b, 0x2c, 0x88, 0xb4, 0xd3, 0x42, 0x70, 0x12,
  19168. 0x18
  19169. },
  19170. { /* Hello 1 */
  19171. 0x63, 0x83, 0x58, 0xab, 0x36, 0xcd, 0x0c, 0xf3,
  19172. 0x26, 0x07, 0xb5, 0x5f, 0x0b, 0x8b, 0x45, 0xd6,
  19173. 0x7d, 0x5b, 0x42, 0xdc, 0xa8, 0xaa, 0x06, 0xfb,
  19174. 0x20, 0xa5, 0xbb, 0x85, 0xdb, 0x54, 0xd8, 0x8b
  19175. },
  19176. { /* Hello 2 */
  19177. 0xea, 0xfe, 0x9e, 0x8e, 0xff, 0x1f, 0x6f, 0x43,
  19178. 0xf9, 0x5d, 0xfd, 0xbf, 0xe2, 0x5f, 0x02, 0x2f,
  19179. 0x6d, 0x47, 0x60, 0x9a, 0x48, 0x9a, 0x75, 0xfb,
  19180. 0xb5, 0x4a, 0xbf, 0x9c, 0x4e, 0xff, 0xbf, 0x0b
  19181. },
  19182. { /* Finished 1 */
  19183. 0xca, 0x25, 0xb3, 0x53, 0x8e, 0x6d, 0xc3, 0x36,
  19184. 0x17, 0x30, 0x07, 0xdf, 0x0d, 0xd7, 0x79, 0xb0,
  19185. 0x7f, 0xcb, 0xbe, 0x7a, 0xbc, 0x2d, 0x9f, 0x2d,
  19186. 0x94, 0x44, 0x94, 0xe6, 0xa4, 0xf3, 0xe8, 0x53
  19187. },
  19188. { /* Finished 2 */
  19189. 0x2e, 0xa6, 0x5a, 0xaf, 0xb5, 0xba, 0x9f, 0x2f,
  19190. 0x74, 0x83, 0x5d, 0xbf, 0x86, 0xa4, 0xa6, 0xf6,
  19191. 0xb9, 0x89, 0xdf, 0x17, 0xe1, 0xa8, 0x14, 0xc0,
  19192. 0xe1, 0x50, 0xfa, 0xec, 0xfa, 0xae, 0x8b, 0x7b
  19193. },
  19194. {
  19195. 0x20, 0x18, 0x72, 0x7c, 0xde, 0x3a, 0x85, 0x17, 0x72, 0xdc, 0xd7, 0x72,
  19196. 0xb0, 0xfc, 0x45, 0xd0, 0x62, 0xb9, 0xbb, 0x38, 0x69, 0x05, 0x7b, 0xb4,
  19197. 0x5e, 0x58, 0x5d, 0xed, 0xcd, 0x0b, 0x96, 0xd3
  19198. },
  19199. {
  19200. 0x68, 0x10, 0x20, 0xd1, 0x5e, 0xfc, 0x0c, 0x53, 0x85, 0xbb, 0xdb, 0x18,
  19201. 0xa8, 0x78, 0xf1, 0x2b, 0x13, 0xba, 0x64, 0x1d, 0xe7, 0x09, 0xbe, 0x13,
  19202. 0x49, 0x26, 0xf9, 0x98, 0x56, 0xf1, 0x43, 0xfb
  19203. },
  19204. {
  19205. 0x24, 0x35, 0x3e, 0x10, 0x6f, 0x39, 0x50, 0xd6, 0xa2, 0x12, 0x99, 0xf2,
  19206. 0xd5, 0xf5, 0x19, 0xf5, 0x84, 0xed, 0xee, 0x78, 0x2a, 0xa6, 0xfa, 0x3d,
  19207. 0x06, 0xa8, 0xa7, 0x5d, 0x97, 0x78, 0xd6, 0x58
  19208. },
  19209. {
  19210. 0xf4, 0x57, 0xac, 0x24, 0x7a, 0xfb, 0x7c, 0x3b, 0xb6, 0x39, 0x17, 0x14,
  19211. 0xd9, 0xd4, 0x58, 0x4d, 0x46, 0xd5, 0x1b, 0xde, 0xf7, 0x9d, 0x06, 0xee,
  19212. 0x8d, 0x1a, 0x2c, 0x25, 0x6d, 0x64, 0xde, 0x89
  19213. },
  19214. {
  19215. 0xb6, 0x00, 0xce, 0x63, 0xed, 0x65, 0x8b, 0x66, 0x66, 0x42, 0xc6, 0xbd,
  19216. 0x89, 0xc4, 0x71, 0x6f, 0xce, 0x28, 0xb2, 0xac, 0x97, 0x07, 0x5b, 0xea,
  19217. 0xb8, 0x1d, 0x4c, 0xeb, 0x9e, 0x71, 0x07, 0x8f
  19218. },
  19219. {
  19220. 0xf8, 0x92, 0xc8, 0xba, 0xe7, 0x83, 0xfe, 0x68, 0xe4, 0xd6, 0x5e, 0xcb,
  19221. 0xb3, 0xef, 0x49, 0xd0, 0xe7, 0xb1, 0xac, 0xcb, 0x39, 0x19, 0xfd, 0xa7,
  19222. 0xf7, 0xca, 0xab, 0x1e, 0x42, 0x14, 0xd8, 0xe7
  19223. },
  19224. {
  19225. 0x32, 0x4a, 0x1a, 0xad, 0xe2, 0xbb, 0x55, 0x8a, 0xdd, 0xe9, 0xa5, 0x2a,
  19226. 0x46, 0x5e, 0x6c, 0x83, 0x66, 0x27, 0x27, 0x94, 0xdd, 0x68, 0x59, 0xa0,
  19227. 0xbb, 0xe8, 0x31, 0x7c, 0x39, 0xd7, 0xfd, 0x6d
  19228. },
  19229. {
  19230. 0x58, 0xbc, 0x6c, 0x5b, 0x24, 0xad, 0x82, 0xb3, 0xcc, 0xc7, 0xd1, 0xa1,
  19231. 0xaa, 0x2b, 0x98, 0x9f, 0x2f, 0x7e, 0xa9, 0x63, 0xc2, 0x8e, 0xb6, 0x06,
  19232. 0xc2, 0x2b, 0x74, 0x4b, 0x79, 0x19, 0x7e, 0x2e
  19233. }
  19234. },
  19235. { /* 11 */
  19236. WC_HASH_TYPE_SHA256, 33, 0,
  19237. { /* PSK */
  19238. 0x3d, 0x39, 0x49, 0x36, 0x98, 0xc5, 0xfd, 0xcd,
  19239. 0xa0, 0x17, 0xbd, 0x65, 0x0a, 0xdb, 0xd4, 0x07,
  19240. 0x56, 0xa2, 0x7b, 0xb8, 0x2a, 0x7e, 0xfb, 0x26,
  19241. 0x74, 0xe1, 0xbc, 0x08, 0x4b, 0xf0, 0x30, 0x14,
  19242. 0x12
  19243. },
  19244. { 0 }, /* DHE */
  19245. { /* Hello 1 */
  19246. 0xb7, 0x44, 0x74, 0x6c, 0x57, 0x1f, 0xf3, 0x84,
  19247. 0x8f, 0x63, 0xfb, 0x8c, 0x94, 0x6c, 0x16, 0x68,
  19248. 0x4b, 0xe1, 0xb5, 0xb5, 0x2a, 0x4e, 0x5f, 0xdf,
  19249. 0x4b, 0x53, 0xb2, 0x35, 0xfc, 0x30, 0xf1, 0x36
  19250. },
  19251. { /* Hello 2 */
  19252. 0xe6, 0x4f, 0x3a, 0x4f, 0xd7, 0xe0, 0x64, 0xd4,
  19253. 0x69, 0x50, 0xe4, 0x8b, 0xba, 0xbc, 0x47, 0x74,
  19254. 0xa7, 0x9b, 0x40, 0x91, 0x8f, 0xa8, 0x72, 0x22,
  19255. 0x97, 0xad, 0x43, 0xa7, 0x11, 0x86, 0xb5, 0x72
  19256. },
  19257. { /* Finished 1 */
  19258. 0x5f, 0xa6, 0x10, 0xe2, 0xa3, 0x99, 0x0b, 0x5e,
  19259. 0x57, 0xee, 0xc3, 0x3a, 0x8e, 0x04, 0xf3, 0x0e,
  19260. 0x58, 0x02, 0x09, 0xb2, 0x7e, 0x2d, 0xc6, 0xd2,
  19261. 0x08, 0xae, 0x68, 0x0a, 0x55, 0xa5, 0xda, 0x51
  19262. },
  19263. { /* Finished 2 */
  19264. 0xfc, 0x5b, 0xc0, 0x7e, 0x1b, 0xaa, 0xc0, 0xb4,
  19265. 0x34, 0x85, 0x49, 0x8e, 0x16, 0x31, 0x98, 0xdf,
  19266. 0x10, 0x54, 0x22, 0xda, 0x1e, 0x6b, 0x51, 0xf6,
  19267. 0x97, 0x57, 0xa0, 0x7a, 0x92, 0xe7, 0x47, 0x52
  19268. },
  19269. {
  19270. 0x80, 0xfa, 0x36, 0x30, 0xb8, 0x65, 0xb3, 0x2a, 0x1d, 0x68, 0x91, 0x06,
  19271. 0x98, 0xa0, 0x17, 0x8f, 0xee, 0xb7, 0x9e, 0x3d, 0xd8, 0x84, 0x99, 0x30,
  19272. 0xb9, 0xd6, 0x09, 0x25, 0x5e, 0xfb, 0x8f, 0xd3 },
  19273. {
  19274. 0xa9, 0x89, 0x29, 0x70, 0xe4, 0x55, 0xec, 0x97, 0xfb, 0x24, 0x5b, 0xf9,
  19275. 0xf1, 0xa3, 0x19, 0x3d, 0xf1, 0x31, 0x14, 0xcd, 0x2a, 0xed, 0x21, 0xc8,
  19276. 0xb1, 0x53, 0xad, 0x11, 0x0b, 0x9e, 0x5a, 0xee },
  19277. {
  19278. 0x72, 0xad, 0x8d, 0x7f, 0xfc, 0xb7, 0x68, 0xda, 0x27, 0x60, 0x37, 0xa3,
  19279. 0x4a, 0x63, 0xe8, 0xa5, 0xc8, 0xcd, 0x36, 0x6a, 0x77, 0x99, 0x0d, 0xa9,
  19280. 0xb1, 0x5b, 0x2f, 0x47, 0x2e, 0x22, 0xa7, 0x5e },
  19281. {
  19282. 0x95, 0x6e, 0x85, 0x09, 0xe5, 0x04, 0x88, 0x14, 0x28, 0x8d, 0xdf, 0xe6,
  19283. 0x0d, 0x0f, 0x0d, 0x6b, 0x4e, 0x66, 0x1c, 0x03, 0xb9, 0xaa, 0x2d, 0x45,
  19284. 0x56, 0x67, 0x5c, 0x55, 0x29, 0xd6, 0x89, 0xd0 },
  19285. {
  19286. 0xe8, 0xf2, 0x14, 0xf9, 0x9b, 0x2b, 0x9f, 0x24, 0x2b, 0x37, 0xbe, 0x86,
  19287. 0xdb, 0x23, 0x4b, 0xbe, 0x39, 0x57, 0xe8, 0xa9, 0xa5, 0xee, 0x08, 0xf2,
  19288. 0x75, 0x58, 0xdb, 0xd9, 0x51, 0xc1, 0x46, 0x02 },
  19289. {
  19290. 0x3d, 0x19, 0xaf, 0xa3, 0x0b, 0x21, 0xf7, 0x3d, 0xe7, 0x37, 0x6e, 0x32,
  19291. 0x13, 0x48, 0x9d, 0xea, 0xe0, 0x90, 0xbf, 0x64, 0x48, 0xf7, 0x1e, 0xcc,
  19292. 0xf0, 0xbc, 0x92, 0xd7, 0x8a, 0x4a, 0xa8, 0xc1 },
  19293. {
  19294. 0x16, 0x35, 0xb1, 0x66, 0x28, 0xa3, 0x3e, 0x19, 0xf5, 0x2d, 0x92, 0x22,
  19295. 0x95, 0x48, 0xe8, 0x34, 0x7b, 0x30, 0x50, 0xa2, 0xa0, 0xd9, 0xc2, 0x59,
  19296. 0x39, 0xf9, 0x8c, 0x69, 0xf2, 0x2a, 0xb9, 0xff },
  19297. {
  19298. 0x32, 0x71, 0xa6, 0x87, 0x0c, 0x97, 0x42, 0x07, 0xdd, 0x5f, 0xc9, 0x44,
  19299. 0xa5, 0x7c, 0x50, 0x14, 0xfd, 0xe7, 0x5f, 0x8b, 0xd3, 0x2f, 0xdc, 0x9b,
  19300. 0xa9, 0x93, 0x22, 0x19, 0xe6, 0xf2, 0x0c, 0xd8 }
  19301. },
  19302. #ifdef WOLFSSL_SHA384
  19303. { /* 26 */
  19304. WC_HASH_TYPE_SHA384, 35, 35,
  19305. { /* PSK */
  19306. 0x62, 0x83, 0x25, 0xc7, 0xcc, 0x08, 0x5e, 0x63,
  19307. 0x64, 0x56, 0xf0, 0xc6, 0x88, 0x27, 0x5a, 0x5b,
  19308. 0x68, 0x59, 0x0b, 0x14, 0x55, 0x13, 0x2e, 0xfd,
  19309. 0x8f, 0x28, 0x5b, 0x3d, 0xe3, 0xad, 0x67, 0xe4,
  19310. 0x68, 0xba, 0xf9
  19311. },
  19312. { /* DHE */
  19313. 0xa8, 0xb1, 0xab, 0xd8, 0xc8, 0x5b, 0x52, 0xdf,
  19314. 0x7f, 0x49, 0x10, 0xf4, 0xa1, 0x31, 0xd1, 0x91,
  19315. 0x36, 0xc1, 0x87, 0x5d, 0x42, 0x2a, 0xe7, 0x1d,
  19316. 0x2c, 0x29, 0x3d, 0x40, 0x64, 0x61, 0x63, 0x76,
  19317. 0xd8, 0x66, 0xac
  19318. },
  19319. { /* Hello 1 */
  19320. 0x6f, 0xc6, 0x4c, 0xe1, 0xc6, 0x68, 0x34, 0x8c,
  19321. 0x0a, 0xe1, 0xf8, 0xb8, 0x3e, 0xd4, 0xf8, 0x0b,
  19322. 0x54, 0x50, 0xe4, 0xc5, 0x4a, 0x33, 0x7d, 0xbd,
  19323. 0x90, 0xd2, 0xa2, 0xb9, 0xb7, 0x92, 0xed, 0xab,
  19324. 0x14, 0xf1, 0xe4, 0x86, 0x22, 0x67, 0xd7, 0x44,
  19325. 0x03, 0x21, 0xdc, 0x51, 0x52, 0x7f, 0x35, 0x80
  19326. },
  19327. { /* Hello 2 */
  19328. 0x3e, 0xcf, 0x2f, 0xc3, 0x87, 0xba, 0xc5, 0xbd,
  19329. 0x7c, 0xe8, 0x35, 0x5b, 0x95, 0x51, 0x30, 0x3b,
  19330. 0x08, 0xcc, 0x2a, 0x7d, 0xb5, 0x74, 0x7c, 0x16,
  19331. 0xb3, 0x0b, 0xe7, 0x61, 0xa3, 0x7c, 0x6c, 0xbd,
  19332. 0x39, 0x74, 0xfd, 0x1e, 0x4c, 0xff, 0xc8, 0xcc,
  19333. 0xa0, 0xef, 0x29, 0x4d, 0x94, 0xaa, 0x55, 0x6f,
  19334. },
  19335. { /* Finished 1 */
  19336. 0x06, 0xc1, 0x47, 0x78, 0x66, 0x53, 0x6f, 0x24,
  19337. 0x94, 0x61, 0x69, 0xec, 0xd8, 0x60, 0x31, 0x2f,
  19338. 0xbf, 0xd6, 0x8a, 0x29, 0x17, 0xff, 0xa3, 0x88,
  19339. 0x13, 0x09, 0x8c, 0x9d, 0x6c, 0x64, 0x84, 0x48,
  19340. 0x44, 0xdd, 0x2d, 0x29, 0x4d, 0xe6, 0x98, 0x2b,
  19341. 0x45, 0x3b, 0x84, 0x33, 0x79, 0xb2, 0x75, 0x68
  19342. },
  19343. { /* Finished 2 */
  19344. 0x28, 0x1e, 0x18, 0xf7, 0x9c, 0x32, 0xa9, 0xbf,
  19345. 0x0c, 0x24, 0x58, 0x21, 0xce, 0xbc, 0xf2, 0x44,
  19346. 0xb1, 0x18, 0xaf, 0x9d, 0xd9, 0x20, 0xf9, 0xf4,
  19347. 0xed, 0xcc, 0x53, 0x82, 0x66, 0x5c, 0x46, 0x94,
  19348. 0x8c, 0x36, 0x5e, 0xca, 0x9f, 0xd8, 0x9a, 0xd3,
  19349. 0xf0, 0xe1, 0x53, 0x71, 0xdd, 0x19, 0x1e, 0x59
  19350. },
  19351. {
  19352. 0xd0, 0xef, 0xa8, 0xcb, 0x5b, 0x14, 0x0f, 0x0a, 0x62, 0xba, 0x5a, 0xb1,
  19353. 0xc5, 0xb5, 0x3f, 0x11, 0xda, 0xa1, 0x0c, 0x9c, 0xb4, 0x32, 0x48, 0x4e,
  19354. 0xfa, 0x84, 0x4f, 0xe4, 0xe7, 0x91, 0x8f, 0x42, 0x3f, 0xc7, 0x4e, 0xd3,
  19355. 0x83, 0x3d, 0x7f, 0x70, 0x12, 0xee, 0x9a, 0x37, 0x01, 0xbb, 0x14, 0xd3
  19356. },
  19357. {
  19358. 0x48, 0x6f, 0x77, 0x1d, 0x39, 0x1b, 0xa5, 0x9a, 0x76, 0xd9, 0x1d, 0x7d,
  19359. 0xb3, 0xd9, 0xb9, 0x78, 0x35, 0x0f, 0xd0, 0xe1, 0x07, 0x1f, 0x8d, 0xe5,
  19360. 0x75, 0x00, 0xda, 0xc0, 0x19, 0x01, 0xfb, 0x08, 0x35, 0xe7, 0x18, 0x8f,
  19361. 0xf0, 0x19, 0xfb, 0x46, 0xf6, 0xa5, 0x77, 0x0e, 0x90, 0x38, 0x8b, 0x15
  19362. },
  19363. {
  19364. 0x80, 0x8c, 0xa7, 0x24, 0x97, 0xf9, 0xd3, 0x52, 0xb0, 0x69, 0x9d, 0x4b,
  19365. 0xa4, 0x19, 0x4a, 0xb1, 0x46, 0x53, 0x3a, 0xc8, 0xe4, 0x02, 0x69, 0xf2,
  19366. 0xe7, 0xb6, 0x1d, 0x33, 0x51, 0xcc, 0x14, 0x40, 0x4a, 0xb0, 0xe7, 0x58,
  19367. 0x84, 0xba, 0xc2, 0x14, 0x58, 0x6b, 0xb9, 0xdc, 0x50, 0x98, 0x67, 0x01
  19368. },
  19369. {
  19370. 0xb1, 0xa8, 0xc0, 0x06, 0xb3, 0x2e, 0xa7, 0x8a, 0x6a, 0x12, 0x88, 0x00,
  19371. 0x65, 0x88, 0x9c, 0x5d, 0x35, 0xee, 0xe5, 0x51, 0x0b, 0x62, 0xf8, 0x67,
  19372. 0xe5, 0xef, 0x15, 0x1f, 0x23, 0x02, 0x74, 0x08, 0x9c, 0xc8, 0xba, 0x27,
  19373. 0x5d, 0x32, 0x19, 0x6f, 0x6d, 0x5d, 0x72, 0x5e, 0x15, 0xde, 0x30, 0xc3
  19374. },
  19375. {
  19376. 0xfd, 0xce, 0xf5, 0x65, 0x45, 0x84, 0xfb, 0x8c, 0x79, 0xa4, 0x6c, 0x1b,
  19377. 0x0e, 0x1b, 0xfd, 0x26, 0xa2, 0x53, 0xf4, 0x4e, 0x00, 0x4d, 0x4b, 0x0b,
  19378. 0x24, 0x6d, 0x35, 0x35, 0xd9, 0x97, 0x70, 0xc5, 0xf4, 0xee, 0xe3, 0xba,
  19379. 0x31, 0x1e, 0x2a, 0x42, 0xcb, 0xdf, 0x40, 0xb1, 0x14, 0xb8, 0x53, 0xce
  19380. },
  19381. {
  19382. 0xbb, 0xb3, 0x26, 0x7c, 0x22, 0x21, 0x9b, 0x72, 0x32, 0xa1, 0x97, 0xfb,
  19383. 0x78, 0x8c, 0xbe, 0x3d, 0x71, 0x45, 0xb8, 0xf5, 0x24, 0x8f, 0x0f, 0xac,
  19384. 0x42, 0x5b, 0x81, 0xe8, 0xd0, 0x71, 0x4a, 0xcb, 0x32, 0x3f, 0x03, 0xfb,
  19385. 0xec, 0x6a, 0x1f, 0x76, 0x80, 0x65, 0x01, 0x7a, 0x3d, 0xce, 0xc4, 0xdf
  19386. },
  19387. {
  19388. 0x3f, 0xcf, 0x2f, 0x63, 0x94, 0x94, 0x99, 0xfd, 0x04, 0x3a, 0x89, 0x83,
  19389. 0xcf, 0x06, 0x05, 0xec, 0x20, 0x3e, 0x5f, 0x51, 0x9d, 0x6e, 0x4a, 0xc6,
  19390. 0xf1, 0x2b, 0x37, 0x17, 0x34, 0x72, 0x6e, 0x1d, 0x2a, 0xfd, 0xc7, 0x73,
  19391. 0xb5, 0x07, 0x22, 0x81, 0x32, 0x2e, 0x21, 0x85, 0xaf, 0x10, 0xb2, 0x73
  19392. },
  19393. {
  19394. 0x52, 0x0c, 0x3d, 0x2e, 0x2d, 0x4a, 0x11, 0xae, 0x96, 0x78, 0xe9, 0x5b,
  19395. 0xd8, 0x0f, 0x6c, 0xf4, 0xbd, 0x96, 0x13, 0x55, 0x88, 0xdd, 0xa3, 0x67,
  19396. 0x36, 0x86, 0x1e, 0x0b, 0x36, 0x41, 0xec, 0xf6, 0x04, 0xb2, 0xc4, 0x16,
  19397. 0xbc, 0x2c, 0xdb, 0x30, 0x02, 0x94, 0xd4, 0x42, 0xbf, 0x38, 0xee, 0x9d
  19398. }
  19399. },
  19400. { /* 36 */
  19401. WC_HASH_TYPE_SHA384, 0, 33,
  19402. { 0 }, /* PSK */
  19403. { /* DHE */
  19404. 0xd3, 0x00, 0x72, 0x9a, 0xa8, 0xc5, 0xf3, 0xc4,
  19405. 0xf1, 0xa0, 0x26, 0x89, 0x65, 0x70, 0xc7, 0x0b,
  19406. 0x77, 0xbb, 0xe1, 0x4b, 0x2b, 0xa8, 0x4f, 0xa6,
  19407. 0x09, 0x4b, 0xba, 0x45, 0x36, 0x15, 0xee, 0x68,
  19408. 0xfd
  19409. },
  19410. { /* Hello 1 */
  19411. 0x10, 0x9d, 0x8b, 0xa2, 0x93, 0xe7, 0xd3, 0xb9,
  19412. 0xb4, 0x0f, 0xeb, 0x6a, 0xb9, 0x69, 0xcb, 0x39,
  19413. 0x16, 0x29, 0xcc, 0xd3, 0xcc, 0x1a, 0x4c, 0x1b,
  19414. 0x53, 0x7c, 0x33, 0x88, 0x06, 0xbc, 0x0a, 0x02,
  19415. 0xa0, 0xbe, 0x62, 0xc0, 0xe6, 0x5e, 0x97, 0x5b,
  19416. 0x6a, 0xa1, 0x98, 0xf3, 0xd2, 0x1e, 0xcd, 0xc5
  19417. },
  19418. { /* Hello 2 */
  19419. 0x74, 0xc0, 0x07, 0x2c, 0xc1, 0x63, 0xcc, 0x11,
  19420. 0xad, 0x1a, 0x55, 0x63, 0xbc, 0x20, 0x77, 0x96,
  19421. 0x30, 0x1c, 0x68, 0x45, 0x1e, 0x9b, 0xa7, 0xb4,
  19422. 0xf3, 0x04, 0x45, 0x16, 0x76, 0x55, 0xf9, 0xdf,
  19423. 0x4b, 0x2f, 0x1a, 0xdf, 0x5a, 0xb0, 0x93, 0xc9,
  19424. 0xab, 0xf5, 0x32, 0x47, 0x79, 0x9c, 0x01, 0xeb
  19425. },
  19426. { /* Finished 1 */
  19427. 0x27, 0x08, 0x8e, 0xa5, 0xf1, 0x30, 0xe1, 0xd6,
  19428. 0x4f, 0xa2, 0x9e, 0x3b, 0x03, 0x2d, 0x2e, 0xa3,
  19429. 0x84, 0x75, 0x51, 0x3a, 0xc3, 0xf6, 0xee, 0x2e,
  19430. 0x37, 0x0c, 0xe3, 0x28, 0x46, 0xa5, 0x2d, 0xc7,
  19431. 0xf0, 0x64, 0x78, 0x53, 0x66, 0x43, 0x02, 0xa4,
  19432. 0x7a, 0x43, 0x66, 0x4b, 0xa7, 0xcb, 0x97, 0x16
  19433. },
  19434. { /* Finished 2 */
  19435. 0x1d, 0x0d, 0xf8, 0xe1, 0x81, 0xa5, 0xbd, 0xa8,
  19436. 0x6f, 0x9d, 0x01, 0xa4, 0x9a, 0x92, 0xe2, 0xef,
  19437. 0x08, 0xab, 0xef, 0x3e, 0x2d, 0xd4, 0x82, 0xac,
  19438. 0x68, 0x9d, 0xe0, 0x54, 0x17, 0xde, 0x1a, 0xed,
  19439. 0x57, 0xcb, 0xd9, 0x2d, 0xc8, 0xbc, 0x93, 0xe6,
  19440. 0xa3, 0xec, 0xde, 0xee, 0xa1, 0x1c, 0x41, 0x85
  19441. },
  19442. {
  19443. 0x7f, 0x1f, 0xe6, 0x7b, 0xd8, 0xf5, 0x2b, 0x37, 0xbe, 0xb7, 0xd0, 0x37,
  19444. 0xce, 0x46, 0xad, 0x04, 0x2f, 0xc7, 0xdb, 0xc9, 0x9a, 0xb6, 0x00, 0x3f,
  19445. 0xc1, 0x97, 0xe9, 0x5c, 0x5e, 0x14, 0xd1, 0x38, 0x4d, 0x55, 0xe1, 0x07,
  19446. 0xb5, 0x85, 0x6d, 0xfa, 0xa7, 0x66, 0xad, 0xfa, 0xb6, 0xad, 0x29, 0x44
  19447. },
  19448. {
  19449. 0x4e, 0x6b, 0x20, 0x99, 0x55, 0x1b, 0x21, 0x89, 0xb6, 0x70, 0xdb, 0xe8,
  19450. 0xa7, 0x16, 0x55, 0xf2, 0x93, 0x13, 0x90, 0x7d, 0xfa, 0x62, 0x65, 0x53,
  19451. 0xa0, 0x97, 0xe9, 0xb4, 0xc0, 0xf1, 0xc9, 0x1a, 0x67, 0xdd, 0xca, 0x57,
  19452. 0xbc, 0xca, 0x39, 0xe6, 0x39, 0x6b, 0x63, 0x47, 0x25, 0x08, 0x3a, 0xd7
  19453. },
  19454. {
  19455. 0x35, 0x0d, 0xac, 0xd8, 0x10, 0x6a, 0x46, 0x50, 0x66, 0xae, 0x02, 0xc9,
  19456. 0xde, 0x13, 0x48, 0xce, 0x53, 0xd4, 0x92, 0x62, 0xc5, 0x65, 0x10, 0x08,
  19457. 0xc2, 0xc2, 0x82, 0xed, 0x9d, 0xc9, 0x6f, 0xa8, 0xc3, 0xc1, 0x0b, 0x7c,
  19458. 0xe1, 0x97, 0x85, 0xd6, 0x46, 0x29, 0x0e, 0x42, 0x51, 0xc1, 0x35, 0xcf
  19459. },
  19460. {
  19461. 0x3d, 0x5d, 0x84, 0xbd, 0x16, 0x46, 0x34, 0xb3, 0xf6, 0x31, 0x49, 0x3e,
  19462. 0x8d, 0xdc, 0xcb, 0x8c, 0x6a, 0x42, 0xf4, 0x88, 0xfc, 0x19, 0xfa, 0xa2,
  19463. 0x25, 0xc7, 0xa0, 0xa4, 0xca, 0xf0, 0xea, 0x2d, 0xe8, 0xc4, 0x02, 0x14,
  19464. 0x63, 0xfb, 0xd3, 0x7b, 0x51, 0x1c, 0xce, 0xca, 0xa3, 0xc3, 0xe4, 0xa5
  19465. },
  19466. {
  19467. 0x7c, 0x3a, 0x55, 0x92, 0x2e, 0xdd, 0x75, 0xdd, 0x76, 0x54, 0x4a, 0x9f,
  19468. 0xd0, 0xa2, 0x88, 0x83, 0xe9, 0x27, 0xda, 0x30, 0xe9, 0x96, 0x58, 0xc5,
  19469. 0xb7, 0x56, 0xfc, 0x4b, 0xb8, 0x5d, 0xee, 0x46, 0x70, 0x4e, 0x1b, 0x06,
  19470. 0x86, 0xaf, 0x48, 0x5c, 0x17, 0x35, 0xfa, 0x69, 0xc2, 0x4d, 0xfb, 0x09
  19471. },
  19472. {
  19473. 0x00, 0x0e, 0x28, 0x51, 0xc1, 0x7f, 0x41, 0x89, 0x6f, 0x9a, 0xca, 0x15,
  19474. 0xee, 0xed, 0x43, 0xca, 0x6d, 0x65, 0x6f, 0x51, 0x18, 0x6c, 0x08, 0x4b,
  19475. 0x77, 0xca, 0x75, 0xc4, 0xc3, 0xde, 0x29, 0x41, 0x8b, 0xaf, 0xa7, 0x1c,
  19476. 0x28, 0x37, 0xa0, 0xa0, 0x74, 0x8e, 0x09, 0x42, 0x7a, 0x1b, 0x68, 0xdb
  19477. },
  19478. {
  19479. 0x14, 0x8f, 0xab, 0x28, 0x64, 0xea, 0x45, 0x88, 0xdb, 0xc1, 0xc6, 0xa0,
  19480. 0x48, 0xdf, 0x15, 0xd0, 0x28, 0x07, 0x2d, 0x6c, 0xb8, 0x42, 0xbb, 0x60,
  19481. 0x02, 0x08, 0x9e, 0x29, 0x9b, 0x8d, 0xd6, 0x1c, 0xaf, 0xf2, 0x1a, 0xdc,
  19482. 0xf0, 0x78, 0x0b, 0x4d, 0x90, 0xa1, 0x0c, 0xb3, 0x13, 0xde, 0xca, 0x5a
  19483. },
  19484. {
  19485. 0x4d, 0x80, 0x7d, 0x0b, 0xb9, 0x00, 0x6f, 0x65, 0x51, 0x65, 0x23, 0xde,
  19486. 0x72, 0xdc, 0x4f, 0x04, 0xa5, 0xa2, 0x90, 0x45, 0x51, 0x9e, 0xd0, 0x3a,
  19487. 0xe4, 0xd7, 0x78, 0xa3, 0x0f, 0x2d, 0x65, 0x12, 0xad, 0xc8, 0x92, 0x30,
  19488. 0x79, 0x9d, 0x9d, 0x08, 0x7a, 0x9c, 0x9f, 0x83, 0xb1, 0xca, 0x59, 0x56
  19489. }
  19490. },
  19491. { /* 41 */
  19492. WC_HASH_TYPE_SHA384, 33, 0,
  19493. { /* PSK */
  19494. 0xa4, 0x8b, 0x1b, 0x5f, 0xd0, 0xea, 0x75, 0x62,
  19495. 0x06, 0x4d, 0x68, 0x40, 0x85, 0x20, 0x45, 0x95,
  19496. 0x4a, 0x00, 0xca, 0x05, 0xeb, 0xd4, 0x1d, 0x48,
  19497. 0x81, 0x89, 0xe8, 0x86, 0x43, 0xfa, 0x28, 0x17,
  19498. 0x12
  19499. },
  19500. { 0 }, /* DHE */
  19501. { /* Hello 1 */
  19502. 0x03, 0x7c, 0x33, 0x75, 0xdc, 0xc5, 0x46, 0x3a,
  19503. 0x0d, 0x56, 0xc6, 0xfb, 0xab, 0x1e, 0x1d, 0xda,
  19504. 0x59, 0xc2, 0xb2, 0xb1, 0x7c, 0x48, 0x9b, 0x06,
  19505. 0x0a, 0x5a, 0xbb, 0xf8, 0x98, 0x53, 0x78, 0x2d,
  19506. 0xd2, 0xcc, 0x87, 0x68, 0x25, 0xdd, 0x88, 0x22,
  19507. 0xcd, 0xb7, 0x74, 0x55, 0x21, 0xf9, 0x34, 0x98
  19508. },
  19509. { /* Hello 2 */
  19510. 0x03, 0xb4, 0xfb, 0xcc, 0x28, 0x2c, 0xc1, 0x70,
  19511. 0x42, 0x73, 0x57, 0xac, 0xdb, 0x47, 0x71, 0xf6,
  19512. 0x2e, 0x11, 0x8a, 0x5b, 0x47, 0x2f, 0x02, 0x54,
  19513. 0x95, 0x34, 0xed, 0x5f, 0x19, 0xc1, 0x75, 0xe0,
  19514. 0x76, 0xad, 0xb0, 0x90, 0x57, 0xcd, 0xfd, 0xd7,
  19515. 0x58, 0x1f, 0x0d, 0x6b, 0x9e, 0x51, 0x3c, 0x08
  19516. },
  19517. { /* Finished 1 */
  19518. 0x2b, 0x50, 0xd9, 0xa7, 0x43, 0x24, 0xda, 0x2c,
  19519. 0x7a, 0xaa, 0x0e, 0x37, 0xd7, 0x6b, 0x2c, 0xab,
  19520. 0x8e, 0xb2, 0xfe, 0x31, 0x1b, 0xa8, 0x12, 0x59,
  19521. 0x5b, 0x7b, 0xdc, 0x3e, 0xa7, 0x86, 0xa5, 0x48,
  19522. 0xe4, 0x46, 0x2b, 0x4c, 0xc1, 0x66, 0x4b, 0xf3,
  19523. 0x2a, 0x99, 0x93, 0x08, 0xbc, 0x3d, 0x08, 0x76
  19524. },
  19525. { /* Finished 2 */
  19526. 0x7c, 0x34, 0xc8, 0x56, 0x17, 0xf1, 0x62, 0x1c,
  19527. 0x9f, 0x0b, 0xeb, 0xfd, 0x69, 0x72, 0x51, 0xc5,
  19528. 0xfa, 0x74, 0x87, 0xc9, 0xbd, 0x50, 0xe9, 0x48,
  19529. 0xa7, 0x3c, 0x94, 0x3e, 0x06, 0x7d, 0xe8, 0x8e,
  19530. 0xc1, 0xd1, 0x08, 0x1f, 0x5d, 0x48, 0x8a, 0x25,
  19531. 0xfc, 0xea, 0xe7, 0xd9, 0xd4, 0xd0, 0xf9, 0xad
  19532. },
  19533. {
  19534. 0x4b, 0x0b, 0xed, 0xb9, 0xc8, 0xb8, 0xa8, 0x1e, 0xb0, 0x81, 0x76, 0xd5,
  19535. 0x33, 0x22, 0x71, 0x33, 0x3a, 0x85, 0x19, 0x67, 0x7e, 0x91, 0x37, 0xf2,
  19536. 0xa6, 0x11, 0x22, 0xdf, 0x41, 0x04, 0x3d, 0xa9, 0x13, 0xb9, 0xb2, 0xb1,
  19537. 0xbb, 0xd8, 0xef, 0x23, 0x7c, 0xc2, 0xab, 0x70, 0x1b, 0x51, 0x9f, 0xc9
  19538. },
  19539. {
  19540. 0xeb, 0x96, 0x10, 0x8c, 0x7d, 0x92, 0xea, 0x80, 0x86, 0xb2, 0xf8, 0x27,
  19541. 0xf2, 0x9a, 0x09, 0xc1, 0x7c, 0x09, 0x43, 0xbc, 0xfe, 0xc8, 0x75, 0xe0,
  19542. 0x97, 0xe7, 0x6d, 0xd5, 0xb2, 0x3c, 0xed, 0x12, 0xb7, 0x74, 0x0e, 0xe3,
  19543. 0xb6, 0xe0, 0xba, 0xe1, 0x8d, 0x89, 0xcf, 0x4f, 0x57, 0xf6, 0x6d, 0x90
  19544. },
  19545. {
  19546. 0x22, 0xb0, 0x39, 0x34, 0xb6, 0x6c, 0x2d, 0x7a, 0x97, 0x1c, 0x5d, 0xcc,
  19547. 0x78, 0x84, 0x71, 0xbb, 0xc6, 0x7b, 0xb6, 0xbc, 0xcc, 0x0b, 0xf8, 0xac,
  19548. 0x8e, 0xd7, 0x20, 0xbd, 0xbe, 0x32, 0xf0, 0xd6, 0xe9, 0x69, 0x13, 0xf2,
  19549. 0x9a, 0xce, 0xfe, 0x86, 0xd3, 0xee, 0xba, 0x69, 0x51, 0xb6, 0x77, 0x56
  19550. },
  19551. {
  19552. 0x16, 0xfd, 0xda, 0xf3, 0x5e, 0xb9, 0xa6, 0x17, 0x24, 0xb2, 0x16, 0x9f,
  19553. 0xb6, 0x59, 0x13, 0x0f, 0x25, 0x5a, 0xf1, 0x5b, 0x5f, 0xe4, 0x54, 0x2a,
  19554. 0xa7, 0xbf, 0x29, 0xaf, 0x5a, 0x77, 0xf4, 0x4f, 0x25, 0xba, 0x94, 0xad,
  19555. 0x6b, 0x91, 0x3b, 0xe7, 0xd5, 0x73, 0x0d, 0xff, 0xaa, 0xe3, 0x72, 0x2c
  19556. },
  19557. {
  19558. 0x22, 0xb4, 0x94, 0xc0, 0x53, 0xd7, 0x82, 0x06, 0x38, 0x9d, 0x4a, 0xa0,
  19559. 0x3f, 0xf1, 0x5f, 0x6e, 0x23, 0x8d, 0x09, 0x62, 0xbf, 0x6f, 0x7c, 0x84,
  19560. 0xc6, 0x3e, 0x15, 0xad, 0x18, 0x37, 0x76, 0x29, 0xc7, 0xd6, 0x68, 0x0c,
  19561. 0x1e, 0xc6, 0x93, 0x31, 0xef, 0x85, 0x69, 0x30, 0x68, 0xf0, 0x1e, 0x37
  19562. },
  19563. {
  19564. 0x6d, 0x4d, 0x20, 0xaf, 0x47, 0xe8, 0x1b, 0xfa, 0xd0, 0xb6, 0xc8, 0x97,
  19565. 0xd1, 0x03, 0xfc, 0x9d, 0x59, 0xa0, 0x68, 0x9d, 0xe9, 0x17, 0x8b, 0xce,
  19566. 0x48, 0x2c, 0x77, 0x8a, 0x22, 0x4b, 0x5c, 0x54, 0x22, 0xa1, 0x15, 0x12,
  19567. 0xe1, 0x07, 0x8e, 0x15, 0xd8, 0x7b, 0x16, 0x65, 0x99, 0x6b, 0xcb, 0x71
  19568. },
  19569. {
  19570. 0x79, 0x64, 0x79, 0xdd, 0x75, 0x5c, 0x6f, 0x98, 0xac, 0x03, 0xe0, 0xcd,
  19571. 0x92, 0xba, 0x0e, 0x2d, 0xb4, 0xd1, 0x8b, 0x97, 0xd0, 0x85, 0xbb, 0x2e,
  19572. 0x4f, 0x26, 0x93, 0xf5, 0x1d, 0xf3, 0xd2, 0x43, 0x4f, 0xd2, 0x47, 0xaa,
  19573. 0x91, 0x1e, 0xf3, 0x67, 0x10, 0x18, 0x2c, 0xb9, 0x01, 0xba, 0x10, 0x9f
  19574. },
  19575. {
  19576. 0x79, 0xb6, 0x9c, 0xbe, 0xf1, 0x6a, 0xb0, 0x92, 0xa0, 0x29, 0x52, 0x61,
  19577. 0xf1, 0xcd, 0x3a, 0x67, 0xe1, 0x6b, 0xb8, 0x9d, 0x0d, 0x95, 0xb6, 0x03,
  19578. 0x80, 0x1f, 0xd5, 0x75, 0xb6, 0x1d, 0x79, 0x02, 0x93, 0x43, 0x77, 0xa7,
  19579. 0x9d, 0x2f, 0xc3, 0x84, 0xc6, 0x83, 0x76, 0x16, 0x06, 0x98, 0x7b, 0x79
  19580. }
  19581. },
  19582. #endif /* WOLFSSL_SHA384 */
  19583. };
  19584. const char protocolLabel[] = "tls13 ";
  19585. const char ceTrafficLabel[] = "c e traffic";
  19586. const char eExpMasterLabel[] = "e exp master";
  19587. const char cHsTrafficLabel[] = "c hs traffic";
  19588. const char sHsTrafficLabel[] = "s hs traffic";
  19589. const char cAppTrafficLabel[] = "c ap traffic";
  19590. const char sAppTrafficLabel[] = "s ap traffic";
  19591. const char expMasterLabel[] = "exp master";
  19592. const char resMasterLabel[] = "res master";
  19593. const char derivedLabel[] = "derived";
  19594. WOLFSSL_TEST_SUBROUTINE int tls13_kdf_test(void)
  19595. {
  19596. int ret = 0;
  19597. word32 i;
  19598. word32 tc = sizeof(tls13KdfTestVectors)/sizeof(Tls13KdfTestVector);
  19599. const Tls13KdfTestVector* tv = NULL;
  19600. for (i = 0, tv = tls13KdfTestVectors; i < tc; i++, tv++) {
  19601. byte output[WC_MAX_DIGEST_SIZE];
  19602. byte secret[WC_MAX_DIGEST_SIZE];
  19603. byte salt[WC_MAX_DIGEST_SIZE];
  19604. byte zeroes[WC_MAX_DIGEST_SIZE];
  19605. byte hashZero[WC_MAX_DIGEST_SIZE];
  19606. int hashAlgSz;
  19607. XMEMSET(zeroes, 0, sizeof zeroes);
  19608. hashAlgSz = wc_HashGetDigestSize(tv->hashAlg);
  19609. if (hashAlgSz == BAD_FUNC_ARG) break;
  19610. ret = wc_Hash(tv->hashAlg, NULL, 0, hashZero, hashAlgSz);
  19611. if (ret != 0) break;
  19612. ret = wc_Tls13_HKDF_Extract(secret, NULL, 0,
  19613. (tv->pskSz == 0) ? zeroes : (byte*)tv->psk,
  19614. tv->pskSz, tv->hashAlg);
  19615. if (ret != 0) break;
  19616. ret = wc_Tls13_HKDF_Expand_Label(output, hashAlgSz,
  19617. secret, hashAlgSz,
  19618. (byte*)protocolLabel, (word32)strlen(protocolLabel),
  19619. (byte*)ceTrafficLabel, (word32)strlen(ceTrafficLabel),
  19620. tv->hashHello1, hashAlgSz, tv->hashAlg);
  19621. if (ret != 0) break;
  19622. ret = XMEMCMP(tv->clientEarlyTrafficSecret, output, hashAlgSz);
  19623. if (ret != 0) break;
  19624. ret = wc_Tls13_HKDF_Expand_Label(output, hashAlgSz,
  19625. secret, hashAlgSz,
  19626. (byte*)protocolLabel, (word32)strlen(protocolLabel),
  19627. (byte*)eExpMasterLabel, (word32)strlen(eExpMasterLabel),
  19628. tv->hashHello1, hashAlgSz, tv->hashAlg);
  19629. if (ret != 0) break;
  19630. ret = XMEMCMP(tv->earlyExporterMasterSecret, output, hashAlgSz);
  19631. if (ret != 0) break;
  19632. ret = wc_Tls13_HKDF_Expand_Label(salt, hashAlgSz,
  19633. secret, hashAlgSz,
  19634. (byte*)protocolLabel, (word32)strlen(protocolLabel),
  19635. (byte*)derivedLabel, (word32)strlen(derivedLabel),
  19636. hashZero, hashAlgSz, tv->hashAlg);
  19637. if (ret != 0) break;
  19638. ret = wc_Tls13_HKDF_Extract(secret, salt, hashAlgSz,
  19639. (tv->dheSz == 0) ? zeroes : (byte*)tv->dhe,
  19640. tv->dheSz, tv->hashAlg);
  19641. if (ret != 0) break;
  19642. ret = wc_Tls13_HKDF_Expand_Label(output, hashAlgSz,
  19643. secret, hashAlgSz,
  19644. (byte*)protocolLabel, (word32)strlen(protocolLabel),
  19645. (byte*)cHsTrafficLabel, (word32)strlen(cHsTrafficLabel),
  19646. tv->hashHello2, hashAlgSz, tv->hashAlg);
  19647. if (ret != 0) break;
  19648. ret = XMEMCMP(tv->clientHandshakeTrafficSecret,
  19649. output, hashAlgSz);
  19650. if (ret != 0) break;
  19651. ret = wc_Tls13_HKDF_Expand_Label(output, hashAlgSz,
  19652. secret, hashAlgSz,
  19653. (byte*)protocolLabel, (word32)strlen(protocolLabel),
  19654. (byte*)sHsTrafficLabel, (word32)strlen(sHsTrafficLabel),
  19655. tv->hashHello2, hashAlgSz, tv->hashAlg);
  19656. if (ret != 0) break;
  19657. ret = XMEMCMP(tv->serverHandshakeTrafficSecret, output, hashAlgSz);
  19658. if (ret != 0) break;
  19659. ret = wc_Tls13_HKDF_Expand_Label(salt, hashAlgSz,
  19660. secret, hashAlgSz,
  19661. (byte*)protocolLabel, (word32)strlen(protocolLabel),
  19662. (byte*)derivedLabel, (word32)strlen(derivedLabel),
  19663. hashZero, hashAlgSz, tv->hashAlg);
  19664. if (ret != 0) break;
  19665. ret = wc_Tls13_HKDF_Extract(secret, salt, hashAlgSz,
  19666. zeroes, hashAlgSz, tv->hashAlg);
  19667. if (ret != 0) break;
  19668. ret = wc_Tls13_HKDF_Expand_Label(output, hashAlgSz,
  19669. secret, hashAlgSz,
  19670. (byte*)protocolLabel, (word32)strlen(protocolLabel),
  19671. (byte*)cAppTrafficLabel, (word32)strlen(cAppTrafficLabel),
  19672. tv->hashFinished1, hashAlgSz, tv->hashAlg);
  19673. if (ret != 0) break;
  19674. ret = XMEMCMP(tv->clientApplicationTrafficSecret, output, hashAlgSz);
  19675. if (ret != 0) break;
  19676. ret = wc_Tls13_HKDF_Expand_Label(output, hashAlgSz,
  19677. secret, hashAlgSz,
  19678. (byte*)protocolLabel, (word32)strlen(protocolLabel),
  19679. (byte*)sAppTrafficLabel, (word32)strlen(sAppTrafficLabel),
  19680. tv->hashFinished1, hashAlgSz, tv->hashAlg);
  19681. if (ret != 0) break;
  19682. ret = XMEMCMP(tv->serverApplicationTrafficSecret, output, hashAlgSz);
  19683. if (ret != 0) break;
  19684. ret = wc_Tls13_HKDF_Expand_Label(output, hashAlgSz,
  19685. secret, hashAlgSz,
  19686. (byte*)protocolLabel, (word32)strlen(protocolLabel),
  19687. (byte*)expMasterLabel, (word32)strlen(expMasterLabel),
  19688. tv->hashFinished1, hashAlgSz, tv->hashAlg);
  19689. if (ret != 0) break;
  19690. ret = XMEMCMP(tv->exporterMasterSecret, output, hashAlgSz);
  19691. if (ret != 0) break;
  19692. ret = wc_Tls13_HKDF_Expand_Label(output, hashAlgSz,
  19693. secret, hashAlgSz,
  19694. (byte*)protocolLabel, (word32)strlen(protocolLabel),
  19695. (byte*)resMasterLabel, (word32)strlen(resMasterLabel),
  19696. tv->hashFinished2, hashAlgSz, tv->hashAlg);
  19697. if (ret != 0) break;
  19698. ret = XMEMCMP(tv->resumptionMasterSecret, output, hashAlgSz);
  19699. if (ret != 0) break;
  19700. }
  19701. return ret;
  19702. }
  19703. #endif /* WOLFSSL_TLS13 */
  19704. #if defined(HAVE_ECC) && defined(HAVE_X963_KDF)
  19705. WOLFSSL_TEST_SUBROUTINE int x963kdf_test(void)
  19706. {
  19707. int ret;
  19708. byte kek[128];
  19709. #ifndef NO_SHA
  19710. /* SHA-1, COUNT = 0
  19711. * shared secret length: 192
  19712. * SharedInfo length: 0
  19713. * key data length: 128
  19714. */
  19715. WOLFSSL_SMALL_STACK_STATIC const byte Z[] = {
  19716. 0x1c, 0x7d, 0x7b, 0x5f, 0x05, 0x97, 0xb0, 0x3d,
  19717. 0x06, 0xa0, 0x18, 0x46, 0x6e, 0xd1, 0xa9, 0x3e,
  19718. 0x30, 0xed, 0x4b, 0x04, 0xdc, 0x64, 0xcc, 0xdd
  19719. };
  19720. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  19721. 0xbf, 0x71, 0xdf, 0xfd, 0x8f, 0x4d, 0x99, 0x22,
  19722. 0x39, 0x36, 0xbe, 0xb4, 0x6f, 0xee, 0x8c, 0xcc
  19723. };
  19724. #endif
  19725. #ifndef NO_SHA256
  19726. /* SHA-256, COUNT = 3
  19727. * shared secret length: 192
  19728. * SharedInfo length: 0
  19729. * key data length: 128
  19730. */
  19731. WOLFSSL_SMALL_STACK_STATIC const byte Z2[] = {
  19732. 0xd3, 0x8b, 0xdb, 0xe5, 0xc4, 0xfc, 0x16, 0x4c,
  19733. 0xdd, 0x96, 0x7f, 0x63, 0xc0, 0x4f, 0xe0, 0x7b,
  19734. 0x60, 0xcd, 0xe8, 0x81, 0xc2, 0x46, 0x43, 0x8c
  19735. };
  19736. WOLFSSL_SMALL_STACK_STATIC const byte verify2[] = {
  19737. 0x5e, 0x67, 0x4d, 0xb9, 0x71, 0xba, 0xc2, 0x0a,
  19738. 0x80, 0xba, 0xd0, 0xd4, 0x51, 0x4d, 0xc4, 0x84
  19739. };
  19740. #endif
  19741. #ifdef WOLFSSL_SHA512
  19742. /* SHA-512, COUNT = 0
  19743. * shared secret length: 192
  19744. * SharedInfo length: 0
  19745. * key data length: 128
  19746. */
  19747. WOLFSSL_SMALL_STACK_STATIC const byte Z3[] = {
  19748. 0x87, 0xfc, 0x0d, 0x8c, 0x44, 0x77, 0x48, 0x5b,
  19749. 0xb5, 0x74, 0xf5, 0xfc, 0xea, 0x26, 0x4b, 0x30,
  19750. 0x88, 0x5d, 0xc8, 0xd9, 0x0a, 0xd8, 0x27, 0x82
  19751. };
  19752. WOLFSSL_SMALL_STACK_STATIC const byte verify3[] = {
  19753. 0x94, 0x76, 0x65, 0xfb, 0xb9, 0x15, 0x21, 0x53,
  19754. 0xef, 0x46, 0x02, 0x38, 0x50, 0x6a, 0x02, 0x45
  19755. };
  19756. /* SHA-512, COUNT = 0
  19757. * shared secret length: 521
  19758. * SharedInfo length: 128
  19759. * key data length: 1024
  19760. */
  19761. WOLFSSL_SMALL_STACK_STATIC const byte Z4[] = {
  19762. 0x00, 0xaa, 0x5b, 0xb7, 0x9b, 0x33, 0xe3, 0x89,
  19763. 0xfa, 0x58, 0xce, 0xad, 0xc0, 0x47, 0x19, 0x7f,
  19764. 0x14, 0xe7, 0x37, 0x12, 0xf4, 0x52, 0xca, 0xa9,
  19765. 0xfc, 0x4c, 0x9a, 0xdb, 0x36, 0x93, 0x48, 0xb8,
  19766. 0x15, 0x07, 0x39, 0x2f, 0x1a, 0x86, 0xdd, 0xfd,
  19767. 0xb7, 0xc4, 0xff, 0x82, 0x31, 0xc4, 0xbd, 0x0f,
  19768. 0x44, 0xe4, 0x4a, 0x1b, 0x55, 0xb1, 0x40, 0x47,
  19769. 0x47, 0xa9, 0xe2, 0xe7, 0x53, 0xf5, 0x5e, 0xf0,
  19770. 0x5a, 0x2d
  19771. };
  19772. WOLFSSL_SMALL_STACK_STATIC const byte info4[] = {
  19773. 0xe3, 0xb5, 0xb4, 0xc1, 0xb0, 0xd5, 0xcf, 0x1d,
  19774. 0x2b, 0x3a, 0x2f, 0x99, 0x37, 0x89, 0x5d, 0x31
  19775. };
  19776. WOLFSSL_SMALL_STACK_STATIC const byte verify4[] = {
  19777. 0x44, 0x63, 0xf8, 0x69, 0xf3, 0xcc, 0x18, 0x76,
  19778. 0x9b, 0x52, 0x26, 0x4b, 0x01, 0x12, 0xb5, 0x85,
  19779. 0x8f, 0x7a, 0xd3, 0x2a, 0x5a, 0x2d, 0x96, 0xd8,
  19780. 0xcf, 0xfa, 0xbf, 0x7f, 0xa7, 0x33, 0x63, 0x3d,
  19781. 0x6e, 0x4d, 0xd2, 0xa5, 0x99, 0xac, 0xce, 0xb3,
  19782. 0xea, 0x54, 0xa6, 0x21, 0x7c, 0xe0, 0xb5, 0x0e,
  19783. 0xef, 0x4f, 0x6b, 0x40, 0xa5, 0xc3, 0x02, 0x50,
  19784. 0xa5, 0xa8, 0xee, 0xee, 0x20, 0x80, 0x02, 0x26,
  19785. 0x70, 0x89, 0xdb, 0xf3, 0x51, 0xf3, 0xf5, 0x02,
  19786. 0x2a, 0xa9, 0x63, 0x8b, 0xf1, 0xee, 0x41, 0x9d,
  19787. 0xea, 0x9c, 0x4f, 0xf7, 0x45, 0xa2, 0x5a, 0xc2,
  19788. 0x7b, 0xda, 0x33, 0xca, 0x08, 0xbd, 0x56, 0xdd,
  19789. 0x1a, 0x59, 0xb4, 0x10, 0x6c, 0xf2, 0xdb, 0xbc,
  19790. 0x0a, 0xb2, 0xaa, 0x8e, 0x2e, 0xfa, 0x7b, 0x17,
  19791. 0x90, 0x2d, 0x34, 0x27, 0x69, 0x51, 0xce, 0xcc,
  19792. 0xab, 0x87, 0xf9, 0x66, 0x1c, 0x3e, 0x88, 0x16
  19793. };
  19794. #endif
  19795. #ifndef NO_SHA
  19796. ret = wc_X963_KDF(WC_HASH_TYPE_SHA, Z, sizeof(Z), NULL, 0,
  19797. kek, sizeof(verify));
  19798. if (ret != 0)
  19799. return -9800;
  19800. if (XMEMCMP(verify, kek, sizeof(verify)) != 0)
  19801. return -9801;
  19802. #endif
  19803. #ifndef NO_SHA256
  19804. ret = wc_X963_KDF(WC_HASH_TYPE_SHA256, Z2, sizeof(Z2), NULL, 0,
  19805. kek, sizeof(verify2));
  19806. if (ret != 0)
  19807. return -9802;
  19808. if (XMEMCMP(verify2, kek, sizeof(verify2)) != 0)
  19809. return -9803;
  19810. #endif
  19811. #ifdef WOLFSSL_SHA512
  19812. ret = wc_X963_KDF(WC_HASH_TYPE_SHA512, Z3, sizeof(Z3), NULL, 0,
  19813. kek, sizeof(verify3));
  19814. if (ret != 0)
  19815. return -9804;
  19816. if (XMEMCMP(verify3, kek, sizeof(verify3)) != 0)
  19817. return -9805;
  19818. ret = wc_X963_KDF(WC_HASH_TYPE_SHA512, Z4, sizeof(Z4), info4,
  19819. sizeof(info4), kek, sizeof(verify4));
  19820. if (ret != 0)
  19821. return -9806;
  19822. if (XMEMCMP(verify4, kek, sizeof(verify4)) != 0)
  19823. return -9807;
  19824. #endif
  19825. return 0;
  19826. }
  19827. #endif /* HAVE_X963_KDF */
  19828. #ifdef HAVE_ECC
  19829. /* size to use for ECC key gen tests */
  19830. #ifndef ECC_KEYGEN_SIZE
  19831. #ifndef NO_ECC256
  19832. #define ECC_KEYGEN_SIZE 32
  19833. #elif defined(HAVE_ECC384)
  19834. #define ECC_KEYGEN_SIZE 48
  19835. #elif defined(HAVE_ECC224)
  19836. #define ECC_KEYGEN_SIZE 28
  19837. #elif defined(HAVE_ECC521)
  19838. #define ECC_KEYGEN_SIZE 66
  19839. #else
  19840. #error No ECC keygen size defined for test
  19841. #endif
  19842. #endif
  19843. #ifdef BENCH_EMBEDDED
  19844. #define ECC_SHARED_SIZE 128
  19845. #else
  19846. #define ECC_SHARED_SIZE MAX_ECC_BYTES
  19847. #endif
  19848. #if defined(WOLFSSL_ECDSA_DETERMINISTIC_K) || defined(WOLFSSL_ECDSA_DETERMINISTIC_K_VARIANT)
  19849. #define HAVE_ECC_DETERMINISTIC_K
  19850. #define ECC_DIGEST_SIZE WC_SHA256_DIGEST_SIZE
  19851. #else
  19852. #define ECC_DIGEST_SIZE MAX_ECC_BYTES
  19853. #endif
  19854. #define ECC_SIG_SIZE ECC_MAX_SIG_SIZE
  19855. #ifndef NO_ECC_VECTOR_TEST
  19856. #if (defined(HAVE_ECC192) || defined(HAVE_ECC224) ||\
  19857. !defined(NO_ECC256) || defined(HAVE_ECC384) ||\
  19858. defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES))
  19859. #define HAVE_ECC_VECTOR_TEST
  19860. #endif
  19861. #endif
  19862. #ifdef HAVE_ECC_VECTOR_TEST
  19863. typedef struct eccVector {
  19864. const char* msg; /* SHA-1 Encoded Message */
  19865. const char* Qx;
  19866. const char* Qy;
  19867. const char* d; /* Private Key */
  19868. const char* R;
  19869. const char* S;
  19870. const char* curveName;
  19871. word32 msgLen;
  19872. word32 keySize;
  19873. #ifndef NO_ASN
  19874. const byte* r;
  19875. word32 rSz;
  19876. const byte* s;
  19877. word32 sSz;
  19878. #endif
  19879. } eccVector;
  19880. #if !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  19881. static int ecc_test_vector_item(const eccVector* vector)
  19882. {
  19883. int ret = 0, verify = 0;
  19884. word32 sigSz;
  19885. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  19886. ecc_key *userA = (ecc_key *)XMALLOC(sizeof *userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19887. #else
  19888. ecc_key userA[1];
  19889. #endif
  19890. WC_DECLARE_VAR(sig, byte, ECC_SIG_SIZE, HEAP_HINT);
  19891. #if !defined(NO_ASN) && !defined(HAVE_SELFTEST)
  19892. word32 sigRawSz, rSz = MAX_ECC_BYTES, sSz = MAX_ECC_BYTES;
  19893. WC_DECLARE_VAR(sigRaw, byte, ECC_SIG_SIZE, HEAP_HINT);
  19894. WC_DECLARE_VAR(r, byte, MAX_ECC_BYTES, HEAP_HINT);
  19895. WC_DECLARE_VAR(s, byte, MAX_ECC_BYTES, HEAP_HINT);
  19896. #endif
  19897. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  19898. if (sig == NULL)
  19899. ERROR_OUT(MEMORY_E, done);
  19900. #if !defined(NO_ASN) && !defined(HAVE_SELFTEST)
  19901. if (sigRaw == NULL || r == NULL || s == NULL)
  19902. ERROR_OUT(MEMORY_E, done);
  19903. #endif
  19904. #endif
  19905. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  19906. if (userA == NULL)
  19907. ERROR_OUT(MEMORY_E, done);
  19908. #endif
  19909. ret = wc_ecc_init_ex(userA, HEAP_HINT, devId);
  19910. if (ret != 0)
  19911. goto done;
  19912. ret = wc_ecc_import_raw(userA, vector->Qx, vector->Qy,
  19913. vector->d, vector->curveName);
  19914. if (ret != 0)
  19915. goto done;
  19916. XMEMSET(sig, 0, ECC_SIG_SIZE);
  19917. sigSz = ECC_SIG_SIZE;
  19918. ret = wc_ecc_rs_to_sig(vector->R, vector->S, sig, &sigSz);
  19919. if (ret != 0)
  19920. goto done;
  19921. #if !defined(NO_ASN) && !defined(HAVE_SELFTEST)
  19922. XMEMSET(sigRaw, 0, ECC_SIG_SIZE);
  19923. sigRawSz = ECC_SIG_SIZE;
  19924. ret = wc_ecc_rs_raw_to_sig(vector->r, vector->rSz, vector->s, vector->sSz,
  19925. sigRaw, &sigRawSz);
  19926. if (ret != 0)
  19927. goto done;
  19928. if (sigSz != sigRawSz || XMEMCMP(sig, sigRaw, sigSz) != 0) {
  19929. ret = -9810;
  19930. goto done;
  19931. }
  19932. ret = wc_ecc_sig_to_rs(sig, sigSz, r, &rSz, s, &sSz);
  19933. if (ret != 0)
  19934. goto done;
  19935. if (rSz != vector->rSz || XMEMCMP(r, vector->r, rSz) != 0 ||
  19936. sSz != vector->sSz || XMEMCMP(s, vector->s, sSz) != 0) {
  19937. ret = -9811;
  19938. goto done;
  19939. }
  19940. #endif
  19941. do {
  19942. #if defined(WOLFSSL_ASYNC_CRYPT)
  19943. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  19944. #endif
  19945. if (ret == 0)
  19946. ret = wc_ecc_verify_hash(sig, sigSz, (byte*)vector->msg,
  19947. vector->msgLen, &verify, userA);
  19948. } while (ret == WC_PENDING_E);
  19949. if (ret != 0)
  19950. goto done;
  19951. TEST_SLEEP();
  19952. if (verify != 1)
  19953. ret = -9812;
  19954. done:
  19955. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  19956. if (userA != NULL) {
  19957. wc_ecc_free(userA);
  19958. XFREE(userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19959. }
  19960. #else
  19961. wc_ecc_free(userA);
  19962. #endif
  19963. #if !defined(NO_ASN) && !defined(HAVE_SELFTEST)
  19964. WC_FREE_VAR(sigRaw, HEAP_HINT);
  19965. WC_FREE_VAR(r, HEAP_HINT);
  19966. WC_FREE_VAR(s, HEAP_HINT);
  19967. #endif
  19968. WC_FREE_VAR(sig, HEAP_HINT);
  19969. return ret;
  19970. }
  19971. static int ecc_test_vector(int keySize)
  19972. {
  19973. int ret;
  19974. eccVector vec;
  19975. XMEMSET(&vec, 0, sizeof(vec));
  19976. vec.keySize = (word32)keySize;
  19977. switch(keySize) {
  19978. #if defined(HAVE_ECC112) || defined(HAVE_ALL_CURVES)
  19979. case 14:
  19980. return 0;
  19981. #endif /* HAVE_ECC112 */
  19982. #if defined(HAVE_ECC128) || defined(HAVE_ALL_CURVES)
  19983. case 16:
  19984. return 0;
  19985. #endif /* HAVE_ECC128 */
  19986. #if defined(HAVE_ECC160) || defined(HAVE_ALL_CURVES)
  19987. case 20:
  19988. return 0;
  19989. #endif /* HAVE_ECC160 */
  19990. #if defined(HAVE_ECC192) || defined(HAVE_ALL_CURVES)
  19991. case 24:
  19992. /* first [P-192,SHA-1] vector from FIPS 186-3 NIST vectors */
  19993. #if 1
  19994. vec.msg = "\x60\x80\x79\x42\x3f\x12\x42\x1d\xe6\x16\xb7\x49\x3e\xbe\x55\x1c\xf4\xd6\x5b\x92";
  19995. vec.msgLen = 20;
  19996. #else
  19997. /* This is the raw message prior to SHA-1 */
  19998. vec.msg =
  19999. "\xeb\xf7\x48\xd7\x48\xeb\xbc\xa7\xd2\x9f\xb4\x73\x69\x8a\x6e\x6b"
  20000. "\x4f\xb1\x0c\x86\x5d\x4a\xf0\x24\xcc\x39\xae\x3d\xf3\x46\x4b\xa4"
  20001. "\xf1\xd6\xd4\x0f\x32\xbf\x96\x18\xa9\x1b\xb5\x98\x6f\xa1\xa2\xaf"
  20002. "\x04\x8a\x0e\x14\xdc\x51\xe5\x26\x7e\xb0\x5e\x12\x7d\x68\x9d\x0a"
  20003. "\xc6\xf1\xa7\xf1\x56\xce\x06\x63\x16\xb9\x71\xcc\x7a\x11\xd0\xfd"
  20004. "\x7a\x20\x93\xe2\x7c\xf2\xd0\x87\x27\xa4\xe6\x74\x8c\xc3\x2f\xd5"
  20005. "\x9c\x78\x10\xc5\xb9\x01\x9d\xf2\x1c\xdc\xc0\xbc\xa4\x32\xc0\xa3"
  20006. "\xee\xd0\x78\x53\x87\x50\x88\x77\x11\x43\x59\xce\xe4\xa0\x71\xcf";
  20007. vec.msgLen = 128;
  20008. #endif
  20009. vec.Qx = "07008ea40b08dbe76432096e80a2494c94982d2d5bcf98e6";
  20010. vec.Qy = "76fab681d00b414ea636ba215de26d98c41bd7f2e4d65477";
  20011. vec.d = "e14f37b3d1374ff8b03f41b9b3fdd2f0ebccf275d660d7f3";
  20012. vec.R = "6994d962bdd0d793ffddf855ec5bf2f91a9698b46258a63e";
  20013. vec.S = "02ba6465a234903744ab02bc8521405b73cf5fc00e1a9f41";
  20014. vec.curveName = "SECP192R1";
  20015. #ifndef NO_ASN
  20016. vec.r = (byte*)"\x69\x94\xd9\x62\xbd\xd0\xd7\x93\xff\xdd\xf8\x55"
  20017. "\xec\x5b\xf2\xf9\x1a\x96\x98\xb4\x62\x58\xa6\x3e";
  20018. vec.rSz = 24;
  20019. vec.s = (byte*)"\x02\xba\x64\x65\xa2\x34\x90\x37\x44\xab\x02\xbc"
  20020. "\x85\x21\x40\x5b\x73\xcf\x5f\xc0\x0e\x1a\x9f\x41";
  20021. vec.sSz = 24;
  20022. #endif
  20023. break;
  20024. #endif /* HAVE_ECC192 */
  20025. #if defined(HAVE_ECC224) || defined(HAVE_ALL_CURVES)
  20026. case 28:
  20027. /* first [P-224,SHA-1] vector from FIPS 186-3 NIST vectors */
  20028. #if 1
  20029. vec.msg = "\xb9\xa3\xb8\x6d\xb0\xba\x99\xfd\xc6\xd2\x94\x6b\xfe\xbe\x9c\xe8\x3f\x10\x74\xfc";
  20030. vec.msgLen = 20;
  20031. #else
  20032. /* This is the raw message prior to SHA-1 */
  20033. vec.msg =
  20034. "\x36\xc8\xb2\x29\x86\x48\x7f\x67\x7c\x18\xd0\x97\x2a\x9e\x20\x47"
  20035. "\xb3\xaf\xa5\x9e\xc1\x62\x76\x4e\xc3\x0b\x5b\x69\xe0\x63\x0f\x99"
  20036. "\x0d\x4e\x05\xc2\x73\xb0\xe5\xa9\xd4\x28\x27\xb6\x95\xfc\x2d\x64"
  20037. "\xd9\x13\x8b\x1c\xf4\xc1\x21\x55\x89\x4c\x42\x13\x21\xa7\xbb\x97"
  20038. "\x0b\xdc\xe0\xfb\xf0\xd2\xae\x85\x61\xaa\xd8\x71\x7f\x2e\x46\xdf"
  20039. "\xe3\xff\x8d\xea\xb4\xd7\x93\x23\x56\x03\x2c\x15\x13\x0d\x59\x9e"
  20040. "\x26\xc1\x0f\x2f\xec\x96\x30\x31\xac\x69\x38\xa1\x8d\x66\x45\x38"
  20041. "\xb9\x4d\xac\x55\x34\xef\x7b\x59\x94\x24\xd6\x9b\xe1\xf7\x1c\x20";
  20042. vec.msgLen = 128;
  20043. #endif
  20044. vec.Qx = "8a4dca35136c4b70e588e23554637ae251077d1365a6ba5db9585de7";
  20045. vec.Qy = "ad3dee06de0be8279d4af435d7245f14f3b4f82eb578e519ee0057b1";
  20046. vec.d = "97c4b796e1639dd1035b708fc00dc7ba1682cec44a1002a1a820619f";
  20047. vec.R = "147b33758321e722a0360a4719738af848449e2c1d08defebc1671a7";
  20048. vec.S = "24fc7ed7f1352ca3872aa0916191289e2e04d454935d50fe6af3ad5b";
  20049. vec.curveName = "SECP224R1";
  20050. #ifndef NO_ASN
  20051. vec.r = (byte*)"\x14\x7b\x33\x75\x83\x21\xe7\x22\xa0\x36\x0a\x47"
  20052. "\x19\x73\x8a\xf8\x48\x44\x9e\x2c\x1d\x08\xde\xfe"
  20053. "\xbc\x16\x71\xa7";
  20054. vec.rSz = 28;
  20055. vec.s = (byte*)"\x24\xfc\x7e\xd7\xf1\x35\x2c\xa3\x87\x2a\xa0\x91"
  20056. "\x61\x91\x28\x9e\x2e\x04\xd4\x54\x93\x5d\x50\xfe"
  20057. "\x6a\xf3\xad\x5b";
  20058. vec.sSz = 28;
  20059. #endif
  20060. break;
  20061. #endif /* HAVE_ECC224 */
  20062. #if defined(HAVE_ECC239) || defined(HAVE_ALL_CURVES)
  20063. case 30:
  20064. return 0;
  20065. #endif /* HAVE_ECC239 */
  20066. #if !defined(NO_ECC256) || defined(HAVE_ALL_CURVES)
  20067. case 32:
  20068. /* first [P-256,SHA-1] vector from FIPS 186-3 NIST vectors */
  20069. #if 1
  20070. vec.msg = "\xa3\xf9\x1a\xe2\x1b\xa6\xb3\x03\x98\x64\x47\x2f\x18\x41\x44\xc6\xaf\x62\xcd\x0e";
  20071. vec.msgLen = 20;
  20072. #else
  20073. /* This is the raw message prior to SHA-1 */
  20074. vec.msg =
  20075. "\xa2\x4b\x21\x76\x2e\x6e\xdb\x15\x3c\xc1\x14\x38\xdb\x0e\x92\xcd"
  20076. "\xf5\x2b\x86\xb0\x6c\xa9\x70\x16\x06\x27\x59\xc7\x0d\x36\xd1\x56"
  20077. "\x2c\xc9\x63\x0d\x7f\xc7\xc7\x74\xb2\x8b\x54\xe3\x1e\xf5\x58\x72"
  20078. "\xb2\xa6\x5d\xf1\xd7\xec\x26\xde\xbb\x33\xe7\xd9\x27\xef\xcc\xf4"
  20079. "\x6b\x63\xde\x52\xa4\xf4\x31\xea\xca\x59\xb0\x5d\x2e\xde\xc4\x84"
  20080. "\x5f\xff\xc0\xee\x15\x03\x94\xd6\x1f\x3d\xfe\xcb\xcd\xbf\x6f\x5a"
  20081. "\x73\x38\xd0\xbe\x3f\x2a\x77\x34\x51\x98\x3e\xba\xeb\x48\xf6\x73"
  20082. "\x8f\xc8\x95\xdf\x35\x7e\x1a\x48\xa6\x53\xbb\x35\x5a\x31\xa1\xb4"
  20083. vec.msgLen = 128;
  20084. #endif
  20085. vec.Qx = "fa2737fb93488d19caef11ae7faf6b7f4bcd67b286e3fc54e8a65c2b74aeccb0";
  20086. vec.Qy = "d4ccd6dae698208aa8c3a6f39e45510d03be09b2f124bfc067856c324f9b4d09";
  20087. vec.d = "be34baa8d040a3b991f9075b56ba292f755b90e4b6dc10dad36715c33cfdac25";
  20088. vec.R = "2b826f5d44e2d0b6de531ad96b51e8f0c56fdfead3c236892e4d84eacfc3b75c";
  20089. vec.S = "a2248b62c03db35a7cd63e8a120a3521a89d3d2f61ff99035a2148ae32e3a248";
  20090. #ifndef NO_ASN
  20091. vec.r = (byte*)"\x2b\x82\x6f\x5d\x44\xe2\xd0\xb6\xde\x53\x1a\xd9"
  20092. "\x6b\x51\xe8\xf0\xc5\x6f\xdf\xea\xd3\xc2\x36\x89"
  20093. "\x2e\x4d\x84\xea\xcf\xc3\xb7\x5c";
  20094. vec.rSz = 32;
  20095. vec.s = (byte*)"\xa2\x24\x8b\x62\xc0\x3d\xb3\x5a\x7c\xd6\x3e\x8a"
  20096. "\x12\x0a\x35\x21\xa8\x9d\x3d\x2f\x61\xff\x99\x03"
  20097. "\x5a\x21\x48\xae\x32\xe3\xa2\x48";
  20098. vec.sSz = 32;
  20099. #endif
  20100. vec.curveName = "SECP256R1";
  20101. break;
  20102. #endif /* !NO_ECC256 */
  20103. #if defined(HAVE_ECC320) || defined(HAVE_ALL_CURVES)
  20104. case 40:
  20105. return 0;
  20106. #endif /* HAVE_ECC320 */
  20107. #if defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)
  20108. case 48:
  20109. /* first [P-384,SHA-1] vector from FIPS 186-3 NIST vectors */
  20110. #if 1
  20111. vec.msg = "\x9b\x9f\x8c\x95\x35\xa5\xca\x26\x60\x5d\xb7\xf2\xfa\x57\x3b\xdf\xc3\x2e\xab\x8b";
  20112. vec.msgLen = 20;
  20113. #else
  20114. /* This is the raw message prior to SHA-1 */
  20115. vec.msg =
  20116. "\xab\xe1\x0a\xce\x13\xe7\xe1\xd9\x18\x6c\x48\xf7\x88\x9d\x51\x47"
  20117. "\x3d\x3a\x09\x61\x98\x4b\xc8\x72\xdf\x70\x8e\xcc\x3e\xd3\xb8\x16"
  20118. "\x9d\x01\xe3\xd9\x6f\xc4\xf1\xd5\xea\x00\xa0\x36\x92\xbc\xc5\xcf"
  20119. "\xfd\x53\x78\x7c\x88\xb9\x34\xaf\x40\x4c\x03\x9d\x32\x89\xb5\xba"
  20120. "\xc5\xae\x7d\xb1\x49\x68\x75\xb5\xdc\x73\xc3\x09\xf9\x25\xc1\x3d"
  20121. "\x1c\x01\xab\xda\xaf\xeb\xcd\xac\x2c\xee\x43\x39\x39\xce\x8d\x4a"
  20122. "\x0a\x5d\x57\xbb\x70\x5f\x3b\xf6\xec\x08\x47\x95\x11\xd4\xb4\xa3"
  20123. "\x21\x1f\x61\x64\x9a\xd6\x27\x43\x14\xbf\x0d\x43\x8a\x81\xe0\x60"
  20124. vec.msgLen = 128;
  20125. #endif
  20126. vec.Qx = "e55fee6c49d8d523f5ce7bf9c0425ce4ff650708b7de5cfb095901523979a7f042602db30854735369813b5c3f5ef868";
  20127. vec.Qy = "28f59cc5dc509892a988d38a8e2519de3d0c4fd0fbdb0993e38f18506c17606c5e24249246f1ce94983a5361c5be983e";
  20128. vec.d = "a492ce8fa90084c227e1a32f7974d39e9ff67a7e8705ec3419b35fb607582bebd461e0b1520ac76ec2dd4e9b63ebae71";
  20129. vec.R = "6820b8585204648aed63bdff47f6d9acebdea62944774a7d14f0e14aa0b9a5b99545b2daee6b3c74ebf606667a3f39b7";
  20130. vec.S = "491af1d0cccd56ddd520b233775d0bc6b40a6255cc55207d8e9356741f23c96c14714221078dbd5c17f4fdd89b32a907";
  20131. vec.curveName = "SECP384R1";
  20132. #ifndef NO_ASN
  20133. vec.r = (byte*)"\x68\x20\xb8\x58\x52\x04\x64\x8a\xed\x63\xbd\xff"
  20134. "\x47\xf6\xd9\xac\xeb\xde\xa6\x29\x44\x77\x4a\x7d"
  20135. "\x14\xf0\xe1\x4a\xa0\xb9\xa5\xb9\x95\x45\xb2\xda"
  20136. "\xee\x6b\x3c\x74\xeb\xf6\x06\x66\x7a\x3f\x39\xb7";
  20137. vec.rSz = 48;
  20138. vec.s = (byte*)"\x49\x1a\xf1\xd0\xcc\xcd\x56\xdd\xd5\x20\xb2\x33"
  20139. "\x77\x5d\x0b\xc6\xb4\x0a\x62\x55\xcc\x55\x20\x7d"
  20140. "\x8e\x93\x56\x74\x1f\x23\xc9\x6c\x14\x71\x42\x21"
  20141. "\x07\x8d\xbd\x5c\x17\xf4\xfd\xd8\x9b\x32\xa9\x07";
  20142. vec.sSz = 48;
  20143. #endif
  20144. break;
  20145. #endif /* HAVE_ECC384 */
  20146. #if defined(HAVE_ECC512) || defined(HAVE_ALL_CURVES)
  20147. case 64:
  20148. return 0;
  20149. #endif /* HAVE_ECC512 */
  20150. #if defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)
  20151. case 66:
  20152. /* first [P-521,SHA-1] vector from FIPS 186-3 NIST vectors */
  20153. #if 1
  20154. vec.msg = "\x1b\xf7\x03\x9c\xca\x23\x94\x27\x3f\x11\xa1\xd4\x8d\xcc\xb4\x46\x6f\x31\x61\xdf";
  20155. vec.msgLen = 20;
  20156. #else
  20157. /* This is the raw message prior to SHA-1 */
  20158. vec.msg =
  20159. "\x50\x3f\x79\x39\x34\x0a\xc7\x23\xcd\x4a\x2f\x4e\x6c\xcc\x27\x33"
  20160. "\x38\x3a\xca\x2f\xba\x90\x02\x19\x9d\x9e\x1f\x94\x8b\xe0\x41\x21"
  20161. "\x07\xa3\xfd\xd5\x14\xd9\x0c\xd4\xf3\x7c\xc3\xac\x62\xef\x00\x3a"
  20162. "\x2d\xb1\xd9\x65\x7a\xb7\x7f\xe7\x55\xbf\x71\xfa\x59\xe4\xd9\x6e"
  20163. "\xa7\x2a\xe7\xbf\x9d\xe8\x7d\x79\x34\x3b\xc1\xa4\xbb\x14\x4d\x16"
  20164. "\x28\xd1\xe9\xe9\xc8\xed\x80\x8b\x96\x2c\x54\xe5\xf9\x6d\x53\xda"
  20165. "\x14\x7a\x96\x38\xf9\x4a\x91\x75\xd8\xed\x61\x05\x5f\x0b\xa5\x73"
  20166. "\xa8\x2b\xb7\xe0\x18\xee\xda\xc4\xea\x7b\x36\x2e\xc8\x9c\x38\x2b"
  20167. vec.msgLen = 128;
  20168. #endif
  20169. vec.Qx = "12fbcaeffa6a51f3ee4d3d2b51c5dec6d7c726ca353fc014ea2bf7cfbb9b910d32cbfa6a00fe39b6cdb8946f22775398b2e233c0cf144d78c8a7742b5c7a3bb5d23";
  20170. vec.Qy = "09cdef823dd7bf9a79e8cceacd2e4527c231d0ae5967af0958e931d7ddccf2805a3e618dc3039fec9febbd33052fe4c0fee98f033106064982d88f4e03549d4a64d";
  20171. vec.d = "1bd56bd106118eda246155bd43b42b8e13f0a6e25dd3bb376026fab4dc92b6157bc6dfec2d15dd3d0cf2a39aa68494042af48ba9601118da82c6f2108a3a203ad74";
  20172. vec.R = "0bd117b4807710898f9dd7778056485777668f0e78e6ddf5b000356121eb7a220e9493c7f9a57c077947f89ac45d5acb6661bbcd17abb3faea149ba0aa3bb1521be";
  20173. vec.S = "019cd2c5c3f9870ecdeb9b323abdf3a98cd5e231d85c6ddc5b71ab190739f7f226e6b134ba1d5889ddeb2751dabd97911dff90c34684cdbe7bb669b6c3d22f2480c";
  20174. vec.curveName = "SECP521R1";
  20175. #ifndef NO_ASN
  20176. vec.r = (byte*)"\xbd\x11\x7b\x48\x07\x71\x08\x98\xf9\xdd\x77\x78"
  20177. "\x05\x64\x85\x77\x76\x68\xf0\xe7\x8e\x6d\xdf\x5b"
  20178. "\x00\x03\x56\x12\x1e\xb7\xa2\x20\xe9\x49\x3c\x7f"
  20179. "\x9a\x57\xc0\x77\x94\x7f\x89\xac\x45\xd5\xac\xb6"
  20180. "\x66\x1b\xbc\xd1\x7a\xbb\x3f\xae\xa1\x49\xba\x0a"
  20181. "\xa3\xbb\x15\x21\xbe";
  20182. vec.rSz = 65;
  20183. vec.s = (byte*)"\x19\xcd\x2c\x5c\x3f\x98\x70\xec\xde\xb9\xb3\x23"
  20184. "\xab\xdf\x3a\x98\xcd\x5e\x23\x1d\x85\xc6\xdd\xc5"
  20185. "\xb7\x1a\xb1\x90\x73\x9f\x7f\x22\x6e\x6b\x13\x4b"
  20186. "\xa1\xd5\x88\x9d\xde\xb2\x75\x1d\xab\xd9\x79\x11"
  20187. "\xdf\xf9\x0c\x34\x68\x4c\xdb\xe7\xbb\x66\x9b\x6c"
  20188. "\x3d\x22\xf2\x48\x0c";
  20189. vec.sSz = 65;
  20190. #endif
  20191. break;
  20192. #endif /* HAVE_ECC521 */
  20193. default:
  20194. return NOT_COMPILED_IN; /* Invalid key size / Not supported */
  20195. }; /* Switch */
  20196. ret = ecc_test_vector_item(&vec);
  20197. if (ret < 0) {
  20198. return ret;
  20199. }
  20200. return 0;
  20201. }
  20202. #endif /* WOLF_CRYPTO_CB_ONLY_ECC */
  20203. #if defined(HAVE_ECC_SIGN) && (defined(WOLFSSL_ECDSA_DETERMINISTIC_K) || \
  20204. defined(WOLFSSL_ECDSA_DETERMINISTIC_K_VARIANT)) \
  20205. && (!defined(FIPS_VERSION_GE) || FIPS_VERSION_GE(5,3))
  20206. #if defined(HAVE_ECC256)
  20207. static int ecc_test_deterministic_k(WC_RNG* rng)
  20208. {
  20209. int ret;
  20210. #ifdef WOLFSSL_SMALL_STACK
  20211. ecc_key *key = NULL;
  20212. #else
  20213. ecc_key key[1];
  20214. #endif
  20215. int key_inited = 0;
  20216. byte sig[72];
  20217. word32 sigSz;
  20218. WOLFSSL_SMALL_STACK_STATIC const unsigned char msg[] = "sample";
  20219. unsigned char hash[32];
  20220. WOLFSSL_SMALL_STACK_STATIC const char* dIUT =
  20221. "C9AFA9D845BA75166B5C215767B1D6934E50C3DB36E89B127B8A622B120F6721";
  20222. WOLFSSL_SMALL_STACK_STATIC const char* QIUTx =
  20223. "60FED4BA255A9D31C961EB74C6356D68C049B8923B61FA6CE669622E60F29FB6";
  20224. WOLFSSL_SMALL_STACK_STATIC const char* QIUTy =
  20225. "7903FE1008B8BC99A41AE9E95628BC64F2F1B20C2D7E9F5177A3C294D4462299";
  20226. WOLFSSL_SMALL_STACK_STATIC const byte expSig[] = {
  20227. 0x30, 0x46, 0x02, 0x21, 0x00, 0xEF, 0xD4, 0x8B,
  20228. 0x2A, 0xAC, 0xB6, 0xA8, 0xFD, 0x11, 0x40, 0xDD,
  20229. 0x9C, 0xD4, 0x5E, 0x81, 0xD6, 0x9D, 0x2C, 0x87,
  20230. 0x7B, 0x56, 0xAA, 0xF9, 0x91, 0xC3, 0x4D, 0x0E,
  20231. 0xA8, 0x4E, 0xAF, 0x37, 0x16, 0x02, 0x21, 0x00,
  20232. 0xF7, 0xCB, 0x1C, 0x94, 0x2D, 0x65, 0x7C, 0x41,
  20233. 0xD4, 0x36, 0xC7, 0xA1, 0xB6, 0xE2, 0x9F, 0x65,
  20234. 0xF3, 0xE9, 0x00, 0xDB, 0xB9, 0xAF, 0xF4, 0x06,
  20235. 0x4D, 0xC4, 0xAB, 0x2F, 0x84, 0x3A, 0xCD, 0xA8
  20236. };
  20237. #ifdef WOLFSSL_SMALL_STACK
  20238. key = (ecc_key *)XMALLOC(sizeof(*key), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20239. if (key == NULL)
  20240. return MEMORY_E;
  20241. #endif
  20242. ret = wc_ecc_init_ex(key, HEAP_HINT, devId);
  20243. if (ret != 0) {
  20244. goto done;
  20245. }
  20246. key_inited = 1;
  20247. ret = wc_ecc_import_raw(key, QIUTx, QIUTy, dIUT, "SECP256R1");
  20248. if (ret != 0) {
  20249. goto done;
  20250. }
  20251. ret = wc_Hash(WC_HASH_TYPE_SHA256, msg,
  20252. (word32)XSTRLEN((const char*)msg), hash, sizeof(hash));
  20253. if (ret != 0) {
  20254. goto done;
  20255. }
  20256. ret = wc_ecc_set_deterministic(key, 1);
  20257. if (ret != 0) {
  20258. goto done;
  20259. }
  20260. sigSz = sizeof(sig);
  20261. do {
  20262. #if defined(WOLFSSL_ASYNC_CRYPT)
  20263. ret = wc_AsyncWait(ret, key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  20264. #endif
  20265. if (ret == 0)
  20266. ret = wc_ecc_sign_hash(hash, sizeof(hash), sig, &sigSz, rng, key);
  20267. } while (ret == WC_PENDING_E);
  20268. if (ret != 0) {
  20269. goto done;
  20270. }
  20271. TEST_SLEEP();
  20272. if (sigSz != sizeof(expSig)) {
  20273. ret = -9830;
  20274. goto done;
  20275. }
  20276. if (XMEMCMP(sig, expSig, sigSz) != 0) {
  20277. ret = -9831;
  20278. goto done;
  20279. }
  20280. sigSz = sizeof(sig);
  20281. do {
  20282. #if defined(WOLFSSL_ASYNC_CRYPT)
  20283. ret = wc_AsyncWait(ret, key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  20284. #endif
  20285. if (ret == 0)
  20286. ret = wc_ecc_sign_hash(hash, sizeof(hash), sig, &sigSz, rng, key);
  20287. } while (ret == WC_PENDING_E);
  20288. if (ret != 0) {
  20289. goto done;
  20290. }
  20291. TEST_SLEEP();
  20292. done:
  20293. if (key_inited)
  20294. wc_ecc_free(key);
  20295. #ifdef WOLFSSL_SMALL_STACK
  20296. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20297. #endif
  20298. return ret;
  20299. }
  20300. #endif
  20301. #ifdef WOLFSSL_PUBLIC_MP
  20302. #if defined(HAVE_ECC384)
  20303. /* KAT from RFC6979 */
  20304. static int ecc384_test_deterministic_k(WC_RNG* rng)
  20305. {
  20306. int ret;
  20307. #ifdef WOLFSSL_SMALL_STACK
  20308. ecc_key *key;
  20309. mp_int *r, *s, *expR, *expS;
  20310. #else
  20311. ecc_key key[1];
  20312. mp_int r[1], s[1], expR[1], expS[1];
  20313. #endif
  20314. int key_inited = 0;
  20315. WOLFSSL_SMALL_STACK_STATIC const unsigned char msg[] = "sample";
  20316. unsigned char hash[32];
  20317. WOLFSSL_SMALL_STACK_STATIC const char* dIUT =
  20318. "6B9D3DAD2E1B8C1C05B19875B6659F4DE23C3B667BF297BA9AA47740787137D8"
  20319. "96D5724E4C70A825F872C9EA60D2EDF5";
  20320. WOLFSSL_SMALL_STACK_STATIC const char* QIUTx =
  20321. "EC3A4E415B4E19A4568618029F427FA5DA9A8BC4AE92E02E06AAE5286B300C64"
  20322. "DEF8F0EA9055866064A254515480BC13";
  20323. WOLFSSL_SMALL_STACK_STATIC const char* QIUTy =
  20324. "8015D9B72D7D57244EA8EF9AC0C621896708A59367F9DFB9F54CA84B3F1C9DB1"
  20325. "288B231C3AE0D4FE7344FD2533264720";
  20326. WOLFSSL_SMALL_STACK_STATIC const char* expRstr =
  20327. "21B13D1E013C7FA1392D03C5F99AF8B30C570C6F98D4EA8E354B63A21D3DAA33"
  20328. "BDE1E888E63355D92FA2B3C36D8FB2CD";
  20329. WOLFSSL_SMALL_STACK_STATIC const char* expSstr =
  20330. "F3AA443FB107745BF4BD77CB3891674632068A10CA67E3D45DB2266FA7D1FEEB"
  20331. "EFDC63ECCD1AC42EC0CB8668A4FA0AB0";
  20332. #ifdef WOLFSSL_SMALL_STACK
  20333. key = (ecc_key *)XMALLOC(sizeof(*key), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20334. r = (mp_int *)XMALLOC(sizeof(*r), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20335. s = (mp_int *)XMALLOC(sizeof(*s), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20336. expR = (mp_int *)XMALLOC(sizeof(*expR), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20337. expS = (mp_int *)XMALLOC(sizeof(*expS), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20338. if ((key == NULL) ||
  20339. (r == NULL) ||
  20340. (s == NULL) ||
  20341. (expR == NULL) ||
  20342. (expS == NULL))
  20343. {
  20344. ret = MEMORY_E;
  20345. goto done;
  20346. }
  20347. #endif
  20348. ret = mp_init_multi(r, s, expR, expS, NULL, NULL);
  20349. if (ret != MP_OKAY) {
  20350. goto done;
  20351. }
  20352. ret = wc_ecc_init_ex(key, HEAP_HINT, devId);
  20353. if (ret != 0) {
  20354. goto done;
  20355. }
  20356. key_inited = 1;
  20357. ret = wc_ecc_import_raw(key, QIUTx, QIUTy, dIUT, "SECP384R1");
  20358. if (ret != 0) {
  20359. goto done;
  20360. }
  20361. ret = wc_Hash(WC_HASH_TYPE_SHA256, msg,
  20362. (word32)XSTRLEN((const char*)msg), hash, sizeof(hash));
  20363. if (ret != 0) {
  20364. goto done;
  20365. }
  20366. ret = wc_ecc_set_deterministic(key, 1);
  20367. if (ret != 0) {
  20368. goto done;
  20369. }
  20370. do {
  20371. #if defined(WOLFSSL_ASYNC_CRYPT)
  20372. ret = wc_AsyncWait(ret, key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  20373. #endif
  20374. if (ret == 0)
  20375. ret = wc_ecc_sign_hash_ex(hash, sizeof(hash), rng, key, r, s);
  20376. } while (ret == WC_PENDING_E);
  20377. if (ret != 0) {
  20378. goto done;
  20379. }
  20380. TEST_SLEEP();
  20381. mp_read_radix(expR, expRstr, MP_RADIX_HEX);
  20382. mp_read_radix(expS, expSstr, MP_RADIX_HEX);
  20383. if (mp_cmp(r, expR) != MP_EQ) {
  20384. ret = -1;
  20385. }
  20386. done:
  20387. if (key_inited)
  20388. wc_ecc_free(key);
  20389. #ifdef WOLFSSL_SMALL_STACK
  20390. if (key != NULL)
  20391. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20392. if (r != NULL)
  20393. XFREE(r, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20394. if (s != NULL)
  20395. XFREE(s, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20396. if (expR != NULL)
  20397. XFREE(expR, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20398. if (expS != NULL)
  20399. XFREE(expS, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20400. #endif
  20401. return ret;
  20402. }
  20403. #endif /* HAVE_ECC384 */
  20404. #if defined(HAVE_ECC521)
  20405. /* KAT from RFC6979 */
  20406. static int ecc521_test_deterministic_k(WC_RNG* rng)
  20407. {
  20408. int ret;
  20409. #ifdef WOLFSSL_SMALL_STACK
  20410. ecc_key *key;
  20411. mp_int *r, *s, *expR, *expS;
  20412. #else
  20413. ecc_key key[1];
  20414. mp_int r[1], s[1], expR[1], expS[1];
  20415. #endif
  20416. int key_inited = 0;
  20417. WOLFSSL_SMALL_STACK_STATIC const unsigned char msg[] = "sample";
  20418. unsigned char hash[32];
  20419. WOLFSSL_SMALL_STACK_STATIC const char* dIUT =
  20420. "0FAD06DAA62BA3B25D2FB40133DA757205DE67F5BB0018FEE8C86E1B68C7E75C"
  20421. "AA896EB32F1F47C70855836A6D16FCC1466F6D8FBEC67DB89EC0C08B0E996B83"
  20422. "538";
  20423. WOLFSSL_SMALL_STACK_STATIC const char* QIUTx =
  20424. "1894550D0785932E00EAA23B694F213F8C3121F86DC97A04E5A7167DB4E5BCD3"
  20425. "71123D46E45DB6B5D5370A7F20FB633155D38FFA16D2BD761DCAC474B9A2F502"
  20426. "3A4";
  20427. WOLFSSL_SMALL_STACK_STATIC const char* QIUTy =
  20428. "0493101C962CD4D2FDDF782285E64584139C2F91B47F87FF82354D6630F746A2"
  20429. "8A0DB25741B5B34A828008B22ACC23F924FAAFBD4D33F81EA66956DFEAA2BFDF"
  20430. "CF5";
  20431. WOLFSSL_SMALL_STACK_STATIC const char* expRstr =
  20432. "1511BB4D675114FE266FC4372B87682BAECC01D3CC62CF2303C92B3526012659"
  20433. "D16876E25C7C1E57648F23B73564D67F61C6F14D527D54972810421E7D87589E"
  20434. "1A7";
  20435. WOLFSSL_SMALL_STACK_STATIC const char* expSstr =
  20436. "04A171143A83163D6DF460AAF61522695F207A58B95C0644D87E52AA1A347916"
  20437. "E4F7A72930B1BC06DBE22CE3F58264AFD23704CBB63B29B931F7DE6C9D949A7E"
  20438. "CFC";
  20439. #ifdef WOLFSSL_SMALL_STACK
  20440. key = (ecc_key *)XMALLOC(sizeof(*key), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20441. r = (mp_int *)XMALLOC(sizeof(*r), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20442. s = (mp_int *)XMALLOC(sizeof(*s), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20443. expR = (mp_int *)XMALLOC(sizeof(*expR), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20444. expS = (mp_int *)XMALLOC(sizeof(*expS), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20445. if ((key == NULL) ||
  20446. (r == NULL) ||
  20447. (s == NULL) ||
  20448. (expR == NULL) ||
  20449. (expS == NULL))
  20450. {
  20451. ret = MEMORY_E;
  20452. goto done;
  20453. }
  20454. #endif
  20455. ret = mp_init_multi(r, s, expR, expS, NULL, NULL);
  20456. if (ret != MP_OKAY) {
  20457. goto done;
  20458. }
  20459. ret = wc_ecc_init_ex(key, HEAP_HINT, devId);
  20460. if (ret != 0) {
  20461. return ret;
  20462. }
  20463. key_inited = 1;
  20464. ret = wc_ecc_import_raw(key, QIUTx, QIUTy, dIUT, "SECP521R1");
  20465. if (ret != 0) {
  20466. goto done;
  20467. }
  20468. ret = wc_Hash(WC_HASH_TYPE_SHA256, msg,
  20469. (word32)XSTRLEN((const char*)msg), hash, sizeof(hash));
  20470. if (ret != 0) {
  20471. goto done;
  20472. }
  20473. ret = wc_ecc_set_deterministic(key, 1);
  20474. if (ret != 0) {
  20475. goto done;
  20476. }
  20477. do {
  20478. #if defined(WOLFSSL_ASYNC_CRYPT)
  20479. ret = wc_AsyncWait(ret, key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  20480. #endif
  20481. if (ret == 0)
  20482. ret = wc_ecc_sign_hash_ex(hash, sizeof(hash), rng, key, r, s);
  20483. } while (ret == WC_PENDING_E);
  20484. if (ret != 0) {
  20485. goto done;
  20486. }
  20487. TEST_SLEEP();
  20488. mp_read_radix(expR, expRstr, MP_RADIX_HEX);
  20489. mp_read_radix(expS, expSstr, MP_RADIX_HEX);
  20490. if (mp_cmp(r, expR) != MP_EQ) {
  20491. ret = -1;
  20492. }
  20493. done:
  20494. if (key_inited)
  20495. wc_ecc_free(key);
  20496. #ifdef WOLFSSL_SMALL_STACK
  20497. if (key != NULL)
  20498. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20499. if (r != NULL)
  20500. XFREE(r, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20501. if (s != NULL)
  20502. XFREE(s, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20503. if (expR != NULL)
  20504. XFREE(expR, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20505. if (expS != NULL)
  20506. XFREE(expS, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20507. #endif
  20508. return ret;
  20509. }
  20510. #endif /* HAVE_ECC521 */
  20511. #endif /* WOLFSSL_PUBLIC_MP */
  20512. #endif /* HAVE_ECC_SIGN && (WOLFSSL_ECDSA_DETERMINISTIC_K ||
  20513. WOLFSSL_ECDSA_DETERMINISTIC_K_VARIANT)
  20514. && (!FIPS_VERSION_GE || FIPS_VERSION_GE(5,3)) */
  20515. #if defined(HAVE_ECC_SIGN) && defined(WOLFSSL_ECDSA_SET_K) && \
  20516. !defined(WOLFSSL_KCAPI_ECC)
  20517. static int ecc_test_sign_vectors(WC_RNG* rng)
  20518. {
  20519. int ret;
  20520. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20521. ecc_key *key = NULL;
  20522. #else
  20523. ecc_key key[1];
  20524. #endif
  20525. int key_inited = 0;
  20526. byte sig[72];
  20527. word32 sigSz;
  20528. WOLFSSL_SMALL_STACK_STATIC const unsigned char hash[32] = "test wolfSSL deterministic sign";
  20529. WOLFSSL_SMALL_STACK_STATIC const char* dIUT = "7d7dc5f71eb29ddaf80d6214632eeae03d9058af1fb6d22ed80badb62bc1a534";
  20530. WOLFSSL_SMALL_STACK_STATIC const char* QIUTx = "ead218590119e8876b29146ff89ca61770c4edbbf97d38ce385ed281d8a6b230";
  20531. WOLFSSL_SMALL_STACK_STATIC const char* QIUTy = "28af61281fd35e2fa7002523acc85a429cb06ee6648325389f59edfce1405141";
  20532. WOLFSSL_SMALL_STACK_STATIC const byte k[1] = { 0x02 };
  20533. WOLFSSL_SMALL_STACK_STATIC const byte expSig[71] = {
  20534. 0x30, 0x45, 0x02, 0x20, 0x7c, 0xf2, 0x7b, 0x18,
  20535. 0x8d, 0x03, 0x4f, 0x7e, 0x8a, 0x52, 0x38, 0x03,
  20536. 0x04, 0xb5, 0x1a, 0xc3, 0xc0, 0x89, 0x69, 0xe2,
  20537. 0x77, 0xf2, 0x1b, 0x35, 0xa6, 0x0b, 0x48, 0xfc,
  20538. 0x47, 0x66, 0x99, 0x78, 0x02, 0x21, 0x00, 0xa8,
  20539. 0x43, 0xa0, 0xce, 0x6c, 0x5e, 0x17, 0x8a, 0x53,
  20540. 0x4d, 0xaf, 0xd2, 0x95, 0x78, 0x9f, 0x84, 0x4f,
  20541. 0x94, 0xb8, 0x75, 0xa3, 0x19, 0xa5, 0xd4, 0xdf,
  20542. 0xe1, 0xd4, 0x5e, 0x9d, 0x97, 0xfe, 0x81
  20543. };
  20544. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20545. if ((key = (ecc_key *)XMALLOC(sizeof(*key), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER)) == NULL)
  20546. return MEMORY_E;
  20547. #endif
  20548. ret = wc_ecc_init_ex(key, HEAP_HINT, devId);
  20549. if (ret != 0) {
  20550. goto done;
  20551. }
  20552. key_inited = 1;
  20553. ret = wc_ecc_import_raw(key, QIUTx, QIUTy, dIUT, "SECP256R1");
  20554. if (ret != 0) {
  20555. goto done;
  20556. }
  20557. wc_ecc_set_flags(key, WC_ECC_FLAG_DEC_SIGN);
  20558. ret = wc_ecc_sign_set_k(k, sizeof(k), key);
  20559. if (ret != 0) {
  20560. goto done;
  20561. }
  20562. sigSz = sizeof(sig);
  20563. do {
  20564. #if defined(WOLFSSL_ASYNC_CRYPT)
  20565. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  20566. #endif
  20567. if (ret == 0)
  20568. ret = wc_ecc_sign_hash(hash, sizeof(hash), sig, &sigSz, rng, key);
  20569. } while (ret == WC_PENDING_E);
  20570. if (ret != 0) {
  20571. goto done;
  20572. }
  20573. TEST_SLEEP();
  20574. if (sigSz != sizeof(expSig)) {
  20575. ret = -9830;
  20576. goto done;
  20577. }
  20578. if (XMEMCMP(sig, expSig, sigSz) != 0) {
  20579. ret = -9831;
  20580. goto done;
  20581. }
  20582. sigSz = sizeof(sig);
  20583. do {
  20584. #if defined(WOLFSSL_ASYNC_CRYPT)
  20585. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  20586. #endif
  20587. if (ret == 0)
  20588. ret = wc_ecc_sign_hash(hash, sizeof(hash), sig, &sigSz, rng, key);
  20589. } while (ret == WC_PENDING_E);
  20590. if (ret != 0) {
  20591. goto done;
  20592. }
  20593. TEST_SLEEP();
  20594. done:
  20595. if (key_inited)
  20596. wc_ecc_free(key);
  20597. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20598. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20599. #endif
  20600. return ret;
  20601. }
  20602. #endif
  20603. #ifdef HAVE_ECC_CDH
  20604. static int ecc_test_cdh_vectors(WC_RNG* rng)
  20605. {
  20606. int ret;
  20607. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20608. ecc_key *pub_key = (ecc_key *)XMALLOC(sizeof *pub_key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20609. ecc_key *priv_key = (ecc_key *)XMALLOC(sizeof *priv_key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20610. #else
  20611. ecc_key pub_key[1], priv_key[1];
  20612. #endif
  20613. byte sharedA[32] = {0}, sharedB[32] = {0};
  20614. word32 x, z;
  20615. WOLFSSL_SMALL_STACK_STATIC const char* QCAVSx = "700c48f77f56584c5cc632ca65640db91b6bacce3a4df6b42ce7cc838833d287";
  20616. WOLFSSL_SMALL_STACK_STATIC const char* QCAVSy = "db71e509e3fd9b060ddb20ba5c51dcc5948d46fbf640dfe0441782cab85fa4ac";
  20617. WOLFSSL_SMALL_STACK_STATIC const char* dIUT = "7d7dc5f71eb29ddaf80d6214632eeae03d9058af1fb6d22ed80badb62bc1a534";
  20618. WOLFSSL_SMALL_STACK_STATIC const char* QIUTx = "ead218590119e8876b29146ff89ca61770c4edbbf97d38ce385ed281d8a6b230";
  20619. WOLFSSL_SMALL_STACK_STATIC const char* QIUTy = "28af61281fd35e2fa7002523acc85a429cb06ee6648325389f59edfce1405141";
  20620. WOLFSSL_SMALL_STACK_STATIC const char* ZIUT = "46fc62106420ff012e54a434fbdd2d25ccc5852060561e68040dd7778997bd7b";
  20621. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20622. if ((pub_key == NULL) ||
  20623. (priv_key == NULL)) {
  20624. ret = MEMORY_E;
  20625. goto done;
  20626. }
  20627. #endif
  20628. XMEMSET(pub_key, 0, sizeof *pub_key);
  20629. XMEMSET(priv_key, 0, sizeof *priv_key);
  20630. /* setup private and public keys */
  20631. ret = wc_ecc_init_ex(pub_key, HEAP_HINT, devId);
  20632. if (ret != 0)
  20633. goto done;
  20634. ret = wc_ecc_init_ex(priv_key, HEAP_HINT, devId);
  20635. if (ret != 0)
  20636. goto done;
  20637. wc_ecc_set_flags(pub_key, WC_ECC_FLAG_COFACTOR);
  20638. wc_ecc_set_flags(priv_key, WC_ECC_FLAG_COFACTOR);
  20639. ret = wc_ecc_import_raw(pub_key, QCAVSx, QCAVSy, NULL, "SECP256R1");
  20640. if (ret != 0)
  20641. goto done;
  20642. ret = wc_ecc_import_raw(priv_key, QIUTx, QIUTy, dIUT, "SECP256R1");
  20643. if (ret != 0)
  20644. goto done;
  20645. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  20646. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  20647. !defined(HAVE_SELFTEST)
  20648. ret = wc_ecc_set_rng(priv_key, rng);
  20649. if (ret != 0)
  20650. goto done;
  20651. #else
  20652. (void)rng;
  20653. #endif
  20654. /* compute ECC Cofactor shared secret */
  20655. x = sizeof(sharedA);
  20656. do {
  20657. #if defined(WOLFSSL_ASYNC_CRYPT)
  20658. ret = wc_AsyncWait(ret, &priv_key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  20659. #endif
  20660. if (ret == 0)
  20661. ret = wc_ecc_shared_secret(priv_key, pub_key, sharedA, &x);
  20662. } while (ret == WC_PENDING_E);
  20663. if (ret != 0) {
  20664. goto done;
  20665. }
  20666. TEST_SLEEP();
  20667. /* read in expected Z */
  20668. z = sizeof(sharedB);
  20669. ret = Base16_Decode((const byte*)ZIUT, (word32)XSTRLEN(ZIUT), sharedB, &z);
  20670. if (ret != 0)
  20671. goto done;
  20672. /* compare results */
  20673. if (x != z || XMEMCMP(sharedA, sharedB, x)) {
  20674. ERROR_OUT(-9840, done);
  20675. }
  20676. done:
  20677. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20678. if (priv_key) {
  20679. wc_ecc_free(priv_key);
  20680. XFREE(priv_key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20681. }
  20682. if (pub_key) {
  20683. wc_ecc_free(pub_key);
  20684. XFREE(pub_key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20685. }
  20686. #else
  20687. wc_ecc_free(priv_key);
  20688. wc_ecc_free(pub_key);
  20689. #endif
  20690. return ret;
  20691. }
  20692. #endif /* HAVE_ECC_CDH */
  20693. #endif /* HAVE_ECC_VECTOR_TEST */
  20694. #ifdef HAVE_ECC_KEY_IMPORT
  20695. /* returns 0 on success */
  20696. static int ecc_test_make_pub(WC_RNG* rng)
  20697. {
  20698. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20699. ecc_key *key = (ecc_key *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20700. #if defined(HAVE_ECC_DHE) && defined(HAVE_ECC_KEY_EXPORT)
  20701. ecc_key *pub = (ecc_key *)XMALLOC(sizeof *pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20702. #endif
  20703. byte *exportBuf = (byte *)XMALLOC(ECC_BUFSIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20704. byte *tmp = (byte *)XMALLOC(ECC_BUFSIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20705. #else
  20706. ecc_key key[1];
  20707. #if defined(HAVE_ECC_DHE) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG) && \
  20708. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  20709. ecc_key pub[1];
  20710. #endif
  20711. byte exportBuf[ECC_BUFSIZE];
  20712. byte tmp[ECC_BUFSIZE];
  20713. #endif
  20714. const byte* msg = (const byte*)"test wolfSSL ECC public gen";
  20715. word32 x;
  20716. word32 tmpSz;
  20717. int ret = 0;
  20718. ecc_point* pubPoint = NULL;
  20719. #ifdef HAVE_ECC_VERIFY
  20720. int verify = 0;
  20721. #endif
  20722. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20723. if ((key == NULL) ||
  20724. #if defined(HAVE_ECC_DHE) && defined(HAVE_ECC_KEY_EXPORT)
  20725. (pub == NULL) ||
  20726. #endif
  20727. (exportBuf == NULL) ||
  20728. (tmp == NULL))
  20729. ERROR_OUT(MEMORY_E, done);
  20730. #endif
  20731. (void)msg;
  20732. (void)verify;
  20733. (void)exportBuf;
  20734. (void)rng;
  20735. wc_ecc_init_ex(key, HEAP_HINT, devId);
  20736. #ifndef NO_ECC256
  20737. #ifdef USE_CERT_BUFFERS_256
  20738. XMEMCPY(tmp, ecc_key_der_256, (size_t)sizeof_ecc_key_der_256);
  20739. tmpSz = (size_t)sizeof_ecc_key_der_256;
  20740. #else
  20741. {
  20742. XFILE file = XFOPEN(eccKeyDerFile, "rb");
  20743. if (!file) {
  20744. ERROR_OUT(-9850, done);
  20745. }
  20746. tmpSz = (word32)XFREAD(tmp, 1, ECC_BUFSIZE, file);
  20747. XFCLOSE(file);
  20748. }
  20749. #endif /* USE_CERT_BUFFERS_256 */
  20750. /* import private only then test with */
  20751. ret = wc_ecc_import_private_key(tmp, tmpSz, NULL, 0, NULL);
  20752. if (ret == 0) {
  20753. ERROR_OUT(-9851, done);
  20754. }
  20755. ret = wc_ecc_import_private_key(NULL, tmpSz, NULL, 0, key);
  20756. if (ret == 0) {
  20757. ERROR_OUT(-9852, done);
  20758. }
  20759. x = 0;
  20760. ret = wc_EccPrivateKeyDecode(tmp, &x, key, tmpSz);
  20761. if (ret != 0) {
  20762. ERROR_OUT(-9853, done);
  20763. }
  20764. #ifdef HAVE_ECC_KEY_EXPORT
  20765. x = ECC_BUFSIZE;
  20766. ret = wc_ecc_export_private_only(key, exportBuf, &x);
  20767. if (ret != 0) {
  20768. ERROR_OUT(-9854, done);
  20769. }
  20770. /* make private only key */
  20771. wc_ecc_free(key);
  20772. wc_ecc_init_ex(key, HEAP_HINT, devId);
  20773. ret = wc_ecc_import_private_key(exportBuf, x, NULL, 0, key);
  20774. if (ret != 0) {
  20775. ERROR_OUT(-9855, done);
  20776. }
  20777. x = ECC_BUFSIZE;
  20778. ret = wc_ecc_export_x963_ex(key, exportBuf, &x, 0);
  20779. if (ret == 0) {
  20780. ERROR_OUT(-9856, done);
  20781. }
  20782. #endif /* HAVE_ECC_KEY_EXPORT */
  20783. ret = wc_ecc_make_pub(NULL, NULL);
  20784. if (ret == 0) {
  20785. ERROR_OUT(-9857, done);
  20786. }
  20787. TEST_SLEEP();
  20788. #ifndef WOLFSSL_NO_MALLOC
  20789. pubPoint = wc_ecc_new_point_h(HEAP_HINT);
  20790. if (pubPoint == NULL) {
  20791. ERROR_OUT(-9858, done);
  20792. }
  20793. #if !defined(WOLFSSL_CRYPTOCELL)
  20794. ret = wc_ecc_make_pub(key, pubPoint);
  20795. #if defined(WOLFSSL_ASYNC_CRYPT)
  20796. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  20797. #endif
  20798. if (ret != 0) {
  20799. ERROR_OUT(-9859, done);
  20800. }
  20801. #endif
  20802. TEST_SLEEP();
  20803. #ifdef HAVE_ECC_KEY_EXPORT
  20804. /* export should still fail, is private only key */
  20805. x = ECC_BUFSIZE;
  20806. ret = wc_ecc_export_x963_ex(key, exportBuf, &x, 0);
  20807. if (ret == 0) {
  20808. ERROR_OUT(-9860, done);
  20809. }
  20810. #endif /* HAVE_ECC_KEY_EXPORT */
  20811. #endif /* !WOLFSSL_NO_MALLOC */
  20812. #endif /* !NO_ECC256 */
  20813. /* create a new key since above test for loading key is not supported */
  20814. #if defined(WOLFSSL_CRYPTOCELL) || defined(NO_ECC256) || \
  20815. defined(WOLFSSL_QNX_CAAM) || defined(WOLFSSL_SE050) || \
  20816. defined(WOLFSSL_SECO_CAAM)
  20817. ret = wc_ecc_make_key(rng, ECC_KEYGEN_SIZE, key);
  20818. if (ret != 0) {
  20819. ERROR_OUT(-9861, done);
  20820. }
  20821. #endif
  20822. #if defined(HAVE_ECC_SIGN) && (!defined(ECC_TIMING_RESISTANT) || \
  20823. (defined(ECC_TIMING_RESISTANT) && !defined(WC_NO_RNG))) && \
  20824. !defined(WOLF_CRYPTO_CB_ONLY_ECC) && !defined(HAVE_ECC_DETERMINISTIC_K)
  20825. tmpSz = ECC_BUFSIZE;
  20826. ret = 0;
  20827. do {
  20828. #if defined(WOLFSSL_ASYNC_CRYPT)
  20829. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  20830. #endif
  20831. if (ret == 0) {
  20832. ret = wc_ecc_sign_hash(msg, (word32)XSTRLEN((const char* )msg), tmp,
  20833. &tmpSz, rng, key);
  20834. }
  20835. } while (ret == WC_PENDING_E);
  20836. if (ret != 0) {
  20837. ERROR_OUT(-9862, done);
  20838. }
  20839. TEST_SLEEP();
  20840. #ifdef HAVE_ECC_VERIFY
  20841. /* try verify with private only key */
  20842. ret = 0;
  20843. do {
  20844. #if defined(WOLFSSL_ASYNC_CRYPT)
  20845. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  20846. #endif
  20847. if (ret == 0) {
  20848. ret = wc_ecc_verify_hash(tmp, tmpSz, msg,
  20849. (word32)XSTRLEN((const char*)msg), &verify, key);
  20850. }
  20851. } while (ret == WC_PENDING_E);
  20852. if (ret != 0) {
  20853. ERROR_OUT(-9863, done);
  20854. }
  20855. if (verify != 1) {
  20856. ERROR_OUT(-9864, done);
  20857. }
  20858. TEST_SLEEP();
  20859. #ifdef HAVE_ECC_KEY_EXPORT
  20860. /* exporting the public part should now work */
  20861. x = ECC_BUFSIZE;
  20862. ret = wc_ecc_export_x963_ex(key, exportBuf, &x, 0);
  20863. if (ret != 0) {
  20864. ERROR_OUT(-9865, done);
  20865. }
  20866. #endif /* HAVE_ECC_KEY_EXPORT */
  20867. #endif /* HAVE_ECC_VERIFY */
  20868. #endif /* HAVE_ECC_SIGN */
  20869. #if defined(HAVE_ECC_DHE) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  20870. /* now test private only key with creating a shared secret */
  20871. x = ECC_BUFSIZE;
  20872. ret = wc_ecc_export_private_only(key, exportBuf, &x);
  20873. if (ret != 0) {
  20874. ERROR_OUT(-9866, done);
  20875. }
  20876. #if !defined(WOLFSSL_QNX_CAAM) && !defined(WOLFSSL_SE050)
  20877. /* make private only key */
  20878. wc_ecc_free(key);
  20879. wc_ecc_init_ex(key, HEAP_HINT, devId);
  20880. ret = wc_ecc_import_private_key(exportBuf, x, NULL, 0, key);
  20881. if (ret != 0) {
  20882. ERROR_OUT(-9867, done);
  20883. }
  20884. /* check that public export fails with private only key */
  20885. x = ECC_BUFSIZE;
  20886. ret = wc_ecc_export_x963_ex(key, exportBuf, &x, 0);
  20887. if (ret == 0) {
  20888. ERROR_OUT(-9868, done);
  20889. }
  20890. #endif /* WOLFSSL_QNX_CAAM */
  20891. #ifndef WOLF_CRYPTO_CB_ONLY_ECC
  20892. /* make public key for shared secret */
  20893. wc_ecc_init_ex(pub, HEAP_HINT, devId);
  20894. ret = wc_ecc_make_key(rng, ECC_KEYGEN_SIZE, pub);
  20895. #ifdef HAVE_ECC_CDH
  20896. wc_ecc_set_flags(key, WC_ECC_FLAG_COFACTOR);
  20897. #endif
  20898. #if defined(WOLFSSL_ASYNC_CRYPT)
  20899. ret = wc_AsyncWait(ret, &pub->asyncDev, WC_ASYNC_FLAG_NONE);
  20900. #endif
  20901. if (ret != 0) {
  20902. ERROR_OUT(-9869, done);
  20903. }
  20904. TEST_SLEEP();
  20905. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  20906. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  20907. !defined(HAVE_SELFTEST)
  20908. ret = wc_ecc_set_rng(key, rng);
  20909. if (ret != 0)
  20910. goto done;
  20911. #endif
  20912. x = ECC_BUFSIZE;
  20913. do {
  20914. #if defined(WOLFSSL_ASYNC_CRYPT)
  20915. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  20916. #endif
  20917. if (ret == 0) {
  20918. ret = wc_ecc_shared_secret(key, pub, exportBuf, &x);
  20919. }
  20920. } while (ret == WC_PENDING_E);
  20921. wc_ecc_free(pub);
  20922. if (ret != 0) {
  20923. ERROR_OUT(-9870, done);
  20924. }
  20925. TEST_SLEEP();
  20926. #endif /* HAVE_ECC_DHE && HAVE_ECC_KEY_EXPORT && !WC_NO_RNG */
  20927. #endif /* WOLF_CRYPTO_CB_ONLY_ECC */
  20928. ret = 0;
  20929. done:
  20930. wc_ecc_del_point_h(pubPoint, HEAP_HINT);
  20931. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20932. if (key != NULL) {
  20933. wc_ecc_free(key);
  20934. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20935. }
  20936. #if defined(HAVE_ECC_DHE) && defined(HAVE_ECC_KEY_EXPORT)
  20937. if (pub != NULL)
  20938. XFREE(pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20939. #endif
  20940. if (exportBuf != NULL)
  20941. XFREE(exportBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20942. if (tmp != NULL)
  20943. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20944. #else
  20945. wc_ecc_free(key);
  20946. #endif
  20947. return ret;
  20948. }
  20949. #if defined(HAVE_ECC_KEY_EXPORT) && !defined(NO_ASN_CRYPT) && \
  20950. !defined(WC_NO_RNG) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  20951. static int ecc_test_key_decode(WC_RNG* rng, int keySize)
  20952. {
  20953. int ret;
  20954. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20955. ecc_key *eccKey = (ecc_key *)XMALLOC(sizeof *eccKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20956. byte *tmpBuf = (byte *)XMALLOC(ECC_BUFSIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20957. #else
  20958. ecc_key eccKey[1];
  20959. byte tmpBuf[ECC_BUFSIZE];
  20960. #endif
  20961. word32 tmpSz;
  20962. word32 idx;
  20963. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20964. if ((eccKey == NULL) || (tmpBuf == NULL))
  20965. ERROR_OUT(MEMORY_E, done);
  20966. #endif
  20967. ret = wc_ecc_init(eccKey);
  20968. if (ret != 0) {
  20969. goto done;
  20970. }
  20971. ret = wc_ecc_make_key(rng, keySize, eccKey);
  20972. #if defined(WOLFSSL_ASYNC_CRYPT)
  20973. ret = wc_AsyncWait(ret, &eccKey->asyncDev, WC_ASYNC_FLAG_NONE);
  20974. #endif
  20975. if (ret != 0) {
  20976. goto done;
  20977. }
  20978. tmpSz = ECC_BUFSIZE;
  20979. ret = wc_EccKeyToDer(eccKey, tmpBuf, tmpSz);
  20980. wc_ecc_free(eccKey);
  20981. if (ret < 0) {
  20982. goto done;
  20983. }
  20984. tmpSz = ret;
  20985. ret = wc_ecc_init(eccKey);
  20986. if (ret != 0) {
  20987. goto done;
  20988. }
  20989. idx = 0;
  20990. ret = wc_EccPrivateKeyDecode(tmpBuf, &idx, eccKey, tmpSz);
  20991. if (ret != 0) {
  20992. goto done;
  20993. }
  20994. wc_ecc_free(eccKey);
  20995. ret = wc_ecc_init(eccKey);
  20996. if (ret != 0) {
  20997. goto done;
  20998. }
  20999. idx = 0;
  21000. ret = wc_EccPublicKeyDecode(tmpBuf, &idx, eccKey, tmpSz);
  21001. if (ret != 0) {
  21002. goto done;
  21003. }
  21004. ret = 0;
  21005. done:
  21006. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21007. if (eccKey != NULL) {
  21008. wc_ecc_free(eccKey);
  21009. XFREE(eccKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21010. }
  21011. if (tmpBuf != NULL)
  21012. XFREE(tmpBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21013. #else
  21014. wc_ecc_free(eccKey);
  21015. #endif
  21016. return ret;
  21017. }
  21018. #endif /* HAVE_ECC_KEY_EXPORT && !NO_ASN_CRYPT */
  21019. #endif /* HAVE_ECC_KEY_IMPORT */
  21020. #if defined(HAVE_ECC_KEY_EXPORT) && !defined(NO_ASN_CRYPT) && \
  21021. !defined(WC_NO_RNG) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  21022. static int ecc_test_key_gen(WC_RNG* rng, int keySize)
  21023. {
  21024. int ret = 0;
  21025. int derSz;
  21026. #ifdef HAVE_PKCS8
  21027. word32 pkcs8Sz;
  21028. #endif
  21029. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21030. byte *der = (byte *)XMALLOC(ECC_BUFSIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21031. ecc_key *userA = (ecc_key *)XMALLOC(sizeof *userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21032. #else
  21033. byte der[ECC_BUFSIZE];
  21034. ecc_key userA[1];
  21035. #endif
  21036. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21037. if ((der == NULL) || (userA == NULL))
  21038. ERROR_OUT(MEMORY_E, done);
  21039. #endif
  21040. ret = wc_ecc_init_ex(userA, HEAP_HINT, devId);
  21041. if (ret != 0)
  21042. goto done;
  21043. ret = wc_ecc_make_key(rng, keySize, userA);
  21044. #if defined(WOLFSSL_ASYNC_CRYPT)
  21045. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_NONE);
  21046. #endif
  21047. if (ret != 0)
  21048. goto done;
  21049. TEST_SLEEP();
  21050. ret = wc_ecc_check_key(userA);
  21051. if (ret != 0)
  21052. goto done;
  21053. TEST_SLEEP();
  21054. derSz = wc_EccKeyToDer(userA, der, ECC_BUFSIZE);
  21055. if (derSz < 0) {
  21056. ERROR_OUT(derSz, done);
  21057. }
  21058. ret = SaveDerAndPem(der, derSz, eccCaKeyTempFile, eccCaKeyPemFile,
  21059. ECC_PRIVATEKEY_TYPE, -8347);
  21060. if (ret != 0) {
  21061. goto done;
  21062. }
  21063. /* test export of public key */
  21064. derSz = wc_EccPublicKeyToDer(userA, der, ECC_BUFSIZE, 1);
  21065. if (derSz < 0) {
  21066. ERROR_OUT(derSz, done);
  21067. }
  21068. if (derSz == 0) {
  21069. ERROR_OUT(-9890, done);
  21070. }
  21071. #ifdef HAVE_COMP_KEY
  21072. /* test export of compressed public key */
  21073. derSz = wc_EccPublicKeyToDer_ex(userA, der, ECC_BUFSIZE, 1, 1);
  21074. if (derSz < 0) {
  21075. ERROR_OUT(derSz, done);
  21076. }
  21077. if (derSz == 0) {
  21078. ERROR_OUT(-9890, done);
  21079. }
  21080. #endif
  21081. ret = SaveDerAndPem(der, derSz, eccPubKeyDerFile, NULL, 0, -8348);
  21082. if (ret != 0) {
  21083. goto done;
  21084. }
  21085. #ifdef HAVE_PKCS8
  21086. /* test export of PKCS#8 unencrypted private key */
  21087. pkcs8Sz = FOURK_BUF;
  21088. derSz = wc_EccPrivateKeyToPKCS8(userA, der, &pkcs8Sz);
  21089. if (derSz < 0) {
  21090. ERROR_OUT(derSz, done);
  21091. }
  21092. if (derSz == 0) {
  21093. ERROR_OUT(-9891, done);
  21094. }
  21095. ret = SaveDerAndPem(der, derSz, eccPkcs8KeyDerFile, NULL, 0, -8349);
  21096. if (ret != 0) {
  21097. goto done;
  21098. }
  21099. #endif /* HAVE_PKCS8 */
  21100. done:
  21101. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21102. if (der != NULL)
  21103. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21104. if (userA != NULL) {
  21105. wc_ecc_free(userA);
  21106. XFREE(userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21107. }
  21108. #else
  21109. wc_ecc_free(userA);
  21110. #endif
  21111. return ret;
  21112. }
  21113. #endif /* HAVE_ECC_KEY_EXPORT && !NO_ASN_CRYPT */
  21114. static int ecc_test_curve_size(WC_RNG* rng, int keySize, int testVerifyCount,
  21115. int curve_id, const ecc_set_type* dp)
  21116. {
  21117. #if (defined(HAVE_ECC_DHE) || defined(HAVE_ECC_CDH)) && !defined(WC_NO_RNG) && \
  21118. !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A)
  21119. WC_DECLARE_VAR(sharedA, byte, ECC_SHARED_SIZE, HEAP_HINT);
  21120. WC_DECLARE_VAR(sharedB, byte, ECC_SHARED_SIZE, HEAP_HINT);
  21121. #endif
  21122. #ifdef HAVE_ECC_KEY_EXPORT
  21123. #define ECC_KEY_EXPORT_BUF_SIZE (MAX_ECC_BYTES * 2 + 32)
  21124. WC_DECLARE_VAR(exportBuf, byte, ECC_KEY_EXPORT_BUF_SIZE, HEAP_HINT);
  21125. #endif
  21126. word32 x = 0;
  21127. #if (defined(HAVE_ECC_DHE) || defined(HAVE_ECC_CDH)) && !defined(WC_NO_RNG) && \
  21128. !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A)
  21129. word32 y;
  21130. #endif
  21131. #if defined(HAVE_ECC_SIGN) && !defined(WOLFSSL_KCAPI_ECC)
  21132. WC_DECLARE_VAR(sig, byte, ECC_SIG_SIZE, HEAP_HINT);
  21133. WC_DECLARE_VAR(digest, byte, ECC_DIGEST_SIZE, HEAP_HINT);
  21134. int i;
  21135. #ifdef HAVE_ECC_VERIFY
  21136. int verify;
  21137. #endif /* HAVE_ECC_VERIFY */
  21138. #endif /* HAVE_ECC_SIGN */
  21139. int ret;
  21140. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21141. ecc_key *userA = (ecc_key *)XMALLOC(sizeof *userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21142. ecc_key *userB = (ecc_key *)XMALLOC(sizeof *userB, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21143. ecc_key *pubKey = (ecc_key *)XMALLOC(sizeof *pubKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21144. #else
  21145. ecc_key userA[1];
  21146. ecc_key userB[1];
  21147. ecc_key pubKey[1];
  21148. #endif
  21149. #ifndef WC_NO_RNG
  21150. int curveSize;
  21151. #endif
  21152. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  21153. #if (defined(HAVE_ECC_DHE) || defined(HAVE_ECC_CDH)) && !defined(WC_NO_RNG) && \
  21154. !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A)
  21155. if (sharedA == NULL || sharedB == NULL)
  21156. ERROR_OUT(-9900, done);
  21157. #endif
  21158. #ifdef HAVE_ECC_KEY_EXPORT
  21159. if (exportBuf == NULL)
  21160. ERROR_OUT(-9901, done);
  21161. #endif
  21162. #if defined(HAVE_ECC_SIGN) && !defined(WOLFSSL_KCAPI_ECC)
  21163. if (sig == NULL || digest == NULL)
  21164. ERROR_OUT(-9902, done);
  21165. #endif
  21166. #endif /* WOLFSSL_SMALL_STACK && !WOLFSSL_NO_MALLOC */
  21167. (void)testVerifyCount;
  21168. (void)dp;
  21169. (void)x;
  21170. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21171. if ((userA == NULL) ||
  21172. (userB == NULL) ||
  21173. (pubKey == NULL))
  21174. ERROR_OUT(-9903, done);
  21175. #endif
  21176. XMEMSET(userA, 0, sizeof *userA);
  21177. XMEMSET(userB, 0, sizeof *userB);
  21178. XMEMSET(pubKey, 0, sizeof *pubKey);
  21179. ret = wc_ecc_init_ex(userA, HEAP_HINT, devId);
  21180. if (ret != 0)
  21181. ERROR_OUT(-9904, done);
  21182. ret = wc_ecc_init_ex(userB, HEAP_HINT, devId);
  21183. if (ret != 0)
  21184. ERROR_OUT(-9905, done);
  21185. ret = wc_ecc_init_ex(pubKey, HEAP_HINT, devId);
  21186. if (ret != 0)
  21187. ERROR_OUT(-9906, done);
  21188. #ifdef WOLFSSL_CUSTOM_CURVES
  21189. if (dp != NULL) {
  21190. ret = wc_ecc_set_custom_curve(userA, dp);
  21191. if (ret != 0)
  21192. ERROR_OUT(-9907, done);
  21193. ret = wc_ecc_set_custom_curve(userB, dp);
  21194. if (ret != 0)
  21195. ERROR_OUT(-9908, done);
  21196. }
  21197. #endif
  21198. #ifndef WC_NO_RNG
  21199. ret = wc_ecc_make_key_ex(rng, keySize, userA, curve_id);
  21200. #if defined(WOLFSSL_ASYNC_CRYPT)
  21201. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_NONE);
  21202. #endif
  21203. #ifdef WOLF_CRYPTO_CB_ONLY_ECC
  21204. if (ret == NO_VALID_DEVID) {
  21205. ret = 0;
  21206. goto done; /* no software case */
  21207. }
  21208. #endif
  21209. if (ret == ECC_CURVE_OID_E)
  21210. goto done; /* catch case, where curve is not supported */
  21211. if (ret != 0)
  21212. ERROR_OUT(-9910, done);
  21213. TEST_SLEEP();
  21214. if (wc_ecc_get_curve_idx(curve_id) != -1) {
  21215. curveSize = wc_ecc_get_curve_size_from_id(userA->dp->id);
  21216. if (curveSize != userA->dp->size)
  21217. ERROR_OUT(-9911, done);
  21218. }
  21219. ret = wc_ecc_check_key(userA);
  21220. if (ret != 0)
  21221. ERROR_OUT(-9912, done);
  21222. TEST_SLEEP();
  21223. /* ATECC508/608 configuration may not support more than one ECDH key */
  21224. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A)
  21225. ret = wc_ecc_make_key_ex(rng, keySize, userB, curve_id);
  21226. #if defined(WOLFSSL_ASYNC_CRYPT)
  21227. ret = wc_AsyncWait(ret, &userB->asyncDev, WC_ASYNC_FLAG_NONE);
  21228. #endif
  21229. if (ret != 0)
  21230. ERROR_OUT(-9914, done);
  21231. TEST_SLEEP();
  21232. /* only perform the below tests if the key size matches */
  21233. if (dp == NULL && keySize > 0 && wc_ecc_size(userA) != keySize)
  21234. ERROR_OUT(ECC_CURVE_OID_E, done);
  21235. #ifdef HAVE_ECC_DHE
  21236. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  21237. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  21238. !defined(HAVE_SELFTEST)
  21239. ret = wc_ecc_set_rng(userA, rng);
  21240. if (ret != 0)
  21241. ERROR_OUT(-9915, done);
  21242. ret = wc_ecc_set_rng(userB, rng);
  21243. if (ret != 0)
  21244. ERROR_OUT(-9916, done);
  21245. #endif
  21246. x = ECC_SHARED_SIZE;
  21247. do {
  21248. #if defined(WOLFSSL_ASYNC_CRYPT)
  21249. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  21250. #endif
  21251. if (ret == 0)
  21252. ret = wc_ecc_shared_secret(userA, userB, sharedA, &x);
  21253. } while (ret == WC_PENDING_E);
  21254. if (ret != 0) {
  21255. ERROR_OUT(-9917, done);
  21256. }
  21257. TEST_SLEEP();
  21258. y = ECC_SHARED_SIZE;
  21259. do {
  21260. #if defined(WOLFSSL_ASYNC_CRYPT)
  21261. ret = wc_AsyncWait(ret, &userB->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  21262. #endif
  21263. if (ret == 0)
  21264. ret = wc_ecc_shared_secret(userB, userA, sharedB, &y);
  21265. } while (ret == WC_PENDING_E);
  21266. if (ret != 0)
  21267. ERROR_OUT(-9918, done);
  21268. if (y != x)
  21269. ERROR_OUT(-9919, done);
  21270. if (XMEMCMP(sharedA, sharedB, x))
  21271. ERROR_OUT(-9920, done);
  21272. TEST_SLEEP();
  21273. #endif /* HAVE_ECC_DHE */
  21274. #ifdef HAVE_ECC_CDH
  21275. /* add cofactor flag */
  21276. wc_ecc_set_flags(userA, WC_ECC_FLAG_COFACTOR);
  21277. wc_ecc_set_flags(userB, WC_ECC_FLAG_COFACTOR);
  21278. x = ECC_SHARED_SIZE;
  21279. do {
  21280. #if defined(WOLFSSL_ASYNC_CRYPT)
  21281. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  21282. #endif
  21283. if (ret == 0)
  21284. ret = wc_ecc_shared_secret(userA, userB, sharedA, &x);
  21285. } while (ret == WC_PENDING_E);
  21286. if (ret != 0)
  21287. ERROR_OUT(-9921, done);
  21288. TEST_SLEEP();
  21289. y = ECC_SHARED_SIZE;
  21290. do {
  21291. #if defined(WOLFSSL_ASYNC_CRYPT)
  21292. ret = wc_AsyncWait(ret, &userB->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  21293. #endif
  21294. if (ret == 0)
  21295. ret = wc_ecc_shared_secret(userB, userA, sharedB, &y);
  21296. } while (ret == WC_PENDING_E);
  21297. if (ret != 0)
  21298. ERROR_OUT(-9922, done);
  21299. if (y != x)
  21300. ERROR_OUT(-9923, done);
  21301. if (XMEMCMP(sharedA, sharedB, x))
  21302. ERROR_OUT(-9924, done);
  21303. TEST_SLEEP();
  21304. /* remove cofactor flag */
  21305. wc_ecc_set_flags(userA, 0);
  21306. wc_ecc_set_flags(userB, 0);
  21307. #endif /* HAVE_ECC_CDH */
  21308. #endif /* !WOLFSSL_ATECC508A && WOLFSSL_ATECC608A */
  21309. #ifdef HAVE_ECC_KEY_EXPORT
  21310. x = ECC_KEY_EXPORT_BUF_SIZE;
  21311. ret = wc_ecc_export_x963_ex(userA, exportBuf, &x, 0);
  21312. if (ret != 0)
  21313. ERROR_OUT(-9925, done);
  21314. #ifdef HAVE_ECC_KEY_IMPORT
  21315. #ifdef WOLFSSL_CUSTOM_CURVES
  21316. if (dp != NULL) {
  21317. ret = wc_ecc_set_custom_curve(pubKey, dp);
  21318. if (ret != 0)
  21319. ERROR_OUT(-9926, done);
  21320. }
  21321. #endif
  21322. ret = wc_ecc_import_x963_ex(exportBuf, x, pubKey, curve_id);
  21323. if (ret != 0)
  21324. ERROR_OUT(-9927, done);
  21325. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A)
  21326. #ifdef HAVE_ECC_DHE
  21327. y = ECC_SHARED_SIZE;
  21328. do {
  21329. #if defined(WOLFSSL_ASYNC_CRYPT)
  21330. ret = wc_AsyncWait(ret, &userB->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  21331. #endif
  21332. if (ret == 0)
  21333. ret = wc_ecc_shared_secret(userB, pubKey, sharedB, &y);
  21334. } while (ret == WC_PENDING_E);
  21335. if (ret != 0)
  21336. ERROR_OUT(-9928, done);
  21337. if (XMEMCMP(sharedA, sharedB, y))
  21338. ERROR_OUT(-9929, done);
  21339. TEST_SLEEP();
  21340. #endif /* HAVE_ECC_DHE */
  21341. #ifdef HAVE_COMP_KEY
  21342. /* try compressed export / import too */
  21343. x = ECC_KEY_EXPORT_BUF_SIZE;
  21344. ret = wc_ecc_export_x963_ex(userA, exportBuf, &x, 1);
  21345. if (ret != 0)
  21346. ERROR_OUT(-9930, done);
  21347. wc_ecc_free(pubKey);
  21348. ret = wc_ecc_init_ex(pubKey, HEAP_HINT, devId);
  21349. if (ret != 0)
  21350. ERROR_OUT(-9931, done);
  21351. #ifdef WOLFSSL_CUSTOM_CURVES
  21352. if (dp != NULL) {
  21353. ret = wc_ecc_set_custom_curve(pubKey, dp);
  21354. if (ret != 0)
  21355. ERROR_OUT(-9932, done);
  21356. }
  21357. #endif
  21358. ret = wc_ecc_import_x963_ex(exportBuf, x, pubKey, curve_id);
  21359. if (ret != 0)
  21360. ERROR_OUT(-9933, done);
  21361. #ifdef HAVE_ECC_DHE
  21362. y = ECC_SHARED_SIZE;
  21363. do {
  21364. #if defined(WOLFSSL_ASYNC_CRYPT)
  21365. ret = wc_AsyncWait(ret, &userB->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  21366. #endif
  21367. if (ret == 0)
  21368. ret = wc_ecc_shared_secret(userB, pubKey, sharedB, &y);
  21369. } while (ret == WC_PENDING_E);
  21370. if (ret != 0)
  21371. ERROR_OUT(-9934, done);
  21372. if (XMEMCMP(sharedA, sharedB, y))
  21373. ERROR_OUT(-9935, done);
  21374. TEST_SLEEP();
  21375. #endif /* HAVE_ECC_DHE */
  21376. #endif /* HAVE_COMP_KEY */
  21377. #endif /* !WOLFSSL_ATECC508A && !WOLFSSL_ATECC608A */
  21378. #endif /* !WC_NO_RNG */
  21379. #endif /* HAVE_ECC_KEY_IMPORT */
  21380. #endif /* HAVE_ECC_KEY_EXPORT */
  21381. /* For KCAPI cannot sign using generated ECDH key */
  21382. #if !defined(ECC_TIMING_RESISTANT) || (defined(ECC_TIMING_RESISTANT) && \
  21383. !defined(WC_NO_RNG) && !defined(WOLFSSL_KCAPI_ECC))
  21384. #ifdef HAVE_ECC_SIGN
  21385. /* ECC w/out Shamir has issue with all 0 digest */
  21386. /* WC_BIGINT doesn't have 0 len well on hardware */
  21387. /* Cryptocell has issues with all 0 digest */
  21388. #if defined(ECC_SHAMIR) && !defined(WOLFSSL_ASYNC_CRYPT) && \
  21389. !defined(WOLFSSL_CRYPTOCELL)
  21390. /* test DSA sign hash with zeros */
  21391. for (i = 0; i < (int)ECC_DIGEST_SIZE; i++) {
  21392. digest[i] = 0;
  21393. }
  21394. x = ECC_SIG_SIZE;
  21395. do {
  21396. #if defined(WOLFSSL_ASYNC_CRYPT)
  21397. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  21398. #endif
  21399. if (ret == 0)
  21400. ret = wc_ecc_sign_hash(digest, ECC_DIGEST_SIZE, sig, &x, rng,
  21401. userA);
  21402. } while (ret == WC_PENDING_E);
  21403. if (ret != 0)
  21404. ERROR_OUT(-9936, done);
  21405. TEST_SLEEP();
  21406. #ifdef HAVE_ECC_VERIFY
  21407. for (i=0; i<testVerifyCount; i++) {
  21408. verify = 0;
  21409. do {
  21410. #if defined(WOLFSSL_ASYNC_CRYPT)
  21411. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  21412. #endif
  21413. if (ret == 0)
  21414. ret = wc_ecc_verify_hash(sig, x, digest, ECC_DIGEST_SIZE,
  21415. &verify, userA);
  21416. } while (ret == WC_PENDING_E);
  21417. if (ret != 0)
  21418. ERROR_OUT(-9937, done);
  21419. if (verify != 1)
  21420. ERROR_OUT(-9938, done);
  21421. TEST_SLEEP();
  21422. }
  21423. #endif /* HAVE_ECC_VERIFY */
  21424. #endif /* ECC_SHAMIR && !WOLFSSL_ASYNC_CRYPT && !WOLFSSL_CRYPTOCELL */
  21425. /* test DSA sign hash with sequence (0,1,2,3,4,...) */
  21426. for (i = 0; i < (int)ECC_DIGEST_SIZE; i++) {
  21427. digest[i] = (byte)i;
  21428. }
  21429. x = ECC_SIG_SIZE;
  21430. do {
  21431. #if defined(WOLFSSL_ASYNC_CRYPT)
  21432. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  21433. #endif
  21434. if (ret == 0)
  21435. ret = wc_ecc_sign_hash(digest, ECC_DIGEST_SIZE, sig, &x, rng, userA);
  21436. } while (ret == WC_PENDING_E);
  21437. if (ret != 0)
  21438. ERROR_OUT(-9939, done);
  21439. TEST_SLEEP();
  21440. #ifdef HAVE_ECC_VERIFY
  21441. for (i=0; i<testVerifyCount; i++) {
  21442. verify = 0;
  21443. do {
  21444. #if defined(WOLFSSL_ASYNC_CRYPT)
  21445. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  21446. #endif
  21447. if (ret == 0)
  21448. ret = wc_ecc_verify_hash(sig, x, digest, ECC_DIGEST_SIZE, &verify, userA);
  21449. } while (ret == WC_PENDING_E);
  21450. if (ret != 0)
  21451. ERROR_OUT(-9940, done);
  21452. if (verify != 1)
  21453. ERROR_OUT(-9941, done);
  21454. TEST_SLEEP();
  21455. }
  21456. #endif /* HAVE_ECC_VERIFY */
  21457. #endif /* HAVE_ECC_SIGN */
  21458. #endif /* !ECC_TIMING_RESISTANT || (ECC_TIMING_RESISTANT &&
  21459. * !WC_NO_RNG && !WOLFSSL_KCAPI_ECC) */
  21460. #if defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG) && \
  21461. !defined(WOLFSSL_ATECC508) && !defined(WOLFSSL_ATECC608A) && \
  21462. !defined(WOLFSSL_KCAPI_ECC)
  21463. x = ECC_KEY_EXPORT_BUF_SIZE;
  21464. ret = wc_ecc_export_private_only(userA, exportBuf, &x);
  21465. if (ret != 0)
  21466. ERROR_OUT(-9942, done);
  21467. #elif defined(HAVE_ECC_KEY_EXPORT)
  21468. (void)exportBuf;
  21469. #endif /* HAVE_ECC_KEY_EXPORT */
  21470. done:
  21471. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21472. if (userA != NULL) {
  21473. wc_ecc_free(userA);
  21474. XFREE(userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21475. }
  21476. if (userB != NULL) {
  21477. wc_ecc_free(userB);
  21478. XFREE(userB, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21479. }
  21480. if (pubKey != NULL) {
  21481. wc_ecc_free(pubKey);
  21482. XFREE(pubKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21483. }
  21484. #else
  21485. wc_ecc_free(pubKey);
  21486. wc_ecc_free(userB);
  21487. wc_ecc_free(userA);
  21488. #endif
  21489. #if defined(HAVE_ECC_DHE) || defined(HAVE_ECC_CDH)
  21490. WC_FREE_VAR(sharedA, HEAP_HINT);
  21491. WC_FREE_VAR(sharedB, HEAP_HINT);
  21492. #endif
  21493. #ifdef HAVE_ECC_KEY_EXPORT
  21494. WC_FREE_VAR(exportBuf, HEAP_HINT);
  21495. #endif
  21496. #ifdef HAVE_ECC_SIGN
  21497. WC_FREE_VAR(sig, HEAP_HINT);
  21498. WC_FREE_VAR(digest, HEAP_HINT);
  21499. #endif
  21500. (void)keySize;
  21501. (void)curve_id;
  21502. (void)rng;
  21503. return ret;
  21504. }
  21505. #undef ECC_TEST_VERIFY_COUNT
  21506. #define ECC_TEST_VERIFY_COUNT 2
  21507. static int ecc_test_curve(WC_RNG* rng, int keySize)
  21508. {
  21509. int ret;
  21510. ret = ecc_test_curve_size(rng, keySize, ECC_TEST_VERIFY_COUNT,
  21511. ECC_CURVE_DEF, NULL);
  21512. if (ret < 0) {
  21513. if (ret == ECC_CURVE_OID_E) {
  21514. /* ignore error for curves not found */
  21515. /* some curve sizes are only available with:
  21516. HAVE_ECC_SECPR2, HAVE_ECC_SECPR3, HAVE_ECC_BRAINPOOL
  21517. and HAVE_ECC_KOBLITZ */
  21518. }
  21519. else {
  21520. printf("ecc_test_curve_size %d failed!: %d\n", keySize, ret);
  21521. return ret;
  21522. }
  21523. }
  21524. #ifndef WOLF_CRYPTO_CB_ONLY_ECC
  21525. #ifdef HAVE_ECC_VECTOR_TEST
  21526. ret = ecc_test_vector(keySize);
  21527. if (ret < 0) {
  21528. printf("ecc_test_vector %d failed!: %d\n", keySize, ret);
  21529. return ret;
  21530. }
  21531. #endif
  21532. #if defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT) && \
  21533. !defined(NO_ASN_CRYPT) && !defined(WC_NO_RNG)
  21534. ret = ecc_test_key_decode(rng, keySize);
  21535. if (ret < 0) {
  21536. if (ret == ECC_CURVE_OID_E) {
  21537. /* ignore error for curves not found */
  21538. }
  21539. else {
  21540. printf("ecc_test_key_decode %d failed!: %d\n", keySize, ret);
  21541. return ret;
  21542. }
  21543. }
  21544. #endif
  21545. #if defined(HAVE_ECC_KEY_EXPORT) && !defined(NO_ASN_CRYPT) && !defined(WC_NO_RNG)
  21546. ret = ecc_test_key_gen(rng, keySize);
  21547. if (ret < 0) {
  21548. if (ret == ECC_CURVE_OID_E) {
  21549. /* ignore error for curves not found */
  21550. }
  21551. else {
  21552. printf("ecc_test_key_gen %d failed!: %d\n", keySize, ret);
  21553. return ret;
  21554. }
  21555. }
  21556. #endif
  21557. #endif /* WOLF_CRYPTO_CB_ONLY_ECC */
  21558. return 0;
  21559. }
  21560. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  21561. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  21562. defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT) && \
  21563. !defined(WOLFSSL_NO_MALLOC) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  21564. static int ecc_point_test(void)
  21565. {
  21566. int ret;
  21567. ecc_point* point;
  21568. ecc_point* point2;
  21569. #ifdef HAVE_COMP_KEY
  21570. ecc_point* point3;
  21571. ecc_point* point4;
  21572. #endif
  21573. word32 outLen;
  21574. byte out[65];
  21575. byte der[] = { 0x04, /* = Uncompressed */
  21576. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  21577. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  21578. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  21579. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  21580. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  21581. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  21582. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  21583. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  21584. #if defined(HAVE_COMP_KEY) && (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) || \
  21585. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  21586. byte derComp0[] = { 0x02, /* = Compressed, y even */
  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. byte derComp1[] = { 0x03, /* = Compressed, y odd */
  21592. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  21593. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  21594. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  21595. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  21596. #endif
  21597. byte altDer[] = { 0x04, /* = Uncompressed */
  21598. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  21599. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  21600. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  21601. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  21602. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  21603. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  21604. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  21605. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
  21606. int curve_idx = wc_ecc_get_curve_idx(ECC_SECP256R1);
  21607. /* if curve P256 is not enabled then test should not fail */
  21608. if (curve_idx == ECC_CURVE_INVALID)
  21609. return 0;
  21610. outLen = sizeof(out);
  21611. point = wc_ecc_new_point();
  21612. if (point == NULL)
  21613. return -10000;
  21614. point2 = wc_ecc_new_point();
  21615. if (point2 == NULL) {
  21616. wc_ecc_del_point(point);
  21617. return -10001;
  21618. }
  21619. #ifdef HAVE_COMP_KEY
  21620. point3 = wc_ecc_new_point();
  21621. if (point3 == NULL) {
  21622. wc_ecc_del_point(point2);
  21623. wc_ecc_del_point(point);
  21624. return -10002;
  21625. }
  21626. point4 = wc_ecc_new_point();
  21627. if (point4 == NULL) {
  21628. wc_ecc_del_point(point3);
  21629. wc_ecc_del_point(point2);
  21630. wc_ecc_del_point(point);
  21631. return -10003;
  21632. }
  21633. #endif
  21634. /* Parameter Validation testing. */
  21635. wc_ecc_del_point(NULL);
  21636. ret = wc_ecc_import_point_der(NULL, sizeof(der), curve_idx, point);
  21637. if (ret != ECC_BAD_ARG_E) {
  21638. ret = -10004;
  21639. goto done;
  21640. }
  21641. ret = wc_ecc_import_point_der(der, sizeof(der), ECC_CURVE_INVALID, point);
  21642. if (ret != ECC_BAD_ARG_E) {
  21643. ret = -10005;
  21644. goto done;
  21645. }
  21646. ret = wc_ecc_import_point_der(der, sizeof(der), curve_idx, NULL);
  21647. if (ret != ECC_BAD_ARG_E) {
  21648. ret = -10006;
  21649. goto done;
  21650. }
  21651. ret = wc_ecc_export_point_der(-1, point, out, &outLen);
  21652. if (ret != ECC_BAD_ARG_E) {
  21653. ret = -10007;
  21654. goto done;
  21655. }
  21656. ret = wc_ecc_export_point_der(curve_idx, NULL, out, &outLen);
  21657. if (ret != ECC_BAD_ARG_E) {
  21658. ret = -10008;
  21659. goto done;
  21660. }
  21661. ret = wc_ecc_export_point_der(curve_idx, point, NULL, &outLen);
  21662. if (ret != LENGTH_ONLY_E || outLen != sizeof(out)) {
  21663. ret = -10009;
  21664. goto done;
  21665. }
  21666. ret = wc_ecc_export_point_der(curve_idx, point, out, NULL);
  21667. if (ret != ECC_BAD_ARG_E) {
  21668. ret = -10010;
  21669. goto done;
  21670. }
  21671. outLen = 0;
  21672. ret = wc_ecc_export_point_der(curve_idx, point, out, &outLen);
  21673. if (ret != BUFFER_E) {
  21674. ret = -10011;
  21675. goto done;
  21676. }
  21677. ret = wc_ecc_copy_point(NULL, NULL);
  21678. if (ret != ECC_BAD_ARG_E) {
  21679. ret = -10012;
  21680. goto done;
  21681. }
  21682. ret = wc_ecc_copy_point(NULL, point2);
  21683. if (ret != ECC_BAD_ARG_E) {
  21684. ret = -10013;
  21685. goto done;
  21686. }
  21687. ret = wc_ecc_copy_point(point, NULL);
  21688. if (ret != ECC_BAD_ARG_E) {
  21689. ret = -10014;
  21690. goto done;
  21691. }
  21692. ret = wc_ecc_cmp_point(NULL, NULL);
  21693. if (ret != BAD_FUNC_ARG) {
  21694. ret = -10015;
  21695. goto done;
  21696. }
  21697. ret = wc_ecc_cmp_point(NULL, point2);
  21698. if (ret != BAD_FUNC_ARG) {
  21699. ret = -10016;
  21700. goto done;
  21701. }
  21702. ret = wc_ecc_cmp_point(point, NULL);
  21703. if (ret != BAD_FUNC_ARG) {
  21704. ret = -10017;
  21705. goto done;
  21706. }
  21707. /* Use API. */
  21708. ret = wc_ecc_import_point_der(der, sizeof(der), curve_idx, point);
  21709. if (ret != 0) {
  21710. ret = -10018;
  21711. goto done;
  21712. }
  21713. outLen = sizeof(out);
  21714. ret = wc_ecc_export_point_der(curve_idx, point, out, &outLen);
  21715. if (ret != 0) {
  21716. ret = -10019;
  21717. goto done;
  21718. }
  21719. if (outLen != sizeof(der)) {
  21720. ret = -10020;
  21721. goto done;
  21722. }
  21723. if (XMEMCMP(out, der, outLen) != 0) {
  21724. ret = -10021;
  21725. goto done;
  21726. }
  21727. ret = wc_ecc_copy_point(point2, point);
  21728. if (ret != MP_OKAY) {
  21729. ret = -10022;
  21730. goto done;
  21731. }
  21732. ret = wc_ecc_cmp_point(point2, point);
  21733. if (ret != MP_EQ) {
  21734. ret = -10023;
  21735. goto done;
  21736. }
  21737. ret = wc_ecc_import_point_der(altDer, sizeof(altDer), curve_idx, point2);
  21738. if (ret != 0) {
  21739. ret = -10024;
  21740. goto done;
  21741. }
  21742. ret = wc_ecc_cmp_point(point2, point);
  21743. if (ret != MP_GT) {
  21744. ret = -10025;
  21745. goto done;
  21746. }
  21747. #if defined(HAVE_COMP_KEY) && (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) || \
  21748. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  21749. ret = wc_ecc_import_point_der(derComp0, sizeof(derComp0)*2-1, curve_idx, point3);
  21750. if (ret != 0) {
  21751. ret = -10026;
  21752. goto done;
  21753. }
  21754. ret = wc_ecc_import_point_der_ex(derComp0, sizeof(derComp0), curve_idx, point4, 0);
  21755. if (ret != 0) {
  21756. ret = -10027;
  21757. goto done;
  21758. }
  21759. ret = wc_ecc_cmp_point(point3, point4);
  21760. if (ret != MP_EQ) {
  21761. ret = -10028;
  21762. goto done;
  21763. }
  21764. ret = wc_ecc_import_point_der(derComp1, sizeof(derComp1)*2-1, curve_idx, point3);
  21765. if (ret != 0) {
  21766. ret = -10029;
  21767. goto done;
  21768. }
  21769. ret = wc_ecc_import_point_der_ex(derComp1, sizeof(derComp1), curve_idx, point4, 0);
  21770. if (ret != 0) {
  21771. ret = -10030;
  21772. goto done;
  21773. }
  21774. ret = wc_ecc_cmp_point(point3, point4);
  21775. if (ret != MP_EQ) {
  21776. ret = -10031;
  21777. goto done;
  21778. }
  21779. #endif
  21780. done:
  21781. #ifdef HAVE_COMP_KEY
  21782. wc_ecc_del_point(point4);
  21783. wc_ecc_del_point(point3);
  21784. #endif
  21785. wc_ecc_del_point(point2);
  21786. wc_ecc_del_point(point);
  21787. return ret;
  21788. }
  21789. #endif /* !WOLFSSL_ATECC508A && HAVE_ECC_KEY_IMPORT && HAVE_ECC_KEY_EXPORT */
  21790. #if !defined(NO_SIG_WRAPPER) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  21791. static int ecc_sig_test(WC_RNG* rng, ecc_key* key)
  21792. {
  21793. int ret;
  21794. word32 sigSz;
  21795. int size;
  21796. byte out[ECC_MAX_SIG_SIZE];
  21797. byte in[] = TEST_STRING;
  21798. WOLFSSL_SMALL_STACK_STATIC const byte hash[] = {
  21799. 0xf2, 0x02, 0x95, 0x65, 0xcb, 0xf6, 0x2a, 0x59,
  21800. 0x39, 0x2c, 0x05, 0xff, 0x0e, 0x29, 0xaf, 0xfe,
  21801. 0x47, 0x33, 0x8c, 0x99, 0x8d, 0x58, 0x64, 0x83,
  21802. 0xa6, 0x58, 0x0a, 0x33, 0x0b, 0x84, 0x5f, 0x5f
  21803. };
  21804. word32 inLen = (word32)XSTRLEN((char*)in);
  21805. size = wc_ecc_sig_size(key);
  21806. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_ECC, key, sizeof(*key));
  21807. if (ret != size)
  21808. return -10040;
  21809. sigSz = (word32)ret;
  21810. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_ECC, in,
  21811. inLen, out, &sigSz, key, sizeof(*key), rng);
  21812. if (ret != 0)
  21813. return -10041;
  21814. TEST_SLEEP();
  21815. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_ECC, in,
  21816. inLen, out, sigSz, key, sizeof(*key));
  21817. if (ret != 0)
  21818. return -10042;
  21819. TEST_SLEEP();
  21820. sigSz = (word32)sizeof(out);
  21821. ret = wc_SignatureGenerateHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_ECC,
  21822. hash, (int)sizeof(hash), out, &sigSz, key, sizeof(*key), rng);
  21823. if (ret != 0)
  21824. return -10043;
  21825. TEST_SLEEP();
  21826. ret = wc_SignatureVerifyHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_ECC,
  21827. hash, (int)sizeof(hash), out, sigSz, key, sizeof(*key));
  21828. if (ret != 0)
  21829. return -10044;
  21830. TEST_SLEEP();
  21831. return 0;
  21832. }
  21833. #endif
  21834. #if defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT) && \
  21835. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  21836. static int ecc_exp_imp_test(ecc_key* key)
  21837. {
  21838. int ret;
  21839. int curve_id;
  21840. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21841. ecc_key *keyImp = (ecc_key *)XMALLOC(sizeof *keyImp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21842. #else
  21843. ecc_key keyImp[1];
  21844. #endif
  21845. byte priv[32];
  21846. word32 privLen;
  21847. byte pub[65*2];
  21848. word32 pubLen, pubLenX, pubLenY;
  21849. const char qx[] = "7a4e287890a1a47ad3457e52f2f76a83"
  21850. "ce46cbc947616d0cbaa82323818a793d";
  21851. const char qy[] = "eec4084f5b29ebf29c44cce3b3059610"
  21852. "922f8b30ea6e8811742ac7238fe87308";
  21853. const char d[] = "8c14b793cb19137e323a6d2e2a870bca"
  21854. "2e7a493ec1153b3a95feb8a4873f8d08";
  21855. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21856. if (keyImp == NULL)
  21857. ERROR_OUT(-10050, done);
  21858. #endif
  21859. wc_ecc_init_ex(keyImp, HEAP_HINT, devId);
  21860. privLen = sizeof(priv);
  21861. ret = wc_ecc_export_private_only(key, priv, &privLen);
  21862. if (ret != 0) {
  21863. ret = -10051;
  21864. goto done;
  21865. }
  21866. pubLen = sizeof(pub);
  21867. ret = wc_ecc_export_point_der(key->idx, &key->pubkey, pub, &pubLen);
  21868. if (ret != 0) {
  21869. ret = -10052;
  21870. goto done;
  21871. }
  21872. ret = wc_ecc_import_private_key(priv, privLen, pub, pubLen, keyImp);
  21873. if (ret != 0) {
  21874. ret = -10053;
  21875. goto done;
  21876. }
  21877. wc_ecc_free(keyImp);
  21878. wc_ecc_init_ex(keyImp, HEAP_HINT, devId);
  21879. ret = wc_ecc_import_raw_ex(keyImp, qx, qy, d, ECC_SECP256R1);
  21880. if (ret != 0) {
  21881. ret = -10054;
  21882. goto done;
  21883. }
  21884. wc_ecc_free(keyImp);
  21885. wc_ecc_init_ex(keyImp, HEAP_HINT, devId);
  21886. curve_id = wc_ecc_get_curve_id(key->idx);
  21887. if (curve_id < 0) {
  21888. ret = -10055;
  21889. goto done;
  21890. }
  21891. /* test import private only */
  21892. ret = wc_ecc_import_private_key_ex(priv, privLen, NULL, 0, keyImp,
  21893. curve_id);
  21894. if (ret != 0) {
  21895. ret = -10056;
  21896. goto done;
  21897. }
  21898. wc_ecc_free(keyImp);
  21899. wc_ecc_init_ex(keyImp, HEAP_HINT, devId);
  21900. /* test export public raw */
  21901. pubLenX = pubLenY = 32;
  21902. ret = wc_ecc_export_public_raw(key, pub, &pubLenX, &pub[32], &pubLenY);
  21903. if (ret != 0) {
  21904. ret = -10057;
  21905. goto done;
  21906. }
  21907. #ifndef HAVE_SELFTEST
  21908. /* test import of public */
  21909. ret = wc_ecc_import_unsigned(keyImp, pub, &pub[32], NULL, ECC_SECP256R1);
  21910. if (ret != 0) {
  21911. ret = -10058;
  21912. goto done;
  21913. }
  21914. #endif
  21915. wc_ecc_free(keyImp);
  21916. wc_ecc_init_ex(keyImp, HEAP_HINT, devId);
  21917. /* test export private and public raw */
  21918. pubLenX = pubLenY = privLen = 32;
  21919. ret = wc_ecc_export_private_raw(key, pub, &pubLenX, &pub[32], &pubLenY,
  21920. priv, &privLen);
  21921. if (ret != 0) {
  21922. ret = -10059;
  21923. goto done;
  21924. }
  21925. #ifndef HAVE_SELFTEST
  21926. /* test import of private and public */
  21927. ret = wc_ecc_import_unsigned(keyImp, pub, &pub[32], priv, ECC_SECP256R1);
  21928. if (ret != 0) {
  21929. ret = -10060;
  21930. goto done;
  21931. }
  21932. #endif
  21933. done:
  21934. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21935. if (keyImp != NULL) {
  21936. wc_ecc_free(keyImp);
  21937. XFREE(keyImp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21938. }
  21939. #else
  21940. wc_ecc_free(keyImp);
  21941. #endif
  21942. return ret;
  21943. }
  21944. #endif /* HAVE_ECC_KEY_IMPORT && HAVE_ECC_KEY_EXPORT */
  21945. #if defined(HAVE_ECC_KEY_IMPORT) && !defined(WOLFSSL_VALIDATE_ECC_IMPORT) && \
  21946. !defined(WOLFSSL_CRYPTOCELL) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  21947. static int ecc_mulmod_test(ecc_key* key1)
  21948. {
  21949. int ret;
  21950. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21951. ecc_key *key2 = (ecc_key *)XMALLOC(sizeof *key2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21952. ecc_key *key3 = (ecc_key *)XMALLOC(sizeof *key3, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21953. #else
  21954. ecc_key key2[1];
  21955. ecc_key key3[1];
  21956. #endif
  21957. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21958. if ((key2 == NULL) || (key3 == NULL))
  21959. ERROR_OUT(MEMORY_E, done);
  21960. #endif
  21961. wc_ecc_init_ex(key2, HEAP_HINT, devId);
  21962. wc_ecc_init_ex(key3, HEAP_HINT, devId);
  21963. /* TODO: Use test data, test with WOLFSSL_VALIDATE_ECC_IMPORT. */
  21964. /* Need base point (Gx,Gy) and parameter A - load them as the public and
  21965. * private key in key2.
  21966. */
  21967. ret = wc_ecc_import_raw_ex(key2, key1->dp->Gx, key1->dp->Gy, key1->dp->Af,
  21968. ECC_SECP256R1);
  21969. if (ret != 0)
  21970. goto done;
  21971. /* Need a point (Gx,Gy) and prime - load them as the public and private key
  21972. * in key3.
  21973. */
  21974. ret = wc_ecc_import_raw_ex(key3, key1->dp->Gx, key1->dp->Gy,
  21975. key1->dp->prime, ECC_SECP256R1);
  21976. if (ret != 0)
  21977. goto done;
  21978. ret = wc_ecc_mulmod(&key1->k, &key2->pubkey, &key3->pubkey, &key2->k, &key3->k,
  21979. 1);
  21980. if (ret != 0) {
  21981. ret = -10070;
  21982. goto done;
  21983. }
  21984. done:
  21985. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21986. if (key2 != NULL) {
  21987. wc_ecc_free(key2);
  21988. XFREE(key2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21989. }
  21990. if (key3 != NULL) {
  21991. wc_ecc_free(key3);
  21992. XFREE(key3, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21993. }
  21994. #else
  21995. wc_ecc_free(key3);
  21996. wc_ecc_free(key2);
  21997. #endif
  21998. return ret;
  21999. }
  22000. #endif
  22001. #if defined(HAVE_ECC_DHE) && !defined(WC_NO_RNG) && \
  22002. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  22003. static int ecc_ssh_test(ecc_key* key, WC_RNG* rng)
  22004. {
  22005. int ret;
  22006. byte out[128];
  22007. word32 outLen = sizeof(out);
  22008. /* Parameter Validation testing. */
  22009. ret = wc_ecc_shared_secret_ssh(NULL, &key->pubkey, out, &outLen);
  22010. if (ret != BAD_FUNC_ARG)
  22011. return -10080;
  22012. ret = wc_ecc_shared_secret_ssh(key, NULL, out, &outLen);
  22013. if (ret != BAD_FUNC_ARG)
  22014. return -10081;
  22015. ret = wc_ecc_shared_secret_ssh(key, &key->pubkey, NULL, &outLen);
  22016. if (ret != BAD_FUNC_ARG)
  22017. return -10082;
  22018. ret = wc_ecc_shared_secret_ssh(key, &key->pubkey, out, NULL);
  22019. if (ret != BAD_FUNC_ARG)
  22020. return -10083;
  22021. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  22022. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  22023. !defined(HAVE_SELFTEST)
  22024. ret = wc_ecc_set_rng(key, rng);
  22025. if (ret != 0)
  22026. return -10084;
  22027. #else
  22028. (void)rng;
  22029. #endif
  22030. /* Use API. */
  22031. ret = 0;
  22032. do {
  22033. #if defined(WOLFSSL_ASYNC_CRYPT)
  22034. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  22035. #endif
  22036. if (ret == 0)
  22037. ret = wc_ecc_shared_secret_ssh(key, &key->pubkey, out, &outLen);
  22038. } while (ret == WC_PENDING_E);
  22039. if (ret != 0)
  22040. return -10085;
  22041. TEST_SLEEP();
  22042. return 0;
  22043. }
  22044. #endif /* HAVE_ECC_DHE && !WC_NO_RNG */
  22045. static int ecc_def_curve_test(WC_RNG *rng)
  22046. {
  22047. int ret;
  22048. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22049. ecc_key *key = (ecc_key *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22050. #else
  22051. ecc_key key[1];
  22052. #endif
  22053. #if (defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT)) || \
  22054. (defined(HAVE_ECC_KEY_IMPORT) && !defined(WOLFSSL_VALIDATE_ECC_IMPORT))
  22055. word32 idx = 0;
  22056. #endif
  22057. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22058. if (key == NULL)
  22059. ERROR_OUT(MEMORY_E, done);
  22060. #endif
  22061. wc_ecc_init_ex(key, HEAP_HINT, devId);
  22062. /* Use API */
  22063. ret = wc_ecc_set_flags(NULL, 0);
  22064. if (ret != BAD_FUNC_ARG) {
  22065. ret = -10090;
  22066. goto done;
  22067. }
  22068. ret = wc_ecc_set_flags(key, 0);
  22069. if (ret != 0) {
  22070. ret = -10091;
  22071. goto done;
  22072. }
  22073. #ifndef WOLF_CRYPTO_CB_ONLY_ECC
  22074. #ifndef WC_NO_RNG
  22075. ret = wc_ecc_make_key(rng, ECC_KEYGEN_SIZE, key);
  22076. #if defined(WOLFSSL_ASYNC_CRYPT)
  22077. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  22078. #endif
  22079. if (ret != 0) {
  22080. goto done;
  22081. }
  22082. #ifndef NO_SIG_WRAPPER
  22083. ret = ecc_sig_test(rng, key);
  22084. if (ret < 0)
  22085. goto done;
  22086. #endif
  22087. TEST_SLEEP();
  22088. #if defined(HAVE_ECC_DHE) && !defined(WOLFSSL_CRYPTOCELL) && \
  22089. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  22090. ret = ecc_ssh_test(key, rng);
  22091. if (ret < 0)
  22092. goto done;
  22093. #endif
  22094. wc_ecc_free(key);
  22095. #else
  22096. (void)rng;
  22097. #endif /* !WC_NO_RNG */
  22098. #if (defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT)) || \
  22099. (defined(HAVE_ECC_KEY_IMPORT) && !defined(WOLFSSL_VALIDATE_ECC_IMPORT))
  22100. /* Use test ECC key - ensure real private "d" exists */
  22101. #ifdef USE_CERT_BUFFERS_256
  22102. ret = wc_EccPrivateKeyDecode(ecc_key_der_256, &idx, key,
  22103. sizeof_ecc_key_der_256);
  22104. #else
  22105. {
  22106. XFILE file = XFOPEN(eccKeyDerFile, "rb");
  22107. byte der[128];
  22108. word32 derSz;
  22109. if (!file) {
  22110. ERROR_OUT(-10093, done);
  22111. }
  22112. derSz = (word32)XFREAD(der, 1, sizeof(der), file);
  22113. XFCLOSE(file);
  22114. ret = wc_EccPrivateKeyDecode(der, &idx, key, derSz);
  22115. }
  22116. #endif
  22117. if (ret != 0) {
  22118. goto done;
  22119. }
  22120. #if defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT)
  22121. ret = ecc_exp_imp_test(key);
  22122. if (ret < 0)
  22123. goto done;
  22124. #endif
  22125. #if defined(HAVE_ECC_KEY_IMPORT) && !defined(WOLFSSL_VALIDATE_ECC_IMPORT) && \
  22126. !defined(WOLFSSL_CRYPTOCELL)
  22127. ret = ecc_mulmod_test(key);
  22128. if (ret < 0)
  22129. goto done;
  22130. #endif
  22131. #endif
  22132. #else
  22133. (void)rng;
  22134. (void)idx;
  22135. #endif /* WOLF_CRYPTO_CB_ONLY_ECC */
  22136. done:
  22137. wc_ecc_free(key);
  22138. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22139. if (key != NULL) {
  22140. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22141. }
  22142. #endif
  22143. return ret;
  22144. }
  22145. #endif /* !NO_ECC256 || HAVE_ALL_CURVES */
  22146. #if defined(WOLFSSL_CERT_EXT) && \
  22147. (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  22148. static int ecc_decode_test(void)
  22149. {
  22150. int ret;
  22151. word32 inSz;
  22152. word32 inOutIdx;
  22153. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22154. ecc_key *key = (ecc_key *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22155. #else
  22156. ecc_key key[1];
  22157. #endif
  22158. /* SECP256R1 OID: 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07 */
  22159. /* This is ecc_clikeypub_der_256. */
  22160. WOLFSSL_SMALL_STACK_STATIC const byte good[] = {
  22161. 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce,
  22162. 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d,
  22163. 0x03, 0x01, 0x07, 0x03, 0x42, 0x00, 0x04, 0x55, 0xbf, 0xf4,
  22164. 0x0f, 0x44, 0x50, 0x9a, 0x3d, 0xce, 0x9b, 0xb7, 0xf0, 0xc5,
  22165. 0x4d, 0xf5, 0x70, 0x7b, 0xd4, 0xec, 0x24, 0x8e, 0x19, 0x80,
  22166. 0xec, 0x5a, 0x4c, 0xa2, 0x24, 0x03, 0x62, 0x2c, 0x9b, 0xda,
  22167. 0xef, 0xa2, 0x35, 0x12, 0x43, 0x84, 0x76, 0x16, 0xc6, 0x56,
  22168. 0x95, 0x06, 0xcc, 0x01, 0xa9, 0xbd, 0xf6, 0x75, 0x1a, 0x42,
  22169. 0xf7, 0xbd, 0xa9, 0xb2, 0x36, 0x22, 0x5f, 0xc7, 0x5d, 0x7f,
  22170. 0xb4 };
  22171. WOLFSSL_SMALL_STACK_STATIC const byte badNoObjId[] = { 0x30, 0x08, 0x30, 0x06, 0x03, 0x04,
  22172. 0x00, 0x04, 0x01, 0x01 };
  22173. WOLFSSL_SMALL_STACK_STATIC const byte badOneObjId[] = { 0x30, 0x0a, 0x30, 0x08, 0x06, 0x00,
  22174. 0x03, 0x04, 0x00, 0x04, 0x01, 0x01 };
  22175. WOLFSSL_SMALL_STACK_STATIC const byte badObjId1Len[] = { 0x30, 0x0c, 0x30, 0x0a, 0x06, 0x09,
  22176. 0x06, 0x00, 0x03, 0x04, 0x00, 0x04, 0x01, 0x01 };
  22177. WOLFSSL_SMALL_STACK_STATIC const byte badObj2d1Len[] = { 0x30, 0x0c, 0x30, 0x0a, 0x06, 0x00,
  22178. 0x06, 0x07, 0x03, 0x04, 0x00, 0x04, 0x01, 0x01 };
  22179. WOLFSSL_SMALL_STACK_STATIC const byte badNotBitStr[] = { 0x30, 0x14, 0x30, 0x0b, 0x06, 0x00,
  22180. 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07,
  22181. 0x04, 0x04, 0x00, 0x04, 0x01, 0x01 };
  22182. WOLFSSL_SMALL_STACK_STATIC const byte badBitStrLen[] = { 0x30, 0x14, 0x30, 0x0b, 0x06, 0x00,
  22183. 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07,
  22184. 0x03, 0x05, 0x00, 0x04, 0x01, 0x01 };
  22185. WOLFSSL_SMALL_STACK_STATIC const byte badNoBitStrZero[] = { 0x30, 0x13, 0x30, 0x0a, 0x06, 0x00,
  22186. 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07,
  22187. 0x03, 0x03, 0x04, 0x01, 0x01 };
  22188. WOLFSSL_SMALL_STACK_STATIC const byte badPoint[] = { 0x30, 0x12, 0x30, 0x09, 0x06, 0x00,
  22189. 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07,
  22190. 0x03, 0x03, 0x00, 0x04, 0x01 };
  22191. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22192. if (key == NULL)
  22193. ERROR_OUT(MEMORY_E, done);
  22194. #endif
  22195. XMEMSET(key, 0, sizeof *key);
  22196. wc_ecc_init_ex(key, HEAP_HINT, devId);
  22197. inSz = sizeof(good);
  22198. ret = wc_EccPublicKeyDecode(NULL, &inOutIdx, key, inSz);
  22199. if (ret != BAD_FUNC_ARG) {
  22200. ret = -10100;
  22201. goto done;
  22202. }
  22203. ret = wc_EccPublicKeyDecode(good, NULL, key, inSz);
  22204. if (ret != BAD_FUNC_ARG) {
  22205. ret = -10101;
  22206. goto done;
  22207. }
  22208. ret = wc_EccPublicKeyDecode(good, &inOutIdx, NULL, inSz);
  22209. if (ret != BAD_FUNC_ARG) {
  22210. ret = -10102;
  22211. goto done;
  22212. }
  22213. ret = wc_EccPublicKeyDecode(good, &inOutIdx, key, 0);
  22214. if (ret != BAD_FUNC_ARG) {
  22215. ret = -10103;
  22216. goto done;
  22217. }
  22218. /* Change offset to produce bad input data. */
  22219. inOutIdx = 2;
  22220. inSz = sizeof(good) - inOutIdx;
  22221. ret = wc_EccPublicKeyDecode(good, &inOutIdx, key, inSz);
  22222. if (ret != ASN_PARSE_E) {
  22223. ret = -10104;
  22224. goto done;
  22225. }
  22226. inOutIdx = 4;
  22227. inSz = sizeof(good) - inOutIdx;
  22228. ret = wc_EccPublicKeyDecode(good, &inOutIdx, key, inSz);
  22229. if (ret != ASN_PARSE_E) {
  22230. ret = -10105;
  22231. goto done;
  22232. }
  22233. /* Bad data. */
  22234. inSz = sizeof(badNoObjId);
  22235. inOutIdx = 0;
  22236. ret = wc_EccPublicKeyDecode(badNoObjId, &inOutIdx, key, inSz);
  22237. if (ret != ASN_OBJECT_ID_E && ret != ASN_PARSE_E) {
  22238. ret = -10106;
  22239. goto done;
  22240. }
  22241. inSz = sizeof(badOneObjId);
  22242. inOutIdx = 0;
  22243. ret = wc_EccPublicKeyDecode(badOneObjId, &inOutIdx, key, inSz);
  22244. if (ret != ASN_OBJECT_ID_E && ret != ASN_PARSE_E) {
  22245. ret = -10107;
  22246. goto done;
  22247. }
  22248. inSz = sizeof(badObjId1Len);
  22249. inOutIdx = 0;
  22250. ret = wc_EccPublicKeyDecode(badObjId1Len, &inOutIdx, key, inSz);
  22251. if (ret != ASN_PARSE_E) {
  22252. ret = -10108;
  22253. goto done;
  22254. }
  22255. inSz = sizeof(badObj2d1Len);
  22256. inOutIdx = 0;
  22257. ret = wc_EccPublicKeyDecode(badObj2d1Len, &inOutIdx, key, inSz);
  22258. if (ret != ASN_PARSE_E) {
  22259. ret = -10109;
  22260. goto done;
  22261. }
  22262. inSz = sizeof(badNotBitStr);
  22263. inOutIdx = 0;
  22264. ret = wc_EccPublicKeyDecode(badNotBitStr, &inOutIdx, key, inSz);
  22265. if (ret != ASN_BITSTR_E && ret != ASN_PARSE_E) {
  22266. ret = -10110;
  22267. goto done;
  22268. }
  22269. inSz = sizeof(badBitStrLen);
  22270. inOutIdx = 0;
  22271. ret = wc_EccPublicKeyDecode(badBitStrLen, &inOutIdx, key, inSz);
  22272. if (ret != ASN_PARSE_E) {
  22273. ret = -10111;
  22274. goto done;
  22275. }
  22276. inSz = sizeof(badNoBitStrZero);
  22277. inOutIdx = 0;
  22278. ret = wc_EccPublicKeyDecode(badNoBitStrZero, &inOutIdx, key, inSz);
  22279. if (ret != ASN_EXPECT_0_E && ret != ASN_PARSE_E) {
  22280. ret = -10112;
  22281. goto done;
  22282. }
  22283. inSz = sizeof(badPoint);
  22284. inOutIdx = 0;
  22285. ret = wc_EccPublicKeyDecode(badPoint, &inOutIdx, key, inSz);
  22286. if (ret != ASN_ECC_KEY_E && ret != ASN_PARSE_E) {
  22287. ret = -10113;
  22288. goto done;
  22289. }
  22290. inSz = sizeof(good);
  22291. inOutIdx = 0;
  22292. ret = wc_EccPublicKeyDecode(good, &inOutIdx, key, inSz);
  22293. if (ret != 0) {
  22294. ret = -10114;
  22295. goto done;
  22296. }
  22297. done:
  22298. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22299. if (key != NULL) {
  22300. wc_ecc_free(key);
  22301. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22302. }
  22303. #else
  22304. wc_ecc_free(key);
  22305. #endif
  22306. return ret;
  22307. }
  22308. #endif /* WOLFSSL_CERT_EXT */
  22309. #ifdef WOLFSSL_CUSTOM_CURVES
  22310. static const byte eccKeyExplicitCurve[] = {
  22311. 0x30, 0x81, 0xf5, 0x30, 0x81, 0xae, 0x06, 0x07,
  22312. 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01, 0x30,
  22313. 0x81, 0xa2, 0x02, 0x01, 0x01, 0x30, 0x2c, 0x06,
  22314. 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x01, 0x01,
  22315. 0x02, 0x21, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff,
  22316. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  22317. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  22318. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0xff,
  22319. 0xff, 0xfc, 0x2f, 0x30, 0x06, 0x04, 0x01, 0x00,
  22320. 0x04, 0x01, 0x07, 0x04, 0x41, 0x04, 0x79, 0xbe,
  22321. 0x66, 0x7e, 0xf9, 0xdc, 0xbb, 0xac, 0x55, 0xa0,
  22322. 0x62, 0x95, 0xce, 0x87, 0x0b, 0x07, 0x02, 0x9b,
  22323. 0xfc, 0xdb, 0x2d, 0xce, 0x28, 0xd9, 0x59, 0xf2,
  22324. 0x81, 0x5b, 0x16, 0xf8, 0x17, 0x98, 0x48, 0x3a,
  22325. 0xda, 0x77, 0x26, 0xa3, 0xc4, 0x65, 0x5d, 0xa4,
  22326. 0xfb, 0xfc, 0x0e, 0x11, 0x08, 0xa8, 0xfd, 0x17,
  22327. 0xb4, 0x48, 0xa6, 0x85, 0x54, 0x19, 0x9c, 0x47,
  22328. 0xd0, 0x8f, 0xfb, 0x10, 0xd4, 0xb8, 0x02, 0x21,
  22329. 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  22330. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  22331. 0xfe, 0xba, 0xae, 0xdc, 0xe6, 0xaf, 0x48, 0xa0,
  22332. 0x3b, 0xbf, 0xd2, 0x5e, 0x8c, 0xd0, 0x36, 0x41,
  22333. 0x41, 0x02, 0x01, 0x01, 0x03, 0x42, 0x00, 0x04,
  22334. 0x3c, 0x4c, 0xc9, 0x5e, 0x2e, 0xa2, 0x3d, 0x49,
  22335. 0xcc, 0x5b, 0xff, 0x4f, 0xc9, 0x2e, 0x1d, 0x4a,
  22336. 0xc6, 0x21, 0xf6, 0xf3, 0xe6, 0x0b, 0x4f, 0xa9,
  22337. 0x9d, 0x74, 0x99, 0xdd, 0x97, 0xc7, 0x6e, 0xbe,
  22338. 0x14, 0x2b, 0x39, 0x9d, 0x63, 0xc7, 0x97, 0x0d,
  22339. 0x45, 0x25, 0x40, 0x30, 0x77, 0x05, 0x76, 0x88,
  22340. 0x38, 0x96, 0x29, 0x7d, 0x9c, 0xe1, 0x50, 0xbe,
  22341. 0xac, 0xf0, 0x1d, 0x86, 0xf4, 0x2f, 0x65, 0x0b
  22342. };
  22343. static int ecc_test_custom_curves(WC_RNG* rng)
  22344. {
  22345. int ret;
  22346. word32 inOutIdx;
  22347. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22348. ecc_key *key = (ecc_key *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22349. #else
  22350. ecc_key key[1];
  22351. #endif
  22352. /* test use of custom curve - using BRAINPOOLP256R1 for test */
  22353. #if defined(HAVE_ECC_BRAINPOOL) && !defined(HAVE_INTEL_QA)
  22354. #ifndef WOLFSSL_ECC_CURVE_STATIC
  22355. WOLFSSL_SMALL_STACK_STATIC const ecc_oid_t ecc_oid_brainpoolp256r1[] = {
  22356. 0x2B,0x24,0x03,0x03,0x02,0x08,0x01,0x01,0x07
  22357. };
  22358. #define ecc_oid_brainpoolp256r1_sz \
  22359. (sizeof(ecc_oid_brainpoolp256r1) / sizeof(ecc_oid_t))
  22360. #else
  22361. #define ecc_oid_brainpoolp256r1 { \
  22362. 0x2B,0x24,0x03,0x03,0x02,0x08,0x01,0x01,0x07 \
  22363. }
  22364. #define ecc_oid_brainpoolp256r1_sz 9
  22365. #endif
  22366. #define ecc_oid_brainpoolp256r1_sum 104
  22367. WOLFSSL_SMALL_STACK_STATIC const ecc_set_type ecc_dp_brainpool256r1 = {
  22368. 32, /* size/bytes */
  22369. ECC_CURVE_CUSTOM, /* ID */
  22370. "BRAINPOOLP256R1", /* curve name */
  22371. "A9FB57DBA1EEA9BC3E660A909D838D726E3BF623D52620282013481D1F6E5377", /* prime */
  22372. "7D5A0975FC2C3057EEF67530417AFFE7FB8055C126DC5C6CE94A4B44F330B5D9", /* A */
  22373. "26DC5C6CE94A4B44F330B5D9BBD77CBF958416295CF7E1CE6BCCDC18FF8C07B6", /* B */
  22374. "A9FB57DBA1EEA9BC3E660A909D838D718C397AA3B561A6F7901E0E82974856A7", /* order */
  22375. "8BD2AEB9CB7E57CB2C4B482FFC81B7AFB9DE27E1E3BD23C23A4453BD9ACE3262", /* Gx */
  22376. "547EF835C3DAC4FD97F8461A14611DC9C27745132DED8E545C1D54C72F046997", /* Gy */
  22377. ecc_oid_brainpoolp256r1, /* oid/oidSz */
  22378. ecc_oid_brainpoolp256r1_sz,
  22379. ecc_oid_brainpoolp256r1_sum, /* oid sum */
  22380. 1, /* cofactor */
  22381. };
  22382. #endif /* HAVE_ECC_BRAINPOOL */
  22383. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22384. if (! key) {
  22385. ret = MEMORY_E;
  22386. goto done;
  22387. }
  22388. #endif
  22389. XMEMSET(key, 0, sizeof *key);
  22390. #if defined(HAVE_ECC_BRAINPOOL) && !defined(HAVE_INTEL_QA)
  22391. ret = ecc_test_curve_size(rng, 0, ECC_TEST_VERIFY_COUNT, ECC_CURVE_DEF,
  22392. &ecc_dp_brainpool256r1);
  22393. if (ret != 0) {
  22394. printf("ECC test for custom curve failed! %d\n", ret);
  22395. goto done;
  22396. }
  22397. #endif
  22398. #if defined(HAVE_ECC_BRAINPOOL) || defined(HAVE_ECC_KOBLITZ)
  22399. {
  22400. int curve_id;
  22401. #ifdef HAVE_ECC_BRAINPOOL
  22402. curve_id = ECC_BRAINPOOLP256R1;
  22403. #else
  22404. curve_id = ECC_SECP256K1;
  22405. #endif
  22406. /* Test and demonstrate use of non-SECP curve */
  22407. ret = ecc_test_curve_size(rng, 0, ECC_TEST_VERIFY_COUNT, curve_id, NULL);
  22408. if (ret < 0) {
  22409. printf("ECC test for curve_id %d failed! %d\n", curve_id, ret);
  22410. goto done;
  22411. }
  22412. }
  22413. #endif
  22414. ret = wc_ecc_init_ex(key, HEAP_HINT, devId);
  22415. if (ret != 0) {
  22416. ret = -10120;
  22417. goto done;
  22418. }
  22419. inOutIdx = 0;
  22420. ret = wc_EccPublicKeyDecode(eccKeyExplicitCurve, &inOutIdx, key,
  22421. sizeof(eccKeyExplicitCurve));
  22422. if (ret != 0)
  22423. ret = -10121;
  22424. done:
  22425. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22426. if (key) {
  22427. wc_ecc_free(key);
  22428. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22429. }
  22430. #else
  22431. wc_ecc_free(key);
  22432. #endif
  22433. (void)rng;
  22434. return ret;
  22435. }
  22436. #endif /* WOLFSSL_CUSTOM_CURVES */
  22437. #ifdef WOLFSSL_CERT_GEN
  22438. /* Make Cert / Sign example for ECC cert and ECC CA */
  22439. static int ecc_test_cert_gen(WC_RNG* rng)
  22440. {
  22441. int ret;
  22442. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22443. Cert *myCert = (Cert *)XMALLOC(sizeof *myCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22444. #ifdef WOLFSSL_TEST_CERT
  22445. DecodedCert *decode = (DecodedCert *)XMALLOC(sizeof *decode, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22446. #endif
  22447. ecc_key *caEccKey = (ecc_key *)XMALLOC(sizeof *caEccKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22448. ecc_key *certPubKey = (ecc_key *)XMALLOC(sizeof *certPubKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22449. #else
  22450. Cert myCert[1];
  22451. #ifdef WOLFSSL_TEST_CERT
  22452. DecodedCert decode[1];
  22453. #endif
  22454. ecc_key caEccKey[1];
  22455. ecc_key certPubKey[1];
  22456. #endif
  22457. int certSz;
  22458. size_t bytes;
  22459. word32 idx = 0;
  22460. #ifndef USE_CERT_BUFFERS_256
  22461. XFILE file;
  22462. #endif
  22463. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22464. byte* der = NULL;
  22465. #else
  22466. byte der[FOURK_BUF];
  22467. #endif
  22468. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22469. if ((myCert == NULL)
  22470. #ifdef WOLFSSL_TEST_CERT
  22471. || (decode == NULL)
  22472. #endif
  22473. || (caEccKey == NULL) || (certPubKey == NULL))
  22474. ERROR_OUT(MEMORY_E, exit);
  22475. #endif
  22476. XMEMSET(caEccKey, 0, sizeof *caEccKey);
  22477. XMEMSET(certPubKey, 0, sizeof *certPubKey);
  22478. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22479. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22480. if (der == NULL) {
  22481. ERROR_OUT(-10130, exit);
  22482. }
  22483. #endif
  22484. /* Get cert private key */
  22485. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  22486. /* Get Cert Key 384 */
  22487. #ifdef USE_CERT_BUFFERS_256
  22488. XMEMCPY(der, ca_ecc_key_der_384, sizeof_ca_ecc_key_der_384);
  22489. bytes = sizeof_ca_ecc_key_der_384;
  22490. #else
  22491. file = XFOPEN(eccCaKey384File, "rb");
  22492. if (!file) {
  22493. ERROR_OUT(-10131, exit);
  22494. }
  22495. bytes = XFREAD(der, 1, FOURK_BUF, file);
  22496. XFCLOSE(file);
  22497. (void)eccCaKeyFile;
  22498. #endif /* USE_CERT_BUFFERS_256 */
  22499. #else
  22500. #ifdef USE_CERT_BUFFERS_256
  22501. XMEMCPY(der, ca_ecc_key_der_256, sizeof_ca_ecc_key_der_256);
  22502. bytes = sizeof_ca_ecc_key_der_256;
  22503. #else
  22504. file = XFOPEN(eccCaKeyFile, "rb");
  22505. if (!file) {
  22506. ERROR_OUT(-10132, exit);
  22507. }
  22508. bytes = XFREAD(der, 1, FOURK_BUF, file);
  22509. XFCLOSE(file);
  22510. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  22511. (void)eccCaKey384File;
  22512. #endif
  22513. #endif /* USE_CERT_BUFFERS_256 */
  22514. #endif /* ENABLE_ECC384_CERT_GEN_TEST */
  22515. /* Get CA Key */
  22516. ret = wc_ecc_init_ex(caEccKey, HEAP_HINT, devId);
  22517. if (ret != 0) {
  22518. ERROR_OUT(-10133, exit);
  22519. }
  22520. ret = wc_EccPrivateKeyDecode(der, &idx, caEccKey, (word32)bytes);
  22521. if (ret != 0) {
  22522. ERROR_OUT(-10134, exit);
  22523. }
  22524. /* Make a public key */
  22525. ret = wc_ecc_init_ex(certPubKey, HEAP_HINT, devId);
  22526. if (ret != 0) {
  22527. ERROR_OUT(-10135, exit);
  22528. }
  22529. ret = wc_ecc_make_key(rng, ECC_KEYGEN_SIZE, certPubKey);
  22530. #if defined(WOLFSSL_ASYNC_CRYPT)
  22531. ret = wc_AsyncWait(ret, &certPubKey->asyncDev, WC_ASYNC_FLAG_NONE);
  22532. #endif
  22533. if (ret != 0) {
  22534. ERROR_OUT(-10136, exit);
  22535. }
  22536. TEST_SLEEP();
  22537. /* Setup Certificate */
  22538. if (wc_InitCert_ex(myCert, HEAP_HINT, devId)) {
  22539. ERROR_OUT(-10137, exit);
  22540. }
  22541. #ifndef NO_SHA256
  22542. myCert->sigType = CTC_SHA256wECDSA;
  22543. #else
  22544. myCert->sigType = CTC_SHAwECDSA;
  22545. #endif
  22546. XMEMCPY(&myCert->subject, &certDefaultName, sizeof(CertName));
  22547. #ifdef WOLFSSL_CERT_EXT
  22548. /* add Policies */
  22549. XSTRNCPY(myCert->certPolicies[0], "2.4.589440.587.101.2.1.9632587.1",
  22550. CTC_MAX_CERTPOL_SZ);
  22551. XSTRNCPY(myCert->certPolicies[1], "1.2.13025.489.1.113549",
  22552. CTC_MAX_CERTPOL_SZ);
  22553. myCert->certPoliciesNb = 2;
  22554. /* add SKID from the Public Key */
  22555. if (wc_SetSubjectKeyIdFromPublicKey(myCert, NULL, certPubKey) != 0) {
  22556. ERROR_OUT(-10138, exit);
  22557. }
  22558. /* add AKID from the Public Key */
  22559. if (wc_SetAuthKeyIdFromPublicKey(myCert, NULL, caEccKey) != 0) {
  22560. ERROR_OUT(-10139, exit);
  22561. }
  22562. /* add Key Usage */
  22563. if (wc_SetKeyUsage(myCert, certKeyUsage) != 0) {
  22564. ERROR_OUT(-10140, exit);
  22565. }
  22566. #endif /* WOLFSSL_CERT_EXT */
  22567. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  22568. #if defined(USE_CERT_BUFFERS_256)
  22569. ret = wc_SetIssuerBuffer(myCert, ca_ecc_cert_der_384,
  22570. sizeof_ca_ecc_cert_der_384);
  22571. #else
  22572. ret = wc_SetIssuer(myCert, eccCaCert384File);
  22573. (void)eccCaCertFile;
  22574. #endif
  22575. #else
  22576. #if defined(USE_CERT_BUFFERS_256)
  22577. ret = wc_SetIssuerBuffer(myCert, ca_ecc_cert_der_256,
  22578. sizeof_ca_ecc_cert_der_256);
  22579. #else
  22580. ret = wc_SetIssuer(myCert, eccCaCertFile);
  22581. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  22582. (void)eccCaCert384File;
  22583. #endif
  22584. #endif
  22585. #endif /* ENABLE_ECC384_CERT_GEN_TEST */
  22586. if (ret < 0) {
  22587. ERROR_OUT(-10141, exit);
  22588. }
  22589. certSz = wc_MakeCert(myCert, der, FOURK_BUF, NULL, certPubKey, rng);
  22590. if (certSz < 0) {
  22591. ERROR_OUT(-10142, exit);
  22592. }
  22593. ret = 0;
  22594. do {
  22595. #if defined(WOLFSSL_ASYNC_CRYPT)
  22596. ret = wc_AsyncWait(ret, &caEccKey->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  22597. #endif
  22598. if (ret >= 0) {
  22599. ret = wc_SignCert(myCert->bodySz, myCert->sigType, der,
  22600. FOURK_BUF, NULL, caEccKey, rng);
  22601. }
  22602. } while (ret == WC_PENDING_E);
  22603. if (ret < 0) {
  22604. ERROR_OUT(-10143, exit);
  22605. }
  22606. certSz = ret;
  22607. TEST_SLEEP();
  22608. #ifdef WOLFSSL_TEST_CERT
  22609. InitDecodedCert(decode, der, certSz, HEAP_HINT);
  22610. ret = ParseCert(decode, CERT_TYPE, NO_VERIFY, 0);
  22611. if (ret != 0) {
  22612. FreeDecodedCert(decode);
  22613. ERROR_OUT(-10144, exit);
  22614. }
  22615. FreeDecodedCert(decode);
  22616. #endif
  22617. ret = SaveDerAndPem(der, certSz, certEccDerFile, certEccPemFile,
  22618. CERT_TYPE, -6735);
  22619. if (ret != 0) {
  22620. goto exit;
  22621. }
  22622. exit:
  22623. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22624. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22625. #endif
  22626. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22627. if (myCert != NULL)
  22628. XFREE(myCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22629. #ifdef WOLFSSL_TEST_CERT
  22630. if (decode != NULL)
  22631. XFREE(decode, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22632. #endif
  22633. if (caEccKey != NULL) {
  22634. wc_ecc_free(caEccKey);
  22635. XFREE(caEccKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22636. }
  22637. if (certPubKey != NULL) {
  22638. wc_ecc_free(certPubKey);
  22639. XFREE(certPubKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22640. }
  22641. #else
  22642. wc_ecc_free(certPubKey);
  22643. wc_ecc_free(caEccKey);
  22644. #endif
  22645. return ret;
  22646. }
  22647. #endif /* WOLFSSL_CERT_GEN */
  22648. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  22649. !defined(WOLFSSL_NO_MALLOC) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  22650. /* Test for the wc_ecc_key_new() and wc_ecc_key_free() functions. */
  22651. static int ecc_test_allocator(WC_RNG* rng)
  22652. {
  22653. int ret = 0;
  22654. ecc_key* key;
  22655. #ifdef WC_NO_RNG
  22656. word32 idx = 0;
  22657. #endif
  22658. key = wc_ecc_key_new(HEAP_HINT);
  22659. if (key == NULL) {
  22660. ERROR_OUT(-10150, exit);
  22661. }
  22662. #ifndef WC_NO_RNG
  22663. ret = wc_ecc_make_key(rng, ECC_KEYGEN_SIZE, key);
  22664. #if defined(WOLFSSL_ASYNC_CRYPT)
  22665. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  22666. #endif
  22667. if (ret != 0) {
  22668. ERROR_OUT(-10151, exit);
  22669. }
  22670. #else
  22671. /* use test ECC key */
  22672. ret = wc_EccPrivateKeyDecode(ecc_key_der_256, &idx, key,
  22673. (word32)sizeof_ecc_key_der_256);
  22674. (void)rng;
  22675. #endif
  22676. exit:
  22677. wc_ecc_key_free(key);
  22678. return ret;
  22679. }
  22680. #endif
  22681. /* ECC Non-blocking tests for Sign and Verify */
  22682. /* Requires SP math and supports P384 or P256 */
  22683. /* ./configure --enable-ecc=nonblock --enable-sp=yes,nonblock CFLAGS="-DWOLFSSL_PUBLIC_MP" */
  22684. #if defined(WC_ECC_NONBLOCK) && defined(WOLFSSL_HAVE_SP_ECC) && \
  22685. defined(WOLFSSL_PUBLIC_MP)
  22686. /* ECC Private Key "d" */
  22687. static const byte p256PrivKey[] = {
  22688. /* SECP256R1 */
  22689. /* d */
  22690. 0x1e, 0xe7, 0x70, 0x07, 0xd3, 0x30, 0x94, 0x39,
  22691. 0x28, 0x90, 0xdf, 0x23, 0x88, 0x2c, 0x4a, 0x34,
  22692. 0x15, 0xdb, 0x4c, 0x43, 0xcd, 0xfa, 0xe5, 0x1f,
  22693. 0x3d, 0x4c, 0x37, 0xfe, 0x59, 0x3b, 0x96, 0xd8
  22694. };
  22695. #ifdef HAVE_ECC384
  22696. static const byte p384PrivKey[] = {
  22697. /* SECP384R1 */
  22698. /* d */
  22699. 0xa4, 0xe5, 0x06, 0xe8, 0x06, 0x16, 0x3e, 0xab,
  22700. 0x89, 0xf8, 0x60, 0x43, 0xc0, 0x60, 0x25, 0xdb,
  22701. 0xba, 0x7b, 0xfe, 0x19, 0x35, 0x08, 0x55, 0x65,
  22702. 0x76, 0xe2, 0xdc, 0xe0, 0x01, 0x8b, 0x6b, 0x68,
  22703. 0xdf, 0xcf, 0x6f, 0x80, 0x12, 0xce, 0x79, 0x37,
  22704. 0xeb, 0x2b, 0x9c, 0x7b, 0xc4, 0x68, 0x1c, 0x74
  22705. };
  22706. #endif /* HAVE_ECC384 */
  22707. #ifdef HAVE_ECC521
  22708. static const byte p521PrivKey[] = {
  22709. /* SECP521R1 */
  22710. /* d */
  22711. 0x01, 0x68, 0x91, 0x33, 0x53, 0xe2, 0x90, 0x68,
  22712. 0x11, 0x8f, 0xaa, 0xa8, 0x76, 0x0c, 0xf7, 0x2a,
  22713. 0x07, 0x1b, 0x92, 0x2a, 0xa7, 0x82, 0x3d, 0xfa,
  22714. 0x83, 0xce, 0x70, 0xc8, 0xc2, 0x60, 0x82, 0xfe,
  22715. 0x18, 0x88, 0x68, 0xda, 0x6a, 0x83, 0x46, 0x78,
  22716. 0xe4, 0xe9, 0xe9, 0xcc, 0x51, 0x7f, 0xed, 0x81,
  22717. 0x02, 0x32, 0xee, 0x26, 0x87, 0xcc, 0xed, 0x63,
  22718. 0x3f, 0x39, 0x27, 0xf0, 0xd7, 0x17, 0x77, 0xa1,
  22719. 0xa4, 0x36
  22720. };
  22721. #endif /* HAVE_ECC521 */
  22722. /* ECC public key Qx/Qy */
  22723. static const byte p256PubKey[] = {
  22724. /* SECP256R1 */
  22725. /* Qx */
  22726. 0x96, 0x93, 0x1c, 0x53, 0x0b, 0x43, 0x6c, 0x42,
  22727. 0x0c, 0x52, 0x90, 0xe4, 0xa7, 0xec, 0x98, 0xb1,
  22728. 0xaf, 0xd4, 0x14, 0x49, 0xd8, 0xc1, 0x42, 0x82,
  22729. 0x04, 0x78, 0xd1, 0x90, 0xae, 0xa0, 0x6c, 0x07,
  22730. /* Qy */
  22731. 0xf2, 0x3a, 0xb5, 0x10, 0x32, 0x8d, 0xce, 0x9e,
  22732. 0x76, 0xa0, 0xd2, 0x8c, 0xf3, 0xfc, 0xa9, 0x94,
  22733. 0x43, 0x24, 0xe6, 0x82, 0x00, 0x40, 0xc6, 0xdb,
  22734. 0x1c, 0x2f, 0xcd, 0x38, 0x4b, 0x60, 0xdd, 0x61
  22735. };
  22736. #ifdef HAVE_ECC384
  22737. static const byte p384PubKey[] = {
  22738. /* SECP384R1 */
  22739. /* Qx */
  22740. 0xea, 0xcf, 0x93, 0x4f, 0x2c, 0x09, 0xbb, 0x39,
  22741. 0x14, 0x0f, 0x56, 0x64, 0xc3, 0x40, 0xb4, 0xdf,
  22742. 0x0e, 0x63, 0xae, 0xe5, 0x71, 0x4b, 0x00, 0xcc,
  22743. 0x04, 0x97, 0xff, 0xe1, 0xe9, 0x38, 0x96, 0xbb,
  22744. 0x5f, 0x91, 0xb2, 0x6a, 0xcc, 0xb5, 0x39, 0x5f,
  22745. 0x8f, 0x70, 0x59, 0xf1, 0x01, 0xf6, 0x5a, 0x2b,
  22746. /* Qy */
  22747. 0x01, 0x6c, 0x68, 0x0b, 0xcf, 0x55, 0x25, 0xaf,
  22748. 0x6d, 0x98, 0x48, 0x0a, 0xa8, 0x74, 0xc9, 0xa9,
  22749. 0x17, 0xa0, 0x0c, 0xc3, 0xfb, 0xd3, 0x23, 0x68,
  22750. 0xfe, 0x04, 0x3c, 0x63, 0x50, 0x88, 0x3b, 0xb9,
  22751. 0x4f, 0x7c, 0x67, 0x34, 0xf7, 0x3b, 0xa9, 0x73,
  22752. 0xe7, 0x1b, 0xc3, 0x51, 0x5e, 0x22, 0x18, 0xec
  22753. };
  22754. #endif
  22755. #ifdef HAVE_ECC521
  22756. static const byte p521PubKey[] = {
  22757. /* SECP521R1 */
  22758. /* Qx */
  22759. 0x01, 0x62, 0x6e, 0xf1, 0x00, 0xec, 0xd8, 0x99,
  22760. 0x58, 0x9b, 0x80, 0x6b, 0xfe, 0x2c, 0xf1, 0xb2,
  22761. 0xf0, 0xc8, 0x48, 0xdf, 0xac, 0xd2, 0x3b, 0x71,
  22762. 0x29, 0xab, 0xf0, 0x66, 0x63, 0xd8, 0x8e, 0xb5,
  22763. 0xc8, 0xc2, 0xfc, 0x99, 0x44, 0xe2, 0x45, 0xb1,
  22764. 0x5a, 0x7b, 0xb9, 0x73, 0x01, 0xda, 0x79, 0xec,
  22765. 0x9c, 0x26, 0x27, 0x34, 0x45, 0x26, 0xd5, 0x89,
  22766. 0x4b, 0x44, 0xfe, 0x69, 0x4e, 0x72, 0x14, 0xe3,
  22767. 0x8b, 0xbc,
  22768. /* Qy */
  22769. 0x00, 0x0f, 0x09, 0xa2, 0x03, 0xc3, 0x5a, 0xdc,
  22770. 0x95, 0x82, 0xf6, 0xf9, 0xf6, 0x9c, 0xff, 0xb5,
  22771. 0x6b, 0x75, 0x95, 0x4b, 0xa4, 0x28, 0x5d, 0x9e,
  22772. 0x90, 0x04, 0xd1, 0xc0, 0x1e, 0xd5, 0xfd, 0x43,
  22773. 0x9e, 0x1e, 0x83, 0xc0, 0x11, 0x2b, 0x2b, 0x07,
  22774. 0x6d, 0xa9, 0x7a, 0x10, 0xd7, 0x67, 0xe7, 0x51,
  22775. 0x37, 0x24, 0xd8, 0xbf, 0x03, 0x0d, 0x8b, 0xb5,
  22776. 0x40, 0x5c, 0x4f, 0xd6, 0x13, 0x73, 0x42, 0xbc,
  22777. 0x91, 0xd9
  22778. };
  22779. /* perform verify of signature and hash using public key */
  22780. /* key is public Qx + public Qy */
  22781. /* sig is r + s */
  22782. static int crypto_ecc_verify(const byte *key, uint32_t keySz,
  22783. const byte *hash, uint32_t hashSz, const byte *sig, uint32_t sigSz,
  22784. uint32_t curveSz, int curveId)
  22785. {
  22786. int ret, verify_res = 0, count = 0;
  22787. mp_int r, s;
  22788. ecc_key ecc;
  22789. ecc_nb_ctx_t nb_ctx;
  22790. /* validate arguments */
  22791. if (key == NULL || hash == NULL || sig == NULL || curveSz == 0 ||
  22792. hashSz == 0 || keySz < (curveSz*2) || sigSz < (curveSz*2))
  22793. {
  22794. return BAD_FUNC_ARG;
  22795. }
  22796. /* Setup the ECC key */
  22797. ret = wc_ecc_init(&ecc);
  22798. if (ret < 0) {
  22799. return ret;
  22800. }
  22801. ret = wc_ecc_set_nonblock(&ecc, &nb_ctx);
  22802. if (ret != MP_OKAY) {
  22803. wc_ecc_free(&ecc);
  22804. return ret;
  22805. }
  22806. /* Setup the signature r/s variables */
  22807. ret = mp_init(&r);
  22808. if (ret != MP_OKAY) {
  22809. wc_ecc_free(&ecc);
  22810. return ret;
  22811. }
  22812. ret = mp_init(&s);
  22813. if (ret != MP_OKAY) {
  22814. mp_clear(&r);
  22815. wc_ecc_free(&ecc);
  22816. return ret;
  22817. }
  22818. /* Import public key x/y */
  22819. ret = wc_ecc_import_unsigned(
  22820. &ecc,
  22821. (byte*)key, /* Public "x" Coordinate */
  22822. (byte*)(key + curveSz), /* Public "y" Coordinate */
  22823. NULL, /* Private "d" (optional) */
  22824. curveId /* ECC Curve Id */
  22825. );
  22826. /* Make sure it was a public key imported */
  22827. if (ret == 0 && ecc.type != ECC_PUBLICKEY) {
  22828. ret = ECC_BAD_ARG_E;
  22829. }
  22830. /* Import signature r/s */
  22831. if (ret == 0) {
  22832. ret = mp_read_unsigned_bin(&r, sig, curveSz);
  22833. }
  22834. if (ret == 0) {
  22835. ret = mp_read_unsigned_bin(&s, sig + curveSz, curveSz);
  22836. }
  22837. /* Verify ECC Signature */
  22838. if (ret == 0) {
  22839. do {
  22840. ret = wc_ecc_verify_hash_ex(
  22841. &r, &s, /* r/s as mp_int */
  22842. hash, hashSz, /* computed hash digest */
  22843. &verify_res, /* verification result 1=success */
  22844. &ecc
  22845. );
  22846. count++;
  22847. /* This is where real-time work could be called */
  22848. } while (ret == FP_WOULDBLOCK);
  22849. #ifdef DEBUG_WOLFSSL
  22850. printf("ECC non-block verify: %d times\n", count);
  22851. #endif
  22852. }
  22853. /* check verify result */
  22854. if (ret == 0 && verify_res == 0) {
  22855. ret = SIG_VERIFY_E;
  22856. }
  22857. mp_clear(&r);
  22858. mp_clear(&s);
  22859. wc_ecc_free(&ecc);
  22860. (void)count;
  22861. return ret;
  22862. }
  22863. /* perform signature operation against hash using private key */
  22864. static int crypto_ecc_sign(const byte *key, uint32_t keySz,
  22865. const byte *hash, uint32_t hashSz, byte *sig, uint32_t* sigSz,
  22866. uint32_t curveSz, int curveId, WC_RNG* rng)
  22867. {
  22868. int ret, count = 0;
  22869. mp_int r, s;
  22870. ecc_key ecc;
  22871. ecc_nb_ctx_t nb_ctx;
  22872. /* validate arguments */
  22873. if (key == NULL || hash == NULL || sig == NULL || sigSz == NULL ||
  22874. curveSz == 0 || hashSz == 0 || keySz < curveSz || *sigSz < (curveSz*2))
  22875. {
  22876. return BAD_FUNC_ARG;
  22877. }
  22878. /* Initialize signature result */
  22879. memset(sig, 0, curveSz*2);
  22880. /* Setup the ECC key */
  22881. ret = wc_ecc_init(&ecc);
  22882. if (ret < 0) {
  22883. return ret;
  22884. }
  22885. ret = wc_ecc_set_nonblock(&ecc, &nb_ctx);
  22886. if (ret != MP_OKAY) {
  22887. wc_ecc_free(&ecc);
  22888. return ret;
  22889. }
  22890. /* Setup the signature r/s variables */
  22891. ret = mp_init(&r);
  22892. if (ret != MP_OKAY) {
  22893. wc_ecc_free(&ecc);
  22894. return ret;
  22895. }
  22896. ret = mp_init(&s);
  22897. if (ret != MP_OKAY) {
  22898. mp_clear(&r);
  22899. wc_ecc_free(&ecc);
  22900. return ret;
  22901. }
  22902. /* Import private key "k" */
  22903. ret = wc_ecc_import_private_key_ex(
  22904. key, keySz, /* private key "d" */
  22905. NULL, 0, /* public (optional) */
  22906. &ecc,
  22907. curveId /* ECC Curve Id */
  22908. );
  22909. if (ret == 0) {
  22910. do {
  22911. /* Verify ECC Signature */
  22912. ret = wc_ecc_sign_hash_ex(
  22913. hash, hashSz, /* computed hash digest */
  22914. rng, &ecc, /* random and key context */
  22915. &r, &s /* r/s as mp_int */
  22916. );
  22917. count++;
  22918. /* This is where real-time work could be called */
  22919. } while (ret == FP_WOULDBLOCK);
  22920. #ifdef DEBUG_WOLFSSL
  22921. printf("ECC non-block sign: %d times\n", count);
  22922. #endif
  22923. }
  22924. if (ret == 0) {
  22925. /* export r/s */
  22926. mp_to_unsigned_bin_len(&r, sig, curveSz);
  22927. mp_to_unsigned_bin_len(&s, sig + curveSz, curveSz);
  22928. }
  22929. mp_clear(&r);
  22930. mp_clear(&s);
  22931. wc_ecc_free(&ecc);
  22932. (void)count;
  22933. return ret;
  22934. }
  22935. #endif /* HAVE_ECC_SIGN && HAVE_ECC_VERIFY */
  22936. /*
  22937. * This test doesn't work with WOLFSSL_VALIDATE_ECC_KEYGEN defined because we
  22938. * don't have non-blocking versions of the key checking functions, yet.
  22939. */
  22940. #if defined(HAVE_ECC_DHE) && !defined(WOLFSSL_VALIDATE_ECC_KEYGEN)
  22941. static int ecc_test_nonblock_dhe(int curveId, word32 curveSz,
  22942. const byte* privKey, const byte* pubKey, WC_RNG* rng)
  22943. {
  22944. int ret;
  22945. ecc_key keyA;
  22946. ecc_key keyB;
  22947. ecc_nb_ctx_t nbCtxA;
  22948. ecc_nb_ctx_t nbCtxB;
  22949. byte secretA[ECC_SHARED_SIZE];
  22950. byte secretB[ECC_SHARED_SIZE];
  22951. word32 secretSzA = ECC_SHARED_SIZE;
  22952. word32 secretSzB = ECC_SHARED_SIZE;
  22953. int count = 0;
  22954. ret = wc_ecc_init(&keyA);
  22955. if (ret == 0) {
  22956. ret = wc_ecc_init(&keyB);
  22957. }
  22958. if (ret == 0) {
  22959. ret = wc_ecc_set_nonblock(&keyA, &nbCtxA);
  22960. }
  22961. if (ret == 0) {
  22962. ret = wc_ecc_set_nonblock(&keyB, &nbCtxB);
  22963. }
  22964. if (ret == 0) {
  22965. do {
  22966. ret = wc_ecc_make_key_ex(rng, curveSz, &keyA, curveId);
  22967. count++;
  22968. } while (ret == FP_WOULDBLOCK);
  22969. }
  22970. #ifdef DEBUG_WOLFSSL
  22971. fprintf(stderr, "ECC non-block key gen: %d times\n", count);
  22972. #endif
  22973. if (ret == 0) {
  22974. ret = wc_ecc_check_key(&keyA);
  22975. }
  22976. if (ret == 0) {
  22977. ret = wc_ecc_import_unsigned(&keyB, pubKey, pubKey + curveSz,
  22978. privKey, curveId);
  22979. }
  22980. count = 0;
  22981. if (ret == 0) {
  22982. do {
  22983. ret = wc_ecc_shared_secret(&keyA, &keyB, secretA, &secretSzA);
  22984. count++;
  22985. } while (ret == FP_WOULDBLOCK);
  22986. }
  22987. #ifdef DEBUG_WOLFSSL
  22988. fprintf(stderr, "ECC non-block shared secret: %d times\n", count);
  22989. #endif
  22990. if (ret == 0) {
  22991. do {
  22992. ret = wc_ecc_shared_secret(&keyB, &keyA, secretB, &secretSzB);
  22993. } while (ret == FP_WOULDBLOCK);
  22994. }
  22995. if (ret == 0) {
  22996. if (secretSzA != secretSzB ||
  22997. XMEMCMP(secretA, secretB, secretSzA) != 0) {
  22998. ret = -1;
  22999. }
  23000. }
  23001. wc_ecc_free(&keyA);
  23002. wc_ecc_free(&keyB);
  23003. return ret;
  23004. }
  23005. #endif /* HAVE_ECC_DHE && !WOLFSSL_VALIDATE_ECC_KEYGEN */
  23006. #if defined(HAVE_ECC_SIGN) && defined(HAVE_ECC_VERIFY)
  23007. static int ecc_test_nonblock_ecdsa(int curveId, word32 curveSz,
  23008. const byte* privKey, word32 privKeySz, const byte* pubKey, word32 pubKeySz,
  23009. WC_RNG* rng)
  23010. {
  23011. int ret = 0;
  23012. byte* sig = NULL;
  23013. word32 sigSz = curveSz * 2;
  23014. static const byte hash[] = {
  23015. 0x8d, 0x28, 0xa3, 0x8b, 0x0b, 0xa9, 0xfe, 0xd4, 0x0e, 0x54, 0xc4, 0x17,
  23016. 0x3d, 0x54, 0x66, 0x34, 0xbf, 0x5d, 0x6f, 0x46, 0xc2, 0x20, 0xcb, 0xc3,
  23017. 0x22, 0xe9, 0xb0, 0xdf, 0xe7, 0x64, 0x3f, 0xd9
  23018. };
  23019. sig = (byte*)XMALLOC(sigSz, HEAP_HINT, DYNAMIC_TYPE_SIGNATURE);
  23020. if (sig == NULL) {
  23021. ret = -1;
  23022. }
  23023. if (ret == 0) {
  23024. /* Sign hash using private key */
  23025. /* Note: result of an ECC sign varies for each call even with same
  23026. private key and hash. This is because a new random public key is
  23027. used for each operation. */
  23028. ret = crypto_ecc_sign(privKey, privKeySz, hash, sizeof(hash), sig,
  23029. &sigSz, curveSz, curveId, rng);
  23030. }
  23031. if (ret == 0) {
  23032. /* Verify generated signature is valid */
  23033. ret = crypto_ecc_verify(pubKey, pubKeySz, hash, sizeof(hash), sig,
  23034. sigSz, curveSz, curveId);
  23035. }
  23036. if (sig != NULL) {
  23037. XFREE(sig, HEAP_HINT, DYNAMIC_TYPE_SIGNATURE);
  23038. }
  23039. return ret;
  23040. }
  23041. #endif /* HAVE_ECC_SIGN && HAVE_ECC_VERIFY */
  23042. static int ecc_test_nonblock(WC_RNG* rng)
  23043. {
  23044. int ret = 0;
  23045. word32 i;
  23046. int curveIds[3] = {0, 0, 0};
  23047. word32 curveSzs[3] = {0, 0, 0};
  23048. const byte* privKeys[3] = {NULL, NULL, NULL};
  23049. word32 privKeySzs[3] = {0, 0, 0};
  23050. const byte* pubKeys[3] = {NULL, NULL, NULL};
  23051. word32 pubKeySzs[3] = {0, 0, 0};
  23052. curveIds[0] = ECC_SECP256R1;
  23053. curveSzs[0] = 32;
  23054. privKeys[0] = p256PrivKey;
  23055. privKeySzs[0] = sizeof(p256PrivKey);
  23056. pubKeys[0] = p256PubKey;
  23057. pubKeySzs[0] = sizeof(p256PubKey);
  23058. #ifdef HAVE_ECC384
  23059. curveIds[1] = ECC_SECP384R1;
  23060. curveSzs[1] = 48;
  23061. privKeys[1] = p384PrivKey;
  23062. privKeySzs[1] = sizeof(p384PrivKey);
  23063. pubKeys[1] = p384PubKey;
  23064. pubKeySzs[1] = sizeof(p384PubKey);
  23065. #endif
  23066. #ifdef HAVE_ECC521
  23067. curveIds[2] = ECC_SECP521R1;
  23068. curveSzs[2] = 66;
  23069. privKeys[2] = p521PrivKey;
  23070. privKeySzs[2] = sizeof(p521PrivKey);
  23071. pubKeys[2] = p521PubKey;
  23072. pubKeySzs[2] = sizeof(p521PubKey);
  23073. #endif
  23074. for (i = 0; ret == 0 && i < sizeof(curveIds) / sizeof(curveIds[0]); ++i) {
  23075. if (curveIds[i] == 0) {
  23076. continue;
  23077. }
  23078. #if defined(HAVE_ECC_SIGN) && defined(HAVE_ECC_VERIFY)
  23079. ret = ecc_test_nonblock_ecdsa(curveIds[i], curveSzs[i], privKeys[i],
  23080. privKeySzs[i], pubKeys[i], pubKeySzs[i], rng);
  23081. #endif /* HAVE_ECC_SIGN && HAVE_ECC_VERIFY */
  23082. #if defined(HAVE_ECC_DHE) && !defined(WOLFSSL_VALIDATE_ECC_KEYGEN)
  23083. if (ret == 0) {
  23084. ret = ecc_test_nonblock_dhe(curveIds[i], curveSzs[i], privKeys[i],
  23085. pubKeys[i], rng);
  23086. }
  23087. #endif /* HAVE_ECC_DHE && !WOLFSSL_VALIDATE_ECC_KEYGEN */
  23088. }
  23089. return ret;
  23090. }
  23091. #endif /* WC_ECC_NONBLOCK && WOLFSSL_HAVE_SP_ECC && WOLFSSL_PUBLIC_MP */
  23092. WOLFSSL_TEST_SUBROUTINE int ecc_test(void)
  23093. {
  23094. int ret;
  23095. WC_RNG rng;
  23096. #if defined(WOLFSSL_CERT_EXT) && \
  23097. (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  23098. ret = ecc_decode_test();
  23099. if (ret < 0)
  23100. return ret;
  23101. #endif
  23102. #ifndef HAVE_FIPS
  23103. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  23104. #else
  23105. ret = wc_InitRng(&rng);
  23106. #endif
  23107. #ifndef WC_NO_RNG
  23108. if (ret != 0)
  23109. return -10300;
  23110. #else
  23111. (void)ret;
  23112. #endif
  23113. #if (defined(HAVE_ECC112) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 112
  23114. ret = ecc_test_curve(&rng, 14);
  23115. if (ret < 0) {
  23116. goto done;
  23117. }
  23118. #endif /* HAVE_ECC112 */
  23119. #if (defined(HAVE_ECC128) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 128
  23120. ret = ecc_test_curve(&rng, 16);
  23121. if (ret < 0) {
  23122. goto done;
  23123. }
  23124. #endif /* HAVE_ECC128 */
  23125. #if (defined(HAVE_ECC160) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 160
  23126. ret = ecc_test_curve(&rng, 20);
  23127. if (ret < 0) {
  23128. goto done;
  23129. }
  23130. #endif /* HAVE_ECC160 */
  23131. #if (defined(HAVE_ECC192) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 192
  23132. ret = ecc_test_curve(&rng, 24);
  23133. if (ret < 0) {
  23134. goto done;
  23135. }
  23136. #endif /* HAVE_ECC192 */
  23137. #if (defined(HAVE_ECC224) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 224
  23138. ret = ecc_test_curve(&rng, 28);
  23139. if (ret < 0) {
  23140. goto done;
  23141. }
  23142. #endif /* HAVE_ECC224 */
  23143. #if (defined(HAVE_ECC239) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 239
  23144. ret = ecc_test_curve(&rng, 30);
  23145. if (ret < 0) {
  23146. goto done;
  23147. }
  23148. #endif /* HAVE_ECC239 */
  23149. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  23150. ret = ecc_test_curve(&rng, 32);
  23151. if (ret < 0) {
  23152. goto done;
  23153. }
  23154. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  23155. defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT) && \
  23156. !defined(WOLFSSL_NO_MALLOC) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  23157. ret = ecc_point_test();
  23158. if (ret < 0) {
  23159. goto done;
  23160. }
  23161. #endif
  23162. ret = ecc_def_curve_test(&rng);
  23163. if (ret < 0) {
  23164. goto done;
  23165. }
  23166. #endif /* !NO_ECC256 */
  23167. #if (defined(HAVE_ECC320) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 320
  23168. ret = ecc_test_curve(&rng, 40);
  23169. if (ret < 0) {
  23170. goto done;
  23171. }
  23172. #endif /* HAVE_ECC320 */
  23173. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 384
  23174. ret = ecc_test_curve(&rng, 48);
  23175. if (ret < 0) {
  23176. goto done;
  23177. }
  23178. #endif /* HAVE_ECC384 */
  23179. #if (defined(HAVE_ECC512) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 512
  23180. ret = ecc_test_curve(&rng, 64);
  23181. if (ret < 0) {
  23182. goto done;
  23183. }
  23184. #endif /* HAVE_ECC512 */
  23185. #if (defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 521
  23186. ret = ecc_test_curve(&rng, 66);
  23187. if (ret < 0) {
  23188. goto done;
  23189. }
  23190. #endif /* HAVE_ECC521 */
  23191. #if defined(WOLFSSL_CUSTOM_CURVES)
  23192. ret = ecc_test_custom_curves(&rng);
  23193. if (ret != 0) {
  23194. goto done;
  23195. }
  23196. #endif
  23197. #if defined(HAVE_ECC_SIGN) && (defined(WOLFSSL_ECDSA_DETERMINISTIC_K) || \
  23198. defined(WOLFSSL_ECDSA_DETERMINISTIC_K_VARIANT)) \
  23199. && (!defined(FIPS_VERSION_GE) || FIPS_VERSION_GE(5,3))
  23200. #ifdef HAVE_ECC256
  23201. ret = ecc_test_deterministic_k(&rng);
  23202. if (ret != 0) {
  23203. printf("ecc_test_deterministic_k failed! %d\n", ret);
  23204. goto done;
  23205. }
  23206. #endif
  23207. #ifdef WOLFSSL_PUBLIC_MP
  23208. #if defined(HAVE_ECC384)
  23209. ret = ecc384_test_deterministic_k(&rng);
  23210. if (ret != 0) {
  23211. printf("ecc384_test_deterministic_k failed! %d\n", ret);
  23212. goto done;
  23213. }
  23214. #endif
  23215. #if defined(HAVE_ECC521)
  23216. ret = ecc521_test_deterministic_k(&rng);
  23217. if (ret != 0) {
  23218. printf("ecc512_test_deterministic_k failed! %d\n", ret);
  23219. goto done;
  23220. }
  23221. #endif
  23222. #endif
  23223. #endif
  23224. #if defined(HAVE_ECC_SIGN) && defined(WOLFSSL_ECDSA_SET_K) && \
  23225. !defined(WOLFSSL_KCAPI_ECC)
  23226. ret = ecc_test_sign_vectors(&rng);
  23227. if (ret != 0) {
  23228. printf("ecc_test_sign_vectors failed! %d\n", ret);
  23229. goto done;
  23230. }
  23231. #endif
  23232. #ifdef HAVE_ECC_CDH
  23233. ret = ecc_test_cdh_vectors(&rng);
  23234. if (ret != 0) {
  23235. printf("ecc_test_cdh_vectors failed! %d\n", ret);
  23236. goto done;
  23237. }
  23238. #endif
  23239. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  23240. !defined(WOLFSSL_STM32_PKA) && !defined(WOLFSSL_SILABS_SE_ACCEL) && \
  23241. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  23242. ret = ecc_test_make_pub(&rng);
  23243. if (ret != 0) {
  23244. printf("ecc_test_make_pub failed!: %d\n", ret);
  23245. goto done;
  23246. }
  23247. #elif defined(HAVE_ECC_KEY_IMPORT)
  23248. (void)ecc_test_make_pub; /* for compiler warning */
  23249. #endif
  23250. #ifdef WOLFSSL_CERT_GEN
  23251. ret = ecc_test_cert_gen(&rng);
  23252. if (ret != 0) {
  23253. printf("ecc_test_cert_gen failed!: %d\n", ret);
  23254. goto done;
  23255. }
  23256. #endif
  23257. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && !defined(WOLFSSL_NO_MALLOC) && \
  23258. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  23259. ret = ecc_test_allocator(&rng);
  23260. if (ret != 0) {
  23261. printf("ecc_test_allocator failed!: %d\n", ret);
  23262. goto done;
  23263. }
  23264. #endif
  23265. #if defined(WC_ECC_NONBLOCK) && defined(WOLFSSL_PUBLIC_MP) && \
  23266. defined(HAVE_ECC_SIGN) && defined(HAVE_ECC_VERIFY)
  23267. ret = ecc_test_nonblock(&rng);
  23268. if (ret != 0) {
  23269. printf("ecc_test_nonblock failed!: %d\n", ret);
  23270. goto done;
  23271. }
  23272. #endif
  23273. done:
  23274. wc_FreeRng(&rng);
  23275. return ret;
  23276. }
  23277. #if defined(HAVE_ECC_ENCRYPT) && defined(HAVE_AES_CBC) && \
  23278. (defined(WOLFSSL_AES_128) || defined(WOLFSSL_AES_256))
  23279. /* ecc_encrypt_e2e_test() uses wc_ecc_ctx_set_algo(), which was added in
  23280. * wolfFIPS 5.3.
  23281. * ecc_encrypt_kat() is used only by ecc_encrypt_e2e_test().
  23282. */
  23283. #if !defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3))
  23284. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && \
  23285. ECC_MIN_KEY_SZ <= 256 && defined(WOLFSSL_AES_128)
  23286. static int ecc_encrypt_kat(WC_RNG *rng)
  23287. {
  23288. int ret = 0;
  23289. #ifdef WOLFSSL_ECIES_OLD
  23290. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  23291. ecc_key* userA = NULL;
  23292. #else
  23293. ecc_key userA[1];
  23294. #endif
  23295. int userAInit = 0;
  23296. #endif
  23297. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  23298. ecc_key* userB = NULL;
  23299. #else
  23300. ecc_key userB[1];
  23301. #endif
  23302. int userBInit = 0;
  23303. ecc_key* tmpKey;
  23304. byte plain[48];
  23305. word32 plainSz = sizeof(plain);
  23306. WOLFSSL_SMALL_STACK_STATIC const byte privKey[] = {
  23307. 0x04, 0x80, 0xef, 0x1d, 0xbe, 0x02, 0x0c, 0x20,
  23308. 0x5b, 0xab, 0x80, 0x35, 0x5b, 0x2a, 0x0f, 0x6d,
  23309. 0xd3, 0xb0, 0x7f, 0x7e, 0x7f, 0x86, 0x8a, 0x49,
  23310. 0xee, 0xb4, 0xaa, 0x09, 0x2d, 0x1e, 0x1d, 0x02
  23311. };
  23312. #if defined(WOLFSSL_ECIES_OLD) || defined(WOLFSSL_QNX_CAAM)
  23313. WOLFSSL_SMALL_STACK_STATIC const byte pubKey[] = {
  23314. 0x04,
  23315. /* X */
  23316. 0x50, 0xf2, 0x93, 0xa2, 0x48, 0xa9, 0xc0, 0x5a,
  23317. 0x9a, 0xa7, 0x70, 0x34, 0xb7, 0x7f, 0x4c, 0x3a,
  23318. 0xad, 0xfc, 0xd8, 0xb6, 0x76, 0x0a, 0xe3, 0xc1,
  23319. 0x87, 0x17, 0x07, 0x2d, 0x8d, 0xa3, 0x63, 0xa0,
  23320. /* X */
  23321. 0xc1, 0x27, 0xb2, 0x97, 0x9b, 0x84, 0xe7, 0xcd,
  23322. 0x20, 0x65, 0x8d, 0x2b, 0x6a, 0x93, 0x75, 0xaa,
  23323. 0x8b, 0xe1, 0x3a, 0x7b, 0x24, 0x1a, 0xbe, 0xe8,
  23324. 0x36, 0xd2, 0xe6, 0x34, 0x8a, 0x7a, 0xb3, 0x28
  23325. };
  23326. #endif
  23327. WOLFSSL_SMALL_STACK_STATIC const byte enc_msg[] = {
  23328. #ifdef WOLFSSL_ECIES_OLD
  23329. 0x42, 0x70, 0xbf, 0xf9, 0xf4, 0x7e, 0x4b, 0x9b,
  23330. 0xb5, 0x4c, 0xcc, 0xc5, 0x94, 0xa7, 0xef, 0xaa,
  23331. 0xc3, 0x7c, 0x85, 0xa6, 0x51, 0x6e, 0xd3, 0xfa,
  23332. 0x56, 0xc9, 0x10, 0x4d, 0x14, 0x32, 0x61, 0xb8,
  23333. 0xbb, 0x66, 0x7a, 0xb5, 0xbc, 0x95, 0xf8, 0xca,
  23334. 0xd1, 0x2a, 0x19, 0x51, 0x44, 0xd8, 0x0e, 0x57,
  23335. 0x34, 0xed, 0x45, 0x89, 0x2e, 0x57, 0xbe, 0xd5,
  23336. 0x06, 0x22, 0xd7, 0x13, 0x0a, 0x0e, 0x40, 0x36,
  23337. 0x0d, 0x05, 0x0d, 0xb6, 0xae, 0x61, 0x37, 0x18,
  23338. 0x83, 0x90, 0x0a, 0x27, 0x95, 0x41, 0x8c, 0x45
  23339. #elif defined(WOLFSSL_ECIES_ISO18033)
  23340. 0x04, 0x50, 0xf2, 0x93, 0xa2, 0x48, 0xa9, 0xc0,
  23341. 0x5a, 0x9a, 0xa7, 0x70, 0x34, 0xb7, 0x7f, 0x4c,
  23342. 0x3a, 0xad, 0xfc, 0xd8, 0xb6, 0x76, 0x0a, 0xe3,
  23343. 0xc1, 0x87, 0x17, 0x07, 0x2d, 0x8d, 0xa3, 0x63,
  23344. 0xa0, 0xc1, 0x27, 0xb2, 0x97, 0x9b, 0x84, 0xe7,
  23345. 0xcd, 0x20, 0x65, 0x8d, 0x2b, 0x6a, 0x93, 0x75,
  23346. 0xaa, 0x8b, 0xe1, 0x3a, 0x7b, 0x24, 0x1a, 0xbe,
  23347. 0xe8, 0x36, 0xd2, 0xe6, 0x34, 0x8a, 0x7a, 0xb3,
  23348. 0x28, 0xbb, 0x9f, 0xa8, 0x2d, 0xe1, 0xf1, 0x67,
  23349. 0x45, 0x02, 0x19, 0xdc, 0xc8, 0x24, 0x8b, 0x20,
  23350. 0x02, 0xa0, 0x8f, 0x95, 0x12, 0x55, 0x51, 0xf8,
  23351. 0x03, 0xc4, 0x54, 0x13, 0x98, 0x2d, 0xf0, 0x31,
  23352. 0x51, 0x80, 0x45, 0x24, 0xcb, 0x8b, 0x48, 0xa6,
  23353. 0x8b, 0x8e, 0x97, 0x9c, 0x56, 0x4d, 0x70, 0x00,
  23354. 0x53, 0xd3, 0x47, 0x00, 0x5a, 0x23, 0x8c, 0xf9,
  23355. 0xfd, 0xd2, 0x33, 0x2c, 0x43, 0x6e, 0x9e, 0xb2,
  23356. 0xf4, 0x95, 0xd4, 0xcf, 0x30, 0xd6, 0xa2, 0xc5,
  23357. 0x35, 0x96, 0x6a, 0xd4, 0x36, 0x15, 0xa9, 0xbd,
  23358. 0x7f
  23359. #elif defined(WOLFSSL_ECIES_GEN_IV)
  23360. /* EC P-256 point */
  23361. 0x04,
  23362. /* X */
  23363. 0x50, 0xf2, 0x93, 0xa2, 0x48, 0xa9, 0xc0, 0x5a,
  23364. 0x9a, 0xa7, 0x70, 0x34, 0xb7, 0x7f, 0x4c, 0x3a,
  23365. 0xad, 0xfc, 0xd8, 0xb6, 0x76, 0x0a, 0xe3, 0xc1,
  23366. 0x87, 0x17, 0x07, 0x2d, 0x8d, 0xa3, 0x63, 0xa0,
  23367. /* Y */
  23368. 0xc1, 0x27, 0xb2, 0x97, 0x9b, 0x84, 0xe7, 0xcd,
  23369. 0x20, 0x65, 0x8d, 0x2b, 0x6a, 0x93, 0x75, 0xaa,
  23370. 0x8b, 0xe1, 0x3a, 0x7b, 0x24, 0x1a, 0xbe, 0xe8,
  23371. 0x36, 0xd2, 0xe6, 0x34, 0x8a, 0x7a, 0xb3, 0x28,
  23372. /* IV */
  23373. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  23374. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  23375. /* Encrypted Msg */
  23376. 0xe5, 0x17, 0xaf, 0x0d, 0x65, 0x4d, 0x3d, 0x50,
  23377. 0x96, 0x05, 0xc9, 0x63, 0x2c, 0xef, 0x1c, 0x1f,
  23378. 0x78, 0xc9, 0x90, 0x7a, 0x14, 0x00, 0xfc, 0x44,
  23379. 0x71, 0x6d, 0x57, 0x8c, 0xdf, 0x23, 0xca, 0x65,
  23380. 0xcf, 0x93, 0x06, 0xb6, 0x9a, 0xf4, 0x61, 0xbd,
  23381. 0x44, 0x1a, 0xeb, 0x52, 0x68, 0x0f, 0xd1, 0xde,
  23382. /* HMAC */
  23383. 0x5a, 0x22, 0xc1, 0x5d, 0x99, 0x66, 0x3f, 0x24,
  23384. 0x35, 0x96, 0xac, 0xf7, 0xf6, 0x28, 0x45, 0x16,
  23385. 0x52, 0x19, 0x0d, 0xe4, 0xb2, 0xca, 0x5b, 0x28,
  23386. 0x4e, 0xbb, 0xf3, 0x98, 0x57, 0xd7, 0x3b, 0xe2
  23387. #else
  23388. 0x04, 0x50, 0xf2, 0x93, 0xa2, 0x48, 0xa9, 0xc0,
  23389. 0x5a, 0x9a, 0xa7, 0x70, 0x34, 0xb7, 0x7f, 0x4c,
  23390. 0x3a, 0xad, 0xfc, 0xd8, 0xb6, 0x76, 0x0a, 0xe3,
  23391. 0xc1, 0x87, 0x17, 0x07, 0x2d, 0x8d, 0xa3, 0x63,
  23392. 0xa0, 0xc1, 0x27, 0xb2, 0x97, 0x9b, 0x84, 0xe7,
  23393. 0xcd, 0x20, 0x65, 0x8d, 0x2b, 0x6a, 0x93, 0x75,
  23394. 0xaa, 0x8b, 0xe1, 0x3a, 0x7b, 0x24, 0x1a, 0xbe,
  23395. 0xe8, 0x36, 0xd2, 0xe6, 0x34, 0x8a, 0x7a, 0xb3,
  23396. 0x28, 0xe5, 0x17, 0xaf, 0x0d, 0x65, 0x4d, 0x3d,
  23397. 0x50, 0x96, 0x05, 0xc9, 0x63, 0x2c, 0xef, 0x1c,
  23398. 0x1f, 0x78, 0xc9, 0x90, 0x7a, 0x14, 0x00, 0xfc,
  23399. 0x44, 0x71, 0x6d, 0x57, 0x8c, 0xdf, 0x23, 0xca,
  23400. 0x65, 0xcf, 0x93, 0x06, 0xb6, 0x9a, 0xf4, 0x61,
  23401. 0xbd, 0x44, 0x1a, 0xeb, 0x52, 0x68, 0x0f, 0xd1,
  23402. 0xde, 0xc7, 0x3f, 0x6f, 0xce, 0xbe, 0x49, 0x61,
  23403. 0x48, 0x01, 0x77, 0x41, 0xd0, 0xd8, 0x5b, 0x48,
  23404. 0xca, 0x4e, 0x47, 0x3e, 0x47, 0xbf, 0x1d, 0x28,
  23405. 0x4c, 0x18, 0x1a, 0xfb, 0x96, 0x95, 0xda, 0xde,
  23406. 0x55
  23407. #endif
  23408. };
  23409. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = {
  23410. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  23411. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  23412. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  23413. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
  23414. 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  23415. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f
  23416. };
  23417. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  23418. userB = (ecc_key *)XMALLOC(sizeof(*userB), HEAP_HINT,
  23419. DYNAMIC_TYPE_TMP_BUFFER);
  23420. if (userB == NULL) {
  23421. ret = -10451;
  23422. }
  23423. #ifdef WOLFSSL_ECIES_OLD
  23424. if (ret == 0) {
  23425. userA = (ecc_key *)XMALLOC(sizeof(*userA), HEAP_HINT,
  23426. DYNAMIC_TYPE_TMP_BUFFER);
  23427. if (userA == NULL) {
  23428. ret = -10450;
  23429. }
  23430. }
  23431. #endif
  23432. #endif
  23433. if (ret == 0) {
  23434. ret = wc_ecc_init_ex(userB, HEAP_HINT, devId);
  23435. if (ret != 0)
  23436. ret = -10453;
  23437. }
  23438. if (ret == 0) {
  23439. userBInit = 1;
  23440. #ifdef WOLFSSL_ECIES_OLD
  23441. ret = wc_ecc_init_ex(userA, HEAP_HINT, devId);
  23442. if (ret != 0)
  23443. ret = -10452;
  23444. }
  23445. if (ret == 0) {
  23446. userAInit = 1;
  23447. tmpKey = userA;
  23448. #else
  23449. tmpKey = NULL;
  23450. #endif
  23451. }
  23452. if (ret == 0) {
  23453. #ifdef WOLFSSL_QNX_CAAM
  23454. ret = wc_ecc_import_private_key_ex(privKey, sizeof(privKey), pubKey,
  23455. sizeof(pubKey), userB, ECC_SECP256R1);
  23456. #else
  23457. ret = wc_ecc_import_private_key_ex(privKey, sizeof(privKey), NULL, 0,
  23458. userB, ECC_SECP256R1);
  23459. #endif
  23460. if (ret != 0)
  23461. ret = -10454;
  23462. }
  23463. #ifdef WOLFSSL_ECIES_OLD
  23464. if (ret == 0) {
  23465. ret = wc_ecc_import_x963_ex(pubKey, sizeof(pubKey), userA,
  23466. ECC_SECP256R1);
  23467. if (ret != 0)
  23468. ret = -10455;
  23469. }
  23470. #endif
  23471. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  23472. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  23473. !defined(HAVE_SELFTEST)
  23474. if (ret == 0) {
  23475. ret = wc_ecc_set_rng(userB, rng);
  23476. if (ret != 0) {
  23477. ret = -10456;
  23478. }
  23479. }
  23480. #else
  23481. (void)rng;
  23482. #endif
  23483. if (ret == 0) {
  23484. ret = wc_ecc_decrypt(userB, tmpKey, enc_msg, sizeof(enc_msg), plain,
  23485. &plainSz, NULL);
  23486. if (ret != 0)
  23487. ret = -10457;
  23488. }
  23489. if (ret == 0) {
  23490. if (XMEMCMP(plain, msg, sizeof(msg)) != 0) {
  23491. ret = -10458;
  23492. }
  23493. }
  23494. if (userBInit)
  23495. wc_ecc_free(userB);
  23496. #ifdef WOLFSSL_ECIES_OLD
  23497. if (userAInit)
  23498. wc_ecc_free(userA);
  23499. #endif
  23500. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  23501. if (userB != NULL) {
  23502. XFREE(userB, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23503. }
  23504. #ifdef WOLFSSL_ECIES_OLD
  23505. if (userA != NULL) {
  23506. XFREE(userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23507. }
  23508. #endif
  23509. #endif
  23510. return ret;
  23511. }
  23512. #endif
  23513. static int ecc_encrypt_e2e_test(WC_RNG* rng, ecc_key* userA, ecc_key* userB,
  23514. byte encAlgo, byte kdfAlgo, byte macAlgo)
  23515. {
  23516. int ret = 0;
  23517. byte msg[48];
  23518. byte plain[48];
  23519. #ifdef WOLFSSL_ECIES_OLD
  23520. byte out[80];
  23521. #elif defined(WOLFSSL_ECIES_GEN_IV)
  23522. byte out[1 + ECC_KEYGEN_SIZE * 2 + 16 + 80];
  23523. #else
  23524. byte out[1 + ECC_KEYGEN_SIZE * 2 + 80];
  23525. #endif
  23526. word32 outSz = sizeof(out);
  23527. word32 plainSz = sizeof(plain);
  23528. int i;
  23529. ecEncCtx* cliCtx = NULL;
  23530. ecEncCtx* srvCtx = NULL;
  23531. byte cliSalt[EXCHANGE_SALT_SZ];
  23532. byte srvSalt[EXCHANGE_SALT_SZ];
  23533. const byte* tmpSalt;
  23534. byte msg2[48];
  23535. byte plain2[48];
  23536. #ifdef WOLFSSL_ECIES_OLD
  23537. byte out2[80];
  23538. #elif defined(WOLFSSL_ECIES_GEN_IV)
  23539. byte out2[1 + ECC_KEYGEN_SIZE * 2 + 16 + 80];
  23540. #else
  23541. byte out2[1 + ECC_KEYGEN_SIZE * 2 + 80];
  23542. #endif
  23543. word32 outSz2 = sizeof(out2);
  23544. word32 plainSz2 = sizeof(plain2);
  23545. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  23546. ecc_key *tmpKey = (ecc_key *)XMALLOC(sizeof(ecc_key), HEAP_HINT,
  23547. DYNAMIC_TYPE_TMP_BUFFER);
  23548. #else
  23549. ecc_key tmpKey[1];
  23550. #endif
  23551. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  23552. if (tmpKey == NULL) {
  23553. ERROR_OUT(MEMORY_E, done);
  23554. }
  23555. #endif
  23556. ret = wc_ecc_init_ex(tmpKey, HEAP_HINT, devId);
  23557. if (ret != 0)
  23558. goto done;
  23559. /* set message to incrementing 0,1,2,etc... */
  23560. for (i = 0; i < (int)sizeof(msg); i++)
  23561. msg[i] = i;
  23562. /* encrypt msg to B */
  23563. ret = wc_ecc_encrypt(userA, userB, msg, sizeof(msg), out, &outSz, NULL);
  23564. if (ret != 0) {
  23565. ret = -10405; goto done;
  23566. }
  23567. #ifdef WOLFSSL_ECIES_OLD
  23568. tmpKey->dp = userA->dp;
  23569. ret = wc_ecc_copy_point(&userA->pubkey, &tmpKey->pubkey);
  23570. if (ret != 0) {
  23571. ret = -10413; goto done;
  23572. }
  23573. #endif
  23574. /* decrypt msg from A */
  23575. ret = wc_ecc_decrypt(userB, tmpKey, out, outSz, plain, &plainSz, NULL);
  23576. if (ret != 0) {
  23577. ret = -10406; goto done;
  23578. }
  23579. if (XMEMCMP(plain, msg, sizeof(msg)) != 0) {
  23580. ret = -10407; goto done;
  23581. }
  23582. #ifndef WOLFSSL_ECIES_OLD
  23583. /* A decrypts msg (response) from B */
  23584. ret = wc_ecc_decrypt(userB, NULL, out, outSz, plain2, &plainSz2, NULL);
  23585. if (ret != 0)
  23586. goto done;
  23587. if (XMEMCMP(plain, msg, sizeof(msg)) != 0) {
  23588. ret = -10415; goto done;
  23589. }
  23590. #endif
  23591. /* let's verify message exchange works, A is client, B is server */
  23592. cliCtx = wc_ecc_ctx_new(REQ_RESP_CLIENT, rng);
  23593. srvCtx = wc_ecc_ctx_new(REQ_RESP_SERVER, rng);
  23594. if (cliCtx == NULL || srvCtx == NULL) {
  23595. ret = -10408; goto done;
  23596. }
  23597. ret = wc_ecc_ctx_set_algo(cliCtx, encAlgo, kdfAlgo, macAlgo);
  23598. if (ret != 0)
  23599. goto done;
  23600. ret = wc_ecc_ctx_set_algo(srvCtx, encAlgo, kdfAlgo, macAlgo);
  23601. if (ret != 0)
  23602. goto done;
  23603. /* get salt to send to peer */
  23604. tmpSalt = wc_ecc_ctx_get_own_salt(cliCtx);
  23605. if (tmpSalt == NULL) {
  23606. ret = -10409; goto done;
  23607. }
  23608. XMEMCPY(cliSalt, tmpSalt, EXCHANGE_SALT_SZ);
  23609. tmpSalt = wc_ecc_ctx_get_own_salt(srvCtx);
  23610. if (tmpSalt == NULL) {
  23611. ret = -10410; goto done;
  23612. }
  23613. XMEMCPY(srvSalt, tmpSalt, EXCHANGE_SALT_SZ);
  23614. /* in actual use, we'd get the peer's salt over the transport */
  23615. ret = wc_ecc_ctx_set_peer_salt(cliCtx, srvSalt);
  23616. if (ret != 0)
  23617. goto done;
  23618. ret = wc_ecc_ctx_set_peer_salt(srvCtx, cliSalt);
  23619. if (ret != 0)
  23620. goto done;
  23621. ret = wc_ecc_ctx_set_info(cliCtx, (byte*)"wolfSSL MSGE", 11);
  23622. if (ret != 0)
  23623. goto done;
  23624. ret = wc_ecc_ctx_set_info(srvCtx, (byte*)"wolfSSL MSGE", 11);
  23625. if (ret != 0)
  23626. goto done;
  23627. /* get encrypted msg (request) to send to B */
  23628. outSz = sizeof(out);
  23629. ret = wc_ecc_encrypt(userA, userB, msg, sizeof(msg), out, &outSz,cliCtx);
  23630. if (ret != 0)
  23631. goto done;
  23632. #ifndef WOLFSSL_ECIES_OLD
  23633. wc_ecc_free(tmpKey);
  23634. #endif
  23635. /* B decrypts msg (request) from A */
  23636. plainSz = sizeof(plain);
  23637. ret = wc_ecc_decrypt(userB, tmpKey, out, outSz, plain, &plainSz, srvCtx);
  23638. if (ret != 0)
  23639. goto done;
  23640. if (XMEMCMP(plain, msg, sizeof(msg)) != 0) {
  23641. ret = -10411; goto done;
  23642. }
  23643. /* msg2 (response) from B to A */
  23644. for (i = 0; i < (int)sizeof(msg2); i++)
  23645. msg2[i] = i + sizeof(msg2);
  23646. /* get encrypted msg (response) to send to B */
  23647. ret = wc_ecc_encrypt(userB, userA, msg2, sizeof(msg2), out2,
  23648. &outSz2, srvCtx);
  23649. if (ret != 0)
  23650. goto done;
  23651. #ifdef WOLFSSL_ECIES_OLD
  23652. tmpKey->dp = userB->dp;
  23653. ret = wc_ecc_copy_point(&userB->pubkey, &tmpKey->pubkey);
  23654. if (ret != 0) {
  23655. ret = -10414; goto done;
  23656. }
  23657. #else
  23658. wc_ecc_free(tmpKey);
  23659. #endif
  23660. /* A decrypts msg (response) from B */
  23661. ret = wc_ecc_decrypt(userA, tmpKey, out2, outSz2, plain2, &plainSz2,
  23662. cliCtx);
  23663. if (ret != 0)
  23664. goto done;
  23665. if (XMEMCMP(plain2, msg2, sizeof(msg2)) != 0) {
  23666. ret = -10412; goto done;
  23667. }
  23668. #if defined(HAVE_COMP_KEY) && \
  23669. (! defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3)))
  23670. /* Create new client and server contexts. */
  23671. wc_ecc_ctx_free(srvCtx);
  23672. wc_ecc_ctx_free(cliCtx);
  23673. /* let's verify message exchange works, A is client, B is server */
  23674. cliCtx = wc_ecc_ctx_new(REQ_RESP_CLIENT, rng);
  23675. srvCtx = wc_ecc_ctx_new(REQ_RESP_SERVER, rng);
  23676. if (cliCtx == NULL || srvCtx == NULL) {
  23677. ret = -10416; goto done;
  23678. }
  23679. ret = wc_ecc_ctx_set_algo(cliCtx, encAlgo, kdfAlgo, macAlgo);
  23680. if (ret != 0)
  23681. goto done;
  23682. ret = wc_ecc_ctx_set_algo(srvCtx, encAlgo, kdfAlgo, macAlgo);
  23683. if (ret != 0)
  23684. goto done;
  23685. /* get salt to send to peer */
  23686. tmpSalt = wc_ecc_ctx_get_own_salt(cliCtx);
  23687. if (tmpSalt == NULL) {
  23688. ret = -10417; goto done;
  23689. }
  23690. XMEMCPY(cliSalt, tmpSalt, EXCHANGE_SALT_SZ);
  23691. tmpSalt = wc_ecc_ctx_get_own_salt(srvCtx);
  23692. if (tmpSalt == NULL) {
  23693. ret = -10418; goto done;
  23694. }
  23695. XMEMCPY(srvSalt, tmpSalt, EXCHANGE_SALT_SZ);
  23696. /* in actual use, we'd get the peer's salt over the transport */
  23697. ret = wc_ecc_ctx_set_peer_salt(cliCtx, srvSalt);
  23698. if (ret != 0)
  23699. goto done;
  23700. ret = wc_ecc_ctx_set_peer_salt(srvCtx, cliSalt);
  23701. if (ret != 0)
  23702. goto done;
  23703. ret = wc_ecc_ctx_set_info(cliCtx, (byte*)"wolfSSL MSGE", 12);
  23704. if (ret != 0)
  23705. goto done;
  23706. ret = wc_ecc_ctx_set_info(srvCtx, (byte*)"wolfSSL MSGE", 12);
  23707. if (ret != 0)
  23708. goto done;
  23709. /* get encrypted msg (request) to send to B - compressed public key */
  23710. outSz = sizeof(out);
  23711. ret = wc_ecc_encrypt_ex(userA, userB, msg, sizeof(msg), out, &outSz, cliCtx,
  23712. 1);
  23713. if (ret != 0)
  23714. goto done;
  23715. #ifndef WOLFSSL_ECIES_OLD
  23716. wc_ecc_free(tmpKey);
  23717. #endif
  23718. /* B decrypts msg (request) from A - out has a compressed public key */
  23719. plainSz = sizeof(plain);
  23720. ret = wc_ecc_decrypt(userB, tmpKey, out, outSz, plain, &plainSz, srvCtx);
  23721. if (ret != 0)
  23722. goto done;
  23723. if (XMEMCMP(plain, msg, sizeof(msg)) != 0) {
  23724. ret = -10419; goto done;
  23725. }
  23726. #endif /* HAVE_COMP_KEY && (!FIPS || FIPS>=5.3) */
  23727. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && \
  23728. (ECC_MIN_KEY_SZ <= 256) && defined(WOLFSSL_AES_128)
  23729. ret = ecc_encrypt_kat(rng);
  23730. #endif
  23731. done:
  23732. /* cleanup */
  23733. wc_ecc_ctx_free(srvCtx);
  23734. wc_ecc_ctx_free(cliCtx);
  23735. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  23736. if (tmpKey != NULL) {
  23737. wc_ecc_free(tmpKey);
  23738. XFREE(tmpKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23739. }
  23740. #else
  23741. wc_ecc_free(tmpKey);
  23742. #endif
  23743. return ret;
  23744. }
  23745. #endif /* !HAVE_FIPS || FIPS_VERSION_GE(5,3) */
  23746. WOLFSSL_TEST_SUBROUTINE int ecc_encrypt_test(void)
  23747. {
  23748. WC_RNG rng;
  23749. int ret;
  23750. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  23751. ecc_key *userA;
  23752. ecc_key *userB;
  23753. #else
  23754. ecc_key userA[1];
  23755. ecc_key userB[1];
  23756. #endif
  23757. #ifndef HAVE_FIPS
  23758. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  23759. #else
  23760. ret = wc_InitRng(&rng);
  23761. #endif
  23762. if (ret != 0)
  23763. return -10400;
  23764. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  23765. userA = (ecc_key *)XMALLOC(sizeof *userA, HEAP_HINT,
  23766. DYNAMIC_TYPE_TMP_BUFFER);
  23767. userB = (ecc_key *)XMALLOC(sizeof *userB, HEAP_HINT,
  23768. DYNAMIC_TYPE_TMP_BUFFER);
  23769. if ((userA == NULL) || (userB == NULL)) {
  23770. ERROR_OUT(MEMORY_E, done);
  23771. }
  23772. #endif
  23773. XMEMSET(userA, 0, sizeof *userA);
  23774. XMEMSET(userB, 0, sizeof *userB);
  23775. ret = wc_ecc_init_ex(userA, HEAP_HINT, devId);
  23776. if (ret != 0)
  23777. goto done;
  23778. ret = wc_ecc_init_ex(userB, HEAP_HINT, devId);
  23779. if (ret != 0)
  23780. goto done;
  23781. ret = wc_ecc_make_key(&rng, ECC_KEYGEN_SIZE, userA);
  23782. #if defined(WOLFSSL_ASYNC_CRYPT)
  23783. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_NONE);
  23784. #endif
  23785. if (ret != 0){
  23786. ret = -10401; goto done;
  23787. }
  23788. ret = wc_ecc_make_key(&rng, ECC_KEYGEN_SIZE, userB);
  23789. #if defined(WOLFSSL_ASYNC_CRYPT)
  23790. ret = wc_AsyncWait(ret, &userB->asyncDev, WC_ASYNC_FLAG_NONE);
  23791. #endif
  23792. if (ret != 0){
  23793. ret = -10402; goto done;
  23794. }
  23795. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  23796. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  23797. !defined(HAVE_SELFTEST)
  23798. ret = wc_ecc_set_rng(userA, &rng);
  23799. if (ret != 0) {
  23800. ret = -10403; goto done;
  23801. }
  23802. ret = wc_ecc_set_rng(userB, &rng);
  23803. if (ret != 0) {
  23804. ret = -10404; goto done;
  23805. }
  23806. #endif
  23807. #if !defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3))
  23808. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  23809. #ifdef WOLFSSL_AES_128
  23810. if (ret == 0) {
  23811. ret = ecc_encrypt_e2e_test(&rng, userA, userB, ecAES_128_CBC,
  23812. ecHKDF_SHA256, ecHMAC_SHA256);
  23813. if (ret != 0) {
  23814. printf("ECIES: AES_128_CBC, HKDF_SHA256, HMAC_SHA256\n");
  23815. }
  23816. }
  23817. #endif
  23818. #ifdef WOLFSSL_AES_256
  23819. if (ret == 0) {
  23820. ret = ecc_encrypt_e2e_test(&rng, userA, userB, ecAES_256_CBC,
  23821. ecHKDF_SHA256, ecHMAC_SHA256);
  23822. if (ret != 0) {
  23823. printf("ECIES: AES_256_CBC, HKDF_SHA256, HMAC_SHA256\n");
  23824. }
  23825. }
  23826. #endif
  23827. #endif
  23828. #if !defined(NO_AES) && defined(WOLFSSL_AES_COUNTER)
  23829. #ifdef WOLFSSL_AES_128
  23830. if (ret == 0) {
  23831. ret = ecc_encrypt_e2e_test(&rng, userA, userB, ecAES_128_CTR,
  23832. ecHKDF_SHA256, ecHMAC_SHA256);
  23833. if (ret != 0) {
  23834. printf("ECIES: AES_128_CTR, HKDF_SHA256, HMAC_SHA256\n");
  23835. }
  23836. }
  23837. #endif
  23838. #ifdef WOLFSSL_AES_256
  23839. if (ret == 0) {
  23840. ret = ecc_encrypt_e2e_test(&rng, userA, userB, ecAES_256_CTR,
  23841. ecHKDF_SHA256, ecHMAC_SHA256);
  23842. if (ret != 0) {
  23843. printf("ECIES: AES_256_CTR, HKDF_SHA256, HMAC_SHA256\n");
  23844. }
  23845. }
  23846. #endif
  23847. #endif
  23848. #endif /* !HAVE_FIPS || FIPS_VERSION_GE(5,3) */
  23849. done:
  23850. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  23851. if (userA != NULL) {
  23852. wc_ecc_free(userA);
  23853. XFREE(userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23854. }
  23855. if (userB != NULL) {
  23856. wc_ecc_free(userB);
  23857. XFREE(userB, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23858. }
  23859. #else
  23860. wc_ecc_free(userB);
  23861. wc_ecc_free(userA);
  23862. #endif
  23863. wc_FreeRng(&rng);
  23864. return ret;
  23865. }
  23866. #endif /* HAVE_ECC_ENCRYPT && HAVE_AES_CBC && WOLFSSL_AES_128 */
  23867. #if defined(USE_CERT_BUFFERS_256) && !defined(WOLFSSL_ATECC508A) && \
  23868. !defined(WOLFSSL_ATECC608A) && !defined(NO_ECC256) && \
  23869. defined(HAVE_ECC_VERIFY) && defined(HAVE_ECC_SIGN) && \
  23870. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  23871. WOLFSSL_TEST_SUBROUTINE int ecc_test_buffers(void)
  23872. {
  23873. size_t bytes;
  23874. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  23875. ecc_key *cliKey = (ecc_key *)XMALLOC(sizeof *cliKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23876. ecc_key *servKey = (ecc_key *)XMALLOC(sizeof *servKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23877. ecc_key *tmpKey = (ecc_key *)XMALLOC(sizeof *tmpKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23878. #else
  23879. ecc_key cliKey[1];
  23880. ecc_key servKey[1];
  23881. ecc_key tmpKey[1];
  23882. #endif
  23883. WC_RNG rng;
  23884. word32 idx = 0;
  23885. int ret;
  23886. /* pad our test message to 32 bytes so evenly divisible by AES_BLOCK_SZ */
  23887. byte in[] = "Everyone gets Friday off. ecc p";
  23888. word32 inLen = (word32)XSTRLEN((char*)in);
  23889. byte out[256];
  23890. byte plain[256];
  23891. int verify = 0;
  23892. word32 x;
  23893. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  23894. if ((cliKey == NULL) || (servKey == NULL) || (tmpKey == NULL))
  23895. ERROR_OUT(MEMORY_E, done);
  23896. #endif
  23897. ret = wc_ecc_init_ex(cliKey, HEAP_HINT, devId);
  23898. if (ret != 0)
  23899. ERROR_OUT(-10420, done);
  23900. ret = wc_ecc_init_ex(servKey, HEAP_HINT, devId);
  23901. if (ret != 0)
  23902. ERROR_OUT(-10421, done);
  23903. ret = wc_ecc_init_ex(tmpKey, HEAP_HINT, devId);
  23904. if (ret != 0)
  23905. ERROR_OUT(-10421, done);
  23906. bytes = (size_t)sizeof_ecc_clikey_der_256;
  23907. /* place client key into ecc_key struct cliKey */
  23908. ret = wc_EccPrivateKeyDecode(ecc_clikey_der_256, &idx, cliKey,
  23909. (word32)bytes);
  23910. if (ret != 0)
  23911. ERROR_OUT(-10422, done);
  23912. idx = 0;
  23913. bytes = (size_t)sizeof_ecc_key_der_256;
  23914. /* place server key into ecc_key struct servKey */
  23915. ret = wc_EccPrivateKeyDecode(ecc_key_der_256, &idx, servKey,
  23916. (word32)bytes);
  23917. if (ret != 0)
  23918. ERROR_OUT(-10423, done);
  23919. #ifndef WC_NO_RNG
  23920. #ifndef HAVE_FIPS
  23921. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  23922. #else
  23923. ret = wc_InitRng(&rng);
  23924. #endif
  23925. if (ret != 0)
  23926. ERROR_OUT(-10424, done);
  23927. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  23928. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  23929. !defined(HAVE_SELFTEST)
  23930. ret = wc_ecc_set_rng(cliKey, &rng);
  23931. if (ret != 0) {
  23932. ERROR_OUT(-10425, done);
  23933. }
  23934. ret = wc_ecc_set_rng(servKey, &rng);
  23935. if (ret != 0) {
  23936. ERROR_OUT(-10425, done);
  23937. }
  23938. #endif
  23939. #endif /* !WC_NO_RNG */
  23940. #if defined(HAVE_ECC_ENCRYPT) && defined(HAVE_HKDF) && \
  23941. defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  23942. {
  23943. word32 y;
  23944. /* test encrypt and decrypt if they're available */
  23945. x = sizeof(out);
  23946. ret = wc_ecc_encrypt(cliKey, servKey, in, sizeof(in), out, &x, NULL);
  23947. if (ret < 0)
  23948. ERROR_OUT(-10426, done);
  23949. #ifdef WOLFSSL_ECIES_OLD
  23950. tmpKey->dp = cliKey->dp;
  23951. ret = wc_ecc_copy_point(&cliKey->pubkey, &tmpKey->pubkey);
  23952. if (ret != 0) {
  23953. ret = -10414; goto done;
  23954. }
  23955. #endif
  23956. y = sizeof(plain);
  23957. ret = wc_ecc_decrypt(servKey, tmpKey, out, x, plain, &y, NULL);
  23958. if (ret < 0)
  23959. ERROR_OUT(-10427, done);
  23960. if (XMEMCMP(plain, in, inLen))
  23961. ERROR_OUT(-10428, done);
  23962. }
  23963. #endif
  23964. x = sizeof(out);
  23965. do {
  23966. #if defined(WOLFSSL_ASYNC_CRYPT)
  23967. ret = wc_AsyncWait(ret, &cliKey->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  23968. #endif
  23969. if (ret == 0)
  23970. ret = wc_ecc_sign_hash(in, inLen, out, &x, &rng, cliKey);
  23971. } while (ret == WC_PENDING_E);
  23972. if (ret < 0)
  23973. ERROR_OUT(-10429, done);
  23974. TEST_SLEEP();
  23975. XMEMSET(plain, 0, sizeof(plain));
  23976. do {
  23977. #if defined(WOLFSSL_ASYNC_CRYPT)
  23978. ret = wc_AsyncWait(ret, &cliKey->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  23979. #endif
  23980. if (ret == 0)
  23981. ret = wc_ecc_verify_hash(out, x, in, inLen, &verify,
  23982. cliKey);
  23983. } while (ret == WC_PENDING_E);
  23984. if (ret < 0)
  23985. ERROR_OUT(-10430, done);
  23986. if (verify != 1)
  23987. ERROR_OUT(-10431, done);
  23988. TEST_SLEEP();
  23989. #ifdef WOLFSSL_CERT_EXT
  23990. idx = 0;
  23991. bytes = sizeof_ecc_clikeypub_der_256;
  23992. ret = wc_EccPublicKeyDecode(ecc_clikeypub_der_256, &idx, cliKey,
  23993. (word32) bytes);
  23994. if (ret != 0)
  23995. ERROR_OUT(-10432, done);
  23996. #endif
  23997. ret = 0;
  23998. done:
  23999. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  24000. if (cliKey != NULL) {
  24001. wc_ecc_free(cliKey);
  24002. XFREE(cliKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24003. }
  24004. if (servKey != NULL) {
  24005. wc_ecc_free(servKey);
  24006. XFREE(servKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24007. }
  24008. if (tmpKey != NULL) {
  24009. wc_ecc_free(tmpKey);
  24010. XFREE(tmpKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24011. }
  24012. #else
  24013. wc_ecc_free(cliKey);
  24014. wc_ecc_free(servKey);
  24015. wc_ecc_free(tmpKey);
  24016. #endif
  24017. wc_FreeRng(&rng);
  24018. return ret;
  24019. }
  24020. #endif /* USE_CERT_BUFFERS_256 && !WOLFSSL_ATECCX08A && !NO_ECC256 */
  24021. #endif /* HAVE_ECC */
  24022. #ifdef HAVE_CURVE25519
  24023. #if defined(HAVE_CURVE25519_SHARED_SECRET) && \
  24024. defined(HAVE_CURVE25519_KEY_IMPORT)
  24025. #ifdef CURVE25519_OVERFLOW_ALL_TESTS
  24026. #define X25519_TEST_CNT 5
  24027. #else
  24028. #define X25519_TEST_CNT 1
  24029. #endif
  24030. static int curve25519_overflow_test(void)
  24031. {
  24032. /* secret key for party a */
  24033. byte sa[X25519_TEST_CNT][32] = {
  24034. {
  24035. 0x8d,0xaf,0x6e,0x7a,0xc1,0xeb,0x8d,0x30,
  24036. 0x99,0x86,0xd3,0x90,0x47,0x96,0x21,0x3c,
  24037. 0x3a,0x75,0xc0,0x7b,0x75,0x01,0x75,0xa3,
  24038. 0x81,0x4b,0xff,0x5a,0xbc,0x96,0x87,0x28
  24039. },
  24040. #ifdef CURVE25519_OVERFLOW_ALL_TESTS
  24041. {
  24042. 0x9d,0x63,0x5f,0xce,0xe2,0xe8,0xd7,0xfb,
  24043. 0x68,0x77,0x0e,0x44,0xd1,0xad,0x87,0x2b,
  24044. 0xf4,0x65,0x06,0xb7,0xbb,0xdb,0xbe,0x6e,
  24045. 0x02,0x43,0x24,0xc7,0x3d,0x7b,0x88,0x60
  24046. },
  24047. {
  24048. 0x63,0xbf,0x76,0xa9,0x73,0xa0,0x09,0xb9,
  24049. 0xcc,0xc9,0x4d,0x47,0x2d,0x14,0x0e,0x52,
  24050. 0xa3,0x84,0x55,0xb8,0x7c,0xdb,0xce,0xb1,
  24051. 0xe4,0x5b,0x8a,0xb9,0x30,0xf1,0xa4,0xa0
  24052. },
  24053. {
  24054. 0x63,0xbf,0x76,0xa9,0x73,0xa0,0x09,0xb9,
  24055. 0xcc,0xc9,0x4d,0x47,0x2d,0x14,0x0e,0x52,
  24056. 0xa3,0x84,0x55,0xb8,0x7c,0xdb,0xce,0xb1,
  24057. 0xe4,0x5b,0x8a,0xb9,0x30,0xf1,0xa4,0xa0
  24058. },
  24059. {
  24060. 0x63,0xbf,0x76,0xa9,0x73,0xa0,0x09,0xb9,
  24061. 0xcc,0xc9,0x4d,0x47,0x2d,0x14,0x0e,0x52,
  24062. 0xa3,0x84,0x55,0xb8,0x7c,0xdb,0xce,0xb1,
  24063. 0xe4,0x5b,0x8a,0xb9,0x30,0xf1,0xa4,0xa0
  24064. }
  24065. #endif
  24066. };
  24067. /* public key for party b */
  24068. byte pb[X25519_TEST_CNT][32] = {
  24069. {
  24070. 0x7f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  24071. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  24072. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  24073. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xf0
  24074. },
  24075. #ifdef CURVE25519_OVERFLOW_ALL_TESTS
  24076. {
  24077. /* 0xff first byte in original - invalid! */
  24078. 0x7f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  24079. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  24080. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  24081. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xf0
  24082. },
  24083. {
  24084. 0x36,0x1a,0x74,0x87,0x28,0x59,0xe0,0xb6,
  24085. 0xe4,0x2b,0x17,0x9b,0x16,0xb0,0x3b,0xf8,
  24086. 0xb8,0x9f,0x2a,0x8f,0xc5,0x33,0x68,0x4f,
  24087. 0xde,0x4d,0xd8,0x80,0x63,0xe7,0xb4,0x0a
  24088. },
  24089. {
  24090. 0x00,0x80,0x38,0x59,0x19,0x3a,0x66,0x12,
  24091. 0xfd,0xa1,0xec,0x1c,0x40,0x84,0x40,0xbd,
  24092. 0x64,0x10,0x8b,0x53,0x81,0x21,0x03,0x2d,
  24093. 0x7d,0x33,0xb4,0x01,0x57,0x0d,0xe1,0x89
  24094. },
  24095. {
  24096. 0x1d,0xf8,0xf8,0x33,0x89,0x6c,0xb7,0xba,
  24097. 0x94,0x73,0xfa,0xc2,0x36,0xac,0xbe,0x49,
  24098. 0xaf,0x85,0x3e,0x93,0x5f,0xae,0xb2,0xc0,
  24099. 0xc8,0x80,0x8f,0x4a,0xaa,0xd3,0x55,0x2b
  24100. }
  24101. #endif
  24102. };
  24103. /* expected shared key */
  24104. byte ss[X25519_TEST_CNT][32] = {
  24105. {
  24106. 0x5c,0x4c,0x85,0x5f,0xfb,0x20,0x38,0xcc,
  24107. 0x55,0x16,0x5b,0x8a,0xa7,0xed,0x57,0x6e,
  24108. 0x35,0xaa,0x71,0x67,0x85,0x1f,0xb6,0x28,
  24109. 0x17,0x07,0x7b,0xda,0x76,0xdd,0xe0,0xb4
  24110. },
  24111. #ifdef CURVE25519_OVERFLOW_ALL_TESTS
  24112. {
  24113. 0x33,0xf6,0xc1,0x34,0x62,0x92,0x06,0x02,
  24114. 0x95,0xdb,0x91,0x4c,0x5d,0x52,0x54,0xc7,
  24115. 0xd2,0x5b,0x24,0xb5,0x4f,0x33,0x59,0x79,
  24116. 0x9f,0x6d,0x7e,0x4a,0x4c,0x30,0xd6,0x38
  24117. },
  24118. {
  24119. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24120. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24121. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24122. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x02
  24123. },
  24124. {
  24125. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  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,0x09
  24129. },
  24130. {
  24131. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  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,0x10
  24135. }
  24136. #endif
  24137. };
  24138. int ret = 0;
  24139. int i;
  24140. word32 y;
  24141. byte shared[32];
  24142. curve25519_key userA;
  24143. wc_curve25519_init_ex(&userA, HEAP_HINT, devId);
  24144. for (i = 0; i < X25519_TEST_CNT; i++) {
  24145. if (wc_curve25519_import_private_raw(sa[i], sizeof(sa[i]), pb[i],
  24146. sizeof(pb[i]), &userA) != 0) {
  24147. ret = -10500 - i; break;
  24148. }
  24149. /* test against known test vector */
  24150. XMEMSET(shared, 0, sizeof(shared));
  24151. y = sizeof(shared);
  24152. if (wc_curve25519_shared_secret(&userA, &userA, shared, &y) != 0) {
  24153. ret = -10510 - i; break;
  24154. }
  24155. if (XMEMCMP(ss[i], shared, y)) {
  24156. ret = -10520 - i; break;
  24157. }
  24158. }
  24159. wc_curve25519_free(&userA);
  24160. return ret;
  24161. }
  24162. /* Test the wc_curve25519_check_public API.
  24163. *
  24164. * returns 0 on success and -ve on failure.
  24165. */
  24166. static int curve25519_check_public_test(void)
  24167. {
  24168. /* Little-endian values that will fail */
  24169. byte fail_le[][CURVE25519_KEYSIZE] = {
  24170. {
  24171. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24172. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24173. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24174. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  24175. },
  24176. {
  24177. 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  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. },
  24182. {
  24183. 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24184. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24185. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24186. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x81
  24187. },
  24188. };
  24189. /* Big-endian values that will fail */
  24190. byte fail_be[][CURVE25519_KEYSIZE] = {
  24191. {
  24192. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24193. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24194. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24195. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  24196. },
  24197. {
  24198. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  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,0x01
  24202. },
  24203. {
  24204. 0x81,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  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,0x01
  24208. },
  24209. };
  24210. /* Good or valid public value */
  24211. byte good[CURVE25519_KEYSIZE] = {
  24212. 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24213. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24214. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24215. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  24216. };
  24217. int i;
  24218. /* Parameter checks */
  24219. /* NULL pointer */
  24220. if (wc_curve25519_check_public(NULL, 0, EC25519_LITTLE_ENDIAN) !=
  24221. BAD_FUNC_ARG) {
  24222. return -10600;
  24223. }
  24224. if (wc_curve25519_check_public(NULL, 0, EC25519_BIG_ENDIAN) !=
  24225. BAD_FUNC_ARG) {
  24226. return -10601;
  24227. }
  24228. /* Length of 0 treated differently to other invalid lengths for TLS */
  24229. if (wc_curve25519_check_public(good, 0, EC25519_LITTLE_ENDIAN) != BUFFER_E)
  24230. return -10602;
  24231. if (wc_curve25519_check_public(good, 0, EC25519_BIG_ENDIAN) != BUFFER_E)
  24232. return -10603;
  24233. /* Length not CURVE25519_KEYSIZE */
  24234. for (i = 1; i < CURVE25519_KEYSIZE + 2; i++) {
  24235. if (i == CURVE25519_KEYSIZE)
  24236. continue;
  24237. if (wc_curve25519_check_public(good, i, EC25519_LITTLE_ENDIAN) !=
  24238. ECC_BAD_ARG_E) {
  24239. return -10604 - i;
  24240. }
  24241. if (wc_curve25519_check_public(good, i, EC25519_BIG_ENDIAN) !=
  24242. ECC_BAD_ARG_E) {
  24243. return -10614 - i;
  24244. }
  24245. }
  24246. /* Little-endian fail cases */
  24247. for (i = 0; i < (int)(sizeof(fail_le) / sizeof(*fail_le)); i++) {
  24248. if (wc_curve25519_check_public(fail_le[i], CURVE25519_KEYSIZE,
  24249. EC25519_LITTLE_ENDIAN) == 0) {
  24250. return -10624 - i;
  24251. }
  24252. }
  24253. /* Big-endian fail cases */
  24254. for (i = 0; i < (int)(sizeof(fail_be) / sizeof(*fail_be)); i++) {
  24255. if (wc_curve25519_check_public(fail_be[i], CURVE25519_KEYSIZE,
  24256. EC25519_BIG_ENDIAN) == 0) {
  24257. return -10634 - i;
  24258. }
  24259. }
  24260. /* Check a valid public value works! */
  24261. if (wc_curve25519_check_public(good, CURVE25519_KEYSIZE,
  24262. EC25519_LITTLE_ENDIAN) != 0) {
  24263. return -10644;
  24264. }
  24265. if (wc_curve25519_check_public(good, CURVE25519_KEYSIZE,
  24266. EC25519_BIG_ENDIAN) != 0) {
  24267. return -10645;
  24268. }
  24269. return 0;
  24270. }
  24271. #endif /* HAVE_CURVE25519_SHARED_SECRET && HAVE_CURVE25519_KEY_IMPORT */
  24272. #if !defined(NO_ASN) && defined(HAVE_CURVE25519_KEY_EXPORT) && \
  24273. defined(HAVE_CURVE25519_KEY_IMPORT)
  24274. static int curve255519_der_test(void)
  24275. {
  24276. int ret = 0;
  24277. /* certs/statickeys/x25519.der */
  24278. const byte kCurve25519PrivDer[] = {
  24279. 0x30, 0x2E, 0x02, 0x01, 0x00, 0x30, 0x05, 0x06, 0x03, 0x2B, 0x65, 0x6E,
  24280. 0x04, 0x22, 0x04, 0x20, 0x78, 0x8E, 0x31, 0x5C, 0x33, 0xA9, 0x19, 0xC0,
  24281. 0x5E, 0x36, 0x70, 0x1B, 0xA4, 0xE8, 0xEF, 0xC1, 0x89, 0x8C, 0xB3, 0x15,
  24282. 0xC6, 0x79, 0xD3, 0xAC, 0x22, 0x00, 0xAE, 0xFA, 0xB3, 0xB7, 0x0F, 0x78
  24283. };
  24284. /* certs/statickeys/x25519-pub.der */
  24285. const byte kCurve25519PubDer[] = {
  24286. 0x30, 0x2A, 0x30, 0x05, 0x06, 0x03, 0x2B, 0x65, 0x6E, 0x03, 0x21, 0x00,
  24287. 0x09, 0xBC, 0x8C, 0xC7, 0x45, 0x0D, 0xC1, 0xC2, 0x02, 0x57, 0x9A, 0x68,
  24288. 0x3A, 0xFD, 0x7A, 0xA8, 0xA5, 0x2F, 0xF0, 0x99, 0x39, 0x98, 0xEA, 0x26,
  24289. 0xA2, 0x5B, 0x38, 0xFD, 0x96, 0xDB, 0x2A, 0x26
  24290. };
  24291. curve25519_key key;
  24292. byte output[128];
  24293. word32 outputSz = 128;
  24294. word32 idx;
  24295. if (wc_curve25519_init_ex(&key, HEAP_HINT, devId) != 0) {
  24296. return -10723;
  24297. }
  24298. /* Test decode / encode of Curve25519 private key only */
  24299. if (ret == 0) {
  24300. idx = 0;
  24301. ret = wc_Curve25519PrivateKeyDecode(kCurve25519PrivDer, &idx, &key,
  24302. (word32)sizeof(kCurve25519PrivDer));
  24303. }
  24304. if (ret == 0) {
  24305. outputSz = (word32)sizeof(output);
  24306. ret = wc_Curve25519PrivateKeyToDer(&key, output, outputSz);
  24307. if (ret >= 0) {
  24308. outputSz = ret;
  24309. ret = 0;
  24310. }
  24311. else {
  24312. ret = -10724;
  24313. }
  24314. }
  24315. if (ret == 0 && (outputSz != (word32)sizeof(kCurve25519PrivDer) ||
  24316. XMEMCMP(output, kCurve25519PrivDer, outputSz) != 0)) {
  24317. ret = -10725;
  24318. }
  24319. /* Test decode / encode of Curve25519 public key only */
  24320. if (ret == 0) {
  24321. idx = 0;
  24322. ret = wc_Curve25519PublicKeyDecode(kCurve25519PubDer, &idx, &key,
  24323. (word32)sizeof(kCurve25519PubDer));
  24324. }
  24325. if (ret == 0) {
  24326. outputSz = (word32)sizeof(output);
  24327. ret = wc_Curve25519PublicKeyToDer(&key, output, outputSz, 1);
  24328. if (ret >= 0) {
  24329. outputSz = ret;
  24330. ret = 0;
  24331. }
  24332. else {
  24333. ret = -10726;
  24334. }
  24335. }
  24336. if (ret == 0 && (outputSz != (word32)sizeof(kCurve25519PubDer) ||
  24337. XMEMCMP(output, kCurve25519PubDer, outputSz) != 0)) {
  24338. ret = -10727;
  24339. }
  24340. wc_curve25519_free(&key);
  24341. return ret;
  24342. }
  24343. #endif /* !NO_ASN && HAVE_CURVE25519_KEY_EXPORT && HAVE_CURVE25519_KEY_IMPORT */
  24344. WOLFSSL_TEST_SUBROUTINE int curve25519_test(void)
  24345. {
  24346. WC_RNG rng;
  24347. int ret;
  24348. #ifdef HAVE_CURVE25519_SHARED_SECRET
  24349. byte sharedA[32];
  24350. byte sharedB[32];
  24351. word32 y;
  24352. #endif
  24353. #ifdef HAVE_CURVE25519_KEY_EXPORT
  24354. byte exportBuf[32];
  24355. #endif
  24356. word32 x = 0;
  24357. curve25519_key userA, userB, pubKey;
  24358. #if defined(HAVE_CURVE25519_SHARED_SECRET) && \
  24359. defined(HAVE_CURVE25519_KEY_IMPORT)
  24360. /* test vectors from
  24361. https://tools.ietf.org/html/draft-josefsson-tls-curve25519-03
  24362. */
  24363. /* secret key for party a */
  24364. byte sa[] = {
  24365. 0x5A,0xC9,0x9F,0x33,0x63,0x2E,0x5A,0x76,
  24366. 0x8D,0xE7,0xE8,0x1B,0xF8,0x54,0xC2,0x7C,
  24367. 0x46,0xE3,0xFB,0xF2,0xAB,0xBA,0xCD,0x29,
  24368. 0xEC,0x4A,0xFF,0x51,0x73,0x69,0xC6,0x60
  24369. };
  24370. /* public key for party a */
  24371. byte pa[] = {
  24372. 0x05,0x7E,0x23,0xEA,0x9F,0x1C,0xBE,0x8A,
  24373. 0x27,0x16,0x8F,0x6E,0x69,0x6A,0x79,0x1D,
  24374. 0xE6,0x1D,0xD3,0xAF,0x7A,0xCD,0x4E,0xEA,
  24375. 0xCC,0x6E,0x7B,0xA5,0x14,0xFD,0xA8,0x63
  24376. };
  24377. /* secret key for party b */
  24378. byte sb[] = {
  24379. 0x47,0xDC,0x3D,0x21,0x41,0x74,0x82,0x0E,
  24380. 0x11,0x54,0xB4,0x9B,0xC6,0xCD,0xB2,0xAB,
  24381. 0xD4,0x5E,0xE9,0x58,0x17,0x05,0x5D,0x25,
  24382. 0x5A,0xA3,0x58,0x31,0xB7,0x0D,0x32,0x60
  24383. };
  24384. /* public key for party b */
  24385. byte pb[] = {
  24386. 0x6E,0xB8,0x9D,0xA9,0x19,0x89,0xAE,0x37,
  24387. 0xC7,0xEA,0xC7,0x61,0x8D,0x9E,0x5C,0x49,
  24388. 0x51,0xDB,0xA1,0xD7,0x3C,0x28,0x5A,0xE1,
  24389. 0xCD,0x26,0xA8,0x55,0x02,0x0E,0xEF,0x04
  24390. };
  24391. /* expected shared key */
  24392. byte ss[] = {
  24393. 0x61,0x45,0x0C,0xD9,0x8E,0x36,0x01,0x6B,
  24394. 0x58,0x77,0x6A,0x89,0x7A,0x9F,0x0A,0xEF,
  24395. 0x73,0x8B,0x99,0xF0,0x94,0x68,0xB8,0xD6,
  24396. 0xB8,0x51,0x11,0x84,0xD5,0x34,0x94,0xAB
  24397. };
  24398. #endif /* HAVE_CURVE25519_SHARED_SECRET */
  24399. (void)x;
  24400. #ifndef HAVE_FIPS
  24401. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  24402. #else
  24403. ret = wc_InitRng(&rng);
  24404. #endif
  24405. if (ret != 0)
  24406. return -10700;
  24407. wc_curve25519_init_ex(&userA, HEAP_HINT, devId);
  24408. wc_curve25519_init_ex(&userB, HEAP_HINT, devId);
  24409. wc_curve25519_init_ex(&pubKey, HEAP_HINT, devId);
  24410. /* make curve25519 keys */
  24411. if (wc_curve25519_make_key(&rng, 32, &userA) != 0)
  24412. return -10701;
  24413. if (wc_curve25519_make_key(&rng, 32, &userB) != 0)
  24414. return -10702;
  24415. #ifdef HAVE_CURVE25519_SHARED_SECRET
  24416. /* find shared secret key */
  24417. x = sizeof(sharedA);
  24418. if ((ret = wc_curve25519_shared_secret(&userA, &userB, sharedA, &x)) != 0) {
  24419. printf("wc_curve25519_shared_secret 1 %d\n", ret);
  24420. return -10703;
  24421. }
  24422. y = sizeof(sharedB);
  24423. if ((ret = wc_curve25519_shared_secret(&userB, &userA, sharedB, &y)) != 0) {
  24424. printf("wc_curve25519_shared_secret 2 %d\n", ret);
  24425. return -10704;
  24426. }
  24427. /* compare shared secret keys to test they are the same */
  24428. if (y != x)
  24429. return -10705;
  24430. if (XMEMCMP(sharedA, sharedB, x))
  24431. return -10706;
  24432. #endif
  24433. #ifdef HAVE_CURVE25519_KEY_EXPORT
  24434. /* export a public key and import it for another user */
  24435. x = sizeof(exportBuf);
  24436. if (wc_curve25519_export_public(&userA, exportBuf, &x) != 0)
  24437. return -10707;
  24438. #ifdef HAVE_CURVE25519_KEY_IMPORT
  24439. if (wc_curve25519_import_public(exportBuf, x, &pubKey) != 0)
  24440. return -10708;
  24441. #endif
  24442. #endif
  24443. #if defined(HAVE_CURVE25519_SHARED_SECRET) && \
  24444. defined(HAVE_CURVE25519_KEY_IMPORT)
  24445. /* test shared key after importing a public key */
  24446. XMEMSET(sharedB, 0, sizeof(sharedB));
  24447. y = sizeof(sharedB);
  24448. if (wc_curve25519_shared_secret(&userB, &pubKey, sharedB, &y) != 0)
  24449. return -10709;
  24450. if (XMEMCMP(sharedA, sharedB, y))
  24451. return -10710;
  24452. /* import RFC test vectors and compare shared key */
  24453. if (wc_curve25519_import_private_raw(sa, sizeof(sa), pa, sizeof(pa), &userA)
  24454. != 0)
  24455. return -10711;
  24456. if (wc_curve25519_import_private_raw(sb, sizeof(sb), pb, sizeof(pb), &userB)
  24457. != 0)
  24458. return -10712;
  24459. /* test against known test vector */
  24460. XMEMSET(sharedB, 0, sizeof(sharedB));
  24461. y = sizeof(sharedB);
  24462. if (wc_curve25519_shared_secret(&userA, &userB, sharedB, &y) != 0)
  24463. return -10713;
  24464. if (XMEMCMP(ss, sharedB, y))
  24465. return -10714;
  24466. /* test swapping roles of keys and generating same shared key */
  24467. XMEMSET(sharedB, 0, sizeof(sharedB));
  24468. y = sizeof(sharedB);
  24469. if (wc_curve25519_shared_secret(&userB, &userA, sharedB, &y) != 0)
  24470. return -10715;
  24471. if (XMEMCMP(ss, sharedB, y))
  24472. return -10716;
  24473. /* test with 1 generated key and 1 from known test vector */
  24474. if (wc_curve25519_import_private_raw(sa, sizeof(sa), pa, sizeof(pa), &userA)
  24475. != 0)
  24476. return -10717;
  24477. wc_curve25519_free(&userB);
  24478. wc_curve25519_init_ex(&userB, HEAP_HINT, devId);
  24479. if (wc_curve25519_make_key(&rng, 32, &userB) != 0)
  24480. return -10718;
  24481. x = sizeof(sharedA);
  24482. if (wc_curve25519_shared_secret(&userA, &userB, sharedA, &x) != 0)
  24483. return -10719;
  24484. y = sizeof(sharedB);
  24485. if (wc_curve25519_shared_secret(&userB, &userA, sharedB, &y) != 0)
  24486. return -10720;
  24487. /* compare shared secret keys to test they are the same */
  24488. if (y != x)
  24489. return -10721;
  24490. if (XMEMCMP(sharedA, sharedB, x))
  24491. return -10722;
  24492. ret = curve25519_overflow_test();
  24493. if (ret != 0)
  24494. return ret;
  24495. ret = curve25519_check_public_test();
  24496. if (ret != 0)
  24497. return ret;
  24498. #endif /* HAVE_CURVE25519_SHARED_SECRET && HAVE_CURVE25519_KEY_IMPORT */
  24499. #if !defined(NO_ASN) && defined(HAVE_CURVE25519_KEY_EXPORT) && \
  24500. defined(HAVE_CURVE25519_KEY_IMPORT)
  24501. ret = curve255519_der_test();
  24502. if (ret != 0)
  24503. return ret;
  24504. #endif
  24505. /* clean up keys when done */
  24506. wc_curve25519_free(&pubKey);
  24507. wc_curve25519_free(&userB);
  24508. wc_curve25519_free(&userA);
  24509. wc_FreeRng(&rng);
  24510. return 0;
  24511. }
  24512. #endif /* HAVE_CURVE25519 */
  24513. #ifdef HAVE_ED25519
  24514. #ifdef WOLFSSL_TEST_CERT
  24515. static int ed25519_test_cert(void)
  24516. {
  24517. DecodedCert cert[2];
  24518. DecodedCert* serverCert = NULL;
  24519. DecodedCert* caCert = NULL;
  24520. #ifdef HAVE_ED25519_VERIFY
  24521. ed25519_key key;
  24522. ed25519_key* pubKey = NULL;
  24523. int verify;
  24524. #endif /* HAVE_ED25519_VERIFY */
  24525. int ret;
  24526. byte* tmp;
  24527. size_t bytes;
  24528. XFILE file;
  24529. tmp = (byte *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24530. if (tmp == NULL) {
  24531. ERROR_OUT(-10730, done);
  24532. }
  24533. #ifdef USE_CERT_BUFFERS_256
  24534. XMEMCPY(tmp, ca_ed25519_cert, sizeof_ca_ed25519_cert);
  24535. bytes = sizeof_ca_ed25519_cert;
  24536. #elif !defined(NO_FILESYSTEM)
  24537. file = XFOPEN(caEd25519Cert, "rb");
  24538. if (file == NULL) {
  24539. ERROR_OUT(-10731, done);
  24540. }
  24541. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  24542. XFCLOSE(file);
  24543. #else
  24544. /* No certificate to use. */
  24545. ERROR_OUT(-10732, done);
  24546. #endif
  24547. InitDecodedCert(&cert[0], tmp, (word32)bytes, 0);
  24548. caCert = &cert[0];
  24549. ret = ParseCert(caCert, CERT_TYPE, NO_VERIFY, NULL);
  24550. if (ret != 0) {
  24551. ERROR_OUT(-10733, done);
  24552. }
  24553. #ifdef USE_CERT_BUFFERS_256
  24554. XMEMCPY(tmp, server_ed25519_cert, sizeof_server_ed25519_cert);
  24555. bytes = sizeof_server_ed25519_cert;
  24556. #elif !defined(NO_FILESYSTEM)
  24557. file = XFOPEN(serverEd25519Cert, "rb");
  24558. if (file == NULL) {
  24559. ERROR_OUT(-10734, done);
  24560. }
  24561. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  24562. XFCLOSE(file);
  24563. #else
  24564. /* No certificate to use. */
  24565. ERROR_OUT(-10735, done);
  24566. #endif
  24567. InitDecodedCert(&cert[1], tmp, (word32)bytes, 0);
  24568. serverCert = &cert[1];
  24569. ret = ParseCert(serverCert, CERT_TYPE, NO_VERIFY, NULL);
  24570. if (ret != 0) {
  24571. ERROR_OUT(-10736, done);
  24572. }
  24573. #ifdef HAVE_ED25519_VERIFY
  24574. ret = wc_ed25519_init(&key);
  24575. if (ret < 0) {
  24576. ERROR_OUT(-10737, done);
  24577. }
  24578. pubKey = &key;
  24579. ret = wc_ed25519_import_public(caCert->publicKey, caCert->pubKeySize,
  24580. pubKey);
  24581. if (ret < 0) {
  24582. ERROR_OUT(-10738, done);
  24583. }
  24584. if (wc_ed25519_verify_msg(serverCert->signature, serverCert->sigLength,
  24585. serverCert->source + serverCert->certBegin,
  24586. serverCert->sigIndex - serverCert->certBegin,
  24587. &verify, pubKey) < 0 || verify != 1) {
  24588. ERROR_OUT(-10739, done);
  24589. }
  24590. #endif /* HAVE_ED25519_VERIFY */
  24591. done:
  24592. if (tmp != NULL)
  24593. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24594. #ifdef HAVE_ED25519_VERIFY
  24595. wc_ed25519_free(pubKey);
  24596. #endif /* HAVE_ED25519_VERIFY */
  24597. if (caCert != NULL)
  24598. FreeDecodedCert(caCert);
  24599. if (serverCert != NULL)
  24600. FreeDecodedCert(serverCert);
  24601. return ret;
  24602. }
  24603. static int ed25519_test_make_cert(void)
  24604. {
  24605. WC_RNG rng;
  24606. Cert cert;
  24607. DecodedCert decode;
  24608. ed25519_key key;
  24609. ed25519_key* privKey = NULL;
  24610. int ret = 0;
  24611. byte* tmp = NULL;
  24612. wc_InitCert_ex(&cert, HEAP_HINT, devId);
  24613. #ifndef HAVE_FIPS
  24614. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  24615. #else
  24616. ret = wc_InitRng(&rng);
  24617. #endif
  24618. if (ret != 0)
  24619. return -10750;
  24620. wc_ed25519_init(&key);
  24621. privKey = &key;
  24622. wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, privKey);
  24623. cert.daysValid = 365 * 2;
  24624. cert.selfSigned = 1;
  24625. XMEMCPY(&cert.issuer, &certDefaultName, sizeof(CertName));
  24626. XMEMCPY(&cert.subject, &certDefaultName, sizeof(CertName));
  24627. cert.isCA = 0;
  24628. #ifdef WOLFSSL_CERT_EXT
  24629. ret = wc_SetKeyUsage(&cert, certKeyUsage);
  24630. if (ret < 0) {
  24631. ERROR_OUT(-10751, done);
  24632. }
  24633. ret = wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ED25519_TYPE, privKey);
  24634. if (ret < 0) {
  24635. ERROR_OUT(-10752, done);
  24636. }
  24637. ret = wc_SetAuthKeyIdFromPublicKey_ex(&cert, ED25519_TYPE, privKey);
  24638. if (ret < 0) {
  24639. ERROR_OUT(-10753, done);
  24640. }
  24641. #endif
  24642. tmp = (byte *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24643. if (tmp == NULL) {
  24644. ERROR_OUT(-10754, done);
  24645. }
  24646. cert.sigType = CTC_ED25519;
  24647. ret = wc_MakeCert_ex(&cert, tmp, FOURK_BUF, ED25519_TYPE, privKey, &rng);
  24648. if (ret < 0) {
  24649. ERROR_OUT(-10755, done);
  24650. }
  24651. ret = wc_SignCert_ex(cert.bodySz, cert.sigType, tmp, FOURK_BUF,
  24652. ED25519_TYPE, privKey, &rng);
  24653. if (ret < 0) {
  24654. ERROR_OUT(-10756, done);
  24655. }
  24656. InitDecodedCert(&decode, tmp, ret, HEAP_HINT);
  24657. ret = ParseCert(&decode, CERT_TYPE, NO_VERIFY, 0);
  24658. FreeDecodedCert(&decode);
  24659. if (ret != 0) {
  24660. ERROR_OUT(-10757, done);
  24661. }
  24662. done:
  24663. if (tmp != NULL)
  24664. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24665. wc_ed25519_free(privKey);
  24666. wc_FreeRng(&rng);
  24667. return ret;
  24668. }
  24669. #endif /* WOLFSSL_TEST_CERT */
  24670. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_EXPORT) && \
  24671. defined(HAVE_ED25519_KEY_IMPORT)
  24672. static int ed25519ctx_test(void)
  24673. {
  24674. int ret;
  24675. byte out[ED25519_SIG_SIZE];
  24676. word32 outlen;
  24677. #ifdef HAVE_ED25519_VERIFY
  24678. int verify = 0;
  24679. #endif /* HAVE_ED25519_VERIFY */
  24680. ed25519_key key;
  24681. WOLFSSL_SMALL_STACK_STATIC const byte sKeyCtx[] = {
  24682. 0x03,0x05,0x33,0x4e,0x38,0x1a,0xf7,0x8f,
  24683. 0x14,0x1c,0xb6,0x66,0xf6,0x19,0x9f,0x57,
  24684. 0xbc,0x34,0x95,0x33,0x5a,0x25,0x6a,0x95,
  24685. 0xbd,0x2a,0x55,0xbf,0x54,0x66,0x63,0xf6
  24686. };
  24687. WOLFSSL_SMALL_STACK_STATIC const byte pKeyCtx[] = {
  24688. 0xdf,0xc9,0x42,0x5e,0x4f,0x96,0x8f,0x7f,
  24689. 0x0c,0x29,0xf0,0x25,0x9c,0xf5,0xf9,0xae,
  24690. 0xd6,0x85,0x1c,0x2b,0xb4,0xad,0x8b,0xfb,
  24691. 0x86,0x0c,0xfe,0xe0,0xab,0x24,0x82,0x92
  24692. };
  24693. WOLFSSL_SMALL_STACK_STATIC const byte sigCtx1[] = {
  24694. 0x55,0xa4,0xcc,0x2f,0x70,0xa5,0x4e,0x04,
  24695. 0x28,0x8c,0x5f,0x4c,0xd1,0xe4,0x5a,0x7b,
  24696. 0xb5,0x20,0xb3,0x62,0x92,0x91,0x18,0x76,
  24697. 0xca,0xda,0x73,0x23,0x19,0x8d,0xd8,0x7a,
  24698. 0x8b,0x36,0x95,0x0b,0x95,0x13,0x00,0x22,
  24699. 0x90,0x7a,0x7f,0xb7,0xc4,0xe9,0xb2,0xd5,
  24700. 0xf6,0xcc,0xa6,0x85,0xa5,0x87,0xb4,0xb2,
  24701. 0x1f,0x4b,0x88,0x8e,0x4e,0x7e,0xdb,0x0d
  24702. };
  24703. WOLFSSL_SMALL_STACK_STATIC const byte sigCtx2[] = {
  24704. 0xcc,0x5e,0x63,0xa2,0x7e,0x94,0xaf,0xd3,
  24705. 0x41,0x83,0x38,0xd2,0x48,0x6f,0xa9,0x2a,
  24706. 0xf9,0x91,0x7c,0x2d,0x98,0x9e,0x06,0xe5,
  24707. 0x02,0x77,0x72,0x1c,0x34,0x38,0x18,0xb4,
  24708. 0x21,0x96,0xbc,0x29,0x2e,0x68,0xf3,0x4d,
  24709. 0x85,0x9b,0xbe,0xad,0x17,0x9f,0x54,0x54,
  24710. 0x2d,0x4b,0x04,0xdc,0xfb,0xfa,0x4a,0x68,
  24711. 0x4e,0x39,0x50,0xfb,0x1c,0xcd,0x8d,0x0d
  24712. };
  24713. WOLFSSL_SMALL_STACK_STATIC const byte msgCtx[] = {
  24714. 0xf7,0x26,0x93,0x6d,0x19,0xc8,0x00,0x49,
  24715. 0x4e,0x3f,0xda,0xff,0x20,0xb2,0x76,0xa8
  24716. };
  24717. WOLFSSL_SMALL_STACK_STATIC const byte contextCtx[] = {
  24718. 0x66,0x6f,0x6f
  24719. };
  24720. outlen = sizeof(out);
  24721. XMEMSET(out, 0, sizeof(out));
  24722. ret = wc_ed25519_init_ex(&key, HEAP_HINT, devId);
  24723. if (ret != 0)
  24724. return 10800;
  24725. ret = wc_ed25519_import_private_key(sKeyCtx, ED25519_KEY_SIZE, pKeyCtx,
  24726. sizeof(pKeyCtx), &key);
  24727. if (ret == 0)
  24728. ret = wc_ed25519ctx_sign_msg(msgCtx, sizeof(msgCtx), out, &outlen, &key,
  24729. contextCtx, sizeof(contextCtx));
  24730. if (ret == 0 && XMEMCMP(out, sigCtx1, 64) != 0)
  24731. ret = -10801;
  24732. #if defined(HAVE_ED25519_VERIFY)
  24733. /* test verify on good msg */
  24734. if (ret == 0)
  24735. ret = wc_ed25519ctx_verify_msg(out, outlen, msgCtx, sizeof(msgCtx),
  24736. &verify, &key, contextCtx, sizeof(contextCtx));
  24737. if (ret == 0 && verify != 1)
  24738. ret = -10802;
  24739. #endif
  24740. if (ret == 0)
  24741. ret = wc_ed25519ctx_sign_msg(msgCtx, sizeof(msgCtx), out, &outlen, &key,
  24742. NULL, 0);
  24743. if (ret == 0 && XMEMCMP(out, sigCtx2, 64) != 0)
  24744. ret = -10803;
  24745. #if defined(HAVE_ED25519_VERIFY)
  24746. /* test verify on good msg */
  24747. if (ret == 0)
  24748. ret = wc_ed25519ctx_verify_msg(out, outlen, msgCtx, sizeof(msgCtx),
  24749. &verify, &key, NULL, 0);
  24750. if (ret == 0 && verify != 1)
  24751. ret = -10804;
  24752. #endif
  24753. wc_ed25519_free(&key);
  24754. return ret;
  24755. }
  24756. static int ed25519ph_test(void)
  24757. {
  24758. int ret = 0;
  24759. byte out[ED25519_SIG_SIZE];
  24760. word32 outlen;
  24761. #ifdef HAVE_ED25519_VERIFY
  24762. int verify = 0;
  24763. #endif /* HAVE_ED25519_VERIFY */
  24764. ed25519_key key;
  24765. WOLFSSL_SMALL_STACK_STATIC const byte sKeyPh[] = {
  24766. 0x83,0x3f,0xe6,0x24,0x09,0x23,0x7b,0x9d,
  24767. 0x62,0xec,0x77,0x58,0x75,0x20,0x91,0x1e,
  24768. 0x9a,0x75,0x9c,0xec,0x1d,0x19,0x75,0x5b,
  24769. 0x7d,0xa9,0x01,0xb9,0x6d,0xca,0x3d,0x42
  24770. };
  24771. WOLFSSL_SMALL_STACK_STATIC const byte pKeyPh[] = {
  24772. 0xec,0x17,0x2b,0x93,0xad,0x5e,0x56,0x3b,
  24773. 0xf4,0x93,0x2c,0x70,0xe1,0x24,0x50,0x34,
  24774. 0xc3,0x54,0x67,0xef,0x2e,0xfd,0x4d,0x64,
  24775. 0xeb,0xf8,0x19,0x68,0x34,0x67,0xe2,0xbf
  24776. };
  24777. WOLFSSL_SMALL_STACK_STATIC const byte sigPh1[] = {
  24778. 0x98,0xa7,0x02,0x22,0xf0,0xb8,0x12,0x1a,
  24779. 0xa9,0xd3,0x0f,0x81,0x3d,0x68,0x3f,0x80,
  24780. 0x9e,0x46,0x2b,0x46,0x9c,0x7f,0xf8,0x76,
  24781. 0x39,0x49,0x9b,0xb9,0x4e,0x6d,0xae,0x41,
  24782. 0x31,0xf8,0x50,0x42,0x46,0x3c,0x2a,0x35,
  24783. 0x5a,0x20,0x03,0xd0,0x62,0xad,0xf5,0xaa,
  24784. 0xa1,0x0b,0x8c,0x61,0xe6,0x36,0x06,0x2a,
  24785. 0xaa,0xd1,0x1c,0x2a,0x26,0x08,0x34,0x06
  24786. };
  24787. WOLFSSL_SMALL_STACK_STATIC const byte sigPh2[] = {
  24788. 0xe0,0x39,0x70,0x2b,0x4c,0x25,0x95,0xa6,
  24789. 0xa5,0x41,0xac,0x85,0x09,0x23,0x6e,0x29,
  24790. 0x90,0x47,0x47,0x95,0x33,0x0c,0x9b,0x34,
  24791. 0xa7,0x5f,0x58,0xa6,0x60,0x12,0x9e,0x08,
  24792. 0xfd,0x73,0x69,0x43,0xfb,0x19,0x43,0xa5,
  24793. 0x57,0x20,0xb9,0xe0,0x95,0x7b,0x1e,0xd6,
  24794. 0x73,0x48,0x16,0x61,0x9f,0x13,0x88,0xf4,
  24795. 0x3f,0x73,0xe6,0xe3,0xba,0xa8,0x1c,0x0e
  24796. };
  24797. WOLFSSL_SMALL_STACK_STATIC const byte msgPh[] = {
  24798. 0x61,0x62,0x63
  24799. };
  24800. /* SHA-512 hash of msgPh */
  24801. WOLFSSL_SMALL_STACK_STATIC const byte hashPh[] = {
  24802. 0xdd,0xaf,0x35,0xa1,0x93,0x61,0x7a,0xba,
  24803. 0xcc,0x41,0x73,0x49,0xae,0x20,0x41,0x31,
  24804. 0x12,0xe6,0xfa,0x4e,0x89,0xa9,0x7e,0xa2,
  24805. 0x0a,0x9e,0xee,0xe6,0x4b,0x55,0xd3,0x9a,
  24806. 0x21,0x92,0x99,0x2a,0x27,0x4f,0xc1,0xa8,
  24807. 0x36,0xba,0x3c,0x23,0xa3,0xfe,0xeb,0xbd,
  24808. 0x45,0x4d,0x44,0x23,0x64,0x3c,0xe8,0x0e,
  24809. 0x2a,0x9a,0xc9,0x4f,0xa5,0x4c,0xa4,0x9f
  24810. };
  24811. WOLFSSL_SMALL_STACK_STATIC const byte contextPh2[] = {
  24812. 0x66,0x6f,0x6f
  24813. };
  24814. outlen = sizeof(out);
  24815. XMEMSET(out, 0, sizeof(out));
  24816. ret = wc_ed25519_init_ex(&key, HEAP_HINT, devId);
  24817. if (ret != 0)
  24818. return -10900;
  24819. ret = wc_ed25519_import_private_key(sKeyPh, ED25519_KEY_SIZE, pKeyPh,
  24820. sizeof(pKeyPh), &key);
  24821. if (ret == 0)
  24822. ret = wc_ed25519ph_sign_msg(msgPh, sizeof(msgPh), out, &outlen, &key,
  24823. NULL, 0);
  24824. if (ret == 0 && XMEMCMP(out, sigPh1, 64) != 0)
  24825. ret = -10901;
  24826. #if defined(HAVE_ED25519_VERIFY)
  24827. /* test verify on good msg */
  24828. if (ret == 0)
  24829. ret = wc_ed25519ph_verify_msg(out, outlen, msgPh, sizeof(msgPh),
  24830. &verify, &key, NULL, 0);
  24831. if (ret == 0 && verify != 1)
  24832. ret = -10902;
  24833. #endif
  24834. if (ret == 0)
  24835. ret = wc_ed25519ph_sign_msg(msgPh, sizeof(msgPh), out, &outlen, &key,
  24836. contextPh2, sizeof(contextPh2));
  24837. if (ret == 0 && XMEMCMP(out, sigPh2, 64) != 0)
  24838. ret = -10903;
  24839. #if defined(HAVE_ED25519_VERIFY)
  24840. /* test verify on good msg */
  24841. if (ret == 0)
  24842. ret = wc_ed25519ph_verify_msg(out, outlen, msgPh, sizeof(msgPh), &verify,
  24843. &key, contextPh2, sizeof(contextPh2));
  24844. if (ret == 0 && verify != 1)
  24845. ret = -10904;
  24846. #endif
  24847. if (ret == 0)
  24848. ret = wc_ed25519ph_sign_hash(hashPh, sizeof(hashPh), out, &outlen, &key,
  24849. NULL, 0);
  24850. if (ret == 0 && XMEMCMP(out, sigPh1, 64) != 0)
  24851. ret = -10905;
  24852. #if defined(HAVE_ED25519_VERIFY)
  24853. if (ret == 0)
  24854. ret = wc_ed25519ph_verify_hash(out, outlen, hashPh, sizeof(hashPh),
  24855. &verify, &key, NULL, 0);
  24856. if (ret == 0 && verify != 1)
  24857. ret = -10906;
  24858. #endif
  24859. if (ret == 0)
  24860. ret = wc_ed25519ph_sign_hash(hashPh, sizeof(hashPh), out, &outlen, &key,
  24861. contextPh2, sizeof(contextPh2));
  24862. if (ret == 0 && XMEMCMP(out, sigPh2, 64) != 0)
  24863. ret = -10907;
  24864. #if defined(HAVE_ED25519_VERIFY)
  24865. if (ret == 0)
  24866. ret = wc_ed25519ph_verify_hash(out, outlen, hashPh, sizeof(hashPh), &verify,
  24867. &key, contextPh2, sizeof(contextPh2));
  24868. if (ret == 0 && verify != 1)
  24869. ret = -10908;
  24870. #endif
  24871. wc_ed25519_free(&key);
  24872. return ret;
  24873. }
  24874. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_EXPORT && HAVE_ED25519_KEY_IMPORT */
  24875. WOLFSSL_TEST_SUBROUTINE int ed25519_test(void)
  24876. {
  24877. int ret;
  24878. WC_RNG rng;
  24879. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_EXPORT) &&\
  24880. defined(HAVE_ED25519_KEY_IMPORT)
  24881. byte out[ED25519_SIG_SIZE];
  24882. byte exportPKey[ED25519_KEY_SIZE];
  24883. byte exportSKey[ED25519_KEY_SIZE];
  24884. word32 exportPSz;
  24885. word32 exportSSz;
  24886. int i;
  24887. word32 outlen;
  24888. #ifdef HAVE_ED25519_VERIFY
  24889. #ifdef WOLFSSL_ED25519_STREAMING_VERIFY
  24890. int j;
  24891. #endif
  24892. int verify;
  24893. #endif /* HAVE_ED25519_VERIFY */
  24894. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_EXPORT && HAVE_ED25519_KEY_IMPORT */
  24895. word32 keySz, sigSz;
  24896. ed25519_key key;
  24897. ed25519_key key2;
  24898. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_EXPORT) && \
  24899. defined(HAVE_ED25519_KEY_IMPORT)
  24900. /* test vectors from
  24901. https://tools.ietf.org/html/draft-josefsson-eddsa-ed25519-02
  24902. */
  24903. WOLFSSL_SMALL_STACK_STATIC const byte sKey1[] = {
  24904. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  24905. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  24906. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  24907. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60
  24908. };
  24909. WOLFSSL_SMALL_STACK_STATIC const byte sKey2[] = {
  24910. 0x4c,0xcd,0x08,0x9b,0x28,0xff,0x96,0xda,
  24911. 0x9d,0xb6,0xc3,0x46,0xec,0x11,0x4e,0x0f,
  24912. 0x5b,0x8a,0x31,0x9f,0x35,0xab,0xa6,0x24,
  24913. 0xda,0x8c,0xf6,0xed,0x4f,0xb8,0xa6,0xfb
  24914. };
  24915. WOLFSSL_SMALL_STACK_STATIC const byte sKey3[] = {
  24916. 0xc5,0xaa,0x8d,0xf4,0x3f,0x9f,0x83,0x7b,
  24917. 0xed,0xb7,0x44,0x2f,0x31,0xdc,0xb7,0xb1,
  24918. 0x66,0xd3,0x85,0x35,0x07,0x6f,0x09,0x4b,
  24919. 0x85,0xce,0x3a,0x2e,0x0b,0x44,0x58,0xf7
  24920. };
  24921. /* uncompressed test */
  24922. WOLFSSL_SMALL_STACK_STATIC const byte sKey4[] = {
  24923. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  24924. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  24925. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  24926. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60
  24927. };
  24928. /* compressed prefix test */
  24929. WOLFSSL_SMALL_STACK_STATIC const byte sKey5[] = {
  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. WOLFSSL_SMALL_STACK_STATIC const byte sKey6[] = {
  24936. 0xf5,0xe5,0x76,0x7c,0xf1,0x53,0x31,0x95,
  24937. 0x17,0x63,0x0f,0x22,0x68,0x76,0xb8,0x6c,
  24938. 0x81,0x60,0xcc,0x58,0x3b,0xc0,0x13,0x74,
  24939. 0x4c,0x6b,0xf2,0x55,0xf5,0xcc,0x0e,0xe5
  24940. };
  24941. WOLFSSL_SMALL_STACK_STATIC const byte* sKeys[] = {sKey1, sKey2, sKey3, sKey4, sKey5, sKey6};
  24942. WOLFSSL_SMALL_STACK_STATIC const byte pKey1[] = {
  24943. 0xd7,0x5a,0x98,0x01,0x82,0xb1,0x0a,0xb7,
  24944. 0xd5,0x4b,0xfe,0xd3,0xc9,0x64,0x07,0x3a,
  24945. 0x0e,0xe1,0x72,0xf3,0xda,0xa6,0x23,0x25,
  24946. 0xaf,0x02,0x1a,0x68,0xf7,0x07,0x51,0x1a
  24947. };
  24948. WOLFSSL_SMALL_STACK_STATIC const byte pKey2[] = {
  24949. 0x3d,0x40,0x17,0xc3,0xe8,0x43,0x89,0x5a,
  24950. 0x92,0xb7,0x0a,0xa7,0x4d,0x1b,0x7e,0xbc,
  24951. 0x9c,0x98,0x2c,0xcf,0x2e,0xc4,0x96,0x8c,
  24952. 0xc0,0xcd,0x55,0xf1,0x2a,0xf4,0x66,0x0c
  24953. };
  24954. WOLFSSL_SMALL_STACK_STATIC const byte pKey3[] = {
  24955. 0xfc,0x51,0xcd,0x8e,0x62,0x18,0xa1,0xa3,
  24956. 0x8d,0xa4,0x7e,0xd0,0x02,0x30,0xf0,0x58,
  24957. 0x08,0x16,0xed,0x13,0xba,0x33,0x03,0xac,
  24958. 0x5d,0xeb,0x91,0x15,0x48,0x90,0x80,0x25
  24959. };
  24960. /* uncompressed test */
  24961. WOLFSSL_SMALL_STACK_STATIC const byte pKey4[] = {
  24962. 0x04,0x55,0xd0,0xe0,0x9a,0x2b,0x9d,0x34,
  24963. 0x29,0x22,0x97,0xe0,0x8d,0x60,0xd0,0xf6,
  24964. 0x20,0xc5,0x13,0xd4,0x72,0x53,0x18,0x7c,
  24965. 0x24,0xb1,0x27,0x86,0xbd,0x77,0x76,0x45,
  24966. 0xce,0x1a,0x51,0x07,0xf7,0x68,0x1a,0x02,
  24967. 0xaf,0x25,0x23,0xa6,0xda,0xf3,0x72,0xe1,
  24968. 0x0e,0x3a,0x07,0x64,0xc9,0xd3,0xfe,0x4b,
  24969. 0xd5,0xb7,0x0a,0xb1,0x82,0x01,0x98,0x5a,
  24970. 0xd7
  24971. };
  24972. /* compressed prefix */
  24973. WOLFSSL_SMALL_STACK_STATIC const byte pKey5[] = {
  24974. 0x40,0xd7,0x5a,0x98,0x01,0x82,0xb1,0x0a,0xb7,
  24975. 0xd5,0x4b,0xfe,0xd3,0xc9,0x64,0x07,0x3a,
  24976. 0x0e,0xe1,0x72,0xf3,0xda,0xa6,0x23,0x25,
  24977. 0xaf,0x02,0x1a,0x68,0xf7,0x07,0x51,0x1a
  24978. };
  24979. WOLFSSL_SMALL_STACK_STATIC const byte pKey6[] = {
  24980. 0x27,0x81,0x17,0xfc,0x14,0x4c,0x72,0x34,
  24981. 0x0f,0x67,0xd0,0xf2,0x31,0x6e,0x83,0x86,
  24982. 0xce,0xff,0xbf,0x2b,0x24,0x28,0xc9,0xc5,
  24983. 0x1f,0xef,0x7c,0x59,0x7f,0x1d,0x42,0x6e
  24984. };
  24985. WOLFSSL_SMALL_STACK_STATIC const byte* pKeys[] = {pKey1, pKey2, pKey3, pKey4, pKey5, pKey6};
  24986. WOLFSSL_SMALL_STACK_STATIC const byte pKeySz[] = {sizeof(pKey1), sizeof(pKey2), sizeof(pKey3),
  24987. sizeof(pKey4), sizeof(pKey5), sizeof(pKey6)};
  24988. WOLFSSL_SMALL_STACK_STATIC const byte sig1[] = {
  24989. 0xe5,0x56,0x43,0x00,0xc3,0x60,0xac,0x72,
  24990. 0x90,0x86,0xe2,0xcc,0x80,0x6e,0x82,0x8a,
  24991. 0x84,0x87,0x7f,0x1e,0xb8,0xe5,0xd9,0x74,
  24992. 0xd8,0x73,0xe0,0x65,0x22,0x49,0x01,0x55,
  24993. 0x5f,0xb8,0x82,0x15,0x90,0xa3,0x3b,0xac,
  24994. 0xc6,0x1e,0x39,0x70,0x1c,0xf9,0xb4,0x6b,
  24995. 0xd2,0x5b,0xf5,0xf0,0x59,0x5b,0xbe,0x24,
  24996. 0x65,0x51,0x41,0x43,0x8e,0x7a,0x10,0x0b
  24997. };
  24998. WOLFSSL_SMALL_STACK_STATIC const byte sig2[] = {
  24999. 0x92,0xa0,0x09,0xa9,0xf0,0xd4,0xca,0xb8,
  25000. 0x72,0x0e,0x82,0x0b,0x5f,0x64,0x25,0x40,
  25001. 0xa2,0xb2,0x7b,0x54,0x16,0x50,0x3f,0x8f,
  25002. 0xb3,0x76,0x22,0x23,0xeb,0xdb,0x69,0xda,
  25003. 0x08,0x5a,0xc1,0xe4,0x3e,0x15,0x99,0x6e,
  25004. 0x45,0x8f,0x36,0x13,0xd0,0xf1,0x1d,0x8c,
  25005. 0x38,0x7b,0x2e,0xae,0xb4,0x30,0x2a,0xee,
  25006. 0xb0,0x0d,0x29,0x16,0x12,0xbb,0x0c,0x00
  25007. };
  25008. WOLFSSL_SMALL_STACK_STATIC const byte sig3[] = {
  25009. 0x62,0x91,0xd6,0x57,0xde,0xec,0x24,0x02,
  25010. 0x48,0x27,0xe6,0x9c,0x3a,0xbe,0x01,0xa3,
  25011. 0x0c,0xe5,0x48,0xa2,0x84,0x74,0x3a,0x44,
  25012. 0x5e,0x36,0x80,0xd7,0xdb,0x5a,0xc3,0xac,
  25013. 0x18,0xff,0x9b,0x53,0x8d,0x16,0xf2,0x90,
  25014. 0xae,0x67,0xf7,0x60,0x98,0x4d,0xc6,0x59,
  25015. 0x4a,0x7c,0x15,0xe9,0x71,0x6e,0xd2,0x8d,
  25016. 0xc0,0x27,0xbe,0xce,0xea,0x1e,0xc4,0x0a
  25017. };
  25018. /* uncompressed test */
  25019. WOLFSSL_SMALL_STACK_STATIC const byte sig4[] = {
  25020. 0xe5,0x56,0x43,0x00,0xc3,0x60,0xac,0x72,
  25021. 0x90,0x86,0xe2,0xcc,0x80,0x6e,0x82,0x8a,
  25022. 0x84,0x87,0x7f,0x1e,0xb8,0xe5,0xd9,0x74,
  25023. 0xd8,0x73,0xe0,0x65,0x22,0x49,0x01,0x55,
  25024. 0x5f,0xb8,0x82,0x15,0x90,0xa3,0x3b,0xac,
  25025. 0xc6,0x1e,0x39,0x70,0x1c,0xf9,0xb4,0x6b,
  25026. 0xd2,0x5b,0xf5,0xf0,0x59,0x5b,0xbe,0x24,
  25027. 0x65,0x51,0x41,0x43,0x8e,0x7a,0x10,0x0b
  25028. };
  25029. /* compressed prefix */
  25030. WOLFSSL_SMALL_STACK_STATIC const byte sig5[] = {
  25031. 0xe5,0x56,0x43,0x00,0xc3,0x60,0xac,0x72,
  25032. 0x90,0x86,0xe2,0xcc,0x80,0x6e,0x82,0x8a,
  25033. 0x84,0x87,0x7f,0x1e,0xb8,0xe5,0xd9,0x74,
  25034. 0xd8,0x73,0xe0,0x65,0x22,0x49,0x01,0x55,
  25035. 0x5f,0xb8,0x82,0x15,0x90,0xa3,0x3b,0xac,
  25036. 0xc6,0x1e,0x39,0x70,0x1c,0xf9,0xb4,0x6b,
  25037. 0xd2,0x5b,0xf5,0xf0,0x59,0x5b,0xbe,0x24,
  25038. 0x65,0x51,0x41,0x43,0x8e,0x7a,0x10,0x0b
  25039. };
  25040. WOLFSSL_SMALL_STACK_STATIC const byte sig6[] = {
  25041. 0x0a,0xab,0x4c,0x90,0x05,0x01,0xb3,0xe2,
  25042. 0x4d,0x7c,0xdf,0x46,0x63,0x32,0x6a,0x3a,
  25043. 0x87,0xdf,0x5e,0x48,0x43,0xb2,0xcb,0xdb,
  25044. 0x67,0xcb,0xf6,0xe4,0x60,0xfe,0xc3,0x50,
  25045. 0xaa,0x53,0x71,0xb1,0x50,0x8f,0x9f,0x45,
  25046. 0x28,0xec,0xea,0x23,0xc4,0x36,0xd9,0x4b,
  25047. 0x5e,0x8f,0xcd,0x4f,0x68,0x1e,0x30,0xa6,
  25048. 0xac,0x00,0xa9,0x70,0x4a,0x18,0x8a,0x03
  25049. };
  25050. WOLFSSL_SMALL_STACK_STATIC const byte* sigs[] = {sig1, sig2, sig3, sig4, sig5, sig6};
  25051. WOLFSSL_SMALL_STACK_STATIC const byte msg1[] = {0x0 };
  25052. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] = {0x72};
  25053. WOLFSSL_SMALL_STACK_STATIC const byte msg3[] = {0xAF,0x82};
  25054. /* test of a 1024 byte long message */
  25055. WOLFSSL_SMALL_STACK_STATIC const byte msg4[] = {
  25056. 0x08,0xb8,0xb2,0xb7,0x33,0x42,0x42,0x43,
  25057. 0x76,0x0f,0xe4,0x26,0xa4,0xb5,0x49,0x08,
  25058. 0x63,0x21,0x10,0xa6,0x6c,0x2f,0x65,0x91,
  25059. 0xea,0xbd,0x33,0x45,0xe3,0xe4,0xeb,0x98,
  25060. 0xfa,0x6e,0x26,0x4b,0xf0,0x9e,0xfe,0x12,
  25061. 0xee,0x50,0xf8,0xf5,0x4e,0x9f,0x77,0xb1,
  25062. 0xe3,0x55,0xf6,0xc5,0x05,0x44,0xe2,0x3f,
  25063. 0xb1,0x43,0x3d,0xdf,0x73,0xbe,0x84,0xd8,
  25064. 0x79,0xde,0x7c,0x00,0x46,0xdc,0x49,0x96,
  25065. 0xd9,0xe7,0x73,0xf4,0xbc,0x9e,0xfe,0x57,
  25066. 0x38,0x82,0x9a,0xdb,0x26,0xc8,0x1b,0x37,
  25067. 0xc9,0x3a,0x1b,0x27,0x0b,0x20,0x32,0x9d,
  25068. 0x65,0x86,0x75,0xfc,0x6e,0xa5,0x34,0xe0,
  25069. 0x81,0x0a,0x44,0x32,0x82,0x6b,0xf5,0x8c,
  25070. 0x94,0x1e,0xfb,0x65,0xd5,0x7a,0x33,0x8b,
  25071. 0xbd,0x2e,0x26,0x64,0x0f,0x89,0xff,0xbc,
  25072. 0x1a,0x85,0x8e,0xfc,0xb8,0x55,0x0e,0xe3,
  25073. 0xa5,0xe1,0x99,0x8b,0xd1,0x77,0xe9,0x3a,
  25074. 0x73,0x63,0xc3,0x44,0xfe,0x6b,0x19,0x9e,
  25075. 0xe5,0xd0,0x2e,0x82,0xd5,0x22,0xc4,0xfe,
  25076. 0xba,0x15,0x45,0x2f,0x80,0x28,0x8a,0x82,
  25077. 0x1a,0x57,0x91,0x16,0xec,0x6d,0xad,0x2b,
  25078. 0x3b,0x31,0x0d,0xa9,0x03,0x40,0x1a,0xa6,
  25079. 0x21,0x00,0xab,0x5d,0x1a,0x36,0x55,0x3e,
  25080. 0x06,0x20,0x3b,0x33,0x89,0x0c,0xc9,0xb8,
  25081. 0x32,0xf7,0x9e,0xf8,0x05,0x60,0xcc,0xb9,
  25082. 0xa3,0x9c,0xe7,0x67,0x96,0x7e,0xd6,0x28,
  25083. 0xc6,0xad,0x57,0x3c,0xb1,0x16,0xdb,0xef,
  25084. 0xef,0xd7,0x54,0x99,0xda,0x96,0xbd,0x68,
  25085. 0xa8,0xa9,0x7b,0x92,0x8a,0x8b,0xbc,0x10,
  25086. 0x3b,0x66,0x21,0xfc,0xde,0x2b,0xec,0xa1,
  25087. 0x23,0x1d,0x20,0x6b,0xe6,0xcd,0x9e,0xc7,
  25088. 0xaf,0xf6,0xf6,0xc9,0x4f,0xcd,0x72,0x04,
  25089. 0xed,0x34,0x55,0xc6,0x8c,0x83,0xf4,0xa4,
  25090. 0x1d,0xa4,0xaf,0x2b,0x74,0xef,0x5c,0x53,
  25091. 0xf1,0xd8,0xac,0x70,0xbd,0xcb,0x7e,0xd1,
  25092. 0x85,0xce,0x81,0xbd,0x84,0x35,0x9d,0x44,
  25093. 0x25,0x4d,0x95,0x62,0x9e,0x98,0x55,0xa9,
  25094. 0x4a,0x7c,0x19,0x58,0xd1,0xf8,0xad,0xa5,
  25095. 0xd0,0x53,0x2e,0xd8,0xa5,0xaa,0x3f,0xb2,
  25096. 0xd1,0x7b,0xa7,0x0e,0xb6,0x24,0x8e,0x59,
  25097. 0x4e,0x1a,0x22,0x97,0xac,0xbb,0xb3,0x9d,
  25098. 0x50,0x2f,0x1a,0x8c,0x6e,0xb6,0xf1,0xce,
  25099. 0x22,0xb3,0xde,0x1a,0x1f,0x40,0xcc,0x24,
  25100. 0x55,0x41,0x19,0xa8,0x31,0xa9,0xaa,0xd6,
  25101. 0x07,0x9c,0xad,0x88,0x42,0x5d,0xe6,0xbd,
  25102. 0xe1,0xa9,0x18,0x7e,0xbb,0x60,0x92,0xcf,
  25103. 0x67,0xbf,0x2b,0x13,0xfd,0x65,0xf2,0x70,
  25104. 0x88,0xd7,0x8b,0x7e,0x88,0x3c,0x87,0x59,
  25105. 0xd2,0xc4,0xf5,0xc6,0x5a,0xdb,0x75,0x53,
  25106. 0x87,0x8a,0xd5,0x75,0xf9,0xfa,0xd8,0x78,
  25107. 0xe8,0x0a,0x0c,0x9b,0xa6,0x3b,0xcb,0xcc,
  25108. 0x27,0x32,0xe6,0x94,0x85,0xbb,0xc9,0xc9,
  25109. 0x0b,0xfb,0xd6,0x24,0x81,0xd9,0x08,0x9b,
  25110. 0xec,0xcf,0x80,0xcf,0xe2,0xdf,0x16,0xa2,
  25111. 0xcf,0x65,0xbd,0x92,0xdd,0x59,0x7b,0x07,
  25112. 0x07,0xe0,0x91,0x7a,0xf4,0x8b,0xbb,0x75,
  25113. 0xfe,0xd4,0x13,0xd2,0x38,0xf5,0x55,0x5a,
  25114. 0x7a,0x56,0x9d,0x80,0xc3,0x41,0x4a,0x8d,
  25115. 0x08,0x59,0xdc,0x65,0xa4,0x61,0x28,0xba,
  25116. 0xb2,0x7a,0xf8,0x7a,0x71,0x31,0x4f,0x31,
  25117. 0x8c,0x78,0x2b,0x23,0xeb,0xfe,0x80,0x8b,
  25118. 0x82,0xb0,0xce,0x26,0x40,0x1d,0x2e,0x22,
  25119. 0xf0,0x4d,0x83,0xd1,0x25,0x5d,0xc5,0x1a,
  25120. 0xdd,0xd3,0xb7,0x5a,0x2b,0x1a,0xe0,0x78,
  25121. 0x45,0x04,0xdf,0x54,0x3a,0xf8,0x96,0x9b,
  25122. 0xe3,0xea,0x70,0x82,0xff,0x7f,0xc9,0x88,
  25123. 0x8c,0x14,0x4d,0xa2,0xaf,0x58,0x42,0x9e,
  25124. 0xc9,0x60,0x31,0xdb,0xca,0xd3,0xda,0xd9,
  25125. 0xaf,0x0d,0xcb,0xaa,0xaf,0x26,0x8c,0xb8,
  25126. 0xfc,0xff,0xea,0xd9,0x4f,0x3c,0x7c,0xa4,
  25127. 0x95,0xe0,0x56,0xa9,0xb4,0x7a,0xcd,0xb7,
  25128. 0x51,0xfb,0x73,0xe6,0x66,0xc6,0xc6,0x55,
  25129. 0xad,0xe8,0x29,0x72,0x97,0xd0,0x7a,0xd1,
  25130. 0xba,0x5e,0x43,0xf1,0xbc,0xa3,0x23,0x01,
  25131. 0x65,0x13,0x39,0xe2,0x29,0x04,0xcc,0x8c,
  25132. 0x42,0xf5,0x8c,0x30,0xc0,0x4a,0xaf,0xdb,
  25133. 0x03,0x8d,0xda,0x08,0x47,0xdd,0x98,0x8d,
  25134. 0xcd,0xa6,0xf3,0xbf,0xd1,0x5c,0x4b,0x4c,
  25135. 0x45,0x25,0x00,0x4a,0xa0,0x6e,0xef,0xf8,
  25136. 0xca,0x61,0x78,0x3a,0xac,0xec,0x57,0xfb,
  25137. 0x3d,0x1f,0x92,0xb0,0xfe,0x2f,0xd1,0xa8,
  25138. 0x5f,0x67,0x24,0x51,0x7b,0x65,0xe6,0x14,
  25139. 0xad,0x68,0x08,0xd6,0xf6,0xee,0x34,0xdf,
  25140. 0xf7,0x31,0x0f,0xdc,0x82,0xae,0xbf,0xd9,
  25141. 0x04,0xb0,0x1e,0x1d,0xc5,0x4b,0x29,0x27,
  25142. 0x09,0x4b,0x2d,0xb6,0x8d,0x6f,0x90,0x3b,
  25143. 0x68,0x40,0x1a,0xde,0xbf,0x5a,0x7e,0x08,
  25144. 0xd7,0x8f,0xf4,0xef,0x5d,0x63,0x65,0x3a,
  25145. 0x65,0x04,0x0c,0xf9,0xbf,0xd4,0xac,0xa7,
  25146. 0x98,0x4a,0x74,0xd3,0x71,0x45,0x98,0x67,
  25147. 0x80,0xfc,0x0b,0x16,0xac,0x45,0x16,0x49,
  25148. 0xde,0x61,0x88,0xa7,0xdb,0xdf,0x19,0x1f,
  25149. 0x64,0xb5,0xfc,0x5e,0x2a,0xb4,0x7b,0x57,
  25150. 0xf7,0xf7,0x27,0x6c,0xd4,0x19,0xc1,0x7a,
  25151. 0x3c,0xa8,0xe1,0xb9,0x39,0xae,0x49,0xe4,
  25152. 0x88,0xac,0xba,0x6b,0x96,0x56,0x10,0xb5,
  25153. 0x48,0x01,0x09,0xc8,0xb1,0x7b,0x80,0xe1,
  25154. 0xb7,0xb7,0x50,0xdf,0xc7,0x59,0x8d,0x5d,
  25155. 0x50,0x11,0xfd,0x2d,0xcc,0x56,0x00,0xa3,
  25156. 0x2e,0xf5,0xb5,0x2a,0x1e,0xcc,0x82,0x0e,
  25157. 0x30,0x8a,0xa3,0x42,0x72,0x1a,0xac,0x09,
  25158. 0x43,0xbf,0x66,0x86,0xb6,0x4b,0x25,0x79,
  25159. 0x37,0x65,0x04,0xcc,0xc4,0x93,0xd9,0x7e,
  25160. 0x6a,0xed,0x3f,0xb0,0xf9,0xcd,0x71,0xa4,
  25161. 0x3d,0xd4,0x97,0xf0,0x1f,0x17,0xc0,0xe2,
  25162. 0xcb,0x37,0x97,0xaa,0x2a,0x2f,0x25,0x66,
  25163. 0x56,0x16,0x8e,0x6c,0x49,0x6a,0xfc,0x5f,
  25164. 0xb9,0x32,0x46,0xf6,0xb1,0x11,0x63,0x98,
  25165. 0xa3,0x46,0xf1,0xa6,0x41,0xf3,0xb0,0x41,
  25166. 0xe9,0x89,0xf7,0x91,0x4f,0x90,0xcc,0x2c,
  25167. 0x7f,0xff,0x35,0x78,0x76,0xe5,0x06,0xb5,
  25168. 0x0d,0x33,0x4b,0xa7,0x7c,0x22,0x5b,0xc3,
  25169. 0x07,0xba,0x53,0x71,0x52,0xf3,0xf1,0x61,
  25170. 0x0e,0x4e,0xaf,0xe5,0x95,0xf6,0xd9,0xd9,
  25171. 0x0d,0x11,0xfa,0xa9,0x33,0xa1,0x5e,0xf1,
  25172. 0x36,0x95,0x46,0x86,0x8a,0x7f,0x3a,0x45,
  25173. 0xa9,0x67,0x68,0xd4,0x0f,0xd9,0xd0,0x34,
  25174. 0x12,0xc0,0x91,0xc6,0x31,0x5c,0xf4,0xfd,
  25175. 0xe7,0xcb,0x68,0x60,0x69,0x37,0x38,0x0d,
  25176. 0xb2,0xea,0xaa,0x70,0x7b,0x4c,0x41,0x85,
  25177. 0xc3,0x2e,0xdd,0xcd,0xd3,0x06,0x70,0x5e,
  25178. 0x4d,0xc1,0xff,0xc8,0x72,0xee,0xee,0x47,
  25179. 0x5a,0x64,0xdf,0xac,0x86,0xab,0xa4,0x1c,
  25180. 0x06,0x18,0x98,0x3f,0x87,0x41,0xc5,0xef,
  25181. 0x68,0xd3,0xa1,0x01,0xe8,0xa3,0xb8,0xca,
  25182. 0xc6,0x0c,0x90,0x5c,0x15,0xfc,0x91,0x08,
  25183. 0x40,0xb9,0x4c,0x00,0xa0,0xb9,0xd0
  25184. };
  25185. WOLFSSL_SMALL_STACK_STATIC const byte* msgs[] = {msg1, msg2, msg3, msg1, msg1, msg4};
  25186. WOLFSSL_SMALL_STACK_STATIC const word16 msgSz[] = {0 /*sizeof(msg1)*/,
  25187. sizeof(msg2),
  25188. sizeof(msg3),
  25189. 0 /*sizeof(msg1)*/,
  25190. 0 /*sizeof(msg1)*/,
  25191. sizeof(msg4)
  25192. };
  25193. #ifndef NO_ASN
  25194. static byte privateEd25519[] = {
  25195. 0x30,0x2e,0x02,0x01,0x00,0x30,0x05,0x06,
  25196. 0x03,0x2b,0x65,0x70,0x04,0x22,0x04,0x20,
  25197. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  25198. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  25199. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  25200. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60
  25201. };
  25202. static byte badPrivateEd25519[] = {
  25203. 0x30,0x52,0x02,0x01,0x00,0x30,0x05,0x06,
  25204. 0x03,0x2b,0x65,0x70,0x04,0x22,0x04,0x20,
  25205. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  25206. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  25207. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  25208. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60,
  25209. 0xa1,0x22,0x04,0x21,0xd7,0x5a,0x98,0x01, /* octet len 0x20 -> 0x21 */
  25210. 0x82,0xb1,0x0a,0xb7,0xd5,0x4b,0xfe,0xd3,
  25211. 0xc9,0x64,0x07,0x3a,0x0e,0xe1,0x72,0xf3,
  25212. 0xda,0xa6,0x23,0x25,0xaf,0x02,0x1a,0x68,
  25213. 0xf7,0x07,0x51,0x1a,
  25214. 0x00 /* add additional bytes to make the pubkey bigger */
  25215. };
  25216. static byte publicEd25519[] = {
  25217. 0x30,0x2a,0x30,0x05,0x06,0x03,0x2b,0x65,
  25218. 0x70,0x03,0x21,0x00,0xd7,0x5a,0x98,0x01,
  25219. 0x82,0xb1,0x0a,0xb7,0xd5,0x4b,0xfe,0xd3,
  25220. 0xc9,0x64,0x07,0x3a,0x0e,0xe1,0x72,0xf3,
  25221. 0xda,0xa6,0x23,0x25,0xaf,0x02,0x1a,0x68,
  25222. 0xf7,0x07,0x51,0x1a
  25223. };
  25224. /* size has been altered to catch if sanity check is done */
  25225. static byte badPublicEd25519[] = {
  25226. 0x30,0x2a,0x30,0x05,0x06,0x03,0x2b,0x65,
  25227. 0x70,0x03,0x21,0x00,0xd7,0x5a,0x98,0x01,
  25228. 0x82,0xb1,0x0a,0xb7,0xd5,0x4b,0xfe,0xd3,
  25229. 0xc9,0x64,0x07,0x3a,0x0e,0xe1,0x72,0xf3,
  25230. 0xda,0xa6,0x23,0x25,0xaf,0x02,0x1a,0x68,
  25231. 0xf7,0x07,0x51,0x1a,
  25232. 0x00 /* add an additional byte to make the pubkey appear bigger */
  25233. };
  25234. static byte privPubEd25519[] = {
  25235. 0x30,0x50,0x02,0x01,0x00,0x30,0x05,0x06,
  25236. 0x03,0x2b,0x65,0x70,0x04,0x22,0x04,0x20,
  25237. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  25238. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  25239. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  25240. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60,
  25241. 0x81,0x20,0xd7,0x5a,0x98,0x01,0x82,0xb1,
  25242. 0x0a,0xb7,0xd5,0x4b,0xfe,0xd3,0xc9,0x64,
  25243. 0x07,0x3a,0x0e,0xe1,0x72,0xf3,0xda,0xa6,
  25244. 0x23,0x25,0xaf,0x02,0x1a,0x68,0xf7,0x07,
  25245. 0x51,0x1a
  25246. };
  25247. word32 idx;
  25248. #endif /* NO_ASN */
  25249. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_EXPORT && HAVE_ED25519_KEY_IMPORT */
  25250. #if !defined(NO_ASN) && defined(HAVE_ED25519_SIGN)
  25251. ed25519_key key3;
  25252. #endif
  25253. /* create ed25519 keys */
  25254. #ifndef HAVE_FIPS
  25255. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  25256. #else
  25257. ret = wc_InitRng(&rng);
  25258. #endif
  25259. if (ret != 0)
  25260. return -11000;
  25261. wc_ed25519_init_ex(&key, HEAP_HINT, devId);
  25262. wc_ed25519_init_ex(&key2, HEAP_HINT, devId);
  25263. #if !defined(NO_ASN) && defined(HAVE_ED25519_SIGN)
  25264. wc_ed25519_init_ex(&key3, HEAP_HINT, devId);
  25265. #endif
  25266. wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  25267. wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key2);
  25268. /* helper functions for signature and key size */
  25269. keySz = wc_ed25519_size(&key);
  25270. sigSz = wc_ed25519_sig_size(&key);
  25271. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_EXPORT) &&\
  25272. defined(HAVE_ED25519_KEY_IMPORT)
  25273. for (i = 0; i < 6; i++) {
  25274. outlen = sizeof(out);
  25275. XMEMSET(out, 0, sizeof(out));
  25276. if (wc_ed25519_import_private_key(sKeys[i], ED25519_KEY_SIZE, pKeys[i],
  25277. pKeySz[i], &key) != 0)
  25278. return -11001 - i;
  25279. if (wc_ed25519_sign_msg(msgs[i], msgSz[i], out, &outlen, &key) != 0)
  25280. return -11011 - i;
  25281. if (XMEMCMP(out, sigs[i], 64))
  25282. return -11021 - i;
  25283. #if defined(HAVE_ED25519_VERIFY)
  25284. /* test verify on good msg */
  25285. if (wc_ed25519_verify_msg(out, outlen, msgs[i], msgSz[i], &verify,
  25286. &key) != 0 || verify != 1)
  25287. return -11031 - i;
  25288. #ifdef WOLFSSL_ED25519_STREAMING_VERIFY
  25289. /* test verify on good msg using streaming interface directly */
  25290. if (wc_ed25519_verify_msg_init(out, outlen,
  25291. &key, (byte)Ed25519, NULL, 0) != 0)
  25292. return -11211 - i;
  25293. for (j = 0; j < msgSz[i]; j += i) {
  25294. if (wc_ed25519_verify_msg_update(msgs[i] + j, MIN(i, msgSz[i] - j), &key) != 0)
  25295. return -11221 - i;
  25296. }
  25297. if (wc_ed25519_verify_msg_final(out, outlen, &verify,
  25298. &key) != 0 || verify != 1)
  25299. return -11231 - i;
  25300. #endif /* WOLFSSL_ED25519_STREAMING_VERIFY */
  25301. /* test verify on bad msg */
  25302. out[outlen-1] = out[outlen-1] + 1;
  25303. if (wc_ed25519_verify_msg(out, outlen, msgs[i], msgSz[i], &verify,
  25304. &key) == 0 || verify == 1)
  25305. return -11041 - i;
  25306. #endif /* HAVE_ED25519_VERIFY */
  25307. /* test api for import/exporting keys */
  25308. exportPSz = sizeof(exportPKey);
  25309. exportSSz = sizeof(exportSKey);
  25310. if (wc_ed25519_export_public(&key, exportPKey, &exportPSz) != 0)
  25311. return -11051 - i;
  25312. if (wc_ed25519_import_public_ex(exportPKey, exportPSz, &key2, 1) != 0)
  25313. return -11061 - i;
  25314. if (wc_ed25519_export_private_only(&key, exportSKey, &exportSSz) != 0)
  25315. return -11071 - i;
  25316. if (wc_ed25519_import_private_key(exportSKey, exportSSz,
  25317. exportPKey, exportPSz, &key2) != 0)
  25318. return -11081 - i;
  25319. /* clear "out" buffer and test sign with imported keys */
  25320. outlen = sizeof(out);
  25321. XMEMSET(out, 0, sizeof(out));
  25322. if (wc_ed25519_sign_msg(msgs[i], msgSz[i], out, &outlen, &key2) != 0)
  25323. return -11091 - i;
  25324. #if defined(HAVE_ED25519_VERIFY)
  25325. if (wc_ed25519_verify_msg(out, outlen, msgs[i], msgSz[i], &verify,
  25326. &key2) != 0 || verify != 1)
  25327. return -11101 - i;
  25328. if (XMEMCMP(out, sigs[i], 64))
  25329. return -11111 - i;
  25330. #endif /* HAVE_ED25519_VERIFY */
  25331. }
  25332. ret = ed25519ctx_test();
  25333. if (ret != 0)
  25334. return ret;
  25335. ret = ed25519ph_test();
  25336. if (ret != 0)
  25337. return ret;
  25338. #ifndef NO_ASN
  25339. /* Try ASN.1 encoded private-only key and public key. */
  25340. idx = 0;
  25341. if (wc_Ed25519PrivateKeyDecode(privateEd25519, &idx, &key3,
  25342. sizeof(privateEd25519)) != 0)
  25343. return -11121;
  25344. idx = 0;
  25345. if (wc_Ed25519PrivateKeyDecode(badPrivateEd25519, &idx, &key3,
  25346. sizeof(badPrivateEd25519)) == 0)
  25347. return -11122;
  25348. if (wc_ed25519_sign_msg(msgs[0], msgSz[0], out, &outlen, &key3)
  25349. != BAD_FUNC_ARG)
  25350. return -11131;
  25351. /* try with a buffer size that is too large */
  25352. idx = 0;
  25353. if (wc_Ed25519PublicKeyDecode(badPublicEd25519, &idx, &key3,
  25354. sizeof(badPublicEd25519)) == 0)
  25355. return -11140;
  25356. idx = 0;
  25357. if (wc_Ed25519PublicKeyDecode(publicEd25519, &idx, &key3,
  25358. sizeof(publicEd25519)) != 0)
  25359. return -11141;
  25360. if (wc_ed25519_sign_msg(msgs[0], msgSz[0], out, &outlen, &key3) != 0)
  25361. return -11151;
  25362. if (XMEMCMP(out, sigs[0], 64))
  25363. return -11161;
  25364. #if defined(HAVE_ED25519_VERIFY)
  25365. /* test verify on good msg */
  25366. if (wc_ed25519_verify_msg(out, outlen, msgs[0], msgSz[0], &verify, &key3)
  25367. != 0 || verify != 1)
  25368. return -11171;
  25369. #endif /* HAVE_ED25519_VERIFY */
  25370. wc_ed25519_free(&key3);
  25371. wc_ed25519_init(&key3);
  25372. idx = 0;
  25373. if (wc_Ed25519PrivateKeyDecode(privPubEd25519, &idx, &key3,
  25374. sizeof(privPubEd25519)) != 0)
  25375. return -11181;
  25376. if (wc_ed25519_sign_msg(msgs[0], msgSz[0], out, &outlen, &key3) != 0)
  25377. return -11191;
  25378. if (XMEMCMP(out, sigs[0], 64))
  25379. return -11201;
  25380. wc_ed25519_free(&key3);
  25381. #endif /* NO_ASN */
  25382. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_EXPORT && HAVE_ED25519_KEY_IMPORT */
  25383. /* clean up keys when done */
  25384. wc_ed25519_free(&key);
  25385. wc_ed25519_free(&key2);
  25386. #if defined(HAVE_HASHDRBG) || defined(NO_RC4)
  25387. wc_FreeRng(&rng);
  25388. #endif
  25389. /* hush warnings of unused keySz and sigSz */
  25390. (void)keySz;
  25391. (void)sigSz;
  25392. #ifdef WOLFSSL_TEST_CERT
  25393. ret = ed25519_test_cert();
  25394. if (ret < 0)
  25395. return ret;
  25396. #ifdef WOLFSSL_CERT_GEN
  25397. ret = ed25519_test_make_cert();
  25398. if (ret < 0)
  25399. return ret;
  25400. #endif /* WOLFSSL_CERT_GEN */
  25401. #endif /* WOLFSSL_TEST_CERT */
  25402. return 0;
  25403. }
  25404. #endif /* HAVE_ED25519 */
  25405. #ifdef HAVE_CURVE448
  25406. #if defined(HAVE_CURVE448_SHARED_SECRET) && \
  25407. defined(HAVE_CURVE448_KEY_IMPORT)
  25408. /* Test the wc_curve448_check_public API.
  25409. *
  25410. * returns 0 on success and -ve on failure.
  25411. */
  25412. static int curve448_check_public_test(void)
  25413. {
  25414. /* Little-endian values that will fail */
  25415. byte fail_le[][CURVE448_KEY_SIZE] = {
  25416. {
  25417. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25418. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25419. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25420. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25421. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25422. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25423. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  25424. },
  25425. {
  25426. 0x01,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. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25432. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  25433. },
  25434. };
  25435. /* Big-endian values that will fail */
  25436. byte fail_be[][CURVE448_KEY_SIZE] = {
  25437. {
  25438. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25439. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25440. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25441. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25442. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25443. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25444. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  25445. },
  25446. {
  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. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25453. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  25454. },
  25455. };
  25456. /* Good or valid public value */
  25457. byte good[CURVE448_KEY_SIZE] = {
  25458. 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25459. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25460. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25461. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25462. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25463. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25464. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  25465. };
  25466. int i;
  25467. /* Parameter checks */
  25468. /* NULL pointer */
  25469. if (wc_curve448_check_public(NULL, 0, EC448_LITTLE_ENDIAN) !=
  25470. BAD_FUNC_ARG) {
  25471. return -11300;
  25472. }
  25473. if (wc_curve448_check_public(NULL, 0, EC448_BIG_ENDIAN) != BAD_FUNC_ARG) {
  25474. return -11301;
  25475. }
  25476. /* Length of 0 treated differently to other invalid lengths for TLS */
  25477. if (wc_curve448_check_public(good, 0, EC448_LITTLE_ENDIAN) != BUFFER_E)
  25478. return -11302;
  25479. if (wc_curve448_check_public(good, 0, EC448_BIG_ENDIAN) != BUFFER_E)
  25480. return -11303;
  25481. /* Length not CURVE448_KEY_SIZE */
  25482. for (i = 1; i < CURVE448_KEY_SIZE + 2; i++) {
  25483. if (i == CURVE448_KEY_SIZE)
  25484. continue;
  25485. if (wc_curve448_check_public(good, i, EC448_LITTLE_ENDIAN) !=
  25486. ECC_BAD_ARG_E) {
  25487. return -11304 - i;
  25488. }
  25489. if (wc_curve448_check_public(good, i, EC448_BIG_ENDIAN) !=
  25490. ECC_BAD_ARG_E) {
  25491. return -11314 - i;
  25492. }
  25493. }
  25494. /* Little-endian fail cases */
  25495. for (i = 0; i < (int)(sizeof(fail_le) / sizeof(*fail_le)); i++) {
  25496. if (wc_curve448_check_public(fail_le[i], CURVE448_KEY_SIZE,
  25497. EC448_LITTLE_ENDIAN) == 0) {
  25498. return -11324 - i;
  25499. }
  25500. }
  25501. /* Big-endian fail cases */
  25502. for (i = 0; i < (int)(sizeof(fail_be) / sizeof(*fail_be)); i++) {
  25503. if (wc_curve448_check_public(fail_be[i], CURVE448_KEY_SIZE,
  25504. EC448_BIG_ENDIAN) == 0) {
  25505. return -11334 - i;
  25506. }
  25507. }
  25508. /* Check a valid public value works! */
  25509. if (wc_curve448_check_public(good, CURVE448_KEY_SIZE,
  25510. EC448_LITTLE_ENDIAN) != 0) {
  25511. return -11344;
  25512. }
  25513. if (wc_curve448_check_public(good, CURVE448_KEY_SIZE,
  25514. EC448_BIG_ENDIAN) != 0) {
  25515. return -11345;
  25516. }
  25517. return 0;
  25518. }
  25519. #endif /* HAVE_CURVE448_SHARED_SECRET && HAVE_CURVE448_KEY_IMPORT */
  25520. WOLFSSL_TEST_SUBROUTINE int curve448_test(void)
  25521. {
  25522. WC_RNG rng;
  25523. int ret;
  25524. #ifdef HAVE_CURVE448_SHARED_SECRET
  25525. byte sharedA[CURVE448_KEY_SIZE];
  25526. byte sharedB[CURVE448_KEY_SIZE];
  25527. word32 y;
  25528. #endif
  25529. #ifdef HAVE_CURVE448_KEY_EXPORT
  25530. byte exportBuf[CURVE448_KEY_SIZE];
  25531. #endif
  25532. word32 x;
  25533. curve448_key userA, userB, pubKey;
  25534. #if defined(HAVE_CURVE448_SHARED_SECRET) && \
  25535. defined(HAVE_CURVE448_KEY_IMPORT)
  25536. /* test vectors from
  25537. https://www.rfc-editor.org/rfc/rfc7748.html
  25538. */
  25539. /* secret key for party a */
  25540. byte sa[] = {
  25541. 0x6b, 0x72, 0x98, 0xa5, 0xc0, 0xd8, 0xc2, 0x9a,
  25542. 0x1d, 0xab, 0x27, 0xf1, 0xa6, 0x82, 0x63, 0x00,
  25543. 0x91, 0x73, 0x89, 0x44, 0x97, 0x41, 0xa9, 0x74,
  25544. 0xf5, 0xba, 0xc9, 0xd9, 0x8d, 0xc2, 0x98, 0xd4,
  25545. 0x65, 0x55, 0xbc, 0xe8, 0xba, 0xe8, 0x9e, 0xee,
  25546. 0xd4, 0x00, 0x58, 0x4b, 0xb0, 0x46, 0xcf, 0x75,
  25547. 0x57, 0x9f, 0x51, 0xd1, 0x25, 0x49, 0x8f, 0x9a,
  25548. };
  25549. /* public key for party a */
  25550. byte pa[] = {
  25551. 0xa0, 0x1f, 0xc4, 0x32, 0xe5, 0x80, 0x7f, 0x17,
  25552. 0x53, 0x0d, 0x12, 0x88, 0xda, 0x12, 0x5b, 0x0c,
  25553. 0xd4, 0x53, 0xd9, 0x41, 0x72, 0x64, 0x36, 0xc8,
  25554. 0xbb, 0xd9, 0xc5, 0x22, 0x2c, 0x3d, 0xa7, 0xfa,
  25555. 0x63, 0x9c, 0xe0, 0x3d, 0xb8, 0xd2, 0x3b, 0x27,
  25556. 0x4a, 0x07, 0x21, 0xa1, 0xae, 0xd5, 0x22, 0x7d,
  25557. 0xe6, 0xe3, 0xb7, 0x31, 0xcc, 0xf7, 0x08, 0x9b,
  25558. };
  25559. /* secret key for party b */
  25560. byte sb[] = {
  25561. 0x2d, 0x99, 0x73, 0x51, 0xb6, 0x10, 0x6f, 0x36,
  25562. 0xb0, 0xd1, 0x09, 0x1b, 0x92, 0x9c, 0x4c, 0x37,
  25563. 0x21, 0x3e, 0x0d, 0x2b, 0x97, 0xe8, 0x5e, 0xbb,
  25564. 0x20, 0xc1, 0x27, 0x69, 0x1d, 0x0d, 0xad, 0x8f,
  25565. 0x1d, 0x81, 0x75, 0xb0, 0x72, 0x37, 0x45, 0xe6,
  25566. 0x39, 0xa3, 0xcb, 0x70, 0x44, 0x29, 0x0b, 0x99,
  25567. 0xe0, 0xe2, 0xa0, 0xc2, 0x7a, 0x6a, 0x30, 0x1c,
  25568. };
  25569. /* public key for party b */
  25570. byte pb[] = {
  25571. 0x09, 0x36, 0xf3, 0x7b, 0xc6, 0xc1, 0xbd, 0x07,
  25572. 0xae, 0x3d, 0xec, 0x7a, 0xb5, 0xdc, 0x06, 0xa7,
  25573. 0x3c, 0xa1, 0x32, 0x42, 0xfb, 0x34, 0x3e, 0xfc,
  25574. 0x72, 0xb9, 0xd8, 0x27, 0x30, 0xb4, 0x45, 0xf3,
  25575. 0xd4, 0xb0, 0xbd, 0x07, 0x71, 0x62, 0xa4, 0x6d,
  25576. 0xcf, 0xec, 0x6f, 0x9b, 0x59, 0x0b, 0xfc, 0xbc,
  25577. 0xf5, 0x20, 0xcd, 0xb0, 0x29, 0xa8, 0xb7, 0x3e,
  25578. };
  25579. /* expected shared key */
  25580. byte ss[] = {
  25581. 0x9d, 0x87, 0x4a, 0x51, 0x37, 0x50, 0x9a, 0x44,
  25582. 0x9a, 0xd5, 0x85, 0x30, 0x40, 0x24, 0x1c, 0x52,
  25583. 0x36, 0x39, 0x54, 0x35, 0xc3, 0x64, 0x24, 0xfd,
  25584. 0x56, 0x0b, 0x0c, 0xb6, 0x2b, 0x28, 0x1d, 0x28,
  25585. 0x52, 0x75, 0xa7, 0x40, 0xce, 0x32, 0xa2, 0x2d,
  25586. 0xd1, 0x74, 0x0f, 0x4a, 0xa9, 0x16, 0x1c, 0xec,
  25587. 0x95, 0xcc, 0xc6, 0x1a, 0x18, 0xf4, 0xff, 0x07,
  25588. };
  25589. #endif /* HAVE_CURVE448_SHARED_SECRET */
  25590. (void)x;
  25591. #ifndef HAVE_FIPS
  25592. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  25593. #else
  25594. ret = wc_InitRng(&rng);
  25595. #endif
  25596. if (ret != 0)
  25597. return -11400;
  25598. wc_curve448_init(&userA);
  25599. wc_curve448_init(&userB);
  25600. wc_curve448_init(&pubKey);
  25601. /* make curve448 keys */
  25602. if (wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &userA) != 0)
  25603. return -11401;
  25604. if (wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &userB) != 0)
  25605. return -11402;
  25606. #ifdef HAVE_CURVE448_SHARED_SECRET
  25607. /* find shared secret key */
  25608. x = sizeof(sharedA);
  25609. if (wc_curve448_shared_secret(&userA, &userB, sharedA, &x) != 0)
  25610. return -11403;
  25611. y = sizeof(sharedB);
  25612. if (wc_curve448_shared_secret(&userB, &userA, sharedB, &y) != 0)
  25613. return -11404;
  25614. /* compare shared secret keys to test they are the same */
  25615. if (y != x)
  25616. return -11405;
  25617. if (XMEMCMP(sharedA, sharedB, x))
  25618. return -11406;
  25619. #endif
  25620. #ifdef HAVE_CURVE448_KEY_EXPORT
  25621. /* export a public key and import it for another user */
  25622. x = sizeof(exportBuf);
  25623. if (wc_curve448_export_public(&userA, exportBuf, &x) != 0)
  25624. return -11407;
  25625. #ifdef HAVE_CURVE448_KEY_IMPORT
  25626. if (wc_curve448_import_public(exportBuf, x, &pubKey) != 0)
  25627. return -11408;
  25628. #endif
  25629. #endif
  25630. #if defined(HAVE_CURVE448_SHARED_SECRET) && \
  25631. defined(HAVE_CURVE448_KEY_IMPORT)
  25632. /* test shared key after importing a public key */
  25633. XMEMSET(sharedB, 0, sizeof(sharedB));
  25634. y = sizeof(sharedB);
  25635. if (wc_curve448_shared_secret(&userB, &pubKey, sharedB, &y) != 0)
  25636. return -11409;
  25637. if (XMEMCMP(sharedA, sharedB, y))
  25638. return -11410;
  25639. /* import RFC test vectors and compare shared key */
  25640. if (wc_curve448_import_private_raw(sa, sizeof(sa), pa, sizeof(pa), &userA)
  25641. != 0)
  25642. return -11411;
  25643. if (wc_curve448_import_private_raw(sb, sizeof(sb), pb, sizeof(pb), &userB)
  25644. != 0)
  25645. return -11412;
  25646. /* test against known test vector */
  25647. XMEMSET(sharedB, 0, sizeof(sharedB));
  25648. y = sizeof(sharedB);
  25649. if (wc_curve448_shared_secret(&userA, &userB, sharedB, &y) != 0)
  25650. return -11413;
  25651. if (XMEMCMP(ss, sharedB, y))
  25652. return -11414;
  25653. /* test swapping roles of keys and generating same shared key */
  25654. XMEMSET(sharedB, 0, sizeof(sharedB));
  25655. y = sizeof(sharedB);
  25656. if (wc_curve448_shared_secret(&userB, &userA, sharedB, &y) != 0)
  25657. return -11415;
  25658. if (XMEMCMP(ss, sharedB, y))
  25659. return -11416;
  25660. /* test with 1 generated key and 1 from known test vector */
  25661. if (wc_curve448_import_private_raw(sa, sizeof(sa), pa, sizeof(pa), &userA)
  25662. != 0)
  25663. return -11417;
  25664. if (wc_curve448_make_key(&rng, 56, &userB) != 0)
  25665. return -11418;
  25666. x = sizeof(sharedA);
  25667. if (wc_curve448_shared_secret(&userA, &userB, sharedA, &x) != 0)
  25668. return -11419;
  25669. y = sizeof(sharedB);
  25670. if (wc_curve448_shared_secret(&userB, &userA, sharedB, &y) != 0)
  25671. return -11420;
  25672. /* compare shared secret keys to test they are the same */
  25673. if (y != x)
  25674. return -11421;
  25675. if (XMEMCMP(sharedA, sharedB, x))
  25676. return -11422;
  25677. ret = curve448_check_public_test();
  25678. if (ret != 0)
  25679. return ret;
  25680. #endif /* HAVE_CURVE448_SHARED_SECRET && HAVE_CURVE448_KEY_IMPORT */
  25681. /* clean up keys when done */
  25682. wc_curve448_free(&pubKey);
  25683. wc_curve448_free(&userB);
  25684. wc_curve448_free(&userA);
  25685. wc_FreeRng(&rng);
  25686. return 0;
  25687. }
  25688. #endif /* HAVE_CURVE448 */
  25689. #ifdef HAVE_ED448
  25690. #ifdef WOLFSSL_TEST_CERT
  25691. static int ed448_test_cert(void)
  25692. {
  25693. DecodedCert cert[2];
  25694. DecodedCert* serverCert = NULL;
  25695. DecodedCert* caCert = NULL;
  25696. #ifdef HAVE_ED448_VERIFY
  25697. ed448_key key;
  25698. ed448_key* pubKey = NULL;
  25699. int verify;
  25700. #endif /* HAVE_ED448_VERIFY */
  25701. int ret;
  25702. byte* tmp;
  25703. size_t bytes;
  25704. XFILE file;
  25705. tmp = (byte *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25706. if (tmp == NULL) {
  25707. ERROR_OUT(-11430, done);
  25708. }
  25709. #ifdef USE_CERT_BUFFERS_256
  25710. XMEMCPY(tmp, ca_ed448_cert, sizeof_ca_ed448_cert);
  25711. bytes = sizeof_ca_ed448_cert;
  25712. #elif !defined(NO_FILESYSTEM)
  25713. file = XFOPEN(caEd448Cert, "rb");
  25714. if (file == NULL) {
  25715. ERROR_OUT(-11431, done);
  25716. }
  25717. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  25718. XFCLOSE(file);
  25719. #else
  25720. /* No certificate to use. */
  25721. ERROR_OUT(-11432, done);
  25722. #endif
  25723. InitDecodedCert(&cert[0], tmp, (word32)bytes, 0);
  25724. caCert = &cert[0];
  25725. ret = ParseCert(caCert, CERT_TYPE, NO_VERIFY, NULL);
  25726. if (ret != 0) {
  25727. ERROR_OUT(-11433, done);
  25728. }
  25729. #ifdef USE_CERT_BUFFERS_256
  25730. XMEMCPY(tmp, server_ed448_cert, sizeof_server_ed448_cert);
  25731. bytes = sizeof_server_ed448_cert;
  25732. #elif !defined(NO_FILESYSTEM)
  25733. file = XFOPEN(serverEd448Cert, "rb");
  25734. if (file == NULL) {
  25735. ERROR_OUT(-11434, done);
  25736. }
  25737. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  25738. XFCLOSE(file);
  25739. #else
  25740. /* No certificate to use. */
  25741. ERROR_OUT(-11435, done);
  25742. #endif
  25743. InitDecodedCert(&cert[1], tmp, (word32)bytes, 0);
  25744. serverCert = &cert[1];
  25745. ret = ParseCert(serverCert, CERT_TYPE, NO_VERIFY, NULL);
  25746. if (ret != 0) {
  25747. ERROR_OUT(-11436, done);
  25748. }
  25749. #ifdef HAVE_ED448_VERIFY
  25750. ret = wc_ed448_init(&key);
  25751. if (ret < 0) {
  25752. ERROR_OUT(-11437, done);
  25753. }
  25754. pubKey = &key;
  25755. ret = wc_ed448_import_public(caCert->publicKey, caCert->pubKeySize, pubKey);
  25756. if (ret < 0) {
  25757. ERROR_OUT(-11438, done);
  25758. }
  25759. if (wc_ed448_verify_msg(serverCert->signature, serverCert->sigLength,
  25760. serverCert->source + serverCert->certBegin,
  25761. serverCert->sigIndex - serverCert->certBegin,
  25762. &verify, pubKey, NULL, 0) < 0 || verify != 1) {
  25763. ERROR_OUT(-11439, done);
  25764. }
  25765. #endif /* HAVE_ED448_VERIFY */
  25766. done:
  25767. if (tmp != NULL)
  25768. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25769. #ifdef HAVE_ED448_VERIFY
  25770. wc_ed448_free(pubKey);
  25771. #endif /* HAVE_ED448_VERIFY */
  25772. if (caCert != NULL)
  25773. FreeDecodedCert(caCert);
  25774. if (serverCert != NULL)
  25775. FreeDecodedCert(serverCert);
  25776. return ret;
  25777. }
  25778. static int ed448_test_make_cert(void)
  25779. {
  25780. WC_RNG rng;
  25781. Cert cert;
  25782. DecodedCert decode;
  25783. ed448_key key;
  25784. ed448_key* privKey = NULL;
  25785. int ret = 0;
  25786. byte* tmp = NULL;
  25787. wc_InitCert_ex(&cert, HEAP_HINT, devId);
  25788. #ifndef HAVE_FIPS
  25789. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  25790. #else
  25791. ret = wc_InitRng(&rng);
  25792. #endif
  25793. if (ret != 0)
  25794. return -11450;
  25795. wc_ed448_init(&key);
  25796. privKey = &key;
  25797. wc_ed448_make_key(&rng, ED448_KEY_SIZE, privKey);
  25798. cert.daysValid = 365 * 2;
  25799. cert.selfSigned = 1;
  25800. XMEMCPY(&cert.issuer, &certDefaultName, sizeof(CertName));
  25801. XMEMCPY(&cert.subject, &certDefaultName, sizeof(CertName));
  25802. cert.isCA = 0;
  25803. #ifdef WOLFSSL_CERT_EXT
  25804. ret = wc_SetKeyUsage(&cert, certKeyUsage);
  25805. if (ret < 0) {
  25806. ERROR_OUT(-11451, done);
  25807. }
  25808. ret = wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ED448_TYPE, privKey);
  25809. if (ret < 0) {
  25810. ERROR_OUT(-11452, done);
  25811. }
  25812. ret = wc_SetAuthKeyIdFromPublicKey_ex(&cert, ED448_TYPE, privKey);
  25813. if (ret < 0) {
  25814. ERROR_OUT(-11453, done);
  25815. }
  25816. #endif
  25817. tmp = (byte *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25818. if (tmp == NULL) {
  25819. ERROR_OUT(-11454, done);
  25820. }
  25821. cert.sigType = CTC_ED448;
  25822. ret = wc_MakeCert_ex(&cert, tmp, FOURK_BUF, ED448_TYPE, privKey, &rng);
  25823. if (ret < 0) {
  25824. ERROR_OUT(-11455, done);
  25825. }
  25826. ret = wc_SignCert_ex(cert.bodySz, cert.sigType, tmp, FOURK_BUF, ED448_TYPE,
  25827. privKey, &rng);
  25828. if (ret < 0) {
  25829. ERROR_OUT(-11456, done);
  25830. }
  25831. InitDecodedCert(&decode, tmp, ret, HEAP_HINT);
  25832. ret = ParseCert(&decode, CERT_TYPE, NO_VERIFY, 0);
  25833. FreeDecodedCert(&decode);
  25834. if (ret != 0) {
  25835. ERROR_OUT(-11457, done);
  25836. }
  25837. done:
  25838. if (tmp != NULL)
  25839. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25840. wc_ed448_free(privKey);
  25841. wc_FreeRng(&rng);
  25842. return ret;
  25843. }
  25844. #endif /* WOLFSSL_TEST_CERT */
  25845. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_EXPORT) && \
  25846. defined(HAVE_ED448_KEY_IMPORT)
  25847. static int ed448_ctx_test(void)
  25848. {
  25849. byte out[ED448_SIG_SIZE];
  25850. word32 outlen;
  25851. #ifdef HAVE_ED448_VERIFY
  25852. int verify;
  25853. #endif /* HAVE_ED448_VERIFY */
  25854. ed448_key key;
  25855. WOLFSSL_SMALL_STACK_STATIC const byte sKeyCtx[] = {
  25856. 0xc4, 0xea, 0xb0, 0x5d, 0x35, 0x70, 0x07, 0xc6,
  25857. 0x32, 0xf3, 0xdb, 0xb4, 0x84, 0x89, 0x92, 0x4d,
  25858. 0x55, 0x2b, 0x08, 0xfe, 0x0c, 0x35, 0x3a, 0x0d,
  25859. 0x4a, 0x1f, 0x00, 0xac, 0xda, 0x2c, 0x46, 0x3a,
  25860. 0xfb, 0xea, 0x67, 0xc5, 0xe8, 0xd2, 0x87, 0x7c,
  25861. 0x5e, 0x3b, 0xc3, 0x97, 0xa6, 0x59, 0x94, 0x9e,
  25862. 0xf8, 0x02, 0x1e, 0x95, 0x4e, 0x0a, 0x12, 0x27,
  25863. 0x4e
  25864. };
  25865. WOLFSSL_SMALL_STACK_STATIC const byte pKeyCtx[] = {
  25866. 0x43, 0xba, 0x28, 0xf4, 0x30, 0xcd, 0xff, 0x45,
  25867. 0x6a, 0xe5, 0x31, 0x54, 0x5f, 0x7e, 0xcd, 0x0a,
  25868. 0xc8, 0x34, 0xa5, 0x5d, 0x93, 0x58, 0xc0, 0x37,
  25869. 0x2b, 0xfa, 0x0c, 0x6c, 0x67, 0x98, 0xc0, 0x86,
  25870. 0x6a, 0xea, 0x01, 0xeb, 0x00, 0x74, 0x28, 0x02,
  25871. 0xb8, 0x43, 0x8e, 0xa4, 0xcb, 0x82, 0x16, 0x9c,
  25872. 0x23, 0x51, 0x60, 0x62, 0x7b, 0x4c, 0x3a, 0x94,
  25873. 0x80
  25874. };
  25875. WOLFSSL_SMALL_STACK_STATIC const byte sigCtx[] = {
  25876. 0xd4, 0xf8, 0xf6, 0x13, 0x17, 0x70, 0xdd, 0x46,
  25877. 0xf4, 0x08, 0x67, 0xd6, 0xfd, 0x5d, 0x50, 0x55,
  25878. 0xde, 0x43, 0x54, 0x1f, 0x8c, 0x5e, 0x35, 0xab,
  25879. 0xbc, 0xd0, 0x01, 0xb3, 0x2a, 0x89, 0xf7, 0xd2,
  25880. 0x15, 0x1f, 0x76, 0x47, 0xf1, 0x1d, 0x8c, 0xa2,
  25881. 0xae, 0x27, 0x9f, 0xb8, 0x42, 0xd6, 0x07, 0x21,
  25882. 0x7f, 0xce, 0x6e, 0x04, 0x2f, 0x68, 0x15, 0xea,
  25883. 0x00, 0x0c, 0x85, 0x74, 0x1d, 0xe5, 0xc8, 0xda,
  25884. 0x11, 0x44, 0xa6, 0xa1, 0xab, 0xa7, 0xf9, 0x6d,
  25885. 0xe4, 0x25, 0x05, 0xd7, 0xa7, 0x29, 0x85, 0x24,
  25886. 0xfd, 0xa5, 0x38, 0xfc, 0xcb, 0xbb, 0x75, 0x4f,
  25887. 0x57, 0x8c, 0x1c, 0xad, 0x10, 0xd5, 0x4d, 0x0d,
  25888. 0x54, 0x28, 0x40, 0x7e, 0x85, 0xdc, 0xbc, 0x98,
  25889. 0xa4, 0x91, 0x55, 0xc1, 0x37, 0x64, 0xe6, 0x6c,
  25890. 0x3c, 0x00
  25891. };
  25892. WOLFSSL_SMALL_STACK_STATIC const byte msgCtx[] = {
  25893. 0x03
  25894. };
  25895. WOLFSSL_SMALL_STACK_STATIC const byte contextCtx[] = {
  25896. 0x66,0x6f,0x6f
  25897. };
  25898. outlen = sizeof(out);
  25899. XMEMSET(out, 0, sizeof(out));
  25900. if (wc_ed448_init_ex(&key, HEAP_HINT, devId) != 0)
  25901. return -11500;
  25902. if (wc_ed448_import_private_key(sKeyCtx, ED448_KEY_SIZE, pKeyCtx,
  25903. sizeof(pKeyCtx), &key) != 0)
  25904. return -11501;
  25905. if (wc_ed448_sign_msg(msgCtx, sizeof(msgCtx), out, &outlen, &key,
  25906. contextCtx, sizeof(contextCtx)) != 0)
  25907. return -11502;
  25908. if (XMEMCMP(out, sigCtx, sizeof(sigCtx)))
  25909. return -11503;
  25910. #if defined(HAVE_ED448_VERIFY)
  25911. /* test verify on good msg */
  25912. if (wc_ed448_verify_msg(out, outlen, msgCtx, sizeof(msgCtx), &verify, &key,
  25913. contextCtx, sizeof(contextCtx)) != 0 || verify != 1)
  25914. return -11504;
  25915. #endif
  25916. wc_ed448_free(&key);
  25917. return 0;
  25918. }
  25919. static int ed448ph_test(void)
  25920. {
  25921. byte out[ED448_SIG_SIZE];
  25922. word32 outlen;
  25923. #ifdef HAVE_ED448_VERIFY
  25924. int verify;
  25925. #endif /* HAVE_ED448_VERIFY */
  25926. ed448_key key;
  25927. WOLFSSL_SMALL_STACK_STATIC const byte sKeyPh[] = {
  25928. 0x83, 0x3f, 0xe6, 0x24, 0x09, 0x23, 0x7b, 0x9d,
  25929. 0x62, 0xec, 0x77, 0x58, 0x75, 0x20, 0x91, 0x1e,
  25930. 0x9a, 0x75, 0x9c, 0xec, 0x1d, 0x19, 0x75, 0x5b,
  25931. 0x7d, 0xa9, 0x01, 0xb9, 0x6d, 0xca, 0x3d, 0x42,
  25932. 0xef, 0x78, 0x22, 0xe0, 0xd5, 0x10, 0x41, 0x27,
  25933. 0xdc, 0x05, 0xd6, 0xdb, 0xef, 0xde, 0x69, 0xe3,
  25934. 0xab, 0x2c, 0xec, 0x7c, 0x86, 0x7c, 0x6e, 0x2c,
  25935. 0x49
  25936. };
  25937. WOLFSSL_SMALL_STACK_STATIC const byte pKeyPh[] = {
  25938. 0x25, 0x9b, 0x71, 0xc1, 0x9f, 0x83, 0xef, 0x77,
  25939. 0xa7, 0xab, 0xd2, 0x65, 0x24, 0xcb, 0xdb, 0x31,
  25940. 0x61, 0xb5, 0x90, 0xa4, 0x8f, 0x7d, 0x17, 0xde,
  25941. 0x3e, 0xe0, 0xba, 0x9c, 0x52, 0xbe, 0xb7, 0x43,
  25942. 0xc0, 0x94, 0x28, 0xa1, 0x31, 0xd6, 0xb1, 0xb5,
  25943. 0x73, 0x03, 0xd9, 0x0d, 0x81, 0x32, 0xc2, 0x76,
  25944. 0xd5, 0xed, 0x3d, 0x5d, 0x01, 0xc0, 0xf5, 0x38,
  25945. 0x80
  25946. };
  25947. WOLFSSL_SMALL_STACK_STATIC const byte sigPh1[] = {
  25948. 0x82, 0x2f, 0x69, 0x01, 0xf7, 0x48, 0x0f, 0x3d,
  25949. 0x5f, 0x56, 0x2c, 0x59, 0x29, 0x94, 0xd9, 0x69,
  25950. 0x36, 0x02, 0x87, 0x56, 0x14, 0x48, 0x32, 0x56,
  25951. 0x50, 0x56, 0x00, 0xbb, 0xc2, 0x81, 0xae, 0x38,
  25952. 0x1f, 0x54, 0xd6, 0xbc, 0xe2, 0xea, 0x91, 0x15,
  25953. 0x74, 0x93, 0x2f, 0x52, 0xa4, 0xe6, 0xca, 0xdd,
  25954. 0x78, 0x76, 0x93, 0x75, 0xec, 0x3f, 0xfd, 0x1b,
  25955. 0x80, 0x1a, 0x0d, 0x9b, 0x3f, 0x40, 0x30, 0xcd,
  25956. 0x43, 0x39, 0x64, 0xb6, 0x45, 0x7e, 0xa3, 0x94,
  25957. 0x76, 0x51, 0x12, 0x14, 0xf9, 0x74, 0x69, 0xb5,
  25958. 0x7d, 0xd3, 0x2d, 0xbc, 0x56, 0x0a, 0x9a, 0x94,
  25959. 0xd0, 0x0b, 0xff, 0x07, 0x62, 0x04, 0x64, 0xa3,
  25960. 0xad, 0x20, 0x3d, 0xf7, 0xdc, 0x7c, 0xe3, 0x60,
  25961. 0xc3, 0xcd, 0x36, 0x96, 0xd9, 0xd9, 0xfa, 0xb9,
  25962. 0x0f, 0x00
  25963. };
  25964. WOLFSSL_SMALL_STACK_STATIC const byte sigPh2[] = {
  25965. 0xc3, 0x22, 0x99, 0xd4, 0x6e, 0xc8, 0xff, 0x02,
  25966. 0xb5, 0x45, 0x40, 0x98, 0x28, 0x14, 0xdc, 0xe9,
  25967. 0xa0, 0x58, 0x12, 0xf8, 0x19, 0x62, 0xb6, 0x49,
  25968. 0xd5, 0x28, 0x09, 0x59, 0x16, 0xa2, 0xaa, 0x48,
  25969. 0x10, 0x65, 0xb1, 0x58, 0x04, 0x23, 0xef, 0x92,
  25970. 0x7e, 0xcf, 0x0a, 0xf5, 0x88, 0x8f, 0x90, 0xda,
  25971. 0x0f, 0x6a, 0x9a, 0x85, 0xad, 0x5d, 0xc3, 0xf2,
  25972. 0x80, 0xd9, 0x12, 0x24, 0xba, 0x99, 0x11, 0xa3,
  25973. 0x65, 0x3d, 0x00, 0xe4, 0x84, 0xe2, 0xce, 0x23,
  25974. 0x25, 0x21, 0x48, 0x1c, 0x86, 0x58, 0xdf, 0x30,
  25975. 0x4b, 0xb7, 0x74, 0x5a, 0x73, 0x51, 0x4c, 0xdb,
  25976. 0x9b, 0xf3, 0xe1, 0x57, 0x84, 0xab, 0x71, 0x28,
  25977. 0x4f, 0x8d, 0x07, 0x04, 0xa6, 0x08, 0xc5, 0x4a,
  25978. 0x6b, 0x62, 0xd9, 0x7b, 0xeb, 0x51, 0x1d, 0x13,
  25979. 0x21, 0x00
  25980. };
  25981. WOLFSSL_SMALL_STACK_STATIC const byte msgPh[] = {
  25982. 0x61,0x62,0x63
  25983. };
  25984. /* SHA-512 hash of msgPh */
  25985. WOLFSSL_SMALL_STACK_STATIC const byte hashPh[] = {
  25986. 0x48, 0x33, 0x66, 0x60, 0x13, 0x60, 0xa8, 0x77,
  25987. 0x1c, 0x68, 0x63, 0x08, 0x0c, 0xc4, 0x11, 0x4d,
  25988. 0x8d, 0xb4, 0x45, 0x30, 0xf8, 0xf1, 0xe1, 0xee,
  25989. 0x4f, 0x94, 0xea, 0x37, 0xe7, 0x8b, 0x57, 0x39,
  25990. 0xd5, 0xa1, 0x5b, 0xef, 0x18, 0x6a, 0x53, 0x86,
  25991. 0xc7, 0x57, 0x44, 0xc0, 0x52, 0x7e, 0x1f, 0xaa,
  25992. 0x9f, 0x87, 0x26, 0xe4, 0x62, 0xa1, 0x2a, 0x4f,
  25993. 0xeb, 0x06, 0xbd, 0x88, 0x01, 0xe7, 0x51, 0xe4
  25994. };
  25995. WOLFSSL_SMALL_STACK_STATIC const byte contextPh2[] = {
  25996. 0x66,0x6f,0x6f
  25997. };
  25998. outlen = sizeof(out);
  25999. XMEMSET(out, 0, sizeof(out));
  26000. if (wc_ed448_init_ex(&key, HEAP_HINT, devId) != 0)
  26001. return -11600;
  26002. if (wc_ed448_import_private_key(sKeyPh, ED448_KEY_SIZE, pKeyPh,
  26003. sizeof(pKeyPh), &key) != 0) {
  26004. return -11601;
  26005. }
  26006. if (wc_ed448ph_sign_msg(msgPh, sizeof(msgPh), out, &outlen, &key, NULL,
  26007. 0) != 0) {
  26008. return -11602;
  26009. }
  26010. if (XMEMCMP(out, sigPh1, sizeof(sigPh1)))
  26011. return -11603;
  26012. #if defined(HAVE_ED448_VERIFY)
  26013. /* test verify on good msg */
  26014. if (wc_ed448ph_verify_msg(out, outlen, msgPh, sizeof(msgPh), &verify, &key,
  26015. NULL, 0) != 0 || verify != 1) {
  26016. return -11604;
  26017. }
  26018. #endif
  26019. if (wc_ed448ph_sign_msg(msgPh, sizeof(msgPh), out, &outlen, &key,
  26020. contextPh2, sizeof(contextPh2)) != 0) {
  26021. return -11605;
  26022. }
  26023. if (XMEMCMP(out, sigPh2, sizeof(sigPh2)))
  26024. return -11606;
  26025. #if defined(HAVE_ED448_VERIFY)
  26026. /* test verify on good msg */
  26027. if (wc_ed448ph_verify_msg(out, outlen, msgPh, sizeof(msgPh), &verify, &key,
  26028. contextPh2, sizeof(contextPh2)) != 0 ||
  26029. verify != 1) {
  26030. return -11607;
  26031. }
  26032. #endif
  26033. if (wc_ed448ph_sign_hash(hashPh, sizeof(hashPh), out, &outlen, &key, NULL,
  26034. 0) != 0) {
  26035. return -11608;
  26036. }
  26037. if (XMEMCMP(out, sigPh1, sizeof(sigPh1)))
  26038. return -11609;
  26039. #if defined(HAVE_ED448_VERIFY)
  26040. if (wc_ed448ph_verify_hash(out, outlen, hashPh, sizeof(hashPh), &verify,
  26041. &key, NULL, 0) != 0 || verify != 1) {
  26042. return -11610;
  26043. }
  26044. #endif
  26045. if (wc_ed448ph_sign_hash(hashPh, sizeof(hashPh), out, &outlen, &key,
  26046. contextPh2, sizeof(contextPh2)) != 0) {
  26047. return -11611;
  26048. }
  26049. if (XMEMCMP(out, sigPh2, sizeof(sigPh2)))
  26050. return -11612;
  26051. #if defined(HAVE_ED448_VERIFY)
  26052. if (wc_ed448ph_verify_hash(out, outlen, hashPh, sizeof(hashPh), &verify,
  26053. &key, contextPh2, sizeof(contextPh2)) != 0 ||
  26054. verify != 1) {
  26055. return -11613;
  26056. }
  26057. #endif
  26058. wc_ed448_free(&key);
  26059. return 0;
  26060. }
  26061. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_EXPORT && HAVE_ED448_KEY_IMPORT */
  26062. WOLFSSL_TEST_SUBROUTINE int ed448_test(void)
  26063. {
  26064. int ret;
  26065. WC_RNG rng;
  26066. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_EXPORT) &&\
  26067. defined(HAVE_ED448_KEY_IMPORT)
  26068. byte out[ED448_SIG_SIZE];
  26069. int i;
  26070. word32 outlen;
  26071. #ifdef HAVE_ED448_VERIFY
  26072. #ifdef WOLFSSL_ED448_STREAMING_VERIFY
  26073. int j;
  26074. #endif /* WOLFSSL_ED448_STREAMING_VERIFY */
  26075. int verify;
  26076. #endif /* HAVE_ED448_VERIFY */
  26077. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_EXPORT && HAVE_ED448_KEY_IMPORT */
  26078. word32 keySz, sigSz;
  26079. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  26080. ed448_key *key = NULL;
  26081. ed448_key *key2 = NULL;
  26082. #else
  26083. ed448_key key[1];
  26084. ed448_key key2[1];
  26085. #endif
  26086. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_EXPORT) && \
  26087. defined(HAVE_ED448_KEY_IMPORT)
  26088. /* test vectors from
  26089. https://tools.ietf.org/html/rfc8032
  26090. */
  26091. WOLFSSL_SMALL_STACK_STATIC const byte sKey1[] = {
  26092. 0x6c, 0x82, 0xa5, 0x62, 0xcb, 0x80, 0x8d, 0x10,
  26093. 0xd6, 0x32, 0xbe, 0x89, 0xc8, 0x51, 0x3e, 0xbf,
  26094. 0x6c, 0x92, 0x9f, 0x34, 0xdd, 0xfa, 0x8c, 0x9f,
  26095. 0x63, 0xc9, 0x96, 0x0e, 0xf6, 0xe3, 0x48, 0xa3,
  26096. 0x52, 0x8c, 0x8a, 0x3f, 0xcc, 0x2f, 0x04, 0x4e,
  26097. 0x39, 0xa3, 0xfc, 0x5b, 0x94, 0x49, 0x2f, 0x8f,
  26098. 0x03, 0x2e, 0x75, 0x49, 0xa2, 0x00, 0x98, 0xf9,
  26099. 0x5b
  26100. };
  26101. WOLFSSL_SMALL_STACK_STATIC const byte sKey2[] = {
  26102. 0xc4, 0xea, 0xb0, 0x5d, 0x35, 0x70, 0x07, 0xc6,
  26103. 0x32, 0xf3, 0xdb, 0xb4, 0x84, 0x89, 0x92, 0x4d,
  26104. 0x55, 0x2b, 0x08, 0xfe, 0x0c, 0x35, 0x3a, 0x0d,
  26105. 0x4a, 0x1f, 0x00, 0xac, 0xda, 0x2c, 0x46, 0x3a,
  26106. 0xfb, 0xea, 0x67, 0xc5, 0xe8, 0xd2, 0x87, 0x7c,
  26107. 0x5e, 0x3b, 0xc3, 0x97, 0xa6, 0x59, 0x94, 0x9e,
  26108. 0xf8, 0x02, 0x1e, 0x95, 0x4e, 0x0a, 0x12, 0x27,
  26109. 0x4e
  26110. };
  26111. WOLFSSL_SMALL_STACK_STATIC const byte sKey3[] = {
  26112. 0x25, 0x8c, 0xdd, 0x4a, 0xda, 0x32, 0xed, 0x9c,
  26113. 0x9f, 0xf5, 0x4e, 0x63, 0x75, 0x6a, 0xe5, 0x82,
  26114. 0xfb, 0x8f, 0xab, 0x2a, 0xc7, 0x21, 0xf2, 0xc8,
  26115. 0xe6, 0x76, 0xa7, 0x27, 0x68, 0x51, 0x3d, 0x93,
  26116. 0x9f, 0x63, 0xdd, 0xdb, 0x55, 0x60, 0x91, 0x33,
  26117. 0xf2, 0x9a, 0xdf, 0x86, 0xec, 0x99, 0x29, 0xdc,
  26118. 0xcb, 0x52, 0xc1, 0xc5, 0xfd, 0x2f, 0xf7, 0xe2,
  26119. 0x1b
  26120. };
  26121. /* uncompressed test */
  26122. WOLFSSL_SMALL_STACK_STATIC const byte sKey4[] = {
  26123. 0x6c, 0x82, 0xa5, 0x62, 0xcb, 0x80, 0x8d, 0x10,
  26124. 0xd6, 0x32, 0xbe, 0x89, 0xc8, 0x51, 0x3e, 0xbf,
  26125. 0x6c, 0x92, 0x9f, 0x34, 0xdd, 0xfa, 0x8c, 0x9f,
  26126. 0x63, 0xc9, 0x96, 0x0e, 0xf6, 0xe3, 0x48, 0xa3,
  26127. 0x52, 0x8c, 0x8a, 0x3f, 0xcc, 0x2f, 0x04, 0x4e,
  26128. 0x39, 0xa3, 0xfc, 0x5b, 0x94, 0x49, 0x2f, 0x8f,
  26129. 0x03, 0x2e, 0x75, 0x49, 0xa2, 0x00, 0x98, 0xf9,
  26130. 0x5b
  26131. };
  26132. /* compressed prefix test */
  26133. WOLFSSL_SMALL_STACK_STATIC const byte sKey5[] = {
  26134. 0x6c, 0x82, 0xa5, 0x62, 0xcb, 0x80, 0x8d, 0x10,
  26135. 0xd6, 0x32, 0xbe, 0x89, 0xc8, 0x51, 0x3e, 0xbf,
  26136. 0x6c, 0x92, 0x9f, 0x34, 0xdd, 0xfa, 0x8c, 0x9f,
  26137. 0x63, 0xc9, 0x96, 0x0e, 0xf6, 0xe3, 0x48, 0xa3,
  26138. 0x52, 0x8c, 0x8a, 0x3f, 0xcc, 0x2f, 0x04, 0x4e,
  26139. 0x39, 0xa3, 0xfc, 0x5b, 0x94, 0x49, 0x2f, 0x8f,
  26140. 0x03, 0x2e, 0x75, 0x49, 0xa2, 0x00, 0x98, 0xf9,
  26141. 0x5b
  26142. };
  26143. WOLFSSL_SMALL_STACK_STATIC const byte sKey6[] = {
  26144. 0x87, 0x2d, 0x09, 0x37, 0x80, 0xf5, 0xd3, 0x73,
  26145. 0x0d, 0xf7, 0xc2, 0x12, 0x66, 0x4b, 0x37, 0xb8,
  26146. 0xa0, 0xf2, 0x4f, 0x56, 0x81, 0x0d, 0xaa, 0x83,
  26147. 0x82, 0xcd, 0x4f, 0xa3, 0xf7, 0x76, 0x34, 0xec,
  26148. 0x44, 0xdc, 0x54, 0xf1, 0xc2, 0xed, 0x9b, 0xea,
  26149. 0x86, 0xfa, 0xfb, 0x76, 0x32, 0xd8, 0xbe, 0x19,
  26150. 0x9e, 0xa1, 0x65, 0xf5, 0xad, 0x55, 0xdd, 0x9c,
  26151. 0xe8
  26152. };
  26153. WOLFSSL_SMALL_STACK_STATIC const byte* sKeys[] = {sKey1, sKey2, sKey3, sKey4, sKey5, sKey6};
  26154. WOLFSSL_SMALL_STACK_STATIC const byte pKey1[] = {
  26155. 0x5f, 0xd7, 0x44, 0x9b, 0x59, 0xb4, 0x61, 0xfd,
  26156. 0x2c, 0xe7, 0x87, 0xec, 0x61, 0x6a, 0xd4, 0x6a,
  26157. 0x1d, 0xa1, 0x34, 0x24, 0x85, 0xa7, 0x0e, 0x1f,
  26158. 0x8a, 0x0e, 0xa7, 0x5d, 0x80, 0xe9, 0x67, 0x78,
  26159. 0xed, 0xf1, 0x24, 0x76, 0x9b, 0x46, 0xc7, 0x06,
  26160. 0x1b, 0xd6, 0x78, 0x3d, 0xf1, 0xe5, 0x0f, 0x6c,
  26161. 0xd1, 0xfa, 0x1a, 0xbe, 0xaf, 0xe8, 0x25, 0x61,
  26162. 0x80
  26163. };
  26164. WOLFSSL_SMALL_STACK_STATIC const byte pKey2[] = {
  26165. 0x43, 0xba, 0x28, 0xf4, 0x30, 0xcd, 0xff, 0x45,
  26166. 0x6a, 0xe5, 0x31, 0x54, 0x5f, 0x7e, 0xcd, 0x0a,
  26167. 0xc8, 0x34, 0xa5, 0x5d, 0x93, 0x58, 0xc0, 0x37,
  26168. 0x2b, 0xfa, 0x0c, 0x6c, 0x67, 0x98, 0xc0, 0x86,
  26169. 0x6a, 0xea, 0x01, 0xeb, 0x00, 0x74, 0x28, 0x02,
  26170. 0xb8, 0x43, 0x8e, 0xa4, 0xcb, 0x82, 0x16, 0x9c,
  26171. 0x23, 0x51, 0x60, 0x62, 0x7b, 0x4c, 0x3a, 0x94,
  26172. 0x80
  26173. };
  26174. WOLFSSL_SMALL_STACK_STATIC const byte pKey3[] = {
  26175. 0x3b, 0xa1, 0x6d, 0xa0, 0xc6, 0xf2, 0xcc, 0x1f,
  26176. 0x30, 0x18, 0x77, 0x40, 0x75, 0x6f, 0x5e, 0x79,
  26177. 0x8d, 0x6b, 0xc5, 0xfc, 0x01, 0x5d, 0x7c, 0x63,
  26178. 0xcc, 0x95, 0x10, 0xee, 0x3f, 0xd4, 0x4a, 0xdc,
  26179. 0x24, 0xd8, 0xe9, 0x68, 0xb6, 0xe4, 0x6e, 0x6f,
  26180. 0x94, 0xd1, 0x9b, 0x94, 0x53, 0x61, 0x72, 0x6b,
  26181. 0xd7, 0x5e, 0x14, 0x9e, 0xf0, 0x98, 0x17, 0xf5,
  26182. 0x80
  26183. };
  26184. /* uncompressed test */
  26185. WOLFSSL_SMALL_STACK_STATIC const byte pKey4[] = {
  26186. 0x5f, 0xd7, 0x44, 0x9b, 0x59, 0xb4, 0x61, 0xfd,
  26187. 0x2c, 0xe7, 0x87, 0xec, 0x61, 0x6a, 0xd4, 0x6a,
  26188. 0x1d, 0xa1, 0x34, 0x24, 0x85, 0xa7, 0x0e, 0x1f,
  26189. 0x8a, 0x0e, 0xa7, 0x5d, 0x80, 0xe9, 0x67, 0x78,
  26190. 0xed, 0xf1, 0x24, 0x76, 0x9b, 0x46, 0xc7, 0x06,
  26191. 0x1b, 0xd6, 0x78, 0x3d, 0xf1, 0xe5, 0x0f, 0x6c,
  26192. 0xd1, 0xfa, 0x1a, 0xbe, 0xaf, 0xe8, 0x25, 0x61,
  26193. 0x80
  26194. };
  26195. /* compressed prefix */
  26196. WOLFSSL_SMALL_STACK_STATIC const byte pKey5[] = {
  26197. 0x5f, 0xd7, 0x44, 0x9b, 0x59, 0xb4, 0x61, 0xfd,
  26198. 0x2c, 0xe7, 0x87, 0xec, 0x61, 0x6a, 0xd4, 0x6a,
  26199. 0x1d, 0xa1, 0x34, 0x24, 0x85, 0xa7, 0x0e, 0x1f,
  26200. 0x8a, 0x0e, 0xa7, 0x5d, 0x80, 0xe9, 0x67, 0x78,
  26201. 0xed, 0xf1, 0x24, 0x76, 0x9b, 0x46, 0xc7, 0x06,
  26202. 0x1b, 0xd6, 0x78, 0x3d, 0xf1, 0xe5, 0x0f, 0x6c,
  26203. 0xd1, 0xfa, 0x1a, 0xbe, 0xaf, 0xe8, 0x25, 0x61,
  26204. 0x80
  26205. };
  26206. WOLFSSL_SMALL_STACK_STATIC const byte pKey6[] = {
  26207. 0xa8, 0x1b, 0x2e, 0x8a, 0x70, 0xa5, 0xac, 0x94,
  26208. 0xff, 0xdb, 0xcc, 0x9b, 0xad, 0xfc, 0x3f, 0xeb,
  26209. 0x08, 0x01, 0xf2, 0x58, 0x57, 0x8b, 0xb1, 0x14,
  26210. 0xad, 0x44, 0xec, 0xe1, 0xec, 0x0e, 0x79, 0x9d,
  26211. 0xa0, 0x8e, 0xff, 0xb8, 0x1c, 0x5d, 0x68, 0x5c,
  26212. 0x0c, 0x56, 0xf6, 0x4e, 0xec, 0xae, 0xf8, 0xcd,
  26213. 0xf1, 0x1c, 0xc3, 0x87, 0x37, 0x83, 0x8c, 0xf4,
  26214. 0x00
  26215. };
  26216. WOLFSSL_SMALL_STACK_STATIC const byte* pKeys[] = {pKey1, pKey2, pKey3, pKey4, pKey5, pKey6};
  26217. WOLFSSL_SMALL_STACK_STATIC const byte pKeySz[] = {sizeof(pKey1), sizeof(pKey2), sizeof(pKey3),
  26218. sizeof(pKey4), sizeof(pKey5), sizeof(pKey6)};
  26219. WOLFSSL_SMALL_STACK_STATIC const byte sig1[] = {
  26220. 0x53, 0x3a, 0x37, 0xf6, 0xbb, 0xe4, 0x57, 0x25,
  26221. 0x1f, 0x02, 0x3c, 0x0d, 0x88, 0xf9, 0x76, 0xae,
  26222. 0x2d, 0xfb, 0x50, 0x4a, 0x84, 0x3e, 0x34, 0xd2,
  26223. 0x07, 0x4f, 0xd8, 0x23, 0xd4, 0x1a, 0x59, 0x1f,
  26224. 0x2b, 0x23, 0x3f, 0x03, 0x4f, 0x62, 0x82, 0x81,
  26225. 0xf2, 0xfd, 0x7a, 0x22, 0xdd, 0xd4, 0x7d, 0x78,
  26226. 0x28, 0xc5, 0x9b, 0xd0, 0xa2, 0x1b, 0xfd, 0x39,
  26227. 0x80, 0xff, 0x0d, 0x20, 0x28, 0xd4, 0xb1, 0x8a,
  26228. 0x9d, 0xf6, 0x3e, 0x00, 0x6c, 0x5d, 0x1c, 0x2d,
  26229. 0x34, 0x5b, 0x92, 0x5d, 0x8d, 0xc0, 0x0b, 0x41,
  26230. 0x04, 0x85, 0x2d, 0xb9, 0x9a, 0xc5, 0xc7, 0xcd,
  26231. 0xda, 0x85, 0x30, 0xa1, 0x13, 0xa0, 0xf4, 0xdb,
  26232. 0xb6, 0x11, 0x49, 0xf0, 0x5a, 0x73, 0x63, 0x26,
  26233. 0x8c, 0x71, 0xd9, 0x58, 0x08, 0xff, 0x2e, 0x65,
  26234. 0x26, 0x00
  26235. };
  26236. WOLFSSL_SMALL_STACK_STATIC const byte sig2[] = {
  26237. 0x26, 0xb8, 0xf9, 0x17, 0x27, 0xbd, 0x62, 0x89,
  26238. 0x7a, 0xf1, 0x5e, 0x41, 0xeb, 0x43, 0xc3, 0x77,
  26239. 0xef, 0xb9, 0xc6, 0x10, 0xd4, 0x8f, 0x23, 0x35,
  26240. 0xcb, 0x0b, 0xd0, 0x08, 0x78, 0x10, 0xf4, 0x35,
  26241. 0x25, 0x41, 0xb1, 0x43, 0xc4, 0xb9, 0x81, 0xb7,
  26242. 0xe1, 0x8f, 0x62, 0xde, 0x8c, 0xcd, 0xf6, 0x33,
  26243. 0xfc, 0x1b, 0xf0, 0x37, 0xab, 0x7c, 0xd7, 0x79,
  26244. 0x80, 0x5e, 0x0d, 0xbc, 0xc0, 0xaa, 0xe1, 0xcb,
  26245. 0xce, 0xe1, 0xaf, 0xb2, 0xe0, 0x27, 0xdf, 0x36,
  26246. 0xbc, 0x04, 0xdc, 0xec, 0xbf, 0x15, 0x43, 0x36,
  26247. 0xc1, 0x9f, 0x0a, 0xf7, 0xe0, 0xa6, 0x47, 0x29,
  26248. 0x05, 0xe7, 0x99, 0xf1, 0x95, 0x3d, 0x2a, 0x0f,
  26249. 0xf3, 0x34, 0x8a, 0xb2, 0x1a, 0xa4, 0xad, 0xaf,
  26250. 0xd1, 0xd2, 0x34, 0x44, 0x1c, 0xf8, 0x07, 0xc0,
  26251. 0x3a, 0x00
  26252. };
  26253. WOLFSSL_SMALL_STACK_STATIC const byte sig3[] = {
  26254. 0x7e, 0xee, 0xab, 0x7c, 0x4e, 0x50, 0xfb, 0x79,
  26255. 0x9b, 0x41, 0x8e, 0xe5, 0xe3, 0x19, 0x7f, 0xf6,
  26256. 0xbf, 0x15, 0xd4, 0x3a, 0x14, 0xc3, 0x43, 0x89,
  26257. 0xb5, 0x9d, 0xd1, 0xa7, 0xb1, 0xb8, 0x5b, 0x4a,
  26258. 0xe9, 0x04, 0x38, 0xac, 0xa6, 0x34, 0xbe, 0xa4,
  26259. 0x5e, 0x3a, 0x26, 0x95, 0xf1, 0x27, 0x0f, 0x07,
  26260. 0xfd, 0xcd, 0xf7, 0xc6, 0x2b, 0x8e, 0xfe, 0xaf,
  26261. 0x00, 0xb4, 0x5c, 0x2c, 0x96, 0xba, 0x45, 0x7e,
  26262. 0xb1, 0xa8, 0xbf, 0x07, 0x5a, 0x3d, 0xb2, 0x8e,
  26263. 0x5c, 0x24, 0xf6, 0xb9, 0x23, 0xed, 0x4a, 0xd7,
  26264. 0x47, 0xc3, 0xc9, 0xe0, 0x3c, 0x70, 0x79, 0xef,
  26265. 0xb8, 0x7c, 0xb1, 0x10, 0xd3, 0xa9, 0x98, 0x61,
  26266. 0xe7, 0x20, 0x03, 0xcb, 0xae, 0x6d, 0x6b, 0x8b,
  26267. 0x82, 0x7e, 0x4e, 0x6c, 0x14, 0x30, 0x64, 0xff,
  26268. 0x3c, 0x00
  26269. };
  26270. /* uncompressed test */
  26271. WOLFSSL_SMALL_STACK_STATIC const byte sig4[] = {
  26272. 0x53, 0x3a, 0x37, 0xf6, 0xbb, 0xe4, 0x57, 0x25,
  26273. 0x1f, 0x02, 0x3c, 0x0d, 0x88, 0xf9, 0x76, 0xae,
  26274. 0x2d, 0xfb, 0x50, 0x4a, 0x84, 0x3e, 0x34, 0xd2,
  26275. 0x07, 0x4f, 0xd8, 0x23, 0xd4, 0x1a, 0x59, 0x1f,
  26276. 0x2b, 0x23, 0x3f, 0x03, 0x4f, 0x62, 0x82, 0x81,
  26277. 0xf2, 0xfd, 0x7a, 0x22, 0xdd, 0xd4, 0x7d, 0x78,
  26278. 0x28, 0xc5, 0x9b, 0xd0, 0xa2, 0x1b, 0xfd, 0x39,
  26279. 0x80, 0xff, 0x0d, 0x20, 0x28, 0xd4, 0xb1, 0x8a,
  26280. 0x9d, 0xf6, 0x3e, 0x00, 0x6c, 0x5d, 0x1c, 0x2d,
  26281. 0x34, 0x5b, 0x92, 0x5d, 0x8d, 0xc0, 0x0b, 0x41,
  26282. 0x04, 0x85, 0x2d, 0xb9, 0x9a, 0xc5, 0xc7, 0xcd,
  26283. 0xda, 0x85, 0x30, 0xa1, 0x13, 0xa0, 0xf4, 0xdb,
  26284. 0xb6, 0x11, 0x49, 0xf0, 0x5a, 0x73, 0x63, 0x26,
  26285. 0x8c, 0x71, 0xd9, 0x58, 0x08, 0xff, 0x2e, 0x65,
  26286. 0x26, 0x00
  26287. };
  26288. /* compressed prefix */
  26289. WOLFSSL_SMALL_STACK_STATIC const byte sig5[] = {
  26290. 0x53, 0x3a, 0x37, 0xf6, 0xbb, 0xe4, 0x57, 0x25,
  26291. 0x1f, 0x02, 0x3c, 0x0d, 0x88, 0xf9, 0x76, 0xae,
  26292. 0x2d, 0xfb, 0x50, 0x4a, 0x84, 0x3e, 0x34, 0xd2,
  26293. 0x07, 0x4f, 0xd8, 0x23, 0xd4, 0x1a, 0x59, 0x1f,
  26294. 0x2b, 0x23, 0x3f, 0x03, 0x4f, 0x62, 0x82, 0x81,
  26295. 0xf2, 0xfd, 0x7a, 0x22, 0xdd, 0xd4, 0x7d, 0x78,
  26296. 0x28, 0xc5, 0x9b, 0xd0, 0xa2, 0x1b, 0xfd, 0x39,
  26297. 0x80, 0xff, 0x0d, 0x20, 0x28, 0xd4, 0xb1, 0x8a,
  26298. 0x9d, 0xf6, 0x3e, 0x00, 0x6c, 0x5d, 0x1c, 0x2d,
  26299. 0x34, 0x5b, 0x92, 0x5d, 0x8d, 0xc0, 0x0b, 0x41,
  26300. 0x04, 0x85, 0x2d, 0xb9, 0x9a, 0xc5, 0xc7, 0xcd,
  26301. 0xda, 0x85, 0x30, 0xa1, 0x13, 0xa0, 0xf4, 0xdb,
  26302. 0xb6, 0x11, 0x49, 0xf0, 0x5a, 0x73, 0x63, 0x26,
  26303. 0x8c, 0x71, 0xd9, 0x58, 0x08, 0xff, 0x2e, 0x65,
  26304. 0x26, 0x00
  26305. };
  26306. WOLFSSL_SMALL_STACK_STATIC const byte sig6[] = {
  26307. 0xe3, 0x01, 0x34, 0x5a, 0x41, 0xa3, 0x9a, 0x4d,
  26308. 0x72, 0xff, 0xf8, 0xdf, 0x69, 0xc9, 0x80, 0x75,
  26309. 0xa0, 0xcc, 0x08, 0x2b, 0x80, 0x2f, 0xc9, 0xb2,
  26310. 0xb6, 0xbc, 0x50, 0x3f, 0x92, 0x6b, 0x65, 0xbd,
  26311. 0xdf, 0x7f, 0x4c, 0x8f, 0x1c, 0xb4, 0x9f, 0x63,
  26312. 0x96, 0xaf, 0xc8, 0xa7, 0x0a, 0xbe, 0x6d, 0x8a,
  26313. 0xef, 0x0d, 0xb4, 0x78, 0xd4, 0xc6, 0xb2, 0x97,
  26314. 0x00, 0x76, 0xc6, 0xa0, 0x48, 0x4f, 0xe7, 0x6d,
  26315. 0x76, 0xb3, 0xa9, 0x76, 0x25, 0xd7, 0x9f, 0x1c,
  26316. 0xe2, 0x40, 0xe7, 0xc5, 0x76, 0x75, 0x0d, 0x29,
  26317. 0x55, 0x28, 0x28, 0x6f, 0x71, 0x9b, 0x41, 0x3d,
  26318. 0xe9, 0xad, 0xa3, 0xe8, 0xeb, 0x78, 0xed, 0x57,
  26319. 0x36, 0x03, 0xce, 0x30, 0xd8, 0xbb, 0x76, 0x17,
  26320. 0x85, 0xdc, 0x30, 0xdb, 0xc3, 0x20, 0x86, 0x9e,
  26321. 0x1a, 0x00
  26322. };
  26323. WOLFSSL_SMALL_STACK_STATIC const byte* sigs[] = {sig1, sig2, sig3, sig4, sig5, sig6};
  26324. #define SIGSZ sizeof(sig1)
  26325. PEDANTIC_EXTENSION WOLFSSL_SMALL_STACK_STATIC const byte msg1[] = { };
  26326. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] = { 0x03 };
  26327. WOLFSSL_SMALL_STACK_STATIC const byte msg3[] = { 0x64, 0xa6, 0x5f, 0x3c, 0xde, 0xdc, 0xdd,
  26328. 0x66, 0x81, 0x1e, 0x29, 0x15 };
  26329. /* test of a 1023 byte long message */
  26330. WOLFSSL_SMALL_STACK_STATIC const byte msg4[] = {
  26331. 0x6d, 0xdf, 0x80, 0x2e, 0x1a, 0xae, 0x49, 0x86,
  26332. 0x93, 0x5f, 0x7f, 0x98, 0x1b, 0xa3, 0xf0, 0x35,
  26333. 0x1d, 0x62, 0x73, 0xc0, 0xa0, 0xc2, 0x2c, 0x9c,
  26334. 0x0e, 0x83, 0x39, 0x16, 0x8e, 0x67, 0x54, 0x12,
  26335. 0xa3, 0xde, 0xbf, 0xaf, 0x43, 0x5e, 0xd6, 0x51,
  26336. 0x55, 0x80, 0x07, 0xdb, 0x43, 0x84, 0xb6, 0x50,
  26337. 0xfc, 0xc0, 0x7e, 0x3b, 0x58, 0x6a, 0x27, 0xa4,
  26338. 0xf7, 0xa0, 0x0a, 0xc8, 0xa6, 0xfe, 0xc2, 0xcd,
  26339. 0x86, 0xae, 0x4b, 0xf1, 0x57, 0x0c, 0x41, 0xe6,
  26340. 0xa4, 0x0c, 0x93, 0x1d, 0xb2, 0x7b, 0x2f, 0xaa,
  26341. 0x15, 0xa8, 0xce, 0xdd, 0x52, 0xcf, 0xf7, 0x36,
  26342. 0x2c, 0x4e, 0x6e, 0x23, 0xda, 0xec, 0x0f, 0xbc,
  26343. 0x3a, 0x79, 0xb6, 0x80, 0x6e, 0x31, 0x6e, 0xfc,
  26344. 0xc7, 0xb6, 0x81, 0x19, 0xbf, 0x46, 0xbc, 0x76,
  26345. 0xa2, 0x60, 0x67, 0xa5, 0x3f, 0x29, 0x6d, 0xaf,
  26346. 0xdb, 0xdc, 0x11, 0xc7, 0x7f, 0x77, 0x77, 0xe9,
  26347. 0x72, 0x66, 0x0c, 0xf4, 0xb6, 0xa9, 0xb3, 0x69,
  26348. 0xa6, 0x66, 0x5f, 0x02, 0xe0, 0xcc, 0x9b, 0x6e,
  26349. 0xdf, 0xad, 0x13, 0x6b, 0x4f, 0xab, 0xe7, 0x23,
  26350. 0xd2, 0x81, 0x3d, 0xb3, 0x13, 0x6c, 0xfd, 0xe9,
  26351. 0xb6, 0xd0, 0x44, 0x32, 0x2f, 0xee, 0x29, 0x47,
  26352. 0x95, 0x2e, 0x03, 0x1b, 0x73, 0xab, 0x5c, 0x60,
  26353. 0x33, 0x49, 0xb3, 0x07, 0xbd, 0xc2, 0x7b, 0xc6,
  26354. 0xcb, 0x8b, 0x8b, 0xbd, 0x7b, 0xd3, 0x23, 0x21,
  26355. 0x9b, 0x80, 0x33, 0xa5, 0x81, 0xb5, 0x9e, 0xad,
  26356. 0xeb, 0xb0, 0x9b, 0x3c, 0x4f, 0x3d, 0x22, 0x77,
  26357. 0xd4, 0xf0, 0x34, 0x36, 0x24, 0xac, 0xc8, 0x17,
  26358. 0x80, 0x47, 0x28, 0xb2, 0x5a, 0xb7, 0x97, 0x17,
  26359. 0x2b, 0x4c, 0x5c, 0x21, 0xa2, 0x2f, 0x9c, 0x78,
  26360. 0x39, 0xd6, 0x43, 0x00, 0x23, 0x2e, 0xb6, 0x6e,
  26361. 0x53, 0xf3, 0x1c, 0x72, 0x3f, 0xa3, 0x7f, 0xe3,
  26362. 0x87, 0xc7, 0xd3, 0xe5, 0x0b, 0xdf, 0x98, 0x13,
  26363. 0xa3, 0x0e, 0x5b, 0xb1, 0x2c, 0xf4, 0xcd, 0x93,
  26364. 0x0c, 0x40, 0xcf, 0xb4, 0xe1, 0xfc, 0x62, 0x25,
  26365. 0x92, 0xa4, 0x95, 0x88, 0x79, 0x44, 0x94, 0xd5,
  26366. 0x6d, 0x24, 0xea, 0x4b, 0x40, 0xc8, 0x9f, 0xc0,
  26367. 0x59, 0x6c, 0xc9, 0xeb, 0xb9, 0x61, 0xc8, 0xcb,
  26368. 0x10, 0xad, 0xde, 0x97, 0x6a, 0x5d, 0x60, 0x2b,
  26369. 0x1c, 0x3f, 0x85, 0xb9, 0xb9, 0xa0, 0x01, 0xed,
  26370. 0x3c, 0x6a, 0x4d, 0x3b, 0x14, 0x37, 0xf5, 0x20,
  26371. 0x96, 0xcd, 0x19, 0x56, 0xd0, 0x42, 0xa5, 0x97,
  26372. 0xd5, 0x61, 0xa5, 0x96, 0xec, 0xd3, 0xd1, 0x73,
  26373. 0x5a, 0x8d, 0x57, 0x0e, 0xa0, 0xec, 0x27, 0x22,
  26374. 0x5a, 0x2c, 0x4a, 0xaf, 0xf2, 0x63, 0x06, 0xd1,
  26375. 0x52, 0x6c, 0x1a, 0xf3, 0xca, 0x6d, 0x9c, 0xf5,
  26376. 0xa2, 0xc9, 0x8f, 0x47, 0xe1, 0xc4, 0x6d, 0xb9,
  26377. 0xa3, 0x32, 0x34, 0xcf, 0xd4, 0xd8, 0x1f, 0x2c,
  26378. 0x98, 0x53, 0x8a, 0x09, 0xeb, 0xe7, 0x69, 0x98,
  26379. 0xd0, 0xd8, 0xfd, 0x25, 0x99, 0x7c, 0x7d, 0x25,
  26380. 0x5c, 0x6d, 0x66, 0xec, 0xe6, 0xfa, 0x56, 0xf1,
  26381. 0x11, 0x44, 0x95, 0x0f, 0x02, 0x77, 0x95, 0xe6,
  26382. 0x53, 0x00, 0x8f, 0x4b, 0xd7, 0xca, 0x2d, 0xee,
  26383. 0x85, 0xd8, 0xe9, 0x0f, 0x3d, 0xc3, 0x15, 0x13,
  26384. 0x0c, 0xe2, 0xa0, 0x03, 0x75, 0xa3, 0x18, 0xc7,
  26385. 0xc3, 0xd9, 0x7b, 0xe2, 0xc8, 0xce, 0x5b, 0x6d,
  26386. 0xb4, 0x1a, 0x62, 0x54, 0xff, 0x26, 0x4f, 0xa6,
  26387. 0x15, 0x5b, 0xae, 0xe3, 0xb0, 0x77, 0x3c, 0x0f,
  26388. 0x49, 0x7c, 0x57, 0x3f, 0x19, 0xbb, 0x4f, 0x42,
  26389. 0x40, 0x28, 0x1f, 0x0b, 0x1f, 0x4f, 0x7b, 0xe8,
  26390. 0x57, 0xa4, 0xe5, 0x9d, 0x41, 0x6c, 0x06, 0xb4,
  26391. 0xc5, 0x0f, 0xa0, 0x9e, 0x18, 0x10, 0xdd, 0xc6,
  26392. 0xb1, 0x46, 0x7b, 0xae, 0xac, 0x5a, 0x36, 0x68,
  26393. 0xd1, 0x1b, 0x6e, 0xca, 0xa9, 0x01, 0x44, 0x00,
  26394. 0x16, 0xf3, 0x89, 0xf8, 0x0a, 0xcc, 0x4d, 0xb9,
  26395. 0x77, 0x02, 0x5e, 0x7f, 0x59, 0x24, 0x38, 0x8c,
  26396. 0x7e, 0x34, 0x0a, 0x73, 0x2e, 0x55, 0x44, 0x40,
  26397. 0xe7, 0x65, 0x70, 0xf8, 0xdd, 0x71, 0xb7, 0xd6,
  26398. 0x40, 0xb3, 0x45, 0x0d, 0x1f, 0xd5, 0xf0, 0x41,
  26399. 0x0a, 0x18, 0xf9, 0xa3, 0x49, 0x4f, 0x70, 0x7c,
  26400. 0x71, 0x7b, 0x79, 0xb4, 0xbf, 0x75, 0xc9, 0x84,
  26401. 0x00, 0xb0, 0x96, 0xb2, 0x16, 0x53, 0xb5, 0xd2,
  26402. 0x17, 0xcf, 0x35, 0x65, 0xc9, 0x59, 0x74, 0x56,
  26403. 0xf7, 0x07, 0x03, 0x49, 0x7a, 0x07, 0x87, 0x63,
  26404. 0x82, 0x9b, 0xc0, 0x1b, 0xb1, 0xcb, 0xc8, 0xfa,
  26405. 0x04, 0xea, 0xdc, 0x9a, 0x6e, 0x3f, 0x66, 0x99,
  26406. 0x58, 0x7a, 0x9e, 0x75, 0xc9, 0x4e, 0x5b, 0xab,
  26407. 0x00, 0x36, 0xe0, 0xb2, 0xe7, 0x11, 0x39, 0x2c,
  26408. 0xff, 0x00, 0x47, 0xd0, 0xd6, 0xb0, 0x5b, 0xd2,
  26409. 0xa5, 0x88, 0xbc, 0x10, 0x97, 0x18, 0x95, 0x42,
  26410. 0x59, 0xf1, 0xd8, 0x66, 0x78, 0xa5, 0x79, 0xa3,
  26411. 0x12, 0x0f, 0x19, 0xcf, 0xb2, 0x96, 0x3f, 0x17,
  26412. 0x7a, 0xeb, 0x70, 0xf2, 0xd4, 0x84, 0x48, 0x26,
  26413. 0x26, 0x2e, 0x51, 0xb8, 0x02, 0x71, 0x27, 0x20,
  26414. 0x68, 0xef, 0x5b, 0x38, 0x56, 0xfa, 0x85, 0x35,
  26415. 0xaa, 0x2a, 0x88, 0xb2, 0xd4, 0x1f, 0x2a, 0x0e,
  26416. 0x2f, 0xda, 0x76, 0x24, 0xc2, 0x85, 0x02, 0x72,
  26417. 0xac, 0x4a, 0x2f, 0x56, 0x1f, 0x8f, 0x2f, 0x7a,
  26418. 0x31, 0x8b, 0xfd, 0x5c, 0xaf, 0x96, 0x96, 0x14,
  26419. 0x9e, 0x4a, 0xc8, 0x24, 0xad, 0x34, 0x60, 0x53,
  26420. 0x8f, 0xdc, 0x25, 0x42, 0x1b, 0xee, 0xc2, 0xcc,
  26421. 0x68, 0x18, 0x16, 0x2d, 0x06, 0xbb, 0xed, 0x0c,
  26422. 0x40, 0xa3, 0x87, 0x19, 0x23, 0x49, 0xdb, 0x67,
  26423. 0xa1, 0x18, 0xba, 0xda, 0x6c, 0xd5, 0xab, 0x01,
  26424. 0x40, 0xee, 0x27, 0x32, 0x04, 0xf6, 0x28, 0xaa,
  26425. 0xd1, 0xc1, 0x35, 0xf7, 0x70, 0x27, 0x9a, 0x65,
  26426. 0x1e, 0x24, 0xd8, 0xc1, 0x4d, 0x75, 0xa6, 0x05,
  26427. 0x9d, 0x76, 0xb9, 0x6a, 0x6f, 0xd8, 0x57, 0xde,
  26428. 0xf5, 0xe0, 0xb3, 0x54, 0xb2, 0x7a, 0xb9, 0x37,
  26429. 0xa5, 0x81, 0x5d, 0x16, 0xb5, 0xfa, 0xe4, 0x07,
  26430. 0xff, 0x18, 0x22, 0x2c, 0x6d, 0x1e, 0xd2, 0x63,
  26431. 0xbe, 0x68, 0xc9, 0x5f, 0x32, 0xd9, 0x08, 0xbd,
  26432. 0x89, 0x5c, 0xd7, 0x62, 0x07, 0xae, 0x72, 0x64,
  26433. 0x87, 0x56, 0x7f, 0x9a, 0x67, 0xda, 0xd7, 0x9a,
  26434. 0xbe, 0xc3, 0x16, 0xf6, 0x83, 0xb1, 0x7f, 0x2d,
  26435. 0x02, 0xbf, 0x07, 0xe0, 0xac, 0x8b, 0x5b, 0xc6,
  26436. 0x16, 0x2c, 0xf9, 0x46, 0x97, 0xb3, 0xc2, 0x7c,
  26437. 0xd1, 0xfe, 0xa4, 0x9b, 0x27, 0xf2, 0x3b, 0xa2,
  26438. 0x90, 0x18, 0x71, 0x96, 0x25, 0x06, 0x52, 0x0c,
  26439. 0x39, 0x2d, 0xa8, 0xb6, 0xad, 0x0d, 0x99, 0xf7,
  26440. 0x01, 0x3f, 0xbc, 0x06, 0xc2, 0xc1, 0x7a, 0x56,
  26441. 0x95, 0x00, 0xc8, 0xa7, 0x69, 0x64, 0x81, 0xc1,
  26442. 0xcd, 0x33, 0xe9, 0xb1, 0x4e, 0x40, 0xb8, 0x2e,
  26443. 0x79, 0xa5, 0xf5, 0xdb, 0x82, 0x57, 0x1b, 0xa9,
  26444. 0x7b, 0xae, 0x3a, 0xd3, 0xe0, 0x47, 0x95, 0x15,
  26445. 0xbb, 0x0e, 0x2b, 0x0f, 0x3b, 0xfc, 0xd1, 0xfd,
  26446. 0x33, 0x03, 0x4e, 0xfc, 0x62, 0x45, 0xed, 0xdd,
  26447. 0x7e, 0xe2, 0x08, 0x6d, 0xda, 0xe2, 0x60, 0x0d,
  26448. 0x8c, 0xa7, 0x3e, 0x21, 0x4e, 0x8c, 0x2b, 0x0b,
  26449. 0xdb, 0x2b, 0x04, 0x7c, 0x6a, 0x46, 0x4a, 0x56,
  26450. 0x2e, 0xd7, 0x7b, 0x73, 0xd2, 0xd8, 0x41, 0xc4,
  26451. 0xb3, 0x49, 0x73, 0x55, 0x12, 0x57, 0x71, 0x3b,
  26452. 0x75, 0x36, 0x32, 0xef, 0xba, 0x34, 0x81, 0x69,
  26453. 0xab, 0xc9, 0x0a, 0x68, 0xf4, 0x26, 0x11, 0xa4,
  26454. 0x01, 0x26, 0xd7, 0xcb, 0x21, 0xb5, 0x86, 0x95,
  26455. 0x56, 0x81, 0x86, 0xf7, 0xe5, 0x69, 0xd2, 0xff,
  26456. 0x0f, 0x9e, 0x74, 0x5d, 0x04, 0x87, 0xdd, 0x2e,
  26457. 0xb9, 0x97, 0xca, 0xfc, 0x5a, 0xbf, 0x9d, 0xd1,
  26458. 0x02, 0xe6, 0x2f, 0xf6, 0x6c, 0xba, 0x87
  26459. };
  26460. WOLFSSL_SMALL_STACK_STATIC const byte* msgs[] = {msg1, msg2, msg3, msg1, msg1, msg4};
  26461. WOLFSSL_SMALL_STACK_STATIC const word16 msgSz[] = {0 /*sizeof(msg1)*/,
  26462. sizeof(msg2),
  26463. sizeof(msg3),
  26464. 0 /*sizeof(msg1)*/,
  26465. 0 /*sizeof(msg1)*/,
  26466. sizeof(msg4)
  26467. };
  26468. #ifndef NO_ASN
  26469. static const byte privateEd448[] = {
  26470. 0x30, 0x47, 0x02, 0x01, 0x00, 0x30, 0x05, 0x06,
  26471. 0x03, 0x2b, 0x65, 0x71, 0x04, 0x3b, 0x04, 0x39,
  26472. 0x6c, 0x82, 0xa5, 0x62, 0xcb, 0x80, 0x8d, 0x10,
  26473. 0xd6, 0x32, 0xbe, 0x89, 0xc8, 0x51, 0x3e, 0xbf,
  26474. 0x6c, 0x92, 0x9f, 0x34, 0xdd, 0xfa, 0x8c, 0x9f,
  26475. 0x63, 0xc9, 0x96, 0x0e, 0xf6, 0xe3, 0x48, 0xa3,
  26476. 0x52, 0x8c, 0x8a, 0x3f, 0xcc, 0x2f, 0x04, 0x4e,
  26477. 0x39, 0xa3, 0xfc, 0x5b, 0x94, 0x49, 0x2f, 0x8f,
  26478. 0x03, 0x2e, 0x75, 0x49, 0xa2, 0x00, 0x98, 0xf9,
  26479. 0x5b
  26480. };
  26481. static const byte publicEd448[] = {
  26482. 0x30, 0x43, 0x30, 0x05, 0x06, 0x03, 0x2b, 0x65,
  26483. 0x71, 0x03, 0x3a, 0x00, 0x5f, 0xd7, 0x44, 0x9b,
  26484. 0x59, 0xb4, 0x61, 0xfd, 0x2c, 0xe7, 0x87, 0xec,
  26485. 0x61, 0x6a, 0xd4, 0x6a, 0x1d, 0xa1, 0x34, 0x24,
  26486. 0x85, 0xa7, 0x0e, 0x1f, 0x8a, 0x0e, 0xa7, 0x5d,
  26487. 0x80, 0xe9, 0x67, 0x78, 0xed, 0xf1, 0x24, 0x76,
  26488. 0x9b, 0x46, 0xc7, 0x06, 0x1b, 0xd6, 0x78, 0x3d,
  26489. 0xf1, 0xe5, 0x0f, 0x6c, 0xd1, 0xfa, 0x1a, 0xbe,
  26490. 0xaf, 0xe8, 0x25, 0x61, 0x80
  26491. };
  26492. static const byte privPubEd448[] = {
  26493. 0x30, 0x81, 0x82, 0x02, 0x01, 0x00, 0x30, 0x05,
  26494. 0x06, 0x03, 0x2b, 0x65, 0x71, 0x04, 0x3b, 0x04,
  26495. 0x39, 0x6c, 0x82, 0xa5, 0x62, 0xcb, 0x80, 0x8d,
  26496. 0x10, 0xd6, 0x32, 0xbe, 0x89, 0xc8, 0x51, 0x3e,
  26497. 0xbf, 0x6c, 0x92, 0x9f, 0x34, 0xdd, 0xfa, 0x8c,
  26498. 0x9f, 0x63, 0xc9, 0x96, 0x0e, 0xf6, 0xe3, 0x48,
  26499. 0xa3, 0x52, 0x8c, 0x8a, 0x3f, 0xcc, 0x2f, 0x04,
  26500. 0x4e, 0x39, 0xa3, 0xfc, 0x5b, 0x94, 0x49, 0x2f,
  26501. 0x8f, 0x03, 0x2e, 0x75, 0x49, 0xa2, 0x00, 0x98,
  26502. 0xf9, 0x5b, 0x81, 0x39, 0x5f, 0xd7, 0x44, 0x9b,
  26503. 0x59, 0xb4, 0x61, 0xfd, 0x2c, 0xe7, 0x87, 0xec,
  26504. 0x61, 0x6a, 0xd4, 0x6a, 0x1d, 0xa1, 0x34, 0x24,
  26505. 0x85, 0xa7, 0x0e, 0x1f, 0x8a, 0x0e, 0xa7, 0x5d,
  26506. 0x80, 0xe9, 0x67, 0x78, 0xed, 0xf1, 0x24, 0x76,
  26507. 0x9b, 0x46, 0xc7, 0x06, 0x1b, 0xd6, 0x78, 0x3d,
  26508. 0xf1, 0xe5, 0x0f, 0x6c, 0xd1, 0xfa, 0x1a, 0xbe,
  26509. 0xaf, 0xe8, 0x25, 0x61, 0x80
  26510. };
  26511. word32 idx;
  26512. #endif /* NO_ASN */
  26513. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_EXPORT && HAVE_ED448_KEY_IMPORT */
  26514. #if !defined(NO_ASN) && defined(HAVE_ED448_SIGN)
  26515. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  26516. ed448_key *key3 = NULL;
  26517. #else
  26518. ed448_key key3[1];
  26519. #endif
  26520. #endif
  26521. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  26522. key = (ed448_key *)XMALLOC(sizeof(*key), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26523. key2 = (ed448_key *)XMALLOC(sizeof(*key2), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26524. #if !defined(NO_ASN) && defined(HAVE_ED448_SIGN)
  26525. key3 = (ed448_key *)XMALLOC(sizeof(*key3), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26526. #endif
  26527. #endif
  26528. /* create ed448 keys */
  26529. #ifndef HAVE_FIPS
  26530. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  26531. #else
  26532. ret = wc_InitRng(&rng);
  26533. #endif
  26534. if (ret != 0) {
  26535. XMEMSET(&rng, 0, sizeof(rng));
  26536. ERROR_OUT(-11700, out);
  26537. }
  26538. if (wc_ed448_init(key) < 0)
  26539. ERROR_OUT(-11903, out);
  26540. if (wc_ed448_init(key2) < 0)
  26541. ERROR_OUT(-11904, out);
  26542. #if !defined(NO_ASN) && defined(HAVE_ED448_SIGN)
  26543. if (wc_ed448_init(key3) < 0)
  26544. ERROR_OUT(-11905, out);
  26545. #endif
  26546. if (wc_ed448_make_key(&rng, ED448_KEY_SIZE, key) < 0)
  26547. ERROR_OUT(-11906, out);
  26548. if (wc_ed448_make_key(&rng, ED448_KEY_SIZE, key2) < 0)
  26549. ERROR_OUT(-11907, out);
  26550. /* helper functions for signature and key size */
  26551. keySz = wc_ed448_size(key);
  26552. sigSz = wc_ed448_sig_size(key);
  26553. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_EXPORT) &&\
  26554. defined(HAVE_ED448_KEY_IMPORT)
  26555. for (i = 0; i < 6; i++) {
  26556. outlen = sizeof(out);
  26557. XMEMSET(out, 0, sizeof(out));
  26558. if (wc_ed448_import_private_key(sKeys[i], ED448_KEY_SIZE, pKeys[i],
  26559. pKeySz[i], key) != 0)
  26560. ERROR_OUT(-11701 - i, out);
  26561. if (wc_ed448_sign_msg(msgs[i], msgSz[i], out, &outlen, key, NULL,
  26562. 0) != 0)
  26563. ERROR_OUT(-11711 - i, out);
  26564. if (XMEMCMP(out, sigs[i], 114))
  26565. ERROR_OUT(-11721 - i, out);
  26566. #if defined(HAVE_ED448_VERIFY)
  26567. /* test verify on good msg */
  26568. if (wc_ed448_verify_msg(out, outlen, msgs[i], msgSz[i], &verify, key,
  26569. NULL, 0) != 0 || verify != 1)
  26570. ERROR_OUT(-11731 - i, out);
  26571. #ifdef WOLFSSL_ED448_STREAMING_VERIFY
  26572. /* test verify on good msg using streaming interface directly */
  26573. if (wc_ed448_verify_msg_init(out, outlen,
  26574. key, (byte)Ed448, NULL, 0) != 0)
  26575. ERROR_OUT(-11911 - i, out);
  26576. for (j = 0; j < msgSz[i]; j += i) {
  26577. if (wc_ed448_verify_msg_update(msgs[i] + j, MIN(i, msgSz[i] - j), key) != 0)
  26578. ERROR_OUT(-11921 - i, out);
  26579. }
  26580. if (wc_ed448_verify_msg_final(out, outlen, &verify,
  26581. key) != 0 || verify != 1)
  26582. ERROR_OUT(-11931 - i, out);
  26583. #endif /* WOLFSSL_ED448_STREAMING_VERIFY */
  26584. /* test verify on bad msg */
  26585. out[outlen-2] = out[outlen-2] + 1;
  26586. if (wc_ed448_verify_msg(out, outlen, msgs[i], msgSz[i], &verify, key,
  26587. NULL, 0) == 0 || verify == 1)
  26588. ERROR_OUT(-11741 - i, out);
  26589. #endif /* HAVE_ED448_VERIFY */
  26590. /* test api for import/exporting keys */
  26591. {
  26592. byte *exportPKey = NULL;
  26593. byte *exportSKey = NULL;
  26594. word32 exportPSz = ED448_KEY_SIZE;
  26595. word32 exportSSz = ED448_KEY_SIZE;
  26596. exportPKey = (byte *)XMALLOC(exportPSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26597. exportSKey = (byte *)XMALLOC(exportSSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26598. if ((exportPKey == NULL) || (exportSKey == NULL))
  26599. ERROR_OUT(-11902, out);
  26600. ret = 0;
  26601. do {
  26602. if (wc_ed448_export_public(key, exportPKey, &exportPSz) != 0) {
  26603. ret = -11751 - i;
  26604. break;
  26605. }
  26606. if (wc_ed448_import_public_ex(exportPKey, exportPSz, key2, 1) != 0) {
  26607. ret = -11761 - i;
  26608. break;
  26609. }
  26610. if (wc_ed448_export_private_only(key, exportSKey, &exportSSz) != 0) {
  26611. ret = -11771 - i;
  26612. break;
  26613. }
  26614. if (wc_ed448_import_private_key(exportSKey, exportSSz,
  26615. exportPKey, exportPSz, key2) != 0) {
  26616. ret = -11781 - i;
  26617. break;
  26618. }
  26619. /* clear "out" buffer and test sign with imported keys */
  26620. outlen = sizeof(out);
  26621. XMEMSET(out, 0, sizeof(out));
  26622. if (wc_ed448_sign_msg(msgs[i], msgSz[i], out, &outlen, key2, NULL,
  26623. 0) != 0) {
  26624. ret = -11791 - i;
  26625. break;
  26626. }
  26627. } while(0);
  26628. XFREE(exportPKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26629. XFREE(exportSKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26630. if (ret != 0)
  26631. goto out;
  26632. }
  26633. #if defined(HAVE_ED448_VERIFY)
  26634. if (wc_ed448_verify_msg(out, outlen, msgs[i], msgSz[i], &verify, key2,
  26635. NULL, 0) != 0 || verify != 1)
  26636. ERROR_OUT(-11801 - i, out);
  26637. if (XMEMCMP(out, sigs[i], SIGSZ))
  26638. ERROR_OUT(-11811 - i, out);
  26639. #endif /* HAVE_ED448_VERIFY */
  26640. }
  26641. ret = ed448_ctx_test();
  26642. if (ret != 0)
  26643. goto out;
  26644. ret = ed448ph_test();
  26645. if (ret != 0)
  26646. goto out;
  26647. #ifndef NO_ASN
  26648. /* Try ASN.1 encoded private-only key and public key. */
  26649. idx = 0;
  26650. if (wc_Ed448PrivateKeyDecode(privateEd448, &idx, key3,
  26651. sizeof(privateEd448)) != 0)
  26652. ERROR_OUT(-11821, out);
  26653. if (wc_ed448_sign_msg(msgs[0], msgSz[0], out, &outlen, key3, NULL, 0)
  26654. != BAD_FUNC_ARG)
  26655. ERROR_OUT(-11831, out);
  26656. idx = 0;
  26657. if (wc_Ed448PublicKeyDecode(publicEd448, &idx, key3,
  26658. sizeof(publicEd448)) != 0)
  26659. ERROR_OUT(-11841, out);
  26660. if (wc_ed448_sign_msg(msgs[0], msgSz[0], out, &outlen, key3, NULL, 0) != 0)
  26661. ERROR_OUT(-11851, out);
  26662. if (XMEMCMP(out, sigs[0], SIGSZ))
  26663. ERROR_OUT(-11861, out);
  26664. #if defined(HAVE_ED448_VERIFY)
  26665. /* test verify on good msg */
  26666. if (wc_ed448_verify_msg(out, outlen, msgs[0], msgSz[0], &verify, key3,
  26667. NULL, 0) != 0 || verify != 1)
  26668. ERROR_OUT(-11871, out);
  26669. #endif /* HAVE_ED448_VERIFY */
  26670. wc_ed448_free(key3);
  26671. if (wc_ed448_init(key3) < 0)
  26672. ERROR_OUT(-11908, out);
  26673. idx = 0;
  26674. if (wc_Ed448PrivateKeyDecode(privPubEd448, &idx, key3,
  26675. sizeof(privPubEd448)) != 0)
  26676. ERROR_OUT(-11881, out);
  26677. if (wc_ed448_sign_msg(msgs[0], msgSz[0], out, &outlen, key3, NULL, 0) != 0)
  26678. ERROR_OUT(-11891, out);
  26679. if (XMEMCMP(out, sigs[0], SIGSZ))
  26680. ERROR_OUT(-11901, out);
  26681. #endif /* NO_ASN */
  26682. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_EXPORT && HAVE_ED448_KEY_IMPORT */
  26683. ret = 0;
  26684. out:
  26685. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  26686. if (key) {
  26687. wc_ed448_free(key);
  26688. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26689. }
  26690. if (key2) {
  26691. wc_ed448_free(key2);
  26692. XFREE(key2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26693. }
  26694. #if !defined(NO_ASN) && defined(HAVE_ED448_SIGN)
  26695. if (key3) {
  26696. wc_ed448_free(key3);
  26697. XFREE(key3, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26698. }
  26699. #endif
  26700. #else
  26701. wc_ed448_free(key);
  26702. wc_ed448_free(key2);
  26703. #if !defined(NO_ASN) && defined(HAVE_ED448_SIGN)
  26704. wc_ed448_free(key3);
  26705. #endif
  26706. #endif
  26707. #if defined(HAVE_HASHDRBG) || defined(NO_RC4)
  26708. wc_FreeRng(&rng);
  26709. #endif
  26710. if (ret < 0)
  26711. return ret;
  26712. /* hush warnings of unused keySz and sigSz */
  26713. (void)keySz;
  26714. (void)sigSz;
  26715. #ifdef WOLFSSL_TEST_CERT
  26716. ret = ed448_test_cert();
  26717. if (ret < 0)
  26718. return ret;
  26719. #ifdef WOLFSSL_CERT_GEN
  26720. ret = ed448_test_make_cert();
  26721. if (ret < 0)
  26722. return ret;
  26723. #endif /* WOLFSSL_CERT_GEN */
  26724. #endif /* WOLFSSL_TEST_CERT */
  26725. return 0;
  26726. }
  26727. #endif /* HAVE_ED448 */
  26728. #ifdef WOLFSSL_HAVE_KYBER
  26729. #ifdef WOLFSSL_WC_KYBER /* OQS and PQM4 do not support KATs */
  26730. #ifdef WOLFSSL_KYBER512
  26731. static int kyber512_kat(void)
  26732. {
  26733. KyberKey key;
  26734. int ret;
  26735. byte priv[KYBER512_PRIVATE_KEY_SIZE];
  26736. byte pub[KYBER512_PUBLIC_KEY_SIZE];
  26737. byte ct[KYBER512_CIPHER_TEXT_SIZE];
  26738. byte ss[KYBER_SS_SZ];
  26739. byte ss_dec[KYBER_SS_SZ];
  26740. const byte kyber512_rand[] = {
  26741. 0x7c, 0x99, 0x35, 0xa0, 0xb0, 0x76, 0x94, 0xaa,
  26742. 0x0c, 0x6d, 0x10, 0xe4, 0xdb, 0x6b, 0x1a, 0xdd,
  26743. 0x2f, 0xd8, 0x1a, 0x25, 0xcc, 0xb1, 0x48, 0x03,
  26744. 0x2d, 0xcd, 0x73, 0x99, 0x36, 0x73, 0x7f, 0x2d,
  26745. 0x86, 0x26, 0xED, 0x79, 0xD4, 0x51, 0x14, 0x08,
  26746. 0x00, 0xE0, 0x3B, 0x59, 0xB9, 0x56, 0xF8, 0x21,
  26747. 0x0E, 0x55, 0x60, 0x67, 0x40, 0x7D, 0x13, 0xDC,
  26748. 0x90, 0xFA, 0x9E, 0x8B, 0x87, 0x2B, 0xFB, 0x8F
  26749. };
  26750. const byte kyber512enc_rand[] = {
  26751. 0x14, 0x7c, 0x03, 0xf7, 0xa5, 0xbe, 0xbb, 0xa4,
  26752. 0x06, 0xc8, 0xfa, 0xe1, 0x87, 0x4d, 0x7f, 0x13,
  26753. 0xc8, 0x0e, 0xfe, 0x79, 0xa3, 0xa9, 0xa8, 0x74,
  26754. 0xcc, 0x09, 0xfe, 0x76, 0xf6, 0x99, 0x76, 0x15
  26755. };
  26756. #ifndef WOLFSSL_KYBER_90S
  26757. const byte kyber512_pk[] = {
  26758. 0x11, 0x5A, 0xCE, 0x0E, 0x64, 0x67, 0x7C, 0xBB,
  26759. 0x7D, 0xCF, 0xC9, 0x3C, 0x16, 0xD3, 0xA3, 0x05,
  26760. 0xF6, 0x76, 0x15, 0xA4, 0x88, 0xD7, 0x11, 0xAA,
  26761. 0x56, 0x69, 0x8C, 0x56, 0x63, 0xAB, 0x7A, 0xC9,
  26762. 0xCE, 0x66, 0xD5, 0x47, 0xC0, 0x59, 0x5F, 0x98,
  26763. 0xA4, 0x3F, 0x46, 0x50, 0xBB, 0xE0, 0x8C, 0x36,
  26764. 0x4D, 0x97, 0x67, 0x89, 0x11, 0x7D, 0x34, 0xF6,
  26765. 0xAE, 0x51, 0xAC, 0x06, 0x3C, 0xB5, 0x5C, 0x6C,
  26766. 0xA3, 0x25, 0x58, 0x22, 0x7D, 0xFE, 0xF8, 0x07,
  26767. 0xD1, 0x9C, 0x30, 0xDE, 0x41, 0x44, 0x24, 0x09,
  26768. 0x7F, 0x6A, 0xA2, 0x36, 0xA1, 0x05, 0x3B, 0x4A,
  26769. 0x07, 0xA7, 0x6B, 0xE3, 0x72, 0xA5, 0xC6, 0xB6,
  26770. 0x00, 0x27, 0x91, 0xEB, 0xE0, 0xAF, 0xDA, 0xF5,
  26771. 0x4E, 0x1C, 0xA2, 0x37, 0xFF, 0x54, 0x5B, 0xA6,
  26772. 0x83, 0x43, 0xE7, 0x45, 0xC0, 0x4A, 0xD1, 0x63,
  26773. 0x9D, 0xBC, 0x59, 0x03, 0x46, 0xB6, 0xB9, 0x56,
  26774. 0x9B, 0x56, 0xDB, 0xBF, 0xE5, 0x31, 0x51, 0x91,
  26775. 0x30, 0x66, 0xE5, 0xC8, 0x55, 0x27, 0xDC, 0x94,
  26776. 0x68, 0x11, 0x0A, 0x13, 0x6A, 0x41, 0x14, 0x97,
  26777. 0xC2, 0x27, 0xDC, 0xB8, 0xC9, 0xB2, 0x55, 0x70,
  26778. 0xB7, 0xA0, 0xE4, 0x2A, 0xAD, 0xA6, 0x70, 0x9F,
  26779. 0x23, 0x20, 0x8F, 0x5D, 0x49, 0x6E, 0xBA, 0xB7,
  26780. 0x84, 0x3F, 0x64, 0x83, 0xBF, 0x0C, 0x0C, 0x73,
  26781. 0xA4, 0x02, 0x96, 0xEC, 0x2C, 0x64, 0x40, 0x00,
  26782. 0x13, 0x94, 0xC9, 0x9C, 0xA1, 0x73, 0xD5, 0xC7,
  26783. 0x75, 0xB7, 0xF4, 0x15, 0xD0, 0x2A, 0x5A, 0x26,
  26784. 0xA0, 0x74, 0x07, 0x91, 0x85, 0x87, 0xC4, 0x11,
  26785. 0x69, 0xF2, 0xB7, 0x17, 0x87, 0x55, 0xAC, 0xC2,
  26786. 0x7F, 0xC8, 0xB1, 0x9C, 0x4C, 0x4B, 0x3F, 0xCD,
  26787. 0x41, 0x05, 0x3F, 0x2C, 0x74, 0xC8, 0xA1, 0x0A,
  26788. 0x83, 0x21, 0x24, 0x1B, 0x28, 0x02, 0x43, 0x28,
  26789. 0x75, 0xAE, 0x80, 0x8B, 0x9E, 0xF1, 0x36, 0x5C,
  26790. 0x7B, 0x8A, 0x52, 0x90, 0x2F, 0x13, 0x17, 0xBA,
  26791. 0x2F, 0xB0, 0x26, 0x9F, 0x47, 0x93, 0x06, 0x72,
  26792. 0x10, 0x7B, 0x47, 0x26, 0xFE, 0xF6, 0x45, 0x47,
  26793. 0x39, 0x4D, 0x33, 0x20, 0xC8, 0xF1, 0x20, 0xB3,
  26794. 0xC2, 0xF4, 0x72, 0x5B, 0x03, 0x05, 0xFA, 0xB8,
  26795. 0x8C, 0xC7, 0x98, 0x1F, 0xCB, 0x09, 0xA7, 0x6A,
  26796. 0x1C, 0xBF, 0x7F, 0x17, 0x9F, 0x43, 0xBB, 0x0A,
  26797. 0x4C, 0x8B, 0x05, 0x90, 0x85, 0x7F, 0x1E, 0x69,
  26798. 0x70, 0x84, 0x66, 0xC7, 0xF8, 0x60, 0x73, 0x91,
  26799. 0xE7, 0xBC, 0x52, 0x68, 0xBF, 0xD3, 0xD7, 0xA1,
  26800. 0xDF, 0xFC, 0xB4, 0xEC, 0xA2, 0xA1, 0xC9, 0xB5,
  26801. 0x97, 0x59, 0x30, 0x13, 0xD5, 0xFC, 0x42, 0x02,
  26802. 0xEC, 0x2B, 0x74, 0xE5, 0x7A, 0xB7, 0x6B, 0xBC,
  26803. 0xF3, 0x63, 0x2B, 0xBA, 0xF9, 0x7C, 0xDC, 0x41,
  26804. 0x8A, 0x6F, 0x16, 0x39, 0x28, 0x38, 0xCA, 0x9B,
  26805. 0xF4, 0x5D, 0xDF, 0x02, 0x37, 0x77, 0xB7, 0x56,
  26806. 0x18, 0x33, 0xC1, 0x05, 0x19, 0x0F, 0x94, 0xF3,
  26807. 0x02, 0xC5, 0x9B, 0x53, 0x19, 0x00, 0xBB, 0xC8,
  26808. 0x16, 0x36, 0x1F, 0xAA, 0x5B, 0x33, 0x80, 0xCA,
  26809. 0x3A, 0x89, 0x31, 0x04, 0xCA, 0x73, 0x88, 0xB1,
  26810. 0x85, 0x67, 0x1B, 0x3E, 0x5F, 0xE3, 0x79, 0x0E,
  26811. 0x9A, 0x62, 0x6E, 0xC4, 0x6D, 0x9B, 0x0B, 0x33,
  26812. 0xC7, 0xA4, 0x19, 0xAF, 0x7B, 0x32, 0xB6, 0x85,
  26813. 0x98, 0x94, 0xF5, 0x75, 0xD8, 0x2A, 0xC5, 0x45,
  26814. 0x6B, 0x54, 0x90, 0xA7, 0xAF, 0x8F, 0xE6, 0x10,
  26815. 0x46, 0x36, 0x05, 0x89, 0xEC, 0xBA, 0x72, 0x44,
  26816. 0x23, 0x6F, 0x41, 0x23, 0x11, 0x6B, 0x61, 0x74,
  26817. 0xAA, 0x17, 0x92, 0x49, 0xA4, 0x91, 0x95, 0xB3,
  26818. 0x56, 0xC7, 0x2F, 0xC6, 0x64, 0x1F, 0x02, 0x51,
  26819. 0x81, 0x2E, 0xAA, 0x98, 0x57, 0x0B, 0x04, 0x66,
  26820. 0x99, 0x07, 0x0E, 0x08, 0x19, 0xDC, 0x27, 0x13,
  26821. 0xF4, 0x69, 0x13, 0x7D, 0xFC, 0x6A, 0x3D, 0x7B,
  26822. 0x92, 0xB2, 0x98, 0x99, 0x5E, 0xE7, 0x80, 0x36,
  26823. 0x91, 0x53, 0xAC, 0x36, 0x6B, 0x06, 0xD7, 0x24,
  26824. 0x9C, 0xD0, 0x9E, 0x1B, 0x33, 0x78, 0xFB, 0x04,
  26825. 0x39, 0x9C, 0xEC, 0xB8, 0x65, 0x05, 0x81, 0xD6,
  26826. 0x37, 0xC7, 0x9A, 0xE6, 0x7D, 0x6F, 0x2C, 0xAF,
  26827. 0x6A, 0xBA, 0xCF, 0x59, 0x81, 0x59, 0xA7, 0x79,
  26828. 0x2C, 0xB3, 0xC9, 0x71, 0xD1, 0x49, 0x9D, 0x23,
  26829. 0x73, 0xAD, 0x20, 0xF6, 0x3F, 0x03, 0xBB, 0x59,
  26830. 0xED, 0x13, 0x73, 0x84, 0xAC, 0x61, 0xA7, 0x15,
  26831. 0x51, 0x43, 0xB8, 0xCA, 0x49, 0x32, 0x61, 0x2E,
  26832. 0xC9, 0x15, 0xE4, 0xCA, 0x34, 0x6A, 0x9B, 0xCE,
  26833. 0x5D, 0xD6, 0x04, 0x17, 0xC6, 0xB2, 0xA8, 0x9B,
  26834. 0x1C, 0xC4, 0x35, 0x64, 0x3F, 0x87, 0x5B, 0xDC,
  26835. 0x5A, 0x7E, 0x5B, 0x34, 0x81, 0xCF, 0x91, 0x9E,
  26836. 0xA0, 0x91, 0x72, 0xFE, 0xBC, 0x46, 0xD4, 0xFC,
  26837. 0x3F, 0xB0, 0xCB, 0x95, 0x91, 0x70, 0x4E, 0xE2,
  26838. 0xDB, 0xB6, 0x18, 0x44, 0xB2, 0xF3, 0x31, 0x4A,
  26839. 0x06, 0xBB, 0x6C, 0x6D, 0x34, 0x00, 0x5E, 0x48,
  26840. 0x5C, 0xE6, 0x67, 0xBD, 0xC7, 0xD0, 0x98, 0x58,
  26841. 0x69, 0x28, 0xD2, 0xD9, 0x13, 0x40, 0xF0, 0x04,
  26842. 0x19, 0xEA, 0x40, 0x13, 0x51, 0xA2, 0x40, 0xA0,
  26843. 0xB0, 0x41, 0x05, 0x8B, 0xEF, 0xB0, 0xC2, 0xFD,
  26844. 0x32, 0x64, 0x5B, 0x7A, 0x2D, 0xF8, 0xF5, 0xCB,
  26845. 0xFD, 0x87, 0x33, 0x27, 0xC9, 0x78, 0xD7, 0xB3,
  26846. 0x51, 0xA2, 0x80, 0x88, 0x43, 0x88, 0x37, 0x02,
  26847. 0x4C, 0x52, 0xB9, 0xC2, 0x95, 0xCD, 0x71, 0x36,
  26848. 0x46, 0xFB, 0x5D, 0x6C, 0x0C, 0xCF, 0xB4, 0x70,
  26849. 0x73, 0x4A, 0xC2, 0xB2, 0xBC, 0x81, 0x23, 0xC2,
  26850. 0xC1, 0x3D, 0xF6, 0x93, 0x8E, 0x92, 0x45, 0x5A,
  26851. 0x86, 0x26, 0x39, 0xFE, 0xB8, 0xA6, 0x4B, 0x85,
  26852. 0x16, 0x3E, 0x32, 0x70, 0x7E, 0x03, 0x7B, 0x38,
  26853. 0xD8, 0xAC, 0x39, 0x22, 0xB4, 0x51, 0x87, 0xBB,
  26854. 0x65, 0xEA, 0xFD, 0x46, 0x5F, 0xC6, 0x4A, 0x0C,
  26855. 0x5F, 0x8F, 0x3F, 0x90, 0x03, 0x48, 0x94, 0x15,
  26856. 0x89, 0x9D, 0x59, 0xA5, 0x43, 0xD8, 0x20, 0x8C,
  26857. 0x54, 0xA3, 0x16, 0x65, 0x29, 0xB5, 0x39, 0x22
  26858. };
  26859. const byte kyber512_sk[] = {
  26860. 0x6C, 0x89, 0x2B, 0x02, 0x97, 0xA9, 0xC7, 0x64,
  26861. 0x14, 0x93, 0xF8, 0x7D, 0xAF, 0x35, 0x33, 0xEE,
  26862. 0xD6, 0x1F, 0x07, 0xF4, 0x65, 0x20, 0x66, 0x33,
  26863. 0x7E, 0xD7, 0x40, 0x46, 0xDC, 0xC7, 0x1B, 0xA0,
  26864. 0x3F, 0x30, 0x96, 0x01, 0x03, 0x16, 0x1F, 0x7D,
  26865. 0xEB, 0x53, 0xA7, 0x1B, 0x11, 0x61, 0x72, 0x63,
  26866. 0xFE, 0x2A, 0x80, 0x97, 0x69, 0xCE, 0x6D, 0x70,
  26867. 0xA8, 0x5F, 0xE6, 0x00, 0xEC, 0xE2, 0x9D, 0x7F,
  26868. 0x36, 0xA1, 0x6D, 0x33, 0x1B, 0x8B, 0x2A, 0x9E,
  26869. 0x1D, 0xB8, 0xC0, 0x90, 0x74, 0x2D, 0xF0, 0x73,
  26870. 0x9F, 0xF0, 0x60, 0xCE, 0xB4, 0xEC, 0xC5, 0xAB,
  26871. 0x1C, 0x5E, 0x55, 0xAC, 0x97, 0xBB, 0x66, 0xA7,
  26872. 0xF8, 0x95, 0x10, 0x5D, 0x57, 0x78, 0x2B, 0x22,
  26873. 0x95, 0x38, 0xE3, 0x42, 0x15, 0x44, 0xA3, 0x42,
  26874. 0x14, 0x08, 0xDB, 0xF4, 0x49, 0x10, 0x93, 0x4C,
  26875. 0xC4, 0x23, 0x77, 0x4F, 0x16, 0x76, 0xFF, 0x1C,
  26876. 0x30, 0x6F, 0x97, 0x55, 0x5F, 0x57, 0xB4, 0xAE,
  26877. 0xD7, 0xA6, 0xBA, 0xB9, 0x50, 0xA8, 0x16, 0x3C,
  26878. 0x8D, 0x31, 0x8D, 0xEA, 0x62, 0x75, 0x1B, 0xD6,
  26879. 0xAB, 0xC5, 0x06, 0x9C, 0x06, 0xC8, 0x8F, 0x33,
  26880. 0x00, 0x26, 0xA1, 0x98, 0x06, 0xA0, 0x3B, 0x97,
  26881. 0xA7, 0x69, 0x6B, 0x56, 0xDA, 0x21, 0x82, 0x7B,
  26882. 0xB4, 0xE8, 0xDC, 0x03, 0x11, 0x52, 0xB4, 0x1B,
  26883. 0x89, 0x2A, 0x9E, 0x99, 0xAD, 0xF6, 0xE1, 0x96,
  26884. 0x3E, 0x96, 0x57, 0x88, 0x28, 0x15, 0x4F, 0x46,
  26885. 0x70, 0x33, 0x84, 0x69, 0x20, 0xFB, 0xB4, 0xB8,
  26886. 0x05, 0x44, 0xE7, 0xE8, 0xA8, 0x1A, 0xE9, 0x63,
  26887. 0xCF, 0x36, 0x8C, 0x9B, 0xA0, 0x37, 0xA8, 0xC2,
  26888. 0xAD, 0x62, 0xE3, 0x2B, 0x6E, 0x61, 0xC9, 0x1D,
  26889. 0x75, 0xCE, 0x00, 0x5A, 0xB3, 0x0F, 0x80, 0x99,
  26890. 0xA1, 0xF2, 0x9D, 0x7B, 0x63, 0x05, 0xB4, 0xDC,
  26891. 0x06, 0xE2, 0x56, 0x80, 0xBB, 0x00, 0x99, 0x2F,
  26892. 0x71, 0x7F, 0xE6, 0xC1, 0x15, 0xA8, 0x08, 0x42,
  26893. 0x31, 0xCC, 0x79, 0xDD, 0x70, 0x0E, 0xA6, 0x91,
  26894. 0x2A, 0xC7, 0xFA, 0x0D, 0x93, 0x7B, 0xB6, 0xA7,
  26895. 0x56, 0x66, 0x22, 0x30, 0x47, 0x0C, 0x18, 0x9B,
  26896. 0x5A, 0xA1, 0x65, 0x3D, 0xEB, 0x93, 0x7D, 0x5A,
  26897. 0x9C, 0x25, 0xA2, 0x1D, 0x93, 0xB1, 0x90, 0x74,
  26898. 0xFC, 0x23, 0x9D, 0x81, 0x53, 0x53, 0x97, 0x97,
  26899. 0xC7, 0xD4, 0xAB, 0x62, 0x64, 0x9D, 0x76, 0xAA,
  26900. 0x55, 0x37, 0x36, 0xA9, 0x49, 0x02, 0x2C, 0x22,
  26901. 0xC5, 0x2B, 0xAE, 0xEC, 0x60, 0x5B, 0x32, 0xCE,
  26902. 0x9E, 0x5B, 0x93, 0x84, 0x90, 0x35, 0x58, 0xCA,
  26903. 0x9D, 0x6A, 0x3A, 0xBA, 0x90, 0x42, 0x3E, 0xED,
  26904. 0xA0, 0x1C, 0x94, 0x19, 0x8B, 0x19, 0x2A, 0x8B,
  26905. 0xA9, 0x06, 0x34, 0x97, 0xA0, 0xC5, 0x01, 0x33,
  26906. 0x07, 0xDD, 0xD8, 0x63, 0x52, 0x64, 0x71, 0xA4,
  26907. 0xD9, 0x95, 0x23, 0xEB, 0x41, 0x7F, 0x29, 0x1A,
  26908. 0xAC, 0x0C, 0x3A, 0x58, 0x1B, 0x6D, 0xA0, 0x07,
  26909. 0x32, 0xE5, 0xE8, 0x1B, 0x1F, 0x7C, 0x87, 0x9B,
  26910. 0x16, 0x93, 0xC1, 0x3B, 0x6F, 0x9F, 0x79, 0x31,
  26911. 0x62, 0x24, 0x29, 0xE5, 0x42, 0xAF, 0x40, 0x69,
  26912. 0x22, 0x2F, 0x04, 0x55, 0x44, 0xE0, 0xCC, 0x4F,
  26913. 0xB2, 0x4D, 0x44, 0x48, 0xCF, 0x2C, 0x65, 0x96,
  26914. 0xF5, 0xCB, 0x08, 0x62, 0x4B, 0x11, 0x85, 0x01,
  26915. 0x3B, 0x6B, 0x02, 0x08, 0x92, 0xF9, 0x6B, 0xDF,
  26916. 0xD4, 0xAD, 0xA9, 0x17, 0x9D, 0xE7, 0x27, 0xB8,
  26917. 0xD9, 0x42, 0x6E, 0x09, 0x96, 0xB5, 0xD3, 0x49,
  26918. 0x48, 0xCE, 0x02, 0xD0, 0xC3, 0x69, 0xB3, 0x7C,
  26919. 0xBB, 0x54, 0xD3, 0x47, 0x9E, 0xD8, 0xB5, 0x82,
  26920. 0xE9, 0xE7, 0x28, 0x92, 0x9B, 0x4C, 0x71, 0xC9,
  26921. 0xBE, 0x11, 0xD4, 0x5B, 0x20, 0xC4, 0xBD, 0xC3,
  26922. 0xC7, 0x43, 0x13, 0x22, 0x3F, 0x58, 0x27, 0x4E,
  26923. 0x8B, 0xA5, 0x24, 0x44, 0x47, 0xC4, 0x95, 0x95,
  26924. 0x0B, 0x84, 0xCB, 0x0C, 0x3C, 0x27, 0x36, 0x40,
  26925. 0x10, 0x8A, 0x33, 0x97, 0x94, 0x45, 0x73, 0x27,
  26926. 0x93, 0x28, 0x99, 0x6C, 0xDC, 0x0C, 0x91, 0x3C,
  26927. 0x95, 0x8A, 0xD6, 0x20, 0xBA, 0x8B, 0x5E, 0x5E,
  26928. 0xCB, 0xBB, 0x7E, 0x13, 0xCB, 0x9C, 0x70, 0xBD,
  26929. 0x5A, 0xB3, 0x0E, 0xB7, 0x48, 0x8C, 0x97, 0x00,
  26930. 0x1C, 0x20, 0x49, 0x8F, 0x1D, 0x7C, 0xC0, 0x6D,
  26931. 0xA7, 0x6B, 0xF5, 0x20, 0xC6, 0x58, 0xCC, 0xAD,
  26932. 0xFA, 0x29, 0x56, 0x42, 0x45, 0x57, 0xAB, 0xEA,
  26933. 0x8A, 0xB8, 0x92, 0x39, 0xC1, 0x78, 0x33, 0xDC,
  26934. 0x3A, 0x49, 0xB3, 0x6A, 0x9A, 0xE9, 0xA4, 0x86,
  26935. 0x94, 0x05, 0x40, 0xEB, 0x44, 0x4F, 0x97, 0x15,
  26936. 0x23, 0x57, 0xE0, 0x20, 0x35, 0x93, 0x9D, 0x75,
  26937. 0xA3, 0xC0, 0x25, 0xF4, 0x1A, 0x40, 0x08, 0x23,
  26938. 0x82, 0xA0, 0x73, 0x3C, 0x39, 0xB0, 0x62, 0x2B,
  26939. 0x74, 0x0E, 0x40, 0x75, 0x92, 0xC6, 0x2E, 0xCA,
  26940. 0xEB, 0x14, 0x32, 0xC4, 0x45, 0xB3, 0x70, 0x3A,
  26941. 0x86, 0xF6, 0x98, 0x1A, 0x27, 0x81, 0x57, 0xEA,
  26942. 0x95, 0xA6, 0xE9, 0x2D, 0x55, 0xE4, 0xB9, 0x72,
  26943. 0xF9, 0x36, 0xC2, 0xF0, 0xA6, 0x58, 0x28, 0x0E,
  26944. 0xA2, 0xB0, 0x7A, 0x48, 0x99, 0x2D, 0xF8, 0x93,
  26945. 0x7E, 0x0A, 0x2A, 0xC1, 0xDC, 0xC9, 0x74, 0xFE,
  26946. 0x00, 0xAA, 0xE1, 0xF5, 0x61, 0xFA, 0x25, 0x8E,
  26947. 0x2D, 0x25, 0x9C, 0x3E, 0x86, 0x1D, 0xCE, 0x23,
  26948. 0x60, 0x39, 0x12, 0x76, 0x06, 0xFC, 0x1C, 0xE0,
  26949. 0x09, 0x00, 0x3A, 0x7B, 0xAC, 0x94, 0x21, 0x01,
  26950. 0xDC, 0xB8, 0x22, 0xB1, 0xF3, 0xC1, 0x2B, 0xF7,
  26951. 0x32, 0x38, 0xF5, 0x46, 0xE0, 0x1C, 0x36, 0xB5,
  26952. 0xA6, 0x93, 0x61, 0x92, 0x99, 0x5C, 0xC6, 0x9C,
  26953. 0x63, 0x23, 0x74, 0x09, 0xCB, 0x53, 0xC2, 0xE3,
  26954. 0x5D, 0x74, 0x89, 0x0D, 0x18, 0x88, 0x53, 0x76,
  26955. 0xFA, 0x55, 0x03, 0xB1, 0x07, 0xA2, 0xA3, 0x92,
  26956. 0x11, 0x5A, 0xCE, 0x0E, 0x64, 0x67, 0x7C, 0xBB,
  26957. 0x7D, 0xCF, 0xC9, 0x3C, 0x16, 0xD3, 0xA3, 0x05,
  26958. 0xF6, 0x76, 0x15, 0xA4, 0x88, 0xD7, 0x11, 0xAA,
  26959. 0x56, 0x69, 0x8C, 0x56, 0x63, 0xAB, 0x7A, 0xC9,
  26960. 0xCE, 0x66, 0xD5, 0x47, 0xC0, 0x59, 0x5F, 0x98,
  26961. 0xA4, 0x3F, 0x46, 0x50, 0xBB, 0xE0, 0x8C, 0x36,
  26962. 0x4D, 0x97, 0x67, 0x89, 0x11, 0x7D, 0x34, 0xF6,
  26963. 0xAE, 0x51, 0xAC, 0x06, 0x3C, 0xB5, 0x5C, 0x6C,
  26964. 0xA3, 0x25, 0x58, 0x22, 0x7D, 0xFE, 0xF8, 0x07,
  26965. 0xD1, 0x9C, 0x30, 0xDE, 0x41, 0x44, 0x24, 0x09,
  26966. 0x7F, 0x6A, 0xA2, 0x36, 0xA1, 0x05, 0x3B, 0x4A,
  26967. 0x07, 0xA7, 0x6B, 0xE3, 0x72, 0xA5, 0xC6, 0xB6,
  26968. 0x00, 0x27, 0x91, 0xEB, 0xE0, 0xAF, 0xDA, 0xF5,
  26969. 0x4E, 0x1C, 0xA2, 0x37, 0xFF, 0x54, 0x5B, 0xA6,
  26970. 0x83, 0x43, 0xE7, 0x45, 0xC0, 0x4A, 0xD1, 0x63,
  26971. 0x9D, 0xBC, 0x59, 0x03, 0x46, 0xB6, 0xB9, 0x56,
  26972. 0x9B, 0x56, 0xDB, 0xBF, 0xE5, 0x31, 0x51, 0x91,
  26973. 0x30, 0x66, 0xE5, 0xC8, 0x55, 0x27, 0xDC, 0x94,
  26974. 0x68, 0x11, 0x0A, 0x13, 0x6A, 0x41, 0x14, 0x97,
  26975. 0xC2, 0x27, 0xDC, 0xB8, 0xC9, 0xB2, 0x55, 0x70,
  26976. 0xB7, 0xA0, 0xE4, 0x2A, 0xAD, 0xA6, 0x70, 0x9F,
  26977. 0x23, 0x20, 0x8F, 0x5D, 0x49, 0x6E, 0xBA, 0xB7,
  26978. 0x84, 0x3F, 0x64, 0x83, 0xBF, 0x0C, 0x0C, 0x73,
  26979. 0xA4, 0x02, 0x96, 0xEC, 0x2C, 0x64, 0x40, 0x00,
  26980. 0x13, 0x94, 0xC9, 0x9C, 0xA1, 0x73, 0xD5, 0xC7,
  26981. 0x75, 0xB7, 0xF4, 0x15, 0xD0, 0x2A, 0x5A, 0x26,
  26982. 0xA0, 0x74, 0x07, 0x91, 0x85, 0x87, 0xC4, 0x11,
  26983. 0x69, 0xF2, 0xB7, 0x17, 0x87, 0x55, 0xAC, 0xC2,
  26984. 0x7F, 0xC8, 0xB1, 0x9C, 0x4C, 0x4B, 0x3F, 0xCD,
  26985. 0x41, 0x05, 0x3F, 0x2C, 0x74, 0xC8, 0xA1, 0x0A,
  26986. 0x83, 0x21, 0x24, 0x1B, 0x28, 0x02, 0x43, 0x28,
  26987. 0x75, 0xAE, 0x80, 0x8B, 0x9E, 0xF1, 0x36, 0x5C,
  26988. 0x7B, 0x8A, 0x52, 0x90, 0x2F, 0x13, 0x17, 0xBA,
  26989. 0x2F, 0xB0, 0x26, 0x9F, 0x47, 0x93, 0x06, 0x72,
  26990. 0x10, 0x7B, 0x47, 0x26, 0xFE, 0xF6, 0x45, 0x47,
  26991. 0x39, 0x4D, 0x33, 0x20, 0xC8, 0xF1, 0x20, 0xB3,
  26992. 0xC2, 0xF4, 0x72, 0x5B, 0x03, 0x05, 0xFA, 0xB8,
  26993. 0x8C, 0xC7, 0x98, 0x1F, 0xCB, 0x09, 0xA7, 0x6A,
  26994. 0x1C, 0xBF, 0x7F, 0x17, 0x9F, 0x43, 0xBB, 0x0A,
  26995. 0x4C, 0x8B, 0x05, 0x90, 0x85, 0x7F, 0x1E, 0x69,
  26996. 0x70, 0x84, 0x66, 0xC7, 0xF8, 0x60, 0x73, 0x91,
  26997. 0xE7, 0xBC, 0x52, 0x68, 0xBF, 0xD3, 0xD7, 0xA1,
  26998. 0xDF, 0xFC, 0xB4, 0xEC, 0xA2, 0xA1, 0xC9, 0xB5,
  26999. 0x97, 0x59, 0x30, 0x13, 0xD5, 0xFC, 0x42, 0x02,
  27000. 0xEC, 0x2B, 0x74, 0xE5, 0x7A, 0xB7, 0x6B, 0xBC,
  27001. 0xF3, 0x63, 0x2B, 0xBA, 0xF9, 0x7C, 0xDC, 0x41,
  27002. 0x8A, 0x6F, 0x16, 0x39, 0x28, 0x38, 0xCA, 0x9B,
  27003. 0xF4, 0x5D, 0xDF, 0x02, 0x37, 0x77, 0xB7, 0x56,
  27004. 0x18, 0x33, 0xC1, 0x05, 0x19, 0x0F, 0x94, 0xF3,
  27005. 0x02, 0xC5, 0x9B, 0x53, 0x19, 0x00, 0xBB, 0xC8,
  27006. 0x16, 0x36, 0x1F, 0xAA, 0x5B, 0x33, 0x80, 0xCA,
  27007. 0x3A, 0x89, 0x31, 0x04, 0xCA, 0x73, 0x88, 0xB1,
  27008. 0x85, 0x67, 0x1B, 0x3E, 0x5F, 0xE3, 0x79, 0x0E,
  27009. 0x9A, 0x62, 0x6E, 0xC4, 0x6D, 0x9B, 0x0B, 0x33,
  27010. 0xC7, 0xA4, 0x19, 0xAF, 0x7B, 0x32, 0xB6, 0x85,
  27011. 0x98, 0x94, 0xF5, 0x75, 0xD8, 0x2A, 0xC5, 0x45,
  27012. 0x6B, 0x54, 0x90, 0xA7, 0xAF, 0x8F, 0xE6, 0x10,
  27013. 0x46, 0x36, 0x05, 0x89, 0xEC, 0xBA, 0x72, 0x44,
  27014. 0x23, 0x6F, 0x41, 0x23, 0x11, 0x6B, 0x61, 0x74,
  27015. 0xAA, 0x17, 0x92, 0x49, 0xA4, 0x91, 0x95, 0xB3,
  27016. 0x56, 0xC7, 0x2F, 0xC6, 0x64, 0x1F, 0x02, 0x51,
  27017. 0x81, 0x2E, 0xAA, 0x98, 0x57, 0x0B, 0x04, 0x66,
  27018. 0x99, 0x07, 0x0E, 0x08, 0x19, 0xDC, 0x27, 0x13,
  27019. 0xF4, 0x69, 0x13, 0x7D, 0xFC, 0x6A, 0x3D, 0x7B,
  27020. 0x92, 0xB2, 0x98, 0x99, 0x5E, 0xE7, 0x80, 0x36,
  27021. 0x91, 0x53, 0xAC, 0x36, 0x6B, 0x06, 0xD7, 0x24,
  27022. 0x9C, 0xD0, 0x9E, 0x1B, 0x33, 0x78, 0xFB, 0x04,
  27023. 0x39, 0x9C, 0xEC, 0xB8, 0x65, 0x05, 0x81, 0xD6,
  27024. 0x37, 0xC7, 0x9A, 0xE6, 0x7D, 0x6F, 0x2C, 0xAF,
  27025. 0x6A, 0xBA, 0xCF, 0x59, 0x81, 0x59, 0xA7, 0x79,
  27026. 0x2C, 0xB3, 0xC9, 0x71, 0xD1, 0x49, 0x9D, 0x23,
  27027. 0x73, 0xAD, 0x20, 0xF6, 0x3F, 0x03, 0xBB, 0x59,
  27028. 0xED, 0x13, 0x73, 0x84, 0xAC, 0x61, 0xA7, 0x15,
  27029. 0x51, 0x43, 0xB8, 0xCA, 0x49, 0x32, 0x61, 0x2E,
  27030. 0xC9, 0x15, 0xE4, 0xCA, 0x34, 0x6A, 0x9B, 0xCE,
  27031. 0x5D, 0xD6, 0x04, 0x17, 0xC6, 0xB2, 0xA8, 0x9B,
  27032. 0x1C, 0xC4, 0x35, 0x64, 0x3F, 0x87, 0x5B, 0xDC,
  27033. 0x5A, 0x7E, 0x5B, 0x34, 0x81, 0xCF, 0x91, 0x9E,
  27034. 0xA0, 0x91, 0x72, 0xFE, 0xBC, 0x46, 0xD4, 0xFC,
  27035. 0x3F, 0xB0, 0xCB, 0x95, 0x91, 0x70, 0x4E, 0xE2,
  27036. 0xDB, 0xB6, 0x18, 0x44, 0xB2, 0xF3, 0x31, 0x4A,
  27037. 0x06, 0xBB, 0x6C, 0x6D, 0x34, 0x00, 0x5E, 0x48,
  27038. 0x5C, 0xE6, 0x67, 0xBD, 0xC7, 0xD0, 0x98, 0x58,
  27039. 0x69, 0x28, 0xD2, 0xD9, 0x13, 0x40, 0xF0, 0x04,
  27040. 0x19, 0xEA, 0x40, 0x13, 0x51, 0xA2, 0x40, 0xA0,
  27041. 0xB0, 0x41, 0x05, 0x8B, 0xEF, 0xB0, 0xC2, 0xFD,
  27042. 0x32, 0x64, 0x5B, 0x7A, 0x2D, 0xF8, 0xF5, 0xCB,
  27043. 0xFD, 0x87, 0x33, 0x27, 0xC9, 0x78, 0xD7, 0xB3,
  27044. 0x51, 0xA2, 0x80, 0x88, 0x43, 0x88, 0x37, 0x02,
  27045. 0x4C, 0x52, 0xB9, 0xC2, 0x95, 0xCD, 0x71, 0x36,
  27046. 0x46, 0xFB, 0x5D, 0x6C, 0x0C, 0xCF, 0xB4, 0x70,
  27047. 0x73, 0x4A, 0xC2, 0xB2, 0xBC, 0x81, 0x23, 0xC2,
  27048. 0xC1, 0x3D, 0xF6, 0x93, 0x8E, 0x92, 0x45, 0x5A,
  27049. 0x86, 0x26, 0x39, 0xFE, 0xB8, 0xA6, 0x4B, 0x85,
  27050. 0x16, 0x3E, 0x32, 0x70, 0x7E, 0x03, 0x7B, 0x38,
  27051. 0xD8, 0xAC, 0x39, 0x22, 0xB4, 0x51, 0x87, 0xBB,
  27052. 0x65, 0xEA, 0xFD, 0x46, 0x5F, 0xC6, 0x4A, 0x0C,
  27053. 0x5F, 0x8F, 0x3F, 0x90, 0x03, 0x48, 0x94, 0x15,
  27054. 0x89, 0x9D, 0x59, 0xA5, 0x43, 0xD8, 0x20, 0x8C,
  27055. 0x54, 0xA3, 0x16, 0x65, 0x29, 0xB5, 0x39, 0x22,
  27056. 0x7F, 0xFA, 0xD1, 0xBC, 0x8A, 0xF7, 0x3B, 0x7E,
  27057. 0x87, 0x49, 0x56, 0xB8, 0x1C, 0x2A, 0x2E, 0xF0,
  27058. 0xBF, 0xAB, 0xE8, 0xDC, 0x93, 0xD7, 0x7B, 0x2F,
  27059. 0xBC, 0x9E, 0x0C, 0x64, 0xEF, 0xA0, 0x1E, 0x84,
  27060. 0x86, 0x26, 0xED, 0x79, 0xD4, 0x51, 0x14, 0x08,
  27061. 0x00, 0xE0, 0x3B, 0x59, 0xB9, 0x56, 0xF8, 0x21,
  27062. 0x0E, 0x55, 0x60, 0x67, 0x40, 0x7D, 0x13, 0xDC,
  27063. 0x90, 0xFA, 0x9E, 0x8B, 0x87, 0x2B, 0xFB, 0x8F
  27064. };
  27065. const byte kyber512_ct[] = {
  27066. 0xED, 0xF2, 0x41, 0x45, 0xE4, 0x3B, 0x4F, 0x6D,
  27067. 0xC6, 0xBF, 0x83, 0x32, 0xF5, 0x4E, 0x02, 0xCA,
  27068. 0xB0, 0x2D, 0xBF, 0x3B, 0x56, 0x05, 0xDD, 0xC9,
  27069. 0x0A, 0x15, 0xC8, 0x86, 0xAD, 0x3E, 0xD4, 0x89,
  27070. 0x46, 0x26, 0x99, 0xE4, 0xAB, 0xED, 0x44, 0x35,
  27071. 0x0B, 0xC3, 0x75, 0x7E, 0x26, 0x96, 0xFB, 0xFB,
  27072. 0x25, 0x34, 0x41, 0x2E, 0x8D, 0xD2, 0x01, 0xF1,
  27073. 0xE4, 0x54, 0x0A, 0x39, 0x70, 0xB0, 0x55, 0xFE,
  27074. 0x3B, 0x0B, 0xEC, 0x3A, 0x71, 0xF9, 0xE1, 0x15,
  27075. 0xB3, 0xF9, 0xF3, 0x91, 0x02, 0x06, 0x5B, 0x1C,
  27076. 0xCA, 0x83, 0x14, 0xDC, 0xC7, 0x95, 0xE3, 0xC0,
  27077. 0xE8, 0xFA, 0x98, 0xEE, 0x83, 0xCA, 0x66, 0x28,
  27078. 0x45, 0x70, 0x28, 0xA4, 0xD0, 0x9E, 0x83, 0x9E,
  27079. 0x55, 0x48, 0x62, 0xCF, 0x0B, 0x7B, 0xF5, 0x6C,
  27080. 0x5C, 0x0A, 0x82, 0x9E, 0x86, 0x57, 0x94, 0x79,
  27081. 0x45, 0xFE, 0x9C, 0x22, 0x56, 0x4F, 0xBA, 0xEB,
  27082. 0xC1, 0xB3, 0xAF, 0x35, 0x0D, 0x79, 0x55, 0x50,
  27083. 0x8A, 0x26, 0xD8, 0xA8, 0xEB, 0x54, 0x7B, 0x8B,
  27084. 0x1A, 0x2C, 0xF0, 0x3C, 0xCA, 0x1A, 0xAB, 0xCE,
  27085. 0x6C, 0x34, 0x97, 0x78, 0x3B, 0x64, 0x65, 0xBA,
  27086. 0x0B, 0x6E, 0x7A, 0xCB, 0xA8, 0x21, 0x19, 0x51,
  27087. 0x24, 0xAE, 0xF0, 0x9E, 0x62, 0x83, 0x82, 0xA1,
  27088. 0xF9, 0x14, 0x04, 0x3B, 0xE7, 0x09, 0x6E, 0x95,
  27089. 0x2C, 0xBC, 0x4F, 0xB4, 0xAF, 0xED, 0x13, 0x60,
  27090. 0x90, 0x46, 0x11, 0x7C, 0x01, 0x1F, 0xD7, 0x41,
  27091. 0xEE, 0x28, 0x6C, 0x83, 0x77, 0x16, 0x90, 0xF0,
  27092. 0xAE, 0xB5, 0x0D, 0xA0, 0xD7, 0x12, 0x85, 0xA1,
  27093. 0x79, 0xB2, 0x15, 0xC6, 0x03, 0x6D, 0xEB, 0x78,
  27094. 0x0F, 0x4D, 0x16, 0x76, 0x9F, 0x72, 0xDE, 0x16,
  27095. 0xFD, 0xAD, 0xAC, 0x73, 0xBE, 0xFA, 0x5B, 0xEF,
  27096. 0x89, 0x43, 0x19, 0x7F, 0x44, 0xC5, 0x95, 0x89,
  27097. 0xDC, 0x9F, 0x49, 0x73, 0xDE, 0x14, 0x50, 0xBA,
  27098. 0x1D, 0x0C, 0x32, 0x90, 0xD6, 0xB1, 0xD6, 0x83,
  27099. 0xF2, 0x94, 0xE7, 0x59, 0xC9, 0x54, 0xAB, 0xE8,
  27100. 0xA7, 0xDA, 0x5B, 0x10, 0x54, 0xFD, 0x6D, 0x21,
  27101. 0x32, 0x9B, 0x8E, 0x73, 0xD3, 0x75, 0x6A, 0xFD,
  27102. 0xA0, 0xDC, 0xB1, 0xFC, 0x8B, 0x15, 0x82, 0xD1,
  27103. 0xF9, 0x0C, 0xF2, 0x75, 0xA1, 0x02, 0xAB, 0xC6,
  27104. 0xAC, 0x69, 0x9D, 0xF0, 0xC5, 0x87, 0x0E, 0x50,
  27105. 0xA1, 0xF9, 0x89, 0xE4, 0xE6, 0x24, 0x1B, 0x60,
  27106. 0xAA, 0xA2, 0xEC, 0xF9, 0xE8, 0xE3, 0x3E, 0x0F,
  27107. 0xFC, 0xF4, 0x0F, 0xE8, 0x31, 0xE8, 0xFD, 0xC2,
  27108. 0xE8, 0x3B, 0x52, 0xCA, 0x7A, 0xB6, 0xD9, 0x3F,
  27109. 0x14, 0x6D, 0x29, 0xDC, 0xA5, 0x3C, 0x7D, 0xA1,
  27110. 0xDB, 0x4A, 0xC4, 0xF2, 0xDB, 0x39, 0xEA, 0x12,
  27111. 0x0D, 0x90, 0xFA, 0x60, 0xF4, 0xD4, 0x37, 0xC6,
  27112. 0xD0, 0x0E, 0xF4, 0x83, 0xBC, 0x94, 0xA3, 0x17,
  27113. 0x5C, 0xDA, 0x16, 0x3F, 0xC1, 0xC2, 0x82, 0x8B,
  27114. 0xE4, 0xDB, 0xD6, 0x43, 0x05, 0x07, 0xB5, 0x84,
  27115. 0xBB, 0x51, 0x77, 0xE1, 0x71, 0xB8, 0xDD, 0xA9,
  27116. 0xA4, 0x29, 0x3C, 0x32, 0x00, 0x29, 0x5C, 0x80,
  27117. 0x3A, 0x86, 0x5D, 0x6D, 0x21, 0x66, 0xF6, 0x6B,
  27118. 0xA5, 0x40, 0x1F, 0xB7, 0xA0, 0xE8, 0x53, 0x16,
  27119. 0x86, 0x00, 0xA2, 0x94, 0x84, 0x37, 0xE0, 0x36,
  27120. 0xE3, 0xBF, 0x19, 0xE1, 0x2F, 0xD3, 0xF2, 0xA2,
  27121. 0xB8, 0xB3, 0x43, 0xF7, 0x84, 0x24, 0x8E, 0x8D,
  27122. 0x68, 0x5E, 0xB0, 0xAF, 0xDE, 0x63, 0x15, 0x33,
  27123. 0x87, 0x30, 0xE7, 0xA1, 0x00, 0x1C, 0x27, 0xD8,
  27124. 0xD2, 0xA7, 0x6F, 0xA6, 0x9D, 0x15, 0x7B, 0xA1,
  27125. 0xAC, 0x7A, 0xD5, 0x6D, 0xA5, 0xA8, 0xC7, 0x0F,
  27126. 0xE4, 0xB5, 0xB8, 0xD7, 0x86, 0xDC, 0x6F, 0xC0,
  27127. 0x56, 0x6B, 0xA8, 0xE1, 0xB8, 0x81, 0x63, 0x34,
  27128. 0xD3, 0x2A, 0x3F, 0xB1, 0xCE, 0x7D, 0x4D, 0x5E,
  27129. 0x4C, 0x33, 0x2A, 0xF7, 0xB0, 0x03, 0xD0, 0x91,
  27130. 0x74, 0x1A, 0x3D, 0x5C, 0x96, 0x52, 0x92, 0x25,
  27131. 0x5D, 0xFF, 0x8E, 0xD2, 0xBB, 0xF1, 0xF9, 0x11,
  27132. 0x6B, 0xE5, 0x0C, 0x17, 0xB8, 0xE5, 0x48, 0x74,
  27133. 0x8A, 0xD4, 0xB2, 0xE9, 0x57, 0xBB, 0xD1, 0x95,
  27134. 0x34, 0x82, 0xA2, 0xE1, 0x71, 0x8C, 0xEC, 0x66,
  27135. 0xCD, 0x2C, 0x81, 0xF5, 0x72, 0xD5, 0x52, 0xB7,
  27136. 0x18, 0x78, 0x85, 0xE6, 0xB8, 0x94, 0x3D, 0x64,
  27137. 0x31, 0x41, 0x3C, 0x59, 0xEB, 0xB7, 0xE0, 0x36,
  27138. 0x04, 0x84, 0x90, 0xBE, 0x52, 0x89, 0xE9, 0x5B,
  27139. 0x20, 0xA8, 0x9E, 0x8B, 0x15, 0x9F, 0x61, 0xA9,
  27140. 0xA9, 0x88, 0x6E, 0x14, 0x75, 0x68, 0xF4, 0xC9,
  27141. 0x02, 0x1F, 0x36, 0x2F, 0x02, 0x68, 0x8A, 0x1C,
  27142. 0x8C, 0x3B, 0xB0, 0xD2, 0x40, 0x86, 0x88, 0x0E,
  27143. 0x55, 0xB6, 0xED, 0xB4, 0x3F, 0x37, 0x45, 0xD2,
  27144. 0xC1, 0x66, 0xDC, 0x1C, 0xB7, 0x43, 0xC7, 0x6F,
  27145. 0xE6, 0xBE, 0x52, 0x3A, 0x89, 0x3C, 0xC7, 0x64,
  27146. 0xD1, 0x64, 0x35, 0xC3, 0x78, 0x51, 0x25, 0x2A,
  27147. 0x81, 0xE2, 0xFF, 0xBA, 0x0F, 0x18, 0x97, 0x1A,
  27148. 0x3D, 0xEE, 0x37, 0xD4, 0x87, 0x7C, 0xB9, 0x28,
  27149. 0xE3, 0x6E, 0x52, 0x35, 0x03, 0x7A, 0x6B, 0x20,
  27150. 0x57, 0x89, 0x7D, 0x51, 0x8A, 0x5F, 0x0E, 0x34,
  27151. 0x8E, 0x3A, 0xB6, 0xD5, 0xB5, 0x2D, 0xFC, 0x60,
  27152. 0x75, 0x7F, 0x3B, 0x41, 0xA4, 0xFE, 0xC7, 0x82,
  27153. 0x8F, 0x1D, 0xEE, 0xAF, 0x45, 0x87, 0xCC, 0xC8,
  27154. 0xEA, 0xDF, 0x64, 0x7F, 0x4D, 0x20, 0x3B, 0x2F,
  27155. 0xAA, 0x05, 0xA6, 0x49, 0xB5, 0x82, 0x34, 0x0C,
  27156. 0xB4, 0xCA, 0xCE, 0x57, 0xA3, 0x07, 0x11, 0xBE,
  27157. 0x75, 0x2F, 0xAC, 0xF0, 0x22, 0x7D, 0x0A, 0x80,
  27158. 0xC4, 0x12, 0x84, 0x42, 0xDD, 0xC5, 0x44, 0xBE,
  27159. 0x80, 0x5B, 0x9C, 0xFE, 0x8F, 0xE9, 0xB1, 0x23,
  27160. 0x7C, 0x80, 0xF9, 0x67, 0x87, 0xCD, 0x92, 0x81,
  27161. 0xCC, 0xF2, 0x70, 0xC1, 0xAF, 0xC0, 0x67, 0x0D
  27162. };
  27163. const byte kyber512_ss[] = {
  27164. 0x0A, 0x69, 0x25, 0x67, 0x6F, 0x24, 0xB2, 0x2C,
  27165. 0x28, 0x6F, 0x4C, 0x81, 0xA4, 0x22, 0x4C, 0xEC,
  27166. 0x50, 0x6C, 0x9B, 0x25, 0x7D, 0x48, 0x0E, 0x02,
  27167. 0xE3, 0xB4, 0x9F, 0x44, 0xCA, 0xA3, 0x23, 0x7F
  27168. };
  27169. #else
  27170. const byte kyber512_pk[] = {
  27171. 0x68, 0xD6, 0x83, 0xF6, 0x01, 0x8F, 0xCB, 0x38,
  27172. 0x71, 0xC0, 0x64, 0x0A, 0xD1, 0x7B, 0x7E, 0x80,
  27173. 0x02, 0x93, 0xC9, 0xF6, 0x36, 0xC1, 0xE2, 0x09,
  27174. 0x10, 0x34, 0x25, 0xAD, 0x72, 0x26, 0xDD, 0x13,
  27175. 0x90, 0x41, 0xFA, 0x84, 0x9F, 0x8C, 0x2F, 0xED,
  27176. 0x18, 0xAB, 0xEF, 0xF5, 0x2A, 0xE0, 0xB6, 0x06,
  27177. 0x86, 0x13, 0xBA, 0x05, 0x73, 0x09, 0x3D, 0xE8,
  27178. 0xAC, 0xC8, 0x56, 0x19, 0x80, 0x62, 0xCD, 0x38,
  27179. 0xDC, 0x0F, 0x80, 0x05, 0x6B, 0x8D, 0x1C, 0x42,
  27180. 0xDF, 0xA3, 0x2C, 0x4C, 0xA4, 0x0E, 0x8C, 0x57,
  27181. 0x03, 0xF3, 0x13, 0xBA, 0x5F, 0xD0, 0x5C, 0x1E,
  27182. 0x75, 0xC3, 0x78, 0xD7, 0x04, 0x77, 0x62, 0x1A,
  27183. 0xF3, 0xA0, 0x8E, 0x52, 0x0A, 0x16, 0x05, 0xC4,
  27184. 0x89, 0x42, 0x2C, 0x97, 0xD0, 0xD9, 0x2E, 0x43,
  27185. 0x12, 0xBB, 0xEF, 0xB6, 0xC0, 0x18, 0x7A, 0x45,
  27186. 0xF9, 0x24, 0x6B, 0xF8, 0xEB, 0xA6, 0x2C, 0x04,
  27187. 0x7F, 0x8C, 0xB2, 0x42, 0x17, 0xB2, 0x01, 0x78,
  27188. 0xBB, 0x09, 0x5E, 0x59, 0x63, 0x6B, 0xCB, 0xA9,
  27189. 0x13, 0x73, 0x52, 0x65, 0xC7, 0xA0, 0x90, 0x06,
  27190. 0x12, 0xEF, 0x16, 0x41, 0xC9, 0xE1, 0x4D, 0x7D,
  27191. 0x89, 0x15, 0xE3, 0xC8, 0x83, 0xB7, 0x50, 0x68,
  27192. 0xA9, 0xB1, 0x3B, 0x0C, 0xF0, 0xA1, 0x32, 0x8A,
  27193. 0x3E, 0xC5, 0xD6, 0x00, 0x1E, 0xE9, 0x6D, 0x01,
  27194. 0x05, 0x6C, 0x25, 0x09, 0xC2, 0x16, 0x51, 0x4D,
  27195. 0xA5, 0x24, 0x61, 0xF7, 0xCA, 0x6A, 0xC4, 0x1A,
  27196. 0x2C, 0x0E, 0x35, 0x2B, 0x53, 0x63, 0x68, 0x99,
  27197. 0xD0, 0x65, 0xBD, 0xF2, 0xBF, 0x14, 0xF5, 0x35,
  27198. 0xFC, 0xE9, 0x42, 0x93, 0x9B, 0x6B, 0x4B, 0x97,
  27199. 0x72, 0x04, 0xD6, 0x29, 0xA6, 0x24, 0x1F, 0xFD,
  27200. 0x99, 0x94, 0x6A, 0x70, 0xBC, 0xA8, 0x91, 0x2B,
  27201. 0x1D, 0xFA, 0x61, 0x4A, 0xE9, 0x91, 0x3C, 0xB3,
  27202. 0x65, 0x63, 0x04, 0x7F, 0x9F, 0xA0, 0xA6, 0x48,
  27203. 0x7B, 0xA4, 0x10, 0x19, 0x5F, 0x56, 0x17, 0x58,
  27204. 0x84, 0x41, 0xC6, 0xA1, 0xA1, 0x50, 0x78, 0xBA,
  27205. 0x60, 0x93, 0x36, 0x79, 0x64, 0xD2, 0x5F, 0x19,
  27206. 0xD7, 0x15, 0x1A, 0xE0, 0x9F, 0xAF, 0xA6, 0xA4,
  27207. 0xCB, 0x63, 0x09, 0x40, 0xC5, 0x8F, 0x88, 0xEB,
  27208. 0x77, 0x8B, 0xFA, 0x44, 0xD7, 0x3B, 0x30, 0x63,
  27209. 0x74, 0x46, 0x8A, 0xBB, 0x8B, 0x56, 0x16, 0x0D,
  27210. 0x49, 0xA2, 0x4E, 0x6F, 0x43, 0x2B, 0xBF, 0xE8,
  27211. 0x8D, 0xE0, 0xC5, 0x6E, 0x9A, 0xE2, 0x13, 0x23,
  27212. 0xF1, 0x2D, 0xEE, 0xFC, 0xAF, 0x32, 0xF6, 0x8C,
  27213. 0x46, 0x21, 0x2F, 0xE3, 0x68, 0x3F, 0xB0, 0x48,
  27214. 0xB7, 0xE2, 0x4C, 0xB8, 0x17, 0xF8, 0x7E, 0xC4,
  27215. 0xA4, 0x2F, 0xE1, 0x1B, 0x21, 0xEA, 0x54, 0x4E,
  27216. 0xB7, 0xD4, 0x82, 0x89, 0xAA, 0xB4, 0xB3, 0x9C,
  27217. 0x57, 0x8F, 0xF3, 0x21, 0xFF, 0xEA, 0x57, 0xE9,
  27218. 0xD8, 0x81, 0x04, 0x9A, 0x1F, 0x92, 0xB7, 0x4A,
  27219. 0xFC, 0xB5, 0x5B, 0xA5, 0x29, 0x1F, 0xF5, 0x25,
  27220. 0x5D, 0x91, 0x11, 0xAA, 0x01, 0x08, 0x52, 0x5A,
  27221. 0xA3, 0x65, 0x33, 0xB4, 0xC2, 0xF0, 0x0C, 0x49,
  27222. 0x73, 0x07, 0x75, 0x67, 0x5C, 0x17, 0x8F, 0xE2,
  27223. 0x80, 0x83, 0x0B, 0xBC, 0x0A, 0xDA, 0x08, 0xC3,
  27224. 0xB3, 0x55, 0x19, 0x90, 0x78, 0x4F, 0xD5, 0x5A,
  27225. 0x12, 0x8B, 0x6A, 0x29, 0xF9, 0xB7, 0xA0, 0xF1,
  27226. 0x68, 0xD5, 0xC2, 0x9D, 0xEC, 0xC9, 0x63, 0x82,
  27227. 0x83, 0x9A, 0xCD, 0xEA, 0x32, 0xA8, 0xD4, 0x90,
  27228. 0x48, 0xFC, 0x47, 0x1D, 0x9B, 0x23, 0xB3, 0xAC,
  27229. 0xBE, 0x7D, 0x16, 0x94, 0x4A, 0xF6, 0x5A, 0x0B,
  27230. 0x76, 0x2D, 0xFF, 0xBA, 0x03, 0xA5, 0x25, 0xCB,
  27231. 0x69, 0x9B, 0x03, 0xB0, 0x7A, 0x2C, 0xFE, 0x55,
  27232. 0x22, 0x1E, 0x52, 0x68, 0x8A, 0xA2, 0xBF, 0xCB,
  27233. 0x8A, 0xA7, 0x3D, 0x75, 0x22, 0x28, 0x29, 0x88,
  27234. 0x8F, 0xA7, 0xC9, 0x97, 0x2A, 0xCD, 0x04, 0x01,
  27235. 0x60, 0xC1, 0x43, 0x25, 0x07, 0x78, 0x65, 0x00,
  27236. 0x08, 0x8A, 0x0A, 0xD5, 0x09, 0xC4, 0x45, 0xBB,
  27237. 0x5B, 0xF1, 0xCF, 0x40, 0x92, 0xAA, 0x74, 0xE1,
  27238. 0x50, 0x34, 0x30, 0x07, 0xE9, 0x00, 0x16, 0xA2,
  27239. 0xF4, 0xBD, 0x6A, 0xE8, 0x6A, 0x71, 0x76, 0x37,
  27240. 0xED, 0x5C, 0x7C, 0x9E, 0x1C, 0xCD, 0x4C, 0x78,
  27241. 0xAF, 0xA2, 0x74, 0xA5, 0x66, 0xB1, 0x7E, 0x5F,
  27242. 0xC4, 0x33, 0x26, 0x7C, 0x40, 0xBD, 0x16, 0x40,
  27243. 0x3C, 0x53, 0x2C, 0x2B, 0x9A, 0xBF, 0xA0, 0xC6,
  27244. 0x25, 0x1B, 0x8A, 0xAD, 0x27, 0xB8, 0x97, 0x3D,
  27245. 0x54, 0x87, 0x0B, 0x50, 0xB9, 0xDE, 0x37, 0xAE,
  27246. 0x5F, 0xA5, 0x6F, 0x79, 0xF9, 0x75, 0x77, 0xE6,
  27247. 0x35, 0xC7, 0x03, 0xBC, 0xBB, 0x02, 0x82, 0x67,
  27248. 0xB5, 0x44, 0x87, 0x96, 0xA0, 0xDC, 0xE0, 0x34,
  27249. 0xE2, 0xCB, 0xBB, 0x09, 0x27, 0x6F, 0x49, 0x50,
  27250. 0xBE, 0x02, 0x23, 0xCC, 0x97, 0xC0, 0x0A, 0x66,
  27251. 0xBB, 0x32, 0x1B, 0x11, 0x4D, 0x5F, 0xB1, 0xBB,
  27252. 0x5A, 0x6C, 0x97, 0x3C, 0xEC, 0x1B, 0x53, 0xDB,
  27253. 0x26, 0x30, 0x81, 0x44, 0x74, 0x76, 0x99, 0x3E,
  27254. 0x68, 0x18, 0xA6, 0xD6, 0xCE, 0x4D, 0x05, 0x61,
  27255. 0xFD, 0xBA, 0x94, 0x1B, 0xD9, 0xBE, 0x17, 0x41,
  27256. 0x6B, 0xEA, 0xBB, 0x51, 0x1C, 0x61, 0xCD, 0xA4,
  27257. 0x12, 0x3D, 0x45, 0xB0, 0x09, 0xEA, 0x6C, 0x09,
  27258. 0xAD, 0x78, 0x09, 0xD4, 0x52, 0xBE, 0x93, 0x42,
  27259. 0x9E, 0x05, 0x39, 0x41, 0xBB, 0xA9, 0xB0, 0x65,
  27260. 0x99, 0x61, 0xBB, 0x02, 0x16, 0x60, 0x08, 0x7C,
  27261. 0xCD, 0x6C, 0x6E, 0x0D, 0xEA, 0xA6, 0xBE, 0xE5,
  27262. 0x87, 0x19, 0x3C, 0x4E, 0x1C, 0x13, 0x20, 0x04,
  27263. 0x5C, 0xB5, 0x09, 0xC7, 0xC0, 0x58, 0x4C, 0x43,
  27264. 0xCD, 0x65, 0x0F, 0xD7, 0x82, 0x65, 0x20, 0x24,
  27265. 0x36, 0xC7, 0xC9, 0x31, 0x75, 0xFB, 0xCB, 0x3B,
  27266. 0x55, 0x69, 0x4A, 0xB9, 0xB8, 0xAE, 0x99, 0xC2,
  27267. 0x69, 0x2D, 0x3C, 0x8E, 0x8C, 0x73, 0x9F, 0xB3,
  27268. 0x78, 0x27, 0x74, 0x15, 0x61, 0xD5, 0xE7, 0x66,
  27269. 0x67, 0x51, 0x82, 0xFF, 0x83, 0xC9, 0x1C, 0xF0,
  27270. 0x33, 0x65, 0x88, 0x23, 0xD8, 0xFA, 0x91, 0xE1
  27271. };
  27272. const byte kyber512_sk[] = {
  27273. 0xD3, 0xBA, 0x7A, 0x3B, 0x91, 0x56, 0x15, 0x32,
  27274. 0x1E, 0xD4, 0xE4, 0x19, 0x6B, 0xC7, 0x34, 0xD0,
  27275. 0x75, 0xB7, 0x40, 0x56, 0xC9, 0x7C, 0x83, 0x00,
  27276. 0x78, 0xF0, 0x28, 0xDD, 0xB3, 0x4E, 0xEF, 0x76,
  27277. 0x9A, 0x22, 0x31, 0xC6, 0x60, 0x05, 0x9A, 0x56,
  27278. 0xA0, 0x9A, 0x64, 0x10, 0x8C, 0x7C, 0x0B, 0xC4,
  27279. 0x8B, 0x0A, 0x5E, 0xD3, 0x47, 0xA4, 0xA8, 0xBC,
  27280. 0x7D, 0xA1, 0x70, 0x74, 0xF4, 0xE3, 0x3E, 0x8A,
  27281. 0x13, 0x0B, 0x2F, 0x67, 0x64, 0x74, 0x63, 0x50,
  27282. 0x0B, 0x50, 0x1E, 0x8C, 0x41, 0x42, 0x8C, 0x87,
  27283. 0x85, 0x63, 0x19, 0x6C, 0x99, 0x40, 0x2B, 0x1F,
  27284. 0x4A, 0x17, 0x9E, 0x18, 0xC2, 0xF8, 0x8B, 0xA5,
  27285. 0x75, 0xD1, 0x5C, 0xF2, 0xF0, 0xA8, 0xDC, 0x3C,
  27286. 0x8A, 0x28, 0x2B, 0x3E, 0xF1, 0x18, 0x1F, 0xAA,
  27287. 0x28, 0x9B, 0x81, 0xF0, 0xA9, 0xBE, 0x47, 0xC3,
  27288. 0xFA, 0xFC, 0xCB, 0x18, 0xAC, 0x08, 0xF8, 0x6C,
  27289. 0xA2, 0xF3, 0xBB, 0x06, 0x99, 0xE5, 0x7E, 0xAD,
  27290. 0x61, 0x0E, 0x6A, 0xA7, 0x02, 0xD1, 0x09, 0xC4,
  27291. 0xF5, 0x04, 0x33, 0xA4, 0x74, 0x2B, 0x74, 0xDC,
  27292. 0x75, 0x27, 0x8B, 0x22, 0x46, 0xA8, 0xC1, 0xE4,
  27293. 0xC3, 0x73, 0xDC, 0x03, 0x85, 0x17, 0xB4, 0x8A,
  27294. 0x0B, 0xDC, 0xB3, 0xB3, 0xB1, 0x9F, 0x5F, 0xFA,
  27295. 0xBD, 0x65, 0x11, 0xAA, 0xD5, 0x4C, 0xCE, 0xB6,
  27296. 0x3C, 0xB9, 0x40, 0xCC, 0xB3, 0xF9, 0x40, 0x75,
  27297. 0xDF, 0x03, 0x19, 0x7D, 0x81, 0xAD, 0x2D, 0x8A,
  27298. 0x8D, 0x35, 0xBC, 0x01, 0x1C, 0xCA, 0xCF, 0xE5,
  27299. 0xB0, 0xA8, 0x21, 0xC1, 0xAD, 0x04, 0x6B, 0xC2,
  27300. 0x0B, 0xD4, 0x48, 0xAB, 0x98, 0xAE, 0x49, 0x46,
  27301. 0x7F, 0xA7, 0xA2, 0xCB, 0x2B, 0xF1, 0x30, 0x32,
  27302. 0xA1, 0x3E, 0x98, 0x65, 0x08, 0x10, 0x0A, 0x22,
  27303. 0x1D, 0x55, 0xAD, 0x97, 0x91, 0xAE, 0x47, 0x37,
  27304. 0x91, 0xCA, 0xA3, 0x18, 0x91, 0x5B, 0x58, 0x3F,
  27305. 0xD4, 0x73, 0x91, 0xC2, 0x31, 0xD9, 0x0B, 0xB7,
  27306. 0x29, 0xB2, 0x06, 0x2A, 0x0A, 0xC4, 0x30, 0x91,
  27307. 0xAF, 0x14, 0x05, 0x02, 0xB0, 0x62, 0x42, 0xFD,
  27308. 0x65, 0x04, 0xFB, 0x74, 0x9C, 0x20, 0x93, 0xCD,
  27309. 0x78, 0x00, 0x63, 0x4B, 0x48, 0xBA, 0xFC, 0xE0,
  27310. 0xB1, 0xDC, 0x22, 0x2A, 0x42, 0xB9, 0x70, 0xB7,
  27311. 0x34, 0x26, 0x02, 0x1B, 0xC0, 0x8F, 0xE7, 0x10,
  27312. 0x98, 0x6B, 0x3E, 0xD6, 0x47, 0x9C, 0x21, 0xEB,
  27313. 0xBB, 0x9D, 0x94, 0x6D, 0x13, 0x3A, 0x97, 0x55,
  27314. 0xB7, 0x33, 0xB0, 0xBA, 0x79, 0x5D, 0x34, 0xAA,
  27315. 0x2C, 0x43, 0x6E, 0xB3, 0x04, 0x87, 0xFE, 0x19,
  27316. 0x3C, 0xDE, 0x86, 0xC7, 0x54, 0x00, 0x14, 0x85,
  27317. 0x48, 0x4C, 0x4D, 0x28, 0x89, 0xD9, 0x67, 0x66,
  27318. 0x7E, 0xD1, 0x72, 0xAB, 0xE4, 0x52, 0x5C, 0x05,
  27319. 0xA7, 0xE9, 0x53, 0x8E, 0x1B, 0x0C, 0x38, 0xBE,
  27320. 0x40, 0x7D, 0x3F, 0x09, 0x72, 0xEA, 0x82, 0xB9,
  27321. 0xA7, 0xB1, 0x55, 0x0B, 0x2A, 0x20, 0x68, 0xFA,
  27322. 0x80, 0x56, 0x86, 0xB2, 0x7F, 0x50, 0xAD, 0xCC,
  27323. 0x47, 0x30, 0xCA, 0xF9, 0x04, 0x28, 0x74, 0x11,
  27324. 0xED, 0xB4, 0x82, 0x46, 0x36, 0x6C, 0x4B, 0x02,
  27325. 0xB7, 0x95, 0x96, 0x69, 0x81, 0x56, 0xA0, 0xE4,
  27326. 0x89, 0x65, 0x2D, 0xA5, 0xC2, 0x2B, 0x51, 0x24,
  27327. 0xD7, 0x90, 0x33, 0xD9, 0x84, 0x0D, 0x51, 0x93,
  27328. 0x39, 0xF2, 0xA9, 0x90, 0x72, 0x1C, 0x73, 0x1B,
  27329. 0x02, 0xC2, 0x36, 0xBC, 0x72, 0x8C, 0x22, 0x61,
  27330. 0xFF, 0x18, 0x18, 0xA3, 0x8A, 0x4D, 0x39, 0x54,
  27331. 0x61, 0xAE, 0x20, 0xC4, 0x2E, 0x12, 0xB0, 0x1C,
  27332. 0xBC, 0x85, 0xFE, 0xC8, 0x9E, 0x9F, 0x75, 0x9D,
  27333. 0x20, 0xE3, 0x87, 0x4F, 0x37, 0x16, 0x0E, 0x19,
  27334. 0x5C, 0x4C, 0x61, 0x47, 0x54, 0xC5, 0x93, 0x03,
  27335. 0xD0, 0x9B, 0x27, 0x76, 0xAE, 0xF6, 0x8C, 0x5B,
  27336. 0xF6, 0x1C, 0x4B, 0x50, 0x47, 0x42, 0x41, 0x3A,
  27337. 0xC3, 0xD7, 0xC4, 0x95, 0x7C, 0x47, 0x0B, 0xC2,
  27338. 0x9A, 0x72, 0x2C, 0xC3, 0x7F, 0xB2, 0xB2, 0x30,
  27339. 0x45, 0x7A, 0x3A, 0x98, 0x8A, 0xBE, 0x48, 0x96,
  27340. 0x71, 0x70, 0x54, 0x4F, 0xD2, 0x57, 0xBB, 0x89,
  27341. 0xEC, 0xC3, 0x13, 0xF1, 0xBD, 0x33, 0x10, 0xCA,
  27342. 0x37, 0xB0, 0x2D, 0x6E, 0x97, 0x07, 0xCA, 0x91,
  27343. 0xB6, 0x25, 0x97, 0x7B, 0xED, 0xB3, 0x1B, 0xA8,
  27344. 0x27, 0x48, 0xEE, 0x31, 0xAF, 0x2B, 0xC7, 0x62,
  27345. 0x92, 0xA7, 0x2B, 0xCF, 0xA0, 0x46, 0xE9, 0x31,
  27346. 0x48, 0xBE, 0x8C, 0x83, 0xB2, 0x58, 0x50, 0x7D,
  27347. 0x96, 0x6F, 0x14, 0xD2, 0x02, 0x81, 0xA3, 0x33,
  27348. 0x3E, 0x69, 0xAB, 0x78, 0x60, 0x0D, 0x3F, 0x3B,
  27349. 0x6B, 0xAF, 0x7A, 0xC9, 0xAC, 0xEA, 0xB5, 0x5B,
  27350. 0xB2, 0x0C, 0xE7, 0x5C, 0x13, 0x47, 0x4A, 0x67,
  27351. 0x8E, 0x7C, 0x60, 0xC0, 0x81, 0x87, 0x1E, 0xF3,
  27352. 0x3C, 0xA9, 0xEC, 0x6C, 0xD6, 0xA3, 0x64, 0x3B,
  27353. 0x6A, 0x41, 0x60, 0xF8, 0x3A, 0x27, 0x8C, 0x21,
  27354. 0x0C, 0xA2, 0xBC, 0xB9, 0xE0, 0x12, 0x27, 0x01,
  27355. 0xC9, 0xAD, 0xF0, 0x45, 0x8E, 0xF4, 0x71, 0x9F,
  27356. 0xB5, 0xB4, 0x01, 0xB3, 0xB5, 0x16, 0xE1, 0xAD,
  27357. 0x25, 0xD7, 0xA4, 0x84, 0xA6, 0x3E, 0xE4, 0x1A,
  27358. 0x81, 0x44, 0xE1, 0xAA, 0x38, 0x8A, 0xC8, 0xAE,
  27359. 0x69, 0x00, 0xCE, 0x87, 0x9E, 0x9F, 0xD7, 0x4F,
  27360. 0x47, 0xC7, 0x30, 0x78, 0xCC, 0x6E, 0x85, 0xC7,
  27361. 0x64, 0x15, 0x0C, 0x00, 0x3A, 0x58, 0x70, 0x56,
  27362. 0x97, 0x33, 0xDA, 0x66, 0x8B, 0xDA, 0x15, 0xC5,
  27363. 0xA2, 0x69, 0x9D, 0x38, 0x25, 0x82, 0x6B, 0x36,
  27364. 0x98, 0x5A, 0x51, 0x08, 0x51, 0x0B, 0x6F, 0xC3,
  27365. 0x65, 0x4B, 0x17, 0x51, 0x02, 0xA4, 0xD9, 0x06,
  27366. 0x14, 0x47, 0xA7, 0x8B, 0xA9, 0x39, 0x40, 0x8B,
  27367. 0x9A, 0x94, 0x57, 0x61, 0xEC, 0x0C, 0x24, 0x6F,
  27368. 0x8A, 0x64, 0x18, 0xF6, 0x2F, 0x1E, 0xA3, 0x00,
  27369. 0x68, 0xD6, 0x83, 0xF6, 0x01, 0x8F, 0xCB, 0x38,
  27370. 0x71, 0xC0, 0x64, 0x0A, 0xD1, 0x7B, 0x7E, 0x80,
  27371. 0x02, 0x93, 0xC9, 0xF6, 0x36, 0xC1, 0xE2, 0x09,
  27372. 0x10, 0x34, 0x25, 0xAD, 0x72, 0x26, 0xDD, 0x13,
  27373. 0x90, 0x41, 0xFA, 0x84, 0x9F, 0x8C, 0x2F, 0xED,
  27374. 0x18, 0xAB, 0xEF, 0xF5, 0x2A, 0xE0, 0xB6, 0x06,
  27375. 0x86, 0x13, 0xBA, 0x05, 0x73, 0x09, 0x3D, 0xE8,
  27376. 0xAC, 0xC8, 0x56, 0x19, 0x80, 0x62, 0xCD, 0x38,
  27377. 0xDC, 0x0F, 0x80, 0x05, 0x6B, 0x8D, 0x1C, 0x42,
  27378. 0xDF, 0xA3, 0x2C, 0x4C, 0xA4, 0x0E, 0x8C, 0x57,
  27379. 0x03, 0xF3, 0x13, 0xBA, 0x5F, 0xD0, 0x5C, 0x1E,
  27380. 0x75, 0xC3, 0x78, 0xD7, 0x04, 0x77, 0x62, 0x1A,
  27381. 0xF3, 0xA0, 0x8E, 0x52, 0x0A, 0x16, 0x05, 0xC4,
  27382. 0x89, 0x42, 0x2C, 0x97, 0xD0, 0xD9, 0x2E, 0x43,
  27383. 0x12, 0xBB, 0xEF, 0xB6, 0xC0, 0x18, 0x7A, 0x45,
  27384. 0xF9, 0x24, 0x6B, 0xF8, 0xEB, 0xA6, 0x2C, 0x04,
  27385. 0x7F, 0x8C, 0xB2, 0x42, 0x17, 0xB2, 0x01, 0x78,
  27386. 0xBB, 0x09, 0x5E, 0x59, 0x63, 0x6B, 0xCB, 0xA9,
  27387. 0x13, 0x73, 0x52, 0x65, 0xC7, 0xA0, 0x90, 0x06,
  27388. 0x12, 0xEF, 0x16, 0x41, 0xC9, 0xE1, 0x4D, 0x7D,
  27389. 0x89, 0x15, 0xE3, 0xC8, 0x83, 0xB7, 0x50, 0x68,
  27390. 0xA9, 0xB1, 0x3B, 0x0C, 0xF0, 0xA1, 0x32, 0x8A,
  27391. 0x3E, 0xC5, 0xD6, 0x00, 0x1E, 0xE9, 0x6D, 0x01,
  27392. 0x05, 0x6C, 0x25, 0x09, 0xC2, 0x16, 0x51, 0x4D,
  27393. 0xA5, 0x24, 0x61, 0xF7, 0xCA, 0x6A, 0xC4, 0x1A,
  27394. 0x2C, 0x0E, 0x35, 0x2B, 0x53, 0x63, 0x68, 0x99,
  27395. 0xD0, 0x65, 0xBD, 0xF2, 0xBF, 0x14, 0xF5, 0x35,
  27396. 0xFC, 0xE9, 0x42, 0x93, 0x9B, 0x6B, 0x4B, 0x97,
  27397. 0x72, 0x04, 0xD6, 0x29, 0xA6, 0x24, 0x1F, 0xFD,
  27398. 0x99, 0x94, 0x6A, 0x70, 0xBC, 0xA8, 0x91, 0x2B,
  27399. 0x1D, 0xFA, 0x61, 0x4A, 0xE9, 0x91, 0x3C, 0xB3,
  27400. 0x65, 0x63, 0x04, 0x7F, 0x9F, 0xA0, 0xA6, 0x48,
  27401. 0x7B, 0xA4, 0x10, 0x19, 0x5F, 0x56, 0x17, 0x58,
  27402. 0x84, 0x41, 0xC6, 0xA1, 0xA1, 0x50, 0x78, 0xBA,
  27403. 0x60, 0x93, 0x36, 0x79, 0x64, 0xD2, 0x5F, 0x19,
  27404. 0xD7, 0x15, 0x1A, 0xE0, 0x9F, 0xAF, 0xA6, 0xA4,
  27405. 0xCB, 0x63, 0x09, 0x40, 0xC5, 0x8F, 0x88, 0xEB,
  27406. 0x77, 0x8B, 0xFA, 0x44, 0xD7, 0x3B, 0x30, 0x63,
  27407. 0x74, 0x46, 0x8A, 0xBB, 0x8B, 0x56, 0x16, 0x0D,
  27408. 0x49, 0xA2, 0x4E, 0x6F, 0x43, 0x2B, 0xBF, 0xE8,
  27409. 0x8D, 0xE0, 0xC5, 0x6E, 0x9A, 0xE2, 0x13, 0x23,
  27410. 0xF1, 0x2D, 0xEE, 0xFC, 0xAF, 0x32, 0xF6, 0x8C,
  27411. 0x46, 0x21, 0x2F, 0xE3, 0x68, 0x3F, 0xB0, 0x48,
  27412. 0xB7, 0xE2, 0x4C, 0xB8, 0x17, 0xF8, 0x7E, 0xC4,
  27413. 0xA4, 0x2F, 0xE1, 0x1B, 0x21, 0xEA, 0x54, 0x4E,
  27414. 0xB7, 0xD4, 0x82, 0x89, 0xAA, 0xB4, 0xB3, 0x9C,
  27415. 0x57, 0x8F, 0xF3, 0x21, 0xFF, 0xEA, 0x57, 0xE9,
  27416. 0xD8, 0x81, 0x04, 0x9A, 0x1F, 0x92, 0xB7, 0x4A,
  27417. 0xFC, 0xB5, 0x5B, 0xA5, 0x29, 0x1F, 0xF5, 0x25,
  27418. 0x5D, 0x91, 0x11, 0xAA, 0x01, 0x08, 0x52, 0x5A,
  27419. 0xA3, 0x65, 0x33, 0xB4, 0xC2, 0xF0, 0x0C, 0x49,
  27420. 0x73, 0x07, 0x75, 0x67, 0x5C, 0x17, 0x8F, 0xE2,
  27421. 0x80, 0x83, 0x0B, 0xBC, 0x0A, 0xDA, 0x08, 0xC3,
  27422. 0xB3, 0x55, 0x19, 0x90, 0x78, 0x4F, 0xD5, 0x5A,
  27423. 0x12, 0x8B, 0x6A, 0x29, 0xF9, 0xB7, 0xA0, 0xF1,
  27424. 0x68, 0xD5, 0xC2, 0x9D, 0xEC, 0xC9, 0x63, 0x82,
  27425. 0x83, 0x9A, 0xCD, 0xEA, 0x32, 0xA8, 0xD4, 0x90,
  27426. 0x48, 0xFC, 0x47, 0x1D, 0x9B, 0x23, 0xB3, 0xAC,
  27427. 0xBE, 0x7D, 0x16, 0x94, 0x4A, 0xF6, 0x5A, 0x0B,
  27428. 0x76, 0x2D, 0xFF, 0xBA, 0x03, 0xA5, 0x25, 0xCB,
  27429. 0x69, 0x9B, 0x03, 0xB0, 0x7A, 0x2C, 0xFE, 0x55,
  27430. 0x22, 0x1E, 0x52, 0x68, 0x8A, 0xA2, 0xBF, 0xCB,
  27431. 0x8A, 0xA7, 0x3D, 0x75, 0x22, 0x28, 0x29, 0x88,
  27432. 0x8F, 0xA7, 0xC9, 0x97, 0x2A, 0xCD, 0x04, 0x01,
  27433. 0x60, 0xC1, 0x43, 0x25, 0x07, 0x78, 0x65, 0x00,
  27434. 0x08, 0x8A, 0x0A, 0xD5, 0x09, 0xC4, 0x45, 0xBB,
  27435. 0x5B, 0xF1, 0xCF, 0x40, 0x92, 0xAA, 0x74, 0xE1,
  27436. 0x50, 0x34, 0x30, 0x07, 0xE9, 0x00, 0x16, 0xA2,
  27437. 0xF4, 0xBD, 0x6A, 0xE8, 0x6A, 0x71, 0x76, 0x37,
  27438. 0xED, 0x5C, 0x7C, 0x9E, 0x1C, 0xCD, 0x4C, 0x78,
  27439. 0xAF, 0xA2, 0x74, 0xA5, 0x66, 0xB1, 0x7E, 0x5F,
  27440. 0xC4, 0x33, 0x26, 0x7C, 0x40, 0xBD, 0x16, 0x40,
  27441. 0x3C, 0x53, 0x2C, 0x2B, 0x9A, 0xBF, 0xA0, 0xC6,
  27442. 0x25, 0x1B, 0x8A, 0xAD, 0x27, 0xB8, 0x97, 0x3D,
  27443. 0x54, 0x87, 0x0B, 0x50, 0xB9, 0xDE, 0x37, 0xAE,
  27444. 0x5F, 0xA5, 0x6F, 0x79, 0xF9, 0x75, 0x77, 0xE6,
  27445. 0x35, 0xC7, 0x03, 0xBC, 0xBB, 0x02, 0x82, 0x67,
  27446. 0xB5, 0x44, 0x87, 0x96, 0xA0, 0xDC, 0xE0, 0x34,
  27447. 0xE2, 0xCB, 0xBB, 0x09, 0x27, 0x6F, 0x49, 0x50,
  27448. 0xBE, 0x02, 0x23, 0xCC, 0x97, 0xC0, 0x0A, 0x66,
  27449. 0xBB, 0x32, 0x1B, 0x11, 0x4D, 0x5F, 0xB1, 0xBB,
  27450. 0x5A, 0x6C, 0x97, 0x3C, 0xEC, 0x1B, 0x53, 0xDB,
  27451. 0x26, 0x30, 0x81, 0x44, 0x74, 0x76, 0x99, 0x3E,
  27452. 0x68, 0x18, 0xA6, 0xD6, 0xCE, 0x4D, 0x05, 0x61,
  27453. 0xFD, 0xBA, 0x94, 0x1B, 0xD9, 0xBE, 0x17, 0x41,
  27454. 0x6B, 0xEA, 0xBB, 0x51, 0x1C, 0x61, 0xCD, 0xA4,
  27455. 0x12, 0x3D, 0x45, 0xB0, 0x09, 0xEA, 0x6C, 0x09,
  27456. 0xAD, 0x78, 0x09, 0xD4, 0x52, 0xBE, 0x93, 0x42,
  27457. 0x9E, 0x05, 0x39, 0x41, 0xBB, 0xA9, 0xB0, 0x65,
  27458. 0x99, 0x61, 0xBB, 0x02, 0x16, 0x60, 0x08, 0x7C,
  27459. 0xCD, 0x6C, 0x6E, 0x0D, 0xEA, 0xA6, 0xBE, 0xE5,
  27460. 0x87, 0x19, 0x3C, 0x4E, 0x1C, 0x13, 0x20, 0x04,
  27461. 0x5C, 0xB5, 0x09, 0xC7, 0xC0, 0x58, 0x4C, 0x43,
  27462. 0xCD, 0x65, 0x0F, 0xD7, 0x82, 0x65, 0x20, 0x24,
  27463. 0x36, 0xC7, 0xC9, 0x31, 0x75, 0xFB, 0xCB, 0x3B,
  27464. 0x55, 0x69, 0x4A, 0xB9, 0xB8, 0xAE, 0x99, 0xC2,
  27465. 0x69, 0x2D, 0x3C, 0x8E, 0x8C, 0x73, 0x9F, 0xB3,
  27466. 0x78, 0x27, 0x74, 0x15, 0x61, 0xD5, 0xE7, 0x66,
  27467. 0x67, 0x51, 0x82, 0xFF, 0x83, 0xC9, 0x1C, 0xF0,
  27468. 0x33, 0x65, 0x88, 0x23, 0xD8, 0xFA, 0x91, 0xE1,
  27469. 0xC1, 0xC0, 0x8E, 0xFF, 0x2A, 0xB6, 0xE2, 0xAF,
  27470. 0x9F, 0x29, 0xA6, 0x32, 0xC5, 0x93, 0x32, 0xE9,
  27471. 0xA6, 0x1B, 0x63, 0x14, 0x6E, 0x8A, 0xC3, 0x5E,
  27472. 0xF3, 0xD3, 0xC4, 0x5A, 0x10, 0x10, 0xAC, 0xE2,
  27473. 0x86, 0x26, 0xED, 0x79, 0xD4, 0x51, 0x14, 0x08,
  27474. 0x00, 0xE0, 0x3B, 0x59, 0xB9, 0x56, 0xF8, 0x21,
  27475. 0x0E, 0x55, 0x60, 0x67, 0x40, 0x7D, 0x13, 0xDC,
  27476. 0x90, 0xFA, 0x9E, 0x8B, 0x87, 0x2B, 0xFB, 0x8F
  27477. };
  27478. const byte kyber512_ct[] = {
  27479. 0x58, 0x56, 0xAE, 0x75, 0x76, 0x21, 0xFD, 0x94,
  27480. 0x9B, 0xA5, 0x49, 0xD2, 0x49, 0x70, 0x52, 0x5F,
  27481. 0x17, 0x95, 0x60, 0xE3, 0x24, 0xF7, 0x1B, 0x3C,
  27482. 0x1F, 0xDC, 0xFC, 0xAF, 0x92, 0xFD, 0x7F, 0xED,
  27483. 0x6B, 0x35, 0x1F, 0x0D, 0xA4, 0x1C, 0x98, 0x51,
  27484. 0x24, 0x9F, 0x6E, 0x63, 0xB7, 0xBC, 0xE3, 0x4F,
  27485. 0xD4, 0x9C, 0x97, 0x70, 0x58, 0x8B, 0x94, 0x2D,
  27486. 0x1B, 0x51, 0x69, 0x57, 0xDB, 0x3A, 0xE9, 0x7C,
  27487. 0x5F, 0x94, 0x51, 0x8D, 0x40, 0xF5, 0x4B, 0x5B,
  27488. 0x78, 0xE1, 0xF3, 0x8F, 0x8D, 0x61, 0x27, 0xA5,
  27489. 0xD0, 0xD5, 0xD4, 0x24, 0x29, 0xEA, 0x79, 0x62,
  27490. 0x13, 0xC0, 0x77, 0x2D, 0x5C, 0x9B, 0x99, 0x2C,
  27491. 0xEC, 0x72, 0x1B, 0x52, 0x17, 0xB6, 0x91, 0x7F,
  27492. 0xF8, 0xC0, 0xCC, 0xBB, 0xCC, 0xFE, 0x13, 0x4B,
  27493. 0xD8, 0x9A, 0x99, 0x48, 0x0B, 0x95, 0x66, 0xE4,
  27494. 0x69, 0x60, 0xBD, 0x21, 0x8C, 0xAC, 0x2D, 0xDD,
  27495. 0x58, 0x00, 0xB0, 0x83, 0x01, 0x9F, 0x09, 0x42,
  27496. 0x0E, 0x8B, 0xA2, 0x8E, 0x1E, 0x4D, 0xCC, 0x51,
  27497. 0xD4, 0xF5, 0xF9, 0x57, 0xD4, 0x57, 0x5B, 0xC3,
  27498. 0x2C, 0xF3, 0xDD, 0x08, 0x15, 0xD7, 0xE1, 0xEC,
  27499. 0xDA, 0x47, 0x44, 0x3B, 0x34, 0xCD, 0x88, 0x68,
  27500. 0xE7, 0x39, 0xD1, 0x57, 0x90, 0x29, 0x37, 0x12,
  27501. 0xBF, 0xF0, 0x89, 0x19, 0x8D, 0xFF, 0xA8, 0x42,
  27502. 0xD9, 0x5C, 0x03, 0x68, 0x49, 0x6C, 0x3E, 0x7D,
  27503. 0xFA, 0x06, 0xC8, 0x99, 0x9B, 0x9A, 0xEA, 0x3B,
  27504. 0x09, 0xC6, 0xD3, 0x62, 0x80, 0xF7, 0x6D, 0xD9,
  27505. 0xC8, 0xC1, 0x58, 0x53, 0xB4, 0x5E, 0xBC, 0xCA,
  27506. 0x5E, 0xC8, 0xF9, 0xB1, 0xF5, 0x7C, 0xFA, 0x6A,
  27507. 0x60, 0x19, 0xA0, 0x52, 0xCD, 0x9D, 0xD9, 0x5F,
  27508. 0x6F, 0x25, 0x97, 0x3D, 0xFA, 0xB9, 0x79, 0x7B,
  27509. 0x3F, 0xDB, 0xDC, 0x2C, 0xEF, 0xCE, 0x8B, 0x0B,
  27510. 0x35, 0x33, 0xA3, 0xA8, 0x54, 0x96, 0xA4, 0x11,
  27511. 0x09, 0xF6, 0x14, 0xB0, 0x53, 0xB0, 0x8E, 0x48,
  27512. 0xFE, 0xA9, 0x2F, 0xA5, 0x61, 0x07, 0xB5, 0x24,
  27513. 0xDD, 0x28, 0x77, 0x93, 0xA0, 0x24, 0x50, 0x03,
  27514. 0xEA, 0x35, 0xA0, 0x4E, 0x03, 0xEA, 0x59, 0x3D,
  27515. 0xAA, 0xFE, 0x31, 0x24, 0xB1, 0x7D, 0x0F, 0x2D,
  27516. 0xC3, 0xBE, 0xAE, 0xD3, 0x48, 0xD8, 0x96, 0xD1,
  27517. 0x65, 0xE5, 0x24, 0x08, 0x67, 0xA2, 0xCB, 0x72,
  27518. 0x3A, 0xA3, 0x88, 0x62, 0xC0, 0x47, 0x45, 0x75,
  27519. 0x40, 0xB4, 0xA4, 0xC0, 0x06, 0xF8, 0x22, 0xBE,
  27520. 0xEF, 0xB6, 0xF2, 0x75, 0xF5, 0x82, 0x8D, 0x3B,
  27521. 0x79, 0xC5, 0x3F, 0x87, 0x5E, 0x3C, 0x5B, 0xD4,
  27522. 0xA7, 0x66, 0x5C, 0xD1, 0x0C, 0xFA, 0x25, 0x1B,
  27523. 0x79, 0xE7, 0x6D, 0x26, 0xC5, 0xDB, 0x24, 0x25,
  27524. 0x72, 0xE5, 0xD8, 0x11, 0xC4, 0x60, 0x04, 0xB0,
  27525. 0xCC, 0xAB, 0x1C, 0xE5, 0x00, 0x2C, 0xA1, 0xDE,
  27526. 0x61, 0x4F, 0x31, 0x0B, 0x30, 0x9D, 0xFA, 0x75,
  27527. 0x63, 0xD0, 0x66, 0x23, 0x03, 0x6C, 0x7C, 0x26,
  27528. 0x17, 0x96, 0x40, 0x26, 0x45, 0x03, 0x60, 0xEF,
  27529. 0x52, 0x5B, 0x1B, 0xE7, 0xD8, 0x16, 0xEA, 0x1D,
  27530. 0xDF, 0xA1, 0x07, 0x15, 0xA1, 0x14, 0x86, 0x9E,
  27531. 0x62, 0x70, 0xC0, 0x3C, 0xDC, 0x58, 0x52, 0xD7,
  27532. 0x20, 0x46, 0x81, 0xB0, 0xF4, 0xD1, 0xB2, 0xA3,
  27533. 0x88, 0xAC, 0x06, 0x1B, 0x99, 0xAE, 0x0C, 0x01,
  27534. 0x1A, 0xFF, 0x0D, 0x3D, 0x89, 0x6B, 0xFF, 0xE6,
  27535. 0x27, 0x2D, 0xCF, 0x30, 0x9B, 0xB4, 0xF8, 0x6E,
  27536. 0xF4, 0xB5, 0x8B, 0xA8, 0xF4, 0xA1, 0x41, 0x15,
  27537. 0x38, 0x83, 0x4B, 0xAE, 0x55, 0x22, 0xBB, 0x51,
  27538. 0x3B, 0xF3, 0x56, 0xA5, 0x20, 0x3F, 0xE8, 0xAA,
  27539. 0x57, 0x5A, 0xCC, 0xD0, 0x94, 0xC9, 0x55, 0x2C,
  27540. 0xC7, 0xB6, 0x40, 0x75, 0x2C, 0xDE, 0x80, 0xD1,
  27541. 0x5F, 0x7F, 0x6B, 0xE0, 0x88, 0xA1, 0x2C, 0x5C,
  27542. 0x58, 0x47, 0xDD, 0x56, 0xCA, 0x66, 0x26, 0x7C,
  27543. 0x08, 0xDC, 0xD5, 0x16, 0x70, 0x6F, 0x3A, 0xE9,
  27544. 0x0F, 0x17, 0xA7, 0x42, 0x6A, 0x1B, 0x24, 0x44,
  27545. 0x05, 0x6E, 0x89, 0x5B, 0x77, 0x6C, 0x1E, 0x7A,
  27546. 0xE1, 0x65, 0xE4, 0xE9, 0x5D, 0x62, 0x0B, 0x20,
  27547. 0x54, 0xE5, 0x00, 0x5C, 0x62, 0x2C, 0x97, 0x03,
  27548. 0x4F, 0x96, 0x39, 0x14, 0x28, 0x0A, 0x0A, 0x06,
  27549. 0x2C, 0x2A, 0x63, 0x0E, 0x65, 0xCF, 0x64, 0xF2,
  27550. 0xD7, 0x0C, 0xD6, 0x0F, 0xE1, 0x57, 0xE3, 0x20,
  27551. 0xB7, 0xE2, 0xD6, 0x33, 0x2C, 0xDB, 0x73, 0x5E,
  27552. 0xA9, 0x8B, 0xEB, 0x1E, 0x2E, 0x01, 0x5C, 0x0A,
  27553. 0x2B, 0x33, 0xD7, 0x0F, 0xE7, 0x68, 0xF1, 0x77,
  27554. 0xE6, 0x23, 0x91, 0x04, 0x88, 0xE8, 0xBB, 0x2F,
  27555. 0x83, 0x83, 0xE3, 0xE4, 0xA6, 0xE0, 0xFC, 0xA1,
  27556. 0x11, 0xF0, 0xA0, 0x2E, 0x57, 0x3B, 0xFB, 0x38,
  27557. 0xBC, 0x34, 0x73, 0x47, 0x08, 0x11, 0x62, 0x08,
  27558. 0xCA, 0x3E, 0x82, 0xEF, 0xE5, 0xC2, 0x15, 0x44,
  27559. 0x3D, 0x3F, 0xE9, 0x46, 0x3B, 0x61, 0x4D, 0xA1,
  27560. 0x7A, 0x09, 0x29, 0x37, 0xFB, 0x9C, 0xCB, 0xCE,
  27561. 0xE9, 0xC8, 0x84, 0x01, 0xCD, 0xC3, 0x55, 0x7E,
  27562. 0xF5, 0xBF, 0xFE, 0x89, 0x45, 0x2C, 0x42, 0x1A,
  27563. 0x6E, 0x8C, 0xB0, 0x0D, 0xA5, 0x00, 0xDB, 0x90,
  27564. 0x94, 0xA9, 0x92, 0xD5, 0x76, 0xAA, 0xCB, 0x2D,
  27565. 0x17, 0xB5, 0xF5, 0xEA, 0x5B, 0xC4, 0x58, 0x8E,
  27566. 0x2B, 0x2E, 0x08, 0x76, 0x9D, 0x62, 0x6C, 0x00,
  27567. 0x98, 0x0D, 0x51, 0x63, 0x9A, 0x43, 0xA0, 0xD0,
  27568. 0xEA, 0x5C, 0xCC, 0x26, 0x9E, 0x86, 0x8C, 0x1E,
  27569. 0xB1, 0xEE, 0xC8, 0x08, 0xCE, 0x64, 0x20, 0x8C,
  27570. 0x6B, 0xA0, 0x73, 0x5A, 0x0B, 0x42, 0x06, 0xB1,
  27571. 0x08, 0x28, 0xCE, 0x9B, 0x5E, 0x51, 0xCD, 0x5F,
  27572. 0x0E, 0xA5, 0x77, 0x7D, 0x0B, 0x7A, 0xCF, 0x14,
  27573. 0x42, 0xC9, 0xAA, 0xCC, 0x3E, 0x80, 0x87, 0x8C,
  27574. 0x4D, 0x7D, 0x3B, 0x6F, 0xDC, 0x56, 0xEF, 0x17
  27575. };
  27576. const byte kyber512_ss[] = {
  27577. 0x0C, 0x92, 0x39, 0xC7, 0x70, 0x5D, 0x63, 0x91,
  27578. 0x51, 0xAD, 0x1B, 0xCA, 0xDF, 0x58, 0xBD, 0x99,
  27579. 0x91, 0x0B, 0x7A, 0x12, 0x44, 0x99, 0x17, 0x21,
  27580. 0x13, 0x22, 0x8B, 0x4C, 0x75, 0xF8, 0x22, 0xE1
  27581. };
  27582. #endif
  27583. ret = wc_KyberKey_Init(KYBER512, &key, HEAP_HINT, INVALID_DEVID);
  27584. if (ret != 0)
  27585. return -20101;
  27586. ret = wc_KyberKey_MakeKeyWithRandom(&key, kyber512_rand,
  27587. sizeof(kyber512_rand));
  27588. if (ret != 0)
  27589. return -20102;
  27590. ret = wc_KyberKey_EncodePublicKey(&key, pub, sizeof(pub));
  27591. if (ret != 0)
  27592. return -20103;
  27593. ret = wc_KyberKey_EncodePrivateKey(&key, priv, sizeof(priv));
  27594. if (ret != 0)
  27595. return -20104;
  27596. if (XMEMCMP(pub, kyber512_pk, sizeof(kyber512_pk)) != 0)
  27597. return -20105;
  27598. if (XMEMCMP(priv, kyber512_sk, sizeof(kyber512_sk)) != 0)
  27599. return -20106;
  27600. ret = wc_KyberKey_EncapsulateWithRandom(&key, ct, ss, kyber512enc_rand,
  27601. sizeof(kyber512enc_rand));
  27602. if (ret != 0)
  27603. return -20107;
  27604. if (XMEMCMP(ct, kyber512_ct, sizeof(kyber512_ct)) != 0)
  27605. return -20108;
  27606. if (XMEMCMP(ss, kyber512_ss, sizeof(kyber512_ss)) != 0)
  27607. return -20109;
  27608. ret = wc_KyberKey_Decapsulate(&key, ss_dec, ct, sizeof(kyber512_ct));
  27609. if (ret != 0)
  27610. return -20110;
  27611. if (XMEMCMP(ss_dec, kyber512_ss, sizeof(kyber512_ss)) != 0)
  27612. return -20111;
  27613. wc_KyberKey_Free(&key);
  27614. return 0;
  27615. }
  27616. #endif /* WOLFSSL_KYBER512 */
  27617. #ifdef WOLFSSL_KYBER768
  27618. static int kyber768_kat(void)
  27619. {
  27620. KyberKey key;
  27621. int ret;
  27622. byte priv[KYBER768_PRIVATE_KEY_SIZE];
  27623. byte pub[KYBER768_PUBLIC_KEY_SIZE];
  27624. byte ct[KYBER768_CIPHER_TEXT_SIZE];
  27625. byte ss[KYBER_SS_SZ];
  27626. byte ss_dec[KYBER_SS_SZ];
  27627. const byte kyber768_rand[] = {
  27628. 0x7c, 0x99, 0x35, 0xa0, 0xb0, 0x76, 0x94, 0xaa,
  27629. 0x0c, 0x6d, 0x10, 0xe4, 0xdb, 0x6b, 0x1a, 0xdd,
  27630. 0x2f, 0xd8, 0x1a, 0x25, 0xcc, 0xb1, 0x48, 0x03,
  27631. 0x2d, 0xcd, 0x73, 0x99, 0x36, 0x73, 0x7f, 0x2d,
  27632. 0x86, 0x26, 0xED, 0x79, 0xD4, 0x51, 0x14, 0x08,
  27633. 0x00, 0xE0, 0x3B, 0x59, 0xB9, 0x56, 0xF8, 0x21,
  27634. 0x0E, 0x55, 0x60, 0x67, 0x40, 0x7D, 0x13, 0xDC,
  27635. 0x90, 0xFA, 0x9E, 0x8B, 0x87, 0x2B, 0xFB, 0x8F
  27636. };
  27637. const byte kyber768enc_rand[] = {
  27638. 0x14, 0x7c, 0x03, 0xf7, 0xa5, 0xbe, 0xbb, 0xa4,
  27639. 0x06, 0xc8, 0xfa, 0xe1, 0x87, 0x4d, 0x7f, 0x13,
  27640. 0xc8, 0x0e, 0xfe, 0x79, 0xa3, 0xa9, 0xa8, 0x74,
  27641. 0xcc, 0x09, 0xfe, 0x76, 0xf6, 0x99, 0x76, 0x15
  27642. };
  27643. #ifndef WOLFSSL_KYBER_90S
  27644. const byte kyber768_pk[] = {
  27645. 0xA7, 0x2C, 0x2D, 0x9C, 0x84, 0x3E, 0xE9, 0xF8,
  27646. 0x31, 0x3E, 0xCC, 0x7F, 0x86, 0xD6, 0x29, 0x4D,
  27647. 0x59, 0x15, 0x9D, 0x9A, 0x87, 0x9A, 0x54, 0x2E,
  27648. 0x26, 0x09, 0x22, 0xAD, 0xF9, 0x99, 0x05, 0x1C,
  27649. 0xC4, 0x52, 0x00, 0xC9, 0xFF, 0xDB, 0x60, 0x44,
  27650. 0x9C, 0x49, 0x46, 0x59, 0x79, 0x27, 0x23, 0x67,
  27651. 0xC0, 0x83, 0xA7, 0xD6, 0x26, 0x7A, 0x3E, 0xD7,
  27652. 0xA7, 0xFD, 0x47, 0x95, 0x7C, 0x21, 0x93, 0x27,
  27653. 0xF7, 0xCA, 0x73, 0xA4, 0x00, 0x7E, 0x16, 0x27,
  27654. 0xF0, 0x0B, 0x11, 0xCC, 0x80, 0x57, 0x3C, 0x15,
  27655. 0xAE, 0xE6, 0x64, 0x0F, 0xB8, 0x56, 0x2D, 0xFA,
  27656. 0x6B, 0x24, 0x0C, 0xA0, 0xAD, 0x35, 0x1A, 0xC4,
  27657. 0xAC, 0x15, 0x5B, 0x96, 0xC1, 0x4C, 0x8A, 0xB1,
  27658. 0x3D, 0xD2, 0x62, 0xCD, 0xFD, 0x51, 0xC4, 0xBB,
  27659. 0x55, 0x72, 0xFD, 0x61, 0x65, 0x53, 0xD1, 0x7B,
  27660. 0xDD, 0x43, 0x0A, 0xCB, 0xEA, 0x3E, 0x95, 0xF0,
  27661. 0xB6, 0x98, 0xD6, 0x69, 0x90, 0xAB, 0x51, 0xE5,
  27662. 0xD0, 0x37, 0x83, 0xA8, 0xB3, 0xD2, 0x78, 0xA5,
  27663. 0x72, 0x04, 0x54, 0xCF, 0x96, 0x95, 0xCF, 0xDC,
  27664. 0xA0, 0x84, 0x85, 0xBA, 0x09, 0x9C, 0x51, 0xCD,
  27665. 0x92, 0xA7, 0xEA, 0x75, 0x87, 0xC1, 0xD1, 0x5C,
  27666. 0x28, 0xE6, 0x09, 0xA8, 0x18, 0x52, 0x60, 0x1B,
  27667. 0x06, 0x04, 0x01, 0x06, 0x79, 0xAA, 0x48, 0x2D,
  27668. 0x51, 0x26, 0x1E, 0xC3, 0x6E, 0x36, 0xB8, 0x71,
  27669. 0x96, 0x76, 0x21, 0x7F, 0xD7, 0x4C, 0x54, 0x78,
  27670. 0x64, 0x88, 0xF4, 0xB4, 0x96, 0x9C, 0x05, 0xA8,
  27671. 0xBA, 0x27, 0xCA, 0x3A, 0x77, 0xCC, 0xE7, 0x3B,
  27672. 0x96, 0x59, 0x23, 0xCA, 0x55, 0x4E, 0x42, 0x2B,
  27673. 0x9B, 0x61, 0xF4, 0x75, 0x46, 0x41, 0x60, 0x8A,
  27674. 0xC1, 0x6C, 0x9B, 0x85, 0x87, 0xA3, 0x2C, 0x1C,
  27675. 0x5D, 0xD7, 0x88, 0xF8, 0x8B, 0x36, 0xB7, 0x17,
  27676. 0xA4, 0x69, 0x65, 0x63, 0x5D, 0xEB, 0x67, 0xF4,
  27677. 0x5B, 0x12, 0x9B, 0x99, 0x07, 0x09, 0x09, 0xC9,
  27678. 0x3E, 0xB8, 0x0B, 0x42, 0xC2, 0xB3, 0xF3, 0xF7,
  27679. 0x03, 0x43, 0xA7, 0xCF, 0x37, 0xE8, 0x52, 0x0E,
  27680. 0x7B, 0xCF, 0xC4, 0x16, 0xAC, 0xA4, 0xF1, 0x8C,
  27681. 0x79, 0x81, 0x26, 0x2B, 0xA2, 0xBF, 0xC7, 0x56,
  27682. 0xAE, 0x03, 0x27, 0x8F, 0x0E, 0xC6, 0x6D, 0xC2,
  27683. 0x05, 0x76, 0x96, 0x82, 0x4B, 0xA6, 0x76, 0x98,
  27684. 0x65, 0xA6, 0x01, 0xD7, 0x14, 0x8E, 0xF6, 0xF5,
  27685. 0x4E, 0x5A, 0xF5, 0x68, 0x6A, 0xA2, 0x90, 0x6F,
  27686. 0x99, 0x4C, 0xE3, 0x8A, 0x5E, 0x0B, 0x93, 0x8F,
  27687. 0x23, 0x90, 0x07, 0x00, 0x30, 0x22, 0xC0, 0x33,
  27688. 0x92, 0xDF, 0x34, 0x01, 0xB1, 0xE4, 0xA3, 0xA7,
  27689. 0xEB, 0xC6, 0x16, 0x14, 0x49, 0xF7, 0x33, 0x74,
  27690. 0xC8, 0xB0, 0x14, 0x03, 0x69, 0x34, 0x3D, 0x92,
  27691. 0x95, 0xFD, 0xF5, 0x11, 0x84, 0x5C, 0x4A, 0x46,
  27692. 0xEB, 0xAA, 0xB6, 0xCA, 0x54, 0x92, 0xF6, 0x80,
  27693. 0x0B, 0x98, 0xC0, 0xCC, 0x80, 0x36, 0x53, 0xA4,
  27694. 0xB1, 0xD6, 0xE6, 0xAA, 0xED, 0x19, 0x32, 0xBA,
  27695. 0xCC, 0x5F, 0xEF, 0xAA, 0x81, 0x8B, 0xA5, 0x02,
  27696. 0x85, 0x9B, 0xA5, 0x49, 0x4C, 0x5F, 0x54, 0x02,
  27697. 0xC8, 0x53, 0x6A, 0x9C, 0x4C, 0x18, 0x88, 0x15,
  27698. 0x06, 0x17, 0xF8, 0x00, 0x98, 0xF6, 0xB2, 0xA9,
  27699. 0x9C, 0x39, 0xBC, 0x5D, 0xC7, 0xCF, 0x3B, 0x59,
  27700. 0x00, 0xA2, 0x13, 0x29, 0xAB, 0x59, 0x05, 0x3A,
  27701. 0xBA, 0xA6, 0x4E, 0xD1, 0x63, 0xE8, 0x59, 0xA8,
  27702. 0xB3, 0xB3, 0xCA, 0x33, 0x59, 0xB7, 0x50, 0xCC,
  27703. 0xC3, 0xE7, 0x10, 0xC7, 0xAC, 0x43, 0xC8, 0x19,
  27704. 0x1C, 0xB5, 0xD6, 0x88, 0x70, 0xC0, 0x63, 0x91,
  27705. 0xC0, 0xCB, 0x8A, 0xEC, 0x72, 0xB8, 0x97, 0xAC,
  27706. 0x6B, 0xE7, 0xFB, 0xAA, 0xCC, 0x67, 0x6E, 0xD6,
  27707. 0x63, 0x14, 0xC8, 0x36, 0x30, 0xE8, 0x94, 0x48,
  27708. 0xC8, 0x8A, 0x1D, 0xF0, 0x4A, 0xCE, 0xB2, 0x3A,
  27709. 0xBF, 0x2E, 0x40, 0x9E, 0xF3, 0x33, 0xC6, 0x22,
  27710. 0x28, 0x9C, 0x18, 0xA2, 0x13, 0x4E, 0x65, 0x0C,
  27711. 0x45, 0x25, 0x7E, 0x47, 0x47, 0x5F, 0xA3, 0x3A,
  27712. 0xA5, 0x37, 0xA5, 0xA8, 0xF7, 0x68, 0x02, 0x14,
  27713. 0x71, 0x6C, 0x50, 0xD4, 0x70, 0xE3, 0x28, 0x49,
  27714. 0x63, 0xCA, 0x64, 0xF5, 0x46, 0x77, 0xAE, 0xC5,
  27715. 0x4B, 0x52, 0x72, 0x16, 0x2B, 0xF5, 0x2B, 0xC8,
  27716. 0x14, 0x2E, 0x1D, 0x41, 0x83, 0xFC, 0x01, 0x74,
  27717. 0x54, 0xA6, 0xB5, 0xA4, 0x96, 0x83, 0x17, 0x59,
  27718. 0x06, 0x40, 0x24, 0x74, 0x59, 0x78, 0xCB, 0xD5,
  27719. 0x1A, 0x6C, 0xED, 0xC8, 0x95, 0x5D, 0xE4, 0xCC,
  27720. 0x6D, 0x36, 0x36, 0x70, 0xA4, 0x74, 0x66, 0xE8,
  27721. 0x2B, 0xE5, 0xC2, 0x36, 0x03, 0xA1, 0x7B, 0xF2,
  27722. 0x2A, 0xCD, 0xB7, 0xCC, 0x98, 0x4A, 0xF0, 0x8C,
  27723. 0x87, 0xE1, 0x4E, 0x27, 0x75, 0x3C, 0xF5, 0x87,
  27724. 0xA8, 0xEC, 0x34, 0x47, 0xE6, 0x2C, 0x64, 0x9E,
  27725. 0x88, 0x7A, 0x67, 0xC3, 0x6C, 0x9C, 0xE9, 0x87,
  27726. 0x21, 0xB6, 0x97, 0x21, 0x32, 0x75, 0x64, 0x6B,
  27727. 0x19, 0x4F, 0x36, 0x75, 0x86, 0x73, 0xA8, 0xED,
  27728. 0x11, 0x28, 0x44, 0x55, 0xAF, 0xC7, 0xA8, 0x52,
  27729. 0x9F, 0x69, 0xC9, 0x7A, 0x3C, 0x2D, 0x7B, 0x8C,
  27730. 0x63, 0x6C, 0x0B, 0xA5, 0x56, 0x14, 0xB7, 0x68,
  27731. 0xE6, 0x24, 0xE7, 0x12, 0x93, 0x0F, 0x77, 0x61,
  27732. 0x69, 0xB0, 0x17, 0x15, 0x72, 0x53, 0x51, 0xBC,
  27733. 0x74, 0xB4, 0x73, 0x95, 0xED, 0x52, 0xB2, 0x5A,
  27734. 0x13, 0x13, 0xC9, 0x51, 0x64, 0x81, 0x4C, 0x34,
  27735. 0xC9, 0x79, 0xCB, 0xDF, 0xAB, 0x85, 0x95, 0x46,
  27736. 0x62, 0xCA, 0xB4, 0x85, 0xE7, 0x50, 0x87, 0xA9,
  27737. 0x8C, 0xC7, 0x4B, 0xB8, 0x2C, 0xA2, 0xD1, 0xB5,
  27738. 0xBF, 0x28, 0x03, 0x23, 0x84, 0x80, 0x63, 0x8C,
  27739. 0x40, 0xE9, 0x0B, 0x43, 0xC7, 0x46, 0x0E, 0x7A,
  27740. 0xA9, 0x17, 0xF0, 0x10, 0x15, 0x1F, 0xAB, 0x11,
  27741. 0x69, 0x98, 0x7B, 0x37, 0x2A, 0xBB, 0x59, 0x27,
  27742. 0x1F, 0x70, 0x06, 0xC2, 0x4E, 0x60, 0x23, 0x6B,
  27743. 0x84, 0xB9, 0xDD, 0xD6, 0x00, 0x62, 0x37, 0x04,
  27744. 0x25, 0x46, 0x17, 0xFB, 0x49, 0x8D, 0x89, 0xE5,
  27745. 0x8B, 0x03, 0x68, 0xBC, 0xB2, 0x10, 0x3E, 0x79,
  27746. 0x35, 0x3E, 0xB5, 0x87, 0x86, 0x0C, 0x14, 0x22,
  27747. 0xE4, 0x76, 0x16, 0x2E, 0x42, 0x5B, 0xC2, 0x38,
  27748. 0x1D, 0xB8, 0x2C, 0x65, 0x92, 0x73, 0x7E, 0x1D,
  27749. 0xD6, 0x02, 0x86, 0x4B, 0x01, 0x67, 0xA7, 0x1E,
  27750. 0xC1, 0xF2, 0x23, 0x30, 0x5C, 0x02, 0xFE, 0x25,
  27751. 0x05, 0x2A, 0xF2, 0xB3, 0xB5, 0xA5, 0x5A, 0x0D,
  27752. 0x7A, 0x20, 0x22, 0xD9, 0xA7, 0x98, 0xDC, 0x0C,
  27753. 0x58, 0x74, 0xA9, 0x87, 0x02, 0xAA, 0xF4, 0x05,
  27754. 0x4C, 0x5D, 0x80, 0x33, 0x8A, 0x52, 0x48, 0xB5,
  27755. 0xB7, 0xBD, 0x09, 0xC5, 0x3B, 0x5E, 0x2A, 0x08,
  27756. 0x4B, 0x04, 0x7D, 0x27, 0x7A, 0x86, 0x1B, 0x1A,
  27757. 0x73, 0xBB, 0x51, 0x48, 0x8D, 0xE0, 0x4E, 0xF5,
  27758. 0x73, 0xC8, 0x52, 0x30, 0xA0, 0x47, 0x0B, 0x73,
  27759. 0x17, 0x5C, 0x9F, 0xA5, 0x05, 0x94, 0xF6, 0x6A,
  27760. 0x5F, 0x50, 0xB4, 0x15, 0x00, 0x54, 0xC9, 0x3B,
  27761. 0x68, 0x18, 0x6F, 0x8B, 0x5C, 0xBC, 0x49, 0x31,
  27762. 0x6C, 0x85, 0x48, 0xA6, 0x42, 0xB2, 0xB3, 0x6A,
  27763. 0x1D, 0x45, 0x4C, 0x74, 0x89, 0xAC, 0x33, 0xB2,
  27764. 0xD2, 0xCE, 0x66, 0x68, 0x09, 0x67, 0x82, 0xA2,
  27765. 0xC1, 0xE0, 0x86, 0x6D, 0x21, 0xA6, 0x5E, 0x16,
  27766. 0xB5, 0x85, 0xE7, 0xAF, 0x86, 0x18, 0xBD, 0xF3,
  27767. 0x18, 0x4C, 0x19, 0x86, 0x87, 0x85, 0x08, 0x91,
  27768. 0x72, 0x77, 0xB9, 0x3E, 0x10, 0x70, 0x6B, 0x16,
  27769. 0x14, 0x97, 0x2B, 0x2A, 0x94, 0xC7, 0x31, 0x0F,
  27770. 0xE9, 0xC7, 0x08, 0xC2, 0x31, 0xA1, 0xA8, 0xAC,
  27771. 0x8D, 0x93, 0x14, 0xA5, 0x29, 0xA9, 0x7F, 0x46,
  27772. 0x9B, 0xF6, 0x49, 0x62, 0xD8, 0x20, 0x64, 0x84,
  27773. 0x43, 0x09, 0x9A, 0x07, 0x6D, 0x55, 0xD4, 0xCE,
  27774. 0xA8, 0x24, 0xA5, 0x83, 0x04, 0x84, 0x4F, 0x99,
  27775. 0x49, 0x7C, 0x10, 0xA2, 0x51, 0x48, 0x61, 0x8A,
  27776. 0x31, 0x5D, 0x72, 0xCA, 0x85, 0x7D, 0x1B, 0x04,
  27777. 0xD5, 0x75, 0xB9, 0x4F, 0x85, 0xC0, 0x1D, 0x19,
  27778. 0xBE, 0xF2, 0x11, 0xBF, 0x0A, 0xA3, 0x36, 0x2E,
  27779. 0x70, 0x41, 0xFD, 0x16, 0x59, 0x6D, 0x80, 0x8E,
  27780. 0x86, 0x7B, 0x44, 0xC4, 0xC0, 0x0D, 0x1C, 0xDA,
  27781. 0x34, 0x18, 0x96, 0x77, 0x17, 0xF1, 0x47, 0xD0,
  27782. 0xEB, 0x21, 0xB4, 0x2A, 0xAE, 0xE7, 0x4A, 0xC3,
  27783. 0x5D, 0x0B, 0x92, 0x41, 0x4B, 0x95, 0x85, 0x31,
  27784. 0xAA, 0xDF, 0x46, 0x3E, 0xC6, 0x30, 0x5A, 0xE5,
  27785. 0xEC, 0xAF, 0x79, 0x17, 0x40, 0x02, 0xF2, 0x6D,
  27786. 0xDE, 0xCC, 0x81, 0x3B, 0xF3, 0x26, 0x72, 0xE8,
  27787. 0x52, 0x9D, 0x95, 0xA4, 0xE7, 0x30, 0xA7, 0xAB,
  27788. 0x4A, 0x3E, 0x8F, 0x8A, 0x8A, 0xF9, 0x79, 0xA6,
  27789. 0x65, 0xEA, 0xFD, 0x46, 0x5F, 0xC6, 0x4A, 0x0C,
  27790. 0x5F, 0x8F, 0x3F, 0x90, 0x03, 0x48, 0x94, 0x15,
  27791. 0x89, 0x9D, 0x59, 0xA5, 0x43, 0xD8, 0x20, 0x8C,
  27792. 0x54, 0xA3, 0x16, 0x65, 0x29, 0xB5, 0x39, 0x22
  27793. };
  27794. const byte kyber768_sk[] = {
  27795. 0x07, 0x63, 0x8F, 0xB6, 0x98, 0x68, 0xF3, 0xD3,
  27796. 0x20, 0xE5, 0x86, 0x2B, 0xD9, 0x69, 0x33, 0xFE,
  27797. 0xB3, 0x11, 0xB3, 0x62, 0x09, 0x3C, 0x9B, 0x5D,
  27798. 0x50, 0x17, 0x0B, 0xCE, 0xD4, 0x3F, 0x1B, 0x53,
  27799. 0x6D, 0x9A, 0x20, 0x4B, 0xB1, 0xF2, 0x26, 0x95,
  27800. 0x95, 0x0B, 0xA1, 0xF2, 0xA9, 0xE8, 0xEB, 0x82,
  27801. 0x8B, 0x28, 0x44, 0x88, 0x76, 0x0B, 0x3F, 0xC8,
  27802. 0x4F, 0xAB, 0xA0, 0x42, 0x75, 0xD5, 0x62, 0x8E,
  27803. 0x39, 0xC5, 0xB2, 0x47, 0x13, 0x74, 0x28, 0x3C,
  27804. 0x50, 0x32, 0x99, 0xC0, 0xAB, 0x49, 0xB6, 0x6B,
  27805. 0x8B, 0xBB, 0x56, 0xA4, 0x18, 0x66, 0x24, 0xF9,
  27806. 0x19, 0xA2, 0xBA, 0x59, 0xBB, 0x08, 0xD8, 0x55,
  27807. 0x18, 0x80, 0xC2, 0xBE, 0xFC, 0x4F, 0x87, 0xF2,
  27808. 0x5F, 0x59, 0xAB, 0x58, 0x7A, 0x79, 0xC3, 0x27,
  27809. 0xD7, 0x92, 0xD5, 0x4C, 0x97, 0x4A, 0x69, 0x26,
  27810. 0x2F, 0xF8, 0xA7, 0x89, 0x38, 0x28, 0x9E, 0x9A,
  27811. 0x87, 0xB6, 0x88, 0xB0, 0x83, 0xE0, 0x59, 0x5F,
  27812. 0xE2, 0x18, 0xB6, 0xBB, 0x15, 0x05, 0x94, 0x1C,
  27813. 0xE2, 0xE8, 0x1A, 0x5A, 0x64, 0xC5, 0xAA, 0xC6,
  27814. 0x04, 0x17, 0x25, 0x69, 0x85, 0x34, 0x9E, 0xE4,
  27815. 0x7A, 0x52, 0x42, 0x0A, 0x5F, 0x97, 0x47, 0x7B,
  27816. 0x72, 0x36, 0xAC, 0x76, 0xBC, 0x70, 0xE8, 0x28,
  27817. 0x87, 0x29, 0x28, 0x7E, 0xE3, 0xE3, 0x4A, 0x3D,
  27818. 0xBC, 0x36, 0x83, 0xC0, 0xB7, 0xB1, 0x00, 0x29,
  27819. 0xFC, 0x20, 0x34, 0x18, 0x53, 0x7E, 0x74, 0x66,
  27820. 0xBA, 0x63, 0x85, 0xA8, 0xFF, 0x30, 0x1E, 0xE1,
  27821. 0x27, 0x08, 0xF8, 0x2A, 0xAA, 0x1E, 0x38, 0x0F,
  27822. 0xC7, 0xA8, 0x8F, 0x8F, 0x20, 0x5A, 0xB7, 0xE8,
  27823. 0x8D, 0x7E, 0x95, 0x95, 0x2A, 0x55, 0xBA, 0x20,
  27824. 0xD0, 0x9B, 0x79, 0xA4, 0x71, 0x41, 0xD6, 0x2B,
  27825. 0xF6, 0xEB, 0x7D, 0xD3, 0x07, 0xB0, 0x8E, 0xCA,
  27826. 0x13, 0xA5, 0xBC, 0x5F, 0x6B, 0x68, 0x58, 0x1C,
  27827. 0x68, 0x65, 0xB2, 0x7B, 0xBC, 0xDD, 0xAB, 0x14,
  27828. 0x2F, 0x4B, 0x2C, 0xBF, 0xF4, 0x88, 0xC8, 0xA2,
  27829. 0x27, 0x05, 0xFA, 0xA9, 0x8A, 0x2B, 0x9E, 0xEA,
  27830. 0x35, 0x30, 0xC7, 0x66, 0x62, 0x33, 0x5C, 0xC7,
  27831. 0xEA, 0x3A, 0x00, 0x77, 0x77, 0x25, 0xEB, 0xCC,
  27832. 0xCD, 0x2A, 0x46, 0x36, 0xB2, 0xD9, 0x12, 0x2F,
  27833. 0xF3, 0xAB, 0x77, 0x12, 0x3C, 0xE0, 0x88, 0x3C,
  27834. 0x19, 0x11, 0x11, 0x5E, 0x50, 0xC9, 0xE8, 0xA9,
  27835. 0x41, 0x94, 0xE4, 0x8D, 0xD0, 0xD0, 0x9C, 0xFF,
  27836. 0xB3, 0xAD, 0xCD, 0x2C, 0x1E, 0x92, 0x43, 0x09,
  27837. 0x03, 0xD0, 0x7A, 0xDB, 0xF0, 0x05, 0x32, 0x03,
  27838. 0x15, 0x75, 0xAA, 0x7F, 0x9E, 0x7B, 0x5A, 0x1F,
  27839. 0x33, 0x62, 0xDE, 0xC9, 0x36, 0xD4, 0x04, 0x3C,
  27840. 0x05, 0xF2, 0x47, 0x6C, 0x07, 0x57, 0x8B, 0xC9,
  27841. 0xCB, 0xAF, 0x2A, 0xB4, 0xE3, 0x82, 0x72, 0x7A,
  27842. 0xD4, 0x16, 0x86, 0xA9, 0x6B, 0x25, 0x48, 0x82,
  27843. 0x0B, 0xB0, 0x3B, 0x32, 0xF1, 0x1B, 0x28, 0x11,
  27844. 0xAD, 0x62, 0xF4, 0x89, 0xE9, 0x51, 0x63, 0x2A,
  27845. 0xBA, 0x0D, 0x1D, 0xF8, 0x96, 0x80, 0xCC, 0x8A,
  27846. 0x8B, 0x53, 0xB4, 0x81, 0xD9, 0x2A, 0x68, 0xD7,
  27847. 0x0B, 0x4E, 0xA1, 0xC3, 0xA6, 0xA5, 0x61, 0xC0,
  27848. 0x69, 0x28, 0x82, 0xB5, 0xCA, 0x8C, 0xC9, 0x42,
  27849. 0xA8, 0xD4, 0x95, 0xAF, 0xCB, 0x06, 0xDE, 0x89,
  27850. 0x49, 0x8F, 0xB9, 0x35, 0xB7, 0x75, 0x90, 0x8F,
  27851. 0xE7, 0xA0, 0x3E, 0x32, 0x4D, 0x54, 0xCC, 0x19,
  27852. 0xD4, 0xE1, 0xAA, 0xBD, 0x35, 0x93, 0xB3, 0x8B,
  27853. 0x19, 0xEE, 0x13, 0x88, 0xFE, 0x49, 0x2B, 0x43,
  27854. 0x12, 0x7E, 0x5A, 0x50, 0x42, 0x53, 0x78, 0x6A,
  27855. 0x0D, 0x69, 0xAD, 0x32, 0x60, 0x1C, 0x28, 0xE2,
  27856. 0xC8, 0x85, 0x04, 0xA5, 0xBA, 0x59, 0x97, 0x06,
  27857. 0x02, 0x3A, 0x61, 0x36, 0x3E, 0x17, 0xC6, 0xB9,
  27858. 0xBB, 0x59, 0xBD, 0xC6, 0x97, 0x45, 0x2C, 0xD0,
  27859. 0x59, 0x45, 0x19, 0x83, 0xD7, 0x38, 0xCA, 0x3F,
  27860. 0xD0, 0x34, 0xE3, 0xF5, 0x98, 0x88, 0x54, 0xCA,
  27861. 0x05, 0x03, 0x1D, 0xB0, 0x96, 0x11, 0x49, 0x89,
  27862. 0x88, 0x19, 0x7C, 0x6B, 0x30, 0xD2, 0x58, 0xDF,
  27863. 0xE2, 0x62, 0x65, 0x54, 0x1C, 0x89, 0xA4, 0xB3,
  27864. 0x1D, 0x68, 0x64, 0xE9, 0x38, 0x9B, 0x03, 0xCB,
  27865. 0x74, 0xF7, 0xEC, 0x43, 0x23, 0xFB, 0x94, 0x21,
  27866. 0xA4, 0xB9, 0x79, 0x0A, 0x26, 0xD1, 0x7B, 0x03,
  27867. 0x98, 0xA2, 0x67, 0x67, 0x35, 0x09, 0x09, 0xF8,
  27868. 0x4D, 0x57, 0xB6, 0x69, 0x4D, 0xF8, 0x30, 0x66,
  27869. 0x4C, 0xA8, 0xB3, 0xC3, 0xC0, 0x3E, 0xD2, 0xAE,
  27870. 0x67, 0xB8, 0x90, 0x06, 0x86, 0x8A, 0x68, 0x52,
  27871. 0x7C, 0xCD, 0x66, 0x64, 0x59, 0xAB, 0x7F, 0x05,
  27872. 0x66, 0x71, 0x00, 0x0C, 0x61, 0x64, 0xD3, 0xA7,
  27873. 0xF2, 0x66, 0xA1, 0x4D, 0x97, 0xCB, 0xD7, 0x00,
  27874. 0x4D, 0x6C, 0x92, 0xCA, 0xCA, 0x77, 0x0B, 0x84,
  27875. 0x4A, 0x4F, 0xA9, 0xB1, 0x82, 0xE7, 0xB1, 0x8C,
  27876. 0xA8, 0x85, 0x08, 0x2A, 0xC5, 0x64, 0x6F, 0xCB,
  27877. 0x4A, 0x14, 0xE1, 0x68, 0x5F, 0xEB, 0x0C, 0x9C,
  27878. 0xE3, 0x37, 0x2A, 0xB9, 0x53, 0x65, 0xC0, 0x4F,
  27879. 0xD8, 0x30, 0x84, 0xF8, 0x0A, 0x23, 0xFF, 0x10,
  27880. 0xA0, 0x5B, 0xF1, 0x5F, 0x7F, 0xA5, 0xAC, 0xC6,
  27881. 0xC0, 0xCB, 0x46, 0x2C, 0x33, 0xCA, 0x52, 0x4F,
  27882. 0xA6, 0xB8, 0xBB, 0x35, 0x90, 0x43, 0xBA, 0x68,
  27883. 0x60, 0x9E, 0xAA, 0x25, 0x36, 0xE8, 0x1D, 0x08,
  27884. 0x46, 0x3B, 0x19, 0x65, 0x3B, 0x54, 0x35, 0xBA,
  27885. 0x94, 0x6C, 0x9A, 0xDD, 0xEB, 0x20, 0x2B, 0x04,
  27886. 0xB0, 0x31, 0xCC, 0x96, 0x0D, 0xCC, 0x12, 0xE4,
  27887. 0x51, 0x8D, 0x42, 0x8B, 0x32, 0xB2, 0x57, 0xA4,
  27888. 0xFC, 0x73, 0x13, 0xD3, 0xA7, 0x98, 0x0D, 0x80,
  27889. 0x08, 0x2E, 0x93, 0x4F, 0x9D, 0x95, 0xC3, 0x2B,
  27890. 0x0A, 0x01, 0x91, 0xA2, 0x36, 0x04, 0x38, 0x4D,
  27891. 0xD9, 0xE0, 0x79, 0xBB, 0xBA, 0xA2, 0x66, 0xD1,
  27892. 0x4C, 0x3F, 0x75, 0x6B, 0x9F, 0x21, 0x33, 0x10,
  27893. 0x74, 0x33, 0xA4, 0xE8, 0x3F, 0xA7, 0x18, 0x72,
  27894. 0x82, 0xA8, 0x09, 0x20, 0x3A, 0x4F, 0xAF, 0x84,
  27895. 0x18, 0x51, 0x83, 0x3D, 0x12, 0x1A, 0xC3, 0x83,
  27896. 0x84, 0x3A, 0x5E, 0x55, 0xBC, 0x23, 0x81, 0x42,
  27897. 0x5E, 0x16, 0xC7, 0xDB, 0x4C, 0xC9, 0xAB, 0x5C,
  27898. 0x1B, 0x0D, 0x91, 0xA4, 0x7E, 0x2B, 0x8D, 0xE0,
  27899. 0xE5, 0x82, 0xC8, 0x6B, 0x6B, 0x0D, 0x90, 0x7B,
  27900. 0xB3, 0x60, 0xB9, 0x7F, 0x40, 0xAB, 0x5D, 0x03,
  27901. 0x8F, 0x6B, 0x75, 0xC8, 0x14, 0xB2, 0x7D, 0x9B,
  27902. 0x96, 0x8D, 0x41, 0x98, 0x32, 0xBC, 0x8C, 0x2B,
  27903. 0xEE, 0x60, 0x5E, 0xF6, 0xE5, 0x05, 0x9D, 0x33,
  27904. 0x10, 0x0D, 0x90, 0x48, 0x5D, 0x37, 0x84, 0x50,
  27905. 0x01, 0x42, 0x21, 0x73, 0x6C, 0x07, 0x40, 0x7C,
  27906. 0xAC, 0x26, 0x04, 0x08, 0xAA, 0x64, 0x92, 0x66,
  27907. 0x19, 0x78, 0x8B, 0x86, 0x01, 0xC2, 0xA7, 0x52,
  27908. 0xD1, 0xA6, 0xCB, 0xF8, 0x20, 0xD7, 0xC7, 0xA0,
  27909. 0x47, 0x16, 0x20, 0x32, 0x25, 0xB3, 0x89, 0x5B,
  27910. 0x93, 0x42, 0xD1, 0x47, 0xA8, 0x18, 0x5C, 0xFC,
  27911. 0x1B, 0xB6, 0x5B, 0xA0, 0x6B, 0x41, 0x42, 0x33,
  27912. 0x99, 0x03, 0xC0, 0xAC, 0x46, 0x51, 0x38, 0x5B,
  27913. 0x45, 0xD9, 0x8A, 0x8B, 0x19, 0xD2, 0x8C, 0xD6,
  27914. 0xBA, 0xB0, 0x88, 0x78, 0x7F, 0x7E, 0xE1, 0xB1,
  27915. 0x24, 0x61, 0x76, 0x6B, 0x43, 0xCB, 0xCC, 0xB9,
  27916. 0x64, 0x34, 0x42, 0x7D, 0x93, 0xC0, 0x65, 0x55,
  27917. 0x06, 0x88, 0xF6, 0x94, 0x8E, 0xD1, 0xB5, 0x47,
  27918. 0x5A, 0x42, 0x5F, 0x1B, 0x85, 0x20, 0x9D, 0x06,
  27919. 0x1C, 0x08, 0xB5, 0x6C, 0x1C, 0xC0, 0x69, 0xF6,
  27920. 0xC0, 0xA7, 0xC6, 0xF2, 0x93, 0x58, 0xCA, 0xB9,
  27921. 0x11, 0x08, 0x77, 0x32, 0xA6, 0x49, 0xD2, 0x7C,
  27922. 0x9B, 0x98, 0xF9, 0xA4, 0x88, 0x79, 0x38, 0x7D,
  27923. 0x9B, 0x00, 0xC2, 0x59, 0x59, 0xA7, 0x16, 0x54,
  27924. 0xD6, 0xF6, 0xA9, 0x46, 0x16, 0x45, 0x13, 0xE4,
  27925. 0x7A, 0x75, 0xD0, 0x05, 0x98, 0x6C, 0x23, 0x63,
  27926. 0xC0, 0x9F, 0x6B, 0x53, 0x7E, 0xCA, 0x78, 0xB9,
  27927. 0x30, 0x3A, 0x5F, 0xA4, 0x57, 0x60, 0x8A, 0x58,
  27928. 0x6A, 0x65, 0x3A, 0x34, 0x7D, 0xB0, 0x4D, 0xFC,
  27929. 0xC1, 0x91, 0x75, 0xB3, 0xA3, 0x01, 0x17, 0x25,
  27930. 0x36, 0x06, 0x2A, 0x65, 0x8A, 0x95, 0x27, 0x75,
  27931. 0x70, 0xC8, 0x85, 0x2C, 0xA8, 0x97, 0x3F, 0x4A,
  27932. 0xE1, 0x23, 0xA3, 0x34, 0x04, 0x7D, 0xD7, 0x11,
  27933. 0xC8, 0x92, 0x7A, 0x63, 0x4A, 0x03, 0x38, 0x8A,
  27934. 0x52, 0x7B, 0x03, 0x4B, 0xF7, 0xA8, 0x17, 0x0F,
  27935. 0xA7, 0x02, 0xC1, 0xF7, 0xC2, 0x3E, 0xC3, 0x2D,
  27936. 0x18, 0xA2, 0x37, 0x48, 0x90, 0xBE, 0x9C, 0x78,
  27937. 0x7A, 0x94, 0x09, 0xC8, 0x2D, 0x19, 0x2C, 0x4B,
  27938. 0xB7, 0x05, 0xA2, 0xF9, 0x96, 0xCE, 0x40, 0x5D,
  27939. 0xA7, 0x2C, 0x2D, 0x9C, 0x84, 0x3E, 0xE9, 0xF8,
  27940. 0x31, 0x3E, 0xCC, 0x7F, 0x86, 0xD6, 0x29, 0x4D,
  27941. 0x59, 0x15, 0x9D, 0x9A, 0x87, 0x9A, 0x54, 0x2E,
  27942. 0x26, 0x09, 0x22, 0xAD, 0xF9, 0x99, 0x05, 0x1C,
  27943. 0xC4, 0x52, 0x00, 0xC9, 0xFF, 0xDB, 0x60, 0x44,
  27944. 0x9C, 0x49, 0x46, 0x59, 0x79, 0x27, 0x23, 0x67,
  27945. 0xC0, 0x83, 0xA7, 0xD6, 0x26, 0x7A, 0x3E, 0xD7,
  27946. 0xA7, 0xFD, 0x47, 0x95, 0x7C, 0x21, 0x93, 0x27,
  27947. 0xF7, 0xCA, 0x73, 0xA4, 0x00, 0x7E, 0x16, 0x27,
  27948. 0xF0, 0x0B, 0x11, 0xCC, 0x80, 0x57, 0x3C, 0x15,
  27949. 0xAE, 0xE6, 0x64, 0x0F, 0xB8, 0x56, 0x2D, 0xFA,
  27950. 0x6B, 0x24, 0x0C, 0xA0, 0xAD, 0x35, 0x1A, 0xC4,
  27951. 0xAC, 0x15, 0x5B, 0x96, 0xC1, 0x4C, 0x8A, 0xB1,
  27952. 0x3D, 0xD2, 0x62, 0xCD, 0xFD, 0x51, 0xC4, 0xBB,
  27953. 0x55, 0x72, 0xFD, 0x61, 0x65, 0x53, 0xD1, 0x7B,
  27954. 0xDD, 0x43, 0x0A, 0xCB, 0xEA, 0x3E, 0x95, 0xF0,
  27955. 0xB6, 0x98, 0xD6, 0x69, 0x90, 0xAB, 0x51, 0xE5,
  27956. 0xD0, 0x37, 0x83, 0xA8, 0xB3, 0xD2, 0x78, 0xA5,
  27957. 0x72, 0x04, 0x54, 0xCF, 0x96, 0x95, 0xCF, 0xDC,
  27958. 0xA0, 0x84, 0x85, 0xBA, 0x09, 0x9C, 0x51, 0xCD,
  27959. 0x92, 0xA7, 0xEA, 0x75, 0x87, 0xC1, 0xD1, 0x5C,
  27960. 0x28, 0xE6, 0x09, 0xA8, 0x18, 0x52, 0x60, 0x1B,
  27961. 0x06, 0x04, 0x01, 0x06, 0x79, 0xAA, 0x48, 0x2D,
  27962. 0x51, 0x26, 0x1E, 0xC3, 0x6E, 0x36, 0xB8, 0x71,
  27963. 0x96, 0x76, 0x21, 0x7F, 0xD7, 0x4C, 0x54, 0x78,
  27964. 0x64, 0x88, 0xF4, 0xB4, 0x96, 0x9C, 0x05, 0xA8,
  27965. 0xBA, 0x27, 0xCA, 0x3A, 0x77, 0xCC, 0xE7, 0x3B,
  27966. 0x96, 0x59, 0x23, 0xCA, 0x55, 0x4E, 0x42, 0x2B,
  27967. 0x9B, 0x61, 0xF4, 0x75, 0x46, 0x41, 0x60, 0x8A,
  27968. 0xC1, 0x6C, 0x9B, 0x85, 0x87, 0xA3, 0x2C, 0x1C,
  27969. 0x5D, 0xD7, 0x88, 0xF8, 0x8B, 0x36, 0xB7, 0x17,
  27970. 0xA4, 0x69, 0x65, 0x63, 0x5D, 0xEB, 0x67, 0xF4,
  27971. 0x5B, 0x12, 0x9B, 0x99, 0x07, 0x09, 0x09, 0xC9,
  27972. 0x3E, 0xB8, 0x0B, 0x42, 0xC2, 0xB3, 0xF3, 0xF7,
  27973. 0x03, 0x43, 0xA7, 0xCF, 0x37, 0xE8, 0x52, 0x0E,
  27974. 0x7B, 0xCF, 0xC4, 0x16, 0xAC, 0xA4, 0xF1, 0x8C,
  27975. 0x79, 0x81, 0x26, 0x2B, 0xA2, 0xBF, 0xC7, 0x56,
  27976. 0xAE, 0x03, 0x27, 0x8F, 0x0E, 0xC6, 0x6D, 0xC2,
  27977. 0x05, 0x76, 0x96, 0x82, 0x4B, 0xA6, 0x76, 0x98,
  27978. 0x65, 0xA6, 0x01, 0xD7, 0x14, 0x8E, 0xF6, 0xF5,
  27979. 0x4E, 0x5A, 0xF5, 0x68, 0x6A, 0xA2, 0x90, 0x6F,
  27980. 0x99, 0x4C, 0xE3, 0x8A, 0x5E, 0x0B, 0x93, 0x8F,
  27981. 0x23, 0x90, 0x07, 0x00, 0x30, 0x22, 0xC0, 0x33,
  27982. 0x92, 0xDF, 0x34, 0x01, 0xB1, 0xE4, 0xA3, 0xA7,
  27983. 0xEB, 0xC6, 0x16, 0x14, 0x49, 0xF7, 0x33, 0x74,
  27984. 0xC8, 0xB0, 0x14, 0x03, 0x69, 0x34, 0x3D, 0x92,
  27985. 0x95, 0xFD, 0xF5, 0x11, 0x84, 0x5C, 0x4A, 0x46,
  27986. 0xEB, 0xAA, 0xB6, 0xCA, 0x54, 0x92, 0xF6, 0x80,
  27987. 0x0B, 0x98, 0xC0, 0xCC, 0x80, 0x36, 0x53, 0xA4,
  27988. 0xB1, 0xD6, 0xE6, 0xAA, 0xED, 0x19, 0x32, 0xBA,
  27989. 0xCC, 0x5F, 0xEF, 0xAA, 0x81, 0x8B, 0xA5, 0x02,
  27990. 0x85, 0x9B, 0xA5, 0x49, 0x4C, 0x5F, 0x54, 0x02,
  27991. 0xC8, 0x53, 0x6A, 0x9C, 0x4C, 0x18, 0x88, 0x15,
  27992. 0x06, 0x17, 0xF8, 0x00, 0x98, 0xF6, 0xB2, 0xA9,
  27993. 0x9C, 0x39, 0xBC, 0x5D, 0xC7, 0xCF, 0x3B, 0x59,
  27994. 0x00, 0xA2, 0x13, 0x29, 0xAB, 0x59, 0x05, 0x3A,
  27995. 0xBA, 0xA6, 0x4E, 0xD1, 0x63, 0xE8, 0x59, 0xA8,
  27996. 0xB3, 0xB3, 0xCA, 0x33, 0x59, 0xB7, 0x50, 0xCC,
  27997. 0xC3, 0xE7, 0x10, 0xC7, 0xAC, 0x43, 0xC8, 0x19,
  27998. 0x1C, 0xB5, 0xD6, 0x88, 0x70, 0xC0, 0x63, 0x91,
  27999. 0xC0, 0xCB, 0x8A, 0xEC, 0x72, 0xB8, 0x97, 0xAC,
  28000. 0x6B, 0xE7, 0xFB, 0xAA, 0xCC, 0x67, 0x6E, 0xD6,
  28001. 0x63, 0x14, 0xC8, 0x36, 0x30, 0xE8, 0x94, 0x48,
  28002. 0xC8, 0x8A, 0x1D, 0xF0, 0x4A, 0xCE, 0xB2, 0x3A,
  28003. 0xBF, 0x2E, 0x40, 0x9E, 0xF3, 0x33, 0xC6, 0x22,
  28004. 0x28, 0x9C, 0x18, 0xA2, 0x13, 0x4E, 0x65, 0x0C,
  28005. 0x45, 0x25, 0x7E, 0x47, 0x47, 0x5F, 0xA3, 0x3A,
  28006. 0xA5, 0x37, 0xA5, 0xA8, 0xF7, 0x68, 0x02, 0x14,
  28007. 0x71, 0x6C, 0x50, 0xD4, 0x70, 0xE3, 0x28, 0x49,
  28008. 0x63, 0xCA, 0x64, 0xF5, 0x46, 0x77, 0xAE, 0xC5,
  28009. 0x4B, 0x52, 0x72, 0x16, 0x2B, 0xF5, 0x2B, 0xC8,
  28010. 0x14, 0x2E, 0x1D, 0x41, 0x83, 0xFC, 0x01, 0x74,
  28011. 0x54, 0xA6, 0xB5, 0xA4, 0x96, 0x83, 0x17, 0x59,
  28012. 0x06, 0x40, 0x24, 0x74, 0x59, 0x78, 0xCB, 0xD5,
  28013. 0x1A, 0x6C, 0xED, 0xC8, 0x95, 0x5D, 0xE4, 0xCC,
  28014. 0x6D, 0x36, 0x36, 0x70, 0xA4, 0x74, 0x66, 0xE8,
  28015. 0x2B, 0xE5, 0xC2, 0x36, 0x03, 0xA1, 0x7B, 0xF2,
  28016. 0x2A, 0xCD, 0xB7, 0xCC, 0x98, 0x4A, 0xF0, 0x8C,
  28017. 0x87, 0xE1, 0x4E, 0x27, 0x75, 0x3C, 0xF5, 0x87,
  28018. 0xA8, 0xEC, 0x34, 0x47, 0xE6, 0x2C, 0x64, 0x9E,
  28019. 0x88, 0x7A, 0x67, 0xC3, 0x6C, 0x9C, 0xE9, 0x87,
  28020. 0x21, 0xB6, 0x97, 0x21, 0x32, 0x75, 0x64, 0x6B,
  28021. 0x19, 0x4F, 0x36, 0x75, 0x86, 0x73, 0xA8, 0xED,
  28022. 0x11, 0x28, 0x44, 0x55, 0xAF, 0xC7, 0xA8, 0x52,
  28023. 0x9F, 0x69, 0xC9, 0x7A, 0x3C, 0x2D, 0x7B, 0x8C,
  28024. 0x63, 0x6C, 0x0B, 0xA5, 0x56, 0x14, 0xB7, 0x68,
  28025. 0xE6, 0x24, 0xE7, 0x12, 0x93, 0x0F, 0x77, 0x61,
  28026. 0x69, 0xB0, 0x17, 0x15, 0x72, 0x53, 0x51, 0xBC,
  28027. 0x74, 0xB4, 0x73, 0x95, 0xED, 0x52, 0xB2, 0x5A,
  28028. 0x13, 0x13, 0xC9, 0x51, 0x64, 0x81, 0x4C, 0x34,
  28029. 0xC9, 0x79, 0xCB, 0xDF, 0xAB, 0x85, 0x95, 0x46,
  28030. 0x62, 0xCA, 0xB4, 0x85, 0xE7, 0x50, 0x87, 0xA9,
  28031. 0x8C, 0xC7, 0x4B, 0xB8, 0x2C, 0xA2, 0xD1, 0xB5,
  28032. 0xBF, 0x28, 0x03, 0x23, 0x84, 0x80, 0x63, 0x8C,
  28033. 0x40, 0xE9, 0x0B, 0x43, 0xC7, 0x46, 0x0E, 0x7A,
  28034. 0xA9, 0x17, 0xF0, 0x10, 0x15, 0x1F, 0xAB, 0x11,
  28035. 0x69, 0x98, 0x7B, 0x37, 0x2A, 0xBB, 0x59, 0x27,
  28036. 0x1F, 0x70, 0x06, 0xC2, 0x4E, 0x60, 0x23, 0x6B,
  28037. 0x84, 0xB9, 0xDD, 0xD6, 0x00, 0x62, 0x37, 0x04,
  28038. 0x25, 0x46, 0x17, 0xFB, 0x49, 0x8D, 0x89, 0xE5,
  28039. 0x8B, 0x03, 0x68, 0xBC, 0xB2, 0x10, 0x3E, 0x79,
  28040. 0x35, 0x3E, 0xB5, 0x87, 0x86, 0x0C, 0x14, 0x22,
  28041. 0xE4, 0x76, 0x16, 0x2E, 0x42, 0x5B, 0xC2, 0x38,
  28042. 0x1D, 0xB8, 0x2C, 0x65, 0x92, 0x73, 0x7E, 0x1D,
  28043. 0xD6, 0x02, 0x86, 0x4B, 0x01, 0x67, 0xA7, 0x1E,
  28044. 0xC1, 0xF2, 0x23, 0x30, 0x5C, 0x02, 0xFE, 0x25,
  28045. 0x05, 0x2A, 0xF2, 0xB3, 0xB5, 0xA5, 0x5A, 0x0D,
  28046. 0x7A, 0x20, 0x22, 0xD9, 0xA7, 0x98, 0xDC, 0x0C,
  28047. 0x58, 0x74, 0xA9, 0x87, 0x02, 0xAA, 0xF4, 0x05,
  28048. 0x4C, 0x5D, 0x80, 0x33, 0x8A, 0x52, 0x48, 0xB5,
  28049. 0xB7, 0xBD, 0x09, 0xC5, 0x3B, 0x5E, 0x2A, 0x08,
  28050. 0x4B, 0x04, 0x7D, 0x27, 0x7A, 0x86, 0x1B, 0x1A,
  28051. 0x73, 0xBB, 0x51, 0x48, 0x8D, 0xE0, 0x4E, 0xF5,
  28052. 0x73, 0xC8, 0x52, 0x30, 0xA0, 0x47, 0x0B, 0x73,
  28053. 0x17, 0x5C, 0x9F, 0xA5, 0x05, 0x94, 0xF6, 0x6A,
  28054. 0x5F, 0x50, 0xB4, 0x15, 0x00, 0x54, 0xC9, 0x3B,
  28055. 0x68, 0x18, 0x6F, 0x8B, 0x5C, 0xBC, 0x49, 0x31,
  28056. 0x6C, 0x85, 0x48, 0xA6, 0x42, 0xB2, 0xB3, 0x6A,
  28057. 0x1D, 0x45, 0x4C, 0x74, 0x89, 0xAC, 0x33, 0xB2,
  28058. 0xD2, 0xCE, 0x66, 0x68, 0x09, 0x67, 0x82, 0xA2,
  28059. 0xC1, 0xE0, 0x86, 0x6D, 0x21, 0xA6, 0x5E, 0x16,
  28060. 0xB5, 0x85, 0xE7, 0xAF, 0x86, 0x18, 0xBD, 0xF3,
  28061. 0x18, 0x4C, 0x19, 0x86, 0x87, 0x85, 0x08, 0x91,
  28062. 0x72, 0x77, 0xB9, 0x3E, 0x10, 0x70, 0x6B, 0x16,
  28063. 0x14, 0x97, 0x2B, 0x2A, 0x94, 0xC7, 0x31, 0x0F,
  28064. 0xE9, 0xC7, 0x08, 0xC2, 0x31, 0xA1, 0xA8, 0xAC,
  28065. 0x8D, 0x93, 0x14, 0xA5, 0x29, 0xA9, 0x7F, 0x46,
  28066. 0x9B, 0xF6, 0x49, 0x62, 0xD8, 0x20, 0x64, 0x84,
  28067. 0x43, 0x09, 0x9A, 0x07, 0x6D, 0x55, 0xD4, 0xCE,
  28068. 0xA8, 0x24, 0xA5, 0x83, 0x04, 0x84, 0x4F, 0x99,
  28069. 0x49, 0x7C, 0x10, 0xA2, 0x51, 0x48, 0x61, 0x8A,
  28070. 0x31, 0x5D, 0x72, 0xCA, 0x85, 0x7D, 0x1B, 0x04,
  28071. 0xD5, 0x75, 0xB9, 0x4F, 0x85, 0xC0, 0x1D, 0x19,
  28072. 0xBE, 0xF2, 0x11, 0xBF, 0x0A, 0xA3, 0x36, 0x2E,
  28073. 0x70, 0x41, 0xFD, 0x16, 0x59, 0x6D, 0x80, 0x8E,
  28074. 0x86, 0x7B, 0x44, 0xC4, 0xC0, 0x0D, 0x1C, 0xDA,
  28075. 0x34, 0x18, 0x96, 0x77, 0x17, 0xF1, 0x47, 0xD0,
  28076. 0xEB, 0x21, 0xB4, 0x2A, 0xAE, 0xE7, 0x4A, 0xC3,
  28077. 0x5D, 0x0B, 0x92, 0x41, 0x4B, 0x95, 0x85, 0x31,
  28078. 0xAA, 0xDF, 0x46, 0x3E, 0xC6, 0x30, 0x5A, 0xE5,
  28079. 0xEC, 0xAF, 0x79, 0x17, 0x40, 0x02, 0xF2, 0x6D,
  28080. 0xDE, 0xCC, 0x81, 0x3B, 0xF3, 0x26, 0x72, 0xE8,
  28081. 0x52, 0x9D, 0x95, 0xA4, 0xE7, 0x30, 0xA7, 0xAB,
  28082. 0x4A, 0x3E, 0x8F, 0x8A, 0x8A, 0xF9, 0x79, 0xA6,
  28083. 0x65, 0xEA, 0xFD, 0x46, 0x5F, 0xC6, 0x4A, 0x0C,
  28084. 0x5F, 0x8F, 0x3F, 0x90, 0x03, 0x48, 0x94, 0x15,
  28085. 0x89, 0x9D, 0x59, 0xA5, 0x43, 0xD8, 0x20, 0x8C,
  28086. 0x54, 0xA3, 0x16, 0x65, 0x29, 0xB5, 0x39, 0x22,
  28087. 0xD4, 0xEC, 0x14, 0x3B, 0x50, 0xF0, 0x14, 0x23,
  28088. 0xB1, 0x77, 0x89, 0x5E, 0xDE, 0xE2, 0x2B, 0xB7,
  28089. 0x39, 0xF6, 0x47, 0xEC, 0xF8, 0x5F, 0x50, 0xBC,
  28090. 0x25, 0xEF, 0x7B, 0x5A, 0x72, 0x5D, 0xEE, 0x86,
  28091. 0x86, 0x26, 0xED, 0x79, 0xD4, 0x51, 0x14, 0x08,
  28092. 0x00, 0xE0, 0x3B, 0x59, 0xB9, 0x56, 0xF8, 0x21,
  28093. 0x0E, 0x55, 0x60, 0x67, 0x40, 0x7D, 0x13, 0xDC,
  28094. 0x90, 0xFA, 0x9E, 0x8B, 0x87, 0x2B, 0xFB, 0x8F
  28095. };
  28096. const byte kyber768_ct[] = {
  28097. 0xB5, 0x2C, 0x56, 0xB9, 0x2A, 0x4B, 0x7C, 0xE9,
  28098. 0xE4, 0xCB, 0x7C, 0x5B, 0x1B, 0x16, 0x31, 0x67,
  28099. 0xA8, 0xA1, 0x67, 0x5B, 0x2F, 0xDE, 0xF8, 0x4A,
  28100. 0x5B, 0x67, 0xCA, 0x15, 0xDB, 0x69, 0x4C, 0x9F,
  28101. 0x11, 0xBD, 0x02, 0x7C, 0x30, 0xAE, 0x22, 0xEC,
  28102. 0x92, 0x1A, 0x1D, 0x91, 0x15, 0x99, 0xAF, 0x05,
  28103. 0x85, 0xE4, 0x8D, 0x20, 0xDA, 0x70, 0xDF, 0x9F,
  28104. 0x39, 0xE3, 0x2E, 0xF9, 0x5D, 0x4C, 0x8F, 0x44,
  28105. 0xBF, 0xEF, 0xDA, 0xA5, 0xDA, 0x64, 0xF1, 0x05,
  28106. 0x46, 0x31, 0xD0, 0x4D, 0x6D, 0x3C, 0xFD, 0x0A,
  28107. 0x54, 0x0D, 0xD7, 0xBA, 0x38, 0x86, 0xE4, 0xB5,
  28108. 0xF1, 0x3E, 0x87, 0x87, 0x88, 0x60, 0x4C, 0x95,
  28109. 0xC0, 0x96, 0xEA, 0xB3, 0x91, 0x9F, 0x42, 0x75,
  28110. 0x21, 0x41, 0x9A, 0x94, 0x6C, 0x26, 0xCC, 0x04,
  28111. 0x14, 0x75, 0xD7, 0x12, 0x4C, 0xDC, 0x01, 0xD0,
  28112. 0x37, 0x3E, 0x5B, 0x09, 0xC7, 0xA7, 0x06, 0x03,
  28113. 0xCF, 0xDB, 0x4F, 0xB3, 0x40, 0x50, 0x23, 0xF2,
  28114. 0x26, 0x4D, 0xC3, 0xF9, 0x83, 0xC4, 0xFC, 0x02,
  28115. 0xA2, 0xD1, 0xB2, 0x68, 0xF2, 0x20, 0x8A, 0x1F,
  28116. 0x6E, 0x2A, 0x62, 0x09, 0xBF, 0xF1, 0x2F, 0x6F,
  28117. 0x46, 0x5F, 0x0B, 0x06, 0x9C, 0x3A, 0x7F, 0x84,
  28118. 0xF6, 0x06, 0xD8, 0xA9, 0x40, 0x64, 0x00, 0x3D,
  28119. 0x6E, 0xC1, 0x14, 0xC8, 0xE8, 0x08, 0xD3, 0x05,
  28120. 0x38, 0x84, 0xC1, 0xD5, 0xA1, 0x42, 0xFB, 0xF2,
  28121. 0x01, 0x12, 0xEB, 0x36, 0x0F, 0xDA, 0x3F, 0x0F,
  28122. 0x28, 0xB1, 0x72, 0xAE, 0x50, 0xF5, 0xE7, 0xD8,
  28123. 0x38, 0x01, 0xFB, 0x3F, 0x00, 0x64, 0xB6, 0x87,
  28124. 0x18, 0x70, 0x74, 0xBD, 0x7F, 0xE3, 0x0E, 0xDD,
  28125. 0xAA, 0x33, 0x4C, 0xF8, 0xFC, 0x04, 0xFA, 0x8C,
  28126. 0xED, 0x89, 0x9C, 0xEA, 0xDE, 0x4B, 0x4F, 0x28,
  28127. 0xB6, 0x83, 0x72, 0xBA, 0xF9, 0x8F, 0xF4, 0x82,
  28128. 0xA4, 0x15, 0xB7, 0x31, 0x15, 0x5B, 0x75, 0xCE,
  28129. 0xB9, 0x76, 0xBE, 0x0E, 0xA0, 0x28, 0x5B, 0xA0,
  28130. 0x1A, 0x27, 0xF1, 0x85, 0x7A, 0x8F, 0xB3, 0x77,
  28131. 0xA3, 0xAE, 0x0C, 0x23, 0xB2, 0xAA, 0x9A, 0x07,
  28132. 0x9B, 0xFA, 0xBF, 0xF0, 0xD5, 0xB2, 0xF1, 0xCD,
  28133. 0x9B, 0x71, 0x8B, 0xEA, 0x03, 0xC4, 0x2F, 0x34,
  28134. 0x3A, 0x39, 0xB4, 0xF1, 0x42, 0xD0, 0x1A, 0xD8,
  28135. 0xAC, 0xBB, 0x50, 0xE3, 0x88, 0x53, 0xCF, 0x9A,
  28136. 0x50, 0xC8, 0xB4, 0x4C, 0x3C, 0xF6, 0x71, 0xA4,
  28137. 0xA9, 0x04, 0x3B, 0x26, 0xDD, 0xBB, 0x24, 0x95,
  28138. 0x9A, 0xD6, 0x71, 0x5C, 0x08, 0x52, 0x18, 0x55,
  28139. 0xC7, 0x9A, 0x23, 0xB9, 0xC3, 0xD6, 0x47, 0x17,
  28140. 0x49, 0xC4, 0x07, 0x25, 0xBD, 0xD5, 0xC2, 0x77,
  28141. 0x6D, 0x43, 0xAE, 0xD2, 0x02, 0x04, 0xBA, 0xA1,
  28142. 0x41, 0xEF, 0xB3, 0x30, 0x49, 0x17, 0x47, 0x4B,
  28143. 0x7F, 0x9F, 0x7A, 0x4B, 0x08, 0xB1, 0xA9, 0x3D,
  28144. 0xAE, 0xD9, 0x8C, 0x67, 0x49, 0x53, 0x59, 0xD3,
  28145. 0x7D, 0x67, 0xF7, 0x43, 0x8B, 0xEE, 0x5E, 0x43,
  28146. 0x58, 0x56, 0x34, 0xB2, 0x6C, 0x6B, 0x38, 0x10,
  28147. 0xD7, 0xCD, 0xCB, 0xC0, 0xF6, 0xEB, 0x87, 0x7A,
  28148. 0x60, 0x87, 0xE6, 0x8A, 0xCB, 0x84, 0x80, 0xD3,
  28149. 0xA8, 0xCF, 0x69, 0x00, 0x44, 0x7E, 0x49, 0xB4,
  28150. 0x17, 0xF1, 0x5A, 0x53, 0xB6, 0x07, 0xA0, 0xE2,
  28151. 0x16, 0xB8, 0x55, 0x97, 0x0D, 0x37, 0x40, 0x68,
  28152. 0x70, 0xB4, 0x56, 0x87, 0x22, 0xDA, 0x77, 0xA4,
  28153. 0x08, 0x47, 0x03, 0x81, 0x67, 0x84, 0xE2, 0xF1,
  28154. 0x6B, 0xED, 0x18, 0x99, 0x65, 0x32, 0xC5, 0xD8,
  28155. 0xB7, 0xF5, 0xD2, 0x14, 0x46, 0x4E, 0x5F, 0x3F,
  28156. 0x6E, 0x90, 0x58, 0x67, 0xB0, 0xCE, 0x11, 0x9E,
  28157. 0x25, 0x2A, 0x66, 0x71, 0x32, 0x53, 0x54, 0x46,
  28158. 0x85, 0xD2, 0x08, 0xE1, 0x72, 0x39, 0x08, 0xA0,
  28159. 0xCE, 0x97, 0x83, 0x46, 0x52, 0xE0, 0x8A, 0xE7,
  28160. 0xBD, 0xC8, 0x81, 0xA1, 0x31, 0xB7, 0x3C, 0x71,
  28161. 0xE8, 0x4D, 0x20, 0xD6, 0x8F, 0xDE, 0xFF, 0x4F,
  28162. 0x5D, 0x70, 0xCD, 0x1A, 0xF5, 0x7B, 0x78, 0xE3,
  28163. 0x49, 0x1A, 0x98, 0x65, 0x94, 0x23, 0x21, 0x80,
  28164. 0x0A, 0x20, 0x3C, 0x05, 0xED, 0x1F, 0xEE, 0xB5,
  28165. 0xA2, 0x8E, 0x58, 0x4E, 0x19, 0xF6, 0x53, 0x5E,
  28166. 0x7F, 0x84, 0xE4, 0xA2, 0x4F, 0x84, 0xA7, 0x2D,
  28167. 0xCA, 0xF5, 0x64, 0x8B, 0x4A, 0x42, 0x35, 0xDD,
  28168. 0x66, 0x44, 0x64, 0x48, 0x2F, 0x03, 0x17, 0x6E,
  28169. 0x88, 0x8C, 0x28, 0xBF, 0xC6, 0xC1, 0xCB, 0x23,
  28170. 0x8C, 0xFF, 0xA3, 0x5A, 0x32, 0x1E, 0x71, 0x79,
  28171. 0x1D, 0x9E, 0xA8, 0xED, 0x08, 0x78, 0xC6, 0x11,
  28172. 0x21, 0xBF, 0x8D, 0x2A, 0x4A, 0xB2, 0xC1, 0xA5,
  28173. 0xE1, 0x20, 0xBC, 0x40, 0xAB, 0xB1, 0x89, 0x2D,
  28174. 0x17, 0x15, 0x09, 0x0A, 0x0E, 0xE4, 0x82, 0x52,
  28175. 0xCA, 0x29, 0x7A, 0x99, 0xAA, 0x0E, 0x51, 0x0C,
  28176. 0xF2, 0x6B, 0x1A, 0xDD, 0x06, 0xCA, 0x54, 0x3E,
  28177. 0x1C, 0x5D, 0x6B, 0xDC, 0xD3, 0xB9, 0xC5, 0x85,
  28178. 0xC8, 0x53, 0x80, 0x45, 0xDB, 0x5C, 0x25, 0x2E,
  28179. 0xC3, 0xC8, 0xC3, 0xC9, 0x54, 0xD9, 0xBE, 0x59,
  28180. 0x07, 0x09, 0x4A, 0x89, 0x4E, 0x60, 0xEA, 0xB4,
  28181. 0x35, 0x38, 0xCF, 0xEE, 0x82, 0xE8, 0xFF, 0xC0,
  28182. 0x79, 0x1B, 0x0D, 0x0F, 0x43, 0xAC, 0x16, 0x27,
  28183. 0x83, 0x0A, 0x61, 0xD5, 0x6D, 0xAD, 0x96, 0xC6,
  28184. 0x29, 0x58, 0xB0, 0xDE, 0x78, 0x0B, 0x78, 0xBD,
  28185. 0x47, 0xA6, 0x04, 0x55, 0x0D, 0xAB, 0x83, 0xFF,
  28186. 0xF2, 0x27, 0xC3, 0x24, 0x04, 0x94, 0x71, 0xF3,
  28187. 0x52, 0x48, 0xCF, 0xB8, 0x49, 0xB2, 0x57, 0x24,
  28188. 0xFF, 0x70, 0x4D, 0x52, 0x77, 0xAA, 0x35, 0x2D,
  28189. 0x55, 0x09, 0x58, 0xBE, 0x3B, 0x23, 0x7D, 0xFF,
  28190. 0x47, 0x3E, 0xC2, 0xAD, 0xBA, 0xEA, 0x48, 0xCA,
  28191. 0x26, 0x58, 0xAE, 0xFC, 0xC7, 0x7B, 0xBD, 0x42,
  28192. 0x64, 0xAB, 0x37, 0x4D, 0x70, 0xEA, 0xE5, 0xB9,
  28193. 0x64, 0x41, 0x6C, 0xE8, 0x22, 0x6A, 0x7E, 0x32,
  28194. 0x55, 0xA0, 0xF8, 0xD7, 0xE2, 0xAD, 0xCA, 0x06,
  28195. 0x2B, 0xCD, 0x6D, 0x78, 0xD6, 0x0D, 0x1B, 0x32,
  28196. 0xE1, 0x14, 0x05, 0xBE, 0x54, 0xB6, 0x6E, 0xF0,
  28197. 0xFD, 0xDD, 0x56, 0x77, 0x02, 0xA3, 0xBC, 0xCF,
  28198. 0xED, 0xE3, 0xC5, 0x84, 0x70, 0x12, 0x69, 0xED,
  28199. 0x14, 0x80, 0x9F, 0x06, 0xF8, 0x96, 0x83, 0x56,
  28200. 0xBB, 0x92, 0x67, 0xFE, 0x86, 0xE5, 0x14, 0x25,
  28201. 0x2E, 0x88, 0xBB, 0x5C, 0x30, 0xA7, 0xEC, 0xB3,
  28202. 0xD0, 0xE6, 0x21, 0x02, 0x1E, 0xE0, 0xFB, 0xF7,
  28203. 0x87, 0x1B, 0x09, 0x34, 0x2B, 0xF8, 0x4F, 0x55,
  28204. 0xC9, 0x7E, 0xAF, 0x86, 0xC4, 0x81, 0x89, 0xC7,
  28205. 0xFF, 0x4D, 0xF3, 0x89, 0xF0, 0x77, 0xE2, 0x80,
  28206. 0x6E, 0x5F, 0xA7, 0x3B, 0x3E, 0x94, 0x58, 0xA1,
  28207. 0x6C, 0x7E, 0x27, 0x5F, 0x4F, 0x60, 0x22, 0x75,
  28208. 0x58, 0x0E, 0xB7, 0xB7, 0x13, 0x5F, 0xB5, 0x37,
  28209. 0xFA, 0x0C, 0xD9, 0x5D, 0x6E, 0xA5, 0x8C, 0x10,
  28210. 0x8C, 0xD8, 0x94, 0x3D, 0x70, 0xC1, 0x64, 0x31,
  28211. 0x11, 0xF4, 0xF0, 0x1C, 0xA8, 0xA8, 0x27, 0x6A,
  28212. 0x90, 0x26, 0x66, 0xED, 0x81, 0xB7, 0x8D, 0x16,
  28213. 0x8B, 0x00, 0x6F, 0x16, 0xAA, 0xA3, 0xD8, 0xE4,
  28214. 0xCE, 0x4F, 0x4D, 0x0F, 0xB0, 0x99, 0x7E, 0x41,
  28215. 0xAE, 0xFF, 0xB5, 0xB3, 0xDA, 0xA8, 0x38, 0x73,
  28216. 0x2F, 0x35, 0x73, 0x49, 0x44, 0x7F, 0x38, 0x77,
  28217. 0x76, 0xC7, 0x93, 0xC0, 0x47, 0x9D, 0xE9, 0xE9,
  28218. 0x94, 0x98, 0xCC, 0x35, 0x6F, 0xDB, 0x00, 0x75,
  28219. 0xA7, 0x03, 0xF2, 0x3C, 0x55, 0xD4, 0x7B, 0x55,
  28220. 0x0E, 0xC8, 0x9B, 0x02, 0xAD, 0xE8, 0x93, 0x29,
  28221. 0x08, 0x6A, 0x50, 0x84, 0x34, 0x56, 0xFE, 0xDC,
  28222. 0x37, 0x88, 0xAC, 0x8D, 0x97, 0x23, 0x3C, 0x54,
  28223. 0x56, 0x04, 0x67, 0xEE, 0x1D, 0x0F, 0x02, 0x4B,
  28224. 0x18, 0x42, 0x8F, 0x0D, 0x73, 0xB3, 0x0E, 0x19,
  28225. 0xF5, 0xC6, 0x3B, 0x9A, 0xBF, 0x11, 0x41, 0x5B,
  28226. 0xEA, 0x4D, 0x01, 0x70, 0x13, 0x0B, 0xAA, 0xBD,
  28227. 0x33, 0xC0, 0x5E, 0x65, 0x24, 0xE5, 0xFB, 0x55,
  28228. 0x81, 0xB2, 0x2B, 0x04, 0x33, 0x34, 0x22, 0x48,
  28229. 0x26, 0x6D, 0x0F, 0x10, 0x53, 0xB2, 0x45, 0xCC,
  28230. 0x24, 0x62, 0xDC, 0x44, 0xD3, 0x49, 0x65, 0x10,
  28231. 0x24, 0x82, 0xA8, 0xED, 0x9E, 0x4E, 0x96, 0x4D,
  28232. 0x56, 0x83, 0xE5, 0xD4, 0x5D, 0x0C, 0x82, 0x69
  28233. };
  28234. const byte kyber768_ss[] = {
  28235. 0x91, 0x4C, 0xB6, 0x7F, 0xE5, 0xC3, 0x8E, 0x73,
  28236. 0xBF, 0x74, 0x18, 0x1C, 0x0A, 0xC5, 0x04, 0x28,
  28237. 0xDE, 0xDF, 0x77, 0x50, 0xA9, 0x80, 0x58, 0xF7,
  28238. 0xD5, 0x36, 0x70, 0x87, 0x74, 0x53, 0x5B, 0x29
  28239. };
  28240. #else
  28241. const byte kyber768_pk[] = {
  28242. 0x86, 0x1A, 0xCF, 0x24, 0x57, 0x9D, 0x8E, 0xE8,
  28243. 0x94, 0x63, 0xB4, 0x78, 0xE2, 0xE9, 0x53, 0xC7,
  28244. 0xD5, 0xCD, 0xA4, 0xD3, 0x07, 0xD1, 0x68, 0xCF,
  28245. 0xC6, 0x79, 0xB1, 0xA3, 0x79, 0x1F, 0x05, 0x83,
  28246. 0x82, 0x86, 0x0B, 0x1C, 0x77, 0x52, 0x87, 0x89,
  28247. 0xC6, 0x96, 0x4B, 0xCC, 0x40, 0x8A, 0x21, 0xCA,
  28248. 0x4E, 0x42, 0x8F, 0xE7, 0x33, 0x9F, 0x7C, 0x08,
  28249. 0x76, 0xA8, 0x58, 0xAB, 0xA9, 0x44, 0xB7, 0x3F,
  28250. 0x9C, 0xC9, 0xB8, 0xAC, 0xC4, 0xB7, 0x9A, 0x9F,
  28251. 0x72, 0xF7, 0xA0, 0x47, 0x72, 0x57, 0xB9, 0xF3,
  28252. 0x26, 0xE4, 0x4C, 0x2C, 0x5F, 0x8B, 0x8A, 0x17,
  28253. 0x58, 0x81, 0x42, 0xB0, 0x91, 0xA3, 0x12, 0x31,
  28254. 0xD0, 0x98, 0xB0, 0x57, 0x30, 0x45, 0x62, 0x90,
  28255. 0x6E, 0x96, 0x05, 0x47, 0x3D, 0x28, 0xB7, 0xB4,
  28256. 0x33, 0x2A, 0xB4, 0x49, 0x82, 0x48, 0x7B, 0x04,
  28257. 0x9E, 0x41, 0x93, 0x74, 0xA4, 0x8E, 0xAC, 0x6A,
  28258. 0xB8, 0xCB, 0x42, 0x6D, 0x13, 0xA4, 0x86, 0x28,
  28259. 0x28, 0x43, 0x02, 0xFB, 0x72, 0x71, 0xD1, 0x54,
  28260. 0xCA, 0x09, 0x06, 0xD8, 0xE2, 0x0F, 0x1D, 0x97,
  28261. 0xBE, 0x9C, 0x82, 0x2E, 0x78, 0x83, 0x79, 0x32,
  28262. 0xEB, 0x28, 0xEE, 0xB0, 0x83, 0x71, 0xBB, 0xC6,
  28263. 0x3A, 0x7A, 0x96, 0x6C, 0xAC, 0x7A, 0x05, 0xB5,
  28264. 0x50, 0x3C, 0x55, 0x1A, 0xEF, 0x80, 0x15, 0x14,
  28265. 0xE1, 0x6A, 0xF6, 0x81, 0x02, 0x64, 0x6C, 0x66,
  28266. 0x9D, 0xD8, 0xA7, 0xE8, 0xA4, 0x11, 0x39, 0x54,
  28267. 0x62, 0xC7, 0xD4, 0x83, 0x8B, 0x91, 0x96, 0x96,
  28268. 0x54, 0x16, 0x59, 0x2B, 0xCF, 0xAC, 0x97, 0x87,
  28269. 0x0C, 0x23, 0x58, 0xA8, 0x59, 0x5B, 0x2F, 0x43,
  28270. 0x55, 0xF0, 0xFA, 0x10, 0x43, 0xA1, 0x1F, 0x62,
  28271. 0x86, 0x2F, 0xD5, 0xD9, 0x7A, 0x9D, 0xE2, 0x1B,
  28272. 0xD3, 0x39, 0x85, 0x5E, 0x17, 0x8C, 0x97, 0xD3,
  28273. 0x77, 0xD1, 0xD1, 0x46, 0xEC, 0x6A, 0x96, 0xE4,
  28274. 0xFB, 0xB7, 0x15, 0xC4, 0xC0, 0x22, 0x84, 0x55,
  28275. 0xBB, 0x64, 0xA3, 0x88, 0xDC, 0xC4, 0xD3, 0x91,
  28276. 0x3A, 0x76, 0x09, 0x57, 0xD1, 0xA6, 0x9F, 0xEA,
  28277. 0xAA, 0x08, 0x92, 0x29, 0xB8, 0xCA, 0xD0, 0xBC,
  28278. 0xC9, 0x38, 0xC7, 0x80, 0x69, 0x8A, 0x5C, 0xE1,
  28279. 0x9B, 0xC5, 0x21, 0x44, 0xDB, 0x46, 0x3B, 0x6D,
  28280. 0x55, 0x4F, 0xC4, 0xEC, 0x5E, 0x18, 0x64, 0xC4,
  28281. 0xC6, 0x88, 0x75, 0x97, 0x50, 0x0F, 0x40, 0x94,
  28282. 0x18, 0xB3, 0xA3, 0x3F, 0x2C, 0x88, 0xB2, 0x6E,
  28283. 0xD9, 0x83, 0x1D, 0xB4, 0x65, 0x08, 0xE8, 0x9F,
  28284. 0x8C, 0x33, 0x5B, 0xE1, 0x94, 0x56, 0x12, 0x05,
  28285. 0x22, 0xA8, 0xB4, 0x43, 0xFE, 0xF2, 0x1A, 0x7B,
  28286. 0xC3, 0x05, 0x9A, 0xFB, 0x4A, 0xAA, 0xD5, 0x0E,
  28287. 0x6C, 0x72, 0x8B, 0xEA, 0x8A, 0xC5, 0xDC, 0x7C,
  28288. 0x6D, 0x06, 0x94, 0x76, 0x03, 0x51, 0x0A, 0x84,
  28289. 0x90, 0xAF, 0xCE, 0x30, 0x4D, 0x57, 0xA7, 0x25,
  28290. 0xC4, 0x3B, 0x5A, 0xA3, 0x71, 0x3B, 0xA0, 0x44,
  28291. 0x98, 0x75, 0xE5, 0x67, 0x55, 0x13, 0x22, 0x60,
  28292. 0x54, 0x63, 0x6C, 0xF8, 0x9E, 0x59, 0x5A, 0xC5,
  28293. 0xAE, 0xE5, 0x66, 0x78, 0x4B, 0x80, 0xFA, 0x39,
  28294. 0x64, 0x0B, 0x74, 0x0F, 0x47, 0x12, 0x22, 0x60,
  28295. 0x25, 0x11, 0x00, 0xFA, 0x8E, 0xE0, 0xF1, 0x65,
  28296. 0x27, 0x11, 0x40, 0x0C, 0x68, 0x16, 0x38, 0x3C,
  28297. 0xC5, 0xE6, 0xDA, 0xB7, 0x9C, 0xDC, 0x3E, 0x41,
  28298. 0x98, 0x37, 0x48, 0x15, 0xB2, 0xBB, 0xA2, 0x0E,
  28299. 0xFC, 0x14, 0x25, 0xCE, 0xD1, 0x9B, 0x60, 0x19,
  28300. 0x77, 0x08, 0x08, 0x00, 0xCF, 0x54, 0x9C, 0xC4,
  28301. 0x80, 0x7D, 0xE6, 0xCA, 0x84, 0xE5, 0xD3, 0xC2,
  28302. 0xCF, 0x8B, 0x11, 0xEB, 0x68, 0x56, 0xE7, 0x1C,
  28303. 0x6B, 0xA5, 0xD2, 0x96, 0x45, 0x46, 0xAA, 0x0C,
  28304. 0x82, 0x53, 0x10, 0xF7, 0x79, 0xED, 0x69, 0x53,
  28305. 0xD9, 0x22, 0x5E, 0x11, 0xF0, 0x57, 0x93, 0x48,
  28306. 0x4C, 0x6B, 0x47, 0xB6, 0xE3, 0xD0, 0x4A, 0x4B,
  28307. 0x27, 0xB7, 0x86, 0x71, 0x46, 0xF6, 0xA0, 0x64,
  28308. 0xB9, 0x8B, 0x8B, 0xDD, 0x2B, 0x69, 0x94, 0xBA,
  28309. 0x20, 0xF8, 0x14, 0x95, 0xB3, 0x74, 0x1C, 0xE1,
  28310. 0xD3, 0x07, 0xE5, 0xEC, 0x0A, 0x7C, 0x20, 0x20,
  28311. 0x2B, 0x81, 0xA1, 0x04, 0x02, 0x83, 0x4D, 0x23,
  28312. 0x83, 0xEC, 0xB2, 0x33, 0x3B, 0x2C, 0x93, 0xD6,
  28313. 0xC4, 0x37, 0xC3, 0x17, 0x58, 0xDF, 0x92, 0x21,
  28314. 0xA3, 0xFA, 0x60, 0x24, 0x81, 0x29, 0x29, 0x62,
  28315. 0x81, 0x17, 0xCB, 0xC4, 0x79, 0x22, 0x76, 0xDA,
  28316. 0x84, 0x0F, 0xAB, 0x37, 0x03, 0x54, 0x65, 0x23,
  28317. 0x0C, 0x7A, 0x79, 0xEB, 0xB1, 0xA8, 0xC2, 0xB4,
  28318. 0x49, 0x0C, 0x2C, 0x1C, 0xB8, 0xE8, 0xB5, 0x73,
  28319. 0x5B, 0xC0, 0x72, 0xFC, 0x4D, 0x0F, 0x02, 0xB5,
  28320. 0x5B, 0x01, 0x3A, 0x53, 0x86, 0x4B, 0x82, 0x5A,
  28321. 0x85, 0xE9, 0x52, 0x03, 0x76, 0xC4, 0x4A, 0x9D,
  28322. 0xBA, 0x2C, 0xE9, 0x06, 0x8F, 0x25, 0x7A, 0x11,
  28323. 0xCF, 0xB6, 0x46, 0x6A, 0xC5, 0xAB, 0xBC, 0x85,
  28324. 0xCE, 0x80, 0x0A, 0x13, 0xB9, 0x64, 0x4D, 0x6E,
  28325. 0x04, 0x70, 0x1D, 0x23, 0xA9, 0x9A, 0x7C, 0x1A,
  28326. 0x5A, 0xEA, 0xBE, 0x2D, 0xB1, 0x34, 0x16, 0xD0,
  28327. 0xC3, 0x5C, 0x6C, 0x04, 0x98, 0x8B, 0x39, 0x91,
  28328. 0x6C, 0x46, 0x67, 0x6C, 0x66, 0x51, 0xF3, 0x5E,
  28329. 0xC2, 0x28, 0x13, 0xCF, 0xE3, 0x84, 0x40, 0x69,
  28330. 0x2F, 0x43, 0x29, 0xC5, 0x85, 0x03, 0x27, 0x30,
  28331. 0xC6, 0x94, 0xF2, 0xB2, 0x41, 0x03, 0xFB, 0x78,
  28332. 0xE4, 0x3A, 0x49, 0x52, 0x70, 0x03, 0xF4, 0xFA,
  28333. 0x5B, 0x73, 0x49, 0x2A, 0x50, 0x71, 0x83, 0x55,
  28334. 0x36, 0x26, 0x78, 0x26, 0x97, 0x37, 0x65, 0x7B,
  28335. 0x7A, 0x24, 0x03, 0xD4, 0x15, 0x09, 0xD6, 0xEC,
  28336. 0x9E, 0x51, 0x65, 0x51, 0x95, 0x56, 0xCB, 0x94,
  28337. 0x92, 0xCD, 0x6D, 0x04, 0x6F, 0x2B, 0x4C, 0x91,
  28338. 0x4A, 0xF6, 0x3C, 0xBF, 0xCC, 0xA0, 0xB4, 0xCC,
  28339. 0x44, 0x6A, 0x54, 0x69, 0x54, 0x59, 0x41, 0x66,
  28340. 0x8A, 0xBD, 0x9A, 0x26, 0x7A, 0xF2, 0x41, 0x28,
  28341. 0x33, 0xD9, 0x67, 0xB8, 0xAB, 0xB4, 0x34, 0xD7,
  28342. 0xA0, 0xAF, 0x5C, 0xAB, 0x11, 0x34, 0xB0, 0x9E,
  28343. 0xDB, 0x7A, 0xA1, 0xC1, 0x60, 0x0A, 0xF1, 0x5E,
  28344. 0xF6, 0x15, 0x27, 0xC8, 0xC1, 0xA1, 0x59, 0x40,
  28345. 0xCA, 0xB1, 0x60, 0x35, 0xF0, 0x5C, 0x71, 0x77,
  28346. 0x15, 0x90, 0x9D, 0x73, 0x2D, 0x1E, 0x9B, 0x4C,
  28347. 0x59, 0x2B, 0xB3, 0x86, 0xA5, 0xC1, 0xF3, 0xB9,
  28348. 0x89, 0x17, 0x58, 0x82, 0x05, 0xD2, 0xA8, 0xB5,
  28349. 0x03, 0x8A, 0x52, 0x4B, 0x99, 0x52, 0xD5, 0x9D,
  28350. 0x85, 0xDB, 0x27, 0x3C, 0xA1, 0xA7, 0x87, 0xF0,
  28351. 0x1F, 0x9B, 0xCB, 0x07, 0xDA, 0xC3, 0x3B, 0xC4,
  28352. 0xF0, 0xB4, 0x33, 0x36, 0x38, 0xCD, 0x3A, 0x60,
  28353. 0x1D, 0x35, 0x63, 0xF3, 0x53, 0xB5, 0xDF, 0xAA,
  28354. 0xA6, 0x2C, 0x68, 0xBF, 0x92, 0xA4, 0x79, 0xD8,
  28355. 0xB9, 0x4F, 0x81, 0x54, 0x31, 0x72, 0xB8, 0x36,
  28356. 0x26, 0xCC, 0x3B, 0x48, 0x72, 0x76, 0xAA, 0x5C,
  28357. 0xCB, 0x4D, 0x10, 0xBB, 0x18, 0xC9, 0x9F, 0x48,
  28358. 0x2A, 0x88, 0x26, 0x1C, 0x21, 0xA7, 0x90, 0x0F,
  28359. 0x60, 0x73, 0xB7, 0x06, 0xAC, 0xC5, 0xD2, 0xD7,
  28360. 0x54, 0x10, 0xB3, 0xA8, 0x84, 0x06, 0x95, 0x87,
  28361. 0x2B, 0xAC, 0xC8, 0xAA, 0x36, 0x60, 0xA4, 0x7D,
  28362. 0x6B, 0x7A, 0x5A, 0xA7, 0x9A, 0x5D, 0x28, 0xB2,
  28363. 0x6B, 0xFF, 0x80, 0x63, 0x14, 0xE6, 0x92, 0x93,
  28364. 0x30, 0x4A, 0xD0, 0x13, 0xB4, 0xE6, 0x75, 0xAE,
  28365. 0x62, 0x0B, 0x52, 0xEF, 0x30, 0x79, 0x00, 0x33,
  28366. 0xCC, 0x42, 0x78, 0xC0, 0x38, 0x50, 0xB4, 0xE6,
  28367. 0x0B, 0x39, 0x63, 0x46, 0x11, 0xDC, 0x3A, 0x5F,
  28368. 0x4C, 0x79, 0x66, 0xA9, 0xF4, 0x92, 0x0B, 0xF7,
  28369. 0x08, 0xBA, 0x8A, 0x01, 0x49, 0x1C, 0x7C, 0xEC,
  28370. 0x97, 0x40, 0xFA, 0x97, 0x6E, 0x8E, 0x76, 0x13,
  28371. 0x00, 0x60, 0x5F, 0x75, 0x20, 0x88, 0x56, 0x56,
  28372. 0x5F, 0x4F, 0x55, 0x60, 0x8F, 0x61, 0x37, 0x92,
  28373. 0x99, 0xA6, 0x4C, 0x96, 0x6B, 0xAD, 0x4A, 0x0E,
  28374. 0x87, 0x92, 0xAD, 0x3D, 0x46, 0x86, 0x41, 0x28,
  28375. 0x37, 0x61, 0xA5, 0x39, 0x99, 0x25, 0x86, 0x63,
  28376. 0x30, 0xB5, 0xBA, 0x37, 0x07, 0xE8, 0x54, 0xA6,
  28377. 0x3F, 0xE7, 0xAA, 0x7F, 0x81, 0x1A, 0xA4, 0x57,
  28378. 0x54, 0x08, 0xC8, 0xC2, 0x06, 0xBB, 0x39, 0x53,
  28379. 0xA7, 0x66, 0x29, 0x9A, 0x5C, 0x26, 0xC6, 0x7A,
  28380. 0x4E, 0x58, 0x21, 0x05, 0x79, 0xB3, 0x10, 0xF7,
  28381. 0x99, 0xB0, 0x97, 0x9B, 0xB9, 0x45, 0x8A, 0xEC,
  28382. 0x21, 0x50, 0xAD, 0x27, 0x3F, 0xEB, 0xCB, 0x3E,
  28383. 0xF3, 0xB6, 0x0B, 0x38, 0x56, 0xA7, 0x16, 0xB9,
  28384. 0x78, 0x3D, 0xF8, 0x08, 0xC6, 0x9C, 0x06, 0x12,
  28385. 0xA0, 0x9E, 0xA5, 0x93, 0x16, 0x64, 0x65, 0x87,
  28386. 0x69, 0x2D, 0x3C, 0x8E, 0x8C, 0x73, 0x9F, 0xB3,
  28387. 0x78, 0x27, 0x74, 0x15, 0x61, 0xD5, 0xE7, 0x66,
  28388. 0x67, 0x51, 0x82, 0xFF, 0x83, 0xC9, 0x1C, 0xF0,
  28389. 0x33, 0x65, 0x88, 0x23, 0xD8, 0xFA, 0x91, 0xE1
  28390. };
  28391. const byte kyber768_sk[] = {
  28392. 0x1B, 0xD1, 0x97, 0x95, 0x10, 0xBC, 0xE5, 0x03,
  28393. 0x1C, 0x0F, 0x99, 0x1D, 0xE0, 0x82, 0xC3, 0x89,
  28394. 0xA1, 0xBD, 0x15, 0x57, 0x19, 0x1D, 0x81, 0x03,
  28395. 0xBD, 0xCC, 0x04, 0xC1, 0x98, 0xAE, 0x3E, 0x95,
  28396. 0x95, 0xFD, 0xB6, 0x57, 0xB5, 0xC6, 0x39, 0x97,
  28397. 0x95, 0x79, 0xAE, 0x13, 0x5B, 0x11, 0xE2, 0x4D,
  28398. 0x36, 0xE5, 0x49, 0x44, 0x67, 0x83, 0x2E, 0x6A,
  28399. 0x8A, 0x46, 0xAA, 0x8F, 0x9F, 0xF9, 0x87, 0x4A,
  28400. 0x16, 0x00, 0x27, 0x84, 0x07, 0x71, 0x56, 0x2D,
  28401. 0x74, 0x61, 0xB1, 0x6C, 0x61, 0x87, 0xF3, 0xC2,
  28402. 0xC1, 0x9C, 0x9C, 0x8F, 0xE6, 0x41, 0x49, 0x7D,
  28403. 0x8A, 0x15, 0x5C, 0x51, 0x15, 0x7D, 0x91, 0x35,
  28404. 0xD9, 0xB4, 0x5D, 0x3C, 0x7C, 0x4C, 0xB1, 0x82,
  28405. 0x31, 0x8C, 0x11, 0xAD, 0x08, 0xB3, 0x9F, 0xAC,
  28406. 0x04, 0x15, 0xE6, 0xE2, 0x6D, 0xB3, 0x43, 0x91,
  28407. 0xDE, 0xC2, 0x47, 0x1B, 0x30, 0x3C, 0x05, 0x05,
  28408. 0xB6, 0xFE, 0x45, 0x6E, 0x81, 0x50, 0x21, 0xE8,
  28409. 0x2B, 0x5F, 0x2D, 0x16, 0x6F, 0xDF, 0xB5, 0xB9,
  28410. 0x23, 0xC8, 0x3A, 0x61, 0x67, 0x30, 0x28, 0xF0,
  28411. 0xBA, 0xE0, 0x28, 0x08, 0x9F, 0x47, 0x61, 0x67,
  28412. 0xE5, 0x75, 0x6C, 0xAA, 0x3E, 0xE0, 0xF1, 0x89,
  28413. 0xC6, 0x47, 0x81, 0xC7, 0x49, 0x27, 0xBC, 0xDB,
  28414. 0x67, 0xE7, 0x75, 0xCC, 0xA8, 0x5B, 0x44, 0xB8,
  28415. 0x8B, 0x10, 0x61, 0xD8, 0x96, 0x3F, 0xE4, 0x0B,
  28416. 0x1F, 0xB4, 0x34, 0x68, 0x20, 0x09, 0x0E, 0x5C,
  28417. 0x0B, 0x7C, 0x49, 0x08, 0x89, 0x33, 0xAD, 0xE3,
  28418. 0x82, 0x96, 0x88, 0x6C, 0x43, 0x45, 0x65, 0x7C,
  28419. 0x3F, 0x07, 0xCF, 0x1A, 0xE9, 0x76, 0xE6, 0xBA,
  28420. 0x77, 0x7E, 0xF0, 0x1F, 0xB9, 0xC3, 0xB0, 0xDC,
  28421. 0xF0, 0x58, 0x3A, 0xE5, 0x44, 0xCD, 0x80, 0x3A,
  28422. 0xC1, 0x39, 0xCA, 0x61, 0x9B, 0x8D, 0xCA, 0x13,
  28423. 0x1C, 0xD2, 0x73, 0xCE, 0xCB, 0x65, 0xBC, 0xEF,
  28424. 0x00, 0x29, 0x95, 0x12, 0xB0, 0x21, 0xBA, 0x1B,
  28425. 0x2F, 0xF2, 0x08, 0xEE, 0xBB, 0x08, 0xA6, 0x28,
  28426. 0x7E, 0xC4, 0x4C, 0x21, 0x7D, 0x94, 0x76, 0x40,
  28427. 0x42, 0x5C, 0x0E, 0x97, 0x13, 0x44, 0x5B, 0xC5,
  28428. 0x2B, 0xC3, 0xCD, 0x66, 0x01, 0x2C, 0x8C, 0xB7,
  28429. 0x10, 0xA2, 0xE8, 0xAD, 0x5F, 0xC5, 0x1C, 0x5B,
  28430. 0x71, 0xCE, 0x69, 0xE9, 0x6A, 0x22, 0x06, 0x6F,
  28431. 0x74, 0x4B, 0xBA, 0xB6, 0x47, 0x2F, 0xEF, 0x3A,
  28432. 0x5B, 0xCF, 0xD6, 0x1D, 0xF1, 0xF8, 0x6A, 0x95,
  28433. 0x64, 0x80, 0x03, 0x1A, 0x99, 0x0E, 0xE0, 0x7D,
  28434. 0xE5, 0x04, 0x5E, 0xBC, 0x49, 0x8F, 0x2D, 0x3A,
  28435. 0xBD, 0xE3, 0x28, 0xC4, 0x2A, 0xCB, 0x21, 0xAD,
  28436. 0x62, 0x5D, 0x7B, 0xCB, 0x85, 0x5A, 0x8A, 0x0A,
  28437. 0xCB, 0xA2, 0x85, 0x16, 0xD3, 0x5A, 0xAF, 0x92,
  28438. 0xBE, 0xB3, 0x9C, 0x46, 0xD8, 0xF4, 0xA0, 0x1F,
  28439. 0x73, 0x20, 0x40, 0xF2, 0x7C, 0x40, 0x9B, 0xA4,
  28440. 0x9A, 0x80, 0xCF, 0x95, 0x55, 0x7C, 0x73, 0x7C,
  28441. 0x4A, 0x45, 0x11, 0x2C, 0x52, 0x2C, 0x78, 0x04,
  28442. 0xC7, 0x7A, 0x3D, 0x44, 0x53, 0xBA, 0x82, 0xC1,
  28443. 0x49, 0x08, 0xBC, 0xC0, 0x47, 0x13, 0x7E, 0x5A,
  28444. 0x7B, 0x23, 0x92, 0xC8, 0x31, 0x25, 0x4C, 0x1A,
  28445. 0x59, 0x54, 0xC8, 0x8C, 0xCF, 0xA5, 0xD8, 0x8B,
  28446. 0x3B, 0x93, 0x31, 0xBE, 0xF4, 0x62, 0x55, 0x09,
  28447. 0x14, 0xD1, 0x84, 0x17, 0x59, 0x40, 0xB4, 0xE9,
  28448. 0xB7, 0xAD, 0x3D, 0x44, 0x2E, 0xA9, 0x68, 0xB6,
  28449. 0xA4, 0x0A, 0x19, 0x72, 0x71, 0x5B, 0xCB, 0x72,
  28450. 0x82, 0x14, 0xA1, 0xBE, 0x8E, 0x04, 0x1D, 0xF5,
  28451. 0x72, 0xCC, 0x2D, 0x01, 0x6F, 0xFD, 0x6B, 0x0A,
  28452. 0xFA, 0xC8, 0x02, 0xBA, 0x97, 0x5A, 0x4A, 0x31,
  28453. 0xA4, 0x18, 0x40, 0x2E, 0x6B, 0x01, 0x41, 0xFA,
  28454. 0xD6, 0x48, 0x49, 0xE1, 0x6C, 0x80, 0x37, 0x23,
  28455. 0xA7, 0x74, 0x2B, 0x08, 0x2A, 0x7A, 0x56, 0x26,
  28456. 0x4E, 0x02, 0x60, 0xA2, 0x4A, 0x66, 0x42, 0x3D,
  28457. 0x52, 0x64, 0x82, 0x07, 0x04, 0x06, 0x5C, 0xAC,
  28458. 0x41, 0x10, 0x72, 0x48, 0x64, 0x37, 0xCA, 0xA5,
  28459. 0x47, 0x2D, 0x73, 0x3C, 0x64, 0x14, 0x5F, 0x10,
  28460. 0x0B, 0x0B, 0x16, 0xC0, 0xAB, 0x0D, 0xB8, 0x7C,
  28461. 0xC5, 0x64, 0x26, 0xC1, 0x1A, 0x69, 0x45, 0x25,
  28462. 0x95, 0x8D, 0xB8, 0x31, 0x59, 0x31, 0xC5, 0x09,
  28463. 0xD6, 0x3C, 0x48, 0x10, 0xBB, 0xA7, 0x68, 0xCA,
  28464. 0xCB, 0xF9, 0xCD, 0x85, 0x7B, 0x0D, 0x66, 0xD6,
  28465. 0x5A, 0xF7, 0xEB, 0xAC, 0x18, 0xB7, 0x9C, 0xBE,
  28466. 0xBC, 0x0A, 0x49, 0xB5, 0xC0, 0x5A, 0xA8, 0x7F,
  28467. 0x8D, 0xE8, 0x25, 0xA6, 0xA1, 0x13, 0x5F, 0xE8,
  28468. 0x64, 0xCF, 0x6C, 0x99, 0x8D, 0xDC, 0x37, 0xFC,
  28469. 0x01, 0x56, 0xBB, 0x52, 0xA7, 0x8E, 0xC2, 0x99,
  28470. 0xCA, 0x53, 0x1C, 0xF8, 0x75, 0xBE, 0xFA, 0x48,
  28471. 0x75, 0x72, 0x60, 0x28, 0xED, 0x37, 0x91, 0xBA,
  28472. 0x31, 0xC0, 0x0E, 0xC7, 0x40, 0x20, 0xCA, 0xA1,
  28473. 0x7F, 0x91, 0x19, 0x63, 0x96, 0xCF, 0xE5, 0x88,
  28474. 0x6E, 0x51, 0x1A, 0x52, 0xD0, 0xCA, 0x8D, 0xEE,
  28475. 0x25, 0xBF, 0x4E, 0x77, 0xCB, 0x92, 0xE6, 0x36,
  28476. 0x40, 0x64, 0x6D, 0xE2, 0x58, 0x6D, 0xCD, 0x00,
  28477. 0x14, 0x16, 0xCC, 0xC1, 0xEC, 0x30, 0x7C, 0xA2,
  28478. 0x75, 0x6D, 0xBE, 0xA8, 0x26, 0x36, 0x14, 0x51,
  28479. 0x3C, 0xA0, 0x5E, 0x70, 0xC7, 0x54, 0xF5, 0x6A,
  28480. 0x2C, 0x3F, 0xF7, 0x42, 0x94, 0x8C, 0x34, 0xAF,
  28481. 0x49, 0x34, 0x2F, 0x55, 0x03, 0x4C, 0x1C, 0x70,
  28482. 0xE0, 0x79, 0x6D, 0x26, 0xB2, 0x30, 0x35, 0x6B,
  28483. 0x1B, 0x74, 0xA1, 0x6C, 0x0B, 0x28, 0x37, 0x4B,
  28484. 0x79, 0xC1, 0x6B, 0x04, 0x96, 0xCA, 0x20, 0x12,
  28485. 0x3E, 0x6C, 0x67, 0x62, 0xD9, 0x01, 0xE2, 0x76,
  28486. 0x08, 0x5D, 0x81, 0x2A, 0xE9, 0xC3, 0x45, 0xAD,
  28487. 0xF5, 0x09, 0xDD, 0xC7, 0x87, 0x61, 0x62, 0xCE,
  28488. 0x34, 0xA6, 0x00, 0x3F, 0xB4, 0x5C, 0xD8, 0x66,
  28489. 0x0F, 0xDD, 0x46, 0x71, 0x6E, 0x82, 0x55, 0x06,
  28490. 0x62, 0x37, 0x5D, 0xB5, 0x5E, 0x63, 0xA2, 0x0F,
  28491. 0x01, 0x2A, 0x71, 0xE3, 0xD9, 0x72, 0x25, 0xC5,
  28492. 0x8F, 0x52, 0x34, 0xC6, 0xB5, 0x3A, 0x8A, 0xA0,
  28493. 0x33, 0x9C, 0xEF, 0xD4, 0x84, 0xE4, 0x7A, 0x3D,
  28494. 0xE6, 0x49, 0x88, 0x05, 0xC2, 0x82, 0xE0, 0x76,
  28495. 0x8E, 0x35, 0xC7, 0x5C, 0x20, 0x07, 0x76, 0x5C,
  28496. 0xA1, 0x7F, 0x01, 0xD2, 0x85, 0xA7, 0xC3, 0x04,
  28497. 0x8B, 0xC7, 0x2B, 0xA3, 0xAA, 0x57, 0x7C, 0x1C,
  28498. 0x43, 0x2A, 0x3C, 0x76, 0x9D, 0x21, 0xB8, 0x6C,
  28499. 0x01, 0x8F, 0x81, 0x7A, 0x26, 0x67, 0x35, 0x1C,
  28500. 0x2B, 0xC8, 0x5F, 0xA5, 0x53, 0x6F, 0xA7, 0xA6,
  28501. 0xCD, 0xEF, 0xCC, 0x0D, 0xD6, 0xF9, 0x1B, 0xDD,
  28502. 0xE8, 0x9D, 0x4E, 0x51, 0x6C, 0x8B, 0x75, 0x40,
  28503. 0xA7, 0xDC, 0x3F, 0xC4, 0x89, 0x8A, 0xB3, 0x30,
  28504. 0x08, 0xBC, 0xC1, 0x9F, 0x9E, 0x46, 0x3D, 0xE9,
  28505. 0x36, 0x26, 0x99, 0x4C, 0xC9, 0x27, 0xA3, 0x4A,
  28506. 0x9D, 0x70, 0x40, 0xC1, 0x31, 0xC8, 0x1F, 0x07,
  28507. 0x83, 0xDD, 0x68, 0x84, 0x29, 0x43, 0xC5, 0x5D,
  28508. 0xC5, 0x77, 0xD3, 0x67, 0xA4, 0x01, 0x83, 0x94,
  28509. 0xBB, 0x11, 0x60, 0x72, 0xAC, 0x65, 0xF2, 0xEB,
  28510. 0x9B, 0x4F, 0xF5, 0x8F, 0x41, 0x15, 0x40, 0x1B,
  28511. 0x11, 0x7D, 0x91, 0x3B, 0x75, 0x75, 0x57, 0x57,
  28512. 0x19, 0x39, 0x0C, 0x9E, 0x80, 0x06, 0x97, 0xA8,
  28513. 0x28, 0xB1, 0x25, 0x83, 0xF9, 0x4A, 0x2F, 0x8F,
  28514. 0x72, 0x0A, 0x6E, 0x87, 0x00, 0xFD, 0x40, 0x7D,
  28515. 0x73, 0xA4, 0xC7, 0x97, 0x55, 0x2B, 0x98, 0x75,
  28516. 0x7A, 0x5F, 0x57, 0x82, 0xA9, 0x50, 0x95, 0xDF,
  28517. 0xA5, 0xAC, 0xB7, 0xD9, 0x3A, 0x80, 0x5C, 0x30,
  28518. 0x18, 0x30, 0x4E, 0x5D, 0x22, 0xB7, 0x6F, 0xA5,
  28519. 0xAA, 0x51, 0x96, 0xB7, 0xEB, 0xDB, 0x7A, 0xA0,
  28520. 0xA5, 0x2D, 0x06, 0xAC, 0x4B, 0x9E, 0x4C, 0x00,
  28521. 0xC0, 0xF8, 0x5C, 0xEC, 0xB0, 0xBD, 0x95, 0x56,
  28522. 0x2D, 0x56, 0x23, 0x30, 0x78, 0x51, 0x43, 0xC7,
  28523. 0xF1, 0x29, 0xFA, 0xF9, 0x19, 0x48, 0xB0, 0xC8,
  28524. 0x8C, 0x60, 0x82, 0xE0, 0xF3, 0x03, 0x54, 0x74,
  28525. 0x12, 0x6E, 0x23, 0x6B, 0x80, 0xD2, 0x22, 0xD2,
  28526. 0x81, 0x4F, 0x6A, 0xF2, 0x4B, 0xD5, 0xD1, 0x19,
  28527. 0x86, 0x33, 0x1F, 0xAF, 0xC2, 0x1E, 0xC8, 0x4C,
  28528. 0x93, 0xA8, 0x81, 0x24, 0x72, 0x51, 0x10, 0x5A,
  28529. 0x6A, 0xB4, 0x35, 0xD7, 0x6E, 0xFD, 0x88, 0x9D,
  28530. 0x27, 0xC0, 0x0C, 0x2A, 0x14, 0xBD, 0x04, 0xA6,
  28531. 0xBD, 0x0B, 0x20, 0x07, 0xB5, 0x51, 0x5A, 0x75,
  28532. 0xF1, 0x14, 0xD1, 0x92, 0x10, 0xBC, 0x70, 0x50,
  28533. 0x85, 0x33, 0x53, 0x2F, 0x1B, 0x0A, 0x30, 0x0C,
  28534. 0x8B, 0x12, 0xF0, 0x0A, 0xC4, 0xFC, 0x46, 0x0F,
  28535. 0xA8, 0x4A, 0x2D, 0x15, 0x21, 0xBD, 0x0B, 0x42,
  28536. 0x86, 0x1A, 0xCF, 0x24, 0x57, 0x9D, 0x8E, 0xE8,
  28537. 0x94, 0x63, 0xB4, 0x78, 0xE2, 0xE9, 0x53, 0xC7,
  28538. 0xD5, 0xCD, 0xA4, 0xD3, 0x07, 0xD1, 0x68, 0xCF,
  28539. 0xC6, 0x79, 0xB1, 0xA3, 0x79, 0x1F, 0x05, 0x83,
  28540. 0x82, 0x86, 0x0B, 0x1C, 0x77, 0x52, 0x87, 0x89,
  28541. 0xC6, 0x96, 0x4B, 0xCC, 0x40, 0x8A, 0x21, 0xCA,
  28542. 0x4E, 0x42, 0x8F, 0xE7, 0x33, 0x9F, 0x7C, 0x08,
  28543. 0x76, 0xA8, 0x58, 0xAB, 0xA9, 0x44, 0xB7, 0x3F,
  28544. 0x9C, 0xC9, 0xB8, 0xAC, 0xC4, 0xB7, 0x9A, 0x9F,
  28545. 0x72, 0xF7, 0xA0, 0x47, 0x72, 0x57, 0xB9, 0xF3,
  28546. 0x26, 0xE4, 0x4C, 0x2C, 0x5F, 0x8B, 0x8A, 0x17,
  28547. 0x58, 0x81, 0x42, 0xB0, 0x91, 0xA3, 0x12, 0x31,
  28548. 0xD0, 0x98, 0xB0, 0x57, 0x30, 0x45, 0x62, 0x90,
  28549. 0x6E, 0x96, 0x05, 0x47, 0x3D, 0x28, 0xB7, 0xB4,
  28550. 0x33, 0x2A, 0xB4, 0x49, 0x82, 0x48, 0x7B, 0x04,
  28551. 0x9E, 0x41, 0x93, 0x74, 0xA4, 0x8E, 0xAC, 0x6A,
  28552. 0xB8, 0xCB, 0x42, 0x6D, 0x13, 0xA4, 0x86, 0x28,
  28553. 0x28, 0x43, 0x02, 0xFB, 0x72, 0x71, 0xD1, 0x54,
  28554. 0xCA, 0x09, 0x06, 0xD8, 0xE2, 0x0F, 0x1D, 0x97,
  28555. 0xBE, 0x9C, 0x82, 0x2E, 0x78, 0x83, 0x79, 0x32,
  28556. 0xEB, 0x28, 0xEE, 0xB0, 0x83, 0x71, 0xBB, 0xC6,
  28557. 0x3A, 0x7A, 0x96, 0x6C, 0xAC, 0x7A, 0x05, 0xB5,
  28558. 0x50, 0x3C, 0x55, 0x1A, 0xEF, 0x80, 0x15, 0x14,
  28559. 0xE1, 0x6A, 0xF6, 0x81, 0x02, 0x64, 0x6C, 0x66,
  28560. 0x9D, 0xD8, 0xA7, 0xE8, 0xA4, 0x11, 0x39, 0x54,
  28561. 0x62, 0xC7, 0xD4, 0x83, 0x8B, 0x91, 0x96, 0x96,
  28562. 0x54, 0x16, 0x59, 0x2B, 0xCF, 0xAC, 0x97, 0x87,
  28563. 0x0C, 0x23, 0x58, 0xA8, 0x59, 0x5B, 0x2F, 0x43,
  28564. 0x55, 0xF0, 0xFA, 0x10, 0x43, 0xA1, 0x1F, 0x62,
  28565. 0x86, 0x2F, 0xD5, 0xD9, 0x7A, 0x9D, 0xE2, 0x1B,
  28566. 0xD3, 0x39, 0x85, 0x5E, 0x17, 0x8C, 0x97, 0xD3,
  28567. 0x77, 0xD1, 0xD1, 0x46, 0xEC, 0x6A, 0x96, 0xE4,
  28568. 0xFB, 0xB7, 0x15, 0xC4, 0xC0, 0x22, 0x84, 0x55,
  28569. 0xBB, 0x64, 0xA3, 0x88, 0xDC, 0xC4, 0xD3, 0x91,
  28570. 0x3A, 0x76, 0x09, 0x57, 0xD1, 0xA6, 0x9F, 0xEA,
  28571. 0xAA, 0x08, 0x92, 0x29, 0xB8, 0xCA, 0xD0, 0xBC,
  28572. 0xC9, 0x38, 0xC7, 0x80, 0x69, 0x8A, 0x5C, 0xE1,
  28573. 0x9B, 0xC5, 0x21, 0x44, 0xDB, 0x46, 0x3B, 0x6D,
  28574. 0x55, 0x4F, 0xC4, 0xEC, 0x5E, 0x18, 0x64, 0xC4,
  28575. 0xC6, 0x88, 0x75, 0x97, 0x50, 0x0F, 0x40, 0x94,
  28576. 0x18, 0xB3, 0xA3, 0x3F, 0x2C, 0x88, 0xB2, 0x6E,
  28577. 0xD9, 0x83, 0x1D, 0xB4, 0x65, 0x08, 0xE8, 0x9F,
  28578. 0x8C, 0x33, 0x5B, 0xE1, 0x94, 0x56, 0x12, 0x05,
  28579. 0x22, 0xA8, 0xB4, 0x43, 0xFE, 0xF2, 0x1A, 0x7B,
  28580. 0xC3, 0x05, 0x9A, 0xFB, 0x4A, 0xAA, 0xD5, 0x0E,
  28581. 0x6C, 0x72, 0x8B, 0xEA, 0x8A, 0xC5, 0xDC, 0x7C,
  28582. 0x6D, 0x06, 0x94, 0x76, 0x03, 0x51, 0x0A, 0x84,
  28583. 0x90, 0xAF, 0xCE, 0x30, 0x4D, 0x57, 0xA7, 0x25,
  28584. 0xC4, 0x3B, 0x5A, 0xA3, 0x71, 0x3B, 0xA0, 0x44,
  28585. 0x98, 0x75, 0xE5, 0x67, 0x55, 0x13, 0x22, 0x60,
  28586. 0x54, 0x63, 0x6C, 0xF8, 0x9E, 0x59, 0x5A, 0xC5,
  28587. 0xAE, 0xE5, 0x66, 0x78, 0x4B, 0x80, 0xFA, 0x39,
  28588. 0x64, 0x0B, 0x74, 0x0F, 0x47, 0x12, 0x22, 0x60,
  28589. 0x25, 0x11, 0x00, 0xFA, 0x8E, 0xE0, 0xF1, 0x65,
  28590. 0x27, 0x11, 0x40, 0x0C, 0x68, 0x16, 0x38, 0x3C,
  28591. 0xC5, 0xE6, 0xDA, 0xB7, 0x9C, 0xDC, 0x3E, 0x41,
  28592. 0x98, 0x37, 0x48, 0x15, 0xB2, 0xBB, 0xA2, 0x0E,
  28593. 0xFC, 0x14, 0x25, 0xCE, 0xD1, 0x9B, 0x60, 0x19,
  28594. 0x77, 0x08, 0x08, 0x00, 0xCF, 0x54, 0x9C, 0xC4,
  28595. 0x80, 0x7D, 0xE6, 0xCA, 0x84, 0xE5, 0xD3, 0xC2,
  28596. 0xCF, 0x8B, 0x11, 0xEB, 0x68, 0x56, 0xE7, 0x1C,
  28597. 0x6B, 0xA5, 0xD2, 0x96, 0x45, 0x46, 0xAA, 0x0C,
  28598. 0x82, 0x53, 0x10, 0xF7, 0x79, 0xED, 0x69, 0x53,
  28599. 0xD9, 0x22, 0x5E, 0x11, 0xF0, 0x57, 0x93, 0x48,
  28600. 0x4C, 0x6B, 0x47, 0xB6, 0xE3, 0xD0, 0x4A, 0x4B,
  28601. 0x27, 0xB7, 0x86, 0x71, 0x46, 0xF6, 0xA0, 0x64,
  28602. 0xB9, 0x8B, 0x8B, 0xDD, 0x2B, 0x69, 0x94, 0xBA,
  28603. 0x20, 0xF8, 0x14, 0x95, 0xB3, 0x74, 0x1C, 0xE1,
  28604. 0xD3, 0x07, 0xE5, 0xEC, 0x0A, 0x7C, 0x20, 0x20,
  28605. 0x2B, 0x81, 0xA1, 0x04, 0x02, 0x83, 0x4D, 0x23,
  28606. 0x83, 0xEC, 0xB2, 0x33, 0x3B, 0x2C, 0x93, 0xD6,
  28607. 0xC4, 0x37, 0xC3, 0x17, 0x58, 0xDF, 0x92, 0x21,
  28608. 0xA3, 0xFA, 0x60, 0x24, 0x81, 0x29, 0x29, 0x62,
  28609. 0x81, 0x17, 0xCB, 0xC4, 0x79, 0x22, 0x76, 0xDA,
  28610. 0x84, 0x0F, 0xAB, 0x37, 0x03, 0x54, 0x65, 0x23,
  28611. 0x0C, 0x7A, 0x79, 0xEB, 0xB1, 0xA8, 0xC2, 0xB4,
  28612. 0x49, 0x0C, 0x2C, 0x1C, 0xB8, 0xE8, 0xB5, 0x73,
  28613. 0x5B, 0xC0, 0x72, 0xFC, 0x4D, 0x0F, 0x02, 0xB5,
  28614. 0x5B, 0x01, 0x3A, 0x53, 0x86, 0x4B, 0x82, 0x5A,
  28615. 0x85, 0xE9, 0x52, 0x03, 0x76, 0xC4, 0x4A, 0x9D,
  28616. 0xBA, 0x2C, 0xE9, 0x06, 0x8F, 0x25, 0x7A, 0x11,
  28617. 0xCF, 0xB6, 0x46, 0x6A, 0xC5, 0xAB, 0xBC, 0x85,
  28618. 0xCE, 0x80, 0x0A, 0x13, 0xB9, 0x64, 0x4D, 0x6E,
  28619. 0x04, 0x70, 0x1D, 0x23, 0xA9, 0x9A, 0x7C, 0x1A,
  28620. 0x5A, 0xEA, 0xBE, 0x2D, 0xB1, 0x34, 0x16, 0xD0,
  28621. 0xC3, 0x5C, 0x6C, 0x04, 0x98, 0x8B, 0x39, 0x91,
  28622. 0x6C, 0x46, 0x67, 0x6C, 0x66, 0x51, 0xF3, 0x5E,
  28623. 0xC2, 0x28, 0x13, 0xCF, 0xE3, 0x84, 0x40, 0x69,
  28624. 0x2F, 0x43, 0x29, 0xC5, 0x85, 0x03, 0x27, 0x30,
  28625. 0xC6, 0x94, 0xF2, 0xB2, 0x41, 0x03, 0xFB, 0x78,
  28626. 0xE4, 0x3A, 0x49, 0x52, 0x70, 0x03, 0xF4, 0xFA,
  28627. 0x5B, 0x73, 0x49, 0x2A, 0x50, 0x71, 0x83, 0x55,
  28628. 0x36, 0x26, 0x78, 0x26, 0x97, 0x37, 0x65, 0x7B,
  28629. 0x7A, 0x24, 0x03, 0xD4, 0x15, 0x09, 0xD6, 0xEC,
  28630. 0x9E, 0x51, 0x65, 0x51, 0x95, 0x56, 0xCB, 0x94,
  28631. 0x92, 0xCD, 0x6D, 0x04, 0x6F, 0x2B, 0x4C, 0x91,
  28632. 0x4A, 0xF6, 0x3C, 0xBF, 0xCC, 0xA0, 0xB4, 0xCC,
  28633. 0x44, 0x6A, 0x54, 0x69, 0x54, 0x59, 0x41, 0x66,
  28634. 0x8A, 0xBD, 0x9A, 0x26, 0x7A, 0xF2, 0x41, 0x28,
  28635. 0x33, 0xD9, 0x67, 0xB8, 0xAB, 0xB4, 0x34, 0xD7,
  28636. 0xA0, 0xAF, 0x5C, 0xAB, 0x11, 0x34, 0xB0, 0x9E,
  28637. 0xDB, 0x7A, 0xA1, 0xC1, 0x60, 0x0A, 0xF1, 0x5E,
  28638. 0xF6, 0x15, 0x27, 0xC8, 0xC1, 0xA1, 0x59, 0x40,
  28639. 0xCA, 0xB1, 0x60, 0x35, 0xF0, 0x5C, 0x71, 0x77,
  28640. 0x15, 0x90, 0x9D, 0x73, 0x2D, 0x1E, 0x9B, 0x4C,
  28641. 0x59, 0x2B, 0xB3, 0x86, 0xA5, 0xC1, 0xF3, 0xB9,
  28642. 0x89, 0x17, 0x58, 0x82, 0x05, 0xD2, 0xA8, 0xB5,
  28643. 0x03, 0x8A, 0x52, 0x4B, 0x99, 0x52, 0xD5, 0x9D,
  28644. 0x85, 0xDB, 0x27, 0x3C, 0xA1, 0xA7, 0x87, 0xF0,
  28645. 0x1F, 0x9B, 0xCB, 0x07, 0xDA, 0xC3, 0x3B, 0xC4,
  28646. 0xF0, 0xB4, 0x33, 0x36, 0x38, 0xCD, 0x3A, 0x60,
  28647. 0x1D, 0x35, 0x63, 0xF3, 0x53, 0xB5, 0xDF, 0xAA,
  28648. 0xA6, 0x2C, 0x68, 0xBF, 0x92, 0xA4, 0x79, 0xD8,
  28649. 0xB9, 0x4F, 0x81, 0x54, 0x31, 0x72, 0xB8, 0x36,
  28650. 0x26, 0xCC, 0x3B, 0x48, 0x72, 0x76, 0xAA, 0x5C,
  28651. 0xCB, 0x4D, 0x10, 0xBB, 0x18, 0xC9, 0x9F, 0x48,
  28652. 0x2A, 0x88, 0x26, 0x1C, 0x21, 0xA7, 0x90, 0x0F,
  28653. 0x60, 0x73, 0xB7, 0x06, 0xAC, 0xC5, 0xD2, 0xD7,
  28654. 0x54, 0x10, 0xB3, 0xA8, 0x84, 0x06, 0x95, 0x87,
  28655. 0x2B, 0xAC, 0xC8, 0xAA, 0x36, 0x60, 0xA4, 0x7D,
  28656. 0x6B, 0x7A, 0x5A, 0xA7, 0x9A, 0x5D, 0x28, 0xB2,
  28657. 0x6B, 0xFF, 0x80, 0x63, 0x14, 0xE6, 0x92, 0x93,
  28658. 0x30, 0x4A, 0xD0, 0x13, 0xB4, 0xE6, 0x75, 0xAE,
  28659. 0x62, 0x0B, 0x52, 0xEF, 0x30, 0x79, 0x00, 0x33,
  28660. 0xCC, 0x42, 0x78, 0xC0, 0x38, 0x50, 0xB4, 0xE6,
  28661. 0x0B, 0x39, 0x63, 0x46, 0x11, 0xDC, 0x3A, 0x5F,
  28662. 0x4C, 0x79, 0x66, 0xA9, 0xF4, 0x92, 0x0B, 0xF7,
  28663. 0x08, 0xBA, 0x8A, 0x01, 0x49, 0x1C, 0x7C, 0xEC,
  28664. 0x97, 0x40, 0xFA, 0x97, 0x6E, 0x8E, 0x76, 0x13,
  28665. 0x00, 0x60, 0x5F, 0x75, 0x20, 0x88, 0x56, 0x56,
  28666. 0x5F, 0x4F, 0x55, 0x60, 0x8F, 0x61, 0x37, 0x92,
  28667. 0x99, 0xA6, 0x4C, 0x96, 0x6B, 0xAD, 0x4A, 0x0E,
  28668. 0x87, 0x92, 0xAD, 0x3D, 0x46, 0x86, 0x41, 0x28,
  28669. 0x37, 0x61, 0xA5, 0x39, 0x99, 0x25, 0x86, 0x63,
  28670. 0x30, 0xB5, 0xBA, 0x37, 0x07, 0xE8, 0x54, 0xA6,
  28671. 0x3F, 0xE7, 0xAA, 0x7F, 0x81, 0x1A, 0xA4, 0x57,
  28672. 0x54, 0x08, 0xC8, 0xC2, 0x06, 0xBB, 0x39, 0x53,
  28673. 0xA7, 0x66, 0x29, 0x9A, 0x5C, 0x26, 0xC6, 0x7A,
  28674. 0x4E, 0x58, 0x21, 0x05, 0x79, 0xB3, 0x10, 0xF7,
  28675. 0x99, 0xB0, 0x97, 0x9B, 0xB9, 0x45, 0x8A, 0xEC,
  28676. 0x21, 0x50, 0xAD, 0x27, 0x3F, 0xEB, 0xCB, 0x3E,
  28677. 0xF3, 0xB6, 0x0B, 0x38, 0x56, 0xA7, 0x16, 0xB9,
  28678. 0x78, 0x3D, 0xF8, 0x08, 0xC6, 0x9C, 0x06, 0x12,
  28679. 0xA0, 0x9E, 0xA5, 0x93, 0x16, 0x64, 0x65, 0x87,
  28680. 0x69, 0x2D, 0x3C, 0x8E, 0x8C, 0x73, 0x9F, 0xB3,
  28681. 0x78, 0x27, 0x74, 0x15, 0x61, 0xD5, 0xE7, 0x66,
  28682. 0x67, 0x51, 0x82, 0xFF, 0x83, 0xC9, 0x1C, 0xF0,
  28683. 0x33, 0x65, 0x88, 0x23, 0xD8, 0xFA, 0x91, 0xE1,
  28684. 0x05, 0xFC, 0x14, 0xDA, 0x24, 0xF1, 0xC4, 0x1E,
  28685. 0xB3, 0xCC, 0xE3, 0xC7, 0x47, 0xEE, 0x30, 0x0C,
  28686. 0x31, 0x1F, 0x27, 0x85, 0x47, 0x7A, 0x01, 0x33,
  28687. 0xF7, 0x13, 0xB0, 0x28, 0x04, 0x67, 0x4E, 0x7A,
  28688. 0x86, 0x26, 0xED, 0x79, 0xD4, 0x51, 0x14, 0x08,
  28689. 0x00, 0xE0, 0x3B, 0x59, 0xB9, 0x56, 0xF8, 0x21,
  28690. 0x0E, 0x55, 0x60, 0x67, 0x40, 0x7D, 0x13, 0xDC,
  28691. 0x90, 0xFA, 0x9E, 0x8B, 0x87, 0x2B, 0xFB, 0x8F
  28692. };
  28693. const byte kyber768_ct[] = {
  28694. 0xB9, 0x54, 0x84, 0x84, 0xA4, 0x87, 0x3B, 0x1F,
  28695. 0x2C, 0xFF, 0x66, 0xA3, 0x23, 0x09, 0x34, 0x60,
  28696. 0x05, 0xEB, 0x7C, 0x04, 0x44, 0x9E, 0xAD, 0xC4,
  28697. 0x66, 0xBC, 0x47, 0x0E, 0x71, 0xED, 0x6C, 0x77,
  28698. 0x1C, 0x2C, 0xF3, 0x92, 0x5E, 0xEB, 0x78, 0x91,
  28699. 0x56, 0xAC, 0xF5, 0x8D, 0x13, 0x21, 0xA5, 0x23,
  28700. 0xE4, 0xDD, 0xB1, 0x58, 0x4C, 0x6E, 0x3F, 0x4A,
  28701. 0x3E, 0xE5, 0xCC, 0x9C, 0x00, 0x7A, 0x00, 0xFF,
  28702. 0xA2, 0xBF, 0xF2, 0xD3, 0x92, 0xD2, 0x9D, 0xCB,
  28703. 0xEF, 0x44, 0x6D, 0x16, 0x51, 0xC9, 0xB3, 0x63,
  28704. 0x8B, 0x0F, 0xB7, 0xEB, 0xEC, 0xE5, 0xB0, 0x59,
  28705. 0xC9, 0x76, 0x21, 0xFF, 0x7A, 0x1E, 0x1C, 0xE7,
  28706. 0x7A, 0xD5, 0x46, 0x1C, 0x51, 0xE1, 0x3A, 0xF7,
  28707. 0x07, 0x89, 0xE4, 0x88, 0x58, 0x98, 0xEA, 0x20,
  28708. 0xFA, 0x17, 0x85, 0xA0, 0x3E, 0x3E, 0xF8, 0xA2,
  28709. 0x98, 0x33, 0xDA, 0x8D, 0x4D, 0x28, 0x32, 0x53,
  28710. 0x74, 0xE4, 0xF4, 0x67, 0x1B, 0xDA, 0x5B, 0x8F,
  28711. 0xAE, 0x9C, 0x9F, 0x68, 0x88, 0xAD, 0x5B, 0xCD,
  28712. 0x0B, 0x91, 0x16, 0x47, 0x35, 0x73, 0xFB, 0x78,
  28713. 0xDD, 0xA7, 0x9A, 0x27, 0x8D, 0x0D, 0x6A, 0xFF,
  28714. 0x71, 0xD0, 0x37, 0x7F, 0xB5, 0x0C, 0x47, 0xEC,
  28715. 0xBD, 0xB4, 0xB1, 0x42, 0x7D, 0xF8, 0xD4, 0x55,
  28716. 0xCC, 0x34, 0x29, 0xCC, 0x84, 0xA1, 0x4A, 0x99,
  28717. 0xFB, 0xE4, 0xAD, 0x1E, 0xB3, 0xE8, 0x07, 0x48,
  28718. 0x3E, 0x62, 0x26, 0xC7, 0x59, 0xAA, 0x0A, 0x6C,
  28719. 0x96, 0x04, 0x41, 0x19, 0x76, 0x8A, 0xB5, 0xA9,
  28720. 0x9B, 0x6F, 0xCD, 0x5B, 0x00, 0x7A, 0x18, 0x82,
  28721. 0xF4, 0xEC, 0xD3, 0x71, 0x39, 0x64, 0x33, 0x32,
  28722. 0x74, 0x4D, 0x08, 0x77, 0xB4, 0x47, 0x01, 0xB0,
  28723. 0xE3, 0xB5, 0xD1, 0x22, 0x8A, 0xB1, 0xE3, 0xA7,
  28724. 0xF9, 0x94, 0xA2, 0xC0, 0xC3, 0xC1, 0x6C, 0x1E,
  28725. 0x6E, 0x02, 0xDF, 0xBF, 0x44, 0x44, 0x4F, 0x6A,
  28726. 0x9D, 0x58, 0x25, 0x49, 0x2A, 0x10, 0x88, 0xD1,
  28727. 0x91, 0xB4, 0x62, 0x22, 0x76, 0x14, 0x30, 0xDD,
  28728. 0x50, 0xEB, 0xF1, 0x1C, 0xAB, 0xE9, 0x95, 0x5C,
  28729. 0x5A, 0x10, 0x79, 0xC4, 0xB7, 0xF2, 0xF4, 0x8F,
  28730. 0xB6, 0xF3, 0x89, 0x3A, 0xBE, 0x38, 0xDD, 0xB0,
  28731. 0x4A, 0xB1, 0x8C, 0xAE, 0x8D, 0x50, 0x70, 0xBD,
  28732. 0x9E, 0x05, 0x71, 0x0A, 0x7B, 0x4C, 0xBE, 0xE2,
  28733. 0xAE, 0x3F, 0xEC, 0xCF, 0xFA, 0x26, 0x65, 0xBE,
  28734. 0xC9, 0xD0, 0xBC, 0x6B, 0x57, 0x34, 0xF2, 0x4C,
  28735. 0x29, 0xB5, 0x9E, 0xF8, 0x21, 0x99, 0x77, 0x58,
  28736. 0x73, 0x2B, 0x04, 0x72, 0xFE, 0x63, 0x81, 0x63,
  28737. 0xC0, 0xF8, 0x67, 0x64, 0x48, 0x12, 0xBB, 0x95,
  28738. 0x83, 0xEE, 0xB7, 0xED, 0x64, 0x73, 0xEE, 0x97,
  28739. 0x23, 0x35, 0xA5, 0x06, 0xCE, 0x64, 0x94, 0x90,
  28740. 0x49, 0x26, 0x3E, 0x32, 0x77, 0x92, 0x88, 0xF8,
  28741. 0xD0, 0x16, 0x5E, 0xE5, 0x22, 0x79, 0xF7, 0x98,
  28742. 0xE0, 0x2F, 0x8B, 0xDE, 0xA2, 0x5F, 0x67, 0x8E,
  28743. 0x2A, 0xB9, 0xE8, 0x8A, 0x77, 0xD2, 0x89, 0x2B,
  28744. 0x79, 0x69, 0x22, 0x1B, 0x9D, 0x81, 0xFD, 0xA7,
  28745. 0x6B, 0x91, 0xD1, 0x8F, 0xE6, 0xBF, 0x14, 0xA1,
  28746. 0xC2, 0x3A, 0xAF, 0x42, 0xCB, 0x95, 0x53, 0x44,
  28747. 0x82, 0xAC, 0x2C, 0x56, 0x9C, 0x01, 0xE0, 0xC2,
  28748. 0x0E, 0x65, 0x8A, 0x1E, 0x17, 0xC1, 0xD8, 0xB3,
  28749. 0x8F, 0xF6, 0x44, 0xD5, 0x3F, 0x41, 0x10, 0x65,
  28750. 0x55, 0x14, 0x09, 0xB7, 0x4A, 0x7A, 0x26, 0x04,
  28751. 0x42, 0xE1, 0x96, 0xE1, 0x2B, 0x48, 0x33, 0x96,
  28752. 0x86, 0x3E, 0x6D, 0x67, 0xDC, 0x4E, 0x54, 0x07,
  28753. 0x4C, 0x73, 0x97, 0x9E, 0x69, 0x83, 0x07, 0xCF,
  28754. 0xA6, 0x37, 0xA3, 0xFD, 0xBB, 0xD0, 0xAA, 0xA0,
  28755. 0xA7, 0x97, 0x0B, 0x38, 0xEE, 0x38, 0xB1, 0x85,
  28756. 0xA6, 0x68, 0x71, 0x29, 0xE6, 0xA2, 0xFF, 0x62,
  28757. 0x4F, 0x65, 0x6C, 0x53, 0x80, 0x5B, 0x72, 0xF4,
  28758. 0xD2, 0x41, 0x50, 0x59, 0x01, 0x22, 0xC8, 0x25,
  28759. 0x96, 0x5C, 0xE1, 0x24, 0x7D, 0x3B, 0x4A, 0x10,
  28760. 0xEE, 0x82, 0x47, 0xDF, 0xB5, 0x60, 0x25, 0x4F,
  28761. 0x79, 0x91, 0x09, 0x82, 0xED, 0x30, 0xE7, 0xF3,
  28762. 0xE6, 0x7A, 0x9F, 0xD1, 0xF0, 0xA7, 0x15, 0x60,
  28763. 0xB9, 0xDF, 0xAB, 0x4A, 0xCC, 0x0A, 0x78, 0xC9,
  28764. 0x9A, 0x7F, 0x15, 0x26, 0x81, 0x2C, 0x16, 0xA7,
  28765. 0xDC, 0x4A, 0xA8, 0xBB, 0x9F, 0xA6, 0xCA, 0xA6,
  28766. 0x80, 0xE3, 0x60, 0x2D, 0x13, 0x3C, 0xCA, 0xE9,
  28767. 0xD1, 0xAD, 0x39, 0xFD, 0xA6, 0xA3, 0xDA, 0x7F,
  28768. 0xF1, 0x0A, 0x25, 0xBD, 0x8F, 0x05, 0xBC, 0x17,
  28769. 0x98, 0xCD, 0x01, 0x2F, 0x63, 0x16, 0xCB, 0xF1,
  28770. 0xC9, 0x9F, 0x75, 0x98, 0x62, 0x43, 0x3B, 0xB7,
  28771. 0x55, 0xCC, 0x5C, 0x40, 0xF3, 0x87, 0x55, 0xEA,
  28772. 0x03, 0xA5, 0x00, 0xB4, 0x7F, 0xDD, 0x2D, 0xBD,
  28773. 0xFE, 0x05, 0x60, 0x69, 0x42, 0xB5, 0x99, 0x1E,
  28774. 0x0E, 0xA8, 0xEC, 0x04, 0x7E, 0x6F, 0x77, 0xD4,
  28775. 0x92, 0x8F, 0xC7, 0xE0, 0xB5, 0x06, 0x05, 0x76,
  28776. 0xFA, 0x45, 0x25, 0xB1, 0xD1, 0xF4, 0x01, 0x3A,
  28777. 0x49, 0xA9, 0x93, 0x77, 0x4B, 0xEE, 0xA2, 0xE8,
  28778. 0xAA, 0x39, 0xBD, 0xD9, 0x90, 0xEB, 0x0F, 0xB5,
  28779. 0xFA, 0xB6, 0xC4, 0x10, 0x79, 0xF5, 0x6A, 0x32,
  28780. 0xFD, 0x9C, 0x38, 0xAC, 0xEC, 0x84, 0xF1, 0x62,
  28781. 0xC6, 0x71, 0xE9, 0xDC, 0x55, 0xD8, 0x40, 0x1D,
  28782. 0x29, 0x8A, 0x97, 0x02, 0x08, 0x44, 0x23, 0x27,
  28783. 0x51, 0x13, 0xC3, 0x46, 0x62, 0x4A, 0x8F, 0x0E,
  28784. 0x79, 0xFC, 0xC3, 0x59, 0x33, 0xC7, 0x75, 0x7F,
  28785. 0x24, 0xE0, 0x7A, 0x25, 0x7A, 0xBF, 0xEA, 0x28,
  28786. 0xDA, 0x2B, 0xDE, 0x70, 0x73, 0xAE, 0xC1, 0x33,
  28787. 0xE6, 0x55, 0xC5, 0xA4, 0xB8, 0x94, 0xC5, 0x95,
  28788. 0x10, 0x2E, 0x7D, 0x13, 0x80, 0x96, 0xCD, 0x6F,
  28789. 0x97, 0xF5, 0x62, 0x27, 0x62, 0x4D, 0xCC, 0xAB,
  28790. 0xFB, 0x9D, 0xCF, 0xE9, 0x0E, 0x26, 0x5A, 0x04,
  28791. 0x9B, 0x90, 0xA3, 0x2E, 0x54, 0xD4, 0x81, 0xE2,
  28792. 0xF3, 0x7B, 0x00, 0x2C, 0xFE, 0x9C, 0xC9, 0xCD,
  28793. 0x24, 0x3F, 0x83, 0xE3, 0x62, 0x09, 0x4C, 0xFB,
  28794. 0xF7, 0x0E, 0x1A, 0x94, 0x79, 0xC5, 0x7E, 0x30,
  28795. 0xB4, 0xB9, 0xEB, 0x38, 0x31, 0x6F, 0xA9, 0xE9,
  28796. 0xBE, 0xE1, 0x90, 0xC6, 0xE8, 0xA8, 0xD4, 0x5C,
  28797. 0xA3, 0x40, 0x76, 0xF4, 0x84, 0x0D, 0xBC, 0x9F,
  28798. 0x42, 0x5C, 0xD5, 0x70, 0x98, 0x8E, 0x58, 0x86,
  28799. 0xC2, 0x7B, 0x64, 0xC2, 0x28, 0x15, 0x8D, 0xA7,
  28800. 0xE2, 0x82, 0xF6, 0xC0, 0x09, 0xC1, 0x61, 0xBA,
  28801. 0xC8, 0xE4, 0xDE, 0x67, 0xCD, 0x46, 0xAB, 0x9D,
  28802. 0x5A, 0xDC, 0xF7, 0xF5, 0xF6, 0xA0, 0x6D, 0x36,
  28803. 0xB5, 0xEC, 0xA3, 0x41, 0x7C, 0x40, 0xDA, 0xAC,
  28804. 0x80, 0x55, 0xFF, 0x3D, 0x77, 0x3D, 0x14, 0x1F,
  28805. 0x50, 0x40, 0x58, 0x74, 0x48, 0xF0, 0xA9, 0x10,
  28806. 0x7C, 0x44, 0xED, 0x67, 0x1A, 0x34, 0xF2, 0x5C,
  28807. 0x4F, 0x86, 0x6F, 0xCA, 0xA6, 0xC1, 0x42, 0x4F,
  28808. 0xE9, 0x59, 0x59, 0x9E, 0xFE, 0x31, 0x63, 0x56,
  28809. 0xF9, 0x05, 0xDD, 0xD6, 0x8B, 0x27, 0xA4, 0xA0,
  28810. 0xBB, 0x17, 0x28, 0x52, 0x0D, 0x5D, 0x36, 0x77,
  28811. 0x7E, 0x22, 0xF1, 0xFE, 0xDE, 0x19, 0x3B, 0x07,
  28812. 0xB3, 0xA2, 0xD2, 0x09, 0x9C, 0x41, 0x93, 0x1E,
  28813. 0xB4, 0xE0, 0xB0, 0xFA, 0x3F, 0xA7, 0xC6, 0x66,
  28814. 0xD8, 0xA7, 0xFD, 0x90, 0xCD, 0x49, 0xA2, 0xEE,
  28815. 0x25, 0x66, 0xCE, 0x31, 0x9E, 0x03, 0x35, 0x23,
  28816. 0x32, 0x2E, 0xDA, 0x7C, 0xD3, 0x14, 0x98, 0x88,
  28817. 0xBA, 0x9B, 0xEF, 0x0C, 0x5F, 0x82, 0x6F, 0x3C,
  28818. 0x5A, 0xB8, 0x6D, 0x2D, 0xD0, 0x84, 0xA3, 0xC3,
  28819. 0x16, 0xDE, 0x98, 0xCB, 0x42, 0x68, 0x4E, 0x0D,
  28820. 0x64, 0x41, 0xE0, 0x73, 0xA5, 0x9F, 0x27, 0x67,
  28821. 0x55, 0x1E, 0x56, 0x48, 0xBF, 0xBE, 0x49, 0x8E,
  28822. 0xF3, 0xC0, 0x3B, 0x65, 0xD2, 0x9F, 0x5F, 0xA8,
  28823. 0x4D, 0x99, 0x79, 0x9F, 0x1D, 0x32, 0xC2, 0x26,
  28824. 0xFB, 0xF7, 0x39, 0x65, 0x7F, 0x02, 0x61, 0xE8,
  28825. 0x96, 0x02, 0xF7, 0xBD, 0x07, 0x73, 0x41, 0x6D,
  28826. 0xFE, 0xEA, 0x80, 0x1C, 0xE1, 0xA7, 0x55, 0x3F,
  28827. 0x1E, 0xF9, 0x34, 0x1A, 0x5F, 0x74, 0x09, 0x25,
  28828. 0x80, 0x75, 0xBE, 0xF5, 0x82, 0x83, 0x35, 0x66,
  28829. 0x1A, 0x8C, 0xB1, 0x50, 0xD5, 0x37, 0x0B, 0xEC
  28830. };
  28831. const byte kyber768_ss[] = {
  28832. 0x44, 0xF6, 0x94, 0xE4, 0x78, 0xEB, 0xAC, 0x4A,
  28833. 0x55, 0x6A, 0x38, 0xA2, 0x5C, 0x95, 0x9B, 0x62,
  28834. 0xAC, 0xC7, 0x2E, 0x17, 0xCF, 0x04, 0xB4, 0xD4,
  28835. 0x7E, 0x54, 0xB0, 0xB7, 0xFE, 0xAC, 0xEB, 0x56
  28836. };
  28837. #endif
  28838. ret = wc_KyberKey_Init(KYBER768, &key, HEAP_HINT, INVALID_DEVID);
  28839. if (ret != 0)
  28840. return -20201;
  28841. ret = wc_KyberKey_MakeKeyWithRandom(&key, kyber768_rand,
  28842. sizeof(kyber768_rand));
  28843. if (ret != 0)
  28844. return -20202;
  28845. ret = wc_KyberKey_EncodePublicKey(&key, pub, sizeof(pub));
  28846. if (ret != 0)
  28847. return -20203;
  28848. ret = wc_KyberKey_EncodePrivateKey(&key, priv, sizeof(priv));
  28849. if (ret != 0)
  28850. return -20204;
  28851. if (XMEMCMP(pub, kyber768_pk, sizeof(kyber768_pk)) != 0)
  28852. return -20205;
  28853. if (XMEMCMP(priv, kyber768_sk, sizeof(kyber768_sk)) != 0)
  28854. return -20206;
  28855. ret = wc_KyberKey_EncapsulateWithRandom(&key, ct, ss, kyber768enc_rand,
  28856. sizeof(kyber768enc_rand));
  28857. if (ret != 0)
  28858. return -20207;
  28859. if (XMEMCMP(ct, kyber768_ct, sizeof(kyber768_ct)) != 0)
  28860. return -20208;
  28861. if (XMEMCMP(ss, kyber768_ss, sizeof(kyber768_ss)) != 0)
  28862. return -20209;
  28863. ret = wc_KyberKey_Decapsulate(&key, ss_dec, ct, sizeof(kyber768_ct));
  28864. if (ret != 0)
  28865. return -20210;
  28866. if (XMEMCMP(ss_dec, kyber768_ss, sizeof(kyber768_ss)) != 0)
  28867. return -20211;
  28868. wc_KyberKey_Free(&key);
  28869. return 0;
  28870. }
  28871. #endif /* WOLFSSL_KYBER768 */
  28872. #ifdef WOLFSSL_KYBER1024
  28873. static int kyber1024_kat(void)
  28874. {
  28875. KyberKey key;
  28876. int ret;
  28877. byte priv[KYBER1024_PRIVATE_KEY_SIZE];
  28878. byte pub[KYBER1024_PUBLIC_KEY_SIZE];
  28879. byte ct[KYBER1024_CIPHER_TEXT_SIZE];
  28880. byte ss[KYBER_SS_SZ];
  28881. byte ss_dec[KYBER_SS_SZ];
  28882. const byte kyber1024_rand[] = {
  28883. 0x7c, 0x99, 0x35, 0xa0, 0xb0, 0x76, 0x94, 0xaa,
  28884. 0x0c, 0x6d, 0x10, 0xe4, 0xdb, 0x6b, 0x1a, 0xdd,
  28885. 0x2f, 0xd8, 0x1a, 0x25, 0xcc, 0xb1, 0x48, 0x03,
  28886. 0x2d, 0xcd, 0x73, 0x99, 0x36, 0x73, 0x7f, 0x2d,
  28887. 0x86, 0x26, 0xED, 0x79, 0xD4, 0x51, 0x14, 0x08,
  28888. 0x00, 0xE0, 0x3B, 0x59, 0xB9, 0x56, 0xF8, 0x21,
  28889. 0x0E, 0x55, 0x60, 0x67, 0x40, 0x7D, 0x13, 0xDC,
  28890. 0x90, 0xFA, 0x9E, 0x8B, 0x87, 0x2B, 0xFB, 0x8F
  28891. };
  28892. const byte kyber1024enc_rand[] = {
  28893. 0x14, 0x7c, 0x03, 0xf7, 0xa5, 0xbe, 0xbb, 0xa4,
  28894. 0x06, 0xc8, 0xfa, 0xe1, 0x87, 0x4d, 0x7f, 0x13,
  28895. 0xc8, 0x0e, 0xfe, 0x79, 0xa3, 0xa9, 0xa8, 0x74,
  28896. 0xcc, 0x09, 0xfe, 0x76, 0xf6, 0x99, 0x76, 0x15
  28897. };
  28898. #ifndef WOLFSSL_KYBER_90S
  28899. const byte kyber1024_pk[] = {
  28900. 0xD2, 0x23, 0x02, 0xCB, 0xD3, 0x39, 0x9F, 0xAC,
  28901. 0xC6, 0x30, 0x99, 0x1F, 0xC8, 0xF2, 0x8B, 0xDB,
  28902. 0x43, 0x54, 0x76, 0x25, 0x41, 0x52, 0x76, 0x78,
  28903. 0xBC, 0xF6, 0x1F, 0x65, 0xC2, 0x41, 0x14, 0x6C,
  28904. 0x42, 0x6D, 0x23, 0xB9, 0xBF, 0xAA, 0x6B, 0x7D,
  28905. 0xF1, 0x8C, 0x97, 0xF2, 0x0C, 0x1B, 0x61, 0x25,
  28906. 0xBF, 0x87, 0x4B, 0x1D, 0x89, 0x47, 0x58, 0x52,
  28907. 0xC4, 0x48, 0x21, 0x5D, 0xB0, 0xEB, 0x77, 0x37,
  28908. 0xF9, 0x14, 0x80, 0xE8, 0xCE, 0xBD, 0x9A, 0x08,
  28909. 0x71, 0x57, 0x4F, 0x5A, 0xB6, 0x2D, 0x90, 0x20,
  28910. 0x17, 0x5E, 0xC6, 0x92, 0x7C, 0xA0, 0xB5, 0x4C,
  28911. 0x09, 0x81, 0x8E, 0x42, 0xCF, 0x92, 0xA3, 0x83,
  28912. 0x17, 0x24, 0x22, 0xC7, 0xDC, 0x18, 0x31, 0xD6,
  28913. 0x3B, 0x0C, 0x29, 0x5D, 0xE7, 0x51, 0x59, 0xDB,
  28914. 0x80, 0x34, 0xE9, 0xE0, 0x7F, 0x7B, 0x0B, 0x91,
  28915. 0x0C, 0x3C, 0x1E, 0x5F, 0xB6, 0x6B, 0x3D, 0xC5,
  28916. 0x23, 0xF1, 0xFA, 0x6E, 0xB4, 0x91, 0x0C, 0xB8,
  28917. 0x9A, 0x6C, 0x17, 0x56, 0x2C, 0x83, 0xAB, 0x4C,
  28918. 0x18, 0xD0, 0xCD, 0x7E, 0x07, 0x96, 0x59, 0x2A,
  28919. 0x37, 0x2A, 0xA4, 0x09, 0xB1, 0xC5, 0x57, 0x34,
  28920. 0x7C, 0xCA, 0xCD, 0xC4, 0x64, 0x4A, 0x11, 0x90,
  28921. 0x64, 0xD0, 0x6D, 0xD4, 0x74, 0x92, 0x9D, 0x1C,
  28922. 0x6F, 0xB4, 0xD6, 0x86, 0xE5, 0x49, 0x1C, 0xE4,
  28923. 0xBC, 0x89, 0xA3, 0x0B, 0xB4, 0xB8, 0xC4, 0x1B,
  28924. 0xCE, 0x51, 0x57, 0xDF, 0xC1, 0x36, 0x08, 0x23,
  28925. 0xB1, 0xAB, 0x61, 0x8C, 0x14, 0xB1, 0x0F, 0x98,
  28926. 0xC2, 0x50, 0x67, 0x39, 0x8E, 0xA7, 0x01, 0x8C,
  28927. 0x27, 0x8A, 0x4B, 0x3D, 0xF3, 0x13, 0x34, 0xD6,
  28928. 0x03, 0xB2, 0x04, 0x4E, 0xF1, 0x87, 0xCD, 0x9B,
  28929. 0xC6, 0xCE, 0x42, 0x72, 0x5B, 0xD9, 0x62, 0xC2,
  28930. 0x64, 0x98, 0x3E, 0x9E, 0x18, 0x15, 0x5A, 0x8B,
  28931. 0x9C, 0x47, 0x14, 0x3D, 0x70, 0x46, 0x0A, 0x26,
  28932. 0xA5, 0x6F, 0xE7, 0x65, 0x8C, 0x1F, 0x15, 0x03,
  28933. 0x48, 0xC6, 0x08, 0x7E, 0xF7, 0x58, 0xAD, 0x16,
  28934. 0x78, 0x87, 0x86, 0x0A, 0x00, 0x7A, 0x5F, 0xC3,
  28935. 0x73, 0x58, 0xD4, 0x3B, 0x5E, 0xBE, 0xE8, 0x20,
  28936. 0xAC, 0xEA, 0x47, 0x4F, 0x0A, 0xC0, 0x7B, 0x76,
  28937. 0x80, 0x28, 0x66, 0x19, 0x9C, 0x61, 0x23, 0x1D,
  28938. 0x5C, 0x74, 0x7C, 0x93, 0x77, 0x4D, 0x2C, 0x1E,
  28939. 0x0C, 0x1C, 0x67, 0xE6, 0xC8, 0x1B, 0x82, 0x75,
  28940. 0x21, 0x73, 0xE1, 0x25, 0xBA, 0xF3, 0x9B, 0x4F,
  28941. 0xD1, 0x9A, 0x4F, 0x45, 0x3D, 0xC5, 0x79, 0x76,
  28942. 0xB1, 0xD9, 0x7F, 0xE6, 0x99, 0x69, 0x92, 0xBB,
  28943. 0xB6, 0x5B, 0x7C, 0xB2, 0x5D, 0x07, 0x7B, 0xBA,
  28944. 0xA6, 0xA1, 0x33, 0x22, 0x89, 0x9A, 0xF6, 0x59,
  28945. 0xCF, 0x1B, 0x35, 0x58, 0xC1, 0xB5, 0x00, 0x11,
  28946. 0x54, 0xB6, 0x25, 0x80, 0x9E, 0xD8, 0x9A, 0xEE,
  28947. 0xBB, 0x89, 0xE6, 0xEA, 0x7D, 0x67, 0xF7, 0x23,
  28948. 0xD0, 0x45, 0xAB, 0x05, 0x71, 0x5C, 0x42, 0x35,
  28949. 0x5D, 0xA6, 0xA5, 0xC8, 0xDD, 0x39, 0xC8, 0xAB,
  28950. 0xE3, 0x03, 0x77, 0x51, 0xA0, 0x1E, 0xD1, 0xC7,
  28951. 0x37, 0x49, 0x19, 0xF3, 0x12, 0x1B, 0x5A, 0x52,
  28952. 0xC5, 0x3D, 0x14, 0x87, 0x31, 0x67, 0x69, 0xF8,
  28953. 0x07, 0x21, 0xDE, 0xEA, 0xAA, 0xD3, 0xC9, 0x0F,
  28954. 0x76, 0xE7, 0xAE, 0x9E, 0x12, 0xBA, 0x92, 0xB3,
  28955. 0x2B, 0x5F, 0xD4, 0x57, 0xE3, 0xC7, 0x52, 0xC2,
  28956. 0x65, 0x0D, 0xFB, 0x88, 0x57, 0x71, 0xCB, 0x77,
  28957. 0xAC, 0x3C, 0x78, 0x5A, 0x8C, 0x56, 0x2E, 0x6A,
  28958. 0x1C, 0x63, 0xC2, 0xA5, 0x5E, 0xA4, 0x7C, 0xF8,
  28959. 0xB9, 0x0E, 0xB8, 0x22, 0x5C, 0x12, 0x3C, 0x34,
  28960. 0x64, 0x52, 0x56, 0x62, 0x35, 0xB2, 0xF3, 0x18,
  28961. 0x23, 0xA3, 0x35, 0x21, 0xE0, 0x87, 0x93, 0x7A,
  28962. 0x34, 0x5D, 0x8D, 0x66, 0x3E, 0xEA, 0xA0, 0x56,
  28963. 0x58, 0x91, 0x7B, 0xBA, 0xA0, 0x08, 0xC2, 0xE3,
  28964. 0x35, 0xF8, 0x85, 0x0A, 0x90, 0xA3, 0x26, 0xD0,
  28965. 0xE6, 0x64, 0x32, 0xF4, 0x4C, 0xEB, 0x82, 0x89,
  28966. 0xE4, 0xEC, 0xB2, 0xD1, 0x29, 0x58, 0xE9, 0x84,
  28967. 0x07, 0x2E, 0xCA, 0xCB, 0x88, 0xE1, 0x34, 0x8F,
  28968. 0xF0, 0xB5, 0x56, 0x54, 0xAC, 0xBA, 0x5B, 0x54,
  28969. 0x97, 0x1C, 0xBA, 0xEB, 0xA8, 0x8E, 0xC4, 0xB9,
  28970. 0x1A, 0x94, 0xC3, 0x71, 0x92, 0xFA, 0x98, 0x2B,
  28971. 0xEC, 0xB9, 0xF3, 0xDA, 0x42, 0x16, 0x03, 0xB6,
  28972. 0x1A, 0x51, 0xBC, 0x8E, 0x36, 0xCB, 0xD0, 0x53,
  28973. 0x85, 0x1C, 0x77, 0xB1, 0xB9, 0x26, 0xB1, 0x7A,
  28974. 0x27, 0x2A, 0xA9, 0x02, 0x32, 0x46, 0xB0, 0x2B,
  28975. 0x3E, 0xD4, 0x7F, 0x66, 0xA0, 0x0B, 0xD5, 0x68,
  28976. 0x48, 0x23, 0x63, 0x4E, 0x7C, 0xE5, 0x8C, 0xF8,
  28977. 0xF3, 0x06, 0xE3, 0x5B, 0x1E, 0x53, 0x22, 0x82,
  28978. 0x4D, 0x90, 0x48, 0x01, 0xF0, 0xA2, 0xFA, 0x7C,
  28979. 0x2B, 0xC9, 0xC2, 0x52, 0xB0, 0xA5, 0x6B, 0x7B,
  28980. 0xA2, 0xAB, 0x0F, 0x63, 0x60, 0x21, 0x74, 0x5A,
  28981. 0x70, 0xA9, 0xA4, 0x3E, 0x2B, 0x0A, 0x8D, 0x61,
  28982. 0x59, 0x70, 0xB6, 0x53, 0x09, 0x62, 0x4B, 0x51,
  28983. 0x84, 0xBC, 0xC3, 0x0B, 0x91, 0x16, 0x79, 0xAE,
  28984. 0xDD, 0x76, 0x02, 0x5F, 0xE3, 0x90, 0x8F, 0xD6,
  28985. 0x78, 0x97, 0xB0, 0xCF, 0x4B, 0xE5, 0xA6, 0xF5,
  28986. 0x41, 0x3D, 0x7D, 0xD9, 0x85, 0x64, 0xB2, 0x3E,
  28987. 0x42, 0xA9, 0x3E, 0x4A, 0xA8, 0x82, 0x1C, 0xD4,
  28988. 0x50, 0x54, 0xC6, 0x43, 0xED, 0xC1, 0x15, 0x8D,
  28989. 0xB6, 0xB3, 0xDE, 0xB1, 0x3F, 0xB5, 0xA5, 0x1E,
  28990. 0xBD, 0x1A, 0x8A, 0x78, 0xB8, 0x72, 0x25, 0xA7,
  28991. 0x33, 0x8E, 0x10, 0x11, 0x04, 0xC4, 0xA2, 0x20,
  28992. 0xD9, 0xBD, 0xED, 0xD4, 0x8C, 0x85, 0xA1, 0xC2,
  28993. 0xDA, 0xE7, 0x81, 0xA8, 0x0C, 0x40, 0xE1, 0x3B,
  28994. 0x87, 0xEA, 0xC7, 0x3A, 0x76, 0x42, 0x01, 0xC9,
  28995. 0xB7, 0x60, 0xCC, 0xFB, 0x1A, 0xE3, 0x92, 0x69,
  28996. 0x9C, 0x70, 0x39, 0xD2, 0x7C, 0x39, 0x36, 0x2B,
  28997. 0x27, 0xB8, 0xFC, 0x6F, 0x07, 0xA8, 0xA3, 0xD4,
  28998. 0x41, 0x0F, 0x15, 0x47, 0xC4, 0x8A, 0x99, 0x97,
  28999. 0xF6, 0x2C, 0x61, 0x07, 0x44, 0x52, 0xEF, 0x15,
  29000. 0x15, 0xF8, 0xA6, 0x49, 0xEB, 0xCA, 0x94, 0x37,
  29001. 0x20, 0x5A, 0x4E, 0x8A, 0x61, 0x60, 0x6B, 0x41,
  29002. 0xDA, 0xF6, 0x83, 0x4D, 0x67, 0x1F, 0x4D, 0x85,
  29003. 0x2C, 0x0C, 0x9C, 0x40, 0x96, 0x61, 0x16, 0x48,
  29004. 0xC6, 0xA3, 0x17, 0x06, 0x78, 0xB1, 0x53, 0x7C,
  29005. 0xC1, 0x82, 0x8D, 0x93, 0x58, 0x0C, 0x9E, 0x58,
  29006. 0x49, 0xA9, 0x65, 0x31, 0x75, 0xAC, 0xB7, 0x53,
  29007. 0xF2, 0xBE, 0x74, 0x37, 0xBE, 0x45, 0xF6, 0xC6,
  29008. 0x03, 0xE4, 0x85, 0xF2, 0xEC, 0x30, 0x1B, 0xB4,
  29009. 0x2B, 0x6C, 0x37, 0xC2, 0x25, 0xD7, 0x49, 0x5A,
  29010. 0x58, 0x4A, 0xE2, 0x31, 0x89, 0x0A, 0xB5, 0xC8,
  29011. 0xC3, 0x5C, 0x26, 0x8C, 0xF4, 0xBB, 0xB0, 0x21,
  29012. 0x3C, 0x09, 0x60, 0x19, 0x31, 0x95, 0x61, 0xA8,
  29013. 0xA6, 0x94, 0x76, 0x37, 0xAA, 0x40, 0xD0, 0x06,
  29014. 0xB4, 0x15, 0xBB, 0x2C, 0xFA, 0x22, 0x37, 0xE0,
  29015. 0x89, 0x0B, 0x6A, 0x3B, 0xC1, 0x34, 0xAB, 0xF8,
  29016. 0xF6, 0x58, 0x5E, 0x10, 0x8D, 0x15, 0x94, 0x0F,
  29017. 0x91, 0xF4, 0xBF, 0x5B, 0x0C, 0x81, 0x80, 0x55,
  29018. 0xB2, 0x1D, 0xEA, 0x6E, 0x63, 0xB5, 0x53, 0x98,
  29019. 0x8C, 0x47, 0xF4, 0xB9, 0x4E, 0x7C, 0xF8, 0x00,
  29020. 0xA4, 0x93, 0xB4, 0x73, 0x47, 0x05, 0xED, 0xC5,
  29021. 0x6A, 0x4B, 0x60, 0x21, 0xC6, 0x29, 0x50, 0x06,
  29022. 0x75, 0x87, 0x68, 0x04, 0xCF, 0x0B, 0x95, 0x1F,
  29023. 0x03, 0x8A, 0x5C, 0x7F, 0xE5, 0x8E, 0x89, 0x77,
  29024. 0x4E, 0xF2, 0x99, 0x2F, 0xD7, 0xC6, 0x30, 0x99,
  29025. 0xD3, 0x52, 0xA7, 0xD2, 0x15, 0x60, 0xB7, 0x88,
  29026. 0xB4, 0x05, 0x70, 0x98, 0x61, 0x81, 0x7E, 0x59,
  29027. 0xA9, 0x6B, 0x3A, 0x3A, 0x83, 0xCB, 0xA8, 0x03,
  29028. 0xB1, 0x69, 0x34, 0x33, 0x10, 0x71, 0x90, 0x5B,
  29029. 0xBE, 0xC6, 0x53, 0x29, 0x00, 0x15, 0x5D, 0x8A,
  29030. 0xC8, 0x8C, 0xB3, 0x2E, 0x4E, 0x21, 0xA3, 0xBD,
  29031. 0x3A, 0x03, 0xFD, 0xEC, 0x32, 0x5A, 0x51, 0xCD,
  29032. 0x27, 0x73, 0x96, 0x4E, 0x67, 0x84, 0xFC, 0xF1,
  29033. 0x85, 0x37, 0x37, 0xAA, 0x64, 0xEB, 0x67, 0x56,
  29034. 0x47, 0x27, 0x27, 0x26, 0x61, 0xAB, 0xF8, 0x43,
  29035. 0x13, 0xA5, 0x7A, 0x44, 0xB1, 0x23, 0xC6, 0x55,
  29036. 0x09, 0xCF, 0xB7, 0xA6, 0xF6, 0x64, 0x1C, 0xDC,
  29037. 0xC3, 0xB5, 0x7F, 0xE6, 0x28, 0xC7, 0xB8, 0x19,
  29038. 0x2D, 0xB4, 0x4F, 0xFB, 0xF5, 0x79, 0x6A, 0x86,
  29039. 0x13, 0xB1, 0xFA, 0x12, 0x6F, 0x60, 0x76, 0x88,
  29040. 0x3C, 0x78, 0x3D, 0xC2, 0x4E, 0x2A, 0x44, 0x64,
  29041. 0xC4, 0x0B, 0x3A, 0x41, 0xCA, 0x70, 0xAE, 0x87,
  29042. 0x62, 0x08, 0x66, 0xCF, 0x4F, 0xCB, 0x2B, 0xD2,
  29043. 0x04, 0xBF, 0x5C, 0x28, 0x38, 0x12, 0xBA, 0x05,
  29044. 0x6A, 0xC0, 0xC3, 0x45, 0xE3, 0x79, 0xC4, 0xBA,
  29045. 0x24, 0xD7, 0x50, 0x90, 0x12, 0x79, 0xBB, 0x2F,
  29046. 0x3A, 0x16, 0xF6, 0x12, 0xBF, 0xAD, 0xB3, 0x57,
  29047. 0x03, 0x33, 0x2C, 0x7C, 0x13, 0x6F, 0x68, 0xEA,
  29048. 0xB6, 0x75, 0x5C, 0x66, 0xB6, 0xA4, 0xAD, 0x1A,
  29049. 0xAB, 0xA7, 0xB7, 0x68, 0xA5, 0x8A, 0xCA, 0xAC,
  29050. 0xC1, 0x0A, 0x45, 0x9A, 0x1C, 0xC8, 0xEF, 0x29,
  29051. 0x37, 0x7B, 0xC2, 0x00, 0xE4, 0xD3, 0x15, 0xA3,
  29052. 0x0A, 0x6B, 0xCC, 0x32, 0x56, 0xF9, 0x73, 0x4D,
  29053. 0x06, 0xE9, 0x77, 0x9C, 0xAA, 0x54, 0x42, 0xA9,
  29054. 0xA1, 0x60, 0x69, 0x08, 0x13, 0x77, 0xC7, 0x6E,
  29055. 0x75, 0x15, 0x43, 0x68, 0x07, 0x2D, 0xC4, 0x46,
  29056. 0xED, 0x6C, 0x8B, 0x8E, 0x62, 0x2A, 0x21, 0xE3,
  29057. 0x83, 0xCF, 0x9B, 0xA1, 0xFB, 0x43, 0x4E, 0x2E,
  29058. 0xCC, 0x81, 0xE7, 0xB7, 0x8C, 0xEE, 0x98, 0x6B,
  29059. 0x8F, 0xF7, 0x98, 0xAB, 0x18, 0xCF, 0x96, 0x34,
  29060. 0x54, 0x35, 0x46, 0x28, 0x4E, 0xDA, 0x2A, 0x26,
  29061. 0xB4, 0x7F, 0x05, 0xB7, 0x35, 0xBC, 0xDB, 0x12,
  29062. 0x02, 0x22, 0x00, 0x76, 0xDC, 0x8B, 0x4E, 0x4B,
  29063. 0x9F, 0x85, 0x35, 0x33, 0xC8, 0xF6, 0xC7, 0xFF,
  29064. 0x38, 0x81, 0x7B, 0xA4, 0x97, 0x12, 0x83, 0x57,
  29065. 0x85, 0xF1, 0x7F, 0x14, 0xCA, 0x01, 0xD0, 0xC1,
  29066. 0xC1, 0xE9, 0x88, 0x10, 0xFE, 0x0B, 0x36, 0xE5,
  29067. 0xB4, 0x27, 0x15, 0x7B, 0x94, 0x18, 0x44, 0x9C,
  29068. 0xED, 0xD6, 0x41, 0xA4, 0x29, 0x3C, 0x85, 0xC3,
  29069. 0x27, 0x00, 0x10, 0x2A, 0xCE, 0xC2, 0x2E, 0xBA,
  29070. 0xD9, 0x8E, 0xD1, 0x60, 0xA5, 0xF0, 0x27, 0xBD,
  29071. 0x4C, 0xDA, 0x57, 0xF1, 0xF3, 0x72, 0x0A, 0x12,
  29072. 0xC1, 0x34, 0x65, 0x4D, 0xD5, 0xE7, 0x3F, 0x82,
  29073. 0x96, 0x76, 0x49, 0x53, 0x90, 0xD0, 0xE7, 0x92,
  29074. 0x9D, 0x60, 0x34, 0xE9, 0xC5, 0x5F, 0x7D, 0x55,
  29075. 0xBA, 0x65, 0x8B, 0xC5, 0x87, 0x98, 0x8E, 0x8A,
  29076. 0xF9, 0x49, 0x60, 0xF6, 0xCF, 0xB8, 0xD5, 0xAF,
  29077. 0x7A, 0x00, 0x21, 0x53, 0x5A, 0x6E, 0x25, 0xE4,
  29078. 0x37, 0xD4, 0x9A, 0x78, 0x06, 0x98, 0xBE, 0x22,
  29079. 0xAC, 0x99, 0x53, 0x94, 0x9F, 0x57, 0x1B, 0x85,
  29080. 0xA6, 0x85, 0x72, 0x5F, 0x82, 0x07, 0xA2, 0xB0,
  29081. 0xAE, 0x84, 0x9B, 0x60, 0x1A, 0xB9, 0x1B, 0x15,
  29082. 0x9B, 0x3D, 0xF4, 0xA1, 0x54, 0xC2, 0x04, 0x1E,
  29083. 0x77, 0x60, 0x70, 0xAF, 0xC4, 0x29, 0x69, 0x32,
  29084. 0x23, 0x80, 0x91, 0x7C, 0x97, 0x51, 0x07, 0x99,
  29085. 0xF3, 0x14, 0x91, 0x31, 0x47, 0x7E, 0x16, 0x66,
  29086. 0x3D, 0x31, 0x74, 0xC7, 0xC1, 0xCA, 0xEA, 0x78,
  29087. 0x85, 0x35, 0xC6, 0xC0, 0x05, 0xA6, 0x4F, 0x28,
  29088. 0x68, 0x63, 0x1B, 0x31, 0xB6, 0x6E, 0x20, 0x5F,
  29089. 0xD3, 0x8C, 0x1D, 0x84, 0x54, 0x2D, 0x0F, 0x1B,
  29090. 0x57, 0x8F, 0x58, 0xC9, 0xBF, 0x5A, 0x0F, 0xAE,
  29091. 0xAB, 0x6A, 0xB6, 0x49, 0x48, 0x93, 0x05, 0x31,
  29092. 0x65, 0xEA, 0xFD, 0x46, 0x5F, 0xC6, 0x4A, 0x0C,
  29093. 0x5F, 0x8F, 0x3F, 0x90, 0x03, 0x48, 0x94, 0x15,
  29094. 0x89, 0x9D, 0x59, 0xA5, 0x43, 0xD8, 0x20, 0x8C,
  29095. 0x54, 0xA3, 0x16, 0x65, 0x29, 0xB5, 0x39, 0x22
  29096. };
  29097. const byte kyber1024_sk[] = {
  29098. 0x07, 0x63, 0x8F, 0xB6, 0x98, 0x68, 0xF3, 0xD3,
  29099. 0x20, 0xE5, 0x86, 0x2B, 0xD9, 0x69, 0x33, 0xFE,
  29100. 0xB3, 0x11, 0xB3, 0x62, 0x09, 0x3C, 0x9B, 0x5D,
  29101. 0x50, 0x17, 0x0B, 0xCE, 0xD4, 0x3F, 0x1B, 0x53,
  29102. 0x6D, 0x9A, 0x20, 0x4B, 0xB1, 0xF2, 0x26, 0x95,
  29103. 0x95, 0x0B, 0xA1, 0xF2, 0xA9, 0xE8, 0xEB, 0x82,
  29104. 0x8B, 0x28, 0x44, 0x88, 0x76, 0x0B, 0x3F, 0xC8,
  29105. 0x4F, 0xAB, 0xA0, 0x42, 0x75, 0xD5, 0x62, 0x8E,
  29106. 0x39, 0xC5, 0xB2, 0x47, 0x13, 0x74, 0x28, 0x3C,
  29107. 0x50, 0x32, 0x99, 0xC0, 0xAB, 0x49, 0xB6, 0x6B,
  29108. 0x8B, 0xBB, 0x56, 0xA4, 0x18, 0x66, 0x24, 0xF9,
  29109. 0x19, 0xA2, 0xBA, 0x59, 0xBB, 0x08, 0xD8, 0x55,
  29110. 0x18, 0x80, 0xC2, 0xBE, 0xFC, 0x4F, 0x87, 0xF2,
  29111. 0x5F, 0x59, 0xAB, 0x58, 0x7A, 0x79, 0xC3, 0x27,
  29112. 0xD7, 0x92, 0xD5, 0x4C, 0x97, 0x4A, 0x69, 0x26,
  29113. 0x2F, 0xF8, 0xA7, 0x89, 0x38, 0x28, 0x9E, 0x9A,
  29114. 0x87, 0xB6, 0x88, 0xB0, 0x83, 0xE0, 0x59, 0x5F,
  29115. 0xE2, 0x18, 0xB6, 0xBB, 0x15, 0x05, 0x94, 0x1C,
  29116. 0xE2, 0xE8, 0x1A, 0x5A, 0x64, 0xC5, 0xAA, 0xC6,
  29117. 0x04, 0x17, 0x25, 0x69, 0x85, 0x34, 0x9E, 0xE4,
  29118. 0x7A, 0x52, 0x42, 0x0A, 0x5F, 0x97, 0x47, 0x7B,
  29119. 0x72, 0x36, 0xAC, 0x76, 0xBC, 0x70, 0xE8, 0x28,
  29120. 0x87, 0x29, 0x28, 0x7E, 0xE3, 0xE3, 0x4A, 0x3D,
  29121. 0xBC, 0x36, 0x83, 0xC0, 0xB7, 0xB1, 0x00, 0x29,
  29122. 0xFC, 0x20, 0x34, 0x18, 0x53, 0x7E, 0x74, 0x66,
  29123. 0xBA, 0x63, 0x85, 0xA8, 0xFF, 0x30, 0x1E, 0xE1,
  29124. 0x27, 0x08, 0xF8, 0x2A, 0xAA, 0x1E, 0x38, 0x0F,
  29125. 0xC7, 0xA8, 0x8F, 0x8F, 0x20, 0x5A, 0xB7, 0xE8,
  29126. 0x8D, 0x7E, 0x95, 0x95, 0x2A, 0x55, 0xBA, 0x20,
  29127. 0xD0, 0x9B, 0x79, 0xA4, 0x71, 0x41, 0xD6, 0x2B,
  29128. 0xF6, 0xEB, 0x7D, 0xD3, 0x07, 0xB0, 0x8E, 0xCA,
  29129. 0x13, 0xA5, 0xBC, 0x5F, 0x6B, 0x68, 0x58, 0x1C,
  29130. 0x68, 0x65, 0xB2, 0x7B, 0xBC, 0xDD, 0xAB, 0x14,
  29131. 0x2F, 0x4B, 0x2C, 0xBF, 0xF4, 0x88, 0xC8, 0xA2,
  29132. 0x27, 0x05, 0xFA, 0xA9, 0x8A, 0x2B, 0x9E, 0xEA,
  29133. 0x35, 0x30, 0xC7, 0x66, 0x62, 0x33, 0x5C, 0xC7,
  29134. 0xEA, 0x3A, 0x00, 0x77, 0x77, 0x25, 0xEB, 0xCC,
  29135. 0xCD, 0x2A, 0x46, 0x36, 0xB2, 0xD9, 0x12, 0x2F,
  29136. 0xF3, 0xAB, 0x77, 0x12, 0x3C, 0xE0, 0x88, 0x3C,
  29137. 0x19, 0x11, 0x11, 0x5E, 0x50, 0xC9, 0xE8, 0xA9,
  29138. 0x41, 0x94, 0xE4, 0x8D, 0xD0, 0xD0, 0x9C, 0xFF,
  29139. 0xB3, 0xAD, 0xCD, 0x2C, 0x1E, 0x92, 0x43, 0x09,
  29140. 0x03, 0xD0, 0x7A, 0xDB, 0xF0, 0x05, 0x32, 0x03,
  29141. 0x15, 0x75, 0xAA, 0x7F, 0x9E, 0x7B, 0x5A, 0x1F,
  29142. 0x33, 0x62, 0xDE, 0xC9, 0x36, 0xD4, 0x04, 0x3C,
  29143. 0x05, 0xF2, 0x47, 0x6C, 0x07, 0x57, 0x8B, 0xC9,
  29144. 0xCB, 0xAF, 0x2A, 0xB4, 0xE3, 0x82, 0x72, 0x7A,
  29145. 0xD4, 0x16, 0x86, 0xA9, 0x6B, 0x25, 0x48, 0x82,
  29146. 0x0B, 0xB0, 0x3B, 0x32, 0xF1, 0x1B, 0x28, 0x11,
  29147. 0xAD, 0x62, 0xF4, 0x89, 0xE9, 0x51, 0x63, 0x2A,
  29148. 0xBA, 0x0D, 0x1D, 0xF8, 0x96, 0x80, 0xCC, 0x8A,
  29149. 0x8B, 0x53, 0xB4, 0x81, 0xD9, 0x2A, 0x68, 0xD7,
  29150. 0x0B, 0x4E, 0xA1, 0xC3, 0xA6, 0xA5, 0x61, 0xC0,
  29151. 0x69, 0x28, 0x82, 0xB5, 0xCA, 0x8C, 0xC9, 0x42,
  29152. 0xA8, 0xD4, 0x95, 0xAF, 0xCB, 0x06, 0xDE, 0x89,
  29153. 0x49, 0x8F, 0xB9, 0x35, 0xB7, 0x75, 0x90, 0x8F,
  29154. 0xE7, 0xA0, 0x3E, 0x32, 0x4D, 0x54, 0xCC, 0x19,
  29155. 0xD4, 0xE1, 0xAA, 0xBD, 0x35, 0x93, 0xB3, 0x8B,
  29156. 0x19, 0xEE, 0x13, 0x88, 0xFE, 0x49, 0x2B, 0x43,
  29157. 0x12, 0x7E, 0x5A, 0x50, 0x42, 0x53, 0x78, 0x6A,
  29158. 0x0D, 0x69, 0xAD, 0x32, 0x60, 0x1C, 0x28, 0xE2,
  29159. 0xC8, 0x85, 0x04, 0xA5, 0xBA, 0x59, 0x97, 0x06,
  29160. 0x02, 0x3A, 0x61, 0x36, 0x3E, 0x17, 0xC6, 0xB9,
  29161. 0xBB, 0x59, 0xBD, 0xC6, 0x97, 0x45, 0x2C, 0xD0,
  29162. 0x59, 0x45, 0x19, 0x83, 0xD7, 0x38, 0xCA, 0x3F,
  29163. 0xD0, 0x34, 0xE3, 0xF5, 0x98, 0x88, 0x54, 0xCA,
  29164. 0x05, 0x03, 0x1D, 0xB0, 0x96, 0x11, 0x49, 0x89,
  29165. 0x88, 0x19, 0x7C, 0x6B, 0x30, 0xD2, 0x58, 0xDF,
  29166. 0xE2, 0x62, 0x65, 0x54, 0x1C, 0x89, 0xA4, 0xB3,
  29167. 0x1D, 0x68, 0x64, 0xE9, 0x38, 0x9B, 0x03, 0xCB,
  29168. 0x74, 0xF7, 0xEC, 0x43, 0x23, 0xFB, 0x94, 0x21,
  29169. 0xA4, 0xB9, 0x79, 0x0A, 0x26, 0xD1, 0x7B, 0x03,
  29170. 0x98, 0xA2, 0x67, 0x67, 0x35, 0x09, 0x09, 0xF8,
  29171. 0x4D, 0x57, 0xB6, 0x69, 0x4D, 0xF8, 0x30, 0x66,
  29172. 0x4C, 0xA8, 0xB3, 0xC3, 0xC0, 0x3E, 0xD2, 0xAE,
  29173. 0x67, 0xB8, 0x90, 0x06, 0x86, 0x8A, 0x68, 0x52,
  29174. 0x7C, 0xCD, 0x66, 0x64, 0x59, 0xAB, 0x7F, 0x05,
  29175. 0x66, 0x71, 0x00, 0x0C, 0x61, 0x64, 0xD3, 0xA7,
  29176. 0xF2, 0x66, 0xA1, 0x4D, 0x97, 0xCB, 0xD7, 0x00,
  29177. 0x4D, 0x6C, 0x92, 0xCA, 0xCA, 0x77, 0x0B, 0x84,
  29178. 0x4A, 0x4F, 0xA9, 0xB1, 0x82, 0xE7, 0xB1, 0x8C,
  29179. 0xA8, 0x85, 0x08, 0x2A, 0xC5, 0x64, 0x6F, 0xCB,
  29180. 0x4A, 0x14, 0xE1, 0x68, 0x5F, 0xEB, 0x0C, 0x9C,
  29181. 0xE3, 0x37, 0x2A, 0xB9, 0x53, 0x65, 0xC0, 0x4F,
  29182. 0xD8, 0x30, 0x84, 0xF8, 0x0A, 0x23, 0xFF, 0x10,
  29183. 0xA0, 0x5B, 0xF1, 0x5F, 0x7F, 0xA5, 0xAC, 0xC6,
  29184. 0xC0, 0xCB, 0x46, 0x2C, 0x33, 0xCA, 0x52, 0x4F,
  29185. 0xA6, 0xB8, 0xBB, 0x35, 0x90, 0x43, 0xBA, 0x68,
  29186. 0x60, 0x9E, 0xAA, 0x25, 0x36, 0xE8, 0x1D, 0x08,
  29187. 0x46, 0x3B, 0x19, 0x65, 0x3B, 0x54, 0x35, 0xBA,
  29188. 0x94, 0x6C, 0x9A, 0xDD, 0xEB, 0x20, 0x2B, 0x04,
  29189. 0xB0, 0x31, 0xCC, 0x96, 0x0D, 0xCC, 0x12, 0xE4,
  29190. 0x51, 0x8D, 0x42, 0x8B, 0x32, 0xB2, 0x57, 0xA4,
  29191. 0xFC, 0x73, 0x13, 0xD3, 0xA7, 0x98, 0x0D, 0x80,
  29192. 0x08, 0x2E, 0x93, 0x4F, 0x9D, 0x95, 0xC3, 0x2B,
  29193. 0x0A, 0x01, 0x91, 0xA2, 0x36, 0x04, 0x38, 0x4D,
  29194. 0xD9, 0xE0, 0x79, 0xBB, 0xBA, 0xA2, 0x66, 0xD1,
  29195. 0x4C, 0x3F, 0x75, 0x6B, 0x9F, 0x21, 0x33, 0x10,
  29196. 0x74, 0x33, 0xA4, 0xE8, 0x3F, 0xA7, 0x18, 0x72,
  29197. 0x82, 0xA8, 0x09, 0x20, 0x3A, 0x4F, 0xAF, 0x84,
  29198. 0x18, 0x51, 0x83, 0x3D, 0x12, 0x1A, 0xC3, 0x83,
  29199. 0x84, 0x3A, 0x5E, 0x55, 0xBC, 0x23, 0x81, 0x42,
  29200. 0x5E, 0x16, 0xC7, 0xDB, 0x4C, 0xC9, 0xAB, 0x5C,
  29201. 0x1B, 0x0D, 0x91, 0xA4, 0x7E, 0x2B, 0x8D, 0xE0,
  29202. 0xE5, 0x82, 0xC8, 0x6B, 0x6B, 0x0D, 0x90, 0x7B,
  29203. 0xB3, 0x60, 0xB9, 0x7F, 0x40, 0xAB, 0x5D, 0x03,
  29204. 0x8F, 0x6B, 0x75, 0xC8, 0x14, 0xB2, 0x7D, 0x9B,
  29205. 0x96, 0x8D, 0x41, 0x98, 0x32, 0xBC, 0x8C, 0x2B,
  29206. 0xEE, 0x60, 0x5E, 0xF6, 0xE5, 0x05, 0x9D, 0x33,
  29207. 0x10, 0x0D, 0x90, 0x48, 0x5D, 0x37, 0x84, 0x50,
  29208. 0x01, 0x42, 0x21, 0x73, 0x6C, 0x07, 0x40, 0x7C,
  29209. 0xAC, 0x26, 0x04, 0x08, 0xAA, 0x64, 0x92, 0x66,
  29210. 0x19, 0x78, 0x8B, 0x86, 0x01, 0xC2, 0xA7, 0x52,
  29211. 0xD1, 0xA6, 0xCB, 0xF8, 0x20, 0xD7, 0xC7, 0xA0,
  29212. 0x47, 0x16, 0x20, 0x32, 0x25, 0xB3, 0x89, 0x5B,
  29213. 0x93, 0x42, 0xD1, 0x47, 0xA8, 0x18, 0x5C, 0xFC,
  29214. 0x1B, 0xB6, 0x5B, 0xA0, 0x6B, 0x41, 0x42, 0x33,
  29215. 0x99, 0x03, 0xC0, 0xAC, 0x46, 0x51, 0x38, 0x5B,
  29216. 0x45, 0xD9, 0x8A, 0x8B, 0x19, 0xD2, 0x8C, 0xD6,
  29217. 0xBA, 0xB0, 0x88, 0x78, 0x7F, 0x7E, 0xE1, 0xB1,
  29218. 0x24, 0x61, 0x76, 0x6B, 0x43, 0xCB, 0xCC, 0xB9,
  29219. 0x64, 0x34, 0x42, 0x7D, 0x93, 0xC0, 0x65, 0x55,
  29220. 0x06, 0x88, 0xF6, 0x94, 0x8E, 0xD1, 0xB5, 0x47,
  29221. 0x5A, 0x42, 0x5F, 0x1B, 0x85, 0x20, 0x9D, 0x06,
  29222. 0x1C, 0x08, 0xB5, 0x6C, 0x1C, 0xC0, 0x69, 0xF6,
  29223. 0xC0, 0xA7, 0xC6, 0xF2, 0x93, 0x58, 0xCA, 0xB9,
  29224. 0x11, 0x08, 0x77, 0x32, 0xA6, 0x49, 0xD2, 0x7C,
  29225. 0x9B, 0x98, 0xF9, 0xA4, 0x88, 0x79, 0x38, 0x7D,
  29226. 0x9B, 0x00, 0xC2, 0x59, 0x59, 0xA7, 0x16, 0x54,
  29227. 0xD6, 0xF6, 0xA9, 0x46, 0x16, 0x45, 0x13, 0xE4,
  29228. 0x7A, 0x75, 0xD0, 0x05, 0x98, 0x6C, 0x23, 0x63,
  29229. 0xC0, 0x9F, 0x6B, 0x53, 0x7E, 0xCA, 0x78, 0xB9,
  29230. 0x30, 0x3A, 0x5F, 0xA4, 0x57, 0x60, 0x8A, 0x58,
  29231. 0x6A, 0x65, 0x3A, 0x34, 0x7D, 0xB0, 0x4D, 0xFC,
  29232. 0xC1, 0x91, 0x75, 0xB3, 0xA3, 0x01, 0x17, 0x25,
  29233. 0x36, 0x06, 0x2A, 0x65, 0x8A, 0x95, 0x27, 0x75,
  29234. 0x70, 0xC8, 0x85, 0x2C, 0xA8, 0x97, 0x3F, 0x4A,
  29235. 0xE1, 0x23, 0xA3, 0x34, 0x04, 0x7D, 0xD7, 0x11,
  29236. 0xC8, 0x92, 0x7A, 0x63, 0x4A, 0x03, 0x38, 0x8A,
  29237. 0x52, 0x7B, 0x03, 0x4B, 0xF7, 0xA8, 0x17, 0x0F,
  29238. 0xA7, 0x02, 0xC1, 0xF7, 0xC2, 0x3E, 0xC3, 0x2D,
  29239. 0x18, 0xA2, 0x37, 0x48, 0x90, 0xBE, 0x9C, 0x78,
  29240. 0x7A, 0x94, 0x09, 0xC8, 0x2D, 0x19, 0x2C, 0x4B,
  29241. 0xB7, 0x05, 0xA2, 0xF9, 0x96, 0xCE, 0x40, 0x5D,
  29242. 0x85, 0xA4, 0xC1, 0xA1, 0xAB, 0x9B, 0x6A, 0xEB,
  29243. 0x49, 0xCC, 0xE1, 0xC2, 0xF8, 0xA9, 0x7C, 0x35,
  29244. 0x16, 0xC7, 0x2A, 0x00, 0xA4, 0x62, 0x63, 0xBA,
  29245. 0xA6, 0x96, 0xBF, 0x25, 0x72, 0x77, 0x19, 0xC3,
  29246. 0x21, 0x64, 0x23, 0x61, 0x8F, 0xF3, 0x33, 0x80,
  29247. 0x93, 0x4A, 0x6C, 0x10, 0x54, 0x5C, 0x4C, 0x5C,
  29248. 0x51, 0x55, 0xB1, 0x24, 0x86, 0x18, 0x1F, 0xC7,
  29249. 0xA2, 0x31, 0x98, 0x73, 0x97, 0x8B, 0x6A, 0x2A,
  29250. 0x67, 0x49, 0x0F, 0x82, 0x56, 0xBD, 0x21, 0x96,
  29251. 0xFE, 0x17, 0x92, 0xA4, 0xC0, 0x00, 0x77, 0xB8,
  29252. 0x12, 0xEA, 0xE8, 0xBE, 0xD3, 0x57, 0x24, 0x99,
  29253. 0x68, 0x4A, 0xB3, 0x37, 0x18, 0x76, 0x76, 0x1E,
  29254. 0x45, 0x0C, 0x9F, 0x9D, 0x27, 0x68, 0xA3, 0x68,
  29255. 0x06, 0xD7, 0xAB, 0x20, 0x46, 0xC9, 0x1F, 0x17,
  29256. 0x59, 0x9E, 0x9A, 0xC5, 0x92, 0x99, 0x08, 0x08,
  29257. 0xDC, 0xD7, 0xB4, 0xD0, 0x91, 0x90, 0x72, 0xF1,
  29258. 0x4E, 0xC3, 0x61, 0x77, 0x3B, 0x72, 0x52, 0x44,
  29259. 0x4C, 0x32, 0x3C, 0x30, 0x83, 0x26, 0xF4, 0xA3,
  29260. 0x0F, 0x86, 0x80, 0xD2, 0xF7, 0x48, 0xF5, 0x6A,
  29261. 0x13, 0x2B, 0x82, 0x67, 0x4E, 0xD0, 0x18, 0x46,
  29262. 0x20, 0xB8, 0x2A, 0xD2, 0xCB, 0x18, 0x2C, 0x97,
  29263. 0xB4, 0x81, 0x62, 0x66, 0x47, 0x49, 0x12, 0x90,
  29264. 0xA0, 0x11, 0xCC, 0x73, 0x82, 0x86, 0x85, 0xA8,
  29265. 0xC3, 0x67, 0xA5, 0xB9, 0xCF, 0x8D, 0x62, 0x1B,
  29266. 0x0D, 0x5C, 0x1E, 0xFF, 0x03, 0x17, 0x27, 0x58,
  29267. 0xBD, 0x00, 0x49, 0x78, 0xC2, 0x51, 0xCD, 0x51,
  29268. 0x34, 0x22, 0x28, 0x98, 0x9C, 0xAE, 0x63, 0x32,
  29269. 0xAC, 0x48, 0x64, 0x37, 0xCB, 0x5C, 0x57, 0xD4,
  29270. 0x30, 0x74, 0x62, 0x86, 0x52, 0x53, 0xBE, 0x21,
  29271. 0x7B, 0x35, 0x15, 0xC7, 0x3D, 0xF4, 0x05, 0xB7,
  29272. 0xF2, 0x82, 0x17, 0xAD, 0x0B, 0x8C, 0xF6, 0x0C,
  29273. 0x2F, 0xFF, 0xAA, 0x0A, 0x00, 0x48, 0xB1, 0xFB,
  29274. 0x4A, 0xCD, 0xCD, 0xC3, 0x8B, 0x52, 0x50, 0xCF,
  29275. 0xEC, 0x35, 0x6A, 0x6D, 0xE2, 0x6C, 0xFA, 0x7A,
  29276. 0x58, 0x8F, 0xDC, 0x86, 0xF9, 0x8C, 0x85, 0x4A,
  29277. 0xC6, 0x4C, 0x7B, 0xFA, 0xA9, 0x6F, 0x5A, 0x32,
  29278. 0xCC, 0x06, 0x10, 0x93, 0x4B, 0xAA, 0x6A, 0x58,
  29279. 0x6B, 0x9A, 0x20, 0x54, 0xF1, 0x3B, 0xA2, 0x74,
  29280. 0x17, 0x4A, 0xA0, 0xD2, 0xB3, 0xA8, 0x1B, 0x96,
  29281. 0xA9, 0x40, 0x66, 0x6F, 0x78, 0x9B, 0x5A, 0x6B,
  29282. 0xCD, 0xC0, 0xA6, 0xA0, 0x17, 0x8A, 0x0C, 0x9A,
  29283. 0x02, 0x57, 0x8A, 0x49, 0x3F, 0x6E, 0xEA, 0x0D,
  29284. 0x2E, 0x6C, 0x13, 0x95, 0x1C, 0x9F, 0x24, 0x9A,
  29285. 0x5E, 0x8D, 0xD7, 0x1D, 0xD4, 0x9A, 0x74, 0x2D,
  29286. 0x45, 0x1F, 0x1A, 0xBB, 0xA1, 0x9A, 0xF8, 0xC5,
  29287. 0x47, 0x85, 0x5E, 0x0A, 0xFC, 0x72, 0x8E, 0x90,
  29288. 0xAB, 0xB4, 0x99, 0xC9, 0xBE, 0xEB, 0x76, 0x6F,
  29289. 0x47, 0x29, 0xCD, 0xA2, 0x22, 0x63, 0xE3, 0x24,
  29290. 0xD2, 0x23, 0x02, 0xCB, 0xD3, 0x39, 0x9F, 0xAC,
  29291. 0xC6, 0x30, 0x99, 0x1F, 0xC8, 0xF2, 0x8B, 0xDB,
  29292. 0x43, 0x54, 0x76, 0x25, 0x41, 0x52, 0x76, 0x78,
  29293. 0xBC, 0xF6, 0x1F, 0x65, 0xC2, 0x41, 0x14, 0x6C,
  29294. 0x42, 0x6D, 0x23, 0xB9, 0xBF, 0xAA, 0x6B, 0x7D,
  29295. 0xF1, 0x8C, 0x97, 0xF2, 0x0C, 0x1B, 0x61, 0x25,
  29296. 0xBF, 0x87, 0x4B, 0x1D, 0x89, 0x47, 0x58, 0x52,
  29297. 0xC4, 0x48, 0x21, 0x5D, 0xB0, 0xEB, 0x77, 0x37,
  29298. 0xF9, 0x14, 0x80, 0xE8, 0xCE, 0xBD, 0x9A, 0x08,
  29299. 0x71, 0x57, 0x4F, 0x5A, 0xB6, 0x2D, 0x90, 0x20,
  29300. 0x17, 0x5E, 0xC6, 0x92, 0x7C, 0xA0, 0xB5, 0x4C,
  29301. 0x09, 0x81, 0x8E, 0x42, 0xCF, 0x92, 0xA3, 0x83,
  29302. 0x17, 0x24, 0x22, 0xC7, 0xDC, 0x18, 0x31, 0xD6,
  29303. 0x3B, 0x0C, 0x29, 0x5D, 0xE7, 0x51, 0x59, 0xDB,
  29304. 0x80, 0x34, 0xE9, 0xE0, 0x7F, 0x7B, 0x0B, 0x91,
  29305. 0x0C, 0x3C, 0x1E, 0x5F, 0xB6, 0x6B, 0x3D, 0xC5,
  29306. 0x23, 0xF1, 0xFA, 0x6E, 0xB4, 0x91, 0x0C, 0xB8,
  29307. 0x9A, 0x6C, 0x17, 0x56, 0x2C, 0x83, 0xAB, 0x4C,
  29308. 0x18, 0xD0, 0xCD, 0x7E, 0x07, 0x96, 0x59, 0x2A,
  29309. 0x37, 0x2A, 0xA4, 0x09, 0xB1, 0xC5, 0x57, 0x34,
  29310. 0x7C, 0xCA, 0xCD, 0xC4, 0x64, 0x4A, 0x11, 0x90,
  29311. 0x64, 0xD0, 0x6D, 0xD4, 0x74, 0x92, 0x9D, 0x1C,
  29312. 0x6F, 0xB4, 0xD6, 0x86, 0xE5, 0x49, 0x1C, 0xE4,
  29313. 0xBC, 0x89, 0xA3, 0x0B, 0xB4, 0xB8, 0xC4, 0x1B,
  29314. 0xCE, 0x51, 0x57, 0xDF, 0xC1, 0x36, 0x08, 0x23,
  29315. 0xB1, 0xAB, 0x61, 0x8C, 0x14, 0xB1, 0x0F, 0x98,
  29316. 0xC2, 0x50, 0x67, 0x39, 0x8E, 0xA7, 0x01, 0x8C,
  29317. 0x27, 0x8A, 0x4B, 0x3D, 0xF3, 0x13, 0x34, 0xD6,
  29318. 0x03, 0xB2, 0x04, 0x4E, 0xF1, 0x87, 0xCD, 0x9B,
  29319. 0xC6, 0xCE, 0x42, 0x72, 0x5B, 0xD9, 0x62, 0xC2,
  29320. 0x64, 0x98, 0x3E, 0x9E, 0x18, 0x15, 0x5A, 0x8B,
  29321. 0x9C, 0x47, 0x14, 0x3D, 0x70, 0x46, 0x0A, 0x26,
  29322. 0xA5, 0x6F, 0xE7, 0x65, 0x8C, 0x1F, 0x15, 0x03,
  29323. 0x48, 0xC6, 0x08, 0x7E, 0xF7, 0x58, 0xAD, 0x16,
  29324. 0x78, 0x87, 0x86, 0x0A, 0x00, 0x7A, 0x5F, 0xC3,
  29325. 0x73, 0x58, 0xD4, 0x3B, 0x5E, 0xBE, 0xE8, 0x20,
  29326. 0xAC, 0xEA, 0x47, 0x4F, 0x0A, 0xC0, 0x7B, 0x76,
  29327. 0x80, 0x28, 0x66, 0x19, 0x9C, 0x61, 0x23, 0x1D,
  29328. 0x5C, 0x74, 0x7C, 0x93, 0x77, 0x4D, 0x2C, 0x1E,
  29329. 0x0C, 0x1C, 0x67, 0xE6, 0xC8, 0x1B, 0x82, 0x75,
  29330. 0x21, 0x73, 0xE1, 0x25, 0xBA, 0xF3, 0x9B, 0x4F,
  29331. 0xD1, 0x9A, 0x4F, 0x45, 0x3D, 0xC5, 0x79, 0x76,
  29332. 0xB1, 0xD9, 0x7F, 0xE6, 0x99, 0x69, 0x92, 0xBB,
  29333. 0xB6, 0x5B, 0x7C, 0xB2, 0x5D, 0x07, 0x7B, 0xBA,
  29334. 0xA6, 0xA1, 0x33, 0x22, 0x89, 0x9A, 0xF6, 0x59,
  29335. 0xCF, 0x1B, 0x35, 0x58, 0xC1, 0xB5, 0x00, 0x11,
  29336. 0x54, 0xB6, 0x25, 0x80, 0x9E, 0xD8, 0x9A, 0xEE,
  29337. 0xBB, 0x89, 0xE6, 0xEA, 0x7D, 0x67, 0xF7, 0x23,
  29338. 0xD0, 0x45, 0xAB, 0x05, 0x71, 0x5C, 0x42, 0x35,
  29339. 0x5D, 0xA6, 0xA5, 0xC8, 0xDD, 0x39, 0xC8, 0xAB,
  29340. 0xE3, 0x03, 0x77, 0x51, 0xA0, 0x1E, 0xD1, 0xC7,
  29341. 0x37, 0x49, 0x19, 0xF3, 0x12, 0x1B, 0x5A, 0x52,
  29342. 0xC5, 0x3D, 0x14, 0x87, 0x31, 0x67, 0x69, 0xF8,
  29343. 0x07, 0x21, 0xDE, 0xEA, 0xAA, 0xD3, 0xC9, 0x0F,
  29344. 0x76, 0xE7, 0xAE, 0x9E, 0x12, 0xBA, 0x92, 0xB3,
  29345. 0x2B, 0x5F, 0xD4, 0x57, 0xE3, 0xC7, 0x52, 0xC2,
  29346. 0x65, 0x0D, 0xFB, 0x88, 0x57, 0x71, 0xCB, 0x77,
  29347. 0xAC, 0x3C, 0x78, 0x5A, 0x8C, 0x56, 0x2E, 0x6A,
  29348. 0x1C, 0x63, 0xC2, 0xA5, 0x5E, 0xA4, 0x7C, 0xF8,
  29349. 0xB9, 0x0E, 0xB8, 0x22, 0x5C, 0x12, 0x3C, 0x34,
  29350. 0x64, 0x52, 0x56, 0x62, 0x35, 0xB2, 0xF3, 0x18,
  29351. 0x23, 0xA3, 0x35, 0x21, 0xE0, 0x87, 0x93, 0x7A,
  29352. 0x34, 0x5D, 0x8D, 0x66, 0x3E, 0xEA, 0xA0, 0x56,
  29353. 0x58, 0x91, 0x7B, 0xBA, 0xA0, 0x08, 0xC2, 0xE3,
  29354. 0x35, 0xF8, 0x85, 0x0A, 0x90, 0xA3, 0x26, 0xD0,
  29355. 0xE6, 0x64, 0x32, 0xF4, 0x4C, 0xEB, 0x82, 0x89,
  29356. 0xE4, 0xEC, 0xB2, 0xD1, 0x29, 0x58, 0xE9, 0x84,
  29357. 0x07, 0x2E, 0xCA, 0xCB, 0x88, 0xE1, 0x34, 0x8F,
  29358. 0xF0, 0xB5, 0x56, 0x54, 0xAC, 0xBA, 0x5B, 0x54,
  29359. 0x97, 0x1C, 0xBA, 0xEB, 0xA8, 0x8E, 0xC4, 0xB9,
  29360. 0x1A, 0x94, 0xC3, 0x71, 0x92, 0xFA, 0x98, 0x2B,
  29361. 0xEC, 0xB9, 0xF3, 0xDA, 0x42, 0x16, 0x03, 0xB6,
  29362. 0x1A, 0x51, 0xBC, 0x8E, 0x36, 0xCB, 0xD0, 0x53,
  29363. 0x85, 0x1C, 0x77, 0xB1, 0xB9, 0x26, 0xB1, 0x7A,
  29364. 0x27, 0x2A, 0xA9, 0x02, 0x32, 0x46, 0xB0, 0x2B,
  29365. 0x3E, 0xD4, 0x7F, 0x66, 0xA0, 0x0B, 0xD5, 0x68,
  29366. 0x48, 0x23, 0x63, 0x4E, 0x7C, 0xE5, 0x8C, 0xF8,
  29367. 0xF3, 0x06, 0xE3, 0x5B, 0x1E, 0x53, 0x22, 0x82,
  29368. 0x4D, 0x90, 0x48, 0x01, 0xF0, 0xA2, 0xFA, 0x7C,
  29369. 0x2B, 0xC9, 0xC2, 0x52, 0xB0, 0xA5, 0x6B, 0x7B,
  29370. 0xA2, 0xAB, 0x0F, 0x63, 0x60, 0x21, 0x74, 0x5A,
  29371. 0x70, 0xA9, 0xA4, 0x3E, 0x2B, 0x0A, 0x8D, 0x61,
  29372. 0x59, 0x70, 0xB6, 0x53, 0x09, 0x62, 0x4B, 0x51,
  29373. 0x84, 0xBC, 0xC3, 0x0B, 0x91, 0x16, 0x79, 0xAE,
  29374. 0xDD, 0x76, 0x02, 0x5F, 0xE3, 0x90, 0x8F, 0xD6,
  29375. 0x78, 0x97, 0xB0, 0xCF, 0x4B, 0xE5, 0xA6, 0xF5,
  29376. 0x41, 0x3D, 0x7D, 0xD9, 0x85, 0x64, 0xB2, 0x3E,
  29377. 0x42, 0xA9, 0x3E, 0x4A, 0xA8, 0x82, 0x1C, 0xD4,
  29378. 0x50, 0x54, 0xC6, 0x43, 0xED, 0xC1, 0x15, 0x8D,
  29379. 0xB6, 0xB3, 0xDE, 0xB1, 0x3F, 0xB5, 0xA5, 0x1E,
  29380. 0xBD, 0x1A, 0x8A, 0x78, 0xB8, 0x72, 0x25, 0xA7,
  29381. 0x33, 0x8E, 0x10, 0x11, 0x04, 0xC4, 0xA2, 0x20,
  29382. 0xD9, 0xBD, 0xED, 0xD4, 0x8C, 0x85, 0xA1, 0xC2,
  29383. 0xDA, 0xE7, 0x81, 0xA8, 0x0C, 0x40, 0xE1, 0x3B,
  29384. 0x87, 0xEA, 0xC7, 0x3A, 0x76, 0x42, 0x01, 0xC9,
  29385. 0xB7, 0x60, 0xCC, 0xFB, 0x1A, 0xE3, 0x92, 0x69,
  29386. 0x9C, 0x70, 0x39, 0xD2, 0x7C, 0x39, 0x36, 0x2B,
  29387. 0x27, 0xB8, 0xFC, 0x6F, 0x07, 0xA8, 0xA3, 0xD4,
  29388. 0x41, 0x0F, 0x15, 0x47, 0xC4, 0x8A, 0x99, 0x97,
  29389. 0xF6, 0x2C, 0x61, 0x07, 0x44, 0x52, 0xEF, 0x15,
  29390. 0x15, 0xF8, 0xA6, 0x49, 0xEB, 0xCA, 0x94, 0x37,
  29391. 0x20, 0x5A, 0x4E, 0x8A, 0x61, 0x60, 0x6B, 0x41,
  29392. 0xDA, 0xF6, 0x83, 0x4D, 0x67, 0x1F, 0x4D, 0x85,
  29393. 0x2C, 0x0C, 0x9C, 0x40, 0x96, 0x61, 0x16, 0x48,
  29394. 0xC6, 0xA3, 0x17, 0x06, 0x78, 0xB1, 0x53, 0x7C,
  29395. 0xC1, 0x82, 0x8D, 0x93, 0x58, 0x0C, 0x9E, 0x58,
  29396. 0x49, 0xA9, 0x65, 0x31, 0x75, 0xAC, 0xB7, 0x53,
  29397. 0xF2, 0xBE, 0x74, 0x37, 0xBE, 0x45, 0xF6, 0xC6,
  29398. 0x03, 0xE4, 0x85, 0xF2, 0xEC, 0x30, 0x1B, 0xB4,
  29399. 0x2B, 0x6C, 0x37, 0xC2, 0x25, 0xD7, 0x49, 0x5A,
  29400. 0x58, 0x4A, 0xE2, 0x31, 0x89, 0x0A, 0xB5, 0xC8,
  29401. 0xC3, 0x5C, 0x26, 0x8C, 0xF4, 0xBB, 0xB0, 0x21,
  29402. 0x3C, 0x09, 0x60, 0x19, 0x31, 0x95, 0x61, 0xA8,
  29403. 0xA6, 0x94, 0x76, 0x37, 0xAA, 0x40, 0xD0, 0x06,
  29404. 0xB4, 0x15, 0xBB, 0x2C, 0xFA, 0x22, 0x37, 0xE0,
  29405. 0x89, 0x0B, 0x6A, 0x3B, 0xC1, 0x34, 0xAB, 0xF8,
  29406. 0xF6, 0x58, 0x5E, 0x10, 0x8D, 0x15, 0x94, 0x0F,
  29407. 0x91, 0xF4, 0xBF, 0x5B, 0x0C, 0x81, 0x80, 0x55,
  29408. 0xB2, 0x1D, 0xEA, 0x6E, 0x63, 0xB5, 0x53, 0x98,
  29409. 0x8C, 0x47, 0xF4, 0xB9, 0x4E, 0x7C, 0xF8, 0x00,
  29410. 0xA4, 0x93, 0xB4, 0x73, 0x47, 0x05, 0xED, 0xC5,
  29411. 0x6A, 0x4B, 0x60, 0x21, 0xC6, 0x29, 0x50, 0x06,
  29412. 0x75, 0x87, 0x68, 0x04, 0xCF, 0x0B, 0x95, 0x1F,
  29413. 0x03, 0x8A, 0x5C, 0x7F, 0xE5, 0x8E, 0x89, 0x77,
  29414. 0x4E, 0xF2, 0x99, 0x2F, 0xD7, 0xC6, 0x30, 0x99,
  29415. 0xD3, 0x52, 0xA7, 0xD2, 0x15, 0x60, 0xB7, 0x88,
  29416. 0xB4, 0x05, 0x70, 0x98, 0x61, 0x81, 0x7E, 0x59,
  29417. 0xA9, 0x6B, 0x3A, 0x3A, 0x83, 0xCB, 0xA8, 0x03,
  29418. 0xB1, 0x69, 0x34, 0x33, 0x10, 0x71, 0x90, 0x5B,
  29419. 0xBE, 0xC6, 0x53, 0x29, 0x00, 0x15, 0x5D, 0x8A,
  29420. 0xC8, 0x8C, 0xB3, 0x2E, 0x4E, 0x21, 0xA3, 0xBD,
  29421. 0x3A, 0x03, 0xFD, 0xEC, 0x32, 0x5A, 0x51, 0xCD,
  29422. 0x27, 0x73, 0x96, 0x4E, 0x67, 0x84, 0xFC, 0xF1,
  29423. 0x85, 0x37, 0x37, 0xAA, 0x64, 0xEB, 0x67, 0x56,
  29424. 0x47, 0x27, 0x27, 0x26, 0x61, 0xAB, 0xF8, 0x43,
  29425. 0x13, 0xA5, 0x7A, 0x44, 0xB1, 0x23, 0xC6, 0x55,
  29426. 0x09, 0xCF, 0xB7, 0xA6, 0xF6, 0x64, 0x1C, 0xDC,
  29427. 0xC3, 0xB5, 0x7F, 0xE6, 0x28, 0xC7, 0xB8, 0x19,
  29428. 0x2D, 0xB4, 0x4F, 0xFB, 0xF5, 0x79, 0x6A, 0x86,
  29429. 0x13, 0xB1, 0xFA, 0x12, 0x6F, 0x60, 0x76, 0x88,
  29430. 0x3C, 0x78, 0x3D, 0xC2, 0x4E, 0x2A, 0x44, 0x64,
  29431. 0xC4, 0x0B, 0x3A, 0x41, 0xCA, 0x70, 0xAE, 0x87,
  29432. 0x62, 0x08, 0x66, 0xCF, 0x4F, 0xCB, 0x2B, 0xD2,
  29433. 0x04, 0xBF, 0x5C, 0x28, 0x38, 0x12, 0xBA, 0x05,
  29434. 0x6A, 0xC0, 0xC3, 0x45, 0xE3, 0x79, 0xC4, 0xBA,
  29435. 0x24, 0xD7, 0x50, 0x90, 0x12, 0x79, 0xBB, 0x2F,
  29436. 0x3A, 0x16, 0xF6, 0x12, 0xBF, 0xAD, 0xB3, 0x57,
  29437. 0x03, 0x33, 0x2C, 0x7C, 0x13, 0x6F, 0x68, 0xEA,
  29438. 0xB6, 0x75, 0x5C, 0x66, 0xB6, 0xA4, 0xAD, 0x1A,
  29439. 0xAB, 0xA7, 0xB7, 0x68, 0xA5, 0x8A, 0xCA, 0xAC,
  29440. 0xC1, 0x0A, 0x45, 0x9A, 0x1C, 0xC8, 0xEF, 0x29,
  29441. 0x37, 0x7B, 0xC2, 0x00, 0xE4, 0xD3, 0x15, 0xA3,
  29442. 0x0A, 0x6B, 0xCC, 0x32, 0x56, 0xF9, 0x73, 0x4D,
  29443. 0x06, 0xE9, 0x77, 0x9C, 0xAA, 0x54, 0x42, 0xA9,
  29444. 0xA1, 0x60, 0x69, 0x08, 0x13, 0x77, 0xC7, 0x6E,
  29445. 0x75, 0x15, 0x43, 0x68, 0x07, 0x2D, 0xC4, 0x46,
  29446. 0xED, 0x6C, 0x8B, 0x8E, 0x62, 0x2A, 0x21, 0xE3,
  29447. 0x83, 0xCF, 0x9B, 0xA1, 0xFB, 0x43, 0x4E, 0x2E,
  29448. 0xCC, 0x81, 0xE7, 0xB7, 0x8C, 0xEE, 0x98, 0x6B,
  29449. 0x8F, 0xF7, 0x98, 0xAB, 0x18, 0xCF, 0x96, 0x34,
  29450. 0x54, 0x35, 0x46, 0x28, 0x4E, 0xDA, 0x2A, 0x26,
  29451. 0xB4, 0x7F, 0x05, 0xB7, 0x35, 0xBC, 0xDB, 0x12,
  29452. 0x02, 0x22, 0x00, 0x76, 0xDC, 0x8B, 0x4E, 0x4B,
  29453. 0x9F, 0x85, 0x35, 0x33, 0xC8, 0xF6, 0xC7, 0xFF,
  29454. 0x38, 0x81, 0x7B, 0xA4, 0x97, 0x12, 0x83, 0x57,
  29455. 0x85, 0xF1, 0x7F, 0x14, 0xCA, 0x01, 0xD0, 0xC1,
  29456. 0xC1, 0xE9, 0x88, 0x10, 0xFE, 0x0B, 0x36, 0xE5,
  29457. 0xB4, 0x27, 0x15, 0x7B, 0x94, 0x18, 0x44, 0x9C,
  29458. 0xED, 0xD6, 0x41, 0xA4, 0x29, 0x3C, 0x85, 0xC3,
  29459. 0x27, 0x00, 0x10, 0x2A, 0xCE, 0xC2, 0x2E, 0xBA,
  29460. 0xD9, 0x8E, 0xD1, 0x60, 0xA5, 0xF0, 0x27, 0xBD,
  29461. 0x4C, 0xDA, 0x57, 0xF1, 0xF3, 0x72, 0x0A, 0x12,
  29462. 0xC1, 0x34, 0x65, 0x4D, 0xD5, 0xE7, 0x3F, 0x82,
  29463. 0x96, 0x76, 0x49, 0x53, 0x90, 0xD0, 0xE7, 0x92,
  29464. 0x9D, 0x60, 0x34, 0xE9, 0xC5, 0x5F, 0x7D, 0x55,
  29465. 0xBA, 0x65, 0x8B, 0xC5, 0x87, 0x98, 0x8E, 0x8A,
  29466. 0xF9, 0x49, 0x60, 0xF6, 0xCF, 0xB8, 0xD5, 0xAF,
  29467. 0x7A, 0x00, 0x21, 0x53, 0x5A, 0x6E, 0x25, 0xE4,
  29468. 0x37, 0xD4, 0x9A, 0x78, 0x06, 0x98, 0xBE, 0x22,
  29469. 0xAC, 0x99, 0x53, 0x94, 0x9F, 0x57, 0x1B, 0x85,
  29470. 0xA6, 0x85, 0x72, 0x5F, 0x82, 0x07, 0xA2, 0xB0,
  29471. 0xAE, 0x84, 0x9B, 0x60, 0x1A, 0xB9, 0x1B, 0x15,
  29472. 0x9B, 0x3D, 0xF4, 0xA1, 0x54, 0xC2, 0x04, 0x1E,
  29473. 0x77, 0x60, 0x70, 0xAF, 0xC4, 0x29, 0x69, 0x32,
  29474. 0x23, 0x80, 0x91, 0x7C, 0x97, 0x51, 0x07, 0x99,
  29475. 0xF3, 0x14, 0x91, 0x31, 0x47, 0x7E, 0x16, 0x66,
  29476. 0x3D, 0x31, 0x74, 0xC7, 0xC1, 0xCA, 0xEA, 0x78,
  29477. 0x85, 0x35, 0xC6, 0xC0, 0x05, 0xA6, 0x4F, 0x28,
  29478. 0x68, 0x63, 0x1B, 0x31, 0xB6, 0x6E, 0x20, 0x5F,
  29479. 0xD3, 0x8C, 0x1D, 0x84, 0x54, 0x2D, 0x0F, 0x1B,
  29480. 0x57, 0x8F, 0x58, 0xC9, 0xBF, 0x5A, 0x0F, 0xAE,
  29481. 0xAB, 0x6A, 0xB6, 0x49, 0x48, 0x93, 0x05, 0x31,
  29482. 0x65, 0xEA, 0xFD, 0x46, 0x5F, 0xC6, 0x4A, 0x0C,
  29483. 0x5F, 0x8F, 0x3F, 0x90, 0x03, 0x48, 0x94, 0x15,
  29484. 0x89, 0x9D, 0x59, 0xA5, 0x43, 0xD8, 0x20, 0x8C,
  29485. 0x54, 0xA3, 0x16, 0x65, 0x29, 0xB5, 0x39, 0x22,
  29486. 0x8A, 0x39, 0xE8, 0x7D, 0x53, 0x1F, 0x35, 0x27,
  29487. 0xC2, 0x07, 0xED, 0xCC, 0x1D, 0xB7, 0xFA, 0xDD,
  29488. 0xCF, 0x96, 0x28, 0x39, 0x18, 0x79, 0xB3, 0x35,
  29489. 0xC7, 0x07, 0x83, 0x9A, 0x0D, 0xB0, 0x51, 0xA8,
  29490. 0x86, 0x26, 0xED, 0x79, 0xD4, 0x51, 0x14, 0x08,
  29491. 0x00, 0xE0, 0x3B, 0x59, 0xB9, 0x56, 0xF8, 0x21,
  29492. 0x0E, 0x55, 0x60, 0x67, 0x40, 0x7D, 0x13, 0xDC,
  29493. 0x90, 0xFA, 0x9E, 0x8B, 0x87, 0x2B, 0xFB, 0x8F
  29494. };
  29495. const byte kyber1024_ct[] = {
  29496. 0xA6, 0xAF, 0x29, 0xD5, 0xF5, 0xB8, 0x0B, 0xD1,
  29497. 0x30, 0xF5, 0x18, 0xBA, 0xDD, 0xD6, 0xC8, 0xF1,
  29498. 0x75, 0x45, 0x41, 0x3D, 0x86, 0x0F, 0xB3, 0xDE,
  29499. 0x45, 0x19, 0x79, 0xEB, 0xFA, 0x5E, 0x4E, 0x31,
  29500. 0x12, 0xC7, 0xC0, 0xAD, 0xF9, 0x98, 0x24, 0xBB,
  29501. 0x52, 0x6F, 0x2C, 0x35, 0x50, 0x74, 0x8E, 0xD0,
  29502. 0xE1, 0x34, 0xF0, 0x45, 0x7A, 0x7C, 0x61, 0xF9,
  29503. 0xF5, 0x26, 0xF0, 0x02, 0xBA, 0xAD, 0xC0, 0x3F,
  29504. 0xC1, 0x3E, 0x38, 0x13, 0x12, 0x19, 0x51, 0x3C,
  29505. 0x3E, 0xDE, 0x06, 0x16, 0x61, 0xE7, 0x4F, 0x60,
  29506. 0x3C, 0x4F, 0xCF, 0x79, 0x51, 0xC8, 0xE5, 0x2C,
  29507. 0x9C, 0x21, 0x3B, 0x0D, 0x22, 0xD9, 0x29, 0x36,
  29508. 0x63, 0xD6, 0x69, 0xA6, 0xB5, 0x8E, 0xD8, 0xFC,
  29509. 0xEF, 0xCF, 0x82, 0x49, 0xD7, 0xBB, 0x52, 0x98,
  29510. 0xF5, 0x57, 0x61, 0x44, 0x5B, 0x2B, 0x83, 0xCE,
  29511. 0x7F, 0x00, 0x5C, 0xB0, 0x42, 0x48, 0xAE, 0xC8,
  29512. 0xBD, 0xA2, 0x2F, 0xD2, 0xD4, 0x2A, 0xA7, 0x66,
  29513. 0x32, 0x20, 0x14, 0xEA, 0x03, 0x8C, 0xC3, 0x2C,
  29514. 0x55, 0xC8, 0xE4, 0xB9, 0xE2, 0x8E, 0xC9, 0x11,
  29515. 0x9F, 0x52, 0x73, 0x41, 0xE4, 0xF6, 0x6A, 0x03,
  29516. 0x51, 0x21, 0x07, 0x3B, 0x85, 0xDE, 0x67, 0x06,
  29517. 0xDA, 0x19, 0xE0, 0x83, 0x8A, 0x9F, 0x33, 0xB7,
  29518. 0x19, 0xA6, 0x8F, 0x03, 0x9B, 0x66, 0x4D, 0xC0,
  29519. 0x02, 0x65, 0x9E, 0xAB, 0xFC, 0x39, 0x86, 0x79,
  29520. 0xAA, 0x70, 0x09, 0xCE, 0x0C, 0xD0, 0x1C, 0xDA,
  29521. 0xFB, 0x6C, 0xD2, 0xA2, 0x6F, 0xE4, 0x10, 0x16,
  29522. 0x72, 0xC9, 0x8F, 0xF5, 0x8F, 0x7C, 0x47, 0xD5,
  29523. 0xBD, 0xA2, 0x90, 0x66, 0x53, 0xB3, 0xA6, 0xF9,
  29524. 0x65, 0x1F, 0x7A, 0x12, 0x1E, 0xA7, 0x7E, 0xA7,
  29525. 0x47, 0x23, 0xFA, 0xE5, 0xB8, 0x73, 0xF9, 0xBB,
  29526. 0x7B, 0x66, 0x4F, 0x0C, 0x8A, 0x93, 0x83, 0x1E,
  29527. 0xF9, 0xD5, 0x1C, 0x7C, 0xC1, 0xEF, 0x44, 0xAC,
  29528. 0x0E, 0x55, 0xA5, 0x5C, 0xA7, 0x6D, 0x13, 0x7F,
  29529. 0xE9, 0xB7, 0x5F, 0x40, 0x50, 0x9C, 0xEF, 0x15,
  29530. 0x6E, 0x5A, 0xD1, 0x8F, 0x9F, 0xB9, 0x99, 0x68,
  29531. 0x00, 0x08, 0xE5, 0x47, 0xD5, 0x5E, 0xEC, 0xD5,
  29532. 0xB4, 0xD1, 0xCB, 0x1D, 0x9F, 0x07, 0x6C, 0xEC,
  29533. 0x21, 0x50, 0x1C, 0x74, 0x02, 0x50, 0x9E, 0xCB,
  29534. 0x77, 0xAF, 0xB2, 0xCB, 0x9A, 0x61, 0x34, 0x0A,
  29535. 0x8B, 0xD1, 0x51, 0x4C, 0x6E, 0x71, 0xB4, 0xAA,
  29536. 0x45, 0xE4, 0x7E, 0xC3, 0x75, 0x12, 0x27, 0x1B,
  29537. 0x91, 0x1F, 0x8F, 0xB4, 0x6C, 0x90, 0x82, 0xC9,
  29538. 0xDF, 0x07, 0x20, 0x4A, 0xBB, 0x5A, 0x50, 0xE6,
  29539. 0xE3, 0x64, 0x7A, 0x8A, 0xD4, 0xD8, 0xD5, 0xD7,
  29540. 0xBF, 0xF1, 0x9C, 0x8A, 0x50, 0x93, 0x08, 0xBC,
  29541. 0xFB, 0x89, 0x55, 0x36, 0xD0, 0x45, 0xCA, 0x2B,
  29542. 0x97, 0xCB, 0x16, 0xA2, 0x9B, 0xB7, 0x18, 0x1C,
  29543. 0xAD, 0x05, 0x09, 0xDD, 0xB9, 0x17, 0x35, 0x02,
  29544. 0x8E, 0xBA, 0x8C, 0x31, 0xD7, 0x4B, 0xD2, 0x75,
  29545. 0xEA, 0xA6, 0x5B, 0x53, 0x40, 0xB3, 0xA4, 0x3F,
  29546. 0xBF, 0xE0, 0xB3, 0x06, 0x1D, 0x6B, 0xAE, 0x7E,
  29547. 0x75, 0xB7, 0x09, 0x8C, 0xDA, 0xBE, 0x91, 0xD4,
  29548. 0xB3, 0x1E, 0x36, 0xC9, 0xAA, 0x7A, 0x82, 0x98,
  29549. 0x86, 0x2A, 0xD6, 0x3C, 0x8F, 0xD2, 0x82, 0xE0,
  29550. 0x3B, 0x46, 0x0B, 0x3A, 0xB4, 0x64, 0xCE, 0x0F,
  29551. 0x27, 0xB1, 0xC3, 0xD1, 0x11, 0x55, 0xAC, 0xAA,
  29552. 0x01, 0x1E, 0xB9, 0xE2, 0xAE, 0x3E, 0x6D, 0xDA,
  29553. 0x07, 0xD6, 0xF4, 0x91, 0x73, 0x7C, 0xBC, 0xE9,
  29554. 0xB0, 0x5F, 0x9B, 0xC5, 0x6B, 0xE2, 0x0E, 0x8D,
  29555. 0x32, 0x6B, 0xA1, 0x32, 0xC5, 0x7F, 0xB2, 0x35,
  29556. 0x16, 0x11, 0x44, 0x51, 0x9C, 0xDF, 0x40, 0x56,
  29557. 0x0F, 0xBE, 0x27, 0x9B, 0xDE, 0x41, 0x1E, 0x11,
  29558. 0x25, 0x31, 0xF8, 0x26, 0xD6, 0xAB, 0x10, 0xD4,
  29559. 0x54, 0x73, 0x50, 0xAD, 0xD2, 0xA9, 0xDE, 0x8D,
  29560. 0x62, 0xC2, 0xAC, 0x82, 0xCA, 0xBE, 0x68, 0x15,
  29561. 0x64, 0x6F, 0x4D, 0xC9, 0x74, 0x2B, 0xB0, 0xC2,
  29562. 0xA3, 0xF7, 0x7E, 0xC7, 0xB4, 0x6C, 0x6B, 0x53,
  29563. 0x76, 0x05, 0xFA, 0x31, 0x79, 0x8C, 0xD8, 0x92,
  29564. 0x81, 0x22, 0x1A, 0x33, 0xDF, 0xB9, 0x79, 0x6E,
  29565. 0x64, 0x43, 0x05, 0x63, 0x03, 0x32, 0xC2, 0xCB,
  29566. 0x93, 0x14, 0x08, 0xAB, 0x48, 0x1A, 0x16, 0xD9,
  29567. 0x53, 0xF6, 0xBE, 0xAE, 0x38, 0x91, 0xD6, 0xD9,
  29568. 0xAC, 0x1F, 0xAB, 0x38, 0x22, 0x2D, 0x92, 0x71,
  29569. 0x87, 0x2D, 0x9D, 0x0C, 0xAD, 0xB9, 0x1A, 0xBE,
  29570. 0x9B, 0x4E, 0x26, 0x5F, 0x75, 0xC6, 0xE5, 0xE8,
  29571. 0x29, 0xE1, 0x46, 0xC3, 0xD8, 0xCE, 0x1E, 0x9D,
  29572. 0x12, 0xE0, 0xD1, 0x29, 0x80, 0x19, 0x57, 0xF4,
  29573. 0x6B, 0x0D, 0x2D, 0xBE, 0x1F, 0x74, 0x9B, 0x1D,
  29574. 0x08, 0xE2, 0x34, 0x5F, 0x62, 0x39, 0xA7, 0x31,
  29575. 0x34, 0x2E, 0xB7, 0x5B, 0x0C, 0xF1, 0xBF, 0x41,
  29576. 0x17, 0x49, 0xBC, 0x2C, 0xAF, 0x28, 0x10, 0xB7,
  29577. 0x88, 0xC6, 0xB7, 0x23, 0x8B, 0x4D, 0x3D, 0xA2,
  29578. 0xD6, 0x31, 0x5C, 0xE9, 0x54, 0x2E, 0x24, 0x40,
  29579. 0x4F, 0x14, 0x57, 0x55, 0xA3, 0x0A, 0xB8, 0x51,
  29580. 0xE4, 0x44, 0x58, 0x41, 0xBD, 0x33, 0xF7, 0x16,
  29581. 0xA5, 0x86, 0x88, 0x48, 0x88, 0xEC, 0xC6, 0xBC,
  29582. 0x64, 0x98, 0xAA, 0x32, 0x91, 0x9A, 0xE8, 0x1D,
  29583. 0x20, 0xC2, 0x69, 0x73, 0xC2, 0xBD, 0x54, 0x58,
  29584. 0x2A, 0x0F, 0x6A, 0xD9, 0x8A, 0xBF, 0xD2, 0x62,
  29585. 0x7E, 0x15, 0x69, 0x0A, 0x72, 0x7E, 0x69, 0xF5,
  29586. 0x81, 0xDD, 0x2A, 0x71, 0x27, 0x98, 0x2A, 0x90,
  29587. 0xE3, 0x3E, 0x2D, 0x4A, 0x03, 0xFE, 0x33, 0x91,
  29588. 0x42, 0xC7, 0xE4, 0x4C, 0x32, 0x6A, 0xC4, 0x6E,
  29589. 0xD3, 0x95, 0xA2, 0x25, 0xD3, 0x03, 0x33, 0x89,
  29590. 0x91, 0x73, 0x28, 0xB4, 0x53, 0x16, 0xB1, 0x58,
  29591. 0x5A, 0x01, 0xB2, 0xC3, 0x04, 0xB2, 0x94, 0x4E,
  29592. 0x90, 0x3A, 0xBB, 0xB3, 0xEC, 0x56, 0x19, 0x44,
  29593. 0x1C, 0xFC, 0x89, 0x65, 0xA4, 0x46, 0xDF, 0x75,
  29594. 0xDE, 0xFA, 0x80, 0xC6, 0xE1, 0x5A, 0xDB, 0xD5,
  29595. 0x06, 0xB7, 0xAB, 0x2D, 0xE1, 0x2D, 0xDA, 0x9B,
  29596. 0xC8, 0x14, 0x41, 0xCF, 0xC8, 0x90, 0x52, 0xE2,
  29597. 0xE5, 0x80, 0x8F, 0x71, 0x26, 0xC6, 0xFD, 0x3A,
  29598. 0xC6, 0xAC, 0x80, 0x81, 0x25, 0x8A, 0x84, 0xA0,
  29599. 0x9A, 0xE5, 0x0F, 0x6C, 0xD7, 0xCC, 0x0F, 0x4A,
  29600. 0xF3, 0x36, 0xFD, 0x1D, 0x64, 0x3E, 0x99, 0x07,
  29601. 0x99, 0x96, 0x26, 0x8C, 0x2D, 0x32, 0xD9, 0x09,
  29602. 0xF2, 0x2E, 0x35, 0x04, 0xF0, 0x7F, 0xBB, 0x56,
  29603. 0x31, 0x96, 0xD4, 0x31, 0x2F, 0xDD, 0xB9, 0x33,
  29604. 0x5D, 0x5C, 0x1D, 0x36, 0xE8, 0xC5, 0xEE, 0xA2,
  29605. 0x27, 0x8D, 0xBA, 0x23, 0xB9, 0x4D, 0x19, 0x3C,
  29606. 0x94, 0x7C, 0xC4, 0x1C, 0xA9, 0x93, 0xDC, 0x7D,
  29607. 0xB1, 0x39, 0x63, 0x40, 0xAD, 0x9C, 0x4F, 0xE6,
  29608. 0x87, 0xDD, 0x7B, 0x8D, 0x0C, 0x7A, 0x51, 0x20,
  29609. 0xAE, 0x02, 0x04, 0xF2, 0xC6, 0x65, 0xBD, 0x5F,
  29610. 0x47, 0x3D, 0x64, 0x4C, 0x7F, 0xF2, 0x6B, 0xFF,
  29611. 0xBA, 0x7A, 0x36, 0x98, 0x08, 0x30, 0x70, 0x21,
  29612. 0x28, 0xA7, 0xE6, 0x61, 0xD6, 0x77, 0xA0, 0x92,
  29613. 0xA3, 0x6E, 0x74, 0x28, 0xA4, 0x13, 0x9F, 0xB2,
  29614. 0x9B, 0x00, 0x95, 0xCC, 0x11, 0x08, 0x6F, 0x44,
  29615. 0x7D, 0x2A, 0x9E, 0xF6, 0xC9, 0xB1, 0x61, 0xF1,
  29616. 0x89, 0xC6, 0x29, 0x9E, 0x08, 0x4C, 0xB7, 0xAA,
  29617. 0x00, 0xFA, 0xF7, 0x87, 0x79, 0x7B, 0xFB, 0x06,
  29618. 0x9F, 0xBC, 0x08, 0x7F, 0xDE, 0x26, 0x25, 0x2A,
  29619. 0x16, 0x64, 0xF1, 0x9C, 0x5A, 0x8A, 0x22, 0xEC,
  29620. 0x5E, 0xE1, 0xAE, 0xB0, 0x76, 0x35, 0x7B, 0x7D,
  29621. 0xC3, 0x7E, 0x6B, 0x0F, 0x15, 0x20, 0xF9, 0x58,
  29622. 0xF7, 0x85, 0x1B, 0xAC, 0xB9, 0x2C, 0x89, 0xFD,
  29623. 0x11, 0x4A, 0x72, 0xFE, 0xAC, 0x54, 0x65, 0x2D,
  29624. 0x45, 0xB0, 0x9E, 0x1A, 0xE7, 0x65, 0x1A, 0xBD,
  29625. 0x16, 0x4B, 0xCD, 0x53, 0x7D, 0x58, 0xFA, 0x39,
  29626. 0xD3, 0xEC, 0x8A, 0xCD, 0xCD, 0xF9, 0x84, 0x25,
  29627. 0x00, 0x58, 0x62, 0xFA, 0x59, 0x69, 0x2D, 0xE1,
  29628. 0x62, 0xB7, 0x7E, 0x62, 0x97, 0xC6, 0x62, 0x33,
  29629. 0x34, 0x84, 0x08, 0xA8, 0xAB, 0x69, 0x5C, 0xE2,
  29630. 0xF2, 0x72, 0x8D, 0xB9, 0xFB, 0xE2, 0x7E, 0x95,
  29631. 0x89, 0x67, 0xEC, 0x59, 0x74, 0x76, 0x7C, 0x5A,
  29632. 0x66, 0x02, 0x30, 0x74, 0xB4, 0xA7, 0x1A, 0xFD,
  29633. 0x26, 0x4A, 0xD2, 0x89, 0x0E, 0x97, 0x0A, 0x1F,
  29634. 0x31, 0xD6, 0xE3, 0x31, 0x1B, 0x73, 0x6F, 0x9F,
  29635. 0x94, 0x88, 0x79, 0x3D, 0xDC, 0x88, 0xF2, 0x34,
  29636. 0x58, 0x06, 0x42, 0x54, 0xC8, 0x2A, 0x1D, 0x9E,
  29637. 0x59, 0xEA, 0xD2, 0xFC, 0xEC, 0x40, 0xB4, 0x30,
  29638. 0x68, 0x7C, 0x4B, 0x7E, 0x28, 0x96, 0x09, 0x26,
  29639. 0xAF, 0xCA, 0xCC, 0x9B, 0xD7, 0x56, 0xA7, 0x10,
  29640. 0x88, 0xC7, 0x84, 0x50, 0xE2, 0x0A, 0x2E, 0x98,
  29641. 0x0A, 0xED, 0xE9, 0xEB, 0xED, 0xFE, 0x7F, 0xAB,
  29642. 0xD6, 0xAB, 0xFE, 0x96, 0xF9, 0x34, 0xC4, 0xB0,
  29643. 0x2C, 0x01, 0xCA, 0x19, 0x4D, 0x01, 0xB7, 0x3C,
  29644. 0x25, 0xD5, 0x99, 0x70, 0x39, 0xD3, 0xFC, 0xD0,
  29645. 0xF0, 0x99, 0x52, 0x1F, 0x70, 0xCA, 0xEE, 0x69,
  29646. 0x11, 0x0A, 0xC1, 0xFC, 0x5A, 0x99, 0x91, 0x7A,
  29647. 0xD7, 0x52, 0xFC, 0x96, 0xAD, 0xFA, 0xD7, 0x18,
  29648. 0x6D, 0x0A, 0x7C, 0x9C, 0xFE, 0x56, 0x01, 0xC0,
  29649. 0x75, 0x14, 0xEA, 0x64, 0x48, 0xD6, 0x61, 0xC5,
  29650. 0x7A, 0xA2, 0x02, 0x42, 0x10, 0x3C, 0x42, 0x76,
  29651. 0xA0, 0x70, 0xA4, 0x89, 0xA4, 0xCB, 0x6B, 0xCA,
  29652. 0x0F, 0x9E, 0xCC, 0x43, 0x79, 0xFB, 0x22, 0x02,
  29653. 0x15, 0xFD, 0x91, 0xF8, 0x10, 0x19, 0xD5, 0xB0,
  29654. 0xAE, 0x61, 0x93, 0x58, 0xB5, 0x24, 0x68, 0xF2,
  29655. 0x72, 0xC1, 0x78, 0xE3, 0xA7, 0x4C, 0xF6, 0x77,
  29656. 0x5A, 0xA9, 0x24, 0xFE, 0x32, 0x9C, 0x31, 0x75,
  29657. 0xD9, 0xE4, 0xC3, 0xE2, 0x1A, 0xB9, 0xEC, 0x83,
  29658. 0x6E, 0xDC, 0x3A, 0xCA, 0xB2, 0xE3, 0x89, 0x1E,
  29659. 0xE8, 0xDE, 0xDA, 0x51, 0x5D, 0x39, 0xAF, 0x9B,
  29660. 0x8D, 0xDD, 0x0E, 0xE7, 0xB0, 0x16, 0x4F, 0x80,
  29661. 0x5C, 0x38, 0x35, 0xF6, 0xD2, 0xBA, 0xBD, 0xB3,
  29662. 0x0E, 0xAB, 0x47, 0x56, 0xE7, 0xEC, 0x7F, 0x82,
  29663. 0x9E, 0xCE, 0x01, 0xE8, 0xEA, 0xDF, 0xBB, 0xED,
  29664. 0x12, 0xFC, 0x28, 0x3B, 0x3D, 0x4C, 0x69, 0xF5,
  29665. 0x75, 0xE7, 0xF8, 0x04, 0x17, 0x68, 0x9F, 0xDF,
  29666. 0xCF, 0xC7, 0xBE, 0x27, 0xEE, 0x3B, 0x8C, 0xDF,
  29667. 0x57, 0xAA, 0xEB, 0xEC, 0x4A, 0x95, 0xB7, 0xE5,
  29668. 0xBB, 0x58, 0x5B, 0x85, 0x22, 0x7F, 0x7C, 0x32,
  29669. 0xBE, 0x30, 0xDB, 0x3E, 0x65, 0xE4, 0x2E, 0x30,
  29670. 0xDC, 0xF5, 0xA5, 0xFA, 0x07, 0x3D, 0xBA, 0x39,
  29671. 0x9D, 0x94, 0x2F, 0x22, 0x22, 0xAD, 0xB9, 0xB9,
  29672. 0x89, 0x81, 0x02, 0xAF, 0xE5, 0x43, 0x2E, 0xDC,
  29673. 0x7F, 0x04, 0xAE, 0x34, 0xA8, 0xFE, 0xC2, 0xD8,
  29674. 0x1C, 0xB4, 0x9A, 0x9A, 0x9B, 0x43, 0x81, 0x4C,
  29675. 0xE7, 0x1D, 0x97, 0xF7, 0x26, 0xE2, 0xB1, 0xE8,
  29676. 0xF6, 0x4B, 0x50, 0xE6, 0x5D, 0xFB, 0x48, 0x16,
  29677. 0xE1, 0x2E, 0x82, 0xA3, 0x19, 0x74, 0x84, 0xA4,
  29678. 0xE9, 0xBB, 0xA4, 0xD2, 0xD6, 0x9E, 0x3F, 0x19,
  29679. 0xD0, 0xB7, 0x5C, 0x21, 0xE2, 0xBF, 0xFE, 0x9F,
  29680. 0xC0, 0xC9, 0x8C, 0xF4, 0x8A, 0x3A, 0xAF, 0x08,
  29681. 0xD4, 0x67, 0xF7, 0x26, 0x87, 0xDF, 0x01, 0x78,
  29682. 0x17, 0x4B, 0x78, 0x97, 0xF7, 0x34, 0x34, 0x9B,
  29683. 0x18, 0x1E, 0xCA, 0x86, 0xA5, 0x98, 0xA0, 0xC5,
  29684. 0xE8, 0xC2, 0x59, 0x46, 0xF2, 0x4D, 0xC5, 0x57,
  29685. 0x2B, 0xD3, 0x24, 0xA4, 0x04, 0x58, 0xA7, 0x88,
  29686. 0xE5, 0x13, 0x7F, 0x3C, 0x7A, 0x7C, 0x97, 0xFC,
  29687. 0x9F, 0x12, 0xA3, 0xC4, 0x63, 0xA8, 0xFE, 0x94,
  29688. 0x49, 0x10, 0x1C, 0xCE, 0x96, 0x6D, 0x7C, 0x00,
  29689. 0x93, 0x23, 0x93, 0x29, 0x98, 0xD5, 0x6E, 0xF4,
  29690. 0x30, 0xC7, 0x3B, 0xC2, 0x4F, 0x5D, 0x95, 0xF7,
  29691. 0x37, 0x85, 0x8D, 0xDC, 0x4F, 0x32, 0xC0, 0x13
  29692. };
  29693. const byte kyber1024_ss[] = {
  29694. 0xB1, 0x0F, 0x73, 0x94, 0x92, 0x6A, 0xD3, 0xB4,
  29695. 0x9C, 0x5D, 0x62, 0xD5, 0xAE, 0xB5, 0x31, 0xD5,
  29696. 0x75, 0x75, 0x38, 0xBC, 0xC0, 0xDA, 0x9E, 0x55,
  29697. 0x0D, 0x43, 0x8F, 0x1B, 0x61, 0xBD, 0x74, 0x19
  29698. };
  29699. #else
  29700. const byte kyber1024_pk[] = {
  29701. 0x68, 0xE6, 0xBC, 0x26, 0x8B, 0x9E, 0x36, 0x1B,
  29702. 0x8F, 0x6E, 0x0A, 0xBC, 0xFE, 0x88, 0x93, 0x37,
  29703. 0x87, 0x53, 0x48, 0xE4, 0x16, 0x4D, 0x66, 0x15,
  29704. 0xF3, 0x82, 0xC6, 0xC7, 0xC4, 0x17, 0x8F, 0x75,
  29705. 0x1F, 0x72, 0x2B, 0x21, 0x27, 0x74, 0x64, 0xFB,
  29706. 0x83, 0x1F, 0x8A, 0xA9, 0xA0, 0x18, 0xC1, 0x9A,
  29707. 0x51, 0xB0, 0x7F, 0xA0, 0x93, 0x9B, 0xA5, 0xC2,
  29708. 0x06, 0x68, 0x63, 0x23, 0xAF, 0xF6, 0xCA, 0x83,
  29709. 0x0B, 0x76, 0xE3, 0x80, 0x02, 0x63, 0x54, 0x47,
  29710. 0x7A, 0xAC, 0x58, 0xD8, 0x93, 0x1D, 0x80, 0x3B,
  29711. 0x6F, 0xAB, 0xD7, 0x12, 0xD1, 0x72, 0x81, 0x4A,
  29712. 0x54, 0xB8, 0x12, 0xE7, 0x92, 0x47, 0x94, 0xAA,
  29713. 0x40, 0x02, 0x49, 0xD5, 0xC0, 0x46, 0x8E, 0xA3,
  29714. 0x2F, 0xB2, 0x15, 0x54, 0xC5, 0x93, 0x67, 0x90,
  29715. 0x3C, 0x60, 0xA8, 0xB4, 0x8E, 0x23, 0x40, 0x71,
  29716. 0x2F, 0x7A, 0x04, 0x5A, 0x67, 0xC2, 0x3C, 0x72,
  29717. 0x41, 0x3A, 0x64, 0x69, 0xFC, 0x83, 0x69, 0xA2,
  29718. 0x91, 0x03, 0x3C, 0x46, 0xCE, 0xA3, 0x96, 0x76,
  29719. 0x65, 0x6B, 0x6C, 0xD8, 0x57, 0x99, 0x7C, 0x04,
  29720. 0x5C, 0x5A, 0xF0, 0x34, 0xE1, 0xE1, 0x14, 0x8A,
  29721. 0x8B, 0x40, 0xCD, 0x16, 0x68, 0x31, 0x93, 0xA3,
  29722. 0x60, 0x0C, 0x84, 0xFC, 0xE3, 0xB9, 0x65, 0x10,
  29723. 0x88, 0xDD, 0x09, 0x44, 0x57, 0x6C, 0x7E, 0xA4,
  29724. 0x07, 0x2D, 0x25, 0xF0, 0x67, 0xD2, 0x42, 0x7E,
  29725. 0x26, 0xD1, 0x86, 0xF9, 0xB7, 0x7A, 0x32, 0x79,
  29726. 0x05, 0x03, 0x3B, 0xA9, 0x40, 0x85, 0xA8, 0xAB,
  29727. 0x6A, 0x8A, 0x2B, 0x86, 0x08, 0x0C, 0x45, 0x2B,
  29728. 0x10, 0x90, 0xAC, 0xC9, 0x5A, 0x2B, 0x9F, 0x5A,
  29729. 0x6E, 0x61, 0x84, 0x6D, 0x63, 0x45, 0x66, 0xFA,
  29730. 0xC0, 0xB0, 0xFF, 0x35, 0x11, 0x18, 0x06, 0x18,
  29731. 0xAF, 0x98, 0x34, 0x87, 0x12, 0x2C, 0xFB, 0x63,
  29732. 0x61, 0xFF, 0x66, 0x0D, 0x5C, 0xA0, 0x0B, 0x9D,
  29733. 0x88, 0xB1, 0xDA, 0x18, 0x36, 0xC9, 0x78, 0x6E,
  29734. 0x55, 0x27, 0x6C, 0x88, 0x53, 0x8C, 0xC1, 0xD3,
  29735. 0x05, 0xFA, 0xAA, 0xB7, 0x79, 0x9A, 0x35, 0x46,
  29736. 0xF0, 0xBE, 0x6C, 0xC7, 0x00, 0xB0, 0x65, 0xB9,
  29737. 0x03, 0x4A, 0x91, 0x63, 0x31, 0x74, 0x66, 0xD9,
  29738. 0x83, 0xDD, 0xC2, 0xAA, 0x5B, 0xB0, 0x3F, 0x5B,
  29739. 0x67, 0x33, 0x48, 0xD5, 0x76, 0x02, 0x8A, 0x02,
  29740. 0x55, 0x15, 0x6C, 0x6A, 0x29, 0x74, 0x46, 0x98,
  29741. 0x34, 0xFB, 0x15, 0x73, 0x23, 0x74, 0x60, 0x54,
  29742. 0xC4, 0xBA, 0x96, 0x40, 0x30, 0x4A, 0x11, 0x63,
  29743. 0x04, 0xF0, 0x18, 0x21, 0x72, 0xBA, 0x8D, 0x03,
  29744. 0xAE, 0xDA, 0x39, 0x91, 0x6A, 0xF1, 0x18, 0xB9,
  29745. 0x84, 0x22, 0xB7, 0x16, 0x3A, 0xB6, 0xE2, 0x47,
  29746. 0xFC, 0xAA, 0x9D, 0x7C, 0x7C, 0xAE, 0x96, 0xBB,
  29747. 0x60, 0xB1, 0xD7, 0x86, 0x0E, 0x4C, 0x7F, 0x1D,
  29748. 0x92, 0x0D, 0x31, 0xA5, 0x41, 0x38, 0x82, 0x29,
  29749. 0xEE, 0x84, 0x2C, 0xA1, 0x59, 0x43, 0x89, 0x4A,
  29750. 0x56, 0xE5, 0x14, 0x0D, 0x29, 0x59, 0xC3, 0x6E,
  29751. 0xA2, 0x6E, 0x2D, 0xE0, 0x51, 0x21, 0x36, 0x6B,
  29752. 0xE4, 0x14, 0x02, 0x7B, 0xD1, 0x47, 0x81, 0x47,
  29753. 0x2E, 0xDD, 0xA4, 0x71, 0x10, 0xD0, 0xB3, 0xAE,
  29754. 0x2A, 0xA2, 0x55, 0xAB, 0x6D, 0x6F, 0xE0, 0x1C,
  29755. 0x26, 0xC2, 0x87, 0x63, 0x97, 0x6B, 0xBF, 0xA9,
  29756. 0x2F, 0x4F, 0x09, 0xA5, 0xB7, 0x3C, 0x47, 0x1F,
  29757. 0x8C, 0x9E, 0x53, 0x26, 0x92, 0x55, 0xDA, 0xC1,
  29758. 0xE0, 0x71, 0x5E, 0x87, 0x00, 0x06, 0x09, 0x70,
  29759. 0x60, 0xFF, 0x23, 0xC3, 0xAB, 0x43, 0xC4, 0x19,
  29760. 0xE4, 0x48, 0xC7, 0x30, 0x40, 0x10, 0xC4, 0x0F,
  29761. 0x3D, 0xC0, 0xB7, 0x26, 0x39, 0x2E, 0x46, 0x03,
  29762. 0x7E, 0xB9, 0x49, 0x04, 0x55, 0xE3, 0x49, 0xA4,
  29763. 0x35, 0xCD, 0xA9, 0x0A, 0xA3, 0xB7, 0x47, 0x8C,
  29764. 0x32, 0xE8, 0x72, 0xB2, 0xB8, 0x5C, 0xEC, 0xF0,
  29765. 0x3D, 0xDC, 0xD6, 0x16, 0x33, 0x78, 0xBF, 0x73,
  29766. 0xB1, 0x3B, 0x2E, 0x1A, 0x7B, 0x6E, 0xBC, 0x39,
  29767. 0x29, 0xEC, 0x08, 0x63, 0x89, 0x3F, 0x73, 0x8C,
  29768. 0x38, 0x9F, 0x3B, 0x3B, 0x2B, 0xB5, 0x4F, 0xC1,
  29769. 0x5B, 0xB7, 0x8A, 0x25, 0x31, 0xEE, 0xBC, 0xB7,
  29770. 0xBC, 0x10, 0x81, 0x07, 0x96, 0x59, 0x81, 0xD2,
  29771. 0x0A, 0x7D, 0x1A, 0x87, 0x21, 0xBA, 0x36, 0x40,
  29772. 0x03, 0x13, 0x2F, 0x03, 0x6C, 0xE4, 0x0C, 0x9E,
  29773. 0x75, 0x5B, 0x2A, 0xB4, 0x86, 0x1D, 0x04, 0xC1,
  29774. 0xBE, 0x0D, 0x53, 0x97, 0x91, 0x4B, 0x6C, 0xD4,
  29775. 0xAA, 0x40, 0x8C, 0x62, 0x2B, 0x21, 0xB1, 0x1B,
  29776. 0xA0, 0xEB, 0xA6, 0x17, 0x29, 0x59, 0x6F, 0x8B,
  29777. 0x0E, 0x82, 0x14, 0xAA, 0x4A, 0xA6, 0x01, 0x86,
  29778. 0x51, 0x07, 0xA2, 0xA7, 0x6A, 0xD7, 0xCC, 0x2F,
  29779. 0x97, 0x43, 0x00, 0x18, 0x28, 0xB8, 0x0B, 0x24,
  29780. 0x80, 0x2B, 0x98, 0x9D, 0x98, 0x42, 0x5E, 0x14,
  29781. 0x7C, 0x6D, 0x93, 0x32, 0xBA, 0x08, 0x4B, 0x8F,
  29782. 0xFB, 0x96, 0x27, 0xE3, 0xFB, 0xC4, 0xBE, 0x65,
  29783. 0x38, 0xEB, 0xDA, 0x1D, 0x9D, 0xB9, 0xB6, 0x75,
  29784. 0xB0, 0x16, 0x66, 0x57, 0x23, 0xD4, 0x11, 0x71,
  29785. 0x54, 0xF8, 0x13, 0x3E, 0xA9, 0x00, 0xCB, 0x47,
  29786. 0x0B, 0x1D, 0xC5, 0xB5, 0x7A, 0x78, 0x11, 0x16,
  29787. 0x72, 0x9E, 0xD6, 0x29, 0x45, 0x6E, 0x1A, 0x7D,
  29788. 0x6A, 0x37, 0x9F, 0x2E, 0x01, 0x0A, 0xCB, 0xEC,
  29789. 0x21, 0xFC, 0xD1, 0xC2, 0xB0, 0xBB, 0x61, 0xC3,
  29790. 0xDA, 0x6C, 0x73, 0x70, 0x5F, 0xD7, 0xB0, 0xC3,
  29791. 0x61, 0x2A, 0xC3, 0xB4, 0xA1, 0x2E, 0xC5, 0xD3,
  29792. 0x7F, 0x4C, 0x1A, 0x4A, 0x4D, 0xF8, 0x16, 0x6E,
  29793. 0xF6, 0x68, 0x9C, 0x06, 0xB5, 0xD4, 0xA6, 0x1A,
  29794. 0x52, 0x14, 0xAE, 0x5E, 0x5C, 0x28, 0x07, 0x4C,
  29795. 0xC5, 0xFE, 0x2A, 0x41, 0x2A, 0xC5, 0x14, 0x15,
  29796. 0x94, 0x28, 0x54, 0x85, 0x15, 0x6E, 0xCA, 0x65,
  29797. 0x64, 0x28, 0x37, 0x20, 0xB0, 0xBA, 0xD1, 0xD9,
  29798. 0x74, 0x5B, 0xA5, 0x77, 0x71, 0x1A, 0xB6, 0xF2,
  29799. 0x14, 0xC5, 0x2F, 0xB5, 0xBA, 0xAF, 0x8A, 0x1F,
  29800. 0x85, 0x33, 0x5A, 0xEB, 0x44, 0x37, 0x0B, 0x39,
  29801. 0xC5, 0x6C, 0x4C, 0x3D, 0x32, 0x75, 0x55, 0x60,
  29802. 0xCB, 0x2C, 0xA9, 0x45, 0x87, 0x51, 0xF1, 0x65,
  29803. 0x4B, 0x82, 0xC5, 0xE7, 0xFB, 0x0D, 0xB5, 0x16,
  29804. 0xBE, 0x1E, 0x42, 0x43, 0x3A, 0xD4, 0x7B, 0x6B,
  29805. 0xCC, 0xB3, 0x63, 0x61, 0xC4, 0x34, 0xEB, 0x59,
  29806. 0xCF, 0xF3, 0x38, 0x49, 0x54, 0xBB, 0x76, 0xF1,
  29807. 0x44, 0x14, 0x91, 0x67, 0x91, 0x3B, 0x85, 0x0F,
  29808. 0x75, 0x28, 0xBD, 0x76, 0x87, 0xB8, 0x8B, 0x64,
  29809. 0x84, 0xA6, 0x82, 0x68, 0x39, 0x97, 0x08, 0x42,
  29810. 0xA5, 0x93, 0xBB, 0x93, 0x45, 0x5A, 0x73, 0xBC,
  29811. 0x2C, 0x74, 0x62, 0x55, 0xC1, 0x3B, 0xC8, 0x52,
  29812. 0x1C, 0x73, 0x54, 0xE9, 0x8B, 0x98, 0x76, 0xA9,
  29813. 0x58, 0x6F, 0xC3, 0x7C, 0x5F, 0x24, 0x8D, 0xA0,
  29814. 0x55, 0x75, 0x8B, 0xD9, 0x61, 0x5E, 0x68, 0xBF,
  29815. 0x93, 0xEA, 0x0B, 0xA1, 0xD0, 0x22, 0xE2, 0xD2,
  29816. 0x22, 0x04, 0xA4, 0x45, 0x8C, 0xB6, 0x12, 0xB0,
  29817. 0x29, 0x41, 0xE8, 0x7A, 0xAC, 0xF1, 0xA3, 0x29,
  29818. 0x07, 0xD9, 0x8B, 0x4B, 0x13, 0xC6, 0xC9, 0xB4,
  29819. 0xAB, 0xD6, 0xDC, 0xCF, 0x66, 0x21, 0x06, 0xF5,
  29820. 0xD5, 0x8C, 0x2F, 0xC7, 0x51, 0x97, 0x35, 0x5E,
  29821. 0xB8, 0xCC, 0x41, 0x74, 0x2C, 0x6D, 0xA0, 0x03,
  29822. 0x40, 0x6D, 0x13, 0x56, 0xF8, 0xA0, 0x7A, 0x37,
  29823. 0x97, 0x88, 0x00, 0x60, 0x67, 0xCB, 0xF5, 0x2F,
  29824. 0x22, 0xD5, 0x3D, 0x68, 0x71, 0x22, 0x41, 0x09,
  29825. 0x34, 0x6E, 0xA8, 0x3B, 0x45, 0xCA, 0x60, 0xAF,
  29826. 0xEC, 0xCA, 0x0F, 0xA5, 0x67, 0x2B, 0x36, 0x64,
  29827. 0x89, 0xEB, 0x8D, 0xD1, 0xF7, 0x20, 0x4C, 0xDB,
  29828. 0x7D, 0x9B, 0xE1, 0x87, 0xF4, 0x39, 0x7B, 0x9B,
  29829. 0x4C, 0xA0, 0x31, 0x34, 0x75, 0x0D, 0xD5, 0x7D,
  29830. 0xAD, 0xE1, 0x88, 0x91, 0xCC, 0xA9, 0x47, 0x4B,
  29831. 0x65, 0x32, 0x60, 0x52, 0x50, 0xBC, 0xBB, 0x6E,
  29832. 0x02, 0x94, 0xEF, 0x95, 0x32, 0x6D, 0x25, 0x9B,
  29833. 0x08, 0xB2, 0x93, 0x8D, 0x8C, 0x59, 0xE2, 0x58,
  29834. 0x4B, 0x69, 0x10, 0x71, 0x85, 0xF3, 0x45, 0x30,
  29835. 0x24, 0x07, 0x35, 0x17, 0xC9, 0x89, 0x00, 0x9E,
  29836. 0xBB, 0x6B, 0x22, 0xED, 0xE9, 0x70, 0x43, 0x66,
  29837. 0xC6, 0x23, 0x2A, 0x62, 0x82, 0xF3, 0xBB, 0x09,
  29838. 0x08, 0x74, 0xA6, 0xFB, 0x0D, 0x84, 0x53, 0x6D,
  29839. 0x3F, 0x61, 0x3A, 0x21, 0xD1, 0x9D, 0x98, 0x44,
  29840. 0x17, 0xFA, 0xF0, 0x84, 0x6C, 0xB4, 0x28, 0x7B,
  29841. 0x3C, 0x70, 0x9F, 0x22, 0x62, 0x26, 0xA0, 0x9E,
  29842. 0x94, 0xFA, 0x89, 0x85, 0x43, 0x87, 0x4A, 0xE2,
  29843. 0x08, 0x11, 0xE3, 0x90, 0x80, 0xE6, 0xB2, 0xCE,
  29844. 0x49, 0x92, 0x26, 0xF5, 0x13, 0x8C, 0x14, 0x38,
  29845. 0x72, 0xDB, 0x86, 0xFB, 0xC2, 0x9C, 0xFD, 0xA2,
  29846. 0x9F, 0xD2, 0x26, 0x0E, 0xC7, 0xB9, 0xA2, 0x37,
  29847. 0xD1, 0x9A, 0xAB, 0x1B, 0x93, 0xE1, 0xDA, 0x4E,
  29848. 0xBC, 0x58, 0x9D, 0x82, 0x4C, 0x58, 0x90, 0x96,
  29849. 0x99, 0xD3, 0xE8, 0xBC, 0x7F, 0x53, 0xB3, 0x55,
  29850. 0xFA, 0x31, 0xE9, 0x6B, 0x1E, 0x35, 0x4A, 0xA4,
  29851. 0xB3, 0xC8, 0x4D, 0xF3, 0xC4, 0x7F, 0x80, 0xF9,
  29852. 0x32, 0xEA, 0x68, 0x5D, 0x96, 0xC5, 0xA5, 0x96,
  29853. 0x08, 0x22, 0xC6, 0x43, 0x64, 0x5F, 0x1A, 0x63,
  29854. 0xDD, 0x60, 0x8A, 0x09, 0x71, 0xC7, 0xF2, 0x15,
  29855. 0x7C, 0xA5, 0x24, 0x4D, 0x49, 0x2C, 0x01, 0x0B,
  29856. 0x71, 0xB7, 0x74, 0xC1, 0x0C, 0x07, 0xD3, 0xC8,
  29857. 0x4C, 0xA5, 0x4D, 0xE5, 0x00, 0x1F, 0xE0, 0xAC,
  29858. 0x4E, 0x69, 0x52, 0x81, 0x4E, 0xC1, 0x0F, 0xF9,
  29859. 0x8B, 0x19, 0xE7, 0x89, 0xC1, 0xAC, 0x68, 0x45,
  29860. 0xC7, 0x62, 0x80, 0x55, 0x27, 0x5C, 0x33, 0x34,
  29861. 0x41, 0x87, 0x1B, 0x8C, 0x30, 0x99, 0x9B, 0x05,
  29862. 0x31, 0x34, 0xC0, 0xE7, 0xB1, 0xAC, 0x6A, 0xA6,
  29863. 0x63, 0x12, 0xC1, 0xA5, 0xE2, 0x97, 0x92, 0x97,
  29864. 0x02, 0x56, 0x88, 0x14, 0x38, 0xDC, 0x46, 0x29,
  29865. 0x37, 0x4D, 0xFB, 0x9A, 0x91, 0x48, 0x62, 0x9D,
  29866. 0x14, 0x8C, 0x60, 0x1D, 0x4B, 0x6A, 0xA2, 0xC2,
  29867. 0x9C, 0xCE, 0xE8, 0x94, 0x7A, 0x27, 0x87, 0x0E,
  29868. 0x4C, 0x65, 0xE4, 0x12, 0x46, 0xD6, 0x25, 0x3D,
  29869. 0xC4, 0xF1, 0x3A, 0xEB, 0x31, 0xCD, 0x7E, 0x36,
  29870. 0x2F, 0x0F, 0x28, 0x84, 0xB9, 0xF7, 0x3B, 0xA9,
  29871. 0x93, 0x50, 0xAA, 0xA4, 0x02, 0x4A, 0x73, 0x14,
  29872. 0xBE, 0x69, 0x3A, 0xE4, 0x13, 0xA5, 0xCE, 0xA0,
  29873. 0x9A, 0x27, 0x27, 0x3A, 0x0B, 0xF6, 0xA4, 0x53,
  29874. 0xF8, 0x63, 0x9B, 0x48, 0x26, 0x8D, 0x21, 0xC9,
  29875. 0xAE, 0xC2, 0x62, 0xBE, 0x28, 0xC0, 0x7E, 0xA4,
  29876. 0xB9, 0x35, 0xA8, 0x69, 0x63, 0x26, 0xAA, 0x18,
  29877. 0xA5, 0x91, 0xCE, 0xE9, 0xC9, 0xF2, 0xA7, 0x29,
  29878. 0xBC, 0x50, 0x33, 0xDD, 0xE6, 0x37, 0x27, 0x38,
  29879. 0x85, 0x92, 0x1B, 0x73, 0xB8, 0x58, 0xA1, 0x88,
  29880. 0x33, 0xA7, 0xB8, 0x3A, 0x82, 0xFE, 0x02, 0xCC,
  29881. 0x33, 0x54, 0xC3, 0x0B, 0x0C, 0x4D, 0x0D, 0x3B,
  29882. 0xA1, 0x33, 0x1C, 0x6E, 0xC7, 0xA7, 0x0B, 0x7E,
  29883. 0x50, 0x3F, 0x76, 0x47, 0xA9, 0x52, 0xB0, 0x03,
  29884. 0x86, 0xCC, 0x90, 0xB3, 0x4A, 0x51, 0x4F, 0x99,
  29885. 0x5A, 0x56, 0x92, 0x05, 0xEB, 0x2A, 0x3F, 0xA7,
  29886. 0x9A, 0x78, 0x65, 0x21, 0x18, 0xD4, 0xBC, 0x48,
  29887. 0x46, 0x27, 0xCD, 0x02, 0x33, 0xBC, 0x5A, 0x35,
  29888. 0x16, 0x15, 0x0B, 0xAF, 0x6E, 0x10, 0x66, 0x5D,
  29889. 0xA7, 0x61, 0x8F, 0x28, 0x41, 0xB5, 0x02, 0x61,
  29890. 0x72, 0x48, 0x89, 0x3B, 0xAA, 0xB7, 0xE9, 0x03,
  29891. 0x89, 0x0D, 0xF0, 0xCA, 0xC2, 0x84, 0xCD, 0x11,
  29892. 0x65, 0xB4, 0x64, 0xC1, 0x8F, 0x4C, 0xFA, 0x8F,
  29893. 0x69, 0x2D, 0x3C, 0x8E, 0x8C, 0x73, 0x9F, 0xB3,
  29894. 0x78, 0x27, 0x74, 0x15, 0x61, 0xD5, 0xE7, 0x66,
  29895. 0x67, 0x51, 0x82, 0xFF, 0x83, 0xC9, 0x1C, 0xF0,
  29896. 0x33, 0x65, 0x88, 0x23, 0xD8, 0xFA, 0x91, 0xE1
  29897. };
  29898. const byte kyber1024_sk[] = {
  29899. 0x1B, 0xD1, 0x97, 0x95, 0x10, 0xBC, 0xE5, 0x03,
  29900. 0x1C, 0x0F, 0x99, 0x1D, 0xE0, 0x82, 0xC3, 0x89,
  29901. 0xA1, 0xBD, 0x15, 0x57, 0x19, 0x1D, 0x81, 0x03,
  29902. 0xBD, 0xCC, 0x04, 0xC1, 0x98, 0xAE, 0x3E, 0x95,
  29903. 0x95, 0xFD, 0xB6, 0x57, 0xB5, 0xC6, 0x39, 0x97,
  29904. 0x95, 0x79, 0xAE, 0x13, 0x5B, 0x11, 0xE2, 0x4D,
  29905. 0x36, 0xE5, 0x49, 0x44, 0x67, 0x83, 0x2E, 0x6A,
  29906. 0x8A, 0x46, 0xAA, 0x8F, 0x9F, 0xF9, 0x87, 0x4A,
  29907. 0x16, 0x00, 0x27, 0x84, 0x07, 0x71, 0x56, 0x2D,
  29908. 0x74, 0x61, 0xB1, 0x6C, 0x61, 0x87, 0xF3, 0xC2,
  29909. 0xC1, 0x9C, 0x9C, 0x8F, 0xE6, 0x41, 0x49, 0x7D,
  29910. 0x8A, 0x15, 0x5C, 0x51, 0x15, 0x7D, 0x91, 0x35,
  29911. 0xD9, 0xB4, 0x5D, 0x3C, 0x7C, 0x4C, 0xB1, 0x82,
  29912. 0x31, 0x8C, 0x11, 0xAD, 0x08, 0xB3, 0x9F, 0xAC,
  29913. 0x04, 0x15, 0xE6, 0xE2, 0x6D, 0xB3, 0x43, 0x91,
  29914. 0xDE, 0xC2, 0x47, 0x1B, 0x30, 0x3C, 0x05, 0x05,
  29915. 0xB6, 0xFE, 0x45, 0x6E, 0x81, 0x50, 0x21, 0xE8,
  29916. 0x2B, 0x5F, 0x2D, 0x16, 0x6F, 0xDF, 0xB5, 0xB9,
  29917. 0x23, 0xC8, 0x3A, 0x61, 0x67, 0x30, 0x28, 0xF0,
  29918. 0xBA, 0xE0, 0x28, 0x08, 0x9F, 0x47, 0x61, 0x67,
  29919. 0xE5, 0x75, 0x6C, 0xAA, 0x3E, 0xE0, 0xF1, 0x89,
  29920. 0xC6, 0x47, 0x81, 0xC7, 0x49, 0x27, 0xBC, 0xDB,
  29921. 0x67, 0xE7, 0x75, 0xCC, 0xA8, 0x5B, 0x44, 0xB8,
  29922. 0x8B, 0x10, 0x61, 0xD8, 0x96, 0x3F, 0xE4, 0x0B,
  29923. 0x1F, 0xB4, 0x34, 0x68, 0x20, 0x09, 0x0E, 0x5C,
  29924. 0x0B, 0x7C, 0x49, 0x08, 0x89, 0x33, 0xAD, 0xE3,
  29925. 0x82, 0x96, 0x88, 0x6C, 0x43, 0x45, 0x65, 0x7C,
  29926. 0x3F, 0x07, 0xCF, 0x1A, 0xE9, 0x76, 0xE6, 0xBA,
  29927. 0x77, 0x7E, 0xF0, 0x1F, 0xB9, 0xC3, 0xB0, 0xDC,
  29928. 0xF0, 0x58, 0x3A, 0xE5, 0x44, 0xCD, 0x80, 0x3A,
  29929. 0xC1, 0x39, 0xCA, 0x61, 0x9B, 0x8D, 0xCA, 0x13,
  29930. 0x1C, 0xD2, 0x73, 0xCE, 0xCB, 0x65, 0xBC, 0xEF,
  29931. 0x00, 0x29, 0x95, 0x12, 0xB0, 0x21, 0xBA, 0x1B,
  29932. 0x2F, 0xF2, 0x08, 0xEE, 0xBB, 0x08, 0xA6, 0x28,
  29933. 0x7E, 0xC4, 0x4C, 0x21, 0x7D, 0x94, 0x76, 0x40,
  29934. 0x42, 0x5C, 0x0E, 0x97, 0x13, 0x44, 0x5B, 0xC5,
  29935. 0x2B, 0xC3, 0xCD, 0x66, 0x01, 0x2C, 0x8C, 0xB7,
  29936. 0x10, 0xA2, 0xE8, 0xAD, 0x5F, 0xC5, 0x1C, 0x5B,
  29937. 0x71, 0xCE, 0x69, 0xE9, 0x6A, 0x22, 0x06, 0x6F,
  29938. 0x74, 0x4B, 0xBA, 0xB6, 0x47, 0x2F, 0xEF, 0x3A,
  29939. 0x5B, 0xCF, 0xD6, 0x1D, 0xF1, 0xF8, 0x6A, 0x95,
  29940. 0x64, 0x80, 0x03, 0x1A, 0x99, 0x0E, 0xE0, 0x7D,
  29941. 0xE5, 0x04, 0x5E, 0xBC, 0x49, 0x8F, 0x2D, 0x3A,
  29942. 0xBD, 0xE3, 0x28, 0xC4, 0x2A, 0xCB, 0x21, 0xAD,
  29943. 0x62, 0x5D, 0x7B, 0xCB, 0x85, 0x5A, 0x8A, 0x0A,
  29944. 0xCB, 0xA2, 0x85, 0x16, 0xD3, 0x5A, 0xAF, 0x92,
  29945. 0xBE, 0xB3, 0x9C, 0x46, 0xD8, 0xF4, 0xA0, 0x1F,
  29946. 0x73, 0x20, 0x40, 0xF2, 0x7C, 0x40, 0x9B, 0xA4,
  29947. 0x9A, 0x80, 0xCF, 0x95, 0x55, 0x7C, 0x73, 0x7C,
  29948. 0x4A, 0x45, 0x11, 0x2C, 0x52, 0x2C, 0x78, 0x04,
  29949. 0xC7, 0x7A, 0x3D, 0x44, 0x53, 0xBA, 0x82, 0xC1,
  29950. 0x49, 0x08, 0xBC, 0xC0, 0x47, 0x13, 0x7E, 0x5A,
  29951. 0x7B, 0x23, 0x92, 0xC8, 0x31, 0x25, 0x4C, 0x1A,
  29952. 0x59, 0x54, 0xC8, 0x8C, 0xCF, 0xA5, 0xD8, 0x8B,
  29953. 0x3B, 0x93, 0x31, 0xBE, 0xF4, 0x62, 0x55, 0x09,
  29954. 0x14, 0xD1, 0x84, 0x17, 0x59, 0x40, 0xB4, 0xE9,
  29955. 0xB7, 0xAD, 0x3D, 0x44, 0x2E, 0xA9, 0x68, 0xB6,
  29956. 0xA4, 0x0A, 0x19, 0x72, 0x71, 0x5B, 0xCB, 0x72,
  29957. 0x82, 0x14, 0xA1, 0xBE, 0x8E, 0x04, 0x1D, 0xF5,
  29958. 0x72, 0xCC, 0x2D, 0x01, 0x6F, 0xFD, 0x6B, 0x0A,
  29959. 0xFA, 0xC8, 0x02, 0xBA, 0x97, 0x5A, 0x4A, 0x31,
  29960. 0xA4, 0x18, 0x40, 0x2E, 0x6B, 0x01, 0x41, 0xFA,
  29961. 0xD6, 0x48, 0x49, 0xE1, 0x6C, 0x80, 0x37, 0x23,
  29962. 0xA7, 0x74, 0x2B, 0x08, 0x2A, 0x7A, 0x56, 0x26,
  29963. 0x4E, 0x02, 0x60, 0xA2, 0x4A, 0x66, 0x42, 0x3D,
  29964. 0x52, 0x64, 0x82, 0x07, 0x04, 0x06, 0x5C, 0xAC,
  29965. 0x41, 0x10, 0x72, 0x48, 0x64, 0x37, 0xCA, 0xA5,
  29966. 0x47, 0x2D, 0x73, 0x3C, 0x64, 0x14, 0x5F, 0x10,
  29967. 0x0B, 0x0B, 0x16, 0xC0, 0xAB, 0x0D, 0xB8, 0x7C,
  29968. 0xC5, 0x64, 0x26, 0xC1, 0x1A, 0x69, 0x45, 0x25,
  29969. 0x95, 0x8D, 0xB8, 0x31, 0x59, 0x31, 0xC5, 0x09,
  29970. 0xD6, 0x3C, 0x48, 0x10, 0xBB, 0xA7, 0x68, 0xCA,
  29971. 0xCB, 0xF9, 0xCD, 0x85, 0x7B, 0x0D, 0x66, 0xD6,
  29972. 0x5A, 0xF7, 0xEB, 0xAC, 0x18, 0xB7, 0x9C, 0xBE,
  29973. 0xBC, 0x0A, 0x49, 0xB5, 0xC0, 0x5A, 0xA8, 0x7F,
  29974. 0x8D, 0xE8, 0x25, 0xA6, 0xA1, 0x13, 0x5F, 0xE8,
  29975. 0x64, 0xCF, 0x6C, 0x99, 0x8D, 0xDC, 0x37, 0xFC,
  29976. 0x01, 0x56, 0xBB, 0x52, 0xA7, 0x8E, 0xC2, 0x99,
  29977. 0xCA, 0x53, 0x1C, 0xF8, 0x75, 0xBE, 0xFA, 0x48,
  29978. 0x75, 0x72, 0x60, 0x28, 0xED, 0x37, 0x91, 0xBA,
  29979. 0x31, 0xC0, 0x0E, 0xC7, 0x40, 0x20, 0xCA, 0xA1,
  29980. 0x7F, 0x91, 0x19, 0x63, 0x96, 0xCF, 0xE5, 0x88,
  29981. 0x6E, 0x51, 0x1A, 0x52, 0xD0, 0xCA, 0x8D, 0xEE,
  29982. 0x25, 0xBF, 0x4E, 0x77, 0xCB, 0x92, 0xE6, 0x36,
  29983. 0x40, 0x64, 0x6D, 0xE2, 0x58, 0x6D, 0xCD, 0x00,
  29984. 0x14, 0x16, 0xCC, 0xC1, 0xEC, 0x30, 0x7C, 0xA2,
  29985. 0x75, 0x6D, 0xBE, 0xA8, 0x26, 0x36, 0x14, 0x51,
  29986. 0x3C, 0xA0, 0x5E, 0x70, 0xC7, 0x54, 0xF5, 0x6A,
  29987. 0x2C, 0x3F, 0xF7, 0x42, 0x94, 0x8C, 0x34, 0xAF,
  29988. 0x49, 0x34, 0x2F, 0x55, 0x03, 0x4C, 0x1C, 0x70,
  29989. 0xE0, 0x79, 0x6D, 0x26, 0xB2, 0x30, 0x35, 0x6B,
  29990. 0x1B, 0x74, 0xA1, 0x6C, 0x0B, 0x28, 0x37, 0x4B,
  29991. 0x79, 0xC1, 0x6B, 0x04, 0x96, 0xCA, 0x20, 0x12,
  29992. 0x3E, 0x6C, 0x67, 0x62, 0xD9, 0x01, 0xE2, 0x76,
  29993. 0x08, 0x5D, 0x81, 0x2A, 0xE9, 0xC3, 0x45, 0xAD,
  29994. 0xF5, 0x09, 0xDD, 0xC7, 0x87, 0x61, 0x62, 0xCE,
  29995. 0x34, 0xA6, 0x00, 0x3F, 0xB4, 0x5C, 0xD8, 0x66,
  29996. 0x0F, 0xDD, 0x46, 0x71, 0x6E, 0x82, 0x55, 0x06,
  29997. 0x62, 0x37, 0x5D, 0xB5, 0x5E, 0x63, 0xA2, 0x0F,
  29998. 0x01, 0x2A, 0x71, 0xE3, 0xD9, 0x72, 0x25, 0xC5,
  29999. 0x8F, 0x52, 0x34, 0xC6, 0xB5, 0x3A, 0x8A, 0xA0,
  30000. 0x33, 0x9C, 0xEF, 0xD4, 0x84, 0xE4, 0x7A, 0x3D,
  30001. 0xE6, 0x49, 0x88, 0x05, 0xC2, 0x82, 0xE0, 0x76,
  30002. 0x8E, 0x35, 0xC7, 0x5C, 0x20, 0x07, 0x76, 0x5C,
  30003. 0xA1, 0x7F, 0x01, 0xD2, 0x85, 0xA7, 0xC3, 0x04,
  30004. 0x8B, 0xC7, 0x2B, 0xA3, 0xAA, 0x57, 0x7C, 0x1C,
  30005. 0x43, 0x2A, 0x3C, 0x76, 0x9D, 0x21, 0xB8, 0x6C,
  30006. 0x01, 0x8F, 0x81, 0x7A, 0x26, 0x67, 0x35, 0x1C,
  30007. 0x2B, 0xC8, 0x5F, 0xA5, 0x53, 0x6F, 0xA7, 0xA6,
  30008. 0xCD, 0xEF, 0xCC, 0x0D, 0xD6, 0xF9, 0x1B, 0xDD,
  30009. 0xE8, 0x9D, 0x4E, 0x51, 0x6C, 0x8B, 0x75, 0x40,
  30010. 0xA7, 0xDC, 0x3F, 0xC4, 0x89, 0x8A, 0xB3, 0x30,
  30011. 0x08, 0xBC, 0xC1, 0x9F, 0x9E, 0x46, 0x3D, 0xE9,
  30012. 0x36, 0x26, 0x99, 0x4C, 0xC9, 0x27, 0xA3, 0x4A,
  30013. 0x9D, 0x70, 0x40, 0xC1, 0x31, 0xC8, 0x1F, 0x07,
  30014. 0x83, 0xDD, 0x68, 0x84, 0x29, 0x43, 0xC5, 0x5D,
  30015. 0xC5, 0x77, 0xD3, 0x67, 0xA4, 0x01, 0x83, 0x94,
  30016. 0xBB, 0x11, 0x60, 0x72, 0xAC, 0x65, 0xF2, 0xEB,
  30017. 0x9B, 0x4F, 0xF5, 0x8F, 0x41, 0x15, 0x40, 0x1B,
  30018. 0x11, 0x7D, 0x91, 0x3B, 0x75, 0x75, 0x57, 0x57,
  30019. 0x19, 0x39, 0x0C, 0x9E, 0x80, 0x06, 0x97, 0xA8,
  30020. 0x28, 0xB1, 0x25, 0x83, 0xF9, 0x4A, 0x2F, 0x8F,
  30021. 0x72, 0x0A, 0x6E, 0x87, 0x00, 0xFD, 0x40, 0x7D,
  30022. 0x73, 0xA4, 0xC7, 0x97, 0x55, 0x2B, 0x98, 0x75,
  30023. 0x7A, 0x5F, 0x57, 0x82, 0xA9, 0x50, 0x95, 0xDF,
  30024. 0xA5, 0xAC, 0xB7, 0xD9, 0x3A, 0x80, 0x5C, 0x30,
  30025. 0x18, 0x30, 0x4E, 0x5D, 0x22, 0xB7, 0x6F, 0xA5,
  30026. 0xAA, 0x51, 0x96, 0xB7, 0xEB, 0xDB, 0x7A, 0xA0,
  30027. 0xA5, 0x2D, 0x06, 0xAC, 0x4B, 0x9E, 0x4C, 0x00,
  30028. 0xC0, 0xF8, 0x5C, 0xEC, 0xB0, 0xBD, 0x95, 0x56,
  30029. 0x2D, 0x56, 0x23, 0x30, 0x78, 0x51, 0x43, 0xC7,
  30030. 0xF1, 0x29, 0xFA, 0xF9, 0x19, 0x48, 0xB0, 0xC8,
  30031. 0x8C, 0x60, 0x82, 0xE0, 0xF3, 0x03, 0x54, 0x74,
  30032. 0x12, 0x6E, 0x23, 0x6B, 0x80, 0xD2, 0x22, 0xD2,
  30033. 0x81, 0x4F, 0x6A, 0xF2, 0x4B, 0xD5, 0xD1, 0x19,
  30034. 0x86, 0x33, 0x1F, 0xAF, 0xC2, 0x1E, 0xC8, 0x4C,
  30035. 0x93, 0xA8, 0x81, 0x24, 0x72, 0x51, 0x10, 0x5A,
  30036. 0x6A, 0xB4, 0x35, 0xD7, 0x6E, 0xFD, 0x88, 0x9D,
  30037. 0x27, 0xC0, 0x0C, 0x2A, 0x14, 0xBD, 0x04, 0xA6,
  30038. 0xBD, 0x0B, 0x20, 0x07, 0xB5, 0x51, 0x5A, 0x75,
  30039. 0xF1, 0x14, 0xD1, 0x92, 0x10, 0xBC, 0x70, 0x50,
  30040. 0x85, 0x33, 0x53, 0x2F, 0x1B, 0x0A, 0x30, 0x0C,
  30041. 0x8B, 0x12, 0xF0, 0x0A, 0xC4, 0xFC, 0x46, 0x0F,
  30042. 0xA8, 0x4A, 0x2D, 0x15, 0x21, 0xBD, 0x0B, 0x42,
  30043. 0x88, 0xD9, 0x2D, 0xD1, 0x2B, 0x17, 0x3E, 0x28,
  30044. 0x8B, 0x8A, 0x30, 0xC4, 0x9C, 0xA3, 0x56, 0x21,
  30045. 0x3C, 0x22, 0x4A, 0x03, 0x64, 0xDC, 0xF1, 0x8F,
  30046. 0xF0, 0x70, 0x0E, 0xAE, 0xBB, 0xB6, 0xD2, 0x02,
  30047. 0x03, 0x71, 0x14, 0x5A, 0x8A, 0xE8, 0x79, 0x59,
  30048. 0x72, 0x2D, 0x34, 0xF3, 0x3F, 0xAE, 0x00, 0x57,
  30049. 0x2D, 0xB3, 0xAC, 0x9A, 0x7B, 0xBD, 0xD4, 0x0B,
  30050. 0x75, 0x3C, 0x57, 0xA7, 0x3B, 0xA1, 0xB5, 0x43,
  30051. 0x12, 0x8A, 0x20, 0xDC, 0x98, 0x31, 0x69, 0x39,
  30052. 0xFE, 0xB8, 0x14, 0xCB, 0x17, 0x6C, 0x41, 0x64,
  30053. 0x77, 0x01, 0x2A, 0x97, 0x23, 0x67, 0x83, 0x46,
  30054. 0x7C, 0x0E, 0x15, 0x97, 0x33, 0xC5, 0xA0, 0x31,
  30055. 0x3C, 0xB2, 0x78, 0xD2, 0x31, 0x41, 0x2C, 0xB0,
  30056. 0x42, 0xC8, 0xE1, 0x16, 0xD6, 0x65, 0xA3, 0x7E,
  30057. 0xA4, 0x86, 0xEE, 0x07, 0x75, 0x44, 0xFC, 0x09,
  30058. 0xD5, 0x71, 0x56, 0x46, 0x3C, 0x1B, 0x10, 0xDC,
  30059. 0x49, 0x41, 0x0A, 0x3C, 0x0F, 0xE4, 0xB2, 0xEF,
  30060. 0x57, 0x1F, 0xAA, 0x56, 0x7A, 0xB5, 0x01, 0x59,
  30061. 0x58, 0xDB, 0xCF, 0x2C, 0xFA, 0x2C, 0xA7, 0x33,
  30062. 0x96, 0x3C, 0x34, 0x45, 0x7F, 0x85, 0x99, 0xCA,
  30063. 0x3A, 0xC8, 0xD2, 0xCB, 0x99, 0xA4, 0x57, 0x11,
  30064. 0x4E, 0x55, 0x9A, 0x9F, 0x82, 0xC4, 0x28, 0x47,
  30065. 0x99, 0xDE, 0x66, 0x15, 0x0A, 0x26, 0x56, 0x8F,
  30066. 0x95, 0xAF, 0x70, 0x65, 0x78, 0x8E, 0x62, 0x86,
  30067. 0x84, 0x60, 0x8C, 0x74, 0xDA, 0x57, 0x17, 0xB8,
  30068. 0x1F, 0x5D, 0x5C, 0x2F, 0x7D, 0x1A, 0x6C, 0xC0,
  30069. 0x71, 0x7C, 0xAB, 0xD2, 0xBA, 0xD2, 0x49, 0xB0,
  30070. 0x87, 0x58, 0x8F, 0x75, 0x65, 0x23, 0xFD, 0xF0,
  30071. 0x4E, 0x7F, 0x31, 0x76, 0x1B, 0x01, 0xAC, 0xB7,
  30072. 0xF6, 0x7E, 0xBF, 0x87, 0x62, 0xF5, 0xCB, 0x0B,
  30073. 0xC3, 0x72, 0xB4, 0xDD, 0x08, 0x3B, 0x06, 0x66,
  30074. 0x74, 0x02, 0x20, 0x4D, 0x08, 0x73, 0x9E, 0xB7,
  30075. 0x95, 0x20, 0xA9, 0x2C, 0x87, 0xC7, 0xE2, 0x2C,
  30076. 0x78, 0x77, 0x97, 0x3A, 0x0B, 0x79, 0xB0, 0xC7,
  30077. 0xAC, 0xCB, 0xBB, 0xAF, 0x88, 0xFB, 0xB0, 0x3D,
  30078. 0x4B, 0x6F, 0x5B, 0xB9, 0x87, 0x8A, 0xAB, 0x81,
  30079. 0x02, 0xF6, 0xB9, 0x1A, 0x27, 0x00, 0xD0, 0xEA,
  30080. 0xC3, 0x1D, 0x26, 0x03, 0x5B, 0xC1, 0x8A, 0x66,
  30081. 0xCB, 0x90, 0xEA, 0x24, 0x3F, 0xCF, 0x1A, 0x9A,
  30082. 0x1D, 0x24, 0x0C, 0x29, 0x0A, 0x54, 0x82, 0xA1,
  30083. 0x55, 0xC8, 0x00, 0x91, 0x13, 0xA0, 0x82, 0xDA,
  30084. 0xD5, 0xCE, 0x5C, 0xD2, 0x53, 0x03, 0x37, 0x02,
  30085. 0x51, 0x59, 0x3C, 0x12, 0x11, 0xAA, 0xB6, 0xC8,
  30086. 0x44, 0xC2, 0xC4, 0xB7, 0x99, 0xD4, 0x92, 0x80,
  30087. 0x00, 0x74, 0x63, 0x89, 0x48, 0xCB, 0x64, 0x31,
  30088. 0x0F, 0x38, 0xB4, 0x88, 0x51, 0xA6, 0x9E, 0x22,
  30089. 0x28, 0x70, 0x3C, 0x97, 0xFC, 0x20, 0x4E, 0x54,
  30090. 0x37, 0x21, 0x79, 0xAA, 0x84, 0xDE, 0xC1, 0xAC,
  30091. 0x68, 0xE6, 0xBC, 0x26, 0x8B, 0x9E, 0x36, 0x1B,
  30092. 0x8F, 0x6E, 0x0A, 0xBC, 0xFE, 0x88, 0x93, 0x37,
  30093. 0x87, 0x53, 0x48, 0xE4, 0x16, 0x4D, 0x66, 0x15,
  30094. 0xF3, 0x82, 0xC6, 0xC7, 0xC4, 0x17, 0x8F, 0x75,
  30095. 0x1F, 0x72, 0x2B, 0x21, 0x27, 0x74, 0x64, 0xFB,
  30096. 0x83, 0x1F, 0x8A, 0xA9, 0xA0, 0x18, 0xC1, 0x9A,
  30097. 0x51, 0xB0, 0x7F, 0xA0, 0x93, 0x9B, 0xA5, 0xC2,
  30098. 0x06, 0x68, 0x63, 0x23, 0xAF, 0xF6, 0xCA, 0x83,
  30099. 0x0B, 0x76, 0xE3, 0x80, 0x02, 0x63, 0x54, 0x47,
  30100. 0x7A, 0xAC, 0x58, 0xD8, 0x93, 0x1D, 0x80, 0x3B,
  30101. 0x6F, 0xAB, 0xD7, 0x12, 0xD1, 0x72, 0x81, 0x4A,
  30102. 0x54, 0xB8, 0x12, 0xE7, 0x92, 0x47, 0x94, 0xAA,
  30103. 0x40, 0x02, 0x49, 0xD5, 0xC0, 0x46, 0x8E, 0xA3,
  30104. 0x2F, 0xB2, 0x15, 0x54, 0xC5, 0x93, 0x67, 0x90,
  30105. 0x3C, 0x60, 0xA8, 0xB4, 0x8E, 0x23, 0x40, 0x71,
  30106. 0x2F, 0x7A, 0x04, 0x5A, 0x67, 0xC2, 0x3C, 0x72,
  30107. 0x41, 0x3A, 0x64, 0x69, 0xFC, 0x83, 0x69, 0xA2,
  30108. 0x91, 0x03, 0x3C, 0x46, 0xCE, 0xA3, 0x96, 0x76,
  30109. 0x65, 0x6B, 0x6C, 0xD8, 0x57, 0x99, 0x7C, 0x04,
  30110. 0x5C, 0x5A, 0xF0, 0x34, 0xE1, 0xE1, 0x14, 0x8A,
  30111. 0x8B, 0x40, 0xCD, 0x16, 0x68, 0x31, 0x93, 0xA3,
  30112. 0x60, 0x0C, 0x84, 0xFC, 0xE3, 0xB9, 0x65, 0x10,
  30113. 0x88, 0xDD, 0x09, 0x44, 0x57, 0x6C, 0x7E, 0xA4,
  30114. 0x07, 0x2D, 0x25, 0xF0, 0x67, 0xD2, 0x42, 0x7E,
  30115. 0x26, 0xD1, 0x86, 0xF9, 0xB7, 0x7A, 0x32, 0x79,
  30116. 0x05, 0x03, 0x3B, 0xA9, 0x40, 0x85, 0xA8, 0xAB,
  30117. 0x6A, 0x8A, 0x2B, 0x86, 0x08, 0x0C, 0x45, 0x2B,
  30118. 0x10, 0x90, 0xAC, 0xC9, 0x5A, 0x2B, 0x9F, 0x5A,
  30119. 0x6E, 0x61, 0x84, 0x6D, 0x63, 0x45, 0x66, 0xFA,
  30120. 0xC0, 0xB0, 0xFF, 0x35, 0x11, 0x18, 0x06, 0x18,
  30121. 0xAF, 0x98, 0x34, 0x87, 0x12, 0x2C, 0xFB, 0x63,
  30122. 0x61, 0xFF, 0x66, 0x0D, 0x5C, 0xA0, 0x0B, 0x9D,
  30123. 0x88, 0xB1, 0xDA, 0x18, 0x36, 0xC9, 0x78, 0x6E,
  30124. 0x55, 0x27, 0x6C, 0x88, 0x53, 0x8C, 0xC1, 0xD3,
  30125. 0x05, 0xFA, 0xAA, 0xB7, 0x79, 0x9A, 0x35, 0x46,
  30126. 0xF0, 0xBE, 0x6C, 0xC7, 0x00, 0xB0, 0x65, 0xB9,
  30127. 0x03, 0x4A, 0x91, 0x63, 0x31, 0x74, 0x66, 0xD9,
  30128. 0x83, 0xDD, 0xC2, 0xAA, 0x5B, 0xB0, 0x3F, 0x5B,
  30129. 0x67, 0x33, 0x48, 0xD5, 0x76, 0x02, 0x8A, 0x02,
  30130. 0x55, 0x15, 0x6C, 0x6A, 0x29, 0x74, 0x46, 0x98,
  30131. 0x34, 0xFB, 0x15, 0x73, 0x23, 0x74, 0x60, 0x54,
  30132. 0xC4, 0xBA, 0x96, 0x40, 0x30, 0x4A, 0x11, 0x63,
  30133. 0x04, 0xF0, 0x18, 0x21, 0x72, 0xBA, 0x8D, 0x03,
  30134. 0xAE, 0xDA, 0x39, 0x91, 0x6A, 0xF1, 0x18, 0xB9,
  30135. 0x84, 0x22, 0xB7, 0x16, 0x3A, 0xB6, 0xE2, 0x47,
  30136. 0xFC, 0xAA, 0x9D, 0x7C, 0x7C, 0xAE, 0x96, 0xBB,
  30137. 0x60, 0xB1, 0xD7, 0x86, 0x0E, 0x4C, 0x7F, 0x1D,
  30138. 0x92, 0x0D, 0x31, 0xA5, 0x41, 0x38, 0x82, 0x29,
  30139. 0xEE, 0x84, 0x2C, 0xA1, 0x59, 0x43, 0x89, 0x4A,
  30140. 0x56, 0xE5, 0x14, 0x0D, 0x29, 0x59, 0xC3, 0x6E,
  30141. 0xA2, 0x6E, 0x2D, 0xE0, 0x51, 0x21, 0x36, 0x6B,
  30142. 0xE4, 0x14, 0x02, 0x7B, 0xD1, 0x47, 0x81, 0x47,
  30143. 0x2E, 0xDD, 0xA4, 0x71, 0x10, 0xD0, 0xB3, 0xAE,
  30144. 0x2A, 0xA2, 0x55, 0xAB, 0x6D, 0x6F, 0xE0, 0x1C,
  30145. 0x26, 0xC2, 0x87, 0x63, 0x97, 0x6B, 0xBF, 0xA9,
  30146. 0x2F, 0x4F, 0x09, 0xA5, 0xB7, 0x3C, 0x47, 0x1F,
  30147. 0x8C, 0x9E, 0x53, 0x26, 0x92, 0x55, 0xDA, 0xC1,
  30148. 0xE0, 0x71, 0x5E, 0x87, 0x00, 0x06, 0x09, 0x70,
  30149. 0x60, 0xFF, 0x23, 0xC3, 0xAB, 0x43, 0xC4, 0x19,
  30150. 0xE4, 0x48, 0xC7, 0x30, 0x40, 0x10, 0xC4, 0x0F,
  30151. 0x3D, 0xC0, 0xB7, 0x26, 0x39, 0x2E, 0x46, 0x03,
  30152. 0x7E, 0xB9, 0x49, 0x04, 0x55, 0xE3, 0x49, 0xA4,
  30153. 0x35, 0xCD, 0xA9, 0x0A, 0xA3, 0xB7, 0x47, 0x8C,
  30154. 0x32, 0xE8, 0x72, 0xB2, 0xB8, 0x5C, 0xEC, 0xF0,
  30155. 0x3D, 0xDC, 0xD6, 0x16, 0x33, 0x78, 0xBF, 0x73,
  30156. 0xB1, 0x3B, 0x2E, 0x1A, 0x7B, 0x6E, 0xBC, 0x39,
  30157. 0x29, 0xEC, 0x08, 0x63, 0x89, 0x3F, 0x73, 0x8C,
  30158. 0x38, 0x9F, 0x3B, 0x3B, 0x2B, 0xB5, 0x4F, 0xC1,
  30159. 0x5B, 0xB7, 0x8A, 0x25, 0x31, 0xEE, 0xBC, 0xB7,
  30160. 0xBC, 0x10, 0x81, 0x07, 0x96, 0x59, 0x81, 0xD2,
  30161. 0x0A, 0x7D, 0x1A, 0x87, 0x21, 0xBA, 0x36, 0x40,
  30162. 0x03, 0x13, 0x2F, 0x03, 0x6C, 0xE4, 0x0C, 0x9E,
  30163. 0x75, 0x5B, 0x2A, 0xB4, 0x86, 0x1D, 0x04, 0xC1,
  30164. 0xBE, 0x0D, 0x53, 0x97, 0x91, 0x4B, 0x6C, 0xD4,
  30165. 0xAA, 0x40, 0x8C, 0x62, 0x2B, 0x21, 0xB1, 0x1B,
  30166. 0xA0, 0xEB, 0xA6, 0x17, 0x29, 0x59, 0x6F, 0x8B,
  30167. 0x0E, 0x82, 0x14, 0xAA, 0x4A, 0xA6, 0x01, 0x86,
  30168. 0x51, 0x07, 0xA2, 0xA7, 0x6A, 0xD7, 0xCC, 0x2F,
  30169. 0x97, 0x43, 0x00, 0x18, 0x28, 0xB8, 0x0B, 0x24,
  30170. 0x80, 0x2B, 0x98, 0x9D, 0x98, 0x42, 0x5E, 0x14,
  30171. 0x7C, 0x6D, 0x93, 0x32, 0xBA, 0x08, 0x4B, 0x8F,
  30172. 0xFB, 0x96, 0x27, 0xE3, 0xFB, 0xC4, 0xBE, 0x65,
  30173. 0x38, 0xEB, 0xDA, 0x1D, 0x9D, 0xB9, 0xB6, 0x75,
  30174. 0xB0, 0x16, 0x66, 0x57, 0x23, 0xD4, 0x11, 0x71,
  30175. 0x54, 0xF8, 0x13, 0x3E, 0xA9, 0x00, 0xCB, 0x47,
  30176. 0x0B, 0x1D, 0xC5, 0xB5, 0x7A, 0x78, 0x11, 0x16,
  30177. 0x72, 0x9E, 0xD6, 0x29, 0x45, 0x6E, 0x1A, 0x7D,
  30178. 0x6A, 0x37, 0x9F, 0x2E, 0x01, 0x0A, 0xCB, 0xEC,
  30179. 0x21, 0xFC, 0xD1, 0xC2, 0xB0, 0xBB, 0x61, 0xC3,
  30180. 0xDA, 0x6C, 0x73, 0x70, 0x5F, 0xD7, 0xB0, 0xC3,
  30181. 0x61, 0x2A, 0xC3, 0xB4, 0xA1, 0x2E, 0xC5, 0xD3,
  30182. 0x7F, 0x4C, 0x1A, 0x4A, 0x4D, 0xF8, 0x16, 0x6E,
  30183. 0xF6, 0x68, 0x9C, 0x06, 0xB5, 0xD4, 0xA6, 0x1A,
  30184. 0x52, 0x14, 0xAE, 0x5E, 0x5C, 0x28, 0x07, 0x4C,
  30185. 0xC5, 0xFE, 0x2A, 0x41, 0x2A, 0xC5, 0x14, 0x15,
  30186. 0x94, 0x28, 0x54, 0x85, 0x15, 0x6E, 0xCA, 0x65,
  30187. 0x64, 0x28, 0x37, 0x20, 0xB0, 0xBA, 0xD1, 0xD9,
  30188. 0x74, 0x5B, 0xA5, 0x77, 0x71, 0x1A, 0xB6, 0xF2,
  30189. 0x14, 0xC5, 0x2F, 0xB5, 0xBA, 0xAF, 0x8A, 0x1F,
  30190. 0x85, 0x33, 0x5A, 0xEB, 0x44, 0x37, 0x0B, 0x39,
  30191. 0xC5, 0x6C, 0x4C, 0x3D, 0x32, 0x75, 0x55, 0x60,
  30192. 0xCB, 0x2C, 0xA9, 0x45, 0x87, 0x51, 0xF1, 0x65,
  30193. 0x4B, 0x82, 0xC5, 0xE7, 0xFB, 0x0D, 0xB5, 0x16,
  30194. 0xBE, 0x1E, 0x42, 0x43, 0x3A, 0xD4, 0x7B, 0x6B,
  30195. 0xCC, 0xB3, 0x63, 0x61, 0xC4, 0x34, 0xEB, 0x59,
  30196. 0xCF, 0xF3, 0x38, 0x49, 0x54, 0xBB, 0x76, 0xF1,
  30197. 0x44, 0x14, 0x91, 0x67, 0x91, 0x3B, 0x85, 0x0F,
  30198. 0x75, 0x28, 0xBD, 0x76, 0x87, 0xB8, 0x8B, 0x64,
  30199. 0x84, 0xA6, 0x82, 0x68, 0x39, 0x97, 0x08, 0x42,
  30200. 0xA5, 0x93, 0xBB, 0x93, 0x45, 0x5A, 0x73, 0xBC,
  30201. 0x2C, 0x74, 0x62, 0x55, 0xC1, 0x3B, 0xC8, 0x52,
  30202. 0x1C, 0x73, 0x54, 0xE9, 0x8B, 0x98, 0x76, 0xA9,
  30203. 0x58, 0x6F, 0xC3, 0x7C, 0x5F, 0x24, 0x8D, 0xA0,
  30204. 0x55, 0x75, 0x8B, 0xD9, 0x61, 0x5E, 0x68, 0xBF,
  30205. 0x93, 0xEA, 0x0B, 0xA1, 0xD0, 0x22, 0xE2, 0xD2,
  30206. 0x22, 0x04, 0xA4, 0x45, 0x8C, 0xB6, 0x12, 0xB0,
  30207. 0x29, 0x41, 0xE8, 0x7A, 0xAC, 0xF1, 0xA3, 0x29,
  30208. 0x07, 0xD9, 0x8B, 0x4B, 0x13, 0xC6, 0xC9, 0xB4,
  30209. 0xAB, 0xD6, 0xDC, 0xCF, 0x66, 0x21, 0x06, 0xF5,
  30210. 0xD5, 0x8C, 0x2F, 0xC7, 0x51, 0x97, 0x35, 0x5E,
  30211. 0xB8, 0xCC, 0x41, 0x74, 0x2C, 0x6D, 0xA0, 0x03,
  30212. 0x40, 0x6D, 0x13, 0x56, 0xF8, 0xA0, 0x7A, 0x37,
  30213. 0x97, 0x88, 0x00, 0x60, 0x67, 0xCB, 0xF5, 0x2F,
  30214. 0x22, 0xD5, 0x3D, 0x68, 0x71, 0x22, 0x41, 0x09,
  30215. 0x34, 0x6E, 0xA8, 0x3B, 0x45, 0xCA, 0x60, 0xAF,
  30216. 0xEC, 0xCA, 0x0F, 0xA5, 0x67, 0x2B, 0x36, 0x64,
  30217. 0x89, 0xEB, 0x8D, 0xD1, 0xF7, 0x20, 0x4C, 0xDB,
  30218. 0x7D, 0x9B, 0xE1, 0x87, 0xF4, 0x39, 0x7B, 0x9B,
  30219. 0x4C, 0xA0, 0x31, 0x34, 0x75, 0x0D, 0xD5, 0x7D,
  30220. 0xAD, 0xE1, 0x88, 0x91, 0xCC, 0xA9, 0x47, 0x4B,
  30221. 0x65, 0x32, 0x60, 0x52, 0x50, 0xBC, 0xBB, 0x6E,
  30222. 0x02, 0x94, 0xEF, 0x95, 0x32, 0x6D, 0x25, 0x9B,
  30223. 0x08, 0xB2, 0x93, 0x8D, 0x8C, 0x59, 0xE2, 0x58,
  30224. 0x4B, 0x69, 0x10, 0x71, 0x85, 0xF3, 0x45, 0x30,
  30225. 0x24, 0x07, 0x35, 0x17, 0xC9, 0x89, 0x00, 0x9E,
  30226. 0xBB, 0x6B, 0x22, 0xED, 0xE9, 0x70, 0x43, 0x66,
  30227. 0xC6, 0x23, 0x2A, 0x62, 0x82, 0xF3, 0xBB, 0x09,
  30228. 0x08, 0x74, 0xA6, 0xFB, 0x0D, 0x84, 0x53, 0x6D,
  30229. 0x3F, 0x61, 0x3A, 0x21, 0xD1, 0x9D, 0x98, 0x44,
  30230. 0x17, 0xFA, 0xF0, 0x84, 0x6C, 0xB4, 0x28, 0x7B,
  30231. 0x3C, 0x70, 0x9F, 0x22, 0x62, 0x26, 0xA0, 0x9E,
  30232. 0x94, 0xFA, 0x89, 0x85, 0x43, 0x87, 0x4A, 0xE2,
  30233. 0x08, 0x11, 0xE3, 0x90, 0x80, 0xE6, 0xB2, 0xCE,
  30234. 0x49, 0x92, 0x26, 0xF5, 0x13, 0x8C, 0x14, 0x38,
  30235. 0x72, 0xDB, 0x86, 0xFB, 0xC2, 0x9C, 0xFD, 0xA2,
  30236. 0x9F, 0xD2, 0x26, 0x0E, 0xC7, 0xB9, 0xA2, 0x37,
  30237. 0xD1, 0x9A, 0xAB, 0x1B, 0x93, 0xE1, 0xDA, 0x4E,
  30238. 0xBC, 0x58, 0x9D, 0x82, 0x4C, 0x58, 0x90, 0x96,
  30239. 0x99, 0xD3, 0xE8, 0xBC, 0x7F, 0x53, 0xB3, 0x55,
  30240. 0xFA, 0x31, 0xE9, 0x6B, 0x1E, 0x35, 0x4A, 0xA4,
  30241. 0xB3, 0xC8, 0x4D, 0xF3, 0xC4, 0x7F, 0x80, 0xF9,
  30242. 0x32, 0xEA, 0x68, 0x5D, 0x96, 0xC5, 0xA5, 0x96,
  30243. 0x08, 0x22, 0xC6, 0x43, 0x64, 0x5F, 0x1A, 0x63,
  30244. 0xDD, 0x60, 0x8A, 0x09, 0x71, 0xC7, 0xF2, 0x15,
  30245. 0x7C, 0xA5, 0x24, 0x4D, 0x49, 0x2C, 0x01, 0x0B,
  30246. 0x71, 0xB7, 0x74, 0xC1, 0x0C, 0x07, 0xD3, 0xC8,
  30247. 0x4C, 0xA5, 0x4D, 0xE5, 0x00, 0x1F, 0xE0, 0xAC,
  30248. 0x4E, 0x69, 0x52, 0x81, 0x4E, 0xC1, 0x0F, 0xF9,
  30249. 0x8B, 0x19, 0xE7, 0x89, 0xC1, 0xAC, 0x68, 0x45,
  30250. 0xC7, 0x62, 0x80, 0x55, 0x27, 0x5C, 0x33, 0x34,
  30251. 0x41, 0x87, 0x1B, 0x8C, 0x30, 0x99, 0x9B, 0x05,
  30252. 0x31, 0x34, 0xC0, 0xE7, 0xB1, 0xAC, 0x6A, 0xA6,
  30253. 0x63, 0x12, 0xC1, 0xA5, 0xE2, 0x97, 0x92, 0x97,
  30254. 0x02, 0x56, 0x88, 0x14, 0x38, 0xDC, 0x46, 0x29,
  30255. 0x37, 0x4D, 0xFB, 0x9A, 0x91, 0x48, 0x62, 0x9D,
  30256. 0x14, 0x8C, 0x60, 0x1D, 0x4B, 0x6A, 0xA2, 0xC2,
  30257. 0x9C, 0xCE, 0xE8, 0x94, 0x7A, 0x27, 0x87, 0x0E,
  30258. 0x4C, 0x65, 0xE4, 0x12, 0x46, 0xD6, 0x25, 0x3D,
  30259. 0xC4, 0xF1, 0x3A, 0xEB, 0x31, 0xCD, 0x7E, 0x36,
  30260. 0x2F, 0x0F, 0x28, 0x84, 0xB9, 0xF7, 0x3B, 0xA9,
  30261. 0x93, 0x50, 0xAA, 0xA4, 0x02, 0x4A, 0x73, 0x14,
  30262. 0xBE, 0x69, 0x3A, 0xE4, 0x13, 0xA5, 0xCE, 0xA0,
  30263. 0x9A, 0x27, 0x27, 0x3A, 0x0B, 0xF6, 0xA4, 0x53,
  30264. 0xF8, 0x63, 0x9B, 0x48, 0x26, 0x8D, 0x21, 0xC9,
  30265. 0xAE, 0xC2, 0x62, 0xBE, 0x28, 0xC0, 0x7E, 0xA4,
  30266. 0xB9, 0x35, 0xA8, 0x69, 0x63, 0x26, 0xAA, 0x18,
  30267. 0xA5, 0x91, 0xCE, 0xE9, 0xC9, 0xF2, 0xA7, 0x29,
  30268. 0xBC, 0x50, 0x33, 0xDD, 0xE6, 0x37, 0x27, 0x38,
  30269. 0x85, 0x92, 0x1B, 0x73, 0xB8, 0x58, 0xA1, 0x88,
  30270. 0x33, 0xA7, 0xB8, 0x3A, 0x82, 0xFE, 0x02, 0xCC,
  30271. 0x33, 0x54, 0xC3, 0x0B, 0x0C, 0x4D, 0x0D, 0x3B,
  30272. 0xA1, 0x33, 0x1C, 0x6E, 0xC7, 0xA7, 0x0B, 0x7E,
  30273. 0x50, 0x3F, 0x76, 0x47, 0xA9, 0x52, 0xB0, 0x03,
  30274. 0x86, 0xCC, 0x90, 0xB3, 0x4A, 0x51, 0x4F, 0x99,
  30275. 0x5A, 0x56, 0x92, 0x05, 0xEB, 0x2A, 0x3F, 0xA7,
  30276. 0x9A, 0x78, 0x65, 0x21, 0x18, 0xD4, 0xBC, 0x48,
  30277. 0x46, 0x27, 0xCD, 0x02, 0x33, 0xBC, 0x5A, 0x35,
  30278. 0x16, 0x15, 0x0B, 0xAF, 0x6E, 0x10, 0x66, 0x5D,
  30279. 0xA7, 0x61, 0x8F, 0x28, 0x41, 0xB5, 0x02, 0x61,
  30280. 0x72, 0x48, 0x89, 0x3B, 0xAA, 0xB7, 0xE9, 0x03,
  30281. 0x89, 0x0D, 0xF0, 0xCA, 0xC2, 0x84, 0xCD, 0x11,
  30282. 0x65, 0xB4, 0x64, 0xC1, 0x8F, 0x4C, 0xFA, 0x8F,
  30283. 0x69, 0x2D, 0x3C, 0x8E, 0x8C, 0x73, 0x9F, 0xB3,
  30284. 0x78, 0x27, 0x74, 0x15, 0x61, 0xD5, 0xE7, 0x66,
  30285. 0x67, 0x51, 0x82, 0xFF, 0x83, 0xC9, 0x1C, 0xF0,
  30286. 0x33, 0x65, 0x88, 0x23, 0xD8, 0xFA, 0x91, 0xE1,
  30287. 0xE8, 0xD6, 0x62, 0xEA, 0xD3, 0x75, 0x0D, 0x71,
  30288. 0x6B, 0x91, 0x8D, 0x26, 0x78, 0x26, 0x59, 0xA5,
  30289. 0xB3, 0xD7, 0x99, 0xE4, 0x26, 0x58, 0xE1, 0x49,
  30290. 0x5F, 0x90, 0x84, 0xFF, 0xD4, 0xD1, 0x00, 0xB8,
  30291. 0x86, 0x26, 0xED, 0x79, 0xD4, 0x51, 0x14, 0x08,
  30292. 0x00, 0xE0, 0x3B, 0x59, 0xB9, 0x56, 0xF8, 0x21,
  30293. 0x0E, 0x55, 0x60, 0x67, 0x40, 0x7D, 0x13, 0xDC,
  30294. 0x90, 0xFA, 0x9E, 0x8B, 0x87, 0x2B, 0xFB, 0x8F
  30295. };
  30296. const byte kyber1024_ct[] = {
  30297. 0x17, 0x50, 0x59, 0x78, 0xD5, 0xCA, 0x04, 0x62,
  30298. 0x81, 0xC9, 0x7C, 0xA1, 0xFE, 0x89, 0xBA, 0xB3,
  30299. 0x99, 0xC1, 0xC9, 0xF8, 0xED, 0x27, 0x1D, 0xC5,
  30300. 0x1B, 0x30, 0x09, 0x43, 0xDB, 0x2E, 0xA9, 0x49,
  30301. 0x08, 0xEE, 0x94, 0x64, 0x61, 0x96, 0x02, 0xA7,
  30302. 0xF8, 0xCD, 0x38, 0xB5, 0xA3, 0x12, 0x5F, 0x46,
  30303. 0x7B, 0xF1, 0xA7, 0xA9, 0x67, 0xEE, 0xE0, 0x84,
  30304. 0x20, 0x98, 0x80, 0x14, 0x33, 0x88, 0x38, 0x92,
  30305. 0xAD, 0x0A, 0xD2, 0x99, 0x6F, 0x76, 0xCE, 0x49,
  30306. 0xA1, 0x79, 0x0A, 0x7A, 0x23, 0x0E, 0xB0, 0xDA,
  30307. 0x07, 0x72, 0xC6, 0xFC, 0x06, 0x0E, 0x75, 0x51,
  30308. 0x8C, 0x94, 0xA1, 0xA6, 0x52, 0xD8, 0x66, 0x0F,
  30309. 0xBA, 0x70, 0x1E, 0xF4, 0x82, 0xF4, 0xFA, 0x26,
  30310. 0xA7, 0xC4, 0xDB, 0x86, 0x02, 0xB7, 0x89, 0x49,
  30311. 0xB2, 0x8D, 0x99, 0xC7, 0x3B, 0x84, 0xD5, 0x5C,
  30312. 0xF6, 0x80, 0xA5, 0x90, 0x89, 0x3C, 0x63, 0xEA,
  30313. 0x8F, 0x85, 0x45, 0x8B, 0x85, 0xF7, 0x39, 0x2C,
  30314. 0x11, 0xB8, 0x90, 0x02, 0x02, 0x73, 0xB7, 0x48,
  30315. 0x33, 0xA8, 0x0C, 0xD6, 0xC7, 0x8C, 0x75, 0x51,
  30316. 0xD7, 0xCC, 0x8A, 0xA1, 0xB4, 0xC5, 0x42, 0x8D,
  30317. 0x2A, 0xA3, 0xE7, 0x43, 0x5E, 0x23, 0xDD, 0x9B,
  30318. 0x4F, 0xFA, 0xB0, 0xD0, 0x21, 0x90, 0xBB, 0xA1,
  30319. 0x36, 0x89, 0xF0, 0x15, 0xC1, 0x9A, 0x3A, 0xD9,
  30320. 0xD6, 0x62, 0x69, 0x77, 0xD1, 0xA3, 0x35, 0x58,
  30321. 0xAB, 0x6C, 0xF4, 0x6F, 0x76, 0xBD, 0x5A, 0x30,
  30322. 0xED, 0x94, 0x6D, 0x04, 0x03, 0x38, 0x8B, 0x13,
  30323. 0xA5, 0xB4, 0x95, 0x83, 0xCF, 0xB9, 0x3D, 0xEB,
  30324. 0x30, 0xC4, 0x63, 0x82, 0xAF, 0xC1, 0x28, 0x62,
  30325. 0xF0, 0x69, 0xF1, 0x36, 0xD5, 0xCF, 0x5F, 0x30,
  30326. 0x06, 0x4D, 0x90, 0xDE, 0x12, 0xF7, 0xD7, 0x6E,
  30327. 0xD7, 0xF4, 0xA5, 0x93, 0x13, 0x9C, 0xB6, 0x77,
  30328. 0x93, 0xF2, 0xAD, 0x94, 0xDB, 0xE1, 0xBA, 0xD2,
  30329. 0x3B, 0xC1, 0x7C, 0x72, 0x66, 0x9C, 0x25, 0x14,
  30330. 0x9A, 0xC8, 0xA2, 0x5C, 0x50, 0xE0, 0xE0, 0x8F,
  30331. 0x5D, 0x76, 0x4B, 0x30, 0xF2, 0x87, 0x07, 0x47,
  30332. 0x0C, 0x5E, 0xC0, 0x9E, 0xDD, 0x21, 0x4C, 0x8A,
  30333. 0x0A, 0x82, 0x8C, 0x2C, 0x8B, 0x6F, 0xCF, 0x74,
  30334. 0xE9, 0x47, 0x36, 0x62, 0xFC, 0x61, 0xC7, 0x28,
  30335. 0x1E, 0x9B, 0xA7, 0x23, 0x04, 0x6A, 0x0A, 0xE7,
  30336. 0x0D, 0x7F, 0xEB, 0x86, 0xFC, 0xFF, 0x4D, 0x87,
  30337. 0x6B, 0x10, 0xEF, 0x2B, 0x67, 0x73, 0xE2, 0x6C,
  30338. 0xB2, 0x52, 0xDF, 0x75, 0x73, 0x07, 0x68, 0x0E,
  30339. 0xCC, 0x47, 0xD8, 0xEC, 0x07, 0xB6, 0xA5, 0x09,
  30340. 0xE2, 0x74, 0x51, 0x31, 0x77, 0xC6, 0xEE, 0x1B,
  30341. 0x91, 0x7B, 0x0C, 0x3E, 0x77, 0xCC, 0xF6, 0x2F,
  30342. 0x41, 0x2A, 0x95, 0x69, 0x57, 0x3B, 0xEB, 0xA1,
  30343. 0x54, 0xB1, 0x51, 0x47, 0xEF, 0x2E, 0x52, 0xA3,
  30344. 0x9C, 0xD0, 0xC6, 0x4B, 0xFD, 0x81, 0xEA, 0xA2,
  30345. 0x1B, 0x50, 0xAB, 0x9A, 0x11, 0x56, 0xBD, 0x41,
  30346. 0xDD, 0xB9, 0x93, 0x87, 0x24, 0xD1, 0x71, 0x39,
  30347. 0x39, 0x3F, 0x2D, 0x36, 0x1A, 0xAE, 0xE6, 0x76,
  30348. 0xB7, 0xF5, 0xA2, 0xBB, 0x45, 0x6B, 0xB5, 0x9C,
  30349. 0xF0, 0x4A, 0xC1, 0x7F, 0x0B, 0xD3, 0x14, 0x83,
  30350. 0xCD, 0x45, 0xC7, 0x5B, 0xF7, 0xD5, 0xD2, 0x7D,
  30351. 0x9A, 0x3A, 0x5D, 0x4E, 0x49, 0x1F, 0x1E, 0x3C,
  30352. 0x73, 0xE9, 0xD5, 0xC3, 0x46, 0x02, 0x3F, 0x5E,
  30353. 0xAC, 0x39, 0x1B, 0xBE, 0xE3, 0x2A, 0xD8, 0x01,
  30354. 0xA5, 0x9D, 0xFC, 0x03, 0x58, 0x26, 0x51, 0x11,
  30355. 0xE0, 0x3D, 0xD6, 0x01, 0xA8, 0x66, 0x5C, 0xE3,
  30356. 0xEC, 0xE5, 0x23, 0x1F, 0x1F, 0x5F, 0x5B, 0x47,
  30357. 0xEF, 0x38, 0x1B, 0x47, 0xBC, 0x18, 0xE4, 0xDE,
  30358. 0x73, 0xD0, 0xD5, 0x68, 0x10, 0x91, 0x33, 0x5E,
  30359. 0x8A, 0xF7, 0xE1, 0x2C, 0x0E, 0xA8, 0x92, 0x24,
  30360. 0xA2, 0x0F, 0x5F, 0x18, 0x66, 0x64, 0x31, 0x9B,
  30361. 0xFB, 0x55, 0x5F, 0x5F, 0x5D, 0x07, 0x41, 0x33,
  30362. 0x77, 0xB3, 0x41, 0x6F, 0x91, 0x35, 0x66, 0xB8,
  30363. 0xFF, 0x31, 0x48, 0x11, 0xD3, 0x3D, 0x42, 0xE4,
  30364. 0xFE, 0xE5, 0x17, 0xF3, 0xD2, 0xB9, 0x82, 0x8F,
  30365. 0x76, 0x81, 0x18, 0x03, 0x67, 0x3E, 0x86, 0x5A,
  30366. 0xFB, 0xA6, 0x64, 0xB7, 0x5D, 0x4A, 0x8B, 0xC7,
  30367. 0xA1, 0x36, 0xA6, 0x8C, 0x81, 0x39, 0x1E, 0x43,
  30368. 0x20, 0x87, 0xDE, 0x28, 0x51, 0xE5, 0xCB, 0x26,
  30369. 0xBF, 0xF6, 0x5D, 0x70, 0xBC, 0x0C, 0xDC, 0x35,
  30370. 0x9F, 0xE5, 0xFA, 0x0A, 0x45, 0x94, 0x80, 0x9C,
  30371. 0xFC, 0x0F, 0xC6, 0x4F, 0x0A, 0x46, 0x98, 0x2E,
  30372. 0xA1, 0xEA, 0x78, 0xA1, 0x8E, 0x1D, 0xCE, 0x00,
  30373. 0xCD, 0x2F, 0xE0, 0x7D, 0xC4, 0xE6, 0xC7, 0x04,
  30374. 0x50, 0x4B, 0x08, 0x33, 0xAA, 0x16, 0xD3, 0xB1,
  30375. 0x5D, 0xA5, 0x68, 0x26, 0x35, 0x70, 0x46, 0x66,
  30376. 0x0D, 0x47, 0x2C, 0x66, 0xAB, 0xEB, 0xD7, 0xFD,
  30377. 0x48, 0xFD, 0x62, 0x0D, 0x37, 0x9F, 0x2A, 0xEF,
  30378. 0x93, 0x99, 0x3B, 0xB8, 0xAA, 0x56, 0x7D, 0x9A,
  30379. 0x5A, 0x12, 0x94, 0xB4, 0x69, 0x80, 0xC0, 0x02,
  30380. 0x3B, 0xB8, 0x94, 0xD3, 0x70, 0x94, 0xF2, 0x95,
  30381. 0x6C, 0x8D, 0x1D, 0x8F, 0xC3, 0xF1, 0x98, 0x09,
  30382. 0x93, 0x54, 0x7E, 0xA4, 0x8F, 0xD4, 0xCB, 0xDB,
  30383. 0xD0, 0x97, 0xB3, 0xEE, 0xFA, 0xF8, 0x65, 0x75,
  30384. 0xEB, 0xBD, 0x23, 0xDB, 0xB2, 0x63, 0x04, 0xA0,
  30385. 0x9B, 0x1D, 0xE0, 0x55, 0xB7, 0x8A, 0x5F, 0xC4,
  30386. 0xBD, 0x41, 0x13, 0x27, 0x41, 0xD3, 0x87, 0xAC,
  30387. 0x3A, 0x58, 0x22, 0x19, 0xFE, 0x39, 0xC5, 0x33,
  30388. 0x2F, 0x8A, 0xD8, 0x8E, 0xB4, 0x9F, 0x9E, 0xEA,
  30389. 0xCB, 0x7C, 0xD7, 0xD5, 0x34, 0x47, 0xAB, 0x62,
  30390. 0x48, 0xBA, 0xA8, 0x22, 0x7F, 0xA1, 0xB2, 0x4D,
  30391. 0x60, 0xE8, 0xBA, 0xAC, 0xB1, 0x9D, 0x00, 0x40,
  30392. 0x24, 0xAF, 0x18, 0x6C, 0xAD, 0x86, 0xAC, 0xA0,
  30393. 0xD6, 0xAA, 0x8E, 0x8C, 0x36, 0x4E, 0xBD, 0xCB,
  30394. 0x43, 0x27, 0xC3, 0x6D, 0x30, 0x88, 0xDC, 0x80,
  30395. 0xCE, 0x04, 0xF3, 0xA0, 0xA7, 0x9C, 0x32, 0xAB,
  30396. 0x6C, 0x3F, 0x14, 0xF4, 0xD6, 0x29, 0x14, 0x69,
  30397. 0x2E, 0x4A, 0x9A, 0xFF, 0xD1, 0x96, 0x09, 0x14,
  30398. 0x05, 0x62, 0x33, 0x3A, 0x05, 0x65, 0x96, 0x2B,
  30399. 0xD8, 0xAC, 0xD2, 0x59, 0x50, 0xF5, 0x6C, 0x94,
  30400. 0x2C, 0xE1, 0x5C, 0x38, 0x1C, 0x95, 0x03, 0xD7,
  30401. 0x1C, 0xF1, 0x7C, 0x5E, 0x97, 0xA6, 0xAE, 0xFB,
  30402. 0x3F, 0xCF, 0x48, 0x80, 0x4E, 0x3F, 0x07, 0x5D,
  30403. 0x71, 0xD5, 0x6F, 0x44, 0x49, 0x93, 0x9A, 0xF0,
  30404. 0x47, 0x86, 0xD6, 0x2D, 0x12, 0xAD, 0x67, 0x06,
  30405. 0x65, 0x3E, 0xE9, 0x68, 0x84, 0x0E, 0xFC, 0xCE,
  30406. 0x28, 0x72, 0x57, 0x64, 0xE3, 0x07, 0xC0, 0x0F,
  30407. 0x0C, 0xBA, 0x57, 0x5A, 0x33, 0xC9, 0x6C, 0x6F,
  30408. 0x5A, 0x74, 0x9E, 0x59, 0x94, 0x5B, 0x92, 0xB3,
  30409. 0x0F, 0xC8, 0x99, 0xDE, 0x61, 0xD1, 0x1C, 0xCF,
  30410. 0xBF, 0xEF, 0x98, 0x3B, 0xF8, 0x54, 0xC8, 0xF5,
  30411. 0x41, 0x06, 0xB3, 0x03, 0x62, 0x4D, 0x79, 0xF0,
  30412. 0x66, 0x58, 0xFB, 0x4B, 0xDC, 0x69, 0x6C, 0xE0,
  30413. 0x59, 0x77, 0xDE, 0x4A, 0x11, 0x5D, 0x83, 0x8D,
  30414. 0x83, 0x50, 0xF3, 0xC2, 0xBE, 0x35, 0xA6, 0x54,
  30415. 0xCA, 0xBB, 0x1E, 0xBD, 0x46, 0xA9, 0xF0, 0x79,
  30416. 0xBA, 0xE4, 0x6B, 0x31, 0xEE, 0x45, 0x74, 0x26,
  30417. 0xF3, 0x97, 0x9E, 0x36, 0x61, 0x1F, 0xAF, 0x16,
  30418. 0x87, 0x0A, 0xBE, 0x9D, 0x8F, 0x93, 0xB5, 0x0A,
  30419. 0xC1, 0x49, 0x98, 0xE1, 0xA4, 0x95, 0xF5, 0x1A,
  30420. 0x1B, 0x02, 0x05, 0x6A, 0x2F, 0xFB, 0x17, 0x11,
  30421. 0x4C, 0xA7, 0x21, 0xAC, 0x24, 0x6C, 0x29, 0x20,
  30422. 0xBC, 0xEA, 0x3A, 0x72, 0x36, 0x7A, 0x0D, 0xA5,
  30423. 0x02, 0xE3, 0xF2, 0x5F, 0xFB, 0x3A, 0x58, 0x10,
  30424. 0x01, 0xAC, 0xFC, 0xDD, 0x91, 0x7E, 0xA7, 0x89,
  30425. 0xE4, 0xEF, 0x88, 0x43, 0x4D, 0xFE, 0x20, 0xBC,
  30426. 0x54, 0x78, 0x42, 0x8D, 0xD0, 0x80, 0xA0, 0xCA,
  30427. 0xDD, 0xC6, 0x1F, 0x02, 0x32, 0x03, 0x15, 0x1C,
  30428. 0x5A, 0x82, 0xD4, 0x95, 0x21, 0x8F, 0xF3, 0xFB,
  30429. 0x1E, 0x22, 0x7D, 0xBF, 0xCA, 0x1B, 0x5A, 0xBC,
  30430. 0xE6, 0x39, 0xC4, 0x9C, 0xD8, 0xEC, 0x12, 0x95,
  30431. 0x83, 0xC5, 0x73, 0xC5, 0x17, 0x02, 0xA1, 0xB5,
  30432. 0xC1, 0x01, 0x72, 0x13, 0x7C, 0x36, 0xDD, 0x66,
  30433. 0xD1, 0x84, 0x99, 0xB0, 0x78, 0x7D, 0xAB, 0xA9,
  30434. 0x78, 0xAC, 0x76, 0xC1, 0x14, 0xD0, 0x17, 0x8C,
  30435. 0xCB, 0xF7, 0xE6, 0xE1, 0x31, 0xBD, 0x9E, 0x53,
  30436. 0x3A, 0x13, 0x33, 0xD2, 0x2B, 0xD1, 0x7B, 0x99,
  30437. 0x6F, 0x76, 0x22, 0x57, 0x5C, 0xF9, 0x8B, 0x37,
  30438. 0x0D, 0x77, 0x17, 0xB7, 0x24, 0x48, 0x7B, 0xFB,
  30439. 0x27, 0x74, 0x62, 0xC8, 0x3C, 0x1C, 0xAA, 0xF6,
  30440. 0x28, 0x85, 0x7E, 0x71, 0xD6, 0x16, 0xDD, 0x5D,
  30441. 0x52, 0x87, 0xBD, 0xF3, 0x7B, 0x19, 0xDA, 0xAA,
  30442. 0xE4, 0xD2, 0x4C, 0xCC, 0xEC, 0xB1, 0x26, 0xAE,
  30443. 0x74, 0x25, 0x19, 0x7A, 0x21, 0x51, 0x13, 0x91,
  30444. 0xBA, 0x1E, 0x7D, 0x13, 0x08, 0x49, 0x82, 0xED,
  30445. 0x29, 0x1D, 0xDD, 0x51, 0x6A, 0x60, 0xED, 0xCD,
  30446. 0x86, 0x26, 0xF0, 0x0E, 0xEF, 0x6A, 0xA4, 0xD3,
  30447. 0x76, 0xE1, 0x53, 0x90, 0x64, 0x88, 0x46, 0x64,
  30448. 0x09, 0xCC, 0x26, 0x50, 0x82, 0xAA, 0xB0, 0x4E,
  30449. 0x5E, 0xDC, 0x8B, 0xE2, 0x45, 0xF5, 0x38, 0xC3,
  30450. 0x6E, 0x7E, 0x0A, 0xE3, 0xE7, 0x10, 0x9E, 0x25,
  30451. 0xDA, 0xF8, 0x09, 0x19, 0xCE, 0xD7, 0x30, 0xA6,
  30452. 0x93, 0xFF, 0xF8, 0xD2, 0x71, 0x6F, 0xB2, 0x27,
  30453. 0x15, 0x93, 0x17, 0xE0, 0x7D, 0x9D, 0xAE, 0x5B,
  30454. 0xDA, 0x9B, 0x01, 0x05, 0xB1, 0x7B, 0xAE, 0x55,
  30455. 0xA7, 0x37, 0x56, 0x6D, 0xC4, 0x4E, 0x63, 0xA4,
  30456. 0x7E, 0xBC, 0xA5, 0xA4, 0xE9, 0xB2, 0xB8, 0x06,
  30457. 0x8C, 0xB8, 0x13, 0xBD, 0xD2, 0xC3, 0xFD, 0x73,
  30458. 0x90, 0x81, 0x0C, 0xCC, 0x2C, 0xA1, 0x48, 0xEF,
  30459. 0xAF, 0x14, 0x59, 0xD7, 0x39, 0xD2, 0x2A, 0x12,
  30460. 0x14, 0x83, 0x92, 0xEA, 0x0D, 0x9A, 0xC3, 0x76,
  30461. 0x10, 0xD5, 0x61, 0x29, 0x1E, 0xE3, 0x88, 0x06,
  30462. 0x09, 0x42, 0xB0, 0xD0, 0x12, 0xB1, 0xBC, 0x52,
  30463. 0x34, 0xF5, 0x77, 0xF5, 0xF3, 0xF3, 0x4A, 0xFA,
  30464. 0x77, 0x9C, 0x15, 0x5E, 0x72, 0x9A, 0x57, 0xD6,
  30465. 0x45, 0xF8, 0x8E, 0x88, 0x23, 0xC1, 0xE7, 0xC5,
  30466. 0x5B, 0x87, 0xB7, 0xEE, 0x06, 0x89, 0x0B, 0x9E,
  30467. 0xD8, 0x13, 0x04, 0x33, 0xC7, 0x57, 0x93, 0x2C,
  30468. 0x4A, 0xAA, 0x8E, 0x7A, 0x26, 0x3E, 0x59, 0x8C,
  30469. 0x87, 0xEE, 0x0D, 0xFE, 0x7E, 0x12, 0x0E, 0x60,
  30470. 0x7C, 0x0F, 0xED, 0x41, 0x72, 0x11, 0xC5, 0x3B,
  30471. 0x10, 0xA7, 0x12, 0x24, 0x61, 0x56, 0xCA, 0xB4,
  30472. 0xBB, 0xCB, 0x46, 0xF2, 0xA4, 0x1F, 0xBB, 0x6B,
  30473. 0xD3, 0x67, 0x3D, 0x4E, 0x89, 0xDA, 0xE8, 0x88,
  30474. 0xD0, 0x35, 0xF8, 0xE5, 0x2A, 0xEA, 0x4D, 0x58,
  30475. 0x41, 0x1D, 0x5E, 0x24, 0xFA, 0x6C, 0x4A, 0x72,
  30476. 0x17, 0x4A, 0xDA, 0xA0, 0xD8, 0x85, 0xFA, 0x64,
  30477. 0xB1, 0x2B, 0x16, 0xEF, 0x85, 0x65, 0xDD, 0xE9,
  30478. 0x84, 0x56, 0xD6, 0x72, 0xBB, 0x52, 0xF6, 0x00,
  30479. 0x7B, 0x23, 0xF4, 0x74, 0x8F, 0x63, 0xA2, 0x48,
  30480. 0xAF, 0x1A, 0xB1, 0x86, 0xEA, 0xAC, 0xBD, 0xA1,
  30481. 0xC2, 0xB0, 0xB4, 0x77, 0xC6, 0x25, 0xA0, 0x0C,
  30482. 0xFF, 0x14, 0xFA, 0xD3, 0xCF, 0xC9, 0xFC, 0x2C,
  30483. 0xF2, 0xAE, 0x7E, 0x4B, 0x55, 0xE0, 0x41, 0xF8,
  30484. 0x5E, 0x2D, 0x03, 0xD3, 0x60, 0x25, 0xAB, 0x4D,
  30485. 0xF6, 0x65, 0x12, 0xBC, 0xE9, 0x66, 0x7F, 0x2A,
  30486. 0x2D, 0xF9, 0xC9, 0xAA, 0xE2, 0x80, 0x39, 0x7F,
  30487. 0xEF, 0x0C, 0x03, 0x8B, 0x54, 0x7A, 0xE5, 0x87,
  30488. 0xE7, 0xBE, 0x80, 0x22, 0x37, 0x96, 0xE3, 0x9A,
  30489. 0x93, 0x99, 0x99, 0x9A, 0xE5, 0x7A, 0xD6, 0x80,
  30490. 0x26, 0xF1, 0x3F, 0xCA, 0x9E, 0xE6, 0x33, 0x79,
  30491. 0xC9, 0x09, 0x9A, 0xCB, 0x2A, 0x61, 0x19, 0x04,
  30492. 0x74, 0x40, 0x5B, 0x57, 0xE3, 0x48, 0x4A, 0x52
  30493. };
  30494. const byte kyber1024_ss[] = {
  30495. 0xC1, 0x4E, 0xBD, 0x6E, 0x37, 0x88, 0xA6, 0x41,
  30496. 0xD1, 0x75, 0x5B, 0x4C, 0x86, 0x9C, 0x46, 0x99,
  30497. 0x4F, 0x75, 0xAF, 0x16, 0xB4, 0x0F, 0x8F, 0x64,
  30498. 0x92, 0xCC, 0xBE, 0xC7, 0xA3, 0xDA, 0x9B, 0xFE
  30499. };
  30500. #endif
  30501. ret = wc_KyberKey_Init(KYBER1024, &key, HEAP_HINT, INVALID_DEVID);
  30502. if (ret != 0)
  30503. return -20301;
  30504. ret = wc_KyberKey_MakeKeyWithRandom(&key, kyber1024_rand,
  30505. sizeof(kyber1024_rand));
  30506. if (ret != 0)
  30507. return -20302;
  30508. ret = wc_KyberKey_EncodePublicKey(&key, pub, sizeof(pub));
  30509. if (ret != 0)
  30510. return -20303;
  30511. ret = wc_KyberKey_EncodePrivateKey(&key, priv, sizeof(priv));
  30512. if (ret != 0)
  30513. return -20304;
  30514. if (XMEMCMP(pub, kyber1024_pk, sizeof(kyber1024_pk)) != 0)
  30515. return -20305;
  30516. if (XMEMCMP(priv, kyber1024_sk, sizeof(kyber1024_sk)) != 0)
  30517. return -20306;
  30518. ret = wc_KyberKey_EncapsulateWithRandom(&key, ct, ss, kyber1024enc_rand,
  30519. sizeof(kyber1024enc_rand));
  30520. if (ret != 0)
  30521. return -20307;
  30522. if (XMEMCMP(ct, kyber1024_ct, sizeof(kyber1024_ct)) != 0)
  30523. return -20308;
  30524. if (XMEMCMP(ss, kyber1024_ss, sizeof(kyber1024_ss)) != 0)
  30525. return -20309;
  30526. ret = wc_KyberKey_Decapsulate(&key, ss_dec, ct, sizeof(kyber1024_ct));
  30527. if (ret != 0)
  30528. return -20310;
  30529. if (XMEMCMP(ss_dec, kyber1024_ss, sizeof(kyber1024_ss)) != 0)
  30530. return -20311;
  30531. wc_KyberKey_Free(&key);
  30532. return 0;
  30533. }
  30534. #endif /* WOLFSSL_KYBER1024 */
  30535. #endif /* WOLFSSL_WC_KYBER */
  30536. WOLFSSL_TEST_SUBROUTINE int kyber_test(void)
  30537. {
  30538. int ret;
  30539. KyberKey key;
  30540. WC_RNG rng;
  30541. int i;
  30542. byte priv[KYBER_MAX_PRIVATE_KEY_SIZE];
  30543. byte pub[KYBER_MAX_PUBLIC_KEY_SIZE];
  30544. byte priv2[KYBER_MAX_PRIVATE_KEY_SIZE];
  30545. byte pub2[KYBER_MAX_PUBLIC_KEY_SIZE];
  30546. byte ct[KYBER_MAX_CIPHER_TEXT_SIZE];
  30547. byte ss[KYBER_SS_SZ];
  30548. byte ss_dec[KYBER_SS_SZ];
  30549. int testData[][4] = {
  30550. #ifdef WOLFSSL_KYBER512
  30551. { KYBER512, KYBER512_PRIVATE_KEY_SIZE, KYBER512_PUBLIC_KEY_SIZE,
  30552. KYBER512_CIPHER_TEXT_SIZE },
  30553. #endif
  30554. #ifdef WOLFSSL_KYBER768
  30555. { KYBER768, KYBER768_PRIVATE_KEY_SIZE, KYBER768_PUBLIC_KEY_SIZE,
  30556. KYBER768_CIPHER_TEXT_SIZE },
  30557. #endif
  30558. #ifdef WOLFSSL_KYBER1024
  30559. { KYBER1024, KYBER1024_PRIVATE_KEY_SIZE, KYBER1024_PUBLIC_KEY_SIZE,
  30560. KYBER1024_CIPHER_TEXT_SIZE },
  30561. #endif
  30562. };
  30563. #ifndef HAVE_FIPS
  30564. ret = wc_InitRng_ex(&rng, HEAP_HINT, INVALID_DEVID);
  30565. #else
  30566. ret = wc_InitRng(&rng);
  30567. #endif
  30568. if (ret != 0)
  30569. return -20000;
  30570. for (i = 0; i < (int)(sizeof(testData) / sizeof(*testData)); i++) {
  30571. ret = wc_KyberKey_Init(testData[i][0], &key, HEAP_HINT, INVALID_DEVID);
  30572. if (ret != 0)
  30573. return -20001 - i * 20;
  30574. ret = wc_KyberKey_MakeKey(&key, &rng);
  30575. if (ret != 0)
  30576. return -20002 - i * 20;
  30577. ret = wc_KyberKey_EncodePublicKey(&key, pub, testData[i][2]);
  30578. if (ret != 0)
  30579. return -20003 - i * 20;
  30580. ret = wc_KyberKey_EncodePrivateKey(&key, priv, testData[i][1]);
  30581. if (ret != 0)
  30582. return -20004 - i * 20;
  30583. ret = wc_KyberKey_Init(testData[i][0], &key, HEAP_HINT, INVALID_DEVID);
  30584. if (ret != 0)
  30585. return -20005 - i * 20;
  30586. ret = wc_KyberKey_DecodePublicKey(&key, pub, testData[i][2]);
  30587. if (ret != 0)
  30588. return -20006 - i * 20;
  30589. ret = wc_KyberKey_Encapsulate(&key, ct, ss, &rng);
  30590. if (ret != 0)
  30591. return -20007 - i * 20;
  30592. ret = wc_KyberKey_EncodePublicKey(&key, pub2, testData[i][2]);
  30593. if (ret != 0)
  30594. return -20008 - i * 20;
  30595. if (XMEMCMP(pub, pub2, testData[i][2]) != 0)
  30596. return -20009 - i * 20;
  30597. ret = wc_KyberKey_Init(testData[i][0], &key, HEAP_HINT, INVALID_DEVID);
  30598. if (ret != 0)
  30599. return -20010 - i * 20;
  30600. ret = wc_KyberKey_DecodePrivateKey(&key, priv, testData[i][1]);
  30601. if (ret != 0)
  30602. return -20011 - i * 20;
  30603. ret = wc_KyberKey_Decapsulate(&key, ss_dec, ct, testData[i][3]);
  30604. if (ret != 0)
  30605. return -20012 - i * 20;
  30606. if (XMEMCMP(ss, ss_dec, sizeof(ss)) != 0)
  30607. return -20013 - i * 20;
  30608. ret = wc_KyberKey_EncodePrivateKey(&key, priv2, testData[i][1]);
  30609. if (ret != 0)
  30610. return -20014 - i * 20;
  30611. if (XMEMCMP(priv, priv2, testData[i][2]) != 0)
  30612. return -20015 - i * 20;
  30613. wc_KyberKey_Free(&key);
  30614. }
  30615. wc_FreeRng(&rng);
  30616. #ifdef WOLFSSL_WC_KYBER
  30617. #ifdef WOLFSSL_KYBER512
  30618. ret = kyber512_kat();
  30619. if (ret != 0)
  30620. return ret;
  30621. #endif
  30622. #ifdef WOLFSSL_KYBER768
  30623. ret = kyber768_kat();
  30624. if (ret != 0)
  30625. return ret;
  30626. #endif
  30627. #ifdef WOLFSSL_KYBER1024
  30628. ret = kyber1024_kat();
  30629. if (ret != 0)
  30630. return ret;
  30631. #endif
  30632. #endif /* WOLFSSL_WC_KYBER */
  30633. return 0;
  30634. }
  30635. #endif /* WOLFSSL_HAVE_KYBER */
  30636. #ifdef WOLFCRYPT_HAVE_ECCSI
  30637. static int eccsi_api_test(WC_RNG* rng, EccsiKey* key, mp_int* ssk,
  30638. ecc_point* pvt)
  30639. {
  30640. int ret;
  30641. byte id[1] = { 0x00 };
  30642. int valid;
  30643. word32 sz;
  30644. byte data[256];
  30645. byte hash[WC_MAX_DIGEST_SIZE];
  30646. byte hashSz;
  30647. byte sig[257];
  30648. word32 sigSz;
  30649. ret = wc_InitEccsiKey_ex(NULL, 32, ECC_SECP256R1, HEAP_HINT, INVALID_DEVID);
  30650. if (ret != BAD_FUNC_ARG)
  30651. return -10023;
  30652. ret = wc_InitEccsiKey_ex(NULL, 32, ECC_SECP256R1, HEAP_HINT, INVALID_DEVID);
  30653. if (ret != BAD_FUNC_ARG)
  30654. return -10024;
  30655. ret = wc_InitEccsiKey(NULL, NULL, INVALID_DEVID);
  30656. if (ret != BAD_FUNC_ARG)
  30657. return -10025;
  30658. ret = wc_InitEccsiKey(NULL, HEAP_HINT, INVALID_DEVID);
  30659. if (ret != BAD_FUNC_ARG)
  30660. return -10026;
  30661. wc_FreeEccsiKey(NULL);
  30662. /* Create a valid key. */
  30663. ret = wc_InitEccsiKey(key, NULL, INVALID_DEVID);
  30664. if (ret != 0)
  30665. return -10027;
  30666. ret = wc_MakeEccsiKey(NULL, NULL);
  30667. if (ret != BAD_FUNC_ARG)
  30668. return -10028;
  30669. ret = wc_MakeEccsiKey(key, NULL);
  30670. if (ret != BAD_FUNC_ARG)
  30671. return -10029;
  30672. ret = wc_MakeEccsiKey(NULL, rng);
  30673. if (ret != BAD_FUNC_ARG)
  30674. return -10030;
  30675. ret = wc_MakeEccsiPair(NULL, NULL, WC_HASH_TYPE_SHA256, NULL, 1, NULL,
  30676. NULL);
  30677. if (ret != BAD_FUNC_ARG)
  30678. return -10031;
  30679. ret = wc_MakeEccsiPair(key, rng, WC_HASH_TYPE_SHA256, id, 1, ssk, NULL);
  30680. if (ret != BAD_FUNC_ARG)
  30681. return -10032;
  30682. ret = wc_MakeEccsiPair(key, rng, WC_HASH_TYPE_SHA256, id, 1, NULL, pvt);
  30683. if (ret != BAD_FUNC_ARG)
  30684. return -10033;
  30685. ret = wc_MakeEccsiPair(key, rng, WC_HASH_TYPE_SHA256, NULL, 1, ssk, pvt);
  30686. if (ret != BAD_FUNC_ARG)
  30687. return -10034;
  30688. ret = wc_MakeEccsiPair(key, NULL, WC_HASH_TYPE_SHA256, id, 1, ssk, pvt);
  30689. if (ret != BAD_FUNC_ARG)
  30690. return -10035;
  30691. ret = wc_MakeEccsiPair(NULL, rng, WC_HASH_TYPE_SHA256, id, 1, ssk, pvt);
  30692. if (ret != BAD_FUNC_ARG)
  30693. return -10036;
  30694. /* No key set */
  30695. ret = wc_MakeEccsiPair(key, rng, WC_HASH_TYPE_SHA256, id, 1, ssk, pvt);
  30696. if (ret != BAD_STATE_E)
  30697. return -10037;
  30698. ret = wc_ValidateEccsiPair(NULL, WC_HASH_TYPE_SHA256, NULL, 1, NULL, NULL,
  30699. NULL);
  30700. if (ret != BAD_FUNC_ARG)
  30701. return -10038;
  30702. ret = wc_ValidateEccsiPair(key, WC_HASH_TYPE_SHA256, id, 1, ssk, pvt,
  30703. NULL);
  30704. if (ret != BAD_FUNC_ARG)
  30705. return -10039;
  30706. ret = wc_ValidateEccsiPair(key, WC_HASH_TYPE_SHA256, id, 1, ssk, NULL,
  30707. &valid);
  30708. if (ret != BAD_FUNC_ARG)
  30709. return -10040;
  30710. ret = wc_ValidateEccsiPair(key, WC_HASH_TYPE_SHA256, id, 1, NULL, pvt,
  30711. &valid);
  30712. if (ret != BAD_FUNC_ARG)
  30713. return -10041;
  30714. ret = wc_ValidateEccsiPair(key, WC_HASH_TYPE_SHA256, NULL, 1, ssk, pvt,
  30715. &valid);
  30716. if (ret != BAD_FUNC_ARG)
  30717. return -10042;
  30718. ret = wc_ValidateEccsiPair(NULL, WC_HASH_TYPE_SHA256, id, 1, ssk, pvt,
  30719. &valid);
  30720. if (ret != BAD_FUNC_ARG)
  30721. return -10043;
  30722. /* No key set */
  30723. ret = wc_ValidateEccsiPair(key, WC_HASH_TYPE_SHA256, id, 1, ssk, pvt,
  30724. &valid);
  30725. if (ret != BAD_STATE_E)
  30726. return -10044;
  30727. ret = wc_ValidateEccsiPvt(NULL, NULL, NULL);
  30728. if (ret != BAD_FUNC_ARG)
  30729. return -10045;
  30730. ret = wc_ValidateEccsiPvt(key, NULL, NULL);
  30731. if (ret != BAD_FUNC_ARG)
  30732. return -10046;
  30733. ret = wc_ValidateEccsiPvt(NULL, pvt, NULL);
  30734. if (ret != BAD_FUNC_ARG)
  30735. return -10047;
  30736. ret = wc_ValidateEccsiPvt(NULL, NULL, &valid);
  30737. if (ret != BAD_FUNC_ARG)
  30738. return -10048;
  30739. ret = wc_ValidateEccsiPvt(key, pvt, NULL);
  30740. if (ret != BAD_FUNC_ARG)
  30741. return -10049;
  30742. ret = wc_ValidateEccsiPvt(key, NULL, &valid);
  30743. if (ret != BAD_FUNC_ARG)
  30744. return -10050;
  30745. ret = wc_ValidateEccsiPvt(NULL, pvt, &valid);
  30746. if (ret != BAD_FUNC_ARG)
  30747. return -10051;
  30748. ret = wc_EncodeEccsiPair(NULL, NULL, NULL, data, NULL);
  30749. if (ret != BAD_FUNC_ARG)
  30750. return -10052;
  30751. ret = wc_EncodeEccsiPair(key, ssk, pvt, data, NULL);
  30752. if (ret != BAD_FUNC_ARG)
  30753. return -10053;
  30754. ret = wc_EncodeEccsiPair(key, ssk, NULL, data, &sz);
  30755. if (ret != BAD_FUNC_ARG)
  30756. return -10054;
  30757. ret = wc_EncodeEccsiPair(key, NULL, pvt, data, &sz);
  30758. if (ret != BAD_FUNC_ARG)
  30759. return -10055;
  30760. ret = wc_EncodeEccsiPair(NULL, ssk, pvt, data, &sz);
  30761. if (ret != BAD_FUNC_ARG)
  30762. return -10056;
  30763. /* No key created so no curve information. */
  30764. ret = wc_EncodeEccsiPair(key, ssk, pvt, NULL, &sz);
  30765. if (ret != LENGTH_ONLY_E)
  30766. return -10057;
  30767. ret = wc_EncodeEccsiSsk(NULL, NULL, data, NULL);
  30768. if (ret != BAD_FUNC_ARG)
  30769. return -10058;
  30770. ret = wc_EncodeEccsiSsk(key, ssk, data, NULL);
  30771. if (ret != BAD_FUNC_ARG)
  30772. return -10059;
  30773. ret = wc_EncodeEccsiSsk(key, NULL, data, &sz);
  30774. if (ret != BAD_FUNC_ARG)
  30775. return -10060;
  30776. ret = wc_EncodeEccsiSsk(NULL, ssk, data, &sz);
  30777. if (ret != BAD_FUNC_ARG)
  30778. return -10061;
  30779. ret = wc_EncodeEccsiPvt(NULL, NULL, data, NULL, 1);
  30780. if (ret != BAD_FUNC_ARG)
  30781. return -10058;
  30782. ret = wc_EncodeEccsiPvt(key, pvt, data, NULL, 1);
  30783. if (ret != BAD_FUNC_ARG)
  30784. return -10059;
  30785. ret = wc_EncodeEccsiPvt(key, NULL, data, &sz, 1);
  30786. if (ret != BAD_FUNC_ARG)
  30787. return -10060;
  30788. ret = wc_EncodeEccsiPvt(NULL, pvt, data, &sz, 1);
  30789. if (ret != BAD_FUNC_ARG)
  30790. return -10061;
  30791. ret = wc_DecodeEccsiPair(NULL, NULL, 0, NULL, NULL);
  30792. if (ret != BAD_FUNC_ARG)
  30793. return -10062;
  30794. ret = wc_DecodeEccsiPair(key, data, 0, ssk, NULL);
  30795. if (ret != BAD_FUNC_ARG)
  30796. return -10063;
  30797. ret = wc_DecodeEccsiPair(key, data, 0, NULL, pvt);
  30798. if (ret != BAD_FUNC_ARG)
  30799. return -10064;
  30800. ret = wc_DecodeEccsiPair(key, NULL, 0, ssk, pvt);
  30801. if (ret != BAD_FUNC_ARG)
  30802. return -10065;
  30803. ret = wc_DecodeEccsiPair(NULL, data, 0, ssk, pvt);
  30804. if (ret != BAD_FUNC_ARG)
  30805. return -10066;
  30806. ret = wc_DecodeEccsiSsk(NULL, NULL, 0, NULL);
  30807. if (ret != BAD_FUNC_ARG)
  30808. return -10067;
  30809. ret = wc_DecodeEccsiSsk(key, data, 0, NULL);
  30810. if (ret != BAD_FUNC_ARG)
  30811. return -10068;
  30812. ret = wc_DecodeEccsiSsk(key, NULL, 0, ssk);
  30813. if (ret != BAD_FUNC_ARG)
  30814. return -10069;
  30815. ret = wc_DecodeEccsiSsk(NULL, data, 0, ssk);
  30816. if (ret != BAD_FUNC_ARG)
  30817. return -10070;
  30818. ret = wc_DecodeEccsiPvt(NULL, NULL, 0, NULL);
  30819. if (ret != BAD_FUNC_ARG)
  30820. return -10067;
  30821. ret = wc_DecodeEccsiPvt(key, data, 0, NULL);
  30822. if (ret != BAD_FUNC_ARG)
  30823. return -10068;
  30824. ret = wc_DecodeEccsiPvt(key, NULL, 0, pvt);
  30825. if (ret != BAD_FUNC_ARG)
  30826. return -10069;
  30827. ret = wc_DecodeEccsiPvt(NULL, data, 0, pvt);
  30828. if (ret != BAD_FUNC_ARG)
  30829. return -10070;
  30830. ret = wc_DecodeEccsiPvtFromSig(NULL, NULL, 0, NULL);
  30831. if (ret != BAD_FUNC_ARG)
  30832. return -10067;
  30833. ret = wc_DecodeEccsiPvtFromSig(key, data, 0, NULL);
  30834. if (ret != BAD_FUNC_ARG)
  30835. return -10068;
  30836. ret = wc_DecodeEccsiPvtFromSig(key, NULL, 0, pvt);
  30837. if (ret != BAD_FUNC_ARG)
  30838. return -10069;
  30839. ret = wc_DecodeEccsiPvtFromSig(NULL, data, 0, pvt);
  30840. if (ret != BAD_FUNC_ARG)
  30841. return -10070;
  30842. ret = wc_ExportEccsiKey(NULL, data, NULL);
  30843. if (ret != BAD_FUNC_ARG)
  30844. return -10071;
  30845. ret = wc_ExportEccsiKey(key, data, NULL);
  30846. if (ret != BAD_FUNC_ARG)
  30847. return -10072;
  30848. ret = wc_ExportEccsiKey(NULL, data, &sz);
  30849. if (ret != BAD_FUNC_ARG)
  30850. return -10073;
  30851. /* No key to export */
  30852. ret = wc_ExportEccsiKey(key, NULL, &sz);
  30853. if (ret != BAD_STATE_E)
  30854. return -10074;
  30855. ret = wc_ImportEccsiKey(NULL, NULL, 0);
  30856. if (ret != BAD_FUNC_ARG)
  30857. return -10075;
  30858. ret = wc_ImportEccsiKey(key, NULL, 0);
  30859. if (ret != BAD_FUNC_ARG)
  30860. return -10076;
  30861. ret = wc_ImportEccsiKey(NULL, data, 0);
  30862. if (ret != BAD_FUNC_ARG)
  30863. return -10077;
  30864. ret = wc_ExportEccsiPrivateKey(NULL, data, NULL);
  30865. if (ret != BAD_FUNC_ARG)
  30866. return -10071;
  30867. ret = wc_ExportEccsiPrivateKey(key, data, NULL);
  30868. if (ret != BAD_FUNC_ARG)
  30869. return -10072;
  30870. ret = wc_ExportEccsiPrivateKey(NULL, data, &sz);
  30871. if (ret != BAD_FUNC_ARG)
  30872. return -10073;
  30873. /* No key to export */
  30874. ret = wc_ExportEccsiPrivateKey(key, NULL, &sz);
  30875. if (ret != BAD_STATE_E)
  30876. return -10074;
  30877. ret = wc_ImportEccsiPrivateKey(NULL, NULL, 0);
  30878. if (ret != BAD_FUNC_ARG)
  30879. return -10075;
  30880. ret = wc_ImportEccsiPrivateKey(key, NULL, 0);
  30881. if (ret != BAD_FUNC_ARG)
  30882. return -10076;
  30883. ret = wc_ImportEccsiPrivateKey(NULL, data, 0);
  30884. if (ret != BAD_FUNC_ARG)
  30885. return -10077;
  30886. ret = wc_ExportEccsiPublicKey(NULL, data, NULL, 1);
  30887. if (ret != BAD_FUNC_ARG)
  30888. return -10078;
  30889. ret = wc_ExportEccsiPublicKey(key, data, NULL, 1);
  30890. if (ret != BAD_FUNC_ARG)
  30891. return -10079;
  30892. ret = wc_ExportEccsiPublicKey(NULL, data, &sz, 1);
  30893. if (ret != BAD_FUNC_ARG)
  30894. return -10080;
  30895. /* No key to export */
  30896. ret = wc_ExportEccsiPublicKey(key, data, &sz, 1);
  30897. if (ret != BAD_STATE_E)
  30898. return -10081;
  30899. ret = wc_ImportEccsiPublicKey(NULL, NULL, 0, 1);
  30900. if (ret != BAD_FUNC_ARG)
  30901. return -10082;
  30902. ret = wc_ImportEccsiPublicKey(key, NULL, 0, 1);
  30903. if (ret != BAD_FUNC_ARG)
  30904. return -10083;
  30905. ret = wc_ImportEccsiPublicKey(NULL, data, 0, 1);
  30906. if (ret != BAD_FUNC_ARG)
  30907. return -10084;
  30908. ret = wc_HashEccsiId(NULL, WC_HASH_TYPE_SHA256, NULL, 1, NULL, NULL, NULL);
  30909. if (ret != BAD_FUNC_ARG)
  30910. return -10085;
  30911. ret = wc_HashEccsiId(key, WC_HASH_TYPE_SHA256, id, 1, pvt, hash, NULL);
  30912. if (ret != BAD_FUNC_ARG)
  30913. return -10086;
  30914. ret = wc_HashEccsiId(key, WC_HASH_TYPE_SHA256, id, 1, pvt, NULL, &hashSz);
  30915. if (ret != BAD_FUNC_ARG)
  30916. return -10087;
  30917. ret = wc_HashEccsiId(key, WC_HASH_TYPE_SHA256, id, 1, NULL, hash, &hashSz);
  30918. if (ret != BAD_FUNC_ARG)
  30919. return -10088;
  30920. ret = wc_HashEccsiId(key, WC_HASH_TYPE_SHA256, NULL, 1, pvt, hash,
  30921. &hashSz);
  30922. if (ret != BAD_FUNC_ARG)
  30923. return -10089;
  30924. ret = wc_HashEccsiId(NULL, WC_HASH_TYPE_SHA256, id, 1, pvt, hash, &hashSz);
  30925. if (ret != BAD_FUNC_ARG)
  30926. return -10090;
  30927. ret = wc_HashEccsiId(key, WC_HASH_TYPE_SHA256, id, 1, pvt, hash, &hashSz);
  30928. if (ret != BAD_STATE_E)
  30929. return -10091;
  30930. ret = wc_SetEccsiHash(NULL, NULL, 1);
  30931. if (ret != BAD_FUNC_ARG)
  30932. return -10090;
  30933. ret = wc_SetEccsiHash(key, NULL, 1);
  30934. if (ret != BAD_FUNC_ARG)
  30935. return -10090;
  30936. ret = wc_SetEccsiHash(NULL, hash, 1);
  30937. if (ret != BAD_FUNC_ARG)
  30938. return -10090;
  30939. ret = wc_SetEccsiPair(NULL, NULL, NULL);
  30940. if (ret != BAD_FUNC_ARG)
  30941. return -10090;
  30942. ret = wc_SetEccsiPair(key, NULL, NULL);
  30943. if (ret != BAD_FUNC_ARG)
  30944. return -10090;
  30945. ret = wc_SetEccsiPair(NULL, ssk, NULL);
  30946. if (ret != BAD_FUNC_ARG)
  30947. return -10090;
  30948. ret = wc_SetEccsiPair(NULL, NULL, pvt);
  30949. if (ret != BAD_FUNC_ARG)
  30950. return -10090;
  30951. ret = wc_SetEccsiPair(key, ssk, NULL);
  30952. if (ret != BAD_FUNC_ARG)
  30953. return -10090;
  30954. ret = wc_SetEccsiPair(key, NULL, pvt);
  30955. if (ret != BAD_FUNC_ARG)
  30956. return -10090;
  30957. ret = wc_SetEccsiPair(NULL, ssk, pvt);
  30958. if (ret != BAD_FUNC_ARG)
  30959. return -10090;
  30960. ret = wc_SignEccsiHash(NULL, NULL, WC_HASH_TYPE_SHA256, NULL, 0, sig, NULL);
  30961. if (ret != BAD_FUNC_ARG)
  30962. return -10092;
  30963. ret = wc_SignEccsiHash(key, rng, WC_HASH_TYPE_SHA256, data, 0, sig, NULL);
  30964. if (ret != BAD_FUNC_ARG)
  30965. return -10093;
  30966. ret = wc_SignEccsiHash(key, rng, WC_HASH_TYPE_SHA256, NULL, 0, sig,
  30967. &sigSz);
  30968. if (ret != BAD_FUNC_ARG)
  30969. return -10096;
  30970. ret = wc_SignEccsiHash(key, NULL, WC_HASH_TYPE_SHA256, data, 0, sig,
  30971. &sigSz);
  30972. if (ret != BAD_FUNC_ARG)
  30973. return -10098;
  30974. ret = wc_SignEccsiHash(NULL, rng, WC_HASH_TYPE_SHA256, data, 0, sig,
  30975. &sigSz);
  30976. if (ret != BAD_FUNC_ARG)
  30977. return -10099;
  30978. /* Key not set. */
  30979. ret = wc_SignEccsiHash(key, rng, WC_HASH_TYPE_SHA256, data, 0, NULL,
  30980. &sigSz);
  30981. if (ret != BAD_STATE_E)
  30982. return -10100;
  30983. ret = wc_VerifyEccsiHash(NULL, WC_HASH_TYPE_SHA256, NULL, 0, NULL, 0, NULL);
  30984. if (ret != BAD_FUNC_ARG)
  30985. return -10101;
  30986. ret = wc_VerifyEccsiHash(key, WC_HASH_TYPE_SHA256, NULL, 0, NULL, 0, NULL);
  30987. if (ret != BAD_FUNC_ARG)
  30988. return -10101;
  30989. ret = wc_VerifyEccsiHash(NULL, WC_HASH_TYPE_SHA256, data, 0, NULL, 0, NULL);
  30990. if (ret != BAD_FUNC_ARG)
  30991. return -10101;
  30992. ret = wc_VerifyEccsiHash(NULL, WC_HASH_TYPE_SHA256, NULL, 0, sig, 0, NULL);
  30993. if (ret != BAD_FUNC_ARG)
  30994. return -10101;
  30995. ret = wc_VerifyEccsiHash(NULL, WC_HASH_TYPE_SHA256, NULL, 0, NULL, 0,
  30996. &valid);
  30997. if (ret != BAD_FUNC_ARG)
  30998. return -10101;
  30999. ret = wc_VerifyEccsiHash(key, WC_HASH_TYPE_SHA256, data, 0, sig, 0, NULL);
  31000. if (ret != BAD_FUNC_ARG)
  31001. return -10102;
  31002. ret = wc_VerifyEccsiHash(key, WC_HASH_TYPE_SHA256, data, 0, NULL, 0,
  31003. &valid);
  31004. if (ret != BAD_FUNC_ARG)
  31005. return -10103;
  31006. ret = wc_VerifyEccsiHash(key, WC_HASH_TYPE_SHA256, NULL, 0, sig, 0,
  31007. &valid);
  31008. if (ret != BAD_FUNC_ARG)
  31009. return -10104;
  31010. ret = wc_VerifyEccsiHash(NULL, WC_HASH_TYPE_SHA256, data, 0, sig, 0,
  31011. &valid);
  31012. if (ret != BAD_FUNC_ARG)
  31013. return -10106;
  31014. ret = wc_VerifyEccsiHash(key, WC_HASH_TYPE_SHA256, data, 0, sig, 0,
  31015. &valid);
  31016. if (ret != BAD_STATE_E)
  31017. return -10106;
  31018. ret = wc_SetEccsiPair(key, ssk, pvt);
  31019. if (ret != 0)
  31020. return -10107;
  31021. /* Identity hash not set. */
  31022. ret = wc_SignEccsiHash(key, rng, WC_HASH_TYPE_SHA256, data, 0, NULL,
  31023. &sigSz);
  31024. if (ret != BAD_STATE_E)
  31025. return -10108;
  31026. wc_FreeEccsiKey(key);
  31027. return 0;
  31028. }
  31029. /* RFC 6507: Appendix A */
  31030. static int eccsi_kat_verify_test(EccsiKey* key, ecc_point* pvt)
  31031. {
  31032. int ret;
  31033. int verified;
  31034. const byte msg[] = { 0x6D, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x00 };
  31035. word32 msgSz = sizeof(msg);
  31036. byte hash[WC_SHA256_DIGEST_SIZE];
  31037. byte hashSz = WC_SHA256_DIGEST_SIZE;
  31038. static const byte id[] = {
  31039. 0x32, 0x30, 0x31, 0x31, 0x2D, 0x30, 0x32, 0x00,
  31040. 0x74, 0x65, 0x6C, 0x3A, 0x2B, 0x34, 0x34, 0x37,
  31041. 0x37, 0x30, 0x30, 0x39, 0x30, 0x30, 0x31, 0x32,
  31042. 0x33, 0x00
  31043. };
  31044. word32 idSz = sizeof(id);
  31045. static const byte sig[] = {
  31046. 0x26, 0x9D, 0x4C, 0x8F, 0xDE, 0xB6, 0x6A, 0x74,
  31047. 0xE4, 0xEF, 0x8C, 0x0D, 0x5D, 0xCC, 0x59, 0x7D,
  31048. 0xDF, 0xE6, 0x02, 0x9C, 0x2A, 0xFF, 0xC4, 0x93,
  31049. 0x60, 0x08, 0xCD, 0x2C, 0xC1, 0x04, 0x5D, 0x81,
  31050. 0xE0, 0x9B, 0x52, 0x8D, 0x0E, 0xF8, 0xD6, 0xDF,
  31051. 0x1A, 0xA3, 0xEC, 0xBF, 0x80, 0x11, 0x0C, 0xFC,
  31052. 0xEC, 0x9F, 0xC6, 0x82, 0x52, 0xCE, 0xBB, 0x67,
  31053. 0x9F, 0x41, 0x34, 0x84, 0x69, 0x40, 0xCC, 0xFD,
  31054. 0x04,
  31055. 0x75, 0x8A, 0x14, 0x27, 0x79, 0xBE, 0x89, 0xE8,
  31056. 0x29, 0xE7, 0x19, 0x84, 0xCB, 0x40, 0xEF, 0x75,
  31057. 0x8C, 0xC4, 0xAD, 0x77, 0x5F, 0xC5, 0xB9, 0xA3,
  31058. 0xE1, 0xC8, 0xED, 0x52, 0xF6, 0xFA, 0x36, 0xD9,
  31059. 0xA7, 0x9D, 0x24, 0x76, 0x92, 0xF4, 0xED, 0xA3,
  31060. 0xA6, 0xBD, 0xAB, 0x77, 0xD6, 0xAA, 0x64, 0x74,
  31061. 0xA4, 0x64, 0xAE, 0x49, 0x34, 0x66, 0x3C, 0x52,
  31062. 0x65, 0xBA, 0x70, 0x18, 0xBA, 0x09, 0x1F, 0x79
  31063. };
  31064. word32 sigSz = sizeof(sig);
  31065. static const byte pubData[] = {
  31066. 0x50, 0xD4, 0x67, 0x0B, 0xDE, 0x75, 0x24, 0x4F,
  31067. 0x28, 0xD2, 0x83, 0x8A, 0x0D, 0x25, 0x55, 0x8A,
  31068. 0x7A, 0x72, 0x68, 0x6D, 0x45, 0x22, 0xD4, 0xC8,
  31069. 0x27, 0x3F, 0xB6, 0x44, 0x2A, 0xEB, 0xFA, 0x93,
  31070. 0xDB, 0xDD, 0x37, 0x55, 0x1A, 0xFD, 0x26, 0x3B,
  31071. 0x5D, 0xFD, 0x61, 0x7F, 0x39, 0x60, 0xC6, 0x5A,
  31072. 0x8C, 0x29, 0x88, 0x50, 0xFF, 0x99, 0xF2, 0x03,
  31073. 0x66, 0xDC, 0xE7, 0xD4, 0x36, 0x72, 0x17, 0xF4
  31074. };
  31075. static const byte expHash[] = {
  31076. 0x49, 0x0f, 0x3f, 0xeb, 0xbc, 0x1c, 0x90, 0x2f,
  31077. 0x62, 0x89, 0x72, 0x3d, 0x7f, 0x8c, 0xbf, 0x79,
  31078. 0xdb, 0x88, 0x93, 0x08, 0x49, 0xd1, 0x9f, 0x38,
  31079. 0xf0, 0x29, 0x5b, 0x5c, 0x27, 0x6c, 0x14, 0xd1
  31080. };
  31081. ret = wc_ImportEccsiPublicKey(key, pubData, sizeof(pubData), 0);
  31082. if (ret != 0)
  31083. return -10108;
  31084. ret = wc_DecodeEccsiPvtFromSig(key, sig, sigSz, pvt);
  31085. if (ret != 0)
  31086. return -10109;
  31087. ret = wc_HashEccsiId(key, WC_HASH_TYPE_SHA256, id, idSz, pvt, hash,
  31088. &hashSz);
  31089. if (ret != 0)
  31090. return -10112;
  31091. if (hashSz != sizeof(expHash))
  31092. return -10113;
  31093. if (XMEMCMP(hash, expHash, hashSz) != 0)
  31094. return -10114;
  31095. ret = wc_SetEccsiHash(key, hash, hashSz);
  31096. if (ret != 0)
  31097. return -10112;
  31098. ret = wc_VerifyEccsiHash(key, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  31099. &verified);
  31100. if (ret != 0)
  31101. return -10115;
  31102. if (!verified)
  31103. return -10116;
  31104. return 0;
  31105. }
  31106. static int eccsi_enc_dec_pair_test(EccsiKey* priv, mp_int* ssk, ecc_point* pvt)
  31107. {
  31108. int ret;
  31109. byte data[32 * 3];
  31110. word32 sz;
  31111. ecc_point* decPvt = NULL;
  31112. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  31113. mp_int *decSsk = (mp_int *)XMALLOC(sizeof(*decSsk), HEAP_HINT,
  31114. DYNAMIC_TYPE_TMP_BUFFER);
  31115. if (decSsk == NULL)
  31116. return -10173;
  31117. #else
  31118. mp_int decSsk[1];
  31119. #endif
  31120. ret = mp_init(decSsk);
  31121. if (ret != 0)
  31122. ERROR_OUT(-10117, out);
  31123. decPvt = wc_ecc_new_point();
  31124. if (decPvt == NULL)
  31125. ERROR_OUT(-10118, out);
  31126. ret = wc_EncodeEccsiPair(priv, ssk, pvt, NULL, &sz);
  31127. if (ret != LENGTH_ONLY_E)
  31128. ERROR_OUT(-10119, out);
  31129. if (sz != 32 * 3)
  31130. ERROR_OUT(-10120, out);
  31131. ret = wc_EncodeEccsiPair(priv, ssk, pvt, data, &sz);
  31132. if (ret != 0)
  31133. ERROR_OUT(-10121, out);
  31134. if (sz != 32* 3)
  31135. ERROR_OUT(-10122, out);
  31136. ret = wc_DecodeEccsiPair(priv, data, sz, decSsk, decPvt);
  31137. if (ret != 0)
  31138. ERROR_OUT(-10123, out);
  31139. if (mp_cmp(ssk, decSsk) != MP_EQ)
  31140. ERROR_OUT(-10124, out);
  31141. if (wc_ecc_cmp_point(pvt, decPvt) != MP_EQ)
  31142. ERROR_OUT(-10125, out);
  31143. ret = wc_EncodeEccsiSsk(priv, ssk, NULL, &sz);
  31144. if (ret != LENGTH_ONLY_E)
  31145. ERROR_OUT(-10119, out);
  31146. if (sz != 32)
  31147. ERROR_OUT(-10120, out);
  31148. ret = wc_EncodeEccsiSsk(priv, ssk, data, &sz);
  31149. if (ret != 0)
  31150. ERROR_OUT(-10121, out);
  31151. if (sz != 32)
  31152. ERROR_OUT(-10122, out);
  31153. ret = wc_DecodeEccsiSsk(priv, data, sz, decSsk);
  31154. if (ret != 0)
  31155. ERROR_OUT(-10123, out);
  31156. if (mp_cmp(ssk, decSsk) != MP_EQ)
  31157. ERROR_OUT(-10124, out);
  31158. ret = wc_EncodeEccsiPvt(priv, pvt, NULL, &sz, 1);
  31159. if (ret != LENGTH_ONLY_E)
  31160. ERROR_OUT(-10126, out);
  31161. if (sz != 32 * 2)
  31162. ERROR_OUT(-10127, out);
  31163. ret = wc_EncodeEccsiPvt(priv, pvt, data, &sz, 1);
  31164. if (ret != 0)
  31165. ERROR_OUT(-10128, out);
  31166. if (sz != 32 * 2)
  31167. ERROR_OUT(-10129, out);
  31168. ret = wc_DecodeEccsiPvt(priv, data, sz, decPvt);
  31169. if (ret != 0)
  31170. ERROR_OUT(-10130, out);
  31171. if (wc_ecc_cmp_point(pvt, decPvt) != MP_EQ)
  31172. ERROR_OUT(-10131, out);
  31173. sz = sizeof(data);
  31174. ret = wc_EncodeEccsiPvt(priv, pvt, data, &sz, 0);
  31175. if (ret != 0)
  31176. ERROR_OUT(-10128, out);
  31177. if (sz != 32 * 2 + 1)
  31178. ERROR_OUT(-10129, out);
  31179. ret = wc_DecodeEccsiPvt(priv, data, sz, decPvt);
  31180. if (ret != 0)
  31181. ERROR_OUT(-10130, out);
  31182. if (wc_ecc_cmp_point(pvt, decPvt) != MP_EQ)
  31183. ERROR_OUT(-10131, out);
  31184. wc_ecc_del_point(decPvt);
  31185. out:
  31186. mp_free(decSsk);
  31187. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  31188. XFREE(decSsk, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  31189. #endif
  31190. return ret;
  31191. }
  31192. static int eccsi_imp_exp_key_test(EccsiKey* priv)
  31193. {
  31194. int ret;
  31195. byte data[32 * 3];
  31196. byte out[32 * 3];
  31197. word32 sz;
  31198. ret = wc_ExportEccsiKey(priv, NULL, &sz);
  31199. if (ret != LENGTH_ONLY_E)
  31200. return -10132;
  31201. if (sz != 32 * 3)
  31202. return -10133;
  31203. ret = wc_ExportEccsiKey(priv, data, &sz);
  31204. if (ret != 0)
  31205. return -10134;
  31206. ret = wc_ImportEccsiKey(priv, data, sz);
  31207. if (ret != 0)
  31208. return -10135;
  31209. ret = wc_ExportEccsiKey(priv, NULL, &sz);
  31210. if (ret != LENGTH_ONLY_E)
  31211. return -10132;
  31212. if (sz != 32 * 3)
  31213. return -10143;
  31214. ret = wc_ExportEccsiKey(priv, out, &sz);
  31215. if (ret != 0)
  31216. return -10144;
  31217. if (sz != 32 * 3)
  31218. return -10145;
  31219. if (XMEMCMP(data, out, sz) != 0)
  31220. return -10146;
  31221. ret = wc_ExportEccsiPrivateKey(priv, NULL, &sz);
  31222. if (ret != LENGTH_ONLY_E)
  31223. return -10156;
  31224. if (sz != 32)
  31225. return -10157;
  31226. ret = wc_ExportEccsiPrivateKey(priv, data, &sz);
  31227. if (ret != 0)
  31228. return -10158;
  31229. ret = wc_ImportEccsiPrivateKey(priv, data, sz);
  31230. if (ret != 0)
  31231. return -10159;
  31232. ret = wc_ExportEccsiPrivateKey(priv, NULL, &sz);
  31233. if (ret != LENGTH_ONLY_E)
  31234. return -10152;
  31235. if (sz != 32)
  31236. return -10163;
  31237. ret = wc_ExportEccsiPrivateKey(priv, out, &sz);
  31238. if (ret != 0)
  31239. return -10164;
  31240. if (sz != 32)
  31241. return -10165;
  31242. if (XMEMCMP(data, out, sz) != 0)
  31243. return -10166;
  31244. return 0;
  31245. }
  31246. static int eccsi_imp_exp_pubkey_test(EccsiKey* key1, EccsiKey* key2)
  31247. {
  31248. int ret;
  31249. byte data[32 * 2 + 1];
  31250. byte pubData[32 * 2 + 1];
  31251. word32 sz;
  31252. ret = wc_ExportEccsiPublicKey(key1, NULL, &sz, 1);
  31253. if (ret != LENGTH_ONLY_E)
  31254. return -10136;
  31255. if (sz != 32 * 2)
  31256. return -10137;
  31257. ret = wc_ExportEccsiPublicKey(key1, data, &sz, 1);
  31258. if (ret != 0)
  31259. return -10138;
  31260. ret = wc_ImportEccsiPublicKey(key2, data, sz, 1);
  31261. if (ret != 0)
  31262. return -10139;
  31263. sz = sizeof(pubData);
  31264. ret = wc_ExportEccsiPublicKey(key2, pubData, &sz, 1);
  31265. if (ret != 0)
  31266. return -10140;
  31267. if (sz != 32 * 2)
  31268. return -10141;
  31269. if (XMEMCMP(data, pubData, sz) != 0)
  31270. return -10142;
  31271. sz = sizeof(pubData);
  31272. ret = wc_ExportEccsiPublicKey(key2, pubData, &sz, 0);
  31273. if (ret != 0)
  31274. return -10140;
  31275. if (sz != 32 * 2 + 1)
  31276. return -10141;
  31277. if (pubData[0] != 0x04)
  31278. return -10140;
  31279. if (XMEMCMP(pubData + 1, data, sz - 1) != 0)
  31280. return -10142;
  31281. ret = wc_ImportEccsiPublicKey(key2, pubData, sz, 0);
  31282. if (ret != 0)
  31283. return -10139;
  31284. return 0;
  31285. }
  31286. static int eccsi_make_key_test(EccsiKey* priv, EccsiKey* pub, WC_RNG* rng,
  31287. mp_int* ssk, ecc_point* pvt)
  31288. {
  31289. int ret;
  31290. char mail[] = "test@wolfssl.com";
  31291. byte* id = (byte*)mail;
  31292. word32 idSz = (word32) XSTRLEN(mail);
  31293. int valid;
  31294. ret = wc_MakeEccsiKey(priv, rng);
  31295. if (ret != 0)
  31296. return -10143;
  31297. ret = eccsi_imp_exp_key_test(priv);
  31298. if (ret < 0)
  31299. return ret;
  31300. ret = eccsi_imp_exp_pubkey_test(priv, pub);
  31301. if (ret < 0)
  31302. return ret;
  31303. ret = wc_MakeEccsiPair(priv, rng, WC_HASH_TYPE_SHA256, id, idSz, ssk, pvt);
  31304. if (ret != 0)
  31305. return -10144;
  31306. ret = wc_ValidateEccsiPair(pub, WC_HASH_TYPE_SHA256, id, idSz, ssk, pvt,
  31307. &valid);
  31308. if (ret != 0)
  31309. return -10145;
  31310. if (!valid)
  31311. return -10146;
  31312. ret = eccsi_enc_dec_pair_test(priv, ssk, pvt);
  31313. if (ret != 0)
  31314. return ret;
  31315. return 0;
  31316. }
  31317. static int eccsi_sign_verify_test(EccsiKey* priv, EccsiKey* pub, WC_RNG* rng,
  31318. mp_int* ssk, ecc_point* pvt)
  31319. {
  31320. int ret;
  31321. byte hashPriv[WC_MAX_DIGEST_SIZE];
  31322. byte hashPub[WC_MAX_DIGEST_SIZE];
  31323. byte hashSz;
  31324. byte sig[144];
  31325. word32 sigSz;
  31326. int verified, valid;
  31327. char mail[] = "test@wolfssl.com";
  31328. byte* id = (byte*)mail;
  31329. word32 idSz = (word32) XSTRLEN(mail);
  31330. byte msg[] = { 0x00 };
  31331. word32 msgSz = sizeof(msg);
  31332. ret = wc_HashEccsiId(priv, WC_HASH_TYPE_SHA256, id, idSz, pvt, hashPriv,
  31333. &hashSz);
  31334. if (ret != 0)
  31335. return -10147;
  31336. if (hashSz != 32)
  31337. return -10148;
  31338. ret = wc_HashEccsiId(priv, WC_HASH_TYPE_SHA256, id, idSz, pvt, hashPub,
  31339. &hashSz);
  31340. if (ret != 0)
  31341. return -10149;
  31342. if (hashSz != 32)
  31343. return -10150;
  31344. if (XMEMCMP(hashPriv, hashPub, hashSz) != 0)
  31345. return -10151;
  31346. ret = wc_SetEccsiHash(priv, hashPriv, hashSz);
  31347. if (ret != 0)
  31348. return -10149;
  31349. ret = wc_SetEccsiPair(priv, ssk, pvt);
  31350. if (ret != 0)
  31351. return -10149;
  31352. ret = wc_SignEccsiHash(priv, rng, WC_HASH_TYPE_SHA256, msg, msgSz, NULL,
  31353. &sigSz);
  31354. if (ret != LENGTH_ONLY_E)
  31355. return -10152;
  31356. if (sigSz != 129)
  31357. return -10153;
  31358. ret = wc_SignEccsiHash(priv, rng, WC_HASH_TYPE_SHA256, msg, msgSz, sig,
  31359. &sigSz);
  31360. if (ret != 0)
  31361. return -10154;
  31362. ret = wc_SetEccsiHash(pub, hashPub, hashSz);
  31363. if (ret != 0)
  31364. return -10149;
  31365. ret = wc_VerifyEccsiHash(pub, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  31366. &verified);
  31367. if (ret != 0)
  31368. return -10155;
  31369. if (!verified)
  31370. return -10156;
  31371. /* Check that changing HS results in verification failure. */
  31372. hashPub[0] ^= 0x80;
  31373. ret = wc_SetEccsiHash(pub, hashPub, hashSz);
  31374. if (ret != 0)
  31375. return -10149;
  31376. ret = wc_VerifyEccsiHash(pub, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  31377. &verified);
  31378. if (ret != 0)
  31379. return -10157;
  31380. if (verified)
  31381. return -10158;
  31382. hashPub[0] ^= 0x80;
  31383. ret = wc_SetEccsiHash(pub, hashPub, hashSz);
  31384. if (ret != 0)
  31385. return -10149;
  31386. /* Check that changing msg results in verification failure. */
  31387. msg[0] ^= 0x80;
  31388. ret = wc_VerifyEccsiHash(pub, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  31389. &verified);
  31390. if (ret != 0)
  31391. return -10159;
  31392. if (verified)
  31393. return -10160;
  31394. msg[0] ^= 0x80;
  31395. /* Check that changing signature results in verification failure. */
  31396. sig[0] ^= 0x80;
  31397. ret = wc_VerifyEccsiHash(pub, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  31398. &verified);
  31399. if (ret != 0)
  31400. return -10161;
  31401. if (verified)
  31402. return -10162;
  31403. sig[0] ^= 0x80;
  31404. /* Check that key state hasn't been invalidated. */
  31405. ret = wc_VerifyEccsiHash(pub, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  31406. &verified);
  31407. if (ret != 0)
  31408. return -10163;
  31409. if (!verified)
  31410. return -10164;
  31411. /* Check that verifying with the private key works. */
  31412. ret = wc_VerifyEccsiHash(priv, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  31413. &verified);
  31414. if (ret != 0)
  31415. return -10165;
  31416. if (!verified)
  31417. return -10166;
  31418. /* Check that the KPAK is converted from montgomery form. */
  31419. ret = eccsi_imp_exp_key_test(priv);
  31420. if (ret != 0)
  31421. return ret;
  31422. /* Check that KPAK can converted to Montgomery form again. */
  31423. ret = wc_VerifyEccsiHash(priv, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  31424. &verified);
  31425. if (ret != 0)
  31426. return -10167;
  31427. if (!verified)
  31428. return -10168;
  31429. /* Check that the KPAK is converted from montgomery form. */
  31430. ret = wc_ValidateEccsiPair(pub, WC_HASH_TYPE_SHA256, id, idSz, ssk, pvt,
  31431. &valid);
  31432. if (ret != 0)
  31433. return -10169;
  31434. if (!valid)
  31435. return -10170;
  31436. /* Check that KPAK can converted to Montgomery form again. */
  31437. ret = wc_VerifyEccsiHash(priv, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  31438. &verified);
  31439. if (ret != 0)
  31440. return -10171;
  31441. if (!verified)
  31442. return -10172;
  31443. /* Check that the KPAK is converted from montgomery form. */
  31444. ret = eccsi_imp_exp_pubkey_test(priv, pub);
  31445. if (ret != 0)
  31446. return ret;
  31447. return 0;
  31448. }
  31449. int eccsi_test(void)
  31450. {
  31451. int ret = 0;
  31452. WC_RNG rng;
  31453. int rng_inited = 0;
  31454. EccsiKey* priv = NULL;
  31455. EccsiKey* pub = NULL;
  31456. mp_int* ssk = NULL;
  31457. ecc_point* pvt = NULL;
  31458. priv = (EccsiKey*)XMALLOC(sizeof(EccsiKey), HEAP_HINT,
  31459. DYNAMIC_TYPE_TMP_BUFFER);
  31460. if (priv == NULL)
  31461. ret = -10205;
  31462. else
  31463. XMEMSET(priv, 0, sizeof(*priv));
  31464. if (ret == 0) {
  31465. pub = (EccsiKey*)XMALLOC(sizeof(EccsiKey), HEAP_HINT,
  31466. DYNAMIC_TYPE_TMP_BUFFER);
  31467. if (pub == NULL)
  31468. ret = -10206;
  31469. else
  31470. XMEMSET(pub, 0, sizeof(*pub));
  31471. }
  31472. if (ret == 0) {
  31473. ssk = (mp_int*)XMALLOC(sizeof(mp_int), HEAP_HINT,
  31474. DYNAMIC_TYPE_TMP_BUFFER);
  31475. if (ssk == NULL)
  31476. ret = -10207;
  31477. else
  31478. XMEMSET(ssk, 0, sizeof(*ssk));
  31479. }
  31480. if (ret == 0) {
  31481. #ifndef HAVE_FIPS
  31482. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  31483. #else
  31484. ret = wc_InitRng(&rng);
  31485. #endif
  31486. if (ret != 0)
  31487. ret = -10200;
  31488. else
  31489. rng_inited = 1;
  31490. }
  31491. if (ret == 0) {
  31492. pvt = wc_ecc_new_point();
  31493. if (pvt == NULL)
  31494. ret = -10201;
  31495. }
  31496. if (ret == 0) {
  31497. ret = mp_init(ssk);
  31498. if (ret != 0)
  31499. ret = -10202;
  31500. }
  31501. if (ret == 0) {
  31502. ret = eccsi_api_test(&rng, priv, ssk, pvt);
  31503. }
  31504. if (ret == 0) {
  31505. ret = wc_InitEccsiKey(pub, HEAP_HINT, INVALID_DEVID);
  31506. if (ret != 0)
  31507. ret = -10203;
  31508. }
  31509. if (ret == 0) {
  31510. ret = wc_InitEccsiKey(priv, HEAP_HINT, INVALID_DEVID);
  31511. if (ret != 0)
  31512. ret = -10204;
  31513. }
  31514. if (ret == 0) {
  31515. ret = eccsi_kat_verify_test(pub, pvt);
  31516. }
  31517. if (ret == 0) {
  31518. ret = eccsi_make_key_test(priv, pub, &rng, ssk, pvt);
  31519. }
  31520. if (ret == 0) {
  31521. ret = eccsi_sign_verify_test(priv, pub, &rng, ssk, pvt);
  31522. }
  31523. if (pvt != NULL)
  31524. wc_ecc_del_point(pvt);
  31525. if (rng_inited)
  31526. wc_FreeRng(&rng);
  31527. if (ssk != NULL) {
  31528. mp_free(ssk);
  31529. XFREE(ssk, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  31530. }
  31531. if (pub != NULL) {
  31532. wc_FreeEccsiKey(pub);
  31533. XFREE(pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  31534. }
  31535. if (priv != NULL) {
  31536. wc_FreeEccsiKey(priv);
  31537. XFREE(priv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  31538. }
  31539. return ret;
  31540. }
  31541. #endif /* WOLFCRYPT_HAVE_ECCSI */
  31542. #ifdef WOLFCRYPT_HAVE_SAKKE
  31543. static int sakke_api_test(WC_RNG* rng, SakkeKey* key, ecc_point* rsk)
  31544. {
  31545. int ret;
  31546. byte id[1] = { 0x00 };
  31547. int valid;
  31548. byte data[256];
  31549. word32 sz;
  31550. byte auth[257];
  31551. word16 authSz;
  31552. byte ssv[256];
  31553. word16 ssvSz;
  31554. word32 len;
  31555. ret = wc_InitSakkeKey_ex(NULL, 128, ECC_SAKKE_1, NULL, INVALID_DEVID);
  31556. if (ret != BAD_FUNC_ARG)
  31557. return -10205;
  31558. ret = wc_InitSakkeKey_ex(NULL, 128, ECC_SAKKE_1, HEAP_HINT, INVALID_DEVID);
  31559. if (ret != BAD_FUNC_ARG)
  31560. return -10206;
  31561. wc_FreeSakkeKey(NULL);
  31562. XMEMSET(key, 0, sizeof(*key));
  31563. wc_FreeSakkeKey(key);
  31564. ret = wc_InitSakkeKey_ex(key, 128, ECC_SAKKE_1, HEAP_HINT, INVALID_DEVID);
  31565. if (ret != 0)
  31566. return -10207;
  31567. ret = wc_MakeSakkeKey(NULL, NULL);
  31568. if (ret != BAD_FUNC_ARG)
  31569. return -10208;
  31570. ret = wc_MakeSakkeKey(key, NULL);
  31571. if (ret != BAD_FUNC_ARG)
  31572. return -10209;
  31573. ret = wc_MakeSakkeKey(NULL, rng);
  31574. if (ret != BAD_FUNC_ARG)
  31575. return -10210;
  31576. ret = wc_MakeSakkePublicKey(NULL, NULL);
  31577. if (ret != BAD_FUNC_ARG)
  31578. return -10211;
  31579. ret = wc_MakeSakkePublicKey(key, NULL);
  31580. if (ret != BAD_FUNC_ARG)
  31581. return -10212;
  31582. ret = wc_MakeSakkePublicKey(NULL, rsk);
  31583. if (ret != BAD_FUNC_ARG)
  31584. return -10213;
  31585. ret = wc_MakeSakkeRsk(NULL, NULL, 1, NULL);
  31586. if (ret != BAD_FUNC_ARG)
  31587. return -10214;
  31588. ret = wc_MakeSakkeRsk(key, id, 1, NULL);
  31589. if (ret != BAD_FUNC_ARG)
  31590. return -10215;
  31591. ret = wc_MakeSakkeRsk(key, NULL, 1, rsk);
  31592. if (ret != BAD_FUNC_ARG)
  31593. return -10216;
  31594. ret = wc_MakeSakkeRsk(NULL, id, 1, rsk);
  31595. if (ret != BAD_FUNC_ARG)
  31596. return -10217;
  31597. ret = wc_ValidateSakkeRsk(NULL, NULL, 1, NULL, NULL);
  31598. if (ret != BAD_FUNC_ARG)
  31599. return -10218;
  31600. ret = wc_ValidateSakkeRsk(key, id, 1, rsk, NULL);
  31601. if (ret != BAD_FUNC_ARG)
  31602. return -10219;
  31603. ret = wc_ValidateSakkeRsk(NULL, id, 1, rsk, &valid);
  31604. if (ret != BAD_FUNC_ARG)
  31605. return -10220;
  31606. ret = wc_ExportSakkeKey(NULL, NULL, NULL);
  31607. if (ret != BAD_FUNC_ARG)
  31608. return -10221;
  31609. ret = wc_ExportSakkeKey(key, data, NULL);
  31610. if (ret != BAD_FUNC_ARG)
  31611. return -10222;
  31612. ret = wc_ExportSakkeKey(NULL, data, &sz);
  31613. if (ret != BAD_FUNC_ARG)
  31614. return -10223;
  31615. ret = wc_ImportSakkeKey(NULL, NULL, 1);
  31616. if (ret != BAD_FUNC_ARG)
  31617. return -10224;
  31618. ret = wc_ImportSakkeKey(key, NULL, 1);
  31619. if (ret != BAD_FUNC_ARG)
  31620. return -10225;
  31621. ret = wc_ImportSakkeKey(NULL, data, 1);
  31622. if (ret != BAD_FUNC_ARG)
  31623. return -10226;
  31624. ret = wc_ExportSakkePrivateKey(NULL, NULL, NULL);
  31625. if (ret != BAD_FUNC_ARG)
  31626. return -10227;
  31627. ret = wc_ExportSakkePrivateKey(key, data, NULL);
  31628. if (ret != BAD_FUNC_ARG)
  31629. return -10228;
  31630. ret = wc_ExportSakkePrivateKey(NULL, data, &sz);
  31631. if (ret != BAD_FUNC_ARG)
  31632. return -10229;
  31633. ret = wc_ImportSakkePrivateKey(NULL, NULL, 1);
  31634. if (ret != BAD_FUNC_ARG)
  31635. return -10230;
  31636. ret = wc_ImportSakkePrivateKey(key, NULL, 1);
  31637. if (ret != BAD_FUNC_ARG)
  31638. return -10231;
  31639. ret = wc_ImportSakkePrivateKey(NULL, data, 1);
  31640. if (ret != BAD_FUNC_ARG)
  31641. return -10232;
  31642. sz = sizeof(data);
  31643. ret = wc_EncodeSakkeRsk(NULL, NULL, data, NULL, 1);
  31644. if (ret != BAD_FUNC_ARG)
  31645. return -10233;
  31646. ret = wc_EncodeSakkeRsk(key, rsk, data, NULL, 1);
  31647. if (ret != BAD_FUNC_ARG)
  31648. return -10234;
  31649. ret = wc_EncodeSakkeRsk(key, NULL, data, &sz, 1);
  31650. if (ret != BAD_FUNC_ARG)
  31651. return -10235;
  31652. ret = wc_EncodeSakkeRsk(NULL, rsk, data, &sz, 1);
  31653. if (ret != BAD_FUNC_ARG)
  31654. return -10236;
  31655. ret = wc_DecodeSakkeRsk(NULL, NULL, sz, NULL);
  31656. if (ret != BAD_FUNC_ARG)
  31657. return -10237;
  31658. ret = wc_DecodeSakkeRsk(key, data, sz, NULL);
  31659. if (ret != BAD_FUNC_ARG)
  31660. return -10238;
  31661. ret = wc_DecodeSakkeRsk(key, NULL, sz, rsk);
  31662. if (ret != BAD_FUNC_ARG)
  31663. return -10239;
  31664. ret = wc_DecodeSakkeRsk(NULL, data, sz, rsk);
  31665. if (ret != BAD_FUNC_ARG)
  31666. return -10240;
  31667. ret = wc_ImportSakkeRsk(NULL, NULL, sz);
  31668. if (ret != BAD_FUNC_ARG)
  31669. return -10237;
  31670. ret = wc_ImportSakkeRsk(key, NULL, sz);
  31671. if (ret != BAD_FUNC_ARG)
  31672. return -10237;
  31673. ret = wc_ImportSakkeRsk(NULL, data, sz);
  31674. if (ret != BAD_FUNC_ARG)
  31675. return -10237;
  31676. ret = wc_ImportSakkeRsk(key, data, 1);
  31677. if (ret != BUFFER_E)
  31678. return -10237;
  31679. ret = wc_GenerateSakkeRskTable(NULL, NULL, data, NULL);
  31680. if (ret != BAD_FUNC_ARG)
  31681. return -10241;
  31682. ret = wc_GenerateSakkeRskTable(key, NULL, data, NULL);
  31683. if (ret != BAD_FUNC_ARG)
  31684. return -10242;
  31685. ret = wc_GenerateSakkeRskTable(NULL, rsk, data, NULL);
  31686. if (ret != BAD_FUNC_ARG)
  31687. return -10243;
  31688. ret = wc_GenerateSakkeRskTable(NULL, NULL, data, &len);
  31689. if (ret != BAD_FUNC_ARG)
  31690. return -10244;
  31691. ret = wc_GenerateSakkeRskTable(key, rsk, data, NULL);
  31692. if (ret != BAD_FUNC_ARG)
  31693. return -10245;
  31694. ret = wc_GenerateSakkeRskTable(key, NULL, data, &len);
  31695. if (ret != BAD_FUNC_ARG)
  31696. return -10246;
  31697. ret = wc_GenerateSakkeRskTable(NULL, rsk, data, &len);
  31698. if (ret != BAD_FUNC_ARG)
  31699. return -10247;
  31700. ret = wc_GenerateSakkeRskTable(key, rsk, NULL, &len);
  31701. if (ret != LENGTH_ONLY_E)
  31702. return -10248;
  31703. len--;
  31704. ret = wc_GenerateSakkeRskTable(key, rsk, data, &len);
  31705. if (ret != BUFFER_E)
  31706. return -10249;
  31707. ret = wc_ExportSakkePublicKey(NULL, data, NULL, 1);
  31708. if (ret != BAD_FUNC_ARG)
  31709. return -10250;
  31710. ret = wc_ExportSakkePublicKey(key, data, NULL, 1);
  31711. if (ret != BAD_FUNC_ARG)
  31712. return -10251;
  31713. ret = wc_ExportSakkePublicKey(NULL, data, &sz, 1);
  31714. if (ret != BAD_FUNC_ARG)
  31715. return -10252;
  31716. ret = wc_ImportSakkePublicKey(NULL, NULL, sz, 1);
  31717. if (ret != BAD_FUNC_ARG)
  31718. return -10253;
  31719. ret = wc_ImportSakkePublicKey(key, NULL, sz, 1);
  31720. if (ret != BAD_FUNC_ARG)
  31721. return -10254;
  31722. ret = wc_ImportSakkePublicKey(NULL, data, sz, 1);
  31723. if (ret != BAD_FUNC_ARG)
  31724. return -10255;
  31725. ret = wc_GetSakkeAuthSize(NULL, NULL);
  31726. if (ret != BAD_FUNC_ARG)
  31727. return -10256;
  31728. ret = wc_GetSakkeAuthSize(key, NULL);
  31729. if (ret != BAD_FUNC_ARG)
  31730. return -10257;
  31731. ret = wc_GetSakkeAuthSize(NULL, &authSz);
  31732. if (ret != BAD_FUNC_ARG)
  31733. return -10258;
  31734. ret = wc_MakeSakkePointI(NULL, NULL, SAKKE_ID_MAX_SIZE + 1);
  31735. if (ret != BAD_FUNC_ARG)
  31736. return -10259;
  31737. ret = wc_MakeSakkePointI(key, NULL, SAKKE_ID_MAX_SIZE + 1);
  31738. if (ret != BAD_FUNC_ARG)
  31739. return -10260;
  31740. ret = wc_MakeSakkePointI(NULL, id, 1);
  31741. if (ret != BAD_FUNC_ARG)
  31742. return -10261;
  31743. ret = wc_MakeSakkePointI(NULL, NULL, 1);
  31744. if (ret != BAD_FUNC_ARG)
  31745. return -10262;
  31746. ret = wc_MakeSakkePointI(key, id, SAKKE_ID_MAX_SIZE + 1);
  31747. if (ret != BAD_FUNC_ARG)
  31748. return -10263;
  31749. ret = wc_MakeSakkePointI(key, NULL, 1);
  31750. if (ret != BAD_FUNC_ARG)
  31751. return -10264;
  31752. ret = wc_MakeSakkePointI(NULL, id, 1);
  31753. if (ret != BAD_FUNC_ARG)
  31754. return -10265;
  31755. ret = wc_GenerateSakkePointITable(NULL, data, NULL);
  31756. if (ret != BAD_FUNC_ARG)
  31757. return -10266;
  31758. ret = wc_GenerateSakkePointITable(key, data, NULL);
  31759. if (ret != BAD_FUNC_ARG)
  31760. return -10267;
  31761. ret = wc_GenerateSakkePointITable(NULL, data, &len);
  31762. if (ret != BAD_FUNC_ARG)
  31763. return -10268;
  31764. ret = wc_GenerateSakkePointITable(key, NULL, &len);
  31765. if (ret != LENGTH_ONLY_E)
  31766. return -10269;
  31767. len--;
  31768. ret = wc_GenerateSakkePointITable(key, data, &len);
  31769. if (ret != BUFFER_E)
  31770. return -10270;
  31771. ret = wc_SetSakkePointITable(NULL, NULL, 1);
  31772. if (ret != BAD_FUNC_ARG)
  31773. return -10271;
  31774. ret = wc_SetSakkePointITable(key, NULL, 1);
  31775. if (ret != BAD_FUNC_ARG)
  31776. return -10272;
  31777. ret = wc_SetSakkePointITable(NULL, data, 1);
  31778. if (ret != BAD_FUNC_ARG)
  31779. return -10273;
  31780. ret = wc_SetSakkePointITable(key, data, 1);
  31781. if (ret != BUFFER_E)
  31782. return -10274;
  31783. ret = wc_ClearSakkePointITable(NULL);
  31784. if (ret != BAD_FUNC_ARG)
  31785. return -10275;
  31786. ret = wc_GetSakkePointI(NULL, data, NULL);
  31787. if (ret != BAD_FUNC_ARG)
  31788. return -10276;
  31789. ret = wc_GetSakkePointI(key, data, NULL);
  31790. if (ret != BAD_FUNC_ARG)
  31791. return -10277;
  31792. ret = wc_GetSakkePointI(NULL, data, &sz);
  31793. if (ret != BAD_FUNC_ARG)
  31794. return -10278;
  31795. sz = 1;
  31796. ret = wc_GetSakkePointI(key, data, &sz);
  31797. if (ret != BUFFER_E)
  31798. return -10279;
  31799. sz = 256;
  31800. ret = wc_SetSakkePointI(NULL, NULL, 1, NULL, sz);
  31801. if (ret != BAD_FUNC_ARG)
  31802. return -10280;
  31803. ret = wc_SetSakkePointI(key, NULL, 1, NULL, sz);
  31804. if (ret != BAD_FUNC_ARG)
  31805. return -10281;
  31806. ret = wc_SetSakkePointI(NULL, id, 1, NULL, sz);
  31807. if (ret != BAD_FUNC_ARG)
  31808. return -10282;
  31809. ret = wc_SetSakkePointI(NULL, NULL, 1, data, sz);
  31810. if (ret != BAD_FUNC_ARG)
  31811. return -10283;
  31812. ret = wc_SetSakkePointI(key, id, 1, NULL, sz);
  31813. if (ret != BAD_FUNC_ARG)
  31814. return -10284;
  31815. ret = wc_SetSakkePointI(key, NULL, 1, data, sz);
  31816. if (ret != BAD_FUNC_ARG)
  31817. return -10285;
  31818. ret = wc_SetSakkePointI(NULL, id, 1, data, sz);
  31819. if (ret != BAD_FUNC_ARG)
  31820. return -10286;
  31821. ret = wc_SetSakkePointI(key, id, SAKKE_ID_MAX_SIZE + 1, data, sz);
  31822. if (ret != BUFFER_E)
  31823. return -10287;
  31824. ret = wc_SetSakkePointI(key, id, 1, data, sz - 1);
  31825. if (ret != BUFFER_E)
  31826. return -10288;
  31827. ret = wc_SetSakkeIdentity(NULL, NULL, 1);
  31828. if (ret != BAD_FUNC_ARG)
  31829. return -10286;
  31830. ret = wc_SetSakkeIdentity(key, NULL, 1);
  31831. if (ret != BAD_FUNC_ARG)
  31832. return -10286;
  31833. ret = wc_SetSakkeIdentity(NULL, id, 1);
  31834. if (ret != BAD_FUNC_ARG)
  31835. return -10286;
  31836. ssvSz = sizeof(ssv);
  31837. ret = wc_MakeSakkeEncapsulatedSSV(NULL, WC_HASH_TYPE_SHA256, NULL, ssvSz,
  31838. auth, NULL);
  31839. if (ret != BAD_FUNC_ARG)
  31840. return -10289;
  31841. ret = wc_MakeSakkeEncapsulatedSSV(key, WC_HASH_TYPE_SHA256, NULL, ssvSz,
  31842. auth, NULL);
  31843. if (ret != BAD_FUNC_ARG)
  31844. return -10290;
  31845. ret = wc_MakeSakkeEncapsulatedSSV(NULL, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  31846. auth, NULL);
  31847. if (ret != BAD_FUNC_ARG)
  31848. return -10291;
  31849. ret = wc_MakeSakkeEncapsulatedSSV(NULL, WC_HASH_TYPE_SHA256, NULL, ssvSz,
  31850. auth, &authSz);
  31851. if (ret != BAD_FUNC_ARG)
  31852. return -10292;
  31853. ret = wc_MakeSakkeEncapsulatedSSV(key, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  31854. auth, NULL);
  31855. if (ret != BAD_FUNC_ARG)
  31856. return -10293;
  31857. ret = wc_MakeSakkeEncapsulatedSSV(key, WC_HASH_TYPE_SHA256, NULL, ssvSz,
  31858. auth, &authSz);
  31859. if (ret != BAD_FUNC_ARG)
  31860. return -10294;
  31861. ret = wc_MakeSakkeEncapsulatedSSV(NULL, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  31862. auth, &authSz);
  31863. if (ret != BAD_FUNC_ARG)
  31864. return -10295;
  31865. ret = wc_MakeSakkeEncapsulatedSSV(key, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  31866. auth, &authSz);
  31867. if (ret != BAD_STATE_E)
  31868. return -10295;
  31869. ret = wc_GenerateSakkeSSV(NULL, NULL, data, NULL);
  31870. if (ret != BAD_FUNC_ARG)
  31871. return -10296;
  31872. ret = wc_GenerateSakkeSSV(key, rng, data, NULL);
  31873. if (ret != BAD_FUNC_ARG)
  31874. return -10297;
  31875. ret = wc_GenerateSakkeSSV(key, NULL, data, &ssvSz);
  31876. if (ret != BAD_FUNC_ARG)
  31877. return -10298;
  31878. ret = wc_GenerateSakkeSSV(NULL, rng, data, &ssvSz);
  31879. if (ret != BAD_FUNC_ARG)
  31880. return -10299;
  31881. ret = wc_SetSakkeRsk(NULL, NULL, data, 1);
  31882. if (ret != BAD_FUNC_ARG)
  31883. return -10286;
  31884. ret = wc_SetSakkeRsk(key, NULL, data, 1);
  31885. if (ret != BAD_FUNC_ARG)
  31886. return -10286;
  31887. ret = wc_SetSakkeRsk(NULL, rsk, data, 1);
  31888. if (ret != BAD_FUNC_ARG)
  31889. return -10286;
  31890. ssvSz = sizeof(ssv);
  31891. authSz = sizeof(auth);
  31892. ret = wc_DeriveSakkeSSV(NULL, WC_HASH_TYPE_SHA256, NULL, ssvSz, NULL,
  31893. authSz);
  31894. if (ret != BAD_FUNC_ARG)
  31895. return -10300;
  31896. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, NULL, ssvSz, NULL,
  31897. authSz);
  31898. if (ret != BAD_FUNC_ARG)
  31899. return -10300;
  31900. ret = wc_DeriveSakkeSSV(NULL, WC_HASH_TYPE_SHA256, ssv, ssvSz, NULL,
  31901. authSz);
  31902. if (ret != BAD_FUNC_ARG)
  31903. return -10300;
  31904. ret = wc_DeriveSakkeSSV(NULL, WC_HASH_TYPE_SHA256, NULL, ssvSz, auth,
  31905. authSz);
  31906. if (ret != BAD_FUNC_ARG)
  31907. return -10300;
  31908. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, ssv, ssvSz, NULL,
  31909. authSz);
  31910. if (ret != BAD_FUNC_ARG)
  31911. return -10300;
  31912. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, NULL, ssvSz, auth,
  31913. authSz);
  31914. if (ret != BAD_FUNC_ARG)
  31915. return -10300;
  31916. ret = wc_DeriveSakkeSSV(NULL, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  31917. authSz);
  31918. if (ret != BAD_FUNC_ARG)
  31919. return -10300;
  31920. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  31921. authSz);
  31922. if (ret != BAD_STATE_E)
  31923. return -10300;
  31924. ret = wc_SetSakkeIdentity(key, id, 1);
  31925. if (ret != 0)
  31926. return -10286;
  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, 0);
  31932. if (ret != 0)
  31933. return -10286;
  31934. ret = wc_SetSakkeRsk(key, rsk, data, 1);
  31935. if (ret != 0)
  31936. return -10286;
  31937. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  31938. authSz);
  31939. if (ret != BAD_STATE_E)
  31940. return -10300;
  31941. wc_FreeSakkeKey(key);
  31942. return 0;
  31943. }
  31944. static int sakke_kat_derive_test(SakkeKey* key, ecc_point* rsk)
  31945. {
  31946. WOLFSSL_SMALL_STACK_STATIC const byte pubData[] = {
  31947. 0x59, 0x58, 0xEF, 0x1B, 0x16, 0x79, 0xBF, 0x09,
  31948. 0x9B, 0x3A, 0x03, 0x0D, 0xF2, 0x55, 0xAA, 0x6A,
  31949. 0x23, 0xC1, 0xD8, 0xF1, 0x43, 0xD4, 0xD2, 0x3F,
  31950. 0x75, 0x3E, 0x69, 0xBD, 0x27, 0xA8, 0x32, 0xF3,
  31951. 0x8C, 0xB4, 0xAD, 0x53, 0xDD, 0xEF, 0x42, 0x60,
  31952. 0xB0, 0xFE, 0x8B, 0xB4, 0x5C, 0x4C, 0x1F, 0xF5,
  31953. 0x10, 0xEF, 0xFE, 0x30, 0x03, 0x67, 0xA3, 0x7B,
  31954. 0x61, 0xF7, 0x01, 0xD9, 0x14, 0xAE, 0xF0, 0x97,
  31955. 0x24, 0x82, 0x5F, 0xA0, 0x70, 0x7D, 0x61, 0xA6,
  31956. 0xDF, 0xF4, 0xFB, 0xD7, 0x27, 0x35, 0x66, 0xCD,
  31957. 0xDE, 0x35, 0x2A, 0x0B, 0x04, 0xB7, 0xC1, 0x6A,
  31958. 0x78, 0x30, 0x9B, 0xE6, 0x40, 0x69, 0x7D, 0xE7,
  31959. 0x47, 0x61, 0x3A, 0x5F, 0xC1, 0x95, 0xE8, 0xB9,
  31960. 0xF3, 0x28, 0x85, 0x2A, 0x57, 0x9D, 0xB8, 0xF9,
  31961. 0x9B, 0x1D, 0x00, 0x34, 0x47, 0x9E, 0xA9, 0xC5,
  31962. 0x59, 0x5F, 0x47, 0xC4, 0xB2, 0xF5, 0x4F, 0xF2,
  31963. 0x15, 0x08, 0xD3, 0x75, 0x14, 0xDC, 0xF7, 0xA8,
  31964. 0xE1, 0x43, 0xA6, 0x05, 0x8C, 0x09, 0xA6, 0xBF,
  31965. 0x2C, 0x98, 0x58, 0xCA, 0x37, 0xC2, 0x58, 0x06,
  31966. 0x5A, 0xE6, 0xBF, 0x75, 0x32, 0xBC, 0x8B, 0x5B,
  31967. 0x63, 0x38, 0x38, 0x66, 0xE0, 0x75, 0x3C, 0x5A,
  31968. 0xC0, 0xE7, 0x27, 0x09, 0xF8, 0x44, 0x5F, 0x2E,
  31969. 0x61, 0x78, 0xE0, 0x65, 0x85, 0x7E, 0x0E, 0xDA,
  31970. 0x10, 0xF6, 0x82, 0x06, 0xB6, 0x35, 0x05, 0xED,
  31971. 0x87, 0xE5, 0x34, 0xFB, 0x28, 0x31, 0xFF, 0x95,
  31972. 0x7F, 0xB7, 0xDC, 0x61, 0x9D, 0xAE, 0x61, 0x30,
  31973. 0x1E, 0xEA, 0xCC, 0x2F, 0xDA, 0x36, 0x80, 0xEA,
  31974. 0x49, 0x99, 0x25, 0x8A, 0x83, 0x3C, 0xEA, 0x8F,
  31975. 0xC6, 0x7C, 0x6D, 0x19, 0x48, 0x7F, 0xB4, 0x49,
  31976. 0x05, 0x9F, 0x26, 0xCC, 0x8A, 0xAB, 0x65, 0x5A,
  31977. 0xB5, 0x8B, 0x7C, 0xC7, 0x96, 0xE2, 0x4E, 0x9A,
  31978. 0x39, 0x40, 0x95, 0x75, 0x4F, 0x5F, 0x8B, 0xAE
  31979. };
  31980. WOLFSSL_SMALL_STACK_STATIC const byte rskData[] = {
  31981. 0x93, 0xAF, 0x67, 0xE5, 0x00, 0x7B, 0xA6, 0xE6,
  31982. 0xA8, 0x0D, 0xA7, 0x93, 0xDA, 0x30, 0x0F, 0xA4,
  31983. 0xB5, 0x2D, 0x0A, 0x74, 0xE2, 0x5E, 0x6E, 0x7B,
  31984. 0x2B, 0x3D, 0x6E, 0xE9, 0xD1, 0x8A, 0x9B, 0x5C,
  31985. 0x50, 0x23, 0x59, 0x7B, 0xD8, 0x2D, 0x80, 0x62,
  31986. 0xD3, 0x40, 0x19, 0x56, 0x3B, 0xA1, 0xD2, 0x5C,
  31987. 0x0D, 0xC5, 0x6B, 0x7B, 0x97, 0x9D, 0x74, 0xAA,
  31988. 0x50, 0xF2, 0x9F, 0xBF, 0x11, 0xCC, 0x2C, 0x93,
  31989. 0xF5, 0xDF, 0xCA, 0x61, 0x5E, 0x60, 0x92, 0x79,
  31990. 0xF6, 0x17, 0x5C, 0xEA, 0xDB, 0x00, 0xB5, 0x8C,
  31991. 0x6B, 0xEE, 0x1E, 0x7A, 0x2A, 0x47, 0xC4, 0xF0,
  31992. 0xC4, 0x56, 0xF0, 0x52, 0x59, 0xA6, 0xFA, 0x94,
  31993. 0xA6, 0x34, 0xA4, 0x0D, 0xAE, 0x1D, 0xF5, 0x93,
  31994. 0xD4, 0xFE, 0xCF, 0x68, 0x8D, 0x5F, 0xC6, 0x78,
  31995. 0xBE, 0x7E, 0xFC, 0x6D, 0xF3, 0xD6, 0x83, 0x53,
  31996. 0x25, 0xB8, 0x3B, 0x2C, 0x6E, 0x69, 0x03, 0x6B,
  31997. 0x15, 0x5F, 0x0A, 0x27, 0x24, 0x10, 0x94, 0xB0,
  31998. 0x4B, 0xFB, 0x0B, 0xDF, 0xAC, 0x6C, 0x67, 0x0A,
  31999. 0x65, 0xC3, 0x25, 0xD3, 0x9A, 0x06, 0x9F, 0x03,
  32000. 0x65, 0x9D, 0x44, 0xCA, 0x27, 0xD3, 0xBE, 0x8D,
  32001. 0xF3, 0x11, 0x17, 0x2B, 0x55, 0x41, 0x60, 0x18,
  32002. 0x1C, 0xBE, 0x94, 0xA2, 0xA7, 0x83, 0x32, 0x0C,
  32003. 0xED, 0x59, 0x0B, 0xC4, 0x26, 0x44, 0x70, 0x2C,
  32004. 0xF3, 0x71, 0x27, 0x1E, 0x49, 0x6B, 0xF2, 0x0F,
  32005. 0x58, 0x8B, 0x78, 0xA1, 0xBC, 0x01, 0xEC, 0xBB,
  32006. 0x65, 0x59, 0x93, 0x4B, 0xDD, 0x2F, 0xB6, 0x5D,
  32007. 0x28, 0x84, 0x31, 0x8A, 0x33, 0xD1, 0xA4, 0x2A,
  32008. 0xDF, 0x5E, 0x33, 0xCC, 0x58, 0x00, 0x28, 0x0B,
  32009. 0x28, 0x35, 0x64, 0x97, 0xF8, 0x71, 0x35, 0xBA,
  32010. 0xB9, 0x61, 0x2A, 0x17, 0x26, 0x04, 0x24, 0x40,
  32011. 0x9A, 0xC1, 0x5F, 0xEE, 0x99, 0x6B, 0x74, 0x4C,
  32012. 0x33, 0x21, 0x51, 0x23, 0x5D, 0xEC, 0xB0, 0xF5
  32013. };
  32014. WOLFSSL_SMALL_STACK_STATIC const byte id[] = {
  32015. 0x32, 0x30, 0x31, 0x31, 0x2D, 0x30, 0x32, 0x00,
  32016. 0x74, 0x65, 0x6C, 0x3A, 0x2B, 0x34, 0x34, 0x37,
  32017. 0x37, 0x30, 0x30, 0x39, 0x30, 0x30, 0x31, 0x32,
  32018. 0x33, 0x00
  32019. };
  32020. WOLFSSL_SMALL_STACK_STATIC const byte ssv[] = {
  32021. 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0,
  32022. 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0
  32023. };
  32024. WOLFSSL_SMALL_STACK_STATIC const byte auth[] = {
  32025. 0x04,
  32026. 0x44, 0xE8, 0xAD, 0x44, 0xAB, 0x85, 0x92, 0xA6,
  32027. 0xA5, 0xA3, 0xDD, 0xCA, 0x5C, 0xF8, 0x96, 0xC7,
  32028. 0x18, 0x04, 0x36, 0x06, 0xA0, 0x1D, 0x65, 0x0D,
  32029. 0xEF, 0x37, 0xA0, 0x1F, 0x37, 0xC2, 0x28, 0xC3,
  32030. 0x32, 0xFC, 0x31, 0x73, 0x54, 0xE2, 0xC2, 0x74,
  32031. 0xD4, 0xDA, 0xF8, 0xAD, 0x00, 0x10, 0x54, 0xC7,
  32032. 0x6C, 0xE5, 0x79, 0x71, 0xC6, 0xF4, 0x48, 0x6D,
  32033. 0x57, 0x23, 0x04, 0x32, 0x61, 0xC5, 0x06, 0xEB,
  32034. 0xF5, 0xBE, 0x43, 0x8F, 0x53, 0xDE, 0x04, 0xF0,
  32035. 0x67, 0xC7, 0x76, 0xE0, 0xDD, 0x3B, 0x71, 0xA6,
  32036. 0x29, 0x01, 0x33, 0x28, 0x37, 0x25, 0xA5, 0x32,
  32037. 0xF2, 0x1A, 0xF1, 0x45, 0x12, 0x6D, 0xC1, 0xD7,
  32038. 0x77, 0xEC, 0xC2, 0x7B, 0xE5, 0x08, 0x35, 0xBD,
  32039. 0x28, 0x09, 0x8B, 0x8A, 0x73, 0xD9, 0xF8, 0x01,
  32040. 0xD8, 0x93, 0x79, 0x3A, 0x41, 0xFF, 0x5C, 0x49,
  32041. 0xB8, 0x7E, 0x79, 0xF2, 0xBE, 0x4D, 0x56, 0xCE,
  32042. 0x55, 0x7E, 0x13, 0x4A, 0xD8, 0x5B, 0xB1, 0xD4,
  32043. 0xB9, 0xCE, 0x4F, 0x8B, 0xE4, 0xB0, 0x8A, 0x12,
  32044. 0xBA, 0xBF, 0x55, 0xB1, 0xD6, 0xF1, 0xD7, 0xA6,
  32045. 0x38, 0x01, 0x9E, 0xA2, 0x8E, 0x15, 0xAB, 0x1C,
  32046. 0x9F, 0x76, 0x37, 0x5F, 0xDD, 0x12, 0x10, 0xD4,
  32047. 0xF4, 0x35, 0x1B, 0x9A, 0x00, 0x94, 0x86, 0xB7,
  32048. 0xF3, 0xED, 0x46, 0xC9, 0x65, 0xDE, 0xD2, 0xD8,
  32049. 0x0D, 0xAD, 0xE4, 0xF3, 0x8C, 0x67, 0x21, 0xD5,
  32050. 0x2C, 0x3A, 0xD1, 0x03, 0xA1, 0x0E, 0xBD, 0x29,
  32051. 0x59, 0x24, 0x8B, 0x4E, 0xF0, 0x06, 0x83, 0x6B,
  32052. 0xF0, 0x97, 0x44, 0x8E, 0x61, 0x07, 0xC9, 0xED,
  32053. 0xEE, 0x9F, 0xB7, 0x04, 0x82, 0x3D, 0xF1, 0x99,
  32054. 0xF8, 0x32, 0xC9, 0x05, 0xAE, 0x45, 0xF8, 0xA2,
  32055. 0x47, 0xA0, 0x72, 0xD8, 0xEF, 0x72, 0x9E, 0xAB,
  32056. 0xC5, 0xE2, 0x75, 0x74, 0xB0, 0x77, 0x39, 0xB3,
  32057. 0x4B, 0xE7, 0x4A, 0x53, 0x2F, 0x74, 0x7B, 0x86
  32058. };
  32059. WOLFSSL_SMALL_STACK_STATIC const byte encSsv[] = {
  32060. 0x89, 0xE0, 0xBC, 0x66, 0x1A, 0xA1, 0xE9, 0x16,
  32061. 0x38, 0xE6, 0xAC, 0xC8, 0x4E, 0x49, 0x65, 0x07
  32062. };
  32063. int ret;
  32064. int valid;
  32065. byte pubKey[sizeof(pubData) + 1];
  32066. word32 sz = sizeof(pubKey);
  32067. byte tmpSsv[sizeof(encSsv)];
  32068. byte* iTable = NULL;
  32069. word32 iTableLen;
  32070. byte* table = NULL;
  32071. word32 len;
  32072. ret = wc_ImportSakkePublicKey(key, pubData, sizeof(pubData), 0);
  32073. if (ret != 0)
  32074. return -10315;
  32075. ret = wc_DecodeSakkeRsk(key, rskData, sizeof(rskData), rsk);
  32076. if (ret != 0)
  32077. return -10316;
  32078. ret = wc_ValidateSakkeRsk(key, id, sizeof(id), rsk, &valid);
  32079. if (ret != 0)
  32080. return -10317;
  32081. if (valid != 1)
  32082. return -10318;
  32083. ret = wc_SetSakkeRsk(key, rsk, NULL, 0);
  32084. if (ret != 0)
  32085. return -10319;
  32086. ret = wc_SetSakkeIdentity(key, id, sizeof(id));
  32087. if (ret != 0)
  32088. return -10319;
  32089. XMEMCPY(tmpSsv, encSsv, sizeof(encSsv));
  32090. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, tmpSsv, sizeof(tmpSsv),
  32091. auth, sizeof(auth));
  32092. if (ret != 0)
  32093. return -10322;
  32094. if (XMEMCMP(tmpSsv, ssv, sizeof(ssv)) != 0)
  32095. return -10320;
  32096. ret = wc_MakeSakkePointI(key, id, sizeof(id));
  32097. if (ret != 0)
  32098. return -10321;
  32099. iTableLen = 0;
  32100. ret = wc_GenerateSakkePointITable(key, NULL, &iTableLen);
  32101. if (ret != LENGTH_ONLY_E)
  32102. return -10322;
  32103. if (iTableLen != 0) {
  32104. iTable = (byte*)XMALLOC(iTableLen, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32105. if (iTable == NULL)
  32106. return -10323;
  32107. ret = wc_GenerateSakkePointITable(key, iTable, &iTableLen);
  32108. if (ret != 0)
  32109. return -10324;
  32110. }
  32111. len = 0;
  32112. ret = wc_GenerateSakkeRskTable(key, rsk, NULL, &len);
  32113. if (ret != LENGTH_ONLY_E)
  32114. return -10325;
  32115. if (len > 0) {
  32116. table = (byte*)XMALLOC(len, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32117. if (table == NULL)
  32118. return -10326;
  32119. ret = wc_GenerateSakkeRskTable(key, rsk, table, &len);
  32120. if (ret != 0)
  32121. return -10327;
  32122. }
  32123. ret = wc_SetSakkeRsk(key, rsk, table, len);
  32124. if (ret != 0)
  32125. return -10319;
  32126. XMEMCPY(tmpSsv, encSsv, sizeof(encSsv));
  32127. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, tmpSsv, sizeof(tmpSsv),
  32128. auth, sizeof(auth));
  32129. if (ret != 0)
  32130. return -10328;
  32131. if (XMEMCMP(tmpSsv, ssv, sizeof(ssv)) != 0)
  32132. return -10329;
  32133. /* Don't reference table that is about to be freed. */
  32134. ret = wc_ClearSakkePointITable(key);
  32135. if (ret != 0)
  32136. return -10330;
  32137. /* Dispose of tables */
  32138. if (iTable != NULL)
  32139. XFREE(iTable, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32140. if (table != NULL)
  32141. XFREE(table, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32142. /* Make sure the key public key is exportable - convert to Montgomery form
  32143. * in Validation.
  32144. */
  32145. ret = wc_ExportSakkePublicKey(key, pubKey, &sz, 1);
  32146. if (ret != 0)
  32147. return -10331;
  32148. if (sz != sizeof(pubData))
  32149. return -10332;
  32150. if (XMEMCMP(pubKey, pubData, sizeof(pubData)) != 0)
  32151. return -10333;
  32152. sz = sizeof(pubData) + 1;
  32153. ret = wc_ExportSakkePublicKey(key, pubKey, &sz, 0);
  32154. if (ret != 0)
  32155. return -10334;
  32156. if (sz != sizeof(pubData) + 1)
  32157. return -10335;
  32158. if (pubKey[0] != 0x04)
  32159. return -10336;
  32160. if (XMEMCMP(pubKey + 1, pubData, sizeof(pubData)) != 0)
  32161. return -10337;
  32162. return 0;
  32163. }
  32164. static int sakke_kat_encapsulate_test(SakkeKey* key)
  32165. {
  32166. static const byte pubData[] = {
  32167. 0x59, 0x58, 0xEF, 0x1B, 0x16, 0x79, 0xBF, 0x09,
  32168. 0x9B, 0x3A, 0x03, 0x0D, 0xF2, 0x55, 0xAA, 0x6A,
  32169. 0x23, 0xC1, 0xD8, 0xF1, 0x43, 0xD4, 0xD2, 0x3F,
  32170. 0x75, 0x3E, 0x69, 0xBD, 0x27, 0xA8, 0x32, 0xF3,
  32171. 0x8C, 0xB4, 0xAD, 0x53, 0xDD, 0xEF, 0x42, 0x60,
  32172. 0xB0, 0xFE, 0x8B, 0xB4, 0x5C, 0x4C, 0x1F, 0xF5,
  32173. 0x10, 0xEF, 0xFE, 0x30, 0x03, 0x67, 0xA3, 0x7B,
  32174. 0x61, 0xF7, 0x01, 0xD9, 0x14, 0xAE, 0xF0, 0x97,
  32175. 0x24, 0x82, 0x5F, 0xA0, 0x70, 0x7D, 0x61, 0xA6,
  32176. 0xDF, 0xF4, 0xFB, 0xD7, 0x27, 0x35, 0x66, 0xCD,
  32177. 0xDE, 0x35, 0x2A, 0x0B, 0x04, 0xB7, 0xC1, 0x6A,
  32178. 0x78, 0x30, 0x9B, 0xE6, 0x40, 0x69, 0x7D, 0xE7,
  32179. 0x47, 0x61, 0x3A, 0x5F, 0xC1, 0x95, 0xE8, 0xB9,
  32180. 0xF3, 0x28, 0x85, 0x2A, 0x57, 0x9D, 0xB8, 0xF9,
  32181. 0x9B, 0x1D, 0x00, 0x34, 0x47, 0x9E, 0xA9, 0xC5,
  32182. 0x59, 0x5F, 0x47, 0xC4, 0xB2, 0xF5, 0x4F, 0xF2,
  32183. 0x15, 0x08, 0xD3, 0x75, 0x14, 0xDC, 0xF7, 0xA8,
  32184. 0xE1, 0x43, 0xA6, 0x05, 0x8C, 0x09, 0xA6, 0xBF,
  32185. 0x2C, 0x98, 0x58, 0xCA, 0x37, 0xC2, 0x58, 0x06,
  32186. 0x5A, 0xE6, 0xBF, 0x75, 0x32, 0xBC, 0x8B, 0x5B,
  32187. 0x63, 0x38, 0x38, 0x66, 0xE0, 0x75, 0x3C, 0x5A,
  32188. 0xC0, 0xE7, 0x27, 0x09, 0xF8, 0x44, 0x5F, 0x2E,
  32189. 0x61, 0x78, 0xE0, 0x65, 0x85, 0x7E, 0x0E, 0xDA,
  32190. 0x10, 0xF6, 0x82, 0x06, 0xB6, 0x35, 0x05, 0xED,
  32191. 0x87, 0xE5, 0x34, 0xFB, 0x28, 0x31, 0xFF, 0x95,
  32192. 0x7F, 0xB7, 0xDC, 0x61, 0x9D, 0xAE, 0x61, 0x30,
  32193. 0x1E, 0xEA, 0xCC, 0x2F, 0xDA, 0x36, 0x80, 0xEA,
  32194. 0x49, 0x99, 0x25, 0x8A, 0x83, 0x3C, 0xEA, 0x8F,
  32195. 0xC6, 0x7C, 0x6D, 0x19, 0x48, 0x7F, 0xB4, 0x49,
  32196. 0x05, 0x9F, 0x26, 0xCC, 0x8A, 0xAB, 0x65, 0x5A,
  32197. 0xB5, 0x8B, 0x7C, 0xC7, 0x96, 0xE2, 0x4E, 0x9A,
  32198. 0x39, 0x40, 0x95, 0x75, 0x4F, 0x5F, 0x8B, 0xAE
  32199. };
  32200. static const byte id[] = {
  32201. 0x32, 0x30, 0x31, 0x31, 0x2D, 0x30, 0x32, 0x00,
  32202. 0x74, 0x65, 0x6C, 0x3A, 0x2B, 0x34, 0x34, 0x37,
  32203. 0x37, 0x30, 0x30, 0x39, 0x30, 0x30, 0x31, 0x32,
  32204. 0x33, 0x00
  32205. };
  32206. static word32 idSz = sizeof(id);
  32207. byte ssv[] = {
  32208. 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0,
  32209. 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0
  32210. };
  32211. static word16 ssvSz = sizeof(ssv);
  32212. static const byte expAuth[] = {
  32213. 0x04,
  32214. 0x44, 0xE8, 0xAD, 0x44, 0xAB, 0x85, 0x92, 0xA6,
  32215. 0xA5, 0xA3, 0xDD, 0xCA, 0x5C, 0xF8, 0x96, 0xC7,
  32216. 0x18, 0x04, 0x36, 0x06, 0xA0, 0x1D, 0x65, 0x0D,
  32217. 0xEF, 0x37, 0xA0, 0x1F, 0x37, 0xC2, 0x28, 0xC3,
  32218. 0x32, 0xFC, 0x31, 0x73, 0x54, 0xE2, 0xC2, 0x74,
  32219. 0xD4, 0xDA, 0xF8, 0xAD, 0x00, 0x10, 0x54, 0xC7,
  32220. 0x6C, 0xE5, 0x79, 0x71, 0xC6, 0xF4, 0x48, 0x6D,
  32221. 0x57, 0x23, 0x04, 0x32, 0x61, 0xC5, 0x06, 0xEB,
  32222. 0xF5, 0xBE, 0x43, 0x8F, 0x53, 0xDE, 0x04, 0xF0,
  32223. 0x67, 0xC7, 0x76, 0xE0, 0xDD, 0x3B, 0x71, 0xA6,
  32224. 0x29, 0x01, 0x33, 0x28, 0x37, 0x25, 0xA5, 0x32,
  32225. 0xF2, 0x1A, 0xF1, 0x45, 0x12, 0x6D, 0xC1, 0xD7,
  32226. 0x77, 0xEC, 0xC2, 0x7B, 0xE5, 0x08, 0x35, 0xBD,
  32227. 0x28, 0x09, 0x8B, 0x8A, 0x73, 0xD9, 0xF8, 0x01,
  32228. 0xD8, 0x93, 0x79, 0x3A, 0x41, 0xFF, 0x5C, 0x49,
  32229. 0xB8, 0x7E, 0x79, 0xF2, 0xBE, 0x4D, 0x56, 0xCE,
  32230. 0x55, 0x7E, 0x13, 0x4A, 0xD8, 0x5B, 0xB1, 0xD4,
  32231. 0xB9, 0xCE, 0x4F, 0x8B, 0xE4, 0xB0, 0x8A, 0x12,
  32232. 0xBA, 0xBF, 0x55, 0xB1, 0xD6, 0xF1, 0xD7, 0xA6,
  32233. 0x38, 0x01, 0x9E, 0xA2, 0x8E, 0x15, 0xAB, 0x1C,
  32234. 0x9F, 0x76, 0x37, 0x5F, 0xDD, 0x12, 0x10, 0xD4,
  32235. 0xF4, 0x35, 0x1B, 0x9A, 0x00, 0x94, 0x86, 0xB7,
  32236. 0xF3, 0xED, 0x46, 0xC9, 0x65, 0xDE, 0xD2, 0xD8,
  32237. 0x0D, 0xAD, 0xE4, 0xF3, 0x8C, 0x67, 0x21, 0xD5,
  32238. 0x2C, 0x3A, 0xD1, 0x03, 0xA1, 0x0E, 0xBD, 0x29,
  32239. 0x59, 0x24, 0x8B, 0x4E, 0xF0, 0x06, 0x83, 0x6B,
  32240. 0xF0, 0x97, 0x44, 0x8E, 0x61, 0x07, 0xC9, 0xED,
  32241. 0xEE, 0x9F, 0xB7, 0x04, 0x82, 0x3D, 0xF1, 0x99,
  32242. 0xF8, 0x32, 0xC9, 0x05, 0xAE, 0x45, 0xF8, 0xA2,
  32243. 0x47, 0xA0, 0x72, 0xD8, 0xEF, 0x72, 0x9E, 0xAB,
  32244. 0xC5, 0xE2, 0x75, 0x74, 0xB0, 0x77, 0x39, 0xB3,
  32245. 0x4B, 0xE7, 0x4A, 0x53, 0x2F, 0x74, 0x7B, 0x86
  32246. };
  32247. static const byte encSsv[] = {
  32248. 0x89, 0xE0, 0xBC, 0x66, 0x1A, 0xA1, 0xE9, 0x16,
  32249. 0x38, 0xE6, 0xAC, 0xC8, 0x4E, 0x49, 0x65, 0x07
  32250. };
  32251. int ret;
  32252. byte auth[257];
  32253. word16 authSz = sizeof(auth);
  32254. ret = wc_ImportSakkePublicKey(key, pubData, sizeof(pubData), 0);
  32255. if (ret != 0)
  32256. return -10334;
  32257. ret = wc_SetSakkeIdentity(key, id, idSz);
  32258. if (ret != 0)
  32259. return -10335;
  32260. ret = wc_MakeSakkeEncapsulatedSSV(key, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  32261. auth, &authSz);
  32262. if (ret != 0)
  32263. return -10336;
  32264. if (authSz != 257)
  32265. return -10337;
  32266. if (XMEMCMP(ssv, encSsv, ssvSz) != 0)
  32267. return -10338;
  32268. if (XMEMCMP(auth, expAuth, authSz) != 0)
  32269. return -10339;
  32270. return 0;
  32271. }
  32272. static int sakke_make_key_test(SakkeKey* priv, SakkeKey* pub, SakkeKey* key,
  32273. WC_RNG* rng, ecc_point* rsk)
  32274. {
  32275. int ret;
  32276. byte data[440];
  32277. byte pubData[257];
  32278. word32 sz;
  32279. char mail[] = "test@wolfssl.com";
  32280. byte* id = (byte*)mail;
  32281. word32 idSz = (word32)XSTRLEN(mail);
  32282. int valid;
  32283. ecc_point* pubKey = rsk;
  32284. ret = wc_InitSakkeKey_ex(key, 128, ECC_SAKKE_1, NULL, INVALID_DEVID);
  32285. if (ret != 0)
  32286. return -10339;
  32287. ret = wc_MakeSakkeKey(priv, rng);
  32288. if (ret != 0)
  32289. return -10340;
  32290. ret = wc_ExportSakkeKey(priv, NULL, &sz);
  32291. if (ret != LENGTH_ONLY_E)
  32292. return -10341;
  32293. if (sz != 384)
  32294. return -10342;
  32295. sz--;
  32296. ret = wc_ExportSakkeKey(priv, data, &sz);
  32297. if (ret == 0)
  32298. return -10343;
  32299. sz++;
  32300. ret = wc_ExportSakkeKey(priv, data, &sz);
  32301. if (ret != 0)
  32302. return -10344;
  32303. if (sz != 384)
  32304. return -10345;
  32305. ret = wc_ImportSakkeKey(key, data, sz - 1);
  32306. if (ret == 0)
  32307. return -10346;
  32308. ret = wc_ImportSakkeKey(key, data, sz);
  32309. if (ret != 0)
  32310. return -10347;
  32311. wc_FreeSakkeKey(key);
  32312. ret = wc_InitSakkeKey_ex(key, 128, ECC_SAKKE_1, NULL, INVALID_DEVID);
  32313. if (ret != 0)
  32314. return -10348;
  32315. ret = wc_ExportSakkePrivateKey(priv, NULL, &sz);
  32316. if (ret != LENGTH_ONLY_E)
  32317. return -10349;
  32318. if (sz != 128)
  32319. return -10350;
  32320. sz--;
  32321. ret = wc_ExportSakkePrivateKey(priv, data, &sz);
  32322. if (ret == 0)
  32323. return -10351;
  32324. sz++;
  32325. ret = wc_ExportSakkePrivateKey(priv, data, &sz);
  32326. if (ret != 0)
  32327. return -10352;
  32328. if (sz != 128)
  32329. return -10353;
  32330. ret = wc_ImportSakkePrivateKey(key, data, sz - 1);
  32331. if (ret == 0)
  32332. return -10354;
  32333. ret = wc_ImportSakkePrivateKey(key, data, sz);
  32334. if (ret != 0)
  32335. return -10355;
  32336. ret = wc_MakeSakkePublicKey(key, pubKey);
  32337. if (ret != 0)
  32338. return -10356;
  32339. ret = wc_ExportSakkePublicKey(priv, NULL, &sz, 1);
  32340. if (ret != LENGTH_ONLY_E)
  32341. return -10357;
  32342. if (sz != 256)
  32343. return -10358;
  32344. sz--;
  32345. ret = wc_ExportSakkePublicKey(priv, data, &sz, 1);
  32346. if (ret == 0)
  32347. return -10359;
  32348. sz++;
  32349. ret = wc_ExportSakkePublicKey(priv, data, &sz, 1);
  32350. if (ret != 0)
  32351. return -10360;
  32352. if (sz != 256)
  32353. return -10361;
  32354. ret = wc_ImportSakkePublicKey(pub, data, sz - 1, 1);
  32355. if (ret == 0)
  32356. return -10362;
  32357. ret = wc_ImportSakkePublicKey(pub, data, sz, 1);
  32358. if (ret != 0)
  32359. return -10363;
  32360. ret = wc_ExportSakkePublicKey(pub, pubData, &sz, 1);
  32361. if (ret != 0)
  32362. return -10364;
  32363. if (sz != 256)
  32364. return -10365;
  32365. if (XMEMCMP(data, pubData, sz) != 0)
  32366. return -10366;
  32367. ret = wc_MakeSakkeRsk(priv, id, idSz, rsk);
  32368. if (ret != 0)
  32369. return -10367;
  32370. ret = wc_ValidateSakkeRsk(priv, id, idSz, rsk, &valid);
  32371. if (ret != 0)
  32372. return -10368;
  32373. if (valid != 1)
  32374. return -10369;
  32375. ret = wc_ValidateSakkeRsk(pub, id, idSz, rsk, &valid);
  32376. if (ret != 0)
  32377. return -10370;
  32378. if (valid != 1)
  32379. return -10371;
  32380. sz = sizeof(data);
  32381. ret = wc_EncodeSakkeRsk(priv, rsk, data, &sz, 1);
  32382. if (ret != 0)
  32383. return -10372;
  32384. if (sz != 256)
  32385. return -10373;
  32386. ret = wc_DecodeSakkeRsk(priv, data, sz, rsk);
  32387. if (ret != 0)
  32388. return -10374;
  32389. sz = sizeof(pubData);
  32390. ret = wc_EncodeSakkeRsk(priv, rsk, pubData, &sz, 0);
  32391. if (ret != 0)
  32392. return -10375;
  32393. if (sz != sizeof(pubData))
  32394. return -10376;
  32395. ret = wc_DecodeSakkeRsk(priv, pubData, sz, rsk);
  32396. if (ret != 0)
  32397. return -10377;
  32398. wc_FreeSakkeKey(key);
  32399. return 0;
  32400. }
  32401. static int sakke_op_test(SakkeKey* priv, SakkeKey* pub, WC_RNG* rng,
  32402. ecc_point* rsk)
  32403. {
  32404. int ret;
  32405. byte ssv[16];
  32406. word16 ssvSz;
  32407. byte auth[257];
  32408. word16 authSz;
  32409. char mail[] = "test@wolfssl.com";
  32410. byte* id = (byte*)mail;
  32411. word32 idSz = (word32)XSTRLEN(mail);
  32412. byte pointI[256];
  32413. word32 sz;
  32414. ret = wc_GenerateSakkeSSV(pub, rng, NULL, &ssvSz);
  32415. if (ret != LENGTH_ONLY_E)
  32416. return -10375;
  32417. if (ssvSz != 16)
  32418. return -10376;
  32419. ssvSz += 128;
  32420. ret = wc_GenerateSakkeSSV(pub, rng, ssv, &ssvSz);
  32421. if (ret == 0)
  32422. return -10377;
  32423. ssvSz -= 128;
  32424. ret = wc_GenerateSakkeSSV(pub, rng, ssv, &ssvSz);
  32425. if (ret != 0)
  32426. return -10378;
  32427. if (ssvSz != 16)
  32428. return -10379;
  32429. ret = wc_GetSakkeAuthSize(pub, &authSz);
  32430. if (ret != 0)
  32431. return -10380;
  32432. ret = wc_SetSakkeIdentity(pub, id, idSz);
  32433. if (ret != 0)
  32434. return -10380;
  32435. ret = wc_MakeSakkeEncapsulatedSSV(pub, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  32436. NULL, &authSz);
  32437. if (ret != LENGTH_ONLY_E)
  32438. return -10381;
  32439. if (authSz != 257)
  32440. return -10382;
  32441. authSz--;
  32442. ret = wc_MakeSakkeEncapsulatedSSV(pub, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  32443. auth, &authSz);
  32444. if (ret == 0)
  32445. return -10383;
  32446. authSz++;
  32447. ret = wc_MakeSakkeEncapsulatedSSV(pub, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  32448. auth, &authSz);
  32449. if (ret != 0)
  32450. return -10384;
  32451. if (authSz != 257)
  32452. return -10385;
  32453. ret = wc_GetSakkePointI(pub, NULL, &sz);
  32454. if (ret != LENGTH_ONLY_E)
  32455. return -10386;
  32456. if (sz != 256)
  32457. return -10387;
  32458. ret = wc_GetSakkePointI(pub, pointI, &sz);
  32459. if (ret != 0)
  32460. return -10388;
  32461. if (sz != 256)
  32462. return -10389;
  32463. /* Bogus identity - make it check and regenerate I. */
  32464. ret = wc_MakeSakkePointI(pub, ssv, ssvSz);
  32465. if (ret != 0)
  32466. return -10391;
  32467. ret = wc_MakeSakkeEncapsulatedSSV(pub, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  32468. auth, &authSz);
  32469. if (ret != 0)
  32470. return -10392;
  32471. if (authSz != 257)
  32472. return -10393;
  32473. ret = wc_SetSakkeRsk(priv, rsk, NULL, 0);
  32474. if (ret != 0)
  32475. return -10392;
  32476. ret = wc_SetSakkeIdentity(priv, id, idSz);
  32477. if (ret != 0)
  32478. return -10392;
  32479. authSz--;
  32480. ret = wc_DeriveSakkeSSV(priv, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  32481. authSz);
  32482. if (ret == 0)
  32483. return -10394;
  32484. authSz++;
  32485. ret = wc_DeriveSakkeSSV(priv, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  32486. authSz);
  32487. if (ret != 0)
  32488. return -10395;
  32489. ssv[0] ^= 0x80;
  32490. ret = wc_DeriveSakkeSSV(priv, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  32491. authSz);
  32492. if (ret != SAKKE_VERIFY_FAIL_E)
  32493. return -10396;
  32494. ssv[0] ^= 0x80;
  32495. /* Bogus identity - make it check and regenerate I. */
  32496. ret = wc_MakeSakkePointI(pub, ssv, idSz);
  32497. if (ret != 0)
  32498. return -10397;
  32499. ret = wc_DeriveSakkeSSV(priv, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  32500. authSz);
  32501. if (ret != 0)
  32502. return -10398;
  32503. return 0;
  32504. }
  32505. int sakke_test(void)
  32506. {
  32507. int ret = 0;
  32508. WC_RNG rng;
  32509. int rng_inited = 0;
  32510. SakkeKey* priv = NULL;
  32511. SakkeKey* pub = NULL;
  32512. SakkeKey* key = NULL;
  32513. ecc_point* rsk = NULL;
  32514. priv = (SakkeKey*)XMALLOC(sizeof(SakkeKey), HEAP_HINT,
  32515. DYNAMIC_TYPE_TMP_BUFFER);
  32516. if (priv == NULL)
  32517. ret = -10404;
  32518. else
  32519. XMEMSET(priv, 0, sizeof(*priv));
  32520. if (ret == 0) {
  32521. pub = (SakkeKey*)XMALLOC(sizeof(SakkeKey), HEAP_HINT,
  32522. DYNAMIC_TYPE_TMP_BUFFER);
  32523. if (pub == NULL)
  32524. ret = -10405;
  32525. else
  32526. XMEMSET(pub, 0, sizeof(*pub));
  32527. }
  32528. if (ret == 0) {
  32529. key = (SakkeKey*)XMALLOC(sizeof(SakkeKey), HEAP_HINT,
  32530. DYNAMIC_TYPE_TMP_BUFFER);
  32531. if (key == NULL)
  32532. ret = -10406;
  32533. else
  32534. XMEMSET(key, 0, sizeof(*key));
  32535. }
  32536. if (ret == 0) {
  32537. #ifndef HAVE_FIPS
  32538. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  32539. #else
  32540. ret = wc_InitRng(&rng);
  32541. #endif
  32542. if (ret == 0)
  32543. rng_inited = 1;
  32544. else
  32545. ret = -10400;
  32546. }
  32547. if (ret == 0) {
  32548. rsk = wc_ecc_new_point();
  32549. if (rsk == NULL)
  32550. ret = -10401;
  32551. }
  32552. if (ret == 0) {
  32553. ret = wc_InitSakkeKey(pub, HEAP_HINT, INVALID_DEVID);
  32554. if (ret != 0)
  32555. ret = -10402;
  32556. }
  32557. if (ret == 0) {
  32558. ret = wc_InitSakkeKey(priv, HEAP_HINT, INVALID_DEVID);
  32559. if (ret != 0)
  32560. ret = -10403;
  32561. }
  32562. if (ret == 0) {
  32563. ret = sakke_api_test(&rng, key, rsk);
  32564. }
  32565. if (ret == 0) {
  32566. ret = sakke_kat_derive_test(pub, rsk);
  32567. }
  32568. if (ret == 0) {
  32569. ret = sakke_kat_encapsulate_test(pub);
  32570. }
  32571. if (ret == 0) {
  32572. ret = sakke_make_key_test(priv, pub, key, &rng, rsk);
  32573. }
  32574. if (ret == 0) {
  32575. ret = sakke_op_test(priv, pub, &rng, rsk);
  32576. }
  32577. if (rsk != NULL) {
  32578. wc_ecc_forcezero_point(rsk);
  32579. wc_ecc_del_point(rsk);
  32580. }
  32581. if (rng_inited)
  32582. wc_FreeRng(&rng);
  32583. if (key != NULL)
  32584. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32585. if (pub != NULL) {
  32586. wc_FreeSakkeKey(pub);
  32587. XFREE(pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32588. }
  32589. if (priv != NULL) {
  32590. wc_FreeSakkeKey(priv);
  32591. XFREE(priv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32592. }
  32593. return ret;
  32594. }
  32595. #endif /* WOLFCRYPT_HAVE_SAKKE */
  32596. #if defined(WOLFSSL_CMAC) && !defined(NO_AES)
  32597. typedef struct CMAC_Test_Case {
  32598. int type;
  32599. int partial;
  32600. const byte* m;
  32601. word32 mSz;
  32602. const byte* k;
  32603. word32 kSz;
  32604. const byte* t;
  32605. word32 tSz;
  32606. } CMAC_Test_Case;
  32607. WOLFSSL_TEST_SUBROUTINE int cmac_test(void)
  32608. {
  32609. #ifdef WOLFSSL_AES_128
  32610. WOLFSSL_SMALL_STACK_STATIC const byte k128[] =
  32611. {
  32612. 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
  32613. 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c
  32614. };
  32615. #define KLEN_128 (sizeof(k128))
  32616. #endif
  32617. #ifdef WOLFSSL_AES_192
  32618. WOLFSSL_SMALL_STACK_STATIC const byte k192[] =
  32619. {
  32620. 0x8e, 0x73, 0xb0, 0xf7, 0xda, 0x0e, 0x64, 0x52,
  32621. 0xc8, 0x10, 0xf3, 0x2b, 0x80, 0x90, 0x79, 0xe5,
  32622. 0x62, 0xf8, 0xea, 0xd2, 0x52, 0x2c, 0x6b, 0x7b
  32623. };
  32624. #define KLEN_192 (sizeof(k192))
  32625. #endif
  32626. #ifdef WOLFSSL_AES_256
  32627. WOLFSSL_SMALL_STACK_STATIC const byte k256[] =
  32628. {
  32629. 0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe,
  32630. 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
  32631. 0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7,
  32632. 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4
  32633. };
  32634. #define KLEN_256 (sizeof(k256))
  32635. #endif
  32636. WOLFSSL_SMALL_STACK_STATIC const byte m[] =
  32637. {
  32638. 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
  32639. 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
  32640. 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c,
  32641. 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51,
  32642. 0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11,
  32643. 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef,
  32644. 0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17,
  32645. 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10
  32646. };
  32647. #define MLEN_0 (0)
  32648. #define MLEN_128 (128/8)
  32649. #define MLEN_320 (320/8)
  32650. #define MLEN_319 (MLEN_320 - 1)
  32651. #define MLEN_512 (512/8)
  32652. #ifdef WOLFSSL_AES_128
  32653. WOLFSSL_SMALL_STACK_STATIC const byte t128_0[] =
  32654. {
  32655. 0xbb, 0x1d, 0x69, 0x29, 0xe9, 0x59, 0x37, 0x28,
  32656. 0x7f, 0xa3, 0x7d, 0x12, 0x9b, 0x75, 0x67, 0x46
  32657. };
  32658. WOLFSSL_SMALL_STACK_STATIC const byte t128_128[] =
  32659. {
  32660. 0x07, 0x0a, 0x16, 0xb4, 0x6b, 0x4d, 0x41, 0x44,
  32661. 0xf7, 0x9b, 0xdd, 0x9d, 0xd0, 0x4a, 0x28, 0x7c
  32662. };
  32663. WOLFSSL_SMALL_STACK_STATIC const byte t128_319[] =
  32664. {
  32665. 0x2c, 0x17, 0x84, 0x4c, 0x93, 0x1c, 0x07, 0x95,
  32666. 0x15, 0x92, 0x73, 0x0a, 0x34, 0xd0, 0xd9, 0xd2
  32667. };
  32668. WOLFSSL_SMALL_STACK_STATIC const byte t128_320[] =
  32669. {
  32670. 0xdf, 0xa6, 0x67, 0x47, 0xde, 0x9a, 0xe6, 0x30,
  32671. 0x30, 0xca, 0x32, 0x61, 0x14, 0x97, 0xc8, 0x27
  32672. };
  32673. WOLFSSL_SMALL_STACK_STATIC const byte t128_512[] =
  32674. {
  32675. 0x51, 0xf0, 0xbe, 0xbf, 0x7e, 0x3b, 0x9d, 0x92,
  32676. 0xfc, 0x49, 0x74, 0x17, 0x79, 0x36, 0x3c, 0xfe
  32677. };
  32678. #endif
  32679. #ifdef WOLFSSL_AES_192
  32680. WOLFSSL_SMALL_STACK_STATIC const byte t192_0[] =
  32681. {
  32682. 0xd1, 0x7d, 0xdf, 0x46, 0xad, 0xaa, 0xcd, 0xe5,
  32683. 0x31, 0xca, 0xc4, 0x83, 0xde, 0x7a, 0x93, 0x67
  32684. };
  32685. WOLFSSL_SMALL_STACK_STATIC const byte t192_128[] =
  32686. {
  32687. 0x9e, 0x99, 0xa7, 0xbf, 0x31, 0xe7, 0x10, 0x90,
  32688. 0x06, 0x62, 0xf6, 0x5e, 0x61, 0x7c, 0x51, 0x84
  32689. };
  32690. WOLFSSL_SMALL_STACK_STATIC const byte t192_320[] =
  32691. {
  32692. 0x8a, 0x1d, 0xe5, 0xbe, 0x2e, 0xb3, 0x1a, 0xad,
  32693. 0x08, 0x9a, 0x82, 0xe6, 0xee, 0x90, 0x8b, 0x0e
  32694. };
  32695. WOLFSSL_SMALL_STACK_STATIC const byte t192_512[] =
  32696. {
  32697. 0xa1, 0xd5, 0xdf, 0x0e, 0xed, 0x79, 0x0f, 0x79,
  32698. 0x4d, 0x77, 0x58, 0x96, 0x59, 0xf3, 0x9a, 0x11
  32699. };
  32700. #endif
  32701. #ifdef WOLFSSL_AES_256
  32702. WOLFSSL_SMALL_STACK_STATIC const byte t256_0[] =
  32703. {
  32704. 0x02, 0x89, 0x62, 0xf6, 0x1b, 0x7b, 0xf8, 0x9e,
  32705. 0xfc, 0x6b, 0x55, 0x1f, 0x46, 0x67, 0xd9, 0x83
  32706. };
  32707. WOLFSSL_SMALL_STACK_STATIC const byte t256_128[] =
  32708. {
  32709. 0x28, 0xa7, 0x02, 0x3f, 0x45, 0x2e, 0x8f, 0x82,
  32710. 0xbd, 0x4b, 0xf2, 0x8d, 0x8c, 0x37, 0xc3, 0x5c
  32711. };
  32712. WOLFSSL_SMALL_STACK_STATIC const byte t256_320[] =
  32713. {
  32714. 0xaa, 0xf3, 0xd8, 0xf1, 0xde, 0x56, 0x40, 0xc2,
  32715. 0x32, 0xf5, 0xb1, 0x69, 0xb9, 0xc9, 0x11, 0xe6
  32716. };
  32717. WOLFSSL_SMALL_STACK_STATIC const byte t256_512[] =
  32718. {
  32719. 0xe1, 0x99, 0x21, 0x90, 0x54, 0x9f, 0x6e, 0xd5,
  32720. 0x69, 0x6a, 0x2c, 0x05, 0x6c, 0x31, 0x54, 0x10
  32721. };
  32722. #endif
  32723. const CMAC_Test_Case testCases[] =
  32724. {
  32725. #ifdef WOLFSSL_AES_128
  32726. {WC_CMAC_AES, 0, m, MLEN_0, k128, KLEN_128, t128_0, AES_BLOCK_SIZE},
  32727. {WC_CMAC_AES, 0, m, MLEN_128, k128, KLEN_128, t128_128, AES_BLOCK_SIZE},
  32728. {WC_CMAC_AES, 0, m, MLEN_320, k128, KLEN_128, t128_320, AES_BLOCK_SIZE},
  32729. {WC_CMAC_AES, 0, m, MLEN_512, k128, KLEN_128, t128_512, AES_BLOCK_SIZE},
  32730. {WC_CMAC_AES, 5, m, MLEN_512, k128, KLEN_128, t128_512, AES_BLOCK_SIZE},
  32731. #endif
  32732. #ifdef WOLFSSL_AES_192
  32733. {WC_CMAC_AES, 0, m, MLEN_0, k192, KLEN_192, t192_0, AES_BLOCK_SIZE},
  32734. {WC_CMAC_AES, 0, m, MLEN_128, k192, KLEN_192, t192_128, AES_BLOCK_SIZE},
  32735. {WC_CMAC_AES, 0, m, MLEN_320, k192, KLEN_192, t192_320, AES_BLOCK_SIZE},
  32736. {WC_CMAC_AES, 0, m, MLEN_512, k192, KLEN_192, t192_512, AES_BLOCK_SIZE},
  32737. #endif
  32738. #ifdef WOLFSSL_AES_256
  32739. {WC_CMAC_AES, 0, m, MLEN_0, k256, KLEN_256, t256_0, AES_BLOCK_SIZE},
  32740. {WC_CMAC_AES, 0, m, MLEN_128, k256, KLEN_256, t256_128, AES_BLOCK_SIZE},
  32741. {WC_CMAC_AES, 0, m, MLEN_320, k256, KLEN_256, t256_320, AES_BLOCK_SIZE},
  32742. {WC_CMAC_AES, 0, m, MLEN_512, k256, KLEN_256, t256_512, AES_BLOCK_SIZE},
  32743. #endif
  32744. #ifdef WOLFSSL_AES_128
  32745. {WC_CMAC_AES, 0, m, MLEN_319, k128, KLEN_128, t128_319, AES_BLOCK_SIZE}
  32746. #endif
  32747. };
  32748. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  32749. Cmac *cmac;
  32750. #else
  32751. Cmac cmac[1];
  32752. #endif
  32753. byte tag[AES_BLOCK_SIZE];
  32754. const CMAC_Test_Case* tc;
  32755. word32 i, tagSz;
  32756. int ret;
  32757. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  32758. if ((cmac = (Cmac *)XMALLOC(sizeof *cmac, HEAP_HINT, DYNAMIC_TYPE_CMAC)) == NULL)
  32759. ERROR_OUT(-12009, out);
  32760. #endif
  32761. for (i = 0, tc = testCases;
  32762. i < sizeof(testCases)/sizeof(CMAC_Test_Case);
  32763. i++, tc++) {
  32764. XMEMSET(tag, 0, sizeof(tag));
  32765. tagSz = AES_BLOCK_SIZE;
  32766. #if !defined(HAVE_FIPS) || \
  32767. defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3)
  32768. if (wc_InitCmac_ex(cmac, tc->k, tc->kSz, tc->type, NULL, HEAP_HINT, devId) != 0)
  32769. #else
  32770. if (wc_InitCmac(cmac, tc->k, tc->kSz, tc->type, NULL) != 0)
  32771. #endif
  32772. {
  32773. ERROR_OUT(-12000, out);
  32774. }
  32775. if (tc->partial) {
  32776. if (wc_CmacUpdate(cmac, tc->m,
  32777. tc->mSz/2 - tc->partial) != 0)
  32778. ERROR_OUT(-12001, out);
  32779. if (wc_CmacUpdate(cmac, tc->m + tc->mSz/2 - tc->partial,
  32780. tc->mSz/2 + tc->partial) != 0)
  32781. ERROR_OUT(-12002, out);
  32782. }
  32783. else {
  32784. if (wc_CmacUpdate(cmac, tc->m, tc->mSz) != 0)
  32785. ERROR_OUT(-12003, out);
  32786. }
  32787. if (wc_CmacFinal(cmac, tag, &tagSz) != 0)
  32788. ERROR_OUT(-12004, out);
  32789. if (XMEMCMP(tag, tc->t, AES_BLOCK_SIZE) != 0)
  32790. ERROR_OUT(-12005, out);
  32791. XMEMSET(tag, 0, sizeof(tag));
  32792. tagSz = sizeof(tag);
  32793. if (wc_AesCmacGenerate(tag, &tagSz, tc->m, tc->mSz,
  32794. tc->k, tc->kSz) != 0)
  32795. ERROR_OUT(-12006, out);
  32796. if (XMEMCMP(tag, tc->t, AES_BLOCK_SIZE) != 0)
  32797. ERROR_OUT(-12007, out);
  32798. if (wc_AesCmacVerify(tc->t, tc->tSz, tc->m, tc->mSz,
  32799. tc->k, tc->kSz) != 0)
  32800. ERROR_OUT(-12008, out);
  32801. }
  32802. ret = 0;
  32803. out:
  32804. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  32805. if (cmac)
  32806. XFREE(cmac, HEAP_HINT, DYNAMIC_TYPE_CMAC);
  32807. #endif
  32808. return ret;
  32809. }
  32810. #endif /* NO_AES && WOLFSSL_CMAC */
  32811. #if defined(WOLFSSL_SIPHASH)
  32812. #if WOLFSSL_SIPHASH_CROUNDS == 2 && WOLFSSL_SIPHASH_DROUNDS == 4
  32813. /* Test vectors from:
  32814. * https://github.com/veorq/SipHash/blob/master/vectors.h
  32815. */
  32816. static const unsigned char siphash_key[SIPHASH_KEY_SIZE] = {
  32817. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  32818. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
  32819. };
  32820. static const unsigned char siphash_msg[64] = {
  32821. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  32822. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  32823. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  32824. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
  32825. 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  32826. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
  32827. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  32828. 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f
  32829. };
  32830. static const unsigned char siphash_r8[64][SIPHASH_MAC_SIZE_8] = {
  32831. { 0x31, 0x0e, 0x0e, 0xdd, 0x47, 0xdb, 0x6f, 0x72, },
  32832. { 0xfd, 0x67, 0xdc, 0x93, 0xc5, 0x39, 0xf8, 0x74, },
  32833. { 0x5a, 0x4f, 0xa9, 0xd9, 0x09, 0x80, 0x6c, 0x0d, },
  32834. { 0x2d, 0x7e, 0xfb, 0xd7, 0x96, 0x66, 0x67, 0x85, },
  32835. { 0xb7, 0x87, 0x71, 0x27, 0xe0, 0x94, 0x27, 0xcf, },
  32836. { 0x8d, 0xa6, 0x99, 0xcd, 0x64, 0x55, 0x76, 0x18, },
  32837. { 0xce, 0xe3, 0xfe, 0x58, 0x6e, 0x46, 0xc9, 0xcb, },
  32838. { 0x37, 0xd1, 0x01, 0x8b, 0xf5, 0x00, 0x02, 0xab, },
  32839. { 0x62, 0x24, 0x93, 0x9a, 0x79, 0xf5, 0xf5, 0x93, },
  32840. { 0xb0, 0xe4, 0xa9, 0x0b, 0xdf, 0x82, 0x00, 0x9e, },
  32841. { 0xf3, 0xb9, 0xdd, 0x94, 0xc5, 0xbb, 0x5d, 0x7a, },
  32842. { 0xa7, 0xad, 0x6b, 0x22, 0x46, 0x2f, 0xb3, 0xf4, },
  32843. { 0xfb, 0xe5, 0x0e, 0x86, 0xbc, 0x8f, 0x1e, 0x75, },
  32844. { 0x90, 0x3d, 0x84, 0xc0, 0x27, 0x56, 0xea, 0x14, },
  32845. { 0xee, 0xf2, 0x7a, 0x8e, 0x90, 0xca, 0x23, 0xf7, },
  32846. { 0xe5, 0x45, 0xbe, 0x49, 0x61, 0xca, 0x29, 0xa1, },
  32847. { 0xdb, 0x9b, 0xc2, 0x57, 0x7f, 0xcc, 0x2a, 0x3f, },
  32848. { 0x94, 0x47, 0xbe, 0x2c, 0xf5, 0xe9, 0x9a, 0x69, },
  32849. { 0x9c, 0xd3, 0x8d, 0x96, 0xf0, 0xb3, 0xc1, 0x4b, },
  32850. { 0xbd, 0x61, 0x79, 0xa7, 0x1d, 0xc9, 0x6d, 0xbb, },
  32851. { 0x98, 0xee, 0xa2, 0x1a, 0xf2, 0x5c, 0xd6, 0xbe, },
  32852. { 0xc7, 0x67, 0x3b, 0x2e, 0xb0, 0xcb, 0xf2, 0xd0, },
  32853. { 0x88, 0x3e, 0xa3, 0xe3, 0x95, 0x67, 0x53, 0x93, },
  32854. { 0xc8, 0xce, 0x5c, 0xcd, 0x8c, 0x03, 0x0c, 0xa8, },
  32855. { 0x94, 0xaf, 0x49, 0xf6, 0xc6, 0x50, 0xad, 0xb8, },
  32856. { 0xea, 0xb8, 0x85, 0x8a, 0xde, 0x92, 0xe1, 0xbc, },
  32857. { 0xf3, 0x15, 0xbb, 0x5b, 0xb8, 0x35, 0xd8, 0x17, },
  32858. { 0xad, 0xcf, 0x6b, 0x07, 0x63, 0x61, 0x2e, 0x2f, },
  32859. { 0xa5, 0xc9, 0x1d, 0xa7, 0xac, 0xaa, 0x4d, 0xde, },
  32860. { 0x71, 0x65, 0x95, 0x87, 0x66, 0x50, 0xa2, 0xa6, },
  32861. { 0x28, 0xef, 0x49, 0x5c, 0x53, 0xa3, 0x87, 0xad, },
  32862. { 0x42, 0xc3, 0x41, 0xd8, 0xfa, 0x92, 0xd8, 0x32, },
  32863. { 0xce, 0x7c, 0xf2, 0x72, 0x2f, 0x51, 0x27, 0x71, },
  32864. { 0xe3, 0x78, 0x59, 0xf9, 0x46, 0x23, 0xf3, 0xa7, },
  32865. { 0x38, 0x12, 0x05, 0xbb, 0x1a, 0xb0, 0xe0, 0x12, },
  32866. { 0xae, 0x97, 0xa1, 0x0f, 0xd4, 0x34, 0xe0, 0x15, },
  32867. { 0xb4, 0xa3, 0x15, 0x08, 0xbe, 0xff, 0x4d, 0x31, },
  32868. { 0x81, 0x39, 0x62, 0x29, 0xf0, 0x90, 0x79, 0x02, },
  32869. { 0x4d, 0x0c, 0xf4, 0x9e, 0xe5, 0xd4, 0xdc, 0xca, },
  32870. { 0x5c, 0x73, 0x33, 0x6a, 0x76, 0xd8, 0xbf, 0x9a, },
  32871. { 0xd0, 0xa7, 0x04, 0x53, 0x6b, 0xa9, 0x3e, 0x0e, },
  32872. { 0x92, 0x59, 0x58, 0xfc, 0xd6, 0x42, 0x0c, 0xad, },
  32873. { 0xa9, 0x15, 0xc2, 0x9b, 0xc8, 0x06, 0x73, 0x18, },
  32874. { 0x95, 0x2b, 0x79, 0xf3, 0xbc, 0x0a, 0xa6, 0xd4, },
  32875. { 0xf2, 0x1d, 0xf2, 0xe4, 0x1d, 0x45, 0x35, 0xf9, },
  32876. { 0x87, 0x57, 0x75, 0x19, 0x04, 0x8f, 0x53, 0xa9, },
  32877. { 0x10, 0xa5, 0x6c, 0xf5, 0xdf, 0xcd, 0x9a, 0xdb, },
  32878. { 0xeb, 0x75, 0x09, 0x5c, 0xcd, 0x98, 0x6c, 0xd0, },
  32879. { 0x51, 0xa9, 0xcb, 0x9e, 0xcb, 0xa3, 0x12, 0xe6, },
  32880. { 0x96, 0xaf, 0xad, 0xfc, 0x2c, 0xe6, 0x66, 0xc7, },
  32881. { 0x72, 0xfe, 0x52, 0x97, 0x5a, 0x43, 0x64, 0xee, },
  32882. { 0x5a, 0x16, 0x45, 0xb2, 0x76, 0xd5, 0x92, 0xa1, },
  32883. { 0xb2, 0x74, 0xcb, 0x8e, 0xbf, 0x87, 0x87, 0x0a, },
  32884. { 0x6f, 0x9b, 0xb4, 0x20, 0x3d, 0xe7, 0xb3, 0x81, },
  32885. { 0xea, 0xec, 0xb2, 0xa3, 0x0b, 0x22, 0xa8, 0x7f, },
  32886. { 0x99, 0x24, 0xa4, 0x3c, 0xc1, 0x31, 0x57, 0x24, },
  32887. { 0xbd, 0x83, 0x8d, 0x3a, 0xaf, 0xbf, 0x8d, 0xb7, },
  32888. { 0x0b, 0x1a, 0x2a, 0x32, 0x65, 0xd5, 0x1a, 0xea, },
  32889. { 0x13, 0x50, 0x79, 0xa3, 0x23, 0x1c, 0xe6, 0x60, },
  32890. { 0x93, 0x2b, 0x28, 0x46, 0xe4, 0xd7, 0x06, 0x66, },
  32891. { 0xe1, 0x91, 0x5f, 0x5c, 0xb1, 0xec, 0xa4, 0x6c, },
  32892. { 0xf3, 0x25, 0x96, 0x5c, 0xa1, 0x6d, 0x62, 0x9f, },
  32893. { 0x57, 0x5f, 0xf2, 0x8e, 0x60, 0x38, 0x1b, 0xe5, },
  32894. { 0x72, 0x45, 0x06, 0xeb, 0x4c, 0x32, 0x8a, 0x95, },
  32895. };
  32896. static const unsigned char siphash_r16[64][SIPHASH_MAC_SIZE_16] = {
  32897. { 0xa3, 0x81, 0x7f, 0x04, 0xba, 0x25, 0xa8, 0xe6,
  32898. 0x6d, 0xf6, 0x72, 0x14, 0xc7, 0x55, 0x02, 0x93, },
  32899. { 0xda, 0x87, 0xc1, 0xd8, 0x6b, 0x99, 0xaf, 0x44,
  32900. 0x34, 0x76, 0x59, 0x11, 0x9b, 0x22, 0xfc, 0x45, },
  32901. { 0x81, 0x77, 0x22, 0x8d, 0xa4, 0xa4, 0x5d, 0xc7,
  32902. 0xfc, 0xa3, 0x8b, 0xde, 0xf6, 0x0a, 0xff, 0xe4, },
  32903. { 0x9c, 0x70, 0xb6, 0x0c, 0x52, 0x67, 0xa9, 0x4e,
  32904. 0x5f, 0x33, 0xb6, 0xb0, 0x29, 0x85, 0xed, 0x51, },
  32905. { 0xf8, 0x81, 0x64, 0xc1, 0x2d, 0x9c, 0x8f, 0xaf,
  32906. 0x7d, 0x0f, 0x6e, 0x7c, 0x7b, 0xcd, 0x55, 0x79, },
  32907. { 0x13, 0x68, 0x87, 0x59, 0x80, 0x77, 0x6f, 0x88,
  32908. 0x54, 0x52, 0x7a, 0x07, 0x69, 0x0e, 0x96, 0x27, },
  32909. { 0x14, 0xee, 0xca, 0x33, 0x8b, 0x20, 0x86, 0x13,
  32910. 0x48, 0x5e, 0xa0, 0x30, 0x8f, 0xd7, 0xa1, 0x5e, },
  32911. { 0xa1, 0xf1, 0xeb, 0xbe, 0xd8, 0xdb, 0xc1, 0x53,
  32912. 0xc0, 0xb8, 0x4a, 0xa6, 0x1f, 0xf0, 0x82, 0x39, },
  32913. { 0x3b, 0x62, 0xa9, 0xba, 0x62, 0x58, 0xf5, 0x61,
  32914. 0x0f, 0x83, 0xe2, 0x64, 0xf3, 0x14, 0x97, 0xb4, },
  32915. { 0x26, 0x44, 0x99, 0x06, 0x0a, 0xd9, 0xba, 0xab,
  32916. 0xc4, 0x7f, 0x8b, 0x02, 0xbb, 0x6d, 0x71, 0xed, },
  32917. { 0x00, 0x11, 0x0d, 0xc3, 0x78, 0x14, 0x69, 0x56,
  32918. 0xc9, 0x54, 0x47, 0xd3, 0xf3, 0xd0, 0xfb, 0xba, },
  32919. { 0x01, 0x51, 0xc5, 0x68, 0x38, 0x6b, 0x66, 0x77,
  32920. 0xa2, 0xb4, 0xdc, 0x6f, 0x81, 0xe5, 0xdc, 0x18, },
  32921. { 0xd6, 0x26, 0xb2, 0x66, 0x90, 0x5e, 0xf3, 0x58,
  32922. 0x82, 0x63, 0x4d, 0xf6, 0x85, 0x32, 0xc1, 0x25, },
  32923. { 0x98, 0x69, 0xe2, 0x47, 0xe9, 0xc0, 0x8b, 0x10,
  32924. 0xd0, 0x29, 0x93, 0x4f, 0xc4, 0xb9, 0x52, 0xf7, },
  32925. { 0x31, 0xfc, 0xef, 0xac, 0x66, 0xd7, 0xde, 0x9c,
  32926. 0x7e, 0xc7, 0x48, 0x5f, 0xe4, 0x49, 0x49, 0x02, },
  32927. { 0x54, 0x93, 0xe9, 0x99, 0x33, 0xb0, 0xa8, 0x11,
  32928. 0x7e, 0x08, 0xec, 0x0f, 0x97, 0xcf, 0xc3, 0xd9, },
  32929. { 0x6e, 0xe2, 0xa4, 0xca, 0x67, 0xb0, 0x54, 0xbb,
  32930. 0xfd, 0x33, 0x15, 0xbf, 0x85, 0x23, 0x05, 0x77, },
  32931. { 0x47, 0x3d, 0x06, 0xe8, 0x73, 0x8d, 0xb8, 0x98,
  32932. 0x54, 0xc0, 0x66, 0xc4, 0x7a, 0xe4, 0x77, 0x40, },
  32933. { 0xa4, 0x26, 0xe5, 0xe4, 0x23, 0xbf, 0x48, 0x85,
  32934. 0x29, 0x4d, 0xa4, 0x81, 0xfe, 0xae, 0xf7, 0x23, },
  32935. { 0x78, 0x01, 0x77, 0x31, 0xcf, 0x65, 0xfa, 0xb0,
  32936. 0x74, 0xd5, 0x20, 0x89, 0x52, 0x51, 0x2e, 0xb1, },
  32937. { 0x9e, 0x25, 0xfc, 0x83, 0x3f, 0x22, 0x90, 0x73,
  32938. 0x3e, 0x93, 0x44, 0xa5, 0xe8, 0x38, 0x39, 0xeb, },
  32939. { 0x56, 0x8e, 0x49, 0x5a, 0xbe, 0x52, 0x5a, 0x21,
  32940. 0x8a, 0x22, 0x14, 0xcd, 0x3e, 0x07, 0x1d, 0x12, },
  32941. { 0x4a, 0x29, 0xb5, 0x45, 0x52, 0xd1, 0x6b, 0x9a,
  32942. 0x46, 0x9c, 0x10, 0x52, 0x8e, 0xff, 0x0a, 0xae, },
  32943. { 0xc9, 0xd1, 0x84, 0xdd, 0xd5, 0xa9, 0xf5, 0xe0,
  32944. 0xcf, 0x8c, 0xe2, 0x9a, 0x9a, 0xbf, 0x69, 0x1c, },
  32945. { 0x2d, 0xb4, 0x79, 0xae, 0x78, 0xbd, 0x50, 0xd8,
  32946. 0x88, 0x2a, 0x8a, 0x17, 0x8a, 0x61, 0x32, 0xad, },
  32947. { 0x8e, 0xce, 0x5f, 0x04, 0x2d, 0x5e, 0x44, 0x7b,
  32948. 0x50, 0x51, 0xb9, 0xea, 0xcb, 0x8d, 0x8f, 0x6f, },
  32949. { 0x9c, 0x0b, 0x53, 0xb4, 0xb3, 0xc3, 0x07, 0xe8,
  32950. 0x7e, 0xae, 0xe0, 0x86, 0x78, 0x14, 0x1f, 0x66, },
  32951. { 0xab, 0xf2, 0x48, 0xaf, 0x69, 0xa6, 0xea, 0xe4,
  32952. 0xbf, 0xd3, 0xeb, 0x2f, 0x12, 0x9e, 0xeb, 0x94, },
  32953. { 0x06, 0x64, 0xda, 0x16, 0x68, 0x57, 0x4b, 0x88,
  32954. 0xb9, 0x35, 0xf3, 0x02, 0x73, 0x58, 0xae, 0xf4, },
  32955. { 0xaa, 0x4b, 0x9d, 0xc4, 0xbf, 0x33, 0x7d, 0xe9,
  32956. 0x0c, 0xd4, 0xfd, 0x3c, 0x46, 0x7c, 0x6a, 0xb7, },
  32957. { 0xea, 0x5c, 0x7f, 0x47, 0x1f, 0xaf, 0x6b, 0xde,
  32958. 0x2b, 0x1a, 0xd7, 0xd4, 0x68, 0x6d, 0x22, 0x87, },
  32959. { 0x29, 0x39, 0xb0, 0x18, 0x32, 0x23, 0xfa, 0xfc,
  32960. 0x17, 0x23, 0xde, 0x4f, 0x52, 0xc4, 0x3d, 0x35, },
  32961. { 0x7c, 0x39, 0x56, 0xca, 0x5e, 0xea, 0xfc, 0x3e,
  32962. 0x36, 0x3e, 0x9d, 0x55, 0x65, 0x46, 0xeb, 0x68, },
  32963. { 0x77, 0xc6, 0x07, 0x71, 0x46, 0xf0, 0x1c, 0x32,
  32964. 0xb6, 0xb6, 0x9d, 0x5f, 0x4e, 0xa9, 0xff, 0xcf, },
  32965. { 0x37, 0xa6, 0x98, 0x6c, 0xb8, 0x84, 0x7e, 0xdf,
  32966. 0x09, 0x25, 0xf0, 0xf1, 0x30, 0x9b, 0x54, 0xde, },
  32967. { 0xa7, 0x05, 0xf0, 0xe6, 0x9d, 0xa9, 0xa8, 0xf9,
  32968. 0x07, 0x24, 0x1a, 0x2e, 0x92, 0x3c, 0x8c, 0xc8, },
  32969. { 0x3d, 0xc4, 0x7d, 0x1f, 0x29, 0xc4, 0x48, 0x46,
  32970. 0x1e, 0x9e, 0x76, 0xed, 0x90, 0x4f, 0x67, 0x11, },
  32971. { 0x0d, 0x62, 0xbf, 0x01, 0xe6, 0xfc, 0x0e, 0x1a,
  32972. 0x0d, 0x3c, 0x47, 0x51, 0xc5, 0xd3, 0x69, 0x2b, },
  32973. { 0x8c, 0x03, 0x46, 0x8b, 0xca, 0x7c, 0x66, 0x9e,
  32974. 0xe4, 0xfd, 0x5e, 0x08, 0x4b, 0xbe, 0xe7, 0xb5, },
  32975. { 0x52, 0x8a, 0x5b, 0xb9, 0x3b, 0xaf, 0x2c, 0x9c,
  32976. 0x44, 0x73, 0xcc, 0xe5, 0xd0, 0xd2, 0x2b, 0xd9, },
  32977. { 0xdf, 0x6a, 0x30, 0x1e, 0x95, 0xc9, 0x5d, 0xad,
  32978. 0x97, 0xae, 0x0c, 0xc8, 0xc6, 0x91, 0x3b, 0xd8, },
  32979. { 0x80, 0x11, 0x89, 0x90, 0x2c, 0x85, 0x7f, 0x39,
  32980. 0xe7, 0x35, 0x91, 0x28, 0x5e, 0x70, 0xb6, 0xdb, },
  32981. { 0xe6, 0x17, 0x34, 0x6a, 0xc9, 0xc2, 0x31, 0xbb,
  32982. 0x36, 0x50, 0xae, 0x34, 0xcc, 0xca, 0x0c, 0x5b, },
  32983. { 0x27, 0xd9, 0x34, 0x37, 0xef, 0xb7, 0x21, 0xaa,
  32984. 0x40, 0x18, 0x21, 0xdc, 0xec, 0x5a, 0xdf, 0x89, },
  32985. { 0x89, 0x23, 0x7d, 0x9d, 0xed, 0x9c, 0x5e, 0x78,
  32986. 0xd8, 0xb1, 0xc9, 0xb1, 0x66, 0xcc, 0x73, 0x42, },
  32987. { 0x4a, 0x6d, 0x80, 0x91, 0xbf, 0x5e, 0x7d, 0x65,
  32988. 0x11, 0x89, 0xfa, 0x94, 0xa2, 0x50, 0xb1, 0x4c, },
  32989. { 0x0e, 0x33, 0xf9, 0x60, 0x55, 0xe7, 0xae, 0x89,
  32990. 0x3f, 0xfc, 0x0e, 0x3d, 0xcf, 0x49, 0x29, 0x02, },
  32991. { 0xe6, 0x1c, 0x43, 0x2b, 0x72, 0x0b, 0x19, 0xd1,
  32992. 0x8e, 0xc8, 0xd8, 0x4b, 0xdc, 0x63, 0x15, 0x1b, },
  32993. { 0xf7, 0xe5, 0xae, 0xf5, 0x49, 0xf7, 0x82, 0xcf,
  32994. 0x37, 0x90, 0x55, 0xa6, 0x08, 0x26, 0x9b, 0x16, },
  32995. { 0x43, 0x8d, 0x03, 0x0f, 0xd0, 0xb7, 0xa5, 0x4f,
  32996. 0xa8, 0x37, 0xf2, 0xad, 0x20, 0x1a, 0x64, 0x03, },
  32997. { 0xa5, 0x90, 0xd3, 0xee, 0x4f, 0xbf, 0x04, 0xe3,
  32998. 0x24, 0x7e, 0x0d, 0x27, 0xf2, 0x86, 0x42, 0x3f, },
  32999. { 0x5f, 0xe2, 0xc1, 0xa1, 0x72, 0xfe, 0x93, 0xc4,
  33000. 0xb1, 0x5c, 0xd3, 0x7c, 0xae, 0xf9, 0xf5, 0x38, },
  33001. { 0x2c, 0x97, 0x32, 0x5c, 0xbd, 0x06, 0xb3, 0x6e,
  33002. 0xb2, 0x13, 0x3d, 0xd0, 0x8b, 0x3a, 0x01, 0x7c, },
  33003. { 0x92, 0xc8, 0x14, 0x22, 0x7a, 0x6b, 0xca, 0x94,
  33004. 0x9f, 0xf0, 0x65, 0x9f, 0x00, 0x2a, 0xd3, 0x9e, },
  33005. { 0xdc, 0xe8, 0x50, 0x11, 0x0b, 0xd8, 0x32, 0x8c,
  33006. 0xfb, 0xd5, 0x08, 0x41, 0xd6, 0x91, 0x1d, 0x87, },
  33007. { 0x67, 0xf1, 0x49, 0x84, 0xc7, 0xda, 0x79, 0x12,
  33008. 0x48, 0xe3, 0x2b, 0xb5, 0x92, 0x25, 0x83, 0xda, },
  33009. { 0x19, 0x38, 0xf2, 0xcf, 0x72, 0xd5, 0x4e, 0xe9,
  33010. 0x7e, 0x94, 0x16, 0x6f, 0xa9, 0x1d, 0x2a, 0x36, },
  33011. { 0x74, 0x48, 0x1e, 0x96, 0x46, 0xed, 0x49, 0xfe,
  33012. 0x0f, 0x62, 0x24, 0x30, 0x16, 0x04, 0x69, 0x8e, },
  33013. { 0x57, 0xfc, 0xa5, 0xde, 0x98, 0xa9, 0xd6, 0xd8,
  33014. 0x00, 0x64, 0x38, 0xd0, 0x58, 0x3d, 0x8a, 0x1d, },
  33015. { 0x9f, 0xec, 0xde, 0x1c, 0xef, 0xdc, 0x1c, 0xbe,
  33016. 0xd4, 0x76, 0x36, 0x74, 0xd9, 0x57, 0x53, 0x59, },
  33017. { 0xe3, 0x04, 0x0c, 0x00, 0xeb, 0x28, 0xf1, 0x53,
  33018. 0x66, 0xca, 0x73, 0xcb, 0xd8, 0x72, 0xe7, 0x40, },
  33019. { 0x76, 0x97, 0x00, 0x9a, 0x6a, 0x83, 0x1d, 0xfe,
  33020. 0xcc, 0xa9, 0x1c, 0x59, 0x93, 0x67, 0x0f, 0x7a, },
  33021. { 0x58, 0x53, 0x54, 0x23, 0x21, 0xf5, 0x67, 0xa0,
  33022. 0x05, 0xd5, 0x47, 0xa4, 0xf0, 0x47, 0x59, 0xbd, },
  33023. { 0x51, 0x50, 0xd1, 0x77, 0x2f, 0x50, 0x83, 0x4a,
  33024. 0x50, 0x3e, 0x06, 0x9a, 0x97, 0x3f, 0xbd, 0x7c, },
  33025. };
  33026. #endif
  33027. WOLFSSL_TEST_SUBROUTINE int siphash_test(void)
  33028. {
  33029. int ret = 0;
  33030. int i;
  33031. #if WOLFSSL_SIPHASH_CROUNDS == 2 && WOLFSSL_SIPHASH_DROUNDS == 4
  33032. unsigned char res[SIPHASH_MAC_SIZE_16];
  33033. SipHash siphash;
  33034. for (i = 0; i < 64; i++) {
  33035. ret = wc_InitSipHash(&siphash, siphash_key, SIPHASH_MAC_SIZE_8);
  33036. if (ret != 0)
  33037. return -12100 - i;
  33038. ret = wc_SipHashUpdate(&siphash, siphash_msg, i);
  33039. if (ret != 0)
  33040. return -12200 - i;
  33041. ret = wc_SipHashFinal(&siphash, res, SIPHASH_MAC_SIZE_8);
  33042. if (ret != 0)
  33043. return -12300 - i;
  33044. if (XMEMCMP(res, siphash_r8[i], SIPHASH_MAC_SIZE_8) != 0)
  33045. return -12400 - i;
  33046. ret = wc_SipHash(siphash_key, siphash_msg, i, res, SIPHASH_MAC_SIZE_8);
  33047. if (ret != 0)
  33048. return -12500 - i;
  33049. if (XMEMCMP(res, siphash_r8[i], SIPHASH_MAC_SIZE_8) != 0)
  33050. return -12600 - i;
  33051. }
  33052. for (i = 0; i < 64; i++) {
  33053. ret = wc_InitSipHash(&siphash, siphash_key, SIPHASH_MAC_SIZE_16);
  33054. if (ret != 0)
  33055. return -12700 - i;
  33056. ret = wc_SipHashUpdate(&siphash, siphash_msg, i);
  33057. if (ret != 0)
  33058. return -12800 - i;
  33059. ret = wc_SipHashFinal(&siphash, res, SIPHASH_MAC_SIZE_16);
  33060. if (ret != 0)
  33061. return -12900 - i;
  33062. if (XMEMCMP(res, siphash_r16[i], SIPHASH_MAC_SIZE_16) != 0)
  33063. return -13000 - i;
  33064. ret = wc_SipHash(siphash_key, siphash_msg, i, res, SIPHASH_MAC_SIZE_16);
  33065. if (ret != 0)
  33066. return -13100 - i;
  33067. if (XMEMCMP(res, siphash_r16[i], SIPHASH_MAC_SIZE_16) != 0)
  33068. return -13200 - i;
  33069. }
  33070. #endif
  33071. /* Testing bad parameters. */
  33072. ret = wc_InitSipHash(NULL, NULL, SIPHASH_MAC_SIZE_8);
  33073. if (ret != BAD_FUNC_ARG)
  33074. return -13300;
  33075. ret = wc_InitSipHash(NULL, siphash_key, SIPHASH_MAC_SIZE_8);
  33076. if (ret != BAD_FUNC_ARG)
  33077. return -13301;
  33078. ret = wc_InitSipHash(&siphash, NULL, SIPHASH_MAC_SIZE_8);
  33079. if (ret != BAD_FUNC_ARG)
  33080. return -13302;
  33081. ret = wc_InitSipHash(&siphash, siphash_key, 7);
  33082. if (ret != BAD_FUNC_ARG)
  33083. return -13303;
  33084. ret = wc_InitSipHash(&siphash, siphash_key, SIPHASH_MAC_SIZE_8);
  33085. if (ret != 0)
  33086. return -13304;
  33087. ret = wc_SipHashUpdate(NULL, NULL, 0);
  33088. if (ret != BAD_FUNC_ARG)
  33089. return -13305;
  33090. ret = wc_SipHashUpdate(&siphash, NULL, 1);
  33091. if (ret != BAD_FUNC_ARG)
  33092. return -13306;
  33093. ret = wc_SipHashFinal(NULL, NULL, SIPHASH_MAC_SIZE_8);
  33094. if (ret != BAD_FUNC_ARG)
  33095. return -13307;
  33096. ret = wc_SipHashFinal(&siphash, NULL, SIPHASH_MAC_SIZE_8);
  33097. if (ret != BAD_FUNC_ARG)
  33098. return -13308;
  33099. ret = wc_SipHashFinal(NULL, res, SIPHASH_MAC_SIZE_8);
  33100. if (ret != BAD_FUNC_ARG)
  33101. return -13309;
  33102. ret = wc_SipHashFinal(&siphash, res, SIPHASH_MAC_SIZE_16);
  33103. if (ret != BAD_FUNC_ARG)
  33104. return -13310;
  33105. ret = wc_SipHash(NULL, NULL, 0, NULL, SIPHASH_MAC_SIZE_16);
  33106. if (ret != BAD_FUNC_ARG)
  33107. return -13311;
  33108. ret = wc_SipHash(siphash_key, NULL, 0, NULL, SIPHASH_MAC_SIZE_16);
  33109. if (ret != BAD_FUNC_ARG)
  33110. return -13312;
  33111. ret = wc_SipHash(NULL, NULL, 0, res, SIPHASH_MAC_SIZE_16);
  33112. if (ret != BAD_FUNC_ARG)
  33113. return -13313;
  33114. ret = wc_SipHash(siphash_key, NULL, 0, res, 15);
  33115. if (ret != BAD_FUNC_ARG)
  33116. return -13314;
  33117. ret = wc_SipHash(siphash_key, NULL, 1, res, SIPHASH_MAC_SIZE_16);
  33118. if (ret != BAD_FUNC_ARG)
  33119. return -13315;
  33120. return 0;
  33121. }
  33122. #endif /* WOLFSSL_SIPHASH */
  33123. #ifdef HAVE_LIBZ
  33124. static const byte sample_text[] =
  33125. "Biodiesel cupidatat marfa, cliche aute put a bird on it incididunt elit\n"
  33126. "polaroid. Sunt tattooed bespoke reprehenderit. Sint twee organic id\n"
  33127. "marfa. Commodo veniam ad esse gastropub. 3 wolf moon sartorial vero,\n"
  33128. "plaid delectus biodiesel squid +1 vice. Post-ironic keffiyeh leggings\n"
  33129. "selfies cray fap hoodie, forage anim. Carles cupidatat shoreditch, VHS\n"
  33130. "small batch meggings kogi dolore food truck bespoke gastropub.\n"
  33131. "\n"
  33132. "Terry richardson adipisicing actually typewriter tumblr, twee whatever\n"
  33133. "four loko you probably haven't heard of them high life. Messenger bag\n"
  33134. "whatever tattooed deep v mlkshk. Brooklyn pinterest assumenda chillwave\n"
  33135. "et, banksy ullamco messenger bag umami pariatur direct trade forage.\n"
  33136. "Typewriter culpa try-hard, pariatur sint brooklyn meggings. Gentrify\n"
  33137. "food truck next level, tousled irony non semiotics PBR ethical anim cred\n"
  33138. "readymade. Mumblecore brunch lomo odd future, portland organic terry\n"
  33139. "richardson elit leggings adipisicing ennui raw denim banjo hella. Godard\n"
  33140. "mixtape polaroid, pork belly readymade organic cray typewriter helvetica\n"
  33141. "four loko whatever street art yr farm-to-table.\n"
  33142. "\n"
  33143. "Vinyl keytar vice tofu. Locavore you probably haven't heard of them pug\n"
  33144. "pickled, hella tonx labore truffaut DIY mlkshk elit cosby sweater sint\n"
  33145. "et mumblecore. Elit swag semiotics, reprehenderit DIY sartorial nisi ugh\n"
  33146. "nesciunt pug pork belly wayfarers selfies delectus. Ethical hoodie\n"
  33147. "seitan fingerstache kale chips. Terry richardson artisan williamsburg,\n"
  33148. "eiusmod fanny pack irony tonx ennui lo-fi incididunt tofu YOLO\n"
  33149. "readymade. 8-bit sed ethnic beard officia. Pour-over iphone DIY butcher,\n"
  33150. "ethnic art party qui letterpress nisi proident jean shorts mlkshk\n"
  33151. "locavore.\n"
  33152. "\n"
  33153. "Narwhal flexitarian letterpress, do gluten-free voluptate next level\n"
  33154. "banh mi tonx incididunt carles DIY. Odd future nulla 8-bit beard ut\n"
  33155. "cillum pickled velit, YOLO officia you probably haven't heard of them\n"
  33156. "trust fund gastropub. Nisi adipisicing tattooed, Austin mlkshk 90's\n"
  33157. "small batch american apparel. Put a bird on it cosby sweater before they\n"
  33158. "sold out pork belly kogi hella. Street art mollit sustainable polaroid,\n"
  33159. "DIY ethnic ea pug beard dreamcatcher cosby sweater magna scenester nisi.\n"
  33160. "Sed pork belly skateboard mollit, labore proident eiusmod. Sriracha\n"
  33161. "excepteur cosby sweater, anim deserunt laborum eu aliquip ethical et\n"
  33162. "neutra PBR selvage.\n"
  33163. "\n"
  33164. "Raw denim pork belly truffaut, irony plaid sustainable put a bird on it\n"
  33165. "next level jean shorts exercitation. Hashtag keytar whatever, nihil\n"
  33166. "authentic aliquip disrupt laborum. Tattooed selfies deserunt trust fund\n"
  33167. "wayfarers. 3 wolf moon synth church-key sartorial, gastropub leggings\n"
  33168. "tattooed. Labore high life commodo, meggings raw denim fingerstache pug\n"
  33169. "trust fund leggings seitan forage. Nostrud ullamco duis, reprehenderit\n"
  33170. "incididunt flannel sustainable helvetica pork belly pug banksy you\n"
  33171. "probably haven't heard of them nesciunt farm-to-table. Disrupt nostrud\n"
  33172. "mollit magna, sriracha sartorial helvetica.\n"
  33173. "\n"
  33174. "Nulla kogi reprehenderit, skateboard sustainable duis adipisicing viral\n"
  33175. "ad fanny pack salvia. Fanny pack trust fund you probably haven't heard\n"
  33176. "of them YOLO vice nihil. Keffiyeh cray lo-fi pinterest cardigan aliqua,\n"
  33177. "reprehenderit aute. Culpa tousled williamsburg, marfa lomo actually anim\n"
  33178. "skateboard. Iphone aliqua ugh, semiotics pariatur vero readymade\n"
  33179. "organic. Marfa squid nulla, in laborum disrupt laboris irure gastropub.\n"
  33180. "Veniam sunt food truck leggings, sint vinyl fap.\n"
  33181. "\n"
  33182. "Hella dolore pork belly, truffaut carles you probably haven't heard of\n"
  33183. "them PBR helvetica in sapiente. Fashion axe ugh bushwick american\n"
  33184. "apparel. Fingerstache sed iphone, jean shorts blue bottle nisi bushwick\n"
  33185. "flexitarian officia veniam plaid bespoke fap YOLO lo-fi. Blog\n"
  33186. "letterpress mumblecore, food truck id cray brooklyn cillum ad sed.\n"
  33187. "Assumenda chambray wayfarers vinyl mixtape sustainable. VHS vinyl\n"
  33188. "delectus, culpa williamsburg polaroid cliche swag church-key synth kogi\n"
  33189. "magna pop-up literally. Swag thundercats ennui shoreditch vegan\n"
  33190. "pitchfork neutra truffaut etsy, sed single-origin coffee craft beer.\n"
  33191. "\n"
  33192. "Odio letterpress brooklyn elit. Nulla single-origin coffee in occaecat\n"
  33193. "meggings. Irony meggings 8-bit, chillwave lo-fi adipisicing cred\n"
  33194. "dreamcatcher veniam. Put a bird on it irony umami, trust fund bushwick\n"
  33195. "locavore kale chips. Sriracha swag thundercats, chillwave disrupt\n"
  33196. "tousled beard mollit mustache leggings portland next level. Nihil esse\n"
  33197. "est, skateboard art party etsy thundercats sed dreamcatcher ut iphone\n"
  33198. "swag consectetur et. Irure skateboard banjo, nulla deserunt messenger\n"
  33199. "bag dolor terry richardson sapiente.\n";
  33200. static const byte sample_text_gz[] = {
  33201. 0x1F, 0x8B, 0x08, 0x08, 0xC5, 0x49, 0xB5, 0x5B, 0x00, 0x03, 0x63, 0x69, 0x70,
  33202. 0x68, 0x65, 0x72, 0x74, 0x65, 0x78, 0x74, 0x2E, 0x74, 0x78, 0x74, 0x00, 0x8D,
  33203. 0x58, 0xCB, 0x92, 0xE4, 0xB6, 0x11, 0xBC, 0xE3, 0x2B, 0xEA, 0xA6, 0x83, 0xD9,
  33204. 0x1D, 0x72, 0xF8, 0x22, 0x1F, 0xB5, 0x96, 0xA5, 0xDD, 0x90, 0xBC, 0xAB, 0xD0,
  33205. 0x28, 0x36, 0x42, 0x47, 0x90, 0x2C, 0x36, 0xA1, 0x06, 0x09, 0x0A, 0x8F, 0xEE,
  33206. 0xE1, 0xDF, 0x3B, 0x0B, 0xE0, 0x73, 0x2C, 0x4B, 0xBA, 0xCD, 0xCE, 0x80, 0x78,
  33207. 0x64, 0x65, 0x65, 0x66, 0xED, 0x3B, 0xE3, 0x5A, 0xC3, 0x81, 0x2D, 0x35, 0x69,
  33208. 0x32, 0xAD, 0x8E, 0x3A, 0xD2, 0xA0, 0x7D, 0xA7, 0x2B, 0x6A, 0xAC, 0x69, 0x7A,
  33209. 0x26, 0x9D, 0x22, 0xD3, 0x94, 0x22, 0x69, 0xAA, 0x8D, 0x6F, 0xC9, 0x8D, 0x64,
  33210. 0x22, 0x99, 0xB1, 0x31, 0xAD, 0x69, 0xD3, 0x18, 0x89, 0xAD, 0x89, 0x6A, 0x72,
  33211. 0x56, 0x7B, 0x67, 0xDA, 0x2B, 0xBD, 0xC8, 0xEF, 0xB0, 0x4D, 0x74, 0x8E, 0x5B,
  33212. 0xAA, 0x39, 0x4C, 0xEE, 0xCE, 0xE4, 0x79, 0xF2, 0xDC, 0xF3, 0xD8, 0xB2, 0x37,
  33213. 0x11, 0x8B, 0x8C, 0x2C, 0x7A, 0x32, 0x93, 0xF3, 0x37, 0x3D, 0x9A, 0x86, 0x4C,
  33214. 0xAB, 0xF2, 0xB9, 0x57, 0xFA, 0x97, 0x1B, 0x06, 0xD7, 0x3A, 0x7A, 0xF0, 0x68,
  33215. 0xF4, 0x40, 0xBA, 0x25, 0x0E, 0x81, 0xE9, 0xA6, 0x43, 0xF4, 0x6E, 0x4A, 0xF5,
  33216. 0x95, 0xFE, 0x41, 0x4F, 0x67, 0x3B, 0x1A, 0x1C, 0xEE, 0x12, 0xB4, 0x8F, 0xCE,
  33217. 0x1B, 0x6D, 0xB1, 0xDE, 0xBB, 0x4A, 0x4D, 0x56, 0x9B, 0x96, 0x5A, 0xB6, 0xDC,
  33218. 0xC4, 0x14, 0x70, 0xE5, 0xF5, 0x7D, 0xE1, 0xB7, 0x84, 0x3F, 0xFC, 0xED, 0xEF,
  33219. 0xF4, 0x30, 0x0D, 0x5F, 0xE9, 0x47, 0x17, 0xE2, 0xC5, 0x78, 0x27, 0x67, 0xDF,
  33220. 0xB9, 0xEB, 0xCC, 0xCC, 0x3D, 0x59, 0xBE, 0xDD, 0xCC, 0x78, 0x0B, 0x0A, 0x1F,
  33221. 0x74, 0xF8, 0x8C, 0x1A, 0xAF, 0x67, 0xEA, 0xF4, 0x44, 0xBD, 0x93, 0x7D, 0x2A,
  33222. 0xEA, 0x9C, 0xD7, 0x37, 0x80, 0x32, 0x9A, 0x01, 0x37, 0xD5, 0xDE, 0xCA, 0xA2,
  33223. 0x0D, 0xB9, 0xD0, 0x3B, 0xCF, 0xAD, 0x89, 0x4D, 0x5F, 0xD1, 0xE7, 0xF7, 0x2F,
  33224. 0x2A, 0x0C, 0xDA, 0x5A, 0xAA, 0x35, 0x7E, 0x41, 0xC3, 0xB2, 0x37, 0xDD, 0xDD,
  33225. 0xCD, 0x50, 0xEB, 0x2C, 0x96, 0x62, 0x3B, 0xD7, 0x52, 0xF4, 0xA9, 0xB9, 0x6F,
  33226. 0x48, 0xED, 0xEF, 0x54, 0xEA, 0x67, 0xF6, 0x7E, 0x26, 0x8F, 0x3A, 0x68, 0xDF,
  33227. 0x06, 0xBC, 0x56, 0xB7, 0x66, 0x32, 0xC1, 0x34, 0xD8, 0x88, 0x34, 0x1E, 0x88,
  33228. 0xED, 0x67, 0x8A, 0xF3, 0xC4, 0x4F, 0xC0, 0xCA, 0x9E, 0x62, 0x1A, 0x6A, 0xEB,
  33229. 0xAB, 0x02, 0xED, 0xB3, 0xD7, 0x91, 0x81, 0x8A, 0xEA, 0x5C, 0xF2, 0x64, 0xDD,
  33230. 0xDD, 0xD1, 0xEC, 0x12, 0x4D, 0xDE, 0xD5, 0xBA, 0xC6, 0x77, 0xBD, 0x06, 0xC4,
  33231. 0x5F, 0x44, 0xEA, 0x59, 0x4B, 0x5D, 0x3B, 0x8A, 0x3D, 0x0F, 0xD4, 0x9B, 0x1B,
  33232. 0x80, 0x30, 0x1D, 0x30, 0xFA, 0x8F, 0x00, 0x3F, 0xDE, 0xB0, 0x6F, 0xAD, 0x6F,
  33233. 0x6A, 0xDD, 0x6E, 0x2F, 0x6E, 0xCB, 0x3C, 0xD1, 0x83, 0x06, 0x7B, 0x0F, 0xFD,
  33234. 0xFD, 0x4A, 0xEF, 0xBC, 0x73, 0x77, 0x3B, 0x8F, 0x34, 0xA1, 0xBA, 0xEC, 0x39,
  33235. 0x80, 0x33, 0x21, 0xA4, 0x01, 0x55, 0xD7, 0xD4, 0xF4, 0xC6, 0xDA, 0x27, 0x4E,
  33236. 0x54, 0x1C, 0x2B, 0xEC, 0x37, 0xDE, 0xC3, 0x4C, 0xC9, 0x5A, 0x3D, 0x34, 0x0E,
  33237. 0xD8, 0x1C, 0x0E, 0xA2, 0x34, 0xE8, 0xC1, 0xD0, 0xA4, 0x51, 0xD5, 0x88, 0x8B,
  33238. 0xB7, 0xC6, 0xA3, 0x96, 0x40, 0x49, 0xB7, 0xBC, 0xE0, 0x7F, 0x55, 0x3F, 0xEF,
  33239. 0x6F, 0x6E, 0x92, 0x9D, 0x34, 0xFE, 0x3C, 0x5F, 0x04, 0xA5, 0x6A, 0xFF, 0x30,
  33240. 0x08, 0xC9, 0xEA, 0xF5, 0x52, 0x2B, 0xFE, 0x57, 0xFA, 0x8E, 0xC7, 0xE8, 0x4D,
  33241. 0x37, 0xAB, 0x03, 0xFA, 0x23, 0xBF, 0x46, 0x94, 0xFF, 0xC1, 0x16, 0xE0, 0xB9,
  33242. 0x14, 0x2C, 0x9E, 0x27, 0xEC, 0x98, 0x69, 0x14, 0x92, 0xF1, 0x60, 0x5C, 0x34,
  33243. 0x4D, 0xA0, 0x1F, 0xDF, 0xFD, 0x44, 0x1C, 0x7B, 0xD3, 0x80, 0x70, 0x42, 0x02,
  33244. 0x30, 0x84, 0x5B, 0xE5, 0x59, 0xB7, 0xF3, 0x80, 0xFB, 0x01, 0x33, 0xA9, 0x00,
  33245. 0x37, 0x52, 0xDC, 0xDA, 0xA7, 0x11, 0x85, 0xB7, 0x6E, 0x70, 0xE4, 0xDA, 0x96,
  33246. 0xBA, 0x84, 0x5B, 0x81, 0x43, 0x93, 0xF3, 0xD1, 0xEA, 0xB1, 0xDD, 0xB8, 0x1F,
  33247. 0xA5, 0xCC, 0xEA, 0x50, 0x66, 0x69, 0xA9, 0x8D, 0x8C, 0xA7, 0xA2, 0xF3, 0x38,
  33248. 0x26, 0x43, 0x5E, 0x3F, 0x01, 0xBE, 0x1C, 0x0F, 0x20, 0x7F, 0x75, 0xA8, 0x20,
  33249. 0x80, 0xC4, 0xC3, 0x5C, 0x8B, 0x0D, 0xD4, 0x60, 0x5E, 0xA3, 0x9E, 0xD0, 0xB4,
  33250. 0x4B, 0x4F, 0xE6, 0x13, 0x85, 0x60, 0x42, 0x96, 0xED, 0xAA, 0xDB, 0xE9, 0x99,
  33251. 0xE3, 0x07, 0x0E, 0x61, 0xB3, 0x07, 0xE3, 0xB1, 0xFA, 0xC0, 0x9B, 0xAD, 0xF6,
  33252. 0xE0, 0x26, 0x33, 0xEA, 0xEA, 0x23, 0xCD, 0x1E, 0x9D, 0xE1, 0x87, 0x4B, 0x74,
  33253. 0x97, 0x08, 0x3E, 0xA1, 0x28, 0xEA, 0xB3, 0x19, 0x67, 0x8B, 0x76, 0x9A, 0xA3,
  33254. 0xF6, 0xB9, 0xCF, 0x80, 0x65, 0x97, 0xAE, 0xF4, 0x83, 0x6B, 0xF4, 0x43, 0x20,
  33255. 0xF9, 0x0B, 0xFC, 0x9B, 0xD2, 0x4D, 0x4D, 0xA6, 0xB9, 0xA3, 0x02, 0x55, 0x79,
  33256. 0x18, 0x36, 0x19, 0x5F, 0xC9, 0xEA, 0x5A, 0x76, 0x40, 0xB9, 0xBA, 0x0E, 0x9A,
  33257. 0x44, 0xDF, 0x7C, 0xF8, 0x65, 0x61, 0x5E, 0x81, 0xAB, 0x71, 0xA1, 0x9E, 0x29,
  33258. 0x3C, 0x59, 0xCB, 0x23, 0xA4, 0xF6, 0x60, 0x1A, 0x0D, 0x5B, 0x39, 0xAE, 0xF4,
  33259. 0x6F, 0x59, 0x16, 0x9E, 0x60, 0xD8, 0x56, 0xCF, 0xEA, 0x2C, 0x4C, 0x79, 0xD3,
  33260. 0x5D, 0x51, 0x46, 0xA0, 0x4E, 0xE9, 0xD6, 0xAB, 0x91, 0x43, 0x63, 0x44, 0xD7,
  33261. 0x70, 0xB9, 0x23, 0x98, 0x4F, 0x3D, 0x03, 0x02, 0xF6, 0x81, 0x56, 0xC1, 0x58,
  33262. 0x85, 0x07, 0xA7, 0x2D, 0x2C, 0x29, 0xCA, 0x01, 0x45, 0x31, 0x51, 0x8F, 0xD4,
  33263. 0x19, 0xA1, 0x79, 0x88, 0x5A, 0xA4, 0xF5, 0xAE, 0x2D, 0x4B, 0x63, 0x4C, 0x58,
  33264. 0xFE, 0xBF, 0xAD, 0xEE, 0xA3, 0x09, 0xF8, 0xE2, 0x89, 0xBE, 0x81, 0x0E, 0x86,
  33265. 0x3A, 0xF9, 0x5B, 0xA5, 0xD8, 0xA4, 0x00, 0x75, 0x04, 0xF2, 0x23, 0xB8, 0x39,
  33266. 0x69, 0x50, 0xB7, 0xD0, 0x34, 0x63, 0x54, 0xD8, 0x61, 0xDD, 0xA5, 0x33, 0x47,
  33267. 0x85, 0x96, 0x22, 0xD0, 0x2F, 0x9F, 0x7E, 0xF8, 0x74, 0x24, 0xEA, 0x57, 0x97,
  33268. 0x5A, 0xE0, 0x00, 0xCF, 0xC1, 0x67, 0xE1, 0x41, 0xBD, 0x94, 0xA1, 0x03, 0xD3,
  33269. 0xB4, 0x08, 0x64, 0xF2, 0x17, 0x27, 0x35, 0x37, 0x53, 0xEF, 0x46, 0xCE, 0xD8,
  33270. 0xD4, 0x09, 0x52, 0xC6, 0x1E, 0xF7, 0x28, 0xDF, 0x08, 0x0F, 0xD0, 0x6F, 0x71,
  33271. 0xA6, 0xDF, 0xE4, 0x60, 0x8E, 0xC0, 0x1E, 0x78, 0x86, 0x50, 0xB0, 0x9B, 0x84,
  33272. 0x7E, 0xE8, 0x36, 0xFA, 0x95, 0xF1, 0x12, 0x51, 0xC7, 0x18, 0x96, 0xA2, 0x29,
  33273. 0xBB, 0x70, 0x02, 0xB4, 0xF9, 0xA8, 0x3D, 0x08, 0x66, 0xA9, 0xB3, 0xFC, 0x0A,
  33274. 0x94, 0x80, 0xFD, 0x78, 0xDC, 0xAB, 0x82, 0x5A, 0xD2, 0xCD, 0xC2, 0x87, 0xC6,
  33275. 0x4B, 0x07, 0xFA, 0xD1, 0xC3, 0xD9, 0x34, 0x41, 0x85, 0xF8, 0xD0, 0xB6, 0x0A,
  33276. 0x9D, 0x00, 0x91, 0x35, 0x05, 0x88, 0xC3, 0xE3, 0x9B, 0x22, 0xD2, 0xB8, 0xFD,
  33277. 0x95, 0x3E, 0x6D, 0x5D, 0x48, 0xA3, 0x68, 0xCF, 0x02, 0x42, 0x79, 0x79, 0x8A,
  33278. 0xAA, 0x01, 0xD6, 0x09, 0x14, 0x2C, 0xF4, 0x83, 0xA3, 0x80, 0x31, 0x55, 0x46,
  33279. 0x6E, 0xC5, 0xE5, 0x2F, 0x30, 0x58, 0x81, 0xA2, 0x90, 0xBE, 0x2E, 0xA1, 0xC3,
  33280. 0x0F, 0xA6, 0xF5, 0x51, 0x00, 0x39, 0xB6, 0xF2, 0x2A, 0xA3, 0x15, 0x7D, 0x8D,
  33281. 0xF5, 0x66, 0x5C, 0xD9, 0xFC, 0xCF, 0x2F, 0xBF, 0x08, 0x27, 0xE7, 0xD0, 0x03,
  33282. 0xB8, 0xD9, 0x00, 0x13, 0x3D, 0x01, 0x6B, 0xB6, 0xA8, 0xCD, 0x5B, 0x3B, 0x3E,
  33283. 0x93, 0xBF, 0xE6, 0x2E, 0xB7, 0x4A, 0xCF, 0xB3, 0x0A, 0xCE, 0x62, 0x11, 0xD6,
  33284. 0x1F, 0x68, 0x9B, 0x1D, 0x68, 0xD1, 0x8C, 0x97, 0xBD, 0xA1, 0x07, 0x67, 0x73,
  33285. 0x87, 0xE0, 0x36, 0xDA, 0x8C, 0xD2, 0xD2, 0xBB, 0x84, 0x28, 0xA9, 0xFE, 0x52,
  33286. 0x74, 0xD6, 0xB9, 0x0F, 0x0A, 0x6A, 0x2D, 0x28, 0x35, 0x34, 0x3A, 0xD3, 0xE2,
  33287. 0xCD, 0x35, 0x06, 0x7D, 0x1B, 0x35, 0x85, 0x86, 0xD1, 0x3E, 0xF2, 0x6F, 0xA1,
  33288. 0xC4, 0x55, 0xBD, 0x00, 0xD8, 0xC3, 0x5D, 0xC2, 0x1D, 0x6B, 0x6B, 0x27, 0x5B,
  33289. 0x95, 0xF3, 0xAB, 0xB5, 0xD3, 0x37, 0xF2, 0x2C, 0x9C, 0xC7, 0x5D, 0xBD, 0xF1,
  33290. 0x68, 0x1C, 0xAD, 0xF8, 0xB5, 0xE1, 0x29, 0x72, 0x7A, 0x73, 0x62, 0x55, 0x24,
  33291. 0xB9, 0x85, 0xDF, 0x7B, 0x29, 0x7D, 0xDE, 0x08, 0xF5, 0xE4, 0x44, 0xDA, 0x1A,
  33292. 0x30, 0x74, 0xDA, 0xB4, 0x9B, 0x23, 0x9A, 0x3A, 0xC1, 0x53, 0xB2, 0xA2, 0xA3,
  33293. 0x7B, 0x1F, 0xD9, 0x56, 0xD4, 0x4F, 0x9B, 0xB2, 0x1E, 0xEE, 0xB8, 0x6A, 0x4E,
  33294. 0xB5, 0xF4, 0x5A, 0xC9, 0x18, 0x27, 0x9C, 0xDE, 0x14, 0x44, 0xED, 0xC4, 0x3C,
  33295. 0x71, 0x9F, 0x5F, 0xD9, 0x37, 0xA0, 0x78, 0x34, 0x6E, 0xBC, 0xD2, 0x7B, 0x1D,
  33296. 0xFA, 0x08, 0x39, 0x5A, 0x04, 0x73, 0x15, 0xD9, 0x0A, 0x48, 0xC1, 0x2D, 0x15,
  33297. 0x4E, 0x84, 0x30, 0x45, 0x69, 0xB3, 0xE5, 0xF6, 0xAD, 0x09, 0x1E, 0xCC, 0x5F,
  33298. 0x1F, 0x06, 0xD5, 0x58, 0xAD, 0x78, 0xD7, 0x9F, 0xE5, 0xED, 0x3B, 0x09, 0xD5,
  33299. 0xA6, 0x52, 0x6F, 0x92, 0xD3, 0x3C, 0xC6, 0x1E, 0xF2, 0x93, 0x7C, 0xD3, 0x5F,
  33300. 0x70, 0x85, 0x5D, 0xF8, 0xAA, 0x9D, 0xB7, 0x7B, 0x24, 0x5A, 0xE9, 0x0A, 0x35,
  33301. 0x2F, 0xF5, 0xD9, 0x82, 0x02, 0x8A, 0x90, 0x13, 0x5B, 0xB5, 0x67, 0x9C, 0xDD,
  33302. 0xA0, 0x4E, 0x82, 0x27, 0xDA, 0x7E, 0xE8, 0x8E, 0xCD, 0xE1, 0x56, 0x71, 0x2C,
  33303. 0xE6, 0x4E, 0x1F, 0x91, 0xCD, 0x7C, 0x6A, 0xB7, 0x78, 0xD0, 0x26, 0xF3, 0x56,
  33304. 0xA9, 0xD5, 0xA1, 0xC3, 0x3B, 0x98, 0xE9, 0x28, 0x09, 0xEF, 0x50, 0x90, 0xCD,
  33305. 0xC4, 0x8E, 0x75, 0xCC, 0xAC, 0x2D, 0xC9, 0x03, 0x6D, 0xAC, 0xFE, 0xC4, 0x88,
  33306. 0x36, 0xD1, 0x3F, 0xBB, 0x1C, 0x7D, 0xB3, 0x14, 0x61, 0x2C, 0xB7, 0x54, 0x4B,
  33307. 0xDB, 0x64, 0xB6, 0x57, 0x14, 0x16, 0x8E, 0x1E, 0x6C, 0x64, 0xBB, 0x8B, 0x48,
  33308. 0x5D, 0x96, 0x9D, 0xDC, 0x80, 0xA7, 0xF7, 0x54, 0xC7, 0x46, 0x38, 0x3E, 0x44,
  33309. 0xDE, 0x7E, 0x92, 0x8D, 0x07, 0xF6, 0x07, 0x37, 0x4E, 0x16, 0x10, 0xB4, 0x7D,
  33310. 0x88, 0x66, 0x7F, 0xBB, 0xFF, 0xEA, 0x00, 0xF3, 0xFF, 0x97, 0x2C, 0xB5, 0xBE,
  33311. 0x35, 0x4B, 0x5C, 0x36, 0xEC, 0x4C, 0xBD, 0x2B, 0x7D, 0xBF, 0x46, 0xE2, 0x9C,
  33312. 0x0E, 0x8A, 0xA3, 0xEC, 0xB1, 0x0E, 0x9A, 0xDA, 0x9A, 0x9B, 0x28, 0x92, 0x10,
  33313. 0x53, 0x57, 0xEA, 0xEC, 0xA2, 0x32, 0x32, 0x20, 0x1D, 0x97, 0x5C, 0xB6, 0x84,
  33314. 0xA9, 0x93, 0x8D, 0x95, 0x11, 0xA3, 0x24, 0xA3, 0x2D, 0xC6, 0x4A, 0xEF, 0xAA,
  33315. 0x1D, 0x85, 0x2B, 0x7D, 0x28, 0xBE, 0x53, 0xCE, 0x10, 0x1F, 0xAE, 0x0E, 0x41,
  33316. 0x6C, 0x4B, 0x79, 0x12, 0xFB, 0xF7, 0x54, 0xA3, 0x96, 0x54, 0x83, 0x20, 0x96,
  33317. 0x8F, 0x28, 0xA9, 0x3F, 0x8B, 0x3D, 0xBA, 0x77, 0xDC, 0x24, 0xE1, 0xD4, 0x49,
  33318. 0x40, 0xD8, 0x78, 0x31, 0x85, 0x43, 0xF6, 0xFE, 0x5C, 0xA6, 0x8F, 0x90, 0x09,
  33319. 0xB0, 0xE7, 0xC4, 0x95, 0xB2, 0x55, 0x49, 0x97, 0x8F, 0x1C, 0x78, 0x30, 0x20,
  33320. 0xA0, 0xB4, 0xEF, 0x73, 0x56, 0x59, 0x82, 0xFD, 0xCE, 0xBA, 0x6A, 0x8F, 0x2C,
  33321. 0x8B, 0x15, 0xFD, 0xA1, 0x85, 0xA8, 0x5C, 0x0F, 0x11, 0xA5, 0x9D, 0xC2, 0x46,
  33322. 0xC6, 0x9C, 0xC9, 0x40, 0x0B, 0x58, 0x6A, 0x1C, 0x7A, 0x23, 0xF9, 0xE0, 0x95,
  33323. 0x05, 0x13, 0x58, 0x72, 0xE8, 0x9F, 0x30, 0xAC, 0xCD, 0x26, 0xD4, 0x66, 0x13,
  33324. 0xDF, 0x1E, 0x7B, 0x4F, 0x9C, 0xBE, 0x38, 0x79, 0x75, 0x92, 0xA4, 0xDA, 0x26,
  33325. 0x44, 0x55, 0x17, 0xA3, 0xE5, 0x62, 0xDA, 0xEB, 0x86, 0xEA, 0x68, 0xC7, 0xAB,
  33326. 0xFD, 0x2D, 0x43, 0x59, 0x51, 0xC0, 0x75, 0x64, 0x91, 0x01, 0x29, 0x33, 0x28,
  33327. 0xF3, 0x04, 0x83, 0x80, 0x75, 0x37, 0x75, 0x0C, 0x03, 0x7B, 0x0A, 0xAB, 0x8E,
  33328. 0x60, 0x62, 0x8B, 0x4C, 0xAF, 0x2D, 0xA3, 0x2F, 0xFE, 0xAB, 0x45, 0xCF, 0xDA,
  33329. 0xAB, 0xFA, 0xFA, 0x30, 0x3D, 0xE8, 0xA1, 0x96, 0xA5, 0x7B, 0xE2, 0x2A, 0xD0,
  33330. 0xAF, 0x59, 0xF7, 0xD0, 0x32, 0x57, 0x19, 0xBD, 0xCA, 0x9F, 0xD5, 0x1A, 0xC7,
  33331. 0xAA, 0x65, 0x4A, 0x38, 0xB2, 0x70, 0x33, 0xB7, 0x75, 0xD2, 0xCD, 0xD1, 0xF0,
  33332. 0xA8, 0x87, 0x59, 0x20, 0xA5, 0x57, 0x55, 0xB1, 0xB2, 0xC9, 0x4D, 0x97, 0x34,
  33333. 0x41, 0xF3, 0xF0, 0x30, 0xA1, 0x2C, 0x1C, 0x49, 0x3E, 0x89, 0x7D, 0x12, 0xE2,
  33334. 0xC3, 0x04, 0xC3, 0x92, 0xC0, 0xF6, 0x39, 0x10, 0x80, 0x81, 0x8F, 0x08, 0xB4,
  33335. 0xF8, 0xB9, 0x13, 0x4E, 0x2C, 0xAE, 0xB3, 0x71, 0x82, 0x63, 0x98, 0xAB, 0x5C,
  33336. 0x1C, 0x10, 0xEA, 0x66, 0xF9, 0x02, 0x3A, 0x82, 0x61, 0xD0, 0xD4, 0xAE, 0x43,
  33337. 0xD4, 0x01, 0x3E, 0x9D, 0x04, 0x14, 0xF6, 0x60, 0xD8, 0xA7, 0xD6, 0xB8, 0x53,
  33338. 0xC8, 0xDA, 0x80, 0x93, 0xA0, 0x02, 0xDD, 0xCC, 0xE2, 0xF2, 0xBB, 0xFB, 0xE0,
  33339. 0x27, 0xD7, 0x34, 0x9A, 0x71, 0x49, 0xB5, 0x4F, 0x42, 0x1F, 0xB2, 0x9D, 0x6D,
  33340. 0xAA, 0x9D, 0xD3, 0x50, 0xB5, 0x8F, 0x6A, 0x4B, 0xDF, 0x1F, 0xD5, 0x27, 0x8F,
  33341. 0x3B, 0x27, 0xCF, 0x2F, 0x8C, 0xF8, 0x9D, 0x4C, 0x52, 0xBC, 0x32, 0x0F, 0x73,
  33342. 0xD5, 0x51, 0x8E, 0x36, 0x7E, 0xAD, 0x09, 0xF0, 0x94, 0x83, 0x5F, 0x36, 0xFD,
  33343. 0x7C, 0x03, 0xED, 0xF1, 0x5E, 0x4B, 0xF7, 0xAA, 0x55, 0x5C, 0x4A, 0x14, 0x59,
  33344. 0x85, 0x38, 0x2D, 0x8C, 0xDF, 0xEC, 0x65, 0x1B, 0xB8, 0x76, 0x57, 0x96, 0x3C,
  33345. 0x86, 0xED, 0xF2, 0x7F, 0x2D, 0x28, 0x48, 0xDA, 0x49, 0x7F, 0xF7, 0x54, 0x2B,
  33346. 0xD5, 0x39, 0xD5, 0x57, 0x0A, 0x75, 0x7A, 0x3E, 0x5E, 0x5D, 0xBA, 0x4A, 0x15,
  33347. 0xFA, 0xB8, 0x31, 0x80, 0x71, 0x2C, 0xCA, 0xC4, 0x51, 0x10, 0x16, 0x5D, 0x39,
  33348. 0xEC, 0x9D, 0x07, 0xB6, 0x6A, 0x89, 0x9F, 0x9B, 0x5B, 0x6F, 0x03, 0xB0, 0x92,
  33349. 0x01, 0x38, 0x6B, 0x48, 0x99, 0x0A, 0x8F, 0x13, 0xC1, 0xA6, 0x01, 0xEA, 0xBF,
  33350. 0x6F, 0x86, 0x43, 0x51, 0xB6, 0x11, 0x00, 0x00
  33351. };
  33352. WOLFSSL_TEST_SUBROUTINE int compress_test(void)
  33353. {
  33354. int ret = 0;
  33355. word32 dSz = sizeof(sample_text);
  33356. word32 cSz = (dSz + (word32)(dSz * 0.001) + 12);
  33357. byte *c;
  33358. byte *d;
  33359. c = (byte *)XMALLOC(cSz * sizeof(byte), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33360. d = (byte *)XMALLOC(dSz * sizeof(byte), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33361. if (c == NULL || d == NULL) {
  33362. ERROR_OUT(-12100, exit);
  33363. }
  33364. /* follow calloc and initialize to 0 */
  33365. XMEMSET(c, 0, cSz);
  33366. XMEMSET(d, 0, dSz);
  33367. if ((ret = wc_Compress(c, cSz, sample_text, dSz, 0)) < 0) {
  33368. ERROR_OUT(-12101, exit);
  33369. }
  33370. cSz = (word32)ret;
  33371. if ((ret = wc_DeCompress(d, dSz, c, cSz)) != (int)dSz) {
  33372. ERROR_OUT(-12102, exit);
  33373. }
  33374. dSz = (word32)ret;
  33375. if (XMEMCMP(d, sample_text, dSz) != 0) {
  33376. ERROR_OUT(-12103, exit);
  33377. }
  33378. /* GZIP tests */
  33379. cSz = (dSz + (word32)(dSz * 0.001) + 12); /* reset cSz */
  33380. XMEMSET(c, 0, cSz);
  33381. XMEMSET(d, 0, dSz);
  33382. ret = wc_Compress_ex(c, cSz, sample_text, dSz, 0, LIBZ_WINBITS_GZIP);
  33383. if (ret < 0) {
  33384. ERROR_OUT(-12104, exit);
  33385. }
  33386. cSz = (word32)ret;
  33387. ret = wc_DeCompress_ex(d, dSz, c, cSz, LIBZ_WINBITS_GZIP);
  33388. if (ret < 0) {
  33389. ERROR_OUT(-12105, exit);
  33390. }
  33391. if (XMEMCMP(d, sample_text, dSz) != 0) {
  33392. ERROR_OUT(-12106, exit);
  33393. }
  33394. /* Try with gzip generated output */
  33395. XMEMSET(d, 0, dSz);
  33396. ret = wc_DeCompress_ex(d, dSz, sample_text_gz, sizeof(sample_text_gz),
  33397. LIBZ_WINBITS_GZIP);
  33398. if (ret < 0) {
  33399. ERROR_OUT(-12107, exit);
  33400. }
  33401. dSz = (word32)ret;
  33402. if (XMEMCMP(d, sample_text, dSz) != 0) {
  33403. ERROR_OUT(-12108, exit);
  33404. }
  33405. ret = 0; /* success */
  33406. exit:
  33407. if (c) XFREE(c, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33408. if (d) XFREE(d, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33409. return ret;
  33410. }
  33411. #endif /* HAVE_LIBZ */
  33412. #ifdef HAVE_PKCS7
  33413. /* External Debugging/Testing Note:
  33414. *
  33415. * PKCS#7 test functions can output generated PKCS#7/CMS bundles for
  33416. * additional testing. To dump bundles to files DER encoded files, please
  33417. * define:
  33418. *
  33419. * #define PKCS7_OUTPUT_TEST_BUNDLES
  33420. */
  33421. /* Loads certs and keys for use with PKCS7 tests, from either files
  33422. * or buffers.
  33423. *
  33424. * rsaClientCertBuf - output buffer for RSA client cert
  33425. * rsaClientCertBufSz - IN/OUT size of output buffer, size of RSA client cert
  33426. * rsaClientPrivKeyBuf - output buffer for RSA client private key
  33427. * rsaClientPrivKeyBufSz - IN/OUT size of output buffer, size of RSA client key
  33428. *
  33429. * rsaServerCertBuf - output buffer for RSA server cert
  33430. * rsaServerCertBufSz - IN/OUT size of output buffer, size of RSA server cert
  33431. * rsaServerPrivKeyBuf - output buffer for RSA server private key
  33432. * rsaServerPrivKeyBufSz - IN/OUT size of output buffer, size of RSA server key
  33433. *
  33434. * rsaCaCertBuf - output buffer for RSA CA cert
  33435. * rsaCaCertBufSz - IN/OUT size of output buffer, size of RSA ca cert
  33436. * rsaCaPrivKeyBuf - output buffer for RSA CA private key
  33437. * rsaCaPrivKeyBufSz - IN/OUT size of output buffer, size of RSA CA key
  33438. *
  33439. * eccClientCertBuf - output buffer for ECC cert
  33440. * eccClientCertBufSz - IN/OUT size of output buffer, size of ECC cert
  33441. * eccClientPrivKeyBuf - output buffer for ECC private key
  33442. * eccClientPrivKeyBufSz - IN/OUT size of output buffer, size of ECC private key
  33443. *
  33444. * Returns 0 on success, negative on error
  33445. */
  33446. static int pkcs7_load_certs_keys(
  33447. byte* rsaClientCertBuf, word32* rsaClientCertBufSz,
  33448. byte* rsaClientPrivKeyBuf, word32* rsaClientPrivKeyBufSz,
  33449. byte* rsaServerCertBuf, word32* rsaServerCertBufSz,
  33450. byte* rsaServerPrivKeyBuf, word32* rsaServerPrivKeyBufSz,
  33451. byte* rsaCaCertBuf, word32* rsaCaCertBufSz,
  33452. byte* rsaCaPrivKeyBuf, word32* rsaCaPrivKeyBufSz,
  33453. byte* eccClientCertBuf, word32* eccClientCertBufSz,
  33454. byte* eccClientPrivKeyBuf, word32* eccClientPrivKeyBufSz)
  33455. {
  33456. #ifndef NO_FILESYSTEM
  33457. XFILE certFile;
  33458. XFILE keyFile;
  33459. (void)certFile;
  33460. (void)keyFile;
  33461. #endif
  33462. #ifndef NO_RSA
  33463. if (rsaClientCertBuf == NULL || rsaClientCertBufSz == NULL ||
  33464. rsaClientPrivKeyBuf == NULL || rsaClientPrivKeyBufSz == NULL)
  33465. return BAD_FUNC_ARG;
  33466. #endif
  33467. #ifdef HAVE_ECC
  33468. if (eccClientCertBuf == NULL || eccClientCertBufSz == NULL ||
  33469. eccClientPrivKeyBuf == NULL || eccClientPrivKeyBufSz == NULL)
  33470. return BAD_FUNC_ARG;
  33471. #endif
  33472. /* RSA */
  33473. #ifndef NO_RSA
  33474. #ifdef USE_CERT_BUFFERS_1024
  33475. if (*rsaClientCertBufSz < (word32)sizeof_client_cert_der_1024)
  33476. return -12110;
  33477. XMEMCPY(rsaClientCertBuf, client_cert_der_1024,
  33478. sizeof_client_cert_der_1024);
  33479. *rsaClientCertBufSz = sizeof_client_cert_der_1024;
  33480. if (rsaServerCertBuf != NULL) {
  33481. if (*rsaServerCertBufSz < (word32)sizeof_server_cert_der_1024)
  33482. return -12111;
  33483. XMEMCPY(rsaServerCertBuf, server_cert_der_1024,
  33484. sizeof_server_cert_der_1024);
  33485. *rsaServerCertBufSz = sizeof_server_cert_der_1024;
  33486. }
  33487. if (rsaCaCertBuf != NULL) {
  33488. if (*rsaCaCertBufSz < (word32)sizeof_ca_cert_der_1024)
  33489. return -12112;
  33490. XMEMCPY(rsaCaCertBuf, ca_cert_der_1024, sizeof_ca_cert_der_1024);
  33491. *rsaCaCertBufSz = sizeof_ca_cert_der_1024;
  33492. }
  33493. #elif defined(USE_CERT_BUFFERS_2048)
  33494. if (*rsaClientCertBufSz < (word32)sizeof_client_cert_der_2048)
  33495. return -12113;
  33496. XMEMCPY(rsaClientCertBuf, client_cert_der_2048,
  33497. sizeof_client_cert_der_2048);
  33498. *rsaClientCertBufSz = sizeof_client_cert_der_2048;
  33499. if (rsaServerCertBuf != NULL) {
  33500. if (*rsaServerCertBufSz < (word32)sizeof_server_cert_der_2048)
  33501. return -12114;
  33502. XMEMCPY(rsaServerCertBuf, server_cert_der_2048,
  33503. sizeof_server_cert_der_2048);
  33504. *rsaServerCertBufSz = sizeof_server_cert_der_2048;
  33505. }
  33506. if (rsaCaCertBuf != NULL) {
  33507. if (*rsaCaCertBufSz < (word32)sizeof_ca_cert_der_2048)
  33508. return -12115;
  33509. XMEMCPY(rsaCaCertBuf, ca_cert_der_2048, sizeof_ca_cert_der_2048);
  33510. *rsaCaCertBufSz = sizeof_ca_cert_der_2048;
  33511. }
  33512. #else
  33513. certFile = XFOPEN(clientCert, "rb");
  33514. if (!certFile)
  33515. return -12116;
  33516. *rsaClientCertBufSz = (word32)XFREAD(rsaClientCertBuf, 1,
  33517. *rsaClientCertBufSz, certFile);
  33518. XFCLOSE(certFile);
  33519. if (rsaServerCertBuf != NULL) {
  33520. certFile = XFOPEN(rsaServerCertDerFile, "rb");
  33521. if (!certFile)
  33522. return -12117;
  33523. *rsaServerCertBufSz = (word32)XFREAD(rsaServerCertBuf, 1,
  33524. *rsaServerCertBufSz, certFile);
  33525. XFCLOSE(certFile);
  33526. }
  33527. if (rsaCaCertBuf != NULL) {
  33528. certFile = XFOPEN(rsaCaCertDerFile, "rb");
  33529. if (!certFile)
  33530. return -12118;
  33531. *rsaCaCertBufSz = (word32)XFREAD(rsaCaCertBuf, 1, *rsaCaCertBufSz,
  33532. certFile);
  33533. XFCLOSE(certFile);
  33534. }
  33535. #endif
  33536. #ifdef USE_CERT_BUFFERS_1024
  33537. if (*rsaClientPrivKeyBufSz < (word32)sizeof_client_key_der_1024)
  33538. return -12119;
  33539. XMEMCPY(rsaClientPrivKeyBuf, client_key_der_1024,
  33540. sizeof_client_key_der_1024);
  33541. *rsaClientPrivKeyBufSz = sizeof_client_key_der_1024;
  33542. if (rsaServerPrivKeyBuf != NULL) {
  33543. if (*rsaServerPrivKeyBufSz < (word32)sizeof_server_key_der_1024)
  33544. return -12120;
  33545. XMEMCPY(rsaServerPrivKeyBuf, server_key_der_1024,
  33546. sizeof_server_key_der_1024);
  33547. *rsaServerPrivKeyBufSz = sizeof_server_key_der_1024;
  33548. }
  33549. if (rsaCaPrivKeyBuf != NULL) {
  33550. if (*rsaCaPrivKeyBufSz < (word32)sizeof_ca_key_der_1024)
  33551. return -12121;
  33552. XMEMCPY(rsaCaPrivKeyBuf, ca_key_der_1024, sizeof_ca_key_der_1024);
  33553. *rsaCaPrivKeyBufSz = sizeof_ca_key_der_1024;
  33554. }
  33555. #elif defined(USE_CERT_BUFFERS_2048)
  33556. if (*rsaClientPrivKeyBufSz < (word32)sizeof_client_key_der_2048)
  33557. return -12122;
  33558. XMEMCPY(rsaClientPrivKeyBuf, client_key_der_2048,
  33559. sizeof_client_key_der_2048);
  33560. *rsaClientPrivKeyBufSz = sizeof_client_key_der_2048;
  33561. if (rsaServerPrivKeyBuf != NULL) {
  33562. if (*rsaServerPrivKeyBufSz < (word32)sizeof_server_key_der_2048)
  33563. return -12123;
  33564. XMEMCPY(rsaServerPrivKeyBuf, server_key_der_2048,
  33565. sizeof_server_key_der_2048);
  33566. *rsaServerPrivKeyBufSz = sizeof_server_key_der_2048;
  33567. }
  33568. if (rsaCaPrivKeyBuf != NULL) {
  33569. if (*rsaCaPrivKeyBufSz < (word32)sizeof_ca_key_der_2048)
  33570. return -12124;
  33571. XMEMCPY(rsaCaPrivKeyBuf, ca_key_der_2048, sizeof_ca_key_der_2048);
  33572. *rsaCaPrivKeyBufSz = sizeof_ca_key_der_2048;
  33573. }
  33574. #else
  33575. keyFile = XFOPEN(clientKey, "rb");
  33576. if (!keyFile)
  33577. return -12125;
  33578. *rsaClientPrivKeyBufSz = (word32)XFREAD(rsaClientPrivKeyBuf, 1,
  33579. *rsaClientPrivKeyBufSz, keyFile);
  33580. XFCLOSE(keyFile);
  33581. if (rsaServerPrivKeyBuf != NULL) {
  33582. keyFile = XFOPEN(rsaServerKeyDerFile, "rb");
  33583. if (!keyFile)
  33584. return -12126;
  33585. *rsaServerPrivKeyBufSz = (word32)XFREAD(rsaServerPrivKeyBuf, 1,
  33586. *rsaServerPrivKeyBufSz, keyFile);
  33587. XFCLOSE(keyFile);
  33588. }
  33589. if (rsaCaPrivKeyBuf != NULL) {
  33590. keyFile = XFOPEN(rsaCaKeyFile, "rb");
  33591. if (!keyFile)
  33592. return -12127;
  33593. *rsaCaPrivKeyBufSz = (word32)XFREAD(rsaCaPrivKeyBuf, 1,
  33594. *rsaCaPrivKeyBufSz, keyFile);
  33595. XFCLOSE(keyFile);
  33596. }
  33597. #endif /* USE_CERT_BUFFERS */
  33598. #endif /* NO_RSA */
  33599. /* ECC */
  33600. #ifdef HAVE_ECC
  33601. #ifdef USE_CERT_BUFFERS_256
  33602. if (*eccClientCertBufSz < (word32)sizeof_cliecc_cert_der_256)
  33603. return -12128;
  33604. XMEMCPY(eccClientCertBuf, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  33605. *eccClientCertBufSz = sizeof_cliecc_cert_der_256;
  33606. #else
  33607. certFile = XFOPEN(eccClientCert, "rb");
  33608. if (!certFile)
  33609. return -12129;
  33610. *eccClientCertBufSz = (word32)XFREAD(eccClientCertBuf, 1,
  33611. *eccClientCertBufSz, certFile);
  33612. XFCLOSE(certFile);
  33613. #endif /* USE_CERT_BUFFERS_256 */
  33614. #ifdef USE_CERT_BUFFERS_256
  33615. if (*eccClientPrivKeyBufSz < (word32)sizeof_ecc_clikey_der_256)
  33616. return -12130;
  33617. XMEMCPY(eccClientPrivKeyBuf, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  33618. *eccClientPrivKeyBufSz = sizeof_ecc_clikey_der_256;
  33619. #else
  33620. keyFile = XFOPEN(eccClientKey, "rb");
  33621. if (!keyFile)
  33622. return -12131;
  33623. *eccClientPrivKeyBufSz = (word32)XFREAD(eccClientPrivKeyBuf, 1,
  33624. *eccClientPrivKeyBufSz, keyFile);
  33625. XFCLOSE(keyFile);
  33626. #endif /* USE_CERT_BUFFERS_256 */
  33627. #endif /* HAVE_ECC */
  33628. #ifdef NO_RSA
  33629. (void)rsaClientCertBuf;
  33630. (void)rsaClientCertBufSz;
  33631. (void)rsaClientPrivKeyBuf;
  33632. (void)rsaClientPrivKeyBufSz;
  33633. (void)rsaServerCertBuf;
  33634. (void)rsaServerCertBufSz;
  33635. (void)rsaServerPrivKeyBuf;
  33636. (void)rsaServerPrivKeyBufSz;
  33637. (void)rsaCaCertBuf;
  33638. (void)rsaCaCertBufSz;
  33639. (void)rsaCaPrivKeyBuf;
  33640. (void)rsaCaPrivKeyBufSz;
  33641. #endif
  33642. #ifndef HAVE_ECC
  33643. (void)eccClientCertBuf;
  33644. (void)eccClientCertBufSz;
  33645. (void)eccClientPrivKeyBuf;
  33646. (void)eccClientPrivKeyBufSz;
  33647. #endif
  33648. #ifndef NO_FILESYSTEM
  33649. (void)certFile;
  33650. (void)keyFile;
  33651. #endif
  33652. return 0;
  33653. }
  33654. typedef struct {
  33655. const byte* content;
  33656. word32 contentSz;
  33657. int contentOID;
  33658. int encryptOID;
  33659. int keyWrapOID;
  33660. int keyAgreeOID;
  33661. byte* cert;
  33662. size_t certSz;
  33663. byte* privateKey;
  33664. word32 privateKeySz;
  33665. byte* optionalUkm;
  33666. word32 optionalUkmSz;
  33667. int ktriOptions; /* KTRI options flags */
  33668. int kariOptions; /* KARI options flags */
  33669. /* KEKRI specific */
  33670. const byte* secretKey; /* key, only for kekri RecipientInfo types */
  33671. word32 secretKeySz; /* size of secretKey, bytes */
  33672. const byte* secretKeyId; /* key identifier */
  33673. word32 secretKeyIdSz; /* size of key identifier, bytes */
  33674. void* timePtr; /* time_t pointer */
  33675. byte* otherAttrOID; /* OPTIONAL, other attribute OID */
  33676. word32 otherAttrOIDSz; /* size of otherAttrOID, bytes */
  33677. byte* otherAttr; /* OPTIONAL, other attribute, ASN.1 encoded */
  33678. word32 otherAttrSz; /* size of otherAttr, bytes */
  33679. int kekriOptions; /* KEKRI options flags */
  33680. /* PWRI specific */
  33681. const char* password;
  33682. word32 passwordSz;
  33683. const byte* salt;
  33684. word32 saltSz;
  33685. int kdfOID;
  33686. int hashOID;
  33687. int kdfIterations;
  33688. int pwriOptions; /* PWRI options flags */
  33689. /* ORI specific */
  33690. int isOri;
  33691. int oriOptions; /* ORI options flags */
  33692. const char* outFileName;
  33693. } pkcs7EnvelopedVector;
  33694. static const byte asnDataOid[] = {
  33695. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x07, 0x01
  33696. };
  33697. /* ORI encrypt callback, responsible for encrypting content-encryption key (CEK)
  33698. * and giving wolfCrypt the value for oriOID and oriValue to place in
  33699. * OtherRecipientInfo.
  33700. *
  33701. * Returns 0 on success, negative upon error. */
  33702. static int myOriEncryptCb(PKCS7* pkcs7, byte* cek, word32 cekSz, byte* oriType,
  33703. word32* oriTypeSz, byte* oriValue, word32* oriValueSz,
  33704. void* ctx)
  33705. {
  33706. int i;
  33707. /* make sure buffers are large enough */
  33708. if ((*oriValueSz < (2 + cekSz)) || (*oriTypeSz < sizeof(oriType)))
  33709. return -12140;
  33710. /* our simple encryption algorithm will be take the bitwise complement */
  33711. oriValue[0] = 0x04; /*ASN OCTET STRING */
  33712. oriValue[1] = (byte)cekSz; /* length */
  33713. for (i = 0; i < (int)cekSz; i++) {
  33714. oriValue[2 + i] = ~cek[i];
  33715. }
  33716. *oriValueSz = 2 + cekSz;
  33717. /* set oriType to ASN.1 encoded data OID */
  33718. XMEMCPY(oriType, asnDataOid, sizeof(asnDataOid));
  33719. *oriTypeSz = sizeof(asnDataOid);
  33720. (void)pkcs7;
  33721. (void)ctx;
  33722. return 0;
  33723. }
  33724. /* ORI decrypt callback, responsible for providing a decrypted content
  33725. * encryption key (CEK) placed into decryptedKey and size placed into
  33726. * decryptedKeySz. oriOID and oriValue are given to the callback to help
  33727. * in decrypting the encrypted CEK.
  33728. *
  33729. * Returns 0 on success, negative upon error. */
  33730. static int myOriDecryptCb(PKCS7* pkcs7, byte* oriType, word32 oriTypeSz,
  33731. byte* oriValue, word32 oriValueSz, byte* decryptedKey,
  33732. word32* decryptedKeySz, void* ctx)
  33733. {
  33734. int i;
  33735. /* make sure oriType matches what we expect */
  33736. if (oriTypeSz != sizeof(asnDataOid))
  33737. return -12150;
  33738. if (XMEMCMP(oriType, asnDataOid, sizeof(asnDataOid)) != 0)
  33739. return -12151;
  33740. /* make sure decrypted buffer is large enough */
  33741. if (*decryptedKeySz < oriValueSz)
  33742. return -12152;
  33743. /* decrypt encrypted CEK using simple bitwise complement,
  33744. only for example */
  33745. for (i = 0; i < (int)oriValueSz - 2; i++) {
  33746. decryptedKey[i] = ~oriValue[2 + i];
  33747. }
  33748. *decryptedKeySz = oriValueSz - 2;
  33749. (void)pkcs7;
  33750. (void)ctx;
  33751. return 0;
  33752. }
  33753. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  33754. /* returns 0 on success */
  33755. static int myDecryptionFunc(PKCS7* pkcs7, int encryptOID, byte* iv, int ivSz,
  33756. byte* aad, word32 aadSz, byte* authTag, word32 authTagSz,
  33757. byte* in, int inSz, byte* out, void* usrCtx)
  33758. {
  33759. int keyId = -1, ret, keySz;
  33760. word32 keyIdSz = 8;
  33761. const byte* key;
  33762. byte keyIdRaw[8];
  33763. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  33764. Aes *aes;
  33765. #else
  33766. Aes aes[1];
  33767. #endif
  33768. /* looking for KEY ID
  33769. * fwDecryptKeyID OID "1.2.840.113549.1.9.16.2.37
  33770. */
  33771. WOLFSSL_SMALL_STACK_STATIC const unsigned char OID[] = {
  33772. /* 0x06, 0x0B do not pass in tag and length */
  33773. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  33774. 0x01, 0x09, 0x10, 0x02, 0x25
  33775. };
  33776. WOLFSSL_SMALL_STACK_STATIC const byte defKey[] = {
  33777. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  33778. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  33779. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  33780. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  33781. };
  33782. WOLFSSL_SMALL_STACK_STATIC const byte altKey[] = {
  33783. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  33784. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  33785. };
  33786. /* test user context passed in */
  33787. if (usrCtx == NULL || *(int*)usrCtx != 1) {
  33788. return -12160;
  33789. }
  33790. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  33791. if ((aes = (Aes *)XMALLOC(sizeof *aes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER)) == NULL)
  33792. return -12164;
  33793. #endif
  33794. /* if needing to find keyIdSz can call with NULL */
  33795. ret = wc_PKCS7_GetAttributeValue(pkcs7, OID, sizeof(OID), NULL,
  33796. &keyIdSz);
  33797. if (ret != LENGTH_ONLY_E) {
  33798. printf("Unexpected error %d when getting keyIdSz\n", ret);
  33799. printf("Possibly no KEY ID attribute set\n");
  33800. ERROR_OUT(-12161, out);
  33801. }
  33802. else {
  33803. XMEMSET(keyIdRaw, 0, sizeof(keyIdRaw));
  33804. ret = wc_PKCS7_GetAttributeValue(pkcs7, OID, sizeof(OID), keyIdRaw,
  33805. &keyIdSz);
  33806. if (ret < 0) {
  33807. goto out;
  33808. }
  33809. if (keyIdSz < 3) {
  33810. printf("keyIdSz is smaller than expected\n");
  33811. ERROR_OUT(-12162, out);
  33812. }
  33813. if (keyIdSz > 2 + sizeof(int)) {
  33814. printf("example case was only expecting a keyId of int size\n");
  33815. ERROR_OUT(-12163, out);
  33816. }
  33817. /* keyIdRaw[0] OCTET TAG */
  33818. /* keyIdRaw[1] Length */
  33819. #ifdef BIG_ENDIAN_ORDER
  33820. if (keyIdRaw[1] == 0x01) {
  33821. keyId = 1;
  33822. }
  33823. #else
  33824. XMEMCPY(&keyId, keyIdRaw + 2, sizeof(keyId));
  33825. #endif
  33826. }
  33827. /* Use keyID here if found to select key and decrypt in HSM or in this
  33828. * example just select key and do software decryption */
  33829. if (keyId == 1) {
  33830. key = altKey;
  33831. keySz = sizeof(altKey);
  33832. }
  33833. else {
  33834. key = defKey;
  33835. keySz = sizeof(defKey);
  33836. }
  33837. switch (encryptOID) {
  33838. #ifdef WOLFSSL_AES_256
  33839. case AES256CBCb:
  33840. if ((keySz != 32 ) || (ivSz != AES_BLOCK_SIZE))
  33841. ERROR_OUT(BAD_FUNC_ARG, out);
  33842. break;
  33843. #endif
  33844. #ifdef WOLFSSL_AES_128
  33845. case AES128CBCb:
  33846. if ((keySz != 16 ) || (ivSz != AES_BLOCK_SIZE))
  33847. ERROR_OUT(BAD_FUNC_ARG, out);
  33848. break;
  33849. #endif
  33850. default:
  33851. printf("Unsupported content cipher type for example");
  33852. ERROR_OUT(ALGO_ID_E, out);
  33853. };
  33854. ret = wc_AesInit(aes, HEAP_HINT, INVALID_DEVID);
  33855. if (ret == 0) {
  33856. ret = wc_AesSetKey(aes, key, keySz, iv, AES_DECRYPTION);
  33857. if (ret == 0)
  33858. ret = wc_AesCbcDecrypt(aes, out, in, inSz);
  33859. wc_AesFree(aes);
  33860. }
  33861. out:
  33862. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  33863. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33864. #endif
  33865. (void)aad;
  33866. (void)aadSz;
  33867. (void)authTag;
  33868. (void)authTagSz;
  33869. return ret;
  33870. }
  33871. #endif /* !NO_AES && HAVE_AES_CBC */
  33872. #define PKCS7_BUF_SIZE 2048
  33873. static int pkcs7enveloped_run_vectors(byte* rsaCert, word32 rsaCertSz,
  33874. byte* rsaPrivKey, word32 rsaPrivKeySz,
  33875. byte* eccCert, word32 eccCertSz,
  33876. byte* eccPrivKey, word32 eccPrivKeySz)
  33877. {
  33878. int ret = 0, testSz = 0, i;
  33879. int envelopedSz, decodedSz;
  33880. byte *enveloped = NULL;
  33881. byte *decoded = NULL;
  33882. PKCS7* pkcs7 = NULL;
  33883. #ifdef ECC_TIMING_RESISTANT
  33884. WC_RNG rng;
  33885. #endif
  33886. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  33887. XFILE pkcs7File;
  33888. #endif
  33889. WOLFSSL_SMALL_STACK_STATIC const byte data[] = { /* Hello World */
  33890. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  33891. 0x72,0x6c,0x64
  33892. };
  33893. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_256) && \
  33894. defined(HAVE_ECC) && defined(WOLFSSL_SHA512)
  33895. byte optionalUkm[] = {
  33896. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07
  33897. };
  33898. #endif /* NO_AES */
  33899. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128) && \
  33900. !defined(NO_SHA)
  33901. /* encryption key for kekri recipient types */
  33902. WOLFSSL_SMALL_STACK_STATIC const byte secretKey[] = {
  33903. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  33904. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07
  33905. };
  33906. /* encryption key identifier */
  33907. WOLFSSL_SMALL_STACK_STATIC const byte secretKeyId[] = {
  33908. 0x02,0x02,0x03,0x04
  33909. };
  33910. #endif
  33911. #if !defined(NO_PWDBASED) && !defined(NO_SHA) && \
  33912. !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  33913. #ifndef HAVE_FIPS
  33914. WOLFSSL_SMALL_STACK_STATIC const char password[] = "password"; /* NOTE: Password is too short for FIPS */
  33915. #else
  33916. WOLFSSL_SMALL_STACK_STATIC const char password[] = "passwordFIPS_MODE";
  33917. #endif
  33918. WOLFSSL_SMALL_STACK_STATIC const byte salt[] = {
  33919. 0x12, 0x34, 0x56, 0x78, 0x78, 0x56, 0x34, 0x12
  33920. };
  33921. #endif
  33922. #define MAX_TESTVECTORS_LEN 13
  33923. #define ADD_PKCS7ENVELOPEDVECTOR(...) { \
  33924. pkcs7EnvelopedVector _this_vector = { __VA_ARGS__ }; \
  33925. if (testSz == MAX_TESTVECTORS_LEN) { \
  33926. ret = -12534; \
  33927. goto out; \
  33928. } \
  33929. XMEMCPY(&testVectors[testSz++], &_this_vector, sizeof _this_vector);\
  33930. }
  33931. pkcs7EnvelopedVector *testVectors = NULL;
  33932. #ifdef ECC_TIMING_RESISTANT
  33933. XMEMSET(&rng, 0, sizeof(rng));
  33934. #endif
  33935. testVectors = (pkcs7EnvelopedVector *)XMALLOC(MAX_TESTVECTORS_LEN * sizeof(*testVectors),
  33936. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33937. if (testVectors == NULL) {
  33938. ret = -12534;
  33939. goto out;
  33940. }
  33941. {
  33942. /* key transport key encryption technique */
  33943. #ifndef NO_RSA
  33944. #ifndef NO_DES3
  33945. ADD_PKCS7ENVELOPEDVECTOR(
  33946. data, (word32)sizeof(data), DATA, DES3b, 0, 0, rsaCert, rsaCertSz,
  33947. rsaPrivKey, rsaPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL,
  33948. 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  33949. "pkcs7envelopedDataDES3.der");
  33950. #endif
  33951. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  33952. #ifdef WOLFSSL_AES_128
  33953. ADD_PKCS7ENVELOPEDVECTOR(
  33954. data, (word32)sizeof(data), DATA, AES128CBCb, 0, 0, rsaCert, rsaCertSz,
  33955. rsaPrivKey, rsaPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL,
  33956. 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  33957. "pkcs7envelopedDataAES128CBC.der");
  33958. #endif
  33959. #ifdef WOLFSSL_AES_192
  33960. ADD_PKCS7ENVELOPEDVECTOR(
  33961. data, (word32)sizeof(data), DATA, AES192CBCb, 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. "pkcs7envelopedDataAES192CBC.der");
  33965. #endif
  33966. #ifdef WOLFSSL_AES_256
  33967. ADD_PKCS7ENVELOPEDVECTOR(
  33968. data, (word32)sizeof(data), DATA, AES256CBCb, 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. "pkcs7envelopedDataAES256CBC.der");
  33972. /* explicitly using SKID for SubjectKeyIdentifier */
  33973. ADD_PKCS7ENVELOPEDVECTOR(
  33974. data, (word32)sizeof(data), DATA, AES256CBCb, 0, 0, rsaCert, rsaCertSz,
  33975. rsaPrivKey, rsaPrivKeySz, NULL, 0, CMS_SKID, 0, NULL, 0, NULL, 0, NULL,
  33976. NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  33977. "pkcs7envelopedDataAES256CBC_SKID.der");
  33978. /* explicitly using IssuerAndSerialNumber for SubjectKeyIdentifier */
  33979. ADD_PKCS7ENVELOPEDVECTOR(
  33980. data, (word32)sizeof(data), DATA, AES256CBCb, 0, 0, rsaCert, rsaCertSz,
  33981. rsaPrivKey, rsaPrivKeySz, NULL, 0, CMS_ISSUER_AND_SERIAL_NUMBER, 0,
  33982. NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0,
  33983. 0, 0, 0, 0, "pkcs7envelopedDataAES256CBC_IANDS.der");
  33984. #endif
  33985. #endif /* !NO_AES && HAVE_AES_CBC */
  33986. #endif
  33987. /* key agreement key encryption technique*/
  33988. #ifdef HAVE_ECC
  33989. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  33990. #if !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  33991. ADD_PKCS7ENVELOPEDVECTOR(
  33992. data, (word32)sizeof(data), DATA, AES128CBCb, AES128_WRAP,
  33993. dhSinglePass_stdDH_sha1kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  33994. eccPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0,
  33995. 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  33996. "pkcs7envelopedDataAES128CBC_ECDH_SHA1KDF.der");
  33997. #endif
  33998. #if !defined(NO_SHA256) && defined(WOLFSSL_AES_256)
  33999. ADD_PKCS7ENVELOPEDVECTOR(
  34000. data, (word32)sizeof(data), DATA, AES256CBCb, AES256_WRAP,
  34001. dhSinglePass_stdDH_sha256kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  34002. eccPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0,
  34003. 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  34004. "pkcs7envelopedDataAES256CBC_ECDH_SHA256KDF.der");
  34005. #endif /* NO_SHA256 && WOLFSSL_AES_256 */
  34006. #if defined(WOLFSSL_SHA512) && defined(WOLFSSL_AES_256)
  34007. ADD_PKCS7ENVELOPEDVECTOR(
  34008. data, (word32)sizeof(data), DATA, AES256CBCb, AES256_WRAP,
  34009. dhSinglePass_stdDH_sha512kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  34010. eccPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0,
  34011. 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  34012. "pkcs7envelopedDataAES256CBC_ECDH_SHA512KDF.der");
  34013. /* with optional user keying material (ukm) */
  34014. ADD_PKCS7ENVELOPEDVECTOR(
  34015. data, (word32)sizeof(data), DATA, AES256CBCb, AES256_WRAP,
  34016. dhSinglePass_stdDH_sha512kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  34017. eccPrivKeySz, optionalUkm, sizeof(optionalUkm), 0, 0, NULL, 0,
  34018. NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  34019. "pkcs7envelopedDataAES256CBC_ECDH_SHA512KDF_ukm.der");
  34020. #endif /* WOLFSSL_SHA512 && WOLFSSL_AES_256 */
  34021. #endif /* !NO_AES && HAVE_AES_CBC */
  34022. #endif
  34023. /* kekri (KEKRecipientInfo) recipient types */
  34024. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  34025. #if !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  34026. ADD_PKCS7ENVELOPEDVECTOR(
  34027. data, (word32)sizeof(data), DATA, AES128CBCb, AES128_WRAP, 0,
  34028. NULL, 0, NULL, 0, NULL, 0, 0, 0, secretKey, sizeof(secretKey),
  34029. secretKeyId, sizeof(secretKeyId), NULL, NULL, 0, NULL, 0,
  34030. 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  34031. "pkcs7envelopedDataAES128CBC_KEKRI.der");
  34032. #endif
  34033. #endif /* !NO_AES && HAVE_AES_CBC */
  34034. /* pwri (PasswordRecipientInfo) recipient types */
  34035. #if !defined(NO_PWDBASED) && !defined(NO_AES) && defined(HAVE_AES_CBC)
  34036. #if !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  34037. ADD_PKCS7ENVELOPEDVECTOR(
  34038. data, (word32)sizeof(data), DATA, AES128CBCb, 0, 0,
  34039. NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0,
  34040. NULL, 0, NULL, NULL, 0, NULL, 0, 0, password,
  34041. (word32)XSTRLEN(password), salt, sizeof(salt), PBKDF2_OID, WC_SHA, 5,
  34042. 0, 0, 0, "pkcs7envelopedDataAES128CBC_PWRI.der");
  34043. #endif
  34044. #endif
  34045. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && !defined(NO_AES_128)
  34046. /* ori (OtherRecipientInfo) recipient types */
  34047. ADD_PKCS7ENVELOPEDVECTOR(
  34048. data, (word32)sizeof(data), DATA, AES128CBCb, 0, 0, NULL, 0, NULL, 0,
  34049. NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0,
  34050. NULL, 0, 0, 0, 0, 0, 1, 0, "pkcs7envelopedDataAES128CBC_ORI.der");
  34051. #endif
  34052. };
  34053. #undef MAX_TESTVECTORS_LEN
  34054. #undef ADD_PKCS7ENVELOPEDVECTOR
  34055. enveloped = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34056. decoded = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34057. if ((! enveloped) || (! decoded)) {
  34058. ERROR_OUT(-12170, out);
  34059. }
  34060. #ifdef ECC_TIMING_RESISTANT
  34061. #ifndef HAVE_FIPS
  34062. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  34063. #else
  34064. ret = wc_InitRng(&rng);
  34065. #endif
  34066. if (ret != 0) {
  34067. ERROR_OUT(-12171, out);
  34068. }
  34069. #endif
  34070. for (i = 0; i < testSz; i++) {
  34071. pkcs7 = wc_PKCS7_New(HEAP_HINT,
  34072. #ifdef WOLFSSL_ASYNC_CRYPT
  34073. INVALID_DEVID /* async PKCS7 is not supported */
  34074. #else
  34075. devId
  34076. #endif
  34077. );
  34078. if (pkcs7 == NULL) {
  34079. ERROR_OUT(-12172, out);
  34080. }
  34081. if (testVectors[i].secretKey != NULL) {
  34082. /* KEKRI recipient type */
  34083. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  34084. if (ret != 0) {
  34085. ERROR_OUT(-12173, out);
  34086. }
  34087. pkcs7->content = (byte*)testVectors[i].content;
  34088. pkcs7->contentSz = testVectors[i].contentSz;
  34089. pkcs7->contentOID = testVectors[i].contentOID;
  34090. pkcs7->encryptOID = testVectors[i].encryptOID;
  34091. pkcs7->ukm = testVectors[i].optionalUkm;
  34092. pkcs7->ukmSz = testVectors[i].optionalUkmSz;
  34093. ret = wc_PKCS7_AddRecipient_KEKRI(pkcs7, testVectors[i].keyWrapOID,
  34094. (byte *)testVectors[i].secretKey, testVectors[i].secretKeySz,
  34095. (byte *)testVectors[i].secretKeyId, testVectors[i].secretKeyIdSz,
  34096. testVectors[i].timePtr, testVectors[i].otherAttrOID,
  34097. testVectors[i].otherAttrOIDSz, testVectors[i].otherAttr,
  34098. testVectors[i].otherAttrSz, testVectors[i].kekriOptions);
  34099. if (ret < 0) {
  34100. wc_PKCS7_Free(pkcs7);
  34101. ERROR_OUT(-12174, out);
  34102. }
  34103. /* set key, for decryption */
  34104. ret = wc_PKCS7_SetKey(pkcs7, (byte *)testVectors[i].secretKey,
  34105. testVectors[i].secretKeySz);
  34106. if (ret != 0) {
  34107. wc_PKCS7_Free(pkcs7);
  34108. ERROR_OUT(-12175, out);
  34109. }
  34110. } else if (testVectors[i].password != NULL) {
  34111. #if !defined(NO_PWDBASED) && !defined(NO_SHA)
  34112. /* PWRI recipient type */
  34113. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  34114. if (ret != 0) {
  34115. ERROR_OUT(-12176, out);
  34116. }
  34117. pkcs7->content = (byte*)testVectors[i].content;
  34118. pkcs7->contentSz = testVectors[i].contentSz;
  34119. pkcs7->contentOID = testVectors[i].contentOID;
  34120. pkcs7->encryptOID = testVectors[i].encryptOID;
  34121. pkcs7->ukm = testVectors[i].optionalUkm;
  34122. pkcs7->ukmSz = testVectors[i].optionalUkmSz;
  34123. ret = wc_PKCS7_AddRecipient_PWRI(pkcs7,
  34124. (byte *)testVectors[i].password, testVectors[i].passwordSz,
  34125. (byte *)testVectors[i].salt, testVectors[i].saltSz,
  34126. testVectors[i].kdfOID,
  34127. testVectors[i].hashOID, testVectors[i].kdfIterations,
  34128. testVectors[i].encryptOID, testVectors[i].pwriOptions);
  34129. if (ret < 0) {
  34130. wc_PKCS7_Free(pkcs7);
  34131. ERROR_OUT(-12177, out);
  34132. }
  34133. /* set password, for decryption */
  34134. ret = wc_PKCS7_SetPassword(pkcs7, (byte*)testVectors[i].password,
  34135. testVectors[i].passwordSz);
  34136. if (ret < 0) {
  34137. wc_PKCS7_Free(pkcs7);
  34138. ERROR_OUT(-12178, out);
  34139. }
  34140. #endif /* ! NO_PWDBASED && ! NO_SHA */
  34141. } else if (testVectors[i].isOri == 1) {
  34142. /* ORI recipient type */
  34143. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  34144. if (ret != 0) {
  34145. ERROR_OUT(-12179, out);
  34146. }
  34147. pkcs7->content = (byte*)testVectors[i].content;
  34148. pkcs7->contentSz = testVectors[i].contentSz;
  34149. pkcs7->contentOID = testVectors[i].contentOID;
  34150. pkcs7->encryptOID = testVectors[i].encryptOID;
  34151. ret = wc_PKCS7_AddRecipient_ORI(pkcs7, myOriEncryptCb,
  34152. testVectors[i].oriOptions);
  34153. if (ret < 0) {
  34154. wc_PKCS7_Free(pkcs7);
  34155. ERROR_OUT(-12180, out);
  34156. }
  34157. /* set decrypt callback for decryption */
  34158. ret = wc_PKCS7_SetOriDecryptCb(pkcs7, myOriDecryptCb);
  34159. if (ret < 0) {
  34160. wc_PKCS7_Free(pkcs7);
  34161. ERROR_OUT(-12181, out);
  34162. }
  34163. } else {
  34164. /* KTRI or KARI recipient types */
  34165. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  34166. if (ret != 0) {
  34167. ERROR_OUT(-12182, out);
  34168. }
  34169. ret = wc_PKCS7_InitWithCert(pkcs7, testVectors[i].cert,
  34170. (word32)testVectors[i].certSz);
  34171. if (ret != 0) {
  34172. wc_PKCS7_Free(pkcs7);
  34173. ERROR_OUT(-12183, out);
  34174. }
  34175. pkcs7->keyWrapOID = testVectors[i].keyWrapOID;
  34176. pkcs7->keyAgreeOID = testVectors[i].keyAgreeOID;
  34177. pkcs7->privateKey = testVectors[i].privateKey;
  34178. pkcs7->privateKeySz = testVectors[i].privateKeySz;
  34179. pkcs7->content = (byte*)testVectors[i].content;
  34180. pkcs7->contentSz = testVectors[i].contentSz;
  34181. pkcs7->contentOID = testVectors[i].contentOID;
  34182. pkcs7->encryptOID = testVectors[i].encryptOID;
  34183. pkcs7->ukm = testVectors[i].optionalUkm;
  34184. pkcs7->ukmSz = testVectors[i].optionalUkmSz;
  34185. /* set SubjectIdentifier type for KTRI types */
  34186. if (testVectors[i].ktriOptions & CMS_SKID) {
  34187. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID);
  34188. if (ret != 0) {
  34189. wc_PKCS7_Free(pkcs7);
  34190. ERROR_OUT(-12184, out);
  34191. }
  34192. } else if (testVectors[i].ktriOptions &
  34193. CMS_ISSUER_AND_SERIAL_NUMBER) {
  34194. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7,
  34195. CMS_ISSUER_AND_SERIAL_NUMBER);
  34196. if (ret != 0) {
  34197. wc_PKCS7_Free(pkcs7);
  34198. ERROR_OUT(-12185, out);
  34199. }
  34200. }
  34201. }
  34202. #ifdef ECC_TIMING_RESISTANT
  34203. pkcs7->rng = &rng;
  34204. #endif
  34205. /* encode envelopedData */
  34206. envelopedSz = wc_PKCS7_EncodeEnvelopedData(pkcs7, enveloped,
  34207. PKCS7_BUF_SIZE);
  34208. if (envelopedSz <= 0) {
  34209. wc_PKCS7_Free(pkcs7);
  34210. ERROR_OUT(-12186, out);
  34211. }
  34212. /* decode envelopedData */
  34213. pkcs7->contentOID = 0;
  34214. decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, enveloped, envelopedSz,
  34215. decoded, PKCS7_BUF_SIZE);
  34216. if (pkcs7->contentOID != testVectors[i].contentOID ||
  34217. decodedSz <= 0) {
  34218. wc_PKCS7_Free(pkcs7);
  34219. ERROR_OUT(-12187, out);
  34220. }
  34221. /* test decode result */
  34222. if (XMEMCMP(decoded, data, sizeof(data)) != 0){
  34223. wc_PKCS7_Free(pkcs7);
  34224. ERROR_OUT(-12188, out);
  34225. }
  34226. #ifndef NO_PKCS7_STREAM
  34227. { /* test reading byte by byte */
  34228. int z;
  34229. for (z = 0; z < envelopedSz; z++) {
  34230. decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, enveloped + z, 1,
  34231. decoded, PKCS7_BUF_SIZE);
  34232. if (decodedSz <= 0 && decodedSz != WC_PKCS7_WANT_READ_E) {
  34233. printf("unexpected error %d\n", decodedSz);
  34234. ERROR_OUT(-12189, out);
  34235. }
  34236. }
  34237. /* test decode result */
  34238. if (XMEMCMP(decoded, data, sizeof(data)) != 0) {
  34239. printf("stream read compare failed\n");
  34240. wc_PKCS7_Free(pkcs7);
  34241. ERROR_OUT(-12190, out);
  34242. }
  34243. }
  34244. #endif
  34245. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  34246. /* output pkcs7 envelopedData for external testing */
  34247. pkcs7File = XFOPEN(testVectors[i].outFileName, "wb");
  34248. if (!pkcs7File) {
  34249. wc_PKCS7_Free(pkcs7);
  34250. ERROR_OUT(-12191, out);
  34251. }
  34252. ret = (int)XFWRITE(enveloped, 1, envelopedSz, pkcs7File);
  34253. XFCLOSE(pkcs7File);
  34254. if (ret != envelopedSz) {
  34255. wc_PKCS7_Free(pkcs7);
  34256. ERROR_OUT(-12192, out);
  34257. } else {
  34258. /* reset ret to 0 for success */
  34259. ret = 0;
  34260. }
  34261. #endif /* PKCS7_OUTPUT_TEST_BUNDLES */
  34262. wc_PKCS7_Free(pkcs7);
  34263. pkcs7 = NULL;
  34264. }
  34265. #ifdef ECC_TIMING_RESISTANT
  34266. wc_FreeRng(&rng);
  34267. #endif
  34268. (void)eccCert;
  34269. (void)eccCertSz;
  34270. (void)eccPrivKey;
  34271. (void)eccPrivKeySz;
  34272. (void)rsaCert;
  34273. (void)rsaCertSz;
  34274. (void)rsaPrivKey;
  34275. (void)rsaPrivKeySz;
  34276. out:
  34277. if (testVectors)
  34278. XFREE(testVectors, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34279. if (enveloped)
  34280. XFREE(enveloped, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34281. if (decoded)
  34282. XFREE(decoded, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34283. return ret;
  34284. }
  34285. WOLFSSL_TEST_SUBROUTINE int pkcs7enveloped_test(void)
  34286. {
  34287. int ret = 0;
  34288. byte* rsaCert = NULL;
  34289. byte* rsaPrivKey = NULL;
  34290. word32 rsaCertSz = 0;
  34291. word32 rsaPrivKeySz = 0;
  34292. byte* eccCert = NULL;
  34293. byte* eccPrivKey = NULL;
  34294. word32 eccCertSz = 0;
  34295. word32 eccPrivKeySz = 0;
  34296. #ifndef NO_RSA
  34297. /* read client RSA cert and key in DER format */
  34298. rsaCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34299. if (rsaCert == NULL)
  34300. return -12200;
  34301. rsaPrivKey = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34302. if (rsaPrivKey == NULL) {
  34303. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34304. return -12201;
  34305. }
  34306. rsaCertSz = FOURK_BUF;
  34307. rsaPrivKeySz = FOURK_BUF;
  34308. #endif /* NO_RSA */
  34309. #ifdef HAVE_ECC
  34310. /* read client ECC cert and key in DER format */
  34311. eccCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34312. if (eccCert == NULL) {
  34313. #ifndef NO_RSA
  34314. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34315. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34316. #endif
  34317. return -12202;
  34318. }
  34319. eccPrivKey =(byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34320. if (eccPrivKey == NULL) {
  34321. #ifndef NO_RSA
  34322. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34323. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34324. #endif
  34325. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34326. return -12203;
  34327. }
  34328. eccCertSz = FOURK_BUF;
  34329. eccPrivKeySz = FOURK_BUF;
  34330. #endif /* HAVE_ECC */
  34331. ret = pkcs7_load_certs_keys(rsaCert, &rsaCertSz, rsaPrivKey,
  34332. &rsaPrivKeySz, NULL, NULL, NULL, NULL,
  34333. NULL, NULL, NULL, NULL, eccCert, &eccCertSz,
  34334. eccPrivKey, &eccPrivKeySz);
  34335. if (ret < 0) {
  34336. #ifndef NO_RSA
  34337. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34338. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34339. #endif
  34340. #ifdef HAVE_ECC
  34341. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34342. XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34343. #endif
  34344. return -12204;
  34345. }
  34346. ret = pkcs7enveloped_run_vectors(rsaCert, (word32)rsaCertSz,
  34347. rsaPrivKey, (word32)rsaPrivKeySz,
  34348. eccCert, (word32)eccCertSz,
  34349. eccPrivKey, (word32)eccPrivKeySz);
  34350. #ifndef NO_RSA
  34351. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34352. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34353. #endif
  34354. #ifdef HAVE_ECC
  34355. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34356. XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34357. #endif
  34358. return ret;
  34359. }
  34360. #if defined(HAVE_AESGCM) || defined(HAVE_AESCCM)
  34361. typedef struct {
  34362. const byte* content;
  34363. word32 contentSz;
  34364. int contentOID;
  34365. int encryptOID;
  34366. int keyWrapOID;
  34367. int keyAgreeOID;
  34368. byte* cert;
  34369. size_t certSz;
  34370. byte* privateKey;
  34371. word32 privateKeySz;
  34372. PKCS7Attrib* authAttribs;
  34373. word32 authAttribsSz;
  34374. PKCS7Attrib* unauthAttribs;
  34375. word32 unauthAttribsSz;
  34376. /* KARI / KTRI specific */
  34377. byte* optionalUkm;
  34378. word32 optionalUkmSz;
  34379. int ktriOptions; /* KTRI options flags */
  34380. int kariOptions; /* KARI options flags */
  34381. /* KEKRI specific */
  34382. byte* secretKey; /* key, only for kekri RecipientInfo types */
  34383. word32 secretKeySz; /* size of secretKey, bytes */
  34384. byte* secretKeyId; /* key identifier */
  34385. word32 secretKeyIdSz; /* size of key identifier, bytes */
  34386. void* timePtr; /* time_t pointer */
  34387. byte* otherAttrOID; /* OPTIONAL, other attribute OID */
  34388. word32 otherAttrOIDSz; /* size of otherAttrOID, bytes */
  34389. byte* otherAttr; /* OPTIONAL, other attribute, ASN.1 encoded */
  34390. word32 otherAttrSz; /* size of otherAttr, bytes */
  34391. int kekriOptions; /* KEKRI options flags */
  34392. /* PWRI specific */
  34393. char* password; /* password */
  34394. word32 passwordSz; /* password size, bytes */
  34395. byte* salt; /* KDF salt */
  34396. word32 saltSz; /* KDF salt size, bytes */
  34397. int kdfOID; /* KDF OID */
  34398. int hashOID; /* KDF hash algorithm OID */
  34399. int kdfIterations; /* KDF iterations */
  34400. int kekEncryptOID; /* KEK encryption algorithm OID */
  34401. int pwriOptions; /* PWRI options flags */
  34402. /* ORI specific */
  34403. int isOri;
  34404. int oriOptions; /* ORI options flags */
  34405. const char* outFileName;
  34406. } pkcs7AuthEnvelopedVector;
  34407. static int pkcs7authenveloped_run_vectors(byte* rsaCert, word32 rsaCertSz,
  34408. byte* rsaPrivKey, word32 rsaPrivKeySz,
  34409. byte* eccCert, word32 eccCertSz,
  34410. byte* eccPrivKey, word32 eccPrivKeySz)
  34411. {
  34412. int ret = 0, testSz = 0, i;
  34413. int envelopedSz, decodedSz;
  34414. byte *enveloped = NULL;
  34415. byte *decoded = NULL;
  34416. WC_RNG rng;
  34417. PKCS7* pkcs7;
  34418. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  34419. XFILE pkcs7File;
  34420. #endif
  34421. WOLFSSL_SMALL_STACK_STATIC const byte data[] = { /* Hello World */
  34422. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  34423. 0x72,0x6c,0x64
  34424. };
  34425. byte senderNonce[PKCS7_NONCE_SZ + 2];
  34426. #ifdef HAVE_ECC
  34427. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  34428. #if !defined(NO_SHA256) && defined(WOLFSSL_AES_256)
  34429. WOLFSSL_SMALL_STACK_STATIC const byte senderNonceOid[] =
  34430. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  34431. 0x09, 0x05 };
  34432. PKCS7Attrib attribs[] =
  34433. {
  34434. { senderNonceOid, sizeof(senderNonceOid), senderNonce,
  34435. sizeof(senderNonce) }
  34436. };
  34437. #endif
  34438. #endif
  34439. #endif
  34440. #if !defined(NO_AES) && defined(WOLFSSL_AES_256) && defined(HAVE_ECC) && \
  34441. defined(WOLFSSL_SHA512)
  34442. WOLFSSL_SMALL_STACK_STATIC const byte optionalUkm[] = {
  34443. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07
  34444. };
  34445. #endif /* NO_AES */
  34446. #if !defined(NO_AES) && !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  34447. /* encryption key for kekri recipient types */
  34448. WOLFSSL_SMALL_STACK_STATIC const byte secretKey[] = {
  34449. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  34450. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07
  34451. };
  34452. /* encryption key identifier */
  34453. WOLFSSL_SMALL_STACK_STATIC const byte secretKeyId[] = {
  34454. 0x02,0x02,0x03,0x04
  34455. };
  34456. #endif
  34457. #if !defined(NO_PWDBASED) && !defined(NO_AES) && defined(HAVE_AESGCM) && \
  34458. !defined(NO_SHA) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  34459. #ifndef HAVE_FIPS
  34460. WOLFSSL_SMALL_STACK_STATIC const char password[] = "password";
  34461. #else
  34462. WOLFSSL_SMALL_STACK_STATIC const char password[] = "passwordFIPS_MODE";
  34463. #endif
  34464. WOLFSSL_SMALL_STACK_STATIC const byte salt[] = {
  34465. 0x12, 0x34, 0x56, 0x78, 0x78, 0x56, 0x34, 0x12
  34466. };
  34467. #endif
  34468. #define MAX_TESTVECTORS_LEN 20
  34469. #define ADD_PKCS7AUTHENVELOPEDVECTOR(...) { \
  34470. pkcs7AuthEnvelopedVector _this_vector = { __VA_ARGS__ }; \
  34471. if (testSz == MAX_TESTVECTORS_LEN) { \
  34472. ret = -12534; \
  34473. goto out; \
  34474. } \
  34475. XMEMCPY(&testVectors[testSz++], &_this_vector, sizeof _this_vector);\
  34476. }
  34477. pkcs7AuthEnvelopedVector *testVectors = NULL;
  34478. XMEMSET(&rng, 0, sizeof(rng));
  34479. testVectors = (pkcs7AuthEnvelopedVector *)XMALLOC(MAX_TESTVECTORS_LEN * sizeof(*testVectors),
  34480. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34481. if (testVectors == NULL) {
  34482. ret = -12534;
  34483. goto out;
  34484. }
  34485. {
  34486. /* key transport key encryption technique */
  34487. #ifndef NO_RSA
  34488. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  34489. #ifdef WOLFSSL_AES_128
  34490. ADD_PKCS7AUTHENVELOPEDVECTOR(
  34491. data, (word32)sizeof(data), DATA, AES128GCMb, 0, 0, rsaCert, rsaCertSz,
  34492. rsaPrivKey, rsaPrivKeySz, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0,
  34493. NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0,
  34494. 0, 0, "pkcs7authEnvelopedDataAES128GCM.der");
  34495. #endif
  34496. #ifdef WOLFSSL_AES_192
  34497. ADD_PKCS7AUTHENVELOPEDVECTOR(
  34498. data, (word32)sizeof(data), DATA, AES192GCMb, 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, "pkcs7authEnvelopedDataAES192GCM.der");
  34502. #endif
  34503. #ifdef WOLFSSL_AES_256
  34504. ADD_PKCS7AUTHENVELOPEDVECTOR(
  34505. data, (word32)sizeof(data), DATA, AES256GCMb, 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, "pkcs7authEnvelopedDataAES256GCM.der");
  34509. /* test with contentType set to FirmwarePkgData */
  34510. ADD_PKCS7AUTHENVELOPEDVECTOR(
  34511. data, (word32)sizeof(data), FIRMWARE_PKG_DATA, AES256GCMb, 0, 0,
  34512. rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz, NULL, 0, NULL, 0, NULL,
  34513. 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL,
  34514. 0, 0, 0, 0, 0, 0, 0, 0,
  34515. "pkcs7authEnvelopedDataAES256GCM_firmwarePkgData.der");
  34516. /* explicitly using SKID for SubjectKeyIdentifier */
  34517. ADD_PKCS7AUTHENVELOPEDVECTOR(
  34518. data, (word32)sizeof(data), DATA, AES256GCMb, 0, 0, rsaCert, rsaCertSz,
  34519. rsaPrivKey, rsaPrivKeySz, NULL, 0, NULL, 0, NULL, 0, CMS_SKID, 0,
  34520. NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0,
  34521. 0, 0, 0, 0, 0, "pkcs7authEnvelopedDataAES256GCM_SKID.der");
  34522. /* explicitly using IssuerAndSerialNumber for SubjectKeyIdentifier */
  34523. ADD_PKCS7AUTHENVELOPEDVECTOR(
  34524. data, (word32)sizeof(data), DATA, AES256GCMb, 0, 0, rsaCert, rsaCertSz,
  34525. rsaPrivKey, rsaPrivKeySz, NULL, 0, NULL, 0, NULL, 0,
  34526. CMS_ISSUER_AND_SERIAL_NUMBER, 0, NULL, 0, NULL, 0, NULL, NULL, 0,
  34527. NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0, 0,
  34528. "pkcs7authEnvelopedDataAES256GCM_IANDS.der");
  34529. #endif
  34530. #endif /* NO_AES */
  34531. #endif
  34532. /* key agreement key encryption technique*/
  34533. #ifdef HAVE_ECC
  34534. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  34535. #if !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  34536. ADD_PKCS7AUTHENVELOPEDVECTOR(
  34537. data, (word32)sizeof(data), DATA, AES128GCMb, AES128_WRAP,
  34538. dhSinglePass_stdDH_sha1kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  34539. eccPrivKeySz, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0, NULL, 0,
  34540. NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0, 0,
  34541. "pkcs7authEnvelopedDataAES128GCM_ECDH_SHA1KDF.der");
  34542. #endif
  34543. #if !defined(NO_SHA256) && defined(WOLFSSL_AES_256)
  34544. ADD_PKCS7AUTHENVELOPEDVECTOR(
  34545. data, (word32)sizeof(data), DATA, AES256GCMb, AES256_WRAP,
  34546. dhSinglePass_stdDH_sha256kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  34547. eccPrivKeySz, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0, NULL, 0,
  34548. NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0, 0,
  34549. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA256KDF.der");
  34550. /* with authenticated attributes */
  34551. ADD_PKCS7AUTHENVELOPEDVECTOR(
  34552. data, (word32)sizeof(data), DATA, AES256GCMb, AES256_WRAP,
  34553. dhSinglePass_stdDH_sha256kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  34554. eccPrivKeySz, attribs, (sizeof(attribs) / sizeof(PKCS7Attrib)),
  34555. NULL, 0, NULL, 0, 0, 0, NULL, 0,
  34556. NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0,
  34557. 0, 0, 0,
  34558. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA256KDF_authAttribs.der");
  34559. /* with unauthenticated attributes */
  34560. ADD_PKCS7AUTHENVELOPEDVECTOR(
  34561. data, (word32)sizeof(data), DATA, AES256GCMb, AES256_WRAP,
  34562. dhSinglePass_stdDH_sha256kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  34563. eccPrivKeySz, NULL, 0, attribs,
  34564. (sizeof(attribs) / sizeof(PKCS7Attrib)), NULL, 0, 0, 0, NULL, 0,
  34565. NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0,
  34566. 0, 0, 0,
  34567. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA256KDF_unauthAttribs.der");
  34568. /* with authenticated AND unauthenticated attributes */
  34569. ADD_PKCS7AUTHENVELOPEDVECTOR(
  34570. data, (word32)sizeof(data), DATA, AES256GCMb, AES256_WRAP,
  34571. dhSinglePass_stdDH_sha256kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  34572. eccPrivKeySz, attribs, (sizeof(attribs) / sizeof(PKCS7Attrib)),
  34573. attribs, (sizeof(attribs) / sizeof(PKCS7Attrib)), NULL, 0, 0, 0,
  34574. NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0,
  34575. 0, 0, 0, 0, 0, 0,
  34576. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA256KDF_bothAttribs.der");
  34577. /* with authenticated AND unauthenticated attributes AND
  34578. * contentType of FirmwarePkgData */
  34579. ADD_PKCS7AUTHENVELOPEDVECTOR(
  34580. data, (word32)sizeof(data), FIRMWARE_PKG_DATA, AES256GCMb, AES256_WRAP,
  34581. dhSinglePass_stdDH_sha256kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  34582. eccPrivKeySz, attribs, (sizeof(attribs) / sizeof(PKCS7Attrib)),
  34583. attribs, (sizeof(attribs) / sizeof(PKCS7Attrib)), NULL, 0, 0, 0,
  34584. NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0,
  34585. 0, 0, 0, 0, 0, 0,
  34586. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA256KDF_fw_bothAttribs.der");
  34587. #endif /* NO_SHA256 && WOLFSSL_AES_256 */
  34588. #if defined(WOLFSSL_SHA512) && defined(WOLFSSL_AES_256)
  34589. ADD_PKCS7AUTHENVELOPEDVECTOR(
  34590. data, (word32)sizeof(data), DATA, AES256GCMb, AES256_WRAP,
  34591. dhSinglePass_stdDH_sha512kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  34592. eccPrivKeySz, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL,
  34593. NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0, 0,
  34594. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA512KDF.der");
  34595. /* with optional user keying material (ukm) */
  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, (byte *)optionalUkm, sizeof(optionalUkm), 0,
  34600. 0, NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0,
  34601. 0, 0, 0, 0, 0, 0,
  34602. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA512KDF_ukm.der");
  34603. #endif /* WOLFSSL_SHA512 && WOLFSSL_AES_256 */
  34604. #endif /* NO_AES */
  34605. #endif
  34606. /* kekri (KEKRecipientInfo) recipient types */
  34607. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  34608. #if !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  34609. ADD_PKCS7AUTHENVELOPEDVECTOR(
  34610. data, (word32)sizeof(data), DATA, AES128GCMb, AES128_WRAP, 0,
  34611. NULL, 0, NULL, 0, NULL, 0, NULL, 0, NULL, 0, 0, 0,
  34612. (byte *)secretKey, sizeof(secretKey), (byte *)secretKeyId, sizeof(secretKeyId),
  34613. NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0, 0,
  34614. "pkcs7authEnvelopedDataAES128GCM_KEKRI.der");
  34615. #endif
  34616. #endif
  34617. /* pwri (PasswordRecipientInfo) recipient types */
  34618. #if !defined(NO_PWDBASED) && !defined(NO_AES) && defined(HAVE_AESGCM)
  34619. #if !defined(NO_SHA) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  34620. ADD_PKCS7AUTHENVELOPEDVECTOR(
  34621. data, (word32)sizeof(data), DATA, AES128GCMb, 0, 0,
  34622. NULL, 0, NULL, 0, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0,
  34623. NULL, 0, NULL, NULL, 0, NULL, 0, 0, (char *)password,
  34624. (word32)XSTRLEN(password), (byte *)salt, sizeof(salt), PBKDF2_OID, WC_SHA, 5,
  34625. AES128CBCb, 0, 0, 0, "pkcs7authEnvelopedDataAES128GCM_PWRI.der");
  34626. #endif
  34627. #endif
  34628. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  34629. #ifdef WOLFSSL_AES_128
  34630. /* ori (OtherRecipientInfo) recipient types */
  34631. ADD_PKCS7AUTHENVELOPEDVECTOR(
  34632. data, (word32)sizeof(data), DATA, AES128GCMb, 0, 0, NULL, 0, NULL, 0,
  34633. NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL, 0,
  34634. NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 1, 0,
  34635. "pkcs7authEnvelopedDataAES128GCM_ORI.der");
  34636. #endif
  34637. #endif
  34638. }
  34639. #undef MAX_TESTVECTORS_LEN
  34640. #undef ADD_PKCS7AUTHENVELOPEDVECTOR
  34641. enveloped = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34642. decoded = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34643. if ((! enveloped) || (! decoded)) {
  34644. ERROR_OUT(-12210, out);
  34645. }
  34646. /* generate senderNonce */
  34647. {
  34648. #ifndef HAVE_FIPS
  34649. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  34650. #else
  34651. ret = wc_InitRng(&rng);
  34652. #endif
  34653. if (ret != 0) {
  34654. ERROR_OUT(-12211, out);
  34655. }
  34656. senderNonce[0] = 0x04;
  34657. senderNonce[1] = PKCS7_NONCE_SZ;
  34658. ret = wc_RNG_GenerateBlock(&rng, &senderNonce[2], PKCS7_NONCE_SZ);
  34659. if (ret != 0) {
  34660. wc_FreeRng(&rng);
  34661. ERROR_OUT(-12212, out);
  34662. }
  34663. }
  34664. for (i = 0; i < testSz; i++) {
  34665. pkcs7 = wc_PKCS7_New(HEAP_HINT,
  34666. #ifdef WOLFSSL_ASYNC_CRYPT
  34667. INVALID_DEVID /* async PKCS7 is not supported */
  34668. #else
  34669. devId
  34670. #endif
  34671. );
  34672. if (pkcs7 == NULL) {
  34673. ERROR_OUT(-12213, out);
  34674. }
  34675. if (testVectors[i].secretKey != NULL) {
  34676. /* KEKRI recipient type */
  34677. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  34678. if (ret != 0) {
  34679. ERROR_OUT(-12214, out);
  34680. }
  34681. pkcs7->content = (byte*)testVectors[i].content;
  34682. pkcs7->contentSz = testVectors[i].contentSz;
  34683. pkcs7->contentOID = testVectors[i].contentOID;
  34684. pkcs7->encryptOID = testVectors[i].encryptOID;
  34685. pkcs7->ukm = testVectors[i].optionalUkm;
  34686. pkcs7->ukmSz = testVectors[i].optionalUkmSz;
  34687. pkcs7->authAttribs = testVectors[i].authAttribs;
  34688. pkcs7->authAttribsSz = testVectors[i].authAttribsSz;
  34689. pkcs7->unauthAttribs = testVectors[i].unauthAttribs;
  34690. pkcs7->unauthAttribsSz = testVectors[i].unauthAttribsSz;
  34691. ret = wc_PKCS7_AddRecipient_KEKRI(pkcs7, testVectors[i].keyWrapOID,
  34692. testVectors[i].secretKey, testVectors[i].secretKeySz,
  34693. testVectors[i].secretKeyId, testVectors[i].secretKeyIdSz,
  34694. testVectors[i].timePtr, testVectors[i].otherAttrOID,
  34695. testVectors[i].otherAttrOIDSz, testVectors[i].otherAttr,
  34696. testVectors[i].otherAttrSz, testVectors[i].kekriOptions);
  34697. if (ret < 0) {
  34698. wc_PKCS7_Free(pkcs7);
  34699. ERROR_OUT(-12215, out);
  34700. }
  34701. /* set key, for decryption */
  34702. ret = wc_PKCS7_SetKey(pkcs7, testVectors[i].secretKey,
  34703. testVectors[i].secretKeySz);
  34704. if (ret != 0) {
  34705. wc_PKCS7_Free(pkcs7);
  34706. ERROR_OUT(-12216, out);
  34707. }
  34708. } else if (testVectors[i].password != NULL) {
  34709. #if !defined(NO_PWDBASED) && !defined(NO_SHA)
  34710. /* PWRI recipient type */
  34711. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  34712. if (ret != 0) {
  34713. ERROR_OUT(-12217, out);
  34714. }
  34715. pkcs7->content = (byte*)testVectors[i].content;
  34716. pkcs7->contentSz = testVectors[i].contentSz;
  34717. pkcs7->contentOID = testVectors[i].contentOID;
  34718. pkcs7->encryptOID = testVectors[i].encryptOID;
  34719. pkcs7->ukm = testVectors[i].optionalUkm;
  34720. pkcs7->ukmSz = testVectors[i].optionalUkmSz;
  34721. pkcs7->authAttribs = testVectors[i].authAttribs;
  34722. pkcs7->authAttribsSz = testVectors[i].authAttribsSz;
  34723. pkcs7->unauthAttribs = testVectors[i].unauthAttribs;
  34724. pkcs7->unauthAttribsSz = testVectors[i].unauthAttribsSz;
  34725. ret = wc_PKCS7_AddRecipient_PWRI(pkcs7,
  34726. (byte*)testVectors[i].password,
  34727. testVectors[i].passwordSz, testVectors[i].salt,
  34728. testVectors[i].saltSz, testVectors[i].kdfOID,
  34729. testVectors[i].hashOID, testVectors[i].kdfIterations,
  34730. testVectors[i].kekEncryptOID, testVectors[i].pwriOptions);
  34731. if (ret < 0) {
  34732. wc_PKCS7_Free(pkcs7);
  34733. ERROR_OUT(-12218, out);
  34734. }
  34735. /* set password, for decryption */
  34736. ret = wc_PKCS7_SetPassword(pkcs7, (byte*)testVectors[i].password,
  34737. testVectors[i].passwordSz);
  34738. if (ret < 0) {
  34739. wc_PKCS7_Free(pkcs7);
  34740. ERROR_OUT(-12219, out);
  34741. }
  34742. #endif /* ! NO_PWDBASED && ! NO_SHA */
  34743. } else if (testVectors[i].isOri == 1) {
  34744. /* ORI recipient type */
  34745. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  34746. if (ret != 0) {
  34747. ERROR_OUT(-12220, out);
  34748. }
  34749. pkcs7->content = (byte*)testVectors[i].content;
  34750. pkcs7->contentSz = testVectors[i].contentSz;
  34751. pkcs7->contentOID = testVectors[i].contentOID;
  34752. pkcs7->encryptOID = testVectors[i].encryptOID;
  34753. pkcs7->authAttribs = testVectors[i].authAttribs;
  34754. pkcs7->authAttribsSz = testVectors[i].authAttribsSz;
  34755. pkcs7->unauthAttribs = testVectors[i].unauthAttribs;
  34756. pkcs7->unauthAttribsSz = testVectors[i].unauthAttribsSz;
  34757. ret = wc_PKCS7_AddRecipient_ORI(pkcs7, myOriEncryptCb,
  34758. testVectors[i].oriOptions);
  34759. if (ret < 0) {
  34760. wc_PKCS7_Free(pkcs7);
  34761. ERROR_OUT(-12221, out);
  34762. }
  34763. /* set decrypt callback for decryption */
  34764. ret = wc_PKCS7_SetOriDecryptCb(pkcs7, myOriDecryptCb);
  34765. if (ret < 0) {
  34766. wc_PKCS7_Free(pkcs7);
  34767. ERROR_OUT(-12222, out);
  34768. }
  34769. } else {
  34770. /* KTRI or KARI recipient types */
  34771. ret = wc_PKCS7_InitWithCert(pkcs7, testVectors[i].cert,
  34772. (word32)testVectors[i].certSz);
  34773. if (ret != 0) {
  34774. wc_PKCS7_Free(pkcs7);
  34775. ERROR_OUT(-12223, out);
  34776. }
  34777. pkcs7->keyWrapOID = testVectors[i].keyWrapOID;
  34778. pkcs7->keyAgreeOID = testVectors[i].keyAgreeOID;
  34779. pkcs7->privateKey = testVectors[i].privateKey;
  34780. pkcs7->privateKeySz = testVectors[i].privateKeySz;
  34781. pkcs7->content = (byte*)testVectors[i].content;
  34782. pkcs7->contentSz = testVectors[i].contentSz;
  34783. pkcs7->contentOID = testVectors[i].contentOID;
  34784. pkcs7->encryptOID = testVectors[i].encryptOID;
  34785. pkcs7->ukm = testVectors[i].optionalUkm;
  34786. pkcs7->ukmSz = testVectors[i].optionalUkmSz;
  34787. pkcs7->authAttribs = testVectors[i].authAttribs;
  34788. pkcs7->authAttribsSz = testVectors[i].authAttribsSz;
  34789. pkcs7->unauthAttribs = testVectors[i].unauthAttribs;
  34790. pkcs7->unauthAttribsSz = testVectors[i].unauthAttribsSz;
  34791. /* set SubjectIdentifier type for KTRI types */
  34792. if (testVectors[i].ktriOptions & CMS_SKID) {
  34793. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID);
  34794. if (ret != 0) {
  34795. wc_PKCS7_Free(pkcs7);
  34796. ERROR_OUT(-12224, out);
  34797. }
  34798. } else if (testVectors[i].ktriOptions &
  34799. CMS_ISSUER_AND_SERIAL_NUMBER) {
  34800. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7,
  34801. CMS_ISSUER_AND_SERIAL_NUMBER);
  34802. if (ret != 0) {
  34803. wc_PKCS7_Free(pkcs7);
  34804. ERROR_OUT(-12225, out);
  34805. }
  34806. }
  34807. }
  34808. #ifdef ECC_TIMING_RESISTANT
  34809. pkcs7->rng = &rng;
  34810. #endif
  34811. /* encode envelopedData */
  34812. envelopedSz = wc_PKCS7_EncodeAuthEnvelopedData(pkcs7, enveloped,
  34813. PKCS7_BUF_SIZE);
  34814. if (envelopedSz <= 0) {
  34815. wc_PKCS7_Free(pkcs7);
  34816. ERROR_OUT(-12226, out);
  34817. }
  34818. #ifndef NO_PKCS7_STREAM
  34819. { /* test reading byte by byte */
  34820. int z;
  34821. for (z = 0; z < envelopedSz; z++) {
  34822. decodedSz = wc_PKCS7_DecodeAuthEnvelopedData(pkcs7,
  34823. enveloped + z, 1, decoded, PKCS7_BUF_SIZE);
  34824. if (decodedSz <= 0 && decodedSz != WC_PKCS7_WANT_READ_E) {
  34825. printf("unexpected error %d\n", decodedSz);
  34826. ERROR_OUT(-12227, out);
  34827. }
  34828. }
  34829. /* test decode result */
  34830. if (XMEMCMP(decoded, data, sizeof(data)) != 0) {
  34831. printf("stream read compare failed\n");
  34832. wc_PKCS7_Free(pkcs7);
  34833. ERROR_OUT(-12228, out);
  34834. }
  34835. }
  34836. #endif
  34837. /* decode envelopedData */
  34838. decodedSz = wc_PKCS7_DecodeAuthEnvelopedData(pkcs7, enveloped,
  34839. envelopedSz, decoded,
  34840. PKCS7_BUF_SIZE);
  34841. if (decodedSz <= 0) {
  34842. wc_PKCS7_Free(pkcs7);
  34843. ERROR_OUT(-12229, out);
  34844. }
  34845. /* test decode result */
  34846. if (XMEMCMP(decoded, data, sizeof(data)) != 0){
  34847. wc_PKCS7_Free(pkcs7);
  34848. ERROR_OUT(-12230, out);
  34849. }
  34850. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  34851. /* output pkcs7 envelopedData for external testing */
  34852. pkcs7File = XFOPEN(testVectors[i].outFileName, "wb");
  34853. if (!pkcs7File) {
  34854. wc_PKCS7_Free(pkcs7);
  34855. ERROR_OUT(-12231, out);
  34856. }
  34857. ret = (int)XFWRITE(enveloped, 1, envelopedSz, pkcs7File);
  34858. XFCLOSE(pkcs7File);
  34859. if (ret != envelopedSz) {
  34860. wc_PKCS7_Free(pkcs7);
  34861. ERROR_OUT(-12232, out);
  34862. } else {
  34863. /* reset ret to 0 for success */
  34864. ret = 0;
  34865. }
  34866. #endif /* PKCS7_OUTPUT_TEST_BUNDLES */
  34867. wc_PKCS7_Free(pkcs7);
  34868. pkcs7 = NULL;
  34869. }
  34870. wc_FreeRng(&rng);
  34871. (void)eccCert;
  34872. (void)eccCertSz;
  34873. (void)eccPrivKey;
  34874. (void)eccPrivKeySz;
  34875. #if !defined(NO_AES) && !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  34876. (void)secretKey;
  34877. (void)secretKeyId;
  34878. #endif
  34879. #ifdef NO_RSA
  34880. (void)rsaCert;
  34881. (void)rsaCertSz;
  34882. (void)rsaPrivKey;
  34883. (void)rsaPrivKeySz;
  34884. #endif
  34885. out:
  34886. if (testVectors)
  34887. XFREE(testVectors, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34888. if (enveloped)
  34889. XFREE(enveloped, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34890. if (decoded)
  34891. XFREE(decoded, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34892. return ret;
  34893. }
  34894. WOLFSSL_TEST_SUBROUTINE int pkcs7authenveloped_test(void)
  34895. {
  34896. int ret = 0;
  34897. byte* rsaCert = NULL;
  34898. byte* rsaPrivKey = NULL;
  34899. word32 rsaCertSz = 0;
  34900. word32 rsaPrivKeySz = 0;
  34901. byte* eccCert = NULL;
  34902. byte* eccPrivKey = NULL;
  34903. word32 eccCertSz = 0;
  34904. word32 eccPrivKeySz = 0;
  34905. #ifndef NO_RSA
  34906. /* read client RSA cert and key in DER format */
  34907. rsaCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34908. if (rsaCert == NULL)
  34909. return -12300;
  34910. rsaPrivKey = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34911. if (rsaPrivKey == NULL) {
  34912. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34913. return -12301;
  34914. }
  34915. rsaCertSz = FOURK_BUF;
  34916. rsaPrivKeySz = FOURK_BUF;
  34917. #endif /* NO_RSA */
  34918. #ifdef HAVE_ECC
  34919. /* read client ECC cert and key in DER format */
  34920. eccCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34921. if (eccCert == NULL) {
  34922. #ifndef NO_RSA
  34923. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34924. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34925. #endif
  34926. return -12302;
  34927. }
  34928. eccPrivKey =(byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34929. if (eccPrivKey == NULL) {
  34930. #ifndef NO_RSA
  34931. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34932. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34933. #endif
  34934. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34935. return -12303;
  34936. }
  34937. eccCertSz = FOURK_BUF;
  34938. eccPrivKeySz = FOURK_BUF;
  34939. #endif /* HAVE_ECC */
  34940. ret = pkcs7_load_certs_keys(rsaCert, &rsaCertSz, rsaPrivKey,
  34941. &rsaPrivKeySz, NULL, NULL, NULL, NULL,
  34942. NULL, NULL, NULL, NULL, eccCert, &eccCertSz,
  34943. eccPrivKey, &eccPrivKeySz);
  34944. if (ret < 0) {
  34945. #ifndef NO_RSA
  34946. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34947. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34948. #endif
  34949. #ifdef HAVE_ECC
  34950. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34951. XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34952. #endif
  34953. return -12304;
  34954. }
  34955. ret = pkcs7authenveloped_run_vectors(rsaCert, (word32)rsaCertSz,
  34956. rsaPrivKey, (word32)rsaPrivKeySz,
  34957. eccCert, (word32)eccCertSz,
  34958. eccPrivKey, (word32)eccPrivKeySz);
  34959. #ifndef NO_RSA
  34960. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34961. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34962. #endif
  34963. #ifdef HAVE_ECC
  34964. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34965. XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34966. #endif
  34967. return ret;
  34968. }
  34969. #endif /* HAVE_AESGCM || HAVE_AESCCM */
  34970. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  34971. static const byte p7DefKey[] = {
  34972. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  34973. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  34974. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  34975. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  34976. };
  34977. static const byte p7AltKey[] = {
  34978. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  34979. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  34980. };
  34981. static int myCEKwrapFunc(PKCS7* pkcs7, byte* cek, word32 cekSz, byte* keyId,
  34982. word32 keyIdSz, byte* orginKey, word32 orginKeySz,
  34983. byte* out, word32 outSz, int keyWrapAlgo, int type, int direction)
  34984. {
  34985. int ret;
  34986. if (cek == NULL || out == NULL)
  34987. return BAD_FUNC_ARG;
  34988. /* test case sanity checks */
  34989. if (keyIdSz != 1) {
  34990. return -12310;
  34991. }
  34992. if (keyId[0] != 0x00) {
  34993. return -12311;
  34994. }
  34995. if (type != (int)PKCS7_KEKRI) {
  34996. return -12312;
  34997. }
  34998. switch (keyWrapAlgo) {
  34999. case AES256_WRAP:
  35000. ret = wc_AesKeyUnWrap(p7DefKey, sizeof(p7DefKey), cek, cekSz,
  35001. out, outSz, NULL);
  35002. if (ret <= 0)
  35003. return ret;
  35004. break;
  35005. default:
  35006. WOLFSSL_MSG("Unsupported key wrap algorithm in example");
  35007. return BAD_KEYWRAP_ALG_E;
  35008. };
  35009. (void)pkcs7;
  35010. (void)direction;
  35011. (void)orginKey; /* used with KAKRI */
  35012. (void)orginKeySz;
  35013. return ret;
  35014. }
  35015. /* returns key size on success */
  35016. static int getFirmwareKey(PKCS7* pkcs7, byte* key, word32 keySz)
  35017. {
  35018. int ret;
  35019. word32 atrSz;
  35020. byte atr[256];
  35021. /* Additionally can look for fwWrappedFirmwareKey
  35022. * 1.2.840.113529.1.9.16.1.16 */
  35023. const unsigned char fwWrappedFirmwareKey[] = {
  35024. /* 0x06, 0x0B */
  35025. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  35026. 0x01, 0x09, 0x10, 0x02, 0x27
  35027. };
  35028. /* find keyID in fwWrappedFirmwareKey */
  35029. ret = wc_PKCS7_GetAttributeValue(pkcs7, fwWrappedFirmwareKey,
  35030. sizeof(fwWrappedFirmwareKey), NULL, &atrSz);
  35031. if (ret == LENGTH_ONLY_E) {
  35032. XMEMSET(atr, 0, sizeof(atr));
  35033. ret = wc_PKCS7_GetAttributeValue(pkcs7, fwWrappedFirmwareKey,
  35034. sizeof(fwWrappedFirmwareKey), atr, &atrSz);
  35035. /* keyIdRaw[0] OCTET TAG */
  35036. /* keyIdRaw[1] Length */
  35037. if (ret > 0) {
  35038. PKCS7* envPkcs7;
  35039. envPkcs7 = wc_PKCS7_New(NULL, 0);
  35040. if (envPkcs7 == NULL) {
  35041. return MEMORY_E;
  35042. }
  35043. wc_PKCS7_Init(envPkcs7, NULL, 0);
  35044. ret = wc_PKCS7_SetWrapCEKCb(envPkcs7, myCEKwrapFunc);
  35045. if (ret == 0) {
  35046. /* expecting FIRMWARE_PKG_DATA content */
  35047. envPkcs7->contentOID = FIRMWARE_PKG_DATA;
  35048. ret = wc_PKCS7_DecodeEnvelopedData(envPkcs7, atr, atrSz,
  35049. key, keySz);
  35050. if (envPkcs7->contentOID != FIRMWARE_PKG_DATA) {
  35051. /* the contentOID should have been set to the inner
  35052. * FIRMWARE_PKG_DATA content */
  35053. ret = BAD_STATE_E;
  35054. }
  35055. }
  35056. wc_PKCS7_Free(envPkcs7);
  35057. }
  35058. }
  35059. return ret;
  35060. }
  35061. /* create a KEKRI enveloped data
  35062. * return size on success */
  35063. static int envelopedData_encrypt(byte* in, word32 inSz, byte* out,
  35064. word32 outSz)
  35065. {
  35066. int ret;
  35067. PKCS7* pkcs7;
  35068. WOLFSSL_SMALL_STACK_STATIC const byte keyId[] = { 0x00 };
  35069. pkcs7 = wc_PKCS7_New(NULL, INVALID_DEVID);
  35070. if (pkcs7 == NULL)
  35071. return -12330;
  35072. pkcs7->content = in;
  35073. pkcs7->contentSz = inSz;
  35074. pkcs7->contentOID = FIRMWARE_PKG_DATA;
  35075. pkcs7->encryptOID = AES256CBCb;
  35076. pkcs7->ukm = NULL;
  35077. pkcs7->ukmSz = 0;
  35078. /* add recipient (KEKRI type) */
  35079. ret = wc_PKCS7_AddRecipient_KEKRI(pkcs7, AES256_WRAP, (byte*)p7DefKey,
  35080. sizeof(p7DefKey), (byte*)keyId,
  35081. sizeof(keyId), NULL, NULL, 0, NULL, 0, 0);
  35082. if (ret < 0) {
  35083. printf("wc_PKCS7_AddRecipient_KEKRI() failed, ret = %d\n", ret);
  35084. wc_PKCS7_Free(pkcs7);
  35085. return -12331;
  35086. }
  35087. /* encode envelopedData, returns size */
  35088. ret = wc_PKCS7_EncodeEnvelopedData(pkcs7, out, outSz);
  35089. if (ret <= 0) {
  35090. printf("wc_PKCS7_EncodeEnvelopedData() failed, ret = %d\n", ret);
  35091. wc_PKCS7_Free(pkcs7);
  35092. return -12332;
  35093. }
  35094. wc_PKCS7_Free(pkcs7);
  35095. return ret;
  35096. }
  35097. /*
  35098. * keyHint is the KeyID to be set in the fwDecryptKeyID attribute
  35099. * returns size of buffer output on success
  35100. */
  35101. static int generateBundle(byte* out, word32 *outSz, const byte* encryptKey,
  35102. word32 encryptKeySz, byte keyHint, byte* cert, word32 certSz,
  35103. byte* key, word32 keySz)
  35104. {
  35105. int ret, attribNum = 1;
  35106. PKCS7* pkcs7;
  35107. /* KEY ID
  35108. * fwDecryptKeyID OID 1.2.840.113549.1.9.16.2.37
  35109. */
  35110. const unsigned char fwDecryptKeyID[] = {
  35111. 0x06, 0x0B,
  35112. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  35113. 0x01, 0x09, 0x10, 0x02, 0x25
  35114. };
  35115. /* fwWrappedFirmwareKey 1.2.840.113529.1.9.16.1.16 */
  35116. const unsigned char fwWrappedFirmwareKey[] = {
  35117. 0x06, 0x0B, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  35118. 0x01, 0x09, 0x10, 0x02, 0x27
  35119. };
  35120. byte keyID[] = { 0x04, 0x01, 0x00 };
  35121. byte env[256];
  35122. char data[] = "Test of wolfSSL PKCS7 decrypt callback";
  35123. PKCS7Attrib attribs[] =
  35124. {
  35125. { fwDecryptKeyID, sizeof(fwDecryptKeyID), keyID, sizeof(keyID) },
  35126. { fwWrappedFirmwareKey, sizeof(fwWrappedFirmwareKey), env, 0 }
  35127. };
  35128. keyID[2] = keyHint;
  35129. /* If using keyHint 0 then create a bundle with fwWrappedFirmwareKey */
  35130. if (keyHint == 0) {
  35131. ret = envelopedData_encrypt((byte*)p7DefKey, sizeof(p7DefKey), env,
  35132. sizeof(env));
  35133. if (ret <= 0) {
  35134. return ret;
  35135. }
  35136. attribs[1].valueSz = ret;
  35137. attribNum++;
  35138. }
  35139. /* init PKCS7 */
  35140. pkcs7 = wc_PKCS7_New(NULL, INVALID_DEVID);
  35141. if (pkcs7 == NULL)
  35142. return -12340;
  35143. ret = wc_PKCS7_InitWithCert(pkcs7, cert, certSz);
  35144. if (ret != 0) {
  35145. printf("ERROR: wc_PKCS7_InitWithCert() failed, ret = %d\n", ret);
  35146. wc_PKCS7_Free(pkcs7);
  35147. return -12341;
  35148. }
  35149. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID);
  35150. if (ret != 0) {
  35151. wc_PKCS7_Free(pkcs7);
  35152. return -12342;
  35153. }
  35154. /* encode Signed Encrypted FirmwarePkgData */
  35155. if (encryptKeySz == 16) {
  35156. ret = wc_PKCS7_EncodeSignedEncryptedFPD(pkcs7, (byte*)encryptKey,
  35157. encryptKeySz, key, keySz, AES128CBCb, RSAk, SHA256h,
  35158. (byte*)data, sizeof(data), NULL, 0,
  35159. attribs, attribNum, out, *outSz);
  35160. }
  35161. else {
  35162. ret = wc_PKCS7_EncodeSignedEncryptedFPD(pkcs7, (byte*)encryptKey,
  35163. encryptKeySz, key, keySz, AES256CBCb, RSAk, SHA256h,
  35164. (byte*)data, sizeof(data), NULL, 0,
  35165. attribs, attribNum, out, *outSz);
  35166. }
  35167. if (ret <= 0) {
  35168. printf("ERROR: wc_PKCS7_EncodeSignedEncryptedFPD() failed, "
  35169. "ret = %d\n", ret);
  35170. wc_PKCS7_Free(pkcs7);
  35171. return -12343;
  35172. } else {
  35173. *outSz = ret;
  35174. }
  35175. wc_PKCS7_Free(pkcs7);
  35176. return ret;
  35177. }
  35178. /* test verification and decryption of PKCS7 bundle
  35179. * return 0 on success
  35180. */
  35181. static int verifyBundle(byte* derBuf, word32 derSz, int keyHint)
  35182. {
  35183. int ret = 0;
  35184. int usrCtx = 1; /* test value to pass as user context to callback */
  35185. PKCS7* pkcs7 = NULL;
  35186. byte* sid = NULL;
  35187. word32 sidSz;
  35188. byte key[256];
  35189. word32 keySz = sizeof(key);
  35190. byte *decoded = NULL;
  35191. int decodedSz = FOURK_BUF/2;
  35192. WOLFSSL_SMALL_STACK_STATIC const byte expectedSid[] = {
  35193. #ifdef USE_CERT_BUFFERS_1024
  35194. 0x81, 0x69, 0x0f, 0xf8, 0xdf, 0xdd, 0xcf, 0x34,
  35195. 0x29, 0xd5, 0x67, 0x75, 0x71, 0x85, 0xc7, 0x75,
  35196. 0x10, 0x69, 0x59, 0xec,
  35197. #else
  35198. 0x33, 0xD8, 0x45, 0x66, 0xD7, 0x68, 0x87, 0x18,
  35199. 0x7E, 0x54, 0x0D, 0x70, 0x27, 0x91, 0xC7, 0x26,
  35200. 0xD7, 0x85, 0x65, 0xC0
  35201. #endif
  35202. };
  35203. decoded = (byte *)XMALLOC(decodedSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35204. if (decoded == NULL) {
  35205. ret = MEMORY_E;
  35206. goto out;
  35207. }
  35208. pkcs7 = wc_PKCS7_New(HEAP_HINT, INVALID_DEVID);
  35209. if (pkcs7 == NULL) {
  35210. ret = MEMORY_E;
  35211. goto out;
  35212. }
  35213. /* Test verify */
  35214. ret = wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID);
  35215. if (ret != 0)
  35216. goto out;
  35217. ret = wc_PKCS7_InitWithCert(pkcs7, NULL, 0);
  35218. if (ret != 0)
  35219. goto out;
  35220. ret = wc_PKCS7_VerifySignedData(pkcs7, derBuf, derSz);
  35221. if (ret != 0)
  35222. goto out;
  35223. /* Get size of SID and print it out */
  35224. ret = wc_PKCS7_GetSignerSID(pkcs7, NULL, &sidSz);
  35225. if (ret != LENGTH_ONLY_E)
  35226. goto out;
  35227. sid = (byte*)XMALLOC(sidSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35228. if (sid == NULL) {
  35229. ret = MEMORY_E;
  35230. goto out;
  35231. }
  35232. ret = wc_PKCS7_GetSignerSID(pkcs7, sid, &sidSz);
  35233. if (ret != 0)
  35234. goto out;
  35235. ret = XMEMCMP(sid, expectedSid, sidSz);
  35236. if (ret != 0) {
  35237. ret = PKCS7_NO_SIGNER_E; /* close enough */
  35238. goto out;
  35239. }
  35240. /* get expected fwWrappedFirmwareKey */
  35241. if (keyHint == 0) {
  35242. ret = getFirmwareKey(pkcs7, key, keySz);
  35243. if (ret < 0)
  35244. goto out;
  35245. pkcs7->encryptionKey = key;
  35246. pkcs7->encryptionKeySz = ret;
  35247. }
  35248. else {
  35249. decodedSz = PKCS7_BUF_SIZE;
  35250. ret = wc_PKCS7_SetDecodeEncryptedCb(pkcs7, myDecryptionFunc);
  35251. if (ret != 0)
  35252. goto out;
  35253. ret = wc_PKCS7_SetDecodeEncryptedCtx(pkcs7, (void*)&usrCtx);
  35254. if (ret != 0)
  35255. goto out;
  35256. }
  35257. decodedSz = wc_PKCS7_DecodeEncryptedData(pkcs7, pkcs7->content,
  35258. pkcs7->contentSz, decoded, decodedSz);
  35259. if (decodedSz < 0) {
  35260. ret = decodedSz;
  35261. goto out;
  35262. }
  35263. ret = 0;
  35264. out:
  35265. if (decoded)
  35266. XFREE(decoded, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35267. if (pkcs7)
  35268. wc_PKCS7_Free(pkcs7);
  35269. if (sid)
  35270. XFREE(sid, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35271. return ret;
  35272. }
  35273. WOLFSSL_TEST_SUBROUTINE int pkcs7callback_test(byte* cert, word32 certSz, byte* key, word32 keySz)
  35274. {
  35275. int ret = 0;
  35276. word32 derSz;
  35277. byte *derBuf = (byte *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35278. if (! derBuf)
  35279. ERROR_OUT(-12360, out);
  35280. /* Doing default generation and verify */
  35281. derSz = FOURK_BUF;
  35282. ret = generateBundle(derBuf, &derSz, p7DefKey, sizeof(p7DefKey), 0, cert,
  35283. certSz, key, keySz);
  35284. if (ret <= 0) {
  35285. ERROR_OUT(-12361, out);
  35286. }
  35287. ret = verifyBundle(derBuf, derSz, 0);
  35288. if (ret != 0) {
  35289. ERROR_OUT(-12362, out);
  35290. }
  35291. /* test choosing other key with keyID */
  35292. derSz = FOURK_BUF;
  35293. ret = generateBundle(derBuf, &derSz, p7AltKey, sizeof(p7AltKey), 1,
  35294. cert, certSz, key, keySz);
  35295. if (ret <= 0) {
  35296. ERROR_OUT(-12363, out);
  35297. }
  35298. ret = verifyBundle(derBuf, derSz, 1);
  35299. if (ret != 0) {
  35300. ERROR_OUT(-12364, out);
  35301. }
  35302. /* test fail case with wrong keyID */
  35303. derSz = FOURK_BUF;
  35304. ret = generateBundle(derBuf, &derSz, p7DefKey, sizeof(p7DefKey), 1,
  35305. cert, certSz, key, keySz);
  35306. if (ret <= 0) {
  35307. ERROR_OUT(-12365, out);
  35308. }
  35309. ret = verifyBundle(derBuf, derSz, 1);
  35310. if (ret == 0) {
  35311. ERROR_OUT(-12366, out);
  35312. }
  35313. ret = 0;
  35314. out:
  35315. if (derBuf)
  35316. XFREE(derBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35317. return ret;
  35318. }
  35319. #endif /* !NO_AES && HAVE_AES_CBC */
  35320. #ifndef NO_PKCS7_ENCRYPTED_DATA
  35321. typedef struct {
  35322. const byte* content;
  35323. word32 contentSz;
  35324. int contentOID;
  35325. int encryptOID;
  35326. byte* encryptionKey;
  35327. word32 encryptionKeySz;
  35328. PKCS7Attrib* attribs;
  35329. word32 attribsSz;
  35330. const char* outFileName;
  35331. } pkcs7EncryptedVector;
  35332. WOLFSSL_TEST_SUBROUTINE int pkcs7encrypted_test(void)
  35333. {
  35334. int ret = 0;
  35335. int i, testSz;
  35336. int encryptedSz, decodedSz, attribIdx;
  35337. PKCS7* pkcs7;
  35338. byte *encrypted;
  35339. byte *decoded;
  35340. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  35341. XFILE pkcs7File;
  35342. #endif
  35343. PKCS7Attrib* expectedAttrib;
  35344. PKCS7DecodedAttrib* decodedAttrib;
  35345. WOLFSSL_SMALL_STACK_STATIC const byte data[] = { /* Hello World */
  35346. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  35347. 0x72,0x6c,0x64
  35348. };
  35349. #ifndef NO_DES3
  35350. byte desKey[] = {
  35351. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
  35352. };
  35353. byte des3Key[] = {
  35354. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  35355. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  35356. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  35357. };
  35358. #endif
  35359. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  35360. #ifdef WOLFSSL_AES_128
  35361. byte aes128Key[] = {
  35362. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  35363. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  35364. };
  35365. #endif
  35366. #ifdef WOLFSSL_AES_192
  35367. byte aes192Key[] = {
  35368. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  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_256
  35374. byte aes256Key[] = {
  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. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  35379. };
  35380. #endif
  35381. #ifdef WOLFSSL_AES_256
  35382. /* Attribute example from RFC 4134, Section 7.2
  35383. * OID = 1.2.5555
  35384. * OCTET STRING = 'This is a test General ASN Attribute, number 1.' */
  35385. static byte genAttrOid[] = { 0x06, 0x03, 0x2a, 0xab, 0x33 };
  35386. static byte genAttr[] = { 0x04, 47,
  35387. 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20,
  35388. 0x61, 0x20, 0x74, 0x65, 0x73, 0x74, 0x20, 0x47,
  35389. 0x65, 0x6e, 0x65, 0x72, 0x61, 0x6c, 0x20, 0x41,
  35390. 0x53, 0x4e, 0x20, 0x41, 0x74, 0x74, 0x72, 0x69,
  35391. 0x62, 0x75, 0x74, 0x65, 0x2c, 0x20, 0x6e, 0x75,
  35392. 0x6d, 0x62, 0x65, 0x72, 0x20, 0x31, 0x2e };
  35393. static byte genAttrOid2[] = { 0x06, 0x03, 0x2a, 0xab, 0x34 };
  35394. static byte genAttr2[] = { 0x04, 47,
  35395. 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20,
  35396. 0x61, 0x20, 0x74, 0x65, 0x73, 0x74, 0x20, 0x47,
  35397. 0x65, 0x6e, 0x65, 0x72, 0x61, 0x6c, 0x20, 0x41,
  35398. 0x53, 0x4e, 0x20, 0x41, 0x74, 0x74, 0x72, 0x69,
  35399. 0x62, 0x75, 0x74, 0x65, 0x2c, 0x20, 0x6e, 0x75,
  35400. 0x6d, 0x62, 0x65, 0x72, 0x20, 0x32, 0x2e };
  35401. PKCS7Attrib attribs[] =
  35402. {
  35403. { genAttrOid, sizeof(genAttrOid), genAttr, sizeof(genAttr) }
  35404. };
  35405. PKCS7Attrib multiAttribs[] =
  35406. {
  35407. { genAttrOid, sizeof(genAttrOid), genAttr, sizeof(genAttr) },
  35408. { genAttrOid2, sizeof(genAttrOid2), genAttr2, sizeof(genAttr2) }
  35409. };
  35410. #endif
  35411. #endif /* NO_AES */
  35412. const pkcs7EncryptedVector testVectors[] =
  35413. {
  35414. #ifndef NO_DES3
  35415. {data, (word32)sizeof(data), DATA, DES3b, des3Key, sizeof(des3Key),
  35416. NULL, 0, "pkcs7encryptedDataDES3.der"},
  35417. {data, (word32)sizeof(data), DATA, DESb, desKey, sizeof(desKey),
  35418. NULL, 0, "pkcs7encryptedDataDES.der"},
  35419. #endif /* NO_DES3 */
  35420. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  35421. #ifdef WOLFSSL_AES_128
  35422. {data, (word32)sizeof(data), DATA, AES128CBCb, aes128Key,
  35423. sizeof(aes128Key), NULL, 0, "pkcs7encryptedDataAES128CBC.der"},
  35424. #endif
  35425. #ifdef WOLFSSL_AES_192
  35426. {data, (word32)sizeof(data), DATA, AES192CBCb, aes192Key,
  35427. sizeof(aes192Key), NULL, 0, "pkcs7encryptedDataAES192CBC.der"},
  35428. #endif
  35429. #ifdef WOLFSSL_AES_256
  35430. {data, (word32)sizeof(data), DATA, AES256CBCb, aes256Key,
  35431. sizeof(aes256Key), NULL, 0, "pkcs7encryptedDataAES256CBC.der"},
  35432. /* test with optional unprotected attributes */
  35433. {data, (word32)sizeof(data), DATA, AES256CBCb, aes256Key,
  35434. sizeof(aes256Key), attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  35435. "pkcs7encryptedDataAES256CBC_attribs.der"},
  35436. /* test with multiple optional unprotected attributes */
  35437. {data, (word32)sizeof(data), DATA, AES256CBCb, aes256Key,
  35438. sizeof(aes256Key), multiAttribs,
  35439. (sizeof(multiAttribs)/sizeof(PKCS7Attrib)),
  35440. "pkcs7encryptedDataAES256CBC_multi_attribs.der"},
  35441. /* test with contentType set to FirmwarePkgData */
  35442. {data, (word32)sizeof(data), FIRMWARE_PKG_DATA, AES256CBCb, aes256Key,
  35443. sizeof(aes256Key), NULL, 0,
  35444. "pkcs7encryptedDataAES256CBC_firmwarePkgData.der"},
  35445. #endif
  35446. #endif /* !NO_AES && HAVE_AES_CBC */
  35447. };
  35448. encrypted = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35449. decoded = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35450. if ((! encrypted) || (! decoded)) {
  35451. ERROR_OUT(MEMORY_E, out);
  35452. }
  35453. testSz = sizeof(testVectors) / sizeof(pkcs7EncryptedVector);
  35454. for (i = 0; i < testSz; i++) {
  35455. pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
  35456. if (pkcs7 == NULL) {
  35457. ERROR_OUT(-12400, out);
  35458. }
  35459. pkcs7->content = (byte*)testVectors[i].content;
  35460. pkcs7->contentSz = testVectors[i].contentSz;
  35461. pkcs7->contentOID = testVectors[i].contentOID;
  35462. pkcs7->encryptOID = testVectors[i].encryptOID;
  35463. pkcs7->encryptionKey = testVectors[i].encryptionKey;
  35464. pkcs7->encryptionKeySz = testVectors[i].encryptionKeySz;
  35465. pkcs7->unprotectedAttribs = testVectors[i].attribs;
  35466. pkcs7->unprotectedAttribsSz = testVectors[i].attribsSz;
  35467. /* encode encryptedData */
  35468. encryptedSz = wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  35469. PKCS7_BUF_SIZE);
  35470. if (encryptedSz <= 0) {
  35471. wc_PKCS7_Free(pkcs7);
  35472. ERROR_OUT(-12401, out);
  35473. }
  35474. /* decode encryptedData */
  35475. #ifndef NO_PKCS7_STREAM
  35476. { /* test reading byte by byte */
  35477. int z;
  35478. for (z = 0; z < encryptedSz; z++) {
  35479. decodedSz = wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted + z, 1,
  35480. decoded, PKCS7_BUF_SIZE);
  35481. if (decodedSz <= 0 && decodedSz != WC_PKCS7_WANT_READ_E) {
  35482. printf("unexpected error %d\n", decodedSz);
  35483. ERROR_OUT(-12402, out);
  35484. }
  35485. }
  35486. /* test decode result */
  35487. if (XMEMCMP(decoded, data, sizeof(data)) != 0) {
  35488. printf("stream read failed\n");
  35489. wc_PKCS7_Free(pkcs7);
  35490. ERROR_OUT(-12403, out);
  35491. }
  35492. }
  35493. #endif
  35494. decodedSz = wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  35495. decoded, PKCS7_BUF_SIZE);
  35496. if (decodedSz <= 0){
  35497. wc_PKCS7_Free(pkcs7);
  35498. ERROR_OUT(-12404, out);
  35499. }
  35500. /* test decode result */
  35501. if (XMEMCMP(decoded, data, sizeof(data)) != 0) {
  35502. wc_PKCS7_Free(pkcs7);
  35503. ERROR_OUT(-12405, out);
  35504. }
  35505. /* verify decoded unprotected attributes */
  35506. if (pkcs7->decodedAttrib != NULL) {
  35507. decodedAttrib = pkcs7->decodedAttrib;
  35508. attribIdx = 1;
  35509. while (decodedAttrib != NULL) {
  35510. /* expected attribute, stored list is reversed */
  35511. expectedAttrib = &(pkcs7->unprotectedAttribs
  35512. [pkcs7->unprotectedAttribsSz - attribIdx]);
  35513. /* verify oid */
  35514. if (XMEMCMP(decodedAttrib->oid, expectedAttrib->oid,
  35515. decodedAttrib->oidSz) != 0) {
  35516. wc_PKCS7_Free(pkcs7);
  35517. ERROR_OUT(-12406, out);
  35518. }
  35519. /* verify value */
  35520. if (XMEMCMP(decodedAttrib->value, expectedAttrib->value,
  35521. decodedAttrib->valueSz) != 0) {
  35522. wc_PKCS7_Free(pkcs7);
  35523. ERROR_OUT(-12407, out);
  35524. }
  35525. decodedAttrib = decodedAttrib->next;
  35526. attribIdx++;
  35527. }
  35528. }
  35529. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  35530. /* output pkcs7 envelopedData for external testing */
  35531. pkcs7File = XFOPEN(testVectors[i].outFileName, "wb");
  35532. if (!pkcs7File) {
  35533. wc_PKCS7_Free(pkcs7);
  35534. ERROR_OUT(-12408, out);
  35535. }
  35536. ret = (int)XFWRITE(encrypted, encryptedSz, 1, pkcs7File);
  35537. XFCLOSE(pkcs7File);
  35538. if (ret > 0)
  35539. ret = 0;
  35540. #endif
  35541. wc_PKCS7_Free(pkcs7);
  35542. }
  35543. out:
  35544. if (encrypted)
  35545. XFREE(encrypted, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35546. if (decoded)
  35547. XFREE(decoded, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35548. return ret;
  35549. }
  35550. #endif /* NO_PKCS7_ENCRYPTED_DATA */
  35551. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  35552. typedef struct {
  35553. const byte* content;
  35554. word32 contentSz;
  35555. int contentOID;
  35556. const char* outFileName;
  35557. } pkcs7CompressedVector;
  35558. WOLFSSL_TEST_SUBROUTINE int pkcs7compressed_test(void)
  35559. {
  35560. int ret = 0;
  35561. int i, testSz;
  35562. int compressedSz, decodedSz;
  35563. PKCS7* pkcs7;
  35564. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  35565. byte *compressed;
  35566. byte *decoded;
  35567. #else
  35568. byte compressed[PKCS7_BUF_SIZE];
  35569. byte decoded[PKCS7_BUF_SIZE];
  35570. #endif
  35571. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  35572. XFILE pkcs7File;
  35573. #endif
  35574. WOLFSSL_SMALL_STACK_STATIC const byte data[] = { /* Hello World */
  35575. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  35576. 0x72,0x6c,0x64
  35577. };
  35578. const pkcs7CompressedVector testVectors[] =
  35579. {
  35580. {data, (word32)sizeof(data), DATA,
  35581. "pkcs7compressedData_data_zlib.der"},
  35582. {data, (word32)sizeof(data), FIRMWARE_PKG_DATA,
  35583. "pkcs7compressedData_firmwarePkgData_zlib.der"},
  35584. };
  35585. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  35586. compressed = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35587. decoded = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35588. if ((! compressed) || (! decoded)) {
  35589. ERROR_OUT(MEMORY_E, out);
  35590. }
  35591. #endif
  35592. testSz = sizeof(testVectors) / sizeof(pkcs7CompressedVector);
  35593. for (i = 0; i < testSz; i++) {
  35594. pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
  35595. if (pkcs7 == NULL) {
  35596. ERROR_OUT(-12500, out);
  35597. }
  35598. pkcs7->content = (byte*)testVectors[i].content;
  35599. pkcs7->contentSz = testVectors[i].contentSz;
  35600. pkcs7->contentOID = testVectors[i].contentOID;
  35601. /* encode compressedData */
  35602. compressedSz = wc_PKCS7_EncodeCompressedData(pkcs7, compressed,
  35603. PKCS7_BUF_SIZE);
  35604. if (compressedSz <= 0) {
  35605. wc_PKCS7_Free(pkcs7);
  35606. ERROR_OUT(-12501, out);
  35607. }
  35608. /* decode compressedData */
  35609. decodedSz = wc_PKCS7_DecodeCompressedData(pkcs7, compressed,
  35610. compressedSz, decoded,
  35611. PKCS7_BUF_SIZE);
  35612. if (decodedSz <= 0){
  35613. wc_PKCS7_Free(pkcs7);
  35614. ERROR_OUT(-12502, out);
  35615. }
  35616. /* test decode result */
  35617. if (XMEMCMP(decoded, testVectors[i].content,
  35618. testVectors[i].contentSz) != 0) {
  35619. wc_PKCS7_Free(pkcs7);
  35620. ERROR_OUT(-12503, out);
  35621. }
  35622. /* make sure content type is the same */
  35623. if (testVectors[i].contentOID != pkcs7->contentOID) {
  35624. ERROR_OUT(-12504, out);
  35625. }
  35626. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  35627. /* output pkcs7 compressedData for external testing */
  35628. pkcs7File = XFOPEN(testVectors[i].outFileName, "wb");
  35629. if (!pkcs7File) {
  35630. wc_PKCS7_Free(pkcs7);
  35631. ERROR_OUT(-12505, out);
  35632. }
  35633. ret = (int)XFWRITE(compressed, compressedSz, 1, pkcs7File);
  35634. XFCLOSE(pkcs7File);
  35635. if (ret > 0)
  35636. ret = 0;
  35637. #endif
  35638. wc_PKCS7_Free(pkcs7);
  35639. }
  35640. out:
  35641. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  35642. if (compressed)
  35643. XFREE(compressed, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35644. if (decoded)
  35645. XFREE(decoded, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35646. #endif
  35647. return ret;
  35648. } /* pkcs7compressed_test() */
  35649. #undef PKCS7_BUF_SIZE
  35650. #endif /* HAVE_LIBZ */
  35651. typedef struct {
  35652. const byte* content;
  35653. word32 contentSz;
  35654. int hashOID;
  35655. int signOID;
  35656. byte* privateKey;
  35657. word32 privateKeySz;
  35658. byte* cert;
  35659. size_t certSz;
  35660. byte* caCert;
  35661. size_t caCertSz;
  35662. PKCS7Attrib* signedAttribs;
  35663. word32 signedAttribsSz;
  35664. const char* outFileName;
  35665. int contentOID;
  35666. byte* contentType;
  35667. word32 contentTypeSz;
  35668. int sidType;
  35669. int encryptOID; /* for single-shot encrypt alg OID */
  35670. int encCompFlag; /* for single-shot. 1 = enc, 2 = comp, 3 = both*/
  35671. byte* encryptKey; /* for single-shot, encryptedData */
  35672. word32 encryptKeySz; /* for single-shot, encryptedData */
  35673. PKCS7Attrib* unprotectedAttribs; /* for single-shot, encryptedData */
  35674. word32 unprotectedAttribsSz; /* for single-shot, encryptedData */
  35675. word16 detachedSignature; /* generate detached signature (0:1) */
  35676. } pkcs7SignedVector;
  35677. static int pkcs7signed_run_vectors(
  35678. byte* rsaClientCertBuf, word32 rsaClientCertBufSz,
  35679. byte* rsaClientPrivKeyBuf, word32 rsaClientPrivKeyBufSz,
  35680. byte* rsaServerCertBuf, word32 rsaServerCertBufSz,
  35681. byte* rsaServerPrivKeyBuf, word32 rsaServerPrivKeyBufSz,
  35682. byte* rsaCaCertBuf, word32 rsaCaCertBufSz,
  35683. byte* rsaCaPrivKeyBuf, word32 rsaCaPrivKeyBufSz,
  35684. byte* eccClientCertBuf, word32 eccClientCertBufSz,
  35685. byte* eccClientPrivKeyBuf, word32 eccClientPrivKeyBufSz)
  35686. {
  35687. int ret, testSz = 0, i;
  35688. int encodedSz;
  35689. byte* out = NULL;
  35690. word32 outSz;
  35691. WC_RNG rng;
  35692. PKCS7* pkcs7 = NULL;
  35693. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  35694. XFILE file;
  35695. #endif
  35696. WOLFSSL_SMALL_STACK_STATIC const byte data[] = { /* Hello World */
  35697. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  35698. 0x72,0x6c,0x64
  35699. };
  35700. static byte transIdOid[] =
  35701. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  35702. 0x09, 0x07 };
  35703. static byte messageTypeOid[] =
  35704. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  35705. 0x09, 0x02 };
  35706. static byte senderNonceOid[] =
  35707. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  35708. 0x09, 0x05 };
  35709. #ifndef NO_SHA
  35710. static byte transId[(WC_SHA_DIGEST_SIZE + 1) * 2 + 1];
  35711. #else
  35712. static byte transId[(WC_SHA256_DIGEST_SIZE + 1) * 2 + 1];
  35713. #endif
  35714. static byte messageType[] = { 0x13, 2, '1', '9' };
  35715. static byte senderNonce[PKCS7_NONCE_SZ + 2];
  35716. static PKCS7Attrib attribs[] =
  35717. {
  35718. { transIdOid, sizeof(transIdOid), transId,
  35719. sizeof(transId) - 1 }, /* take off the null */
  35720. { messageTypeOid, sizeof(messageTypeOid), messageType,
  35721. sizeof(messageType) },
  35722. { senderNonceOid, sizeof(senderNonceOid), senderNonce,
  35723. sizeof(senderNonce) }
  35724. };
  35725. /* for testing custom contentType, FirmwarePkgData */
  35726. static byte customContentType[] = { 0x06, 0x0B, 0x2A, 0x86,
  35727. 0x48, 0x86, 0xF7, 0x0D,
  35728. 0x01, 0x09, 0x10, 0x01, 0x10 };
  35729. #define MAX_TESTVECTORS_LEN 20
  35730. #define ADD_PKCS7SIGNEDVECTOR(...) { \
  35731. pkcs7SignedVector _this_vector = { __VA_ARGS__ }; \
  35732. if (testSz == MAX_TESTVECTORS_LEN) { \
  35733. ret = -12534; \
  35734. goto out; \
  35735. } \
  35736. XMEMCPY(&testVectors[testSz++], &_this_vector, sizeof _this_vector);\
  35737. }
  35738. pkcs7SignedVector *testVectors = NULL;
  35739. XMEMSET(&rng, 0, sizeof(rng));
  35740. testVectors = (pkcs7SignedVector *)XMALLOC(MAX_TESTVECTORS_LEN * sizeof(*testVectors),
  35741. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35742. if (testVectors == NULL) {
  35743. ret = -12567;
  35744. goto out;
  35745. }
  35746. {
  35747. #ifndef NO_RSA
  35748. #ifndef NO_SHA
  35749. /* RSA with SHA */
  35750. ADD_PKCS7SIGNEDVECTOR(
  35751. data, (word32)sizeof(data), SHAh, RSAk, rsaClientPrivKeyBuf,
  35752. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  35753. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  35754. "pkcs7signedData_RSA_SHA.der", 0, NULL, 0, 0, 0, 0, NULL, 0, NULL,
  35755. 0, 0);
  35756. /* RSA with SHA, no signed attributes */
  35757. ADD_PKCS7SIGNEDVECTOR(
  35758. data, (word32)sizeof(data), SHAh, RSAk, rsaClientPrivKeyBuf,
  35759. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz,
  35760. NULL, 0, NULL, 0,
  35761. "pkcs7signedData_RSA_SHA_noattr.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  35762. NULL, 0, 0);
  35763. #endif
  35764. #ifdef WOLFSSL_SHA224
  35765. /* RSA with SHA224 */
  35766. ADD_PKCS7SIGNEDVECTOR(
  35767. data, (word32)sizeof(data), SHA224h, RSAk, rsaClientPrivKeyBuf,
  35768. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  35769. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  35770. "pkcs7signedData_RSA_SHA224.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  35771. NULL, 0, 0);
  35772. #endif
  35773. #ifndef NO_SHA256
  35774. /* RSA with SHA256 */
  35775. ADD_PKCS7SIGNEDVECTOR(
  35776. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  35777. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  35778. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  35779. "pkcs7signedData_RSA_SHA256.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  35780. NULL, 0, 0);
  35781. /* RSA with SHA256, detached signature */
  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_detachedSig.der", 0, NULL, 0, 0, 0, 0,
  35787. NULL, 0, NULL, 0, 1);
  35788. /* RSA with SHA256 and SubjectKeyIdentifier in SignerIdentifier */
  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_SKID.der", 0, NULL, 0, CMS_SKID, 0, 0,
  35794. NULL, 0, NULL, 0, 0);
  35795. /* RSA with SHA256 and custom contentType */
  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_custom_contentType.der", 0,
  35801. customContentType, sizeof(customContentType), 0, 0, 0, NULL, 0,
  35802. NULL, 0, 0);
  35803. /* RSA with SHA256 and FirmwarePkgData contentType */
  35804. ADD_PKCS7SIGNEDVECTOR(
  35805. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  35806. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  35807. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  35808. "pkcs7signedData_RSA_SHA256_firmwarePkgData.der",
  35809. FIRMWARE_PKG_DATA, NULL, 0, 0, 0, 0, NULL, 0, NULL, 0, 0);
  35810. /* RSA with SHA256 using server cert and ca cert */
  35811. ADD_PKCS7SIGNEDVECTOR(
  35812. data, (word32)sizeof(data), SHA256h, RSAk, rsaServerPrivKeyBuf,
  35813. rsaServerPrivKeyBufSz, rsaServerCertBuf, rsaServerCertBufSz,
  35814. rsaCaCertBuf, rsaCaCertBufSz,
  35815. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  35816. "pkcs7signedData_RSA_SHA256_with_ca_cert.der", 0, NULL, 0, 0, 0, 0,
  35817. NULL, 0, NULL, 0, 0);
  35818. #endif
  35819. #if defined(WOLFSSL_SHA384)
  35820. /* RSA with SHA384 */
  35821. ADD_PKCS7SIGNEDVECTOR(
  35822. data, (word32)sizeof(data), SHA384h, RSAk, rsaClientPrivKeyBuf,
  35823. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  35824. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  35825. "pkcs7signedData_RSA_SHA384.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  35826. NULL, 0, 0);
  35827. #endif
  35828. #if defined(WOLFSSL_SHA512)
  35829. /* RSA with SHA512 */
  35830. ADD_PKCS7SIGNEDVECTOR(
  35831. data, (word32)sizeof(data), SHA512h, RSAk, rsaClientPrivKeyBuf,
  35832. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  35833. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  35834. "pkcs7signedData_RSA_SHA512.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  35835. NULL, 0, 0);
  35836. #endif
  35837. #endif /* NO_RSA */
  35838. #ifdef HAVE_ECC
  35839. #ifndef NO_SHA
  35840. /* ECDSA with SHA */
  35841. ADD_PKCS7SIGNEDVECTOR(
  35842. data, (word32)sizeof(data), SHAh, ECDSAk, eccClientPrivKeyBuf,
  35843. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  35844. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  35845. "pkcs7signedData_ECDSA_SHA.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  35846. NULL, 0, 0);
  35847. /* ECDSA with SHA, no signed attributes */
  35848. ADD_PKCS7SIGNEDVECTOR(
  35849. data, (word32)sizeof(data), SHAh, ECDSAk, eccClientPrivKeyBuf,
  35850. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz,
  35851. NULL, 0, NULL, 0,
  35852. "pkcs7signedData_ECDSA_SHA_noattr.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  35853. NULL, 0, 0);
  35854. #endif
  35855. #ifdef WOLFSSL_SHA224
  35856. /* ECDSA with SHA224 */
  35857. ADD_PKCS7SIGNEDVECTOR(
  35858. data, (word32)sizeof(data), SHA224h, ECDSAk, eccClientPrivKeyBuf,
  35859. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  35860. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  35861. "pkcs7signedData_ECDSA_SHA224.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  35862. NULL, 0, 0);
  35863. #endif
  35864. #ifndef NO_SHA256
  35865. /* ECDSA with SHA256 */
  35866. ADD_PKCS7SIGNEDVECTOR(
  35867. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  35868. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  35869. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  35870. "pkcs7signedData_ECDSA_SHA256.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  35871. NULL, 0, 0);
  35872. /* ECDSA with SHA256 and SubjectKeyIdentifier in SigherIdentifier */
  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_SKID.der", 0, NULL, 0, CMS_SKID, 0, 0,
  35878. NULL, 0, NULL, 0, 0);
  35879. /* ECDSA with SHA256 and custom contentType */
  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_custom_contentType.der", 0,
  35885. customContentType, sizeof(customContentType), 0, 0, 0, NULL, 0,
  35886. NULL, 0, 0);
  35887. /* ECDSA with SHA256 and FirmwarePkgData contentType */
  35888. ADD_PKCS7SIGNEDVECTOR(
  35889. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  35890. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  35891. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  35892. "pkcs7signedData_ECDSA_SHA256_firmwarePkgData.der",
  35893. FIRMWARE_PKG_DATA, NULL, 0, 0, 0, 0, NULL, 0, NULL, 0, 0);
  35894. #endif
  35895. #ifdef WOLFSSL_SHA384
  35896. /* ECDSA with SHA384 */
  35897. ADD_PKCS7SIGNEDVECTOR(
  35898. data, (word32)sizeof(data), SHA384h, ECDSAk, eccClientPrivKeyBuf,
  35899. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  35900. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  35901. "pkcs7signedData_ECDSA_SHA384.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  35902. NULL, 0, 0);
  35903. #endif
  35904. #ifdef WOLFSSL_SHA512
  35905. /* ECDSA with SHA512 */
  35906. ADD_PKCS7SIGNEDVECTOR(
  35907. data, (word32)sizeof(data), SHA512h, ECDSAk, eccClientPrivKeyBuf,
  35908. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  35909. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  35910. "pkcs7signedData_ECDSA_SHA512.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  35911. NULL, 0, 0);
  35912. #endif
  35913. #endif /* HAVE_ECC */
  35914. };
  35915. #undef MAX_TESTVECTORS_LEN
  35916. #undef ADD_PKCS7SIGNEDVECTOR
  35917. outSz = FOURK_BUF;
  35918. out = (byte*)XMALLOC(outSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35919. if (out == NULL)
  35920. ERROR_OUT(-12510, out);
  35921. XMEMSET(out, 0, outSz);
  35922. ret = wc_PKCS7_PadData((byte*)data, sizeof(data), out, outSz, 16);
  35923. if (ret < 0)
  35924. ERROR_OUT(-12511, out);
  35925. #ifndef HAVE_FIPS
  35926. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  35927. #else
  35928. ret = wc_InitRng(&rng);
  35929. #endif
  35930. if (ret != 0)
  35931. ERROR_OUT(-12512, out);
  35932. for (i = 0; i < testSz; i++) {
  35933. if (pkcs7)
  35934. wc_PKCS7_Free(pkcs7);
  35935. pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
  35936. if (pkcs7 == NULL)
  35937. ERROR_OUT(-12513, out);
  35938. ret = wc_PKCS7_InitWithCert(pkcs7, testVectors[i].cert,
  35939. (word32)testVectors[i].certSz);
  35940. if (ret != 0)
  35941. ERROR_OUT(-12514, out);
  35942. /* load CA certificate, if present */
  35943. if (testVectors[i].caCert != NULL) {
  35944. ret = wc_PKCS7_AddCertificate(pkcs7, testVectors[i].caCert,
  35945. (word32)testVectors[i].caCertSz);
  35946. if (ret != 0)
  35947. ERROR_OUT(-12515, out);
  35948. }
  35949. pkcs7->rng = &rng;
  35950. pkcs7->content = (byte*)testVectors[i].content;
  35951. pkcs7->contentSz = testVectors[i].contentSz;
  35952. pkcs7->contentOID = testVectors[i].contentOID;
  35953. pkcs7->hashOID = testVectors[i].hashOID;
  35954. pkcs7->encryptOID = testVectors[i].signOID;
  35955. pkcs7->privateKey = testVectors[i].privateKey;
  35956. pkcs7->privateKeySz = testVectors[i].privateKeySz;
  35957. pkcs7->signedAttribs = testVectors[i].signedAttribs;
  35958. pkcs7->signedAttribsSz = testVectors[i].signedAttribsSz;
  35959. /* optional custom contentType, default is DATA,
  35960. overrides contentOID if set */
  35961. if (testVectors[i].contentType != NULL) {
  35962. ret = wc_PKCS7_SetContentType(pkcs7, testVectors[i].contentType,
  35963. testVectors[i].contentTypeSz);
  35964. if (ret != 0)
  35965. ERROR_OUT(-12516, out);
  35966. }
  35967. /* set SignerIdentifier to use SubjectKeyIdentifier if desired,
  35968. default is IssuerAndSerialNumber */
  35969. if (testVectors[i].sidType == CMS_SKID) {
  35970. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID);
  35971. if (ret != 0)
  35972. ERROR_OUT(-12517, out);
  35973. }
  35974. /* generate senderNonce */
  35975. {
  35976. senderNonce[0] = 0x04;
  35977. senderNonce[1] = PKCS7_NONCE_SZ;
  35978. ret = wc_RNG_GenerateBlock(&rng, &senderNonce[2], PKCS7_NONCE_SZ);
  35979. if (ret != 0)
  35980. ERROR_OUT(-12518, out);
  35981. }
  35982. /* generate transactionID (used with SCEP) */
  35983. {
  35984. #ifndef NO_SHA
  35985. wc_Sha sha;
  35986. byte digest[WC_SHA_DIGEST_SIZE];
  35987. #else
  35988. wc_Sha256 sha;
  35989. byte digest[WC_SHA256_DIGEST_SIZE];
  35990. #endif
  35991. int j,k;
  35992. transId[0] = 0x13;
  35993. transId[1] = sizeof(digest) * 2;
  35994. #ifndef NO_SHA
  35995. ret = wc_InitSha_ex(&sha, HEAP_HINT, devId);
  35996. if (ret != 0)
  35997. ERROR_OUT(-12519, out);
  35998. wc_ShaUpdate(&sha, pkcs7->publicKey, pkcs7->publicKeySz);
  35999. wc_ShaFinal(&sha, digest);
  36000. wc_ShaFree(&sha);
  36001. #else
  36002. ret = wc_InitSha256_ex(&sha, HEAP_HINT, devId);
  36003. if (ret != 0)
  36004. ERROR_OUT(-12520, out);
  36005. wc_Sha256Update(&sha, pkcs7->publicKey, pkcs7->publicKeySz);
  36006. wc_Sha256Final(&sha, digest);
  36007. wc_Sha256Free(&sha);
  36008. #endif
  36009. for (j = 0, k = 2; j < (int)sizeof(digest); j++, k += 2) {
  36010. #if defined(WOLF_C89)
  36011. XSPRINTF((char*)&transId[k], "%02x", digest[j]);
  36012. #else
  36013. (void)XSNPRINTF((char*)&transId[k], 3, "%02x", digest[j]);
  36014. #endif
  36015. }
  36016. }
  36017. /* enable detached signature generation, if set */
  36018. if (testVectors[i].detachedSignature == 1) {
  36019. ret = wc_PKCS7_SetDetached(pkcs7, 1);
  36020. if (ret != 0)
  36021. ERROR_OUT(-12521, out);
  36022. }
  36023. encodedSz = wc_PKCS7_EncodeSignedData(pkcs7, out, outSz);
  36024. if (encodedSz < 0)
  36025. ERROR_OUT(-12522, out);
  36026. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  36027. /* write PKCS#7 to output file for more testing */
  36028. file = XFOPEN(testVectors[i].outFileName, "wb");
  36029. if (!file) {
  36030. ERROR_OUT(-12523, out);
  36031. }
  36032. ret = (int)XFWRITE(out, 1, encodedSz, file);
  36033. XFCLOSE(file);
  36034. if (ret != (int)encodedSz)
  36035. ERROR_OUT(-12524, out);
  36036. #endif /* PKCS7_OUTPUT_TEST_BUNDLES */
  36037. wc_PKCS7_Free(pkcs7);
  36038. pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
  36039. if (pkcs7 == NULL)
  36040. ERROR_OUT(-12525, out);
  36041. wc_PKCS7_InitWithCert(pkcs7, NULL, 0);
  36042. if (testVectors[i].detachedSignature == 1) {
  36043. /* set content for verifying detached signatures */
  36044. pkcs7->content = (byte*)testVectors[i].content;
  36045. pkcs7->contentSz = testVectors[i].contentSz;
  36046. }
  36047. ret = wc_PKCS7_VerifySignedData(pkcs7, out, outSz);
  36048. if (ret < 0)
  36049. ERROR_OUT(-12526, out);
  36050. /* verify contentType extracted successfully for custom content types */
  36051. if (testVectors[i].contentTypeSz > 0) {
  36052. if (pkcs7->contentTypeSz != testVectors[i].contentTypeSz) {
  36053. ERROR_OUT(-12527, out);
  36054. } else if (XMEMCMP(pkcs7->contentType, testVectors[i].contentType,
  36055. pkcs7->contentTypeSz) != 0) {
  36056. ERROR_OUT(-12528, out);
  36057. }
  36058. }
  36059. if (pkcs7->singleCert == NULL || pkcs7->singleCertSz == 0)
  36060. ERROR_OUT(-12529, out);
  36061. {
  36062. /* check getting signed attributes */
  36063. #ifndef NO_SHA
  36064. byte buf[(WC_SHA_DIGEST_SIZE + 1) * 2 + 1];
  36065. #else
  36066. byte buf[(WC_SHA256_DIGEST_SIZE + 1) * 2 + 1];
  36067. #endif
  36068. byte* oidPt = transIdOid + 2; /* skip object id tag and size */
  36069. int oidSz = (int)sizeof(transIdOid) - 2;
  36070. int bufSz = 0;
  36071. if (testVectors[i].signedAttribs != NULL &&
  36072. wc_PKCS7_GetAttributeValue(pkcs7, oidPt, oidSz,
  36073. NULL, (word32*)&bufSz) != LENGTH_ONLY_E)
  36074. ERROR_OUT(-12530, out);
  36075. if (bufSz > (int)sizeof(buf))
  36076. ERROR_OUT(-12531, out);
  36077. bufSz = wc_PKCS7_GetAttributeValue(pkcs7, oidPt, oidSz,
  36078. buf, (word32*)&bufSz);
  36079. if ((testVectors[i].signedAttribs != NULL && bufSz < 0) ||
  36080. (testVectors[i].signedAttribs == NULL && bufSz > 0))
  36081. ERROR_OUT(-12532, out);
  36082. }
  36083. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  36084. file = XFOPEN("./pkcs7cert.der", "wb");
  36085. if (!file)
  36086. ERROR_OUT(-12533, out);
  36087. ret = (int)XFWRITE(pkcs7->singleCert, 1, pkcs7->singleCertSz, file);
  36088. XFCLOSE(file);
  36089. #endif /* PKCS7_OUTPUT_TEST_BUNDLES */
  36090. }
  36091. out:
  36092. if (pkcs7 != NULL)
  36093. wc_PKCS7_Free(pkcs7);
  36094. if (out != NULL)
  36095. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  36096. if (testVectors != NULL)
  36097. XFREE(testVectors, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  36098. wc_FreeRng(&rng);
  36099. if (ret > 0)
  36100. return 0;
  36101. (void)rsaClientCertBuf;
  36102. (void)rsaClientCertBufSz;
  36103. (void)rsaClientPrivKeyBuf;
  36104. (void)rsaClientPrivKeyBufSz;
  36105. (void)rsaServerCertBuf;
  36106. (void)rsaServerCertBufSz;
  36107. (void)rsaServerPrivKeyBuf;
  36108. (void)rsaServerPrivKeyBufSz;
  36109. (void)rsaCaCertBuf;
  36110. (void)rsaCaCertBufSz;
  36111. (void)rsaCaPrivKeyBuf;
  36112. (void)rsaCaPrivKeyBufSz;
  36113. (void)eccClientCertBuf;
  36114. (void)eccClientCertBufSz;
  36115. (void)eccClientPrivKeyBuf;
  36116. (void)eccClientPrivKeyBufSz;
  36117. return ret;
  36118. }
  36119. static int pkcs7signed_run_SingleShotVectors(
  36120. byte* rsaClientCertBuf, word32 rsaClientCertBufSz,
  36121. byte* rsaClientPrivKeyBuf, word32 rsaClientPrivKeyBufSz,
  36122. byte* rsaServerCertBuf, word32 rsaServerCertBufSz,
  36123. byte* rsaServerPrivKeyBuf, word32 rsaServerPrivKeyBufSz,
  36124. byte* rsaCaCertBuf, word32 rsaCaCertBufSz,
  36125. byte* rsaCaPrivKeyBuf, word32 rsaCaPrivKeyBufSz,
  36126. byte* eccClientCertBuf, word32 eccClientCertBufSz,
  36127. byte* eccClientPrivKeyBuf, word32 eccClientPrivKeyBufSz)
  36128. {
  36129. int ret, testSz = 0, i;
  36130. int encodedSz;
  36131. byte* out = NULL;
  36132. word32 outSz;
  36133. WC_RNG rng;
  36134. PKCS7* pkcs7 = NULL;
  36135. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  36136. XFILE file;
  36137. #endif
  36138. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA) && \
  36139. !defined(NO_PKCS7_ENCRYPTED_DATA)
  36140. byte* encryptedTmp = NULL;
  36141. int encryptedTmpSz;
  36142. #endif
  36143. WOLFSSL_SMALL_STACK_STATIC const byte data[] = { /* Hello World */
  36144. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  36145. 0x72,0x6c,0x64
  36146. };
  36147. #if !defined(NO_PKCS7_ENCRYPTED_DATA) && \
  36148. defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_256)
  36149. static byte aes256Key[] = {
  36150. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  36151. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  36152. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  36153. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  36154. };
  36155. #endif
  36156. static byte messageTypeOid[] =
  36157. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  36158. 0x09, 0x02 };
  36159. static byte messageType[] = { 0x13, 2, '1', '9' };
  36160. PKCS7Attrib attribs[] =
  36161. {
  36162. { messageTypeOid, sizeof(messageTypeOid), messageType,
  36163. sizeof(messageType) },
  36164. };
  36165. #define MAX_TESTVECTORS_LEN 19
  36166. #define ADD_PKCS7SIGNEDVECTOR(...) { \
  36167. pkcs7SignedVector _this_vector = { __VA_ARGS__ }; \
  36168. if (testSz == MAX_TESTVECTORS_LEN) { \
  36169. ret = -12568; \
  36170. goto out; \
  36171. } \
  36172. XMEMCPY(&testVectors[testSz++], &_this_vector, sizeof _this_vector);\
  36173. }
  36174. pkcs7SignedVector *testVectors = NULL;
  36175. XMEMSET(&rng, 0, sizeof(rng));
  36176. testVectors = (pkcs7SignedVector *)XMALLOC(MAX_TESTVECTORS_LEN * sizeof(*testVectors),
  36177. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  36178. if (testVectors == NULL) {
  36179. ret = -12567;
  36180. goto out;
  36181. }
  36182. {
  36183. #ifndef NO_RSA
  36184. #ifndef NO_SHA256
  36185. /* Signed FirmwarePkgData, RSA, SHA256, no attribs */
  36186. ADD_PKCS7SIGNEDVECTOR(
  36187. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  36188. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  36189. NULL, 0,
  36190. "pkcs7signedFirmwarePkgData_RSA_SHA256_noattr.der", 0, NULL, 0, 0,
  36191. 0, 0, NULL, 0, NULL, 0, 0);
  36192. /* Signed FirmwarePkgData, RSA, SHA256, attrs */
  36193. ADD_PKCS7SIGNEDVECTOR(
  36194. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  36195. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  36196. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  36197. "pkcs7signedFirmwarePkgData_RSA_SHA256.der", 0, NULL, 0, 0, 0, 0,
  36198. NULL, 0, NULL, 0, 0);
  36199. /* Signed FirmwarePkgData, RSA, SHA256, SubjectKeyIdentifier, 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_SKID.der", 0, NULL,
  36205. 0, CMS_SKID, 0, 0, NULL, 0, NULL, 0, 0);
  36206. /* Signed FirmwraePkgData, RSA, SHA256, server cert and ca cert, attr */
  36207. ADD_PKCS7SIGNEDVECTOR(
  36208. data, (word32)sizeof(data), SHA256h, RSAk, rsaServerPrivKeyBuf,
  36209. rsaServerPrivKeyBufSz, rsaServerCertBuf, rsaServerCertBufSz,
  36210. rsaCaCertBuf, rsaCaCertBufSz,
  36211. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  36212. "pkcs7signedFirmwarePkgData_RSA_SHA256_with_ca_cert.der", 0, NULL,
  36213. 0, 0, 0, 0, NULL, 0, NULL, 0, 0);
  36214. #if !defined(NO_PKCS7_ENCRYPTED_DATA) && \
  36215. defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_256)
  36216. /* Signed Encrypted FirmwarePkgData, RSA, SHA256, no attribs */
  36217. ADD_PKCS7SIGNEDVECTOR(
  36218. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  36219. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  36220. NULL, 0,
  36221. "pkcs7signedEncryptedFirmwarePkgData_RSA_SHA256_noattr.der", 0,
  36222. NULL, 0, 0, AES256CBCb, 1, aes256Key, sizeof(aes256Key), NULL, 0, 0);
  36223. /* Signed Encrypted FirmwarePkgData, RSA, SHA256, attribs */
  36224. ADD_PKCS7SIGNEDVECTOR(
  36225. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  36226. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  36227. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  36228. "pkcs7signedEncryptedFirmwarePkgData_RSA_SHA256.der", 0,
  36229. NULL, 0, 0, AES256CBCb, 1, aes256Key, sizeof(aes256Key),
  36230. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)), 0);
  36231. #endif /* WOLFSSL_AES_256 && !NO_PKCS7_ENCRYPTED_DATA */
  36232. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  36233. /* Signed Compressed FirmwarePkgData, RSA, SHA256, no attribs */
  36234. ADD_PKCS7SIGNEDVECTOR(
  36235. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  36236. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  36237. NULL, 0,
  36238. "pkcs7signedCompressedFirmwarePkgData_RSA_SHA256_noattr.der", 0,
  36239. NULL, 0, 0, 0, 2, NULL, 0, NULL, 0, 0);
  36240. /* Signed Compressed FirmwarePkgData, RSA, SHA256, attribs */
  36241. ADD_PKCS7SIGNEDVECTOR(
  36242. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  36243. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  36244. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  36245. "pkcs7signedCompressedFirmwarePkgData_RSA_SHA256.der", 0,
  36246. NULL, 0, 0, 0, 2, NULL, 0, NULL, 0, 0);
  36247. #ifndef NO_PKCS7_ENCRYPTED_DATA
  36248. /* Signed Encrypted Compressed FirmwarePkgData, RSA, SHA256,
  36249. no attribs */
  36250. ADD_PKCS7SIGNEDVECTOR(
  36251. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  36252. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  36253. NULL, 0,
  36254. "pkcs7signedEncryptedCompressedFirmwarePkgData_RSA_SHA256_noattr.der",
  36255. 0, NULL, 0, 0, AES256CBCb, 3, aes256Key, sizeof(aes256Key), NULL,
  36256. 0, 0);
  36257. /* Signed Encrypted Compressed FirmwarePkgData, RSA, SHA256,
  36258. attribs */
  36259. ADD_PKCS7SIGNEDVECTOR(
  36260. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  36261. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  36262. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  36263. "pkcs7signedEncryptedCompressedFirmwarePkgData_RSA_SHA256.der",
  36264. 0, NULL, 0, 0, AES256CBCb, 3, aes256Key, sizeof(aes256Key),
  36265. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)), 0);
  36266. #endif /* !NO_PKCS7_ENCRYPTED_DATA */
  36267. #endif /* HAVE_LIBZ && !NO_PKCS7_COMPRESSED_DATA */
  36268. #endif /* NO_SHA256 */
  36269. #endif /* NO_RSA */
  36270. #ifdef HAVE_ECC
  36271. #ifndef NO_SHA256
  36272. /* Signed FirmwarePkgData, ECDSA, SHA256, no attribs */
  36273. ADD_PKCS7SIGNEDVECTOR(
  36274. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  36275. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  36276. NULL, 0,
  36277. "pkcs7signedFirmwarePkgData_ECDSA_SHA256_noattr.der", 0, NULL,
  36278. 0, 0, 0, 0, NULL, 0, NULL, 0, 0);
  36279. /* Signed FirmwarePkgData, ECDSA, SHA256, attribs */
  36280. ADD_PKCS7SIGNEDVECTOR(
  36281. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  36282. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  36283. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  36284. "pkcs7signedFirmwarePkgData_ECDSA_SHA256.der", 0, NULL,
  36285. 0, 0, 0, 0, NULL, 0, NULL, 0, 0);
  36286. /* Signed FirmwarePkgData, ECDSA, SHA256, SubjectKeyIdentifier, attr */
  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_SKID.der", 0, NULL,
  36292. 0, CMS_SKID, 0, 0, NULL, 0, NULL, 0, 0);
  36293. #if !defined(NO_PKCS7_ENCRYPTED_DATA) && \
  36294. defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_256)
  36295. /* Signed Encrypted FirmwarePkgData, ECDSA, SHA256, no attribs */
  36296. ADD_PKCS7SIGNEDVECTOR(
  36297. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  36298. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  36299. NULL, 0,
  36300. "pkcs7signedEncryptedFirmwarePkgData_ECDSA_SHA256_noattr.der", 0, NULL,
  36301. 0, 0, AES256CBCb, 1, aes256Key, sizeof(aes256Key), NULL, 0, 0);
  36302. /* Signed Encrypted FirmwarePkgData, ECDSA, SHA256, attribs */
  36303. ADD_PKCS7SIGNEDVECTOR(
  36304. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  36305. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  36306. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  36307. "pkcs7signedEncryptedFirmwarePkgData_ECDSA_SHA256.der", 0, NULL,
  36308. 0, 0, AES256CBCb, 1, aes256Key, sizeof(aes256Key),
  36309. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)), 0);
  36310. #endif /* WOLFSSL_AES_256 && !NO_PKCS7_ENCRYPTED_DATA */
  36311. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  36312. /* Signed Compressed FirmwarePkgData, ECDSA, SHA256, no attribs */
  36313. ADD_PKCS7SIGNEDVECTOR(
  36314. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  36315. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  36316. NULL, 0,
  36317. "pkcs7signedCompressedFirmwarePkgData_ECDSA_SHA256_noattr.der", 0, NULL,
  36318. 0, 0, 0, 2, NULL, 0, NULL, 0, 0);
  36319. /* Signed Compressed FirmwarePkgData, ECDSA, SHA256, attrib */
  36320. ADD_PKCS7SIGNEDVECTOR(
  36321. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  36322. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  36323. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  36324. "pkcs7signedCompressedFirmwarePkgData_ECDSA_SHA256.der", 0, NULL,
  36325. 0, 0, 0, 2, NULL, 0, NULL, 0, 0);
  36326. #ifndef NO_PKCS7_ENCRYPTED_DATA
  36327. /* Signed Encrypted Compressed FirmwarePkgData, ECDSA, SHA256,
  36328. no attribs */
  36329. ADD_PKCS7SIGNEDVECTOR(
  36330. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  36331. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  36332. NULL, 0,
  36333. "pkcs7signedEncryptedCompressedFirmwarePkgData_ECDSA_SHA256_noattr.der",
  36334. 0, NULL, 0, 0, AES256CBCb, 3, aes256Key, sizeof(aes256Key), NULL,
  36335. 0, 0);
  36336. /* Signed Encrypted Compressed FirmwarePkgData, ECDSA, SHA256,
  36337. attribs */
  36338. ADD_PKCS7SIGNEDVECTOR(
  36339. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  36340. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  36341. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  36342. "pkcs7signedEncryptedCompressedFirmwarePkgData_ECDSA_SHA256.der",
  36343. 0, NULL, 0, 0, AES256CBCb, 3, aes256Key, sizeof(aes256Key),
  36344. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)), 0);
  36345. #endif /* !NO_PKCS7_ENCRYPTED_DATA */
  36346. #endif /* HAVE_LIBZ && !NO_PKCS7_COMPRESSED_DATA */
  36347. #endif /* NO_SHA256 */
  36348. #endif /* HAVE_ECC */
  36349. };
  36350. #undef MAX_TESTVECTORS_LEN
  36351. #undef ADD_PKCS7SIGNEDVECTOR
  36352. outSz = FOURK_BUF;
  36353. out = (byte*)XMALLOC(outSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  36354. if (out == NULL)
  36355. ERROR_OUT(-12540, out);
  36356. XMEMSET(out, 0, outSz);
  36357. ret = wc_PKCS7_PadData((byte*)data, sizeof(data), out, outSz, 16);
  36358. if (ret < 0)
  36359. ERROR_OUT(-12541, out);
  36360. #ifndef HAVE_FIPS
  36361. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  36362. #else
  36363. ret = wc_InitRng(&rng);
  36364. #endif
  36365. if (ret != 0)
  36366. ERROR_OUT(-12542, out);
  36367. for (i = 0; i < testSz; i++) {
  36368. if (pkcs7)
  36369. wc_PKCS7_Free(pkcs7);
  36370. pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
  36371. if (pkcs7 == NULL)
  36372. ERROR_OUT(-12543, out);
  36373. ret = wc_PKCS7_InitWithCert(pkcs7, testVectors[i].cert,
  36374. (word32)testVectors[i].certSz);
  36375. if (ret != 0)
  36376. ERROR_OUT(-12544, out);
  36377. /* load CA certificate, if present */
  36378. if (testVectors[i].caCert != NULL) {
  36379. ret = wc_PKCS7_AddCertificate(pkcs7, testVectors[i].caCert,
  36380. (word32)testVectors[i].caCertSz);
  36381. if (ret != 0)
  36382. ERROR_OUT(-12545, out);
  36383. }
  36384. /* set SignerIdentifier to use SubjectKeyIdentifier if desired,
  36385. default is IssuerAndSerialNumber */
  36386. if (testVectors[i].sidType == CMS_SKID) {
  36387. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID);
  36388. if (ret != 0)
  36389. ERROR_OUT(-12546, out);
  36390. }
  36391. if (testVectors[i].encCompFlag == 0) {
  36392. /* encode Signed FirmwarePkgData */
  36393. encodedSz = wc_PKCS7_EncodeSignedFPD(pkcs7,
  36394. testVectors[i].privateKey, testVectors[i].privateKeySz,
  36395. testVectors[i].signOID, testVectors[i].hashOID,
  36396. (byte*)testVectors[i].content, testVectors[i].contentSz,
  36397. testVectors[i].signedAttribs,
  36398. testVectors[i].signedAttribsSz, out, outSz);
  36399. if (encodedSz < 0)
  36400. ERROR_OUT(-12547, out);
  36401. #ifndef NO_PKCS7_ENCRYPTED_DATA
  36402. } else if (testVectors[i].encCompFlag == 1) {
  36403. /* encode Signed Encrypted FirmwarePkgData */
  36404. encodedSz = wc_PKCS7_EncodeSignedEncryptedFPD(pkcs7,
  36405. testVectors[i].encryptKey, testVectors[i].encryptKeySz,
  36406. testVectors[i].privateKey, testVectors[i].privateKeySz,
  36407. testVectors[i].encryptOID, testVectors[i].signOID,
  36408. testVectors[i].hashOID, (byte*)testVectors[i].content,
  36409. testVectors[i].contentSz, testVectors[i].unprotectedAttribs,
  36410. testVectors[i].unprotectedAttribsSz,
  36411. testVectors[i].signedAttribs,
  36412. testVectors[i].signedAttribsSz, out, outSz);
  36413. if (encodedSz <= 0)
  36414. ERROR_OUT(-12548, out);
  36415. #endif
  36416. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  36417. } else if (testVectors[i].encCompFlag == 2) {
  36418. /* encode Signed Compressed FirmwarePkgData */
  36419. encodedSz = wc_PKCS7_EncodeSignedCompressedFPD(pkcs7,
  36420. testVectors[i].privateKey, testVectors[i].privateKeySz,
  36421. testVectors[i].signOID, testVectors[i].hashOID,
  36422. (byte*)testVectors[i].content, testVectors[i].contentSz,
  36423. testVectors[i].signedAttribs,
  36424. testVectors[i].signedAttribsSz, out, outSz);
  36425. if (encodedSz <= 0)
  36426. ERROR_OUT(-12549, out);
  36427. #ifndef NO_PKCS7_ENCRYPTED_DATA
  36428. } else if (testVectors[i].encCompFlag == 3) {
  36429. /* encode Signed Encrypted Compressed FirmwarePkgData */
  36430. encodedSz = wc_PKCS7_EncodeSignedEncryptedCompressedFPD(pkcs7,
  36431. testVectors[i].encryptKey, testVectors[i].encryptKeySz,
  36432. testVectors[i].privateKey, testVectors[i].privateKeySz,
  36433. testVectors[i].encryptOID, testVectors[i].signOID,
  36434. testVectors[i].hashOID, (byte*)testVectors[i].content,
  36435. testVectors[i].contentSz, testVectors[i].unprotectedAttribs,
  36436. testVectors[i].unprotectedAttribsSz,
  36437. testVectors[i].signedAttribs,
  36438. testVectors[i].signedAttribsSz, out, outSz);
  36439. if (encodedSz <= 0)
  36440. ERROR_OUT(-12550, out);
  36441. #endif /* NO_PKCS7_ENCRYPTED_DATA */
  36442. #endif /* HAVE_LIBZ && !NO_PKCS7_COMPRESSED_DATA */
  36443. } else {
  36444. /* unsupported SignedData single-shot combination */
  36445. ERROR_OUT(-12551, out);
  36446. }
  36447. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  36448. /* write PKCS#7 to output file for more testing */
  36449. file = XFOPEN(testVectors[i].outFileName, "wb");
  36450. if (!file)
  36451. ERROR_OUT(-12552, out);
  36452. ret = (int)XFWRITE(out, 1, encodedSz, file);
  36453. XFCLOSE(file);
  36454. file = NULL;
  36455. if (ret != (int)encodedSz)
  36456. ERROR_OUT(-12553, out);
  36457. #endif /* PKCS7_OUTPUT_TEST_BUNDLES */
  36458. wc_PKCS7_Free(pkcs7);
  36459. pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
  36460. if (pkcs7 == NULL)
  36461. ERROR_OUT(-12554, out);
  36462. wc_PKCS7_InitWithCert(pkcs7, NULL, 0);
  36463. ret = wc_PKCS7_VerifySignedData(pkcs7, out, outSz);
  36464. if (ret < 0)
  36465. ERROR_OUT(-12555, out);
  36466. #ifndef NO_PKCS7_STREAM
  36467. {
  36468. word32 z;
  36469. for (z = 0; z < outSz && ret != 0; z++) {
  36470. ret = wc_PKCS7_VerifySignedData(pkcs7, out + z, 1);
  36471. if (ret < 0 && ret != WC_PKCS7_WANT_READ_E) {
  36472. printf("unexpected error %d\n", ret);
  36473. ERROR_OUT(-12556, out);
  36474. }
  36475. }
  36476. }
  36477. #endif
  36478. if (pkcs7->singleCert == NULL || pkcs7->singleCertSz == 0)
  36479. ERROR_OUT(-12557, out);
  36480. if (testVectors[i].encCompFlag == 0) {
  36481. /* verify decoded content matches expected */
  36482. if ((pkcs7->contentSz != testVectors[i].contentSz) ||
  36483. XMEMCMP(pkcs7->content, testVectors[i].content,
  36484. pkcs7->contentSz)) {
  36485. ERROR_OUT(-12558, out);
  36486. }
  36487. }
  36488. #ifndef NO_PKCS7_ENCRYPTED_DATA
  36489. else if (testVectors[i].encCompFlag == 1) {
  36490. /* decrypt inner encryptedData */
  36491. pkcs7->encryptionKey = testVectors[i].encryptKey;
  36492. pkcs7->encryptionKeySz = testVectors[i].encryptKeySz;
  36493. ret = wc_PKCS7_DecodeEncryptedData(pkcs7, pkcs7->content,
  36494. pkcs7->contentSz, out, outSz);
  36495. if (ret < 0)
  36496. ERROR_OUT(-12559, out);
  36497. /* compare decrypted to expected */
  36498. if (((word32)ret != testVectors[i].contentSz) ||
  36499. XMEMCMP(out, testVectors[i].content, ret))
  36500. ERROR_OUT(-12560, out);
  36501. }
  36502. #endif
  36503. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  36504. else if (testVectors[i].encCompFlag == 2) {
  36505. /* decompress inner compressedData */
  36506. ret = wc_PKCS7_DecodeCompressedData(pkcs7, pkcs7->content,
  36507. pkcs7->contentSz, out, outSz);
  36508. if (ret < 0)
  36509. ERROR_OUT(-12561, out);
  36510. /* compare decompressed to expected */
  36511. if (((word32)ret != testVectors[i].contentSz) ||
  36512. XMEMCMP(out, testVectors[i].content, ret))
  36513. ERROR_OUT(-12562, out);
  36514. }
  36515. #ifndef NO_PKCS7_ENCRYPTED_DATA
  36516. else if (testVectors[i].encCompFlag == 3) {
  36517. encryptedTmpSz = FOURK_BUF;
  36518. encryptedTmp = (byte*)XMALLOC(encryptedTmpSz, HEAP_HINT,
  36519. DYNAMIC_TYPE_TMP_BUFFER);
  36520. if (encryptedTmp == NULL)
  36521. ERROR_OUT(-12563, out);
  36522. XMEMSET(encryptedTmp, 0, encryptedTmpSz);
  36523. /* decrypt inner encryptedData */
  36524. pkcs7->encryptionKey = testVectors[i].encryptKey;
  36525. pkcs7->encryptionKeySz = testVectors[i].encryptKeySz;
  36526. encryptedTmpSz = wc_PKCS7_DecodeEncryptedData(pkcs7, pkcs7->content,
  36527. pkcs7->contentSz, encryptedTmp,
  36528. encryptedTmpSz);
  36529. if (encryptedTmpSz < 0 || pkcs7->contentOID != COMPRESSED_DATA)
  36530. ERROR_OUT(-12564, out);
  36531. /* decompress inner compressedData */
  36532. ret = wc_PKCS7_DecodeCompressedData(pkcs7, encryptedTmp,
  36533. encryptedTmpSz, out, outSz);
  36534. if (ret < 0)
  36535. ERROR_OUT(-12565, out);
  36536. /* compare decompressed to expected */
  36537. if (((word32)ret != testVectors[i].contentSz) ||
  36538. XMEMCMP(out, testVectors[i].content, ret))
  36539. ERROR_OUT(-12566, out);
  36540. }
  36541. #endif /* NO_PKCS7_ENCRYPTED_DATA */
  36542. #endif /* HAVE_LIBZ && !NO_PKCS7_COMPRESSED_DATA */
  36543. }
  36544. out:
  36545. if (pkcs7 != NULL)
  36546. wc_PKCS7_Free(pkcs7);
  36547. if (out != NULL)
  36548. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  36549. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA) && \
  36550. !defined(NO_PKCS7_ENCRYPTED_DATA)
  36551. if (encryptedTmp != NULL)
  36552. XFREE(encryptedTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  36553. #endif
  36554. if (testVectors != NULL)
  36555. XFREE(testVectors, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  36556. wc_FreeRng(&rng);
  36557. if (ret > 0)
  36558. return 0;
  36559. (void)eccClientCertBuf;
  36560. (void)eccClientCertBufSz;
  36561. (void)eccClientPrivKeyBuf;
  36562. (void)eccClientPrivKeyBufSz;
  36563. (void)rsaClientCertBuf;
  36564. (void)rsaClientCertBufSz;
  36565. (void)rsaClientPrivKeyBuf;
  36566. (void)rsaClientPrivKeyBufSz;
  36567. (void)rsaServerCertBuf;
  36568. (void)rsaServerCertBufSz;
  36569. (void)rsaServerPrivKeyBuf;
  36570. (void)rsaServerPrivKeyBufSz;
  36571. (void)rsaCaCertBuf;
  36572. (void)rsaCaCertBufSz;
  36573. (void)rsaCaPrivKeyBuf;
  36574. (void)rsaCaPrivKeyBufSz;
  36575. return ret;
  36576. }
  36577. WOLFSSL_TEST_SUBROUTINE int pkcs7signed_test(void)
  36578. {
  36579. int ret = 0;
  36580. byte* rsaClientCertBuf = NULL;
  36581. byte* rsaServerCertBuf = NULL;
  36582. byte* rsaCaCertBuf = NULL;
  36583. byte* eccClientCertBuf = NULL;
  36584. byte* rsaClientPrivKeyBuf = NULL;
  36585. byte* rsaServerPrivKeyBuf = NULL;
  36586. byte* rsaCaPrivKeyBuf = NULL;
  36587. byte* eccClientPrivKeyBuf = NULL;
  36588. word32 rsaClientCertBufSz = 0;
  36589. word32 rsaServerCertBufSz = 0;
  36590. word32 rsaCaCertBufSz = 0;
  36591. word32 eccClientCertBufSz = 0;
  36592. word32 rsaClientPrivKeyBufSz = 0;
  36593. word32 rsaServerPrivKeyBufSz = 0;
  36594. word32 rsaCaPrivKeyBufSz = 0;
  36595. word32 eccClientPrivKeyBufSz = 0;
  36596. #ifndef NO_RSA
  36597. /* read client RSA cert and key in DER format */
  36598. rsaClientCertBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  36599. DYNAMIC_TYPE_TMP_BUFFER);
  36600. if (rsaClientCertBuf == NULL)
  36601. ret = -12600;
  36602. rsaClientPrivKeyBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  36603. DYNAMIC_TYPE_TMP_BUFFER);
  36604. if (ret == 0 && rsaClientPrivKeyBuf == NULL) {
  36605. ret = -12601;
  36606. }
  36607. rsaClientCertBufSz = FOURK_BUF;
  36608. rsaClientPrivKeyBufSz = FOURK_BUF;
  36609. /* read server RSA cert and key in DER format */
  36610. rsaServerCertBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  36611. DYNAMIC_TYPE_TMP_BUFFER);
  36612. if (ret == 0 && rsaServerCertBuf == NULL)
  36613. ret = -12602;
  36614. rsaServerPrivKeyBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  36615. DYNAMIC_TYPE_TMP_BUFFER);
  36616. if (ret == 0 && rsaServerPrivKeyBuf == NULL) {
  36617. ret = -12603;
  36618. }
  36619. rsaServerCertBufSz = FOURK_BUF;
  36620. rsaServerPrivKeyBufSz = FOURK_BUF;
  36621. /* read CA RSA cert and key in DER format, for use with server cert */
  36622. rsaCaCertBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  36623. DYNAMIC_TYPE_TMP_BUFFER);
  36624. if (ret == 0 && rsaCaCertBuf == NULL)
  36625. ret = -12604;
  36626. rsaCaPrivKeyBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  36627. DYNAMIC_TYPE_TMP_BUFFER);
  36628. if (ret == 0 && rsaCaPrivKeyBuf == NULL) {
  36629. ret = -12605;
  36630. }
  36631. rsaCaCertBufSz = FOURK_BUF;
  36632. rsaCaPrivKeyBufSz = FOURK_BUF;
  36633. #endif /* NO_RSA */
  36634. #ifdef HAVE_ECC
  36635. /* read client ECC cert and key in DER format */
  36636. eccClientCertBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  36637. DYNAMIC_TYPE_TMP_BUFFER);
  36638. if (ret == 0 && eccClientCertBuf == NULL) {
  36639. ret = -12606;
  36640. }
  36641. eccClientPrivKeyBuf =(byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  36642. DYNAMIC_TYPE_TMP_BUFFER);
  36643. if (ret == 0 && eccClientPrivKeyBuf == NULL) {
  36644. ret = -12607;
  36645. }
  36646. eccClientCertBufSz = FOURK_BUF;
  36647. eccClientPrivKeyBufSz = FOURK_BUF;
  36648. #endif /* HAVE_ECC */
  36649. if (ret >= 0)
  36650. ret = pkcs7_load_certs_keys(rsaClientCertBuf, &rsaClientCertBufSz,
  36651. rsaClientPrivKeyBuf, &rsaClientPrivKeyBufSz,
  36652. rsaServerCertBuf, &rsaServerCertBufSz,
  36653. rsaServerPrivKeyBuf, &rsaServerPrivKeyBufSz,
  36654. rsaCaCertBuf, &rsaCaCertBufSz,
  36655. rsaCaPrivKeyBuf, &rsaCaPrivKeyBufSz,
  36656. eccClientCertBuf, &eccClientCertBufSz,
  36657. eccClientPrivKeyBuf, &eccClientPrivKeyBufSz);
  36658. if (ret < 0) {
  36659. ret = -12608;
  36660. }
  36661. if (ret >= 0)
  36662. ret = pkcs7signed_run_vectors(rsaClientCertBuf, (word32)rsaClientCertBufSz,
  36663. rsaClientPrivKeyBuf, (word32)rsaClientPrivKeyBufSz,
  36664. rsaServerCertBuf, (word32)rsaServerCertBufSz,
  36665. rsaServerPrivKeyBuf, (word32)rsaServerPrivKeyBufSz,
  36666. rsaCaCertBuf, (word32)rsaCaCertBufSz,
  36667. rsaCaPrivKeyBuf, (word32)rsaCaPrivKeyBufSz,
  36668. eccClientCertBuf, (word32)eccClientCertBufSz,
  36669. eccClientPrivKeyBuf, (word32)eccClientPrivKeyBufSz);
  36670. if (ret >= 0)
  36671. ret = pkcs7signed_run_SingleShotVectors(
  36672. rsaClientCertBuf, (word32)rsaClientCertBufSz,
  36673. rsaClientPrivKeyBuf, (word32)rsaClientPrivKeyBufSz,
  36674. rsaServerCertBuf, (word32)rsaServerCertBufSz,
  36675. rsaServerPrivKeyBuf, (word32)rsaServerPrivKeyBufSz,
  36676. rsaCaCertBuf, (word32)rsaCaCertBufSz,
  36677. rsaCaPrivKeyBuf, (word32)rsaCaPrivKeyBufSz,
  36678. eccClientCertBuf, (word32)eccClientCertBufSz,
  36679. eccClientPrivKeyBuf, (word32)eccClientPrivKeyBufSz);
  36680. #if !defined(NO_RSA) && !defined(NO_AES) && defined(HAVE_AES_CBC)
  36681. if (ret >= 0)
  36682. ret = pkcs7callback_test(
  36683. rsaClientCertBuf, (word32)rsaClientCertBufSz,
  36684. rsaClientPrivKeyBuf, (word32)rsaClientPrivKeyBufSz);
  36685. #endif
  36686. XFREE(rsaClientCertBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  36687. XFREE(rsaClientPrivKeyBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  36688. XFREE(rsaServerCertBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  36689. XFREE(rsaServerPrivKeyBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  36690. XFREE(rsaCaCertBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  36691. XFREE(rsaCaPrivKeyBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  36692. XFREE(eccClientCertBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  36693. XFREE(eccClientPrivKeyBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  36694. return ret;
  36695. }
  36696. #endif /* HAVE_PKCS7 */
  36697. #ifdef HAVE_VALGRIND
  36698. /* Need a static build to have access to symbols. */
  36699. /* Maximum number of bytes in a number to test. */
  36700. #define MP_MAX_TEST_BYTE_LEN 32
  36701. static int randNum(mp_int* n, int len, WC_RNG* rng, void* heap)
  36702. {
  36703. byte d[MP_MAX_TEST_BYTE_LEN];
  36704. int ret;
  36705. (void)heap;
  36706. do {
  36707. ret = wc_RNG_GenerateBlock(rng, d, len);
  36708. if (ret != 0)
  36709. return ret;
  36710. ret = mp_read_unsigned_bin(n, d, len);
  36711. if (ret != 0)
  36712. return ret;
  36713. } while (mp_iszero(n));
  36714. return 0;
  36715. }
  36716. #if defined(WOLFSSL_SP_MATH_ALL) || !defined(USE_FAST_MATH)
  36717. static int mp_test_div_3(mp_int* a, mp_int* r, WC_RNG* rng)
  36718. {
  36719. int i, j;
  36720. mp_digit rem;
  36721. mp_digit rem2;
  36722. #if (defined(WOLFSSL_SP_MATH_ALL) && !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
  36723. defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  36724. for (i = 0; i < 10; i++) {
  36725. for (j = 1; j < 10; j++) {
  36726. if (randNum(a, j, rng, NULL) != 0)
  36727. return -12620;
  36728. if (mp_div_3(a, r, &rem) != 0)
  36729. return -12621;
  36730. if (mp_mul_d(r, 3, r) != 0)
  36731. return -12622;
  36732. if (mp_add_d(r, rem, r) != 0)
  36733. return -12623;
  36734. if (mp_cmp(r, a) != MP_EQ)
  36735. return -12624;
  36736. }
  36737. }
  36738. if (mp_div_3(a, r, &rem) != 0)
  36739. return -12625;
  36740. if (mp_div_3(a, a, NULL) != 0)
  36741. return -12626;
  36742. if (mp_cmp(r, a) != MP_EQ)
  36743. return -12627;
  36744. #endif
  36745. #if defined(WOLFSSL_SP_MATH_ALL)
  36746. if (mp_div_d(a, 10, r, &rem) != 0)
  36747. return -12628;
  36748. if (mp_div_d(a, 10, a, NULL) != 0)
  36749. return -12629;
  36750. if (mp_cmp(r, a) != MP_EQ)
  36751. return -12630;
  36752. if (mp_div_d(a, 12, r, &rem) != 0)
  36753. return -12631;
  36754. if (mp_div_d(a, 12, a, NULL) != 0)
  36755. return -12632;
  36756. if (mp_cmp(r, a) != MP_EQ)
  36757. return -12633;
  36758. if (mp_div_d(a, (mp_digit)1 << (DIGIT_BIT / 2), r, &rem) != 0)
  36759. return -12634;
  36760. if (mp_div_d(a, (mp_digit)1 << (DIGIT_BIT / 2), NULL, &rem2) != 0)
  36761. return -12635;
  36762. if (mp_div_d(a, (mp_digit)1 << (DIGIT_BIT / 2), a, NULL) != 0)
  36763. return -12636;
  36764. if (mp_cmp(r, a) != MP_EQ)
  36765. return -12637;
  36766. if (rem != rem2)
  36767. return -12638;
  36768. #endif
  36769. (void)a;
  36770. (void)r;
  36771. (void)rng;
  36772. (void)i;
  36773. (void)j;
  36774. (void)rem;
  36775. (void)rem2;
  36776. return 0;
  36777. }
  36778. #endif /* WOLFSSL_SP_MATH || !USE_FAST_MATH */
  36779. #if (defined(WOLFSSL_SP_MATH_ALL) && !defined(NO_RSA) && \
  36780. !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
  36781. (!defined WOLFSSL_SP_MATH && !defined(WOLFSSL_SP_MATH_ALL) && \
  36782. (defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)))
  36783. static int mp_test_radix_10(mp_int* a, mp_int* r, WC_RNG* rng)
  36784. {
  36785. int ret;
  36786. int i, j;
  36787. int size;
  36788. char str[30];
  36789. WOLFSSL_SMALL_STACK_STATIC const char* badStr1 = "A";
  36790. WOLFSSL_SMALL_STACK_STATIC const char* badStr2 = "a";
  36791. WOLFSSL_SMALL_STACK_STATIC const char* badStr3 = " ";
  36792. WOLFSSL_SMALL_STACK_STATIC const char* zeros = "000";
  36793. WOLFSSL_SMALL_STACK_STATIC const char* empty = "";
  36794. for (i = 0; i < 10; i++) {
  36795. for (j = 2; j < 12; j++) {
  36796. if (randNum(a, j, rng, NULL) != 0)
  36797. return -12640;
  36798. if (mp_radix_size(a, MP_RADIX_DEC, &size) != MP_OKAY)
  36799. return -12641;
  36800. if (mp_toradix(a, str, MP_RADIX_DEC) != MP_OKAY)
  36801. return -12660;
  36802. if ((int)XSTRLEN(str) != size - 1)
  36803. return -12642;
  36804. if (mp_read_radix(r, str, MP_RADIX_DEC) != MP_OKAY)
  36805. return -12661;
  36806. if (mp_cmp(a, r) != MP_EQ)
  36807. return -12643;
  36808. }
  36809. }
  36810. if (mp_read_radix(r, badStr1, MP_RADIX_DEC) != MP_VAL)
  36811. return -12644;
  36812. if (mp_read_radix(r, badStr2, MP_RADIX_DEC) != MP_VAL)
  36813. return -12645;
  36814. if (mp_read_radix(r, badStr3, MP_RADIX_DEC) != MP_VAL)
  36815. return -12646;
  36816. if (mp_read_radix(r, zeros, MP_RADIX_DEC) != MP_OKAY)
  36817. return -12647;
  36818. if (!mp_iszero(r))
  36819. return -12648;
  36820. mp_set(r, 1);
  36821. if (mp_read_radix(r, empty, MP_RADIX_DEC) != MP_OKAY)
  36822. return -12649;
  36823. if (!mp_iszero(r))
  36824. return -12650;
  36825. mp_zero(a);
  36826. ret = mp_radix_size(a, MP_RADIX_DEC, &size);
  36827. if (ret != 0)
  36828. return -12651;
  36829. if (size != 2)
  36830. return -12652;
  36831. ret = mp_toradix(a, str, MP_RADIX_DEC);
  36832. if (ret != 0)
  36833. return -12653;
  36834. if ((int)XSTRLEN(str) != size - 1)
  36835. return -12654;
  36836. ret = mp_read_radix(r, str, MP_RADIX_DEC);
  36837. if (ret != 0)
  36838. return -12655;
  36839. if (!mp_iszero(r))
  36840. return -12656;
  36841. return 0;
  36842. }
  36843. #endif
  36844. #if defined(WOLFSSL_SP_MATH_ALL) || (!defined WOLFSSL_SP_MATH && \
  36845. defined(HAVE_ECC))
  36846. static int mp_test_radix_16(mp_int* a, mp_int* r, WC_RNG* rng)
  36847. {
  36848. int ret;
  36849. int i, j;
  36850. int size;
  36851. char str[30];
  36852. #if defined(WOLFSSL_SP_MATH) || defined(USE_FAST_MATH)
  36853. static char longStr[2 * sizeof(a->dp) + 2];
  36854. #endif
  36855. WOLFSSL_SMALL_STACK_STATIC const char* badStr1 = " ";
  36856. WOLFSSL_SMALL_STACK_STATIC const char* badStr2 = "}";
  36857. WOLFSSL_SMALL_STACK_STATIC const char* empty = "";
  36858. for (i = 0; i < 10; i++) {
  36859. for (j = 2; j < 12; j++) {
  36860. if (randNum(a, j, rng, NULL) != 0)
  36861. return -12660;
  36862. mp_radix_size(a, MP_RADIX_HEX, &size);
  36863. mp_toradix(a, str, MP_RADIX_HEX);
  36864. if ((int)XSTRLEN(str) != size - 1)
  36865. return -12661;
  36866. mp_read_radix(r, str, MP_RADIX_HEX);
  36867. if (mp_cmp(a, r) != MP_EQ)
  36868. return -12662;
  36869. }
  36870. }
  36871. if (mp_read_radix(r, badStr1, MP_RADIX_HEX) != MP_VAL)
  36872. return -12663;
  36873. if (mp_read_radix(r, badStr2, MP_RADIX_HEX) != MP_VAL)
  36874. return -12664;
  36875. mp_set(r, 1);
  36876. if (mp_read_radix(r, empty, MP_RADIX_HEX) != MP_OKAY)
  36877. return -12665;
  36878. if (!mp_iszero(r))
  36879. return -12666;
  36880. #if defined(WOLFSSL_SP_MATH) || defined(USE_FAST_MATH)
  36881. /* Fixed MP data size - string can be too long. */
  36882. longStr[0] = '8';
  36883. XMEMSET(longStr+1, '0', sizeof(longStr) - 2);
  36884. longStr[sizeof(longStr)-1] = '\0';
  36885. if (mp_read_radix(r, longStr, MP_RADIX_HEX) != MP_VAL)
  36886. return -12667;
  36887. #endif
  36888. mp_zero(a);
  36889. ret = mp_radix_size(a, MP_RADIX_HEX, &size);
  36890. if (ret != 0)
  36891. return -12668;
  36892. #ifndef WC_DISABLE_RADIX_ZERO_PAD
  36893. if (size != 3)
  36894. #else
  36895. if (size != 2)
  36896. #endif
  36897. return -12669;
  36898. ret = mp_toradix(a, str, MP_RADIX_HEX);
  36899. if (ret != 0)
  36900. return -12670;
  36901. if ((int)XSTRLEN(str) != size - 1)
  36902. return -12671;
  36903. ret = mp_read_radix(r, str, MP_RADIX_HEX);
  36904. if (ret != 0)
  36905. return -12672;
  36906. if (!mp_iszero(r))
  36907. return -12673;
  36908. #ifdef WOLFSSL_SP_MATH
  36909. ret = mp_toradix(a, str, 8);
  36910. if (ret != MP_VAL)
  36911. return -12674;
  36912. ret = mp_radix_size(a, 8, &size);
  36913. if (ret != MP_VAL)
  36914. return -12675;
  36915. #endif
  36916. return 0;
  36917. }
  36918. #endif
  36919. static int mp_test_shift(mp_int* a, mp_int* r1, WC_RNG* rng)
  36920. {
  36921. int i;
  36922. if (randNum(a, 4, rng, NULL) != 0)
  36923. return -12680;
  36924. for (i = 0; i < 4; i++) {
  36925. mp_copy(r1, a);
  36926. if (mp_lshd(r1, i) != MP_OKAY)
  36927. return -12681;
  36928. mp_rshd(r1, i);
  36929. if (mp_cmp(a, r1) != MP_EQ)
  36930. return -12682;
  36931. }
  36932. #ifndef WOLFSSL_SP_MATH
  36933. for (i = 0; i < DIGIT_BIT+1; i++) {
  36934. if (mp_mul_2d(a, i, r1) != MP_OKAY)
  36935. return -12683;
  36936. mp_rshb(r1, i);
  36937. if (mp_cmp(a, r1) != MP_EQ)
  36938. return -12684;
  36939. }
  36940. #endif
  36941. return 0;
  36942. }
  36943. static int mp_test_add_sub_d(mp_int* a, mp_int* r1)
  36944. {
  36945. int i, j;
  36946. for (i = 0; i <= DIGIT_BIT * 2; i++) {
  36947. mp_zero(a);
  36948. mp_set_bit(a, i);
  36949. if (a->used != (i + DIGIT_BIT) / DIGIT_BIT)
  36950. return -12690;
  36951. for (j = 0; j < i && j < DIGIT_BIT; j++) {
  36952. mp_zero(r1);
  36953. mp_set_bit(r1, i);
  36954. if (mp_sub_d(r1, (mp_digit)1 << j, r1) != MP_OKAY)
  36955. return -12691;
  36956. if (mp_add_d(r1, (mp_digit)1 << j, r1) != MP_OKAY)
  36957. return -12692;
  36958. if (mp_cmp(a, r1) != MP_EQ)
  36959. return -12693;
  36960. }
  36961. }
  36962. mp_zero(r1);
  36963. if (mp_add_d(r1, 1, r1) != MP_OKAY)
  36964. return -12694;
  36965. if (r1->used != 1)
  36966. return -12695;
  36967. if (mp_sub_d(r1, 1, r1) != MP_OKAY)
  36968. return -12696;
  36969. if (r1->used != 0)
  36970. return -12697;
  36971. return 0;
  36972. }
  36973. static int mp_test_read_to_bin(mp_int* a)
  36974. {
  36975. WOLFSSL_SMALL_STACK_STATIC const byte in[16] = {
  36976. 0x91, 0xa2, 0xb3, 0xc4, 0xd5, 0xe6, 0xf7, 0x08,
  36977. 0x93, 0xa4, 0xb4, 0xc5, 0xd6, 0xe7, 0xf8, 0x09
  36978. };
  36979. byte out[24];
  36980. int i, j, k;
  36981. const byte* p;
  36982. int ret;
  36983. for (i = 0; i < (int)sizeof(in); i++) {
  36984. p = in + sizeof(in) - i;
  36985. ret = mp_read_unsigned_bin(a, p, i);
  36986. if (ret != 0)
  36987. return -12710;
  36988. for (j = i; j < (int)sizeof(out); j++) {
  36989. XMEMSET(out, 0xff, sizeof(out));
  36990. ret = mp_to_unsigned_bin_len(a, out, j);
  36991. if (ret != 0)
  36992. return -12711;
  36993. for (k = 0; k < j - i; k++) {
  36994. if (out[k] != 0)
  36995. return -12712;
  36996. }
  36997. for (; k < j; k++) {
  36998. if (out[k] != p[k - (j - i)])
  36999. return -12713;
  37000. }
  37001. }
  37002. }
  37003. ret = mp_read_unsigned_bin(a, NULL, 0);
  37004. if (ret != 0)
  37005. return -12714;
  37006. if (!mp_iszero(a))
  37007. return -12715;
  37008. return 0;
  37009. }
  37010. #if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  37011. static int mp_test_set_int(mp_int* a)
  37012. {
  37013. #if SP_ULONG_BITS == 64
  37014. unsigned long n = 0xfedcba9876543210UL;
  37015. byte exp[8] = { 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10 };
  37016. byte out[8] = { 0 };
  37017. #elif SP_ULONG_BITS == 32
  37018. unsigned long n = 0xfedcba98UL;
  37019. byte exp[4] = { 0xfe, 0xdc, 0xba, 0x98 };
  37020. byte out[4] = { 0 };
  37021. #elif SP_ULONG_BITS == 16
  37022. unsigned long n = 0xfedc;
  37023. byte exp[2] = { 0xfe, 0xdc };
  37024. byte out[2] = { 0 };
  37025. #elif SP_ULONG_BITS == 8
  37026. unsigned long n = 0xfe;
  37027. byte exp[1] = { 0xfe };
  37028. byte out[1] = { 0 };
  37029. #endif
  37030. int ret;
  37031. ret = mp_set_int(a, n);
  37032. if (ret != 0)
  37033. return -12720;
  37034. ret = mp_unsigned_bin_size(a);
  37035. if (ret != sizeof(exp))
  37036. return -12721;
  37037. ret = mp_to_unsigned_bin(a, out);
  37038. if (ret != 0)
  37039. return -12722;
  37040. if (XMEMCMP(exp, out, sizeof(exp)) != 0)
  37041. return -12723;
  37042. return 0;
  37043. }
  37044. #endif
  37045. #if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  37046. static int mp_test_param(mp_int* a, mp_int* b, mp_int* r, WC_RNG* rng)
  37047. {
  37048. byte buffer[16];
  37049. #if defined(HAVE_ECC) || defined(WOLFSSL_SP_MATH_ALL)
  37050. char hexStr[] = "abcdef0123456789";
  37051. #ifndef WOLFSSL_SP_INT_NEGATIVE
  37052. char negStr[] = "-1234";
  37053. #endif
  37054. #endif
  37055. #if !defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_KEY_GEN) || \
  37056. defined(HAVE_COMP_KEY)
  37057. char decStr[] = "0987654321";
  37058. #endif
  37059. int ret;
  37060. #ifdef WOLFSSL_SP_MATH_ALL
  37061. mp_digit rho;
  37062. int size;
  37063. #endif
  37064. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH)
  37065. int result;
  37066. #endif
  37067. #if (defined(HAVE_ECC) && defined(HAVE_COMP_KEY)) || \
  37068. (defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN))
  37069. mp_digit rd;
  37070. #endif
  37071. (void)rng;
  37072. (void)r;
  37073. ret = mp_init(NULL);
  37074. if (ret != MP_VAL)
  37075. return -12730;
  37076. #if !defined(WOLFSSL_RSA_PUBLIC_ONLY) || (!defined(NO_DH) || defined(HAVE_ECC))
  37077. ret = mp_init_multi(NULL, NULL, NULL, NULL, NULL, NULL);
  37078. if (ret != MP_OKAY)
  37079. return -12731;
  37080. #endif
  37081. mp_free(NULL);
  37082. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) || !defined(NO_DH) || defined(HAVE_ECC)
  37083. ret = mp_grow(NULL, 1);
  37084. if (ret != MP_VAL)
  37085. return -12732;
  37086. #ifdef WOLFSSL_SP_MATH
  37087. ret = mp_grow(a, SP_INT_DIGITS + 1);
  37088. if (ret != MP_MEM)
  37089. return -12733;
  37090. #endif
  37091. #endif
  37092. mp_clear(NULL);
  37093. ret = mp_abs(NULL, NULL);
  37094. if (ret != MP_VAL)
  37095. return -12734;
  37096. ret = mp_abs(a, NULL);
  37097. if (ret != MP_VAL)
  37098. return -12735;
  37099. ret = mp_abs(NULL, b);
  37100. if (ret != MP_VAL)
  37101. return -12736;
  37102. ret = mp_unsigned_bin_size(NULL);
  37103. if (ret != 0)
  37104. return -12737;
  37105. ret = mp_read_unsigned_bin(NULL, NULL, sizeof(buffer));
  37106. if (ret != MP_VAL)
  37107. return -12738;
  37108. ret = mp_read_unsigned_bin(NULL, buffer, sizeof(buffer));
  37109. if (ret != MP_VAL)
  37110. return -12739;
  37111. ret = mp_read_unsigned_bin(a, NULL, sizeof(buffer));
  37112. if (ret != MP_VAL)
  37113. return -12740;
  37114. ret = mp_read_unsigned_bin(a, buffer, SP_INT_DIGITS * SP_WORD_SIZEOF + 1);
  37115. if (ret != MP_VAL)
  37116. return -12741;
  37117. #if defined(HAVE_ECC) || defined(WOLFSSL_SP_MATH_ALL)
  37118. ret = mp_read_radix(NULL, NULL, 16);
  37119. if (ret != MP_VAL)
  37120. return -12742;
  37121. ret = mp_read_radix(a, NULL, 16);
  37122. if (ret != MP_VAL)
  37123. return -12743;
  37124. ret = mp_read_radix(NULL, hexStr, 16);
  37125. if (ret != MP_VAL)
  37126. return -12744;
  37127. #ifndef WOLFSSL_SP_INT_NEGATIVE
  37128. ret = mp_read_radix(a, negStr, 16);
  37129. if (ret != MP_VAL)
  37130. return -12745;
  37131. #ifdef WOLFSSL_SP_MATH_ALL
  37132. ret = mp_read_radix(a, negStr, 10);
  37133. if (ret != MP_VAL)
  37134. return -12746;
  37135. #endif /* WOLFSSL_SP_MATH_ALL */
  37136. #endif /* WOLFSSL_SP_INT_NEGATIVE */
  37137. #endif
  37138. #ifndef WOLFSSL_SP_MATH_ALL
  37139. /* Radix 10 only supported with ALL. */
  37140. ret = mp_read_radix(a, decStr, 10);
  37141. if (ret != MP_VAL)
  37142. return -12747;
  37143. #endif
  37144. /* Radix 8 not supported SP_INT. */
  37145. ret = mp_read_radix(a, "0123", 8);
  37146. if (ret != MP_VAL)
  37147. return -12748;
  37148. ret = mp_count_bits(NULL);
  37149. if (ret != 0)
  37150. return -12749;
  37151. ret = mp_is_bit_set(NULL, 0);
  37152. if (ret != 0)
  37153. return -12750;
  37154. ret = mp_leading_bit(NULL);
  37155. if (ret != 0)
  37156. return -12751;
  37157. mp_zero(a);
  37158. ret = mp_leading_bit(a);
  37159. if (ret != 0)
  37160. return -12752;
  37161. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH) || \
  37162. defined(HAVE_ECC) || defined(WOLFSSL_KEY_GEN) || defined(OPENSSL_EXTRA) || \
  37163. !defined(NO_RSA)
  37164. ret = mp_set_bit(NULL, 1);
  37165. if (ret != MP_VAL)
  37166. return -12753;
  37167. #endif
  37168. #if !defined(NO_DH) || defined(HAVE_ECC) || defined(WC_RSA_BLINDING) || \
  37169. !defined(WOLFSSL_RSA_VERIFY_ONLY)
  37170. ret = mp_to_unsigned_bin(NULL, NULL);
  37171. if (ret != MP_VAL)
  37172. return -12754;
  37173. ret = mp_to_unsigned_bin(a, NULL);
  37174. if (ret != MP_VAL)
  37175. return -12755;
  37176. ret = mp_to_unsigned_bin(NULL, buffer);
  37177. if (ret != MP_VAL)
  37178. return -12756;
  37179. #endif
  37180. ret = mp_to_unsigned_bin_len(NULL, NULL, 1);
  37181. if (ret != MP_VAL)
  37182. return -12757;
  37183. ret = mp_to_unsigned_bin_len(a, NULL, 1);
  37184. if (ret != MP_VAL)
  37185. return -12758;
  37186. ret = mp_to_unsigned_bin_len(NULL, buffer, 1);
  37187. if (ret != MP_VAL)
  37188. return -12759;
  37189. #if defined(WOLFSSL_SP_MATH_ALL) && !defined(NO_RSA) && \
  37190. !defined(WOLFSSL_RSA_VERIFY_ONLY)
  37191. ret = mp_to_unsigned_bin_at_pos(0, NULL, NULL);
  37192. if (ret != MP_VAL)
  37193. return -12760;
  37194. ret = mp_to_unsigned_bin_at_pos(0, a, NULL);
  37195. if (ret != MP_VAL)
  37196. return -12761;
  37197. ret = mp_to_unsigned_bin_at_pos(0, NULL, buffer);
  37198. if (ret != MP_VAL)
  37199. return -12762;
  37200. ret = mp_to_unsigned_bin_at_pos(0, a, buffer);
  37201. if (ret != MP_OKAY)
  37202. return -12763;
  37203. #endif
  37204. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) || (!defined(NO_DH) || defined(HAVE_ECC))
  37205. ret = mp_copy(NULL, NULL);
  37206. if (ret != MP_VAL)
  37207. return -12764;
  37208. ret = mp_copy(a, NULL);
  37209. if (ret != MP_VAL)
  37210. return -12765;
  37211. ret = mp_copy(NULL, b);
  37212. if (ret != MP_VAL)
  37213. return -12766;
  37214. #endif
  37215. #if defined(WOLFSSL_KEY_GEN) || !defined(NO_DH)
  37216. ret = sp_2expt(NULL, 1);
  37217. if (ret != MP_VAL)
  37218. return -12767;
  37219. #endif
  37220. ret = mp_set(NULL, 0);
  37221. if (ret != MP_VAL)
  37222. return -12768;
  37223. ret = mp_cmp_d(NULL, 0);
  37224. if (ret != MP_LT)
  37225. return -12769;
  37226. ret = mp_cmp(NULL, NULL);
  37227. if (ret != MP_EQ)
  37228. return -12770;
  37229. ret = mp_cmp(a, NULL);
  37230. if (ret != MP_GT)
  37231. return -12771;
  37232. ret = mp_cmp(NULL, b);
  37233. if (ret != MP_LT)
  37234. return -12772;
  37235. #if !defined(NO_DH) || defined(HAVE_ECC) || !defined(WOLFSSL_RSA_VERIFY_ONLY)
  37236. mp_rshd(NULL, 1);
  37237. #endif
  37238. mp_zero(NULL);
  37239. #if !defined(NO_DH) || defined(HAVE_ECC) || defined(WC_RSA_BLINDING) || \
  37240. !defined(WOLFSSL_RSA_VERIFY_ONLY)
  37241. ret = mp_lshd(NULL, 0);
  37242. if (ret != MP_VAL)
  37243. return -12773;
  37244. ret = mp_lshd(a, SP_INT_DIGITS + 1);
  37245. if (ret != MP_VAL)
  37246. return -12774;
  37247. #endif
  37248. #if defined(WOLFSSL_SP_MATH_ALL)
  37249. ret = mp_div(NULL, NULL, a, b);
  37250. if (ret != MP_VAL)
  37251. return -12775;
  37252. ret = mp_div(a, NULL, a, b);
  37253. if (ret != MP_VAL)
  37254. return -12776;
  37255. ret = mp_div(NULL, b, a, b);
  37256. if (ret != MP_VAL)
  37257. return -12777;
  37258. ret = mp_div(a, b, NULL, NULL);
  37259. if (ret != MP_VAL)
  37260. return -12778;
  37261. #endif
  37262. #if defined(WOLFSSL_SP_MATH_ALL) || !defined(NO_DH) || defined(HAVE_ECC) || \
  37263. (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY))
  37264. ret = mp_mod(NULL, NULL, NULL);
  37265. if (ret != MP_VAL)
  37266. return -12779;
  37267. ret = mp_mod(a, NULL, NULL);
  37268. if (ret != MP_VAL)
  37269. return -12780;
  37270. ret = mp_mod(NULL, b, NULL);
  37271. if (ret != MP_VAL)
  37272. return -12781;
  37273. ret = mp_mod(NULL, NULL, r);
  37274. if (ret != MP_VAL)
  37275. return -12782;
  37276. ret = mp_mod(a, b, NULL);
  37277. if (ret != MP_VAL)
  37278. return -12783;
  37279. ret = mp_mod(a, NULL, r);
  37280. if (ret != MP_VAL)
  37281. return -12784;
  37282. ret = mp_mod(NULL, b, r);
  37283. if (ret != MP_VAL)
  37284. return -12785;
  37285. #endif
  37286. #if !defined(NO_RSA) || defined(WOLFSSL_SP_MATH_ALL)
  37287. ret = mp_set_int(NULL, 0);
  37288. if (ret != MP_VAL)
  37289. return -12786;
  37290. #endif
  37291. #if !defined(NO_RSA) || !defined(NO_DSA) || !defined(NO_DH) || \
  37292. (defined(HAVE_ECC) && defined(HAVE_COMP_KEY)) || defined(OPENSSL_EXTRA)
  37293. ret = mp_exptmod_ex(NULL, NULL, 1, NULL, NULL);
  37294. if (ret != MP_VAL)
  37295. return 9950;
  37296. ret = mp_exptmod_ex(a, NULL, 1, NULL, NULL);
  37297. if (ret != MP_VAL)
  37298. return 9951;
  37299. ret = mp_exptmod_ex(NULL, a, 1, NULL, NULL);
  37300. if (ret != MP_VAL)
  37301. return 9952;
  37302. ret = mp_exptmod_ex(NULL, NULL, 1, a, NULL);
  37303. if (ret != MP_VAL)
  37304. return 9953;
  37305. ret = mp_exptmod_ex(NULL, NULL, 1, NULL, a);
  37306. if (ret != MP_VAL)
  37307. return 9954;
  37308. ret = mp_exptmod_ex(a, a, 1, a, NULL);
  37309. if (ret != MP_VAL)
  37310. return 9955;
  37311. ret = mp_exptmod_ex(a, a, 1, NULL, a);
  37312. if (ret != MP_VAL)
  37313. return 9956;
  37314. ret = mp_exptmod_ex(a, NULL, 1, a, a);
  37315. if (ret != MP_VAL)
  37316. return 9957;
  37317. ret = mp_exptmod_ex(NULL, a, 1, a, a);
  37318. if (ret != MP_VAL)
  37319. return 9958;
  37320. ret = mp_exptmod_nct(NULL, NULL, NULL, NULL);
  37321. if (ret != MP_VAL)
  37322. return 9960;
  37323. ret = mp_exptmod_nct(a, NULL, NULL, NULL);
  37324. if (ret != MP_VAL)
  37325. return 9961;
  37326. ret = mp_exptmod_nct(NULL, a, NULL, NULL);
  37327. if (ret != MP_VAL)
  37328. return 9962;
  37329. ret = mp_exptmod_nct(NULL, NULL, a, NULL);
  37330. if (ret != MP_VAL)
  37331. return 9963;
  37332. ret = mp_exptmod_nct(NULL, NULL, NULL, a);
  37333. if (ret != MP_VAL)
  37334. return 9964;
  37335. ret = mp_exptmod_nct(a, a, a, NULL);
  37336. if (ret != MP_VAL)
  37337. return 9965;
  37338. ret = mp_exptmod_nct(a, a, NULL, a);
  37339. if (ret != MP_VAL)
  37340. return 9966;
  37341. ret = mp_exptmod_nct(a, NULL, a, a);
  37342. if (ret != MP_VAL)
  37343. return 9967;
  37344. ret = mp_exptmod_nct(NULL, a, a, a);
  37345. if (ret != MP_VAL)
  37346. return 9968;
  37347. #endif
  37348. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_DH) || !defined(NO_DSA)) && \
  37349. !defined(WC_NO_RNG)
  37350. ret = mp_rand_prime(NULL, 32, NULL, NULL);
  37351. if (ret != MP_VAL)
  37352. return -12787;
  37353. ret = mp_rand_prime(a, 32, NULL, NULL);
  37354. if (ret != MP_VAL)
  37355. return -12788;
  37356. ret = mp_rand_prime(NULL, 32, rng, NULL);
  37357. if (ret != MP_VAL)
  37358. return -12789;
  37359. ret = mp_rand_prime(a, 0, rng, NULL);
  37360. if (ret != MP_VAL)
  37361. return -9969;
  37362. #endif
  37363. #if defined(WOLFSSL_SP_MATH_ALL) && !defined(WOLFSSL_RSA_VERIFY_ONLY)
  37364. ret = mp_mul(NULL, NULL, NULL);
  37365. if (ret != MP_VAL)
  37366. return -12790;
  37367. ret = mp_mul(a, NULL, NULL);
  37368. if (ret != MP_VAL)
  37369. return -12791;
  37370. ret = mp_mul(NULL, b, NULL);
  37371. if (ret != MP_VAL)
  37372. return -12792;
  37373. ret = mp_mul(NULL, NULL, r);
  37374. if (ret != MP_VAL)
  37375. return -12793;
  37376. ret = mp_mul(a, b, NULL);
  37377. if (ret != MP_VAL)
  37378. return -12794;
  37379. ret = mp_mul(a, NULL, r);
  37380. if (ret != MP_VAL)
  37381. return -12795;
  37382. ret = mp_mul(NULL, b, r);
  37383. if (ret != MP_VAL)
  37384. return -12796;
  37385. #endif
  37386. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH) || \
  37387. defined(HAVE_ECC) || (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY))
  37388. ret = mp_sqr(NULL, NULL);
  37389. if (ret != MP_VAL)
  37390. return -12797;
  37391. ret = mp_sqr(a, NULL);
  37392. if (ret != MP_VAL)
  37393. return -12798;
  37394. ret = mp_sqr(NULL, r);
  37395. if (ret != MP_VAL)
  37396. return -12799;
  37397. #endif
  37398. #if !defined(WOLFSSL_RSA_VERIFY_ONLY)
  37399. ret = mp_sqrmod(NULL, NULL, NULL);
  37400. if (ret != MP_VAL)
  37401. return -12800;
  37402. ret = mp_sqrmod(a, NULL, NULL);
  37403. if (ret != MP_VAL)
  37404. return -12801;
  37405. ret = mp_sqrmod(NULL, a, NULL);
  37406. if (ret != MP_VAL)
  37407. return -12802;
  37408. ret = mp_sqrmod(NULL, NULL, a);
  37409. if (ret != MP_VAL)
  37410. return -12803;
  37411. ret = mp_sqrmod(a, b, NULL);
  37412. if (ret != MP_VAL)
  37413. return -12804;
  37414. ret = mp_sqrmod(a, NULL, b);
  37415. if (ret != MP_VAL)
  37416. return -12805;
  37417. ret = mp_sqrmod(NULL, a, b);
  37418. if (ret != MP_VAL)
  37419. return -12806;
  37420. ret = mp_mulmod(NULL, NULL, NULL, NULL);
  37421. if (ret != MP_VAL)
  37422. return -12807;
  37423. ret = mp_mulmod(a, NULL, NULL, NULL);
  37424. if (ret != MP_VAL)
  37425. return -12808;
  37426. ret = mp_mulmod(NULL, a, NULL, NULL);
  37427. if (ret != MP_VAL)
  37428. return -12809;
  37429. ret = mp_mulmod(NULL, NULL, a, NULL);
  37430. if (ret != MP_VAL)
  37431. return -12810;
  37432. ret = mp_mulmod(NULL, NULL, NULL, a);
  37433. if (ret != MP_VAL)
  37434. return -12811;
  37435. ret = mp_mulmod(a, b, b, NULL);
  37436. if (ret != MP_VAL)
  37437. return -12812;
  37438. ret = mp_mulmod(a, b, NULL, a);
  37439. if (ret != MP_VAL)
  37440. return -12813;
  37441. ret = mp_mulmod(a, NULL, b, a);
  37442. if (ret != MP_VAL)
  37443. return -12814;
  37444. ret = mp_mulmod(NULL, b, b, a);
  37445. if (ret != MP_VAL)
  37446. return -12815;
  37447. #endif
  37448. #if !defined(NO_PWDBASED) || defined(WOLFSSL_KEY_GEN) || !defined(NO_DH) || \
  37449. !defined(NO_RSA) || !defined(NO_DSA)
  37450. ret = mp_add_d(NULL, 1, NULL);
  37451. if (ret != MP_VAL)
  37452. return -12816;
  37453. ret = mp_add_d(a, 1, NULL);
  37454. if (ret != MP_VAL)
  37455. return -12817;
  37456. ret = mp_add_d(NULL, 1, b);
  37457. if (ret != MP_VAL)
  37458. return -12818;
  37459. #endif
  37460. #if (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
  37461. !defined(NO_DH) || defined(HAVE_ECC) || !defined(NO_DSA)
  37462. ret = mp_sub_d(NULL, 1, NULL);
  37463. if (ret != MP_VAL)
  37464. return -12819;
  37465. ret = mp_sub_d(a, 1, NULL);
  37466. if (ret != MP_VAL)
  37467. return -12820;
  37468. ret = mp_sub_d(NULL, 1, b);
  37469. if (ret != MP_VAL)
  37470. return -12821;
  37471. #endif
  37472. #if (defined(WOLFSSL_SP_MATH_ALL) && !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
  37473. defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  37474. ret = mp_div_d(NULL, 0, NULL, NULL);
  37475. if (ret != MP_VAL)
  37476. return -12822;
  37477. ret = mp_div_d(a, 0, NULL, NULL);
  37478. if (ret != MP_VAL)
  37479. return -12823;
  37480. ret = mp_div_d(NULL, 1, NULL, NULL);
  37481. if (ret != MP_VAL)
  37482. return -12824;
  37483. #endif
  37484. #if (defined(HAVE_ECC) && defined(HAVE_COMP_KEY)) || \
  37485. (defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN))
  37486. ret = mp_mod_d(NULL, 0, NULL);
  37487. if (ret != MP_VAL)
  37488. return -12825;
  37489. ret = mp_mod_d(a, 0, NULL);
  37490. if (ret != MP_VAL)
  37491. return -12826;
  37492. ret = mp_mod_d(NULL, 0, &rd);
  37493. if (ret != MP_VAL)
  37494. return -12827;
  37495. #endif
  37496. #if defined(WOLFSSL_SP_MATH_ALL) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  37497. ret = mp_gcd(NULL, NULL, NULL);
  37498. if (ret != MP_VAL)
  37499. return -12828;
  37500. ret = mp_gcd(a, NULL, NULL);
  37501. if (ret != MP_VAL)
  37502. return -12829;
  37503. ret = mp_gcd(NULL, a, NULL);
  37504. if (ret != MP_VAL)
  37505. return -12830;
  37506. ret = mp_gcd(NULL, NULL, a);
  37507. if (ret != MP_VAL)
  37508. return -12831;
  37509. ret = mp_gcd(a, b, NULL);
  37510. if (ret != MP_VAL)
  37511. return -12832;
  37512. ret = mp_gcd(a, NULL, b);
  37513. if (ret != MP_VAL)
  37514. return -12833;
  37515. ret = mp_gcd(NULL, a, b);
  37516. if (ret != MP_VAL)
  37517. return -12834;
  37518. #endif
  37519. #if !defined(WOLFSSL_SP_MATH) && defined(HAVE_ECC)
  37520. ret = mp_div_2_mod_ct(NULL, NULL, NULL);
  37521. if (ret != MP_VAL)
  37522. return -12835;
  37523. ret = mp_div_2_mod_ct(a, NULL, NULL);
  37524. if (ret != MP_VAL)
  37525. return -12836;
  37526. ret = mp_div_2_mod_ct(NULL, b, NULL);
  37527. if (ret != MP_VAL)
  37528. return -12837;
  37529. ret = mp_div_2_mod_ct(NULL, NULL, a);
  37530. if (ret != MP_VAL)
  37531. return -12838;
  37532. ret = mp_div_2_mod_ct(a, b, NULL);
  37533. if (ret != MP_VAL)
  37534. return -12839;
  37535. ret = mp_div_2_mod_ct(a, b, NULL);
  37536. if (ret != MP_VAL)
  37537. return -12840;
  37538. ret = mp_div_2_mod_ct(NULL, b, a);
  37539. if (ret != MP_VAL)
  37540. return -12841;
  37541. ret = mp_div_2(NULL, NULL);
  37542. if (ret != MP_VAL)
  37543. return -12842;
  37544. ret = mp_div_2(a, NULL);
  37545. if (ret != MP_VAL)
  37546. return -12843;
  37547. ret = mp_div_2(NULL, a);
  37548. if (ret != MP_VAL)
  37549. return -12844;
  37550. #endif
  37551. #if (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
  37552. defined(HAVE_ECC) || !defined(NO_DSA) || defined(OPENSSL_EXTRA)
  37553. ret = mp_invmod(NULL, NULL, NULL);
  37554. if (ret != MP_VAL)
  37555. return -12845;
  37556. ret = mp_invmod(a, NULL, NULL);
  37557. if (ret != MP_VAL)
  37558. return -12846;
  37559. ret = mp_invmod(NULL, b, NULL);
  37560. if (ret != MP_VAL)
  37561. return -12847;
  37562. ret = mp_invmod(NULL, NULL, a);
  37563. if (ret != MP_VAL)
  37564. return -12848;
  37565. ret = mp_invmod(a, b, NULL);
  37566. if (ret != MP_VAL)
  37567. return -12849;
  37568. ret = mp_invmod(a, NULL, a);
  37569. if (ret != MP_VAL)
  37570. return -12850;
  37571. ret = mp_invmod(NULL, b, a);
  37572. if (ret != MP_VAL)
  37573. return -12851;
  37574. #endif
  37575. #if !defined(WOLFSSL_SP_MATH) && defined(HAVE_ECC)
  37576. ret = mp_invmod_mont_ct(NULL, NULL, NULL, 1);
  37577. if (ret != MP_VAL)
  37578. return -12852;
  37579. ret = mp_invmod_mont_ct(a, NULL, NULL, 1);
  37580. if (ret != MP_VAL)
  37581. return -12853;
  37582. ret = mp_invmod_mont_ct(NULL, b, NULL, 1);
  37583. if (ret != MP_VAL)
  37584. return -12854;
  37585. ret = mp_invmod_mont_ct(NULL, NULL, a, 1);
  37586. if (ret != MP_VAL)
  37587. return -12855;
  37588. ret = mp_invmod_mont_ct(a, b, NULL, 1);
  37589. if (ret != MP_VAL)
  37590. return -12856;
  37591. ret = mp_invmod_mont_ct(a, NULL, a, 1);
  37592. if (ret != MP_VAL)
  37593. return -12857;
  37594. ret = mp_invmod_mont_ct(NULL, b, a, 1);
  37595. if (ret != MP_VAL)
  37596. return -12858;
  37597. #endif
  37598. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(WC_RSA_BLINDING)
  37599. ret = mp_lcm(NULL, NULL, NULL);
  37600. if (ret != MP_VAL)
  37601. return -12859;
  37602. ret = mp_lcm(a, NULL, NULL);
  37603. if (ret != MP_VAL)
  37604. return -12860;
  37605. ret = mp_lcm(NULL, b, NULL);
  37606. if (ret != MP_VAL)
  37607. return -12861;
  37608. ret = mp_lcm(NULL, NULL, a);
  37609. if (ret != MP_VAL)
  37610. return -12862;
  37611. ret = mp_lcm(a, b, NULL);
  37612. if (ret != MP_VAL)
  37613. return -12863;
  37614. ret = mp_lcm(a, NULL, a);
  37615. if (ret != MP_VAL)
  37616. return -12864;
  37617. ret = mp_lcm(NULL, b, a);
  37618. if (ret != MP_VAL)
  37619. return -12865;
  37620. #endif
  37621. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH)
  37622. ret = mp_exptmod_ex(NULL, NULL, 1, NULL, NULL);
  37623. if (ret != MP_VAL)
  37624. return -12866;
  37625. ret = mp_exptmod_ex(a, NULL, 1, NULL, NULL);
  37626. if (ret != MP_VAL)
  37627. return -12867;
  37628. ret = mp_exptmod_ex(NULL, b, 1, NULL, NULL);
  37629. if (ret != MP_VAL)
  37630. return -12868;
  37631. ret = mp_exptmod_ex(NULL, NULL, 1, b, NULL);
  37632. if (ret != MP_VAL)
  37633. return -12869;
  37634. ret = mp_exptmod_ex(NULL, NULL, 1, NULL, a);
  37635. if (ret != MP_VAL)
  37636. return -12870;
  37637. ret = mp_exptmod_ex(a, b, 1, b, NULL);
  37638. if (ret != MP_VAL)
  37639. return -12871;
  37640. ret = mp_exptmod_ex(a, b, 1, NULL, a);
  37641. if (ret != MP_VAL)
  37642. return -12872;
  37643. ret = mp_exptmod_ex(a, NULL, 1, b, a);
  37644. if (ret != MP_VAL)
  37645. return -12873;
  37646. ret = mp_exptmod_ex(NULL, b, 1, b, a);
  37647. if (ret != MP_VAL)
  37648. return -12874;
  37649. ret = mp_exptmod(NULL, NULL, NULL, NULL);
  37650. if (ret != MP_VAL)
  37651. return -12875;
  37652. ret = mp_exptmod(a, NULL, NULL, NULL);
  37653. if (ret != MP_VAL)
  37654. return -12876;
  37655. ret = mp_exptmod(NULL, b, NULL, NULL);
  37656. if (ret != MP_VAL)
  37657. return -12877;
  37658. ret = mp_exptmod(NULL, NULL, b, NULL);
  37659. if (ret != MP_VAL)
  37660. return -12878;
  37661. ret = mp_exptmod(NULL, NULL, NULL, a);
  37662. if (ret != MP_VAL)
  37663. return -12879;
  37664. ret = mp_exptmod(a, b, b, NULL);
  37665. if (ret != MP_VAL)
  37666. return -12880;
  37667. ret = mp_exptmod(a, b, NULL, a);
  37668. if (ret != MP_VAL)
  37669. return -12881;
  37670. ret = mp_exptmod(a, NULL, b, a);
  37671. if (ret != MP_VAL)
  37672. return -12882;
  37673. ret = mp_exptmod(NULL, b, b, a);
  37674. if (ret != MP_VAL)
  37675. return -12883;
  37676. ret = mp_exptmod_nct(NULL, NULL, NULL, NULL);
  37677. if (ret != MP_VAL)
  37678. return -12884;
  37679. ret = mp_exptmod_nct(a, NULL, NULL, NULL);
  37680. if (ret != MP_VAL)
  37681. return -12885;
  37682. ret = mp_exptmod_nct(NULL, b, NULL, NULL);
  37683. if (ret != MP_VAL)
  37684. return -12886;
  37685. ret = mp_exptmod_nct(NULL, NULL, b, NULL);
  37686. if (ret != MP_VAL)
  37687. return -12887;
  37688. ret = mp_exptmod_nct(NULL, NULL, NULL, a);
  37689. if (ret != MP_VAL)
  37690. return -12888;
  37691. ret = mp_exptmod_nct(a, b, b, NULL);
  37692. if (ret != MP_VAL)
  37693. return -12889;
  37694. ret = mp_exptmod_nct(a, b, NULL, a);
  37695. if (ret != MP_VAL)
  37696. return -12890;
  37697. ret = mp_exptmod_nct(a, NULL, b, a);
  37698. if (ret != MP_VAL)
  37699. return -12891;
  37700. ret = mp_exptmod_nct(NULL, b, b, a);
  37701. if (ret != MP_VAL)
  37702. return -12892;
  37703. #endif
  37704. #if defined(HAVE_ECC) && defined(HAVE_COMP_KEY)
  37705. ret = mp_cnt_lsb(NULL);
  37706. if (ret != 0)
  37707. return -12893;
  37708. #endif
  37709. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH)
  37710. ret = mp_prime_is_prime(NULL, 1, NULL);
  37711. if (ret != MP_VAL)
  37712. return -12894;
  37713. ret = mp_prime_is_prime(a, 1, NULL);
  37714. if (ret != MP_VAL)
  37715. return -12895;
  37716. ret = mp_prime_is_prime(NULL, 1, &result);
  37717. if (ret != MP_VAL)
  37718. return -12896;
  37719. ret = mp_prime_is_prime(a, 0, &result);
  37720. if (ret != MP_VAL)
  37721. return -12897;
  37722. ret = mp_prime_is_prime(a, 1024, &result);
  37723. if (ret != MP_VAL)
  37724. return -12898;
  37725. ret = mp_prime_is_prime_ex(NULL, 1, NULL, NULL);
  37726. if (ret != MP_VAL)
  37727. return -12899;
  37728. ret = mp_prime_is_prime_ex(a, 1, NULL, NULL);
  37729. if (ret != MP_VAL)
  37730. return -12900;
  37731. ret = mp_prime_is_prime_ex(NULL, 1, &result, NULL);
  37732. if (ret != MP_VAL)
  37733. return -12901;
  37734. ret = mp_prime_is_prime_ex(NULL, 1, NULL, rng);
  37735. if (ret != MP_VAL)
  37736. return -12902;
  37737. ret = mp_prime_is_prime_ex(a, 1, &result, NULL);
  37738. if (ret != MP_VAL)
  37739. return -12903;
  37740. ret = mp_prime_is_prime_ex(a, 1, NULL, rng);
  37741. if (ret != MP_VAL)
  37742. return -12904;
  37743. ret = mp_prime_is_prime_ex(NULL, 1, &result, rng);
  37744. if (ret != MP_VAL)
  37745. return -12905;
  37746. #endif
  37747. #if defined(WOLFSSL_SP_MATH_ALL) || !defined(NO_DH) || !defined(NO_DSA)
  37748. ret = mp_exch(NULL, NULL);
  37749. if (ret != MP_VAL)
  37750. return -12906;
  37751. ret = mp_exch(a, NULL);
  37752. if (ret != MP_VAL)
  37753. return -12907;
  37754. ret = mp_exch(NULL, b);
  37755. if (ret != MP_VAL)
  37756. return -12908;
  37757. #endif
  37758. #if (defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA)) || \
  37759. defined(WOLFSSL_SP_MATH_ALL)
  37760. ret = mp_mul_d(NULL, 1, NULL);
  37761. if (ret != MP_VAL)
  37762. return -12909;
  37763. ret = mp_mul_d(a, 1, NULL);
  37764. if (ret != MP_VAL)
  37765. return -12910;
  37766. ret = mp_mul_d(NULL, 1, b);
  37767. if (ret != MP_VAL)
  37768. return -12911;
  37769. #endif
  37770. #if !defined(WOLFSSL_RSA_VERIFY_ONLY)
  37771. ret = mp_add(NULL, NULL, NULL);
  37772. if (ret != MP_VAL)
  37773. return -12912;
  37774. ret = mp_add(a, NULL, NULL);
  37775. if (ret != MP_VAL)
  37776. return -12913;
  37777. ret = mp_add(NULL, b, NULL);
  37778. if (ret != MP_VAL)
  37779. return -12914;
  37780. ret = mp_add(NULL, NULL, r);
  37781. if (ret != MP_VAL)
  37782. return -12915;
  37783. ret = mp_add(a, b, NULL);
  37784. if (ret != MP_VAL)
  37785. return -12916;
  37786. ret = mp_add(a, NULL, r);
  37787. if (ret != MP_VAL)
  37788. return -12917;
  37789. ret = mp_add(NULL, b, r);
  37790. if (ret != MP_VAL)
  37791. return -12918;
  37792. #endif
  37793. #if defined(WOLFSSL_SP_MATH_ALL) || !defined(NO_DH) || defined(HAVE_ECC) || \
  37794. (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY))
  37795. ret = mp_sub(NULL, NULL, NULL);
  37796. if (ret != MP_VAL)
  37797. return -12919;
  37798. ret = mp_sub(a, NULL, NULL);
  37799. if (ret != MP_VAL)
  37800. return -12920;
  37801. ret = mp_sub(NULL, b, NULL);
  37802. if (ret != MP_VAL)
  37803. return -12921;
  37804. ret = mp_sub(NULL, NULL, r);
  37805. if (ret != MP_VAL)
  37806. return -12922;
  37807. ret = mp_sub(a, b, NULL);
  37808. if (ret != MP_VAL)
  37809. return -12923;
  37810. ret = mp_sub(a, NULL, r);
  37811. if (ret != MP_VAL)
  37812. return -12924;
  37813. ret = mp_sub(NULL, b, r);
  37814. if (ret != MP_VAL)
  37815. return -12925;
  37816. #endif
  37817. #if defined(WOLFSSL_SP_MATH_ALL) || (!defined(WOLFSSL_SP_MATH) && \
  37818. defined(WOLFSSL_CUSTOM_CURVES))
  37819. ret = mp_addmod(NULL, NULL, NULL, NULL);
  37820. if (ret != MP_VAL)
  37821. return -12926;
  37822. ret = mp_addmod(a, NULL, NULL, NULL);
  37823. if (ret != MP_VAL)
  37824. return -12927;
  37825. ret = mp_addmod(NULL, b, NULL, NULL);
  37826. if (ret != MP_VAL)
  37827. return -12928;
  37828. ret = mp_addmod(NULL, NULL, b, NULL);
  37829. if (ret != MP_VAL)
  37830. return -12929;
  37831. ret = mp_addmod(NULL, NULL, NULL, a);
  37832. if (ret != MP_VAL)
  37833. return -12930;
  37834. ret = mp_addmod(a, b, b, NULL);
  37835. if (ret != MP_VAL)
  37836. return -12931;
  37837. ret = mp_addmod(a, b, NULL, a);
  37838. if (ret != MP_VAL)
  37839. return -12932;
  37840. ret = mp_addmod(a, NULL, b, a);
  37841. if (ret != MP_VAL)
  37842. return -12933;
  37843. ret = mp_addmod(NULL, b, b, a);
  37844. if (ret != MP_VAL)
  37845. return -12934;
  37846. #endif
  37847. #ifdef WOLFSSL_SP_MATH_ALL
  37848. ret = mp_submod(NULL, NULL, NULL, NULL);
  37849. if (ret != MP_VAL)
  37850. return -12935;
  37851. ret = mp_submod(a, NULL, NULL, NULL);
  37852. if (ret != MP_VAL)
  37853. return -12936;
  37854. ret = mp_submod(NULL, b, NULL, NULL);
  37855. if (ret != MP_VAL)
  37856. return -12937;
  37857. ret = mp_submod(NULL, NULL, b, NULL);
  37858. if (ret != MP_VAL)
  37859. return -12938;
  37860. ret = mp_submod(NULL, NULL, NULL, a);
  37861. if (ret != MP_VAL)
  37862. return -12939;
  37863. ret = mp_submod(a, b, b, NULL);
  37864. if (ret != MP_VAL)
  37865. return -12940;
  37866. ret = mp_submod(a, b, NULL, a);
  37867. if (ret != MP_VAL)
  37868. return -12941;
  37869. ret = mp_submod(a, NULL, b, a);
  37870. if (ret != MP_VAL)
  37871. return -12942;
  37872. ret = mp_submod(NULL, b, b, a);
  37873. if (ret != MP_VAL)
  37874. return -12943;
  37875. #endif
  37876. #ifdef WOLFSSL_SP_MATH_ALL
  37877. ret = mp_div_2d(NULL, 1, a, b);
  37878. if (ret != MP_VAL)
  37879. return -12944;
  37880. ret = mp_mod_2d(NULL, 1, NULL);
  37881. if (ret != MP_VAL)
  37882. return -12945;
  37883. ret = mp_mod_2d(a, 1, NULL);
  37884. if (ret != MP_VAL)
  37885. return -12946;
  37886. ret = mp_mod_2d(NULL, 1, b);
  37887. if (ret != MP_VAL)
  37888. return -12947;
  37889. ret = mp_mul_2d(NULL, 1, NULL);
  37890. if (ret != MP_VAL)
  37891. return -12948;
  37892. ret = mp_mul_2d(a, 1, NULL);
  37893. if (ret != MP_VAL)
  37894. return -12949;
  37895. ret = mp_mul_2d(NULL, 1, b);
  37896. if (ret != MP_VAL)
  37897. return -12950;
  37898. #endif
  37899. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH) || \
  37900. defined(HAVE_ECC) || (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY))
  37901. ret = mp_montgomery_reduce(NULL, NULL, 1);
  37902. if (ret != MP_VAL)
  37903. return -12951;
  37904. ret = mp_montgomery_reduce(a, NULL, 1);
  37905. if (ret != MP_VAL)
  37906. return -12952;
  37907. ret = mp_montgomery_reduce(NULL, b, 1);
  37908. if (ret != MP_VAL)
  37909. return -12953;
  37910. mp_zero(b);
  37911. ret = mp_montgomery_reduce(a, b, 1);
  37912. if (ret != MP_VAL)
  37913. return -12954;
  37914. #endif
  37915. #ifdef WOLFSSL_SP_MATH_ALL
  37916. ret = mp_montgomery_setup(NULL, NULL);
  37917. if (ret != MP_VAL)
  37918. return -12955;
  37919. ret = mp_montgomery_setup(a, NULL);
  37920. if (ret != MP_VAL)
  37921. return -12956;
  37922. ret = mp_montgomery_setup(NULL, &rho);
  37923. if (ret != MP_VAL)
  37924. return -12957;
  37925. ret = mp_montgomery_calc_normalization(NULL, NULL);
  37926. if (ret != MP_VAL)
  37927. return -12958;
  37928. ret = mp_montgomery_calc_normalization(a, NULL);
  37929. if (ret != MP_VAL)
  37930. return -12959;
  37931. ret = mp_montgomery_calc_normalization(NULL, b);
  37932. if (ret != MP_VAL)
  37933. return -12960;
  37934. #endif
  37935. ret = mp_unsigned_bin_size(NULL);
  37936. if (ret != 0)
  37937. return -12961;
  37938. #if defined(WC_MP_TO_RADIX) || defined(WOLFSSL_SP_MATH_ALL)
  37939. ret = mp_tohex(NULL, NULL);
  37940. if (ret != MP_VAL)
  37941. return -12962;
  37942. ret = mp_tohex(a, NULL);
  37943. if (ret != MP_VAL)
  37944. return -12963;
  37945. ret = mp_tohex(NULL, hexStr);
  37946. if (ret != MP_VAL)
  37947. return -12964;
  37948. #endif
  37949. #if defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  37950. ret = mp_todecimal(NULL, NULL);
  37951. if (ret != MP_VAL)
  37952. return -12965;
  37953. ret = mp_todecimal(a, NULL);
  37954. if (ret != MP_VAL)
  37955. return -12966;
  37956. ret = mp_todecimal(NULL, decStr);
  37957. if (ret != MP_VAL)
  37958. return -12967;
  37959. #endif
  37960. #ifdef WOLFSSL_SP_MATH_ALL
  37961. ret = mp_toradix(NULL, NULL, MP_RADIX_HEX);
  37962. if (ret != MP_VAL)
  37963. return -12968;
  37964. ret = mp_toradix(a, NULL, MP_RADIX_HEX);
  37965. if (ret != MP_VAL)
  37966. return -12969;
  37967. ret = mp_toradix(NULL, hexStr, MP_RADIX_HEX);
  37968. if (ret != MP_VAL)
  37969. return -12970;
  37970. ret = mp_toradix(a, hexStr, 3);
  37971. if (ret != MP_VAL)
  37972. return -12971;
  37973. ret = mp_radix_size(NULL, MP_RADIX_HEX, NULL);
  37974. if (ret != MP_VAL)
  37975. return -12972;
  37976. ret = mp_radix_size(a, MP_RADIX_HEX, NULL);
  37977. if (ret != MP_VAL)
  37978. return -12973;
  37979. ret = mp_radix_size(NULL, MP_RADIX_HEX, &size);
  37980. if (ret != MP_VAL)
  37981. return -12974;
  37982. ret = mp_radix_size(a, 3, &size);
  37983. if (ret != MP_VAL)
  37984. return -12975;
  37985. #endif
  37986. return 0;
  37987. }
  37988. #endif
  37989. #if !defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  37990. static int mp_test_set_is_bit(mp_int* a)
  37991. {
  37992. int i, j;
  37993. mp_zero(a);
  37994. for (i = 0; i <= DIGIT_BIT * 2; i++) {
  37995. if (mp_is_bit_set(a, i))
  37996. return -12980;
  37997. for (j = 0; j < i; j++) {
  37998. if (!mp_is_bit_set(a, j))
  37999. return -12981;
  38000. }
  38001. if (mp_set_bit(a, i) != 0)
  38002. return -12982;
  38003. if (!mp_is_bit_set(a, i))
  38004. return -12983;
  38005. }
  38006. mp_zero(a);
  38007. for (i = 0; i <= DIGIT_BIT * 2; i++) {
  38008. if (mp_is_bit_set(a, i))
  38009. return -12984;
  38010. }
  38011. for (i = 0; i <= DIGIT_BIT * 2; i++) {
  38012. mp_zero(a);
  38013. if (mp_set_bit(a, i) != 0)
  38014. return -12985;
  38015. for (j = 0; j < i; j++) {
  38016. if (mp_is_bit_set(a, j))
  38017. return -12986;
  38018. }
  38019. if (!mp_is_bit_set(a, i))
  38020. return -12987;
  38021. }
  38022. #ifdef WOLFSSL_KEY_GEN
  38023. for (i = 0; i < DIGIT_BIT * 2; i++) {
  38024. mp_set(a, 1);
  38025. if (mp_2expt(a, i) != 0)
  38026. return -12988;
  38027. for (j = 0; j < i; j++) {
  38028. if (mp_is_bit_set(a, j))
  38029. return -12989;
  38030. }
  38031. if (!mp_is_bit_set(a, i))
  38032. return -12990;
  38033. }
  38034. #endif
  38035. #ifdef WOLFSSL_SP_MATH
  38036. mp_zero(a);
  38037. for (j = 1; j <= 3; j++) {
  38038. i = SP_INT_MAX_BITS - j;
  38039. if (mp_is_bit_set(a, i))
  38040. return -12991;
  38041. if (mp_set_bit(a, i) != 0)
  38042. return -12992;
  38043. if (!mp_is_bit_set(a, i))
  38044. return -12993;
  38045. #ifdef WOLFSSL_KEY_GEN
  38046. if (mp_2expt(a, i) != 0)
  38047. return -12994;
  38048. if (!mp_is_bit_set(a, i))
  38049. return -12995;
  38050. #endif
  38051. }
  38052. mp_zero(a);
  38053. for (j = 0; j <= 3; j++) {
  38054. i = SP_INT_MAX_BITS + j;
  38055. if (mp_is_bit_set(a, i))
  38056. return -12996;
  38057. if (mp_set_bit(a, i) != MP_VAL)
  38058. return -12997;
  38059. #ifdef WOLFSSL_KEY_GEN
  38060. if (mp_2expt(a, i) != MP_VAL)
  38061. return -12998;
  38062. #endif
  38063. }
  38064. #endif
  38065. return 0;
  38066. }
  38067. #endif /* !WOLFSSL_SP_MATH || WOLFSSL_SP_MATH_ALL */
  38068. static int mp_test_cmp(mp_int* a, mp_int* b)
  38069. {
  38070. int ret;
  38071. mp_zero(a);
  38072. mp_zero(b);
  38073. ret = mp_cmp_d(a, 0);
  38074. if (ret != MP_EQ)
  38075. return -13000;
  38076. ret = mp_cmp_d(a, 1);
  38077. if (ret != MP_LT)
  38078. return -13001;
  38079. ret = mp_cmp(a, b);
  38080. if (ret != MP_EQ)
  38081. return -13002;
  38082. mp_set(a, 1);
  38083. ret = mp_cmp_d(a, 0);
  38084. if (ret != MP_GT)
  38085. return -13003;
  38086. ret = mp_cmp_d(a, 1);
  38087. if (ret != MP_EQ)
  38088. return -13004;
  38089. ret = mp_cmp_d(a, 2);
  38090. if (ret != MP_LT)
  38091. return -13005;
  38092. ret = mp_cmp(a, b);
  38093. if (ret != MP_GT)
  38094. return -13006;
  38095. mp_read_radix(b, "1234567890123456789", MP_RADIX_HEX);
  38096. ret = mp_cmp_d(b, -1);
  38097. if (ret != MP_GT)
  38098. return -13007;
  38099. ret = mp_cmp(a, b);
  38100. if (ret != MP_LT)
  38101. return -13008;
  38102. ret = mp_cmp(b, a);
  38103. if (ret != MP_GT)
  38104. return -13009;
  38105. ret = mp_cmp(b, b);
  38106. if (ret != MP_EQ)
  38107. return -13010;
  38108. #if (!defined(WOLFSSL_SP_MATH) && !defined(WOLFSSL_SP_MATH_ALL)) || \
  38109. defined(WOLFSSL_SP_INT_NEGATIVE)
  38110. mp_read_radix(a, "-1", MP_RADIX_HEX);
  38111. mp_read_radix(a, "1", MP_RADIX_HEX);
  38112. ret = mp_cmp(a, b);
  38113. if (ret != MP_LT)
  38114. return -13011;
  38115. ret = mp_cmp(b, a);
  38116. if (ret != MP_GT)
  38117. return -13012;
  38118. mp_read_radix(b, "-2", MP_RADIX_HEX);
  38119. ret = mp_cmp(a, b);
  38120. if (ret != MP_GT)
  38121. return -13013;
  38122. ret = mp_cmp(b, a);
  38123. if (ret != MP_LT)
  38124. return -13014;
  38125. mp_read_radix(a, "-2", MP_RADIX_HEX);
  38126. ret = mp_cmp(a, b);
  38127. if (ret != MP_EQ)
  38128. return -13015;
  38129. #endif
  38130. return 0;
  38131. }
  38132. #if !defined(NO_DH) || defined(HAVE_ECC) || !defined(WOLFSSL_RSA_VERIFY_ONLY)
  38133. static int mp_test_shbd(mp_int* a, mp_int* b, WC_RNG* rng)
  38134. {
  38135. int ret;
  38136. int i, j, k;
  38137. #ifndef WOLFSSL_SP_MATH
  38138. for (i = 0; i < 10; i++) {
  38139. for (j = 1; j < (DIGIT_BIT + 7) / 8 * 3; j++) {
  38140. ret = randNum(a, j, rng, NULL);
  38141. if (ret != MP_OKAY)
  38142. return -13020;
  38143. mp_copy(a, b);
  38144. for (k = 0; k <= DIGIT_BIT * 2; k++) {
  38145. ret = mp_mul_2d(a, k, a);
  38146. if (ret != MP_OKAY)
  38147. return -13021;
  38148. mp_rshb(a, k);
  38149. if (mp_cmp(a, b) != MP_EQ)
  38150. return -13022;
  38151. }
  38152. }
  38153. }
  38154. #endif
  38155. for (i = 0; i < 10; i++) {
  38156. for (j = 1; j < (DIGIT_BIT + 7) / 8 * 3; j++) {
  38157. ret = randNum(a, j, rng, NULL);
  38158. if (ret != MP_OKAY)
  38159. return -13023;
  38160. mp_copy(a, b);
  38161. for (k = 0; k < 10; k++) {
  38162. ret = mp_lshd(a, k);
  38163. if (ret != MP_OKAY)
  38164. return -13024;
  38165. mp_rshd(a, k);
  38166. if (mp_cmp(a, b) != MP_EQ)
  38167. return -13025;
  38168. }
  38169. }
  38170. }
  38171. mp_zero(a);
  38172. mp_rshd(a, 1);
  38173. if (!mp_iszero(a))
  38174. return -13026;
  38175. mp_set(a, 1);
  38176. mp_rshd(a, 1);
  38177. if (!mp_iszero(a))
  38178. return -13027;
  38179. mp_set(a, 1);
  38180. mp_rshd(a, 2);
  38181. if (!mp_iszero(a))
  38182. return -13028;
  38183. return 0;
  38184. }
  38185. #endif
  38186. #if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  38187. static int mp_test_div(mp_int* a, mp_int* d, mp_int* r, mp_int* rem,
  38188. WC_RNG* rng)
  38189. {
  38190. int ret;
  38191. int i, j, k;
  38192. mp_zero(a);
  38193. mp_zero(d);
  38194. ret = mp_div(a, d, r, rem);
  38195. if (ret != MP_VAL)
  38196. return -13030;
  38197. mp_set(d, 1);
  38198. ret = mp_div(a, d, r, rem);
  38199. if (ret != MP_OKAY)
  38200. return -13031;
  38201. if (!mp_iszero(r))
  38202. return -13032;
  38203. if (!mp_iszero(rem))
  38204. return -13033;
  38205. mp_set(a, 1);
  38206. ret = mp_div(a, d, r, rem);
  38207. if (ret != MP_OKAY)
  38208. return -13034;
  38209. if (!mp_isone(r))
  38210. return -13035;
  38211. if (!mp_iszero(rem))
  38212. return -13036;
  38213. for (i = 0; i < 100; i++) {
  38214. for (j = 1; j < (DIGIT_BIT + 7) / 8 * 2; j++) {
  38215. ret = randNum(d, j, rng, NULL);
  38216. if (ret != MP_OKAY)
  38217. return -13037;
  38218. for (k = 1; k < (DIGIT_BIT + 7) / 8 * 2 + 1; k++) {
  38219. ret = randNum(a, k, rng, NULL);
  38220. if (ret != MP_OKAY)
  38221. return -13038;
  38222. ret = mp_div(a, d, NULL, rem);
  38223. if (ret != MP_OKAY)
  38224. return -13039;
  38225. ret = mp_div(a, d, r, NULL);
  38226. if (ret != MP_OKAY)
  38227. return -13040;
  38228. ret = mp_div(a, d, r, rem);
  38229. if (ret != MP_OKAY)
  38230. return -13041;
  38231. mp_mul(r, d, r);
  38232. mp_add(r, rem, r);
  38233. if (mp_cmp(r, a) != MP_EQ)
  38234. return -13042;
  38235. }
  38236. }
  38237. }
  38238. ret = randNum(d, (DIGIT_BIT + 7) / 8 * 2, rng, NULL);
  38239. if (ret != MP_OKAY)
  38240. return -13043;
  38241. mp_add(d, d, a);
  38242. mp_set(rem, 1);
  38243. mp_div(a, d, NULL, rem);
  38244. if (ret != MP_OKAY)
  38245. return -13044;
  38246. if (!mp_iszero(rem))
  38247. return -13045;
  38248. mp_set(r, 1);
  38249. mp_div(a, d, r, NULL);
  38250. if (ret != MP_OKAY)
  38251. return -13046;
  38252. if (mp_cmp_d(r, 2) != MP_EQ)
  38253. return -13047;
  38254. mp_set(r, 1);
  38255. mp_set(rem, 1);
  38256. mp_div(a, d, r, rem);
  38257. if (ret != MP_OKAY)
  38258. return -13048;
  38259. if (mp_cmp_d(r, 2) != MP_EQ)
  38260. return -13049;
  38261. if (!mp_iszero(rem))
  38262. return -13050;
  38263. mp_set(a, 0xfe);
  38264. mp_lshd(a, 3);
  38265. mp_add_d(a, 0xff, a);
  38266. mp_set(d, 0xfe);
  38267. mp_lshd(d, 2);
  38268. ret = mp_div(a, d, r, rem);
  38269. if (ret != MP_OKAY)
  38270. return -13051;
  38271. mp_mul(r, d, d);
  38272. mp_add(rem, d, d);
  38273. if (mp_cmp(a, d) != MP_EQ)
  38274. return -13052;
  38275. /* Force (hi | lo) / d to be (d | 0) / d which will would not fit in
  38276. * a digit. So mp_div must detect and handle.
  38277. * For example: 0x800000 / 0x8001, DIGIT_BIT = 8
  38278. */
  38279. mp_set(a, 1);
  38280. mp_mul_2d(a, DIGIT_BIT * 3 - 1, a);
  38281. mp_set(d, 1);
  38282. mp_mul_2d(d, DIGIT_BIT * 2 - 1, d);
  38283. mp_add_d(d, 1, d);
  38284. ret = mp_div(a, d, r, rem);
  38285. if (ret != MP_OKAY)
  38286. return -13053;
  38287. /* Make sure [d | d] / d is handled. */
  38288. mp_zero(a);
  38289. mp_set_bit(a, DIGIT_BIT * 2 - 1);
  38290. mp_set_bit(a, DIGIT_BIT * 1 - 1);
  38291. mp_zero(d);
  38292. mp_set_bit(d, DIGIT_BIT - 1);
  38293. ret = mp_div(a, d, r, rem);
  38294. if (ret != MP_OKAY)
  38295. return -13054;
  38296. mp_zero(a);
  38297. mp_set_bit(a, DIGIT_BIT);
  38298. mp_set_bit(a, 0);
  38299. mp_zero(d);
  38300. if (mp_cmp(r, a) != MP_EQ)
  38301. return -13055;
  38302. if (mp_cmp(rem, d) != MP_EQ)
  38303. return -13056;
  38304. return 0;
  38305. }
  38306. #endif
  38307. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_DH) || !defined(NO_DSA)) && \
  38308. !defined(WC_NO_RNG)
  38309. static int mp_test_prime(mp_int* a, WC_RNG* rng)
  38310. {
  38311. int ret;
  38312. int res;
  38313. ret = mp_rand_prime(a, 1, rng, NULL);
  38314. #if defined(WOLFSSL_SP_MATH_ALL)
  38315. if (ret != 0)
  38316. return -13060;
  38317. #else
  38318. if (ret != MP_VAL)
  38319. return -13060;
  38320. #endif
  38321. #ifndef WOLFSSL_SP_MATH
  38322. ret = mp_rand_prime(a, -5, rng, NULL);
  38323. if (ret != 0 || (a->dp[0] & 3) != 3)
  38324. return -13061;
  38325. #endif
  38326. ret = mp_prime_is_prime(a, 1, &res);
  38327. if (ret != MP_OKAY)
  38328. return -13062;
  38329. #ifndef WOLFSSL_SP_MATH
  38330. if (res != MP_YES)
  38331. return -13063;
  38332. #else
  38333. if (res != MP_NO)
  38334. return -13063;
  38335. #endif
  38336. ret = mp_prime_is_prime(a, 0, &res);
  38337. if (ret != MP_VAL)
  38338. return -13064;
  38339. ret = mp_prime_is_prime(a, -1, &res);
  38340. if (ret != MP_VAL)
  38341. return -13065;
  38342. ret = mp_prime_is_prime(a, 257, &res);
  38343. if (ret != MP_VAL)
  38344. return -13066;
  38345. mp_set(a, 1);
  38346. ret = mp_prime_is_prime(a, 1, &res);
  38347. if (ret != MP_OKAY)
  38348. return -13067;
  38349. if (res != MP_NO)
  38350. return -13068;
  38351. ret = mp_prime_is_prime_ex(a, 1, &res, rng);
  38352. if (ret != MP_OKAY)
  38353. return -13069;
  38354. if (res != MP_NO)
  38355. return -13070;
  38356. mp_set(a, 2);
  38357. ret = mp_prime_is_prime(a, 1, &res);
  38358. if (ret != MP_OKAY)
  38359. return -13071;
  38360. if (res != MP_YES)
  38361. return -13072;
  38362. ret = mp_prime_is_prime_ex(a, 1, &res, rng);
  38363. if (ret != MP_OKAY)
  38364. return -13073;
  38365. if (res != MP_YES)
  38366. return -13074;
  38367. mp_set(a, 0xfb);
  38368. ret = mp_prime_is_prime(a, 1, &res);
  38369. if (ret != MP_OKAY)
  38370. return -13075;
  38371. if (res != MP_YES)
  38372. return -13076;
  38373. ret = mp_prime_is_prime_ex(a, 1, &res, rng);
  38374. if (ret != MP_OKAY)
  38375. return -13077;
  38376. if (res != MP_YES)
  38377. return -13078;
  38378. mp_set(a, 0x6);
  38379. ret = mp_prime_is_prime(a, 1, &res);
  38380. if (ret != MP_OKAY)
  38381. return -13079;
  38382. if (res != MP_NO)
  38383. return -13080;
  38384. ret = mp_prime_is_prime_ex(a, 1, &res, rng);
  38385. if (ret != MP_OKAY)
  38386. return -13081;
  38387. if (res != MP_NO)
  38388. return -13082;
  38389. mp_set_int(a, 0x655 * 0x65b);
  38390. ret = mp_prime_is_prime(a, 10, &res);
  38391. if (ret != MP_OKAY)
  38392. return -13083;
  38393. if (res != MP_NO)
  38394. return -13084;
  38395. ret = mp_prime_is_prime_ex(a, 10, &res, rng);
  38396. if (ret != MP_OKAY)
  38397. return -13085;
  38398. if (res != MP_NO)
  38399. return -13086;
  38400. return 0;
  38401. }
  38402. #endif
  38403. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(WC_RSA_BLINDING)
  38404. static int mp_test_lcm_gcd(mp_int* a, mp_int* b, mp_int* r, mp_int* exp,
  38405. WC_RNG* rng)
  38406. {
  38407. int ret;
  38408. int i;
  38409. WOLFSSL_SMALL_STACK_STATIC const int kat[][3] = {
  38410. { 1, 1, 1 }, { 2, 1, 2 }, { 1, 2, 2 }, { 2, 4, 4 }, { 4, 2, 4 },
  38411. { 12, 56, 168 }, { 56, 12, 168 }
  38412. };
  38413. (void)exp;
  38414. mp_set(a, 0);
  38415. mp_set(b, 1);
  38416. ret = mp_lcm(a, a, r);
  38417. if (ret != MP_VAL)
  38418. return -13090;
  38419. ret = mp_lcm(a, b, r);
  38420. if (ret != MP_VAL)
  38421. return -13091;
  38422. ret = mp_lcm(b, a, r);
  38423. if (ret != MP_VAL)
  38424. return -13092;
  38425. for (i = 0; i < (int)(sizeof(kat) / sizeof(*kat)); i++) {
  38426. mp_set(a, kat[i][0]);
  38427. mp_set(b, kat[i][1]);
  38428. ret = mp_lcm(a, b, r);
  38429. if (ret != MP_OKAY)
  38430. return -13093;
  38431. mp_set(exp, kat[i][2]);
  38432. if (mp_cmp(r, exp) != MP_EQ)
  38433. return -13094;
  38434. }
  38435. (void)rng;
  38436. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_DH) || !defined(NO_DSA)) && \
  38437. !defined(WC_NO_RNG)
  38438. if (mp_rand_prime(a, 20, rng, NULL) != MP_OKAY)
  38439. return -13095;
  38440. if (mp_rand_prime(b, 20, rng, NULL) != MP_OKAY)
  38441. return -13096;
  38442. if (mp_mul(a, b, exp) != MP_OKAY)
  38443. return -13097;
  38444. ret = mp_lcm(a, b, r);
  38445. if (ret != MP_OKAY)
  38446. return -13098;
  38447. if (mp_cmp(r, exp) != MP_EQ)
  38448. return -13099;
  38449. ret = mp_lcm(b, a, r);
  38450. if (ret != MP_OKAY)
  38451. return -13100;
  38452. if (mp_cmp(r, exp) != MP_EQ)
  38453. return -13101;
  38454. #endif
  38455. mp_set(a, 11);
  38456. mp_zero(b);
  38457. ret = mp_gcd(a, b, r);
  38458. if (ret != MP_OKAY)
  38459. return -13102;
  38460. if (mp_cmp_d(r, 11) != MP_EQ)
  38461. return -13103;
  38462. ret = mp_gcd(b, a, r);
  38463. if (ret != MP_OKAY)
  38464. return -13104;
  38465. if (mp_cmp_d(r, 11) != MP_EQ)
  38466. return -13105;
  38467. ret = mp_gcd(b, b, r);
  38468. if (ret != MP_VAL)
  38469. return -13106;
  38470. return 0;
  38471. }
  38472. #endif
  38473. #if (!defined(WOLFSSL_SP_MATH) && !defined(USE_FAST_MATH)) || \
  38474. defined(WOLFSSL_SP_MATH_ALL)
  38475. static int mp_test_mod_2d(mp_int* a, mp_int* r, mp_int* t, WC_RNG* rng)
  38476. {
  38477. int ret;
  38478. int i;
  38479. int j;
  38480. mp_set(a, 10);
  38481. ret = mp_mod_2d(a, 0, r);
  38482. if (ret != MP_OKAY)
  38483. return -13110;
  38484. if (!mp_iszero(r))
  38485. return -13111;
  38486. ret = mp_mod_2d(a, 1, r);
  38487. if (ret != MP_OKAY)
  38488. return -13112;
  38489. if (!mp_iszero(r))
  38490. return -13113;
  38491. ret = mp_mod_2d(a, 2, r);
  38492. if (ret != MP_OKAY)
  38493. return -13114;
  38494. if (mp_cmp_d(r, 2))
  38495. return -13115;
  38496. for (i = 2; i < 20; i++) {
  38497. ret = randNum(a, i, rng, NULL);
  38498. if (ret != 0)
  38499. return -13116;
  38500. for (j = 1; j <= mp_count_bits(a); j++) {
  38501. /* Get top part */
  38502. ret = mp_div_2d(a, j, t, NULL);
  38503. if (ret != 0)
  38504. return -13117;
  38505. ret = mp_mul_2d(t, j, t);
  38506. if (ret != 0)
  38507. return -13118;
  38508. /* Get bottom part */
  38509. ret = mp_mod_2d(a, j, r);
  38510. if (ret != 0)
  38511. return -13119;
  38512. /* Reassemble */
  38513. ret = mp_add(t, r, r);
  38514. if (ret != 0)
  38515. return -13120;
  38516. if (mp_cmp(a, r) != MP_EQ)
  38517. return -13121;
  38518. }
  38519. }
  38520. #if !defined(WOLFSSL_SP_MATH) && defined(WOLFSSL_SP_INT_NEGATIVE)
  38521. /* Test negative value being moded. */
  38522. for (j = 0; j < 20; j++) {
  38523. ret = randNum(a, 2, rng, NULL);
  38524. if (ret != 0)
  38525. return -13122;
  38526. a->sign = MP_NEG;
  38527. for (i = 1; i < DIGIT_BIT * 3 + 1; i++) {
  38528. ret = mp_mod_2d(a, i, r);
  38529. if (ret != 0)
  38530. return -13124;
  38531. mp_zero(t);
  38532. ret = mp_set_bit(t, i);
  38533. if (ret != 0)
  38534. return -13125;
  38535. ret = mp_mod(a, t, t);
  38536. if (ret != 0)
  38537. return -13126;
  38538. if (mp_cmp(r, t) != MP_EQ)
  38539. return -13127;
  38540. }
  38541. }
  38542. #endif
  38543. return 0;
  38544. }
  38545. #endif
  38546. #if (defined(HAVE_ECC) && defined(HAVE_COMP_KEY)) || \
  38547. (defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN))
  38548. static int mp_test_mod_d(mp_int* a)
  38549. {
  38550. int ret;
  38551. mp_digit r;
  38552. if (mp_set(a, 1) != MP_OKAY)
  38553. return -13130;
  38554. ret = mp_mod_d(a, 0, &r);
  38555. if (ret != MP_VAL)
  38556. return -13131;
  38557. mp_zero(a);
  38558. ret = mp_mod_d(a, 1, &r);
  38559. if (ret != MP_OKAY)
  38560. return -13132;
  38561. ret = mp_mod_d(a, 3, &r);
  38562. if (ret != MP_OKAY)
  38563. return -13133;
  38564. ret = mp_mod_d(a, 5, &r);
  38565. if (ret != MP_OKAY)
  38566. return -13134;
  38567. return 0;
  38568. }
  38569. #endif
  38570. static int mp_test_mul_sqr(mp_int* a, mp_int* b, mp_int* r1, mp_int* r2,
  38571. WC_RNG* rng)
  38572. {
  38573. int ret;
  38574. int i;
  38575. for (i = 1; i < 16; i++) {
  38576. ret = randNum(a, i, rng, NULL);
  38577. if (ret != 0)
  38578. return -13140;
  38579. ret = mp_mul(a, a, r1);
  38580. if (ret != 0)
  38581. return -13141;
  38582. ret = mp_sqr(a, r2);
  38583. if (ret != 0)
  38584. return -13142;
  38585. if (mp_cmp(r1, r2) != MP_EQ)
  38586. return -13143;
  38587. }
  38588. ret = mp_set(b, 0);
  38589. if (ret != MP_OKAY)
  38590. return -13144;
  38591. ret = mp_mul(a, b, r1);
  38592. if (ret != MP_OKAY)
  38593. return -13145;
  38594. if (!mp_iszero(r1))
  38595. return -13146;
  38596. ret = mp_sqr(b, r1);
  38597. if (ret != MP_OKAY)
  38598. return -13147;
  38599. if (!mp_iszero(r1))
  38600. return -13148;
  38601. #ifdef WOLFSSL_SP_MATH_ALL
  38602. ret = mp_set(a, 1);
  38603. if (ret != MP_OKAY)
  38604. return -13149;
  38605. i = (SP_INT_DIGITS / 2) + 1;
  38606. ret = mp_mul_2d(a, i * SP_WORD_SIZE - 1, a);
  38607. if (ret != MP_OKAY)
  38608. return -13150;
  38609. ret = mp_set(b, 1);
  38610. if (ret != MP_OKAY)
  38611. return -13151;
  38612. ret = mp_mul_2d(b, (SP_INT_DIGITS - 1 - i) * SP_WORD_SIZE - 1, b);
  38613. if (ret != MP_OKAY)
  38614. return -13152;
  38615. ret = mp_mul(a, b, r1);
  38616. if (ret != MP_OKAY)
  38617. return -13153;
  38618. ret = mp_mul(a, a, r1);
  38619. if (ret == MP_OKAY)
  38620. return -13154;
  38621. ret = mp_sqr(a, r1);
  38622. if (ret == MP_OKAY)
  38623. return -13155;
  38624. ret = mp_sqr(b, r1);
  38625. if (ret != MP_OKAY)
  38626. return -13156;
  38627. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH) || \
  38628. (defined(HAVE_ECC) && defined(FP_ECC))
  38629. ret = mp_mulmod(a, b, b, r1);
  38630. if (ret != MP_OKAY)
  38631. return -13157;
  38632. ret = mp_mulmod(a, a, b, r1);
  38633. if (ret == MP_OKAY)
  38634. return -13158;
  38635. #if defined(HAVE_ECC) && (defined(ECC_SHAMIR) || defined(FP_ECC))
  38636. ret = mp_sqrmod(a, b, r1);
  38637. if (ret == MP_OKAY)
  38638. return -13159;
  38639. ret = mp_sqrmod(b, a, r1);
  38640. if (ret != MP_OKAY)
  38641. return -13160;
  38642. #endif /* HAVE_ECC && (ECC_SHAMIR || FP_ECC) */
  38643. #endif /* WOLFSSL_SP_MATH_ALL || WOLFSSL_HAVE_SP_DH || (HAVE_ECC && FP_ECC) */
  38644. #endif /* WOLFSSL_SP_MATH_ALL */
  38645. return 0;
  38646. }
  38647. #if !defined(NO_RSA) || defined(HAVE_ECC) || !defined(NO_DSA) || \
  38648. defined(OPENSSL_EXTRA)
  38649. static int mp_test_invmod(mp_int* a, mp_int* m, mp_int* r)
  38650. {
  38651. int ret;
  38652. mp_set(a, 0);
  38653. mp_set(m, 1);
  38654. ret = mp_invmod(a, m, r);
  38655. if (ret != MP_VAL)
  38656. return -13170;
  38657. ret = mp_invmod(m, a, r);
  38658. if (ret != MP_VAL)
  38659. return -13171;
  38660. mp_set(a, 2);
  38661. mp_set(m, 4);
  38662. ret = mp_invmod(a, m, r);
  38663. if (ret != MP_VAL)
  38664. return -13172;
  38665. mp_set(a, 1);
  38666. mp_set(m, 4);
  38667. ret = mp_invmod(a, m, r);
  38668. if (ret != MP_OKAY)
  38669. return -13173;
  38670. if (!mp_isone(r))
  38671. return -13174;
  38672. mp_set(a, 3);
  38673. mp_set(m, 4);
  38674. ret = mp_invmod(a, m, r);
  38675. if (ret != MP_OKAY)
  38676. return -13175;
  38677. if (mp_cmp_d(r, 3))
  38678. return -13176;
  38679. mp_set(a, 3);
  38680. mp_set(m, 5);
  38681. ret = mp_invmod(a, m, r);
  38682. if (ret != MP_OKAY)
  38683. return -13177;
  38684. #if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  38685. /* Maximum 'a' */
  38686. mp_set(a, 0);
  38687. mp_set_bit(a, (r->size / 2)* SP_WORD_SIZE - 1);
  38688. mp_sub_d(a, 1, a);
  38689. /* Modulus too big. */
  38690. mp_set(m, 0);
  38691. mp_set_bit(m, (r->size / 2) * SP_WORD_SIZE);
  38692. ret = mp_invmod(a, m, r);
  38693. if (ret != MP_VAL)
  38694. return -13178;
  38695. /* Maximum modulus - even. */
  38696. mp_set(m, 0);
  38697. mp_set_bit(m, (r->size / 2) * SP_WORD_SIZE - 1);
  38698. ret = mp_invmod(a, m, r);
  38699. if (ret != MP_OKAY)
  38700. return -13179;
  38701. #endif
  38702. #if !defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_INT_NEGATIVE)
  38703. mp_read_radix(a, "-3", 16);
  38704. ret = mp_invmod(a, m, r);
  38705. if (ret != MP_OKAY)
  38706. return -13180;
  38707. #endif
  38708. #if defined(WOLFSSL_SP_MATH_ALL) && defined(HAVE_ECC)
  38709. mp_set(a, 0);
  38710. mp_set(m, 3);
  38711. ret = mp_invmod_mont_ct(a, m, r, 1);
  38712. if (ret != MP_VAL)
  38713. return -13190;
  38714. mp_set(a, 1);
  38715. mp_set(m, 0);
  38716. ret = mp_invmod_mont_ct(a, m, r, 1);
  38717. if (ret != MP_VAL)
  38718. return -13191;
  38719. mp_set(a, 1);
  38720. mp_set(m, 1);
  38721. ret = mp_invmod_mont_ct(a, m, r, 1);
  38722. if (ret != MP_VAL)
  38723. return -13192;
  38724. mp_set(a, 1);
  38725. mp_set(m, 2);
  38726. ret = mp_invmod_mont_ct(a, m, r, 1);
  38727. if (ret != MP_VAL)
  38728. return -13193;
  38729. mp_set(a, 1);
  38730. mp_set(m, 3);
  38731. ret = mp_invmod_mont_ct(a, m, r, 1);
  38732. if (ret != MP_OKAY)
  38733. return -13194;
  38734. #endif
  38735. return 0;
  38736. }
  38737. #endif /* !NO_RSA || HAVE_ECC || !NO_DSA || OPENSSL_EXTRA */
  38738. #if !defined(NO_RSA) || !defined(NO_DSA) || !defined(NO_DH) || \
  38739. (defined(HAVE_ECC) && defined(HAVE_COMP_KEY)) || defined(OPENSSL_EXTRA)
  38740. static int mp_test_exptmod(mp_int* b, mp_int* e, mp_int* m, mp_int* r)
  38741. {
  38742. int ret;
  38743. mp_set(b, 0x2);
  38744. mp_set(e, 0x3);
  38745. mp_set(m, 0x0);
  38746. ret = mp_exptmod_ex(b, e, 1, m, r);
  38747. if (ret != MP_VAL)
  38748. return -13190;
  38749. ret = mp_exptmod_nct(b, e, m, r);
  38750. if (ret != MP_VAL)
  38751. return -13191;
  38752. mp_set(b, 0x2);
  38753. mp_set(e, 0x3);
  38754. mp_set(m, 0x1);
  38755. ret = mp_exptmod_ex(b, e, 1, m, r);
  38756. if (ret != MP_OKAY)
  38757. return -13192;
  38758. if (!mp_iszero(r))
  38759. return -13193;
  38760. ret = mp_exptmod_nct(b, e, m, r);
  38761. if (ret != MP_OKAY)
  38762. return -13194;
  38763. if (!mp_iszero(r))
  38764. return -13195;
  38765. mp_set(b, 0x2);
  38766. mp_set(e, 0x0);
  38767. mp_set(m, 0x7);
  38768. ret = mp_exptmod_ex(b, e, 1, m, r);
  38769. if (ret != MP_OKAY)
  38770. return -13196;
  38771. if (!mp_isone(r))
  38772. return -13197;
  38773. ret = mp_exptmod_nct(b, e, m, r);
  38774. if (ret != MP_OKAY)
  38775. return -13198;
  38776. if (!mp_isone(r))
  38777. return -13199;
  38778. mp_set(b, 0x0);
  38779. mp_set(e, 0x3);
  38780. mp_set(m, 0x7);
  38781. ret = mp_exptmod_ex(b, e, 1, m, r);
  38782. if (ret != MP_OKAY)
  38783. return -13200;
  38784. if (!mp_iszero(r))
  38785. return -13201;
  38786. ret = mp_exptmod_nct(b, e, m, r);
  38787. if (ret != MP_OKAY)
  38788. return -13202;
  38789. if (!mp_iszero(r))
  38790. return -13203;
  38791. mp_set(b, 0x10);
  38792. mp_set(e, 0x3);
  38793. mp_set(m, 0x7);
  38794. ret = mp_exptmod_ex(b, e, 1, m, r);
  38795. if (ret != MP_OKAY)
  38796. return -13204;
  38797. ret = mp_exptmod_nct(b, e, m, r);
  38798. if (ret != MP_OKAY)
  38799. return -13205;
  38800. mp_set(b, 0x7);
  38801. mp_set(e, 0x3);
  38802. mp_set(m, 0x7);
  38803. ret = mp_exptmod_ex(b, e, 1, m, r);
  38804. if (ret != MP_OKAY)
  38805. return -13206;
  38806. if (!mp_iszero(r))
  38807. return -13207;
  38808. ret = mp_exptmod_nct(b, e, m, r);
  38809. if (ret != MP_OKAY)
  38810. return -13208;
  38811. if (!mp_iszero(r))
  38812. return -13209;
  38813. #ifndef WOLFSSL_SP_MATH
  38814. mp_set(b, 0x01);
  38815. mp_mul_2d(b, DIGIT_BIT, b);
  38816. mp_add_d(b, 1, b);
  38817. mp_set(e, 0x3);
  38818. mp_copy(b, m);
  38819. ret = mp_exptmod_ex(b, e, 1, m, r);
  38820. if (ret != MP_OKAY)
  38821. return -13210;
  38822. if (!mp_iszero(r))
  38823. return -13211;
  38824. ret = mp_exptmod_nct(b, e, m, r);
  38825. if (ret != MP_OKAY)
  38826. return -13212;
  38827. if (!mp_iszero(r))
  38828. return -13213;
  38829. #endif
  38830. mp_set(b, 0x2);
  38831. mp_set(e, 0x3);
  38832. mp_set(m, 0x7);
  38833. ret = mp_exptmod_ex(b, e, 1, m, r);
  38834. if (ret != MP_OKAY)
  38835. return -13214;
  38836. ret = mp_exptmod_nct(b, e, m, r);
  38837. if (ret != MP_OKAY)
  38838. return -13215;
  38839. #ifdef WOLFSSL_SP_MATH_ALL
  38840. mp_set(b, 0x2);
  38841. mp_set(e, 0x3);
  38842. mp_set(m, 0x01);
  38843. mp_mul_2d(m, SP_WORD_SIZE * SP_INT_DIGITS / 2, m);
  38844. mp_add_d(m, 0x01, m);
  38845. ret = mp_exptmod_ex(b, e, 1, m, r);
  38846. if (ret != MP_VAL)
  38847. return -13216;
  38848. ret = mp_exptmod_nct(b, e, m, r);
  38849. if (ret != MP_VAL)
  38850. return -13217;
  38851. #endif
  38852. return 0;
  38853. }
  38854. #endif /* !NO_RSA || !NO_DSA || !NO_DH || (HAVE_ECC && HAVE_COMP_KEY) ||
  38855. * OPENSSL_EXTRA */
  38856. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH) || \
  38857. defined(HAVE_ECC) || (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY))
  38858. static int mp_test_mont(mp_int* a, mp_int* m, mp_int* n, mp_int* r, WC_RNG* rng)
  38859. {
  38860. int ret;
  38861. mp_digit mp;
  38862. static int exp[] = { 7, 8, 16, 27, 32, 64,
  38863. 127, 128, 255, 256,
  38864. #if defined(SP_WORD_SIZE) && SP_WORD_SIZE > 8
  38865. 383, 384, 2033, 2048
  38866. #endif
  38867. };
  38868. static mp_digit sub[] = { 0x01, 0x05, 0x0f, 0x27, 0x05, 0x3b,
  38869. 0x01, 0x9f, 0x13, 0xbd,
  38870. #if defined(SP_WORD_SIZE) && SP_WORD_SIZE > 8
  38871. 0x1f, 0x13d, 0x45, 0x615
  38872. #endif
  38873. };
  38874. int bits[] = { 256, 384,
  38875. #if defined(SP_INT_MAX_BITS) && SP_INT_MAX_BITS > 4096
  38876. 2048,
  38877. #endif
  38878. #if defined(SP_INT_MAX_BITS) && SP_INT_MAX_BITS > 6144
  38879. 3072
  38880. #endif
  38881. };
  38882. int i;
  38883. int j;
  38884. for (i = 0; i < (int)(sizeof(exp) / sizeof(*exp)); i++) {
  38885. if (exp[i] >= DIGIT_BIT)
  38886. continue;
  38887. mp_zero(m);
  38888. ret = mp_set_bit(m, exp[i]);
  38889. if (ret != MP_OKAY)
  38890. return -13220;
  38891. ret = mp_sub_d(m, sub[i], m);
  38892. if (ret != MP_OKAY)
  38893. return -13221;
  38894. ret = mp_montgomery_setup(m, &mp);
  38895. if (ret != MP_OKAY)
  38896. return -13222;
  38897. ret = mp_montgomery_calc_normalization(n, m);
  38898. if (ret != MP_OKAY)
  38899. return -13223;
  38900. for (j = 0; j < 10; j++) {
  38901. ret = randNum(a, (exp[i] + DIGIT_BIT - 1) / DIGIT_BIT, rng, NULL);
  38902. if (ret != 0)
  38903. return -13224;
  38904. ret = mp_mod(a, m, a);
  38905. if (ret != 0)
  38906. return -13225;
  38907. /* r = a * a */
  38908. ret = mp_sqrmod(a, m, r);
  38909. if (ret != MP_OKAY)
  38910. return -13226;
  38911. /* Convert to Montgomery form = a*n */
  38912. ret = mp_mulmod(a, n, m, a);
  38913. if (ret != MP_OKAY)
  38914. return -13227;
  38915. /* a*a mod m == ((a*n) * (a*n)) / n / n */
  38916. ret = mp_sqr(a, a);
  38917. if (ret != MP_OKAY)
  38918. return -13228;
  38919. ret = mp_montgomery_reduce(a, m, mp);
  38920. if (ret != MP_OKAY)
  38921. return -13229;
  38922. ret = mp_montgomery_reduce(a, m, mp);
  38923. if (ret != MP_OKAY)
  38924. return -13230;
  38925. if (mp_cmp(a, r) != MP_EQ)
  38926. return -13231;
  38927. }
  38928. }
  38929. /* Force carries. */
  38930. for (i = 0; i < (int)(sizeof(bits) / sizeof(*bits)); i++) {
  38931. /* a = 2^(bits*2) - 1 */
  38932. mp_zero(a);
  38933. mp_set_bit(a, bits[i] * 2);
  38934. mp_sub_d(a, 1, a);
  38935. /* m = 2^(bits) - 1 */
  38936. mp_zero(m);
  38937. mp_set_bit(m, bits[i]);
  38938. mp_sub_d(m, 1, m);
  38939. mp = 1;
  38940. /* result = r = 2^(bits) - 1 */
  38941. mp_zero(r);
  38942. mp_set_bit(r, bits[i]);
  38943. mp_sub_d(r, 1, r);
  38944. ret = mp_montgomery_reduce(a, m, mp);
  38945. if (ret != MP_OKAY)
  38946. return -13240;
  38947. /* Result is m or 0 if reduced to range of modulus. */
  38948. if (mp_cmp(a, r) != MP_EQ && mp_iszero(a) != MP_YES)
  38949. return -13241;
  38950. }
  38951. return 0;
  38952. }
  38953. #endif
  38954. WOLFSSL_TEST_SUBROUTINE int mp_test(void)
  38955. {
  38956. WC_RNG rng;
  38957. int ret;
  38958. #if defined(HAVE_ECC) || defined(WOLFSSL_KEY_GEN)
  38959. int i, j;
  38960. #ifndef WOLFSSL_SP_MATH
  38961. int k;
  38962. #endif
  38963. mp_digit d;
  38964. #endif
  38965. mp_int a, b, r1, r2, p;
  38966. ret = mp_init_multi(&a, &b, &r1, &r2, NULL, NULL);
  38967. if (ret != 0)
  38968. return -13300;
  38969. #ifdef WOLFSSL_SP_MATH_ALL
  38970. mp_init_copy(&p, &a);
  38971. #else
  38972. ret = mp_init(&p);
  38973. if (ret != 0)
  38974. return -13301;
  38975. #endif
  38976. #ifndef HAVE_FIPS
  38977. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  38978. #else
  38979. ret = wc_InitRng(&rng);
  38980. #endif
  38981. if (ret != 0)
  38982. goto done;
  38983. #if defined(HAVE_ECC) || defined(WOLFSSL_KEY_GEN)
  38984. mp_set_int(&a, 0);
  38985. if (a.used != 0 || a.dp[0] != 0)
  38986. return -13302;
  38987. for (j = 1; j <= MP_MAX_TEST_BYTE_LEN; j++) {
  38988. for (i = 0; i < 4 * j; i++) {
  38989. /* New values to use. */
  38990. ret = randNum(&p, j, &rng, NULL);
  38991. if (ret != 0)
  38992. return -13303;
  38993. ret = randNum(&a, j, &rng, NULL);
  38994. if (ret != 0)
  38995. return -13304;
  38996. ret = randNum(&b, j, &rng, NULL);
  38997. if (ret != 0)
  38998. return -13305;
  38999. ret = wc_RNG_GenerateBlock(&rng, (byte*)&d, sizeof(d));
  39000. if (ret != 0)
  39001. return -13306;
  39002. d &= MP_MASK;
  39003. #if !defined(WOLFSSL_SP_MATH) || (defined(HAVE_ECC) && \
  39004. (defined(ECC_SHAMIR) || defined(FP_ECC)))
  39005. /* Ensure sqrmod produce same result as mulmod. */
  39006. ret = mp_sqrmod(&a, &p, &r1);
  39007. if (ret != 0)
  39008. return -13307;
  39009. ret = mp_mulmod(&a, &a, &p, &r2);
  39010. if (ret != 0)
  39011. return -13308;
  39012. if (mp_cmp(&r1, &r2) != 0)
  39013. return -13309;
  39014. #endif
  39015. #if defined(WOLFSSL_SP_MATH_ALL) && !defined(WOLFSSL_RSA_VERIFY_ONLY)
  39016. #if defined(WOLFSSL_SP_MATH) || (defined(WOLFSSL_SP_MATH_ALL) && \
  39017. !defined(WOLFSSL_SP_INT_NEGATIVE))
  39018. ret = mp_addmod(&a, &b, &p, &r1);
  39019. if (ret != 0)
  39020. return -13310;
  39021. ret = mp_submod(&r1, &b, &p, &r2);
  39022. if (ret != 0)
  39023. return -13311;
  39024. ret = mp_mod(&a, &p, &r1);
  39025. if (ret != 0)
  39026. return -13312;
  39027. if (mp_cmp(&r1, &r2) != MP_EQ)
  39028. return -13313;
  39029. #else
  39030. /* Ensure add with mod produce same result as sub with mod. */
  39031. ret = mp_addmod(&a, &b, &p, &r1);
  39032. if (ret != 0)
  39033. return -13314;
  39034. b.sign ^= 1;
  39035. ret = mp_submod(&a, &b, &p, &r2);
  39036. if (ret != 0)
  39037. return -13315;
  39038. if (mp_cmp(&r1, &r2) != 0)
  39039. return -13316;
  39040. #endif
  39041. #endif
  39042. /* Ensure add digit produce same result as sub digit. */
  39043. ret = mp_add_d(&a, d, &r1);
  39044. if (ret != 0)
  39045. return -13317;
  39046. ret = mp_sub_d(&r1, d, &r2);
  39047. if (ret != 0)
  39048. return -13318;
  39049. if (mp_cmp(&a, &r2) != 0)
  39050. return -13319;
  39051. /* Invert - if p is even it will use the slow impl.
  39052. * - if p and a are even it will fail.
  39053. */
  39054. ret = mp_invmod(&a, &p, &r1);
  39055. if (ret != 0 && ret != MP_VAL)
  39056. return -13320;
  39057. ret = 0;
  39058. #ifndef WOLFSSL_SP_MATH
  39059. /* Shift up and down number all bits in a digit. */
  39060. for (k = 0; k < DIGIT_BIT; k++) {
  39061. mp_mul_2d(&a, k, &r1);
  39062. mp_div_2d(&r1, k, &r2, &p);
  39063. if (mp_cmp(&a, &r2) != 0)
  39064. return -13321;
  39065. if (!mp_iszero(&p))
  39066. return -13322;
  39067. mp_rshb(&r1, k);
  39068. if (mp_cmp(&a, &r1) != 0)
  39069. return -13323;
  39070. }
  39071. #endif
  39072. }
  39073. }
  39074. /* Test adding and subtracting zero from zero. */
  39075. mp_zero(&a);
  39076. ret = mp_add_d(&a, 0, &r1);
  39077. if (ret != 0)
  39078. return -13329;
  39079. if (!mp_iszero(&r1)) {
  39080. return -13330;
  39081. }
  39082. ret = mp_sub_d(&a, 0, &r2);
  39083. if (ret != 0)
  39084. return -13331;
  39085. if (!mp_iszero(&r2)) {
  39086. return -13332;
  39087. }
  39088. #if DIGIT_BIT >= 32
  39089. /* Check that setting a 32-bit digit works. */
  39090. d &= 0xffffffffU;
  39091. mp_set_int(&a, d);
  39092. if (a.used != 1 || a.dp[0] != d)
  39093. return -13324;
  39094. #endif
  39095. /* Check setting a bit and testing a bit works. */
  39096. for (i = 0; i < MP_MAX_TEST_BYTE_LEN * 8; i++) {
  39097. mp_zero(&a);
  39098. mp_set_bit(&a, i);
  39099. if (!mp_is_bit_set(&a, i))
  39100. return -13325;
  39101. }
  39102. #endif
  39103. #if defined(HAVE_ECC) && defined(HAVE_COMP_KEY)
  39104. mp_zero(&a);
  39105. i = mp_cnt_lsb(&a);
  39106. if (i != 0)
  39107. return -13326;
  39108. mp_set(&a, 1);
  39109. i = mp_cnt_lsb(&a);
  39110. if (i != 0)
  39111. return -13327;
  39112. mp_set(&a, 32);
  39113. i = mp_cnt_lsb(&a);
  39114. if (i != 5)
  39115. return -13328;
  39116. mp_zero(&a);
  39117. mp_set_bit(&a, 129);
  39118. i = mp_cnt_lsb(&a);
  39119. if (i != 129)
  39120. return -13328;
  39121. #endif
  39122. #if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  39123. if ((ret = mp_test_param(&a, &b, &r1, &rng)) != 0)
  39124. return ret;
  39125. #endif
  39126. #if defined(WOLFSSL_SP_MATH_ALL) || !defined(USE_FAST_MATH)
  39127. if ((ret = mp_test_div_3(&a, &r1, &rng)) != 0)
  39128. return ret;
  39129. #endif
  39130. #if (defined(WOLFSSL_SP_MATH_ALL) && !defined(NO_RSA) && \
  39131. !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
  39132. (!defined WOLFSSL_SP_MATH && !defined(WOLFSSL_SP_MATH_ALL) && \
  39133. (defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)))
  39134. if ((ret = mp_test_radix_10(&a, &r1, &rng)) != 0)
  39135. return ret;
  39136. #endif
  39137. #if defined(WOLFSSL_SP_MATH_ALL) || (!defined WOLFSSL_SP_MATH && \
  39138. defined(HAVE_ECC))
  39139. if ((ret = mp_test_radix_16(&a, &r1, &rng)) != 0)
  39140. return ret;
  39141. #endif
  39142. if ((ret = mp_test_shift(&a, &r1, &rng)) != 0)
  39143. return ret;
  39144. if ((ret = mp_test_add_sub_d(&a, &r1)) != 0)
  39145. return ret;
  39146. if ((ret = mp_test_read_to_bin(&a)) != 0)
  39147. return ret;
  39148. #if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  39149. if ((ret = mp_test_set_int(&a)) != 0)
  39150. return ret;
  39151. #endif
  39152. if ((ret = mp_test_cmp(&a, &r1)) != 0)
  39153. return ret;
  39154. #if !defined(NO_DH) || defined(HAVE_ECC) || !defined(WOLFSSL_RSA_VERIFY_ONLY)
  39155. if ((ret = mp_test_shbd(&a, &b, &rng)) != 0)
  39156. return ret;
  39157. #endif
  39158. #if !defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  39159. if ((ret = mp_test_set_is_bit(&a)) != 0)
  39160. return ret;
  39161. #endif
  39162. #if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  39163. if ((ret = mp_test_div(&a, &b, &r1, &r2, &rng)) != 0)
  39164. return ret;
  39165. #endif
  39166. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_DH) || !defined(NO_DSA)) && \
  39167. !defined(WC_NO_RNG)
  39168. if ((ret = mp_test_prime(&a, &rng)) != 0)
  39169. return ret;
  39170. #endif
  39171. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(WC_RSA_BLINDING)
  39172. if ((ret = mp_test_lcm_gcd(&a, &b, &r1, &r2, &rng)) != 0)
  39173. return ret;
  39174. #endif
  39175. #if (!defined(WOLFSSL_SP_MATH) && !defined(USE_FAST_MATH)) || \
  39176. defined(WOLFSSL_SP_MATH_ALL)
  39177. if ((ret = mp_test_mod_2d(&a, &r1, &p, &rng)) != 0)
  39178. return ret;
  39179. #endif
  39180. #if (defined(HAVE_ECC) && defined(HAVE_COMP_KEY)) || \
  39181. (defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN))
  39182. if ((ret = mp_test_mod_d(&a)) != 0)
  39183. return ret;
  39184. #endif
  39185. if ((ret = mp_test_mul_sqr(&a, &b, &r1, &r2, &rng)) != 0)
  39186. return ret;
  39187. #if !defined(NO_RSA) || defined(HAVE_ECC) || !defined(NO_DSA) || \
  39188. defined(OPENSSL_EXTRA)
  39189. if ((ret = mp_test_invmod(&a, &b, &r1)) != 0)
  39190. return ret;
  39191. #endif
  39192. #if !defined(NO_RSA) || !defined(NO_DSA) || !defined(NO_DH) || \
  39193. (defined(HAVE_ECC) && defined(HAVE_COMP_KEY)) || defined(OPENSSL_EXTRA)
  39194. if ((ret = mp_test_exptmod(&a, &b, &r1, &r2)) != 0)
  39195. return ret;
  39196. #endif
  39197. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH) || \
  39198. defined(HAVE_ECC) || (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY))
  39199. if ((ret = mp_test_mont(&a, &b, &r1, &r2, &rng)) != 0)
  39200. return ret;
  39201. #endif
  39202. done:
  39203. mp_clear(&p);
  39204. mp_clear(&r2);
  39205. mp_clear(&r1);
  39206. mp_clear(&b);
  39207. mp_clear(&a);
  39208. wc_FreeRng(&rng);
  39209. return ret;
  39210. }
  39211. #endif
  39212. #if defined(WOLFSSL_PUBLIC_MP) && defined(WOLFSSL_KEY_GEN)
  39213. typedef struct pairs_t {
  39214. const unsigned char* coeff;
  39215. int coeffSz;
  39216. int exp;
  39217. } pairs_t;
  39218. /*
  39219. n =p1p2p3, where pi = ki(p1-1)+1 with (k2,k3) = (173,293)
  39220. p1 = 2^192 * 0x000000000000e24fd4f6d6363200bf2323ec46285cac1d3a
  39221. + 2^0 * 0x0b2488b0c29d96c5e67f8bec15b54b189ae5636efe89b45b
  39222. */
  39223. static const unsigned char c192a[] =
  39224. {
  39225. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe2, 0x4f,
  39226. 0xd4, 0xf6, 0xd6, 0x36, 0x32, 0x00, 0xbf, 0x23,
  39227. 0x23, 0xec, 0x46, 0x28, 0x5c, 0xac, 0x1d, 0x3a
  39228. };
  39229. static const unsigned char c0a[] =
  39230. {
  39231. 0x0b, 0x24, 0x88, 0xb0, 0xc2, 0x9d, 0x96, 0xc5,
  39232. 0xe6, 0x7f, 0x8b, 0xec, 0x15, 0xb5, 0x4b, 0x18,
  39233. 0x9a, 0xe5, 0x63, 0x6e, 0xfe, 0x89, 0xb4, 0x5b
  39234. };
  39235. static const pairs_t ecPairsA[] =
  39236. {
  39237. {c192a, sizeof(c192a), 192},
  39238. {c0a, sizeof(c0a), 0}
  39239. };
  39240. static const int kA[] = {173, 293};
  39241. static const unsigned char controlPrime[] = {
  39242. 0xe1, 0x76, 0x45, 0x80, 0x59, 0xb6, 0xd3, 0x49,
  39243. 0xdf, 0x0a, 0xef, 0x12, 0xd6, 0x0f, 0xf0, 0xb7,
  39244. 0xcb, 0x2a, 0x37, 0xbf, 0xa7, 0xf8, 0xb5, 0x4d,
  39245. 0xf5, 0x31, 0x35, 0xad, 0xe4, 0xa3, 0x94, 0xa1,
  39246. 0xdb, 0xf1, 0x96, 0xad, 0xb5, 0x05, 0x64, 0x85,
  39247. 0x83, 0xfc, 0x1b, 0x5b, 0x29, 0xaa, 0xbe, 0xf8,
  39248. 0x26, 0x3f, 0x76, 0x7e, 0xad, 0x1c, 0xf0, 0xcb,
  39249. 0xd7, 0x26, 0xb4, 0x1b, 0x05, 0x8e, 0x56, 0x86,
  39250. 0x7e, 0x08, 0x62, 0x21, 0xc1, 0x86, 0xd6, 0x47,
  39251. 0x79, 0x3e, 0xb7, 0x5d, 0xa4, 0xc6, 0x3a, 0xd7,
  39252. 0xb1, 0x74, 0x20, 0xf6, 0x50, 0x97, 0x41, 0x04,
  39253. 0x53, 0xed, 0x3f, 0x26, 0xd6, 0x6f, 0x91, 0xfa,
  39254. 0x68, 0x26, 0xec, 0x2a, 0xdc, 0x9a, 0xf1, 0xe7,
  39255. 0xdc, 0xfb, 0x73, 0xf0, 0x79, 0x43, 0x1b, 0x21,
  39256. 0xa3, 0x59, 0x04, 0x63, 0x52, 0x07, 0xc9, 0xd7,
  39257. 0xe6, 0xd1, 0x1b, 0x5d, 0x5e, 0x96, 0xfa, 0x53
  39258. };
  39259. static const unsigned char testOne[] = { 1 };
  39260. static int GenerateNextP(mp_int* p1, mp_int* p2, int k)
  39261. {
  39262. int ret;
  39263. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  39264. mp_int *ki = (mp_int *)XMALLOC(sizeof(*ki), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  39265. if (ki == NULL)
  39266. return MEMORY_E;
  39267. #else
  39268. mp_int ki[1];
  39269. #endif
  39270. ret = mp_init(ki);
  39271. if (ret == 0)
  39272. ret = mp_set(ki, k);
  39273. if (ret == 0)
  39274. ret = mp_sub_d(p1, 1, p2);
  39275. if (ret == 0)
  39276. ret = mp_mul(p2, ki, p2);
  39277. if (ret == 0)
  39278. ret = mp_add_d(p2, 1, p2);
  39279. mp_clear(ki);
  39280. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  39281. XFREE(ki, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  39282. #endif
  39283. return ret;
  39284. }
  39285. static int GenerateP(mp_int* p1, mp_int* p2, mp_int* p3,
  39286. const pairs_t* ecPairs, int ecPairsSz,
  39287. const int* k)
  39288. {
  39289. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  39290. mp_int *x = NULL, *y = NULL;
  39291. #else
  39292. mp_int x[1], y[1];
  39293. #endif
  39294. int ret, i;
  39295. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  39296. if (((x = (mp_int *)XMALLOC(sizeof(*x), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER)) == NULL) ||
  39297. ((y = (mp_int *)XMALLOC(sizeof(*x), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER)) == NULL)) {
  39298. ret = MEMORY_E;
  39299. goto out;
  39300. }
  39301. #endif
  39302. ret = mp_init_multi(x, y, NULL, NULL, NULL, NULL);
  39303. if (ret != 0) {
  39304. ret = MP_MEM;
  39305. goto out;
  39306. }
  39307. for (i = 0; ret == 0 && i < ecPairsSz; i++) {
  39308. ret = mp_read_unsigned_bin(x, ecPairs[i].coeff, ecPairs[i].coeffSz);
  39309. /* p1 = 2^exp */
  39310. if (ret == 0)
  39311. ret = mp_2expt(y, ecPairs[i].exp);
  39312. /* p1 = p1 * m */
  39313. if (ret == 0)
  39314. ret = mp_mul(x, y, x);
  39315. /* p1 += */
  39316. if (ret == 0)
  39317. ret = mp_add(p1, x, p1);
  39318. mp_zero(x);
  39319. mp_zero(y);
  39320. }
  39321. if (ret == 0)
  39322. ret = GenerateNextP(p1, p2, k[0]);
  39323. if (ret == 0)
  39324. ret = GenerateNextP(p1, p3, k[1]);
  39325. out:
  39326. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  39327. if (x != NULL) {
  39328. mp_clear(x);
  39329. XFREE(x, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  39330. }
  39331. if (y != NULL) {
  39332. mp_clear(y);
  39333. XFREE(y, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  39334. }
  39335. #else
  39336. mp_clear(x);
  39337. mp_clear(y);
  39338. #endif
  39339. return ret;
  39340. }
  39341. WOLFSSL_TEST_SUBROUTINE int prime_test(void)
  39342. {
  39343. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  39344. mp_int *n = (mp_int *)XMALLOC(sizeof *n, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER),
  39345. *p1 = (mp_int *)XMALLOC(sizeof *p1, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER),
  39346. *p2 = (mp_int *)XMALLOC(sizeof *p2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER),
  39347. *p3 = (mp_int *)XMALLOC(sizeof *p3, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  39348. #else
  39349. mp_int n[1],
  39350. p1[1],
  39351. p2[1],
  39352. p3[1];
  39353. #endif
  39354. int ret, isPrime = 0;
  39355. WC_RNG rng;
  39356. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  39357. if ((n == NULL) ||
  39358. (p1 == NULL) ||
  39359. (p2 == NULL) ||
  39360. (p3 == NULL))
  39361. ERROR_OUT(MEMORY_E, out);
  39362. #endif
  39363. ret = wc_InitRng(&rng);
  39364. if (ret == 0)
  39365. ret = mp_init_multi(n, p1, p2, p3, NULL, NULL);
  39366. if (ret == 0)
  39367. ret = GenerateP(p1, p2, p3,
  39368. ecPairsA, sizeof(ecPairsA) / sizeof(ecPairsA[0]), kA);
  39369. if (ret == 0)
  39370. ret = mp_mul(p1, p2, n);
  39371. if (ret == 0)
  39372. ret = mp_mul(n, p3, n);
  39373. if (ret != 0)
  39374. ERROR_OUT(-13400, out);
  39375. /* Check the old prime test using the number that false positives.
  39376. * This test result should indicate as not prime. */
  39377. ret = mp_prime_is_prime(n, 40, &isPrime);
  39378. if (ret != 0)
  39379. ERROR_OUT(-13401, out);
  39380. if (isPrime)
  39381. ERROR_OUT(-13402, out);
  39382. /* This test result should fail. It should indicate the value as prime. */
  39383. ret = mp_prime_is_prime(n, 8, &isPrime);
  39384. if (ret != 0)
  39385. ERROR_OUT(-13403, out);
  39386. if (!isPrime)
  39387. ERROR_OUT(-13404, out);
  39388. /* This test result should indicate the value as not prime. */
  39389. ret = mp_prime_is_prime_ex(n, 8, &isPrime, &rng);
  39390. if (ret != 0)
  39391. ERROR_OUT(-13405, out);
  39392. if (isPrime)
  39393. ERROR_OUT(-13406, out);
  39394. ret = mp_read_unsigned_bin(n, controlPrime, sizeof(controlPrime));
  39395. if (ret != 0)
  39396. ERROR_OUT(-13407, out);
  39397. /* This test result should indicate the value as prime. */
  39398. ret = mp_prime_is_prime_ex(n, 8, &isPrime, &rng);
  39399. if (ret != 0)
  39400. ERROR_OUT(-13408, out);
  39401. if (!isPrime)
  39402. ERROR_OUT(-13409, out);
  39403. /* This test result should indicate the value as prime. */
  39404. isPrime = -1;
  39405. ret = mp_prime_is_prime(n, 8, &isPrime);
  39406. if (ret != 0)
  39407. ERROR_OUT(-13410, out);
  39408. if (!isPrime)
  39409. ERROR_OUT(-13411, out);
  39410. ret = mp_read_unsigned_bin(n, testOne, sizeof(testOne));
  39411. if (ret != 0)
  39412. ERROR_OUT(-13412, out);
  39413. /* This test result should indicate the value as not prime. */
  39414. ret = mp_prime_is_prime_ex(n, 8, &isPrime, &rng);
  39415. if (ret != 0)
  39416. ERROR_OUT(-13413, out);
  39417. if (isPrime)
  39418. ERROR_OUT(-13414, out);
  39419. ret = mp_prime_is_prime(n, 8, &isPrime);
  39420. if (ret != 0)
  39421. ERROR_OUT(-13415, out);
  39422. if (isPrime)
  39423. ERROR_OUT(-13416, out);
  39424. ret = 0;
  39425. out:
  39426. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  39427. if (n != NULL) {
  39428. mp_clear(n);
  39429. XFREE(n, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  39430. }
  39431. if (p1 != NULL) {
  39432. mp_clear(p1);
  39433. XFREE(p1, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  39434. }
  39435. if (p2 != NULL) {
  39436. mp_clear(p2);
  39437. XFREE(p2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  39438. }
  39439. if (p3 != NULL) {
  39440. mp_clear(p3);
  39441. XFREE(p3, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  39442. }
  39443. #else
  39444. mp_clear(p3);
  39445. mp_clear(p2);
  39446. mp_clear(p1);
  39447. mp_clear(n);
  39448. #endif
  39449. wc_FreeRng(&rng);
  39450. return ret;
  39451. }
  39452. #endif /* WOLFSSL_PUBLIC_MP */
  39453. #if defined(ASN_BER_TO_DER) && \
  39454. (defined(WOLFSSL_TEST_CERT) || defined(OPENSSL_EXTRA) || \
  39455. defined(OPENSSL_EXTRA_X509_SMALL))
  39456. /* wc_BerToDer is only public facing in the case of test cert or opensslextra */
  39457. typedef struct berDerTestData {
  39458. const byte *in;
  39459. word32 inSz;
  39460. const byte *out;
  39461. word32 outSz;
  39462. } berDerTestData;
  39463. WOLFSSL_TEST_SUBROUTINE int berder_test(void)
  39464. {
  39465. int ret;
  39466. int i;
  39467. word32 len = 0, l;
  39468. byte out[32];
  39469. WOLFSSL_SMALL_STACK_STATIC const byte good1_in[] = { 0x30, 0x80, 0x00, 0x00 };
  39470. WOLFSSL_SMALL_STACK_STATIC const byte good1_out[] = { 0x30, 0x00 };
  39471. WOLFSSL_SMALL_STACK_STATIC const byte good2_in[] = { 0x30, 0x80, 0x02, 0x01, 0x01, 0x00, 0x00 };
  39472. WOLFSSL_SMALL_STACK_STATIC const byte good2_out[] = { 0x30, 0x03, 0x02, 0x01, 0x01 };
  39473. WOLFSSL_SMALL_STACK_STATIC const byte good3_in[] = {
  39474. 0x24, 0x80, 0x04, 0x01, 0x01, 0x00, 0x00
  39475. };
  39476. WOLFSSL_SMALL_STACK_STATIC const byte good3_out[] = { 0x04, 0x1, 0x01 };
  39477. WOLFSSL_SMALL_STACK_STATIC const byte good4_in[] = {
  39478. 0x30, 0x80,
  39479. 0x02, 0x01, 0x01,
  39480. 0x30, 0x80,
  39481. 0x24, 0x80,
  39482. 0x04, 0x01, 0x01,
  39483. 0x04, 0x02, 0x02, 0x03,
  39484. 0x00, 0x00,
  39485. 0x06, 0x01, 0x01,
  39486. 0x00, 0x00,
  39487. 0x31, 0x80,
  39488. 0x06, 0x01, 0x01,
  39489. 0x00, 0x00,
  39490. 0x00, 0x00,
  39491. };
  39492. WOLFSSL_SMALL_STACK_STATIC const byte good4_out[] = {
  39493. 0x30, 0x12,
  39494. 0x02, 0x01, 0x01,
  39495. 0x30, 0x08,
  39496. 0x04, 0x03, 0x01, 0x02, 0x03,
  39497. 0x06, 0x01, 0x01,
  39498. 0x31, 0x03,
  39499. 0x06, 0x01, 0x01
  39500. };
  39501. WOLFSSL_SMALL_STACK_STATIC const byte good5_in[] = { 0x30, 0x03, 0x02, 0x01, 0x01 };
  39502. berDerTestData testData[] = {
  39503. { good1_in, sizeof(good1_in), good1_out, sizeof(good1_out) },
  39504. { good2_in, sizeof(good2_in), good2_out, sizeof(good2_out) },
  39505. { good3_in, sizeof(good3_in), good3_out, sizeof(good3_out) },
  39506. { good4_in, sizeof(good4_in), good4_out, sizeof(good4_out) },
  39507. { good5_in, sizeof(good5_in), good5_in , sizeof(good5_in ) },
  39508. };
  39509. for (i = 0; i < (int)(sizeof(testData) / sizeof(*testData)); i++) {
  39510. ret = wc_BerToDer(testData[i].in, testData[i].inSz, NULL, &len);
  39511. if (ret != LENGTH_ONLY_E)
  39512. return -13500 - i;
  39513. if (len != testData[i].outSz)
  39514. return -13510 - i;
  39515. len = testData[i].outSz;
  39516. ret = wc_BerToDer(testData[i].in, testData[i].inSz, out, &len);
  39517. if (ret != 0)
  39518. return -13520 - i;
  39519. if (XMEMCMP(out, testData[i].out, len) != 0)
  39520. return -13530 - i;
  39521. for (l = 1; l < testData[i].inSz; l++) {
  39522. ret = wc_BerToDer(testData[i].in, l, NULL, &len);
  39523. if (ret != ASN_PARSE_E)
  39524. return -13540;
  39525. len = testData[i].outSz;
  39526. ret = wc_BerToDer(testData[i].in, l, out, &len);
  39527. if (ret != ASN_PARSE_E)
  39528. return -13541;
  39529. }
  39530. for (l = 0; l < testData[i].outSz-1; l++) {
  39531. ret = wc_BerToDer(testData[i].in, testData[i].inSz, out, &l);
  39532. if (ret != BUFFER_E)
  39533. return -13542;
  39534. }
  39535. }
  39536. ret = wc_BerToDer(NULL, 4, NULL, NULL);
  39537. if (ret != BAD_FUNC_ARG)
  39538. return -13543;
  39539. ret = wc_BerToDer(out, 4, NULL, NULL);
  39540. if (ret != BAD_FUNC_ARG)
  39541. return -13544;
  39542. ret = wc_BerToDer(NULL, 4, NULL, &len);
  39543. if (ret != BAD_FUNC_ARG)
  39544. return -13545;
  39545. ret = wc_BerToDer(NULL, 4, out, NULL);
  39546. if (ret != BAD_FUNC_ARG)
  39547. return -13546;
  39548. ret = wc_BerToDer(out, 4, out, NULL);
  39549. if (ret != BAD_FUNC_ARG)
  39550. return -13547;
  39551. ret = wc_BerToDer(NULL, 4, out, &len);
  39552. if (ret != BAD_FUNC_ARG)
  39553. return -13548;
  39554. for (l = 1; l < sizeof(good4_out); l++) {
  39555. len = l;
  39556. ret = wc_BerToDer(good4_in, sizeof(good4_in), out, &len);
  39557. if (ret != BUFFER_E)
  39558. return -13549;
  39559. }
  39560. return 0;
  39561. }
  39562. #endif
  39563. #ifdef DEBUG_WOLFSSL
  39564. static THREAD_LS_T int log_cnt = 0;
  39565. static void my_Logging_cb(const int logLevel, const char *const logMessage)
  39566. {
  39567. (void)logLevel;
  39568. (void)logMessage;
  39569. log_cnt++;
  39570. }
  39571. #endif /* DEBUG_WOLFSSL */
  39572. WOLFSSL_TEST_SUBROUTINE int logging_test(void)
  39573. {
  39574. #ifdef DEBUG_WOLFSSL
  39575. const char* msg = "Testing, testing. 1, 2, 3, 4 ...";
  39576. byte a[8] = { 1, 2, 3, 4, 5, 6, 7, 8 };
  39577. byte b[256];
  39578. int i;
  39579. for (i = 0; i < (int)sizeof(b); i++)
  39580. b[i] = i;
  39581. if (wolfSSL_Debugging_ON() != 0)
  39582. return -13600;
  39583. if (wolfSSL_SetLoggingCb(my_Logging_cb) != 0)
  39584. return -13601;
  39585. WOLFSSL_MSG(msg);
  39586. WOLFSSL_BUFFER(a, sizeof(a));
  39587. WOLFSSL_BUFFER(b, sizeof(b));
  39588. WOLFSSL_BUFFER(NULL, 0);
  39589. WOLFSSL_ERROR(MEMORY_E);
  39590. WOLFSSL_ERROR_MSG(msg);
  39591. /* turn off logs */
  39592. wolfSSL_Debugging_OFF();
  39593. /* capture log count */
  39594. i = log_cnt;
  39595. /* validate no logs are output when disabled */
  39596. WOLFSSL_MSG(msg);
  39597. WOLFSSL_BUFFER(a, sizeof(a));
  39598. WOLFSSL_BUFFER(b, sizeof(b));
  39599. WOLFSSL_BUFFER(NULL, 0);
  39600. WOLFSSL_ERROR(MEMORY_E);
  39601. WOLFSSL_ERROR_MSG(msg);
  39602. /* check the logs were disabled */
  39603. if (i != log_cnt)
  39604. return -13602;
  39605. /* restore callback and leave logging enabled */
  39606. wolfSSL_SetLoggingCb(NULL);
  39607. wolfSSL_Debugging_ON();
  39608. /* suppress unused args */
  39609. (void)a;
  39610. (void)b;
  39611. #else
  39612. if (wolfSSL_Debugging_ON() != NOT_COMPILED_IN)
  39613. return -13603;
  39614. wolfSSL_Debugging_OFF();
  39615. if (wolfSSL_SetLoggingCb(NULL) != NOT_COMPILED_IN)
  39616. return -13604;
  39617. #endif /* DEBUG_WOLFSSL */
  39618. return 0;
  39619. }
  39620. WOLFSSL_TEST_SUBROUTINE int mutex_test(void)
  39621. {
  39622. #ifdef WOLFSSL_PTHREADS
  39623. wolfSSL_Mutex m;
  39624. #endif
  39625. #if !defined(WOLFSSL_NO_MALLOC) && !defined(WOLFSSL_USER_MUTEX)
  39626. wolfSSL_Mutex *mm = wc_InitAndAllocMutex();
  39627. if (mm == NULL)
  39628. return -13700;
  39629. wc_FreeMutex(mm);
  39630. XFREE(mm, HEAP_HINT, DYNAMIC_TYPE_MUTEX);
  39631. #endif
  39632. /* Can optionally enable advanced pthread tests using "ENABLE_PTHREAD_LOCKFREE_TESTS" */
  39633. #ifdef WOLFSSL_PTHREADS
  39634. if (wc_InitMutex(&m) != 0)
  39635. return -13701;
  39636. if (wc_LockMutex(&m) != 0)
  39637. return -13702;
  39638. #if !defined(WOLFSSL_SOLARIS) && defined(ENABLE_PTHREAD_LOCKFREE_TESTS)
  39639. /* trying to free a locked mutex is not portable behavior with pthread */
  39640. /* Attempting to destroy a locked mutex results in undefined behavior */
  39641. if (wc_FreeMutex(&m) != BAD_MUTEX_E)
  39642. return -13703;
  39643. #endif
  39644. if (wc_UnLockMutex(&m) != 0)
  39645. return -13704;
  39646. if (wc_FreeMutex(&m) != 0)
  39647. return -13705;
  39648. #if !defined(WOLFSSL_SOLARIS) && defined(ENABLE_PTHREAD_LOCKFREE_TESTS)
  39649. /* Trying to use a pthread after free'ing is not portable behavior */
  39650. if (wc_LockMutex(&m) != BAD_MUTEX_E)
  39651. return -13706;
  39652. if (wc_UnLockMutex(&m) != BAD_MUTEX_E)
  39653. return -13707;
  39654. #endif
  39655. #endif
  39656. return 0;
  39657. }
  39658. #if defined(USE_WOLFSSL_MEMORY) && !defined(FREERTOS)
  39659. #if !defined(WOLFSSL_NO_MALLOC) && !defined(WOLFSSL_LINUXKM) && \
  39660. !defined(WOLFSSL_STATIC_MEMORY)
  39661. static int malloc_cnt = 0;
  39662. static int realloc_cnt = 0;
  39663. static int free_cnt = 0;
  39664. #ifdef WOLFSSL_DEBUG_MEMORY
  39665. static void *my_Malloc_cb(size_t size, const char* func, unsigned int line)
  39666. {
  39667. (void) func;
  39668. (void) line;
  39669. #else
  39670. static void *my_Malloc_cb(size_t size)
  39671. {
  39672. #endif
  39673. malloc_cnt++;
  39674. #ifndef WOLFSSL_NO_MALLOC
  39675. return malloc(size);
  39676. #else
  39677. WOLFSSL_MSG("No malloc available");
  39678. (void)size;
  39679. return NULL;
  39680. #endif
  39681. }
  39682. #ifdef WOLFSSL_DEBUG_MEMORY
  39683. static void my_Free_cb(void *ptr, const char* func, unsigned int line)
  39684. {
  39685. (void) func;
  39686. (void) line;
  39687. #else
  39688. static void my_Free_cb(void *ptr)
  39689. {
  39690. #endif
  39691. free_cnt++;
  39692. #ifndef WOLFSSL_NO_MALLOC
  39693. free(ptr);
  39694. #else
  39695. WOLFSSL_MSG("No free available");
  39696. (void)ptr;
  39697. #endif
  39698. }
  39699. #ifdef WOLFSSL_DEBUG_MEMORY
  39700. static void *my_Realloc_cb(void *ptr, size_t size, const char* func, unsigned int line)
  39701. {
  39702. (void) func;
  39703. (void) line;
  39704. #else
  39705. static void *my_Realloc_cb(void *ptr, size_t size)
  39706. {
  39707. #endif
  39708. realloc_cnt++;
  39709. #ifndef WOLFSSL_NO_MALLOC
  39710. return realloc(ptr, size);
  39711. #else
  39712. WOLFSSL_MSG("No realloc available");
  39713. (void)ptr;
  39714. (void)size;
  39715. return NULL;
  39716. #endif
  39717. }
  39718. #endif /* !WOLFSSL_NO_MALLOC */
  39719. WOLFSSL_TEST_SUBROUTINE int memcb_test(void)
  39720. {
  39721. int ret = 0;
  39722. #if !defined(WOLFSSL_NO_MALLOC) && !defined(WOLFSSL_LINUXKM) && \
  39723. !defined(WOLFSSL_STATIC_MEMORY)
  39724. byte* b = NULL;
  39725. #endif
  39726. wolfSSL_Malloc_cb mc;
  39727. wolfSSL_Free_cb fc;
  39728. wolfSSL_Realloc_cb rc;
  39729. /* Save existing memory callbacks */
  39730. if (wolfSSL_GetAllocators(&mc, &fc, &rc) != 0)
  39731. return -13800;
  39732. #if !defined(WOLFSSL_NO_MALLOC) && !defined(WOLFSSL_LINUXKM) && \
  39733. !defined(WOLFSSL_STATIC_MEMORY)
  39734. /* test realloc */
  39735. b = (byte*)XREALLOC(b, 1024, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  39736. if (b == NULL) {
  39737. ERROR_OUT(-13801, exit_memcb);
  39738. }
  39739. XFREE(b, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  39740. b = NULL;
  39741. /* Use API. */
  39742. if (wolfSSL_SetAllocators((wolfSSL_Malloc_cb)my_Malloc_cb,
  39743. (wolfSSL_Free_cb)my_Free_cb,
  39744. (wolfSSL_Realloc_cb)my_Realloc_cb) != 0) {
  39745. ERROR_OUT(-13802, exit_memcb);
  39746. }
  39747. b = (byte*)XMALLOC(1024, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  39748. b = (byte*)XREALLOC(b, 1024, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  39749. XFREE(b, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  39750. #ifndef WOLFSSL_STATIC_MEMORY
  39751. #ifndef WOLFSSL_CHECK_MEM_ZERO
  39752. if (malloc_cnt != 1 || free_cnt != 1 || realloc_cnt != 1)
  39753. #else
  39754. /* Checking zeroized memory means realloc is a malloc and free. */
  39755. if (malloc_cnt != 2 || free_cnt != 2 || realloc_cnt != 0)
  39756. #endif
  39757. #else
  39758. if (malloc_cnt != 0 || free_cnt != 0 || realloc_cnt != 0)
  39759. #endif
  39760. ret = -13803;
  39761. #endif /* !WOLFSSL_NO_MALLOC */
  39762. #if !defined(WOLFSSL_NO_MALLOC) && !defined(WOLFSSL_LINUXKM) && \
  39763. !defined(WOLFSSL_STATIC_MEMORY)
  39764. exit_memcb:
  39765. /* reset malloc/free/realloc counts */
  39766. malloc_cnt = 0;
  39767. free_cnt = 0;
  39768. realloc_cnt = 0;
  39769. #endif
  39770. /* restore memory callbacks */
  39771. wolfSSL_SetAllocators(mc, fc, rc);
  39772. return ret;
  39773. }
  39774. #endif /* USE_WOLFSSL_MEMORY && !WOLFSSL_NO_MALLOC */
  39775. #ifdef WOLFSSL_IMX6_CAAM_BLOB
  39776. WOLFSSL_TEST_SUBROUTINE int blob_test(void)
  39777. {
  39778. int ret = 0;
  39779. byte out[112];
  39780. byte blob[112];
  39781. word32 outSz;
  39782. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  39783. {
  39784. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  39785. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  39786. };
  39787. WOLFSSL_SMALL_STACK_STATIC const byte text[] =
  39788. {
  39789. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  39790. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  39791. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  39792. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  39793. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  39794. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  39795. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  39796. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  39797. };
  39798. XMEMSET(blob, 0, sizeof(blob));
  39799. XMEMSET(out, 0, sizeof(out));
  39800. outSz = sizeof(blob);
  39801. ret = wc_caamCreateBlob((byte*)iv, sizeof(iv), blob, &outSz);
  39802. if (ret != 0) {
  39803. ERROR_OUT(-13900, exit_blob);
  39804. }
  39805. blob[outSz - 2] += 1;
  39806. ret = wc_caamOpenBlob(blob, outSz, out, &outSz);
  39807. if (ret == 0) { /* should fail with altered blob */
  39808. ERROR_OUT(-13901, exit_blob);
  39809. }
  39810. XMEMSET(blob, 0, sizeof(blob));
  39811. outSz = sizeof(blob);
  39812. ret = wc_caamCreateBlob((byte*)iv, sizeof(iv), blob, &outSz);
  39813. if (ret != 0) {
  39814. ERROR_OUT(-13902, exit_blob);
  39815. }
  39816. ret = wc_caamOpenBlob(blob, outSz, out, &outSz);
  39817. if (ret != 0) {
  39818. ERROR_OUT(-13903, exit_blob);
  39819. }
  39820. if (XMEMCMP(out, iv, sizeof(iv))) {
  39821. ERROR_OUT(-13904, exit_blob);
  39822. }
  39823. XMEMSET(blob, 0, sizeof(blob));
  39824. outSz = sizeof(blob);
  39825. ret = wc_caamCreateBlob((byte*)text, sizeof(text), blob, &outSz);
  39826. if (ret != 0) {
  39827. ERROR_OUT(-13905, exit_blob);
  39828. }
  39829. ret = wc_caamOpenBlob(blob, outSz, out, &outSz);
  39830. if (ret != 0) {
  39831. ERROR_OUT(-13906, exit_blob);
  39832. }
  39833. if (XMEMCMP(out, text, sizeof(text))) {
  39834. ERROR_OUT(-13907, exit_blob);
  39835. }
  39836. exit_blob:
  39837. return ret;
  39838. }
  39839. #endif /* WOLFSSL_IMX6_CAAM_BLOB */
  39840. #ifdef WOLF_CRYPTO_CB
  39841. /* Example custom context for crypto callback */
  39842. typedef struct {
  39843. int exampleVar; /* flag for testing if only crypt is enabled. */
  39844. } myCryptoDevCtx;
  39845. #ifdef WOLF_CRYPTO_CB_ONLY_RSA
  39846. /* Testing rsa cb when CB_ONLY_RSA is enabled
  39847. * When CB_ONLY_RSA is enabled, software imple. is not available.
  39848. *
  39849. * ctx callback ctx
  39850. * returen 0 on success, otherwise return -8000 - -8007
  39851. */
  39852. static int rsa_onlycb_test(myCryptoDevCtx *ctx)
  39853. {
  39854. int ret = 0;
  39855. #if !defined(NO_RSA)
  39856. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  39857. RsaKey *key = (RsaKey *)XMALLOC(sizeof *key,
  39858. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  39859. byte* tmp = NULL;
  39860. #else
  39861. RsaKey key[1];
  39862. byte tmp[FOURK_BUF];
  39863. #endif
  39864. size_t bytes;
  39865. const word32 inLen = (word32)TEST_STRING_SZ;
  39866. word32 idx = 0;
  39867. word32 sigSz;
  39868. WOLFSSL_SMALL_STACK_STATIC const byte in[] = TEST_STRING;
  39869. byte out[RSA_TEST_BYTES];
  39870. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  39871. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096) && \
  39872. !defined(NO_FILESYSTEM)
  39873. XFILE file;
  39874. #endif
  39875. #ifdef WOLFSSL_KEY_GEN
  39876. WC_RNG rng;
  39877. #endif
  39878. #ifdef USE_CERT_BUFFERS_1024
  39879. bytes = (size_t)sizeof_client_key_der_1024;
  39880. if (bytes < (size_t)sizeof_client_cert_der_1024)
  39881. bytes = (size_t)sizeof_client_cert_der_1024;
  39882. #elif defined(USE_CERT_BUFFERS_2048)
  39883. bytes = (size_t)sizeof_client_key_der_2048;
  39884. if (bytes < (size_t)sizeof_client_cert_der_2048)
  39885. bytes = (size_t)sizeof_client_cert_der_2048;
  39886. #elif defined(USE_CERT_BUFFERS_3072)
  39887. bytes = (size_t)sizeof_client_key_der_3072;
  39888. if (bytes < (size_t)sizeof_client_cert_der_3072)
  39889. bytes = (size_t)sizeof_client_cert_der_3072;
  39890. #elif defined(USE_CERT_BUFFERS_4096)
  39891. bytes = (size_t)sizeof_client_key_der_4096;
  39892. if (bytes < (size_t)sizeof_client_cert_der_4096)
  39893. bytes = (size_t)sizeof_client_cert_der_4096;
  39894. #else
  39895. bytes = FOURK_BUF;
  39896. #endif
  39897. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  39898. tmp = (byte*)XMALLOC(bytes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  39899. if (tmp == NULL)
  39900. ERROR_OUT(-8000, exit_onlycb);
  39901. #endif
  39902. #ifdef USE_CERT_BUFFERS_1024
  39903. XMEMCPY(tmp, client_key_der_1024, (size_t)sizeof_client_key_der_1024);
  39904. #elif defined(USE_CERT_BUFFERS_2048)
  39905. XMEMCPY(tmp, client_key_der_2048, (size_t)sizeof_client_key_der_2048);
  39906. #elif defined(USE_CERT_BUFFERS_3072)
  39907. XMEMCPY(tmp, client_key_der_3072, (size_t)sizeof_client_key_der_3072);
  39908. #elif defined(USE_CERT_BUFFERS_4096)
  39909. XMEMCPY(tmp, client_key_der_4096, (size_t)sizeof_client_key_der_4096);
  39910. #elif !defined(NO_FILESYSTEM)
  39911. file = XFOPEN(clientKey, "rb");
  39912. if (!file) {
  39913. err_sys("can't open ./certs/client-key.der, "
  39914. "Please run from wolfSSL home dir", -40);
  39915. ERROR_OUT(-8001, exit_onlycb);
  39916. }
  39917. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  39918. XFCLOSE(file);
  39919. #endif
  39920. #ifdef WOLFSSL_KEY_GEN
  39921. /* wc_CryptoCb_MakeRsaKey cb test, no actual making key
  39922. * wc_MakeRsaKey() -> rsa cb ->
  39923. * myCryptoDevCb -> wc_MakeRsaKey(CBONLY_TEST_DEVID)
  39924. * wc_MakeRsaKey(CBONLY_TEST_DEVID) expects to return 0(success)
  39925. */
  39926. ctx->exampleVar = 99;
  39927. ret = wc_MakeRsaKey(key, keySz, WC_RSA_EXPONENT, rng);
  39928. if (ret != 0) {
  39929. ERROR_OUT(-8002, exit_onlycb);
  39930. }
  39931. /* wc_MakeRsaKey() -> rsa cb ->
  39932. * myCryptoDevCb -> wc_MakeRsaKey(INVALID_DEVID)
  39933. * wc_MakeRsaKey(CBONLY_TEST_DEVID) expects to return NO_VALID_DEVID(failure)
  39934. */
  39935. ctx->exampleVar = 1;
  39936. ret = wc_MakeRsaKey(key, keySz, WC_RSA_EXPONENT, rng);
  39937. if (ret != NO_VALID_DEVID) {
  39938. ERROR_OUT(-8003, exit_onlycb);
  39939. } else
  39940. /* reset return code */
  39941. ret = 0;
  39942. #endif
  39943. ret = wc_InitRsaKey_ex(key, HEAP_HINT, devId);
  39944. if (ret != 0) {
  39945. ERROR_OUT(-8004, exit_onlycb);
  39946. }
  39947. ret = wc_RsaPrivateKeyDecode(tmp, &idx, key, (word32)bytes);
  39948. if (ret != 0) {
  39949. ERROR_OUT(-8005, exit_onlycb);
  39950. }
  39951. sigSz = (word32)wc_RsaEncryptSize(key);
  39952. /* wc_CryptoCb_Rsa cb test, no actual rsa operation */
  39953. if (ret == 0) {
  39954. /* wc_SignatureGenerate() -> rsa cb ->
  39955. * myCryptoDevCb -> wc_RsaFunction(CBONLY_TEST_DEVID)
  39956. * wc_RsaFunction(CBONLY_TEST_DEVID) expects to return 0(success)
  39957. */
  39958. ctx->exampleVar = 99;
  39959. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA,
  39960. in, inLen, out, &sigSz, key, sizeof(*key), NULL);
  39961. if (ret != 0) {
  39962. ERROR_OUT(-8006, exit_onlycb);
  39963. }
  39964. }
  39965. if (ret == 0) {
  39966. /* wc_SignatureGenerate() -> rsa cb ->
  39967. * myCryptoDevCb -> wc_RsaFunction(INVALID_DEVID)
  39968. * wc_SignatureGenerate(INVALID_DEVID) expects to
  39969. * return NO_VALID_DEVID(failure)
  39970. */
  39971. ctx->exampleVar = 1;
  39972. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA,
  39973. in, inLen, out, &sigSz, key, sizeof(*key), NULL);
  39974. if (ret != NO_VALID_DEVID) {
  39975. ERROR_OUT(-8007, exit_onlycb);
  39976. } else
  39977. /* reset return code */
  39978. ret = 0;
  39979. }
  39980. exit_onlycb:
  39981. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  39982. if (key != NULL) {
  39983. wc_FreeRsaKey(key);
  39984. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  39985. }
  39986. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  39987. #else
  39988. wc_FreeRsaKey(key);
  39989. #endif
  39990. #endif
  39991. return ret;
  39992. }
  39993. #endif
  39994. #ifdef WOLF_CRYPTO_CB_ONLY_ECC
  39995. /* Testing rsa cb when CB_ONLY_ECC is enabled
  39996. * When CB_ONLY_ECC is enabled, software imple. is not available.
  39997. *
  39998. * ctx callback ctx
  39999. * returen 0 on success, otherwise return -8008 - -8018
  40000. */
  40001. static int ecc_onlycb_test(myCryptoDevCtx *ctx)
  40002. {
  40003. int ret = 0;
  40004. #if defined(HAVE_ECC)
  40005. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  40006. ecc_key* key = (ecc_key *)XMALLOC(sizeof *key,
  40007. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40008. ecc_key* pub = (ecc_key *)XMALLOC(sizeof *pub,
  40009. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40010. byte* out = (byte*)XMALLOC(sizeof(byte),
  40011. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40012. #ifdef OPENSSL_EXTRA
  40013. byte* check = (byte*)XMALLOC(sizeof(byte)*(256), HEAP_HINT,
  40014. DYNAMIC_TYPE_TMP_BUFFER);
  40015. #endif
  40016. #else
  40017. ecc_key key[1];
  40018. ecc_key pub[1];
  40019. byte out[256];
  40020. #ifdef OPENSSL_EXTRA
  40021. unsigned char check[256];
  40022. #endif
  40023. #endif
  40024. #ifdef OPENSSL_EXTRA
  40025. EVP_PKEY* privKey = NULL;
  40026. EVP_PKEY* pubKey = NULL;
  40027. ecc_key* pkey;
  40028. EVP_MD_CTX mdCtx;
  40029. const char testData[] = "Hi There";
  40030. size_t checkSz = -1;
  40031. const unsigned char* cp;
  40032. const unsigned char* p;
  40033. const unsigned char check_v[256] = {
  40034. 0x30,0x45,0x02,0x20,0x1b,0x5c,0x2a,0xf0,0x18,0x09,
  40035. 0x74,0x65,0xa1,0x04,0x76,0x3a,0xce,0xcc,0xe5,0x34,
  40036. 0x5e,0x89,0xed,0x40,0x1e,0x5a,0xb1,0x53,0xb4,0xff,
  40037. 0xc7,0x18,0xfe,0x0f,0xc7,0xa6,0x02,0x21,0x00,0xe5,
  40038. 0x70,0x21,0xfc,0xf9,0x63,0x36,0xfd,0x16,0x18,0x08,
  40039. 0x9a,0x63,0x61,0x0f,0xe7,0x7c,0xa3,0xc9,0x14,0xa3,
  40040. 0x30,0x87,0xf7,0xf5,0x70,0x19,0xaf,0x56,0x96,0x9b,
  40041. 0xd8,0x64,0xcd,0xd9,0xff,0x7b,0x2a,0x55,0x52,0xca,
  40042. 0x41,0xb2,0xa6,0xa4,0x8a,0x3b,0x02,0x20,0x8c,0xc5,
  40043. 0xf9,0xc1,0x7d,0x2a,0x65,0x6c,0xe6,0x5a,0xe3,0x76,
  40044. 0x9b,0xab,0x0b,0x9f,0xaf,0x62,0x5d,0xb2,0x60,0xd7,
  40045. 0xeb,0xb4,0x1b,0x73,0xdc,0x01,0x7d,0x7b,0xab,0xc1,
  40046. 0x0c,0x74,0x96,0x41,0xe6,0x3f,0xc5,0x86,0xe6,0x7d,
  40047. 0x2b,0x9d,0x54,0x6b,0xcd,0x31,0x35,0x1f,0xdb,0x49,
  40048. 0x1f,0x32,0x34,0xf8,0x57,0x12,0x86,0x5c,0x0e,0x80,
  40049. 0x55,0x8d,0xff,0xd8,0xbd,0xdf,0x32,0x26,0x62,0x42,
  40050. 0x09,0xda,0xf7,0x74,0xf2,0x3f,0xe6,0xf1,0x77,0x82,
  40051. 0xce,0xe4,0xbb,0x61,0xa6,0xc0,0x17,0x0c,0x6c,0x47,
  40052. 0x2a,0x40,0x1c,0x2b,0xe0,0x98,0x3b,0xbf,0xc6,0xf8,
  40053. 0x6d,0xfd,0xd0,0xfa,0xc1,0x02,0xfb,0x5f,0xfb,0xb0,
  40054. 0xcb,0xd9,0xa3,0x59,0x94,0xe9,0x0f,0x74,0xbb,0x3f,
  40055. 0x64,0xa3,0x83,0xc4,0x2b,0xf7,0xd2,0x97,0xbf,0x3b,
  40056. 0xcf,0xbb,0x60,0x81,0x33,0x94,0xfa,0x0d,0x35,0xd2,
  40057. 0x3d,0xb9,0x99,0xe3,0x12,0xf8,0xf4,0xa3,0x74,0xf4,
  40058. 0x94,0x1d,0x7a,0x66,0xf8,0xd1,0x1d,0xcf,0xb0,0x48,
  40059. 0xef,0x8c,0x94,0x6f,0xdd,0x62,
  40060. };
  40061. #endif
  40062. WC_RNG rng;
  40063. EncryptedInfo encInfo;
  40064. int keyFormat = 0;
  40065. word32 keyIdx = 0;
  40066. byte in[] = "Everyone gets Friday off. ecc p";
  40067. word32 inLen = (word32)XSTRLEN((char*)in);
  40068. word32 outLen;
  40069. int verify;
  40070. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  40071. if (key == NULL || pub == NULL) {
  40072. ERROR_OUT(-8008, exit_onlycb);
  40073. }
  40074. #endif
  40075. ret = wc_ecc_init_ex(key, HEAP_HINT, devId);
  40076. if (ret != 0) {
  40077. ERROR_OUT(-8009, exit_onlycb);
  40078. }
  40079. /* wc_CryptoCb_MakeEccKey cb test, , no actual testing */
  40080. ctx->exampleVar = 99;
  40081. ret = wc_ecc_make_key(&rng, ECC_KEYGEN_SIZE, key);
  40082. if (ret != 0) {
  40083. ERROR_OUT(-8010, exit_onlycb);
  40084. }
  40085. ctx->exampleVar = 1;
  40086. ret = wc_ecc_make_key(&rng, ECC_KEYGEN_SIZE, key);
  40087. if (ret != NO_VALID_DEVID) {
  40088. ERROR_OUT(-8011, exit_onlycb);
  40089. } else
  40090. /* reset return code */
  40091. ret = 0;
  40092. #ifdef USE_CERT_BUFFERS_256
  40093. if (ret == 0) {
  40094. /* load ECC private key and perform private transform */
  40095. ret = wc_EccPrivateKeyDecode(ecc_key_der_256, &keyIdx,
  40096. key, sizeof_ecc_key_der_256);
  40097. }
  40098. if (ret != 0) {
  40099. ERROR_OUT(-8012, exit_onlycb);
  40100. }
  40101. /* wc_CryptoCb_EccSign cb test, no actual testing */
  40102. ctx->exampleVar = 99;
  40103. if (ret == 0) {
  40104. ret = wc_ecc_sign_hash(in, inLen, out, &outLen, &rng, key);
  40105. }
  40106. if (ret != 0) {
  40107. ERROR_OUT(-8013, exit_onlycb);
  40108. }
  40109. ctx->exampleVar = 1;
  40110. if (ret == 0) {
  40111. ret = wc_ecc_sign_hash(in, inLen, out, &outLen, &rng, key);
  40112. }
  40113. if (ret != NO_VALID_DEVID) {
  40114. ERROR_OUT(-8014, exit_onlycb);
  40115. }
  40116. else
  40117. ret = 0;
  40118. /* wc_CryptoCb_EccVerify cb test, no actual testing */
  40119. ctx->exampleVar = 99;
  40120. if (ret == 0) {
  40121. ret = wc_ecc_verify_hash(in, inLen, out, outLen, &verify, key);
  40122. }
  40123. if (ret != 0) {
  40124. ERROR_OUT(-8015, exit_onlycb);
  40125. }
  40126. ctx->exampleVar = 1;
  40127. if (ret == 0) {
  40128. ret = wc_ecc_verify_hash(in, inLen, out, outLen, &verify, key);
  40129. }
  40130. if (ret != NO_VALID_DEVID) {
  40131. ERROR_OUT(-8016, exit_onlycb);
  40132. }
  40133. else
  40134. ret = 0;
  40135. /* wc_CryptoCb_Ecdh cb test, no actual testing */
  40136. /* make public key for shared secret */
  40137. wc_ecc_init_ex(pub, HEAP_HINT, devId);
  40138. ctx->exampleVar = 99;
  40139. if (ret == 0) {
  40140. ret = wc_ecc_shared_secret(key, pub, out, &outLen);
  40141. }
  40142. if (ret != 0) {
  40143. ERROR_OUT(-8017, exit_onlycb);
  40144. }
  40145. ctx->exampleVar = 1;
  40146. if (ret == 0) {
  40147. ret = wc_ecc_shared_secret(key, pub, out, &outLen);
  40148. }
  40149. if (ret != NO_VALID_DEVID) {
  40150. ERROR_OUT(-8018, exit_onlycb);
  40151. }
  40152. else
  40153. ret = 0;
  40154. #ifdef OPENSSL_EXTRA
  40155. (void)pkey;
  40156. cp = ecc_clikey_der_256;
  40157. privKey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &cp,
  40158. sizeof_ecc_clikey_der_256);
  40159. if (privKey == NULL) {
  40160. ERROR_OUT(-8019, exit_onlycb);
  40161. }
  40162. pkey = (ecc_key*)privKey->ecc->internal;
  40163. pkey->devId = devId;
  40164. p = ecc_clikeypub_der_256;
  40165. pubKey = d2i_PUBKEY(NULL, &p, sizeof_ecc_clikeypub_der_256);
  40166. if (pubKey == NULL) {
  40167. ERROR_OUT(-8020, exit_onlycb);
  40168. }
  40169. pkey = (ecc_key*)pubKey->ecc->internal;
  40170. pkey->devId = devId;
  40171. /* sign */
  40172. EVP_MD_CTX_init(&mdCtx);
  40173. ret = EVP_DigestSignInit(&mdCtx, NULL, EVP_sha256(), NULL, privKey);
  40174. if (ret != WOLFSSL_SUCCESS) {
  40175. ERROR_OUT(-8021, exit_onlycb);
  40176. }
  40177. ret = EVP_DigestSignUpdate(&mdCtx, testData,
  40178. (unsigned int)XSTRLEN(testData));
  40179. if (ret != WOLFSSL_SUCCESS) {
  40180. ERROR_OUT(-8022, exit_onlycb);
  40181. }
  40182. ret = EVP_DigestSignFinal(&mdCtx, NULL, &checkSz);
  40183. if (ret != WOLFSSL_SUCCESS) {
  40184. ERROR_OUT(-8023, exit_onlycb);
  40185. }
  40186. ctx->exampleVar = 99;
  40187. ret = EVP_DigestSignFinal(&mdCtx, check, &checkSz);
  40188. /* just called crypt callback as dummy
  40189. * EVP_DigestSignFinal returns 0 internally.
  40190. */
  40191. if (ret != 0) {
  40192. ERROR_OUT(-8024, exit_onlycb);
  40193. }
  40194. ctx->exampleVar = 1;
  40195. ret = EVP_DigestSignFinal(&mdCtx, check, &checkSz);
  40196. /* just called crypt callback as dummy
  40197. * EVP_DigestSignFinal returns 0 internally.
  40198. */
  40199. if (ret != 0) {
  40200. ERROR_OUT(-8025, exit_onlycb);
  40201. }
  40202. /* restore checkSz for verify */
  40203. checkSz = 71;
  40204. ret = EVP_MD_CTX_cleanup(&mdCtx);
  40205. if (ret != SSL_SUCCESS) {
  40206. ERROR_OUT(-8026, exit_onlycb);
  40207. }
  40208. /* verify */
  40209. EVP_MD_CTX_init(&mdCtx);
  40210. if (ret == SSL_SUCCESS) {
  40211. ret = EVP_DigestVerifyInit(&mdCtx, NULL, EVP_sha256(), NULL, pubKey);
  40212. }
  40213. if (ret != WOLFSSL_SUCCESS) {
  40214. ERROR_OUT(-8027, exit_onlycb);
  40215. }
  40216. if (ret == WOLFSSL_SUCCESS) {
  40217. ret = EVP_DigestVerifyUpdate(&mdCtx, testData,
  40218. (unsigned int)XSTRLEN(testData));
  40219. }
  40220. if (ret != WOLFSSL_SUCCESS) {
  40221. ERROR_OUT(-8028, exit_onlycb);
  40222. }
  40223. ctx->exampleVar = 99;
  40224. ret = EVP_DigestVerifyFinal(&mdCtx, check_v, checkSz);
  40225. /* just called crypt callback as dummy
  40226. * EVP_DigestSignFinal returns 0 internally.
  40227. */
  40228. if (ret != 0) {
  40229. ERROR_OUT(-8029, exit_onlycb);
  40230. }
  40231. ctx->exampleVar = 1;
  40232. ret = EVP_DigestVerifyFinal(&mdCtx, check_v, checkSz);
  40233. /* just called crypt callback as dummy
  40234. * EVP_DigestVerifyFinal returns -1 internally rather than NO_VALID_DEVID.
  40235. */
  40236. if (ret != -1) {
  40237. ERROR_OUT(-8030, exit_onlycb);
  40238. }
  40239. ret = EVP_MD_CTX_cleanup(&mdCtx);
  40240. if (ret != SSL_SUCCESS) {
  40241. ERROR_OUT(-8031, exit_onlycb);
  40242. } else
  40243. ret = 0;
  40244. #endif
  40245. #else
  40246. (void)verify;
  40247. (void)outLen;
  40248. (void)inLen;
  40249. (void)out;
  40250. (void)pub;
  40251. #ifdef OPENSSL_EXTRA
  40252. (void)privKey;
  40253. (void)pubKey;
  40254. (void)mdCtx;
  40255. (void)check;
  40256. (void)checkSz;
  40257. (void)p;
  40258. #endif
  40259. #endif
  40260. (void)keyFormat;
  40261. (void)encInfo;
  40262. exit_onlycb:
  40263. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  40264. if (key != NULL) {
  40265. wc_ecc_free(key);
  40266. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40267. }
  40268. if (pub != NULL) {
  40269. XFREE(pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40270. }
  40271. if (out != NULL) {
  40272. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40273. }
  40274. #ifdef OPENSSL_EXTRA
  40275. if (check) {
  40276. FREE(check, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40277. }
  40278. #endif
  40279. #else
  40280. wc_ecc_free(key);
  40281. #ifdef OPENSSL_EXTRA
  40282. if (privKey)
  40283. EVP_PKEY_free(privKey);
  40284. if (pubKey)
  40285. EVP_PKEY_free(pubKey);
  40286. #endif
  40287. #endif
  40288. #endif /* HAVE_ECC */
  40289. return ret;
  40290. }
  40291. #endif
  40292. /* Example crypto dev callback function that calls software version */
  40293. static int myCryptoDevCb(int devIdArg, wc_CryptoInfo* info, void* ctx)
  40294. {
  40295. int ret = NOT_COMPILED_IN; /* return this to bypass HW and use SW */
  40296. myCryptoDevCtx* myCtx = (myCryptoDevCtx*)ctx;
  40297. if (info == NULL)
  40298. return BAD_FUNC_ARG;
  40299. #ifdef DEBUG_WOLFSSL
  40300. printf("CryptoDevCb: Algo Type %d\n", info->algo_type);
  40301. #endif
  40302. if (info->algo_type == WC_ALGO_TYPE_RNG) {
  40303. #ifndef WC_NO_RNG
  40304. /* set devId to invalid, so software is used */
  40305. info->rng.rng->devId = INVALID_DEVID;
  40306. ret = wc_RNG_GenerateBlock(info->rng.rng,
  40307. info->rng.out, info->rng.sz);
  40308. /* reset devId */
  40309. info->rng.rng->devId = devIdArg;
  40310. #endif
  40311. }
  40312. else if (info->algo_type == WC_ALGO_TYPE_SEED) {
  40313. #ifndef WC_NO_RNG
  40314. ALIGN32 static byte seed[sizeof(word32)] = { 0x00, 0x00, 0x00, 0x01 };
  40315. word32* seedWord32 = (word32*)seed;
  40316. word32 len;
  40317. /* wc_GenerateSeed is a local symbol so we need to fake the entropy. */
  40318. while (info->seed.sz > 0) {
  40319. len = (word32)sizeof(seed);
  40320. if (info->seed.sz < len)
  40321. len = info->seed.sz;
  40322. XMEMCPY(info->seed.seed, seed, sizeof(seed));
  40323. info->seed.seed += len;
  40324. info->seed.sz -= len;
  40325. (*seedWord32)++;
  40326. }
  40327. ret = 0;
  40328. #endif
  40329. }
  40330. else if (info->algo_type == WC_ALGO_TYPE_PK) {
  40331. #ifdef DEBUG_WOLFSSL
  40332. printf("CryptoDevCb: Pk Type %d\n", info->pk.type);
  40333. #endif
  40334. #ifndef NO_RSA
  40335. if (info->pk.type == WC_PK_TYPE_RSA) {
  40336. /* set devId to invalid, so software is used */
  40337. info->pk.rsa.key->devId = INVALID_DEVID;
  40338. #if defined(WOLF_CRYPTO_CB_ONLY_RSA)
  40339. #ifdef DEBUG_WOLFSSL
  40340. printf("CryptoDevCb: exampleVar %d\n", myCtx->exampleVar);
  40341. #endif
  40342. if (myCtx->exampleVar == 99) {
  40343. info->pk.rsa.key->devId = devIdArg;
  40344. return 0;
  40345. }
  40346. #endif
  40347. switch (info->pk.rsa.type) {
  40348. case RSA_PUBLIC_ENCRYPT:
  40349. case RSA_PUBLIC_DECRYPT:
  40350. /* perform software based RSA public op */
  40351. ret = wc_RsaFunction(
  40352. info->pk.rsa.in, info->pk.rsa.inLen,
  40353. info->pk.rsa.out, info->pk.rsa.outLen,
  40354. info->pk.rsa.type, info->pk.rsa.key, info->pk.rsa.rng);
  40355. break;
  40356. case RSA_PRIVATE_ENCRYPT:
  40357. case RSA_PRIVATE_DECRYPT:
  40358. /* perform software based RSA private op */
  40359. ret = wc_RsaFunction(
  40360. info->pk.rsa.in, info->pk.rsa.inLen,
  40361. info->pk.rsa.out, info->pk.rsa.outLen,
  40362. info->pk.rsa.type, info->pk.rsa.key, info->pk.rsa.rng);
  40363. break;
  40364. }
  40365. /* reset devId */
  40366. info->pk.rsa.key->devId = devIdArg;
  40367. }
  40368. #ifdef WOLFSSL_KEY_GEN
  40369. else if (info->pk.type == WC_PK_TYPE_RSA_KEYGEN) {
  40370. info->pk.rsakg.key->devId = INVALID_DEVID;
  40371. #if defined(WOLF_CRYPTO_CB_ONLY_RSA)
  40372. #ifdef DEBUG_WOLFSSL
  40373. printf("CryptoDevCb: exampleVar %d\n", myCtx->exampleVar);
  40374. #endif
  40375. if (myCtx->exampleVar == 99) {
  40376. info->pk.rsakg.key->devId = devIdArg;
  40377. return 0;
  40378. }
  40379. #endif
  40380. #ifdef HAVE_FIPS
  40381. for (;;) {
  40382. #endif
  40383. ret = wc_MakeRsaKey(info->pk.rsakg.key, info->pk.rsakg.size,
  40384. info->pk.rsakg.e, info->pk.rsakg.rng);
  40385. #ifdef HAVE_FIPS
  40386. if (ret == PRIME_GEN_E)
  40387. continue;
  40388. break;
  40389. }
  40390. #endif
  40391. /* reset devId */
  40392. info->pk.rsakg.key->devId = devIdArg;
  40393. }
  40394. #endif
  40395. #endif /* !NO_RSA */
  40396. #ifdef HAVE_ECC
  40397. if (info->pk.type == WC_PK_TYPE_EC_KEYGEN) {
  40398. /* set devId to invalid, so software is used */
  40399. info->pk.eckg.key->devId = INVALID_DEVID;
  40400. #if defined(WOLF_CRYPTO_CB_ONLY_ECC)
  40401. #ifdef DEBUG_WOLFSSL
  40402. printf("CryptoDevCb: exampleVar %d\n", myCtx->exampleVar);
  40403. #endif
  40404. if (myCtx->exampleVar == 99) {
  40405. info->pk.eckg.key->devId = devIdArg;
  40406. return 0;
  40407. }
  40408. #endif
  40409. ret = wc_ecc_make_key_ex(info->pk.eckg.rng, info->pk.eckg.size,
  40410. info->pk.eckg.key, info->pk.eckg.curveId);
  40411. /* reset devId */
  40412. info->pk.eckg.key->devId = devIdArg;
  40413. }
  40414. else if (info->pk.type == WC_PK_TYPE_ECDSA_SIGN) {
  40415. /* set devId to invalid, so software is used */
  40416. info->pk.eccsign.key->devId = INVALID_DEVID;
  40417. #if defined(WOLF_CRYPTO_CB_ONLY_ECC)
  40418. #ifdef DEBUG_WOLFSSL
  40419. printf("CryptoDevCb: exampleVar %d\n", myCtx->exampleVar);
  40420. #endif
  40421. if (myCtx->exampleVar == 99) {
  40422. info->pk.eccsign.key->devId = devIdArg;
  40423. return 0;
  40424. }
  40425. #endif
  40426. ret = wc_ecc_sign_hash(
  40427. info->pk.eccsign.in, info->pk.eccsign.inlen,
  40428. info->pk.eccsign.out, info->pk.eccsign.outlen,
  40429. info->pk.eccsign.rng, info->pk.eccsign.key);
  40430. /* reset devId */
  40431. info->pk.eccsign.key->devId = devIdArg;
  40432. }
  40433. else if (info->pk.type == WC_PK_TYPE_ECDSA_VERIFY) {
  40434. /* set devId to invalid, so software is used */
  40435. info->pk.eccverify.key->devId = INVALID_DEVID;
  40436. #if defined(WOLF_CRYPTO_CB_ONLY_ECC)
  40437. #ifdef DEBUG_WOLFSSL
  40438. printf("CryptoDevCb: exampleVar %d\n", myCtx->exampleVar);
  40439. #endif
  40440. if (myCtx->exampleVar == 99) {
  40441. info->pk.eccverify.key->devId = devIdArg;
  40442. return 0;
  40443. }
  40444. #endif
  40445. ret = wc_ecc_verify_hash(
  40446. info->pk.eccverify.sig, info->pk.eccverify.siglen,
  40447. info->pk.eccverify.hash, info->pk.eccverify.hashlen,
  40448. info->pk.eccverify.res, info->pk.eccverify.key);
  40449. /* reset devId */
  40450. info->pk.eccverify.key->devId = devIdArg;
  40451. }
  40452. else if (info->pk.type == WC_PK_TYPE_ECDH) {
  40453. /* set devId to invalid, so software is used */
  40454. info->pk.ecdh.private_key->devId = INVALID_DEVID;
  40455. #if defined(WOLF_CRYPTO_CB_ONLY_ECC)
  40456. #ifdef DEBUG_WOLFSSL
  40457. printf("CryptoDevCb: exampleVar %d\n", myCtx->exampleVar);
  40458. #endif
  40459. if (myCtx->exampleVar == 99) {
  40460. info->pk.ecdh.private_key->devId = devIdArg;
  40461. return 0;
  40462. }
  40463. #endif
  40464. ret = wc_ecc_shared_secret(
  40465. info->pk.ecdh.private_key, info->pk.ecdh.public_key,
  40466. info->pk.ecdh.out, info->pk.ecdh.outlen);
  40467. /* reset devId */
  40468. info->pk.ecdh.private_key->devId = devIdArg;
  40469. }
  40470. #endif /* HAVE_ECC */
  40471. #ifdef HAVE_CURVE25519
  40472. if (info->pk.type == WC_PK_TYPE_CURVE25519_KEYGEN) {
  40473. /* set devId to invalid, so software is used */
  40474. info->pk.curve25519kg.key->devId = INVALID_DEVID;
  40475. ret = wc_curve25519_make_key(info->pk.curve25519kg.rng,
  40476. info->pk.curve25519kg.size, info->pk.curve25519kg.key);
  40477. /* reset devId */
  40478. info->pk.curve25519kg.key->devId = devIdArg;
  40479. }
  40480. else if (info->pk.type == WC_PK_TYPE_CURVE25519) {
  40481. /* set devId to invalid, so software is used */
  40482. info->pk.curve25519.private_key->devId = INVALID_DEVID;
  40483. ret = wc_curve25519_shared_secret_ex(
  40484. info->pk.curve25519.private_key, info->pk.curve25519.public_key,
  40485. info->pk.curve25519.out, info->pk.curve25519.outlen,
  40486. info->pk.curve25519.endian);
  40487. /* reset devId */
  40488. info->pk.curve25519.private_key->devId = devIdArg;
  40489. }
  40490. #endif /* HAVE_CURVE25519 */
  40491. #ifdef HAVE_ED25519
  40492. if (info->pk.type == WC_PK_TYPE_ED25519_KEYGEN) {
  40493. /* set devId to invalid, so software is used */
  40494. info->pk.ed25519kg.key->devId = INVALID_DEVID;
  40495. ret = wc_ed25519_make_key(info->pk.ed25519kg.rng,
  40496. info->pk.ed25519kg.size, info->pk.ed25519kg.key);
  40497. /* reset devId */
  40498. info->pk.ed25519kg.key->devId = devIdArg;
  40499. }
  40500. #ifdef HAVE_ED25519_SIGN
  40501. else if (info->pk.type == WC_PK_TYPE_ED25519_SIGN) {
  40502. /* set devId to invalid, so software is used */
  40503. info->pk.ed25519sign.key->devId = INVALID_DEVID;
  40504. ret = wc_ed25519_sign_msg_ex(
  40505. info->pk.ed25519sign.in, info->pk.ed25519sign.inLen,
  40506. info->pk.ed25519sign.out, info->pk.ed25519sign.outLen,
  40507. info->pk.ed25519sign.key, info->pk.ed25519sign.type,
  40508. info->pk.ed25519sign.context, info->pk.ed25519sign.contextLen);
  40509. /* reset devId */
  40510. info->pk.ed25519sign.key->devId = devIdArg;
  40511. }
  40512. #endif
  40513. #ifdef HAVE_ED25519_VERIFY
  40514. else if (info->pk.type == WC_PK_TYPE_ED25519_VERIFY) {
  40515. /* set devId to invalid, so software is used */
  40516. info->pk.ed25519verify.key->devId = INVALID_DEVID;
  40517. ret = wc_ed25519_verify_msg_ex(
  40518. info->pk.ed25519verify.sig, info->pk.ed25519verify.sigLen,
  40519. info->pk.ed25519verify.msg, info->pk.ed25519verify.msgLen,
  40520. info->pk.ed25519verify.res, info->pk.ed25519verify.key,
  40521. info->pk.ed25519verify.type, info->pk.ed25519verify.context,
  40522. info->pk.ed25519verify.contextLen);
  40523. /* reset devId */
  40524. info->pk.ed25519verify.key->devId = devIdArg;
  40525. }
  40526. #endif
  40527. #endif /* HAVE_ED25519 */
  40528. }
  40529. else if (info->algo_type == WC_ALGO_TYPE_CIPHER) {
  40530. #if !defined(NO_AES) || !defined(NO_DES3)
  40531. #ifdef HAVE_AESGCM
  40532. if (info->cipher.type == WC_CIPHER_AES_GCM) {
  40533. if (info->cipher.enc) {
  40534. /* set devId to invalid, so software is used */
  40535. info->cipher.aesgcm_enc.aes->devId = INVALID_DEVID;
  40536. ret = wc_AesGcmEncrypt(
  40537. info->cipher.aesgcm_enc.aes,
  40538. info->cipher.aesgcm_enc.out,
  40539. info->cipher.aesgcm_enc.in,
  40540. info->cipher.aesgcm_enc.sz,
  40541. info->cipher.aesgcm_enc.iv,
  40542. info->cipher.aesgcm_enc.ivSz,
  40543. info->cipher.aesgcm_enc.authTag,
  40544. info->cipher.aesgcm_enc.authTagSz,
  40545. info->cipher.aesgcm_enc.authIn,
  40546. info->cipher.aesgcm_enc.authInSz);
  40547. /* reset devId */
  40548. info->cipher.aesgcm_enc.aes->devId = devIdArg;
  40549. }
  40550. else {
  40551. /* set devId to invalid, so software is used */
  40552. info->cipher.aesgcm_dec.aes->devId = INVALID_DEVID;
  40553. ret = wc_AesGcmDecrypt(
  40554. info->cipher.aesgcm_dec.aes,
  40555. info->cipher.aesgcm_dec.out,
  40556. info->cipher.aesgcm_dec.in,
  40557. info->cipher.aesgcm_dec.sz,
  40558. info->cipher.aesgcm_dec.iv,
  40559. info->cipher.aesgcm_dec.ivSz,
  40560. info->cipher.aesgcm_dec.authTag,
  40561. info->cipher.aesgcm_dec.authTagSz,
  40562. info->cipher.aesgcm_dec.authIn,
  40563. info->cipher.aesgcm_dec.authInSz);
  40564. /* reset devId */
  40565. info->cipher.aesgcm_dec.aes->devId = devIdArg;
  40566. }
  40567. }
  40568. #endif /* HAVE_AESGCM */
  40569. #ifdef HAVE_AES_CBC
  40570. if (info->cipher.type == WC_CIPHER_AES_CBC) {
  40571. if (info->cipher.enc) {
  40572. /* set devId to invalid, so software is used */
  40573. info->cipher.aescbc.aes->devId = INVALID_DEVID;
  40574. ret = wc_AesCbcEncrypt(
  40575. info->cipher.aescbc.aes,
  40576. info->cipher.aescbc.out,
  40577. info->cipher.aescbc.in,
  40578. info->cipher.aescbc.sz);
  40579. /* reset devId */
  40580. info->cipher.aescbc.aes->devId = devIdArg;
  40581. }
  40582. else {
  40583. /* set devId to invalid, so software is used */
  40584. info->cipher.aescbc.aes->devId = INVALID_DEVID;
  40585. ret = wc_AesCbcDecrypt(
  40586. info->cipher.aescbc.aes,
  40587. info->cipher.aescbc.out,
  40588. info->cipher.aescbc.in,
  40589. info->cipher.aescbc.sz);
  40590. /* reset devId */
  40591. info->cipher.aescbc.aes->devId = devIdArg;
  40592. }
  40593. }
  40594. #endif /* HAVE_AES_CBC */
  40595. #if defined(HAVE_AES_ECB) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  40596. if (info->cipher.type == WC_CIPHER_AES_ECB) {
  40597. if (info->cipher.enc) {
  40598. /* set devId to invalid, so software is used */
  40599. info->cipher.aesecb.aes->devId = INVALID_DEVID;
  40600. ret = wc_AesEcbEncrypt(
  40601. info->cipher.aesecb.aes,
  40602. info->cipher.aesecb.out,
  40603. info->cipher.aesecb.in,
  40604. info->cipher.aesecb.sz);
  40605. /* reset devId */
  40606. info->cipher.aesecb.aes->devId = devIdArg;
  40607. }
  40608. else {
  40609. /* set devId to invalid, so software is used */
  40610. info->cipher.aesecb.aes->devId = INVALID_DEVID;
  40611. ret = wc_AesEcbDecrypt(
  40612. info->cipher.aesecb.aes,
  40613. info->cipher.aesecb.out,
  40614. info->cipher.aesecb.in,
  40615. info->cipher.aesecb.sz);
  40616. /* reset devId */
  40617. info->cipher.aesecb.aes->devId = devIdArg;
  40618. }
  40619. }
  40620. #endif /* HAVE_AES_ECB */
  40621. #if defined(WOLFSSL_AES_COUNTER) && !defined(HAVE_FIPS) && \
  40622. !defined(HAVE_SELFTEST)
  40623. if (info->cipher.type == WC_CIPHER_AES_CTR) {
  40624. /* set devId to invalid, so software is used */
  40625. info->cipher.aesctr.aes->devId = INVALID_DEVID;
  40626. ret = wc_AesCtrEncrypt(
  40627. info->cipher.aesctr.aes,
  40628. info->cipher.aesctr.out,
  40629. info->cipher.aesctr.in,
  40630. info->cipher.aesctr.sz);
  40631. /* reset devId */
  40632. info->cipher.aesctr.aes->devId = devIdArg;
  40633. }
  40634. #endif /* WOLFSSL_AES_COUNTER */
  40635. #if defined(HAVE_AESCCM) && defined(WOLFSSL_AES_128)
  40636. if (info->cipher.type == WC_CIPHER_AES_CCM) {
  40637. if (info->cipher.enc) {
  40638. /* set devId to invalid, so software is used */
  40639. info->cipher.aesccm_enc.aes->devId = INVALID_DEVID;
  40640. ret = wc_AesCcmEncrypt(
  40641. info->cipher.aesccm_enc.aes,
  40642. info->cipher.aesccm_enc.out,
  40643. info->cipher.aesccm_enc.in,
  40644. info->cipher.aesccm_enc.sz,
  40645. info->cipher.aesccm_enc.nonce,
  40646. info->cipher.aesccm_enc.nonceSz,
  40647. info->cipher.aesccm_enc.authTag,
  40648. info->cipher.aesccm_enc.authTagSz,
  40649. info->cipher.aesccm_enc.authIn,
  40650. info->cipher.aesccm_enc.authInSz);
  40651. /* reset devId */
  40652. info->cipher.aesccm_enc.aes->devId = devIdArg;
  40653. }
  40654. else {
  40655. /* set devId to invalid, so software is used */
  40656. info->cipher.aesccm_dec.aes->devId = INVALID_DEVID;
  40657. ret = wc_AesCcmDecrypt(
  40658. info->cipher.aesccm_dec.aes,
  40659. info->cipher.aesccm_dec.out,
  40660. info->cipher.aesccm_dec.in,
  40661. info->cipher.aesccm_dec.sz,
  40662. info->cipher.aesccm_dec.nonce,
  40663. info->cipher.aesccm_dec.nonceSz,
  40664. info->cipher.aesccm_dec.authTag,
  40665. info->cipher.aesccm_dec.authTagSz,
  40666. info->cipher.aesccm_dec.authIn,
  40667. info->cipher.aesccm_dec.authInSz);
  40668. /* reset devId */
  40669. info->cipher.aesccm_dec.aes->devId = devIdArg;
  40670. }
  40671. }
  40672. #endif
  40673. #ifndef NO_DES3
  40674. if (info->cipher.type == WC_CIPHER_DES3) {
  40675. if (info->cipher.enc) {
  40676. /* set devId to invalid, so software is used */
  40677. info->cipher.des3.des->devId = INVALID_DEVID;
  40678. ret = wc_Des3_CbcEncrypt(
  40679. info->cipher.des3.des,
  40680. info->cipher.des3.out,
  40681. info->cipher.des3.in,
  40682. info->cipher.des3.sz);
  40683. /* reset devId */
  40684. info->cipher.des3.des->devId = devIdArg;
  40685. }
  40686. else {
  40687. /* set devId to invalid, so software is used */
  40688. info->cipher.des3.des->devId = INVALID_DEVID;
  40689. ret = wc_Des3_CbcDecrypt(
  40690. info->cipher.des3.des,
  40691. info->cipher.des3.out,
  40692. info->cipher.des3.in,
  40693. info->cipher.des3.sz);
  40694. /* reset devId */
  40695. info->cipher.des3.des->devId = devIdArg;
  40696. }
  40697. }
  40698. #endif /* !NO_DES3 */
  40699. #endif /* !NO_AES || !NO_DES3 */
  40700. }
  40701. #if !defined(NO_SHA) || !defined(NO_SHA256) || \
  40702. defined(WOLFSSL_SHA384) || defined(WOLFSSL_SHA512)
  40703. else if (info->algo_type == WC_ALGO_TYPE_HASH) {
  40704. #if !defined(NO_SHA)
  40705. if (info->hash.type == WC_HASH_TYPE_SHA) {
  40706. if (info->hash.sha1 == NULL)
  40707. return NOT_COMPILED_IN;
  40708. /* set devId to invalid, so software is used */
  40709. info->hash.sha1->devId = INVALID_DEVID;
  40710. if (info->hash.in != NULL) {
  40711. ret = wc_ShaUpdate(
  40712. info->hash.sha1,
  40713. info->hash.in,
  40714. info->hash.inSz);
  40715. }
  40716. if (info->hash.digest != NULL) {
  40717. ret = wc_ShaFinal(
  40718. info->hash.sha1,
  40719. info->hash.digest);
  40720. }
  40721. /* reset devId */
  40722. info->hash.sha1->devId = devIdArg;
  40723. }
  40724. else
  40725. #endif
  40726. #if !defined(NO_SHA256)
  40727. if (info->hash.type == WC_HASH_TYPE_SHA256) {
  40728. if (info->hash.sha256 == NULL)
  40729. return NOT_COMPILED_IN;
  40730. /* set devId to invalid, so software is used */
  40731. info->hash.sha256->devId = INVALID_DEVID;
  40732. if (info->hash.in != NULL) {
  40733. ret = wc_Sha256Update(
  40734. info->hash.sha256,
  40735. info->hash.in,
  40736. info->hash.inSz);
  40737. }
  40738. if (info->hash.digest != NULL) {
  40739. ret = wc_Sha256Final(
  40740. info->hash.sha256,
  40741. info->hash.digest);
  40742. }
  40743. /* reset devId */
  40744. info->hash.sha256->devId = devIdArg;
  40745. }
  40746. else
  40747. #endif
  40748. #ifdef WOLFSSL_SHA384
  40749. if (info->hash.type == WC_HASH_TYPE_SHA384) {
  40750. if (info->hash.sha384 == NULL)
  40751. return NOT_COMPILED_IN;
  40752. #ifndef NO_SHA2_CRYPTO_CB
  40753. /* set devId to invalid, so software is used */
  40754. info->hash.sha384->devId = INVALID_DEVID;
  40755. #endif
  40756. if (info->hash.in != NULL) {
  40757. ret = wc_Sha384Update(
  40758. info->hash.sha384,
  40759. info->hash.in,
  40760. info->hash.inSz);
  40761. }
  40762. if (info->hash.digest != NULL) {
  40763. ret = wc_Sha384Final(
  40764. info->hash.sha384,
  40765. info->hash.digest);
  40766. }
  40767. #ifndef NO_SHA2_CRYPTO_CB
  40768. /* reset devId */
  40769. info->hash.sha384->devId = devIdArg;
  40770. #endif
  40771. }
  40772. else
  40773. #endif
  40774. #ifdef WOLFSSL_SHA512
  40775. if (info->hash.type == WC_HASH_TYPE_SHA512) {
  40776. if (info->hash.sha512 == NULL)
  40777. return NOT_COMPILED_IN;
  40778. #ifndef NO_SHA2_CRYPTO_CB
  40779. /* set devId to invalid, so software is used */
  40780. info->hash.sha512->devId = INVALID_DEVID;
  40781. #endif
  40782. if (info->hash.in != NULL) {
  40783. ret = wc_Sha512Update(
  40784. info->hash.sha512,
  40785. info->hash.in,
  40786. info->hash.inSz);
  40787. }
  40788. if (info->hash.digest != NULL) {
  40789. ret = wc_Sha512Final(
  40790. info->hash.sha512,
  40791. info->hash.digest);
  40792. }
  40793. #ifndef NO_SHA2_CRYPTO_CB
  40794. /* reset devId */
  40795. info->hash.sha512->devId = devIdArg;
  40796. #endif
  40797. }
  40798. else
  40799. #endif
  40800. {
  40801. }
  40802. }
  40803. #endif /* !NO_SHA || !NO_SHA256 */
  40804. #ifndef NO_HMAC
  40805. else if (info->algo_type == WC_ALGO_TYPE_HMAC) {
  40806. if (info->hmac.hmac == NULL)
  40807. return NOT_COMPILED_IN;
  40808. /* set devId to invalid, so software is used */
  40809. info->hmac.hmac->devId = INVALID_DEVID;
  40810. if (info->hash.in != NULL) {
  40811. ret = wc_HmacUpdate(
  40812. info->hmac.hmac,
  40813. info->hmac.in,
  40814. info->hmac.inSz);
  40815. }
  40816. else if (info->hash.digest != NULL) {
  40817. ret = wc_HmacFinal(
  40818. info->hmac.hmac,
  40819. info->hmac.digest);
  40820. }
  40821. /* reset devId */
  40822. info->hmac.hmac->devId = devIdArg;
  40823. }
  40824. #endif
  40825. (void)devIdArg;
  40826. (void)myCtx;
  40827. return ret;
  40828. }
  40829. WOLFSSL_TEST_SUBROUTINE int cryptocb_test(void)
  40830. {
  40831. int ret = 0;
  40832. myCryptoDevCtx myCtx;
  40833. /* example data for callback */
  40834. myCtx.exampleVar = 1;
  40835. /* set devId to something other than INVALID_DEVID */
  40836. devId = 1;
  40837. ret = wc_CryptoCb_RegisterDevice(devId, myCryptoDevCb, &myCtx);
  40838. #ifndef WC_NO_RNG
  40839. if (ret == 0)
  40840. ret = random_test();
  40841. #endif /* WC_NO_RNG */
  40842. #if !defined(NO_RSA)
  40843. PRIVATE_KEY_UNLOCK();
  40844. if (ret == 0)
  40845. ret = rsa_test();
  40846. PRIVATE_KEY_LOCK();
  40847. #endif
  40848. #if defined(WOLF_CRYPTO_CB_ONLY_RSA)
  40849. PRIVATE_KEY_UNLOCK();
  40850. if (ret == 0)
  40851. ret = rsa_onlycb_test(&myCtx);
  40852. PRIVATE_KEY_LOCK();
  40853. #endif
  40854. #if defined(HAVE_ECC)
  40855. PRIVATE_KEY_UNLOCK();
  40856. if (ret == 0)
  40857. ret = ecc_test();
  40858. PRIVATE_KEY_LOCK();
  40859. #endif
  40860. #if defined(WOLF_CRYPTO_CB_ONLY_ECC)
  40861. PRIVATE_KEY_UNLOCK();
  40862. if (ret == 0)
  40863. ret = ecc_onlycb_test(&myCtx);
  40864. PRIVATE_KEY_LOCK();
  40865. #endif
  40866. #ifdef HAVE_ED25519
  40867. if (ret == 0)
  40868. ret = ed25519_test();
  40869. #endif
  40870. #ifdef HAVE_CURVE25519
  40871. if (ret == 0)
  40872. ret = curve25519_test();
  40873. #endif
  40874. #ifndef NO_AES
  40875. #ifdef HAVE_AESGCM
  40876. if (ret == 0)
  40877. ret = aesgcm_test();
  40878. #endif
  40879. #ifdef HAVE_AES_CBC
  40880. if (ret == 0)
  40881. ret = aes_test();
  40882. #endif
  40883. #if defined(HAVE_AESCCM) && defined(WOLFSSL_AES_128)
  40884. if (ret == 0)
  40885. ret = aesccm_test();
  40886. #endif
  40887. #endif /* !NO_AES */
  40888. #ifndef NO_DES3
  40889. if (ret == 0)
  40890. ret = des3_test();
  40891. #endif /* !NO_DES3 */
  40892. #ifndef NO_SHA
  40893. if (ret == 0)
  40894. ret = sha_test();
  40895. #endif
  40896. #ifndef NO_SHA256
  40897. if (ret == 0)
  40898. ret = sha256_test();
  40899. #endif
  40900. #ifdef WOLFSSL_SHA384
  40901. if (ret == 0)
  40902. ret = sha384_test();
  40903. #endif
  40904. #ifdef WOLFSSL_SHA512
  40905. if (ret == 0)
  40906. ret = sha512_test();
  40907. #endif
  40908. #ifndef NO_HMAC
  40909. #ifndef NO_SHA
  40910. if (ret == 0)
  40911. ret = hmac_sha_test();
  40912. #endif
  40913. #ifndef NO_SHA256
  40914. if (ret == 0)
  40915. ret = hmac_sha256_test();
  40916. #endif
  40917. #endif
  40918. #ifndef NO_PWDBASED
  40919. #if defined(HAVE_PBKDF2) && !defined(NO_SHA256)
  40920. if (ret == 0)
  40921. ret = pbkdf2_test();
  40922. #endif
  40923. #endif
  40924. #if defined(WOLFSSL_CMAC) && !defined(NO_AES)
  40925. if (ret == 0)
  40926. ret = cmac_test();
  40927. #endif
  40928. /* reset devId */
  40929. devId = INVALID_DEVID;
  40930. return ret;
  40931. }
  40932. #endif /* WOLF_CRYPTO_CB */
  40933. #ifdef WOLFSSL_CERT_PIV
  40934. WOLFSSL_TEST_SUBROUTINE int certpiv_test(void)
  40935. {
  40936. int ret;
  40937. wc_CertPIV piv;
  40938. /* Template for Identiv PIV cert, nonce and signature */
  40939. WOLFSSL_SMALL_STACK_STATIC const byte pivCertIdentiv[] = {
  40940. 0x0A, 0x0B,
  40941. 0x53, 0x09, /* NIST PIV Cert */
  40942. 0x70, 0x02, /* Certificate */
  40943. 0x30, 0x00,
  40944. 0x71, 0x01, 0x05, /* Cert Info */
  40945. 0xFE, 0x00, /* Error Detection */
  40946. 0x0B, 0x01, 0x00, /* Nonce */
  40947. 0x0C, 0x01, 0x00, /* Signed Nonce */
  40948. };
  40949. /* PIV certificate data including certificate, info and error dectection. */
  40950. WOLFSSL_SMALL_STACK_STATIC const byte pivCert[] = {
  40951. 0x53, 0x09, /* NIST PIV Cert */
  40952. 0x70, 0x02, /* Certificate */
  40953. 0x30, 0x00,
  40954. 0x71, 0x01, 0x04, /* Cert Info */
  40955. 0xFE, 0x00, /* Error Detection */
  40956. };
  40957. XMEMSET(&piv, 0, sizeof(piv));
  40958. /* Test with Identiv 0x0A, 0x0B and 0x0C markers */
  40959. ret = wc_ParseCertPIV(&piv, pivCertIdentiv, sizeof(pivCertIdentiv));
  40960. if (ret != 0) {
  40961. return -14000;
  40962. }
  40963. if (!piv.isIdentiv) {
  40964. return -14001;
  40965. }
  40966. if ((piv.cert == NULL) || (piv.certSz != 2)) {
  40967. return -14002;
  40968. }
  40969. if ((piv.certErrDet == NULL) || (piv.certErrDetSz != 0)) {
  40970. return -14003;
  40971. }
  40972. if ((piv.compression != ASN_PIV_CERT_INFO_GZIP)) {
  40973. return -14004;
  40974. }
  40975. if (!piv.isX509) {
  40976. return -14005;
  40977. }
  40978. if ((piv.nonce == NULL) || (piv.nonceSz != 1)) {
  40979. return -14006;
  40980. }
  40981. if ((piv.signedNonce == NULL) || (piv.signedNonceSz != 1)) {
  40982. return -14007;
  40983. }
  40984. XMEMSET(&piv, 0, sizeof(piv));
  40985. /* Test with NIST PIV format */
  40986. ret = wc_ParseCertPIV(&piv, pivCert, sizeof(pivCert));
  40987. if (ret != 0) {
  40988. return -14010;
  40989. }
  40990. if (piv.isIdentiv) {
  40991. return -14011;
  40992. }
  40993. if ((piv.cert == NULL) || (piv.certSz != 2)) {
  40994. return -14012;
  40995. }
  40996. if ((piv.certErrDet == NULL) || (piv.certErrDetSz != 0)) {
  40997. return -14013;
  40998. }
  40999. if ((piv.compression != 0)) {
  41000. return -14014;
  41001. }
  41002. if (!piv.isX509) {
  41003. return -14015;
  41004. }
  41005. return ret;
  41006. }
  41007. #endif /* WOLFSSL_CERT_PIV */
  41008. #if !defined(NO_ASN) && !defined(NO_ASN_TIME)
  41009. static time_t time_cb(time_t* t)
  41010. {
  41011. if (t != NULL) {
  41012. *t = 99;
  41013. }
  41014. return 99;
  41015. }
  41016. WOLFSSL_TEST_SUBROUTINE int time_test(void)
  41017. {
  41018. time_t t;
  41019. if (wc_SetTimeCb(time_cb) != 0)
  41020. return -15000;
  41021. t = wc_Time(NULL);
  41022. if (t != 99)
  41023. return -15001;
  41024. if (wc_GetTime(&t, sizeof(time_t)) != 0)
  41025. return -15002;
  41026. if (t != 99)
  41027. return -15003;
  41028. if (wc_SetTimeCb(NULL) != 0)
  41029. return -15004;
  41030. return 0;
  41031. }
  41032. #endif
  41033. #ifdef WOLFSSL_AES_SIV
  41034. typedef struct {
  41035. const byte key[33];
  41036. word32 keySz;
  41037. const byte nonce[49];
  41038. word32 nonceSz;
  41039. const byte assoc[81];
  41040. word32 assocSz;
  41041. const byte plaintext[83];
  41042. word32 plaintextSz;
  41043. const byte siv[AES_BLOCK_SIZE+1];
  41044. const byte ciphertext[82];
  41045. word32 ciphertextSz;
  41046. } AesSivTestVector;
  41047. #define AES_SIV_TEST_VECTORS 7
  41048. WOLFSSL_TEST_SUBROUTINE int aes_siv_test(void)
  41049. {
  41050. /* These test vectors come from chrony 4.1's SIV unit tests. */
  41051. WOLFSSL_SMALL_STACK_STATIC const AesSivTestVector testVectors[AES_SIV_TEST_VECTORS] = {
  41052. { "\x01\x23\x45\x67\x89\xab\xcd\xef\xf0\x12\x34\x56\x78\x9a\xbc\xde"
  41053. "\xef\x01\x23\x45\x67\x89\xab\xcd\xde\xf0\x12\x34\x56\x78\x9a\xbc", 32,
  41054. "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f", 16,
  41055. "", 0,
  41056. "", 0,
  41057. "\x22\x3e\xb5\x94\xe0\xe0\x25\x4b\x00\x25\x8e\x21\x9a\x1c\xa4\x21",
  41058. "", 0
  41059. },
  41060. { "\x01\x23\x45\x67\x89\xab\xcd\xef\xf0\x12\x34\x56\x78\x9a\xbc\xde"
  41061. "\xef\x01\x23\x45\x67\x89\xab\xcd\xde\xf0\x12\x34\x56\x78\x9a\xbc", 32,
  41062. "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f", 16,
  41063. "\x4c\x9d\x4f\xca\xed\x8a\xe2\xba\xad\x3f\x3e\xa6\xe9\x3c\x8c\x8b", 16,
  41064. "", 0,
  41065. "\xd7\x20\x19\x89\xc6\xdb\xc6\xd6\x61\xfc\x62\xbc\x86\x5e\xee\xef",
  41066. "", 0
  41067. },
  41068. { "\x01\x23\x45\x67\x89\xab\xcd\xef\xf0\x12\x34\x56\x78\x9a\xbc\xde"
  41069. "\xef\x01\x23\x45\x67\x89\xab\xcd\xde\xf0\x12\x34\x56\x78\x9a\xbc", 32,
  41070. "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f", 16,
  41071. "", 0,
  41072. "\x4c\x9d\x4f\xca\xed\x8a\xe2\xba\xad\x3f\x3e\xa6\xe9\x3c\x8c\x8b", 16,
  41073. "\xb6\xc1\x60\xe9\xc2\xfd\x2a\xe8\xde\xc5\x36\x8b\x2a\x33\xed\xe1",
  41074. "\x14\xff\xb3\x97\x34\x5c\xcb\xe4\x4a\xa4\xde\xac\xd9\x36\x90\x46", 16
  41075. },
  41076. { "\x01\x23\x45\x67\x89\xab\xcd\xef\xf0\x12\x34\x56\x78\x9a\xbc\xde"
  41077. "\xef\x01\x23\x45\x67\x89\xab\xcd\xde\xf0\x12\x34\x56\x78\x9a\xbc", 32,
  41078. "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e", 15,
  41079. "\x4c\x9d\x4f\xca\xed\x8a\xe2\xba\xad\x3f\x3e\xa6\xe9\x3c\x8c", 15,
  41080. "\xba\x99\x79\x31\x23\x7e\x3c\x53\x58\x7e\xd4\x93\x02\xab\xe4", 15,
  41081. "\x03\x8c\x41\x51\xba\x7a\x8f\x77\x6e\x56\x31\x99\x42\x0b\xc7\x03",
  41082. "\xe7\x6c\x67\xc9\xda\xb7\x0d\x5b\x44\x06\x26\x5a\xd0\xd2\x3b", 15
  41083. },
  41084. { "\x01\x23\x45\x67\x89\xab\xcd\xef\xf0\x12\x34\x56\x78\x9a\xbc\xde"
  41085. "\xef\x01\x23\x45\x67\x89\xab\xcd\xde\xf0\x12\x34\x56\x78\x9a\xbc", 32,
  41086. "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f", 16,
  41087. "\x4c\x9d\x4f\xca\xed\x8a\xe2\xba\xad\x3f\x3e\xa6\xe9\x3c\x8c\x8b", 16,
  41088. "\xba\x99\x79\x31\x23\x7e\x3c\x53\x58\x7e\xd4\x93\x02\xab\xe4\xa7", 16,
  41089. "\x5c\x05\x23\x65\xf4\x57\x0a\xa0\xfb\x38\x3e\xce\x9b\x75\x85\xeb",
  41090. "\x68\x85\x19\x36\x0c\x7c\x48\x11\x40\xcb\x9b\x57\x9a\x0e\x65\x32", 16
  41091. },
  41092. { "\x01\x23\x45\x67\x89\xab\xcd\xef\xf0\x12\x34\x56\x78\x9a\xbc\xde"
  41093. "\xef\x01\x23\x45\x67\x89\xab\xcd\xde\xf0\x12\x34\x56\x78\x9a\xbc", 32,
  41094. "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f"
  41095. "\xd5", 17,
  41096. "\x4c\x9d\x4f\xca\xed\x8a\xe2\xba\xad\x3f\x3e\xa6\xe9\x3c\x8c\x8b"
  41097. "\xa0", 17,
  41098. "\xba\x99\x79\x31\x23\x7e\x3c\x53\x58\x7e\xd4\x93\x02\xab\xe4\xa7"
  41099. "\x08", 17,
  41100. "\xaf\x58\x4b\xe7\x82\x1e\x96\x19\x29\x91\x25\xe0\xdd\x80\x3b\x49",
  41101. "\xa5\x11\xcd\xb6\x08\xf3\x76\xa0\xb6\xfa\x15\x82\xf3\x95\xe1\xeb"
  41102. "\xbd", 17
  41103. },
  41104. { "\x01\x23\x45\x67\x89\xab\xcd\xef\xf0\x12\x34\x56\x78\x9a\xbc\xde"
  41105. "\xef\x01\x23\x45\x67\x89\xab\xcd\xde\xf0\x12\x34\x56\x78\x9a\xbc", 32,
  41106. "\xb0\x5a\x1b\xc7\x56\xe7\xb6\x2c\xb4\x85\xe5\x56\xa5\x28\xc0\x6c"
  41107. "\x2f\x3b\x0b\x9d\x1a\x0c\xdf\x69\x47\xe0\xcc\xc0\x87\xaa\x5c\x09"
  41108. "\x98\x48\x8d\x6a\x8e\x1e\x05\xd7\x8b\x68\x74\x83\xb5\x1d\xf1\x2c", 48,
  41109. "\xe5\x8b\xd2\x6a\x30\xc5\xc5\x61\xcc\xbd\x7c\x27\xbf\xfe\xf9\x06"
  41110. "\x00\x5b\xd7\xfc\x11\x0b\xcf\x16\x61\xef\xac\x05\xa7\xaf\xec\x27"
  41111. "\x41\xc8\x5e\x9e\x0d\xf9\x2f\xaf\x20\x79\x17\xe5\x17\x91\x2a\x27"
  41112. "\x34\x1c\xbc\xaf\xeb\xef\x7f\x52\xe7\x1e\x4c\x2a\xca\xbd\x2b\xbe"
  41113. "\x34\xd6\xfb\x69\xd3\x3e\x49\x59\x60\xb4\x26\xc9\xb8\xce\xba", 79,
  41114. "\x6c\xe7\xcf\x7e\xab\x7b\xa0\xe1\xa7\x22\xcb\x88\xde\x5e\x42\xd2"
  41115. "\xec\x79\xe0\xa2\xcf\x5f\x0f\x6f\x6b\x89\x57\xcd\xae\x17\xd4\xc2"
  41116. "\xf3\x1b\xa2\xa8\x13\x78\x23\x2f\x83\xa8\xd4\x0c\xc0\xd2\xf3\x99"
  41117. "\xae\x81\xa1\xca\x5b\x5f\x45\xa6\x6f\x0c\x8a\xf3\xd4\x67\x40\x81"
  41118. "\x26\xe2\x01\x86\xe8\x5a\xd5\xf8\x58\x80\x9f\x56\xaa\x76\x96\xbf"
  41119. "\x31", 81,
  41120. "\x9a\x06\x33\xe0\xee\x00\x6a\x9b\xc8\x20\xd5\xe2\xc2\xed\xb5\x75",
  41121. "\xfa\x9e\x42\x2a\x31\x6b\xda\xca\xaa\x7d\x31\x8b\x84\x7a\xb8\xd7"
  41122. "\x8a\x81\x25\x64\xed\x41\x9b\xa9\x77\x10\xbd\x05\x0c\x4e\xc5\x31"
  41123. "\x0c\xa2\x86\xec\x8a\x94\xc8\x24\x23\x3c\x13\xee\xa5\x51\xc9\xdf"
  41124. "\x48\xc9\x55\xc5\x2f\x40\x73\x3f\x98\xbb\x8d\x69\x78\x46\x64\x17"
  41125. "\x8d\x49\x2f\x14\x62\xa4\x7c\x2a\x57\x38\x87\xce\xc6\x72\xd3\x5c"
  41126. "\xa1", 81
  41127. }};
  41128. int i;
  41129. byte computedCiphertext[82];
  41130. byte computedPlaintext[82];
  41131. byte siv[AES_BLOCK_SIZE];
  41132. int rc = 0;
  41133. for (i = 0; i < AES_SIV_TEST_VECTORS; ++i) {
  41134. rc = wc_AesSivEncrypt(testVectors[i].key, testVectors[i].keySz,
  41135. testVectors[i].assoc, testVectors[i].assocSz,
  41136. testVectors[i].nonce, testVectors[i].nonceSz,
  41137. testVectors[i].plaintext,
  41138. testVectors[i].plaintextSz, siv,
  41139. computedCiphertext);
  41140. if (rc != 0) {
  41141. return -16000;
  41142. }
  41143. rc = XMEMCMP(siv, testVectors[i].siv, AES_BLOCK_SIZE);
  41144. if (rc != 0) {
  41145. return -16001;
  41146. }
  41147. rc = XMEMCMP(computedCiphertext, testVectors[i].ciphertext,
  41148. testVectors[i].ciphertextSz);
  41149. if (rc != 0) {
  41150. return -16002;
  41151. }
  41152. rc = wc_AesSivDecrypt(testVectors[i].key, testVectors[i].keySz,
  41153. testVectors[i].assoc, testVectors[i].assocSz,
  41154. testVectors[i].nonce, testVectors[i].nonceSz,
  41155. computedCiphertext, testVectors[i].plaintextSz,
  41156. siv, computedPlaintext);
  41157. if (rc != 0) {
  41158. return -16003;
  41159. }
  41160. rc = XMEMCMP(computedPlaintext, testVectors[i].plaintext,
  41161. testVectors[i].plaintextSz);
  41162. if (rc != 0) {
  41163. return -16004;
  41164. }
  41165. }
  41166. return 0;
  41167. }
  41168. #endif
  41169. #undef ERROR_OUT
  41170. #else
  41171. #ifndef NO_MAIN_DRIVER
  41172. int main() { return 0; }
  41173. #endif
  41174. #endif /* NO_CRYPT_TEST */