test.c 1.5 MB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213202142021520216202172021820219202202022120222202232022420225202262022720228202292023020231202322023320234202352023620237202382023920240202412024220243202442024520246202472024820249202502025120252202532025420255202562025720258202592026020261202622026320264202652026620267202682026920270202712027220273202742027520276202772027820279202802028120282202832028420285202862028720288202892029020291202922029320294202952029620297202982029920300203012030220303203042030520306203072030820309203102031120312203132031420315203162031720318203192032020321203222032320324203252032620327203282032920330203312033220333203342033520336203372033820339203402034120342203432034420345203462034720348203492035020351203522035320354203552035620357203582035920360203612036220363203642036520366203672036820369203702037120372203732037420375203762037720378203792038020381203822038320384203852038620387203882038920390203912039220393203942039520396203972039820399204002040120402204032040420405204062040720408204092041020411204122041320414204152041620417204182041920420204212042220423204242042520426204272042820429204302043120432204332043420435204362043720438204392044020441204422044320444204452044620447204482044920450204512045220453204542045520456204572045820459204602046120462204632046420465204662046720468204692047020471204722047320474204752047620477204782047920480204812048220483204842048520486204872048820489204902049120492204932049420495204962049720498204992050020501205022050320504205052050620507205082050920510205112051220513205142051520516205172051820519205202052120522205232052420525205262052720528205292053020531205322053320534205352053620537205382053920540205412054220543205442054520546205472054820549205502055120552205532055420555205562055720558205592056020561205622056320564205652056620567205682056920570205712057220573205742057520576205772057820579205802058120582205832058420585205862058720588205892059020591205922059320594205952059620597205982059920600206012060220603206042060520606206072060820609206102061120612206132061420615206162061720618206192062020621206222062320624206252062620627206282062920630206312063220633206342063520636206372063820639206402064120642206432064420645206462064720648206492065020651206522065320654206552065620657206582065920660206612066220663206642066520666206672066820669206702067120672206732067420675206762067720678206792068020681206822068320684206852068620687206882068920690206912069220693206942069520696206972069820699207002070120702207032070420705207062070720708207092071020711207122071320714207152071620717207182071920720207212072220723207242072520726207272072820729207302073120732207332073420735207362073720738207392074020741207422074320744207452074620747207482074920750207512075220753207542075520756207572075820759207602076120762207632076420765207662076720768207692077020771207722077320774207752077620777207782077920780207812078220783207842078520786207872078820789207902079120792207932079420795207962079720798207992080020801208022080320804208052080620807208082080920810208112081220813208142081520816208172081820819208202082120822208232082420825208262082720828208292083020831208322083320834208352083620837208382083920840208412084220843208442084520846208472084820849208502085120852208532085420855208562085720858208592086020861208622086320864208652086620867208682086920870208712087220873208742087520876208772087820879208802088120882208832088420885208862088720888208892089020891208922089320894208952089620897208982089920900209012090220903209042090520906209072090820909209102091120912209132091420915209162091720918209192092020921209222092320924209252092620927209282092920930209312093220933209342093520936209372093820939209402094120942209432094420945209462094720948209492095020951209522095320954209552095620957209582095920960209612096220963209642096520966209672096820969209702097120972209732097420975209762097720978209792098020981209822098320984209852098620987209882098920990209912099220993209942099520996209972099820999210002100121002210032100421005210062100721008210092101021011210122101321014210152101621017210182101921020210212102221023210242102521026210272102821029210302103121032210332103421035210362103721038210392104021041210422104321044210452104621047210482104921050210512105221053210542105521056210572105821059210602106121062210632106421065210662106721068210692107021071210722107321074210752107621077210782107921080210812108221083210842108521086210872108821089210902109121092210932109421095210962109721098210992110021101211022110321104211052110621107211082110921110211112111221113211142111521116211172111821119211202112121122211232112421125211262112721128211292113021131211322113321134211352113621137211382113921140211412114221143211442114521146211472114821149211502115121152211532115421155211562115721158211592116021161211622116321164211652116621167211682116921170211712117221173211742117521176211772117821179211802118121182211832118421185211862118721188211892119021191211922119321194211952119621197211982119921200212012120221203212042120521206212072120821209212102121121212212132121421215212162121721218212192122021221212222122321224212252122621227212282122921230212312123221233212342123521236212372123821239212402124121242212432124421245212462124721248212492125021251212522125321254212552125621257212582125921260212612126221263212642126521266212672126821269212702127121272212732127421275212762127721278212792128021281212822128321284212852128621287212882128921290212912129221293212942129521296212972129821299213002130121302213032130421305213062130721308213092131021311213122131321314213152131621317213182131921320213212132221323213242132521326213272132821329213302133121332213332133421335213362133721338213392134021341213422134321344213452134621347213482134921350213512135221353213542135521356213572135821359213602136121362213632136421365213662136721368213692137021371213722137321374213752137621377213782137921380213812138221383213842138521386213872138821389213902139121392213932139421395213962139721398213992140021401214022140321404214052140621407214082140921410214112141221413214142141521416214172141821419214202142121422214232142421425214262142721428214292143021431214322143321434214352143621437214382143921440214412144221443214442144521446214472144821449214502145121452214532145421455214562145721458214592146021461214622146321464214652146621467214682146921470214712147221473214742147521476214772147821479214802148121482214832148421485214862148721488214892149021491214922149321494214952149621497214982149921500215012150221503215042150521506215072150821509215102151121512215132151421515215162151721518215192152021521215222152321524215252152621527215282152921530215312153221533215342153521536215372153821539215402154121542215432154421545215462154721548215492155021551215522155321554215552155621557215582155921560215612156221563215642156521566215672156821569215702157121572215732157421575215762157721578215792158021581215822158321584215852158621587215882158921590215912159221593215942159521596215972159821599216002160121602216032160421605216062160721608216092161021611216122161321614216152161621617216182161921620216212162221623216242162521626216272162821629216302163121632216332163421635216362163721638216392164021641216422164321644216452164621647216482164921650216512165221653216542165521656216572165821659216602166121662216632166421665216662166721668216692167021671216722167321674216752167621677216782167921680216812168221683216842168521686216872168821689216902169121692216932169421695216962169721698216992170021701217022170321704217052170621707217082170921710217112171221713217142171521716217172171821719217202172121722217232172421725217262172721728217292173021731217322173321734217352173621737217382173921740217412174221743217442174521746217472174821749217502175121752217532175421755217562175721758217592176021761217622176321764217652176621767217682176921770217712177221773217742177521776217772177821779217802178121782217832178421785217862178721788217892179021791217922179321794217952179621797217982179921800218012180221803218042180521806218072180821809218102181121812218132181421815218162181721818218192182021821218222182321824218252182621827218282182921830218312183221833218342183521836218372183821839218402184121842218432184421845218462184721848218492185021851218522185321854218552185621857218582185921860218612186221863218642186521866218672186821869218702187121872218732187421875218762187721878218792188021881218822188321884218852188621887218882188921890218912189221893218942189521896218972189821899219002190121902219032190421905219062190721908219092191021911219122191321914219152191621917219182191921920219212192221923219242192521926219272192821929219302193121932219332193421935219362193721938219392194021941219422194321944219452194621947219482194921950219512195221953219542195521956219572195821959219602196121962219632196421965219662196721968219692197021971219722197321974219752197621977219782197921980219812198221983219842198521986219872198821989219902199121992219932199421995219962199721998219992200022001220022200322004220052200622007220082200922010220112201222013220142201522016220172201822019220202202122022220232202422025220262202722028220292203022031220322203322034220352203622037220382203922040220412204222043220442204522046220472204822049220502205122052220532205422055220562205722058220592206022061220622206322064220652206622067220682206922070220712207222073220742207522076220772207822079220802208122082220832208422085220862208722088220892209022091220922209322094220952209622097220982209922100221012210222103221042210522106221072210822109221102211122112221132211422115221162211722118221192212022121221222212322124221252212622127221282212922130221312213222133221342213522136221372213822139221402214122142221432214422145221462214722148221492215022151221522215322154221552215622157221582215922160221612216222163221642216522166221672216822169221702217122172221732217422175221762217722178221792218022181221822218322184221852218622187221882218922190221912219222193221942219522196221972219822199222002220122202222032220422205222062220722208222092221022211222122221322214222152221622217222182221922220222212222222223222242222522226222272222822229222302223122232222332223422235222362223722238222392224022241222422224322244222452224622247222482224922250222512225222253222542225522256222572225822259222602226122262222632226422265222662226722268222692227022271222722227322274222752227622277222782227922280222812228222283222842228522286222872228822289222902229122292222932229422295222962229722298222992230022301223022230322304223052230622307223082230922310223112231222313223142231522316223172231822319223202232122322223232232422325223262232722328223292233022331223322233322334223352233622337223382233922340223412234222343223442234522346223472234822349223502235122352223532235422355223562235722358223592236022361223622236322364223652236622367223682236922370223712237222373223742237522376223772237822379223802238122382223832238422385223862238722388223892239022391223922239322394223952239622397223982239922400224012240222403224042240522406224072240822409224102241122412224132241422415224162241722418224192242022421224222242322424224252242622427224282242922430224312243222433224342243522436224372243822439224402244122442224432244422445224462244722448224492245022451224522245322454224552245622457224582245922460224612246222463224642246522466224672246822469224702247122472224732247422475224762247722478224792248022481224822248322484224852248622487224882248922490224912249222493224942249522496224972249822499225002250122502225032250422505225062250722508225092251022511225122251322514225152251622517225182251922520225212252222523225242252522526225272252822529225302253122532225332253422535225362253722538225392254022541225422254322544225452254622547225482254922550225512255222553225542255522556225572255822559225602256122562225632256422565225662256722568225692257022571225722257322574225752257622577225782257922580225812258222583225842258522586225872258822589225902259122592225932259422595225962259722598225992260022601226022260322604226052260622607226082260922610226112261222613226142261522616226172261822619226202262122622226232262422625226262262722628226292263022631226322263322634226352263622637226382263922640226412264222643226442264522646226472264822649226502265122652226532265422655226562265722658226592266022661226622266322664226652266622667226682266922670226712267222673226742267522676226772267822679226802268122682226832268422685226862268722688226892269022691226922269322694226952269622697226982269922700227012270222703227042270522706227072270822709227102271122712227132271422715227162271722718227192272022721227222272322724227252272622727227282272922730227312273222733227342273522736227372273822739227402274122742227432274422745227462274722748227492275022751227522275322754227552275622757227582275922760227612276222763227642276522766227672276822769227702277122772227732277422775227762277722778227792278022781227822278322784227852278622787227882278922790227912279222793227942279522796227972279822799228002280122802228032280422805228062280722808228092281022811228122281322814228152281622817228182281922820228212282222823228242282522826228272282822829228302283122832228332283422835228362283722838228392284022841228422284322844228452284622847228482284922850228512285222853228542285522856228572285822859228602286122862228632286422865228662286722868228692287022871228722287322874228752287622877228782287922880228812288222883228842288522886228872288822889228902289122892228932289422895228962289722898228992290022901229022290322904229052290622907229082290922910229112291222913229142291522916229172291822919229202292122922229232292422925229262292722928229292293022931229322293322934229352293622937229382293922940229412294222943229442294522946229472294822949229502295122952229532295422955229562295722958229592296022961229622296322964229652296622967229682296922970229712297222973229742297522976229772297822979229802298122982229832298422985229862298722988229892299022991229922299322994229952299622997229982299923000230012300223003230042300523006230072300823009230102301123012230132301423015230162301723018230192302023021230222302323024230252302623027230282302923030230312303223033230342303523036230372303823039230402304123042230432304423045230462304723048230492305023051230522305323054230552305623057230582305923060230612306223063230642306523066230672306823069230702307123072230732307423075230762307723078230792308023081230822308323084230852308623087230882308923090230912309223093230942309523096230972309823099231002310123102231032310423105231062310723108231092311023111231122311323114231152311623117231182311923120231212312223123231242312523126231272312823129231302313123132231332313423135231362313723138231392314023141231422314323144231452314623147231482314923150231512315223153231542315523156231572315823159231602316123162231632316423165231662316723168231692317023171231722317323174231752317623177231782317923180231812318223183231842318523186231872318823189231902319123192231932319423195231962319723198231992320023201232022320323204232052320623207232082320923210232112321223213232142321523216232172321823219232202322123222232232322423225232262322723228232292323023231232322323323234232352323623237232382323923240232412324223243232442324523246232472324823249232502325123252232532325423255232562325723258232592326023261232622326323264232652326623267232682326923270232712327223273232742327523276232772327823279232802328123282232832328423285232862328723288232892329023291232922329323294232952329623297232982329923300233012330223303233042330523306233072330823309233102331123312233132331423315233162331723318233192332023321233222332323324233252332623327233282332923330233312333223333233342333523336233372333823339233402334123342233432334423345233462334723348233492335023351233522335323354233552335623357233582335923360233612336223363233642336523366233672336823369233702337123372233732337423375233762337723378233792338023381233822338323384233852338623387233882338923390233912339223393233942339523396233972339823399234002340123402234032340423405234062340723408234092341023411234122341323414234152341623417234182341923420234212342223423234242342523426234272342823429234302343123432234332343423435234362343723438234392344023441234422344323444234452344623447234482344923450234512345223453234542345523456234572345823459234602346123462234632346423465234662346723468234692347023471234722347323474234752347623477234782347923480234812348223483234842348523486234872348823489234902349123492234932349423495234962349723498234992350023501235022350323504235052350623507235082350923510235112351223513235142351523516235172351823519235202352123522235232352423525235262352723528235292353023531235322353323534235352353623537235382353923540235412354223543235442354523546235472354823549235502355123552235532355423555235562355723558235592356023561235622356323564235652356623567235682356923570235712357223573235742357523576235772357823579235802358123582235832358423585235862358723588235892359023591235922359323594235952359623597235982359923600236012360223603236042360523606236072360823609236102361123612236132361423615236162361723618236192362023621236222362323624236252362623627236282362923630236312363223633236342363523636236372363823639236402364123642236432364423645236462364723648236492365023651236522365323654236552365623657236582365923660236612366223663236642366523666236672366823669236702367123672236732367423675236762367723678236792368023681236822368323684236852368623687236882368923690236912369223693236942369523696236972369823699237002370123702237032370423705237062370723708237092371023711237122371323714237152371623717237182371923720237212372223723237242372523726237272372823729237302373123732237332373423735237362373723738237392374023741237422374323744237452374623747237482374923750237512375223753237542375523756237572375823759237602376123762237632376423765237662376723768237692377023771237722377323774237752377623777237782377923780237812378223783237842378523786237872378823789237902379123792237932379423795237962379723798237992380023801238022380323804238052380623807238082380923810238112381223813238142381523816238172381823819238202382123822238232382423825238262382723828238292383023831238322383323834238352383623837238382383923840238412384223843238442384523846238472384823849238502385123852238532385423855238562385723858238592386023861238622386323864238652386623867238682386923870238712387223873238742387523876238772387823879238802388123882238832388423885238862388723888238892389023891238922389323894238952389623897238982389923900239012390223903239042390523906239072390823909239102391123912239132391423915239162391723918239192392023921239222392323924239252392623927239282392923930239312393223933239342393523936239372393823939239402394123942239432394423945239462394723948239492395023951239522395323954239552395623957239582395923960239612396223963239642396523966239672396823969239702397123972239732397423975239762397723978239792398023981239822398323984239852398623987239882398923990239912399223993239942399523996239972399823999240002400124002240032400424005240062400724008240092401024011240122401324014240152401624017240182401924020240212402224023240242402524026240272402824029240302403124032240332403424035240362403724038240392404024041240422404324044240452404624047240482404924050240512405224053240542405524056240572405824059240602406124062240632406424065240662406724068240692407024071240722407324074240752407624077240782407924080240812408224083240842408524086240872408824089240902409124092240932409424095240962409724098240992410024101241022410324104241052410624107241082410924110241112411224113241142411524116241172411824119241202412124122241232412424125241262412724128241292413024131241322413324134241352413624137241382413924140241412414224143241442414524146241472414824149241502415124152241532415424155241562415724158241592416024161241622416324164241652416624167241682416924170241712417224173241742417524176241772417824179241802418124182241832418424185241862418724188241892419024191241922419324194241952419624197241982419924200242012420224203242042420524206242072420824209242102421124212242132421424215242162421724218242192422024221242222422324224242252422624227242282422924230242312423224233242342423524236242372423824239242402424124242242432424424245242462424724248242492425024251242522425324254242552425624257242582425924260242612426224263242642426524266242672426824269242702427124272242732427424275242762427724278242792428024281242822428324284242852428624287242882428924290242912429224293242942429524296242972429824299243002430124302243032430424305243062430724308243092431024311243122431324314243152431624317243182431924320243212432224323243242432524326243272432824329243302433124332243332433424335243362433724338243392434024341243422434324344243452434624347243482434924350243512435224353243542435524356243572435824359243602436124362243632436424365243662436724368243692437024371243722437324374243752437624377243782437924380243812438224383243842438524386243872438824389243902439124392243932439424395243962439724398243992440024401244022440324404244052440624407244082440924410244112441224413244142441524416244172441824419244202442124422244232442424425244262442724428244292443024431244322443324434244352443624437244382443924440244412444224443244442444524446244472444824449244502445124452244532445424455244562445724458244592446024461244622446324464244652446624467244682446924470244712447224473244742447524476244772447824479244802448124482244832448424485244862448724488244892449024491244922449324494244952449624497244982449924500245012450224503245042450524506245072450824509245102451124512245132451424515245162451724518245192452024521245222452324524245252452624527245282452924530245312453224533245342453524536245372453824539245402454124542245432454424545245462454724548245492455024551245522455324554245552455624557245582455924560245612456224563245642456524566245672456824569245702457124572245732457424575245762457724578245792458024581245822458324584245852458624587245882458924590245912459224593245942459524596245972459824599246002460124602246032460424605246062460724608246092461024611246122461324614246152461624617246182461924620246212462224623246242462524626246272462824629246302463124632246332463424635246362463724638246392464024641246422464324644246452464624647246482464924650246512465224653246542465524656246572465824659246602466124662246632466424665246662466724668246692467024671246722467324674246752467624677246782467924680246812468224683246842468524686246872468824689246902469124692246932469424695246962469724698246992470024701247022470324704247052470624707247082470924710247112471224713247142471524716247172471824719247202472124722247232472424725247262472724728247292473024731247322473324734247352473624737247382473924740247412474224743247442474524746247472474824749247502475124752247532475424755247562475724758247592476024761247622476324764247652476624767247682476924770247712477224773247742477524776247772477824779247802478124782247832478424785247862478724788247892479024791247922479324794247952479624797247982479924800248012480224803248042480524806248072480824809248102481124812248132481424815248162481724818248192482024821248222482324824248252482624827248282482924830248312483224833248342483524836248372483824839248402484124842248432484424845248462484724848248492485024851248522485324854248552485624857248582485924860248612486224863248642486524866248672486824869248702487124872248732487424875248762487724878248792488024881248822488324884248852488624887248882488924890248912489224893248942489524896248972489824899249002490124902249032490424905249062490724908249092491024911249122491324914249152491624917249182491924920249212492224923249242492524926249272492824929249302493124932249332493424935249362493724938249392494024941249422494324944249452494624947249482494924950249512495224953249542495524956249572495824959249602496124962249632496424965249662496724968249692497024971249722497324974249752497624977249782497924980249812498224983249842498524986249872498824989249902499124992249932499424995249962499724998249992500025001250022500325004250052500625007250082500925010250112501225013250142501525016250172501825019250202502125022250232502425025250262502725028250292503025031250322503325034250352503625037250382503925040250412504225043250442504525046250472504825049250502505125052250532505425055250562505725058250592506025061250622506325064250652506625067250682506925070250712507225073250742507525076250772507825079250802508125082250832508425085250862508725088250892509025091250922509325094250952509625097250982509925100251012510225103251042510525106251072510825109251102511125112251132511425115251162511725118251192512025121251222512325124251252512625127251282512925130251312513225133251342513525136251372513825139251402514125142251432514425145251462514725148251492515025151251522515325154251552515625157251582515925160251612516225163251642516525166251672516825169251702517125172251732517425175251762517725178251792518025181251822518325184251852518625187251882518925190251912519225193251942519525196251972519825199252002520125202252032520425205252062520725208252092521025211252122521325214252152521625217252182521925220252212522225223252242522525226252272522825229252302523125232252332523425235252362523725238252392524025241252422524325244252452524625247252482524925250252512525225253252542525525256252572525825259252602526125262252632526425265252662526725268252692527025271252722527325274252752527625277252782527925280252812528225283252842528525286252872528825289252902529125292252932529425295252962529725298252992530025301253022530325304253052530625307253082530925310253112531225313253142531525316253172531825319253202532125322253232532425325253262532725328253292533025331253322533325334253352533625337253382533925340253412534225343253442534525346253472534825349253502535125352253532535425355253562535725358253592536025361253622536325364253652536625367253682536925370253712537225373253742537525376253772537825379253802538125382253832538425385253862538725388253892539025391253922539325394253952539625397253982539925400254012540225403254042540525406254072540825409254102541125412254132541425415254162541725418254192542025421254222542325424254252542625427254282542925430254312543225433254342543525436254372543825439254402544125442254432544425445254462544725448254492545025451254522545325454254552545625457254582545925460254612546225463254642546525466254672546825469254702547125472254732547425475254762547725478254792548025481254822548325484254852548625487254882548925490254912549225493254942549525496254972549825499255002550125502255032550425505255062550725508255092551025511255122551325514255152551625517255182551925520255212552225523255242552525526255272552825529255302553125532255332553425535255362553725538255392554025541255422554325544255452554625547255482554925550255512555225553255542555525556255572555825559255602556125562255632556425565255662556725568255692557025571255722557325574255752557625577255782557925580255812558225583255842558525586255872558825589255902559125592255932559425595255962559725598255992560025601256022560325604256052560625607256082560925610256112561225613256142561525616256172561825619256202562125622256232562425625256262562725628256292563025631256322563325634256352563625637256382563925640256412564225643256442564525646256472564825649256502565125652256532565425655256562565725658256592566025661256622566325664256652566625667256682566925670256712567225673256742567525676256772567825679256802568125682256832568425685256862568725688256892569025691256922569325694256952569625697256982569925700257012570225703257042570525706257072570825709257102571125712257132571425715257162571725718257192572025721257222572325724257252572625727257282572925730257312573225733257342573525736257372573825739257402574125742257432574425745257462574725748257492575025751257522575325754257552575625757257582575925760257612576225763257642576525766257672576825769257702577125772257732577425775257762577725778257792578025781257822578325784257852578625787257882578925790257912579225793257942579525796257972579825799258002580125802258032580425805258062580725808258092581025811258122581325814258152581625817258182581925820258212582225823258242582525826258272582825829258302583125832258332583425835258362583725838258392584025841258422584325844258452584625847258482584925850258512585225853258542585525856258572585825859258602586125862258632586425865258662586725868258692587025871258722587325874258752587625877258782587925880258812588225883258842588525886258872588825889258902589125892258932589425895258962589725898258992590025901259022590325904259052590625907259082590925910259112591225913259142591525916259172591825919259202592125922259232592425925259262592725928259292593025931259322593325934259352593625937259382593925940259412594225943259442594525946259472594825949259502595125952259532595425955259562595725958259592596025961259622596325964259652596625967259682596925970259712597225973259742597525976259772597825979259802598125982259832598425985259862598725988259892599025991259922599325994259952599625997259982599926000260012600226003260042600526006260072600826009260102601126012260132601426015260162601726018260192602026021260222602326024260252602626027260282602926030260312603226033260342603526036260372603826039260402604126042260432604426045260462604726048260492605026051260522605326054260552605626057260582605926060260612606226063260642606526066260672606826069260702607126072260732607426075260762607726078260792608026081260822608326084260852608626087260882608926090260912609226093260942609526096260972609826099261002610126102261032610426105261062610726108261092611026111261122611326114261152611626117261182611926120261212612226123261242612526126261272612826129261302613126132261332613426135261362613726138261392614026141261422614326144261452614626147261482614926150261512615226153261542615526156261572615826159261602616126162261632616426165261662616726168261692617026171261722617326174261752617626177261782617926180261812618226183261842618526186261872618826189261902619126192261932619426195261962619726198261992620026201262022620326204262052620626207262082620926210262112621226213262142621526216262172621826219262202622126222262232622426225262262622726228262292623026231262322623326234262352623626237262382623926240262412624226243262442624526246262472624826249262502625126252262532625426255262562625726258262592626026261262622626326264262652626626267262682626926270262712627226273262742627526276262772627826279262802628126282262832628426285262862628726288262892629026291262922629326294262952629626297262982629926300263012630226303263042630526306263072630826309263102631126312263132631426315263162631726318263192632026321263222632326324263252632626327263282632926330263312633226333263342633526336263372633826339263402634126342263432634426345263462634726348263492635026351263522635326354263552635626357263582635926360263612636226363263642636526366263672636826369263702637126372263732637426375263762637726378263792638026381263822638326384263852638626387263882638926390263912639226393263942639526396263972639826399264002640126402264032640426405264062640726408264092641026411264122641326414264152641626417264182641926420264212642226423264242642526426264272642826429264302643126432264332643426435264362643726438264392644026441264422644326444264452644626447264482644926450264512645226453264542645526456264572645826459264602646126462264632646426465264662646726468264692647026471264722647326474264752647626477264782647926480264812648226483264842648526486264872648826489264902649126492264932649426495264962649726498264992650026501265022650326504265052650626507265082650926510265112651226513265142651526516265172651826519265202652126522265232652426525265262652726528265292653026531265322653326534265352653626537265382653926540265412654226543265442654526546265472654826549265502655126552265532655426555265562655726558265592656026561265622656326564265652656626567265682656926570265712657226573265742657526576265772657826579265802658126582265832658426585265862658726588265892659026591265922659326594265952659626597265982659926600266012660226603266042660526606266072660826609266102661126612266132661426615266162661726618266192662026621266222662326624266252662626627266282662926630266312663226633266342663526636266372663826639266402664126642266432664426645266462664726648266492665026651266522665326654266552665626657266582665926660266612666226663266642666526666266672666826669266702667126672266732667426675266762667726678266792668026681266822668326684266852668626687266882668926690266912669226693266942669526696266972669826699267002670126702267032670426705267062670726708267092671026711267122671326714267152671626717267182671926720267212672226723267242672526726267272672826729267302673126732267332673426735267362673726738267392674026741267422674326744267452674626747267482674926750267512675226753267542675526756267572675826759267602676126762267632676426765267662676726768267692677026771267722677326774267752677626777267782677926780267812678226783267842678526786267872678826789267902679126792267932679426795267962679726798267992680026801268022680326804268052680626807268082680926810268112681226813268142681526816268172681826819268202682126822268232682426825268262682726828268292683026831268322683326834268352683626837268382683926840268412684226843268442684526846268472684826849268502685126852268532685426855268562685726858268592686026861268622686326864268652686626867268682686926870268712687226873268742687526876268772687826879268802688126882268832688426885268862688726888268892689026891268922689326894268952689626897268982689926900269012690226903269042690526906269072690826909269102691126912269132691426915269162691726918269192692026921269222692326924269252692626927269282692926930269312693226933269342693526936269372693826939269402694126942269432694426945269462694726948269492695026951269522695326954269552695626957269582695926960269612696226963269642696526966269672696826969269702697126972269732697426975269762697726978269792698026981269822698326984269852698626987269882698926990269912699226993269942699526996269972699826999270002700127002270032700427005270062700727008270092701027011270122701327014270152701627017270182701927020270212702227023270242702527026270272702827029270302703127032270332703427035270362703727038270392704027041270422704327044270452704627047270482704927050270512705227053270542705527056270572705827059270602706127062270632706427065270662706727068270692707027071270722707327074270752707627077270782707927080270812708227083270842708527086270872708827089270902709127092270932709427095270962709727098270992710027101271022710327104271052710627107271082710927110271112711227113271142711527116271172711827119271202712127122271232712427125271262712727128271292713027131271322713327134271352713627137271382713927140271412714227143271442714527146271472714827149271502715127152271532715427155271562715727158271592716027161271622716327164271652716627167271682716927170271712717227173271742717527176271772717827179271802718127182271832718427185271862718727188271892719027191271922719327194271952719627197271982719927200272012720227203272042720527206272072720827209272102721127212272132721427215272162721727218272192722027221272222722327224272252722627227272282722927230272312723227233272342723527236272372723827239272402724127242272432724427245272462724727248272492725027251272522725327254272552725627257272582725927260272612726227263272642726527266272672726827269272702727127272272732727427275272762727727278272792728027281272822728327284272852728627287272882728927290272912729227293272942729527296272972729827299273002730127302273032730427305273062730727308273092731027311273122731327314273152731627317273182731927320273212732227323273242732527326273272732827329273302733127332273332733427335273362733727338273392734027341273422734327344273452734627347273482734927350273512735227353273542735527356273572735827359273602736127362273632736427365273662736727368273692737027371273722737327374273752737627377273782737927380273812738227383273842738527386273872738827389273902739127392273932739427395273962739727398273992740027401274022740327404274052740627407274082740927410274112741227413274142741527416274172741827419274202742127422274232742427425274262742727428274292743027431274322743327434274352743627437274382743927440274412744227443274442744527446274472744827449274502745127452274532745427455274562745727458274592746027461274622746327464274652746627467274682746927470274712747227473274742747527476274772747827479274802748127482274832748427485274862748727488274892749027491274922749327494274952749627497274982749927500275012750227503275042750527506275072750827509275102751127512275132751427515275162751727518275192752027521275222752327524275252752627527275282752927530275312753227533275342753527536275372753827539275402754127542275432754427545275462754727548275492755027551275522755327554275552755627557275582755927560275612756227563275642756527566275672756827569275702757127572275732757427575275762757727578275792758027581275822758327584275852758627587275882758927590275912759227593275942759527596275972759827599276002760127602276032760427605276062760727608276092761027611276122761327614276152761627617276182761927620276212762227623276242762527626276272762827629276302763127632276332763427635276362763727638276392764027641276422764327644276452764627647276482764927650276512765227653276542765527656276572765827659276602766127662276632766427665276662766727668276692767027671276722767327674276752767627677276782767927680276812768227683276842768527686276872768827689276902769127692276932769427695276962769727698276992770027701277022770327704277052770627707277082770927710277112771227713277142771527716277172771827719277202772127722277232772427725277262772727728277292773027731277322773327734277352773627737277382773927740277412774227743277442774527746277472774827749277502775127752277532775427755277562775727758277592776027761277622776327764277652776627767277682776927770277712777227773277742777527776277772777827779277802778127782277832778427785277862778727788277892779027791277922779327794277952779627797277982779927800278012780227803278042780527806278072780827809278102781127812278132781427815278162781727818278192782027821278222782327824278252782627827278282782927830278312783227833278342783527836278372783827839278402784127842278432784427845278462784727848278492785027851278522785327854278552785627857278582785927860278612786227863278642786527866278672786827869278702787127872278732787427875278762787727878278792788027881278822788327884278852788627887278882788927890278912789227893278942789527896278972789827899279002790127902279032790427905279062790727908279092791027911279122791327914279152791627917279182791927920279212792227923279242792527926279272792827929279302793127932279332793427935279362793727938279392794027941279422794327944279452794627947279482794927950279512795227953279542795527956279572795827959279602796127962279632796427965279662796727968279692797027971279722797327974279752797627977279782797927980279812798227983279842798527986279872798827989279902799127992279932799427995279962799727998279992800028001280022800328004280052800628007280082800928010280112801228013280142801528016280172801828019280202802128022280232802428025280262802728028280292803028031280322803328034280352803628037280382803928040280412804228043280442804528046280472804828049280502805128052280532805428055280562805728058280592806028061280622806328064280652806628067280682806928070280712807228073280742807528076280772807828079280802808128082280832808428085280862808728088280892809028091280922809328094280952809628097280982809928100281012810228103281042810528106281072810828109281102811128112281132811428115281162811728118281192812028121281222812328124281252812628127281282812928130281312813228133281342813528136281372813828139281402814128142281432814428145281462814728148281492815028151281522815328154281552815628157281582815928160281612816228163281642816528166281672816828169281702817128172281732817428175281762817728178281792818028181281822818328184281852818628187281882818928190281912819228193281942819528196281972819828199282002820128202282032820428205282062820728208282092821028211282122821328214282152821628217282182821928220282212822228223282242822528226282272822828229282302823128232282332823428235282362823728238282392824028241282422824328244282452824628247282482824928250282512825228253282542825528256282572825828259282602826128262282632826428265282662826728268282692827028271282722827328274282752827628277282782827928280282812828228283282842828528286282872828828289282902829128292282932829428295282962829728298282992830028301283022830328304283052830628307283082830928310283112831228313283142831528316283172831828319283202832128322283232832428325283262832728328283292833028331283322833328334283352833628337283382833928340283412834228343283442834528346283472834828349283502835128352283532835428355283562835728358283592836028361283622836328364283652836628367283682836928370283712837228373283742837528376283772837828379283802838128382283832838428385283862838728388283892839028391283922839328394283952839628397283982839928400284012840228403284042840528406284072840828409284102841128412284132841428415284162841728418284192842028421284222842328424284252842628427284282842928430284312843228433284342843528436284372843828439284402844128442284432844428445284462844728448284492845028451284522845328454284552845628457284582845928460284612846228463284642846528466284672846828469284702847128472284732847428475284762847728478284792848028481284822848328484284852848628487284882848928490284912849228493284942849528496284972849828499285002850128502285032850428505285062850728508285092851028511285122851328514285152851628517285182851928520285212852228523285242852528526285272852828529285302853128532285332853428535285362853728538285392854028541285422854328544285452854628547285482854928550285512855228553285542855528556285572855828559285602856128562285632856428565285662856728568285692857028571285722857328574285752857628577285782857928580285812858228583285842858528586285872858828589285902859128592285932859428595285962859728598285992860028601286022860328604286052860628607286082860928610286112861228613286142861528616286172861828619286202862128622286232862428625286262862728628286292863028631286322863328634286352863628637286382863928640286412864228643286442864528646286472864828649286502865128652286532865428655286562865728658286592866028661286622866328664286652866628667286682866928670286712867228673286742867528676286772867828679286802868128682286832868428685286862868728688286892869028691286922869328694286952869628697286982869928700287012870228703287042870528706287072870828709287102871128712287132871428715287162871728718287192872028721287222872328724287252872628727287282872928730287312873228733287342873528736287372873828739287402874128742287432874428745287462874728748287492875028751287522875328754287552875628757287582875928760287612876228763287642876528766287672876828769287702877128772287732877428775287762877728778287792878028781287822878328784287852878628787287882878928790287912879228793287942879528796287972879828799288002880128802288032880428805288062880728808288092881028811288122881328814288152881628817288182881928820288212882228823288242882528826288272882828829288302883128832288332883428835288362883728838288392884028841288422884328844288452884628847288482884928850288512885228853288542885528856288572885828859288602886128862288632886428865288662886728868288692887028871288722887328874288752887628877288782887928880288812888228883288842888528886288872888828889288902889128892288932889428895288962889728898288992890028901289022890328904289052890628907289082890928910289112891228913289142891528916289172891828919289202892128922289232892428925289262892728928289292893028931289322893328934289352893628937289382893928940289412894228943289442894528946289472894828949289502895128952289532895428955289562895728958289592896028961289622896328964289652896628967289682896928970289712897228973289742897528976289772897828979289802898128982289832898428985289862898728988289892899028991289922899328994289952899628997289982899929000290012900229003290042900529006290072900829009290102901129012290132901429015290162901729018290192902029021290222902329024290252902629027290282902929030290312903229033290342903529036290372903829039290402904129042290432904429045290462904729048290492905029051290522905329054290552905629057290582905929060290612906229063290642906529066290672906829069290702907129072290732907429075290762907729078290792908029081290822908329084290852908629087290882908929090290912909229093290942909529096290972909829099291002910129102291032910429105291062910729108291092911029111291122911329114291152911629117291182911929120291212912229123291242912529126291272912829129291302913129132291332913429135291362913729138291392914029141291422914329144291452914629147291482914929150291512915229153291542915529156291572915829159291602916129162291632916429165291662916729168291692917029171291722917329174291752917629177291782917929180291812918229183291842918529186291872918829189291902919129192291932919429195291962919729198291992920029201292022920329204292052920629207292082920929210292112921229213292142921529216292172921829219292202922129222292232922429225292262922729228292292923029231292322923329234292352923629237292382923929240292412924229243292442924529246292472924829249292502925129252292532925429255292562925729258292592926029261292622926329264292652926629267292682926929270292712927229273292742927529276292772927829279292802928129282292832928429285292862928729288292892929029291292922929329294292952929629297292982929929300293012930229303293042930529306293072930829309293102931129312293132931429315293162931729318293192932029321293222932329324293252932629327293282932929330293312933229333293342933529336293372933829339293402934129342293432934429345293462934729348293492935029351293522935329354293552935629357293582935929360293612936229363293642936529366293672936829369293702937129372293732937429375293762937729378293792938029381293822938329384293852938629387293882938929390293912939229393293942939529396293972939829399294002940129402294032940429405294062940729408294092941029411294122941329414294152941629417294182941929420294212942229423294242942529426294272942829429294302943129432294332943429435294362943729438294392944029441294422944329444294452944629447294482944929450294512945229453294542945529456294572945829459294602946129462294632946429465294662946729468294692947029471294722947329474294752947629477294782947929480294812948229483294842948529486294872948829489294902949129492294932949429495294962949729498294992950029501295022950329504295052950629507295082950929510295112951229513295142951529516295172951829519295202952129522295232952429525295262952729528295292953029531295322953329534295352953629537295382953929540295412954229543295442954529546295472954829549295502955129552295532955429555295562955729558295592956029561295622956329564295652956629567295682956929570295712957229573295742957529576295772957829579295802958129582295832958429585295862958729588295892959029591295922959329594295952959629597295982959929600296012960229603296042960529606296072960829609296102961129612296132961429615296162961729618296192962029621296222962329624296252962629627296282962929630296312963229633296342963529636296372963829639296402964129642296432964429645296462964729648296492965029651296522965329654296552965629657296582965929660296612966229663296642966529666296672966829669296702967129672296732967429675296762967729678296792968029681296822968329684296852968629687296882968929690296912969229693296942969529696296972969829699297002970129702297032970429705297062970729708297092971029711297122971329714297152971629717297182971929720297212972229723297242972529726297272972829729297302973129732297332973429735297362973729738297392974029741297422974329744297452974629747297482974929750297512975229753297542975529756297572975829759297602976129762297632976429765297662976729768297692977029771297722977329774297752977629777297782977929780297812978229783297842978529786297872978829789297902979129792297932979429795297962979729798297992980029801298022980329804298052980629807298082980929810298112981229813298142981529816298172981829819298202982129822298232982429825298262982729828298292983029831298322983329834298352983629837298382983929840298412984229843298442984529846298472984829849298502985129852298532985429855298562985729858298592986029861298622986329864298652986629867298682986929870298712987229873298742987529876298772987829879298802988129882298832988429885298862988729888298892989029891298922989329894298952989629897298982989929900299012990229903299042990529906299072990829909299102991129912299132991429915299162991729918299192992029921299222992329924299252992629927299282992929930299312993229933299342993529936299372993829939299402994129942299432994429945299462994729948299492995029951299522995329954299552995629957299582995929960299612996229963299642996529966299672996829969299702997129972299732997429975299762997729978299792998029981299822998329984299852998629987299882998929990299912999229993299942999529996299972999829999300003000130002300033000430005300063000730008300093001030011300123001330014300153001630017300183001930020300213002230023300243002530026300273002830029300303003130032300333003430035300363003730038300393004030041300423004330044300453004630047300483004930050300513005230053300543005530056300573005830059300603006130062300633006430065300663006730068300693007030071300723007330074300753007630077300783007930080300813008230083300843008530086300873008830089300903009130092300933009430095300963009730098300993010030101301023010330104301053010630107301083010930110301113011230113301143011530116301173011830119301203012130122301233012430125301263012730128301293013030131301323013330134301353013630137301383013930140301413014230143301443014530146301473014830149301503015130152301533015430155301563015730158301593016030161301623016330164301653016630167301683016930170301713017230173301743017530176301773017830179301803018130182301833018430185301863018730188301893019030191301923019330194301953019630197301983019930200302013020230203302043020530206302073020830209302103021130212302133021430215302163021730218302193022030221302223022330224302253022630227302283022930230302313023230233302343023530236302373023830239302403024130242302433024430245302463024730248302493025030251302523025330254302553025630257302583025930260302613026230263302643026530266302673026830269302703027130272302733027430275302763027730278302793028030281302823028330284302853028630287302883028930290302913029230293302943029530296302973029830299303003030130302303033030430305303063030730308303093031030311303123031330314303153031630317303183031930320303213032230323303243032530326303273032830329303303033130332303333033430335303363033730338303393034030341303423034330344303453034630347303483034930350303513035230353303543035530356303573035830359303603036130362303633036430365303663036730368303693037030371303723037330374303753037630377303783037930380303813038230383303843038530386303873038830389303903039130392303933039430395303963039730398303993040030401304023040330404304053040630407304083040930410304113041230413304143041530416304173041830419304203042130422304233042430425304263042730428304293043030431304323043330434304353043630437304383043930440304413044230443304443044530446304473044830449304503045130452304533045430455304563045730458304593046030461304623046330464304653046630467304683046930470304713047230473304743047530476304773047830479304803048130482304833048430485304863048730488304893049030491304923049330494304953049630497304983049930500305013050230503305043050530506305073050830509305103051130512305133051430515305163051730518305193052030521305223052330524305253052630527305283052930530305313053230533305343053530536305373053830539305403054130542305433054430545305463054730548305493055030551305523055330554305553055630557305583055930560305613056230563305643056530566305673056830569305703057130572305733057430575305763057730578305793058030581305823058330584305853058630587305883058930590305913059230593305943059530596305973059830599306003060130602306033060430605306063060730608306093061030611306123061330614306153061630617306183061930620306213062230623306243062530626306273062830629306303063130632306333063430635306363063730638306393064030641306423064330644306453064630647306483064930650306513065230653306543065530656306573065830659306603066130662306633066430665306663066730668306693067030671306723067330674306753067630677306783067930680306813068230683306843068530686306873068830689306903069130692306933069430695306963069730698306993070030701307023070330704307053070630707307083070930710307113071230713307143071530716307173071830719307203072130722307233072430725307263072730728307293073030731307323073330734307353073630737307383073930740307413074230743307443074530746307473074830749307503075130752307533075430755307563075730758307593076030761307623076330764307653076630767307683076930770307713077230773307743077530776307773077830779307803078130782307833078430785307863078730788307893079030791307923079330794307953079630797307983079930800308013080230803308043080530806308073080830809308103081130812308133081430815308163081730818308193082030821308223082330824308253082630827308283082930830308313083230833308343083530836308373083830839308403084130842308433084430845308463084730848308493085030851308523085330854308553085630857308583085930860308613086230863308643086530866308673086830869308703087130872308733087430875308763087730878308793088030881308823088330884308853088630887308883088930890308913089230893308943089530896308973089830899309003090130902309033090430905309063090730908309093091030911309123091330914309153091630917309183091930920309213092230923309243092530926309273092830929309303093130932309333093430935309363093730938309393094030941309423094330944309453094630947309483094930950309513095230953309543095530956309573095830959309603096130962309633096430965309663096730968309693097030971309723097330974309753097630977309783097930980309813098230983309843098530986309873098830989309903099130992309933099430995309963099730998309993100031001310023100331004310053100631007310083100931010310113101231013310143101531016310173101831019310203102131022310233102431025310263102731028310293103031031310323103331034310353103631037310383103931040310413104231043310443104531046310473104831049310503105131052310533105431055310563105731058310593106031061310623106331064310653106631067310683106931070310713107231073310743107531076310773107831079310803108131082310833108431085310863108731088310893109031091310923109331094310953109631097310983109931100311013110231103311043110531106311073110831109311103111131112311133111431115311163111731118311193112031121311223112331124311253112631127311283112931130311313113231133311343113531136311373113831139311403114131142311433114431145311463114731148311493115031151311523115331154311553115631157311583115931160311613116231163311643116531166311673116831169311703117131172311733117431175311763117731178311793118031181311823118331184311853118631187311883118931190311913119231193311943119531196311973119831199312003120131202312033120431205312063120731208312093121031211312123121331214312153121631217312183121931220312213122231223312243122531226312273122831229312303123131232312333123431235312363123731238312393124031241312423124331244312453124631247312483124931250312513125231253312543125531256312573125831259312603126131262312633126431265312663126731268312693127031271312723127331274312753127631277312783127931280312813128231283312843128531286312873128831289312903129131292312933129431295312963129731298312993130031301313023130331304313053130631307313083130931310313113131231313313143131531316313173131831319313203132131322313233132431325313263132731328313293133031331313323133331334313353133631337313383133931340313413134231343313443134531346313473134831349313503135131352313533135431355313563135731358313593136031361313623136331364313653136631367313683136931370313713137231373313743137531376313773137831379313803138131382313833138431385313863138731388313893139031391313923139331394313953139631397313983139931400314013140231403314043140531406314073140831409314103141131412314133141431415314163141731418314193142031421314223142331424314253142631427314283142931430314313143231433314343143531436314373143831439314403144131442314433144431445314463144731448314493145031451314523145331454314553145631457314583145931460314613146231463314643146531466314673146831469314703147131472314733147431475314763147731478314793148031481314823148331484314853148631487314883148931490314913149231493314943149531496314973149831499315003150131502315033150431505315063150731508315093151031511315123151331514315153151631517315183151931520315213152231523315243152531526315273152831529315303153131532315333153431535315363153731538315393154031541315423154331544315453154631547315483154931550315513155231553315543155531556315573155831559315603156131562315633156431565315663156731568315693157031571315723157331574315753157631577315783157931580315813158231583315843158531586315873158831589315903159131592315933159431595315963159731598315993160031601316023160331604316053160631607316083160931610316113161231613316143161531616316173161831619316203162131622316233162431625316263162731628316293163031631316323163331634316353163631637316383163931640316413164231643316443164531646316473164831649316503165131652316533165431655316563165731658316593166031661316623166331664316653166631667316683166931670316713167231673316743167531676316773167831679316803168131682316833168431685316863168731688316893169031691316923169331694316953169631697316983169931700317013170231703317043170531706317073170831709317103171131712317133171431715317163171731718317193172031721317223172331724317253172631727317283172931730317313173231733317343173531736317373173831739317403174131742317433174431745317463174731748317493175031751317523175331754317553175631757317583175931760317613176231763317643176531766317673176831769317703177131772317733177431775317763177731778317793178031781317823178331784317853178631787317883178931790317913179231793317943179531796317973179831799318003180131802318033180431805318063180731808318093181031811318123181331814318153181631817318183181931820318213182231823318243182531826318273182831829318303183131832318333183431835318363183731838318393184031841318423184331844318453184631847318483184931850318513185231853318543185531856318573185831859318603186131862318633186431865318663186731868318693187031871318723187331874318753187631877318783187931880318813188231883318843188531886318873188831889318903189131892318933189431895318963189731898318993190031901319023190331904319053190631907319083190931910319113191231913319143191531916319173191831919319203192131922319233192431925319263192731928319293193031931319323193331934319353193631937319383193931940319413194231943319443194531946319473194831949319503195131952319533195431955319563195731958319593196031961319623196331964319653196631967319683196931970319713197231973319743197531976319773197831979319803198131982319833198431985319863198731988319893199031991319923199331994319953199631997319983199932000320013200232003320043200532006320073200832009320103201132012320133201432015320163201732018320193202032021320223202332024320253202632027320283202932030320313203232033320343203532036320373203832039320403204132042320433204432045320463204732048320493205032051320523205332054320553205632057320583205932060320613206232063320643206532066320673206832069320703207132072320733207432075320763207732078320793208032081320823208332084320853208632087320883208932090320913209232093320943209532096320973209832099321003210132102321033210432105321063210732108321093211032111321123211332114321153211632117321183211932120321213212232123321243212532126321273212832129321303213132132321333213432135321363213732138321393214032141321423214332144321453214632147321483214932150321513215232153321543215532156321573215832159321603216132162321633216432165321663216732168321693217032171321723217332174321753217632177321783217932180321813218232183321843218532186321873218832189321903219132192321933219432195321963219732198321993220032201322023220332204322053220632207322083220932210322113221232213322143221532216322173221832219322203222132222322233222432225322263222732228322293223032231322323223332234322353223632237322383223932240322413224232243322443224532246322473224832249322503225132252322533225432255322563225732258322593226032261322623226332264322653226632267322683226932270322713227232273322743227532276322773227832279322803228132282322833228432285322863228732288322893229032291322923229332294322953229632297322983229932300323013230232303323043230532306323073230832309323103231132312323133231432315323163231732318323193232032321323223232332324323253232632327323283232932330323313233232333323343233532336323373233832339323403234132342323433234432345323463234732348323493235032351323523235332354323553235632357323583235932360323613236232363323643236532366323673236832369323703237132372323733237432375323763237732378323793238032381323823238332384323853238632387323883238932390323913239232393323943239532396323973239832399324003240132402324033240432405324063240732408324093241032411324123241332414324153241632417324183241932420324213242232423324243242532426324273242832429324303243132432324333243432435324363243732438324393244032441324423244332444324453244632447324483244932450324513245232453324543245532456324573245832459324603246132462324633246432465324663246732468324693247032471324723247332474324753247632477324783247932480324813248232483324843248532486324873248832489324903249132492324933249432495324963249732498324993250032501325023250332504325053250632507325083250932510325113251232513325143251532516325173251832519325203252132522325233252432525325263252732528325293253032531325323253332534325353253632537325383253932540325413254232543325443254532546325473254832549325503255132552325533255432555325563255732558325593256032561325623256332564325653256632567325683256932570325713257232573325743257532576325773257832579325803258132582325833258432585325863258732588325893259032591325923259332594325953259632597325983259932600326013260232603326043260532606326073260832609326103261132612326133261432615326163261732618326193262032621326223262332624326253262632627326283262932630326313263232633326343263532636326373263832639326403264132642326433264432645326463264732648326493265032651326523265332654326553265632657326583265932660326613266232663326643266532666326673266832669326703267132672326733267432675326763267732678326793268032681326823268332684326853268632687326883268932690326913269232693326943269532696326973269832699327003270132702327033270432705327063270732708327093271032711327123271332714327153271632717327183271932720327213272232723327243272532726327273272832729327303273132732327333273432735327363273732738327393274032741327423274332744327453274632747327483274932750327513275232753327543275532756327573275832759327603276132762327633276432765327663276732768327693277032771327723277332774327753277632777327783277932780327813278232783327843278532786327873278832789327903279132792327933279432795327963279732798327993280032801328023280332804328053280632807328083280932810328113281232813328143281532816328173281832819328203282132822328233282432825328263282732828328293283032831328323283332834328353283632837328383283932840328413284232843328443284532846328473284832849328503285132852328533285432855328563285732858328593286032861328623286332864328653286632867328683286932870328713287232873328743287532876328773287832879328803288132882328833288432885328863288732888328893289032891328923289332894328953289632897328983289932900329013290232903329043290532906329073290832909329103291132912329133291432915329163291732918329193292032921329223292332924329253292632927329283292932930329313293232933329343293532936329373293832939329403294132942329433294432945329463294732948329493295032951329523295332954329553295632957329583295932960329613296232963329643296532966329673296832969329703297132972329733297432975329763297732978329793298032981329823298332984329853298632987329883298932990329913299232993329943299532996329973299832999330003300133002330033300433005330063300733008330093301033011330123301333014330153301633017330183301933020330213302233023330243302533026330273302833029330303303133032330333303433035330363303733038330393304033041330423304333044330453304633047330483304933050330513305233053330543305533056330573305833059330603306133062330633306433065330663306733068330693307033071330723307333074330753307633077330783307933080330813308233083330843308533086330873308833089330903309133092330933309433095330963309733098330993310033101331023310333104331053310633107331083310933110331113311233113331143311533116331173311833119331203312133122331233312433125331263312733128331293313033131331323313333134331353313633137331383313933140331413314233143331443314533146331473314833149331503315133152331533315433155331563315733158331593316033161331623316333164331653316633167331683316933170331713317233173331743317533176331773317833179331803318133182331833318433185331863318733188331893319033191331923319333194331953319633197331983319933200332013320233203332043320533206332073320833209332103321133212332133321433215332163321733218332193322033221332223322333224332253322633227332283322933230332313323233233332343323533236332373323833239332403324133242332433324433245332463324733248332493325033251332523325333254332553325633257332583325933260332613326233263332643326533266332673326833269332703327133272332733327433275332763327733278332793328033281332823328333284332853328633287332883328933290332913329233293332943329533296332973329833299333003330133302333033330433305333063330733308333093331033311333123331333314333153331633317333183331933320333213332233323333243332533326333273332833329333303333133332333333333433335333363333733338333393334033341333423334333344333453334633347333483334933350333513335233353333543335533356333573335833359333603336133362333633336433365333663336733368333693337033371333723337333374333753337633377333783337933380333813338233383333843338533386333873338833389333903339133392333933339433395333963339733398333993340033401334023340333404334053340633407334083340933410334113341233413334143341533416334173341833419334203342133422334233342433425334263342733428334293343033431334323343333434334353343633437334383343933440334413344233443334443344533446334473344833449334503345133452334533345433455334563345733458334593346033461334623346333464334653346633467334683346933470334713347233473334743347533476334773347833479334803348133482334833348433485334863348733488334893349033491334923349333494334953349633497334983349933500335013350233503335043350533506335073350833509335103351133512335133351433515335163351733518335193352033521335223352333524335253352633527335283352933530335313353233533335343353533536335373353833539335403354133542335433354433545335463354733548335493355033551335523355333554335553355633557335583355933560335613356233563335643356533566335673356833569335703357133572335733357433575335763357733578335793358033581335823358333584335853358633587335883358933590335913359233593335943359533596335973359833599336003360133602336033360433605336063360733608336093361033611336123361333614336153361633617336183361933620336213362233623336243362533626336273362833629336303363133632336333363433635336363363733638336393364033641336423364333644336453364633647336483364933650336513365233653336543365533656336573365833659336603366133662336633366433665336663366733668336693367033671336723367333674336753367633677336783367933680336813368233683336843368533686336873368833689336903369133692336933369433695336963369733698336993370033701337023370333704337053370633707337083370933710337113371233713337143371533716337173371833719337203372133722337233372433725337263372733728337293373033731337323373333734337353373633737337383373933740337413374233743337443374533746337473374833749337503375133752337533375433755337563375733758337593376033761337623376333764337653376633767337683376933770337713377233773337743377533776337773377833779337803378133782337833378433785337863378733788337893379033791337923379333794337953379633797337983379933800338013380233803338043380533806338073380833809338103381133812338133381433815338163381733818338193382033821338223382333824338253382633827338283382933830338313383233833338343383533836338373383833839338403384133842338433384433845338463384733848338493385033851338523385333854338553385633857338583385933860338613386233863338643386533866338673386833869338703387133872338733387433875338763387733878338793388033881338823388333884338853388633887338883388933890338913389233893338943389533896338973389833899339003390133902339033390433905339063390733908339093391033911339123391333914339153391633917339183391933920339213392233923339243392533926339273392833929339303393133932339333393433935339363393733938339393394033941339423394333944339453394633947339483394933950339513395233953339543395533956339573395833959339603396133962339633396433965339663396733968339693397033971339723397333974339753397633977339783397933980339813398233983339843398533986339873398833989339903399133992339933399433995339963399733998339993400034001340023400334004340053400634007340083400934010340113401234013340143401534016340173401834019340203402134022340233402434025340263402734028340293403034031340323403334034340353403634037340383403934040340413404234043340443404534046340473404834049340503405134052340533405434055340563405734058340593406034061340623406334064340653406634067340683406934070340713407234073340743407534076340773407834079340803408134082340833408434085340863408734088340893409034091340923409334094340953409634097340983409934100341013410234103341043410534106341073410834109341103411134112341133411434115341163411734118341193412034121341223412334124341253412634127341283412934130341313413234133341343413534136341373413834139341403414134142341433414434145341463414734148341493415034151341523415334154341553415634157341583415934160341613416234163341643416534166341673416834169341703417134172341733417434175341763417734178341793418034181341823418334184341853418634187341883418934190341913419234193341943419534196341973419834199342003420134202342033420434205342063420734208342093421034211342123421334214342153421634217342183421934220342213422234223342243422534226342273422834229342303423134232342333423434235342363423734238342393424034241342423424334244342453424634247342483424934250342513425234253342543425534256342573425834259342603426134262342633426434265342663426734268342693427034271342723427334274342753427634277342783427934280342813428234283342843428534286342873428834289342903429134292342933429434295342963429734298342993430034301343023430334304343053430634307343083430934310343113431234313343143431534316343173431834319343203432134322343233432434325343263432734328343293433034331343323433334334343353433634337343383433934340343413434234343343443434534346343473434834349343503435134352343533435434355343563435734358343593436034361343623436334364343653436634367343683436934370343713437234373343743437534376343773437834379343803438134382343833438434385343863438734388343893439034391343923439334394343953439634397343983439934400344013440234403344043440534406344073440834409344103441134412344133441434415344163441734418344193442034421344223442334424344253442634427344283442934430344313443234433344343443534436344373443834439344403444134442344433444434445344463444734448344493445034451344523445334454344553445634457344583445934460344613446234463344643446534466344673446834469344703447134472344733447434475344763447734478344793448034481344823448334484344853448634487344883448934490344913449234493344943449534496344973449834499345003450134502345033450434505345063450734508345093451034511345123451334514345153451634517345183451934520345213452234523345243452534526345273452834529345303453134532345333453434535345363453734538345393454034541345423454334544345453454634547345483454934550345513455234553345543455534556345573455834559345603456134562345633456434565345663456734568345693457034571345723457334574345753457634577345783457934580345813458234583345843458534586345873458834589345903459134592345933459434595345963459734598345993460034601346023460334604346053460634607346083460934610346113461234613346143461534616346173461834619346203462134622346233462434625346263462734628346293463034631346323463334634346353463634637346383463934640346413464234643346443464534646346473464834649346503465134652346533465434655346563465734658346593466034661346623466334664346653466634667346683466934670346713467234673346743467534676346773467834679346803468134682346833468434685346863468734688346893469034691346923469334694346953469634697346983469934700347013470234703347043470534706347073470834709347103471134712347133471434715347163471734718347193472034721347223472334724347253472634727347283472934730347313473234733347343473534736347373473834739347403474134742347433474434745347463474734748347493475034751347523475334754347553475634757347583475934760347613476234763347643476534766347673476834769347703477134772347733477434775347763477734778347793478034781347823478334784347853478634787347883478934790347913479234793347943479534796347973479834799348003480134802348033480434805348063480734808348093481034811348123481334814348153481634817348183481934820348213482234823348243482534826348273482834829348303483134832348333483434835348363483734838348393484034841348423484334844348453484634847348483484934850348513485234853348543485534856348573485834859348603486134862348633486434865348663486734868348693487034871348723487334874348753487634877348783487934880348813488234883348843488534886348873488834889348903489134892348933489434895348963489734898348993490034901349023490334904349053490634907349083490934910349113491234913349143491534916349173491834919349203492134922349233492434925349263492734928349293493034931349323493334934349353493634937349383493934940349413494234943349443494534946349473494834949349503495134952349533495434955349563495734958349593496034961349623496334964349653496634967349683496934970349713497234973349743497534976349773497834979349803498134982349833498434985349863498734988349893499034991349923499334994349953499634997349983499935000350013500235003350043500535006350073500835009350103501135012350133501435015350163501735018350193502035021350223502335024350253502635027350283502935030350313503235033350343503535036350373503835039350403504135042350433504435045350463504735048350493505035051350523505335054350553505635057350583505935060350613506235063350643506535066350673506835069350703507135072350733507435075350763507735078350793508035081350823508335084350853508635087350883508935090350913509235093350943509535096350973509835099351003510135102351033510435105351063510735108351093511035111351123511335114351153511635117351183511935120351213512235123351243512535126351273512835129351303513135132351333513435135351363513735138351393514035141351423514335144351453514635147351483514935150351513515235153351543515535156351573515835159351603516135162351633516435165351663516735168351693517035171351723517335174351753517635177351783517935180351813518235183351843518535186351873518835189351903519135192351933519435195351963519735198351993520035201352023520335204352053520635207352083520935210352113521235213352143521535216352173521835219352203522135222352233522435225352263522735228352293523035231352323523335234352353523635237352383523935240352413524235243352443524535246352473524835249352503525135252352533525435255352563525735258352593526035261352623526335264352653526635267352683526935270352713527235273352743527535276352773527835279352803528135282352833528435285352863528735288352893529035291352923529335294352953529635297352983529935300353013530235303353043530535306353073530835309353103531135312353133531435315353163531735318353193532035321353223532335324353253532635327353283532935330353313533235333353343533535336353373533835339353403534135342353433534435345353463534735348353493535035351353523535335354353553535635357353583535935360353613536235363353643536535366353673536835369353703537135372353733537435375353763537735378353793538035381353823538335384353853538635387353883538935390353913539235393353943539535396353973539835399354003540135402354033540435405354063540735408354093541035411354123541335414354153541635417354183541935420354213542235423354243542535426354273542835429354303543135432354333543435435354363543735438354393544035441354423544335444354453544635447354483544935450354513545235453354543545535456354573545835459354603546135462354633546435465354663546735468354693547035471354723547335474354753547635477354783547935480354813548235483354843548535486354873548835489354903549135492354933549435495354963549735498354993550035501355023550335504355053550635507355083550935510355113551235513355143551535516355173551835519355203552135522355233552435525355263552735528355293553035531355323553335534355353553635537355383553935540355413554235543355443554535546355473554835549355503555135552355533555435555355563555735558355593556035561355623556335564355653556635567355683556935570355713557235573355743557535576355773557835579355803558135582355833558435585355863558735588355893559035591355923559335594355953559635597355983559935600356013560235603356043560535606356073560835609356103561135612356133561435615356163561735618356193562035621356223562335624356253562635627356283562935630356313563235633356343563535636356373563835639356403564135642356433564435645356463564735648356493565035651356523565335654356553565635657356583565935660356613566235663356643566535666356673566835669356703567135672356733567435675356763567735678356793568035681356823568335684356853568635687356883568935690356913569235693356943569535696356973569835699357003570135702357033570435705357063570735708357093571035711357123571335714357153571635717357183571935720357213572235723357243572535726357273572835729357303573135732357333573435735357363573735738357393574035741357423574335744357453574635747357483574935750357513575235753357543575535756357573575835759357603576135762357633576435765357663576735768357693577035771357723577335774357753577635777357783577935780357813578235783357843578535786357873578835789357903579135792357933579435795357963579735798357993580035801358023580335804358053580635807358083580935810358113581235813358143581535816358173581835819358203582135822358233582435825358263582735828358293583035831358323583335834358353583635837358383583935840358413584235843358443584535846358473584835849358503585135852358533585435855358563585735858358593586035861358623586335864358653586635867358683586935870358713587235873358743587535876358773587835879358803588135882358833588435885358863588735888358893589035891358923589335894358953589635897358983589935900359013590235903359043590535906359073590835909359103591135912359133591435915359163591735918359193592035921359223592335924359253592635927359283592935930359313593235933359343593535936359373593835939359403594135942359433594435945359463594735948359493595035951359523595335954359553595635957359583595935960359613596235963359643596535966359673596835969359703597135972359733597435975359763597735978359793598035981359823598335984359853598635987359883598935990359913599235993359943599535996359973599835999360003600136002360033600436005360063600736008360093601036011360123601336014360153601636017360183601936020360213602236023360243602536026360273602836029360303603136032360333603436035360363603736038360393604036041360423604336044360453604636047360483604936050360513605236053360543605536056360573605836059360603606136062360633606436065360663606736068360693607036071360723607336074360753607636077360783607936080360813608236083360843608536086360873608836089360903609136092360933609436095360963609736098360993610036101361023610336104361053610636107361083610936110361113611236113361143611536116361173611836119361203612136122361233612436125361263612736128361293613036131361323613336134361353613636137361383613936140361413614236143361443614536146361473614836149361503615136152361533615436155361563615736158361593616036161361623616336164361653616636167361683616936170361713617236173361743617536176361773617836179361803618136182361833618436185361863618736188361893619036191361923619336194361953619636197361983619936200362013620236203362043620536206362073620836209362103621136212362133621436215362163621736218362193622036221362223622336224362253622636227362283622936230362313623236233362343623536236362373623836239362403624136242362433624436245362463624736248362493625036251362523625336254362553625636257362583625936260362613626236263362643626536266362673626836269362703627136272362733627436275362763627736278362793628036281362823628336284362853628636287362883628936290362913629236293362943629536296362973629836299363003630136302363033630436305363063630736308363093631036311363123631336314363153631636317363183631936320363213632236323363243632536326363273632836329363303633136332363333633436335363363633736338363393634036341363423634336344363453634636347363483634936350363513635236353363543635536356363573635836359363603636136362363633636436365363663636736368363693637036371363723637336374363753637636377363783637936380363813638236383363843638536386363873638836389363903639136392363933639436395363963639736398363993640036401364023640336404364053640636407364083640936410364113641236413364143641536416364173641836419364203642136422364233642436425364263642736428364293643036431364323643336434364353643636437364383643936440364413644236443364443644536446364473644836449364503645136452364533645436455364563645736458364593646036461364623646336464364653646636467364683646936470364713647236473364743647536476364773647836479364803648136482364833648436485364863648736488364893649036491364923649336494364953649636497364983649936500365013650236503365043650536506365073650836509365103651136512365133651436515365163651736518365193652036521365223652336524365253652636527365283652936530365313653236533365343653536536365373653836539365403654136542365433654436545365463654736548365493655036551365523655336554365553655636557365583655936560365613656236563365643656536566365673656836569365703657136572365733657436575365763657736578365793658036581365823658336584365853658636587365883658936590365913659236593365943659536596365973659836599366003660136602366033660436605366063660736608366093661036611366123661336614366153661636617366183661936620366213662236623366243662536626366273662836629366303663136632366333663436635366363663736638366393664036641366423664336644366453664636647366483664936650366513665236653366543665536656366573665836659366603666136662366633666436665366663666736668366693667036671366723667336674366753667636677366783667936680366813668236683366843668536686366873668836689366903669136692366933669436695366963669736698366993670036701367023670336704367053670636707367083670936710367113671236713367143671536716367173671836719367203672136722367233672436725367263672736728367293673036731367323673336734367353673636737367383673936740367413674236743367443674536746367473674836749367503675136752367533675436755367563675736758367593676036761367623676336764367653676636767367683676936770367713677236773367743677536776367773677836779367803678136782367833678436785367863678736788367893679036791367923679336794367953679636797367983679936800368013680236803368043680536806368073680836809368103681136812368133681436815368163681736818368193682036821368223682336824368253682636827368283682936830368313683236833368343683536836368373683836839368403684136842368433684436845368463684736848368493685036851368523685336854368553685636857368583685936860368613686236863368643686536866368673686836869368703687136872368733687436875368763687736878368793688036881368823688336884368853688636887368883688936890368913689236893368943689536896368973689836899369003690136902369033690436905369063690736908369093691036911369123691336914369153691636917369183691936920369213692236923369243692536926369273692836929369303693136932369333693436935369363693736938369393694036941369423694336944369453694636947369483694936950369513695236953369543695536956369573695836959369603696136962369633696436965369663696736968369693697036971369723697336974369753697636977369783697936980369813698236983369843698536986369873698836989369903699136992369933699436995369963699736998369993700037001370023700337004370053700637007370083700937010370113701237013370143701537016370173701837019370203702137022370233702437025370263702737028370293703037031370323703337034370353703637037370383703937040370413704237043370443704537046370473704837049370503705137052370533705437055370563705737058370593706037061370623706337064370653706637067370683706937070370713707237073370743707537076370773707837079370803708137082370833708437085370863708737088370893709037091370923709337094370953709637097370983709937100371013710237103371043710537106371073710837109371103711137112371133711437115371163711737118371193712037121371223712337124371253712637127371283712937130371313713237133371343713537136371373713837139371403714137142371433714437145371463714737148371493715037151371523715337154371553715637157371583715937160371613716237163371643716537166371673716837169371703717137172371733717437175371763717737178371793718037181371823718337184371853718637187371883718937190371913719237193371943719537196371973719837199372003720137202372033720437205372063720737208372093721037211372123721337214372153721637217372183721937220372213722237223372243722537226372273722837229372303723137232372333723437235372363723737238372393724037241372423724337244372453724637247372483724937250372513725237253372543725537256372573725837259372603726137262372633726437265372663726737268372693727037271372723727337274372753727637277372783727937280372813728237283372843728537286372873728837289372903729137292372933729437295372963729737298372993730037301373023730337304373053730637307373083730937310373113731237313373143731537316373173731837319373203732137322373233732437325373263732737328373293733037331373323733337334373353733637337373383733937340373413734237343373443734537346373473734837349373503735137352373533735437355373563735737358373593736037361373623736337364373653736637367373683736937370373713737237373373743737537376373773737837379373803738137382373833738437385373863738737388373893739037391373923739337394373953739637397373983739937400374013740237403374043740537406374073740837409374103741137412374133741437415374163741737418374193742037421374223742337424374253742637427374283742937430374313743237433374343743537436374373743837439374403744137442374433744437445374463744737448374493745037451374523745337454374553745637457374583745937460374613746237463374643746537466374673746837469374703747137472374733747437475374763747737478374793748037481374823748337484374853748637487374883748937490374913749237493374943749537496374973749837499375003750137502375033750437505375063750737508375093751037511375123751337514375153751637517375183751937520375213752237523375243752537526375273752837529375303753137532375333753437535375363753737538375393754037541375423754337544375453754637547375483754937550375513755237553375543755537556375573755837559375603756137562375633756437565375663756737568375693757037571375723757337574375753757637577375783757937580375813758237583375843758537586375873758837589375903759137592375933759437595375963759737598375993760037601376023760337604376053760637607376083760937610376113761237613376143761537616376173761837619376203762137622376233762437625376263762737628376293763037631376323763337634376353763637637376383763937640376413764237643376443764537646376473764837649376503765137652376533765437655376563765737658376593766037661376623766337664376653766637667376683766937670376713767237673376743767537676376773767837679376803768137682376833768437685376863768737688376893769037691376923769337694376953769637697376983769937700377013770237703377043770537706377073770837709377103771137712377133771437715377163771737718377193772037721377223772337724377253772637727377283772937730377313773237733377343773537736377373773837739377403774137742377433774437745377463774737748377493775037751377523775337754377553775637757377583775937760377613776237763377643776537766377673776837769377703777137772377733777437775377763777737778377793778037781377823778337784377853778637787377883778937790377913779237793377943779537796377973779837799378003780137802378033780437805378063780737808378093781037811378123781337814378153781637817378183781937820378213782237823378243782537826378273782837829378303783137832378333783437835378363783737838378393784037841378423784337844378453784637847378483784937850378513785237853378543785537856378573785837859378603786137862378633786437865378663786737868378693787037871378723787337874378753787637877378783787937880378813788237883378843788537886378873788837889378903789137892378933789437895378963789737898378993790037901379023790337904379053790637907379083790937910379113791237913379143791537916379173791837919379203792137922379233792437925379263792737928379293793037931379323793337934379353793637937379383793937940379413794237943379443794537946379473794837949379503795137952379533795437955379563795737958379593796037961379623796337964379653796637967379683796937970379713797237973379743797537976379773797837979379803798137982379833798437985379863798737988379893799037991379923799337994379953799637997379983799938000380013800238003380043800538006380073800838009380103801138012380133801438015380163801738018380193802038021380223802338024380253802638027380283802938030380313803238033380343803538036380373803838039380403804138042380433804438045380463804738048380493805038051380523805338054380553805638057380583805938060380613806238063380643806538066380673806838069380703807138072380733807438075380763807738078380793808038081380823808338084380853808638087380883808938090380913809238093380943809538096380973809838099381003810138102381033810438105381063810738108381093811038111381123811338114381153811638117381183811938120381213812238123381243812538126381273812838129381303813138132381333813438135381363813738138381393814038141381423814338144381453814638147381483814938150381513815238153381543815538156381573815838159381603816138162381633816438165381663816738168381693817038171381723817338174381753817638177381783817938180381813818238183381843818538186381873818838189381903819138192381933819438195381963819738198381993820038201382023820338204382053820638207382083820938210382113821238213382143821538216382173821838219382203822138222382233822438225382263822738228382293823038231382323823338234382353823638237382383823938240382413824238243382443824538246382473824838249382503825138252382533825438255382563825738258382593826038261382623826338264382653826638267382683826938270382713827238273382743827538276382773827838279382803828138282382833828438285382863828738288382893829038291382923829338294382953829638297382983829938300383013830238303383043830538306383073830838309383103831138312383133831438315383163831738318383193832038321383223832338324383253832638327383283832938330383313833238333383343833538336383373833838339383403834138342383433834438345383463834738348383493835038351383523835338354383553835638357383583835938360383613836238363383643836538366383673836838369383703837138372383733837438375383763837738378383793838038381383823838338384383853838638387383883838938390383913839238393383943839538396383973839838399384003840138402384033840438405384063840738408384093841038411384123841338414384153841638417384183841938420384213842238423384243842538426384273842838429384303843138432384333843438435384363843738438384393844038441384423844338444384453844638447384483844938450384513845238453384543845538456384573845838459384603846138462384633846438465384663846738468384693847038471384723847338474384753847638477384783847938480384813848238483384843848538486384873848838489384903849138492384933849438495384963849738498384993850038501385023850338504385053850638507385083850938510385113851238513385143851538516385173851838519385203852138522385233852438525385263852738528385293853038531385323853338534385353853638537385383853938540385413854238543385443854538546385473854838549385503855138552385533855438555385563855738558385593856038561385623856338564385653856638567385683856938570385713857238573385743857538576385773857838579385803858138582385833858438585385863858738588385893859038591385923859338594385953859638597385983859938600386013860238603386043860538606386073860838609386103861138612386133861438615386163861738618386193862038621386223862338624386253862638627386283862938630386313863238633386343863538636386373863838639386403864138642386433864438645386463864738648386493865038651386523865338654386553865638657386583865938660386613866238663386643866538666386673866838669386703867138672386733867438675386763867738678386793868038681386823868338684386853868638687386883868938690386913869238693386943869538696386973869838699387003870138702387033870438705387063870738708387093871038711387123871338714387153871638717387183871938720387213872238723387243872538726387273872838729387303873138732387333873438735387363873738738387393874038741387423874338744387453874638747387483874938750387513875238753387543875538756387573875838759387603876138762387633876438765387663876738768387693877038771387723877338774387753877638777387783877938780387813878238783387843878538786387873878838789387903879138792387933879438795387963879738798387993880038801388023880338804388053880638807388083880938810388113881238813388143881538816388173881838819388203882138822388233882438825388263882738828388293883038831388323883338834388353883638837388383883938840388413884238843388443884538846388473884838849388503885138852388533885438855388563885738858388593886038861388623886338864388653886638867388683886938870388713887238873388743887538876388773887838879388803888138882388833888438885388863888738888388893889038891388923889338894388953889638897388983889938900389013890238903389043890538906389073890838909389103891138912389133891438915389163891738918389193892038921389223892338924389253892638927389283892938930389313893238933389343893538936389373893838939389403894138942389433894438945389463894738948389493895038951389523895338954389553895638957389583895938960389613896238963389643896538966389673896838969389703897138972389733897438975389763897738978389793898038981389823898338984389853898638987389883898938990389913899238993389943899538996389973899838999390003900139002390033900439005390063900739008390093901039011390123901339014390153901639017390183901939020390213902239023390243902539026390273902839029390303903139032390333903439035390363903739038390393904039041390423904339044390453904639047390483904939050390513905239053390543905539056390573905839059390603906139062390633906439065390663906739068390693907039071390723907339074390753907639077390783907939080390813908239083390843908539086390873908839089390903909139092390933909439095390963909739098390993910039101391023910339104391053910639107391083910939110391113911239113391143911539116391173911839119391203912139122391233912439125391263912739128391293913039131391323913339134391353913639137391383913939140391413914239143391443914539146391473914839149391503915139152391533915439155391563915739158391593916039161391623916339164391653916639167391683916939170391713917239173391743917539176391773917839179391803918139182391833918439185391863918739188391893919039191391923919339194391953919639197391983919939200392013920239203392043920539206392073920839209392103921139212392133921439215392163921739218392193922039221392223922339224392253922639227392283922939230392313923239233392343923539236392373923839239392403924139242392433924439245392463924739248392493925039251392523925339254392553925639257392583925939260392613926239263392643926539266392673926839269392703927139272392733927439275392763927739278392793928039281392823928339284392853928639287392883928939290392913929239293392943929539296392973929839299393003930139302393033930439305393063930739308393093931039311393123931339314393153931639317393183931939320393213932239323393243932539326393273932839329393303933139332393333933439335393363933739338393393934039341393423934339344393453934639347393483934939350393513935239353393543935539356393573935839359393603936139362393633936439365393663936739368393693937039371393723937339374393753937639377393783937939380393813938239383393843938539386393873938839389393903939139392393933939439395393963939739398393993940039401394023940339404394053940639407394083940939410394113941239413394143941539416394173941839419394203942139422394233942439425394263942739428394293943039431394323943339434394353943639437394383943939440394413944239443394443944539446394473944839449394503945139452394533945439455394563945739458394593946039461394623946339464394653946639467394683946939470394713947239473394743947539476394773947839479394803948139482394833948439485394863948739488394893949039491394923949339494394953949639497394983949939500395013950239503395043950539506395073950839509395103951139512395133951439515395163951739518395193952039521395223952339524395253952639527395283952939530395313953239533395343953539536395373953839539395403954139542395433954439545395463954739548395493955039551395523955339554395553955639557395583955939560395613956239563395643956539566395673956839569395703957139572395733957439575395763957739578395793958039581395823958339584395853958639587395883958939590395913959239593395943959539596395973959839599396003960139602396033960439605396063960739608396093961039611396123961339614396153961639617396183961939620396213962239623396243962539626396273962839629396303963139632396333963439635396363963739638396393964039641396423964339644396453964639647396483964939650396513965239653396543965539656396573965839659396603966139662396633966439665396663966739668396693967039671396723967339674396753967639677396783967939680396813968239683396843968539686396873968839689396903969139692396933969439695396963969739698396993970039701397023970339704397053970639707397083970939710397113971239713397143971539716397173971839719397203972139722397233972439725397263972739728397293973039731397323973339734397353973639737397383973939740397413974239743397443974539746397473974839749397503975139752397533975439755397563975739758397593976039761397623976339764397653976639767397683976939770397713977239773397743977539776397773977839779397803978139782397833978439785397863978739788397893979039791397923979339794397953979639797397983979939800398013980239803398043980539806398073980839809398103981139812398133981439815398163981739818398193982039821398223982339824398253982639827398283982939830398313983239833398343983539836398373983839839398403984139842398433984439845398463984739848398493985039851398523985339854398553985639857398583985939860398613986239863398643986539866398673986839869398703987139872398733987439875398763987739878398793988039881398823988339884398853988639887398883988939890398913989239893398943989539896398973989839899399003990139902399033990439905399063990739908399093991039911399123991339914399153991639917399183991939920399213992239923399243992539926399273992839929399303993139932399333993439935399363993739938399393994039941399423994339944399453994639947399483994939950399513995239953399543995539956399573995839959399603996139962399633996439965399663996739968399693997039971399723997339974399753997639977399783997939980399813998239983399843998539986399873998839989399903999139992399933999439995399963999739998399994000040001400024000340004400054000640007400084000940010400114001240013400144001540016400174001840019400204002140022400234002440025400264002740028400294003040031400324003340034400354003640037400384003940040400414004240043400444004540046400474004840049400504005140052400534005440055400564005740058400594006040061400624006340064400654006640067400684006940070400714007240073400744007540076400774007840079400804008140082400834008440085400864008740088400894009040091400924009340094400954009640097400984009940100401014010240103401044010540106401074010840109401104011140112401134011440115401164011740118401194012040121401224012340124401254012640127401284012940130401314013240133401344013540136401374013840139401404014140142401434014440145401464014740148401494015040151401524015340154401554015640157401584015940160401614016240163401644016540166401674016840169401704017140172401734017440175401764017740178401794018040181401824018340184401854018640187401884018940190401914019240193401944019540196401974019840199402004020140202402034020440205402064020740208402094021040211402124021340214402154021640217402184021940220402214022240223402244022540226402274022840229402304023140232402334023440235402364023740238402394024040241402424024340244402454024640247402484024940250402514025240253402544025540256402574025840259402604026140262402634026440265402664026740268402694027040271402724027340274402754027640277402784027940280402814028240283402844028540286402874028840289402904029140292402934029440295402964029740298402994030040301403024030340304403054030640307403084030940310403114031240313403144031540316403174031840319403204032140322403234032440325403264032740328403294033040331403324033340334403354033640337403384033940340403414034240343403444034540346403474034840349403504035140352403534035440355403564035740358403594036040361403624036340364403654036640367403684036940370403714037240373403744037540376403774037840379403804038140382403834038440385403864038740388403894039040391403924039340394403954039640397403984039940400404014040240403404044040540406404074040840409404104041140412404134041440415404164041740418404194042040421404224042340424404254042640427404284042940430404314043240433404344043540436404374043840439404404044140442404434044440445404464044740448404494045040451404524045340454404554045640457404584045940460404614046240463404644046540466404674046840469404704047140472404734047440475404764047740478404794048040481404824048340484404854048640487404884048940490404914049240493404944049540496404974049840499405004050140502405034050440505405064050740508405094051040511405124051340514405154051640517405184051940520405214052240523405244052540526405274052840529405304053140532405334053440535405364053740538405394054040541405424054340544405454054640547405484054940550405514055240553405544055540556405574055840559405604056140562405634056440565405664056740568405694057040571405724057340574405754057640577405784057940580405814058240583405844058540586405874058840589405904059140592405934059440595405964059740598405994060040601406024060340604406054060640607406084060940610406114061240613406144061540616406174061840619406204062140622406234062440625406264062740628406294063040631406324063340634406354063640637406384063940640406414064240643406444064540646406474064840649406504065140652406534065440655406564065740658406594066040661406624066340664406654066640667406684066940670406714067240673406744067540676406774067840679406804068140682406834068440685406864068740688406894069040691406924069340694406954069640697406984069940700407014070240703407044070540706407074070840709407104071140712407134071440715407164071740718407194072040721407224072340724407254072640727407284072940730407314073240733407344073540736407374073840739407404074140742407434074440745407464074740748407494075040751407524075340754407554075640757407584075940760407614076240763407644076540766407674076840769407704077140772407734077440775407764077740778407794078040781407824078340784407854078640787407884078940790407914079240793407944079540796407974079840799408004080140802408034080440805408064080740808408094081040811408124081340814408154081640817408184081940820408214082240823408244082540826408274082840829408304083140832408334083440835408364083740838408394084040841408424084340844408454084640847408484084940850408514085240853408544085540856408574085840859408604086140862408634086440865408664086740868408694087040871408724087340874408754087640877408784087940880408814088240883408844088540886408874088840889408904089140892408934089440895408964089740898408994090040901409024090340904409054090640907409084090940910409114091240913409144091540916409174091840919409204092140922409234092440925409264092740928409294093040931409324093340934409354093640937409384093940940409414094240943409444094540946409474094840949409504095140952409534095440955409564095740958409594096040961409624096340964409654096640967409684096940970409714097240973409744097540976409774097840979409804098140982409834098440985409864098740988409894099040991409924099340994409954099640997409984099941000410014100241003410044100541006410074100841009410104101141012410134101441015410164101741018410194102041021410224102341024410254102641027410284102941030410314103241033410344103541036410374103841039410404104141042410434104441045410464104741048410494105041051410524105341054410554105641057410584105941060410614106241063410644106541066410674106841069410704107141072410734107441075410764107741078410794108041081410824108341084410854108641087410884108941090410914109241093410944109541096410974109841099411004110141102411034110441105411064110741108411094111041111411124111341114411154111641117411184111941120411214112241123411244112541126411274112841129411304113141132411334113441135411364113741138411394114041141411424114341144411454114641147411484114941150411514115241153411544115541156411574115841159411604116141162411634116441165411664116741168411694117041171411724117341174411754117641177411784117941180411814118241183411844118541186411874118841189411904119141192411934119441195411964119741198411994120041201412024120341204412054120641207412084120941210412114121241213412144121541216412174121841219412204122141222412234122441225412264122741228412294123041231412324123341234412354123641237412384123941240412414124241243412444124541246412474124841249412504125141252412534125441255412564125741258412594126041261412624126341264412654126641267412684126941270412714127241273412744127541276412774127841279412804128141282412834128441285412864128741288412894129041291412924129341294412954129641297412984129941300413014130241303413044130541306413074130841309413104131141312413134131441315413164131741318413194132041321413224132341324413254132641327413284132941330413314133241333413344133541336413374133841339413404134141342413434134441345413464134741348413494135041351413524135341354413554135641357413584135941360413614136241363413644136541366413674136841369413704137141372413734137441375413764137741378413794138041381413824138341384413854138641387413884138941390413914139241393413944139541396413974139841399414004140141402414034140441405414064140741408414094141041411414124141341414414154141641417414184141941420414214142241423414244142541426414274142841429414304143141432414334143441435414364143741438414394144041441414424144341444414454144641447414484144941450414514145241453414544145541456414574145841459414604146141462414634146441465414664146741468414694147041471414724147341474414754147641477414784147941480414814148241483414844148541486414874148841489414904149141492414934149441495414964149741498414994150041501415024150341504415054150641507415084150941510415114151241513415144151541516415174151841519415204152141522415234152441525415264152741528415294153041531415324153341534415354153641537415384153941540415414154241543415444154541546415474154841549415504155141552415534155441555415564155741558415594156041561415624156341564415654156641567415684156941570415714157241573415744157541576415774157841579415804158141582415834158441585415864158741588415894159041591415924159341594415954159641597415984159941600416014160241603416044160541606416074160841609416104161141612416134161441615416164161741618416194162041621416224162341624416254162641627416284162941630416314163241633416344163541636416374163841639416404164141642416434164441645416464164741648416494165041651416524165341654416554165641657416584165941660416614166241663416644166541666416674166841669416704167141672416734167441675416764167741678416794168041681416824168341684416854168641687416884168941690416914169241693416944169541696416974169841699417004170141702417034170441705417064170741708417094171041711417124171341714417154171641717417184171941720417214172241723417244172541726417274172841729417304173141732417334173441735417364173741738417394174041741417424174341744417454174641747417484174941750417514175241753417544175541756417574175841759417604176141762417634176441765417664176741768417694177041771417724177341774417754177641777417784177941780417814178241783417844178541786417874178841789417904179141792417934179441795417964179741798417994180041801418024180341804418054180641807418084180941810418114181241813418144181541816418174181841819418204182141822418234182441825418264182741828418294183041831418324183341834418354183641837418384183941840418414184241843418444184541846418474184841849418504185141852418534185441855418564185741858418594186041861418624186341864418654186641867418684186941870418714187241873418744187541876418774187841879418804188141882418834188441885418864188741888418894189041891418924189341894418954189641897418984189941900419014190241903419044190541906419074190841909419104191141912419134191441915419164191741918419194192041921419224192341924419254192641927419284192941930419314193241933419344193541936419374193841939419404194141942419434194441945419464194741948419494195041951419524195341954419554195641957419584195941960419614196241963419644196541966419674196841969419704197141972419734197441975419764197741978419794198041981419824198341984419854198641987419884198941990419914199241993419944199541996419974199841999420004200142002420034200442005420064200742008420094201042011420124201342014420154201642017420184201942020420214202242023420244202542026420274202842029420304203142032420334203442035420364203742038420394204042041420424204342044420454204642047420484204942050420514205242053420544205542056420574205842059420604206142062420634206442065420664206742068420694207042071420724207342074420754207642077420784207942080420814208242083420844208542086420874208842089420904209142092420934209442095420964209742098420994210042101421024210342104421054210642107421084210942110421114211242113421144211542116421174211842119421204212142122421234212442125421264212742128421294213042131421324213342134421354213642137421384213942140421414214242143421444214542146421474214842149421504215142152421534215442155421564215742158421594216042161421624216342164421654216642167421684216942170421714217242173421744217542176421774217842179421804218142182421834218442185421864218742188421894219042191421924219342194421954219642197421984219942200422014220242203422044220542206422074220842209422104221142212422134221442215422164221742218422194222042221422224222342224422254222642227422284222942230422314223242233422344223542236422374223842239422404224142242422434224442245422464224742248422494225042251422524225342254422554225642257422584225942260422614226242263422644226542266422674226842269422704227142272422734227442275422764227742278422794228042281422824228342284422854228642287422884228942290422914229242293422944229542296422974229842299423004230142302423034230442305423064230742308423094231042311423124231342314423154231642317423184231942320423214232242323423244232542326423274232842329423304233142332423334233442335423364233742338423394234042341423424234342344423454234642347423484234942350423514235242353423544235542356423574235842359423604236142362423634236442365423664236742368423694237042371423724237342374423754237642377423784237942380423814238242383423844238542386423874238842389423904239142392423934239442395423964239742398423994240042401424024240342404424054240642407424084240942410424114241242413424144241542416424174241842419424204242142422424234242442425424264242742428424294243042431424324243342434424354243642437424384243942440424414244242443424444244542446424474244842449424504245142452424534245442455424564245742458424594246042461424624246342464424654246642467424684246942470424714247242473424744247542476424774247842479424804248142482424834248442485424864248742488424894249042491424924249342494424954249642497424984249942500425014250242503425044250542506425074250842509425104251142512425134251442515425164251742518425194252042521425224252342524425254252642527425284252942530425314253242533425344253542536425374253842539425404254142542425434254442545425464254742548425494255042551425524255342554425554255642557425584255942560425614256242563425644256542566425674256842569425704257142572425734257442575425764257742578425794258042581425824258342584425854258642587425884258942590425914259242593425944259542596425974259842599426004260142602426034260442605426064260742608426094261042611426124261342614426154261642617426184261942620426214262242623426244262542626426274262842629426304263142632426334263442635426364263742638426394264042641426424264342644426454264642647426484264942650426514265242653426544265542656426574265842659426604266142662426634266442665426664266742668426694267042671426724267342674426754267642677426784267942680426814268242683426844268542686426874268842689426904269142692426934269442695426964269742698426994270042701427024270342704427054270642707427084270942710427114271242713427144271542716427174271842719427204272142722427234272442725427264272742728427294273042731427324273342734427354273642737427384273942740427414274242743427444274542746427474274842749427504275142752427534275442755427564275742758427594276042761427624276342764427654276642767427684276942770427714277242773427744277542776427774277842779427804278142782427834278442785427864278742788427894279042791427924279342794427954279642797427984279942800428014280242803428044280542806428074280842809428104281142812428134281442815428164281742818428194282042821428224282342824428254282642827428284282942830428314283242833428344283542836428374283842839428404284142842428434284442845428464284742848428494285042851428524285342854428554285642857428584285942860428614286242863428644286542866428674286842869428704287142872428734287442875428764287742878428794288042881428824288342884428854288642887428884288942890428914289242893428944289542896428974289842899429004290142902429034290442905429064290742908429094291042911429124291342914429154291642917429184291942920429214292242923429244292542926429274292842929429304293142932429334293442935429364293742938429394294042941429424294342944429454294642947429484294942950429514295242953429544295542956429574295842959429604296142962429634296442965429664296742968429694297042971429724297342974429754297642977429784297942980429814298242983429844298542986429874298842989429904299142992429934299442995429964299742998429994300043001430024300343004430054300643007430084300943010430114301243013430144301543016430174301843019430204302143022430234302443025430264302743028430294303043031430324303343034430354303643037430384303943040430414304243043430444304543046430474304843049430504305143052430534305443055430564305743058430594306043061430624306343064430654306643067430684306943070430714307243073430744307543076430774307843079430804308143082430834308443085430864308743088430894309043091430924309343094430954309643097430984309943100431014310243103431044310543106431074310843109431104311143112431134311443115431164311743118431194312043121431224312343124431254312643127431284312943130431314313243133431344313543136431374313843139431404314143142431434314443145431464314743148431494315043151431524315343154431554315643157431584315943160431614316243163431644316543166431674316843169431704317143172431734317443175431764317743178431794318043181431824318343184431854318643187431884318943190431914319243193431944319543196431974319843199432004320143202432034320443205432064320743208432094321043211432124321343214432154321643217432184321943220432214322243223432244322543226432274322843229432304323143232432334323443235432364323743238432394324043241432424324343244432454324643247432484324943250432514325243253432544325543256432574325843259432604326143262432634326443265432664326743268432694327043271432724327343274432754327643277432784327943280432814328243283432844328543286432874328843289432904329143292432934329443295432964329743298432994330043301433024330343304433054330643307433084330943310433114331243313433144331543316433174331843319433204332143322433234332443325433264332743328433294333043331433324333343334433354333643337433384333943340433414334243343433444334543346433474334843349433504335143352433534335443355433564335743358433594336043361433624336343364433654336643367433684336943370433714337243373433744337543376433774337843379433804338143382433834338443385433864338743388433894339043391433924339343394433954339643397433984339943400434014340243403434044340543406434074340843409434104341143412434134341443415434164341743418434194342043421434224342343424434254342643427434284342943430434314343243433434344343543436434374343843439434404344143442434434344443445434464344743448434494345043451434524345343454434554345643457434584345943460434614346243463434644346543466434674346843469434704347143472434734347443475434764347743478434794348043481434824348343484434854348643487434884348943490434914349243493434944349543496434974349843499435004350143502435034350443505435064350743508435094351043511435124351343514435154351643517435184351943520435214352243523435244352543526435274352843529435304353143532435334353443535435364353743538435394354043541435424354343544435454354643547435484354943550435514355243553435544355543556435574355843559435604356143562435634356443565435664356743568435694357043571435724357343574435754357643577435784357943580435814358243583435844358543586435874358843589435904359143592435934359443595435964359743598435994360043601436024360343604436054360643607436084360943610436114361243613436144361543616436174361843619436204362143622436234362443625436264362743628436294363043631436324363343634436354363643637436384363943640436414364243643436444364543646436474364843649436504365143652436534365443655436564365743658436594366043661436624366343664436654366643667436684366943670436714367243673436744367543676436774367843679436804368143682436834368443685436864368743688436894369043691436924369343694436954369643697436984369943700437014370243703437044370543706437074370843709437104371143712437134371443715437164371743718437194372043721437224372343724437254372643727437284372943730437314373243733437344373543736437374373843739437404374143742437434374443745437464374743748437494375043751437524375343754437554375643757437584375943760437614376243763437644376543766437674376843769437704377143772437734377443775437764377743778437794378043781437824378343784437854378643787437884378943790437914379243793437944379543796437974379843799438004380143802438034380443805438064380743808438094381043811438124381343814438154381643817438184381943820438214382243823438244382543826438274382843829438304383143832438334383443835438364383743838438394384043841438424384343844438454384643847438484384943850438514385243853438544385543856438574385843859438604386143862438634386443865438664386743868438694387043871438724387343874438754387643877438784387943880438814388243883438844388543886438874388843889438904389143892438934389443895438964389743898438994390043901439024390343904439054390643907439084390943910439114391243913439144391543916439174391843919439204392143922439234392443925439264392743928439294393043931439324393343934439354393643937439384393943940439414394243943439444394543946439474394843949439504395143952439534395443955439564395743958439594396043961439624396343964439654396643967439684396943970439714397243973439744397543976439774397843979439804398143982439834398443985439864398743988439894399043991439924399343994439954399643997439984399944000440014400244003440044400544006440074400844009440104401144012440134401444015440164401744018440194402044021440224402344024440254402644027440284402944030440314403244033440344403544036440374403844039440404404144042440434404444045440464404744048440494405044051440524405344054440554405644057440584405944060440614406244063440644406544066440674406844069440704407144072440734407444075440764407744078440794408044081440824408344084440854408644087440884408944090440914409244093440944409544096440974409844099441004410144102441034410444105441064410744108441094411044111441124411344114441154411644117441184411944120441214412244123441244412544126441274412844129441304413144132441334413444135441364413744138441394414044141441424414344144441454414644147441484414944150441514415244153441544415544156441574415844159441604416144162441634416444165441664416744168441694417044171441724417344174441754417644177441784417944180441814418244183441844418544186441874418844189441904419144192441934419444195441964419744198441994420044201442024420344204442054420644207442084420944210442114421244213442144421544216442174421844219442204422144222442234422444225442264422744228442294423044231442324423344234442354423644237442384423944240442414424244243442444424544246442474424844249442504425144252442534425444255442564425744258442594426044261442624426344264442654426644267442684426944270442714427244273442744427544276442774427844279442804428144282442834428444285442864428744288442894429044291442924429344294442954429644297442984429944300443014430244303443044430544306443074430844309443104431144312443134431444315443164431744318443194432044321443224432344324443254432644327443284432944330443314433244333443344433544336443374433844339443404434144342443434434444345443464434744348443494435044351443524435344354443554435644357443584435944360443614436244363443644436544366443674436844369443704437144372443734437444375443764437744378443794438044381443824438344384443854438644387443884438944390443914439244393443944439544396443974439844399444004440144402444034440444405444064440744408444094441044411444124441344414444154441644417444184441944420444214442244423444244442544426444274442844429444304443144432444334443444435
  1. /* test.c
  2. *
  3. * Copyright (C) 2006-2023 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL.
  6. *
  7. * wolfSSL is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * wolfSSL is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  20. */
  21. #ifdef HAVE_CONFIG_H
  22. #include <config.h>
  23. #endif
  24. #ifndef WOLFSSL_USER_SETTINGS
  25. #include <wolfssl/options.h>
  26. #endif
  27. #include <wolfssl/wolfcrypt/settings.h>
  28. #ifndef NO_CRYPT_TEST
  29. #include <wolfssl/version.h>
  30. #include <wolfssl/wolfcrypt/types.h>
  31. #include <wolfssl/wolfcrypt/wc_port.h>
  32. #include <wolfssl/wolfcrypt/mem_track.h>
  33. #if defined(HAVE_WOLFCRYPT_TEST_OPTIONS)
  34. #include <wolfssl/ssl.h>
  35. #define err_sys err_sys_remap /* remap err_sys */
  36. #include <wolfssl/test.h>
  37. #undef err_sys
  38. #endif
  39. #if defined(WC_ECC_NONBLOCK) && defined(WOLFSSL_PUBLIC_MP) && \
  40. defined(HAVE_ECC_SIGN) && defined(HAVE_ECC_VERIFY)
  41. #include <stdint.h>
  42. #endif
  43. #ifdef HAVE_STACK_SIZE_VERBOSE
  44. #ifdef WOLFSSL_TEST_MAX_RELATIVE_STACK_BYTES
  45. static ssize_t max_relative_stack = WOLFSSL_TEST_MAX_RELATIVE_STACK_BYTES;
  46. #else
  47. static ssize_t max_relative_stack = -1;
  48. #endif
  49. #endif
  50. #ifdef WOLFSSL_TRACK_MEMORY_VERBOSE
  51. #ifdef WOLFSSL_TEST_MAX_RELATIVE_HEAP_ALLOCS
  52. static ssize_t max_relative_heap_allocs = WOLFSSL_TEST_MAX_RELATIVE_HEAP_ALLOCS;
  53. #else
  54. static ssize_t max_relative_heap_allocs = -1;
  55. #endif
  56. #ifdef WOLFSSL_TEST_MAX_RELATIVE_HEAP_BYTES
  57. static ssize_t max_relative_heap_bytes = WOLFSSL_TEST_MAX_RELATIVE_HEAP_BYTES;
  58. #else
  59. static ssize_t max_relative_heap_bytes = -1;
  60. #endif
  61. #define PRINT_HEAP_CHECKPOINT() { \
  62. const ssize_t _rha = wolfCrypt_heap_peakAllocs_checkpoint() - heap_baselineAllocs; \
  63. const ssize_t _rhb = wolfCrypt_heap_peakBytes_checkpoint() - heap_baselineBytes; \
  64. printf(" relative heap peak usage: %ld alloc%s, %ld bytes\n", \
  65. (long int)_rha, \
  66. _rha == 1 ? "" : "s", \
  67. (long int)_rhb); \
  68. if ((max_relative_heap_allocs > 0) && (_rha > max_relative_heap_allocs)) \
  69. return err_sys("heap allocs exceed designated max.", -1); \
  70. if ((max_relative_heap_bytes > 0) && (_rhb > max_relative_heap_bytes)) \
  71. return err_sys("heap bytes exceed designated max.", -1); \
  72. heap_baselineAllocs = wolfCrypt_heap_peakAllocs_checkpoint(); \
  73. heap_baselineBytes = wolfCrypt_heap_peakBytes_checkpoint(); \
  74. }
  75. #else
  76. #define PRINT_HEAP_CHECKPOINT()
  77. #endif
  78. #ifdef USE_FLAT_TEST_H
  79. #ifdef HAVE_CONFIG_H
  80. #include "test_paths.h"
  81. #endif
  82. #include "test.h"
  83. #else
  84. #ifdef HAVE_CONFIG_H
  85. #include "wolfcrypt/test/test_paths.h"
  86. #endif
  87. #include "wolfcrypt/test/test.h"
  88. #endif
  89. /* printf mappings */
  90. #ifndef WOLFSSL_LOG_PRINTF
  91. #if defined(FREESCALE_MQX) || defined(FREESCALE_KSDK_MQX)
  92. #include <mqx.h>
  93. #include <stdlib.h>
  94. /* see wc_port.h for fio.h and nio.h includes */
  95. #elif defined(FREESCALE_KSDK_BM)
  96. #include "fsl_debug_console.h"
  97. #undef printf
  98. #define printf PRINTF
  99. #elif defined(WOLFSSL_APACHE_MYNEWT)
  100. #include <assert.h>
  101. #include <string.h>
  102. #include "sysinit/sysinit.h"
  103. #include "os/os.h"
  104. #ifdef ARCH_sim
  105. #include "mcu/mcu_sim.h"
  106. #endif
  107. #include "os/os_time.h"
  108. #elif defined(WOLFSSL_ESPIDF)
  109. #include <time.h>
  110. #include <sys/time.h>
  111. #include <esp_log.h>
  112. #elif defined(WOLFSSL_ZEPHYR)
  113. #include <stdio.h>
  114. #define printf printk
  115. #elif defined(MICRIUM)
  116. #include <os.h>
  117. #if (OS_VERSION < 50000)
  118. #include <bsp_ser.h>
  119. void BSP_Ser_Printf (CPU_CHAR* format, ...);
  120. #undef printf
  121. #define printf BSP_Ser_Printf
  122. #else
  123. #include <stdio.h>
  124. #endif
  125. #elif defined(WOLFSSL_PB)
  126. #include <stdarg.h>
  127. int wolfssl_pb_print(const char*, ...);
  128. #undef printf
  129. #define printf wolfssl_pb_print
  130. #elif defined(WOLFSSL_TELIT_M2MB)
  131. #include "wolfssl/wolfcrypt/wc_port.h" /* for m2mb headers */
  132. #include "m2m_log.h" /* for M2M_LOG_INFO - not standard API */
  133. /* remap printf */
  134. #undef printf
  135. #define printf M2M_LOG_INFO
  136. /* OS requires occasional sleep() */
  137. #ifndef TEST_SLEEP_MS
  138. #define TEST_SLEEP_MS 50
  139. #endif
  140. #define TEST_SLEEP() m2mb_os_taskSleep(M2MB_OS_MS2TICKS(TEST_SLEEP_MS))
  141. /* don't use file system for these tests, since ./certs dir isn't loaded */
  142. #undef NO_FILESYSTEM
  143. #define NO_FILESYSTEM
  144. #elif defined(THREADX) && !defined(WOLFSSL_WICED) && \
  145. !defined(THREADX_NO_DC_PRINTF)
  146. #ifndef NETOS
  147. /* since just testing, use THREADX log printf instead (NETOS prototypes
  148. * this elsewhere) */
  149. int dc_log_printf(char*, ...);
  150. #endif
  151. #undef printf
  152. #define printf dc_log_printf
  153. #elif defined(ANDROID)
  154. #ifdef XMALLOC_USER
  155. #include <stdlib.h> /* we're using malloc / free direct here */
  156. #endif
  157. #ifndef STRING_USER
  158. #include <stdio.h>
  159. #endif
  160. #include <android/log.h>
  161. #ifdef ANDROID_V454 /* See fips/android/wolfCrypt_v454_android */
  162. #ifndef NO_FILESYSTEM
  163. #define NO_FILESYSTEM /* Turn off tests that want to call SaveDerAndPem() */
  164. #endif
  165. #else
  166. #define printf(...) \
  167. __android_log_print(ANDROID_LOG_DEBUG, "[WOLFCRYPT]", __VA_ARGS__)
  168. #define fprintf(fp, ...) \
  169. __android_log_print(ANDROID_LOG_DEBUG, "[WOLFCRYPT]", __VA_ARGS__)
  170. #endif
  171. #elif defined(WOLFSSL_DEOS)
  172. #include <printx.h>
  173. #undef printf
  174. #define printf printx
  175. #else
  176. #ifdef XMALLOC_USER
  177. #include <stdlib.h> /* we're using malloc / free direct here */
  178. #endif
  179. #if !defined(STRING_USER) && !defined(WOLFSSL_LINUXKM)
  180. #include <stdio.h>
  181. #endif
  182. #if defined(WOLFSSL_LINUXKM) && !defined(WOLFSSL_LINUXKM_VERBOSE_DEBUG)
  183. #undef printf
  184. #define printf(...) ({})
  185. #endif
  186. /* enable way for customer to override test/bench printf */
  187. #ifdef XPRINTF
  188. #undef printf
  189. #define printf XPRINTF
  190. #elif !defined(printf)
  191. /* arrange for printf() to flush after every message -- this assures
  192. * redirected output (to a log file) records progress right up to the
  193. * moment of a crash/abort(); otherwise anything queued in stdout would
  194. * be lost.
  195. */
  196. #define printf(...) ( printf(__VA_ARGS__), fflush(stdout) )
  197. #endif
  198. #endif
  199. #endif /* !WOLFSSL_LOG_PRINTF */
  200. #include <wolfssl/wolfcrypt/memory.h>
  201. #include <wolfssl/wolfcrypt/wc_port.h>
  202. #include <wolfssl/wolfcrypt/logging.h>
  203. #include <wolfssl/wolfcrypt/types.h>
  204. #include <wolfssl/wolfcrypt/asn.h>
  205. #include <wolfssl/wolfcrypt/md2.h>
  206. #include <wolfssl/wolfcrypt/md5.h>
  207. #include <wolfssl/wolfcrypt/md4.h>
  208. #include <wolfssl/wolfcrypt/sha.h>
  209. #include <wolfssl/wolfcrypt/sha256.h>
  210. #include <wolfssl/wolfcrypt/sha512.h>
  211. #include <wolfssl/wolfcrypt/rc2.h>
  212. #include <wolfssl/wolfcrypt/arc4.h>
  213. #if defined(WC_NO_RNG)
  214. #include <wolfssl/wolfcrypt/integer.h>
  215. #else
  216. #include <wolfssl/wolfcrypt/random.h>
  217. #endif
  218. #include <wolfssl/wolfcrypt/coding.h>
  219. #include <wolfssl/wolfcrypt/signature.h>
  220. #include <wolfssl/wolfcrypt/rsa.h>
  221. #include <wolfssl/wolfcrypt/des3.h>
  222. #include <wolfssl/wolfcrypt/aes.h>
  223. #include <wolfssl/wolfcrypt/wc_encrypt.h>
  224. #include <wolfssl/wolfcrypt/cmac.h>
  225. #include <wolfssl/wolfcrypt/siphash.h>
  226. #include <wolfssl/wolfcrypt/poly1305.h>
  227. #include <wolfssl/wolfcrypt/camellia.h>
  228. #include <wolfssl/wolfcrypt/hmac.h>
  229. #include <wolfssl/wolfcrypt/kdf.h>
  230. #include <wolfssl/wolfcrypt/dh.h>
  231. #include <wolfssl/wolfcrypt/dsa.h>
  232. #include <wolfssl/wolfcrypt/srp.h>
  233. #include <wolfssl/wolfcrypt/chacha.h>
  234. #include <wolfssl/wolfcrypt/chacha20_poly1305.h>
  235. #include <wolfssl/wolfcrypt/pwdbased.h>
  236. #include <wolfssl/wolfcrypt/ripemd.h>
  237. #include <wolfssl/wolfcrypt/error-crypt.h>
  238. #ifdef HAVE_ECC
  239. #include <wolfssl/wolfcrypt/ecc.h>
  240. #endif
  241. #ifdef HAVE_HPKE
  242. #include <wolfssl/wolfcrypt/hpke.h>
  243. #endif
  244. #ifdef HAVE_CURVE25519
  245. #include <wolfssl/wolfcrypt/curve25519.h>
  246. #endif
  247. #ifdef HAVE_ED25519
  248. #include <wolfssl/wolfcrypt/ed25519.h>
  249. #endif
  250. #ifdef HAVE_CURVE448
  251. #include <wolfssl/wolfcrypt/curve448.h>
  252. #endif
  253. #ifdef HAVE_ED448
  254. #include <wolfssl/wolfcrypt/ed448.h>
  255. #endif
  256. #ifdef WOLFSSL_HAVE_KYBER
  257. #include <wolfssl/wolfcrypt/kyber.h>
  258. #ifdef WOLFSSL_WC_KYBER
  259. #include <wolfssl/wolfcrypt/wc_kyber.h>
  260. #endif
  261. #if defined(HAVE_LIBOQS) || defined(HAVE_PQM4)
  262. #include <wolfssl/wolfcrypt/ext_kyber.h>
  263. #endif
  264. #endif
  265. #ifdef WOLFCRYPT_HAVE_ECCSI
  266. #include <wolfssl/wolfcrypt/eccsi.h>
  267. #endif
  268. #ifdef WOLFCRYPT_HAVE_SAKKE
  269. #include <wolfssl/wolfcrypt/sakke.h>
  270. #endif
  271. #if defined(HAVE_BLAKE2) || defined(HAVE_BLAKE2S)
  272. #include <wolfssl/wolfcrypt/blake2.h>
  273. #endif
  274. #ifdef WOLFSSL_SHA3
  275. #include <wolfssl/wolfcrypt/sha3.h>
  276. #endif
  277. #ifdef HAVE_LIBZ
  278. #include <wolfssl/wolfcrypt/compress.h>
  279. #endif
  280. #ifdef HAVE_PKCS7
  281. #include <wolfssl/wolfcrypt/pkcs7.h>
  282. #endif
  283. #ifdef HAVE_FIPS
  284. #include <wolfssl/wolfcrypt/fips_test.h>
  285. #endif
  286. #ifdef HAVE_SELFTEST
  287. #include <wolfssl/wolfcrypt/selftest.h>
  288. #endif
  289. #ifdef WOLFSSL_ASYNC_CRYPT
  290. #include <wolfssl/wolfcrypt/async.h>
  291. #endif
  292. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  293. #include <wolfssl/wolfcrypt/logging.h>
  294. #endif
  295. #ifdef WOLFSSL_CAAM
  296. #include <wolfssl/wolfcrypt/port/caam/wolfcaam.h>
  297. #endif
  298. #ifdef WOLF_CRYPTO_CB
  299. #include <wolfssl/wolfcrypt/cryptocb.h>
  300. #ifdef HAVE_INTEL_QA_SYNC
  301. #include <wolfssl/wolfcrypt/port/intel/quickassist_sync.h>
  302. #endif
  303. #ifdef HAVE_CAVIUM_OCTEON_SYNC
  304. #include <wolfssl/wolfcrypt/port/cavium/cavium_octeon_sync.h>
  305. #endif
  306. #endif
  307. #ifdef _MSC_VER
  308. /* 4996 warning to use MS extensions e.g., strcpy_s instead of strncpy */
  309. #pragma warning(disable: 4996)
  310. #endif
  311. #ifdef OPENSSL_EXTRA
  312. #ifndef WOLFCRYPT_ONLY
  313. #include <wolfssl/openssl/evp.h>
  314. #include <wolfssl/openssl/hmac.h>
  315. #endif
  316. #include <wolfssl/openssl/rand.h>
  317. #include <wolfssl/openssl/aes.h>
  318. #include <wolfssl/openssl/des.h>
  319. #endif
  320. #if defined(NO_FILESYSTEM) || defined(WC_NO_RNG)
  321. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  322. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096)
  323. #define USE_CERT_BUFFERS_2048
  324. #endif
  325. #if !defined(USE_CERT_BUFFERS_256)
  326. #define USE_CERT_BUFFERS_256
  327. #endif
  328. #endif
  329. #if defined(WOLFSSL_CERT_GEN) && (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES))
  330. #define ENABLE_ECC384_CERT_GEN_TEST
  331. #endif
  332. #include <wolfssl/certs_test.h>
  333. #ifdef DEVKITPRO
  334. #include <wiiuse/wpad.h>
  335. #endif
  336. #ifdef WOLFSSL_STATIC_MEMORY
  337. static WOLFSSL_HEAP_HINT* HEAP_HINT;
  338. #else
  339. #define HEAP_HINT NULL
  340. #endif /* WOLFSSL_STATIC_MEMORY */
  341. /* these cases do not have intermediate hashing support */
  342. #if (defined(WOLFSSL_AFALG_XILINX_SHA3) && !defined(WOLFSSL_AFALG_HASH_KEEP)) \
  343. && !defined(WOLFSSL_XILINX_CRYPT) || defined(WOLFSSL_XILINX_CRYPT_VERSAL)
  344. #define NO_INTM_HASH_TEST
  345. #endif
  346. #if defined(WOLFSSL_RENESAS_TSIP) || defined(WOLFSSL_RENESAS_SCEPROTECT) || \
  347. defined(WOLFSSL_SECO_CAAM)
  348. #define HASH_SIZE_LIMIT
  349. #endif
  350. #if defined(WOLFSSL_CERT_GEN) && (!defined(NO_RSA) || defined(HAVE_ECC)) || \
  351. (defined(WOLFSSL_TEST_CERT) && (defined(HAVE_ED25519) || defined(HAVE_ED448)))
  352. static void initDefaultName(void);
  353. #endif
  354. /* for async devices */
  355. #ifdef WOLFSSL_CAAM_DEVID
  356. static int devId = WOLFSSL_CAAM_DEVID;
  357. #else
  358. static int devId = INVALID_DEVID;
  359. #endif
  360. #ifdef HAVE_WNR
  361. const char* wnrConfigFile = "wnr-example.conf";
  362. #endif
  363. #define TEST_STRING "Everyone gets Friday off."
  364. #define TEST_STRING_SZ 25
  365. typedef struct testVector {
  366. const char* input;
  367. const char* output;
  368. size_t inLen;
  369. size_t outLen;
  370. } testVector;
  371. #ifndef WOLFSSL_TEST_SUBROUTINE
  372. #define WOLFSSL_TEST_SUBROUTINE
  373. #endif
  374. PRAGMA_GCC("GCC diagnostic ignored \"-Wunused-function\"")
  375. PRAGMA_CLANG("clang diagnostic ignored \"-Wunused-function\"")
  376. WOLFSSL_TEST_SUBROUTINE int error_test(void);
  377. WOLFSSL_TEST_SUBROUTINE int base64_test(void);
  378. WOLFSSL_TEST_SUBROUTINE int base16_test(void);
  379. WOLFSSL_TEST_SUBROUTINE int asn_test(void);
  380. WOLFSSL_TEST_SUBROUTINE int md2_test(void);
  381. WOLFSSL_TEST_SUBROUTINE int md5_test(void);
  382. WOLFSSL_TEST_SUBROUTINE int md4_test(void);
  383. WOLFSSL_TEST_SUBROUTINE int sha_test(void);
  384. WOLFSSL_TEST_SUBROUTINE int sha224_test(void);
  385. WOLFSSL_TEST_SUBROUTINE int sha256_test(void);
  386. WOLFSSL_TEST_SUBROUTINE int sha512_test(void);
  387. WOLFSSL_TEST_SUBROUTINE int sha384_test(void);
  388. WOLFSSL_TEST_SUBROUTINE int sha3_test(void);
  389. WOLFSSL_TEST_SUBROUTINE int shake128_test(void);
  390. WOLFSSL_TEST_SUBROUTINE int shake256_test(void);
  391. WOLFSSL_TEST_SUBROUTINE int hash_test(void);
  392. WOLFSSL_TEST_SUBROUTINE int hmac_md5_test(void);
  393. WOLFSSL_TEST_SUBROUTINE int hmac_sha_test(void);
  394. WOLFSSL_TEST_SUBROUTINE int hmac_sha224_test(void);
  395. WOLFSSL_TEST_SUBROUTINE int hmac_sha256_test(void);
  396. WOLFSSL_TEST_SUBROUTINE int hmac_sha384_test(void);
  397. WOLFSSL_TEST_SUBROUTINE int hmac_sha512_test(void);
  398. WOLFSSL_TEST_SUBROUTINE int hmac_sha3_test(void);
  399. #if defined(HAVE_HKDF) && !defined(NO_HMAC)
  400. /* hkdf_test has issue with WOLFSSL_TEST_SUBROUTINE set on Xilinx with afalg */
  401. static int hkdf_test(void);
  402. #endif
  403. WOLFSSL_TEST_SUBROUTINE int sshkdf_test(void);
  404. #ifdef WOLFSSL_TLS13
  405. WOLFSSL_TEST_SUBROUTINE int tls13_kdf_test(void);
  406. #endif
  407. WOLFSSL_TEST_SUBROUTINE int x963kdf_test(void);
  408. WOLFSSL_TEST_SUBROUTINE int hpke_test(void);
  409. WOLFSSL_TEST_SUBROUTINE int arc4_test(void);
  410. #ifdef WC_RC2
  411. WOLFSSL_TEST_SUBROUTINE int rc2_test(void);
  412. #endif
  413. WOLFSSL_TEST_SUBROUTINE int chacha_test(void);
  414. WOLFSSL_TEST_SUBROUTINE int XChaCha_test(void);
  415. WOLFSSL_TEST_SUBROUTINE int chacha20_poly1305_aead_test(void);
  416. WOLFSSL_TEST_SUBROUTINE int XChaCha20Poly1305_test(void);
  417. WOLFSSL_TEST_SUBROUTINE int des_test(void);
  418. WOLFSSL_TEST_SUBROUTINE int des3_test(void);
  419. WOLFSSL_TEST_SUBROUTINE int aes_test(void);
  420. WOLFSSL_TEST_SUBROUTINE int aes192_test(void);
  421. WOLFSSL_TEST_SUBROUTINE int aes256_test(void);
  422. WOLFSSL_TEST_SUBROUTINE int aesofb_test(void);
  423. WOLFSSL_TEST_SUBROUTINE int cmac_test(void);
  424. #if defined(WOLFSSL_SIPHASH)
  425. WOLFSSL_TEST_SUBROUTINE int siphash_test(void);
  426. #endif
  427. WOLFSSL_TEST_SUBROUTINE int poly1305_test(void);
  428. WOLFSSL_TEST_SUBROUTINE int aesgcm_test(void);
  429. WOLFSSL_TEST_SUBROUTINE int aesgcm_default_test(void);
  430. WOLFSSL_TEST_SUBROUTINE int gmac_test(void);
  431. WOLFSSL_TEST_SUBROUTINE int aesccm_test(void);
  432. WOLFSSL_TEST_SUBROUTINE int aeskeywrap_test(void);
  433. WOLFSSL_TEST_SUBROUTINE int camellia_test(void);
  434. #ifdef WC_RSA_NO_PADDING
  435. WOLFSSL_TEST_SUBROUTINE int rsa_no_pad_test(void);
  436. #endif
  437. WOLFSSL_TEST_SUBROUTINE int rsa_test(void);
  438. WOLFSSL_TEST_SUBROUTINE int dh_test(void);
  439. WOLFSSL_TEST_SUBROUTINE int dsa_test(void);
  440. WOLFSSL_TEST_SUBROUTINE int srp_test(void);
  441. #ifndef WC_NO_RNG
  442. WOLFSSL_TEST_SUBROUTINE int random_test(void);
  443. #endif /* WC_NO_RNG */
  444. WOLFSSL_TEST_SUBROUTINE int pwdbased_test(void);
  445. WOLFSSL_TEST_SUBROUTINE int ripemd_test(void);
  446. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  447. WOLFSSL_TEST_SUBROUTINE int openssl_test(void); /* test mini api */
  448. WOLFSSL_TEST_SUBROUTINE int openssl_pkey0_test(void);
  449. WOLFSSL_TEST_SUBROUTINE int openssl_pkey1_test(void);
  450. WOLFSSL_TEST_SUBROUTINE int openSSL_evpMD_test(void);
  451. WOLFSSL_TEST_SUBROUTINE int openssl_evpSig_test(void);
  452. #endif
  453. WOLFSSL_TEST_SUBROUTINE int pbkdf1_test(void);
  454. WOLFSSL_TEST_SUBROUTINE int pkcs12_test(void);
  455. WOLFSSL_TEST_SUBROUTINE int pbkdf2_test(void);
  456. WOLFSSL_TEST_SUBROUTINE int scrypt_test(void);
  457. #ifdef HAVE_ECC
  458. WOLFSSL_TEST_SUBROUTINE int ecc_test(void);
  459. #if defined(HAVE_ECC_ENCRYPT) && defined(HAVE_AES_CBC) && \
  460. (defined(WOLFSSL_AES_128) || defined(WOLFSSL_AES_256))
  461. WOLFSSL_TEST_SUBROUTINE int ecc_encrypt_test(void);
  462. #endif
  463. #if defined(USE_CERT_BUFFERS_256) && !defined(WOLFSSL_ATECC508A) && \
  464. !defined(WOLFSSL_ATECC608A) && !defined(NO_ECC256) && \
  465. defined(HAVE_ECC_VERIFY) && defined(HAVE_ECC_SIGN) && \
  466. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  467. /* skip for ATECC508/608A, cannot import private key buffers */
  468. WOLFSSL_TEST_SUBROUTINE int ecc_test_buffers(void);
  469. #endif
  470. #endif
  471. #ifdef HAVE_CURVE25519
  472. WOLFSSL_TEST_SUBROUTINE int curve25519_test(void);
  473. #endif
  474. #ifdef HAVE_ED25519
  475. WOLFSSL_TEST_SUBROUTINE int ed25519_test(void);
  476. #endif
  477. #ifdef HAVE_CURVE448
  478. WOLFSSL_TEST_SUBROUTINE int curve448_test(void);
  479. #endif
  480. #ifdef HAVE_ED448
  481. WOLFSSL_TEST_SUBROUTINE int ed448_test(void);
  482. #endif
  483. #ifdef WOLFSSL_HAVE_KYBER
  484. WOLFSSL_TEST_SUBROUTINE int kyber_test(void);
  485. #endif
  486. #ifdef WOLFCRYPT_HAVE_ECCSI
  487. WOLFSSL_TEST_SUBROUTINE int eccsi_test(void);
  488. #endif
  489. #ifdef WOLFCRYPT_HAVE_SAKKE
  490. WOLFSSL_TEST_SUBROUTINE int sakke_test(void);
  491. #endif
  492. #ifdef HAVE_BLAKE2
  493. WOLFSSL_TEST_SUBROUTINE int blake2b_test(void);
  494. #endif
  495. #ifdef HAVE_BLAKE2S
  496. WOLFSSL_TEST_SUBROUTINE int blake2s_test(void);
  497. #endif
  498. #ifdef HAVE_LIBZ
  499. WOLFSSL_TEST_SUBROUTINE int compress_test(void);
  500. #endif
  501. #ifdef HAVE_PKCS7
  502. #ifndef NO_PKCS7_ENCRYPTED_DATA
  503. WOLFSSL_TEST_SUBROUTINE int pkcs7encrypted_test(void);
  504. #endif
  505. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  506. WOLFSSL_TEST_SUBROUTINE int pkcs7compressed_test(void);
  507. #endif
  508. WOLFSSL_TEST_SUBROUTINE int pkcs7signed_test(void);
  509. WOLFSSL_TEST_SUBROUTINE int pkcs7enveloped_test(void);
  510. #if defined(HAVE_AESGCM) || defined(HAVE_AESCCM)
  511. WOLFSSL_TEST_SUBROUTINE int pkcs7authenveloped_test(void);
  512. #endif
  513. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  514. WOLFSSL_TEST_SUBROUTINE int pkcs7callback_test(byte* cert, word32 certSz, byte* key,
  515. word32 keySz);
  516. #endif
  517. #endif
  518. #if !defined(NO_ASN_TIME) && !defined(NO_RSA) && defined(WOLFSSL_TEST_CERT) && \
  519. !defined(NO_FILESYSTEM)
  520. WOLFSSL_TEST_SUBROUTINE int cert_test(void);
  521. #endif
  522. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_TEST_CERT) && \
  523. !defined(NO_FILESYSTEM) && defined(WOLFSSL_CERT_GEN)
  524. WOLFSSL_TEST_SUBROUTINE int certext_test(void);
  525. #endif
  526. #if defined(WOLFSSL_CERT_GEN_CACHE) && defined(WOLFSSL_TEST_CERT) && \
  527. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  528. WOLFSSL_TEST_SUBROUTINE int decodedCertCache_test(void);
  529. #endif
  530. WOLFSSL_TEST_SUBROUTINE int memory_test(void);
  531. #ifdef HAVE_VALGRIND
  532. WOLFSSL_TEST_SUBROUTINE int mp_test(void);
  533. #endif
  534. #if defined(WOLFSSL_PUBLIC_MP) && defined(WOLFSSL_KEY_GEN)
  535. WOLFSSL_TEST_SUBROUTINE int prime_test(void);
  536. #endif
  537. #if defined(ASN_BER_TO_DER) && \
  538. (defined(WOLFSSL_TEST_CERT) || defined(OPENSSL_EXTRA) || \
  539. defined(OPENSSL_EXTRA_X509_SMALL))
  540. WOLFSSL_TEST_SUBROUTINE int berder_test(void);
  541. #endif
  542. WOLFSSL_TEST_SUBROUTINE int logging_test(void);
  543. #if !defined(NO_ASN) && !defined(NO_ASN_TIME)
  544. WOLFSSL_TEST_SUBROUTINE int time_test(void);
  545. #endif
  546. WOLFSSL_TEST_SUBROUTINE int mutex_test(void);
  547. #if defined(USE_WOLFSSL_MEMORY) && !defined(FREERTOS)
  548. WOLFSSL_TEST_SUBROUTINE int memcb_test(void);
  549. #endif
  550. #ifdef WOLFSSL_IMX6_CAAM_BLOB
  551. WOLFSSL_TEST_SUBROUTINE int blob_test(void);
  552. #endif
  553. #ifdef WOLF_CRYPTO_CB
  554. WOLFSSL_TEST_SUBROUTINE int cryptocb_test(void);
  555. #endif
  556. #ifdef WOLFSSL_CERT_PIV
  557. WOLFSSL_TEST_SUBROUTINE int certpiv_test(void);
  558. #endif
  559. #ifdef WOLFSSL_AES_SIV
  560. WOLFSSL_TEST_SUBROUTINE int aes_siv_test(void);
  561. #endif
  562. /* General big buffer size for many tests. */
  563. #define FOURK_BUF 4096
  564. #define ERROR_OUT(err, eLabel) do { ret = (err); goto eLabel; } while (0)
  565. #ifdef HAVE_STACK_SIZE
  566. static THREAD_RETURN err_sys(const char* msg, int es)
  567. #else
  568. static int err_sys(const char* msg, int es)
  569. #endif
  570. {
  571. (void)msg;
  572. (void)es;
  573. #ifdef WOLFSSL_LINUXKM
  574. lkm_printf("%s error = %d\n", msg, es);
  575. EXIT_TEST(es);
  576. #else
  577. printf("%s error = %d\n", msg, es);
  578. EXIT_TEST(-1);
  579. #endif
  580. }
  581. #ifndef HAVE_WOLFCRYPT_TEST_OPTIONS
  582. /* func_args from test.h, so don't have to pull in other stuff */
  583. typedef struct func_args {
  584. int argc;
  585. char** argv;
  586. int return_code;
  587. } func_args;
  588. #endif /* !HAVE_WOLFCRYPT_TEST_OPTIONS */
  589. #if defined(HAVE_FIPS) && !defined(WOLFSSL_LINUXKM)
  590. static void myFipsCb(int ok, int err, const char* hash)
  591. {
  592. printf("in my Fips callback, ok = %d, err = %d\n", ok, err);
  593. printf("message = %s\n", wc_GetErrorString(err));
  594. printf("hash = %s\n", hash);
  595. if (err == IN_CORE_FIPS_E) {
  596. printf("In core integrity hash check failure, copy above hash\n");
  597. printf("into verifyCore[] in fips_test.c and rebuild\n");
  598. }
  599. }
  600. #endif /* HAVE_FIPS && !WOLFSSL_LINUXKM */
  601. #ifdef WOLFSSL_STATIC_MEMORY
  602. #if defined(WOLFSSL_STATIC_MEMORY_TEST_SZ)
  603. static byte gTestMemory[WOLFSSL_STATIC_MEMORY_TEST_SZ];
  604. #elif defined(BENCH_EMBEDDED)
  605. static byte gTestMemory[14000];
  606. #elif defined(WOLFSSL_CERT_EXT)
  607. static byte gTestMemory[140000];
  608. #elif (defined(WOLFSSL_SP_MATH_ALL) || defined(USE_FAST_MATH)) && \
  609. !defined(ALT_ECC_SIZE)
  610. static byte gTestMemory[160000];
  611. #else
  612. static byte gTestMemory[80000];
  613. #endif
  614. #endif
  615. #ifdef WOLFSSL_PB
  616. static int wolfssl_pb_print(const char* msg, ...)
  617. {
  618. int ret;
  619. va_list args;
  620. char tmpBuf[80];
  621. va_start(args, msg);
  622. ret = vsprint(tmpBuf, msg, args);
  623. va_end(args);
  624. fnDumpStringToSystemLog(tmpBuf);
  625. return ret;
  626. }
  627. #endif /* WOLFSSL_PB */
  628. /* optional macro to add sleep between tests */
  629. #ifndef TEST_SLEEP
  630. #define TEST_SLEEP()
  631. #else
  632. #define TEST_PASS test_pass
  633. #include <stdarg.h> /* for var args */
  634. static WC_INLINE void test_pass(const char* fmt, ...)
  635. {
  636. va_list args;
  637. va_start(args, fmt);
  638. STACK_SIZE_CHECKPOINT_WITH_MAX_CHECK(max_relative_stack, vprintf(fmt, args));
  639. va_end(args);
  640. PRINT_HEAP_CHECKPOINT();
  641. TEST_SLEEP();
  642. ASSERT_RESTORED_VECTOR_REGISTERS(exit(1););
  643. }
  644. #endif
  645. /* set test pass output to printf if not overriden */
  646. #ifndef TEST_PASS
  647. /* redirect to printf */
  648. #define TEST_PASS(...) { \
  649. if (STACK_SIZE_CHECKPOINT_WITH_MAX_CHECK \
  650. (max_relative_stack, printf(__VA_ARGS__)) < 0) { \
  651. return err_sys("post-test check failed", -1); \
  652. } \
  653. PRINT_HEAP_CHECKPOINT(); \
  654. ASSERT_RESTORED_VECTOR_REGISTERS(exit(1);); \
  655. }
  656. #endif
  657. #ifdef HAVE_STACK_SIZE
  658. THREAD_RETURN WOLFSSL_THREAD wolfcrypt_test(void* args)
  659. #else
  660. int wolfcrypt_test(void* args)
  661. #endif
  662. {
  663. int ret;
  664. #ifdef WOLFSSL_TRACK_MEMORY_VERBOSE
  665. long heap_baselineAllocs, heap_baselineBytes;
  666. #endif
  667. STACK_SIZE_INIT();
  668. #ifdef WOLFSSL_TRACK_MEMORY_VERBOSE
  669. (void)wolfCrypt_heap_peakAllocs_checkpoint();
  670. heap_baselineAllocs = wolfCrypt_heap_peakAllocs_checkpoint();
  671. (void)wolfCrypt_heap_peakBytes_checkpoint();
  672. heap_baselineBytes = wolfCrypt_heap_peakBytes_checkpoint();
  673. #endif
  674. printf("------------------------------------------------------------------------------\n");
  675. printf(" wolfSSL version %s\n", LIBWOLFSSL_VERSION_STRING);
  676. printf("------------------------------------------------------------------------------\n");
  677. if (args) {
  678. #ifdef HAVE_WOLFCRYPT_TEST_OPTIONS
  679. int ch;
  680. #endif
  681. ((func_args*)args)->return_code = -1; /* error state */
  682. #ifdef HAVE_WOLFCRYPT_TEST_OPTIONS
  683. while ((ch = mygetopt(((func_args*)args)->argc, ((func_args*)args)->argv, "s:m:a:h")) != -1) {
  684. switch(ch) {
  685. case 's':
  686. #ifdef HAVE_STACK_SIZE_VERBOSE
  687. max_relative_stack = (ssize_t)atoi(myoptarg);
  688. break;
  689. #else
  690. return err_sys("-s (max relative stack bytes) requires HAVE_STACK_SIZE_VERBOSE (--enable-stacksize=verbose).", -1);
  691. #endif
  692. case 'm':
  693. #ifdef WOLFSSL_TRACK_MEMORY_VERBOSE
  694. max_relative_heap_bytes = (ssize_t)atoi(myoptarg);
  695. break;
  696. #else
  697. return err_sys("-m (max relative heap memory bytes) requires WOLFSSL_TRACK_MEMORY_VERBOSE (--enable-trackmemory=verbose).", -1);
  698. #endif
  699. case 'a':
  700. #ifdef WOLFSSL_TRACK_MEMORY_VERBOSE
  701. max_relative_heap_allocs = (ssize_t)atoi(myoptarg);
  702. break;
  703. #else
  704. return err_sys("-a (max relative heap allocs) requires WOLFSSL_TRACK_MEMORY_VERBOSE (--enable-trackmemory=verbose).", -1);
  705. #endif
  706. case 'h':
  707. return err_sys("\
  708. options: [-s max_relative_stack_bytes] [-m max_relative_heap_memory_bytes]\n\
  709. [-a max_relative_heap_allocs] [-h]\n", 0);
  710. default:
  711. return err_sys("unknown test option. try -h.", -1);
  712. }
  713. }
  714. #endif
  715. }
  716. #ifdef WOLFSSL_STATIC_MEMORY
  717. if (wc_LoadStaticMemory(&HEAP_HINT, gTestMemory, sizeof(gTestMemory),
  718. WOLFMEM_GENERAL, 1) != 0) {
  719. printf("unable to load static memory.\n");
  720. return(EXIT_FAILURE);
  721. }
  722. #endif
  723. #if defined(DEBUG_WOLFSSL) && !defined(HAVE_VALGRIND)
  724. wolfSSL_Debugging_ON();
  725. #endif
  726. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  727. wc_SetLoggingHeap(HEAP_HINT);
  728. #endif
  729. #if defined(HAVE_FIPS) && !defined(WOLFSSL_LINUXKM)
  730. wolfCrypt_SetCb_fips(myFipsCb);
  731. #endif
  732. #if !defined(NO_BIG_INT)
  733. if (CheckCtcSettings() != 1) {
  734. printf("Sizeof mismatch (build) %x != (run) %lx\n",
  735. CTC_SETTINGS, (unsigned long)CheckRunTimeSettings());
  736. return err_sys("Build vs runtime math mismatch\n", -1000);
  737. }
  738. #if defined(USE_FAST_MATH) && \
  739. (!defined(NO_RSA) || !defined(NO_DH) || defined(HAVE_ECC))
  740. if (CheckFastMathSettings() != 1)
  741. return err_sys("Build vs runtime fastmath FP_MAX_BITS mismatch\n",
  742. -1001);
  743. #endif /* USE_FAST_MATH */
  744. #endif /* !NO_BIG_INT */
  745. #if defined(WOLFSSL_CERT_GEN) && (!defined(NO_RSA) || defined(HAVE_ECC)) || \
  746. (defined(WOLFSSL_TEST_CERT) && (defined(HAVE_ED25519) || defined(HAVE_ED448)))
  747. initDefaultName();
  748. #endif
  749. #ifdef WOLFSSL_ASYNC_CRYPT
  750. ret = wolfAsync_DevOpen(&devId);
  751. if (ret < 0) {
  752. printf("Async device open failed\nRunning without async\n");
  753. }
  754. #else
  755. (void)devId;
  756. #endif /* WOLFSSL_ASYNC_CRYPT */
  757. #ifdef WOLF_CRYPTO_CB
  758. #ifdef HAVE_INTEL_QA_SYNC
  759. devId = wc_CryptoCb_InitIntelQa();
  760. if (INVALID_DEVID == devId) {
  761. printf("Couldn't init the Intel QA\n");
  762. }
  763. #endif
  764. #ifdef HAVE_CAVIUM_OCTEON_SYNC
  765. devId = wc_CryptoCb_InitOcteon();
  766. if (INVALID_DEVID == devId) {
  767. printf("Couldn't init the Cavium Octeon\n");
  768. }
  769. #endif
  770. #endif
  771. #ifdef HAVE_SELFTEST
  772. if ( (ret = wolfCrypt_SelfTest()) != 0)
  773. return err_sys("CAVP selftest failed!\n", ret);
  774. else
  775. TEST_PASS("CAVP selftest passed!\n");
  776. #endif
  777. if ( (ret = error_test()) != 0)
  778. return err_sys("error test failed!\n", ret);
  779. else
  780. TEST_PASS("error test passed!\n");
  781. if ( (ret = memory_test()) != 0)
  782. return err_sys("MEMORY test failed!\n", ret);
  783. else
  784. TEST_PASS("MEMORY test passed!\n");
  785. #ifndef NO_CODING
  786. if ( (ret = base64_test()) != 0)
  787. return err_sys("base64 test failed!\n", ret);
  788. else
  789. TEST_PASS("base64 test passed!\n");
  790. #ifdef WOLFSSL_BASE16
  791. if ( (ret = base16_test()) != 0)
  792. return err_sys("base16 test failed!\n", ret);
  793. else
  794. TEST_PASS("base16 test passed!\n");
  795. #endif
  796. #endif /* !NO_CODING */
  797. #ifndef NO_ASN
  798. if ( (ret = asn_test()) != 0)
  799. return err_sys("asn test failed!\n", ret);
  800. else
  801. TEST_PASS("asn test passed!\n");
  802. #endif
  803. #ifndef WC_NO_RNG
  804. if ( (ret = random_test()) != 0)
  805. return err_sys("RANDOM test failed!\n", ret);
  806. else
  807. TEST_PASS("RANDOM test passed!\n");
  808. #endif /* WC_NO_RNG */
  809. #ifndef NO_MD5
  810. if ( (ret = md5_test()) != 0)
  811. return err_sys("MD5 test failed!\n", ret);
  812. else
  813. TEST_PASS("MD5 test passed!\n");
  814. #endif
  815. #ifdef WOLFSSL_MD2
  816. if ( (ret = md2_test()) != 0)
  817. return err_sys("MD2 test failed!\n", ret);
  818. else
  819. TEST_PASS("MD2 test passed!\n");
  820. #endif
  821. #ifndef NO_MD4
  822. if ( (ret = md4_test()) != 0)
  823. return err_sys("MD4 test failed!\n", ret);
  824. else
  825. TEST_PASS("MD4 test passed!\n");
  826. #endif
  827. #ifndef NO_SHA
  828. if ( (ret = sha_test()) != 0)
  829. return err_sys("SHA test failed!\n", ret);
  830. else
  831. TEST_PASS("SHA test passed!\n");
  832. #endif
  833. #ifdef WOLFSSL_SHA224
  834. if ( (ret = sha224_test()) != 0)
  835. return err_sys("SHA-224 test failed!\n", ret);
  836. else
  837. TEST_PASS("SHA-224 test passed!\n");
  838. #endif
  839. #ifndef NO_SHA256
  840. if ( (ret = sha256_test()) != 0)
  841. return err_sys("SHA-256 test failed!\n", ret);
  842. else
  843. TEST_PASS("SHA-256 test passed!\n");
  844. #endif
  845. #ifdef WOLFSSL_SHA384
  846. if ( (ret = sha384_test()) != 0)
  847. return err_sys("SHA-384 test failed!\n", ret);
  848. else
  849. TEST_PASS("SHA-384 test passed!\n");
  850. #endif
  851. #ifdef WOLFSSL_SHA512
  852. if ( (ret = sha512_test()) != 0)
  853. return err_sys("SHA-512 test failed!\n", ret);
  854. else
  855. TEST_PASS("SHA-512 test passed!\n");
  856. #endif
  857. #ifdef WOLFSSL_SHA3
  858. if ( (ret = sha3_test()) != 0)
  859. return err_sys("SHA-3 test failed!\n", ret);
  860. else
  861. TEST_PASS("SHA-3 test passed!\n");
  862. #endif
  863. #ifdef WOLFSSL_SHAKE128
  864. if ( (ret = shake128_test()) != 0)
  865. return err_sys("SHAKE128 test failed!\n", ret);
  866. else
  867. TEST_PASS("SHAKE128 test passed!\n");
  868. #endif
  869. #ifdef WOLFSSL_SHAKE256
  870. if ( (ret = shake256_test()) != 0)
  871. return err_sys("SHAKE256 test failed!\n", ret);
  872. else
  873. TEST_PASS("SHAKE256 test passed!\n");
  874. #endif
  875. #ifndef NO_HASH_WRAPPER
  876. if ( (ret = hash_test()) != 0)
  877. return err_sys("Hash test failed!\n", ret);
  878. else
  879. TEST_PASS("Hash test passed!\n");
  880. #endif
  881. #ifdef WOLFSSL_RIPEMD
  882. if ( (ret = ripemd_test()) != 0)
  883. return err_sys("RIPEMD test failed!\n", ret);
  884. else
  885. TEST_PASS("RIPEMD test passed!\n");
  886. #endif
  887. #ifdef HAVE_BLAKE2
  888. if ( (ret = blake2b_test()) != 0)
  889. return err_sys("BLAKE2b test failed!\n", ret);
  890. else
  891. TEST_PASS("BLAKE2b test passed!\n");
  892. #endif
  893. #ifdef HAVE_BLAKE2S
  894. if ( (ret = blake2s_test()) != 0)
  895. return err_sys("BLAKE2s test failed!\n", ret);
  896. else
  897. TEST_PASS("BLAKE2s test passed!\n");
  898. #endif
  899. #ifndef NO_HMAC
  900. #if !defined(NO_MD5) && !(defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) \
  901. && (HAVE_FIPS_VERSION >= 5))
  902. if ( (ret = hmac_md5_test()) != 0)
  903. return err_sys("HMAC-MD5 test failed!\n", ret);
  904. else
  905. TEST_PASS("HMAC-MD5 test passed!\n");
  906. #endif
  907. #ifndef NO_SHA
  908. if ( (ret = hmac_sha_test()) != 0)
  909. return err_sys("HMAC-SHA test failed!\n", ret);
  910. else
  911. TEST_PASS("HMAC-SHA test passed!\n");
  912. #endif
  913. #ifdef WOLFSSL_SHA224
  914. if ( (ret = hmac_sha224_test()) != 0)
  915. return err_sys("HMAC-SHA224 test failed!\n", ret);
  916. else
  917. TEST_PASS("HMAC-SHA224 test passed!\n");
  918. #endif
  919. #ifndef NO_SHA256
  920. if ( (ret = hmac_sha256_test()) != 0)
  921. return err_sys("HMAC-SHA256 test failed!\n", ret);
  922. else
  923. TEST_PASS("HMAC-SHA256 test passed!\n");
  924. #endif
  925. #ifdef WOLFSSL_SHA384
  926. if ( (ret = hmac_sha384_test()) != 0)
  927. return err_sys("HMAC-SHA384 test failed!\n", ret);
  928. else
  929. TEST_PASS("HMAC-SHA384 test passed!\n");
  930. #endif
  931. #ifdef WOLFSSL_SHA512
  932. if ( (ret = hmac_sha512_test()) != 0)
  933. return err_sys("HMAC-SHA512 test failed!\n", ret);
  934. else
  935. TEST_PASS("HMAC-SHA512 test passed!\n");
  936. #endif
  937. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA3) && \
  938. !defined(WOLFSSL_NOSHA3_224) && !defined(WOLFSSL_NOSHA3_256) && \
  939. !defined(WOLFSSL_NOSHA3_384) && !defined(WOLFSSL_NOSHA3_512)
  940. if ( (ret = hmac_sha3_test()) != 0)
  941. return err_sys("HMAC-SHA3 test failed!\n", ret);
  942. else
  943. TEST_PASS("HMAC-SHA3 test passed!\n");
  944. #endif
  945. #if defined(HAVE_HKDF) && !defined(NO_HMAC)
  946. PRIVATE_KEY_UNLOCK();
  947. if ( (ret = hkdf_test()) != 0)
  948. return err_sys("HMAC-KDF test failed!\n", ret);
  949. else
  950. TEST_PASS("HMAC-KDF test passed!\n");
  951. PRIVATE_KEY_LOCK();
  952. #endif
  953. #endif /* !NO_HMAC */
  954. #ifdef WOLFSSL_WOLFSSH
  955. PRIVATE_KEY_UNLOCK();
  956. if ( (ret = sshkdf_test()) != 0)
  957. return err_sys("SSH-KDF test failed!\n", ret);
  958. else
  959. TEST_PASS("SSH-KDF test passed!\n");
  960. PRIVATE_KEY_LOCK();
  961. #endif /* WOLFSSL_WOLFSSH */
  962. #ifdef WOLFSSL_TLS13
  963. PRIVATE_KEY_UNLOCK();
  964. if ( (ret = tls13_kdf_test()) != 0)
  965. return err_sys("TLSv1.3 KDF test failed!\n", ret);
  966. else
  967. TEST_PASS("TLSv1.3 KDF test passed!\n");
  968. PRIVATE_KEY_LOCK();
  969. #endif /* WOLFSSL_TLS13 */
  970. #if defined(HAVE_X963_KDF) && defined(HAVE_ECC)
  971. if ( (ret = x963kdf_test()) != 0)
  972. return err_sys("X963-KDF test failed!\n", ret);
  973. else
  974. TEST_PASS("X963-KDF test passed!\n");
  975. #endif
  976. #if defined(HAVE_HPKE) && defined(HAVE_ECC) && defined(HAVE_AESGCM)
  977. if ( (ret = hpke_test()) != 0)
  978. return err_sys("HPKE test failed!\n", ret);
  979. else
  980. TEST_PASS("HPKE test passed!\n");
  981. #endif
  982. #if defined(HAVE_AESGCM) && defined(WOLFSSL_AES_128) && \
  983. !defined(WOLFSSL_AFALG_XILINX_AES) && !defined(WOLFSSL_XILINX_CRYPT)
  984. if ( (ret = gmac_test()) != 0)
  985. return err_sys("GMAC test failed!\n", ret);
  986. else
  987. TEST_PASS("GMAC test passed!\n");
  988. #endif
  989. #ifdef WC_RC2
  990. if ( (ret = rc2_test()) != 0)
  991. return err_sys("RC2 test failed!\n", ret);
  992. else
  993. TEST_PASS("RC2 test passed!\n");
  994. #endif
  995. #ifndef NO_RC4
  996. if ( (ret = arc4_test()) != 0)
  997. return err_sys("ARC4 test failed!\n", ret);
  998. else
  999. TEST_PASS("ARC4 test passed!\n");
  1000. #endif
  1001. #ifdef HAVE_CHACHA
  1002. if ( (ret = chacha_test()) != 0)
  1003. return err_sys("Chacha test failed!\n", ret);
  1004. else
  1005. TEST_PASS("Chacha test passed!\n");
  1006. #endif
  1007. #ifdef HAVE_XCHACHA
  1008. if ( (ret = XChaCha_test()) != 0)
  1009. return err_sys("XChacha test failed!\n", ret);
  1010. else
  1011. TEST_PASS("XChacha test passed!\n");
  1012. #endif
  1013. #ifdef HAVE_POLY1305
  1014. if ( (ret = poly1305_test()) != 0)
  1015. return err_sys("POLY1305 test failed!\n", ret);
  1016. else
  1017. TEST_PASS("POLY1305 test passed!\n");
  1018. #endif
  1019. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  1020. if ( (ret = chacha20_poly1305_aead_test()) != 0)
  1021. return err_sys("ChaCha20-Poly1305 AEAD test failed!\n", ret);
  1022. else
  1023. TEST_PASS("ChaCha20-Poly1305 AEAD test passed!\n");
  1024. #endif
  1025. #if defined(HAVE_XCHACHA) && defined(HAVE_POLY1305)
  1026. if ( (ret = XChaCha20Poly1305_test()) != 0)
  1027. return err_sys("XChaCha20-Poly1305 AEAD test failed!\n", ret);
  1028. else
  1029. TEST_PASS("XChaCha20-Poly1305 AEAD test passed!\n");
  1030. #endif
  1031. #ifndef NO_DES3
  1032. if ( (ret = des_test()) != 0)
  1033. return err_sys("DES test failed!\n", ret);
  1034. else
  1035. TEST_PASS("DES test passed!\n");
  1036. #endif
  1037. #ifndef NO_DES3
  1038. if ( (ret = des3_test()) != 0)
  1039. return err_sys("DES3 test failed!\n", ret);
  1040. else
  1041. TEST_PASS("DES3 test passed!\n");
  1042. #endif
  1043. #ifndef NO_AES
  1044. if ( (ret = aes_test()) != 0)
  1045. return err_sys("AES test failed!\n", ret);
  1046. else
  1047. TEST_PASS("AES test passed!\n");
  1048. #ifdef WOLFSSL_AES_192
  1049. if ( (ret = aes192_test()) != 0)
  1050. return err_sys("AES192 test failed!\n", ret);
  1051. else
  1052. TEST_PASS("AES192 test passed!\n");
  1053. #endif
  1054. #ifdef WOLFSSL_AES_256
  1055. if ( (ret = aes256_test()) != 0)
  1056. return err_sys("AES256 test failed!\n", ret);
  1057. else
  1058. TEST_PASS("AES256 test passed!\n");
  1059. #endif
  1060. #ifdef WOLFSSL_AES_OFB
  1061. if ( (ret = aesofb_test()) != 0)
  1062. return err_sys("AES-OFB test failed!\n", ret);
  1063. else
  1064. TEST_PASS("AESOFB test passed!\n");
  1065. #endif
  1066. #ifdef HAVE_AESGCM
  1067. #if !defined(WOLFSSL_AFALG) && !defined(WOLFSSL_DEVCRYPTO)
  1068. if ( (ret = aesgcm_test()) != 0)
  1069. return err_sys("AES-GCM test failed!\n", ret);
  1070. #endif
  1071. #if !defined(WOLFSSL_AFALG_XILINX_AES) && !defined(WOLFSSL_XILINX_CRYPT) && \
  1072. !defined(WOLFSSL_KCAPI_AES) && !(defined(WOLF_CRYPTO_CB) && \
  1073. (defined(HAVE_INTEL_QA_SYNC) || defined(HAVE_CAVIUM_OCTEON_SYNC)))
  1074. if ((ret = aesgcm_default_test()) != 0) {
  1075. return err_sys("AES-GCM test failed!\n", ret);
  1076. }
  1077. #endif
  1078. if (ret == 0) {
  1079. TEST_PASS("AES-GCM test passed!\n");
  1080. }
  1081. #endif
  1082. #if defined(HAVE_AESCCM) && defined(WOLFSSL_AES_128)
  1083. if ( (ret = aesccm_test()) != 0)
  1084. return err_sys("AES-CCM test failed!\n", ret);
  1085. else
  1086. TEST_PASS("AES-CCM test passed!\n");
  1087. #endif
  1088. #ifdef HAVE_AES_KEYWRAP
  1089. if ( (ret = aeskeywrap_test()) != 0)
  1090. return err_sys("AES Key Wrap test failed!\n", ret);
  1091. else
  1092. TEST_PASS("AES Key Wrap test passed!\n");
  1093. #endif
  1094. #ifdef WOLFSSL_AES_SIV
  1095. if ( (ret = aes_siv_test()) != 0)
  1096. return err_sys("AES-SIV test failed!\n", ret);
  1097. else
  1098. TEST_PASS("AES-SIV test passed!\n");
  1099. #endif
  1100. #endif
  1101. #ifdef HAVE_CAMELLIA
  1102. if ( (ret = camellia_test()) != 0)
  1103. return err_sys("CAMELLIA test failed!\n", ret);
  1104. else
  1105. TEST_PASS("CAMELLIA test passed!\n");
  1106. #endif
  1107. #if !defined(NO_RSA)
  1108. #ifdef WC_RSA_NO_PADDING
  1109. if ( (ret = rsa_no_pad_test()) != 0)
  1110. return err_sys("RSA NOPAD test failed!\n", ret);
  1111. else
  1112. TEST_PASS("RSA NOPAD test passed!\n");
  1113. #endif
  1114. if ( (ret = rsa_test()) != 0)
  1115. return err_sys("RSA test failed!\n", ret);
  1116. else
  1117. TEST_PASS("RSA test passed!\n");
  1118. #endif
  1119. #ifndef NO_DH
  1120. PRIVATE_KEY_UNLOCK();
  1121. if ( (ret = dh_test()) != 0)
  1122. return err_sys("DH test failed!\n", ret);
  1123. else
  1124. TEST_PASS("DH test passed!\n");
  1125. PRIVATE_KEY_LOCK();
  1126. #endif
  1127. #ifndef NO_DSA
  1128. if ( (ret = dsa_test()) != 0)
  1129. return err_sys("DSA test failed!\n", ret);
  1130. else
  1131. TEST_PASS("DSA test passed!\n");
  1132. #endif
  1133. #ifdef WOLFCRYPT_HAVE_SRP
  1134. if ( (ret = srp_test()) != 0)
  1135. return err_sys("SRP test failed!\n", ret);
  1136. else
  1137. TEST_PASS("SRP test passed!\n");
  1138. #endif
  1139. #ifndef NO_PWDBASED
  1140. if ( (ret = pwdbased_test()) != 0)
  1141. return err_sys("PWDBASED test failed!\n", ret);
  1142. else
  1143. TEST_PASS("PWDBASED test passed!\n");
  1144. #endif
  1145. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  1146. if ( (ret = openssl_test()) != 0)
  1147. return err_sys("OPENSSL test failed!\n", ret);
  1148. else
  1149. TEST_PASS("OPENSSL test passed!\n");
  1150. if ( (ret = openSSL_evpMD_test()) != 0)
  1151. return err_sys("OPENSSL (EVP MD) test failed!\n", ret);
  1152. else
  1153. TEST_PASS("OPENSSL (EVP MD) passed!\n");
  1154. if ( (ret = openssl_pkey0_test()) != 0)
  1155. return err_sys("OPENSSL (PKEY0) test failed!\n", ret);
  1156. else
  1157. TEST_PASS("OPENSSL (PKEY0) passed!\n");
  1158. if ( (ret = openssl_pkey1_test()) != 0)
  1159. return err_sys("OPENSSL (PKEY1) test failed!\n", ret);
  1160. else
  1161. TEST_PASS("OPENSSL (PKEY1) passed!\n");
  1162. #if !defined(WOLF_CRYPTO_CB_ONLY_RSA) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  1163. if ( (ret = openssl_evpSig_test()) != 0)
  1164. return err_sys("OPENSSL (EVP Sign/Verify) test failed!\n", ret);
  1165. else
  1166. TEST_PASS("OPENSSL (EVP Sign/Verify) passed!\n");
  1167. #endif
  1168. #endif
  1169. #if defined(HAVE_ECC)
  1170. PRIVATE_KEY_UNLOCK();
  1171. if ( (ret = ecc_test()) != 0)
  1172. return err_sys("ECC test failed!\n", ret);
  1173. else
  1174. TEST_PASS("ECC test passed!\n");
  1175. PRIVATE_KEY_LOCK();
  1176. #if defined(HAVE_ECC_ENCRYPT) && defined(HAVE_AES_CBC) && \
  1177. (defined(WOLFSSL_AES_128) || defined(WOLFSSL_AES_256))
  1178. if ( (ret = ecc_encrypt_test()) != 0)
  1179. return err_sys("ECC Enc test failed!\n", ret);
  1180. else
  1181. TEST_PASS("ECC Enc test passed!\n");
  1182. #endif
  1183. #if defined(USE_CERT_BUFFERS_256) && !defined(WOLFSSL_ATECC508A) && \
  1184. !defined(WOLFSSL_ATECC608A) && !defined(NO_ECC256) && \
  1185. defined(HAVE_ECC_VERIFY) && defined(HAVE_ECC_SIGN) && \
  1186. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  1187. /* skip for ATECC508/608A, cannot import private key buffers */
  1188. if ( (ret = ecc_test_buffers()) != 0)
  1189. return err_sys("ECC buffer test failed!\n", ret);
  1190. else
  1191. TEST_PASS("ECC buffer test passed!\n");
  1192. #endif
  1193. #endif
  1194. #if !defined(NO_ASN_TIME) && !defined(NO_RSA) && defined(WOLFSSL_TEST_CERT) && \
  1195. !defined(NO_FILESYSTEM)
  1196. if ( (ret = cert_test()) != 0)
  1197. return err_sys("CERT test failed!\n", ret);
  1198. else
  1199. TEST_PASS("CERT test passed!\n");
  1200. #endif
  1201. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_TEST_CERT) && \
  1202. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && defined(WOLFSSL_GEN_CERT)
  1203. if ( (ret = certext_test()) != 0)
  1204. return err_sys("CERT EXT test failed!\n", ret);
  1205. else
  1206. TEST_PASS("CERT EXT test passed!\n");
  1207. #endif
  1208. #if defined(WOLFSSL_CERT_GEN_CACHE) && defined(WOLFSSL_TEST_CERT) && \
  1209. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  1210. if ( (ret = decodedCertCache_test()) != 0)
  1211. return err_sys("DECODED CERT CACHE test failed!\n", ret);
  1212. else
  1213. TEST_PASS("DECODED CERT CACHE test passed!\n");
  1214. #endif
  1215. #ifdef HAVE_CURVE25519
  1216. if ( (ret = curve25519_test()) != 0)
  1217. return err_sys("CURVE25519 test failed!\n", ret);
  1218. else
  1219. TEST_PASS("CURVE25519 test passed!\n");
  1220. #endif
  1221. #ifdef HAVE_ED25519
  1222. if ( (ret = ed25519_test()) != 0)
  1223. return err_sys("ED25519 test failed!\n", ret);
  1224. else
  1225. TEST_PASS("ED25519 test passed!\n");
  1226. #endif
  1227. #ifdef HAVE_CURVE448
  1228. if ( (ret = curve448_test()) != 0)
  1229. return err_sys("CURVE448 test failed!\n", ret);
  1230. else
  1231. TEST_PASS("CURVE448 test passed!\n");
  1232. #endif
  1233. #ifdef HAVE_ED448
  1234. if ( (ret = ed448_test()) != 0)
  1235. return err_sys("ED448 test failed!\n", ret);
  1236. else
  1237. TEST_PASS("ED448 test passed!\n");
  1238. #endif
  1239. #ifdef WOLFSSL_HAVE_KYBER
  1240. if ( (ret = kyber_test()) != 0)
  1241. return err_sys("KYBER test failed!\n", ret);
  1242. else
  1243. TEST_PASS("KYBER test passed!\n");
  1244. #endif
  1245. #ifdef WOLFCRYPT_HAVE_ECCSI
  1246. if ( (ret = eccsi_test()) != 0)
  1247. return err_sys("ECCSI test failed!\n", ret);
  1248. else
  1249. TEST_PASS("ECCSI test passed!\n");
  1250. #endif
  1251. #ifdef WOLFCRYPT_HAVE_SAKKE
  1252. if ( (ret = sakke_test()) != 0)
  1253. return err_sys("SAKKE test failed!\n", ret);
  1254. else
  1255. TEST_PASS("SAKKE test passed!\n");
  1256. #endif
  1257. #if defined(WOLFSSL_CMAC) && !defined(NO_AES)
  1258. if ( (ret = cmac_test()) != 0)
  1259. return err_sys("CMAC test failed!\n", ret);
  1260. else
  1261. TEST_PASS("CMAC test passed!\n");
  1262. #endif
  1263. #if defined(WOLFSSL_SIPHASH)
  1264. if ( (ret = siphash_test()) != 0)
  1265. return err_sys("SipHash test failed!\n", ret);
  1266. else
  1267. TEST_PASS("SipHash test passed!\n");
  1268. #endif
  1269. #ifdef HAVE_LIBZ
  1270. if ( (ret = compress_test()) != 0)
  1271. return err_sys("COMPRESS test failed!\n", ret);
  1272. else
  1273. TEST_PASS("COMPRESS test passed!\n");
  1274. #endif
  1275. #ifdef HAVE_PKCS7
  1276. #ifndef NO_PKCS7_ENCRYPTED_DATA
  1277. if ( (ret = pkcs7encrypted_test()) != 0)
  1278. return err_sys("PKCS7encrypted test failed!\n", ret);
  1279. else
  1280. TEST_PASS("PKCS7encrypted test passed!\n");
  1281. #endif
  1282. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  1283. if ( (ret = pkcs7compressed_test()) != 0)
  1284. return err_sys("PKCS7compressed test failed!\n", ret);
  1285. else
  1286. TEST_PASS("PKCS7compressed test passed!\n");
  1287. #endif
  1288. if ( (ret = pkcs7signed_test()) != 0)
  1289. return err_sys("PKCS7signed test failed!\n", ret);
  1290. else
  1291. TEST_PASS("PKCS7signed test passed!\n");
  1292. if ( (ret = pkcs7enveloped_test()) != 0)
  1293. return err_sys("PKCS7enveloped test failed!\n", ret);
  1294. else
  1295. TEST_PASS("PKCS7enveloped test passed!\n");
  1296. #if defined(HAVE_AESGCM) || defined(HAVE_AESCCM)
  1297. if ( (ret = pkcs7authenveloped_test()) != 0)
  1298. return err_sys("PKCS7authenveloped test failed!\n", ret);
  1299. else
  1300. TEST_PASS("PKCS7authenveloped test passed!\n");
  1301. #endif
  1302. #endif
  1303. #ifdef HAVE_VALGRIND
  1304. if ( (ret = mp_test()) != 0)
  1305. return err_sys("mp test failed!\n", ret);
  1306. else
  1307. TEST_PASS("mp test passed!\n");
  1308. #endif
  1309. #if defined(WOLFSSL_PUBLIC_MP) && defined(WOLFSSL_KEY_GEN)
  1310. if ( (ret = prime_test()) != 0)
  1311. return err_sys("prime test failed!\n", ret);
  1312. else
  1313. TEST_PASS("prime test passed!\n");
  1314. #endif
  1315. #if defined(ASN_BER_TO_DER) && \
  1316. (defined(WOLFSSL_TEST_CERT) || defined(OPENSSL_EXTRA) || \
  1317. defined(OPENSSL_EXTRA_X509_SMALL))
  1318. if ( (ret = berder_test()) != 0)
  1319. return err_sys("ber-der test failed!\n", ret);
  1320. else
  1321. TEST_PASS("ber-der test passed!\n");
  1322. #endif
  1323. if ( (ret = logging_test()) != 0)
  1324. return err_sys("logging test failed!\n", ret);
  1325. else
  1326. TEST_PASS("logging test passed!\n");
  1327. #if !defined(NO_ASN) && !defined(NO_ASN_TIME)
  1328. if ( (ret = time_test()) != 0)
  1329. return err_sys("time test failed!\n", ret);
  1330. else
  1331. TEST_PASS("time test passed!\n");
  1332. #endif
  1333. if ( (ret = mutex_test()) != 0)
  1334. return err_sys("mutex test failed!\n", ret);
  1335. else
  1336. TEST_PASS("mutex test passed!\n");
  1337. #if defined(USE_WOLFSSL_MEMORY) && !defined(FREERTOS)
  1338. if ( (ret = memcb_test()) != 0)
  1339. return err_sys("memcb test failed!\n", ret);
  1340. else
  1341. TEST_PASS("memcb test passed!\n");
  1342. #endif
  1343. #ifdef WOLFSSL_IMX6_CAAM_BLOB
  1344. if ( (ret = blob_test()) != 0)
  1345. return err_sys("blob test failed!\n", ret);
  1346. else
  1347. TEST_PASS("blob test passed!\n");
  1348. #endif
  1349. #if defined(WOLF_CRYPTO_CB) && \
  1350. !(defined(HAVE_INTEL_QAT_SYNC) || defined(HAVE_CAVIUM_OCTEON_SYNC) || \
  1351. defined(WOLFSSL_QNX_CAAM))
  1352. if ( (ret = cryptocb_test()) != 0)
  1353. return err_sys("crypto callback test failed!\n", ret);
  1354. else
  1355. TEST_PASS("crypto callback test passed!\n");
  1356. #endif
  1357. #ifdef WOLFSSL_CERT_PIV
  1358. if ( (ret = certpiv_test()) != 0)
  1359. return err_sys("cert piv test failed!\n", ret);
  1360. else
  1361. TEST_PASS("cert piv test passed!\n");
  1362. #endif
  1363. #ifdef WOLF_CRYPTO_CB
  1364. #ifdef HAVE_INTEL_QA_SYNC
  1365. wc_CryptoCb_CleanupIntelQa(&devId);
  1366. #endif
  1367. #ifdef HAVE_CAVIUM_OCTEON_SYNC
  1368. wc_CryptoCb_CleanupOcteon(&devId);
  1369. #endif
  1370. #endif
  1371. #ifdef WOLFSSL_ASYNC_CRYPT
  1372. wolfAsync_DevClose(&devId);
  1373. #endif
  1374. /* cleanup the thread if fixed point cache is enabled and have thread local */
  1375. #if defined(HAVE_THREAD_LS) && defined(HAVE_ECC) && defined(FP_ECC)
  1376. wc_ecc_fp_free();
  1377. #endif
  1378. if (args)
  1379. ((func_args*)args)->return_code = ret;
  1380. TEST_PASS("Test complete\n");
  1381. EXIT_TEST(ret);
  1382. }
  1383. #ifndef NO_MAIN_DRIVER
  1384. #ifdef HAVE_WOLFCRYPT_TEST_OPTIONS
  1385. int myoptind = 0;
  1386. char* myoptarg = NULL;
  1387. #endif
  1388. /* so overall tests can pull in test function */
  1389. #if defined(WOLFSSL_ESPIDF) || defined(_WIN32_WCE)
  1390. int wolf_test_task(void)
  1391. #else
  1392. #ifndef NO_MAIN_FUNCTION
  1393. int main(int argc, char** argv)
  1394. {
  1395. return wolfcrypt_test_main(argc, argv);
  1396. }
  1397. #endif
  1398. int wolfcrypt_test_main(int argc, char** argv)
  1399. #endif
  1400. {
  1401. int ret;
  1402. func_args args;
  1403. #if defined(WOLFSSL_ESPIDF) || defined(WOLFSSL_SE050)
  1404. /* set dummy wallclock time. */
  1405. struct timeval utctime;
  1406. struct timezone tz;
  1407. utctime.tv_sec = 1521725159; /* dummy time: 2018-03-22T13:25:59+00:00 */
  1408. utctime.tv_usec = 0;
  1409. tz.tz_minuteswest = 0;
  1410. tz.tz_dsttime = 0;
  1411. settimeofday(&utctime, &tz);
  1412. #endif
  1413. #ifdef WOLFSSL_APACHE_MYNEWT
  1414. #ifdef ARCH_sim
  1415. mcu_sim_parse_args(argc, argv);
  1416. #endif
  1417. sysinit();
  1418. /* set dummy wallclock time. */
  1419. struct os_timeval utctime;
  1420. struct os_timezone tz;
  1421. utctime.tv_sec = 1521725159; /* dummy time: 2018-03-22T13:25:59+00:00 */
  1422. utctime.tv_usec = 0;
  1423. tz.tz_minuteswest = 0;
  1424. tz.tz_dsttime = 0;
  1425. os_settimeofday(&utctime, &tz);
  1426. #endif
  1427. #ifdef WOLFSSL_ZEPHYR
  1428. /* set dummy wallclock time. */
  1429. struct timespec utctime;
  1430. utctime.tv_sec = 1521725159; /* dummy time: 2018-03-22T13:25:59+00:00 */
  1431. utctime.tv_nsec = 0;
  1432. clock_settime(CLOCK_REALTIME, &utctime);
  1433. #endif
  1434. #ifdef DEVKITPRO
  1435. void *framebuffer;
  1436. GXRModeObj *rmode = NULL;
  1437. VIDEO_Init();
  1438. WPAD_Init();
  1439. rmode = VIDEO_GetPreferredMode(NULL);
  1440. #pragma GCC diagnostic ignored "-Wbad-function-cast"
  1441. framebuffer = MEM_K0_TO_K1(SYS_AllocateFramebuffer(rmode));
  1442. #pragma GCC diagnostic pop
  1443. console_init(framebuffer,20,20,rmode->fbWidth,rmode->xfbHeight,rmode->fbWidth*VI_DISPLAY_PIX_SZ);
  1444. VIDEO_Configure(rmode);
  1445. VIDEO_SetNextFramebuffer(framebuffer);
  1446. VIDEO_SetBlack(FALSE);
  1447. VIDEO_Flush();
  1448. VIDEO_WaitVSync();
  1449. if(rmode->viTVMode&VI_NON_INTERLACE) VIDEO_WaitVSync();
  1450. #endif
  1451. #ifdef HAVE_WNR
  1452. if (wc_InitNetRandom(wnrConfigFile, NULL, 5000) != 0) {
  1453. err_sys("Whitewood netRandom global config failed", -1001);
  1454. return -1002;
  1455. }
  1456. #endif
  1457. #ifndef WOLFSSL_ESPIDF
  1458. args.argc = argc;
  1459. args.argv = argv;
  1460. #endif
  1461. if ((ret = wolfCrypt_Init()) != 0) {
  1462. printf("wolfCrypt_Init failed %d\n", ret);
  1463. err_sys("Error with wolfCrypt_Init!\n", -1003);
  1464. }
  1465. #ifdef WC_RNG_SEED_CB
  1466. wc_SetSeed_Cb(wc_GenerateSeed);
  1467. #endif
  1468. #ifdef HAVE_STACK_SIZE
  1469. StackSizeCheck(&args, wolfcrypt_test);
  1470. #else
  1471. wolfcrypt_test(&args);
  1472. #endif
  1473. if ((ret = wolfCrypt_Cleanup()) != 0) {
  1474. printf("wolfCrypt_Cleanup failed %d\n", ret);
  1475. err_sys("Error with wolfCrypt_Cleanup!\n", -1004);
  1476. }
  1477. #ifdef HAVE_WNR
  1478. if (wc_FreeNetRandom() < 0)
  1479. err_sys("Failed to free netRandom context", -1005);
  1480. #endif /* HAVE_WNR */
  1481. #ifdef DOLPHIN_EMULATOR
  1482. /* Returning from main panics the emulator. Just hang
  1483. * and let the user force quit the emulator window. */
  1484. printf("args.return_code: %d\n", args.return_code);
  1485. printf("Testing complete. You may close the window now\n");
  1486. while (1);
  1487. #endif
  1488. #ifdef WOLFSSL_ESPIDF
  1489. /* ESP_LOGI to print takes up a lot less memory than printf */
  1490. ESP_LOGI("wolfcrypt_test", "Exiting main with return code: % d\n", args.return_code);
  1491. #endif
  1492. /* everything else will use printf */
  1493. #if !defined(WOLFSSL_ESPIDF)
  1494. /* gate this for target platforms wishing to avoid printf reference */
  1495. printf("Exiting main with return code: %d\n", args.return_code);
  1496. #endif
  1497. return args.return_code;
  1498. } /* wolfcrypt_test_main or wolf_test_task */
  1499. #endif /* NO_MAIN_DRIVER */
  1500. /* helper to save DER, convert to PEM and save PEM */
  1501. #if !defined(NO_ASN) && (defined(HAVE_ECC) || !defined(NO_DSA) || \
  1502. (!defined(NO_RSA) && (defined(WOLFSSL_KEY_GEN) || defined(WOLFSSL_CERT_GEN)))) \
  1503. && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  1504. #if !defined(NO_FILESYSTEM) && !defined(NO_WRITE_TEMP_FILES)
  1505. #define SaveDerAndPem(d, dSz, fD, fP, pT, eB) _SaveDerAndPem(d, dSz, fD, fP, pT, eB)
  1506. #else
  1507. #define SaveDerAndPem(d, dSz, fD, fP, pT, eB) _SaveDerAndPem(d, dSz, NULL, NULL, pT, eB)
  1508. #endif
  1509. static int _SaveDerAndPem(const byte* der, int derSz,
  1510. const char* fileDer, const char* filePem, int pemType, int errBase)
  1511. {
  1512. #if !defined(NO_FILESYSTEM) && !defined(NO_WRITE_TEMP_FILES)
  1513. int ret;
  1514. XFILE derFile;
  1515. derFile = XFOPEN(fileDer, "wb");
  1516. if (!derFile) {
  1517. return errBase + 0;
  1518. }
  1519. ret = (int)XFWRITE(der, 1, derSz, derFile);
  1520. XFCLOSE(derFile);
  1521. if (ret != derSz) {
  1522. return errBase + 1;
  1523. }
  1524. #endif
  1525. #ifdef WOLFSSL_DER_TO_PEM
  1526. if (filePem) {
  1527. #if !defined(NO_FILESYSTEM) && !defined(NO_WRITE_TEMP_FILES)
  1528. XFILE pemFile;
  1529. #endif
  1530. byte* pem;
  1531. int pemSz;
  1532. /* calculate PEM size */
  1533. pemSz = wc_DerToPem(der, derSz, NULL, 0, pemType);
  1534. if (pemSz < 0) {
  1535. return pemSz;
  1536. }
  1537. pem = (byte*)XMALLOC(pemSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  1538. if (pem == NULL) {
  1539. return MEMORY_E;
  1540. }
  1541. /* Convert to PEM */
  1542. pemSz = wc_DerToPem(der, derSz, pem, pemSz, pemType);
  1543. if (pemSz < 0) {
  1544. XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  1545. return errBase + 2;
  1546. }
  1547. #if !defined(NO_FILESYSTEM) && !defined(NO_WRITE_TEMP_FILES)
  1548. pemFile = XFOPEN(filePem, "wb");
  1549. if (!pemFile) {
  1550. XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  1551. return errBase + 3;
  1552. }
  1553. ret = (int)XFWRITE(pem, 1, pemSz, pemFile);
  1554. XFCLOSE(pemFile);
  1555. if (ret != pemSz) {
  1556. XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  1557. return errBase + 4;
  1558. }
  1559. #endif
  1560. XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  1561. }
  1562. #endif /* WOLFSSL_DER_TO_PEM */
  1563. /* suppress unused variable warnings */
  1564. (void)der;
  1565. (void)derSz;
  1566. (void)filePem;
  1567. (void)fileDer;
  1568. (void)pemType;
  1569. (void)errBase;
  1570. return 0;
  1571. }
  1572. #endif /* WOLFSSL_KEY_GEN || WOLFSSL_CERT_GEN */
  1573. WOLFSSL_TEST_SUBROUTINE int error_test(void)
  1574. {
  1575. const char* errStr;
  1576. char out[WOLFSSL_MAX_ERROR_SZ];
  1577. const char* unknownStr = wc_GetErrorString(0);
  1578. #ifdef NO_ERROR_STRINGS
  1579. /* Ensure a valid error code's string matches an invalid code's.
  1580. * The string is that error strings are not available.
  1581. */
  1582. errStr = wc_GetErrorString(OPEN_RAN_E);
  1583. wc_ErrorString(OPEN_RAN_E, out);
  1584. if (XSTRCMP(errStr, unknownStr) != 0)
  1585. return -1100;
  1586. if (XSTRCMP(out, unknownStr) != 0)
  1587. return -1101;
  1588. #else
  1589. int i;
  1590. int j = 0;
  1591. /* Values that are not or no longer error codes. */
  1592. int missing[] = { -122, -123, -124, -127, -128, -129, -159,
  1593. -163, -164, -165, -166, -167, -168, -169, -233,
  1594. 0 };
  1595. /* Check that all errors have a string and it's the same through the two
  1596. * APIs. Check that the values that are not errors map to the unknown
  1597. * string.
  1598. */
  1599. for (i = MAX_CODE_E-1; i >= WC_LAST_E; i--) {
  1600. errStr = wc_GetErrorString(i);
  1601. wc_ErrorString(i, out);
  1602. if (i != missing[j]) {
  1603. if (XSTRCMP(errStr, unknownStr) == 0)
  1604. return -1102;
  1605. if (XSTRCMP(out, unknownStr) == 0)
  1606. return -1103;
  1607. if (XSTRCMP(errStr, out) != 0)
  1608. return -1104;
  1609. if (XSTRLEN(errStr) >= WOLFSSL_MAX_ERROR_SZ)
  1610. return -1105;
  1611. }
  1612. else {
  1613. j++;
  1614. if (XSTRCMP(errStr, unknownStr) != 0)
  1615. return -1106;
  1616. if (XSTRCMP(out, unknownStr) != 0)
  1617. return -1107;
  1618. }
  1619. }
  1620. /* Check if the next possible value has been given a string. */
  1621. errStr = wc_GetErrorString(i);
  1622. wc_ErrorString(i, out);
  1623. if (XSTRCMP(errStr, unknownStr) != 0)
  1624. return -1108;
  1625. if (XSTRCMP(out, unknownStr) != 0)
  1626. return -1109;
  1627. #endif
  1628. return 0;
  1629. }
  1630. #ifndef NO_CODING
  1631. WOLFSSL_TEST_SUBROUTINE int base64_test(void)
  1632. {
  1633. int ret;
  1634. WOLFSSL_SMALL_STACK_STATIC const byte good[] = "A+Gd\0\0\0";
  1635. WOLFSSL_SMALL_STACK_STATIC const byte goodEnd[] = "A+Gd \r\n";
  1636. WOLFSSL_SMALL_STACK_STATIC const byte good_spaces[] = " A + G d \0";
  1637. byte out[128];
  1638. word32 outLen;
  1639. #ifdef WOLFSSL_BASE64_ENCODE
  1640. byte data[3];
  1641. word32 dataLen;
  1642. byte longData[79] = { 0 };
  1643. WOLFSSL_SMALL_STACK_STATIC const byte symbols[] = "+/A=";
  1644. #endif
  1645. WOLFSSL_SMALL_STACK_STATIC const byte badSmall[] = "AAA!Gdj=";
  1646. WOLFSSL_SMALL_STACK_STATIC const byte badLarge[] = "AAA~Gdj=";
  1647. WOLFSSL_SMALL_STACK_STATIC const byte badEOL[] = "A+Gd!AA";
  1648. WOLFSSL_SMALL_STACK_STATIC const byte badPadding[] = "AA=A";
  1649. WOLFSSL_SMALL_STACK_STATIC const byte badChar[] = ",-.:;<=>?@[\\]^_`";
  1650. byte goodChar[] =
  1651. "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  1652. "abcdefghijklmnopqrstuvwxyz"
  1653. "0123456789+/;";
  1654. byte charTest[] = "A+Gd\0\0\0";
  1655. int i;
  1656. /* Good Base64 encodings. */
  1657. outLen = sizeof(out);
  1658. ret = Base64_Decode(good, sizeof(good), out, &outLen);
  1659. if (ret != 0)
  1660. return -1200;
  1661. outLen = sizeof(out);
  1662. ret = Base64_Decode(goodEnd, sizeof(goodEnd), out, &outLen);
  1663. if (ret != 0)
  1664. return -1201;
  1665. outLen = sizeof(goodChar);
  1666. ret = Base64_Decode(goodChar, sizeof(goodChar), goodChar, &outLen);
  1667. if (ret != 0)
  1668. return -1235;
  1669. if (outLen != 64 / 4 * 3)
  1670. return -1236;
  1671. outLen = sizeof(out);
  1672. ret = Base64_Decode(good_spaces, sizeof(good_spaces), out, &outLen);
  1673. if (ret != 0)
  1674. return -1201;
  1675. /* Bad parameters. */
  1676. outLen = 1;
  1677. ret = Base64_Decode(good, sizeof(good), out, &outLen);
  1678. if (ret != BAD_FUNC_ARG)
  1679. return -1202;
  1680. outLen = sizeof(out);
  1681. ret = Base64_Decode(badEOL, sizeof(badEOL), out, &outLen);
  1682. if (ret != ASN_INPUT_E)
  1683. return -1203;
  1684. outLen = sizeof(out);
  1685. ret = Base64_Decode(badPadding, sizeof(badPadding), out, &outLen);
  1686. if (ret != ASN_INPUT_E)
  1687. return -1203;
  1688. /* Bad character at each offset 0-3. */
  1689. for (i = 0; i < 4; i++) {
  1690. outLen = sizeof(out);
  1691. ret = Base64_Decode(badSmall + i, 4, out, &outLen);
  1692. if (ret != ASN_INPUT_E)
  1693. return -1204 - i;
  1694. ret = Base64_Decode(badLarge + i, 4, out, &outLen);
  1695. if (ret != ASN_INPUT_E)
  1696. return -1214 - i;
  1697. }
  1698. /* Invalid character less than 0x2b */
  1699. for (i = 1; i < 0x2b; i++) {
  1700. outLen = sizeof(out);
  1701. charTest[0] = (byte)i;
  1702. ret = Base64_Decode(charTest, sizeof(charTest), out, &outLen);
  1703. if (ret != ASN_INPUT_E)
  1704. return -1240 - i;
  1705. }
  1706. /* Bad characters in range 0x2b - 0x7a. */
  1707. for (i = 0; i < (int)sizeof(badChar) - 1; i++) {
  1708. outLen = sizeof(out);
  1709. charTest[0] = badChar[i];
  1710. ret = Base64_Decode(charTest, sizeof(charTest), out, &outLen);
  1711. if (ret != ASN_INPUT_E)
  1712. return -1270 - i;
  1713. }
  1714. /* Invalid character greater than 0x7a */
  1715. for (i = 0x7b; i < 0x100; i++) {
  1716. outLen = sizeof(out);
  1717. charTest[0] = (byte)i;
  1718. ret = Base64_Decode(charTest, sizeof(charTest), out, &outLen);
  1719. if (ret != ASN_INPUT_E)
  1720. return -1290 - i;
  1721. }
  1722. #ifdef WOLFSSL_BASE64_ENCODE
  1723. /* Decode and encode all symbols - non-alphanumeric. */
  1724. dataLen = sizeof(data);
  1725. ret = Base64_Decode(symbols, sizeof(symbols), data, &dataLen);
  1726. if (ret != 0)
  1727. return -1224;
  1728. outLen = sizeof(out);
  1729. ret = Base64_Encode(data, dataLen, NULL, &outLen);
  1730. if (ret != LENGTH_ONLY_E)
  1731. return -1225;
  1732. outLen = sizeof(out);
  1733. ret = Base64_Encode(data, dataLen, out, &outLen);
  1734. if (ret != 0)
  1735. return -1226;
  1736. outLen = 7;
  1737. ret = Base64_EncodeEsc(data, dataLen, out, &outLen);
  1738. if (ret != BUFFER_E)
  1739. return -1227;
  1740. outLen = sizeof(out);
  1741. ret = Base64_EncodeEsc(data, dataLen, NULL, &outLen);
  1742. if (ret != LENGTH_ONLY_E)
  1743. return -1228;
  1744. outLen = sizeof(out);
  1745. ret = Base64_EncodeEsc(data, dataLen, out, &outLen);
  1746. if (ret != 0)
  1747. return -1229;
  1748. outLen = sizeof(out);
  1749. ret = Base64_Encode_NoNl(data, dataLen, out, &outLen);
  1750. if (ret != 0)
  1751. return -1230;
  1752. /* Data that results in an encoding longer than one line. */
  1753. outLen = sizeof(out);
  1754. dataLen = sizeof(longData);
  1755. ret = Base64_Encode(longData, dataLen, out, &outLen);
  1756. if (ret != 0)
  1757. return -1231;
  1758. outLen = sizeof(out);
  1759. ret = Base64_EncodeEsc(longData, dataLen, out, &outLen);
  1760. if (ret != 0)
  1761. return -1232;
  1762. outLen = sizeof(out);
  1763. ret = Base64_Encode_NoNl(longData, dataLen, out, &outLen);
  1764. if (ret != 0)
  1765. return -1233;
  1766. #endif
  1767. return 0;
  1768. }
  1769. #ifdef WOLFSSL_BASE16
  1770. WOLFSSL_TEST_SUBROUTINE int base16_test(void)
  1771. {
  1772. int ret;
  1773. WOLFSSL_SMALL_STACK_STATIC const byte testData[] = "SomeDataToEncode\n";
  1774. WOLFSSL_SMALL_STACK_STATIC const byte encodedTestData[] = "536F6D6544617461546F456E636F64650A00";
  1775. byte encoded[40];
  1776. word32 encodedLen;
  1777. byte plain[40];
  1778. word32 len;
  1779. /* length returned includes null termination */
  1780. encodedLen = sizeof(encoded);
  1781. ret = Base16_Encode(testData, sizeof(testData), encoded, &encodedLen);
  1782. if (ret != 0)
  1783. return -1300;
  1784. len = (word32)XSTRLEN((char*)encoded);
  1785. if (len != encodedLen - 1)
  1786. return -1301;
  1787. len = sizeof(plain);
  1788. ret = Base16_Decode(encoded, encodedLen - 1, plain, &len);
  1789. if (ret != 0)
  1790. return -1302;
  1791. if (len != sizeof(testData) || XMEMCMP(testData, plain, len) != 0)
  1792. return -1303;
  1793. if (encodedLen != sizeof(encodedTestData) ||
  1794. XMEMCMP(encoded, encodedTestData, encodedLen) != 0) {
  1795. return -1304;
  1796. }
  1797. return 0;
  1798. }
  1799. #endif /* WOLFSSL_BASE16 */
  1800. #endif /* !NO_CODING */
  1801. #ifndef NO_ASN
  1802. WOLFSSL_TEST_SUBROUTINE int asn_test(void)
  1803. {
  1804. int ret;
  1805. /* ASN1 encoded date buffer */
  1806. WOLFSSL_SMALL_STACK_STATIC const byte dateBuf[] = {0x17, 0x0d, 0x31, 0x36, 0x30, 0x38, 0x31, 0x31,
  1807. 0x32, 0x30, 0x30, 0x37, 0x33, 0x37, 0x5a};
  1808. byte format;
  1809. int length;
  1810. const byte* datePart;
  1811. #ifndef NO_ASN_TIME
  1812. struct tm timearg;
  1813. time_t now;
  1814. #endif
  1815. ret = wc_GetDateInfo(dateBuf, (int)sizeof(dateBuf), &datePart, &format,
  1816. &length);
  1817. if (ret != 0)
  1818. return -1400;
  1819. #ifndef NO_ASN_TIME
  1820. /* Parameter Validation tests. */
  1821. if (wc_GetTime(NULL, sizeof(now)) != BAD_FUNC_ARG)
  1822. return -1401;
  1823. if (wc_GetTime(&now, 0) != BUFFER_E)
  1824. return -1402;
  1825. now = 0;
  1826. if (wc_GetTime(&now, sizeof(now)) != 0) {
  1827. return -1403;
  1828. }
  1829. if (now == 0) {
  1830. printf("RTC/Time not set!\n");
  1831. return -1404;
  1832. }
  1833. ret = wc_GetDateAsCalendarTime(datePart, length, format, &timearg);
  1834. if (ret != 0)
  1835. return -1405;
  1836. #endif /* !NO_ASN_TIME */
  1837. return 0;
  1838. }
  1839. #endif /* !NO_ASN */
  1840. #ifdef WOLFSSL_MD2
  1841. WOLFSSL_TEST_SUBROUTINE int md2_test(void)
  1842. {
  1843. int ret = 0;
  1844. Md2 md2;
  1845. byte hash[MD2_DIGEST_SIZE];
  1846. testVector a, b, c, d, e, f, g;
  1847. testVector test_md2[7];
  1848. int times = sizeof(test_md2) / sizeof(testVector), i;
  1849. a.input = "";
  1850. a.output = "\x83\x50\xe5\xa3\xe2\x4c\x15\x3d\xf2\x27\x5c\x9f\x80\x69"
  1851. "\x27\x73";
  1852. a.inLen = XSTRLEN(a.input);
  1853. a.outLen = MD2_DIGEST_SIZE;
  1854. b.input = "a";
  1855. b.output = "\x32\xec\x01\xec\x4a\x6d\xac\x72\xc0\xab\x96\xfb\x34\xc0"
  1856. "\xb5\xd1";
  1857. b.inLen = XSTRLEN(b.input);
  1858. b.outLen = MD2_DIGEST_SIZE;
  1859. c.input = "abc";
  1860. c.output = "\xda\x85\x3b\x0d\x3f\x88\xd9\x9b\x30\x28\x3a\x69\xe6\xde"
  1861. "\xd6\xbb";
  1862. c.inLen = XSTRLEN(c.input);
  1863. c.outLen = MD2_DIGEST_SIZE;
  1864. d.input = "message digest";
  1865. d.output = "\xab\x4f\x49\x6b\xfb\x2a\x53\x0b\x21\x9f\xf3\x30\x31\xfe"
  1866. "\x06\xb0";
  1867. d.inLen = XSTRLEN(d.input);
  1868. d.outLen = MD2_DIGEST_SIZE;
  1869. e.input = "abcdefghijklmnopqrstuvwxyz";
  1870. e.output = "\x4e\x8d\xdf\xf3\x65\x02\x92\xab\x5a\x41\x08\xc3\xaa\x47"
  1871. "\x94\x0b";
  1872. e.inLen = XSTRLEN(e.input);
  1873. e.outLen = MD2_DIGEST_SIZE;
  1874. f.input = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345"
  1875. "6789";
  1876. f.output = "\xda\x33\xde\xf2\xa4\x2d\xf1\x39\x75\x35\x28\x46\xc3\x03"
  1877. "\x38\xcd";
  1878. f.inLen = XSTRLEN(f.input);
  1879. f.outLen = MD2_DIGEST_SIZE;
  1880. g.input = "1234567890123456789012345678901234567890123456789012345678"
  1881. "9012345678901234567890";
  1882. g.output = "\xd5\x97\x6f\x79\xd8\x3d\x3a\x0d\xc9\x80\x6c\x3c\x66\xf3"
  1883. "\xef\xd8";
  1884. g.inLen = XSTRLEN(g.input);
  1885. g.outLen = MD2_DIGEST_SIZE;
  1886. test_md2[0] = a;
  1887. test_md2[1] = b;
  1888. test_md2[2] = c;
  1889. test_md2[3] = d;
  1890. test_md2[4] = e;
  1891. test_md2[5] = f;
  1892. test_md2[6] = g;
  1893. wc_InitMd2(&md2);
  1894. for (i = 0; i < times; ++i) {
  1895. wc_Md2Update(&md2, (byte*)test_md2[i].input, (word32)test_md2[i].inLen);
  1896. wc_Md2Final(&md2, hash);
  1897. if (XMEMCMP(hash, test_md2[i].output, MD2_DIGEST_SIZE) != 0)
  1898. return -1500 - i;
  1899. }
  1900. for (i = 0; i < times; ++i) {
  1901. ret = wc_Md2Hash((byte*)test_md2[i].input, (word32)test_md2[i].inLen, hash);
  1902. if (ret != 0) {
  1903. return -1507 - i;
  1904. }
  1905. if (XMEMCMP(hash, test_md2[i].output, MD2_DIGEST_SIZE) != 0) {
  1906. return -1507 - i;
  1907. }
  1908. }
  1909. return 0;
  1910. }
  1911. #endif
  1912. #ifndef NO_MD5
  1913. WOLFSSL_TEST_SUBROUTINE int md5_test(void)
  1914. {
  1915. int ret = 0;
  1916. wc_Md5 md5, md5Copy;
  1917. byte hash[WC_MD5_DIGEST_SIZE];
  1918. byte hashcopy[WC_MD5_DIGEST_SIZE];
  1919. testVector a, b, c, d, e, f;
  1920. testVector test_md5[6];
  1921. int times = sizeof(test_md5) / sizeof(testVector), i;
  1922. a.input = "";
  1923. a.output = "\xd4\x1d\x8c\xd9\x8f\x00\xb2\x04\xe9\x80\x09\x98\xec\xf8\x42"
  1924. "\x7e";
  1925. a.inLen = XSTRLEN(a.input);
  1926. a.outLen = WC_MD5_DIGEST_SIZE;
  1927. b.input = "abc";
  1928. b.output = "\x90\x01\x50\x98\x3c\xd2\x4f\xb0\xd6\x96\x3f\x7d\x28\xe1\x7f"
  1929. "\x72";
  1930. b.inLen = XSTRLEN(b.input);
  1931. b.outLen = WC_MD5_DIGEST_SIZE;
  1932. c.input = "message digest";
  1933. c.output = "\xf9\x6b\x69\x7d\x7c\xb7\x93\x8d\x52\x5a\x2f\x31\xaa\xf1\x61"
  1934. "\xd0";
  1935. c.inLen = XSTRLEN(c.input);
  1936. c.outLen = WC_MD5_DIGEST_SIZE;
  1937. d.input = "abcdefghijklmnopqrstuvwxyz";
  1938. d.output = "\xc3\xfc\xd3\xd7\x61\x92\xe4\x00\x7d\xfb\x49\x6c\xca\x67\xe1"
  1939. "\x3b";
  1940. d.inLen = XSTRLEN(d.input);
  1941. d.outLen = WC_MD5_DIGEST_SIZE;
  1942. e.input = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345"
  1943. "6789";
  1944. e.output = "\xd1\x74\xab\x98\xd2\x77\xd9\xf5\xa5\x61\x1c\x2c\x9f\x41\x9d"
  1945. "\x9f";
  1946. e.inLen = XSTRLEN(e.input);
  1947. e.outLen = WC_MD5_DIGEST_SIZE;
  1948. f.input = "1234567890123456789012345678901234567890123456789012345678"
  1949. "9012345678901234567890";
  1950. f.output = "\x57\xed\xf4\xa2\x2b\xe3\xc9\x55\xac\x49\xda\x2e\x21\x07\xb6"
  1951. "\x7a";
  1952. f.inLen = XSTRLEN(f.input);
  1953. f.outLen = WC_MD5_DIGEST_SIZE;
  1954. test_md5[0] = a;
  1955. test_md5[1] = b;
  1956. test_md5[2] = c;
  1957. test_md5[3] = d;
  1958. test_md5[4] = e;
  1959. test_md5[5] = f;
  1960. ret = wc_InitMd5_ex(&md5, HEAP_HINT, devId);
  1961. if (ret != 0)
  1962. return -1600;
  1963. ret = wc_InitMd5_ex(&md5Copy, HEAP_HINT, devId);
  1964. if (ret != 0) {
  1965. wc_Md5Free(&md5);
  1966. return -1601;
  1967. }
  1968. for (i = 0; i < times; ++i) {
  1969. ret = wc_Md5Update(&md5, (byte*)test_md5[i].input,
  1970. (word32)test_md5[i].inLen);
  1971. if (ret != 0)
  1972. ERROR_OUT(-1602 - i, exit);
  1973. ret = wc_Md5GetHash(&md5, hashcopy);
  1974. if (ret != 0)
  1975. ERROR_OUT(-1603 - i, exit);
  1976. ret = wc_Md5Copy(&md5, &md5Copy);
  1977. if (ret != 0)
  1978. ERROR_OUT(-1604 - i, exit);
  1979. ret = wc_Md5Final(&md5, hash);
  1980. if (ret != 0)
  1981. ERROR_OUT(-1605 - i, exit);
  1982. wc_Md5Free(&md5Copy);
  1983. if (XMEMCMP(hash, test_md5[i].output, WC_MD5_DIGEST_SIZE) != 0)
  1984. ERROR_OUT(-1606 - i, exit);
  1985. if (XMEMCMP(hash, hashcopy, WC_MD5_DIGEST_SIZE) != 0)
  1986. ERROR_OUT(-1607 - i, exit);
  1987. }
  1988. #ifndef NO_LARGE_HASH_TEST
  1989. /* BEGIN LARGE HASH TEST */ {
  1990. byte large_input[1024];
  1991. const char* large_digest =
  1992. "\x44\xd0\x88\xce\xf1\x36\xd1\x78\xe9\xc8\xba\x84\xc3\xfd\xf6\xca";
  1993. for (i = 0; i < (int)sizeof(large_input); i++) {
  1994. large_input[i] = (byte)(i & 0xFF);
  1995. }
  1996. times = 100;
  1997. #ifdef WOLFSSL_PIC32MZ_HASH
  1998. wc_Md5SizeSet(&md5, times * sizeof(large_input));
  1999. #endif
  2000. for (i = 0; i < times; ++i) {
  2001. ret = wc_Md5Update(&md5, (byte*)large_input,
  2002. (word32)sizeof(large_input));
  2003. if (ret != 0)
  2004. ERROR_OUT(-1608, exit);
  2005. }
  2006. ret = wc_Md5Final(&md5, hash);
  2007. if (ret != 0)
  2008. ERROR_OUT(-1609, exit);
  2009. if (XMEMCMP(hash, large_digest, WC_MD5_DIGEST_SIZE) != 0)
  2010. ERROR_OUT(-1610, exit);
  2011. } /* END LARGE HASH TEST */
  2012. #endif /* NO_LARGE_HASH_TEST */
  2013. exit:
  2014. wc_Md5Free(&md5);
  2015. wc_Md5Free(&md5Copy);
  2016. return ret;
  2017. }
  2018. #endif /* NO_MD5 */
  2019. #ifndef NO_MD4
  2020. WOLFSSL_TEST_SUBROUTINE int md4_test(void)
  2021. {
  2022. Md4 md4;
  2023. byte hash[MD4_DIGEST_SIZE];
  2024. testVector a, b, c, d, e, f, g;
  2025. testVector test_md4[7];
  2026. int times = sizeof(test_md4) / sizeof(testVector), i;
  2027. a.input = "";
  2028. a.output = "\x31\xd6\xcf\xe0\xd1\x6a\xe9\x31\xb7\x3c\x59\xd7\xe0\xc0\x89"
  2029. "\xc0";
  2030. a.inLen = XSTRLEN(a.input);
  2031. a.outLen = MD4_DIGEST_SIZE;
  2032. b.input = "a";
  2033. b.output = "\xbd\xe5\x2c\xb3\x1d\xe3\x3e\x46\x24\x5e\x05\xfb\xdb\xd6\xfb"
  2034. "\x24";
  2035. b.inLen = XSTRLEN(b.input);
  2036. b.outLen = MD4_DIGEST_SIZE;
  2037. c.input = "abc";
  2038. c.output = "\xa4\x48\x01\x7a\xaf\x21\xd8\x52\x5f\xc1\x0a\xe8\x7a\xa6\x72"
  2039. "\x9d";
  2040. c.inLen = XSTRLEN(c.input);
  2041. c.outLen = MD4_DIGEST_SIZE;
  2042. d.input = "message digest";
  2043. d.output = "\xd9\x13\x0a\x81\x64\x54\x9f\xe8\x18\x87\x48\x06\xe1\xc7\x01"
  2044. "\x4b";
  2045. d.inLen = XSTRLEN(d.input);
  2046. d.outLen = MD4_DIGEST_SIZE;
  2047. e.input = "abcdefghijklmnopqrstuvwxyz";
  2048. e.output = "\xd7\x9e\x1c\x30\x8a\xa5\xbb\xcd\xee\xa8\xed\x63\xdf\x41\x2d"
  2049. "\xa9";
  2050. e.inLen = XSTRLEN(e.input);
  2051. e.outLen = MD4_DIGEST_SIZE;
  2052. f.input = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345"
  2053. "6789";
  2054. f.output = "\x04\x3f\x85\x82\xf2\x41\xdb\x35\x1c\xe6\x27\xe1\x53\xe7\xf0"
  2055. "\xe4";
  2056. f.inLen = XSTRLEN(f.input);
  2057. f.outLen = MD4_DIGEST_SIZE;
  2058. g.input = "1234567890123456789012345678901234567890123456789012345678"
  2059. "9012345678901234567890";
  2060. g.output = "\xe3\x3b\x4d\xdc\x9c\x38\xf2\x19\x9c\x3e\x7b\x16\x4f\xcc\x05"
  2061. "\x36";
  2062. g.inLen = XSTRLEN(g.input);
  2063. g.outLen = MD4_DIGEST_SIZE;
  2064. test_md4[0] = a;
  2065. test_md4[1] = b;
  2066. test_md4[2] = c;
  2067. test_md4[3] = d;
  2068. test_md4[4] = e;
  2069. test_md4[5] = f;
  2070. test_md4[6] = g;
  2071. wc_InitMd4(&md4);
  2072. for (i = 0; i < times; ++i) {
  2073. wc_Md4Update(&md4, (byte*)test_md4[i].input, (word32)test_md4[i].inLen);
  2074. wc_Md4Final(&md4, hash);
  2075. if (XMEMCMP(hash, test_md4[i].output, MD4_DIGEST_SIZE) != 0)
  2076. return -1700 - i;
  2077. }
  2078. return 0;
  2079. }
  2080. #endif /* NO_MD4 */
  2081. #ifndef NO_SHA
  2082. WOLFSSL_TEST_SUBROUTINE int sha_test(void)
  2083. {
  2084. int ret = 0;
  2085. wc_Sha sha, shaCopy;
  2086. byte hash[WC_SHA_DIGEST_SIZE];
  2087. byte hashcopy[WC_SHA_DIGEST_SIZE];
  2088. testVector a, b, c, d, e;
  2089. testVector test_sha[5];
  2090. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2091. a.input = "";
  2092. a.output = "\xda\x39\xa3\xee\x5e\x6b\x4b\x0d\x32\x55\xbf\xef\x95\x60\x18"
  2093. "\x90\xaf\xd8\x07\x09";
  2094. a.inLen = XSTRLEN(a.input);
  2095. a.outLen = WC_SHA_DIGEST_SIZE;
  2096. b.input = "abc";
  2097. b.output = "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E\x25\x71\x78\x50\xC2"
  2098. "\x6C\x9C\xD0\xD8\x9D";
  2099. b.inLen = XSTRLEN(b.input);
  2100. b.outLen = WC_SHA_DIGEST_SIZE;
  2101. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  2102. c.output = "\x84\x98\x3E\x44\x1C\x3B\xD2\x6E\xBA\xAE\x4A\xA1\xF9\x51\x29"
  2103. "\xE5\xE5\x46\x70\xF1";
  2104. c.inLen = XSTRLEN(c.input);
  2105. c.outLen = WC_SHA_DIGEST_SIZE;
  2106. d.input = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  2107. "aaaaaa";
  2108. d.output = "\x00\x98\xBA\x82\x4B\x5C\x16\x42\x7B\xD7\xA1\x12\x2A\x5A\x44"
  2109. "\x2A\x25\xEC\x64\x4D";
  2110. d.inLen = XSTRLEN(d.input);
  2111. d.outLen = WC_SHA_DIGEST_SIZE;
  2112. e.input = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  2113. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  2114. "aaaaaaaaaa";
  2115. e.output = "\xAD\x5B\x3F\xDB\xCB\x52\x67\x78\xC2\x83\x9D\x2F\x15\x1E\xA7"
  2116. "\x53\x99\x5E\x26\xA0";
  2117. e.inLen = XSTRLEN(e.input);
  2118. e.outLen = WC_SHA_DIGEST_SIZE;
  2119. test_sha[0] = a;
  2120. test_sha[1] = b;
  2121. test_sha[2] = c;
  2122. test_sha[3] = d;
  2123. test_sha[4] = e;
  2124. ret = wc_InitSha_ex(&sha, HEAP_HINT, devId);
  2125. if (ret != 0)
  2126. return -1800;
  2127. ret = wc_InitSha_ex(&shaCopy, HEAP_HINT, devId);
  2128. if (ret != 0) {
  2129. wc_ShaFree(&sha);
  2130. return -1801;
  2131. }
  2132. for (i = 0; i < times; ++i) {
  2133. ret = wc_ShaUpdate(&sha, (byte*)test_sha[i].input,
  2134. (word32)test_sha[i].inLen);
  2135. if (ret != 0)
  2136. ERROR_OUT(-1802 - i, exit);
  2137. ret = wc_ShaGetHash(&sha, hashcopy);
  2138. if (ret != 0)
  2139. ERROR_OUT(-1803 - i, exit);
  2140. ret = wc_ShaCopy(&sha, &shaCopy);
  2141. if (ret != 0)
  2142. ERROR_OUT(-1804 - i, exit);
  2143. ret = wc_ShaFinal(&sha, hash);
  2144. if (ret != 0)
  2145. ERROR_OUT(-1805 - i, exit);
  2146. wc_ShaFree(&shaCopy);
  2147. if (XMEMCMP(hash, test_sha[i].output, WC_SHA_DIGEST_SIZE) != 0)
  2148. ERROR_OUT(-1806 - i, exit);
  2149. if (XMEMCMP(hash, hashcopy, WC_SHA_DIGEST_SIZE) != 0)
  2150. ERROR_OUT(-1807 - i, exit);
  2151. }
  2152. #ifndef NO_LARGE_HASH_TEST
  2153. /* BEGIN LARGE HASH TEST */ {
  2154. byte large_input[1024];
  2155. #if defined(WOLFSSL_RENESAS_TSIP) || defined(WOLFSSL_RENESAS_SCEPROTECT) || \
  2156. defined(HASH_SIZE_LIMIT)
  2157. const char* large_digest =
  2158. "\x1d\x6a\x5a\xf6\xe5\x7c\x86\xce\x7f\x7c\xaf\xd5\xdb\x08\xcd\x59"
  2159. "\x15\x8c\x6d\xb6";
  2160. #else
  2161. const char* large_digest =
  2162. "\x8b\x77\x02\x48\x39\xe8\xdb\xd3\x9a\xf4\x05\x24\x66\x12\x2d\x9e"
  2163. "\xc5\xd9\x0a\xac";
  2164. #endif
  2165. for (i = 0; i < (int)sizeof(large_input); i++) {
  2166. large_input[i] = (byte)(i & 0xFF);
  2167. }
  2168. #if defined(WOLFSSL_RENESAS_TSIP) || defined(WOLFSSL_RENESAS_SCEPROTECT) || \
  2169. defined(HASH_SIZE_LIMIT)
  2170. times = 20;
  2171. #else
  2172. times = 100;
  2173. #endif
  2174. #ifdef WOLFSSL_PIC32MZ_HASH
  2175. wc_ShaSizeSet(&sha, times * sizeof(large_input));
  2176. #endif
  2177. for (i = 0; i < times; ++i) {
  2178. ret = wc_ShaUpdate(&sha, (byte*)large_input,
  2179. (word32)sizeof(large_input));
  2180. if (ret != 0)
  2181. ERROR_OUT(-1808, exit);
  2182. }
  2183. ret = wc_ShaFinal(&sha, hash);
  2184. if (ret != 0)
  2185. ERROR_OUT(-1809, exit);
  2186. if (XMEMCMP(hash, large_digest, WC_SHA_DIGEST_SIZE) != 0)
  2187. ERROR_OUT(-1810, exit);
  2188. } /* END LARGE HASH TEST */
  2189. #endif /* NO_LARGE_HASH_TEST */
  2190. exit:
  2191. wc_ShaFree(&sha);
  2192. wc_ShaFree(&shaCopy);
  2193. return ret;
  2194. }
  2195. #endif /* NO_SHA */
  2196. #ifdef WOLFSSL_RIPEMD
  2197. WOLFSSL_TEST_SUBROUTINE int ripemd_test(void)
  2198. {
  2199. RipeMd ripemd;
  2200. int ret;
  2201. byte hash[RIPEMD_DIGEST_SIZE];
  2202. testVector a, b, c, d;
  2203. testVector test_ripemd[4];
  2204. int times = sizeof(test_ripemd) / sizeof(struct testVector), i;
  2205. a.input = "abc";
  2206. a.output = "\x8e\xb2\x08\xf7\xe0\x5d\x98\x7a\x9b\x04\x4a\x8e\x98\xc6"
  2207. "\xb0\x87\xf1\x5a\x0b\xfc";
  2208. a.inLen = XSTRLEN(a.input);
  2209. a.outLen = RIPEMD_DIGEST_SIZE;
  2210. b.input = "message digest";
  2211. b.output = "\x5d\x06\x89\xef\x49\xd2\xfa\xe5\x72\xb8\x81\xb1\x23\xa8"
  2212. "\x5f\xfa\x21\x59\x5f\x36";
  2213. b.inLen = XSTRLEN(b.input);
  2214. b.outLen = RIPEMD_DIGEST_SIZE;
  2215. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  2216. c.output = "\x12\xa0\x53\x38\x4a\x9c\x0c\x88\xe4\x05\xa0\x6c\x27\xdc"
  2217. "\xf4\x9a\xda\x62\xeb\x2b";
  2218. c.inLen = XSTRLEN(c.input);
  2219. c.outLen = RIPEMD_DIGEST_SIZE;
  2220. d.input = "12345678901234567890123456789012345678901234567890123456"
  2221. "789012345678901234567890";
  2222. d.output = "\x9b\x75\x2e\x45\x57\x3d\x4b\x39\xf4\xdb\xd3\x32\x3c\xab"
  2223. "\x82\xbf\x63\x32\x6b\xfb";
  2224. d.inLen = XSTRLEN(d.input);
  2225. d.outLen = RIPEMD_DIGEST_SIZE;
  2226. test_ripemd[0] = a;
  2227. test_ripemd[1] = b;
  2228. test_ripemd[2] = c;
  2229. test_ripemd[3] = d;
  2230. ret = wc_InitRipeMd(&ripemd);
  2231. if (ret != 0) {
  2232. return -1900;
  2233. }
  2234. for (i = 0; i < times; ++i) {
  2235. ret = wc_RipeMdUpdate(&ripemd, (byte*)test_ripemd[i].input,
  2236. (word32)test_ripemd[i].inLen);
  2237. if (ret != 0) {
  2238. return -1901 - i;
  2239. }
  2240. ret = wc_RipeMdFinal(&ripemd, hash);
  2241. if (ret != 0) {
  2242. return -1911 - i;
  2243. }
  2244. if (XMEMCMP(hash, test_ripemd[i].output, RIPEMD_DIGEST_SIZE) != 0)
  2245. return -1921 - i;
  2246. }
  2247. return 0;
  2248. }
  2249. #endif /* WOLFSSL_RIPEMD */
  2250. #ifdef HAVE_BLAKE2
  2251. #define BLAKE2B_TESTS 3
  2252. static const byte blake2b_vec[BLAKE2B_TESTS][BLAKE2B_OUTBYTES] =
  2253. {
  2254. {
  2255. 0x78, 0x6A, 0x02, 0xF7, 0x42, 0x01, 0x59, 0x03,
  2256. 0xC6, 0xC6, 0xFD, 0x85, 0x25, 0x52, 0xD2, 0x72,
  2257. 0x91, 0x2F, 0x47, 0x40, 0xE1, 0x58, 0x47, 0x61,
  2258. 0x8A, 0x86, 0xE2, 0x17, 0xF7, 0x1F, 0x54, 0x19,
  2259. 0xD2, 0x5E, 0x10, 0x31, 0xAF, 0xEE, 0x58, 0x53,
  2260. 0x13, 0x89, 0x64, 0x44, 0x93, 0x4E, 0xB0, 0x4B,
  2261. 0x90, 0x3A, 0x68, 0x5B, 0x14, 0x48, 0xB7, 0x55,
  2262. 0xD5, 0x6F, 0x70, 0x1A, 0xFE, 0x9B, 0xE2, 0xCE
  2263. },
  2264. {
  2265. 0x2F, 0xA3, 0xF6, 0x86, 0xDF, 0x87, 0x69, 0x95,
  2266. 0x16, 0x7E, 0x7C, 0x2E, 0x5D, 0x74, 0xC4, 0xC7,
  2267. 0xB6, 0xE4, 0x8F, 0x80, 0x68, 0xFE, 0x0E, 0x44,
  2268. 0x20, 0x83, 0x44, 0xD4, 0x80, 0xF7, 0x90, 0x4C,
  2269. 0x36, 0x96, 0x3E, 0x44, 0x11, 0x5F, 0xE3, 0xEB,
  2270. 0x2A, 0x3A, 0xC8, 0x69, 0x4C, 0x28, 0xBC, 0xB4,
  2271. 0xF5, 0xA0, 0xF3, 0x27, 0x6F, 0x2E, 0x79, 0x48,
  2272. 0x7D, 0x82, 0x19, 0x05, 0x7A, 0x50, 0x6E, 0x4B
  2273. },
  2274. {
  2275. 0x1C, 0x08, 0x79, 0x8D, 0xC6, 0x41, 0xAB, 0xA9,
  2276. 0xDE, 0xE4, 0x35, 0xE2, 0x25, 0x19, 0xA4, 0x72,
  2277. 0x9A, 0x09, 0xB2, 0xBF, 0xE0, 0xFF, 0x00, 0xEF,
  2278. 0x2D, 0xCD, 0x8E, 0xD6, 0xF8, 0xA0, 0x7D, 0x15,
  2279. 0xEA, 0xF4, 0xAE, 0xE5, 0x2B, 0xBF, 0x18, 0xAB,
  2280. 0x56, 0x08, 0xA6, 0x19, 0x0F, 0x70, 0xB9, 0x04,
  2281. 0x86, 0xC8, 0xA7, 0xD4, 0x87, 0x37, 0x10, 0xB1,
  2282. 0x11, 0x5D, 0x3D, 0xEB, 0xBB, 0x43, 0x27, 0xB5
  2283. }
  2284. };
  2285. WOLFSSL_TEST_SUBROUTINE int blake2b_test(void)
  2286. {
  2287. Blake2b b2b;
  2288. byte digest[64];
  2289. byte input[64];
  2290. int i, ret;
  2291. for (i = 0; i < (int)sizeof(input); i++)
  2292. input[i] = (byte)i;
  2293. for (i = 0; i < BLAKE2B_TESTS; i++) {
  2294. ret = wc_InitBlake2b(&b2b, 64);
  2295. if (ret != 0)
  2296. return -2000 - i;
  2297. ret = wc_Blake2bUpdate(&b2b, input, i);
  2298. if (ret != 0)
  2299. return -2010 - 1;
  2300. ret = wc_Blake2bFinal(&b2b, digest, 64);
  2301. if (ret != 0)
  2302. return -2020 - i;
  2303. if (XMEMCMP(digest, blake2b_vec[i], 64) != 0) {
  2304. return -2030 - i;
  2305. }
  2306. }
  2307. return 0;
  2308. }
  2309. #endif /* HAVE_BLAKE2 */
  2310. #ifdef HAVE_BLAKE2S
  2311. #define BLAKE2S_TESTS 3
  2312. static const byte blake2s_vec[BLAKE2S_TESTS][BLAKE2S_OUTBYTES] =
  2313. {
  2314. {
  2315. 0x69, 0x21, 0x7a, 0x30, 0x79, 0x90, 0x80, 0x94,
  2316. 0xe1, 0x11, 0x21, 0xd0, 0x42, 0x35, 0x4a, 0x7c,
  2317. 0x1f, 0x55, 0xb6, 0x48, 0x2c, 0xa1, 0xa5, 0x1e,
  2318. 0x1b, 0x25, 0x0d, 0xfd, 0x1e, 0xd0, 0xee, 0xf9,
  2319. },
  2320. {
  2321. 0xe3, 0x4d, 0x74, 0xdb, 0xaf, 0x4f, 0xf4, 0xc6,
  2322. 0xab, 0xd8, 0x71, 0xcc, 0x22, 0x04, 0x51, 0xd2,
  2323. 0xea, 0x26, 0x48, 0x84, 0x6c, 0x77, 0x57, 0xfb,
  2324. 0xaa, 0xc8, 0x2f, 0xe5, 0x1a, 0xd6, 0x4b, 0xea,
  2325. },
  2326. {
  2327. 0xdd, 0xad, 0x9a, 0xb1, 0x5d, 0xac, 0x45, 0x49,
  2328. 0xba, 0x42, 0xf4, 0x9d, 0x26, 0x24, 0x96, 0xbe,
  2329. 0xf6, 0xc0, 0xba, 0xe1, 0xdd, 0x34, 0x2a, 0x88,
  2330. 0x08, 0xf8, 0xea, 0x26, 0x7c, 0x6e, 0x21, 0x0c,
  2331. }
  2332. };
  2333. WOLFSSL_TEST_SUBROUTINE int blake2s_test(void)
  2334. {
  2335. Blake2s b2s;
  2336. byte digest[32];
  2337. byte input[64];
  2338. int i, ret;
  2339. for (i = 0; i < (int)sizeof(input); i++)
  2340. input[i] = (byte)i;
  2341. for (i = 0; i < BLAKE2S_TESTS; i++) {
  2342. ret = wc_InitBlake2s(&b2s, 32);
  2343. if (ret != 0)
  2344. return -2100 - i;
  2345. ret = wc_Blake2sUpdate(&b2s, input, i);
  2346. if (ret != 0)
  2347. return -2110 - 1;
  2348. ret = wc_Blake2sFinal(&b2s, digest, 32);
  2349. if (ret != 0)
  2350. return -2120 - i;
  2351. if (XMEMCMP(digest, blake2s_vec[i], 32) != 0) {
  2352. return -2130 - i;
  2353. }
  2354. }
  2355. return 0;
  2356. }
  2357. #endif /* HAVE_BLAKE2S */
  2358. #ifdef WOLFSSL_SHA224
  2359. WOLFSSL_TEST_SUBROUTINE int sha224_test(void)
  2360. {
  2361. wc_Sha224 sha, shaCopy;
  2362. byte hash[WC_SHA224_DIGEST_SIZE];
  2363. byte hashcopy[WC_SHA224_DIGEST_SIZE];
  2364. int ret = 0;
  2365. testVector a, b, c;
  2366. testVector test_sha[3];
  2367. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2368. a.input = "";
  2369. a.output = "\xd1\x4a\x02\x8c\x2a\x3a\x2b\xc9\x47\x61\x02\xbb\x28\x82\x34"
  2370. "\xc4\x15\xa2\xb0\x1f\x82\x8e\xa6\x2a\xc5\xb3\xe4\x2f";
  2371. a.inLen = XSTRLEN(a.input);
  2372. a.outLen = WC_SHA224_DIGEST_SIZE;
  2373. b.input = "abc";
  2374. b.output = "\x23\x09\x7d\x22\x34\x05\xd8\x22\x86\x42\xa4\x77\xbd\xa2\x55"
  2375. "\xb3\x2a\xad\xbc\xe4\xbd\xa0\xb3\xf7\xe3\x6c\x9d\xa7";
  2376. b.inLen = XSTRLEN(b.input);
  2377. b.outLen = WC_SHA224_DIGEST_SIZE;
  2378. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  2379. c.output = "\x75\x38\x8b\x16\x51\x27\x76\xcc\x5d\xba\x5d\xa1\xfd\x89\x01"
  2380. "\x50\xb0\xc6\x45\x5c\xb4\xf5\x8b\x19\x52\x52\x25\x25";
  2381. c.inLen = XSTRLEN(c.input);
  2382. c.outLen = WC_SHA224_DIGEST_SIZE;
  2383. test_sha[0] = a;
  2384. test_sha[1] = b;
  2385. test_sha[2] = c;
  2386. ret = wc_InitSha224_ex(&sha, HEAP_HINT, devId);
  2387. if (ret != 0)
  2388. return -2200;
  2389. ret = wc_InitSha224_ex(&shaCopy, HEAP_HINT, devId);
  2390. if (ret != 0) {
  2391. wc_Sha224Free(&sha);
  2392. return -2201;
  2393. }
  2394. for (i = 0; i < times; ++i) {
  2395. ret = wc_Sha224Update(&sha, (byte*)test_sha[i].input,
  2396. (word32)test_sha[i].inLen);
  2397. if (ret != 0)
  2398. ERROR_OUT(-2202 - i, exit);
  2399. ret = wc_Sha224GetHash(&sha, hashcopy);
  2400. if (ret != 0)
  2401. ERROR_OUT(-2203 - i, exit);
  2402. ret = wc_Sha224Copy(&sha, &shaCopy);
  2403. if (ret != 0)
  2404. ERROR_OUT(-2204 - i, exit);
  2405. ret = wc_Sha224Final(&sha, hash);
  2406. if (ret != 0)
  2407. ERROR_OUT(-2205 - i, exit);
  2408. wc_Sha224Free(&shaCopy);
  2409. if (XMEMCMP(hash, test_sha[i].output, WC_SHA224_DIGEST_SIZE) != 0)
  2410. ERROR_OUT(-2206 - i, exit);
  2411. if (XMEMCMP(hash, hashcopy, WC_SHA224_DIGEST_SIZE) != 0)
  2412. ERROR_OUT(-2207 - i, exit);
  2413. }
  2414. exit:
  2415. wc_Sha224Free(&sha);
  2416. wc_Sha224Free(&shaCopy);
  2417. return ret;
  2418. }
  2419. #endif
  2420. #ifndef NO_SHA256
  2421. WOLFSSL_TEST_SUBROUTINE int sha256_test(void)
  2422. {
  2423. wc_Sha256 sha, shaCopy;
  2424. byte hash[WC_SHA256_DIGEST_SIZE];
  2425. byte hashcopy[WC_SHA256_DIGEST_SIZE];
  2426. int ret = 0;
  2427. testVector a, b, c;
  2428. testVector test_sha[3];
  2429. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2430. a.input = "";
  2431. a.output = "\xe3\xb0\xc4\x42\x98\xfc\x1c\x14\x9a\xfb\xf4\xc8\x99\x6f\xb9"
  2432. "\x24\x27\xae\x41\xe4\x64\x9b\x93\x4c\xa4\x95\x99\x1b\x78\x52"
  2433. "\xb8\x55";
  2434. a.inLen = XSTRLEN(a.input);
  2435. a.outLen = WC_SHA256_DIGEST_SIZE;
  2436. b.input = "abc";
  2437. b.output = "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  2438. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  2439. "\x15\xAD";
  2440. b.inLen = XSTRLEN(b.input);
  2441. b.outLen = WC_SHA256_DIGEST_SIZE;
  2442. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  2443. c.output = "\x24\x8D\x6A\x61\xD2\x06\x38\xB8\xE5\xC0\x26\x93\x0C\x3E\x60"
  2444. "\x39\xA3\x3C\xE4\x59\x64\xFF\x21\x67\xF6\xEC\xED\xD4\x19\xDB"
  2445. "\x06\xC1";
  2446. c.inLen = XSTRLEN(c.input);
  2447. c.outLen = WC_SHA256_DIGEST_SIZE;
  2448. test_sha[0] = a;
  2449. test_sha[1] = b;
  2450. test_sha[2] = c;
  2451. ret = wc_InitSha256_ex(&sha, HEAP_HINT, devId);
  2452. if (ret != 0)
  2453. return -2300;
  2454. ret = wc_InitSha256_ex(&shaCopy, HEAP_HINT, devId);
  2455. if (ret != 0) {
  2456. wc_Sha256Free(&sha);
  2457. return -2301;
  2458. }
  2459. for (i = 0; i < times; ++i) {
  2460. ret = wc_Sha256Update(&sha, (byte*)test_sha[i].input,
  2461. (word32)test_sha[i].inLen);
  2462. if (ret != 0) {
  2463. ERROR_OUT(-2302 - i, exit);
  2464. }
  2465. ret = wc_Sha256GetHash(&sha, hashcopy);
  2466. if (ret != 0)
  2467. ERROR_OUT(-2303 - i, exit);
  2468. ret = wc_Sha256Copy(&sha, &shaCopy);
  2469. if (ret != 0)
  2470. ERROR_OUT(-2304 - i, exit);
  2471. ret = wc_Sha256Final(&sha, hash);
  2472. if (ret != 0)
  2473. ERROR_OUT(-2305 - i, exit);
  2474. wc_Sha256Free(&shaCopy);
  2475. if (XMEMCMP(hash, test_sha[i].output, WC_SHA256_DIGEST_SIZE) != 0)
  2476. ERROR_OUT(-2306 - i, exit);
  2477. if (XMEMCMP(hash, hashcopy, WC_SHA256_DIGEST_SIZE) != 0)
  2478. ERROR_OUT(-2307 - i, exit);
  2479. }
  2480. #ifndef NO_LARGE_HASH_TEST
  2481. /* BEGIN LARGE HASH TEST */ {
  2482. byte large_input[1024];
  2483. #ifdef HASH_SIZE_LIMIT
  2484. const char* large_digest =
  2485. "\xa4\x75\x9e\x7a\xa2\x03\x38\x32\x88\x66\xa2\xea\x17\xea\xf8\xc7"
  2486. "\xfe\x4e\xc6\xbb\xe3\xbb\x71\xce\xe7\xdf\x7c\x04\x61\xb3\xc2\x2f";
  2487. #else
  2488. const char* large_digest =
  2489. "\x27\x78\x3e\x87\x96\x3a\x4e\xfb\x68\x29\xb5\x31\xc9\xba\x57\xb4"
  2490. "\x4f\x45\x79\x7f\x67\x70\xbd\x63\x7f\xbf\x0d\x80\x7c\xbd\xba\xe0";
  2491. #endif
  2492. for (i = 0; i < (int)sizeof(large_input); i++) {
  2493. large_input[i] = (byte)(i & 0xFF);
  2494. }
  2495. #ifdef HASH_SIZE_LIMIT
  2496. times = 20;
  2497. #else
  2498. times = 100;
  2499. #endif
  2500. #ifdef WOLFSSL_PIC32MZ_HASH
  2501. wc_Sha256SizeSet(&sha, times * sizeof(large_input));
  2502. #endif
  2503. for (i = 0; i < times; ++i) {
  2504. ret = wc_Sha256Update(&sha, (byte*)large_input,
  2505. (word32)sizeof(large_input));
  2506. if (ret != 0)
  2507. ERROR_OUT(-2308, exit);
  2508. }
  2509. ret = wc_Sha256Final(&sha, hash);
  2510. if (ret != 0)
  2511. ERROR_OUT(-2309, exit);
  2512. if (XMEMCMP(hash, large_digest, WC_SHA256_DIGEST_SIZE) != 0)
  2513. ERROR_OUT(-2310, exit);
  2514. } /* END LARGE HASH TEST */
  2515. #endif /* NO_LARGE_HASH_TEST */
  2516. exit:
  2517. wc_Sha256Free(&sha);
  2518. wc_Sha256Free(&shaCopy);
  2519. return ret;
  2520. }
  2521. #endif
  2522. #ifdef WOLFSSL_SHA512
  2523. WOLFSSL_TEST_SUBROUTINE int sha512_test(void)
  2524. {
  2525. wc_Sha512 sha, shaCopy;
  2526. byte hash[WC_SHA512_DIGEST_SIZE];
  2527. byte hashcopy[WC_SHA512_DIGEST_SIZE];
  2528. int ret = 0;
  2529. testVector a, b, c;
  2530. testVector test_sha[3];
  2531. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2532. a.input = "";
  2533. a.output = "\xcf\x83\xe1\x35\x7e\xef\xb8\xbd\xf1\x54\x28\x50\xd6\x6d\x80"
  2534. "\x07\xd6\x20\xe4\x05\x0b\x57\x15\xdc\x83\xf4\xa9\x21\xd3\x6c"
  2535. "\xe9\xce\x47\xd0\xd1\x3c\x5d\x85\xf2\xb0\xff\x83\x18\xd2\x87"
  2536. "\x7e\xec\x2f\x63\xb9\x31\xbd\x47\x41\x7a\x81\xa5\x38\x32\x7a"
  2537. "\xf9\x27\xda\x3e";
  2538. a.inLen = XSTRLEN(a.input);
  2539. a.outLen = WC_SHA512_DIGEST_SIZE;
  2540. b.input = "abc";
  2541. b.output = "\xdd\xaf\x35\xa1\x93\x61\x7a\xba\xcc\x41\x73\x49\xae\x20\x41"
  2542. "\x31\x12\xe6\xfa\x4e\x89\xa9\x7e\xa2\x0a\x9e\xee\xe6\x4b\x55"
  2543. "\xd3\x9a\x21\x92\x99\x2a\x27\x4f\xc1\xa8\x36\xba\x3c\x23\xa3"
  2544. "\xfe\xeb\xbd\x45\x4d\x44\x23\x64\x3c\xe8\x0e\x2a\x9a\xc9\x4f"
  2545. "\xa5\x4c\xa4\x9f";
  2546. b.inLen = XSTRLEN(b.input);
  2547. b.outLen = WC_SHA512_DIGEST_SIZE;
  2548. c.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  2549. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  2550. c.output = "\x8e\x95\x9b\x75\xda\xe3\x13\xda\x8c\xf4\xf7\x28\x14\xfc\x14"
  2551. "\x3f\x8f\x77\x79\xc6\xeb\x9f\x7f\xa1\x72\x99\xae\xad\xb6\x88"
  2552. "\x90\x18\x50\x1d\x28\x9e\x49\x00\xf7\xe4\x33\x1b\x99\xde\xc4"
  2553. "\xb5\x43\x3a\xc7\xd3\x29\xee\xb6\xdd\x26\x54\x5e\x96\xe5\x5b"
  2554. "\x87\x4b\xe9\x09";
  2555. c.inLen = XSTRLEN(c.input);
  2556. c.outLen = WC_SHA512_DIGEST_SIZE;
  2557. test_sha[0] = a;
  2558. test_sha[1] = b;
  2559. test_sha[2] = c;
  2560. ret = wc_InitSha512_ex(&sha, HEAP_HINT, devId);
  2561. if (ret != 0)
  2562. return -2400;
  2563. ret = wc_InitSha512_ex(&shaCopy, HEAP_HINT, devId);
  2564. if (ret != 0) {
  2565. wc_Sha512Free(&sha);
  2566. return -2401;
  2567. }
  2568. for (i = 0; i < times; ++i) {
  2569. ret = wc_Sha512Update(&sha, (byte*)test_sha[i].input,
  2570. (word32)test_sha[i].inLen);
  2571. if (ret != 0)
  2572. ERROR_OUT(-2402 - i, exit);
  2573. ret = wc_Sha512GetHash(&sha, hashcopy);
  2574. if (ret != 0)
  2575. ERROR_OUT(-2403 - i, exit);
  2576. ret = wc_Sha512Copy(&sha, &shaCopy);
  2577. if (ret != 0)
  2578. ERROR_OUT(-2404 - i, exit);
  2579. ret = wc_Sha512Final(&sha, hash);
  2580. if (ret != 0)
  2581. ERROR_OUT(-2405 - i, exit);
  2582. wc_Sha512Free(&shaCopy);
  2583. if (XMEMCMP(hash, test_sha[i].output, WC_SHA512_DIGEST_SIZE) != 0)
  2584. ERROR_OUT(-2406 - i, exit);
  2585. if (XMEMCMP(hash, hashcopy, WC_SHA512_DIGEST_SIZE) != 0)
  2586. ERROR_OUT(-2407 - i, exit);
  2587. }
  2588. #ifndef NO_LARGE_HASH_TEST
  2589. /* BEGIN LARGE HASH TEST */ {
  2590. byte large_input[1024];
  2591. #ifdef HASH_SIZE_LIMIT
  2592. const char* large_digest =
  2593. "\x30\x9B\x96\xA6\xE9\x43\x78\x30\xA3\x71\x51\x61\xC1\xEB\xE1\xBE"
  2594. "\xC8\xA5\xF9\x13\x5A\xD6\x6D\x9E\x46\x31\x31\x67\x8D\xE2\xC0\x0B"
  2595. "\x2A\x1A\x03\xE1\xF3\x48\xA7\x33\xBD\x49\xF8\xFF\xF1\xC2\xC2\x95"
  2596. "\xCB\xF0\xAF\x87\x61\x85\x58\x63\x6A\xCA\x70\x9C\x8B\x83\x3F\x5D";
  2597. #else
  2598. const char* large_digest =
  2599. "\x5a\x1f\x73\x90\xbd\x8c\xe4\x63\x54\xce\xa0\x9b\xef\x32\x78\x2d"
  2600. "\x2e\xe7\x0d\x5e\x2f\x9d\x15\x1b\xdd\x2d\xde\x65\x0c\x7b\xfa\x83"
  2601. "\x5e\x80\x02\x13\x84\xb8\x3f\xff\x71\x62\xb5\x09\x89\x63\xe1\xdc"
  2602. "\xa5\xdc\xfc\xfa\x9d\x1a\x4d\xc0\xfa\x3a\x14\xf6\x01\x51\x90\xa4";
  2603. #endif
  2604. for (i = 0; i < (int)sizeof(large_input); i++) {
  2605. large_input[i] = (byte)(i & 0xFF);
  2606. }
  2607. #ifdef HASH_SIZE_LIMIT
  2608. times = 20;
  2609. #else
  2610. times = 100;
  2611. #endif
  2612. for (i = 0; i < times; ++i) {
  2613. ret = wc_Sha512Update(&sha, (byte*)large_input,
  2614. (word32)sizeof(large_input));
  2615. if (ret != 0)
  2616. ERROR_OUT(-2408, exit);
  2617. }
  2618. ret = wc_Sha512Final(&sha, hash);
  2619. if (ret != 0)
  2620. ERROR_OUT(-2409, exit);
  2621. if (XMEMCMP(hash, large_digest, WC_SHA512_DIGEST_SIZE) != 0)
  2622. ERROR_OUT(-2410, exit);
  2623. #ifndef NO_UNALIGNED_MEMORY_TEST
  2624. /* Unaligned memory access test */
  2625. for (i = 1; i < 16; i++) {
  2626. ret = wc_Sha512Update(&sha, (byte*)large_input + i,
  2627. (word32)sizeof(large_input) - i);
  2628. if (ret != 0)
  2629. ERROR_OUT(-2411, exit);
  2630. ret = wc_Sha512Final(&sha, hash);
  2631. }
  2632. #endif
  2633. } /* END LARGE HASH TEST */
  2634. #endif /* NO_LARGE_HASH_TEST */
  2635. exit:
  2636. wc_Sha512Free(&sha);
  2637. wc_Sha512Free(&shaCopy);
  2638. return ret;
  2639. }
  2640. #endif
  2641. #ifdef WOLFSSL_SHA384
  2642. WOLFSSL_TEST_SUBROUTINE int sha384_test(void)
  2643. {
  2644. wc_Sha384 sha, shaCopy;
  2645. byte hash[WC_SHA384_DIGEST_SIZE];
  2646. byte hashcopy[WC_SHA384_DIGEST_SIZE];
  2647. int ret = 0;
  2648. testVector a, b, c;
  2649. testVector test_sha[3];
  2650. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2651. a.input = "";
  2652. a.output = "\x38\xb0\x60\xa7\x51\xac\x96\x38\x4c\xd9\x32\x7e\xb1\xb1\xe3"
  2653. "\x6a\x21\xfd\xb7\x11\x14\xbe\x07\x43\x4c\x0c\xc7\xbf\x63\xf6"
  2654. "\xe1\xda\x27\x4e\xde\xbf\xe7\x6f\x65\xfb\xd5\x1a\xd2\xf1\x48"
  2655. "\x98\xb9\x5b";
  2656. a.inLen = XSTRLEN(a.input);
  2657. a.outLen = WC_SHA384_DIGEST_SIZE;
  2658. b.input = "abc";
  2659. b.output = "\xcb\x00\x75\x3f\x45\xa3\x5e\x8b\xb5\xa0\x3d\x69\x9a\xc6\x50"
  2660. "\x07\x27\x2c\x32\xab\x0e\xde\xd1\x63\x1a\x8b\x60\x5a\x43\xff"
  2661. "\x5b\xed\x80\x86\x07\x2b\xa1\xe7\xcc\x23\x58\xba\xec\xa1\x34"
  2662. "\xc8\x25\xa7";
  2663. b.inLen = XSTRLEN(b.input);
  2664. b.outLen = WC_SHA384_DIGEST_SIZE;
  2665. c.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  2666. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  2667. c.output = "\x09\x33\x0c\x33\xf7\x11\x47\xe8\x3d\x19\x2f\xc7\x82\xcd\x1b"
  2668. "\x47\x53\x11\x1b\x17\x3b\x3b\x05\xd2\x2f\xa0\x80\x86\xe3\xb0"
  2669. "\xf7\x12\xfc\xc7\xc7\x1a\x55\x7e\x2d\xb9\x66\xc3\xe9\xfa\x91"
  2670. "\x74\x60\x39";
  2671. c.inLen = XSTRLEN(c.input);
  2672. c.outLen = WC_SHA384_DIGEST_SIZE;
  2673. test_sha[0] = a;
  2674. test_sha[1] = b;
  2675. test_sha[2] = c;
  2676. ret = wc_InitSha384_ex(&sha, HEAP_HINT, devId);
  2677. if (ret != 0)
  2678. return -2500;
  2679. ret = wc_InitSha384_ex(&shaCopy, HEAP_HINT, devId);
  2680. if (ret != 0) {
  2681. wc_Sha384Free(&sha);
  2682. return -2501;
  2683. }
  2684. for (i = 0; i < times; ++i) {
  2685. ret = wc_Sha384Update(&sha, (byte*)test_sha[i].input,
  2686. (word32)test_sha[i].inLen);
  2687. if (ret != 0)
  2688. ERROR_OUT(-2502 - i, exit);
  2689. ret = wc_Sha384GetHash(&sha, hashcopy);
  2690. if (ret != 0)
  2691. ERROR_OUT(-2503 - i, exit);
  2692. ret = wc_Sha384Copy(&sha, &shaCopy);
  2693. if (ret != 0)
  2694. ERROR_OUT(-2504 - i, exit);
  2695. ret = wc_Sha384Final(&sha, hash);
  2696. if (ret != 0)
  2697. ERROR_OUT(-2505 - i, exit);
  2698. wc_Sha384Free(&shaCopy);
  2699. if (XMEMCMP(hash, test_sha[i].output, WC_SHA384_DIGEST_SIZE) != 0)
  2700. ERROR_OUT(-2506 - i, exit);
  2701. if (XMEMCMP(hash, hashcopy, WC_SHA384_DIGEST_SIZE) != 0)
  2702. ERROR_OUT(-2507 - i, exit);
  2703. }
  2704. #ifndef NO_LARGE_HASH_TEST
  2705. /* BEGIN LARGE HASH TEST */ {
  2706. byte large_input[1024];
  2707. #ifdef HASH_SIZE_LIMIT
  2708. const char* large_digest =
  2709. "\xB5\xAD\x66\x6F\xD9\x58\x5E\x68\xDD\x5E\x30\xD3\x95\x72\x33\xA4"
  2710. "\xE9\x4B\x99\x3A\xEF\xF8\xE1\xBF\x1F\x05\x32\xAA\x16\x00\x82\xEC"
  2711. "\x15\xDA\xF2\x75\xEE\xE9\x06\xAF\x52\x8A\x5C\xEF\x72\x81\x80\xD6";
  2712. #else
  2713. const char* large_digest =
  2714. "\x37\x01\xdb\xff\x1e\x40\x4f\xe1\xe2\xea\x0b\x40\xbb\x3b\x39\x9a"
  2715. "\xcc\xe8\x44\x8e\x7e\xe5\x64\xb5\x6b\x7f\x56\x64\xa7\x2b\x84\xe3"
  2716. "\xc5\xd7\x79\x03\x25\x90\xf7\xa4\x58\xcb\x97\xa8\x8b\xb1\xa4\x81";
  2717. #endif
  2718. for (i = 0; i < (int)sizeof(large_input); i++) {
  2719. large_input[i] = (byte)(i & 0xFF);
  2720. }
  2721. #ifdef HASH_SIZE_LIMIT
  2722. times = 20;
  2723. #else
  2724. times = 100;
  2725. #endif
  2726. for (i = 0; i < times; ++i) {
  2727. ret = wc_Sha384Update(&sha, (byte*)large_input,
  2728. (word32)sizeof(large_input));
  2729. if (ret != 0)
  2730. ERROR_OUT(-2508, exit);
  2731. }
  2732. ret = wc_Sha384Final(&sha, hash);
  2733. if (ret != 0)
  2734. ERROR_OUT(-2509, exit);
  2735. if (XMEMCMP(hash, large_digest, WC_SHA384_DIGEST_SIZE) != 0)
  2736. ERROR_OUT(-2510, exit);
  2737. } /* END LARGE HASH TEST */
  2738. #endif /* NO_LARGE_HASH_TEST */
  2739. exit:
  2740. wc_Sha384Free(&sha);
  2741. wc_Sha384Free(&shaCopy);
  2742. return ret;
  2743. }
  2744. #endif /* WOLFSSL_SHA384 */
  2745. #ifdef WOLFSSL_SHA3
  2746. #ifndef WOLFSSL_NOSHA3_224
  2747. static int sha3_224_test(void)
  2748. {
  2749. wc_Sha3 sha;
  2750. byte hash[WC_SHA3_224_DIGEST_SIZE];
  2751. byte hashcopy[WC_SHA3_224_DIGEST_SIZE];
  2752. testVector a, b, c;
  2753. testVector test_sha[3];
  2754. int ret = 0;
  2755. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2756. a.input = "";
  2757. a.output = "\x6b\x4e\x03\x42\x36\x67\xdb\xb7\x3b\x6e\x15\x45\x4f\x0e\xb1"
  2758. "\xab\xd4\x59\x7f\x9a\x1b\x07\x8e\x3f\x5b\x5a\x6b\xc7";
  2759. a.inLen = XSTRLEN(a.input);
  2760. a.outLen = WC_SHA3_224_DIGEST_SIZE;
  2761. b.input = "abc";
  2762. b.output = "\xe6\x42\x82\x4c\x3f\x8c\xf2\x4a\xd0\x92\x34\xee\x7d\x3c\x76"
  2763. "\x6f\xc9\xa3\xa5\x16\x8d\x0c\x94\xad\x73\xb4\x6f\xdf";
  2764. b.inLen = XSTRLEN(b.input);
  2765. b.outLen = WC_SHA3_224_DIGEST_SIZE;
  2766. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  2767. c.output = "\x8a\x24\x10\x8b\x15\x4a\xda\x21\xc9\xfd\x55\x74\x49\x44\x79"
  2768. "\xba\x5c\x7e\x7a\xb7\x6e\xf2\x64\xea\xd0\xfc\xce\x33";
  2769. c.inLen = XSTRLEN(c.input);
  2770. c.outLen = WC_SHA3_224_DIGEST_SIZE;
  2771. test_sha[0] = a;
  2772. test_sha[1] = b;
  2773. test_sha[2] = c;
  2774. ret = wc_InitSha3_224(&sha, HEAP_HINT, devId);
  2775. if (ret != 0)
  2776. return -2600;
  2777. for (i = 0; i < times; ++i) {
  2778. ret = wc_Sha3_224_Update(&sha, (byte*)test_sha[i].input,
  2779. (word32)test_sha[i].inLen);
  2780. if (ret != 0)
  2781. ERROR_OUT(-2601 - i, exit);
  2782. ret = wc_Sha3_224_GetHash(&sha, hashcopy);
  2783. if (ret != 0)
  2784. ERROR_OUT(-2602 - i, exit);
  2785. ret = wc_Sha3_224_Final(&sha, hash);
  2786. if (ret != 0)
  2787. ERROR_OUT(-2603 - i, exit);
  2788. if (XMEMCMP(hash, test_sha[i].output, WC_SHA3_224_DIGEST_SIZE) != 0)
  2789. ERROR_OUT(-2604 - i, exit);
  2790. if (XMEMCMP(hash, hashcopy, WC_SHA3_224_DIGEST_SIZE) != 0)
  2791. ERROR_OUT(-2605 - i, exit);
  2792. }
  2793. #ifndef NO_LARGE_HASH_TEST
  2794. /* BEGIN LARGE HASH TEST */ {
  2795. byte large_input[1024];
  2796. const char* large_digest =
  2797. "\x13\xe5\xd3\x98\x7b\x94\xda\x41\x12\xc7\x1e\x92\x3a\x19"
  2798. "\x21\x20\x86\x6f\x24\xbf\x0a\x31\xbc\xfd\xd6\x70\x36\xf3";
  2799. for (i = 0; i < (int)sizeof(large_input); i++) {
  2800. large_input[i] = (byte)(i & 0xFF);
  2801. }
  2802. times = 100;
  2803. for (i = 0; i < times; ++i) {
  2804. ret = wc_Sha3_224_Update(&sha, (byte*)large_input,
  2805. (word32)sizeof(large_input));
  2806. if (ret != 0)
  2807. ERROR_OUT(-2606, exit);
  2808. }
  2809. ret = wc_Sha3_224_Final(&sha, hash);
  2810. if (ret != 0)
  2811. ERROR_OUT(-2607, exit);
  2812. if (XMEMCMP(hash, large_digest, WC_SHA3_224_DIGEST_SIZE) != 0)
  2813. ERROR_OUT(-2608, exit);
  2814. } /* END LARGE HASH TEST */
  2815. #endif /* NO_LARGE_HASH_TEST */
  2816. exit:
  2817. wc_Sha3_224_Free(&sha);
  2818. return ret;
  2819. }
  2820. #endif /* WOLFSSL_NOSHA3_224 */
  2821. #ifndef WOLFSSL_NOSHA3_256
  2822. static int sha3_256_test(void)
  2823. {
  2824. wc_Sha3 sha;
  2825. byte hash[WC_SHA3_256_DIGEST_SIZE];
  2826. byte hashcopy[WC_SHA3_256_DIGEST_SIZE];
  2827. testVector a, b, c;
  2828. testVector test_sha[3];
  2829. int ret = 0;
  2830. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2831. byte large_input[1024];
  2832. const char* large_digest =
  2833. "\xdc\x90\xc0\xb1\x25\xdb\x2c\x34\x81\xa3\xff\xbc\x1e\x2e\x87\xeb"
  2834. "\x6d\x70\x85\x61\xe0\xe9\x63\x61\xff\xe5\x84\x4b\x1f\x68\x05\x15";
  2835. #if defined(WOLFSSL_HASH_FLAGS) && !defined(WOLFSSL_ASYNC_CRYPT)
  2836. /* test vector with hash of empty string */
  2837. const char* Keccak256EmptyOut =
  2838. "\xc5\xd2\x46\x01\x86\xf7\x23\x3c\x92\x7e\x7d\xb2\xdc\xc7\x03\xc0"
  2839. "\xe5\x00\xb6\x53\xca\x82\x27\x3b\x7b\xfa\xd8\x04\x5d\x85\xa4\x70";
  2840. #endif
  2841. a.input = "";
  2842. a.output = "\xa7\xff\xc6\xf8\xbf\x1e\xd7\x66\x51\xc1\x47\x56\xa0\x61\xd6"
  2843. "\x62\xf5\x80\xff\x4d\xe4\x3b\x49\xfa\x82\xd8\x0a\x4b\x80\xf8"
  2844. "\x43\x4a";
  2845. a.inLen = XSTRLEN(a.input);
  2846. a.outLen = WC_SHA3_256_DIGEST_SIZE;
  2847. b.input = "abc";
  2848. b.output = "\x3a\x98\x5d\xa7\x4f\xe2\x25\xb2\x04\x5c\x17\x2d\x6b\xd3\x90"
  2849. "\xbd\x85\x5f\x08\x6e\x3e\x9d\x52\x5b\x46\xbf\xe2\x45\x11\x43"
  2850. "\x15\x32";
  2851. b.inLen = XSTRLEN(b.input);
  2852. b.outLen = WC_SHA3_256_DIGEST_SIZE;
  2853. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  2854. c.output = "\x41\xc0\xdb\xa2\xa9\xd6\x24\x08\x49\x10\x03\x76\xa8\x23\x5e"
  2855. "\x2c\x82\xe1\xb9\x99\x8a\x99\x9e\x21\xdb\x32\xdd\x97\x49\x6d"
  2856. "\x33\x76";
  2857. c.inLen = XSTRLEN(c.input);
  2858. c.outLen = WC_SHA3_256_DIGEST_SIZE;
  2859. test_sha[0] = a;
  2860. test_sha[1] = b;
  2861. test_sha[2] = c;
  2862. ret = wc_InitSha3_256(&sha, HEAP_HINT, devId);
  2863. if (ret != 0)
  2864. return -2700;
  2865. for (i = 0; i < times; ++i) {
  2866. ret = wc_Sha3_256_Update(&sha, (byte*)test_sha[i].input,
  2867. (word32)test_sha[i].inLen);
  2868. if (ret != 0)
  2869. ERROR_OUT(-2701 - i, exit);
  2870. ret = wc_Sha3_256_GetHash(&sha, hashcopy);
  2871. if (ret != 0)
  2872. ERROR_OUT(-2702 - i, exit);
  2873. ret = wc_Sha3_256_Final(&sha, hash);
  2874. if (ret != 0)
  2875. ERROR_OUT(-2703 - i, exit);
  2876. if (XMEMCMP(hash, test_sha[i].output, WC_SHA3_256_DIGEST_SIZE) != 0)
  2877. ERROR_OUT(-2704 - i, exit);
  2878. if (XMEMCMP(hash, hashcopy, WC_SHA3_256_DIGEST_SIZE) != 0)
  2879. ERROR_OUT(-2705 - i, exit);
  2880. }
  2881. #ifndef NO_LARGE_HASH_TEST
  2882. /* BEGIN LARGE HASH TEST */ {
  2883. for (i = 0; i < (int)sizeof(large_input); i++) {
  2884. large_input[i] = (byte)(i & 0xFF);
  2885. }
  2886. times = 100;
  2887. for (i = 0; i < times; ++i) {
  2888. ret = wc_Sha3_256_Update(&sha, (byte*)large_input,
  2889. (word32)sizeof(large_input));
  2890. if (ret != 0)
  2891. ERROR_OUT(-2706, exit);
  2892. }
  2893. ret = wc_Sha3_256_Final(&sha, hash);
  2894. if (ret != 0)
  2895. ERROR_OUT(-2707, exit);
  2896. if (XMEMCMP(hash, large_digest, WC_SHA3_256_DIGEST_SIZE) != 0)
  2897. ERROR_OUT(-2708, exit);
  2898. } /* END LARGE HASH TEST */
  2899. #endif /* NO_LARGE_HASH_TEST */
  2900. /* this is a software only variant of SHA3 not supported by external hardware devices */
  2901. #if defined(WOLFSSL_HASH_FLAGS) && !defined(WOLFSSL_ASYNC_CRYPT)
  2902. /* Test for Keccak256 */
  2903. ret = wc_Sha3_SetFlags(&sha, WC_HASH_SHA3_KECCAK256);
  2904. if (ret != 0) {
  2905. ERROR_OUT(-2709, exit);
  2906. }
  2907. ret = wc_Sha3_256_Update(&sha, (byte*)"", 0);
  2908. if (ret != 0) {
  2909. ERROR_OUT(-2710, exit);
  2910. }
  2911. ret = wc_Sha3_256_Final(&sha, hash);
  2912. if (ret != 0) {
  2913. ERROR_OUT(-2711, exit);
  2914. }
  2915. if (XMEMCMP(hash, Keccak256EmptyOut, WC_SHA3_256_DIGEST_SIZE) != 0) {
  2916. ERROR_OUT(-2712, exit);
  2917. }
  2918. #endif /* WOLFSSL_HASH_FLAGS && !WOLFSSL_ASYNC_CRYPT */
  2919. exit:
  2920. wc_Sha3_256_Free(&sha);
  2921. return ret;
  2922. }
  2923. #endif /* WOLFSSL_NOSHA3_256 */
  2924. #ifndef WOLFSSL_NOSHA3_384
  2925. static int sha3_384_test(void)
  2926. {
  2927. wc_Sha3 sha;
  2928. byte hash[WC_SHA3_384_DIGEST_SIZE];
  2929. byte buf[64];
  2930. #ifndef NO_INTM_HASH_TEST
  2931. byte hashcopy[WC_SHA3_384_DIGEST_SIZE];
  2932. #endif
  2933. testVector a, b, c;
  2934. testVector test_sha[3];
  2935. int ret;
  2936. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2937. a.input = "";
  2938. a.output = "\x0c\x63\xa7\x5b\x84\x5e\x4f\x7d\x01\x10\x7d\x85\x2e\x4c\x24"
  2939. "\x85\xc5\x1a\x50\xaa\xaa\x94\xfc\x61\x99\x5e\x71\xbb\xee\x98"
  2940. "\x3a\x2a\xc3\x71\x38\x31\x26\x4a\xdb\x47\xfb\x6b\xd1\xe0\x58"
  2941. "\xd5\xf0\x04";
  2942. a.inLen = XSTRLEN(a.input);
  2943. a.outLen = WC_SHA3_384_DIGEST_SIZE;
  2944. #if defined(WOLFSSL_AFALG_XILINX_SHA3) || defined(WOLFSSL_XILINX_CRYPT) && !defined(WOLFSSL_XILINX_CRYPT_VERSAL)
  2945. /* NIST test vector with a length that is a multiple of 4 */
  2946. b.input = "\x7d\x80\xb1\x60\xc4\xb5\x36\xa3\xbe\xb7\x99\x80\x59\x93\x44"
  2947. "\x04\x7c\x5f\x82\xa1\xdf\xc3\xee\xd4";
  2948. b.output = "\x04\x1c\xc5\x86\x1b\xa3\x34\x56\x3c\x61\xd4\xef\x97\x10\xd4"
  2949. "\x89\x6c\x31\x1c\x92\xed\xbe\x0d\x7c\xd5\x3e\x80\x3b\xf2\xf4"
  2950. "\xeb\x60\x57\x23\x55\x70\x77\x0c\xe8\x7c\x55\x20\xd7\xec\x14"
  2951. "\x19\x87\x22";
  2952. b.inLen = XSTRLEN(b.input);
  2953. b.outLen = WC_SHA3_384_DIGEST_SIZE;
  2954. #else
  2955. b.input = "abc";
  2956. b.output = "\xec\x01\x49\x82\x88\x51\x6f\xc9\x26\x45\x9f\x58\xe2\xc6\xad"
  2957. "\x8d\xf9\xb4\x73\xcb\x0f\xc0\x8c\x25\x96\xda\x7c\xf0\xe4\x9b"
  2958. "\xe4\xb2\x98\xd8\x8c\xea\x92\x7a\xc7\xf5\x39\xf1\xed\xf2\x28"
  2959. "\x37\x6d\x25";
  2960. b.inLen = XSTRLEN(b.input);
  2961. b.outLen = WC_SHA3_384_DIGEST_SIZE;
  2962. #endif
  2963. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  2964. c.output = "\x99\x1c\x66\x57\x55\xeb\x3a\x4b\x6b\xbd\xfb\x75\xc7\x8a\x49"
  2965. "\x2e\x8c\x56\xa2\x2c\x5c\x4d\x7e\x42\x9b\xfd\xbc\x32\xb9\xd4"
  2966. "\xad\x5a\xa0\x4a\x1f\x07\x6e\x62\xfe\xa1\x9e\xef\x51\xac\xd0"
  2967. "\x65\x7c\x22";
  2968. c.inLen = XSTRLEN(c.input);
  2969. c.outLen = WC_SHA3_384_DIGEST_SIZE;
  2970. #if defined(WOLFSSL_XILINX_CRYPT) && !defined(WOLFSSL_XILINX_CRYPT_VERSAL)
  2971. test_sha[0] = b; /* hardware acc. pre-Versal can not handle "" string */
  2972. #else
  2973. test_sha[0] = a;
  2974. #endif
  2975. test_sha[1] = b;
  2976. test_sha[2] = c;
  2977. ret = wc_InitSha3_384(&sha, HEAP_HINT, devId);
  2978. if (ret != 0)
  2979. return -2800;
  2980. for (i = 0; i < times; ++i) {
  2981. XMEMCPY(buf, test_sha[i].input, test_sha[i].inLen);
  2982. ret = wc_Sha3_384_Update(&sha, buf,
  2983. (word32)test_sha[i].inLen);
  2984. if (ret != 0)
  2985. ERROR_OUT(-2801 - (i * 10), exit);
  2986. #ifndef NO_INTM_HASH_TEST
  2987. ret = wc_Sha3_384_GetHash(&sha, hashcopy);
  2988. if (ret != 0)
  2989. ERROR_OUT(-2802 - (i * 10), exit);
  2990. #endif
  2991. ret = wc_Sha3_384_Final(&sha, hash);
  2992. if (ret != 0)
  2993. ERROR_OUT(-2803 - (i * 10), exit);
  2994. if (XMEMCMP(hash, test_sha[i].output, WC_SHA3_384_DIGEST_SIZE) != 0)
  2995. ERROR_OUT(-2804 - (i * 10), exit);
  2996. #ifndef NO_INTM_HASH_TEST
  2997. if (XMEMCMP(hash, hashcopy, WC_SHA3_384_DIGEST_SIZE) != 0)
  2998. ERROR_OUT(-2805 - (i * 10), exit);
  2999. #endif
  3000. }
  3001. #ifndef NO_LARGE_HASH_TEST
  3002. /* BEGIN LARGE HASH TEST */ {
  3003. byte large_input[1024];
  3004. const char* large_digest =
  3005. "\x30\x44\xec\x17\xef\x47\x9f\x55\x36\x11\xd6\x3f\x8a\x31\x5a\x71"
  3006. "\x8a\x71\xa7\x1d\x8e\x84\xe8\x6c\x24\x02\x2f\x7a\x08\x4e\xea\xd7"
  3007. "\x42\x36\x5d\xa8\xc2\xb7\x42\xad\xec\x19\xfb\xca\xc6\x64\xb3\xa4";
  3008. for (i = 0; i < (int)sizeof(large_input); i++) {
  3009. large_input[i] = (byte)(i & 0xFF);
  3010. }
  3011. times = 100;
  3012. for (i = 0; i < times; ++i) {
  3013. ret = wc_Sha3_384_Update(&sha, (byte*)large_input,
  3014. (word32)sizeof(large_input));
  3015. if (ret != 0)
  3016. ERROR_OUT(-2806, exit);
  3017. }
  3018. ret = wc_Sha3_384_Final(&sha, hash);
  3019. if (ret != 0)
  3020. ERROR_OUT(-2807, exit);
  3021. if (XMEMCMP(hash, large_digest, WC_SHA3_384_DIGEST_SIZE) != 0)
  3022. ERROR_OUT(-2808, exit);
  3023. } /* END LARGE HASH TEST */
  3024. #endif /* NO_LARGE_HASH_TEST */
  3025. exit:
  3026. wc_Sha3_384_Free(&sha);
  3027. return ret;
  3028. }
  3029. #endif /* WOLFSSL_NOSHA3_384 */
  3030. #ifndef WOLFSSL_NOSHA3_512
  3031. static int sha3_512_test(void)
  3032. {
  3033. wc_Sha3 sha;
  3034. byte hash[WC_SHA3_512_DIGEST_SIZE];
  3035. byte hashcopy[WC_SHA3_512_DIGEST_SIZE];
  3036. testVector a, b, c;
  3037. testVector test_sha[3];
  3038. int ret;
  3039. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  3040. a.input = "";
  3041. a.output = "\xa6\x9f\x73\xcc\xa2\x3a\x9a\xc5\xc8\xb5\x67\xdc\x18\x5a\x75"
  3042. "\x6e\x97\xc9\x82\x16\x4f\xe2\x58\x59\xe0\xd1\xdc\xc1\x47\x5c"
  3043. "\x80\xa6\x15\xb2\x12\x3a\xf1\xf5\xf9\x4c\x11\xe3\xe9\x40\x2c"
  3044. "\x3a\xc5\x58\xf5\x00\x19\x9d\x95\xb6\xd3\xe3\x01\x75\x85\x86"
  3045. "\x28\x1d\xcd\x26";
  3046. a.inLen = XSTRLEN(a.input);
  3047. a.outLen = WC_SHA3_512_DIGEST_SIZE;
  3048. b.input = "abc";
  3049. b.output = "\xb7\x51\x85\x0b\x1a\x57\x16\x8a\x56\x93\xcd\x92\x4b\x6b\x09"
  3050. "\x6e\x08\xf6\x21\x82\x74\x44\xf7\x0d\x88\x4f\x5d\x02\x40\xd2"
  3051. "\x71\x2e\x10\xe1\x16\xe9\x19\x2a\xf3\xc9\x1a\x7e\xc5\x76\x47"
  3052. "\xe3\x93\x40\x57\x34\x0b\x4c\xf4\x08\xd5\xa5\x65\x92\xf8\x27"
  3053. "\x4e\xec\x53\xf0";
  3054. b.inLen = XSTRLEN(b.input);
  3055. b.outLen = WC_SHA3_512_DIGEST_SIZE;
  3056. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  3057. c.output = "\x04\xa3\x71\xe8\x4e\xcf\xb5\xb8\xb7\x7c\xb4\x86\x10\xfc\xa8"
  3058. "\x18\x2d\xd4\x57\xce\x6f\x32\x6a\x0f\xd3\xd7\xec\x2f\x1e\x91"
  3059. "\x63\x6d\xee\x69\x1f\xbe\x0c\x98\x53\x02\xba\x1b\x0d\x8d\xc7"
  3060. "\x8c\x08\x63\x46\xb5\x33\xb4\x9c\x03\x0d\x99\xa2\x7d\xaf\x11"
  3061. "\x39\xd6\xe7\x5e";
  3062. c.inLen = XSTRLEN(c.input);
  3063. c.outLen = WC_SHA3_512_DIGEST_SIZE;
  3064. test_sha[0] = a;
  3065. test_sha[1] = b;
  3066. test_sha[2] = c;
  3067. ret = wc_InitSha3_512(&sha, HEAP_HINT, devId);
  3068. if (ret != 0)
  3069. return -2900;
  3070. for (i = 0; i < times; ++i) {
  3071. ret = wc_Sha3_512_Update(&sha, (byte*)test_sha[i].input,
  3072. (word32)test_sha[i].inLen);
  3073. if (ret != 0)
  3074. ERROR_OUT(-2901 - i, exit);
  3075. ret = wc_Sha3_512_GetHash(&sha, hashcopy);
  3076. if (ret != 0)
  3077. ERROR_OUT(-2902 - i, exit);
  3078. ret = wc_Sha3_512_Final(&sha, hash);
  3079. if (ret != 0)
  3080. ERROR_OUT(-2903 - i, exit);
  3081. if (XMEMCMP(hash, test_sha[i].output, WC_SHA3_512_DIGEST_SIZE) != 0)
  3082. ERROR_OUT(-2904 - i, exit);
  3083. if (XMEMCMP(hash, hashcopy, WC_SHA3_512_DIGEST_SIZE) != 0)
  3084. ERROR_OUT(-2905 - i, exit);
  3085. }
  3086. #ifndef NO_LARGE_HASH_TEST
  3087. /* BEGIN LARGE HASH TEST */ {
  3088. byte large_input[1024];
  3089. const char* large_digest =
  3090. "\x9c\x13\x26\xb6\x26\xb2\x94\x31\xbc\xf4\x34\xe9\x6f\xf2\xd6\x29"
  3091. "\x9a\xd0\x9b\x32\x63\x2f\x18\xa7\x5f\x23\xc9\x60\xc2\x32\x0c\xbc"
  3092. "\x57\x77\x33\xf1\x83\x81\x8a\xd3\x15\x7c\x93\xdc\x80\x9f\xed\x61"
  3093. "\x41\xa7\x5b\xfd\x32\x0e\x38\x15\xb0\x46\x3b\x7a\x4f\xfd\x44\x88";
  3094. for (i = 0; i < (int)sizeof(large_input); i++) {
  3095. large_input[i] = (byte)(i & 0xFF);
  3096. }
  3097. times = 100;
  3098. for (i = 0; i < times; ++i) {
  3099. ret = wc_Sha3_512_Update(&sha, (byte*)large_input,
  3100. (word32)sizeof(large_input));
  3101. if (ret != 0)
  3102. ERROR_OUT(-2906, exit);
  3103. }
  3104. ret = wc_Sha3_512_Final(&sha, hash);
  3105. if (ret != 0)
  3106. ERROR_OUT(-2907, exit);
  3107. if (XMEMCMP(hash, large_digest, WC_SHA3_512_DIGEST_SIZE) != 0)
  3108. ERROR_OUT(-2908, exit);
  3109. } /* END LARGE HASH TEST */
  3110. #endif /* NO_LARGE_HASH_TEST */
  3111. exit:
  3112. wc_Sha3_512_Free(&sha);
  3113. return ret;
  3114. }
  3115. #endif /* WOLFSSL_NOSHA3_512 */
  3116. WOLFSSL_TEST_SUBROUTINE int sha3_test(void)
  3117. {
  3118. int ret;
  3119. (void)ret;
  3120. #ifndef WOLFSSL_NOSHA3_224
  3121. if ((ret = sha3_224_test()) != 0)
  3122. return ret;
  3123. #endif
  3124. #ifndef WOLFSSL_NOSHA3_256
  3125. if ((ret = sha3_256_test()) != 0)
  3126. return ret;
  3127. #endif
  3128. #ifndef WOLFSSL_NOSHA3_384
  3129. if ((ret = sha3_384_test()) != 0)
  3130. return ret;
  3131. #endif
  3132. #ifndef WOLFSSL_NOSHA3_512
  3133. if ((ret = sha3_512_test()) != 0)
  3134. return ret;
  3135. #endif
  3136. return 0;
  3137. }
  3138. #endif /* WOLFSSL_SHA3 */
  3139. #ifdef WOLFSSL_SHAKE128
  3140. static int shake128_absorb_test(wc_Shake* sha)
  3141. {
  3142. byte hash[WC_SHA3_128_BLOCK_SIZE*2];
  3143. testVector a, b, c, d, e;
  3144. testVector test_sha[5];
  3145. int ret = 0;
  3146. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  3147. byte large_input[1024];
  3148. const char* large_digest =
  3149. "\x2b\xd1\x69\x9f\xb3\x75\x40\x74\xb8\xb2\xd2\x0b\x92\x47\x9b\xfe"
  3150. "\xc9\x91\x48\xbe\xda\xa4\x09\xd7\x61\x35\x18\x05\x07\x71\xa5\x61"
  3151. "\x4d\xc4\x94\xad\xbe\x04\x7d\xad\x95\x2f\xeb\x2c\xc0\x10\x67\x43"
  3152. "\x40\xf1\x4a\x58\x1c\x54\xfa\x24\x1c\x1a\x4e\x8d\x9b\xbc\xea\xa7"
  3153. "\x32\xf2\x4c\xc7\x86\x05\x36\xdc\xb4\x42\xd8\x35\xd1\xb4\xa2\x79"
  3154. "\xa2\xe6\xee\x67\x4f\xbf\x2a\x93\x41\x88\x25\x56\x29\x90\x1a\x06"
  3155. "\xba\xfe\x9f\xa6\x1a\x74\xe8\x7e\x85\x4a\xc8\x58\x60\xb1\x7b\x18"
  3156. "\xdf\x77\x59\x46\x04\xc1\xff\x4b\x9b\xcb\xad\xfe\x91\x28\xf0\x01"
  3157. "\xc1\x33\xd0\x99\x99\x2e\x0c\x86\x84\x67\x4d\x37\xa4\x42\x45\x10"
  3158. "\xdc\x8f\xdb\x6f\xa6\x9b\xee\x8a\x60\xa5\x1f\x95\x3f\x8f\xf5\x31"
  3159. "\x4b\x1d\x48\x1e\x45\xff\x79\x5c\xbe\x72\xfc\x56\xed\x6d\x1a\x99"
  3160. "\x7f\x23\x7c\xd1\xa5\x50\x9e\xb0\x4d\x61\x37\xa5\xcb\x24\x71\x3b"
  3161. "\xa3\x60\x51\x2e\x80\x83\x8b\xe0\x55\x50\xa7\x1e\xcc\x9f\xac\x41"
  3162. "\x77\x2c\x79\x22\x30\x09\x1b\x1a\x83\x5b\x2c\x48\xdc\x09\x7d\x59"
  3163. "\x0d\xf0\x54\x17\xfb\x5e\x38\x68\xde\xdb\xc5\x93\xab\x17\x5f\x4b"
  3164. "\x4d\x6d\xf2\xc7\x4e\x15\x1e\x10\x76\xc4\xcb\x87\xd8\xb7\x9d\xa8"
  3165. "\xbf\xc5\x2e\x5e\xfc\xd3\x6c\x45\xd4\x5d\x72\x0f\x66\xeb\x67\x86"
  3166. "\xfa\x6c\xd6\x80\xa4\x23\xcb\x5d\xed\x3c\xde\xdc\x5b\x3d\xca\x95"
  3167. "\x43\x4b\xdc\xe8\x49\xd3\xe1\x01\xd4\xf1\xe4\x47\xcf\x56\xba\x71"
  3168. "\xb4\x69\xed\xe7\xdb\x0f\x89\xd6\xbb\xcd\x1a\xff\xb4\xbe\x72\x26"
  3169. "\xdc\x76\x79\xb3\x1a\x4b\xe6\x8d\x9b\x8e\xd9\xe9\xe6\xf9\xff\xa5";
  3170. a.input = "";
  3171. a.output = "\x7f\x9c\x2b\xa4\xe8\x8f\x82\x7d\x61\x60\x45\x50\x76\x05\x85"
  3172. "\x3e\xd7\x3b\x80\x93\xf6\xef\xbc\x88\xeb\x1a\x6e\xac\xfa\x66"
  3173. "\xef\x26\x3c\xb1\xee\xa9\x88\x00\x4b\x93\x10\x3c\xfb\x0a\xee"
  3174. "\xfd\x2a\x68\x6e\x01\xfa\x4a\x58\xe8\xa3\x63\x9c\xa8\xa1\xe3"
  3175. "\xf9\xae\x57\xe2\x35\xb8\xcc\x87\x3c\x23\xdc\x62\xb8\xd2\x60"
  3176. "\x16\x9a\xfa\x2f\x75\xab\x91\x6a\x58\xd9\x74\x91\x88\x35\xd2"
  3177. "\x5e\x6a\x43\x50\x85\xb2\xba\xdf\xd6\xdf\xaa\xc3\x59\xa5\xef"
  3178. "\xbb\x7b\xcc\x4b\x59\xd5\x38\xdf\x9a\x04\x30\x2e\x10\xc8\xbc"
  3179. "\x1c\xbf\x1a\x0b\x3a\x51\x20\xea\x17\xcd\xa7\xcf\xad\x76\x5f"
  3180. "\x56\x23\x47\x4d\x36\x8c\xcc\xa8\xaf\x00\x07\xcd\x9f\x5e\x4c"
  3181. "\x84\x9f\x16\x7a\x58\x0b\x14\xaa\xbd\xef\xae\xe7\xee\xf4\x7c"
  3182. "\xb0\xfc\xa9";
  3183. a.inLen = XSTRLEN(a.input);
  3184. a.outLen = WC_SHA3_128_BLOCK_SIZE;
  3185. b.input = "abc";
  3186. b.output = "\x58\x81\x09\x2d\xd8\x18\xbf\x5c\xf8\xa3\xdd\xb7\x93\xfb\xcb"
  3187. "\xa7\x40\x97\xd5\xc5\x26\xa6\xd3\x5f\x97\xb8\x33\x51\x94\x0f"
  3188. "\x2c\xc8\x44\xc5\x0a\xf3\x2a\xcd\x3f\x2c\xdd\x06\x65\x68\x70"
  3189. "\x6f\x50\x9b\xc1\xbd\xde\x58\x29\x5d\xae\x3f\x89\x1a\x9a\x0f"
  3190. "\xca\x57\x83\x78\x9a\x41\xf8\x61\x12\x14\xce\x61\x23\x94\xdf"
  3191. "\x28\x6a\x62\xd1\xa2\x25\x2a\xa9\x4d\xb9\xc5\x38\x95\x6c\x71"
  3192. "\x7d\xc2\xbe\xd4\xf2\x32\xa0\x29\x4c\x85\x7c\x73\x0a\xa1\x60"
  3193. "\x67\xac\x10\x62\xf1\x20\x1f\xb0\xd3\x77\xcf\xb9\xcd\xe4\xc6"
  3194. "\x35\x99\xb2\x7f\x34\x62\xbb\xa4\xa0\xed\x29\x6c\x80\x1f\x9f"
  3195. "\xf7\xf5\x73\x02\xbb\x30\x76\xee\x14\x5f\x97\xa3\x2a\xe6\x8e"
  3196. "\x76\xab\x66\xc4\x8d\x51\x67\x5b\xd4\x9a\xcc\x29\x08\x2f\x56"
  3197. "\x47\x58\x4e";
  3198. b.inLen = XSTRLEN(b.input);
  3199. b.outLen = WC_SHA3_128_BLOCK_SIZE;
  3200. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  3201. c.output = "\x1a\x96\x18\x2b\x50\xfb\x8c\x7e\x74\xe0\xa7\x07\x78\x8f\x55"
  3202. "\xe9\x82\x09\xb8\xd9\x1f\xad\xe8\xf3\x2f\x8d\xd5\xcf\xf7\xbf"
  3203. "\x21\xf5\x4e\xe5\xf1\x95\x50\x82\x5a\x6e\x07\x00\x30\x51\x9e"
  3204. "\x94\x42\x63\xac\x1c\x67\x65\x28\x70\x65\x62\x1f\x9f\xcb\x32"
  3205. "\x01\x72\x3e\x32\x23\xb6\x3a\x46\xc2\x93\x8a\xa9\x53\xba\x84"
  3206. "\x01\xd0\xea\x77\xb8\xd2\x64\x90\x77\x55\x66\x40\x7b\x95\x67"
  3207. "\x3c\x0f\x4c\xc1\xce\x9f\xd9\x66\x14\x8d\x7e\xfd\xff\x26\xbb"
  3208. "\xf9\xf4\x8a\x21\xc6\xda\x35\xbf\xaa\x54\x56\x54\xf7\x0a\xe5"
  3209. "\x86\xff\x10\x13\x14\x20\x77\x14\x83\xec\x92\xed\xab\x40\x8c"
  3210. "\x76\x7b\xf4\xc5\xb4\xff\xfa\xa8\x0c\x8c\xa2\x14\xd8\x4c\x4d"
  3211. "\xc7\x00\xd0\xc5\x06\x30\xb2\xff\xc3\x79\x3e\xa4\xd8\x72\x58"
  3212. "\xb4\xc9\x54";
  3213. c.inLen = XSTRLEN(c.input);
  3214. c.outLen = WC_SHA3_128_BLOCK_SIZE;
  3215. /* Taken from NIST CAVP test vectors - full rate output. */
  3216. d.input = "\xdc\x88\x6d\xf3\xf6\x9c\x49\x51\x3d\xe3\x62\x7e\x94\x81\xdb"
  3217. "\x58\x71\xe8\xee\x88\xeb\x9f\x99\x61\x15\x41\x93\x0a\x8b\xc8"
  3218. "\x85\xe0";
  3219. d.output = "\x93\x68\xf0\x15\x10\x92\x44\xeb\x02\x47\xfa\x3a\x0e\x57\xf5"
  3220. "\x2e\xa7\xd9\xeb\xa2\x3d\xae\x7a\x19\x7f\x0a\x29\xe9\x22\x55"
  3221. "\x06\x05\x98\x16\xb7\x84\x48\xb6\x49\x7a\x76\xeb\x96\x2d\xb3"
  3222. "\xf8\x4d\x37\x60\xf1\xfe\xb4\xbd\xc1\xfd\x4a\xc9\x4e\x91\x7a"
  3223. "\xc2\xea\x5e\x4f\x38\x37\x4a\xa5\x6e\x4f\x47\x67\xb8\xd7\x83"
  3224. "\x1b\x2d\x51\x49\x5a\xb8\xea\xb7\xc9\x82\x20\xaf\x13\x41\x5a"
  3225. "\x59\xbb\x7c\x17\x7a\xcd\x62\x8e\xf0\xff\xe3\x6c\xeb\x18\x59"
  3226. "\x5d\x14\x4c\xbf\x25\xef\xc0\x6c\xd9\x56\xa5\x78\x20\x6e\xa8"
  3227. "\xf9\x14\x5e\xf9\xce\x19\x50\x6a\x9d\x04\x4e\xc7\x00\x79\x9f"
  3228. "\xa1\x41\x9b\xaf\x60\x52\xc0\xc1\xb4\x45\xf8\x35\x17\x57\xb0"
  3229. "\xd0\x22\x87\x21\x89\xe2\xc0\x27\x3f\x82\xd9\x69\x69\x66\x3e"
  3230. "\x55\x4d\x09";
  3231. d.inLen = 32;
  3232. d.outLen = WC_SHA3_128_BLOCK_SIZE;
  3233. /* Taken from NIST CAVP test vectors - more than one output block. */
  3234. e.input = "\x8d\x80\x01\xe2\xc0\x96\xf1\xb8\x8e\x7c\x92\x24\xa0\x86\xef"
  3235. "\xd4\x79\x7f\xbf\x74\xa8\x03\x3a\x2d\x42\x2a\x2b\x6b\x8f\x67"
  3236. "\x47\xe4";
  3237. e.output = "\xe1\x7e\xab\x0d\xa4\x04\xf9\xb6\xac\xc0\x84\x97\x2f\xc5\x79"
  3238. "\xe8\x6d\xaa\x76\x10\xa5\xe1\x7c\x23\x2f\x79\x19\x83\x96\xfd"
  3239. "\x01\xc2\x4c\x34\xbb\x54\xf4\xb0\x1e\xf7\x40\xb4\x25\x33\x4a"
  3240. "\x55\xdd\x24\x81\x3d\xc8\xea\x86\xf5\x6e\xf7\x27\x67\x26\x2b"
  3241. "\xf2\x25\x74\x8c\xcc\x3d\x9f\x48\x6f\xfb\x72\x8f\x4e\xad\x29"
  3242. "\x60\xc9\x6c\x3e\x44\x63\x86\xea\xce\x21\x9c\x84\x28\x16\x11"
  3243. "\x63\x58\xb0\xf4\x2d\x7d\xff\xf7\xdd\x24\x11\xfa\x2a\x56\x79"
  3244. "\xfd\x7a\x94\x77\x45\x75\xba\xf9\xfc\xad\x68\xa1\x9e\x30\xd1"
  3245. "\x49\xb0\x59\xb5\x9c\x44\x6c\x4e\xdc\xa5\x9b\xc5\xa4\x79\x9d"
  3246. "\xc4\x65\xaa\x9e\x78\x2c\xed\x9f\x21\xc5\x5d\xe2\x42\xdd\x25"
  3247. "\xd0\xd9\xde\x60\xd0\x9f\xf8\x6a\xba\xf3\xa0\x3a\x76\x71\xb3"
  3248. "\x05\x42\xdf\xbe\x72\xfc\x56\xed\x6d\x1a\x99\x7f\x23\x7c\xd1"
  3249. "\xa5\x50\x9e\xb0\x4d\x61\x37\xa5\xcb\x24\x71\x3b\xa3\x60\x51"
  3250. "\x2e\x80\x83\x8b\xe0\x55\x50\xa7\x1e\xcc\x9f\xac\x41\x77\x2c"
  3251. "\x79\x22\x30\x09\x1b\x1a\x83\x5b\x2c\x48\xdc\x09\x7d\x59\x0d"
  3252. "\xf0\x54\x17\xfb\x5e\x38\x68\xde\xdb\xc5\x93\xab\x17\x5f\x4b"
  3253. "\x4d\x6d\xf2\xc7\x4e\x15\x1e\x10\x76\xc4\xcb\x87\xd8\xb7\x9d"
  3254. "\xa8\xbf\xc5\x2e\x5e\xfc\xd3\x6c\x45\xd4\x5d\x72\x0f\x66\xeb"
  3255. "\x67\x86\xfa\x6c\xd6\x80\xa4\x23\xcb\x5d\xed\x3c\xde\xdc\x5b"
  3256. "\x3d\xca\x95\x43\x4b\xdc\xe8\x49\xd3\xe1\x01\xd4\xf1\xe4\x47"
  3257. "\xcf\x56\xba\x71\xb4\x69\xed\xe7\xdb\x0f\x89\xd6\xbb\xcd\x1a"
  3258. "\xff\xb4\xbe\x72\x26\xdc\x76\x79\xb3\x1a\x4b\xe6\x8d\x9b\x8e"
  3259. "\xd9\xe9\xe6\xf9\xff\xa5";
  3260. e.inLen = 32;
  3261. e.outLen = 2 * WC_SHA3_128_BLOCK_SIZE;
  3262. test_sha[0] = a;
  3263. test_sha[1] = b;
  3264. test_sha[2] = c;
  3265. test_sha[3] = d;
  3266. test_sha[4] = e;
  3267. for (i = 0; i < times; ++i) {
  3268. ret = wc_InitShake128(sha, HEAP_HINT, devId);
  3269. if (ret != 0)
  3270. ERROR_OUT(-3100 - i, exit);
  3271. ret = wc_Shake128_Absorb(sha, (byte*)test_sha[i].input,
  3272. (word32)test_sha[i].inLen);
  3273. if (ret != 0)
  3274. ERROR_OUT(-3101 - i, exit);
  3275. ret = wc_Shake128_SqueezeBlocks(sha, hash,
  3276. (word32)test_sha[i].outLen / WC_SHA3_128_BLOCK_SIZE);
  3277. if (ret != 0)
  3278. ERROR_OUT(-3102 - i, exit);
  3279. if (XMEMCMP(hash, test_sha[i].output, (word32)test_sha[i].outLen) != 0)
  3280. ERROR_OUT(-3103 - i, exit);
  3281. }
  3282. #ifndef NO_LARGE_HASH_TEST
  3283. /* BEGIN LARGE HASH TEST */ {
  3284. for (i = 0; i < (int)sizeof(large_input); i++) {
  3285. large_input[i] = (byte)(i & 0xFF);
  3286. }
  3287. ret = wc_InitShake128(sha, HEAP_HINT, devId);
  3288. if (ret != 0)
  3289. ERROR_OUT(-3104, exit);
  3290. /* Absorb is non-incremental. */
  3291. ret = wc_Shake128_Absorb(sha, (byte*)large_input,
  3292. (word32)sizeof(large_input));
  3293. if (ret != 0)
  3294. ERROR_OUT(-3105, exit);
  3295. /* Able to squeeze out blocks incrementally. */
  3296. ret = wc_Shake128_SqueezeBlocks(sha, hash, 1);
  3297. if (ret != 0)
  3298. ERROR_OUT(-3106, exit);
  3299. ret = wc_Shake128_SqueezeBlocks(sha, hash,
  3300. ((word32)sizeof(hash) / WC_SHA3_128_BLOCK_SIZE) - 1);
  3301. if (ret != 0)
  3302. ERROR_OUT(-3106, exit);
  3303. if (XMEMCMP(hash, large_digest, sizeof(hash)) != 0)
  3304. ERROR_OUT(-3107, exit);
  3305. } /* END LARGE HASH TEST */
  3306. #endif /* NO_LARGE_HASH_TEST */
  3307. exit:
  3308. return ret;
  3309. }
  3310. WOLFSSL_TEST_SUBROUTINE int shake128_test(void)
  3311. {
  3312. wc_Shake sha;
  3313. byte hash[250];
  3314. testVector a, b, c, d, e;
  3315. testVector test_sha[5];
  3316. int ret = 0;
  3317. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  3318. byte large_input[1024];
  3319. const char* large_digest =
  3320. "\x88\xd7\x0e\x86\x46\x72\x6b\x3d\x7d\x22\xe1\xa9\x2d\x02\xdb\x35"
  3321. "\x92\x4f\x1b\x03\x90\xee\xa3\xce\xd1\x3a\x08\x3a\xd7\x4e\x10\xdf"
  3322. "\x09\x67\x33\x35\x4f\xdd\x38\x50\x5b\xcb\x75\xc7\xba\x65\xe5\xe8"
  3323. "\xb8\x76\xde\xc5\xee\xd7\xf1\x65\x93\x4e\x5e\xc4\xb1\xd7\x6b\xee"
  3324. "\x4b\x57\x48\xf5\x38\x49\x9e\x45\xa0\xf7\x32\xe9\x05\x26\x6a\x10"
  3325. "\x70\xd4\x7c\x19\x01\x1f\x6d\x37\xba\x7b\x74\xc2\xbc\xb6\xbc\x74"
  3326. "\xa3\x66\x6c\x9b\x11\x84\x9d\x4a\x36\xbc\x8a\x0d\x4c\xe3\x39\xfa"
  3327. "\xfa\x1b";
  3328. a.input = "";
  3329. a.output = "\x7f\x9c\x2b\xa4\xe8\x8f\x82\x7d\x61\x60\x45\x50\x76\x05\x85"
  3330. "\x3e\xd7\x3b\x80\x93\xf6\xef\xbc\x88\xeb\x1a\x6e\xac\xfa\x66"
  3331. "\xef\x26\x3c\xb1\xee\xa9\x88\x00\x4b\x93\x10\x3c\xfb\x0a\xee"
  3332. "\xfd\x2a\x68\x6e\x01\xfa\x4a\x58\xe8\xa3\x63\x9c\xa8\xa1\xe3"
  3333. "\xf9\xae\x57\xe2\x35\xb8\xcc\x87\x3c\x23\xdc\x62\xb8\xd2\x60"
  3334. "\x16\x9a\xfa\x2f\x75\xab\x91\x6a\x58\xd9\x74\x91\x88\x35\xd2"
  3335. "\x5e\x6a\x43\x50\x85\xb2\xba\xdf\xd6\xdf\xaa\xc3\x59\xa5\xef"
  3336. "\xbb\x7b\xcc\x4b\x59\xd5\x38\xdf\x9a";
  3337. a.inLen = XSTRLEN(a.input);
  3338. a.outLen = 114;
  3339. b.input = "abc";
  3340. b.output = "\x58\x81\x09\x2d\xd8\x18\xbf\x5c\xf8\xa3\xdd\xb7\x93\xfb\xcb"
  3341. "\xa7\x40\x97\xd5\xc5\x26\xa6\xd3\x5f\x97\xb8\x33\x51\x94\x0f"
  3342. "\x2c\xc8\x44\xc5\x0a\xf3\x2a\xcd\x3f\x2c\xdd\x06\x65\x68\x70"
  3343. "\x6f\x50\x9b\xc1\xbd\xde\x58\x29\x5d\xae\x3f\x89\x1a\x9a\x0f"
  3344. "\xca\x57\x83\x78\x9a\x41\xf8\x61\x12\x14\xce\x61\x23\x94\xdf"
  3345. "\x28\x6a\x62\xd1\xa2\x25\x2a\xa9\x4d\xb9\xc5\x38\x95\x6c\x71"
  3346. "\x7d\xc2\xbe\xd4\xf2\x32\xa0\x29\x4c\x85\x7c\x73\x0a\xa1\x60"
  3347. "\x67\xac\x10\x62\xf1\x20\x1f\xb0\xd3";
  3348. b.inLen = XSTRLEN(b.input);
  3349. b.outLen = 114;
  3350. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  3351. c.output = "\x1a\x96\x18\x2b\x50\xfb\x8c\x7e\x74\xe0\xa7\x07\x78\x8f\x55"
  3352. "\xe9\x82\x09\xb8\xd9\x1f\xad\xe8\xf3\x2f\x8d\xd5\xcf\xf7\xbf"
  3353. "\x21\xf5\x4e\xe5\xf1\x95\x50\x82\x5a\x6e\x07\x00\x30\x51\x9e"
  3354. "\x94\x42\x63\xac\x1c\x67\x65\x28\x70\x65\x62\x1f\x9f\xcb\x32"
  3355. "\x01\x72\x3e\x32\x23\xb6\x3a\x46\xc2\x93\x8a\xa9\x53\xba\x84"
  3356. "\x01\xd0\xea\x77\xb8\xd2\x64\x90\x77\x55\x66\x40\x7b\x95\x67"
  3357. "\x3c\x0f\x4c\xc1\xce\x9f\xd9\x66\x14\x8d\x7e\xfd\xff\x26\xbb"
  3358. "\xf9\xf4\x8a\x21\xc6\xda\x35\xbf\xaa";
  3359. c.inLen = XSTRLEN(c.input);
  3360. c.outLen = 114;
  3361. /* Taken from NIST CAVP test vectors - full rate output. */
  3362. d.input = "\xdc\x88\x6d\xf3\xf6\x9c\x49\x51\x3d\xe3\x62\x7e\x94\x81\xdb"
  3363. "\x58\x71\xe8\xee\x88\xeb\x9f\x99\x61\x15\x41\x93\x0a\x8b\xc8"
  3364. "\x85\xe0";
  3365. d.output = "\x93\x68\xf0\x15\x10\x92\x44\xeb\x02\x47\xfa\x3a\x0e\x57\xf5"
  3366. "\x2e\xa7\xd9\xeb\xa2\x3d\xae\x7a\x19\x7f\x0a\x29\xe9\x22\x55"
  3367. "\x06\x05\x98\x16\xb7\x84\x48\xb6\x49\x7a\x76\xeb\x96\x2d\xb3"
  3368. "\xf8\x4d\x37\x60\xf1\xfe\xb4\xbd\xc1\xfd\x4a\xc9\x4e\x91\x7a"
  3369. "\xc2\xea\x5e\x4f\x38\x37\x4a\xa5\x6e\x4f\x47\x67\xb8\xd7\x83"
  3370. "\x1b\x2d\x51\x49\x5a\xb8\xea\xb7\xc9\x82\x20\xaf\x13\x41\x5a"
  3371. "\x59\xbb\x7c\x17\x7a\xcd\x62\x8e\xf0\xff\xe3\x6c\xeb\x18\x59"
  3372. "\x5d\x14\x4c\xbf\x25\xef\xc0\x6c\xd9\x56\xa5\x78\x20\x6e\xa8"
  3373. "\xf9\x14\x5e\xf9\xce\x19\x50\x6a\x9d\x04\x4e\xc7\x00\x79\x9f"
  3374. "\xa1";
  3375. d.inLen = 32;
  3376. d.outLen = 136;
  3377. /* Taken from NIST CAVP test vectors - more than one output block. */
  3378. e.input = "\x8d\x80\x01\xe2\xc0\x96\xf1\xb8\x8e\x7c\x92\x24\xa0\x86\xef"
  3379. "\xd4\x79\x7f\xbf\x74\xa8\x03\x3a\x2d\x42\x2a\x2b\x6b\x8f\x67"
  3380. "\x47\xe4";
  3381. e.output = "\xe1\x7e\xab\x0d\xa4\x04\xf9\xb6\xac\xc0\x84\x97\x2f\xc5\x79"
  3382. "\xe8\x6d\xaa\x76\x10\xa5\xe1\x7c\x23\x2f\x79\x19\x83\x96\xfd"
  3383. "\x01\xc2\x4c\x34\xbb\x54\xf4\xb0\x1e\xf7\x40\xb4\x25\x33\x4a"
  3384. "\x55\xdd\x24\x81\x3d\xc8\xea\x86\xf5\x6e\xf7\x27\x67\x26\x2b"
  3385. "\xf2\x25\x74\x8c\xcc\x3d\x9f\x48\x6f\xfb\x72\x8f\x4e\xad\x29"
  3386. "\x60\xc9\x6c\x3e\x44\x63\x86\xea\xce\x21\x9c\x84\x28\x16\x11"
  3387. "\x63\x58\xb0\xf4\x2d\x7d\xff\xf7\xdd\x24\x11\xfa\x2a\x56\x79"
  3388. "\xfd\x7a\x94\x77\x45\x75\xba\xf9\xfc\xad\x68\xa1\x9e\x30\xd1"
  3389. "\x49\xb0\x59\xb5\x9c\x44\x6c\x4e\xdc\xa5\x9b\xc5\xa4\x79\x9d"
  3390. "\xc4\x65\xaa\x9e\x78\x2c\xed\x9f\x21\xc5\x5d\xe2\x42\xdd\x25"
  3391. "\xd0\xd9\xde\x60\xd0\x9f\xf8\x6a\xba\xf3\xa0\x3a\x76\x71\xb3"
  3392. "\x05\x42\xdf\xbe\x72\xfc\x56\xed\x6d\x1a\x99\x7f\x23\x7c\xd1"
  3393. "\xa5\x50\x9e\xb0\x4d\x61\x37\xa5\xcb\x24\x71\x3b\xa3\x60\x51"
  3394. "\x2e\x80\x83\x8b\xe0\x55\x50\xa7\x1e\xcc\x9f\xac\x41\x77\x2c"
  3395. "\x79\x22\x30\x09\x1b\x1a\x83\x5b\x2c\x48\xdc\x09\x7d\x59\x0d"
  3396. "\xf0\x54\x17\xfb\x5e\x38\x68\xde\xdb\xc5\x93\xab\x17\x5f\x4b"
  3397. "\x4d\x6d\xf2\xc7\x4e\x15\x1e\x10\x76\xc4";
  3398. e.inLen = 32;
  3399. e.outLen = 250;
  3400. test_sha[0] = a;
  3401. test_sha[1] = b;
  3402. test_sha[2] = c;
  3403. test_sha[3] = d;
  3404. test_sha[4] = e;
  3405. ret = wc_InitShake128(&sha, HEAP_HINT, devId);
  3406. if (ret != 0)
  3407. return -3100;
  3408. for (i = 0; i < times; ++i) {
  3409. ret = wc_Shake128_Update(&sha, (byte*)test_sha[i].input,
  3410. (word32)test_sha[i].inLen);
  3411. if (ret != 0)
  3412. ERROR_OUT(-3101 - i, exit);
  3413. ret = wc_Shake128_Final(&sha, hash, (word32)test_sha[i].outLen);
  3414. if (ret != 0)
  3415. ERROR_OUT(-3102 - i, exit);
  3416. if (XMEMCMP(hash, test_sha[i].output, test_sha[i].outLen) != 0)
  3417. ERROR_OUT(-3103 - i, exit);
  3418. }
  3419. #ifndef NO_LARGE_HASH_TEST
  3420. /* BEGIN LARGE HASH TEST */ {
  3421. for (i = 0; i < (int)sizeof(large_input); i++) {
  3422. large_input[i] = (byte)(i & 0xFF);
  3423. }
  3424. times = 100;
  3425. for (i = 0; i < times; ++i) {
  3426. ret = wc_Shake128_Update(&sha, (byte*)large_input,
  3427. (word32)sizeof(large_input));
  3428. if (ret != 0)
  3429. ERROR_OUT(-3104, exit);
  3430. }
  3431. ret = wc_Shake128_Final(&sha, hash, (word32)sizeof(hash));
  3432. if (ret != 0)
  3433. ERROR_OUT(-3105, exit);
  3434. if (XMEMCMP(hash, large_digest, 114) != 0)
  3435. ERROR_OUT(-3106, exit);
  3436. } /* END LARGE HASH TEST */
  3437. #endif /* NO_LARGE_HASH_TEST */
  3438. ret = shake128_absorb_test(&sha);
  3439. exit:
  3440. wc_Shake128_Free(&sha);
  3441. return ret;
  3442. }
  3443. #endif
  3444. #ifdef WOLFSSL_SHAKE256
  3445. static int shake256_absorb_test(wc_Shake* sha, byte *large_input_buf,
  3446. size_t large_input_buf_size)
  3447. {
  3448. byte hash[WC_SHA3_256_BLOCK_SIZE*2];
  3449. testVector a, b, c, d, e;
  3450. testVector test_sha[5];
  3451. int ret = 0;
  3452. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  3453. const char* large_digest =
  3454. "\x21\x25\x8e\xae\x6e\x4f\xa7\xe1\xb9\x6d\xa7\xc9\x7d\x46\x03\x69"
  3455. "\x29\x0d\x81\x49\xba\x5d\xaf\x37\xfd\xeb\x25\x52\x1d\xd9\xbd\x65"
  3456. "\xfa\x99\xb9\xd1\x70\x6b\xeb\xd4\xc1\x2c\xea\x24\x20\x27\xa7\xcd"
  3457. "\xfa\xe1\x81\xd9\xd5\xc1\x1c\xc7\xe9\x70\xc3\xc7\x21\x6f\x32\x22"
  3458. "\xe3\x27\xdb\x58\x5e\xea\x18\x2d\x63\x4d\x14\x6c\x94\xcf\x2b\x7e"
  3459. "\x6e\x2a\x74\xf3\xe0\xac\xb3\xb2\xcc\xef\x38\xe9\xe7\x35\xb3\xc5"
  3460. "\x77\x9d\xff\xe3\x08\x8e\xf8\x2c\x89\xbb\x45\x22\x16\x99\x91\xc0"
  3461. "\xe7\x71\x57\x75\xc5\xb1\xc6\xaf\x27\xcb\x64\x8c\xc4\xee\x3d\x5f"
  3462. "\x4c\x35\xfb\x1c\xf3\xf8\x0e\xfd\x5e\xfc\x07\xd8\x4d\x55\x32\x49"
  3463. "\x45\x0d\xab\x4a\x49\xc4\x83\xde\xd2\x50\xc9\x33\x8f\x85\xcd\x93"
  3464. "\x7a\xe6\x6b\xb4\x36\xf3\xb4\x02\x6e\x85\x9f\xda\x1c\xa5\x71\x43"
  3465. "\x2f\x3b\xfc\x09\xe7\xc0\x3c\xa4\xd1\x83\xb7\x41\x11\x1c\xa0\x48"
  3466. "\x3d\x0e\xda\xbc\x03\xfe\xb2\x3b\x17\xee\x48\xe8\x44\xba\x24\x08"
  3467. "\xd9\xdc\xfd\x01\x39\xd2\xe8\xc7\x31\x01\x25\xae\xe8\x01\xc6\x1a"
  3468. "\xb7\x90\x0d\x1e\xfc\x47\xc0\x78\x28\x17\x66\xf3\x61\xc5\xe6\x11"
  3469. "\x13\x46\x23\x5e\x1d\xc3\x83\x25\x66\x6c\x68\x1b\x30\xdd\xc4\xe6"
  3470. "\x83\x8b\x0f\x23\x58\x7e\x06\x5f\x4a\x2b\xed\xc9\x6c\x97\x68\x44";
  3471. a.input = "";
  3472. a.output = "\x46\xb9\xdd\x2b\x0b\xa8\x8d\x13\x23\x3b\x3f\xeb\x74\x3e\xeb"
  3473. "\x24\x3f\xcd\x52\xea\x62\xb8\x1b\x82\xb5\x0c\x27\x64\x6e\xd5"
  3474. "\x76\x2f\xd7\x5d\xc4\xdd\xd8\xc0\xf2\x00\xcb\x05\x01\x9d\x67"
  3475. "\xb5\x92\xf6\xfc\x82\x1c\x49\x47\x9a\xb4\x86\x40\x29\x2e\xac"
  3476. "\xb3\xb7\xc4\xbe\x14\x1e\x96\x61\x6f\xb1\x39\x57\x69\x2c\xc7"
  3477. "\xed\xd0\xb4\x5a\xe3\xdc\x07\x22\x3c\x8e\x92\x93\x7b\xef\x84"
  3478. "\xbc\x0e\xab\x86\x28\x53\x34\x9e\xc7\x55\x46\xf5\x8f\xb7\xc2"
  3479. "\x77\x5c\x38\x46\x2c\x50\x10\xd8\x46\xc1\x85\xc1\x51\x11\xe5"
  3480. "\x95\x52\x2a\x6b\xcd\x16\xcf\x86\xf3\xd1\x22\x10\x9e\x3b\x1f"
  3481. "\xdd";
  3482. a.inLen = XSTRLEN(a.input);
  3483. a.outLen = WC_SHA3_256_BLOCK_SIZE;
  3484. b.input = "abc";
  3485. b.output = "\x48\x33\x66\x60\x13\x60\xa8\x77\x1c\x68\x63\x08\x0c\xc4\x11"
  3486. "\x4d\x8d\xb4\x45\x30\xf8\xf1\xe1\xee\x4f\x94\xea\x37\xe7\x8b"
  3487. "\x57\x39\xd5\xa1\x5b\xef\x18\x6a\x53\x86\xc7\x57\x44\xc0\x52"
  3488. "\x7e\x1f\xaa\x9f\x87\x26\xe4\x62\xa1\x2a\x4f\xeb\x06\xbd\x88"
  3489. "\x01\xe7\x51\xe4\x13\x85\x14\x12\x04\xf3\x29\x97\x9f\xd3\x04"
  3490. "\x7a\x13\xc5\x65\x77\x24\xad\xa6\x4d\x24\x70\x15\x7b\x3c\xdc"
  3491. "\x28\x86\x20\x94\x4d\x78\xdb\xcd\xdb\xd9\x12\x99\x3f\x09\x13"
  3492. "\xf1\x64\xfb\x2c\xe9\x51\x31\xa2\xd0\x9a\x3e\x6d\x51\xcb\xfc"
  3493. "\x62\x27\x20\xd7\xa7\x5c\x63\x34\xe8\xa2\xd7\xec\x71\xa7\xcc"
  3494. "\x29";
  3495. b.inLen = XSTRLEN(b.input);
  3496. b.outLen = WC_SHA3_256_BLOCK_SIZE;
  3497. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  3498. c.output = "\x4d\x8c\x2d\xd2\x43\x5a\x01\x28\xee\xfb\xb8\xc3\x6f\x6f\x87"
  3499. "\x13\x3a\x79\x11\xe1\x8d\x97\x9e\xe1\xae\x6b\xe5\xd4\xfd\x2e"
  3500. "\x33\x29\x40\xd8\x68\x8a\x4e\x6a\x59\xaa\x80\x60\xf1\xf9\xbc"
  3501. "\x99\x6c\x05\xac\xa3\xc6\x96\xa8\xb6\x62\x79\xdc\x67\x2c\x74"
  3502. "\x0b\xb2\x24\xec\x37\xa9\x2b\x65\xdb\x05\x39\xc0\x20\x34\x55"
  3503. "\xf5\x1d\x97\xcc\xe4\xcf\xc4\x91\x27\xd7\x26\x0a\xfc\x67\x3a"
  3504. "\xf2\x08\xba\xf1\x9b\xe2\x12\x33\xf3\xde\xbe\x78\xd0\x67\x60"
  3505. "\xcf\xa5\x51\xee\x1e\x07\x91\x41\xd4\x9d\xd3\xef\x7e\x18\x2b"
  3506. "\x15\x24\xdf\x82\xea\x1c\xef\xe1\xc6\xc3\x96\x61\x75\xf0\x22"
  3507. "\x8d";
  3508. c.inLen = XSTRLEN(c.input);
  3509. c.outLen = WC_SHA3_256_BLOCK_SIZE;
  3510. /* Taken from NIST CAVP test vectors - full rate output. */
  3511. d.input = "\xdc\x88\x6d\xf3\xf6\x9c\x49\x51\x3d\xe3\x62\x7e\x94\x81\xdb"
  3512. "\x58\x71\xe8\xee\x88\xeb\x9f\x99\x61\x15\x41\x93\x0a\x8b\xc8"
  3513. "\x85\xe0";
  3514. d.output = "\x00\x64\x8a\xfb\xc5\xe6\x51\x64\x9d\xb1\xfd\x82\x93\x6b\x00"
  3515. "\xdb\xbc\x12\x2f\xb4\xc8\x77\x86\x0d\x38\x5c\x49\x50\xd5\x6d"
  3516. "\xe7\xe0\x96\xd6\x13\xd7\xa3\xf2\x7e\xd8\xf2\x63\x34\xb0\xcc"
  3517. "\xc1\x40\x7b\x41\xdc\xcb\x23\xdf\xaa\x52\x98\x18\xd1\x12\x5c"
  3518. "\xd5\x34\x80\x92\x52\x43\x66\xb8\x5f\xab\xb9\x7c\x6c\xd1\xe6"
  3519. "\x06\x6f\x45\x9b\xcc\x56\x6d\xa8\x7e\xc9\xb7\xba\x36\x79\x2d"
  3520. "\x11\x8a\xc3\x9a\x4c\xce\xf6\x19\x2b\xbf\x3a\x54\xaf\x18\xe5"
  3521. "\x7b\x0c\x14\x61\x01\xf6\xae\xaa\x82\x2b\xc4\xb4\xc9\x70\x8b"
  3522. "\x09\xf0\xb3\xba\xb4\x1b\xcc\xe9\x64\xd9\x99\xd1\x10\x7b\xd7"
  3523. "\xc2";
  3524. d.inLen = 32;
  3525. d.outLen = WC_SHA3_256_BLOCK_SIZE;
  3526. /* Taken from NIST CAVP test vectors - more than one output block. */
  3527. e.input = "\x8d\x80\x01\xe2\xc0\x96\xf1\xb8\x8e\x7c\x92\x24\xa0\x86\xef"
  3528. "\xd4\x79\x7f\xbf\x74\xa8\x03\x3a\x2d\x42\x2a\x2b\x6b\x8f\x67"
  3529. "\x47\xe4";
  3530. e.output = "\x2e\x97\x5f\x6a\x8a\x14\xf0\x70\x4d\x51\xb1\x36\x67\xd8\x19"
  3531. "\x5c\x21\x9f\x71\xe6\x34\x56\x96\xc4\x9f\xa4\xb9\xd0\x8e\x92"
  3532. "\x25\xd3\xd3\x93\x93\x42\x51\x52\xc9\x7e\x71\xdd\x24\x60\x1c"
  3533. "\x11\xab\xcf\xa0\xf1\x2f\x53\xc6\x80\xbd\x3a\xe7\x57\xb8\x13"
  3534. "\x4a\x9c\x10\xd4\x29\x61\x58\x69\x21\x7f\xdd\x58\x85\xc4\xdb"
  3535. "\x17\x49\x85\x70\x3a\x6d\x6d\xe9\x4a\x66\x7e\xac\x30\x23\x44"
  3536. "\x3a\x83\x37\xae\x1b\xc6\x01\xb7\x6d\x7d\x38\xec\x3c\x34\x46"
  3537. "\x31\x05\xf0\xd3\x94\x9d\x78\xe5\x62\xa0\x39\xe4\x46\x95\x48"
  3538. "\xb6\x09\x39\x5d\xe5\xa4\xfd\x43\xc4\x6c\xa9\xfd\x6e\xe2\x9a"
  3539. "\xda\x5e\xfc\x07\xd8\x4d\x55\x32\x49\x45\x0d\xab\x4a\x49\xc4"
  3540. "\x83\xde\xd2\x50\xc9\x33\x8f\x85\xcd\x93\x7a\xe6\x6b\xb4\x36"
  3541. "\xf3\xb4\x02\x6e\x85\x9f\xda\x1c\xa5\x71\x43\x2f\x3b\xfc\x09"
  3542. "\xe7\xc0\x3c\xa4\xd1\x83\xb7\x41\x11\x1c\xa0\x48\x3d\x0e\xda"
  3543. "\xbc\x03\xfe\xb2\x3b\x17\xee\x48\xe8\x44\xba\x24\x08\xd9\xdc"
  3544. "\xfd\x01\x39\xd2\xe8\xc7\x31\x01\x25\xae\xe8\x01\xc6\x1a\xb7"
  3545. "\x90\x0d\x1e\xfc\x47\xc0\x78\x28\x17\x66\xf3\x61\xc5\xe6\x11"
  3546. "\x13\x46\x23\x5e\x1d\xc3\x83\x25\x66\x6c\x68\x1b\x30\xdd\xc4"
  3547. "\xe6\x83\x8b\x0f\x23\x58\x7e\x06\x5f\x4a\x2b\xed\xc9\x6c\x97"
  3548. "\x68\x44";
  3549. e.inLen = 32;
  3550. e.outLen = 2 * WC_SHA3_256_BLOCK_SIZE;
  3551. test_sha[0] = a;
  3552. test_sha[1] = b;
  3553. test_sha[2] = c;
  3554. test_sha[3] = d;
  3555. test_sha[4] = e;
  3556. for (i = 0; i < times; ++i) {
  3557. ret = wc_InitShake256(sha, HEAP_HINT, devId);
  3558. if (ret != 0)
  3559. ERROR_OUT(-3100 - i, exit);
  3560. ret = wc_Shake256_Absorb(sha, (byte*)test_sha[i].input,
  3561. (word32)test_sha[i].inLen);
  3562. if (ret != 0)
  3563. ERROR_OUT(-3101 - i, exit);
  3564. ret = wc_Shake256_SqueezeBlocks(sha, hash,
  3565. (word32)test_sha[i].outLen / WC_SHA3_256_BLOCK_SIZE);
  3566. if (ret != 0)
  3567. ERROR_OUT(-3102 - i, exit);
  3568. if (XMEMCMP(hash, test_sha[i].output, (word32)test_sha[i].outLen) != 0)
  3569. ERROR_OUT(-3103 - i, exit);
  3570. }
  3571. #ifndef NO_LARGE_HASH_TEST
  3572. /* BEGIN LARGE HASH TEST */ {
  3573. for (i = 0; i < (int)large_input_buf_size; i++) {
  3574. large_input_buf[i] = (byte)(i & 0xFF);
  3575. }
  3576. ret = wc_InitShake256(sha, HEAP_HINT, devId);
  3577. if (ret != 0)
  3578. ERROR_OUT(-3104, exit);
  3579. /* Absorb is non-incremental. */
  3580. ret = wc_Shake256_Absorb(sha, large_input_buf,
  3581. (word32)large_input_buf_size);
  3582. if (ret != 0)
  3583. ERROR_OUT(-3105, exit);
  3584. /* Able to squeeze out blocks incrementally. */
  3585. ret = wc_Shake256_SqueezeBlocks(sha, hash, 1);
  3586. if (ret != 0)
  3587. ERROR_OUT(-3106, exit);
  3588. ret = wc_Shake256_SqueezeBlocks(sha, hash,
  3589. ((word32)sizeof(hash) / WC_SHA3_256_BLOCK_SIZE) - 1);
  3590. if (ret != 0)
  3591. ERROR_OUT(-3106, exit);
  3592. if (XMEMCMP(hash, large_digest, sizeof(hash)) != 0)
  3593. ERROR_OUT(-3107, exit);
  3594. } /* END LARGE HASH TEST */
  3595. #endif /* NO_LARGE_HASH_TEST */
  3596. exit:
  3597. return ret;
  3598. }
  3599. WOLFSSL_TEST_SUBROUTINE int shake256_test(void)
  3600. {
  3601. wc_Shake sha;
  3602. byte hash[250];
  3603. testVector a, b, c, d, e;
  3604. testVector test_sha[5];
  3605. int ret = 0;
  3606. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  3607. #define SHAKE256_LARGE_INPUT_BUFSIZ 1024
  3608. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  3609. byte *large_input = NULL;
  3610. #else
  3611. byte large_input[SHAKE256_LARGE_INPUT_BUFSIZ];
  3612. #endif
  3613. const char* large_digest =
  3614. "\x90\x32\x4a\xcc\xd1\xdf\xb8\x0b\x79\x1f\xb8\xc8\x5b\x54\xc8\xe7"
  3615. "\x45\xf5\x60\x6b\x38\x26\xb2\x0a\xee\x38\x01\xf3\xd9\xfa\x96\x9f"
  3616. "\x6a\xd7\x15\xdf\xb6\xc2\xf4\x20\x33\x44\x55\xe8\x2a\x09\x2b\x68"
  3617. "\x2e\x18\x65\x5e\x65\x93\x28\xbc\xb1\x9e\xe2\xb1\x92\xea\x98\xac"
  3618. "\x21\xef\x4c\xe1\xb4\xb7\xbe\x81\x5c\x1d\xd3\xb7\x17\xe5\xbb\xc5"
  3619. "\x8c\x68\xb7\xfb\xac\x55\x8a\x9b\x4d\x91\xe4\x9f\x72\xbb\x6e\x38"
  3620. "\xaf\x21\x7d\x21\xaa\x98\x4e\x75\xc4\xb4\x1c\x7c\x50\x45\x54\xf9"
  3621. "\xea\x26";
  3622. a.input = "";
  3623. a.output = "\x46\xb9\xdd\x2b\x0b\xa8\x8d\x13\x23\x3b\x3f\xeb\x74\x3e\xeb"
  3624. "\x24\x3f\xcd\x52\xea\x62\xb8\x1b\x82\xb5\x0c\x27\x64\x6e\xd5"
  3625. "\x76\x2f\xd7\x5d\xc4\xdd\xd8\xc0\xf2\x00\xcb\x05\x01\x9d\x67"
  3626. "\xb5\x92\xf6\xfc\x82\x1c\x49\x47\x9a\xb4\x86\x40\x29\x2e\xac"
  3627. "\xb3\xb7\xc4\xbe\x14\x1e\x96\x61\x6f\xb1\x39\x57\x69\x2c\xc7"
  3628. "\xed\xd0\xb4\x5a\xe3\xdc\x07\x22\x3c\x8e\x92\x93\x7b\xef\x84"
  3629. "\xbc\x0e\xab\x86\x28\x53\x34\x9e\xc7\x55\x46\xf5\x8f\xb7\xc2"
  3630. "\x77\x5c\x38\x46\x2c\x50\x10\xd8\x46";
  3631. a.inLen = XSTRLEN(a.input);
  3632. a.outLen = 114;
  3633. b.input = "abc";
  3634. b.output = "\x48\x33\x66\x60\x13\x60\xa8\x77\x1c\x68\x63\x08\x0c\xc4\x11"
  3635. "\x4d\x8d\xb4\x45\x30\xf8\xf1\xe1\xee\x4f\x94\xea\x37\xe7\x8b"
  3636. "\x57\x39\xd5\xa1\x5b\xef\x18\x6a\x53\x86\xc7\x57\x44\xc0\x52"
  3637. "\x7e\x1f\xaa\x9f\x87\x26\xe4\x62\xa1\x2a\x4f\xeb\x06\xbd\x88"
  3638. "\x01\xe7\x51\xe4\x13\x85\x14\x12\x04\xf3\x29\x97\x9f\xd3\x04"
  3639. "\x7a\x13\xc5\x65\x77\x24\xad\xa6\x4d\x24\x70\x15\x7b\x3c\xdc"
  3640. "\x28\x86\x20\x94\x4d\x78\xdb\xcd\xdb\xd9\x12\x99\x3f\x09\x13"
  3641. "\xf1\x64\xfb\x2c\xe9\x51\x31\xa2\xd0";
  3642. b.inLen = XSTRLEN(b.input);
  3643. b.outLen = 114;
  3644. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  3645. c.output = "\x4d\x8c\x2d\xd2\x43\x5a\x01\x28\xee\xfb\xb8\xc3\x6f\x6f\x87"
  3646. "\x13\x3a\x79\x11\xe1\x8d\x97\x9e\xe1\xae\x6b\xe5\xd4\xfd\x2e"
  3647. "\x33\x29\x40\xd8\x68\x8a\x4e\x6a\x59\xaa\x80\x60\xf1\xf9\xbc"
  3648. "\x99\x6c\x05\xac\xa3\xc6\x96\xa8\xb6\x62\x79\xdc\x67\x2c\x74"
  3649. "\x0b\xb2\x24\xec\x37\xa9\x2b\x65\xdb\x05\x39\xc0\x20\x34\x55"
  3650. "\xf5\x1d\x97\xcc\xe4\xcf\xc4\x91\x27\xd7\x26\x0a\xfc\x67\x3a"
  3651. "\xf2\x08\xba\xf1\x9b\xe2\x12\x33\xf3\xde\xbe\x78\xd0\x67\x60"
  3652. "\xcf\xa5\x51\xee\x1e\x07\x91\x41\xd4";
  3653. c.inLen = XSTRLEN(c.input);
  3654. c.outLen = 114;
  3655. /* Taken from NIST CAVP test vectors - full rate output. */
  3656. d.input = "\xdc\x88\x6d\xf3\xf6\x9c\x49\x51\x3d\xe3\x62\x7e\x94\x81\xdb"
  3657. "\x58\x71\xe8\xee\x88\xeb\x9f\x99\x61\x15\x41\x93\x0a\x8b\xc8"
  3658. "\x85\xe0";
  3659. d.output = "\x00\x64\x8a\xfb\xc5\xe6\x51\x64\x9d\xb1\xfd\x82\x93\x6b\x00"
  3660. "\xdb\xbc\x12\x2f\xb4\xc8\x77\x86\x0d\x38\x5c\x49\x50\xd5\x6d"
  3661. "\xe7\xe0\x96\xd6\x13\xd7\xa3\xf2\x7e\xd8\xf2\x63\x34\xb0\xcc"
  3662. "\xc1\x40\x7b\x41\xdc\xcb\x23\xdf\xaa\x52\x98\x18\xd1\x12\x5c"
  3663. "\xd5\x34\x80\x92\x52\x43\x66\xb8\x5f\xab\xb9\x7c\x6c\xd1\xe6"
  3664. "\x06\x6f\x45\x9b\xcc\x56\x6d\xa8\x7e\xc9\xb7\xba\x36\x79\x2d"
  3665. "\x11\x8a\xc3\x9a\x4c\xce\xf6\x19\x2b\xbf\x3a\x54\xaf\x18\xe5"
  3666. "\x7b\x0c\x14\x61\x01\xf6\xae\xaa\x82\x2b\xc4\xb4\xc9\x70\x8b"
  3667. "\x09\xf0\xb3\xba\xb4\x1b\xcc\xe9\x64\xd9\x99\xd1\x10\x7b\xd7"
  3668. "\xc2";
  3669. d.inLen = 32;
  3670. d.outLen = 136;
  3671. /* Taken from NIST CAVP test vectors - more than one output block. */
  3672. e.input = "\x8d\x80\x01\xe2\xc0\x96\xf1\xb8\x8e\x7c\x92\x24\xa0\x86\xef"
  3673. "\xd4\x79\x7f\xbf\x74\xa8\x03\x3a\x2d\x42\x2a\x2b\x6b\x8f\x67"
  3674. "\x47\xe4";
  3675. e.output = "\x2e\x97\x5f\x6a\x8a\x14\xf0\x70\x4d\x51\xb1\x36\x67\xd8\x19"
  3676. "\x5c\x21\x9f\x71\xe6\x34\x56\x96\xc4\x9f\xa4\xb9\xd0\x8e\x92"
  3677. "\x25\xd3\xd3\x93\x93\x42\x51\x52\xc9\x7e\x71\xdd\x24\x60\x1c"
  3678. "\x11\xab\xcf\xa0\xf1\x2f\x53\xc6\x80\xbd\x3a\xe7\x57\xb8\x13"
  3679. "\x4a\x9c\x10\xd4\x29\x61\x58\x69\x21\x7f\xdd\x58\x85\xc4\xdb"
  3680. "\x17\x49\x85\x70\x3a\x6d\x6d\xe9\x4a\x66\x7e\xac\x30\x23\x44"
  3681. "\x3a\x83\x37\xae\x1b\xc6\x01\xb7\x6d\x7d\x38\xec\x3c\x34\x46"
  3682. "\x31\x05\xf0\xd3\x94\x9d\x78\xe5\x62\xa0\x39\xe4\x46\x95\x48"
  3683. "\xb6\x09\x39\x5d\xe5\xa4\xfd\x43\xc4\x6c\xa9\xfd\x6e\xe2\x9a"
  3684. "\xda\x5e\xfc\x07\xd8\x4d\x55\x32\x49\x45\x0d\xab\x4a\x49\xc4"
  3685. "\x83\xde\xd2\x50\xc9\x33\x8f\x85\xcd\x93\x7a\xe6\x6b\xb4\x36"
  3686. "\xf3\xb4\x02\x6e\x85\x9f\xda\x1c\xa5\x71\x43\x2f\x3b\xfc\x09"
  3687. "\xe7\xc0\x3c\xa4\xd1\x83\xb7\x41\x11\x1c\xa0\x48\x3d\x0e\xda"
  3688. "\xbc\x03\xfe\xb2\x3b\x17\xee\x48\xe8\x44\xba\x24\x08\xd9\xdc"
  3689. "\xfd\x01\x39\xd2\xe8\xc7\x31\x01\x25\xae\xe8\x01\xc6\x1a\xb7"
  3690. "\x90\x0d\x1e\xfc\x47\xc0\x78\x28\x17\x66\xf3\x61\xc5\xe6\x11"
  3691. "\x13\x46\x23\x5e\x1d\xc3\x83\x25\x66\x6c";
  3692. e.inLen = 32;
  3693. e.outLen = 250;
  3694. test_sha[0] = a;
  3695. test_sha[1] = b;
  3696. test_sha[2] = c;
  3697. test_sha[3] = d;
  3698. test_sha[4] = e;
  3699. ret = wc_InitShake256(&sha, HEAP_HINT, devId);
  3700. if (ret != 0)
  3701. return -3100;
  3702. for (i = 0; i < times; ++i) {
  3703. ret = wc_Shake256_Update(&sha, (byte*)test_sha[i].input,
  3704. (word32)test_sha[i].inLen);
  3705. if (ret != 0)
  3706. ERROR_OUT(-3101 - i, exit);
  3707. ret = wc_Shake256_Final(&sha, hash, (word32)test_sha[i].outLen);
  3708. if (ret != 0)
  3709. ERROR_OUT(-3102 - i, exit);
  3710. if (XMEMCMP(hash, test_sha[i].output, test_sha[i].outLen) != 0)
  3711. ERROR_OUT(-3103 - i, exit);
  3712. }
  3713. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  3714. large_input = (byte *)XMALLOC(SHAKE256_LARGE_INPUT_BUFSIZ, NULL,
  3715. DYNAMIC_TYPE_TMP_BUFFER);
  3716. if (large_input == NULL)
  3717. ERROR_OUT(-3107, exit);
  3718. #endif
  3719. #ifndef NO_LARGE_HASH_TEST
  3720. /* BEGIN LARGE HASH TEST */ {
  3721. for (i = 0; i < SHAKE256_LARGE_INPUT_BUFSIZ; i++) {
  3722. large_input[i] = (byte)(i & 0xFF);
  3723. }
  3724. times = 100;
  3725. for (i = 0; i < times; ++i) {
  3726. ret = wc_Shake256_Update(&sha, (byte*)large_input,
  3727. SHAKE256_LARGE_INPUT_BUFSIZ);
  3728. if (ret != 0)
  3729. ERROR_OUT(-3104, exit);
  3730. }
  3731. ret = wc_Shake256_Final(&sha, hash, (word32)sizeof(hash));
  3732. if (ret != 0)
  3733. ERROR_OUT(-3105, exit);
  3734. if (XMEMCMP(hash, large_digest, 114) != 0)
  3735. ERROR_OUT(-3106, exit);
  3736. } /* END LARGE HASH TEST */
  3737. #endif /* NO_LARGE_HASH_TEST */
  3738. ret = shake256_absorb_test(&sha, large_input, SHAKE256_LARGE_INPUT_BUFSIZ);
  3739. exit:
  3740. wc_Shake256_Free(&sha);
  3741. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  3742. if (large_input != NULL)
  3743. XFREE(large_input, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  3744. #endif
  3745. return ret;
  3746. }
  3747. #endif
  3748. #ifndef NO_HASH_WRAPPER
  3749. WOLFSSL_TEST_SUBROUTINE int hash_test(void)
  3750. {
  3751. wc_HashAlg hash;
  3752. int ret, exp_ret;
  3753. int i, j;
  3754. int digestSz;
  3755. byte data[] = "0123456789abcdef0123456789abcdef0123456";
  3756. byte out[WC_MAX_DIGEST_SIZE];
  3757. byte hashOut[WC_MAX_DIGEST_SIZE];
  3758. #if !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC)
  3759. enum wc_HashType hashType;
  3760. #endif
  3761. enum wc_HashType typesGood[] = { WC_HASH_TYPE_MD5, WC_HASH_TYPE_SHA,
  3762. WC_HASH_TYPE_SHA224, WC_HASH_TYPE_SHA256,
  3763. WC_HASH_TYPE_SHA384, WC_HASH_TYPE_SHA512,
  3764. WC_HASH_TYPE_SHA3_224,
  3765. WC_HASH_TYPE_SHA3_256,
  3766. WC_HASH_TYPE_SHA3_384,
  3767. WC_HASH_TYPE_SHA3_512 };
  3768. enum wc_HashType typesNoImpl[] = {
  3769. #ifdef NO_MD5
  3770. WC_HASH_TYPE_MD5,
  3771. #endif
  3772. #ifdef NO_SHA
  3773. WC_HASH_TYPE_SHA,
  3774. #endif
  3775. #ifndef WOLFSSL_SHA224
  3776. WC_HASH_TYPE_SHA224,
  3777. #endif
  3778. #ifdef NO_SHA256
  3779. WC_HASH_TYPE_SHA256,
  3780. #endif
  3781. #ifndef WOLFSSL_SHA384
  3782. WC_HASH_TYPE_SHA384,
  3783. #endif
  3784. #ifndef WOLFSSL_SHA512
  3785. WC_HASH_TYPE_SHA512,
  3786. #endif
  3787. #if !defined(WOLFSSL_SHA3) || defined(WOLFSSL_NOSHA3_224)
  3788. WC_HASH_TYPE_SHA3_224,
  3789. #endif
  3790. #if !defined(WOLFSSL_SHA3) || defined(WOLFSSL_NOSHA3_256)
  3791. WC_HASH_TYPE_SHA3_256,
  3792. #endif
  3793. #if !defined(WOLFSSL_SHA3) || defined(WOLFSSL_NOSHA3_384)
  3794. WC_HASH_TYPE_SHA3_384,
  3795. #endif
  3796. #if !defined(WOLFSSL_SHA3) || defined(WOLFSSL_NOSHA3_512)
  3797. WC_HASH_TYPE_SHA3_512,
  3798. #endif
  3799. WC_HASH_TYPE_NONE
  3800. };
  3801. enum wc_HashType typesBad[] = { WC_HASH_TYPE_NONE, WC_HASH_TYPE_MD5_SHA,
  3802. WC_HASH_TYPE_MD2, WC_HASH_TYPE_MD4 };
  3803. enum wc_HashType typesHashBad[] = { WC_HASH_TYPE_MD2, WC_HASH_TYPE_MD4,
  3804. WC_HASH_TYPE_BLAKE2B,
  3805. WC_HASH_TYPE_NONE };
  3806. /* Parameter Validation testing. */
  3807. ret = wc_HashInit(NULL, WC_HASH_TYPE_SHA256);
  3808. if (ret != BAD_FUNC_ARG)
  3809. return -3200;
  3810. ret = wc_HashUpdate(NULL, WC_HASH_TYPE_SHA256, NULL, sizeof(data));
  3811. if (ret != BAD_FUNC_ARG)
  3812. return -3201;
  3813. ret = wc_HashUpdate(&hash, WC_HASH_TYPE_SHA256, NULL, sizeof(data));
  3814. if (ret != BAD_FUNC_ARG)
  3815. return -3202;
  3816. ret = wc_HashUpdate(NULL, WC_HASH_TYPE_SHA256, data, sizeof(data));
  3817. if (ret != BAD_FUNC_ARG)
  3818. return -3203;
  3819. ret = wc_HashFinal(NULL, WC_HASH_TYPE_SHA256, NULL);
  3820. if (ret != BAD_FUNC_ARG)
  3821. return -3204;
  3822. ret = wc_HashFinal(&hash, WC_HASH_TYPE_SHA256, NULL);
  3823. if (ret != BAD_FUNC_ARG)
  3824. return -3205;
  3825. ret = wc_HashFinal(NULL, WC_HASH_TYPE_SHA256, out);
  3826. if (ret != BAD_FUNC_ARG)
  3827. return -3206;
  3828. /* Try invalid hash algorithms. */
  3829. for (i = 0; i < (int)(sizeof(typesBad)/sizeof(*typesBad)); i++) {
  3830. ret = wc_HashInit(&hash, typesBad[i]);
  3831. if (ret != BAD_FUNC_ARG)
  3832. return -3207 - i;
  3833. ret = wc_HashUpdate(&hash, typesBad[i], data, sizeof(data));
  3834. if (ret != BAD_FUNC_ARG)
  3835. return -3217 - i;
  3836. ret = wc_HashFinal(&hash, typesBad[i], out);
  3837. if (ret != BAD_FUNC_ARG)
  3838. return -3227 - i;
  3839. wc_HashFree(&hash, typesBad[i]);
  3840. }
  3841. /* Try valid hash algorithms. */
  3842. for (i = 0, j = 0; i < (int)(sizeof(typesGood)/sizeof(*typesGood)); i++) {
  3843. exp_ret = 0;
  3844. if (typesGood[i] == typesNoImpl[j]) {
  3845. /* Recognized but no implementation compiled in. */
  3846. exp_ret = HASH_TYPE_E;
  3847. j++;
  3848. }
  3849. ret = wc_HashInit(&hash, typesGood[i]);
  3850. if (ret != exp_ret)
  3851. return -3237 - i;
  3852. ret = wc_HashUpdate(&hash, typesGood[i], data, sizeof(data));
  3853. if (ret != exp_ret)
  3854. return -3247 - i;
  3855. ret = wc_HashFinal(&hash, typesGood[i], out);
  3856. if (ret != exp_ret)
  3857. return -3257 - i;
  3858. wc_HashFree(&hash, typesGood[i]);
  3859. digestSz = wc_HashGetDigestSize(typesGood[i]);
  3860. if (exp_ret < 0 && digestSz != exp_ret)
  3861. return -3267 - i;
  3862. if (exp_ret == 0 && digestSz < 0)
  3863. return -3277 - i;
  3864. if (exp_ret == 0) {
  3865. ret = wc_Hash(typesGood[i], data, sizeof(data), hashOut,
  3866. digestSz - 1);
  3867. if (ret != BUFFER_E)
  3868. return -3287 - i;
  3869. }
  3870. ret = wc_Hash(typesGood[i], data, sizeof(data), hashOut, digestSz);
  3871. if (ret != exp_ret)
  3872. return -3297 - i;
  3873. if (exp_ret == 0 && XMEMCMP(out, hashOut, digestSz) != 0)
  3874. return -3307 -i;
  3875. ret = wc_HashGetBlockSize(typesGood[i]);
  3876. if (exp_ret < 0 && ret != exp_ret)
  3877. return -3308 - i;
  3878. if (exp_ret == 0 && ret < 0)
  3879. return -3318 - i;
  3880. #if !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC)
  3881. ret = wc_HashGetOID(typesGood[i]);
  3882. if (ret == BAD_FUNC_ARG ||
  3883. (exp_ret == 0 && ret == HASH_TYPE_E) ||
  3884. (exp_ret != 0 && ret != HASH_TYPE_E)) {
  3885. return -3328 - i;
  3886. }
  3887. hashType = wc_OidGetHash(ret);
  3888. if (exp_ret == 0 && hashType != typesGood[i])
  3889. return -3338 - i;
  3890. #endif /* !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC) */
  3891. }
  3892. for (i = 0; i < (int)(sizeof(typesHashBad)/sizeof(*typesHashBad)); i++) {
  3893. ret = wc_Hash(typesHashBad[i], data, sizeof(data), out, sizeof(out));
  3894. if (ret != BAD_FUNC_ARG && ret != BUFFER_E)
  3895. return -3348 - i;
  3896. }
  3897. #if !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC)
  3898. ret = wc_HashGetOID(WC_HASH_TYPE_MD2);
  3899. #ifdef WOLFSSL_MD2
  3900. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  3901. return -3358;
  3902. #else
  3903. if (ret != HASH_TYPE_E)
  3904. return -3359;
  3905. #endif
  3906. hashType = wc_OidGetHash(646); /* Md2h */
  3907. #ifdef WOLFSSL_MD2
  3908. if (hashType != WC_HASH_TYPE_MD2)
  3909. return -3360;
  3910. #else
  3911. if (hashType != WC_HASH_TYPE_NONE)
  3912. return -3361;
  3913. #endif
  3914. ret = wc_HashGetOID(WC_HASH_TYPE_MD5_SHA);
  3915. #ifndef NO_MD5
  3916. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  3917. return -3362;
  3918. #else
  3919. if (ret != HASH_TYPE_E)
  3920. return -3363;
  3921. #endif
  3922. ret = wc_HashGetOID(WC_HASH_TYPE_MD4);
  3923. if (ret != BAD_FUNC_ARG)
  3924. return -3364;
  3925. ret = wc_HashGetOID(WC_HASH_TYPE_NONE);
  3926. if (ret != BAD_FUNC_ARG)
  3927. return -3365;
  3928. hashType = wc_OidGetHash(0);
  3929. if (hashType != WC_HASH_TYPE_NONE)
  3930. return -3366;
  3931. #endif /* !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC) */
  3932. ret = wc_HashGetBlockSize(WC_HASH_TYPE_MD2);
  3933. #ifdef WOLFSSL_MD2
  3934. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  3935. return -3367;
  3936. #else
  3937. if (ret != HASH_TYPE_E)
  3938. return -3368;
  3939. #endif
  3940. ret = wc_HashGetDigestSize(WC_HASH_TYPE_MD2);
  3941. #ifdef WOLFSSL_MD2
  3942. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  3943. return -3369;
  3944. #else
  3945. if (ret != HASH_TYPE_E)
  3946. return -3370;
  3947. #endif
  3948. ret = wc_HashGetBlockSize(WC_HASH_TYPE_MD4);
  3949. #ifndef NO_MD4
  3950. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  3951. return -3371;
  3952. #else
  3953. if (ret != HASH_TYPE_E)
  3954. return -3372;
  3955. #endif
  3956. ret = wc_HashGetDigestSize(WC_HASH_TYPE_MD4);
  3957. #ifndef NO_MD4
  3958. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  3959. return -3373;
  3960. #else
  3961. if (ret != HASH_TYPE_E)
  3962. return -3374;
  3963. #endif
  3964. ret = wc_HashGetBlockSize(WC_HASH_TYPE_MD5_SHA);
  3965. #if !defined(NO_MD5) && !defined(NO_SHA)
  3966. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  3967. return -3375;
  3968. #else
  3969. if (ret != HASH_TYPE_E)
  3970. return -3376;
  3971. #endif
  3972. ret = wc_HashGetBlockSize(WC_HASH_TYPE_BLAKE2B);
  3973. #if defined(HAVE_BLAKE2) || defined(HAVE_BLAKE2S)
  3974. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  3975. return -3377;
  3976. #else
  3977. if (ret != HASH_TYPE_E)
  3978. return -3378;
  3979. #endif
  3980. ret = wc_HashGetDigestSize(WC_HASH_TYPE_BLAKE2B);
  3981. #if defined(HAVE_BLAKE2) || defined(HAVE_BLAKE2S)
  3982. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  3983. return -3379;
  3984. #else
  3985. if (ret != HASH_TYPE_E)
  3986. return -3380;
  3987. #endif
  3988. ret = wc_HashGetBlockSize(WC_HASH_TYPE_NONE);
  3989. if (ret != BAD_FUNC_ARG)
  3990. return -3381;
  3991. ret = wc_HashGetDigestSize(WC_HASH_TYPE_NONE);
  3992. if (ret != BAD_FUNC_ARG)
  3993. return -3382;
  3994. #if !defined(NO_CERTS) && !defined(NO_ASN)
  3995. #if defined(WOLFSSL_MD2) && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  3996. ret = wc_GetCTC_HashOID(MD2);
  3997. if (ret == 0)
  3998. return -3383;
  3999. #endif
  4000. #ifndef NO_MD5
  4001. ret = wc_GetCTC_HashOID(WC_MD5);
  4002. if (ret == 0)
  4003. return -3384;
  4004. #endif
  4005. #ifndef NO_SHA
  4006. ret = wc_GetCTC_HashOID(WC_SHA);
  4007. if (ret == 0)
  4008. return -3385;
  4009. #endif
  4010. #ifdef WOLFSSL_SHA224
  4011. ret = wc_GetCTC_HashOID(WC_SHA224);
  4012. if (ret == 0)
  4013. return -3386;
  4014. #endif
  4015. #ifndef NO_SHA256
  4016. ret = wc_GetCTC_HashOID(WC_SHA256);
  4017. if (ret == 0)
  4018. return -3387;
  4019. #endif
  4020. #ifdef WOLFSSL_SHA384
  4021. ret = wc_GetCTC_HashOID(WC_SHA384);
  4022. if (ret == 0)
  4023. return -3388;
  4024. #endif
  4025. #ifdef WOLFSSL_SHA512
  4026. ret = wc_GetCTC_HashOID(WC_SHA512);
  4027. if (ret == 0)
  4028. return -3389;
  4029. #endif
  4030. ret = wc_GetCTC_HashOID(-1);
  4031. if (ret != 0)
  4032. return -3390;
  4033. #endif
  4034. return 0;
  4035. }
  4036. #endif /* !NO_HASH_WRAPPER */
  4037. #if !defined(NO_HMAC) && !defined(NO_MD5) && !(defined(HAVE_FIPS) && \
  4038. defined(HAVE_FIPS_VERSION) && \
  4039. (HAVE_FIPS_VERSION >= 5))
  4040. WOLFSSL_TEST_SUBROUTINE int hmac_md5_test(void)
  4041. {
  4042. Hmac hmac;
  4043. byte hash[WC_MD5_DIGEST_SIZE];
  4044. const char* keys[]=
  4045. {
  4046. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
  4047. "Jefe",
  4048. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  4049. };
  4050. testVector a, b, c;
  4051. testVector test_hmac[3];
  4052. int ret;
  4053. int times = sizeof(test_hmac) / sizeof(testVector), i;
  4054. a.input = "Hi There";
  4055. a.output = "\x92\x94\x72\x7a\x36\x38\xbb\x1c\x13\xf4\x8e\xf8\x15\x8b\xfc"
  4056. "\x9d";
  4057. a.inLen = XSTRLEN(a.input);
  4058. a.outLen = WC_MD5_DIGEST_SIZE;
  4059. b.input = "what do ya want for nothing?";
  4060. b.output = "\x75\x0c\x78\x3e\x6a\xb0\xb5\x03\xea\xa8\x6e\x31\x0a\x5d\xb7"
  4061. "\x38";
  4062. b.inLen = XSTRLEN(b.input);
  4063. b.outLen = WC_MD5_DIGEST_SIZE;
  4064. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4065. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4066. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4067. "\xDD\xDD\xDD\xDD\xDD\xDD";
  4068. c.output = "\x56\xbe\x34\x52\x1d\x14\x4c\x88\xdb\xb8\xc7\x33\xf0\xe8\xb3"
  4069. "\xf6";
  4070. c.inLen = XSTRLEN(c.input);
  4071. c.outLen = WC_MD5_DIGEST_SIZE;
  4072. test_hmac[0] = a;
  4073. test_hmac[1] = b;
  4074. test_hmac[2] = c;
  4075. for (i = 0; i < times; ++i) {
  4076. #if defined(HAVE_FIPS) || defined(HAVE_CAVIUM)
  4077. if (i == 1) {
  4078. continue; /* cavium can't handle short keys, fips not allowed */
  4079. }
  4080. #endif
  4081. if (wc_HmacInit(&hmac, HEAP_HINT, devId) != 0) {
  4082. return -3400;
  4083. }
  4084. ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys[i],
  4085. (word32)XSTRLEN(keys[i]));
  4086. if (ret != 0)
  4087. return -3401;
  4088. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  4089. (word32)test_hmac[i].inLen);
  4090. if (ret != 0)
  4091. return -3402;
  4092. ret = wc_HmacFinal(&hmac, hash);
  4093. if (ret != 0)
  4094. return -3403;
  4095. if (XMEMCMP(hash, test_hmac[i].output, WC_MD5_DIGEST_SIZE) != 0)
  4096. return -3404 - i;
  4097. wc_HmacFree(&hmac);
  4098. }
  4099. #ifndef HAVE_FIPS
  4100. if (wc_HmacSizeByType(WC_MD5) != WC_MD5_DIGEST_SIZE)
  4101. return -3414;
  4102. #endif
  4103. return 0;
  4104. }
  4105. #endif /* !NO_HMAC && !NO_MD5 && (!HAVE_FIPS || (HAVE_FIPS_VERSION < 5)) */
  4106. #if !defined(NO_HMAC) && !defined(NO_SHA)
  4107. WOLFSSL_TEST_SUBROUTINE int hmac_sha_test(void)
  4108. {
  4109. Hmac hmac;
  4110. byte hash[WC_SHA_DIGEST_SIZE];
  4111. const char* keys[]=
  4112. {
  4113. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  4114. "\x0b\x0b\x0b",
  4115. "Jefe",
  4116. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  4117. "\xAA\xAA\xAA"
  4118. };
  4119. testVector a, b, c;
  4120. testVector test_hmac[3];
  4121. int ret;
  4122. int times = sizeof(test_hmac) / sizeof(testVector), i;
  4123. a.input = "Hi There";
  4124. a.output = "\xb6\x17\x31\x86\x55\x05\x72\x64\xe2\x8b\xc0\xb6\xfb\x37\x8c"
  4125. "\x8e\xf1\x46\xbe\x00";
  4126. a.inLen = XSTRLEN(a.input);
  4127. a.outLen = WC_SHA_DIGEST_SIZE;
  4128. b.input = "what do ya want for nothing?";
  4129. b.output = "\xef\xfc\xdf\x6a\xe5\xeb\x2f\xa2\xd2\x74\x16\xd5\xf1\x84\xdf"
  4130. "\x9c\x25\x9a\x7c\x79";
  4131. b.inLen = XSTRLEN(b.input);
  4132. b.outLen = WC_SHA_DIGEST_SIZE;
  4133. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4134. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4135. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4136. "\xDD\xDD\xDD\xDD\xDD\xDD";
  4137. c.output = "\x12\x5d\x73\x42\xb9\xac\x11\xcd\x91\xa3\x9a\xf4\x8a\xa1\x7b"
  4138. "\x4f\x63\xf1\x75\xd3";
  4139. c.inLen = XSTRLEN(c.input);
  4140. c.outLen = WC_SHA_DIGEST_SIZE;
  4141. test_hmac[0] = a;
  4142. test_hmac[1] = b;
  4143. test_hmac[2] = c;
  4144. for (i = 0; i < times; ++i) {
  4145. #if defined(HAVE_FIPS) || defined(HAVE_CAVIUM)
  4146. if (i == 1)
  4147. continue; /* cavium can't handle short keys, fips not allowed */
  4148. #endif
  4149. if (wc_HmacInit(&hmac, HEAP_HINT, devId) != 0)
  4150. return -3500;
  4151. ret = wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys[i],
  4152. (word32)XSTRLEN(keys[i]));
  4153. if (ret != 0)
  4154. return -3501;
  4155. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  4156. (word32)test_hmac[i].inLen);
  4157. if (ret != 0)
  4158. return -3502;
  4159. ret = wc_HmacFinal(&hmac, hash);
  4160. if (ret != 0)
  4161. return -3503;
  4162. if (XMEMCMP(hash, test_hmac[i].output, WC_SHA_DIGEST_SIZE) != 0)
  4163. return -3504 - i;
  4164. wc_HmacFree(&hmac);
  4165. }
  4166. #ifndef HAVE_FIPS
  4167. if (wc_HmacSizeByType(WC_SHA) != WC_SHA_DIGEST_SIZE)
  4168. return -3514;
  4169. #endif
  4170. return 0;
  4171. }
  4172. #endif
  4173. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
  4174. WOLFSSL_TEST_SUBROUTINE int hmac_sha224_test(void)
  4175. {
  4176. Hmac hmac;
  4177. byte hash[WC_SHA224_DIGEST_SIZE];
  4178. const char* keys[]=
  4179. {
  4180. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  4181. "\x0b\x0b\x0b",
  4182. "Jefe",
  4183. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  4184. "\xAA\xAA\xAA",
  4185. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4186. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4187. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4188. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4189. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4190. };
  4191. testVector a, b, c, d;
  4192. testVector test_hmac[4];
  4193. int ret;
  4194. int times = sizeof(test_hmac) / sizeof(testVector), i;
  4195. a.input = "Hi There";
  4196. a.output = "\x89\x6f\xb1\x12\x8a\xbb\xdf\x19\x68\x32\x10\x7c\xd4\x9d\xf3"
  4197. "\x3f\x47\xb4\xb1\x16\x99\x12\xba\x4f\x53\x68\x4b\x22";
  4198. a.inLen = XSTRLEN(a.input);
  4199. a.outLen = WC_SHA224_DIGEST_SIZE;
  4200. b.input = "what do ya want for nothing?";
  4201. b.output = "\xa3\x0e\x01\x09\x8b\xc6\xdb\xbf\x45\x69\x0f\x3a\x7e\x9e\x6d"
  4202. "\x0f\x8b\xbe\xa2\xa3\x9e\x61\x48\x00\x8f\xd0\x5e\x44";
  4203. b.inLen = XSTRLEN(b.input);
  4204. b.outLen = WC_SHA224_DIGEST_SIZE;
  4205. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4206. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4207. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4208. "\xDD\xDD\xDD\xDD\xDD\xDD";
  4209. c.output = "\x7f\xb3\xcb\x35\x88\xc6\xc1\xf6\xff\xa9\x69\x4d\x7d\x6a\xd2"
  4210. "\x64\x93\x65\xb0\xc1\xf6\x5d\x69\xd1\xec\x83\x33\xea";
  4211. c.inLen = XSTRLEN(c.input);
  4212. c.outLen = WC_SHA224_DIGEST_SIZE;
  4213. d.input = "Big Key Input";
  4214. d.output = "\xe7\x4e\x2b\x8a\xa9\xf0\x37\x2f\xed\xae\x70\x0c\x49\x47\xf1"
  4215. "\x46\x54\xa7\x32\x6b\x55\x01\x87\xd2\xc8\x02\x0e\x3a";
  4216. d.inLen = XSTRLEN(d.input);
  4217. d.outLen = WC_SHA224_DIGEST_SIZE;
  4218. test_hmac[0] = a;
  4219. test_hmac[1] = b;
  4220. test_hmac[2] = c;
  4221. test_hmac[3] = d;
  4222. for (i = 0; i < times; ++i) {
  4223. #if defined(HAVE_FIPS) || defined(HAVE_CAVIUM)
  4224. if (i == 1)
  4225. continue; /* cavium can't handle short keys, fips not allowed */
  4226. #endif
  4227. if (wc_HmacInit(&hmac, HEAP_HINT, devId) != 0)
  4228. return -3600;
  4229. ret = wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys[i],
  4230. (word32)XSTRLEN(keys[i]));
  4231. if (ret != 0)
  4232. return -3601;
  4233. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  4234. (word32)test_hmac[i].inLen);
  4235. if (ret != 0)
  4236. return -3602;
  4237. ret = wc_HmacFinal(&hmac, hash);
  4238. if (ret != 0)
  4239. return -3603;
  4240. if (XMEMCMP(hash, test_hmac[i].output, WC_SHA224_DIGEST_SIZE) != 0)
  4241. return -3604 - i;
  4242. wc_HmacFree(&hmac);
  4243. }
  4244. #ifndef HAVE_FIPS
  4245. if (wc_HmacSizeByType(WC_SHA224) != WC_SHA224_DIGEST_SIZE)
  4246. return -3614;
  4247. #endif
  4248. return 0;
  4249. }
  4250. #endif
  4251. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  4252. WOLFSSL_TEST_SUBROUTINE int hmac_sha256_test(void)
  4253. {
  4254. Hmac hmac;
  4255. byte hash[WC_SHA256_DIGEST_SIZE];
  4256. const char* keys[]=
  4257. {
  4258. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  4259. "\x0b\x0b\x0b",
  4260. "Jefe",
  4261. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  4262. "\xAA\xAA\xAA",
  4263. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  4264. "\xAA\xAA\xAA",
  4265. };
  4266. testVector a, b, c, d;
  4267. testVector test_hmac[4];
  4268. int ret;
  4269. int times = sizeof(test_hmac) / sizeof(testVector), i;
  4270. a.input = "Hi There";
  4271. a.output = "\xb0\x34\x4c\x61\xd8\xdb\x38\x53\x5c\xa8\xaf\xce\xaf\x0b\xf1"
  4272. "\x2b\x88\x1d\xc2\x00\xc9\x83\x3d\xa7\x26\xe9\x37\x6c\x2e\x32"
  4273. "\xcf\xf7";
  4274. a.inLen = XSTRLEN(a.input);
  4275. a.outLen = WC_SHA256_DIGEST_SIZE;
  4276. b.input = "what do ya want for nothing?";
  4277. b.output = "\x5b\xdc\xc1\x46\xbf\x60\x75\x4e\x6a\x04\x24\x26\x08\x95\x75"
  4278. "\xc7\x5a\x00\x3f\x08\x9d\x27\x39\x83\x9d\xec\x58\xb9\x64\xec"
  4279. "\x38\x43";
  4280. b.inLen = XSTRLEN(b.input);
  4281. b.outLen = WC_SHA256_DIGEST_SIZE;
  4282. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4283. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4284. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4285. "\xDD\xDD\xDD\xDD\xDD\xDD";
  4286. c.output = "\x77\x3e\xa9\x1e\x36\x80\x0e\x46\x85\x4d\xb8\xeb\xd0\x91\x81"
  4287. "\xa7\x29\x59\x09\x8b\x3e\xf8\xc1\x22\xd9\x63\x55\x14\xce\xd5"
  4288. "\x65\xfe";
  4289. c.inLen = XSTRLEN(c.input);
  4290. c.outLen = WC_SHA256_DIGEST_SIZE;
  4291. d.input = 0;
  4292. d.output = "\x86\xe5\x4f\xd4\x48\x72\x5d\x7e\x5d\xcf\xe2\x23\x53\xc8\x28"
  4293. "\xaf\x48\x78\x1e\xb4\x8c\xae\x81\x06\xa7\xe1\xd4\x98\x94\x9f"
  4294. "\x3e\x46";
  4295. d.inLen = 0;
  4296. d.outLen = WC_SHA256_DIGEST_SIZE;
  4297. test_hmac[0] = a;
  4298. test_hmac[1] = b;
  4299. test_hmac[2] = c;
  4300. test_hmac[3] = d;
  4301. for (i = 0; i < times; ++i) {
  4302. #if defined(HAVE_FIPS) || defined(HAVE_CAVIUM)
  4303. if (i == 1)
  4304. continue; /* cavium can't handle short keys, fips not allowed */
  4305. #endif
  4306. #if defined(HAVE_INTEL_QA) || defined(HAVE_CAVIUM)
  4307. if (i == 3)
  4308. continue; /* QuickAssist can't handle empty HMAC */
  4309. #endif
  4310. if (wc_HmacInit(&hmac, HEAP_HINT, devId) != 0)
  4311. return -3700 - i;
  4312. ret = wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys[i],
  4313. (word32)XSTRLEN(keys[i]));
  4314. if (ret != 0)
  4315. return -3710 - i;
  4316. if (test_hmac[i].input != NULL) {
  4317. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  4318. (word32)test_hmac[i].inLen);
  4319. if (ret != 0)
  4320. return -3720 - i;
  4321. }
  4322. ret = wc_HmacFinal(&hmac, hash);
  4323. if (ret != 0)
  4324. return -3730 - i;
  4325. if (XMEMCMP(hash, test_hmac[i].output, WC_SHA256_DIGEST_SIZE) != 0)
  4326. return -3740 - i;
  4327. wc_HmacFree(&hmac);
  4328. }
  4329. #ifndef HAVE_FIPS
  4330. if (wc_HmacSizeByType(WC_SHA256) != WC_SHA256_DIGEST_SIZE)
  4331. return -3750;
  4332. if (wc_HmacSizeByType(20) != BAD_FUNC_ARG)
  4333. return -3751;
  4334. #endif
  4335. if (wolfSSL_GetHmacMaxSize() != WC_MAX_DIGEST_SIZE)
  4336. return -3752;
  4337. return 0;
  4338. }
  4339. #endif
  4340. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  4341. WOLFSSL_TEST_SUBROUTINE int hmac_sha384_test(void)
  4342. {
  4343. Hmac hmac;
  4344. byte hash[WC_SHA384_DIGEST_SIZE];
  4345. const char* keys[]=
  4346. {
  4347. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  4348. "\x0b\x0b\x0b",
  4349. "Jefe",
  4350. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  4351. "\xAA\xAA\xAA",
  4352. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4353. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4354. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4355. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4356. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4357. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4358. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4359. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4360. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4361. };
  4362. testVector a, b, c, d;
  4363. testVector test_hmac[4];
  4364. int ret;
  4365. int times = sizeof(test_hmac) / sizeof(testVector), i;
  4366. a.input = "Hi There";
  4367. a.output = "\xaf\xd0\x39\x44\xd8\x48\x95\x62\x6b\x08\x25\xf4\xab\x46\x90"
  4368. "\x7f\x15\xf9\xda\xdb\xe4\x10\x1e\xc6\x82\xaa\x03\x4c\x7c\xeb"
  4369. "\xc5\x9c\xfa\xea\x9e\xa9\x07\x6e\xde\x7f\x4a\xf1\x52\xe8\xb2"
  4370. "\xfa\x9c\xb6";
  4371. a.inLen = XSTRLEN(a.input);
  4372. a.outLen = WC_SHA384_DIGEST_SIZE;
  4373. b.input = "what do ya want for nothing?";
  4374. b.output = "\xaf\x45\xd2\xe3\x76\x48\x40\x31\x61\x7f\x78\xd2\xb5\x8a\x6b"
  4375. "\x1b\x9c\x7e\xf4\x64\xf5\xa0\x1b\x47\xe4\x2e\xc3\x73\x63\x22"
  4376. "\x44\x5e\x8e\x22\x40\xca\x5e\x69\xe2\xc7\x8b\x32\x39\xec\xfa"
  4377. "\xb2\x16\x49";
  4378. b.inLen = XSTRLEN(b.input);
  4379. b.outLen = WC_SHA384_DIGEST_SIZE;
  4380. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4381. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4382. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4383. "\xDD\xDD\xDD\xDD\xDD\xDD";
  4384. c.output = "\x88\x06\x26\x08\xd3\xe6\xad\x8a\x0a\xa2\xac\xe0\x14\xc8\xa8"
  4385. "\x6f\x0a\xa6\x35\xd9\x47\xac\x9f\xeb\xe8\x3e\xf4\xe5\x59\x66"
  4386. "\x14\x4b\x2a\x5a\xb3\x9d\xc1\x38\x14\xb9\x4e\x3a\xb6\xe1\x01"
  4387. "\xa3\x4f\x27";
  4388. c.inLen = XSTRLEN(c.input);
  4389. c.outLen = WC_SHA384_DIGEST_SIZE;
  4390. d.input = "Big Key Input";
  4391. d.output = "\xd2\x3d\x29\x6e\xf5\x1e\x23\x23\x49\x18\xb3\xbf\x4c\x38\x7b"
  4392. "\x31\x21\x17\xbb\x09\x73\x27\xf8\x12\x9d\xe9\xc6\x5d\xf9\x54"
  4393. "\xd6\x38\x5a\x68\x53\x14\xee\xe0\xa6\x4f\x36\x7e\xb2\xf3\x1a"
  4394. "\x57\x41\x69";
  4395. d.inLen = XSTRLEN(d.input);
  4396. d.outLen = WC_SHA384_DIGEST_SIZE;
  4397. test_hmac[0] = a;
  4398. test_hmac[1] = b;
  4399. test_hmac[2] = c;
  4400. test_hmac[3] = d;
  4401. for (i = 0; i < times; ++i) {
  4402. #if defined(HAVE_FIPS)
  4403. if (i == 1)
  4404. continue; /* fips not allowed */
  4405. #endif
  4406. if (wc_HmacInit(&hmac, HEAP_HINT, devId) != 0)
  4407. return -3800;
  4408. ret = wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys[i],
  4409. (word32)XSTRLEN(keys[i]));
  4410. if (ret != 0)
  4411. return -3801;
  4412. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  4413. (word32)test_hmac[i].inLen);
  4414. if (ret != 0)
  4415. return -3802;
  4416. ret = wc_HmacFinal(&hmac, hash);
  4417. if (ret != 0)
  4418. return -3803;
  4419. if (XMEMCMP(hash, test_hmac[i].output, WC_SHA384_DIGEST_SIZE) != 0)
  4420. return -3804 - i;
  4421. wc_HmacFree(&hmac);
  4422. }
  4423. #ifndef HAVE_FIPS
  4424. if (wc_HmacSizeByType(WC_SHA384) != WC_SHA384_DIGEST_SIZE)
  4425. return -3814;
  4426. #endif
  4427. return 0;
  4428. }
  4429. #endif
  4430. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA512)
  4431. WOLFSSL_TEST_SUBROUTINE int hmac_sha512_test(void)
  4432. {
  4433. Hmac hmac;
  4434. byte hash[WC_SHA512_DIGEST_SIZE];
  4435. const char* keys[]=
  4436. {
  4437. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  4438. "\x0b\x0b\x0b",
  4439. "Jefe",
  4440. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  4441. "\xAA\xAA\xAA",
  4442. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4443. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4444. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4445. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4446. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4447. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4448. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4449. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4450. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4451. };
  4452. testVector a, b, c, d;
  4453. testVector test_hmac[4];
  4454. int ret;
  4455. int times = sizeof(test_hmac) / sizeof(testVector), i;
  4456. a.input = "Hi There";
  4457. a.output = "\x87\xaa\x7c\xde\xa5\xef\x61\x9d\x4f\xf0\xb4\x24\x1a\x1d\x6c"
  4458. "\xb0\x23\x79\xf4\xe2\xce\x4e\xc2\x78\x7a\xd0\xb3\x05\x45\xe1"
  4459. "\x7c\xde\xda\xa8\x33\xb7\xd6\xb8\xa7\x02\x03\x8b\x27\x4e\xae"
  4460. "\xa3\xf4\xe4\xbe\x9d\x91\x4e\xeb\x61\xf1\x70\x2e\x69\x6c\x20"
  4461. "\x3a\x12\x68\x54";
  4462. a.inLen = XSTRLEN(a.input);
  4463. a.outLen = WC_SHA512_DIGEST_SIZE;
  4464. b.input = "what do ya want for nothing?";
  4465. b.output = "\x16\x4b\x7a\x7b\xfc\xf8\x19\xe2\xe3\x95\xfb\xe7\x3b\x56\xe0"
  4466. "\xa3\x87\xbd\x64\x22\x2e\x83\x1f\xd6\x10\x27\x0c\xd7\xea\x25"
  4467. "\x05\x54\x97\x58\xbf\x75\xc0\x5a\x99\x4a\x6d\x03\x4f\x65\xf8"
  4468. "\xf0\xe6\xfd\xca\xea\xb1\xa3\x4d\x4a\x6b\x4b\x63\x6e\x07\x0a"
  4469. "\x38\xbc\xe7\x37";
  4470. b.inLen = XSTRLEN(b.input);
  4471. b.outLen = WC_SHA512_DIGEST_SIZE;
  4472. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4473. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4474. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4475. "\xDD\xDD\xDD\xDD\xDD\xDD";
  4476. c.output = "\xfa\x73\xb0\x08\x9d\x56\xa2\x84\xef\xb0\xf0\x75\x6c\x89\x0b"
  4477. "\xe9\xb1\xb5\xdb\xdd\x8e\xe8\x1a\x36\x55\xf8\x3e\x33\xb2\x27"
  4478. "\x9d\x39\xbf\x3e\x84\x82\x79\xa7\x22\xc8\x06\xb4\x85\xa4\x7e"
  4479. "\x67\xc8\x07\xb9\x46\xa3\x37\xbe\xe8\x94\x26\x74\x27\x88\x59"
  4480. "\xe1\x32\x92\xfb";
  4481. c.inLen = XSTRLEN(c.input);
  4482. c.outLen = WC_SHA512_DIGEST_SIZE;
  4483. d.input = "Big Key Input";
  4484. d.output = "\x3f\xa9\xc9\xe1\xbd\xbb\x04\x55\x1f\xef\xcc\x92\x33\x08\xeb"
  4485. "\xcf\xc1\x9a\x5b\x5b\xc0\x7c\x86\x84\xae\x8c\x40\xaf\xb1\x27"
  4486. "\x87\x38\x92\x04\xa8\xed\xd7\xd7\x07\xa9\x85\xa0\xc2\xcd\x30"
  4487. "\xc0\x56\x14\x49\xbc\x2f\x69\x15\x6a\x97\xd8\x79\x2f\xb3\x3b"
  4488. "\x1e\x18\xfe\xfa";
  4489. d.inLen = XSTRLEN(d.input);
  4490. d.outLen = WC_SHA512_DIGEST_SIZE;
  4491. test_hmac[0] = a;
  4492. test_hmac[1] = b;
  4493. test_hmac[2] = c;
  4494. test_hmac[3] = d;
  4495. for (i = 0; i < times; ++i) {
  4496. #if defined(HAVE_FIPS)
  4497. if (i == 1)
  4498. continue; /* fips not allowed */
  4499. #endif
  4500. if (wc_HmacInit(&hmac, HEAP_HINT, devId) != 0)
  4501. return -3900;
  4502. ret = wc_HmacSetKey(&hmac, WC_SHA512, (byte*)keys[i],
  4503. (word32)XSTRLEN(keys[i]));
  4504. if (ret != 0)
  4505. return -3901;
  4506. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  4507. (word32)test_hmac[i].inLen);
  4508. if (ret != 0)
  4509. return -3902;
  4510. ret = wc_HmacFinal(&hmac, hash);
  4511. if (ret != 0)
  4512. return -3903;
  4513. if (XMEMCMP(hash, test_hmac[i].output, WC_SHA512_DIGEST_SIZE) != 0)
  4514. return -3904 - i;
  4515. wc_HmacFree(&hmac);
  4516. }
  4517. #ifndef HAVE_FIPS
  4518. if (wc_HmacSizeByType(WC_SHA512) != WC_SHA512_DIGEST_SIZE)
  4519. return -3914;
  4520. #endif
  4521. return 0;
  4522. }
  4523. #endif
  4524. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA3) && \
  4525. !defined(WOLFSSL_NOSHA3_224) && !defined(WOLFSSL_NOSHA3_256) && \
  4526. !defined(WOLFSSL_NOSHA3_384) && !defined(WOLFSSL_NOSHA3_512)
  4527. WOLFSSL_TEST_SUBROUTINE int hmac_sha3_test(void)
  4528. {
  4529. Hmac hmac;
  4530. byte hash[WC_SHA3_512_DIGEST_SIZE];
  4531. const char* key[4] =
  4532. {
  4533. "Jefe",
  4534. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  4535. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
  4536. "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
  4537. "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa",
  4538. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4539. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4540. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4541. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4542. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4543. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4544. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4545. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4546. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4547. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4548. };
  4549. const char* input[4] =
  4550. {
  4551. "what do ya want for nothing?",
  4552. "Hi There",
  4553. "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
  4554. "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
  4555. "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
  4556. "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
  4557. "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd",
  4558. "Big Key Input"
  4559. };
  4560. const int hashType[4] =
  4561. {
  4562. WC_SHA3_224, WC_SHA3_256, WC_SHA3_384, WC_SHA3_512
  4563. };
  4564. const int hashSz[4] =
  4565. {
  4566. WC_SHA3_224_DIGEST_SIZE, WC_SHA3_256_DIGEST_SIZE,
  4567. WC_SHA3_384_DIGEST_SIZE, WC_SHA3_512_DIGEST_SIZE
  4568. };
  4569. const char* output[16] =
  4570. {
  4571. /* key = jefe, input = what do ya want for nothing? */
  4572. /* HMAC-SHA3-224 */
  4573. "\x7f\xdb\x8d\xd8\x8b\xd2\xf6\x0d\x1b\x79\x86\x34\xad\x38\x68\x11"
  4574. "\xc2\xcf\xc8\x5b\xfa\xf5\xd5\x2b\xba\xce\x5e\x66",
  4575. /* HMAC-SHA3-256 */
  4576. "\xc7\xd4\x07\x2e\x78\x88\x77\xae\x35\x96\xbb\xb0\xda\x73\xb8\x87"
  4577. "\xc9\x17\x1f\x93\x09\x5b\x29\x4a\xe8\x57\xfb\xe2\x64\x5e\x1b\xa5",
  4578. /* HMAC-SHA3-384 */
  4579. "\xf1\x10\x1f\x8c\xbf\x97\x66\xfd\x67\x64\xd2\xed\x61\x90\x3f\x21"
  4580. "\xca\x9b\x18\xf5\x7c\xf3\xe1\xa2\x3c\xa1\x35\x08\xa9\x32\x43\xce"
  4581. "\x48\xc0\x45\xdc\x00\x7f\x26\xa2\x1b\x3f\x5e\x0e\x9d\xf4\xc2\x0a",
  4582. /* HMAC-SHA3-512 */
  4583. "\x5a\x4b\xfe\xab\x61\x66\x42\x7c\x7a\x36\x47\xb7\x47\x29\x2b\x83"
  4584. "\x84\x53\x7c\xdb\x89\xaf\xb3\xbf\x56\x65\xe4\xc5\xe7\x09\x35\x0b"
  4585. "\x28\x7b\xae\xc9\x21\xfd\x7c\xa0\xee\x7a\x0c\x31\xd0\x22\xa9\x5e"
  4586. "\x1f\xc9\x2b\xa9\xd7\x7d\xf8\x83\x96\x02\x75\xbe\xb4\xe6\x20\x24",
  4587. /* key = 0b..., input = Hi There */
  4588. /* HMAC-SHA3-224 */
  4589. "\x3b\x16\x54\x6b\xbc\x7b\xe2\x70\x6a\x03\x1d\xca\xfd\x56\x37\x3d"
  4590. "\x98\x84\x36\x76\x41\xd8\xc5\x9a\xf3\xc8\x60\xf7",
  4591. /* HMAC-SHA3-256 */
  4592. "\xba\x85\x19\x23\x10\xdf\xfa\x96\xe2\xa3\xa4\x0e\x69\x77\x43\x51"
  4593. "\x14\x0b\xb7\x18\x5e\x12\x02\xcd\xcc\x91\x75\x89\xf9\x5e\x16\xbb",
  4594. /* HMAC-SHA3-384 */
  4595. "\x68\xd2\xdc\xf7\xfd\x4d\xdd\x0a\x22\x40\xc8\xa4\x37\x30\x5f\x61"
  4596. "\xfb\x73\x34\xcf\xb5\xd0\x22\x6e\x1b\xc2\x7d\xc1\x0a\x2e\x72\x3a"
  4597. "\x20\xd3\x70\xb4\x77\x43\x13\x0e\x26\xac\x7e\x3d\x53\x28\x86\xbd",
  4598. /* HMAC-SHA3-512 */
  4599. "\xeb\x3f\xbd\x4b\x2e\xaa\xb8\xf5\xc5\x04\xbd\x3a\x41\x46\x5a\xac"
  4600. "\xec\x15\x77\x0a\x7c\xab\xac\x53\x1e\x48\x2f\x86\x0b\x5e\xc7\xba"
  4601. "\x47\xcc\xb2\xc6\xf2\xaf\xce\x8f\x88\xd2\x2b\x6d\xc6\x13\x80\xf2"
  4602. "\x3a\x66\x8f\xd3\x88\x8b\xb8\x05\x37\xc0\xa0\xb8\x64\x07\x68\x9e",
  4603. /* key = aa..., output = dd... */
  4604. /* HMAC-SHA3-224 */
  4605. "\x67\x6c\xfc\x7d\x16\x15\x36\x38\x78\x03\x90\x69\x2b\xe1\x42\xd2"
  4606. "\xdf\x7c\xe9\x24\xb9\x09\xc0\xc0\x8d\xbf\xdc\x1a",
  4607. /* HMAC-SHA3-256 */
  4608. "\x84\xec\x79\x12\x4a\x27\x10\x78\x65\xce\xdd\x8b\xd8\x2d\xa9\x96"
  4609. "\x5e\x5e\xd8\xc3\x7b\x0a\xc9\x80\x05\xa7\xf3\x9e\xd5\x8a\x42\x07",
  4610. /* HMAC-SHA3-384 */
  4611. "\x27\x5c\xd0\xe6\x61\xbb\x8b\x15\x1c\x64\xd2\x88\xf1\xf7\x82\xfb"
  4612. "\x91\xa8\xab\xd5\x68\x58\xd7\x2b\xab\xb2\xd4\x76\xf0\x45\x83\x73"
  4613. "\xb4\x1b\x6a\xb5\xbf\x17\x4b\xec\x42\x2e\x53\xfc\x31\x35\xac\x6e",
  4614. /* HMAC-SHA3-512 */
  4615. "\x30\x9e\x99\xf9\xec\x07\x5e\xc6\xc6\xd4\x75\xed\xa1\x18\x06\x87"
  4616. "\xfc\xf1\x53\x11\x95\x80\x2a\x99\xb5\x67\x74\x49\xa8\x62\x51\x82"
  4617. "\x85\x1c\xb3\x32\xaf\xb6\xa8\x9c\x41\x13\x25\xfb\xcb\xcd\x42\xaf"
  4618. "\xcb\x7b\x6e\x5a\xab\x7e\xa4\x2c\x66\x0f\x97\xfd\x85\x84\xbf\x03",
  4619. /* key = big key, input = Big Key Input */
  4620. /* HMAC-SHA3-224 */
  4621. "\x29\xe0\x5e\x46\xc4\xa4\x5e\x46\x74\xbf\xd7\x2d\x1a\xd8\x66\xdb"
  4622. "\x2d\x0d\x10\x4e\x2b\xfa\xad\x53\x7d\x15\x69\x8b",
  4623. /* HMAC-SHA3-256 */
  4624. "\xb5\x5b\x8d\x64\xb6\x9c\x21\xd0\xbf\x20\x5c\xa2\xf7\xb9\xb1\x4e"
  4625. "\x88\x21\x61\x2c\x66\xc3\x91\xae\x6c\x95\x16\x85\x83\xe6\xf4\x9b",
  4626. /* HMAC-SHA3-384 */
  4627. "\xaa\x91\xb3\xa6\x2f\x56\xa1\xbe\x8c\x3e\x74\x38\xdb\x58\xd9\xd3"
  4628. "\x34\xde\xa0\x60\x6d\x8d\x46\xe0\xec\xa9\xf6\x06\x35\x14\xe6\xed"
  4629. "\x83\xe6\x7c\x77\x24\x6c\x11\xb5\x90\x82\xb5\x75\xda\x7b\x83\x2d",
  4630. /* HMAC-SHA3-512 */
  4631. "\x1c\xc3\xa9\x24\x4a\x4a\x3f\xbd\xc7\x20\x00\x16\x9b\x79\x47\x03"
  4632. "\x78\x75\x2c\xb5\xf1\x2e\x62\x7c\xbe\xef\x4e\x8f\x0b\x11\x2b\x32"
  4633. "\xa0\xee\xc9\xd0\x4d\x64\x64\x0b\x37\xf4\xdd\x66\xf7\x8b\xb3\xad"
  4634. "\x52\x52\x6b\x65\x12\xde\x0d\x7c\xc0\x8b\x60\x01\x6c\x37\xd7\xa8"
  4635. };
  4636. int i = 0, iMax = sizeof(input) / sizeof(input[0]),
  4637. j, jMax = sizeof(hashType) / sizeof(hashType[0]),
  4638. ret;
  4639. #ifdef HAVE_FIPS
  4640. /* FIPS requires a minimum length for HMAC keys, and "Jefe" is too
  4641. * short. Skip it in FIPS builds. */
  4642. i = 1;
  4643. #endif
  4644. for (; i < iMax; i++) {
  4645. for (j = 0; j < jMax; j++) {
  4646. if (wc_HmacInit(&hmac, HEAP_HINT, devId) != 0)
  4647. return -4000;
  4648. ret = wc_HmacSetKey(&hmac, hashType[j], (byte*)key[i],
  4649. (word32)XSTRLEN(key[i]));
  4650. if (ret != 0)
  4651. return -4001;
  4652. ret = wc_HmacUpdate(&hmac, (byte*)input[i],
  4653. (word32)XSTRLEN(input[i]));
  4654. if (ret != 0)
  4655. return -4002;
  4656. ret = wc_HmacFinal(&hmac, hash);
  4657. if (ret != 0)
  4658. return -4003;
  4659. if (XMEMCMP(hash, output[(i*jMax) + j], hashSz[j]) != 0)
  4660. return -4004;
  4661. wc_HmacFree(&hmac);
  4662. if (i > 0)
  4663. continue;
  4664. #ifndef HAVE_FIPS
  4665. ret = wc_HmacSizeByType(hashType[j]);
  4666. if (ret != hashSz[j])
  4667. return -4005;
  4668. #endif
  4669. }
  4670. }
  4671. return 0;
  4672. }
  4673. #endif
  4674. #ifdef WC_RC2
  4675. typedef struct rc2TestVector {
  4676. const char* input;
  4677. const char* output;
  4678. const char* key; /* Key, variable up to 128 bytes */
  4679. const char* iv; /* IV, 8-bytes */
  4680. int inLen;
  4681. int outLen;
  4682. int keyLen;
  4683. int effectiveKeyBits; /* Up to 1024 bits supported */
  4684. } rc2TestVector;
  4685. static int rc2_ecb_test(void)
  4686. {
  4687. int ret = 0;
  4688. byte cipher[RC2_BLOCK_SIZE];
  4689. byte plain[RC2_BLOCK_SIZE];
  4690. rc2TestVector a, b, c, d, e, f, g, h;
  4691. rc2TestVector test_rc2[8];
  4692. int times = sizeof(test_rc2) / sizeof(rc2TestVector), i;
  4693. a.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4694. a.output = "\xeb\xb7\x73\xf9\x93\x27\x8e\xff";
  4695. a.key = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4696. a.inLen = RC2_BLOCK_SIZE;
  4697. a.outLen = RC2_BLOCK_SIZE;
  4698. a.keyLen = 8;
  4699. a.effectiveKeyBits = 63;
  4700. b.input = "\xff\xff\xff\xff\xff\xff\xff\xff";
  4701. b.output = "\x27\x8b\x27\xe4\x2e\x2f\x0d\x49";
  4702. b.key = "\xff\xff\xff\xff\xff\xff\xff\xff";
  4703. b.inLen = RC2_BLOCK_SIZE;
  4704. b.outLen = RC2_BLOCK_SIZE;
  4705. b.keyLen = 8;
  4706. b.effectiveKeyBits = 64;
  4707. c.input = "\x10\x00\x00\x00\x00\x00\x00\x01";
  4708. c.output = "\x30\x64\x9e\xdf\x9b\xe7\xd2\xc2";
  4709. c.key = "\x30\x00\x00\x00\x00\x00\x00\x00";
  4710. c.inLen = RC2_BLOCK_SIZE;
  4711. c.outLen = RC2_BLOCK_SIZE;
  4712. c.keyLen = 8;
  4713. c.effectiveKeyBits = 64;
  4714. d.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4715. d.output = "\x61\xa8\xa2\x44\xad\xac\xcc\xf0";
  4716. d.key = "\x88";
  4717. d.inLen = RC2_BLOCK_SIZE;
  4718. d.outLen = RC2_BLOCK_SIZE;
  4719. d.keyLen = 1;
  4720. d.effectiveKeyBits = 64;
  4721. e.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4722. e.output = "\x6c\xcf\x43\x08\x97\x4c\x26\x7f";
  4723. e.key = "\x88\xbc\xa9\x0e\x90\x87\x5a";
  4724. e.inLen = RC2_BLOCK_SIZE;
  4725. e.outLen = RC2_BLOCK_SIZE;
  4726. e.keyLen = 7;
  4727. e.effectiveKeyBits = 64;
  4728. f.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4729. f.output = "\x1a\x80\x7d\x27\x2b\xbe\x5d\xb1";
  4730. f.key = "\x88\xbc\xa9\x0e\x90\x87\x5a\x7f"
  4731. "\x0f\x79\xc3\x84\x62\x7b\xaf\xb2";
  4732. f.inLen = RC2_BLOCK_SIZE;
  4733. f.outLen = RC2_BLOCK_SIZE;
  4734. f.keyLen = 16;
  4735. f.effectiveKeyBits = 64;
  4736. g.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4737. g.output = "\x22\x69\x55\x2a\xb0\xf8\x5c\xa6";
  4738. g.key = "\x88\xbc\xa9\x0e\x90\x87\x5a\x7f"
  4739. "\x0f\x79\xc3\x84\x62\x7b\xaf\xb2";
  4740. g.inLen = RC2_BLOCK_SIZE;
  4741. g.outLen = RC2_BLOCK_SIZE;
  4742. g.keyLen = 16;
  4743. g.effectiveKeyBits = 128;
  4744. h.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4745. h.output = "\x5b\x78\xd3\xa4\x3d\xff\xf1\xf1";
  4746. h.key = "\x88\xbc\xa9\x0e\x90\x87\x5a\x7f"
  4747. "\x0f\x79\xc3\x84\x62\x7b\xaf\xb2"
  4748. "\x16\xf8\x0a\x6f\x85\x92\x05\x84"
  4749. "\xc4\x2f\xce\xb0\xbe\x25\x5d\xaf"
  4750. "\x1e";
  4751. h.inLen = RC2_BLOCK_SIZE;
  4752. h.outLen = RC2_BLOCK_SIZE;
  4753. h.keyLen = 33;
  4754. h.effectiveKeyBits = 129;
  4755. a.iv = b.iv = c.iv = d.iv = e.iv = f.iv = g.iv = h.iv = NULL;
  4756. test_rc2[0] = a;
  4757. test_rc2[1] = b;
  4758. test_rc2[2] = c;
  4759. test_rc2[3] = d;
  4760. test_rc2[4] = e;
  4761. test_rc2[5] = f;
  4762. test_rc2[6] = g;
  4763. test_rc2[7] = h;
  4764. for (i = 0; i < times; ++i) {
  4765. Rc2 enc;
  4766. XMEMSET(cipher, 0, RC2_BLOCK_SIZE);
  4767. XMEMSET(plain, 0, RC2_BLOCK_SIZE);
  4768. ret = wc_Rc2SetKey(&enc, (byte*)test_rc2[i].key, test_rc2[i].keyLen,
  4769. NULL, test_rc2[i].effectiveKeyBits);
  4770. if (ret != 0) {
  4771. return -4100;
  4772. }
  4773. /* ECB encrypt */
  4774. ret = wc_Rc2EcbEncrypt(&enc, cipher, (byte*)test_rc2[i].input,
  4775. (word32)test_rc2[i].outLen);
  4776. if (ret != 0) {
  4777. return -4101;
  4778. }
  4779. if (XMEMCMP(cipher, test_rc2[i].output, test_rc2[i].outLen)) {
  4780. return -4102;
  4781. }
  4782. /* ECB decrypt */
  4783. ret = wc_Rc2EcbDecrypt(&enc, plain, cipher, RC2_BLOCK_SIZE);
  4784. if (ret != 0) {
  4785. return -4103;
  4786. }
  4787. if (XMEMCMP(plain, test_rc2[i].input, RC2_BLOCK_SIZE)) {
  4788. return -4104;
  4789. }
  4790. }
  4791. return 0;
  4792. }
  4793. static int rc2_cbc_test(void)
  4794. {
  4795. int ret = 0;
  4796. byte cipher[128];
  4797. byte plain[128];
  4798. rc2TestVector a, b, c, d, e, f, g, h, i;
  4799. rc2TestVector test_rc2[9];
  4800. int times = sizeof(test_rc2) / sizeof(rc2TestVector), j;
  4801. /* key length = 7, effective key bits = 63 */
  4802. a.input = "\x00\x00\x00\x00\x00\x00\x00\x00"
  4803. "\x00\x00\x00\x00\x00\x00\x00\x00";
  4804. a.output = "\xEB\xB7\x73\xF9\x93\x27\x8E\xFF"
  4805. "\xF0\x51\x77\x8B\x65\xDB\x13\x57";
  4806. a.key = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4807. a.iv = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4808. a.inLen = RC2_BLOCK_SIZE*2;
  4809. a.outLen = RC2_BLOCK_SIZE*2;
  4810. a.keyLen = 8;
  4811. a.effectiveKeyBits = 63;
  4812. /* key length = 8, effective key bits = 64, all 0xFF */
  4813. b.input = "\xff\xff\xff\xff\xff\xff\xff\xff"
  4814. "\xff\xff\xff\xff\xff\xff\xff\xff";
  4815. b.output = "\xA3\xA1\x12\x65\x4F\x81\xC5\xCD"
  4816. "\xB6\x94\x3E\xEA\x3E\x8B\x9D\x1F";
  4817. b.key = "\xff\xff\xff\xff\xff\xff\xff\xff";
  4818. b.iv = "\xff\xff\xff\xff\xff\xff\xff\xff";
  4819. b.inLen = RC2_BLOCK_SIZE*2;
  4820. b.outLen = RC2_BLOCK_SIZE*2;
  4821. b.keyLen = 8;
  4822. b.effectiveKeyBits = 64;
  4823. /* key length = 8, effective key bits = 64 */
  4824. c.input = "\x10\x00\x00\x00\x00\x00\x00\x01"
  4825. "\x10\x00\x00\x00\x00\x00\x00\x01";
  4826. c.output = "\xB5\x70\x14\xA2\x5F\x40\xE3\x6D"
  4827. "\x81\x99\x8D\xE0\xB5\xD5\x3A\x05";
  4828. c.key = "\x30\x00\x00\x00\x00\x00\x00\x00";
  4829. c.iv = "\x30\x00\x00\x00\x00\x00\x00\x00";
  4830. c.inLen = RC2_BLOCK_SIZE*2;
  4831. c.outLen = RC2_BLOCK_SIZE*2;
  4832. c.keyLen = 8;
  4833. c.effectiveKeyBits = 64;
  4834. /* key length = 1, effective key bits = 64 */
  4835. d.input = "\x00\x00\x00\x00\x00\x00\x00\x00"
  4836. "\x00\x00\x00\x00\x00\x00\x00\x00";
  4837. d.output = "\x61\xA8\xA2\x44\xAD\xAC\xCC\xF0"
  4838. "\x6D\x19\xE8\xF1\xFC\xE7\x38\x87";
  4839. d.key = "\x88";
  4840. d.iv = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4841. d.inLen = RC2_BLOCK_SIZE*2;
  4842. d.outLen = RC2_BLOCK_SIZE*2;
  4843. d.keyLen = 1;
  4844. d.effectiveKeyBits = 64;
  4845. /* key length = 7, effective key bits = 64 */
  4846. e.input = "\x00\x00\x00\x00\x00\x00\x00\x00"
  4847. "\x00\x00\x00\x00\x00\x00\x00\x00";
  4848. e.output = "\x6C\xCF\x43\x08\x97\x4C\x26\x7F"
  4849. "\xCC\x3C\x53\x57\x7C\xA1\xA4\x4B";
  4850. e.key = "\x88\xbc\xa9\x0e\x90\x87\x5a";
  4851. e.iv = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4852. e.inLen = RC2_BLOCK_SIZE*2;
  4853. e.outLen = RC2_BLOCK_SIZE*2;
  4854. e.keyLen = 7;
  4855. e.effectiveKeyBits = 64;
  4856. /* key length = 16, effective key bits = 64 */
  4857. f.input = "\x00\x00\x00\x00\x00\x00\x00\x00"
  4858. "\x00\x00\x00\x00\x00\x00\x00\x00";
  4859. f.output = "\x1A\x80\x7D\x27\x2B\xBE\x5D\xB1"
  4860. "\x64\xEF\xE1\xC3\xB8\xAD\xFB\xBA";
  4861. f.key = "\x88\xbc\xa9\x0e\x90\x87\x5a\x7f"
  4862. "\x0f\x79\xc3\x84\x62\x7b\xaf\xb2";
  4863. f.iv = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4864. f.inLen = RC2_BLOCK_SIZE*2;
  4865. f.outLen = RC2_BLOCK_SIZE*2;
  4866. f.keyLen = 16;
  4867. f.effectiveKeyBits = 64;
  4868. /* key length = 16, effective bits = 128 */
  4869. g.input = "\x00\x00\x00\x00\x00\x00\x00\x00"
  4870. "\x00\x00\x00\x00\x00\x00\x00\x00";
  4871. g.output = "\x22\x69\x55\x2A\xB0\xF8\x5C\xA6"
  4872. "\x53\x6E\xFD\x2D\x89\xE1\x2A\x73";
  4873. g.key = "\x88\xbc\xa9\x0e\x90\x87\x5a\x7f"
  4874. "\x0f\x79\xc3\x84\x62\x7b\xaf\xb2";
  4875. g.iv = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4876. g.inLen = RC2_BLOCK_SIZE*2;
  4877. g.outLen = RC2_BLOCK_SIZE*2;
  4878. g.keyLen = 16;
  4879. g.effectiveKeyBits = 128;
  4880. /* key length = 33, effective bits = 129 */
  4881. h.input = "\x00\x00\x00\x00\x00\x00\x00\x00"
  4882. "\x00\x00\x00\x00\x00\x00\x00\x00";
  4883. h.output = "\x5B\x78\xD3\xA4\x3D\xFF\xF1\xF1"
  4884. "\x45\x30\xA8\xD5\xC7\x7C\x46\x19";
  4885. h.key = "\x88\xbc\xa9\x0e\x90\x87\x5a\x7f"
  4886. "\x0f\x79\xc3\x84\x62\x7b\xaf\xb2"
  4887. "\x16\xf8\x0a\x6f\x85\x92\x05\x84"
  4888. "\xc4\x2f\xce\xb0\xbe\x25\x5d\xaf"
  4889. "\x1e";
  4890. h.iv = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4891. h.inLen = RC2_BLOCK_SIZE*2;
  4892. h.outLen = RC2_BLOCK_SIZE*2;
  4893. h.keyLen = 33;
  4894. h.effectiveKeyBits = 129;
  4895. /* key length = 10, effective bits = 40 */
  4896. i.input = "\x11\x22\x33\x44\x55\x66\x77\x88"
  4897. "\x99\xAA\xBB\xCC\xDD\xEE\xFF\x00"
  4898. "\x11\x22\x33\x44\x55\x66\x77\x88"
  4899. "\x99\xAA\xBB\xCC\xDD\xEE\xFF\x00";
  4900. i.output = "\x71\x2D\x11\x99\xC9\xA0\x78\x4F"
  4901. "\xCD\xF1\x1E\x3D\xFD\x21\x7E\xDB"
  4902. "\xB2\x6E\x0D\xA4\x72\xBC\x31\x51"
  4903. "\x48\xEF\x4E\x68\x3B\xDC\xCD\x7D";
  4904. i.key = "\x26\x1E\x57\x8E\xC9\x62\xBF\xB8"
  4905. "\x3E\x96";
  4906. i.iv = "\x01\x02\x03\x04\x05\x06\x07\x08";
  4907. i.inLen = RC2_BLOCK_SIZE*4;
  4908. i.outLen = RC2_BLOCK_SIZE*4;
  4909. i.keyLen = 10;
  4910. i.effectiveKeyBits = 40;
  4911. test_rc2[0] = a;
  4912. test_rc2[1] = b;
  4913. test_rc2[2] = c;
  4914. test_rc2[3] = d;
  4915. test_rc2[4] = e;
  4916. test_rc2[5] = f;
  4917. test_rc2[6] = g;
  4918. test_rc2[7] = h;
  4919. test_rc2[8] = i;
  4920. for (j = 0; j < times; ++j) {
  4921. Rc2 rc2;
  4922. XMEMSET(cipher, 0, sizeof(cipher));
  4923. XMEMSET(plain, 0, sizeof(plain));
  4924. ret = wc_Rc2SetKey(&rc2, (byte*)test_rc2[j].key, test_rc2[j].keyLen,
  4925. (byte*)test_rc2[j].iv, test_rc2[j].effectiveKeyBits);
  4926. if (ret != 0) {
  4927. return -4200;
  4928. }
  4929. ret = wc_Rc2CbcEncrypt(&rc2, cipher, (byte*)test_rc2[j].input,
  4930. test_rc2[j].inLen);
  4931. if (ret != 0) {
  4932. return -4201;
  4933. }
  4934. if (XMEMCMP(cipher, (byte*)test_rc2[j].output, test_rc2[j].outLen)) {
  4935. return -4202;
  4936. }
  4937. /* reset IV for decrypt, since overriden by encrypt operation */
  4938. ret = wc_Rc2SetIV(&rc2, (byte*)test_rc2[j].iv);
  4939. if (ret != 0) {
  4940. return -4203;
  4941. }
  4942. ret = wc_Rc2CbcDecrypt(&rc2, plain, cipher, test_rc2[j].outLen);
  4943. if (ret != 0) {
  4944. return -4204;
  4945. }
  4946. if (XMEMCMP(plain, (byte*)test_rc2[j].input, test_rc2[j].inLen)) {
  4947. return -4205;
  4948. }
  4949. }
  4950. return 0;
  4951. }
  4952. WOLFSSL_TEST_SUBROUTINE int rc2_test(void)
  4953. {
  4954. int ret = 0;
  4955. ret = rc2_ecb_test();
  4956. if (ret != 0) {
  4957. return ret;
  4958. }
  4959. return rc2_cbc_test();
  4960. }
  4961. #endif
  4962. #ifndef NO_RC4
  4963. WOLFSSL_TEST_SUBROUTINE int arc4_test(void)
  4964. {
  4965. byte cipher[16];
  4966. byte plain[16];
  4967. const char* keys[] =
  4968. {
  4969. "\x01\x23\x45\x67\x89\xab\xcd\xef",
  4970. "\x01\x23\x45\x67\x89\xab\xcd\xef",
  4971. "\x00\x00\x00\x00\x00\x00\x00\x00",
  4972. "\xef\x01\x23\x45"
  4973. };
  4974. testVector a, b, c, d;
  4975. testVector test_arc4[4];
  4976. int times = sizeof(test_arc4) / sizeof(testVector), i;
  4977. a.input = "\x01\x23\x45\x67\x89\xab\xcd\xef";
  4978. a.output = "\x75\xb7\x87\x80\x99\xe0\xc5\x96";
  4979. a.inLen = 8;
  4980. a.outLen = 8;
  4981. b.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4982. b.output = "\x74\x94\xc2\xe7\x10\x4b\x08\x79";
  4983. b.inLen = 8;
  4984. b.outLen = 8;
  4985. c.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4986. c.output = "\xde\x18\x89\x41\xa3\x37\x5d\x3a";
  4987. c.inLen = 8;
  4988. c.outLen = 8;
  4989. d.input = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";
  4990. d.output = "\xd6\xa1\x41\xa7\xec\x3c\x38\xdf\xbd\x61";
  4991. d.inLen = 10;
  4992. d.outLen = 10;
  4993. test_arc4[0] = a;
  4994. test_arc4[1] = b;
  4995. test_arc4[2] = c;
  4996. test_arc4[3] = d;
  4997. for (i = 0; i < times; ++i) {
  4998. Arc4 enc;
  4999. Arc4 dec;
  5000. int keylen = 8; /* XSTRLEN with key 0x00 not good */
  5001. if (i == 3)
  5002. keylen = 4;
  5003. if (wc_Arc4Init(&enc, HEAP_HINT, devId) != 0)
  5004. return -4400;
  5005. if (wc_Arc4Init(&dec, HEAP_HINT, devId) != 0)
  5006. return -4401;
  5007. wc_Arc4SetKey(&enc, (byte*)keys[i], keylen);
  5008. wc_Arc4SetKey(&dec, (byte*)keys[i], keylen);
  5009. wc_Arc4Process(&enc, cipher, (byte*)test_arc4[i].input,
  5010. (word32)test_arc4[i].outLen);
  5011. wc_Arc4Process(&dec, plain, cipher, (word32)test_arc4[i].outLen);
  5012. if (XMEMCMP(plain, test_arc4[i].input, test_arc4[i].outLen))
  5013. return -4402 - i;
  5014. if (XMEMCMP(cipher, test_arc4[i].output, test_arc4[i].outLen))
  5015. return -4412 - i;
  5016. wc_Arc4Free(&enc);
  5017. wc_Arc4Free(&dec);
  5018. }
  5019. return 0;
  5020. }
  5021. #endif
  5022. #ifdef HAVE_CHACHA
  5023. WOLFSSL_TEST_SUBROUTINE int chacha_test(void)
  5024. {
  5025. ChaCha enc;
  5026. ChaCha dec;
  5027. byte cipher[128];
  5028. byte plain[128];
  5029. byte sliver[64];
  5030. byte input[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  5031. word32 keySz = 32;
  5032. int ret = 0;
  5033. int i;
  5034. int times = 4;
  5035. WOLFSSL_SMALL_STACK_STATIC const byte key1[] =
  5036. {
  5037. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5038. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5039. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5040. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  5041. };
  5042. WOLFSSL_SMALL_STACK_STATIC const byte key2[] =
  5043. {
  5044. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5045. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5046. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5047. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  5048. };
  5049. WOLFSSL_SMALL_STACK_STATIC const byte key3[] =
  5050. {
  5051. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5052. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5053. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5054. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  5055. };
  5056. /* 128 bit key */
  5057. WOLFSSL_SMALL_STACK_STATIC const byte key4[] =
  5058. {
  5059. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5060. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  5061. };
  5062. const byte* keys[] = {key1, key2, key3, key4};
  5063. WOLFSSL_SMALL_STACK_STATIC const byte ivs1[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
  5064. WOLFSSL_SMALL_STACK_STATIC const byte ivs2[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
  5065. WOLFSSL_SMALL_STACK_STATIC const byte ivs3[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00};
  5066. WOLFSSL_SMALL_STACK_STATIC const byte ivs4[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
  5067. const byte* ivs[] = {ivs1, ivs2, ivs3, ivs4};
  5068. #ifndef BENCH_EMBEDDED
  5069. WOLFSSL_SMALL_STACK_STATIC const byte cipher_big_result[] = {
  5070. 0x06, 0xa6, 0x5d, 0x31, 0x21, 0x6c, 0xdb, 0x37, 0x48, 0x7c, 0x01, 0x9d,
  5071. 0x72, 0xdf, 0x0a, 0x5b, 0x64, 0x74, 0x20, 0xba, 0x9e, 0xe0, 0x26, 0x7a,
  5072. 0xbf, 0xdf, 0x83, 0x34, 0x3b, 0x4f, 0x94, 0x3f, 0x37, 0x89, 0xaf, 0x00,
  5073. 0xdf, 0x0f, 0x2e, 0x75, 0x16, 0x41, 0xf6, 0x7a, 0x86, 0x94, 0x9d, 0x32,
  5074. 0x56, 0xf0, 0x79, 0x71, 0x68, 0x6f, 0xa6, 0x6b, 0xc6, 0x59, 0x49, 0xf6,
  5075. 0x10, 0x34, 0x03, 0x03, 0x16, 0x53, 0x9a, 0x98, 0x2a, 0x46, 0xde, 0x17,
  5076. 0x06, 0x65, 0x70, 0xca, 0x0a, 0x1f, 0xab, 0x80, 0x26, 0x96, 0x3f, 0x3e,
  5077. 0x7a, 0x3c, 0xa8, 0x87, 0xbb, 0x65, 0xdd, 0x5e, 0x07, 0x7b, 0x34, 0xe0,
  5078. 0x56, 0xda, 0x32, 0x13, 0x30, 0xc9, 0x0c, 0xd7, 0xba, 0xe4, 0x1f, 0xa6,
  5079. 0x91, 0x4f, 0x72, 0x9f, 0xd9, 0x5c, 0x62, 0x7d, 0xa6, 0xc2, 0xbc, 0x87,
  5080. 0xae, 0x64, 0x11, 0x94, 0x3b, 0xbc, 0x6c, 0x23, 0xbd, 0x7d, 0x00, 0xb4,
  5081. 0x99, 0xf2, 0x68, 0xb5, 0x59, 0x70, 0x93, 0xad, 0x69, 0xd0, 0xb1, 0x28,
  5082. 0x70, 0x92, 0xeb, 0xec, 0x39, 0x80, 0x82, 0xde, 0x44, 0xe2, 0x8a, 0x26,
  5083. 0xb3, 0xe9, 0x45, 0xcf, 0x83, 0x76, 0x9f, 0x6a, 0xa0, 0x46, 0x4a, 0x3d,
  5084. 0x26, 0x56, 0xaf, 0x49, 0x41, 0x26, 0x1b, 0x6a, 0x41, 0x37, 0x65, 0x91,
  5085. 0x72, 0xc4, 0xe7, 0x3c, 0x17, 0x31, 0xae, 0x2e, 0x2b, 0x31, 0x45, 0xe4,
  5086. 0x93, 0xd3, 0x10, 0xaa, 0xc5, 0x62, 0xd5, 0x11, 0x4b, 0x57, 0x1d, 0xad,
  5087. 0x48, 0x06, 0xd0, 0x0d, 0x98, 0xa5, 0xc6, 0x5b, 0xd0, 0x9e, 0x22, 0xc0,
  5088. 0x00, 0x32, 0x5a, 0xf5, 0x1c, 0x89, 0x6d, 0x54, 0x97, 0x55, 0x6b, 0x46,
  5089. 0xc5, 0xc7, 0xc4, 0x48, 0x9c, 0xbf, 0x47, 0xdc, 0x03, 0xc4, 0x1b, 0xcb,
  5090. 0x65, 0xa6, 0x91, 0x9d, 0x6d, 0xf1, 0xb0, 0x7a, 0x4d, 0x3b, 0x03, 0x95,
  5091. 0xf4, 0x8b, 0x0b, 0xae, 0x39, 0xff, 0x3f, 0xf6, 0xc0, 0x14, 0x18, 0x8a,
  5092. 0xe5, 0x19, 0xbd, 0xc1, 0xb4, 0x05, 0x4e, 0x29, 0x2f, 0x0b, 0x33, 0x76,
  5093. 0x28, 0x16, 0xa4, 0xa6, 0x93, 0x04, 0xb5, 0x55, 0x6b, 0x89, 0x3d, 0xa5,
  5094. 0x0f, 0xd3, 0xad, 0xfa, 0xd9, 0xfd, 0x05, 0x5d, 0x48, 0x94, 0x25, 0x5a,
  5095. 0x2c, 0x9a, 0x94, 0x80, 0xb0, 0xe7, 0xcb, 0x4d, 0x77, 0xbf, 0xca, 0xd8,
  5096. 0x55, 0x48, 0xbd, 0x66, 0xb1, 0x85, 0x81, 0xb1, 0x37, 0x79, 0xab, 0x52,
  5097. 0x08, 0x14, 0x12, 0xac, 0xcd, 0x45, 0x4d, 0x53, 0x6b, 0xca, 0x96, 0xc7,
  5098. 0x3b, 0x2f, 0x73, 0xb1, 0x5a, 0x23, 0xbd, 0x65, 0xd5, 0xea, 0x17, 0xb3,
  5099. 0xdc, 0xa1, 0x17, 0x1b, 0x2d, 0xb3, 0x9c, 0xd0, 0xdb, 0x41, 0x77, 0xef,
  5100. 0x93, 0x20, 0x52, 0x3e, 0x9d, 0xf5, 0xbf, 0x33, 0xf7, 0x52, 0xc1, 0x90,
  5101. 0xa0, 0x15, 0x17, 0xce, 0xf7, 0xf7, 0xd0, 0x3a, 0x3b, 0xd1, 0x72, 0x56,
  5102. 0x31, 0x81, 0xae, 0x60, 0xab, 0x40, 0xc1, 0xd1, 0x28, 0x77, 0x53, 0xac,
  5103. 0x9f, 0x11, 0x0a, 0x88, 0x36, 0x4b, 0xda, 0x57, 0xa7, 0x28, 0x5c, 0x85,
  5104. 0xd3, 0x85, 0x9b, 0x79, 0xad, 0x05, 0x1c, 0x37, 0x14, 0x5e, 0x0d, 0xd0,
  5105. 0x23, 0x03, 0x42, 0x1d, 0x48, 0x5d, 0xc5, 0x3c, 0x5a, 0x08, 0xa9, 0x0d,
  5106. 0x6e, 0x82, 0x7c, 0x2e, 0x3c, 0x41, 0xcc, 0x96, 0x8e, 0xad, 0xee, 0x2a,
  5107. 0x61, 0x0b, 0x16, 0x0f, 0xa9, 0x24, 0x40, 0x85, 0xbc, 0x9f, 0x28, 0x8d,
  5108. 0xe6, 0x68, 0x4d, 0x8f, 0x30, 0x48, 0xd9, 0x73, 0x73, 0x6c, 0x9a, 0x7f,
  5109. 0x67, 0xf7, 0xde, 0x4c, 0x0a, 0x8b, 0xe4, 0xb3, 0x08, 0x2a, 0x52, 0xda,
  5110. 0x54, 0xee, 0xcd, 0xb5, 0x62, 0x4a, 0x26, 0x20, 0xfb, 0x40, 0xbb, 0x39,
  5111. 0x3a, 0x0f, 0x09, 0xe8, 0x00, 0xd1, 0x24, 0x97, 0x60, 0xe9, 0x83, 0x83,
  5112. 0xfe, 0x9f, 0x9c, 0x15, 0xcf, 0x69, 0x03, 0x9f, 0x03, 0xe1, 0xe8, 0x6e,
  5113. 0xbd, 0x87, 0x58, 0x68, 0xee, 0xec, 0xd8, 0x29, 0x46, 0x23, 0x49, 0x92,
  5114. 0x72, 0x95, 0x5b, 0x49, 0xca, 0xe0, 0x45, 0x59, 0xb2, 0xca, 0xf4, 0xfc,
  5115. 0xb7, 0x59, 0x37, 0x49, 0x28, 0xbc, 0xf3, 0xd7, 0x61, 0xbc, 0x4b, 0xf3,
  5116. 0xa9, 0x4b, 0x2f, 0x05, 0xa8, 0x01, 0xa5, 0xdc, 0x00, 0x6e, 0x01, 0xb6,
  5117. 0x45, 0x3c, 0xd5, 0x49, 0x7d, 0x5c, 0x25, 0xe8, 0x31, 0x87, 0xb2, 0xb9,
  5118. 0xbf, 0xb3, 0x01, 0x62, 0x0c, 0xd0, 0x48, 0x77, 0xa2, 0x34, 0x0f, 0x16,
  5119. 0x22, 0x28, 0xee, 0x54, 0x08, 0x93, 0x3b, 0xe4, 0xde, 0x7e, 0x63, 0xf7,
  5120. 0x97, 0x16, 0x5d, 0x71, 0x58, 0xc2, 0x2e, 0xf2, 0x36, 0xa6, 0x12, 0x65,
  5121. 0x94, 0x17, 0xac, 0x66, 0x23, 0x7e, 0xc6, 0x72, 0x79, 0x24, 0xce, 0x8f,
  5122. 0x55, 0x19, 0x97, 0x44, 0xfc, 0x55, 0xec, 0x85, 0x26, 0x27, 0xdb, 0x38,
  5123. 0xb1, 0x42, 0x0a, 0xdd, 0x05, 0x99, 0x28, 0xeb, 0x03, 0x6c, 0x9a, 0xe9,
  5124. 0x17, 0xf6, 0x2c, 0xb0, 0xfe, 0xe7, 0xa4, 0xa7, 0x31, 0xda, 0x4d, 0xb0,
  5125. 0x29, 0xdb, 0xdd, 0x8d, 0x12, 0x13, 0x9c, 0xb4, 0xcc, 0x83, 0x97, 0xfb,
  5126. 0x1a, 0xdc, 0x08, 0xd6, 0x30, 0x62, 0xe8, 0xeb, 0x8b, 0x61, 0xcb, 0x1d,
  5127. 0x06, 0xe3, 0xa5, 0x4d, 0x35, 0xdb, 0x59, 0xa8, 0x2d, 0x87, 0x27, 0x44,
  5128. 0x6f, 0xc0, 0x38, 0x97, 0xe4, 0x85, 0x00, 0x02, 0x09, 0xf6, 0x69, 0x3a,
  5129. 0xcf, 0x08, 0x1b, 0x21, 0xbb, 0x79, 0xb1, 0xa1, 0x34, 0x09, 0xe0, 0x80,
  5130. 0xca, 0xb0, 0x78, 0x8a, 0x11, 0x97, 0xd4, 0x07, 0xbe, 0x1b, 0x6a, 0x5d,
  5131. 0xdb, 0xd6, 0x1f, 0x76, 0x6b, 0x16, 0xf0, 0x58, 0x84, 0x5f, 0x59, 0xce,
  5132. 0x62, 0x34, 0xc3, 0xdf, 0x94, 0xb8, 0x2f, 0x84, 0x68, 0xf0, 0xb8, 0x51,
  5133. 0xd9, 0x6d, 0x8e, 0x4a, 0x1d, 0xe6, 0x5c, 0xd8, 0x86, 0x25, 0xe3, 0x24,
  5134. 0xfd, 0x21, 0x61, 0x13, 0x48, 0x3e, 0xf6, 0x7d, 0xa6, 0x71, 0x9b, 0xd2,
  5135. 0x6e, 0xe6, 0xd2, 0x08, 0x94, 0x62, 0x6c, 0x98, 0xfe, 0x2f, 0x9c, 0x88,
  5136. 0x7e, 0x78, 0x15, 0x02, 0x00, 0xf0, 0xba, 0x24, 0x91, 0xf2, 0xdc, 0x47,
  5137. 0x51, 0x4d, 0x15, 0x5e, 0x91, 0x5f, 0x57, 0x5b, 0x1d, 0x35, 0x24, 0x45,
  5138. 0x75, 0x9b, 0x88, 0x75, 0xf1, 0x2f, 0x85, 0xe7, 0x89, 0xd1, 0x01, 0xb4,
  5139. 0xc8, 0x18, 0xb7, 0x97, 0xef, 0x4b, 0x90, 0xf4, 0xbf, 0x10, 0x27, 0x3c,
  5140. 0x60, 0xff, 0xc4, 0x94, 0x20, 0x2f, 0x93, 0x4b, 0x4d, 0xe3, 0x80, 0xf7,
  5141. 0x2c, 0x71, 0xd9, 0xe3, 0x68, 0xb4, 0x77, 0x2b, 0xc7, 0x0d, 0x39, 0x92,
  5142. 0xef, 0x91, 0x0d, 0xb2, 0x11, 0x50, 0x0e, 0xe8, 0xad, 0x3b, 0xf6, 0xb5,
  5143. 0xc6, 0x14, 0x4d, 0x33, 0x53, 0xa7, 0x60, 0x15, 0xc7, 0x27, 0x51, 0xdc,
  5144. 0x54, 0x29, 0xa7, 0x0d, 0x6a, 0x7b, 0x72, 0x13, 0xad, 0x7d, 0x41, 0x19,
  5145. 0x4e, 0x42, 0x49, 0xcc, 0x42, 0xe4, 0xbd, 0x99, 0x13, 0xd9, 0x7f, 0xf3,
  5146. 0x38, 0xa4, 0xb6, 0x33, 0xed, 0x07, 0x48, 0x7e, 0x8e, 0x82, 0xfe, 0x3a,
  5147. 0x9d, 0x75, 0x93, 0xba, 0x25, 0x4e, 0x37, 0x3c, 0x0c, 0xd5, 0x69, 0xa9,
  5148. 0x2d, 0x9e, 0xfd, 0xe8, 0xbb, 0xf5, 0x0c, 0xe2, 0x86, 0xb9, 0x5e, 0x6f,
  5149. 0x28, 0xe4, 0x19, 0xb3, 0x0b, 0xa4, 0x86, 0xd7, 0x24, 0xd0, 0xb8, 0x89,
  5150. 0x7b, 0x76, 0xec, 0x05, 0x10, 0x5b, 0x68, 0xe9, 0x58, 0x66, 0xa3, 0xc5,
  5151. 0xb6, 0x63, 0x20, 0x0e, 0x0e, 0xea, 0x3d, 0x61, 0x5e, 0xda, 0x3d, 0x3c,
  5152. 0xf9, 0xfd, 0xed, 0xa9, 0xdb, 0x52, 0x94, 0x8a, 0x00, 0xca, 0x3c, 0x8d,
  5153. 0x66, 0x8f, 0xb0, 0xf0, 0x5a, 0xca, 0x3f, 0x63, 0x71, 0xbf, 0xca, 0x99,
  5154. 0x37, 0x9b, 0x75, 0x97, 0x89, 0x10, 0x6e, 0xcf, 0xf2, 0xf5, 0xe3, 0xd5,
  5155. 0x45, 0x9b, 0xad, 0x10, 0x71, 0x6c, 0x5f, 0x6f, 0x7f, 0x22, 0x77, 0x18,
  5156. 0x2f, 0xf9, 0x99, 0xc5, 0x69, 0x58, 0x03, 0x12, 0x86, 0x82, 0x3e, 0xbf,
  5157. 0xc2, 0x12, 0x35, 0x43, 0xa3, 0xd9, 0x18, 0x4f, 0x41, 0x11, 0x6b, 0xf3,
  5158. 0x67, 0xaf, 0x3d, 0x78, 0xe4, 0x22, 0x2d, 0xb3, 0x48, 0x43, 0x31, 0x1d,
  5159. 0xef, 0xa8, 0xba, 0x49, 0x8e, 0xa9, 0xa7, 0xb6, 0x18, 0x77, 0x84, 0xca,
  5160. 0xbd, 0xa2, 0x02, 0x1b, 0x6a, 0xf8, 0x5f, 0xda, 0xff, 0xcf, 0x01, 0x6a,
  5161. 0x86, 0x69, 0xa9, 0xe9, 0xcb, 0x60, 0x1e, 0x15, 0xdc, 0x8f, 0x5d, 0x39,
  5162. 0xb5, 0xce, 0x55, 0x5f, 0x47, 0x97, 0xb1, 0x19, 0x6e, 0x21, 0xd6, 0x13,
  5163. 0x39, 0xb2, 0x24, 0xe0, 0x62, 0x82, 0x9f, 0xed, 0x12, 0x81, 0xed, 0xee,
  5164. 0xab, 0xd0, 0x2f, 0x19, 0x89, 0x3f, 0x57, 0x2e, 0xc2, 0xe2, 0x67, 0xe8,
  5165. 0xae, 0x03, 0x56, 0xba, 0xd4, 0xd0, 0xa4, 0x89, 0x03, 0x06, 0x5b, 0xcc,
  5166. 0xf2, 0x22, 0xb8, 0x0e, 0x76, 0x79, 0x4a, 0x42, 0x1d, 0x37, 0x51, 0x5a,
  5167. 0xaa, 0x46, 0x6c, 0x2a, 0xdd, 0x66, 0xfe, 0xc6, 0x68, 0xc3, 0x38, 0xa2,
  5168. 0xae, 0x5b, 0x98, 0x24, 0x5d, 0x43, 0x05, 0x82, 0x38, 0x12, 0xd3, 0xd1,
  5169. 0x75, 0x2d, 0x4f, 0x61, 0xbd, 0xb9, 0x10, 0x87, 0x44, 0x2a, 0x78, 0x07,
  5170. 0xff, 0xf4, 0x0f, 0xa1, 0xf3, 0x68, 0x9f, 0xbe, 0xae, 0xa2, 0x91, 0xf0,
  5171. 0xc7, 0x55, 0x7a, 0x52, 0xd5, 0xa3, 0x8d, 0x6f, 0xe4, 0x90, 0x5c, 0xf3,
  5172. 0x5f, 0xce, 0x3d, 0x23, 0xf9, 0x8e, 0xae, 0x14, 0xfb, 0x82, 0x9a, 0xa3,
  5173. 0x04, 0x5f, 0xbf, 0xad, 0x3e, 0xf2, 0x97, 0x0a, 0x60, 0x40, 0x70, 0x19,
  5174. 0x72, 0xad, 0x66, 0xfb, 0x78, 0x1b, 0x84, 0x6c, 0x98, 0xbc, 0x8c, 0xf8,
  5175. 0x4f, 0xcb, 0xb5, 0xf6, 0xaf, 0x7a, 0xb7, 0x93, 0xef, 0x67, 0x48, 0x02,
  5176. 0x2c, 0xcb, 0xe6, 0x77, 0x0f, 0x7b, 0xc1, 0xee, 0xc5, 0xb6, 0x2d, 0x7e,
  5177. 0x62, 0xa0, 0xc0, 0xa7, 0xa5, 0x80, 0x31, 0x92, 0x50, 0xa1, 0x28, 0x22,
  5178. 0x95, 0x03, 0x17, 0xd1, 0x0f, 0xf6, 0x08, 0xe5, 0xec
  5179. };
  5180. #define CHACHA_BIG_TEST_SIZE 1305
  5181. #if !defined(WOLFSSL_SMALL_STACK) || defined(WOLFSSL_NO_MALLOC)
  5182. byte cipher_big[CHACHA_BIG_TEST_SIZE] = {0};
  5183. byte plain_big[CHACHA_BIG_TEST_SIZE] = {0};
  5184. byte input_big[CHACHA_BIG_TEST_SIZE] = {0};
  5185. #else
  5186. byte* cipher_big;
  5187. byte* plain_big;
  5188. byte* input_big;
  5189. #endif /* WOLFSSL_SMALL_STACK && !WOLFSSL_NO_MALLOC */
  5190. int block_size;
  5191. #endif /* BENCH_EMBEDDED */
  5192. byte a[] = {0x76,0xb8,0xe0,0xad,0xa0,0xf1,0x3d,0x90};
  5193. byte b[] = {0x45,0x40,0xf0,0x5a,0x9f,0x1f,0xb2,0x96};
  5194. byte c[] = {0xde,0x9c,0xba,0x7b,0xf3,0xd6,0x9e,0xf5};
  5195. byte d[] = {0x89,0x67,0x09,0x52,0x60,0x83,0x64,0xfd};
  5196. byte* test_chacha[4];
  5197. test_chacha[0] = a;
  5198. test_chacha[1] = b;
  5199. test_chacha[2] = c;
  5200. test_chacha[3] = d;
  5201. #ifndef BENCH_EMBEDDED
  5202. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  5203. cipher_big = (byte*)XMALLOC(CHACHA_BIG_TEST_SIZE, HEAP_HINT,
  5204. DYNAMIC_TYPE_TMP_BUFFER);
  5205. if (cipher_big == NULL) {
  5206. return MEMORY_E;
  5207. }
  5208. plain_big = (byte*)XMALLOC(CHACHA_BIG_TEST_SIZE, HEAP_HINT,
  5209. DYNAMIC_TYPE_TMP_BUFFER);
  5210. if (plain_big == NULL) {
  5211. return MEMORY_E;
  5212. }
  5213. input_big = (byte*)XMALLOC(CHACHA_BIG_TEST_SIZE, HEAP_HINT,
  5214. DYNAMIC_TYPE_TMP_BUFFER);
  5215. if (input_big == NULL) {
  5216. return MEMORY_E;
  5217. }
  5218. XMEMSET(cipher_big, 0, CHACHA_BIG_TEST_SIZE);
  5219. XMEMSET(plain_big, 0, CHACHA_BIG_TEST_SIZE);
  5220. XMEMSET(input_big, 0, CHACHA_BIG_TEST_SIZE);
  5221. #endif /* WOLFSSL_SMALL_STACK && !WOLFSSL_NO_MALLOC */
  5222. #endif /* BENCH_EMBEDDED */
  5223. for (i = 0; i < times; ++i) {
  5224. if (i < 3) {
  5225. keySz = 32;
  5226. }
  5227. else {
  5228. keySz = 16;
  5229. }
  5230. XMEMCPY(plain, keys[i], keySz);
  5231. XMEMSET(cipher, 0, 32);
  5232. XMEMCPY(cipher + 4, ivs[i], 8);
  5233. ret |= wc_Chacha_SetKey(&enc, keys[i], keySz);
  5234. ret |= wc_Chacha_SetKey(&dec, keys[i], keySz);
  5235. if (ret != 0)
  5236. return ret;
  5237. ret |= wc_Chacha_SetIV(&enc, cipher, 0);
  5238. ret |= wc_Chacha_SetIV(&dec, cipher, 0);
  5239. if (ret != 0)
  5240. return ret;
  5241. XMEMCPY(plain, input, 8);
  5242. ret |= wc_Chacha_Process(&enc, cipher, plain, (word32)8);
  5243. ret |= wc_Chacha_Process(&dec, plain, cipher, (word32)8);
  5244. if (ret != 0)
  5245. return ret;
  5246. if (XMEMCMP(test_chacha[i], cipher, 8))
  5247. return -4700 - i;
  5248. if (XMEMCMP(plain, input, 8))
  5249. return -4710 - i;
  5250. }
  5251. /* test of starting at a different counter
  5252. encrypts all of the information and decrypts starting at 2nd chunk */
  5253. XMEMSET(plain, 0, sizeof(plain));
  5254. XMEMSET(sliver, 1, sizeof(sliver)); /* set as 1's to not match plain */
  5255. XMEMSET(cipher, 0, sizeof(cipher));
  5256. XMEMCPY(cipher + 4, ivs[0], 8);
  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, cipher, 0);
  5262. ret |= wc_Chacha_SetIV(&dec, cipher, 1);
  5263. if (ret != 0)
  5264. return ret;
  5265. ret |= wc_Chacha_Process(&enc, cipher, plain, sizeof(plain));
  5266. ret |= wc_Chacha_Process(&dec, sliver, cipher + 64, sizeof(sliver));
  5267. if (ret != 0)
  5268. return ret;
  5269. if (XMEMCMP(plain + 64, sliver, 64))
  5270. return -4720;
  5271. #ifndef BENCH_EMBEDDED
  5272. /* test of encrypting more data */
  5273. keySz = 32;
  5274. ret |= wc_Chacha_SetKey(&enc, keys[0], keySz);
  5275. ret |= wc_Chacha_SetKey(&dec, keys[0], keySz);
  5276. if (ret != 0)
  5277. return ret;
  5278. ret |= wc_Chacha_SetIV(&enc, ivs[2], 0);
  5279. ret |= wc_Chacha_SetIV(&dec, ivs[2], 0);
  5280. if (ret != 0)
  5281. return ret;
  5282. ret |= wc_Chacha_Process(&enc, cipher_big, plain_big, CHACHA_BIG_TEST_SIZE);
  5283. ret |= wc_Chacha_Process(&dec, plain_big, cipher_big,
  5284. CHACHA_BIG_TEST_SIZE);
  5285. if (ret != 0)
  5286. return ret;
  5287. if (XMEMCMP(plain_big, input_big, CHACHA_BIG_TEST_SIZE))
  5288. return -4721;
  5289. if (XMEMCMP(cipher_big, cipher_big_result, CHACHA_BIG_TEST_SIZE))
  5290. return -4722;
  5291. for (i = 0; i < 18; ++i) {
  5292. /* this will test all paths
  5293. * block sizes: 1 3 7 15 31 63 127 255 511 (i = 0- 8)
  5294. * 2 4 8 16 32 64 128 256 512 (i = 9-17)
  5295. */
  5296. block_size = (2 << (i%9)) - (i<9?1:0);
  5297. keySz = 32;
  5298. ret |= wc_Chacha_SetKey(&enc, keys[0], keySz);
  5299. ret |= wc_Chacha_SetKey(&dec, keys[0], keySz);
  5300. if (ret != 0)
  5301. return ret;
  5302. ret |= wc_Chacha_SetIV(&enc, ivs[2], 0);
  5303. ret |= wc_Chacha_SetIV(&dec, ivs[2], 0);
  5304. if (ret != 0)
  5305. return ret;
  5306. ret |= wc_Chacha_Process(&enc, cipher_big, plain_big , block_size);
  5307. ret |= wc_Chacha_Process(&dec, plain_big , cipher_big, block_size);
  5308. if (ret != 0)
  5309. return ret;
  5310. if (XMEMCMP(plain_big, input_big, block_size))
  5311. return -4740-i*2;
  5312. if (XMEMCMP(cipher_big, cipher_big_result, block_size))
  5313. return -4741-i*2;
  5314. }
  5315. /* Streaming test */
  5316. for (i = 1; i <= (int)CHACHA_CHUNK_BYTES + 1; i++) {
  5317. int j, rem;
  5318. ret = wc_Chacha_SetKey(&enc, keys[0], keySz);
  5319. if (ret != 0)
  5320. return -4725;
  5321. ret = wc_Chacha_SetKey(&dec, keys[0], keySz);
  5322. if (ret != 0)
  5323. return -4726;
  5324. ret = wc_Chacha_SetIV(&enc, ivs[2], 0);
  5325. if (ret != 0)
  5326. return -4727;
  5327. ret = wc_Chacha_SetIV(&dec, ivs[2], 0);
  5328. if (ret != 0)
  5329. return -4728;
  5330. for (j = 0; j < CHACHA_BIG_TEST_SIZE - i; j+= i) {
  5331. ret = wc_Chacha_Process(&enc, cipher_big + j, plain_big + j, i);
  5332. if (ret != 0)
  5333. return -4729;
  5334. ret = wc_Chacha_Process(&dec, plain_big + j, cipher_big + j, i);
  5335. if (ret != 0)
  5336. return -4730;
  5337. }
  5338. rem = CHACHA_BIG_TEST_SIZE - j;
  5339. ret = wc_Chacha_Process(&enc, cipher_big + j, plain_big + j, rem);
  5340. if (ret != 0)
  5341. return -4731;
  5342. ret = wc_Chacha_Process(&dec, plain_big + j, cipher_big + j, rem);
  5343. if (ret != 0)
  5344. return -4732;
  5345. if (XMEMCMP(plain_big, input_big, CHACHA_BIG_TEST_SIZE))
  5346. return -4733;
  5347. if (XMEMCMP(cipher_big, cipher_big_result, CHACHA_BIG_TEST_SIZE))
  5348. return -4734;
  5349. }
  5350. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  5351. XFREE(cipher_big, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  5352. XFREE(plain_big, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  5353. XFREE(input_big, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  5354. #endif /* WOLFSSL_SMALL_STACK && !WOLFSSL_NO_MALLOC */
  5355. #endif /* BENCH_EMBEDDED */
  5356. return 0;
  5357. }
  5358. #endif /* HAVE_CHACHA */
  5359. #ifdef HAVE_POLY1305
  5360. WOLFSSL_TEST_SUBROUTINE int poly1305_test(void)
  5361. {
  5362. int ret = 0;
  5363. int i;
  5364. byte tag[16];
  5365. Poly1305 enc;
  5366. WOLFSSL_SMALL_STACK_STATIC const byte msg1[] =
  5367. {
  5368. 0x43,0x72,0x79,0x70,0x74,0x6f,0x67,0x72,
  5369. 0x61,0x70,0x68,0x69,0x63,0x20,0x46,0x6f,
  5370. 0x72,0x75,0x6d,0x20,0x52,0x65,0x73,0x65,
  5371. 0x61,0x72,0x63,0x68,0x20,0x47,0x72,0x6f,
  5372. 0x75,0x70
  5373. };
  5374. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] =
  5375. {
  5376. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x77,0x6f,0x72,
  5377. 0x6c,0x64,0x21
  5378. };
  5379. WOLFSSL_SMALL_STACK_STATIC const byte msg3[] =
  5380. {
  5381. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5382. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5383. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5384. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  5385. };
  5386. WOLFSSL_SMALL_STACK_STATIC const byte msg4[] =
  5387. {
  5388. 0xd3,0x1a,0x8d,0x34,0x64,0x8e,0x60,0xdb,
  5389. 0x7b,0x86,0xaf,0xbc,0x53,0xef,0x7e,0xc2,
  5390. 0xa4,0xad,0xed,0x51,0x29,0x6e,0x08,0xfe,
  5391. 0xa9,0xe2,0xb5,0xa7,0x36,0xee,0x62,0xd6,
  5392. 0x3d,0xbe,0xa4,0x5e,0x8c,0xa9,0x67,0x12,
  5393. 0x82,0xfa,0xfb,0x69,0xda,0x92,0x72,0x8b,
  5394. 0x1a,0x71,0xde,0x0a,0x9e,0x06,0x0b,0x29,
  5395. 0x05,0xd6,0xa5,0xb6,0x7e,0xcd,0x3b,0x36,
  5396. 0x92,0xdd,0xbd,0x7f,0x2d,0x77,0x8b,0x8c,
  5397. 0x98,0x03,0xae,0xe3,0x28,0x09,0x1b,0x58,
  5398. 0xfa,0xb3,0x24,0xe4,0xfa,0xd6,0x75,0x94,
  5399. 0x55,0x85,0x80,0x8b,0x48,0x31,0xd7,0xbc,
  5400. 0x3f,0xf4,0xde,0xf0,0x8e,0x4b,0x7a,0x9d,
  5401. 0xe5,0x76,0xd2,0x65,0x86,0xce,0xc6,0x4b,
  5402. 0x61,0x16
  5403. };
  5404. WOLFSSL_SMALL_STACK_STATIC const byte msg5[] =
  5405. {
  5406. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  5407. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  5408. };
  5409. WOLFSSL_SMALL_STACK_STATIC const byte msg6[] =
  5410. {
  5411. 0xd3,0x1a,0x8d,0x34,0x64,0x8e,0x60,0xdb,
  5412. 0x7b,0x86,0xaf,0xbc,0x53,0xef,0x7e,0xc2,
  5413. 0xa4,0xad,0xed,0x51,0x29,0x6e,0x08,0xfe,
  5414. 0xa9,0xe2,0xb5,0xa7,0x36,0xee,0x62,0xd6,
  5415. 0x3d,0xbe,0xa4,0x5e,0x8c,0xa9,0x67,0x12,
  5416. 0x82,0xfa,0xfb,0x69,0xda,0x92,0x72,0x8b,
  5417. 0xfa,0xb3,0x24,0xe4,0xfa,0xd6,0x75,0x94,
  5418. 0x1a,0x71,0xde,0x0a,0x9e,0x06,0x0b,0x29,
  5419. 0xa9,0xe2,0xb5,0xa7,0x36,0xee,0x62,0xd6,
  5420. 0x3d,0xbe,0xa4,0x5e,0x8c,0xa9,0x67,0x12,
  5421. 0xfa,0xb3,0x24,0xe4,0xfa,0xd6,0x75,0x94,
  5422. 0x05,0xd6,0xa5,0xb6,0x7e,0xcd,0x3b,0x36,
  5423. 0x92,0xdd,0xbd,0x7f,0x2d,0x77,0x8b,0x8c,
  5424. 0x7b,0x86,0xaf,0xbc,0x53,0xef,0x7e,0xc2,
  5425. 0x98,0x03,0xae,0xe3,0x28,0x09,0x1b,0x58,
  5426. 0xfa,0xb3,0x24,0xe4,0xfa,0xd6,0x75,0x94,
  5427. 0x55,0x85,0x80,0x8b,0x48,0x31,0xd7,0xbc,
  5428. 0x3f,0xf4,0xde,0xf0,0x8e,0x4b,0x7a,0x9d,
  5429. 0xe5,0x76,0xd2,0x65,0x86,0xce,0xc6,0x4b,
  5430. 0x61,0x16
  5431. };
  5432. byte additional[] =
  5433. {
  5434. 0x50,0x51,0x52,0x53,0xc0,0xc1,0xc2,0xc3,
  5435. 0xc4,0xc5,0xc6,0xc7
  5436. };
  5437. WOLFSSL_SMALL_STACK_STATIC const byte correct0[] =
  5438. {
  5439. 0x01,0x03,0x80,0x8a,0xfb,0x0d,0xb2,0xfd,
  5440. 0x4a,0xbf,0xf6,0xaf,0x41,0x49,0xf5,0x1b
  5441. };
  5442. WOLFSSL_SMALL_STACK_STATIC const byte correct1[] =
  5443. {
  5444. 0xa8,0x06,0x1d,0xc1,0x30,0x51,0x36,0xc6,
  5445. 0xc2,0x2b,0x8b,0xaf,0x0c,0x01,0x27,0xa9
  5446. };
  5447. WOLFSSL_SMALL_STACK_STATIC const byte correct2[] =
  5448. {
  5449. 0xa6,0xf7,0x45,0x00,0x8f,0x81,0xc9,0x16,
  5450. 0xa2,0x0d,0xcc,0x74,0xee,0xf2,0xb2,0xf0
  5451. };
  5452. WOLFSSL_SMALL_STACK_STATIC const byte correct3[] =
  5453. {
  5454. 0x49,0xec,0x78,0x09,0x0e,0x48,0x1e,0xc6,
  5455. 0xc2,0x6b,0x33,0xb9,0x1c,0xcc,0x03,0x07
  5456. };
  5457. WOLFSSL_SMALL_STACK_STATIC const byte correct4[] =
  5458. {
  5459. 0x1a,0xe1,0x0b,0x59,0x4f,0x09,0xe2,0x6a,
  5460. 0x7e,0x90,0x2e,0xcb,0xd0,0x60,0x06,0x91
  5461. };
  5462. WOLFSSL_SMALL_STACK_STATIC const byte correct5[] =
  5463. {
  5464. 0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5465. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5466. };
  5467. WOLFSSL_SMALL_STACK_STATIC const byte correct6[] =
  5468. {
  5469. 0xea,0x11,0x5c,0x4f,0xd0,0xc0,0x10,0xae,
  5470. 0xf7,0xdf,0xda,0x77,0xa2,0xe9,0xaf,0xca
  5471. };
  5472. WOLFSSL_SMALL_STACK_STATIC const byte key[] = {
  5473. 0x85,0xd6,0xbe,0x78,0x57,0x55,0x6d,0x33,
  5474. 0x7f,0x44,0x52,0xfe,0x42,0xd5,0x06,0xa8,
  5475. 0x01,0x03,0x80,0x8a,0xfb,0x0d,0xb2,0xfd,
  5476. 0x4a,0xbf,0xf6,0xaf,0x41,0x49,0xf5,0x1b
  5477. };
  5478. WOLFSSL_SMALL_STACK_STATIC const byte key2[] = {
  5479. 0x74,0x68,0x69,0x73,0x20,0x69,0x73,0x20,
  5480. 0x33,0x32,0x2d,0x62,0x79,0x74,0x65,0x20,
  5481. 0x6b,0x65,0x79,0x20,0x66,0x6f,0x72,0x20,
  5482. 0x50,0x6f,0x6c,0x79,0x31,0x33,0x30,0x35
  5483. };
  5484. WOLFSSL_SMALL_STACK_STATIC const byte key4[] = {
  5485. 0x7b,0xac,0x2b,0x25,0x2d,0xb4,0x47,0xaf,
  5486. 0x09,0xb6,0x7a,0x55,0xa4,0xe9,0x55,0x84,
  5487. 0x0a,0xe1,0xd6,0x73,0x10,0x75,0xd9,0xeb,
  5488. 0x2a,0x93,0x75,0x78,0x3e,0xd5,0x53,0xff
  5489. };
  5490. WOLFSSL_SMALL_STACK_STATIC const byte key5[] = {
  5491. 0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5492. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5493. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5494. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  5495. };
  5496. const byte* msgs[] = {NULL, msg1, msg2, msg3, msg5, msg6};
  5497. word32 szm[] = {0, sizeof(msg1), sizeof(msg2),
  5498. sizeof(msg3), sizeof(msg5), sizeof(msg6)};
  5499. const byte* keys[] = {key, key, key2, key2, key5, key};
  5500. const byte* tests[] = {correct0, correct1, correct2, correct3, correct5,
  5501. correct6};
  5502. for (i = 0; i < 6; i++) {
  5503. ret = wc_Poly1305SetKey(&enc, keys[i], 32);
  5504. if (ret != 0)
  5505. return -4800 - i;
  5506. ret = wc_Poly1305Update(&enc, msgs[i], szm[i]);
  5507. if (ret != 0)
  5508. return -4810 - i;
  5509. ret = wc_Poly1305Final(&enc, tag);
  5510. if (ret != 0)
  5511. return -4820 - i;
  5512. if (XMEMCMP(tag, tests[i], sizeof(tag)))
  5513. return -4830 - i;
  5514. }
  5515. /* Check TLS MAC function from 2.8.2 https://tools.ietf.org/html/rfc7539 */
  5516. XMEMSET(tag, 0, sizeof(tag));
  5517. ret = wc_Poly1305SetKey(&enc, key4, sizeof(key4));
  5518. if (ret != 0)
  5519. return -4840;
  5520. ret = wc_Poly1305_MAC(&enc, additional, sizeof(additional),
  5521. (byte*)msg4, sizeof(msg4), tag, sizeof(tag));
  5522. if (ret != 0)
  5523. return -4841;
  5524. if (XMEMCMP(tag, correct4, sizeof(tag)))
  5525. return -4842;
  5526. /* Check fail of TLS MAC function if altering additional data */
  5527. XMEMSET(tag, 0, sizeof(tag));
  5528. additional[0]++;
  5529. ret = wc_Poly1305_MAC(&enc, additional, sizeof(additional),
  5530. (byte*)msg4, sizeof(msg4), tag, sizeof(tag));
  5531. if (ret != 0)
  5532. return -4843;
  5533. if (XMEMCMP(tag, correct4, sizeof(tag)) == 0)
  5534. return -4844;
  5535. return 0;
  5536. }
  5537. #endif /* HAVE_POLY1305 */
  5538. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  5539. WOLFSSL_TEST_SUBROUTINE int chacha20_poly1305_aead_test(void)
  5540. {
  5541. /* Test #1 from Section 2.8.2 of draft-irtf-cfrg-chacha20-poly1305-10 */
  5542. /* https://tools.ietf.org/html/draft-irtf-cfrg-chacha20-poly1305-10 */
  5543. WOLFSSL_SMALL_STACK_STATIC const byte key1[] = {
  5544. 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
  5545. 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  5546. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
  5547. 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f
  5548. };
  5549. WOLFSSL_SMALL_STACK_STATIC const byte plaintext1[] = {
  5550. 0x4c, 0x61, 0x64, 0x69, 0x65, 0x73, 0x20, 0x61,
  5551. 0x6e, 0x64, 0x20, 0x47, 0x65, 0x6e, 0x74, 0x6c,
  5552. 0x65, 0x6d, 0x65, 0x6e, 0x20, 0x6f, 0x66, 0x20,
  5553. 0x74, 0x68, 0x65, 0x20, 0x63, 0x6c, 0x61, 0x73,
  5554. 0x73, 0x20, 0x6f, 0x66, 0x20, 0x27, 0x39, 0x39,
  5555. 0x3a, 0x20, 0x49, 0x66, 0x20, 0x49, 0x20, 0x63,
  5556. 0x6f, 0x75, 0x6c, 0x64, 0x20, 0x6f, 0x66, 0x66,
  5557. 0x65, 0x72, 0x20, 0x79, 0x6f, 0x75, 0x20, 0x6f,
  5558. 0x6e, 0x6c, 0x79, 0x20, 0x6f, 0x6e, 0x65, 0x20,
  5559. 0x74, 0x69, 0x70, 0x20, 0x66, 0x6f, 0x72, 0x20,
  5560. 0x74, 0x68, 0x65, 0x20, 0x66, 0x75, 0x74, 0x75,
  5561. 0x72, 0x65, 0x2c, 0x20, 0x73, 0x75, 0x6e, 0x73,
  5562. 0x63, 0x72, 0x65, 0x65, 0x6e, 0x20, 0x77, 0x6f,
  5563. 0x75, 0x6c, 0x64, 0x20, 0x62, 0x65, 0x20, 0x69,
  5564. 0x74, 0x2e
  5565. };
  5566. WOLFSSL_SMALL_STACK_STATIC const byte iv1[] = {
  5567. 0x07, 0x00, 0x00, 0x00, 0x40, 0x41, 0x42, 0x43,
  5568. 0x44, 0x45, 0x46, 0x47
  5569. };
  5570. WOLFSSL_SMALL_STACK_STATIC const byte aad1[] = { /* additional data */
  5571. 0x50, 0x51, 0x52, 0x53, 0xc0, 0xc1, 0xc2, 0xc3,
  5572. 0xc4, 0xc5, 0xc6, 0xc7
  5573. };
  5574. WOLFSSL_SMALL_STACK_STATIC const byte cipher1[] = { /* expected output from operation */
  5575. 0xd3, 0x1a, 0x8d, 0x34, 0x64, 0x8e, 0x60, 0xdb,
  5576. 0x7b, 0x86, 0xaf, 0xbc, 0x53, 0xef, 0x7e, 0xc2,
  5577. 0xa4, 0xad, 0xed, 0x51, 0x29, 0x6e, 0x08, 0xfe,
  5578. 0xa9, 0xe2, 0xb5, 0xa7, 0x36, 0xee, 0x62, 0xd6,
  5579. 0x3d, 0xbe, 0xa4, 0x5e, 0x8c, 0xa9, 0x67, 0x12,
  5580. 0x82, 0xfa, 0xfb, 0x69, 0xda, 0x92, 0x72, 0x8b,
  5581. 0x1a, 0x71, 0xde, 0x0a, 0x9e, 0x06, 0x0b, 0x29,
  5582. 0x05, 0xd6, 0xa5, 0xb6, 0x7e, 0xcd, 0x3b, 0x36,
  5583. 0x92, 0xdd, 0xbd, 0x7f, 0x2d, 0x77, 0x8b, 0x8c,
  5584. 0x98, 0x03, 0xae, 0xe3, 0x28, 0x09, 0x1b, 0x58,
  5585. 0xfa, 0xb3, 0x24, 0xe4, 0xfa, 0xd6, 0x75, 0x94,
  5586. 0x55, 0x85, 0x80, 0x8b, 0x48, 0x31, 0xd7, 0xbc,
  5587. 0x3f, 0xf4, 0xde, 0xf0, 0x8e, 0x4b, 0x7a, 0x9d,
  5588. 0xe5, 0x76, 0xd2, 0x65, 0x86, 0xce, 0xc6, 0x4b,
  5589. 0x61, 0x16
  5590. };
  5591. WOLFSSL_SMALL_STACK_STATIC const byte authTag1[] = { /* expected output from operation */
  5592. 0x1a, 0xe1, 0x0b, 0x59, 0x4f, 0x09, 0xe2, 0x6a,
  5593. 0x7e, 0x90, 0x2e, 0xcb, 0xd0, 0x60, 0x06, 0x91
  5594. };
  5595. /* Test #2 from Appendix A.2 in draft-irtf-cfrg-chacha20-poly1305-10 */
  5596. /* https://tools.ietf.org/html/draft-irtf-cfrg-chacha20-poly1305-10 */
  5597. WOLFSSL_SMALL_STACK_STATIC const byte key2[] = {
  5598. 0x1c, 0x92, 0x40, 0xa5, 0xeb, 0x55, 0xd3, 0x8a,
  5599. 0xf3, 0x33, 0x88, 0x86, 0x04, 0xf6, 0xb5, 0xf0,
  5600. 0x47, 0x39, 0x17, 0xc1, 0x40, 0x2b, 0x80, 0x09,
  5601. 0x9d, 0xca, 0x5c, 0xbc, 0x20, 0x70, 0x75, 0xc0
  5602. };
  5603. WOLFSSL_SMALL_STACK_STATIC const byte plaintext2[] = {
  5604. 0x49, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x65, 0x74,
  5605. 0x2d, 0x44, 0x72, 0x61, 0x66, 0x74, 0x73, 0x20,
  5606. 0x61, 0x72, 0x65, 0x20, 0x64, 0x72, 0x61, 0x66,
  5607. 0x74, 0x20, 0x64, 0x6f, 0x63, 0x75, 0x6d, 0x65,
  5608. 0x6e, 0x74, 0x73, 0x20, 0x76, 0x61, 0x6c, 0x69,
  5609. 0x64, 0x20, 0x66, 0x6f, 0x72, 0x20, 0x61, 0x20,
  5610. 0x6d, 0x61, 0x78, 0x69, 0x6d, 0x75, 0x6d, 0x20,
  5611. 0x6f, 0x66, 0x20, 0x73, 0x69, 0x78, 0x20, 0x6d,
  5612. 0x6f, 0x6e, 0x74, 0x68, 0x73, 0x20, 0x61, 0x6e,
  5613. 0x64, 0x20, 0x6d, 0x61, 0x79, 0x20, 0x62, 0x65,
  5614. 0x20, 0x75, 0x70, 0x64, 0x61, 0x74, 0x65, 0x64,
  5615. 0x2c, 0x20, 0x72, 0x65, 0x70, 0x6c, 0x61, 0x63,
  5616. 0x65, 0x64, 0x2c, 0x20, 0x6f, 0x72, 0x20, 0x6f,
  5617. 0x62, 0x73, 0x6f, 0x6c, 0x65, 0x74, 0x65, 0x64,
  5618. 0x20, 0x62, 0x79, 0x20, 0x6f, 0x74, 0x68, 0x65,
  5619. 0x72, 0x20, 0x64, 0x6f, 0x63, 0x75, 0x6d, 0x65,
  5620. 0x6e, 0x74, 0x73, 0x20, 0x61, 0x74, 0x20, 0x61,
  5621. 0x6e, 0x79, 0x20, 0x74, 0x69, 0x6d, 0x65, 0x2e,
  5622. 0x20, 0x49, 0x74, 0x20, 0x69, 0x73, 0x20, 0x69,
  5623. 0x6e, 0x61, 0x70, 0x70, 0x72, 0x6f, 0x70, 0x72,
  5624. 0x69, 0x61, 0x74, 0x65, 0x20, 0x74, 0x6f, 0x20,
  5625. 0x75, 0x73, 0x65, 0x20, 0x49, 0x6e, 0x74, 0x65,
  5626. 0x72, 0x6e, 0x65, 0x74, 0x2d, 0x44, 0x72, 0x61,
  5627. 0x66, 0x74, 0x73, 0x20, 0x61, 0x73, 0x20, 0x72,
  5628. 0x65, 0x66, 0x65, 0x72, 0x65, 0x6e, 0x63, 0x65,
  5629. 0x20, 0x6d, 0x61, 0x74, 0x65, 0x72, 0x69, 0x61,
  5630. 0x6c, 0x20, 0x6f, 0x72, 0x20, 0x74, 0x6f, 0x20,
  5631. 0x63, 0x69, 0x74, 0x65, 0x20, 0x74, 0x68, 0x65,
  5632. 0x6d, 0x20, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20,
  5633. 0x74, 0x68, 0x61, 0x6e, 0x20, 0x61, 0x73, 0x20,
  5634. 0x2f, 0xe2, 0x80, 0x9c, 0x77, 0x6f, 0x72, 0x6b,
  5635. 0x20, 0x69, 0x6e, 0x20, 0x70, 0x72, 0x6f, 0x67,
  5636. 0x72, 0x65, 0x73, 0x73, 0x2e, 0x2f, 0xe2, 0x80,
  5637. 0x9d
  5638. };
  5639. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] = {
  5640. 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04,
  5641. 0x05, 0x06, 0x07, 0x08
  5642. };
  5643. WOLFSSL_SMALL_STACK_STATIC const byte aad2[] = { /* additional data */
  5644. 0xf3, 0x33, 0x88, 0x86, 0x00, 0x00, 0x00, 0x00,
  5645. 0x00, 0x00, 0x4e, 0x91
  5646. };
  5647. WOLFSSL_SMALL_STACK_STATIC const byte cipher2[] = { /* expected output from operation */
  5648. 0x64, 0xa0, 0x86, 0x15, 0x75, 0x86, 0x1a, 0xf4,
  5649. 0x60, 0xf0, 0x62, 0xc7, 0x9b, 0xe6, 0x43, 0xbd,
  5650. 0x5e, 0x80, 0x5c, 0xfd, 0x34, 0x5c, 0xf3, 0x89,
  5651. 0xf1, 0x08, 0x67, 0x0a, 0xc7, 0x6c, 0x8c, 0xb2,
  5652. 0x4c, 0x6c, 0xfc, 0x18, 0x75, 0x5d, 0x43, 0xee,
  5653. 0xa0, 0x9e, 0xe9, 0x4e, 0x38, 0x2d, 0x26, 0xb0,
  5654. 0xbd, 0xb7, 0xb7, 0x3c, 0x32, 0x1b, 0x01, 0x00,
  5655. 0xd4, 0xf0, 0x3b, 0x7f, 0x35, 0x58, 0x94, 0xcf,
  5656. 0x33, 0x2f, 0x83, 0x0e, 0x71, 0x0b, 0x97, 0xce,
  5657. 0x98, 0xc8, 0xa8, 0x4a, 0xbd, 0x0b, 0x94, 0x81,
  5658. 0x14, 0xad, 0x17, 0x6e, 0x00, 0x8d, 0x33, 0xbd,
  5659. 0x60, 0xf9, 0x82, 0xb1, 0xff, 0x37, 0xc8, 0x55,
  5660. 0x97, 0x97, 0xa0, 0x6e, 0xf4, 0xf0, 0xef, 0x61,
  5661. 0xc1, 0x86, 0x32, 0x4e, 0x2b, 0x35, 0x06, 0x38,
  5662. 0x36, 0x06, 0x90, 0x7b, 0x6a, 0x7c, 0x02, 0xb0,
  5663. 0xf9, 0xf6, 0x15, 0x7b, 0x53, 0xc8, 0x67, 0xe4,
  5664. 0xb9, 0x16, 0x6c, 0x76, 0x7b, 0x80, 0x4d, 0x46,
  5665. 0xa5, 0x9b, 0x52, 0x16, 0xcd, 0xe7, 0xa4, 0xe9,
  5666. 0x90, 0x40, 0xc5, 0xa4, 0x04, 0x33, 0x22, 0x5e,
  5667. 0xe2, 0x82, 0xa1, 0xb0, 0xa0, 0x6c, 0x52, 0x3e,
  5668. 0xaf, 0x45, 0x34, 0xd7, 0xf8, 0x3f, 0xa1, 0x15,
  5669. 0x5b, 0x00, 0x47, 0x71, 0x8c, 0xbc, 0x54, 0x6a,
  5670. 0x0d, 0x07, 0x2b, 0x04, 0xb3, 0x56, 0x4e, 0xea,
  5671. 0x1b, 0x42, 0x22, 0x73, 0xf5, 0x48, 0x27, 0x1a,
  5672. 0x0b, 0xb2, 0x31, 0x60, 0x53, 0xfa, 0x76, 0x99,
  5673. 0x19, 0x55, 0xeb, 0xd6, 0x31, 0x59, 0x43, 0x4e,
  5674. 0xce, 0xbb, 0x4e, 0x46, 0x6d, 0xae, 0x5a, 0x10,
  5675. 0x73, 0xa6, 0x72, 0x76, 0x27, 0x09, 0x7a, 0x10,
  5676. 0x49, 0xe6, 0x17, 0xd9, 0x1d, 0x36, 0x10, 0x94,
  5677. 0xfa, 0x68, 0xf0, 0xff, 0x77, 0x98, 0x71, 0x30,
  5678. 0x30, 0x5b, 0xea, 0xba, 0x2e, 0xda, 0x04, 0xdf,
  5679. 0x99, 0x7b, 0x71, 0x4d, 0x6c, 0x6f, 0x2c, 0x29,
  5680. 0xa6, 0xad, 0x5c, 0xb4, 0x02, 0x2b, 0x02, 0x70,
  5681. 0x9b
  5682. };
  5683. WOLFSSL_SMALL_STACK_STATIC const byte authTag2[] = { /* expected output from operation */
  5684. 0xee, 0xad, 0x9d, 0x67, 0x89, 0x0c, 0xbb, 0x22,
  5685. 0x39, 0x23, 0x36, 0xfe, 0xa1, 0x85, 0x1f, 0x38
  5686. };
  5687. byte generatedCiphertext[265]; /* max plaintext2/cipher2 */
  5688. byte generatedPlaintext[265]; /* max plaintext2/cipher2 */
  5689. byte generatedAuthTag[CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE];
  5690. int err;
  5691. ChaChaPoly_Aead aead;
  5692. #if !defined(USE_INTEL_CHACHA_SPEEDUP) && !defined(WOLFSSL_ARMASM)
  5693. #define TEST_SMALL_CHACHA_CHUNKS 32
  5694. #else
  5695. #define TEST_SMALL_CHACHA_CHUNKS 64
  5696. #endif
  5697. #ifdef TEST_SMALL_CHACHA_CHUNKS
  5698. word32 testLen;
  5699. #endif
  5700. XMEMSET(generatedCiphertext, 0, sizeof(generatedCiphertext));
  5701. XMEMSET(generatedAuthTag, 0, sizeof(generatedAuthTag));
  5702. XMEMSET(generatedPlaintext, 0, sizeof(generatedPlaintext));
  5703. /* Parameter Validation testing */
  5704. /* Encrypt */
  5705. err = wc_ChaCha20Poly1305_Encrypt(NULL, iv1, aad1, sizeof(aad1), plaintext1,
  5706. sizeof(plaintext1), generatedCiphertext, generatedAuthTag);
  5707. if (err != BAD_FUNC_ARG)
  5708. return -4900;
  5709. err = wc_ChaCha20Poly1305_Encrypt(key1, NULL, aad1, sizeof(aad1),
  5710. plaintext1, sizeof(plaintext1), generatedCiphertext,
  5711. generatedAuthTag);
  5712. if (err != BAD_FUNC_ARG)
  5713. return -4901;
  5714. err = wc_ChaCha20Poly1305_Encrypt(key1, iv1, aad1, sizeof(aad1), NULL,
  5715. sizeof(plaintext1), generatedCiphertext, generatedAuthTag);
  5716. if (err != BAD_FUNC_ARG)
  5717. return -4902;
  5718. err = wc_ChaCha20Poly1305_Encrypt(key1, iv1, aad1, sizeof(aad1), plaintext1,
  5719. sizeof(plaintext1), NULL, generatedAuthTag);
  5720. if (err != BAD_FUNC_ARG)
  5721. return -4903;
  5722. err = wc_ChaCha20Poly1305_Encrypt(key1, iv1, aad1, sizeof(aad1), plaintext1,
  5723. sizeof(plaintext1), generatedCiphertext, NULL);
  5724. if (err != BAD_FUNC_ARG)
  5725. return -4904;
  5726. err = wc_ChaCha20Poly1305_Encrypt(key1, iv1, aad1, sizeof(aad1), NULL,
  5727. sizeof(plaintext1), generatedCiphertext, generatedAuthTag);
  5728. if (err != BAD_FUNC_ARG)
  5729. return -4905;
  5730. /* Decrypt */
  5731. err = wc_ChaCha20Poly1305_Decrypt(NULL, iv2, aad2, sizeof(aad2), cipher2,
  5732. sizeof(cipher2), authTag2, generatedPlaintext);
  5733. if (err != BAD_FUNC_ARG)
  5734. return -4906;
  5735. err = wc_ChaCha20Poly1305_Decrypt(key2, NULL, aad2, sizeof(aad2), cipher2,
  5736. sizeof(cipher2), authTag2, generatedPlaintext);
  5737. if (err != BAD_FUNC_ARG)
  5738. return -4907;
  5739. err = wc_ChaCha20Poly1305_Decrypt(key2, iv2, aad2, sizeof(aad2), NULL,
  5740. sizeof(cipher2), authTag2, generatedPlaintext);
  5741. if (err != BAD_FUNC_ARG)
  5742. return -4908;
  5743. err = wc_ChaCha20Poly1305_Decrypt(key2, iv2, aad2, sizeof(aad2), cipher2,
  5744. sizeof(cipher2), NULL, generatedPlaintext);
  5745. if (err != BAD_FUNC_ARG)
  5746. return -4909;
  5747. err = wc_ChaCha20Poly1305_Decrypt(key2, iv2, aad2, sizeof(aad2), cipher2,
  5748. sizeof(cipher2), authTag2, NULL);
  5749. if (err != BAD_FUNC_ARG)
  5750. return -4910;
  5751. err = wc_ChaCha20Poly1305_Decrypt(key2, iv2, aad2, sizeof(aad2), NULL,
  5752. sizeof(cipher2), authTag2, generatedPlaintext);
  5753. if (err != BAD_FUNC_ARG)
  5754. return -4911;
  5755. /* Test #1 */
  5756. err = wc_ChaCha20Poly1305_Encrypt(key1, iv1,
  5757. aad1, sizeof(aad1),
  5758. plaintext1, sizeof(plaintext1),
  5759. generatedCiphertext, generatedAuthTag);
  5760. if (err) {
  5761. return err;
  5762. }
  5763. /* -- Check the ciphertext and authtag */
  5764. if (XMEMCMP(generatedCiphertext, cipher1, sizeof(cipher1))) {
  5765. return -4912;
  5766. }
  5767. if (XMEMCMP(generatedAuthTag, authTag1, sizeof(authTag1))) {
  5768. return -4913;
  5769. }
  5770. /* -- Verify decryption works */
  5771. err = wc_ChaCha20Poly1305_Decrypt(key1, iv1,
  5772. aad1, sizeof(aad1),
  5773. cipher1, sizeof(cipher1),
  5774. authTag1, generatedPlaintext);
  5775. if (err) {
  5776. return err;
  5777. }
  5778. if (XMEMCMP(generatedPlaintext, plaintext1, sizeof(plaintext1))) {
  5779. return -4914;
  5780. }
  5781. XMEMSET(generatedCiphertext, 0, sizeof(generatedCiphertext));
  5782. XMEMSET(generatedAuthTag, 0, sizeof(generatedAuthTag));
  5783. XMEMSET(generatedPlaintext, 0, sizeof(generatedPlaintext));
  5784. /* Test #2 */
  5785. err = wc_ChaCha20Poly1305_Encrypt(key2, iv2,
  5786. aad2, sizeof(aad2),
  5787. plaintext2, sizeof(plaintext2),
  5788. generatedCiphertext, generatedAuthTag);
  5789. if (err) {
  5790. return err;
  5791. }
  5792. /* -- Check the ciphertext and authtag */
  5793. if (XMEMCMP(generatedCiphertext, cipher2, sizeof(cipher2))) {
  5794. return -4915;
  5795. }
  5796. if (XMEMCMP(generatedAuthTag, authTag2, sizeof(authTag2))) {
  5797. return -4916;
  5798. }
  5799. /* -- Verify decryption works */
  5800. err = wc_ChaCha20Poly1305_Decrypt(key2, iv2,
  5801. aad2, sizeof(aad2),
  5802. cipher2, sizeof(cipher2),
  5803. authTag2, generatedPlaintext);
  5804. if (err) {
  5805. return err;
  5806. }
  5807. if (XMEMCMP(generatedPlaintext, plaintext2, sizeof(plaintext2))) {
  5808. return -4917;
  5809. }
  5810. /* AEAD init/update/final - bad argument tests */
  5811. err = wc_ChaCha20Poly1305_Init(NULL, key1, iv1,
  5812. CHACHA20_POLY1305_AEAD_DECRYPT);
  5813. if (err != BAD_FUNC_ARG)
  5814. return -4918;
  5815. err = wc_ChaCha20Poly1305_Init(&aead, NULL, iv1,
  5816. CHACHA20_POLY1305_AEAD_DECRYPT);
  5817. if (err != BAD_FUNC_ARG)
  5818. return -4919;
  5819. err = wc_ChaCha20Poly1305_Init(&aead, key1, NULL,
  5820. CHACHA20_POLY1305_AEAD_DECRYPT);
  5821. if (err != BAD_FUNC_ARG)
  5822. return -4920;
  5823. err = wc_ChaCha20Poly1305_UpdateAad(NULL, aad1, sizeof(aad1));
  5824. if (err != BAD_FUNC_ARG)
  5825. return -4921;
  5826. err = wc_ChaCha20Poly1305_UpdateAad(&aead, NULL, sizeof(aad1));
  5827. if (err != BAD_FUNC_ARG)
  5828. return -4922;
  5829. err = wc_ChaCha20Poly1305_UpdateData(NULL, generatedPlaintext,
  5830. generatedPlaintext, sizeof(plaintext1));
  5831. if (err != BAD_FUNC_ARG)
  5832. return -4923;
  5833. err = wc_ChaCha20Poly1305_UpdateData(&aead, generatedPlaintext, NULL,
  5834. sizeof(plaintext1));
  5835. if (err != BAD_FUNC_ARG)
  5836. return -4924;
  5837. err = wc_ChaCha20Poly1305_UpdateData(&aead, NULL, generatedPlaintext,
  5838. sizeof(plaintext1));
  5839. if (err != BAD_FUNC_ARG)
  5840. return -4925;
  5841. err = wc_ChaCha20Poly1305_Final(NULL, generatedAuthTag);
  5842. if (err != BAD_FUNC_ARG)
  5843. return -4926;
  5844. err = wc_ChaCha20Poly1305_Final(&aead, NULL);
  5845. if (err != BAD_FUNC_ARG)
  5846. return -4927;
  5847. /* AEAD init/update/final - bad state tests */
  5848. /* clear struct - make valgrind happy to resolve
  5849. "Conditional jump or move depends on uninitialised value(s)".
  5850. The enum is "int" size and aead.state is "byte" */
  5851. /* The wc_ChaCha20Poly1305_Init function does this normally */
  5852. XMEMSET(&aead, 0, sizeof(aead));
  5853. aead.state = CHACHA20_POLY1305_STATE_INIT;
  5854. err = wc_ChaCha20Poly1305_UpdateAad(&aead, aad1, sizeof(aad1));
  5855. if (err != BAD_STATE_E)
  5856. return -4928;
  5857. aead.state = CHACHA20_POLY1305_STATE_DATA;
  5858. err = wc_ChaCha20Poly1305_UpdateAad(&aead, aad1, sizeof(aad1));
  5859. if (err != BAD_STATE_E)
  5860. return -4929;
  5861. aead.state = CHACHA20_POLY1305_STATE_INIT;
  5862. err = wc_ChaCha20Poly1305_UpdateData(&aead, generatedPlaintext,
  5863. generatedPlaintext, sizeof(plaintext1));
  5864. if (err != BAD_STATE_E)
  5865. return -4930;
  5866. aead.state = CHACHA20_POLY1305_STATE_INIT;
  5867. err = wc_ChaCha20Poly1305_Final(&aead, generatedAuthTag);
  5868. if (err != BAD_STATE_E)
  5869. return -4931;
  5870. aead.state = CHACHA20_POLY1305_STATE_READY;
  5871. err = wc_ChaCha20Poly1305_Final(&aead, generatedAuthTag);
  5872. if (err != BAD_STATE_E)
  5873. return -4932;
  5874. XMEMSET(generatedCiphertext, 0, sizeof(generatedCiphertext));
  5875. XMEMSET(generatedAuthTag, 0, sizeof(generatedAuthTag));
  5876. XMEMSET(generatedPlaintext, 0, sizeof(generatedPlaintext));
  5877. /* Test 1 - Encrypt */
  5878. err = wc_ChaCha20Poly1305_Init(&aead, key1, iv1,
  5879. CHACHA20_POLY1305_AEAD_ENCRYPT);
  5880. if (err != 0)
  5881. return -4933;
  5882. err = wc_ChaCha20Poly1305_UpdateAad(&aead, aad1, sizeof(aad1));
  5883. if (err != 0)
  5884. return -4934;
  5885. #ifdef TEST_SMALL_CHACHA_CHUNKS
  5886. /* test doing data in smaller chunks */
  5887. for (testLen=0; testLen<sizeof(plaintext1); ) {
  5888. word32 dataLen = sizeof(plaintext1) - testLen;
  5889. if (dataLen > TEST_SMALL_CHACHA_CHUNKS)
  5890. dataLen = TEST_SMALL_CHACHA_CHUNKS;
  5891. err = wc_ChaCha20Poly1305_UpdateData(&aead, &plaintext1[testLen],
  5892. &generatedCiphertext[testLen], dataLen);
  5893. if (err != 0)
  5894. return -4935;
  5895. testLen += dataLen;
  5896. }
  5897. #else
  5898. err = wc_ChaCha20Poly1305_UpdateData(&aead, plaintext1,
  5899. generatedCiphertext, sizeof(plaintext1));
  5900. #endif
  5901. err = wc_ChaCha20Poly1305_Final(&aead, generatedAuthTag);
  5902. if (err != 0)
  5903. return -4936;
  5904. err = wc_ChaCha20Poly1305_CheckTag(generatedAuthTag, authTag1);
  5905. if (err != 0)
  5906. return -4937;
  5907. if (XMEMCMP(generatedCiphertext, cipher1, sizeof(cipher1))) {
  5908. return -4938;
  5909. }
  5910. /* Test 1 - Decrypt */
  5911. err = wc_ChaCha20Poly1305_Init(&aead, key1, iv1,
  5912. CHACHA20_POLY1305_AEAD_DECRYPT);
  5913. if (err != 0)
  5914. return -4939;
  5915. err = wc_ChaCha20Poly1305_UpdateAad(&aead, aad1, sizeof(aad1));
  5916. if (err != 0)
  5917. return -4940;
  5918. #ifdef TEST_SMALL_CHACHA_CHUNKS
  5919. /* test doing data in smaller chunks */
  5920. for (testLen=0; testLen<sizeof(plaintext1); ) {
  5921. word32 dataLen = sizeof(plaintext1) - testLen;
  5922. if (dataLen > TEST_SMALL_CHACHA_CHUNKS)
  5923. dataLen = TEST_SMALL_CHACHA_CHUNKS;
  5924. err = wc_ChaCha20Poly1305_UpdateData(&aead,
  5925. &generatedCiphertext[testLen], &generatedPlaintext[testLen],
  5926. dataLen);
  5927. if (err != 0)
  5928. return -4941;
  5929. testLen += dataLen;
  5930. }
  5931. #else
  5932. err = wc_ChaCha20Poly1305_UpdateData(&aead, generatedCiphertext,
  5933. generatedPlaintext, sizeof(cipher1));
  5934. #endif
  5935. err = wc_ChaCha20Poly1305_Final(&aead, generatedAuthTag);
  5936. if (err != 0)
  5937. return -4942;
  5938. err = wc_ChaCha20Poly1305_CheckTag(generatedAuthTag, authTag1);
  5939. if (err != 0)
  5940. return -4943;
  5941. if (XMEMCMP(generatedPlaintext, plaintext1, sizeof(plaintext1))) {
  5942. return -4944;
  5943. }
  5944. XMEMSET(generatedCiphertext, 0, sizeof(generatedCiphertext));
  5945. XMEMSET(generatedAuthTag, 0, sizeof(generatedAuthTag));
  5946. XMEMSET(generatedPlaintext, 0, sizeof(generatedPlaintext));
  5947. /* Test 2 - Encrypt */
  5948. err = wc_ChaCha20Poly1305_Init(&aead, key2, iv2,
  5949. CHACHA20_POLY1305_AEAD_ENCRYPT);
  5950. if (err != 0)
  5951. return -4945;
  5952. err = wc_ChaCha20Poly1305_UpdateAad(&aead, aad2, sizeof(aad2));
  5953. if (err != 0)
  5954. return -4946;
  5955. #ifdef TEST_SMALL_CHACHA_CHUNKS
  5956. /* test doing data in smaller chunks */
  5957. for (testLen=0; testLen<sizeof(plaintext2); ) {
  5958. word32 dataLen = sizeof(plaintext2) - testLen;
  5959. if (dataLen > TEST_SMALL_CHACHA_CHUNKS)
  5960. dataLen = TEST_SMALL_CHACHA_CHUNKS;
  5961. err = wc_ChaCha20Poly1305_UpdateData(&aead, &plaintext2[testLen],
  5962. &generatedCiphertext[testLen], dataLen);
  5963. if (err != 0)
  5964. return -4947;
  5965. testLen += dataLen;
  5966. }
  5967. #else
  5968. err = wc_ChaCha20Poly1305_UpdateData(&aead, plaintext2, generatedCiphertext,
  5969. sizeof(plaintext2));
  5970. #endif
  5971. err = wc_ChaCha20Poly1305_Final(&aead, generatedAuthTag);
  5972. if (err != 0)
  5973. return -4948;
  5974. err = wc_ChaCha20Poly1305_CheckTag(generatedAuthTag, authTag2);
  5975. if (err != 0)
  5976. return -4949;
  5977. if (XMEMCMP(generatedCiphertext, cipher2, sizeof(cipher2))) {
  5978. return -4950;
  5979. }
  5980. /* Test 2 - Decrypt */
  5981. err = wc_ChaCha20Poly1305_Init(&aead, key2, iv2,
  5982. CHACHA20_POLY1305_AEAD_DECRYPT);
  5983. if (err != 0)
  5984. return -4951;
  5985. err = wc_ChaCha20Poly1305_UpdateAad(&aead, aad2, sizeof(aad2));
  5986. if (err != 0)
  5987. return -4952;
  5988. #ifdef TEST_SMALL_CHACHA_CHUNKS
  5989. /* test doing data in smaller chunks */
  5990. for (testLen=0; testLen<sizeof(plaintext2); ) {
  5991. word32 dataLen = sizeof(plaintext2) - testLen;
  5992. if (dataLen > TEST_SMALL_CHACHA_CHUNKS)
  5993. dataLen = TEST_SMALL_CHACHA_CHUNKS;
  5994. err = wc_ChaCha20Poly1305_UpdateData(&aead,
  5995. &generatedCiphertext[testLen], &generatedPlaintext[testLen],
  5996. dataLen);
  5997. if (err != 0)
  5998. return -4953;
  5999. testLen += dataLen;
  6000. }
  6001. #else
  6002. err = wc_ChaCha20Poly1305_UpdateData(&aead, generatedCiphertext,
  6003. generatedPlaintext, sizeof(cipher2));
  6004. #endif
  6005. err = wc_ChaCha20Poly1305_Final(&aead, generatedAuthTag);
  6006. if (err != 0)
  6007. return -4954;
  6008. err = wc_ChaCha20Poly1305_CheckTag(generatedAuthTag, authTag2);
  6009. if (err != 0)
  6010. return -4955;
  6011. if (XMEMCMP(generatedPlaintext, plaintext2, sizeof(plaintext2))) {
  6012. return -4956;
  6013. }
  6014. return err;
  6015. }
  6016. #endif /* HAVE_CHACHA && HAVE_POLY1305 */
  6017. #ifndef NO_DES3
  6018. WOLFSSL_TEST_SUBROUTINE int des_test(void)
  6019. {
  6020. WOLFSSL_SMALL_STACK_STATIC const byte vector[] = { /* "now is the time for all " w/o trailing 0 */
  6021. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  6022. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  6023. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  6024. };
  6025. byte plain[24];
  6026. byte cipher[24];
  6027. Des enc;
  6028. Des dec;
  6029. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  6030. {
  6031. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
  6032. };
  6033. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  6034. {
  6035. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef
  6036. };
  6037. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  6038. {
  6039. 0x8b,0x7c,0x52,0xb0,0x01,0x2b,0x6c,0xb8,
  6040. 0x4f,0x0f,0xeb,0xf3,0xfb,0x5f,0x86,0x73,
  6041. 0x15,0x85,0xb3,0x22,0x4b,0x86,0x2b,0x4b
  6042. };
  6043. int ret;
  6044. ret = wc_Des_SetKey(&enc, key, iv, DES_ENCRYPTION);
  6045. if (ret != 0)
  6046. return -5000;
  6047. ret = wc_Des_CbcEncrypt(&enc, cipher, vector, sizeof(vector));
  6048. if (ret != 0)
  6049. return -5001;
  6050. ret = wc_Des_SetKey(&dec, key, iv, DES_DECRYPTION);
  6051. if (ret != 0)
  6052. return -5002;
  6053. ret = wc_Des_CbcDecrypt(&dec, plain, cipher, sizeof(cipher));
  6054. if (ret != 0)
  6055. return -5003;
  6056. if (XMEMCMP(plain, vector, sizeof(plain)))
  6057. return -5004;
  6058. if (XMEMCMP(cipher, verify, sizeof(cipher)))
  6059. return -5005;
  6060. ret = wc_Des_CbcEncryptWithKey(cipher, vector, sizeof(vector), key, iv);
  6061. if (ret != 0)
  6062. return -5006;
  6063. #if defined(WOLFSSL_ENCRYPTED_KEYS) && !defined(NO_SHA)
  6064. {
  6065. EncryptedInfo info;
  6066. XMEMSET(&info, 0, sizeof(EncryptedInfo));
  6067. XMEMCPY(info.iv, iv, sizeof(iv));
  6068. info.ivSz = sizeof(iv);
  6069. info.keySz = sizeof(key);
  6070. info.cipherType = WC_CIPHER_DES;
  6071. ret = wc_BufferKeyEncrypt(&info, cipher, sizeof(cipher), key,
  6072. sizeof(key), WC_HASH_TYPE_SHA);
  6073. if (ret != 0)
  6074. return -5007;
  6075. /* Test invalid info ptr */
  6076. ret = wc_BufferKeyEncrypt(NULL, cipher, sizeof(cipher), key,
  6077. sizeof(key), WC_HASH_TYPE_SHA);
  6078. if (ret != BAD_FUNC_ARG)
  6079. return -5008;
  6080. #ifndef NO_PWDBASED
  6081. /* Test invalid hash type - only applies to wc_PBKDF1 call */
  6082. ret = wc_BufferKeyEncrypt(&info, cipher, sizeof(cipher), key,
  6083. sizeof(key), WC_HASH_TYPE_NONE);
  6084. if (ret == 0)
  6085. return -5009;
  6086. #endif /* !NO_PWDBASED */
  6087. }
  6088. #endif
  6089. return 0;
  6090. }
  6091. #endif /* !NO_DES3 */
  6092. #ifndef NO_DES3
  6093. WOLFSSL_TEST_SUBROUTINE int des3_test(void)
  6094. {
  6095. WOLFSSL_SMALL_STACK_STATIC const byte vector[] = { /* "Now is the time for all " w/o trailing 0 */
  6096. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  6097. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  6098. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  6099. };
  6100. byte plain[24];
  6101. byte cipher[24];
  6102. Des3 enc;
  6103. Des3 dec;
  6104. WOLFSSL_SMALL_STACK_STATIC const byte key3[] =
  6105. {
  6106. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  6107. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  6108. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  6109. };
  6110. WOLFSSL_SMALL_STACK_STATIC const byte iv3[] =
  6111. {
  6112. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  6113. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  6114. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  6115. };
  6116. WOLFSSL_SMALL_STACK_STATIC const byte verify3[] =
  6117. {
  6118. 0x43,0xa0,0x29,0x7e,0xd1,0x84,0xf8,0x0e,
  6119. 0x89,0x64,0x84,0x32,0x12,0xd5,0x08,0x98,
  6120. 0x18,0x94,0x15,0x74,0x87,0x12,0x7d,0xb0
  6121. };
  6122. int ret;
  6123. if (wc_Des3Init(&enc, HEAP_HINT, devId) != 0)
  6124. return -5100;
  6125. if (wc_Des3Init(&dec, HEAP_HINT, devId) != 0)
  6126. return -5101;
  6127. ret = wc_Des3_SetKey(&enc, key3, iv3, DES_ENCRYPTION);
  6128. if (ret != 0)
  6129. return -5102;
  6130. ret = wc_Des3_SetKey(&dec, key3, iv3, DES_DECRYPTION);
  6131. if (ret != 0)
  6132. return -5103;
  6133. ret = wc_Des3_CbcEncrypt(&enc, cipher, vector, sizeof(vector));
  6134. #if defined(WOLFSSL_ASYNC_CRYPT)
  6135. ret = wc_AsyncWait(ret, &enc.asyncDev, WC_ASYNC_FLAG_NONE);
  6136. #endif
  6137. if (ret != 0)
  6138. return -5104;
  6139. ret = wc_Des3_CbcDecrypt(&dec, plain, cipher, sizeof(cipher));
  6140. #if defined(WOLFSSL_ASYNC_CRYPT)
  6141. ret = wc_AsyncWait(ret, &dec.asyncDev, WC_ASYNC_FLAG_NONE);
  6142. #endif
  6143. if (ret != 0)
  6144. return -5105;
  6145. if (XMEMCMP(plain, vector, sizeof(plain)))
  6146. return -5106;
  6147. if (XMEMCMP(cipher, verify3, sizeof(cipher)))
  6148. return -5107;
  6149. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  6150. /* test the same vectors with using compatibility layer */
  6151. {
  6152. DES_key_schedule ks1;
  6153. DES_key_schedule ks2;
  6154. DES_key_schedule ks3;
  6155. DES_cblock iv4;
  6156. XMEMCPY(ks1, key3, sizeof(DES_key_schedule));
  6157. XMEMCPY(ks2, key3 + 8, sizeof(DES_key_schedule));
  6158. XMEMCPY(ks3, key3 + 16, sizeof(DES_key_schedule));
  6159. XMEMCPY(iv4, iv3, sizeof(DES_cblock));
  6160. XMEMSET(plain, 0, sizeof(plain));
  6161. XMEMSET(cipher, 0, sizeof(cipher));
  6162. DES_ede3_cbc_encrypt(vector, cipher, sizeof(vector), &ks1, &ks2, &ks3,
  6163. &iv4, DES_ENCRYPT);
  6164. DES_ede3_cbc_encrypt(cipher, plain, sizeof(cipher), &ks1, &ks2, &ks3,
  6165. &iv4, DES_DECRYPT);
  6166. if (XMEMCMP(plain, vector, sizeof(plain)))
  6167. return -5108;
  6168. if (XMEMCMP(cipher, verify3, sizeof(cipher)))
  6169. return -5109;
  6170. }
  6171. #endif /* OPENSSL_EXTRA */
  6172. wc_Des3Free(&enc);
  6173. wc_Des3Free(&dec);
  6174. #if defined(WOLFSSL_ENCRYPTED_KEYS) && !defined(NO_SHA)
  6175. {
  6176. EncryptedInfo info;
  6177. XMEMSET(&info, 0, sizeof(EncryptedInfo));
  6178. XMEMCPY(info.iv, iv3, sizeof(iv3));
  6179. info.ivSz = sizeof(iv3);
  6180. info.keySz = sizeof(key3);
  6181. info.cipherType = WC_CIPHER_DES3;
  6182. ret = wc_BufferKeyEncrypt(&info, cipher, sizeof(cipher), key3,
  6183. sizeof(key3), WC_HASH_TYPE_SHA);
  6184. if (ret != 0)
  6185. return -5110;
  6186. }
  6187. #endif
  6188. return 0;
  6189. }
  6190. #endif /* NO_DES3 */
  6191. #ifndef NO_AES
  6192. #if defined(WOLFSSL_AES_OFB) || defined(WOLFSSL_AES_CFB) || \
  6193. defined(WOLFSSL_AES_XTS)
  6194. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  6195. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  6196. /* pass in the function, key, iv, plain text and expected and this function
  6197. * tests that the encryption and decryption is successful */
  6198. static int EVP_test(const WOLFSSL_EVP_CIPHER* type, const byte* key,
  6199. const byte* iv, const byte* plain, int plainSz,
  6200. const byte* expected, int expectedSz)
  6201. {
  6202. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  6203. EVP_CIPHER_CTX *ctx = NULL;
  6204. #else
  6205. EVP_CIPHER_CTX ctx[1];
  6206. #endif
  6207. int idx, ret = 0, cipherSz;
  6208. byte* cipher;
  6209. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  6210. if ((ctx = wolfSSL_EVP_CIPHER_CTX_new()) == NULL)
  6211. return MEMORY_E;
  6212. #endif
  6213. cipher = (byte*)XMALLOC(plainSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  6214. if (cipher == NULL) {
  6215. ret = -5120;
  6216. goto EVP_TEST_END;
  6217. }
  6218. /* test encrypt */
  6219. EVP_CIPHER_CTX_init(ctx);
  6220. if (EVP_CipherInit(ctx, type, key, iv, 1) == 0) {
  6221. ret = -5121;
  6222. goto EVP_TEST_END;
  6223. }
  6224. if (EVP_CipherUpdate(ctx, cipher, &idx, plain, expectedSz) == 0) {
  6225. ret = -5122;
  6226. goto EVP_TEST_END;
  6227. }
  6228. cipherSz = idx;
  6229. if (EVP_CipherFinal(ctx, cipher + cipherSz, &idx) == 0) {
  6230. ret = -5123;
  6231. goto EVP_TEST_END;
  6232. }
  6233. cipherSz += idx;
  6234. if (XMEMCMP(cipher, expected, plainSz)) {
  6235. ret = -5124;
  6236. goto EVP_TEST_END;
  6237. }
  6238. /* test decrypt */
  6239. EVP_CIPHER_CTX_init(ctx);
  6240. if (EVP_CipherInit(ctx, type, key, iv, 0) == 0) {
  6241. ret = -5125;
  6242. goto EVP_TEST_END;
  6243. }
  6244. if (EVP_CipherUpdate(ctx, cipher, &idx, cipher, expectedSz) == 0) {
  6245. ret = -5126;
  6246. goto EVP_TEST_END;
  6247. }
  6248. cipherSz = idx;
  6249. if (EVP_CipherFinal(ctx, cipher + cipherSz, &idx) == 0) {
  6250. ret = -5127;
  6251. goto EVP_TEST_END;
  6252. }
  6253. cipherSz += idx;
  6254. if ((expectedSz != cipherSz) || XMEMCMP(plain, cipher, plainSz)) {
  6255. ret = -5128;
  6256. goto EVP_TEST_END;
  6257. }
  6258. EVP_TEST_END:
  6259. if (cipher)
  6260. XFREE(cipher, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  6261. (void)cipherSz;
  6262. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  6263. wolfSSL_EVP_CIPHER_CTX_free(ctx);
  6264. #endif
  6265. return ret;
  6266. }
  6267. #endif /* OPENSSL_EXTRA */
  6268. #endif /* WOLFSSL_AES_OFB || WOLFSSL_AES_CFB */
  6269. #ifdef WOLFSSL_AES_OFB
  6270. /* test vector from https://csrc.nist.gov/Projects/cryptographic-algorithm-validation-program/Block-Ciphers */
  6271. WOLFSSL_TEST_SUBROUTINE int aesofb_test(void)
  6272. {
  6273. #ifdef WOLFSSL_AES_256
  6274. WOLFSSL_SMALL_STACK_STATIC const byte key1[] =
  6275. {
  6276. 0xc4,0xc7,0xfa,0xd6,0x53,0x5c,0xb8,0x71,
  6277. 0x4a,0x5c,0x40,0x77,0x9a,0x8b,0xa1,0xd2,
  6278. 0x53,0x3e,0x23,0xb4,0xb2,0x58,0x73,0x2a,
  6279. 0x5b,0x78,0x01,0xf4,0xe3,0x71,0xa7,0x94
  6280. };
  6281. WOLFSSL_SMALL_STACK_STATIC const byte iv1[] =
  6282. {
  6283. 0x5e,0xb9,0x33,0x13,0xb8,0x71,0xff,0x16,
  6284. 0xb9,0x8a,0x9b,0xcb,0x43,0x33,0x0d,0x6f
  6285. };
  6286. WOLFSSL_SMALL_STACK_STATIC const byte plain1[] =
  6287. {
  6288. 0x6d,0x0b,0xb0,0x79,0x63,0x84,0x71,0xe9,
  6289. 0x39,0xd4,0x53,0x14,0x86,0xc1,0x4c,0x25,
  6290. 0x9a,0xee,0xc6,0xf3,0xc0,0x0d,0xfd,0xd6,
  6291. 0xc0,0x50,0xa8,0xba,0xa8,0x20,0xdb,0x71,
  6292. 0xcc,0x12,0x2c,0x4e,0x0c,0x17,0x15,0xef,
  6293. 0x55,0xf3,0x99,0x5a,0x6b,0xf0,0x2a,0x4c
  6294. };
  6295. WOLFSSL_SMALL_STACK_STATIC const byte cipher1[] =
  6296. {
  6297. 0x0f,0x54,0x61,0x71,0x59,0xd0,0x3f,0xfc,
  6298. 0x1b,0xfa,0xfb,0x60,0x29,0x30,0xd7,0x00,
  6299. 0xf4,0xa4,0xa8,0xe6,0xdd,0x93,0x94,0x46,
  6300. 0x64,0xd2,0x19,0xc4,0xc5,0x4d,0xde,0x1b,
  6301. 0x04,0x53,0xe1,0x73,0xf5,0x18,0x74,0xae,
  6302. 0xfd,0x64,0xa2,0xe1,0xe2,0x76,0x13,0xb0
  6303. };
  6304. #endif /* WOLFSSL_AES_256 */
  6305. #ifdef WOLFSSL_AES_128
  6306. WOLFSSL_SMALL_STACK_STATIC const byte key2[] =
  6307. {
  6308. 0x10,0xa5,0x88,0x69,0xd7,0x4b,0xe5,0xa3,
  6309. 0x74,0xcf,0x86,0x7c,0xfb,0x47,0x38,0x59
  6310. };
  6311. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] =
  6312. {
  6313. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6314. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  6315. };
  6316. WOLFSSL_SMALL_STACK_STATIC const byte plain2[] =
  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 cipher2[] =
  6322. {
  6323. 0x6d,0x25,0x1e,0x69,0x44,0xb0,0x51,0xe0,
  6324. 0x4e,0xaa,0x6f,0xb4,0xdb,0xf7,0x84,0x65
  6325. };
  6326. #endif /* WOLFSSL_AES_128 */
  6327. #ifdef WOLFSSL_AES_192
  6328. WOLFSSL_SMALL_STACK_STATIC const byte key3[] = {
  6329. 0xd0,0x77,0xa0,0x3b,0xd8,0xa3,0x89,0x73,
  6330. 0x92,0x8c,0xca,0xfe,0x4a,0x9d,0x2f,0x45,
  6331. 0x51,0x30,0xbd,0x0a,0xf5,0xae,0x46,0xa9
  6332. };
  6333. WOLFSSL_SMALL_STACK_STATIC const byte iv3[] =
  6334. {
  6335. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6336. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  6337. };
  6338. WOLFSSL_SMALL_STACK_STATIC const byte cipher3[] =
  6339. {
  6340. 0xab,0xc7,0x86,0xfb,0x1e,0xdb,0x50,0x45,
  6341. 0x80,0xc4,0xd8,0x82,0xef,0x29,0xa0,0xc7
  6342. };
  6343. WOLFSSL_SMALL_STACK_STATIC const byte plain3[] =
  6344. {
  6345. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6346. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  6347. };
  6348. #endif /* WOLFSSL_AES_192 */
  6349. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  6350. Aes *enc = NULL;
  6351. #else
  6352. Aes enc[1];
  6353. #endif
  6354. byte cipher[AES_BLOCK_SIZE * 4];
  6355. #ifdef HAVE_AES_DECRYPT
  6356. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  6357. Aes *dec = NULL;
  6358. #else
  6359. Aes dec[1];
  6360. #endif
  6361. byte plain [AES_BLOCK_SIZE * 4];
  6362. #endif
  6363. int ret = 0;
  6364. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  6365. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  6366. ERROR_OUT(-1, out);
  6367. #ifdef HAVE_AES_DECRYPT
  6368. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  6369. ERROR_OUT(-1, out);
  6370. #endif
  6371. #endif
  6372. XMEMSET(enc, 0, sizeof *enc);
  6373. #ifdef HAVE_AES_DECRYPT
  6374. XMEMSET(dec, 0, sizeof *dec);
  6375. #endif
  6376. #ifdef WOLFSSL_AES_128
  6377. /* 128 key size test */
  6378. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  6379. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  6380. ret = EVP_test(EVP_aes_128_ofb(), key2, iv2, plain2, sizeof(plain2),
  6381. cipher2, sizeof(cipher2));
  6382. if (ret != 0) {
  6383. goto out;
  6384. }
  6385. #endif
  6386. ret = wc_AesSetKey(enc, key2, sizeof(key2), iv2, AES_ENCRYPTION);
  6387. if (ret != 0)
  6388. ERROR_OUT(-5129, out);
  6389. #ifdef HAVE_AES_DECRYPT
  6390. /* decrypt uses AES_ENCRYPTION */
  6391. ret = wc_AesSetKey(dec, key2, sizeof(key2), iv2, AES_ENCRYPTION);
  6392. if (ret != 0)
  6393. ERROR_OUT(-5130, out);
  6394. #endif
  6395. XMEMSET(cipher, 0, sizeof(cipher));
  6396. ret = wc_AesOfbEncrypt(enc, cipher, plain2, AES_BLOCK_SIZE);
  6397. if (ret != 0)
  6398. ERROR_OUT(-5131, out);
  6399. if (XMEMCMP(cipher, cipher2, AES_BLOCK_SIZE))
  6400. ERROR_OUT(-5132, out);
  6401. #ifdef HAVE_AES_DECRYPT
  6402. ret = wc_AesOfbDecrypt(dec, plain, cipher2, AES_BLOCK_SIZE);
  6403. if (ret != 0)
  6404. ERROR_OUT(-5133, out);
  6405. if (XMEMCMP(plain, plain2, AES_BLOCK_SIZE))
  6406. ERROR_OUT(-5134, out);
  6407. #endif /* HAVE_AES_DECRYPT */
  6408. #endif /* WOLFSSL_AES_128 */
  6409. #ifdef WOLFSSL_AES_192
  6410. /* 192 key size test */
  6411. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  6412. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  6413. ret = EVP_test(EVP_aes_192_ofb(), key3, iv3, plain3, sizeof(plain3),
  6414. cipher3, sizeof(cipher3));
  6415. if (ret != 0) {
  6416. goto out;
  6417. }
  6418. #endif
  6419. ret = wc_AesSetKey(enc, key3, sizeof(key3), iv3, AES_ENCRYPTION);
  6420. if (ret != 0)
  6421. ERROR_OUT(-5135, out);
  6422. #ifdef HAVE_AES_DECRYPT
  6423. /* decrypt uses AES_ENCRYPTION */
  6424. ret = wc_AesSetKey(dec, key3, sizeof(key3), iv3, AES_ENCRYPTION);
  6425. if (ret != 0)
  6426. ERROR_OUT(-5136, out);
  6427. #endif
  6428. XMEMSET(cipher, 0, sizeof(cipher));
  6429. ret = wc_AesOfbEncrypt(enc, cipher, plain3, AES_BLOCK_SIZE);
  6430. if (ret != 0)
  6431. ERROR_OUT(-5137, out);
  6432. if (XMEMCMP(cipher, cipher3, AES_BLOCK_SIZE))
  6433. ERROR_OUT(-5138, out);
  6434. #ifdef HAVE_AES_DECRYPT
  6435. ret = wc_AesOfbDecrypt(dec, plain, cipher3, AES_BLOCK_SIZE);
  6436. if (ret != 0)
  6437. ERROR_OUT(-5139, out);
  6438. if (XMEMCMP(plain, plain3, AES_BLOCK_SIZE))
  6439. ERROR_OUT(-5140, out);
  6440. #endif /* HAVE_AES_DECRYPT */
  6441. #endif /* WOLFSSL_AES_192 */
  6442. #ifdef WOLFSSL_AES_256
  6443. /* 256 key size test */
  6444. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  6445. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  6446. ret = EVP_test(EVP_aes_256_ofb(), key1, iv1, plain1, sizeof(plain1),
  6447. cipher1, sizeof(cipher1));
  6448. if (ret != 0) {
  6449. goto out;
  6450. }
  6451. #endif
  6452. ret = wc_AesSetKey(enc, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  6453. if (ret != 0)
  6454. ERROR_OUT(-5141, out);
  6455. #ifdef HAVE_AES_DECRYPT
  6456. /* decrypt uses AES_ENCRYPTION */
  6457. ret = wc_AesSetKey(dec, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  6458. if (ret != 0)
  6459. ERROR_OUT(-5142, out);
  6460. #endif
  6461. XMEMSET(cipher, 0, sizeof(cipher));
  6462. ret = wc_AesOfbEncrypt(enc, cipher, plain1, AES_BLOCK_SIZE);
  6463. if (ret != 0)
  6464. ERROR_OUT(-5143, out);
  6465. if (XMEMCMP(cipher, cipher1, AES_BLOCK_SIZE))
  6466. ERROR_OUT(-5144, out);
  6467. ret = wc_AesOfbEncrypt(enc, cipher + AES_BLOCK_SIZE,
  6468. plain1 + AES_BLOCK_SIZE, AES_BLOCK_SIZE);
  6469. if (ret != 0)
  6470. ERROR_OUT(-5145, out);
  6471. if (XMEMCMP(cipher + AES_BLOCK_SIZE, cipher1 + AES_BLOCK_SIZE,
  6472. AES_BLOCK_SIZE))
  6473. ERROR_OUT(-5146, out);
  6474. #ifdef HAVE_AES_DECRYPT
  6475. ret = wc_AesOfbDecrypt(dec, plain, cipher1, AES_BLOCK_SIZE);
  6476. if (ret != 0)
  6477. ERROR_OUT(-5147, out);
  6478. if (XMEMCMP(plain, plain1, AES_BLOCK_SIZE))
  6479. ERROR_OUT(-5148, out);
  6480. ret = wc_AesOfbDecrypt(dec, plain + AES_BLOCK_SIZE,
  6481. cipher1 + AES_BLOCK_SIZE, AES_BLOCK_SIZE);
  6482. if (ret != 0)
  6483. ERROR_OUT(-5149, out);
  6484. if (XMEMCMP(plain + AES_BLOCK_SIZE, plain1 + AES_BLOCK_SIZE,
  6485. AES_BLOCK_SIZE))
  6486. ERROR_OUT(-5150, out);
  6487. #endif /* HAVE_AES_DECRYPT */
  6488. /* multiple blocks at once */
  6489. ret = wc_AesSetKey(enc, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  6490. if (ret != 0)
  6491. ERROR_OUT(-5151, out);
  6492. #ifdef HAVE_AES_DECRYPT
  6493. /* decrypt uses AES_ENCRYPTION */
  6494. ret = wc_AesSetKey(dec, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  6495. if (ret != 0)
  6496. ERROR_OUT(-5152, out);
  6497. #endif
  6498. XMEMSET(cipher, 0, sizeof(cipher));
  6499. ret = wc_AesOfbEncrypt(enc, cipher, plain1, AES_BLOCK_SIZE * 3);
  6500. if (ret != 0)
  6501. ERROR_OUT(-5153, out);
  6502. if (XMEMCMP(cipher, cipher1, AES_BLOCK_SIZE * 3))
  6503. ERROR_OUT(-5154, out);
  6504. #ifdef HAVE_AES_DECRYPT
  6505. ret = wc_AesOfbDecrypt(dec, plain, cipher1, AES_BLOCK_SIZE * 3);
  6506. if (ret != 0)
  6507. ERROR_OUT(-5155, out);
  6508. if (XMEMCMP(plain, plain1, AES_BLOCK_SIZE * 3))
  6509. ERROR_OUT(-5156, out);
  6510. #endif /* HAVE_AES_DECRYPT */
  6511. /* inline decrypt/encrypt*/
  6512. ret = wc_AesSetKey(enc, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  6513. if (ret != 0)
  6514. ERROR_OUT(-5157, out);
  6515. #ifdef HAVE_AES_DECRYPT
  6516. /* decrypt uses AES_ENCRYPTION */
  6517. ret = wc_AesSetKey(dec, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  6518. if (ret != 0)
  6519. ERROR_OUT(-5158, out);
  6520. #endif
  6521. XMEMCPY(cipher, plain1, AES_BLOCK_SIZE * 2);
  6522. ret = wc_AesOfbEncrypt(enc, cipher, cipher, AES_BLOCK_SIZE * 2);
  6523. if (ret != 0)
  6524. ERROR_OUT(-5159, out);
  6525. if (XMEMCMP(cipher, cipher1, AES_BLOCK_SIZE * 2))
  6526. ERROR_OUT(-5160, out);
  6527. #ifdef HAVE_AES_DECRYPT
  6528. ret = wc_AesOfbDecrypt(dec, cipher, cipher, AES_BLOCK_SIZE * 2);
  6529. if (ret != 0)
  6530. ERROR_OUT(-5161, out);
  6531. if (XMEMCMP(cipher, plain1, AES_BLOCK_SIZE * 2))
  6532. ERROR_OUT(-5162, out);
  6533. #endif /* HAVE_AES_DECRYPT */
  6534. /* 256 key size test leftover support */
  6535. ret = wc_AesSetKey(enc, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  6536. if (ret != 0)
  6537. ERROR_OUT(-5163, out);
  6538. #ifdef HAVE_AES_DECRYPT
  6539. /* decrypt uses AES_ENCRYPTION */
  6540. ret = wc_AesSetKey(dec, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  6541. if (ret != 0)
  6542. ERROR_OUT(-5164, out);
  6543. #endif
  6544. XMEMSET(cipher, 0, sizeof(cipher));
  6545. ret = wc_AesOfbEncrypt(enc, cipher, plain1, 3);
  6546. if (ret != 0)
  6547. ERROR_OUT(-5165, out);
  6548. if (XMEMCMP(cipher, cipher1, 3))
  6549. ERROR_OUT(-5166, out);
  6550. ret = wc_AesOfbEncrypt(enc, cipher + 3, plain1 + 3, AES_BLOCK_SIZE);
  6551. if (ret != 0)
  6552. ERROR_OUT(-5167, out);
  6553. if (XMEMCMP(cipher + 3, cipher1 + 3, AES_BLOCK_SIZE))
  6554. ERROR_OUT(-5168, out);
  6555. #ifdef HAVE_AES_DECRYPT
  6556. ret = wc_AesOfbDecrypt(dec, plain, cipher1, 6);
  6557. if (ret != 0)
  6558. ERROR_OUT(-5169, out);
  6559. if (XMEMCMP(plain, plain1, 6))
  6560. ERROR_OUT(-5170, out);
  6561. ret = wc_AesOfbDecrypt(dec, plain + 6, cipher1 + 6, AES_BLOCK_SIZE);
  6562. if (ret != 0)
  6563. ERROR_OUT(-5171, out);
  6564. if (XMEMCMP(plain + 6, plain1 + 6, AES_BLOCK_SIZE))
  6565. ERROR_OUT(-5172, out);
  6566. #endif /* HAVE_AES_DECRYPT */
  6567. out:
  6568. wc_AesFree(enc);
  6569. wc_AesFree(dec);
  6570. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  6571. if (enc)
  6572. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  6573. #ifdef HAVE_AES_DECRYPT
  6574. if (dec)
  6575. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  6576. #endif
  6577. #endif
  6578. #endif /* WOLFSSL_AES_256 */
  6579. return ret;
  6580. }
  6581. #endif /* WOLFSSL_AES_OFB */
  6582. #if defined(WOLFSSL_AES_CFB)
  6583. /* Test cases from NIST SP 800-38A, Recommendation for Block Cipher Modes of Operation Methods an*/
  6584. static int aescfb_test(void)
  6585. {
  6586. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  6587. Aes *enc = NULL;
  6588. #else
  6589. Aes enc[1];
  6590. #endif
  6591. int enc_inited = 0;
  6592. byte cipher[AES_BLOCK_SIZE * 4];
  6593. #ifdef HAVE_AES_DECRYPT
  6594. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  6595. Aes *dec = NULL;
  6596. #else
  6597. Aes dec[1];
  6598. #endif
  6599. int dec_inited = 0;
  6600. byte plain [AES_BLOCK_SIZE * 4];
  6601. #endif
  6602. int ret = 0;
  6603. WOLFSSL_SMALL_STACK_STATIC const byte iv[] = {
  6604. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  6605. 0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f
  6606. };
  6607. #ifdef WOLFSSL_AES_128
  6608. WOLFSSL_SMALL_STACK_STATIC const byte key1[] =
  6609. {
  6610. 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,
  6611. 0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c
  6612. };
  6613. WOLFSSL_SMALL_STACK_STATIC const byte cipher1[] =
  6614. {
  6615. 0x3b,0x3f,0xd9,0x2e,0xb7,0x2d,0xad,0x20,
  6616. 0x33,0x34,0x49,0xf8,0xe8,0x3c,0xfb,0x4a,
  6617. 0xc8,0xa6,0x45,0x37,0xa0,0xb3,0xa9,0x3f,
  6618. 0xcd,0xe3,0xcd,0xad,0x9f,0x1c,0xe5,0x8b,
  6619. 0x26,0x75,0x1f,0x67,0xa3,0xcb,0xb1,0x40,
  6620. 0xb1,0x80,0x8c,0xf1,0x87,0xa4,0xf4,0xdf
  6621. };
  6622. WOLFSSL_SMALL_STACK_STATIC const byte msg1[] =
  6623. {
  6624. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  6625. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  6626. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  6627. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  6628. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  6629. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef
  6630. };
  6631. #endif /* WOLFSSL_AES_128 */
  6632. #ifdef WOLFSSL_AES_192
  6633. /* 192 size key test */
  6634. WOLFSSL_SMALL_STACK_STATIC const byte key2[] =
  6635. {
  6636. 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  6637. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  6638. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b
  6639. };
  6640. WOLFSSL_SMALL_STACK_STATIC const byte cipher2[] =
  6641. {
  6642. 0xcd,0xc8,0x0d,0x6f,0xdd,0xf1,0x8c,0xab,
  6643. 0x34,0xc2,0x59,0x09,0xc9,0x9a,0x41,0x74,
  6644. 0x67,0xce,0x7f,0x7f,0x81,0x17,0x36,0x21,
  6645. 0x96,0x1a,0x2b,0x70,0x17,0x1d,0x3d,0x7a,
  6646. 0x2e,0x1e,0x8a,0x1d,0xd5,0x9b,0x88,0xb1,
  6647. 0xc8,0xe6,0x0f,0xed,0x1e,0xfa,0xc4,0xc9,
  6648. 0xc0,0x5f,0x9f,0x9c,0xa9,0x83,0x4f,0xa0,
  6649. 0x42,0xae,0x8f,0xba,0x58,0x4b,0x09,0xff
  6650. };
  6651. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] =
  6652. {
  6653. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  6654. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  6655. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  6656. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  6657. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  6658. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  6659. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  6660. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  6661. };
  6662. #endif /* WOLFSSL_AES_192 */
  6663. #ifdef WOLFSSL_AES_256
  6664. /* 256 size key simple test */
  6665. WOLFSSL_SMALL_STACK_STATIC const byte key3[] =
  6666. {
  6667. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  6668. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  6669. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  6670. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  6671. };
  6672. WOLFSSL_SMALL_STACK_STATIC const byte cipher3[] =
  6673. {
  6674. 0xdc,0x7e,0x84,0xbf,0xda,0x79,0x16,0x4b,
  6675. 0x7e,0xcd,0x84,0x86,0x98,0x5d,0x38,0x60,
  6676. 0x39,0xff,0xed,0x14,0x3b,0x28,0xb1,0xc8,
  6677. 0x32,0x11,0x3c,0x63,0x31,0xe5,0x40,0x7b,
  6678. 0xdf,0x10,0x13,0x24,0x15,0xe5,0x4b,0x92,
  6679. 0xa1,0x3e,0xd0,0xa8,0x26,0x7a,0xe2,0xf9,
  6680. 0x75,0xa3,0x85,0x74,0x1a,0xb9,0xce,0xf8,
  6681. 0x20,0x31,0x62,0x3d,0x55,0xb1,0xe4,0x71
  6682. };
  6683. WOLFSSL_SMALL_STACK_STATIC const byte msg3[] =
  6684. {
  6685. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  6686. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  6687. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  6688. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  6689. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  6690. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  6691. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  6692. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  6693. };
  6694. #endif /* WOLFSSL_AES_256 */
  6695. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  6696. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  6697. ERROR_OUT(-1, out);
  6698. #ifdef HAVE_AES_DECRYPT
  6699. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  6700. ERROR_OUT(-1, out);
  6701. #endif
  6702. #endif
  6703. if (wc_AesInit(enc, HEAP_HINT, devId) != 0)
  6704. ERROR_OUT(-5173, out);
  6705. else
  6706. enc_inited = 1;
  6707. #ifdef HAVE_AES_DECRYPT
  6708. if (wc_AesInit(dec, HEAP_HINT, devId) != 0)
  6709. ERROR_OUT(-5174, out);
  6710. else
  6711. dec_inited = 1;
  6712. #endif
  6713. #ifdef WOLFSSL_AES_128
  6714. /* 128 key tests */
  6715. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  6716. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  6717. ret = EVP_test(EVP_aes_128_cfb128(), key1, iv, msg1, sizeof(msg1),
  6718. cipher1, sizeof(cipher1));
  6719. if (ret != 0) {
  6720. return ret;
  6721. }
  6722. #endif
  6723. ret = wc_AesSetKey(enc, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  6724. if (ret != 0)
  6725. ERROR_OUT(-5175, out);
  6726. #ifdef HAVE_AES_DECRYPT
  6727. /* decrypt uses AES_ENCRYPTION */
  6728. ret = wc_AesSetKey(dec, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  6729. if (ret != 0)
  6730. ERROR_OUT(-5176, out);
  6731. #endif
  6732. XMEMSET(cipher, 0, sizeof(cipher));
  6733. ret = wc_AesCfbEncrypt(enc, cipher, msg1, AES_BLOCK_SIZE * 2);
  6734. if (ret != 0)
  6735. ERROR_OUT(-5177, out);
  6736. if (XMEMCMP(cipher, cipher1, AES_BLOCK_SIZE * 2))
  6737. ERROR_OUT(-5178, out);
  6738. /* test restarting encryption process */
  6739. ret = wc_AesCfbEncrypt(enc, cipher + (AES_BLOCK_SIZE * 2),
  6740. msg1 + (AES_BLOCK_SIZE * 2), AES_BLOCK_SIZE);
  6741. if (ret != 0)
  6742. ERROR_OUT(-5179, out);
  6743. if (XMEMCMP(cipher + (AES_BLOCK_SIZE * 2),
  6744. cipher1 + (AES_BLOCK_SIZE * 2), AES_BLOCK_SIZE))
  6745. ERROR_OUT(-5180, out);
  6746. #ifdef HAVE_AES_DECRYPT
  6747. ret = wc_AesCfbDecrypt(dec, plain, cipher, AES_BLOCK_SIZE * 3);
  6748. if (ret != 0)
  6749. ERROR_OUT(-5181, out);
  6750. if (XMEMCMP(plain, msg1, AES_BLOCK_SIZE * 3))
  6751. ERROR_OUT(-5182, out);
  6752. #endif /* HAVE_AES_DECRYPT */
  6753. #endif /* WOLFSSL_AES_128 */
  6754. #ifdef WOLFSSL_AES_192
  6755. /* 192 key size test */
  6756. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  6757. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  6758. ret = EVP_test(EVP_aes_192_cfb128(), key2, iv, msg2, sizeof(msg2),
  6759. cipher2, sizeof(cipher2));
  6760. if (ret != 0) {
  6761. return ret;
  6762. }
  6763. #endif
  6764. ret = wc_AesSetKey(enc, key2, sizeof(key2), iv, AES_ENCRYPTION);
  6765. if (ret != 0)
  6766. ERROR_OUT(-5183, out);
  6767. #ifdef HAVE_AES_DECRYPT
  6768. /* decrypt uses AES_ENCRYPTION */
  6769. ret = wc_AesSetKey(dec, key2, sizeof(key2), iv, AES_ENCRYPTION);
  6770. if (ret != 0)
  6771. ERROR_OUT(-5184, out);
  6772. #endif
  6773. XMEMSET(cipher, 0, sizeof(cipher));
  6774. ret = wc_AesCfbEncrypt(enc, cipher, msg2, AES_BLOCK_SIZE * 4);
  6775. if (ret != 0)
  6776. ERROR_OUT(-5185, out);
  6777. if (XMEMCMP(cipher, cipher2, AES_BLOCK_SIZE * 4))
  6778. ERROR_OUT(-5186, out);
  6779. #ifdef HAVE_AES_DECRYPT
  6780. ret = wc_AesCfbDecrypt(dec, plain, cipher, AES_BLOCK_SIZE * 4);
  6781. if (ret != 0)
  6782. ERROR_OUT(-5187, out);
  6783. if (XMEMCMP(plain, msg2, AES_BLOCK_SIZE * 4))
  6784. ERROR_OUT(-5188, out);
  6785. #endif /* HAVE_AES_DECRYPT */
  6786. #endif /* WOLFSSL_AES_192 */
  6787. #ifdef WOLFSSL_AES_256
  6788. /* 256 key size test */
  6789. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  6790. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  6791. ret = EVP_test(EVP_aes_256_cfb128(), key3, iv, msg3, sizeof(msg3),
  6792. cipher3, sizeof(cipher3));
  6793. if (ret != 0) {
  6794. return ret;
  6795. }
  6796. #endif
  6797. ret = wc_AesSetKey(enc, key3, sizeof(key3), iv, AES_ENCRYPTION);
  6798. if (ret != 0)
  6799. ERROR_OUT(-5189, out);
  6800. #ifdef HAVE_AES_DECRYPT
  6801. /* decrypt uses AES_ENCRYPTION */
  6802. ret = wc_AesSetKey(dec, key3, sizeof(key3), iv, AES_ENCRYPTION);
  6803. if (ret != 0)
  6804. ERROR_OUT(-5190, out);
  6805. #endif
  6806. /* test with data left overs, magic lengths are checking near edges */
  6807. XMEMSET(cipher, 0, sizeof(cipher));
  6808. ret = wc_AesCfbEncrypt(enc, cipher, msg3, 4);
  6809. if (ret != 0)
  6810. ERROR_OUT(-5191, out);
  6811. if (XMEMCMP(cipher, cipher3, 4))
  6812. ERROR_OUT(-5192, out);
  6813. ret = wc_AesCfbEncrypt(enc, cipher + 4, msg3 + 4, 27);
  6814. if (ret != 0)
  6815. ERROR_OUT(-5193, out);
  6816. if (XMEMCMP(cipher + 4, cipher3 + 4, 27))
  6817. ERROR_OUT(-5194, out);
  6818. ret = wc_AesCfbEncrypt(enc, cipher + 31, msg3 + 31,
  6819. (AES_BLOCK_SIZE * 4) - 31);
  6820. if (ret != 0)
  6821. ERROR_OUT(-5195, out);
  6822. if (XMEMCMP(cipher, cipher3, AES_BLOCK_SIZE * 4))
  6823. ERROR_OUT(-5196, out);
  6824. #ifdef HAVE_AES_DECRYPT
  6825. ret = wc_AesCfbDecrypt(dec, plain, cipher, 4);
  6826. if (ret != 0)
  6827. ERROR_OUT(-5197, out);
  6828. if (XMEMCMP(plain, msg3, 4))
  6829. ERROR_OUT(-5198, out);
  6830. ret = wc_AesCfbDecrypt(dec, plain + 4, cipher + 4, 4);
  6831. if (ret != 0)
  6832. ERROR_OUT(-5199, out);
  6833. ret = wc_AesCfbDecrypt(dec, plain + 8, cipher + 8, 23);
  6834. if (ret != 0)
  6835. ERROR_OUT(-5200, out);
  6836. if (XMEMCMP(plain + 4, msg3 + 4, 27))
  6837. ERROR_OUT(-5201, out);
  6838. ret = wc_AesCfbDecrypt(dec, plain + 31, cipher + 31,
  6839. (AES_BLOCK_SIZE * 4) - 31);
  6840. if (ret != 0)
  6841. ERROR_OUT(-5202, out);
  6842. if (XMEMCMP(plain, msg3, AES_BLOCK_SIZE * 4))
  6843. ERROR_OUT(-5203, out);
  6844. #endif /* HAVE_AES_DECRYPT */
  6845. #endif /* WOLFSSL_AES_256 */
  6846. out:
  6847. if (enc_inited)
  6848. wc_AesFree(enc);
  6849. if (dec_inited)
  6850. wc_AesFree(dec);
  6851. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  6852. if (enc)
  6853. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  6854. #ifdef HAVE_AES_DECRYPT
  6855. if (dec)
  6856. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  6857. #endif
  6858. #endif
  6859. return ret;
  6860. }
  6861. #if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  6862. static int aescfb1_test(void)
  6863. {
  6864. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  6865. Aes *enc = NULL;
  6866. #else
  6867. Aes enc[1];
  6868. #endif
  6869. int enc_inited = 0;
  6870. byte cipher[AES_BLOCK_SIZE];
  6871. #ifdef HAVE_AES_DECRYPT
  6872. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  6873. Aes *dec = NULL;
  6874. #else
  6875. Aes dec[1];
  6876. #endif
  6877. int dec_inited = 0;
  6878. byte plain [AES_BLOCK_SIZE];
  6879. #endif
  6880. int ret = 0;
  6881. #ifdef WOLFSSL_AES_128
  6882. WOLFSSL_SMALL_STACK_STATIC const byte iv[] = {
  6883. 0x4d,0xbb,0xdc,0xaa,0x59,0xf3,0x63,0xc9,
  6884. 0x2a,0x3b,0x98,0x43,0xad,0x20,0xe2,0xb7
  6885. };
  6886. WOLFSSL_SMALL_STACK_STATIC const byte key1[] =
  6887. {
  6888. 0xcd,0xef,0x9d,0x06,0x61,0xba,0xe4,0x73,
  6889. 0x8d,0x1a,0x58,0xa2,0xa6,0x22,0x8b,0x66
  6890. };
  6891. WOLFSSL_SMALL_STACK_STATIC const byte cipher1[] =
  6892. {
  6893. 0x00
  6894. };
  6895. WOLFSSL_SMALL_STACK_STATIC const byte msg1[] =
  6896. {
  6897. 0xC0
  6898. };
  6899. #endif /* WOLFSSL_AES_128 */
  6900. #ifdef WOLFSSL_AES_192
  6901. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] = {
  6902. 0x57,0xc6,0x89,0x7c,0x99,0x52,0x28,0x13,
  6903. 0xbf,0x67,0x9c,0xe1,0x13,0x70,0xaf,0x5e
  6904. };
  6905. WOLFSSL_SMALL_STACK_STATIC const byte key2[] =
  6906. {
  6907. 0xba,0xa1,0x58,0xa1,0x6b,0x50,0x4a,0x10,
  6908. 0x8e,0xd4,0x33,0x2e,0xe7,0xf2,0x9b,0xf6,
  6909. 0xd1,0xac,0x46,0xa8,0xde,0x5a,0xfe,0x7a
  6910. };
  6911. WOLFSSL_SMALL_STACK_STATIC const byte cipher2[] =
  6912. {
  6913. 0x30
  6914. };
  6915. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] =
  6916. {
  6917. 0x80
  6918. };
  6919. #endif /* WOLFSSL_AES_192 */
  6920. #ifdef WOLFSSL_AES_256
  6921. WOLFSSL_SMALL_STACK_STATIC const byte iv3[] = {
  6922. 0x63,0x2e,0x9f,0x83,0x1f,0xa3,0x80,0x5e,
  6923. 0x52,0x02,0xbc,0xe0,0x6d,0x04,0xf9,0xa0
  6924. };
  6925. WOLFSSL_SMALL_STACK_STATIC const byte key3[] =
  6926. {
  6927. 0xf6,0xfa,0xe4,0xf1,0x5d,0x91,0xfc,0x50,
  6928. 0x88,0x78,0x4f,0x84,0xa5,0x37,0x12,0x7e,
  6929. 0x32,0x63,0x55,0x9c,0x62,0x73,0x88,0x20,
  6930. 0xc2,0xcf,0x3d,0xe1,0x1c,0x2a,0x30,0x40
  6931. };
  6932. WOLFSSL_SMALL_STACK_STATIC const byte cipher3[] =
  6933. {
  6934. 0xF7, 0x00
  6935. };
  6936. WOLFSSL_SMALL_STACK_STATIC const byte msg3[] =
  6937. {
  6938. 0x41, 0xC0
  6939. };
  6940. #endif /* WOLFSSL_AES_256 */
  6941. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  6942. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  6943. ERROR_OUT(-1, out);
  6944. #ifdef HAVE_AES_DECRYPT
  6945. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  6946. ERROR_OUT(-1, out);
  6947. #endif
  6948. #endif
  6949. if (wc_AesInit(enc, HEAP_HINT, devId) != 0)
  6950. ERROR_OUT(-5204, out);
  6951. else
  6952. enc_inited = 1;
  6953. #ifdef HAVE_AES_DECRYPT
  6954. if (wc_AesInit(dec, HEAP_HINT, devId) != 0)
  6955. ERROR_OUT(-5205, out);
  6956. else
  6957. dec_inited = 1;
  6958. #endif
  6959. #ifdef WOLFSSL_AES_128
  6960. /* 128 key tests */
  6961. ret = wc_AesSetKey(enc, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  6962. if (ret != 0)
  6963. ERROR_OUT(-5206, out);
  6964. #ifdef HAVE_AES_DECRYPT
  6965. /* decrypt uses AES_ENCRYPTION */
  6966. ret = wc_AesSetKey(dec, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  6967. if (ret != 0)
  6968. ERROR_OUT(-5207, out);
  6969. #endif
  6970. XMEMSET(cipher, 0, sizeof(cipher));
  6971. ret = wc_AesCfb1Encrypt(enc, cipher, msg1, 2);
  6972. if (ret != 0)
  6973. ERROR_OUT(-5208, out);
  6974. if (cipher[0] != cipher1[0])
  6975. ERROR_OUT(-5209, out);
  6976. #ifdef HAVE_AES_DECRYPT
  6977. ret = wc_AesCfb1Decrypt(dec, plain, cipher, 2);
  6978. if (ret != 0)
  6979. ERROR_OUT(-5210, out);
  6980. if (plain[0] != msg1[0])
  6981. ERROR_OUT(-5211, out);
  6982. #endif /* HAVE_AES_DECRYPT */
  6983. #ifdef OPENSSL_EXTRA
  6984. ret = wc_AesSetKey(enc, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  6985. if (ret != 0)
  6986. ERROR_OUT(-5212, out);
  6987. XMEMSET(cipher, 0, sizeof(cipher));
  6988. ret = wc_AesCfb1Encrypt(enc, cipher, msg1,
  6989. sizeof(msg1) * WOLFSSL_BIT_SIZE);
  6990. if (ret != 0)
  6991. ERROR_OUT(-5213, out);
  6992. #ifndef WOLFCRYPT_ONLY
  6993. ret = EVP_test(EVP_aes_128_cfb1(), key1, iv, msg1, sizeof(msg1),
  6994. cipher, sizeof(msg1));
  6995. if (ret != 0) {
  6996. goto out;
  6997. }
  6998. #endif
  6999. #endif
  7000. #endif /* WOLFSSL_AES_128 */
  7001. #ifdef WOLFSSL_AES_192
  7002. /* 192 key tests */
  7003. ret = wc_AesSetKey(enc, key2, sizeof(key2), iv2, AES_ENCRYPTION);
  7004. if (ret != 0)
  7005. ERROR_OUT(-5214, out);
  7006. XMEMSET(cipher, 0, sizeof(cipher));
  7007. ret = wc_AesCfb1Encrypt(enc, cipher, msg2, 4);
  7008. if (ret != 0)
  7009. ERROR_OUT(-5215, out);
  7010. if (XMEMCMP(cipher, cipher2, sizeof(cipher2)) != 0)
  7011. ERROR_OUT(-5216, out);
  7012. #ifdef OPENSSL_EXTRA
  7013. ret = wc_AesSetKey(enc, key2, sizeof(key2), iv2, AES_ENCRYPTION);
  7014. if (ret != 0)
  7015. ERROR_OUT(-5217, out);
  7016. XMEMSET(cipher, 0, sizeof(cipher));
  7017. ret = wc_AesCfb1Encrypt(enc, cipher, msg2,
  7018. sizeof(msg2) * WOLFSSL_BIT_SIZE);
  7019. if (ret != 0)
  7020. ERROR_OUT(-5218, out);
  7021. #ifndef WOLFCRYPT_ONLY
  7022. ret = EVP_test(EVP_aes_192_cfb1(), key2, iv2, msg2, sizeof(msg2),
  7023. cipher, sizeof(msg2));
  7024. if (ret != 0) {
  7025. goto out;
  7026. }
  7027. #endif
  7028. #endif
  7029. #endif /* WOLFSSL_AES_192 */
  7030. #ifdef WOLFSSL_AES_256
  7031. /* 256 key tests */
  7032. ret = wc_AesSetKey(enc, key3, sizeof(key3), iv3, AES_ENCRYPTION);
  7033. if (ret != 0)
  7034. ERROR_OUT(-5219, out);
  7035. XMEMSET(cipher, 0, sizeof(cipher));
  7036. ret = wc_AesCfb1Encrypt(enc, cipher, msg3, 10);
  7037. if (ret != 0)
  7038. ERROR_OUT(-5220, out);
  7039. if (XMEMCMP(cipher, cipher3, sizeof(cipher3)) != 0)
  7040. ERROR_OUT(-5221, out);
  7041. #ifdef OPENSSL_EXTRA
  7042. ret = wc_AesSetKey(enc, key3, sizeof(key3), iv3, AES_ENCRYPTION);
  7043. if (ret != 0)
  7044. ERROR_OUT(-5222, out);
  7045. XMEMSET(cipher, 0, sizeof(cipher));
  7046. ret = wc_AesCfb1Encrypt(enc, cipher, msg3,
  7047. sizeof(msg3) * WOLFSSL_BIT_SIZE);
  7048. if (ret != 0)
  7049. ERROR_OUT(-5223, out);
  7050. #ifndef WOLFCRYPT_ONLY
  7051. ret = EVP_test(EVP_aes_256_cfb1(), key3, iv3, msg3, sizeof(msg3),
  7052. cipher, sizeof(msg3));
  7053. if (ret != 0) {
  7054. goto out;
  7055. }
  7056. #endif
  7057. #endif
  7058. out:
  7059. if (enc_inited)
  7060. wc_AesFree(enc);
  7061. #ifdef HAVE_AES_DECRYPT
  7062. if (dec_inited)
  7063. wc_AesFree(dec);
  7064. #endif
  7065. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7066. if (enc)
  7067. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  7068. #ifdef HAVE_AES_DECRYPT
  7069. if (dec)
  7070. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  7071. #endif
  7072. #endif
  7073. #endif /* WOLFSSL_AES_256 */
  7074. return ret;
  7075. }
  7076. static int aescfb8_test(void)
  7077. {
  7078. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7079. Aes *enc = NULL;
  7080. #else
  7081. Aes enc[1];
  7082. #endif
  7083. int enc_inited = 0;
  7084. byte cipher[AES_BLOCK_SIZE];
  7085. #ifdef HAVE_AES_DECRYPT
  7086. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7087. Aes *dec = NULL;
  7088. #else
  7089. Aes dec[1];
  7090. #endif
  7091. int dec_inited = 0;
  7092. byte plain [AES_BLOCK_SIZE];
  7093. #endif
  7094. int ret = 0;
  7095. #ifdef WOLFSSL_AES_128
  7096. WOLFSSL_SMALL_STACK_STATIC const byte iv[] = {
  7097. 0xf4,0x75,0xc6,0x49,0x91,0xb2,0x0e,0xae,
  7098. 0xe1,0x83,0xa2,0x26,0x29,0xe2,0x1e,0x22
  7099. };
  7100. WOLFSSL_SMALL_STACK_STATIC const byte key1[] =
  7101. {
  7102. 0xc8,0xfe,0x9b,0xf7,0x7b,0x93,0x0f,0x46,
  7103. 0xd2,0x07,0x8b,0x8c,0x0e,0x65,0x7c,0xd4
  7104. };
  7105. WOLFSSL_SMALL_STACK_STATIC const byte cipher1[] =
  7106. {
  7107. 0xd2,0x76,0x91
  7108. };
  7109. WOLFSSL_SMALL_STACK_STATIC const byte msg1[] =
  7110. {
  7111. 0xc9,0x06,0x35
  7112. };
  7113. #endif /* WOLFSSL_AES_128 */
  7114. #ifdef WOLFSSL_AES_192
  7115. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] = {
  7116. 0x0a,0x02,0x84,0x6b,0x62,0xab,0xb6,0x93,
  7117. 0xef,0x31,0xd7,0x54,0x84,0x2e,0xed,0x29
  7118. };
  7119. WOLFSSL_SMALL_STACK_STATIC const byte key2[] =
  7120. {
  7121. 0xba,0xf0,0x8b,0x76,0x31,0x7a,0x65,0xc5,
  7122. 0xf0,0x7a,0xe6,0xf5,0x7e,0xb0,0xe6,0x54,
  7123. 0x88,0x65,0x93,0x24,0xd2,0x97,0x09,0xe3
  7124. };
  7125. WOLFSSL_SMALL_STACK_STATIC const byte cipher2[] =
  7126. {
  7127. 0x72,0x9c,0x0b,0x6d,0xeb,0x75,0xfa,0x6e,
  7128. 0xb5,0xe8
  7129. };
  7130. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] =
  7131. {
  7132. 0x98,0x95,0x93,0x24,0x02,0x39,0x3d,0xc3,
  7133. 0x3a,0x60
  7134. };
  7135. #endif
  7136. #ifdef WOLFSSL_AES_256
  7137. WOLFSSL_SMALL_STACK_STATIC const byte iv3[] = {
  7138. 0x33,0x8c,0x55,0x2f,0xf1,0xec,0xa1,0x44,
  7139. 0x08,0xe0,0x5d,0x8c,0xf9,0xf3,0xb3,0x1b
  7140. };
  7141. WOLFSSL_SMALL_STACK_STATIC const byte key3[] =
  7142. {
  7143. 0x06,0x48,0x74,0x09,0x2f,0x7a,0x13,0xcc,
  7144. 0x44,0x62,0x24,0x7a,0xd4,0x23,0xd0,0xe9,
  7145. 0x6e,0xdf,0x42,0xe8,0xb6,0x7a,0x5a,0x23,
  7146. 0xb7,0xa0,0xa6,0x47,0x7b,0x09,0x8e,0x66
  7147. };
  7148. WOLFSSL_SMALL_STACK_STATIC const byte cipher3[] =
  7149. {
  7150. 0x1c,0xff,0x95
  7151. };
  7152. WOLFSSL_SMALL_STACK_STATIC const byte msg3[] =
  7153. {
  7154. 0xb9,0x74,0xfa
  7155. };
  7156. #endif
  7157. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7158. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  7159. ERROR_OUT(-5238, out);
  7160. #ifdef HAVE_AES_DECRYPT
  7161. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  7162. ERROR_OUT(-5239, out);
  7163. #endif
  7164. #endif
  7165. if (wc_AesInit(enc, HEAP_HINT, devId) != 0)
  7166. ERROR_OUT(-5224, out);
  7167. else
  7168. enc_inited = 1;
  7169. #ifdef HAVE_AES_DECRYPT
  7170. if (wc_AesInit(dec, HEAP_HINT, devId) != 0)
  7171. ERROR_OUT(-5225, out);
  7172. else
  7173. dec_inited = 1;
  7174. #endif
  7175. #ifdef WOLFSSL_AES_128
  7176. /* 128 key tests */
  7177. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  7178. ret = EVP_test(EVP_aes_128_cfb8(), key1, iv, msg1, sizeof(msg1),
  7179. cipher1, sizeof(cipher1));
  7180. if (ret != 0) {
  7181. return ret;
  7182. }
  7183. #endif
  7184. ret = wc_AesSetKey(enc, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  7185. if (ret != 0)
  7186. ERROR_OUT(-5226, out);
  7187. #ifdef HAVE_AES_DECRYPT
  7188. /* decrypt uses AES_ENCRYPTION */
  7189. ret = wc_AesSetKey(dec, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  7190. if (ret != 0)
  7191. ERROR_OUT(-5227, out);
  7192. #endif
  7193. XMEMSET(cipher, 0, sizeof(cipher));
  7194. ret = wc_AesCfb8Encrypt(enc, cipher, msg1, sizeof(msg1));
  7195. if (ret != 0)
  7196. ERROR_OUT(-5228, out);
  7197. if (XMEMCMP(cipher, cipher1, sizeof(cipher1)) != 0)
  7198. ERROR_OUT(-5229, out);
  7199. #ifdef HAVE_AES_DECRYPT
  7200. ret = wc_AesCfb8Decrypt(dec, plain, cipher, sizeof(msg1));
  7201. if (ret != 0)
  7202. ERROR_OUT(-5230, out);
  7203. if (XMEMCMP(plain, msg1, sizeof(msg1)) != 0)
  7204. ERROR_OUT(-5231, out);
  7205. #endif /* HAVE_AES_DECRYPT */
  7206. #endif /* WOLFSSL_AES_128 */
  7207. #ifdef WOLFSSL_AES_192
  7208. /* 192 key tests */
  7209. ret = wc_AesSetKey(enc, key2, sizeof(key2), iv2, AES_ENCRYPTION);
  7210. if (ret != 0)
  7211. ERROR_OUT(-5232, out);
  7212. XMEMSET(cipher, 0, sizeof(cipher));
  7213. ret = wc_AesCfb8Encrypt(enc, cipher, msg2, sizeof(msg2));
  7214. if (ret != 0)
  7215. ERROR_OUT(-5233, out);
  7216. if (XMEMCMP(cipher, cipher2, sizeof(msg2)) != 0)
  7217. ERROR_OUT(-5234, out);
  7218. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  7219. ret = EVP_test(EVP_aes_192_cfb8(), key2, iv2, msg2, sizeof(msg2),
  7220. cipher2, sizeof(msg2));
  7221. if (ret != 0) {
  7222. return ret;
  7223. }
  7224. #endif
  7225. #endif /* WOLFSSL_AES_192 */
  7226. #ifdef WOLFSSL_AES_256
  7227. /* 256 key tests */
  7228. ret = wc_AesSetKey(enc, key3, sizeof(key3), iv3, AES_ENCRYPTION);
  7229. if (ret != 0)
  7230. ERROR_OUT(-5235, out);
  7231. XMEMSET(cipher, 0, sizeof(cipher));
  7232. ret = wc_AesCfb8Encrypt(enc, cipher, msg3, sizeof(msg3));
  7233. if (ret != 0)
  7234. ERROR_OUT(-5236, out);
  7235. if (XMEMCMP(cipher, cipher3, sizeof(cipher3)) != 0)
  7236. ERROR_OUT(-5237, out);
  7237. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  7238. ret = EVP_test(EVP_aes_256_cfb8(), key3, iv3, msg3, sizeof(msg3),
  7239. cipher3, sizeof(msg3));
  7240. if (ret != 0) {
  7241. goto out;
  7242. }
  7243. #endif
  7244. out:
  7245. if (enc_inited)
  7246. wc_AesFree(enc);
  7247. #ifdef HAVE_AES_DECRYPT
  7248. if (dec_inited)
  7249. wc_AesFree(dec);
  7250. #endif
  7251. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7252. if (enc)
  7253. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  7254. #ifdef HAVE_AES_DECRYPT
  7255. if (dec)
  7256. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  7257. #endif
  7258. #endif
  7259. #endif /* WOLFSSL_AES_256 */
  7260. return ret;
  7261. }
  7262. #endif /* !HAVE_SELFTEST && !HAVE_FIPS */
  7263. #endif /* WOLFSSL_AES_CFB */
  7264. static int aes_key_size_test(void)
  7265. {
  7266. int ret;
  7267. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7268. Aes *aes;
  7269. #else
  7270. Aes aes[1];
  7271. #endif
  7272. byte key16[] = { 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  7273. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66 };
  7274. #ifndef WOLFSSL_CRYPTOCELL
  7275. byte key24[] = { 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  7276. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  7277. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37 };
  7278. #endif
  7279. byte key32[] = { 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  7280. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  7281. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  7282. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66 };
  7283. byte iv[] = "1234567890abcdef";
  7284. #ifndef HAVE_FIPS
  7285. word32 keySize;
  7286. #endif
  7287. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7288. if ((aes = (Aes *)XMALLOC(sizeof *aes, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  7289. return -5315;
  7290. #endif
  7291. #if !defined(HAVE_FIPS) || \
  7292. defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)
  7293. /* w/ FIPS v1 (cert 2425) wc_AesInit just returns 0 always as it's not
  7294. * supported with that FIPS version */
  7295. ret = wc_AesInit(NULL, HEAP_HINT, devId);
  7296. if (ret != BAD_FUNC_ARG)
  7297. ERROR_OUT(-5300, out);
  7298. #endif
  7299. ret = wc_AesInit(aes, HEAP_HINT, devId);
  7300. /* 0 check OK for FIPSv1 */
  7301. if (ret != 0)
  7302. ERROR_OUT(-5301, out);
  7303. #ifndef HAVE_FIPS
  7304. /* Parameter Validation testing. */
  7305. ret = wc_AesGetKeySize(NULL, NULL);
  7306. if (ret != BAD_FUNC_ARG)
  7307. ERROR_OUT(-5302, out);
  7308. ret = wc_AesGetKeySize(aes, NULL);
  7309. if (ret != BAD_FUNC_ARG)
  7310. ERROR_OUT(-5303, out);
  7311. ret = wc_AesGetKeySize(NULL, &keySize);
  7312. if (ret != BAD_FUNC_ARG)
  7313. ERROR_OUT(-5304, out);
  7314. /* Crashes in FIPS */
  7315. ret = wc_AesSetKey(NULL, key16, sizeof(key16), iv, AES_ENCRYPTION);
  7316. if (ret != BAD_FUNC_ARG)
  7317. ERROR_OUT(-5305, out);
  7318. #endif
  7319. /* NULL IV indicates to use all zeros IV. */
  7320. ret = wc_AesSetKey(aes, key16, sizeof(key16), NULL, AES_ENCRYPTION);
  7321. #ifdef WOLFSSL_AES_128
  7322. if (ret != 0)
  7323. #else
  7324. if (ret != BAD_FUNC_ARG)
  7325. #endif
  7326. ERROR_OUT(-5306, out);
  7327. ret = wc_AesSetKey(aes, key32, sizeof(key32) - 1, iv, AES_ENCRYPTION);
  7328. if (ret != BAD_FUNC_ARG)
  7329. ERROR_OUT(-5307, out);
  7330. /* CryptoCell handles rounds internally */
  7331. #if !defined(HAVE_FIPS) && !defined(WOLFSSL_CRYPTOCELL)
  7332. /* PSA don't use aes->rounds */
  7333. #if !defined(WOLFSSL_HAVE_PSA) || defined(WOLFSSL_PSA_NO_AES)
  7334. /* Force invalid rounds */
  7335. aes->rounds = 16;
  7336. ret = wc_AesGetKeySize(aes, &keySize);
  7337. if (ret != BAD_FUNC_ARG)
  7338. ERROR_OUT(-5308, out);
  7339. #endif
  7340. #endif
  7341. ret = wc_AesSetKey(aes, key16, sizeof(key16), iv, AES_ENCRYPTION);
  7342. #ifdef WOLFSSL_AES_128
  7343. if (ret != 0)
  7344. #else
  7345. if (ret != BAD_FUNC_ARG)
  7346. #endif
  7347. ERROR_OUT(-5309, out);
  7348. #if !defined(HAVE_FIPS) && defined(WOLFSSL_AES_128)
  7349. ret = wc_AesGetKeySize(aes, &keySize);
  7350. if (ret != 0 || keySize != sizeof(key16))
  7351. ERROR_OUT(-5310, out);
  7352. #endif
  7353. #ifndef WOLFSSL_CRYPTOCELL
  7354. /* Cryptocell only supports AES-128 key size */
  7355. ret = wc_AesSetKey(aes, key24, sizeof(key24), iv, AES_ENCRYPTION);
  7356. #ifdef WOLFSSL_AES_192
  7357. if (ret != 0)
  7358. #else
  7359. if (ret != BAD_FUNC_ARG)
  7360. #endif
  7361. ERROR_OUT(-5311, out);
  7362. #if !defined(HAVE_FIPS) && defined(WOLFSSL_AES_192)
  7363. ret = wc_AesGetKeySize(aes, &keySize);
  7364. if (ret != 0 || keySize != sizeof(key24))
  7365. ERROR_OUT(-5312, out);
  7366. #endif
  7367. ret = wc_AesSetKey(aes, key32, sizeof(key32), iv, AES_ENCRYPTION);
  7368. #ifdef WOLFSSL_AES_256
  7369. if (ret != 0)
  7370. #else
  7371. if (ret != BAD_FUNC_ARG)
  7372. #endif
  7373. ERROR_OUT(-5313, out);
  7374. #if !defined(HAVE_FIPS) && defined(WOLFSSL_AES_256)
  7375. ret = wc_AesGetKeySize(aes, &keySize);
  7376. if (ret != 0 || keySize != sizeof(key32))
  7377. ERROR_OUT(-5314, out);
  7378. #endif
  7379. #endif /* !WOLFSSL_CRYPTOCELL */
  7380. ret = 0; /* success */
  7381. out:
  7382. wc_AesFree(aes);
  7383. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7384. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_AES);
  7385. #endif
  7386. return ret;
  7387. }
  7388. #if defined(WOLFSSL_AES_XTS)
  7389. /* test vectors from http://csrc.nist.gov/groups/STM/cavp/block-cipher-modes.html */
  7390. #ifdef WOLFSSL_AES_128
  7391. static int aes_xts_128_test(void)
  7392. {
  7393. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7394. XtsAes *aes = NULL;
  7395. #else
  7396. XtsAes aes[1];
  7397. #endif
  7398. int aes_inited = 0;
  7399. int ret = 0;
  7400. unsigned char buf[AES_BLOCK_SIZE * 2];
  7401. unsigned char cipher[AES_BLOCK_SIZE * 2];
  7402. /* 128 key tests */
  7403. WOLFSSL_SMALL_STACK_STATIC unsigned char k1[] = {
  7404. 0xa1, 0xb9, 0x0c, 0xba, 0x3f, 0x06, 0xac, 0x35,
  7405. 0x3b, 0x2c, 0x34, 0x38, 0x76, 0x08, 0x17, 0x62,
  7406. 0x09, 0x09, 0x23, 0x02, 0x6e, 0x91, 0x77, 0x18,
  7407. 0x15, 0xf2, 0x9d, 0xab, 0x01, 0x93, 0x2f, 0x2f
  7408. };
  7409. WOLFSSL_SMALL_STACK_STATIC unsigned char i1[] = {
  7410. 0x4f, 0xae, 0xf7, 0x11, 0x7c, 0xda, 0x59, 0xc6,
  7411. 0x6e, 0x4b, 0x92, 0x01, 0x3e, 0x76, 0x8a, 0xd5
  7412. };
  7413. WOLFSSL_SMALL_STACK_STATIC unsigned char p1[] = {
  7414. 0xeb, 0xab, 0xce, 0x95, 0xb1, 0x4d, 0x3c, 0x8d,
  7415. 0x6f, 0xb3, 0x50, 0x39, 0x07, 0x90, 0x31, 0x1c
  7416. };
  7417. /* plain text test of partial block is not from NIST test vector list */
  7418. WOLFSSL_SMALL_STACK_STATIC unsigned char pp[] = {
  7419. 0xeb, 0xab, 0xce, 0x95, 0xb1, 0x4d, 0x3c, 0x8d,
  7420. 0x6f, 0xb3, 0x50, 0x39, 0x07, 0x90, 0x31, 0x1c,
  7421. 0x6e, 0x4b, 0x92, 0x01, 0x3e, 0x76, 0x8a, 0xd5
  7422. };
  7423. WOLFSSL_SMALL_STACK_STATIC unsigned char c1[] = {
  7424. 0x77, 0x8a, 0xe8, 0xb4, 0x3c, 0xb9, 0x8d, 0x5a,
  7425. 0x82, 0x50, 0x81, 0xd5, 0xbe, 0x47, 0x1c, 0x63
  7426. };
  7427. WOLFSSL_SMALL_STACK_STATIC unsigned char k2[] = {
  7428. 0x39, 0x25, 0x79, 0x05, 0xdf, 0xcc, 0x77, 0x76,
  7429. 0x6c, 0x87, 0x0a, 0x80, 0x6a, 0x60, 0xe3, 0xc0,
  7430. 0x93, 0xd1, 0x2a, 0xcf, 0xcb, 0x51, 0x42, 0xfa,
  7431. 0x09, 0x69, 0x89, 0x62, 0x5b, 0x60, 0xdb, 0x16
  7432. };
  7433. WOLFSSL_SMALL_STACK_STATIC unsigned char i2[] = {
  7434. 0x5c, 0xf7, 0x9d, 0xb6, 0xc5, 0xcd, 0x99, 0x1a,
  7435. 0x1c, 0x78, 0x81, 0x42, 0x24, 0x95, 0x1e, 0x84
  7436. };
  7437. WOLFSSL_SMALL_STACK_STATIC unsigned char p2[] = {
  7438. 0xbd, 0xc5, 0x46, 0x8f, 0xbc, 0x8d, 0x50, 0xa1,
  7439. 0x0d, 0x1c, 0x85, 0x7f, 0x79, 0x1c, 0x5c, 0xba,
  7440. 0xb3, 0x81, 0x0d, 0x0d, 0x73, 0xcf, 0x8f, 0x20,
  7441. 0x46, 0xb1, 0xd1, 0x9e, 0x7d, 0x5d, 0x8a, 0x56
  7442. };
  7443. WOLFSSL_SMALL_STACK_STATIC unsigned char c2[] = {
  7444. 0xd6, 0xbe, 0x04, 0x6d, 0x41, 0xf2, 0x3b, 0x5e,
  7445. 0xd7, 0x0b, 0x6b, 0x3d, 0x5c, 0x8e, 0x66, 0x23,
  7446. 0x2b, 0xe6, 0xb8, 0x07, 0xd4, 0xdc, 0xc6, 0x0e,
  7447. 0xff, 0x8d, 0xbc, 0x1d, 0x9f, 0x7f, 0xc8, 0x22
  7448. };
  7449. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7450. if ((aes = (XtsAes *)XMALLOC(sizeof *aes, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  7451. ERROR_OUT(-5417, out);
  7452. #endif
  7453. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  7454. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  7455. ret = EVP_test(EVP_aes_128_xts(), k2, i2, p2, sizeof(p2), c2, sizeof(c2));
  7456. if (ret != 0) {
  7457. printf("EVP_aes_128_xts failed!\n");
  7458. goto out;
  7459. }
  7460. #endif
  7461. XMEMSET(buf, 0, sizeof(buf));
  7462. if (wc_AesXtsSetKey(aes, k2, sizeof(k2), AES_ENCRYPTION,
  7463. HEAP_HINT, devId) != 0)
  7464. ERROR_OUT(-5400, out);
  7465. else
  7466. aes_inited = 1;
  7467. ret = wc_AesXtsEncrypt(aes, buf, p2, sizeof(p2), i2, sizeof(i2));
  7468. #if defined(WOLFSSL_ASYNC_CRYPT)
  7469. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7470. #endif
  7471. if (ret != 0)
  7472. ERROR_OUT(-5401, out);
  7473. if (XMEMCMP(c2, buf, sizeof(c2)))
  7474. ERROR_OUT(-5402, out);
  7475. XMEMSET(buf, 0, sizeof(buf));
  7476. wc_AesXtsFree(aes);
  7477. if (wc_AesXtsSetKey(aes, k1, sizeof(k1), AES_ENCRYPTION,
  7478. HEAP_HINT, devId) != 0)
  7479. ERROR_OUT(-5403, out);
  7480. ret = wc_AesXtsEncrypt(aes, buf, p1, sizeof(p1), i1, sizeof(i1));
  7481. #if defined(WOLFSSL_ASYNC_CRYPT)
  7482. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7483. #endif
  7484. if (ret != 0)
  7485. ERROR_OUT(-5404, out);
  7486. if (XMEMCMP(c1, buf, AES_BLOCK_SIZE))
  7487. ERROR_OUT(-5405, out);
  7488. /* partial block encryption test */
  7489. XMEMSET(cipher, 0, sizeof(cipher));
  7490. ret = wc_AesXtsEncrypt(aes, cipher, pp, sizeof(pp), i1, sizeof(i1));
  7491. #if defined(WOLFSSL_ASYNC_CRYPT)
  7492. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7493. #endif
  7494. if (ret != 0)
  7495. ERROR_OUT(-5406, out);
  7496. wc_AesXtsFree(aes);
  7497. /* partial block decrypt test */
  7498. XMEMSET(buf, 0, sizeof(buf));
  7499. if (wc_AesXtsSetKey(aes, k1, sizeof(k1), AES_DECRYPTION,
  7500. HEAP_HINT, devId) != 0)
  7501. ERROR_OUT(-5407, out);
  7502. ret = wc_AesXtsDecrypt(aes, buf, cipher, sizeof(pp), i1, sizeof(i1));
  7503. #if defined(WOLFSSL_ASYNC_CRYPT)
  7504. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7505. #endif
  7506. if (ret != 0)
  7507. ERROR_OUT(-5408, out);
  7508. if (XMEMCMP(pp, buf, sizeof(pp)))
  7509. ERROR_OUT(-5409, out);
  7510. /* NIST decrypt test vector */
  7511. XMEMSET(buf, 0, sizeof(buf));
  7512. ret = wc_AesXtsDecrypt(aes, buf, c1, sizeof(c1), i1, sizeof(i1));
  7513. #if defined(WOLFSSL_ASYNC_CRYPT)
  7514. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7515. #endif
  7516. if (ret != 0)
  7517. ERROR_OUT(-5410, out);
  7518. if (XMEMCMP(p1, buf, AES_BLOCK_SIZE))
  7519. ERROR_OUT(-5411, out);
  7520. /* fail case with decrypting using wrong key */
  7521. XMEMSET(buf, 0, sizeof(buf));
  7522. ret = wc_AesXtsDecrypt(aes, buf, c2, sizeof(c2), i2, sizeof(i2));
  7523. #if defined(WOLFSSL_ASYNC_CRYPT)
  7524. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7525. #endif
  7526. if (ret != 0)
  7527. ERROR_OUT(-5412, out);
  7528. if (XMEMCMP(p2, buf, sizeof(p2)) == 0) /* fail case with wrong key */
  7529. ERROR_OUT(-5413, out);
  7530. wc_AesXtsFree(aes);
  7531. /* set correct key and retest */
  7532. XMEMSET(buf, 0, sizeof(buf));
  7533. if (wc_AesXtsSetKey(aes, k2, sizeof(k2), AES_DECRYPTION,
  7534. HEAP_HINT, devId) != 0)
  7535. ERROR_OUT(-5414, out);
  7536. ret = wc_AesXtsDecrypt(aes, buf, c2, sizeof(c2), i2, sizeof(i2));
  7537. #if defined(WOLFSSL_ASYNC_CRYPT)
  7538. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7539. #endif
  7540. if (ret != 0)
  7541. ERROR_OUT(-5415, out);
  7542. if (XMEMCMP(p2, buf, sizeof(p2)))
  7543. ERROR_OUT(-5416, out);
  7544. out:
  7545. if (aes_inited)
  7546. wc_AesXtsFree(aes);
  7547. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7548. if (aes)
  7549. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_AES);
  7550. #endif
  7551. return ret;
  7552. }
  7553. #endif /* WOLFSSL_AES_128 */
  7554. #ifdef WOLFSSL_AES_256
  7555. static int aes_xts_256_test(void)
  7556. {
  7557. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7558. XtsAes *aes = NULL;
  7559. #else
  7560. XtsAes aes[1];
  7561. #endif
  7562. int aes_inited = 0;
  7563. int ret = 0;
  7564. unsigned char buf[AES_BLOCK_SIZE * 3];
  7565. unsigned char cipher[AES_BLOCK_SIZE * 3];
  7566. /* 256 key tests */
  7567. WOLFSSL_SMALL_STACK_STATIC unsigned char k1[] = {
  7568. 0x1e, 0xa6, 0x61, 0xc5, 0x8d, 0x94, 0x3a, 0x0e,
  7569. 0x48, 0x01, 0xe4, 0x2f, 0x4b, 0x09, 0x47, 0x14,
  7570. 0x9e, 0x7f, 0x9f, 0x8e, 0x3e, 0x68, 0xd0, 0xc7,
  7571. 0x50, 0x52, 0x10, 0xbd, 0x31, 0x1a, 0x0e, 0x7c,
  7572. 0xd6, 0xe1, 0x3f, 0xfd, 0xf2, 0x41, 0x8d, 0x8d,
  7573. 0x19, 0x11, 0xc0, 0x04, 0xcd, 0xa5, 0x8d, 0xa3,
  7574. 0xd6, 0x19, 0xb7, 0xe2, 0xb9, 0x14, 0x1e, 0x58,
  7575. 0x31, 0x8e, 0xea, 0x39, 0x2c, 0xf4, 0x1b, 0x08
  7576. };
  7577. WOLFSSL_SMALL_STACK_STATIC unsigned char i1[] = {
  7578. 0xad, 0xf8, 0xd9, 0x26, 0x27, 0x46, 0x4a, 0xd2,
  7579. 0xf0, 0x42, 0x8e, 0x84, 0xa9, 0xf8, 0x75, 0x64
  7580. };
  7581. WOLFSSL_SMALL_STACK_STATIC unsigned char p1[] = {
  7582. 0x2e, 0xed, 0xea, 0x52, 0xcd, 0x82, 0x15, 0xe1,
  7583. 0xac, 0xc6, 0x47, 0xe8, 0x10, 0xbb, 0xc3, 0x64,
  7584. 0x2e, 0x87, 0x28, 0x7f, 0x8d, 0x2e, 0x57, 0xe3,
  7585. 0x6c, 0x0a, 0x24, 0xfb, 0xc1, 0x2a, 0x20, 0x2e
  7586. };
  7587. /* plain text test of partial block is not from NIST test vector list */
  7588. WOLFSSL_SMALL_STACK_STATIC unsigned char pp[] = {
  7589. 0xeb, 0xab, 0xce, 0x95, 0xb1, 0x4d, 0x3c, 0x8d,
  7590. 0x6f, 0xb3, 0x50, 0x39, 0x07, 0x90, 0x31, 0x1c,
  7591. 0x6e, 0x4b, 0x92, 0x01, 0x3e, 0x76, 0x8a, 0xd5
  7592. };
  7593. WOLFSSL_SMALL_STACK_STATIC unsigned char c1[] = {
  7594. 0xcb, 0xaa, 0xd0, 0xe2, 0xf6, 0xce, 0xa3, 0xf5,
  7595. 0x0b, 0x37, 0xf9, 0x34, 0xd4, 0x6a, 0x9b, 0x13,
  7596. 0x0b, 0x9d, 0x54, 0xf0, 0x7e, 0x34, 0xf3, 0x6a,
  7597. 0xf7, 0x93, 0xe8, 0x6f, 0x73, 0xc6, 0xd7, 0xdb
  7598. };
  7599. WOLFSSL_SMALL_STACK_STATIC unsigned char k2[] = {
  7600. 0xad, 0x50, 0x4b, 0x85, 0xd7, 0x51, 0xbf, 0xba,
  7601. 0x69, 0x13, 0xb4, 0xcc, 0x79, 0xb6, 0x5a, 0x62,
  7602. 0xf7, 0xf3, 0x9d, 0x36, 0x0f, 0x35, 0xb5, 0xec,
  7603. 0x4a, 0x7e, 0x95, 0xbd, 0x9b, 0xa5, 0xf2, 0xec,
  7604. 0xc1, 0xd7, 0x7e, 0xa3, 0xc3, 0x74, 0xbd, 0x4b,
  7605. 0x13, 0x1b, 0x07, 0x83, 0x87, 0xdd, 0x55, 0x5a,
  7606. 0xb5, 0xb0, 0xc7, 0xe5, 0x2d, 0xb5, 0x06, 0x12,
  7607. 0xd2, 0xb5, 0x3a, 0xcb, 0x47, 0x8a, 0x53, 0xb4
  7608. };
  7609. WOLFSSL_SMALL_STACK_STATIC unsigned char i2[] = {
  7610. 0xe6, 0x42, 0x19, 0xed, 0xe0, 0xe1, 0xc2, 0xa0,
  7611. 0x0e, 0xf5, 0x58, 0x6a, 0xc4, 0x9b, 0xeb, 0x6f
  7612. };
  7613. WOLFSSL_SMALL_STACK_STATIC unsigned char p2[] = {
  7614. 0x24, 0xcb, 0x76, 0x22, 0x55, 0xb5, 0xa8, 0x00,
  7615. 0xf4, 0x6e, 0x80, 0x60, 0x56, 0x9e, 0x05, 0x53,
  7616. 0xbc, 0xfe, 0x86, 0x55, 0x3b, 0xca, 0xd5, 0x89,
  7617. 0xc7, 0x54, 0x1a, 0x73, 0xac, 0xc3, 0x9a, 0xbd,
  7618. 0x53, 0xc4, 0x07, 0x76, 0xd8, 0xe8, 0x22, 0x61,
  7619. 0x9e, 0xa9, 0xad, 0x77, 0xa0, 0x13, 0x4c, 0xfc
  7620. };
  7621. WOLFSSL_SMALL_STACK_STATIC unsigned char c2[] = {
  7622. 0xa3, 0xc6, 0xf3, 0xf3, 0x82, 0x79, 0x5b, 0x10,
  7623. 0x87, 0xd7, 0x02, 0x50, 0xdb, 0x2c, 0xd3, 0xb1,
  7624. 0xa1, 0x62, 0xa8, 0xb6, 0xdc, 0x12, 0x60, 0x61,
  7625. 0xc1, 0x0a, 0x84, 0xa5, 0x85, 0x3f, 0x3a, 0x89,
  7626. 0xe6, 0x6c, 0xdb, 0xb7, 0x9a, 0xb4, 0x28, 0x9b,
  7627. 0xc3, 0xea, 0xd8, 0x10, 0xe9, 0xc0, 0xaf, 0x92
  7628. };
  7629. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7630. if ((aes = (XtsAes *)XMALLOC(sizeof *aes, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  7631. ERROR_OUT(-5515, out);
  7632. #endif
  7633. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  7634. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  7635. ret = EVP_test(EVP_aes_256_xts(), k2, i2, p2, sizeof(p2), c2, sizeof(c2));
  7636. if (ret != 0) {
  7637. printf("EVP_aes_256_xts failed\n");
  7638. goto out;
  7639. }
  7640. #endif
  7641. XMEMSET(buf, 0, sizeof(buf));
  7642. if (wc_AesXtsSetKey(aes, k2, sizeof(k2), AES_ENCRYPTION,
  7643. HEAP_HINT, devId) != 0)
  7644. ERROR_OUT(-5500, out);
  7645. else
  7646. aes_inited = 1;
  7647. ret = wc_AesXtsEncrypt(aes, buf, p2, sizeof(p2), i2, sizeof(i2));
  7648. #if defined(WOLFSSL_ASYNC_CRYPT)
  7649. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7650. #endif
  7651. if (ret != 0)
  7652. ERROR_OUT(-5501, out);
  7653. if (XMEMCMP(c2, buf, sizeof(c2)))
  7654. ERROR_OUT(-5502, out);
  7655. wc_AesXtsFree(aes);
  7656. XMEMSET(buf, 0, sizeof(buf));
  7657. if (wc_AesXtsSetKey(aes, k1, sizeof(k1), AES_ENCRYPTION,
  7658. HEAP_HINT, devId) != 0)
  7659. ERROR_OUT(-5503, out);
  7660. ret = wc_AesXtsEncrypt(aes, buf, p1, sizeof(p1), i1, sizeof(i1));
  7661. #if defined(WOLFSSL_ASYNC_CRYPT)
  7662. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7663. #endif
  7664. if (ret != 0)
  7665. ERROR_OUT(-5504, out);
  7666. if (XMEMCMP(c1, buf, AES_BLOCK_SIZE))
  7667. ERROR_OUT(-5505, out);
  7668. /* partial block encryption test */
  7669. XMEMSET(cipher, 0, sizeof(cipher));
  7670. ret = wc_AesXtsEncrypt(aes, cipher, pp, sizeof(pp), i1, sizeof(i1));
  7671. #if defined(WOLFSSL_ASYNC_CRYPT)
  7672. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7673. #endif
  7674. if (ret != 0)
  7675. ERROR_OUT(-5506, out);
  7676. wc_AesXtsFree(aes);
  7677. /* partial block decrypt test */
  7678. XMEMSET(buf, 0, sizeof(buf));
  7679. if (wc_AesXtsSetKey(aes, k1, sizeof(k1), AES_DECRYPTION,
  7680. HEAP_HINT, devId) != 0)
  7681. ERROR_OUT(-5507, out);
  7682. ret = wc_AesXtsDecrypt(aes, buf, cipher, sizeof(pp), i1, sizeof(i1));
  7683. #if defined(WOLFSSL_ASYNC_CRYPT)
  7684. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7685. #endif
  7686. if (ret != 0)
  7687. ERROR_OUT(-5508, out);
  7688. if (XMEMCMP(pp, buf, sizeof(pp)))
  7689. ERROR_OUT(-5509, out);
  7690. /* NIST decrypt test vector */
  7691. XMEMSET(buf, 0, sizeof(buf));
  7692. ret = wc_AesXtsDecrypt(aes, buf, c1, sizeof(c1), i1, sizeof(i1));
  7693. #if defined(WOLFSSL_ASYNC_CRYPT)
  7694. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7695. #endif
  7696. if (ret != 0)
  7697. ERROR_OUT(-5510, out);
  7698. if (XMEMCMP(p1, buf, AES_BLOCK_SIZE))
  7699. ERROR_OUT(-5511, out);
  7700. wc_AesXtsFree(aes);
  7701. XMEMSET(buf, 0, sizeof(buf));
  7702. if (wc_AesXtsSetKey(aes, k2, sizeof(k2), AES_DECRYPTION,
  7703. HEAP_HINT, devId) != 0)
  7704. ERROR_OUT(-5512, out);
  7705. ret = wc_AesXtsDecrypt(aes, buf, c2, sizeof(c2), i2, sizeof(i2));
  7706. #if defined(WOLFSSL_ASYNC_CRYPT)
  7707. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7708. #endif
  7709. if (ret != 0)
  7710. ERROR_OUT(-5513, out);
  7711. if (XMEMCMP(p2, buf, sizeof(p2)))
  7712. ERROR_OUT(-5514, out);
  7713. out:
  7714. if (aes_inited)
  7715. wc_AesXtsFree(aes);
  7716. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7717. if (aes)
  7718. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_AES);
  7719. #endif
  7720. return ret;
  7721. }
  7722. #endif /* WOLFSSL_AES_256 */
  7723. #if defined(WOLFSSL_AES_128) && defined(WOLFSSL_AES_256)
  7724. /* both 128 and 256 bit key test */
  7725. static int aes_xts_sector_test(void)
  7726. {
  7727. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7728. XtsAes *aes = NULL;
  7729. #else
  7730. XtsAes aes[1];
  7731. #endif
  7732. int aes_inited = 0;
  7733. int ret = 0;
  7734. unsigned char buf[AES_BLOCK_SIZE * 2];
  7735. /* 128 key tests */
  7736. WOLFSSL_SMALL_STACK_STATIC unsigned char k1[] = {
  7737. 0xa3, 0xe4, 0x0d, 0x5b, 0xd4, 0xb6, 0xbb, 0xed,
  7738. 0xb2, 0xd1, 0x8c, 0x70, 0x0a, 0xd2, 0xdb, 0x22,
  7739. 0x10, 0xc8, 0x11, 0x90, 0x64, 0x6d, 0x67, 0x3c,
  7740. 0xbc, 0xa5, 0x3f, 0x13, 0x3e, 0xab, 0x37, 0x3c
  7741. };
  7742. WOLFSSL_SMALL_STACK_STATIC unsigned char p1[] = {
  7743. 0x20, 0xe0, 0x71, 0x94, 0x05, 0x99, 0x3f, 0x09,
  7744. 0xa6, 0x6a, 0xe5, 0xbb, 0x50, 0x0e, 0x56, 0x2c
  7745. };
  7746. WOLFSSL_SMALL_STACK_STATIC unsigned char c1[] = {
  7747. 0x74, 0x62, 0x35, 0x51, 0x21, 0x02, 0x16, 0xac,
  7748. 0x92, 0x6b, 0x96, 0x50, 0xb6, 0xd3, 0xfa, 0x52
  7749. };
  7750. word64 s1 = 141;
  7751. /* 256 key tests */
  7752. WOLFSSL_SMALL_STACK_STATIC unsigned char k2[] = {
  7753. 0xef, 0x01, 0x0c, 0xa1, 0xa3, 0x66, 0x3e, 0x32,
  7754. 0x53, 0x43, 0x49, 0xbc, 0x0b, 0xae, 0x62, 0x23,
  7755. 0x2a, 0x15, 0x73, 0x34, 0x85, 0x68, 0xfb, 0x9e,
  7756. 0xf4, 0x17, 0x68, 0xa7, 0x67, 0x4f, 0x50, 0x7a,
  7757. 0x72, 0x7f, 0x98, 0x75, 0x53, 0x97, 0xd0, 0xe0,
  7758. 0xaa, 0x32, 0xf8, 0x30, 0x33, 0x8c, 0xc7, 0xa9,
  7759. 0x26, 0xc7, 0x73, 0xf0, 0x9e, 0x57, 0xb3, 0x57,
  7760. 0xcd, 0x15, 0x6a, 0xfb, 0xca, 0x46, 0xe1, 0xa0
  7761. };
  7762. WOLFSSL_SMALL_STACK_STATIC unsigned char p2[] = {
  7763. 0xed, 0x98, 0xe0, 0x17, 0x70, 0xa8, 0x53, 0xb4,
  7764. 0x9d, 0xb9, 0xe6, 0xaa, 0xf8, 0x8f, 0x0a, 0x41,
  7765. 0xb9, 0xb5, 0x6e, 0x91, 0xa5, 0xa2, 0xb1, 0x1d,
  7766. 0x40, 0x52, 0x92, 0x54, 0xf5, 0x52, 0x3e, 0x75
  7767. };
  7768. WOLFSSL_SMALL_STACK_STATIC unsigned char c2[] = {
  7769. 0xca, 0x20, 0xc5, 0x5e, 0x8d, 0xc1, 0x49, 0x68,
  7770. 0x7d, 0x25, 0x41, 0xde, 0x39, 0xc3, 0xdf, 0x63,
  7771. 0x00, 0xbb, 0x5a, 0x16, 0x3c, 0x10, 0xce, 0xd3,
  7772. 0x66, 0x6b, 0x13, 0x57, 0xdb, 0x8b, 0xd3, 0x9d
  7773. };
  7774. word64 s2 = 187;
  7775. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7776. if ((aes = (XtsAes *)XMALLOC(sizeof *aes, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  7777. ERROR_OUT(-5612, out);
  7778. #endif
  7779. XMEMSET(buf, 0, sizeof(buf));
  7780. if (wc_AesXtsSetKey(aes, k1, sizeof(k1), AES_ENCRYPTION,
  7781. HEAP_HINT, devId) != 0)
  7782. ERROR_OUT(-5600, out);
  7783. else
  7784. aes_inited = 1;
  7785. ret = wc_AesXtsEncryptSector(aes, buf, p1, sizeof(p1), s1);
  7786. #if defined(WOLFSSL_ASYNC_CRYPT)
  7787. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7788. #endif
  7789. if (ret != 0)
  7790. ERROR_OUT(-5601, out);
  7791. if (XMEMCMP(c1, buf, AES_BLOCK_SIZE))
  7792. ERROR_OUT(-5602, out);
  7793. wc_AesXtsFree(aes);
  7794. /* decrypt test */
  7795. XMEMSET(buf, 0, sizeof(buf));
  7796. if (wc_AesXtsSetKey(aes, k1, sizeof(k1), AES_DECRYPTION,
  7797. HEAP_HINT, devId) != 0)
  7798. ERROR_OUT(-5603, out);
  7799. ret = wc_AesXtsDecryptSector(aes, buf, c1, sizeof(c1), s1);
  7800. #if defined(WOLFSSL_ASYNC_CRYPT)
  7801. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7802. #endif
  7803. if (ret != 0)
  7804. ERROR_OUT(-5604, out);
  7805. if (XMEMCMP(p1, buf, AES_BLOCK_SIZE))
  7806. ERROR_OUT(-5605, out);
  7807. wc_AesXtsFree(aes);
  7808. /* 256 bit key tests */
  7809. XMEMSET(buf, 0, sizeof(buf));
  7810. if (wc_AesXtsSetKey(aes, k2, sizeof(k2), AES_ENCRYPTION,
  7811. HEAP_HINT, devId) != 0)
  7812. ERROR_OUT(-5606, out);
  7813. ret = wc_AesXtsEncryptSector(aes, buf, p2, sizeof(p2), s2);
  7814. #if defined(WOLFSSL_ASYNC_CRYPT)
  7815. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7816. #endif
  7817. if (ret != 0)
  7818. ERROR_OUT(-5607, out);
  7819. if (XMEMCMP(c2, buf, sizeof(c2)))
  7820. ERROR_OUT(-5608, out);
  7821. wc_AesXtsFree(aes);
  7822. /* decrypt test */
  7823. XMEMSET(buf, 0, sizeof(buf));
  7824. if (wc_AesXtsSetKey(aes, k2, sizeof(k2), AES_DECRYPTION,
  7825. HEAP_HINT, devId) != 0)
  7826. ERROR_OUT(-5609, out);
  7827. ret = wc_AesXtsDecryptSector(aes, buf, c2, sizeof(c2), s2);
  7828. #if defined(WOLFSSL_ASYNC_CRYPT)
  7829. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7830. #endif
  7831. if (ret != 0)
  7832. ERROR_OUT(-5610, out);
  7833. if (XMEMCMP(p2, buf, sizeof(p2)))
  7834. ERROR_OUT(-5611, out);
  7835. out:
  7836. if (aes_inited)
  7837. wc_AesXtsFree(aes);
  7838. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7839. if (aes)
  7840. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_AES);
  7841. #endif
  7842. return ret;
  7843. }
  7844. #endif /* WOLFSSL_AES_128 && WOLFSSL_AES_256 */
  7845. #ifdef WOLFSSL_AES_128
  7846. /* testing of bad arguments */
  7847. static int aes_xts_args_test(void)
  7848. {
  7849. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7850. XtsAes *aes = NULL;
  7851. #else
  7852. XtsAes aes[1];
  7853. #endif
  7854. int aes_inited = 0;
  7855. int ret;
  7856. unsigned char buf[AES_BLOCK_SIZE * 2];
  7857. /* 128 key tests */
  7858. WOLFSSL_SMALL_STACK_STATIC unsigned char k1[] = {
  7859. 0xa3, 0xe4, 0x0d, 0x5b, 0xd4, 0xb6, 0xbb, 0xed,
  7860. 0xb2, 0xd1, 0x8c, 0x70, 0x0a, 0xd2, 0xdb, 0x22,
  7861. 0x10, 0xc8, 0x11, 0x90, 0x64, 0x6d, 0x67, 0x3c,
  7862. 0xbc, 0xa5, 0x3f, 0x13, 0x3e, 0xab, 0x37, 0x3c
  7863. };
  7864. WOLFSSL_SMALL_STACK_STATIC unsigned char p1[] = {
  7865. 0x20, 0xe0, 0x71, 0x94, 0x05, 0x99, 0x3f, 0x09,
  7866. 0xa6, 0x6a, 0xe5, 0xbb, 0x50, 0x0e, 0x56, 0x2c
  7867. };
  7868. WOLFSSL_SMALL_STACK_STATIC unsigned char c1[] = {
  7869. 0x74, 0x62, 0x35, 0x51, 0x21, 0x02, 0x16, 0xac,
  7870. 0x92, 0x6b, 0x96, 0x50, 0xb6, 0xd3, 0xfa, 0x52
  7871. };
  7872. word64 s1 = 141;
  7873. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7874. if ((aes = (XtsAes *)XMALLOC(sizeof *aes, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  7875. ERROR_OUT(-5708, out);
  7876. #endif
  7877. if (wc_AesXtsSetKey(NULL, k1, sizeof(k1), AES_ENCRYPTION,
  7878. HEAP_HINT, devId) == 0)
  7879. ERROR_OUT(-5700, out);
  7880. if (wc_AesXtsSetKey(aes, NULL, sizeof(k1), AES_ENCRYPTION,
  7881. HEAP_HINT, devId) == 0)
  7882. ERROR_OUT(-5701, out);
  7883. /* encryption operations */
  7884. if (wc_AesXtsSetKey(aes, k1, sizeof(k1), AES_ENCRYPTION,
  7885. HEAP_HINT, devId) != 0)
  7886. ERROR_OUT(-5702, out);
  7887. else
  7888. aes_inited = 1;
  7889. ret = wc_AesXtsEncryptSector(NULL, buf, p1, sizeof(p1), 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(-5703, out);
  7895. ret = wc_AesXtsEncryptSector(aes, NULL, p1, sizeof(p1), 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(-5704, out);
  7901. wc_AesXtsFree(aes);
  7902. /* decryption operations */
  7903. if (wc_AesXtsSetKey(aes, k1, sizeof(k1), AES_DECRYPTION,
  7904. HEAP_HINT, devId) != 0)
  7905. ERROR_OUT(-5705, out);
  7906. ret = wc_AesXtsDecryptSector(NULL, buf, c1, sizeof(c1), s1);
  7907. #if defined(WOLFSSL_ASYNC_CRYPT)
  7908. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7909. #endif
  7910. if (ret == 0)
  7911. ERROR_OUT(-5706, out);
  7912. ret = wc_AesXtsDecryptSector(aes, NULL, c1, sizeof(c1), s1);
  7913. #if defined(WOLFSSL_ASYNC_CRYPT)
  7914. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7915. #endif
  7916. if (ret == 0)
  7917. ERROR_OUT(-5707, out);
  7918. ret = 0;
  7919. out:
  7920. if (aes_inited)
  7921. wc_AesXtsFree(aes);
  7922. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7923. if (aes)
  7924. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_AES);
  7925. #endif
  7926. return ret;
  7927. }
  7928. #endif /* WOLFSSL_AES_128 */
  7929. #endif /* WOLFSSL_AES_XTS */
  7930. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  7931. static int aes_cbc_test(void)
  7932. {
  7933. byte cipher[AES_BLOCK_SIZE];
  7934. byte plain[AES_BLOCK_SIZE];
  7935. int ret;
  7936. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = { /* "Now is the time for all " w/o trailing 0 */
  7937. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  7938. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  7939. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  7940. };
  7941. byte key[] = "0123456789abcdef "; /* align */
  7942. byte iv[] = "1234567890abcdef "; /* align */
  7943. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  7944. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  7945. /* Parameter Validation testing. */
  7946. ret = wc_AesCbcEncryptWithKey(cipher, msg, AES_BLOCK_SIZE, key, 17, NULL);
  7947. if (ret != BAD_FUNC_ARG)
  7948. return -5800;
  7949. #ifdef HAVE_AES_DECRYPT
  7950. ret = wc_AesCbcDecryptWithKey(plain, cipher, AES_BLOCK_SIZE, key, 17, NULL);
  7951. if (ret != BAD_FUNC_ARG)
  7952. return -5801;
  7953. #endif
  7954. ret = wc_AesCbcEncryptWithKey(cipher, msg, AES_BLOCK_SIZE, key,
  7955. AES_BLOCK_SIZE, iv);
  7956. if (ret != 0)
  7957. return -5802;
  7958. #ifdef HAVE_AES_DECRYPT
  7959. ret = wc_AesCbcDecryptWithKey(plain, cipher, AES_BLOCK_SIZE, key,
  7960. AES_BLOCK_SIZE, iv);
  7961. if (ret != 0)
  7962. return -5803;
  7963. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE) != 0)
  7964. return -5804;
  7965. #endif /* HAVE_AES_DECRYPT */
  7966. (void)plain;
  7967. return 0;
  7968. }
  7969. #endif
  7970. #if defined(HAVE_AES_ECB) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  7971. static int aesecb_test(void)
  7972. {
  7973. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7974. Aes *enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  7975. #else
  7976. Aes enc[1];
  7977. #endif
  7978. byte cipher[AES_BLOCK_SIZE * 4];
  7979. #ifdef HAVE_AES_DECRYPT
  7980. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7981. Aes *dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  7982. #else
  7983. Aes dec[1];
  7984. #endif
  7985. byte plain [AES_BLOCK_SIZE * 4];
  7986. #endif /* HAVE_AES_DECRYPT */
  7987. int ret = 0;
  7988. #if defined(WOLFSSL_AES_256)
  7989. {
  7990. WOLFSSL_SMALL_STACK_STATIC const byte niPlain[] =
  7991. {
  7992. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  7993. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  7994. };
  7995. WOLFSSL_SMALL_STACK_STATIC const byte niCipher[] =
  7996. {
  7997. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  7998. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  7999. };
  8000. WOLFSSL_SMALL_STACK_STATIC const byte niKey[] =
  8001. {
  8002. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  8003. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  8004. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  8005. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  8006. };
  8007. if (wc_AesInit(enc, HEAP_HINT, devId) != 0)
  8008. ERROR_OUT(-5900, out);
  8009. #if defined(HAVE_AES_DECRYPT)
  8010. if (wc_AesInit(dec, HEAP_HINT, devId) != 0)
  8011. ERROR_OUT(-5901, out);
  8012. #endif
  8013. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  8014. ret = wc_AesSetKey(enc, niKey, sizeof(niKey), cipher, AES_ENCRYPTION);
  8015. if (ret != 0)
  8016. ERROR_OUT(-5923, out);
  8017. if (wc_AesEcbEncrypt(enc, cipher, niPlain, AES_BLOCK_SIZE) != 0)
  8018. ERROR_OUT(-5924, out);
  8019. if (XMEMCMP(cipher, niCipher, AES_BLOCK_SIZE) != 0)
  8020. ERROR_OUT(-5925, out);
  8021. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  8022. ret = wc_AesSetKey(dec, niKey, sizeof(niKey), plain, AES_DECRYPTION);
  8023. if (ret != 0)
  8024. ERROR_OUT(-5926, out);
  8025. if (wc_AesEcbDecrypt(dec, plain, niCipher, AES_BLOCK_SIZE) != 0)
  8026. ERROR_OUT(-5927, out);
  8027. wc_AesEcbDecrypt(dec, plain, niCipher, AES_BLOCK_SIZE);
  8028. if (XMEMCMP(plain, niPlain, AES_BLOCK_SIZE) != 0)
  8029. ERROR_OUT(-5928, out);
  8030. }
  8031. wc_AesFree(enc);
  8032. #ifdef HAVE_AES_DECRYPT
  8033. wc_AesFree(dec);
  8034. #endif
  8035. out:
  8036. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8037. if (enc)
  8038. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  8039. #ifdef HAVE_AES_DECRYPT
  8040. if (dec)
  8041. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  8042. #endif
  8043. #endif
  8044. #endif /* WOLFSSL_AES_256 */
  8045. return ret;
  8046. }
  8047. #endif /* HAVE_AES_ECB */
  8048. #ifdef WOLFSSL_AES_COUNTER
  8049. static int aesctr_test(Aes* enc, Aes* dec, byte* cipher, byte* plain)
  8050. {
  8051. int ret;
  8052. /* test vectors from "Recommendation for Block Cipher Modes of
  8053. * Operation" NIST Special Publication 800-38A */
  8054. WOLFSSL_SMALL_STACK_STATIC const byte ctrIv[] =
  8055. {
  8056. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  8057. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  8058. };
  8059. WOLFSSL_SMALL_STACK_STATIC const byte ctrPlain[] =
  8060. {
  8061. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  8062. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  8063. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  8064. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  8065. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  8066. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  8067. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  8068. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  8069. };
  8070. #ifdef WOLFSSL_ARMASM
  8071. WOLFSSL_SMALL_STACK_STATIC const byte ctrIvWrap32[] =
  8072. {
  8073. 0xff,0xff,0xff,0xff,0x0f,0xff,0xff,0xff,
  8074. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff
  8075. };
  8076. WOLFSSL_SMALL_STACK_STATIC const byte ctrIvWrap32_2[] =
  8077. {
  8078. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  8079. 0xff,0xff,0xff,0xff,0x0f,0xff,0xff,0xfe
  8080. };
  8081. WOLFSSL_SMALL_STACK_STATIC const byte ctrIvWrap64[] =
  8082. {
  8083. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  8084. 0x0f,0xff,0xff,0xff,0xff,0xff,0xff,0xff
  8085. };
  8086. WOLFSSL_SMALL_STACK_STATIC const byte ctrIvWrap64_2[] =
  8087. {
  8088. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xf0,
  8089. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfe
  8090. };
  8091. WOLFSSL_SMALL_STACK_STATIC const byte ctrIvWrap96[] =
  8092. {
  8093. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  8094. 0xff,0xff,0xff,0xff,0x0f,0xff,0xff,0xff
  8095. };
  8096. WOLFSSL_SMALL_STACK_STATIC const byte ctrIvWrap96_2[] =
  8097. {
  8098. 0xff,0xff,0xff,0xf0,0xff,0xff,0xff,0xff,
  8099. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfe
  8100. };
  8101. #endif
  8102. WOLFSSL_SMALL_STACK_STATIC const byte ctrIvWrap128[] =
  8103. {
  8104. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  8105. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff
  8106. };
  8107. #ifdef WOLFSSL_ARMASM
  8108. WOLFSSL_SMALL_STACK_STATIC const byte ctrIvWrap128_2[] =
  8109. {
  8110. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  8111. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfe
  8112. };
  8113. #endif
  8114. #ifdef WOLFSSL_AES_128
  8115. WOLFSSL_SMALL_STACK_STATIC const byte oddCipher[] =
  8116. {
  8117. 0xb9,0xd7,0xcb,0x08,0xb0,0xe1,0x7b,0xa0,
  8118. 0xc2
  8119. };
  8120. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Key[] =
  8121. {
  8122. 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,
  8123. 0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c
  8124. };
  8125. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Cipher[] =
  8126. {
  8127. 0x87,0x4d,0x61,0x91,0xb6,0x20,0xe3,0x26,
  8128. 0x1b,0xef,0x68,0x64,0x99,0x0d,0xb6,0xce,
  8129. 0x98,0x06,0xf6,0x6b,0x79,0x70,0xfd,0xff,
  8130. 0x86,0x17,0x18,0x7b,0xb9,0xff,0xfd,0xff,
  8131. 0x5a,0xe4,0xdf,0x3e,0xdb,0xd5,0xd3,0x5e,
  8132. 0x5b,0x4f,0x09,0x02,0x0d,0xb0,0x3e,0xab,
  8133. 0x1e,0x03,0x1d,0xda,0x2f,0xbe,0x03,0xd1,
  8134. 0x79,0x21,0x70,0xa0,0xf3,0x00,0x9c,0xee
  8135. };
  8136. #ifdef WOLFSSL_ARMASM
  8137. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap32Cipher[] =
  8138. {
  8139. 0xb3,0x8b,0x58,0xbc,0xce,0xf4,0x71,0x78,
  8140. 0xf6,0x7c,0xdb,0xb4,0x27,0x2b,0x0a,0xbf,
  8141. 0x7e,0xad,0xea,0x5c,0xd1
  8142. };
  8143. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap32CipherLong[] =
  8144. {
  8145. 0xb3,0x8b,0x58,0xbc,0xce,0xf4,0x71,0x78,
  8146. 0xf6,0x7c,0xdb,0xb4,0x27,0x2b,0x0a,0xbf,
  8147. 0x7e,0xad,0xea,0x5c,0xd1,0xb7,0x98,0xf0,
  8148. 0x22,0x20,0xfe,0x67,0xb0,0x02,0x23,0x50
  8149. };
  8150. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap32_2CipherLong[] =
  8151. {
  8152. 0x6e,0xa1,0x27,0x4d,0xea,0x20,0x5f,0x39,
  8153. 0x68,0xc8,0xb6,0x78,0xde,0xfc,0x53,0x5c,
  8154. 0x90,0xc8,0xf6,0xc6,0xfa,0xe0,0x7b,0x09,
  8155. 0x7c,0xf8,0x9c,0x6a,0x5a,0xa5,0x17,0x7f,
  8156. 0x03,0x92,0x5f,0x4e,0x85,0xea,0x26,0xc9,
  8157. 0x5a,0xc2,0x74,0xe2,0xbf,0xe4,0x1b,0xd4
  8158. };
  8159. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap64Cipher[] =
  8160. {
  8161. 0xdd,0x17,0x10,0x7c,0x45,0x04,0xac,0x43,
  8162. 0xef,0xa8,0xcc,0x32,0x34,0x87,0x88,0xd7,
  8163. 0xae,0x74,0x94,0x72,0x8e
  8164. };
  8165. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap64CipherLong[] =
  8166. {
  8167. 0xdd,0x17,0x10,0x7c,0x45,0x04,0xac,0x43,
  8168. 0xef,0xa8,0xcc,0x32,0x34,0x87,0x88,0xd7,
  8169. 0xae,0x74,0x94,0x72,0x8e,0xd0,0x71,0xc0,
  8170. 0x89,0x8a,0xa1,0xb0,0x29,0xa0,0x10,0x9e
  8171. };
  8172. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap64_2CipherLong[] =
  8173. {
  8174. 0x3f,0xe7,0xd5,0xf3,0xfa,0x09,0xfe,0x40,
  8175. 0xa6,0xa1,0x32,0x8b,0x57,0x12,0xb9,0xfa,
  8176. 0xf2,0x2d,0xe4,0x3c,0x66,0x1d,0x0a,0x8e,
  8177. 0x46,0xf8,0x2e,0x33,0xce,0x8d,0x4e,0x3b,
  8178. 0x17,0x67,0x9e,0x9f,0x76,0x9e,0xc2,0x99,
  8179. 0xd5,0xd4,0x71,0xed,0xb4,0x33,0xb2,0xcd
  8180. };
  8181. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap96Cipher[] =
  8182. {
  8183. 0x55,0x24,0xc2,0x73,0xca,0xa3,0x48,0x03,
  8184. 0x0b,0x72,0x8d,0xd7,0x6c,0x99,0x8e,0x04,
  8185. 0x9d,0x77,0xc9,0x5f,0x38
  8186. };
  8187. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap96CipherLong[] =
  8188. {
  8189. 0x55,0x24,0xc2,0x73,0xca,0xa3,0x48,0x03,
  8190. 0x0b,0x72,0x8d,0xd7,0x6c,0x99,0x8e,0x04,
  8191. 0x9d,0x77,0xc9,0x5f,0x38,0xb5,0x6e,0x44,
  8192. 0x21,0x8e,0xda,0x57,0xe0,0x41,0xc7,0x6a
  8193. };
  8194. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap96_2CipherLong[] =
  8195. {
  8196. 0xc8,0x81,0x1a,0xbe,0xc7,0x5b,0x93,0x6f,
  8197. 0xe6,0x52,0xe4,0xb1,0x2d,0x1c,0x39,0xbc,
  8198. 0xeb,0x82,0x27,0x0a,0x7e,0xa5,0x0e,0x2d,
  8199. 0x32,0xda,0xbe,0x10,0x7a,0x10,0xcc,0xd3,
  8200. 0x6f,0xc6,0x83,0x28,0x05,0x57,0x8a,0x24,
  8201. 0x44,0x76,0x17,0x81,0xb9,0x5c,0x94,0x81
  8202. };
  8203. #endif
  8204. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap128Cipher[] =
  8205. {
  8206. 0xe1,0x33,0x38,0xe3,0x6c,0xb7,0x19,0x62,
  8207. 0xe0,0x0d,0x02,0x0b,0x4c,0xed,0xbd,0x86,
  8208. 0xd3,0xda,0xe1,0x5b,0x04
  8209. };
  8210. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap128CipherLong[] =
  8211. {
  8212. 0xe1,0x33,0x38,0xe3,0x6c,0xb7,0x19,0x62,
  8213. 0xe0,0x0d,0x02,0x0b,0x4c,0xed,0xbd,0x86,
  8214. 0xd3,0xda,0xe1,0x5b,0x04,0xbb,0x35,0x2f,
  8215. 0xa0,0xf5,0x9f,0xeb,0xfc,0xb4,0xda,0x3e
  8216. };
  8217. #ifdef WOLFSSL_ARMASM
  8218. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap128_2CipherLong[] =
  8219. {
  8220. 0xba,0x76,0xaa,0x54,0xd5,0xb5,0x60,0x67,
  8221. 0xc1,0xa7,0x90,0x3b,0x3f,0xdd,0xfa,0x89,
  8222. 0x24,0xdf,0x0c,0x56,0x5c,0xf4,0x2a,0x68,
  8223. 0x97,0x87,0x13,0xb6,0x7a,0xd1,0x24,0xfd,
  8224. 0x4d,0x3f,0x77,0x4a,0xb9,0xe4,0x7d,0xa2,
  8225. 0xdb,0xb9,0x31,0x5e,0xa3,0x11,0x06,0x80
  8226. };
  8227. #endif
  8228. #endif /* WOLFSSL_AES_128 */
  8229. #ifdef WOLFSSL_AES_192
  8230. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Key[] =
  8231. {
  8232. 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  8233. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  8234. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b
  8235. };
  8236. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Cipher[] =
  8237. {
  8238. 0x1a,0xbc,0x93,0x24,0x17,0x52,0x1c,0xa2,
  8239. 0x4f,0x2b,0x04,0x59,0xfe,0x7e,0x6e,0x0b,
  8240. 0x09,0x03,0x39,0xec,0x0a,0xa6,0xfa,0xef,
  8241. 0xd5,0xcc,0xc2,0xc6,0xf4,0xce,0x8e,0x94,
  8242. 0x1e,0x36,0xb2,0x6b,0xd1,0xeb,0xc6,0x70,
  8243. 0xd1,0xbd,0x1d,0x66,0x56,0x20,0xab,0xf7,
  8244. 0x4f,0x78,0xa7,0xf6,0xd2,0x98,0x09,0x58,
  8245. 0x5a,0x97,0xda,0xec,0x58,0xc6,0xb0,0x50
  8246. };
  8247. #ifdef WOLFSSL_ARMASM
  8248. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap32Cipher[] =
  8249. {
  8250. 0x28,0xaa,0xfa,0x90,0x72,0x74,0x86,0xaf,
  8251. 0x72,0x73,0x35,0x17,0x70,0x4e,0x7d,0xca,
  8252. 0x0c,0x33,0x97,0x06,0xc0
  8253. };
  8254. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap32CipherLong[] =
  8255. {
  8256. 0x28,0xaa,0xfa,0x90,0x72,0x74,0x86,0xaf,
  8257. 0x72,0x73,0x35,0x17,0x70,0x4e,0x7d,0xca,
  8258. 0x0c,0x33,0x97,0x06,0xc0,0xbe,0x83,0x87,
  8259. 0xdd,0xd3,0xff,0xd8,0xe4,0x6a,0x5b,0x84
  8260. };
  8261. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap32_2CipherLong[] =
  8262. {
  8263. 0xf5,0x00,0xa2,0x91,0x54,0xa3,0x76,0xa2,
  8264. 0xdd,0xad,0x16,0x89,0xe5,0xf0,0x1d,0x40,
  8265. 0x84,0xcd,0x74,0x84,0xcb,0x8b,0x9e,0x29,
  8266. 0xa9,0x8a,0x12,0x65,0xa0,0x79,0x5e,0xce,
  8267. 0xd9,0x50,0x65,0x21,0x86,0xb0,0x85,0x0d,
  8268. 0x98,0x2d,0x9a,0x5a,0x11,0xbe,0xa5,0x7f
  8269. };
  8270. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap64Cipher[] =
  8271. {
  8272. 0xfe,0x39,0x27,0x97,0xac,0xe5,0xb8,0x74,
  8273. 0xb9,0x8c,0xbf,0x58,0x71,0xa4,0x80,0x33,
  8274. 0x3d,0xf7,0xb4,0xfd,0x8c
  8275. };
  8276. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap64CipherLong[] =
  8277. {
  8278. 0xfe,0x39,0x27,0x97,0xac,0xe5,0xb8,0x74,
  8279. 0xb9,0x8c,0xbf,0x58,0x71,0xa4,0x80,0x33,
  8280. 0x3d,0xf7,0xb4,0xfd,0x8c,0x55,0x47,0x10,
  8281. 0xd5,0x91,0x35,0xbe,0xd8,0x0d,0xa5,0x9e
  8282. };
  8283. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap64_2CipherLong[] =
  8284. {
  8285. 0x59,0xf1,0xed,0x70,0x62,0x42,0xa8,0x06,
  8286. 0x07,0x36,0xe1,0xc5,0x04,0x79,0xc3,0x9b,
  8287. 0xd1,0x14,0x5c,0xcc,0x6f,0x81,0x5f,0x2f,
  8288. 0xa0,0xde,0xcf,0x61,0x55,0x18,0x7a,0xac,
  8289. 0xb0,0x59,0x37,0x90,0x53,0xb3,0x00,0x88,
  8290. 0xb4,0x49,0x90,0x7b,0x96,0xcd,0xcc,0xc3
  8291. };
  8292. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap96Cipher[] =
  8293. {
  8294. 0x41,0x21,0x40,0x31,0xfb,0xc8,0xad,0x23,
  8295. 0xde,0x00,0x03,0xd8,0x96,0x45,0xc7,0xb5,
  8296. 0x47,0xb5,0xf3,0x30,0x3b
  8297. };
  8298. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap96CipherLong[] =
  8299. {
  8300. 0x41,0x21,0x40,0x31,0xfb,0xc8,0xad,0x23,
  8301. 0xde,0x00,0x03,0xd8,0x96,0x45,0xc7,0xb5,
  8302. 0x47,0xb5,0xf3,0x30,0x3b,0xef,0xcd,0x80,
  8303. 0xe3,0x61,0x34,0xef,0x4e,0x1b,0x79,0xc1
  8304. };
  8305. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap96_2CipherLong[] =
  8306. {
  8307. 0x3c,0xb2,0xff,0xc0,0x24,0xe1,0xf5,0xc4,
  8308. 0x0f,0xd1,0x0a,0x1b,0xbe,0x1f,0x23,0xa1,
  8309. 0x8e,0xbf,0x2b,0x96,0xb6,0x37,0xc8,0x25,
  8310. 0x06,0x90,0xe2,0xca,0x71,0x24,0x52,0x95,
  8311. 0xaa,0x8c,0x80,0xdf,0xb7,0xd7,0x30,0xb0,
  8312. 0xcc,0x06,0x4f,0x28,0xa2,0x74,0x27,0xf8
  8313. };
  8314. #endif
  8315. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap128Cipher[] =
  8316. {
  8317. 0x5c,0xc3,0x8f,0xab,0x30,0xb6,0xac,0x67,
  8318. 0xdc,0xc2,0x1e,0x7b,0x01,0x2e,0xcf,0x98,
  8319. 0x8c,0x68,0xa7,0xd9,0x57
  8320. };
  8321. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap128CipherLong[] =
  8322. {
  8323. 0x5c,0xc3,0x8f,0xab,0x30,0xb6,0xac,0x67,
  8324. 0xdc,0xc2,0x1e,0x7b,0x01,0x2e,0xcf,0x98,
  8325. 0x8c,0x68,0xa7,0xd9,0x57,0xab,0x09,0x0f,
  8326. 0x01,0xc4,0x4e,0x62,0xaf,0xc2,0xdf,0x1a
  8327. };
  8328. #ifdef WOLFSSL_ARMASM
  8329. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap128_2CipherLong[] =
  8330. {
  8331. 0x88,0x0a,0x26,0x4e,0xa8,0x26,0x21,0xe0,
  8332. 0xfc,0xbc,0x63,0xdc,0xd9,0x60,0x52,0xb2,
  8333. 0x99,0x2f,0xbb,0x1e,0x00,0xf5,0x9f,0x6d,
  8334. 0xab,0x48,0x0f,0xc6,0x37,0x12,0x56,0xe3,
  8335. 0x12,0x8d,0x31,0xc8,0xea,0xf4,0x41,0x82,
  8336. 0x7a,0x88,0xe0,0xd7,0xf0,0x67,0x03,0xa4
  8337. };
  8338. #endif
  8339. #endif
  8340. #ifdef WOLFSSL_AES_256
  8341. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Key[] =
  8342. {
  8343. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  8344. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  8345. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  8346. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  8347. };
  8348. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Cipher[] =
  8349. {
  8350. 0x60,0x1e,0xc3,0x13,0x77,0x57,0x89,0xa5,
  8351. 0xb7,0xa7,0xf5,0x04,0xbb,0xf3,0xd2,0x28,
  8352. 0xf4,0x43,0xe3,0xca,0x4d,0x62,0xb5,0x9a,
  8353. 0xca,0x84,0xe9,0x90,0xca,0xca,0xf5,0xc5,
  8354. 0x2b,0x09,0x30,0xda,0xa2,0x3d,0xe9,0x4c,
  8355. 0xe8,0x70,0x17,0xba,0x2d,0x84,0x98,0x8d,
  8356. 0xdf,0xc9,0xc5,0x8d,0xb6,0x7a,0xad,0xa6,
  8357. 0x13,0xc2,0xdd,0x08,0x45,0x79,0x41,0xa6
  8358. };
  8359. #ifdef WOLFSSL_ARMASM
  8360. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap32Cipher[] =
  8361. {
  8362. 0xb0,0xa8,0xc0,0x65,0x85,0x20,0x0d,0x5c,
  8363. 0x25,0xcf,0xe7,0x58,0x63,0xc8,0xd4,0xea,
  8364. 0xa2,0x13,0x47,0x74,0xda
  8365. };
  8366. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap32CipherLong[] =
  8367. {
  8368. 0xb0,0xa8,0xc0,0x65,0x85,0x20,0x0d,0x5c,
  8369. 0x25,0xcf,0xe7,0x58,0x63,0xc8,0xd4,0xea,
  8370. 0xa2,0x13,0x47,0x74,0xda,0x89,0x77,0x40,
  8371. 0x28,0x9c,0xe8,0x19,0x26,0x32,0xd8,0x1f
  8372. };
  8373. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap32_2CipherLong[] =
  8374. {
  8375. 0xf6,0xd9,0x22,0xc6,0x80,0x29,0xaf,0x14,
  8376. 0x54,0x6c,0x0a,0xce,0x42,0xea,0x3c,0xa1,
  8377. 0x7c,0xeb,0x36,0x0d,0x8e,0xd7,0x8c,0x59,
  8378. 0xa8,0x09,0x9f,0x9e,0xba,0x5b,0x95,0xfa,
  8379. 0x26,0x8c,0x37,0x59,0xf8,0xae,0x8e,0xaa,
  8380. 0x4d,0xe4,0x1c,0xfe,0x51,0xc7,0xb7,0xcc
  8381. };
  8382. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap64Cipher[] =
  8383. {
  8384. 0x74,0x1a,0x52,0x41,0x76,0xb4,0x11,0x8f,
  8385. 0xfd,0x57,0x31,0xfd,0x3d,0x76,0x8f,0x07,
  8386. 0xd4,0x94,0x4c,0xcd,0x4d
  8387. };
  8388. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap64CipherLong[] =
  8389. {
  8390. 0x74,0x1a,0x52,0x41,0x76,0xb4,0x11,0x8f,
  8391. 0xfd,0x57,0x31,0xfd,0x3d,0x76,0x8f,0x07,
  8392. 0xd4,0x94,0x4c,0xcd,0x4d,0x47,0x5a,0x92,
  8393. 0x26,0x49,0x81,0x7a,0xda,0x36,0x27,0x01
  8394. };
  8395. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap64_2CipherLong[] =
  8396. {
  8397. 0xf7,0x9c,0xbf,0xf6,0xa2,0xaa,0x8a,0x0a,
  8398. 0x63,0x8a,0x20,0x2f,0x12,0xf1,0x8e,0x49,
  8399. 0x30,0xc0,0x8d,0x5c,0x5f,0x8b,0xbc,0x16,
  8400. 0xdd,0x71,0xee,0x13,0x14,0x7b,0xe1,0x25,
  8401. 0xcb,0x87,0x8a,0xc6,0xdc,0x1d,0x54,0x7a,
  8402. 0xe1,0xe4,0x6f,0x0d,0x95,0x1b,0xd1,0x8b
  8403. };
  8404. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap96Cipher[] =
  8405. {
  8406. 0xb9,0x07,0x02,0xb8,0xbe,0x94,0xbf,0x53,
  8407. 0xdf,0x83,0x8e,0x23,0x8c,0x67,0x0c,0x81,
  8408. 0xb8,0x69,0xa1,0x48,0x45
  8409. };
  8410. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap96CipherLong[] =
  8411. {
  8412. 0xb9,0x07,0x02,0xb8,0xbe,0x94,0xbf,0x53,
  8413. 0xdf,0x83,0x8e,0x23,0x8c,0x67,0x0c,0x81,
  8414. 0xb8,0x69,0xa1,0x48,0x45,0xf1,0xc6,0x27,
  8415. 0x36,0xa8,0xb2,0x4b,0x0e,0x62,0x6b,0x72
  8416. };
  8417. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap96_2CipherLong[] =
  8418. {
  8419. 0xd5,0x56,0x73,0xaa,0xb8,0xe4,0x06,0xf6,
  8420. 0x83,0x45,0x3a,0xb4,0xb9,0x63,0xec,0xad,
  8421. 0x73,0xc5,0xab,0x78,0xb1,0x21,0xab,0xef,
  8422. 0x69,0x15,0xb7,0x0c,0xe9,0xb4,0x3a,0xe7,
  8423. 0xbc,0xc4,0x22,0xbd,0x93,0xba,0x52,0xe0,
  8424. 0x91,0x2f,0x5e,0x8d,0x6d,0x59,0xf7,0xc2
  8425. };
  8426. #endif
  8427. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap128Cipher[] =
  8428. {
  8429. 0x50,0xfd,0x97,0xc3,0xe6,0x1a,0xbb,0x48,
  8430. 0x73,0xfb,0x78,0xdf,0x1e,0x8e,0x77,0xe6,
  8431. 0x4b,0x45,0x7c,0xd6,0x8a
  8432. };
  8433. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap128CipherLong[] =
  8434. {
  8435. 0x50,0xfd,0x97,0xc3,0xe6,0x1a,0xbb,0x48,
  8436. 0x73,0xfb,0x78,0xdf,0x1e,0x8e,0x77,0xe6,
  8437. 0x4b,0x45,0x7c,0xd6,0x8a,0xcc,0xda,0x4a,
  8438. 0x89,0xfa,0x23,0x6c,0x06,0xbf,0x26,0x05
  8439. };
  8440. #ifdef WOLFSSL_ARMASM
  8441. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap128_2CipherLong[] =
  8442. {
  8443. 0x24,0x5c,0x09,0xa0,0x3b,0x1a,0x5a,0x94,
  8444. 0x2b,0x93,0x56,0x13,0x48,0xa0,0x21,0xce,
  8445. 0x95,0x11,0xa3,0x76,0xd6,0x59,0x88,0x42,
  8446. 0x04,0x71,0x69,0x62,0x28,0xb2,0xee,0x9d,
  8447. 0xd5,0xa0,0xea,0xc7,0x37,0x93,0x92,0xc7,
  8448. 0xf2,0xb6,0x8d,0xd9,0x59,0x1a,0xfa,0xbb
  8449. };
  8450. #endif
  8451. #endif
  8452. int i;
  8453. struct {
  8454. const byte* key;
  8455. int keySz;
  8456. const byte* iv;
  8457. const byte* plain;
  8458. int len;
  8459. const byte* cipher;
  8460. } testVec[] = {
  8461. #ifdef WOLFSSL_AES_128
  8462. { ctr128Key, (int)sizeof(ctr128Key), ctrIv,
  8463. ctrPlain, (int)sizeof(ctrPlain), ctr128Cipher },
  8464. /* let's try with just 9 bytes, non block size test */
  8465. { ctr128Key, (int)sizeof(ctr128Key), ctrIv,
  8466. ctrPlain, (int)sizeof(oddCipher), ctr128Cipher },
  8467. /* and an additional 9 bytes to reuse tmp left buffer */
  8468. { NULL, 0, NULL, ctrPlain, (int)sizeof(oddCipher), oddCipher },
  8469. /* Counter wrapping */
  8470. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap128,
  8471. ctrPlain, (int)sizeof(ctr128Wrap128Cipher), ctr128Wrap128Cipher },
  8472. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap128,
  8473. ctrPlain, (int)sizeof(ctr128Wrap128CipherLong),
  8474. ctr128Wrap128CipherLong },
  8475. #ifdef WOLFSSL_ARMASM
  8476. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap128_2,
  8477. ctrPlain, (int)sizeof(ctr128Wrap128_2CipherLong),
  8478. ctr128Wrap128_2CipherLong },
  8479. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap96,
  8480. ctrPlain, (int)sizeof(ctr128Wrap96Cipher), ctr128Wrap96Cipher },
  8481. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap96,
  8482. ctrPlain, (int)sizeof(ctr128Wrap96CipherLong),
  8483. ctr128Wrap96CipherLong },
  8484. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap96_2,
  8485. ctrPlain, (int)sizeof(ctr128Wrap96_2CipherLong),
  8486. ctr128Wrap96_2CipherLong },
  8487. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap64,
  8488. ctrPlain, (int)sizeof(ctr128Wrap64Cipher), ctr128Wrap64Cipher },
  8489. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap64,
  8490. ctrPlain, (int)sizeof(ctr128Wrap64CipherLong),
  8491. ctr128Wrap64CipherLong },
  8492. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap64_2,
  8493. ctrPlain, (int)sizeof(ctr128Wrap64_2CipherLong),
  8494. ctr128Wrap64_2CipherLong },
  8495. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap32,
  8496. ctrPlain, (int)sizeof(ctr128Wrap32Cipher), ctr128Wrap32Cipher },
  8497. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap32,
  8498. ctrPlain, (int)sizeof(ctr128Wrap32CipherLong),
  8499. ctr128Wrap32CipherLong },
  8500. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap32_2,
  8501. ctrPlain, (int)sizeof(ctr128Wrap32_2CipherLong),
  8502. ctr128Wrap32_2CipherLong },
  8503. #endif
  8504. #endif
  8505. #ifdef WOLFSSL_AES_192
  8506. { ctr192Key, (int)sizeof(ctr192Key), ctrIv,
  8507. ctrPlain, (int)sizeof(ctrPlain), ctr192Cipher },
  8508. /* let's try with just 9 bytes, non block size test */
  8509. { ctr192Key, (int)sizeof(ctr192Key), ctrIv,
  8510. ctrPlain, (int)sizeof(oddCipher), ctr192Cipher },
  8511. /* Counter wrapping */
  8512. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap128,
  8513. ctrPlain, (int)sizeof(ctr192Wrap128Cipher), ctr192Wrap128Cipher },
  8514. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap128,
  8515. ctrPlain, (int)sizeof(ctr192Wrap128CipherLong),
  8516. ctr192Wrap128CipherLong },
  8517. #ifdef WOLFSSL_ARMASM
  8518. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap128_2,
  8519. ctrPlain, (int)sizeof(ctr192Wrap128_2CipherLong),
  8520. ctr192Wrap128_2CipherLong },
  8521. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap96,
  8522. ctrPlain, (int)sizeof(ctr192Wrap96Cipher), ctr192Wrap96Cipher },
  8523. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap96,
  8524. ctrPlain, (int)sizeof(ctr192Wrap96CipherLong),
  8525. ctr192Wrap96CipherLong },
  8526. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap96_2,
  8527. ctrPlain, (int)sizeof(ctr192Wrap96_2CipherLong),
  8528. ctr192Wrap96_2CipherLong },
  8529. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap64,
  8530. ctrPlain, (int)sizeof(ctr192Wrap64Cipher), ctr192Wrap64Cipher },
  8531. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap64,
  8532. ctrPlain, (int)sizeof(ctr192Wrap64CipherLong),
  8533. ctr192Wrap64CipherLong },
  8534. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap64_2,
  8535. ctrPlain, (int)sizeof(ctr192Wrap64_2CipherLong),
  8536. ctr192Wrap64_2CipherLong },
  8537. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap32,
  8538. ctrPlain, (int)sizeof(ctr192Wrap32Cipher), ctr192Wrap32Cipher },
  8539. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap32,
  8540. ctrPlain, (int)sizeof(ctr192Wrap32CipherLong),
  8541. ctr192Wrap32CipherLong },
  8542. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap32_2,
  8543. ctrPlain, (int)sizeof(ctr192Wrap32_2CipherLong),
  8544. ctr192Wrap32_2CipherLong },
  8545. #endif
  8546. #endif
  8547. #ifdef WOLFSSL_AES_256
  8548. { ctr256Key, (int)sizeof(ctr256Key), ctrIv,
  8549. ctrPlain, (int)sizeof(ctrPlain), ctr256Cipher },
  8550. /* let's try with just 9 bytes, non block size test */
  8551. { ctr256Key, (int)sizeof(ctr256Key), ctrIv,
  8552. ctrPlain, (int)sizeof(oddCipher), ctr256Cipher },
  8553. /* Counter wrapping */
  8554. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap128,
  8555. ctrPlain, (int)sizeof(ctr256Wrap128Cipher), ctr256Wrap128Cipher },
  8556. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap128,
  8557. ctrPlain, (int)sizeof(ctr256Wrap128CipherLong),
  8558. ctr256Wrap128CipherLong },
  8559. #ifdef WOLFSSL_ARMASM
  8560. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap128_2,
  8561. ctrPlain, (int)sizeof(ctr256Wrap128_2CipherLong),
  8562. ctr256Wrap128_2CipherLong },
  8563. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap96,
  8564. ctrPlain, (int)sizeof(ctr256Wrap96Cipher), ctr256Wrap96Cipher },
  8565. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap96,
  8566. ctrPlain, (int)sizeof(ctr256Wrap96CipherLong),
  8567. ctr256Wrap96CipherLong },
  8568. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap96_2,
  8569. ctrPlain, (int)sizeof(ctr256Wrap96_2CipherLong),
  8570. ctr256Wrap96_2CipherLong },
  8571. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap64,
  8572. ctrPlain, (int)sizeof(ctr256Wrap64Cipher), ctr256Wrap64Cipher },
  8573. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap64,
  8574. ctrPlain, (int)sizeof(ctr256Wrap64CipherLong),
  8575. ctr256Wrap64CipherLong },
  8576. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap64_2,
  8577. ctrPlain, (int)sizeof(ctr256Wrap64_2CipherLong),
  8578. ctr256Wrap64_2CipherLong },
  8579. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap32,
  8580. ctrPlain, (int)sizeof(ctr256Wrap32Cipher), ctr256Wrap32Cipher },
  8581. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap32,
  8582. ctrPlain, (int)sizeof(ctr256Wrap32CipherLong),
  8583. ctr256Wrap32CipherLong },
  8584. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap32_2,
  8585. ctrPlain, (int)sizeof(ctr256Wrap32_2CipherLong),
  8586. ctr256Wrap32_2CipherLong },
  8587. #endif
  8588. #endif
  8589. };
  8590. #define AES_CTR_TEST_LEN (int)(sizeof(testVec) / sizeof(*testVec))
  8591. for (i = 0; i < AES_CTR_TEST_LEN; i++) {
  8592. if (testVec[i].key != NULL) {
  8593. ret = wc_AesSetKeyDirect(enc, testVec[i].key, testVec[i].keySz,
  8594. testVec[i].iv, AES_ENCRYPTION);
  8595. if (ret != 0) {
  8596. ERROR_OUT(-5930 - i * 10, out);
  8597. }
  8598. /* Ctr only uses encrypt, even on key setup */
  8599. ret = wc_AesSetKeyDirect(dec, testVec[i].key, testVec[i].keySz,
  8600. testVec[i].iv, AES_ENCRYPTION);
  8601. if (ret != 0) {
  8602. ERROR_OUT(-5931 - i * 10, out);
  8603. }
  8604. }
  8605. ret = wc_AesCtrEncrypt(enc, cipher, testVec[i].plain, testVec[i].len);
  8606. if (ret != 0) {
  8607. ERROR_OUT(-5932 - i * 10, out);
  8608. }
  8609. ret = wc_AesCtrEncrypt(dec, plain, cipher, testVec[i].len);
  8610. if (ret != 0) {
  8611. ERROR_OUT(-5933 - i * 10, out);
  8612. }
  8613. if (XMEMCMP(plain, ctrPlain, testVec[i].len)) {
  8614. ERROR_OUT(-5934 - i * 10, out);
  8615. }
  8616. #if !(FIPS_VERSION_EQ(2,0) && defined(WOLFSSL_ARMASM))
  8617. if (XMEMCMP(cipher, testVec[i].cipher, testVec[i].len)) {
  8618. ERROR_OUT(-5935 - i * 10, out);
  8619. }
  8620. #endif
  8621. }
  8622. out:
  8623. return ret;
  8624. }
  8625. #endif /* WOLFSSL_AES_COUNTER */
  8626. WOLFSSL_TEST_SUBROUTINE int aes_test(void)
  8627. {
  8628. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_COUNTER) || defined(WOLFSSL_AES_DIRECT)
  8629. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8630. Aes *enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  8631. #else
  8632. Aes enc[1];
  8633. #endif
  8634. byte cipher[AES_BLOCK_SIZE * 4];
  8635. #ifdef HAVE_AES_DECRYPT
  8636. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8637. Aes *dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  8638. #else
  8639. Aes dec[1];
  8640. #endif
  8641. byte plain [AES_BLOCK_SIZE * 4];
  8642. #endif /* HAVE_AES_DECRYPT */
  8643. #endif /* HAVE_AES_CBC || WOLFSSL_AES_COUNTER || WOLFSSL_AES_DIRECT */
  8644. int ret = 0;
  8645. #ifdef HAVE_AES_CBC
  8646. #ifdef WOLFSSL_AES_128
  8647. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = { /* "Now is the time for all " w/o trailing 0 */
  8648. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  8649. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  8650. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  8651. };
  8652. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  8653. {
  8654. 0x95,0x94,0x92,0x57,0x5f,0x42,0x81,0x53,
  8655. 0x2c,0xcc,0x9d,0x46,0x77,0xa2,0x33,0xcb
  8656. };
  8657. WOLFSSL_SMALL_STACK_STATIC const byte key[] = "0123456789abcdef "; /* align */
  8658. WOLFSSL_SMALL_STACK_STATIC const byte iv[] = "1234567890abcdef "; /* align */
  8659. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8660. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_COUNTER) || defined(WOLFSSL_AES_DIRECT)
  8661. if (enc == NULL)
  8662. ERROR_OUT(-5990, out);
  8663. #endif
  8664. #if defined(HAVE_AES_DECRYPT) || defined(WOLFSSL_AES_COUNTER) || defined(WOLFSSL_AES_DIRECT)
  8665. if (dec == NULL)
  8666. ERROR_OUT(-5991, out);
  8667. #endif
  8668. #endif
  8669. if (wc_AesInit(enc, HEAP_HINT, devId) != 0)
  8670. ERROR_OUT(-5900, out); /* note this error code is used programmatically in cleanup. */
  8671. #if defined(HAVE_AES_DECRYPT) || defined(WOLFSSL_AES_COUNTER)
  8672. if (wc_AesInit(dec, HEAP_HINT, devId) != 0)
  8673. ERROR_OUT(-5901, out); /* note this error code is used programmatically in cleanup. */
  8674. #endif
  8675. ret = wc_AesSetKey(enc, key, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  8676. if (ret != 0)
  8677. ERROR_OUT(-5902, out);
  8678. #if defined(HAVE_AES_DECRYPT) || defined(WOLFSSL_AES_COUNTER)
  8679. ret = wc_AesSetKey(dec, key, AES_BLOCK_SIZE, iv, AES_DECRYPTION);
  8680. if (ret != 0)
  8681. ERROR_OUT(-5903, out);
  8682. #endif
  8683. XMEMSET(cipher, 0, AES_BLOCK_SIZE * 4);
  8684. ret = wc_AesCbcEncrypt(enc, cipher, msg, AES_BLOCK_SIZE);
  8685. #if defined(WOLFSSL_ASYNC_CRYPT)
  8686. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  8687. #endif
  8688. if (ret != 0)
  8689. ERROR_OUT(-5904, out);
  8690. #ifdef HAVE_AES_DECRYPT
  8691. XMEMSET(plain, 0, AES_BLOCK_SIZE * 4);
  8692. ret = wc_AesCbcDecrypt(dec, plain, cipher, AES_BLOCK_SIZE);
  8693. #if defined(WOLFSSL_ASYNC_CRYPT)
  8694. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  8695. #endif
  8696. if (ret != 0)
  8697. ERROR_OUT(-5905, out);
  8698. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  8699. ERROR_OUT(-5906, out);
  8700. #endif /* HAVE_AES_DECRYPT */
  8701. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE))
  8702. ERROR_OUT(-5907, out);
  8703. #endif /* WOLFSSL_AES_128 */
  8704. #if defined(WOLFSSL_AESNI) && defined(HAVE_AES_DECRYPT)
  8705. {
  8706. WOLFSSL_SMALL_STACK_STATIC const byte bigMsg[] = {
  8707. /* "All work and no play makes Jack a dull boy. " */
  8708. 0x41,0x6c,0x6c,0x20,0x77,0x6f,0x72,0x6b,
  8709. 0x20,0x61,0x6e,0x64,0x20,0x6e,0x6f,0x20,
  8710. 0x70,0x6c,0x61,0x79,0x20,0x6d,0x61,0x6b,
  8711. 0x65,0x73,0x20,0x4a,0x61,0x63,0x6b,0x20,
  8712. 0x61,0x20,0x64,0x75,0x6c,0x6c,0x20,0x62,
  8713. 0x6f,0x79,0x2e,0x20,0x41,0x6c,0x6c,0x20,
  8714. 0x77,0x6f,0x72,0x6b,0x20,0x61,0x6e,0x64,
  8715. 0x20,0x6e,0x6f,0x20,0x70,0x6c,0x61,0x79,
  8716. 0x20,0x6d,0x61,0x6b,0x65,0x73,0x20,0x4a,
  8717. 0x61,0x63,0x6b,0x20,0x61,0x20,0x64,0x75,
  8718. 0x6c,0x6c,0x20,0x62,0x6f,0x79,0x2e,0x20,
  8719. 0x41,0x6c,0x6c,0x20,0x77,0x6f,0x72,0x6b,
  8720. 0x20,0x61,0x6e,0x64,0x20,0x6e,0x6f,0x20,
  8721. 0x70,0x6c,0x61,0x79,0x20,0x6d,0x61,0x6b,
  8722. 0x65,0x73,0x20,0x4a,0x61,0x63,0x6b,0x20,
  8723. 0x61,0x20,0x64,0x75,0x6c,0x6c,0x20,0x62,
  8724. 0x6f,0x79,0x2e,0x20,0x41,0x6c,0x6c,0x20,
  8725. 0x77,0x6f,0x72,0x6b,0x20,0x61,0x6e,0x64,
  8726. 0x20,0x6e,0x6f,0x20,0x70,0x6c,0x61,0x79,
  8727. 0x20,0x6d,0x61,0x6b,0x65,0x73,0x20,0x4a,
  8728. 0x61,0x63,0x6b,0x20,0x61,0x20,0x64,0x75,
  8729. 0x6c,0x6c,0x20,0x62,0x6f,0x79,0x2e,0x20,
  8730. 0x41,0x6c,0x6c,0x20,0x77,0x6f,0x72,0x6b,
  8731. 0x20,0x61,0x6e,0x64,0x20,0x6e,0x6f,0x20,
  8732. 0x70,0x6c,0x61,0x79,0x20,0x6d,0x61,0x6b,
  8733. 0x65,0x73,0x20,0x4a,0x61,0x63,0x6b,0x20,
  8734. 0x61,0x20,0x64,0x75,0x6c,0x6c,0x20,0x62,
  8735. 0x6f,0x79,0x2e,0x20,0x41,0x6c,0x6c,0x20,
  8736. 0x77,0x6f,0x72,0x6b,0x20,0x61,0x6e,0x64,
  8737. 0x20,0x6e,0x6f,0x20,0x70,0x6c,0x61,0x79,
  8738. 0x20,0x6d,0x61,0x6b,0x65,0x73,0x20,0x4a,
  8739. 0x61,0x63,0x6b,0x20,0x61,0x20,0x64,0x75,
  8740. 0x6c,0x6c,0x20,0x62,0x6f,0x79,0x2e,0x20,
  8741. 0x41,0x6c,0x6c,0x20,0x77,0x6f,0x72,0x6b,
  8742. 0x20,0x61,0x6e,0x64,0x20,0x6e,0x6f,0x20,
  8743. 0x70,0x6c,0x61,0x79,0x20,0x6d,0x61,0x6b,
  8744. 0x65,0x73,0x20,0x4a,0x61,0x63,0x6b,0x20,
  8745. 0x61,0x20,0x64,0x75,0x6c,0x6c,0x20,0x62,
  8746. 0x6f,0x79,0x2e,0x20,0x41,0x6c,0x6c,0x20,
  8747. 0x77,0x6f,0x72,0x6b,0x20,0x61,0x6e,0x64,
  8748. 0x20,0x6e,0x6f,0x20,0x70,0x6c,0x61,0x79,
  8749. 0x20,0x6d,0x61,0x6b,0x65,0x73,0x20,0x4a,
  8750. 0x61,0x63,0x6b,0x20,0x61,0x20,0x64,0x75,
  8751. 0x6c,0x6c,0x20,0x62,0x6f,0x79,0x2e,0x20,
  8752. 0x41,0x6c,0x6c,0x20,0x77,0x6f,0x72,0x6b,
  8753. 0x20,0x61,0x6e,0x64,0x20,0x6e,0x6f,0x20,
  8754. 0x70,0x6c,0x61,0x79,0x20,0x6d,0x61,0x6b,
  8755. 0x65,0x73,0x20,0x4a,0x61,0x63,0x6b,0x20
  8756. };
  8757. WOLFSSL_SMALL_STACK_STATIC const byte bigKey[] = "0123456789abcdeffedcba9876543210";
  8758. word32 keySz, msgSz;
  8759. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8760. byte *bigCipher = (byte *)XMALLOC(sizeof(bigMsg), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8761. byte *bigPlain = (byte *)XMALLOC(sizeof(bigMsg), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8762. if ((bigCipher == NULL) ||
  8763. (bigPlain == NULL)) {
  8764. if (bigCipher != NULL)
  8765. XFREE(bigCipher, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8766. ERROR_OUT(-5992, out);
  8767. }
  8768. #else
  8769. byte bigCipher[sizeof(bigMsg)];
  8770. byte bigPlain[sizeof(bigMsg)];
  8771. #endif
  8772. /* Iterate from one AES_BLOCK_SIZE of bigMsg through the whole
  8773. * message by AES_BLOCK_SIZE for each size of AES key. */
  8774. for (keySz = 16; keySz <= 32; keySz += 8) {
  8775. for (msgSz = AES_BLOCK_SIZE;
  8776. msgSz <= sizeof(bigMsg);
  8777. msgSz += AES_BLOCK_SIZE) {
  8778. XMEMSET(bigCipher, 0, sizeof(bigMsg));
  8779. XMEMSET(bigPlain, 0, sizeof(bigMsg));
  8780. ret = wc_AesSetKey(enc, bigKey, keySz, iv, AES_ENCRYPTION);
  8781. if (ret != 0) {
  8782. ret = -5908;
  8783. break;
  8784. }
  8785. ret = wc_AesSetKey(dec, bigKey, keySz, iv, AES_DECRYPTION);
  8786. if (ret != 0) {
  8787. ret = -5909;
  8788. break;
  8789. }
  8790. ret = wc_AesCbcEncrypt(enc, bigCipher, bigMsg, msgSz);
  8791. #if defined(WOLFSSL_ASYNC_CRYPT)
  8792. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  8793. #endif
  8794. if (ret != 0) {
  8795. ret = -5910;
  8796. break;
  8797. }
  8798. ret = wc_AesCbcDecrypt(dec, bigPlain, bigCipher, msgSz);
  8799. #if defined(WOLFSSL_ASYNC_CRYPT)
  8800. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  8801. #endif
  8802. if (ret != 0) {
  8803. ret = -5911;
  8804. break;
  8805. }
  8806. if (XMEMCMP(bigPlain, bigMsg, msgSz)) {
  8807. ret = -5912;
  8808. break;
  8809. }
  8810. }
  8811. if (ret != 0)
  8812. break;
  8813. }
  8814. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8815. XFREE(bigCipher, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8816. XFREE(bigPlain, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8817. #endif
  8818. if (ret != 0)
  8819. goto out;
  8820. }
  8821. #endif /* WOLFSSL_AESNI && HAVE_AES_DECRYPT */
  8822. /* Test of AES IV state with encrypt/decrypt */
  8823. #ifdef WOLFSSL_AES_128
  8824. {
  8825. /* Test Vector from "NIST Special Publication 800-38A, 2001 Edition"
  8826. * https://nvlpubs.nist.gov/nistpubs/legacy/sp/nistspecialpublication800-38a.pdf
  8827. */
  8828. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] =
  8829. {
  8830. 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
  8831. 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
  8832. 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c,
  8833. 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51
  8834. };
  8835. WOLFSSL_SMALL_STACK_STATIC const byte verify2[] =
  8836. {
  8837. 0x76, 0x49, 0xab, 0xac, 0x81, 0x19, 0xb2, 0x46,
  8838. 0xce, 0xe9, 0x8e, 0x9b, 0x12, 0xe9, 0x19, 0x7d,
  8839. 0x50, 0x86, 0xcb, 0x9b, 0x50, 0x72, 0x19, 0xee,
  8840. 0x95, 0xdb, 0x11, 0x3a, 0x91, 0x76, 0x78, 0xb2
  8841. };
  8842. WOLFSSL_SMALL_STACK_STATIC const byte key2[] = {
  8843. 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
  8844. 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c
  8845. };
  8846. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] = {
  8847. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  8848. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
  8849. };
  8850. ret = wc_AesSetKey(enc, key2, sizeof(key2), iv2, AES_ENCRYPTION);
  8851. if (ret != 0)
  8852. ERROR_OUT(-5913, out);
  8853. XMEMSET(cipher, 0, AES_BLOCK_SIZE * 2);
  8854. ret = wc_AesCbcEncrypt(enc, cipher, msg2, AES_BLOCK_SIZE);
  8855. #if defined(WOLFSSL_ASYNC_CRYPT)
  8856. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  8857. #endif
  8858. if (ret != 0)
  8859. ERROR_OUT(-5914, out);
  8860. if (XMEMCMP(cipher, verify2, AES_BLOCK_SIZE))
  8861. ERROR_OUT(-5915, out);
  8862. ret = wc_AesCbcEncrypt(enc, cipher + AES_BLOCK_SIZE,
  8863. msg2 + AES_BLOCK_SIZE, AES_BLOCK_SIZE);
  8864. #if defined(WOLFSSL_ASYNC_CRYPT)
  8865. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  8866. #endif
  8867. if (ret != 0)
  8868. ERROR_OUT(-5916, out);
  8869. if (XMEMCMP(cipher + AES_BLOCK_SIZE, verify2 + AES_BLOCK_SIZE,
  8870. AES_BLOCK_SIZE))
  8871. ERROR_OUT(-5917, out);
  8872. #if defined(HAVE_AES_DECRYPT)
  8873. ret = wc_AesSetKey(dec, key2, sizeof(key2), iv2, AES_DECRYPTION);
  8874. if (ret != 0)
  8875. ERROR_OUT(-5918, out);
  8876. XMEMSET(plain, 0, AES_BLOCK_SIZE * 2);
  8877. ret = wc_AesCbcDecrypt(dec, plain, verify2, AES_BLOCK_SIZE);
  8878. #if defined(WOLFSSL_ASYNC_CRYPT)
  8879. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  8880. #endif
  8881. if (ret != 0)
  8882. ERROR_OUT(-5919, out);
  8883. if (XMEMCMP(plain, msg2, AES_BLOCK_SIZE))
  8884. ERROR_OUT(-5920, out);
  8885. ret = wc_AesCbcDecrypt(dec, plain + AES_BLOCK_SIZE,
  8886. verify2 + AES_BLOCK_SIZE, AES_BLOCK_SIZE);
  8887. #if defined(WOLFSSL_ASYNC_CRYPT)
  8888. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  8889. #endif
  8890. if (ret != 0)
  8891. ERROR_OUT(-5921, out);
  8892. if (XMEMCMP(plain + AES_BLOCK_SIZE, msg2 + AES_BLOCK_SIZE,
  8893. AES_BLOCK_SIZE))
  8894. ERROR_OUT(-5922, out);
  8895. #endif /* HAVE_AES_DECRYPT */
  8896. }
  8897. #endif /* WOLFSSL_AES_128 */
  8898. #endif /* HAVE_AES_CBC */
  8899. #ifdef WOLFSSL_AES_COUNTER
  8900. ret = aesctr_test(enc, dec, cipher, plain);
  8901. if (ret != 0)
  8902. return ret;
  8903. #endif
  8904. #if defined(WOLFSSL_AES_DIRECT) && defined(WOLFSSL_AES_256)
  8905. {
  8906. WOLFSSL_SMALL_STACK_STATIC const byte niPlain[] =
  8907. {
  8908. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  8909. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  8910. };
  8911. WOLFSSL_SMALL_STACK_STATIC const byte niCipher[] =
  8912. {
  8913. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  8914. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  8915. };
  8916. WOLFSSL_SMALL_STACK_STATIC const byte niKey[] =
  8917. {
  8918. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  8919. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  8920. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  8921. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  8922. };
  8923. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  8924. ret = wc_AesSetKey(enc, niKey, sizeof(niKey), cipher, AES_ENCRYPTION);
  8925. if (ret != 0)
  8926. ERROR_OUT(-5994, out);
  8927. #if !defined(HAVE_SELFTEST) && \
  8928. (defined(WOLFSSL_LINUXKM) || \
  8929. !defined(HAVE_FIPS) || \
  8930. (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3)))
  8931. if (wc_AesEncryptDirect(enc, cipher, niPlain) != 0)
  8932. ERROR_OUT(-5995, out);
  8933. #else
  8934. wc_AesEncryptDirect(enc, cipher, niPlain);
  8935. #endif
  8936. if (XMEMCMP(cipher, niCipher, AES_BLOCK_SIZE) != 0)
  8937. ERROR_OUT(-5996, out);
  8938. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  8939. ret = wc_AesSetKey(dec, niKey, sizeof(niKey), plain, AES_DECRYPTION);
  8940. if (ret != 0)
  8941. ERROR_OUT(-5997, out);
  8942. #if !defined(HAVE_SELFTEST) && \
  8943. (defined(WOLFSSL_LINUXKM) || \
  8944. !defined(HAVE_FIPS) || \
  8945. (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3)))
  8946. if (wc_AesDecryptDirect(dec, plain, niCipher) != 0)
  8947. ERROR_OUT(-5998, out);
  8948. #else
  8949. wc_AesDecryptDirect(dec, plain, niCipher);
  8950. #endif
  8951. if (XMEMCMP(plain, niPlain, AES_BLOCK_SIZE) != 0)
  8952. ERROR_OUT(-5999, out);
  8953. }
  8954. #endif /* WOLFSSL_AES_DIRECT && WOLFSSL_AES_256 */
  8955. ret = aes_key_size_test();
  8956. if (ret != 0)
  8957. goto out;
  8958. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  8959. ret = aes_cbc_test();
  8960. if (ret != 0)
  8961. goto out;
  8962. #endif
  8963. #if defined(WOLFSSL_AES_XTS)
  8964. #ifdef WOLFSSL_AES_128
  8965. ret = aes_xts_128_test();
  8966. if (ret != 0)
  8967. goto out;
  8968. #endif
  8969. #ifdef WOLFSSL_AES_256
  8970. ret = aes_xts_256_test();
  8971. if (ret != 0)
  8972. goto out;
  8973. #endif
  8974. #if defined(WOLFSSL_AES_128) && defined(WOLFSSL_AES_256)
  8975. ret = aes_xts_sector_test();
  8976. if (ret != 0)
  8977. goto out;
  8978. #endif
  8979. #ifdef WOLFSSL_AES_128
  8980. ret = aes_xts_args_test();
  8981. if (ret != 0)
  8982. goto out;
  8983. #endif
  8984. #endif
  8985. #if defined(WOLFSSL_AES_CFB)
  8986. ret = aescfb_test();
  8987. if (ret != 0)
  8988. goto out;
  8989. #if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  8990. ret = aescfb1_test();
  8991. if (ret != 0)
  8992. goto out;
  8993. ret = aescfb8_test();
  8994. if (ret != 0)
  8995. goto out;
  8996. #endif
  8997. #endif
  8998. #if defined(HAVE_AES_ECB) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  8999. ret = aesecb_test();
  9000. if (ret != 0)
  9001. goto out;
  9002. #endif
  9003. out:
  9004. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_COUNTER) || defined(WOLFSSL_AES_DIRECT)
  9005. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9006. if (enc) {
  9007. if (ret != -5900) /* note this must match ERRROR_OUT() code
  9008. * for wc_AesInit(enc, ...) failure above.
  9009. */
  9010. wc_AesFree(enc);
  9011. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  9012. }
  9013. #else
  9014. if (ret != -5900)
  9015. wc_AesFree(enc);
  9016. #endif
  9017. (void)cipher;
  9018. #ifdef HAVE_AES_DECRYPT
  9019. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9020. if (dec) {
  9021. if ((ret != -5900) && (ret != -5901))
  9022. /* note these codes must match the ERRROR_OUT() codes for
  9023. * wc_AesInit() failures above.
  9024. */
  9025. wc_AesFree(dec);
  9026. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  9027. }
  9028. #else
  9029. if ((ret != -5900) && (ret != -5901))
  9030. wc_AesFree(dec);
  9031. #endif
  9032. (void)plain;
  9033. #endif /* HAVE_AES_DECRYPT */
  9034. #endif /* HAVE_AES_CBC || WOLFSSL_AES_COUNTER || WOLFSSL_AES_DIRECT */
  9035. return ret;
  9036. }
  9037. #ifdef WOLFSSL_AES_192
  9038. WOLFSSL_TEST_SUBROUTINE int aes192_test(void)
  9039. {
  9040. #ifdef HAVE_AES_CBC
  9041. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9042. Aes *enc = NULL;
  9043. #else
  9044. Aes enc[1];
  9045. #endif
  9046. byte cipher[AES_BLOCK_SIZE];
  9047. #ifdef HAVE_AES_DECRYPT
  9048. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9049. Aes *dec = NULL;
  9050. #else
  9051. Aes dec[1];
  9052. #endif
  9053. byte plain[AES_BLOCK_SIZE];
  9054. #endif
  9055. #endif /* HAVE_AES_CBC */
  9056. int ret = 0;
  9057. #ifdef HAVE_AES_CBC
  9058. /* Test vectors from NIST Special Publication 800-38A, 2001 Edition
  9059. * Appendix F.2.3 */
  9060. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = {
  9061. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  9062. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  9063. };
  9064. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  9065. {
  9066. 0x4f,0x02,0x1d,0xb2,0x43,0xbc,0x63,0x3d,
  9067. 0x71,0x78,0x18,0x3a,0x9f,0xa0,0x71,0xe8
  9068. };
  9069. WOLFSSL_SMALL_STACK_STATIC byte key[] = {
  9070. 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  9071. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  9072. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b
  9073. };
  9074. WOLFSSL_SMALL_STACK_STATIC byte iv[] = {
  9075. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  9076. 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F
  9077. };
  9078. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9079. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  9080. ERROR_OUT(-6008, out);
  9081. #ifdef HAVE_AES_DECRYPT
  9082. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  9083. ERROR_OUT(-6009, out);
  9084. #endif
  9085. #endif
  9086. if (wc_AesInit(enc, HEAP_HINT, devId) != 0)
  9087. ERROR_OUT(-6000, out);
  9088. #ifdef HAVE_AES_DECRYPT
  9089. if (wc_AesInit(dec, HEAP_HINT, devId) != 0)
  9090. ERROR_OUT(-6001, out);
  9091. #endif
  9092. ret = wc_AesSetKey(enc, key, (int) sizeof(key), iv, AES_ENCRYPTION);
  9093. if (ret != 0)
  9094. ERROR_OUT(-6002, out);
  9095. #ifdef HAVE_AES_DECRYPT
  9096. ret = wc_AesSetKey(dec, key, (int) sizeof(key), iv, AES_DECRYPTION);
  9097. if (ret != 0)
  9098. ERROR_OUT(-6003, out);
  9099. #endif
  9100. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  9101. ret = wc_AesCbcEncrypt(enc, cipher, msg, (int) sizeof(msg));
  9102. #if defined(WOLFSSL_ASYNC_CRYPT)
  9103. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  9104. #endif
  9105. if (ret != 0)
  9106. ERROR_OUT(-6004, out);
  9107. #ifdef HAVE_AES_DECRYPT
  9108. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  9109. ret = wc_AesCbcDecrypt(dec, plain, cipher, (int) sizeof(cipher));
  9110. #if defined(WOLFSSL_ASYNC_CRYPT)
  9111. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  9112. #endif
  9113. if (ret != 0)
  9114. ERROR_OUT(-6005, out);
  9115. if (XMEMCMP(plain, msg, (int) sizeof(plain))) {
  9116. ERROR_OUT(-6006, out);
  9117. }
  9118. #endif
  9119. if (XMEMCMP(cipher, verify, (int) sizeof(cipher)))
  9120. ERROR_OUT(-6007, out);
  9121. wc_AesFree(enc);
  9122. #ifdef HAVE_AES_DECRYPT
  9123. wc_AesFree(dec);
  9124. #endif
  9125. out:
  9126. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9127. if (enc)
  9128. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  9129. #ifdef HAVE_AES_DECRYPT
  9130. if (dec)
  9131. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  9132. #endif
  9133. #endif
  9134. #endif /* HAVE_AES_CBC */
  9135. return ret;
  9136. }
  9137. #endif /* WOLFSSL_AES_192 */
  9138. #ifdef WOLFSSL_AES_256
  9139. WOLFSSL_TEST_SUBROUTINE int aes256_test(void)
  9140. {
  9141. #ifdef HAVE_AES_CBC
  9142. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9143. Aes *enc = NULL;
  9144. #else
  9145. Aes enc[1];
  9146. #endif
  9147. byte cipher[AES_BLOCK_SIZE];
  9148. #ifdef HAVE_AES_DECRYPT
  9149. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9150. Aes *dec = NULL;
  9151. #else
  9152. Aes dec[1];
  9153. #endif
  9154. byte plain[AES_BLOCK_SIZE];
  9155. #endif
  9156. #endif /* HAVE_AES_CBC */
  9157. int ret = 0;
  9158. #ifdef HAVE_AES_CBC
  9159. /* Test vectors from NIST Special Publication 800-38A, 2001 Edition,
  9160. * Appendix F.2.5 */
  9161. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = {
  9162. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  9163. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  9164. };
  9165. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  9166. {
  9167. 0xf5,0x8c,0x4c,0x04,0xd6,0xe5,0xf1,0xba,
  9168. 0x77,0x9e,0xab,0xfb,0x5f,0x7b,0xfb,0xd6
  9169. };
  9170. WOLFSSL_SMALL_STACK_STATIC byte key[] = {
  9171. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  9172. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  9173. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  9174. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  9175. };
  9176. WOLFSSL_SMALL_STACK_STATIC byte iv[] = {
  9177. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  9178. 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F
  9179. };
  9180. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9181. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  9182. ERROR_OUT(-6108, out);
  9183. #ifdef HAVE_AES_DECRYPT
  9184. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  9185. ERROR_OUT(-6109, out);
  9186. #endif
  9187. #endif
  9188. if (wc_AesInit(enc, HEAP_HINT, devId) != 0)
  9189. ERROR_OUT(-6100, out);
  9190. #ifdef HAVE_AES_DECRYPT
  9191. if (wc_AesInit(dec, HEAP_HINT, devId) != 0)
  9192. ERROR_OUT(-6101, out);
  9193. #endif
  9194. ret = wc_AesSetKey(enc, key, (int) sizeof(key), iv, AES_ENCRYPTION);
  9195. if (ret != 0)
  9196. ERROR_OUT(-6102, out);
  9197. #ifdef HAVE_AES_DECRYPT
  9198. ret = wc_AesSetKey(dec, key, (int) sizeof(key), iv, AES_DECRYPTION);
  9199. if (ret != 0)
  9200. ERROR_OUT(-6103, out);
  9201. #endif
  9202. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  9203. ret = wc_AesCbcEncrypt(enc, cipher, msg, (int) sizeof(msg));
  9204. #if defined(WOLFSSL_ASYNC_CRYPT)
  9205. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  9206. #endif
  9207. if (ret != 0)
  9208. ERROR_OUT(-6104, out);
  9209. #ifdef HAVE_AES_DECRYPT
  9210. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  9211. ret = wc_AesCbcDecrypt(dec, plain, cipher, (int) sizeof(cipher));
  9212. #if defined(WOLFSSL_ASYNC_CRYPT)
  9213. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  9214. #endif
  9215. if (ret != 0)
  9216. ERROR_OUT(-6105, out);
  9217. if (XMEMCMP(plain, msg, (int) sizeof(plain))) {
  9218. ERROR_OUT(-6106, out);
  9219. }
  9220. #endif
  9221. if (XMEMCMP(cipher, verify, (int) sizeof(cipher)))
  9222. ERROR_OUT(-6107, out);
  9223. wc_AesFree(enc);
  9224. #ifdef HAVE_AES_DECRYPT
  9225. wc_AesFree(dec);
  9226. #endif
  9227. out:
  9228. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9229. if (enc)
  9230. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  9231. #ifdef HAVE_AES_DECRYPT
  9232. if (dec)
  9233. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  9234. #endif
  9235. #endif
  9236. #endif /* HAVE_AES_CBC */
  9237. return ret;
  9238. }
  9239. #endif /* WOLFSSL_AES_256 */
  9240. #ifdef HAVE_AESGCM
  9241. #ifdef WOLFSSL_AES_128
  9242. static int aesgcm_default_test_helper(byte* key, int keySz, byte* iv, int ivSz,
  9243. byte* plain, int plainSz, byte* cipher, int cipherSz,
  9244. byte* aad, int aadSz, byte* tag, int tagSz)
  9245. {
  9246. int ret, enc_inited = 0, dec_inited = 0;
  9247. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9248. Aes *enc = NULL;
  9249. Aes *dec = NULL;
  9250. #else
  9251. Aes enc[1];
  9252. Aes dec[1];
  9253. #endif
  9254. byte resultT[AES_BLOCK_SIZE];
  9255. byte resultP[AES_BLOCK_SIZE * 3];
  9256. byte resultC[AES_BLOCK_SIZE * 3];
  9257. int result;
  9258. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9259. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  9260. ERROR_OUT(-6118, out);
  9261. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  9262. ERROR_OUT(-6119, out);
  9263. #endif
  9264. XMEMSET(resultT, 0, sizeof(resultT));
  9265. XMEMSET(resultC, 0, sizeof(resultC));
  9266. XMEMSET(resultP, 0, sizeof(resultP));
  9267. if (wc_AesInit(enc, HEAP_HINT, devId) != 0)
  9268. ERROR_OUT(-6110, out);
  9269. else
  9270. enc_inited = 1;
  9271. if (wc_AesInit(dec, HEAP_HINT, devId) != 0)
  9272. ERROR_OUT(-6111, out);
  9273. else
  9274. dec_inited = 1;
  9275. result = wc_AesGcmSetKey(enc, key, keySz);
  9276. if (result != 0)
  9277. ERROR_OUT(-6112, out);
  9278. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  9279. result = wc_AesGcmEncrypt(enc, resultC, plain, plainSz, iv, ivSz,
  9280. resultT, tagSz, aad, aadSz);
  9281. #if defined(WOLFSSL_ASYNC_CRYPT)
  9282. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  9283. #endif
  9284. if (result != 0)
  9285. ERROR_OUT(-6113, out);
  9286. if (cipher != NULL) {
  9287. if (XMEMCMP(cipher, resultC, cipherSz))
  9288. ERROR_OUT(-6114, out);
  9289. }
  9290. if (XMEMCMP(tag, resultT, tagSz))
  9291. ERROR_OUT(-6115, out);
  9292. #ifdef HAVE_AES_DECRYPT
  9293. result = wc_AesGcmSetKey(dec, key, keySz);
  9294. if (result != 0)
  9295. ERROR_OUT(-6116, out);
  9296. result = wc_AesGcmDecrypt(dec, resultP, resultC, cipherSz,
  9297. iv, ivSz, resultT, tagSz, aad, aadSz);
  9298. #if defined(WOLFSSL_ASYNC_CRYPT)
  9299. result = wc_AsyncWait(result, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  9300. #endif
  9301. if (result != 0)
  9302. ERROR_OUT(-6117, out);
  9303. if (plain != NULL) {
  9304. if (XMEMCMP(plain, resultP, plainSz))
  9305. ERROR_OUT(-6118, out);
  9306. }
  9307. #endif /* HAVE_AES_DECRYPT */
  9308. ret = 0;
  9309. out:
  9310. if (enc_inited)
  9311. wc_AesFree(enc);
  9312. if (dec_inited)
  9313. wc_AesFree(dec);
  9314. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9315. if (enc)
  9316. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  9317. if (dec)
  9318. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  9319. #endif
  9320. return ret;
  9321. }
  9322. #endif
  9323. /* tests that only use 12 byte IV and 16 or less byte AAD
  9324. * test vectors are from NIST SP 800-38D
  9325. * https://csrc.nist.gov/Projects/Cryptographic-Algorithm-Validation-Program/CAVP-TESTING-BLOCK-CIPHER-MODES*/
  9326. WOLFSSL_TEST_SUBROUTINE int aesgcm_default_test(void)
  9327. {
  9328. #ifdef WOLFSSL_AES_128
  9329. byte key1[] = {
  9330. 0x29, 0x8e, 0xfa, 0x1c, 0xcf, 0x29, 0xcf, 0x62,
  9331. 0xae, 0x68, 0x24, 0xbf, 0xc1, 0x95, 0x57, 0xfc
  9332. };
  9333. byte iv1[] = {
  9334. 0x6f, 0x58, 0xa9, 0x3f, 0xe1, 0xd2, 0x07, 0xfa,
  9335. 0xe4, 0xed, 0x2f, 0x6d
  9336. };
  9337. ALIGN64 byte plain1[] = {
  9338. 0xcc, 0x38, 0xbc, 0xcd, 0x6b, 0xc5, 0x36, 0xad,
  9339. 0x91, 0x9b, 0x13, 0x95, 0xf5, 0xd6, 0x38, 0x01,
  9340. 0xf9, 0x9f, 0x80, 0x68, 0xd6, 0x5c, 0xa5, 0xac,
  9341. 0x63, 0x87, 0x2d, 0xaf, 0x16, 0xb9, 0x39, 0x01
  9342. };
  9343. byte aad1[] = {
  9344. 0x02, 0x1f, 0xaf, 0xd2, 0x38, 0x46, 0x39, 0x73,
  9345. 0xff, 0xe8, 0x02, 0x56, 0xe5, 0xb1, 0xc6, 0xb1
  9346. };
  9347. ALIGN64 byte cipher1[] = {
  9348. 0xdf, 0xce, 0x4e, 0x9c, 0xd2, 0x91, 0x10, 0x3d,
  9349. 0x7f, 0xe4, 0xe6, 0x33, 0x51, 0xd9, 0xe7, 0x9d,
  9350. 0x3d, 0xfd, 0x39, 0x1e, 0x32, 0x67, 0x10, 0x46,
  9351. 0x58, 0x21, 0x2d, 0xa9, 0x65, 0x21, 0xb7, 0xdb
  9352. };
  9353. byte tag1[] = {
  9354. 0x54, 0x24, 0x65, 0xef, 0x59, 0x93, 0x16, 0xf7,
  9355. 0x3a, 0x7a, 0x56, 0x05, 0x09, 0xa2, 0xd9, 0xf2
  9356. };
  9357. byte key2[] = {
  9358. 0x01, 0x6d, 0xbb, 0x38, 0xda, 0xa7, 0x6d, 0xfe,
  9359. 0x7d, 0xa3, 0x84, 0xeb, 0xf1, 0x24, 0x03, 0x64
  9360. };
  9361. byte iv2[] = {
  9362. 0x07, 0x93, 0xef, 0x3a, 0xda, 0x78, 0x2f, 0x78,
  9363. 0xc9, 0x8a, 0xff, 0xe3
  9364. };
  9365. ALIGN64 byte plain2[] = {
  9366. 0x4b, 0x34, 0xa9, 0xec, 0x57, 0x63, 0x52, 0x4b,
  9367. 0x19, 0x1d, 0x56, 0x16, 0xc5, 0x47, 0xf6, 0xb7
  9368. };
  9369. ALIGN64 byte cipher2[] = {
  9370. 0x60, 0x9a, 0xa3, 0xf4, 0x54, 0x1b, 0xc0, 0xfe,
  9371. 0x99, 0x31, 0xda, 0xad, 0x2e, 0xe1, 0x5d, 0x0c
  9372. };
  9373. byte tag2[] = {
  9374. 0x33, 0xaf, 0xec, 0x59, 0xc4, 0x5b, 0xaf, 0x68,
  9375. 0x9a, 0x5e, 0x1b, 0x13, 0xae, 0x42, 0x36, 0x19
  9376. };
  9377. byte key3[] = {
  9378. 0xb0, 0x1e, 0x45, 0xcc, 0x30, 0x88, 0xaa, 0xba,
  9379. 0x9f, 0xa4, 0x3d, 0x81, 0xd4, 0x81, 0x82, 0x3f
  9380. };
  9381. byte iv3[] = {
  9382. 0x5a, 0x2c, 0x4a, 0x66, 0x46, 0x87, 0x13, 0x45,
  9383. 0x6a, 0x4b, 0xd5, 0xe1
  9384. };
  9385. byte tag3[] = {
  9386. 0x01, 0x42, 0x80, 0xf9, 0x44, 0xf5, 0x3c, 0x68,
  9387. 0x11, 0x64, 0xb2, 0xff
  9388. };
  9389. int ret;
  9390. ret = aesgcm_default_test_helper(key1, sizeof(key1), iv1, sizeof(iv1),
  9391. plain1, sizeof(plain1), cipher1, sizeof(cipher1),
  9392. aad1, sizeof(aad1), tag1, sizeof(tag1));
  9393. if (ret != 0) {
  9394. return ret;
  9395. }
  9396. ret = aesgcm_default_test_helper(key2, sizeof(key2), iv2, sizeof(iv2),
  9397. plain2, sizeof(plain2), cipher2, sizeof(cipher2),
  9398. NULL, 0, tag2, sizeof(tag2));
  9399. if (ret != 0) {
  9400. return ret;
  9401. }
  9402. ret = aesgcm_default_test_helper(key3, sizeof(key3), iv3, sizeof(iv3),
  9403. NULL, 0, NULL, 0,
  9404. NULL, 0, tag3, sizeof(tag3));
  9405. if (ret != 0) {
  9406. return ret;
  9407. }
  9408. #endif
  9409. return 0;
  9410. }
  9411. WOLFSSL_TEST_SUBROUTINE int aesgcm_test(void)
  9412. {
  9413. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9414. Aes *enc = NULL;
  9415. Aes *dec = NULL;
  9416. #else
  9417. Aes enc[1];
  9418. Aes dec[1];
  9419. #endif
  9420. /*
  9421. * This is Test Case 16 from the document Galois/
  9422. * Counter Mode of Operation (GCM) by McGrew and
  9423. * Viega.
  9424. */
  9425. WOLFSSL_SMALL_STACK_STATIC const byte p[] =
  9426. {
  9427. 0xd9, 0x31, 0x32, 0x25, 0xf8, 0x84, 0x06, 0xe5,
  9428. 0xa5, 0x59, 0x09, 0xc5, 0xaf, 0xf5, 0x26, 0x9a,
  9429. 0x86, 0xa7, 0xa9, 0x53, 0x15, 0x34, 0xf7, 0xda,
  9430. 0x2e, 0x4c, 0x30, 0x3d, 0x8a, 0x31, 0x8a, 0x72,
  9431. 0x1c, 0x3c, 0x0c, 0x95, 0x95, 0x68, 0x09, 0x53,
  9432. 0x2f, 0xcf, 0x0e, 0x24, 0x49, 0xa6, 0xb5, 0x25,
  9433. 0xb1, 0x6a, 0xed, 0xf5, 0xaa, 0x0d, 0xe6, 0x57,
  9434. 0xba, 0x63, 0x7b, 0x39
  9435. };
  9436. #if defined(WOLFSSL_AES_256) || defined(WOLFSSL_AES_192)
  9437. WOLFSSL_SMALL_STACK_STATIC const byte a[] =
  9438. {
  9439. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  9440. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  9441. 0xab, 0xad, 0xda, 0xd2
  9442. };
  9443. #endif
  9444. #ifdef WOLFSSL_AES_256
  9445. WOLFSSL_SMALL_STACK_STATIC const byte k1[] =
  9446. {
  9447. 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c,
  9448. 0x6d, 0x6a, 0x8f, 0x94, 0x67, 0x30, 0x83, 0x08,
  9449. 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c,
  9450. 0x6d, 0x6a, 0x8f, 0x94, 0x67, 0x30, 0x83, 0x08
  9451. };
  9452. WOLFSSL_SMALL_STACK_STATIC const byte iv1[] =
  9453. {
  9454. 0xca, 0xfe, 0xba, 0xbe, 0xfa, 0xce, 0xdb, 0xad,
  9455. 0xde, 0xca, 0xf8, 0x88
  9456. };
  9457. #endif /* WOLFSSL_AES_256 */
  9458. #if defined(WOLFSSL_AES_256) || defined(WOLFSSL_AES_192)
  9459. WOLFSSL_SMALL_STACK_STATIC const byte c1[] =
  9460. {
  9461. 0x52, 0x2d, 0xc1, 0xf0, 0x99, 0x56, 0x7d, 0x07,
  9462. 0xf4, 0x7f, 0x37, 0xa3, 0x2a, 0x84, 0x42, 0x7d,
  9463. 0x64, 0x3a, 0x8c, 0xdc, 0xbf, 0xe5, 0xc0, 0xc9,
  9464. 0x75, 0x98, 0xa2, 0xbd, 0x25, 0x55, 0xd1, 0xaa,
  9465. 0x8c, 0xb0, 0x8e, 0x48, 0x59, 0x0d, 0xbb, 0x3d,
  9466. 0xa7, 0xb0, 0x8b, 0x10, 0x56, 0x82, 0x88, 0x38,
  9467. 0xc5, 0xf6, 0x1e, 0x63, 0x93, 0xba, 0x7a, 0x0a,
  9468. 0xbc, 0xc9, 0xf6, 0x62
  9469. };
  9470. #endif /* WOLFSSL_AES_256 || WOLFSSL_AES_192 */
  9471. WOLFSSL_SMALL_STACK_STATIC const byte t1[] =
  9472. {
  9473. 0x76, 0xfc, 0x6e, 0xce, 0x0f, 0x4e, 0x17, 0x68,
  9474. 0xcd, 0xdf, 0x88, 0x53, 0xbb, 0x2d, 0x55, 0x1b
  9475. };
  9476. /* FIPS, QAT and PIC32MZ HW Crypto only support 12-byte IV */
  9477. #if !defined(HAVE_FIPS) && \
  9478. !defined(WOLFSSL_PIC32MZ_CRYPT) && \
  9479. !defined(FREESCALE_LTC) && !defined(FREESCALE_MMCAU) && \
  9480. !defined(WOLFSSL_XILINX_CRYPT) && !defined(WOLFSSL_AFALG_XILINX_AES) && \
  9481. !defined(WOLFSSL_SILABS_SE_ACCEL) && !defined(WOLFSSL_KCAPI_AES) && \
  9482. !(defined(WOLF_CRYPTO_CB) && \
  9483. (defined(HAVE_INTEL_QA_SYNC) || defined(HAVE_CAVIUM_OCTEON_SYNC)))
  9484. #define ENABLE_NON_12BYTE_IV_TEST
  9485. #ifdef WOLFSSL_AES_192
  9486. /* Test Case 12, uses same plaintext and AAD data. */
  9487. WOLFSSL_SMALL_STACK_STATIC const byte k2[] =
  9488. {
  9489. 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c,
  9490. 0x6d, 0x6a, 0x8f, 0x94, 0x67, 0x30, 0x83, 0x08,
  9491. 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c
  9492. };
  9493. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] =
  9494. {
  9495. 0x93, 0x13, 0x22, 0x5d, 0xf8, 0x84, 0x06, 0xe5,
  9496. 0x55, 0x90, 0x9c, 0x5a, 0xff, 0x52, 0x69, 0xaa,
  9497. 0x6a, 0x7a, 0x95, 0x38, 0x53, 0x4f, 0x7d, 0xa1,
  9498. 0xe4, 0xc3, 0x03, 0xd2, 0xa3, 0x18, 0xa7, 0x28,
  9499. 0xc3, 0xc0, 0xc9, 0x51, 0x56, 0x80, 0x95, 0x39,
  9500. 0xfc, 0xf0, 0xe2, 0x42, 0x9a, 0x6b, 0x52, 0x54,
  9501. 0x16, 0xae, 0xdb, 0xf5, 0xa0, 0xde, 0x6a, 0x57,
  9502. 0xa6, 0x37, 0xb3, 0x9b
  9503. };
  9504. WOLFSSL_SMALL_STACK_STATIC const byte c2[] =
  9505. {
  9506. 0xd2, 0x7e, 0x88, 0x68, 0x1c, 0xe3, 0x24, 0x3c,
  9507. 0x48, 0x30, 0x16, 0x5a, 0x8f, 0xdc, 0xf9, 0xff,
  9508. 0x1d, 0xe9, 0xa1, 0xd8, 0xe6, 0xb4, 0x47, 0xef,
  9509. 0x6e, 0xf7, 0xb7, 0x98, 0x28, 0x66, 0x6e, 0x45,
  9510. 0x81, 0xe7, 0x90, 0x12, 0xaf, 0x34, 0xdd, 0xd9,
  9511. 0xe2, 0xf0, 0x37, 0x58, 0x9b, 0x29, 0x2d, 0xb3,
  9512. 0xe6, 0x7c, 0x03, 0x67, 0x45, 0xfa, 0x22, 0xe7,
  9513. 0xe9, 0xb7, 0x37, 0x3b
  9514. };
  9515. WOLFSSL_SMALL_STACK_STATIC const byte t2[] =
  9516. {
  9517. 0xdc, 0xf5, 0x66, 0xff, 0x29, 0x1c, 0x25, 0xbb,
  9518. 0xb8, 0x56, 0x8f, 0xc3, 0xd3, 0x76, 0xa6, 0xd9
  9519. };
  9520. #endif /* WOLFSSL_AES_192 */
  9521. #ifdef WOLFSSL_AES_128
  9522. /* The following is an interesting test case from the example
  9523. * FIPS test vectors for AES-GCM. IVlen = 1 byte */
  9524. WOLFSSL_SMALL_STACK_STATIC const byte p3[] =
  9525. {
  9526. 0x57, 0xce, 0x45, 0x1f, 0xa5, 0xe2, 0x35, 0xa5,
  9527. 0x8e, 0x1a, 0xa2, 0x3b, 0x77, 0xcb, 0xaf, 0xe2
  9528. };
  9529. WOLFSSL_SMALL_STACK_STATIC const byte k3[] =
  9530. {
  9531. 0xbb, 0x01, 0xd7, 0x03, 0x81, 0x1c, 0x10, 0x1a,
  9532. 0x35, 0xe0, 0xff, 0xd2, 0x91, 0xba, 0xf2, 0x4b
  9533. };
  9534. WOLFSSL_SMALL_STACK_STATIC const byte iv3[] =
  9535. {
  9536. 0xca
  9537. };
  9538. WOLFSSL_SMALL_STACK_STATIC const byte c3[] =
  9539. {
  9540. 0x6b, 0x5f, 0xb3, 0x9d, 0xc1, 0xc5, 0x7a, 0x4f,
  9541. 0xf3, 0x51, 0x4d, 0xc2, 0xd5, 0xf0, 0xd0, 0x07
  9542. };
  9543. WOLFSSL_SMALL_STACK_STATIC const byte a3[] =
  9544. {
  9545. 0x40, 0xfc, 0xdc, 0xd7, 0x4a, 0xd7, 0x8b, 0xf1,
  9546. 0x3e, 0x7c, 0x60, 0x55, 0x50, 0x51, 0xdd, 0x54
  9547. };
  9548. WOLFSSL_SMALL_STACK_STATIC const byte t3[] =
  9549. {
  9550. 0x06, 0x90, 0xed, 0x01, 0x34, 0xdd, 0xc6, 0x95,
  9551. 0x31, 0x2e, 0x2a, 0xf9, 0x57, 0x7a, 0x1e, 0xa6
  9552. };
  9553. #endif /* WOLFSSL_AES_128 */
  9554. #ifdef WOLFSSL_AES_256
  9555. int ivlen;
  9556. #endif
  9557. #endif
  9558. byte resultT[sizeof(t1) + AES_BLOCK_SIZE];
  9559. byte resultP[sizeof(p) + AES_BLOCK_SIZE];
  9560. byte resultC[sizeof(p) + AES_BLOCK_SIZE];
  9561. int result = 0;
  9562. int ret;
  9563. #ifdef WOLFSSL_AES_256
  9564. #if !(defined(WOLF_CRYPTO_CB) && defined(HAVE_INTEL_QA_SYNC))
  9565. int alen;
  9566. #endif
  9567. #if !defined(WOLFSSL_AFALG_XILINX_AES) && !defined(WOLFSSL_XILINX_CRYPT)
  9568. int plen;
  9569. #endif
  9570. #endif
  9571. #if defined(WOLFSSL_XILINX_CRYPT_VERSAL)
  9572. byte buf[sizeof(p) + AES_BLOCK_SIZE];
  9573. byte bufA[sizeof(a) + 1];
  9574. byte *large_aad = (byte*)XMALLOC((size_t)1024 + 16, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9575. #endif
  9576. #if !defined(BENCH_EMBEDDED) && !defined(HAVE_CAVIUM)
  9577. #if !defined(BENCH_AESGCM_LARGE)
  9578. #define BENCH_AESGCM_LARGE 1024
  9579. #endif
  9580. #ifndef WOLFSSL_NO_MALLOC
  9581. byte *large_input = (byte *)XMALLOC(BENCH_AESGCM_LARGE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9582. byte *large_output = (byte *)XMALLOC(BENCH_AESGCM_LARGE + AES_BLOCK_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9583. byte *large_outdec = (byte *)XMALLOC(BENCH_AESGCM_LARGE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9584. if ((! large_input) || (! large_output) || (! large_outdec))
  9585. ERROR_OUT(MEMORY_E, out);
  9586. #else
  9587. byte large_input[BENCH_AESGCM_LARGE];
  9588. byte large_output[BENCH_AESGCM_LARGE];
  9589. byte large_outdec[BENCH_AESGCM_LARGE];
  9590. #endif
  9591. XMEMSET(large_input, 0, BENCH_AESGCM_LARGE);
  9592. XMEMSET(large_output, 0, BENCH_AESGCM_LARGE + AES_BLOCK_SIZE);
  9593. XMEMSET(large_outdec, 0, BENCH_AESGCM_LARGE);
  9594. #endif
  9595. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9596. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  9597. ERROR_OUT(-6342, out);
  9598. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  9599. ERROR_OUT(-6343, out);
  9600. #endif
  9601. (void)result;
  9602. XMEMSET(resultT, 0, sizeof(resultT));
  9603. XMEMSET(resultC, 0, sizeof(resultC));
  9604. XMEMSET(resultP, 0, sizeof(resultP));
  9605. if (wc_AesInit(enc, HEAP_HINT, devId) != 0) {
  9606. ERROR_OUT(-6300, out);
  9607. }
  9608. if (wc_AesInit(dec, HEAP_HINT, devId) != 0) {
  9609. ERROR_OUT(-6301, out);
  9610. }
  9611. #ifdef WOLFSSL_AES_256
  9612. result = wc_AesGcmSetKey(enc, k1, sizeof(k1));
  9613. if (result != 0)
  9614. ERROR_OUT(-6302, out);
  9615. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  9616. result = wc_AesGcmEncrypt(enc, resultC, p, sizeof(p), iv1, sizeof(iv1),
  9617. resultT, sizeof(t1), a, sizeof(a));
  9618. #if defined(WOLFSSL_ASYNC_CRYPT)
  9619. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  9620. #endif
  9621. if (result != 0)
  9622. ERROR_OUT(-6303, out);
  9623. if (XMEMCMP(c1, resultC, sizeof(c1)))
  9624. ERROR_OUT(-6304, out);
  9625. if (XMEMCMP(t1, resultT, sizeof(t1)))
  9626. ERROR_OUT(-6305, out);
  9627. #ifdef HAVE_AES_DECRYPT
  9628. result = wc_AesGcmSetKey(dec, k1, sizeof(k1));
  9629. if (result != 0)
  9630. ERROR_OUT(-6306, out);
  9631. result = wc_AesGcmDecrypt(dec, resultP, resultC, sizeof(c1),
  9632. iv1, sizeof(iv1), resultT, sizeof(t1), a, sizeof(a));
  9633. #if defined(WOLFSSL_ASYNC_CRYPT)
  9634. result = wc_AsyncWait(result, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  9635. #endif
  9636. if (result != 0)
  9637. ERROR_OUT(-6307, out);
  9638. if (XMEMCMP(p, resultP, sizeof(p)))
  9639. ERROR_OUT(-6308, out);
  9640. #endif /* HAVE_AES_DECRYPT */
  9641. /* Large buffer test */
  9642. #ifdef BENCH_AESGCM_LARGE
  9643. /* setup test buffer */
  9644. for (alen=0; alen<BENCH_AESGCM_LARGE; alen++)
  9645. large_input[alen] = (byte)alen;
  9646. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  9647. result = wc_AesGcmEncrypt(enc, large_output, large_input,
  9648. BENCH_AESGCM_LARGE, iv1, sizeof(iv1),
  9649. resultT, sizeof(t1), a, sizeof(a));
  9650. #if defined(WOLFSSL_ASYNC_CRYPT)
  9651. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  9652. #endif
  9653. if (result != 0)
  9654. ERROR_OUT(-6309, out);
  9655. #ifdef HAVE_AES_DECRYPT
  9656. result = wc_AesGcmDecrypt(dec, large_outdec, large_output,
  9657. BENCH_AESGCM_LARGE, iv1, sizeof(iv1), resultT,
  9658. sizeof(t1), a, sizeof(a));
  9659. #if defined(WOLFSSL_ASYNC_CRYPT)
  9660. result = wc_AsyncWait(result, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  9661. #endif
  9662. if (result != 0)
  9663. ERROR_OUT(-6310, out);
  9664. if (XMEMCMP(large_input, large_outdec, BENCH_AESGCM_LARGE))
  9665. ERROR_OUT(-6311, out);
  9666. #endif /* HAVE_AES_DECRYPT */
  9667. #endif /* BENCH_AESGCM_LARGE */
  9668. #if defined(ENABLE_NON_12BYTE_IV_TEST) && defined(WOLFSSL_AES_256)
  9669. /* Variable IV length test */
  9670. for (ivlen=1; ivlen<(int)sizeof(k1); ivlen++) {
  9671. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  9672. result = wc_AesGcmEncrypt(enc, resultC, p, sizeof(p), k1,
  9673. (word32)ivlen, resultT, sizeof(t1), a, sizeof(a));
  9674. #if defined(WOLFSSL_ASYNC_CRYPT)
  9675. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  9676. #endif
  9677. if (result != 0)
  9678. ERROR_OUT(-6312, out);
  9679. #ifdef HAVE_AES_DECRYPT
  9680. result = wc_AesGcmDecrypt(dec, resultP, resultC, sizeof(c1), k1,
  9681. (word32)ivlen, resultT, sizeof(t1), a, sizeof(a));
  9682. #if defined(WOLFSSL_ASYNC_CRYPT)
  9683. result = wc_AsyncWait(result, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  9684. #endif
  9685. if (result != 0)
  9686. ERROR_OUT(-6313, out);
  9687. #endif /* HAVE_AES_DECRYPT */
  9688. }
  9689. #endif
  9690. #if !(defined(WOLF_CRYPTO_CB) && defined(HAVE_INTEL_QA_SYNC))
  9691. /* Variable authenticated data length test */
  9692. for (alen=0; alen<(int)sizeof(p); alen++) {
  9693. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  9694. result = wc_AesGcmEncrypt(enc, resultC, p, sizeof(p), iv1,
  9695. sizeof(iv1), resultT, sizeof(t1), p, (word32)alen);
  9696. #if defined(WOLFSSL_ASYNC_CRYPT)
  9697. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  9698. #endif
  9699. if (result != 0)
  9700. ERROR_OUT(-6314, out);
  9701. #ifdef HAVE_AES_DECRYPT
  9702. result = wc_AesGcmDecrypt(dec, resultP, resultC, sizeof(c1), iv1,
  9703. sizeof(iv1), resultT, sizeof(t1), p, (word32)alen);
  9704. #if defined(WOLFSSL_ASYNC_CRYPT)
  9705. result = wc_AsyncWait(result, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  9706. #endif
  9707. if (result != 0)
  9708. ERROR_OUT(-6315, out);
  9709. #endif /* HAVE_AES_DECRYPT */
  9710. }
  9711. #if defined(WOLFSSL_XILINX_CRYPT_VERSAL)
  9712. if (! large_aad)
  9713. ERROR_OUT(MEMORY_E, out);
  9714. XMEMSET(large_aad, 0, 1024+16);
  9715. /* Variable authenticated data length test */
  9716. for (alen=0; alen<=1024; alen+=16) {
  9717. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  9718. result = wc_AesGcmEncrypt(enc, resultC, p, sizeof(p), iv1,
  9719. sizeof(iv1), resultT, sizeof(t1), large_aad, (word32)alen);
  9720. if (result != 0)
  9721. ERROR_OUT(-6316, out);
  9722. #ifdef HAVE_AES_DECRYPT
  9723. result = wc_AesGcmDecrypt(dec, resultP, resultC, sizeof(c1), iv1,
  9724. sizeof(iv1), resultT, sizeof(t1), large_aad, (word32)alen);
  9725. if (result != 0)
  9726. ERROR_OUT(-6317, out);
  9727. #endif /* HAVE_AES_DECRYPT */
  9728. }
  9729. /* Test unaligned memory of all potential arguments */
  9730. result = wc_AesGcmSetKey(enc, k1, sizeof(k1));
  9731. if (result != 0)
  9732. ERROR_OUT(-6318, out);
  9733. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  9734. XMEMCPY(&buf[1], p, sizeof(p));
  9735. XMEMCPY(&bufA[1], a, sizeof(a));
  9736. result = wc_AesGcmEncrypt(enc, &resultC[1], &buf[1], sizeof(p), iv1, sizeof(iv1),
  9737. &resultT[1], sizeof(t1), &bufA[1], sizeof(a));
  9738. if (result != 0)
  9739. ERROR_OUT(-6319, out);
  9740. if (XMEMCMP(c1, &resultC[1], sizeof(c1)))
  9741. ERROR_OUT(-6320, out);
  9742. if (XMEMCMP(t1, &resultT[1], sizeof(t1)))
  9743. ERROR_OUT(-6321, out);
  9744. #ifdef HAVE_AES_DECRYPT
  9745. result = wc_AesGcmSetKey(dec, k1, sizeof(k1));
  9746. if (result != 0)
  9747. ERROR_OUT(-6322, out);
  9748. result = wc_AesGcmDecrypt(dec, &resultP[1], &resultC[1], sizeof(c1),
  9749. iv1, sizeof(iv1), &resultT[1], sizeof(t1), &bufA[1], sizeof(a));
  9750. if (result != 0)
  9751. ERROR_OUT(-6323, out);
  9752. if (XMEMCMP(p, &resultP[1], sizeof(p)))
  9753. ERROR_OUT(-6324, out);
  9754. #endif /* HAVE_AES_DECRYPT */
  9755. #endif /* Xilinx Versal */
  9756. #endif
  9757. #if !defined(WOLFSSL_AFALG_XILINX_AES) && !defined(WOLFSSL_XILINX_CRYPT)
  9758. #ifdef BENCH_AESGCM_LARGE
  9759. /* Variable plain text length test */
  9760. for (plen=1; plen<BENCH_AESGCM_LARGE; plen++) {
  9761. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  9762. result = wc_AesGcmEncrypt(enc, large_output, large_input,
  9763. plen, iv1, sizeof(iv1), resultT,
  9764. sizeof(t1), a, sizeof(a));
  9765. #if defined(WOLFSSL_ASYNC_CRYPT)
  9766. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  9767. #endif
  9768. if (result != 0)
  9769. ERROR_OUT(-6316, out);
  9770. #ifdef HAVE_AES_DECRYPT
  9771. result = wc_AesGcmDecrypt(dec, large_outdec, large_output,
  9772. plen, iv1, sizeof(iv1), resultT,
  9773. sizeof(t1), a, sizeof(a));
  9774. #if defined(WOLFSSL_ASYNC_CRYPT)
  9775. result = wc_AsyncWait(result, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  9776. #endif
  9777. if (result != 0)
  9778. ERROR_OUT(-6317, out);
  9779. #endif /* HAVE_AES_DECRYPT */
  9780. }
  9781. #else /* BENCH_AESGCM_LARGE */
  9782. /* Variable plain text length test */
  9783. for (plen=1; plen<(int)sizeof(p); plen++) {
  9784. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  9785. result = wc_AesGcmEncrypt(enc, resultC, p, (word32)plen, iv1,
  9786. sizeof(iv1), resultT, sizeof(t1), a, sizeof(a));
  9787. #if defined(WOLFSSL_ASYNC_CRYPT)
  9788. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  9789. #endif
  9790. if (result != 0)
  9791. ERROR_OUT(-6318, out);
  9792. #ifdef HAVE_AES_DECRYPT
  9793. result = wc_AesGcmDecrypt(dec, resultP, resultC, (word32)plen, iv1,
  9794. sizeof(iv1), resultT, sizeof(t1), a, sizeof(a));
  9795. #if defined(WOLFSSL_ASYNC_CRYPT)
  9796. result = wc_AsyncWait(result, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  9797. #endif
  9798. if (result != 0)
  9799. ERROR_OUT(-6319, out);
  9800. #endif /* HAVE_AES_DECRYPT */
  9801. }
  9802. #endif /* BENCH_AESGCM_LARGE */
  9803. #endif
  9804. #endif /* WOLFSSL_AES_256 */
  9805. /* test with IV != 12 bytes */
  9806. #ifdef ENABLE_NON_12BYTE_IV_TEST
  9807. XMEMSET(resultT, 0, sizeof(resultT));
  9808. XMEMSET(resultC, 0, sizeof(resultC));
  9809. XMEMSET(resultP, 0, sizeof(resultP));
  9810. #ifdef WOLFSSL_AES_192
  9811. wc_AesGcmSetKey(enc, k2, sizeof(k2));
  9812. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  9813. result = wc_AesGcmEncrypt(enc, resultC, p, sizeof(p), iv2, sizeof(iv2),
  9814. resultT, sizeof(t1), a, sizeof(a));
  9815. #if defined(WOLFSSL_ASYNC_CRYPT)
  9816. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  9817. #endif
  9818. if (result != 0)
  9819. ERROR_OUT(-6320, out);
  9820. if (XMEMCMP(c2, resultC, sizeof(c2)))
  9821. ERROR_OUT(-6321, out);
  9822. if (XMEMCMP(t2, resultT, sizeof(t1)))
  9823. ERROR_OUT(-6322, out);
  9824. #ifdef HAVE_AES_DECRYPT
  9825. result = wc_AesGcmDecrypt(enc, resultP, resultC, sizeof(c1),
  9826. iv2, sizeof(iv2), resultT, sizeof(t1), a, sizeof(a));
  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(-6323, out);
  9832. if (XMEMCMP(p, resultP, sizeof(p)))
  9833. ERROR_OUT(-6324, out);
  9834. #endif /* HAVE_AES_DECRYPT */
  9835. XMEMSET(resultT, 0, sizeof(resultT));
  9836. XMEMSET(resultC, 0, sizeof(resultC));
  9837. XMEMSET(resultP, 0, sizeof(resultP));
  9838. #endif /* WOLFSSL_AES_192 */
  9839. #ifdef WOLFSSL_AES_128
  9840. wc_AesGcmSetKey(enc, k3, sizeof(k3));
  9841. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  9842. result = wc_AesGcmEncrypt(enc, resultC, p3, sizeof(p3), iv3, sizeof(iv3),
  9843. resultT, sizeof(t3), a3, sizeof(a3));
  9844. #if defined(WOLFSSL_ASYNC_CRYPT)
  9845. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  9846. #endif
  9847. if (result != 0)
  9848. ERROR_OUT(-6325, out);
  9849. if (XMEMCMP(c3, resultC, sizeof(c3)))
  9850. ERROR_OUT(-6326, out);
  9851. if (XMEMCMP(t3, resultT, sizeof(t3)))
  9852. ERROR_OUT(-6327, out);
  9853. #ifdef HAVE_AES_DECRYPT
  9854. result = wc_AesGcmDecrypt(enc, resultP, resultC, sizeof(c3),
  9855. iv3, sizeof(iv3), resultT, sizeof(t3), a3, sizeof(a3));
  9856. #if defined(WOLFSSL_ASYNC_CRYPT)
  9857. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  9858. #endif
  9859. if (result != 0)
  9860. ERROR_OUT(-6328, out);
  9861. if (XMEMCMP(p3, resultP, sizeof(p3)))
  9862. ERROR_OUT(-6329, out);
  9863. #endif /* HAVE_AES_DECRYPT */
  9864. #endif /* WOLFSSL_AES_128 */
  9865. #endif /* ENABLE_NON_12BYTE_IV_TEST */
  9866. #if defined(WOLFSSL_AES_256) && !defined(WOLFSSL_AFALG_XILINX_AES) && \
  9867. !defined(WOLFSSL_XILINX_CRYPT) && \
  9868. !(defined(WOLF_CRYPTO_CB) && \
  9869. defined(HAVE_INTEL_QA_SYNC) || defined(HAVE_CAVIUM_OCTEON_SYNC))
  9870. XMEMSET(resultT, 0, sizeof(resultT));
  9871. XMEMSET(resultC, 0, sizeof(resultC));
  9872. XMEMSET(resultP, 0, sizeof(resultP));
  9873. wc_AesGcmSetKey(enc, k1, sizeof(k1));
  9874. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  9875. result = wc_AesGcmEncrypt(enc, resultC, p, sizeof(p), iv1, sizeof(iv1),
  9876. resultT + 1, sizeof(t1) - 1, a, sizeof(a));
  9877. #if defined(WOLFSSL_ASYNC_CRYPT)
  9878. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  9879. #endif
  9880. if (result != 0)
  9881. ERROR_OUT(-6330, out);
  9882. if (XMEMCMP(c1, resultC, sizeof(c1)))
  9883. ERROR_OUT(-6331, out);
  9884. if (XMEMCMP(t1, resultT + 1, sizeof(t1) - 1))
  9885. ERROR_OUT(-6332, out);
  9886. #ifdef HAVE_AES_DECRYPT
  9887. result = wc_AesGcmDecrypt(enc, resultP, resultC, sizeof(p),
  9888. iv1, sizeof(iv1), resultT + 1, sizeof(t1) - 1, a, sizeof(a));
  9889. #if defined(WOLFSSL_ASYNC_CRYPT)
  9890. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  9891. #endif
  9892. if (result != 0)
  9893. ERROR_OUT(-6333, out);
  9894. if (XMEMCMP(p, resultP, sizeof(p)))
  9895. ERROR_OUT(-6334, out);
  9896. #endif /* HAVE_AES_DECRYPT */
  9897. #endif /* WOLFSSL_AES_256 */
  9898. #if !defined(HAVE_FIPS) || \
  9899. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))
  9900. /* Test encrypt with internally generated IV */
  9901. #if defined(WOLFSSL_AES_256) && !(defined(WC_NO_RNG) || defined(HAVE_SELFTEST)) \
  9902. && !(defined(WOLF_CRYPTO_CB) && defined(HAVE_CAVIUM_OCTEON_SYNC))
  9903. {
  9904. WC_RNG rng;
  9905. byte randIV[12];
  9906. result = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  9907. if (result != 0)
  9908. ERROR_OUT(-6335, out);
  9909. XMEMSET(randIV, 0, sizeof(randIV));
  9910. XMEMSET(resultT, 0, sizeof(resultT));
  9911. XMEMSET(resultC, 0, sizeof(resultC));
  9912. XMEMSET(resultP, 0, sizeof(resultP));
  9913. wc_AesGcmSetKey(enc, k1, sizeof(k1));
  9914. result = wc_AesGcmSetIV(enc, sizeof(randIV), NULL, 0, &rng);
  9915. if (result != 0)
  9916. ERROR_OUT(-6336, out);
  9917. result = wc_AesGcmEncrypt_ex(enc,
  9918. resultC, p, sizeof(p),
  9919. randIV, sizeof(randIV),
  9920. resultT, sizeof(t1),
  9921. a, sizeof(a));
  9922. #if defined(WOLFSSL_ASYNC_CRYPT)
  9923. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  9924. #endif
  9925. if (result != 0)
  9926. ERROR_OUT(-6337, out);
  9927. /* Check the IV has been set. */
  9928. {
  9929. word32 i, ivSum = 0;
  9930. for (i = 0; i < sizeof(randIV); i++)
  9931. ivSum += randIV[i];
  9932. if (ivSum == 0)
  9933. ERROR_OUT(-6338, out);
  9934. }
  9935. #ifdef HAVE_AES_DECRYPT
  9936. wc_AesGcmSetKey(dec, k1, sizeof(k1));
  9937. result = wc_AesGcmSetIV(dec, sizeof(randIV), NULL, 0, &rng);
  9938. if (result != 0)
  9939. ERROR_OUT(-6339, out);
  9940. result = wc_AesGcmDecrypt(dec,
  9941. resultP, resultC, sizeof(c1),
  9942. randIV, sizeof(randIV),
  9943. resultT, sizeof(t1),
  9944. a, sizeof(a));
  9945. #if defined(WOLFSSL_ASYNC_CRYPT)
  9946. result = wc_AsyncWait(result, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  9947. #endif
  9948. if (result != 0)
  9949. ERROR_OUT(-6340, out);
  9950. if (XMEMCMP(p, resultP, sizeof(p)))
  9951. ERROR_OUT(-6341, out);
  9952. #endif /* HAVE_AES_DECRYPT */
  9953. wc_FreeRng(&rng);
  9954. }
  9955. #endif /* WOLFSSL_AES_256 && !(WC_NO_RNG || HAVE_SELFTEST) */
  9956. #endif /* HAVE_FIPS_VERSION >= 2 */
  9957. #if !defined(WOLFSSL_AFALG_XILINX_AES) && !defined(WOLFSSL_XILINX_CRYPT)
  9958. #ifdef WOLFSSL_AES_256
  9959. #ifdef WOLFSSL_AESGCM_STREAM
  9960. result = wc_AesGcmEncryptInit(enc, k1, sizeof(k1), iv1, sizeof(iv1));
  9961. if (result != 0)
  9962. ERROR_OUT(-6360, out);
  9963. result = wc_AesGcmEncryptUpdate(enc, resultC, p, sizeof(p), a, sizeof(a));
  9964. if (result != 0)
  9965. ERROR_OUT(-6361, out);
  9966. result = wc_AesGcmEncryptFinal(enc, resultT, sizeof(t1));
  9967. if (result != 0)
  9968. ERROR_OUT(-6362, out);
  9969. if (XMEMCMP(resultC, c1, sizeof(c1)) != 0)
  9970. ERROR_OUT(-6363, out);
  9971. if (XMEMCMP(resultT, t1, sizeof(t1)) != 0)
  9972. ERROR_OUT(-6364, out);
  9973. #ifdef HAVE_AES_DECRYPT
  9974. result = wc_AesGcmDecryptInit(enc, k1, sizeof(k1), iv1, sizeof(iv1));
  9975. if (result != 0)
  9976. ERROR_OUT(-6370, out);
  9977. result = wc_AesGcmDecryptUpdate(enc, resultP, c1, sizeof(c1), a, sizeof(a));
  9978. if (result != 0)
  9979. ERROR_OUT(-6371, out);
  9980. result = wc_AesGcmDecryptFinal(enc, t1, sizeof(t1));
  9981. if (result != 0)
  9982. ERROR_OUT(-6372, out);
  9983. if (XMEMCMP(resultP, p, sizeof(p)) != 0)
  9984. ERROR_OUT(-6373, out);
  9985. #endif
  9986. /* alen is the size to pass in with each update. */
  9987. for (alen = 1; alen < AES_BLOCK_SIZE + 1; alen++) {
  9988. result = wc_AesGcmEncryptInit(enc, k1, sizeof(k1), iv1, sizeof(iv1));
  9989. if (result != 0)
  9990. ERROR_OUT(-6380, out);
  9991. /* plen is the offset into AAD to update with. */
  9992. for (plen = 0; plen < (int)sizeof(a); plen += alen) {
  9993. int len = sizeof(a) - plen;
  9994. if (len > alen) len = alen;
  9995. result = wc_AesGcmEncryptUpdate(enc, NULL, NULL, 0, a + plen, len);
  9996. if (result != 0)
  9997. ERROR_OUT(-6381, out);
  9998. }
  9999. /* plen is the offset into plaintext to update with. */
  10000. for (plen = 0; plen < (int)sizeof(p); plen += alen) {
  10001. int len = sizeof(p) - plen;
  10002. if (len > alen) len = alen;
  10003. result = wc_AesGcmEncryptUpdate(enc, resultC + plen, p + plen, len,
  10004. NULL, 0);
  10005. if (result != 0)
  10006. ERROR_OUT(-6382, out);
  10007. }
  10008. result = wc_AesGcmEncryptFinal(enc, resultT, sizeof(t1));
  10009. if (result != 0)
  10010. ERROR_OUT(-6383, out);
  10011. if (XMEMCMP(resultC, c1, sizeof(c1)) != 0)
  10012. ERROR_OUT(-6384, out);
  10013. if (XMEMCMP(resultT, t1, sizeof(t1)) != 0)
  10014. ERROR_OUT(-6385, out);
  10015. }
  10016. #ifdef HAVE_AES_DECRYPT
  10017. for (alen = 1; alen < AES_BLOCK_SIZE + 1; alen++) {
  10018. result = wc_AesGcmDecryptInit(enc, k1, sizeof(k1), iv1, sizeof(iv1));
  10019. if (result != 0)
  10020. ERROR_OUT(-6390, out);
  10021. /* plen is the offset into AAD to update with. */
  10022. for (plen = 0; plen < (int)sizeof(a); plen += alen) {
  10023. int len = sizeof(a) - plen;
  10024. if (len > alen) len = alen;
  10025. result = wc_AesGcmDecryptUpdate(enc, NULL, NULL, 0, a + plen, len);
  10026. if (result != 0)
  10027. ERROR_OUT(-6391, out);
  10028. }
  10029. /* plen is the offset into cipher text to update with. */
  10030. for (plen = 0; plen < (int)sizeof(c1); plen += alen) {
  10031. int len = sizeof(c1) - plen;
  10032. if (len > alen) len = alen;
  10033. result = wc_AesGcmDecryptUpdate(enc, resultP + plen, c1 + plen, len,
  10034. NULL, 0);
  10035. if (result != 0)
  10036. ERROR_OUT(-6392, out);
  10037. }
  10038. result = wc_AesGcmDecryptFinal(enc, t1, sizeof(t1));
  10039. if (result != 0)
  10040. ERROR_OUT(-6393, out);
  10041. if (XMEMCMP(resultP, p, sizeof(p)) != 0)
  10042. ERROR_OUT(-6394, out);
  10043. }
  10044. #endif /* HAVE_AES_DECRYPT */
  10045. #ifdef BENCH_AESGCM_LARGE
  10046. /* setup test buffer */
  10047. result = wc_AesGcmEncryptInit(enc, k1, sizeof(k1), iv1, sizeof(iv1));
  10048. if (result != 0)
  10049. ERROR_OUT(-6360, out);
  10050. result = wc_AesGcmEncryptUpdate(enc, large_output, large_input,
  10051. BENCH_AESGCM_LARGE, a, sizeof(a));
  10052. if (result != 0)
  10053. ERROR_OUT(-6361, out);
  10054. result = wc_AesGcmEncryptFinal(enc, resultT, sizeof(t1));
  10055. if (result != 0)
  10056. ERROR_OUT(-6362, out);
  10057. #ifdef HAVE_AES_DECRYPT
  10058. result = wc_AesGcmDecryptInit(enc, k1, sizeof(k1), iv1, sizeof(iv1));
  10059. if (result != 0)
  10060. ERROR_OUT(-6363, out);
  10061. result = wc_AesGcmDecryptUpdate(enc, large_outdec, large_output,
  10062. BENCH_AESGCM_LARGE, a, sizeof(a));
  10063. if (result != 0)
  10064. ERROR_OUT(-6364, out);
  10065. result = wc_AesGcmDecryptFinal(enc, resultT, sizeof(t1));
  10066. if (result != 0)
  10067. ERROR_OUT(-6365, out);
  10068. if (XMEMCMP(large_input, large_outdec, BENCH_AESGCM_LARGE))
  10069. ERROR_OUT(-6366, out);
  10070. #endif /* HAVE_AES_DECRYPT */
  10071. #endif /* BENCH_AESGCM_LARGE */
  10072. #endif /* WOLFSSL_AESGCM_STREAM */
  10073. #endif /* WOLFSSL_AES_256 */
  10074. #endif /* !WOLFSSL_AFALG_XILINX_AES && !WOLFSSL_XILINX_CRYPT */
  10075. wc_AesFree(enc);
  10076. wc_AesFree(dec);
  10077. ret = 0;
  10078. out:
  10079. #if !defined(BENCH_EMBEDDED) && !defined(HAVE_CAVIUM) && \
  10080. !defined(WOLFSSL_NO_MALLOC)
  10081. if (large_input)
  10082. XFREE(large_input, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10083. if (large_output)
  10084. XFREE(large_output, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10085. if (large_outdec)
  10086. XFREE(large_outdec, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10087. #endif
  10088. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10089. if (enc)
  10090. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  10091. if (dec)
  10092. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  10093. #endif
  10094. return ret;
  10095. }
  10096. #ifdef WOLFSSL_AES_128
  10097. WOLFSSL_TEST_SUBROUTINE int gmac_test(void)
  10098. {
  10099. int ret;
  10100. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10101. Gmac *gmac;
  10102. #else
  10103. Gmac gmac[1];
  10104. #endif
  10105. WOLFSSL_SMALL_STACK_STATIC const byte k1[] =
  10106. {
  10107. 0x89, 0xc9, 0x49, 0xe9, 0xc8, 0x04, 0xaf, 0x01,
  10108. 0x4d, 0x56, 0x04, 0xb3, 0x94, 0x59, 0xf2, 0xc8
  10109. };
  10110. WOLFSSL_SMALL_STACK_STATIC const byte iv1[] =
  10111. {
  10112. 0xd1, 0xb1, 0x04, 0xc8, 0x15, 0xbf, 0x1e, 0x94,
  10113. 0xe2, 0x8c, 0x8f, 0x16
  10114. };
  10115. WOLFSSL_SMALL_STACK_STATIC const byte a1[] =
  10116. {
  10117. 0x82, 0xad, 0xcd, 0x63, 0x8d, 0x3f, 0xa9, 0xd9,
  10118. 0xf3, 0xe8, 0x41, 0x00, 0xd6, 0x1e, 0x07, 0x77
  10119. };
  10120. WOLFSSL_SMALL_STACK_STATIC const byte t1[] =
  10121. {
  10122. 0x88, 0xdb, 0x9d, 0x62, 0x17, 0x2e, 0xd0, 0x43,
  10123. 0xaa, 0x10, 0xf1, 0x6d, 0x22, 0x7d, 0xc4, 0x1b
  10124. };
  10125. #if (!defined(HAVE_FIPS) || \
  10126. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)))
  10127. /* FIPS builds only allow 16-byte auth tags. */
  10128. /* This sample uses a 15-byte auth tag. */
  10129. WOLFSSL_SMALL_STACK_STATIC const byte k2[] =
  10130. {
  10131. 0x40, 0xf7, 0xec, 0xb2, 0x52, 0x6d, 0xaa, 0xd4,
  10132. 0x74, 0x25, 0x1d, 0xf4, 0x88, 0x9e, 0xf6, 0x5b
  10133. };
  10134. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] =
  10135. {
  10136. 0xee, 0x9c, 0x6e, 0x06, 0x15, 0x45, 0x45, 0x03,
  10137. 0x1a, 0x60, 0x24, 0xa7
  10138. };
  10139. WOLFSSL_SMALL_STACK_STATIC const byte a2[] =
  10140. {
  10141. 0x94, 0x81, 0x2c, 0x87, 0x07, 0x4e, 0x15, 0x18,
  10142. 0x34, 0xb8, 0x35, 0xaf, 0x1c, 0xa5, 0x7e, 0x56
  10143. };
  10144. WOLFSSL_SMALL_STACK_STATIC const byte t2[] =
  10145. {
  10146. 0xc6, 0x81, 0x79, 0x8e, 0x3d, 0xda, 0xb0, 0x9f,
  10147. 0x8d, 0x83, 0xb0, 0xbb, 0x14, 0xb6, 0x91
  10148. };
  10149. #endif
  10150. byte tag[16];
  10151. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10152. if ((gmac = (Gmac *)XMALLOC(sizeof *gmac, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  10153. return -6409;
  10154. #endif
  10155. XMEMSET(gmac, 0, sizeof *gmac); /* clear context */
  10156. (void)wc_AesInit(&gmac->aes, HEAP_HINT, INVALID_DEVID); /* Make sure devId updated */
  10157. XMEMSET(tag, 0, sizeof(tag));
  10158. wc_GmacSetKey(gmac, k1, sizeof(k1));
  10159. wc_GmacUpdate(gmac, iv1, sizeof(iv1), a1, sizeof(a1), tag, sizeof(t1));
  10160. if (XMEMCMP(t1, tag, sizeof(t1)) != 0)
  10161. ERROR_OUT(-6400, out);
  10162. #if (!defined(HAVE_FIPS) || \
  10163. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)) )
  10164. XMEMSET(tag, 0, sizeof(tag));
  10165. wc_GmacSetKey(gmac, k2, sizeof(k2));
  10166. wc_GmacUpdate(gmac, iv2, sizeof(iv2), a2, sizeof(a2), tag, sizeof(t2));
  10167. if (XMEMCMP(t2, tag, sizeof(t2)) != 0)
  10168. ERROR_OUT(-6401, out);
  10169. #if !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && !defined(NO_AES_DECRYPT)
  10170. {
  10171. WOLFSSL_SMALL_STACK_STATIC const byte badT[] =
  10172. {
  10173. 0xde, 0xad, 0xbe, 0xef, 0x17, 0x2e, 0xd0, 0x43,
  10174. 0xaa, 0x10, 0xf1, 0x6d, 0x22, 0x7d, 0xc4, 0x1b
  10175. };
  10176. WC_RNG rng;
  10177. byte iv[12];
  10178. #ifndef HAVE_FIPS
  10179. if (wc_InitRng_ex(&rng, HEAP_HINT, devId) != 0)
  10180. ERROR_OUT(-6402, out);
  10181. #else
  10182. if (wc_InitRng(&rng) != 0)
  10183. ERROR_OUT(-6403, out);
  10184. #endif
  10185. if (wc_GmacVerify(k1, sizeof(k1), iv1, sizeof(iv1), a1, sizeof(a1),
  10186. t1, sizeof(t1)) != 0)
  10187. ERROR_OUT(-6404, out);
  10188. if (wc_GmacVerify(k1, sizeof(k1), iv1, sizeof(iv1), a1, sizeof(a1),
  10189. badT, sizeof(badT)) != AES_GCM_AUTH_E)
  10190. ERROR_OUT(-6405, out);
  10191. if (wc_GmacVerify(k2, sizeof(k2), iv2, sizeof(iv2), a2, sizeof(a2),
  10192. t2, sizeof(t2)) != 0)
  10193. ERROR_OUT(-6406, out);
  10194. XMEMSET(tag, 0, sizeof(tag));
  10195. XMEMSET(iv, 0, sizeof(iv));
  10196. if (wc_Gmac(k1, sizeof(k1), iv, sizeof(iv), a1, sizeof(a1),
  10197. tag, sizeof(tag), &rng) != 0)
  10198. ERROR_OUT(-6407, out);
  10199. if (wc_GmacVerify(k1, sizeof(k1), iv, sizeof(iv), a1, sizeof(a1),
  10200. tag, sizeof(tag)) != 0)
  10201. ERROR_OUT(-6408, out);
  10202. wc_FreeRng(&rng);
  10203. }
  10204. #endif /* !WC_NO_RNG && !HAVE_SELFTEST && !NO_AES_DECRYPT */
  10205. #endif /* HAVE_FIPS */
  10206. ret = 0;
  10207. out:
  10208. wc_AesFree(&gmac->aes);
  10209. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10210. XFREE(gmac, HEAP_HINT, DYNAMIC_TYPE_AES);
  10211. #endif
  10212. return ret;
  10213. }
  10214. #endif /* WOLFSSL_AES_128 */
  10215. #endif /* HAVE_AESGCM */
  10216. #if defined(HAVE_AESCCM)
  10217. #if defined(WOLFSSL_AES_256)
  10218. static int aesccm_256_test(void)
  10219. {
  10220. int ret;
  10221. /* Test vectors from NIST AES CCM 256-bit CAST Example #1 */
  10222. WOLFSSL_SMALL_STACK_STATIC const byte in_key[32] = {
  10223. 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
  10224. 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F,
  10225. 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  10226. 0x58, 0x59, 0x5A, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F
  10227. };
  10228. WOLFSSL_SMALL_STACK_STATIC const byte in_nonce[7] = {
  10229. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16};
  10230. WOLFSSL_SMALL_STACK_STATIC const byte in_auth[8] = {
  10231. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
  10232. WOLFSSL_SMALL_STACK_STATIC const byte in_plaintext[4] = {
  10233. 0x20, 0x21, 0x22, 0x23};
  10234. WOLFSSL_SMALL_STACK_STATIC const byte exp_ciphertext[4] = {
  10235. 0x8A, 0xB1, 0xA8, 0x74};
  10236. WOLFSSL_SMALL_STACK_STATIC const byte exp_tag[4] = {
  10237. 0x95, 0xFC, 0x08, 0x20};
  10238. byte output[sizeof(in_plaintext)];
  10239. byte atag[sizeof(exp_tag)];
  10240. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10241. Aes* aes = (Aes*)XMALLOC(sizeof(Aes), HEAP_HINT, DYNAMIC_TYPE_AES);
  10242. if (aes == NULL) {
  10243. return MEMORY_E;
  10244. }
  10245. #else
  10246. Aes aes[1];
  10247. #endif
  10248. ret = wc_AesInit(aes, HEAP_HINT, devId);
  10249. if (ret == 0) {
  10250. ret = wc_AesCcmSetKey(aes, in_key, sizeof(in_key));
  10251. }
  10252. if (ret == 0) {
  10253. ret = wc_AesCcmEncrypt(aes, output, in_plaintext, sizeof(in_plaintext),
  10254. in_nonce, sizeof(in_nonce),
  10255. atag, sizeof(atag),
  10256. in_auth, sizeof(in_auth));
  10257. }
  10258. /* Verify we produce the proper ciphertext and tag */
  10259. if (ret == 0 &&
  10260. (XMEMCMP(output, exp_ciphertext, sizeof(output)) ||
  10261. XMEMCMP(atag, exp_tag, sizeof(atag)))) {
  10262. ret = -1;
  10263. }
  10264. if (ret == 0) {
  10265. /* decrypt inline */
  10266. ret = wc_AesCcmDecrypt(aes, output, output, sizeof(output),
  10267. in_nonce, sizeof(in_nonce),
  10268. atag, sizeof(atag),
  10269. in_auth, sizeof(in_auth));
  10270. }
  10271. /* Verify decryption was successful */
  10272. if (ret == 0 &&
  10273. XMEMCMP(output, in_plaintext, sizeof(output))) {
  10274. ret = -1;
  10275. }
  10276. wc_AesFree(aes);
  10277. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10278. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_AES);
  10279. #endif
  10280. return ret;
  10281. }
  10282. #endif /* WOLFSSL_AES_256 */
  10283. #if defined(WOLFSSL_AES_128)
  10284. static int aesccm_128_test(void)
  10285. {
  10286. int ret;
  10287. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10288. Aes *enc;
  10289. #else
  10290. Aes enc[1];
  10291. #endif
  10292. /* key */
  10293. WOLFSSL_SMALL_STACK_STATIC const byte k[] =
  10294. {
  10295. 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
  10296. 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf
  10297. };
  10298. /* nonce */
  10299. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  10300. {
  10301. 0x00, 0x00, 0x00, 0x03, 0x02, 0x01, 0x00, 0xa0,
  10302. 0xa1, 0xa2, 0xa3, 0xa4, 0xa5
  10303. };
  10304. /* plaintext */
  10305. WOLFSSL_SMALL_STACK_STATIC const byte p[] =
  10306. {
  10307. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  10308. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  10309. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e
  10310. };
  10311. /* plaintext - long */
  10312. WOLFSSL_SMALL_STACK_STATIC const byte pl[] =
  10313. {
  10314. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  10315. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  10316. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
  10317. 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  10318. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
  10319. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  10320. 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
  10321. 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
  10322. 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
  10323. 0x50
  10324. };
  10325. WOLFSSL_SMALL_STACK_STATIC const byte a[] =
  10326. {
  10327. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
  10328. };
  10329. /* ciphertext */
  10330. WOLFSSL_SMALL_STACK_STATIC const byte c[] =
  10331. {
  10332. 0x58, 0x8c, 0x97, 0x9a, 0x61, 0xc6, 0x63, 0xd2,
  10333. 0xf0, 0x66, 0xd0, 0xc2, 0xc0, 0xf9, 0x89, 0x80,
  10334. 0x6d, 0x5f, 0x6b, 0x61, 0xda, 0xc3, 0x84
  10335. };
  10336. /* tag - authentication */
  10337. WOLFSSL_SMALL_STACK_STATIC const byte t[] =
  10338. {
  10339. 0x17, 0xe8, 0xd1, 0x2c, 0xfd, 0xf9, 0x26, 0xe0
  10340. };
  10341. /* ciphertext - long */
  10342. WOLFSSL_SMALL_STACK_STATIC const byte cl[] =
  10343. {
  10344. 0x58, 0x8c, 0x97, 0x9a, 0x61, 0xc6, 0x63, 0xd2,
  10345. 0xf0, 0x66, 0xd0, 0xc2, 0xc0, 0xf9, 0x89, 0x80,
  10346. 0x6d, 0x5f, 0x6b, 0x61, 0xda, 0xc3, 0x84, 0xe0,
  10347. 0x44, 0x2d, 0xbe, 0x25, 0xfa, 0x48, 0x2b, 0xa8,
  10348. 0x36, 0x0b, 0xbf, 0x01, 0xc0, 0x12, 0x45, 0xa4,
  10349. 0x82, 0x9f, 0x20, 0x6c, 0xc3, 0xd6, 0xae, 0x5b,
  10350. 0x54, 0x8d, 0xd0, 0xb1, 0x69, 0x2c, 0xec, 0x5e,
  10351. 0x95, 0xa5, 0x6b, 0x48, 0xc3, 0xc6, 0xc8, 0x9e,
  10352. 0xc7, 0x92, 0x98, 0x9d, 0x26, 0x7d, 0x2a, 0x10,
  10353. 0x0b
  10354. };
  10355. /* tag - authentication - long */
  10356. WOLFSSL_SMALL_STACK_STATIC const byte tl[] =
  10357. {
  10358. 0x89, 0xd8, 0xd2, 0x02, 0xc5, 0xcf, 0xae, 0xf4
  10359. };
  10360. /* tag - authentication - empty plaintext */
  10361. WOLFSSL_SMALL_STACK_STATIC const byte t_empty[] =
  10362. {
  10363. 0xe4, 0x28, 0x8a, 0xc3, 0x78, 0x00, 0x0f, 0xf5
  10364. };
  10365. byte t2[sizeof(t)];
  10366. byte p2[sizeof(p)];
  10367. byte c2[sizeof(c)];
  10368. byte iv2[sizeof(iv)];
  10369. byte pl2[sizeof(pl)];
  10370. byte cl2[sizeof(cl)];
  10371. byte tl2[sizeof(tl)];
  10372. byte t_empty2[sizeof(t_empty)];
  10373. int result;
  10374. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10375. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  10376. return -6521;
  10377. #endif
  10378. XMEMSET(enc, 0, sizeof *enc); /* clear context */
  10379. XMEMSET(t2, 0, sizeof(t2));
  10380. XMEMSET(c2, 0, sizeof(c2));
  10381. XMEMSET(p2, 0, sizeof(p2));
  10382. result = wc_AesInit(enc, HEAP_HINT, devId);
  10383. if (result != 0)
  10384. ERROR_OUT(-6499, out);
  10385. result = wc_AesCcmSetKey(enc, k, sizeof(k));
  10386. if (result != 0)
  10387. ERROR_OUT(-6500, out);
  10388. /* AES-CCM encrypt and decrypt both use AES encrypt internally */
  10389. result = wc_AesCcmEncrypt(enc, c2, p, sizeof(c2), iv, sizeof(iv),
  10390. t2, sizeof(t2), a, sizeof(a));
  10391. if (result != 0)
  10392. ERROR_OUT(-6501, out);
  10393. if (XMEMCMP(c, c2, sizeof(c2)))
  10394. ERROR_OUT(-6502, out);
  10395. if (XMEMCMP(t, t2, sizeof(t2)))
  10396. ERROR_OUT(-6503, out);
  10397. result = wc_AesCcmDecrypt(enc, p2, c2, sizeof(p2), iv, sizeof(iv),
  10398. t2, sizeof(t2), a, sizeof(a));
  10399. if (result != 0)
  10400. ERROR_OUT(-6504, out);
  10401. if (XMEMCMP(p, p2, sizeof(p2)))
  10402. ERROR_OUT(-6505, out);
  10403. /* Test the authentication failure */
  10404. t2[0]++; /* Corrupt the authentication tag. */
  10405. result = wc_AesCcmDecrypt(enc, p2, c, sizeof(p2), iv, sizeof(iv),
  10406. t2, sizeof(t2), a, sizeof(a));
  10407. if (result == 0)
  10408. ERROR_OUT(-6506, out);
  10409. /* Clear c2 to compare against p2. p2 should be set to zero in case of
  10410. * authentication fail. */
  10411. XMEMSET(c2, 0, sizeof(c2));
  10412. if (XMEMCMP(p2, c2, sizeof(p2)))
  10413. ERROR_OUT(-6507, out);
  10414. wc_AesFree(enc);
  10415. XMEMSET(enc, 0, sizeof(Aes)); /* clear context */
  10416. XMEMSET(t2, 0, sizeof(t2));
  10417. XMEMSET(c2, 0, sizeof(c2));
  10418. XMEMSET(p2, 0, sizeof(p2));
  10419. XMEMSET(iv2, 0, sizeof(iv2));
  10420. #ifndef HAVE_SELFTEST
  10421. /* selftest build does not have wc_AesCcmSetNonce() or
  10422. * wc_AesCcmEncrypt_ex() */
  10423. if (wc_AesCcmSetKey(enc, k, sizeof(k)) != 0)
  10424. ERROR_OUT(-6508, out);
  10425. if (wc_AesCcmSetNonce(enc, iv, sizeof(iv)) != 0)
  10426. ERROR_OUT(-6509, out);
  10427. if (wc_AesCcmEncrypt_ex(enc, c2, p, sizeof(c2), iv2, sizeof(iv2),
  10428. t2, sizeof(t2), a, sizeof(a)) != 0)
  10429. ERROR_OUT(-6510, out);
  10430. if (XMEMCMP(iv, iv2, sizeof(iv2)))
  10431. ERROR_OUT(-6511, out);
  10432. if (XMEMCMP(c, c2, sizeof(c2)))
  10433. ERROR_OUT(-6512, out);
  10434. if (XMEMCMP(t, t2, sizeof(t2)))
  10435. ERROR_OUT(-6513, out);
  10436. #endif
  10437. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  10438. /* test fail on invalid IV sizes */
  10439. result = wc_AesCcmSetKey(enc, k, sizeof(k));
  10440. if (result != 0)
  10441. ERROR_OUT(-6514, out);
  10442. /* AES-CCM encrypt and decrypt both use AES encrypt internally */
  10443. result = wc_AesCcmEncrypt(enc, c2, p, sizeof(c2), iv, sizeof(iv),
  10444. t2, 1, a, sizeof(a));
  10445. if (result == 0) {
  10446. ERROR_OUT(-6515, out);
  10447. }
  10448. #endif
  10449. /* AES-CCM encrypt and decrypt both use AES encrypt internally */
  10450. result = wc_AesCcmEncrypt(enc, cl2, pl, sizeof(cl2), iv, sizeof(iv),
  10451. tl2, sizeof(tl2), a, sizeof(a));
  10452. if (result != 0)
  10453. ERROR_OUT(-6516, out);
  10454. if (XMEMCMP(cl, cl2, sizeof(cl2)))
  10455. ERROR_OUT(-6517, out);
  10456. if (XMEMCMP(tl, tl2, sizeof(tl2)))
  10457. ERROR_OUT(-6518, out);
  10458. result = wc_AesCcmDecrypt(enc, pl2, cl2, sizeof(pl2), iv, sizeof(iv),
  10459. tl2, sizeof(tl2), a, sizeof(a));
  10460. if (result != 0)
  10461. ERROR_OUT(-6519, out);
  10462. if (XMEMCMP(pl, pl2, sizeof(pl2)))
  10463. ERROR_OUT(-6520, out);
  10464. /* test empty message as null input or output with nonzero inSz. */
  10465. result = wc_AesCcmEncrypt(enc, pl2 /* out */, NULL /* in */, 1 /* inSz */,
  10466. iv, sizeof(iv), t_empty2, sizeof(t_empty2),
  10467. a, sizeof(a));
  10468. if (result != BAD_FUNC_ARG)
  10469. ERROR_OUT(-6527, out);
  10470. result = wc_AesCcmEncrypt(enc, NULL /* out */, (const byte *)"" /* in */, 1 /* inSz */,
  10471. iv, sizeof(iv), t_empty2, sizeof(t_empty2),
  10472. a, sizeof(a));
  10473. if (result != BAD_FUNC_ARG)
  10474. ERROR_OUT(-6528, out);
  10475. result = wc_AesCcmDecrypt(enc, pl2, NULL /* in */, 1 /* inSz */,
  10476. iv, sizeof(iv), t_empty2, sizeof(t_empty2), a,
  10477. sizeof(a));
  10478. if (result != BAD_FUNC_ARG)
  10479. ERROR_OUT(-6529, out);
  10480. result = wc_AesCcmDecrypt(enc, NULL /* out */, (const byte *)"" /* in */, 1 /* inSz */,
  10481. iv, sizeof(iv), t_empty2, sizeof(t_empty2), a,
  10482. sizeof(a));
  10483. if (result != BAD_FUNC_ARG)
  10484. ERROR_OUT(-6530, out);
  10485. /* test empty message as null input and output with zero inSz --
  10486. * must either succeed, or fail early with BAD_FUNC_ARG.
  10487. */
  10488. result = wc_AesCcmEncrypt(enc, NULL /* out */, NULL /* in */, 0 /* inSz */,
  10489. iv, sizeof(iv), t_empty2, sizeof(t_empty2),
  10490. a, sizeof(a));
  10491. if (result != BAD_FUNC_ARG) {
  10492. if (result != 0)
  10493. ERROR_OUT(-6521, out);
  10494. if (XMEMCMP(t_empty, t_empty2, sizeof(t_empty2)))
  10495. ERROR_OUT(-6522, out);
  10496. result = wc_AesCcmDecrypt(enc, NULL /* out */, NULL /* in */,
  10497. 0 /* inSz */, iv, sizeof(iv), t_empty2,
  10498. sizeof(t_empty2), a, sizeof(a));
  10499. if (result != 0)
  10500. ERROR_OUT(-6523, out);
  10501. }
  10502. /* test empty message as zero-length string -- must work. */
  10503. result = wc_AesCcmEncrypt(enc, pl2, (const byte *)"", 0 /* inSz */, iv,
  10504. sizeof(iv), t_empty2, sizeof(t_empty2), a,
  10505. sizeof(a));
  10506. if (result != 0)
  10507. ERROR_OUT(-6524, out);
  10508. if (XMEMCMP(t_empty, t_empty2, sizeof(t_empty2)))
  10509. ERROR_OUT(-6525, out);
  10510. result = wc_AesCcmDecrypt(enc, pl2, (const byte *)"", 0 /* inSz */,
  10511. iv, sizeof(iv), t_empty2, sizeof(t_empty2), a,
  10512. sizeof(a));
  10513. if (result != 0)
  10514. ERROR_OUT(-6526, out);
  10515. wc_AesFree(enc);
  10516. ret = 0;
  10517. out:
  10518. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10519. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  10520. #endif
  10521. return ret;
  10522. }
  10523. #endif /* WOLFSSL_AES_128 */
  10524. WOLFSSL_TEST_SUBROUTINE int aesccm_test(void)
  10525. {
  10526. int ret = 0;
  10527. #ifdef WOLFSSL_AES_128
  10528. if (ret == 0)
  10529. ret = aesccm_128_test();
  10530. #endif
  10531. #ifdef WOLFSSL_AES_256
  10532. if (ret == 0)
  10533. ret = aesccm_256_test();
  10534. #endif
  10535. return ret;
  10536. }
  10537. #endif /* HAVE_AESCCM */
  10538. #ifdef HAVE_AES_KEYWRAP
  10539. #define MAX_KEYWRAP_TEST_OUTLEN 40
  10540. #define MAX_KEYWRAP_TEST_PLAINLEN 32
  10541. typedef struct keywrapVector {
  10542. const byte* kek;
  10543. const byte* data;
  10544. const byte* verify;
  10545. word32 kekLen;
  10546. word32 dataLen;
  10547. word32 verifyLen;
  10548. } keywrapVector;
  10549. WOLFSSL_TEST_SUBROUTINE int aeskeywrap_test(void)
  10550. {
  10551. int wrapSz, plainSz, testSz, i;
  10552. /* test vectors from RFC 3394 (kek, data, verify) */
  10553. #ifdef WOLFSSL_AES_128
  10554. /* Wrap 128 bits of Key Data with a 128-bit KEK */
  10555. WOLFSSL_SMALL_STACK_STATIC const byte k1[] = {
  10556. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  10557. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  10558. };
  10559. WOLFSSL_SMALL_STACK_STATIC const byte d1[] = {
  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 v1[] = {
  10564. 0x1F, 0xA6, 0x8B, 0x0A, 0x81, 0x12, 0xB4, 0x47,
  10565. 0xAE, 0xF3, 0x4B, 0xD8, 0xFB, 0x5A, 0x7B, 0x82,
  10566. 0x9D, 0x3E, 0x86, 0x23, 0x71, 0xD2, 0xCF, 0xE5
  10567. };
  10568. #endif /* WOLFSSL_AES_128 */
  10569. #ifdef WOLFSSL_AES_192
  10570. /* Wrap 128 bits of Key Data with a 192-bit KEK */
  10571. WOLFSSL_SMALL_STACK_STATIC const byte k2[] = {
  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. };
  10576. WOLFSSL_SMALL_STACK_STATIC const byte d2[] = {
  10577. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  10578. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF
  10579. };
  10580. WOLFSSL_SMALL_STACK_STATIC const byte v2[] = {
  10581. 0x96, 0x77, 0x8B, 0x25, 0xAE, 0x6C, 0xA4, 0x35,
  10582. 0xF9, 0x2B, 0x5B, 0x97, 0xC0, 0x50, 0xAE, 0xD2,
  10583. 0x46, 0x8A, 0xB8, 0xA1, 0x7A, 0xD8, 0x4E, 0x5D
  10584. };
  10585. #endif
  10586. #ifdef WOLFSSL_AES_256
  10587. /* Wrap 128 bits of Key Data with a 256-bit KEK */
  10588. WOLFSSL_SMALL_STACK_STATIC const byte k3[] = {
  10589. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  10590. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  10591. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  10592. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F
  10593. };
  10594. WOLFSSL_SMALL_STACK_STATIC const byte d3[] = {
  10595. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  10596. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF
  10597. };
  10598. WOLFSSL_SMALL_STACK_STATIC const byte v3[] = {
  10599. 0x64, 0xE8, 0xC3, 0xF9, 0xCE, 0x0F, 0x5B, 0xA2,
  10600. 0x63, 0xE9, 0x77, 0x79, 0x05, 0x81, 0x8A, 0x2A,
  10601. 0x93, 0xC8, 0x19, 0x1E, 0x7D, 0x6E, 0x8A, 0xE7
  10602. };
  10603. #endif
  10604. #ifdef WOLFSSL_AES_192
  10605. /* Wrap 192 bits of Key Data with a 192-bit KEK */
  10606. WOLFSSL_SMALL_STACK_STATIC const byte k4[] = {
  10607. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  10608. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  10609. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17
  10610. };
  10611. WOLFSSL_SMALL_STACK_STATIC const byte d4[] = {
  10612. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  10613. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF,
  10614. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
  10615. };
  10616. WOLFSSL_SMALL_STACK_STATIC const byte v4[] = {
  10617. 0x03, 0x1D, 0x33, 0x26, 0x4E, 0x15, 0xD3, 0x32,
  10618. 0x68, 0xF2, 0x4E, 0xC2, 0x60, 0x74, 0x3E, 0xDC,
  10619. 0xE1, 0xC6, 0xC7, 0xDD, 0xEE, 0x72, 0x5A, 0x93,
  10620. 0x6B, 0xA8, 0x14, 0x91, 0x5C, 0x67, 0x62, 0xD2
  10621. };
  10622. #endif
  10623. #ifdef WOLFSSL_AES_256
  10624. /* Wrap 192 bits of Key Data with a 256-bit KEK */
  10625. WOLFSSL_SMALL_STACK_STATIC const byte k5[] = {
  10626. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  10627. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  10628. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  10629. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F
  10630. };
  10631. WOLFSSL_SMALL_STACK_STATIC const byte d5[] = {
  10632. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  10633. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF,
  10634. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
  10635. };
  10636. WOLFSSL_SMALL_STACK_STATIC const byte v5[] = {
  10637. 0xA8, 0xF9, 0xBC, 0x16, 0x12, 0xC6, 0x8B, 0x3F,
  10638. 0xF6, 0xE6, 0xF4, 0xFB, 0xE3, 0x0E, 0x71, 0xE4,
  10639. 0x76, 0x9C, 0x8B, 0x80, 0xA3, 0x2C, 0xB8, 0x95,
  10640. 0x8C, 0xD5, 0xD1, 0x7D, 0x6B, 0x25, 0x4D, 0xA1
  10641. };
  10642. /* Wrap 256 bits of Key Data with a 256-bit KEK */
  10643. WOLFSSL_SMALL_STACK_STATIC const byte k6[] = {
  10644. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  10645. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  10646. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  10647. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F
  10648. };
  10649. WOLFSSL_SMALL_STACK_STATIC const byte d6[] = {
  10650. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  10651. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF,
  10652. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  10653. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  10654. };
  10655. WOLFSSL_SMALL_STACK_STATIC const byte v6[] = {
  10656. 0x28, 0xC9, 0xF4, 0x04, 0xC4, 0xB8, 0x10, 0xF4,
  10657. 0xCB, 0xCC, 0xB3, 0x5C, 0xFB, 0x87, 0xF8, 0x26,
  10658. 0x3F, 0x57, 0x86, 0xE2, 0xD8, 0x0E, 0xD3, 0x26,
  10659. 0xCB, 0xC7, 0xF0, 0xE7, 0x1A, 0x99, 0xF4, 0x3B,
  10660. 0xFB, 0x98, 0x8B, 0x9B, 0x7A, 0x02, 0xDD, 0x21
  10661. };
  10662. #endif /* WOLFSSL_AES_256 */
  10663. byte output[MAX_KEYWRAP_TEST_OUTLEN];
  10664. byte plain [MAX_KEYWRAP_TEST_PLAINLEN];
  10665. const keywrapVector test_wrap[] =
  10666. {
  10667. #ifdef WOLFSSL_AES_128
  10668. {k1, d1, v1, sizeof(k1), sizeof(d1), sizeof(v1)},
  10669. #endif
  10670. #ifdef WOLFSSL_AES_192
  10671. {k2, d2, v2, sizeof(k2), sizeof(d2), sizeof(v2)},
  10672. #endif
  10673. #ifdef WOLFSSL_AES_256
  10674. {k3, d3, v3, sizeof(k3), sizeof(d3), sizeof(v3)},
  10675. #endif
  10676. #ifdef WOLFSSL_AES_192
  10677. {k4, d4, v4, sizeof(k4), sizeof(d4), sizeof(v4)},
  10678. #endif
  10679. #ifdef WOLFSSL_AES_256
  10680. {k5, d5, v5, sizeof(k5), sizeof(d5), sizeof(v5)},
  10681. {k6, d6, v6, sizeof(k6), sizeof(d6), sizeof(v6)}
  10682. #endif
  10683. };
  10684. testSz = sizeof(test_wrap) / sizeof(keywrapVector);
  10685. XMEMSET(output, 0, sizeof(output));
  10686. XMEMSET(plain, 0, sizeof(plain));
  10687. for (i = 0; i < testSz; i++) {
  10688. wrapSz = wc_AesKeyWrap(test_wrap[i].kek, test_wrap[i].kekLen,
  10689. test_wrap[i].data, test_wrap[i].dataLen,
  10690. output, sizeof(output), NULL);
  10691. if ( (wrapSz < 0) || (wrapSz != (int)test_wrap[i].verifyLen) )
  10692. return -6600;
  10693. if (XMEMCMP(output, test_wrap[i].verify, test_wrap[i].verifyLen) != 0)
  10694. return -6601;
  10695. plainSz = wc_AesKeyUnWrap((byte*)test_wrap[i].kek, test_wrap[i].kekLen,
  10696. output, wrapSz,
  10697. plain, sizeof(plain), NULL);
  10698. if ( (plainSz < 0) || (plainSz != (int)test_wrap[i].dataLen) )
  10699. return -6602;
  10700. if (XMEMCMP(plain, test_wrap[i].data, test_wrap[i].dataLen) != 0)
  10701. return -6603 - i;
  10702. }
  10703. return 0;
  10704. }
  10705. #endif /* HAVE_AES_KEYWRAP */
  10706. #endif /* NO_AES */
  10707. #ifdef HAVE_CAMELLIA
  10708. enum {
  10709. CAM_ECB_ENC, CAM_ECB_DEC, CAM_CBC_ENC, CAM_CBC_DEC
  10710. };
  10711. typedef struct {
  10712. int type;
  10713. const byte* plaintext;
  10714. const byte* iv;
  10715. const byte* ciphertext;
  10716. const byte* key;
  10717. word32 keySz;
  10718. int errorCode;
  10719. } test_vector_t;
  10720. WOLFSSL_TEST_SUBROUTINE int camellia_test(void)
  10721. {
  10722. /* Camellia ECB Test Plaintext */
  10723. WOLFSSL_SMALL_STACK_STATIC const byte pte[] =
  10724. {
  10725. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  10726. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
  10727. };
  10728. /* Camellia ECB Test Initialization Vector */
  10729. WOLFSSL_SMALL_STACK_STATIC const byte ive[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  10730. /* Test 1: Camellia ECB 128-bit key */
  10731. WOLFSSL_SMALL_STACK_STATIC const byte k1[] =
  10732. {
  10733. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  10734. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
  10735. };
  10736. WOLFSSL_SMALL_STACK_STATIC const byte c1[] =
  10737. {
  10738. 0x67, 0x67, 0x31, 0x38, 0x54, 0x96, 0x69, 0x73,
  10739. 0x08, 0x57, 0x06, 0x56, 0x48, 0xea, 0xbe, 0x43
  10740. };
  10741. /* Test 2: Camellia ECB 192-bit key */
  10742. WOLFSSL_SMALL_STACK_STATIC const byte k2[] =
  10743. {
  10744. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  10745. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  10746. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  10747. };
  10748. WOLFSSL_SMALL_STACK_STATIC const byte c2[] =
  10749. {
  10750. 0xb4, 0x99, 0x34, 0x01, 0xb3, 0xe9, 0x96, 0xf8,
  10751. 0x4e, 0xe5, 0xce, 0xe7, 0xd7, 0x9b, 0x09, 0xb9
  10752. };
  10753. /* Test 3: Camellia ECB 256-bit key */
  10754. WOLFSSL_SMALL_STACK_STATIC const byte k3[] =
  10755. {
  10756. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  10757. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  10758. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  10759. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff
  10760. };
  10761. WOLFSSL_SMALL_STACK_STATIC const byte c3[] =
  10762. {
  10763. 0x9a, 0xcc, 0x23, 0x7d, 0xff, 0x16, 0xd7, 0x6c,
  10764. 0x20, 0xef, 0x7c, 0x91, 0x9e, 0x3a, 0x75, 0x09
  10765. };
  10766. /* Camellia CBC Test Plaintext */
  10767. WOLFSSL_SMALL_STACK_STATIC const byte ptc[] =
  10768. {
  10769. 0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96,
  10770. 0xE9, 0x3D, 0x7E, 0x11, 0x73, 0x93, 0x17, 0x2A
  10771. };
  10772. /* Camellia CBC Test Initialization Vector */
  10773. WOLFSSL_SMALL_STACK_STATIC const byte ivc[] =
  10774. {
  10775. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  10776. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  10777. };
  10778. /* Test 4: Camellia-CBC 128-bit key */
  10779. WOLFSSL_SMALL_STACK_STATIC const byte k4[] =
  10780. {
  10781. 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6,
  10782. 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C
  10783. };
  10784. WOLFSSL_SMALL_STACK_STATIC const byte c4[] =
  10785. {
  10786. 0x16, 0x07, 0xCF, 0x49, 0x4B, 0x36, 0xBB, 0xF0,
  10787. 0x0D, 0xAE, 0xB0, 0xB5, 0x03, 0xC8, 0x31, 0xAB
  10788. };
  10789. /* Test 5: Camellia-CBC 192-bit key */
  10790. WOLFSSL_SMALL_STACK_STATIC const byte k5[] =
  10791. {
  10792. 0x8E, 0x73, 0xB0, 0xF7, 0xDA, 0x0E, 0x64, 0x52,
  10793. 0xC8, 0x10, 0xF3, 0x2B, 0x80, 0x90, 0x79, 0xE5,
  10794. 0x62, 0xF8, 0xEA, 0xD2, 0x52, 0x2C, 0x6B, 0x7B
  10795. };
  10796. WOLFSSL_SMALL_STACK_STATIC const byte c5[] =
  10797. {
  10798. 0x2A, 0x48, 0x30, 0xAB, 0x5A, 0xC4, 0xA1, 0xA2,
  10799. 0x40, 0x59, 0x55, 0xFD, 0x21, 0x95, 0xCF, 0x93
  10800. };
  10801. /* Test 6: CBC 256-bit key */
  10802. WOLFSSL_SMALL_STACK_STATIC const byte k6[] =
  10803. {
  10804. 0x60, 0x3D, 0xEB, 0x10, 0x15, 0xCA, 0x71, 0xBE,
  10805. 0x2B, 0x73, 0xAE, 0xF0, 0x85, 0x7D, 0x77, 0x81,
  10806. 0x1F, 0x35, 0x2C, 0x07, 0x3B, 0x61, 0x08, 0xD7,
  10807. 0x2D, 0x98, 0x10, 0xA3, 0x09, 0x14, 0xDF, 0xF4
  10808. };
  10809. WOLFSSL_SMALL_STACK_STATIC const byte c6[] =
  10810. {
  10811. 0xE6, 0xCF, 0xA3, 0x5F, 0xC0, 0x2B, 0x13, 0x4A,
  10812. 0x4D, 0x2C, 0x0B, 0x67, 0x37, 0xAC, 0x3E, 0xDA
  10813. };
  10814. byte out[CAMELLIA_BLOCK_SIZE];
  10815. Camellia cam;
  10816. int i, testsSz, ret;
  10817. WOLFSSL_SMALL_STACK_STATIC const test_vector_t testVectors[] =
  10818. {
  10819. {CAM_ECB_ENC, pte, ive, c1, k1, sizeof(k1), -114},
  10820. {CAM_ECB_ENC, pte, ive, c2, k2, sizeof(k2), -115},
  10821. {CAM_ECB_ENC, pte, ive, c3, k3, sizeof(k3), -116},
  10822. {CAM_ECB_DEC, pte, ive, c1, k1, sizeof(k1), -117},
  10823. {CAM_ECB_DEC, pte, ive, c2, k2, sizeof(k2), -118},
  10824. {CAM_ECB_DEC, pte, ive, c3, k3, sizeof(k3), -119},
  10825. {CAM_CBC_ENC, ptc, ivc, c4, k4, sizeof(k4), -120},
  10826. {CAM_CBC_ENC, ptc, ivc, c5, k5, sizeof(k5), -121},
  10827. {CAM_CBC_ENC, ptc, ivc, c6, k6, sizeof(k6), -122},
  10828. {CAM_CBC_DEC, ptc, ivc, c4, k4, sizeof(k4), -123},
  10829. {CAM_CBC_DEC, ptc, ivc, c5, k5, sizeof(k5), -124},
  10830. {CAM_CBC_DEC, ptc, ivc, c6, k6, sizeof(k6), -125}
  10831. };
  10832. testsSz = sizeof(testVectors)/sizeof(test_vector_t);
  10833. for (i = 0; i < testsSz; i++) {
  10834. if (wc_CamelliaSetKey(&cam, testVectors[i].key, testVectors[i].keySz,
  10835. testVectors[i].iv) != 0)
  10836. return testVectors[i].errorCode;
  10837. switch (testVectors[i].type) {
  10838. case CAM_ECB_ENC:
  10839. ret = wc_CamelliaEncryptDirect(&cam, out,
  10840. testVectors[i].plaintext);
  10841. if (ret != 0 || XMEMCMP(out, testVectors[i].ciphertext,
  10842. CAMELLIA_BLOCK_SIZE))
  10843. return testVectors[i].errorCode;
  10844. break;
  10845. case CAM_ECB_DEC:
  10846. ret = wc_CamelliaDecryptDirect(&cam, out,
  10847. testVectors[i].ciphertext);
  10848. if (ret != 0 || XMEMCMP(out, testVectors[i].plaintext,
  10849. CAMELLIA_BLOCK_SIZE))
  10850. return testVectors[i].errorCode;
  10851. break;
  10852. case CAM_CBC_ENC:
  10853. ret = wc_CamelliaCbcEncrypt(&cam, out, testVectors[i].plaintext,
  10854. CAMELLIA_BLOCK_SIZE);
  10855. if (ret != 0 || XMEMCMP(out, testVectors[i].ciphertext,
  10856. CAMELLIA_BLOCK_SIZE))
  10857. return testVectors[i].errorCode;
  10858. break;
  10859. case CAM_CBC_DEC:
  10860. ret = wc_CamelliaCbcDecrypt(&cam, out,
  10861. testVectors[i].ciphertext, CAMELLIA_BLOCK_SIZE);
  10862. if (ret != 0 || XMEMCMP(out, testVectors[i].plaintext,
  10863. CAMELLIA_BLOCK_SIZE))
  10864. return testVectors[i].errorCode;
  10865. break;
  10866. default:
  10867. break;
  10868. }
  10869. }
  10870. /* Setting the IV and checking it was actually set. */
  10871. ret = wc_CamelliaSetIV(&cam, ivc);
  10872. if (ret != 0 || XMEMCMP(cam.reg, ivc, CAMELLIA_BLOCK_SIZE))
  10873. return -6700;
  10874. /* Setting the IV to NULL should be same as all zeros IV */
  10875. if (wc_CamelliaSetIV(&cam, NULL) != 0 ||
  10876. XMEMCMP(cam.reg, ive, CAMELLIA_BLOCK_SIZE))
  10877. return -6701;
  10878. /* First parameter should never be null */
  10879. if (wc_CamelliaSetIV(NULL, NULL) == 0)
  10880. return -6702;
  10881. /* First parameter should never be null, check it fails */
  10882. if (wc_CamelliaSetKey(NULL, k1, sizeof(k1), NULL) == 0)
  10883. return -6703;
  10884. /* Key should have a size of 16, 24, or 32 */
  10885. if (wc_CamelliaSetKey(&cam, k1, 0, NULL) == 0)
  10886. return -6704;
  10887. return 0;
  10888. }
  10889. #endif /* HAVE_CAMELLIA */
  10890. #ifdef HAVE_XCHACHA
  10891. WOLFSSL_TEST_SUBROUTINE int XChaCha_test(void) {
  10892. int ret = -6830;
  10893. WOLFSSL_SMALL_STACK_STATIC const byte Plaintext[] = {
  10894. 0x54, 0x68, 0x65, 0x20, 0x64, 0x68, 0x6f, 0x6c, 0x65, 0x20, 0x28, 0x70, 0x72, 0x6f, 0x6e, 0x6f, /* The dhole (prono */
  10895. 0x75, 0x6e, 0x63, 0x65, 0x64, 0x20, 0x22, 0x64, 0x6f, 0x6c, 0x65, 0x22, 0x29, 0x20, 0x69, 0x73, /* unced "dole") is */
  10896. 0x20, 0x61, 0x6c, 0x73, 0x6f, 0x20, 0x6b, 0x6e, 0x6f, 0x77, 0x6e, 0x20, 0x61, 0x73, 0x20, 0x74, /* also known as t */
  10897. 0x68, 0x65, 0x20, 0x41, 0x73, 0x69, 0x61, 0x74, 0x69, 0x63, 0x20, 0x77, 0x69, 0x6c, 0x64, 0x20, /* he Asiatic wild */
  10898. 0x64, 0x6f, 0x67, 0x2c, 0x20, 0x72, 0x65, 0x64, 0x20, 0x64, 0x6f, 0x67, 0x2c, 0x20, 0x61, 0x6e, /* dog, red dog, an */
  10899. 0x64, 0x20, 0x77, 0x68, 0x69, 0x73, 0x74, 0x6c, 0x69, 0x6e, 0x67, 0x20, 0x64, 0x6f, 0x67, 0x2e, /* d whistling dog. */
  10900. 0x20, 0x49, 0x74, 0x20, 0x69, 0x73, 0x20, 0x61, 0x62, 0x6f, 0x75, 0x74, 0x20, 0x74, 0x68, 0x65, /* It is about the */
  10901. 0x20, 0x73, 0x69, 0x7a, 0x65, 0x20, 0x6f, 0x66, 0x20, 0x61, 0x20, 0x47, 0x65, 0x72, 0x6d, 0x61, /* size of a Germa */
  10902. 0x6e, 0x20, 0x73, 0x68, 0x65, 0x70, 0x68, 0x65, 0x72, 0x64, 0x20, 0x62, 0x75, 0x74, 0x20, 0x6c, /* n shepherd but l */
  10903. 0x6f, 0x6f, 0x6b, 0x73, 0x20, 0x6d, 0x6f, 0x72, 0x65, 0x20, 0x6c, 0x69, 0x6b, 0x65, 0x20, 0x61, /* ooks more like a */
  10904. 0x20, 0x6c, 0x6f, 0x6e, 0x67, 0x2d, 0x6c, 0x65, 0x67, 0x67, 0x65, 0x64, 0x20, 0x66, 0x6f, 0x78, /* long-legged fox */
  10905. 0x2e, 0x20, 0x54, 0x68, 0x69, 0x73, 0x20, 0x68, 0x69, 0x67, 0x68, 0x6c, 0x79, 0x20, 0x65, 0x6c, /* . This highly el */
  10906. 0x75, 0x73, 0x69, 0x76, 0x65, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x73, 0x6b, 0x69, 0x6c, 0x6c, 0x65, /* usive and skille */
  10907. 0x64, 0x20, 0x6a, 0x75, 0x6d, 0x70, 0x65, 0x72, 0x20, 0x69, 0x73, 0x20, 0x63, 0x6c, 0x61, 0x73, /* d jumper is clas */
  10908. 0x73, 0x69, 0x66, 0x69, 0x65, 0x64, 0x20, 0x77, 0x69, 0x74, 0x68, 0x20, 0x77, 0x6f, 0x6c, 0x76, /* sified with wolv */
  10909. 0x65, 0x73, 0x2c, 0x20, 0x63, 0x6f, 0x79, 0x6f, 0x74, 0x65, 0x73, 0x2c, 0x20, 0x6a, 0x61, 0x63, /* es, coyotes, jac */
  10910. 0x6b, 0x61, 0x6c, 0x73, 0x2c, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x66, 0x6f, 0x78, 0x65, 0x73, 0x20, /* kals, and foxes */
  10911. 0x69, 0x6e, 0x20, 0x74, 0x68, 0x65, 0x20, 0x74, 0x61, 0x78, 0x6f, 0x6e, 0x6f, 0x6d, 0x69, 0x63, /* in the taxonomic */
  10912. 0x20, 0x66, 0x61, 0x6d, 0x69, 0x6c, 0x79, 0x20, 0x43, 0x61, 0x6e, 0x69, 0x64, 0x61, 0x65, 0x2e /* family Canidae. */
  10913. };
  10914. WOLFSSL_SMALL_STACK_STATIC const byte Key[] = {
  10915. 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  10916. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f
  10917. };
  10918. WOLFSSL_SMALL_STACK_STATIC const byte IV[] = {
  10919. 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, /* @ABCDEFGHIJKLMNO */
  10920. 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x58 }; /* PQRSTUVW */
  10921. WOLFSSL_SMALL_STACK_STATIC const byte Ciphertext[] = {
  10922. 0x45, 0x59, 0xab, 0xba, 0x4e, 0x48, 0xc1, 0x61, 0x02, 0xe8, 0xbb, 0x2c, 0x05, 0xe6, 0x94, 0x7f,
  10923. 0x50, 0xa7, 0x86, 0xde, 0x16, 0x2f, 0x9b, 0x0b, 0x7e, 0x59, 0x2a, 0x9b, 0x53, 0xd0, 0xd4, 0xe9,
  10924. 0x8d, 0x8d, 0x64, 0x10, 0xd5, 0x40, 0xa1, 0xa6, 0x37, 0x5b, 0x26, 0xd8, 0x0d, 0xac, 0xe4, 0xfa,
  10925. 0xb5, 0x23, 0x84, 0xc7, 0x31, 0xac, 0xbf, 0x16, 0xa5, 0x92, 0x3c, 0x0c, 0x48, 0xd3, 0x57, 0x5d,
  10926. 0x4d, 0x0d, 0x2c, 0x67, 0x3b, 0x66, 0x6f, 0xaa, 0x73, 0x10, 0x61, 0x27, 0x77, 0x01, 0x09, 0x3a,
  10927. 0x6b, 0xf7, 0xa1, 0x58, 0xa8, 0x86, 0x42, 0x92, 0xa4, 0x1c, 0x48, 0xe3, 0xa9, 0xb4, 0xc0, 0xda,
  10928. 0xec, 0xe0, 0xf8, 0xd9, 0x8d, 0x0d, 0x7e, 0x05, 0xb3, 0x7a, 0x30, 0x7b, 0xbb, 0x66, 0x33, 0x31,
  10929. 0x64, 0xec, 0x9e, 0x1b, 0x24, 0xea, 0x0d, 0x6c, 0x3f, 0xfd, 0xdc, 0xec, 0x4f, 0x68, 0xe7, 0x44,
  10930. 0x30, 0x56, 0x19, 0x3a, 0x03, 0xc8, 0x10, 0xe1, 0x13, 0x44, 0xca, 0x06, 0xd8, 0xed, 0x8a, 0x2b,
  10931. 0xfb, 0x1e, 0x8d, 0x48, 0xcf, 0xa6, 0xbc, 0x0e, 0xb4, 0xe2, 0x46, 0x4b, 0x74, 0x81, 0x42, 0x40,
  10932. 0x7c, 0x9f, 0x43, 0x1a, 0xee, 0x76, 0x99, 0x60, 0xe1, 0x5b, 0xa8, 0xb9, 0x68, 0x90, 0x46, 0x6e,
  10933. 0xf2, 0x45, 0x75, 0x99, 0x85, 0x23, 0x85, 0xc6, 0x61, 0xf7, 0x52, 0xce, 0x20, 0xf9, 0xda, 0x0c,
  10934. 0x09, 0xab, 0x6b, 0x19, 0xdf, 0x74, 0xe7, 0x6a, 0x95, 0x96, 0x74, 0x46, 0xf8, 0xd0, 0xfd, 0x41,
  10935. 0x5e, 0x7b, 0xee, 0x2a, 0x12, 0xa1, 0x14, 0xc2, 0x0e, 0xb5, 0x29, 0x2a, 0xe7, 0xa3, 0x49, 0xae,
  10936. 0x57, 0x78, 0x20, 0xd5, 0x52, 0x0a, 0x1f, 0x3f, 0xb6, 0x2a, 0x17, 0xce, 0x6a, 0x7e, 0x68, 0xfa,
  10937. 0x7c, 0x79, 0x11, 0x1d, 0x88, 0x60, 0x92, 0x0b, 0xc0, 0x48, 0xef, 0x43, 0xfe, 0x84, 0x48, 0x6c,
  10938. 0xcb, 0x87, 0xc2, 0x5f, 0x0a, 0xe0, 0x45, 0xf0, 0xcc, 0xe1, 0xe7, 0x98, 0x9a, 0x9a, 0xa2, 0x20,
  10939. 0xa2, 0x8b, 0xdd, 0x48, 0x27, 0xe7, 0x51, 0xa2, 0x4a, 0x6d, 0x5c, 0x62, 0xd7, 0x90, 0xa6, 0x63,
  10940. 0x93, 0xb9, 0x31, 0x11, 0xc1, 0xa5, 0x5d, 0xd7, 0x42, 0x1a, 0x10, 0x18, 0x49, 0x74, 0xc7, 0xc5
  10941. };
  10942. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10943. struct ChaCha *chacha = (struct ChaCha *)XMALLOC(sizeof *chacha, HEAP_HINT, DYNAMIC_TYPE_CIPHER);
  10944. byte *buf1 = (byte *)XMALLOC(sizeof Plaintext, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10945. byte *buf2 = (byte *)XMALLOC(sizeof Plaintext, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10946. if ((chacha == NULL) || (buf1 == NULL) || (buf2 == NULL))
  10947. ERROR_OUT(MEMORY_E, out);
  10948. #else
  10949. struct ChaCha chacha[1];
  10950. byte buf1[sizeof Plaintext];
  10951. byte buf2[sizeof Plaintext];
  10952. #endif
  10953. ret = wc_XChacha_SetKey(chacha, Key, sizeof Key, IV, sizeof IV, 0);
  10954. if (ret < 0)
  10955. ERROR_OUT(-6831, out);
  10956. ret = wc_Chacha_Process(chacha, buf1, Plaintext, sizeof Plaintext);
  10957. if (ret < 0)
  10958. ERROR_OUT(-6832, out);
  10959. if (XMEMCMP(buf1, Ciphertext, sizeof Plaintext))
  10960. ERROR_OUT(-6833, out);
  10961. ret = wc_XChacha_SetKey(chacha, Key, sizeof Key, IV, sizeof IV, 0);
  10962. if (ret < 0)
  10963. ERROR_OUT(-6834, out);
  10964. ret = wc_Chacha_Process(chacha, buf2, buf1, sizeof Plaintext);
  10965. if (ret < 0)
  10966. ERROR_OUT(-6835, out);
  10967. if (XMEMCMP(buf2, Plaintext, sizeof Plaintext))
  10968. ERROR_OUT(-6836, out);
  10969. out:
  10970. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10971. if (chacha)
  10972. XFREE(chacha, HEAP_HINT, DYNAMIC_TYPE_CIPHER);
  10973. if (buf1)
  10974. XFREE(buf1, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10975. if (buf2)
  10976. XFREE(buf2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10977. #endif
  10978. return ret;
  10979. }
  10980. #endif /* HAVE_XCHACHA */
  10981. #if defined(HAVE_XCHACHA) && defined(HAVE_POLY1305)
  10982. WOLFSSL_TEST_SUBROUTINE int XChaCha20Poly1305_test(void) {
  10983. int ret;
  10984. WOLFSSL_SMALL_STACK_STATIC const byte Plaintext[] = {
  10985. 0x4c, 0x61, 0x64, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x47, 0x65, 0x6e, 0x74, 0x6c, /* Ladies and Gentl */
  10986. 0x65, 0x6d, 0x65, 0x6e, 0x20, 0x6f, 0x66, 0x20, 0x74, 0x68, 0x65, 0x20, 0x63, 0x6c, 0x61, 0x73, /* emen of the clas */
  10987. 0x73, 0x20, 0x6f, 0x66, 0x20, 0x27, 0x39, 0x39, 0x3a, 0x20, 0x49, 0x66, 0x20, 0x49, 0x20, 0x63, /* s of '99: If I c */
  10988. 0x6f, 0x75, 0x6c, 0x64, 0x20, 0x6f, 0x66, 0x66, 0x65, 0x72, 0x20, 0x79, 0x6f, 0x75, 0x20, 0x6f, /* ould offer you o */
  10989. 0x6e, 0x6c, 0x79, 0x20, 0x6f, 0x6e, 0x65, 0x20, 0x74, 0x69, 0x70, 0x20, 0x66, 0x6f, 0x72, 0x20, /* nly one tip for */
  10990. 0x74, 0x68, 0x65, 0x20, 0x66, 0x75, 0x74, 0x75, 0x72, 0x65, 0x2c, 0x20, 0x73, 0x75, 0x6e, 0x73, /* the future, suns */
  10991. 0x63, 0x72, 0x65, 0x65, 0x6e, 0x20, 0x77, 0x6f, 0x75, 0x6c, 0x64, 0x20, 0x62, 0x65, 0x20, 0x69, /* creen would be i */
  10992. 0x74, 0x2e }; /* t. */
  10993. WOLFSSL_SMALL_STACK_STATIC const byte AAD[] = { 0x50, 0x51, 0x52, 0x53, 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7 }; /* PQRS........ */
  10994. WOLFSSL_SMALL_STACK_STATIC const byte Key[] = {
  10995. 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  10996. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f
  10997. };
  10998. WOLFSSL_SMALL_STACK_STATIC const byte IV[] = {
  10999. 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, /* @ABCDEFGHIJKLMNO */
  11000. 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57 }; /* PQRSTUVW */
  11001. WOLFSSL_SMALL_STACK_STATIC const byte Ciphertext[] = {
  11002. 0xbd, 0x6d, 0x17, 0x9d, 0x3e, 0x83, 0xd4, 0x3b, 0x95, 0x76, 0x57, 0x94, 0x93, 0xc0, 0xe9, 0x39,
  11003. 0x57, 0x2a, 0x17, 0x00, 0x25, 0x2b, 0xfa, 0xcc, 0xbe, 0xd2, 0x90, 0x2c, 0x21, 0x39, 0x6c, 0xbb,
  11004. 0x73, 0x1c, 0x7f, 0x1b, 0x0b, 0x4a, 0xa6, 0x44, 0x0b, 0xf3, 0xa8, 0x2f, 0x4e, 0xda, 0x7e, 0x39,
  11005. 0xae, 0x64, 0xc6, 0x70, 0x8c, 0x54, 0xc2, 0x16, 0xcb, 0x96, 0xb7, 0x2e, 0x12, 0x13, 0xb4, 0x52,
  11006. 0x2f, 0x8c, 0x9b, 0xa4, 0x0d, 0xb5, 0xd9, 0x45, 0xb1, 0x1b, 0x69, 0xb9, 0x82, 0xc1, 0xbb, 0x9e,
  11007. 0x3f, 0x3f, 0xac, 0x2b, 0xc3, 0x69, 0x48, 0x8f, 0x76, 0xb2, 0x38, 0x35, 0x65, 0xd3, 0xff, 0xf9,
  11008. 0x21, 0xf9, 0x66, 0x4c, 0x97, 0x63, 0x7d, 0xa9, 0x76, 0x88, 0x12, 0xf6, 0x15, 0xc6, 0x8b, 0x13,
  11009. 0xb5, 0x2e };
  11010. WOLFSSL_SMALL_STACK_STATIC const byte Tag[] = {
  11011. 0xc0, 0x87, 0x59, 0x24, 0xc1, 0xc7, 0x98, 0x79, 0x47, 0xde, 0xaf, 0xd8, 0x78, 0x0a, 0xcf, 0x49
  11012. };
  11013. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  11014. byte *buf1 = (byte *)XMALLOC(sizeof Ciphertext + sizeof Tag, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11015. byte *buf2 = (byte *)XMALLOC(sizeof Plaintext, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11016. if ((buf1 == NULL) || (buf2 == NULL))
  11017. ERROR_OUT(-6480, out);
  11018. #else
  11019. byte buf1[sizeof Ciphertext + sizeof Tag];
  11020. byte buf2[sizeof Plaintext];
  11021. #endif
  11022. ret = wc_XChaCha20Poly1305_Encrypt(buf1, sizeof Ciphertext + sizeof Tag,
  11023. Plaintext, sizeof Plaintext,
  11024. AAD, sizeof AAD,
  11025. IV, sizeof IV,
  11026. Key, sizeof Key);
  11027. if (ret < 0)
  11028. ERROR_OUT(-6841, out);
  11029. if (XMEMCMP(buf1, Ciphertext, sizeof Ciphertext))
  11030. ERROR_OUT(-6842, out);
  11031. if (XMEMCMP(buf1 + sizeof Ciphertext, Tag, CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE))
  11032. ERROR_OUT(-6843, out);
  11033. ret = wc_XChaCha20Poly1305_Decrypt(buf2, sizeof Plaintext,
  11034. buf1, sizeof Ciphertext + sizeof Tag,
  11035. AAD, sizeof AAD,
  11036. IV, sizeof IV,
  11037. Key, sizeof Key);
  11038. if (ret < 0)
  11039. ERROR_OUT(-6844, out);
  11040. if (XMEMCMP(buf2, Plaintext, sizeof Plaintext))
  11041. ERROR_OUT(-6845, out);
  11042. out:
  11043. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  11044. if (buf1 != NULL)
  11045. XFREE(buf1, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11046. if (buf2 != NULL)
  11047. XFREE(buf2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11048. #endif
  11049. return ret;
  11050. }
  11051. #endif /* defined(HAVE_XCHACHA) && defined(HAVE_POLY1305) */
  11052. #ifndef WC_NO_RNG
  11053. static int _rng_test(WC_RNG* rng, int errorOffset)
  11054. {
  11055. byte block[32];
  11056. int ret, i;
  11057. XMEMSET(block, 0, sizeof(block));
  11058. ret = wc_RNG_GenerateBlock(rng, block, sizeof(block));
  11059. if (ret != 0) {
  11060. ret = -6850;
  11061. goto exit;
  11062. }
  11063. /* Check for 0's */
  11064. for (i=0; i<(int)sizeof(block); i++) {
  11065. if (block[i] == 0) {
  11066. ret++;
  11067. }
  11068. }
  11069. /* All zeros count check */
  11070. if (ret >= (int)sizeof(block)) {
  11071. ret = -6851;
  11072. goto exit;
  11073. }
  11074. ret = wc_RNG_GenerateByte(rng, block);
  11075. if (ret != 0) {
  11076. ret = -6852;
  11077. goto exit;
  11078. }
  11079. /* Parameter validation testing. */
  11080. ret = wc_RNG_GenerateBlock(NULL, block, sizeof(block));
  11081. if (ret != BAD_FUNC_ARG) {
  11082. ret = -6853;
  11083. goto exit;
  11084. }
  11085. ret = wc_RNG_GenerateBlock(rng, NULL, sizeof(block));
  11086. if (ret != BAD_FUNC_ARG) {
  11087. ret = -6854;
  11088. goto exit;
  11089. }
  11090. ret = wc_RNG_GenerateByte(NULL, block);
  11091. if (ret != BAD_FUNC_ARG) {
  11092. ret = -6855;
  11093. goto exit;
  11094. }
  11095. ret = wc_RNG_GenerateByte(rng, NULL);
  11096. if (ret != BAD_FUNC_ARG) {
  11097. ret = -6856;
  11098. goto exit;
  11099. }
  11100. ret = 0;
  11101. exit:
  11102. if (ret != 0)
  11103. ret += errorOffset;
  11104. return ret;
  11105. }
  11106. static int random_rng_test(void)
  11107. {
  11108. WC_RNG localRng;
  11109. WC_RNG* rng;
  11110. int ret;
  11111. rng = &localRng;
  11112. /* Test stack based RNG. */
  11113. #ifndef HAVE_FIPS
  11114. ret = wc_InitRng_ex(rng, HEAP_HINT, devId);
  11115. #else
  11116. ret = wc_InitRng(rng);
  11117. #endif
  11118. if (ret != 0) return -6900;
  11119. ret = _rng_test(rng, -6300);
  11120. /* Make sure and free RNG */
  11121. wc_FreeRng(rng);
  11122. if (ret != 0) return ret;
  11123. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && !defined(WOLFSSL_NO_MALLOC)
  11124. {
  11125. byte nonce[8] = { 0 };
  11126. /* Test dynamic RNG. */
  11127. rng = wc_rng_new(nonce, (word32)sizeof(nonce), HEAP_HINT);
  11128. if (rng == NULL) return -6901;
  11129. ret = _rng_test(rng, -6310);
  11130. wc_rng_free(rng);
  11131. }
  11132. #endif
  11133. return ret;
  11134. }
  11135. #if defined(HAVE_HASHDRBG) && !defined(CUSTOM_RAND_GENERATE_BLOCK)
  11136. #ifdef WC_RNG_SEED_CB
  11137. static int seed_cb(OS_Seed* os, byte* output, word32 sz)
  11138. {
  11139. word32 i;
  11140. (void)os;
  11141. /* Known answer test. Set the seed to the same value every time. */
  11142. for (i = 0; i < sz; i++)
  11143. output[i] = (byte)i;
  11144. return 0;
  11145. }
  11146. static int rng_seed_test(void)
  11147. {
  11148. #ifndef HAVE_FIPS
  11149. WOLFSSL_SMALL_STACK_STATIC const byte check[] =
  11150. {
  11151. 0x83, 0x46, 0x65, 0x2f, 0x5c, 0x44, 0x16, 0x5f,
  11152. 0xb3, 0x89, 0x26, 0xde, 0x0b, 0x6b, 0xa2, 0x06,
  11153. 0x7e, 0xa7, 0x9a, 0x55, 0x22, 0x01, 0xb0, 0x22,
  11154. 0xf4, 0x7e, 0xa2, 0x66, 0xc4, 0x08, 0x6f, 0xba
  11155. };
  11156. #else
  11157. /* FIPS uses a longer seed, so different check value. */
  11158. WOLFSSL_SMALL_STACK_STATIC const byte check[] =
  11159. {
  11160. 0xaf, 0x31, 0xcc, 0xef, 0xa9, 0x29, 0x4c, 0x24,
  11161. 0xbd, 0xa5, 0xa3, 0x52, 0x69, 0xf3, 0xb9, 0xb2,
  11162. 0x1e, 0xd4, 0x52, 0x3b, 0x9a, 0x96, 0x06, 0x20,
  11163. 0xc0, 0x5f, 0x44, 0x06, 0x1f, 0x80, 0xdf, 0xe0
  11164. };
  11165. #endif
  11166. byte output[WC_SHA256_DIGEST_SIZE];
  11167. WC_RNG rng;
  11168. int ret;
  11169. ret = wc_SetSeed_Cb(seed_cb);
  11170. if (ret != 0) {
  11171. ret = -7007;
  11172. goto exit;
  11173. }
  11174. ret = wc_InitRng(&rng);
  11175. if (ret != 0) {
  11176. ret = -7008;
  11177. goto exit;
  11178. }
  11179. ret = wc_RNG_GenerateBlock(&rng, output, sizeof(output));
  11180. if (ret != 0) {
  11181. ret = -7009;
  11182. goto exit;
  11183. }
  11184. ret = XMEMCMP(output, check, sizeof(output));
  11185. if (ret != 0) {
  11186. ret = -7010;
  11187. goto exit;
  11188. }
  11189. ret = wc_FreeRng(&rng);
  11190. if (ret != 0) {
  11191. ret = -7011;
  11192. goto exit;
  11193. }
  11194. ret = wc_SetSeed_Cb(wc_GenerateSeed);
  11195. if (ret != 0) {
  11196. ret = -7012;
  11197. }
  11198. exit:
  11199. return ret;
  11200. }
  11201. #endif
  11202. WOLFSSL_TEST_SUBROUTINE int random_test(void)
  11203. {
  11204. WOLFSSL_SMALL_STACK_STATIC const byte test1Entropy[] =
  11205. {
  11206. 0xa6, 0x5a, 0xd0, 0xf3, 0x45, 0xdb, 0x4e, 0x0e, 0xff, 0xe8, 0x75, 0xc3,
  11207. 0xa2, 0xe7, 0x1f, 0x42, 0xc7, 0x12, 0x9d, 0x62, 0x0f, 0xf5, 0xc1, 0x19,
  11208. 0xa9, 0xef, 0x55, 0xf0, 0x51, 0x85, 0xe0, 0xfb, 0x85, 0x81, 0xf9, 0x31,
  11209. 0x75, 0x17, 0x27, 0x6e, 0x06, 0xe9, 0x60, 0x7d, 0xdb, 0xcb, 0xcc, 0x2e
  11210. };
  11211. WOLFSSL_SMALL_STACK_STATIC const byte test1Output[] =
  11212. {
  11213. 0xd3, 0xe1, 0x60, 0xc3, 0x5b, 0x99, 0xf3, 0x40, 0xb2, 0x62, 0x82, 0x64,
  11214. 0xd1, 0x75, 0x10, 0x60, 0xe0, 0x04, 0x5d, 0xa3, 0x83, 0xff, 0x57, 0xa5,
  11215. 0x7d, 0x73, 0xa6, 0x73, 0xd2, 0xb8, 0xd8, 0x0d, 0xaa, 0xf6, 0xa6, 0xc3,
  11216. 0x5a, 0x91, 0xbb, 0x45, 0x79, 0xd7, 0x3f, 0xd0, 0xc8, 0xfe, 0xd1, 0x11,
  11217. 0xb0, 0x39, 0x13, 0x06, 0x82, 0x8a, 0xdf, 0xed, 0x52, 0x8f, 0x01, 0x81,
  11218. 0x21, 0xb3, 0xfe, 0xbd, 0xc3, 0x43, 0xe7, 0x97, 0xb8, 0x7d, 0xbb, 0x63,
  11219. 0xdb, 0x13, 0x33, 0xde, 0xd9, 0xd1, 0xec, 0xe1, 0x77, 0xcf, 0xa6, 0xb7,
  11220. 0x1f, 0xe8, 0xab, 0x1d, 0xa4, 0x66, 0x24, 0xed, 0x64, 0x15, 0xe5, 0x1c,
  11221. 0xcd, 0xe2, 0xc7, 0xca, 0x86, 0xe2, 0x83, 0x99, 0x0e, 0xea, 0xeb, 0x91,
  11222. 0x12, 0x04, 0x15, 0x52, 0x8b, 0x22, 0x95, 0x91, 0x02, 0x81, 0xb0, 0x2d,
  11223. 0xd4, 0x31, 0xf4, 0xc9, 0xf7, 0x04, 0x27, 0xdf
  11224. };
  11225. WOLFSSL_SMALL_STACK_STATIC const byte test2EntropyA[] =
  11226. {
  11227. 0x63, 0x36, 0x33, 0x77, 0xe4, 0x1e, 0x86, 0x46, 0x8d, 0xeb, 0x0a, 0xb4,
  11228. 0xa8, 0xed, 0x68, 0x3f, 0x6a, 0x13, 0x4e, 0x47, 0xe0, 0x14, 0xc7, 0x00,
  11229. 0x45, 0x4e, 0x81, 0xe9, 0x53, 0x58, 0xa5, 0x69, 0x80, 0x8a, 0xa3, 0x8f,
  11230. 0x2a, 0x72, 0xa6, 0x23, 0x59, 0x91, 0x5a, 0x9f, 0x8a, 0x04, 0xca, 0x68
  11231. };
  11232. WOLFSSL_SMALL_STACK_STATIC const byte test2EntropyB[] =
  11233. {
  11234. 0xe6, 0x2b, 0x8a, 0x8e, 0xe8, 0xf1, 0x41, 0xb6, 0x98, 0x05, 0x66, 0xe3,
  11235. 0xbf, 0xe3, 0xc0, 0x49, 0x03, 0xda, 0xd4, 0xac, 0x2c, 0xdf, 0x9f, 0x22,
  11236. 0x80, 0x01, 0x0a, 0x67, 0x39, 0xbc, 0x83, 0xd3
  11237. };
  11238. WOLFSSL_SMALL_STACK_STATIC const byte test2Output[] =
  11239. {
  11240. 0x04, 0xee, 0xc6, 0x3b, 0xb2, 0x31, 0xdf, 0x2c, 0x63, 0x0a, 0x1a, 0xfb,
  11241. 0xe7, 0x24, 0x94, 0x9d, 0x00, 0x5a, 0x58, 0x78, 0x51, 0xe1, 0xaa, 0x79,
  11242. 0x5e, 0x47, 0x73, 0x47, 0xc8, 0xb0, 0x56, 0x62, 0x1c, 0x18, 0xbd, 0xdc,
  11243. 0xdd, 0x8d, 0x99, 0xfc, 0x5f, 0xc2, 0xb9, 0x20, 0x53, 0xd8, 0xcf, 0xac,
  11244. 0xfb, 0x0b, 0xb8, 0x83, 0x12, 0x05, 0xfa, 0xd1, 0xdd, 0xd6, 0xc0, 0x71,
  11245. 0x31, 0x8a, 0x60, 0x18, 0xf0, 0x3b, 0x73, 0xf5, 0xed, 0xe4, 0xd4, 0xd0,
  11246. 0x71, 0xf9, 0xde, 0x03, 0xfd, 0x7a, 0xea, 0x10, 0x5d, 0x92, 0x99, 0xb8,
  11247. 0xaf, 0x99, 0xaa, 0x07, 0x5b, 0xdb, 0x4d, 0xb9, 0xaa, 0x28, 0xc1, 0x8d,
  11248. 0x17, 0x4b, 0x56, 0xee, 0x2a, 0x01, 0x4d, 0x09, 0x88, 0x96, 0xff, 0x22,
  11249. 0x82, 0xc9, 0x55, 0xa8, 0x19, 0x69, 0xe0, 0x69, 0xfa, 0x8c, 0xe0, 0x07,
  11250. 0xa1, 0x80, 0x18, 0x3a, 0x07, 0xdf, 0xae, 0x17
  11251. };
  11252. byte output[WC_SHA256_DIGEST_SIZE * 4];
  11253. int ret;
  11254. ret = wc_RNG_HealthTest(0, test1Entropy, sizeof(test1Entropy), NULL, 0,
  11255. output, sizeof(output));
  11256. if (ret != 0)
  11257. return -7000;
  11258. if (XMEMCMP(test1Output, output, sizeof(output)) != 0)
  11259. return -7001;
  11260. ret = wc_RNG_HealthTest(1, test2EntropyA, sizeof(test2EntropyA),
  11261. test2EntropyB, sizeof(test2EntropyB),
  11262. output, sizeof(output));
  11263. if (ret != 0)
  11264. return -7002;
  11265. if (XMEMCMP(test2Output, output, sizeof(output)) != 0)
  11266. return -7003;
  11267. /* Basic RNG generate block test */
  11268. if ((ret = random_rng_test()) != 0)
  11269. return ret;
  11270. /* Test the seed check function. */
  11271. #if !(defined(HAVE_FIPS) || defined(HAVE_SELFTEST)) || \
  11272. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))
  11273. {
  11274. word32 i, outputSz;
  11275. /* Repeat the same byte over and over. Should fail. */
  11276. outputSz = sizeof(output);
  11277. XMEMSET(output, 1, outputSz);
  11278. ret = wc_RNG_TestSeed(output, outputSz);
  11279. if (ret == 0)
  11280. return -7004;
  11281. /* Every byte of the entropy scratch is different,
  11282. * entropy is a single byte that shouldn't match. */
  11283. outputSz = (sizeof(output) / 2) + 1;
  11284. for (i = 0; i < outputSz; i++)
  11285. output[i] = (byte)i;
  11286. ret = wc_RNG_TestSeed(output, outputSz);
  11287. if (ret != 0)
  11288. return -7005;
  11289. outputSz = sizeof(output);
  11290. for (i = 0; i < outputSz; i++)
  11291. output[i] = (byte)i;
  11292. ret = wc_RNG_TestSeed(output, outputSz);
  11293. if (ret != 0)
  11294. return -7006;
  11295. }
  11296. #endif
  11297. /* Test the seed callback. */
  11298. #ifdef WC_RNG_SEED_CB
  11299. if ((ret = rng_seed_test()) != 0)
  11300. return ret;
  11301. #endif
  11302. return 0;
  11303. }
  11304. #else
  11305. WOLFSSL_TEST_SUBROUTINE int random_test(void)
  11306. {
  11307. /* Basic RNG generate block test */
  11308. return random_rng_test();
  11309. }
  11310. #endif /* HAVE_HASHDRBG && !CUSTOM_RAND_GENERATE_BLOCK */
  11311. #endif /* WC_NO_RNG */
  11312. #ifndef MEM_TEST_SZ
  11313. #define MEM_TEST_SZ 1024
  11314. #endif
  11315. #if defined(WOLFSSL_STATIC_MEMORY) || !defined(WOLFSSL_NO_MALLOC)
  11316. static int simple_mem_test(int sz)
  11317. {
  11318. int ret = 0;
  11319. byte* b;
  11320. int i;
  11321. b = (byte*)XMALLOC(sz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11322. if (b == NULL) {
  11323. return -7110;
  11324. }
  11325. /* utilize memory */
  11326. for (i = 0; i < sz; i++) {
  11327. b[i] = (byte)i;
  11328. }
  11329. /* read back and verify */
  11330. for (i = 0; i < sz; i++) {
  11331. if (b[i] != (byte)i) {
  11332. ret = -7111;
  11333. break;
  11334. }
  11335. }
  11336. XFREE(b, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11337. return ret;
  11338. }
  11339. #endif
  11340. WOLFSSL_TEST_SUBROUTINE int memory_test(void)
  11341. {
  11342. int ret = 0;
  11343. #if defined(COMPLEX_MEM_TEST) || defined(WOLFSSL_STATIC_MEMORY)
  11344. int i;
  11345. #endif
  11346. #ifdef WOLFSSL_STATIC_MEMORY
  11347. word32 size[] = { WOLFMEM_BUCKETS };
  11348. word32 dist[] = { WOLFMEM_DIST };
  11349. byte buffer[30000]; /* make large enough to involve many bucket sizes */
  11350. int pad = -(int)((wc_ptr_t)buffer) & (WOLFSSL_STATIC_ALIGN - 1);
  11351. /* pad to account for if head of buffer is not at set memory
  11352. * alignment when tests are ran */
  11353. #endif
  11354. #ifdef WOLFSSL_STATIC_MEMORY
  11355. /* check macro settings */
  11356. if (sizeof(size)/sizeof(word32) != WOLFMEM_MAX_BUCKETS) {
  11357. return -7200;
  11358. }
  11359. if (sizeof(dist)/sizeof(word32) != WOLFMEM_MAX_BUCKETS) {
  11360. return -7201;
  11361. }
  11362. for (i = 0; i < WOLFMEM_MAX_BUCKETS; i++) {
  11363. if ((size[i] % WOLFSSL_STATIC_ALIGN) != 0) {
  11364. /* each element in array should be divisible by alignment size */
  11365. return -7202;
  11366. }
  11367. }
  11368. for (i = 1; i < WOLFMEM_MAX_BUCKETS; i++) {
  11369. if (size[i - 1] >= size[i]) {
  11370. return -7203; /* sizes should be in increasing order */
  11371. }
  11372. }
  11373. /* check that padding size returned is possible */
  11374. if (wolfSSL_MemoryPaddingSz() < WOLFSSL_STATIC_ALIGN) {
  11375. return -7204; /* no room for wc_Memory struct */
  11376. }
  11377. if (wolfSSL_MemoryPaddingSz() < 0) {
  11378. return -7205;
  11379. }
  11380. if (wolfSSL_MemoryPaddingSz() % WOLFSSL_STATIC_ALIGN != 0) {
  11381. return -7206; /* not aligned! */
  11382. }
  11383. /* check function to return optimum buffer size (rounded down) */
  11384. ret = wolfSSL_StaticBufferSz(buffer, sizeof(buffer), WOLFMEM_GENERAL);
  11385. if ((ret - pad) % WOLFSSL_STATIC_ALIGN != 0) {
  11386. return -7207; /* not aligned! */
  11387. }
  11388. if (ret < 0) {
  11389. return -7208;
  11390. }
  11391. if ((unsigned int)ret > sizeof(buffer)) {
  11392. return -7209; /* did not round down as expected */
  11393. }
  11394. if (ret != wolfSSL_StaticBufferSz(buffer, ret, WOLFMEM_GENERAL)) {
  11395. return -7210; /* return value changed when using suggested value */
  11396. }
  11397. ret = wolfSSL_MemoryPaddingSz();
  11398. ret += pad; /* add space that is going to be needed if buffer not aligned */
  11399. if (wolfSSL_StaticBufferSz(buffer, size[0] + ret + 1, WOLFMEM_GENERAL) !=
  11400. (ret + (int)size[0])) {
  11401. return -7211; /* did not round down to nearest bucket value */
  11402. }
  11403. ret = wolfSSL_StaticBufferSz(buffer, sizeof(buffer), WOLFMEM_IO_POOL);
  11404. if ((ret - pad) < 0) {
  11405. return -7212;
  11406. }
  11407. if (((ret - pad) % (WOLFMEM_IO_SZ + wolfSSL_MemoryPaddingSz())) != 0) {
  11408. return -7213; /* not even chunks of memory for IO size */
  11409. }
  11410. if (((ret - pad) % WOLFSSL_STATIC_ALIGN) != 0) {
  11411. return -7214; /* memory not aligned */
  11412. }
  11413. /* check for passing bad or unknown arguments to functions */
  11414. if (wolfSSL_StaticBufferSz(NULL, 1, WOLFMEM_GENERAL) > 0) {
  11415. return -7215;
  11416. }
  11417. if (wolfSSL_StaticBufferSz(buffer, 1, WOLFMEM_GENERAL) != 0) {
  11418. return -7216; /* should round to 0 since struct + bucket will not fit */
  11419. }
  11420. (void)dist; /* avoid static analysis warning of variable not used */
  11421. #endif
  11422. #if defined(WOLFSSL_STATIC_MEMORY) || !defined(WOLFSSL_NO_MALLOC)
  11423. /* simple test */
  11424. ret = simple_mem_test(MEM_TEST_SZ);
  11425. if (ret != 0)
  11426. return ret;
  11427. #endif
  11428. #ifdef COMPLEX_MEM_TEST
  11429. /* test various size blocks */
  11430. for (i = 1; i < MEM_TEST_SZ; i*=2) {
  11431. ret = simple_mem_test(i);
  11432. if (ret != 0)
  11433. return ret;
  11434. }
  11435. #endif
  11436. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_NO_MALLOC)
  11437. /* realloc test */
  11438. {
  11439. byte *c = NULL;
  11440. byte *b = (byte*)XMALLOC(MEM_TEST_SZ, HEAP_HINT,
  11441. DYNAMIC_TYPE_TMP_BUFFER);
  11442. if (b) {
  11443. c = (byte*)XREALLOC(b, MEM_TEST_SZ+sizeof(word32), HEAP_HINT,
  11444. DYNAMIC_TYPE_TMP_BUFFER);
  11445. if (c)
  11446. b = c;
  11447. }
  11448. if (b)
  11449. XFREE(b, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11450. if ((b == NULL) || (c == NULL)) {
  11451. return -7217;
  11452. }
  11453. }
  11454. #endif
  11455. return ret;
  11456. }
  11457. #ifndef NO_FILESYSTEM
  11458. /* Cert Paths */
  11459. #ifdef FREESCALE_MQX
  11460. #define CERT_PREFIX "a:\\"
  11461. #define CERT_PATH_SEP "\\"
  11462. #elif defined(WOLFSSL_uTKERNEL2)
  11463. #define CERT_PREFIX "/uda/"
  11464. #define CERT_PATH_SEP "/"
  11465. #elif defined(_WIN32_WCE)
  11466. #define CERT_PREFIX "\\windows\\"
  11467. #define CERT_PATH_SEP "\\"
  11468. #endif
  11469. #ifndef CERT_PREFIX
  11470. #define CERT_PREFIX "./"
  11471. #endif
  11472. #ifndef CERT_PATH_SEP
  11473. #define CERT_PATH_SEP "/"
  11474. #endif
  11475. #ifndef CERT_WRITE_TEMP_DIR
  11476. #define CERT_WRITE_TEMP_DIR CERT_PREFIX
  11477. #endif
  11478. #define CERT_ROOT CERT_PREFIX "certs" CERT_PATH_SEP
  11479. /* Generated Test Certs */
  11480. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  11481. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096)
  11482. #if !defined(NO_RSA) && !defined(NO_ASN)
  11483. static const char* clientKey = CERT_ROOT "client-key.der";
  11484. static const char* clientCert = CERT_ROOT "client-cert.der";
  11485. #ifdef WOLFSSL_CERT_EXT
  11486. static const char* clientKeyPub = CERT_ROOT "client-keyPub.der";
  11487. #endif
  11488. #endif /* !NO_RSA && !NO_ASN */
  11489. #endif
  11490. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  11491. #if !defined(NO_RSA) && !defined(NO_ASN)
  11492. #if defined(WOLFSSL_CERT_GEN) || defined(HAVE_PKCS7)
  11493. static const char* rsaCaKeyFile = CERT_ROOT "ca-key.der";
  11494. #ifdef WOLFSSL_CERT_GEN
  11495. static const char* rsaCaCertFile = CERT_ROOT "ca-cert.pem";
  11496. #endif
  11497. #if defined(WOLFSSL_ALT_NAMES) || defined(HAVE_PKCS7)
  11498. static const char* rsaCaCertDerFile = CERT_ROOT "ca-cert.der";
  11499. #endif
  11500. #ifdef HAVE_PKCS7
  11501. static const char* rsaServerCertDerFile =
  11502. CERT_ROOT "server-cert.der";
  11503. static const char* rsaServerKeyDerFile =
  11504. CERT_ROOT "server-key.der";
  11505. #endif
  11506. #endif
  11507. #endif /* !NO_RSA && !NO_ASN */
  11508. #endif /* !USE_CERT_BUFFER_* */
  11509. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  11510. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096)
  11511. #if !defined(NO_ASN) && !defined(NO_DH)
  11512. static const char* dhParamsFile = CERT_ROOT "dh2048.der";
  11513. #endif
  11514. #endif
  11515. #if !defined(NO_ASN) && !defined(NO_DH)
  11516. #if defined(WOLFSSL_DH_EXTRA) && (!defined(HAVE_FIPS) || \
  11517. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  11518. static const char* dhKeyFile = CERT_ROOT "statickeys/dh-ffdhe2048.der";
  11519. static const char* dhKeyPubFile = CERT_ROOT "statickeys/dh-ffdhe2048-pub.der";
  11520. #endif
  11521. #endif
  11522. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  11523. #ifndef NO_DSA
  11524. static const char* dsaKey = CERT_ROOT "dsa2048.der";
  11525. #endif
  11526. #endif /* !USE_CERT_BUFFER_* */
  11527. #if !defined(USE_CERT_BUFFERS_256) && !defined(NO_ECC256)
  11528. #ifdef HAVE_ECC
  11529. /* cert files to be used in rsa cert gen test, check if RSA enabled */
  11530. #ifdef HAVE_ECC_KEY_IMPORT
  11531. static const char* eccKeyDerFile = CERT_ROOT "ecc-key.der";
  11532. #endif
  11533. #endif
  11534. #if !defined(USE_CERT_BUFFERS_256) && !defined(NO_ASN)
  11535. #if defined(HAVE_ECC) && defined(WOLFSSL_CERT_GEN)
  11536. #ifndef NO_RSA
  11537. static const char* eccKeyPubFileDer = CERT_ROOT "ecc-keyPub.der";
  11538. #endif
  11539. static const char* eccCaKeyFile = CERT_ROOT "ca-ecc-key.der";
  11540. static const char* eccCaCertFile = CERT_ROOT "ca-ecc-cert.pem";
  11541. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  11542. static const char* eccCaKey384File =
  11543. CERT_ROOT "ca-ecc384-key.der";
  11544. static const char* eccCaCert384File =
  11545. CERT_ROOT "ca-ecc384-cert.pem";
  11546. #endif
  11547. #endif
  11548. #if defined(HAVE_PKCS7) && defined(HAVE_ECC)
  11549. static const char* eccClientKey = CERT_ROOT "ecc-client-key.der";
  11550. static const char* eccClientCert = CERT_ROOT "client-ecc-cert.der";
  11551. #endif
  11552. #endif /* HAVE_ECC */
  11553. #ifdef HAVE_ED25519
  11554. #ifdef WOLFSSL_TEST_CERT
  11555. static const char* serverEd25519Cert =
  11556. CERT_ROOT "ed25519/server-ed25519.der";
  11557. static const char* caEd25519Cert =
  11558. CERT_ROOT "ed25519/ca-ed25519.der";
  11559. #endif
  11560. #endif
  11561. #ifdef HAVE_ED448
  11562. #ifdef WOLFSSL_TEST_CERT
  11563. static const char* serverEd448Cert =
  11564. CERT_ROOT "ed448/server-ed448.der";
  11565. static const char* caEd448Cert = CERT_ROOT "ed448/ca-ed448.der";
  11566. #endif
  11567. #endif
  11568. #endif /* !USE_CERT_BUFFER_* */
  11569. #if !defined(NO_ASN_TIME) && !defined(NO_RSA) && defined(WOLFSSL_TEST_CERT) && \
  11570. !defined(NO_FILESYSTEM)
  11571. static const char* certExtNc =
  11572. CERT_ROOT "test" CERT_PATH_SEP "cert-ext-nc.der";
  11573. static const char* certExtIa =
  11574. CERT_ROOT "test" CERT_PATH_SEP "cert-ext-ia.der";
  11575. static const char* certExtNct =
  11576. CERT_ROOT "test" CERT_PATH_SEP "cert-ext-nct.der";
  11577. #endif
  11578. #ifndef NO_WRITE_TEMP_FILES
  11579. #ifdef HAVE_ECC
  11580. #ifdef WOLFSSL_CERT_GEN
  11581. static const char* certEccPemFile = CERT_WRITE_TEMP_DIR "certecc.pem";
  11582. static const char* certEccDerFile = CERT_WRITE_TEMP_DIR "certecc.der";
  11583. #endif
  11584. #if defined(WOLFSSL_CERT_GEN) && !defined(NO_RSA)
  11585. static const char* certEccRsaPemFile = CERT_WRITE_TEMP_DIR "certeccrsa.pem";
  11586. static const char* certEccRsaDerFile = CERT_WRITE_TEMP_DIR "certeccrsa.der";
  11587. #endif
  11588. #if defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG) && \
  11589. !defined(WOLF_CRYPTO_CB_ONLY_ECC) && !defined(NO_ASN_CRYPT)
  11590. static const char* eccCaKeyPemFile = CERT_WRITE_TEMP_DIR "ecc-key.pem";
  11591. static const char* eccPubKeyDerFile = CERT_WRITE_TEMP_DIR "ecc-public-key.der";
  11592. static const char* eccCaKeyTempFile = CERT_WRITE_TEMP_DIR "ecc-key.der";
  11593. #if defined(HAVE_PKCS8) && !defined(WC_NO_RNG) && \
  11594. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  11595. static const char* eccPkcs8KeyDerFile = CERT_WRITE_TEMP_DIR "ecc-key-pkcs8.der";
  11596. #endif
  11597. #endif /* HAVE_ECC_KEY_EXPORT */
  11598. #endif /* HAVE_ECC */
  11599. #ifndef NO_RSA
  11600. #ifdef WOLFSSL_CERT_GEN
  11601. static const char* otherCertDerFile = CERT_WRITE_TEMP_DIR "othercert.der";
  11602. static const char* certDerFile = CERT_WRITE_TEMP_DIR "cert.der";
  11603. static const char* otherCertPemFile = CERT_WRITE_TEMP_DIR "othercert.pem";
  11604. static const char* certPemFile = CERT_WRITE_TEMP_DIR "cert.pem";
  11605. #if defined(WOLFSSL_CERT_REQ) && !defined(WOLFSSL_NO_MALLOC)
  11606. static const char* certReqDerFile = CERT_WRITE_TEMP_DIR "certreq.der";
  11607. static const char* certReqPemFile = CERT_WRITE_TEMP_DIR "certreq.pem";
  11608. #endif
  11609. #endif
  11610. #endif /* !NO_RSA */
  11611. #if !defined(NO_RSA) || !defined(NO_DSA)
  11612. #ifdef WOLFSSL_KEY_GEN
  11613. static const char* keyDerFile = CERT_WRITE_TEMP_DIR "key.der";
  11614. static const char* keyPemFile = CERT_WRITE_TEMP_DIR "key.pem";
  11615. #endif
  11616. #endif
  11617. #endif /* !NO_WRITE_TEMP_FILES */
  11618. #endif /* !NO_FILESYSTEM */
  11619. #if defined(WOLFSSL_CERT_GEN) && (!defined(NO_RSA) || defined(HAVE_ECC)) || \
  11620. (defined(WOLFSSL_TEST_CERT) && (defined(HAVE_ED25519) || defined(HAVE_ED448)))
  11621. static CertName certDefaultName;
  11622. static void initDefaultName(void)
  11623. {
  11624. #if defined(WOLFSSL_MULTI_ATTRIB) && defined(WOLFSSL_TEST_CERT)
  11625. NameAttrib* n;
  11626. #endif
  11627. XMEMCPY(certDefaultName.country, "US", sizeof("US"));
  11628. certDefaultName.countryEnc = CTC_PRINTABLE;
  11629. XMEMCPY(certDefaultName.state, "Oregon", sizeof("Oregon"));
  11630. certDefaultName.stateEnc = CTC_UTF8;
  11631. XMEMCPY(certDefaultName.street, "Main St", sizeof("Main St"));
  11632. certDefaultName.streetEnc = CTC_UTF8;
  11633. XMEMCPY(certDefaultName.locality, "Portland", sizeof("Portland"));
  11634. certDefaultName.localityEnc = CTC_UTF8;
  11635. XMEMCPY(certDefaultName.sur, "Test", sizeof("Test"));
  11636. certDefaultName.surEnc = CTC_UTF8;
  11637. XMEMCPY(certDefaultName.org, "wolfSSL", sizeof("wolfSSL"));
  11638. certDefaultName.orgEnc = CTC_UTF8;
  11639. XMEMCPY(certDefaultName.unit, "Development", sizeof("Development"));
  11640. certDefaultName.unitEnc = CTC_UTF8;
  11641. XMEMCPY(certDefaultName.commonName, "www.wolfssl.com", sizeof("www.wolfssl.com"));
  11642. certDefaultName.commonNameEnc = CTC_UTF8;
  11643. XMEMCPY(certDefaultName.serialDev, "wolfSSL12345", sizeof("wolfSSL12345"));
  11644. certDefaultName.serialDevEnc = CTC_PRINTABLE;
  11645. XMEMCPY(certDefaultName.postalCode, "12-456", sizeof("12-456"));
  11646. certDefaultName.postalCodeEnc = CTC_PRINTABLE;
  11647. #ifdef WOLFSSL_CERT_EXT
  11648. XMEMCPY(certDefaultName.busCat, "Private Organization", sizeof("Private Organization"));
  11649. certDefaultName.busCatEnc = CTC_UTF8;
  11650. XMEMCPY(certDefaultName.joiSt, "US", sizeof("US"));
  11651. certDefaultName.joiStEnc = CTC_PRINTABLE;
  11652. XMEMCPY(certDefaultName.joiC, "Oregon", sizeof("Oregon"));
  11653. certDefaultName.joiCEnc = CTC_PRINTABLE;
  11654. #endif
  11655. XMEMCPY(certDefaultName.email, "info@wolfssl.com", sizeof("info@wolfssl.com"));
  11656. XMEMCPY(certDefaultName.userId, "TestUserID", sizeof("TestUserID"));
  11657. certDefaultName.userIdEnc = CTC_PRINTABLE;
  11658. #if defined(WOLFSSL_MULTI_ATTRIB) && defined(WOLFSSL_TEST_CERT)
  11659. /* test having additional OUs and setting DC */
  11660. n = &certDefaultName.name[0];
  11661. n->id = ASN_ORGUNIT_NAME;
  11662. n->type = CTC_UTF8;
  11663. n->sz = sizeof("Development-2");
  11664. XMEMCPY(n->value, "Development-2", sizeof("Development-2"));
  11665. #if CTC_MAX_ATTRIB > 3
  11666. n = &certDefaultName.name[1];
  11667. n->id = ASN_DOMAIN_COMPONENT;
  11668. n->type = CTC_UTF8;
  11669. n->sz = sizeof("com");
  11670. XMEMCPY(n->value, "com", sizeof("com"));
  11671. n = &certDefaultName.name[2];
  11672. n->id = ASN_DOMAIN_COMPONENT;
  11673. n->type = CTC_UTF8;
  11674. n->sz = sizeof("wolfssl");
  11675. XMEMCPY(n->value, "wolfssl", sizeof("wolfssl"));
  11676. #endif
  11677. #endif /* WOLFSSL_MULTI_ATTRIB && WOLFSSL_TEST_CERT */
  11678. #ifdef WOLFSSL_CUSTOM_OID
  11679. /* TODO: Add test case for custom OID's */
  11680. #endif
  11681. }
  11682. #ifdef WOLFSSL_CERT_EXT
  11683. #if ((defined(HAVE_ED25519) || defined(HAVE_ED448)) && \
  11684. defined(WOLFSSL_TEST_CERT)) || defined(HAVE_ECC)
  11685. WOLFSSL_SMALL_STACK_STATIC const char certKeyUsage[] =
  11686. "digitalSignature,nonRepudiation";
  11687. #endif
  11688. #if defined(WOLFSSL_CERT_REQ) && !defined(NO_RSA)
  11689. WOLFSSL_SMALL_STACK_STATIC const char certKeyUsage2[] =
  11690. "digitalSignature,nonRepudiation,keyEncipherment,keyAgreement";
  11691. #endif
  11692. #endif /* WOLFSSL_CERT_EXT */
  11693. #endif /* WOLFSSL_CERT_GEN */
  11694. #ifndef NO_RSA
  11695. #if !defined(NO_ASN_TIME) && !defined(NO_RSA) && defined(WOLFSSL_TEST_CERT) && \
  11696. !defined(NO_FILESYSTEM)
  11697. static byte minSerial[] = { 0x02, 0x01, 0x01 };
  11698. static byte minName[] = { 0x30, 0x00 };
  11699. static byte nameBad[] = {
  11700. 0x30, 0x08,
  11701. 0x31, 0x06,
  11702. 0x30, 0x04,
  11703. 0x06, 0x02,
  11704. 0x55, 0x04,
  11705. };
  11706. static byte minDates[] = {
  11707. 0x30, 0x1e,
  11708. 0x17, 0x0d,
  11709. 0x31, 0x38, 0x30, 0x34, 0x31, 0x33, 0x31, 0x35,
  11710. 0x32, 0x33, 0x31, 0x30, 0x5a,
  11711. 0x17, 0x0d,
  11712. 0x32, 0x31, 0x30, 0x31, 0x30, 0x37, 0x31, 0x35,
  11713. 0x32, 0x33, 0x31, 0x30, 0x5a
  11714. };
  11715. static byte minPubKey[] = {
  11716. 0x30, 0x1c,
  11717. 0x30, 0x0d,
  11718. 0x06, 0x09,
  11719. 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01,
  11720. 0x01,
  11721. 0x05, 0x00,
  11722. 0x03, 0x0b,
  11723. 0x00, 0x30, 0x08,
  11724. 0x02, 0x01,
  11725. 0x03,
  11726. 0x02, 0x03,
  11727. 0x01, 0x00, 0x01
  11728. };
  11729. static byte minSigAlg[] = {
  11730. 0x30, 0x0d,
  11731. 0x06, 0x09,
  11732. 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01,
  11733. 0x0b,
  11734. 0x05, 0x00
  11735. };
  11736. static byte minSig[] = {
  11737. 0x03, 0x01,
  11738. 0x00
  11739. };
  11740. static int add_seq(byte* certData, int offset, byte* data, byte length)
  11741. {
  11742. XMEMMOVE(certData + offset + 2, data, length);
  11743. certData[offset++] = 0x30;
  11744. certData[offset++] = length;
  11745. return offset + length;
  11746. }
  11747. static int add_data(byte* certData, int offset, byte* data, byte length)
  11748. {
  11749. XMEMCPY(certData + offset, data, length);
  11750. return offset + length;
  11751. }
  11752. static int cert_asn1_test(void)
  11753. {
  11754. int ret;
  11755. int len[3];
  11756. DecodedCert cert;
  11757. byte certData[114];
  11758. byte* badCert = NULL;
  11759. len[2] = add_data(certData, 0, minSerial, (byte)sizeof(minSerial));
  11760. len[2] = add_data(certData, len[2], minSigAlg, (byte)sizeof(minSigAlg));
  11761. len[2] = add_data(certData, len[2], minName, (byte)sizeof(minName));
  11762. len[2] = add_data(certData, len[2], minDates, (byte)sizeof(minDates));
  11763. len[2] = add_data(certData, len[2], minName, (byte)sizeof(minName));
  11764. len[2] = add_data(certData, len[2], minPubKey, (byte)sizeof(minPubKey));
  11765. len[1] = add_seq(certData, 0, certData, len[2]);
  11766. len[1] = add_data(certData, len[1], minSigAlg, (byte)sizeof(minSigAlg));
  11767. len[1] = add_data(certData, len[1], minSig, (byte)sizeof(minSig));
  11768. len[0] = add_seq(certData, 0, certData, len[1]);
  11769. /* Minimal good certificate */
  11770. InitDecodedCert(&cert, certData, len[0], 0);
  11771. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL);
  11772. FreeDecodedCert(&cert);
  11773. if (ret != 0) {
  11774. ERROR_OUT(-7300, done);
  11775. }
  11776. /* Bad issuer name */
  11777. len[2] = add_data(certData, 0, minSerial, (byte)sizeof(minSerial));
  11778. len[2] = add_data(certData, len[2], minSigAlg, (byte)sizeof(minSigAlg));
  11779. len[2] = add_data(certData, len[2], nameBad, (byte)sizeof(nameBad));
  11780. len[2] = add_data(certData, len[2], minDates, (byte)sizeof(minDates));
  11781. len[2] = add_data(certData, len[2], minName, (byte)sizeof(minName));
  11782. len[2] = add_data(certData, len[2], minPubKey, (byte)sizeof(minPubKey));
  11783. len[1] = add_seq(certData, 0, certData, len[2]);
  11784. len[1] = add_data(certData, len[1], minSigAlg, (byte)sizeof(minSigAlg));
  11785. len[1] = add_data(certData, len[1], minSig, (byte)sizeof(minSig));
  11786. len[0] = add_seq(certData, 0, certData, len[1]);
  11787. /* Put data into allocated buffer to allow access error checking. */
  11788. badCert = (byte*)XMALLOC(len[0], HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11789. XMEMCPY(badCert, certData, len[0]);
  11790. InitDecodedCert(&cert, badCert, len[0], 0);
  11791. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL);
  11792. FreeDecodedCert(&cert);
  11793. if (ret != ASN_PARSE_E) {
  11794. ERROR_OUT(-7301, done);
  11795. }
  11796. XFREE(badCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11797. badCert = NULL;
  11798. ret = 0;
  11799. done:
  11800. if (badCert != NULL)
  11801. XFREE(badCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11802. return ret;
  11803. }
  11804. WOLFSSL_TEST_SUBROUTINE int cert_test(void)
  11805. {
  11806. #if !defined(NO_FILESYSTEM)
  11807. DecodedCert cert;
  11808. byte* tmp;
  11809. size_t bytes;
  11810. XFILE file;
  11811. int ret;
  11812. tmp = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11813. if (tmp == NULL)
  11814. return -7400;
  11815. /* Certificate with Name Constraints extension. */
  11816. file = XFOPEN(certExtNc, "rb");
  11817. if (!file) {
  11818. ERROR_OUT(-7401, done);
  11819. }
  11820. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  11821. XFCLOSE(file);
  11822. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  11823. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL);
  11824. if (ret != 0) {
  11825. ERROR_OUT(-7402, done);
  11826. }
  11827. FreeDecodedCert(&cert);
  11828. /* Certificate with Inhibit Any Policy extension. */
  11829. file = XFOPEN(certExtIa, "rb");
  11830. if (!file) {
  11831. ERROR_OUT(-7403, done);
  11832. }
  11833. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  11834. XFCLOSE(file);
  11835. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  11836. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL);
  11837. if (ret != 0) {
  11838. ERROR_OUT(-7404, done);
  11839. }
  11840. FreeDecodedCert(&cert);
  11841. /* Certificate with Netscape Certificate Type extension. */
  11842. file = XFOPEN(certExtNct, "rb");
  11843. if (!file) {
  11844. ERROR_OUT(-7405, done);
  11845. }
  11846. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  11847. XFCLOSE(file);
  11848. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  11849. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL);
  11850. #ifndef IGNORE_NETSCAPE_CERT_TYPE
  11851. if (ret != 0) {
  11852. ERROR_OUT(-7406, done);
  11853. }
  11854. #else
  11855. if (ret != ASN_CRIT_EXT_E) {
  11856. ERROR_OUT(-7407, done);
  11857. }
  11858. ret = 0;
  11859. #endif
  11860. done:
  11861. FreeDecodedCert(&cert);
  11862. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11863. #endif /* !NO_FILESYSTEM */
  11864. if (ret == 0)
  11865. ret = cert_asn1_test();
  11866. return ret;
  11867. }
  11868. #endif /* WOLFSSL_TEST_CERT */
  11869. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_TEST_CERT) && \
  11870. !defined(NO_FILESYSTEM) && defined(WOLFSSL_CERT_GEN)
  11871. WOLFSSL_TEST_SUBROUTINE int certext_test(void)
  11872. {
  11873. DecodedCert cert;
  11874. byte* tmp;
  11875. size_t bytes;
  11876. XFILE file;
  11877. int ret;
  11878. /* created from rsa_test : othercert.der */
  11879. byte skid_rsa[] = "\x33\xD8\x45\x66\xD7\x68\x87\x18\x7E\x54"
  11880. "\x0D\x70\x27\x91\xC7\x26\xD7\x85\x65\xC0";
  11881. /* created from rsa_test : othercert.der */
  11882. byte akid_rsa[] = "\x27\x8E\x67\x11\x74\xC3\x26\x1D\x3F\xED"
  11883. "\x33\x63\xB3\xA4\xD8\x1D\x30\xE5\xE8\xD5";
  11884. #ifdef HAVE_ECC
  11885. /* created from ecc_test_cert_gen : certecc.der */
  11886. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  11887. /* Authority key id from ./certs/ca-ecc384-cert.pem */
  11888. byte akid_ecc[] = "\xAB\xE0\xC3\x26\x4C\x18\xD4\x72\xBB\xD2"
  11889. "\x84\x8C\x9C\x0A\x05\x92\x80\x12\x53\x52";
  11890. #else
  11891. /* Authority key id from ./certs/ca-ecc-cert.pem */
  11892. byte akid_ecc[] = "\x56\x8E\x9A\xC3\xF0\x42\xDE\x18\xB9\x45"
  11893. "\x55\x6E\xF9\x93\xCF\xEA\xC3\xF3\xA5\x21";
  11894. #endif
  11895. #endif /* HAVE_ECC */
  11896. /* created from rsa_test : cert.der */
  11897. byte kid_ca[] = "\x33\xD8\x45\x66\xD7\x68\x87\x18\x7E\x54"
  11898. "\x0D\x70\x27\x91\xC7\x26\xD7\x85\x65\xC0";
  11899. tmp = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11900. if (tmp == NULL)
  11901. return -7500;
  11902. /* load othercert.der (Cert signed by an authority) */
  11903. file = XFOPEN(otherCertDerFile, "rb");
  11904. if (!file) {
  11905. XFREE(tmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  11906. return -7501;
  11907. }
  11908. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  11909. XFCLOSE(file);
  11910. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  11911. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, 0);
  11912. if (ret != 0)
  11913. return -7502;
  11914. /* check the SKID from a RSA certificate */
  11915. if (XMEMCMP(skid_rsa, cert.extSubjKeyId, sizeof(cert.extSubjKeyId)))
  11916. return -7503;
  11917. /* check the AKID from an RSA certificate */
  11918. if (XMEMCMP(akid_rsa, cert.extAuthKeyId, sizeof(cert.extAuthKeyId)))
  11919. return -7504;
  11920. /* check the Key Usage from an RSA certificate */
  11921. if (!cert.extKeyUsageSet)
  11922. return -7505;
  11923. if (cert.extKeyUsage != (KEYUSE_KEY_ENCIPHER|KEYUSE_KEY_AGREE))
  11924. return -7506;
  11925. /* check the CA Basic Constraints from an RSA certificate */
  11926. if (cert.isCA)
  11927. return -7507;
  11928. #ifndef WOLFSSL_SEP /* test only if not using SEP policies */
  11929. /* check the Certificate Policies Id */
  11930. if (cert.extCertPoliciesNb != 1)
  11931. return -7508;
  11932. if (strncmp(cert.extCertPolicies[0], "2.16.840.1.101.3.4.1.42", 23))
  11933. return -7509;
  11934. #endif
  11935. FreeDecodedCert(&cert);
  11936. #ifdef HAVE_ECC
  11937. /* load certecc.der (Cert signed by our ECC CA test in ecc_test_cert_gen) */
  11938. file = XFOPEN(certEccDerFile, "rb");
  11939. if (!file) {
  11940. XFREE(tmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  11941. return -7510;
  11942. }
  11943. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  11944. XFCLOSE(file);
  11945. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  11946. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, 0);
  11947. if (ret != 0)
  11948. return -7511;
  11949. /* check the SKID from a ECC certificate - generated dynamically */
  11950. /* check the AKID from an ECC certificate */
  11951. if (XMEMCMP(akid_ecc, cert.extAuthKeyId, sizeof(cert.extAuthKeyId)))
  11952. return -7512;
  11953. /* check the Key Usage from an ECC certificate */
  11954. if (!cert.extKeyUsageSet)
  11955. return -7513;
  11956. if (cert.extKeyUsage != (KEYUSE_DIGITAL_SIG|KEYUSE_CONTENT_COMMIT))
  11957. return -7514;
  11958. /* check the CA Basic Constraints from an ECC certificate */
  11959. if (cert.isCA)
  11960. return -7515;
  11961. #ifndef WOLFSSL_SEP /* test only if not using SEP policies */
  11962. /* check the Certificate Policies Id */
  11963. if (cert.extCertPoliciesNb != 2)
  11964. return -7516;
  11965. if (strncmp(cert.extCertPolicies[0], "2.4.589440.587.101.2.1.9632587.1", 32))
  11966. return -7517;
  11967. if (strncmp(cert.extCertPolicies[1], "1.2.13025.489.1.113549", 22))
  11968. return -7518;
  11969. #endif
  11970. FreeDecodedCert(&cert);
  11971. #endif /* HAVE_ECC */
  11972. /* load cert.der (self signed certificate) */
  11973. file = XFOPEN(certDerFile, "rb");
  11974. if (!file) {
  11975. XFREE(tmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  11976. return -7519;
  11977. }
  11978. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  11979. XFCLOSE(file);
  11980. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  11981. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, 0);
  11982. if (ret != 0)
  11983. return -7520;
  11984. /* check the SKID from a CA certificate */
  11985. if (XMEMCMP(kid_ca, cert.extSubjKeyId, sizeof(cert.extSubjKeyId)))
  11986. return -7521;
  11987. /* check the AKID from an CA certificate */
  11988. if (XMEMCMP(kid_ca, cert.extAuthKeyId, sizeof(cert.extAuthKeyId)))
  11989. return -7522;
  11990. /* check the Key Usage from CA certificate */
  11991. if (!cert.extKeyUsageSet)
  11992. return -7523;
  11993. if (cert.extKeyUsage != (KEYUSE_KEY_CERT_SIGN|KEYUSE_CRL_SIGN))
  11994. return -7524;
  11995. /* check the CA Basic Constraints CA certificate */
  11996. if (!cert.isCA)
  11997. return -7525;
  11998. #ifndef WOLFSSL_SEP /* test only if not using SEP policies */
  11999. /* check the Certificate Policies Id */
  12000. if (cert.extCertPoliciesNb != 2)
  12001. return -7526;
  12002. if (strncmp(cert.extCertPolicies[0], "2.16.840.1.101.3.4.1.42", 23))
  12003. return -7527;
  12004. if (strncmp(cert.extCertPolicies[1], "1.2.840.113549.1.9.16.6.5", 25))
  12005. return -7528;
  12006. #endif
  12007. FreeDecodedCert(&cert);
  12008. XFREE(tmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  12009. return 0;
  12010. }
  12011. #endif /* WOLFSSL_CERT_EXT && WOLFSSL_TEST_CERT &&
  12012. !NO_FILESYSTEM && WOLFSSL_CERT_GEN */
  12013. #if defined(WOLFSSL_CERT_GEN_CACHE) && defined(WOLFSSL_TEST_CERT) && \
  12014. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  12015. WOLFSSL_TEST_SUBROUTINE int decodedCertCache_test(void)
  12016. {
  12017. int ret = 0;
  12018. Cert cert;
  12019. FILE* file;
  12020. byte* der;
  12021. word32 derSz;
  12022. derSz = FOURK_BUF;
  12023. der = (byte *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12024. if (der == NULL)
  12025. ret = -7600;
  12026. if (ret == 0) {
  12027. /* load cert.der */
  12028. file = XFOPEN(certDerFile, "rb");
  12029. if (file != NULL) {
  12030. derSz = (word32)XFREAD(der, 1, FOURK_BUF, file);
  12031. XFCLOSE(file);
  12032. }
  12033. else
  12034. ret = -7601;
  12035. }
  12036. if (ret == 0) {
  12037. if (wc_InitCert_ex(&cert, HEAP_HINT, devId)) {
  12038. ret = -7602;
  12039. }
  12040. }
  12041. if (ret == 0) {
  12042. ret = wc_SetSubjectBuffer(&cert, der, derSz);
  12043. }
  12044. if (ret == 0) {
  12045. if(wc_SetSubjectBuffer(NULL, der, derSz) != BAD_FUNC_ARG)
  12046. ret = -7603;
  12047. }
  12048. if (ret == 0) {
  12049. if (wc_SetSubjectRaw(&cert, der, derSz) != 0)
  12050. ret = -7604;
  12051. }
  12052. if (ret == 0) {
  12053. if(wc_SetSubjectRaw(NULL, der, derSz) != BAD_FUNC_ARG)
  12054. ret = -7605;
  12055. }
  12056. if (ret == 0) {
  12057. if(wc_SetIssuerBuffer(&cert, der, derSz) != 0)
  12058. ret = -7606;
  12059. }
  12060. if (ret == 0) {
  12061. if(wc_SetIssuerBuffer(NULL, der, derSz) != BAD_FUNC_ARG)
  12062. ret = -7607;
  12063. }
  12064. if (ret == 0) {
  12065. if(wc_SetIssuerRaw(&cert, der, derSz) != 0)
  12066. ret = -7608;
  12067. }
  12068. if (ret == 0) {
  12069. if(wc_SetIssuerRaw(NULL, der, derSz) != BAD_FUNC_ARG)
  12070. ret = -7609;
  12071. }
  12072. #ifdef WOLFSSL_ALT_NAMES
  12073. if (ret == 0) {
  12074. if(wc_SetAltNamesBuffer(&cert, der, derSz) != 0)
  12075. ret = -7610;
  12076. }
  12077. if (ret == 0) {
  12078. if(wc_SetAltNamesBuffer(NULL, der, derSz) != BAD_FUNC_ARG)
  12079. ret = -7611;
  12080. }
  12081. if (ret == 0) {
  12082. if(wc_SetDatesBuffer(&cert, der, derSz) != 0)
  12083. ret = -7612;
  12084. }
  12085. if (ret == 0) {
  12086. if(wc_SetDatesBuffer(NULL, der, derSz) != BAD_FUNC_ARG)
  12087. ret = -7613;
  12088. }
  12089. #endif
  12090. if (ret == 0) {
  12091. if(wc_SetAuthKeyIdFromCert(&cert, der, derSz) != 0)
  12092. ret = -7614;
  12093. }
  12094. if (ret == 0) {
  12095. if(wc_SetAuthKeyIdFromCert(NULL, der, derSz) != BAD_FUNC_ARG)
  12096. ret = -7615;
  12097. }
  12098. wc_SetCert_Free(&cert);
  12099. if (ret == 0) {
  12100. if(cert.decodedCert != NULL)
  12101. ret = -7616;
  12102. }
  12103. XFREE(der, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  12104. return ret;
  12105. }
  12106. #endif /* defined(WOLFSSL_CERT_GEN_CACHE) && defined(WOLFSSL_TEST_CERT) &&
  12107. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN) */
  12108. #define RSA_TEST_BYTES 512 /* up to 4096-bit key */
  12109. #if !defined(NO_ASN) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
  12110. !defined(WOLFSSL_RSA_VERIFY_ONLY)
  12111. static int rsa_flatten_test(RsaKey* key)
  12112. {
  12113. int ret;
  12114. byte e[RSA_TEST_BYTES];
  12115. byte n[RSA_TEST_BYTES];
  12116. word32 eSz = sizeof(e);
  12117. word32 nSz = sizeof(n);
  12118. /* Parameter Validation testing. */
  12119. ret = wc_RsaFlattenPublicKey(NULL, e, &eSz, n, &nSz);
  12120. #ifdef HAVE_USER_RSA
  12121. /* Implementation using IPP Libraries returns:
  12122. * -101 = USER_CRYPTO_ERROR
  12123. */
  12124. if (ret == 0)
  12125. #else
  12126. if (ret != BAD_FUNC_ARG)
  12127. #endif
  12128. return -7620;
  12129. ret = wc_RsaFlattenPublicKey(key, NULL, &eSz, n, &nSz);
  12130. #ifdef HAVE_USER_RSA
  12131. /* Implementation using IPP Libraries returns:
  12132. * -101 = USER_CRYPTO_ERROR
  12133. */
  12134. if (ret == 0)
  12135. #else
  12136. if (ret != BAD_FUNC_ARG)
  12137. #endif
  12138. return -7621;
  12139. ret = wc_RsaFlattenPublicKey(key, e, NULL, n, &nSz);
  12140. #ifdef HAVE_USER_RSA
  12141. /* Implementation using IPP Libraries returns:
  12142. * -101 = USER_CRYPTO_ERROR
  12143. */
  12144. if (ret == 0)
  12145. #else
  12146. if (ret != BAD_FUNC_ARG)
  12147. #endif
  12148. return -7622;
  12149. ret = wc_RsaFlattenPublicKey(key, e, &eSz, NULL, &nSz);
  12150. #ifdef HAVE_USER_RSA
  12151. /* Implementation using IPP Libraries returns:
  12152. * -101 = USER_CRYPTO_ERROR
  12153. */
  12154. if (ret == 0)
  12155. #else
  12156. if (ret != BAD_FUNC_ARG)
  12157. #endif
  12158. return -7623;
  12159. ret = wc_RsaFlattenPublicKey(key, e, &eSz, n, NULL);
  12160. #ifdef HAVE_USER_RSA
  12161. /* Implementation using IPP Libraries returns:
  12162. * -101 = USER_CRYPTO_ERROR
  12163. */
  12164. if (ret == 0)
  12165. #else
  12166. if (ret != BAD_FUNC_ARG)
  12167. #endif
  12168. return -7624;
  12169. ret = wc_RsaFlattenPublicKey(key, e, &eSz, n, &nSz);
  12170. if (ret != 0)
  12171. return -7625;
  12172. eSz = 0;
  12173. ret = wc_RsaFlattenPublicKey(key, e, &eSz, n, &nSz);
  12174. #ifdef HAVE_USER_RSA
  12175. /* Implementation using IPP Libraries returns:
  12176. * -101 = USER_CRYPTO_ERROR
  12177. */
  12178. if (ret == 0)
  12179. #elif defined(HAVE_FIPS) && \
  12180. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 2))
  12181. if (ret != 0)
  12182. #else
  12183. if (ret != RSA_BUFFER_E)
  12184. #endif
  12185. return -7626;
  12186. eSz = sizeof(e);
  12187. nSz = 0;
  12188. ret = wc_RsaFlattenPublicKey(key, e, &eSz, n, &nSz);
  12189. #ifdef HAVE_USER_RSA
  12190. /* Implementation using IPP Libraries returns:
  12191. * -101 = USER_CRYPTO_ERROR
  12192. */
  12193. if (ret == 0)
  12194. #else
  12195. if (ret != RSA_BUFFER_E)
  12196. #endif
  12197. return -7627;
  12198. return 0;
  12199. }
  12200. #endif /* NO_ASN */
  12201. #if !defined(HAVE_FIPS) && !defined(HAVE_USER_RSA) && !defined(NO_ASN) \
  12202. && !defined(WOLFSSL_RSA_VERIFY_ONLY)
  12203. static int rsa_export_key_test(RsaKey* key)
  12204. {
  12205. int ret;
  12206. byte e[3];
  12207. word32 eSz = sizeof(e);
  12208. byte n[RSA_TEST_BYTES];
  12209. word32 nSz = sizeof(n);
  12210. byte d[RSA_TEST_BYTES];
  12211. word32 dSz = sizeof(d);
  12212. byte p[RSA_TEST_BYTES/2];
  12213. word32 pSz = sizeof(p);
  12214. byte q[RSA_TEST_BYTES/2];
  12215. word32 qSz = sizeof(q);
  12216. word32 zero = 0;
  12217. ret = wc_RsaExportKey(NULL, e, &eSz, n, &nSz, d, &dSz, p, &pSz, q, &qSz);
  12218. if (ret != BAD_FUNC_ARG)
  12219. return -7630;
  12220. ret = wc_RsaExportKey(key, NULL, &eSz, n, &nSz, d, &dSz, p, &pSz, q, &qSz);
  12221. if (ret != BAD_FUNC_ARG)
  12222. return -7631;
  12223. ret = wc_RsaExportKey(key, e, NULL, n, &nSz, d, &dSz, p, &pSz, q, &qSz);
  12224. if (ret != BAD_FUNC_ARG)
  12225. return -7632;
  12226. ret = wc_RsaExportKey(key, e, &eSz, NULL, &nSz, d, &dSz, p, &pSz, q, &qSz);
  12227. if (ret != BAD_FUNC_ARG)
  12228. return -7633;
  12229. ret = wc_RsaExportKey(key, e, &eSz, n, NULL, d, &dSz, p, &pSz, q, &qSz);
  12230. if (ret != BAD_FUNC_ARG)
  12231. return -7634;
  12232. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, NULL, &dSz, p, &pSz, q, &qSz);
  12233. if (ret != BAD_FUNC_ARG)
  12234. return -7635;
  12235. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, NULL, p, &pSz, q, &qSz);
  12236. if (ret != BAD_FUNC_ARG)
  12237. return -7636;
  12238. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, NULL, &pSz, q, &qSz);
  12239. if (ret != BAD_FUNC_ARG)
  12240. return -7637;
  12241. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, NULL, q, &qSz);
  12242. if (ret != BAD_FUNC_ARG)
  12243. return -7638;
  12244. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, &pSz, NULL, &qSz);
  12245. if (ret != BAD_FUNC_ARG)
  12246. return -7639;
  12247. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, &pSz, q, NULL);
  12248. if (ret != BAD_FUNC_ARG)
  12249. return -7640;
  12250. ret = wc_RsaExportKey(key, e, &zero, n, &nSz, d, &dSz, p, &pSz, q, &qSz);
  12251. if (ret != RSA_BUFFER_E)
  12252. return -7641;
  12253. ret = wc_RsaExportKey(key, e, &eSz, n, &zero, d, &dSz, p, &pSz, q, &qSz);
  12254. if (ret != RSA_BUFFER_E)
  12255. return -7642;
  12256. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  12257. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &zero, p, &pSz, q, &qSz);
  12258. if (ret != RSA_BUFFER_E)
  12259. return -7643;
  12260. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, &zero, q, &qSz);
  12261. if (ret != RSA_BUFFER_E)
  12262. return -7644;
  12263. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, &pSz, q, &zero);
  12264. if (ret != RSA_BUFFER_E)
  12265. return -7645;
  12266. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  12267. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, &pSz, q, &qSz);
  12268. if (ret != 0)
  12269. return -7646;
  12270. return 0;
  12271. }
  12272. #endif /* !HAVE_FIPS && !USER_RSA && !NO_ASN */
  12273. #ifndef NO_SIG_WRAPPER
  12274. static int rsa_sig_test(RsaKey* key, word32 keyLen, int modLen, WC_RNG* rng)
  12275. {
  12276. int ret;
  12277. word32 sigSz;
  12278. WOLFSSL_SMALL_STACK_STATIC const byte in[] = TEST_STRING;
  12279. WOLFSSL_SMALL_STACK_STATIC const byte hash[] = {
  12280. 0xf2, 0x02, 0x95, 0x65, 0xcb, 0xf6, 0x2a, 0x59,
  12281. 0x39, 0x2c, 0x05, 0xff, 0x0e, 0x29, 0xaf, 0xfe,
  12282. 0x47, 0x33, 0x8c, 0x99, 0x8d, 0x58, 0x64, 0x83,
  12283. 0xa6, 0x58, 0x0a, 0x33, 0x0b, 0x84, 0x5f, 0x5f
  12284. };
  12285. WOLFSSL_SMALL_STACK_STATIC const byte hashEnc[] = {
  12286. 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
  12287. 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05,
  12288. 0x00, 0x04, 0x20,
  12289. 0xf2, 0x02, 0x95, 0x65, 0xcb, 0xf6, 0x2a, 0x59,
  12290. 0x39, 0x2c, 0x05, 0xff, 0x0e, 0x29, 0xaf, 0xfe,
  12291. 0x47, 0x33, 0x8c, 0x99, 0x8d, 0x58, 0x64, 0x83,
  12292. 0xa6, 0x58, 0x0a, 0x33, 0x0b, 0x84, 0x5f, 0x5f
  12293. };
  12294. word32 inLen = (word32)XSTRLEN((char*)in);
  12295. byte out[RSA_TEST_BYTES];
  12296. /* Parameter Validation testing. */
  12297. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_NONE, key, keyLen);
  12298. if (ret != BAD_FUNC_ARG)
  12299. return -7650;
  12300. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_RSA, key, 0);
  12301. if (ret != BAD_FUNC_ARG)
  12302. return -7651;
  12303. sigSz = (word32)modLen;
  12304. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, NULL,
  12305. inLen, out, &sigSz, key, keyLen, rng);
  12306. if (ret != BAD_FUNC_ARG)
  12307. return -7652;
  12308. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  12309. 0, out, &sigSz, key, keyLen, rng);
  12310. if (ret != BAD_FUNC_ARG)
  12311. return -7653;
  12312. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  12313. inLen, NULL, &sigSz, key, keyLen, rng);
  12314. if (ret != BAD_FUNC_ARG)
  12315. return -7654;
  12316. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  12317. inLen, out, NULL, key, keyLen, rng);
  12318. if (ret != BAD_FUNC_ARG)
  12319. return -7655;
  12320. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  12321. inLen, out, &sigSz, NULL, keyLen, rng);
  12322. if (ret != BAD_FUNC_ARG)
  12323. return -7656;
  12324. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  12325. inLen, out, &sigSz, key, 0, rng);
  12326. if (ret != BAD_FUNC_ARG)
  12327. return -7657;
  12328. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  12329. inLen, out, &sigSz, key, keyLen, NULL);
  12330. #ifdef HAVE_USER_RSA
  12331. /* Implementation using IPP Libraries returns:
  12332. * -101 = USER_CRYPTO_ERROR
  12333. */
  12334. if (ret == 0)
  12335. #elif defined(WOLFSSL_AFALG_XILINX_RSA) || defined(WOLFSSL_XILINX_CRYPT)
  12336. /* blinding / rng handled with hardware acceleration */
  12337. if (ret != 0)
  12338. #elif defined(WOLFSSL_ASYNC_CRYPT) || defined(WOLF_CRYPTO_CB)
  12339. /* async may not require RNG */
  12340. #if defined(WOLF_CRYPTO_CB_ONLY_RSA)
  12341. if (ret != NO_VALID_DEVID)
  12342. #else
  12343. if (ret != 0 && ret != MISSING_RNG_E)
  12344. #endif
  12345. #elif defined(HAVE_FIPS) || !defined(WC_RSA_BLINDING)
  12346. /* FIPS140 implementation does not do blinding */
  12347. if (ret != 0)
  12348. #elif defined(WOLFSSL_RSA_PUBLIC_ONLY) || defined(WOLFSSL_RSA_VERIFY_ONLY)
  12349. if (ret != SIG_TYPE_E)
  12350. #elif defined(WOLFSSL_CRYPTOCELL) || defined(WOLFSSL_SE050)
  12351. /* RNG is handled by hardware */
  12352. if (ret != 0)
  12353. #else
  12354. if (ret != MISSING_RNG_E)
  12355. #endif
  12356. return -7658;
  12357. sigSz = 0;
  12358. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  12359. inLen, out, &sigSz, key, keyLen, rng);
  12360. if (ret != BAD_FUNC_ARG)
  12361. return -7659;
  12362. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, NULL,
  12363. inLen, out, (word32)modLen, key, keyLen);
  12364. if (ret != BAD_FUNC_ARG)
  12365. return -7660;
  12366. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  12367. 0, out, (word32)modLen, key, keyLen);
  12368. if (ret != BAD_FUNC_ARG)
  12369. return -7661;
  12370. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  12371. inLen, NULL, (word32)modLen, key, keyLen);
  12372. if (ret != BAD_FUNC_ARG)
  12373. return -7662;
  12374. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  12375. inLen, out, 0, key, keyLen);
  12376. if (ret != BAD_FUNC_ARG)
  12377. return -7663;
  12378. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  12379. inLen, out, (word32)modLen, NULL, keyLen);
  12380. if (ret != BAD_FUNC_ARG)
  12381. return -7664;
  12382. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  12383. inLen, out, (word32)modLen, key, 0);
  12384. if (ret != BAD_FUNC_ARG)
  12385. return -7665;
  12386. #ifndef HAVE_ECC
  12387. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_ECC, key, keyLen);
  12388. if (ret != SIG_TYPE_E)
  12389. return -7666;
  12390. #endif
  12391. #if defined(WOLF_CRYPTO_CB_ONLY_RSA)
  12392. return 0;
  12393. #endif
  12394. /* Use APIs. */
  12395. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_RSA, key, keyLen);
  12396. if (ret != modLen)
  12397. return -7667;
  12398. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_RSA_W_ENC, key, keyLen);
  12399. if (ret != modLen)
  12400. return -7668;
  12401. sigSz = (word32)ret;
  12402. #if !defined(WOLFSSL_RSA_PUBLIC_ONLY) && !defined(WOLFSSL_RSA_VERIFY_ONLY)
  12403. XMEMSET(out, 0, sizeof(out));
  12404. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  12405. inLen, out, &sigSz, key, keyLen, rng);
  12406. if (ret != 0)
  12407. return -7669;
  12408. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  12409. inLen, out, (word32)modLen, key, keyLen);
  12410. if (ret != 0)
  12411. return -7670;
  12412. sigSz = (word32)sizeof(out);
  12413. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA_W_ENC,
  12414. in, inLen, out, &sigSz, key, keyLen, rng);
  12415. if (ret != 0)
  12416. return -7671;
  12417. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA_W_ENC,
  12418. in, inLen, out, (word32)modLen, key, keyLen);
  12419. if (ret != 0)
  12420. return -7672;
  12421. /* Wrong signature type. */
  12422. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  12423. inLen, out, (word32)modLen, key, keyLen);
  12424. if (ret == 0)
  12425. return -7673;
  12426. /* check hash functions */
  12427. sigSz = (word32)sizeof(out);
  12428. ret = wc_SignatureGenerateHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA,
  12429. hash, (int)sizeof(hash), out, &sigSz, key, keyLen, rng);
  12430. if (ret != 0)
  12431. return -7674;
  12432. ret = wc_SignatureVerifyHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA,
  12433. hash, (int)sizeof(hash), out, (word32)modLen, key, keyLen);
  12434. if (ret != 0)
  12435. return -7675;
  12436. sigSz = (word32)sizeof(out);
  12437. ret = wc_SignatureGenerateHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA_W_ENC,
  12438. hashEnc, (int)sizeof(hashEnc), out, &sigSz, key, keyLen, rng);
  12439. if (ret != 0)
  12440. return -7676;
  12441. ret = wc_SignatureVerifyHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA_W_ENC,
  12442. hashEnc, (int)sizeof(hashEnc), out, (word32)modLen, key, keyLen);
  12443. if (ret != 0)
  12444. return -7677;
  12445. #else
  12446. (void)hash;
  12447. (void)hashEnc;
  12448. #endif /* !WOLFSSL_RSA_PUBLIC_ONLY && !WOLFSSL_RSA_VERIFY_ONLY */
  12449. return 0;
  12450. }
  12451. #endif /* !NO_SIG_WRAPPER */
  12452. #ifdef WC_RSA_NONBLOCK
  12453. static int rsa_nb_test(RsaKey* key, const byte* in, word32 inLen, byte* out,
  12454. word32 outSz, byte* plain, word32 plainSz, WC_RNG* rng)
  12455. {
  12456. int ret = 0, count;
  12457. int signSz = 0;
  12458. RsaNb nb;
  12459. byte* inlinePlain = NULL;
  12460. /* Enable non-blocking RSA mode - provide context */
  12461. ret = wc_RsaSetNonBlock(key, &nb);
  12462. if (ret != 0)
  12463. return ret;
  12464. #ifdef WC_RSA_NONBLOCK_TIME
  12465. /* Enable time based RSA blocking. 8 microseconds max (3.1GHz) */
  12466. ret = wc_RsaSetNonBlockTime(key, 8, 3100);
  12467. if (ret != 0)
  12468. return ret;
  12469. #endif
  12470. count = 0;
  12471. do {
  12472. ret = wc_RsaSSL_Sign(in, inLen, out, outSz, key, rng);
  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 sign: %d times\n", count);
  12483. #endif
  12484. signSz = ret;
  12485. /* Test non-blocking verify */
  12486. XMEMSET(plain, 0, plainSz);
  12487. count = 0;
  12488. do {
  12489. ret = wc_RsaSSL_Verify(out, (word32)signSz, plain, plainSz, key);
  12490. count++; /* track number of would blocks */
  12491. if (ret == FP_WOULDBLOCK) {
  12492. /* do "other" work here */
  12493. }
  12494. } while (ret == FP_WOULDBLOCK);
  12495. if (ret < 0) {
  12496. return ret;
  12497. }
  12498. #ifdef DEBUG_WOLFSSL
  12499. printf("RSA non-block verify: %d times\n", count);
  12500. #endif
  12501. if (signSz == ret && XMEMCMP(plain, in, (size_t)ret)) {
  12502. return SIG_VERIFY_E;
  12503. }
  12504. /* Test inline non-blocking verify */
  12505. count = 0;
  12506. do {
  12507. ret = wc_RsaSSL_VerifyInline(out, (word32)signSz, &inlinePlain, key);
  12508. count++; /* track number of would blocks */
  12509. if (ret == FP_WOULDBLOCK) {
  12510. /* do "other" work here */
  12511. }
  12512. } while (ret == FP_WOULDBLOCK);
  12513. if (ret < 0) {
  12514. return ret;
  12515. }
  12516. #ifdef DEBUG_WOLFSSL
  12517. printf("RSA non-block inline verify: %d times\n", count);
  12518. #endif
  12519. if (signSz == ret && XMEMCMP(inlinePlain, in, (size_t)ret)) {
  12520. return SIG_VERIFY_E;
  12521. }
  12522. /* Disabling non-block RSA mode */
  12523. ret = wc_RsaSetNonBlock(key, NULL);
  12524. (void)count;
  12525. return 0;
  12526. }
  12527. #endif
  12528. #if !defined(HAVE_USER_RSA) && !defined(NO_ASN)
  12529. static int rsa_decode_test(RsaKey* keyPub)
  12530. {
  12531. int ret;
  12532. word32 inSz;
  12533. word32 inOutIdx;
  12534. WOLFSSL_SMALL_STACK_STATIC const byte n[2] = { 0x00, 0x23 };
  12535. WOLFSSL_SMALL_STACK_STATIC const byte e[2] = { 0x00, 0x03 };
  12536. WOLFSSL_SMALL_STACK_STATIC const byte good[] = { 0x30, 0x06, 0x02, 0x01, 0x23, 0x02, 0x1,
  12537. 0x03 };
  12538. WOLFSSL_SMALL_STACK_STATIC const byte goodAlgId[] = {
  12539. 0x30, 0x18, 0x30, 0x16,
  12540. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
  12541. 0x03, 0x09, 0x00, 0x30, 0x06, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  12542. WOLFSSL_SMALL_STACK_STATIC const byte goodAlgIdNull[] = {
  12543. 0x30, 0x1a, 0x30, 0x18,
  12544. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
  12545. 0x05, 0x00, 0x03, 0x09, 0x00, 0x30, 0x06, 0x02, 0x01, 0x23,
  12546. 0x02, 0x1, 0x03 };
  12547. WOLFSSL_SMALL_STACK_STATIC const byte badAlgIdNull[] = {
  12548. 0x30, 0x1b, 0x30, 0x19,
  12549. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
  12550. 0x05, 0x01, 0x00, 0x03, 0x09, 0x00, 0x30, 0x06, 0x02, 0x01, 0x23,
  12551. 0x02, 0x1, 0x03 };
  12552. WOLFSSL_SMALL_STACK_STATIC const byte badNotBitString[] = {
  12553. 0x30, 0x18, 0x30, 0x16,
  12554. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
  12555. 0x04, 0x09, 0x00, 0x30, 0x06, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  12556. WOLFSSL_SMALL_STACK_STATIC const byte badBitStringLen[] = {
  12557. 0x30, 0x18, 0x30, 0x16,
  12558. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
  12559. 0x03, 0x0a, 0x00, 0x30, 0x06, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  12560. WOLFSSL_SMALL_STACK_STATIC const byte badNoSeq[] = {
  12561. 0x30, 0x16, 0x30, 0x14,
  12562. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
  12563. 0x07, 0x00, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  12564. WOLFSSL_SMALL_STACK_STATIC const byte badNoObj[] = {
  12565. 0x30, 0x0f, 0x30, 0x0d, 0x05, 0x00, 0x03, 0x09, 0x00, 0x30, 0x06,
  12566. 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  12567. WOLFSSL_SMALL_STACK_STATIC const byte badIntN[] = {
  12568. 0x30, 0x06, 0x02, 0x05, 0x23, 0x02, 0x1, 0x03 };
  12569. WOLFSSL_SMALL_STACK_STATIC const byte badNotIntE[] = {
  12570. 0x30, 0x06, 0x02, 0x01, 0x23, 0x04, 0x1, 0x03 };
  12571. WOLFSSL_SMALL_STACK_STATIC const byte badLength[] = {
  12572. 0x30, 0x04, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  12573. WOLFSSL_SMALL_STACK_STATIC const byte badBitStrNoZero[] = {
  12574. 0x30, 0x17, 0x30, 0x15,
  12575. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
  12576. 0x03, 0x08, 0x30, 0x06, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  12577. ret = wc_InitRsaKey(keyPub, NULL);
  12578. if (ret != 0)
  12579. return -7690;
  12580. /* Parameter Validation testing. */
  12581. ret = wc_RsaPublicKeyDecodeRaw(NULL, sizeof(n), e, sizeof(e), keyPub);
  12582. if (ret != BAD_FUNC_ARG) {
  12583. ret = -7691;
  12584. goto done;
  12585. }
  12586. ret = wc_RsaPublicKeyDecodeRaw(n, sizeof(n), NULL, sizeof(e), keyPub);
  12587. if (ret != BAD_FUNC_ARG) {
  12588. ret = -7692;
  12589. goto done;
  12590. }
  12591. ret = wc_RsaPublicKeyDecodeRaw(n, sizeof(n), e, sizeof(e), NULL);
  12592. if (ret != BAD_FUNC_ARG) {
  12593. ret = -7693;
  12594. goto done;
  12595. }
  12596. ret = wc_RsaPublicKeyDecodeRaw(n, (word32)-1, e, sizeof(e), keyPub);
  12597. #if !defined(WOLFSSL_SP_MATH) & !defined(WOLFSSL_SP_MATH_ALL)
  12598. if (ret != 0) {
  12599. #else
  12600. if (ret != ASN_GETINT_E) {
  12601. #endif
  12602. ret = -7694;
  12603. goto done;
  12604. }
  12605. wc_FreeRsaKey(keyPub);
  12606. ret = wc_InitRsaKey(keyPub, NULL);
  12607. if (ret != 0)
  12608. return -7695;
  12609. ret = wc_RsaPublicKeyDecodeRaw(n, sizeof(n), e, (word32)-1, keyPub);
  12610. #if !defined(WOLFSSL_SP_MATH) & !defined(WOLFSSL_SP_MATH_ALL)
  12611. if (ret != 0) {
  12612. #else
  12613. if (ret != ASN_GETINT_E) {
  12614. #endif
  12615. ret = -7696;
  12616. goto done;
  12617. }
  12618. wc_FreeRsaKey(keyPub);
  12619. ret = wc_InitRsaKey(keyPub, NULL);
  12620. if (ret != 0)
  12621. return -7697;
  12622. /* Use API. */
  12623. ret = wc_RsaPublicKeyDecodeRaw(n, sizeof(n), e, sizeof(e), keyPub);
  12624. if (ret != 0) {
  12625. ret = -7698;
  12626. goto done;
  12627. }
  12628. wc_FreeRsaKey(keyPub);
  12629. ret = wc_InitRsaKey(keyPub, NULL);
  12630. if (ret != 0)
  12631. return -7699;
  12632. /* Parameter Validation testing. */
  12633. inSz = sizeof(good);
  12634. ret = wc_RsaPublicKeyDecode(NULL, &inOutIdx, keyPub, inSz);
  12635. if (ret != BAD_FUNC_ARG) {
  12636. ret = -7700;
  12637. goto done;
  12638. }
  12639. ret = wc_RsaPublicKeyDecode(good, NULL, keyPub, inSz);
  12640. if (ret != BAD_FUNC_ARG) {
  12641. ret = -7701;
  12642. goto done;
  12643. }
  12644. ret = wc_RsaPublicKeyDecode(good, &inOutIdx, NULL, inSz);
  12645. if (ret != BAD_FUNC_ARG) {
  12646. ret = -7702;
  12647. goto done;
  12648. }
  12649. /* Use good data and offset to bad data. */
  12650. inOutIdx = 2;
  12651. inSz = sizeof(good) - inOutIdx;
  12652. ret = wc_RsaPublicKeyDecode(good, &inOutIdx, keyPub, inSz);
  12653. if (ret != ASN_PARSE_E) {
  12654. ret = -7703;
  12655. goto done;
  12656. }
  12657. inOutIdx = 2;
  12658. inSz = sizeof(goodAlgId) - inOutIdx;
  12659. ret = wc_RsaPublicKeyDecode(goodAlgId, &inOutIdx, keyPub, inSz);
  12660. if (ret != ASN_PARSE_E) {
  12661. ret = -7704;
  12662. goto done;
  12663. }
  12664. inOutIdx = 2;
  12665. inSz = sizeof(goodAlgId);
  12666. ret = wc_RsaPublicKeyDecode(goodAlgId, &inOutIdx, keyPub, inSz);
  12667. #ifndef WOLFSSL_NO_DECODE_EXTRA
  12668. if (ret != ASN_PARSE_E)
  12669. #else
  12670. if (ret != ASN_RSA_KEY_E)
  12671. #endif
  12672. {
  12673. ret = -7705;
  12674. goto done;
  12675. }
  12676. /* Try different bad data. */
  12677. inSz = sizeof(badAlgIdNull);
  12678. inOutIdx = 0;
  12679. ret = wc_RsaPublicKeyDecode(badAlgIdNull, &inOutIdx, keyPub, inSz);
  12680. if (ret != ASN_EXPECT_0_E) {
  12681. ret = -7706;
  12682. goto done;
  12683. }
  12684. inSz = sizeof(badNotBitString);
  12685. inOutIdx = 0;
  12686. ret = wc_RsaPublicKeyDecode(badNotBitString, &inOutIdx, keyPub, inSz);
  12687. if (ret != ASN_BITSTR_E) {
  12688. ret = -7707;
  12689. goto done;
  12690. }
  12691. inSz = sizeof(badBitStringLen);
  12692. inOutIdx = 0;
  12693. ret = wc_RsaPublicKeyDecode(badBitStringLen, &inOutIdx, keyPub, inSz);
  12694. if (ret != ASN_PARSE_E) {
  12695. ret = -7708;
  12696. goto done;
  12697. }
  12698. inSz = sizeof(badNoSeq);
  12699. inOutIdx = 0;
  12700. ret = wc_RsaPublicKeyDecode(badNoSeq, &inOutIdx, keyPub, inSz);
  12701. if (ret != ASN_PARSE_E) {
  12702. ret = -7709;
  12703. goto done;
  12704. }
  12705. inSz = sizeof(badNoObj);
  12706. inOutIdx = 0;
  12707. ret = wc_RsaPublicKeyDecode(badNoObj, &inOutIdx, keyPub, inSz);
  12708. if (ret != ASN_PARSE_E && ret != ASN_OBJECT_ID_E) {
  12709. ret = -7710;
  12710. goto done;
  12711. }
  12712. inSz = sizeof(badIntN);
  12713. inOutIdx = 0;
  12714. ret = wc_RsaPublicKeyDecode(badIntN, &inOutIdx, keyPub, inSz);
  12715. if (ret != ASN_RSA_KEY_E && ret != ASN_PARSE_E) {
  12716. ret = -7711;
  12717. goto done;
  12718. }
  12719. inSz = sizeof(badNotIntE);
  12720. inOutIdx = 0;
  12721. ret = wc_RsaPublicKeyDecode(badNotIntE, &inOutIdx, keyPub, inSz);
  12722. if (ret != ASN_RSA_KEY_E && ret != ASN_PARSE_E) {
  12723. ret = -7712;
  12724. goto done;
  12725. }
  12726. /* TODO: Shouldn't pass as the sequence length is too small. */
  12727. inSz = sizeof(badLength);
  12728. inOutIdx = 0;
  12729. ret = wc_RsaPublicKeyDecode(badLength, &inOutIdx, keyPub, inSz);
  12730. #ifndef WOLFSSL_ASN_TEMPLATE
  12731. if (ret != 0)
  12732. #else
  12733. if (ret != ASN_PARSE_E)
  12734. #endif
  12735. {
  12736. ret = -7713;
  12737. goto done;
  12738. }
  12739. /* TODO: Shouldn't ignore object id's data. */
  12740. wc_FreeRsaKey(keyPub);
  12741. ret = wc_InitRsaKey(keyPub, NULL);
  12742. if (ret != 0)
  12743. return -7714;
  12744. inSz = sizeof(badBitStrNoZero);
  12745. inOutIdx = 0;
  12746. ret = wc_RsaPublicKeyDecode(badBitStrNoZero, &inOutIdx, keyPub, inSz);
  12747. if (ret != ASN_EXPECT_0_E && ret != ASN_PARSE_E) {
  12748. ret = -7715;
  12749. goto done;
  12750. }
  12751. wc_FreeRsaKey(keyPub);
  12752. ret = wc_InitRsaKey(keyPub, NULL);
  12753. if (ret != 0)
  12754. return -7716;
  12755. /* Valid data cases. */
  12756. inSz = sizeof(good);
  12757. inOutIdx = 0;
  12758. ret = wc_RsaPublicKeyDecode(good, &inOutIdx, keyPub, inSz);
  12759. if (ret != 0) {
  12760. ret = -7717;
  12761. goto done;
  12762. }
  12763. if (inOutIdx != inSz) {
  12764. ret = -7718;
  12765. goto done;
  12766. }
  12767. wc_FreeRsaKey(keyPub);
  12768. ret = wc_InitRsaKey(keyPub, NULL);
  12769. if (ret != 0)
  12770. return -7719;
  12771. inSz = sizeof(goodAlgId);
  12772. inOutIdx = 0;
  12773. ret = wc_RsaPublicKeyDecode(goodAlgId, &inOutIdx, keyPub, inSz);
  12774. if (ret != 0) {
  12775. ret = -7720;
  12776. goto done;
  12777. }
  12778. if (inOutIdx != inSz) {
  12779. ret = -7721;
  12780. goto done;
  12781. }
  12782. wc_FreeRsaKey(keyPub);
  12783. ret = wc_InitRsaKey(keyPub, NULL);
  12784. if (ret != 0)
  12785. return -7722;
  12786. inSz = sizeof(goodAlgIdNull);
  12787. inOutIdx = 0;
  12788. ret = wc_RsaPublicKeyDecode(goodAlgIdNull, &inOutIdx, keyPub, inSz);
  12789. if (ret != 0) {
  12790. ret = -7723;
  12791. goto done;
  12792. }
  12793. if (inOutIdx != inSz) {
  12794. ret = -7724;
  12795. goto done;
  12796. }
  12797. done:
  12798. wc_FreeRsaKey(keyPub);
  12799. return ret;
  12800. }
  12801. #endif
  12802. #if defined(WC_RSA_PSS) && !defined(HAVE_FIPS_VERSION) /* not supported with FIPSv1 */
  12803. /* Need to create known good signatures to test with this. */
  12804. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
  12805. !defined(WOLF_CRYPTO_CB_ONLY_RSA)
  12806. static int rsa_pss_test(WC_RNG* rng, RsaKey* key)
  12807. {
  12808. byte digest[WC_MAX_DIGEST_SIZE];
  12809. int ret = 0;
  12810. const char inStr[] = TEST_STRING;
  12811. word32 inLen = (word32)TEST_STRING_SZ;
  12812. word32 outSz;
  12813. word32 plainSz;
  12814. word32 digestSz;
  12815. int i, j;
  12816. #ifdef RSA_PSS_TEST_WRONG_PARAMS
  12817. int k, l;
  12818. #endif
  12819. #ifndef WOLFSSL_SE050
  12820. int len;
  12821. #endif
  12822. byte* plain;
  12823. int mgf[] = {
  12824. #ifndef NO_SHA
  12825. WC_MGF1SHA1,
  12826. #endif
  12827. #ifdef WOLFSSL_SHA224
  12828. WC_MGF1SHA224,
  12829. #endif
  12830. WC_MGF1SHA256,
  12831. #ifdef WOLFSSL_SHA384
  12832. WC_MGF1SHA384,
  12833. #endif
  12834. #ifdef WOLFSSL_SHA512
  12835. WC_MGF1SHA512
  12836. #endif
  12837. };
  12838. enum wc_HashType hash[] = {
  12839. #ifndef NO_SHA
  12840. WC_HASH_TYPE_SHA,
  12841. #endif
  12842. #ifdef WOLFSSL_SHA224
  12843. WC_HASH_TYPE_SHA224,
  12844. #endif
  12845. WC_HASH_TYPE_SHA256,
  12846. #ifdef WOLFSSL_SHA384
  12847. WC_HASH_TYPE_SHA384,
  12848. #endif
  12849. #ifdef WOLFSSL_SHA512
  12850. WC_HASH_TYPE_SHA512,
  12851. #endif
  12852. };
  12853. WC_DECLARE_VAR(in, byte, RSA_TEST_BYTES, HEAP_HINT);
  12854. WC_DECLARE_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
  12855. WC_DECLARE_VAR(sig, byte, RSA_TEST_BYTES, HEAP_HINT);
  12856. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  12857. if (in == NULL || out == NULL || sig == NULL)
  12858. ERROR_OUT(MEMORY_E, exit_rsa_pss);
  12859. #endif
  12860. XMEMCPY(in, inStr, inLen);
  12861. /* Test all combinations of hash and MGF. */
  12862. for (j = 0; j < (int)(sizeof(hash)/sizeof(*hash)); j++) {
  12863. /* Calculate hash of message. */
  12864. ret = wc_Hash(hash[j], in, inLen, digest, sizeof(digest));
  12865. if (ret != 0)
  12866. ERROR_OUT(-7730, exit_rsa_pss);
  12867. digestSz = wc_HashGetDigestSize(hash[j]);
  12868. #ifdef WOLFSSL_SE050
  12869. /* SE050 only supports MGF matched to same hash type */
  12870. i = j;
  12871. #else
  12872. for (i = 0; i < (int)(sizeof(mgf)/sizeof(*mgf)); i++) {
  12873. #endif
  12874. outSz = RSA_TEST_BYTES;
  12875. do {
  12876. #if defined(WOLFSSL_ASYNC_CRYPT)
  12877. ret = wc_AsyncWait(ret, &key->asyncDev,
  12878. WC_ASYNC_FLAG_CALL_AGAIN);
  12879. #endif
  12880. if (ret >= 0) {
  12881. ret = wc_RsaPSS_Sign_ex(digest, digestSz, out, outSz,
  12882. hash[j], mgf[i], -1, key, rng);
  12883. }
  12884. } while (ret == WC_PENDING_E);
  12885. if (ret <= 0)
  12886. ERROR_OUT(-7731, exit_rsa_pss);
  12887. outSz = ret;
  12888. XMEMCPY(sig, out, outSz);
  12889. plain = NULL;
  12890. TEST_SLEEP();
  12891. do {
  12892. #if defined(WOLFSSL_ASYNC_CRYPT)
  12893. ret = wc_AsyncWait(ret, &key->asyncDev,
  12894. WC_ASYNC_FLAG_CALL_AGAIN);
  12895. #endif
  12896. if (ret >= 0) {
  12897. ret = wc_RsaPSS_VerifyInline_ex(sig, outSz, &plain, hash[j],
  12898. mgf[i], -1, key);
  12899. }
  12900. } while (ret == WC_PENDING_E);
  12901. if (ret <= 0)
  12902. ERROR_OUT(-7732, exit_rsa_pss);
  12903. plainSz = ret;
  12904. TEST_SLEEP();
  12905. #if defined(HAVE_SELFTEST) && \
  12906. (!defined(HAVE_SELFTEST_VERSION) || (HAVE_SELFTEST_VERSION < 2))
  12907. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  12908. hash[j], -1);
  12909. #elif defined(HAVE_SELFTEST) && (HAVE_SELFTEST_VERSION == 2)
  12910. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  12911. hash[j], -1, 0);
  12912. #else
  12913. ret = wc_RsaPSS_CheckPadding_ex2(digest, digestSz, plain, plainSz,
  12914. hash[j], -1, wc_RsaEncryptSize(key)*8, HEAP_HINT);
  12915. #endif
  12916. if (ret != 0)
  12917. ERROR_OUT(-7733, exit_rsa_pss);
  12918. #ifdef RSA_PSS_TEST_WRONG_PARAMS
  12919. for (k = 0; k < (int)(sizeof(mgf)/sizeof(*mgf)); k++) {
  12920. for (l = 0; l < (int)(sizeof(hash)/sizeof(*hash)); l++) {
  12921. if (i == k && j == l)
  12922. continue;
  12923. XMEMCPY(sig, out, outSz);
  12924. do {
  12925. #if defined(WOLFSSL_ASYNC_CRYPT)
  12926. ret = wc_AsyncWait(ret, &key->asyncDev,
  12927. WC_ASYNC_FLAG_CALL_AGAIN);
  12928. #endif
  12929. if (ret >= 0) {
  12930. ret = wc_RsaPSS_VerifyInline_ex(sig, outSz,
  12931. (byte**)&plain, hash[l], mgf[k], -1, key);
  12932. }
  12933. } while (ret == WC_PENDING_E);
  12934. if (ret >= 0)
  12935. ERROR_OUT(-7734, exit_rsa_pss);
  12936. }
  12937. }
  12938. #endif
  12939. #ifndef WOLFSSL_SE050
  12940. } /* end mgf for loop */
  12941. #endif
  12942. }
  12943. /* SE050 generates salts internally only of hash length */
  12944. #ifndef WOLFSSL_SE050
  12945. /* Test that a salt length of zero works. */
  12946. digestSz = wc_HashGetDigestSize(hash[0]);
  12947. outSz = RSA_TEST_BYTES;
  12948. do {
  12949. #if defined(WOLFSSL_ASYNC_CRYPT)
  12950. ret = wc_AsyncWait(ret, &key->asyncDev,
  12951. WC_ASYNC_FLAG_CALL_AGAIN);
  12952. #endif
  12953. if (ret >= 0) {
  12954. ret = wc_RsaPSS_Sign_ex(digest, digestSz, out, outSz, hash[0],
  12955. mgf[0], 0, key, rng);
  12956. }
  12957. } while (ret == WC_PENDING_E);
  12958. if (ret <= 0)
  12959. ERROR_OUT(-7735, exit_rsa_pss);
  12960. outSz = ret;
  12961. TEST_SLEEP();
  12962. do {
  12963. #if defined(WOLFSSL_ASYNC_CRYPT)
  12964. ret = wc_AsyncWait(ret, &key->asyncDev,
  12965. WC_ASYNC_FLAG_CALL_AGAIN);
  12966. #endif
  12967. if (ret >= 0) {
  12968. ret = wc_RsaPSS_Verify_ex(out, outSz, sig, outSz, hash[0], mgf[0],
  12969. 0, key);
  12970. }
  12971. } while (ret == WC_PENDING_E);
  12972. if (ret <= 0)
  12973. ERROR_OUT(-7736, exit_rsa_pss);
  12974. plainSz = ret;
  12975. TEST_SLEEP();
  12976. do {
  12977. #if defined(WOLFSSL_ASYNC_CRYPT)
  12978. ret = wc_AsyncWait(ret, &key->asyncDev,
  12979. WC_ASYNC_FLAG_CALL_AGAIN);
  12980. #endif
  12981. if (ret >= 0) {
  12982. #if defined(HAVE_SELFTEST) && \
  12983. (!defined(HAVE_SELFTEST_VERSION) || (HAVE_SELFTEST_VERSION < 2))
  12984. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, sig, plainSz,
  12985. hash[0], 0);
  12986. #elif defined(HAVE_SELFTEST) && (HAVE_SELFTEST_VERSION == 2)
  12987. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, sig, plainSz,
  12988. hash[0], 0, 0);
  12989. #else
  12990. ret = wc_RsaPSS_CheckPadding_ex2(digest, digestSz, sig, plainSz,
  12991. hash[0], 0, 0, HEAP_HINT);
  12992. #endif
  12993. }
  12994. } while (ret == WC_PENDING_E);
  12995. if (ret != 0)
  12996. ERROR_OUT(-7737, exit_rsa_pss);
  12997. XMEMCPY(sig, out, outSz);
  12998. plain = NULL;
  12999. do {
  13000. #if defined(WOLFSSL_ASYNC_CRYPT)
  13001. ret = wc_AsyncWait(ret, &key->asyncDev,
  13002. WC_ASYNC_FLAG_CALL_AGAIN);
  13003. #endif
  13004. if (ret >= 0) {
  13005. ret = wc_RsaPSS_VerifyInline_ex(sig, outSz, &plain, hash[0], mgf[0],
  13006. 0, key);
  13007. }
  13008. } while (ret == WC_PENDING_E);
  13009. if (ret <= 0)
  13010. ERROR_OUT(-7738, exit_rsa_pss);
  13011. plainSz = ret;
  13012. TEST_SLEEP();
  13013. #if defined(HAVE_SELFTEST) && \
  13014. (!defined(HAVE_SELFTEST_VERSION) || (HAVE_SELFTEST_VERSION < 2))
  13015. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  13016. hash[0], 0);
  13017. #elif defined(HAVE_SELFTEST) && (HAVE_SELFTEST_VERSION == 2)
  13018. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  13019. hash[0], 0, 0);
  13020. #else
  13021. ret = wc_RsaPSS_CheckPadding_ex2(digest, digestSz, plain, plainSz, hash[0],
  13022. 0, 0, HEAP_HINT);
  13023. #endif
  13024. if (ret != 0)
  13025. ERROR_OUT(-7739, exit_rsa_pss);
  13026. /* Test bad salt lengths in various APIs. */
  13027. digestSz = wc_HashGetDigestSize(hash[0]);
  13028. outSz = RSA_TEST_BYTES;
  13029. #ifndef WOLFSSL_PSS_SALT_LEN_DISCOVER
  13030. len = -2;
  13031. #else
  13032. len = -3;
  13033. #endif
  13034. do {
  13035. #if defined(WOLFSSL_ASYNC_CRYPT)
  13036. ret = wc_AsyncWait(ret, &key->asyncDev,
  13037. WC_ASYNC_FLAG_CALL_AGAIN);
  13038. #endif
  13039. if (ret >= 0) {
  13040. ret = wc_RsaPSS_Sign_ex(digest, digestSz, out, outSz, hash[0],
  13041. mgf[0], len, key, rng);
  13042. }
  13043. } while (ret == WC_PENDING_E);
  13044. if (ret != PSS_SALTLEN_E)
  13045. ERROR_OUT(-7740, exit_rsa_pss);
  13046. do {
  13047. #if defined(WOLFSSL_ASYNC_CRYPT)
  13048. ret = wc_AsyncWait(ret, &key->asyncDev,
  13049. WC_ASYNC_FLAG_CALL_AGAIN);
  13050. #endif
  13051. if (ret >= 0) {
  13052. ret = wc_RsaPSS_Sign_ex(digest, digestSz, out, outSz, hash[0],
  13053. mgf[0], digestSz + 1, key, rng);
  13054. }
  13055. } while (ret == WC_PENDING_E);
  13056. if (ret != PSS_SALTLEN_E)
  13057. ERROR_OUT(-7741, exit_rsa_pss);
  13058. TEST_SLEEP();
  13059. do {
  13060. #if defined(WOLFSSL_ASYNC_CRYPT)
  13061. ret = wc_AsyncWait(ret, &key->asyncDev,
  13062. WC_ASYNC_FLAG_CALL_AGAIN);
  13063. #endif
  13064. if (ret >= 0) {
  13065. ret = wc_RsaPSS_VerifyInline_ex(sig, outSz, &plain, hash[0],
  13066. mgf[0], -2, key);
  13067. }
  13068. } while (ret == WC_PENDING_E);
  13069. if (ret != PSS_SALTLEN_E)
  13070. ERROR_OUT(-7742, exit_rsa_pss);
  13071. TEST_SLEEP();
  13072. do {
  13073. #if defined(WOLFSSL_ASYNC_CRYPT)
  13074. ret = wc_AsyncWait(ret, &key->asyncDev,
  13075. WC_ASYNC_FLAG_CALL_AGAIN);
  13076. #endif
  13077. if (ret >= 0) {
  13078. ret = wc_RsaPSS_VerifyInline_ex(sig, outSz, &plain, hash[0], mgf[0],
  13079. digestSz + 1, key);
  13080. }
  13081. } while (ret == WC_PENDING_E);
  13082. if (ret != PSS_SALTLEN_E)
  13083. ERROR_OUT(-7743, exit_rsa_pss);
  13084. TEST_SLEEP();
  13085. #ifndef WOLFSSL_PSS_SALT_LEN_DISCOVER
  13086. len = -2;
  13087. #else
  13088. len = -3;
  13089. #endif
  13090. #if defined(HAVE_SELFTEST) && \
  13091. (!defined(HAVE_SELFTEST_VERSION) || (HAVE_SELFTEST_VERSION < 2))
  13092. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  13093. hash[0], len);
  13094. #elif defined(HAVE_SELFTEST) && (HAVE_SELFTEST_VERSION == 2)
  13095. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  13096. hash[0], len, 0);
  13097. #else
  13098. ret = wc_RsaPSS_CheckPadding_ex2(digest, digestSz, plain, plainSz, hash[0],
  13099. len, 0, HEAP_HINT);
  13100. #endif
  13101. if (ret != PSS_SALTLEN_E)
  13102. ERROR_OUT(-7744, exit_rsa_pss);
  13103. #ifndef WOLFSSL_PSS_LONG_SALT
  13104. len = digestSz + 1;
  13105. #else
  13106. len = plainSz - digestSz - 1;
  13107. #endif
  13108. #if defined(HAVE_SELFTEST) && \
  13109. (!defined(HAVE_SELFTEST_VERSION) || (HAVE_SELFTEST_VERSION < 2))
  13110. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  13111. hash[0], len);
  13112. if (ret != PSS_SALTLEN_E)
  13113. ERROR_OUT(-7745, exit_rsa_pss);
  13114. #elif defined(HAVE_SELFTEST) && (HAVE_SELFTEST_VERSION == 2)
  13115. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  13116. hash[0], len, 0);
  13117. if (ret != BAD_PADDING_E)
  13118. ERROR_OUT(-7745, exit_rsa_pss);
  13119. #else
  13120. ret = wc_RsaPSS_CheckPadding_ex2(digest, digestSz, plain, plainSz, hash[0],
  13121. len, 0, HEAP_HINT);
  13122. if (ret != PSS_SALTLEN_E)
  13123. ERROR_OUT(-7745, exit_rsa_pss);
  13124. #endif
  13125. ret = 0;
  13126. #endif /* WOLFSSL_SE050 */
  13127. exit_rsa_pss:
  13128. WC_FREE_VAR(sig, HEAP_HINT);
  13129. WC_FREE_VAR(in, HEAP_HINT);
  13130. WC_FREE_VAR(out, HEAP_HINT);
  13131. return ret;
  13132. }
  13133. #endif /* !WOLFSSL_RSA_VERIFY_ONLY && !WOLFSSL_RSA_PUBLIC_ONLY */
  13134. #endif
  13135. #ifdef WC_RSA_NO_PADDING
  13136. WOLFSSL_TEST_SUBROUTINE int rsa_no_pad_test(void)
  13137. {
  13138. WC_RNG rng;
  13139. byte* tmp = NULL;
  13140. size_t bytes;
  13141. int ret;
  13142. word32 inLen = 0;
  13143. word32 idx = 0;
  13144. word32 outSz = RSA_TEST_BYTES;
  13145. word32 plainSz = RSA_TEST_BYTES;
  13146. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  13147. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096) && \
  13148. !defined(NO_FILESYSTEM)
  13149. XFILE file;
  13150. #endif
  13151. WC_DECLARE_VAR(key, RsaKey, 1, HEAP_HINT);
  13152. WC_DECLARE_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
  13153. WC_DECLARE_VAR(plain, byte, RSA_TEST_BYTES, HEAP_HINT);
  13154. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  13155. if (key == NULL || out == NULL || plain == NULL)
  13156. ERROR_OUT(MEMORY_E, exit_rsa_nopadding);
  13157. #endif
  13158. /* initialize stack structures */
  13159. XMEMSET(&rng, 0, sizeof(rng));
  13160. XMEMSET(key, 0, sizeof(RsaKey));
  13161. #ifdef USE_CERT_BUFFERS_1024
  13162. bytes = (size_t)sizeof_client_key_der_1024;
  13163. if (bytes < (size_t)sizeof_client_cert_der_1024)
  13164. bytes = (size_t)sizeof_client_cert_der_1024;
  13165. #elif defined(USE_CERT_BUFFERS_2048)
  13166. bytes = (size_t)sizeof_client_key_der_2048;
  13167. if (bytes < (size_t)sizeof_client_cert_der_2048)
  13168. bytes = (size_t)sizeof_client_cert_der_2048;
  13169. #else
  13170. bytes = FOURK_BUF;
  13171. #endif
  13172. tmp = (byte*)XMALLOC(bytes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13173. if (tmp == NULL
  13174. #ifdef WOLFSSL_ASYNC_CRYPT
  13175. || out == NULL || plain == NULL
  13176. #endif
  13177. ) {
  13178. ERROR_OUT(-7800, exit_rsa_nopadding);
  13179. }
  13180. #ifdef USE_CERT_BUFFERS_1024
  13181. XMEMCPY(tmp, client_key_der_1024, (size_t)sizeof_client_key_der_1024);
  13182. #elif defined(USE_CERT_BUFFERS_2048)
  13183. XMEMCPY(tmp, client_key_der_2048, (size_t)sizeof_client_key_der_2048);
  13184. #elif defined(USE_CERT_BUFFERS_3072)
  13185. XMEMCPY(tmp, client_key_der_3072, (size_t)sizeof_client_key_der_3072);
  13186. #elif defined(USE_CERT_BUFFERS_4096)
  13187. XMEMCPY(tmp, client_key_der_4096, (size_t)sizeof_client_key_der_4096);
  13188. #elif !defined(NO_FILESYSTEM)
  13189. file = XFOPEN(clientKey, "rb");
  13190. if (!file) {
  13191. err_sys("can't open clientKey, Please run from wolfSSL home dir", -40);
  13192. ERROR_OUT(-7801, exit_rsa_nopadding);
  13193. }
  13194. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  13195. XFCLOSE(file);
  13196. #else
  13197. /* No key to use. */
  13198. ERROR_OUT(-7802, exit_rsa_nopadding);
  13199. #endif /* USE_CERT_BUFFERS */
  13200. ret = wc_InitRsaKey_ex(key, HEAP_HINT, devId);
  13201. if (ret != 0) {
  13202. ERROR_OUT(-7803, exit_rsa_nopadding);
  13203. }
  13204. ret = wc_RsaPrivateKeyDecode(tmp, &idx, key, (word32)bytes);
  13205. if (ret != 0) {
  13206. ERROR_OUT(-7804, exit_rsa_nopadding);
  13207. }
  13208. /* after loading in key use tmp as the test buffer */
  13209. #ifndef HAVE_FIPS
  13210. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  13211. #else
  13212. ret = wc_InitRng(&rng);
  13213. #endif
  13214. if (ret != 0) {
  13215. ERROR_OUT(-7805, exit_rsa_nopadding);
  13216. }
  13217. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  13218. inLen = wc_RsaEncryptSize(key);
  13219. outSz = inLen;
  13220. plainSz = inLen;
  13221. XMEMSET(tmp, 7, inLen);
  13222. do {
  13223. #if defined(WOLFSSL_ASYNC_CRYPT)
  13224. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13225. #endif
  13226. if (ret >= 0) {
  13227. ret = wc_RsaDirect(tmp, inLen, out, &outSz, key,
  13228. RSA_PRIVATE_ENCRYPT, &rng);
  13229. }
  13230. } while (ret == WC_PENDING_E);
  13231. if (ret <= 0) {
  13232. ERROR_OUT(-7806, exit_rsa_nopadding);
  13233. }
  13234. /* encrypted result should not be the same as input */
  13235. if (XMEMCMP(out, tmp, inLen) == 0) {
  13236. ERROR_OUT(-7807, exit_rsa_nopadding);
  13237. }
  13238. TEST_SLEEP();
  13239. /* decrypt with public key and compare result */
  13240. do {
  13241. #if defined(WOLFSSL_ASYNC_CRYPT)
  13242. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13243. #endif
  13244. if (ret >= 0) {
  13245. ret = wc_RsaDirect(out, outSz, plain, &plainSz, key,
  13246. RSA_PUBLIC_DECRYPT, &rng);
  13247. }
  13248. } while (ret == WC_PENDING_E);
  13249. if (ret <= 0) {
  13250. ERROR_OUT(-7808, exit_rsa_nopadding);
  13251. }
  13252. if (XMEMCMP(plain, tmp, inLen) != 0) {
  13253. ERROR_OUT(-7809, exit_rsa_nopadding);
  13254. }
  13255. TEST_SLEEP();
  13256. #endif
  13257. #ifdef WC_RSA_BLINDING
  13258. ret = wc_RsaSetRNG(NULL, &rng);
  13259. if (ret != BAD_FUNC_ARG) {
  13260. ERROR_OUT(-7810, exit_rsa_nopadding);
  13261. }
  13262. ret = wc_RsaSetRNG(key, &rng);
  13263. if (ret < 0) {
  13264. ERROR_OUT(-7811, exit_rsa_nopadding);
  13265. }
  13266. #endif
  13267. /* test encrypt and decrypt using WC_RSA_NO_PAD */
  13268. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  13269. do {
  13270. #if defined(WOLFSSL_ASYNC_CRYPT)
  13271. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13272. #endif
  13273. if (ret >= 0) {
  13274. ret = wc_RsaPublicEncrypt_ex(tmp, inLen, out, (int)outSz, key, &rng,
  13275. WC_RSA_NO_PAD, WC_HASH_TYPE_NONE, WC_MGF1NONE, NULL, 0);
  13276. }
  13277. } while (ret == WC_PENDING_E);
  13278. if (ret < 0) {
  13279. ERROR_OUT(-7812, exit_rsa_nopadding);
  13280. }
  13281. TEST_SLEEP();
  13282. #endif /* WOLFSSL_RSA_VERIFY_ONLY */
  13283. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  13284. do {
  13285. #if defined(WOLFSSL_ASYNC_CRYPT)
  13286. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13287. #endif
  13288. if (ret >= 0) {
  13289. ret = wc_RsaPrivateDecrypt_ex(out, outSz, plain, (int)plainSz, key,
  13290. WC_RSA_NO_PAD, WC_HASH_TYPE_NONE, WC_MGF1NONE, NULL, 0);
  13291. }
  13292. } while (ret == WC_PENDING_E);
  13293. if (ret < 0) {
  13294. ERROR_OUT(-7813, exit_rsa_nopadding);
  13295. }
  13296. if (XMEMCMP(plain, tmp, inLen) != 0) {
  13297. ERROR_OUT(-7814, exit_rsa_nopadding);
  13298. }
  13299. TEST_SLEEP();
  13300. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  13301. /* test some bad arguments */
  13302. ret = wc_RsaDirect(out, outSz, plain, &plainSz, key, -1,
  13303. &rng);
  13304. if (ret != BAD_FUNC_ARG) {
  13305. ERROR_OUT(-7815, exit_rsa_nopadding);
  13306. }
  13307. ret = wc_RsaDirect(out, outSz, plain, &plainSz, NULL, RSA_PUBLIC_DECRYPT,
  13308. &rng);
  13309. if (ret != BAD_FUNC_ARG) {
  13310. ERROR_OUT(-7816, exit_rsa_nopadding);
  13311. }
  13312. ret = wc_RsaDirect(out, outSz, NULL, &plainSz, key, RSA_PUBLIC_DECRYPT,
  13313. &rng);
  13314. if (ret != LENGTH_ONLY_E || plainSz != inLen) {
  13315. ERROR_OUT(-7817, exit_rsa_nopadding);
  13316. }
  13317. ret = wc_RsaDirect(out, outSz - 10, plain, &plainSz, key,
  13318. RSA_PUBLIC_DECRYPT, &rng);
  13319. if (ret != BAD_FUNC_ARG) {
  13320. ERROR_OUT(-7818, exit_rsa_nopadding);
  13321. }
  13322. /* if making it to this point of code without hitting an ERROR_OUT then
  13323. * all tests have passed */
  13324. ret = 0;
  13325. exit_rsa_nopadding:
  13326. wc_FreeRsaKey(key);
  13327. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13328. WC_FREE_VAR(key, HEAP_HINT);
  13329. WC_FREE_VAR(out, HEAP_HINT);
  13330. WC_FREE_VAR(plain, HEAP_HINT);
  13331. wc_FreeRng(&rng);
  13332. return ret;
  13333. }
  13334. #endif /* WC_RSA_NO_PADDING */
  13335. #if defined(WOLFSSL_HAVE_SP_RSA) && defined(USE_FAST_MATH)
  13336. static int rsa_even_mod_test(WC_RNG* rng, RsaKey* key)
  13337. {
  13338. byte* tmp = NULL;
  13339. size_t bytes;
  13340. int ret;
  13341. word32 inLen = 0;
  13342. #ifndef NO_ASN
  13343. word32 idx = 0;
  13344. #endif
  13345. word32 outSz = RSA_TEST_BYTES;
  13346. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  13347. word32 plainSz = RSA_TEST_BYTES;
  13348. #endif
  13349. #if !defined(USE_CERT_BUFFERS_2048) && !defined(USE_CERT_BUFFERS_3072) && \
  13350. !defined(USE_CERT_BUFFERS_4096) && !defined(NO_FILESYSTEM)
  13351. XFILE file;
  13352. #endif
  13353. WC_DECLARE_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
  13354. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  13355. WC_DECLARE_VAR(plain, byte, RSA_TEST_BYTES, HEAP_HINT);
  13356. #endif
  13357. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  13358. if (out == NULL
  13359. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  13360. || plain == NULL
  13361. #endif
  13362. ) {
  13363. ERROR_OUT(MEMORY_E, exit_rsa_even_mod);
  13364. }
  13365. #endif
  13366. #if defined(USE_CERT_BUFFERS_2048)
  13367. bytes = (size_t)sizeof_client_key_der_2048;
  13368. if (bytes < (size_t)sizeof_client_cert_der_2048)
  13369. bytes = (size_t)sizeof_client_cert_der_2048;
  13370. #else
  13371. bytes = FOURK_BUF;
  13372. #endif
  13373. tmp = (byte*)XMALLOC(bytes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13374. if (tmp == NULL
  13375. #ifdef WOLFSSL_ASYNC_CRYPT
  13376. || out == NULL || plain == NULL
  13377. #endif
  13378. ) {
  13379. ERROR_OUT(-7800, exit_rsa_even_mod);
  13380. }
  13381. #if defined(USE_CERT_BUFFERS_2048)
  13382. XMEMCPY(tmp, client_key_der_2048, (size_t)sizeof_client_key_der_2048);
  13383. #elif defined(USE_CERT_BUFFERS_3072)
  13384. XMEMCPY(tmp, client_key_der_3072, (size_t)sizeof_client_key_der_3072);
  13385. #elif defined(USE_CERT_BUFFERS_4096)
  13386. XMEMCPY(tmp, client_key_der_4096, (size_t)sizeof_client_key_der_4096);
  13387. #elif !defined(NO_FILESYSTEM)
  13388. file = XFOPEN(clientKey, "rb");
  13389. if (!file) {
  13390. err_sys("can't open ./certs/client-key.der, "
  13391. "Please run from wolfSSL home dir", -40);
  13392. ERROR_OUT(-7801, exit_rsa_even_mod);
  13393. }
  13394. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  13395. XFCLOSE(file);
  13396. #else
  13397. /* No key to use. */
  13398. ERROR_OUT(-7802, exit_rsa_even_mod);
  13399. #endif /* USE_CERT_BUFFERS */
  13400. #ifndef NO_ASN
  13401. ret = wc_RsaPrivateKeyDecode(tmp, &idx, key, (word32)bytes);
  13402. if (ret != 0) {
  13403. ERROR_OUT(-7804, exit_rsa_even_mod);
  13404. }
  13405. #else
  13406. #ifdef USE_CERT_BUFFERS_2048
  13407. ret = mp_read_unsigned_bin(&key->n, &tmp[12], 256);
  13408. if (ret != 0) {
  13409. ERROR_OUT(-7804, exit_rsa_even_mod);
  13410. }
  13411. ret = mp_set_int(&key->e, WC_RSA_EXPONENT);
  13412. if (ret != 0) {
  13413. ERROR_OUT(-7804, exit_rsa_even_mod);
  13414. }
  13415. #ifndef NO_SIG_WRAPPER
  13416. modLen = 2048;
  13417. #endif
  13418. #else
  13419. #error Not supported yet!
  13420. #endif
  13421. #endif
  13422. key->n.dp[0] &= (mp_digit)-2;
  13423. if (ret != 0) {
  13424. ERROR_OUT(-7804, exit_rsa_even_mod);
  13425. }
  13426. /* after loading in key use tmp as the test buffer */
  13427. #if !(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION == 2) && \
  13428. (defined(WOLFSSL_SP_ARM64_ASM) || defined(WOLFSSL_SP_ARM32_ASM))) && \
  13429. !defined(WOLFSSL_XILINX_CRYPT)
  13430. /* The ARM64_ASM code that was FIPS validated did not return these expected
  13431. * failure codes. These tests cases were added after the assembly was
  13432. * in-lined in the module and validated, these tests will be available in
  13433. * the 140-3 module */
  13434. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_PUBLIC_ONLY)
  13435. inLen = 32;
  13436. outSz = wc_RsaEncryptSize(key);
  13437. XMEMSET(tmp, 7, plainSz);
  13438. ret = wc_RsaSSL_Sign(tmp, inLen, out, outSz, key, rng);
  13439. if (ret != MP_VAL && ret != MP_EXPTMOD_E && ret != MP_INVMOD_E) {
  13440. ERROR_OUT(-7806, exit_rsa_even_mod);
  13441. }
  13442. ret = wc_RsaSSL_Verify(out, outSz, tmp, inLen, key);
  13443. if (ret != MP_VAL && ret != MP_EXPTMOD_E) {
  13444. ERROR_OUT(-7808, exit_rsa_even_mod);
  13445. }
  13446. #endif
  13447. #ifdef WC_RSA_BLINDING
  13448. ret = wc_RsaSetRNG(key, rng);
  13449. if (ret < 0) {
  13450. ERROR_OUT(-7811, exit_rsa_even_mod);
  13451. }
  13452. #endif
  13453. /* test encrypt and decrypt using WC_RSA_NO_PAD */
  13454. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_PUBLIC_ONLY)
  13455. ret = wc_RsaPublicEncrypt(tmp, inLen, out, (int)outSz, key, rng);
  13456. if (ret != MP_VAL && ret != MP_EXPTMOD_E) {
  13457. ERROR_OUT(-7812, exit_rsa_even_mod);
  13458. }
  13459. #endif /* WOLFSSL_RSA_VERIFY_ONLY */
  13460. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  13461. ret = wc_RsaPrivateDecrypt(out, outSz, plain, (int)plainSz, key);
  13462. if (ret != MP_VAL && ret != MP_EXPTMOD_E && ret != MP_INVMOD_E) {
  13463. ERROR_OUT(-7813, exit_rsa_even_mod);
  13464. }
  13465. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  13466. #endif /* !(HAVE_FIPS_VERSION == 2 && WOLFSSL_SP_ARMxx_ASM) */
  13467. /* if making it to this point of code without hitting an ERROR_OUT then
  13468. * all tests have passed */
  13469. ret = 0;
  13470. exit_rsa_even_mod:
  13471. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13472. WC_FREE_VAR(out, HEAP_HINT);
  13473. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  13474. WC_FREE_VAR(plain, HEAP_HINT);
  13475. #endif
  13476. (void)out;
  13477. (void)outSz;
  13478. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  13479. (void)plain;
  13480. (void)plainSz;
  13481. #endif
  13482. (void)inLen;
  13483. (void)rng;
  13484. return ret;
  13485. }
  13486. #endif /* WOLFSSL_HAVE_SP_RSA */
  13487. #ifdef WOLFSSL_CERT_GEN
  13488. static int rsa_certgen_test(RsaKey* key, RsaKey* keypub, WC_RNG* rng, byte* tmp)
  13489. {
  13490. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  13491. RsaKey *caKey = (RsaKey *)XMALLOC(sizeof *caKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13492. #ifdef WOLFSSL_TEST_CERT
  13493. DecodedCert *decode = (DecodedCert *)XMALLOC(sizeof *decode, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13494. #endif
  13495. #else
  13496. RsaKey caKey[1];
  13497. #ifdef WOLFSSL_TEST_CERT
  13498. DecodedCert decode[1];
  13499. #endif
  13500. #endif
  13501. byte* der = NULL;
  13502. int ret;
  13503. Cert* myCert = NULL;
  13504. int certSz;
  13505. size_t bytes3;
  13506. word32 idx3 = 0;
  13507. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  13508. XFILE file3;
  13509. #endif
  13510. #if defined(WOLFSSL_ALT_NAMES) && !defined(NO_ASN_TIME)
  13511. struct tm beforeTime;
  13512. struct tm afterTime;
  13513. #endif
  13514. const byte mySerial[8] = {1,2,3,4,5,6,7,8};
  13515. (void)keypub;
  13516. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  13517. if (caKey == NULL)
  13518. ERROR_OUT(MEMORY_E, exit_rsa);
  13519. #ifdef WOLFSSL_TEST_CERT
  13520. if (decode == NULL)
  13521. ERROR_OUT(MEMORY_E, exit_rsa);
  13522. #endif
  13523. #endif
  13524. XMEMSET(caKey, 0, sizeof *caKey);
  13525. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13526. if (der == NULL) {
  13527. ERROR_OUT(-7820, exit_rsa);
  13528. }
  13529. myCert = (Cert*)XMALLOC(sizeof(Cert), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13530. if (myCert == NULL) {
  13531. ERROR_OUT(-7821, exit_rsa);
  13532. }
  13533. /* self signed */
  13534. if (wc_InitCert_ex(myCert, HEAP_HINT, devId)) {
  13535. ERROR_OUT(-7822, exit_rsa);
  13536. }
  13537. XMEMCPY(&myCert->subject, &certDefaultName, sizeof(CertName));
  13538. XMEMCPY(myCert->serial, mySerial, sizeof(mySerial));
  13539. myCert->serialSz = (int)sizeof(mySerial);
  13540. myCert->isCA = 1;
  13541. #ifndef NO_SHA256
  13542. myCert->sigType = CTC_SHA256wRSA;
  13543. #else
  13544. myCert->sigType = CTC_SHAwRSA;
  13545. #endif
  13546. #ifdef WOLFSSL_CERT_EXT
  13547. /* add Policies */
  13548. XSTRNCPY(myCert->certPolicies[0], "2.16.840.1.101.3.4.1.42",
  13549. CTC_MAX_CERTPOL_SZ);
  13550. XSTRNCPY(myCert->certPolicies[1], "1.2.840.113549.1.9.16.6.5",
  13551. CTC_MAX_CERTPOL_SZ);
  13552. myCert->certPoliciesNb = 2;
  13553. /* add SKID from the Public Key */
  13554. if (wc_SetSubjectKeyIdFromPublicKey(myCert, keypub, NULL) != 0) {
  13555. ERROR_OUT(-7823, exit_rsa);
  13556. }
  13557. /* add AKID from the Public Key */
  13558. if (wc_SetAuthKeyIdFromPublicKey(myCert, keypub, NULL) != 0) {
  13559. ERROR_OUT(-7824, exit_rsa);
  13560. }
  13561. /* add Key Usage */
  13562. if (wc_SetKeyUsage(myCert,"cRLSign,keyCertSign") != 0) {
  13563. ERROR_OUT(-7825, exit_rsa);
  13564. }
  13565. #ifdef WOLFSSL_EKU_OID
  13566. {
  13567. const char unique[] = "2.16.840.1.111111.100.1.10.1";
  13568. if (wc_SetExtKeyUsageOID(myCert, unique, sizeof(unique), 0,
  13569. HEAP_HINT) != 0) {
  13570. ERROR_OUT(-7826, exit_rsa);
  13571. }
  13572. }
  13573. #endif /* WOLFSSL_EKU_OID */
  13574. #endif /* WOLFSSL_CERT_EXT */
  13575. ret = 0;
  13576. do {
  13577. #if defined(WOLFSSL_ASYNC_CRYPT)
  13578. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13579. #endif
  13580. if (ret >= 0) {
  13581. ret = wc_MakeSelfCert(myCert, der, FOURK_BUF, key, rng);
  13582. }
  13583. } while (ret == WC_PENDING_E);
  13584. if (ret < 0) {
  13585. ERROR_OUT(-7827, exit_rsa);
  13586. }
  13587. certSz = ret;
  13588. #ifdef WOLFSSL_TEST_CERT
  13589. InitDecodedCert(decode, der, certSz, HEAP_HINT);
  13590. ret = ParseCert(decode, CERT_TYPE, NO_VERIFY, 0);
  13591. if (ret != 0) {
  13592. FreeDecodedCert(decode);
  13593. ERROR_OUT(-7828, exit_rsa);
  13594. }
  13595. FreeDecodedCert(decode);
  13596. #endif
  13597. ret = SaveDerAndPem(der, certSz, certDerFile, certPemFile,
  13598. CERT_TYPE, -5578);
  13599. if (ret != 0) {
  13600. goto exit_rsa;
  13601. }
  13602. /* Setup Certificate */
  13603. if (wc_InitCert_ex(myCert, HEAP_HINT, devId)) {
  13604. ERROR_OUT(-7829, exit_rsa);
  13605. }
  13606. #ifdef WOLFSSL_ALT_NAMES
  13607. /* Get CA Cert for testing */
  13608. #ifdef USE_CERT_BUFFERS_1024
  13609. XMEMCPY(tmp, ca_cert_der_1024, sizeof_ca_cert_der_1024);
  13610. bytes3 = sizeof_ca_cert_der_1024;
  13611. #elif defined(USE_CERT_BUFFERS_2048)
  13612. XMEMCPY(tmp, ca_cert_der_2048, sizeof_ca_cert_der_2048);
  13613. bytes3 = sizeof_ca_cert_der_2048;
  13614. #else
  13615. file3 = XFOPEN(rsaCaCertDerFile, "rb");
  13616. if (!file3) {
  13617. ERROR_OUT(-7830, exit_rsa);
  13618. }
  13619. bytes3 = XFREAD(tmp, 1, FOURK_BUF, file3);
  13620. XFCLOSE(file3);
  13621. #endif /* USE_CERT_BUFFERS */
  13622. #if !defined(NO_FILESYSTEM) && !defined(USE_CERT_BUFFERS_1024) && \
  13623. !defined(USE_CERT_BUFFERS_2048) && !defined(NO_ASN)
  13624. ret = wc_SetAltNames(myCert, rsaCaCertFile);
  13625. if (ret != 0) {
  13626. ERROR_OUT(-7831, exit_rsa);
  13627. }
  13628. #endif
  13629. /* get alt names from der */
  13630. ret = wc_SetAltNamesBuffer(myCert, tmp, (int)bytes3);
  13631. if (ret != 0) {
  13632. ERROR_OUT(-7832, exit_rsa);
  13633. }
  13634. /* get dates from der */
  13635. ret = wc_SetDatesBuffer(myCert, tmp, (int)bytes3);
  13636. if (ret != 0) {
  13637. ERROR_OUT(-7833, exit_rsa);
  13638. }
  13639. #ifndef NO_ASN_TIME
  13640. ret = wc_GetCertDates(myCert, &beforeTime, &afterTime);
  13641. if (ret < 0) {
  13642. ERROR_OUT(-7834, exit_rsa);
  13643. }
  13644. #endif
  13645. #endif /* WOLFSSL_ALT_NAMES */
  13646. /* Get CA Key */
  13647. #ifdef USE_CERT_BUFFERS_1024
  13648. XMEMCPY(tmp, ca_key_der_1024, sizeof_ca_key_der_1024);
  13649. bytes3 = sizeof_ca_key_der_1024;
  13650. #elif defined(USE_CERT_BUFFERS_2048)
  13651. XMEMCPY(tmp, ca_key_der_2048, sizeof_ca_key_der_2048);
  13652. bytes3 = sizeof_ca_key_der_2048;
  13653. #else
  13654. file3 = XFOPEN(rsaCaKeyFile, "rb");
  13655. if (!file3) {
  13656. ERROR_OUT(-7835, exit_rsa);
  13657. }
  13658. bytes3 = XFREAD(tmp, 1, FOURK_BUF, file3);
  13659. XFCLOSE(file3);
  13660. #endif /* USE_CERT_BUFFERS */
  13661. ret = wc_InitRsaKey(caKey, HEAP_HINT);
  13662. if (ret != 0) {
  13663. ERROR_OUT(-7836, exit_rsa);
  13664. }
  13665. ret = wc_RsaPrivateKeyDecode(tmp, &idx3, caKey, (word32)bytes3);
  13666. if (ret != 0) {
  13667. ERROR_OUT(-7837, exit_rsa);
  13668. }
  13669. #ifndef NO_SHA256
  13670. myCert->sigType = CTC_SHA256wRSA;
  13671. #else
  13672. myCert->sigType = CTC_SHAwRSA;
  13673. #endif
  13674. XMEMCPY(&myCert->subject, &certDefaultName, sizeof(CertName));
  13675. #ifdef WOLFSSL_CERT_EXT
  13676. /* add Policies */
  13677. XSTRNCPY(myCert->certPolicies[0], "2.16.840.1.101.3.4.1.42",
  13678. CTC_MAX_CERTPOL_SZ);
  13679. myCert->certPoliciesNb =1;
  13680. /* add SKID from the Public Key */
  13681. if (wc_SetSubjectKeyIdFromPublicKey(myCert, key, NULL) != 0) {
  13682. ERROR_OUT(-7838, exit_rsa);
  13683. }
  13684. /* add AKID from the CA certificate */
  13685. #if defined(USE_CERT_BUFFERS_2048)
  13686. ret = wc_SetAuthKeyIdFromCert(myCert, ca_cert_der_2048,
  13687. sizeof_ca_cert_der_2048);
  13688. #elif defined(USE_CERT_BUFFERS_1024)
  13689. ret = wc_SetAuthKeyIdFromCert(myCert, ca_cert_der_1024,
  13690. sizeof_ca_cert_der_1024);
  13691. #else
  13692. ret = wc_SetAuthKeyId(myCert, rsaCaCertFile);
  13693. #endif
  13694. if (ret != 0) {
  13695. ERROR_OUT(-7839, exit_rsa);
  13696. }
  13697. /* add Key Usage */
  13698. if (wc_SetKeyUsage(myCert,"keyEncipherment,keyAgreement") != 0) {
  13699. ERROR_OUT(-7840, exit_rsa);
  13700. }
  13701. #endif /* WOLFSSL_CERT_EXT */
  13702. #if defined(USE_CERT_BUFFERS_2048)
  13703. ret = wc_SetIssuerBuffer(myCert, ca_cert_der_2048,
  13704. sizeof_ca_cert_der_2048);
  13705. #elif defined(USE_CERT_BUFFERS_1024)
  13706. ret = wc_SetIssuerBuffer(myCert, ca_cert_der_1024,
  13707. sizeof_ca_cert_der_1024);
  13708. #else
  13709. ret = wc_SetIssuer(myCert, rsaCaCertFile);
  13710. #endif
  13711. if (ret < 0) {
  13712. ERROR_OUT(-7841, exit_rsa);
  13713. }
  13714. certSz = wc_MakeCert(myCert, der, FOURK_BUF, key, NULL, rng);
  13715. if (certSz < 0) {
  13716. ERROR_OUT(-7842, exit_rsa);
  13717. }
  13718. ret = 0;
  13719. do {
  13720. #if defined(WOLFSSL_ASYNC_CRYPT)
  13721. ret = wc_AsyncWait(ret, &caKey->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13722. #endif
  13723. if (ret >= 0) {
  13724. ret = wc_SignCert(myCert->bodySz, myCert->sigType, der, FOURK_BUF,
  13725. caKey, NULL, rng);
  13726. }
  13727. } while (ret == WC_PENDING_E);
  13728. if (ret < 0) {
  13729. ERROR_OUT(-7843, exit_rsa);
  13730. }
  13731. certSz = ret;
  13732. #ifdef WOLFSSL_TEST_CERT
  13733. InitDecodedCert(decode, der, certSz, HEAP_HINT);
  13734. ret = ParseCert(decode, CERT_TYPE, NO_VERIFY, 0);
  13735. if (ret != 0) {
  13736. FreeDecodedCert(decode);
  13737. ERROR_OUT(-7844, exit_rsa);
  13738. }
  13739. FreeDecodedCert(decode);
  13740. #endif
  13741. ret = SaveDerAndPem(der, certSz, otherCertDerFile, otherCertPemFile,
  13742. CERT_TYPE, -5598);
  13743. if (ret != 0) {
  13744. goto exit_rsa;
  13745. }
  13746. exit_rsa:
  13747. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  13748. if (caKey != NULL) {
  13749. wc_FreeRsaKey(caKey);
  13750. XFREE(caKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13751. }
  13752. #ifdef WOLFSSL_TEST_CERT
  13753. if (decode != NULL)
  13754. XFREE(decode, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13755. #endif
  13756. #else
  13757. wc_FreeRsaKey(caKey);
  13758. #endif
  13759. XFREE(myCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13760. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13761. return ret;
  13762. }
  13763. #endif
  13764. #if !defined(NO_RSA) && defined(HAVE_ECC) && defined(WOLFSSL_CERT_GEN)
  13765. /* Make Cert / Sign example for ECC cert and RSA CA */
  13766. static int rsa_ecc_certgen_test(WC_RNG* rng, byte* tmp)
  13767. {
  13768. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  13769. RsaKey *caKey = (RsaKey *)XMALLOC(sizeof *caKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13770. ecc_key *caEccKey = (ecc_key *)XMALLOC(sizeof *caEccKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13771. ecc_key *caEccKeyPub = (ecc_key *)XMALLOC(sizeof *caEccKeyPub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13772. #ifdef WOLFSSL_TEST_CERT
  13773. DecodedCert *decode = (DecodedCert *)XMALLOC(sizeof *decode, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13774. #endif
  13775. #else
  13776. RsaKey caKey[1];
  13777. ecc_key caEccKey[1];
  13778. ecc_key caEccKeyPub[1];
  13779. #ifdef WOLFSSL_TEST_CERT
  13780. DecodedCert decode[1];
  13781. #endif
  13782. #endif
  13783. byte* der = NULL;
  13784. Cert* myCert = NULL;
  13785. int certSz;
  13786. size_t bytes3;
  13787. word32 idx3 = 0;
  13788. #if (!defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)) \
  13789. || !defined(USE_CERT_BUFFERS_256)
  13790. XFILE file3;
  13791. #endif
  13792. int ret;
  13793. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  13794. if ((caKey == NULL) || (caEccKey == NULL) || (caEccKeyPub == NULL)
  13795. #ifdef WOLFSSL_TEST_CERT
  13796. || (decode == NULL)
  13797. #endif
  13798. )
  13799. ERROR_OUT(MEMORY_E, exit_rsa);
  13800. #endif
  13801. XMEMSET(caKey, 0, sizeof *caKey);
  13802. XMEMSET(caEccKey, 0, sizeof *caEccKey);
  13803. XMEMSET(caEccKeyPub, 0, sizeof *caEccKeyPub);
  13804. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13805. if (der == NULL) {
  13806. ERROR_OUT(-7850, exit_rsa);
  13807. }
  13808. myCert = (Cert*)XMALLOC(sizeof(Cert), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13809. if (myCert == NULL) {
  13810. ERROR_OUT(-7851, exit_rsa);
  13811. }
  13812. /* Get CA Key */
  13813. #ifdef USE_CERT_BUFFERS_1024
  13814. XMEMCPY(tmp, ca_key_der_1024, sizeof_ca_key_der_1024);
  13815. bytes3 = sizeof_ca_key_der_1024;
  13816. #elif defined(USE_CERT_BUFFERS_2048)
  13817. XMEMCPY(tmp, ca_key_der_2048, sizeof_ca_key_der_2048);
  13818. bytes3 = sizeof_ca_key_der_2048;
  13819. #else
  13820. file3 = XFOPEN(rsaCaKeyFile, "rb");
  13821. if (!file3) {
  13822. ERROR_OUT(-7852, exit_rsa);
  13823. }
  13824. bytes3 = XFREAD(tmp, 1, FOURK_BUF, file3);
  13825. XFCLOSE(file3);
  13826. #endif /* USE_CERT_BUFFERS */
  13827. ret = wc_InitRsaKey(caKey, HEAP_HINT);
  13828. if (ret != 0) {
  13829. ERROR_OUT(-7853, exit_rsa);
  13830. }
  13831. ret = wc_RsaPrivateKeyDecode(tmp, &idx3, caKey, (word32)bytes3);
  13832. if (ret != 0) {
  13833. ERROR_OUT(-7854, exit_rsa);
  13834. }
  13835. /* Get Cert Key */
  13836. #ifdef USE_CERT_BUFFERS_256
  13837. XMEMCPY(tmp, ecc_key_pub_der_256, sizeof_ecc_key_pub_der_256);
  13838. bytes3 = sizeof_ecc_key_pub_der_256;
  13839. #else
  13840. file3 = XFOPEN(eccKeyPubFileDer, "rb");
  13841. if (!file3) {
  13842. ERROR_OUT(-7855, exit_rsa);
  13843. }
  13844. bytes3 = XFREAD(tmp, 1, FOURK_BUF, file3);
  13845. XFCLOSE(file3);
  13846. #endif
  13847. ret = wc_ecc_init_ex(caEccKeyPub, HEAP_HINT, devId);
  13848. if (ret != 0) {
  13849. ERROR_OUT(-7856, exit_rsa);
  13850. }
  13851. idx3 = 0;
  13852. ret = wc_EccPublicKeyDecode(tmp, &idx3, caEccKeyPub, (word32)bytes3);
  13853. if (ret != 0) {
  13854. ERROR_OUT(-7857, exit_rsa);
  13855. }
  13856. /* Setup Certificate */
  13857. if (wc_InitCert_ex(myCert, HEAP_HINT, devId)) {
  13858. ERROR_OUT(-7858, exit_rsa);
  13859. }
  13860. #ifndef NO_SHA256
  13861. myCert->sigType = CTC_SHA256wRSA;
  13862. #else
  13863. myCert->sigType = CTC_SHAwRSA;
  13864. #endif
  13865. XMEMCPY(&myCert->subject, &certDefaultName, sizeof(CertName));
  13866. #ifdef WOLFSSL_CERT_EXT
  13867. /* add Policies */
  13868. XSTRNCPY(myCert->certPolicies[0], "2.4.589440.587.101.2.1.9632587.1",
  13869. CTC_MAX_CERTPOL_SZ);
  13870. XSTRNCPY(myCert->certPolicies[1], "1.2.13025.489.1.113549",
  13871. CTC_MAX_CERTPOL_SZ);
  13872. myCert->certPoliciesNb = 2;
  13873. /* add SKID from the Public Key */
  13874. if (wc_SetSubjectKeyIdFromPublicKey(myCert, NULL, caEccKeyPub) != 0) {
  13875. ERROR_OUT(-7859, exit_rsa);
  13876. }
  13877. /* add AKID from the CA certificate */
  13878. #if defined(USE_CERT_BUFFERS_2048)
  13879. ret = wc_SetAuthKeyIdFromCert(myCert, ca_cert_der_2048,
  13880. sizeof_ca_cert_der_2048);
  13881. #elif defined(USE_CERT_BUFFERS_1024)
  13882. ret = wc_SetAuthKeyIdFromCert(myCert, ca_cert_der_1024,
  13883. sizeof_ca_cert_der_1024);
  13884. #else
  13885. ret = wc_SetAuthKeyId(myCert, rsaCaCertFile);
  13886. #endif
  13887. if (ret != 0) {
  13888. ERROR_OUT(-7860, exit_rsa);
  13889. }
  13890. /* add Key Usage */
  13891. if (wc_SetKeyUsage(myCert, certKeyUsage) != 0) {
  13892. ERROR_OUT(-7861, exit_rsa);
  13893. }
  13894. #endif /* WOLFSSL_CERT_EXT */
  13895. #if defined(USE_CERT_BUFFERS_2048)
  13896. ret = wc_SetIssuerBuffer(myCert, ca_cert_der_2048,
  13897. sizeof_ca_cert_der_2048);
  13898. #elif defined(USE_CERT_BUFFERS_1024)
  13899. ret = wc_SetIssuerBuffer(myCert, ca_cert_der_1024,
  13900. sizeof_ca_cert_der_1024);
  13901. #else
  13902. ret = wc_SetIssuer(myCert, rsaCaCertFile);
  13903. #endif
  13904. if (ret < 0) {
  13905. ERROR_OUT(-7862, exit_rsa);
  13906. }
  13907. certSz = wc_MakeCert(myCert, der, FOURK_BUF, NULL, caEccKeyPub, rng);
  13908. if (certSz < 0) {
  13909. ERROR_OUT(-7863, exit_rsa);
  13910. }
  13911. ret = 0;
  13912. do {
  13913. #if defined(WOLFSSL_ASYNC_CRYPT)
  13914. ret = wc_AsyncWait(ret, &caEccKey->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13915. #endif
  13916. if (ret >= 0) {
  13917. ret = wc_SignCert(myCert->bodySz, myCert->sigType, der,
  13918. FOURK_BUF, caKey, NULL, rng);
  13919. }
  13920. } while (ret == WC_PENDING_E);
  13921. if (ret < 0) {
  13922. ERROR_OUT(-7864, exit_rsa);
  13923. }
  13924. certSz = ret;
  13925. #ifdef WOLFSSL_TEST_CERT
  13926. InitDecodedCert(decode, der, certSz, 0);
  13927. ret = ParseCert(decode, CERT_TYPE, NO_VERIFY, 0);
  13928. if (ret != 0) {
  13929. FreeDecodedCert(decode);
  13930. ERROR_OUT(-7865, exit_rsa);
  13931. }
  13932. FreeDecodedCert(decode);
  13933. #endif
  13934. ret = SaveDerAndPem(der, certSz, certEccRsaDerFile, certEccRsaPemFile,
  13935. CERT_TYPE, -5616);
  13936. if (ret != 0) {
  13937. goto exit_rsa;
  13938. }
  13939. exit_rsa:
  13940. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  13941. if (caKey != NULL) {
  13942. wc_FreeRsaKey(caKey);
  13943. XFREE(caKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13944. }
  13945. if (caEccKey != NULL) {
  13946. wc_ecc_free(caEccKey);
  13947. XFREE(caEccKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13948. }
  13949. if (caEccKeyPub != NULL) {
  13950. wc_ecc_free(caEccKeyPub);
  13951. XFREE(caEccKeyPub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13952. }
  13953. #ifdef WOLFSSL_TEST_CERT
  13954. if (decode != NULL)
  13955. XFREE(decode, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13956. #endif
  13957. #else
  13958. wc_FreeRsaKey(caKey);
  13959. wc_ecc_free(caEccKey);
  13960. wc_ecc_free(caEccKeyPub);
  13961. #endif
  13962. XFREE(myCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13963. myCert = NULL;
  13964. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13965. der = NULL;
  13966. if (ret >= 0)
  13967. ret = 0;
  13968. return ret;
  13969. }
  13970. #endif /* !NO_RSA && HAVE_ECC && WOLFSSL_CERT_GEN */
  13971. #ifdef WOLFSSL_KEY_GEN
  13972. static int rsa_keygen_test(WC_RNG* rng)
  13973. {
  13974. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  13975. RsaKey *genKey = (RsaKey *)XMALLOC(sizeof *genKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13976. #else
  13977. RsaKey genKey[1];
  13978. #endif
  13979. int ret;
  13980. byte* der = NULL;
  13981. #ifndef WOLFSSL_CRYPTOCELL
  13982. word32 idx = 0;
  13983. #endif
  13984. int derSz = 0;
  13985. #if !defined(WOLFSSL_SP_MATH) && !defined(HAVE_FIPS)
  13986. int keySz = 1024;
  13987. #else
  13988. int keySz = 2048;
  13989. #endif
  13990. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  13991. if (! genKey)
  13992. ERROR_OUT(MEMORY_E, exit_rsa);
  13993. #endif
  13994. XMEMSET(genKey, 0, sizeof *genKey);
  13995. ret = wc_InitRsaKey_ex(genKey, HEAP_HINT, devId);
  13996. if (ret != 0) {
  13997. ERROR_OUT(-7870, exit_rsa);
  13998. }
  13999. #ifdef HAVE_FIPS
  14000. for (;;) {
  14001. #endif
  14002. ret = wc_MakeRsaKey(genKey, keySz, WC_RSA_EXPONENT, rng);
  14003. #if defined(WOLFSSL_ASYNC_CRYPT)
  14004. ret = wc_AsyncWait(ret, &genKey->asyncDev, WC_ASYNC_FLAG_NONE);
  14005. #endif
  14006. #ifdef HAVE_FIPS
  14007. if (ret == PRIME_GEN_E)
  14008. continue;
  14009. break;
  14010. }
  14011. #endif
  14012. if (ret != 0) {
  14013. ERROR_OUT(-7871, exit_rsa);
  14014. }
  14015. TEST_SLEEP();
  14016. #ifdef WOLFSSL_RSA_KEY_CHECK
  14017. ret = wc_CheckRsaKey(genKey);
  14018. if (ret != 0) {
  14019. ERROR_OUT(-7872, exit_rsa);
  14020. }
  14021. #endif
  14022. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14023. if (der == NULL) {
  14024. ERROR_OUT(-7873, exit_rsa);
  14025. }
  14026. derSz = wc_RsaKeyToDer(genKey, der, FOURK_BUF);
  14027. if (derSz < 0) {
  14028. ERROR_OUT(-7874, exit_rsa);
  14029. }
  14030. ret = SaveDerAndPem(der, derSz, keyDerFile, keyPemFile,
  14031. PRIVATEKEY_TYPE, -5555);
  14032. if (ret != 0) {
  14033. goto exit_rsa;
  14034. }
  14035. wc_FreeRsaKey(genKey);
  14036. ret = wc_InitRsaKey(genKey, HEAP_HINT);
  14037. if (ret != 0) {
  14038. ERROR_OUT(-7875, exit_rsa);
  14039. }
  14040. #ifndef WOLFSSL_CRYPTOCELL
  14041. idx = 0;
  14042. /* The private key part of the key gen pairs from cryptocell can't be exported */
  14043. ret = wc_RsaPrivateKeyDecode(der, &idx, genKey, derSz);
  14044. if (ret != 0) {
  14045. ERROR_OUT(-7876, exit_rsa);
  14046. }
  14047. #endif /* WOLFSSL_CRYPTOCELL */
  14048. exit_rsa:
  14049. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  14050. if (genKey) {
  14051. wc_FreeRsaKey(genKey);
  14052. XFREE(genKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14053. }
  14054. #else
  14055. wc_FreeRsaKey(genKey);
  14056. #endif
  14057. if (der != NULL) {
  14058. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14059. der = NULL;
  14060. }
  14061. return ret;
  14062. }
  14063. #endif
  14064. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  14065. #if !defined(WC_NO_RSA_OAEP) && !defined(WC_NO_RNG) && \
  14066. !defined(HAVE_FAST_RSA) && !defined(HAVE_USER_RSA) && \
  14067. (!defined(HAVE_FIPS) || \
  14068. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))) \
  14069. && !defined(WOLF_CRYPTO_CB_ONLY_RSA)
  14070. static int rsa_oaep_padding_test(RsaKey* key, WC_RNG* rng)
  14071. {
  14072. int ret = 0;
  14073. word32 idx = 0;
  14074. const char inStr[] = TEST_STRING;
  14075. const word32 inLen = (word32)TEST_STRING_SZ;
  14076. const word32 outSz = RSA_TEST_BYTES;
  14077. const word32 plainSz = RSA_TEST_BYTES;
  14078. byte* res = NULL;
  14079. WC_DECLARE_VAR(in, byte, TEST_STRING_SZ, HEAP_HINT);
  14080. WC_DECLARE_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
  14081. WC_DECLARE_VAR(plain, byte, RSA_TEST_BYTES, HEAP_HINT);
  14082. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  14083. if (in == NULL || out == NULL || plain == NULL)
  14084. ERROR_OUT(MEMORY_E, exit_rsa);
  14085. #endif
  14086. XMEMCPY(in, inStr, inLen);
  14087. #ifndef NO_SHA
  14088. do {
  14089. #if defined(WOLFSSL_ASYNC_CRYPT)
  14090. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14091. #endif
  14092. if (ret >= 0) {
  14093. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, rng,
  14094. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA, WC_MGF1SHA1, NULL, 0);
  14095. }
  14096. } while (ret == WC_PENDING_E);
  14097. if (ret < 0) {
  14098. ERROR_OUT(-7918, exit_rsa);
  14099. }
  14100. TEST_SLEEP();
  14101. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  14102. idx = (word32)ret;
  14103. do {
  14104. #if defined(WOLFSSL_ASYNC_CRYPT)
  14105. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14106. #endif
  14107. if (ret >= 0) {
  14108. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  14109. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA, WC_MGF1SHA1, NULL, 0);
  14110. }
  14111. } while (ret == WC_PENDING_E);
  14112. if (ret < 0) {
  14113. ERROR_OUT(-7919, exit_rsa);
  14114. }
  14115. if (XMEMCMP(plain, in, inLen)) {
  14116. ERROR_OUT(-7920, exit_rsa);
  14117. }
  14118. TEST_SLEEP();
  14119. #endif /* NO_SHA */
  14120. #endif
  14121. #ifndef NO_SHA256
  14122. XMEMSET(plain, 0, plainSz);
  14123. do {
  14124. #if defined(WOLFSSL_ASYNC_CRYPT)
  14125. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14126. #endif
  14127. if (ret >= 0) {
  14128. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, rng,
  14129. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0);
  14130. }
  14131. } while (ret == WC_PENDING_E);
  14132. if (ret < 0) {
  14133. ERROR_OUT(-7921, exit_rsa);
  14134. }
  14135. TEST_SLEEP();
  14136. idx = (word32)ret;
  14137. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  14138. do {
  14139. #if defined(WOLFSSL_ASYNC_CRYPT)
  14140. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14141. #endif
  14142. if (ret >= 0) {
  14143. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  14144. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0);
  14145. }
  14146. } while (ret == WC_PENDING_E);
  14147. if (ret < 0) {
  14148. ERROR_OUT(-7922, exit_rsa);
  14149. }
  14150. if (XMEMCMP(plain, in, inLen)) {
  14151. ERROR_OUT(-7923, exit_rsa);
  14152. }
  14153. TEST_SLEEP();
  14154. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  14155. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  14156. do {
  14157. #if defined(WOLFSSL_ASYNC_CRYPT)
  14158. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14159. #endif
  14160. if (ret >= 0) {
  14161. ret = wc_RsaPrivateDecryptInline_ex(out, idx, &res, key,
  14162. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0);
  14163. }
  14164. } while (ret == WC_PENDING_E);
  14165. if (ret < 0) {
  14166. ERROR_OUT(-7924, exit_rsa);
  14167. }
  14168. if (ret != (int)inLen) {
  14169. ERROR_OUT(-7925, exit_rsa);
  14170. }
  14171. if (XMEMCMP(res, in, inLen)) {
  14172. ERROR_OUT(-7926, exit_rsa);
  14173. }
  14174. TEST_SLEEP();
  14175. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  14176. /* check fails if not using the same optional label */
  14177. XMEMSET(plain, 0, plainSz);
  14178. do {
  14179. #if defined(WOLFSSL_ASYNC_CRYPT)
  14180. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14181. #endif
  14182. if (ret >= 0) {
  14183. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, rng,
  14184. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0);
  14185. }
  14186. } while (ret == WC_PENDING_E);
  14187. if (ret < 0) {
  14188. ERROR_OUT(-7927, exit_rsa);
  14189. }
  14190. TEST_SLEEP();
  14191. /* TODO: investigate why Cavium Nitrox doesn't detect decrypt error here */
  14192. #if !defined(HAVE_CAVIUM) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
  14193. !defined(WOLFSSL_CRYPTOCELL) && !defined(WOLFSSL_SE050)
  14194. /* label is unused in cryptocell and SE050 so it won't detect decrypt error
  14195. * due to label */
  14196. idx = (word32)ret;
  14197. do {
  14198. #if defined(WOLFSSL_ASYNC_CRYPT)
  14199. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14200. #endif
  14201. if (ret >= 0) {
  14202. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  14203. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, in, inLen);
  14204. }
  14205. } while (ret == WC_PENDING_E);
  14206. if (ret > 0) { /* in this case decrypt should fail */
  14207. ERROR_OUT(-7928, exit_rsa);
  14208. }
  14209. ret = 0;
  14210. TEST_SLEEP();
  14211. #endif /* !HAVE_CAVIUM */
  14212. /* check using optional label with encrypt/decrypt */
  14213. XMEMSET(plain, 0, plainSz);
  14214. do {
  14215. #if defined(WOLFSSL_ASYNC_CRYPT)
  14216. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14217. #endif
  14218. if (ret >= 0) {
  14219. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, rng,
  14220. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, in, inLen);
  14221. }
  14222. } while (ret == WC_PENDING_E);
  14223. if (ret < 0) {
  14224. ERROR_OUT(-7929, exit_rsa);
  14225. }
  14226. TEST_SLEEP();
  14227. idx = (word32)ret;
  14228. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  14229. do {
  14230. #if defined(WOLFSSL_ASYNC_CRYPT)
  14231. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14232. #endif
  14233. if (ret >= 0) {
  14234. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  14235. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, in, inLen);
  14236. }
  14237. } while (ret == WC_PENDING_E);
  14238. if (ret < 0) {
  14239. ERROR_OUT(-7930, exit_rsa);
  14240. }
  14241. if (XMEMCMP(plain, in, inLen)) {
  14242. ERROR_OUT(-7931, exit_rsa);
  14243. }
  14244. TEST_SLEEP();
  14245. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  14246. #ifndef NO_SHA
  14247. /* check fail using mismatch hash algorithms */
  14248. XMEMSET(plain, 0, plainSz);
  14249. do {
  14250. #if defined(WOLFSSL_ASYNC_CRYPT)
  14251. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14252. #endif
  14253. if (ret >= 0) {
  14254. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, rng,
  14255. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA, WC_MGF1SHA1, in, inLen);
  14256. }
  14257. } while (ret == WC_PENDING_E);
  14258. if (ret < 0) {
  14259. ERROR_OUT(-7932, exit_rsa);
  14260. }
  14261. TEST_SLEEP();
  14262. /* TODO: investigate why Cavium Nitrox doesn't detect decrypt error here */
  14263. #if !defined(HAVE_CAVIUM) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
  14264. !defined(WOLFSSL_CRYPTOCELL) && !defined(WOLFSSL_SE050)
  14265. idx = (word32)ret;
  14266. do {
  14267. #if defined(WOLFSSL_ASYNC_CRYPT)
  14268. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14269. #endif
  14270. if (ret >= 0) {
  14271. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  14272. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256,
  14273. in, inLen);
  14274. }
  14275. } while (ret == WC_PENDING_E);
  14276. if (ret > 0) { /* should fail */
  14277. ERROR_OUT(-7933, exit_rsa);
  14278. }
  14279. ret = 0;
  14280. TEST_SLEEP();
  14281. #endif /* !HAVE_CAVIUM */
  14282. #endif /* NO_SHA */
  14283. #endif /* NO_SHA256 */
  14284. #ifdef WOLFSSL_SHA512
  14285. /* Check valid RSA key size is used while using hash length of SHA512
  14286. If key size is less than (hash length * 2) + 2 then is invalid use
  14287. and test, since OAEP padding requires this.
  14288. BAD_FUNC_ARG is returned when this case is not met */
  14289. if (wc_RsaEncryptSize(key) > ((int)WC_SHA512_DIGEST_SIZE * 2) + 2) {
  14290. XMEMSET(plain, 0, plainSz);
  14291. do {
  14292. #if defined(WOLFSSL_ASYNC_CRYPT)
  14293. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14294. #endif
  14295. if (ret >= 0) {
  14296. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, rng,
  14297. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA512, WC_MGF1SHA512, NULL, 0);
  14298. }
  14299. } while (ret == WC_PENDING_E);
  14300. if (ret < 0) {
  14301. ERROR_OUT(-7934, exit_rsa);
  14302. }
  14303. TEST_SLEEP();
  14304. idx = ret;
  14305. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  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_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  14312. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA512, WC_MGF1SHA512, NULL, 0);
  14313. }
  14314. } while (ret == WC_PENDING_E);
  14315. if (ret < 0) {
  14316. ERROR_OUT(-7935, exit_rsa);
  14317. }
  14318. if (XMEMCMP(plain, in, inLen)) {
  14319. ERROR_OUT(-7936, exit_rsa);
  14320. }
  14321. TEST_SLEEP();
  14322. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  14323. }
  14324. #endif /* WOLFSSL_SHA512 */
  14325. /* check using pkcsv15 padding with _ex API */
  14326. XMEMSET(plain, 0, plainSz);
  14327. do {
  14328. #if defined(WOLFSSL_ASYNC_CRYPT)
  14329. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14330. #endif
  14331. if (ret >= 0) {
  14332. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, rng,
  14333. WC_RSA_PKCSV15_PAD, WC_HASH_TYPE_NONE, 0, NULL, 0);
  14334. }
  14335. } while (ret == WC_PENDING_E);
  14336. if (ret < 0) {
  14337. ERROR_OUT(-7937, exit_rsa);
  14338. }
  14339. TEST_SLEEP();
  14340. idx = (word32)ret;
  14341. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  14342. do {
  14343. #if defined(WOLFSSL_ASYNC_CRYPT)
  14344. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14345. #endif
  14346. if (ret >= 0) {
  14347. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  14348. WC_RSA_PKCSV15_PAD, WC_HASH_TYPE_NONE, 0, NULL, 0);
  14349. }
  14350. } while (ret == WC_PENDING_E);
  14351. if (ret < 0) {
  14352. ERROR_OUT(-7938, exit_rsa);
  14353. }
  14354. if (XMEMCMP(plain, in, inLen)) {
  14355. ERROR_OUT(-7939, exit_rsa);
  14356. }
  14357. TEST_SLEEP();
  14358. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  14359. exit_rsa:
  14360. WC_FREE_VAR(in, HEAP_HINT);
  14361. WC_FREE_VAR(out, HEAP_HINT);
  14362. WC_FREE_VAR(plain, HEAP_HINT);
  14363. (void)idx;
  14364. (void)inStr;
  14365. (void)res;
  14366. if (ret >= 0)
  14367. ret = 0;
  14368. return ret;
  14369. }
  14370. #endif
  14371. #endif
  14372. WOLFSSL_TEST_SUBROUTINE int rsa_test(void)
  14373. {
  14374. int ret;
  14375. size_t bytes;
  14376. WC_RNG rng;
  14377. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  14378. byte* tmp = NULL;
  14379. byte* der = NULL;
  14380. RsaKey *key = (RsaKey *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14381. #else
  14382. RsaKey key[1];
  14383. byte tmp[FOURK_BUF];
  14384. #endif
  14385. #if defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_CERT_GEN)
  14386. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  14387. RsaKey *keypub = (RsaKey *)XMALLOC(sizeof *keypub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14388. #else
  14389. RsaKey keypub[1];
  14390. #endif
  14391. #endif
  14392. word32 idx = 0;
  14393. const char inStr[] = TEST_STRING;
  14394. const word32 inLen = (word32)TEST_STRING_SZ;
  14395. const word32 outSz = RSA_TEST_BYTES;
  14396. const word32 plainSz = RSA_TEST_BYTES;
  14397. byte* res = NULL;
  14398. #ifndef NO_SIG_WRAPPER
  14399. int modLen;
  14400. #endif
  14401. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  14402. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096) && \
  14403. !defined(NO_FILESYSTEM)
  14404. XFILE file;
  14405. #ifdef WOLFSSL_TEST_CERT
  14406. XFILE file2;
  14407. #endif
  14408. #endif
  14409. #ifdef WOLFSSL_TEST_CERT
  14410. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  14411. DecodedCert *cert = (DecodedCert *)XMALLOC(sizeof *cert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14412. #else
  14413. DecodedCert cert[1];
  14414. #endif
  14415. #ifndef NO_ASN_TIME
  14416. struct tm timearg;
  14417. const byte* date;
  14418. byte dateFormat;
  14419. int dateLength;
  14420. #endif
  14421. #endif
  14422. WC_DECLARE_VAR(in, byte, TEST_STRING_SZ, HEAP_HINT);
  14423. WC_DECLARE_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
  14424. WC_DECLARE_VAR(plain, byte, RSA_TEST_BYTES, HEAP_HINT);
  14425. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  14426. if (in == NULL || out == NULL || plain == NULL)
  14427. ERROR_OUT(MEMORY_E, exit_rsa);
  14428. #endif
  14429. XMEMCPY(in, inStr, inLen);
  14430. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  14431. if (key == NULL)
  14432. ERROR_OUT(MEMORY_E, exit_rsa);
  14433. #if defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_CERT_GEN)
  14434. if (keypub == NULL)
  14435. ERROR_OUT(MEMORY_E, exit_rsa);
  14436. #endif
  14437. #ifdef WOLFSSL_TEST_CERT
  14438. if (cert == NULL)
  14439. ERROR_OUT(MEMORY_E, exit_rsa);
  14440. #endif
  14441. #endif /* WOLFSSL_SMALL_STACK && !WOLFSSL_NO_MALLOC */
  14442. /* initialize stack structures */
  14443. XMEMSET(&rng, 0, sizeof(rng));
  14444. XMEMSET(key, 0, sizeof *key);
  14445. #if defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_CERT_GEN)
  14446. XMEMSET(keypub, 0, sizeof *keypub);
  14447. #endif
  14448. #if !defined(HAVE_USER_RSA) && !defined(NO_ASN)
  14449. ret = rsa_decode_test(key);
  14450. if (ret != 0)
  14451. ERROR_OUT(ret, exit_rsa);
  14452. #endif
  14453. #ifdef USE_CERT_BUFFERS_1024
  14454. bytes = (size_t)sizeof_client_key_der_1024;
  14455. if (bytes < (size_t)sizeof_client_cert_der_1024)
  14456. bytes = (size_t)sizeof_client_cert_der_1024;
  14457. #elif defined(USE_CERT_BUFFERS_2048)
  14458. bytes = (size_t)sizeof_client_key_der_2048;
  14459. if (bytes < (size_t)sizeof_client_cert_der_2048)
  14460. bytes = (size_t)sizeof_client_cert_der_2048;
  14461. #elif defined(USE_CERT_BUFFERS_3072)
  14462. bytes = (size_t)sizeof_client_key_der_3072;
  14463. if (bytes < (size_t)sizeof_client_cert_der_3072)
  14464. bytes = (size_t)sizeof_client_cert_der_3072;
  14465. #elif defined(USE_CERT_BUFFERS_4096)
  14466. bytes = (size_t)sizeof_client_key_der_4096;
  14467. if (bytes < (size_t)sizeof_client_cert_der_4096)
  14468. bytes = (size_t)sizeof_client_cert_der_4096;
  14469. #else
  14470. bytes = FOURK_BUF;
  14471. #endif
  14472. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  14473. tmp = (byte*)XMALLOC(bytes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14474. if (tmp == NULL)
  14475. ERROR_OUT(-7900, exit_rsa);
  14476. #endif
  14477. #ifdef USE_CERT_BUFFERS_1024
  14478. XMEMCPY(tmp, client_key_der_1024, (size_t)sizeof_client_key_der_1024);
  14479. #elif defined(USE_CERT_BUFFERS_2048)
  14480. XMEMCPY(tmp, client_key_der_2048, (size_t)sizeof_client_key_der_2048);
  14481. #elif defined(USE_CERT_BUFFERS_3072)
  14482. XMEMCPY(tmp, client_key_der_3072, (size_t)sizeof_client_key_der_3072);
  14483. #elif defined(USE_CERT_BUFFERS_4096)
  14484. XMEMCPY(tmp, client_key_der_4096, (size_t)sizeof_client_key_der_4096);
  14485. #elif !defined(NO_FILESYSTEM)
  14486. file = XFOPEN(clientKey, "rb");
  14487. if (!file) {
  14488. err_sys("can't open ./certs/client-key.der, "
  14489. "Please run from wolfSSL home dir", -40);
  14490. ERROR_OUT(-7901, exit_rsa);
  14491. }
  14492. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  14493. XFCLOSE(file);
  14494. #else
  14495. /* No key to use. */
  14496. ERROR_OUT(-7902, exit_rsa);
  14497. #endif /* USE_CERT_BUFFERS */
  14498. ret = wc_InitRsaKey_ex(key, HEAP_HINT, devId);
  14499. if (ret != 0) {
  14500. ERROR_OUT(-7903, exit_rsa);
  14501. }
  14502. #ifndef NO_ASN
  14503. ret = wc_RsaPrivateKeyDecode(tmp, &idx, key, (word32)bytes);
  14504. if (ret != 0) {
  14505. ERROR_OUT(-7904, exit_rsa);
  14506. }
  14507. #ifndef NO_SIG_WRAPPER
  14508. modLen = wc_RsaEncryptSize(key);
  14509. #endif
  14510. #else
  14511. #ifdef USE_CERT_BUFFERS_2048
  14512. ret = mp_read_unsigned_bin(&key->n, &tmp[12], 256);
  14513. if (ret != 0) {
  14514. ERROR_OUT(-7905, exit_rsa);
  14515. }
  14516. ret = mp_set_int(&key->e, WC_RSA_EXPONENT);
  14517. if (ret != 0) {
  14518. ERROR_OUT(-7906, exit_rsa);
  14519. }
  14520. #ifndef NO_SIG_WRAPPER
  14521. modLen = 2048;
  14522. #endif
  14523. #else
  14524. #error Not supported yet!
  14525. #endif
  14526. #endif
  14527. #ifndef WC_NO_RNG
  14528. #ifndef HAVE_FIPS
  14529. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  14530. #else
  14531. ret = wc_InitRng(&rng);
  14532. #endif
  14533. if (ret != 0) {
  14534. ERROR_OUT(-7907, exit_rsa);
  14535. }
  14536. #endif
  14537. #ifndef NO_SIG_WRAPPER
  14538. ret = rsa_sig_test(key, sizeof *key, modLen, &rng);
  14539. if (ret != 0)
  14540. goto exit_rsa;
  14541. #endif
  14542. #ifdef WC_RSA_NONBLOCK
  14543. ret = rsa_nb_test(key, in, inLen, out, outSz, plain, plainSz, &rng);
  14544. if (ret != 0)
  14545. goto exit_rsa;
  14546. #endif
  14547. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
  14548. !defined(WC_NO_RNG) && !defined(WOLF_CRYPTO_CB_ONLY_RSA)
  14549. do {
  14550. #if defined(WOLFSSL_ASYNC_CRYPT)
  14551. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14552. #endif
  14553. if (ret >= 0) {
  14554. ret = wc_RsaPublicEncrypt(in, inLen, out, outSz, key, &rng);
  14555. }
  14556. } while (ret == WC_PENDING_E);
  14557. if (ret < 0) {
  14558. ERROR_OUT(-7908, exit_rsa);
  14559. }
  14560. TEST_SLEEP();
  14561. #ifdef WC_RSA_BLINDING
  14562. {
  14563. int tmpret = ret;
  14564. ret = wc_RsaSetRNG(key, &rng);
  14565. if (ret < 0) {
  14566. ERROR_OUT(-7909, exit_rsa);
  14567. }
  14568. ret = tmpret;
  14569. }
  14570. #endif
  14571. idx = (word32)ret; /* save off encrypted length */
  14572. do {
  14573. #if defined(WOLFSSL_ASYNC_CRYPT)
  14574. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14575. #endif
  14576. if (ret >= 0) {
  14577. ret = wc_RsaPrivateDecrypt(out, idx, plain, plainSz, key);
  14578. }
  14579. } while (ret == WC_PENDING_E);
  14580. if (ret < 0) {
  14581. ERROR_OUT(-7910, exit_rsa);
  14582. }
  14583. if (XMEMCMP(plain, in, inLen)) {
  14584. ERROR_OUT(-7911, exit_rsa);
  14585. }
  14586. TEST_SLEEP();
  14587. do {
  14588. #if defined(WOLFSSL_ASYNC_CRYPT)
  14589. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14590. #endif
  14591. if (ret >= 0) {
  14592. ret = wc_RsaPrivateDecryptInline(out, idx, &res, key);
  14593. }
  14594. } while (ret == WC_PENDING_E);
  14595. if (ret < 0) {
  14596. ERROR_OUT(-7912, exit_rsa);
  14597. }
  14598. if (ret != (int)inLen) {
  14599. ERROR_OUT(-7913, exit_rsa);
  14600. }
  14601. if (XMEMCMP(res, in, inLen)) {
  14602. ERROR_OUT(-7914, exit_rsa);
  14603. }
  14604. TEST_SLEEP();
  14605. do {
  14606. #if defined(WOLFSSL_ASYNC_CRYPT)
  14607. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14608. #endif
  14609. if (ret >= 0) {
  14610. ret = wc_RsaSSL_Sign(in, inLen, out, outSz, key, &rng);
  14611. }
  14612. } while (ret == WC_PENDING_E);
  14613. if (ret < 0) {
  14614. ERROR_OUT(-7915, exit_rsa);
  14615. }
  14616. TEST_SLEEP();
  14617. #elif defined(WOLFSSL_PUBLIC_MP)
  14618. {
  14619. static byte signature_2048[] = {
  14620. 0x07, 0x6f, 0xc9, 0x85, 0x73, 0x9e, 0x21, 0x79,
  14621. 0x47, 0xf1, 0xa3, 0xd7, 0xf4, 0x27, 0x29, 0xbe,
  14622. 0x99, 0x5d, 0xac, 0xb2, 0x10, 0x3f, 0x95, 0xda,
  14623. 0x89, 0x23, 0xb8, 0x96, 0x13, 0x57, 0x72, 0x30,
  14624. 0xa1, 0xfe, 0x5a, 0x68, 0x9c, 0x99, 0x9d, 0x1e,
  14625. 0x05, 0xa4, 0x80, 0xb0, 0xbb, 0xd9, 0xd9, 0xa1,
  14626. 0x69, 0x97, 0x74, 0xb3, 0x41, 0x21, 0x3b, 0x47,
  14627. 0xf5, 0x51, 0xb1, 0xfb, 0xc7, 0xaa, 0xcc, 0xdc,
  14628. 0xcd, 0x76, 0xa0, 0x28, 0x4d, 0x27, 0x14, 0xa4,
  14629. 0xb9, 0x41, 0x68, 0x7c, 0xb3, 0x66, 0xe6, 0x6f,
  14630. 0x40, 0x76, 0xe4, 0x12, 0xfd, 0xae, 0x29, 0xb5,
  14631. 0x63, 0x60, 0x87, 0xce, 0x49, 0x6b, 0xf3, 0x05,
  14632. 0x9a, 0x14, 0xb5, 0xcc, 0xcd, 0xf7, 0x30, 0x95,
  14633. 0xd2, 0x72, 0x52, 0x1d, 0x5b, 0x7e, 0xef, 0x4a,
  14634. 0x02, 0x96, 0x21, 0x6c, 0x55, 0xa5, 0x15, 0xb1,
  14635. 0x57, 0x63, 0x2c, 0xa3, 0x8e, 0x9d, 0x3d, 0x45,
  14636. 0xcc, 0xb8, 0xe6, 0xa1, 0xc8, 0x59, 0xcd, 0xf5,
  14637. 0xdc, 0x0a, 0x51, 0xb6, 0x9d, 0xfb, 0xf4, 0x6b,
  14638. 0xfd, 0x32, 0x71, 0x6e, 0xcf, 0xcb, 0xb3, 0xd9,
  14639. 0xe0, 0x4a, 0x77, 0x34, 0xd6, 0x61, 0xf5, 0x7c,
  14640. 0xf9, 0xa9, 0xa4, 0xb0, 0x8e, 0x3b, 0xd6, 0x04,
  14641. 0xe0, 0xde, 0x2b, 0x5b, 0x5a, 0xbf, 0xd9, 0xef,
  14642. 0x8d, 0xa3, 0xf5, 0xb1, 0x67, 0xf3, 0xb9, 0x72,
  14643. 0x0a, 0x37, 0x12, 0x35, 0x6c, 0x8e, 0x10, 0x8b,
  14644. 0x38, 0x06, 0x16, 0x4b, 0x20, 0x20, 0x13, 0x00,
  14645. 0x2e, 0x6d, 0xc2, 0x59, 0x23, 0x67, 0x4a, 0x6d,
  14646. 0xa1, 0x46, 0x8b, 0xee, 0xcf, 0x44, 0xb4, 0x3e,
  14647. 0x56, 0x75, 0x00, 0x68, 0xb5, 0x7d, 0x0f, 0x20,
  14648. 0x79, 0x5d, 0x7f, 0x12, 0x15, 0x32, 0x89, 0x61,
  14649. 0x6b, 0x29, 0xb7, 0x52, 0xf5, 0x25, 0xd8, 0x98,
  14650. 0xe8, 0x6f, 0xf9, 0x22, 0xb4, 0xbb, 0xe5, 0xff,
  14651. 0xd0, 0x92, 0x86, 0x9a, 0x88, 0xa2, 0xaf, 0x6b
  14652. };
  14653. ret = sizeof(signature_2048);
  14654. XMEMCPY(out, signature_2048, ret);
  14655. }
  14656. #endif
  14657. #if !defined(WC_NO_RNG) && !defined(WC_NO_RSA_OAEP) && \
  14658. ((!defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_PUBLIC_ONLY)) || \
  14659. defined(WOLFSSL_PUBLIC_MP)) && !defined(WOLF_CRYPTO_CB_ONLY_RSA)
  14660. idx = (word32)ret;
  14661. XMEMSET(plain, 0, plainSz);
  14662. do {
  14663. #if defined(WOLFSSL_ASYNC_CRYPT)
  14664. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14665. #endif
  14666. if (ret >= 0) {
  14667. #ifndef WOLFSSL_RSA_VERIFY_INLINE
  14668. #if defined(WOLFSSL_CRYPTOCELL)
  14669. /*
  14670. Cryptocell requires the input data and signature byte array to verify.
  14671. first argument must be the input data
  14672. second argument must be the length of input data
  14673. third argument must be the signature byte array or the output from
  14674. wc_RsaSSL_Sign()
  14675. fourth argument must be the length of the signature byte array
  14676. */
  14677. ret = wc_RsaSSL_Verify(in, inLen, out, outSz, key);
  14678. #else
  14679. ret = wc_RsaSSL_Verify(out, idx, plain, plainSz, key);
  14680. #endif /* WOLFSSL_CRYPTOCELL */
  14681. #else
  14682. byte* dec = NULL;
  14683. ret = wc_RsaSSL_VerifyInline(out, idx, &dec, key);
  14684. if (ret > 0) {
  14685. XMEMCPY(plain, dec, ret);
  14686. }
  14687. #endif
  14688. }
  14689. } while (ret == WC_PENDING_E);
  14690. if (ret < 0) {
  14691. ERROR_OUT(-7916, exit_rsa);
  14692. }
  14693. if (XMEMCMP(plain, in, (size_t)ret)) {
  14694. ERROR_OUT(-7917, exit_rsa);
  14695. }
  14696. TEST_SLEEP();
  14697. #endif
  14698. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  14699. #if !defined(WC_NO_RSA_OAEP) && !defined(WC_NO_RNG)
  14700. #if !defined(HAVE_FAST_RSA) && !defined(HAVE_USER_RSA) && \
  14701. (!defined(HAVE_FIPS) || \
  14702. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))) \
  14703. && !defined(WOLF_CRYPTO_CB_ONLY_RSA)
  14704. ret = rsa_oaep_padding_test(key, &rng);
  14705. if (ret != 0)
  14706. return ret;
  14707. #endif /* !HAVE_FAST_RSA && !HAVE_FIPS */
  14708. #endif /* WC_NO_RSA_OAEP && !WC_NO_RNG */
  14709. #endif /* WOLFSSL_RSA_VERIFY_ONLY */
  14710. #if !defined(HAVE_FIPS) && !defined(HAVE_USER_RSA) && !defined(NO_ASN) \
  14711. && !defined(WOLFSSL_RSA_VERIFY_ONLY)
  14712. ret = rsa_export_key_test(key);
  14713. if (ret != 0)
  14714. return ret;
  14715. #endif
  14716. #if !defined(NO_ASN) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
  14717. !defined(WOLFSSL_RSA_VERIFY_ONLY)
  14718. ret = rsa_flatten_test(key);
  14719. if (ret != 0)
  14720. return ret;
  14721. #endif
  14722. #if !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_ASN) && \
  14723. !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  14724. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096)
  14725. (void)clientCert;
  14726. #endif
  14727. #ifdef WOLFSSL_TEST_CERT
  14728. #if defined(WOLFSSL_MDK_ARM)
  14729. #define sizeof(s) XSTRLEN((char *)(s))
  14730. #endif
  14731. #ifdef USE_CERT_BUFFERS_1024
  14732. XMEMCPY(tmp, client_cert_der_1024, (size_t)sizeof_client_cert_der_1024);
  14733. bytes = (size_t)sizeof_client_cert_der_1024;
  14734. #elif defined(USE_CERT_BUFFERS_2048)
  14735. XMEMCPY(tmp, client_cert_der_2048, (size_t)sizeof_client_cert_der_2048);
  14736. bytes = (size_t)sizeof_client_cert_der_2048;
  14737. #elif defined(USE_CERT_BUFFERS_3072)
  14738. XMEMCPY(tmp, client_cert_der_3072, (size_t)sizeof_client_cert_der_3072);
  14739. bytes = (size_t)sizeof_client_cert_der_3072;
  14740. #elif defined(USE_CERT_BUFFERS_4096)
  14741. XMEMCPY(tmp, client_cert_der_4096, (size_t)sizeof_client_cert_der_4096);
  14742. bytes = (size_t)sizeof_client_cert_der_4096;
  14743. #elif !defined(NO_FILESYSTEM)
  14744. file2 = XFOPEN(clientCert, "rb");
  14745. if (!file2) {
  14746. ERROR_OUT(-7940, exit_rsa);
  14747. }
  14748. bytes = XFREAD(tmp, 1, FOURK_BUF, file2);
  14749. XFCLOSE(file2);
  14750. #else
  14751. /* No certificate to use. */
  14752. ERROR_OUT(-7941, exit_rsa);
  14753. #endif
  14754. #ifdef sizeof
  14755. #undef sizeof
  14756. #endif
  14757. InitDecodedCert(cert, tmp, (word32)bytes, NULL);
  14758. ret = ParseCert(cert, CERT_TYPE, NO_VERIFY, NULL);
  14759. if (ret != 0) {
  14760. FreeDecodedCert(cert);
  14761. ERROR_OUT(-7942, exit_rsa);
  14762. }
  14763. #ifndef NO_ASN_TIME
  14764. ret = wc_GetDateInfo(cert->afterDate, cert->afterDateLen, &date,
  14765. &dateFormat, &dateLength);
  14766. if (ret != 0) {
  14767. FreeDecodedCert(cert);
  14768. ERROR_OUT(-7943, exit_rsa);
  14769. }
  14770. ret = wc_GetDateAsCalendarTime(date, dateLength, dateFormat, &timearg);
  14771. if (ret != 0) {
  14772. FreeDecodedCert(cert);
  14773. ERROR_OUT(-7944, exit_rsa);
  14774. }
  14775. #endif
  14776. FreeDecodedCert(cert);
  14777. #endif /* WOLFSSL_TEST_CERT */
  14778. #ifdef WOLFSSL_CERT_EXT
  14779. #ifdef USE_CERT_BUFFERS_1024
  14780. XMEMCPY(tmp, client_keypub_der_1024, sizeof_client_keypub_der_1024);
  14781. bytes = sizeof_client_keypub_der_1024;
  14782. #elif defined(USE_CERT_BUFFERS_2048)
  14783. XMEMCPY(tmp, client_keypub_der_2048, sizeof_client_keypub_der_2048);
  14784. bytes = sizeof_client_keypub_der_2048;
  14785. #elif defined(USE_CERT_BUFFERS_3072)
  14786. XMEMCPY(tmp, client_keypub_der_3072, sizeof_client_keypub_der_3072);
  14787. bytes = sizeof_client_keypub_der_3072;
  14788. #elif defined(USE_CERT_BUFFERS_4096)
  14789. XMEMCPY(tmp, client_keypub_der_4096, sizeof_client_keypub_der_4096);
  14790. bytes = sizeof_client_keypub_der_4096;
  14791. #else
  14792. file = XFOPEN(clientKeyPub, "rb");
  14793. if (!file) {
  14794. err_sys("can't open ./certs/client-keyPub.der, "
  14795. "Please run from wolfSSL home dir", -40);
  14796. ERROR_OUT(-7945, exit_rsa);
  14797. }
  14798. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  14799. XFCLOSE(file);
  14800. #endif /* USE_CERT_BUFFERS */
  14801. ret = wc_InitRsaKey(keypub, HEAP_HINT);
  14802. if (ret != 0) {
  14803. ERROR_OUT(-7946, exit_rsa);
  14804. }
  14805. idx = 0;
  14806. ret = wc_RsaPublicKeyDecode(tmp, &idx, keypub, (word32)bytes);
  14807. if (ret != 0) {
  14808. ERROR_OUT(-7947, exit_rsa);
  14809. }
  14810. #endif /* WOLFSSL_CERT_EXT */
  14811. #ifdef WOLFSSL_KEY_GEN
  14812. ret = rsa_keygen_test(&rng);
  14813. if (ret != 0)
  14814. goto exit_rsa;
  14815. #endif
  14816. #ifdef WOLFSSL_CERT_GEN
  14817. /* Make Cert / Sign example for RSA cert and RSA CA */
  14818. ret = rsa_certgen_test(key, keypub, &rng, tmp);
  14819. if (ret != 0)
  14820. goto exit_rsa;
  14821. #if !defined(NO_RSA) && defined(HAVE_ECC)
  14822. ret = rsa_ecc_certgen_test(&rng, tmp);
  14823. if (ret != 0)
  14824. goto exit_rsa;
  14825. #endif
  14826. #if defined(WOLFSSL_CERT_REQ) && !defined(WOLFSSL_NO_MALLOC)
  14827. {
  14828. Cert *req;
  14829. int derSz;
  14830. #ifndef WOLFSSL_SMALL_STACK
  14831. byte* der = NULL;
  14832. #endif
  14833. req = (Cert *)XMALLOC(sizeof *req, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14834. if (! req)
  14835. ERROR_OUT(MEMORY_E, exit_rsa);
  14836. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,DYNAMIC_TYPE_TMP_BUFFER);
  14837. if (der == NULL) {
  14838. ERROR_OUT(-7964, exit_rsa);
  14839. }
  14840. if (wc_InitCert_ex(req, HEAP_HINT, devId)) {
  14841. ERROR_OUT(-7965, exit_rsa);
  14842. }
  14843. req->version = 0;
  14844. req->isCA = 1;
  14845. XSTRNCPY(req->challengePw, "wolf123", CTC_NAME_SIZE);
  14846. XMEMCPY(&req->subject, &certDefaultName, sizeof(CertName));
  14847. #ifndef NO_SHA256
  14848. req->sigType = CTC_SHA256wRSA;
  14849. #else
  14850. req->sigType = CTC_SHAwRSA;
  14851. #endif
  14852. #ifdef WOLFSSL_CERT_EXT
  14853. /* add SKID from the Public Key */
  14854. if (wc_SetSubjectKeyIdFromPublicKey(req, keypub, NULL) != 0) {
  14855. ERROR_OUT(-7966, exit_rsa);
  14856. }
  14857. /* add Key Usage */
  14858. if (wc_SetKeyUsage(req, certKeyUsage2) != 0) {
  14859. ERROR_OUT(-7967, exit_rsa);
  14860. }
  14861. /* add Extended Key Usage */
  14862. if (wc_SetExtKeyUsage(req, "serverAuth,clientAuth,codeSigning,"
  14863. "emailProtection,timeStamping,OCSPSigning") != 0) {
  14864. ERROR_OUT(-7968, exit_rsa);
  14865. }
  14866. #ifdef WOLFSSL_EKU_OID
  14867. {
  14868. WOLFSSL_SMALL_STACK_STATIC const char unique[] = "2.16.840.1.111111.100.1.10.1";
  14869. if (wc_SetExtKeyUsageOID(req, unique, sizeof(unique), 0,
  14870. HEAP_HINT) != 0) {
  14871. ERROR_OUT(-7969, exit_rsa);
  14872. }
  14873. }
  14874. #endif /* WOLFSSL_EKU_OID */
  14875. #endif /* WOLFSSL_CERT_EXT */
  14876. derSz = wc_MakeCertReq(req, der, FOURK_BUF, key, NULL);
  14877. if (derSz < 0) {
  14878. ERROR_OUT(-7970, exit_rsa);
  14879. }
  14880. #ifdef WOLFSSL_CERT_EXT
  14881. /* Try again with "any" flag set, will override all others */
  14882. if (wc_SetExtKeyUsage(req, "any") != 0) {
  14883. ERROR_OUT(-7971, exit_rsa);
  14884. }
  14885. derSz = wc_MakeCertReq(req, der, FOURK_BUF, key, NULL);
  14886. if (derSz < 0) {
  14887. ERROR_OUT(-7972, exit_rsa);
  14888. }
  14889. #endif /* WOLFSSL_CERT_EXT */
  14890. ret = 0;
  14891. do {
  14892. #if defined(WOLFSSL_ASYNC_CRYPT)
  14893. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14894. #endif
  14895. if (ret >= 0) {
  14896. ret = wc_SignCert(req->bodySz, req->sigType, der, FOURK_BUF,
  14897. key, NULL, &rng);
  14898. }
  14899. } while (ret == WC_PENDING_E);
  14900. if (ret < 0) {
  14901. ERROR_OUT(-7973, exit_rsa);
  14902. }
  14903. derSz = ret;
  14904. ret = SaveDerAndPem(der, derSz, certReqDerFile, certReqPemFile,
  14905. CERTREQ_TYPE, -5650);
  14906. if (ret != 0) {
  14907. goto exit_rsa;
  14908. }
  14909. derSz = wc_MakeCertReq_ex(req, der, FOURK_BUF, RSA_TYPE, key);
  14910. if (derSz < 0) {
  14911. ERROR_OUT(-7974, exit_rsa);
  14912. }
  14913. /* Test getting the size of the buffer without providing the buffer.
  14914. * derSz is set to the "largest buffer" we are willing to allocate. */
  14915. derSz = wc_MakeCertReq(req, NULL, 10000, key, NULL);
  14916. if (derSz < 0) {
  14917. ERROR_OUT(-7975, exit_rsa);
  14918. }
  14919. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14920. XFREE(req, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14921. der = NULL;
  14922. }
  14923. #endif /* WOLFSSL_CERT_REQ */
  14924. #endif /* WOLFSSL_CERT_GEN */
  14925. #if defined(WC_RSA_PSS) && !defined(HAVE_FIPS_VERSION) /* not supported with FIPSv1 */
  14926. /* Need to create known good signatures to test with this. */
  14927. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
  14928. !defined(WOLF_CRYPTO_CB_ONLY_RSA)
  14929. ret = rsa_pss_test(&rng, key);
  14930. if (ret != 0)
  14931. goto exit_rsa;
  14932. #endif
  14933. #endif
  14934. #if defined(WOLFSSL_HAVE_SP_RSA) && defined(USE_FAST_MATH)
  14935. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  14936. /* New key to be loaded in rsa_even_mod_test(). */
  14937. if (key != NULL)
  14938. #endif
  14939. wc_FreeRsaKey(key);
  14940. /* New key to be loaded in rsa_even_mod_test(). */
  14941. ret = rsa_even_mod_test(&rng, key);
  14942. #endif
  14943. exit_rsa:
  14944. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  14945. if (key != NULL) {
  14946. wc_FreeRsaKey(key);
  14947. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14948. }
  14949. #if defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_CERT_GEN)
  14950. if (keypub != NULL) {
  14951. wc_FreeRsaKey(keypub);
  14952. XFREE(keypub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14953. }
  14954. #endif
  14955. #ifdef WOLFSSL_TEST_CERT
  14956. if (cert != NULL)
  14957. XFREE(cert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14958. #endif
  14959. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14960. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14961. #else
  14962. wc_FreeRsaKey(key);
  14963. #if defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_CERT_GEN)
  14964. wc_FreeRsaKey(keypub);
  14965. #endif
  14966. #endif /* WOLFSSL_SMALL_STACK && !WOLFSSL_NO_MALLOC */
  14967. wc_FreeRng(&rng);
  14968. WC_FREE_VAR(in, HEAP_HINT);
  14969. WC_FREE_VAR(out, HEAP_HINT);
  14970. WC_FREE_VAR(plain, HEAP_HINT);
  14971. (void)res;
  14972. (void)bytes;
  14973. (void)idx;
  14974. (void)in;
  14975. (void)out;
  14976. (void)plain;
  14977. (void)idx;
  14978. (void)inStr;
  14979. (void)inLen;
  14980. (void)outSz;
  14981. (void)plainSz;
  14982. /* ret can be greater then 0 with certgen but all negative values should
  14983. * be returned and treated as an error */
  14984. if (ret >= 0) {
  14985. return 0;
  14986. }
  14987. else {
  14988. return ret;
  14989. }
  14990. }
  14991. #endif /* !NO_RSA */
  14992. #ifndef NO_DH
  14993. static int dh_fips_generate_test(WC_RNG *rng)
  14994. {
  14995. int ret = 0;
  14996. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  14997. DhKey *key = (DhKey *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14998. #else
  14999. DhKey key[1];
  15000. #endif
  15001. WOLFSSL_SMALL_STACK_STATIC const byte p[] = {
  15002. 0xc5, 0x7c, 0xa2, 0x4f, 0x4b, 0xd6, 0x8c, 0x3c,
  15003. 0xda, 0xc7, 0xba, 0xaa, 0xea, 0x2e, 0x5c, 0x1e,
  15004. 0x18, 0xb2, 0x7b, 0x8c, 0x55, 0x65, 0x9f, 0xea,
  15005. 0xe0, 0xa1, 0x36, 0x53, 0x2b, 0x36, 0xe0, 0x4e,
  15006. 0x3e, 0x64, 0xa9, 0xe4, 0xfc, 0x8f, 0x32, 0x62,
  15007. 0x97, 0xe4, 0xbe, 0xf7, 0xc1, 0xde, 0x07, 0x5a,
  15008. 0x89, 0x28, 0xf3, 0xfe, 0x4f, 0xfe, 0x68, 0xbc,
  15009. 0xfb, 0x0a, 0x7c, 0xa4, 0xb3, 0x14, 0x48, 0x89,
  15010. 0x9f, 0xaf, 0xb8, 0x43, 0xe2, 0xa0, 0x62, 0x5c,
  15011. 0xb4, 0x88, 0x3f, 0x06, 0x50, 0x11, 0xfe, 0x65,
  15012. 0x8d, 0x49, 0xd2, 0xf5, 0x4b, 0x74, 0x79, 0xdb,
  15013. 0x06, 0x62, 0x92, 0x89, 0xed, 0xda, 0xcb, 0x87,
  15014. 0x37, 0x16, 0xd2, 0xa1, 0x7a, 0xe8, 0xde, 0x92,
  15015. 0xee, 0x3e, 0x41, 0x4a, 0x91, 0x5e, 0xed, 0xf3,
  15016. 0x6c, 0x6b, 0x7e, 0xfd, 0x15, 0x92, 0x18, 0xfc,
  15017. 0xa7, 0xac, 0x42, 0x85, 0x57, 0xe9, 0xdc, 0xda,
  15018. 0x55, 0xc9, 0x8b, 0x28, 0x9e, 0xc1, 0xc4, 0x46,
  15019. 0x4d, 0x88, 0xed, 0x62, 0x8e, 0xdb, 0x3f, 0xb9,
  15020. 0xd7, 0xc8, 0xe3, 0xcf, 0xb8, 0x34, 0x2c, 0xd2,
  15021. 0x6f, 0x28, 0x06, 0x41, 0xe3, 0x66, 0x8c, 0xfc,
  15022. 0x72, 0xff, 0x26, 0x3b, 0x6b, 0x6c, 0x6f, 0x73,
  15023. 0xde, 0xf2, 0x90, 0x29, 0xe0, 0x61, 0x32, 0xc4,
  15024. 0x12, 0x74, 0x09, 0x52, 0xec, 0xf3, 0x1b, 0xa6,
  15025. 0x45, 0x98, 0xac, 0xf9, 0x1c, 0x65, 0x8e, 0x3a,
  15026. 0x91, 0x84, 0x4b, 0x23, 0x8a, 0xb2, 0x3c, 0xc9,
  15027. 0xfa, 0xea, 0xf1, 0x38, 0xce, 0xd8, 0x05, 0xe0,
  15028. 0xfa, 0x44, 0x68, 0x1f, 0xeb, 0xd9, 0x57, 0xb8,
  15029. 0x4a, 0x97, 0x5b, 0x88, 0xc5, 0xf1, 0xbb, 0xb0,
  15030. 0x49, 0xc3, 0x91, 0x7c, 0xd3, 0x13, 0xb9, 0x47,
  15031. 0xbb, 0x91, 0x8f, 0xe5, 0x26, 0x07, 0xab, 0xa9,
  15032. 0xc5, 0xd0, 0x3d, 0x95, 0x41, 0x26, 0x92, 0x9d,
  15033. 0x13, 0x67, 0xf2, 0x7e, 0x11, 0x88, 0xdc, 0x2d
  15034. };
  15035. WOLFSSL_SMALL_STACK_STATIC const byte g[] = {
  15036. 0x4a, 0x1a, 0xf3, 0xa4, 0x92, 0xe9, 0xee, 0x74,
  15037. 0x6e, 0x57, 0xd5, 0x8c, 0x2c, 0x5b, 0x41, 0x41,
  15038. 0x5e, 0xd4, 0x55, 0x19, 0xdc, 0xd9, 0x32, 0x91,
  15039. 0xf7, 0xfd, 0xc2, 0x57, 0xff, 0x03, 0x14, 0xdb,
  15040. 0xf1, 0xb7, 0x60, 0x0c, 0x43, 0x59, 0x3f, 0xff,
  15041. 0xac, 0xf1, 0x80, 0x9a, 0x15, 0x6f, 0xd8, 0x6e,
  15042. 0xb7, 0x85, 0x18, 0xc8, 0xec, 0x4e, 0x59, 0x4a,
  15043. 0xe2, 0x91, 0x43, 0x4c, 0xeb, 0x95, 0xb6, 0x2e,
  15044. 0x9a, 0xea, 0x53, 0x68, 0x80, 0x64, 0x69, 0x40,
  15045. 0xf9, 0xec, 0xbd, 0x85, 0x89, 0x26, 0x97, 0x67,
  15046. 0xaf, 0xb0, 0xad, 0x00, 0x1b, 0xd4, 0xfd, 0x94,
  15047. 0xd3, 0xe9, 0x92, 0xb1, 0xb4, 0xbc, 0x5a, 0xaa,
  15048. 0x92, 0x80, 0x89, 0x3b, 0x39, 0x05, 0x6c, 0x22,
  15049. 0x26, 0xfe, 0x5a, 0x28, 0x6c, 0x37, 0x50, 0x5a,
  15050. 0x38, 0x99, 0xcf, 0xf3, 0xc1, 0x96, 0x45, 0xdc,
  15051. 0x01, 0xcb, 0x20, 0x87, 0xa5, 0x00, 0x8c, 0xf5,
  15052. 0x4d, 0xc2, 0xef, 0xb8, 0x9b, 0xd1, 0x87, 0xbe,
  15053. 0xed, 0xd5, 0x0a, 0x29, 0x15, 0x34, 0x59, 0x4c,
  15054. 0x3a, 0x05, 0x22, 0x05, 0x44, 0x4f, 0x9f, 0xc8,
  15055. 0x47, 0x12, 0x24, 0x8e, 0xa8, 0x79, 0xe4, 0x67,
  15056. 0xba, 0x4d, 0x5b, 0x75, 0x56, 0x95, 0xeb, 0xe8,
  15057. 0x8a, 0xfa, 0x8e, 0x01, 0x8c, 0x1b, 0x74, 0x63,
  15058. 0xd9, 0x2f, 0xf7, 0xd3, 0x44, 0x8f, 0xa8, 0xf5,
  15059. 0xaf, 0x6c, 0x4f, 0xdb, 0xe7, 0xc9, 0x6c, 0x71,
  15060. 0x22, 0xa3, 0x1d, 0xf1, 0x40, 0xb2, 0xe0, 0x9a,
  15061. 0xb6, 0x72, 0xc9, 0xc0, 0x13, 0x16, 0xa2, 0x4a,
  15062. 0xe1, 0x92, 0xc7, 0x54, 0x23, 0xab, 0x9d, 0xa1,
  15063. 0xa1, 0xe5, 0x0b, 0xed, 0xba, 0xe8, 0x84, 0x37,
  15064. 0xb2, 0xe7, 0xfe, 0x32, 0x8d, 0xfa, 0x1c, 0x53,
  15065. 0x77, 0x97, 0xc7, 0xf3, 0x48, 0xc9, 0xdb, 0x2d,
  15066. 0x75, 0x52, 0x9d, 0x42, 0x51, 0x78, 0x62, 0x68,
  15067. 0x05, 0x45, 0x15, 0xf8, 0xa2, 0x4e, 0xf3, 0x0b
  15068. };
  15069. WOLFSSL_SMALL_STACK_STATIC const byte q[] = {
  15070. 0xe0, 0x35, 0x37, 0xaf, 0xb2, 0x50, 0x91, 0x8e,
  15071. 0xf2, 0x62, 0x2b, 0xd9, 0x9f, 0x6c, 0x11, 0x75,
  15072. 0xec, 0x24, 0x5d, 0x78, 0x59, 0xe7, 0x8d, 0xb5,
  15073. 0x40, 0x52, 0xed, 0x41
  15074. };
  15075. WOLFSSL_SMALL_STACK_STATIC const byte q0[] = {
  15076. 0x00,
  15077. 0xe0, 0x35, 0x37, 0xaf, 0xb2, 0x50, 0x91, 0x8e,
  15078. 0xf2, 0x62, 0x2b, 0xd9, 0x9f, 0x6c, 0x11, 0x75,
  15079. 0xec, 0x24, 0x5d, 0x78, 0x59, 0xe7, 0x8d, 0xb5,
  15080. 0x40, 0x52, 0xed, 0x41
  15081. };
  15082. byte priv[256];
  15083. byte pub[256];
  15084. word32 privSz = sizeof(priv);
  15085. word32 pubSz = sizeof(pub);
  15086. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15087. if (key == NULL)
  15088. ERROR_OUT(MEMORY_E, exit_gen_test);
  15089. #endif
  15090. /* Parameter Validation testing. */
  15091. ret = wc_DhGenerateKeyPair(NULL, rng, priv, &privSz, pub, &pubSz);
  15092. if (ret != BAD_FUNC_ARG)
  15093. ERROR_OUT(-7980, exit_gen_test);
  15094. ret = wc_DhGenerateKeyPair(key, NULL, priv, &privSz, pub, &pubSz);
  15095. if (ret != BAD_FUNC_ARG)
  15096. ERROR_OUT(-7981, exit_gen_test);
  15097. ret = wc_DhGenerateKeyPair(key, rng, NULL, &privSz, pub, &pubSz);
  15098. if (ret != BAD_FUNC_ARG)
  15099. ERROR_OUT(-7982, exit_gen_test);
  15100. ret = wc_DhGenerateKeyPair(key, rng, priv, NULL, pub, &pubSz);
  15101. if (ret != BAD_FUNC_ARG)
  15102. ERROR_OUT(-7983, exit_gen_test);
  15103. ret = wc_DhGenerateKeyPair(key, rng, priv, &privSz, NULL, &pubSz);
  15104. if (ret != BAD_FUNC_ARG)
  15105. ERROR_OUT(-7984, exit_gen_test);
  15106. ret = wc_DhGenerateKeyPair(key, rng, priv, &privSz, pub, NULL);
  15107. if (ret != BAD_FUNC_ARG)
  15108. ERROR_OUT(-7985, exit_gen_test);
  15109. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  15110. if (ret != 0)
  15111. ERROR_OUT(-7986, exit_gen_test);
  15112. ret = wc_DhSetKey_ex(key, p, sizeof(p), g, sizeof(g), q0, sizeof(q0));
  15113. if (ret != 0) {
  15114. ERROR_OUT(-7987, exit_gen_test);
  15115. }
  15116. wc_FreeDhKey(key);
  15117. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  15118. if (ret != 0)
  15119. ERROR_OUT(-7988, exit_gen_test);
  15120. ret = wc_DhSetKey_ex(key, p, sizeof(p), g, sizeof(g), q, sizeof(q));
  15121. if (ret != 0) {
  15122. ERROR_OUT(-7989, exit_gen_test);
  15123. }
  15124. /* Use API. */
  15125. ret = wc_DhGenerateKeyPair(key, rng, priv, &privSz, pub, &pubSz);
  15126. #if defined(WOLFSSL_ASYNC_CRYPT)
  15127. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  15128. #endif
  15129. if (ret != 0) {
  15130. ERROR_OUT(-7990, exit_gen_test);
  15131. }
  15132. ret = wc_DhCheckPubKey_ex(key, pub, pubSz, q0, sizeof(q0));
  15133. if (ret != 0) {
  15134. ERROR_OUT(-7991, exit_gen_test);
  15135. }
  15136. wc_FreeDhKey(key);
  15137. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  15138. if (ret != 0)
  15139. ERROR_OUT(-7992, exit_gen_test);
  15140. ret = wc_DhSetKey(key, p, sizeof(p), g, sizeof(g));
  15141. if (ret != 0) {
  15142. ERROR_OUT(-7993, exit_gen_test);
  15143. }
  15144. ret = wc_DhCheckPubKey_ex(key, pub, pubSz, q, sizeof(q));
  15145. if (ret != 0) {
  15146. ERROR_OUT(-7994, exit_gen_test);
  15147. }
  15148. #ifndef HAVE_SELFTEST
  15149. ret = wc_DhCheckKeyPair(key, pub, pubSz, priv, privSz);
  15150. if (ret != 0) {
  15151. ERROR_OUT(-7995, exit_gen_test);
  15152. }
  15153. /* Taint the public key so the check fails. */
  15154. pub[0]++;
  15155. ret = wc_DhCheckKeyPair(key, pub, pubSz, priv, privSz);
  15156. if (ret != MP_CMP_E) {
  15157. ERROR_OUT(-7996, exit_gen_test);
  15158. }
  15159. #ifdef WOLFSSL_KEY_GEN
  15160. wc_FreeDhKey(key);
  15161. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  15162. if (ret != 0)
  15163. ERROR_OUT(-7997, exit_gen_test);
  15164. ret = wc_DhGenerateParams(rng, 2048, key);
  15165. if (ret != 0) {
  15166. ERROR_OUT(-7998, exit_gen_test);
  15167. }
  15168. privSz = sizeof(priv);
  15169. pubSz = sizeof(pub);
  15170. ret = wc_DhGenerateKeyPair(key, rng, priv, &privSz, pub, &pubSz);
  15171. #if defined(WOLFSSL_ASYNC_CRYPT)
  15172. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  15173. #endif
  15174. if (ret != 0) {
  15175. ERROR_OUT(-7999, exit_gen_test);
  15176. }
  15177. #endif /* WOLFSSL_KEY_GEN */
  15178. #endif /* HAVE_SELFTEST */
  15179. ret = 0;
  15180. exit_gen_test:
  15181. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15182. if (key) {
  15183. wc_FreeDhKey(key);
  15184. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15185. }
  15186. #else
  15187. wc_FreeDhKey(key);
  15188. #endif
  15189. return ret;
  15190. }
  15191. static int dh_generate_test(WC_RNG *rng)
  15192. {
  15193. int ret = 0;
  15194. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15195. DhKey *smallKey = NULL;
  15196. #else
  15197. DhKey smallKey[1];
  15198. #endif
  15199. byte p[2] = { 1, 7 }; /* 263 in decimal */
  15200. byte g[2] = { 0, 2 };
  15201. #if !defined(WOLFSSL_SP_MATH) && !defined(HAVE_FFDHE)
  15202. #ifdef WOLFSSL_DH_CONST
  15203. /* the table for constant DH lookup will round to the lowest byte size 21 */
  15204. byte priv[21];
  15205. byte pub[21];
  15206. #else
  15207. byte priv[2];
  15208. byte pub[2];
  15209. #endif
  15210. word32 privSz = sizeof(priv);
  15211. word32 pubSz = sizeof(pub);
  15212. #endif
  15213. int smallKey_inited = 0;
  15214. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15215. if ((smallKey = (DhKey *)XMALLOC(sizeof(*smallKey), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER)) == NULL)
  15216. return -8019;
  15217. #endif
  15218. ret = wc_InitDhKey_ex(smallKey, HEAP_HINT, devId);
  15219. if (ret != 0)
  15220. ERROR_OUT(-8010, exit_gen_test);
  15221. smallKey_inited = 1;
  15222. /* Parameter Validation testing. */
  15223. ret = wc_InitDhKey_ex(NULL, HEAP_HINT, devId);
  15224. if (ret != BAD_FUNC_ARG)
  15225. return -8011;
  15226. wc_FreeDhKey(NULL);
  15227. ret = wc_DhSetKey(NULL, p, sizeof(p), g, sizeof(g));
  15228. if (ret != BAD_FUNC_ARG) {
  15229. ERROR_OUT(-8012, exit_gen_test);
  15230. }
  15231. ret = wc_DhSetKey(smallKey, NULL, sizeof(p), g, sizeof(g));
  15232. if (ret != BAD_FUNC_ARG) {
  15233. ERROR_OUT(-8013, exit_gen_test);
  15234. }
  15235. ret = wc_DhSetKey(smallKey, p, 0, g, sizeof(g));
  15236. if (ret != BAD_FUNC_ARG) {
  15237. ERROR_OUT(-8014, exit_gen_test);
  15238. }
  15239. ret = wc_DhSetKey(smallKey, p, sizeof(p), NULL, sizeof(g));
  15240. if (ret != BAD_FUNC_ARG) {
  15241. ERROR_OUT(-8015, exit_gen_test);
  15242. }
  15243. ret = wc_DhSetKey(smallKey, p, sizeof(p), g, 0);
  15244. if (ret != BAD_FUNC_ARG) {
  15245. ERROR_OUT(-8016, exit_gen_test);
  15246. }
  15247. ret = wc_DhSetKey(smallKey, p, sizeof(p), g, sizeof(g));
  15248. if (ret != 0) {
  15249. ERROR_OUT(-8017, exit_gen_test);
  15250. }
  15251. #if !defined(WOLFSSL_SP_MATH) && !defined(HAVE_FFDHE)
  15252. /* Use API. */
  15253. ret = wc_DhGenerateKeyPair(smallKey, rng, priv, &privSz, pub, &pubSz);
  15254. #if defined(WOLFSSL_ASYNC_CRYPT)
  15255. ret = wc_AsyncWait(ret, &smallKey->asyncDev, WC_ASYNC_FLAG_NONE);
  15256. #endif
  15257. if (ret != 0) {
  15258. ret = -8018;
  15259. }
  15260. #else
  15261. (void)rng;
  15262. #if defined(HAVE_FIPS) || !defined(WOLFSSL_NO_DH186)
  15263. ret = 0;
  15264. #endif
  15265. #endif
  15266. #if !defined(HAVE_FIPS) && defined(WOLFSSL_NO_DH186)
  15267. {
  15268. byte priv[260];
  15269. byte pub[260];
  15270. word32 privSz = sizeof(priv);
  15271. word32 pubSz = sizeof(pub);
  15272. /* test odd ball param generation with DH */
  15273. wc_FreeDhKey(smallKey);
  15274. ret = wc_InitDhKey_ex(smallKey, HEAP_HINT, devId);
  15275. if (ret != 0)
  15276. ERROR_OUT(-8019, exit_gen_test);
  15277. ret = wc_DhGenerateParams(rng, 2056, smallKey);
  15278. if (ret != 0) {
  15279. ERROR_OUT(-8020, exit_gen_test);
  15280. }
  15281. privSz = sizeof(priv);
  15282. pubSz = sizeof(pub);
  15283. ret = wc_DhGenerateKeyPair(smallKey, rng, priv, &privSz, pub, &pubSz);
  15284. #if defined(WOLFSSL_ASYNC_CRYPT)
  15285. ret = wc_AsyncWait(ret, &smallKey->asyncDev, WC_ASYNC_FLAG_NONE);
  15286. #endif
  15287. if (ret != 0) {
  15288. ERROR_OUT(-8021, exit_gen_test);
  15289. }
  15290. }
  15291. #endif /* !HAVE_FIPS and WOLFSSL_NO_DH186 */
  15292. exit_gen_test:
  15293. if (smallKey_inited)
  15294. wc_FreeDhKey(smallKey);
  15295. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15296. if (smallKey != NULL)
  15297. XFREE(smallKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15298. #endif
  15299. return ret;
  15300. }
  15301. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  15302. typedef struct dh_pubvalue_test {
  15303. const byte* data;
  15304. word32 len;
  15305. } dh_pubvalue_test;
  15306. static int dh_test_check_pubvalue(void)
  15307. {
  15308. int ret;
  15309. word32 i;
  15310. WOLFSSL_SMALL_STACK_STATIC const byte prime[] = {0x01, 0x00, 0x01};
  15311. WOLFSSL_SMALL_STACK_STATIC const byte pubValZero[] = { 0x00 };
  15312. WOLFSSL_SMALL_STACK_STATIC const byte pubValZeroLong[] = { 0x00, 0x00, 0x00 };
  15313. WOLFSSL_SMALL_STACK_STATIC const byte pubValOne[] = { 0x01 };
  15314. WOLFSSL_SMALL_STACK_STATIC const byte pubValOneLong[] = { 0x00, 0x00, 0x01 };
  15315. WOLFSSL_SMALL_STACK_STATIC const byte pubValPrimeMinusOne[] = { 0x01, 0x00, 0x00 };
  15316. WOLFSSL_SMALL_STACK_STATIC const byte pubValPrimeLong[] = {0x00, 0x01, 0x00, 0x01};
  15317. WOLFSSL_SMALL_STACK_STATIC const byte pubValPrimePlusOne[] = { 0x01, 0x00, 0x02 };
  15318. WOLFSSL_SMALL_STACK_STATIC const byte pubValTooBig0[] = { 0x02, 0x00, 0x01 };
  15319. WOLFSSL_SMALL_STACK_STATIC const byte pubValTooBig1[] = { 0x01, 0x01, 0x01 };
  15320. WOLFSSL_SMALL_STACK_STATIC const byte pubValTooLong[] = { 0x01, 0x00, 0x00, 0x01 };
  15321. const dh_pubvalue_test dh_pubval_fail[] = {
  15322. { prime, sizeof(prime) },
  15323. { pubValZero, sizeof(pubValZero) },
  15324. { pubValZeroLong, sizeof(pubValZeroLong) },
  15325. { pubValOne, sizeof(pubValOne) },
  15326. { pubValOneLong, sizeof(pubValOneLong) },
  15327. { pubValPrimeMinusOne, sizeof(pubValPrimeMinusOne) },
  15328. { pubValPrimeLong, sizeof(pubValPrimeLong) },
  15329. { pubValPrimePlusOne, sizeof(pubValPrimePlusOne) },
  15330. { pubValTooBig0, sizeof(pubValTooBig0) },
  15331. { pubValTooBig1, sizeof(pubValTooBig1) },
  15332. { pubValTooLong, sizeof(pubValTooLong) },
  15333. };
  15334. WOLFSSL_SMALL_STACK_STATIC const byte pubValTwo[] = { 0x02 };
  15335. WOLFSSL_SMALL_STACK_STATIC const byte pubValTwoLong[] = { 0x00, 0x00, 0x02 };
  15336. WOLFSSL_SMALL_STACK_STATIC const byte pubValGood[] = { 0x12, 0x34 };
  15337. WOLFSSL_SMALL_STACK_STATIC const byte pubValGoodLen[] = { 0x00, 0x12, 0x34 };
  15338. WOLFSSL_SMALL_STACK_STATIC const byte pubValGoodLong[] = { 0x00, 0x00, 0x12, 0x34 };
  15339. const dh_pubvalue_test dh_pubval_pass[] = {
  15340. { pubValTwo, sizeof(pubValTwo) },
  15341. { pubValTwoLong, sizeof(pubValTwoLong) },
  15342. { pubValGood, sizeof(pubValGood) },
  15343. { pubValGoodLen, sizeof(pubValGoodLen) },
  15344. { pubValGoodLong, sizeof(pubValGoodLong) },
  15345. };
  15346. for (i = 0; i < sizeof(dh_pubval_fail) / sizeof(*dh_pubval_fail); i++) {
  15347. ret = wc_DhCheckPubValue(prime, sizeof(prime), dh_pubval_fail[i].data,
  15348. dh_pubval_fail[i].len);
  15349. if (ret != MP_VAL)
  15350. return -8020 - (int)i;
  15351. }
  15352. for (i = 0; i < sizeof(dh_pubval_pass) / sizeof(*dh_pubval_pass); i++) {
  15353. ret = wc_DhCheckPubValue(prime, sizeof(prime), dh_pubval_pass[i].data,
  15354. dh_pubval_pass[i].len);
  15355. if (ret != 0)
  15356. return -8030 - (int)i;
  15357. }
  15358. return 0;
  15359. }
  15360. #endif
  15361. #if defined(HAVE_FFDHE)
  15362. #if defined(HAVE_FFDHE_4096)
  15363. #define MAX_DH_PRIV_SZ 39
  15364. #define MAX_DH_KEY_SZ 512
  15365. #elif defined(HAVE_FFDHE_3072)
  15366. #define MAX_DH_PRIV_SZ 34
  15367. #define MAX_DH_KEY_SZ 384
  15368. #else
  15369. #define MAX_DH_PRIV_SZ 29
  15370. #define MAX_DH_KEY_SZ 256
  15371. #endif
  15372. #ifndef WC_NO_RNG
  15373. #if !(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION == 2) && \
  15374. (defined(WOLFSSL_SP_ARM64_ASM) || defined(WOLFSSL_SP_ARM32_ASM)))
  15375. #ifdef HAVE_PUBLIC_FFDHE
  15376. static int dh_ffdhe_test(WC_RNG *rng, const DhParams* params)
  15377. #else
  15378. static int dh_ffdhe_test(WC_RNG *rng, int name)
  15379. #endif
  15380. {
  15381. int ret;
  15382. word32 privSz, pubSz, privSz2, pubSz2;
  15383. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15384. byte *priv = (byte*)XMALLOC(MAX_DH_PRIV_SZ, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15385. byte *pub = (byte*)XMALLOC(MAX_DH_KEY_SZ, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15386. byte *priv2 = (byte*)XMALLOC(MAX_DH_PRIV_SZ, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15387. byte *pub2 = (byte*)XMALLOC(MAX_DH_KEY_SZ, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15388. byte *agree = (byte*)XMALLOC(MAX_DH_KEY_SZ, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15389. byte *agree2 = (byte*)XMALLOC(MAX_DH_KEY_SZ, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15390. DhKey *key = (DhKey*)XMALLOC(sizeof(*key), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15391. DhKey *key2 = (DhKey*)XMALLOC(sizeof(*key2), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15392. #else
  15393. byte priv[MAX_DH_PRIV_SZ];
  15394. byte pub[MAX_DH_KEY_SZ];
  15395. byte priv2[MAX_DH_PRIV_SZ];
  15396. byte pub2[MAX_DH_KEY_SZ];
  15397. byte agree[MAX_DH_KEY_SZ];
  15398. byte agree2[MAX_DH_KEY_SZ];
  15399. DhKey key[1];
  15400. DhKey key2[1];
  15401. #endif
  15402. word32 agreeSz = MAX_DH_KEY_SZ;
  15403. word32 agreeSz2 = MAX_DH_KEY_SZ;
  15404. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15405. if ((priv == NULL) ||
  15406. (pub == NULL) ||
  15407. (priv2 == NULL) ||
  15408. (pub2 == NULL) ||
  15409. (agree == NULL) ||
  15410. (agree2 == NULL) ||
  15411. (key == NULL) ||
  15412. (key2 == NULL))
  15413. ERROR_OUT(-8050, done);
  15414. #endif
  15415. pubSz = MAX_DH_KEY_SZ;
  15416. pubSz2 = MAX_DH_KEY_SZ;
  15417. #ifdef HAVE_PUBLIC_FFDHE
  15418. privSz = MAX_DH_PRIV_SZ;
  15419. privSz2 = MAX_DH_PRIV_SZ;
  15420. #else
  15421. privSz = wc_DhGetNamedKeyMinSize(name);
  15422. privSz2 = privSz;
  15423. #endif
  15424. XMEMSET(key, 0, sizeof(*key));
  15425. XMEMSET(key2, 0, sizeof(*key2));
  15426. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  15427. if (ret != 0) {
  15428. ERROR_OUT(-8051, done);
  15429. }
  15430. ret = wc_InitDhKey_ex(key2, HEAP_HINT, devId);
  15431. if (ret != 0) {
  15432. ERROR_OUT(-8052, done);
  15433. }
  15434. #ifdef HAVE_PUBLIC_FFDHE
  15435. ret = wc_DhSetKey(key, params->p, params->p_len, params->g, params->g_len);
  15436. #else
  15437. ret = wc_DhSetNamedKey(key, name);
  15438. #endif
  15439. if (ret != 0) {
  15440. ERROR_OUT(-8053, done);
  15441. }
  15442. #ifdef HAVE_PUBLIC_FFDHE
  15443. ret = wc_DhSetKey(key2, params->p, params->p_len, params->g,
  15444. params->g_len);
  15445. #else
  15446. ret = wc_DhSetNamedKey(key2, name);
  15447. #endif
  15448. if (ret != 0) {
  15449. ERROR_OUT(-8054, done);
  15450. }
  15451. ret = wc_DhGenerateKeyPair(key, rng, priv, &privSz, pub, &pubSz);
  15452. #if defined(WOLFSSL_ASYNC_CRYPT)
  15453. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  15454. #endif
  15455. if (ret != 0) {
  15456. ERROR_OUT(-8055, done);
  15457. }
  15458. ret = wc_DhGenerateKeyPair(key2, rng, priv2, &privSz2, pub2, &pubSz2);
  15459. #if defined(WOLFSSL_ASYNC_CRYPT)
  15460. ret = wc_AsyncWait(ret, &key2->asyncDev, WC_ASYNC_FLAG_NONE);
  15461. #endif
  15462. if (ret != 0) {
  15463. ERROR_OUT(-8056, done);
  15464. }
  15465. ret = wc_DhAgree(key, agree, &agreeSz, priv, privSz, pub2, pubSz2);
  15466. #if defined(WOLFSSL_ASYNC_CRYPT)
  15467. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  15468. #endif
  15469. if (ret != 0) {
  15470. ERROR_OUT(-8057, done);
  15471. }
  15472. ret = wc_DhAgree(key2, agree2, &agreeSz2, priv2, privSz2, pub, pubSz);
  15473. #if defined(WOLFSSL_ASYNC_CRYPT)
  15474. ret = wc_AsyncWait(ret, &key2->asyncDev, WC_ASYNC_FLAG_NONE);
  15475. #endif
  15476. if (ret != 0) {
  15477. ERROR_OUT(-8058, done);
  15478. }
  15479. if (agreeSz != agreeSz2 || XMEMCMP(agree, agree2, agreeSz)) {
  15480. ERROR_OUT(-8059, done);
  15481. }
  15482. #if defined(WOLFSSL_HAVE_SP_DH) || defined(USE_FAST_MATH)
  15483. /* Make p even */
  15484. key->p.dp[0] &= (mp_digit)-2;
  15485. if (ret != 0) {
  15486. ERROR_OUT(-8058, done);
  15487. }
  15488. ret = wc_DhGenerateKeyPair(key, rng, priv, &privSz, pub, &pubSz);
  15489. #if defined(WOLFSSL_ASYNC_CRYPT)
  15490. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  15491. #endif
  15492. if (ret != MP_VAL && ret != MP_EXPTMOD_E) {
  15493. ERROR_OUT(-8058, done);
  15494. }
  15495. ret = wc_DhAgree(key, agree, &agreeSz, priv, privSz, pub2, pubSz2);
  15496. #if defined(WOLFSSL_ASYNC_CRYPT)
  15497. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  15498. #endif
  15499. if (ret != MP_VAL && ret != MP_EXPTMOD_E && ret != ASYNC_OP_E) {
  15500. ERROR_OUT(-8057, done);
  15501. }
  15502. #ifndef HAVE_SELFTEST
  15503. ret = wc_DhCheckKeyPair(key, pub, pubSz, priv, privSz);
  15504. if (ret != MP_VAL && ret != MP_EXPTMOD_E && ret != MP_CMP_E &&
  15505. ret != ASYNC_OP_E) {
  15506. ERROR_OUT(-8057, done);
  15507. }
  15508. #endif
  15509. /* Getting here means success - set ret to 0. */
  15510. ret = 0;
  15511. #endif
  15512. done:
  15513. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC) && \
  15514. !defined(WC_NO_RNG)
  15515. if (priv)
  15516. XFREE(priv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15517. if (pub)
  15518. XFREE(pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15519. if (priv2)
  15520. XFREE(priv2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15521. if (pub2)
  15522. XFREE(pub2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15523. if (agree)
  15524. XFREE(agree, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15525. if (agree2)
  15526. XFREE(agree2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15527. if (key) {
  15528. wc_FreeDhKey(key);
  15529. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15530. }
  15531. if (key2) {
  15532. wc_FreeDhKey(key2);
  15533. XFREE(key2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15534. }
  15535. #else
  15536. wc_FreeDhKey(key);
  15537. wc_FreeDhKey(key2);
  15538. #endif
  15539. return ret;
  15540. }
  15541. #endif /* !(HAVE_FIPS_VERSION == 2 && WOLFSSL_SP_ARMxx_ASM) */
  15542. #endif /* !WC_NO_RNG */
  15543. #endif /* HAVE_FFDHE */
  15544. WOLFSSL_TEST_SUBROUTINE int dh_test(void)
  15545. {
  15546. int ret;
  15547. word32 bytes;
  15548. word32 idx = 0, privSz, pubSz, privSz2, pubSz2;
  15549. #ifndef WC_NO_RNG
  15550. WC_RNG rng;
  15551. int rngInit = 0;
  15552. #endif
  15553. int keyInit = 0, key2Init = 0;
  15554. #define DH_TEST_TMP_SIZE 1024
  15555. #if !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096)
  15556. #define DH_TEST_BUF_SIZE 256
  15557. #else
  15558. #define DH_TEST_BUF_SIZE 512
  15559. #endif
  15560. #ifndef WC_NO_RNG
  15561. word32 agreeSz = DH_TEST_BUF_SIZE;
  15562. word32 agreeSz2 = DH_TEST_BUF_SIZE;
  15563. #endif
  15564. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15565. DhKey *key = (DhKey *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15566. DhKey *key2 = (DhKey *)XMALLOC(sizeof *key2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15567. byte *tmp = (byte *)XMALLOC(DH_TEST_TMP_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15568. #else
  15569. DhKey key[1];
  15570. DhKey key2[1];
  15571. byte tmp[DH_TEST_TMP_SIZE];
  15572. #endif
  15573. #ifndef WC_NO_RNG
  15574. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15575. byte *priv = (byte *)XMALLOC(DH_TEST_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15576. byte *pub = (byte *)XMALLOC(DH_TEST_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15577. byte *priv2 = (byte *)XMALLOC(DH_TEST_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15578. byte *pub2 = (byte *)XMALLOC(DH_TEST_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15579. byte *agree = (byte *)XMALLOC(DH_TEST_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15580. byte *agree2 = (byte *)XMALLOC(DH_TEST_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15581. if (priv == NULL || pub == NULL || priv2 == NULL || pub2 == NULL ||
  15582. agree == NULL || agree2 == NULL) {
  15583. ERROR_OUT(-8100, done);
  15584. }
  15585. #else
  15586. byte priv[DH_TEST_BUF_SIZE];
  15587. byte pub[DH_TEST_BUF_SIZE];
  15588. byte priv2[DH_TEST_BUF_SIZE];
  15589. byte pub2[DH_TEST_BUF_SIZE];
  15590. byte agree[DH_TEST_BUF_SIZE];
  15591. byte agree2[DH_TEST_BUF_SIZE];
  15592. #endif
  15593. #endif /* !WC_NO_RNG */
  15594. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15595. if (key == NULL || key2 == NULL || tmp == NULL) {
  15596. ERROR_OUT(-8100, done);
  15597. }
  15598. #endif
  15599. #ifdef USE_CERT_BUFFERS_1024
  15600. XMEMCPY(tmp, dh_key_der_1024, (size_t)sizeof_dh_key_der_1024);
  15601. bytes = (size_t)sizeof_dh_key_der_1024;
  15602. #elif defined(USE_CERT_BUFFERS_2048)
  15603. XMEMCPY(tmp, dh_key_der_2048, (size_t)sizeof_dh_key_der_2048);
  15604. bytes = (size_t)sizeof_dh_key_der_2048;
  15605. #elif defined(USE_CERT_BUFFERS_3072)
  15606. XMEMCPY(tmp, dh_key_der_3072, (size_t)sizeof_dh_key_der_3072);
  15607. bytes = (size_t)sizeof_dh_key_der_3072;
  15608. #elif defined(USE_CERT_BUFFERS_4096)
  15609. XMEMCPY(tmp, dh_key_der_4096, (size_t)sizeof_dh_key_der_4096);
  15610. bytes = (size_t)sizeof_dh_key_der_4096;
  15611. #elif defined(NO_ASN)
  15612. /* don't use file, no DER parsing */
  15613. #elif !defined(NO_FILESYSTEM)
  15614. {
  15615. XFILE file = XFOPEN(dhParamsFile, "rb");
  15616. if (! file)
  15617. ERROR_OUT(-8101, done);
  15618. bytes = (word32) XFREAD(tmp, 1, DH_TEST_TMP_SIZE, file);
  15619. XFCLOSE(file);
  15620. }
  15621. #else
  15622. /* No DH key to use. */
  15623. ERROR_OUT(-8102, done);
  15624. #endif /* USE_CERT_BUFFERS */
  15625. (void)idx;
  15626. (void)tmp;
  15627. (void)bytes;
  15628. pubSz = DH_TEST_BUF_SIZE;
  15629. pubSz2 = DH_TEST_BUF_SIZE;
  15630. privSz = DH_TEST_BUF_SIZE;
  15631. privSz2 = DH_TEST_BUF_SIZE;
  15632. #ifndef WC_NO_RNG
  15633. XMEMSET(&rng, 0, sizeof(rng));
  15634. #endif
  15635. /* Use API for coverage. */
  15636. ret = wc_InitDhKey(key);
  15637. if (ret != 0) {
  15638. ERROR_OUT(-8103, done);
  15639. }
  15640. wc_FreeDhKey(key);
  15641. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  15642. if (ret != 0) {
  15643. ERROR_OUT(-8104, done);
  15644. }
  15645. keyInit = 1;
  15646. ret = wc_InitDhKey_ex(key2, HEAP_HINT, devId);
  15647. if (ret != 0) {
  15648. ERROR_OUT(-8105, done);
  15649. }
  15650. key2Init = 1;
  15651. #ifdef NO_ASN
  15652. #ifndef WOLFSSL_SP_MATH
  15653. ret = wc_DhSetKey(key, dh_p, sizeof(dh_p), dh_g, sizeof(dh_g));
  15654. if (ret != 0) {
  15655. ERROR_OUT(-8106, done);
  15656. }
  15657. ret = wc_DhSetKey(key2, dh_p, sizeof(dh_p), dh_g, sizeof(dh_g));
  15658. if (ret != 0) {
  15659. ERROR_OUT(-8107, done);
  15660. }
  15661. #else
  15662. ret = wc_DhSetKey(key, dh2048_p, sizeof(dh2048_p), dh2048_g,
  15663. sizeof(dh2048_g));
  15664. if (ret != 0) {
  15665. ERROR_OUT(-8106, done);
  15666. }
  15667. ret = wc_DhSetKey(key2, dh2048_p, sizeof(dh2048_p), dh2048_g,
  15668. sizeof(dh2048_g));
  15669. if (ret != 0) {
  15670. ERROR_OUT(-8107, done);
  15671. }
  15672. #endif
  15673. #else
  15674. ret = wc_DhKeyDecode(tmp, &idx, key, bytes);
  15675. if (ret != 0) {
  15676. ERROR_OUT(-8108, done);
  15677. }
  15678. idx = 0;
  15679. ret = wc_DhKeyDecode(tmp, &idx, key2, bytes);
  15680. if (ret != 0) {
  15681. ERROR_OUT(-8109, done);
  15682. }
  15683. #endif
  15684. #ifndef WC_NO_RNG
  15685. #ifndef HAVE_FIPS
  15686. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  15687. #else
  15688. ret = wc_InitRng(&rng);
  15689. #endif
  15690. if (ret != 0) {
  15691. ERROR_OUT(-8110, done);
  15692. }
  15693. rngInit = 1;
  15694. ret = wc_DhGenerateKeyPair(key, &rng, priv, &privSz, pub, &pubSz);
  15695. #if defined(WOLFSSL_ASYNC_CRYPT)
  15696. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  15697. #endif
  15698. if (ret != 0) {
  15699. ERROR_OUT(-8111, done);
  15700. }
  15701. ret = wc_DhGenerateKeyPair(key2, &rng, priv2, &privSz2, pub2, &pubSz2);
  15702. #if defined(WOLFSSL_ASYNC_CRYPT)
  15703. ret = wc_AsyncWait(ret, &key2->asyncDev, WC_ASYNC_FLAG_NONE);
  15704. #endif
  15705. if (ret != 0) {
  15706. ERROR_OUT(-8112, done);
  15707. }
  15708. ret = wc_DhAgree(key, agree, &agreeSz, priv, privSz, pub2, pubSz2);
  15709. #if defined(WOLFSSL_ASYNC_CRYPT)
  15710. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  15711. #endif
  15712. if (ret != 0) {
  15713. ERROR_OUT(-8113, done);
  15714. }
  15715. ret = wc_DhAgree(key2, agree2, &agreeSz2, priv2, privSz2, pub, pubSz);
  15716. #if defined(WOLFSSL_ASYNC_CRYPT)
  15717. ret = wc_AsyncWait(ret, &key2->asyncDev, WC_ASYNC_FLAG_NONE);
  15718. #endif
  15719. if (ret != 0) {
  15720. ERROR_OUT(-8114, done);
  15721. }
  15722. if (agreeSz != agreeSz2 || XMEMCMP(agree, agree2, agreeSz)) {
  15723. ERROR_OUT(-8115, done);
  15724. }
  15725. #endif /* !WC_NO_RNG */
  15726. #if defined(WOLFSSL_KEY_GEN) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  15727. if (wc_DhCheckPrivKey(NULL, NULL, 0) != BAD_FUNC_ARG)
  15728. ERROR_OUT(-8116, done);
  15729. if (wc_DhCheckPrivKey(key, priv, privSz) != 0)
  15730. ERROR_OUT(-8117, done);
  15731. if (wc_DhExportParamsRaw(NULL, NULL, NULL, NULL, NULL, NULL, NULL) != BAD_FUNC_ARG)
  15732. ERROR_OUT(-8118, done);
  15733. {
  15734. word32 pSz, qSz, gSz;
  15735. if (wc_DhExportParamsRaw(key, NULL, &pSz, NULL, &qSz, NULL, &gSz) != LENGTH_ONLY_E)
  15736. ERROR_OUT(-8119, done);
  15737. }
  15738. #endif
  15739. /* Test DH key import / export */
  15740. #if defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM) && \
  15741. (!defined(HAVE_FIPS) || \
  15742. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  15743. wc_FreeDhKey(key);
  15744. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  15745. if (ret != 0) {
  15746. ERROR_OUT(-8120, done);
  15747. }
  15748. #ifndef NO_ASN
  15749. {
  15750. /* DH Private - Key Export / Import */
  15751. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15752. byte *tmp2;
  15753. #else
  15754. byte tmp2[DH_TEST_TMP_SIZE];
  15755. #endif
  15756. XFILE file = XFOPEN(dhKeyFile, "rb");
  15757. if (!file)
  15758. ERROR_OUT(-8130, done);
  15759. bytes = (word32)XFREAD(tmp, 1, DH_TEST_TMP_SIZE, file);
  15760. XFCLOSE(file);
  15761. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15762. tmp2 = (byte*)XMALLOC(DH_TEST_TMP_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15763. if (tmp2 == NULL)
  15764. ERROR_OUT(-8131, done);
  15765. #endif
  15766. idx = 0;
  15767. XMEMSET(tmp2, 0, DH_TEST_TMP_SIZE);
  15768. /* Import DH Private key as DER */
  15769. ret = wc_DhKeyDecode(tmp, &idx, key, bytes);
  15770. if (ret == 0) {
  15771. /* Export as DER */
  15772. idx = DH_TEST_TMP_SIZE;
  15773. ret = wc_DhPrivKeyToDer(key, tmp2, &idx);
  15774. }
  15775. /* Verify export matches original */
  15776. if (ret <= 0 || bytes != idx || XMEMCMP(tmp, tmp2, bytes) != 0) {
  15777. ERROR_OUT(-8132, done);
  15778. }
  15779. /* DH Public Key - Export / Import */
  15780. file = XFOPEN(dhKeyPubFile, "rb");
  15781. if (!file)
  15782. ERROR_OUT(-8133, done);
  15783. bytes = (word32)XFREAD(tmp, 1, DH_TEST_TMP_SIZE, file);
  15784. XFCLOSE(file);
  15785. /* for HAVE_WOLF_BIGINT prevent leak */
  15786. wc_FreeDhKey(key);
  15787. (void)wc_InitDhKey_ex(key, HEAP_HINT, devId);
  15788. idx = 0;
  15789. XMEMSET(tmp2, 0, DH_TEST_TMP_SIZE);
  15790. /* Import DH Public key as DER */
  15791. ret = wc_DhKeyDecode(tmp, &idx, key, bytes);
  15792. if (ret == 0) {
  15793. /* Export as DER */
  15794. idx = DH_TEST_TMP_SIZE;
  15795. ret = wc_DhPubKeyToDer(key, tmp2, &idx);
  15796. }
  15797. /* Verify export matches original */
  15798. if (ret <= 0 || bytes != idx || XMEMCMP(tmp, tmp2, bytes) != 0) {
  15799. ERROR_OUT(-8134, done);
  15800. }
  15801. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15802. XFREE(tmp2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15803. #endif
  15804. }
  15805. #else
  15806. ret = wc_DhSetKey(key, dh_p, sizeof(dh_p), dh_g, sizeof(dh_g));
  15807. if (ret != 0) {
  15808. ERROR_OUT(-8121, done);
  15809. }
  15810. #endif /* !NO_ASN */
  15811. privSz = DH_TEST_BUF_SIZE;
  15812. pubSz = DH_TEST_BUF_SIZE;
  15813. ret = wc_DhExportKeyPair(key, priv, &privSz, pub, &pubSz);
  15814. if (ret != 0) {
  15815. ERROR_OUT(-8122, done);
  15816. }
  15817. ret = wc_DhImportKeyPair(key2, priv, privSz, pub, pubSz);
  15818. if (ret != 0) {
  15819. ERROR_OUT(-8125, done);
  15820. }
  15821. #endif /* WOLFSSL_DH_EXTRA && !NO_FILESYSTEM && !FIPS <= 2 */
  15822. #ifndef WC_NO_RNG
  15823. ret = dh_generate_test(&rng);
  15824. if (ret != 0)
  15825. ERROR_OUT(-8123, done);
  15826. ret = dh_fips_generate_test(&rng);
  15827. if (ret != 0)
  15828. ERROR_OUT(-8124, done);
  15829. #endif /* !WC_NO_RNG */
  15830. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  15831. ret = dh_test_check_pubvalue();
  15832. if (ret != 0)
  15833. ERROR_OUT(-8125, done);
  15834. #endif
  15835. #if !(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION == 2) && \
  15836. (defined(WOLFSSL_SP_ARM64_ASM) || defined(WOLFSSL_SP_ARM32_ASM)))
  15837. /* RNG with DH and SP_ASM code not supported in the in-lined FIPS ASM code,
  15838. * this will be available for testing in the 140-3 module */
  15839. #ifndef WC_NO_RNG
  15840. /* Specialized code for key gen when using FFDHE-2048, FFDHE-3072 and FFDHE-4096 */
  15841. #ifdef HAVE_FFDHE_2048
  15842. #ifdef HAVE_PUBLIC_FFDHE
  15843. ret = dh_ffdhe_test(&rng, wc_Dh_ffdhe2048_Get());
  15844. #else
  15845. ret = dh_ffdhe_test(&rng, WC_FFDHE_2048);
  15846. #endif
  15847. if (ret != 0)
  15848. ERROR_OUT(-8126, done);
  15849. #endif
  15850. #ifdef HAVE_FFDHE_3072
  15851. #ifdef HAVE_PUBLIC_FFDHE
  15852. ret = dh_ffdhe_test(&rng, wc_Dh_ffdhe3072_Get());
  15853. #else
  15854. ret = dh_ffdhe_test(&rng, WC_FFDHE_3072);
  15855. #endif
  15856. if (ret != 0)
  15857. ERROR_OUT(-8127, done);
  15858. #endif
  15859. #ifdef HAVE_FFDHE_4096
  15860. #ifdef HAVE_PUBLIC_FFDHE
  15861. ret = dh_ffdhe_test(&rng, wc_Dh_ffdhe4096_Get());
  15862. #else
  15863. ret = dh_ffdhe_test(&rng, WC_FFDHE_4096);
  15864. #endif
  15865. if (ret != 0)
  15866. ERROR_OUT(-8128, done);
  15867. #endif
  15868. #endif /* !WC_NO_RNG */
  15869. #endif /* HAVE_FIPS_VERSION == 2 && !WOLFSSL_SP_ARM64_ASM */
  15870. wc_FreeDhKey(key);
  15871. keyInit = 0;
  15872. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  15873. !defined(WOLFSSL_OLD_PRIME_CHECK) && !defined(WC_NO_RNG)
  15874. /* Test Check Key */
  15875. ret = wc_DhSetCheckKey(key, dh_p, sizeof(dh_p), dh_g, sizeof(dh_g),
  15876. NULL, 0, 0, &rng);
  15877. if (ret != 0)
  15878. ERROR_OUT(-8129, done);
  15879. keyInit = 1; /* DhSetCheckKey also initializes the key, free it */
  15880. #endif
  15881. done:
  15882. #ifndef WC_NO_RNG
  15883. if (rngInit)
  15884. wc_FreeRng(&rng);
  15885. #endif
  15886. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15887. if (key) {
  15888. if (keyInit)
  15889. wc_FreeDhKey(key);
  15890. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15891. }
  15892. if (key2) {
  15893. if (key2Init)
  15894. wc_FreeDhKey(key2);
  15895. XFREE(key2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15896. }
  15897. if (tmp)
  15898. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15899. if (priv)
  15900. XFREE(priv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15901. if (pub)
  15902. XFREE(pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15903. if (priv2)
  15904. XFREE(priv2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15905. if (pub2)
  15906. XFREE(pub2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15907. if (agree)
  15908. XFREE(agree, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15909. if (agree2)
  15910. XFREE(agree2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15911. #else
  15912. if (keyInit)
  15913. wc_FreeDhKey(key);
  15914. if (key2Init)
  15915. wc_FreeDhKey(key2);
  15916. #endif
  15917. (void)privSz;
  15918. (void)pubSz;
  15919. (void)pubSz2;
  15920. (void)privSz2;
  15921. return ret;
  15922. #undef DH_TEST_BUF_SIZE
  15923. #undef DH_TEST_TMP_SIZE
  15924. }
  15925. #endif /* NO_DH */
  15926. #ifndef NO_DSA
  15927. WOLFSSL_TEST_SUBROUTINE int dsa_test(void)
  15928. {
  15929. int ret = 0, answer;
  15930. word32 bytes;
  15931. word32 idx = 0;
  15932. WC_RNG rng;
  15933. wc_Sha sha;
  15934. byte hash[WC_SHA_DIGEST_SIZE];
  15935. byte signature[40];
  15936. #ifdef WOLFSSL_KEY_GEN
  15937. byte* der = 0;
  15938. #endif
  15939. #define DSA_TEST_TMP_SIZE 1024
  15940. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15941. byte *tmp = (byte *)XMALLOC(DSA_TEST_TMP_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15942. DsaKey *key = (DsaKey *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15943. #ifdef WOLFSSL_KEY_GEN
  15944. DsaKey *derIn = (DsaKey *)XMALLOC(sizeof *derIn, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15945. DsaKey *genKey = (DsaKey *)XMALLOC(sizeof *genKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15946. #endif
  15947. if ((tmp == NULL) ||
  15948. (key == NULL)
  15949. #ifdef WOLFSSL_KEY_GEN
  15950. || (derIn == NULL)
  15951. || (genKey == NULL)
  15952. #endif
  15953. ) {
  15954. ret = -8216;
  15955. goto out;
  15956. }
  15957. #else
  15958. byte tmp[1024];
  15959. DsaKey key[1];
  15960. #ifdef WOLFSSL_KEY_GEN
  15961. DsaKey derIn[1];
  15962. DsaKey genKey[1];
  15963. #endif
  15964. #endif
  15965. #ifdef USE_CERT_BUFFERS_1024
  15966. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  15967. bytes = sizeof_dsa_key_der_1024;
  15968. #elif defined(USE_CERT_BUFFERS_2048)
  15969. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  15970. bytes = sizeof_dsa_key_der_2048;
  15971. #else
  15972. {
  15973. XFILE file = XFOPEN(dsaKey, "rb");
  15974. if (!file)
  15975. ERROR_OUT(-8200, out);
  15976. bytes = (word32) XFREAD(tmp, 1, DSA_TEST_TMP_SIZE, file);
  15977. XFCLOSE(file);
  15978. }
  15979. #endif /* USE_CERT_BUFFERS */
  15980. ret = wc_InitSha_ex(&sha, HEAP_HINT, devId);
  15981. if (ret != 0)
  15982. ERROR_OUT(-8201, out);
  15983. wc_ShaUpdate(&sha, tmp, bytes);
  15984. wc_ShaFinal(&sha, hash);
  15985. wc_ShaFree(&sha);
  15986. ret = wc_InitDsaKey(key);
  15987. if (ret != 0)
  15988. ERROR_OUT(-8202, out);
  15989. ret = wc_DsaPrivateKeyDecode(tmp, &idx, key, bytes);
  15990. if (ret != 0)
  15991. ERROR_OUT(-8203, out);
  15992. #ifndef HAVE_FIPS
  15993. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  15994. #else
  15995. ret = wc_InitRng(&rng);
  15996. #endif
  15997. if (ret != 0)
  15998. ERROR_OUT(-8204, out);
  15999. ret = wc_DsaSign(hash, signature, key, &rng);
  16000. if (ret != 0)
  16001. ERROR_OUT(-8205, out);
  16002. ret = wc_DsaVerify(hash, signature, key, &answer);
  16003. if (ret != 0)
  16004. ERROR_OUT(-8206, out);
  16005. if (answer != 1)
  16006. ERROR_OUT(-8207, out);
  16007. wc_FreeDsaKey(key);
  16008. #ifdef WOLFSSL_KEY_GEN
  16009. {
  16010. int derSz = 0;
  16011. ret = wc_InitDsaKey(genKey);
  16012. if (ret != 0)
  16013. ERROR_OUT(-8208, out);
  16014. ret = wc_MakeDsaParameters(&rng, 1024, genKey);
  16015. if (ret != 0) {
  16016. wc_FreeDsaKey(genKey);
  16017. ERROR_OUT(-8209, out);
  16018. }
  16019. ret = wc_MakeDsaKey(&rng, genKey);
  16020. if (ret != 0) {
  16021. wc_FreeDsaKey(genKey);
  16022. ERROR_OUT(-8210, out);
  16023. }
  16024. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16025. if (der == NULL) {
  16026. wc_FreeDsaKey(genKey);
  16027. ERROR_OUT(-8211, out);
  16028. }
  16029. derSz = wc_DsaKeyToDer(genKey, der, FOURK_BUF);
  16030. if (derSz < 0) {
  16031. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16032. ERROR_OUT(-8212, out);
  16033. }
  16034. ret = SaveDerAndPem(der, derSz, keyDerFile, keyPemFile,
  16035. DSA_PRIVATEKEY_TYPE, -5814);
  16036. if (ret != 0) {
  16037. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16038. wc_FreeDsaKey(genKey);
  16039. goto out;
  16040. }
  16041. ret = wc_InitDsaKey(derIn);
  16042. if (ret != 0) {
  16043. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16044. wc_FreeDsaKey(genKey);
  16045. ERROR_OUT(-8213, out);
  16046. }
  16047. idx = 0;
  16048. ret = wc_DsaPrivateKeyDecode(der, &idx, derIn, derSz);
  16049. if (ret != 0) {
  16050. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16051. wc_FreeDsaKey(derIn);
  16052. wc_FreeDsaKey(genKey);
  16053. ERROR_OUT(-8214, out);
  16054. }
  16055. }
  16056. #endif /* WOLFSSL_KEY_GEN */
  16057. out:
  16058. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16059. if (key) {
  16060. #endif
  16061. if (wc_InitDsaKey_h(key, NULL) != 0)
  16062. ret = -8215;
  16063. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16064. }
  16065. #endif
  16066. #ifdef WOLFSSL_KEY_GEN
  16067. if (der)
  16068. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16069. #endif
  16070. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16071. if (tmp)
  16072. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16073. if (key)
  16074. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16075. #ifdef WOLFSSL_KEY_GEN
  16076. if (derIn) {
  16077. wc_FreeDsaKey(derIn);
  16078. XFREE(derIn, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16079. }
  16080. if (genKey) {
  16081. wc_FreeDsaKey(genKey);
  16082. XFREE(genKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16083. }
  16084. #endif
  16085. #else /* !WOLFSSL_SMALL_STACK || WOLFSSL_NO_MALLOC */
  16086. #ifdef WOLFSSL_KEY_GEN
  16087. wc_FreeDsaKey(derIn);
  16088. wc_FreeDsaKey(genKey);
  16089. #endif
  16090. #endif
  16091. wc_FreeRng(&rng);
  16092. return ret;
  16093. }
  16094. #endif /* NO_DSA */
  16095. #ifdef WOLFCRYPT_HAVE_SRP
  16096. static int generate_random_salt(byte *buf, word32 size)
  16097. {
  16098. int ret = -8220;
  16099. WC_RNG rng;
  16100. if(NULL == buf || !size)
  16101. return -8221;
  16102. if (buf && size && wc_InitRng_ex(&rng, HEAP_HINT, devId) == 0) {
  16103. ret = wc_RNG_GenerateBlock(&rng, (byte *)buf, size);
  16104. wc_FreeRng(&rng);
  16105. }
  16106. return ret;
  16107. }
  16108. static int srp_test_digest(SrpType dgstType)
  16109. {
  16110. int r;
  16111. byte clientPubKey[192]; /* A */
  16112. byte serverPubKey[192]; /* B */
  16113. word32 clientPubKeySz = 192;
  16114. word32 serverPubKeySz = 192;
  16115. byte username[] = "user";
  16116. word32 usernameSz = 4;
  16117. byte password[] = "password";
  16118. word32 passwordSz = 8;
  16119. WOLFSSL_SMALL_STACK_STATIC const byte N[] = {
  16120. 0xfc, 0x58, 0x7a, 0x8a, 0x70, 0xfb, 0x5a, 0x9a,
  16121. 0x5d, 0x39, 0x48, 0xbf, 0x1c, 0x46, 0xd8, 0x3b,
  16122. 0x7a, 0xe9, 0x1f, 0x85, 0x36, 0x18, 0xc4, 0x35,
  16123. 0x3f, 0xf8, 0x8a, 0x8f, 0x8c, 0x10, 0x2e, 0x01,
  16124. 0x58, 0x1d, 0x41, 0xcb, 0xc4, 0x47, 0xa8, 0xaf,
  16125. 0x9a, 0x6f, 0x58, 0x14, 0xa4, 0x68, 0xf0, 0x9c,
  16126. 0xa6, 0xe7, 0xbf, 0x0d, 0xe9, 0x62, 0x0b, 0xd7,
  16127. 0x26, 0x46, 0x5b, 0x27, 0xcb, 0x4c, 0xf9, 0x7e,
  16128. 0x1e, 0x8b, 0xe6, 0xdd, 0x29, 0xb7, 0xb7, 0x15,
  16129. 0x2e, 0xcf, 0x23, 0xa6, 0x4b, 0x97, 0x9f, 0x89,
  16130. 0xd4, 0x86, 0xc4, 0x90, 0x63, 0x92, 0xf4, 0x30,
  16131. 0x26, 0x69, 0x48, 0x9d, 0x7a, 0x4f, 0xad, 0xb5,
  16132. 0x6a, 0x51, 0xad, 0xeb, 0xf9, 0x90, 0x31, 0x77,
  16133. 0x53, 0x30, 0x2a, 0x85, 0xf7, 0x11, 0x21, 0x0c,
  16134. 0xb8, 0x4b, 0x56, 0x03, 0x5e, 0xbb, 0x25, 0x33,
  16135. 0x7c, 0xd9, 0x5a, 0xd1, 0x5c, 0xb2, 0xd4, 0x53,
  16136. 0xc5, 0x16, 0x68, 0xf0, 0xdf, 0x48, 0x55, 0x3e,
  16137. 0xd4, 0x59, 0x87, 0x64, 0x59, 0xaa, 0x39, 0x01,
  16138. 0x45, 0x89, 0x9c, 0x72, 0xff, 0xdd, 0x8f, 0x6d,
  16139. 0xa0, 0x42, 0xbc, 0x6f, 0x6e, 0x62, 0x18, 0x2d,
  16140. 0x50, 0xe8, 0x18, 0x97, 0x87, 0xfc, 0xef, 0x1f,
  16141. 0xf5, 0x53, 0x68, 0xe8, 0x49, 0xd1, 0xa2, 0xe8,
  16142. 0xb9, 0x26, 0x03, 0xba, 0xb5, 0x58, 0x6f, 0x6c,
  16143. 0x8b, 0x08, 0xa1, 0x7b, 0x6f, 0x42, 0xc9, 0x53
  16144. };
  16145. WOLFSSL_SMALL_STACK_STATIC const byte g[] = {
  16146. 0x02
  16147. };
  16148. byte salt[10];
  16149. byte verifier[192];
  16150. word32 v_size = sizeof(verifier);
  16151. word32 clientProofSz = SRP_MAX_DIGEST_SIZE;
  16152. word32 serverProofSz = SRP_MAX_DIGEST_SIZE;
  16153. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16154. Srp *cli = (Srp *)XMALLOC(sizeof *cli, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16155. Srp *srv = (Srp *)XMALLOC(sizeof *srv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16156. byte *clientProof = (byte *)XMALLOC(SRP_MAX_DIGEST_SIZE, HEAP_HINT,
  16157. DYNAMIC_TYPE_TMP_BUFFER); /* M1 */
  16158. byte *serverProof = (byte *)XMALLOC(SRP_MAX_DIGEST_SIZE, HEAP_HINT,
  16159. DYNAMIC_TYPE_TMP_BUFFER); /* M2 */
  16160. if ((cli == NULL) ||
  16161. (srv == NULL) ||
  16162. (clientProof == NULL) ||
  16163. (serverProof == NULL)) {
  16164. r = -8222;
  16165. goto out;
  16166. }
  16167. #else
  16168. Srp cli[1], srv[1];
  16169. byte clientProof[SRP_MAX_DIGEST_SIZE]; /* M1 */
  16170. byte serverProof[SRP_MAX_DIGEST_SIZE]; /* M2 */
  16171. #endif
  16172. /* set as 0's so if second init on srv not called SrpTerm is not on
  16173. * garbage values */
  16174. XMEMSET(srv, 0, sizeof *srv);
  16175. XMEMSET(cli, 0, sizeof *cli);
  16176. /* generating random salt */
  16177. r = generate_random_salt(salt, sizeof(salt));
  16178. /* client knows username and password. */
  16179. /* server knows N, g, salt and verifier. */
  16180. if (!r) r = wc_SrpInit_ex(cli, dgstType, SRP_CLIENT_SIDE, HEAP_HINT, devId);
  16181. if (!r) r = wc_SrpSetUsername(cli, username, usernameSz);
  16182. /* loading N, g and salt in advance to generate the verifier. */
  16183. if (!r) r = wc_SrpSetParams(cli, N, sizeof(N),
  16184. g, sizeof(g),
  16185. salt, sizeof(salt));
  16186. if (!r) r = wc_SrpSetPassword(cli, password, passwordSz);
  16187. if (!r) r = wc_SrpGetVerifier(cli, verifier, &v_size);
  16188. /* client sends username to server */
  16189. if (!r) r = wc_SrpInit_ex(srv, dgstType, SRP_SERVER_SIDE, HEAP_HINT, devId);
  16190. if (!r) r = wc_SrpSetUsername(srv, username, usernameSz);
  16191. if (!r) r = wc_SrpSetParams(srv, N, sizeof(N),
  16192. g, sizeof(g),
  16193. salt, sizeof(salt));
  16194. if (!r) r = wc_SrpSetVerifier(srv, verifier, v_size);
  16195. if (!r) r = wc_SrpGetPublic(srv, serverPubKey, &serverPubKeySz);
  16196. /* server sends N, g, salt and B to client */
  16197. if (!r) r = wc_SrpGetPublic(cli, clientPubKey, &clientPubKeySz);
  16198. if (!r) r = wc_SrpComputeKey(cli, clientPubKey, clientPubKeySz,
  16199. serverPubKey, serverPubKeySz);
  16200. if (!r) r = wc_SrpGetProof(cli, clientProof, &clientProofSz);
  16201. /* client sends A and M1 to server */
  16202. if (!r) r = wc_SrpComputeKey(srv, clientPubKey, clientPubKeySz,
  16203. serverPubKey, serverPubKeySz);
  16204. if (!r) r = wc_SrpVerifyPeersProof(srv, clientProof, clientProofSz);
  16205. if (!r) r = wc_SrpGetProof(srv, serverProof, &serverProofSz);
  16206. /* server sends M2 to client */
  16207. if (!r) r = wc_SrpVerifyPeersProof(cli, serverProof, serverProofSz);
  16208. wc_SrpTerm(cli);
  16209. wc_SrpTerm(srv);
  16210. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16211. out:
  16212. if (cli)
  16213. XFREE(cli, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16214. if (srv)
  16215. XFREE(srv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16216. if (clientProof)
  16217. XFREE(clientProof, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16218. if (serverProof)
  16219. XFREE(serverProof, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16220. #endif
  16221. return r;
  16222. }
  16223. WOLFSSL_TEST_SUBROUTINE int srp_test(void)
  16224. {
  16225. int ret;
  16226. #ifndef NO_SHA
  16227. ret = srp_test_digest(SRP_TYPE_SHA);
  16228. if (ret != 0)
  16229. return ret;
  16230. #endif
  16231. #ifndef NO_SHA256
  16232. ret = srp_test_digest(SRP_TYPE_SHA256);
  16233. if (ret != 0)
  16234. return ret;
  16235. #endif
  16236. #ifdef WOLFSSL_SHA384
  16237. ret = srp_test_digest(SRP_TYPE_SHA384);
  16238. if (ret != 0)
  16239. return ret;
  16240. #endif
  16241. #ifdef WOLFSSL_SHA512
  16242. ret = srp_test_digest(SRP_TYPE_SHA512);
  16243. if (ret != 0)
  16244. return ret;
  16245. #endif
  16246. return ret;
  16247. }
  16248. #endif /* WOLFCRYPT_HAVE_SRP */
  16249. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  16250. #if !defined(NO_AES) && !defined(WOLFCRYPT_ONLY)
  16251. static int openssl_aes_test(void)
  16252. {
  16253. #ifdef HAVE_AES_CBC
  16254. #ifdef WOLFSSL_AES_128
  16255. {
  16256. /* EVP_CipherUpdate test */
  16257. WOLFSSL_SMALL_STACK_STATIC const byte cbcPlain[] =
  16258. {
  16259. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  16260. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  16261. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  16262. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  16263. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  16264. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  16265. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  16266. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  16267. };
  16268. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  16269. "0123456789abcdef "; /* align */
  16270. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  16271. "1234567890abcdef "; /* align */
  16272. byte cipher[AES_BLOCK_SIZE * 4];
  16273. byte plain [AES_BLOCK_SIZE * 4];
  16274. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16275. EVP_CIPHER_CTX *en = wolfSSL_EVP_CIPHER_CTX_new();
  16276. EVP_CIPHER_CTX *de = wolfSSL_EVP_CIPHER_CTX_new();
  16277. #else
  16278. EVP_CIPHER_CTX en[1];
  16279. EVP_CIPHER_CTX de[1];
  16280. #endif
  16281. int outlen ;
  16282. int total = 0;
  16283. int i;
  16284. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16285. if ((en == NULL) || (de == NULL))
  16286. return MEMORY_E;
  16287. #endif
  16288. EVP_CIPHER_CTX_init(en);
  16289. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  16290. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  16291. return -8400;
  16292. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen,
  16293. (byte*)cbcPlain, 9) == 0)
  16294. return -8401;
  16295. if (outlen != 0)
  16296. return -8402;
  16297. total += outlen;
  16298. if (EVP_CipherUpdate(en, (byte*)&cipher[total], &outlen,
  16299. (byte*)&cbcPlain[9] , 9) == 0)
  16300. return -8403;
  16301. if (outlen != 16)
  16302. return -8404;
  16303. total += outlen;
  16304. if (EVP_CipherFinal(en, (byte*)&cipher[total], &outlen) == 0)
  16305. return -8405;
  16306. if (outlen != 16)
  16307. return -8406;
  16308. total += outlen;
  16309. if (total != 32)
  16310. return 3408;
  16311. total = 0;
  16312. EVP_CIPHER_CTX_init(de);
  16313. if (EVP_CipherInit(de, EVP_aes_128_cbc(),
  16314. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  16315. return -8407;
  16316. if (EVP_CipherUpdate(de, (byte*)plain, &outlen, (byte*)cipher, 6) == 0)
  16317. return -8408;
  16318. if (outlen != 0)
  16319. return -8409;
  16320. total += outlen;
  16321. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen,
  16322. (byte*)&cipher[6], 12) == 0)
  16323. return -8410;
  16324. if (outlen != 0)
  16325. total += outlen;
  16326. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen,
  16327. (byte*)&cipher[6+12], 14) == 0)
  16328. return -8411;
  16329. if (outlen != 16)
  16330. return -8412;
  16331. total += outlen;
  16332. if (EVP_CipherFinal(de, (byte*)&plain[total], &outlen) == 0)
  16333. return -8413;
  16334. if (outlen != 2)
  16335. return -8414;
  16336. total += outlen;
  16337. if (total != 18)
  16338. return 3427;
  16339. if (XMEMCMP(plain, cbcPlain, 18))
  16340. return -8415;
  16341. /* test with encrypting/decrypting more than 16 bytes at once */
  16342. total = 0;
  16343. EVP_CIPHER_CTX_init(en);
  16344. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  16345. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  16346. return -8416;
  16347. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen,
  16348. (byte*)cbcPlain, 17) == 0)
  16349. return -8417;
  16350. if (outlen != 16)
  16351. return -8418;
  16352. total += outlen;
  16353. if (EVP_CipherUpdate(en, (byte*)&cipher[total], &outlen,
  16354. (byte*)&cbcPlain[17] , 1) == 0)
  16355. return -8419;
  16356. if (outlen != 0)
  16357. return -8420;
  16358. total += outlen;
  16359. if (EVP_CipherFinal(en, (byte*)&cipher[total], &outlen) == 0)
  16360. return -8421;
  16361. if (outlen != 16)
  16362. return -8422;
  16363. total += outlen;
  16364. if (total != 32)
  16365. return -8423;
  16366. total = 0;
  16367. EVP_CIPHER_CTX_init(de);
  16368. if (EVP_CipherInit(de, EVP_aes_128_cbc(),
  16369. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  16370. return -8424;
  16371. if (EVP_CipherUpdate(de, (byte*)plain, &outlen, (byte*)cipher, 17) == 0)
  16372. return -8425;
  16373. if (outlen != 16)
  16374. return -8426;
  16375. total += outlen;
  16376. /* final call on non block size should fail */
  16377. if (EVP_CipherFinal(de, (byte*)&plain[total], &outlen) != 0)
  16378. return -8427;
  16379. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen,
  16380. (byte*)&cipher[17], 1) == 0)
  16381. return -8428;
  16382. if (outlen != 0)
  16383. total += outlen;
  16384. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen,
  16385. (byte*)&cipher[17+1], 14) == 0)
  16386. return -8429;
  16387. if (outlen != 0)
  16388. return -8430;
  16389. total += outlen;
  16390. if (EVP_CipherFinal(de, (byte*)&plain[total], &outlen) == 0)
  16391. return -8431;
  16392. if (outlen != 2)
  16393. return -8432;
  16394. total += outlen;
  16395. if (total != 18)
  16396. return -8433;
  16397. if (XMEMCMP(plain, cbcPlain, 18))
  16398. return -8434;
  16399. /* test byte by byte decrypt */
  16400. for (i = 0; i < AES_BLOCK_SIZE * 3; i++) {
  16401. plain[i] = i;
  16402. }
  16403. total = 0;
  16404. EVP_CIPHER_CTX_init(en);
  16405. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  16406. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  16407. return -8435;
  16408. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen,
  16409. (byte*)plain, AES_BLOCK_SIZE * 3) == 0)
  16410. return -8436;
  16411. if (outlen != AES_BLOCK_SIZE * 3)
  16412. return -8437;
  16413. total += outlen;
  16414. if (EVP_CipherFinal(en, (byte*)&cipher[total], &outlen) == 0)
  16415. return -8438;
  16416. if (outlen != AES_BLOCK_SIZE)
  16417. return -8439;
  16418. total += outlen;
  16419. if (total != sizeof(plain))
  16420. return -8440;
  16421. total = 0;
  16422. EVP_CIPHER_CTX_init(de);
  16423. if (EVP_CipherInit(de, EVP_aes_128_cbc(),
  16424. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  16425. return -8441;
  16426. for (i = 0; i < AES_BLOCK_SIZE * 4; i++) {
  16427. if (EVP_CipherUpdate(de, (byte*)plain + total, &outlen,
  16428. (byte*)cipher + i, 1) == 0)
  16429. return -8442;
  16430. if (outlen > 0) {
  16431. int j;
  16432. total += outlen;
  16433. for (j = 0; j < total; j++) {
  16434. if (plain[j] != j) {
  16435. return -8443;
  16436. }
  16437. }
  16438. }
  16439. }
  16440. if (EVP_CipherFinal(de, (byte*)&plain[total], &outlen) == 0)
  16441. return -8444;
  16442. total += outlen;
  16443. if (total != AES_BLOCK_SIZE * 3) {
  16444. return -8445;
  16445. }
  16446. for (i = 0; i < AES_BLOCK_SIZE * 3; i++) {
  16447. if (plain[i] != i) {
  16448. return -8446;
  16449. }
  16450. }
  16451. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16452. wolfSSL_EVP_CIPHER_CTX_free(en);
  16453. wolfSSL_EVP_CIPHER_CTX_free(de);
  16454. #endif
  16455. }
  16456. /* set buffers to be exact size to catch potential over read/write */
  16457. {
  16458. /* EVP_CipherUpdate test */
  16459. WOLFSSL_SMALL_STACK_STATIC const byte cbcPlain[] =
  16460. {
  16461. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  16462. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  16463. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  16464. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  16465. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  16466. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  16467. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  16468. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  16469. };
  16470. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  16471. "0123456789abcdef "; /* align */
  16472. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  16473. "1234567890abcdef "; /* align */
  16474. #define EVP_TEST_BUF_SZ 18
  16475. #define EVP_TEST_BUF_PAD 32
  16476. byte cipher[EVP_TEST_BUF_SZ];
  16477. byte plain [EVP_TEST_BUF_SZ];
  16478. byte padded[EVP_TEST_BUF_PAD];
  16479. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16480. EVP_CIPHER_CTX *en = wolfSSL_EVP_CIPHER_CTX_new();
  16481. EVP_CIPHER_CTX *de = wolfSSL_EVP_CIPHER_CTX_new();
  16482. #else
  16483. EVP_CIPHER_CTX en[1];
  16484. EVP_CIPHER_CTX de[1];
  16485. #endif
  16486. int outlen ;
  16487. int total = 0;
  16488. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16489. if ((en == NULL) || (de == NULL))
  16490. return MEMORY_E;
  16491. #endif
  16492. EVP_CIPHER_CTX_init(en);
  16493. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  16494. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  16495. return -8447;
  16496. if (EVP_CIPHER_CTX_set_padding(en, 0) != 1)
  16497. return -8448;
  16498. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen,
  16499. (byte*)cbcPlain, EVP_TEST_BUF_SZ) == 0)
  16500. return -8449;
  16501. if (outlen != 16)
  16502. return -8450;
  16503. total += outlen;
  16504. /* should fail here */
  16505. if (EVP_CipherFinal(en, (byte*)&cipher[total], &outlen) != 0)
  16506. return -8451;
  16507. /* turn padding back on and do successful encrypt */
  16508. total = 0;
  16509. EVP_CIPHER_CTX_init(en);
  16510. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  16511. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  16512. return -8452;
  16513. if (EVP_CIPHER_CTX_set_padding(en, 1) != 1)
  16514. return -8453;
  16515. if (EVP_CipherUpdate(en, (byte*)padded, &outlen,
  16516. (byte*)cbcPlain, EVP_TEST_BUF_SZ) == 0)
  16517. return -8454;
  16518. if (outlen != 16)
  16519. return -8455;
  16520. total += outlen;
  16521. if (EVP_CipherFinal(en, (byte*)&padded[total], &outlen) == 0)
  16522. return -8456;
  16523. total += outlen;
  16524. if (total != 32)
  16525. return -8457;
  16526. XMEMCPY(cipher, padded, EVP_TEST_BUF_SZ);
  16527. /* test out of bounds read on buffers w/o padding during decryption */
  16528. total = 0;
  16529. EVP_CIPHER_CTX_init(de);
  16530. if (EVP_CipherInit(de, EVP_aes_128_cbc(),
  16531. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  16532. return -8458;
  16533. if (EVP_CIPHER_CTX_set_padding(de, 0) != 1)
  16534. return -8459;
  16535. if (EVP_CipherUpdate(de, (byte*)plain, &outlen, (byte*)cipher,
  16536. EVP_TEST_BUF_SZ) == 0)
  16537. return -8460;
  16538. if (outlen != 16)
  16539. return -8461;
  16540. total += outlen;
  16541. /* should fail since not using padding */
  16542. if (EVP_CipherFinal(de, (byte*)&plain[total], &outlen) != 0)
  16543. return -8462;
  16544. total = 0;
  16545. EVP_CIPHER_CTX_init(de);
  16546. if (EVP_CipherInit(de, EVP_aes_128_cbc(),
  16547. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  16548. return -8463;
  16549. if (EVP_CIPHER_CTX_set_padding(de, 1) != 1)
  16550. return -8464;
  16551. if (EVP_CipherUpdate(de, (byte*)padded, &outlen, (byte*)padded,
  16552. EVP_TEST_BUF_PAD) == 0)
  16553. return -8465;
  16554. if (outlen != 16)
  16555. return -8466;
  16556. total += outlen;
  16557. if (EVP_CipherFinal(de, (byte*)&padded[total], &outlen) == 0)
  16558. return -8467;
  16559. if (XMEMCMP(padded, cbcPlain, EVP_TEST_BUF_SZ))
  16560. return -8468;
  16561. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16562. wolfSSL_EVP_CIPHER_CTX_free(en);
  16563. wolfSSL_EVP_CIPHER_CTX_free(de);
  16564. #endif
  16565. }
  16566. { /* evp_cipher test: EVP_aes_128_cbc */
  16567. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16568. EVP_CIPHER_CTX *ctx = wolfSSL_EVP_CIPHER_CTX_new();
  16569. #else
  16570. EVP_CIPHER_CTX ctx[1];
  16571. #endif
  16572. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = { /* "Now is the time for all " w/o trailing 0 */
  16573. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  16574. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  16575. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  16576. };
  16577. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  16578. {
  16579. 0x95,0x94,0x92,0x57,0x5f,0x42,0x81,0x53,
  16580. 0x2c,0xcc,0x9d,0x46,0x77,0xa2,0x33,0xcb
  16581. };
  16582. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  16583. "0123456789abcdef "; /* align */
  16584. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  16585. "1234567890abcdef "; /* align */
  16586. byte cipher[AES_BLOCK_SIZE * 4];
  16587. byte plain [AES_BLOCK_SIZE * 4];
  16588. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16589. if (ctx == NULL)
  16590. return MEMORY_E;
  16591. #endif
  16592. EVP_CIPHER_CTX_init(ctx);
  16593. if (EVP_CipherInit(ctx, EVP_aes_128_cbc(), key, iv, 1) == 0)
  16594. return -8469;
  16595. if (EVP_Cipher(ctx, cipher, (byte*)msg, 16) != 16)
  16596. return -8470;
  16597. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE))
  16598. return -8471;
  16599. EVP_CIPHER_CTX_init(ctx);
  16600. if (EVP_CipherInit(ctx, EVP_aes_128_cbc(), key, iv, 0) == 0)
  16601. return -8472;
  16602. if (EVP_Cipher(ctx, plain, cipher, 16) != 16)
  16603. return -8473;
  16604. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  16605. return -8474;
  16606. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16607. wolfSSL_EVP_CIPHER_CTX_free(ctx);
  16608. #endif
  16609. } /* end evp_cipher test: EVP_aes_128_cbc*/
  16610. #endif /* WOLFSSL_AES_128 */
  16611. #endif /* HAVE_AES_CBC */
  16612. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_256)
  16613. { /* evp_cipher test: EVP_aes_256_ecb*/
  16614. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16615. EVP_CIPHER_CTX *ctx = wolfSSL_EVP_CIPHER_CTX_new();
  16616. #else
  16617. EVP_CIPHER_CTX ctx[1];
  16618. #endif
  16619. WOLFSSL_SMALL_STACK_STATIC const byte msg[] =
  16620. {
  16621. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  16622. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  16623. };
  16624. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  16625. {
  16626. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  16627. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  16628. };
  16629. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  16630. {
  16631. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  16632. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  16633. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  16634. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  16635. };
  16636. byte cipher[AES_BLOCK_SIZE * 4];
  16637. byte plain [AES_BLOCK_SIZE * 4];
  16638. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16639. if (ctx == NULL)
  16640. return MEMORY_E;
  16641. #endif
  16642. EVP_CIPHER_CTX_init(ctx);
  16643. if (EVP_CipherInit(ctx, EVP_aes_256_ecb(), (unsigned char*)key, NULL, 1) == 0)
  16644. return -8475;
  16645. if (EVP_Cipher(ctx, cipher, (byte*)msg, 16) != 16)
  16646. return -8476;
  16647. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE))
  16648. return -8477;
  16649. EVP_CIPHER_CTX_init(ctx);
  16650. if (EVP_CipherInit(ctx, EVP_aes_256_ecb(), (unsigned char*)key, NULL, 0) == 0)
  16651. return -8478;
  16652. if (EVP_Cipher(ctx, plain, cipher, 16) != 16)
  16653. return -8479;
  16654. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  16655. return -8480;
  16656. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16657. wolfSSL_EVP_CIPHER_CTX_free(ctx);
  16658. #endif
  16659. } /* end evp_cipher test */
  16660. #endif /* HAVE_AES_ECB && WOLFSSL_AES_256 */
  16661. #if defined(WOLFSSL_AES_DIRECT) && defined(WOLFSSL_AES_256)
  16662. /* enable HAVE_AES_DECRYPT for AES_encrypt/decrypt */
  16663. {
  16664. /* Test: AES_encrypt/decrypt/set Key */
  16665. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16666. AES_KEY *enc = (AES_KEY *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  16667. #ifdef HAVE_AES_DECRYPT
  16668. AES_KEY *dec = (AES_KEY *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  16669. #endif
  16670. #else
  16671. AES_KEY enc[1];
  16672. #ifdef HAVE_AES_DECRYPT
  16673. AES_KEY dec[1];
  16674. #endif
  16675. #endif
  16676. WOLFSSL_SMALL_STACK_STATIC const byte msg[] =
  16677. {
  16678. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  16679. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  16680. };
  16681. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  16682. {
  16683. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  16684. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  16685. };
  16686. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  16687. {
  16688. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  16689. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  16690. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  16691. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  16692. };
  16693. byte plain[sizeof(msg)];
  16694. byte cipher[sizeof(msg)];
  16695. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16696. if (enc == NULL)
  16697. return MEMORY_E;
  16698. #ifdef HAVE_AES_DECRYPT
  16699. if (dec == NULL)
  16700. return MEMORY_E;
  16701. #endif
  16702. #endif
  16703. AES_set_encrypt_key(key, sizeof(key)*8, enc);
  16704. AES_set_decrypt_key(key, sizeof(key)*8, dec);
  16705. AES_encrypt(msg, cipher, enc);
  16706. #ifdef HAVE_AES_DECRYPT
  16707. AES_decrypt(cipher, plain, dec);
  16708. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  16709. return -8481;
  16710. #endif /* HAVE_AES_DECRYPT */
  16711. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE))
  16712. return -8482;
  16713. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16714. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  16715. #ifdef HAVE_AES_DECRYPT
  16716. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  16717. #endif
  16718. #endif
  16719. }
  16720. #endif /* WOLFSSL_AES_DIRECT && WOLFSSL_AES_256 */
  16721. /* EVP_Cipher with EVP_aes_xxx_ctr() */
  16722. #ifdef WOLFSSL_AES_COUNTER
  16723. {
  16724. byte plainBuff [64];
  16725. byte cipherBuff[64];
  16726. #ifdef WOLFSSL_AES_128
  16727. WOLFSSL_SMALL_STACK_STATIC const byte ctrKey[] =
  16728. {
  16729. 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,
  16730. 0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c
  16731. };
  16732. WOLFSSL_SMALL_STACK_STATIC const byte ctrIv[] =
  16733. {
  16734. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  16735. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  16736. };
  16737. WOLFSSL_SMALL_STACK_STATIC const byte ctrPlain[] =
  16738. {
  16739. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  16740. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  16741. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  16742. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  16743. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  16744. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  16745. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  16746. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  16747. };
  16748. WOLFSSL_SMALL_STACK_STATIC const byte ctrCipher[] =
  16749. {
  16750. 0x87,0x4d,0x61,0x91,0xb6,0x20,0xe3,0x26,
  16751. 0x1b,0xef,0x68,0x64,0x99,0x0d,0xb6,0xce,
  16752. 0x98,0x06,0xf6,0x6b,0x79,0x70,0xfd,0xff,
  16753. 0x86,0x17,0x18,0x7b,0xb9,0xff,0xfd,0xff,
  16754. 0x5a,0xe4,0xdf,0x3e,0xdb,0xd5,0xd3,0x5e,
  16755. 0x5b,0x4f,0x09,0x02,0x0d,0xb0,0x3e,0xab,
  16756. 0x1e,0x03,0x1d,0xda,0x2f,0xbe,0x03,0xd1,
  16757. 0x79,0x21,0x70,0xa0,0xf3,0x00,0x9c,0xee
  16758. };
  16759. WOLFSSL_SMALL_STACK_STATIC const byte oddCipher[] =
  16760. {
  16761. 0xb9,0xd7,0xcb,0x08,0xb0,0xe1,0x7b,0xa0,
  16762. 0xc2
  16763. };
  16764. #endif
  16765. /* test vector from "Recommendation for Block Cipher Modes of Operation"
  16766. * NIST Special Publication 800-38A */
  16767. #ifdef WOLFSSL_AES_192
  16768. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Key[] =
  16769. {
  16770. 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  16771. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  16772. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b
  16773. };
  16774. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Iv[] =
  16775. {
  16776. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  16777. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  16778. };
  16779. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Plain[] =
  16780. {
  16781. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  16782. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  16783. };
  16784. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Cipher[] =
  16785. {
  16786. 0x1a,0xbc,0x93,0x24,0x17,0x52,0x1c,0xa2,
  16787. 0x4f,0x2b,0x04,0x59,0xfe,0x7e,0x6e,0x0b
  16788. };
  16789. #endif /* WOLFSSL_AES_192 */
  16790. #ifdef WOLFSSL_AES_256
  16791. /* test vector from "Recommendation for Block Cipher Modes of Operation"
  16792. * NIST Special Publication 800-38A */
  16793. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Key[] =
  16794. {
  16795. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  16796. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  16797. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  16798. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  16799. };
  16800. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Iv[] =
  16801. {
  16802. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  16803. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  16804. };
  16805. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Plain[] =
  16806. {
  16807. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  16808. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  16809. };
  16810. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Cipher[] =
  16811. {
  16812. 0x60,0x1e,0xc3,0x13,0x77,0x57,0x89,0xa5,
  16813. 0xb7,0xa7,0xf5,0x04,0xbb,0xf3,0xd2,0x28
  16814. };
  16815. #endif /* WOLFSSL_AES_256 */
  16816. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16817. EVP_CIPHER_CTX *en = wolfSSL_EVP_CIPHER_CTX_new();
  16818. EVP_CIPHER_CTX *de = wolfSSL_EVP_CIPHER_CTX_new();
  16819. #else
  16820. EVP_CIPHER_CTX en[1];
  16821. EVP_CIPHER_CTX de[1];
  16822. #endif
  16823. #ifdef WOLFSSL_AES_128
  16824. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16825. EVP_CIPHER_CTX *p_en;
  16826. EVP_CIPHER_CTX *p_de;
  16827. #endif
  16828. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16829. if ((en == NULL) || (de == NULL))
  16830. return MEMORY_E;
  16831. #endif
  16832. EVP_CIPHER_CTX_init(en);
  16833. if (EVP_CipherInit(en, EVP_aes_128_ctr(),
  16834. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  16835. return -8483;
  16836. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctrPlain,
  16837. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  16838. return -8484;
  16839. EVP_CIPHER_CTX_init(de);
  16840. if (EVP_CipherInit(de, EVP_aes_128_ctr(),
  16841. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  16842. return -8485;
  16843. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff,
  16844. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  16845. return -8486;
  16846. if (XMEMCMP(cipherBuff, ctrCipher, AES_BLOCK_SIZE*4))
  16847. return -8487;
  16848. if (XMEMCMP(plainBuff, ctrPlain, AES_BLOCK_SIZE*4))
  16849. return -8488;
  16850. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16851. p_en = wolfSSL_EVP_CIPHER_CTX_new();
  16852. if (p_en == NULL)
  16853. return -8489;
  16854. p_de = wolfSSL_EVP_CIPHER_CTX_new();
  16855. if (p_de == NULL)
  16856. return -8490;
  16857. if (EVP_CipherInit(p_en, EVP_aes_128_ctr(),
  16858. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  16859. return -8491;
  16860. if (EVP_Cipher(p_en, (byte*)cipherBuff, (byte*)ctrPlain,
  16861. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  16862. return -8492;
  16863. if (EVP_CipherInit(p_de, EVP_aes_128_ctr(),
  16864. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  16865. return -8493;
  16866. if (EVP_Cipher(p_de, (byte*)plainBuff, (byte*)cipherBuff,
  16867. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  16868. return -8494;
  16869. wolfSSL_EVP_CIPHER_CTX_free(p_en);
  16870. wolfSSL_EVP_CIPHER_CTX_free(p_de);
  16871. #endif /* WOLFSSL_SMALL_STACK && !WOLFSSL_NO_MALLOC */
  16872. if (XMEMCMP(cipherBuff, ctrCipher, AES_BLOCK_SIZE*4))
  16873. return -8495;
  16874. if (XMEMCMP(plainBuff, ctrPlain, AES_BLOCK_SIZE*4))
  16875. return -8496;
  16876. EVP_CIPHER_CTX_init(en);
  16877. if (EVP_CipherInit(en, EVP_aes_128_ctr(),
  16878. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  16879. return -8497;
  16880. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctrPlain, 9) != 9)
  16881. return -8498;
  16882. EVP_CIPHER_CTX_init(de);
  16883. if (EVP_CipherInit(de, EVP_aes_128_ctr(),
  16884. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  16885. return -8499;
  16886. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff, 9) != 9)
  16887. return -8500;
  16888. if (XMEMCMP(plainBuff, ctrPlain, 9))
  16889. return -8501;
  16890. if (XMEMCMP(cipherBuff, ctrCipher, 9))
  16891. return -8502;
  16892. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctrPlain, 9) != 9)
  16893. return -8503;
  16894. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff, 9) != 9)
  16895. return -8504;
  16896. if (XMEMCMP(plainBuff, ctrPlain, 9))
  16897. return -8505;
  16898. if (XMEMCMP(cipherBuff, oddCipher, 9))
  16899. return -8506;
  16900. #endif /* WOLFSSL_AES_128 */
  16901. #ifdef WOLFSSL_AES_192
  16902. EVP_CIPHER_CTX_init(en);
  16903. if (EVP_CipherInit(en, EVP_aes_192_ctr(),
  16904. (unsigned char*)ctr192Key, (unsigned char*)ctr192Iv, 0) == 0)
  16905. return -8507;
  16906. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctr192Plain,
  16907. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  16908. return -8508;
  16909. EVP_CIPHER_CTX_init(de);
  16910. if (EVP_CipherInit(de, EVP_aes_192_ctr(),
  16911. (unsigned char*)ctr192Key, (unsigned char*)ctr192Iv, 0) == 0)
  16912. return -8509;
  16913. XMEMSET(plainBuff, 0, sizeof(plainBuff));
  16914. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff,
  16915. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  16916. return -8510;
  16917. if (XMEMCMP(plainBuff, ctr192Plain, sizeof(ctr192Plain)))
  16918. return -8511;
  16919. if (XMEMCMP(ctr192Cipher, cipherBuff, sizeof(ctr192Cipher)))
  16920. return -8512;
  16921. #endif /* WOLFSSL_AES_192 */
  16922. #ifdef WOLFSSL_AES_256
  16923. EVP_CIPHER_CTX_init(en);
  16924. if (EVP_CipherInit(en, EVP_aes_256_ctr(),
  16925. (unsigned char*)ctr256Key, (unsigned char*)ctr256Iv, 0) == 0)
  16926. return -8513;
  16927. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctr256Plain,
  16928. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  16929. return -8514;
  16930. EVP_CIPHER_CTX_init(de);
  16931. if (EVP_CipherInit(de, EVP_aes_256_ctr(),
  16932. (unsigned char*)ctr256Key, (unsigned char*)ctr256Iv, 0) == 0)
  16933. return -8515;
  16934. XMEMSET(plainBuff, 0, sizeof(plainBuff));
  16935. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff,
  16936. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  16937. return -8516;
  16938. if (XMEMCMP(plainBuff, ctr256Plain, sizeof(ctr256Plain)))
  16939. return -8517;
  16940. if (XMEMCMP(ctr256Cipher, cipherBuff, sizeof(ctr256Cipher)))
  16941. return -8518;
  16942. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16943. wolfSSL_EVP_CIPHER_CTX_free(en);
  16944. wolfSSL_EVP_CIPHER_CTX_free(de);
  16945. #endif
  16946. #endif /* WOLFSSL_AES_256 */
  16947. }
  16948. #endif /* HAVE_AES_COUNTER */
  16949. #if defined(WOLFSSL_AES_CFB) && defined(WOLFSSL_AES_128)
  16950. {
  16951. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16952. AES_KEY *enc = (AES_KEY *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  16953. AES_KEY *dec = (AES_KEY *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  16954. #else
  16955. AES_KEY enc[1];
  16956. AES_KEY dec[1];
  16957. #endif
  16958. WOLFSSL_SMALL_STACK_STATIC const byte setIv[] = {
  16959. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  16960. 0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f
  16961. };
  16962. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  16963. {
  16964. 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,
  16965. 0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c
  16966. };
  16967. WOLFSSL_SMALL_STACK_STATIC const byte cipher1[] =
  16968. {
  16969. 0x3b,0x3f,0xd9,0x2e,0xb7,0x2d,0xad,0x20,
  16970. 0x33,0x34,0x49,0xf8,0xe8,0x3c,0xfb,0x4a,
  16971. 0xc8,0xa6,0x45,0x37,0xa0,0xb3,0xa9,0x3f,
  16972. 0xcd,0xe3,0xcd,0xad,0x9f,0x1c,0xe5,0x8b
  16973. };
  16974. WOLFSSL_SMALL_STACK_STATIC const byte msg[] =
  16975. {
  16976. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  16977. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  16978. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  16979. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51
  16980. };
  16981. byte cipher[AES_BLOCK_SIZE * 2];
  16982. byte iv[AES_BLOCK_SIZE]; /* iv buffer is updeated by API */
  16983. int num = 0;
  16984. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16985. if ((enc == NULL) || (dec == NULL))
  16986. return MEMORY_E;
  16987. #endif
  16988. XMEMCPY(iv, setIv, sizeof(setIv));
  16989. wolfSSL_AES_set_encrypt_key(key, sizeof(key) * 8, enc);
  16990. wolfSSL_AES_set_encrypt_key(key, sizeof(key) * 8, dec);
  16991. wolfSSL_AES_cfb128_encrypt(msg, cipher, AES_BLOCK_SIZE - 1, enc, iv,
  16992. &num, AES_ENCRYPT);
  16993. if (XMEMCMP(cipher, cipher1, AES_BLOCK_SIZE - 1))
  16994. return -8519;
  16995. if (num != 15) /* should have used 15 of the 16 bytes */
  16996. return -8520;
  16997. wolfSSL_AES_cfb128_encrypt(msg + AES_BLOCK_SIZE - 1,
  16998. cipher + AES_BLOCK_SIZE - 1, AES_BLOCK_SIZE + 1, enc, iv,
  16999. &num, AES_ENCRYPT);
  17000. if (XMEMCMP(cipher, cipher1, AES_BLOCK_SIZE * 2))
  17001. return -8521;
  17002. if (num != 0)
  17003. return -8522;
  17004. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17005. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  17006. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  17007. #endif
  17008. }
  17009. #endif /* WOLFSSL_AES_CFB && WOLFSSL_AES_128 */
  17010. return 0;
  17011. }
  17012. #endif /* !defined(NO_AES) && !defined(WOLFCRYPT_ONLY) */
  17013. WOLFSSL_TEST_SUBROUTINE int openssl_test(void)
  17014. {
  17015. int ret;
  17016. EVP_MD_CTX md_ctx;
  17017. testVector a, b, c, d, e, f;
  17018. byte hash[WC_SHA256_DIGEST_SIZE*2]; /* max size */
  17019. a.inLen = 0;
  17020. b.inLen = c.inLen = d.inLen = e.inLen = f.inLen = a.inLen;
  17021. (void)a;
  17022. (void)b;
  17023. (void)c;
  17024. (void)d;
  17025. (void)e;
  17026. (void)f;
  17027. /* test malloc / free , 10 is an arbitrary amount of memory chosen */
  17028. {
  17029. byte* p;
  17030. p = (byte*)CRYPTO_malloc(10, "", 0);
  17031. if (p == NULL) {
  17032. return -8600;
  17033. }
  17034. XMEMSET(p, 0, 10);
  17035. CRYPTO_free(p, "", 0);
  17036. }
  17037. #ifndef NO_MD5
  17038. a.input = "1234567890123456789012345678901234567890123456789012345678"
  17039. "9012345678901234567890";
  17040. a.output = "\x57\xed\xf4\xa2\x2b\xe3\xc9\x55\xac\x49\xda\x2e\x21\x07\xb6"
  17041. "\x7a";
  17042. a.inLen = XSTRLEN(a.input);
  17043. a.outLen = WC_MD5_DIGEST_SIZE;
  17044. EVP_MD_CTX_init(&md_ctx);
  17045. ret = EVP_DigestInit(&md_ctx, EVP_md5());
  17046. if (ret == WOLFSSL_SUCCESS) {
  17047. ret = EVP_DigestUpdate(&md_ctx, a.input, (unsigned long)a.inLen);
  17048. }
  17049. if (ret == WOLFSSL_SUCCESS) {
  17050. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  17051. }
  17052. EVP_MD_CTX_cleanup(&md_ctx);
  17053. if (ret != WOLFSSL_SUCCESS)
  17054. return -18601;
  17055. if (XMEMCMP(hash, a.output, WC_MD5_DIGEST_SIZE) != 0)
  17056. return -8601;
  17057. #endif /* NO_MD5 */
  17058. #ifndef NO_SHA
  17059. b.input = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  17060. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  17061. "aaaaaaaaaa";
  17062. b.output = "\xAD\x5B\x3F\xDB\xCB\x52\x67\x78\xC2\x83\x9D\x2F\x15\x1E\xA7"
  17063. "\x53\x99\x5E\x26\xA0";
  17064. b.inLen = XSTRLEN(b.input);
  17065. b.outLen = WC_SHA_DIGEST_SIZE;
  17066. EVP_MD_CTX_init(&md_ctx);
  17067. ret = EVP_DigestInit(&md_ctx, EVP_sha1());
  17068. if (ret == WOLFSSL_SUCCESS) {
  17069. ret = EVP_DigestUpdate(&md_ctx, b.input, (unsigned long)b.inLen);
  17070. if (ret == WOLFSSL_SUCCESS)
  17071. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  17072. }
  17073. EVP_MD_CTX_cleanup(&md_ctx);
  17074. if (ret != WOLFSSL_SUCCESS)
  17075. return -18602;
  17076. if (XMEMCMP(hash, b.output, WC_SHA_DIGEST_SIZE) != 0)
  17077. return -8602;
  17078. #endif /* NO_SHA */
  17079. #ifdef WOLFSSL_SHA224
  17080. e.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  17081. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  17082. e.output = "\xc9\x7c\xa9\xa5\x59\x85\x0c\xe9\x7a\x04\xa9\x6d\xef\x6d\x99"
  17083. "\xa9\xe0\xe0\xe2\xab\x14\xe6\xb8\xdf\x26\x5f\xc0\xb3";
  17084. e.inLen = XSTRLEN(e.input);
  17085. e.outLen = WC_SHA224_DIGEST_SIZE;
  17086. EVP_MD_CTX_init(&md_ctx);
  17087. ret = EVP_DigestInit(&md_ctx, EVP_sha224());
  17088. if (ret == WOLFSSL_SUCCESS) {
  17089. ret = EVP_DigestUpdate(&md_ctx, e.input, (unsigned long)e.inLen);
  17090. if (ret == WOLFSSL_SUCCESS)
  17091. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  17092. }
  17093. EVP_MD_CTX_cleanup(&md_ctx);
  17094. if (ret != WOLFSSL_SUCCESS ||
  17095. XMEMCMP(hash, e.output, WC_SHA224_DIGEST_SIZE) != 0) {
  17096. return -8603;
  17097. }
  17098. #endif /* WOLFSSL_SHA224 */
  17099. #ifndef NO_SHA256
  17100. d.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  17101. d.output = "\x24\x8D\x6A\x61\xD2\x06\x38\xB8\xE5\xC0\x26\x93\x0C\x3E\x60"
  17102. "\x39\xA3\x3C\xE4\x59\x64\xFF\x21\x67\xF6\xEC\xED\xD4\x19\xDB"
  17103. "\x06\xC1";
  17104. d.inLen = XSTRLEN(d.input);
  17105. d.outLen = WC_SHA256_DIGEST_SIZE;
  17106. EVP_MD_CTX_init(&md_ctx);
  17107. ret = EVP_DigestInit(&md_ctx, EVP_sha256());
  17108. if (ret == WOLFSSL_SUCCESS) {
  17109. ret = EVP_DigestUpdate(&md_ctx, d.input, (unsigned long)d.inLen);
  17110. if (ret == WOLFSSL_SUCCESS)
  17111. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  17112. }
  17113. EVP_MD_CTX_cleanup(&md_ctx);
  17114. if (ret != WOLFSSL_SUCCESS ||
  17115. XMEMCMP(hash, d.output, WC_SHA256_DIGEST_SIZE) != 0) {
  17116. return -8604;
  17117. }
  17118. #endif /* !NO_SHA256 */
  17119. #ifdef WOLFSSL_SHA384
  17120. e.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  17121. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  17122. e.output = "\x09\x33\x0c\x33\xf7\x11\x47\xe8\x3d\x19\x2f\xc7\x82\xcd\x1b"
  17123. "\x47\x53\x11\x1b\x17\x3b\x3b\x05\xd2\x2f\xa0\x80\x86\xe3\xb0"
  17124. "\xf7\x12\xfc\xc7\xc7\x1a\x55\x7e\x2d\xb9\x66\xc3\xe9\xfa\x91"
  17125. "\x74\x60\x39";
  17126. e.inLen = XSTRLEN(e.input);
  17127. e.outLen = WC_SHA384_DIGEST_SIZE;
  17128. EVP_MD_CTX_init(&md_ctx);
  17129. ret = EVP_DigestInit(&md_ctx, EVP_sha384());
  17130. if (ret == WOLFSSL_SUCCESS) {
  17131. ret = EVP_DigestUpdate(&md_ctx, e.input, (unsigned long)e.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, e.output, WC_SHA384_DIGEST_SIZE) != 0) {
  17138. return -8605;
  17139. }
  17140. #endif /* WOLFSSL_SHA384 */
  17141. #ifdef WOLFSSL_SHA512
  17142. f.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  17143. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  17144. f.output = "\x8e\x95\x9b\x75\xda\xe3\x13\xda\x8c\xf4\xf7\x28\x14\xfc\x14"
  17145. "\x3f\x8f\x77\x79\xc6\xeb\x9f\x7f\xa1\x72\x99\xae\xad\xb6\x88"
  17146. "\x90\x18\x50\x1d\x28\x9e\x49\x00\xf7\xe4\x33\x1b\x99\xde\xc4"
  17147. "\xb5\x43\x3a\xc7\xd3\x29\xee\xb6\xdd\x26\x54\x5e\x96\xe5\x5b"
  17148. "\x87\x4b\xe9\x09";
  17149. f.inLen = XSTRLEN(f.input);
  17150. f.outLen = WC_SHA512_DIGEST_SIZE;
  17151. EVP_MD_CTX_init(&md_ctx);
  17152. ret = EVP_DigestInit(&md_ctx, EVP_sha512());
  17153. if (ret == WOLFSSL_SUCCESS) {
  17154. ret = EVP_DigestUpdate(&md_ctx, f.input, (unsigned long)f.inLen);
  17155. if (ret == WOLFSSL_SUCCESS)
  17156. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  17157. }
  17158. EVP_MD_CTX_cleanup(&md_ctx);
  17159. if (ret != WOLFSSL_SUCCESS ||
  17160. XMEMCMP(hash, f.output, WC_SHA512_DIGEST_SIZE) != 0) {
  17161. return -8606;
  17162. }
  17163. #endif /* WOLFSSL_SHA512 */
  17164. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  17165. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  17166. f.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  17167. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  17168. f.output = "\x23\xfe\xc5\xbb\x94\xd6\x0b\x23\x30\x81\x92\x64\x0b\x0c\x45"
  17169. "\x33\x35\xd6\x64\x73\x4f\xe4\x0e\x72\x68\x67\x4a\xf9";
  17170. f.inLen = XSTRLEN(f.input);
  17171. f.outLen = WC_SHA512_224_DIGEST_SIZE;
  17172. EVP_MD_CTX_init(&md_ctx);
  17173. ret = EVP_DigestInit(&md_ctx, EVP_sha512_224());
  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_224_DIGEST_SIZE) != 0) {
  17182. return -8722;
  17183. }
  17184. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_224 */
  17185. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  17186. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  17187. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  17188. f.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  17189. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  17190. f.output = "\x39\x28\xe1\x84\xfb\x86\x90\xf8\x40\xda\x39\x88\x12\x1d\x31"
  17191. "\xbe\x65\xcb\x9d\x3e\xf8\x3e\xe6\x14\x6f\xea\xc8\x61\xe1\x9b"
  17192. "\x56\x3a";
  17193. f.inLen = XSTRLEN(f.input);
  17194. f.outLen = WC_SHA512_256_DIGEST_SIZE;
  17195. EVP_MD_CTX_init(&md_ctx);
  17196. ret = EVP_DigestInit(&md_ctx, EVP_sha512_256());
  17197. if (ret == WOLFSSL_SUCCESS) {
  17198. ret = EVP_DigestUpdate(&md_ctx, f.input, (unsigned long)f.inLen);
  17199. if (ret == WOLFSSL_SUCCESS)
  17200. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  17201. }
  17202. EVP_MD_CTX_cleanup(&md_ctx);
  17203. if (ret != WOLFSSL_SUCCESS ||
  17204. XMEMCMP(hash, f.output, WC_SHA512_256_DIGEST_SIZE) != 0) {
  17205. return -8723;
  17206. }
  17207. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_224 */
  17208. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  17209. #ifdef WOLFSSL_SHA3
  17210. #ifndef WOLFSSL_NOSHA3_224
  17211. e.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  17212. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  17213. e.output = "\x54\x3e\x68\x68\xe1\x66\x6c\x1a\x64\x36\x30\xdf\x77\x36\x7a"
  17214. "\xe5\xa6\x2a\x85\x07\x0a\x51\xc1\x4c\xbf\x66\x5c\xbc";
  17215. e.inLen = XSTRLEN(e.input);
  17216. e.outLen = WC_SHA3_224_DIGEST_SIZE;
  17217. EVP_MD_CTX_init(&md_ctx);
  17218. ret = EVP_DigestInit(&md_ctx, EVP_sha3_224());
  17219. if (ret == WOLFSSL_SUCCESS) {
  17220. ret = EVP_DigestUpdate(&md_ctx, e.input, (unsigned long)e.inLen);
  17221. if (ret == WOLFSSL_SUCCESS)
  17222. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  17223. }
  17224. EVP_MD_CTX_cleanup(&md_ctx);
  17225. if (ret != WOLFSSL_SUCCESS ||
  17226. XMEMCMP(hash, e.output, WC_SHA3_224_DIGEST_SIZE) != 0) {
  17227. return -8607;
  17228. }
  17229. #endif /* WOLFSSL_NOSHA3_224 */
  17230. #ifndef WOLFSSL_NOSHA3_256
  17231. d.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  17232. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  17233. d.output = "\x91\x6f\x60\x61\xfe\x87\x97\x41\xca\x64\x69\xb4\x39\x71\xdf"
  17234. "\xdb\x28\xb1\xa3\x2d\xc3\x6c\xb3\x25\x4e\x81\x2b\xe2\x7a\xad"
  17235. "\x1d\x18";
  17236. d.inLen = XSTRLEN(d.input);
  17237. d.outLen = WC_SHA3_256_DIGEST_SIZE;
  17238. EVP_MD_CTX_init(&md_ctx);
  17239. ret = EVP_DigestInit(&md_ctx, EVP_sha3_256());
  17240. if (ret == WOLFSSL_SUCCESS) {
  17241. ret = EVP_DigestUpdate(&md_ctx, d.input, (unsigned long)d.inLen);
  17242. if (ret == WOLFSSL_SUCCESS)
  17243. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  17244. }
  17245. EVP_MD_CTX_cleanup(&md_ctx);
  17246. if (ret != WOLFSSL_SUCCESS ||
  17247. XMEMCMP(hash, d.output, WC_SHA3_256_DIGEST_SIZE) != 0) {
  17248. return -8608;
  17249. }
  17250. #endif /* WOLFSSL_NOSHA3_256 */
  17251. e.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  17252. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  17253. e.output = "\x79\x40\x7d\x3b\x59\x16\xb5\x9c\x3e\x30\xb0\x98\x22\x97\x47"
  17254. "\x91\xc3\x13\xfb\x9e\xcc\x84\x9e\x40\x6f\x23\x59\x2d\x04\xf6"
  17255. "\x25\xdc\x8c\x70\x9b\x98\xb4\x3b\x38\x52\xb3\x37\x21\x61\x79"
  17256. "\xaa\x7f\xc7";
  17257. e.inLen = XSTRLEN(e.input);
  17258. e.outLen = WC_SHA3_384_DIGEST_SIZE;
  17259. EVP_MD_CTX_init(&md_ctx);
  17260. ret = EVP_DigestInit(&md_ctx, EVP_sha3_384());
  17261. if (ret == WOLFSSL_SUCCESS) {
  17262. ret = EVP_DigestUpdate(&md_ctx, e.input, (unsigned long)e.inLen);
  17263. if (ret == WOLFSSL_SUCCESS)
  17264. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  17265. }
  17266. EVP_MD_CTX_cleanup(&md_ctx);
  17267. if (ret != WOLFSSL_SUCCESS ||
  17268. XMEMCMP(hash, e.output, WC_SHA3_384_DIGEST_SIZE) != 0) {
  17269. return -8609;
  17270. }
  17271. #ifndef WOLFSSL_NOSHA3_512
  17272. f.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  17273. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  17274. f.output = "\xaf\xeb\xb2\xef\x54\x2e\x65\x79\xc5\x0c\xad\x06\xd2\xe5\x78"
  17275. "\xf9\xf8\xdd\x68\x81\xd7\xdc\x82\x4d\x26\x36\x0f\xee\xbf\x18"
  17276. "\xa4\xfa\x73\xe3\x26\x11\x22\x94\x8e\xfc\xfd\x49\x2e\x74\xe8"
  17277. "\x2e\x21\x89\xed\x0f\xb4\x40\xd1\x87\xf3\x82\x27\x0c\xb4\x55"
  17278. "\xf2\x1d\xd1\x85";
  17279. f.inLen = XSTRLEN(f.input);
  17280. f.outLen = WC_SHA3_512_DIGEST_SIZE;
  17281. EVP_MD_CTX_init(&md_ctx);
  17282. ret = EVP_DigestInit(&md_ctx, EVP_sha3_512());
  17283. if (ret == WOLFSSL_SUCCESS) {
  17284. ret = EVP_DigestUpdate(&md_ctx, f.input, (unsigned long)f.inLen);
  17285. if (ret == WOLFSSL_SUCCESS)
  17286. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  17287. }
  17288. EVP_MD_CTX_cleanup(&md_ctx);
  17289. if (ret != WOLFSSL_SUCCESS ||
  17290. XMEMCMP(hash, f.output, WC_SHA3_512_DIGEST_SIZE) != 0) {
  17291. return -8610;
  17292. }
  17293. #endif /* WOLFSSL_NOSHA3_512 */
  17294. #endif /* WOLFSSL_SHA3 */
  17295. #ifndef WC_NO_RNG
  17296. if (RAND_bytes(hash, sizeof(hash)) != WOLFSSL_SUCCESS)
  17297. return -8611;
  17298. #endif
  17299. #ifndef NO_MD5
  17300. c.input = "what do ya want for nothing?";
  17301. c.output = "\x55\x78\xe8\x48\x4b\xcc\x93\x80\x93\xec\x53\xaf\x22\xd6\x14"
  17302. "\x76";
  17303. c.inLen = XSTRLEN(c.input);
  17304. c.outLen = WC_MD5_DIGEST_SIZE;
  17305. #if defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)
  17306. /* Expect failure with MD5 + HMAC when using FIPS 140-3. */
  17307. if (HMAC(EVP_md5(), "JefeJefeJefeJefe", 16, (byte*)c.input, (int)c.inLen,
  17308. hash, 0) != NULL)
  17309. #else
  17310. if (HMAC(EVP_md5(), "JefeJefeJefeJefe", 16, (byte*)c.input, (int)c.inLen,
  17311. hash, 0) == NULL ||
  17312. XMEMCMP(hash, c.output, WC_MD5_DIGEST_SIZE) != 0)
  17313. #endif
  17314. {
  17315. return -8612;
  17316. }
  17317. #endif /* NO_MD5 */
  17318. #ifndef NO_DES3
  17319. { /* des test */
  17320. WOLFSSL_SMALL_STACK_STATIC const byte vector[] = { /* "now is the time for all " w/o trailing 0 */
  17321. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  17322. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  17323. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  17324. };
  17325. byte plain[24];
  17326. byte cipher[24];
  17327. const_DES_cblock key = {
  17328. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
  17329. };
  17330. DES_cblock iv = {
  17331. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef
  17332. };
  17333. DES_key_schedule sched;
  17334. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  17335. 0x8b,0x7c,0x52,0xb0,0x01,0x2b,0x6c,0xb8,
  17336. 0x4f,0x0f,0xeb,0xf3,0xfb,0x5f,0x86,0x73,
  17337. 0x15,0x85,0xb3,0x22,0x4b,0x86,0x2b,0x4b
  17338. };
  17339. DES_key_sched(&key, &sched);
  17340. DES_cbc_encrypt(vector, cipher, sizeof(vector), &sched, &iv, DES_ENCRYPT);
  17341. DES_cbc_encrypt(cipher, plain, sizeof(vector), &sched, &iv, DES_DECRYPT);
  17342. if (XMEMCMP(plain, vector, sizeof(vector)) != 0)
  17343. return -8613;
  17344. if (XMEMCMP(cipher, verify, sizeof(verify)) != 0)
  17345. return -8614;
  17346. /* test changing iv */
  17347. DES_ncbc_encrypt(vector, cipher, 8, &sched, &iv, DES_ENCRYPT);
  17348. DES_ncbc_encrypt(vector + 8, cipher + 8, 16, &sched, &iv, DES_ENCRYPT);
  17349. if (XMEMCMP(cipher, verify, sizeof(verify)) != 0)
  17350. return -8615;
  17351. } /* end des test */
  17352. #endif /* NO_DES3 */
  17353. #if !defined(NO_AES) && !defined(WOLFCRYPT_ONLY)
  17354. if ((ret = openssl_aes_test()) != 0) {
  17355. return ret;
  17356. }
  17357. #if defined(WOLFSSL_AES_128) && defined(HAVE_AES_CBC)
  17358. { /* evp_cipher test: EVP_aes_128_cbc */
  17359. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17360. EVP_CIPHER_CTX *ctx = wolfSSL_EVP_CIPHER_CTX_new();
  17361. #else
  17362. EVP_CIPHER_CTX ctx[1];
  17363. #endif
  17364. int idx, cipherSz, plainSz;
  17365. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = { /* "Now is the time for all " w/o trailing 0 */
  17366. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  17367. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  17368. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  17369. };
  17370. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  17371. 0x95,0x94,0x92,0x57,0x5f,0x42,0x81,0x53,
  17372. 0x2c,0xcc,0x9d,0x46,0x77,0xa2,0x33,0xcb,
  17373. 0x3b,0x5d,0x41,0x97,0x94,0x25,0xa4,0xb4,
  17374. 0xae,0x7b,0x34,0xd0,0x3f,0x0c,0xbc,0x06
  17375. };
  17376. WOLFSSL_SMALL_STACK_STATIC const byte verify2[] = {
  17377. 0x95,0x94,0x92,0x57,0x5f,0x42,0x81,0x53,
  17378. 0x2c,0xcc,0x9d,0x46,0x77,0xa2,0x33,0xcb,
  17379. 0x7d,0x37,0x7b,0x0b,0x44,0xaa,0xb5,0xf0,
  17380. 0x5f,0x34,0xb4,0xde,0xb5,0xbd,0x2a,0xbb
  17381. };
  17382. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  17383. "0123456789abcdef "; /* align */
  17384. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  17385. "1234567890abcdef "; /* align */
  17386. byte cipher[AES_BLOCK_SIZE * 4];
  17387. byte plain [AES_BLOCK_SIZE * 4];
  17388. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17389. if (ctx == NULL)
  17390. return MEMORY_E;
  17391. #endif
  17392. cipherSz = 0;
  17393. EVP_CIPHER_CTX_init(ctx);
  17394. ret = EVP_CipherInit(ctx, EVP_aes_128_cbc(), key, iv, 1);
  17395. if (ret == WOLFSSL_SUCCESS) {
  17396. ret = EVP_CipherUpdate(ctx, cipher, &idx, (byte*)msg, sizeof(msg));
  17397. if (ret == WOLFSSL_SUCCESS)
  17398. cipherSz += idx;
  17399. }
  17400. if (ret == WOLFSSL_SUCCESS) {
  17401. ret = EVP_CipherFinal(ctx, cipher + cipherSz, &idx);
  17402. if (ret == WOLFSSL_SUCCESS)
  17403. cipherSz += idx;
  17404. }
  17405. EVP_CIPHER_CTX_cleanup(ctx);
  17406. if (ret != WOLFSSL_SUCCESS)
  17407. return -8617;
  17408. if (cipherSz != (int)sizeof(verify) || XMEMCMP(cipher, verify, cipherSz))
  17409. return -8618;
  17410. /* check partial decrypt (not enough padding for full block) */
  17411. plainSz = 0;
  17412. EVP_CIPHER_CTX_init(ctx);
  17413. ret = EVP_CipherInit(ctx, EVP_aes_128_cbc(), key, iv, 0);
  17414. if (ret == WOLFSSL_SUCCESS) {
  17415. ret = EVP_CipherUpdate(ctx, plain, &idx, cipher, 1);
  17416. if (ret == WOLFSSL_SUCCESS)
  17417. plainSz += idx;
  17418. }
  17419. if (ret == WOLFSSL_SUCCESS) {
  17420. /* this test should fail... not enough padding for full block */
  17421. ret = EVP_CipherFinal(ctx, plain + plainSz, &idx);
  17422. if (plainSz == 0 && ret != WOLFSSL_SUCCESS)
  17423. ret = WOLFSSL_SUCCESS;
  17424. else
  17425. ret = -8619;
  17426. }
  17427. else
  17428. ret = -8620;
  17429. EVP_CIPHER_CTX_cleanup(ctx);
  17430. if (ret != WOLFSSL_SUCCESS)
  17431. return ret;
  17432. plainSz = 0;
  17433. EVP_CIPHER_CTX_init(ctx);
  17434. ret = EVP_CipherInit(ctx, EVP_aes_128_cbc(), key, iv, 0);
  17435. if (ret == WOLFSSL_SUCCESS) {
  17436. ret = EVP_CipherUpdate(ctx, plain, &idx, cipher, cipherSz);
  17437. if (ret == WOLFSSL_SUCCESS)
  17438. plainSz += idx;
  17439. }
  17440. if (ret == WOLFSSL_SUCCESS) {
  17441. ret = EVP_CipherFinal(ctx, plain + plainSz, &idx);
  17442. if (ret == WOLFSSL_SUCCESS)
  17443. plainSz += idx;
  17444. }
  17445. EVP_CIPHER_CTX_cleanup(ctx);
  17446. if (ret != WOLFSSL_SUCCESS)
  17447. return -8621;
  17448. if (plainSz != (int)sizeof(msg) || XMEMCMP(plain, msg, sizeof(msg)))
  17449. return -8622;
  17450. cipherSz = 0;
  17451. EVP_CIPHER_CTX_init(ctx);
  17452. ret = EVP_CipherInit(ctx, EVP_aes_128_cbc(), key, iv, 1);
  17453. if (ret == WOLFSSL_SUCCESS) {
  17454. ret = EVP_CipherUpdate(ctx, cipher, &idx, msg, AES_BLOCK_SIZE);
  17455. if (ret == WOLFSSL_SUCCESS)
  17456. cipherSz += idx;
  17457. }
  17458. if (ret == WOLFSSL_SUCCESS) {
  17459. ret = EVP_CipherFinal(ctx, cipher + cipherSz, &idx);
  17460. if (ret == WOLFSSL_SUCCESS)
  17461. cipherSz += idx;
  17462. }
  17463. EVP_CIPHER_CTX_cleanup(ctx);
  17464. if (ret != WOLFSSL_SUCCESS)
  17465. return -8623;
  17466. if (cipherSz != (int)sizeof(verify2) || XMEMCMP(cipher, verify2, cipherSz))
  17467. return -8624;
  17468. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17469. wolfSSL_EVP_CIPHER_CTX_free(ctx);
  17470. #endif
  17471. } /* end evp_cipher test: EVP_aes_128_cbc*/
  17472. #endif /* WOLFSSL_AES_128 && HAVE_AES_CBC */
  17473. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_256)
  17474. { /* evp_cipher test: EVP_aes_256_ecb*/
  17475. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17476. EVP_CIPHER_CTX *ctx = wolfSSL_EVP_CIPHER_CTX_new();
  17477. #else
  17478. EVP_CIPHER_CTX ctx[1];
  17479. #endif
  17480. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = {
  17481. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  17482. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  17483. };
  17484. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  17485. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  17486. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  17487. };
  17488. WOLFSSL_SMALL_STACK_STATIC const byte key[] = {
  17489. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  17490. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  17491. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  17492. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  17493. };
  17494. byte cipher[AES_BLOCK_SIZE * 4];
  17495. byte plain [AES_BLOCK_SIZE * 4];
  17496. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17497. if (ctx == NULL)
  17498. return MEMORY_E;
  17499. #endif
  17500. EVP_CIPHER_CTX_init(ctx);
  17501. ret = EVP_CipherInit(ctx, EVP_aes_256_ecb(), (unsigned char*)key, NULL, 1);
  17502. if (ret == WOLFSSL_SUCCESS)
  17503. ret = EVP_Cipher(ctx, cipher, (byte*)msg, 16);
  17504. EVP_CIPHER_CTX_cleanup(ctx);
  17505. if (ret != 16)
  17506. return -8625;
  17507. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE))
  17508. return -8626;
  17509. EVP_CIPHER_CTX_init(ctx);
  17510. ret = EVP_CipherInit(ctx, EVP_aes_256_ecb(), (unsigned char*)key, NULL, 0);
  17511. if (ret == WOLFSSL_SUCCESS)
  17512. ret = EVP_Cipher(ctx, plain, cipher, 16);
  17513. EVP_CIPHER_CTX_cleanup(ctx);
  17514. if (ret != 16)
  17515. return -8627;
  17516. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  17517. return -8628;
  17518. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17519. wolfSSL_EVP_CIPHER_CTX_free(ctx);
  17520. #endif
  17521. } /* end evp_cipher test */
  17522. #endif /* HAVE_AES_ECB && WOLFSSL_AES_128 */
  17523. #define OPENSSL_TEST_ERROR (-10000)
  17524. #if defined(WOLFSSL_AES_DIRECT) && defined(WOLFSSL_AES_256)
  17525. /* enable HAVE_AES_DECRYPT for AES_encrypt/decrypt */
  17526. {
  17527. /* Test: AES_encrypt/decrypt/set Key */
  17528. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17529. AES_KEY *enc = (AES_KEY *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  17530. #ifdef HAVE_AES_DECRYPT
  17531. AES_KEY *dec = (AES_KEY *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  17532. #endif
  17533. #else
  17534. AES_KEY enc[1];
  17535. #ifdef HAVE_AES_DECRYPT
  17536. AES_KEY dec[1];
  17537. #endif
  17538. #endif
  17539. WOLFSSL_SMALL_STACK_STATIC const byte msg[] =
  17540. {
  17541. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  17542. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  17543. };
  17544. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  17545. {
  17546. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  17547. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  17548. };
  17549. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  17550. {
  17551. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  17552. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  17553. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  17554. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  17555. };
  17556. byte plain[sizeof(msg)];
  17557. byte cipher[sizeof(msg)];
  17558. printf("openSSL extra test\n") ;
  17559. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17560. if (enc == NULL)
  17561. return MEMORY_E;
  17562. #ifdef HAVE_AES_DECRYPT
  17563. if (dec == NULL)
  17564. return MEMORY_E;
  17565. #endif
  17566. #endif
  17567. AES_set_encrypt_key(key, sizeof(key)*8, enc);
  17568. AES_set_decrypt_key(key, sizeof(key)*8, dec);
  17569. AES_encrypt(msg, cipher, enc);
  17570. #ifdef HAVE_AES_DECRYPT
  17571. AES_decrypt(cipher, plain, dec);
  17572. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  17573. return OPENSSL_TEST_ERROR-60;
  17574. #endif /* HAVE_AES_DECRYPT */
  17575. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE))
  17576. return OPENSSL_TEST_ERROR-61;
  17577. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17578. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  17579. #ifdef HAVE_AES_DECRYPT
  17580. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  17581. #endif
  17582. #endif
  17583. }
  17584. #endif /* WOLFSSL_AES_DIRECT && WOLFSSL_AES_256 */
  17585. /* EVP_Cipher with EVP_aes_xxx_ctr() */
  17586. #ifdef WOLFSSL_AES_COUNTER
  17587. {
  17588. byte plainBuff [64];
  17589. byte cipherBuff[64];
  17590. #ifdef WOLFSSL_AES_128
  17591. WOLFSSL_SMALL_STACK_STATIC const byte ctrKey[] =
  17592. {
  17593. 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,
  17594. 0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c
  17595. };
  17596. WOLFSSL_SMALL_STACK_STATIC const byte ctrIv[] =
  17597. {
  17598. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  17599. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  17600. };
  17601. WOLFSSL_SMALL_STACK_STATIC const byte ctrPlain[] =
  17602. {
  17603. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  17604. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  17605. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  17606. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  17607. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  17608. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  17609. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  17610. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  17611. };
  17612. WOLFSSL_SMALL_STACK_STATIC const byte ctrCipher[] =
  17613. {
  17614. 0x87,0x4d,0x61,0x91,0xb6,0x20,0xe3,0x26,
  17615. 0x1b,0xef,0x68,0x64,0x99,0x0d,0xb6,0xce,
  17616. 0x98,0x06,0xf6,0x6b,0x79,0x70,0xfd,0xff,
  17617. 0x86,0x17,0x18,0x7b,0xb9,0xff,0xfd,0xff,
  17618. 0x5a,0xe4,0xdf,0x3e,0xdb,0xd5,0xd3,0x5e,
  17619. 0x5b,0x4f,0x09,0x02,0x0d,0xb0,0x3e,0xab,
  17620. 0x1e,0x03,0x1d,0xda,0x2f,0xbe,0x03,0xd1,
  17621. 0x79,0x21,0x70,0xa0,0xf3,0x00,0x9c,0xee
  17622. };
  17623. WOLFSSL_SMALL_STACK_STATIC const byte oddCipher[] =
  17624. {
  17625. 0xb9,0xd7,0xcb,0x08,0xb0,0xe1,0x7b,0xa0,
  17626. 0xc2
  17627. };
  17628. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17629. EVP_CIPHER_CTX *p_en;
  17630. EVP_CIPHER_CTX *p_de;
  17631. #endif
  17632. #endif /* WOLFSSL_AES_128 */
  17633. #ifdef WOLFSSL_AES_192
  17634. /* test vector from "Recommendation for Block Cipher Modes of Operation"
  17635. * NIST Special Publication 800-38A */
  17636. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Key[] =
  17637. {
  17638. 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  17639. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  17640. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b
  17641. };
  17642. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Iv[] =
  17643. {
  17644. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  17645. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  17646. };
  17647. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Plain[] =
  17648. {
  17649. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  17650. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  17651. };
  17652. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Cipher[] =
  17653. {
  17654. 0x1a,0xbc,0x93,0x24,0x17,0x52,0x1c,0xa2,
  17655. 0x4f,0x2b,0x04,0x59,0xfe,0x7e,0x6e,0x0b
  17656. };
  17657. #endif /* WOLFSSL_AES_192 */
  17658. #ifdef WOLFSSL_AES_256
  17659. /* test vector from "Recommendation for Block Cipher Modes of Operation"
  17660. * NIST Special Publication 800-38A */
  17661. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Key[] =
  17662. {
  17663. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  17664. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  17665. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  17666. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  17667. };
  17668. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Iv[] =
  17669. {
  17670. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  17671. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  17672. };
  17673. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Plain[] =
  17674. {
  17675. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  17676. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  17677. };
  17678. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Cipher[] =
  17679. {
  17680. 0x60,0x1e,0xc3,0x13,0x77,0x57,0x89,0xa5,
  17681. 0xb7,0xa7,0xf5,0x04,0xbb,0xf3,0xd2,0x28
  17682. };
  17683. #endif /* WOLFSSL_AES_256 */
  17684. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17685. EVP_CIPHER_CTX *en = wolfSSL_EVP_CIPHER_CTX_new();
  17686. EVP_CIPHER_CTX *de = wolfSSL_EVP_CIPHER_CTX_new();
  17687. if ((en == NULL) || (de == NULL))
  17688. return MEMORY_E;
  17689. #else
  17690. EVP_CIPHER_CTX en[1];
  17691. EVP_CIPHER_CTX de[1];
  17692. #endif
  17693. #ifdef WOLFSSL_AES_128
  17694. EVP_CIPHER_CTX_init(en);
  17695. if (EVP_CipherInit(en, EVP_aes_128_ctr(),
  17696. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  17697. return -8629;
  17698. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctrPlain,
  17699. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  17700. return -8630;
  17701. EVP_CIPHER_CTX_init(de);
  17702. if (EVP_CipherInit(de, EVP_aes_128_ctr(),
  17703. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  17704. return -8631;
  17705. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff,
  17706. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  17707. return -8632;
  17708. if (XMEMCMP(cipherBuff, ctrCipher, AES_BLOCK_SIZE*4))
  17709. return -8633;
  17710. if (XMEMCMP(plainBuff, ctrPlain, AES_BLOCK_SIZE*4))
  17711. return -8634;
  17712. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17713. p_en = wolfSSL_EVP_CIPHER_CTX_new();
  17714. if (p_en == NULL)
  17715. return -8635;
  17716. p_de = wolfSSL_EVP_CIPHER_CTX_new();
  17717. if (p_de == NULL)
  17718. return -8636;
  17719. if (EVP_CipherInit(p_en, EVP_aes_128_ctr(),
  17720. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  17721. return -8637;
  17722. if (EVP_Cipher(p_en, (byte*)cipherBuff, (byte*)ctrPlain,
  17723. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  17724. return -8638;
  17725. if (EVP_CipherInit(p_de, EVP_aes_128_ctr(),
  17726. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  17727. return -8639;
  17728. if (EVP_Cipher(p_de, (byte*)plainBuff, (byte*)cipherBuff,
  17729. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  17730. return -8640;
  17731. wolfSSL_EVP_CIPHER_CTX_free(p_en);
  17732. wolfSSL_EVP_CIPHER_CTX_free(p_de);
  17733. #endif /* WOLFSSL_SMALL_STACK && !WOLFSSL_NO_MALLOC */
  17734. if (XMEMCMP(cipherBuff, ctrCipher, AES_BLOCK_SIZE*4))
  17735. return -8641;
  17736. if (XMEMCMP(plainBuff, ctrPlain, AES_BLOCK_SIZE*4))
  17737. return -8642;
  17738. EVP_CIPHER_CTX_init(en);
  17739. if (EVP_CipherInit(en, EVP_aes_128_ctr(),
  17740. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  17741. return -8643;
  17742. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctrPlain, 9) != 9)
  17743. return -8644;
  17744. EVP_CIPHER_CTX_init(de);
  17745. if (EVP_CipherInit(de, EVP_aes_128_ctr(),
  17746. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  17747. return -8645;
  17748. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff, 9) != 9)
  17749. return -8646;
  17750. if (XMEMCMP(plainBuff, ctrPlain, 9))
  17751. return -8647;
  17752. if (XMEMCMP(cipherBuff, ctrCipher, 9))
  17753. return -8648;
  17754. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctrPlain, 9) != 9)
  17755. return -8649;
  17756. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff, 9) != 9)
  17757. return -8650;
  17758. if (XMEMCMP(plainBuff, ctrPlain, 9))
  17759. return -8651;
  17760. if (XMEMCMP(cipherBuff, oddCipher, 9))
  17761. return -8652;
  17762. #endif /* WOLFSSL_AES_128 */
  17763. #ifdef WOLFSSL_AES_192
  17764. EVP_CIPHER_CTX_init(en);
  17765. if (EVP_CipherInit(en, EVP_aes_192_ctr(),
  17766. (unsigned char*)ctr192Key, (unsigned char*)ctr192Iv, 0) == 0)
  17767. return -8653;
  17768. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctr192Plain,
  17769. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  17770. return -8654;
  17771. EVP_CIPHER_CTX_init(de);
  17772. if (EVP_CipherInit(de, EVP_aes_192_ctr(),
  17773. (unsigned char*)ctr192Key, (unsigned char*)ctr192Iv, 0) == 0)
  17774. return -8655;
  17775. XMEMSET(plainBuff, 0, sizeof(plainBuff));
  17776. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff,
  17777. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  17778. return -8656;
  17779. if (XMEMCMP(plainBuff, ctr192Plain, sizeof(ctr192Plain)))
  17780. return -8657;
  17781. if (XMEMCMP(ctr192Cipher, cipherBuff, sizeof(ctr192Cipher)))
  17782. return -8658;
  17783. #endif /* WOLFSSL_AES_192 */
  17784. #ifdef WOLFSSL_AES_256
  17785. EVP_CIPHER_CTX_init(en);
  17786. if (EVP_CipherInit(en, EVP_aes_256_ctr(),
  17787. (unsigned char*)ctr256Key, (unsigned char*)ctr256Iv, 0) == 0)
  17788. return -8659;
  17789. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctr256Plain,
  17790. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  17791. return -8660;
  17792. EVP_CIPHER_CTX_init(de);
  17793. if (EVP_CipherInit(de, EVP_aes_256_ctr(),
  17794. (unsigned char*)ctr256Key, (unsigned char*)ctr256Iv, 0) == 0)
  17795. return -8661;
  17796. XMEMSET(plainBuff, 0, sizeof(plainBuff));
  17797. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff,
  17798. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  17799. return -8662;
  17800. if (XMEMCMP(plainBuff, ctr256Plain, sizeof(ctr256Plain)))
  17801. return -8663;
  17802. if (XMEMCMP(ctr256Cipher, cipherBuff, sizeof(ctr256Cipher)))
  17803. return -8664;
  17804. #endif /* WOLFSSL_AES_256 */
  17805. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17806. wolfSSL_EVP_CIPHER_CTX_free(en);
  17807. wolfSSL_EVP_CIPHER_CTX_free(de);
  17808. #endif
  17809. }
  17810. #endif /* HAVE_AES_COUNTER */
  17811. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  17812. {
  17813. /* EVP_CipherUpdate test */
  17814. WOLFSSL_SMALL_STACK_STATIC const byte cbcPlain[] =
  17815. {
  17816. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  17817. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  17818. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  17819. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  17820. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  17821. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  17822. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  17823. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  17824. };
  17825. byte key[] = "0123456789abcdef "; /* align */
  17826. byte iv[] = "1234567890abcdef "; /* align */
  17827. byte cipher[AES_BLOCK_SIZE * 4];
  17828. byte plain [AES_BLOCK_SIZE * 4];
  17829. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17830. EVP_CIPHER_CTX *en = wolfSSL_EVP_CIPHER_CTX_new();
  17831. EVP_CIPHER_CTX *de = wolfSSL_EVP_CIPHER_CTX_new();
  17832. #else
  17833. EVP_CIPHER_CTX en[1];
  17834. EVP_CIPHER_CTX de[1];
  17835. #endif
  17836. int outlen ;
  17837. int total = 0;
  17838. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17839. if ((en == NULL) || (de == NULL))
  17840. return MEMORY_E;
  17841. #endif
  17842. EVP_CIPHER_CTX_init(en);
  17843. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  17844. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  17845. return -8665;
  17846. /* openSSL compatibility, if(inlen == 0)return 1; */
  17847. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen,
  17848. (byte*)cbcPlain, 0) != 1)
  17849. return -8666;
  17850. EVP_CIPHER_CTX_init(en);
  17851. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  17852. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  17853. return -8667;
  17854. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen,
  17855. (byte*)cbcPlain, 9) == 0)
  17856. return -8668;
  17857. if(outlen != 0)
  17858. return -8669;
  17859. total += outlen;
  17860. if (EVP_CipherUpdate(en, (byte*)&cipher[total], &outlen,
  17861. (byte*)&cbcPlain[9] , 9) == 0)
  17862. return -8670;
  17863. if(outlen != 16)
  17864. return -8671;
  17865. total += outlen;
  17866. if (EVP_CipherFinal(en, (byte*)&cipher[total], &outlen) == 0)
  17867. return -8672;
  17868. if(outlen != 16)
  17869. return -8673;
  17870. total += outlen;
  17871. if(total != 32)
  17872. return -8674;
  17873. total = 0;
  17874. EVP_CIPHER_CTX_init(de);
  17875. if (EVP_CipherInit(de, EVP_aes_128_cbc(),
  17876. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  17877. return -8675;
  17878. if (EVP_CipherUpdate(de, (byte*)plain, &outlen, (byte*)cipher, 6) == 0)
  17879. return -8676;
  17880. if(outlen != 0)
  17881. return -8677;
  17882. total += outlen;
  17883. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen,
  17884. (byte*)&cipher[6], 12) == 0)
  17885. return -8678;
  17886. if(outlen != 0)
  17887. total += outlen;
  17888. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen,
  17889. (byte*)&cipher[6+12], 14) == 0)
  17890. return -8679;
  17891. if(outlen != 16)
  17892. return -8680;
  17893. total += outlen;
  17894. if (EVP_CipherFinal(de, (byte*)&plain[total], &outlen) == 0)
  17895. return -8681;
  17896. if(outlen != 2)
  17897. return -8682;
  17898. total += outlen;
  17899. if(total != 18)
  17900. return -8683;
  17901. if (XMEMCMP(plain, cbcPlain, 18))
  17902. return -8684;
  17903. total = 0;
  17904. EVP_CIPHER_CTX_init(en);
  17905. if (EVP_EncryptInit(en, EVP_aes_128_cbc(),
  17906. (unsigned char*)key, (unsigned char*)iv) == 0)
  17907. return -8685;
  17908. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen, (byte*)cbcPlain, 9) == 0)
  17909. return -8686;
  17910. if(outlen != 0)
  17911. return -8687;
  17912. total += outlen;
  17913. if (EVP_CipherUpdate(en, (byte*)&cipher[total], &outlen, (byte*)&cbcPlain[9] , 9) == 0)
  17914. return -8688;
  17915. if(outlen != 16)
  17916. return -8689;
  17917. total += outlen;
  17918. if (EVP_EncryptFinal(en, (byte*)&cipher[total], &outlen) == 0)
  17919. return -8690;
  17920. if(outlen != 16)
  17921. return -8691;
  17922. total += outlen;
  17923. if(total != 32)
  17924. return 3438;
  17925. total = 0;
  17926. EVP_CIPHER_CTX_init(de);
  17927. if (EVP_DecryptInit(de, EVP_aes_128_cbc(),
  17928. (unsigned char*)key, (unsigned char*)iv) == 0)
  17929. return -8692;
  17930. if (EVP_CipherUpdate(de, (byte*)plain, &outlen, (byte*)cipher, 6) == 0)
  17931. return -8693;
  17932. if(outlen != 0)
  17933. return -8694;
  17934. total += outlen;
  17935. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen, (byte*)&cipher[6], 12) == 0)
  17936. return -8695;
  17937. if(outlen != 0)
  17938. total += outlen;
  17939. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen, (byte*)&cipher[6+12], 14) == 0)
  17940. return -8696;
  17941. if(outlen != 16)
  17942. return -8697;
  17943. total += outlen;
  17944. if (EVP_DecryptFinal(de, (byte*)&plain[total], &outlen) == 0)
  17945. return -8698;
  17946. if(outlen != 2)
  17947. return -8699;
  17948. total += outlen;
  17949. if(total != 18)
  17950. return 3447;
  17951. if (XMEMCMP(plain, cbcPlain, 18))
  17952. return -8700;
  17953. if (EVP_CIPHER_key_length(NULL) != 0)
  17954. return -8701;
  17955. if (EVP_CIPHER_key_length(EVP_aes_128_cbc()) != 16)
  17956. return -8702;
  17957. if (EVP_CIPHER_CTX_mode(NULL) != 0)
  17958. return -8703;
  17959. if (EVP_CIPHER_CTX_mode(en) != (en->flags & WOLFSSL_EVP_CIPH_MODE))
  17960. return -8704;
  17961. EVP_CIPHER_CTX_init(en);
  17962. if (EVP_CipherInit_ex(en, EVP_aes_128_cbc(), NULL,
  17963. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  17964. return -8705;
  17965. EVP_CIPHER_CTX_init(en);
  17966. if (EVP_EncryptInit_ex(en, EVP_aes_128_cbc(), NULL,
  17967. (unsigned char*)key, (unsigned char*)iv) == 0)
  17968. return -8706;
  17969. if (wolfSSL_EVP_EncryptFinal_ex(NULL, NULL, NULL) != WOLFSSL_FAILURE)
  17970. return -8707;
  17971. if (wolfSSL_EVP_EncryptFinal(NULL, NULL, NULL) != WOLFSSL_FAILURE)
  17972. return -8708;
  17973. EVP_CIPHER_CTX_init(de);
  17974. if (EVP_DecryptInit_ex(de, EVP_aes_128_cbc(), NULL,
  17975. (unsigned char*)key, (unsigned char*)iv) == 0)
  17976. return -8709;
  17977. if (wolfSSL_EVP_DecryptFinal(NULL, NULL, NULL) != WOLFSSL_FAILURE)
  17978. return -8710;
  17979. if (wolfSSL_EVP_DecryptFinal_ex(NULL, NULL, NULL) != WOLFSSL_FAILURE)
  17980. return -8711;
  17981. if (EVP_CIPHER_CTX_block_size(NULL) != BAD_FUNC_ARG)
  17982. return -8712;
  17983. EVP_CIPHER_CTX_init(en);
  17984. EVP_EncryptInit_ex(en, EVP_aes_128_cbc(), NULL,
  17985. (unsigned char*)key, (unsigned char*)iv);
  17986. if (EVP_CIPHER_CTX_block_size(en) != en->block_size)
  17987. return -8713;
  17988. if (EVP_CIPHER_block_size(NULL) != BAD_FUNC_ARG)
  17989. return -8714;
  17990. if (EVP_CIPHER_block_size(EVP_aes_128_cbc()) != AES_BLOCK_SIZE)
  17991. return -8715;
  17992. if (WOLFSSL_EVP_CIPHER_mode(NULL) != 0)
  17993. return -8716;
  17994. if (EVP_CIPHER_flags(EVP_aes_128_cbc()) != WOLFSSL_EVP_CIPH_CBC_MODE)
  17995. return -8717;
  17996. EVP_CIPHER_CTX_clear_flags(en, 0xFFFFFFFF);
  17997. EVP_CIPHER_CTX_set_flags(en, 42);
  17998. if (en->flags != 42)
  17999. return -8718;
  18000. if (EVP_CIPHER_CTX_set_padding(NULL, 0) != BAD_FUNC_ARG)
  18001. return -8719;
  18002. if (EVP_CIPHER_CTX_set_padding(en, 0) != WOLFSSL_SUCCESS)
  18003. return -8720;
  18004. if (EVP_CIPHER_CTX_set_padding(en, 1) != WOLFSSL_SUCCESS)
  18005. return -8721;
  18006. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18007. wolfSSL_EVP_CIPHER_CTX_free(en);
  18008. wolfSSL_EVP_CIPHER_CTX_free(de);
  18009. #endif
  18010. }
  18011. #endif /* WOLFSSL_AES_128 && HAVE_AES_CBC */
  18012. #endif /* ifndef NO_AES */
  18013. return 0;
  18014. }
  18015. WOLFSSL_TEST_SUBROUTINE int openSSL_evpMD_test(void)
  18016. {
  18017. int ret = 0;
  18018. #if !defined(NO_SHA256) && !defined(NO_SHA)
  18019. WOLFSSL_EVP_MD_CTX* ctx;
  18020. WOLFSSL_EVP_MD_CTX* ctx2;
  18021. ctx = EVP_MD_CTX_create();
  18022. ctx2 = EVP_MD_CTX_create();
  18023. ret = EVP_DigestInit(ctx, EVP_sha256());
  18024. if (ret != SSL_SUCCESS) {
  18025. ret = -8800;
  18026. goto openSSL_evpMD_test_done;
  18027. }
  18028. ret = EVP_MD_CTX_copy(ctx2, ctx);
  18029. if (ret != SSL_SUCCESS) {
  18030. ret = -8801;
  18031. goto openSSL_evpMD_test_done;
  18032. }
  18033. if (EVP_MD_type(EVP_sha256()) != EVP_MD_CTX_type(ctx2)) {
  18034. ret = -8802;
  18035. goto openSSL_evpMD_test_done;
  18036. }
  18037. ret = EVP_DigestInit(ctx, EVP_sha1());
  18038. if (ret != SSL_SUCCESS) {
  18039. ret = -8803;
  18040. goto openSSL_evpMD_test_done;
  18041. }
  18042. if (EVP_MD_type(EVP_sha256()) != EVP_MD_CTX_type(ctx2)) {
  18043. ret = -8804;
  18044. goto openSSL_evpMD_test_done;
  18045. }
  18046. ret = EVP_MD_CTX_copy_ex(ctx2, ctx);
  18047. if (ret != SSL_SUCCESS) {
  18048. ret = -8805;
  18049. goto openSSL_evpMD_test_done;
  18050. }
  18051. if (EVP_MD_type(EVP_sha256()) == EVP_MD_CTX_type(ctx2)) {
  18052. ret = -8806;
  18053. goto openSSL_evpMD_test_done;
  18054. }
  18055. if (EVP_MD_type(EVP_sha1()) != EVP_MD_CTX_type(ctx2)) {
  18056. ret = -8807;
  18057. goto openSSL_evpMD_test_done;
  18058. }
  18059. if (EVP_DigestInit_ex(ctx, EVP_sha1(), NULL) != SSL_SUCCESS) {
  18060. ret = -8808;
  18061. goto openSSL_evpMD_test_done;
  18062. }
  18063. if (EVP_add_digest(NULL) != 0) {
  18064. ret = -8809;
  18065. goto openSSL_evpMD_test_done;
  18066. }
  18067. if (wolfSSL_EVP_add_cipher(NULL) != 0) {
  18068. ret = -8810;
  18069. goto openSSL_evpMD_test_done;
  18070. }
  18071. ret = 0; /* got to success state without jumping to end with a fail */
  18072. openSSL_evpMD_test_done:
  18073. EVP_MD_CTX_destroy(ctx);
  18074. EVP_MD_CTX_destroy(ctx2);
  18075. #endif /* NO_SHA256 */
  18076. return ret;
  18077. }
  18078. #ifdef DEBUG_SIGN
  18079. static void show(const char *title, const char *p, unsigned int s) {
  18080. char* i;
  18081. printf("%s: ", title);
  18082. for (i = p;
  18083. i < p + s;
  18084. printf("%c", *i), i++);
  18085. printf("\n");
  18086. }
  18087. #else
  18088. #define show(a,b,c)
  18089. #endif
  18090. #define FOURK_BUFF 4096
  18091. #define ERR_BASE_PKEY (-5000)
  18092. WOLFSSL_TEST_SUBROUTINE int openssl_pkey0_test(void)
  18093. {
  18094. int ret = 0;
  18095. #if !defined(NO_RSA) && !defined(HAVE_USER_RSA) && !defined(NO_SHA)
  18096. byte* prvTmp;
  18097. byte* pubTmp;
  18098. int prvBytes;
  18099. int pubBytes;
  18100. RSA *prvRsa = NULL;
  18101. RSA *pubRsa = NULL;
  18102. EVP_PKEY *prvPkey = NULL;
  18103. EVP_PKEY *pubPkey = NULL;
  18104. EVP_PKEY_CTX *enc = NULL;
  18105. EVP_PKEY_CTX *dec = NULL;
  18106. byte in[] = TEST_STRING;
  18107. byte out[256];
  18108. size_t outlen;
  18109. size_t keySz;
  18110. byte plain[256];
  18111. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  18112. XFILE keyFile;
  18113. XFILE keypubFile;
  18114. char cliKey[] = "./certs/client-key.der";
  18115. char cliKeypub[] = "./certs/client-keyPub.der";
  18116. #endif
  18117. prvTmp = (byte*)XMALLOC(FOURK_BUFF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18118. if (prvTmp == NULL)
  18119. return ERR_BASE_PKEY-1;
  18120. pubTmp = (byte*)XMALLOC(FOURK_BUFF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18121. if (pubTmp == NULL) {
  18122. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18123. return ERR_BASE_PKEY-2;
  18124. }
  18125. #ifdef USE_CERT_BUFFERS_1024
  18126. XMEMCPY(prvTmp, client_key_der_1024, sizeof_client_key_der_1024);
  18127. prvBytes = sizeof_client_key_der_1024;
  18128. XMEMCPY(pubTmp, client_keypub_der_1024, sizeof_client_keypub_der_1024);
  18129. pubBytes = sizeof_client_keypub_der_1024;
  18130. #elif defined(USE_CERT_BUFFERS_2048)
  18131. XMEMCPY(prvTmp, client_key_der_2048, sizeof_client_key_der_2048);
  18132. prvBytes = sizeof_client_key_der_2048;
  18133. XMEMCPY(pubTmp, client_keypub_der_2048, sizeof_client_keypub_der_2048);
  18134. pubBytes = sizeof_client_keypub_der_2048;
  18135. #else
  18136. keyFile = XFOPEN(cliKey, "rb");
  18137. if (!keyFile) {
  18138. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18139. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18140. err_sys("can't open ./certs/client-key.der, "
  18141. "Please run from wolfSSL home dir", ERR_BASE_PKEY-3);
  18142. return ERR_BASE_PKEY-3;
  18143. }
  18144. prvBytes = (int)XFREAD(prvTmp, 1, (int)FOURK_BUFF, keyFile);
  18145. XFCLOSE(keyFile);
  18146. keypubFile = XFOPEN(cliKeypub, "rb");
  18147. if (!keypubFile) {
  18148. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18149. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18150. err_sys("can't open ./certs/client-cert.der, "
  18151. "Please run from wolfSSL home dir", -4);
  18152. return ERR_BASE_PKEY-4;
  18153. }
  18154. pubBytes = (int)XFREAD(pubTmp, 1, (int)FOURK_BUFF, keypubFile);
  18155. XFCLOSE(keypubFile);
  18156. #endif /* USE_CERT_BUFFERS */
  18157. prvRsa = wolfSSL_RSA_new();
  18158. pubRsa = wolfSSL_RSA_new();
  18159. if((prvRsa == NULL) || (pubRsa == NULL)){
  18160. printf("error with RSA_new\n");
  18161. ret = ERR_BASE_PKEY-10;
  18162. goto openssl_pkey0_test_done;
  18163. }
  18164. ret = wolfSSL_RSA_LoadDer_ex(prvRsa, prvTmp, prvBytes, WOLFSSL_RSA_LOAD_PRIVATE);
  18165. if(ret != SSL_SUCCESS){
  18166. printf("error with RSA_LoadDer_ex\n");
  18167. ret = ERR_BASE_PKEY-11;
  18168. goto openssl_pkey0_test_done;
  18169. }
  18170. ret = wolfSSL_RSA_LoadDer_ex(pubRsa, pubTmp, pubBytes, WOLFSSL_RSA_LOAD_PUBLIC);
  18171. if(ret != SSL_SUCCESS){
  18172. printf("error with RSA_LoadDer_ex\n");
  18173. ret = ERR_BASE_PKEY-12;
  18174. goto openssl_pkey0_test_done;
  18175. }
  18176. keySz = (size_t)RSA_size(pubRsa);
  18177. prvPkey = wolfSSL_EVP_PKEY_new();
  18178. pubPkey = wolfSSL_EVP_PKEY_new();
  18179. if((prvPkey == NULL) || (pubPkey == NULL)){
  18180. printf("error with PKEY_new\n");
  18181. ret = ERR_BASE_PKEY-13;
  18182. goto openssl_pkey0_test_done;
  18183. }
  18184. ret = wolfSSL_EVP_PKEY_set1_RSA(prvPkey, prvRsa);
  18185. ret += wolfSSL_EVP_PKEY_set1_RSA(pubPkey, pubRsa);
  18186. if(ret != 2){
  18187. printf("error with PKEY_set1_RSA\n");
  18188. ret = ERR_BASE_PKEY-14;
  18189. goto openssl_pkey0_test_done;
  18190. }
  18191. dec = EVP_PKEY_CTX_new(prvPkey, NULL);
  18192. enc = EVP_PKEY_CTX_new(pubPkey, NULL);
  18193. if((dec == NULL)||(enc==NULL)){
  18194. printf("error with EVP_PKEY_CTX_new\n");
  18195. ret = ERR_BASE_PKEY-15;
  18196. goto openssl_pkey0_test_done;
  18197. }
  18198. ret = EVP_PKEY_decrypt_init(dec);
  18199. if (ret != 1) {
  18200. printf("error with decrypt init\n");
  18201. ret = ERR_BASE_PKEY-16;
  18202. goto openssl_pkey0_test_done;
  18203. }
  18204. ret = EVP_PKEY_encrypt_init(enc);
  18205. if (ret != 1) {
  18206. printf("error with encrypt init\n");
  18207. ret = ERR_BASE_PKEY-17;
  18208. goto openssl_pkey0_test_done;
  18209. }
  18210. XMEMSET(out, 0, sizeof(out));
  18211. ret = EVP_PKEY_encrypt(enc, out, &outlen, in, sizeof(in));
  18212. if (ret != 1) {
  18213. printf("error encrypting msg\n");
  18214. ret = ERR_BASE_PKEY-18;
  18215. goto openssl_pkey0_test_done;
  18216. }
  18217. show("encrypted msg", out, outlen);
  18218. XMEMSET(plain, 0, sizeof(plain));
  18219. ret = EVP_PKEY_decrypt(dec, plain, &outlen, out, keySz);
  18220. if (ret != 1) {
  18221. printf("error decrypting msg\n");
  18222. ret = ERR_BASE_PKEY-19;
  18223. goto openssl_pkey0_test_done;
  18224. }
  18225. show("decrypted msg", plain, outlen);
  18226. /* RSA_PKCS1_OAEP_PADDING test */
  18227. ret = EVP_PKEY_decrypt_init(dec);
  18228. if (ret != 1) {
  18229. printf("error with decrypt init\n");
  18230. ret = ERR_BASE_PKEY-30;
  18231. goto openssl_pkey0_test_done;
  18232. }
  18233. ret = EVP_PKEY_encrypt_init(enc);
  18234. if (ret != 1) {
  18235. printf("error with encrypt init\n");
  18236. ret = ERR_BASE_PKEY-31;
  18237. goto openssl_pkey0_test_done;
  18238. }
  18239. if (EVP_PKEY_CTX_set_rsa_padding(dec, RSA_PKCS1_PADDING) <= 0) {
  18240. printf("first set rsa padding error\n");
  18241. ret = ERR_BASE_PKEY-32;
  18242. goto openssl_pkey0_test_done;
  18243. }
  18244. #ifndef HAVE_FIPS
  18245. if (EVP_PKEY_CTX_set_rsa_padding(dec, RSA_PKCS1_OAEP_PADDING) <= 0){
  18246. printf("second set rsa padding error\n");
  18247. ret = ERR_BASE_PKEY-33;
  18248. goto openssl_pkey0_test_done;
  18249. }
  18250. if (EVP_PKEY_CTX_set_rsa_padding(enc, RSA_PKCS1_OAEP_PADDING) <= 0) {
  18251. printf("third set rsa padding error\n");
  18252. ret = ERR_BASE_PKEY-34;
  18253. goto openssl_pkey0_test_done;
  18254. }
  18255. #endif
  18256. XMEMSET(out, 0, sizeof(out));
  18257. ret = EVP_PKEY_encrypt(enc, out, &outlen, in, sizeof(in));
  18258. if (ret != 1) {
  18259. printf("error encrypting msg\n");
  18260. ret = ERR_BASE_PKEY-35;
  18261. goto openssl_pkey0_test_done;
  18262. }
  18263. show("encrypted msg", out, outlen);
  18264. XMEMSET(plain, 0, sizeof(plain));
  18265. ret = EVP_PKEY_decrypt(dec, plain, &outlen, out, keySz);
  18266. if (ret != 1) {
  18267. printf("error decrypting msg\n");
  18268. ret = ERR_BASE_PKEY-36;
  18269. goto openssl_pkey0_test_done;
  18270. }
  18271. show("decrypted msg", plain, outlen);
  18272. ret = 0; /* made it to this point without error then set success */
  18273. openssl_pkey0_test_done:
  18274. wolfSSL_RSA_free(prvRsa);
  18275. wolfSSL_RSA_free(pubRsa);
  18276. EVP_PKEY_free(pubPkey);
  18277. EVP_PKEY_free(prvPkey);
  18278. EVP_PKEY_CTX_free(dec);
  18279. EVP_PKEY_CTX_free(enc);
  18280. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18281. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18282. #endif /* NO_RSA */
  18283. return ret;
  18284. }
  18285. WOLFSSL_TEST_SUBROUTINE int openssl_pkey1_test(void)
  18286. {
  18287. int ret = 0;
  18288. #if !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(HAVE_USER_RSA) && \
  18289. !defined(NO_SHA)
  18290. EVP_PKEY_CTX* dec = NULL;
  18291. EVP_PKEY_CTX* enc = NULL;
  18292. EVP_PKEY* pubKey = NULL;
  18293. EVP_PKEY* prvKey = NULL;
  18294. X509* x509 = NULL;
  18295. WOLFSSL_SMALL_STACK_STATIC const unsigned char msg[] = "sugar slapped";
  18296. const unsigned char* clikey;
  18297. long cliKeySz;
  18298. size_t outlen;
  18299. int keyLenBits = 2048;
  18300. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18301. unsigned char *tmp = (unsigned char *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18302. unsigned char *cipher = (unsigned char *)XMALLOC(RSA_TEST_BYTES, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18303. unsigned char *plain = (unsigned char *)XMALLOC(RSA_TEST_BYTES, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18304. if ((tmp == NULL) ||
  18305. (cipher == NULL) ||
  18306. (plain == NULL)) {
  18307. ret = -9015;
  18308. goto openssl_pkey1_test_done;
  18309. }
  18310. #else
  18311. unsigned char tmp[FOURK_BUF];
  18312. unsigned char cipher[RSA_TEST_BYTES];
  18313. unsigned char plain[RSA_TEST_BYTES];
  18314. #endif
  18315. #if defined(USE_CERT_BUFFERS_1024)
  18316. XMEMCPY(tmp, client_key_der_1024, sizeof_client_key_der_1024);
  18317. cliKeySz = (long)sizeof_client_key_der_1024;
  18318. x509 = wolfSSL_X509_load_certificate_buffer(client_cert_der_1024,
  18319. sizeof_client_cert_der_1024, SSL_FILETYPE_ASN1);
  18320. keyLenBits = 1024;
  18321. #elif defined(USE_CERT_BUFFERS_2048)
  18322. XMEMCPY(tmp, client_key_der_2048, sizeof_client_key_der_2048);
  18323. cliKeySz = (long)sizeof_client_key_der_2048;
  18324. x509 = wolfSSL_X509_load_certificate_buffer(client_cert_der_2048,
  18325. sizeof_client_cert_der_2048, SSL_FILETYPE_ASN1);
  18326. #elif defined(USE_CERT_BUFFERS_3072)
  18327. XMEMCPY(tmp, client_key_der_3072, sizeof_client_key_der_3072);
  18328. cliKeySz = (long)sizeof_client_key_der_3072;
  18329. x509 = wolfSSL_X509_load_certificate_buffer(client_cert_der_3072,
  18330. sizeof_client_cert_der_3072, SSL_FILETYPE_ASN1);
  18331. keyLenBits = 3072;
  18332. #elif defined(USE_CERT_BUFFERS_4096)
  18333. XMEMCPY(tmp, client_key_der_4096, sizeof_client_key_der_4096);
  18334. cliKeySz = (long)sizeof_client_key_der_4096;
  18335. x509 = wolfSSL_X509_load_certificate_buffer(client_cert_der_4096,
  18336. sizeof_client_cert_der_4096, SSL_FILETYPE_ASN1);
  18337. keyLenBits = 4096;
  18338. #else
  18339. {
  18340. XFILE f;
  18341. f = XFOPEN(clientKey, "rb");
  18342. if (!f) {
  18343. err_sys("can't open ./certs/client-key.der, "
  18344. "Please run from wolfSSL home dir", -41);
  18345. ret = -9000;
  18346. goto openssl_pkey1_test_done;
  18347. }
  18348. cliKeySz = (long)XFREAD(tmp, 1, FOURK_BUF, f);
  18349. XFCLOSE(f);
  18350. }
  18351. /* using existing wolfSSL api to get public and private key */
  18352. x509 = wolfSSL_X509_load_certificate_file(clientCert, SSL_FILETYPE_ASN1);
  18353. #endif /* USE_CERT_BUFFERS */
  18354. clikey = tmp;
  18355. if ((prvKey = EVP_PKEY_new()) == NULL) {
  18356. ret = -9001;
  18357. goto openssl_pkey1_test_done;
  18358. }
  18359. EVP_PKEY_free(prvKey);
  18360. prvKey = NULL;
  18361. if (x509 == NULL) {
  18362. ret = -9002;
  18363. goto openssl_pkey1_test_done;
  18364. }
  18365. pubKey = X509_get_pubkey(x509);
  18366. if (pubKey == NULL) {
  18367. ret = -9003;
  18368. goto openssl_pkey1_test_done;
  18369. }
  18370. prvKey = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &clikey, cliKeySz);
  18371. if (prvKey == NULL) {
  18372. ret = -9004;
  18373. goto openssl_pkey1_test_done;
  18374. }
  18375. /* phase 2 API to create EVP_PKEY_CTX and encrypt/decrypt */
  18376. if (EVP_PKEY_bits(prvKey) != keyLenBits) {
  18377. ret = -9005;
  18378. goto openssl_pkey1_test_done;
  18379. }
  18380. if (EVP_PKEY_size(prvKey) != keyLenBits/8) {
  18381. ret = -9006;
  18382. goto openssl_pkey1_test_done;
  18383. }
  18384. dec = EVP_PKEY_CTX_new(prvKey, NULL);
  18385. enc = EVP_PKEY_CTX_new(pubKey, NULL);
  18386. if (dec == NULL || enc == NULL) {
  18387. ret = -9007;
  18388. goto openssl_pkey1_test_done;
  18389. }
  18390. if (EVP_PKEY_decrypt_init(dec) != 1) {
  18391. ret = -9008;
  18392. goto openssl_pkey1_test_done;
  18393. }
  18394. if (EVP_PKEY_encrypt_init(enc) != 1) {
  18395. ret = -9009;
  18396. goto openssl_pkey1_test_done;
  18397. }
  18398. if (EVP_PKEY_CTX_set_rsa_padding(dec, RSA_PKCS1_PADDING) <= 0) {
  18399. ret = -9010;
  18400. goto openssl_pkey1_test_done;
  18401. }
  18402. #ifndef HAVE_FIPS
  18403. if (EVP_PKEY_CTX_set_rsa_padding(dec, RSA_PKCS1_OAEP_PADDING) <= 0){
  18404. ret = -9011;
  18405. goto openssl_pkey1_test_done;
  18406. }
  18407. if (EVP_PKEY_CTX_set_rsa_padding(enc, RSA_PKCS1_OAEP_PADDING) <= 0) {
  18408. ret = -9012;
  18409. goto openssl_pkey1_test_done;
  18410. }
  18411. #endif
  18412. XMEMSET(cipher, 0, RSA_TEST_BYTES);
  18413. outlen = keyLenBits/8;
  18414. if (EVP_PKEY_encrypt(enc, cipher, &outlen, msg, sizeof(msg)) < 0) {
  18415. ret = -9013;
  18416. goto openssl_pkey1_test_done;
  18417. }
  18418. XMEMSET(plain, 0, RSA_TEST_BYTES);
  18419. if (EVP_PKEY_decrypt(dec, plain, &outlen, cipher, outlen) != 1) {
  18420. ret = -9014;
  18421. goto openssl_pkey1_test_done;
  18422. }
  18423. openssl_pkey1_test_done:
  18424. if (pubKey != NULL) {
  18425. EVP_PKEY_free(pubKey);
  18426. }
  18427. if (prvKey != NULL) {
  18428. EVP_PKEY_free(prvKey);
  18429. }
  18430. if (dec != NULL) {
  18431. EVP_PKEY_CTX_free(dec);
  18432. }
  18433. if (enc != NULL) {
  18434. EVP_PKEY_CTX_free(enc);
  18435. }
  18436. if (x509 != NULL) {
  18437. X509_free(x509);
  18438. }
  18439. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18440. if (tmp != NULL)
  18441. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18442. if (cipher != NULL)
  18443. XFREE(cipher, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18444. if (plain != NULL)
  18445. XFREE(plain, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18446. #endif
  18447. #endif
  18448. return ret;
  18449. }
  18450. #define ERR_BASE_EVPSIG (-5100)
  18451. WOLFSSL_TEST_SUBROUTINE int openssl_evpSig_test(void)
  18452. {
  18453. #if !defined(NO_RSA) && !defined(NO_SHA) && !defined(HAVE_USER_RSA)
  18454. byte* prvTmp;
  18455. byte* pubTmp;
  18456. int prvBytes;
  18457. int pubBytes;
  18458. RSA *prvRsa;
  18459. RSA *pubRsa;
  18460. EVP_PKEY *prvPkey;
  18461. EVP_PKEY *pubPkey;
  18462. EVP_MD_CTX* sign;
  18463. EVP_MD_CTX* verf;
  18464. char msg[] = "see spot run";
  18465. unsigned char sig[256];
  18466. unsigned int sigSz;
  18467. const void* pt;
  18468. unsigned int count;
  18469. int ret, ret1, ret2;
  18470. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  18471. XFILE keyFile;
  18472. XFILE keypubFile;
  18473. char cliKey[] = "./certs/client-key.der";
  18474. char cliKeypub[] = "./certs/client-keyPub.der";
  18475. #endif
  18476. prvTmp = (byte*)XMALLOC(FOURK_BUFF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18477. if (prvTmp == NULL)
  18478. return ERR_BASE_EVPSIG-1;
  18479. pubTmp = (byte*)XMALLOC(FOURK_BUFF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18480. if (pubTmp == NULL) {
  18481. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18482. return ERR_BASE_EVPSIG-2;
  18483. }
  18484. #ifdef USE_CERT_BUFFERS_1024
  18485. XMEMCPY(prvTmp, client_key_der_1024, sizeof_client_key_der_1024);
  18486. prvBytes = sizeof_client_key_der_1024;
  18487. XMEMCPY(pubTmp, client_keypub_der_1024, sizeof_client_keypub_der_1024);
  18488. pubBytes = sizeof_client_keypub_der_1024;
  18489. #elif defined(USE_CERT_BUFFERS_2048)
  18490. XMEMCPY(prvTmp, client_key_der_2048, sizeof_client_key_der_2048);
  18491. prvBytes = sizeof_client_key_der_2048;
  18492. XMEMCPY(pubTmp, client_keypub_der_2048, sizeof_client_keypub_der_2048);
  18493. pubBytes = sizeof_client_keypub_der_2048;
  18494. #else
  18495. keyFile = XFOPEN(cliKey, "rb");
  18496. if (!keyFile) {
  18497. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18498. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18499. err_sys("can't open ./certs/client-key.der, "
  18500. "Please run from wolfSSL home dir", -40);
  18501. return ERR_BASE_EVPSIG-3;
  18502. }
  18503. prvBytes = (int)XFREAD(prvTmp, 1, (int)FOURK_BUFF, keyFile);
  18504. XFCLOSE(keyFile);
  18505. keypubFile = XFOPEN(cliKeypub, "rb");
  18506. if (!keypubFile) {
  18507. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18508. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18509. err_sys("can't open ./certs/client-cert.der, "
  18510. "Please run from wolfSSL home dir", -41);
  18511. return ERR_BASE_EVPSIG-4;
  18512. }
  18513. pubBytes = (int)XFREAD(pubTmp, 1, (int)FOURK_BUFF, keypubFile);
  18514. XFCLOSE(keypubFile);
  18515. #endif /* USE_CERT_BUFFERS */
  18516. prvRsa = wolfSSL_RSA_new();
  18517. pubRsa = wolfSSL_RSA_new();
  18518. if((prvRsa == NULL) || (pubRsa == NULL)){
  18519. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18520. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18521. err_sys("ERROR with RSA_new", -9100);
  18522. return ERR_BASE_EVPSIG-5;
  18523. }
  18524. ret1 = wolfSSL_RSA_LoadDer_ex(prvRsa, prvTmp, prvBytes, WOLFSSL_RSA_LOAD_PRIVATE);
  18525. ret2 = wolfSSL_RSA_LoadDer_ex(pubRsa, pubTmp, pubBytes, WOLFSSL_RSA_LOAD_PUBLIC);
  18526. if((ret1 != SSL_SUCCESS) || (ret2 != SSL_SUCCESS)){
  18527. printf("error with RSA_LoadDer_ex\n");
  18528. return ERR_BASE_EVPSIG-6;
  18529. }
  18530. prvPkey = wolfSSL_EVP_PKEY_new();
  18531. pubPkey = wolfSSL_EVP_PKEY_new();
  18532. if((prvPkey == NULL) || (pubPkey == NULL)){
  18533. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18534. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18535. printf("error with KEY_new\n");
  18536. return ERR_BASE_EVPSIG-7;
  18537. }
  18538. ret1 = wolfSSL_EVP_PKEY_set1_RSA(prvPkey, prvRsa);
  18539. ret2 = wolfSSL_EVP_PKEY_set1_RSA(pubPkey, pubRsa);
  18540. if((ret1 != 1) || (ret2 != 1)){
  18541. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18542. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18543. printf("error with EVP_PKEY_set1_RSA\n");
  18544. return ERR_BASE_EVPSIG-8;
  18545. }
  18546. /****************** sign and verify *******************/
  18547. sign = EVP_MD_CTX_create();
  18548. verf = EVP_MD_CTX_create();
  18549. if((sign == NULL)||(verf == NULL)){
  18550. printf("error with EVP_MD_CTX_create\n");
  18551. EVP_MD_CTX_destroy(sign);
  18552. EVP_MD_CTX_destroy(verf);
  18553. return ERR_BASE_EVPSIG-10;
  18554. }
  18555. ret = EVP_SignInit(sign, EVP_sha1());
  18556. if (ret != SSL_SUCCESS){
  18557. printf("error with EVP_SignInit\n");
  18558. EVP_MD_CTX_destroy(sign);
  18559. EVP_MD_CTX_destroy(verf);
  18560. return ERR_BASE_EVPSIG-11;
  18561. }
  18562. count = sizeof(msg);
  18563. show("message = ", (char *)msg, count);
  18564. /* sign */
  18565. XMEMSET(sig, 0, sizeof(sig));
  18566. pt = (const void*)msg;
  18567. ret1 = EVP_SignUpdate(sign, pt, count);
  18568. ret2 = EVP_SignFinal(sign, sig, &sigSz, prvPkey);
  18569. if((ret1 != SSL_SUCCESS) || (ret2 != SSL_SUCCESS)){
  18570. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18571. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18572. EVP_MD_CTX_destroy(sign);
  18573. EVP_MD_CTX_destroy(verf);
  18574. printf("error with EVP_MD_CTX_create\n");
  18575. return ERR_BASE_EVPSIG-12;
  18576. }
  18577. show("signature = ", (char *)sig, sigSz);
  18578. /* verify */
  18579. pt = (const void*)msg;
  18580. ret1 = EVP_VerifyInit(verf, EVP_sha1());
  18581. ret2 = EVP_VerifyUpdate(verf, pt, count);
  18582. if((ret1 != SSL_SUCCESS) || (ret2 != SSL_SUCCESS)){
  18583. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18584. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18585. EVP_MD_CTX_destroy(sign);
  18586. EVP_MD_CTX_destroy(verf);
  18587. printf("error with EVP_Verify\n");
  18588. return ERR_BASE_EVPSIG-13;
  18589. }
  18590. if (EVP_VerifyFinal(verf, sig, sigSz, pubPkey) != 1) {
  18591. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18592. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18593. EVP_MD_CTX_destroy(sign);
  18594. EVP_MD_CTX_destroy(verf);
  18595. printf("error with EVP_VerifyFinal\n");
  18596. return ERR_BASE_EVPSIG-14;
  18597. }
  18598. /* expect fail without update */
  18599. EVP_VerifyInit(verf, EVP_sha1());
  18600. if (EVP_VerifyFinal(verf, sig, sigSz, pubPkey) == 1) {
  18601. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18602. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18603. EVP_MD_CTX_destroy(sign);
  18604. EVP_MD_CTX_destroy(verf);
  18605. printf("EVP_VerifyInit without update not detected\n");
  18606. return ERR_BASE_EVPSIG-15;
  18607. }
  18608. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18609. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18610. EVP_MD_CTX_destroy(sign);
  18611. EVP_MD_CTX_destroy(verf);
  18612. wolfSSL_RSA_free(prvRsa);
  18613. wolfSSL_RSA_free(pubRsa);
  18614. EVP_PKEY_free(pubPkey);
  18615. EVP_PKEY_free(prvPkey);
  18616. #endif /* NO_RSA */
  18617. return 0;
  18618. }
  18619. #endif /* OPENSSL_EXTRA */
  18620. #ifndef NO_PWDBASED
  18621. #ifdef HAVE_SCRYPT
  18622. /* Test vectors taken from RFC 7914: scrypt PBKDF - Section 12. */
  18623. WOLFSSL_TEST_SUBROUTINE int scrypt_test(void)
  18624. {
  18625. #ifdef HAVE_FIPS
  18626. /* RFC 7914 test vector keys are too short for FIPS. */
  18627. #else
  18628. int ret;
  18629. byte derived[64];
  18630. WOLFSSL_SMALL_STACK_STATIC const byte verify1[] = {
  18631. 0x77, 0xd6, 0x57, 0x62, 0x38, 0x65, 0x7b, 0x20,
  18632. 0x3b, 0x19, 0xca, 0x42, 0xc1, 0x8a, 0x04, 0x97,
  18633. 0xf1, 0x6b, 0x48, 0x44, 0xe3, 0x07, 0x4a, 0xe8,
  18634. 0xdf, 0xdf, 0xfa, 0x3f, 0xed, 0xe2, 0x14, 0x42,
  18635. 0xfc, 0xd0, 0x06, 0x9d, 0xed, 0x09, 0x48, 0xf8,
  18636. 0x32, 0x6a, 0x75, 0x3a, 0x0f, 0xc8, 0x1f, 0x17,
  18637. 0xe8, 0xd3, 0xe0, 0xfb, 0x2e, 0x0d, 0x36, 0x28,
  18638. 0xcf, 0x35, 0xe2, 0x0c, 0x38, 0xd1, 0x89, 0x06
  18639. };
  18640. WOLFSSL_SMALL_STACK_STATIC const byte verify2[] = {
  18641. 0xfd, 0xba, 0xbe, 0x1c, 0x9d, 0x34, 0x72, 0x00,
  18642. 0x78, 0x56, 0xe7, 0x19, 0x0d, 0x01, 0xe9, 0xfe,
  18643. 0x7c, 0x6a, 0xd7, 0xcb, 0xc8, 0x23, 0x78, 0x30,
  18644. 0xe7, 0x73, 0x76, 0x63, 0x4b, 0x37, 0x31, 0x62,
  18645. 0x2e, 0xaf, 0x30, 0xd9, 0x2e, 0x22, 0xa3, 0x88,
  18646. 0x6f, 0xf1, 0x09, 0x27, 0x9d, 0x98, 0x30, 0xda,
  18647. 0xc7, 0x27, 0xaf, 0xb9, 0x4a, 0x83, 0xee, 0x6d,
  18648. 0x83, 0x60, 0xcb, 0xdf, 0xa2, 0xcc, 0x06, 0x40
  18649. };
  18650. #if !defined(BENCH_EMBEDDED) && !defined(WOLFSSL_LINUXKM) && !defined(HAVE_INTEL_QA)
  18651. WOLFSSL_SMALL_STACK_STATIC const byte verify3[] = {
  18652. 0x70, 0x23, 0xbd, 0xcb, 0x3a, 0xfd, 0x73, 0x48,
  18653. 0x46, 0x1c, 0x06, 0xcd, 0x81, 0xfd, 0x38, 0xeb,
  18654. 0xfd, 0xa8, 0xfb, 0xba, 0x90, 0x4f, 0x8e, 0x3e,
  18655. 0xa9, 0xb5, 0x43, 0xf6, 0x54, 0x5d, 0xa1, 0xf2,
  18656. 0xd5, 0x43, 0x29, 0x55, 0x61, 0x3f, 0x0f, 0xcf,
  18657. 0x62, 0xd4, 0x97, 0x05, 0x24, 0x2a, 0x9a, 0xf9,
  18658. 0xe6, 0x1e, 0x85, 0xdc, 0x0d, 0x65, 0x1e, 0x40,
  18659. 0xdf, 0xcf, 0x01, 0x7b, 0x45, 0x57, 0x58, 0x87
  18660. };
  18661. #endif
  18662. #ifdef SCRYPT_TEST_ALL
  18663. /* Test case is very slow.
  18664. * Use for confirmation after code change or new platform.
  18665. */
  18666. WOLFSSL_SMALL_STACK_STATIC const byte verify4[] = {
  18667. 0x21, 0x01, 0xcb, 0x9b, 0x6a, 0x51, 0x1a, 0xae,
  18668. 0xad, 0xdb, 0xbe, 0x09, 0xcf, 0x70, 0xf8, 0x81,
  18669. 0xec, 0x56, 0x8d, 0x57, 0x4a, 0x2f, 0xfd, 0x4d,
  18670. 0xab, 0xe5, 0xee, 0x98, 0x20, 0xad, 0xaa, 0x47,
  18671. 0x8e, 0x56, 0xfd, 0x8f, 0x4b, 0xa5, 0xd0, 0x9f,
  18672. 0xfa, 0x1c, 0x6d, 0x92, 0x7c, 0x40, 0xf4, 0xc3,
  18673. 0x37, 0x30, 0x40, 0x49, 0xe8, 0xa9, 0x52, 0xfb,
  18674. 0xcb, 0xf4, 0x5c, 0x6f, 0xa7, 0x7a, 0x41, 0xa4
  18675. };
  18676. #endif
  18677. ret = wc_scrypt(derived, NULL, 0, NULL, 0, 4, 1, 1, sizeof(verify1));
  18678. if (ret != 0)
  18679. return -9200;
  18680. if (XMEMCMP(derived, verify1, sizeof(verify1)) != 0)
  18681. return -9201;
  18682. ret = wc_scrypt(derived, (byte*)"password", 8, (byte*)"NaCl", 4, 10, 8, 16,
  18683. sizeof(verify2));
  18684. if (ret != 0)
  18685. return -9202;
  18686. if (XMEMCMP(derived, verify2, sizeof(verify2)) != 0)
  18687. return -9203;
  18688. /* Test case with parallel overflowing */
  18689. ret = wc_scrypt(derived, (byte*)"password", 16, (byte*)"NaCl", 16, 2, 4, 8388608,
  18690. sizeof(verify2));
  18691. if (ret != BAD_FUNC_ARG)
  18692. return -9210;
  18693. /* Don't run these test on embedded, since they use large mallocs */
  18694. #if !defined(BENCH_EMBEDDED) && !defined(WOLFSSL_LINUXKM) && !defined(HAVE_INTEL_QA)
  18695. ret = wc_scrypt(derived, (byte*)"pleaseletmein", 13,
  18696. (byte*)"SodiumChloride", 14, 14, 8, 1, sizeof(verify3));
  18697. if (ret != 0)
  18698. return -9204;
  18699. if (XMEMCMP(derived, verify3, sizeof(verify3)) != 0)
  18700. return -9205;
  18701. #ifdef SCRYPT_TEST_ALL
  18702. ret = wc_scrypt(derived, (byte*)"pleaseletmein", 13,
  18703. (byte*)"SodiumChloride", 14, 20, 8, 1, sizeof(verify4));
  18704. if (ret != 0)
  18705. return -9206;
  18706. if (XMEMCMP(derived, verify4, sizeof(verify4)) != 0)
  18707. return -9207;
  18708. #endif
  18709. #endif /* !BENCH_EMBEDDED && !defined(WOLFSSL_LINUXKM) && !HAVE_INTEL_QA */
  18710. ret = wc_scrypt_ex(derived, (byte*)"password", 8, (byte*)"NaCl", 4, 1<<10,
  18711. 8, 16, sizeof(verify2));
  18712. if (ret != 0)
  18713. return -9208;
  18714. if (XMEMCMP(derived, verify2, sizeof(verify2)) != 0)
  18715. return -9209;
  18716. #endif /* !HAVE_FIPS */
  18717. return 0;
  18718. }
  18719. #endif
  18720. #ifdef HAVE_PKCS12
  18721. WOLFSSL_TEST_SUBROUTINE int pkcs12_test(void)
  18722. {
  18723. WOLFSSL_SMALL_STACK_STATIC const byte passwd[] = { 0x00, 0x73, 0x00, 0x6d, 0x00, 0x65, 0x00, 0x67,
  18724. 0x00, 0x00 };
  18725. WOLFSSL_SMALL_STACK_STATIC const byte salt[] = { 0x0a, 0x58, 0xCF, 0x64, 0x53, 0x0d, 0x82, 0x3f };
  18726. WOLFSSL_SMALL_STACK_STATIC const byte passwd2[] = { 0x00, 0x71, 0x00, 0x75, 0x00, 0x65, 0x00, 0x65,
  18727. 0x00, 0x67, 0x00, 0x00 };
  18728. WOLFSSL_SMALL_STACK_STATIC const byte salt2[] = { 0x16, 0x82, 0xC0, 0xfC, 0x5b, 0x3f, 0x7e, 0xc5 };
  18729. byte derived[64];
  18730. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  18731. 0x27, 0xE9, 0x0D, 0x7E, 0xD5, 0xA1, 0xC4, 0x11,
  18732. 0xBA, 0x87, 0x8B, 0xC0, 0x90, 0xF5, 0xCE, 0xBE,
  18733. 0x5E, 0x9D, 0x5F, 0xE3, 0xD6, 0x2B, 0x73, 0xAA
  18734. };
  18735. WOLFSSL_SMALL_STACK_STATIC const byte verify2[] = {
  18736. 0x90, 0x1B, 0x49, 0x70, 0xF0, 0x94, 0xF0, 0xF8,
  18737. 0x45, 0xC0, 0xF3, 0xF3, 0x13, 0x59, 0x18, 0x6A,
  18738. 0x35, 0xE3, 0x67, 0xFE, 0xD3, 0x21, 0xFD, 0x7C
  18739. };
  18740. int id = 1;
  18741. int kLen = 24;
  18742. int iterations = 1;
  18743. int ret = wc_PKCS12_PBKDF(derived, passwd, sizeof(passwd), salt, 8,
  18744. iterations, kLen, WC_SHA256, id);
  18745. if (ret < 0)
  18746. return -9300;
  18747. if (XMEMCMP(derived, verify, kLen) != 0)
  18748. return -9301;
  18749. iterations = 1000;
  18750. ret = wc_PKCS12_PBKDF(derived, passwd2, sizeof(passwd2), salt2, 8,
  18751. iterations, kLen, WC_SHA256, id);
  18752. if (ret < 0)
  18753. return -9302;
  18754. ret = wc_PKCS12_PBKDF_ex(derived, passwd2, sizeof(passwd2), salt2, 8,
  18755. iterations, kLen, WC_SHA256, id, HEAP_HINT);
  18756. if (ret < 0)
  18757. return -9303;
  18758. if (XMEMCMP(derived, verify2, 24) != 0)
  18759. return -9304;
  18760. return 0;
  18761. }
  18762. #endif /* HAVE_PKCS12 */
  18763. #if defined(HAVE_PBKDF2) && !defined(NO_SHA256) && !defined(NO_HMAC)
  18764. WOLFSSL_TEST_SUBROUTINE int pbkdf2_test(void)
  18765. {
  18766. char passwd[] = "passwordpassword";
  18767. WOLFSSL_SMALL_STACK_STATIC const byte salt[] = { 0x78, 0x57, 0x8E, 0x5a, 0x5d, 0x63, 0xcb, 0x06 };
  18768. int iterations = 2048;
  18769. int kLen = 24;
  18770. byte derived[64];
  18771. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  18772. 0x43, 0x6d, 0xb5, 0xe8, 0xd0, 0xfb, 0x3f, 0x35, 0x42, 0x48, 0x39, 0xbc,
  18773. 0x2d, 0xd4, 0xf9, 0x37, 0xd4, 0x95, 0x16, 0xa7, 0x2a, 0x9a, 0x21, 0xd1
  18774. };
  18775. int ret = wc_PBKDF2_ex(derived, (byte*)passwd, (int)XSTRLEN(passwd), salt,
  18776. (int)sizeof(salt), iterations, kLen, WC_SHA256, HEAP_HINT, devId);
  18777. if (ret != 0)
  18778. return ret;
  18779. if (XMEMCMP(derived, verify, sizeof(verify)) != 0)
  18780. return -9400;
  18781. return 0;
  18782. }
  18783. #endif /* HAVE_PBKDF2 && !NO_SHA256 && !NO_HMAC */
  18784. #if defined(HAVE_PBKDF1) && !defined(NO_SHA)
  18785. WOLFSSL_TEST_SUBROUTINE int pbkdf1_test(void)
  18786. {
  18787. char passwd[] = "password";
  18788. WOLFSSL_SMALL_STACK_STATIC const byte salt[] = { 0x78, 0x57, 0x8E, 0x5a, 0x5d, 0x63, 0xcb, 0x06 };
  18789. int iterations = 1000;
  18790. int kLen = 16;
  18791. byte derived[16];
  18792. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  18793. 0xDC, 0x19, 0x84, 0x7E, 0x05, 0xC6, 0x4D, 0x2F,
  18794. 0xAF, 0x10, 0xEB, 0xFB, 0x4A, 0x3D, 0x2A, 0x20
  18795. };
  18796. int ret = wc_PBKDF1_ex(derived, kLen, NULL, 0, (byte*)passwd,
  18797. (int)XSTRLEN(passwd), salt, (int)sizeof(salt), iterations, WC_SHA,
  18798. HEAP_HINT);
  18799. if (ret != 0)
  18800. return ret;
  18801. if (XMEMCMP(derived, verify, sizeof(verify)) != 0)
  18802. return -9500;
  18803. return 0;
  18804. }
  18805. #endif /* HAVE_PBKDF2 && !NO_SHA */
  18806. WOLFSSL_TEST_SUBROUTINE int pwdbased_test(void)
  18807. {
  18808. int ret = 0;
  18809. #if defined(HAVE_PBKDF1) && !defined(NO_SHA)
  18810. ret = pbkdf1_test();
  18811. if (ret != 0)
  18812. return ret;
  18813. #endif
  18814. #if defined(HAVE_PBKDF2) && !defined(NO_SHA256) && !defined(NO_HMAC)
  18815. ret = pbkdf2_test();
  18816. if (ret != 0)
  18817. return ret;
  18818. #endif
  18819. #ifdef HAVE_PKCS12
  18820. ret = pkcs12_test();
  18821. if (ret != 0)
  18822. return ret;
  18823. #endif
  18824. #ifdef HAVE_SCRYPT
  18825. ret = scrypt_test();
  18826. if (ret != 0)
  18827. return ret;
  18828. #endif
  18829. return ret;
  18830. }
  18831. #endif /* NO_PWDBASED */
  18832. #if defined(HAVE_HKDF) && !defined(NO_HMAC)
  18833. /* hkdf_test has issue with WOLFSSL_TEST_SUBROUTINE set on Xilinx with afalg */
  18834. static int hkdf_test(void)
  18835. {
  18836. int ret = 0;
  18837. #if !defined(NO_SHA) || !defined(NO_SHA256)
  18838. int L;
  18839. byte okm1[42];
  18840. byte ikm1[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  18841. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  18842. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b };
  18843. #ifndef HAVE_FIPS
  18844. byte salt1[13] ={ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18845. 0x08, 0x09, 0x0a, 0x0b, 0x0c };
  18846. byte info1[10] ={ 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
  18847. 0xf8, 0xf9 };
  18848. #endif
  18849. #ifndef NO_SHA
  18850. byte res1[42] = { 0x0a, 0xc1, 0xaf, 0x70, 0x02, 0xb3, 0xd7, 0x61,
  18851. 0xd1, 0xe5, 0x52, 0x98, 0xda, 0x9d, 0x05, 0x06,
  18852. 0xb9, 0xae, 0x52, 0x05, 0x72, 0x20, 0xa3, 0x06,
  18853. 0xe0, 0x7b, 0x6b, 0x87, 0xe8, 0xdf, 0x21, 0xd0,
  18854. 0xea, 0x00, 0x03, 0x3d, 0xe0, 0x39, 0x84, 0xd3,
  18855. 0x49, 0x18 };
  18856. #ifndef HAVE_FIPS
  18857. byte res2[42] = { 0x08, 0x5a, 0x01, 0xea, 0x1b, 0x10, 0xf3, 0x69,
  18858. 0x33, 0x06, 0x8b, 0x56, 0xef, 0xa5, 0xad, 0x81,
  18859. 0xa4, 0xf1, 0x4b, 0x82, 0x2f, 0x5b, 0x09, 0x15,
  18860. 0x68, 0xa9, 0xcd, 0xd4, 0xf1, 0x55, 0xfd, 0xa2,
  18861. 0xc2, 0x2e, 0x42, 0x24, 0x78, 0xd3, 0x05, 0xf3,
  18862. 0xf8, 0x96 };
  18863. #endif
  18864. #endif /* !NO_SHA */
  18865. #ifndef NO_SHA256
  18866. byte res3[42] = { 0x8d, 0xa4, 0xe7, 0x75, 0xa5, 0x63, 0xc1, 0x8f,
  18867. 0x71, 0x5f, 0x80, 0x2a, 0x06, 0x3c, 0x5a, 0x31,
  18868. 0xb8, 0xa1, 0x1f, 0x5c, 0x5e, 0xe1, 0x87, 0x9e,
  18869. 0xc3, 0x45, 0x4e, 0x5f, 0x3c, 0x73, 0x8d, 0x2d,
  18870. 0x9d, 0x20, 0x13, 0x95, 0xfa, 0xa4, 0xb6, 0x1a,
  18871. 0x96, 0xc8 };
  18872. #ifndef HAVE_FIPS
  18873. byte res4[42] = { 0x3c, 0xb2, 0x5f, 0x25, 0xfa, 0xac, 0xd5, 0x7a,
  18874. 0x90, 0x43, 0x4f, 0x64, 0xd0, 0x36, 0x2f, 0x2a,
  18875. 0x2d, 0x2d, 0x0a, 0x90, 0xcf, 0x1a, 0x5a, 0x4c,
  18876. 0x5d, 0xb0, 0x2d, 0x56, 0xec, 0xc4, 0xc5, 0xbf,
  18877. 0x34, 0x00, 0x72, 0x08, 0xd5, 0xb8, 0x87, 0x18,
  18878. 0x58, 0x65 };
  18879. #endif
  18880. #endif /* !NO_SHA256 */
  18881. XMEMSET(okm1, 0, sizeof(okm1));
  18882. L = (int)sizeof(okm1);
  18883. #ifndef NO_SHA
  18884. ret = wc_HKDF(WC_SHA, ikm1, (word32)sizeof(ikm1), NULL, 0, NULL, 0,
  18885. okm1, L);
  18886. if (ret != 0)
  18887. return -9700;
  18888. if (XMEMCMP(okm1, res1, L) != 0)
  18889. return -9701;
  18890. #ifndef HAVE_FIPS
  18891. /* fips can't have key size under 14 bytes, salt is key too */
  18892. L = (int)sizeof(okm1);
  18893. ret = wc_HKDF(WC_SHA, ikm1, 11, salt1, (word32)sizeof(salt1),
  18894. info1, (word32)sizeof(info1), okm1, L);
  18895. if (ret != 0)
  18896. return -9702;
  18897. if (XMEMCMP(okm1, res2, L) != 0)
  18898. return -9703;
  18899. #endif /* HAVE_FIPS */
  18900. #endif /* !NO_SHA */
  18901. #ifndef NO_SHA256
  18902. ret = wc_HKDF(WC_SHA256, ikm1, (word32)sizeof(ikm1), NULL, 0, NULL, 0,
  18903. okm1, L);
  18904. if (ret != 0)
  18905. return -9704;
  18906. if (XMEMCMP(okm1, res3, L) != 0)
  18907. return -9705;
  18908. #ifndef HAVE_FIPS
  18909. /* fips can't have key size under 14 bytes, salt is key too */
  18910. ret = wc_HKDF(WC_SHA256, ikm1, (word32)sizeof(ikm1),
  18911. salt1, (word32)sizeof(salt1), info1, (word32)sizeof(info1), okm1, L);
  18912. if (ret != 0)
  18913. return -9706;
  18914. if (XMEMCMP(okm1, res4, L) != 0)
  18915. return -9707;
  18916. #endif /* HAVE_FIPS */
  18917. #endif /* !NO_SHA256 */
  18918. #endif /* !NO_SHA || !NO_SHA256 */
  18919. return ret;
  18920. }
  18921. #endif /* HAVE_HKDF */
  18922. #ifdef WOLFSSL_WOLFSSH
  18923. typedef struct {
  18924. byte hashId;
  18925. byte keyId;
  18926. const byte* k;
  18927. word32 kSz;
  18928. const byte* h;
  18929. word32 hSz;
  18930. const byte* sessionId;
  18931. word32 sessionIdSz;
  18932. const byte* expectedKey;
  18933. word32 expectedKeySz;
  18934. } SshKdfTestVector;
  18935. /** Test Vector Set #3: SHA-256 **/
  18936. static const byte sshKdfTvSet3k[] = {
  18937. 0x6A, 0xC3, 0x82, 0xEA, 0xAC, 0xA0, 0x93, 0xE1,
  18938. 0x25, 0xE2, 0x5C, 0x24, 0xBE, 0xBC, 0x84, 0x64,
  18939. 0x0C, 0x11, 0x98, 0x75, 0x07, 0x34, 0x4B, 0x5C,
  18940. 0x73, 0x9C, 0xEB, 0x84, 0xA9, 0xE0, 0xB2, 0x22,
  18941. 0xB9, 0xA8, 0xB5, 0x1C, 0x83, 0x9E, 0x5E, 0xBE,
  18942. 0x49, 0xCF, 0xAD, 0xBF, 0xB3, 0x95, 0x99, 0x76,
  18943. 0x4E, 0xD5, 0x22, 0x09, 0x9D, 0xC9, 0x12, 0x75,
  18944. 0x19, 0x50, 0xDC, 0x7D, 0xC9, 0x7F, 0xBD, 0xC0,
  18945. 0x63, 0x28, 0xB6, 0x8F, 0x22, 0x78, 0x1F, 0xD3,
  18946. 0x15, 0xAF, 0x56, 0x80, 0x09, 0xA5, 0x50, 0x9E,
  18947. 0x5B, 0x87, 0xA1, 0x1B, 0xF5, 0x27, 0xC0, 0x56,
  18948. 0xDA, 0xFF, 0xD8, 0x2A, 0xB6, 0xCB, 0xC2, 0x5C,
  18949. 0xCA, 0x37, 0x14, 0x34, 0x59, 0xE7, 0xBC, 0x63,
  18950. 0xBC, 0xDE, 0x52, 0x75, 0x7A, 0xDE, 0xB7, 0xDF,
  18951. 0x01, 0xCF, 0x12, 0x17, 0x3F, 0x1F, 0xEF, 0x81,
  18952. 0x02, 0xEC, 0x5A, 0xB1, 0x42, 0xC2, 0x13, 0xDD,
  18953. 0x9D, 0x30, 0x69, 0x62, 0x78, 0xA8, 0xD8, 0xBC,
  18954. 0x32, 0xDD, 0xE9, 0x59, 0x2D, 0x28, 0xC0, 0x78,
  18955. 0xC6, 0xD9, 0x2B, 0x94, 0x7D, 0x82, 0x5A, 0xCA,
  18956. 0xAB, 0x64, 0x94, 0x84, 0x6A, 0x49, 0xDE, 0x24,
  18957. 0xB9, 0x62, 0x3F, 0x48, 0x89, 0xE8, 0xAD, 0xC3,
  18958. 0x8E, 0x8C, 0x66, 0x9E, 0xFF, 0xEF, 0x17, 0x60,
  18959. 0x40, 0xAD, 0x94, 0x5E, 0x90, 0xA7, 0xD3, 0xEE,
  18960. 0xC1, 0x5E, 0xFE, 0xEE, 0x78, 0xAE, 0x71, 0x04,
  18961. 0x3C, 0x96, 0x51, 0x11, 0x03, 0xA1, 0x6B, 0xA7,
  18962. 0xCA, 0xF0, 0xAC, 0xD0, 0x64, 0x2E, 0xFD, 0xBE,
  18963. 0x80, 0x99, 0x34, 0xFA, 0xA1, 0xA5, 0xF1, 0xBD,
  18964. 0x11, 0x04, 0x36, 0x49, 0xB2, 0x5C, 0xCD, 0x1F,
  18965. 0xEE, 0x2E, 0x38, 0x81, 0x5D, 0x4D, 0x5F, 0x5F,
  18966. 0xC6, 0xB4, 0x10, 0x29, 0x69, 0xF2, 0x1C, 0x22,
  18967. 0xAE, 0x1B, 0x0E, 0x7D, 0x36, 0x03, 0xA5, 0x56,
  18968. 0xA1, 0x32, 0x62, 0xFF, 0x62, 0x8D, 0xE2, 0x22
  18969. };
  18970. static const byte sshKdfTvSet3h[] = {
  18971. 0x7B, 0x70, 0x01, 0x18, 0x5E, 0x25, 0x6D, 0x44,
  18972. 0x93, 0x44, 0x5F, 0x39, 0xA5, 0x5F, 0xB9, 0x05,
  18973. 0xE6, 0x32, 0x1F, 0x4B, 0x5D, 0xD8, 0xBB, 0xF3,
  18974. 0x10, 0x0D, 0x51, 0xBA, 0x0B, 0xDA, 0x3D, 0x2D
  18975. };
  18976. static const byte sshKdfTvSet3sid[] = {
  18977. 0x7B, 0x70, 0x01, 0x18, 0x5E, 0x25, 0x6D, 0x44,
  18978. 0x93, 0x44, 0x5F, 0x39, 0xA5, 0x5F, 0xB9, 0x05,
  18979. 0xE6, 0x32, 0x1F, 0x4B, 0x5D, 0xD8, 0xBB, 0xF3,
  18980. 0x10, 0x0D, 0x51, 0xBA, 0x0B, 0xDA, 0x3D, 0x2D
  18981. };
  18982. static const byte sshKdfTvSet3a[] = {
  18983. 0x81, 0xF0, 0x33, 0x0E, 0xF6, 0xF0, 0x53, 0x61,
  18984. 0xB3, 0x82, 0x3B, 0xFD, 0xED, 0x6E, 0x1D, 0xE9
  18985. };
  18986. static const byte sshKdfTvSet3b[] = {
  18987. 0x3F, 0x6F, 0xD2, 0x06, 0x5E, 0xEB, 0x2B, 0x0B,
  18988. 0x1D, 0x93, 0x19, 0x5A, 0x1F, 0xED, 0x48, 0xA5
  18989. };
  18990. static const byte sshKdfTvSet3c[] = {
  18991. 0xC3, 0x54, 0x71, 0x03, 0x4E, 0x6F, 0xD6, 0x54,
  18992. 0x76, 0x13, 0x17, 0x8E, 0x23, 0x43, 0x5F, 0x21
  18993. };
  18994. static const byte sshKdfTvSet3d[] = {
  18995. 0x7E, 0x9D, 0x79, 0x03, 0x20, 0x90, 0xD9, 0x9F,
  18996. 0x98, 0xB0, 0x15, 0x63, 0x4D, 0xD9, 0xF4, 0x62
  18997. };
  18998. static const byte sshKdfTvSet3e[] = {
  18999. 0x24, 0xEE, 0x55, 0x9A, 0xD7, 0xCE, 0x71, 0x2B,
  19000. 0x68, 0x5D, 0x0B, 0x22, 0x71, 0xE4, 0x43, 0xC1,
  19001. 0x7A, 0xB1, 0xD1, 0xDC, 0xEB, 0x5A, 0x36, 0x05,
  19002. 0x69, 0xD2, 0x5D, 0x5D, 0xC2, 0x43, 0x00, 0x2F
  19003. };
  19004. static const byte sshKdfTvSet3f[] = {
  19005. 0xC3, 0x41, 0x9C, 0x2B, 0x96, 0x62, 0x35, 0x86,
  19006. 0x9D, 0x71, 0x4B, 0xA5, 0xAC, 0x48, 0xDD, 0xB7,
  19007. 0xD9, 0xE3, 0x5C, 0x8C, 0x19, 0xAA, 0xC7, 0x34,
  19008. 0x22, 0x33, 0x7A, 0x37, 0x34, 0x53, 0x60, 0x7E
  19009. };
  19010. static const SshKdfTestVector sshKdfTestVectors[] = {
  19011. {WC_HASH_TYPE_SHA256, 'A',
  19012. sshKdfTvSet3k, sizeof(sshKdfTvSet3k),
  19013. sshKdfTvSet3h, sizeof(sshKdfTvSet3h),
  19014. sshKdfTvSet3sid, sizeof(sshKdfTvSet3sid),
  19015. sshKdfTvSet3a, sizeof(sshKdfTvSet3a)},
  19016. {WC_HASH_TYPE_SHA256, 'B',
  19017. sshKdfTvSet3k, sizeof(sshKdfTvSet3k),
  19018. sshKdfTvSet3h, sizeof(sshKdfTvSet3h),
  19019. sshKdfTvSet3sid, sizeof(sshKdfTvSet3sid),
  19020. sshKdfTvSet3b, sizeof(sshKdfTvSet3b)},
  19021. {WC_HASH_TYPE_SHA256, 'C',
  19022. sshKdfTvSet3k, sizeof(sshKdfTvSet3k),
  19023. sshKdfTvSet3h, sizeof(sshKdfTvSet3h),
  19024. sshKdfTvSet3sid, sizeof(sshKdfTvSet3sid),
  19025. sshKdfTvSet3c, sizeof(sshKdfTvSet3c)},
  19026. {WC_HASH_TYPE_SHA256, 'D',
  19027. sshKdfTvSet3k, sizeof(sshKdfTvSet3k),
  19028. sshKdfTvSet3h, sizeof(sshKdfTvSet3h),
  19029. sshKdfTvSet3sid, sizeof(sshKdfTvSet3sid),
  19030. sshKdfTvSet3d, sizeof(sshKdfTvSet3d)},
  19031. {WC_HASH_TYPE_SHA256, 'E',
  19032. sshKdfTvSet3k, sizeof(sshKdfTvSet3k),
  19033. sshKdfTvSet3h, sizeof(sshKdfTvSet3h),
  19034. sshKdfTvSet3sid, sizeof(sshKdfTvSet3sid),
  19035. sshKdfTvSet3e, sizeof(sshKdfTvSet3e)},
  19036. {WC_HASH_TYPE_SHA256, 'F',
  19037. sshKdfTvSet3k, sizeof(sshKdfTvSet3k),
  19038. sshKdfTvSet3h, sizeof(sshKdfTvSet3h),
  19039. sshKdfTvSet3sid, sizeof(sshKdfTvSet3sid),
  19040. sshKdfTvSet3f, sizeof(sshKdfTvSet3f)},
  19041. };
  19042. int sshkdf_test(void)
  19043. {
  19044. int result = 0;
  19045. word32 i;
  19046. word32 tc = sizeof(sshKdfTestVectors)/sizeof(SshKdfTestVector);
  19047. const SshKdfTestVector* tv = NULL;
  19048. byte cKey[32]; /* Greater of SHA256_DIGEST_SIZE and AES_BLOCK_SIZE */
  19049. /* sId - Session ID, eKey - Expected Key, cKey - Calculated Key */
  19050. for (i = 0, tv = sshKdfTestVectors; i < tc; i++, tv++) {
  19051. result = wc_SSH_KDF(tv->hashId, tv->keyId,
  19052. cKey, tv->expectedKeySz,
  19053. tv->k, tv->kSz, tv->h, tv->hSz,
  19054. tv->sessionId, tv->sessionIdSz);
  19055. if (result != 0) {
  19056. printf("KDF: Could not derive key.\n");
  19057. result = -101;
  19058. }
  19059. else {
  19060. if (XMEMCMP(cKey, tv->expectedKey, tv->expectedKeySz) != 0) {
  19061. printf("KDF: Calculated Key does not match Expected Key.\n");
  19062. result = -102;
  19063. }
  19064. }
  19065. if (result != 0) break;
  19066. }
  19067. return result;
  19068. }
  19069. #endif /* WOLFSSL_WOLFSSH */
  19070. #ifdef WOLFSSL_TLS13
  19071. #define TLSV13_PSK_DHE_SZ 40
  19072. typedef struct {
  19073. enum wc_HashType hashAlg;
  19074. word32 pskSz;
  19075. word32 dheSz;
  19076. byte psk[TLSV13_PSK_DHE_SZ];
  19077. byte dhe[TLSV13_PSK_DHE_SZ];
  19078. byte hashHello1[WC_MAX_DIGEST_SIZE];
  19079. byte hashHello2[WC_MAX_DIGEST_SIZE];
  19080. byte hashFinished1[WC_MAX_DIGEST_SIZE];
  19081. byte hashFinished2[WC_MAX_DIGEST_SIZE];
  19082. /* Expected */
  19083. byte clientEarlyTrafficSecret[WC_MAX_DIGEST_SIZE];
  19084. byte earlyExporterMasterSecret[WC_MAX_DIGEST_SIZE];
  19085. byte clientHandshakeTrafficSecret[WC_MAX_DIGEST_SIZE];
  19086. byte serverHandshakeTrafficSecret[WC_MAX_DIGEST_SIZE];
  19087. byte clientApplicationTrafficSecret[WC_MAX_DIGEST_SIZE];
  19088. byte serverApplicationTrafficSecret[WC_MAX_DIGEST_SIZE];
  19089. byte exporterMasterSecret[WC_MAX_DIGEST_SIZE];
  19090. byte resumptionMasterSecret[WC_MAX_DIGEST_SIZE];
  19091. } Tls13KdfTestVector;
  19092. /* The following tests come from the CAVP test vectors we used for
  19093. * our FIPS validation. The hash values used are the components from
  19094. * the test hashed together. hashHello1 is the hash of the
  19095. * clientHelloRandom value of the test vector. hashHello2 is the hash
  19096. * of the clientHelloRandom and serverHelloRandom values from the test
  19097. * vector. hashFinished1 is clientHelloRandom, serverHelloRandom, and
  19098. * serverFinishedRandom. hashFinished2 is clientHelloRandom,
  19099. * serverHelloRandom, serverFinishedRandom, and clietnFinishedRandom
  19100. * hashed together. */
  19101. static const Tls13KdfTestVector tls13KdfTestVectors[] = {
  19102. { /* 1 */
  19103. WC_HASH_TYPE_SHA256, 35, 35,
  19104. { /* PSK */
  19105. 0x7b, 0xf1, 0x05, 0x31, 0x36, 0xfa, 0x03, 0xdc,
  19106. 0x31, 0x97, 0x88, 0x04, 0x9c, 0xbc, 0xee, 0xf7,
  19107. 0x8d, 0x84, 0x95, 0x26, 0xaf, 0x1d, 0x68, 0xb0,
  19108. 0x60, 0x7a, 0xcc, 0x4f, 0xc1, 0xd3, 0xa1, 0x68,
  19109. 0x7f, 0x6d, 0xbe
  19110. },
  19111. { /* DHE */
  19112. 0x6e, 0xa1, 0x77, 0xab, 0x2f, 0x43, 0xd2, 0x4b,
  19113. 0xe5, 0xa1, 0x09, 0xe0, 0x7a, 0xd0, 0x01, 0x35,
  19114. 0x8d, 0xf8, 0xf2, 0x5c, 0x91, 0x02, 0xb0, 0x6c,
  19115. 0x3f, 0xeb, 0xee, 0xa4, 0x42, 0x19, 0xce, 0xdc,
  19116. 0x81, 0x26, 0x40
  19117. },
  19118. { /* Hello 1 */
  19119. 0xd9, 0x4b, 0xe4, 0x17, 0xef, 0x58, 0x73, 0x7d,
  19120. 0x28, 0x3d, 0xf0, 0xcc, 0x05, 0x03, 0xaf, 0xac,
  19121. 0x3d, 0x92, 0x79, 0x48, 0xe8, 0x8c, 0xdb, 0xce,
  19122. 0x95, 0x82, 0x21, 0x31, 0x7b, 0x61, 0xd7, 0xc6
  19123. },
  19124. { /* Hello 2 */
  19125. 0xb7, 0x7f, 0x29, 0x91, 0xa4, 0x8b, 0x34, 0xdb,
  19126. 0xbd, 0xc7, 0x54, 0x1c, 0x3b, 0x86, 0xa3, 0x69,
  19127. 0xfe, 0x26, 0xe4, 0x7b, 0xac, 0x57, 0x71, 0xb3,
  19128. 0x32, 0x97, 0xed, 0xd2, 0x0e, 0x95, 0xb8, 0x63
  19129. },
  19130. { /* Finished 1 */
  19131. 0x65, 0xdb, 0x6d, 0x71, 0x71, 0xd0, 0xd8, 0x49,
  19132. 0xd0, 0x3c, 0x8e, 0x2b, 0x24, 0xdf, 0xc2, 0xe9,
  19133. 0xd6, 0xfd, 0xea, 0x04, 0x95, 0x7c, 0xf0, 0x7e,
  19134. 0x57, 0x74, 0x7c, 0xdd, 0xa3, 0x0b, 0x2b, 0x36
  19135. },
  19136. { /* Finished 2 */
  19137. 0x28, 0xf2, 0xf2, 0x79, 0xcf, 0x20, 0x52, 0x90,
  19138. 0x1d, 0x91, 0x05, 0xad, 0x44, 0x26, 0x23, 0x96,
  19139. 0x32, 0xce, 0xec, 0x61, 0xd1, 0xbf, 0x00, 0x48,
  19140. 0x4a, 0xa5, 0x60, 0xcc, 0x28, 0xb5, 0x8d, 0x98
  19141. },
  19142. { /* Client Early Traffic Secret */
  19143. 0x07, 0x14, 0x6a, 0x26, 0x5b, 0x6c, 0x7f, 0x4d, 0x6b, 0x47, 0x3f, 0xd5,
  19144. 0x03, 0x1d, 0xd2, 0x23, 0x3d, 0x89, 0x3e, 0xc6, 0x51, 0xd1, 0xac, 0xf8,
  19145. 0x28, 0xae, 0x4b, 0x76, 0xc8, 0x10, 0x7e, 0xdd
  19146. },
  19147. { /* Early Exporter Master Secret */
  19148. 0xb8, 0xd3, 0x25, 0x7e, 0x2d, 0x41, 0x7b, 0xcb, 0x5e, 0x82, 0x49, 0xf5,
  19149. 0x51, 0x3d, 0xb7, 0x59, 0x32, 0xb3, 0xdf, 0x99, 0x4e, 0x04, 0x69, 0xc6,
  19150. 0x96, 0x8e, 0xe6, 0x3d, 0x91, 0xe4, 0x81, 0x11
  19151. },
  19152. { /* Client Handshake Traffic Secret */
  19153. 0xd9, 0x3b, 0x54, 0xe2, 0xb0, 0xd1, 0x85, 0xf0, 0xfd, 0xf3, 0x48, 0x4a,
  19154. 0xf8, 0x0b, 0xa5, 0xdc, 0x4c, 0x37, 0xcb, 0xd4, 0x20, 0xaf, 0x60, 0xc7,
  19155. 0xd5, 0x50, 0x5d, 0x0c, 0x77, 0x3b, 0x6f, 0xd2
  19156. },
  19157. { /* Server Handshake Traffic Secret */
  19158. 0x4d, 0x40, 0x2b, 0xd2, 0x8c, 0x33, 0x90, 0x39, 0x67, 0x67, 0x05, 0xf7,
  19159. 0x5d, 0x37, 0x1e, 0xdc, 0x4a, 0x70, 0x6b, 0x9e, 0xf8, 0x06, 0x61, 0x89,
  19160. 0x70, 0xe1, 0x3d, 0x36, 0xad, 0x88, 0x7e, 0x5b
  19161. },
  19162. { /* Client Application Traffic Secret */
  19163. 0x74, 0x6e, 0xa0, 0x13, 0x18, 0x34, 0x48, 0x4d, 0x23, 0x31, 0xf1, 0xf9,
  19164. 0xee, 0x44, 0x6d, 0xad, 0xc1, 0xad, 0x92, 0x73, 0xca, 0x27, 0x16, 0x91,
  19165. 0xa2, 0x50, 0x9a, 0xfc, 0xec, 0xf0, 0x6b, 0x24
  19166. },
  19167. { /* Server Application Traffic Secret */
  19168. 0x89, 0x18, 0x7e, 0x34, 0x8d, 0xfc, 0x14, 0xb1, 0x4f, 0x21, 0xd8, 0x29,
  19169. 0xdb, 0x9b, 0xfb, 0x55, 0xcf, 0xa1, 0x4f, 0x95, 0xf8, 0xe0, 0xb0, 0x83,
  19170. 0xd5, 0x34, 0x9e, 0x0b, 0x83, 0x37, 0x42, 0x93
  19171. },
  19172. { /* Exporter Master Secret */
  19173. 0x7d, 0xc8, 0x88, 0x46, 0xd5, 0x57, 0x15, 0xb6, 0x24, 0x25, 0x92, 0x61,
  19174. 0xb1, 0x18, 0x86, 0x2a, 0x6d, 0xa5, 0x84, 0xeb, 0x59, 0xdf, 0x13, 0xbd,
  19175. 0x73, 0xaa, 0x5d, 0x65, 0xab, 0xd9, 0xb4, 0x56
  19176. },
  19177. { /* Resumption Master Secret */
  19178. 0x20, 0xb7, 0xd0, 0xe3, 0x82, 0x01, 0xa1, 0x04, 0xb8, 0x13, 0x29, 0xed,
  19179. 0x35, 0xe4, 0x2f, 0xbf, 0x58, 0x23, 0x7f, 0x21, 0xdb, 0x9f, 0xf8, 0xe0,
  19180. 0xe8, 0xe4, 0xab, 0xc4, 0xa1, 0x61, 0xb9, 0xbb
  19181. }
  19182. },
  19183. { /* 6 */
  19184. WC_HASH_TYPE_SHA256, 0, 33,
  19185. { 0 }, /* PSK */
  19186. { /* DHE */
  19187. 0x7a, 0x46, 0x8c, 0x5a, 0xd1, 0x8e, 0x95, 0xba,
  19188. 0x61, 0xe6, 0x6f, 0xe6, 0x76, 0x0c, 0x20, 0x43,
  19189. 0x16, 0x82, 0x15, 0xfe, 0x54, 0xa3, 0xc7, 0xfd,
  19190. 0x3b, 0x2c, 0x88, 0xb4, 0xd3, 0x42, 0x70, 0x12,
  19191. 0x18
  19192. },
  19193. { /* Hello 1 */
  19194. 0x63, 0x83, 0x58, 0xab, 0x36, 0xcd, 0x0c, 0xf3,
  19195. 0x26, 0x07, 0xb5, 0x5f, 0x0b, 0x8b, 0x45, 0xd6,
  19196. 0x7d, 0x5b, 0x42, 0xdc, 0xa8, 0xaa, 0x06, 0xfb,
  19197. 0x20, 0xa5, 0xbb, 0x85, 0xdb, 0x54, 0xd8, 0x8b
  19198. },
  19199. { /* Hello 2 */
  19200. 0xea, 0xfe, 0x9e, 0x8e, 0xff, 0x1f, 0x6f, 0x43,
  19201. 0xf9, 0x5d, 0xfd, 0xbf, 0xe2, 0x5f, 0x02, 0x2f,
  19202. 0x6d, 0x47, 0x60, 0x9a, 0x48, 0x9a, 0x75, 0xfb,
  19203. 0xb5, 0x4a, 0xbf, 0x9c, 0x4e, 0xff, 0xbf, 0x0b
  19204. },
  19205. { /* Finished 1 */
  19206. 0xca, 0x25, 0xb3, 0x53, 0x8e, 0x6d, 0xc3, 0x36,
  19207. 0x17, 0x30, 0x07, 0xdf, 0x0d, 0xd7, 0x79, 0xb0,
  19208. 0x7f, 0xcb, 0xbe, 0x7a, 0xbc, 0x2d, 0x9f, 0x2d,
  19209. 0x94, 0x44, 0x94, 0xe6, 0xa4, 0xf3, 0xe8, 0x53
  19210. },
  19211. { /* Finished 2 */
  19212. 0x2e, 0xa6, 0x5a, 0xaf, 0xb5, 0xba, 0x9f, 0x2f,
  19213. 0x74, 0x83, 0x5d, 0xbf, 0x86, 0xa4, 0xa6, 0xf6,
  19214. 0xb9, 0x89, 0xdf, 0x17, 0xe1, 0xa8, 0x14, 0xc0,
  19215. 0xe1, 0x50, 0xfa, 0xec, 0xfa, 0xae, 0x8b, 0x7b
  19216. },
  19217. {
  19218. 0x20, 0x18, 0x72, 0x7c, 0xde, 0x3a, 0x85, 0x17, 0x72, 0xdc, 0xd7, 0x72,
  19219. 0xb0, 0xfc, 0x45, 0xd0, 0x62, 0xb9, 0xbb, 0x38, 0x69, 0x05, 0x7b, 0xb4,
  19220. 0x5e, 0x58, 0x5d, 0xed, 0xcd, 0x0b, 0x96, 0xd3
  19221. },
  19222. {
  19223. 0x68, 0x10, 0x20, 0xd1, 0x5e, 0xfc, 0x0c, 0x53, 0x85, 0xbb, 0xdb, 0x18,
  19224. 0xa8, 0x78, 0xf1, 0x2b, 0x13, 0xba, 0x64, 0x1d, 0xe7, 0x09, 0xbe, 0x13,
  19225. 0x49, 0x26, 0xf9, 0x98, 0x56, 0xf1, 0x43, 0xfb
  19226. },
  19227. {
  19228. 0x24, 0x35, 0x3e, 0x10, 0x6f, 0x39, 0x50, 0xd6, 0xa2, 0x12, 0x99, 0xf2,
  19229. 0xd5, 0xf5, 0x19, 0xf5, 0x84, 0xed, 0xee, 0x78, 0x2a, 0xa6, 0xfa, 0x3d,
  19230. 0x06, 0xa8, 0xa7, 0x5d, 0x97, 0x78, 0xd6, 0x58
  19231. },
  19232. {
  19233. 0xf4, 0x57, 0xac, 0x24, 0x7a, 0xfb, 0x7c, 0x3b, 0xb6, 0x39, 0x17, 0x14,
  19234. 0xd9, 0xd4, 0x58, 0x4d, 0x46, 0xd5, 0x1b, 0xde, 0xf7, 0x9d, 0x06, 0xee,
  19235. 0x8d, 0x1a, 0x2c, 0x25, 0x6d, 0x64, 0xde, 0x89
  19236. },
  19237. {
  19238. 0xb6, 0x00, 0xce, 0x63, 0xed, 0x65, 0x8b, 0x66, 0x66, 0x42, 0xc6, 0xbd,
  19239. 0x89, 0xc4, 0x71, 0x6f, 0xce, 0x28, 0xb2, 0xac, 0x97, 0x07, 0x5b, 0xea,
  19240. 0xb8, 0x1d, 0x4c, 0xeb, 0x9e, 0x71, 0x07, 0x8f
  19241. },
  19242. {
  19243. 0xf8, 0x92, 0xc8, 0xba, 0xe7, 0x83, 0xfe, 0x68, 0xe4, 0xd6, 0x5e, 0xcb,
  19244. 0xb3, 0xef, 0x49, 0xd0, 0xe7, 0xb1, 0xac, 0xcb, 0x39, 0x19, 0xfd, 0xa7,
  19245. 0xf7, 0xca, 0xab, 0x1e, 0x42, 0x14, 0xd8, 0xe7
  19246. },
  19247. {
  19248. 0x32, 0x4a, 0x1a, 0xad, 0xe2, 0xbb, 0x55, 0x8a, 0xdd, 0xe9, 0xa5, 0x2a,
  19249. 0x46, 0x5e, 0x6c, 0x83, 0x66, 0x27, 0x27, 0x94, 0xdd, 0x68, 0x59, 0xa0,
  19250. 0xbb, 0xe8, 0x31, 0x7c, 0x39, 0xd7, 0xfd, 0x6d
  19251. },
  19252. {
  19253. 0x58, 0xbc, 0x6c, 0x5b, 0x24, 0xad, 0x82, 0xb3, 0xcc, 0xc7, 0xd1, 0xa1,
  19254. 0xaa, 0x2b, 0x98, 0x9f, 0x2f, 0x7e, 0xa9, 0x63, 0xc2, 0x8e, 0xb6, 0x06,
  19255. 0xc2, 0x2b, 0x74, 0x4b, 0x79, 0x19, 0x7e, 0x2e
  19256. }
  19257. },
  19258. { /* 11 */
  19259. WC_HASH_TYPE_SHA256, 33, 0,
  19260. { /* PSK */
  19261. 0x3d, 0x39, 0x49, 0x36, 0x98, 0xc5, 0xfd, 0xcd,
  19262. 0xa0, 0x17, 0xbd, 0x65, 0x0a, 0xdb, 0xd4, 0x07,
  19263. 0x56, 0xa2, 0x7b, 0xb8, 0x2a, 0x7e, 0xfb, 0x26,
  19264. 0x74, 0xe1, 0xbc, 0x08, 0x4b, 0xf0, 0x30, 0x14,
  19265. 0x12
  19266. },
  19267. { 0 }, /* DHE */
  19268. { /* Hello 1 */
  19269. 0xb7, 0x44, 0x74, 0x6c, 0x57, 0x1f, 0xf3, 0x84,
  19270. 0x8f, 0x63, 0xfb, 0x8c, 0x94, 0x6c, 0x16, 0x68,
  19271. 0x4b, 0xe1, 0xb5, 0xb5, 0x2a, 0x4e, 0x5f, 0xdf,
  19272. 0x4b, 0x53, 0xb2, 0x35, 0xfc, 0x30, 0xf1, 0x36
  19273. },
  19274. { /* Hello 2 */
  19275. 0xe6, 0x4f, 0x3a, 0x4f, 0xd7, 0xe0, 0x64, 0xd4,
  19276. 0x69, 0x50, 0xe4, 0x8b, 0xba, 0xbc, 0x47, 0x74,
  19277. 0xa7, 0x9b, 0x40, 0x91, 0x8f, 0xa8, 0x72, 0x22,
  19278. 0x97, 0xad, 0x43, 0xa7, 0x11, 0x86, 0xb5, 0x72
  19279. },
  19280. { /* Finished 1 */
  19281. 0x5f, 0xa6, 0x10, 0xe2, 0xa3, 0x99, 0x0b, 0x5e,
  19282. 0x57, 0xee, 0xc3, 0x3a, 0x8e, 0x04, 0xf3, 0x0e,
  19283. 0x58, 0x02, 0x09, 0xb2, 0x7e, 0x2d, 0xc6, 0xd2,
  19284. 0x08, 0xae, 0x68, 0x0a, 0x55, 0xa5, 0xda, 0x51
  19285. },
  19286. { /* Finished 2 */
  19287. 0xfc, 0x5b, 0xc0, 0x7e, 0x1b, 0xaa, 0xc0, 0xb4,
  19288. 0x34, 0x85, 0x49, 0x8e, 0x16, 0x31, 0x98, 0xdf,
  19289. 0x10, 0x54, 0x22, 0xda, 0x1e, 0x6b, 0x51, 0xf6,
  19290. 0x97, 0x57, 0xa0, 0x7a, 0x92, 0xe7, 0x47, 0x52
  19291. },
  19292. {
  19293. 0x80, 0xfa, 0x36, 0x30, 0xb8, 0x65, 0xb3, 0x2a, 0x1d, 0x68, 0x91, 0x06,
  19294. 0x98, 0xa0, 0x17, 0x8f, 0xee, 0xb7, 0x9e, 0x3d, 0xd8, 0x84, 0x99, 0x30,
  19295. 0xb9, 0xd6, 0x09, 0x25, 0x5e, 0xfb, 0x8f, 0xd3 },
  19296. {
  19297. 0xa9, 0x89, 0x29, 0x70, 0xe4, 0x55, 0xec, 0x97, 0xfb, 0x24, 0x5b, 0xf9,
  19298. 0xf1, 0xa3, 0x19, 0x3d, 0xf1, 0x31, 0x14, 0xcd, 0x2a, 0xed, 0x21, 0xc8,
  19299. 0xb1, 0x53, 0xad, 0x11, 0x0b, 0x9e, 0x5a, 0xee },
  19300. {
  19301. 0x72, 0xad, 0x8d, 0x7f, 0xfc, 0xb7, 0x68, 0xda, 0x27, 0x60, 0x37, 0xa3,
  19302. 0x4a, 0x63, 0xe8, 0xa5, 0xc8, 0xcd, 0x36, 0x6a, 0x77, 0x99, 0x0d, 0xa9,
  19303. 0xb1, 0x5b, 0x2f, 0x47, 0x2e, 0x22, 0xa7, 0x5e },
  19304. {
  19305. 0x95, 0x6e, 0x85, 0x09, 0xe5, 0x04, 0x88, 0x14, 0x28, 0x8d, 0xdf, 0xe6,
  19306. 0x0d, 0x0f, 0x0d, 0x6b, 0x4e, 0x66, 0x1c, 0x03, 0xb9, 0xaa, 0x2d, 0x45,
  19307. 0x56, 0x67, 0x5c, 0x55, 0x29, 0xd6, 0x89, 0xd0 },
  19308. {
  19309. 0xe8, 0xf2, 0x14, 0xf9, 0x9b, 0x2b, 0x9f, 0x24, 0x2b, 0x37, 0xbe, 0x86,
  19310. 0xdb, 0x23, 0x4b, 0xbe, 0x39, 0x57, 0xe8, 0xa9, 0xa5, 0xee, 0x08, 0xf2,
  19311. 0x75, 0x58, 0xdb, 0xd9, 0x51, 0xc1, 0x46, 0x02 },
  19312. {
  19313. 0x3d, 0x19, 0xaf, 0xa3, 0x0b, 0x21, 0xf7, 0x3d, 0xe7, 0x37, 0x6e, 0x32,
  19314. 0x13, 0x48, 0x9d, 0xea, 0xe0, 0x90, 0xbf, 0x64, 0x48, 0xf7, 0x1e, 0xcc,
  19315. 0xf0, 0xbc, 0x92, 0xd7, 0x8a, 0x4a, 0xa8, 0xc1 },
  19316. {
  19317. 0x16, 0x35, 0xb1, 0x66, 0x28, 0xa3, 0x3e, 0x19, 0xf5, 0x2d, 0x92, 0x22,
  19318. 0x95, 0x48, 0xe8, 0x34, 0x7b, 0x30, 0x50, 0xa2, 0xa0, 0xd9, 0xc2, 0x59,
  19319. 0x39, 0xf9, 0x8c, 0x69, 0xf2, 0x2a, 0xb9, 0xff },
  19320. {
  19321. 0x32, 0x71, 0xa6, 0x87, 0x0c, 0x97, 0x42, 0x07, 0xdd, 0x5f, 0xc9, 0x44,
  19322. 0xa5, 0x7c, 0x50, 0x14, 0xfd, 0xe7, 0x5f, 0x8b, 0xd3, 0x2f, 0xdc, 0x9b,
  19323. 0xa9, 0x93, 0x22, 0x19, 0xe6, 0xf2, 0x0c, 0xd8 }
  19324. },
  19325. #ifdef WOLFSSL_SHA384
  19326. { /* 26 */
  19327. WC_HASH_TYPE_SHA384, 35, 35,
  19328. { /* PSK */
  19329. 0x62, 0x83, 0x25, 0xc7, 0xcc, 0x08, 0x5e, 0x63,
  19330. 0x64, 0x56, 0xf0, 0xc6, 0x88, 0x27, 0x5a, 0x5b,
  19331. 0x68, 0x59, 0x0b, 0x14, 0x55, 0x13, 0x2e, 0xfd,
  19332. 0x8f, 0x28, 0x5b, 0x3d, 0xe3, 0xad, 0x67, 0xe4,
  19333. 0x68, 0xba, 0xf9
  19334. },
  19335. { /* DHE */
  19336. 0xa8, 0xb1, 0xab, 0xd8, 0xc8, 0x5b, 0x52, 0xdf,
  19337. 0x7f, 0x49, 0x10, 0xf4, 0xa1, 0x31, 0xd1, 0x91,
  19338. 0x36, 0xc1, 0x87, 0x5d, 0x42, 0x2a, 0xe7, 0x1d,
  19339. 0x2c, 0x29, 0x3d, 0x40, 0x64, 0x61, 0x63, 0x76,
  19340. 0xd8, 0x66, 0xac
  19341. },
  19342. { /* Hello 1 */
  19343. 0x6f, 0xc6, 0x4c, 0xe1, 0xc6, 0x68, 0x34, 0x8c,
  19344. 0x0a, 0xe1, 0xf8, 0xb8, 0x3e, 0xd4, 0xf8, 0x0b,
  19345. 0x54, 0x50, 0xe4, 0xc5, 0x4a, 0x33, 0x7d, 0xbd,
  19346. 0x90, 0xd2, 0xa2, 0xb9, 0xb7, 0x92, 0xed, 0xab,
  19347. 0x14, 0xf1, 0xe4, 0x86, 0x22, 0x67, 0xd7, 0x44,
  19348. 0x03, 0x21, 0xdc, 0x51, 0x52, 0x7f, 0x35, 0x80
  19349. },
  19350. { /* Hello 2 */
  19351. 0x3e, 0xcf, 0x2f, 0xc3, 0x87, 0xba, 0xc5, 0xbd,
  19352. 0x7c, 0xe8, 0x35, 0x5b, 0x95, 0x51, 0x30, 0x3b,
  19353. 0x08, 0xcc, 0x2a, 0x7d, 0xb5, 0x74, 0x7c, 0x16,
  19354. 0xb3, 0x0b, 0xe7, 0x61, 0xa3, 0x7c, 0x6c, 0xbd,
  19355. 0x39, 0x74, 0xfd, 0x1e, 0x4c, 0xff, 0xc8, 0xcc,
  19356. 0xa0, 0xef, 0x29, 0x4d, 0x94, 0xaa, 0x55, 0x6f,
  19357. },
  19358. { /* Finished 1 */
  19359. 0x06, 0xc1, 0x47, 0x78, 0x66, 0x53, 0x6f, 0x24,
  19360. 0x94, 0x61, 0x69, 0xec, 0xd8, 0x60, 0x31, 0x2f,
  19361. 0xbf, 0xd6, 0x8a, 0x29, 0x17, 0xff, 0xa3, 0x88,
  19362. 0x13, 0x09, 0x8c, 0x9d, 0x6c, 0x64, 0x84, 0x48,
  19363. 0x44, 0xdd, 0x2d, 0x29, 0x4d, 0xe6, 0x98, 0x2b,
  19364. 0x45, 0x3b, 0x84, 0x33, 0x79, 0xb2, 0x75, 0x68
  19365. },
  19366. { /* Finished 2 */
  19367. 0x28, 0x1e, 0x18, 0xf7, 0x9c, 0x32, 0xa9, 0xbf,
  19368. 0x0c, 0x24, 0x58, 0x21, 0xce, 0xbc, 0xf2, 0x44,
  19369. 0xb1, 0x18, 0xaf, 0x9d, 0xd9, 0x20, 0xf9, 0xf4,
  19370. 0xed, 0xcc, 0x53, 0x82, 0x66, 0x5c, 0x46, 0x94,
  19371. 0x8c, 0x36, 0x5e, 0xca, 0x9f, 0xd8, 0x9a, 0xd3,
  19372. 0xf0, 0xe1, 0x53, 0x71, 0xdd, 0x19, 0x1e, 0x59
  19373. },
  19374. {
  19375. 0xd0, 0xef, 0xa8, 0xcb, 0x5b, 0x14, 0x0f, 0x0a, 0x62, 0xba, 0x5a, 0xb1,
  19376. 0xc5, 0xb5, 0x3f, 0x11, 0xda, 0xa1, 0x0c, 0x9c, 0xb4, 0x32, 0x48, 0x4e,
  19377. 0xfa, 0x84, 0x4f, 0xe4, 0xe7, 0x91, 0x8f, 0x42, 0x3f, 0xc7, 0x4e, 0xd3,
  19378. 0x83, 0x3d, 0x7f, 0x70, 0x12, 0xee, 0x9a, 0x37, 0x01, 0xbb, 0x14, 0xd3
  19379. },
  19380. {
  19381. 0x48, 0x6f, 0x77, 0x1d, 0x39, 0x1b, 0xa5, 0x9a, 0x76, 0xd9, 0x1d, 0x7d,
  19382. 0xb3, 0xd9, 0xb9, 0x78, 0x35, 0x0f, 0xd0, 0xe1, 0x07, 0x1f, 0x8d, 0xe5,
  19383. 0x75, 0x00, 0xda, 0xc0, 0x19, 0x01, 0xfb, 0x08, 0x35, 0xe7, 0x18, 0x8f,
  19384. 0xf0, 0x19, 0xfb, 0x46, 0xf6, 0xa5, 0x77, 0x0e, 0x90, 0x38, 0x8b, 0x15
  19385. },
  19386. {
  19387. 0x80, 0x8c, 0xa7, 0x24, 0x97, 0xf9, 0xd3, 0x52, 0xb0, 0x69, 0x9d, 0x4b,
  19388. 0xa4, 0x19, 0x4a, 0xb1, 0x46, 0x53, 0x3a, 0xc8, 0xe4, 0x02, 0x69, 0xf2,
  19389. 0xe7, 0xb6, 0x1d, 0x33, 0x51, 0xcc, 0x14, 0x40, 0x4a, 0xb0, 0xe7, 0x58,
  19390. 0x84, 0xba, 0xc2, 0x14, 0x58, 0x6b, 0xb9, 0xdc, 0x50, 0x98, 0x67, 0x01
  19391. },
  19392. {
  19393. 0xb1, 0xa8, 0xc0, 0x06, 0xb3, 0x2e, 0xa7, 0x8a, 0x6a, 0x12, 0x88, 0x00,
  19394. 0x65, 0x88, 0x9c, 0x5d, 0x35, 0xee, 0xe5, 0x51, 0x0b, 0x62, 0xf8, 0x67,
  19395. 0xe5, 0xef, 0x15, 0x1f, 0x23, 0x02, 0x74, 0x08, 0x9c, 0xc8, 0xba, 0x27,
  19396. 0x5d, 0x32, 0x19, 0x6f, 0x6d, 0x5d, 0x72, 0x5e, 0x15, 0xde, 0x30, 0xc3
  19397. },
  19398. {
  19399. 0xfd, 0xce, 0xf5, 0x65, 0x45, 0x84, 0xfb, 0x8c, 0x79, 0xa4, 0x6c, 0x1b,
  19400. 0x0e, 0x1b, 0xfd, 0x26, 0xa2, 0x53, 0xf4, 0x4e, 0x00, 0x4d, 0x4b, 0x0b,
  19401. 0x24, 0x6d, 0x35, 0x35, 0xd9, 0x97, 0x70, 0xc5, 0xf4, 0xee, 0xe3, 0xba,
  19402. 0x31, 0x1e, 0x2a, 0x42, 0xcb, 0xdf, 0x40, 0xb1, 0x14, 0xb8, 0x53, 0xce
  19403. },
  19404. {
  19405. 0xbb, 0xb3, 0x26, 0x7c, 0x22, 0x21, 0x9b, 0x72, 0x32, 0xa1, 0x97, 0xfb,
  19406. 0x78, 0x8c, 0xbe, 0x3d, 0x71, 0x45, 0xb8, 0xf5, 0x24, 0x8f, 0x0f, 0xac,
  19407. 0x42, 0x5b, 0x81, 0xe8, 0xd0, 0x71, 0x4a, 0xcb, 0x32, 0x3f, 0x03, 0xfb,
  19408. 0xec, 0x6a, 0x1f, 0x76, 0x80, 0x65, 0x01, 0x7a, 0x3d, 0xce, 0xc4, 0xdf
  19409. },
  19410. {
  19411. 0x3f, 0xcf, 0x2f, 0x63, 0x94, 0x94, 0x99, 0xfd, 0x04, 0x3a, 0x89, 0x83,
  19412. 0xcf, 0x06, 0x05, 0xec, 0x20, 0x3e, 0x5f, 0x51, 0x9d, 0x6e, 0x4a, 0xc6,
  19413. 0xf1, 0x2b, 0x37, 0x17, 0x34, 0x72, 0x6e, 0x1d, 0x2a, 0xfd, 0xc7, 0x73,
  19414. 0xb5, 0x07, 0x22, 0x81, 0x32, 0x2e, 0x21, 0x85, 0xaf, 0x10, 0xb2, 0x73
  19415. },
  19416. {
  19417. 0x52, 0x0c, 0x3d, 0x2e, 0x2d, 0x4a, 0x11, 0xae, 0x96, 0x78, 0xe9, 0x5b,
  19418. 0xd8, 0x0f, 0x6c, 0xf4, 0xbd, 0x96, 0x13, 0x55, 0x88, 0xdd, 0xa3, 0x67,
  19419. 0x36, 0x86, 0x1e, 0x0b, 0x36, 0x41, 0xec, 0xf6, 0x04, 0xb2, 0xc4, 0x16,
  19420. 0xbc, 0x2c, 0xdb, 0x30, 0x02, 0x94, 0xd4, 0x42, 0xbf, 0x38, 0xee, 0x9d
  19421. }
  19422. },
  19423. { /* 36 */
  19424. WC_HASH_TYPE_SHA384, 0, 33,
  19425. { 0 }, /* PSK */
  19426. { /* DHE */
  19427. 0xd3, 0x00, 0x72, 0x9a, 0xa8, 0xc5, 0xf3, 0xc4,
  19428. 0xf1, 0xa0, 0x26, 0x89, 0x65, 0x70, 0xc7, 0x0b,
  19429. 0x77, 0xbb, 0xe1, 0x4b, 0x2b, 0xa8, 0x4f, 0xa6,
  19430. 0x09, 0x4b, 0xba, 0x45, 0x36, 0x15, 0xee, 0x68,
  19431. 0xfd
  19432. },
  19433. { /* Hello 1 */
  19434. 0x10, 0x9d, 0x8b, 0xa2, 0x93, 0xe7, 0xd3, 0xb9,
  19435. 0xb4, 0x0f, 0xeb, 0x6a, 0xb9, 0x69, 0xcb, 0x39,
  19436. 0x16, 0x29, 0xcc, 0xd3, 0xcc, 0x1a, 0x4c, 0x1b,
  19437. 0x53, 0x7c, 0x33, 0x88, 0x06, 0xbc, 0x0a, 0x02,
  19438. 0xa0, 0xbe, 0x62, 0xc0, 0xe6, 0x5e, 0x97, 0x5b,
  19439. 0x6a, 0xa1, 0x98, 0xf3, 0xd2, 0x1e, 0xcd, 0xc5
  19440. },
  19441. { /* Hello 2 */
  19442. 0x74, 0xc0, 0x07, 0x2c, 0xc1, 0x63, 0xcc, 0x11,
  19443. 0xad, 0x1a, 0x55, 0x63, 0xbc, 0x20, 0x77, 0x96,
  19444. 0x30, 0x1c, 0x68, 0x45, 0x1e, 0x9b, 0xa7, 0xb4,
  19445. 0xf3, 0x04, 0x45, 0x16, 0x76, 0x55, 0xf9, 0xdf,
  19446. 0x4b, 0x2f, 0x1a, 0xdf, 0x5a, 0xb0, 0x93, 0xc9,
  19447. 0xab, 0xf5, 0x32, 0x47, 0x79, 0x9c, 0x01, 0xeb
  19448. },
  19449. { /* Finished 1 */
  19450. 0x27, 0x08, 0x8e, 0xa5, 0xf1, 0x30, 0xe1, 0xd6,
  19451. 0x4f, 0xa2, 0x9e, 0x3b, 0x03, 0x2d, 0x2e, 0xa3,
  19452. 0x84, 0x75, 0x51, 0x3a, 0xc3, 0xf6, 0xee, 0x2e,
  19453. 0x37, 0x0c, 0xe3, 0x28, 0x46, 0xa5, 0x2d, 0xc7,
  19454. 0xf0, 0x64, 0x78, 0x53, 0x66, 0x43, 0x02, 0xa4,
  19455. 0x7a, 0x43, 0x66, 0x4b, 0xa7, 0xcb, 0x97, 0x16
  19456. },
  19457. { /* Finished 2 */
  19458. 0x1d, 0x0d, 0xf8, 0xe1, 0x81, 0xa5, 0xbd, 0xa8,
  19459. 0x6f, 0x9d, 0x01, 0xa4, 0x9a, 0x92, 0xe2, 0xef,
  19460. 0x08, 0xab, 0xef, 0x3e, 0x2d, 0xd4, 0x82, 0xac,
  19461. 0x68, 0x9d, 0xe0, 0x54, 0x17, 0xde, 0x1a, 0xed,
  19462. 0x57, 0xcb, 0xd9, 0x2d, 0xc8, 0xbc, 0x93, 0xe6,
  19463. 0xa3, 0xec, 0xde, 0xee, 0xa1, 0x1c, 0x41, 0x85
  19464. },
  19465. {
  19466. 0x7f, 0x1f, 0xe6, 0x7b, 0xd8, 0xf5, 0x2b, 0x37, 0xbe, 0xb7, 0xd0, 0x37,
  19467. 0xce, 0x46, 0xad, 0x04, 0x2f, 0xc7, 0xdb, 0xc9, 0x9a, 0xb6, 0x00, 0x3f,
  19468. 0xc1, 0x97, 0xe9, 0x5c, 0x5e, 0x14, 0xd1, 0x38, 0x4d, 0x55, 0xe1, 0x07,
  19469. 0xb5, 0x85, 0x6d, 0xfa, 0xa7, 0x66, 0xad, 0xfa, 0xb6, 0xad, 0x29, 0x44
  19470. },
  19471. {
  19472. 0x4e, 0x6b, 0x20, 0x99, 0x55, 0x1b, 0x21, 0x89, 0xb6, 0x70, 0xdb, 0xe8,
  19473. 0xa7, 0x16, 0x55, 0xf2, 0x93, 0x13, 0x90, 0x7d, 0xfa, 0x62, 0x65, 0x53,
  19474. 0xa0, 0x97, 0xe9, 0xb4, 0xc0, 0xf1, 0xc9, 0x1a, 0x67, 0xdd, 0xca, 0x57,
  19475. 0xbc, 0xca, 0x39, 0xe6, 0x39, 0x6b, 0x63, 0x47, 0x25, 0x08, 0x3a, 0xd7
  19476. },
  19477. {
  19478. 0x35, 0x0d, 0xac, 0xd8, 0x10, 0x6a, 0x46, 0x50, 0x66, 0xae, 0x02, 0xc9,
  19479. 0xde, 0x13, 0x48, 0xce, 0x53, 0xd4, 0x92, 0x62, 0xc5, 0x65, 0x10, 0x08,
  19480. 0xc2, 0xc2, 0x82, 0xed, 0x9d, 0xc9, 0x6f, 0xa8, 0xc3, 0xc1, 0x0b, 0x7c,
  19481. 0xe1, 0x97, 0x85, 0xd6, 0x46, 0x29, 0x0e, 0x42, 0x51, 0xc1, 0x35, 0xcf
  19482. },
  19483. {
  19484. 0x3d, 0x5d, 0x84, 0xbd, 0x16, 0x46, 0x34, 0xb3, 0xf6, 0x31, 0x49, 0x3e,
  19485. 0x8d, 0xdc, 0xcb, 0x8c, 0x6a, 0x42, 0xf4, 0x88, 0xfc, 0x19, 0xfa, 0xa2,
  19486. 0x25, 0xc7, 0xa0, 0xa4, 0xca, 0xf0, 0xea, 0x2d, 0xe8, 0xc4, 0x02, 0x14,
  19487. 0x63, 0xfb, 0xd3, 0x7b, 0x51, 0x1c, 0xce, 0xca, 0xa3, 0xc3, 0xe4, 0xa5
  19488. },
  19489. {
  19490. 0x7c, 0x3a, 0x55, 0x92, 0x2e, 0xdd, 0x75, 0xdd, 0x76, 0x54, 0x4a, 0x9f,
  19491. 0xd0, 0xa2, 0x88, 0x83, 0xe9, 0x27, 0xda, 0x30, 0xe9, 0x96, 0x58, 0xc5,
  19492. 0xb7, 0x56, 0xfc, 0x4b, 0xb8, 0x5d, 0xee, 0x46, 0x70, 0x4e, 0x1b, 0x06,
  19493. 0x86, 0xaf, 0x48, 0x5c, 0x17, 0x35, 0xfa, 0x69, 0xc2, 0x4d, 0xfb, 0x09
  19494. },
  19495. {
  19496. 0x00, 0x0e, 0x28, 0x51, 0xc1, 0x7f, 0x41, 0x89, 0x6f, 0x9a, 0xca, 0x15,
  19497. 0xee, 0xed, 0x43, 0xca, 0x6d, 0x65, 0x6f, 0x51, 0x18, 0x6c, 0x08, 0x4b,
  19498. 0x77, 0xca, 0x75, 0xc4, 0xc3, 0xde, 0x29, 0x41, 0x8b, 0xaf, 0xa7, 0x1c,
  19499. 0x28, 0x37, 0xa0, 0xa0, 0x74, 0x8e, 0x09, 0x42, 0x7a, 0x1b, 0x68, 0xdb
  19500. },
  19501. {
  19502. 0x14, 0x8f, 0xab, 0x28, 0x64, 0xea, 0x45, 0x88, 0xdb, 0xc1, 0xc6, 0xa0,
  19503. 0x48, 0xdf, 0x15, 0xd0, 0x28, 0x07, 0x2d, 0x6c, 0xb8, 0x42, 0xbb, 0x60,
  19504. 0x02, 0x08, 0x9e, 0x29, 0x9b, 0x8d, 0xd6, 0x1c, 0xaf, 0xf2, 0x1a, 0xdc,
  19505. 0xf0, 0x78, 0x0b, 0x4d, 0x90, 0xa1, 0x0c, 0xb3, 0x13, 0xde, 0xca, 0x5a
  19506. },
  19507. {
  19508. 0x4d, 0x80, 0x7d, 0x0b, 0xb9, 0x00, 0x6f, 0x65, 0x51, 0x65, 0x23, 0xde,
  19509. 0x72, 0xdc, 0x4f, 0x04, 0xa5, 0xa2, 0x90, 0x45, 0x51, 0x9e, 0xd0, 0x3a,
  19510. 0xe4, 0xd7, 0x78, 0xa3, 0x0f, 0x2d, 0x65, 0x12, 0xad, 0xc8, 0x92, 0x30,
  19511. 0x79, 0x9d, 0x9d, 0x08, 0x7a, 0x9c, 0x9f, 0x83, 0xb1, 0xca, 0x59, 0x56
  19512. }
  19513. },
  19514. { /* 41 */
  19515. WC_HASH_TYPE_SHA384, 33, 0,
  19516. { /* PSK */
  19517. 0xa4, 0x8b, 0x1b, 0x5f, 0xd0, 0xea, 0x75, 0x62,
  19518. 0x06, 0x4d, 0x68, 0x40, 0x85, 0x20, 0x45, 0x95,
  19519. 0x4a, 0x00, 0xca, 0x05, 0xeb, 0xd4, 0x1d, 0x48,
  19520. 0x81, 0x89, 0xe8, 0x86, 0x43, 0xfa, 0x28, 0x17,
  19521. 0x12
  19522. },
  19523. { 0 }, /* DHE */
  19524. { /* Hello 1 */
  19525. 0x03, 0x7c, 0x33, 0x75, 0xdc, 0xc5, 0x46, 0x3a,
  19526. 0x0d, 0x56, 0xc6, 0xfb, 0xab, 0x1e, 0x1d, 0xda,
  19527. 0x59, 0xc2, 0xb2, 0xb1, 0x7c, 0x48, 0x9b, 0x06,
  19528. 0x0a, 0x5a, 0xbb, 0xf8, 0x98, 0x53, 0x78, 0x2d,
  19529. 0xd2, 0xcc, 0x87, 0x68, 0x25, 0xdd, 0x88, 0x22,
  19530. 0xcd, 0xb7, 0x74, 0x55, 0x21, 0xf9, 0x34, 0x98
  19531. },
  19532. { /* Hello 2 */
  19533. 0x03, 0xb4, 0xfb, 0xcc, 0x28, 0x2c, 0xc1, 0x70,
  19534. 0x42, 0x73, 0x57, 0xac, 0xdb, 0x47, 0x71, 0xf6,
  19535. 0x2e, 0x11, 0x8a, 0x5b, 0x47, 0x2f, 0x02, 0x54,
  19536. 0x95, 0x34, 0xed, 0x5f, 0x19, 0xc1, 0x75, 0xe0,
  19537. 0x76, 0xad, 0xb0, 0x90, 0x57, 0xcd, 0xfd, 0xd7,
  19538. 0x58, 0x1f, 0x0d, 0x6b, 0x9e, 0x51, 0x3c, 0x08
  19539. },
  19540. { /* Finished 1 */
  19541. 0x2b, 0x50, 0xd9, 0xa7, 0x43, 0x24, 0xda, 0x2c,
  19542. 0x7a, 0xaa, 0x0e, 0x37, 0xd7, 0x6b, 0x2c, 0xab,
  19543. 0x8e, 0xb2, 0xfe, 0x31, 0x1b, 0xa8, 0x12, 0x59,
  19544. 0x5b, 0x7b, 0xdc, 0x3e, 0xa7, 0x86, 0xa5, 0x48,
  19545. 0xe4, 0x46, 0x2b, 0x4c, 0xc1, 0x66, 0x4b, 0xf3,
  19546. 0x2a, 0x99, 0x93, 0x08, 0xbc, 0x3d, 0x08, 0x76
  19547. },
  19548. { /* Finished 2 */
  19549. 0x7c, 0x34, 0xc8, 0x56, 0x17, 0xf1, 0x62, 0x1c,
  19550. 0x9f, 0x0b, 0xeb, 0xfd, 0x69, 0x72, 0x51, 0xc5,
  19551. 0xfa, 0x74, 0x87, 0xc9, 0xbd, 0x50, 0xe9, 0x48,
  19552. 0xa7, 0x3c, 0x94, 0x3e, 0x06, 0x7d, 0xe8, 0x8e,
  19553. 0xc1, 0xd1, 0x08, 0x1f, 0x5d, 0x48, 0x8a, 0x25,
  19554. 0xfc, 0xea, 0xe7, 0xd9, 0xd4, 0xd0, 0xf9, 0xad
  19555. },
  19556. {
  19557. 0x4b, 0x0b, 0xed, 0xb9, 0xc8, 0xb8, 0xa8, 0x1e, 0xb0, 0x81, 0x76, 0xd5,
  19558. 0x33, 0x22, 0x71, 0x33, 0x3a, 0x85, 0x19, 0x67, 0x7e, 0x91, 0x37, 0xf2,
  19559. 0xa6, 0x11, 0x22, 0xdf, 0x41, 0x04, 0x3d, 0xa9, 0x13, 0xb9, 0xb2, 0xb1,
  19560. 0xbb, 0xd8, 0xef, 0x23, 0x7c, 0xc2, 0xab, 0x70, 0x1b, 0x51, 0x9f, 0xc9
  19561. },
  19562. {
  19563. 0xeb, 0x96, 0x10, 0x8c, 0x7d, 0x92, 0xea, 0x80, 0x86, 0xb2, 0xf8, 0x27,
  19564. 0xf2, 0x9a, 0x09, 0xc1, 0x7c, 0x09, 0x43, 0xbc, 0xfe, 0xc8, 0x75, 0xe0,
  19565. 0x97, 0xe7, 0x6d, 0xd5, 0xb2, 0x3c, 0xed, 0x12, 0xb7, 0x74, 0x0e, 0xe3,
  19566. 0xb6, 0xe0, 0xba, 0xe1, 0x8d, 0x89, 0xcf, 0x4f, 0x57, 0xf6, 0x6d, 0x90
  19567. },
  19568. {
  19569. 0x22, 0xb0, 0x39, 0x34, 0xb6, 0x6c, 0x2d, 0x7a, 0x97, 0x1c, 0x5d, 0xcc,
  19570. 0x78, 0x84, 0x71, 0xbb, 0xc6, 0x7b, 0xb6, 0xbc, 0xcc, 0x0b, 0xf8, 0xac,
  19571. 0x8e, 0xd7, 0x20, 0xbd, 0xbe, 0x32, 0xf0, 0xd6, 0xe9, 0x69, 0x13, 0xf2,
  19572. 0x9a, 0xce, 0xfe, 0x86, 0xd3, 0xee, 0xba, 0x69, 0x51, 0xb6, 0x77, 0x56
  19573. },
  19574. {
  19575. 0x16, 0xfd, 0xda, 0xf3, 0x5e, 0xb9, 0xa6, 0x17, 0x24, 0xb2, 0x16, 0x9f,
  19576. 0xb6, 0x59, 0x13, 0x0f, 0x25, 0x5a, 0xf1, 0x5b, 0x5f, 0xe4, 0x54, 0x2a,
  19577. 0xa7, 0xbf, 0x29, 0xaf, 0x5a, 0x77, 0xf4, 0x4f, 0x25, 0xba, 0x94, 0xad,
  19578. 0x6b, 0x91, 0x3b, 0xe7, 0xd5, 0x73, 0x0d, 0xff, 0xaa, 0xe3, 0x72, 0x2c
  19579. },
  19580. {
  19581. 0x22, 0xb4, 0x94, 0xc0, 0x53, 0xd7, 0x82, 0x06, 0x38, 0x9d, 0x4a, 0xa0,
  19582. 0x3f, 0xf1, 0x5f, 0x6e, 0x23, 0x8d, 0x09, 0x62, 0xbf, 0x6f, 0x7c, 0x84,
  19583. 0xc6, 0x3e, 0x15, 0xad, 0x18, 0x37, 0x76, 0x29, 0xc7, 0xd6, 0x68, 0x0c,
  19584. 0x1e, 0xc6, 0x93, 0x31, 0xef, 0x85, 0x69, 0x30, 0x68, 0xf0, 0x1e, 0x37
  19585. },
  19586. {
  19587. 0x6d, 0x4d, 0x20, 0xaf, 0x47, 0xe8, 0x1b, 0xfa, 0xd0, 0xb6, 0xc8, 0x97,
  19588. 0xd1, 0x03, 0xfc, 0x9d, 0x59, 0xa0, 0x68, 0x9d, 0xe9, 0x17, 0x8b, 0xce,
  19589. 0x48, 0x2c, 0x77, 0x8a, 0x22, 0x4b, 0x5c, 0x54, 0x22, 0xa1, 0x15, 0x12,
  19590. 0xe1, 0x07, 0x8e, 0x15, 0xd8, 0x7b, 0x16, 0x65, 0x99, 0x6b, 0xcb, 0x71
  19591. },
  19592. {
  19593. 0x79, 0x64, 0x79, 0xdd, 0x75, 0x5c, 0x6f, 0x98, 0xac, 0x03, 0xe0, 0xcd,
  19594. 0x92, 0xba, 0x0e, 0x2d, 0xb4, 0xd1, 0x8b, 0x97, 0xd0, 0x85, 0xbb, 0x2e,
  19595. 0x4f, 0x26, 0x93, 0xf5, 0x1d, 0xf3, 0xd2, 0x43, 0x4f, 0xd2, 0x47, 0xaa,
  19596. 0x91, 0x1e, 0xf3, 0x67, 0x10, 0x18, 0x2c, 0xb9, 0x01, 0xba, 0x10, 0x9f
  19597. },
  19598. {
  19599. 0x79, 0xb6, 0x9c, 0xbe, 0xf1, 0x6a, 0xb0, 0x92, 0xa0, 0x29, 0x52, 0x61,
  19600. 0xf1, 0xcd, 0x3a, 0x67, 0xe1, 0x6b, 0xb8, 0x9d, 0x0d, 0x95, 0xb6, 0x03,
  19601. 0x80, 0x1f, 0xd5, 0x75, 0xb6, 0x1d, 0x79, 0x02, 0x93, 0x43, 0x77, 0xa7,
  19602. 0x9d, 0x2f, 0xc3, 0x84, 0xc6, 0x83, 0x76, 0x16, 0x06, 0x98, 0x7b, 0x79
  19603. }
  19604. },
  19605. #endif /* WOLFSSL_SHA384 */
  19606. };
  19607. const char protocolLabel[] = "tls13 ";
  19608. const char ceTrafficLabel[] = "c e traffic";
  19609. const char eExpMasterLabel[] = "e exp master";
  19610. const char cHsTrafficLabel[] = "c hs traffic";
  19611. const char sHsTrafficLabel[] = "s hs traffic";
  19612. const char cAppTrafficLabel[] = "c ap traffic";
  19613. const char sAppTrafficLabel[] = "s ap traffic";
  19614. const char expMasterLabel[] = "exp master";
  19615. const char resMasterLabel[] = "res master";
  19616. const char derivedLabel[] = "derived";
  19617. WOLFSSL_TEST_SUBROUTINE int tls13_kdf_test(void)
  19618. {
  19619. int ret = 0;
  19620. word32 i;
  19621. word32 tc = sizeof(tls13KdfTestVectors)/sizeof(Tls13KdfTestVector);
  19622. const Tls13KdfTestVector* tv = NULL;
  19623. for (i = 0, tv = tls13KdfTestVectors; i < tc; i++, tv++) {
  19624. byte output[WC_MAX_DIGEST_SIZE];
  19625. byte secret[WC_MAX_DIGEST_SIZE];
  19626. byte salt[WC_MAX_DIGEST_SIZE];
  19627. byte zeroes[WC_MAX_DIGEST_SIZE];
  19628. byte hashZero[WC_MAX_DIGEST_SIZE];
  19629. int hashAlgSz;
  19630. XMEMSET(zeroes, 0, sizeof zeroes);
  19631. hashAlgSz = wc_HashGetDigestSize(tv->hashAlg);
  19632. if (hashAlgSz == BAD_FUNC_ARG) break;
  19633. ret = wc_Hash(tv->hashAlg, NULL, 0, hashZero, hashAlgSz);
  19634. if (ret != 0) break;
  19635. ret = wc_Tls13_HKDF_Extract(secret, NULL, 0,
  19636. (tv->pskSz == 0) ? zeroes : (byte*)tv->psk,
  19637. tv->pskSz, tv->hashAlg);
  19638. if (ret != 0) break;
  19639. ret = wc_Tls13_HKDF_Expand_Label(output, hashAlgSz,
  19640. secret, hashAlgSz,
  19641. (byte*)protocolLabel, (word32)XSTRLEN(protocolLabel),
  19642. (byte*)ceTrafficLabel, (word32)XSTRLEN(ceTrafficLabel),
  19643. tv->hashHello1, hashAlgSz, tv->hashAlg);
  19644. if (ret != 0) break;
  19645. ret = XMEMCMP(tv->clientEarlyTrafficSecret, output, hashAlgSz);
  19646. if (ret != 0) break;
  19647. ret = wc_Tls13_HKDF_Expand_Label(output, hashAlgSz,
  19648. secret, hashAlgSz,
  19649. (byte*)protocolLabel, (word32)XSTRLEN(protocolLabel),
  19650. (byte*)eExpMasterLabel, (word32)XSTRLEN(eExpMasterLabel),
  19651. tv->hashHello1, hashAlgSz, tv->hashAlg);
  19652. if (ret != 0) break;
  19653. ret = XMEMCMP(tv->earlyExporterMasterSecret, output, hashAlgSz);
  19654. if (ret != 0) break;
  19655. ret = wc_Tls13_HKDF_Expand_Label(salt, hashAlgSz,
  19656. secret, hashAlgSz,
  19657. (byte*)protocolLabel, (word32)XSTRLEN(protocolLabel),
  19658. (byte*)derivedLabel, (word32)XSTRLEN(derivedLabel),
  19659. hashZero, hashAlgSz, tv->hashAlg);
  19660. if (ret != 0) break;
  19661. ret = wc_Tls13_HKDF_Extract(secret, salt, hashAlgSz,
  19662. (tv->dheSz == 0) ? zeroes : (byte*)tv->dhe,
  19663. tv->dheSz, tv->hashAlg);
  19664. if (ret != 0) break;
  19665. ret = wc_Tls13_HKDF_Expand_Label(output, hashAlgSz,
  19666. secret, hashAlgSz,
  19667. (byte*)protocolLabel, (word32)XSTRLEN(protocolLabel),
  19668. (byte*)cHsTrafficLabel, (word32)XSTRLEN(cHsTrafficLabel),
  19669. tv->hashHello2, hashAlgSz, tv->hashAlg);
  19670. if (ret != 0) break;
  19671. ret = XMEMCMP(tv->clientHandshakeTrafficSecret,
  19672. output, hashAlgSz);
  19673. if (ret != 0) break;
  19674. ret = wc_Tls13_HKDF_Expand_Label(output, hashAlgSz,
  19675. secret, hashAlgSz,
  19676. (byte*)protocolLabel, (word32)XSTRLEN(protocolLabel),
  19677. (byte*)sHsTrafficLabel, (word32)XSTRLEN(sHsTrafficLabel),
  19678. tv->hashHello2, hashAlgSz, tv->hashAlg);
  19679. if (ret != 0) break;
  19680. ret = XMEMCMP(tv->serverHandshakeTrafficSecret, output, hashAlgSz);
  19681. if (ret != 0) break;
  19682. ret = wc_Tls13_HKDF_Expand_Label(salt, hashAlgSz,
  19683. secret, hashAlgSz,
  19684. (byte*)protocolLabel, (word32)XSTRLEN(protocolLabel),
  19685. (byte*)derivedLabel, (word32)XSTRLEN(derivedLabel),
  19686. hashZero, hashAlgSz, tv->hashAlg);
  19687. if (ret != 0) break;
  19688. ret = wc_Tls13_HKDF_Extract(secret, salt, hashAlgSz,
  19689. zeroes, hashAlgSz, tv->hashAlg);
  19690. if (ret != 0) break;
  19691. ret = wc_Tls13_HKDF_Expand_Label(output, hashAlgSz,
  19692. secret, hashAlgSz,
  19693. (byte*)protocolLabel, (word32)XSTRLEN(protocolLabel),
  19694. (byte*)cAppTrafficLabel, (word32)XSTRLEN(cAppTrafficLabel),
  19695. tv->hashFinished1, hashAlgSz, tv->hashAlg);
  19696. if (ret != 0) break;
  19697. ret = XMEMCMP(tv->clientApplicationTrafficSecret, output, hashAlgSz);
  19698. if (ret != 0) break;
  19699. ret = wc_Tls13_HKDF_Expand_Label(output, hashAlgSz,
  19700. secret, hashAlgSz,
  19701. (byte*)protocolLabel, (word32)XSTRLEN(protocolLabel),
  19702. (byte*)sAppTrafficLabel, (word32)XSTRLEN(sAppTrafficLabel),
  19703. tv->hashFinished1, hashAlgSz, tv->hashAlg);
  19704. if (ret != 0) break;
  19705. ret = XMEMCMP(tv->serverApplicationTrafficSecret, output, hashAlgSz);
  19706. if (ret != 0) break;
  19707. ret = wc_Tls13_HKDF_Expand_Label(output, hashAlgSz,
  19708. secret, hashAlgSz,
  19709. (byte*)protocolLabel, (word32)XSTRLEN(protocolLabel),
  19710. (byte*)expMasterLabel, (word32)XSTRLEN(expMasterLabel),
  19711. tv->hashFinished1, hashAlgSz, tv->hashAlg);
  19712. if (ret != 0) break;
  19713. ret = XMEMCMP(tv->exporterMasterSecret, output, hashAlgSz);
  19714. if (ret != 0) break;
  19715. ret = wc_Tls13_HKDF_Expand_Label(output, hashAlgSz,
  19716. secret, hashAlgSz,
  19717. (byte*)protocolLabel, (word32)XSTRLEN(protocolLabel),
  19718. (byte*)resMasterLabel, (word32)XSTRLEN(resMasterLabel),
  19719. tv->hashFinished2, hashAlgSz, tv->hashAlg);
  19720. if (ret != 0) break;
  19721. ret = XMEMCMP(tv->resumptionMasterSecret, output, hashAlgSz);
  19722. if (ret != 0) break;
  19723. }
  19724. return ret;
  19725. }
  19726. #endif /* WOLFSSL_TLS13 */
  19727. #if defined(HAVE_ECC) && defined(HAVE_X963_KDF)
  19728. WOLFSSL_TEST_SUBROUTINE int x963kdf_test(void)
  19729. {
  19730. int ret;
  19731. byte kek[128];
  19732. #ifndef NO_SHA
  19733. /* SHA-1, COUNT = 0
  19734. * shared secret length: 192
  19735. * SharedInfo length: 0
  19736. * key data length: 128
  19737. */
  19738. WOLFSSL_SMALL_STACK_STATIC const byte Z[] = {
  19739. 0x1c, 0x7d, 0x7b, 0x5f, 0x05, 0x97, 0xb0, 0x3d,
  19740. 0x06, 0xa0, 0x18, 0x46, 0x6e, 0xd1, 0xa9, 0x3e,
  19741. 0x30, 0xed, 0x4b, 0x04, 0xdc, 0x64, 0xcc, 0xdd
  19742. };
  19743. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  19744. 0xbf, 0x71, 0xdf, 0xfd, 0x8f, 0x4d, 0x99, 0x22,
  19745. 0x39, 0x36, 0xbe, 0xb4, 0x6f, 0xee, 0x8c, 0xcc
  19746. };
  19747. #endif
  19748. #ifndef NO_SHA256
  19749. /* SHA-256, COUNT = 3
  19750. * shared secret length: 192
  19751. * SharedInfo length: 0
  19752. * key data length: 128
  19753. */
  19754. WOLFSSL_SMALL_STACK_STATIC const byte Z2[] = {
  19755. 0xd3, 0x8b, 0xdb, 0xe5, 0xc4, 0xfc, 0x16, 0x4c,
  19756. 0xdd, 0x96, 0x7f, 0x63, 0xc0, 0x4f, 0xe0, 0x7b,
  19757. 0x60, 0xcd, 0xe8, 0x81, 0xc2, 0x46, 0x43, 0x8c
  19758. };
  19759. WOLFSSL_SMALL_STACK_STATIC const byte verify2[] = {
  19760. 0x5e, 0x67, 0x4d, 0xb9, 0x71, 0xba, 0xc2, 0x0a,
  19761. 0x80, 0xba, 0xd0, 0xd4, 0x51, 0x4d, 0xc4, 0x84
  19762. };
  19763. #endif
  19764. #ifdef WOLFSSL_SHA512
  19765. /* SHA-512, COUNT = 0
  19766. * shared secret length: 192
  19767. * SharedInfo length: 0
  19768. * key data length: 128
  19769. */
  19770. WOLFSSL_SMALL_STACK_STATIC const byte Z3[] = {
  19771. 0x87, 0xfc, 0x0d, 0x8c, 0x44, 0x77, 0x48, 0x5b,
  19772. 0xb5, 0x74, 0xf5, 0xfc, 0xea, 0x26, 0x4b, 0x30,
  19773. 0x88, 0x5d, 0xc8, 0xd9, 0x0a, 0xd8, 0x27, 0x82
  19774. };
  19775. WOLFSSL_SMALL_STACK_STATIC const byte verify3[] = {
  19776. 0x94, 0x76, 0x65, 0xfb, 0xb9, 0x15, 0x21, 0x53,
  19777. 0xef, 0x46, 0x02, 0x38, 0x50, 0x6a, 0x02, 0x45
  19778. };
  19779. /* SHA-512, COUNT = 0
  19780. * shared secret length: 521
  19781. * SharedInfo length: 128
  19782. * key data length: 1024
  19783. */
  19784. WOLFSSL_SMALL_STACK_STATIC const byte Z4[] = {
  19785. 0x00, 0xaa, 0x5b, 0xb7, 0x9b, 0x33, 0xe3, 0x89,
  19786. 0xfa, 0x58, 0xce, 0xad, 0xc0, 0x47, 0x19, 0x7f,
  19787. 0x14, 0xe7, 0x37, 0x12, 0xf4, 0x52, 0xca, 0xa9,
  19788. 0xfc, 0x4c, 0x9a, 0xdb, 0x36, 0x93, 0x48, 0xb8,
  19789. 0x15, 0x07, 0x39, 0x2f, 0x1a, 0x86, 0xdd, 0xfd,
  19790. 0xb7, 0xc4, 0xff, 0x82, 0x31, 0xc4, 0xbd, 0x0f,
  19791. 0x44, 0xe4, 0x4a, 0x1b, 0x55, 0xb1, 0x40, 0x47,
  19792. 0x47, 0xa9, 0xe2, 0xe7, 0x53, 0xf5, 0x5e, 0xf0,
  19793. 0x5a, 0x2d
  19794. };
  19795. WOLFSSL_SMALL_STACK_STATIC const byte info4[] = {
  19796. 0xe3, 0xb5, 0xb4, 0xc1, 0xb0, 0xd5, 0xcf, 0x1d,
  19797. 0x2b, 0x3a, 0x2f, 0x99, 0x37, 0x89, 0x5d, 0x31
  19798. };
  19799. WOLFSSL_SMALL_STACK_STATIC const byte verify4[] = {
  19800. 0x44, 0x63, 0xf8, 0x69, 0xf3, 0xcc, 0x18, 0x76,
  19801. 0x9b, 0x52, 0x26, 0x4b, 0x01, 0x12, 0xb5, 0x85,
  19802. 0x8f, 0x7a, 0xd3, 0x2a, 0x5a, 0x2d, 0x96, 0xd8,
  19803. 0xcf, 0xfa, 0xbf, 0x7f, 0xa7, 0x33, 0x63, 0x3d,
  19804. 0x6e, 0x4d, 0xd2, 0xa5, 0x99, 0xac, 0xce, 0xb3,
  19805. 0xea, 0x54, 0xa6, 0x21, 0x7c, 0xe0, 0xb5, 0x0e,
  19806. 0xef, 0x4f, 0x6b, 0x40, 0xa5, 0xc3, 0x02, 0x50,
  19807. 0xa5, 0xa8, 0xee, 0xee, 0x20, 0x80, 0x02, 0x26,
  19808. 0x70, 0x89, 0xdb, 0xf3, 0x51, 0xf3, 0xf5, 0x02,
  19809. 0x2a, 0xa9, 0x63, 0x8b, 0xf1, 0xee, 0x41, 0x9d,
  19810. 0xea, 0x9c, 0x4f, 0xf7, 0x45, 0xa2, 0x5a, 0xc2,
  19811. 0x7b, 0xda, 0x33, 0xca, 0x08, 0xbd, 0x56, 0xdd,
  19812. 0x1a, 0x59, 0xb4, 0x10, 0x6c, 0xf2, 0xdb, 0xbc,
  19813. 0x0a, 0xb2, 0xaa, 0x8e, 0x2e, 0xfa, 0x7b, 0x17,
  19814. 0x90, 0x2d, 0x34, 0x27, 0x69, 0x51, 0xce, 0xcc,
  19815. 0xab, 0x87, 0xf9, 0x66, 0x1c, 0x3e, 0x88, 0x16
  19816. };
  19817. #endif
  19818. #ifndef NO_SHA
  19819. ret = wc_X963_KDF(WC_HASH_TYPE_SHA, Z, sizeof(Z), NULL, 0,
  19820. kek, sizeof(verify));
  19821. if (ret != 0)
  19822. return -9800;
  19823. if (XMEMCMP(verify, kek, sizeof(verify)) != 0)
  19824. return -9801;
  19825. #endif
  19826. #ifndef NO_SHA256
  19827. ret = wc_X963_KDF(WC_HASH_TYPE_SHA256, Z2, sizeof(Z2), NULL, 0,
  19828. kek, sizeof(verify2));
  19829. if (ret != 0)
  19830. return -9802;
  19831. if (XMEMCMP(verify2, kek, sizeof(verify2)) != 0)
  19832. return -9803;
  19833. #endif
  19834. #ifdef WOLFSSL_SHA512
  19835. ret = wc_X963_KDF(WC_HASH_TYPE_SHA512, Z3, sizeof(Z3), NULL, 0,
  19836. kek, sizeof(verify3));
  19837. if (ret != 0)
  19838. return -9804;
  19839. if (XMEMCMP(verify3, kek, sizeof(verify3)) != 0)
  19840. return -9805;
  19841. ret = wc_X963_KDF(WC_HASH_TYPE_SHA512, Z4, sizeof(Z4), info4,
  19842. sizeof(info4), kek, sizeof(verify4));
  19843. if (ret != 0)
  19844. return -9806;
  19845. if (XMEMCMP(verify4, kek, sizeof(verify4)) != 0)
  19846. return -9807;
  19847. #endif
  19848. return 0;
  19849. }
  19850. #endif /* HAVE_X963_KDF */
  19851. #if defined(HAVE_HPKE) && (defined(HAVE_ECC) || defined(HAVE_CURVE25519)) && \
  19852. defined(HAVE_AESGCM)
  19853. static int hpke_test_single(Hpke* hpke)
  19854. {
  19855. int ret = 0;
  19856. int rngRet = 0;
  19857. WC_RNG rng[1];
  19858. const char* start_text = "this is a test";
  19859. const char* info_text = "info";
  19860. const char* aad_text = "aad";
  19861. byte ciphertext[MAX_HPKE_LABEL_SZ];
  19862. byte plaintext[MAX_HPKE_LABEL_SZ];
  19863. void* receiverKey = NULL;
  19864. void* ephemeralKey = NULL;
  19865. uint8_t pubKey[HPKE_Npk_MAX]; /* public key */
  19866. word16 pubKeySz = (word16)sizeof(pubKey);
  19867. rngRet = ret = wc_InitRng(rng);
  19868. if (ret != 0)
  19869. return ret;
  19870. /* generate the keys */
  19871. if (ret == 0)
  19872. ret = wc_HpkeGenerateKeyPair(hpke, &ephemeralKey, rng);
  19873. if (ret == 0)
  19874. ret = wc_HpkeGenerateKeyPair(hpke, &receiverKey, rng);
  19875. /* seal */
  19876. if (ret == 0)
  19877. ret = wc_HpkeSealBase(hpke, ephemeralKey, receiverKey,
  19878. (byte*)info_text, (word32)XSTRLEN(info_text),
  19879. (byte*)aad_text, (word32)XSTRLEN(aad_text),
  19880. (byte*)start_text, (word32)XSTRLEN(start_text),
  19881. ciphertext);
  19882. /* export ephemeral key */
  19883. if (ret == 0)
  19884. ret = wc_HpkeSerializePublicKey(hpke, ephemeralKey, pubKey, &pubKeySz);
  19885. /* open with exported ephemeral key */
  19886. if (ret == 0)
  19887. ret = wc_HpkeOpenBase(hpke, receiverKey, pubKey, pubKeySz,
  19888. (byte*)info_text, (word32)XSTRLEN(info_text),
  19889. (byte*)aad_text, (word32)XSTRLEN(aad_text),
  19890. ciphertext, (word32)XSTRLEN(start_text),
  19891. plaintext);
  19892. if (ret == 0)
  19893. ret = XMEMCMP(plaintext, start_text, XSTRLEN(start_text));
  19894. if (ephemeralKey != NULL)
  19895. wc_HpkeFreeKey(hpke, hpke->kem, ephemeralKey, hpke->heap);
  19896. if (receiverKey != NULL)
  19897. wc_HpkeFreeKey(hpke, hpke->kem, receiverKey, hpke->heap);
  19898. if (rngRet == 0)
  19899. wc_FreeRng(rng);
  19900. return ret;
  19901. }
  19902. WOLFSSL_TEST_SUBROUTINE int hpke_test(void)
  19903. {
  19904. int ret = 0;
  19905. Hpke hpke[1];
  19906. #if defined(HAVE_ECC)
  19907. #if defined(WOLFSSL_SHA224) || !defined(NO_SHA256)
  19908. /* p256 */
  19909. ret = wc_HpkeInit(hpke, DHKEM_P256_HKDF_SHA256, HKDF_SHA256,
  19910. HPKE_AES_128_GCM, NULL);
  19911. if (ret != 0)
  19912. return ret;
  19913. ret = hpke_test_single(hpke);
  19914. if (ret != 0)
  19915. return ret;
  19916. #endif
  19917. #ifdef WOLFSSL_SHA384
  19918. /* p384 */
  19919. ret = wc_HpkeInit(hpke, DHKEM_P384_HKDF_SHA384, HKDF_SHA384,
  19920. HPKE_AES_128_GCM, NULL);
  19921. if (ret != 0)
  19922. return ret;
  19923. ret = hpke_test_single(hpke);
  19924. if (ret != 0)
  19925. return ret;
  19926. #endif
  19927. #if defined(WOLFSSL_SHA384) || defined(WOLFSSL_SHA512)
  19928. /* p521 */
  19929. ret = wc_HpkeInit(hpke, DHKEM_P521_HKDF_SHA512, HKDF_SHA512,
  19930. HPKE_AES_128_GCM, NULL);
  19931. if (ret != 0)
  19932. return ret;
  19933. ret = hpke_test_single(hpke);
  19934. if (ret != 0)
  19935. return ret;
  19936. #endif
  19937. #endif
  19938. #if defined(HAVE_CURVE25519)
  19939. /* test with curve25519 and aes256 */
  19940. ret = wc_HpkeInit(hpke, DHKEM_X25519_HKDF_SHA256, HKDF_SHA256,
  19941. HPKE_AES_256_GCM, NULL);
  19942. if (ret != 0)
  19943. return ret;
  19944. ret = hpke_test_single(hpke);
  19945. if (ret != 0)
  19946. return ret;
  19947. #endif
  19948. return ret;
  19949. /* x448 and chacha20 are unimplemented */
  19950. }
  19951. #endif /* HAVE_HPKE && HAVE_ECC && HAVE_AESGCM */
  19952. #ifdef HAVE_ECC
  19953. /* size to use for ECC key gen tests */
  19954. #ifndef ECC_KEYGEN_SIZE
  19955. #ifndef NO_ECC256
  19956. #define ECC_KEYGEN_SIZE 32
  19957. #elif defined(HAVE_ECC384)
  19958. #define ECC_KEYGEN_SIZE 48
  19959. #elif defined(HAVE_ECC224)
  19960. #define ECC_KEYGEN_SIZE 28
  19961. #elif defined(HAVE_ECC521)
  19962. #define ECC_KEYGEN_SIZE 66
  19963. #else
  19964. #error No ECC keygen size defined for test
  19965. #endif
  19966. #endif
  19967. #ifdef BENCH_EMBEDDED
  19968. #define ECC_SHARED_SIZE 128
  19969. #else
  19970. #define ECC_SHARED_SIZE MAX_ECC_BYTES
  19971. #endif
  19972. #if defined(WOLFSSL_ECDSA_DETERMINISTIC_K) || defined(WOLFSSL_ECDSA_DETERMINISTIC_K_VARIANT)
  19973. #define HAVE_ECC_DETERMINISTIC_K
  19974. #define ECC_DIGEST_SIZE WC_SHA256_DIGEST_SIZE
  19975. #else
  19976. #define ECC_DIGEST_SIZE MAX_ECC_BYTES
  19977. #endif
  19978. #define ECC_SIG_SIZE ECC_MAX_SIG_SIZE
  19979. #ifndef NO_ECC_VECTOR_TEST
  19980. #if (defined(HAVE_ECC192) || defined(HAVE_ECC224) ||\
  19981. !defined(NO_ECC256) || defined(HAVE_ECC384) ||\
  19982. defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES))
  19983. #define HAVE_ECC_VECTOR_TEST
  19984. #endif
  19985. #endif
  19986. #ifdef HAVE_ECC_VECTOR_TEST
  19987. typedef struct eccVector {
  19988. const char* msg; /* SHA-1 Encoded Message */
  19989. const char* Qx;
  19990. const char* Qy;
  19991. const char* d; /* Private Key */
  19992. const char* R;
  19993. const char* S;
  19994. const char* curveName;
  19995. word32 msgLen;
  19996. word32 keySize;
  19997. #ifndef NO_ASN
  19998. const byte* r;
  19999. word32 rSz;
  20000. const byte* s;
  20001. word32 sSz;
  20002. #endif
  20003. } eccVector;
  20004. #if !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  20005. static int ecc_test_vector_item(const eccVector* vector)
  20006. {
  20007. int ret = 0, verify = 0;
  20008. word32 sigSz;
  20009. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20010. ecc_key *userA = (ecc_key *)XMALLOC(sizeof *userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20011. #else
  20012. ecc_key userA[1];
  20013. #endif
  20014. WC_DECLARE_VAR(sig, byte, ECC_SIG_SIZE, HEAP_HINT);
  20015. #if !defined(NO_ASN) && !defined(HAVE_SELFTEST)
  20016. word32 sigRawSz, rSz = MAX_ECC_BYTES, sSz = MAX_ECC_BYTES;
  20017. WC_DECLARE_VAR(sigRaw, byte, ECC_SIG_SIZE, HEAP_HINT);
  20018. WC_DECLARE_VAR(r, byte, MAX_ECC_BYTES, HEAP_HINT);
  20019. WC_DECLARE_VAR(s, byte, MAX_ECC_BYTES, HEAP_HINT);
  20020. #endif
  20021. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  20022. if (sig == NULL)
  20023. ERROR_OUT(MEMORY_E, done);
  20024. #if !defined(NO_ASN) && !defined(HAVE_SELFTEST)
  20025. if (sigRaw == NULL || r == NULL || s == NULL)
  20026. ERROR_OUT(MEMORY_E, done);
  20027. #endif
  20028. #endif
  20029. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20030. if (userA == NULL)
  20031. ERROR_OUT(MEMORY_E, done);
  20032. #endif
  20033. ret = wc_ecc_init_ex(userA, HEAP_HINT, devId);
  20034. if (ret != 0)
  20035. goto done;
  20036. ret = wc_ecc_import_raw(userA, vector->Qx, vector->Qy,
  20037. vector->d, vector->curveName);
  20038. if (ret != 0)
  20039. goto done;
  20040. XMEMSET(sig, 0, ECC_SIG_SIZE);
  20041. sigSz = ECC_SIG_SIZE;
  20042. ret = wc_ecc_rs_to_sig(vector->R, vector->S, sig, &sigSz);
  20043. if (ret != 0)
  20044. goto done;
  20045. #if !defined(NO_ASN) && !defined(HAVE_SELFTEST)
  20046. XMEMSET(sigRaw, 0, ECC_SIG_SIZE);
  20047. sigRawSz = ECC_SIG_SIZE;
  20048. ret = wc_ecc_rs_raw_to_sig(vector->r, vector->rSz, vector->s, vector->sSz,
  20049. sigRaw, &sigRawSz);
  20050. if (ret != 0)
  20051. goto done;
  20052. if (sigSz != sigRawSz || XMEMCMP(sig, sigRaw, sigSz) != 0) {
  20053. ret = -9810;
  20054. goto done;
  20055. }
  20056. ret = wc_ecc_sig_to_rs(sig, sigSz, r, &rSz, s, &sSz);
  20057. if (ret != 0)
  20058. goto done;
  20059. if (rSz != vector->rSz || XMEMCMP(r, vector->r, rSz) != 0 ||
  20060. sSz != vector->sSz || XMEMCMP(s, vector->s, sSz) != 0) {
  20061. ret = -9811;
  20062. goto done;
  20063. }
  20064. #endif
  20065. #ifdef HAVE_ECC_VERIFY
  20066. do {
  20067. #if defined(WOLFSSL_ASYNC_CRYPT)
  20068. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  20069. #endif
  20070. if (ret == 0)
  20071. ret = wc_ecc_verify_hash(sig, sigSz, (byte*)vector->msg,
  20072. vector->msgLen, &verify, userA);
  20073. } while (ret == WC_PENDING_E);
  20074. if (ret != 0)
  20075. goto done;
  20076. TEST_SLEEP();
  20077. if (verify != 1)
  20078. ret = -9812;
  20079. #endif
  20080. done:
  20081. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20082. if (userA != NULL) {
  20083. wc_ecc_free(userA);
  20084. XFREE(userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20085. }
  20086. #else
  20087. wc_ecc_free(userA);
  20088. #endif
  20089. #if !defined(NO_ASN) && !defined(HAVE_SELFTEST)
  20090. WC_FREE_VAR(sigRaw, HEAP_HINT);
  20091. WC_FREE_VAR(r, HEAP_HINT);
  20092. WC_FREE_VAR(s, HEAP_HINT);
  20093. #endif
  20094. WC_FREE_VAR(sig, HEAP_HINT);
  20095. return ret;
  20096. }
  20097. static int ecc_test_vector(int keySize)
  20098. {
  20099. int ret;
  20100. eccVector vec;
  20101. XMEMSET(&vec, 0, sizeof(vec));
  20102. vec.keySize = (word32)keySize;
  20103. switch(keySize) {
  20104. #if defined(HAVE_ECC112) || defined(HAVE_ALL_CURVES)
  20105. case 14:
  20106. return 0;
  20107. #endif /* HAVE_ECC112 */
  20108. #if defined(HAVE_ECC128) || defined(HAVE_ALL_CURVES)
  20109. case 16:
  20110. return 0;
  20111. #endif /* HAVE_ECC128 */
  20112. #if defined(HAVE_ECC160) || defined(HAVE_ALL_CURVES)
  20113. case 20:
  20114. return 0;
  20115. #endif /* HAVE_ECC160 */
  20116. #if defined(HAVE_ECC192) || defined(HAVE_ALL_CURVES)
  20117. case 24:
  20118. /* first [P-192,SHA-1] vector from FIPS 186-3 NIST vectors */
  20119. #if 1
  20120. vec.msg = "\x60\x80\x79\x42\x3f\x12\x42\x1d\xe6\x16\xb7\x49\x3e\xbe\x55\x1c\xf4\xd6\x5b\x92";
  20121. vec.msgLen = 20;
  20122. #else
  20123. /* This is the raw message prior to SHA-1 */
  20124. vec.msg =
  20125. "\xeb\xf7\x48\xd7\x48\xeb\xbc\xa7\xd2\x9f\xb4\x73\x69\x8a\x6e\x6b"
  20126. "\x4f\xb1\x0c\x86\x5d\x4a\xf0\x24\xcc\x39\xae\x3d\xf3\x46\x4b\xa4"
  20127. "\xf1\xd6\xd4\x0f\x32\xbf\x96\x18\xa9\x1b\xb5\x98\x6f\xa1\xa2\xaf"
  20128. "\x04\x8a\x0e\x14\xdc\x51\xe5\x26\x7e\xb0\x5e\x12\x7d\x68\x9d\x0a"
  20129. "\xc6\xf1\xa7\xf1\x56\xce\x06\x63\x16\xb9\x71\xcc\x7a\x11\xd0\xfd"
  20130. "\x7a\x20\x93\xe2\x7c\xf2\xd0\x87\x27\xa4\xe6\x74\x8c\xc3\x2f\xd5"
  20131. "\x9c\x78\x10\xc5\xb9\x01\x9d\xf2\x1c\xdc\xc0\xbc\xa4\x32\xc0\xa3"
  20132. "\xee\xd0\x78\x53\x87\x50\x88\x77\x11\x43\x59\xce\xe4\xa0\x71\xcf";
  20133. vec.msgLen = 128;
  20134. #endif
  20135. vec.Qx = "07008ea40b08dbe76432096e80a2494c94982d2d5bcf98e6";
  20136. vec.Qy = "76fab681d00b414ea636ba215de26d98c41bd7f2e4d65477";
  20137. vec.d = "e14f37b3d1374ff8b03f41b9b3fdd2f0ebccf275d660d7f3";
  20138. vec.R = "6994d962bdd0d793ffddf855ec5bf2f91a9698b46258a63e";
  20139. vec.S = "02ba6465a234903744ab02bc8521405b73cf5fc00e1a9f41";
  20140. vec.curveName = "SECP192R1";
  20141. #ifndef NO_ASN
  20142. vec.r = (byte*)"\x69\x94\xd9\x62\xbd\xd0\xd7\x93\xff\xdd\xf8\x55"
  20143. "\xec\x5b\xf2\xf9\x1a\x96\x98\xb4\x62\x58\xa6\x3e";
  20144. vec.rSz = 24;
  20145. vec.s = (byte*)"\x02\xba\x64\x65\xa2\x34\x90\x37\x44\xab\x02\xbc"
  20146. "\x85\x21\x40\x5b\x73\xcf\x5f\xc0\x0e\x1a\x9f\x41";
  20147. vec.sSz = 24;
  20148. #endif
  20149. break;
  20150. #endif /* HAVE_ECC192 */
  20151. #if defined(HAVE_ECC224) || defined(HAVE_ALL_CURVES)
  20152. case 28:
  20153. /* first [P-224,SHA-1] vector from FIPS 186-3 NIST vectors */
  20154. #if 1
  20155. vec.msg = "\xb9\xa3\xb8\x6d\xb0\xba\x99\xfd\xc6\xd2\x94\x6b\xfe\xbe\x9c\xe8\x3f\x10\x74\xfc";
  20156. vec.msgLen = 20;
  20157. #else
  20158. /* This is the raw message prior to SHA-1 */
  20159. vec.msg =
  20160. "\x36\xc8\xb2\x29\x86\x48\x7f\x67\x7c\x18\xd0\x97\x2a\x9e\x20\x47"
  20161. "\xb3\xaf\xa5\x9e\xc1\x62\x76\x4e\xc3\x0b\x5b\x69\xe0\x63\x0f\x99"
  20162. "\x0d\x4e\x05\xc2\x73\xb0\xe5\xa9\xd4\x28\x27\xb6\x95\xfc\x2d\x64"
  20163. "\xd9\x13\x8b\x1c\xf4\xc1\x21\x55\x89\x4c\x42\x13\x21\xa7\xbb\x97"
  20164. "\x0b\xdc\xe0\xfb\xf0\xd2\xae\x85\x61\xaa\xd8\x71\x7f\x2e\x46\xdf"
  20165. "\xe3\xff\x8d\xea\xb4\xd7\x93\x23\x56\x03\x2c\x15\x13\x0d\x59\x9e"
  20166. "\x26\xc1\x0f\x2f\xec\x96\x30\x31\xac\x69\x38\xa1\x8d\x66\x45\x38"
  20167. "\xb9\x4d\xac\x55\x34\xef\x7b\x59\x94\x24\xd6\x9b\xe1\xf7\x1c\x20";
  20168. vec.msgLen = 128;
  20169. #endif
  20170. vec.Qx = "8a4dca35136c4b70e588e23554637ae251077d1365a6ba5db9585de7";
  20171. vec.Qy = "ad3dee06de0be8279d4af435d7245f14f3b4f82eb578e519ee0057b1";
  20172. vec.d = "97c4b796e1639dd1035b708fc00dc7ba1682cec44a1002a1a820619f";
  20173. vec.R = "147b33758321e722a0360a4719738af848449e2c1d08defebc1671a7";
  20174. vec.S = "24fc7ed7f1352ca3872aa0916191289e2e04d454935d50fe6af3ad5b";
  20175. vec.curveName = "SECP224R1";
  20176. #ifndef NO_ASN
  20177. vec.r = (byte*)"\x14\x7b\x33\x75\x83\x21\xe7\x22\xa0\x36\x0a\x47"
  20178. "\x19\x73\x8a\xf8\x48\x44\x9e\x2c\x1d\x08\xde\xfe"
  20179. "\xbc\x16\x71\xa7";
  20180. vec.rSz = 28;
  20181. vec.s = (byte*)"\x24\xfc\x7e\xd7\xf1\x35\x2c\xa3\x87\x2a\xa0\x91"
  20182. "\x61\x91\x28\x9e\x2e\x04\xd4\x54\x93\x5d\x50\xfe"
  20183. "\x6a\xf3\xad\x5b";
  20184. vec.sSz = 28;
  20185. #endif
  20186. break;
  20187. #endif /* HAVE_ECC224 */
  20188. #if defined(HAVE_ECC239) || defined(HAVE_ALL_CURVES)
  20189. case 30:
  20190. return 0;
  20191. #endif /* HAVE_ECC239 */
  20192. #if !defined(NO_ECC256) || defined(HAVE_ALL_CURVES)
  20193. case 32:
  20194. /* first [P-256,SHA-1] vector from FIPS 186-3 NIST vectors */
  20195. #if 1
  20196. vec.msg = "\xa3\xf9\x1a\xe2\x1b\xa6\xb3\x03\x98\x64\x47\x2f\x18\x41\x44\xc6\xaf\x62\xcd\x0e";
  20197. vec.msgLen = 20;
  20198. #else
  20199. /* This is the raw message prior to SHA-1 */
  20200. vec.msg =
  20201. "\xa2\x4b\x21\x76\x2e\x6e\xdb\x15\x3c\xc1\x14\x38\xdb\x0e\x92\xcd"
  20202. "\xf5\x2b\x86\xb0\x6c\xa9\x70\x16\x06\x27\x59\xc7\x0d\x36\xd1\x56"
  20203. "\x2c\xc9\x63\x0d\x7f\xc7\xc7\x74\xb2\x8b\x54\xe3\x1e\xf5\x58\x72"
  20204. "\xb2\xa6\x5d\xf1\xd7\xec\x26\xde\xbb\x33\xe7\xd9\x27\xef\xcc\xf4"
  20205. "\x6b\x63\xde\x52\xa4\xf4\x31\xea\xca\x59\xb0\x5d\x2e\xde\xc4\x84"
  20206. "\x5f\xff\xc0\xee\x15\x03\x94\xd6\x1f\x3d\xfe\xcb\xcd\xbf\x6f\x5a"
  20207. "\x73\x38\xd0\xbe\x3f\x2a\x77\x34\x51\x98\x3e\xba\xeb\x48\xf6\x73"
  20208. "\x8f\xc8\x95\xdf\x35\x7e\x1a\x48\xa6\x53\xbb\x35\x5a\x31\xa1\xb4"
  20209. vec.msgLen = 128;
  20210. #endif
  20211. vec.Qx = "fa2737fb93488d19caef11ae7faf6b7f4bcd67b286e3fc54e8a65c2b74aeccb0";
  20212. vec.Qy = "d4ccd6dae698208aa8c3a6f39e45510d03be09b2f124bfc067856c324f9b4d09";
  20213. vec.d = "be34baa8d040a3b991f9075b56ba292f755b90e4b6dc10dad36715c33cfdac25";
  20214. vec.R = "2b826f5d44e2d0b6de531ad96b51e8f0c56fdfead3c236892e4d84eacfc3b75c";
  20215. vec.S = "a2248b62c03db35a7cd63e8a120a3521a89d3d2f61ff99035a2148ae32e3a248";
  20216. #ifndef NO_ASN
  20217. vec.r = (byte*)"\x2b\x82\x6f\x5d\x44\xe2\xd0\xb6\xde\x53\x1a\xd9"
  20218. "\x6b\x51\xe8\xf0\xc5\x6f\xdf\xea\xd3\xc2\x36\x89"
  20219. "\x2e\x4d\x84\xea\xcf\xc3\xb7\x5c";
  20220. vec.rSz = 32;
  20221. vec.s = (byte*)"\xa2\x24\x8b\x62\xc0\x3d\xb3\x5a\x7c\xd6\x3e\x8a"
  20222. "\x12\x0a\x35\x21\xa8\x9d\x3d\x2f\x61\xff\x99\x03"
  20223. "\x5a\x21\x48\xae\x32\xe3\xa2\x48";
  20224. vec.sSz = 32;
  20225. #endif
  20226. vec.curveName = "SECP256R1";
  20227. break;
  20228. #endif /* !NO_ECC256 */
  20229. #if defined(HAVE_ECC320) || defined(HAVE_ALL_CURVES)
  20230. case 40:
  20231. return 0;
  20232. #endif /* HAVE_ECC320 */
  20233. #if defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)
  20234. case 48:
  20235. /* first [P-384,SHA-1] vector from FIPS 186-3 NIST vectors */
  20236. #if 1
  20237. vec.msg = "\x9b\x9f\x8c\x95\x35\xa5\xca\x26\x60\x5d\xb7\xf2\xfa\x57\x3b\xdf\xc3\x2e\xab\x8b";
  20238. vec.msgLen = 20;
  20239. #else
  20240. /* This is the raw message prior to SHA-1 */
  20241. vec.msg =
  20242. "\xab\xe1\x0a\xce\x13\xe7\xe1\xd9\x18\x6c\x48\xf7\x88\x9d\x51\x47"
  20243. "\x3d\x3a\x09\x61\x98\x4b\xc8\x72\xdf\x70\x8e\xcc\x3e\xd3\xb8\x16"
  20244. "\x9d\x01\xe3\xd9\x6f\xc4\xf1\xd5\xea\x00\xa0\x36\x92\xbc\xc5\xcf"
  20245. "\xfd\x53\x78\x7c\x88\xb9\x34\xaf\x40\x4c\x03\x9d\x32\x89\xb5\xba"
  20246. "\xc5\xae\x7d\xb1\x49\x68\x75\xb5\xdc\x73\xc3\x09\xf9\x25\xc1\x3d"
  20247. "\x1c\x01\xab\xda\xaf\xeb\xcd\xac\x2c\xee\x43\x39\x39\xce\x8d\x4a"
  20248. "\x0a\x5d\x57\xbb\x70\x5f\x3b\xf6\xec\x08\x47\x95\x11\xd4\xb4\xa3"
  20249. "\x21\x1f\x61\x64\x9a\xd6\x27\x43\x14\xbf\x0d\x43\x8a\x81\xe0\x60"
  20250. vec.msgLen = 128;
  20251. #endif
  20252. vec.Qx = "e55fee6c49d8d523f5ce7bf9c0425ce4ff650708b7de5cfb095901523979a7f042602db30854735369813b5c3f5ef868";
  20253. vec.Qy = "28f59cc5dc509892a988d38a8e2519de3d0c4fd0fbdb0993e38f18506c17606c5e24249246f1ce94983a5361c5be983e";
  20254. vec.d = "a492ce8fa90084c227e1a32f7974d39e9ff67a7e8705ec3419b35fb607582bebd461e0b1520ac76ec2dd4e9b63ebae71";
  20255. vec.R = "6820b8585204648aed63bdff47f6d9acebdea62944774a7d14f0e14aa0b9a5b99545b2daee6b3c74ebf606667a3f39b7";
  20256. vec.S = "491af1d0cccd56ddd520b233775d0bc6b40a6255cc55207d8e9356741f23c96c14714221078dbd5c17f4fdd89b32a907";
  20257. vec.curveName = "SECP384R1";
  20258. #ifndef NO_ASN
  20259. vec.r = (byte*)"\x68\x20\xb8\x58\x52\x04\x64\x8a\xed\x63\xbd\xff"
  20260. "\x47\xf6\xd9\xac\xeb\xde\xa6\x29\x44\x77\x4a\x7d"
  20261. "\x14\xf0\xe1\x4a\xa0\xb9\xa5\xb9\x95\x45\xb2\xda"
  20262. "\xee\x6b\x3c\x74\xeb\xf6\x06\x66\x7a\x3f\x39\xb7";
  20263. vec.rSz = 48;
  20264. vec.s = (byte*)"\x49\x1a\xf1\xd0\xcc\xcd\x56\xdd\xd5\x20\xb2\x33"
  20265. "\x77\x5d\x0b\xc6\xb4\x0a\x62\x55\xcc\x55\x20\x7d"
  20266. "\x8e\x93\x56\x74\x1f\x23\xc9\x6c\x14\x71\x42\x21"
  20267. "\x07\x8d\xbd\x5c\x17\xf4\xfd\xd8\x9b\x32\xa9\x07";
  20268. vec.sSz = 48;
  20269. #endif
  20270. break;
  20271. #endif /* HAVE_ECC384 */
  20272. #if defined(HAVE_ECC512) || defined(HAVE_ALL_CURVES)
  20273. case 64:
  20274. return 0;
  20275. #endif /* HAVE_ECC512 */
  20276. #if defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)
  20277. case 66:
  20278. /* first [P-521,SHA-1] vector from FIPS 186-3 NIST vectors */
  20279. #if 1
  20280. vec.msg = "\x1b\xf7\x03\x9c\xca\x23\x94\x27\x3f\x11\xa1\xd4\x8d\xcc\xb4\x46\x6f\x31\x61\xdf";
  20281. vec.msgLen = 20;
  20282. #else
  20283. /* This is the raw message prior to SHA-1 */
  20284. vec.msg =
  20285. "\x50\x3f\x79\x39\x34\x0a\xc7\x23\xcd\x4a\x2f\x4e\x6c\xcc\x27\x33"
  20286. "\x38\x3a\xca\x2f\xba\x90\x02\x19\x9d\x9e\x1f\x94\x8b\xe0\x41\x21"
  20287. "\x07\xa3\xfd\xd5\x14\xd9\x0c\xd4\xf3\x7c\xc3\xac\x62\xef\x00\x3a"
  20288. "\x2d\xb1\xd9\x65\x7a\xb7\x7f\xe7\x55\xbf\x71\xfa\x59\xe4\xd9\x6e"
  20289. "\xa7\x2a\xe7\xbf\x9d\xe8\x7d\x79\x34\x3b\xc1\xa4\xbb\x14\x4d\x16"
  20290. "\x28\xd1\xe9\xe9\xc8\xed\x80\x8b\x96\x2c\x54\xe5\xf9\x6d\x53\xda"
  20291. "\x14\x7a\x96\x38\xf9\x4a\x91\x75\xd8\xed\x61\x05\x5f\x0b\xa5\x73"
  20292. "\xa8\x2b\xb7\xe0\x18\xee\xda\xc4\xea\x7b\x36\x2e\xc8\x9c\x38\x2b"
  20293. vec.msgLen = 128;
  20294. #endif
  20295. vec.Qx = "12fbcaeffa6a51f3ee4d3d2b51c5dec6d7c726ca353fc014ea2bf7cfbb9b910d32cbfa6a00fe39b6cdb8946f22775398b2e233c0cf144d78c8a7742b5c7a3bb5d23";
  20296. vec.Qy = "09cdef823dd7bf9a79e8cceacd2e4527c231d0ae5967af0958e931d7ddccf2805a3e618dc3039fec9febbd33052fe4c0fee98f033106064982d88f4e03549d4a64d";
  20297. vec.d = "1bd56bd106118eda246155bd43b42b8e13f0a6e25dd3bb376026fab4dc92b6157bc6dfec2d15dd3d0cf2a39aa68494042af48ba9601118da82c6f2108a3a203ad74";
  20298. vec.R = "0bd117b4807710898f9dd7778056485777668f0e78e6ddf5b000356121eb7a220e9493c7f9a57c077947f89ac45d5acb6661bbcd17abb3faea149ba0aa3bb1521be";
  20299. vec.S = "019cd2c5c3f9870ecdeb9b323abdf3a98cd5e231d85c6ddc5b71ab190739f7f226e6b134ba1d5889ddeb2751dabd97911dff90c34684cdbe7bb669b6c3d22f2480c";
  20300. vec.curveName = "SECP521R1";
  20301. #ifndef NO_ASN
  20302. vec.r = (byte*)"\xbd\x11\x7b\x48\x07\x71\x08\x98\xf9\xdd\x77\x78"
  20303. "\x05\x64\x85\x77\x76\x68\xf0\xe7\x8e\x6d\xdf\x5b"
  20304. "\x00\x03\x56\x12\x1e\xb7\xa2\x20\xe9\x49\x3c\x7f"
  20305. "\x9a\x57\xc0\x77\x94\x7f\x89\xac\x45\xd5\xac\xb6"
  20306. "\x66\x1b\xbc\xd1\x7a\xbb\x3f\xae\xa1\x49\xba\x0a"
  20307. "\xa3\xbb\x15\x21\xbe";
  20308. vec.rSz = 65;
  20309. vec.s = (byte*)"\x19\xcd\x2c\x5c\x3f\x98\x70\xec\xde\xb9\xb3\x23"
  20310. "\xab\xdf\x3a\x98\xcd\x5e\x23\x1d\x85\xc6\xdd\xc5"
  20311. "\xb7\x1a\xb1\x90\x73\x9f\x7f\x22\x6e\x6b\x13\x4b"
  20312. "\xa1\xd5\x88\x9d\xde\xb2\x75\x1d\xab\xd9\x79\x11"
  20313. "\xdf\xf9\x0c\x34\x68\x4c\xdb\xe7\xbb\x66\x9b\x6c"
  20314. "\x3d\x22\xf2\x48\x0c";
  20315. vec.sSz = 65;
  20316. #endif
  20317. break;
  20318. #endif /* HAVE_ECC521 */
  20319. default:
  20320. return NOT_COMPILED_IN; /* Invalid key size / Not supported */
  20321. }; /* Switch */
  20322. ret = ecc_test_vector_item(&vec);
  20323. if (ret < 0) {
  20324. return ret;
  20325. }
  20326. return 0;
  20327. }
  20328. #endif /* WOLF_CRYPTO_CB_ONLY_ECC */
  20329. #if defined(HAVE_ECC_SIGN) && (defined(WOLFSSL_ECDSA_DETERMINISTIC_K) || \
  20330. defined(WOLFSSL_ECDSA_DETERMINISTIC_K_VARIANT)) \
  20331. && (!defined(FIPS_VERSION_GE) || FIPS_VERSION_GE(5,3))
  20332. #if defined(HAVE_ECC256)
  20333. static int ecc_test_deterministic_k(WC_RNG* rng)
  20334. {
  20335. int ret;
  20336. #ifdef WOLFSSL_SMALL_STACK
  20337. ecc_key *key = NULL;
  20338. #else
  20339. ecc_key key[1];
  20340. #endif
  20341. int key_inited = 0;
  20342. byte sig[72];
  20343. word32 sigSz;
  20344. WOLFSSL_SMALL_STACK_STATIC const unsigned char msg[] = "sample";
  20345. unsigned char hash[32];
  20346. WOLFSSL_SMALL_STACK_STATIC const char* dIUT =
  20347. "C9AFA9D845BA75166B5C215767B1D6934E50C3DB36E89B127B8A622B120F6721";
  20348. WOLFSSL_SMALL_STACK_STATIC const char* QIUTx =
  20349. "60FED4BA255A9D31C961EB74C6356D68C049B8923B61FA6CE669622E60F29FB6";
  20350. WOLFSSL_SMALL_STACK_STATIC const char* QIUTy =
  20351. "7903FE1008B8BC99A41AE9E95628BC64F2F1B20C2D7E9F5177A3C294D4462299";
  20352. WOLFSSL_SMALL_STACK_STATIC const byte expSig[] = {
  20353. 0x30, 0x46, 0x02, 0x21, 0x00, 0xEF, 0xD4, 0x8B,
  20354. 0x2A, 0xAC, 0xB6, 0xA8, 0xFD, 0x11, 0x40, 0xDD,
  20355. 0x9C, 0xD4, 0x5E, 0x81, 0xD6, 0x9D, 0x2C, 0x87,
  20356. 0x7B, 0x56, 0xAA, 0xF9, 0x91, 0xC3, 0x4D, 0x0E,
  20357. 0xA8, 0x4E, 0xAF, 0x37, 0x16, 0x02, 0x21, 0x00,
  20358. 0xF7, 0xCB, 0x1C, 0x94, 0x2D, 0x65, 0x7C, 0x41,
  20359. 0xD4, 0x36, 0xC7, 0xA1, 0xB6, 0xE2, 0x9F, 0x65,
  20360. 0xF3, 0xE9, 0x00, 0xDB, 0xB9, 0xAF, 0xF4, 0x06,
  20361. 0x4D, 0xC4, 0xAB, 0x2F, 0x84, 0x3A, 0xCD, 0xA8
  20362. };
  20363. #ifdef WOLFSSL_SMALL_STACK
  20364. key = (ecc_key *)XMALLOC(sizeof(*key), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20365. if (key == NULL)
  20366. return MEMORY_E;
  20367. #endif
  20368. ret = wc_ecc_init_ex(key, HEAP_HINT, devId);
  20369. if (ret != 0) {
  20370. goto done;
  20371. }
  20372. key_inited = 1;
  20373. ret = wc_ecc_import_raw(key, QIUTx, QIUTy, dIUT, "SECP256R1");
  20374. if (ret != 0) {
  20375. goto done;
  20376. }
  20377. ret = wc_Hash(WC_HASH_TYPE_SHA256, msg,
  20378. (word32)XSTRLEN((const char*)msg), hash, sizeof(hash));
  20379. if (ret != 0) {
  20380. goto done;
  20381. }
  20382. ret = wc_ecc_set_deterministic(key, 1);
  20383. if (ret != 0) {
  20384. goto done;
  20385. }
  20386. sigSz = sizeof(sig);
  20387. do {
  20388. #if defined(WOLFSSL_ASYNC_CRYPT)
  20389. ret = wc_AsyncWait(ret, key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  20390. #endif
  20391. if (ret == 0)
  20392. ret = wc_ecc_sign_hash(hash, sizeof(hash), sig, &sigSz, rng, key);
  20393. } while (ret == WC_PENDING_E);
  20394. if (ret != 0) {
  20395. goto done;
  20396. }
  20397. TEST_SLEEP();
  20398. if (sigSz != sizeof(expSig)) {
  20399. ret = -9830;
  20400. goto done;
  20401. }
  20402. if (XMEMCMP(sig, expSig, sigSz) != 0) {
  20403. ret = -9831;
  20404. goto done;
  20405. }
  20406. sigSz = sizeof(sig);
  20407. do {
  20408. #if defined(WOLFSSL_ASYNC_CRYPT)
  20409. ret = wc_AsyncWait(ret, key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  20410. #endif
  20411. if (ret == 0)
  20412. ret = wc_ecc_sign_hash(hash, sizeof(hash), sig, &sigSz, rng, key);
  20413. } while (ret == WC_PENDING_E);
  20414. if (ret != 0) {
  20415. goto done;
  20416. }
  20417. TEST_SLEEP();
  20418. done:
  20419. if (key_inited)
  20420. wc_ecc_free(key);
  20421. #ifdef WOLFSSL_SMALL_STACK
  20422. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20423. #endif
  20424. return ret;
  20425. }
  20426. #endif
  20427. #ifdef WOLFSSL_PUBLIC_MP
  20428. #if defined(HAVE_ECC384)
  20429. /* KAT from RFC6979 */
  20430. static int ecc384_test_deterministic_k(WC_RNG* rng)
  20431. {
  20432. int ret;
  20433. #ifdef WOLFSSL_SMALL_STACK
  20434. ecc_key *key;
  20435. mp_int *r, *s, *expR, *expS;
  20436. #else
  20437. ecc_key key[1];
  20438. mp_int r[1], s[1], expR[1], expS[1];
  20439. #endif
  20440. int key_inited = 0;
  20441. WOLFSSL_SMALL_STACK_STATIC const unsigned char msg[] = "sample";
  20442. unsigned char hash[32];
  20443. WOLFSSL_SMALL_STACK_STATIC const char* dIUT =
  20444. "6B9D3DAD2E1B8C1C05B19875B6659F4DE23C3B667BF297BA9AA47740787137D8"
  20445. "96D5724E4C70A825F872C9EA60D2EDF5";
  20446. WOLFSSL_SMALL_STACK_STATIC const char* QIUTx =
  20447. "EC3A4E415B4E19A4568618029F427FA5DA9A8BC4AE92E02E06AAE5286B300C64"
  20448. "DEF8F0EA9055866064A254515480BC13";
  20449. WOLFSSL_SMALL_STACK_STATIC const char* QIUTy =
  20450. "8015D9B72D7D57244EA8EF9AC0C621896708A59367F9DFB9F54CA84B3F1C9DB1"
  20451. "288B231C3AE0D4FE7344FD2533264720";
  20452. WOLFSSL_SMALL_STACK_STATIC const char* expRstr =
  20453. "21B13D1E013C7FA1392D03C5F99AF8B30C570C6F98D4EA8E354B63A21D3DAA33"
  20454. "BDE1E888E63355D92FA2B3C36D8FB2CD";
  20455. WOLFSSL_SMALL_STACK_STATIC const char* expSstr =
  20456. "F3AA443FB107745BF4BD77CB3891674632068A10CA67E3D45DB2266FA7D1FEEB"
  20457. "EFDC63ECCD1AC42EC0CB8668A4FA0AB0";
  20458. #ifdef WOLFSSL_SMALL_STACK
  20459. key = (ecc_key *)XMALLOC(sizeof(*key), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20460. r = (mp_int *)XMALLOC(sizeof(*r), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20461. s = (mp_int *)XMALLOC(sizeof(*s), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20462. expR = (mp_int *)XMALLOC(sizeof(*expR), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20463. expS = (mp_int *)XMALLOC(sizeof(*expS), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20464. if ((key == NULL) ||
  20465. (r == NULL) ||
  20466. (s == NULL) ||
  20467. (expR == NULL) ||
  20468. (expS == NULL))
  20469. {
  20470. ret = MEMORY_E;
  20471. goto done;
  20472. }
  20473. #endif
  20474. ret = mp_init_multi(r, s, expR, expS, NULL, NULL);
  20475. if (ret != MP_OKAY) {
  20476. goto done;
  20477. }
  20478. ret = wc_ecc_init_ex(key, HEAP_HINT, devId);
  20479. if (ret != 0) {
  20480. goto done;
  20481. }
  20482. key_inited = 1;
  20483. ret = wc_ecc_import_raw(key, QIUTx, QIUTy, dIUT, "SECP384R1");
  20484. if (ret != 0) {
  20485. goto done;
  20486. }
  20487. ret = wc_Hash(WC_HASH_TYPE_SHA256, msg,
  20488. (word32)XSTRLEN((const char*)msg), hash, sizeof(hash));
  20489. if (ret != 0) {
  20490. goto done;
  20491. }
  20492. ret = wc_ecc_set_deterministic(key, 1);
  20493. if (ret != 0) {
  20494. goto done;
  20495. }
  20496. do {
  20497. #if defined(WOLFSSL_ASYNC_CRYPT)
  20498. ret = wc_AsyncWait(ret, key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  20499. #endif
  20500. if (ret == 0)
  20501. ret = wc_ecc_sign_hash_ex(hash, sizeof(hash), rng, key, r, s);
  20502. } while (ret == WC_PENDING_E);
  20503. if (ret != 0) {
  20504. goto done;
  20505. }
  20506. TEST_SLEEP();
  20507. mp_read_radix(expR, expRstr, MP_RADIX_HEX);
  20508. mp_read_radix(expS, expSstr, MP_RADIX_HEX);
  20509. if (mp_cmp(r, expR) != MP_EQ) {
  20510. ret = -1;
  20511. }
  20512. done:
  20513. if (key_inited)
  20514. wc_ecc_free(key);
  20515. #ifdef WOLFSSL_SMALL_STACK
  20516. if (key != NULL)
  20517. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20518. if (r != NULL)
  20519. XFREE(r, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20520. if (s != NULL)
  20521. XFREE(s, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20522. if (expR != NULL)
  20523. XFREE(expR, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20524. if (expS != NULL)
  20525. XFREE(expS, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20526. #endif
  20527. return ret;
  20528. }
  20529. #endif /* HAVE_ECC384 */
  20530. #if defined(HAVE_ECC521)
  20531. /* KAT from RFC6979 */
  20532. static int ecc521_test_deterministic_k(WC_RNG* rng)
  20533. {
  20534. int ret;
  20535. #ifdef WOLFSSL_SMALL_STACK
  20536. ecc_key *key;
  20537. mp_int *r, *s, *expR, *expS;
  20538. #else
  20539. ecc_key key[1];
  20540. mp_int r[1], s[1], expR[1], expS[1];
  20541. #endif
  20542. int key_inited = 0;
  20543. WOLFSSL_SMALL_STACK_STATIC const unsigned char msg[] = "sample";
  20544. unsigned char hash[32];
  20545. WOLFSSL_SMALL_STACK_STATIC const char* dIUT =
  20546. "0FAD06DAA62BA3B25D2FB40133DA757205DE67F5BB0018FEE8C86E1B68C7E75C"
  20547. "AA896EB32F1F47C70855836A6D16FCC1466F6D8FBEC67DB89EC0C08B0E996B83"
  20548. "538";
  20549. WOLFSSL_SMALL_STACK_STATIC const char* QIUTx =
  20550. "1894550D0785932E00EAA23B694F213F8C3121F86DC97A04E5A7167DB4E5BCD3"
  20551. "71123D46E45DB6B5D5370A7F20FB633155D38FFA16D2BD761DCAC474B9A2F502"
  20552. "3A4";
  20553. WOLFSSL_SMALL_STACK_STATIC const char* QIUTy =
  20554. "0493101C962CD4D2FDDF782285E64584139C2F91B47F87FF82354D6630F746A2"
  20555. "8A0DB25741B5B34A828008B22ACC23F924FAAFBD4D33F81EA66956DFEAA2BFDF"
  20556. "CF5";
  20557. WOLFSSL_SMALL_STACK_STATIC const char* expRstr =
  20558. "1511BB4D675114FE266FC4372B87682BAECC01D3CC62CF2303C92B3526012659"
  20559. "D16876E25C7C1E57648F23B73564D67F61C6F14D527D54972810421E7D87589E"
  20560. "1A7";
  20561. WOLFSSL_SMALL_STACK_STATIC const char* expSstr =
  20562. "04A171143A83163D6DF460AAF61522695F207A58B95C0644D87E52AA1A347916"
  20563. "E4F7A72930B1BC06DBE22CE3F58264AFD23704CBB63B29B931F7DE6C9D949A7E"
  20564. "CFC";
  20565. #ifdef WOLFSSL_SMALL_STACK
  20566. key = (ecc_key *)XMALLOC(sizeof(*key), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20567. r = (mp_int *)XMALLOC(sizeof(*r), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20568. s = (mp_int *)XMALLOC(sizeof(*s), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20569. expR = (mp_int *)XMALLOC(sizeof(*expR), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20570. expS = (mp_int *)XMALLOC(sizeof(*expS), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20571. if ((key == NULL) ||
  20572. (r == NULL) ||
  20573. (s == NULL) ||
  20574. (expR == NULL) ||
  20575. (expS == NULL))
  20576. {
  20577. ret = MEMORY_E;
  20578. goto done;
  20579. }
  20580. #endif
  20581. ret = mp_init_multi(r, s, expR, expS, NULL, NULL);
  20582. if (ret != MP_OKAY) {
  20583. goto done;
  20584. }
  20585. ret = wc_ecc_init_ex(key, HEAP_HINT, devId);
  20586. if (ret != 0) {
  20587. return ret;
  20588. }
  20589. key_inited = 1;
  20590. ret = wc_ecc_import_raw(key, QIUTx, QIUTy, dIUT, "SECP521R1");
  20591. if (ret != 0) {
  20592. goto done;
  20593. }
  20594. ret = wc_Hash(WC_HASH_TYPE_SHA256, msg,
  20595. (word32)XSTRLEN((const char*)msg), hash, sizeof(hash));
  20596. if (ret != 0) {
  20597. goto done;
  20598. }
  20599. ret = wc_ecc_set_deterministic(key, 1);
  20600. if (ret != 0) {
  20601. goto done;
  20602. }
  20603. do {
  20604. #if defined(WOLFSSL_ASYNC_CRYPT)
  20605. ret = wc_AsyncWait(ret, key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  20606. #endif
  20607. if (ret == 0)
  20608. ret = wc_ecc_sign_hash_ex(hash, sizeof(hash), rng, key, r, s);
  20609. } while (ret == WC_PENDING_E);
  20610. if (ret != 0) {
  20611. goto done;
  20612. }
  20613. TEST_SLEEP();
  20614. mp_read_radix(expR, expRstr, MP_RADIX_HEX);
  20615. mp_read_radix(expS, expSstr, MP_RADIX_HEX);
  20616. if (mp_cmp(r, expR) != MP_EQ) {
  20617. ret = -1;
  20618. }
  20619. done:
  20620. if (key_inited)
  20621. wc_ecc_free(key);
  20622. #ifdef WOLFSSL_SMALL_STACK
  20623. if (key != NULL)
  20624. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20625. if (r != NULL)
  20626. XFREE(r, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20627. if (s != NULL)
  20628. XFREE(s, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20629. if (expR != NULL)
  20630. XFREE(expR, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20631. if (expS != NULL)
  20632. XFREE(expS, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20633. #endif
  20634. return ret;
  20635. }
  20636. #endif /* HAVE_ECC521 */
  20637. #endif /* WOLFSSL_PUBLIC_MP */
  20638. #endif /* HAVE_ECC_SIGN && (WOLFSSL_ECDSA_DETERMINISTIC_K ||
  20639. WOLFSSL_ECDSA_DETERMINISTIC_K_VARIANT)
  20640. && (!FIPS_VERSION_GE || FIPS_VERSION_GE(5,3)) */
  20641. #if defined(HAVE_ECC_SIGN) && defined(WOLFSSL_ECDSA_SET_K) && \
  20642. !defined(WOLFSSL_KCAPI_ECC)
  20643. static int ecc_test_sign_vectors(WC_RNG* rng)
  20644. {
  20645. int ret;
  20646. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20647. ecc_key *key = NULL;
  20648. #else
  20649. ecc_key key[1];
  20650. #endif
  20651. int key_inited = 0;
  20652. byte sig[72];
  20653. word32 sigSz;
  20654. WOLFSSL_SMALL_STACK_STATIC const unsigned char hash[32] = "test wolfSSL deterministic sign";
  20655. WOLFSSL_SMALL_STACK_STATIC const char* dIUT = "7d7dc5f71eb29ddaf80d6214632eeae03d9058af1fb6d22ed80badb62bc1a534";
  20656. WOLFSSL_SMALL_STACK_STATIC const char* QIUTx = "ead218590119e8876b29146ff89ca61770c4edbbf97d38ce385ed281d8a6b230";
  20657. WOLFSSL_SMALL_STACK_STATIC const char* QIUTy = "28af61281fd35e2fa7002523acc85a429cb06ee6648325389f59edfce1405141";
  20658. WOLFSSL_SMALL_STACK_STATIC const byte k[1] = { 0x02 };
  20659. WOLFSSL_SMALL_STACK_STATIC const byte expSig[71] = {
  20660. 0x30, 0x45, 0x02, 0x20, 0x7c, 0xf2, 0x7b, 0x18,
  20661. 0x8d, 0x03, 0x4f, 0x7e, 0x8a, 0x52, 0x38, 0x03,
  20662. 0x04, 0xb5, 0x1a, 0xc3, 0xc0, 0x89, 0x69, 0xe2,
  20663. 0x77, 0xf2, 0x1b, 0x35, 0xa6, 0x0b, 0x48, 0xfc,
  20664. 0x47, 0x66, 0x99, 0x78, 0x02, 0x21, 0x00, 0xa8,
  20665. 0x43, 0xa0, 0xce, 0x6c, 0x5e, 0x17, 0x8a, 0x53,
  20666. 0x4d, 0xaf, 0xd2, 0x95, 0x78, 0x9f, 0x84, 0x4f,
  20667. 0x94, 0xb8, 0x75, 0xa3, 0x19, 0xa5, 0xd4, 0xdf,
  20668. 0xe1, 0xd4, 0x5e, 0x9d, 0x97, 0xfe, 0x81
  20669. };
  20670. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20671. if ((key = (ecc_key *)XMALLOC(sizeof(*key), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER)) == NULL)
  20672. return MEMORY_E;
  20673. #endif
  20674. ret = wc_ecc_init_ex(key, HEAP_HINT, devId);
  20675. if (ret != 0) {
  20676. goto done;
  20677. }
  20678. key_inited = 1;
  20679. ret = wc_ecc_import_raw(key, QIUTx, QIUTy, dIUT, "SECP256R1");
  20680. if (ret != 0) {
  20681. goto done;
  20682. }
  20683. wc_ecc_set_flags(key, WC_ECC_FLAG_DEC_SIGN);
  20684. ret = wc_ecc_sign_set_k(k, sizeof(k), key);
  20685. if (ret != 0) {
  20686. goto done;
  20687. }
  20688. sigSz = sizeof(sig);
  20689. do {
  20690. #if defined(WOLFSSL_ASYNC_CRYPT)
  20691. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  20692. #endif
  20693. if (ret == 0)
  20694. ret = wc_ecc_sign_hash(hash, sizeof(hash), sig, &sigSz, rng, key);
  20695. } while (ret == WC_PENDING_E);
  20696. if (ret != 0) {
  20697. goto done;
  20698. }
  20699. TEST_SLEEP();
  20700. if (sigSz != sizeof(expSig)) {
  20701. ret = -9830;
  20702. goto done;
  20703. }
  20704. if (XMEMCMP(sig, expSig, sigSz) != 0) {
  20705. ret = -9831;
  20706. goto done;
  20707. }
  20708. sigSz = sizeof(sig);
  20709. do {
  20710. #if defined(WOLFSSL_ASYNC_CRYPT)
  20711. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  20712. #endif
  20713. if (ret == 0)
  20714. ret = wc_ecc_sign_hash(hash, sizeof(hash), sig, &sigSz, rng, key);
  20715. } while (ret == WC_PENDING_E);
  20716. if (ret != 0) {
  20717. goto done;
  20718. }
  20719. TEST_SLEEP();
  20720. done:
  20721. if (key_inited)
  20722. wc_ecc_free(key);
  20723. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20724. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20725. #endif
  20726. return ret;
  20727. }
  20728. #endif
  20729. #if defined(HAVE_ECC_CDH) && defined(HAVE_ECC_DHE)
  20730. static int ecc_test_cdh_vectors(WC_RNG* rng)
  20731. {
  20732. int ret;
  20733. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20734. ecc_key *pub_key = (ecc_key *)XMALLOC(sizeof *pub_key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20735. ecc_key *priv_key = (ecc_key *)XMALLOC(sizeof *priv_key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20736. #else
  20737. ecc_key pub_key[1], priv_key[1];
  20738. #endif
  20739. byte sharedA[32] = {0}, sharedB[32] = {0};
  20740. word32 x, z;
  20741. WOLFSSL_SMALL_STACK_STATIC const char* QCAVSx = "700c48f77f56584c5cc632ca65640db91b6bacce3a4df6b42ce7cc838833d287";
  20742. WOLFSSL_SMALL_STACK_STATIC const char* QCAVSy = "db71e509e3fd9b060ddb20ba5c51dcc5948d46fbf640dfe0441782cab85fa4ac";
  20743. WOLFSSL_SMALL_STACK_STATIC const char* dIUT = "7d7dc5f71eb29ddaf80d6214632eeae03d9058af1fb6d22ed80badb62bc1a534";
  20744. WOLFSSL_SMALL_STACK_STATIC const char* QIUTx = "ead218590119e8876b29146ff89ca61770c4edbbf97d38ce385ed281d8a6b230";
  20745. WOLFSSL_SMALL_STACK_STATIC const char* QIUTy = "28af61281fd35e2fa7002523acc85a429cb06ee6648325389f59edfce1405141";
  20746. WOLFSSL_SMALL_STACK_STATIC const char* ZIUT = "46fc62106420ff012e54a434fbdd2d25ccc5852060561e68040dd7778997bd7b";
  20747. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20748. if ((pub_key == NULL) ||
  20749. (priv_key == NULL)) {
  20750. ret = MEMORY_E;
  20751. goto done;
  20752. }
  20753. #endif
  20754. XMEMSET(pub_key, 0, sizeof *pub_key);
  20755. XMEMSET(priv_key, 0, sizeof *priv_key);
  20756. /* setup private and public keys */
  20757. ret = wc_ecc_init_ex(pub_key, HEAP_HINT, devId);
  20758. if (ret != 0)
  20759. goto done;
  20760. ret = wc_ecc_init_ex(priv_key, HEAP_HINT, devId);
  20761. if (ret != 0)
  20762. goto done;
  20763. wc_ecc_set_flags(pub_key, WC_ECC_FLAG_COFACTOR);
  20764. wc_ecc_set_flags(priv_key, WC_ECC_FLAG_COFACTOR);
  20765. ret = wc_ecc_import_raw(pub_key, QCAVSx, QCAVSy, NULL, "SECP256R1");
  20766. if (ret != 0)
  20767. goto done;
  20768. ret = wc_ecc_import_raw(priv_key, QIUTx, QIUTy, dIUT, "SECP256R1");
  20769. if (ret != 0)
  20770. goto done;
  20771. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  20772. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  20773. !defined(HAVE_SELFTEST)
  20774. ret = wc_ecc_set_rng(priv_key, rng);
  20775. if (ret != 0)
  20776. goto done;
  20777. #else
  20778. (void)rng;
  20779. #endif
  20780. /* compute ECC Cofactor shared secret */
  20781. x = sizeof(sharedA);
  20782. do {
  20783. #if defined(WOLFSSL_ASYNC_CRYPT)
  20784. ret = wc_AsyncWait(ret, &priv_key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  20785. #endif
  20786. if (ret == 0)
  20787. ret = wc_ecc_shared_secret(priv_key, pub_key, sharedA, &x);
  20788. } while (ret == WC_PENDING_E);
  20789. if (ret != 0) {
  20790. goto done;
  20791. }
  20792. TEST_SLEEP();
  20793. /* read in expected Z */
  20794. z = sizeof(sharedB);
  20795. ret = Base16_Decode((const byte*)ZIUT, (word32)XSTRLEN(ZIUT), sharedB, &z);
  20796. if (ret != 0)
  20797. goto done;
  20798. /* compare results */
  20799. if (x != z || XMEMCMP(sharedA, sharedB, x)) {
  20800. ERROR_OUT(-9840, done);
  20801. }
  20802. done:
  20803. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20804. if (priv_key) {
  20805. wc_ecc_free(priv_key);
  20806. XFREE(priv_key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20807. }
  20808. if (pub_key) {
  20809. wc_ecc_free(pub_key);
  20810. XFREE(pub_key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20811. }
  20812. #else
  20813. wc_ecc_free(priv_key);
  20814. wc_ecc_free(pub_key);
  20815. #endif
  20816. return ret;
  20817. }
  20818. #endif /* HAVE_ECC_CDH && HAVE_ECC_DHE */
  20819. #endif /* HAVE_ECC_VECTOR_TEST */
  20820. #ifdef HAVE_ECC_KEY_IMPORT
  20821. /* returns 0 on success */
  20822. static int ecc_test_make_pub(WC_RNG* rng)
  20823. {
  20824. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20825. ecc_key *key = (ecc_key *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20826. #if defined(HAVE_ECC_DHE) && defined(HAVE_ECC_KEY_EXPORT)
  20827. ecc_key *pub = (ecc_key *)XMALLOC(sizeof *pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20828. #endif
  20829. byte *exportBuf = (byte *)XMALLOC(ECC_BUFSIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20830. byte *tmp = (byte *)XMALLOC(ECC_BUFSIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20831. #else
  20832. ecc_key key[1];
  20833. #if defined(HAVE_ECC_DHE) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG) && \
  20834. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  20835. ecc_key pub[1];
  20836. #endif
  20837. byte exportBuf[ECC_BUFSIZE];
  20838. byte tmp[ECC_BUFSIZE];
  20839. #endif
  20840. const byte* msg = (const byte*)"test wolfSSL ECC public gen";
  20841. word32 x;
  20842. word32 tmpSz;
  20843. int ret = 0;
  20844. ecc_point* pubPoint = NULL;
  20845. #ifdef HAVE_ECC_VERIFY
  20846. int verify = 0;
  20847. #endif
  20848. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20849. if ((key == NULL) ||
  20850. #if defined(HAVE_ECC_DHE) && defined(HAVE_ECC_KEY_EXPORT)
  20851. (pub == NULL) ||
  20852. #endif
  20853. (exportBuf == NULL) ||
  20854. (tmp == NULL))
  20855. ERROR_OUT(MEMORY_E, done);
  20856. #endif
  20857. (void)msg;
  20858. (void)verify;
  20859. (void)exportBuf;
  20860. (void)rng;
  20861. wc_ecc_init_ex(key, HEAP_HINT, devId);
  20862. #ifndef NO_ECC256
  20863. #ifdef USE_CERT_BUFFERS_256
  20864. XMEMCPY(tmp, ecc_key_der_256, (size_t)sizeof_ecc_key_der_256);
  20865. tmpSz = (size_t)sizeof_ecc_key_der_256;
  20866. #else
  20867. {
  20868. XFILE file = XFOPEN(eccKeyDerFile, "rb");
  20869. if (!file) {
  20870. ERROR_OUT(-9850, done);
  20871. }
  20872. tmpSz = (word32)XFREAD(tmp, 1, ECC_BUFSIZE, file);
  20873. XFCLOSE(file);
  20874. }
  20875. #endif /* USE_CERT_BUFFERS_256 */
  20876. /* import private only then test with */
  20877. ret = wc_ecc_import_private_key(tmp, tmpSz, NULL, 0, NULL);
  20878. if (ret == 0) {
  20879. ERROR_OUT(-9851, done);
  20880. }
  20881. ret = wc_ecc_import_private_key(NULL, tmpSz, NULL, 0, key);
  20882. if (ret == 0) {
  20883. ERROR_OUT(-9852, done);
  20884. }
  20885. x = 0;
  20886. ret = wc_EccPrivateKeyDecode(tmp, &x, key, tmpSz);
  20887. if (ret != 0) {
  20888. ERROR_OUT(-9853, done);
  20889. }
  20890. #ifdef HAVE_ECC_KEY_EXPORT
  20891. x = ECC_BUFSIZE;
  20892. ret = wc_ecc_export_private_only(key, exportBuf, &x);
  20893. if (ret != 0) {
  20894. ERROR_OUT(-9854, done);
  20895. }
  20896. /* make private only key */
  20897. wc_ecc_free(key);
  20898. wc_ecc_init_ex(key, HEAP_HINT, devId);
  20899. ret = wc_ecc_import_private_key(exportBuf, x, NULL, 0, key);
  20900. if (ret != 0) {
  20901. ERROR_OUT(-9855, done);
  20902. }
  20903. x = ECC_BUFSIZE;
  20904. ret = wc_ecc_export_x963_ex(key, exportBuf, &x, 0);
  20905. if (ret == 0) {
  20906. ERROR_OUT(-9856, done);
  20907. }
  20908. #endif /* HAVE_ECC_KEY_EXPORT */
  20909. ret = wc_ecc_make_pub(NULL, NULL);
  20910. if (ret == 0) {
  20911. ERROR_OUT(-9857, done);
  20912. }
  20913. TEST_SLEEP();
  20914. #ifndef WOLFSSL_NO_MALLOC
  20915. pubPoint = wc_ecc_new_point_h(HEAP_HINT);
  20916. if (pubPoint == NULL) {
  20917. ERROR_OUT(-9858, done);
  20918. }
  20919. #if !defined(WOLFSSL_CRYPTOCELL)
  20920. ret = wc_ecc_make_pub(key, pubPoint);
  20921. #if defined(WOLFSSL_ASYNC_CRYPT)
  20922. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  20923. #endif
  20924. if (ret != 0) {
  20925. ERROR_OUT(-9859, done);
  20926. }
  20927. #endif
  20928. TEST_SLEEP();
  20929. #ifdef HAVE_ECC_KEY_EXPORT
  20930. /* export should still fail, is private only key */
  20931. x = ECC_BUFSIZE;
  20932. ret = wc_ecc_export_x963_ex(key, exportBuf, &x, 0);
  20933. if (ret == 0) {
  20934. ERROR_OUT(-9860, done);
  20935. }
  20936. #endif /* HAVE_ECC_KEY_EXPORT */
  20937. #endif /* !WOLFSSL_NO_MALLOC */
  20938. #endif /* !NO_ECC256 */
  20939. /* create a new key since above test for loading key is not supported */
  20940. #if defined(WOLFSSL_CRYPTOCELL) || defined(NO_ECC256) || \
  20941. defined(WOLFSSL_QNX_CAAM) || defined(WOLFSSL_SE050) || \
  20942. defined(WOLFSSL_SECO_CAAM)
  20943. ret = wc_ecc_make_key(rng, ECC_KEYGEN_SIZE, key);
  20944. if (ret != 0) {
  20945. ERROR_OUT(-9861, done);
  20946. }
  20947. #endif
  20948. #if defined(HAVE_ECC_SIGN) && (!defined(ECC_TIMING_RESISTANT) || \
  20949. (defined(ECC_TIMING_RESISTANT) && !defined(WC_NO_RNG))) && \
  20950. !defined(WOLF_CRYPTO_CB_ONLY_ECC) && !defined(HAVE_ECC_DETERMINISTIC_K)
  20951. tmpSz = ECC_BUFSIZE;
  20952. ret = 0;
  20953. do {
  20954. #if defined(WOLFSSL_ASYNC_CRYPT)
  20955. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  20956. #endif
  20957. if (ret == 0) {
  20958. ret = wc_ecc_sign_hash(msg, (word32)XSTRLEN((const char* )msg), tmp,
  20959. &tmpSz, rng, key);
  20960. }
  20961. } while (ret == WC_PENDING_E);
  20962. if (ret != 0) {
  20963. ERROR_OUT(-9862, done);
  20964. }
  20965. TEST_SLEEP();
  20966. #ifdef HAVE_ECC_VERIFY
  20967. /* try verify with private only key */
  20968. ret = 0;
  20969. do {
  20970. #if defined(WOLFSSL_ASYNC_CRYPT)
  20971. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  20972. #endif
  20973. if (ret == 0) {
  20974. ret = wc_ecc_verify_hash(tmp, tmpSz, msg,
  20975. (word32)XSTRLEN((const char*)msg), &verify, key);
  20976. }
  20977. } while (ret == WC_PENDING_E);
  20978. if (ret != 0) {
  20979. ERROR_OUT(-9863, done);
  20980. }
  20981. if (verify != 1) {
  20982. ERROR_OUT(-9864, done);
  20983. }
  20984. TEST_SLEEP();
  20985. #ifdef HAVE_ECC_KEY_EXPORT
  20986. /* exporting the public part should now work */
  20987. x = ECC_BUFSIZE;
  20988. ret = wc_ecc_export_x963_ex(key, exportBuf, &x, 0);
  20989. if (ret != 0) {
  20990. ERROR_OUT(-9865, done);
  20991. }
  20992. #endif /* HAVE_ECC_KEY_EXPORT */
  20993. #endif /* HAVE_ECC_VERIFY */
  20994. #endif /* HAVE_ECC_SIGN */
  20995. #if defined(HAVE_ECC_DHE) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  20996. /* now test private only key with creating a shared secret */
  20997. x = ECC_BUFSIZE;
  20998. ret = wc_ecc_export_private_only(key, exportBuf, &x);
  20999. if (ret != 0) {
  21000. ERROR_OUT(-9866, done);
  21001. }
  21002. #if !defined(WOLFSSL_QNX_CAAM) && !defined(WOLFSSL_SE050)
  21003. /* make private only key */
  21004. wc_ecc_free(key);
  21005. wc_ecc_init_ex(key, HEAP_HINT, devId);
  21006. ret = wc_ecc_import_private_key(exportBuf, x, NULL, 0, key);
  21007. if (ret != 0) {
  21008. ERROR_OUT(-9867, done);
  21009. }
  21010. /* check that public export fails with private only key */
  21011. x = ECC_BUFSIZE;
  21012. ret = wc_ecc_export_x963_ex(key, exportBuf, &x, 0);
  21013. if (ret == 0) {
  21014. ERROR_OUT(-9868, done);
  21015. }
  21016. #endif /* WOLFSSL_QNX_CAAM */
  21017. #ifndef WOLF_CRYPTO_CB_ONLY_ECC
  21018. /* make public key for shared secret */
  21019. wc_ecc_init_ex(pub, HEAP_HINT, devId);
  21020. ret = wc_ecc_make_key(rng, ECC_KEYGEN_SIZE, pub);
  21021. #ifdef HAVE_ECC_CDH
  21022. wc_ecc_set_flags(key, WC_ECC_FLAG_COFACTOR);
  21023. #endif
  21024. #if defined(WOLFSSL_ASYNC_CRYPT)
  21025. ret = wc_AsyncWait(ret, &pub->asyncDev, WC_ASYNC_FLAG_NONE);
  21026. #endif
  21027. if (ret != 0) {
  21028. ERROR_OUT(-9869, done);
  21029. }
  21030. TEST_SLEEP();
  21031. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  21032. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  21033. !defined(HAVE_SELFTEST)
  21034. ret = wc_ecc_set_rng(key, rng);
  21035. if (ret != 0)
  21036. goto done;
  21037. #endif
  21038. x = ECC_BUFSIZE;
  21039. do {
  21040. #if defined(WOLFSSL_ASYNC_CRYPT)
  21041. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  21042. #endif
  21043. if (ret == 0) {
  21044. ret = wc_ecc_shared_secret(key, pub, exportBuf, &x);
  21045. }
  21046. } while (ret == WC_PENDING_E);
  21047. wc_ecc_free(pub);
  21048. if (ret != 0) {
  21049. ERROR_OUT(-9870, done);
  21050. }
  21051. TEST_SLEEP();
  21052. #endif /* HAVE_ECC_DHE && HAVE_ECC_KEY_EXPORT && !WC_NO_RNG */
  21053. #endif /* WOLF_CRYPTO_CB_ONLY_ECC */
  21054. ret = 0;
  21055. done:
  21056. wc_ecc_del_point_h(pubPoint, HEAP_HINT);
  21057. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21058. if (key != NULL) {
  21059. wc_ecc_free(key);
  21060. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21061. }
  21062. #if defined(HAVE_ECC_DHE) && defined(HAVE_ECC_KEY_EXPORT)
  21063. if (pub != NULL)
  21064. XFREE(pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21065. #endif
  21066. if (exportBuf != NULL)
  21067. XFREE(exportBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21068. if (tmp != NULL)
  21069. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21070. #else
  21071. wc_ecc_free(key);
  21072. #endif
  21073. return ret;
  21074. }
  21075. #if defined(HAVE_ECC_KEY_EXPORT) && !defined(NO_ASN_CRYPT) && \
  21076. !defined(WC_NO_RNG) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  21077. static int ecc_test_key_decode(WC_RNG* rng, int keySize)
  21078. {
  21079. int ret;
  21080. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21081. ecc_key *eccKey = (ecc_key *)XMALLOC(sizeof *eccKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21082. byte *tmpBuf = (byte *)XMALLOC(ECC_BUFSIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21083. #else
  21084. ecc_key eccKey[1];
  21085. byte tmpBuf[ECC_BUFSIZE];
  21086. #endif
  21087. word32 tmpSz;
  21088. word32 idx;
  21089. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21090. if ((eccKey == NULL) || (tmpBuf == NULL))
  21091. ERROR_OUT(MEMORY_E, done);
  21092. #endif
  21093. ret = wc_ecc_init(eccKey);
  21094. if (ret != 0) {
  21095. goto done;
  21096. }
  21097. ret = wc_ecc_make_key(rng, keySize, eccKey);
  21098. #if defined(WOLFSSL_ASYNC_CRYPT)
  21099. ret = wc_AsyncWait(ret, &eccKey->asyncDev, WC_ASYNC_FLAG_NONE);
  21100. #endif
  21101. if (ret != 0) {
  21102. goto done;
  21103. }
  21104. tmpSz = ECC_BUFSIZE;
  21105. ret = wc_EccKeyToDer(eccKey, tmpBuf, tmpSz);
  21106. wc_ecc_free(eccKey);
  21107. if (ret < 0) {
  21108. goto done;
  21109. }
  21110. tmpSz = ret;
  21111. ret = wc_ecc_init(eccKey);
  21112. if (ret != 0) {
  21113. goto done;
  21114. }
  21115. idx = 0;
  21116. ret = wc_EccPrivateKeyDecode(tmpBuf, &idx, eccKey, tmpSz);
  21117. if (ret != 0) {
  21118. goto done;
  21119. }
  21120. wc_ecc_free(eccKey);
  21121. ret = wc_ecc_init(eccKey);
  21122. if (ret != 0) {
  21123. goto done;
  21124. }
  21125. idx = 0;
  21126. ret = wc_EccPublicKeyDecode(tmpBuf, &idx, eccKey, tmpSz);
  21127. if (ret != 0) {
  21128. goto done;
  21129. }
  21130. ret = 0;
  21131. done:
  21132. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21133. if (eccKey != NULL) {
  21134. wc_ecc_free(eccKey);
  21135. XFREE(eccKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21136. }
  21137. if (tmpBuf != NULL)
  21138. XFREE(tmpBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21139. #else
  21140. wc_ecc_free(eccKey);
  21141. #endif
  21142. return ret;
  21143. }
  21144. #endif /* HAVE_ECC_KEY_EXPORT && !NO_ASN_CRYPT */
  21145. #endif /* HAVE_ECC_KEY_IMPORT */
  21146. #if defined(HAVE_ECC_KEY_EXPORT) && !defined(NO_ASN_CRYPT) && \
  21147. !defined(WC_NO_RNG) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  21148. static int ecc_test_key_gen(WC_RNG* rng, int keySize)
  21149. {
  21150. int ret = 0;
  21151. int derSz;
  21152. #ifdef HAVE_PKCS8
  21153. word32 pkcs8Sz;
  21154. #endif
  21155. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21156. byte *der = (byte *)XMALLOC(ECC_BUFSIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21157. ecc_key *userA = (ecc_key *)XMALLOC(sizeof *userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21158. #else
  21159. byte der[ECC_BUFSIZE];
  21160. ecc_key userA[1];
  21161. #endif
  21162. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21163. if ((der == NULL) || (userA == NULL))
  21164. ERROR_OUT(MEMORY_E, done);
  21165. #endif
  21166. ret = wc_ecc_init_ex(userA, HEAP_HINT, devId);
  21167. if (ret != 0)
  21168. goto done;
  21169. ret = wc_ecc_make_key(rng, keySize, userA);
  21170. #if defined(WOLFSSL_ASYNC_CRYPT)
  21171. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_NONE);
  21172. #endif
  21173. if (ret != 0)
  21174. goto done;
  21175. TEST_SLEEP();
  21176. ret = wc_ecc_check_key(userA);
  21177. if (ret != 0)
  21178. goto done;
  21179. TEST_SLEEP();
  21180. derSz = wc_EccKeyToDer(userA, der, ECC_BUFSIZE);
  21181. if (derSz < 0) {
  21182. ERROR_OUT(derSz, done);
  21183. }
  21184. ret = SaveDerAndPem(der, derSz, eccCaKeyTempFile, eccCaKeyPemFile,
  21185. ECC_PRIVATEKEY_TYPE, -8347);
  21186. if (ret != 0) {
  21187. goto done;
  21188. }
  21189. /* test export of public key */
  21190. derSz = wc_EccPublicKeyToDer(userA, der, ECC_BUFSIZE, 1);
  21191. if (derSz < 0) {
  21192. ERROR_OUT(derSz, done);
  21193. }
  21194. if (derSz == 0) {
  21195. ERROR_OUT(-9890, done);
  21196. }
  21197. #ifdef HAVE_COMP_KEY
  21198. /* test export of compressed public key */
  21199. derSz = wc_EccPublicKeyToDer_ex(userA, der, ECC_BUFSIZE, 1, 1);
  21200. if (derSz < 0) {
  21201. ERROR_OUT(derSz, done);
  21202. }
  21203. if (derSz == 0) {
  21204. ERROR_OUT(-9890, done);
  21205. }
  21206. #endif
  21207. ret = SaveDerAndPem(der, derSz, eccPubKeyDerFile, NULL, 0, -8348);
  21208. if (ret != 0) {
  21209. goto done;
  21210. }
  21211. #ifdef HAVE_PKCS8
  21212. /* test export of PKCS#8 unencrypted private key */
  21213. pkcs8Sz = FOURK_BUF;
  21214. derSz = wc_EccPrivateKeyToPKCS8(userA, der, &pkcs8Sz);
  21215. if (derSz < 0) {
  21216. ERROR_OUT(derSz, done);
  21217. }
  21218. if (derSz == 0) {
  21219. ERROR_OUT(-9891, done);
  21220. }
  21221. ret = SaveDerAndPem(der, derSz, eccPkcs8KeyDerFile, NULL, 0, -8349);
  21222. if (ret != 0) {
  21223. goto done;
  21224. }
  21225. #endif /* HAVE_PKCS8 */
  21226. done:
  21227. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21228. if (der != NULL)
  21229. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21230. if (userA != NULL) {
  21231. wc_ecc_free(userA);
  21232. XFREE(userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21233. }
  21234. #else
  21235. wc_ecc_free(userA);
  21236. #endif
  21237. return ret;
  21238. }
  21239. #endif /* HAVE_ECC_KEY_EXPORT && !NO_ASN_CRYPT */
  21240. static int ecc_test_curve_size(WC_RNG* rng, int keySize, int testVerifyCount,
  21241. int curve_id, const ecc_set_type* dp)
  21242. {
  21243. #if defined(HAVE_ECC_DHE) && !defined(WC_NO_RNG) && \
  21244. !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A)
  21245. WC_DECLARE_VAR(sharedA, byte, ECC_SHARED_SIZE, HEAP_HINT);
  21246. WC_DECLARE_VAR(sharedB, byte, ECC_SHARED_SIZE, HEAP_HINT);
  21247. word32 y;
  21248. #endif
  21249. #ifdef HAVE_ECC_KEY_EXPORT
  21250. #define ECC_KEY_EXPORT_BUF_SIZE (MAX_ECC_BYTES * 2 + 32)
  21251. WC_DECLARE_VAR(exportBuf, byte, ECC_KEY_EXPORT_BUF_SIZE, HEAP_HINT);
  21252. #endif
  21253. word32 x = 0;
  21254. #if defined(HAVE_ECC_SIGN) && !defined(WOLFSSL_KCAPI_ECC)
  21255. WC_DECLARE_VAR(sig, byte, ECC_SIG_SIZE, HEAP_HINT);
  21256. WC_DECLARE_VAR(digest, byte, ECC_DIGEST_SIZE, HEAP_HINT);
  21257. int i;
  21258. #ifdef HAVE_ECC_VERIFY
  21259. int verify;
  21260. #endif /* HAVE_ECC_VERIFY */
  21261. #endif /* HAVE_ECC_SIGN */
  21262. int ret;
  21263. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21264. ecc_key *userA = (ecc_key *)XMALLOC(sizeof *userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21265. ecc_key *userB = (ecc_key *)XMALLOC(sizeof *userB, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21266. ecc_key *pubKey = (ecc_key *)XMALLOC(sizeof *pubKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21267. #else
  21268. ecc_key userA[1];
  21269. ecc_key userB[1];
  21270. ecc_key pubKey[1];
  21271. #endif
  21272. #ifndef WC_NO_RNG
  21273. int curveSize;
  21274. #endif
  21275. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  21276. #if (defined(HAVE_ECC_DHE) || defined(HAVE_ECC_CDH)) && !defined(WC_NO_RNG) && \
  21277. !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A)
  21278. if (sharedA == NULL || sharedB == NULL)
  21279. ERROR_OUT(-9900, done);
  21280. #endif
  21281. #ifdef HAVE_ECC_KEY_EXPORT
  21282. if (exportBuf == NULL)
  21283. ERROR_OUT(-9901, done);
  21284. #endif
  21285. #if defined(HAVE_ECC_SIGN) && !defined(WOLFSSL_KCAPI_ECC)
  21286. if (sig == NULL || digest == NULL)
  21287. ERROR_OUT(-9902, done);
  21288. #endif
  21289. #endif /* WOLFSSL_SMALL_STACK && !WOLFSSL_NO_MALLOC */
  21290. (void)testVerifyCount;
  21291. (void)dp;
  21292. (void)x;
  21293. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21294. if ((userA == NULL) ||
  21295. (userB == NULL) ||
  21296. (pubKey == NULL))
  21297. ERROR_OUT(-9903, done);
  21298. #endif
  21299. XMEMSET(userA, 0, sizeof *userA);
  21300. XMEMSET(userB, 0, sizeof *userB);
  21301. XMEMSET(pubKey, 0, sizeof *pubKey);
  21302. ret = wc_ecc_init_ex(userA, HEAP_HINT, devId);
  21303. if (ret != 0)
  21304. ERROR_OUT(-9904, done);
  21305. ret = wc_ecc_init_ex(userB, HEAP_HINT, devId);
  21306. if (ret != 0)
  21307. ERROR_OUT(-9905, done);
  21308. ret = wc_ecc_init_ex(pubKey, HEAP_HINT, devId);
  21309. if (ret != 0)
  21310. ERROR_OUT(-9906, done);
  21311. #ifdef WOLFSSL_CUSTOM_CURVES
  21312. if (dp != NULL) {
  21313. ret = wc_ecc_set_custom_curve(userA, dp);
  21314. if (ret != 0)
  21315. ERROR_OUT(-9907, done);
  21316. ret = wc_ecc_set_custom_curve(userB, dp);
  21317. if (ret != 0)
  21318. ERROR_OUT(-9908, done);
  21319. }
  21320. #endif
  21321. #ifndef WC_NO_RNG
  21322. ret = wc_ecc_make_key_ex(rng, keySize, userA, curve_id);
  21323. #if defined(WOLFSSL_ASYNC_CRYPT)
  21324. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_NONE);
  21325. #endif
  21326. #ifdef WOLF_CRYPTO_CB_ONLY_ECC
  21327. if (ret == NO_VALID_DEVID) {
  21328. ret = 0;
  21329. goto done; /* no software case */
  21330. }
  21331. #endif
  21332. if (ret == ECC_CURVE_OID_E)
  21333. goto done; /* catch case, where curve is not supported */
  21334. if (ret != 0)
  21335. ERROR_OUT(-9910, done);
  21336. TEST_SLEEP();
  21337. if (wc_ecc_get_curve_idx(curve_id) != -1) {
  21338. curveSize = wc_ecc_get_curve_size_from_id(userA->dp->id);
  21339. if (curveSize != userA->dp->size)
  21340. ERROR_OUT(-9911, done);
  21341. }
  21342. ret = wc_ecc_check_key(userA);
  21343. if (ret != 0)
  21344. ERROR_OUT(-9912, done);
  21345. TEST_SLEEP();
  21346. /* ATECC508/608 configuration may not support more than one ECDH key */
  21347. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A)
  21348. ret = wc_ecc_make_key_ex(rng, keySize, userB, curve_id);
  21349. #if defined(WOLFSSL_ASYNC_CRYPT)
  21350. ret = wc_AsyncWait(ret, &userB->asyncDev, WC_ASYNC_FLAG_NONE);
  21351. #endif
  21352. if (ret != 0)
  21353. ERROR_OUT(-9914, done);
  21354. TEST_SLEEP();
  21355. /* only perform the below tests if the key size matches */
  21356. if (dp == NULL && keySize > 0 && wc_ecc_size(userA) != keySize)
  21357. ERROR_OUT(ECC_CURVE_OID_E, done);
  21358. #ifdef HAVE_ECC_DHE
  21359. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  21360. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  21361. !defined(HAVE_SELFTEST)
  21362. ret = wc_ecc_set_rng(userA, rng);
  21363. if (ret != 0)
  21364. ERROR_OUT(-9915, done);
  21365. ret = wc_ecc_set_rng(userB, rng);
  21366. if (ret != 0)
  21367. ERROR_OUT(-9916, done);
  21368. #endif
  21369. x = ECC_SHARED_SIZE;
  21370. do {
  21371. #if defined(WOLFSSL_ASYNC_CRYPT)
  21372. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  21373. #endif
  21374. if (ret == 0)
  21375. ret = wc_ecc_shared_secret(userA, userB, sharedA, &x);
  21376. } while (ret == WC_PENDING_E);
  21377. if (ret != 0) {
  21378. ERROR_OUT(-9917, done);
  21379. }
  21380. TEST_SLEEP();
  21381. y = ECC_SHARED_SIZE;
  21382. do {
  21383. #if defined(WOLFSSL_ASYNC_CRYPT)
  21384. ret = wc_AsyncWait(ret, &userB->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  21385. #endif
  21386. if (ret == 0)
  21387. ret = wc_ecc_shared_secret(userB, userA, sharedB, &y);
  21388. } while (ret == WC_PENDING_E);
  21389. if (ret != 0)
  21390. ERROR_OUT(-9918, done);
  21391. if (y != x)
  21392. ERROR_OUT(-9919, done);
  21393. if (XMEMCMP(sharedA, sharedB, x))
  21394. ERROR_OUT(-9920, done);
  21395. TEST_SLEEP();
  21396. #ifdef HAVE_ECC_CDH
  21397. /* add cofactor flag */
  21398. wc_ecc_set_flags(userA, WC_ECC_FLAG_COFACTOR);
  21399. wc_ecc_set_flags(userB, WC_ECC_FLAG_COFACTOR);
  21400. x = ECC_SHARED_SIZE;
  21401. do {
  21402. #if defined(WOLFSSL_ASYNC_CRYPT)
  21403. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  21404. #endif
  21405. if (ret == 0)
  21406. ret = wc_ecc_shared_secret(userA, userB, sharedA, &x);
  21407. } while (ret == WC_PENDING_E);
  21408. if (ret != 0)
  21409. ERROR_OUT(-9921, done);
  21410. TEST_SLEEP();
  21411. y = ECC_SHARED_SIZE;
  21412. do {
  21413. #if defined(WOLFSSL_ASYNC_CRYPT)
  21414. ret = wc_AsyncWait(ret, &userB->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  21415. #endif
  21416. if (ret == 0)
  21417. ret = wc_ecc_shared_secret(userB, userA, sharedB, &y);
  21418. } while (ret == WC_PENDING_E);
  21419. if (ret != 0)
  21420. ERROR_OUT(-9922, done);
  21421. if (y != x)
  21422. ERROR_OUT(-9923, done);
  21423. if (XMEMCMP(sharedA, sharedB, x))
  21424. ERROR_OUT(-9924, done);
  21425. TEST_SLEEP();
  21426. /* remove cofactor flag */
  21427. wc_ecc_set_flags(userA, 0);
  21428. wc_ecc_set_flags(userB, 0);
  21429. #endif /* HAVE_ECC_CDH */
  21430. #endif /* HAVE_ECC_DHE */
  21431. #endif /* !WOLFSSL_ATECC508A && WOLFSSL_ATECC608A */
  21432. #ifdef HAVE_ECC_KEY_EXPORT
  21433. x = ECC_KEY_EXPORT_BUF_SIZE;
  21434. ret = wc_ecc_export_x963_ex(userA, exportBuf, &x, 0);
  21435. if (ret != 0)
  21436. ERROR_OUT(-9925, done);
  21437. #ifdef HAVE_ECC_KEY_IMPORT
  21438. #ifdef WOLFSSL_CUSTOM_CURVES
  21439. if (dp != NULL) {
  21440. ret = wc_ecc_set_custom_curve(pubKey, dp);
  21441. if (ret != 0)
  21442. ERROR_OUT(-9926, done);
  21443. }
  21444. #endif
  21445. ret = wc_ecc_import_x963_ex(exportBuf, x, pubKey, curve_id);
  21446. if (ret != 0)
  21447. ERROR_OUT(-9927, done);
  21448. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A)
  21449. #ifdef HAVE_ECC_DHE
  21450. y = ECC_SHARED_SIZE;
  21451. do {
  21452. #if defined(WOLFSSL_ASYNC_CRYPT)
  21453. ret = wc_AsyncWait(ret, &userB->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  21454. #endif
  21455. if (ret == 0)
  21456. ret = wc_ecc_shared_secret(userB, pubKey, sharedB, &y);
  21457. } while (ret == WC_PENDING_E);
  21458. if (ret != 0)
  21459. ERROR_OUT(-9928, done);
  21460. if (XMEMCMP(sharedA, sharedB, y))
  21461. ERROR_OUT(-9929, done);
  21462. TEST_SLEEP();
  21463. #endif /* HAVE_ECC_DHE */
  21464. #ifdef HAVE_COMP_KEY
  21465. /* try compressed export / import too */
  21466. x = ECC_KEY_EXPORT_BUF_SIZE;
  21467. ret = wc_ecc_export_x963_ex(userA, exportBuf, &x, 1);
  21468. if (ret != 0)
  21469. ERROR_OUT(-9930, done);
  21470. wc_ecc_free(pubKey);
  21471. ret = wc_ecc_init_ex(pubKey, HEAP_HINT, devId);
  21472. if (ret != 0)
  21473. ERROR_OUT(-9931, done);
  21474. #ifdef WOLFSSL_CUSTOM_CURVES
  21475. if (dp != NULL) {
  21476. ret = wc_ecc_set_custom_curve(pubKey, dp);
  21477. if (ret != 0)
  21478. ERROR_OUT(-9932, done);
  21479. }
  21480. #endif
  21481. ret = wc_ecc_import_x963_ex(exportBuf, x, pubKey, curve_id);
  21482. if (ret != 0)
  21483. ERROR_OUT(-9933, done);
  21484. #ifdef HAVE_ECC_DHE
  21485. y = ECC_SHARED_SIZE;
  21486. do {
  21487. #if defined(WOLFSSL_ASYNC_CRYPT)
  21488. ret = wc_AsyncWait(ret, &userB->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  21489. #endif
  21490. if (ret == 0)
  21491. ret = wc_ecc_shared_secret(userB, pubKey, sharedB, &y);
  21492. } while (ret == WC_PENDING_E);
  21493. if (ret != 0)
  21494. ERROR_OUT(-9934, done);
  21495. if (XMEMCMP(sharedA, sharedB, y))
  21496. ERROR_OUT(-9935, done);
  21497. TEST_SLEEP();
  21498. #endif /* HAVE_ECC_DHE */
  21499. #endif /* HAVE_COMP_KEY */
  21500. #endif /* !WOLFSSL_ATECC508A && !WOLFSSL_ATECC608A */
  21501. #endif /* !WC_NO_RNG */
  21502. #endif /* HAVE_ECC_KEY_IMPORT */
  21503. #endif /* HAVE_ECC_KEY_EXPORT */
  21504. /* For KCAPI cannot sign using generated ECDH key */
  21505. #if !defined(ECC_TIMING_RESISTANT) || (defined(ECC_TIMING_RESISTANT) && \
  21506. !defined(WC_NO_RNG) && !defined(WOLFSSL_KCAPI_ECC))
  21507. #ifdef HAVE_ECC_SIGN
  21508. /* ECC w/out Shamir has issue with all 0 digest */
  21509. /* WC_BIGINT doesn't have 0 len well on hardware */
  21510. /* Cryptocell has issues with all 0 digest */
  21511. #if defined(ECC_SHAMIR) && !defined(WOLFSSL_ASYNC_CRYPT) && \
  21512. !defined(WOLFSSL_CRYPTOCELL)
  21513. /* test DSA sign hash with zeros */
  21514. for (i = 0; i < (int)ECC_DIGEST_SIZE; i++) {
  21515. digest[i] = 0;
  21516. }
  21517. x = ECC_SIG_SIZE;
  21518. do {
  21519. #if defined(WOLFSSL_ASYNC_CRYPT)
  21520. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  21521. #endif
  21522. if (ret == 0)
  21523. ret = wc_ecc_sign_hash(digest, ECC_DIGEST_SIZE, sig, &x, rng,
  21524. userA);
  21525. } while (ret == WC_PENDING_E);
  21526. if (ret != 0)
  21527. ERROR_OUT(-9936, done);
  21528. TEST_SLEEP();
  21529. #ifdef HAVE_ECC_VERIFY
  21530. for (i=0; i<testVerifyCount; i++) {
  21531. verify = 0;
  21532. do {
  21533. #if defined(WOLFSSL_ASYNC_CRYPT)
  21534. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  21535. #endif
  21536. if (ret == 0)
  21537. ret = wc_ecc_verify_hash(sig, x, digest, ECC_DIGEST_SIZE,
  21538. &verify, userA);
  21539. } while (ret == WC_PENDING_E);
  21540. if (ret != 0)
  21541. ERROR_OUT(-9937, done);
  21542. if (verify != 1)
  21543. ERROR_OUT(-9938, done);
  21544. TEST_SLEEP();
  21545. }
  21546. #endif /* HAVE_ECC_VERIFY */
  21547. #endif /* ECC_SHAMIR && !WOLFSSL_ASYNC_CRYPT && !WOLFSSL_CRYPTOCELL */
  21548. /* test DSA sign hash with sequence (0,1,2,3,4,...) */
  21549. for (i = 0; i < (int)ECC_DIGEST_SIZE; i++) {
  21550. digest[i] = (byte)i;
  21551. }
  21552. x = ECC_SIG_SIZE;
  21553. do {
  21554. #if defined(WOLFSSL_ASYNC_CRYPT)
  21555. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  21556. #endif
  21557. if (ret == 0)
  21558. ret = wc_ecc_sign_hash(digest, ECC_DIGEST_SIZE, sig, &x, rng, userA);
  21559. } while (ret == WC_PENDING_E);
  21560. if (ret != 0)
  21561. ERROR_OUT(-9939, done);
  21562. TEST_SLEEP();
  21563. #ifdef HAVE_ECC_VERIFY
  21564. for (i=0; i<testVerifyCount; i++) {
  21565. verify = 0;
  21566. do {
  21567. #if defined(WOLFSSL_ASYNC_CRYPT)
  21568. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  21569. #endif
  21570. if (ret == 0)
  21571. ret = wc_ecc_verify_hash(sig, x, digest, ECC_DIGEST_SIZE, &verify, userA);
  21572. } while (ret == WC_PENDING_E);
  21573. if (ret != 0)
  21574. ERROR_OUT(-9940, done);
  21575. if (verify != 1)
  21576. ERROR_OUT(-9941, done);
  21577. TEST_SLEEP();
  21578. }
  21579. #endif /* HAVE_ECC_VERIFY */
  21580. #endif /* HAVE_ECC_SIGN */
  21581. #endif /* !ECC_TIMING_RESISTANT || (ECC_TIMING_RESISTANT &&
  21582. * !WC_NO_RNG && !WOLFSSL_KCAPI_ECC) */
  21583. #if defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG) && \
  21584. !defined(WOLFSSL_ATECC508) && !defined(WOLFSSL_ATECC608A) && \
  21585. !defined(WOLFSSL_KCAPI_ECC)
  21586. x = ECC_KEY_EXPORT_BUF_SIZE;
  21587. ret = wc_ecc_export_private_only(userA, exportBuf, &x);
  21588. if (ret != 0)
  21589. ERROR_OUT(-9942, done);
  21590. #elif defined(HAVE_ECC_KEY_EXPORT)
  21591. (void)exportBuf;
  21592. #endif /* HAVE_ECC_KEY_EXPORT */
  21593. done:
  21594. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21595. if (userA != NULL) {
  21596. wc_ecc_free(userA);
  21597. XFREE(userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21598. }
  21599. if (userB != NULL) {
  21600. wc_ecc_free(userB);
  21601. XFREE(userB, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21602. }
  21603. if (pubKey != NULL) {
  21604. wc_ecc_free(pubKey);
  21605. XFREE(pubKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21606. }
  21607. #else
  21608. wc_ecc_free(pubKey);
  21609. wc_ecc_free(userB);
  21610. wc_ecc_free(userA);
  21611. #endif
  21612. #if defined(HAVE_ECC_DHE) || defined(HAVE_ECC_CDH)
  21613. WC_FREE_VAR(sharedA, HEAP_HINT);
  21614. WC_FREE_VAR(sharedB, HEAP_HINT);
  21615. #endif
  21616. #ifdef HAVE_ECC_KEY_EXPORT
  21617. WC_FREE_VAR(exportBuf, HEAP_HINT);
  21618. #endif
  21619. #ifdef HAVE_ECC_SIGN
  21620. WC_FREE_VAR(sig, HEAP_HINT);
  21621. WC_FREE_VAR(digest, HEAP_HINT);
  21622. #endif
  21623. (void)keySize;
  21624. (void)curve_id;
  21625. (void)rng;
  21626. return ret;
  21627. }
  21628. #undef ECC_TEST_VERIFY_COUNT
  21629. #define ECC_TEST_VERIFY_COUNT 2
  21630. static int ecc_test_curve(WC_RNG* rng, int keySize)
  21631. {
  21632. int ret;
  21633. ret = ecc_test_curve_size(rng, keySize, ECC_TEST_VERIFY_COUNT,
  21634. ECC_CURVE_DEF, NULL);
  21635. if (ret < 0) {
  21636. if (ret == ECC_CURVE_OID_E) {
  21637. /* ignore error for curves not found */
  21638. /* some curve sizes are only available with:
  21639. HAVE_ECC_SECPR2, HAVE_ECC_SECPR3, HAVE_ECC_BRAINPOOL
  21640. and HAVE_ECC_KOBLITZ */
  21641. }
  21642. else {
  21643. printf("ecc_test_curve_size %d failed!: %d\n", keySize, ret);
  21644. return ret;
  21645. }
  21646. }
  21647. #ifndef WOLF_CRYPTO_CB_ONLY_ECC
  21648. #ifdef HAVE_ECC_VECTOR_TEST
  21649. ret = ecc_test_vector(keySize);
  21650. if (ret < 0) {
  21651. printf("ecc_test_vector %d failed!: %d\n", keySize, ret);
  21652. return ret;
  21653. }
  21654. #endif
  21655. #if defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT) && \
  21656. !defined(NO_ASN_CRYPT) && !defined(WC_NO_RNG)
  21657. ret = ecc_test_key_decode(rng, keySize);
  21658. if (ret < 0) {
  21659. if (ret == ECC_CURVE_OID_E) {
  21660. /* ignore error for curves not found */
  21661. }
  21662. else {
  21663. printf("ecc_test_key_decode %d failed!: %d\n", keySize, ret);
  21664. return ret;
  21665. }
  21666. }
  21667. #endif
  21668. #if defined(HAVE_ECC_KEY_EXPORT) && !defined(NO_ASN_CRYPT) && !defined(WC_NO_RNG)
  21669. ret = ecc_test_key_gen(rng, keySize);
  21670. if (ret < 0) {
  21671. if (ret == ECC_CURVE_OID_E) {
  21672. /* ignore error for curves not found */
  21673. }
  21674. else {
  21675. printf("ecc_test_key_gen %d failed!: %d\n", keySize, ret);
  21676. return ret;
  21677. }
  21678. }
  21679. #endif
  21680. #endif /* WOLF_CRYPTO_CB_ONLY_ECC */
  21681. return 0;
  21682. }
  21683. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  21684. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  21685. defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT) && \
  21686. !defined(WOLFSSL_NO_MALLOC) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  21687. static int ecc_point_test(void)
  21688. {
  21689. int ret;
  21690. ecc_point* point;
  21691. ecc_point* point2;
  21692. #ifdef HAVE_COMP_KEY
  21693. ecc_point* point3;
  21694. ecc_point* point4;
  21695. #endif
  21696. word32 outLen;
  21697. byte out[65];
  21698. byte der[] = { 0x04, /* = Uncompressed */
  21699. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  21700. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  21701. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  21702. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  21703. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  21704. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  21705. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  21706. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  21707. #if defined(HAVE_COMP_KEY) && (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) || \
  21708. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  21709. byte derComp0[] = { 0x02, /* = Compressed, y even */
  21710. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  21711. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  21712. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  21713. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  21714. byte derComp1[] = { 0x03, /* = Compressed, y odd */
  21715. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  21716. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  21717. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  21718. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  21719. #endif
  21720. byte altDer[] = { 0x04, /* = Uncompressed */
  21721. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  21722. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  21723. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  21724. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  21725. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  21726. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  21727. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  21728. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
  21729. int curve_idx = wc_ecc_get_curve_idx(ECC_SECP256R1);
  21730. /* if curve P256 is not enabled then test should not fail */
  21731. if (curve_idx == ECC_CURVE_INVALID)
  21732. return 0;
  21733. outLen = sizeof(out);
  21734. point = wc_ecc_new_point();
  21735. if (point == NULL)
  21736. return -10000;
  21737. point2 = wc_ecc_new_point();
  21738. if (point2 == NULL) {
  21739. wc_ecc_del_point(point);
  21740. return -10001;
  21741. }
  21742. #ifdef HAVE_COMP_KEY
  21743. point3 = wc_ecc_new_point();
  21744. if (point3 == NULL) {
  21745. wc_ecc_del_point(point2);
  21746. wc_ecc_del_point(point);
  21747. return -10002;
  21748. }
  21749. point4 = wc_ecc_new_point();
  21750. if (point4 == NULL) {
  21751. wc_ecc_del_point(point3);
  21752. wc_ecc_del_point(point2);
  21753. wc_ecc_del_point(point);
  21754. return -10003;
  21755. }
  21756. #endif
  21757. /* Parameter Validation testing. */
  21758. wc_ecc_del_point(NULL);
  21759. ret = wc_ecc_import_point_der(NULL, sizeof(der), curve_idx, point);
  21760. if (ret != ECC_BAD_ARG_E) {
  21761. ret = -10004;
  21762. goto done;
  21763. }
  21764. ret = wc_ecc_import_point_der(der, sizeof(der), ECC_CURVE_INVALID, point);
  21765. if (ret != ECC_BAD_ARG_E) {
  21766. ret = -10005;
  21767. goto done;
  21768. }
  21769. ret = wc_ecc_import_point_der(der, sizeof(der), curve_idx, NULL);
  21770. if (ret != ECC_BAD_ARG_E) {
  21771. ret = -10006;
  21772. goto done;
  21773. }
  21774. ret = wc_ecc_export_point_der(-1, point, out, &outLen);
  21775. if (ret != ECC_BAD_ARG_E) {
  21776. ret = -10007;
  21777. goto done;
  21778. }
  21779. ret = wc_ecc_export_point_der(curve_idx, NULL, out, &outLen);
  21780. if (ret != ECC_BAD_ARG_E) {
  21781. ret = -10008;
  21782. goto done;
  21783. }
  21784. ret = wc_ecc_export_point_der(curve_idx, point, NULL, &outLen);
  21785. if (ret != LENGTH_ONLY_E || outLen != sizeof(out)) {
  21786. ret = -10009;
  21787. goto done;
  21788. }
  21789. ret = wc_ecc_export_point_der(curve_idx, point, out, NULL);
  21790. if (ret != ECC_BAD_ARG_E) {
  21791. ret = -10010;
  21792. goto done;
  21793. }
  21794. outLen = 0;
  21795. ret = wc_ecc_export_point_der(curve_idx, point, out, &outLen);
  21796. if (ret != BUFFER_E) {
  21797. ret = -10011;
  21798. goto done;
  21799. }
  21800. ret = wc_ecc_copy_point(NULL, NULL);
  21801. if (ret != ECC_BAD_ARG_E) {
  21802. ret = -10012;
  21803. goto done;
  21804. }
  21805. ret = wc_ecc_copy_point(NULL, point2);
  21806. if (ret != ECC_BAD_ARG_E) {
  21807. ret = -10013;
  21808. goto done;
  21809. }
  21810. ret = wc_ecc_copy_point(point, NULL);
  21811. if (ret != ECC_BAD_ARG_E) {
  21812. ret = -10014;
  21813. goto done;
  21814. }
  21815. ret = wc_ecc_cmp_point(NULL, NULL);
  21816. if (ret != BAD_FUNC_ARG) {
  21817. ret = -10015;
  21818. goto done;
  21819. }
  21820. ret = wc_ecc_cmp_point(NULL, point2);
  21821. if (ret != BAD_FUNC_ARG) {
  21822. ret = -10016;
  21823. goto done;
  21824. }
  21825. ret = wc_ecc_cmp_point(point, NULL);
  21826. if (ret != BAD_FUNC_ARG) {
  21827. ret = -10017;
  21828. goto done;
  21829. }
  21830. /* Use API. */
  21831. ret = wc_ecc_import_point_der(der, sizeof(der), curve_idx, point);
  21832. if (ret != 0) {
  21833. ret = -10018;
  21834. goto done;
  21835. }
  21836. outLen = sizeof(out);
  21837. ret = wc_ecc_export_point_der(curve_idx, point, out, &outLen);
  21838. if (ret != 0) {
  21839. ret = -10019;
  21840. goto done;
  21841. }
  21842. if (outLen != sizeof(der)) {
  21843. ret = -10020;
  21844. goto done;
  21845. }
  21846. if (XMEMCMP(out, der, outLen) != 0) {
  21847. ret = -10021;
  21848. goto done;
  21849. }
  21850. ret = wc_ecc_copy_point(point2, point);
  21851. if (ret != MP_OKAY) {
  21852. ret = -10022;
  21853. goto done;
  21854. }
  21855. ret = wc_ecc_cmp_point(point2, point);
  21856. if (ret != MP_EQ) {
  21857. ret = -10023;
  21858. goto done;
  21859. }
  21860. ret = wc_ecc_import_point_der(altDer, sizeof(altDer), curve_idx, point2);
  21861. if (ret != 0) {
  21862. ret = -10024;
  21863. goto done;
  21864. }
  21865. ret = wc_ecc_cmp_point(point2, point);
  21866. if (ret != MP_GT) {
  21867. ret = -10025;
  21868. goto done;
  21869. }
  21870. #if defined(HAVE_COMP_KEY) && (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) || \
  21871. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  21872. ret = wc_ecc_import_point_der(derComp0, sizeof(derComp0)*2-1, curve_idx, point3);
  21873. if (ret != 0) {
  21874. ret = -10026;
  21875. goto done;
  21876. }
  21877. ret = wc_ecc_import_point_der_ex(derComp0, sizeof(derComp0), curve_idx, point4, 0);
  21878. if (ret != 0) {
  21879. ret = -10027;
  21880. goto done;
  21881. }
  21882. ret = wc_ecc_cmp_point(point3, point4);
  21883. if (ret != MP_EQ) {
  21884. ret = -10028;
  21885. goto done;
  21886. }
  21887. ret = wc_ecc_import_point_der(derComp1, sizeof(derComp1)*2-1, curve_idx, point3);
  21888. if (ret != 0) {
  21889. ret = -10029;
  21890. goto done;
  21891. }
  21892. ret = wc_ecc_import_point_der_ex(derComp1, sizeof(derComp1), curve_idx, point4, 0);
  21893. if (ret != 0) {
  21894. ret = -10030;
  21895. goto done;
  21896. }
  21897. ret = wc_ecc_cmp_point(point3, point4);
  21898. if (ret != MP_EQ) {
  21899. ret = -10031;
  21900. goto done;
  21901. }
  21902. #endif
  21903. done:
  21904. #ifdef HAVE_COMP_KEY
  21905. wc_ecc_del_point(point4);
  21906. wc_ecc_del_point(point3);
  21907. #endif
  21908. wc_ecc_del_point(point2);
  21909. wc_ecc_del_point(point);
  21910. return ret;
  21911. }
  21912. #endif /* !WOLFSSL_ATECC508A && HAVE_ECC_KEY_IMPORT && HAVE_ECC_KEY_EXPORT */
  21913. #if !defined(NO_SIG_WRAPPER) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  21914. static int ecc_sig_test(WC_RNG* rng, ecc_key* key)
  21915. {
  21916. int ret;
  21917. word32 sigSz;
  21918. int size;
  21919. byte out[ECC_MAX_SIG_SIZE];
  21920. byte in[] = TEST_STRING;
  21921. WOLFSSL_SMALL_STACK_STATIC const byte hash[] = {
  21922. 0xf2, 0x02, 0x95, 0x65, 0xcb, 0xf6, 0x2a, 0x59,
  21923. 0x39, 0x2c, 0x05, 0xff, 0x0e, 0x29, 0xaf, 0xfe,
  21924. 0x47, 0x33, 0x8c, 0x99, 0x8d, 0x58, 0x64, 0x83,
  21925. 0xa6, 0x58, 0x0a, 0x33, 0x0b, 0x84, 0x5f, 0x5f
  21926. };
  21927. word32 inLen = (word32)XSTRLEN((char*)in);
  21928. size = wc_ecc_sig_size(key);
  21929. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_ECC, key, sizeof(*key));
  21930. if (ret != size)
  21931. return -10040;
  21932. sigSz = (word32)ret;
  21933. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_ECC, in,
  21934. inLen, out, &sigSz, key, sizeof(*key), rng);
  21935. if (ret != 0)
  21936. return -10041;
  21937. TEST_SLEEP();
  21938. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_ECC, in,
  21939. inLen, out, sigSz, key, sizeof(*key));
  21940. if (ret != 0)
  21941. return -10042;
  21942. TEST_SLEEP();
  21943. sigSz = (word32)sizeof(out);
  21944. ret = wc_SignatureGenerateHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_ECC,
  21945. hash, (int)sizeof(hash), out, &sigSz, key, sizeof(*key), rng);
  21946. if (ret != 0)
  21947. return -10043;
  21948. TEST_SLEEP();
  21949. ret = wc_SignatureVerifyHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_ECC,
  21950. hash, (int)sizeof(hash), out, sigSz, key, sizeof(*key));
  21951. if (ret != 0)
  21952. return -10044;
  21953. TEST_SLEEP();
  21954. return 0;
  21955. }
  21956. #endif
  21957. #if defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT) && \
  21958. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  21959. static int ecc_exp_imp_test(ecc_key* key)
  21960. {
  21961. int ret;
  21962. int curve_id;
  21963. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21964. ecc_key *keyImp = (ecc_key *)XMALLOC(sizeof *keyImp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21965. #else
  21966. ecc_key keyImp[1];
  21967. #endif
  21968. byte priv[32];
  21969. word32 privLen;
  21970. byte pub[65*2];
  21971. word32 pubLen, pubLenX, pubLenY;
  21972. const char qx[] = "7a4e287890a1a47ad3457e52f2f76a83"
  21973. "ce46cbc947616d0cbaa82323818a793d";
  21974. const char qy[] = "eec4084f5b29ebf29c44cce3b3059610"
  21975. "922f8b30ea6e8811742ac7238fe87308";
  21976. const char d[] = "8c14b793cb19137e323a6d2e2a870bca"
  21977. "2e7a493ec1153b3a95feb8a4873f8d08";
  21978. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21979. if (keyImp == NULL)
  21980. ERROR_OUT(-10050, done);
  21981. #endif
  21982. wc_ecc_init_ex(keyImp, HEAP_HINT, devId);
  21983. privLen = sizeof(priv);
  21984. ret = wc_ecc_export_private_only(key, priv, &privLen);
  21985. if (ret != 0) {
  21986. ret = -10051;
  21987. goto done;
  21988. }
  21989. pubLen = sizeof(pub);
  21990. ret = wc_ecc_export_point_der(key->idx, &key->pubkey, pub, &pubLen);
  21991. if (ret != 0) {
  21992. ret = -10052;
  21993. goto done;
  21994. }
  21995. ret = wc_ecc_import_private_key(priv, privLen, pub, pubLen, keyImp);
  21996. if (ret != 0) {
  21997. ret = -10053;
  21998. goto done;
  21999. }
  22000. wc_ecc_free(keyImp);
  22001. wc_ecc_init_ex(keyImp, HEAP_HINT, devId);
  22002. ret = wc_ecc_import_raw_ex(keyImp, qx, qy, d, ECC_SECP256R1);
  22003. if (ret != 0) {
  22004. ret = -10054;
  22005. goto done;
  22006. }
  22007. wc_ecc_free(keyImp);
  22008. wc_ecc_init_ex(keyImp, HEAP_HINT, devId);
  22009. curve_id = wc_ecc_get_curve_id(key->idx);
  22010. if (curve_id < 0) {
  22011. ret = -10055;
  22012. goto done;
  22013. }
  22014. /* test import private only */
  22015. ret = wc_ecc_import_private_key_ex(priv, privLen, NULL, 0, keyImp,
  22016. curve_id);
  22017. if (ret != 0) {
  22018. ret = -10056;
  22019. goto done;
  22020. }
  22021. wc_ecc_free(keyImp);
  22022. wc_ecc_init_ex(keyImp, HEAP_HINT, devId);
  22023. /* test export public raw */
  22024. pubLenX = pubLenY = 32;
  22025. ret = wc_ecc_export_public_raw(key, pub, &pubLenX, &pub[32], &pubLenY);
  22026. if (ret != 0) {
  22027. ret = -10057;
  22028. goto done;
  22029. }
  22030. #ifndef HAVE_SELFTEST
  22031. /* test import of public */
  22032. ret = wc_ecc_import_unsigned(keyImp, pub, &pub[32], NULL, ECC_SECP256R1);
  22033. if (ret != 0) {
  22034. ret = -10058;
  22035. goto done;
  22036. }
  22037. #endif
  22038. wc_ecc_free(keyImp);
  22039. wc_ecc_init_ex(keyImp, HEAP_HINT, devId);
  22040. /* test export private and public raw */
  22041. pubLenX = pubLenY = privLen = 32;
  22042. ret = wc_ecc_export_private_raw(key, pub, &pubLenX, &pub[32], &pubLenY,
  22043. priv, &privLen);
  22044. if (ret != 0) {
  22045. ret = -10059;
  22046. goto done;
  22047. }
  22048. #ifndef HAVE_SELFTEST
  22049. /* test import of private and public */
  22050. ret = wc_ecc_import_unsigned(keyImp, pub, &pub[32], priv, ECC_SECP256R1);
  22051. if (ret != 0) {
  22052. ret = -10060;
  22053. goto done;
  22054. }
  22055. #endif
  22056. done:
  22057. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22058. if (keyImp != NULL) {
  22059. wc_ecc_free(keyImp);
  22060. XFREE(keyImp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22061. }
  22062. #else
  22063. wc_ecc_free(keyImp);
  22064. #endif
  22065. return ret;
  22066. }
  22067. #endif /* HAVE_ECC_KEY_IMPORT && HAVE_ECC_KEY_EXPORT */
  22068. #if defined(HAVE_ECC_KEY_IMPORT) && !defined(WOLFSSL_VALIDATE_ECC_IMPORT) && \
  22069. !defined(WOLFSSL_CRYPTOCELL) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  22070. static int ecc_mulmod_test(ecc_key* key1)
  22071. {
  22072. int ret;
  22073. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22074. ecc_key *key2 = (ecc_key *)XMALLOC(sizeof *key2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22075. ecc_key *key3 = (ecc_key *)XMALLOC(sizeof *key3, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22076. #else
  22077. ecc_key key2[1];
  22078. ecc_key key3[1];
  22079. #endif
  22080. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22081. if ((key2 == NULL) || (key3 == NULL))
  22082. ERROR_OUT(MEMORY_E, done);
  22083. #endif
  22084. wc_ecc_init_ex(key2, HEAP_HINT, devId);
  22085. wc_ecc_init_ex(key3, HEAP_HINT, devId);
  22086. /* TODO: Use test data, test with WOLFSSL_VALIDATE_ECC_IMPORT. */
  22087. /* Need base point (Gx,Gy) and parameter A - load them as the public and
  22088. * private key in key2.
  22089. */
  22090. ret = wc_ecc_import_raw_ex(key2, key1->dp->Gx, key1->dp->Gy, key1->dp->Af,
  22091. ECC_SECP256R1);
  22092. if (ret != 0)
  22093. goto done;
  22094. /* Need a point (Gx,Gy) and prime - load them as the public and private key
  22095. * in key3.
  22096. */
  22097. ret = wc_ecc_import_raw_ex(key3, key1->dp->Gx, key1->dp->Gy,
  22098. key1->dp->prime, ECC_SECP256R1);
  22099. if (ret != 0)
  22100. goto done;
  22101. ret = wc_ecc_mulmod(&key1->k, &key2->pubkey, &key3->pubkey, &key2->k, &key3->k,
  22102. 1);
  22103. if (ret != 0) {
  22104. ret = -10070;
  22105. goto done;
  22106. }
  22107. done:
  22108. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22109. if (key2 != NULL) {
  22110. wc_ecc_free(key2);
  22111. XFREE(key2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22112. }
  22113. if (key3 != NULL) {
  22114. wc_ecc_free(key3);
  22115. XFREE(key3, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22116. }
  22117. #else
  22118. wc_ecc_free(key3);
  22119. wc_ecc_free(key2);
  22120. #endif
  22121. return ret;
  22122. }
  22123. #endif
  22124. #if defined(HAVE_ECC_DHE) && !defined(WC_NO_RNG) && \
  22125. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  22126. static int ecc_ssh_test(ecc_key* key, WC_RNG* rng)
  22127. {
  22128. int ret;
  22129. byte out[128];
  22130. word32 outLen = sizeof(out);
  22131. /* Parameter Validation testing. */
  22132. ret = wc_ecc_shared_secret_ssh(NULL, &key->pubkey, out, &outLen);
  22133. if (ret != BAD_FUNC_ARG)
  22134. return -10080;
  22135. ret = wc_ecc_shared_secret_ssh(key, NULL, out, &outLen);
  22136. if (ret != BAD_FUNC_ARG)
  22137. return -10081;
  22138. ret = wc_ecc_shared_secret_ssh(key, &key->pubkey, NULL, &outLen);
  22139. if (ret != BAD_FUNC_ARG)
  22140. return -10082;
  22141. ret = wc_ecc_shared_secret_ssh(key, &key->pubkey, out, NULL);
  22142. if (ret != BAD_FUNC_ARG)
  22143. return -10083;
  22144. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  22145. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  22146. !defined(HAVE_SELFTEST)
  22147. ret = wc_ecc_set_rng(key, rng);
  22148. if (ret != 0)
  22149. return -10084;
  22150. #else
  22151. (void)rng;
  22152. #endif
  22153. /* Use API. */
  22154. ret = 0;
  22155. do {
  22156. #if defined(WOLFSSL_ASYNC_CRYPT)
  22157. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  22158. #endif
  22159. if (ret == 0)
  22160. ret = wc_ecc_shared_secret_ssh(key, &key->pubkey, out, &outLen);
  22161. } while (ret == WC_PENDING_E);
  22162. if (ret != 0)
  22163. return -10085;
  22164. TEST_SLEEP();
  22165. return 0;
  22166. }
  22167. #endif /* HAVE_ECC_DHE && !WC_NO_RNG */
  22168. static int ecc_def_curve_test(WC_RNG *rng)
  22169. {
  22170. int ret;
  22171. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22172. ecc_key *key = (ecc_key *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22173. #else
  22174. ecc_key key[1];
  22175. #endif
  22176. #if (defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT)) || \
  22177. (defined(HAVE_ECC_KEY_IMPORT) && !defined(WOLFSSL_VALIDATE_ECC_IMPORT))
  22178. word32 idx = 0;
  22179. #endif
  22180. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22181. if (key == NULL)
  22182. ERROR_OUT(MEMORY_E, done);
  22183. #endif
  22184. wc_ecc_init_ex(key, HEAP_HINT, devId);
  22185. /* Use API */
  22186. ret = wc_ecc_set_flags(NULL, 0);
  22187. if (ret != BAD_FUNC_ARG) {
  22188. ret = -10090;
  22189. goto done;
  22190. }
  22191. ret = wc_ecc_set_flags(key, 0);
  22192. if (ret != 0) {
  22193. ret = -10091;
  22194. goto done;
  22195. }
  22196. #ifndef WOLF_CRYPTO_CB_ONLY_ECC
  22197. #ifndef WC_NO_RNG
  22198. ret = wc_ecc_make_key(rng, ECC_KEYGEN_SIZE, key);
  22199. #if defined(WOLFSSL_ASYNC_CRYPT)
  22200. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  22201. #endif
  22202. if (ret != 0) {
  22203. goto done;
  22204. }
  22205. #ifndef NO_SIG_WRAPPER
  22206. ret = ecc_sig_test(rng, key);
  22207. if (ret < 0)
  22208. goto done;
  22209. #endif
  22210. TEST_SLEEP();
  22211. #if defined(HAVE_ECC_DHE) && !defined(WOLFSSL_CRYPTOCELL) && \
  22212. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  22213. ret = ecc_ssh_test(key, rng);
  22214. if (ret < 0)
  22215. goto done;
  22216. #endif
  22217. wc_ecc_free(key);
  22218. #else
  22219. (void)rng;
  22220. #endif /* !WC_NO_RNG */
  22221. #if (defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT)) || \
  22222. (defined(HAVE_ECC_KEY_IMPORT) && !defined(WOLFSSL_VALIDATE_ECC_IMPORT))
  22223. /* Use test ECC key - ensure real private "d" exists */
  22224. #ifdef USE_CERT_BUFFERS_256
  22225. ret = wc_EccPrivateKeyDecode(ecc_key_der_256, &idx, key,
  22226. sizeof_ecc_key_der_256);
  22227. #else
  22228. {
  22229. XFILE file = XFOPEN(eccKeyDerFile, "rb");
  22230. byte der[128];
  22231. word32 derSz;
  22232. if (!file) {
  22233. ERROR_OUT(-10093, done);
  22234. }
  22235. derSz = (word32)XFREAD(der, 1, sizeof(der), file);
  22236. XFCLOSE(file);
  22237. ret = wc_EccPrivateKeyDecode(der, &idx, key, derSz);
  22238. }
  22239. #endif
  22240. if (ret != 0) {
  22241. goto done;
  22242. }
  22243. #if defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT)
  22244. ret = ecc_exp_imp_test(key);
  22245. if (ret < 0)
  22246. goto done;
  22247. #endif
  22248. #if defined(HAVE_ECC_KEY_IMPORT) && !defined(WOLFSSL_VALIDATE_ECC_IMPORT) && \
  22249. !defined(WOLFSSL_CRYPTOCELL)
  22250. ret = ecc_mulmod_test(key);
  22251. if (ret < 0)
  22252. goto done;
  22253. #endif
  22254. #endif
  22255. #else
  22256. (void)rng;
  22257. (void)idx;
  22258. #endif /* WOLF_CRYPTO_CB_ONLY_ECC */
  22259. done:
  22260. wc_ecc_free(key);
  22261. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22262. if (key != NULL) {
  22263. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22264. }
  22265. #endif
  22266. return ret;
  22267. }
  22268. #endif /* !NO_ECC256 || HAVE_ALL_CURVES */
  22269. #if defined(WOLFSSL_CERT_EXT) && \
  22270. (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  22271. static int ecc_decode_test(void)
  22272. {
  22273. int ret;
  22274. word32 inSz;
  22275. word32 inOutIdx;
  22276. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22277. ecc_key *key = (ecc_key *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22278. #else
  22279. ecc_key key[1];
  22280. #endif
  22281. /* SECP256R1 OID: 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07 */
  22282. /* This is ecc_clikeypub_der_256. */
  22283. WOLFSSL_SMALL_STACK_STATIC const byte good[] = {
  22284. 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce,
  22285. 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d,
  22286. 0x03, 0x01, 0x07, 0x03, 0x42, 0x00, 0x04, 0x55, 0xbf, 0xf4,
  22287. 0x0f, 0x44, 0x50, 0x9a, 0x3d, 0xce, 0x9b, 0xb7, 0xf0, 0xc5,
  22288. 0x4d, 0xf5, 0x70, 0x7b, 0xd4, 0xec, 0x24, 0x8e, 0x19, 0x80,
  22289. 0xec, 0x5a, 0x4c, 0xa2, 0x24, 0x03, 0x62, 0x2c, 0x9b, 0xda,
  22290. 0xef, 0xa2, 0x35, 0x12, 0x43, 0x84, 0x76, 0x16, 0xc6, 0x56,
  22291. 0x95, 0x06, 0xcc, 0x01, 0xa9, 0xbd, 0xf6, 0x75, 0x1a, 0x42,
  22292. 0xf7, 0xbd, 0xa9, 0xb2, 0x36, 0x22, 0x5f, 0xc7, 0x5d, 0x7f,
  22293. 0xb4 };
  22294. WOLFSSL_SMALL_STACK_STATIC const byte badNoObjId[] = { 0x30, 0x08, 0x30, 0x06, 0x03, 0x04,
  22295. 0x00, 0x04, 0x01, 0x01 };
  22296. WOLFSSL_SMALL_STACK_STATIC const byte badOneObjId[] = { 0x30, 0x0a, 0x30, 0x08, 0x06, 0x00,
  22297. 0x03, 0x04, 0x00, 0x04, 0x01, 0x01 };
  22298. WOLFSSL_SMALL_STACK_STATIC const byte badObjId1Len[] = { 0x30, 0x0c, 0x30, 0x0a, 0x06, 0x09,
  22299. 0x06, 0x00, 0x03, 0x04, 0x00, 0x04, 0x01, 0x01 };
  22300. WOLFSSL_SMALL_STACK_STATIC const byte badObj2d1Len[] = { 0x30, 0x0c, 0x30, 0x0a, 0x06, 0x00,
  22301. 0x06, 0x07, 0x03, 0x04, 0x00, 0x04, 0x01, 0x01 };
  22302. WOLFSSL_SMALL_STACK_STATIC const byte badNotBitStr[] = { 0x30, 0x14, 0x30, 0x0b, 0x06, 0x00,
  22303. 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07,
  22304. 0x04, 0x04, 0x00, 0x04, 0x01, 0x01 };
  22305. WOLFSSL_SMALL_STACK_STATIC const byte badBitStrLen[] = { 0x30, 0x14, 0x30, 0x0b, 0x06, 0x00,
  22306. 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07,
  22307. 0x03, 0x05, 0x00, 0x04, 0x01, 0x01 };
  22308. WOLFSSL_SMALL_STACK_STATIC const byte badNoBitStrZero[] = { 0x30, 0x13, 0x30, 0x0a, 0x06, 0x00,
  22309. 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07,
  22310. 0x03, 0x03, 0x04, 0x01, 0x01 };
  22311. WOLFSSL_SMALL_STACK_STATIC const byte badPoint[] = { 0x30, 0x12, 0x30, 0x09, 0x06, 0x00,
  22312. 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07,
  22313. 0x03, 0x03, 0x00, 0x04, 0x01 };
  22314. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22315. if (key == NULL)
  22316. ERROR_OUT(MEMORY_E, done);
  22317. #endif
  22318. XMEMSET(key, 0, sizeof *key);
  22319. wc_ecc_init_ex(key, HEAP_HINT, devId);
  22320. inSz = sizeof(good);
  22321. ret = wc_EccPublicKeyDecode(NULL, &inOutIdx, key, inSz);
  22322. if (ret != BAD_FUNC_ARG) {
  22323. ret = -10100;
  22324. goto done;
  22325. }
  22326. ret = wc_EccPublicKeyDecode(good, NULL, key, inSz);
  22327. if (ret != BAD_FUNC_ARG) {
  22328. ret = -10101;
  22329. goto done;
  22330. }
  22331. ret = wc_EccPublicKeyDecode(good, &inOutIdx, NULL, inSz);
  22332. if (ret != BAD_FUNC_ARG) {
  22333. ret = -10102;
  22334. goto done;
  22335. }
  22336. ret = wc_EccPublicKeyDecode(good, &inOutIdx, key, 0);
  22337. if (ret != BAD_FUNC_ARG) {
  22338. ret = -10103;
  22339. goto done;
  22340. }
  22341. /* Change offset to produce bad input data. */
  22342. inOutIdx = 2;
  22343. inSz = sizeof(good) - inOutIdx;
  22344. ret = wc_EccPublicKeyDecode(good, &inOutIdx, key, inSz);
  22345. if (ret != ASN_PARSE_E) {
  22346. ret = -10104;
  22347. goto done;
  22348. }
  22349. inOutIdx = 4;
  22350. inSz = sizeof(good) - inOutIdx;
  22351. ret = wc_EccPublicKeyDecode(good, &inOutIdx, key, inSz);
  22352. if (ret != ASN_PARSE_E) {
  22353. ret = -10105;
  22354. goto done;
  22355. }
  22356. /* Bad data. */
  22357. inSz = sizeof(badNoObjId);
  22358. inOutIdx = 0;
  22359. ret = wc_EccPublicKeyDecode(badNoObjId, &inOutIdx, key, inSz);
  22360. if (ret != ASN_OBJECT_ID_E && ret != ASN_PARSE_E) {
  22361. ret = -10106;
  22362. goto done;
  22363. }
  22364. inSz = sizeof(badOneObjId);
  22365. inOutIdx = 0;
  22366. ret = wc_EccPublicKeyDecode(badOneObjId, &inOutIdx, key, inSz);
  22367. if (ret != ASN_OBJECT_ID_E && ret != ASN_PARSE_E) {
  22368. ret = -10107;
  22369. goto done;
  22370. }
  22371. inSz = sizeof(badObjId1Len);
  22372. inOutIdx = 0;
  22373. ret = wc_EccPublicKeyDecode(badObjId1Len, &inOutIdx, key, inSz);
  22374. if (ret != ASN_PARSE_E) {
  22375. ret = -10108;
  22376. goto done;
  22377. }
  22378. inSz = sizeof(badObj2d1Len);
  22379. inOutIdx = 0;
  22380. ret = wc_EccPublicKeyDecode(badObj2d1Len, &inOutIdx, key, inSz);
  22381. if (ret != ASN_PARSE_E) {
  22382. ret = -10109;
  22383. goto done;
  22384. }
  22385. inSz = sizeof(badNotBitStr);
  22386. inOutIdx = 0;
  22387. ret = wc_EccPublicKeyDecode(badNotBitStr, &inOutIdx, key, inSz);
  22388. if (ret != ASN_BITSTR_E && ret != ASN_PARSE_E) {
  22389. ret = -10110;
  22390. goto done;
  22391. }
  22392. inSz = sizeof(badBitStrLen);
  22393. inOutIdx = 0;
  22394. ret = wc_EccPublicKeyDecode(badBitStrLen, &inOutIdx, key, inSz);
  22395. if (ret != ASN_PARSE_E) {
  22396. ret = -10111;
  22397. goto done;
  22398. }
  22399. inSz = sizeof(badNoBitStrZero);
  22400. inOutIdx = 0;
  22401. ret = wc_EccPublicKeyDecode(badNoBitStrZero, &inOutIdx, key, inSz);
  22402. if (ret != ASN_EXPECT_0_E && ret != ASN_PARSE_E) {
  22403. ret = -10112;
  22404. goto done;
  22405. }
  22406. inSz = sizeof(badPoint);
  22407. inOutIdx = 0;
  22408. ret = wc_EccPublicKeyDecode(badPoint, &inOutIdx, key, inSz);
  22409. if (ret != ASN_ECC_KEY_E && ret != ASN_PARSE_E) {
  22410. ret = -10113;
  22411. goto done;
  22412. }
  22413. inSz = sizeof(good);
  22414. inOutIdx = 0;
  22415. ret = wc_EccPublicKeyDecode(good, &inOutIdx, key, inSz);
  22416. if (ret != 0) {
  22417. ret = -10114;
  22418. goto done;
  22419. }
  22420. done:
  22421. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22422. if (key != NULL) {
  22423. wc_ecc_free(key);
  22424. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22425. }
  22426. #else
  22427. wc_ecc_free(key);
  22428. #endif
  22429. return ret;
  22430. }
  22431. #endif /* WOLFSSL_CERT_EXT */
  22432. #ifdef WOLFSSL_CUSTOM_CURVES
  22433. static const byte eccKeyExplicitCurve[] = {
  22434. 0x30, 0x81, 0xf5, 0x30, 0x81, 0xae, 0x06, 0x07,
  22435. 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01, 0x30,
  22436. 0x81, 0xa2, 0x02, 0x01, 0x01, 0x30, 0x2c, 0x06,
  22437. 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x01, 0x01,
  22438. 0x02, 0x21, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff,
  22439. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  22440. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  22441. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0xff,
  22442. 0xff, 0xfc, 0x2f, 0x30, 0x06, 0x04, 0x01, 0x00,
  22443. 0x04, 0x01, 0x07, 0x04, 0x41, 0x04, 0x79, 0xbe,
  22444. 0x66, 0x7e, 0xf9, 0xdc, 0xbb, 0xac, 0x55, 0xa0,
  22445. 0x62, 0x95, 0xce, 0x87, 0x0b, 0x07, 0x02, 0x9b,
  22446. 0xfc, 0xdb, 0x2d, 0xce, 0x28, 0xd9, 0x59, 0xf2,
  22447. 0x81, 0x5b, 0x16, 0xf8, 0x17, 0x98, 0x48, 0x3a,
  22448. 0xda, 0x77, 0x26, 0xa3, 0xc4, 0x65, 0x5d, 0xa4,
  22449. 0xfb, 0xfc, 0x0e, 0x11, 0x08, 0xa8, 0xfd, 0x17,
  22450. 0xb4, 0x48, 0xa6, 0x85, 0x54, 0x19, 0x9c, 0x47,
  22451. 0xd0, 0x8f, 0xfb, 0x10, 0xd4, 0xb8, 0x02, 0x21,
  22452. 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  22453. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  22454. 0xfe, 0xba, 0xae, 0xdc, 0xe6, 0xaf, 0x48, 0xa0,
  22455. 0x3b, 0xbf, 0xd2, 0x5e, 0x8c, 0xd0, 0x36, 0x41,
  22456. 0x41, 0x02, 0x01, 0x01, 0x03, 0x42, 0x00, 0x04,
  22457. 0x3c, 0x4c, 0xc9, 0x5e, 0x2e, 0xa2, 0x3d, 0x49,
  22458. 0xcc, 0x5b, 0xff, 0x4f, 0xc9, 0x2e, 0x1d, 0x4a,
  22459. 0xc6, 0x21, 0xf6, 0xf3, 0xe6, 0x0b, 0x4f, 0xa9,
  22460. 0x9d, 0x74, 0x99, 0xdd, 0x97, 0xc7, 0x6e, 0xbe,
  22461. 0x14, 0x2b, 0x39, 0x9d, 0x63, 0xc7, 0x97, 0x0d,
  22462. 0x45, 0x25, 0x40, 0x30, 0x77, 0x05, 0x76, 0x88,
  22463. 0x38, 0x96, 0x29, 0x7d, 0x9c, 0xe1, 0x50, 0xbe,
  22464. 0xac, 0xf0, 0x1d, 0x86, 0xf4, 0x2f, 0x65, 0x0b
  22465. };
  22466. static int ecc_test_custom_curves(WC_RNG* rng)
  22467. {
  22468. int ret;
  22469. word32 inOutIdx;
  22470. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22471. ecc_key *key = (ecc_key *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22472. #else
  22473. ecc_key key[1];
  22474. #endif
  22475. /* test use of custom curve - using BRAINPOOLP256R1 for test */
  22476. #if defined(HAVE_ECC_BRAINPOOL) && !defined(HAVE_INTEL_QA)
  22477. #ifndef WOLFSSL_ECC_CURVE_STATIC
  22478. WOLFSSL_SMALL_STACK_STATIC const ecc_oid_t ecc_oid_brainpoolp256r1[] = {
  22479. 0x2B,0x24,0x03,0x03,0x02,0x08,0x01,0x01,0x07
  22480. };
  22481. #define ecc_oid_brainpoolp256r1_sz \
  22482. (sizeof(ecc_oid_brainpoolp256r1) / sizeof(ecc_oid_t))
  22483. #else
  22484. #define ecc_oid_brainpoolp256r1 { \
  22485. 0x2B,0x24,0x03,0x03,0x02,0x08,0x01,0x01,0x07 \
  22486. }
  22487. #define ecc_oid_brainpoolp256r1_sz 9
  22488. #endif
  22489. #define ecc_oid_brainpoolp256r1_sum 104
  22490. WOLFSSL_SMALL_STACK_STATIC const ecc_set_type ecc_dp_brainpool256r1 = {
  22491. 32, /* size/bytes */
  22492. ECC_CURVE_CUSTOM, /* ID */
  22493. "BRAINPOOLP256R1", /* curve name */
  22494. "A9FB57DBA1EEA9BC3E660A909D838D726E3BF623D52620282013481D1F6E5377", /* prime */
  22495. "7D5A0975FC2C3057EEF67530417AFFE7FB8055C126DC5C6CE94A4B44F330B5D9", /* A */
  22496. "26DC5C6CE94A4B44F330B5D9BBD77CBF958416295CF7E1CE6BCCDC18FF8C07B6", /* B */
  22497. "A9FB57DBA1EEA9BC3E660A909D838D718C397AA3B561A6F7901E0E82974856A7", /* order */
  22498. "8BD2AEB9CB7E57CB2C4B482FFC81B7AFB9DE27E1E3BD23C23A4453BD9ACE3262", /* Gx */
  22499. "547EF835C3DAC4FD97F8461A14611DC9C27745132DED8E545C1D54C72F046997", /* Gy */
  22500. ecc_oid_brainpoolp256r1, /* oid/oidSz */
  22501. ecc_oid_brainpoolp256r1_sz,
  22502. ecc_oid_brainpoolp256r1_sum, /* oid sum */
  22503. 1, /* cofactor */
  22504. };
  22505. #endif /* HAVE_ECC_BRAINPOOL */
  22506. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22507. if (! key) {
  22508. ret = MEMORY_E;
  22509. goto done;
  22510. }
  22511. #endif
  22512. XMEMSET(key, 0, sizeof *key);
  22513. #if defined(HAVE_ECC_BRAINPOOL) && !defined(HAVE_INTEL_QA)
  22514. ret = ecc_test_curve_size(rng, 0, ECC_TEST_VERIFY_COUNT, ECC_CURVE_DEF,
  22515. &ecc_dp_brainpool256r1);
  22516. if (ret != 0) {
  22517. printf("ECC test for custom curve failed! %d\n", ret);
  22518. goto done;
  22519. }
  22520. #endif
  22521. #if defined(HAVE_ECC_BRAINPOOL) || defined(HAVE_ECC_KOBLITZ)
  22522. {
  22523. int curve_id;
  22524. #ifdef HAVE_ECC_BRAINPOOL
  22525. curve_id = ECC_BRAINPOOLP256R1;
  22526. #else
  22527. curve_id = ECC_SECP256K1;
  22528. #endif
  22529. /* Test and demonstrate use of non-SECP curve */
  22530. ret = ecc_test_curve_size(rng, 0, ECC_TEST_VERIFY_COUNT, curve_id, NULL);
  22531. if (ret < 0) {
  22532. printf("ECC test for curve_id %d failed! %d\n", curve_id, ret);
  22533. goto done;
  22534. }
  22535. }
  22536. #endif
  22537. ret = wc_ecc_init_ex(key, HEAP_HINT, devId);
  22538. if (ret != 0) {
  22539. ret = -10120;
  22540. goto done;
  22541. }
  22542. inOutIdx = 0;
  22543. ret = wc_EccPublicKeyDecode(eccKeyExplicitCurve, &inOutIdx, key,
  22544. sizeof(eccKeyExplicitCurve));
  22545. if (ret != 0)
  22546. ret = -10121;
  22547. done:
  22548. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22549. if (key) {
  22550. wc_ecc_free(key);
  22551. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22552. }
  22553. #else
  22554. wc_ecc_free(key);
  22555. #endif
  22556. (void)rng;
  22557. return ret;
  22558. }
  22559. #endif /* WOLFSSL_CUSTOM_CURVES */
  22560. #ifdef WOLFSSL_CERT_GEN
  22561. /* Make Cert / Sign example for ECC cert and ECC CA */
  22562. static int ecc_test_cert_gen(WC_RNG* rng)
  22563. {
  22564. int ret;
  22565. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22566. Cert *myCert = (Cert *)XMALLOC(sizeof *myCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22567. #ifdef WOLFSSL_TEST_CERT
  22568. DecodedCert *decode = (DecodedCert *)XMALLOC(sizeof *decode, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22569. #endif
  22570. ecc_key *caEccKey = (ecc_key *)XMALLOC(sizeof *caEccKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22571. ecc_key *certPubKey = (ecc_key *)XMALLOC(sizeof *certPubKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22572. #else
  22573. Cert myCert[1];
  22574. #ifdef WOLFSSL_TEST_CERT
  22575. DecodedCert decode[1];
  22576. #endif
  22577. ecc_key caEccKey[1];
  22578. ecc_key certPubKey[1];
  22579. #endif
  22580. int certSz;
  22581. size_t bytes;
  22582. word32 idx = 0;
  22583. #ifndef USE_CERT_BUFFERS_256
  22584. XFILE file;
  22585. #endif
  22586. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22587. byte* der = NULL;
  22588. #else
  22589. byte der[FOURK_BUF];
  22590. #endif
  22591. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22592. if ((myCert == NULL)
  22593. #ifdef WOLFSSL_TEST_CERT
  22594. || (decode == NULL)
  22595. #endif
  22596. || (caEccKey == NULL) || (certPubKey == NULL))
  22597. ERROR_OUT(MEMORY_E, exit);
  22598. #endif
  22599. XMEMSET(caEccKey, 0, sizeof *caEccKey);
  22600. XMEMSET(certPubKey, 0, sizeof *certPubKey);
  22601. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22602. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22603. if (der == NULL) {
  22604. ERROR_OUT(-10130, exit);
  22605. }
  22606. #endif
  22607. /* Get cert private key */
  22608. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  22609. /* Get Cert Key 384 */
  22610. #ifdef USE_CERT_BUFFERS_256
  22611. XMEMCPY(der, ca_ecc_key_der_384, sizeof_ca_ecc_key_der_384);
  22612. bytes = sizeof_ca_ecc_key_der_384;
  22613. #else
  22614. file = XFOPEN(eccCaKey384File, "rb");
  22615. if (!file) {
  22616. ERROR_OUT(-10131, exit);
  22617. }
  22618. bytes = XFREAD(der, 1, FOURK_BUF, file);
  22619. XFCLOSE(file);
  22620. (void)eccCaKeyFile;
  22621. #endif /* USE_CERT_BUFFERS_256 */
  22622. #else
  22623. #ifdef USE_CERT_BUFFERS_256
  22624. XMEMCPY(der, ca_ecc_key_der_256, sizeof_ca_ecc_key_der_256);
  22625. bytes = sizeof_ca_ecc_key_der_256;
  22626. #else
  22627. file = XFOPEN(eccCaKeyFile, "rb");
  22628. if (!file) {
  22629. ERROR_OUT(-10132, exit);
  22630. }
  22631. bytes = XFREAD(der, 1, FOURK_BUF, file);
  22632. XFCLOSE(file);
  22633. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  22634. (void)eccCaKey384File;
  22635. #endif
  22636. #endif /* USE_CERT_BUFFERS_256 */
  22637. #endif /* ENABLE_ECC384_CERT_GEN_TEST */
  22638. /* Get CA Key */
  22639. ret = wc_ecc_init_ex(caEccKey, HEAP_HINT, devId);
  22640. if (ret != 0) {
  22641. ERROR_OUT(-10133, exit);
  22642. }
  22643. ret = wc_EccPrivateKeyDecode(der, &idx, caEccKey, (word32)bytes);
  22644. if (ret != 0) {
  22645. ERROR_OUT(-10134, exit);
  22646. }
  22647. /* Make a public key */
  22648. ret = wc_ecc_init_ex(certPubKey, HEAP_HINT, devId);
  22649. if (ret != 0) {
  22650. ERROR_OUT(-10135, exit);
  22651. }
  22652. ret = wc_ecc_make_key(rng, ECC_KEYGEN_SIZE, certPubKey);
  22653. #if defined(WOLFSSL_ASYNC_CRYPT)
  22654. ret = wc_AsyncWait(ret, &certPubKey->asyncDev, WC_ASYNC_FLAG_NONE);
  22655. #endif
  22656. if (ret != 0) {
  22657. ERROR_OUT(-10136, exit);
  22658. }
  22659. TEST_SLEEP();
  22660. /* Setup Certificate */
  22661. if (wc_InitCert_ex(myCert, HEAP_HINT, devId)) {
  22662. ERROR_OUT(-10137, exit);
  22663. }
  22664. #ifndef NO_SHA256
  22665. myCert->sigType = CTC_SHA256wECDSA;
  22666. #else
  22667. myCert->sigType = CTC_SHAwECDSA;
  22668. #endif
  22669. XMEMCPY(&myCert->subject, &certDefaultName, sizeof(CertName));
  22670. #ifdef WOLFSSL_CERT_EXT
  22671. /* add Policies */
  22672. XSTRNCPY(myCert->certPolicies[0], "2.4.589440.587.101.2.1.9632587.1",
  22673. CTC_MAX_CERTPOL_SZ);
  22674. XSTRNCPY(myCert->certPolicies[1], "1.2.13025.489.1.113549",
  22675. CTC_MAX_CERTPOL_SZ);
  22676. myCert->certPoliciesNb = 2;
  22677. /* add SKID from the Public Key */
  22678. if (wc_SetSubjectKeyIdFromPublicKey(myCert, NULL, certPubKey) != 0) {
  22679. ERROR_OUT(-10138, exit);
  22680. }
  22681. /* add AKID from the Public Key */
  22682. if (wc_SetAuthKeyIdFromPublicKey(myCert, NULL, caEccKey) != 0) {
  22683. ERROR_OUT(-10139, exit);
  22684. }
  22685. /* add Key Usage */
  22686. if (wc_SetKeyUsage(myCert, certKeyUsage) != 0) {
  22687. ERROR_OUT(-10140, exit);
  22688. }
  22689. #endif /* WOLFSSL_CERT_EXT */
  22690. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  22691. #if defined(USE_CERT_BUFFERS_256)
  22692. ret = wc_SetIssuerBuffer(myCert, ca_ecc_cert_der_384,
  22693. sizeof_ca_ecc_cert_der_384);
  22694. #else
  22695. ret = wc_SetIssuer(myCert, eccCaCert384File);
  22696. (void)eccCaCertFile;
  22697. #endif
  22698. #else
  22699. #if defined(USE_CERT_BUFFERS_256)
  22700. ret = wc_SetIssuerBuffer(myCert, ca_ecc_cert_der_256,
  22701. sizeof_ca_ecc_cert_der_256);
  22702. #else
  22703. ret = wc_SetIssuer(myCert, eccCaCertFile);
  22704. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  22705. (void)eccCaCert384File;
  22706. #endif
  22707. #endif
  22708. #endif /* ENABLE_ECC384_CERT_GEN_TEST */
  22709. if (ret < 0) {
  22710. ERROR_OUT(-10141, exit);
  22711. }
  22712. certSz = wc_MakeCert(myCert, der, FOURK_BUF, NULL, certPubKey, rng);
  22713. if (certSz < 0) {
  22714. ERROR_OUT(-10142, exit);
  22715. }
  22716. ret = 0;
  22717. do {
  22718. #if defined(WOLFSSL_ASYNC_CRYPT)
  22719. ret = wc_AsyncWait(ret, &caEccKey->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  22720. #endif
  22721. if (ret >= 0) {
  22722. ret = wc_SignCert(myCert->bodySz, myCert->sigType, der,
  22723. FOURK_BUF, NULL, caEccKey, rng);
  22724. }
  22725. } while (ret == WC_PENDING_E);
  22726. if (ret < 0) {
  22727. ERROR_OUT(-10143, exit);
  22728. }
  22729. certSz = ret;
  22730. TEST_SLEEP();
  22731. #ifdef WOLFSSL_TEST_CERT
  22732. InitDecodedCert(decode, der, certSz, HEAP_HINT);
  22733. ret = ParseCert(decode, CERT_TYPE, NO_VERIFY, 0);
  22734. if (ret != 0) {
  22735. FreeDecodedCert(decode);
  22736. ERROR_OUT(-10144, exit);
  22737. }
  22738. FreeDecodedCert(decode);
  22739. #endif
  22740. ret = SaveDerAndPem(der, certSz, certEccDerFile, certEccPemFile,
  22741. CERT_TYPE, -6735);
  22742. if (ret != 0) {
  22743. goto exit;
  22744. }
  22745. exit:
  22746. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22747. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22748. #endif
  22749. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22750. if (myCert != NULL)
  22751. XFREE(myCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22752. #ifdef WOLFSSL_TEST_CERT
  22753. if (decode != NULL)
  22754. XFREE(decode, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22755. #endif
  22756. if (caEccKey != NULL) {
  22757. wc_ecc_free(caEccKey);
  22758. XFREE(caEccKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22759. }
  22760. if (certPubKey != NULL) {
  22761. wc_ecc_free(certPubKey);
  22762. XFREE(certPubKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22763. }
  22764. #else
  22765. wc_ecc_free(certPubKey);
  22766. wc_ecc_free(caEccKey);
  22767. #endif
  22768. return ret;
  22769. }
  22770. #endif /* WOLFSSL_CERT_GEN */
  22771. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  22772. !defined(WOLFSSL_NO_MALLOC) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  22773. /* Test for the wc_ecc_key_new() and wc_ecc_key_free() functions. */
  22774. static int ecc_test_allocator(WC_RNG* rng)
  22775. {
  22776. int ret = 0;
  22777. ecc_key* key;
  22778. #ifdef WC_NO_RNG
  22779. word32 idx = 0;
  22780. #endif
  22781. key = wc_ecc_key_new(HEAP_HINT);
  22782. if (key == NULL) {
  22783. ERROR_OUT(-10150, exit);
  22784. }
  22785. #ifndef WC_NO_RNG
  22786. ret = wc_ecc_make_key(rng, ECC_KEYGEN_SIZE, key);
  22787. #if defined(WOLFSSL_ASYNC_CRYPT)
  22788. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  22789. #endif
  22790. if (ret != 0) {
  22791. ERROR_OUT(-10151, exit);
  22792. }
  22793. #else
  22794. /* use test ECC key */
  22795. ret = wc_EccPrivateKeyDecode(ecc_key_der_256, &idx, key,
  22796. (word32)sizeof_ecc_key_der_256);
  22797. (void)rng;
  22798. #endif
  22799. exit:
  22800. wc_ecc_key_free(key);
  22801. return ret;
  22802. }
  22803. #endif
  22804. /* ECC Non-blocking tests for Sign and Verify */
  22805. /* Requires SP math and supports P384 or P256 */
  22806. /* ./configure --enable-ecc=nonblock --enable-sp=yes,nonblock CFLAGS="-DWOLFSSL_PUBLIC_MP" */
  22807. #if defined(WC_ECC_NONBLOCK) && defined(WOLFSSL_HAVE_SP_ECC) && \
  22808. defined(WOLFSSL_PUBLIC_MP)
  22809. /* ECC Private Key "d" */
  22810. static const byte p256PrivKey[] = {
  22811. /* SECP256R1 */
  22812. /* d */
  22813. 0x1e, 0xe7, 0x70, 0x07, 0xd3, 0x30, 0x94, 0x39,
  22814. 0x28, 0x90, 0xdf, 0x23, 0x88, 0x2c, 0x4a, 0x34,
  22815. 0x15, 0xdb, 0x4c, 0x43, 0xcd, 0xfa, 0xe5, 0x1f,
  22816. 0x3d, 0x4c, 0x37, 0xfe, 0x59, 0x3b, 0x96, 0xd8
  22817. };
  22818. #ifdef HAVE_ECC384
  22819. static const byte p384PrivKey[] = {
  22820. /* SECP384R1 */
  22821. /* d */
  22822. 0xa4, 0xe5, 0x06, 0xe8, 0x06, 0x16, 0x3e, 0xab,
  22823. 0x89, 0xf8, 0x60, 0x43, 0xc0, 0x60, 0x25, 0xdb,
  22824. 0xba, 0x7b, 0xfe, 0x19, 0x35, 0x08, 0x55, 0x65,
  22825. 0x76, 0xe2, 0xdc, 0xe0, 0x01, 0x8b, 0x6b, 0x68,
  22826. 0xdf, 0xcf, 0x6f, 0x80, 0x12, 0xce, 0x79, 0x37,
  22827. 0xeb, 0x2b, 0x9c, 0x7b, 0xc4, 0x68, 0x1c, 0x74
  22828. };
  22829. #endif /* HAVE_ECC384 */
  22830. #ifdef HAVE_ECC521
  22831. static const byte p521PrivKey[] = {
  22832. /* SECP521R1 */
  22833. /* d */
  22834. 0x01, 0x68, 0x91, 0x33, 0x53, 0xe2, 0x90, 0x68,
  22835. 0x11, 0x8f, 0xaa, 0xa8, 0x76, 0x0c, 0xf7, 0x2a,
  22836. 0x07, 0x1b, 0x92, 0x2a, 0xa7, 0x82, 0x3d, 0xfa,
  22837. 0x83, 0xce, 0x70, 0xc8, 0xc2, 0x60, 0x82, 0xfe,
  22838. 0x18, 0x88, 0x68, 0xda, 0x6a, 0x83, 0x46, 0x78,
  22839. 0xe4, 0xe9, 0xe9, 0xcc, 0x51, 0x7f, 0xed, 0x81,
  22840. 0x02, 0x32, 0xee, 0x26, 0x87, 0xcc, 0xed, 0x63,
  22841. 0x3f, 0x39, 0x27, 0xf0, 0xd7, 0x17, 0x77, 0xa1,
  22842. 0xa4, 0x36
  22843. };
  22844. #endif /* HAVE_ECC521 */
  22845. /* ECC public key Qx/Qy */
  22846. static const byte p256PubKey[] = {
  22847. /* SECP256R1 */
  22848. /* Qx */
  22849. 0x96, 0x93, 0x1c, 0x53, 0x0b, 0x43, 0x6c, 0x42,
  22850. 0x0c, 0x52, 0x90, 0xe4, 0xa7, 0xec, 0x98, 0xb1,
  22851. 0xaf, 0xd4, 0x14, 0x49, 0xd8, 0xc1, 0x42, 0x82,
  22852. 0x04, 0x78, 0xd1, 0x90, 0xae, 0xa0, 0x6c, 0x07,
  22853. /* Qy */
  22854. 0xf2, 0x3a, 0xb5, 0x10, 0x32, 0x8d, 0xce, 0x9e,
  22855. 0x76, 0xa0, 0xd2, 0x8c, 0xf3, 0xfc, 0xa9, 0x94,
  22856. 0x43, 0x24, 0xe6, 0x82, 0x00, 0x40, 0xc6, 0xdb,
  22857. 0x1c, 0x2f, 0xcd, 0x38, 0x4b, 0x60, 0xdd, 0x61
  22858. };
  22859. #ifdef HAVE_ECC384
  22860. static const byte p384PubKey[] = {
  22861. /* SECP384R1 */
  22862. /* Qx */
  22863. 0xea, 0xcf, 0x93, 0x4f, 0x2c, 0x09, 0xbb, 0x39,
  22864. 0x14, 0x0f, 0x56, 0x64, 0xc3, 0x40, 0xb4, 0xdf,
  22865. 0x0e, 0x63, 0xae, 0xe5, 0x71, 0x4b, 0x00, 0xcc,
  22866. 0x04, 0x97, 0xff, 0xe1, 0xe9, 0x38, 0x96, 0xbb,
  22867. 0x5f, 0x91, 0xb2, 0x6a, 0xcc, 0xb5, 0x39, 0x5f,
  22868. 0x8f, 0x70, 0x59, 0xf1, 0x01, 0xf6, 0x5a, 0x2b,
  22869. /* Qy */
  22870. 0x01, 0x6c, 0x68, 0x0b, 0xcf, 0x55, 0x25, 0xaf,
  22871. 0x6d, 0x98, 0x48, 0x0a, 0xa8, 0x74, 0xc9, 0xa9,
  22872. 0x17, 0xa0, 0x0c, 0xc3, 0xfb, 0xd3, 0x23, 0x68,
  22873. 0xfe, 0x04, 0x3c, 0x63, 0x50, 0x88, 0x3b, 0xb9,
  22874. 0x4f, 0x7c, 0x67, 0x34, 0xf7, 0x3b, 0xa9, 0x73,
  22875. 0xe7, 0x1b, 0xc3, 0x51, 0x5e, 0x22, 0x18, 0xec
  22876. };
  22877. #endif
  22878. #ifdef HAVE_ECC521
  22879. static const byte p521PubKey[] = {
  22880. /* SECP521R1 */
  22881. /* Qx */
  22882. 0x01, 0x62, 0x6e, 0xf1, 0x00, 0xec, 0xd8, 0x99,
  22883. 0x58, 0x9b, 0x80, 0x6b, 0xfe, 0x2c, 0xf1, 0xb2,
  22884. 0xf0, 0xc8, 0x48, 0xdf, 0xac, 0xd2, 0x3b, 0x71,
  22885. 0x29, 0xab, 0xf0, 0x66, 0x63, 0xd8, 0x8e, 0xb5,
  22886. 0xc8, 0xc2, 0xfc, 0x99, 0x44, 0xe2, 0x45, 0xb1,
  22887. 0x5a, 0x7b, 0xb9, 0x73, 0x01, 0xda, 0x79, 0xec,
  22888. 0x9c, 0x26, 0x27, 0x34, 0x45, 0x26, 0xd5, 0x89,
  22889. 0x4b, 0x44, 0xfe, 0x69, 0x4e, 0x72, 0x14, 0xe3,
  22890. 0x8b, 0xbc,
  22891. /* Qy */
  22892. 0x00, 0x0f, 0x09, 0xa2, 0x03, 0xc3, 0x5a, 0xdc,
  22893. 0x95, 0x82, 0xf6, 0xf9, 0xf6, 0x9c, 0xff, 0xb5,
  22894. 0x6b, 0x75, 0x95, 0x4b, 0xa4, 0x28, 0x5d, 0x9e,
  22895. 0x90, 0x04, 0xd1, 0xc0, 0x1e, 0xd5, 0xfd, 0x43,
  22896. 0x9e, 0x1e, 0x83, 0xc0, 0x11, 0x2b, 0x2b, 0x07,
  22897. 0x6d, 0xa9, 0x7a, 0x10, 0xd7, 0x67, 0xe7, 0x51,
  22898. 0x37, 0x24, 0xd8, 0xbf, 0x03, 0x0d, 0x8b, 0xb5,
  22899. 0x40, 0x5c, 0x4f, 0xd6, 0x13, 0x73, 0x42, 0xbc,
  22900. 0x91, 0xd9
  22901. };
  22902. /* perform verify of signature and hash using public key */
  22903. /* key is public Qx + public Qy */
  22904. /* sig is r + s */
  22905. static int crypto_ecc_verify(const byte *key, uint32_t keySz,
  22906. const byte *hash, uint32_t hashSz, const byte *sig, uint32_t sigSz,
  22907. uint32_t curveSz, int curveId)
  22908. {
  22909. int ret, verify_res = 0, count = 0;
  22910. mp_int r, s;
  22911. ecc_key ecc;
  22912. ecc_nb_ctx_t nb_ctx;
  22913. /* validate arguments */
  22914. if (key == NULL || hash == NULL || sig == NULL || curveSz == 0 ||
  22915. hashSz == 0 || keySz < (curveSz*2) || sigSz < (curveSz*2))
  22916. {
  22917. return BAD_FUNC_ARG;
  22918. }
  22919. /* Setup the ECC key */
  22920. ret = wc_ecc_init(&ecc);
  22921. if (ret < 0) {
  22922. return ret;
  22923. }
  22924. ret = wc_ecc_set_nonblock(&ecc, &nb_ctx);
  22925. if (ret != MP_OKAY) {
  22926. wc_ecc_free(&ecc);
  22927. return ret;
  22928. }
  22929. /* Setup the signature r/s variables */
  22930. ret = mp_init(&r);
  22931. if (ret != MP_OKAY) {
  22932. wc_ecc_free(&ecc);
  22933. return ret;
  22934. }
  22935. ret = mp_init(&s);
  22936. if (ret != MP_OKAY) {
  22937. mp_clear(&r);
  22938. wc_ecc_free(&ecc);
  22939. return ret;
  22940. }
  22941. /* Import public key x/y */
  22942. ret = wc_ecc_import_unsigned(
  22943. &ecc,
  22944. (byte*)key, /* Public "x" Coordinate */
  22945. (byte*)(key + curveSz), /* Public "y" Coordinate */
  22946. NULL, /* Private "d" (optional) */
  22947. curveId /* ECC Curve Id */
  22948. );
  22949. /* Make sure it was a public key imported */
  22950. if (ret == 0 && ecc.type != ECC_PUBLICKEY) {
  22951. ret = ECC_BAD_ARG_E;
  22952. }
  22953. /* Import signature r/s */
  22954. if (ret == 0) {
  22955. ret = mp_read_unsigned_bin(&r, sig, curveSz);
  22956. }
  22957. if (ret == 0) {
  22958. ret = mp_read_unsigned_bin(&s, sig + curveSz, curveSz);
  22959. }
  22960. /* Verify ECC Signature */
  22961. if (ret == 0) {
  22962. do {
  22963. ret = wc_ecc_verify_hash_ex(
  22964. &r, &s, /* r/s as mp_int */
  22965. hash, hashSz, /* computed hash digest */
  22966. &verify_res, /* verification result 1=success */
  22967. &ecc
  22968. );
  22969. count++;
  22970. /* This is where real-time work could be called */
  22971. } while (ret == FP_WOULDBLOCK);
  22972. #ifdef DEBUG_WOLFSSL
  22973. printf("ECC non-block verify: %d times\n", count);
  22974. #endif
  22975. }
  22976. /* check verify result */
  22977. if (ret == 0 && verify_res == 0) {
  22978. ret = SIG_VERIFY_E;
  22979. }
  22980. mp_clear(&r);
  22981. mp_clear(&s);
  22982. wc_ecc_free(&ecc);
  22983. (void)count;
  22984. return ret;
  22985. }
  22986. /* perform signature operation against hash using private key */
  22987. static int crypto_ecc_sign(const byte *key, uint32_t keySz,
  22988. const byte *hash, uint32_t hashSz, byte *sig, uint32_t* sigSz,
  22989. uint32_t curveSz, int curveId, WC_RNG* rng)
  22990. {
  22991. int ret, count = 0;
  22992. mp_int r, s;
  22993. ecc_key ecc;
  22994. ecc_nb_ctx_t nb_ctx;
  22995. /* validate arguments */
  22996. if (key == NULL || hash == NULL || sig == NULL || sigSz == NULL ||
  22997. curveSz == 0 || hashSz == 0 || keySz < curveSz || *sigSz < (curveSz*2))
  22998. {
  22999. return BAD_FUNC_ARG;
  23000. }
  23001. /* Initialize signature result */
  23002. memset(sig, 0, curveSz*2);
  23003. /* Setup the ECC key */
  23004. ret = wc_ecc_init(&ecc);
  23005. if (ret < 0) {
  23006. return ret;
  23007. }
  23008. ret = wc_ecc_set_nonblock(&ecc, &nb_ctx);
  23009. if (ret != MP_OKAY) {
  23010. wc_ecc_free(&ecc);
  23011. return ret;
  23012. }
  23013. /* Setup the signature r/s variables */
  23014. ret = mp_init(&r);
  23015. if (ret != MP_OKAY) {
  23016. wc_ecc_free(&ecc);
  23017. return ret;
  23018. }
  23019. ret = mp_init(&s);
  23020. if (ret != MP_OKAY) {
  23021. mp_clear(&r);
  23022. wc_ecc_free(&ecc);
  23023. return ret;
  23024. }
  23025. /* Import private key "k" */
  23026. ret = wc_ecc_import_private_key_ex(
  23027. key, keySz, /* private key "d" */
  23028. NULL, 0, /* public (optional) */
  23029. &ecc,
  23030. curveId /* ECC Curve Id */
  23031. );
  23032. if (ret == 0) {
  23033. do {
  23034. /* Verify ECC Signature */
  23035. ret = wc_ecc_sign_hash_ex(
  23036. hash, hashSz, /* computed hash digest */
  23037. rng, &ecc, /* random and key context */
  23038. &r, &s /* r/s as mp_int */
  23039. );
  23040. count++;
  23041. /* This is where real-time work could be called */
  23042. } while (ret == FP_WOULDBLOCK);
  23043. #ifdef DEBUG_WOLFSSL
  23044. printf("ECC non-block sign: %d times\n", count);
  23045. #endif
  23046. }
  23047. if (ret == 0) {
  23048. /* export r/s */
  23049. mp_to_unsigned_bin_len(&r, sig, curveSz);
  23050. mp_to_unsigned_bin_len(&s, sig + curveSz, curveSz);
  23051. }
  23052. mp_clear(&r);
  23053. mp_clear(&s);
  23054. wc_ecc_free(&ecc);
  23055. (void)count;
  23056. return ret;
  23057. }
  23058. #endif /* HAVE_ECC_SIGN && HAVE_ECC_VERIFY */
  23059. /*
  23060. * This test doesn't work with WOLFSSL_VALIDATE_ECC_KEYGEN defined because we
  23061. * don't have non-blocking versions of the key checking functions, yet.
  23062. */
  23063. #if defined(HAVE_ECC_DHE) && !defined(WOLFSSL_VALIDATE_ECC_KEYGEN)
  23064. static int ecc_test_nonblock_dhe(int curveId, word32 curveSz,
  23065. const byte* privKey, const byte* pubKey, WC_RNG* rng)
  23066. {
  23067. int ret;
  23068. ecc_key keyA;
  23069. ecc_key keyB;
  23070. ecc_nb_ctx_t nbCtxA;
  23071. ecc_nb_ctx_t nbCtxB;
  23072. byte secretA[ECC_SHARED_SIZE];
  23073. byte secretB[ECC_SHARED_SIZE];
  23074. word32 secretSzA = ECC_SHARED_SIZE;
  23075. word32 secretSzB = ECC_SHARED_SIZE;
  23076. int count = 0;
  23077. ret = wc_ecc_init(&keyA);
  23078. if (ret == 0) {
  23079. ret = wc_ecc_init(&keyB);
  23080. }
  23081. if (ret == 0) {
  23082. ret = wc_ecc_set_nonblock(&keyA, &nbCtxA);
  23083. }
  23084. if (ret == 0) {
  23085. ret = wc_ecc_set_nonblock(&keyB, &nbCtxB);
  23086. }
  23087. if (ret == 0) {
  23088. do {
  23089. ret = wc_ecc_make_key_ex(rng, curveSz, &keyA, curveId);
  23090. count++;
  23091. } while (ret == FP_WOULDBLOCK);
  23092. }
  23093. #ifdef DEBUG_WOLFSSL
  23094. fprintf(stderr, "ECC non-block key gen: %d times\n", count);
  23095. #endif
  23096. if (ret == 0) {
  23097. ret = wc_ecc_check_key(&keyA);
  23098. }
  23099. if (ret == 0) {
  23100. ret = wc_ecc_import_unsigned(&keyB, pubKey, pubKey + curveSz,
  23101. privKey, curveId);
  23102. }
  23103. count = 0;
  23104. if (ret == 0) {
  23105. do {
  23106. ret = wc_ecc_shared_secret(&keyA, &keyB, secretA, &secretSzA);
  23107. count++;
  23108. } while (ret == FP_WOULDBLOCK);
  23109. }
  23110. #ifdef DEBUG_WOLFSSL
  23111. fprintf(stderr, "ECC non-block shared secret: %d times\n", count);
  23112. #endif
  23113. if (ret == 0) {
  23114. do {
  23115. ret = wc_ecc_shared_secret(&keyB, &keyA, secretB, &secretSzB);
  23116. } while (ret == FP_WOULDBLOCK);
  23117. }
  23118. if (ret == 0) {
  23119. if (secretSzA != secretSzB ||
  23120. XMEMCMP(secretA, secretB, secretSzA) != 0) {
  23121. ret = -1;
  23122. }
  23123. }
  23124. wc_ecc_free(&keyA);
  23125. wc_ecc_free(&keyB);
  23126. return ret;
  23127. }
  23128. #endif /* HAVE_ECC_DHE && !WOLFSSL_VALIDATE_ECC_KEYGEN */
  23129. #if defined(HAVE_ECC_SIGN) && defined(HAVE_ECC_VERIFY)
  23130. static int ecc_test_nonblock_ecdsa(int curveId, word32 curveSz,
  23131. const byte* privKey, word32 privKeySz, const byte* pubKey, word32 pubKeySz,
  23132. WC_RNG* rng)
  23133. {
  23134. int ret = 0;
  23135. byte* sig = NULL;
  23136. word32 sigSz = curveSz * 2;
  23137. static const byte hash[] = {
  23138. 0x8d, 0x28, 0xa3, 0x8b, 0x0b, 0xa9, 0xfe, 0xd4, 0x0e, 0x54, 0xc4, 0x17,
  23139. 0x3d, 0x54, 0x66, 0x34, 0xbf, 0x5d, 0x6f, 0x46, 0xc2, 0x20, 0xcb, 0xc3,
  23140. 0x22, 0xe9, 0xb0, 0xdf, 0xe7, 0x64, 0x3f, 0xd9
  23141. };
  23142. sig = (byte*)XMALLOC(sigSz, HEAP_HINT, DYNAMIC_TYPE_SIGNATURE);
  23143. if (sig == NULL) {
  23144. ret = -1;
  23145. }
  23146. if (ret == 0) {
  23147. /* Sign hash using private key */
  23148. /* Note: result of an ECC sign varies for each call even with same
  23149. private key and hash. This is because a new random public key is
  23150. used for each operation. */
  23151. ret = crypto_ecc_sign(privKey, privKeySz, hash, sizeof(hash), sig,
  23152. &sigSz, curveSz, curveId, rng);
  23153. }
  23154. if (ret == 0) {
  23155. /* Verify generated signature is valid */
  23156. ret = crypto_ecc_verify(pubKey, pubKeySz, hash, sizeof(hash), sig,
  23157. sigSz, curveSz, curveId);
  23158. }
  23159. if (sig != NULL) {
  23160. XFREE(sig, HEAP_HINT, DYNAMIC_TYPE_SIGNATURE);
  23161. }
  23162. return ret;
  23163. }
  23164. #endif /* HAVE_ECC_SIGN && HAVE_ECC_VERIFY */
  23165. static int ecc_test_nonblock(WC_RNG* rng)
  23166. {
  23167. int ret = 0;
  23168. word32 i;
  23169. int curveIds[3] = {0, 0, 0};
  23170. word32 curveSzs[3] = {0, 0, 0};
  23171. const byte* privKeys[3] = {NULL, NULL, NULL};
  23172. word32 privKeySzs[3] = {0, 0, 0};
  23173. const byte* pubKeys[3] = {NULL, NULL, NULL};
  23174. word32 pubKeySzs[3] = {0, 0, 0};
  23175. curveIds[0] = ECC_SECP256R1;
  23176. curveSzs[0] = 32;
  23177. privKeys[0] = p256PrivKey;
  23178. privKeySzs[0] = sizeof(p256PrivKey);
  23179. pubKeys[0] = p256PubKey;
  23180. pubKeySzs[0] = sizeof(p256PubKey);
  23181. #ifdef HAVE_ECC384
  23182. curveIds[1] = ECC_SECP384R1;
  23183. curveSzs[1] = 48;
  23184. privKeys[1] = p384PrivKey;
  23185. privKeySzs[1] = sizeof(p384PrivKey);
  23186. pubKeys[1] = p384PubKey;
  23187. pubKeySzs[1] = sizeof(p384PubKey);
  23188. #endif
  23189. #ifdef HAVE_ECC521
  23190. curveIds[2] = ECC_SECP521R1;
  23191. curveSzs[2] = 66;
  23192. privKeys[2] = p521PrivKey;
  23193. privKeySzs[2] = sizeof(p521PrivKey);
  23194. pubKeys[2] = p521PubKey;
  23195. pubKeySzs[2] = sizeof(p521PubKey);
  23196. #endif
  23197. for (i = 0; ret == 0 && i < sizeof(curveIds) / sizeof(curveIds[0]); ++i) {
  23198. if (curveIds[i] == 0) {
  23199. continue;
  23200. }
  23201. #if defined(HAVE_ECC_SIGN) && defined(HAVE_ECC_VERIFY)
  23202. ret = ecc_test_nonblock_ecdsa(curveIds[i], curveSzs[i], privKeys[i],
  23203. privKeySzs[i], pubKeys[i], pubKeySzs[i], rng);
  23204. #endif /* HAVE_ECC_SIGN && HAVE_ECC_VERIFY */
  23205. #if defined(HAVE_ECC_DHE) && !defined(WOLFSSL_VALIDATE_ECC_KEYGEN)
  23206. if (ret == 0) {
  23207. ret = ecc_test_nonblock_dhe(curveIds[i], curveSzs[i], privKeys[i],
  23208. pubKeys[i], rng);
  23209. }
  23210. #endif /* HAVE_ECC_DHE && !WOLFSSL_VALIDATE_ECC_KEYGEN */
  23211. }
  23212. return ret;
  23213. }
  23214. #endif /* WC_ECC_NONBLOCK && WOLFSSL_HAVE_SP_ECC && WOLFSSL_PUBLIC_MP */
  23215. WOLFSSL_TEST_SUBROUTINE int ecc_test(void)
  23216. {
  23217. int ret;
  23218. WC_RNG rng;
  23219. #if defined(WOLFSSL_CERT_EXT) && \
  23220. (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  23221. ret = ecc_decode_test();
  23222. if (ret < 0)
  23223. return ret;
  23224. #endif
  23225. #ifndef HAVE_FIPS
  23226. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  23227. #else
  23228. ret = wc_InitRng(&rng);
  23229. #endif
  23230. #ifndef WC_NO_RNG
  23231. if (ret != 0)
  23232. return -10300;
  23233. #else
  23234. (void)ret;
  23235. #endif
  23236. #if (defined(HAVE_ECC112) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 112
  23237. ret = ecc_test_curve(&rng, 14);
  23238. if (ret < 0) {
  23239. goto done;
  23240. }
  23241. #endif /* HAVE_ECC112 */
  23242. #if (defined(HAVE_ECC128) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 128
  23243. ret = ecc_test_curve(&rng, 16);
  23244. if (ret < 0) {
  23245. goto done;
  23246. }
  23247. #endif /* HAVE_ECC128 */
  23248. #if (defined(HAVE_ECC160) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 160
  23249. ret = ecc_test_curve(&rng, 20);
  23250. if (ret < 0) {
  23251. goto done;
  23252. }
  23253. #endif /* HAVE_ECC160 */
  23254. #if (defined(HAVE_ECC192) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 192
  23255. ret = ecc_test_curve(&rng, 24);
  23256. if (ret < 0) {
  23257. goto done;
  23258. }
  23259. #endif /* HAVE_ECC192 */
  23260. #if (defined(HAVE_ECC224) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 224
  23261. ret = ecc_test_curve(&rng, 28);
  23262. if (ret < 0) {
  23263. goto done;
  23264. }
  23265. #endif /* HAVE_ECC224 */
  23266. #if (defined(HAVE_ECC239) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 239
  23267. ret = ecc_test_curve(&rng, 30);
  23268. if (ret < 0) {
  23269. goto done;
  23270. }
  23271. #endif /* HAVE_ECC239 */
  23272. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  23273. ret = ecc_test_curve(&rng, 32);
  23274. if (ret < 0) {
  23275. goto done;
  23276. }
  23277. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  23278. defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT) && \
  23279. !defined(WOLFSSL_NO_MALLOC) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  23280. ret = ecc_point_test();
  23281. if (ret < 0) {
  23282. goto done;
  23283. }
  23284. #endif
  23285. ret = ecc_def_curve_test(&rng);
  23286. if (ret < 0) {
  23287. goto done;
  23288. }
  23289. #endif /* !NO_ECC256 */
  23290. #if (defined(HAVE_ECC320) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 320
  23291. ret = ecc_test_curve(&rng, 40);
  23292. if (ret < 0) {
  23293. goto done;
  23294. }
  23295. #endif /* HAVE_ECC320 */
  23296. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 384
  23297. ret = ecc_test_curve(&rng, 48);
  23298. if (ret < 0) {
  23299. goto done;
  23300. }
  23301. #endif /* HAVE_ECC384 */
  23302. #if (defined(HAVE_ECC512) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 512
  23303. ret = ecc_test_curve(&rng, 64);
  23304. if (ret < 0) {
  23305. goto done;
  23306. }
  23307. #endif /* HAVE_ECC512 */
  23308. #if (defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 521
  23309. ret = ecc_test_curve(&rng, 66);
  23310. if (ret < 0) {
  23311. goto done;
  23312. }
  23313. #endif /* HAVE_ECC521 */
  23314. #if defined(WOLFSSL_CUSTOM_CURVES)
  23315. ret = ecc_test_custom_curves(&rng);
  23316. if (ret != 0) {
  23317. goto done;
  23318. }
  23319. #endif
  23320. #if defined(HAVE_ECC_SIGN) && (defined(WOLFSSL_ECDSA_DETERMINISTIC_K) || \
  23321. defined(WOLFSSL_ECDSA_DETERMINISTIC_K_VARIANT)) \
  23322. && (!defined(FIPS_VERSION_GE) || FIPS_VERSION_GE(5,3))
  23323. #ifdef HAVE_ECC256
  23324. ret = ecc_test_deterministic_k(&rng);
  23325. if (ret != 0) {
  23326. printf("ecc_test_deterministic_k failed! %d\n", ret);
  23327. goto done;
  23328. }
  23329. #endif
  23330. #ifdef WOLFSSL_PUBLIC_MP
  23331. #if defined(HAVE_ECC384)
  23332. ret = ecc384_test_deterministic_k(&rng);
  23333. if (ret != 0) {
  23334. printf("ecc384_test_deterministic_k failed! %d\n", ret);
  23335. goto done;
  23336. }
  23337. #endif
  23338. #if defined(HAVE_ECC521)
  23339. ret = ecc521_test_deterministic_k(&rng);
  23340. if (ret != 0) {
  23341. printf("ecc512_test_deterministic_k failed! %d\n", ret);
  23342. goto done;
  23343. }
  23344. #endif
  23345. #endif
  23346. #endif
  23347. #if defined(HAVE_ECC_SIGN) && defined(WOLFSSL_ECDSA_SET_K) && \
  23348. !defined(WOLFSSL_KCAPI_ECC)
  23349. ret = ecc_test_sign_vectors(&rng);
  23350. if (ret != 0) {
  23351. printf("ecc_test_sign_vectors failed! %d\n", ret);
  23352. goto done;
  23353. }
  23354. #endif
  23355. #if defined(HAVE_ECC_CDH) && defined(HAVE_ECC_DHE)
  23356. ret = ecc_test_cdh_vectors(&rng);
  23357. if (ret != 0) {
  23358. printf("ecc_test_cdh_vectors failed! %d\n", ret);
  23359. goto done;
  23360. }
  23361. #endif
  23362. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  23363. !defined(WOLFSSL_STM32_PKA) && !defined(WOLFSSL_SILABS_SE_ACCEL) && \
  23364. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  23365. ret = ecc_test_make_pub(&rng);
  23366. if (ret != 0) {
  23367. printf("ecc_test_make_pub failed!: %d\n", ret);
  23368. goto done;
  23369. }
  23370. #elif defined(HAVE_ECC_KEY_IMPORT)
  23371. (void)ecc_test_make_pub; /* for compiler warning */
  23372. #endif
  23373. #ifdef WOLFSSL_CERT_GEN
  23374. ret = ecc_test_cert_gen(&rng);
  23375. if (ret != 0) {
  23376. printf("ecc_test_cert_gen failed!: %d\n", ret);
  23377. goto done;
  23378. }
  23379. #endif
  23380. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && !defined(WOLFSSL_NO_MALLOC) && \
  23381. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  23382. ret = ecc_test_allocator(&rng);
  23383. if (ret != 0) {
  23384. printf("ecc_test_allocator failed!: %d\n", ret);
  23385. goto done;
  23386. }
  23387. #endif
  23388. #if defined(WC_ECC_NONBLOCK) && defined(WOLFSSL_PUBLIC_MP) && \
  23389. defined(HAVE_ECC_SIGN) && defined(HAVE_ECC_VERIFY)
  23390. ret = ecc_test_nonblock(&rng);
  23391. if (ret != 0) {
  23392. printf("ecc_test_nonblock failed!: %d\n", ret);
  23393. goto done;
  23394. }
  23395. #endif
  23396. done:
  23397. wc_FreeRng(&rng);
  23398. return ret;
  23399. }
  23400. #if defined(HAVE_ECC_ENCRYPT) && defined(HAVE_AES_CBC) && \
  23401. (defined(WOLFSSL_AES_128) || defined(WOLFSSL_AES_256))
  23402. /* ecc_encrypt_e2e_test() uses wc_ecc_ctx_set_algo(), which was added in
  23403. * wolfFIPS 5.3.
  23404. * ecc_encrypt_kat() is used only by ecc_encrypt_e2e_test().
  23405. */
  23406. #if !defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3))
  23407. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && \
  23408. ECC_MIN_KEY_SZ <= 256 && defined(WOLFSSL_AES_128)
  23409. static int ecc_encrypt_kat(WC_RNG *rng)
  23410. {
  23411. int ret = 0;
  23412. #ifdef WOLFSSL_ECIES_OLD
  23413. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  23414. ecc_key* userA = NULL;
  23415. #else
  23416. ecc_key userA[1];
  23417. #endif
  23418. int userAInit = 0;
  23419. #endif
  23420. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  23421. ecc_key* userB = NULL;
  23422. #else
  23423. ecc_key userB[1];
  23424. #endif
  23425. int userBInit = 0;
  23426. ecc_key* tmpKey;
  23427. byte plain[48];
  23428. word32 plainSz = sizeof(plain);
  23429. WOLFSSL_SMALL_STACK_STATIC const byte privKey[] = {
  23430. 0x04, 0x80, 0xef, 0x1d, 0xbe, 0x02, 0x0c, 0x20,
  23431. 0x5b, 0xab, 0x80, 0x35, 0x5b, 0x2a, 0x0f, 0x6d,
  23432. 0xd3, 0xb0, 0x7f, 0x7e, 0x7f, 0x86, 0x8a, 0x49,
  23433. 0xee, 0xb4, 0xaa, 0x09, 0x2d, 0x1e, 0x1d, 0x02
  23434. };
  23435. #if defined(WOLFSSL_ECIES_OLD) || defined(WOLFSSL_QNX_CAAM)
  23436. WOLFSSL_SMALL_STACK_STATIC const byte pubKey[] = {
  23437. 0x04,
  23438. /* X */
  23439. 0x50, 0xf2, 0x93, 0xa2, 0x48, 0xa9, 0xc0, 0x5a,
  23440. 0x9a, 0xa7, 0x70, 0x34, 0xb7, 0x7f, 0x4c, 0x3a,
  23441. 0xad, 0xfc, 0xd8, 0xb6, 0x76, 0x0a, 0xe3, 0xc1,
  23442. 0x87, 0x17, 0x07, 0x2d, 0x8d, 0xa3, 0x63, 0xa0,
  23443. /* X */
  23444. 0xc1, 0x27, 0xb2, 0x97, 0x9b, 0x84, 0xe7, 0xcd,
  23445. 0x20, 0x65, 0x8d, 0x2b, 0x6a, 0x93, 0x75, 0xaa,
  23446. 0x8b, 0xe1, 0x3a, 0x7b, 0x24, 0x1a, 0xbe, 0xe8,
  23447. 0x36, 0xd2, 0xe6, 0x34, 0x8a, 0x7a, 0xb3, 0x28
  23448. };
  23449. #endif
  23450. WOLFSSL_SMALL_STACK_STATIC const byte enc_msg[] = {
  23451. #ifdef WOLFSSL_ECIES_OLD
  23452. 0x42, 0x70, 0xbf, 0xf9, 0xf4, 0x7e, 0x4b, 0x9b,
  23453. 0xb5, 0x4c, 0xcc, 0xc5, 0x94, 0xa7, 0xef, 0xaa,
  23454. 0xc3, 0x7c, 0x85, 0xa6, 0x51, 0x6e, 0xd3, 0xfa,
  23455. 0x56, 0xc9, 0x10, 0x4d, 0x14, 0x32, 0x61, 0xb8,
  23456. 0xbb, 0x66, 0x7a, 0xb5, 0xbc, 0x95, 0xf8, 0xca,
  23457. 0xd1, 0x2a, 0x19, 0x51, 0x44, 0xd8, 0x0e, 0x57,
  23458. 0x34, 0xed, 0x45, 0x89, 0x2e, 0x57, 0xbe, 0xd5,
  23459. 0x06, 0x22, 0xd7, 0x13, 0x0a, 0x0e, 0x40, 0x36,
  23460. 0x0d, 0x05, 0x0d, 0xb6, 0xae, 0x61, 0x37, 0x18,
  23461. 0x83, 0x90, 0x0a, 0x27, 0x95, 0x41, 0x8c, 0x45
  23462. #elif defined(WOLFSSL_ECIES_ISO18033)
  23463. 0x04, 0x50, 0xf2, 0x93, 0xa2, 0x48, 0xa9, 0xc0,
  23464. 0x5a, 0x9a, 0xa7, 0x70, 0x34, 0xb7, 0x7f, 0x4c,
  23465. 0x3a, 0xad, 0xfc, 0xd8, 0xb6, 0x76, 0x0a, 0xe3,
  23466. 0xc1, 0x87, 0x17, 0x07, 0x2d, 0x8d, 0xa3, 0x63,
  23467. 0xa0, 0xc1, 0x27, 0xb2, 0x97, 0x9b, 0x84, 0xe7,
  23468. 0xcd, 0x20, 0x65, 0x8d, 0x2b, 0x6a, 0x93, 0x75,
  23469. 0xaa, 0x8b, 0xe1, 0x3a, 0x7b, 0x24, 0x1a, 0xbe,
  23470. 0xe8, 0x36, 0xd2, 0xe6, 0x34, 0x8a, 0x7a, 0xb3,
  23471. 0x28, 0xbb, 0x9f, 0xa8, 0x2d, 0xe1, 0xf1, 0x67,
  23472. 0x45, 0x02, 0x19, 0xdc, 0xc8, 0x24, 0x8b, 0x20,
  23473. 0x02, 0xa0, 0x8f, 0x95, 0x12, 0x55, 0x51, 0xf8,
  23474. 0x03, 0xc4, 0x54, 0x13, 0x98, 0x2d, 0xf0, 0x31,
  23475. 0x51, 0x80, 0x45, 0x24, 0xcb, 0x8b, 0x48, 0xa6,
  23476. 0x8b, 0x8e, 0x97, 0x9c, 0x56, 0x4d, 0x70, 0x00,
  23477. 0x53, 0xd3, 0x47, 0x00, 0x5a, 0x23, 0x8c, 0xf9,
  23478. 0xfd, 0xd2, 0x33, 0x2c, 0x43, 0x6e, 0x9e, 0xb2,
  23479. 0xf4, 0x95, 0xd4, 0xcf, 0x30, 0xd6, 0xa2, 0xc5,
  23480. 0x35, 0x96, 0x6a, 0xd4, 0x36, 0x15, 0xa9, 0xbd,
  23481. 0x7f
  23482. #elif defined(WOLFSSL_ECIES_GEN_IV)
  23483. /* EC P-256 point */
  23484. 0x04,
  23485. /* X */
  23486. 0x50, 0xf2, 0x93, 0xa2, 0x48, 0xa9, 0xc0, 0x5a,
  23487. 0x9a, 0xa7, 0x70, 0x34, 0xb7, 0x7f, 0x4c, 0x3a,
  23488. 0xad, 0xfc, 0xd8, 0xb6, 0x76, 0x0a, 0xe3, 0xc1,
  23489. 0x87, 0x17, 0x07, 0x2d, 0x8d, 0xa3, 0x63, 0xa0,
  23490. /* Y */
  23491. 0xc1, 0x27, 0xb2, 0x97, 0x9b, 0x84, 0xe7, 0xcd,
  23492. 0x20, 0x65, 0x8d, 0x2b, 0x6a, 0x93, 0x75, 0xaa,
  23493. 0x8b, 0xe1, 0x3a, 0x7b, 0x24, 0x1a, 0xbe, 0xe8,
  23494. 0x36, 0xd2, 0xe6, 0x34, 0x8a, 0x7a, 0xb3, 0x28,
  23495. /* IV */
  23496. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  23497. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  23498. /* Encrypted Msg */
  23499. 0xe5, 0x17, 0xaf, 0x0d, 0x65, 0x4d, 0x3d, 0x50,
  23500. 0x96, 0x05, 0xc9, 0x63, 0x2c, 0xef, 0x1c, 0x1f,
  23501. 0x78, 0xc9, 0x90, 0x7a, 0x14, 0x00, 0xfc, 0x44,
  23502. 0x71, 0x6d, 0x57, 0x8c, 0xdf, 0x23, 0xca, 0x65,
  23503. 0xcf, 0x93, 0x06, 0xb6, 0x9a, 0xf4, 0x61, 0xbd,
  23504. 0x44, 0x1a, 0xeb, 0x52, 0x68, 0x0f, 0xd1, 0xde,
  23505. /* HMAC */
  23506. 0x5a, 0x22, 0xc1, 0x5d, 0x99, 0x66, 0x3f, 0x24,
  23507. 0x35, 0x96, 0xac, 0xf7, 0xf6, 0x28, 0x45, 0x16,
  23508. 0x52, 0x19, 0x0d, 0xe4, 0xb2, 0xca, 0x5b, 0x28,
  23509. 0x4e, 0xbb, 0xf3, 0x98, 0x57, 0xd7, 0x3b, 0xe2
  23510. #else
  23511. 0x04, 0x50, 0xf2, 0x93, 0xa2, 0x48, 0xa9, 0xc0,
  23512. 0x5a, 0x9a, 0xa7, 0x70, 0x34, 0xb7, 0x7f, 0x4c,
  23513. 0x3a, 0xad, 0xfc, 0xd8, 0xb6, 0x76, 0x0a, 0xe3,
  23514. 0xc1, 0x87, 0x17, 0x07, 0x2d, 0x8d, 0xa3, 0x63,
  23515. 0xa0, 0xc1, 0x27, 0xb2, 0x97, 0x9b, 0x84, 0xe7,
  23516. 0xcd, 0x20, 0x65, 0x8d, 0x2b, 0x6a, 0x93, 0x75,
  23517. 0xaa, 0x8b, 0xe1, 0x3a, 0x7b, 0x24, 0x1a, 0xbe,
  23518. 0xe8, 0x36, 0xd2, 0xe6, 0x34, 0x8a, 0x7a, 0xb3,
  23519. 0x28, 0xe5, 0x17, 0xaf, 0x0d, 0x65, 0x4d, 0x3d,
  23520. 0x50, 0x96, 0x05, 0xc9, 0x63, 0x2c, 0xef, 0x1c,
  23521. 0x1f, 0x78, 0xc9, 0x90, 0x7a, 0x14, 0x00, 0xfc,
  23522. 0x44, 0x71, 0x6d, 0x57, 0x8c, 0xdf, 0x23, 0xca,
  23523. 0x65, 0xcf, 0x93, 0x06, 0xb6, 0x9a, 0xf4, 0x61,
  23524. 0xbd, 0x44, 0x1a, 0xeb, 0x52, 0x68, 0x0f, 0xd1,
  23525. 0xde, 0xc7, 0x3f, 0x6f, 0xce, 0xbe, 0x49, 0x61,
  23526. 0x48, 0x01, 0x77, 0x41, 0xd0, 0xd8, 0x5b, 0x48,
  23527. 0xca, 0x4e, 0x47, 0x3e, 0x47, 0xbf, 0x1d, 0x28,
  23528. 0x4c, 0x18, 0x1a, 0xfb, 0x96, 0x95, 0xda, 0xde,
  23529. 0x55
  23530. #endif
  23531. };
  23532. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = {
  23533. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  23534. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  23535. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  23536. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
  23537. 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  23538. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f
  23539. };
  23540. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  23541. userB = (ecc_key *)XMALLOC(sizeof(*userB), HEAP_HINT,
  23542. DYNAMIC_TYPE_TMP_BUFFER);
  23543. if (userB == NULL) {
  23544. ret = -10451;
  23545. }
  23546. #ifdef WOLFSSL_ECIES_OLD
  23547. if (ret == 0) {
  23548. userA = (ecc_key *)XMALLOC(sizeof(*userA), HEAP_HINT,
  23549. DYNAMIC_TYPE_TMP_BUFFER);
  23550. if (userA == NULL) {
  23551. ret = -10450;
  23552. }
  23553. }
  23554. #endif
  23555. #endif
  23556. if (ret == 0) {
  23557. ret = wc_ecc_init_ex(userB, HEAP_HINT, devId);
  23558. if (ret != 0)
  23559. ret = -10453;
  23560. }
  23561. if (ret == 0) {
  23562. userBInit = 1;
  23563. #ifdef WOLFSSL_ECIES_OLD
  23564. ret = wc_ecc_init_ex(userA, HEAP_HINT, devId);
  23565. if (ret != 0)
  23566. ret = -10452;
  23567. }
  23568. if (ret == 0) {
  23569. userAInit = 1;
  23570. tmpKey = userA;
  23571. #else
  23572. tmpKey = NULL;
  23573. #endif
  23574. }
  23575. if (ret == 0) {
  23576. #ifdef WOLFSSL_QNX_CAAM
  23577. ret = wc_ecc_import_private_key_ex(privKey, sizeof(privKey), pubKey,
  23578. sizeof(pubKey), userB, ECC_SECP256R1);
  23579. #else
  23580. ret = wc_ecc_import_private_key_ex(privKey, sizeof(privKey), NULL, 0,
  23581. userB, ECC_SECP256R1);
  23582. #endif
  23583. if (ret != 0)
  23584. ret = -10454;
  23585. }
  23586. #ifdef WOLFSSL_ECIES_OLD
  23587. if (ret == 0) {
  23588. ret = wc_ecc_import_x963_ex(pubKey, sizeof(pubKey), userA,
  23589. ECC_SECP256R1);
  23590. if (ret != 0)
  23591. ret = -10455;
  23592. }
  23593. #endif
  23594. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  23595. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  23596. !defined(HAVE_SELFTEST)
  23597. if (ret == 0) {
  23598. ret = wc_ecc_set_rng(userB, rng);
  23599. if (ret != 0) {
  23600. ret = -10456;
  23601. }
  23602. }
  23603. #else
  23604. (void)rng;
  23605. #endif
  23606. if (ret == 0) {
  23607. ret = wc_ecc_decrypt(userB, tmpKey, enc_msg, sizeof(enc_msg), plain,
  23608. &plainSz, NULL);
  23609. if (ret != 0)
  23610. ret = -10457;
  23611. }
  23612. if (ret == 0) {
  23613. if (XMEMCMP(plain, msg, sizeof(msg)) != 0) {
  23614. ret = -10458;
  23615. }
  23616. }
  23617. if (userBInit)
  23618. wc_ecc_free(userB);
  23619. #ifdef WOLFSSL_ECIES_OLD
  23620. if (userAInit)
  23621. wc_ecc_free(userA);
  23622. #endif
  23623. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  23624. if (userB != NULL) {
  23625. XFREE(userB, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23626. }
  23627. #ifdef WOLFSSL_ECIES_OLD
  23628. if (userA != NULL) {
  23629. XFREE(userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23630. }
  23631. #endif
  23632. #endif
  23633. return ret;
  23634. }
  23635. #endif
  23636. static int ecc_encrypt_e2e_test(WC_RNG* rng, ecc_key* userA, ecc_key* userB,
  23637. byte encAlgo, byte kdfAlgo, byte macAlgo)
  23638. {
  23639. int ret = 0;
  23640. byte msg[48];
  23641. byte plain[48];
  23642. #ifdef WOLFSSL_ECIES_OLD
  23643. byte out[80];
  23644. #elif defined(WOLFSSL_ECIES_GEN_IV)
  23645. byte out[1 + ECC_KEYGEN_SIZE * 2 + 16 + 80];
  23646. #else
  23647. byte out[1 + ECC_KEYGEN_SIZE * 2 + 80];
  23648. #endif
  23649. word32 outSz = sizeof(out);
  23650. word32 plainSz = sizeof(plain);
  23651. int i;
  23652. ecEncCtx* cliCtx = NULL;
  23653. ecEncCtx* srvCtx = NULL;
  23654. byte cliSalt[EXCHANGE_SALT_SZ];
  23655. byte srvSalt[EXCHANGE_SALT_SZ];
  23656. const byte* tmpSalt;
  23657. byte msg2[48];
  23658. byte plain2[48];
  23659. #ifdef WOLFSSL_ECIES_OLD
  23660. byte out2[80];
  23661. #elif defined(WOLFSSL_ECIES_GEN_IV)
  23662. byte out2[1 + ECC_KEYGEN_SIZE * 2 + 16 + 80];
  23663. #else
  23664. byte out2[1 + ECC_KEYGEN_SIZE * 2 + 80];
  23665. #endif
  23666. word32 outSz2 = sizeof(out2);
  23667. word32 plainSz2 = sizeof(plain2);
  23668. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  23669. ecc_key *tmpKey = (ecc_key *)XMALLOC(sizeof(ecc_key), HEAP_HINT,
  23670. DYNAMIC_TYPE_TMP_BUFFER);
  23671. #else
  23672. ecc_key tmpKey[1];
  23673. #endif
  23674. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  23675. if (tmpKey == NULL) {
  23676. ERROR_OUT(MEMORY_E, done);
  23677. }
  23678. #endif
  23679. ret = wc_ecc_init_ex(tmpKey, HEAP_HINT, devId);
  23680. if (ret != 0)
  23681. goto done;
  23682. /* set message to incrementing 0,1,2,etc... */
  23683. for (i = 0; i < (int)sizeof(msg); i++)
  23684. msg[i] = i;
  23685. /* encrypt msg to B */
  23686. ret = wc_ecc_encrypt(userA, userB, msg, sizeof(msg), out, &outSz, NULL);
  23687. if (ret != 0) {
  23688. ret = -10405; goto done;
  23689. }
  23690. #ifdef WOLFSSL_ECIES_OLD
  23691. tmpKey->dp = userA->dp;
  23692. ret = wc_ecc_copy_point(&userA->pubkey, &tmpKey->pubkey);
  23693. if (ret != 0) {
  23694. ret = -10413; goto done;
  23695. }
  23696. #endif
  23697. /* decrypt msg from A */
  23698. ret = wc_ecc_decrypt(userB, tmpKey, out, outSz, plain, &plainSz, NULL);
  23699. if (ret != 0) {
  23700. ret = -10406; goto done;
  23701. }
  23702. if (XMEMCMP(plain, msg, sizeof(msg)) != 0) {
  23703. ret = -10407; goto done;
  23704. }
  23705. #ifndef WOLFSSL_ECIES_OLD
  23706. /* A decrypts msg (response) from B */
  23707. ret = wc_ecc_decrypt(userB, NULL, out, outSz, plain2, &plainSz2, NULL);
  23708. if (ret != 0)
  23709. goto done;
  23710. if (XMEMCMP(plain, msg, sizeof(msg)) != 0) {
  23711. ret = -10415; goto done;
  23712. }
  23713. #endif
  23714. /* let's verify message exchange works, A is client, B is server */
  23715. cliCtx = wc_ecc_ctx_new(REQ_RESP_CLIENT, rng);
  23716. srvCtx = wc_ecc_ctx_new(REQ_RESP_SERVER, rng);
  23717. if (cliCtx == NULL || srvCtx == NULL) {
  23718. ret = -10408; goto done;
  23719. }
  23720. ret = wc_ecc_ctx_set_algo(cliCtx, encAlgo, kdfAlgo, macAlgo);
  23721. if (ret != 0)
  23722. goto done;
  23723. ret = wc_ecc_ctx_set_algo(srvCtx, encAlgo, kdfAlgo, macAlgo);
  23724. if (ret != 0)
  23725. goto done;
  23726. /* get salt to send to peer */
  23727. tmpSalt = wc_ecc_ctx_get_own_salt(cliCtx);
  23728. if (tmpSalt == NULL) {
  23729. ret = -10409; goto done;
  23730. }
  23731. XMEMCPY(cliSalt, tmpSalt, EXCHANGE_SALT_SZ);
  23732. tmpSalt = wc_ecc_ctx_get_own_salt(srvCtx);
  23733. if (tmpSalt == NULL) {
  23734. ret = -10410; goto done;
  23735. }
  23736. XMEMCPY(srvSalt, tmpSalt, EXCHANGE_SALT_SZ);
  23737. /* in actual use, we'd get the peer's salt over the transport */
  23738. ret = wc_ecc_ctx_set_peer_salt(cliCtx, srvSalt);
  23739. if (ret != 0)
  23740. goto done;
  23741. ret = wc_ecc_ctx_set_peer_salt(srvCtx, cliSalt);
  23742. if (ret != 0)
  23743. goto done;
  23744. ret = wc_ecc_ctx_set_info(cliCtx, (byte*)"wolfSSL MSGE", 11);
  23745. if (ret != 0)
  23746. goto done;
  23747. ret = wc_ecc_ctx_set_info(srvCtx, (byte*)"wolfSSL MSGE", 11);
  23748. if (ret != 0)
  23749. goto done;
  23750. /* get encrypted msg (request) to send to B */
  23751. outSz = sizeof(out);
  23752. ret = wc_ecc_encrypt(userA, userB, msg, sizeof(msg), out, &outSz,cliCtx);
  23753. if (ret != 0)
  23754. goto done;
  23755. #ifndef WOLFSSL_ECIES_OLD
  23756. wc_ecc_free(tmpKey);
  23757. #endif
  23758. /* B decrypts msg (request) from A */
  23759. plainSz = sizeof(plain);
  23760. ret = wc_ecc_decrypt(userB, tmpKey, out, outSz, plain, &plainSz, srvCtx);
  23761. if (ret != 0)
  23762. goto done;
  23763. if (XMEMCMP(plain, msg, sizeof(msg)) != 0) {
  23764. ret = -10411; goto done;
  23765. }
  23766. /* msg2 (response) from B to A */
  23767. for (i = 0; i < (int)sizeof(msg2); i++)
  23768. msg2[i] = i + sizeof(msg2);
  23769. /* get encrypted msg (response) to send to B */
  23770. ret = wc_ecc_encrypt(userB, userA, msg2, sizeof(msg2), out2,
  23771. &outSz2, srvCtx);
  23772. if (ret != 0)
  23773. goto done;
  23774. #ifdef WOLFSSL_ECIES_OLD
  23775. tmpKey->dp = userB->dp;
  23776. ret = wc_ecc_copy_point(&userB->pubkey, &tmpKey->pubkey);
  23777. if (ret != 0) {
  23778. ret = -10414; goto done;
  23779. }
  23780. #else
  23781. wc_ecc_free(tmpKey);
  23782. #endif
  23783. /* A decrypts msg (response) from B */
  23784. ret = wc_ecc_decrypt(userA, tmpKey, out2, outSz2, plain2, &plainSz2,
  23785. cliCtx);
  23786. if (ret != 0)
  23787. goto done;
  23788. if (XMEMCMP(plain2, msg2, sizeof(msg2)) != 0) {
  23789. ret = -10412; goto done;
  23790. }
  23791. #if defined(HAVE_COMP_KEY) && \
  23792. (! defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3)))
  23793. /* Create new client and server contexts. */
  23794. wc_ecc_ctx_free(srvCtx);
  23795. wc_ecc_ctx_free(cliCtx);
  23796. /* let's verify message exchange works, A is client, B is server */
  23797. cliCtx = wc_ecc_ctx_new(REQ_RESP_CLIENT, rng);
  23798. srvCtx = wc_ecc_ctx_new(REQ_RESP_SERVER, rng);
  23799. if (cliCtx == NULL || srvCtx == NULL) {
  23800. ret = -10416; goto done;
  23801. }
  23802. ret = wc_ecc_ctx_set_algo(cliCtx, encAlgo, kdfAlgo, macAlgo);
  23803. if (ret != 0)
  23804. goto done;
  23805. ret = wc_ecc_ctx_set_algo(srvCtx, encAlgo, kdfAlgo, macAlgo);
  23806. if (ret != 0)
  23807. goto done;
  23808. /* get salt to send to peer */
  23809. tmpSalt = wc_ecc_ctx_get_own_salt(cliCtx);
  23810. if (tmpSalt == NULL) {
  23811. ret = -10417; goto done;
  23812. }
  23813. XMEMCPY(cliSalt, tmpSalt, EXCHANGE_SALT_SZ);
  23814. tmpSalt = wc_ecc_ctx_get_own_salt(srvCtx);
  23815. if (tmpSalt == NULL) {
  23816. ret = -10418; goto done;
  23817. }
  23818. XMEMCPY(srvSalt, tmpSalt, EXCHANGE_SALT_SZ);
  23819. /* in actual use, we'd get the peer's salt over the transport */
  23820. ret = wc_ecc_ctx_set_peer_salt(cliCtx, srvSalt);
  23821. if (ret != 0)
  23822. goto done;
  23823. ret = wc_ecc_ctx_set_peer_salt(srvCtx, cliSalt);
  23824. if (ret != 0)
  23825. goto done;
  23826. ret = wc_ecc_ctx_set_info(cliCtx, (byte*)"wolfSSL MSGE", 12);
  23827. if (ret != 0)
  23828. goto done;
  23829. ret = wc_ecc_ctx_set_info(srvCtx, (byte*)"wolfSSL MSGE", 12);
  23830. if (ret != 0)
  23831. goto done;
  23832. /* get encrypted msg (request) to send to B - compressed public key */
  23833. outSz = sizeof(out);
  23834. ret = wc_ecc_encrypt_ex(userA, userB, msg, sizeof(msg), out, &outSz, cliCtx,
  23835. 1);
  23836. if (ret != 0)
  23837. goto done;
  23838. #ifndef WOLFSSL_ECIES_OLD
  23839. wc_ecc_free(tmpKey);
  23840. #endif
  23841. /* B decrypts msg (request) from A - out has a compressed public key */
  23842. plainSz = sizeof(plain);
  23843. ret = wc_ecc_decrypt(userB, tmpKey, out, outSz, plain, &plainSz, srvCtx);
  23844. if (ret != 0)
  23845. goto done;
  23846. if (XMEMCMP(plain, msg, sizeof(msg)) != 0) {
  23847. ret = -10419; goto done;
  23848. }
  23849. #endif /* HAVE_COMP_KEY && (!FIPS || FIPS>=5.3) */
  23850. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && \
  23851. (ECC_MIN_KEY_SZ <= 256) && defined(WOLFSSL_AES_128)
  23852. ret = ecc_encrypt_kat(rng);
  23853. #endif
  23854. done:
  23855. /* cleanup */
  23856. wc_ecc_ctx_free(srvCtx);
  23857. wc_ecc_ctx_free(cliCtx);
  23858. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  23859. if (tmpKey != NULL) {
  23860. wc_ecc_free(tmpKey);
  23861. XFREE(tmpKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23862. }
  23863. #else
  23864. wc_ecc_free(tmpKey);
  23865. #endif
  23866. return ret;
  23867. }
  23868. #endif /* !HAVE_FIPS || FIPS_VERSION_GE(5,3) */
  23869. WOLFSSL_TEST_SUBROUTINE int ecc_encrypt_test(void)
  23870. {
  23871. WC_RNG rng;
  23872. int ret;
  23873. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  23874. ecc_key *userA;
  23875. ecc_key *userB;
  23876. #else
  23877. ecc_key userA[1];
  23878. ecc_key userB[1];
  23879. #endif
  23880. #ifndef HAVE_FIPS
  23881. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  23882. #else
  23883. ret = wc_InitRng(&rng);
  23884. #endif
  23885. if (ret != 0)
  23886. return -10400;
  23887. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  23888. userA = (ecc_key *)XMALLOC(sizeof *userA, HEAP_HINT,
  23889. DYNAMIC_TYPE_TMP_BUFFER);
  23890. userB = (ecc_key *)XMALLOC(sizeof *userB, HEAP_HINT,
  23891. DYNAMIC_TYPE_TMP_BUFFER);
  23892. if ((userA == NULL) || (userB == NULL)) {
  23893. ERROR_OUT(MEMORY_E, done);
  23894. }
  23895. #endif
  23896. XMEMSET(userA, 0, sizeof *userA);
  23897. XMEMSET(userB, 0, sizeof *userB);
  23898. ret = wc_ecc_init_ex(userA, HEAP_HINT, devId);
  23899. if (ret != 0)
  23900. goto done;
  23901. ret = wc_ecc_init_ex(userB, HEAP_HINT, devId);
  23902. if (ret != 0)
  23903. goto done;
  23904. ret = wc_ecc_make_key(&rng, ECC_KEYGEN_SIZE, userA);
  23905. #if defined(WOLFSSL_ASYNC_CRYPT)
  23906. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_NONE);
  23907. #endif
  23908. if (ret != 0){
  23909. ret = -10401; goto done;
  23910. }
  23911. ret = wc_ecc_make_key(&rng, ECC_KEYGEN_SIZE, userB);
  23912. #if defined(WOLFSSL_ASYNC_CRYPT)
  23913. ret = wc_AsyncWait(ret, &userB->asyncDev, WC_ASYNC_FLAG_NONE);
  23914. #endif
  23915. if (ret != 0){
  23916. ret = -10402; goto done;
  23917. }
  23918. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  23919. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  23920. !defined(HAVE_SELFTEST)
  23921. ret = wc_ecc_set_rng(userA, &rng);
  23922. if (ret != 0) {
  23923. ret = -10403; goto done;
  23924. }
  23925. ret = wc_ecc_set_rng(userB, &rng);
  23926. if (ret != 0) {
  23927. ret = -10404; goto done;
  23928. }
  23929. #endif
  23930. #if !defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3))
  23931. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  23932. #ifdef WOLFSSL_AES_128
  23933. if (ret == 0) {
  23934. ret = ecc_encrypt_e2e_test(&rng, userA, userB, ecAES_128_CBC,
  23935. ecHKDF_SHA256, ecHMAC_SHA256);
  23936. if (ret != 0) {
  23937. printf("ECIES: AES_128_CBC, HKDF_SHA256, HMAC_SHA256\n");
  23938. }
  23939. }
  23940. #endif
  23941. #ifdef WOLFSSL_AES_256
  23942. if (ret == 0) {
  23943. ret = ecc_encrypt_e2e_test(&rng, userA, userB, ecAES_256_CBC,
  23944. ecHKDF_SHA256, ecHMAC_SHA256);
  23945. if (ret != 0) {
  23946. printf("ECIES: AES_256_CBC, HKDF_SHA256, HMAC_SHA256\n");
  23947. }
  23948. }
  23949. #endif
  23950. #endif
  23951. #if !defined(NO_AES) && defined(WOLFSSL_AES_COUNTER)
  23952. #ifdef WOLFSSL_AES_128
  23953. if (ret == 0) {
  23954. ret = ecc_encrypt_e2e_test(&rng, userA, userB, ecAES_128_CTR,
  23955. ecHKDF_SHA256, ecHMAC_SHA256);
  23956. if (ret != 0) {
  23957. printf("ECIES: AES_128_CTR, HKDF_SHA256, HMAC_SHA256\n");
  23958. }
  23959. }
  23960. #endif
  23961. #ifdef WOLFSSL_AES_256
  23962. if (ret == 0) {
  23963. ret = ecc_encrypt_e2e_test(&rng, userA, userB, ecAES_256_CTR,
  23964. ecHKDF_SHA256, ecHMAC_SHA256);
  23965. if (ret != 0) {
  23966. printf("ECIES: AES_256_CTR, HKDF_SHA256, HMAC_SHA256\n");
  23967. }
  23968. }
  23969. #endif
  23970. #endif
  23971. #endif /* !HAVE_FIPS || FIPS_VERSION_GE(5,3) */
  23972. done:
  23973. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  23974. if (userA != NULL) {
  23975. wc_ecc_free(userA);
  23976. XFREE(userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23977. }
  23978. if (userB != NULL) {
  23979. wc_ecc_free(userB);
  23980. XFREE(userB, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23981. }
  23982. #else
  23983. wc_ecc_free(userB);
  23984. wc_ecc_free(userA);
  23985. #endif
  23986. wc_FreeRng(&rng);
  23987. return ret;
  23988. }
  23989. #endif /* HAVE_ECC_ENCRYPT && HAVE_AES_CBC && WOLFSSL_AES_128 */
  23990. #if defined(USE_CERT_BUFFERS_256) && !defined(WOLFSSL_ATECC508A) && \
  23991. !defined(WOLFSSL_ATECC608A) && !defined(NO_ECC256) && \
  23992. defined(HAVE_ECC_VERIFY) && defined(HAVE_ECC_SIGN) && \
  23993. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  23994. WOLFSSL_TEST_SUBROUTINE int ecc_test_buffers(void)
  23995. {
  23996. size_t bytes;
  23997. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  23998. ecc_key *cliKey = (ecc_key *)XMALLOC(sizeof *cliKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23999. ecc_key *servKey = (ecc_key *)XMALLOC(sizeof *servKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24000. ecc_key *tmpKey = (ecc_key *)XMALLOC(sizeof *tmpKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24001. #else
  24002. ecc_key cliKey[1];
  24003. ecc_key servKey[1];
  24004. ecc_key tmpKey[1];
  24005. #endif
  24006. WC_RNG rng;
  24007. word32 idx = 0;
  24008. int ret;
  24009. /* pad our test message to 32 bytes so evenly divisible by AES_BLOCK_SZ */
  24010. byte in[] = "Everyone gets Friday off. ecc p";
  24011. word32 inLen = (word32)XSTRLEN((char*)in);
  24012. byte out[256];
  24013. byte plain[256];
  24014. int verify = 0;
  24015. word32 x;
  24016. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  24017. if ((cliKey == NULL) || (servKey == NULL) || (tmpKey == NULL))
  24018. ERROR_OUT(MEMORY_E, done);
  24019. #endif
  24020. ret = wc_ecc_init_ex(cliKey, HEAP_HINT, devId);
  24021. if (ret != 0)
  24022. ERROR_OUT(-10420, done);
  24023. ret = wc_ecc_init_ex(servKey, HEAP_HINT, devId);
  24024. if (ret != 0)
  24025. ERROR_OUT(-10421, done);
  24026. ret = wc_ecc_init_ex(tmpKey, HEAP_HINT, devId);
  24027. if (ret != 0)
  24028. ERROR_OUT(-10421, done);
  24029. bytes = (size_t)sizeof_ecc_clikey_der_256;
  24030. /* place client key into ecc_key struct cliKey */
  24031. ret = wc_EccPrivateKeyDecode(ecc_clikey_der_256, &idx, cliKey,
  24032. (word32)bytes);
  24033. if (ret != 0)
  24034. ERROR_OUT(-10422, done);
  24035. idx = 0;
  24036. bytes = (size_t)sizeof_ecc_key_der_256;
  24037. /* place server key into ecc_key struct servKey */
  24038. ret = wc_EccPrivateKeyDecode(ecc_key_der_256, &idx, servKey,
  24039. (word32)bytes);
  24040. if (ret != 0)
  24041. ERROR_OUT(-10423, done);
  24042. #ifndef WC_NO_RNG
  24043. #ifndef HAVE_FIPS
  24044. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  24045. #else
  24046. ret = wc_InitRng(&rng);
  24047. #endif
  24048. if (ret != 0)
  24049. ERROR_OUT(-10424, done);
  24050. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  24051. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  24052. !defined(HAVE_SELFTEST)
  24053. ret = wc_ecc_set_rng(cliKey, &rng);
  24054. if (ret != 0) {
  24055. ERROR_OUT(-10425, done);
  24056. }
  24057. ret = wc_ecc_set_rng(servKey, &rng);
  24058. if (ret != 0) {
  24059. ERROR_OUT(-10425, done);
  24060. }
  24061. #endif
  24062. #endif /* !WC_NO_RNG */
  24063. #if defined(HAVE_ECC_ENCRYPT) && defined(HAVE_HKDF) && \
  24064. defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  24065. {
  24066. word32 y;
  24067. /* test encrypt and decrypt if they're available */
  24068. x = sizeof(out);
  24069. ret = wc_ecc_encrypt(cliKey, servKey, in, sizeof(in), out, &x, NULL);
  24070. if (ret < 0)
  24071. ERROR_OUT(-10426, done);
  24072. #ifdef WOLFSSL_ECIES_OLD
  24073. tmpKey->dp = cliKey->dp;
  24074. ret = wc_ecc_copy_point(&cliKey->pubkey, &tmpKey->pubkey);
  24075. if (ret != 0) {
  24076. ret = -10414; goto done;
  24077. }
  24078. #endif
  24079. y = sizeof(plain);
  24080. ret = wc_ecc_decrypt(servKey, tmpKey, out, x, plain, &y, NULL);
  24081. if (ret < 0)
  24082. ERROR_OUT(-10427, done);
  24083. if (XMEMCMP(plain, in, inLen))
  24084. ERROR_OUT(-10428, done);
  24085. }
  24086. #endif
  24087. x = sizeof(out);
  24088. do {
  24089. #if defined(WOLFSSL_ASYNC_CRYPT)
  24090. ret = wc_AsyncWait(ret, &cliKey->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  24091. #endif
  24092. if (ret == 0)
  24093. ret = wc_ecc_sign_hash(in, inLen, out, &x, &rng, cliKey);
  24094. } while (ret == WC_PENDING_E);
  24095. if (ret < 0)
  24096. ERROR_OUT(-10429, done);
  24097. TEST_SLEEP();
  24098. XMEMSET(plain, 0, sizeof(plain));
  24099. do {
  24100. #if defined(WOLFSSL_ASYNC_CRYPT)
  24101. ret = wc_AsyncWait(ret, &cliKey->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  24102. #endif
  24103. if (ret == 0)
  24104. ret = wc_ecc_verify_hash(out, x, in, inLen, &verify,
  24105. cliKey);
  24106. } while (ret == WC_PENDING_E);
  24107. if (ret < 0)
  24108. ERROR_OUT(-10430, done);
  24109. if (verify != 1)
  24110. ERROR_OUT(-10431, done);
  24111. TEST_SLEEP();
  24112. #ifdef WOLFSSL_CERT_EXT
  24113. idx = 0;
  24114. bytes = sizeof_ecc_clikeypub_der_256;
  24115. ret = wc_EccPublicKeyDecode(ecc_clikeypub_der_256, &idx, cliKey,
  24116. (word32) bytes);
  24117. if (ret != 0)
  24118. ERROR_OUT(-10432, done);
  24119. #endif
  24120. ret = 0;
  24121. done:
  24122. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  24123. if (cliKey != NULL) {
  24124. wc_ecc_free(cliKey);
  24125. XFREE(cliKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24126. }
  24127. if (servKey != NULL) {
  24128. wc_ecc_free(servKey);
  24129. XFREE(servKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24130. }
  24131. if (tmpKey != NULL) {
  24132. wc_ecc_free(tmpKey);
  24133. XFREE(tmpKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24134. }
  24135. #else
  24136. wc_ecc_free(cliKey);
  24137. wc_ecc_free(servKey);
  24138. wc_ecc_free(tmpKey);
  24139. #endif
  24140. wc_FreeRng(&rng);
  24141. return ret;
  24142. }
  24143. #endif /* USE_CERT_BUFFERS_256 && !WOLFSSL_ATECCX08A && !NO_ECC256 */
  24144. #endif /* HAVE_ECC */
  24145. #ifdef HAVE_CURVE25519
  24146. #if defined(HAVE_CURVE25519_SHARED_SECRET) && \
  24147. defined(HAVE_CURVE25519_KEY_IMPORT)
  24148. #ifdef CURVE25519_OVERFLOW_ALL_TESTS
  24149. #define X25519_TEST_CNT 5
  24150. #else
  24151. #define X25519_TEST_CNT 1
  24152. #endif
  24153. static int curve25519_overflow_test(void)
  24154. {
  24155. /* secret key for party a */
  24156. byte sa[X25519_TEST_CNT][32] = {
  24157. {
  24158. 0x8d,0xaf,0x6e,0x7a,0xc1,0xeb,0x8d,0x30,
  24159. 0x99,0x86,0xd3,0x90,0x47,0x96,0x21,0x3c,
  24160. 0x3a,0x75,0xc0,0x7b,0x75,0x01,0x75,0xa3,
  24161. 0x81,0x4b,0xff,0x5a,0xbc,0x96,0x87,0x28
  24162. },
  24163. #ifdef CURVE25519_OVERFLOW_ALL_TESTS
  24164. {
  24165. 0x9d,0x63,0x5f,0xce,0xe2,0xe8,0xd7,0xfb,
  24166. 0x68,0x77,0x0e,0x44,0xd1,0xad,0x87,0x2b,
  24167. 0xf4,0x65,0x06,0xb7,0xbb,0xdb,0xbe,0x6e,
  24168. 0x02,0x43,0x24,0xc7,0x3d,0x7b,0x88,0x60
  24169. },
  24170. {
  24171. 0x63,0xbf,0x76,0xa9,0x73,0xa0,0x09,0xb9,
  24172. 0xcc,0xc9,0x4d,0x47,0x2d,0x14,0x0e,0x52,
  24173. 0xa3,0x84,0x55,0xb8,0x7c,0xdb,0xce,0xb1,
  24174. 0xe4,0x5b,0x8a,0xb9,0x30,0xf1,0xa4,0xa0
  24175. },
  24176. {
  24177. 0x63,0xbf,0x76,0xa9,0x73,0xa0,0x09,0xb9,
  24178. 0xcc,0xc9,0x4d,0x47,0x2d,0x14,0x0e,0x52,
  24179. 0xa3,0x84,0x55,0xb8,0x7c,0xdb,0xce,0xb1,
  24180. 0xe4,0x5b,0x8a,0xb9,0x30,0xf1,0xa4,0xa0
  24181. },
  24182. {
  24183. 0x63,0xbf,0x76,0xa9,0x73,0xa0,0x09,0xb9,
  24184. 0xcc,0xc9,0x4d,0x47,0x2d,0x14,0x0e,0x52,
  24185. 0xa3,0x84,0x55,0xb8,0x7c,0xdb,0xce,0xb1,
  24186. 0xe4,0x5b,0x8a,0xb9,0x30,0xf1,0xa4,0xa0
  24187. }
  24188. #endif
  24189. };
  24190. /* public key for party b */
  24191. byte pb[X25519_TEST_CNT][32] = {
  24192. {
  24193. 0x7f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  24194. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  24195. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  24196. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xf0
  24197. },
  24198. #ifdef CURVE25519_OVERFLOW_ALL_TESTS
  24199. {
  24200. /* 0xff first byte in original - invalid! */
  24201. 0x7f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  24202. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  24203. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  24204. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xf0
  24205. },
  24206. {
  24207. 0x36,0x1a,0x74,0x87,0x28,0x59,0xe0,0xb6,
  24208. 0xe4,0x2b,0x17,0x9b,0x16,0xb0,0x3b,0xf8,
  24209. 0xb8,0x9f,0x2a,0x8f,0xc5,0x33,0x68,0x4f,
  24210. 0xde,0x4d,0xd8,0x80,0x63,0xe7,0xb4,0x0a
  24211. },
  24212. {
  24213. 0x00,0x80,0x38,0x59,0x19,0x3a,0x66,0x12,
  24214. 0xfd,0xa1,0xec,0x1c,0x40,0x84,0x40,0xbd,
  24215. 0x64,0x10,0x8b,0x53,0x81,0x21,0x03,0x2d,
  24216. 0x7d,0x33,0xb4,0x01,0x57,0x0d,0xe1,0x89
  24217. },
  24218. {
  24219. 0x1d,0xf8,0xf8,0x33,0x89,0x6c,0xb7,0xba,
  24220. 0x94,0x73,0xfa,0xc2,0x36,0xac,0xbe,0x49,
  24221. 0xaf,0x85,0x3e,0x93,0x5f,0xae,0xb2,0xc0,
  24222. 0xc8,0x80,0x8f,0x4a,0xaa,0xd3,0x55,0x2b
  24223. }
  24224. #endif
  24225. };
  24226. /* expected shared key */
  24227. byte ss[X25519_TEST_CNT][32] = {
  24228. {
  24229. 0x5c,0x4c,0x85,0x5f,0xfb,0x20,0x38,0xcc,
  24230. 0x55,0x16,0x5b,0x8a,0xa7,0xed,0x57,0x6e,
  24231. 0x35,0xaa,0x71,0x67,0x85,0x1f,0xb6,0x28,
  24232. 0x17,0x07,0x7b,0xda,0x76,0xdd,0xe0,0xb4
  24233. },
  24234. #ifdef CURVE25519_OVERFLOW_ALL_TESTS
  24235. {
  24236. 0x33,0xf6,0xc1,0x34,0x62,0x92,0x06,0x02,
  24237. 0x95,0xdb,0x91,0x4c,0x5d,0x52,0x54,0xc7,
  24238. 0xd2,0x5b,0x24,0xb5,0x4f,0x33,0x59,0x79,
  24239. 0x9f,0x6d,0x7e,0x4a,0x4c,0x30,0xd6,0x38
  24240. },
  24241. {
  24242. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24243. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24244. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24245. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x02
  24246. },
  24247. {
  24248. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24249. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24250. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24251. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x09
  24252. },
  24253. {
  24254. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24255. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24256. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24257. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x10
  24258. }
  24259. #endif
  24260. };
  24261. int ret = 0;
  24262. int i;
  24263. word32 y;
  24264. byte shared[32];
  24265. curve25519_key userA;
  24266. wc_curve25519_init_ex(&userA, HEAP_HINT, devId);
  24267. for (i = 0; i < X25519_TEST_CNT; i++) {
  24268. if (wc_curve25519_import_private_raw(sa[i], sizeof(sa[i]), pb[i],
  24269. sizeof(pb[i]), &userA) != 0) {
  24270. ret = -10500 - i; break;
  24271. }
  24272. /* test against known test vector */
  24273. XMEMSET(shared, 0, sizeof(shared));
  24274. y = sizeof(shared);
  24275. if (wc_curve25519_shared_secret(&userA, &userA, shared, &y) != 0) {
  24276. ret = -10510 - i; break;
  24277. }
  24278. if (XMEMCMP(ss[i], shared, y)) {
  24279. ret = -10520 - i; break;
  24280. }
  24281. }
  24282. wc_curve25519_free(&userA);
  24283. return ret;
  24284. }
  24285. /* Test the wc_curve25519_check_public API.
  24286. *
  24287. * returns 0 on success and -ve on failure.
  24288. */
  24289. static int curve25519_check_public_test(void)
  24290. {
  24291. /* Little-endian values that will fail */
  24292. byte fail_le[][CURVE25519_KEYSIZE] = {
  24293. {
  24294. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24295. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24296. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24297. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  24298. },
  24299. {
  24300. 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24301. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24302. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24303. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  24304. },
  24305. {
  24306. 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24307. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24308. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24309. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x81
  24310. },
  24311. };
  24312. /* Big-endian values that will fail */
  24313. byte fail_be[][CURVE25519_KEYSIZE] = {
  24314. {
  24315. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24316. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24317. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24318. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  24319. },
  24320. {
  24321. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24322. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24323. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24324. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  24325. },
  24326. {
  24327. 0x81,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24328. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24329. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24330. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  24331. },
  24332. };
  24333. /* Good or valid public value */
  24334. byte good[CURVE25519_KEYSIZE] = {
  24335. 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24336. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24337. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24338. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  24339. };
  24340. int i;
  24341. /* Parameter checks */
  24342. /* NULL pointer */
  24343. if (wc_curve25519_check_public(NULL, 0, EC25519_LITTLE_ENDIAN) !=
  24344. BAD_FUNC_ARG) {
  24345. return -10600;
  24346. }
  24347. if (wc_curve25519_check_public(NULL, 0, EC25519_BIG_ENDIAN) !=
  24348. BAD_FUNC_ARG) {
  24349. return -10601;
  24350. }
  24351. /* Length of 0 treated differently to other invalid lengths for TLS */
  24352. if (wc_curve25519_check_public(good, 0, EC25519_LITTLE_ENDIAN) != BUFFER_E)
  24353. return -10602;
  24354. if (wc_curve25519_check_public(good, 0, EC25519_BIG_ENDIAN) != BUFFER_E)
  24355. return -10603;
  24356. /* Length not CURVE25519_KEYSIZE */
  24357. for (i = 1; i < CURVE25519_KEYSIZE + 2; i++) {
  24358. if (i == CURVE25519_KEYSIZE)
  24359. continue;
  24360. if (wc_curve25519_check_public(good, i, EC25519_LITTLE_ENDIAN) !=
  24361. ECC_BAD_ARG_E) {
  24362. return -10604 - i;
  24363. }
  24364. if (wc_curve25519_check_public(good, i, EC25519_BIG_ENDIAN) !=
  24365. ECC_BAD_ARG_E) {
  24366. return -10614 - i;
  24367. }
  24368. }
  24369. /* Little-endian fail cases */
  24370. for (i = 0; i < (int)(sizeof(fail_le) / sizeof(*fail_le)); i++) {
  24371. if (wc_curve25519_check_public(fail_le[i], CURVE25519_KEYSIZE,
  24372. EC25519_LITTLE_ENDIAN) == 0) {
  24373. return -10624 - i;
  24374. }
  24375. }
  24376. /* Big-endian fail cases */
  24377. for (i = 0; i < (int)(sizeof(fail_be) / sizeof(*fail_be)); i++) {
  24378. if (wc_curve25519_check_public(fail_be[i], CURVE25519_KEYSIZE,
  24379. EC25519_BIG_ENDIAN) == 0) {
  24380. return -10634 - i;
  24381. }
  24382. }
  24383. /* Check a valid public value works! */
  24384. if (wc_curve25519_check_public(good, CURVE25519_KEYSIZE,
  24385. EC25519_LITTLE_ENDIAN) != 0) {
  24386. return -10644;
  24387. }
  24388. if (wc_curve25519_check_public(good, CURVE25519_KEYSIZE,
  24389. EC25519_BIG_ENDIAN) != 0) {
  24390. return -10645;
  24391. }
  24392. return 0;
  24393. }
  24394. #endif /* HAVE_CURVE25519_SHARED_SECRET && HAVE_CURVE25519_KEY_IMPORT */
  24395. #if !defined(NO_ASN) && defined(HAVE_CURVE25519_KEY_EXPORT) && \
  24396. defined(HAVE_CURVE25519_KEY_IMPORT)
  24397. static int curve255519_der_test(void)
  24398. {
  24399. int ret = 0;
  24400. /* certs/statickeys/x25519.der */
  24401. const byte kCurve25519PrivDer[] = {
  24402. 0x30, 0x2E, 0x02, 0x01, 0x00, 0x30, 0x05, 0x06, 0x03, 0x2B, 0x65, 0x6E,
  24403. 0x04, 0x22, 0x04, 0x20, 0x78, 0x8E, 0x31, 0x5C, 0x33, 0xA9, 0x19, 0xC0,
  24404. 0x5E, 0x36, 0x70, 0x1B, 0xA4, 0xE8, 0xEF, 0xC1, 0x89, 0x8C, 0xB3, 0x15,
  24405. 0xC6, 0x79, 0xD3, 0xAC, 0x22, 0x00, 0xAE, 0xFA, 0xB3, 0xB7, 0x0F, 0x78
  24406. };
  24407. /* certs/statickeys/x25519-pub.der */
  24408. const byte kCurve25519PubDer[] = {
  24409. 0x30, 0x2A, 0x30, 0x05, 0x06, 0x03, 0x2B, 0x65, 0x6E, 0x03, 0x21, 0x00,
  24410. 0x09, 0xBC, 0x8C, 0xC7, 0x45, 0x0D, 0xC1, 0xC2, 0x02, 0x57, 0x9A, 0x68,
  24411. 0x3A, 0xFD, 0x7A, 0xA8, 0xA5, 0x2F, 0xF0, 0x99, 0x39, 0x98, 0xEA, 0x26,
  24412. 0xA2, 0x5B, 0x38, 0xFD, 0x96, 0xDB, 0x2A, 0x26
  24413. };
  24414. curve25519_key key;
  24415. byte output[128];
  24416. word32 outputSz = 128;
  24417. word32 idx;
  24418. if (wc_curve25519_init_ex(&key, HEAP_HINT, devId) != 0) {
  24419. return -10723;
  24420. }
  24421. /* Test decode / encode of Curve25519 private key only */
  24422. if (ret == 0) {
  24423. idx = 0;
  24424. ret = wc_Curve25519PrivateKeyDecode(kCurve25519PrivDer, &idx, &key,
  24425. (word32)sizeof(kCurve25519PrivDer));
  24426. }
  24427. if (ret == 0) {
  24428. outputSz = (word32)sizeof(output);
  24429. ret = wc_Curve25519PrivateKeyToDer(&key, output, outputSz);
  24430. if (ret >= 0) {
  24431. outputSz = ret;
  24432. ret = 0;
  24433. }
  24434. else {
  24435. ret = -10724;
  24436. }
  24437. }
  24438. if (ret == 0 && (outputSz != (word32)sizeof(kCurve25519PrivDer) ||
  24439. XMEMCMP(output, kCurve25519PrivDer, outputSz) != 0)) {
  24440. ret = -10725;
  24441. }
  24442. /* Test decode / encode of Curve25519 public key only */
  24443. if (ret == 0) {
  24444. idx = 0;
  24445. ret = wc_Curve25519PublicKeyDecode(kCurve25519PubDer, &idx, &key,
  24446. (word32)sizeof(kCurve25519PubDer));
  24447. }
  24448. if (ret == 0) {
  24449. outputSz = (word32)sizeof(output);
  24450. ret = wc_Curve25519PublicKeyToDer(&key, output, outputSz, 1);
  24451. if (ret >= 0) {
  24452. outputSz = ret;
  24453. ret = 0;
  24454. }
  24455. else {
  24456. ret = -10726;
  24457. }
  24458. }
  24459. if (ret == 0 && (outputSz != (word32)sizeof(kCurve25519PubDer) ||
  24460. XMEMCMP(output, kCurve25519PubDer, outputSz) != 0)) {
  24461. ret = -10727;
  24462. }
  24463. wc_curve25519_free(&key);
  24464. return ret;
  24465. }
  24466. #endif /* !NO_ASN && HAVE_CURVE25519_KEY_EXPORT && HAVE_CURVE25519_KEY_IMPORT */
  24467. WOLFSSL_TEST_SUBROUTINE int curve25519_test(void)
  24468. {
  24469. WC_RNG rng;
  24470. int ret;
  24471. #ifdef HAVE_CURVE25519_SHARED_SECRET
  24472. byte sharedA[32];
  24473. byte sharedB[32];
  24474. word32 y;
  24475. #endif
  24476. #ifdef HAVE_CURVE25519_KEY_EXPORT
  24477. byte exportBuf[32];
  24478. #endif
  24479. word32 x = 0;
  24480. curve25519_key userA, userB, pubKey;
  24481. #if defined(HAVE_CURVE25519_SHARED_SECRET) && \
  24482. defined(HAVE_CURVE25519_KEY_IMPORT)
  24483. /* test vectors from
  24484. https://tools.ietf.org/html/draft-josefsson-tls-curve25519-03
  24485. */
  24486. /* secret key for party a */
  24487. byte sa[] = {
  24488. 0x5A,0xC9,0x9F,0x33,0x63,0x2E,0x5A,0x76,
  24489. 0x8D,0xE7,0xE8,0x1B,0xF8,0x54,0xC2,0x7C,
  24490. 0x46,0xE3,0xFB,0xF2,0xAB,0xBA,0xCD,0x29,
  24491. 0xEC,0x4A,0xFF,0x51,0x73,0x69,0xC6,0x60
  24492. };
  24493. /* public key for party a */
  24494. byte pa[] = {
  24495. 0x05,0x7E,0x23,0xEA,0x9F,0x1C,0xBE,0x8A,
  24496. 0x27,0x16,0x8F,0x6E,0x69,0x6A,0x79,0x1D,
  24497. 0xE6,0x1D,0xD3,0xAF,0x7A,0xCD,0x4E,0xEA,
  24498. 0xCC,0x6E,0x7B,0xA5,0x14,0xFD,0xA8,0x63
  24499. };
  24500. /* secret key for party b */
  24501. byte sb[] = {
  24502. 0x47,0xDC,0x3D,0x21,0x41,0x74,0x82,0x0E,
  24503. 0x11,0x54,0xB4,0x9B,0xC6,0xCD,0xB2,0xAB,
  24504. 0xD4,0x5E,0xE9,0x58,0x17,0x05,0x5D,0x25,
  24505. 0x5A,0xA3,0x58,0x31,0xB7,0x0D,0x32,0x60
  24506. };
  24507. /* public key for party b */
  24508. byte pb[] = {
  24509. 0x6E,0xB8,0x9D,0xA9,0x19,0x89,0xAE,0x37,
  24510. 0xC7,0xEA,0xC7,0x61,0x8D,0x9E,0x5C,0x49,
  24511. 0x51,0xDB,0xA1,0xD7,0x3C,0x28,0x5A,0xE1,
  24512. 0xCD,0x26,0xA8,0x55,0x02,0x0E,0xEF,0x04
  24513. };
  24514. /* expected shared key */
  24515. byte ss[] = {
  24516. 0x61,0x45,0x0C,0xD9,0x8E,0x36,0x01,0x6B,
  24517. 0x58,0x77,0x6A,0x89,0x7A,0x9F,0x0A,0xEF,
  24518. 0x73,0x8B,0x99,0xF0,0x94,0x68,0xB8,0xD6,
  24519. 0xB8,0x51,0x11,0x84,0xD5,0x34,0x94,0xAB
  24520. };
  24521. #endif /* HAVE_CURVE25519_SHARED_SECRET */
  24522. (void)x;
  24523. #ifndef HAVE_FIPS
  24524. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  24525. #else
  24526. ret = wc_InitRng(&rng);
  24527. #endif
  24528. if (ret != 0)
  24529. return -10700;
  24530. wc_curve25519_init_ex(&userA, HEAP_HINT, devId);
  24531. wc_curve25519_init_ex(&userB, HEAP_HINT, devId);
  24532. wc_curve25519_init_ex(&pubKey, HEAP_HINT, devId);
  24533. /* make curve25519 keys */
  24534. if (wc_curve25519_make_key(&rng, 32, &userA) != 0)
  24535. return -10701;
  24536. if (wc_curve25519_make_key(&rng, 32, &userB) != 0)
  24537. return -10702;
  24538. #ifdef HAVE_CURVE25519_SHARED_SECRET
  24539. /* find shared secret key */
  24540. x = sizeof(sharedA);
  24541. if ((ret = wc_curve25519_shared_secret(&userA, &userB, sharedA, &x)) != 0) {
  24542. printf("wc_curve25519_shared_secret 1 %d\n", ret);
  24543. return -10703;
  24544. }
  24545. y = sizeof(sharedB);
  24546. if ((ret = wc_curve25519_shared_secret(&userB, &userA, sharedB, &y)) != 0) {
  24547. printf("wc_curve25519_shared_secret 2 %d\n", ret);
  24548. return -10704;
  24549. }
  24550. /* compare shared secret keys to test they are the same */
  24551. if (y != x)
  24552. return -10705;
  24553. if (XMEMCMP(sharedA, sharedB, x))
  24554. return -10706;
  24555. #endif
  24556. #ifdef HAVE_CURVE25519_KEY_EXPORT
  24557. /* export a public key and import it for another user */
  24558. x = sizeof(exportBuf);
  24559. if (wc_curve25519_export_public(&userA, exportBuf, &x) != 0)
  24560. return -10707;
  24561. #ifdef HAVE_CURVE25519_KEY_IMPORT
  24562. if (wc_curve25519_import_public(exportBuf, x, &pubKey) != 0)
  24563. return -10708;
  24564. #endif
  24565. #endif
  24566. #if defined(HAVE_CURVE25519_SHARED_SECRET) && \
  24567. defined(HAVE_CURVE25519_KEY_IMPORT)
  24568. /* test shared key after importing a public key */
  24569. XMEMSET(sharedB, 0, sizeof(sharedB));
  24570. y = sizeof(sharedB);
  24571. if (wc_curve25519_shared_secret(&userB, &pubKey, sharedB, &y) != 0)
  24572. return -10709;
  24573. if (XMEMCMP(sharedA, sharedB, y))
  24574. return -10710;
  24575. /* import RFC test vectors and compare shared key */
  24576. if (wc_curve25519_import_private_raw(sa, sizeof(sa), pa, sizeof(pa), &userA)
  24577. != 0)
  24578. return -10711;
  24579. if (wc_curve25519_import_private_raw(sb, sizeof(sb), pb, sizeof(pb), &userB)
  24580. != 0)
  24581. return -10712;
  24582. /* test against known test vector */
  24583. XMEMSET(sharedB, 0, sizeof(sharedB));
  24584. y = sizeof(sharedB);
  24585. if (wc_curve25519_shared_secret(&userA, &userB, sharedB, &y) != 0)
  24586. return -10713;
  24587. if (XMEMCMP(ss, sharedB, y))
  24588. return -10714;
  24589. /* test swapping roles of keys and generating same shared key */
  24590. XMEMSET(sharedB, 0, sizeof(sharedB));
  24591. y = sizeof(sharedB);
  24592. if (wc_curve25519_shared_secret(&userB, &userA, sharedB, &y) != 0)
  24593. return -10715;
  24594. if (XMEMCMP(ss, sharedB, y))
  24595. return -10716;
  24596. /* test with 1 generated key and 1 from known test vector */
  24597. if (wc_curve25519_import_private_raw(sa, sizeof(sa), pa, sizeof(pa), &userA)
  24598. != 0)
  24599. return -10717;
  24600. wc_curve25519_free(&userB);
  24601. wc_curve25519_init_ex(&userB, HEAP_HINT, devId);
  24602. if (wc_curve25519_make_key(&rng, 32, &userB) != 0)
  24603. return -10718;
  24604. x = sizeof(sharedA);
  24605. if (wc_curve25519_shared_secret(&userA, &userB, sharedA, &x) != 0)
  24606. return -10719;
  24607. y = sizeof(sharedB);
  24608. if (wc_curve25519_shared_secret(&userB, &userA, sharedB, &y) != 0)
  24609. return -10720;
  24610. /* compare shared secret keys to test they are the same */
  24611. if (y != x)
  24612. return -10721;
  24613. if (XMEMCMP(sharedA, sharedB, x))
  24614. return -10722;
  24615. ret = curve25519_overflow_test();
  24616. if (ret != 0)
  24617. return ret;
  24618. ret = curve25519_check_public_test();
  24619. if (ret != 0)
  24620. return ret;
  24621. #endif /* HAVE_CURVE25519_SHARED_SECRET && HAVE_CURVE25519_KEY_IMPORT */
  24622. #if !defined(NO_ASN) && defined(HAVE_CURVE25519_KEY_EXPORT) && \
  24623. defined(HAVE_CURVE25519_KEY_IMPORT)
  24624. ret = curve255519_der_test();
  24625. if (ret != 0)
  24626. return ret;
  24627. #endif
  24628. /* clean up keys when done */
  24629. wc_curve25519_free(&pubKey);
  24630. wc_curve25519_free(&userB);
  24631. wc_curve25519_free(&userA);
  24632. wc_FreeRng(&rng);
  24633. return 0;
  24634. }
  24635. #endif /* HAVE_CURVE25519 */
  24636. #ifdef HAVE_ED25519
  24637. #ifdef WOLFSSL_TEST_CERT
  24638. static int ed25519_test_cert(void)
  24639. {
  24640. DecodedCert cert[2];
  24641. DecodedCert* serverCert = NULL;
  24642. DecodedCert* caCert = NULL;
  24643. #ifdef HAVE_ED25519_VERIFY
  24644. ed25519_key key;
  24645. ed25519_key* pubKey = NULL;
  24646. int verify;
  24647. #endif /* HAVE_ED25519_VERIFY */
  24648. int ret;
  24649. byte* tmp;
  24650. size_t bytes;
  24651. XFILE file;
  24652. tmp = (byte *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24653. if (tmp == NULL) {
  24654. ERROR_OUT(-10730, done);
  24655. }
  24656. #ifdef USE_CERT_BUFFERS_256
  24657. XMEMCPY(tmp, ca_ed25519_cert, sizeof_ca_ed25519_cert);
  24658. bytes = sizeof_ca_ed25519_cert;
  24659. #elif !defined(NO_FILESYSTEM)
  24660. file = XFOPEN(caEd25519Cert, "rb");
  24661. if (file == NULL) {
  24662. ERROR_OUT(-10731, done);
  24663. }
  24664. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  24665. XFCLOSE(file);
  24666. #else
  24667. /* No certificate to use. */
  24668. ERROR_OUT(-10732, done);
  24669. #endif
  24670. InitDecodedCert(&cert[0], tmp, (word32)bytes, 0);
  24671. caCert = &cert[0];
  24672. ret = ParseCert(caCert, CERT_TYPE, NO_VERIFY, NULL);
  24673. if (ret != 0) {
  24674. ERROR_OUT(-10733, done);
  24675. }
  24676. #ifdef USE_CERT_BUFFERS_256
  24677. XMEMCPY(tmp, server_ed25519_cert, sizeof_server_ed25519_cert);
  24678. bytes = sizeof_server_ed25519_cert;
  24679. #elif !defined(NO_FILESYSTEM)
  24680. file = XFOPEN(serverEd25519Cert, "rb");
  24681. if (file == NULL) {
  24682. ERROR_OUT(-10734, done);
  24683. }
  24684. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  24685. XFCLOSE(file);
  24686. #else
  24687. /* No certificate to use. */
  24688. ERROR_OUT(-10735, done);
  24689. #endif
  24690. InitDecodedCert(&cert[1], tmp, (word32)bytes, 0);
  24691. serverCert = &cert[1];
  24692. ret = ParseCert(serverCert, CERT_TYPE, NO_VERIFY, NULL);
  24693. if (ret != 0) {
  24694. ERROR_OUT(-10736, done);
  24695. }
  24696. #ifdef HAVE_ED25519_VERIFY
  24697. ret = wc_ed25519_init(&key);
  24698. if (ret < 0) {
  24699. ERROR_OUT(-10737, done);
  24700. }
  24701. pubKey = &key;
  24702. ret = wc_ed25519_import_public(caCert->publicKey, caCert->pubKeySize,
  24703. pubKey);
  24704. if (ret < 0) {
  24705. ERROR_OUT(-10738, done);
  24706. }
  24707. if (wc_ed25519_verify_msg(serverCert->signature, serverCert->sigLength,
  24708. serverCert->source + serverCert->certBegin,
  24709. serverCert->sigIndex - serverCert->certBegin,
  24710. &verify, pubKey) < 0 || verify != 1) {
  24711. ERROR_OUT(-10739, done);
  24712. }
  24713. #endif /* HAVE_ED25519_VERIFY */
  24714. done:
  24715. if (tmp != NULL)
  24716. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24717. #ifdef HAVE_ED25519_VERIFY
  24718. wc_ed25519_free(pubKey);
  24719. #endif /* HAVE_ED25519_VERIFY */
  24720. if (caCert != NULL)
  24721. FreeDecodedCert(caCert);
  24722. if (serverCert != NULL)
  24723. FreeDecodedCert(serverCert);
  24724. return ret;
  24725. }
  24726. static int ed25519_test_make_cert(void)
  24727. {
  24728. WC_RNG rng;
  24729. Cert cert;
  24730. DecodedCert decode;
  24731. ed25519_key key;
  24732. ed25519_key* privKey = NULL;
  24733. int ret = 0;
  24734. byte* tmp = NULL;
  24735. wc_InitCert_ex(&cert, HEAP_HINT, devId);
  24736. #ifndef HAVE_FIPS
  24737. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  24738. #else
  24739. ret = wc_InitRng(&rng);
  24740. #endif
  24741. if (ret != 0)
  24742. return -10750;
  24743. wc_ed25519_init(&key);
  24744. privKey = &key;
  24745. wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, privKey);
  24746. cert.daysValid = 365 * 2;
  24747. cert.selfSigned = 1;
  24748. XMEMCPY(&cert.issuer, &certDefaultName, sizeof(CertName));
  24749. XMEMCPY(&cert.subject, &certDefaultName, sizeof(CertName));
  24750. cert.isCA = 0;
  24751. #ifdef WOLFSSL_CERT_EXT
  24752. ret = wc_SetKeyUsage(&cert, certKeyUsage);
  24753. if (ret < 0) {
  24754. ERROR_OUT(-10751, done);
  24755. }
  24756. ret = wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ED25519_TYPE, privKey);
  24757. if (ret < 0) {
  24758. ERROR_OUT(-10752, done);
  24759. }
  24760. ret = wc_SetAuthKeyIdFromPublicKey_ex(&cert, ED25519_TYPE, privKey);
  24761. if (ret < 0) {
  24762. ERROR_OUT(-10753, done);
  24763. }
  24764. #endif
  24765. tmp = (byte *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24766. if (tmp == NULL) {
  24767. ERROR_OUT(-10754, done);
  24768. }
  24769. cert.sigType = CTC_ED25519;
  24770. ret = wc_MakeCert_ex(&cert, tmp, FOURK_BUF, ED25519_TYPE, privKey, &rng);
  24771. if (ret < 0) {
  24772. ERROR_OUT(-10755, done);
  24773. }
  24774. ret = wc_SignCert_ex(cert.bodySz, cert.sigType, tmp, FOURK_BUF,
  24775. ED25519_TYPE, privKey, &rng);
  24776. if (ret < 0) {
  24777. ERROR_OUT(-10756, done);
  24778. }
  24779. InitDecodedCert(&decode, tmp, ret, HEAP_HINT);
  24780. ret = ParseCert(&decode, CERT_TYPE, NO_VERIFY, 0);
  24781. FreeDecodedCert(&decode);
  24782. if (ret != 0) {
  24783. ERROR_OUT(-10757, done);
  24784. }
  24785. done:
  24786. if (tmp != NULL)
  24787. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24788. wc_ed25519_free(privKey);
  24789. wc_FreeRng(&rng);
  24790. return ret;
  24791. }
  24792. #endif /* WOLFSSL_TEST_CERT */
  24793. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_EXPORT) && \
  24794. defined(HAVE_ED25519_KEY_IMPORT)
  24795. static int ed25519ctx_test(void)
  24796. {
  24797. int ret;
  24798. byte out[ED25519_SIG_SIZE];
  24799. word32 outlen;
  24800. #ifdef HAVE_ED25519_VERIFY
  24801. int verify = 0;
  24802. #endif /* HAVE_ED25519_VERIFY */
  24803. ed25519_key key;
  24804. WOLFSSL_SMALL_STACK_STATIC const byte sKeyCtx[] = {
  24805. 0x03,0x05,0x33,0x4e,0x38,0x1a,0xf7,0x8f,
  24806. 0x14,0x1c,0xb6,0x66,0xf6,0x19,0x9f,0x57,
  24807. 0xbc,0x34,0x95,0x33,0x5a,0x25,0x6a,0x95,
  24808. 0xbd,0x2a,0x55,0xbf,0x54,0x66,0x63,0xf6
  24809. };
  24810. WOLFSSL_SMALL_STACK_STATIC const byte pKeyCtx[] = {
  24811. 0xdf,0xc9,0x42,0x5e,0x4f,0x96,0x8f,0x7f,
  24812. 0x0c,0x29,0xf0,0x25,0x9c,0xf5,0xf9,0xae,
  24813. 0xd6,0x85,0x1c,0x2b,0xb4,0xad,0x8b,0xfb,
  24814. 0x86,0x0c,0xfe,0xe0,0xab,0x24,0x82,0x92
  24815. };
  24816. WOLFSSL_SMALL_STACK_STATIC const byte sigCtx1[] = {
  24817. 0x55,0xa4,0xcc,0x2f,0x70,0xa5,0x4e,0x04,
  24818. 0x28,0x8c,0x5f,0x4c,0xd1,0xe4,0x5a,0x7b,
  24819. 0xb5,0x20,0xb3,0x62,0x92,0x91,0x18,0x76,
  24820. 0xca,0xda,0x73,0x23,0x19,0x8d,0xd8,0x7a,
  24821. 0x8b,0x36,0x95,0x0b,0x95,0x13,0x00,0x22,
  24822. 0x90,0x7a,0x7f,0xb7,0xc4,0xe9,0xb2,0xd5,
  24823. 0xf6,0xcc,0xa6,0x85,0xa5,0x87,0xb4,0xb2,
  24824. 0x1f,0x4b,0x88,0x8e,0x4e,0x7e,0xdb,0x0d
  24825. };
  24826. WOLFSSL_SMALL_STACK_STATIC const byte sigCtx2[] = {
  24827. 0xcc,0x5e,0x63,0xa2,0x7e,0x94,0xaf,0xd3,
  24828. 0x41,0x83,0x38,0xd2,0x48,0x6f,0xa9,0x2a,
  24829. 0xf9,0x91,0x7c,0x2d,0x98,0x9e,0x06,0xe5,
  24830. 0x02,0x77,0x72,0x1c,0x34,0x38,0x18,0xb4,
  24831. 0x21,0x96,0xbc,0x29,0x2e,0x68,0xf3,0x4d,
  24832. 0x85,0x9b,0xbe,0xad,0x17,0x9f,0x54,0x54,
  24833. 0x2d,0x4b,0x04,0xdc,0xfb,0xfa,0x4a,0x68,
  24834. 0x4e,0x39,0x50,0xfb,0x1c,0xcd,0x8d,0x0d
  24835. };
  24836. WOLFSSL_SMALL_STACK_STATIC const byte msgCtx[] = {
  24837. 0xf7,0x26,0x93,0x6d,0x19,0xc8,0x00,0x49,
  24838. 0x4e,0x3f,0xda,0xff,0x20,0xb2,0x76,0xa8
  24839. };
  24840. WOLFSSL_SMALL_STACK_STATIC const byte contextCtx[] = {
  24841. 0x66,0x6f,0x6f
  24842. };
  24843. outlen = sizeof(out);
  24844. XMEMSET(out, 0, sizeof(out));
  24845. ret = wc_ed25519_init_ex(&key, HEAP_HINT, devId);
  24846. if (ret != 0)
  24847. return 10800;
  24848. ret = wc_ed25519_import_private_key(sKeyCtx, ED25519_KEY_SIZE, pKeyCtx,
  24849. sizeof(pKeyCtx), &key);
  24850. if (ret == 0)
  24851. ret = wc_ed25519ctx_sign_msg(msgCtx, sizeof(msgCtx), out, &outlen, &key,
  24852. contextCtx, sizeof(contextCtx));
  24853. if (ret == 0 && XMEMCMP(out, sigCtx1, 64) != 0)
  24854. ret = -10801;
  24855. #if defined(HAVE_ED25519_VERIFY)
  24856. /* test verify on good msg */
  24857. if (ret == 0)
  24858. ret = wc_ed25519ctx_verify_msg(out, outlen, msgCtx, sizeof(msgCtx),
  24859. &verify, &key, contextCtx, sizeof(contextCtx));
  24860. if (ret == 0 && verify != 1)
  24861. ret = -10802;
  24862. #endif
  24863. if (ret == 0)
  24864. ret = wc_ed25519ctx_sign_msg(msgCtx, sizeof(msgCtx), out, &outlen, &key,
  24865. NULL, 0);
  24866. if (ret == 0 && XMEMCMP(out, sigCtx2, 64) != 0)
  24867. ret = -10803;
  24868. #if defined(HAVE_ED25519_VERIFY)
  24869. /* test verify on good msg */
  24870. if (ret == 0)
  24871. ret = wc_ed25519ctx_verify_msg(out, outlen, msgCtx, sizeof(msgCtx),
  24872. &verify, &key, NULL, 0);
  24873. if (ret == 0 && verify != 1)
  24874. ret = -10804;
  24875. #endif
  24876. wc_ed25519_free(&key);
  24877. return ret;
  24878. }
  24879. static int ed25519ph_test(void)
  24880. {
  24881. int ret = 0;
  24882. byte out[ED25519_SIG_SIZE];
  24883. word32 outlen;
  24884. #ifdef HAVE_ED25519_VERIFY
  24885. int verify = 0;
  24886. #endif /* HAVE_ED25519_VERIFY */
  24887. ed25519_key key;
  24888. WOLFSSL_SMALL_STACK_STATIC const byte sKeyPh[] = {
  24889. 0x83,0x3f,0xe6,0x24,0x09,0x23,0x7b,0x9d,
  24890. 0x62,0xec,0x77,0x58,0x75,0x20,0x91,0x1e,
  24891. 0x9a,0x75,0x9c,0xec,0x1d,0x19,0x75,0x5b,
  24892. 0x7d,0xa9,0x01,0xb9,0x6d,0xca,0x3d,0x42
  24893. };
  24894. WOLFSSL_SMALL_STACK_STATIC const byte pKeyPh[] = {
  24895. 0xec,0x17,0x2b,0x93,0xad,0x5e,0x56,0x3b,
  24896. 0xf4,0x93,0x2c,0x70,0xe1,0x24,0x50,0x34,
  24897. 0xc3,0x54,0x67,0xef,0x2e,0xfd,0x4d,0x64,
  24898. 0xeb,0xf8,0x19,0x68,0x34,0x67,0xe2,0xbf
  24899. };
  24900. WOLFSSL_SMALL_STACK_STATIC const byte sigPh1[] = {
  24901. 0x98,0xa7,0x02,0x22,0xf0,0xb8,0x12,0x1a,
  24902. 0xa9,0xd3,0x0f,0x81,0x3d,0x68,0x3f,0x80,
  24903. 0x9e,0x46,0x2b,0x46,0x9c,0x7f,0xf8,0x76,
  24904. 0x39,0x49,0x9b,0xb9,0x4e,0x6d,0xae,0x41,
  24905. 0x31,0xf8,0x50,0x42,0x46,0x3c,0x2a,0x35,
  24906. 0x5a,0x20,0x03,0xd0,0x62,0xad,0xf5,0xaa,
  24907. 0xa1,0x0b,0x8c,0x61,0xe6,0x36,0x06,0x2a,
  24908. 0xaa,0xd1,0x1c,0x2a,0x26,0x08,0x34,0x06
  24909. };
  24910. WOLFSSL_SMALL_STACK_STATIC const byte sigPh2[] = {
  24911. 0xe0,0x39,0x70,0x2b,0x4c,0x25,0x95,0xa6,
  24912. 0xa5,0x41,0xac,0x85,0x09,0x23,0x6e,0x29,
  24913. 0x90,0x47,0x47,0x95,0x33,0x0c,0x9b,0x34,
  24914. 0xa7,0x5f,0x58,0xa6,0x60,0x12,0x9e,0x08,
  24915. 0xfd,0x73,0x69,0x43,0xfb,0x19,0x43,0xa5,
  24916. 0x57,0x20,0xb9,0xe0,0x95,0x7b,0x1e,0xd6,
  24917. 0x73,0x48,0x16,0x61,0x9f,0x13,0x88,0xf4,
  24918. 0x3f,0x73,0xe6,0xe3,0xba,0xa8,0x1c,0x0e
  24919. };
  24920. WOLFSSL_SMALL_STACK_STATIC const byte msgPh[] = {
  24921. 0x61,0x62,0x63
  24922. };
  24923. /* SHA-512 hash of msgPh */
  24924. WOLFSSL_SMALL_STACK_STATIC const byte hashPh[] = {
  24925. 0xdd,0xaf,0x35,0xa1,0x93,0x61,0x7a,0xba,
  24926. 0xcc,0x41,0x73,0x49,0xae,0x20,0x41,0x31,
  24927. 0x12,0xe6,0xfa,0x4e,0x89,0xa9,0x7e,0xa2,
  24928. 0x0a,0x9e,0xee,0xe6,0x4b,0x55,0xd3,0x9a,
  24929. 0x21,0x92,0x99,0x2a,0x27,0x4f,0xc1,0xa8,
  24930. 0x36,0xba,0x3c,0x23,0xa3,0xfe,0xeb,0xbd,
  24931. 0x45,0x4d,0x44,0x23,0x64,0x3c,0xe8,0x0e,
  24932. 0x2a,0x9a,0xc9,0x4f,0xa5,0x4c,0xa4,0x9f
  24933. };
  24934. WOLFSSL_SMALL_STACK_STATIC const byte contextPh2[] = {
  24935. 0x66,0x6f,0x6f
  24936. };
  24937. outlen = sizeof(out);
  24938. XMEMSET(out, 0, sizeof(out));
  24939. ret = wc_ed25519_init_ex(&key, HEAP_HINT, devId);
  24940. if (ret != 0)
  24941. return -10900;
  24942. ret = wc_ed25519_import_private_key(sKeyPh, ED25519_KEY_SIZE, pKeyPh,
  24943. sizeof(pKeyPh), &key);
  24944. if (ret == 0)
  24945. ret = wc_ed25519ph_sign_msg(msgPh, sizeof(msgPh), out, &outlen, &key,
  24946. NULL, 0);
  24947. if (ret == 0 && XMEMCMP(out, sigPh1, 64) != 0)
  24948. ret = -10901;
  24949. #if defined(HAVE_ED25519_VERIFY)
  24950. /* test verify on good msg */
  24951. if (ret == 0)
  24952. ret = wc_ed25519ph_verify_msg(out, outlen, msgPh, sizeof(msgPh),
  24953. &verify, &key, NULL, 0);
  24954. if (ret == 0 && verify != 1)
  24955. ret = -10902;
  24956. #endif
  24957. if (ret == 0)
  24958. ret = wc_ed25519ph_sign_msg(msgPh, sizeof(msgPh), out, &outlen, &key,
  24959. contextPh2, sizeof(contextPh2));
  24960. if (ret == 0 && XMEMCMP(out, sigPh2, 64) != 0)
  24961. ret = -10903;
  24962. #if defined(HAVE_ED25519_VERIFY)
  24963. /* test verify on good msg */
  24964. if (ret == 0)
  24965. ret = wc_ed25519ph_verify_msg(out, outlen, msgPh, sizeof(msgPh), &verify,
  24966. &key, contextPh2, sizeof(contextPh2));
  24967. if (ret == 0 && verify != 1)
  24968. ret = -10904;
  24969. #endif
  24970. if (ret == 0)
  24971. ret = wc_ed25519ph_sign_hash(hashPh, sizeof(hashPh), out, &outlen, &key,
  24972. NULL, 0);
  24973. if (ret == 0 && XMEMCMP(out, sigPh1, 64) != 0)
  24974. ret = -10905;
  24975. #if defined(HAVE_ED25519_VERIFY)
  24976. if (ret == 0)
  24977. ret = wc_ed25519ph_verify_hash(out, outlen, hashPh, sizeof(hashPh),
  24978. &verify, &key, NULL, 0);
  24979. if (ret == 0 && verify != 1)
  24980. ret = -10906;
  24981. #endif
  24982. if (ret == 0)
  24983. ret = wc_ed25519ph_sign_hash(hashPh, sizeof(hashPh), out, &outlen, &key,
  24984. contextPh2, sizeof(contextPh2));
  24985. if (ret == 0 && XMEMCMP(out, sigPh2, 64) != 0)
  24986. ret = -10907;
  24987. #if defined(HAVE_ED25519_VERIFY)
  24988. if (ret == 0)
  24989. ret = wc_ed25519ph_verify_hash(out, outlen, hashPh, sizeof(hashPh), &verify,
  24990. &key, contextPh2, sizeof(contextPh2));
  24991. if (ret == 0 && verify != 1)
  24992. ret = -10908;
  24993. #endif
  24994. wc_ed25519_free(&key);
  24995. return ret;
  24996. }
  24997. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_EXPORT && HAVE_ED25519_KEY_IMPORT */
  24998. WOLFSSL_TEST_SUBROUTINE int ed25519_test(void)
  24999. {
  25000. int ret;
  25001. WC_RNG rng;
  25002. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_EXPORT) &&\
  25003. defined(HAVE_ED25519_KEY_IMPORT)
  25004. byte out[ED25519_SIG_SIZE];
  25005. byte exportPKey[ED25519_KEY_SIZE];
  25006. byte exportSKey[ED25519_KEY_SIZE];
  25007. word32 exportPSz;
  25008. word32 exportSSz;
  25009. int i;
  25010. word32 outlen;
  25011. #ifdef HAVE_ED25519_VERIFY
  25012. #ifdef WOLFSSL_ED25519_STREAMING_VERIFY
  25013. int j;
  25014. #endif
  25015. int verify;
  25016. #endif /* HAVE_ED25519_VERIFY */
  25017. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_EXPORT && HAVE_ED25519_KEY_IMPORT */
  25018. word32 keySz, sigSz;
  25019. ed25519_key key;
  25020. ed25519_key key2;
  25021. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_EXPORT) && \
  25022. defined(HAVE_ED25519_KEY_IMPORT)
  25023. /* test vectors from
  25024. https://tools.ietf.org/html/draft-josefsson-eddsa-ed25519-02
  25025. */
  25026. WOLFSSL_SMALL_STACK_STATIC const byte sKey1[] = {
  25027. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  25028. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  25029. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  25030. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60
  25031. };
  25032. WOLFSSL_SMALL_STACK_STATIC const byte sKey2[] = {
  25033. 0x4c,0xcd,0x08,0x9b,0x28,0xff,0x96,0xda,
  25034. 0x9d,0xb6,0xc3,0x46,0xec,0x11,0x4e,0x0f,
  25035. 0x5b,0x8a,0x31,0x9f,0x35,0xab,0xa6,0x24,
  25036. 0xda,0x8c,0xf6,0xed,0x4f,0xb8,0xa6,0xfb
  25037. };
  25038. WOLFSSL_SMALL_STACK_STATIC const byte sKey3[] = {
  25039. 0xc5,0xaa,0x8d,0xf4,0x3f,0x9f,0x83,0x7b,
  25040. 0xed,0xb7,0x44,0x2f,0x31,0xdc,0xb7,0xb1,
  25041. 0x66,0xd3,0x85,0x35,0x07,0x6f,0x09,0x4b,
  25042. 0x85,0xce,0x3a,0x2e,0x0b,0x44,0x58,0xf7
  25043. };
  25044. /* uncompressed test */
  25045. WOLFSSL_SMALL_STACK_STATIC const byte sKey4[] = {
  25046. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  25047. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  25048. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  25049. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60
  25050. };
  25051. /* compressed prefix test */
  25052. WOLFSSL_SMALL_STACK_STATIC const byte sKey5[] = {
  25053. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  25054. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  25055. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  25056. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60
  25057. };
  25058. WOLFSSL_SMALL_STACK_STATIC const byte sKey6[] = {
  25059. 0xf5,0xe5,0x76,0x7c,0xf1,0x53,0x31,0x95,
  25060. 0x17,0x63,0x0f,0x22,0x68,0x76,0xb8,0x6c,
  25061. 0x81,0x60,0xcc,0x58,0x3b,0xc0,0x13,0x74,
  25062. 0x4c,0x6b,0xf2,0x55,0xf5,0xcc,0x0e,0xe5
  25063. };
  25064. WOLFSSL_SMALL_STACK_STATIC const byte* sKeys[] = {sKey1, sKey2, sKey3, sKey4, sKey5, sKey6};
  25065. WOLFSSL_SMALL_STACK_STATIC const byte pKey1[] = {
  25066. 0xd7,0x5a,0x98,0x01,0x82,0xb1,0x0a,0xb7,
  25067. 0xd5,0x4b,0xfe,0xd3,0xc9,0x64,0x07,0x3a,
  25068. 0x0e,0xe1,0x72,0xf3,0xda,0xa6,0x23,0x25,
  25069. 0xaf,0x02,0x1a,0x68,0xf7,0x07,0x51,0x1a
  25070. };
  25071. WOLFSSL_SMALL_STACK_STATIC const byte pKey2[] = {
  25072. 0x3d,0x40,0x17,0xc3,0xe8,0x43,0x89,0x5a,
  25073. 0x92,0xb7,0x0a,0xa7,0x4d,0x1b,0x7e,0xbc,
  25074. 0x9c,0x98,0x2c,0xcf,0x2e,0xc4,0x96,0x8c,
  25075. 0xc0,0xcd,0x55,0xf1,0x2a,0xf4,0x66,0x0c
  25076. };
  25077. WOLFSSL_SMALL_STACK_STATIC const byte pKey3[] = {
  25078. 0xfc,0x51,0xcd,0x8e,0x62,0x18,0xa1,0xa3,
  25079. 0x8d,0xa4,0x7e,0xd0,0x02,0x30,0xf0,0x58,
  25080. 0x08,0x16,0xed,0x13,0xba,0x33,0x03,0xac,
  25081. 0x5d,0xeb,0x91,0x15,0x48,0x90,0x80,0x25
  25082. };
  25083. /* uncompressed test */
  25084. WOLFSSL_SMALL_STACK_STATIC const byte pKey4[] = {
  25085. 0x04,0x55,0xd0,0xe0,0x9a,0x2b,0x9d,0x34,
  25086. 0x29,0x22,0x97,0xe0,0x8d,0x60,0xd0,0xf6,
  25087. 0x20,0xc5,0x13,0xd4,0x72,0x53,0x18,0x7c,
  25088. 0x24,0xb1,0x27,0x86,0xbd,0x77,0x76,0x45,
  25089. 0xce,0x1a,0x51,0x07,0xf7,0x68,0x1a,0x02,
  25090. 0xaf,0x25,0x23,0xa6,0xda,0xf3,0x72,0xe1,
  25091. 0x0e,0x3a,0x07,0x64,0xc9,0xd3,0xfe,0x4b,
  25092. 0xd5,0xb7,0x0a,0xb1,0x82,0x01,0x98,0x5a,
  25093. 0xd7
  25094. };
  25095. /* compressed prefix */
  25096. WOLFSSL_SMALL_STACK_STATIC const byte pKey5[] = {
  25097. 0x40,0xd7,0x5a,0x98,0x01,0x82,0xb1,0x0a,0xb7,
  25098. 0xd5,0x4b,0xfe,0xd3,0xc9,0x64,0x07,0x3a,
  25099. 0x0e,0xe1,0x72,0xf3,0xda,0xa6,0x23,0x25,
  25100. 0xaf,0x02,0x1a,0x68,0xf7,0x07,0x51,0x1a
  25101. };
  25102. WOLFSSL_SMALL_STACK_STATIC const byte pKey6[] = {
  25103. 0x27,0x81,0x17,0xfc,0x14,0x4c,0x72,0x34,
  25104. 0x0f,0x67,0xd0,0xf2,0x31,0x6e,0x83,0x86,
  25105. 0xce,0xff,0xbf,0x2b,0x24,0x28,0xc9,0xc5,
  25106. 0x1f,0xef,0x7c,0x59,0x7f,0x1d,0x42,0x6e
  25107. };
  25108. WOLFSSL_SMALL_STACK_STATIC const byte* pKeys[] = {pKey1, pKey2, pKey3, pKey4, pKey5, pKey6};
  25109. WOLFSSL_SMALL_STACK_STATIC const byte pKeySz[] = {sizeof(pKey1), sizeof(pKey2), sizeof(pKey3),
  25110. sizeof(pKey4), sizeof(pKey5), sizeof(pKey6)};
  25111. WOLFSSL_SMALL_STACK_STATIC const byte sig1[] = {
  25112. 0xe5,0x56,0x43,0x00,0xc3,0x60,0xac,0x72,
  25113. 0x90,0x86,0xe2,0xcc,0x80,0x6e,0x82,0x8a,
  25114. 0x84,0x87,0x7f,0x1e,0xb8,0xe5,0xd9,0x74,
  25115. 0xd8,0x73,0xe0,0x65,0x22,0x49,0x01,0x55,
  25116. 0x5f,0xb8,0x82,0x15,0x90,0xa3,0x3b,0xac,
  25117. 0xc6,0x1e,0x39,0x70,0x1c,0xf9,0xb4,0x6b,
  25118. 0xd2,0x5b,0xf5,0xf0,0x59,0x5b,0xbe,0x24,
  25119. 0x65,0x51,0x41,0x43,0x8e,0x7a,0x10,0x0b
  25120. };
  25121. WOLFSSL_SMALL_STACK_STATIC const byte sig2[] = {
  25122. 0x92,0xa0,0x09,0xa9,0xf0,0xd4,0xca,0xb8,
  25123. 0x72,0x0e,0x82,0x0b,0x5f,0x64,0x25,0x40,
  25124. 0xa2,0xb2,0x7b,0x54,0x16,0x50,0x3f,0x8f,
  25125. 0xb3,0x76,0x22,0x23,0xeb,0xdb,0x69,0xda,
  25126. 0x08,0x5a,0xc1,0xe4,0x3e,0x15,0x99,0x6e,
  25127. 0x45,0x8f,0x36,0x13,0xd0,0xf1,0x1d,0x8c,
  25128. 0x38,0x7b,0x2e,0xae,0xb4,0x30,0x2a,0xee,
  25129. 0xb0,0x0d,0x29,0x16,0x12,0xbb,0x0c,0x00
  25130. };
  25131. WOLFSSL_SMALL_STACK_STATIC const byte sig3[] = {
  25132. 0x62,0x91,0xd6,0x57,0xde,0xec,0x24,0x02,
  25133. 0x48,0x27,0xe6,0x9c,0x3a,0xbe,0x01,0xa3,
  25134. 0x0c,0xe5,0x48,0xa2,0x84,0x74,0x3a,0x44,
  25135. 0x5e,0x36,0x80,0xd7,0xdb,0x5a,0xc3,0xac,
  25136. 0x18,0xff,0x9b,0x53,0x8d,0x16,0xf2,0x90,
  25137. 0xae,0x67,0xf7,0x60,0x98,0x4d,0xc6,0x59,
  25138. 0x4a,0x7c,0x15,0xe9,0x71,0x6e,0xd2,0x8d,
  25139. 0xc0,0x27,0xbe,0xce,0xea,0x1e,0xc4,0x0a
  25140. };
  25141. /* uncompressed test */
  25142. WOLFSSL_SMALL_STACK_STATIC const byte sig4[] = {
  25143. 0xe5,0x56,0x43,0x00,0xc3,0x60,0xac,0x72,
  25144. 0x90,0x86,0xe2,0xcc,0x80,0x6e,0x82,0x8a,
  25145. 0x84,0x87,0x7f,0x1e,0xb8,0xe5,0xd9,0x74,
  25146. 0xd8,0x73,0xe0,0x65,0x22,0x49,0x01,0x55,
  25147. 0x5f,0xb8,0x82,0x15,0x90,0xa3,0x3b,0xac,
  25148. 0xc6,0x1e,0x39,0x70,0x1c,0xf9,0xb4,0x6b,
  25149. 0xd2,0x5b,0xf5,0xf0,0x59,0x5b,0xbe,0x24,
  25150. 0x65,0x51,0x41,0x43,0x8e,0x7a,0x10,0x0b
  25151. };
  25152. /* compressed prefix */
  25153. WOLFSSL_SMALL_STACK_STATIC const byte sig5[] = {
  25154. 0xe5,0x56,0x43,0x00,0xc3,0x60,0xac,0x72,
  25155. 0x90,0x86,0xe2,0xcc,0x80,0x6e,0x82,0x8a,
  25156. 0x84,0x87,0x7f,0x1e,0xb8,0xe5,0xd9,0x74,
  25157. 0xd8,0x73,0xe0,0x65,0x22,0x49,0x01,0x55,
  25158. 0x5f,0xb8,0x82,0x15,0x90,0xa3,0x3b,0xac,
  25159. 0xc6,0x1e,0x39,0x70,0x1c,0xf9,0xb4,0x6b,
  25160. 0xd2,0x5b,0xf5,0xf0,0x59,0x5b,0xbe,0x24,
  25161. 0x65,0x51,0x41,0x43,0x8e,0x7a,0x10,0x0b
  25162. };
  25163. WOLFSSL_SMALL_STACK_STATIC const byte sig6[] = {
  25164. 0x0a,0xab,0x4c,0x90,0x05,0x01,0xb3,0xe2,
  25165. 0x4d,0x7c,0xdf,0x46,0x63,0x32,0x6a,0x3a,
  25166. 0x87,0xdf,0x5e,0x48,0x43,0xb2,0xcb,0xdb,
  25167. 0x67,0xcb,0xf6,0xe4,0x60,0xfe,0xc3,0x50,
  25168. 0xaa,0x53,0x71,0xb1,0x50,0x8f,0x9f,0x45,
  25169. 0x28,0xec,0xea,0x23,0xc4,0x36,0xd9,0x4b,
  25170. 0x5e,0x8f,0xcd,0x4f,0x68,0x1e,0x30,0xa6,
  25171. 0xac,0x00,0xa9,0x70,0x4a,0x18,0x8a,0x03
  25172. };
  25173. WOLFSSL_SMALL_STACK_STATIC const byte* sigs[] = {sig1, sig2, sig3, sig4, sig5, sig6};
  25174. WOLFSSL_SMALL_STACK_STATIC const byte msg1[] = {0x0 };
  25175. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] = {0x72};
  25176. WOLFSSL_SMALL_STACK_STATIC const byte msg3[] = {0xAF,0x82};
  25177. /* test of a 1024 byte long message */
  25178. WOLFSSL_SMALL_STACK_STATIC const byte msg4[] = {
  25179. 0x08,0xb8,0xb2,0xb7,0x33,0x42,0x42,0x43,
  25180. 0x76,0x0f,0xe4,0x26,0xa4,0xb5,0x49,0x08,
  25181. 0x63,0x21,0x10,0xa6,0x6c,0x2f,0x65,0x91,
  25182. 0xea,0xbd,0x33,0x45,0xe3,0xe4,0xeb,0x98,
  25183. 0xfa,0x6e,0x26,0x4b,0xf0,0x9e,0xfe,0x12,
  25184. 0xee,0x50,0xf8,0xf5,0x4e,0x9f,0x77,0xb1,
  25185. 0xe3,0x55,0xf6,0xc5,0x05,0x44,0xe2,0x3f,
  25186. 0xb1,0x43,0x3d,0xdf,0x73,0xbe,0x84,0xd8,
  25187. 0x79,0xde,0x7c,0x00,0x46,0xdc,0x49,0x96,
  25188. 0xd9,0xe7,0x73,0xf4,0xbc,0x9e,0xfe,0x57,
  25189. 0x38,0x82,0x9a,0xdb,0x26,0xc8,0x1b,0x37,
  25190. 0xc9,0x3a,0x1b,0x27,0x0b,0x20,0x32,0x9d,
  25191. 0x65,0x86,0x75,0xfc,0x6e,0xa5,0x34,0xe0,
  25192. 0x81,0x0a,0x44,0x32,0x82,0x6b,0xf5,0x8c,
  25193. 0x94,0x1e,0xfb,0x65,0xd5,0x7a,0x33,0x8b,
  25194. 0xbd,0x2e,0x26,0x64,0x0f,0x89,0xff,0xbc,
  25195. 0x1a,0x85,0x8e,0xfc,0xb8,0x55,0x0e,0xe3,
  25196. 0xa5,0xe1,0x99,0x8b,0xd1,0x77,0xe9,0x3a,
  25197. 0x73,0x63,0xc3,0x44,0xfe,0x6b,0x19,0x9e,
  25198. 0xe5,0xd0,0x2e,0x82,0xd5,0x22,0xc4,0xfe,
  25199. 0xba,0x15,0x45,0x2f,0x80,0x28,0x8a,0x82,
  25200. 0x1a,0x57,0x91,0x16,0xec,0x6d,0xad,0x2b,
  25201. 0x3b,0x31,0x0d,0xa9,0x03,0x40,0x1a,0xa6,
  25202. 0x21,0x00,0xab,0x5d,0x1a,0x36,0x55,0x3e,
  25203. 0x06,0x20,0x3b,0x33,0x89,0x0c,0xc9,0xb8,
  25204. 0x32,0xf7,0x9e,0xf8,0x05,0x60,0xcc,0xb9,
  25205. 0xa3,0x9c,0xe7,0x67,0x96,0x7e,0xd6,0x28,
  25206. 0xc6,0xad,0x57,0x3c,0xb1,0x16,0xdb,0xef,
  25207. 0xef,0xd7,0x54,0x99,0xda,0x96,0xbd,0x68,
  25208. 0xa8,0xa9,0x7b,0x92,0x8a,0x8b,0xbc,0x10,
  25209. 0x3b,0x66,0x21,0xfc,0xde,0x2b,0xec,0xa1,
  25210. 0x23,0x1d,0x20,0x6b,0xe6,0xcd,0x9e,0xc7,
  25211. 0xaf,0xf6,0xf6,0xc9,0x4f,0xcd,0x72,0x04,
  25212. 0xed,0x34,0x55,0xc6,0x8c,0x83,0xf4,0xa4,
  25213. 0x1d,0xa4,0xaf,0x2b,0x74,0xef,0x5c,0x53,
  25214. 0xf1,0xd8,0xac,0x70,0xbd,0xcb,0x7e,0xd1,
  25215. 0x85,0xce,0x81,0xbd,0x84,0x35,0x9d,0x44,
  25216. 0x25,0x4d,0x95,0x62,0x9e,0x98,0x55,0xa9,
  25217. 0x4a,0x7c,0x19,0x58,0xd1,0xf8,0xad,0xa5,
  25218. 0xd0,0x53,0x2e,0xd8,0xa5,0xaa,0x3f,0xb2,
  25219. 0xd1,0x7b,0xa7,0x0e,0xb6,0x24,0x8e,0x59,
  25220. 0x4e,0x1a,0x22,0x97,0xac,0xbb,0xb3,0x9d,
  25221. 0x50,0x2f,0x1a,0x8c,0x6e,0xb6,0xf1,0xce,
  25222. 0x22,0xb3,0xde,0x1a,0x1f,0x40,0xcc,0x24,
  25223. 0x55,0x41,0x19,0xa8,0x31,0xa9,0xaa,0xd6,
  25224. 0x07,0x9c,0xad,0x88,0x42,0x5d,0xe6,0xbd,
  25225. 0xe1,0xa9,0x18,0x7e,0xbb,0x60,0x92,0xcf,
  25226. 0x67,0xbf,0x2b,0x13,0xfd,0x65,0xf2,0x70,
  25227. 0x88,0xd7,0x8b,0x7e,0x88,0x3c,0x87,0x59,
  25228. 0xd2,0xc4,0xf5,0xc6,0x5a,0xdb,0x75,0x53,
  25229. 0x87,0x8a,0xd5,0x75,0xf9,0xfa,0xd8,0x78,
  25230. 0xe8,0x0a,0x0c,0x9b,0xa6,0x3b,0xcb,0xcc,
  25231. 0x27,0x32,0xe6,0x94,0x85,0xbb,0xc9,0xc9,
  25232. 0x0b,0xfb,0xd6,0x24,0x81,0xd9,0x08,0x9b,
  25233. 0xec,0xcf,0x80,0xcf,0xe2,0xdf,0x16,0xa2,
  25234. 0xcf,0x65,0xbd,0x92,0xdd,0x59,0x7b,0x07,
  25235. 0x07,0xe0,0x91,0x7a,0xf4,0x8b,0xbb,0x75,
  25236. 0xfe,0xd4,0x13,0xd2,0x38,0xf5,0x55,0x5a,
  25237. 0x7a,0x56,0x9d,0x80,0xc3,0x41,0x4a,0x8d,
  25238. 0x08,0x59,0xdc,0x65,0xa4,0x61,0x28,0xba,
  25239. 0xb2,0x7a,0xf8,0x7a,0x71,0x31,0x4f,0x31,
  25240. 0x8c,0x78,0x2b,0x23,0xeb,0xfe,0x80,0x8b,
  25241. 0x82,0xb0,0xce,0x26,0x40,0x1d,0x2e,0x22,
  25242. 0xf0,0x4d,0x83,0xd1,0x25,0x5d,0xc5,0x1a,
  25243. 0xdd,0xd3,0xb7,0x5a,0x2b,0x1a,0xe0,0x78,
  25244. 0x45,0x04,0xdf,0x54,0x3a,0xf8,0x96,0x9b,
  25245. 0xe3,0xea,0x70,0x82,0xff,0x7f,0xc9,0x88,
  25246. 0x8c,0x14,0x4d,0xa2,0xaf,0x58,0x42,0x9e,
  25247. 0xc9,0x60,0x31,0xdb,0xca,0xd3,0xda,0xd9,
  25248. 0xaf,0x0d,0xcb,0xaa,0xaf,0x26,0x8c,0xb8,
  25249. 0xfc,0xff,0xea,0xd9,0x4f,0x3c,0x7c,0xa4,
  25250. 0x95,0xe0,0x56,0xa9,0xb4,0x7a,0xcd,0xb7,
  25251. 0x51,0xfb,0x73,0xe6,0x66,0xc6,0xc6,0x55,
  25252. 0xad,0xe8,0x29,0x72,0x97,0xd0,0x7a,0xd1,
  25253. 0xba,0x5e,0x43,0xf1,0xbc,0xa3,0x23,0x01,
  25254. 0x65,0x13,0x39,0xe2,0x29,0x04,0xcc,0x8c,
  25255. 0x42,0xf5,0x8c,0x30,0xc0,0x4a,0xaf,0xdb,
  25256. 0x03,0x8d,0xda,0x08,0x47,0xdd,0x98,0x8d,
  25257. 0xcd,0xa6,0xf3,0xbf,0xd1,0x5c,0x4b,0x4c,
  25258. 0x45,0x25,0x00,0x4a,0xa0,0x6e,0xef,0xf8,
  25259. 0xca,0x61,0x78,0x3a,0xac,0xec,0x57,0xfb,
  25260. 0x3d,0x1f,0x92,0xb0,0xfe,0x2f,0xd1,0xa8,
  25261. 0x5f,0x67,0x24,0x51,0x7b,0x65,0xe6,0x14,
  25262. 0xad,0x68,0x08,0xd6,0xf6,0xee,0x34,0xdf,
  25263. 0xf7,0x31,0x0f,0xdc,0x82,0xae,0xbf,0xd9,
  25264. 0x04,0xb0,0x1e,0x1d,0xc5,0x4b,0x29,0x27,
  25265. 0x09,0x4b,0x2d,0xb6,0x8d,0x6f,0x90,0x3b,
  25266. 0x68,0x40,0x1a,0xde,0xbf,0x5a,0x7e,0x08,
  25267. 0xd7,0x8f,0xf4,0xef,0x5d,0x63,0x65,0x3a,
  25268. 0x65,0x04,0x0c,0xf9,0xbf,0xd4,0xac,0xa7,
  25269. 0x98,0x4a,0x74,0xd3,0x71,0x45,0x98,0x67,
  25270. 0x80,0xfc,0x0b,0x16,0xac,0x45,0x16,0x49,
  25271. 0xde,0x61,0x88,0xa7,0xdb,0xdf,0x19,0x1f,
  25272. 0x64,0xb5,0xfc,0x5e,0x2a,0xb4,0x7b,0x57,
  25273. 0xf7,0xf7,0x27,0x6c,0xd4,0x19,0xc1,0x7a,
  25274. 0x3c,0xa8,0xe1,0xb9,0x39,0xae,0x49,0xe4,
  25275. 0x88,0xac,0xba,0x6b,0x96,0x56,0x10,0xb5,
  25276. 0x48,0x01,0x09,0xc8,0xb1,0x7b,0x80,0xe1,
  25277. 0xb7,0xb7,0x50,0xdf,0xc7,0x59,0x8d,0x5d,
  25278. 0x50,0x11,0xfd,0x2d,0xcc,0x56,0x00,0xa3,
  25279. 0x2e,0xf5,0xb5,0x2a,0x1e,0xcc,0x82,0x0e,
  25280. 0x30,0x8a,0xa3,0x42,0x72,0x1a,0xac,0x09,
  25281. 0x43,0xbf,0x66,0x86,0xb6,0x4b,0x25,0x79,
  25282. 0x37,0x65,0x04,0xcc,0xc4,0x93,0xd9,0x7e,
  25283. 0x6a,0xed,0x3f,0xb0,0xf9,0xcd,0x71,0xa4,
  25284. 0x3d,0xd4,0x97,0xf0,0x1f,0x17,0xc0,0xe2,
  25285. 0xcb,0x37,0x97,0xaa,0x2a,0x2f,0x25,0x66,
  25286. 0x56,0x16,0x8e,0x6c,0x49,0x6a,0xfc,0x5f,
  25287. 0xb9,0x32,0x46,0xf6,0xb1,0x11,0x63,0x98,
  25288. 0xa3,0x46,0xf1,0xa6,0x41,0xf3,0xb0,0x41,
  25289. 0xe9,0x89,0xf7,0x91,0x4f,0x90,0xcc,0x2c,
  25290. 0x7f,0xff,0x35,0x78,0x76,0xe5,0x06,0xb5,
  25291. 0x0d,0x33,0x4b,0xa7,0x7c,0x22,0x5b,0xc3,
  25292. 0x07,0xba,0x53,0x71,0x52,0xf3,0xf1,0x61,
  25293. 0x0e,0x4e,0xaf,0xe5,0x95,0xf6,0xd9,0xd9,
  25294. 0x0d,0x11,0xfa,0xa9,0x33,0xa1,0x5e,0xf1,
  25295. 0x36,0x95,0x46,0x86,0x8a,0x7f,0x3a,0x45,
  25296. 0xa9,0x67,0x68,0xd4,0x0f,0xd9,0xd0,0x34,
  25297. 0x12,0xc0,0x91,0xc6,0x31,0x5c,0xf4,0xfd,
  25298. 0xe7,0xcb,0x68,0x60,0x69,0x37,0x38,0x0d,
  25299. 0xb2,0xea,0xaa,0x70,0x7b,0x4c,0x41,0x85,
  25300. 0xc3,0x2e,0xdd,0xcd,0xd3,0x06,0x70,0x5e,
  25301. 0x4d,0xc1,0xff,0xc8,0x72,0xee,0xee,0x47,
  25302. 0x5a,0x64,0xdf,0xac,0x86,0xab,0xa4,0x1c,
  25303. 0x06,0x18,0x98,0x3f,0x87,0x41,0xc5,0xef,
  25304. 0x68,0xd3,0xa1,0x01,0xe8,0xa3,0xb8,0xca,
  25305. 0xc6,0x0c,0x90,0x5c,0x15,0xfc,0x91,0x08,
  25306. 0x40,0xb9,0x4c,0x00,0xa0,0xb9,0xd0
  25307. };
  25308. WOLFSSL_SMALL_STACK_STATIC const byte* msgs[] = {msg1, msg2, msg3, msg1, msg1, msg4};
  25309. WOLFSSL_SMALL_STACK_STATIC const word16 msgSz[] = {0 /*sizeof(msg1)*/,
  25310. sizeof(msg2),
  25311. sizeof(msg3),
  25312. 0 /*sizeof(msg1)*/,
  25313. 0 /*sizeof(msg1)*/,
  25314. sizeof(msg4)
  25315. };
  25316. #ifndef NO_ASN
  25317. static byte privateEd25519[] = {
  25318. 0x30,0x2e,0x02,0x01,0x00,0x30,0x05,0x06,
  25319. 0x03,0x2b,0x65,0x70,0x04,0x22,0x04,0x20,
  25320. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  25321. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  25322. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  25323. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60
  25324. };
  25325. static byte badPrivateEd25519[] = {
  25326. 0x30,0x52,0x02,0x01,0x00,0x30,0x05,0x06,
  25327. 0x03,0x2b,0x65,0x70,0x04,0x22,0x04,0x20,
  25328. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  25329. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  25330. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  25331. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60,
  25332. 0xa1,0x22,0x04,0x21,0xd7,0x5a,0x98,0x01, /* octet len 0x20 -> 0x21 */
  25333. 0x82,0xb1,0x0a,0xb7,0xd5,0x4b,0xfe,0xd3,
  25334. 0xc9,0x64,0x07,0x3a,0x0e,0xe1,0x72,0xf3,
  25335. 0xda,0xa6,0x23,0x25,0xaf,0x02,0x1a,0x68,
  25336. 0xf7,0x07,0x51,0x1a,
  25337. 0x00 /* add additional bytes to make the pubkey bigger */
  25338. };
  25339. static byte publicEd25519[] = {
  25340. 0x30,0x2a,0x30,0x05,0x06,0x03,0x2b,0x65,
  25341. 0x70,0x03,0x21,0x00,0xd7,0x5a,0x98,0x01,
  25342. 0x82,0xb1,0x0a,0xb7,0xd5,0x4b,0xfe,0xd3,
  25343. 0xc9,0x64,0x07,0x3a,0x0e,0xe1,0x72,0xf3,
  25344. 0xda,0xa6,0x23,0x25,0xaf,0x02,0x1a,0x68,
  25345. 0xf7,0x07,0x51,0x1a
  25346. };
  25347. /* size has been altered to catch if sanity check is done */
  25348. static byte badPublicEd25519[] = {
  25349. 0x30,0x2a,0x30,0x05,0x06,0x03,0x2b,0x65,
  25350. 0x70,0x03,0x21,0x00,0xd7,0x5a,0x98,0x01,
  25351. 0x82,0xb1,0x0a,0xb7,0xd5,0x4b,0xfe,0xd3,
  25352. 0xc9,0x64,0x07,0x3a,0x0e,0xe1,0x72,0xf3,
  25353. 0xda,0xa6,0x23,0x25,0xaf,0x02,0x1a,0x68,
  25354. 0xf7,0x07,0x51,0x1a,
  25355. 0x00 /* add an additional byte to make the pubkey appear bigger */
  25356. };
  25357. static byte privPubEd25519[] = {
  25358. 0x30,0x50,0x02,0x01,0x00,0x30,0x05,0x06,
  25359. 0x03,0x2b,0x65,0x70,0x04,0x22,0x04,0x20,
  25360. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  25361. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  25362. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  25363. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60,
  25364. 0x81,0x20,0xd7,0x5a,0x98,0x01,0x82,0xb1,
  25365. 0x0a,0xb7,0xd5,0x4b,0xfe,0xd3,0xc9,0x64,
  25366. 0x07,0x3a,0x0e,0xe1,0x72,0xf3,0xda,0xa6,
  25367. 0x23,0x25,0xaf,0x02,0x1a,0x68,0xf7,0x07,
  25368. 0x51,0x1a
  25369. };
  25370. word32 idx;
  25371. #endif /* NO_ASN */
  25372. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_EXPORT && HAVE_ED25519_KEY_IMPORT */
  25373. #if !defined(NO_ASN) && defined(HAVE_ED25519_SIGN)
  25374. ed25519_key key3;
  25375. #endif
  25376. /* create ed25519 keys */
  25377. #ifndef HAVE_FIPS
  25378. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  25379. #else
  25380. ret = wc_InitRng(&rng);
  25381. #endif
  25382. if (ret != 0)
  25383. return -11000;
  25384. wc_ed25519_init_ex(&key, HEAP_HINT, devId);
  25385. wc_ed25519_init_ex(&key2, HEAP_HINT, devId);
  25386. #if !defined(NO_ASN) && defined(HAVE_ED25519_SIGN)
  25387. wc_ed25519_init_ex(&key3, HEAP_HINT, devId);
  25388. #endif
  25389. wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  25390. wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key2);
  25391. /* helper functions for signature and key size */
  25392. keySz = wc_ed25519_size(&key);
  25393. sigSz = wc_ed25519_sig_size(&key);
  25394. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_EXPORT) &&\
  25395. defined(HAVE_ED25519_KEY_IMPORT)
  25396. for (i = 0; i < 6; i++) {
  25397. outlen = sizeof(out);
  25398. XMEMSET(out, 0, sizeof(out));
  25399. if (wc_ed25519_import_private_key(sKeys[i], ED25519_KEY_SIZE, pKeys[i],
  25400. pKeySz[i], &key) != 0)
  25401. return -11001 - i;
  25402. if (wc_ed25519_sign_msg(msgs[i], msgSz[i], out, &outlen, &key) != 0)
  25403. return -11011 - i;
  25404. if (XMEMCMP(out, sigs[i], 64))
  25405. return -11021 - i;
  25406. #if defined(HAVE_ED25519_VERIFY)
  25407. /* test verify on good msg */
  25408. if (wc_ed25519_verify_msg(out, outlen, msgs[i], msgSz[i], &verify,
  25409. &key) != 0 || verify != 1)
  25410. return -11031 - i;
  25411. #ifdef WOLFSSL_ED25519_STREAMING_VERIFY
  25412. /* test verify on good msg using streaming interface directly */
  25413. if (wc_ed25519_verify_msg_init(out, outlen,
  25414. &key, (byte)Ed25519, NULL, 0) != 0)
  25415. return -11211 - i;
  25416. for (j = 0; j < msgSz[i]; j += i) {
  25417. if (wc_ed25519_verify_msg_update(msgs[i] + j, MIN(i, msgSz[i] - j), &key) != 0)
  25418. return -11221 - i;
  25419. }
  25420. if (wc_ed25519_verify_msg_final(out, outlen, &verify,
  25421. &key) != 0 || verify != 1)
  25422. return -11231 - i;
  25423. #endif /* WOLFSSL_ED25519_STREAMING_VERIFY */
  25424. /* test verify on bad msg */
  25425. out[outlen-1] = out[outlen-1] + 1;
  25426. if (wc_ed25519_verify_msg(out, outlen, msgs[i], msgSz[i], &verify,
  25427. &key) == 0 || verify == 1)
  25428. return -11041 - i;
  25429. #endif /* HAVE_ED25519_VERIFY */
  25430. /* test api for import/exporting keys */
  25431. exportPSz = sizeof(exportPKey);
  25432. exportSSz = sizeof(exportSKey);
  25433. if (wc_ed25519_export_public(&key, exportPKey, &exportPSz) != 0)
  25434. return -11051 - i;
  25435. if (wc_ed25519_import_public_ex(exportPKey, exportPSz, &key2, 1) != 0)
  25436. return -11061 - i;
  25437. if (wc_ed25519_export_private_only(&key, exportSKey, &exportSSz) != 0)
  25438. return -11071 - i;
  25439. if (wc_ed25519_import_private_key(exportSKey, exportSSz,
  25440. exportPKey, exportPSz, &key2) != 0)
  25441. return -11081 - i;
  25442. /* clear "out" buffer and test sign with imported keys */
  25443. outlen = sizeof(out);
  25444. XMEMSET(out, 0, sizeof(out));
  25445. if (wc_ed25519_sign_msg(msgs[i], msgSz[i], out, &outlen, &key2) != 0)
  25446. return -11091 - i;
  25447. #if defined(HAVE_ED25519_VERIFY)
  25448. if (wc_ed25519_verify_msg(out, outlen, msgs[i], msgSz[i], &verify,
  25449. &key2) != 0 || verify != 1)
  25450. return -11101 - i;
  25451. if (XMEMCMP(out, sigs[i], 64))
  25452. return -11111 - i;
  25453. #endif /* HAVE_ED25519_VERIFY */
  25454. }
  25455. ret = ed25519ctx_test();
  25456. if (ret != 0)
  25457. return ret;
  25458. ret = ed25519ph_test();
  25459. if (ret != 0)
  25460. return ret;
  25461. #ifndef NO_ASN
  25462. /* Try ASN.1 encoded private-only key and public key. */
  25463. idx = 0;
  25464. if (wc_Ed25519PrivateKeyDecode(privateEd25519, &idx, &key3,
  25465. sizeof(privateEd25519)) != 0)
  25466. return -11121;
  25467. idx = 0;
  25468. if (wc_Ed25519PrivateKeyDecode(badPrivateEd25519, &idx, &key3,
  25469. sizeof(badPrivateEd25519)) == 0)
  25470. return -11122;
  25471. if (wc_ed25519_sign_msg(msgs[0], msgSz[0], out, &outlen, &key3)
  25472. != BAD_FUNC_ARG)
  25473. return -11131;
  25474. /* try with a buffer size that is too large */
  25475. idx = 0;
  25476. if (wc_Ed25519PublicKeyDecode(badPublicEd25519, &idx, &key3,
  25477. sizeof(badPublicEd25519)) == 0)
  25478. return -11140;
  25479. idx = 0;
  25480. if (wc_Ed25519PublicKeyDecode(publicEd25519, &idx, &key3,
  25481. sizeof(publicEd25519)) != 0)
  25482. return -11141;
  25483. if (wc_ed25519_sign_msg(msgs[0], msgSz[0], out, &outlen, &key3) != 0)
  25484. return -11151;
  25485. if (XMEMCMP(out, sigs[0], 64))
  25486. return -11161;
  25487. #if defined(HAVE_ED25519_VERIFY)
  25488. /* test verify on good msg */
  25489. if (wc_ed25519_verify_msg(out, outlen, msgs[0], msgSz[0], &verify, &key3)
  25490. != 0 || verify != 1)
  25491. return -11171;
  25492. #endif /* HAVE_ED25519_VERIFY */
  25493. wc_ed25519_free(&key3);
  25494. wc_ed25519_init(&key3);
  25495. idx = 0;
  25496. if (wc_Ed25519PrivateKeyDecode(privPubEd25519, &idx, &key3,
  25497. sizeof(privPubEd25519)) != 0)
  25498. return -11181;
  25499. if (wc_ed25519_sign_msg(msgs[0], msgSz[0], out, &outlen, &key3) != 0)
  25500. return -11191;
  25501. if (XMEMCMP(out, sigs[0], 64))
  25502. return -11201;
  25503. wc_ed25519_free(&key3);
  25504. #endif /* NO_ASN */
  25505. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_EXPORT && HAVE_ED25519_KEY_IMPORT */
  25506. /* clean up keys when done */
  25507. wc_ed25519_free(&key);
  25508. wc_ed25519_free(&key2);
  25509. #if defined(HAVE_HASHDRBG) || defined(NO_RC4)
  25510. wc_FreeRng(&rng);
  25511. #endif
  25512. /* hush warnings of unused keySz and sigSz */
  25513. (void)keySz;
  25514. (void)sigSz;
  25515. #ifdef WOLFSSL_TEST_CERT
  25516. ret = ed25519_test_cert();
  25517. if (ret < 0)
  25518. return ret;
  25519. #ifdef WOLFSSL_CERT_GEN
  25520. ret = ed25519_test_make_cert();
  25521. if (ret < 0)
  25522. return ret;
  25523. #endif /* WOLFSSL_CERT_GEN */
  25524. #endif /* WOLFSSL_TEST_CERT */
  25525. return 0;
  25526. }
  25527. #endif /* HAVE_ED25519 */
  25528. #ifdef HAVE_CURVE448
  25529. #if defined(HAVE_CURVE448_SHARED_SECRET) && \
  25530. defined(HAVE_CURVE448_KEY_IMPORT)
  25531. /* Test the wc_curve448_check_public API.
  25532. *
  25533. * returns 0 on success and -ve on failure.
  25534. */
  25535. static int curve448_check_public_test(void)
  25536. {
  25537. /* Little-endian values that will fail */
  25538. byte fail_le[][CURVE448_KEY_SIZE] = {
  25539. {
  25540. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25541. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25542. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25543. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25544. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25545. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25546. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  25547. },
  25548. {
  25549. 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25550. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25551. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25552. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25553. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25554. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25555. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  25556. },
  25557. };
  25558. /* Big-endian values that will fail */
  25559. byte fail_be[][CURVE448_KEY_SIZE] = {
  25560. {
  25561. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25562. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25563. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25564. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25565. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25566. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25567. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  25568. },
  25569. {
  25570. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25571. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25572. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25573. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25574. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25575. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25576. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  25577. },
  25578. };
  25579. /* Good or valid public value */
  25580. byte good[CURVE448_KEY_SIZE] = {
  25581. 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25582. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25583. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25584. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25585. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25586. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25587. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  25588. };
  25589. int i;
  25590. /* Parameter checks */
  25591. /* NULL pointer */
  25592. if (wc_curve448_check_public(NULL, 0, EC448_LITTLE_ENDIAN) !=
  25593. BAD_FUNC_ARG) {
  25594. return -11300;
  25595. }
  25596. if (wc_curve448_check_public(NULL, 0, EC448_BIG_ENDIAN) != BAD_FUNC_ARG) {
  25597. return -11301;
  25598. }
  25599. /* Length of 0 treated differently to other invalid lengths for TLS */
  25600. if (wc_curve448_check_public(good, 0, EC448_LITTLE_ENDIAN) != BUFFER_E)
  25601. return -11302;
  25602. if (wc_curve448_check_public(good, 0, EC448_BIG_ENDIAN) != BUFFER_E)
  25603. return -11303;
  25604. /* Length not CURVE448_KEY_SIZE */
  25605. for (i = 1; i < CURVE448_KEY_SIZE + 2; i++) {
  25606. if (i == CURVE448_KEY_SIZE)
  25607. continue;
  25608. if (wc_curve448_check_public(good, i, EC448_LITTLE_ENDIAN) !=
  25609. ECC_BAD_ARG_E) {
  25610. return -11304 - i;
  25611. }
  25612. if (wc_curve448_check_public(good, i, EC448_BIG_ENDIAN) !=
  25613. ECC_BAD_ARG_E) {
  25614. return -11314 - i;
  25615. }
  25616. }
  25617. /* Little-endian fail cases */
  25618. for (i = 0; i < (int)(sizeof(fail_le) / sizeof(*fail_le)); i++) {
  25619. if (wc_curve448_check_public(fail_le[i], CURVE448_KEY_SIZE,
  25620. EC448_LITTLE_ENDIAN) == 0) {
  25621. return -11324 - i;
  25622. }
  25623. }
  25624. /* Big-endian fail cases */
  25625. for (i = 0; i < (int)(sizeof(fail_be) / sizeof(*fail_be)); i++) {
  25626. if (wc_curve448_check_public(fail_be[i], CURVE448_KEY_SIZE,
  25627. EC448_BIG_ENDIAN) == 0) {
  25628. return -11334 - i;
  25629. }
  25630. }
  25631. /* Check a valid public value works! */
  25632. if (wc_curve448_check_public(good, CURVE448_KEY_SIZE,
  25633. EC448_LITTLE_ENDIAN) != 0) {
  25634. return -11344;
  25635. }
  25636. if (wc_curve448_check_public(good, CURVE448_KEY_SIZE,
  25637. EC448_BIG_ENDIAN) != 0) {
  25638. return -11345;
  25639. }
  25640. return 0;
  25641. }
  25642. #endif /* HAVE_CURVE448_SHARED_SECRET && HAVE_CURVE448_KEY_IMPORT */
  25643. WOLFSSL_TEST_SUBROUTINE int curve448_test(void)
  25644. {
  25645. WC_RNG rng;
  25646. int ret;
  25647. #ifdef HAVE_CURVE448_SHARED_SECRET
  25648. byte sharedA[CURVE448_KEY_SIZE];
  25649. byte sharedB[CURVE448_KEY_SIZE];
  25650. word32 y;
  25651. #endif
  25652. #ifdef HAVE_CURVE448_KEY_EXPORT
  25653. byte exportBuf[CURVE448_KEY_SIZE];
  25654. #endif
  25655. word32 x;
  25656. curve448_key userA, userB, pubKey;
  25657. #if defined(HAVE_CURVE448_SHARED_SECRET) && \
  25658. defined(HAVE_CURVE448_KEY_IMPORT)
  25659. /* test vectors from
  25660. https://www.rfc-editor.org/rfc/rfc7748.html
  25661. */
  25662. /* secret key for party a */
  25663. byte sa[] = {
  25664. 0x6b, 0x72, 0x98, 0xa5, 0xc0, 0xd8, 0xc2, 0x9a,
  25665. 0x1d, 0xab, 0x27, 0xf1, 0xa6, 0x82, 0x63, 0x00,
  25666. 0x91, 0x73, 0x89, 0x44, 0x97, 0x41, 0xa9, 0x74,
  25667. 0xf5, 0xba, 0xc9, 0xd9, 0x8d, 0xc2, 0x98, 0xd4,
  25668. 0x65, 0x55, 0xbc, 0xe8, 0xba, 0xe8, 0x9e, 0xee,
  25669. 0xd4, 0x00, 0x58, 0x4b, 0xb0, 0x46, 0xcf, 0x75,
  25670. 0x57, 0x9f, 0x51, 0xd1, 0x25, 0x49, 0x8f, 0x9a,
  25671. };
  25672. /* public key for party a */
  25673. byte pa[] = {
  25674. 0xa0, 0x1f, 0xc4, 0x32, 0xe5, 0x80, 0x7f, 0x17,
  25675. 0x53, 0x0d, 0x12, 0x88, 0xda, 0x12, 0x5b, 0x0c,
  25676. 0xd4, 0x53, 0xd9, 0x41, 0x72, 0x64, 0x36, 0xc8,
  25677. 0xbb, 0xd9, 0xc5, 0x22, 0x2c, 0x3d, 0xa7, 0xfa,
  25678. 0x63, 0x9c, 0xe0, 0x3d, 0xb8, 0xd2, 0x3b, 0x27,
  25679. 0x4a, 0x07, 0x21, 0xa1, 0xae, 0xd5, 0x22, 0x7d,
  25680. 0xe6, 0xe3, 0xb7, 0x31, 0xcc, 0xf7, 0x08, 0x9b,
  25681. };
  25682. /* secret key for party b */
  25683. byte sb[] = {
  25684. 0x2d, 0x99, 0x73, 0x51, 0xb6, 0x10, 0x6f, 0x36,
  25685. 0xb0, 0xd1, 0x09, 0x1b, 0x92, 0x9c, 0x4c, 0x37,
  25686. 0x21, 0x3e, 0x0d, 0x2b, 0x97, 0xe8, 0x5e, 0xbb,
  25687. 0x20, 0xc1, 0x27, 0x69, 0x1d, 0x0d, 0xad, 0x8f,
  25688. 0x1d, 0x81, 0x75, 0xb0, 0x72, 0x37, 0x45, 0xe6,
  25689. 0x39, 0xa3, 0xcb, 0x70, 0x44, 0x29, 0x0b, 0x99,
  25690. 0xe0, 0xe2, 0xa0, 0xc2, 0x7a, 0x6a, 0x30, 0x1c,
  25691. };
  25692. /* public key for party b */
  25693. byte pb[] = {
  25694. 0x09, 0x36, 0xf3, 0x7b, 0xc6, 0xc1, 0xbd, 0x07,
  25695. 0xae, 0x3d, 0xec, 0x7a, 0xb5, 0xdc, 0x06, 0xa7,
  25696. 0x3c, 0xa1, 0x32, 0x42, 0xfb, 0x34, 0x3e, 0xfc,
  25697. 0x72, 0xb9, 0xd8, 0x27, 0x30, 0xb4, 0x45, 0xf3,
  25698. 0xd4, 0xb0, 0xbd, 0x07, 0x71, 0x62, 0xa4, 0x6d,
  25699. 0xcf, 0xec, 0x6f, 0x9b, 0x59, 0x0b, 0xfc, 0xbc,
  25700. 0xf5, 0x20, 0xcd, 0xb0, 0x29, 0xa8, 0xb7, 0x3e,
  25701. };
  25702. /* expected shared key */
  25703. byte ss[] = {
  25704. 0x9d, 0x87, 0x4a, 0x51, 0x37, 0x50, 0x9a, 0x44,
  25705. 0x9a, 0xd5, 0x85, 0x30, 0x40, 0x24, 0x1c, 0x52,
  25706. 0x36, 0x39, 0x54, 0x35, 0xc3, 0x64, 0x24, 0xfd,
  25707. 0x56, 0x0b, 0x0c, 0xb6, 0x2b, 0x28, 0x1d, 0x28,
  25708. 0x52, 0x75, 0xa7, 0x40, 0xce, 0x32, 0xa2, 0x2d,
  25709. 0xd1, 0x74, 0x0f, 0x4a, 0xa9, 0x16, 0x1c, 0xec,
  25710. 0x95, 0xcc, 0xc6, 0x1a, 0x18, 0xf4, 0xff, 0x07,
  25711. };
  25712. #endif /* HAVE_CURVE448_SHARED_SECRET */
  25713. (void)x;
  25714. #ifndef HAVE_FIPS
  25715. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  25716. #else
  25717. ret = wc_InitRng(&rng);
  25718. #endif
  25719. if (ret != 0)
  25720. return -11400;
  25721. wc_curve448_init(&userA);
  25722. wc_curve448_init(&userB);
  25723. wc_curve448_init(&pubKey);
  25724. /* make curve448 keys */
  25725. if (wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &userA) != 0)
  25726. return -11401;
  25727. if (wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &userB) != 0)
  25728. return -11402;
  25729. #ifdef HAVE_CURVE448_SHARED_SECRET
  25730. /* find shared secret key */
  25731. x = sizeof(sharedA);
  25732. if (wc_curve448_shared_secret(&userA, &userB, sharedA, &x) != 0)
  25733. return -11403;
  25734. y = sizeof(sharedB);
  25735. if (wc_curve448_shared_secret(&userB, &userA, sharedB, &y) != 0)
  25736. return -11404;
  25737. /* compare shared secret keys to test they are the same */
  25738. if (y != x)
  25739. return -11405;
  25740. if (XMEMCMP(sharedA, sharedB, x))
  25741. return -11406;
  25742. #endif
  25743. #ifdef HAVE_CURVE448_KEY_EXPORT
  25744. /* export a public key and import it for another user */
  25745. x = sizeof(exportBuf);
  25746. if (wc_curve448_export_public(&userA, exportBuf, &x) != 0)
  25747. return -11407;
  25748. #ifdef HAVE_CURVE448_KEY_IMPORT
  25749. if (wc_curve448_import_public(exportBuf, x, &pubKey) != 0)
  25750. return -11408;
  25751. #endif
  25752. #endif
  25753. #if defined(HAVE_CURVE448_SHARED_SECRET) && \
  25754. defined(HAVE_CURVE448_KEY_IMPORT)
  25755. /* test shared key after importing a public key */
  25756. XMEMSET(sharedB, 0, sizeof(sharedB));
  25757. y = sizeof(sharedB);
  25758. if (wc_curve448_shared_secret(&userB, &pubKey, sharedB, &y) != 0)
  25759. return -11409;
  25760. if (XMEMCMP(sharedA, sharedB, y))
  25761. return -11410;
  25762. /* import RFC test vectors and compare shared key */
  25763. if (wc_curve448_import_private_raw(sa, sizeof(sa), pa, sizeof(pa), &userA)
  25764. != 0)
  25765. return -11411;
  25766. if (wc_curve448_import_private_raw(sb, sizeof(sb), pb, sizeof(pb), &userB)
  25767. != 0)
  25768. return -11412;
  25769. /* test against known test vector */
  25770. XMEMSET(sharedB, 0, sizeof(sharedB));
  25771. y = sizeof(sharedB);
  25772. if (wc_curve448_shared_secret(&userA, &userB, sharedB, &y) != 0)
  25773. return -11413;
  25774. if (XMEMCMP(ss, sharedB, y))
  25775. return -11414;
  25776. /* test swapping roles of keys and generating same shared key */
  25777. XMEMSET(sharedB, 0, sizeof(sharedB));
  25778. y = sizeof(sharedB);
  25779. if (wc_curve448_shared_secret(&userB, &userA, sharedB, &y) != 0)
  25780. return -11415;
  25781. if (XMEMCMP(ss, sharedB, y))
  25782. return -11416;
  25783. /* test with 1 generated key and 1 from known test vector */
  25784. if (wc_curve448_import_private_raw(sa, sizeof(sa), pa, sizeof(pa), &userA)
  25785. != 0)
  25786. return -11417;
  25787. if (wc_curve448_make_key(&rng, 56, &userB) != 0)
  25788. return -11418;
  25789. x = sizeof(sharedA);
  25790. if (wc_curve448_shared_secret(&userA, &userB, sharedA, &x) != 0)
  25791. return -11419;
  25792. y = sizeof(sharedB);
  25793. if (wc_curve448_shared_secret(&userB, &userA, sharedB, &y) != 0)
  25794. return -11420;
  25795. /* compare shared secret keys to test they are the same */
  25796. if (y != x)
  25797. return -11421;
  25798. if (XMEMCMP(sharedA, sharedB, x))
  25799. return -11422;
  25800. ret = curve448_check_public_test();
  25801. if (ret != 0)
  25802. return ret;
  25803. #endif /* HAVE_CURVE448_SHARED_SECRET && HAVE_CURVE448_KEY_IMPORT */
  25804. /* clean up keys when done */
  25805. wc_curve448_free(&pubKey);
  25806. wc_curve448_free(&userB);
  25807. wc_curve448_free(&userA);
  25808. wc_FreeRng(&rng);
  25809. return 0;
  25810. }
  25811. #endif /* HAVE_CURVE448 */
  25812. #ifdef HAVE_ED448
  25813. #ifdef WOLFSSL_TEST_CERT
  25814. static int ed448_test_cert(void)
  25815. {
  25816. DecodedCert cert[2];
  25817. DecodedCert* serverCert = NULL;
  25818. DecodedCert* caCert = NULL;
  25819. #ifdef HAVE_ED448_VERIFY
  25820. ed448_key key;
  25821. ed448_key* pubKey = NULL;
  25822. int verify;
  25823. #endif /* HAVE_ED448_VERIFY */
  25824. int ret;
  25825. byte* tmp;
  25826. size_t bytes;
  25827. XFILE file;
  25828. tmp = (byte *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25829. if (tmp == NULL) {
  25830. ERROR_OUT(-11430, done);
  25831. }
  25832. #ifdef USE_CERT_BUFFERS_256
  25833. XMEMCPY(tmp, ca_ed448_cert, sizeof_ca_ed448_cert);
  25834. bytes = sizeof_ca_ed448_cert;
  25835. #elif !defined(NO_FILESYSTEM)
  25836. file = XFOPEN(caEd448Cert, "rb");
  25837. if (file == NULL) {
  25838. ERROR_OUT(-11431, done);
  25839. }
  25840. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  25841. XFCLOSE(file);
  25842. #else
  25843. /* No certificate to use. */
  25844. ERROR_OUT(-11432, done);
  25845. #endif
  25846. InitDecodedCert(&cert[0], tmp, (word32)bytes, 0);
  25847. caCert = &cert[0];
  25848. ret = ParseCert(caCert, CERT_TYPE, NO_VERIFY, NULL);
  25849. if (ret != 0) {
  25850. ERROR_OUT(-11433, done);
  25851. }
  25852. #ifdef USE_CERT_BUFFERS_256
  25853. XMEMCPY(tmp, server_ed448_cert, sizeof_server_ed448_cert);
  25854. bytes = sizeof_server_ed448_cert;
  25855. #elif !defined(NO_FILESYSTEM)
  25856. file = XFOPEN(serverEd448Cert, "rb");
  25857. if (file == NULL) {
  25858. ERROR_OUT(-11434, done);
  25859. }
  25860. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  25861. XFCLOSE(file);
  25862. #else
  25863. /* No certificate to use. */
  25864. ERROR_OUT(-11435, done);
  25865. #endif
  25866. InitDecodedCert(&cert[1], tmp, (word32)bytes, 0);
  25867. serverCert = &cert[1];
  25868. ret = ParseCert(serverCert, CERT_TYPE, NO_VERIFY, NULL);
  25869. if (ret != 0) {
  25870. ERROR_OUT(-11436, done);
  25871. }
  25872. #ifdef HAVE_ED448_VERIFY
  25873. ret = wc_ed448_init(&key);
  25874. if (ret < 0) {
  25875. ERROR_OUT(-11437, done);
  25876. }
  25877. pubKey = &key;
  25878. ret = wc_ed448_import_public(caCert->publicKey, caCert->pubKeySize, pubKey);
  25879. if (ret < 0) {
  25880. ERROR_OUT(-11438, done);
  25881. }
  25882. if (wc_ed448_verify_msg(serverCert->signature, serverCert->sigLength,
  25883. serverCert->source + serverCert->certBegin,
  25884. serverCert->sigIndex - serverCert->certBegin,
  25885. &verify, pubKey, NULL, 0) < 0 || verify != 1) {
  25886. ERROR_OUT(-11439, done);
  25887. }
  25888. #endif /* HAVE_ED448_VERIFY */
  25889. done:
  25890. if (tmp != NULL)
  25891. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25892. #ifdef HAVE_ED448_VERIFY
  25893. wc_ed448_free(pubKey);
  25894. #endif /* HAVE_ED448_VERIFY */
  25895. if (caCert != NULL)
  25896. FreeDecodedCert(caCert);
  25897. if (serverCert != NULL)
  25898. FreeDecodedCert(serverCert);
  25899. return ret;
  25900. }
  25901. static int ed448_test_make_cert(void)
  25902. {
  25903. WC_RNG rng;
  25904. Cert cert;
  25905. DecodedCert decode;
  25906. ed448_key key;
  25907. ed448_key* privKey = NULL;
  25908. int ret = 0;
  25909. byte* tmp = NULL;
  25910. wc_InitCert_ex(&cert, HEAP_HINT, devId);
  25911. #ifndef HAVE_FIPS
  25912. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  25913. #else
  25914. ret = wc_InitRng(&rng);
  25915. #endif
  25916. if (ret != 0)
  25917. return -11450;
  25918. wc_ed448_init(&key);
  25919. privKey = &key;
  25920. wc_ed448_make_key(&rng, ED448_KEY_SIZE, privKey);
  25921. cert.daysValid = 365 * 2;
  25922. cert.selfSigned = 1;
  25923. XMEMCPY(&cert.issuer, &certDefaultName, sizeof(CertName));
  25924. XMEMCPY(&cert.subject, &certDefaultName, sizeof(CertName));
  25925. cert.isCA = 0;
  25926. #ifdef WOLFSSL_CERT_EXT
  25927. ret = wc_SetKeyUsage(&cert, certKeyUsage);
  25928. if (ret < 0) {
  25929. ERROR_OUT(-11451, done);
  25930. }
  25931. ret = wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ED448_TYPE, privKey);
  25932. if (ret < 0) {
  25933. ERROR_OUT(-11452, done);
  25934. }
  25935. ret = wc_SetAuthKeyIdFromPublicKey_ex(&cert, ED448_TYPE, privKey);
  25936. if (ret < 0) {
  25937. ERROR_OUT(-11453, done);
  25938. }
  25939. #endif
  25940. tmp = (byte *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25941. if (tmp == NULL) {
  25942. ERROR_OUT(-11454, done);
  25943. }
  25944. cert.sigType = CTC_ED448;
  25945. ret = wc_MakeCert_ex(&cert, tmp, FOURK_BUF, ED448_TYPE, privKey, &rng);
  25946. if (ret < 0) {
  25947. ERROR_OUT(-11455, done);
  25948. }
  25949. ret = wc_SignCert_ex(cert.bodySz, cert.sigType, tmp, FOURK_BUF, ED448_TYPE,
  25950. privKey, &rng);
  25951. if (ret < 0) {
  25952. ERROR_OUT(-11456, done);
  25953. }
  25954. InitDecodedCert(&decode, tmp, ret, HEAP_HINT);
  25955. ret = ParseCert(&decode, CERT_TYPE, NO_VERIFY, 0);
  25956. FreeDecodedCert(&decode);
  25957. if (ret != 0) {
  25958. ERROR_OUT(-11457, done);
  25959. }
  25960. done:
  25961. if (tmp != NULL)
  25962. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25963. wc_ed448_free(privKey);
  25964. wc_FreeRng(&rng);
  25965. return ret;
  25966. }
  25967. #endif /* WOLFSSL_TEST_CERT */
  25968. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_EXPORT) && \
  25969. defined(HAVE_ED448_KEY_IMPORT)
  25970. static int ed448_ctx_test(void)
  25971. {
  25972. byte out[ED448_SIG_SIZE];
  25973. word32 outlen;
  25974. #ifdef HAVE_ED448_VERIFY
  25975. int verify;
  25976. #endif /* HAVE_ED448_VERIFY */
  25977. ed448_key key;
  25978. WOLFSSL_SMALL_STACK_STATIC const byte sKeyCtx[] = {
  25979. 0xc4, 0xea, 0xb0, 0x5d, 0x35, 0x70, 0x07, 0xc6,
  25980. 0x32, 0xf3, 0xdb, 0xb4, 0x84, 0x89, 0x92, 0x4d,
  25981. 0x55, 0x2b, 0x08, 0xfe, 0x0c, 0x35, 0x3a, 0x0d,
  25982. 0x4a, 0x1f, 0x00, 0xac, 0xda, 0x2c, 0x46, 0x3a,
  25983. 0xfb, 0xea, 0x67, 0xc5, 0xe8, 0xd2, 0x87, 0x7c,
  25984. 0x5e, 0x3b, 0xc3, 0x97, 0xa6, 0x59, 0x94, 0x9e,
  25985. 0xf8, 0x02, 0x1e, 0x95, 0x4e, 0x0a, 0x12, 0x27,
  25986. 0x4e
  25987. };
  25988. WOLFSSL_SMALL_STACK_STATIC const byte pKeyCtx[] = {
  25989. 0x43, 0xba, 0x28, 0xf4, 0x30, 0xcd, 0xff, 0x45,
  25990. 0x6a, 0xe5, 0x31, 0x54, 0x5f, 0x7e, 0xcd, 0x0a,
  25991. 0xc8, 0x34, 0xa5, 0x5d, 0x93, 0x58, 0xc0, 0x37,
  25992. 0x2b, 0xfa, 0x0c, 0x6c, 0x67, 0x98, 0xc0, 0x86,
  25993. 0x6a, 0xea, 0x01, 0xeb, 0x00, 0x74, 0x28, 0x02,
  25994. 0xb8, 0x43, 0x8e, 0xa4, 0xcb, 0x82, 0x16, 0x9c,
  25995. 0x23, 0x51, 0x60, 0x62, 0x7b, 0x4c, 0x3a, 0x94,
  25996. 0x80
  25997. };
  25998. WOLFSSL_SMALL_STACK_STATIC const byte sigCtx[] = {
  25999. 0xd4, 0xf8, 0xf6, 0x13, 0x17, 0x70, 0xdd, 0x46,
  26000. 0xf4, 0x08, 0x67, 0xd6, 0xfd, 0x5d, 0x50, 0x55,
  26001. 0xde, 0x43, 0x54, 0x1f, 0x8c, 0x5e, 0x35, 0xab,
  26002. 0xbc, 0xd0, 0x01, 0xb3, 0x2a, 0x89, 0xf7, 0xd2,
  26003. 0x15, 0x1f, 0x76, 0x47, 0xf1, 0x1d, 0x8c, 0xa2,
  26004. 0xae, 0x27, 0x9f, 0xb8, 0x42, 0xd6, 0x07, 0x21,
  26005. 0x7f, 0xce, 0x6e, 0x04, 0x2f, 0x68, 0x15, 0xea,
  26006. 0x00, 0x0c, 0x85, 0x74, 0x1d, 0xe5, 0xc8, 0xda,
  26007. 0x11, 0x44, 0xa6, 0xa1, 0xab, 0xa7, 0xf9, 0x6d,
  26008. 0xe4, 0x25, 0x05, 0xd7, 0xa7, 0x29, 0x85, 0x24,
  26009. 0xfd, 0xa5, 0x38, 0xfc, 0xcb, 0xbb, 0x75, 0x4f,
  26010. 0x57, 0x8c, 0x1c, 0xad, 0x10, 0xd5, 0x4d, 0x0d,
  26011. 0x54, 0x28, 0x40, 0x7e, 0x85, 0xdc, 0xbc, 0x98,
  26012. 0xa4, 0x91, 0x55, 0xc1, 0x37, 0x64, 0xe6, 0x6c,
  26013. 0x3c, 0x00
  26014. };
  26015. WOLFSSL_SMALL_STACK_STATIC const byte msgCtx[] = {
  26016. 0x03
  26017. };
  26018. WOLFSSL_SMALL_STACK_STATIC const byte contextCtx[] = {
  26019. 0x66,0x6f,0x6f
  26020. };
  26021. outlen = sizeof(out);
  26022. XMEMSET(out, 0, sizeof(out));
  26023. if (wc_ed448_init_ex(&key, HEAP_HINT, devId) != 0)
  26024. return -11500;
  26025. if (wc_ed448_import_private_key(sKeyCtx, ED448_KEY_SIZE, pKeyCtx,
  26026. sizeof(pKeyCtx), &key) != 0)
  26027. return -11501;
  26028. if (wc_ed448_sign_msg(msgCtx, sizeof(msgCtx), out, &outlen, &key,
  26029. contextCtx, sizeof(contextCtx)) != 0)
  26030. return -11502;
  26031. if (XMEMCMP(out, sigCtx, sizeof(sigCtx)))
  26032. return -11503;
  26033. #if defined(HAVE_ED448_VERIFY)
  26034. /* test verify on good msg */
  26035. if (wc_ed448_verify_msg(out, outlen, msgCtx, sizeof(msgCtx), &verify, &key,
  26036. contextCtx, sizeof(contextCtx)) != 0 || verify != 1)
  26037. return -11504;
  26038. #endif
  26039. wc_ed448_free(&key);
  26040. return 0;
  26041. }
  26042. static int ed448ph_test(void)
  26043. {
  26044. byte out[ED448_SIG_SIZE];
  26045. word32 outlen;
  26046. #ifdef HAVE_ED448_VERIFY
  26047. int verify;
  26048. #endif /* HAVE_ED448_VERIFY */
  26049. ed448_key key;
  26050. WOLFSSL_SMALL_STACK_STATIC const byte sKeyPh[] = {
  26051. 0x83, 0x3f, 0xe6, 0x24, 0x09, 0x23, 0x7b, 0x9d,
  26052. 0x62, 0xec, 0x77, 0x58, 0x75, 0x20, 0x91, 0x1e,
  26053. 0x9a, 0x75, 0x9c, 0xec, 0x1d, 0x19, 0x75, 0x5b,
  26054. 0x7d, 0xa9, 0x01, 0xb9, 0x6d, 0xca, 0x3d, 0x42,
  26055. 0xef, 0x78, 0x22, 0xe0, 0xd5, 0x10, 0x41, 0x27,
  26056. 0xdc, 0x05, 0xd6, 0xdb, 0xef, 0xde, 0x69, 0xe3,
  26057. 0xab, 0x2c, 0xec, 0x7c, 0x86, 0x7c, 0x6e, 0x2c,
  26058. 0x49
  26059. };
  26060. WOLFSSL_SMALL_STACK_STATIC const byte pKeyPh[] = {
  26061. 0x25, 0x9b, 0x71, 0xc1, 0x9f, 0x83, 0xef, 0x77,
  26062. 0xa7, 0xab, 0xd2, 0x65, 0x24, 0xcb, 0xdb, 0x31,
  26063. 0x61, 0xb5, 0x90, 0xa4, 0x8f, 0x7d, 0x17, 0xde,
  26064. 0x3e, 0xe0, 0xba, 0x9c, 0x52, 0xbe, 0xb7, 0x43,
  26065. 0xc0, 0x94, 0x28, 0xa1, 0x31, 0xd6, 0xb1, 0xb5,
  26066. 0x73, 0x03, 0xd9, 0x0d, 0x81, 0x32, 0xc2, 0x76,
  26067. 0xd5, 0xed, 0x3d, 0x5d, 0x01, 0xc0, 0xf5, 0x38,
  26068. 0x80
  26069. };
  26070. WOLFSSL_SMALL_STACK_STATIC const byte sigPh1[] = {
  26071. 0x82, 0x2f, 0x69, 0x01, 0xf7, 0x48, 0x0f, 0x3d,
  26072. 0x5f, 0x56, 0x2c, 0x59, 0x29, 0x94, 0xd9, 0x69,
  26073. 0x36, 0x02, 0x87, 0x56, 0x14, 0x48, 0x32, 0x56,
  26074. 0x50, 0x56, 0x00, 0xbb, 0xc2, 0x81, 0xae, 0x38,
  26075. 0x1f, 0x54, 0xd6, 0xbc, 0xe2, 0xea, 0x91, 0x15,
  26076. 0x74, 0x93, 0x2f, 0x52, 0xa4, 0xe6, 0xca, 0xdd,
  26077. 0x78, 0x76, 0x93, 0x75, 0xec, 0x3f, 0xfd, 0x1b,
  26078. 0x80, 0x1a, 0x0d, 0x9b, 0x3f, 0x40, 0x30, 0xcd,
  26079. 0x43, 0x39, 0x64, 0xb6, 0x45, 0x7e, 0xa3, 0x94,
  26080. 0x76, 0x51, 0x12, 0x14, 0xf9, 0x74, 0x69, 0xb5,
  26081. 0x7d, 0xd3, 0x2d, 0xbc, 0x56, 0x0a, 0x9a, 0x94,
  26082. 0xd0, 0x0b, 0xff, 0x07, 0x62, 0x04, 0x64, 0xa3,
  26083. 0xad, 0x20, 0x3d, 0xf7, 0xdc, 0x7c, 0xe3, 0x60,
  26084. 0xc3, 0xcd, 0x36, 0x96, 0xd9, 0xd9, 0xfa, 0xb9,
  26085. 0x0f, 0x00
  26086. };
  26087. WOLFSSL_SMALL_STACK_STATIC const byte sigPh2[] = {
  26088. 0xc3, 0x22, 0x99, 0xd4, 0x6e, 0xc8, 0xff, 0x02,
  26089. 0xb5, 0x45, 0x40, 0x98, 0x28, 0x14, 0xdc, 0xe9,
  26090. 0xa0, 0x58, 0x12, 0xf8, 0x19, 0x62, 0xb6, 0x49,
  26091. 0xd5, 0x28, 0x09, 0x59, 0x16, 0xa2, 0xaa, 0x48,
  26092. 0x10, 0x65, 0xb1, 0x58, 0x04, 0x23, 0xef, 0x92,
  26093. 0x7e, 0xcf, 0x0a, 0xf5, 0x88, 0x8f, 0x90, 0xda,
  26094. 0x0f, 0x6a, 0x9a, 0x85, 0xad, 0x5d, 0xc3, 0xf2,
  26095. 0x80, 0xd9, 0x12, 0x24, 0xba, 0x99, 0x11, 0xa3,
  26096. 0x65, 0x3d, 0x00, 0xe4, 0x84, 0xe2, 0xce, 0x23,
  26097. 0x25, 0x21, 0x48, 0x1c, 0x86, 0x58, 0xdf, 0x30,
  26098. 0x4b, 0xb7, 0x74, 0x5a, 0x73, 0x51, 0x4c, 0xdb,
  26099. 0x9b, 0xf3, 0xe1, 0x57, 0x84, 0xab, 0x71, 0x28,
  26100. 0x4f, 0x8d, 0x07, 0x04, 0xa6, 0x08, 0xc5, 0x4a,
  26101. 0x6b, 0x62, 0xd9, 0x7b, 0xeb, 0x51, 0x1d, 0x13,
  26102. 0x21, 0x00
  26103. };
  26104. WOLFSSL_SMALL_STACK_STATIC const byte msgPh[] = {
  26105. 0x61,0x62,0x63
  26106. };
  26107. /* SHA-512 hash of msgPh */
  26108. WOLFSSL_SMALL_STACK_STATIC const byte hashPh[] = {
  26109. 0x48, 0x33, 0x66, 0x60, 0x13, 0x60, 0xa8, 0x77,
  26110. 0x1c, 0x68, 0x63, 0x08, 0x0c, 0xc4, 0x11, 0x4d,
  26111. 0x8d, 0xb4, 0x45, 0x30, 0xf8, 0xf1, 0xe1, 0xee,
  26112. 0x4f, 0x94, 0xea, 0x37, 0xe7, 0x8b, 0x57, 0x39,
  26113. 0xd5, 0xa1, 0x5b, 0xef, 0x18, 0x6a, 0x53, 0x86,
  26114. 0xc7, 0x57, 0x44, 0xc0, 0x52, 0x7e, 0x1f, 0xaa,
  26115. 0x9f, 0x87, 0x26, 0xe4, 0x62, 0xa1, 0x2a, 0x4f,
  26116. 0xeb, 0x06, 0xbd, 0x88, 0x01, 0xe7, 0x51, 0xe4
  26117. };
  26118. WOLFSSL_SMALL_STACK_STATIC const byte contextPh2[] = {
  26119. 0x66,0x6f,0x6f
  26120. };
  26121. outlen = sizeof(out);
  26122. XMEMSET(out, 0, sizeof(out));
  26123. if (wc_ed448_init_ex(&key, HEAP_HINT, devId) != 0)
  26124. return -11600;
  26125. if (wc_ed448_import_private_key(sKeyPh, ED448_KEY_SIZE, pKeyPh,
  26126. sizeof(pKeyPh), &key) != 0) {
  26127. return -11601;
  26128. }
  26129. if (wc_ed448ph_sign_msg(msgPh, sizeof(msgPh), out, &outlen, &key, NULL,
  26130. 0) != 0) {
  26131. return -11602;
  26132. }
  26133. if (XMEMCMP(out, sigPh1, sizeof(sigPh1)))
  26134. return -11603;
  26135. #if defined(HAVE_ED448_VERIFY)
  26136. /* test verify on good msg */
  26137. if (wc_ed448ph_verify_msg(out, outlen, msgPh, sizeof(msgPh), &verify, &key,
  26138. NULL, 0) != 0 || verify != 1) {
  26139. return -11604;
  26140. }
  26141. #endif
  26142. if (wc_ed448ph_sign_msg(msgPh, sizeof(msgPh), out, &outlen, &key,
  26143. contextPh2, sizeof(contextPh2)) != 0) {
  26144. return -11605;
  26145. }
  26146. if (XMEMCMP(out, sigPh2, sizeof(sigPh2)))
  26147. return -11606;
  26148. #if defined(HAVE_ED448_VERIFY)
  26149. /* test verify on good msg */
  26150. if (wc_ed448ph_verify_msg(out, outlen, msgPh, sizeof(msgPh), &verify, &key,
  26151. contextPh2, sizeof(contextPh2)) != 0 ||
  26152. verify != 1) {
  26153. return -11607;
  26154. }
  26155. #endif
  26156. if (wc_ed448ph_sign_hash(hashPh, sizeof(hashPh), out, &outlen, &key, NULL,
  26157. 0) != 0) {
  26158. return -11608;
  26159. }
  26160. if (XMEMCMP(out, sigPh1, sizeof(sigPh1)))
  26161. return -11609;
  26162. #if defined(HAVE_ED448_VERIFY)
  26163. if (wc_ed448ph_verify_hash(out, outlen, hashPh, sizeof(hashPh), &verify,
  26164. &key, NULL, 0) != 0 || verify != 1) {
  26165. return -11610;
  26166. }
  26167. #endif
  26168. if (wc_ed448ph_sign_hash(hashPh, sizeof(hashPh), out, &outlen, &key,
  26169. contextPh2, sizeof(contextPh2)) != 0) {
  26170. return -11611;
  26171. }
  26172. if (XMEMCMP(out, sigPh2, sizeof(sigPh2)))
  26173. return -11612;
  26174. #if defined(HAVE_ED448_VERIFY)
  26175. if (wc_ed448ph_verify_hash(out, outlen, hashPh, sizeof(hashPh), &verify,
  26176. &key, contextPh2, sizeof(contextPh2)) != 0 ||
  26177. verify != 1) {
  26178. return -11613;
  26179. }
  26180. #endif
  26181. wc_ed448_free(&key);
  26182. return 0;
  26183. }
  26184. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_EXPORT && HAVE_ED448_KEY_IMPORT */
  26185. WOLFSSL_TEST_SUBROUTINE int ed448_test(void)
  26186. {
  26187. int ret;
  26188. WC_RNG rng;
  26189. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_EXPORT) &&\
  26190. defined(HAVE_ED448_KEY_IMPORT)
  26191. byte out[ED448_SIG_SIZE];
  26192. int i;
  26193. word32 outlen;
  26194. #ifdef HAVE_ED448_VERIFY
  26195. #ifdef WOLFSSL_ED448_STREAMING_VERIFY
  26196. int j;
  26197. #endif /* WOLFSSL_ED448_STREAMING_VERIFY */
  26198. int verify;
  26199. #endif /* HAVE_ED448_VERIFY */
  26200. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_EXPORT && HAVE_ED448_KEY_IMPORT */
  26201. word32 keySz, sigSz;
  26202. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  26203. ed448_key *key = NULL;
  26204. ed448_key *key2 = NULL;
  26205. #else
  26206. ed448_key key[1];
  26207. ed448_key key2[1];
  26208. #endif
  26209. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_EXPORT) && \
  26210. defined(HAVE_ED448_KEY_IMPORT)
  26211. /* test vectors from
  26212. https://tools.ietf.org/html/rfc8032
  26213. */
  26214. WOLFSSL_SMALL_STACK_STATIC const byte sKey1[] = {
  26215. 0x6c, 0x82, 0xa5, 0x62, 0xcb, 0x80, 0x8d, 0x10,
  26216. 0xd6, 0x32, 0xbe, 0x89, 0xc8, 0x51, 0x3e, 0xbf,
  26217. 0x6c, 0x92, 0x9f, 0x34, 0xdd, 0xfa, 0x8c, 0x9f,
  26218. 0x63, 0xc9, 0x96, 0x0e, 0xf6, 0xe3, 0x48, 0xa3,
  26219. 0x52, 0x8c, 0x8a, 0x3f, 0xcc, 0x2f, 0x04, 0x4e,
  26220. 0x39, 0xa3, 0xfc, 0x5b, 0x94, 0x49, 0x2f, 0x8f,
  26221. 0x03, 0x2e, 0x75, 0x49, 0xa2, 0x00, 0x98, 0xf9,
  26222. 0x5b
  26223. };
  26224. WOLFSSL_SMALL_STACK_STATIC const byte sKey2[] = {
  26225. 0xc4, 0xea, 0xb0, 0x5d, 0x35, 0x70, 0x07, 0xc6,
  26226. 0x32, 0xf3, 0xdb, 0xb4, 0x84, 0x89, 0x92, 0x4d,
  26227. 0x55, 0x2b, 0x08, 0xfe, 0x0c, 0x35, 0x3a, 0x0d,
  26228. 0x4a, 0x1f, 0x00, 0xac, 0xda, 0x2c, 0x46, 0x3a,
  26229. 0xfb, 0xea, 0x67, 0xc5, 0xe8, 0xd2, 0x87, 0x7c,
  26230. 0x5e, 0x3b, 0xc3, 0x97, 0xa6, 0x59, 0x94, 0x9e,
  26231. 0xf8, 0x02, 0x1e, 0x95, 0x4e, 0x0a, 0x12, 0x27,
  26232. 0x4e
  26233. };
  26234. WOLFSSL_SMALL_STACK_STATIC const byte sKey3[] = {
  26235. 0x25, 0x8c, 0xdd, 0x4a, 0xda, 0x32, 0xed, 0x9c,
  26236. 0x9f, 0xf5, 0x4e, 0x63, 0x75, 0x6a, 0xe5, 0x82,
  26237. 0xfb, 0x8f, 0xab, 0x2a, 0xc7, 0x21, 0xf2, 0xc8,
  26238. 0xe6, 0x76, 0xa7, 0x27, 0x68, 0x51, 0x3d, 0x93,
  26239. 0x9f, 0x63, 0xdd, 0xdb, 0x55, 0x60, 0x91, 0x33,
  26240. 0xf2, 0x9a, 0xdf, 0x86, 0xec, 0x99, 0x29, 0xdc,
  26241. 0xcb, 0x52, 0xc1, 0xc5, 0xfd, 0x2f, 0xf7, 0xe2,
  26242. 0x1b
  26243. };
  26244. /* uncompressed test */
  26245. WOLFSSL_SMALL_STACK_STATIC const byte sKey4[] = {
  26246. 0x6c, 0x82, 0xa5, 0x62, 0xcb, 0x80, 0x8d, 0x10,
  26247. 0xd6, 0x32, 0xbe, 0x89, 0xc8, 0x51, 0x3e, 0xbf,
  26248. 0x6c, 0x92, 0x9f, 0x34, 0xdd, 0xfa, 0x8c, 0x9f,
  26249. 0x63, 0xc9, 0x96, 0x0e, 0xf6, 0xe3, 0x48, 0xa3,
  26250. 0x52, 0x8c, 0x8a, 0x3f, 0xcc, 0x2f, 0x04, 0x4e,
  26251. 0x39, 0xa3, 0xfc, 0x5b, 0x94, 0x49, 0x2f, 0x8f,
  26252. 0x03, 0x2e, 0x75, 0x49, 0xa2, 0x00, 0x98, 0xf9,
  26253. 0x5b
  26254. };
  26255. /* compressed prefix test */
  26256. WOLFSSL_SMALL_STACK_STATIC const byte sKey5[] = {
  26257. 0x6c, 0x82, 0xa5, 0x62, 0xcb, 0x80, 0x8d, 0x10,
  26258. 0xd6, 0x32, 0xbe, 0x89, 0xc8, 0x51, 0x3e, 0xbf,
  26259. 0x6c, 0x92, 0x9f, 0x34, 0xdd, 0xfa, 0x8c, 0x9f,
  26260. 0x63, 0xc9, 0x96, 0x0e, 0xf6, 0xe3, 0x48, 0xa3,
  26261. 0x52, 0x8c, 0x8a, 0x3f, 0xcc, 0x2f, 0x04, 0x4e,
  26262. 0x39, 0xa3, 0xfc, 0x5b, 0x94, 0x49, 0x2f, 0x8f,
  26263. 0x03, 0x2e, 0x75, 0x49, 0xa2, 0x00, 0x98, 0xf9,
  26264. 0x5b
  26265. };
  26266. WOLFSSL_SMALL_STACK_STATIC const byte sKey6[] = {
  26267. 0x87, 0x2d, 0x09, 0x37, 0x80, 0xf5, 0xd3, 0x73,
  26268. 0x0d, 0xf7, 0xc2, 0x12, 0x66, 0x4b, 0x37, 0xb8,
  26269. 0xa0, 0xf2, 0x4f, 0x56, 0x81, 0x0d, 0xaa, 0x83,
  26270. 0x82, 0xcd, 0x4f, 0xa3, 0xf7, 0x76, 0x34, 0xec,
  26271. 0x44, 0xdc, 0x54, 0xf1, 0xc2, 0xed, 0x9b, 0xea,
  26272. 0x86, 0xfa, 0xfb, 0x76, 0x32, 0xd8, 0xbe, 0x19,
  26273. 0x9e, 0xa1, 0x65, 0xf5, 0xad, 0x55, 0xdd, 0x9c,
  26274. 0xe8
  26275. };
  26276. WOLFSSL_SMALL_STACK_STATIC const byte* sKeys[] = {sKey1, sKey2, sKey3, sKey4, sKey5, sKey6};
  26277. WOLFSSL_SMALL_STACK_STATIC const byte pKey1[] = {
  26278. 0x5f, 0xd7, 0x44, 0x9b, 0x59, 0xb4, 0x61, 0xfd,
  26279. 0x2c, 0xe7, 0x87, 0xec, 0x61, 0x6a, 0xd4, 0x6a,
  26280. 0x1d, 0xa1, 0x34, 0x24, 0x85, 0xa7, 0x0e, 0x1f,
  26281. 0x8a, 0x0e, 0xa7, 0x5d, 0x80, 0xe9, 0x67, 0x78,
  26282. 0xed, 0xf1, 0x24, 0x76, 0x9b, 0x46, 0xc7, 0x06,
  26283. 0x1b, 0xd6, 0x78, 0x3d, 0xf1, 0xe5, 0x0f, 0x6c,
  26284. 0xd1, 0xfa, 0x1a, 0xbe, 0xaf, 0xe8, 0x25, 0x61,
  26285. 0x80
  26286. };
  26287. WOLFSSL_SMALL_STACK_STATIC const byte pKey2[] = {
  26288. 0x43, 0xba, 0x28, 0xf4, 0x30, 0xcd, 0xff, 0x45,
  26289. 0x6a, 0xe5, 0x31, 0x54, 0x5f, 0x7e, 0xcd, 0x0a,
  26290. 0xc8, 0x34, 0xa5, 0x5d, 0x93, 0x58, 0xc0, 0x37,
  26291. 0x2b, 0xfa, 0x0c, 0x6c, 0x67, 0x98, 0xc0, 0x86,
  26292. 0x6a, 0xea, 0x01, 0xeb, 0x00, 0x74, 0x28, 0x02,
  26293. 0xb8, 0x43, 0x8e, 0xa4, 0xcb, 0x82, 0x16, 0x9c,
  26294. 0x23, 0x51, 0x60, 0x62, 0x7b, 0x4c, 0x3a, 0x94,
  26295. 0x80
  26296. };
  26297. WOLFSSL_SMALL_STACK_STATIC const byte pKey3[] = {
  26298. 0x3b, 0xa1, 0x6d, 0xa0, 0xc6, 0xf2, 0xcc, 0x1f,
  26299. 0x30, 0x18, 0x77, 0x40, 0x75, 0x6f, 0x5e, 0x79,
  26300. 0x8d, 0x6b, 0xc5, 0xfc, 0x01, 0x5d, 0x7c, 0x63,
  26301. 0xcc, 0x95, 0x10, 0xee, 0x3f, 0xd4, 0x4a, 0xdc,
  26302. 0x24, 0xd8, 0xe9, 0x68, 0xb6, 0xe4, 0x6e, 0x6f,
  26303. 0x94, 0xd1, 0x9b, 0x94, 0x53, 0x61, 0x72, 0x6b,
  26304. 0xd7, 0x5e, 0x14, 0x9e, 0xf0, 0x98, 0x17, 0xf5,
  26305. 0x80
  26306. };
  26307. /* uncompressed test */
  26308. WOLFSSL_SMALL_STACK_STATIC const byte pKey4[] = {
  26309. 0x5f, 0xd7, 0x44, 0x9b, 0x59, 0xb4, 0x61, 0xfd,
  26310. 0x2c, 0xe7, 0x87, 0xec, 0x61, 0x6a, 0xd4, 0x6a,
  26311. 0x1d, 0xa1, 0x34, 0x24, 0x85, 0xa7, 0x0e, 0x1f,
  26312. 0x8a, 0x0e, 0xa7, 0x5d, 0x80, 0xe9, 0x67, 0x78,
  26313. 0xed, 0xf1, 0x24, 0x76, 0x9b, 0x46, 0xc7, 0x06,
  26314. 0x1b, 0xd6, 0x78, 0x3d, 0xf1, 0xe5, 0x0f, 0x6c,
  26315. 0xd1, 0xfa, 0x1a, 0xbe, 0xaf, 0xe8, 0x25, 0x61,
  26316. 0x80
  26317. };
  26318. /* compressed prefix */
  26319. WOLFSSL_SMALL_STACK_STATIC const byte pKey5[] = {
  26320. 0x5f, 0xd7, 0x44, 0x9b, 0x59, 0xb4, 0x61, 0xfd,
  26321. 0x2c, 0xe7, 0x87, 0xec, 0x61, 0x6a, 0xd4, 0x6a,
  26322. 0x1d, 0xa1, 0x34, 0x24, 0x85, 0xa7, 0x0e, 0x1f,
  26323. 0x8a, 0x0e, 0xa7, 0x5d, 0x80, 0xe9, 0x67, 0x78,
  26324. 0xed, 0xf1, 0x24, 0x76, 0x9b, 0x46, 0xc7, 0x06,
  26325. 0x1b, 0xd6, 0x78, 0x3d, 0xf1, 0xe5, 0x0f, 0x6c,
  26326. 0xd1, 0xfa, 0x1a, 0xbe, 0xaf, 0xe8, 0x25, 0x61,
  26327. 0x80
  26328. };
  26329. WOLFSSL_SMALL_STACK_STATIC const byte pKey6[] = {
  26330. 0xa8, 0x1b, 0x2e, 0x8a, 0x70, 0xa5, 0xac, 0x94,
  26331. 0xff, 0xdb, 0xcc, 0x9b, 0xad, 0xfc, 0x3f, 0xeb,
  26332. 0x08, 0x01, 0xf2, 0x58, 0x57, 0x8b, 0xb1, 0x14,
  26333. 0xad, 0x44, 0xec, 0xe1, 0xec, 0x0e, 0x79, 0x9d,
  26334. 0xa0, 0x8e, 0xff, 0xb8, 0x1c, 0x5d, 0x68, 0x5c,
  26335. 0x0c, 0x56, 0xf6, 0x4e, 0xec, 0xae, 0xf8, 0xcd,
  26336. 0xf1, 0x1c, 0xc3, 0x87, 0x37, 0x83, 0x8c, 0xf4,
  26337. 0x00
  26338. };
  26339. WOLFSSL_SMALL_STACK_STATIC const byte* pKeys[] = {pKey1, pKey2, pKey3, pKey4, pKey5, pKey6};
  26340. WOLFSSL_SMALL_STACK_STATIC const byte pKeySz[] = {sizeof(pKey1), sizeof(pKey2), sizeof(pKey3),
  26341. sizeof(pKey4), sizeof(pKey5), sizeof(pKey6)};
  26342. WOLFSSL_SMALL_STACK_STATIC const byte sig1[] = {
  26343. 0x53, 0x3a, 0x37, 0xf6, 0xbb, 0xe4, 0x57, 0x25,
  26344. 0x1f, 0x02, 0x3c, 0x0d, 0x88, 0xf9, 0x76, 0xae,
  26345. 0x2d, 0xfb, 0x50, 0x4a, 0x84, 0x3e, 0x34, 0xd2,
  26346. 0x07, 0x4f, 0xd8, 0x23, 0xd4, 0x1a, 0x59, 0x1f,
  26347. 0x2b, 0x23, 0x3f, 0x03, 0x4f, 0x62, 0x82, 0x81,
  26348. 0xf2, 0xfd, 0x7a, 0x22, 0xdd, 0xd4, 0x7d, 0x78,
  26349. 0x28, 0xc5, 0x9b, 0xd0, 0xa2, 0x1b, 0xfd, 0x39,
  26350. 0x80, 0xff, 0x0d, 0x20, 0x28, 0xd4, 0xb1, 0x8a,
  26351. 0x9d, 0xf6, 0x3e, 0x00, 0x6c, 0x5d, 0x1c, 0x2d,
  26352. 0x34, 0x5b, 0x92, 0x5d, 0x8d, 0xc0, 0x0b, 0x41,
  26353. 0x04, 0x85, 0x2d, 0xb9, 0x9a, 0xc5, 0xc7, 0xcd,
  26354. 0xda, 0x85, 0x30, 0xa1, 0x13, 0xa0, 0xf4, 0xdb,
  26355. 0xb6, 0x11, 0x49, 0xf0, 0x5a, 0x73, 0x63, 0x26,
  26356. 0x8c, 0x71, 0xd9, 0x58, 0x08, 0xff, 0x2e, 0x65,
  26357. 0x26, 0x00
  26358. };
  26359. WOLFSSL_SMALL_STACK_STATIC const byte sig2[] = {
  26360. 0x26, 0xb8, 0xf9, 0x17, 0x27, 0xbd, 0x62, 0x89,
  26361. 0x7a, 0xf1, 0x5e, 0x41, 0xeb, 0x43, 0xc3, 0x77,
  26362. 0xef, 0xb9, 0xc6, 0x10, 0xd4, 0x8f, 0x23, 0x35,
  26363. 0xcb, 0x0b, 0xd0, 0x08, 0x78, 0x10, 0xf4, 0x35,
  26364. 0x25, 0x41, 0xb1, 0x43, 0xc4, 0xb9, 0x81, 0xb7,
  26365. 0xe1, 0x8f, 0x62, 0xde, 0x8c, 0xcd, 0xf6, 0x33,
  26366. 0xfc, 0x1b, 0xf0, 0x37, 0xab, 0x7c, 0xd7, 0x79,
  26367. 0x80, 0x5e, 0x0d, 0xbc, 0xc0, 0xaa, 0xe1, 0xcb,
  26368. 0xce, 0xe1, 0xaf, 0xb2, 0xe0, 0x27, 0xdf, 0x36,
  26369. 0xbc, 0x04, 0xdc, 0xec, 0xbf, 0x15, 0x43, 0x36,
  26370. 0xc1, 0x9f, 0x0a, 0xf7, 0xe0, 0xa6, 0x47, 0x29,
  26371. 0x05, 0xe7, 0x99, 0xf1, 0x95, 0x3d, 0x2a, 0x0f,
  26372. 0xf3, 0x34, 0x8a, 0xb2, 0x1a, 0xa4, 0xad, 0xaf,
  26373. 0xd1, 0xd2, 0x34, 0x44, 0x1c, 0xf8, 0x07, 0xc0,
  26374. 0x3a, 0x00
  26375. };
  26376. WOLFSSL_SMALL_STACK_STATIC const byte sig3[] = {
  26377. 0x7e, 0xee, 0xab, 0x7c, 0x4e, 0x50, 0xfb, 0x79,
  26378. 0x9b, 0x41, 0x8e, 0xe5, 0xe3, 0x19, 0x7f, 0xf6,
  26379. 0xbf, 0x15, 0xd4, 0x3a, 0x14, 0xc3, 0x43, 0x89,
  26380. 0xb5, 0x9d, 0xd1, 0xa7, 0xb1, 0xb8, 0x5b, 0x4a,
  26381. 0xe9, 0x04, 0x38, 0xac, 0xa6, 0x34, 0xbe, 0xa4,
  26382. 0x5e, 0x3a, 0x26, 0x95, 0xf1, 0x27, 0x0f, 0x07,
  26383. 0xfd, 0xcd, 0xf7, 0xc6, 0x2b, 0x8e, 0xfe, 0xaf,
  26384. 0x00, 0xb4, 0x5c, 0x2c, 0x96, 0xba, 0x45, 0x7e,
  26385. 0xb1, 0xa8, 0xbf, 0x07, 0x5a, 0x3d, 0xb2, 0x8e,
  26386. 0x5c, 0x24, 0xf6, 0xb9, 0x23, 0xed, 0x4a, 0xd7,
  26387. 0x47, 0xc3, 0xc9, 0xe0, 0x3c, 0x70, 0x79, 0xef,
  26388. 0xb8, 0x7c, 0xb1, 0x10, 0xd3, 0xa9, 0x98, 0x61,
  26389. 0xe7, 0x20, 0x03, 0xcb, 0xae, 0x6d, 0x6b, 0x8b,
  26390. 0x82, 0x7e, 0x4e, 0x6c, 0x14, 0x30, 0x64, 0xff,
  26391. 0x3c, 0x00
  26392. };
  26393. /* uncompressed test */
  26394. WOLFSSL_SMALL_STACK_STATIC const byte sig4[] = {
  26395. 0x53, 0x3a, 0x37, 0xf6, 0xbb, 0xe4, 0x57, 0x25,
  26396. 0x1f, 0x02, 0x3c, 0x0d, 0x88, 0xf9, 0x76, 0xae,
  26397. 0x2d, 0xfb, 0x50, 0x4a, 0x84, 0x3e, 0x34, 0xd2,
  26398. 0x07, 0x4f, 0xd8, 0x23, 0xd4, 0x1a, 0x59, 0x1f,
  26399. 0x2b, 0x23, 0x3f, 0x03, 0x4f, 0x62, 0x82, 0x81,
  26400. 0xf2, 0xfd, 0x7a, 0x22, 0xdd, 0xd4, 0x7d, 0x78,
  26401. 0x28, 0xc5, 0x9b, 0xd0, 0xa2, 0x1b, 0xfd, 0x39,
  26402. 0x80, 0xff, 0x0d, 0x20, 0x28, 0xd4, 0xb1, 0x8a,
  26403. 0x9d, 0xf6, 0x3e, 0x00, 0x6c, 0x5d, 0x1c, 0x2d,
  26404. 0x34, 0x5b, 0x92, 0x5d, 0x8d, 0xc0, 0x0b, 0x41,
  26405. 0x04, 0x85, 0x2d, 0xb9, 0x9a, 0xc5, 0xc7, 0xcd,
  26406. 0xda, 0x85, 0x30, 0xa1, 0x13, 0xa0, 0xf4, 0xdb,
  26407. 0xb6, 0x11, 0x49, 0xf0, 0x5a, 0x73, 0x63, 0x26,
  26408. 0x8c, 0x71, 0xd9, 0x58, 0x08, 0xff, 0x2e, 0x65,
  26409. 0x26, 0x00
  26410. };
  26411. /* compressed prefix */
  26412. WOLFSSL_SMALL_STACK_STATIC const byte sig5[] = {
  26413. 0x53, 0x3a, 0x37, 0xf6, 0xbb, 0xe4, 0x57, 0x25,
  26414. 0x1f, 0x02, 0x3c, 0x0d, 0x88, 0xf9, 0x76, 0xae,
  26415. 0x2d, 0xfb, 0x50, 0x4a, 0x84, 0x3e, 0x34, 0xd2,
  26416. 0x07, 0x4f, 0xd8, 0x23, 0xd4, 0x1a, 0x59, 0x1f,
  26417. 0x2b, 0x23, 0x3f, 0x03, 0x4f, 0x62, 0x82, 0x81,
  26418. 0xf2, 0xfd, 0x7a, 0x22, 0xdd, 0xd4, 0x7d, 0x78,
  26419. 0x28, 0xc5, 0x9b, 0xd0, 0xa2, 0x1b, 0xfd, 0x39,
  26420. 0x80, 0xff, 0x0d, 0x20, 0x28, 0xd4, 0xb1, 0x8a,
  26421. 0x9d, 0xf6, 0x3e, 0x00, 0x6c, 0x5d, 0x1c, 0x2d,
  26422. 0x34, 0x5b, 0x92, 0x5d, 0x8d, 0xc0, 0x0b, 0x41,
  26423. 0x04, 0x85, 0x2d, 0xb9, 0x9a, 0xc5, 0xc7, 0xcd,
  26424. 0xda, 0x85, 0x30, 0xa1, 0x13, 0xa0, 0xf4, 0xdb,
  26425. 0xb6, 0x11, 0x49, 0xf0, 0x5a, 0x73, 0x63, 0x26,
  26426. 0x8c, 0x71, 0xd9, 0x58, 0x08, 0xff, 0x2e, 0x65,
  26427. 0x26, 0x00
  26428. };
  26429. WOLFSSL_SMALL_STACK_STATIC const byte sig6[] = {
  26430. 0xe3, 0x01, 0x34, 0x5a, 0x41, 0xa3, 0x9a, 0x4d,
  26431. 0x72, 0xff, 0xf8, 0xdf, 0x69, 0xc9, 0x80, 0x75,
  26432. 0xa0, 0xcc, 0x08, 0x2b, 0x80, 0x2f, 0xc9, 0xb2,
  26433. 0xb6, 0xbc, 0x50, 0x3f, 0x92, 0x6b, 0x65, 0xbd,
  26434. 0xdf, 0x7f, 0x4c, 0x8f, 0x1c, 0xb4, 0x9f, 0x63,
  26435. 0x96, 0xaf, 0xc8, 0xa7, 0x0a, 0xbe, 0x6d, 0x8a,
  26436. 0xef, 0x0d, 0xb4, 0x78, 0xd4, 0xc6, 0xb2, 0x97,
  26437. 0x00, 0x76, 0xc6, 0xa0, 0x48, 0x4f, 0xe7, 0x6d,
  26438. 0x76, 0xb3, 0xa9, 0x76, 0x25, 0xd7, 0x9f, 0x1c,
  26439. 0xe2, 0x40, 0xe7, 0xc5, 0x76, 0x75, 0x0d, 0x29,
  26440. 0x55, 0x28, 0x28, 0x6f, 0x71, 0x9b, 0x41, 0x3d,
  26441. 0xe9, 0xad, 0xa3, 0xe8, 0xeb, 0x78, 0xed, 0x57,
  26442. 0x36, 0x03, 0xce, 0x30, 0xd8, 0xbb, 0x76, 0x17,
  26443. 0x85, 0xdc, 0x30, 0xdb, 0xc3, 0x20, 0x86, 0x9e,
  26444. 0x1a, 0x00
  26445. };
  26446. WOLFSSL_SMALL_STACK_STATIC const byte* sigs[] = {sig1, sig2, sig3, sig4, sig5, sig6};
  26447. #define SIGSZ sizeof(sig1)
  26448. PEDANTIC_EXTENSION WOLFSSL_SMALL_STACK_STATIC const byte msg1[] = { };
  26449. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] = { 0x03 };
  26450. WOLFSSL_SMALL_STACK_STATIC const byte msg3[] = { 0x64, 0xa6, 0x5f, 0x3c, 0xde, 0xdc, 0xdd,
  26451. 0x66, 0x81, 0x1e, 0x29, 0x15 };
  26452. /* test of a 1023 byte long message */
  26453. WOLFSSL_SMALL_STACK_STATIC const byte msg4[] = {
  26454. 0x6d, 0xdf, 0x80, 0x2e, 0x1a, 0xae, 0x49, 0x86,
  26455. 0x93, 0x5f, 0x7f, 0x98, 0x1b, 0xa3, 0xf0, 0x35,
  26456. 0x1d, 0x62, 0x73, 0xc0, 0xa0, 0xc2, 0x2c, 0x9c,
  26457. 0x0e, 0x83, 0x39, 0x16, 0x8e, 0x67, 0x54, 0x12,
  26458. 0xa3, 0xde, 0xbf, 0xaf, 0x43, 0x5e, 0xd6, 0x51,
  26459. 0x55, 0x80, 0x07, 0xdb, 0x43, 0x84, 0xb6, 0x50,
  26460. 0xfc, 0xc0, 0x7e, 0x3b, 0x58, 0x6a, 0x27, 0xa4,
  26461. 0xf7, 0xa0, 0x0a, 0xc8, 0xa6, 0xfe, 0xc2, 0xcd,
  26462. 0x86, 0xae, 0x4b, 0xf1, 0x57, 0x0c, 0x41, 0xe6,
  26463. 0xa4, 0x0c, 0x93, 0x1d, 0xb2, 0x7b, 0x2f, 0xaa,
  26464. 0x15, 0xa8, 0xce, 0xdd, 0x52, 0xcf, 0xf7, 0x36,
  26465. 0x2c, 0x4e, 0x6e, 0x23, 0xda, 0xec, 0x0f, 0xbc,
  26466. 0x3a, 0x79, 0xb6, 0x80, 0x6e, 0x31, 0x6e, 0xfc,
  26467. 0xc7, 0xb6, 0x81, 0x19, 0xbf, 0x46, 0xbc, 0x76,
  26468. 0xa2, 0x60, 0x67, 0xa5, 0x3f, 0x29, 0x6d, 0xaf,
  26469. 0xdb, 0xdc, 0x11, 0xc7, 0x7f, 0x77, 0x77, 0xe9,
  26470. 0x72, 0x66, 0x0c, 0xf4, 0xb6, 0xa9, 0xb3, 0x69,
  26471. 0xa6, 0x66, 0x5f, 0x02, 0xe0, 0xcc, 0x9b, 0x6e,
  26472. 0xdf, 0xad, 0x13, 0x6b, 0x4f, 0xab, 0xe7, 0x23,
  26473. 0xd2, 0x81, 0x3d, 0xb3, 0x13, 0x6c, 0xfd, 0xe9,
  26474. 0xb6, 0xd0, 0x44, 0x32, 0x2f, 0xee, 0x29, 0x47,
  26475. 0x95, 0x2e, 0x03, 0x1b, 0x73, 0xab, 0x5c, 0x60,
  26476. 0x33, 0x49, 0xb3, 0x07, 0xbd, 0xc2, 0x7b, 0xc6,
  26477. 0xcb, 0x8b, 0x8b, 0xbd, 0x7b, 0xd3, 0x23, 0x21,
  26478. 0x9b, 0x80, 0x33, 0xa5, 0x81, 0xb5, 0x9e, 0xad,
  26479. 0xeb, 0xb0, 0x9b, 0x3c, 0x4f, 0x3d, 0x22, 0x77,
  26480. 0xd4, 0xf0, 0x34, 0x36, 0x24, 0xac, 0xc8, 0x17,
  26481. 0x80, 0x47, 0x28, 0xb2, 0x5a, 0xb7, 0x97, 0x17,
  26482. 0x2b, 0x4c, 0x5c, 0x21, 0xa2, 0x2f, 0x9c, 0x78,
  26483. 0x39, 0xd6, 0x43, 0x00, 0x23, 0x2e, 0xb6, 0x6e,
  26484. 0x53, 0xf3, 0x1c, 0x72, 0x3f, 0xa3, 0x7f, 0xe3,
  26485. 0x87, 0xc7, 0xd3, 0xe5, 0x0b, 0xdf, 0x98, 0x13,
  26486. 0xa3, 0x0e, 0x5b, 0xb1, 0x2c, 0xf4, 0xcd, 0x93,
  26487. 0x0c, 0x40, 0xcf, 0xb4, 0xe1, 0xfc, 0x62, 0x25,
  26488. 0x92, 0xa4, 0x95, 0x88, 0x79, 0x44, 0x94, 0xd5,
  26489. 0x6d, 0x24, 0xea, 0x4b, 0x40, 0xc8, 0x9f, 0xc0,
  26490. 0x59, 0x6c, 0xc9, 0xeb, 0xb9, 0x61, 0xc8, 0xcb,
  26491. 0x10, 0xad, 0xde, 0x97, 0x6a, 0x5d, 0x60, 0x2b,
  26492. 0x1c, 0x3f, 0x85, 0xb9, 0xb9, 0xa0, 0x01, 0xed,
  26493. 0x3c, 0x6a, 0x4d, 0x3b, 0x14, 0x37, 0xf5, 0x20,
  26494. 0x96, 0xcd, 0x19, 0x56, 0xd0, 0x42, 0xa5, 0x97,
  26495. 0xd5, 0x61, 0xa5, 0x96, 0xec, 0xd3, 0xd1, 0x73,
  26496. 0x5a, 0x8d, 0x57, 0x0e, 0xa0, 0xec, 0x27, 0x22,
  26497. 0x5a, 0x2c, 0x4a, 0xaf, 0xf2, 0x63, 0x06, 0xd1,
  26498. 0x52, 0x6c, 0x1a, 0xf3, 0xca, 0x6d, 0x9c, 0xf5,
  26499. 0xa2, 0xc9, 0x8f, 0x47, 0xe1, 0xc4, 0x6d, 0xb9,
  26500. 0xa3, 0x32, 0x34, 0xcf, 0xd4, 0xd8, 0x1f, 0x2c,
  26501. 0x98, 0x53, 0x8a, 0x09, 0xeb, 0xe7, 0x69, 0x98,
  26502. 0xd0, 0xd8, 0xfd, 0x25, 0x99, 0x7c, 0x7d, 0x25,
  26503. 0x5c, 0x6d, 0x66, 0xec, 0xe6, 0xfa, 0x56, 0xf1,
  26504. 0x11, 0x44, 0x95, 0x0f, 0x02, 0x77, 0x95, 0xe6,
  26505. 0x53, 0x00, 0x8f, 0x4b, 0xd7, 0xca, 0x2d, 0xee,
  26506. 0x85, 0xd8, 0xe9, 0x0f, 0x3d, 0xc3, 0x15, 0x13,
  26507. 0x0c, 0xe2, 0xa0, 0x03, 0x75, 0xa3, 0x18, 0xc7,
  26508. 0xc3, 0xd9, 0x7b, 0xe2, 0xc8, 0xce, 0x5b, 0x6d,
  26509. 0xb4, 0x1a, 0x62, 0x54, 0xff, 0x26, 0x4f, 0xa6,
  26510. 0x15, 0x5b, 0xae, 0xe3, 0xb0, 0x77, 0x3c, 0x0f,
  26511. 0x49, 0x7c, 0x57, 0x3f, 0x19, 0xbb, 0x4f, 0x42,
  26512. 0x40, 0x28, 0x1f, 0x0b, 0x1f, 0x4f, 0x7b, 0xe8,
  26513. 0x57, 0xa4, 0xe5, 0x9d, 0x41, 0x6c, 0x06, 0xb4,
  26514. 0xc5, 0x0f, 0xa0, 0x9e, 0x18, 0x10, 0xdd, 0xc6,
  26515. 0xb1, 0x46, 0x7b, 0xae, 0xac, 0x5a, 0x36, 0x68,
  26516. 0xd1, 0x1b, 0x6e, 0xca, 0xa9, 0x01, 0x44, 0x00,
  26517. 0x16, 0xf3, 0x89, 0xf8, 0x0a, 0xcc, 0x4d, 0xb9,
  26518. 0x77, 0x02, 0x5e, 0x7f, 0x59, 0x24, 0x38, 0x8c,
  26519. 0x7e, 0x34, 0x0a, 0x73, 0x2e, 0x55, 0x44, 0x40,
  26520. 0xe7, 0x65, 0x70, 0xf8, 0xdd, 0x71, 0xb7, 0xd6,
  26521. 0x40, 0xb3, 0x45, 0x0d, 0x1f, 0xd5, 0xf0, 0x41,
  26522. 0x0a, 0x18, 0xf9, 0xa3, 0x49, 0x4f, 0x70, 0x7c,
  26523. 0x71, 0x7b, 0x79, 0xb4, 0xbf, 0x75, 0xc9, 0x84,
  26524. 0x00, 0xb0, 0x96, 0xb2, 0x16, 0x53, 0xb5, 0xd2,
  26525. 0x17, 0xcf, 0x35, 0x65, 0xc9, 0x59, 0x74, 0x56,
  26526. 0xf7, 0x07, 0x03, 0x49, 0x7a, 0x07, 0x87, 0x63,
  26527. 0x82, 0x9b, 0xc0, 0x1b, 0xb1, 0xcb, 0xc8, 0xfa,
  26528. 0x04, 0xea, 0xdc, 0x9a, 0x6e, 0x3f, 0x66, 0x99,
  26529. 0x58, 0x7a, 0x9e, 0x75, 0xc9, 0x4e, 0x5b, 0xab,
  26530. 0x00, 0x36, 0xe0, 0xb2, 0xe7, 0x11, 0x39, 0x2c,
  26531. 0xff, 0x00, 0x47, 0xd0, 0xd6, 0xb0, 0x5b, 0xd2,
  26532. 0xa5, 0x88, 0xbc, 0x10, 0x97, 0x18, 0x95, 0x42,
  26533. 0x59, 0xf1, 0xd8, 0x66, 0x78, 0xa5, 0x79, 0xa3,
  26534. 0x12, 0x0f, 0x19, 0xcf, 0xb2, 0x96, 0x3f, 0x17,
  26535. 0x7a, 0xeb, 0x70, 0xf2, 0xd4, 0x84, 0x48, 0x26,
  26536. 0x26, 0x2e, 0x51, 0xb8, 0x02, 0x71, 0x27, 0x20,
  26537. 0x68, 0xef, 0x5b, 0x38, 0x56, 0xfa, 0x85, 0x35,
  26538. 0xaa, 0x2a, 0x88, 0xb2, 0xd4, 0x1f, 0x2a, 0x0e,
  26539. 0x2f, 0xda, 0x76, 0x24, 0xc2, 0x85, 0x02, 0x72,
  26540. 0xac, 0x4a, 0x2f, 0x56, 0x1f, 0x8f, 0x2f, 0x7a,
  26541. 0x31, 0x8b, 0xfd, 0x5c, 0xaf, 0x96, 0x96, 0x14,
  26542. 0x9e, 0x4a, 0xc8, 0x24, 0xad, 0x34, 0x60, 0x53,
  26543. 0x8f, 0xdc, 0x25, 0x42, 0x1b, 0xee, 0xc2, 0xcc,
  26544. 0x68, 0x18, 0x16, 0x2d, 0x06, 0xbb, 0xed, 0x0c,
  26545. 0x40, 0xa3, 0x87, 0x19, 0x23, 0x49, 0xdb, 0x67,
  26546. 0xa1, 0x18, 0xba, 0xda, 0x6c, 0xd5, 0xab, 0x01,
  26547. 0x40, 0xee, 0x27, 0x32, 0x04, 0xf6, 0x28, 0xaa,
  26548. 0xd1, 0xc1, 0x35, 0xf7, 0x70, 0x27, 0x9a, 0x65,
  26549. 0x1e, 0x24, 0xd8, 0xc1, 0x4d, 0x75, 0xa6, 0x05,
  26550. 0x9d, 0x76, 0xb9, 0x6a, 0x6f, 0xd8, 0x57, 0xde,
  26551. 0xf5, 0xe0, 0xb3, 0x54, 0xb2, 0x7a, 0xb9, 0x37,
  26552. 0xa5, 0x81, 0x5d, 0x16, 0xb5, 0xfa, 0xe4, 0x07,
  26553. 0xff, 0x18, 0x22, 0x2c, 0x6d, 0x1e, 0xd2, 0x63,
  26554. 0xbe, 0x68, 0xc9, 0x5f, 0x32, 0xd9, 0x08, 0xbd,
  26555. 0x89, 0x5c, 0xd7, 0x62, 0x07, 0xae, 0x72, 0x64,
  26556. 0x87, 0x56, 0x7f, 0x9a, 0x67, 0xda, 0xd7, 0x9a,
  26557. 0xbe, 0xc3, 0x16, 0xf6, 0x83, 0xb1, 0x7f, 0x2d,
  26558. 0x02, 0xbf, 0x07, 0xe0, 0xac, 0x8b, 0x5b, 0xc6,
  26559. 0x16, 0x2c, 0xf9, 0x46, 0x97, 0xb3, 0xc2, 0x7c,
  26560. 0xd1, 0xfe, 0xa4, 0x9b, 0x27, 0xf2, 0x3b, 0xa2,
  26561. 0x90, 0x18, 0x71, 0x96, 0x25, 0x06, 0x52, 0x0c,
  26562. 0x39, 0x2d, 0xa8, 0xb6, 0xad, 0x0d, 0x99, 0xf7,
  26563. 0x01, 0x3f, 0xbc, 0x06, 0xc2, 0xc1, 0x7a, 0x56,
  26564. 0x95, 0x00, 0xc8, 0xa7, 0x69, 0x64, 0x81, 0xc1,
  26565. 0xcd, 0x33, 0xe9, 0xb1, 0x4e, 0x40, 0xb8, 0x2e,
  26566. 0x79, 0xa5, 0xf5, 0xdb, 0x82, 0x57, 0x1b, 0xa9,
  26567. 0x7b, 0xae, 0x3a, 0xd3, 0xe0, 0x47, 0x95, 0x15,
  26568. 0xbb, 0x0e, 0x2b, 0x0f, 0x3b, 0xfc, 0xd1, 0xfd,
  26569. 0x33, 0x03, 0x4e, 0xfc, 0x62, 0x45, 0xed, 0xdd,
  26570. 0x7e, 0xe2, 0x08, 0x6d, 0xda, 0xe2, 0x60, 0x0d,
  26571. 0x8c, 0xa7, 0x3e, 0x21, 0x4e, 0x8c, 0x2b, 0x0b,
  26572. 0xdb, 0x2b, 0x04, 0x7c, 0x6a, 0x46, 0x4a, 0x56,
  26573. 0x2e, 0xd7, 0x7b, 0x73, 0xd2, 0xd8, 0x41, 0xc4,
  26574. 0xb3, 0x49, 0x73, 0x55, 0x12, 0x57, 0x71, 0x3b,
  26575. 0x75, 0x36, 0x32, 0xef, 0xba, 0x34, 0x81, 0x69,
  26576. 0xab, 0xc9, 0x0a, 0x68, 0xf4, 0x26, 0x11, 0xa4,
  26577. 0x01, 0x26, 0xd7, 0xcb, 0x21, 0xb5, 0x86, 0x95,
  26578. 0x56, 0x81, 0x86, 0xf7, 0xe5, 0x69, 0xd2, 0xff,
  26579. 0x0f, 0x9e, 0x74, 0x5d, 0x04, 0x87, 0xdd, 0x2e,
  26580. 0xb9, 0x97, 0xca, 0xfc, 0x5a, 0xbf, 0x9d, 0xd1,
  26581. 0x02, 0xe6, 0x2f, 0xf6, 0x6c, 0xba, 0x87
  26582. };
  26583. WOLFSSL_SMALL_STACK_STATIC const byte* msgs[] = {msg1, msg2, msg3, msg1, msg1, msg4};
  26584. WOLFSSL_SMALL_STACK_STATIC const word16 msgSz[] = {0 /*sizeof(msg1)*/,
  26585. sizeof(msg2),
  26586. sizeof(msg3),
  26587. 0 /*sizeof(msg1)*/,
  26588. 0 /*sizeof(msg1)*/,
  26589. sizeof(msg4)
  26590. };
  26591. #ifndef NO_ASN
  26592. static const byte privateEd448[] = {
  26593. 0x30, 0x47, 0x02, 0x01, 0x00, 0x30, 0x05, 0x06,
  26594. 0x03, 0x2b, 0x65, 0x71, 0x04, 0x3b, 0x04, 0x39,
  26595. 0x6c, 0x82, 0xa5, 0x62, 0xcb, 0x80, 0x8d, 0x10,
  26596. 0xd6, 0x32, 0xbe, 0x89, 0xc8, 0x51, 0x3e, 0xbf,
  26597. 0x6c, 0x92, 0x9f, 0x34, 0xdd, 0xfa, 0x8c, 0x9f,
  26598. 0x63, 0xc9, 0x96, 0x0e, 0xf6, 0xe3, 0x48, 0xa3,
  26599. 0x52, 0x8c, 0x8a, 0x3f, 0xcc, 0x2f, 0x04, 0x4e,
  26600. 0x39, 0xa3, 0xfc, 0x5b, 0x94, 0x49, 0x2f, 0x8f,
  26601. 0x03, 0x2e, 0x75, 0x49, 0xa2, 0x00, 0x98, 0xf9,
  26602. 0x5b
  26603. };
  26604. static const byte publicEd448[] = {
  26605. 0x30, 0x43, 0x30, 0x05, 0x06, 0x03, 0x2b, 0x65,
  26606. 0x71, 0x03, 0x3a, 0x00, 0x5f, 0xd7, 0x44, 0x9b,
  26607. 0x59, 0xb4, 0x61, 0xfd, 0x2c, 0xe7, 0x87, 0xec,
  26608. 0x61, 0x6a, 0xd4, 0x6a, 0x1d, 0xa1, 0x34, 0x24,
  26609. 0x85, 0xa7, 0x0e, 0x1f, 0x8a, 0x0e, 0xa7, 0x5d,
  26610. 0x80, 0xe9, 0x67, 0x78, 0xed, 0xf1, 0x24, 0x76,
  26611. 0x9b, 0x46, 0xc7, 0x06, 0x1b, 0xd6, 0x78, 0x3d,
  26612. 0xf1, 0xe5, 0x0f, 0x6c, 0xd1, 0xfa, 0x1a, 0xbe,
  26613. 0xaf, 0xe8, 0x25, 0x61, 0x80
  26614. };
  26615. static const byte privPubEd448[] = {
  26616. 0x30, 0x81, 0x82, 0x02, 0x01, 0x00, 0x30, 0x05,
  26617. 0x06, 0x03, 0x2b, 0x65, 0x71, 0x04, 0x3b, 0x04,
  26618. 0x39, 0x6c, 0x82, 0xa5, 0x62, 0xcb, 0x80, 0x8d,
  26619. 0x10, 0xd6, 0x32, 0xbe, 0x89, 0xc8, 0x51, 0x3e,
  26620. 0xbf, 0x6c, 0x92, 0x9f, 0x34, 0xdd, 0xfa, 0x8c,
  26621. 0x9f, 0x63, 0xc9, 0x96, 0x0e, 0xf6, 0xe3, 0x48,
  26622. 0xa3, 0x52, 0x8c, 0x8a, 0x3f, 0xcc, 0x2f, 0x04,
  26623. 0x4e, 0x39, 0xa3, 0xfc, 0x5b, 0x94, 0x49, 0x2f,
  26624. 0x8f, 0x03, 0x2e, 0x75, 0x49, 0xa2, 0x00, 0x98,
  26625. 0xf9, 0x5b, 0x81, 0x39, 0x5f, 0xd7, 0x44, 0x9b,
  26626. 0x59, 0xb4, 0x61, 0xfd, 0x2c, 0xe7, 0x87, 0xec,
  26627. 0x61, 0x6a, 0xd4, 0x6a, 0x1d, 0xa1, 0x34, 0x24,
  26628. 0x85, 0xa7, 0x0e, 0x1f, 0x8a, 0x0e, 0xa7, 0x5d,
  26629. 0x80, 0xe9, 0x67, 0x78, 0xed, 0xf1, 0x24, 0x76,
  26630. 0x9b, 0x46, 0xc7, 0x06, 0x1b, 0xd6, 0x78, 0x3d,
  26631. 0xf1, 0xe5, 0x0f, 0x6c, 0xd1, 0xfa, 0x1a, 0xbe,
  26632. 0xaf, 0xe8, 0x25, 0x61, 0x80
  26633. };
  26634. word32 idx;
  26635. #endif /* NO_ASN */
  26636. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_EXPORT && HAVE_ED448_KEY_IMPORT */
  26637. #if !defined(NO_ASN) && defined(HAVE_ED448_SIGN)
  26638. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  26639. ed448_key *key3 = NULL;
  26640. #else
  26641. ed448_key key3[1];
  26642. #endif
  26643. #endif
  26644. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  26645. key = (ed448_key *)XMALLOC(sizeof(*key), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26646. key2 = (ed448_key *)XMALLOC(sizeof(*key2), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26647. #if !defined(NO_ASN) && defined(HAVE_ED448_SIGN)
  26648. key3 = (ed448_key *)XMALLOC(sizeof(*key3), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26649. #endif
  26650. #endif
  26651. /* create ed448 keys */
  26652. #ifndef HAVE_FIPS
  26653. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  26654. #else
  26655. ret = wc_InitRng(&rng);
  26656. #endif
  26657. if (ret != 0) {
  26658. XMEMSET(&rng, 0, sizeof(rng));
  26659. ERROR_OUT(-11700, out);
  26660. }
  26661. if (wc_ed448_init(key) < 0)
  26662. ERROR_OUT(-11903, out);
  26663. if (wc_ed448_init(key2) < 0)
  26664. ERROR_OUT(-11904, out);
  26665. #if !defined(NO_ASN) && defined(HAVE_ED448_SIGN)
  26666. if (wc_ed448_init(key3) < 0)
  26667. ERROR_OUT(-11905, out);
  26668. #endif
  26669. if (wc_ed448_make_key(&rng, ED448_KEY_SIZE, key) < 0)
  26670. ERROR_OUT(-11906, out);
  26671. if (wc_ed448_make_key(&rng, ED448_KEY_SIZE, key2) < 0)
  26672. ERROR_OUT(-11907, out);
  26673. /* helper functions for signature and key size */
  26674. keySz = wc_ed448_size(key);
  26675. sigSz = wc_ed448_sig_size(key);
  26676. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_EXPORT) &&\
  26677. defined(HAVE_ED448_KEY_IMPORT)
  26678. for (i = 0; i < 6; i++) {
  26679. outlen = sizeof(out);
  26680. XMEMSET(out, 0, sizeof(out));
  26681. if (wc_ed448_import_private_key(sKeys[i], ED448_KEY_SIZE, pKeys[i],
  26682. pKeySz[i], key) != 0)
  26683. ERROR_OUT(-11701 - i, out);
  26684. if (wc_ed448_sign_msg(msgs[i], msgSz[i], out, &outlen, key, NULL,
  26685. 0) != 0)
  26686. ERROR_OUT(-11711 - i, out);
  26687. if (XMEMCMP(out, sigs[i], 114))
  26688. ERROR_OUT(-11721 - i, out);
  26689. #if defined(HAVE_ED448_VERIFY)
  26690. /* test verify on good msg */
  26691. if (wc_ed448_verify_msg(out, outlen, msgs[i], msgSz[i], &verify, key,
  26692. NULL, 0) != 0 || verify != 1)
  26693. ERROR_OUT(-11731 - i, out);
  26694. #ifdef WOLFSSL_ED448_STREAMING_VERIFY
  26695. /* test verify on good msg using streaming interface directly */
  26696. if (wc_ed448_verify_msg_init(out, outlen,
  26697. key, (byte)Ed448, NULL, 0) != 0)
  26698. ERROR_OUT(-11911 - i, out);
  26699. for (j = 0; j < msgSz[i]; j += i) {
  26700. if (wc_ed448_verify_msg_update(msgs[i] + j, MIN(i, msgSz[i] - j), key) != 0)
  26701. ERROR_OUT(-11921 - i, out);
  26702. }
  26703. if (wc_ed448_verify_msg_final(out, outlen, &verify,
  26704. key) != 0 || verify != 1)
  26705. ERROR_OUT(-11931 - i, out);
  26706. #endif /* WOLFSSL_ED448_STREAMING_VERIFY */
  26707. /* test verify on bad msg */
  26708. out[outlen-2] = out[outlen-2] + 1;
  26709. if (wc_ed448_verify_msg(out, outlen, msgs[i], msgSz[i], &verify, key,
  26710. NULL, 0) == 0 || verify == 1)
  26711. ERROR_OUT(-11741 - i, out);
  26712. #endif /* HAVE_ED448_VERIFY */
  26713. /* test api for import/exporting keys */
  26714. {
  26715. byte *exportPKey = NULL;
  26716. byte *exportSKey = NULL;
  26717. word32 exportPSz = ED448_KEY_SIZE;
  26718. word32 exportSSz = ED448_KEY_SIZE;
  26719. exportPKey = (byte *)XMALLOC(exportPSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26720. exportSKey = (byte *)XMALLOC(exportSSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26721. if ((exportPKey == NULL) || (exportSKey == NULL))
  26722. ERROR_OUT(-11902, out);
  26723. ret = 0;
  26724. do {
  26725. if (wc_ed448_export_public(key, exportPKey, &exportPSz) != 0) {
  26726. ret = -11751 - i;
  26727. break;
  26728. }
  26729. if (wc_ed448_import_public_ex(exportPKey, exportPSz, key2, 1) != 0) {
  26730. ret = -11761 - i;
  26731. break;
  26732. }
  26733. if (wc_ed448_export_private_only(key, exportSKey, &exportSSz) != 0) {
  26734. ret = -11771 - i;
  26735. break;
  26736. }
  26737. if (wc_ed448_import_private_key(exportSKey, exportSSz,
  26738. exportPKey, exportPSz, key2) != 0) {
  26739. ret = -11781 - i;
  26740. break;
  26741. }
  26742. /* clear "out" buffer and test sign with imported keys */
  26743. outlen = sizeof(out);
  26744. XMEMSET(out, 0, sizeof(out));
  26745. if (wc_ed448_sign_msg(msgs[i], msgSz[i], out, &outlen, key2, NULL,
  26746. 0) != 0) {
  26747. ret = -11791 - i;
  26748. break;
  26749. }
  26750. } while(0);
  26751. XFREE(exportPKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26752. XFREE(exportSKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26753. if (ret != 0)
  26754. goto out;
  26755. }
  26756. #if defined(HAVE_ED448_VERIFY)
  26757. if (wc_ed448_verify_msg(out, outlen, msgs[i], msgSz[i], &verify, key2,
  26758. NULL, 0) != 0 || verify != 1)
  26759. ERROR_OUT(-11801 - i, out);
  26760. if (XMEMCMP(out, sigs[i], SIGSZ))
  26761. ERROR_OUT(-11811 - i, out);
  26762. #endif /* HAVE_ED448_VERIFY */
  26763. }
  26764. ret = ed448_ctx_test();
  26765. if (ret != 0)
  26766. goto out;
  26767. ret = ed448ph_test();
  26768. if (ret != 0)
  26769. goto out;
  26770. #ifndef NO_ASN
  26771. /* Try ASN.1 encoded private-only key and public key. */
  26772. idx = 0;
  26773. if (wc_Ed448PrivateKeyDecode(privateEd448, &idx, key3,
  26774. sizeof(privateEd448)) != 0)
  26775. ERROR_OUT(-11821, out);
  26776. if (wc_ed448_sign_msg(msgs[0], msgSz[0], out, &outlen, key3, NULL, 0)
  26777. != BAD_FUNC_ARG)
  26778. ERROR_OUT(-11831, out);
  26779. idx = 0;
  26780. if (wc_Ed448PublicKeyDecode(publicEd448, &idx, key3,
  26781. sizeof(publicEd448)) != 0)
  26782. ERROR_OUT(-11841, out);
  26783. if (wc_ed448_sign_msg(msgs[0], msgSz[0], out, &outlen, key3, NULL, 0) != 0)
  26784. ERROR_OUT(-11851, out);
  26785. if (XMEMCMP(out, sigs[0], SIGSZ))
  26786. ERROR_OUT(-11861, out);
  26787. #if defined(HAVE_ED448_VERIFY)
  26788. /* test verify on good msg */
  26789. if (wc_ed448_verify_msg(out, outlen, msgs[0], msgSz[0], &verify, key3,
  26790. NULL, 0) != 0 || verify != 1)
  26791. ERROR_OUT(-11871, out);
  26792. #endif /* HAVE_ED448_VERIFY */
  26793. wc_ed448_free(key3);
  26794. if (wc_ed448_init(key3) < 0)
  26795. ERROR_OUT(-11908, out);
  26796. idx = 0;
  26797. if (wc_Ed448PrivateKeyDecode(privPubEd448, &idx, key3,
  26798. sizeof(privPubEd448)) != 0)
  26799. ERROR_OUT(-11881, out);
  26800. if (wc_ed448_sign_msg(msgs[0], msgSz[0], out, &outlen, key3, NULL, 0) != 0)
  26801. ERROR_OUT(-11891, out);
  26802. if (XMEMCMP(out, sigs[0], SIGSZ))
  26803. ERROR_OUT(-11901, out);
  26804. #endif /* NO_ASN */
  26805. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_EXPORT && HAVE_ED448_KEY_IMPORT */
  26806. ret = 0;
  26807. out:
  26808. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  26809. if (key) {
  26810. wc_ed448_free(key);
  26811. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26812. }
  26813. if (key2) {
  26814. wc_ed448_free(key2);
  26815. XFREE(key2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26816. }
  26817. #if !defined(NO_ASN) && defined(HAVE_ED448_SIGN)
  26818. if (key3) {
  26819. wc_ed448_free(key3);
  26820. XFREE(key3, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26821. }
  26822. #endif
  26823. #else
  26824. wc_ed448_free(key);
  26825. wc_ed448_free(key2);
  26826. #if !defined(NO_ASN) && defined(HAVE_ED448_SIGN)
  26827. wc_ed448_free(key3);
  26828. #endif
  26829. #endif
  26830. #if defined(HAVE_HASHDRBG) || defined(NO_RC4)
  26831. wc_FreeRng(&rng);
  26832. #endif
  26833. if (ret < 0)
  26834. return ret;
  26835. /* hush warnings of unused keySz and sigSz */
  26836. (void)keySz;
  26837. (void)sigSz;
  26838. #ifdef WOLFSSL_TEST_CERT
  26839. ret = ed448_test_cert();
  26840. if (ret < 0)
  26841. return ret;
  26842. #ifdef WOLFSSL_CERT_GEN
  26843. ret = ed448_test_make_cert();
  26844. if (ret < 0)
  26845. return ret;
  26846. #endif /* WOLFSSL_CERT_GEN */
  26847. #endif /* WOLFSSL_TEST_CERT */
  26848. return 0;
  26849. }
  26850. #endif /* HAVE_ED448 */
  26851. #ifdef WOLFSSL_HAVE_KYBER
  26852. #ifdef WOLFSSL_WC_KYBER /* OQS and PQM4 do not support KATs */
  26853. #ifdef WOLFSSL_KYBER512
  26854. static int kyber512_kat(void)
  26855. {
  26856. KyberKey key;
  26857. int ret;
  26858. byte priv[KYBER512_PRIVATE_KEY_SIZE];
  26859. byte pub[KYBER512_PUBLIC_KEY_SIZE];
  26860. byte ct[KYBER512_CIPHER_TEXT_SIZE];
  26861. byte ss[KYBER_SS_SZ];
  26862. byte ss_dec[KYBER_SS_SZ];
  26863. const byte kyber512_rand[] = {
  26864. 0x7c, 0x99, 0x35, 0xa0, 0xb0, 0x76, 0x94, 0xaa,
  26865. 0x0c, 0x6d, 0x10, 0xe4, 0xdb, 0x6b, 0x1a, 0xdd,
  26866. 0x2f, 0xd8, 0x1a, 0x25, 0xcc, 0xb1, 0x48, 0x03,
  26867. 0x2d, 0xcd, 0x73, 0x99, 0x36, 0x73, 0x7f, 0x2d,
  26868. 0x86, 0x26, 0xED, 0x79, 0xD4, 0x51, 0x14, 0x08,
  26869. 0x00, 0xE0, 0x3B, 0x59, 0xB9, 0x56, 0xF8, 0x21,
  26870. 0x0E, 0x55, 0x60, 0x67, 0x40, 0x7D, 0x13, 0xDC,
  26871. 0x90, 0xFA, 0x9E, 0x8B, 0x87, 0x2B, 0xFB, 0x8F
  26872. };
  26873. const byte kyber512enc_rand[] = {
  26874. 0x14, 0x7c, 0x03, 0xf7, 0xa5, 0xbe, 0xbb, 0xa4,
  26875. 0x06, 0xc8, 0xfa, 0xe1, 0x87, 0x4d, 0x7f, 0x13,
  26876. 0xc8, 0x0e, 0xfe, 0x79, 0xa3, 0xa9, 0xa8, 0x74,
  26877. 0xcc, 0x09, 0xfe, 0x76, 0xf6, 0x99, 0x76, 0x15
  26878. };
  26879. const byte kyber512_pk[] = {
  26880. 0x11, 0x5A, 0xCE, 0x0E, 0x64, 0x67, 0x7C, 0xBB,
  26881. 0x7D, 0xCF, 0xC9, 0x3C, 0x16, 0xD3, 0xA3, 0x05,
  26882. 0xF6, 0x76, 0x15, 0xA4, 0x88, 0xD7, 0x11, 0xAA,
  26883. 0x56, 0x69, 0x8C, 0x56, 0x63, 0xAB, 0x7A, 0xC9,
  26884. 0xCE, 0x66, 0xD5, 0x47, 0xC0, 0x59, 0x5F, 0x98,
  26885. 0xA4, 0x3F, 0x46, 0x50, 0xBB, 0xE0, 0x8C, 0x36,
  26886. 0x4D, 0x97, 0x67, 0x89, 0x11, 0x7D, 0x34, 0xF6,
  26887. 0xAE, 0x51, 0xAC, 0x06, 0x3C, 0xB5, 0x5C, 0x6C,
  26888. 0xA3, 0x25, 0x58, 0x22, 0x7D, 0xFE, 0xF8, 0x07,
  26889. 0xD1, 0x9C, 0x30, 0xDE, 0x41, 0x44, 0x24, 0x09,
  26890. 0x7F, 0x6A, 0xA2, 0x36, 0xA1, 0x05, 0x3B, 0x4A,
  26891. 0x07, 0xA7, 0x6B, 0xE3, 0x72, 0xA5, 0xC6, 0xB6,
  26892. 0x00, 0x27, 0x91, 0xEB, 0xE0, 0xAF, 0xDA, 0xF5,
  26893. 0x4E, 0x1C, 0xA2, 0x37, 0xFF, 0x54, 0x5B, 0xA6,
  26894. 0x83, 0x43, 0xE7, 0x45, 0xC0, 0x4A, 0xD1, 0x63,
  26895. 0x9D, 0xBC, 0x59, 0x03, 0x46, 0xB6, 0xB9, 0x56,
  26896. 0x9B, 0x56, 0xDB, 0xBF, 0xE5, 0x31, 0x51, 0x91,
  26897. 0x30, 0x66, 0xE5, 0xC8, 0x55, 0x27, 0xDC, 0x94,
  26898. 0x68, 0x11, 0x0A, 0x13, 0x6A, 0x41, 0x14, 0x97,
  26899. 0xC2, 0x27, 0xDC, 0xB8, 0xC9, 0xB2, 0x55, 0x70,
  26900. 0xB7, 0xA0, 0xE4, 0x2A, 0xAD, 0xA6, 0x70, 0x9F,
  26901. 0x23, 0x20, 0x8F, 0x5D, 0x49, 0x6E, 0xBA, 0xB7,
  26902. 0x84, 0x3F, 0x64, 0x83, 0xBF, 0x0C, 0x0C, 0x73,
  26903. 0xA4, 0x02, 0x96, 0xEC, 0x2C, 0x64, 0x40, 0x00,
  26904. 0x13, 0x94, 0xC9, 0x9C, 0xA1, 0x73, 0xD5, 0xC7,
  26905. 0x75, 0xB7, 0xF4, 0x15, 0xD0, 0x2A, 0x5A, 0x26,
  26906. 0xA0, 0x74, 0x07, 0x91, 0x85, 0x87, 0xC4, 0x11,
  26907. 0x69, 0xF2, 0xB7, 0x17, 0x87, 0x55, 0xAC, 0xC2,
  26908. 0x7F, 0xC8, 0xB1, 0x9C, 0x4C, 0x4B, 0x3F, 0xCD,
  26909. 0x41, 0x05, 0x3F, 0x2C, 0x74, 0xC8, 0xA1, 0x0A,
  26910. 0x83, 0x21, 0x24, 0x1B, 0x28, 0x02, 0x43, 0x28,
  26911. 0x75, 0xAE, 0x80, 0x8B, 0x9E, 0xF1, 0x36, 0x5C,
  26912. 0x7B, 0x8A, 0x52, 0x90, 0x2F, 0x13, 0x17, 0xBA,
  26913. 0x2F, 0xB0, 0x26, 0x9F, 0x47, 0x93, 0x06, 0x72,
  26914. 0x10, 0x7B, 0x47, 0x26, 0xFE, 0xF6, 0x45, 0x47,
  26915. 0x39, 0x4D, 0x33, 0x20, 0xC8, 0xF1, 0x20, 0xB3,
  26916. 0xC2, 0xF4, 0x72, 0x5B, 0x03, 0x05, 0xFA, 0xB8,
  26917. 0x8C, 0xC7, 0x98, 0x1F, 0xCB, 0x09, 0xA7, 0x6A,
  26918. 0x1C, 0xBF, 0x7F, 0x17, 0x9F, 0x43, 0xBB, 0x0A,
  26919. 0x4C, 0x8B, 0x05, 0x90, 0x85, 0x7F, 0x1E, 0x69,
  26920. 0x70, 0x84, 0x66, 0xC7, 0xF8, 0x60, 0x73, 0x91,
  26921. 0xE7, 0xBC, 0x52, 0x68, 0xBF, 0xD3, 0xD7, 0xA1,
  26922. 0xDF, 0xFC, 0xB4, 0xEC, 0xA2, 0xA1, 0xC9, 0xB5,
  26923. 0x97, 0x59, 0x30, 0x13, 0xD5, 0xFC, 0x42, 0x02,
  26924. 0xEC, 0x2B, 0x74, 0xE5, 0x7A, 0xB7, 0x6B, 0xBC,
  26925. 0xF3, 0x63, 0x2B, 0xBA, 0xF9, 0x7C, 0xDC, 0x41,
  26926. 0x8A, 0x6F, 0x16, 0x39, 0x28, 0x38, 0xCA, 0x9B,
  26927. 0xF4, 0x5D, 0xDF, 0x02, 0x37, 0x77, 0xB7, 0x56,
  26928. 0x18, 0x33, 0xC1, 0x05, 0x19, 0x0F, 0x94, 0xF3,
  26929. 0x02, 0xC5, 0x9B, 0x53, 0x19, 0x00, 0xBB, 0xC8,
  26930. 0x16, 0x36, 0x1F, 0xAA, 0x5B, 0x33, 0x80, 0xCA,
  26931. 0x3A, 0x89, 0x31, 0x04, 0xCA, 0x73, 0x88, 0xB1,
  26932. 0x85, 0x67, 0x1B, 0x3E, 0x5F, 0xE3, 0x79, 0x0E,
  26933. 0x9A, 0x62, 0x6E, 0xC4, 0x6D, 0x9B, 0x0B, 0x33,
  26934. 0xC7, 0xA4, 0x19, 0xAF, 0x7B, 0x32, 0xB6, 0x85,
  26935. 0x98, 0x94, 0xF5, 0x75, 0xD8, 0x2A, 0xC5, 0x45,
  26936. 0x6B, 0x54, 0x90, 0xA7, 0xAF, 0x8F, 0xE6, 0x10,
  26937. 0x46, 0x36, 0x05, 0x89, 0xEC, 0xBA, 0x72, 0x44,
  26938. 0x23, 0x6F, 0x41, 0x23, 0x11, 0x6B, 0x61, 0x74,
  26939. 0xAA, 0x17, 0x92, 0x49, 0xA4, 0x91, 0x95, 0xB3,
  26940. 0x56, 0xC7, 0x2F, 0xC6, 0x64, 0x1F, 0x02, 0x51,
  26941. 0x81, 0x2E, 0xAA, 0x98, 0x57, 0x0B, 0x04, 0x66,
  26942. 0x99, 0x07, 0x0E, 0x08, 0x19, 0xDC, 0x27, 0x13,
  26943. 0xF4, 0x69, 0x13, 0x7D, 0xFC, 0x6A, 0x3D, 0x7B,
  26944. 0x92, 0xB2, 0x98, 0x99, 0x5E, 0xE7, 0x80, 0x36,
  26945. 0x91, 0x53, 0xAC, 0x36, 0x6B, 0x06, 0xD7, 0x24,
  26946. 0x9C, 0xD0, 0x9E, 0x1B, 0x33, 0x78, 0xFB, 0x04,
  26947. 0x39, 0x9C, 0xEC, 0xB8, 0x65, 0x05, 0x81, 0xD6,
  26948. 0x37, 0xC7, 0x9A, 0xE6, 0x7D, 0x6F, 0x2C, 0xAF,
  26949. 0x6A, 0xBA, 0xCF, 0x59, 0x81, 0x59, 0xA7, 0x79,
  26950. 0x2C, 0xB3, 0xC9, 0x71, 0xD1, 0x49, 0x9D, 0x23,
  26951. 0x73, 0xAD, 0x20, 0xF6, 0x3F, 0x03, 0xBB, 0x59,
  26952. 0xED, 0x13, 0x73, 0x84, 0xAC, 0x61, 0xA7, 0x15,
  26953. 0x51, 0x43, 0xB8, 0xCA, 0x49, 0x32, 0x61, 0x2E,
  26954. 0xC9, 0x15, 0xE4, 0xCA, 0x34, 0x6A, 0x9B, 0xCE,
  26955. 0x5D, 0xD6, 0x04, 0x17, 0xC6, 0xB2, 0xA8, 0x9B,
  26956. 0x1C, 0xC4, 0x35, 0x64, 0x3F, 0x87, 0x5B, 0xDC,
  26957. 0x5A, 0x7E, 0x5B, 0x34, 0x81, 0xCF, 0x91, 0x9E,
  26958. 0xA0, 0x91, 0x72, 0xFE, 0xBC, 0x46, 0xD4, 0xFC,
  26959. 0x3F, 0xB0, 0xCB, 0x95, 0x91, 0x70, 0x4E, 0xE2,
  26960. 0xDB, 0xB6, 0x18, 0x44, 0xB2, 0xF3, 0x31, 0x4A,
  26961. 0x06, 0xBB, 0x6C, 0x6D, 0x34, 0x00, 0x5E, 0x48,
  26962. 0x5C, 0xE6, 0x67, 0xBD, 0xC7, 0xD0, 0x98, 0x58,
  26963. 0x69, 0x28, 0xD2, 0xD9, 0x13, 0x40, 0xF0, 0x04,
  26964. 0x19, 0xEA, 0x40, 0x13, 0x51, 0xA2, 0x40, 0xA0,
  26965. 0xB0, 0x41, 0x05, 0x8B, 0xEF, 0xB0, 0xC2, 0xFD,
  26966. 0x32, 0x64, 0x5B, 0x7A, 0x2D, 0xF8, 0xF5, 0xCB,
  26967. 0xFD, 0x87, 0x33, 0x27, 0xC9, 0x78, 0xD7, 0xB3,
  26968. 0x51, 0xA2, 0x80, 0x88, 0x43, 0x88, 0x37, 0x02,
  26969. 0x4C, 0x52, 0xB9, 0xC2, 0x95, 0xCD, 0x71, 0x36,
  26970. 0x46, 0xFB, 0x5D, 0x6C, 0x0C, 0xCF, 0xB4, 0x70,
  26971. 0x73, 0x4A, 0xC2, 0xB2, 0xBC, 0x81, 0x23, 0xC2,
  26972. 0xC1, 0x3D, 0xF6, 0x93, 0x8E, 0x92, 0x45, 0x5A,
  26973. 0x86, 0x26, 0x39, 0xFE, 0xB8, 0xA6, 0x4B, 0x85,
  26974. 0x16, 0x3E, 0x32, 0x70, 0x7E, 0x03, 0x7B, 0x38,
  26975. 0xD8, 0xAC, 0x39, 0x22, 0xB4, 0x51, 0x87, 0xBB,
  26976. 0x65, 0xEA, 0xFD, 0x46, 0x5F, 0xC6, 0x4A, 0x0C,
  26977. 0x5F, 0x8F, 0x3F, 0x90, 0x03, 0x48, 0x94, 0x15,
  26978. 0x89, 0x9D, 0x59, 0xA5, 0x43, 0xD8, 0x20, 0x8C,
  26979. 0x54, 0xA3, 0x16, 0x65, 0x29, 0xB5, 0x39, 0x22
  26980. };
  26981. const byte kyber512_sk[] = {
  26982. 0x6C, 0x89, 0x2B, 0x02, 0x97, 0xA9, 0xC7, 0x64,
  26983. 0x14, 0x93, 0xF8, 0x7D, 0xAF, 0x35, 0x33, 0xEE,
  26984. 0xD6, 0x1F, 0x07, 0xF4, 0x65, 0x20, 0x66, 0x33,
  26985. 0x7E, 0xD7, 0x40, 0x46, 0xDC, 0xC7, 0x1B, 0xA0,
  26986. 0x3F, 0x30, 0x96, 0x01, 0x03, 0x16, 0x1F, 0x7D,
  26987. 0xEB, 0x53, 0xA7, 0x1B, 0x11, 0x61, 0x72, 0x63,
  26988. 0xFE, 0x2A, 0x80, 0x97, 0x69, 0xCE, 0x6D, 0x70,
  26989. 0xA8, 0x5F, 0xE6, 0x00, 0xEC, 0xE2, 0x9D, 0x7F,
  26990. 0x36, 0xA1, 0x6D, 0x33, 0x1B, 0x8B, 0x2A, 0x9E,
  26991. 0x1D, 0xB8, 0xC0, 0x90, 0x74, 0x2D, 0xF0, 0x73,
  26992. 0x9F, 0xF0, 0x60, 0xCE, 0xB4, 0xEC, 0xC5, 0xAB,
  26993. 0x1C, 0x5E, 0x55, 0xAC, 0x97, 0xBB, 0x66, 0xA7,
  26994. 0xF8, 0x95, 0x10, 0x5D, 0x57, 0x78, 0x2B, 0x22,
  26995. 0x95, 0x38, 0xE3, 0x42, 0x15, 0x44, 0xA3, 0x42,
  26996. 0x14, 0x08, 0xDB, 0xF4, 0x49, 0x10, 0x93, 0x4C,
  26997. 0xC4, 0x23, 0x77, 0x4F, 0x16, 0x76, 0xFF, 0x1C,
  26998. 0x30, 0x6F, 0x97, 0x55, 0x5F, 0x57, 0xB4, 0xAE,
  26999. 0xD7, 0xA6, 0xBA, 0xB9, 0x50, 0xA8, 0x16, 0x3C,
  27000. 0x8D, 0x31, 0x8D, 0xEA, 0x62, 0x75, 0x1B, 0xD6,
  27001. 0xAB, 0xC5, 0x06, 0x9C, 0x06, 0xC8, 0x8F, 0x33,
  27002. 0x00, 0x26, 0xA1, 0x98, 0x06, 0xA0, 0x3B, 0x97,
  27003. 0xA7, 0x69, 0x6B, 0x56, 0xDA, 0x21, 0x82, 0x7B,
  27004. 0xB4, 0xE8, 0xDC, 0x03, 0x11, 0x52, 0xB4, 0x1B,
  27005. 0x89, 0x2A, 0x9E, 0x99, 0xAD, 0xF6, 0xE1, 0x96,
  27006. 0x3E, 0x96, 0x57, 0x88, 0x28, 0x15, 0x4F, 0x46,
  27007. 0x70, 0x33, 0x84, 0x69, 0x20, 0xFB, 0xB4, 0xB8,
  27008. 0x05, 0x44, 0xE7, 0xE8, 0xA8, 0x1A, 0xE9, 0x63,
  27009. 0xCF, 0x36, 0x8C, 0x9B, 0xA0, 0x37, 0xA8, 0xC2,
  27010. 0xAD, 0x62, 0xE3, 0x2B, 0x6E, 0x61, 0xC9, 0x1D,
  27011. 0x75, 0xCE, 0x00, 0x5A, 0xB3, 0x0F, 0x80, 0x99,
  27012. 0xA1, 0xF2, 0x9D, 0x7B, 0x63, 0x05, 0xB4, 0xDC,
  27013. 0x06, 0xE2, 0x56, 0x80, 0xBB, 0x00, 0x99, 0x2F,
  27014. 0x71, 0x7F, 0xE6, 0xC1, 0x15, 0xA8, 0x08, 0x42,
  27015. 0x31, 0xCC, 0x79, 0xDD, 0x70, 0x0E, 0xA6, 0x91,
  27016. 0x2A, 0xC7, 0xFA, 0x0D, 0x93, 0x7B, 0xB6, 0xA7,
  27017. 0x56, 0x66, 0x22, 0x30, 0x47, 0x0C, 0x18, 0x9B,
  27018. 0x5A, 0xA1, 0x65, 0x3D, 0xEB, 0x93, 0x7D, 0x5A,
  27019. 0x9C, 0x25, 0xA2, 0x1D, 0x93, 0xB1, 0x90, 0x74,
  27020. 0xFC, 0x23, 0x9D, 0x81, 0x53, 0x53, 0x97, 0x97,
  27021. 0xC7, 0xD4, 0xAB, 0x62, 0x64, 0x9D, 0x76, 0xAA,
  27022. 0x55, 0x37, 0x36, 0xA9, 0x49, 0x02, 0x2C, 0x22,
  27023. 0xC5, 0x2B, 0xAE, 0xEC, 0x60, 0x5B, 0x32, 0xCE,
  27024. 0x9E, 0x5B, 0x93, 0x84, 0x90, 0x35, 0x58, 0xCA,
  27025. 0x9D, 0x6A, 0x3A, 0xBA, 0x90, 0x42, 0x3E, 0xED,
  27026. 0xA0, 0x1C, 0x94, 0x19, 0x8B, 0x19, 0x2A, 0x8B,
  27027. 0xA9, 0x06, 0x34, 0x97, 0xA0, 0xC5, 0x01, 0x33,
  27028. 0x07, 0xDD, 0xD8, 0x63, 0x52, 0x64, 0x71, 0xA4,
  27029. 0xD9, 0x95, 0x23, 0xEB, 0x41, 0x7F, 0x29, 0x1A,
  27030. 0xAC, 0x0C, 0x3A, 0x58, 0x1B, 0x6D, 0xA0, 0x07,
  27031. 0x32, 0xE5, 0xE8, 0x1B, 0x1F, 0x7C, 0x87, 0x9B,
  27032. 0x16, 0x93, 0xC1, 0x3B, 0x6F, 0x9F, 0x79, 0x31,
  27033. 0x62, 0x24, 0x29, 0xE5, 0x42, 0xAF, 0x40, 0x69,
  27034. 0x22, 0x2F, 0x04, 0x55, 0x44, 0xE0, 0xCC, 0x4F,
  27035. 0xB2, 0x4D, 0x44, 0x48, 0xCF, 0x2C, 0x65, 0x96,
  27036. 0xF5, 0xCB, 0x08, 0x62, 0x4B, 0x11, 0x85, 0x01,
  27037. 0x3B, 0x6B, 0x02, 0x08, 0x92, 0xF9, 0x6B, 0xDF,
  27038. 0xD4, 0xAD, 0xA9, 0x17, 0x9D, 0xE7, 0x27, 0xB8,
  27039. 0xD9, 0x42, 0x6E, 0x09, 0x96, 0xB5, 0xD3, 0x49,
  27040. 0x48, 0xCE, 0x02, 0xD0, 0xC3, 0x69, 0xB3, 0x7C,
  27041. 0xBB, 0x54, 0xD3, 0x47, 0x9E, 0xD8, 0xB5, 0x82,
  27042. 0xE9, 0xE7, 0x28, 0x92, 0x9B, 0x4C, 0x71, 0xC9,
  27043. 0xBE, 0x11, 0xD4, 0x5B, 0x20, 0xC4, 0xBD, 0xC3,
  27044. 0xC7, 0x43, 0x13, 0x22, 0x3F, 0x58, 0x27, 0x4E,
  27045. 0x8B, 0xA5, 0x24, 0x44, 0x47, 0xC4, 0x95, 0x95,
  27046. 0x0B, 0x84, 0xCB, 0x0C, 0x3C, 0x27, 0x36, 0x40,
  27047. 0x10, 0x8A, 0x33, 0x97, 0x94, 0x45, 0x73, 0x27,
  27048. 0x93, 0x28, 0x99, 0x6C, 0xDC, 0x0C, 0x91, 0x3C,
  27049. 0x95, 0x8A, 0xD6, 0x20, 0xBA, 0x8B, 0x5E, 0x5E,
  27050. 0xCB, 0xBB, 0x7E, 0x13, 0xCB, 0x9C, 0x70, 0xBD,
  27051. 0x5A, 0xB3, 0x0E, 0xB7, 0x48, 0x8C, 0x97, 0x00,
  27052. 0x1C, 0x20, 0x49, 0x8F, 0x1D, 0x7C, 0xC0, 0x6D,
  27053. 0xA7, 0x6B, 0xF5, 0x20, 0xC6, 0x58, 0xCC, 0xAD,
  27054. 0xFA, 0x29, 0x56, 0x42, 0x45, 0x57, 0xAB, 0xEA,
  27055. 0x8A, 0xB8, 0x92, 0x39, 0xC1, 0x78, 0x33, 0xDC,
  27056. 0x3A, 0x49, 0xB3, 0x6A, 0x9A, 0xE9, 0xA4, 0x86,
  27057. 0x94, 0x05, 0x40, 0xEB, 0x44, 0x4F, 0x97, 0x15,
  27058. 0x23, 0x57, 0xE0, 0x20, 0x35, 0x93, 0x9D, 0x75,
  27059. 0xA3, 0xC0, 0x25, 0xF4, 0x1A, 0x40, 0x08, 0x23,
  27060. 0x82, 0xA0, 0x73, 0x3C, 0x39, 0xB0, 0x62, 0x2B,
  27061. 0x74, 0x0E, 0x40, 0x75, 0x92, 0xC6, 0x2E, 0xCA,
  27062. 0xEB, 0x14, 0x32, 0xC4, 0x45, 0xB3, 0x70, 0x3A,
  27063. 0x86, 0xF6, 0x98, 0x1A, 0x27, 0x81, 0x57, 0xEA,
  27064. 0x95, 0xA6, 0xE9, 0x2D, 0x55, 0xE4, 0xB9, 0x72,
  27065. 0xF9, 0x36, 0xC2, 0xF0, 0xA6, 0x58, 0x28, 0x0E,
  27066. 0xA2, 0xB0, 0x7A, 0x48, 0x99, 0x2D, 0xF8, 0x93,
  27067. 0x7E, 0x0A, 0x2A, 0xC1, 0xDC, 0xC9, 0x74, 0xFE,
  27068. 0x00, 0xAA, 0xE1, 0xF5, 0x61, 0xFA, 0x25, 0x8E,
  27069. 0x2D, 0x25, 0x9C, 0x3E, 0x86, 0x1D, 0xCE, 0x23,
  27070. 0x60, 0x39, 0x12, 0x76, 0x06, 0xFC, 0x1C, 0xE0,
  27071. 0x09, 0x00, 0x3A, 0x7B, 0xAC, 0x94, 0x21, 0x01,
  27072. 0xDC, 0xB8, 0x22, 0xB1, 0xF3, 0xC1, 0x2B, 0xF7,
  27073. 0x32, 0x38, 0xF5, 0x46, 0xE0, 0x1C, 0x36, 0xB5,
  27074. 0xA6, 0x93, 0x61, 0x92, 0x99, 0x5C, 0xC6, 0x9C,
  27075. 0x63, 0x23, 0x74, 0x09, 0xCB, 0x53, 0xC2, 0xE3,
  27076. 0x5D, 0x74, 0x89, 0x0D, 0x18, 0x88, 0x53, 0x76,
  27077. 0xFA, 0x55, 0x03, 0xB1, 0x07, 0xA2, 0xA3, 0x92,
  27078. 0x11, 0x5A, 0xCE, 0x0E, 0x64, 0x67, 0x7C, 0xBB,
  27079. 0x7D, 0xCF, 0xC9, 0x3C, 0x16, 0xD3, 0xA3, 0x05,
  27080. 0xF6, 0x76, 0x15, 0xA4, 0x88, 0xD7, 0x11, 0xAA,
  27081. 0x56, 0x69, 0x8C, 0x56, 0x63, 0xAB, 0x7A, 0xC9,
  27082. 0xCE, 0x66, 0xD5, 0x47, 0xC0, 0x59, 0x5F, 0x98,
  27083. 0xA4, 0x3F, 0x46, 0x50, 0xBB, 0xE0, 0x8C, 0x36,
  27084. 0x4D, 0x97, 0x67, 0x89, 0x11, 0x7D, 0x34, 0xF6,
  27085. 0xAE, 0x51, 0xAC, 0x06, 0x3C, 0xB5, 0x5C, 0x6C,
  27086. 0xA3, 0x25, 0x58, 0x22, 0x7D, 0xFE, 0xF8, 0x07,
  27087. 0xD1, 0x9C, 0x30, 0xDE, 0x41, 0x44, 0x24, 0x09,
  27088. 0x7F, 0x6A, 0xA2, 0x36, 0xA1, 0x05, 0x3B, 0x4A,
  27089. 0x07, 0xA7, 0x6B, 0xE3, 0x72, 0xA5, 0xC6, 0xB6,
  27090. 0x00, 0x27, 0x91, 0xEB, 0xE0, 0xAF, 0xDA, 0xF5,
  27091. 0x4E, 0x1C, 0xA2, 0x37, 0xFF, 0x54, 0x5B, 0xA6,
  27092. 0x83, 0x43, 0xE7, 0x45, 0xC0, 0x4A, 0xD1, 0x63,
  27093. 0x9D, 0xBC, 0x59, 0x03, 0x46, 0xB6, 0xB9, 0x56,
  27094. 0x9B, 0x56, 0xDB, 0xBF, 0xE5, 0x31, 0x51, 0x91,
  27095. 0x30, 0x66, 0xE5, 0xC8, 0x55, 0x27, 0xDC, 0x94,
  27096. 0x68, 0x11, 0x0A, 0x13, 0x6A, 0x41, 0x14, 0x97,
  27097. 0xC2, 0x27, 0xDC, 0xB8, 0xC9, 0xB2, 0x55, 0x70,
  27098. 0xB7, 0xA0, 0xE4, 0x2A, 0xAD, 0xA6, 0x70, 0x9F,
  27099. 0x23, 0x20, 0x8F, 0x5D, 0x49, 0x6E, 0xBA, 0xB7,
  27100. 0x84, 0x3F, 0x64, 0x83, 0xBF, 0x0C, 0x0C, 0x73,
  27101. 0xA4, 0x02, 0x96, 0xEC, 0x2C, 0x64, 0x40, 0x00,
  27102. 0x13, 0x94, 0xC9, 0x9C, 0xA1, 0x73, 0xD5, 0xC7,
  27103. 0x75, 0xB7, 0xF4, 0x15, 0xD0, 0x2A, 0x5A, 0x26,
  27104. 0xA0, 0x74, 0x07, 0x91, 0x85, 0x87, 0xC4, 0x11,
  27105. 0x69, 0xF2, 0xB7, 0x17, 0x87, 0x55, 0xAC, 0xC2,
  27106. 0x7F, 0xC8, 0xB1, 0x9C, 0x4C, 0x4B, 0x3F, 0xCD,
  27107. 0x41, 0x05, 0x3F, 0x2C, 0x74, 0xC8, 0xA1, 0x0A,
  27108. 0x83, 0x21, 0x24, 0x1B, 0x28, 0x02, 0x43, 0x28,
  27109. 0x75, 0xAE, 0x80, 0x8B, 0x9E, 0xF1, 0x36, 0x5C,
  27110. 0x7B, 0x8A, 0x52, 0x90, 0x2F, 0x13, 0x17, 0xBA,
  27111. 0x2F, 0xB0, 0x26, 0x9F, 0x47, 0x93, 0x06, 0x72,
  27112. 0x10, 0x7B, 0x47, 0x26, 0xFE, 0xF6, 0x45, 0x47,
  27113. 0x39, 0x4D, 0x33, 0x20, 0xC8, 0xF1, 0x20, 0xB3,
  27114. 0xC2, 0xF4, 0x72, 0x5B, 0x03, 0x05, 0xFA, 0xB8,
  27115. 0x8C, 0xC7, 0x98, 0x1F, 0xCB, 0x09, 0xA7, 0x6A,
  27116. 0x1C, 0xBF, 0x7F, 0x17, 0x9F, 0x43, 0xBB, 0x0A,
  27117. 0x4C, 0x8B, 0x05, 0x90, 0x85, 0x7F, 0x1E, 0x69,
  27118. 0x70, 0x84, 0x66, 0xC7, 0xF8, 0x60, 0x73, 0x91,
  27119. 0xE7, 0xBC, 0x52, 0x68, 0xBF, 0xD3, 0xD7, 0xA1,
  27120. 0xDF, 0xFC, 0xB4, 0xEC, 0xA2, 0xA1, 0xC9, 0xB5,
  27121. 0x97, 0x59, 0x30, 0x13, 0xD5, 0xFC, 0x42, 0x02,
  27122. 0xEC, 0x2B, 0x74, 0xE5, 0x7A, 0xB7, 0x6B, 0xBC,
  27123. 0xF3, 0x63, 0x2B, 0xBA, 0xF9, 0x7C, 0xDC, 0x41,
  27124. 0x8A, 0x6F, 0x16, 0x39, 0x28, 0x38, 0xCA, 0x9B,
  27125. 0xF4, 0x5D, 0xDF, 0x02, 0x37, 0x77, 0xB7, 0x56,
  27126. 0x18, 0x33, 0xC1, 0x05, 0x19, 0x0F, 0x94, 0xF3,
  27127. 0x02, 0xC5, 0x9B, 0x53, 0x19, 0x00, 0xBB, 0xC8,
  27128. 0x16, 0x36, 0x1F, 0xAA, 0x5B, 0x33, 0x80, 0xCA,
  27129. 0x3A, 0x89, 0x31, 0x04, 0xCA, 0x73, 0x88, 0xB1,
  27130. 0x85, 0x67, 0x1B, 0x3E, 0x5F, 0xE3, 0x79, 0x0E,
  27131. 0x9A, 0x62, 0x6E, 0xC4, 0x6D, 0x9B, 0x0B, 0x33,
  27132. 0xC7, 0xA4, 0x19, 0xAF, 0x7B, 0x32, 0xB6, 0x85,
  27133. 0x98, 0x94, 0xF5, 0x75, 0xD8, 0x2A, 0xC5, 0x45,
  27134. 0x6B, 0x54, 0x90, 0xA7, 0xAF, 0x8F, 0xE6, 0x10,
  27135. 0x46, 0x36, 0x05, 0x89, 0xEC, 0xBA, 0x72, 0x44,
  27136. 0x23, 0x6F, 0x41, 0x23, 0x11, 0x6B, 0x61, 0x74,
  27137. 0xAA, 0x17, 0x92, 0x49, 0xA4, 0x91, 0x95, 0xB3,
  27138. 0x56, 0xC7, 0x2F, 0xC6, 0x64, 0x1F, 0x02, 0x51,
  27139. 0x81, 0x2E, 0xAA, 0x98, 0x57, 0x0B, 0x04, 0x66,
  27140. 0x99, 0x07, 0x0E, 0x08, 0x19, 0xDC, 0x27, 0x13,
  27141. 0xF4, 0x69, 0x13, 0x7D, 0xFC, 0x6A, 0x3D, 0x7B,
  27142. 0x92, 0xB2, 0x98, 0x99, 0x5E, 0xE7, 0x80, 0x36,
  27143. 0x91, 0x53, 0xAC, 0x36, 0x6B, 0x06, 0xD7, 0x24,
  27144. 0x9C, 0xD0, 0x9E, 0x1B, 0x33, 0x78, 0xFB, 0x04,
  27145. 0x39, 0x9C, 0xEC, 0xB8, 0x65, 0x05, 0x81, 0xD6,
  27146. 0x37, 0xC7, 0x9A, 0xE6, 0x7D, 0x6F, 0x2C, 0xAF,
  27147. 0x6A, 0xBA, 0xCF, 0x59, 0x81, 0x59, 0xA7, 0x79,
  27148. 0x2C, 0xB3, 0xC9, 0x71, 0xD1, 0x49, 0x9D, 0x23,
  27149. 0x73, 0xAD, 0x20, 0xF6, 0x3F, 0x03, 0xBB, 0x59,
  27150. 0xED, 0x13, 0x73, 0x84, 0xAC, 0x61, 0xA7, 0x15,
  27151. 0x51, 0x43, 0xB8, 0xCA, 0x49, 0x32, 0x61, 0x2E,
  27152. 0xC9, 0x15, 0xE4, 0xCA, 0x34, 0x6A, 0x9B, 0xCE,
  27153. 0x5D, 0xD6, 0x04, 0x17, 0xC6, 0xB2, 0xA8, 0x9B,
  27154. 0x1C, 0xC4, 0x35, 0x64, 0x3F, 0x87, 0x5B, 0xDC,
  27155. 0x5A, 0x7E, 0x5B, 0x34, 0x81, 0xCF, 0x91, 0x9E,
  27156. 0xA0, 0x91, 0x72, 0xFE, 0xBC, 0x46, 0xD4, 0xFC,
  27157. 0x3F, 0xB0, 0xCB, 0x95, 0x91, 0x70, 0x4E, 0xE2,
  27158. 0xDB, 0xB6, 0x18, 0x44, 0xB2, 0xF3, 0x31, 0x4A,
  27159. 0x06, 0xBB, 0x6C, 0x6D, 0x34, 0x00, 0x5E, 0x48,
  27160. 0x5C, 0xE6, 0x67, 0xBD, 0xC7, 0xD0, 0x98, 0x58,
  27161. 0x69, 0x28, 0xD2, 0xD9, 0x13, 0x40, 0xF0, 0x04,
  27162. 0x19, 0xEA, 0x40, 0x13, 0x51, 0xA2, 0x40, 0xA0,
  27163. 0xB0, 0x41, 0x05, 0x8B, 0xEF, 0xB0, 0xC2, 0xFD,
  27164. 0x32, 0x64, 0x5B, 0x7A, 0x2D, 0xF8, 0xF5, 0xCB,
  27165. 0xFD, 0x87, 0x33, 0x27, 0xC9, 0x78, 0xD7, 0xB3,
  27166. 0x51, 0xA2, 0x80, 0x88, 0x43, 0x88, 0x37, 0x02,
  27167. 0x4C, 0x52, 0xB9, 0xC2, 0x95, 0xCD, 0x71, 0x36,
  27168. 0x46, 0xFB, 0x5D, 0x6C, 0x0C, 0xCF, 0xB4, 0x70,
  27169. 0x73, 0x4A, 0xC2, 0xB2, 0xBC, 0x81, 0x23, 0xC2,
  27170. 0xC1, 0x3D, 0xF6, 0x93, 0x8E, 0x92, 0x45, 0x5A,
  27171. 0x86, 0x26, 0x39, 0xFE, 0xB8, 0xA6, 0x4B, 0x85,
  27172. 0x16, 0x3E, 0x32, 0x70, 0x7E, 0x03, 0x7B, 0x38,
  27173. 0xD8, 0xAC, 0x39, 0x22, 0xB4, 0x51, 0x87, 0xBB,
  27174. 0x65, 0xEA, 0xFD, 0x46, 0x5F, 0xC6, 0x4A, 0x0C,
  27175. 0x5F, 0x8F, 0x3F, 0x90, 0x03, 0x48, 0x94, 0x15,
  27176. 0x89, 0x9D, 0x59, 0xA5, 0x43, 0xD8, 0x20, 0x8C,
  27177. 0x54, 0xA3, 0x16, 0x65, 0x29, 0xB5, 0x39, 0x22,
  27178. 0x7F, 0xFA, 0xD1, 0xBC, 0x8A, 0xF7, 0x3B, 0x7E,
  27179. 0x87, 0x49, 0x56, 0xB8, 0x1C, 0x2A, 0x2E, 0xF0,
  27180. 0xBF, 0xAB, 0xE8, 0xDC, 0x93, 0xD7, 0x7B, 0x2F,
  27181. 0xBC, 0x9E, 0x0C, 0x64, 0xEF, 0xA0, 0x1E, 0x84,
  27182. 0x86, 0x26, 0xED, 0x79, 0xD4, 0x51, 0x14, 0x08,
  27183. 0x00, 0xE0, 0x3B, 0x59, 0xB9, 0x56, 0xF8, 0x21,
  27184. 0x0E, 0x55, 0x60, 0x67, 0x40, 0x7D, 0x13, 0xDC,
  27185. 0x90, 0xFA, 0x9E, 0x8B, 0x87, 0x2B, 0xFB, 0x8F
  27186. };
  27187. const byte kyber512_ct[] = {
  27188. 0xED, 0xF2, 0x41, 0x45, 0xE4, 0x3B, 0x4F, 0x6D,
  27189. 0xC6, 0xBF, 0x83, 0x32, 0xF5, 0x4E, 0x02, 0xCA,
  27190. 0xB0, 0x2D, 0xBF, 0x3B, 0x56, 0x05, 0xDD, 0xC9,
  27191. 0x0A, 0x15, 0xC8, 0x86, 0xAD, 0x3E, 0xD4, 0x89,
  27192. 0x46, 0x26, 0x99, 0xE4, 0xAB, 0xED, 0x44, 0x35,
  27193. 0x0B, 0xC3, 0x75, 0x7E, 0x26, 0x96, 0xFB, 0xFB,
  27194. 0x25, 0x34, 0x41, 0x2E, 0x8D, 0xD2, 0x01, 0xF1,
  27195. 0xE4, 0x54, 0x0A, 0x39, 0x70, 0xB0, 0x55, 0xFE,
  27196. 0x3B, 0x0B, 0xEC, 0x3A, 0x71, 0xF9, 0xE1, 0x15,
  27197. 0xB3, 0xF9, 0xF3, 0x91, 0x02, 0x06, 0x5B, 0x1C,
  27198. 0xCA, 0x83, 0x14, 0xDC, 0xC7, 0x95, 0xE3, 0xC0,
  27199. 0xE8, 0xFA, 0x98, 0xEE, 0x83, 0xCA, 0x66, 0x28,
  27200. 0x45, 0x70, 0x28, 0xA4, 0xD0, 0x9E, 0x83, 0x9E,
  27201. 0x55, 0x48, 0x62, 0xCF, 0x0B, 0x7B, 0xF5, 0x6C,
  27202. 0x5C, 0x0A, 0x82, 0x9E, 0x86, 0x57, 0x94, 0x79,
  27203. 0x45, 0xFE, 0x9C, 0x22, 0x56, 0x4F, 0xBA, 0xEB,
  27204. 0xC1, 0xB3, 0xAF, 0x35, 0x0D, 0x79, 0x55, 0x50,
  27205. 0x8A, 0x26, 0xD8, 0xA8, 0xEB, 0x54, 0x7B, 0x8B,
  27206. 0x1A, 0x2C, 0xF0, 0x3C, 0xCA, 0x1A, 0xAB, 0xCE,
  27207. 0x6C, 0x34, 0x97, 0x78, 0x3B, 0x64, 0x65, 0xBA,
  27208. 0x0B, 0x6E, 0x7A, 0xCB, 0xA8, 0x21, 0x19, 0x51,
  27209. 0x24, 0xAE, 0xF0, 0x9E, 0x62, 0x83, 0x82, 0xA1,
  27210. 0xF9, 0x14, 0x04, 0x3B, 0xE7, 0x09, 0x6E, 0x95,
  27211. 0x2C, 0xBC, 0x4F, 0xB4, 0xAF, 0xED, 0x13, 0x60,
  27212. 0x90, 0x46, 0x11, 0x7C, 0x01, 0x1F, 0xD7, 0x41,
  27213. 0xEE, 0x28, 0x6C, 0x83, 0x77, 0x16, 0x90, 0xF0,
  27214. 0xAE, 0xB5, 0x0D, 0xA0, 0xD7, 0x12, 0x85, 0xA1,
  27215. 0x79, 0xB2, 0x15, 0xC6, 0x03, 0x6D, 0xEB, 0x78,
  27216. 0x0F, 0x4D, 0x16, 0x76, 0x9F, 0x72, 0xDE, 0x16,
  27217. 0xFD, 0xAD, 0xAC, 0x73, 0xBE, 0xFA, 0x5B, 0xEF,
  27218. 0x89, 0x43, 0x19, 0x7F, 0x44, 0xC5, 0x95, 0x89,
  27219. 0xDC, 0x9F, 0x49, 0x73, 0xDE, 0x14, 0x50, 0xBA,
  27220. 0x1D, 0x0C, 0x32, 0x90, 0xD6, 0xB1, 0xD6, 0x83,
  27221. 0xF2, 0x94, 0xE7, 0x59, 0xC9, 0x54, 0xAB, 0xE8,
  27222. 0xA7, 0xDA, 0x5B, 0x10, 0x54, 0xFD, 0x6D, 0x21,
  27223. 0x32, 0x9B, 0x8E, 0x73, 0xD3, 0x75, 0x6A, 0xFD,
  27224. 0xA0, 0xDC, 0xB1, 0xFC, 0x8B, 0x15, 0x82, 0xD1,
  27225. 0xF9, 0x0C, 0xF2, 0x75, 0xA1, 0x02, 0xAB, 0xC6,
  27226. 0xAC, 0x69, 0x9D, 0xF0, 0xC5, 0x87, 0x0E, 0x50,
  27227. 0xA1, 0xF9, 0x89, 0xE4, 0xE6, 0x24, 0x1B, 0x60,
  27228. 0xAA, 0xA2, 0xEC, 0xF9, 0xE8, 0xE3, 0x3E, 0x0F,
  27229. 0xFC, 0xF4, 0x0F, 0xE8, 0x31, 0xE8, 0xFD, 0xC2,
  27230. 0xE8, 0x3B, 0x52, 0xCA, 0x7A, 0xB6, 0xD9, 0x3F,
  27231. 0x14, 0x6D, 0x29, 0xDC, 0xA5, 0x3C, 0x7D, 0xA1,
  27232. 0xDB, 0x4A, 0xC4, 0xF2, 0xDB, 0x39, 0xEA, 0x12,
  27233. 0x0D, 0x90, 0xFA, 0x60, 0xF4, 0xD4, 0x37, 0xC6,
  27234. 0xD0, 0x0E, 0xF4, 0x83, 0xBC, 0x94, 0xA3, 0x17,
  27235. 0x5C, 0xDA, 0x16, 0x3F, 0xC1, 0xC2, 0x82, 0x8B,
  27236. 0xE4, 0xDB, 0xD6, 0x43, 0x05, 0x07, 0xB5, 0x84,
  27237. 0xBB, 0x51, 0x77, 0xE1, 0x71, 0xB8, 0xDD, 0xA9,
  27238. 0xA4, 0x29, 0x3C, 0x32, 0x00, 0x29, 0x5C, 0x80,
  27239. 0x3A, 0x86, 0x5D, 0x6D, 0x21, 0x66, 0xF6, 0x6B,
  27240. 0xA5, 0x40, 0x1F, 0xB7, 0xA0, 0xE8, 0x53, 0x16,
  27241. 0x86, 0x00, 0xA2, 0x94, 0x84, 0x37, 0xE0, 0x36,
  27242. 0xE3, 0xBF, 0x19, 0xE1, 0x2F, 0xD3, 0xF2, 0xA2,
  27243. 0xB8, 0xB3, 0x43, 0xF7, 0x84, 0x24, 0x8E, 0x8D,
  27244. 0x68, 0x5E, 0xB0, 0xAF, 0xDE, 0x63, 0x15, 0x33,
  27245. 0x87, 0x30, 0xE7, 0xA1, 0x00, 0x1C, 0x27, 0xD8,
  27246. 0xD2, 0xA7, 0x6F, 0xA6, 0x9D, 0x15, 0x7B, 0xA1,
  27247. 0xAC, 0x7A, 0xD5, 0x6D, 0xA5, 0xA8, 0xC7, 0x0F,
  27248. 0xE4, 0xB5, 0xB8, 0xD7, 0x86, 0xDC, 0x6F, 0xC0,
  27249. 0x56, 0x6B, 0xA8, 0xE1, 0xB8, 0x81, 0x63, 0x34,
  27250. 0xD3, 0x2A, 0x3F, 0xB1, 0xCE, 0x7D, 0x4D, 0x5E,
  27251. 0x4C, 0x33, 0x2A, 0xF7, 0xB0, 0x03, 0xD0, 0x91,
  27252. 0x74, 0x1A, 0x3D, 0x5C, 0x96, 0x52, 0x92, 0x25,
  27253. 0x5D, 0xFF, 0x8E, 0xD2, 0xBB, 0xF1, 0xF9, 0x11,
  27254. 0x6B, 0xE5, 0x0C, 0x17, 0xB8, 0xE5, 0x48, 0x74,
  27255. 0x8A, 0xD4, 0xB2, 0xE9, 0x57, 0xBB, 0xD1, 0x95,
  27256. 0x34, 0x82, 0xA2, 0xE1, 0x71, 0x8C, 0xEC, 0x66,
  27257. 0xCD, 0x2C, 0x81, 0xF5, 0x72, 0xD5, 0x52, 0xB7,
  27258. 0x18, 0x78, 0x85, 0xE6, 0xB8, 0x94, 0x3D, 0x64,
  27259. 0x31, 0x41, 0x3C, 0x59, 0xEB, 0xB7, 0xE0, 0x36,
  27260. 0x04, 0x84, 0x90, 0xBE, 0x52, 0x89, 0xE9, 0x5B,
  27261. 0x20, 0xA8, 0x9E, 0x8B, 0x15, 0x9F, 0x61, 0xA9,
  27262. 0xA9, 0x88, 0x6E, 0x14, 0x75, 0x68, 0xF4, 0xC9,
  27263. 0x02, 0x1F, 0x36, 0x2F, 0x02, 0x68, 0x8A, 0x1C,
  27264. 0x8C, 0x3B, 0xB0, 0xD2, 0x40, 0x86, 0x88, 0x0E,
  27265. 0x55, 0xB6, 0xED, 0xB4, 0x3F, 0x37, 0x45, 0xD2,
  27266. 0xC1, 0x66, 0xDC, 0x1C, 0xB7, 0x43, 0xC7, 0x6F,
  27267. 0xE6, 0xBE, 0x52, 0x3A, 0x89, 0x3C, 0xC7, 0x64,
  27268. 0xD1, 0x64, 0x35, 0xC3, 0x78, 0x51, 0x25, 0x2A,
  27269. 0x81, 0xE2, 0xFF, 0xBA, 0x0F, 0x18, 0x97, 0x1A,
  27270. 0x3D, 0xEE, 0x37, 0xD4, 0x87, 0x7C, 0xB9, 0x28,
  27271. 0xE3, 0x6E, 0x52, 0x35, 0x03, 0x7A, 0x6B, 0x20,
  27272. 0x57, 0x89, 0x7D, 0x51, 0x8A, 0x5F, 0x0E, 0x34,
  27273. 0x8E, 0x3A, 0xB6, 0xD5, 0xB5, 0x2D, 0xFC, 0x60,
  27274. 0x75, 0x7F, 0x3B, 0x41, 0xA4, 0xFE, 0xC7, 0x82,
  27275. 0x8F, 0x1D, 0xEE, 0xAF, 0x45, 0x87, 0xCC, 0xC8,
  27276. 0xEA, 0xDF, 0x64, 0x7F, 0x4D, 0x20, 0x3B, 0x2F,
  27277. 0xAA, 0x05, 0xA6, 0x49, 0xB5, 0x82, 0x34, 0x0C,
  27278. 0xB4, 0xCA, 0xCE, 0x57, 0xA3, 0x07, 0x11, 0xBE,
  27279. 0x75, 0x2F, 0xAC, 0xF0, 0x22, 0x7D, 0x0A, 0x80,
  27280. 0xC4, 0x12, 0x84, 0x42, 0xDD, 0xC5, 0x44, 0xBE,
  27281. 0x80, 0x5B, 0x9C, 0xFE, 0x8F, 0xE9, 0xB1, 0x23,
  27282. 0x7C, 0x80, 0xF9, 0x67, 0x87, 0xCD, 0x92, 0x81,
  27283. 0xCC, 0xF2, 0x70, 0xC1, 0xAF, 0xC0, 0x67, 0x0D
  27284. };
  27285. const byte kyber512_ss[] = {
  27286. 0x0A, 0x69, 0x25, 0x67, 0x6F, 0x24, 0xB2, 0x2C,
  27287. 0x28, 0x6F, 0x4C, 0x81, 0xA4, 0x22, 0x4C, 0xEC,
  27288. 0x50, 0x6C, 0x9B, 0x25, 0x7D, 0x48, 0x0E, 0x02,
  27289. 0xE3, 0xB4, 0x9F, 0x44, 0xCA, 0xA3, 0x23, 0x7F
  27290. };
  27291. ret = wc_KyberKey_Init(KYBER512, &key, HEAP_HINT, INVALID_DEVID);
  27292. if (ret != 0)
  27293. return -20101;
  27294. ret = wc_KyberKey_MakeKeyWithRandom(&key, kyber512_rand,
  27295. sizeof(kyber512_rand));
  27296. if (ret != 0)
  27297. return -20102;
  27298. ret = wc_KyberKey_EncodePublicKey(&key, pub, sizeof(pub));
  27299. if (ret != 0)
  27300. return -20103;
  27301. ret = wc_KyberKey_EncodePrivateKey(&key, priv, sizeof(priv));
  27302. if (ret != 0)
  27303. return -20104;
  27304. if (XMEMCMP(pub, kyber512_pk, sizeof(kyber512_pk)) != 0)
  27305. return -20105;
  27306. if (XMEMCMP(priv, kyber512_sk, sizeof(kyber512_sk)) != 0)
  27307. return -20106;
  27308. ret = wc_KyberKey_EncapsulateWithRandom(&key, ct, ss, kyber512enc_rand,
  27309. sizeof(kyber512enc_rand));
  27310. if (ret != 0)
  27311. return -20107;
  27312. if (XMEMCMP(ct, kyber512_ct, sizeof(kyber512_ct)) != 0)
  27313. return -20108;
  27314. if (XMEMCMP(ss, kyber512_ss, sizeof(kyber512_ss)) != 0)
  27315. return -20109;
  27316. ret = wc_KyberKey_Decapsulate(&key, ss_dec, ct, sizeof(kyber512_ct));
  27317. if (ret != 0)
  27318. return -20110;
  27319. if (XMEMCMP(ss_dec, kyber512_ss, sizeof(kyber512_ss)) != 0)
  27320. return -20111;
  27321. wc_KyberKey_Free(&key);
  27322. return 0;
  27323. }
  27324. #endif /* WOLFSSL_KYBER512 */
  27325. #ifdef WOLFSSL_KYBER768
  27326. static int kyber768_kat(void)
  27327. {
  27328. KyberKey key;
  27329. int ret;
  27330. byte priv[KYBER768_PRIVATE_KEY_SIZE];
  27331. byte pub[KYBER768_PUBLIC_KEY_SIZE];
  27332. byte ct[KYBER768_CIPHER_TEXT_SIZE];
  27333. byte ss[KYBER_SS_SZ];
  27334. byte ss_dec[KYBER_SS_SZ];
  27335. const byte kyber768_rand[] = {
  27336. 0x7c, 0x99, 0x35, 0xa0, 0xb0, 0x76, 0x94, 0xaa,
  27337. 0x0c, 0x6d, 0x10, 0xe4, 0xdb, 0x6b, 0x1a, 0xdd,
  27338. 0x2f, 0xd8, 0x1a, 0x25, 0xcc, 0xb1, 0x48, 0x03,
  27339. 0x2d, 0xcd, 0x73, 0x99, 0x36, 0x73, 0x7f, 0x2d,
  27340. 0x86, 0x26, 0xED, 0x79, 0xD4, 0x51, 0x14, 0x08,
  27341. 0x00, 0xE0, 0x3B, 0x59, 0xB9, 0x56, 0xF8, 0x21,
  27342. 0x0E, 0x55, 0x60, 0x67, 0x40, 0x7D, 0x13, 0xDC,
  27343. 0x90, 0xFA, 0x9E, 0x8B, 0x87, 0x2B, 0xFB, 0x8F
  27344. };
  27345. const byte kyber768enc_rand[] = {
  27346. 0x14, 0x7c, 0x03, 0xf7, 0xa5, 0xbe, 0xbb, 0xa4,
  27347. 0x06, 0xc8, 0xfa, 0xe1, 0x87, 0x4d, 0x7f, 0x13,
  27348. 0xc8, 0x0e, 0xfe, 0x79, 0xa3, 0xa9, 0xa8, 0x74,
  27349. 0xcc, 0x09, 0xfe, 0x76, 0xf6, 0x99, 0x76, 0x15
  27350. };
  27351. const byte kyber768_pk[] = {
  27352. 0xA7, 0x2C, 0x2D, 0x9C, 0x84, 0x3E, 0xE9, 0xF8,
  27353. 0x31, 0x3E, 0xCC, 0x7F, 0x86, 0xD6, 0x29, 0x4D,
  27354. 0x59, 0x15, 0x9D, 0x9A, 0x87, 0x9A, 0x54, 0x2E,
  27355. 0x26, 0x09, 0x22, 0xAD, 0xF9, 0x99, 0x05, 0x1C,
  27356. 0xC4, 0x52, 0x00, 0xC9, 0xFF, 0xDB, 0x60, 0x44,
  27357. 0x9C, 0x49, 0x46, 0x59, 0x79, 0x27, 0x23, 0x67,
  27358. 0xC0, 0x83, 0xA7, 0xD6, 0x26, 0x7A, 0x3E, 0xD7,
  27359. 0xA7, 0xFD, 0x47, 0x95, 0x7C, 0x21, 0x93, 0x27,
  27360. 0xF7, 0xCA, 0x73, 0xA4, 0x00, 0x7E, 0x16, 0x27,
  27361. 0xF0, 0x0B, 0x11, 0xCC, 0x80, 0x57, 0x3C, 0x15,
  27362. 0xAE, 0xE6, 0x64, 0x0F, 0xB8, 0x56, 0x2D, 0xFA,
  27363. 0x6B, 0x24, 0x0C, 0xA0, 0xAD, 0x35, 0x1A, 0xC4,
  27364. 0xAC, 0x15, 0x5B, 0x96, 0xC1, 0x4C, 0x8A, 0xB1,
  27365. 0x3D, 0xD2, 0x62, 0xCD, 0xFD, 0x51, 0xC4, 0xBB,
  27366. 0x55, 0x72, 0xFD, 0x61, 0x65, 0x53, 0xD1, 0x7B,
  27367. 0xDD, 0x43, 0x0A, 0xCB, 0xEA, 0x3E, 0x95, 0xF0,
  27368. 0xB6, 0x98, 0xD6, 0x69, 0x90, 0xAB, 0x51, 0xE5,
  27369. 0xD0, 0x37, 0x83, 0xA8, 0xB3, 0xD2, 0x78, 0xA5,
  27370. 0x72, 0x04, 0x54, 0xCF, 0x96, 0x95, 0xCF, 0xDC,
  27371. 0xA0, 0x84, 0x85, 0xBA, 0x09, 0x9C, 0x51, 0xCD,
  27372. 0x92, 0xA7, 0xEA, 0x75, 0x87, 0xC1, 0xD1, 0x5C,
  27373. 0x28, 0xE6, 0x09, 0xA8, 0x18, 0x52, 0x60, 0x1B,
  27374. 0x06, 0x04, 0x01, 0x06, 0x79, 0xAA, 0x48, 0x2D,
  27375. 0x51, 0x26, 0x1E, 0xC3, 0x6E, 0x36, 0xB8, 0x71,
  27376. 0x96, 0x76, 0x21, 0x7F, 0xD7, 0x4C, 0x54, 0x78,
  27377. 0x64, 0x88, 0xF4, 0xB4, 0x96, 0x9C, 0x05, 0xA8,
  27378. 0xBA, 0x27, 0xCA, 0x3A, 0x77, 0xCC, 0xE7, 0x3B,
  27379. 0x96, 0x59, 0x23, 0xCA, 0x55, 0x4E, 0x42, 0x2B,
  27380. 0x9B, 0x61, 0xF4, 0x75, 0x46, 0x41, 0x60, 0x8A,
  27381. 0xC1, 0x6C, 0x9B, 0x85, 0x87, 0xA3, 0x2C, 0x1C,
  27382. 0x5D, 0xD7, 0x88, 0xF8, 0x8B, 0x36, 0xB7, 0x17,
  27383. 0xA4, 0x69, 0x65, 0x63, 0x5D, 0xEB, 0x67, 0xF4,
  27384. 0x5B, 0x12, 0x9B, 0x99, 0x07, 0x09, 0x09, 0xC9,
  27385. 0x3E, 0xB8, 0x0B, 0x42, 0xC2, 0xB3, 0xF3, 0xF7,
  27386. 0x03, 0x43, 0xA7, 0xCF, 0x37, 0xE8, 0x52, 0x0E,
  27387. 0x7B, 0xCF, 0xC4, 0x16, 0xAC, 0xA4, 0xF1, 0x8C,
  27388. 0x79, 0x81, 0x26, 0x2B, 0xA2, 0xBF, 0xC7, 0x56,
  27389. 0xAE, 0x03, 0x27, 0x8F, 0x0E, 0xC6, 0x6D, 0xC2,
  27390. 0x05, 0x76, 0x96, 0x82, 0x4B, 0xA6, 0x76, 0x98,
  27391. 0x65, 0xA6, 0x01, 0xD7, 0x14, 0x8E, 0xF6, 0xF5,
  27392. 0x4E, 0x5A, 0xF5, 0x68, 0x6A, 0xA2, 0x90, 0x6F,
  27393. 0x99, 0x4C, 0xE3, 0x8A, 0x5E, 0x0B, 0x93, 0x8F,
  27394. 0x23, 0x90, 0x07, 0x00, 0x30, 0x22, 0xC0, 0x33,
  27395. 0x92, 0xDF, 0x34, 0x01, 0xB1, 0xE4, 0xA3, 0xA7,
  27396. 0xEB, 0xC6, 0x16, 0x14, 0x49, 0xF7, 0x33, 0x74,
  27397. 0xC8, 0xB0, 0x14, 0x03, 0x69, 0x34, 0x3D, 0x92,
  27398. 0x95, 0xFD, 0xF5, 0x11, 0x84, 0x5C, 0x4A, 0x46,
  27399. 0xEB, 0xAA, 0xB6, 0xCA, 0x54, 0x92, 0xF6, 0x80,
  27400. 0x0B, 0x98, 0xC0, 0xCC, 0x80, 0x36, 0x53, 0xA4,
  27401. 0xB1, 0xD6, 0xE6, 0xAA, 0xED, 0x19, 0x32, 0xBA,
  27402. 0xCC, 0x5F, 0xEF, 0xAA, 0x81, 0x8B, 0xA5, 0x02,
  27403. 0x85, 0x9B, 0xA5, 0x49, 0x4C, 0x5F, 0x54, 0x02,
  27404. 0xC8, 0x53, 0x6A, 0x9C, 0x4C, 0x18, 0x88, 0x15,
  27405. 0x06, 0x17, 0xF8, 0x00, 0x98, 0xF6, 0xB2, 0xA9,
  27406. 0x9C, 0x39, 0xBC, 0x5D, 0xC7, 0xCF, 0x3B, 0x59,
  27407. 0x00, 0xA2, 0x13, 0x29, 0xAB, 0x59, 0x05, 0x3A,
  27408. 0xBA, 0xA6, 0x4E, 0xD1, 0x63, 0xE8, 0x59, 0xA8,
  27409. 0xB3, 0xB3, 0xCA, 0x33, 0x59, 0xB7, 0x50, 0xCC,
  27410. 0xC3, 0xE7, 0x10, 0xC7, 0xAC, 0x43, 0xC8, 0x19,
  27411. 0x1C, 0xB5, 0xD6, 0x88, 0x70, 0xC0, 0x63, 0x91,
  27412. 0xC0, 0xCB, 0x8A, 0xEC, 0x72, 0xB8, 0x97, 0xAC,
  27413. 0x6B, 0xE7, 0xFB, 0xAA, 0xCC, 0x67, 0x6E, 0xD6,
  27414. 0x63, 0x14, 0xC8, 0x36, 0x30, 0xE8, 0x94, 0x48,
  27415. 0xC8, 0x8A, 0x1D, 0xF0, 0x4A, 0xCE, 0xB2, 0x3A,
  27416. 0xBF, 0x2E, 0x40, 0x9E, 0xF3, 0x33, 0xC6, 0x22,
  27417. 0x28, 0x9C, 0x18, 0xA2, 0x13, 0x4E, 0x65, 0x0C,
  27418. 0x45, 0x25, 0x7E, 0x47, 0x47, 0x5F, 0xA3, 0x3A,
  27419. 0xA5, 0x37, 0xA5, 0xA8, 0xF7, 0x68, 0x02, 0x14,
  27420. 0x71, 0x6C, 0x50, 0xD4, 0x70, 0xE3, 0x28, 0x49,
  27421. 0x63, 0xCA, 0x64, 0xF5, 0x46, 0x77, 0xAE, 0xC5,
  27422. 0x4B, 0x52, 0x72, 0x16, 0x2B, 0xF5, 0x2B, 0xC8,
  27423. 0x14, 0x2E, 0x1D, 0x41, 0x83, 0xFC, 0x01, 0x74,
  27424. 0x54, 0xA6, 0xB5, 0xA4, 0x96, 0x83, 0x17, 0x59,
  27425. 0x06, 0x40, 0x24, 0x74, 0x59, 0x78, 0xCB, 0xD5,
  27426. 0x1A, 0x6C, 0xED, 0xC8, 0x95, 0x5D, 0xE4, 0xCC,
  27427. 0x6D, 0x36, 0x36, 0x70, 0xA4, 0x74, 0x66, 0xE8,
  27428. 0x2B, 0xE5, 0xC2, 0x36, 0x03, 0xA1, 0x7B, 0xF2,
  27429. 0x2A, 0xCD, 0xB7, 0xCC, 0x98, 0x4A, 0xF0, 0x8C,
  27430. 0x87, 0xE1, 0x4E, 0x27, 0x75, 0x3C, 0xF5, 0x87,
  27431. 0xA8, 0xEC, 0x34, 0x47, 0xE6, 0x2C, 0x64, 0x9E,
  27432. 0x88, 0x7A, 0x67, 0xC3, 0x6C, 0x9C, 0xE9, 0x87,
  27433. 0x21, 0xB6, 0x97, 0x21, 0x32, 0x75, 0x64, 0x6B,
  27434. 0x19, 0x4F, 0x36, 0x75, 0x86, 0x73, 0xA8, 0xED,
  27435. 0x11, 0x28, 0x44, 0x55, 0xAF, 0xC7, 0xA8, 0x52,
  27436. 0x9F, 0x69, 0xC9, 0x7A, 0x3C, 0x2D, 0x7B, 0x8C,
  27437. 0x63, 0x6C, 0x0B, 0xA5, 0x56, 0x14, 0xB7, 0x68,
  27438. 0xE6, 0x24, 0xE7, 0x12, 0x93, 0x0F, 0x77, 0x61,
  27439. 0x69, 0xB0, 0x17, 0x15, 0x72, 0x53, 0x51, 0xBC,
  27440. 0x74, 0xB4, 0x73, 0x95, 0xED, 0x52, 0xB2, 0x5A,
  27441. 0x13, 0x13, 0xC9, 0x51, 0x64, 0x81, 0x4C, 0x34,
  27442. 0xC9, 0x79, 0xCB, 0xDF, 0xAB, 0x85, 0x95, 0x46,
  27443. 0x62, 0xCA, 0xB4, 0x85, 0xE7, 0x50, 0x87, 0xA9,
  27444. 0x8C, 0xC7, 0x4B, 0xB8, 0x2C, 0xA2, 0xD1, 0xB5,
  27445. 0xBF, 0x28, 0x03, 0x23, 0x84, 0x80, 0x63, 0x8C,
  27446. 0x40, 0xE9, 0x0B, 0x43, 0xC7, 0x46, 0x0E, 0x7A,
  27447. 0xA9, 0x17, 0xF0, 0x10, 0x15, 0x1F, 0xAB, 0x11,
  27448. 0x69, 0x98, 0x7B, 0x37, 0x2A, 0xBB, 0x59, 0x27,
  27449. 0x1F, 0x70, 0x06, 0xC2, 0x4E, 0x60, 0x23, 0x6B,
  27450. 0x84, 0xB9, 0xDD, 0xD6, 0x00, 0x62, 0x37, 0x04,
  27451. 0x25, 0x46, 0x17, 0xFB, 0x49, 0x8D, 0x89, 0xE5,
  27452. 0x8B, 0x03, 0x68, 0xBC, 0xB2, 0x10, 0x3E, 0x79,
  27453. 0x35, 0x3E, 0xB5, 0x87, 0x86, 0x0C, 0x14, 0x22,
  27454. 0xE4, 0x76, 0x16, 0x2E, 0x42, 0x5B, 0xC2, 0x38,
  27455. 0x1D, 0xB8, 0x2C, 0x65, 0x92, 0x73, 0x7E, 0x1D,
  27456. 0xD6, 0x02, 0x86, 0x4B, 0x01, 0x67, 0xA7, 0x1E,
  27457. 0xC1, 0xF2, 0x23, 0x30, 0x5C, 0x02, 0xFE, 0x25,
  27458. 0x05, 0x2A, 0xF2, 0xB3, 0xB5, 0xA5, 0x5A, 0x0D,
  27459. 0x7A, 0x20, 0x22, 0xD9, 0xA7, 0x98, 0xDC, 0x0C,
  27460. 0x58, 0x74, 0xA9, 0x87, 0x02, 0xAA, 0xF4, 0x05,
  27461. 0x4C, 0x5D, 0x80, 0x33, 0x8A, 0x52, 0x48, 0xB5,
  27462. 0xB7, 0xBD, 0x09, 0xC5, 0x3B, 0x5E, 0x2A, 0x08,
  27463. 0x4B, 0x04, 0x7D, 0x27, 0x7A, 0x86, 0x1B, 0x1A,
  27464. 0x73, 0xBB, 0x51, 0x48, 0x8D, 0xE0, 0x4E, 0xF5,
  27465. 0x73, 0xC8, 0x52, 0x30, 0xA0, 0x47, 0x0B, 0x73,
  27466. 0x17, 0x5C, 0x9F, 0xA5, 0x05, 0x94, 0xF6, 0x6A,
  27467. 0x5F, 0x50, 0xB4, 0x15, 0x00, 0x54, 0xC9, 0x3B,
  27468. 0x68, 0x18, 0x6F, 0x8B, 0x5C, 0xBC, 0x49, 0x31,
  27469. 0x6C, 0x85, 0x48, 0xA6, 0x42, 0xB2, 0xB3, 0x6A,
  27470. 0x1D, 0x45, 0x4C, 0x74, 0x89, 0xAC, 0x33, 0xB2,
  27471. 0xD2, 0xCE, 0x66, 0x68, 0x09, 0x67, 0x82, 0xA2,
  27472. 0xC1, 0xE0, 0x86, 0x6D, 0x21, 0xA6, 0x5E, 0x16,
  27473. 0xB5, 0x85, 0xE7, 0xAF, 0x86, 0x18, 0xBD, 0xF3,
  27474. 0x18, 0x4C, 0x19, 0x86, 0x87, 0x85, 0x08, 0x91,
  27475. 0x72, 0x77, 0xB9, 0x3E, 0x10, 0x70, 0x6B, 0x16,
  27476. 0x14, 0x97, 0x2B, 0x2A, 0x94, 0xC7, 0x31, 0x0F,
  27477. 0xE9, 0xC7, 0x08, 0xC2, 0x31, 0xA1, 0xA8, 0xAC,
  27478. 0x8D, 0x93, 0x14, 0xA5, 0x29, 0xA9, 0x7F, 0x46,
  27479. 0x9B, 0xF6, 0x49, 0x62, 0xD8, 0x20, 0x64, 0x84,
  27480. 0x43, 0x09, 0x9A, 0x07, 0x6D, 0x55, 0xD4, 0xCE,
  27481. 0xA8, 0x24, 0xA5, 0x83, 0x04, 0x84, 0x4F, 0x99,
  27482. 0x49, 0x7C, 0x10, 0xA2, 0x51, 0x48, 0x61, 0x8A,
  27483. 0x31, 0x5D, 0x72, 0xCA, 0x85, 0x7D, 0x1B, 0x04,
  27484. 0xD5, 0x75, 0xB9, 0x4F, 0x85, 0xC0, 0x1D, 0x19,
  27485. 0xBE, 0xF2, 0x11, 0xBF, 0x0A, 0xA3, 0x36, 0x2E,
  27486. 0x70, 0x41, 0xFD, 0x16, 0x59, 0x6D, 0x80, 0x8E,
  27487. 0x86, 0x7B, 0x44, 0xC4, 0xC0, 0x0D, 0x1C, 0xDA,
  27488. 0x34, 0x18, 0x96, 0x77, 0x17, 0xF1, 0x47, 0xD0,
  27489. 0xEB, 0x21, 0xB4, 0x2A, 0xAE, 0xE7, 0x4A, 0xC3,
  27490. 0x5D, 0x0B, 0x92, 0x41, 0x4B, 0x95, 0x85, 0x31,
  27491. 0xAA, 0xDF, 0x46, 0x3E, 0xC6, 0x30, 0x5A, 0xE5,
  27492. 0xEC, 0xAF, 0x79, 0x17, 0x40, 0x02, 0xF2, 0x6D,
  27493. 0xDE, 0xCC, 0x81, 0x3B, 0xF3, 0x26, 0x72, 0xE8,
  27494. 0x52, 0x9D, 0x95, 0xA4, 0xE7, 0x30, 0xA7, 0xAB,
  27495. 0x4A, 0x3E, 0x8F, 0x8A, 0x8A, 0xF9, 0x79, 0xA6,
  27496. 0x65, 0xEA, 0xFD, 0x46, 0x5F, 0xC6, 0x4A, 0x0C,
  27497. 0x5F, 0x8F, 0x3F, 0x90, 0x03, 0x48, 0x94, 0x15,
  27498. 0x89, 0x9D, 0x59, 0xA5, 0x43, 0xD8, 0x20, 0x8C,
  27499. 0x54, 0xA3, 0x16, 0x65, 0x29, 0xB5, 0x39, 0x22
  27500. };
  27501. const byte kyber768_sk[] = {
  27502. 0x07, 0x63, 0x8F, 0xB6, 0x98, 0x68, 0xF3, 0xD3,
  27503. 0x20, 0xE5, 0x86, 0x2B, 0xD9, 0x69, 0x33, 0xFE,
  27504. 0xB3, 0x11, 0xB3, 0x62, 0x09, 0x3C, 0x9B, 0x5D,
  27505. 0x50, 0x17, 0x0B, 0xCE, 0xD4, 0x3F, 0x1B, 0x53,
  27506. 0x6D, 0x9A, 0x20, 0x4B, 0xB1, 0xF2, 0x26, 0x95,
  27507. 0x95, 0x0B, 0xA1, 0xF2, 0xA9, 0xE8, 0xEB, 0x82,
  27508. 0x8B, 0x28, 0x44, 0x88, 0x76, 0x0B, 0x3F, 0xC8,
  27509. 0x4F, 0xAB, 0xA0, 0x42, 0x75, 0xD5, 0x62, 0x8E,
  27510. 0x39, 0xC5, 0xB2, 0x47, 0x13, 0x74, 0x28, 0x3C,
  27511. 0x50, 0x32, 0x99, 0xC0, 0xAB, 0x49, 0xB6, 0x6B,
  27512. 0x8B, 0xBB, 0x56, 0xA4, 0x18, 0x66, 0x24, 0xF9,
  27513. 0x19, 0xA2, 0xBA, 0x59, 0xBB, 0x08, 0xD8, 0x55,
  27514. 0x18, 0x80, 0xC2, 0xBE, 0xFC, 0x4F, 0x87, 0xF2,
  27515. 0x5F, 0x59, 0xAB, 0x58, 0x7A, 0x79, 0xC3, 0x27,
  27516. 0xD7, 0x92, 0xD5, 0x4C, 0x97, 0x4A, 0x69, 0x26,
  27517. 0x2F, 0xF8, 0xA7, 0x89, 0x38, 0x28, 0x9E, 0x9A,
  27518. 0x87, 0xB6, 0x88, 0xB0, 0x83, 0xE0, 0x59, 0x5F,
  27519. 0xE2, 0x18, 0xB6, 0xBB, 0x15, 0x05, 0x94, 0x1C,
  27520. 0xE2, 0xE8, 0x1A, 0x5A, 0x64, 0xC5, 0xAA, 0xC6,
  27521. 0x04, 0x17, 0x25, 0x69, 0x85, 0x34, 0x9E, 0xE4,
  27522. 0x7A, 0x52, 0x42, 0x0A, 0x5F, 0x97, 0x47, 0x7B,
  27523. 0x72, 0x36, 0xAC, 0x76, 0xBC, 0x70, 0xE8, 0x28,
  27524. 0x87, 0x29, 0x28, 0x7E, 0xE3, 0xE3, 0x4A, 0x3D,
  27525. 0xBC, 0x36, 0x83, 0xC0, 0xB7, 0xB1, 0x00, 0x29,
  27526. 0xFC, 0x20, 0x34, 0x18, 0x53, 0x7E, 0x74, 0x66,
  27527. 0xBA, 0x63, 0x85, 0xA8, 0xFF, 0x30, 0x1E, 0xE1,
  27528. 0x27, 0x08, 0xF8, 0x2A, 0xAA, 0x1E, 0x38, 0x0F,
  27529. 0xC7, 0xA8, 0x8F, 0x8F, 0x20, 0x5A, 0xB7, 0xE8,
  27530. 0x8D, 0x7E, 0x95, 0x95, 0x2A, 0x55, 0xBA, 0x20,
  27531. 0xD0, 0x9B, 0x79, 0xA4, 0x71, 0x41, 0xD6, 0x2B,
  27532. 0xF6, 0xEB, 0x7D, 0xD3, 0x07, 0xB0, 0x8E, 0xCA,
  27533. 0x13, 0xA5, 0xBC, 0x5F, 0x6B, 0x68, 0x58, 0x1C,
  27534. 0x68, 0x65, 0xB2, 0x7B, 0xBC, 0xDD, 0xAB, 0x14,
  27535. 0x2F, 0x4B, 0x2C, 0xBF, 0xF4, 0x88, 0xC8, 0xA2,
  27536. 0x27, 0x05, 0xFA, 0xA9, 0x8A, 0x2B, 0x9E, 0xEA,
  27537. 0x35, 0x30, 0xC7, 0x66, 0x62, 0x33, 0x5C, 0xC7,
  27538. 0xEA, 0x3A, 0x00, 0x77, 0x77, 0x25, 0xEB, 0xCC,
  27539. 0xCD, 0x2A, 0x46, 0x36, 0xB2, 0xD9, 0x12, 0x2F,
  27540. 0xF3, 0xAB, 0x77, 0x12, 0x3C, 0xE0, 0x88, 0x3C,
  27541. 0x19, 0x11, 0x11, 0x5E, 0x50, 0xC9, 0xE8, 0xA9,
  27542. 0x41, 0x94, 0xE4, 0x8D, 0xD0, 0xD0, 0x9C, 0xFF,
  27543. 0xB3, 0xAD, 0xCD, 0x2C, 0x1E, 0x92, 0x43, 0x09,
  27544. 0x03, 0xD0, 0x7A, 0xDB, 0xF0, 0x05, 0x32, 0x03,
  27545. 0x15, 0x75, 0xAA, 0x7F, 0x9E, 0x7B, 0x5A, 0x1F,
  27546. 0x33, 0x62, 0xDE, 0xC9, 0x36, 0xD4, 0x04, 0x3C,
  27547. 0x05, 0xF2, 0x47, 0x6C, 0x07, 0x57, 0x8B, 0xC9,
  27548. 0xCB, 0xAF, 0x2A, 0xB4, 0xE3, 0x82, 0x72, 0x7A,
  27549. 0xD4, 0x16, 0x86, 0xA9, 0x6B, 0x25, 0x48, 0x82,
  27550. 0x0B, 0xB0, 0x3B, 0x32, 0xF1, 0x1B, 0x28, 0x11,
  27551. 0xAD, 0x62, 0xF4, 0x89, 0xE9, 0x51, 0x63, 0x2A,
  27552. 0xBA, 0x0D, 0x1D, 0xF8, 0x96, 0x80, 0xCC, 0x8A,
  27553. 0x8B, 0x53, 0xB4, 0x81, 0xD9, 0x2A, 0x68, 0xD7,
  27554. 0x0B, 0x4E, 0xA1, 0xC3, 0xA6, 0xA5, 0x61, 0xC0,
  27555. 0x69, 0x28, 0x82, 0xB5, 0xCA, 0x8C, 0xC9, 0x42,
  27556. 0xA8, 0xD4, 0x95, 0xAF, 0xCB, 0x06, 0xDE, 0x89,
  27557. 0x49, 0x8F, 0xB9, 0x35, 0xB7, 0x75, 0x90, 0x8F,
  27558. 0xE7, 0xA0, 0x3E, 0x32, 0x4D, 0x54, 0xCC, 0x19,
  27559. 0xD4, 0xE1, 0xAA, 0xBD, 0x35, 0x93, 0xB3, 0x8B,
  27560. 0x19, 0xEE, 0x13, 0x88, 0xFE, 0x49, 0x2B, 0x43,
  27561. 0x12, 0x7E, 0x5A, 0x50, 0x42, 0x53, 0x78, 0x6A,
  27562. 0x0D, 0x69, 0xAD, 0x32, 0x60, 0x1C, 0x28, 0xE2,
  27563. 0xC8, 0x85, 0x04, 0xA5, 0xBA, 0x59, 0x97, 0x06,
  27564. 0x02, 0x3A, 0x61, 0x36, 0x3E, 0x17, 0xC6, 0xB9,
  27565. 0xBB, 0x59, 0xBD, 0xC6, 0x97, 0x45, 0x2C, 0xD0,
  27566. 0x59, 0x45, 0x19, 0x83, 0xD7, 0x38, 0xCA, 0x3F,
  27567. 0xD0, 0x34, 0xE3, 0xF5, 0x98, 0x88, 0x54, 0xCA,
  27568. 0x05, 0x03, 0x1D, 0xB0, 0x96, 0x11, 0x49, 0x89,
  27569. 0x88, 0x19, 0x7C, 0x6B, 0x30, 0xD2, 0x58, 0xDF,
  27570. 0xE2, 0x62, 0x65, 0x54, 0x1C, 0x89, 0xA4, 0xB3,
  27571. 0x1D, 0x68, 0x64, 0xE9, 0x38, 0x9B, 0x03, 0xCB,
  27572. 0x74, 0xF7, 0xEC, 0x43, 0x23, 0xFB, 0x94, 0x21,
  27573. 0xA4, 0xB9, 0x79, 0x0A, 0x26, 0xD1, 0x7B, 0x03,
  27574. 0x98, 0xA2, 0x67, 0x67, 0x35, 0x09, 0x09, 0xF8,
  27575. 0x4D, 0x57, 0xB6, 0x69, 0x4D, 0xF8, 0x30, 0x66,
  27576. 0x4C, 0xA8, 0xB3, 0xC3, 0xC0, 0x3E, 0xD2, 0xAE,
  27577. 0x67, 0xB8, 0x90, 0x06, 0x86, 0x8A, 0x68, 0x52,
  27578. 0x7C, 0xCD, 0x66, 0x64, 0x59, 0xAB, 0x7F, 0x05,
  27579. 0x66, 0x71, 0x00, 0x0C, 0x61, 0x64, 0xD3, 0xA7,
  27580. 0xF2, 0x66, 0xA1, 0x4D, 0x97, 0xCB, 0xD7, 0x00,
  27581. 0x4D, 0x6C, 0x92, 0xCA, 0xCA, 0x77, 0x0B, 0x84,
  27582. 0x4A, 0x4F, 0xA9, 0xB1, 0x82, 0xE7, 0xB1, 0x8C,
  27583. 0xA8, 0x85, 0x08, 0x2A, 0xC5, 0x64, 0x6F, 0xCB,
  27584. 0x4A, 0x14, 0xE1, 0x68, 0x5F, 0xEB, 0x0C, 0x9C,
  27585. 0xE3, 0x37, 0x2A, 0xB9, 0x53, 0x65, 0xC0, 0x4F,
  27586. 0xD8, 0x30, 0x84, 0xF8, 0x0A, 0x23, 0xFF, 0x10,
  27587. 0xA0, 0x5B, 0xF1, 0x5F, 0x7F, 0xA5, 0xAC, 0xC6,
  27588. 0xC0, 0xCB, 0x46, 0x2C, 0x33, 0xCA, 0x52, 0x4F,
  27589. 0xA6, 0xB8, 0xBB, 0x35, 0x90, 0x43, 0xBA, 0x68,
  27590. 0x60, 0x9E, 0xAA, 0x25, 0x36, 0xE8, 0x1D, 0x08,
  27591. 0x46, 0x3B, 0x19, 0x65, 0x3B, 0x54, 0x35, 0xBA,
  27592. 0x94, 0x6C, 0x9A, 0xDD, 0xEB, 0x20, 0x2B, 0x04,
  27593. 0xB0, 0x31, 0xCC, 0x96, 0x0D, 0xCC, 0x12, 0xE4,
  27594. 0x51, 0x8D, 0x42, 0x8B, 0x32, 0xB2, 0x57, 0xA4,
  27595. 0xFC, 0x73, 0x13, 0xD3, 0xA7, 0x98, 0x0D, 0x80,
  27596. 0x08, 0x2E, 0x93, 0x4F, 0x9D, 0x95, 0xC3, 0x2B,
  27597. 0x0A, 0x01, 0x91, 0xA2, 0x36, 0x04, 0x38, 0x4D,
  27598. 0xD9, 0xE0, 0x79, 0xBB, 0xBA, 0xA2, 0x66, 0xD1,
  27599. 0x4C, 0x3F, 0x75, 0x6B, 0x9F, 0x21, 0x33, 0x10,
  27600. 0x74, 0x33, 0xA4, 0xE8, 0x3F, 0xA7, 0x18, 0x72,
  27601. 0x82, 0xA8, 0x09, 0x20, 0x3A, 0x4F, 0xAF, 0x84,
  27602. 0x18, 0x51, 0x83, 0x3D, 0x12, 0x1A, 0xC3, 0x83,
  27603. 0x84, 0x3A, 0x5E, 0x55, 0xBC, 0x23, 0x81, 0x42,
  27604. 0x5E, 0x16, 0xC7, 0xDB, 0x4C, 0xC9, 0xAB, 0x5C,
  27605. 0x1B, 0x0D, 0x91, 0xA4, 0x7E, 0x2B, 0x8D, 0xE0,
  27606. 0xE5, 0x82, 0xC8, 0x6B, 0x6B, 0x0D, 0x90, 0x7B,
  27607. 0xB3, 0x60, 0xB9, 0x7F, 0x40, 0xAB, 0x5D, 0x03,
  27608. 0x8F, 0x6B, 0x75, 0xC8, 0x14, 0xB2, 0x7D, 0x9B,
  27609. 0x96, 0x8D, 0x41, 0x98, 0x32, 0xBC, 0x8C, 0x2B,
  27610. 0xEE, 0x60, 0x5E, 0xF6, 0xE5, 0x05, 0x9D, 0x33,
  27611. 0x10, 0x0D, 0x90, 0x48, 0x5D, 0x37, 0x84, 0x50,
  27612. 0x01, 0x42, 0x21, 0x73, 0x6C, 0x07, 0x40, 0x7C,
  27613. 0xAC, 0x26, 0x04, 0x08, 0xAA, 0x64, 0x92, 0x66,
  27614. 0x19, 0x78, 0x8B, 0x86, 0x01, 0xC2, 0xA7, 0x52,
  27615. 0xD1, 0xA6, 0xCB, 0xF8, 0x20, 0xD7, 0xC7, 0xA0,
  27616. 0x47, 0x16, 0x20, 0x32, 0x25, 0xB3, 0x89, 0x5B,
  27617. 0x93, 0x42, 0xD1, 0x47, 0xA8, 0x18, 0x5C, 0xFC,
  27618. 0x1B, 0xB6, 0x5B, 0xA0, 0x6B, 0x41, 0x42, 0x33,
  27619. 0x99, 0x03, 0xC0, 0xAC, 0x46, 0x51, 0x38, 0x5B,
  27620. 0x45, 0xD9, 0x8A, 0x8B, 0x19, 0xD2, 0x8C, 0xD6,
  27621. 0xBA, 0xB0, 0x88, 0x78, 0x7F, 0x7E, 0xE1, 0xB1,
  27622. 0x24, 0x61, 0x76, 0x6B, 0x43, 0xCB, 0xCC, 0xB9,
  27623. 0x64, 0x34, 0x42, 0x7D, 0x93, 0xC0, 0x65, 0x55,
  27624. 0x06, 0x88, 0xF6, 0x94, 0x8E, 0xD1, 0xB5, 0x47,
  27625. 0x5A, 0x42, 0x5F, 0x1B, 0x85, 0x20, 0x9D, 0x06,
  27626. 0x1C, 0x08, 0xB5, 0x6C, 0x1C, 0xC0, 0x69, 0xF6,
  27627. 0xC0, 0xA7, 0xC6, 0xF2, 0x93, 0x58, 0xCA, 0xB9,
  27628. 0x11, 0x08, 0x77, 0x32, 0xA6, 0x49, 0xD2, 0x7C,
  27629. 0x9B, 0x98, 0xF9, 0xA4, 0x88, 0x79, 0x38, 0x7D,
  27630. 0x9B, 0x00, 0xC2, 0x59, 0x59, 0xA7, 0x16, 0x54,
  27631. 0xD6, 0xF6, 0xA9, 0x46, 0x16, 0x45, 0x13, 0xE4,
  27632. 0x7A, 0x75, 0xD0, 0x05, 0x98, 0x6C, 0x23, 0x63,
  27633. 0xC0, 0x9F, 0x6B, 0x53, 0x7E, 0xCA, 0x78, 0xB9,
  27634. 0x30, 0x3A, 0x5F, 0xA4, 0x57, 0x60, 0x8A, 0x58,
  27635. 0x6A, 0x65, 0x3A, 0x34, 0x7D, 0xB0, 0x4D, 0xFC,
  27636. 0xC1, 0x91, 0x75, 0xB3, 0xA3, 0x01, 0x17, 0x25,
  27637. 0x36, 0x06, 0x2A, 0x65, 0x8A, 0x95, 0x27, 0x75,
  27638. 0x70, 0xC8, 0x85, 0x2C, 0xA8, 0x97, 0x3F, 0x4A,
  27639. 0xE1, 0x23, 0xA3, 0x34, 0x04, 0x7D, 0xD7, 0x11,
  27640. 0xC8, 0x92, 0x7A, 0x63, 0x4A, 0x03, 0x38, 0x8A,
  27641. 0x52, 0x7B, 0x03, 0x4B, 0xF7, 0xA8, 0x17, 0x0F,
  27642. 0xA7, 0x02, 0xC1, 0xF7, 0xC2, 0x3E, 0xC3, 0x2D,
  27643. 0x18, 0xA2, 0x37, 0x48, 0x90, 0xBE, 0x9C, 0x78,
  27644. 0x7A, 0x94, 0x09, 0xC8, 0x2D, 0x19, 0x2C, 0x4B,
  27645. 0xB7, 0x05, 0xA2, 0xF9, 0x96, 0xCE, 0x40, 0x5D,
  27646. 0xA7, 0x2C, 0x2D, 0x9C, 0x84, 0x3E, 0xE9, 0xF8,
  27647. 0x31, 0x3E, 0xCC, 0x7F, 0x86, 0xD6, 0x29, 0x4D,
  27648. 0x59, 0x15, 0x9D, 0x9A, 0x87, 0x9A, 0x54, 0x2E,
  27649. 0x26, 0x09, 0x22, 0xAD, 0xF9, 0x99, 0x05, 0x1C,
  27650. 0xC4, 0x52, 0x00, 0xC9, 0xFF, 0xDB, 0x60, 0x44,
  27651. 0x9C, 0x49, 0x46, 0x59, 0x79, 0x27, 0x23, 0x67,
  27652. 0xC0, 0x83, 0xA7, 0xD6, 0x26, 0x7A, 0x3E, 0xD7,
  27653. 0xA7, 0xFD, 0x47, 0x95, 0x7C, 0x21, 0x93, 0x27,
  27654. 0xF7, 0xCA, 0x73, 0xA4, 0x00, 0x7E, 0x16, 0x27,
  27655. 0xF0, 0x0B, 0x11, 0xCC, 0x80, 0x57, 0x3C, 0x15,
  27656. 0xAE, 0xE6, 0x64, 0x0F, 0xB8, 0x56, 0x2D, 0xFA,
  27657. 0x6B, 0x24, 0x0C, 0xA0, 0xAD, 0x35, 0x1A, 0xC4,
  27658. 0xAC, 0x15, 0x5B, 0x96, 0xC1, 0x4C, 0x8A, 0xB1,
  27659. 0x3D, 0xD2, 0x62, 0xCD, 0xFD, 0x51, 0xC4, 0xBB,
  27660. 0x55, 0x72, 0xFD, 0x61, 0x65, 0x53, 0xD1, 0x7B,
  27661. 0xDD, 0x43, 0x0A, 0xCB, 0xEA, 0x3E, 0x95, 0xF0,
  27662. 0xB6, 0x98, 0xD6, 0x69, 0x90, 0xAB, 0x51, 0xE5,
  27663. 0xD0, 0x37, 0x83, 0xA8, 0xB3, 0xD2, 0x78, 0xA5,
  27664. 0x72, 0x04, 0x54, 0xCF, 0x96, 0x95, 0xCF, 0xDC,
  27665. 0xA0, 0x84, 0x85, 0xBA, 0x09, 0x9C, 0x51, 0xCD,
  27666. 0x92, 0xA7, 0xEA, 0x75, 0x87, 0xC1, 0xD1, 0x5C,
  27667. 0x28, 0xE6, 0x09, 0xA8, 0x18, 0x52, 0x60, 0x1B,
  27668. 0x06, 0x04, 0x01, 0x06, 0x79, 0xAA, 0x48, 0x2D,
  27669. 0x51, 0x26, 0x1E, 0xC3, 0x6E, 0x36, 0xB8, 0x71,
  27670. 0x96, 0x76, 0x21, 0x7F, 0xD7, 0x4C, 0x54, 0x78,
  27671. 0x64, 0x88, 0xF4, 0xB4, 0x96, 0x9C, 0x05, 0xA8,
  27672. 0xBA, 0x27, 0xCA, 0x3A, 0x77, 0xCC, 0xE7, 0x3B,
  27673. 0x96, 0x59, 0x23, 0xCA, 0x55, 0x4E, 0x42, 0x2B,
  27674. 0x9B, 0x61, 0xF4, 0x75, 0x46, 0x41, 0x60, 0x8A,
  27675. 0xC1, 0x6C, 0x9B, 0x85, 0x87, 0xA3, 0x2C, 0x1C,
  27676. 0x5D, 0xD7, 0x88, 0xF8, 0x8B, 0x36, 0xB7, 0x17,
  27677. 0xA4, 0x69, 0x65, 0x63, 0x5D, 0xEB, 0x67, 0xF4,
  27678. 0x5B, 0x12, 0x9B, 0x99, 0x07, 0x09, 0x09, 0xC9,
  27679. 0x3E, 0xB8, 0x0B, 0x42, 0xC2, 0xB3, 0xF3, 0xF7,
  27680. 0x03, 0x43, 0xA7, 0xCF, 0x37, 0xE8, 0x52, 0x0E,
  27681. 0x7B, 0xCF, 0xC4, 0x16, 0xAC, 0xA4, 0xF1, 0x8C,
  27682. 0x79, 0x81, 0x26, 0x2B, 0xA2, 0xBF, 0xC7, 0x56,
  27683. 0xAE, 0x03, 0x27, 0x8F, 0x0E, 0xC6, 0x6D, 0xC2,
  27684. 0x05, 0x76, 0x96, 0x82, 0x4B, 0xA6, 0x76, 0x98,
  27685. 0x65, 0xA6, 0x01, 0xD7, 0x14, 0x8E, 0xF6, 0xF5,
  27686. 0x4E, 0x5A, 0xF5, 0x68, 0x6A, 0xA2, 0x90, 0x6F,
  27687. 0x99, 0x4C, 0xE3, 0x8A, 0x5E, 0x0B, 0x93, 0x8F,
  27688. 0x23, 0x90, 0x07, 0x00, 0x30, 0x22, 0xC0, 0x33,
  27689. 0x92, 0xDF, 0x34, 0x01, 0xB1, 0xE4, 0xA3, 0xA7,
  27690. 0xEB, 0xC6, 0x16, 0x14, 0x49, 0xF7, 0x33, 0x74,
  27691. 0xC8, 0xB0, 0x14, 0x03, 0x69, 0x34, 0x3D, 0x92,
  27692. 0x95, 0xFD, 0xF5, 0x11, 0x84, 0x5C, 0x4A, 0x46,
  27693. 0xEB, 0xAA, 0xB6, 0xCA, 0x54, 0x92, 0xF6, 0x80,
  27694. 0x0B, 0x98, 0xC0, 0xCC, 0x80, 0x36, 0x53, 0xA4,
  27695. 0xB1, 0xD6, 0xE6, 0xAA, 0xED, 0x19, 0x32, 0xBA,
  27696. 0xCC, 0x5F, 0xEF, 0xAA, 0x81, 0x8B, 0xA5, 0x02,
  27697. 0x85, 0x9B, 0xA5, 0x49, 0x4C, 0x5F, 0x54, 0x02,
  27698. 0xC8, 0x53, 0x6A, 0x9C, 0x4C, 0x18, 0x88, 0x15,
  27699. 0x06, 0x17, 0xF8, 0x00, 0x98, 0xF6, 0xB2, 0xA9,
  27700. 0x9C, 0x39, 0xBC, 0x5D, 0xC7, 0xCF, 0x3B, 0x59,
  27701. 0x00, 0xA2, 0x13, 0x29, 0xAB, 0x59, 0x05, 0x3A,
  27702. 0xBA, 0xA6, 0x4E, 0xD1, 0x63, 0xE8, 0x59, 0xA8,
  27703. 0xB3, 0xB3, 0xCA, 0x33, 0x59, 0xB7, 0x50, 0xCC,
  27704. 0xC3, 0xE7, 0x10, 0xC7, 0xAC, 0x43, 0xC8, 0x19,
  27705. 0x1C, 0xB5, 0xD6, 0x88, 0x70, 0xC0, 0x63, 0x91,
  27706. 0xC0, 0xCB, 0x8A, 0xEC, 0x72, 0xB8, 0x97, 0xAC,
  27707. 0x6B, 0xE7, 0xFB, 0xAA, 0xCC, 0x67, 0x6E, 0xD6,
  27708. 0x63, 0x14, 0xC8, 0x36, 0x30, 0xE8, 0x94, 0x48,
  27709. 0xC8, 0x8A, 0x1D, 0xF0, 0x4A, 0xCE, 0xB2, 0x3A,
  27710. 0xBF, 0x2E, 0x40, 0x9E, 0xF3, 0x33, 0xC6, 0x22,
  27711. 0x28, 0x9C, 0x18, 0xA2, 0x13, 0x4E, 0x65, 0x0C,
  27712. 0x45, 0x25, 0x7E, 0x47, 0x47, 0x5F, 0xA3, 0x3A,
  27713. 0xA5, 0x37, 0xA5, 0xA8, 0xF7, 0x68, 0x02, 0x14,
  27714. 0x71, 0x6C, 0x50, 0xD4, 0x70, 0xE3, 0x28, 0x49,
  27715. 0x63, 0xCA, 0x64, 0xF5, 0x46, 0x77, 0xAE, 0xC5,
  27716. 0x4B, 0x52, 0x72, 0x16, 0x2B, 0xF5, 0x2B, 0xC8,
  27717. 0x14, 0x2E, 0x1D, 0x41, 0x83, 0xFC, 0x01, 0x74,
  27718. 0x54, 0xA6, 0xB5, 0xA4, 0x96, 0x83, 0x17, 0x59,
  27719. 0x06, 0x40, 0x24, 0x74, 0x59, 0x78, 0xCB, 0xD5,
  27720. 0x1A, 0x6C, 0xED, 0xC8, 0x95, 0x5D, 0xE4, 0xCC,
  27721. 0x6D, 0x36, 0x36, 0x70, 0xA4, 0x74, 0x66, 0xE8,
  27722. 0x2B, 0xE5, 0xC2, 0x36, 0x03, 0xA1, 0x7B, 0xF2,
  27723. 0x2A, 0xCD, 0xB7, 0xCC, 0x98, 0x4A, 0xF0, 0x8C,
  27724. 0x87, 0xE1, 0x4E, 0x27, 0x75, 0x3C, 0xF5, 0x87,
  27725. 0xA8, 0xEC, 0x34, 0x47, 0xE6, 0x2C, 0x64, 0x9E,
  27726. 0x88, 0x7A, 0x67, 0xC3, 0x6C, 0x9C, 0xE9, 0x87,
  27727. 0x21, 0xB6, 0x97, 0x21, 0x32, 0x75, 0x64, 0x6B,
  27728. 0x19, 0x4F, 0x36, 0x75, 0x86, 0x73, 0xA8, 0xED,
  27729. 0x11, 0x28, 0x44, 0x55, 0xAF, 0xC7, 0xA8, 0x52,
  27730. 0x9F, 0x69, 0xC9, 0x7A, 0x3C, 0x2D, 0x7B, 0x8C,
  27731. 0x63, 0x6C, 0x0B, 0xA5, 0x56, 0x14, 0xB7, 0x68,
  27732. 0xE6, 0x24, 0xE7, 0x12, 0x93, 0x0F, 0x77, 0x61,
  27733. 0x69, 0xB0, 0x17, 0x15, 0x72, 0x53, 0x51, 0xBC,
  27734. 0x74, 0xB4, 0x73, 0x95, 0xED, 0x52, 0xB2, 0x5A,
  27735. 0x13, 0x13, 0xC9, 0x51, 0x64, 0x81, 0x4C, 0x34,
  27736. 0xC9, 0x79, 0xCB, 0xDF, 0xAB, 0x85, 0x95, 0x46,
  27737. 0x62, 0xCA, 0xB4, 0x85, 0xE7, 0x50, 0x87, 0xA9,
  27738. 0x8C, 0xC7, 0x4B, 0xB8, 0x2C, 0xA2, 0xD1, 0xB5,
  27739. 0xBF, 0x28, 0x03, 0x23, 0x84, 0x80, 0x63, 0x8C,
  27740. 0x40, 0xE9, 0x0B, 0x43, 0xC7, 0x46, 0x0E, 0x7A,
  27741. 0xA9, 0x17, 0xF0, 0x10, 0x15, 0x1F, 0xAB, 0x11,
  27742. 0x69, 0x98, 0x7B, 0x37, 0x2A, 0xBB, 0x59, 0x27,
  27743. 0x1F, 0x70, 0x06, 0xC2, 0x4E, 0x60, 0x23, 0x6B,
  27744. 0x84, 0xB9, 0xDD, 0xD6, 0x00, 0x62, 0x37, 0x04,
  27745. 0x25, 0x46, 0x17, 0xFB, 0x49, 0x8D, 0x89, 0xE5,
  27746. 0x8B, 0x03, 0x68, 0xBC, 0xB2, 0x10, 0x3E, 0x79,
  27747. 0x35, 0x3E, 0xB5, 0x87, 0x86, 0x0C, 0x14, 0x22,
  27748. 0xE4, 0x76, 0x16, 0x2E, 0x42, 0x5B, 0xC2, 0x38,
  27749. 0x1D, 0xB8, 0x2C, 0x65, 0x92, 0x73, 0x7E, 0x1D,
  27750. 0xD6, 0x02, 0x86, 0x4B, 0x01, 0x67, 0xA7, 0x1E,
  27751. 0xC1, 0xF2, 0x23, 0x30, 0x5C, 0x02, 0xFE, 0x25,
  27752. 0x05, 0x2A, 0xF2, 0xB3, 0xB5, 0xA5, 0x5A, 0x0D,
  27753. 0x7A, 0x20, 0x22, 0xD9, 0xA7, 0x98, 0xDC, 0x0C,
  27754. 0x58, 0x74, 0xA9, 0x87, 0x02, 0xAA, 0xF4, 0x05,
  27755. 0x4C, 0x5D, 0x80, 0x33, 0x8A, 0x52, 0x48, 0xB5,
  27756. 0xB7, 0xBD, 0x09, 0xC5, 0x3B, 0x5E, 0x2A, 0x08,
  27757. 0x4B, 0x04, 0x7D, 0x27, 0x7A, 0x86, 0x1B, 0x1A,
  27758. 0x73, 0xBB, 0x51, 0x48, 0x8D, 0xE0, 0x4E, 0xF5,
  27759. 0x73, 0xC8, 0x52, 0x30, 0xA0, 0x47, 0x0B, 0x73,
  27760. 0x17, 0x5C, 0x9F, 0xA5, 0x05, 0x94, 0xF6, 0x6A,
  27761. 0x5F, 0x50, 0xB4, 0x15, 0x00, 0x54, 0xC9, 0x3B,
  27762. 0x68, 0x18, 0x6F, 0x8B, 0x5C, 0xBC, 0x49, 0x31,
  27763. 0x6C, 0x85, 0x48, 0xA6, 0x42, 0xB2, 0xB3, 0x6A,
  27764. 0x1D, 0x45, 0x4C, 0x74, 0x89, 0xAC, 0x33, 0xB2,
  27765. 0xD2, 0xCE, 0x66, 0x68, 0x09, 0x67, 0x82, 0xA2,
  27766. 0xC1, 0xE0, 0x86, 0x6D, 0x21, 0xA6, 0x5E, 0x16,
  27767. 0xB5, 0x85, 0xE7, 0xAF, 0x86, 0x18, 0xBD, 0xF3,
  27768. 0x18, 0x4C, 0x19, 0x86, 0x87, 0x85, 0x08, 0x91,
  27769. 0x72, 0x77, 0xB9, 0x3E, 0x10, 0x70, 0x6B, 0x16,
  27770. 0x14, 0x97, 0x2B, 0x2A, 0x94, 0xC7, 0x31, 0x0F,
  27771. 0xE9, 0xC7, 0x08, 0xC2, 0x31, 0xA1, 0xA8, 0xAC,
  27772. 0x8D, 0x93, 0x14, 0xA5, 0x29, 0xA9, 0x7F, 0x46,
  27773. 0x9B, 0xF6, 0x49, 0x62, 0xD8, 0x20, 0x64, 0x84,
  27774. 0x43, 0x09, 0x9A, 0x07, 0x6D, 0x55, 0xD4, 0xCE,
  27775. 0xA8, 0x24, 0xA5, 0x83, 0x04, 0x84, 0x4F, 0x99,
  27776. 0x49, 0x7C, 0x10, 0xA2, 0x51, 0x48, 0x61, 0x8A,
  27777. 0x31, 0x5D, 0x72, 0xCA, 0x85, 0x7D, 0x1B, 0x04,
  27778. 0xD5, 0x75, 0xB9, 0x4F, 0x85, 0xC0, 0x1D, 0x19,
  27779. 0xBE, 0xF2, 0x11, 0xBF, 0x0A, 0xA3, 0x36, 0x2E,
  27780. 0x70, 0x41, 0xFD, 0x16, 0x59, 0x6D, 0x80, 0x8E,
  27781. 0x86, 0x7B, 0x44, 0xC4, 0xC0, 0x0D, 0x1C, 0xDA,
  27782. 0x34, 0x18, 0x96, 0x77, 0x17, 0xF1, 0x47, 0xD0,
  27783. 0xEB, 0x21, 0xB4, 0x2A, 0xAE, 0xE7, 0x4A, 0xC3,
  27784. 0x5D, 0x0B, 0x92, 0x41, 0x4B, 0x95, 0x85, 0x31,
  27785. 0xAA, 0xDF, 0x46, 0x3E, 0xC6, 0x30, 0x5A, 0xE5,
  27786. 0xEC, 0xAF, 0x79, 0x17, 0x40, 0x02, 0xF2, 0x6D,
  27787. 0xDE, 0xCC, 0x81, 0x3B, 0xF3, 0x26, 0x72, 0xE8,
  27788. 0x52, 0x9D, 0x95, 0xA4, 0xE7, 0x30, 0xA7, 0xAB,
  27789. 0x4A, 0x3E, 0x8F, 0x8A, 0x8A, 0xF9, 0x79, 0xA6,
  27790. 0x65, 0xEA, 0xFD, 0x46, 0x5F, 0xC6, 0x4A, 0x0C,
  27791. 0x5F, 0x8F, 0x3F, 0x90, 0x03, 0x48, 0x94, 0x15,
  27792. 0x89, 0x9D, 0x59, 0xA5, 0x43, 0xD8, 0x20, 0x8C,
  27793. 0x54, 0xA3, 0x16, 0x65, 0x29, 0xB5, 0x39, 0x22,
  27794. 0xD4, 0xEC, 0x14, 0x3B, 0x50, 0xF0, 0x14, 0x23,
  27795. 0xB1, 0x77, 0x89, 0x5E, 0xDE, 0xE2, 0x2B, 0xB7,
  27796. 0x39, 0xF6, 0x47, 0xEC, 0xF8, 0x5F, 0x50, 0xBC,
  27797. 0x25, 0xEF, 0x7B, 0x5A, 0x72, 0x5D, 0xEE, 0x86,
  27798. 0x86, 0x26, 0xED, 0x79, 0xD4, 0x51, 0x14, 0x08,
  27799. 0x00, 0xE0, 0x3B, 0x59, 0xB9, 0x56, 0xF8, 0x21,
  27800. 0x0E, 0x55, 0x60, 0x67, 0x40, 0x7D, 0x13, 0xDC,
  27801. 0x90, 0xFA, 0x9E, 0x8B, 0x87, 0x2B, 0xFB, 0x8F
  27802. };
  27803. const byte kyber768_ct[] = {
  27804. 0xB5, 0x2C, 0x56, 0xB9, 0x2A, 0x4B, 0x7C, 0xE9,
  27805. 0xE4, 0xCB, 0x7C, 0x5B, 0x1B, 0x16, 0x31, 0x67,
  27806. 0xA8, 0xA1, 0x67, 0x5B, 0x2F, 0xDE, 0xF8, 0x4A,
  27807. 0x5B, 0x67, 0xCA, 0x15, 0xDB, 0x69, 0x4C, 0x9F,
  27808. 0x11, 0xBD, 0x02, 0x7C, 0x30, 0xAE, 0x22, 0xEC,
  27809. 0x92, 0x1A, 0x1D, 0x91, 0x15, 0x99, 0xAF, 0x05,
  27810. 0x85, 0xE4, 0x8D, 0x20, 0xDA, 0x70, 0xDF, 0x9F,
  27811. 0x39, 0xE3, 0x2E, 0xF9, 0x5D, 0x4C, 0x8F, 0x44,
  27812. 0xBF, 0xEF, 0xDA, 0xA5, 0xDA, 0x64, 0xF1, 0x05,
  27813. 0x46, 0x31, 0xD0, 0x4D, 0x6D, 0x3C, 0xFD, 0x0A,
  27814. 0x54, 0x0D, 0xD7, 0xBA, 0x38, 0x86, 0xE4, 0xB5,
  27815. 0xF1, 0x3E, 0x87, 0x87, 0x88, 0x60, 0x4C, 0x95,
  27816. 0xC0, 0x96, 0xEA, 0xB3, 0x91, 0x9F, 0x42, 0x75,
  27817. 0x21, 0x41, 0x9A, 0x94, 0x6C, 0x26, 0xCC, 0x04,
  27818. 0x14, 0x75, 0xD7, 0x12, 0x4C, 0xDC, 0x01, 0xD0,
  27819. 0x37, 0x3E, 0x5B, 0x09, 0xC7, 0xA7, 0x06, 0x03,
  27820. 0xCF, 0xDB, 0x4F, 0xB3, 0x40, 0x50, 0x23, 0xF2,
  27821. 0x26, 0x4D, 0xC3, 0xF9, 0x83, 0xC4, 0xFC, 0x02,
  27822. 0xA2, 0xD1, 0xB2, 0x68, 0xF2, 0x20, 0x8A, 0x1F,
  27823. 0x6E, 0x2A, 0x62, 0x09, 0xBF, 0xF1, 0x2F, 0x6F,
  27824. 0x46, 0x5F, 0x0B, 0x06, 0x9C, 0x3A, 0x7F, 0x84,
  27825. 0xF6, 0x06, 0xD8, 0xA9, 0x40, 0x64, 0x00, 0x3D,
  27826. 0x6E, 0xC1, 0x14, 0xC8, 0xE8, 0x08, 0xD3, 0x05,
  27827. 0x38, 0x84, 0xC1, 0xD5, 0xA1, 0x42, 0xFB, 0xF2,
  27828. 0x01, 0x12, 0xEB, 0x36, 0x0F, 0xDA, 0x3F, 0x0F,
  27829. 0x28, 0xB1, 0x72, 0xAE, 0x50, 0xF5, 0xE7, 0xD8,
  27830. 0x38, 0x01, 0xFB, 0x3F, 0x00, 0x64, 0xB6, 0x87,
  27831. 0x18, 0x70, 0x74, 0xBD, 0x7F, 0xE3, 0x0E, 0xDD,
  27832. 0xAA, 0x33, 0x4C, 0xF8, 0xFC, 0x04, 0xFA, 0x8C,
  27833. 0xED, 0x89, 0x9C, 0xEA, 0xDE, 0x4B, 0x4F, 0x28,
  27834. 0xB6, 0x83, 0x72, 0xBA, 0xF9, 0x8F, 0xF4, 0x82,
  27835. 0xA4, 0x15, 0xB7, 0x31, 0x15, 0x5B, 0x75, 0xCE,
  27836. 0xB9, 0x76, 0xBE, 0x0E, 0xA0, 0x28, 0x5B, 0xA0,
  27837. 0x1A, 0x27, 0xF1, 0x85, 0x7A, 0x8F, 0xB3, 0x77,
  27838. 0xA3, 0xAE, 0x0C, 0x23, 0xB2, 0xAA, 0x9A, 0x07,
  27839. 0x9B, 0xFA, 0xBF, 0xF0, 0xD5, 0xB2, 0xF1, 0xCD,
  27840. 0x9B, 0x71, 0x8B, 0xEA, 0x03, 0xC4, 0x2F, 0x34,
  27841. 0x3A, 0x39, 0xB4, 0xF1, 0x42, 0xD0, 0x1A, 0xD8,
  27842. 0xAC, 0xBB, 0x50, 0xE3, 0x88, 0x53, 0xCF, 0x9A,
  27843. 0x50, 0xC8, 0xB4, 0x4C, 0x3C, 0xF6, 0x71, 0xA4,
  27844. 0xA9, 0x04, 0x3B, 0x26, 0xDD, 0xBB, 0x24, 0x95,
  27845. 0x9A, 0xD6, 0x71, 0x5C, 0x08, 0x52, 0x18, 0x55,
  27846. 0xC7, 0x9A, 0x23, 0xB9, 0xC3, 0xD6, 0x47, 0x17,
  27847. 0x49, 0xC4, 0x07, 0x25, 0xBD, 0xD5, 0xC2, 0x77,
  27848. 0x6D, 0x43, 0xAE, 0xD2, 0x02, 0x04, 0xBA, 0xA1,
  27849. 0x41, 0xEF, 0xB3, 0x30, 0x49, 0x17, 0x47, 0x4B,
  27850. 0x7F, 0x9F, 0x7A, 0x4B, 0x08, 0xB1, 0xA9, 0x3D,
  27851. 0xAE, 0xD9, 0x8C, 0x67, 0x49, 0x53, 0x59, 0xD3,
  27852. 0x7D, 0x67, 0xF7, 0x43, 0x8B, 0xEE, 0x5E, 0x43,
  27853. 0x58, 0x56, 0x34, 0xB2, 0x6C, 0x6B, 0x38, 0x10,
  27854. 0xD7, 0xCD, 0xCB, 0xC0, 0xF6, 0xEB, 0x87, 0x7A,
  27855. 0x60, 0x87, 0xE6, 0x8A, 0xCB, 0x84, 0x80, 0xD3,
  27856. 0xA8, 0xCF, 0x69, 0x00, 0x44, 0x7E, 0x49, 0xB4,
  27857. 0x17, 0xF1, 0x5A, 0x53, 0xB6, 0x07, 0xA0, 0xE2,
  27858. 0x16, 0xB8, 0x55, 0x97, 0x0D, 0x37, 0x40, 0x68,
  27859. 0x70, 0xB4, 0x56, 0x87, 0x22, 0xDA, 0x77, 0xA4,
  27860. 0x08, 0x47, 0x03, 0x81, 0x67, 0x84, 0xE2, 0xF1,
  27861. 0x6B, 0xED, 0x18, 0x99, 0x65, 0x32, 0xC5, 0xD8,
  27862. 0xB7, 0xF5, 0xD2, 0x14, 0x46, 0x4E, 0x5F, 0x3F,
  27863. 0x6E, 0x90, 0x58, 0x67, 0xB0, 0xCE, 0x11, 0x9E,
  27864. 0x25, 0x2A, 0x66, 0x71, 0x32, 0x53, 0x54, 0x46,
  27865. 0x85, 0xD2, 0x08, 0xE1, 0x72, 0x39, 0x08, 0xA0,
  27866. 0xCE, 0x97, 0x83, 0x46, 0x52, 0xE0, 0x8A, 0xE7,
  27867. 0xBD, 0xC8, 0x81, 0xA1, 0x31, 0xB7, 0x3C, 0x71,
  27868. 0xE8, 0x4D, 0x20, 0xD6, 0x8F, 0xDE, 0xFF, 0x4F,
  27869. 0x5D, 0x70, 0xCD, 0x1A, 0xF5, 0x7B, 0x78, 0xE3,
  27870. 0x49, 0x1A, 0x98, 0x65, 0x94, 0x23, 0x21, 0x80,
  27871. 0x0A, 0x20, 0x3C, 0x05, 0xED, 0x1F, 0xEE, 0xB5,
  27872. 0xA2, 0x8E, 0x58, 0x4E, 0x19, 0xF6, 0x53, 0x5E,
  27873. 0x7F, 0x84, 0xE4, 0xA2, 0x4F, 0x84, 0xA7, 0x2D,
  27874. 0xCA, 0xF5, 0x64, 0x8B, 0x4A, 0x42, 0x35, 0xDD,
  27875. 0x66, 0x44, 0x64, 0x48, 0x2F, 0x03, 0x17, 0x6E,
  27876. 0x88, 0x8C, 0x28, 0xBF, 0xC6, 0xC1, 0xCB, 0x23,
  27877. 0x8C, 0xFF, 0xA3, 0x5A, 0x32, 0x1E, 0x71, 0x79,
  27878. 0x1D, 0x9E, 0xA8, 0xED, 0x08, 0x78, 0xC6, 0x11,
  27879. 0x21, 0xBF, 0x8D, 0x2A, 0x4A, 0xB2, 0xC1, 0xA5,
  27880. 0xE1, 0x20, 0xBC, 0x40, 0xAB, 0xB1, 0x89, 0x2D,
  27881. 0x17, 0x15, 0x09, 0x0A, 0x0E, 0xE4, 0x82, 0x52,
  27882. 0xCA, 0x29, 0x7A, 0x99, 0xAA, 0x0E, 0x51, 0x0C,
  27883. 0xF2, 0x6B, 0x1A, 0xDD, 0x06, 0xCA, 0x54, 0x3E,
  27884. 0x1C, 0x5D, 0x6B, 0xDC, 0xD3, 0xB9, 0xC5, 0x85,
  27885. 0xC8, 0x53, 0x80, 0x45, 0xDB, 0x5C, 0x25, 0x2E,
  27886. 0xC3, 0xC8, 0xC3, 0xC9, 0x54, 0xD9, 0xBE, 0x59,
  27887. 0x07, 0x09, 0x4A, 0x89, 0x4E, 0x60, 0xEA, 0xB4,
  27888. 0x35, 0x38, 0xCF, 0xEE, 0x82, 0xE8, 0xFF, 0xC0,
  27889. 0x79, 0x1B, 0x0D, 0x0F, 0x43, 0xAC, 0x16, 0x27,
  27890. 0x83, 0x0A, 0x61, 0xD5, 0x6D, 0xAD, 0x96, 0xC6,
  27891. 0x29, 0x58, 0xB0, 0xDE, 0x78, 0x0B, 0x78, 0xBD,
  27892. 0x47, 0xA6, 0x04, 0x55, 0x0D, 0xAB, 0x83, 0xFF,
  27893. 0xF2, 0x27, 0xC3, 0x24, 0x04, 0x94, 0x71, 0xF3,
  27894. 0x52, 0x48, 0xCF, 0xB8, 0x49, 0xB2, 0x57, 0x24,
  27895. 0xFF, 0x70, 0x4D, 0x52, 0x77, 0xAA, 0x35, 0x2D,
  27896. 0x55, 0x09, 0x58, 0xBE, 0x3B, 0x23, 0x7D, 0xFF,
  27897. 0x47, 0x3E, 0xC2, 0xAD, 0xBA, 0xEA, 0x48, 0xCA,
  27898. 0x26, 0x58, 0xAE, 0xFC, 0xC7, 0x7B, 0xBD, 0x42,
  27899. 0x64, 0xAB, 0x37, 0x4D, 0x70, 0xEA, 0xE5, 0xB9,
  27900. 0x64, 0x41, 0x6C, 0xE8, 0x22, 0x6A, 0x7E, 0x32,
  27901. 0x55, 0xA0, 0xF8, 0xD7, 0xE2, 0xAD, 0xCA, 0x06,
  27902. 0x2B, 0xCD, 0x6D, 0x78, 0xD6, 0x0D, 0x1B, 0x32,
  27903. 0xE1, 0x14, 0x05, 0xBE, 0x54, 0xB6, 0x6E, 0xF0,
  27904. 0xFD, 0xDD, 0x56, 0x77, 0x02, 0xA3, 0xBC, 0xCF,
  27905. 0xED, 0xE3, 0xC5, 0x84, 0x70, 0x12, 0x69, 0xED,
  27906. 0x14, 0x80, 0x9F, 0x06, 0xF8, 0x96, 0x83, 0x56,
  27907. 0xBB, 0x92, 0x67, 0xFE, 0x86, 0xE5, 0x14, 0x25,
  27908. 0x2E, 0x88, 0xBB, 0x5C, 0x30, 0xA7, 0xEC, 0xB3,
  27909. 0xD0, 0xE6, 0x21, 0x02, 0x1E, 0xE0, 0xFB, 0xF7,
  27910. 0x87, 0x1B, 0x09, 0x34, 0x2B, 0xF8, 0x4F, 0x55,
  27911. 0xC9, 0x7E, 0xAF, 0x86, 0xC4, 0x81, 0x89, 0xC7,
  27912. 0xFF, 0x4D, 0xF3, 0x89, 0xF0, 0x77, 0xE2, 0x80,
  27913. 0x6E, 0x5F, 0xA7, 0x3B, 0x3E, 0x94, 0x58, 0xA1,
  27914. 0x6C, 0x7E, 0x27, 0x5F, 0x4F, 0x60, 0x22, 0x75,
  27915. 0x58, 0x0E, 0xB7, 0xB7, 0x13, 0x5F, 0xB5, 0x37,
  27916. 0xFA, 0x0C, 0xD9, 0x5D, 0x6E, 0xA5, 0x8C, 0x10,
  27917. 0x8C, 0xD8, 0x94, 0x3D, 0x70, 0xC1, 0x64, 0x31,
  27918. 0x11, 0xF4, 0xF0, 0x1C, 0xA8, 0xA8, 0x27, 0x6A,
  27919. 0x90, 0x26, 0x66, 0xED, 0x81, 0xB7, 0x8D, 0x16,
  27920. 0x8B, 0x00, 0x6F, 0x16, 0xAA, 0xA3, 0xD8, 0xE4,
  27921. 0xCE, 0x4F, 0x4D, 0x0F, 0xB0, 0x99, 0x7E, 0x41,
  27922. 0xAE, 0xFF, 0xB5, 0xB3, 0xDA, 0xA8, 0x38, 0x73,
  27923. 0x2F, 0x35, 0x73, 0x49, 0x44, 0x7F, 0x38, 0x77,
  27924. 0x76, 0xC7, 0x93, 0xC0, 0x47, 0x9D, 0xE9, 0xE9,
  27925. 0x94, 0x98, 0xCC, 0x35, 0x6F, 0xDB, 0x00, 0x75,
  27926. 0xA7, 0x03, 0xF2, 0x3C, 0x55, 0xD4, 0x7B, 0x55,
  27927. 0x0E, 0xC8, 0x9B, 0x02, 0xAD, 0xE8, 0x93, 0x29,
  27928. 0x08, 0x6A, 0x50, 0x84, 0x34, 0x56, 0xFE, 0xDC,
  27929. 0x37, 0x88, 0xAC, 0x8D, 0x97, 0x23, 0x3C, 0x54,
  27930. 0x56, 0x04, 0x67, 0xEE, 0x1D, 0x0F, 0x02, 0x4B,
  27931. 0x18, 0x42, 0x8F, 0x0D, 0x73, 0xB3, 0x0E, 0x19,
  27932. 0xF5, 0xC6, 0x3B, 0x9A, 0xBF, 0x11, 0x41, 0x5B,
  27933. 0xEA, 0x4D, 0x01, 0x70, 0x13, 0x0B, 0xAA, 0xBD,
  27934. 0x33, 0xC0, 0x5E, 0x65, 0x24, 0xE5, 0xFB, 0x55,
  27935. 0x81, 0xB2, 0x2B, 0x04, 0x33, 0x34, 0x22, 0x48,
  27936. 0x26, 0x6D, 0x0F, 0x10, 0x53, 0xB2, 0x45, 0xCC,
  27937. 0x24, 0x62, 0xDC, 0x44, 0xD3, 0x49, 0x65, 0x10,
  27938. 0x24, 0x82, 0xA8, 0xED, 0x9E, 0x4E, 0x96, 0x4D,
  27939. 0x56, 0x83, 0xE5, 0xD4, 0x5D, 0x0C, 0x82, 0x69
  27940. };
  27941. const byte kyber768_ss[] = {
  27942. 0x91, 0x4C, 0xB6, 0x7F, 0xE5, 0xC3, 0x8E, 0x73,
  27943. 0xBF, 0x74, 0x18, 0x1C, 0x0A, 0xC5, 0x04, 0x28,
  27944. 0xDE, 0xDF, 0x77, 0x50, 0xA9, 0x80, 0x58, 0xF7,
  27945. 0xD5, 0x36, 0x70, 0x87, 0x74, 0x53, 0x5B, 0x29
  27946. };
  27947. ret = wc_KyberKey_Init(KYBER768, &key, HEAP_HINT, INVALID_DEVID);
  27948. if (ret != 0)
  27949. return -20201;
  27950. ret = wc_KyberKey_MakeKeyWithRandom(&key, kyber768_rand,
  27951. sizeof(kyber768_rand));
  27952. if (ret != 0)
  27953. return -20202;
  27954. ret = wc_KyberKey_EncodePublicKey(&key, pub, sizeof(pub));
  27955. if (ret != 0)
  27956. return -20203;
  27957. ret = wc_KyberKey_EncodePrivateKey(&key, priv, sizeof(priv));
  27958. if (ret != 0)
  27959. return -20204;
  27960. if (XMEMCMP(pub, kyber768_pk, sizeof(kyber768_pk)) != 0)
  27961. return -20205;
  27962. if (XMEMCMP(priv, kyber768_sk, sizeof(kyber768_sk)) != 0)
  27963. return -20206;
  27964. ret = wc_KyberKey_EncapsulateWithRandom(&key, ct, ss, kyber768enc_rand,
  27965. sizeof(kyber768enc_rand));
  27966. if (ret != 0)
  27967. return -20207;
  27968. if (XMEMCMP(ct, kyber768_ct, sizeof(kyber768_ct)) != 0)
  27969. return -20208;
  27970. if (XMEMCMP(ss, kyber768_ss, sizeof(kyber768_ss)) != 0)
  27971. return -20209;
  27972. ret = wc_KyberKey_Decapsulate(&key, ss_dec, ct, sizeof(kyber768_ct));
  27973. if (ret != 0)
  27974. return -20210;
  27975. if (XMEMCMP(ss_dec, kyber768_ss, sizeof(kyber768_ss)) != 0)
  27976. return -20211;
  27977. wc_KyberKey_Free(&key);
  27978. return 0;
  27979. }
  27980. #endif /* WOLFSSL_KYBER768 */
  27981. #ifdef WOLFSSL_KYBER1024
  27982. static int kyber1024_kat(void)
  27983. {
  27984. KyberKey key;
  27985. int ret;
  27986. byte priv[KYBER1024_PRIVATE_KEY_SIZE];
  27987. byte pub[KYBER1024_PUBLIC_KEY_SIZE];
  27988. byte ct[KYBER1024_CIPHER_TEXT_SIZE];
  27989. byte ss[KYBER_SS_SZ];
  27990. byte ss_dec[KYBER_SS_SZ];
  27991. const byte kyber1024_rand[] = {
  27992. 0x7c, 0x99, 0x35, 0xa0, 0xb0, 0x76, 0x94, 0xaa,
  27993. 0x0c, 0x6d, 0x10, 0xe4, 0xdb, 0x6b, 0x1a, 0xdd,
  27994. 0x2f, 0xd8, 0x1a, 0x25, 0xcc, 0xb1, 0x48, 0x03,
  27995. 0x2d, 0xcd, 0x73, 0x99, 0x36, 0x73, 0x7f, 0x2d,
  27996. 0x86, 0x26, 0xED, 0x79, 0xD4, 0x51, 0x14, 0x08,
  27997. 0x00, 0xE0, 0x3B, 0x59, 0xB9, 0x56, 0xF8, 0x21,
  27998. 0x0E, 0x55, 0x60, 0x67, 0x40, 0x7D, 0x13, 0xDC,
  27999. 0x90, 0xFA, 0x9E, 0x8B, 0x87, 0x2B, 0xFB, 0x8F
  28000. };
  28001. const byte kyber1024enc_rand[] = {
  28002. 0x14, 0x7c, 0x03, 0xf7, 0xa5, 0xbe, 0xbb, 0xa4,
  28003. 0x06, 0xc8, 0xfa, 0xe1, 0x87, 0x4d, 0x7f, 0x13,
  28004. 0xc8, 0x0e, 0xfe, 0x79, 0xa3, 0xa9, 0xa8, 0x74,
  28005. 0xcc, 0x09, 0xfe, 0x76, 0xf6, 0x99, 0x76, 0x15
  28006. };
  28007. const byte kyber1024_pk[] = {
  28008. 0xD2, 0x23, 0x02, 0xCB, 0xD3, 0x39, 0x9F, 0xAC,
  28009. 0xC6, 0x30, 0x99, 0x1F, 0xC8, 0xF2, 0x8B, 0xDB,
  28010. 0x43, 0x54, 0x76, 0x25, 0x41, 0x52, 0x76, 0x78,
  28011. 0xBC, 0xF6, 0x1F, 0x65, 0xC2, 0x41, 0x14, 0x6C,
  28012. 0x42, 0x6D, 0x23, 0xB9, 0xBF, 0xAA, 0x6B, 0x7D,
  28013. 0xF1, 0x8C, 0x97, 0xF2, 0x0C, 0x1B, 0x61, 0x25,
  28014. 0xBF, 0x87, 0x4B, 0x1D, 0x89, 0x47, 0x58, 0x52,
  28015. 0xC4, 0x48, 0x21, 0x5D, 0xB0, 0xEB, 0x77, 0x37,
  28016. 0xF9, 0x14, 0x80, 0xE8, 0xCE, 0xBD, 0x9A, 0x08,
  28017. 0x71, 0x57, 0x4F, 0x5A, 0xB6, 0x2D, 0x90, 0x20,
  28018. 0x17, 0x5E, 0xC6, 0x92, 0x7C, 0xA0, 0xB5, 0x4C,
  28019. 0x09, 0x81, 0x8E, 0x42, 0xCF, 0x92, 0xA3, 0x83,
  28020. 0x17, 0x24, 0x22, 0xC7, 0xDC, 0x18, 0x31, 0xD6,
  28021. 0x3B, 0x0C, 0x29, 0x5D, 0xE7, 0x51, 0x59, 0xDB,
  28022. 0x80, 0x34, 0xE9, 0xE0, 0x7F, 0x7B, 0x0B, 0x91,
  28023. 0x0C, 0x3C, 0x1E, 0x5F, 0xB6, 0x6B, 0x3D, 0xC5,
  28024. 0x23, 0xF1, 0xFA, 0x6E, 0xB4, 0x91, 0x0C, 0xB8,
  28025. 0x9A, 0x6C, 0x17, 0x56, 0x2C, 0x83, 0xAB, 0x4C,
  28026. 0x18, 0xD0, 0xCD, 0x7E, 0x07, 0x96, 0x59, 0x2A,
  28027. 0x37, 0x2A, 0xA4, 0x09, 0xB1, 0xC5, 0x57, 0x34,
  28028. 0x7C, 0xCA, 0xCD, 0xC4, 0x64, 0x4A, 0x11, 0x90,
  28029. 0x64, 0xD0, 0x6D, 0xD4, 0x74, 0x92, 0x9D, 0x1C,
  28030. 0x6F, 0xB4, 0xD6, 0x86, 0xE5, 0x49, 0x1C, 0xE4,
  28031. 0xBC, 0x89, 0xA3, 0x0B, 0xB4, 0xB8, 0xC4, 0x1B,
  28032. 0xCE, 0x51, 0x57, 0xDF, 0xC1, 0x36, 0x08, 0x23,
  28033. 0xB1, 0xAB, 0x61, 0x8C, 0x14, 0xB1, 0x0F, 0x98,
  28034. 0xC2, 0x50, 0x67, 0x39, 0x8E, 0xA7, 0x01, 0x8C,
  28035. 0x27, 0x8A, 0x4B, 0x3D, 0xF3, 0x13, 0x34, 0xD6,
  28036. 0x03, 0xB2, 0x04, 0x4E, 0xF1, 0x87, 0xCD, 0x9B,
  28037. 0xC6, 0xCE, 0x42, 0x72, 0x5B, 0xD9, 0x62, 0xC2,
  28038. 0x64, 0x98, 0x3E, 0x9E, 0x18, 0x15, 0x5A, 0x8B,
  28039. 0x9C, 0x47, 0x14, 0x3D, 0x70, 0x46, 0x0A, 0x26,
  28040. 0xA5, 0x6F, 0xE7, 0x65, 0x8C, 0x1F, 0x15, 0x03,
  28041. 0x48, 0xC6, 0x08, 0x7E, 0xF7, 0x58, 0xAD, 0x16,
  28042. 0x78, 0x87, 0x86, 0x0A, 0x00, 0x7A, 0x5F, 0xC3,
  28043. 0x73, 0x58, 0xD4, 0x3B, 0x5E, 0xBE, 0xE8, 0x20,
  28044. 0xAC, 0xEA, 0x47, 0x4F, 0x0A, 0xC0, 0x7B, 0x76,
  28045. 0x80, 0x28, 0x66, 0x19, 0x9C, 0x61, 0x23, 0x1D,
  28046. 0x5C, 0x74, 0x7C, 0x93, 0x77, 0x4D, 0x2C, 0x1E,
  28047. 0x0C, 0x1C, 0x67, 0xE6, 0xC8, 0x1B, 0x82, 0x75,
  28048. 0x21, 0x73, 0xE1, 0x25, 0xBA, 0xF3, 0x9B, 0x4F,
  28049. 0xD1, 0x9A, 0x4F, 0x45, 0x3D, 0xC5, 0x79, 0x76,
  28050. 0xB1, 0xD9, 0x7F, 0xE6, 0x99, 0x69, 0x92, 0xBB,
  28051. 0xB6, 0x5B, 0x7C, 0xB2, 0x5D, 0x07, 0x7B, 0xBA,
  28052. 0xA6, 0xA1, 0x33, 0x22, 0x89, 0x9A, 0xF6, 0x59,
  28053. 0xCF, 0x1B, 0x35, 0x58, 0xC1, 0xB5, 0x00, 0x11,
  28054. 0x54, 0xB6, 0x25, 0x80, 0x9E, 0xD8, 0x9A, 0xEE,
  28055. 0xBB, 0x89, 0xE6, 0xEA, 0x7D, 0x67, 0xF7, 0x23,
  28056. 0xD0, 0x45, 0xAB, 0x05, 0x71, 0x5C, 0x42, 0x35,
  28057. 0x5D, 0xA6, 0xA5, 0xC8, 0xDD, 0x39, 0xC8, 0xAB,
  28058. 0xE3, 0x03, 0x77, 0x51, 0xA0, 0x1E, 0xD1, 0xC7,
  28059. 0x37, 0x49, 0x19, 0xF3, 0x12, 0x1B, 0x5A, 0x52,
  28060. 0xC5, 0x3D, 0x14, 0x87, 0x31, 0x67, 0x69, 0xF8,
  28061. 0x07, 0x21, 0xDE, 0xEA, 0xAA, 0xD3, 0xC9, 0x0F,
  28062. 0x76, 0xE7, 0xAE, 0x9E, 0x12, 0xBA, 0x92, 0xB3,
  28063. 0x2B, 0x5F, 0xD4, 0x57, 0xE3, 0xC7, 0x52, 0xC2,
  28064. 0x65, 0x0D, 0xFB, 0x88, 0x57, 0x71, 0xCB, 0x77,
  28065. 0xAC, 0x3C, 0x78, 0x5A, 0x8C, 0x56, 0x2E, 0x6A,
  28066. 0x1C, 0x63, 0xC2, 0xA5, 0x5E, 0xA4, 0x7C, 0xF8,
  28067. 0xB9, 0x0E, 0xB8, 0x22, 0x5C, 0x12, 0x3C, 0x34,
  28068. 0x64, 0x52, 0x56, 0x62, 0x35, 0xB2, 0xF3, 0x18,
  28069. 0x23, 0xA3, 0x35, 0x21, 0xE0, 0x87, 0x93, 0x7A,
  28070. 0x34, 0x5D, 0x8D, 0x66, 0x3E, 0xEA, 0xA0, 0x56,
  28071. 0x58, 0x91, 0x7B, 0xBA, 0xA0, 0x08, 0xC2, 0xE3,
  28072. 0x35, 0xF8, 0x85, 0x0A, 0x90, 0xA3, 0x26, 0xD0,
  28073. 0xE6, 0x64, 0x32, 0xF4, 0x4C, 0xEB, 0x82, 0x89,
  28074. 0xE4, 0xEC, 0xB2, 0xD1, 0x29, 0x58, 0xE9, 0x84,
  28075. 0x07, 0x2E, 0xCA, 0xCB, 0x88, 0xE1, 0x34, 0x8F,
  28076. 0xF0, 0xB5, 0x56, 0x54, 0xAC, 0xBA, 0x5B, 0x54,
  28077. 0x97, 0x1C, 0xBA, 0xEB, 0xA8, 0x8E, 0xC4, 0xB9,
  28078. 0x1A, 0x94, 0xC3, 0x71, 0x92, 0xFA, 0x98, 0x2B,
  28079. 0xEC, 0xB9, 0xF3, 0xDA, 0x42, 0x16, 0x03, 0xB6,
  28080. 0x1A, 0x51, 0xBC, 0x8E, 0x36, 0xCB, 0xD0, 0x53,
  28081. 0x85, 0x1C, 0x77, 0xB1, 0xB9, 0x26, 0xB1, 0x7A,
  28082. 0x27, 0x2A, 0xA9, 0x02, 0x32, 0x46, 0xB0, 0x2B,
  28083. 0x3E, 0xD4, 0x7F, 0x66, 0xA0, 0x0B, 0xD5, 0x68,
  28084. 0x48, 0x23, 0x63, 0x4E, 0x7C, 0xE5, 0x8C, 0xF8,
  28085. 0xF3, 0x06, 0xE3, 0x5B, 0x1E, 0x53, 0x22, 0x82,
  28086. 0x4D, 0x90, 0x48, 0x01, 0xF0, 0xA2, 0xFA, 0x7C,
  28087. 0x2B, 0xC9, 0xC2, 0x52, 0xB0, 0xA5, 0x6B, 0x7B,
  28088. 0xA2, 0xAB, 0x0F, 0x63, 0x60, 0x21, 0x74, 0x5A,
  28089. 0x70, 0xA9, 0xA4, 0x3E, 0x2B, 0x0A, 0x8D, 0x61,
  28090. 0x59, 0x70, 0xB6, 0x53, 0x09, 0x62, 0x4B, 0x51,
  28091. 0x84, 0xBC, 0xC3, 0x0B, 0x91, 0x16, 0x79, 0xAE,
  28092. 0xDD, 0x76, 0x02, 0x5F, 0xE3, 0x90, 0x8F, 0xD6,
  28093. 0x78, 0x97, 0xB0, 0xCF, 0x4B, 0xE5, 0xA6, 0xF5,
  28094. 0x41, 0x3D, 0x7D, 0xD9, 0x85, 0x64, 0xB2, 0x3E,
  28095. 0x42, 0xA9, 0x3E, 0x4A, 0xA8, 0x82, 0x1C, 0xD4,
  28096. 0x50, 0x54, 0xC6, 0x43, 0xED, 0xC1, 0x15, 0x8D,
  28097. 0xB6, 0xB3, 0xDE, 0xB1, 0x3F, 0xB5, 0xA5, 0x1E,
  28098. 0xBD, 0x1A, 0x8A, 0x78, 0xB8, 0x72, 0x25, 0xA7,
  28099. 0x33, 0x8E, 0x10, 0x11, 0x04, 0xC4, 0xA2, 0x20,
  28100. 0xD9, 0xBD, 0xED, 0xD4, 0x8C, 0x85, 0xA1, 0xC2,
  28101. 0xDA, 0xE7, 0x81, 0xA8, 0x0C, 0x40, 0xE1, 0x3B,
  28102. 0x87, 0xEA, 0xC7, 0x3A, 0x76, 0x42, 0x01, 0xC9,
  28103. 0xB7, 0x60, 0xCC, 0xFB, 0x1A, 0xE3, 0x92, 0x69,
  28104. 0x9C, 0x70, 0x39, 0xD2, 0x7C, 0x39, 0x36, 0x2B,
  28105. 0x27, 0xB8, 0xFC, 0x6F, 0x07, 0xA8, 0xA3, 0xD4,
  28106. 0x41, 0x0F, 0x15, 0x47, 0xC4, 0x8A, 0x99, 0x97,
  28107. 0xF6, 0x2C, 0x61, 0x07, 0x44, 0x52, 0xEF, 0x15,
  28108. 0x15, 0xF8, 0xA6, 0x49, 0xEB, 0xCA, 0x94, 0x37,
  28109. 0x20, 0x5A, 0x4E, 0x8A, 0x61, 0x60, 0x6B, 0x41,
  28110. 0xDA, 0xF6, 0x83, 0x4D, 0x67, 0x1F, 0x4D, 0x85,
  28111. 0x2C, 0x0C, 0x9C, 0x40, 0x96, 0x61, 0x16, 0x48,
  28112. 0xC6, 0xA3, 0x17, 0x06, 0x78, 0xB1, 0x53, 0x7C,
  28113. 0xC1, 0x82, 0x8D, 0x93, 0x58, 0x0C, 0x9E, 0x58,
  28114. 0x49, 0xA9, 0x65, 0x31, 0x75, 0xAC, 0xB7, 0x53,
  28115. 0xF2, 0xBE, 0x74, 0x37, 0xBE, 0x45, 0xF6, 0xC6,
  28116. 0x03, 0xE4, 0x85, 0xF2, 0xEC, 0x30, 0x1B, 0xB4,
  28117. 0x2B, 0x6C, 0x37, 0xC2, 0x25, 0xD7, 0x49, 0x5A,
  28118. 0x58, 0x4A, 0xE2, 0x31, 0x89, 0x0A, 0xB5, 0xC8,
  28119. 0xC3, 0x5C, 0x26, 0x8C, 0xF4, 0xBB, 0xB0, 0x21,
  28120. 0x3C, 0x09, 0x60, 0x19, 0x31, 0x95, 0x61, 0xA8,
  28121. 0xA6, 0x94, 0x76, 0x37, 0xAA, 0x40, 0xD0, 0x06,
  28122. 0xB4, 0x15, 0xBB, 0x2C, 0xFA, 0x22, 0x37, 0xE0,
  28123. 0x89, 0x0B, 0x6A, 0x3B, 0xC1, 0x34, 0xAB, 0xF8,
  28124. 0xF6, 0x58, 0x5E, 0x10, 0x8D, 0x15, 0x94, 0x0F,
  28125. 0x91, 0xF4, 0xBF, 0x5B, 0x0C, 0x81, 0x80, 0x55,
  28126. 0xB2, 0x1D, 0xEA, 0x6E, 0x63, 0xB5, 0x53, 0x98,
  28127. 0x8C, 0x47, 0xF4, 0xB9, 0x4E, 0x7C, 0xF8, 0x00,
  28128. 0xA4, 0x93, 0xB4, 0x73, 0x47, 0x05, 0xED, 0xC5,
  28129. 0x6A, 0x4B, 0x60, 0x21, 0xC6, 0x29, 0x50, 0x06,
  28130. 0x75, 0x87, 0x68, 0x04, 0xCF, 0x0B, 0x95, 0x1F,
  28131. 0x03, 0x8A, 0x5C, 0x7F, 0xE5, 0x8E, 0x89, 0x77,
  28132. 0x4E, 0xF2, 0x99, 0x2F, 0xD7, 0xC6, 0x30, 0x99,
  28133. 0xD3, 0x52, 0xA7, 0xD2, 0x15, 0x60, 0xB7, 0x88,
  28134. 0xB4, 0x05, 0x70, 0x98, 0x61, 0x81, 0x7E, 0x59,
  28135. 0xA9, 0x6B, 0x3A, 0x3A, 0x83, 0xCB, 0xA8, 0x03,
  28136. 0xB1, 0x69, 0x34, 0x33, 0x10, 0x71, 0x90, 0x5B,
  28137. 0xBE, 0xC6, 0x53, 0x29, 0x00, 0x15, 0x5D, 0x8A,
  28138. 0xC8, 0x8C, 0xB3, 0x2E, 0x4E, 0x21, 0xA3, 0xBD,
  28139. 0x3A, 0x03, 0xFD, 0xEC, 0x32, 0x5A, 0x51, 0xCD,
  28140. 0x27, 0x73, 0x96, 0x4E, 0x67, 0x84, 0xFC, 0xF1,
  28141. 0x85, 0x37, 0x37, 0xAA, 0x64, 0xEB, 0x67, 0x56,
  28142. 0x47, 0x27, 0x27, 0x26, 0x61, 0xAB, 0xF8, 0x43,
  28143. 0x13, 0xA5, 0x7A, 0x44, 0xB1, 0x23, 0xC6, 0x55,
  28144. 0x09, 0xCF, 0xB7, 0xA6, 0xF6, 0x64, 0x1C, 0xDC,
  28145. 0xC3, 0xB5, 0x7F, 0xE6, 0x28, 0xC7, 0xB8, 0x19,
  28146. 0x2D, 0xB4, 0x4F, 0xFB, 0xF5, 0x79, 0x6A, 0x86,
  28147. 0x13, 0xB1, 0xFA, 0x12, 0x6F, 0x60, 0x76, 0x88,
  28148. 0x3C, 0x78, 0x3D, 0xC2, 0x4E, 0x2A, 0x44, 0x64,
  28149. 0xC4, 0x0B, 0x3A, 0x41, 0xCA, 0x70, 0xAE, 0x87,
  28150. 0x62, 0x08, 0x66, 0xCF, 0x4F, 0xCB, 0x2B, 0xD2,
  28151. 0x04, 0xBF, 0x5C, 0x28, 0x38, 0x12, 0xBA, 0x05,
  28152. 0x6A, 0xC0, 0xC3, 0x45, 0xE3, 0x79, 0xC4, 0xBA,
  28153. 0x24, 0xD7, 0x50, 0x90, 0x12, 0x79, 0xBB, 0x2F,
  28154. 0x3A, 0x16, 0xF6, 0x12, 0xBF, 0xAD, 0xB3, 0x57,
  28155. 0x03, 0x33, 0x2C, 0x7C, 0x13, 0x6F, 0x68, 0xEA,
  28156. 0xB6, 0x75, 0x5C, 0x66, 0xB6, 0xA4, 0xAD, 0x1A,
  28157. 0xAB, 0xA7, 0xB7, 0x68, 0xA5, 0x8A, 0xCA, 0xAC,
  28158. 0xC1, 0x0A, 0x45, 0x9A, 0x1C, 0xC8, 0xEF, 0x29,
  28159. 0x37, 0x7B, 0xC2, 0x00, 0xE4, 0xD3, 0x15, 0xA3,
  28160. 0x0A, 0x6B, 0xCC, 0x32, 0x56, 0xF9, 0x73, 0x4D,
  28161. 0x06, 0xE9, 0x77, 0x9C, 0xAA, 0x54, 0x42, 0xA9,
  28162. 0xA1, 0x60, 0x69, 0x08, 0x13, 0x77, 0xC7, 0x6E,
  28163. 0x75, 0x15, 0x43, 0x68, 0x07, 0x2D, 0xC4, 0x46,
  28164. 0xED, 0x6C, 0x8B, 0x8E, 0x62, 0x2A, 0x21, 0xE3,
  28165. 0x83, 0xCF, 0x9B, 0xA1, 0xFB, 0x43, 0x4E, 0x2E,
  28166. 0xCC, 0x81, 0xE7, 0xB7, 0x8C, 0xEE, 0x98, 0x6B,
  28167. 0x8F, 0xF7, 0x98, 0xAB, 0x18, 0xCF, 0x96, 0x34,
  28168. 0x54, 0x35, 0x46, 0x28, 0x4E, 0xDA, 0x2A, 0x26,
  28169. 0xB4, 0x7F, 0x05, 0xB7, 0x35, 0xBC, 0xDB, 0x12,
  28170. 0x02, 0x22, 0x00, 0x76, 0xDC, 0x8B, 0x4E, 0x4B,
  28171. 0x9F, 0x85, 0x35, 0x33, 0xC8, 0xF6, 0xC7, 0xFF,
  28172. 0x38, 0x81, 0x7B, 0xA4, 0x97, 0x12, 0x83, 0x57,
  28173. 0x85, 0xF1, 0x7F, 0x14, 0xCA, 0x01, 0xD0, 0xC1,
  28174. 0xC1, 0xE9, 0x88, 0x10, 0xFE, 0x0B, 0x36, 0xE5,
  28175. 0xB4, 0x27, 0x15, 0x7B, 0x94, 0x18, 0x44, 0x9C,
  28176. 0xED, 0xD6, 0x41, 0xA4, 0x29, 0x3C, 0x85, 0xC3,
  28177. 0x27, 0x00, 0x10, 0x2A, 0xCE, 0xC2, 0x2E, 0xBA,
  28178. 0xD9, 0x8E, 0xD1, 0x60, 0xA5, 0xF0, 0x27, 0xBD,
  28179. 0x4C, 0xDA, 0x57, 0xF1, 0xF3, 0x72, 0x0A, 0x12,
  28180. 0xC1, 0x34, 0x65, 0x4D, 0xD5, 0xE7, 0x3F, 0x82,
  28181. 0x96, 0x76, 0x49, 0x53, 0x90, 0xD0, 0xE7, 0x92,
  28182. 0x9D, 0x60, 0x34, 0xE9, 0xC5, 0x5F, 0x7D, 0x55,
  28183. 0xBA, 0x65, 0x8B, 0xC5, 0x87, 0x98, 0x8E, 0x8A,
  28184. 0xF9, 0x49, 0x60, 0xF6, 0xCF, 0xB8, 0xD5, 0xAF,
  28185. 0x7A, 0x00, 0x21, 0x53, 0x5A, 0x6E, 0x25, 0xE4,
  28186. 0x37, 0xD4, 0x9A, 0x78, 0x06, 0x98, 0xBE, 0x22,
  28187. 0xAC, 0x99, 0x53, 0x94, 0x9F, 0x57, 0x1B, 0x85,
  28188. 0xA6, 0x85, 0x72, 0x5F, 0x82, 0x07, 0xA2, 0xB0,
  28189. 0xAE, 0x84, 0x9B, 0x60, 0x1A, 0xB9, 0x1B, 0x15,
  28190. 0x9B, 0x3D, 0xF4, 0xA1, 0x54, 0xC2, 0x04, 0x1E,
  28191. 0x77, 0x60, 0x70, 0xAF, 0xC4, 0x29, 0x69, 0x32,
  28192. 0x23, 0x80, 0x91, 0x7C, 0x97, 0x51, 0x07, 0x99,
  28193. 0xF3, 0x14, 0x91, 0x31, 0x47, 0x7E, 0x16, 0x66,
  28194. 0x3D, 0x31, 0x74, 0xC7, 0xC1, 0xCA, 0xEA, 0x78,
  28195. 0x85, 0x35, 0xC6, 0xC0, 0x05, 0xA6, 0x4F, 0x28,
  28196. 0x68, 0x63, 0x1B, 0x31, 0xB6, 0x6E, 0x20, 0x5F,
  28197. 0xD3, 0x8C, 0x1D, 0x84, 0x54, 0x2D, 0x0F, 0x1B,
  28198. 0x57, 0x8F, 0x58, 0xC9, 0xBF, 0x5A, 0x0F, 0xAE,
  28199. 0xAB, 0x6A, 0xB6, 0x49, 0x48, 0x93, 0x05, 0x31,
  28200. 0x65, 0xEA, 0xFD, 0x46, 0x5F, 0xC6, 0x4A, 0x0C,
  28201. 0x5F, 0x8F, 0x3F, 0x90, 0x03, 0x48, 0x94, 0x15,
  28202. 0x89, 0x9D, 0x59, 0xA5, 0x43, 0xD8, 0x20, 0x8C,
  28203. 0x54, 0xA3, 0x16, 0x65, 0x29, 0xB5, 0x39, 0x22
  28204. };
  28205. const byte kyber1024_sk[] = {
  28206. 0x07, 0x63, 0x8F, 0xB6, 0x98, 0x68, 0xF3, 0xD3,
  28207. 0x20, 0xE5, 0x86, 0x2B, 0xD9, 0x69, 0x33, 0xFE,
  28208. 0xB3, 0x11, 0xB3, 0x62, 0x09, 0x3C, 0x9B, 0x5D,
  28209. 0x50, 0x17, 0x0B, 0xCE, 0xD4, 0x3F, 0x1B, 0x53,
  28210. 0x6D, 0x9A, 0x20, 0x4B, 0xB1, 0xF2, 0x26, 0x95,
  28211. 0x95, 0x0B, 0xA1, 0xF2, 0xA9, 0xE8, 0xEB, 0x82,
  28212. 0x8B, 0x28, 0x44, 0x88, 0x76, 0x0B, 0x3F, 0xC8,
  28213. 0x4F, 0xAB, 0xA0, 0x42, 0x75, 0xD5, 0x62, 0x8E,
  28214. 0x39, 0xC5, 0xB2, 0x47, 0x13, 0x74, 0x28, 0x3C,
  28215. 0x50, 0x32, 0x99, 0xC0, 0xAB, 0x49, 0xB6, 0x6B,
  28216. 0x8B, 0xBB, 0x56, 0xA4, 0x18, 0x66, 0x24, 0xF9,
  28217. 0x19, 0xA2, 0xBA, 0x59, 0xBB, 0x08, 0xD8, 0x55,
  28218. 0x18, 0x80, 0xC2, 0xBE, 0xFC, 0x4F, 0x87, 0xF2,
  28219. 0x5F, 0x59, 0xAB, 0x58, 0x7A, 0x79, 0xC3, 0x27,
  28220. 0xD7, 0x92, 0xD5, 0x4C, 0x97, 0x4A, 0x69, 0x26,
  28221. 0x2F, 0xF8, 0xA7, 0x89, 0x38, 0x28, 0x9E, 0x9A,
  28222. 0x87, 0xB6, 0x88, 0xB0, 0x83, 0xE0, 0x59, 0x5F,
  28223. 0xE2, 0x18, 0xB6, 0xBB, 0x15, 0x05, 0x94, 0x1C,
  28224. 0xE2, 0xE8, 0x1A, 0x5A, 0x64, 0xC5, 0xAA, 0xC6,
  28225. 0x04, 0x17, 0x25, 0x69, 0x85, 0x34, 0x9E, 0xE4,
  28226. 0x7A, 0x52, 0x42, 0x0A, 0x5F, 0x97, 0x47, 0x7B,
  28227. 0x72, 0x36, 0xAC, 0x76, 0xBC, 0x70, 0xE8, 0x28,
  28228. 0x87, 0x29, 0x28, 0x7E, 0xE3, 0xE3, 0x4A, 0x3D,
  28229. 0xBC, 0x36, 0x83, 0xC0, 0xB7, 0xB1, 0x00, 0x29,
  28230. 0xFC, 0x20, 0x34, 0x18, 0x53, 0x7E, 0x74, 0x66,
  28231. 0xBA, 0x63, 0x85, 0xA8, 0xFF, 0x30, 0x1E, 0xE1,
  28232. 0x27, 0x08, 0xF8, 0x2A, 0xAA, 0x1E, 0x38, 0x0F,
  28233. 0xC7, 0xA8, 0x8F, 0x8F, 0x20, 0x5A, 0xB7, 0xE8,
  28234. 0x8D, 0x7E, 0x95, 0x95, 0x2A, 0x55, 0xBA, 0x20,
  28235. 0xD0, 0x9B, 0x79, 0xA4, 0x71, 0x41, 0xD6, 0x2B,
  28236. 0xF6, 0xEB, 0x7D, 0xD3, 0x07, 0xB0, 0x8E, 0xCA,
  28237. 0x13, 0xA5, 0xBC, 0x5F, 0x6B, 0x68, 0x58, 0x1C,
  28238. 0x68, 0x65, 0xB2, 0x7B, 0xBC, 0xDD, 0xAB, 0x14,
  28239. 0x2F, 0x4B, 0x2C, 0xBF, 0xF4, 0x88, 0xC8, 0xA2,
  28240. 0x27, 0x05, 0xFA, 0xA9, 0x8A, 0x2B, 0x9E, 0xEA,
  28241. 0x35, 0x30, 0xC7, 0x66, 0x62, 0x33, 0x5C, 0xC7,
  28242. 0xEA, 0x3A, 0x00, 0x77, 0x77, 0x25, 0xEB, 0xCC,
  28243. 0xCD, 0x2A, 0x46, 0x36, 0xB2, 0xD9, 0x12, 0x2F,
  28244. 0xF3, 0xAB, 0x77, 0x12, 0x3C, 0xE0, 0x88, 0x3C,
  28245. 0x19, 0x11, 0x11, 0x5E, 0x50, 0xC9, 0xE8, 0xA9,
  28246. 0x41, 0x94, 0xE4, 0x8D, 0xD0, 0xD0, 0x9C, 0xFF,
  28247. 0xB3, 0xAD, 0xCD, 0x2C, 0x1E, 0x92, 0x43, 0x09,
  28248. 0x03, 0xD0, 0x7A, 0xDB, 0xF0, 0x05, 0x32, 0x03,
  28249. 0x15, 0x75, 0xAA, 0x7F, 0x9E, 0x7B, 0x5A, 0x1F,
  28250. 0x33, 0x62, 0xDE, 0xC9, 0x36, 0xD4, 0x04, 0x3C,
  28251. 0x05, 0xF2, 0x47, 0x6C, 0x07, 0x57, 0x8B, 0xC9,
  28252. 0xCB, 0xAF, 0x2A, 0xB4, 0xE3, 0x82, 0x72, 0x7A,
  28253. 0xD4, 0x16, 0x86, 0xA9, 0x6B, 0x25, 0x48, 0x82,
  28254. 0x0B, 0xB0, 0x3B, 0x32, 0xF1, 0x1B, 0x28, 0x11,
  28255. 0xAD, 0x62, 0xF4, 0x89, 0xE9, 0x51, 0x63, 0x2A,
  28256. 0xBA, 0x0D, 0x1D, 0xF8, 0x96, 0x80, 0xCC, 0x8A,
  28257. 0x8B, 0x53, 0xB4, 0x81, 0xD9, 0x2A, 0x68, 0xD7,
  28258. 0x0B, 0x4E, 0xA1, 0xC3, 0xA6, 0xA5, 0x61, 0xC0,
  28259. 0x69, 0x28, 0x82, 0xB5, 0xCA, 0x8C, 0xC9, 0x42,
  28260. 0xA8, 0xD4, 0x95, 0xAF, 0xCB, 0x06, 0xDE, 0x89,
  28261. 0x49, 0x8F, 0xB9, 0x35, 0xB7, 0x75, 0x90, 0x8F,
  28262. 0xE7, 0xA0, 0x3E, 0x32, 0x4D, 0x54, 0xCC, 0x19,
  28263. 0xD4, 0xE1, 0xAA, 0xBD, 0x35, 0x93, 0xB3, 0x8B,
  28264. 0x19, 0xEE, 0x13, 0x88, 0xFE, 0x49, 0x2B, 0x43,
  28265. 0x12, 0x7E, 0x5A, 0x50, 0x42, 0x53, 0x78, 0x6A,
  28266. 0x0D, 0x69, 0xAD, 0x32, 0x60, 0x1C, 0x28, 0xE2,
  28267. 0xC8, 0x85, 0x04, 0xA5, 0xBA, 0x59, 0x97, 0x06,
  28268. 0x02, 0x3A, 0x61, 0x36, 0x3E, 0x17, 0xC6, 0xB9,
  28269. 0xBB, 0x59, 0xBD, 0xC6, 0x97, 0x45, 0x2C, 0xD0,
  28270. 0x59, 0x45, 0x19, 0x83, 0xD7, 0x38, 0xCA, 0x3F,
  28271. 0xD0, 0x34, 0xE3, 0xF5, 0x98, 0x88, 0x54, 0xCA,
  28272. 0x05, 0x03, 0x1D, 0xB0, 0x96, 0x11, 0x49, 0x89,
  28273. 0x88, 0x19, 0x7C, 0x6B, 0x30, 0xD2, 0x58, 0xDF,
  28274. 0xE2, 0x62, 0x65, 0x54, 0x1C, 0x89, 0xA4, 0xB3,
  28275. 0x1D, 0x68, 0x64, 0xE9, 0x38, 0x9B, 0x03, 0xCB,
  28276. 0x74, 0xF7, 0xEC, 0x43, 0x23, 0xFB, 0x94, 0x21,
  28277. 0xA4, 0xB9, 0x79, 0x0A, 0x26, 0xD1, 0x7B, 0x03,
  28278. 0x98, 0xA2, 0x67, 0x67, 0x35, 0x09, 0x09, 0xF8,
  28279. 0x4D, 0x57, 0xB6, 0x69, 0x4D, 0xF8, 0x30, 0x66,
  28280. 0x4C, 0xA8, 0xB3, 0xC3, 0xC0, 0x3E, 0xD2, 0xAE,
  28281. 0x67, 0xB8, 0x90, 0x06, 0x86, 0x8A, 0x68, 0x52,
  28282. 0x7C, 0xCD, 0x66, 0x64, 0x59, 0xAB, 0x7F, 0x05,
  28283. 0x66, 0x71, 0x00, 0x0C, 0x61, 0x64, 0xD3, 0xA7,
  28284. 0xF2, 0x66, 0xA1, 0x4D, 0x97, 0xCB, 0xD7, 0x00,
  28285. 0x4D, 0x6C, 0x92, 0xCA, 0xCA, 0x77, 0x0B, 0x84,
  28286. 0x4A, 0x4F, 0xA9, 0xB1, 0x82, 0xE7, 0xB1, 0x8C,
  28287. 0xA8, 0x85, 0x08, 0x2A, 0xC5, 0x64, 0x6F, 0xCB,
  28288. 0x4A, 0x14, 0xE1, 0x68, 0x5F, 0xEB, 0x0C, 0x9C,
  28289. 0xE3, 0x37, 0x2A, 0xB9, 0x53, 0x65, 0xC0, 0x4F,
  28290. 0xD8, 0x30, 0x84, 0xF8, 0x0A, 0x23, 0xFF, 0x10,
  28291. 0xA0, 0x5B, 0xF1, 0x5F, 0x7F, 0xA5, 0xAC, 0xC6,
  28292. 0xC0, 0xCB, 0x46, 0x2C, 0x33, 0xCA, 0x52, 0x4F,
  28293. 0xA6, 0xB8, 0xBB, 0x35, 0x90, 0x43, 0xBA, 0x68,
  28294. 0x60, 0x9E, 0xAA, 0x25, 0x36, 0xE8, 0x1D, 0x08,
  28295. 0x46, 0x3B, 0x19, 0x65, 0x3B, 0x54, 0x35, 0xBA,
  28296. 0x94, 0x6C, 0x9A, 0xDD, 0xEB, 0x20, 0x2B, 0x04,
  28297. 0xB0, 0x31, 0xCC, 0x96, 0x0D, 0xCC, 0x12, 0xE4,
  28298. 0x51, 0x8D, 0x42, 0x8B, 0x32, 0xB2, 0x57, 0xA4,
  28299. 0xFC, 0x73, 0x13, 0xD3, 0xA7, 0x98, 0x0D, 0x80,
  28300. 0x08, 0x2E, 0x93, 0x4F, 0x9D, 0x95, 0xC3, 0x2B,
  28301. 0x0A, 0x01, 0x91, 0xA2, 0x36, 0x04, 0x38, 0x4D,
  28302. 0xD9, 0xE0, 0x79, 0xBB, 0xBA, 0xA2, 0x66, 0xD1,
  28303. 0x4C, 0x3F, 0x75, 0x6B, 0x9F, 0x21, 0x33, 0x10,
  28304. 0x74, 0x33, 0xA4, 0xE8, 0x3F, 0xA7, 0x18, 0x72,
  28305. 0x82, 0xA8, 0x09, 0x20, 0x3A, 0x4F, 0xAF, 0x84,
  28306. 0x18, 0x51, 0x83, 0x3D, 0x12, 0x1A, 0xC3, 0x83,
  28307. 0x84, 0x3A, 0x5E, 0x55, 0xBC, 0x23, 0x81, 0x42,
  28308. 0x5E, 0x16, 0xC7, 0xDB, 0x4C, 0xC9, 0xAB, 0x5C,
  28309. 0x1B, 0x0D, 0x91, 0xA4, 0x7E, 0x2B, 0x8D, 0xE0,
  28310. 0xE5, 0x82, 0xC8, 0x6B, 0x6B, 0x0D, 0x90, 0x7B,
  28311. 0xB3, 0x60, 0xB9, 0x7F, 0x40, 0xAB, 0x5D, 0x03,
  28312. 0x8F, 0x6B, 0x75, 0xC8, 0x14, 0xB2, 0x7D, 0x9B,
  28313. 0x96, 0x8D, 0x41, 0x98, 0x32, 0xBC, 0x8C, 0x2B,
  28314. 0xEE, 0x60, 0x5E, 0xF6, 0xE5, 0x05, 0x9D, 0x33,
  28315. 0x10, 0x0D, 0x90, 0x48, 0x5D, 0x37, 0x84, 0x50,
  28316. 0x01, 0x42, 0x21, 0x73, 0x6C, 0x07, 0x40, 0x7C,
  28317. 0xAC, 0x26, 0x04, 0x08, 0xAA, 0x64, 0x92, 0x66,
  28318. 0x19, 0x78, 0x8B, 0x86, 0x01, 0xC2, 0xA7, 0x52,
  28319. 0xD1, 0xA6, 0xCB, 0xF8, 0x20, 0xD7, 0xC7, 0xA0,
  28320. 0x47, 0x16, 0x20, 0x32, 0x25, 0xB3, 0x89, 0x5B,
  28321. 0x93, 0x42, 0xD1, 0x47, 0xA8, 0x18, 0x5C, 0xFC,
  28322. 0x1B, 0xB6, 0x5B, 0xA0, 0x6B, 0x41, 0x42, 0x33,
  28323. 0x99, 0x03, 0xC0, 0xAC, 0x46, 0x51, 0x38, 0x5B,
  28324. 0x45, 0xD9, 0x8A, 0x8B, 0x19, 0xD2, 0x8C, 0xD6,
  28325. 0xBA, 0xB0, 0x88, 0x78, 0x7F, 0x7E, 0xE1, 0xB1,
  28326. 0x24, 0x61, 0x76, 0x6B, 0x43, 0xCB, 0xCC, 0xB9,
  28327. 0x64, 0x34, 0x42, 0x7D, 0x93, 0xC0, 0x65, 0x55,
  28328. 0x06, 0x88, 0xF6, 0x94, 0x8E, 0xD1, 0xB5, 0x47,
  28329. 0x5A, 0x42, 0x5F, 0x1B, 0x85, 0x20, 0x9D, 0x06,
  28330. 0x1C, 0x08, 0xB5, 0x6C, 0x1C, 0xC0, 0x69, 0xF6,
  28331. 0xC0, 0xA7, 0xC6, 0xF2, 0x93, 0x58, 0xCA, 0xB9,
  28332. 0x11, 0x08, 0x77, 0x32, 0xA6, 0x49, 0xD2, 0x7C,
  28333. 0x9B, 0x98, 0xF9, 0xA4, 0x88, 0x79, 0x38, 0x7D,
  28334. 0x9B, 0x00, 0xC2, 0x59, 0x59, 0xA7, 0x16, 0x54,
  28335. 0xD6, 0xF6, 0xA9, 0x46, 0x16, 0x45, 0x13, 0xE4,
  28336. 0x7A, 0x75, 0xD0, 0x05, 0x98, 0x6C, 0x23, 0x63,
  28337. 0xC0, 0x9F, 0x6B, 0x53, 0x7E, 0xCA, 0x78, 0xB9,
  28338. 0x30, 0x3A, 0x5F, 0xA4, 0x57, 0x60, 0x8A, 0x58,
  28339. 0x6A, 0x65, 0x3A, 0x34, 0x7D, 0xB0, 0x4D, 0xFC,
  28340. 0xC1, 0x91, 0x75, 0xB3, 0xA3, 0x01, 0x17, 0x25,
  28341. 0x36, 0x06, 0x2A, 0x65, 0x8A, 0x95, 0x27, 0x75,
  28342. 0x70, 0xC8, 0x85, 0x2C, 0xA8, 0x97, 0x3F, 0x4A,
  28343. 0xE1, 0x23, 0xA3, 0x34, 0x04, 0x7D, 0xD7, 0x11,
  28344. 0xC8, 0x92, 0x7A, 0x63, 0x4A, 0x03, 0x38, 0x8A,
  28345. 0x52, 0x7B, 0x03, 0x4B, 0xF7, 0xA8, 0x17, 0x0F,
  28346. 0xA7, 0x02, 0xC1, 0xF7, 0xC2, 0x3E, 0xC3, 0x2D,
  28347. 0x18, 0xA2, 0x37, 0x48, 0x90, 0xBE, 0x9C, 0x78,
  28348. 0x7A, 0x94, 0x09, 0xC8, 0x2D, 0x19, 0x2C, 0x4B,
  28349. 0xB7, 0x05, 0xA2, 0xF9, 0x96, 0xCE, 0x40, 0x5D,
  28350. 0x85, 0xA4, 0xC1, 0xA1, 0xAB, 0x9B, 0x6A, 0xEB,
  28351. 0x49, 0xCC, 0xE1, 0xC2, 0xF8, 0xA9, 0x7C, 0x35,
  28352. 0x16, 0xC7, 0x2A, 0x00, 0xA4, 0x62, 0x63, 0xBA,
  28353. 0xA6, 0x96, 0xBF, 0x25, 0x72, 0x77, 0x19, 0xC3,
  28354. 0x21, 0x64, 0x23, 0x61, 0x8F, 0xF3, 0x33, 0x80,
  28355. 0x93, 0x4A, 0x6C, 0x10, 0x54, 0x5C, 0x4C, 0x5C,
  28356. 0x51, 0x55, 0xB1, 0x24, 0x86, 0x18, 0x1F, 0xC7,
  28357. 0xA2, 0x31, 0x98, 0x73, 0x97, 0x8B, 0x6A, 0x2A,
  28358. 0x67, 0x49, 0x0F, 0x82, 0x56, 0xBD, 0x21, 0x96,
  28359. 0xFE, 0x17, 0x92, 0xA4, 0xC0, 0x00, 0x77, 0xB8,
  28360. 0x12, 0xEA, 0xE8, 0xBE, 0xD3, 0x57, 0x24, 0x99,
  28361. 0x68, 0x4A, 0xB3, 0x37, 0x18, 0x76, 0x76, 0x1E,
  28362. 0x45, 0x0C, 0x9F, 0x9D, 0x27, 0x68, 0xA3, 0x68,
  28363. 0x06, 0xD7, 0xAB, 0x20, 0x46, 0xC9, 0x1F, 0x17,
  28364. 0x59, 0x9E, 0x9A, 0xC5, 0x92, 0x99, 0x08, 0x08,
  28365. 0xDC, 0xD7, 0xB4, 0xD0, 0x91, 0x90, 0x72, 0xF1,
  28366. 0x4E, 0xC3, 0x61, 0x77, 0x3B, 0x72, 0x52, 0x44,
  28367. 0x4C, 0x32, 0x3C, 0x30, 0x83, 0x26, 0xF4, 0xA3,
  28368. 0x0F, 0x86, 0x80, 0xD2, 0xF7, 0x48, 0xF5, 0x6A,
  28369. 0x13, 0x2B, 0x82, 0x67, 0x4E, 0xD0, 0x18, 0x46,
  28370. 0x20, 0xB8, 0x2A, 0xD2, 0xCB, 0x18, 0x2C, 0x97,
  28371. 0xB4, 0x81, 0x62, 0x66, 0x47, 0x49, 0x12, 0x90,
  28372. 0xA0, 0x11, 0xCC, 0x73, 0x82, 0x86, 0x85, 0xA8,
  28373. 0xC3, 0x67, 0xA5, 0xB9, 0xCF, 0x8D, 0x62, 0x1B,
  28374. 0x0D, 0x5C, 0x1E, 0xFF, 0x03, 0x17, 0x27, 0x58,
  28375. 0xBD, 0x00, 0x49, 0x78, 0xC2, 0x51, 0xCD, 0x51,
  28376. 0x34, 0x22, 0x28, 0x98, 0x9C, 0xAE, 0x63, 0x32,
  28377. 0xAC, 0x48, 0x64, 0x37, 0xCB, 0x5C, 0x57, 0xD4,
  28378. 0x30, 0x74, 0x62, 0x86, 0x52, 0x53, 0xBE, 0x21,
  28379. 0x7B, 0x35, 0x15, 0xC7, 0x3D, 0xF4, 0x05, 0xB7,
  28380. 0xF2, 0x82, 0x17, 0xAD, 0x0B, 0x8C, 0xF6, 0x0C,
  28381. 0x2F, 0xFF, 0xAA, 0x0A, 0x00, 0x48, 0xB1, 0xFB,
  28382. 0x4A, 0xCD, 0xCD, 0xC3, 0x8B, 0x52, 0x50, 0xCF,
  28383. 0xEC, 0x35, 0x6A, 0x6D, 0xE2, 0x6C, 0xFA, 0x7A,
  28384. 0x58, 0x8F, 0xDC, 0x86, 0xF9, 0x8C, 0x85, 0x4A,
  28385. 0xC6, 0x4C, 0x7B, 0xFA, 0xA9, 0x6F, 0x5A, 0x32,
  28386. 0xCC, 0x06, 0x10, 0x93, 0x4B, 0xAA, 0x6A, 0x58,
  28387. 0x6B, 0x9A, 0x20, 0x54, 0xF1, 0x3B, 0xA2, 0x74,
  28388. 0x17, 0x4A, 0xA0, 0xD2, 0xB3, 0xA8, 0x1B, 0x96,
  28389. 0xA9, 0x40, 0x66, 0x6F, 0x78, 0x9B, 0x5A, 0x6B,
  28390. 0xCD, 0xC0, 0xA6, 0xA0, 0x17, 0x8A, 0x0C, 0x9A,
  28391. 0x02, 0x57, 0x8A, 0x49, 0x3F, 0x6E, 0xEA, 0x0D,
  28392. 0x2E, 0x6C, 0x13, 0x95, 0x1C, 0x9F, 0x24, 0x9A,
  28393. 0x5E, 0x8D, 0xD7, 0x1D, 0xD4, 0x9A, 0x74, 0x2D,
  28394. 0x45, 0x1F, 0x1A, 0xBB, 0xA1, 0x9A, 0xF8, 0xC5,
  28395. 0x47, 0x85, 0x5E, 0x0A, 0xFC, 0x72, 0x8E, 0x90,
  28396. 0xAB, 0xB4, 0x99, 0xC9, 0xBE, 0xEB, 0x76, 0x6F,
  28397. 0x47, 0x29, 0xCD, 0xA2, 0x22, 0x63, 0xE3, 0x24,
  28398. 0xD2, 0x23, 0x02, 0xCB, 0xD3, 0x39, 0x9F, 0xAC,
  28399. 0xC6, 0x30, 0x99, 0x1F, 0xC8, 0xF2, 0x8B, 0xDB,
  28400. 0x43, 0x54, 0x76, 0x25, 0x41, 0x52, 0x76, 0x78,
  28401. 0xBC, 0xF6, 0x1F, 0x65, 0xC2, 0x41, 0x14, 0x6C,
  28402. 0x42, 0x6D, 0x23, 0xB9, 0xBF, 0xAA, 0x6B, 0x7D,
  28403. 0xF1, 0x8C, 0x97, 0xF2, 0x0C, 0x1B, 0x61, 0x25,
  28404. 0xBF, 0x87, 0x4B, 0x1D, 0x89, 0x47, 0x58, 0x52,
  28405. 0xC4, 0x48, 0x21, 0x5D, 0xB0, 0xEB, 0x77, 0x37,
  28406. 0xF9, 0x14, 0x80, 0xE8, 0xCE, 0xBD, 0x9A, 0x08,
  28407. 0x71, 0x57, 0x4F, 0x5A, 0xB6, 0x2D, 0x90, 0x20,
  28408. 0x17, 0x5E, 0xC6, 0x92, 0x7C, 0xA0, 0xB5, 0x4C,
  28409. 0x09, 0x81, 0x8E, 0x42, 0xCF, 0x92, 0xA3, 0x83,
  28410. 0x17, 0x24, 0x22, 0xC7, 0xDC, 0x18, 0x31, 0xD6,
  28411. 0x3B, 0x0C, 0x29, 0x5D, 0xE7, 0x51, 0x59, 0xDB,
  28412. 0x80, 0x34, 0xE9, 0xE0, 0x7F, 0x7B, 0x0B, 0x91,
  28413. 0x0C, 0x3C, 0x1E, 0x5F, 0xB6, 0x6B, 0x3D, 0xC5,
  28414. 0x23, 0xF1, 0xFA, 0x6E, 0xB4, 0x91, 0x0C, 0xB8,
  28415. 0x9A, 0x6C, 0x17, 0x56, 0x2C, 0x83, 0xAB, 0x4C,
  28416. 0x18, 0xD0, 0xCD, 0x7E, 0x07, 0x96, 0x59, 0x2A,
  28417. 0x37, 0x2A, 0xA4, 0x09, 0xB1, 0xC5, 0x57, 0x34,
  28418. 0x7C, 0xCA, 0xCD, 0xC4, 0x64, 0x4A, 0x11, 0x90,
  28419. 0x64, 0xD0, 0x6D, 0xD4, 0x74, 0x92, 0x9D, 0x1C,
  28420. 0x6F, 0xB4, 0xD6, 0x86, 0xE5, 0x49, 0x1C, 0xE4,
  28421. 0xBC, 0x89, 0xA3, 0x0B, 0xB4, 0xB8, 0xC4, 0x1B,
  28422. 0xCE, 0x51, 0x57, 0xDF, 0xC1, 0x36, 0x08, 0x23,
  28423. 0xB1, 0xAB, 0x61, 0x8C, 0x14, 0xB1, 0x0F, 0x98,
  28424. 0xC2, 0x50, 0x67, 0x39, 0x8E, 0xA7, 0x01, 0x8C,
  28425. 0x27, 0x8A, 0x4B, 0x3D, 0xF3, 0x13, 0x34, 0xD6,
  28426. 0x03, 0xB2, 0x04, 0x4E, 0xF1, 0x87, 0xCD, 0x9B,
  28427. 0xC6, 0xCE, 0x42, 0x72, 0x5B, 0xD9, 0x62, 0xC2,
  28428. 0x64, 0x98, 0x3E, 0x9E, 0x18, 0x15, 0x5A, 0x8B,
  28429. 0x9C, 0x47, 0x14, 0x3D, 0x70, 0x46, 0x0A, 0x26,
  28430. 0xA5, 0x6F, 0xE7, 0x65, 0x8C, 0x1F, 0x15, 0x03,
  28431. 0x48, 0xC6, 0x08, 0x7E, 0xF7, 0x58, 0xAD, 0x16,
  28432. 0x78, 0x87, 0x86, 0x0A, 0x00, 0x7A, 0x5F, 0xC3,
  28433. 0x73, 0x58, 0xD4, 0x3B, 0x5E, 0xBE, 0xE8, 0x20,
  28434. 0xAC, 0xEA, 0x47, 0x4F, 0x0A, 0xC0, 0x7B, 0x76,
  28435. 0x80, 0x28, 0x66, 0x19, 0x9C, 0x61, 0x23, 0x1D,
  28436. 0x5C, 0x74, 0x7C, 0x93, 0x77, 0x4D, 0x2C, 0x1E,
  28437. 0x0C, 0x1C, 0x67, 0xE6, 0xC8, 0x1B, 0x82, 0x75,
  28438. 0x21, 0x73, 0xE1, 0x25, 0xBA, 0xF3, 0x9B, 0x4F,
  28439. 0xD1, 0x9A, 0x4F, 0x45, 0x3D, 0xC5, 0x79, 0x76,
  28440. 0xB1, 0xD9, 0x7F, 0xE6, 0x99, 0x69, 0x92, 0xBB,
  28441. 0xB6, 0x5B, 0x7C, 0xB2, 0x5D, 0x07, 0x7B, 0xBA,
  28442. 0xA6, 0xA1, 0x33, 0x22, 0x89, 0x9A, 0xF6, 0x59,
  28443. 0xCF, 0x1B, 0x35, 0x58, 0xC1, 0xB5, 0x00, 0x11,
  28444. 0x54, 0xB6, 0x25, 0x80, 0x9E, 0xD8, 0x9A, 0xEE,
  28445. 0xBB, 0x89, 0xE6, 0xEA, 0x7D, 0x67, 0xF7, 0x23,
  28446. 0xD0, 0x45, 0xAB, 0x05, 0x71, 0x5C, 0x42, 0x35,
  28447. 0x5D, 0xA6, 0xA5, 0xC8, 0xDD, 0x39, 0xC8, 0xAB,
  28448. 0xE3, 0x03, 0x77, 0x51, 0xA0, 0x1E, 0xD1, 0xC7,
  28449. 0x37, 0x49, 0x19, 0xF3, 0x12, 0x1B, 0x5A, 0x52,
  28450. 0xC5, 0x3D, 0x14, 0x87, 0x31, 0x67, 0x69, 0xF8,
  28451. 0x07, 0x21, 0xDE, 0xEA, 0xAA, 0xD3, 0xC9, 0x0F,
  28452. 0x76, 0xE7, 0xAE, 0x9E, 0x12, 0xBA, 0x92, 0xB3,
  28453. 0x2B, 0x5F, 0xD4, 0x57, 0xE3, 0xC7, 0x52, 0xC2,
  28454. 0x65, 0x0D, 0xFB, 0x88, 0x57, 0x71, 0xCB, 0x77,
  28455. 0xAC, 0x3C, 0x78, 0x5A, 0x8C, 0x56, 0x2E, 0x6A,
  28456. 0x1C, 0x63, 0xC2, 0xA5, 0x5E, 0xA4, 0x7C, 0xF8,
  28457. 0xB9, 0x0E, 0xB8, 0x22, 0x5C, 0x12, 0x3C, 0x34,
  28458. 0x64, 0x52, 0x56, 0x62, 0x35, 0xB2, 0xF3, 0x18,
  28459. 0x23, 0xA3, 0x35, 0x21, 0xE0, 0x87, 0x93, 0x7A,
  28460. 0x34, 0x5D, 0x8D, 0x66, 0x3E, 0xEA, 0xA0, 0x56,
  28461. 0x58, 0x91, 0x7B, 0xBA, 0xA0, 0x08, 0xC2, 0xE3,
  28462. 0x35, 0xF8, 0x85, 0x0A, 0x90, 0xA3, 0x26, 0xD0,
  28463. 0xE6, 0x64, 0x32, 0xF4, 0x4C, 0xEB, 0x82, 0x89,
  28464. 0xE4, 0xEC, 0xB2, 0xD1, 0x29, 0x58, 0xE9, 0x84,
  28465. 0x07, 0x2E, 0xCA, 0xCB, 0x88, 0xE1, 0x34, 0x8F,
  28466. 0xF0, 0xB5, 0x56, 0x54, 0xAC, 0xBA, 0x5B, 0x54,
  28467. 0x97, 0x1C, 0xBA, 0xEB, 0xA8, 0x8E, 0xC4, 0xB9,
  28468. 0x1A, 0x94, 0xC3, 0x71, 0x92, 0xFA, 0x98, 0x2B,
  28469. 0xEC, 0xB9, 0xF3, 0xDA, 0x42, 0x16, 0x03, 0xB6,
  28470. 0x1A, 0x51, 0xBC, 0x8E, 0x36, 0xCB, 0xD0, 0x53,
  28471. 0x85, 0x1C, 0x77, 0xB1, 0xB9, 0x26, 0xB1, 0x7A,
  28472. 0x27, 0x2A, 0xA9, 0x02, 0x32, 0x46, 0xB0, 0x2B,
  28473. 0x3E, 0xD4, 0x7F, 0x66, 0xA0, 0x0B, 0xD5, 0x68,
  28474. 0x48, 0x23, 0x63, 0x4E, 0x7C, 0xE5, 0x8C, 0xF8,
  28475. 0xF3, 0x06, 0xE3, 0x5B, 0x1E, 0x53, 0x22, 0x82,
  28476. 0x4D, 0x90, 0x48, 0x01, 0xF0, 0xA2, 0xFA, 0x7C,
  28477. 0x2B, 0xC9, 0xC2, 0x52, 0xB0, 0xA5, 0x6B, 0x7B,
  28478. 0xA2, 0xAB, 0x0F, 0x63, 0x60, 0x21, 0x74, 0x5A,
  28479. 0x70, 0xA9, 0xA4, 0x3E, 0x2B, 0x0A, 0x8D, 0x61,
  28480. 0x59, 0x70, 0xB6, 0x53, 0x09, 0x62, 0x4B, 0x51,
  28481. 0x84, 0xBC, 0xC3, 0x0B, 0x91, 0x16, 0x79, 0xAE,
  28482. 0xDD, 0x76, 0x02, 0x5F, 0xE3, 0x90, 0x8F, 0xD6,
  28483. 0x78, 0x97, 0xB0, 0xCF, 0x4B, 0xE5, 0xA6, 0xF5,
  28484. 0x41, 0x3D, 0x7D, 0xD9, 0x85, 0x64, 0xB2, 0x3E,
  28485. 0x42, 0xA9, 0x3E, 0x4A, 0xA8, 0x82, 0x1C, 0xD4,
  28486. 0x50, 0x54, 0xC6, 0x43, 0xED, 0xC1, 0x15, 0x8D,
  28487. 0xB6, 0xB3, 0xDE, 0xB1, 0x3F, 0xB5, 0xA5, 0x1E,
  28488. 0xBD, 0x1A, 0x8A, 0x78, 0xB8, 0x72, 0x25, 0xA7,
  28489. 0x33, 0x8E, 0x10, 0x11, 0x04, 0xC4, 0xA2, 0x20,
  28490. 0xD9, 0xBD, 0xED, 0xD4, 0x8C, 0x85, 0xA1, 0xC2,
  28491. 0xDA, 0xE7, 0x81, 0xA8, 0x0C, 0x40, 0xE1, 0x3B,
  28492. 0x87, 0xEA, 0xC7, 0x3A, 0x76, 0x42, 0x01, 0xC9,
  28493. 0xB7, 0x60, 0xCC, 0xFB, 0x1A, 0xE3, 0x92, 0x69,
  28494. 0x9C, 0x70, 0x39, 0xD2, 0x7C, 0x39, 0x36, 0x2B,
  28495. 0x27, 0xB8, 0xFC, 0x6F, 0x07, 0xA8, 0xA3, 0xD4,
  28496. 0x41, 0x0F, 0x15, 0x47, 0xC4, 0x8A, 0x99, 0x97,
  28497. 0xF6, 0x2C, 0x61, 0x07, 0x44, 0x52, 0xEF, 0x15,
  28498. 0x15, 0xF8, 0xA6, 0x49, 0xEB, 0xCA, 0x94, 0x37,
  28499. 0x20, 0x5A, 0x4E, 0x8A, 0x61, 0x60, 0x6B, 0x41,
  28500. 0xDA, 0xF6, 0x83, 0x4D, 0x67, 0x1F, 0x4D, 0x85,
  28501. 0x2C, 0x0C, 0x9C, 0x40, 0x96, 0x61, 0x16, 0x48,
  28502. 0xC6, 0xA3, 0x17, 0x06, 0x78, 0xB1, 0x53, 0x7C,
  28503. 0xC1, 0x82, 0x8D, 0x93, 0x58, 0x0C, 0x9E, 0x58,
  28504. 0x49, 0xA9, 0x65, 0x31, 0x75, 0xAC, 0xB7, 0x53,
  28505. 0xF2, 0xBE, 0x74, 0x37, 0xBE, 0x45, 0xF6, 0xC6,
  28506. 0x03, 0xE4, 0x85, 0xF2, 0xEC, 0x30, 0x1B, 0xB4,
  28507. 0x2B, 0x6C, 0x37, 0xC2, 0x25, 0xD7, 0x49, 0x5A,
  28508. 0x58, 0x4A, 0xE2, 0x31, 0x89, 0x0A, 0xB5, 0xC8,
  28509. 0xC3, 0x5C, 0x26, 0x8C, 0xF4, 0xBB, 0xB0, 0x21,
  28510. 0x3C, 0x09, 0x60, 0x19, 0x31, 0x95, 0x61, 0xA8,
  28511. 0xA6, 0x94, 0x76, 0x37, 0xAA, 0x40, 0xD0, 0x06,
  28512. 0xB4, 0x15, 0xBB, 0x2C, 0xFA, 0x22, 0x37, 0xE0,
  28513. 0x89, 0x0B, 0x6A, 0x3B, 0xC1, 0x34, 0xAB, 0xF8,
  28514. 0xF6, 0x58, 0x5E, 0x10, 0x8D, 0x15, 0x94, 0x0F,
  28515. 0x91, 0xF4, 0xBF, 0x5B, 0x0C, 0x81, 0x80, 0x55,
  28516. 0xB2, 0x1D, 0xEA, 0x6E, 0x63, 0xB5, 0x53, 0x98,
  28517. 0x8C, 0x47, 0xF4, 0xB9, 0x4E, 0x7C, 0xF8, 0x00,
  28518. 0xA4, 0x93, 0xB4, 0x73, 0x47, 0x05, 0xED, 0xC5,
  28519. 0x6A, 0x4B, 0x60, 0x21, 0xC6, 0x29, 0x50, 0x06,
  28520. 0x75, 0x87, 0x68, 0x04, 0xCF, 0x0B, 0x95, 0x1F,
  28521. 0x03, 0x8A, 0x5C, 0x7F, 0xE5, 0x8E, 0x89, 0x77,
  28522. 0x4E, 0xF2, 0x99, 0x2F, 0xD7, 0xC6, 0x30, 0x99,
  28523. 0xD3, 0x52, 0xA7, 0xD2, 0x15, 0x60, 0xB7, 0x88,
  28524. 0xB4, 0x05, 0x70, 0x98, 0x61, 0x81, 0x7E, 0x59,
  28525. 0xA9, 0x6B, 0x3A, 0x3A, 0x83, 0xCB, 0xA8, 0x03,
  28526. 0xB1, 0x69, 0x34, 0x33, 0x10, 0x71, 0x90, 0x5B,
  28527. 0xBE, 0xC6, 0x53, 0x29, 0x00, 0x15, 0x5D, 0x8A,
  28528. 0xC8, 0x8C, 0xB3, 0x2E, 0x4E, 0x21, 0xA3, 0xBD,
  28529. 0x3A, 0x03, 0xFD, 0xEC, 0x32, 0x5A, 0x51, 0xCD,
  28530. 0x27, 0x73, 0x96, 0x4E, 0x67, 0x84, 0xFC, 0xF1,
  28531. 0x85, 0x37, 0x37, 0xAA, 0x64, 0xEB, 0x67, 0x56,
  28532. 0x47, 0x27, 0x27, 0x26, 0x61, 0xAB, 0xF8, 0x43,
  28533. 0x13, 0xA5, 0x7A, 0x44, 0xB1, 0x23, 0xC6, 0x55,
  28534. 0x09, 0xCF, 0xB7, 0xA6, 0xF6, 0x64, 0x1C, 0xDC,
  28535. 0xC3, 0xB5, 0x7F, 0xE6, 0x28, 0xC7, 0xB8, 0x19,
  28536. 0x2D, 0xB4, 0x4F, 0xFB, 0xF5, 0x79, 0x6A, 0x86,
  28537. 0x13, 0xB1, 0xFA, 0x12, 0x6F, 0x60, 0x76, 0x88,
  28538. 0x3C, 0x78, 0x3D, 0xC2, 0x4E, 0x2A, 0x44, 0x64,
  28539. 0xC4, 0x0B, 0x3A, 0x41, 0xCA, 0x70, 0xAE, 0x87,
  28540. 0x62, 0x08, 0x66, 0xCF, 0x4F, 0xCB, 0x2B, 0xD2,
  28541. 0x04, 0xBF, 0x5C, 0x28, 0x38, 0x12, 0xBA, 0x05,
  28542. 0x6A, 0xC0, 0xC3, 0x45, 0xE3, 0x79, 0xC4, 0xBA,
  28543. 0x24, 0xD7, 0x50, 0x90, 0x12, 0x79, 0xBB, 0x2F,
  28544. 0x3A, 0x16, 0xF6, 0x12, 0xBF, 0xAD, 0xB3, 0x57,
  28545. 0x03, 0x33, 0x2C, 0x7C, 0x13, 0x6F, 0x68, 0xEA,
  28546. 0xB6, 0x75, 0x5C, 0x66, 0xB6, 0xA4, 0xAD, 0x1A,
  28547. 0xAB, 0xA7, 0xB7, 0x68, 0xA5, 0x8A, 0xCA, 0xAC,
  28548. 0xC1, 0x0A, 0x45, 0x9A, 0x1C, 0xC8, 0xEF, 0x29,
  28549. 0x37, 0x7B, 0xC2, 0x00, 0xE4, 0xD3, 0x15, 0xA3,
  28550. 0x0A, 0x6B, 0xCC, 0x32, 0x56, 0xF9, 0x73, 0x4D,
  28551. 0x06, 0xE9, 0x77, 0x9C, 0xAA, 0x54, 0x42, 0xA9,
  28552. 0xA1, 0x60, 0x69, 0x08, 0x13, 0x77, 0xC7, 0x6E,
  28553. 0x75, 0x15, 0x43, 0x68, 0x07, 0x2D, 0xC4, 0x46,
  28554. 0xED, 0x6C, 0x8B, 0x8E, 0x62, 0x2A, 0x21, 0xE3,
  28555. 0x83, 0xCF, 0x9B, 0xA1, 0xFB, 0x43, 0x4E, 0x2E,
  28556. 0xCC, 0x81, 0xE7, 0xB7, 0x8C, 0xEE, 0x98, 0x6B,
  28557. 0x8F, 0xF7, 0x98, 0xAB, 0x18, 0xCF, 0x96, 0x34,
  28558. 0x54, 0x35, 0x46, 0x28, 0x4E, 0xDA, 0x2A, 0x26,
  28559. 0xB4, 0x7F, 0x05, 0xB7, 0x35, 0xBC, 0xDB, 0x12,
  28560. 0x02, 0x22, 0x00, 0x76, 0xDC, 0x8B, 0x4E, 0x4B,
  28561. 0x9F, 0x85, 0x35, 0x33, 0xC8, 0xF6, 0xC7, 0xFF,
  28562. 0x38, 0x81, 0x7B, 0xA4, 0x97, 0x12, 0x83, 0x57,
  28563. 0x85, 0xF1, 0x7F, 0x14, 0xCA, 0x01, 0xD0, 0xC1,
  28564. 0xC1, 0xE9, 0x88, 0x10, 0xFE, 0x0B, 0x36, 0xE5,
  28565. 0xB4, 0x27, 0x15, 0x7B, 0x94, 0x18, 0x44, 0x9C,
  28566. 0xED, 0xD6, 0x41, 0xA4, 0x29, 0x3C, 0x85, 0xC3,
  28567. 0x27, 0x00, 0x10, 0x2A, 0xCE, 0xC2, 0x2E, 0xBA,
  28568. 0xD9, 0x8E, 0xD1, 0x60, 0xA5, 0xF0, 0x27, 0xBD,
  28569. 0x4C, 0xDA, 0x57, 0xF1, 0xF3, 0x72, 0x0A, 0x12,
  28570. 0xC1, 0x34, 0x65, 0x4D, 0xD5, 0xE7, 0x3F, 0x82,
  28571. 0x96, 0x76, 0x49, 0x53, 0x90, 0xD0, 0xE7, 0x92,
  28572. 0x9D, 0x60, 0x34, 0xE9, 0xC5, 0x5F, 0x7D, 0x55,
  28573. 0xBA, 0x65, 0x8B, 0xC5, 0x87, 0x98, 0x8E, 0x8A,
  28574. 0xF9, 0x49, 0x60, 0xF6, 0xCF, 0xB8, 0xD5, 0xAF,
  28575. 0x7A, 0x00, 0x21, 0x53, 0x5A, 0x6E, 0x25, 0xE4,
  28576. 0x37, 0xD4, 0x9A, 0x78, 0x06, 0x98, 0xBE, 0x22,
  28577. 0xAC, 0x99, 0x53, 0x94, 0x9F, 0x57, 0x1B, 0x85,
  28578. 0xA6, 0x85, 0x72, 0x5F, 0x82, 0x07, 0xA2, 0xB0,
  28579. 0xAE, 0x84, 0x9B, 0x60, 0x1A, 0xB9, 0x1B, 0x15,
  28580. 0x9B, 0x3D, 0xF4, 0xA1, 0x54, 0xC2, 0x04, 0x1E,
  28581. 0x77, 0x60, 0x70, 0xAF, 0xC4, 0x29, 0x69, 0x32,
  28582. 0x23, 0x80, 0x91, 0x7C, 0x97, 0x51, 0x07, 0x99,
  28583. 0xF3, 0x14, 0x91, 0x31, 0x47, 0x7E, 0x16, 0x66,
  28584. 0x3D, 0x31, 0x74, 0xC7, 0xC1, 0xCA, 0xEA, 0x78,
  28585. 0x85, 0x35, 0xC6, 0xC0, 0x05, 0xA6, 0x4F, 0x28,
  28586. 0x68, 0x63, 0x1B, 0x31, 0xB6, 0x6E, 0x20, 0x5F,
  28587. 0xD3, 0x8C, 0x1D, 0x84, 0x54, 0x2D, 0x0F, 0x1B,
  28588. 0x57, 0x8F, 0x58, 0xC9, 0xBF, 0x5A, 0x0F, 0xAE,
  28589. 0xAB, 0x6A, 0xB6, 0x49, 0x48, 0x93, 0x05, 0x31,
  28590. 0x65, 0xEA, 0xFD, 0x46, 0x5F, 0xC6, 0x4A, 0x0C,
  28591. 0x5F, 0x8F, 0x3F, 0x90, 0x03, 0x48, 0x94, 0x15,
  28592. 0x89, 0x9D, 0x59, 0xA5, 0x43, 0xD8, 0x20, 0x8C,
  28593. 0x54, 0xA3, 0x16, 0x65, 0x29, 0xB5, 0x39, 0x22,
  28594. 0x8A, 0x39, 0xE8, 0x7D, 0x53, 0x1F, 0x35, 0x27,
  28595. 0xC2, 0x07, 0xED, 0xCC, 0x1D, 0xB7, 0xFA, 0xDD,
  28596. 0xCF, 0x96, 0x28, 0x39, 0x18, 0x79, 0xB3, 0x35,
  28597. 0xC7, 0x07, 0x83, 0x9A, 0x0D, 0xB0, 0x51, 0xA8,
  28598. 0x86, 0x26, 0xED, 0x79, 0xD4, 0x51, 0x14, 0x08,
  28599. 0x00, 0xE0, 0x3B, 0x59, 0xB9, 0x56, 0xF8, 0x21,
  28600. 0x0E, 0x55, 0x60, 0x67, 0x40, 0x7D, 0x13, 0xDC,
  28601. 0x90, 0xFA, 0x9E, 0x8B, 0x87, 0x2B, 0xFB, 0x8F
  28602. };
  28603. const byte kyber1024_ct[] = {
  28604. 0xA6, 0xAF, 0x29, 0xD5, 0xF5, 0xB8, 0x0B, 0xD1,
  28605. 0x30, 0xF5, 0x18, 0xBA, 0xDD, 0xD6, 0xC8, 0xF1,
  28606. 0x75, 0x45, 0x41, 0x3D, 0x86, 0x0F, 0xB3, 0xDE,
  28607. 0x45, 0x19, 0x79, 0xEB, 0xFA, 0x5E, 0x4E, 0x31,
  28608. 0x12, 0xC7, 0xC0, 0xAD, 0xF9, 0x98, 0x24, 0xBB,
  28609. 0x52, 0x6F, 0x2C, 0x35, 0x50, 0x74, 0x8E, 0xD0,
  28610. 0xE1, 0x34, 0xF0, 0x45, 0x7A, 0x7C, 0x61, 0xF9,
  28611. 0xF5, 0x26, 0xF0, 0x02, 0xBA, 0xAD, 0xC0, 0x3F,
  28612. 0xC1, 0x3E, 0x38, 0x13, 0x12, 0x19, 0x51, 0x3C,
  28613. 0x3E, 0xDE, 0x06, 0x16, 0x61, 0xE7, 0x4F, 0x60,
  28614. 0x3C, 0x4F, 0xCF, 0x79, 0x51, 0xC8, 0xE5, 0x2C,
  28615. 0x9C, 0x21, 0x3B, 0x0D, 0x22, 0xD9, 0x29, 0x36,
  28616. 0x63, 0xD6, 0x69, 0xA6, 0xB5, 0x8E, 0xD8, 0xFC,
  28617. 0xEF, 0xCF, 0x82, 0x49, 0xD7, 0xBB, 0x52, 0x98,
  28618. 0xF5, 0x57, 0x61, 0x44, 0x5B, 0x2B, 0x83, 0xCE,
  28619. 0x7F, 0x00, 0x5C, 0xB0, 0x42, 0x48, 0xAE, 0xC8,
  28620. 0xBD, 0xA2, 0x2F, 0xD2, 0xD4, 0x2A, 0xA7, 0x66,
  28621. 0x32, 0x20, 0x14, 0xEA, 0x03, 0x8C, 0xC3, 0x2C,
  28622. 0x55, 0xC8, 0xE4, 0xB9, 0xE2, 0x8E, 0xC9, 0x11,
  28623. 0x9F, 0x52, 0x73, 0x41, 0xE4, 0xF6, 0x6A, 0x03,
  28624. 0x51, 0x21, 0x07, 0x3B, 0x85, 0xDE, 0x67, 0x06,
  28625. 0xDA, 0x19, 0xE0, 0x83, 0x8A, 0x9F, 0x33, 0xB7,
  28626. 0x19, 0xA6, 0x8F, 0x03, 0x9B, 0x66, 0x4D, 0xC0,
  28627. 0x02, 0x65, 0x9E, 0xAB, 0xFC, 0x39, 0x86, 0x79,
  28628. 0xAA, 0x70, 0x09, 0xCE, 0x0C, 0xD0, 0x1C, 0xDA,
  28629. 0xFB, 0x6C, 0xD2, 0xA2, 0x6F, 0xE4, 0x10, 0x16,
  28630. 0x72, 0xC9, 0x8F, 0xF5, 0x8F, 0x7C, 0x47, 0xD5,
  28631. 0xBD, 0xA2, 0x90, 0x66, 0x53, 0xB3, 0xA6, 0xF9,
  28632. 0x65, 0x1F, 0x7A, 0x12, 0x1E, 0xA7, 0x7E, 0xA7,
  28633. 0x47, 0x23, 0xFA, 0xE5, 0xB8, 0x73, 0xF9, 0xBB,
  28634. 0x7B, 0x66, 0x4F, 0x0C, 0x8A, 0x93, 0x83, 0x1E,
  28635. 0xF9, 0xD5, 0x1C, 0x7C, 0xC1, 0xEF, 0x44, 0xAC,
  28636. 0x0E, 0x55, 0xA5, 0x5C, 0xA7, 0x6D, 0x13, 0x7F,
  28637. 0xE9, 0xB7, 0x5F, 0x40, 0x50, 0x9C, 0xEF, 0x15,
  28638. 0x6E, 0x5A, 0xD1, 0x8F, 0x9F, 0xB9, 0x99, 0x68,
  28639. 0x00, 0x08, 0xE5, 0x47, 0xD5, 0x5E, 0xEC, 0xD5,
  28640. 0xB4, 0xD1, 0xCB, 0x1D, 0x9F, 0x07, 0x6C, 0xEC,
  28641. 0x21, 0x50, 0x1C, 0x74, 0x02, 0x50, 0x9E, 0xCB,
  28642. 0x77, 0xAF, 0xB2, 0xCB, 0x9A, 0x61, 0x34, 0x0A,
  28643. 0x8B, 0xD1, 0x51, 0x4C, 0x6E, 0x71, 0xB4, 0xAA,
  28644. 0x45, 0xE4, 0x7E, 0xC3, 0x75, 0x12, 0x27, 0x1B,
  28645. 0x91, 0x1F, 0x8F, 0xB4, 0x6C, 0x90, 0x82, 0xC9,
  28646. 0xDF, 0x07, 0x20, 0x4A, 0xBB, 0x5A, 0x50, 0xE6,
  28647. 0xE3, 0x64, 0x7A, 0x8A, 0xD4, 0xD8, 0xD5, 0xD7,
  28648. 0xBF, 0xF1, 0x9C, 0x8A, 0x50, 0x93, 0x08, 0xBC,
  28649. 0xFB, 0x89, 0x55, 0x36, 0xD0, 0x45, 0xCA, 0x2B,
  28650. 0x97, 0xCB, 0x16, 0xA2, 0x9B, 0xB7, 0x18, 0x1C,
  28651. 0xAD, 0x05, 0x09, 0xDD, 0xB9, 0x17, 0x35, 0x02,
  28652. 0x8E, 0xBA, 0x8C, 0x31, 0xD7, 0x4B, 0xD2, 0x75,
  28653. 0xEA, 0xA6, 0x5B, 0x53, 0x40, 0xB3, 0xA4, 0x3F,
  28654. 0xBF, 0xE0, 0xB3, 0x06, 0x1D, 0x6B, 0xAE, 0x7E,
  28655. 0x75, 0xB7, 0x09, 0x8C, 0xDA, 0xBE, 0x91, 0xD4,
  28656. 0xB3, 0x1E, 0x36, 0xC9, 0xAA, 0x7A, 0x82, 0x98,
  28657. 0x86, 0x2A, 0xD6, 0x3C, 0x8F, 0xD2, 0x82, 0xE0,
  28658. 0x3B, 0x46, 0x0B, 0x3A, 0xB4, 0x64, 0xCE, 0x0F,
  28659. 0x27, 0xB1, 0xC3, 0xD1, 0x11, 0x55, 0xAC, 0xAA,
  28660. 0x01, 0x1E, 0xB9, 0xE2, 0xAE, 0x3E, 0x6D, 0xDA,
  28661. 0x07, 0xD6, 0xF4, 0x91, 0x73, 0x7C, 0xBC, 0xE9,
  28662. 0xB0, 0x5F, 0x9B, 0xC5, 0x6B, 0xE2, 0x0E, 0x8D,
  28663. 0x32, 0x6B, 0xA1, 0x32, 0xC5, 0x7F, 0xB2, 0x35,
  28664. 0x16, 0x11, 0x44, 0x51, 0x9C, 0xDF, 0x40, 0x56,
  28665. 0x0F, 0xBE, 0x27, 0x9B, 0xDE, 0x41, 0x1E, 0x11,
  28666. 0x25, 0x31, 0xF8, 0x26, 0xD6, 0xAB, 0x10, 0xD4,
  28667. 0x54, 0x73, 0x50, 0xAD, 0xD2, 0xA9, 0xDE, 0x8D,
  28668. 0x62, 0xC2, 0xAC, 0x82, 0xCA, 0xBE, 0x68, 0x15,
  28669. 0x64, 0x6F, 0x4D, 0xC9, 0x74, 0x2B, 0xB0, 0xC2,
  28670. 0xA3, 0xF7, 0x7E, 0xC7, 0xB4, 0x6C, 0x6B, 0x53,
  28671. 0x76, 0x05, 0xFA, 0x31, 0x79, 0x8C, 0xD8, 0x92,
  28672. 0x81, 0x22, 0x1A, 0x33, 0xDF, 0xB9, 0x79, 0x6E,
  28673. 0x64, 0x43, 0x05, 0x63, 0x03, 0x32, 0xC2, 0xCB,
  28674. 0x93, 0x14, 0x08, 0xAB, 0x48, 0x1A, 0x16, 0xD9,
  28675. 0x53, 0xF6, 0xBE, 0xAE, 0x38, 0x91, 0xD6, 0xD9,
  28676. 0xAC, 0x1F, 0xAB, 0x38, 0x22, 0x2D, 0x92, 0x71,
  28677. 0x87, 0x2D, 0x9D, 0x0C, 0xAD, 0xB9, 0x1A, 0xBE,
  28678. 0x9B, 0x4E, 0x26, 0x5F, 0x75, 0xC6, 0xE5, 0xE8,
  28679. 0x29, 0xE1, 0x46, 0xC3, 0xD8, 0xCE, 0x1E, 0x9D,
  28680. 0x12, 0xE0, 0xD1, 0x29, 0x80, 0x19, 0x57, 0xF4,
  28681. 0x6B, 0x0D, 0x2D, 0xBE, 0x1F, 0x74, 0x9B, 0x1D,
  28682. 0x08, 0xE2, 0x34, 0x5F, 0x62, 0x39, 0xA7, 0x31,
  28683. 0x34, 0x2E, 0xB7, 0x5B, 0x0C, 0xF1, 0xBF, 0x41,
  28684. 0x17, 0x49, 0xBC, 0x2C, 0xAF, 0x28, 0x10, 0xB7,
  28685. 0x88, 0xC6, 0xB7, 0x23, 0x8B, 0x4D, 0x3D, 0xA2,
  28686. 0xD6, 0x31, 0x5C, 0xE9, 0x54, 0x2E, 0x24, 0x40,
  28687. 0x4F, 0x14, 0x57, 0x55, 0xA3, 0x0A, 0xB8, 0x51,
  28688. 0xE4, 0x44, 0x58, 0x41, 0xBD, 0x33, 0xF7, 0x16,
  28689. 0xA5, 0x86, 0x88, 0x48, 0x88, 0xEC, 0xC6, 0xBC,
  28690. 0x64, 0x98, 0xAA, 0x32, 0x91, 0x9A, 0xE8, 0x1D,
  28691. 0x20, 0xC2, 0x69, 0x73, 0xC2, 0xBD, 0x54, 0x58,
  28692. 0x2A, 0x0F, 0x6A, 0xD9, 0x8A, 0xBF, 0xD2, 0x62,
  28693. 0x7E, 0x15, 0x69, 0x0A, 0x72, 0x7E, 0x69, 0xF5,
  28694. 0x81, 0xDD, 0x2A, 0x71, 0x27, 0x98, 0x2A, 0x90,
  28695. 0xE3, 0x3E, 0x2D, 0x4A, 0x03, 0xFE, 0x33, 0x91,
  28696. 0x42, 0xC7, 0xE4, 0x4C, 0x32, 0x6A, 0xC4, 0x6E,
  28697. 0xD3, 0x95, 0xA2, 0x25, 0xD3, 0x03, 0x33, 0x89,
  28698. 0x91, 0x73, 0x28, 0xB4, 0x53, 0x16, 0xB1, 0x58,
  28699. 0x5A, 0x01, 0xB2, 0xC3, 0x04, 0xB2, 0x94, 0x4E,
  28700. 0x90, 0x3A, 0xBB, 0xB3, 0xEC, 0x56, 0x19, 0x44,
  28701. 0x1C, 0xFC, 0x89, 0x65, 0xA4, 0x46, 0xDF, 0x75,
  28702. 0xDE, 0xFA, 0x80, 0xC6, 0xE1, 0x5A, 0xDB, 0xD5,
  28703. 0x06, 0xB7, 0xAB, 0x2D, 0xE1, 0x2D, 0xDA, 0x9B,
  28704. 0xC8, 0x14, 0x41, 0xCF, 0xC8, 0x90, 0x52, 0xE2,
  28705. 0xE5, 0x80, 0x8F, 0x71, 0x26, 0xC6, 0xFD, 0x3A,
  28706. 0xC6, 0xAC, 0x80, 0x81, 0x25, 0x8A, 0x84, 0xA0,
  28707. 0x9A, 0xE5, 0x0F, 0x6C, 0xD7, 0xCC, 0x0F, 0x4A,
  28708. 0xF3, 0x36, 0xFD, 0x1D, 0x64, 0x3E, 0x99, 0x07,
  28709. 0x99, 0x96, 0x26, 0x8C, 0x2D, 0x32, 0xD9, 0x09,
  28710. 0xF2, 0x2E, 0x35, 0x04, 0xF0, 0x7F, 0xBB, 0x56,
  28711. 0x31, 0x96, 0xD4, 0x31, 0x2F, 0xDD, 0xB9, 0x33,
  28712. 0x5D, 0x5C, 0x1D, 0x36, 0xE8, 0xC5, 0xEE, 0xA2,
  28713. 0x27, 0x8D, 0xBA, 0x23, 0xB9, 0x4D, 0x19, 0x3C,
  28714. 0x94, 0x7C, 0xC4, 0x1C, 0xA9, 0x93, 0xDC, 0x7D,
  28715. 0xB1, 0x39, 0x63, 0x40, 0xAD, 0x9C, 0x4F, 0xE6,
  28716. 0x87, 0xDD, 0x7B, 0x8D, 0x0C, 0x7A, 0x51, 0x20,
  28717. 0xAE, 0x02, 0x04, 0xF2, 0xC6, 0x65, 0xBD, 0x5F,
  28718. 0x47, 0x3D, 0x64, 0x4C, 0x7F, 0xF2, 0x6B, 0xFF,
  28719. 0xBA, 0x7A, 0x36, 0x98, 0x08, 0x30, 0x70, 0x21,
  28720. 0x28, 0xA7, 0xE6, 0x61, 0xD6, 0x77, 0xA0, 0x92,
  28721. 0xA3, 0x6E, 0x74, 0x28, 0xA4, 0x13, 0x9F, 0xB2,
  28722. 0x9B, 0x00, 0x95, 0xCC, 0x11, 0x08, 0x6F, 0x44,
  28723. 0x7D, 0x2A, 0x9E, 0xF6, 0xC9, 0xB1, 0x61, 0xF1,
  28724. 0x89, 0xC6, 0x29, 0x9E, 0x08, 0x4C, 0xB7, 0xAA,
  28725. 0x00, 0xFA, 0xF7, 0x87, 0x79, 0x7B, 0xFB, 0x06,
  28726. 0x9F, 0xBC, 0x08, 0x7F, 0xDE, 0x26, 0x25, 0x2A,
  28727. 0x16, 0x64, 0xF1, 0x9C, 0x5A, 0x8A, 0x22, 0xEC,
  28728. 0x5E, 0xE1, 0xAE, 0xB0, 0x76, 0x35, 0x7B, 0x7D,
  28729. 0xC3, 0x7E, 0x6B, 0x0F, 0x15, 0x20, 0xF9, 0x58,
  28730. 0xF7, 0x85, 0x1B, 0xAC, 0xB9, 0x2C, 0x89, 0xFD,
  28731. 0x11, 0x4A, 0x72, 0xFE, 0xAC, 0x54, 0x65, 0x2D,
  28732. 0x45, 0xB0, 0x9E, 0x1A, 0xE7, 0x65, 0x1A, 0xBD,
  28733. 0x16, 0x4B, 0xCD, 0x53, 0x7D, 0x58, 0xFA, 0x39,
  28734. 0xD3, 0xEC, 0x8A, 0xCD, 0xCD, 0xF9, 0x84, 0x25,
  28735. 0x00, 0x58, 0x62, 0xFA, 0x59, 0x69, 0x2D, 0xE1,
  28736. 0x62, 0xB7, 0x7E, 0x62, 0x97, 0xC6, 0x62, 0x33,
  28737. 0x34, 0x84, 0x08, 0xA8, 0xAB, 0x69, 0x5C, 0xE2,
  28738. 0xF2, 0x72, 0x8D, 0xB9, 0xFB, 0xE2, 0x7E, 0x95,
  28739. 0x89, 0x67, 0xEC, 0x59, 0x74, 0x76, 0x7C, 0x5A,
  28740. 0x66, 0x02, 0x30, 0x74, 0xB4, 0xA7, 0x1A, 0xFD,
  28741. 0x26, 0x4A, 0xD2, 0x89, 0x0E, 0x97, 0x0A, 0x1F,
  28742. 0x31, 0xD6, 0xE3, 0x31, 0x1B, 0x73, 0x6F, 0x9F,
  28743. 0x94, 0x88, 0x79, 0x3D, 0xDC, 0x88, 0xF2, 0x34,
  28744. 0x58, 0x06, 0x42, 0x54, 0xC8, 0x2A, 0x1D, 0x9E,
  28745. 0x59, 0xEA, 0xD2, 0xFC, 0xEC, 0x40, 0xB4, 0x30,
  28746. 0x68, 0x7C, 0x4B, 0x7E, 0x28, 0x96, 0x09, 0x26,
  28747. 0xAF, 0xCA, 0xCC, 0x9B, 0xD7, 0x56, 0xA7, 0x10,
  28748. 0x88, 0xC7, 0x84, 0x50, 0xE2, 0x0A, 0x2E, 0x98,
  28749. 0x0A, 0xED, 0xE9, 0xEB, 0xED, 0xFE, 0x7F, 0xAB,
  28750. 0xD6, 0xAB, 0xFE, 0x96, 0xF9, 0x34, 0xC4, 0xB0,
  28751. 0x2C, 0x01, 0xCA, 0x19, 0x4D, 0x01, 0xB7, 0x3C,
  28752. 0x25, 0xD5, 0x99, 0x70, 0x39, 0xD3, 0xFC, 0xD0,
  28753. 0xF0, 0x99, 0x52, 0x1F, 0x70, 0xCA, 0xEE, 0x69,
  28754. 0x11, 0x0A, 0xC1, 0xFC, 0x5A, 0x99, 0x91, 0x7A,
  28755. 0xD7, 0x52, 0xFC, 0x96, 0xAD, 0xFA, 0xD7, 0x18,
  28756. 0x6D, 0x0A, 0x7C, 0x9C, 0xFE, 0x56, 0x01, 0xC0,
  28757. 0x75, 0x14, 0xEA, 0x64, 0x48, 0xD6, 0x61, 0xC5,
  28758. 0x7A, 0xA2, 0x02, 0x42, 0x10, 0x3C, 0x42, 0x76,
  28759. 0xA0, 0x70, 0xA4, 0x89, 0xA4, 0xCB, 0x6B, 0xCA,
  28760. 0x0F, 0x9E, 0xCC, 0x43, 0x79, 0xFB, 0x22, 0x02,
  28761. 0x15, 0xFD, 0x91, 0xF8, 0x10, 0x19, 0xD5, 0xB0,
  28762. 0xAE, 0x61, 0x93, 0x58, 0xB5, 0x24, 0x68, 0xF2,
  28763. 0x72, 0xC1, 0x78, 0xE3, 0xA7, 0x4C, 0xF6, 0x77,
  28764. 0x5A, 0xA9, 0x24, 0xFE, 0x32, 0x9C, 0x31, 0x75,
  28765. 0xD9, 0xE4, 0xC3, 0xE2, 0x1A, 0xB9, 0xEC, 0x83,
  28766. 0x6E, 0xDC, 0x3A, 0xCA, 0xB2, 0xE3, 0x89, 0x1E,
  28767. 0xE8, 0xDE, 0xDA, 0x51, 0x5D, 0x39, 0xAF, 0x9B,
  28768. 0x8D, 0xDD, 0x0E, 0xE7, 0xB0, 0x16, 0x4F, 0x80,
  28769. 0x5C, 0x38, 0x35, 0xF6, 0xD2, 0xBA, 0xBD, 0xB3,
  28770. 0x0E, 0xAB, 0x47, 0x56, 0xE7, 0xEC, 0x7F, 0x82,
  28771. 0x9E, 0xCE, 0x01, 0xE8, 0xEA, 0xDF, 0xBB, 0xED,
  28772. 0x12, 0xFC, 0x28, 0x3B, 0x3D, 0x4C, 0x69, 0xF5,
  28773. 0x75, 0xE7, 0xF8, 0x04, 0x17, 0x68, 0x9F, 0xDF,
  28774. 0xCF, 0xC7, 0xBE, 0x27, 0xEE, 0x3B, 0x8C, 0xDF,
  28775. 0x57, 0xAA, 0xEB, 0xEC, 0x4A, 0x95, 0xB7, 0xE5,
  28776. 0xBB, 0x58, 0x5B, 0x85, 0x22, 0x7F, 0x7C, 0x32,
  28777. 0xBE, 0x30, 0xDB, 0x3E, 0x65, 0xE4, 0x2E, 0x30,
  28778. 0xDC, 0xF5, 0xA5, 0xFA, 0x07, 0x3D, 0xBA, 0x39,
  28779. 0x9D, 0x94, 0x2F, 0x22, 0x22, 0xAD, 0xB9, 0xB9,
  28780. 0x89, 0x81, 0x02, 0xAF, 0xE5, 0x43, 0x2E, 0xDC,
  28781. 0x7F, 0x04, 0xAE, 0x34, 0xA8, 0xFE, 0xC2, 0xD8,
  28782. 0x1C, 0xB4, 0x9A, 0x9A, 0x9B, 0x43, 0x81, 0x4C,
  28783. 0xE7, 0x1D, 0x97, 0xF7, 0x26, 0xE2, 0xB1, 0xE8,
  28784. 0xF6, 0x4B, 0x50, 0xE6, 0x5D, 0xFB, 0x48, 0x16,
  28785. 0xE1, 0x2E, 0x82, 0xA3, 0x19, 0x74, 0x84, 0xA4,
  28786. 0xE9, 0xBB, 0xA4, 0xD2, 0xD6, 0x9E, 0x3F, 0x19,
  28787. 0xD0, 0xB7, 0x5C, 0x21, 0xE2, 0xBF, 0xFE, 0x9F,
  28788. 0xC0, 0xC9, 0x8C, 0xF4, 0x8A, 0x3A, 0xAF, 0x08,
  28789. 0xD4, 0x67, 0xF7, 0x26, 0x87, 0xDF, 0x01, 0x78,
  28790. 0x17, 0x4B, 0x78, 0x97, 0xF7, 0x34, 0x34, 0x9B,
  28791. 0x18, 0x1E, 0xCA, 0x86, 0xA5, 0x98, 0xA0, 0xC5,
  28792. 0xE8, 0xC2, 0x59, 0x46, 0xF2, 0x4D, 0xC5, 0x57,
  28793. 0x2B, 0xD3, 0x24, 0xA4, 0x04, 0x58, 0xA7, 0x88,
  28794. 0xE5, 0x13, 0x7F, 0x3C, 0x7A, 0x7C, 0x97, 0xFC,
  28795. 0x9F, 0x12, 0xA3, 0xC4, 0x63, 0xA8, 0xFE, 0x94,
  28796. 0x49, 0x10, 0x1C, 0xCE, 0x96, 0x6D, 0x7C, 0x00,
  28797. 0x93, 0x23, 0x93, 0x29, 0x98, 0xD5, 0x6E, 0xF4,
  28798. 0x30, 0xC7, 0x3B, 0xC2, 0x4F, 0x5D, 0x95, 0xF7,
  28799. 0x37, 0x85, 0x8D, 0xDC, 0x4F, 0x32, 0xC0, 0x13
  28800. };
  28801. const byte kyber1024_ss[] = {
  28802. 0xB1, 0x0F, 0x73, 0x94, 0x92, 0x6A, 0xD3, 0xB4,
  28803. 0x9C, 0x5D, 0x62, 0xD5, 0xAE, 0xB5, 0x31, 0xD5,
  28804. 0x75, 0x75, 0x38, 0xBC, 0xC0, 0xDA, 0x9E, 0x55,
  28805. 0x0D, 0x43, 0x8F, 0x1B, 0x61, 0xBD, 0x74, 0x19
  28806. };
  28807. ret = wc_KyberKey_Init(KYBER1024, &key, HEAP_HINT, INVALID_DEVID);
  28808. if (ret != 0)
  28809. return -20301;
  28810. ret = wc_KyberKey_MakeKeyWithRandom(&key, kyber1024_rand,
  28811. sizeof(kyber1024_rand));
  28812. if (ret != 0)
  28813. return -20302;
  28814. ret = wc_KyberKey_EncodePublicKey(&key, pub, sizeof(pub));
  28815. if (ret != 0)
  28816. return -20303;
  28817. ret = wc_KyberKey_EncodePrivateKey(&key, priv, sizeof(priv));
  28818. if (ret != 0)
  28819. return -20304;
  28820. if (XMEMCMP(pub, kyber1024_pk, sizeof(kyber1024_pk)) != 0)
  28821. return -20305;
  28822. if (XMEMCMP(priv, kyber1024_sk, sizeof(kyber1024_sk)) != 0)
  28823. return -20306;
  28824. ret = wc_KyberKey_EncapsulateWithRandom(&key, ct, ss, kyber1024enc_rand,
  28825. sizeof(kyber1024enc_rand));
  28826. if (ret != 0)
  28827. return -20307;
  28828. if (XMEMCMP(ct, kyber1024_ct, sizeof(kyber1024_ct)) != 0)
  28829. return -20308;
  28830. if (XMEMCMP(ss, kyber1024_ss, sizeof(kyber1024_ss)) != 0)
  28831. return -20309;
  28832. ret = wc_KyberKey_Decapsulate(&key, ss_dec, ct, sizeof(kyber1024_ct));
  28833. if (ret != 0)
  28834. return -20310;
  28835. if (XMEMCMP(ss_dec, kyber1024_ss, sizeof(kyber1024_ss)) != 0)
  28836. return -20311;
  28837. wc_KyberKey_Free(&key);
  28838. return 0;
  28839. }
  28840. #endif /* WOLFSSL_KYBER1024 */
  28841. #endif /* WOLFSSL_WC_KYBER */
  28842. WOLFSSL_TEST_SUBROUTINE int kyber_test(void)
  28843. {
  28844. int ret;
  28845. KyberKey key;
  28846. WC_RNG rng;
  28847. int i;
  28848. byte priv[KYBER_MAX_PRIVATE_KEY_SIZE];
  28849. byte pub[KYBER_MAX_PUBLIC_KEY_SIZE];
  28850. byte priv2[KYBER_MAX_PRIVATE_KEY_SIZE];
  28851. byte pub2[KYBER_MAX_PUBLIC_KEY_SIZE];
  28852. byte ct[KYBER_MAX_CIPHER_TEXT_SIZE];
  28853. byte ss[KYBER_SS_SZ];
  28854. byte ss_dec[KYBER_SS_SZ];
  28855. int testData[][4] = {
  28856. #ifdef WOLFSSL_KYBER512
  28857. { KYBER512, KYBER512_PRIVATE_KEY_SIZE, KYBER512_PUBLIC_KEY_SIZE,
  28858. KYBER512_CIPHER_TEXT_SIZE },
  28859. #endif
  28860. #ifdef WOLFSSL_KYBER768
  28861. { KYBER768, KYBER768_PRIVATE_KEY_SIZE, KYBER768_PUBLIC_KEY_SIZE,
  28862. KYBER768_CIPHER_TEXT_SIZE },
  28863. #endif
  28864. #ifdef WOLFSSL_KYBER1024
  28865. { KYBER1024, KYBER1024_PRIVATE_KEY_SIZE, KYBER1024_PUBLIC_KEY_SIZE,
  28866. KYBER1024_CIPHER_TEXT_SIZE },
  28867. #endif
  28868. };
  28869. #ifndef HAVE_FIPS
  28870. ret = wc_InitRng_ex(&rng, HEAP_HINT, INVALID_DEVID);
  28871. #else
  28872. ret = wc_InitRng(&rng);
  28873. #endif
  28874. if (ret != 0)
  28875. return -20000;
  28876. for (i = 0; i < (int)(sizeof(testData) / sizeof(*testData)); i++) {
  28877. ret = wc_KyberKey_Init(testData[i][0], &key, HEAP_HINT, INVALID_DEVID);
  28878. if (ret != 0)
  28879. return -20001 - i * 20;
  28880. ret = wc_KyberKey_MakeKey(&key, &rng);
  28881. if (ret != 0)
  28882. return -20002 - i * 20;
  28883. ret = wc_KyberKey_EncodePublicKey(&key, pub, testData[i][2]);
  28884. if (ret != 0)
  28885. return -20003 - i * 20;
  28886. ret = wc_KyberKey_EncodePrivateKey(&key, priv, testData[i][1]);
  28887. if (ret != 0)
  28888. return -20004 - i * 20;
  28889. ret = wc_KyberKey_Init(testData[i][0], &key, HEAP_HINT, INVALID_DEVID);
  28890. if (ret != 0)
  28891. return -20005 - i * 20;
  28892. ret = wc_KyberKey_DecodePublicKey(&key, pub, testData[i][2]);
  28893. if (ret != 0)
  28894. return -20006 - i * 20;
  28895. ret = wc_KyberKey_Encapsulate(&key, ct, ss, &rng);
  28896. if (ret != 0)
  28897. return -20007 - i * 20;
  28898. ret = wc_KyberKey_EncodePublicKey(&key, pub2, testData[i][2]);
  28899. if (ret != 0)
  28900. return -20008 - i * 20;
  28901. if (XMEMCMP(pub, pub2, testData[i][2]) != 0)
  28902. return -20009 - i * 20;
  28903. ret = wc_KyberKey_Init(testData[i][0], &key, HEAP_HINT, INVALID_DEVID);
  28904. if (ret != 0)
  28905. return -20010 - i * 20;
  28906. ret = wc_KyberKey_DecodePrivateKey(&key, priv, testData[i][1]);
  28907. if (ret != 0)
  28908. return -20011 - i * 20;
  28909. ret = wc_KyberKey_Decapsulate(&key, ss_dec, ct, testData[i][3]);
  28910. if (ret != 0)
  28911. return -20012 - i * 20;
  28912. if (XMEMCMP(ss, ss_dec, sizeof(ss)) != 0)
  28913. return -20013 - i * 20;
  28914. ret = wc_KyberKey_EncodePrivateKey(&key, priv2, testData[i][1]);
  28915. if (ret != 0)
  28916. return -20014 - i * 20;
  28917. if (XMEMCMP(priv, priv2, testData[i][2]) != 0)
  28918. return -20015 - i * 20;
  28919. wc_KyberKey_Free(&key);
  28920. }
  28921. wc_FreeRng(&rng);
  28922. #ifdef WOLFSSL_WC_KYBER
  28923. #ifdef WOLFSSL_KYBER512
  28924. ret = kyber512_kat();
  28925. if (ret != 0)
  28926. return ret;
  28927. #endif
  28928. #ifdef WOLFSSL_KYBER768
  28929. ret = kyber768_kat();
  28930. if (ret != 0)
  28931. return ret;
  28932. #endif
  28933. #ifdef WOLFSSL_KYBER1024
  28934. ret = kyber1024_kat();
  28935. if (ret != 0)
  28936. return ret;
  28937. #endif
  28938. #endif /* WOLFSSL_WC_KYBER */
  28939. return 0;
  28940. }
  28941. #endif /* WOLFSSL_HAVE_KYBER */
  28942. #ifdef WOLFCRYPT_HAVE_ECCSI
  28943. static int eccsi_api_test(WC_RNG* rng, EccsiKey* key, mp_int* ssk,
  28944. ecc_point* pvt)
  28945. {
  28946. int ret;
  28947. byte id[1] = { 0x00 };
  28948. int valid;
  28949. word32 sz;
  28950. byte data[256];
  28951. byte hash[WC_MAX_DIGEST_SIZE];
  28952. byte hashSz;
  28953. byte sig[257];
  28954. word32 sigSz;
  28955. ret = wc_InitEccsiKey_ex(NULL, 32, ECC_SECP256R1, HEAP_HINT, INVALID_DEVID);
  28956. if (ret != BAD_FUNC_ARG)
  28957. return -10023;
  28958. ret = wc_InitEccsiKey_ex(NULL, 32, ECC_SECP256R1, HEAP_HINT, INVALID_DEVID);
  28959. if (ret != BAD_FUNC_ARG)
  28960. return -10024;
  28961. ret = wc_InitEccsiKey(NULL, NULL, INVALID_DEVID);
  28962. if (ret != BAD_FUNC_ARG)
  28963. return -10025;
  28964. ret = wc_InitEccsiKey(NULL, HEAP_HINT, INVALID_DEVID);
  28965. if (ret != BAD_FUNC_ARG)
  28966. return -10026;
  28967. wc_FreeEccsiKey(NULL);
  28968. /* Create a valid key. */
  28969. ret = wc_InitEccsiKey(key, NULL, INVALID_DEVID);
  28970. if (ret != 0)
  28971. return -10027;
  28972. ret = wc_MakeEccsiKey(NULL, NULL);
  28973. if (ret != BAD_FUNC_ARG)
  28974. return -10028;
  28975. ret = wc_MakeEccsiKey(key, NULL);
  28976. if (ret != BAD_FUNC_ARG)
  28977. return -10029;
  28978. ret = wc_MakeEccsiKey(NULL, rng);
  28979. if (ret != BAD_FUNC_ARG)
  28980. return -10030;
  28981. ret = wc_MakeEccsiPair(NULL, NULL, WC_HASH_TYPE_SHA256, NULL, 1, NULL,
  28982. NULL);
  28983. if (ret != BAD_FUNC_ARG)
  28984. return -10031;
  28985. ret = wc_MakeEccsiPair(key, rng, WC_HASH_TYPE_SHA256, id, 1, ssk, NULL);
  28986. if (ret != BAD_FUNC_ARG)
  28987. return -10032;
  28988. ret = wc_MakeEccsiPair(key, rng, WC_HASH_TYPE_SHA256, id, 1, NULL, pvt);
  28989. if (ret != BAD_FUNC_ARG)
  28990. return -10033;
  28991. ret = wc_MakeEccsiPair(key, rng, WC_HASH_TYPE_SHA256, NULL, 1, ssk, pvt);
  28992. if (ret != BAD_FUNC_ARG)
  28993. return -10034;
  28994. ret = wc_MakeEccsiPair(key, NULL, WC_HASH_TYPE_SHA256, id, 1, ssk, pvt);
  28995. if (ret != BAD_FUNC_ARG)
  28996. return -10035;
  28997. ret = wc_MakeEccsiPair(NULL, rng, WC_HASH_TYPE_SHA256, id, 1, ssk, pvt);
  28998. if (ret != BAD_FUNC_ARG)
  28999. return -10036;
  29000. /* No key set */
  29001. ret = wc_MakeEccsiPair(key, rng, WC_HASH_TYPE_SHA256, id, 1, ssk, pvt);
  29002. if (ret != BAD_STATE_E)
  29003. return -10037;
  29004. ret = wc_ValidateEccsiPair(NULL, WC_HASH_TYPE_SHA256, NULL, 1, NULL, NULL,
  29005. NULL);
  29006. if (ret != BAD_FUNC_ARG)
  29007. return -10038;
  29008. ret = wc_ValidateEccsiPair(key, WC_HASH_TYPE_SHA256, id, 1, ssk, pvt,
  29009. NULL);
  29010. if (ret != BAD_FUNC_ARG)
  29011. return -10039;
  29012. ret = wc_ValidateEccsiPair(key, WC_HASH_TYPE_SHA256, id, 1, ssk, NULL,
  29013. &valid);
  29014. if (ret != BAD_FUNC_ARG)
  29015. return -10040;
  29016. ret = wc_ValidateEccsiPair(key, WC_HASH_TYPE_SHA256, id, 1, NULL, pvt,
  29017. &valid);
  29018. if (ret != BAD_FUNC_ARG)
  29019. return -10041;
  29020. ret = wc_ValidateEccsiPair(key, WC_HASH_TYPE_SHA256, NULL, 1, ssk, pvt,
  29021. &valid);
  29022. if (ret != BAD_FUNC_ARG)
  29023. return -10042;
  29024. ret = wc_ValidateEccsiPair(NULL, WC_HASH_TYPE_SHA256, id, 1, ssk, pvt,
  29025. &valid);
  29026. if (ret != BAD_FUNC_ARG)
  29027. return -10043;
  29028. /* No key set */
  29029. ret = wc_ValidateEccsiPair(key, WC_HASH_TYPE_SHA256, id, 1, ssk, pvt,
  29030. &valid);
  29031. if (ret != BAD_STATE_E)
  29032. return -10044;
  29033. ret = wc_ValidateEccsiPvt(NULL, NULL, NULL);
  29034. if (ret != BAD_FUNC_ARG)
  29035. return -10045;
  29036. ret = wc_ValidateEccsiPvt(key, NULL, NULL);
  29037. if (ret != BAD_FUNC_ARG)
  29038. return -10046;
  29039. ret = wc_ValidateEccsiPvt(NULL, pvt, NULL);
  29040. if (ret != BAD_FUNC_ARG)
  29041. return -10047;
  29042. ret = wc_ValidateEccsiPvt(NULL, NULL, &valid);
  29043. if (ret != BAD_FUNC_ARG)
  29044. return -10048;
  29045. ret = wc_ValidateEccsiPvt(key, pvt, NULL);
  29046. if (ret != BAD_FUNC_ARG)
  29047. return -10049;
  29048. ret = wc_ValidateEccsiPvt(key, NULL, &valid);
  29049. if (ret != BAD_FUNC_ARG)
  29050. return -10050;
  29051. ret = wc_ValidateEccsiPvt(NULL, pvt, &valid);
  29052. if (ret != BAD_FUNC_ARG)
  29053. return -10051;
  29054. ret = wc_EncodeEccsiPair(NULL, NULL, NULL, data, NULL);
  29055. if (ret != BAD_FUNC_ARG)
  29056. return -10052;
  29057. ret = wc_EncodeEccsiPair(key, ssk, pvt, data, NULL);
  29058. if (ret != BAD_FUNC_ARG)
  29059. return -10053;
  29060. ret = wc_EncodeEccsiPair(key, ssk, NULL, data, &sz);
  29061. if (ret != BAD_FUNC_ARG)
  29062. return -10054;
  29063. ret = wc_EncodeEccsiPair(key, NULL, pvt, data, &sz);
  29064. if (ret != BAD_FUNC_ARG)
  29065. return -10055;
  29066. ret = wc_EncodeEccsiPair(NULL, ssk, pvt, data, &sz);
  29067. if (ret != BAD_FUNC_ARG)
  29068. return -10056;
  29069. /* No key created so no curve information. */
  29070. ret = wc_EncodeEccsiPair(key, ssk, pvt, NULL, &sz);
  29071. if (ret != LENGTH_ONLY_E)
  29072. return -10057;
  29073. ret = wc_EncodeEccsiSsk(NULL, NULL, data, NULL);
  29074. if (ret != BAD_FUNC_ARG)
  29075. return -10058;
  29076. ret = wc_EncodeEccsiSsk(key, ssk, data, NULL);
  29077. if (ret != BAD_FUNC_ARG)
  29078. return -10059;
  29079. ret = wc_EncodeEccsiSsk(key, NULL, data, &sz);
  29080. if (ret != BAD_FUNC_ARG)
  29081. return -10060;
  29082. ret = wc_EncodeEccsiSsk(NULL, ssk, data, &sz);
  29083. if (ret != BAD_FUNC_ARG)
  29084. return -10061;
  29085. ret = wc_EncodeEccsiPvt(NULL, NULL, data, NULL, 1);
  29086. if (ret != BAD_FUNC_ARG)
  29087. return -10058;
  29088. ret = wc_EncodeEccsiPvt(key, pvt, data, NULL, 1);
  29089. if (ret != BAD_FUNC_ARG)
  29090. return -10059;
  29091. ret = wc_EncodeEccsiPvt(key, NULL, data, &sz, 1);
  29092. if (ret != BAD_FUNC_ARG)
  29093. return -10060;
  29094. ret = wc_EncodeEccsiPvt(NULL, pvt, data, &sz, 1);
  29095. if (ret != BAD_FUNC_ARG)
  29096. return -10061;
  29097. ret = wc_DecodeEccsiPair(NULL, NULL, 0, NULL, NULL);
  29098. if (ret != BAD_FUNC_ARG)
  29099. return -10062;
  29100. ret = wc_DecodeEccsiPair(key, data, 0, ssk, NULL);
  29101. if (ret != BAD_FUNC_ARG)
  29102. return -10063;
  29103. ret = wc_DecodeEccsiPair(key, data, 0, NULL, pvt);
  29104. if (ret != BAD_FUNC_ARG)
  29105. return -10064;
  29106. ret = wc_DecodeEccsiPair(key, NULL, 0, ssk, pvt);
  29107. if (ret != BAD_FUNC_ARG)
  29108. return -10065;
  29109. ret = wc_DecodeEccsiPair(NULL, data, 0, ssk, pvt);
  29110. if (ret != BAD_FUNC_ARG)
  29111. return -10066;
  29112. ret = wc_DecodeEccsiSsk(NULL, NULL, 0, NULL);
  29113. if (ret != BAD_FUNC_ARG)
  29114. return -10067;
  29115. ret = wc_DecodeEccsiSsk(key, data, 0, NULL);
  29116. if (ret != BAD_FUNC_ARG)
  29117. return -10068;
  29118. ret = wc_DecodeEccsiSsk(key, NULL, 0, ssk);
  29119. if (ret != BAD_FUNC_ARG)
  29120. return -10069;
  29121. ret = wc_DecodeEccsiSsk(NULL, data, 0, ssk);
  29122. if (ret != BAD_FUNC_ARG)
  29123. return -10070;
  29124. ret = wc_DecodeEccsiPvt(NULL, NULL, 0, NULL);
  29125. if (ret != BAD_FUNC_ARG)
  29126. return -10067;
  29127. ret = wc_DecodeEccsiPvt(key, data, 0, NULL);
  29128. if (ret != BAD_FUNC_ARG)
  29129. return -10068;
  29130. ret = wc_DecodeEccsiPvt(key, NULL, 0, pvt);
  29131. if (ret != BAD_FUNC_ARG)
  29132. return -10069;
  29133. ret = wc_DecodeEccsiPvt(NULL, data, 0, pvt);
  29134. if (ret != BAD_FUNC_ARG)
  29135. return -10070;
  29136. ret = wc_DecodeEccsiPvtFromSig(NULL, NULL, 0, NULL);
  29137. if (ret != BAD_FUNC_ARG)
  29138. return -10067;
  29139. ret = wc_DecodeEccsiPvtFromSig(key, data, 0, NULL);
  29140. if (ret != BAD_FUNC_ARG)
  29141. return -10068;
  29142. ret = wc_DecodeEccsiPvtFromSig(key, NULL, 0, pvt);
  29143. if (ret != BAD_FUNC_ARG)
  29144. return -10069;
  29145. ret = wc_DecodeEccsiPvtFromSig(NULL, data, 0, pvt);
  29146. if (ret != BAD_FUNC_ARG)
  29147. return -10070;
  29148. ret = wc_ExportEccsiKey(NULL, data, NULL);
  29149. if (ret != BAD_FUNC_ARG)
  29150. return -10071;
  29151. ret = wc_ExportEccsiKey(key, data, NULL);
  29152. if (ret != BAD_FUNC_ARG)
  29153. return -10072;
  29154. ret = wc_ExportEccsiKey(NULL, data, &sz);
  29155. if (ret != BAD_FUNC_ARG)
  29156. return -10073;
  29157. /* No key to export */
  29158. ret = wc_ExportEccsiKey(key, NULL, &sz);
  29159. if (ret != BAD_STATE_E)
  29160. return -10074;
  29161. ret = wc_ImportEccsiKey(NULL, NULL, 0);
  29162. if (ret != BAD_FUNC_ARG)
  29163. return -10075;
  29164. ret = wc_ImportEccsiKey(key, NULL, 0);
  29165. if (ret != BAD_FUNC_ARG)
  29166. return -10076;
  29167. ret = wc_ImportEccsiKey(NULL, data, 0);
  29168. if (ret != BAD_FUNC_ARG)
  29169. return -10077;
  29170. ret = wc_ExportEccsiPrivateKey(NULL, data, NULL);
  29171. if (ret != BAD_FUNC_ARG)
  29172. return -10071;
  29173. ret = wc_ExportEccsiPrivateKey(key, data, NULL);
  29174. if (ret != BAD_FUNC_ARG)
  29175. return -10072;
  29176. ret = wc_ExportEccsiPrivateKey(NULL, data, &sz);
  29177. if (ret != BAD_FUNC_ARG)
  29178. return -10073;
  29179. /* No key to export */
  29180. ret = wc_ExportEccsiPrivateKey(key, NULL, &sz);
  29181. if (ret != BAD_STATE_E)
  29182. return -10074;
  29183. ret = wc_ImportEccsiPrivateKey(NULL, NULL, 0);
  29184. if (ret != BAD_FUNC_ARG)
  29185. return -10075;
  29186. ret = wc_ImportEccsiPrivateKey(key, NULL, 0);
  29187. if (ret != BAD_FUNC_ARG)
  29188. return -10076;
  29189. ret = wc_ImportEccsiPrivateKey(NULL, data, 0);
  29190. if (ret != BAD_FUNC_ARG)
  29191. return -10077;
  29192. ret = wc_ExportEccsiPublicKey(NULL, data, NULL, 1);
  29193. if (ret != BAD_FUNC_ARG)
  29194. return -10078;
  29195. ret = wc_ExportEccsiPublicKey(key, data, NULL, 1);
  29196. if (ret != BAD_FUNC_ARG)
  29197. return -10079;
  29198. ret = wc_ExportEccsiPublicKey(NULL, data, &sz, 1);
  29199. if (ret != BAD_FUNC_ARG)
  29200. return -10080;
  29201. /* No key to export */
  29202. ret = wc_ExportEccsiPublicKey(key, data, &sz, 1);
  29203. if (ret != BAD_STATE_E)
  29204. return -10081;
  29205. ret = wc_ImportEccsiPublicKey(NULL, NULL, 0, 1);
  29206. if (ret != BAD_FUNC_ARG)
  29207. return -10082;
  29208. ret = wc_ImportEccsiPublicKey(key, NULL, 0, 1);
  29209. if (ret != BAD_FUNC_ARG)
  29210. return -10083;
  29211. ret = wc_ImportEccsiPublicKey(NULL, data, 0, 1);
  29212. if (ret != BAD_FUNC_ARG)
  29213. return -10084;
  29214. ret = wc_HashEccsiId(NULL, WC_HASH_TYPE_SHA256, NULL, 1, NULL, NULL, NULL);
  29215. if (ret != BAD_FUNC_ARG)
  29216. return -10085;
  29217. ret = wc_HashEccsiId(key, WC_HASH_TYPE_SHA256, id, 1, pvt, hash, NULL);
  29218. if (ret != BAD_FUNC_ARG)
  29219. return -10086;
  29220. ret = wc_HashEccsiId(key, WC_HASH_TYPE_SHA256, id, 1, pvt, NULL, &hashSz);
  29221. if (ret != BAD_FUNC_ARG)
  29222. return -10087;
  29223. ret = wc_HashEccsiId(key, WC_HASH_TYPE_SHA256, id, 1, NULL, hash, &hashSz);
  29224. if (ret != BAD_FUNC_ARG)
  29225. return -10088;
  29226. ret = wc_HashEccsiId(key, WC_HASH_TYPE_SHA256, NULL, 1, pvt, hash,
  29227. &hashSz);
  29228. if (ret != BAD_FUNC_ARG)
  29229. return -10089;
  29230. ret = wc_HashEccsiId(NULL, WC_HASH_TYPE_SHA256, id, 1, pvt, hash, &hashSz);
  29231. if (ret != BAD_FUNC_ARG)
  29232. return -10090;
  29233. ret = wc_HashEccsiId(key, WC_HASH_TYPE_SHA256, id, 1, pvt, hash, &hashSz);
  29234. if (ret != BAD_STATE_E)
  29235. return -10091;
  29236. ret = wc_SetEccsiHash(NULL, NULL, 1);
  29237. if (ret != BAD_FUNC_ARG)
  29238. return -10090;
  29239. ret = wc_SetEccsiHash(key, NULL, 1);
  29240. if (ret != BAD_FUNC_ARG)
  29241. return -10090;
  29242. ret = wc_SetEccsiHash(NULL, hash, 1);
  29243. if (ret != BAD_FUNC_ARG)
  29244. return -10090;
  29245. ret = wc_SetEccsiPair(NULL, NULL, NULL);
  29246. if (ret != BAD_FUNC_ARG)
  29247. return -10090;
  29248. ret = wc_SetEccsiPair(key, NULL, NULL);
  29249. if (ret != BAD_FUNC_ARG)
  29250. return -10090;
  29251. ret = wc_SetEccsiPair(NULL, ssk, NULL);
  29252. if (ret != BAD_FUNC_ARG)
  29253. return -10090;
  29254. ret = wc_SetEccsiPair(NULL, NULL, pvt);
  29255. if (ret != BAD_FUNC_ARG)
  29256. return -10090;
  29257. ret = wc_SetEccsiPair(key, ssk, NULL);
  29258. if (ret != BAD_FUNC_ARG)
  29259. return -10090;
  29260. ret = wc_SetEccsiPair(key, NULL, pvt);
  29261. if (ret != BAD_FUNC_ARG)
  29262. return -10090;
  29263. ret = wc_SetEccsiPair(NULL, ssk, pvt);
  29264. if (ret != BAD_FUNC_ARG)
  29265. return -10090;
  29266. ret = wc_SignEccsiHash(NULL, NULL, WC_HASH_TYPE_SHA256, NULL, 0, sig, NULL);
  29267. if (ret != BAD_FUNC_ARG)
  29268. return -10092;
  29269. ret = wc_SignEccsiHash(key, rng, WC_HASH_TYPE_SHA256, data, 0, sig, NULL);
  29270. if (ret != BAD_FUNC_ARG)
  29271. return -10093;
  29272. ret = wc_SignEccsiHash(key, rng, WC_HASH_TYPE_SHA256, NULL, 0, sig,
  29273. &sigSz);
  29274. if (ret != BAD_FUNC_ARG)
  29275. return -10096;
  29276. ret = wc_SignEccsiHash(key, NULL, WC_HASH_TYPE_SHA256, data, 0, sig,
  29277. &sigSz);
  29278. if (ret != BAD_FUNC_ARG)
  29279. return -10098;
  29280. ret = wc_SignEccsiHash(NULL, rng, WC_HASH_TYPE_SHA256, data, 0, sig,
  29281. &sigSz);
  29282. if (ret != BAD_FUNC_ARG)
  29283. return -10099;
  29284. /* Key not set. */
  29285. ret = wc_SignEccsiHash(key, rng, WC_HASH_TYPE_SHA256, data, 0, NULL,
  29286. &sigSz);
  29287. if (ret != BAD_STATE_E)
  29288. return -10100;
  29289. ret = wc_VerifyEccsiHash(NULL, WC_HASH_TYPE_SHA256, NULL, 0, NULL, 0, NULL);
  29290. if (ret != BAD_FUNC_ARG)
  29291. return -10101;
  29292. ret = wc_VerifyEccsiHash(key, WC_HASH_TYPE_SHA256, NULL, 0, NULL, 0, NULL);
  29293. if (ret != BAD_FUNC_ARG)
  29294. return -10101;
  29295. ret = wc_VerifyEccsiHash(NULL, WC_HASH_TYPE_SHA256, data, 0, NULL, 0, NULL);
  29296. if (ret != BAD_FUNC_ARG)
  29297. return -10101;
  29298. ret = wc_VerifyEccsiHash(NULL, WC_HASH_TYPE_SHA256, NULL, 0, sig, 0, NULL);
  29299. if (ret != BAD_FUNC_ARG)
  29300. return -10101;
  29301. ret = wc_VerifyEccsiHash(NULL, WC_HASH_TYPE_SHA256, NULL, 0, NULL, 0,
  29302. &valid);
  29303. if (ret != BAD_FUNC_ARG)
  29304. return -10101;
  29305. ret = wc_VerifyEccsiHash(key, WC_HASH_TYPE_SHA256, data, 0, sig, 0, NULL);
  29306. if (ret != BAD_FUNC_ARG)
  29307. return -10102;
  29308. ret = wc_VerifyEccsiHash(key, WC_HASH_TYPE_SHA256, data, 0, NULL, 0,
  29309. &valid);
  29310. if (ret != BAD_FUNC_ARG)
  29311. return -10103;
  29312. ret = wc_VerifyEccsiHash(key, WC_HASH_TYPE_SHA256, NULL, 0, sig, 0,
  29313. &valid);
  29314. if (ret != BAD_FUNC_ARG)
  29315. return -10104;
  29316. ret = wc_VerifyEccsiHash(NULL, WC_HASH_TYPE_SHA256, data, 0, sig, 0,
  29317. &valid);
  29318. if (ret != BAD_FUNC_ARG)
  29319. return -10106;
  29320. ret = wc_VerifyEccsiHash(key, WC_HASH_TYPE_SHA256, data, 0, sig, 0,
  29321. &valid);
  29322. if (ret != BAD_STATE_E)
  29323. return -10106;
  29324. ret = wc_SetEccsiPair(key, ssk, pvt);
  29325. if (ret != 0)
  29326. return -10107;
  29327. /* Identity hash not set. */
  29328. ret = wc_SignEccsiHash(key, rng, WC_HASH_TYPE_SHA256, data, 0, NULL,
  29329. &sigSz);
  29330. if (ret != BAD_STATE_E)
  29331. return -10108;
  29332. wc_FreeEccsiKey(key);
  29333. return 0;
  29334. }
  29335. /* RFC 6507: Appendix A */
  29336. static int eccsi_kat_verify_test(EccsiKey* key, ecc_point* pvt)
  29337. {
  29338. int ret;
  29339. int verified;
  29340. const byte msg[] = { 0x6D, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x00 };
  29341. word32 msgSz = sizeof(msg);
  29342. byte hash[WC_SHA256_DIGEST_SIZE];
  29343. byte hashSz = WC_SHA256_DIGEST_SIZE;
  29344. static const byte id[] = {
  29345. 0x32, 0x30, 0x31, 0x31, 0x2D, 0x30, 0x32, 0x00,
  29346. 0x74, 0x65, 0x6C, 0x3A, 0x2B, 0x34, 0x34, 0x37,
  29347. 0x37, 0x30, 0x30, 0x39, 0x30, 0x30, 0x31, 0x32,
  29348. 0x33, 0x00
  29349. };
  29350. word32 idSz = sizeof(id);
  29351. static const byte sig[] = {
  29352. 0x26, 0x9D, 0x4C, 0x8F, 0xDE, 0xB6, 0x6A, 0x74,
  29353. 0xE4, 0xEF, 0x8C, 0x0D, 0x5D, 0xCC, 0x59, 0x7D,
  29354. 0xDF, 0xE6, 0x02, 0x9C, 0x2A, 0xFF, 0xC4, 0x93,
  29355. 0x60, 0x08, 0xCD, 0x2C, 0xC1, 0x04, 0x5D, 0x81,
  29356. 0xE0, 0x9B, 0x52, 0x8D, 0x0E, 0xF8, 0xD6, 0xDF,
  29357. 0x1A, 0xA3, 0xEC, 0xBF, 0x80, 0x11, 0x0C, 0xFC,
  29358. 0xEC, 0x9F, 0xC6, 0x82, 0x52, 0xCE, 0xBB, 0x67,
  29359. 0x9F, 0x41, 0x34, 0x84, 0x69, 0x40, 0xCC, 0xFD,
  29360. 0x04,
  29361. 0x75, 0x8A, 0x14, 0x27, 0x79, 0xBE, 0x89, 0xE8,
  29362. 0x29, 0xE7, 0x19, 0x84, 0xCB, 0x40, 0xEF, 0x75,
  29363. 0x8C, 0xC4, 0xAD, 0x77, 0x5F, 0xC5, 0xB9, 0xA3,
  29364. 0xE1, 0xC8, 0xED, 0x52, 0xF6, 0xFA, 0x36, 0xD9,
  29365. 0xA7, 0x9D, 0x24, 0x76, 0x92, 0xF4, 0xED, 0xA3,
  29366. 0xA6, 0xBD, 0xAB, 0x77, 0xD6, 0xAA, 0x64, 0x74,
  29367. 0xA4, 0x64, 0xAE, 0x49, 0x34, 0x66, 0x3C, 0x52,
  29368. 0x65, 0xBA, 0x70, 0x18, 0xBA, 0x09, 0x1F, 0x79
  29369. };
  29370. word32 sigSz = sizeof(sig);
  29371. static const byte pubData[] = {
  29372. 0x50, 0xD4, 0x67, 0x0B, 0xDE, 0x75, 0x24, 0x4F,
  29373. 0x28, 0xD2, 0x83, 0x8A, 0x0D, 0x25, 0x55, 0x8A,
  29374. 0x7A, 0x72, 0x68, 0x6D, 0x45, 0x22, 0xD4, 0xC8,
  29375. 0x27, 0x3F, 0xB6, 0x44, 0x2A, 0xEB, 0xFA, 0x93,
  29376. 0xDB, 0xDD, 0x37, 0x55, 0x1A, 0xFD, 0x26, 0x3B,
  29377. 0x5D, 0xFD, 0x61, 0x7F, 0x39, 0x60, 0xC6, 0x5A,
  29378. 0x8C, 0x29, 0x88, 0x50, 0xFF, 0x99, 0xF2, 0x03,
  29379. 0x66, 0xDC, 0xE7, 0xD4, 0x36, 0x72, 0x17, 0xF4
  29380. };
  29381. static const byte expHash[] = {
  29382. 0x49, 0x0f, 0x3f, 0xeb, 0xbc, 0x1c, 0x90, 0x2f,
  29383. 0x62, 0x89, 0x72, 0x3d, 0x7f, 0x8c, 0xbf, 0x79,
  29384. 0xdb, 0x88, 0x93, 0x08, 0x49, 0xd1, 0x9f, 0x38,
  29385. 0xf0, 0x29, 0x5b, 0x5c, 0x27, 0x6c, 0x14, 0xd1
  29386. };
  29387. ret = wc_ImportEccsiPublicKey(key, pubData, sizeof(pubData), 0);
  29388. if (ret != 0)
  29389. return -10108;
  29390. ret = wc_DecodeEccsiPvtFromSig(key, sig, sigSz, pvt);
  29391. if (ret != 0)
  29392. return -10109;
  29393. ret = wc_HashEccsiId(key, WC_HASH_TYPE_SHA256, id, idSz, pvt, hash,
  29394. &hashSz);
  29395. if (ret != 0)
  29396. return -10112;
  29397. if (hashSz != sizeof(expHash))
  29398. return -10113;
  29399. if (XMEMCMP(hash, expHash, hashSz) != 0)
  29400. return -10114;
  29401. ret = wc_SetEccsiHash(key, hash, hashSz);
  29402. if (ret != 0)
  29403. return -10112;
  29404. ret = wc_VerifyEccsiHash(key, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  29405. &verified);
  29406. if (ret != 0)
  29407. return -10115;
  29408. if (!verified)
  29409. return -10116;
  29410. return 0;
  29411. }
  29412. static int eccsi_enc_dec_pair_test(EccsiKey* priv, mp_int* ssk, ecc_point* pvt)
  29413. {
  29414. int ret;
  29415. byte data[32 * 3];
  29416. word32 sz;
  29417. ecc_point* decPvt = NULL;
  29418. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  29419. mp_int *decSsk = (mp_int *)XMALLOC(sizeof(*decSsk), HEAP_HINT,
  29420. DYNAMIC_TYPE_TMP_BUFFER);
  29421. if (decSsk == NULL)
  29422. return -10173;
  29423. #else
  29424. mp_int decSsk[1];
  29425. #endif
  29426. ret = mp_init(decSsk);
  29427. if (ret != 0)
  29428. ERROR_OUT(-10117, out);
  29429. decPvt = wc_ecc_new_point();
  29430. if (decPvt == NULL)
  29431. ERROR_OUT(-10118, out);
  29432. ret = wc_EncodeEccsiPair(priv, ssk, pvt, NULL, &sz);
  29433. if (ret != LENGTH_ONLY_E)
  29434. ERROR_OUT(-10119, out);
  29435. if (sz != 32 * 3)
  29436. ERROR_OUT(-10120, out);
  29437. ret = wc_EncodeEccsiPair(priv, ssk, pvt, data, &sz);
  29438. if (ret != 0)
  29439. ERROR_OUT(-10121, out);
  29440. if (sz != 32* 3)
  29441. ERROR_OUT(-10122, out);
  29442. ret = wc_DecodeEccsiPair(priv, data, sz, decSsk, decPvt);
  29443. if (ret != 0)
  29444. ERROR_OUT(-10123, out);
  29445. if (mp_cmp(ssk, decSsk) != MP_EQ)
  29446. ERROR_OUT(-10124, out);
  29447. if (wc_ecc_cmp_point(pvt, decPvt) != MP_EQ)
  29448. ERROR_OUT(-10125, out);
  29449. ret = wc_EncodeEccsiSsk(priv, ssk, NULL, &sz);
  29450. if (ret != LENGTH_ONLY_E)
  29451. ERROR_OUT(-10119, out);
  29452. if (sz != 32)
  29453. ERROR_OUT(-10120, out);
  29454. ret = wc_EncodeEccsiSsk(priv, ssk, data, &sz);
  29455. if (ret != 0)
  29456. ERROR_OUT(-10121, out);
  29457. if (sz != 32)
  29458. ERROR_OUT(-10122, out);
  29459. ret = wc_DecodeEccsiSsk(priv, data, sz, decSsk);
  29460. if (ret != 0)
  29461. ERROR_OUT(-10123, out);
  29462. if (mp_cmp(ssk, decSsk) != MP_EQ)
  29463. ERROR_OUT(-10124, out);
  29464. ret = wc_EncodeEccsiPvt(priv, pvt, NULL, &sz, 1);
  29465. if (ret != LENGTH_ONLY_E)
  29466. ERROR_OUT(-10126, out);
  29467. if (sz != 32 * 2)
  29468. ERROR_OUT(-10127, out);
  29469. ret = wc_EncodeEccsiPvt(priv, pvt, data, &sz, 1);
  29470. if (ret != 0)
  29471. ERROR_OUT(-10128, out);
  29472. if (sz != 32 * 2)
  29473. ERROR_OUT(-10129, out);
  29474. ret = wc_DecodeEccsiPvt(priv, data, sz, decPvt);
  29475. if (ret != 0)
  29476. ERROR_OUT(-10130, out);
  29477. if (wc_ecc_cmp_point(pvt, decPvt) != MP_EQ)
  29478. ERROR_OUT(-10131, out);
  29479. sz = sizeof(data);
  29480. ret = wc_EncodeEccsiPvt(priv, pvt, data, &sz, 0);
  29481. if (ret != 0)
  29482. ERROR_OUT(-10128, out);
  29483. if (sz != 32 * 2 + 1)
  29484. ERROR_OUT(-10129, out);
  29485. ret = wc_DecodeEccsiPvt(priv, data, sz, decPvt);
  29486. if (ret != 0)
  29487. ERROR_OUT(-10130, out);
  29488. if (wc_ecc_cmp_point(pvt, decPvt) != MP_EQ)
  29489. ERROR_OUT(-10131, out);
  29490. wc_ecc_del_point(decPvt);
  29491. out:
  29492. mp_free(decSsk);
  29493. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  29494. XFREE(decSsk, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29495. #endif
  29496. return ret;
  29497. }
  29498. static int eccsi_imp_exp_key_test(EccsiKey* priv)
  29499. {
  29500. int ret;
  29501. byte data[32 * 3];
  29502. byte out[32 * 3];
  29503. word32 sz;
  29504. ret = wc_ExportEccsiKey(priv, NULL, &sz);
  29505. if (ret != LENGTH_ONLY_E)
  29506. return -10132;
  29507. if (sz != 32 * 3)
  29508. return -10133;
  29509. ret = wc_ExportEccsiKey(priv, data, &sz);
  29510. if (ret != 0)
  29511. return -10134;
  29512. ret = wc_ImportEccsiKey(priv, data, sz);
  29513. if (ret != 0)
  29514. return -10135;
  29515. ret = wc_ExportEccsiKey(priv, NULL, &sz);
  29516. if (ret != LENGTH_ONLY_E)
  29517. return -10132;
  29518. if (sz != 32 * 3)
  29519. return -10143;
  29520. ret = wc_ExportEccsiKey(priv, out, &sz);
  29521. if (ret != 0)
  29522. return -10144;
  29523. if (sz != 32 * 3)
  29524. return -10145;
  29525. if (XMEMCMP(data, out, sz) != 0)
  29526. return -10146;
  29527. ret = wc_ExportEccsiPrivateKey(priv, NULL, &sz);
  29528. if (ret != LENGTH_ONLY_E)
  29529. return -10156;
  29530. if (sz != 32)
  29531. return -10157;
  29532. ret = wc_ExportEccsiPrivateKey(priv, data, &sz);
  29533. if (ret != 0)
  29534. return -10158;
  29535. ret = wc_ImportEccsiPrivateKey(priv, data, sz);
  29536. if (ret != 0)
  29537. return -10159;
  29538. ret = wc_ExportEccsiPrivateKey(priv, NULL, &sz);
  29539. if (ret != LENGTH_ONLY_E)
  29540. return -10152;
  29541. if (sz != 32)
  29542. return -10163;
  29543. ret = wc_ExportEccsiPrivateKey(priv, out, &sz);
  29544. if (ret != 0)
  29545. return -10164;
  29546. if (sz != 32)
  29547. return -10165;
  29548. if (XMEMCMP(data, out, sz) != 0)
  29549. return -10166;
  29550. return 0;
  29551. }
  29552. static int eccsi_imp_exp_pubkey_test(EccsiKey* key1, EccsiKey* key2)
  29553. {
  29554. int ret;
  29555. byte data[32 * 2 + 1];
  29556. byte pubData[32 * 2 + 1];
  29557. word32 sz;
  29558. ret = wc_ExportEccsiPublicKey(key1, NULL, &sz, 1);
  29559. if (ret != LENGTH_ONLY_E)
  29560. return -10136;
  29561. if (sz != 32 * 2)
  29562. return -10137;
  29563. ret = wc_ExportEccsiPublicKey(key1, data, &sz, 1);
  29564. if (ret != 0)
  29565. return -10138;
  29566. ret = wc_ImportEccsiPublicKey(key2, data, sz, 1);
  29567. if (ret != 0)
  29568. return -10139;
  29569. sz = sizeof(pubData);
  29570. ret = wc_ExportEccsiPublicKey(key2, pubData, &sz, 1);
  29571. if (ret != 0)
  29572. return -10140;
  29573. if (sz != 32 * 2)
  29574. return -10141;
  29575. if (XMEMCMP(data, pubData, sz) != 0)
  29576. return -10142;
  29577. sz = sizeof(pubData);
  29578. ret = wc_ExportEccsiPublicKey(key2, pubData, &sz, 0);
  29579. if (ret != 0)
  29580. return -10140;
  29581. if (sz != 32 * 2 + 1)
  29582. return -10141;
  29583. if (pubData[0] != 0x04)
  29584. return -10140;
  29585. if (XMEMCMP(pubData + 1, data, sz - 1) != 0)
  29586. return -10142;
  29587. ret = wc_ImportEccsiPublicKey(key2, pubData, sz, 0);
  29588. if (ret != 0)
  29589. return -10139;
  29590. return 0;
  29591. }
  29592. static int eccsi_make_key_test(EccsiKey* priv, EccsiKey* pub, WC_RNG* rng,
  29593. mp_int* ssk, ecc_point* pvt)
  29594. {
  29595. int ret;
  29596. char mail[] = "test@wolfssl.com";
  29597. byte* id = (byte*)mail;
  29598. word32 idSz = (word32) XSTRLEN(mail);
  29599. int valid;
  29600. ret = wc_MakeEccsiKey(priv, rng);
  29601. if (ret != 0)
  29602. return -10143;
  29603. ret = eccsi_imp_exp_key_test(priv);
  29604. if (ret < 0)
  29605. return ret;
  29606. ret = eccsi_imp_exp_pubkey_test(priv, pub);
  29607. if (ret < 0)
  29608. return ret;
  29609. ret = wc_MakeEccsiPair(priv, rng, WC_HASH_TYPE_SHA256, id, idSz, ssk, pvt);
  29610. if (ret != 0)
  29611. return -10144;
  29612. ret = wc_ValidateEccsiPair(pub, WC_HASH_TYPE_SHA256, id, idSz, ssk, pvt,
  29613. &valid);
  29614. if (ret != 0)
  29615. return -10145;
  29616. if (!valid)
  29617. return -10146;
  29618. ret = eccsi_enc_dec_pair_test(priv, ssk, pvt);
  29619. if (ret != 0)
  29620. return ret;
  29621. return 0;
  29622. }
  29623. static int eccsi_sign_verify_test(EccsiKey* priv, EccsiKey* pub, WC_RNG* rng,
  29624. mp_int* ssk, ecc_point* pvt)
  29625. {
  29626. int ret;
  29627. byte hashPriv[WC_MAX_DIGEST_SIZE];
  29628. byte hashPub[WC_MAX_DIGEST_SIZE];
  29629. byte hashSz;
  29630. byte sig[144];
  29631. word32 sigSz;
  29632. int verified, valid;
  29633. char mail[] = "test@wolfssl.com";
  29634. byte* id = (byte*)mail;
  29635. word32 idSz = (word32) XSTRLEN(mail);
  29636. byte msg[] = { 0x00 };
  29637. word32 msgSz = sizeof(msg);
  29638. ret = wc_HashEccsiId(priv, WC_HASH_TYPE_SHA256, id, idSz, pvt, hashPriv,
  29639. &hashSz);
  29640. if (ret != 0)
  29641. return -10147;
  29642. if (hashSz != 32)
  29643. return -10148;
  29644. ret = wc_HashEccsiId(priv, WC_HASH_TYPE_SHA256, id, idSz, pvt, hashPub,
  29645. &hashSz);
  29646. if (ret != 0)
  29647. return -10149;
  29648. if (hashSz != 32)
  29649. return -10150;
  29650. if (XMEMCMP(hashPriv, hashPub, hashSz) != 0)
  29651. return -10151;
  29652. ret = wc_SetEccsiHash(priv, hashPriv, hashSz);
  29653. if (ret != 0)
  29654. return -10149;
  29655. ret = wc_SetEccsiPair(priv, ssk, pvt);
  29656. if (ret != 0)
  29657. return -10149;
  29658. ret = wc_SignEccsiHash(priv, rng, WC_HASH_TYPE_SHA256, msg, msgSz, NULL,
  29659. &sigSz);
  29660. if (ret != LENGTH_ONLY_E)
  29661. return -10152;
  29662. if (sigSz != 129)
  29663. return -10153;
  29664. ret = wc_SignEccsiHash(priv, rng, WC_HASH_TYPE_SHA256, msg, msgSz, sig,
  29665. &sigSz);
  29666. if (ret != 0)
  29667. return -10154;
  29668. ret = wc_SetEccsiHash(pub, hashPub, hashSz);
  29669. if (ret != 0)
  29670. return -10149;
  29671. ret = wc_VerifyEccsiHash(pub, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  29672. &verified);
  29673. if (ret != 0)
  29674. return -10155;
  29675. if (!verified)
  29676. return -10156;
  29677. /* Check that changing HS results in verification failure. */
  29678. hashPub[0] ^= 0x80;
  29679. ret = wc_SetEccsiHash(pub, hashPub, hashSz);
  29680. if (ret != 0)
  29681. return -10149;
  29682. ret = wc_VerifyEccsiHash(pub, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  29683. &verified);
  29684. if (ret != 0)
  29685. return -10157;
  29686. if (verified)
  29687. return -10158;
  29688. hashPub[0] ^= 0x80;
  29689. ret = wc_SetEccsiHash(pub, hashPub, hashSz);
  29690. if (ret != 0)
  29691. return -10149;
  29692. /* Check that changing msg results in verification failure. */
  29693. msg[0] ^= 0x80;
  29694. ret = wc_VerifyEccsiHash(pub, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  29695. &verified);
  29696. if (ret != 0)
  29697. return -10159;
  29698. if (verified)
  29699. return -10160;
  29700. msg[0] ^= 0x80;
  29701. /* Check that changing signature results in verification failure. */
  29702. sig[0] ^= 0x80;
  29703. ret = wc_VerifyEccsiHash(pub, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  29704. &verified);
  29705. if (ret != 0)
  29706. return -10161;
  29707. if (verified)
  29708. return -10162;
  29709. sig[0] ^= 0x80;
  29710. /* Check that key state hasn't been invalidated. */
  29711. ret = wc_VerifyEccsiHash(pub, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  29712. &verified);
  29713. if (ret != 0)
  29714. return -10163;
  29715. if (!verified)
  29716. return -10164;
  29717. /* Check that verifying with the private key works. */
  29718. ret = wc_VerifyEccsiHash(priv, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  29719. &verified);
  29720. if (ret != 0)
  29721. return -10165;
  29722. if (!verified)
  29723. return -10166;
  29724. /* Check that the KPAK is converted from montgomery form. */
  29725. ret = eccsi_imp_exp_key_test(priv);
  29726. if (ret != 0)
  29727. return ret;
  29728. /* Check that KPAK can converted to Montgomery form again. */
  29729. ret = wc_VerifyEccsiHash(priv, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  29730. &verified);
  29731. if (ret != 0)
  29732. return -10167;
  29733. if (!verified)
  29734. return -10168;
  29735. /* Check that the KPAK is converted from montgomery form. */
  29736. ret = wc_ValidateEccsiPair(pub, WC_HASH_TYPE_SHA256, id, idSz, ssk, pvt,
  29737. &valid);
  29738. if (ret != 0)
  29739. return -10169;
  29740. if (!valid)
  29741. return -10170;
  29742. /* Check that KPAK can converted to Montgomery form again. */
  29743. ret = wc_VerifyEccsiHash(priv, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  29744. &verified);
  29745. if (ret != 0)
  29746. return -10171;
  29747. if (!verified)
  29748. return -10172;
  29749. /* Check that the KPAK is converted from montgomery form. */
  29750. ret = eccsi_imp_exp_pubkey_test(priv, pub);
  29751. if (ret != 0)
  29752. return ret;
  29753. return 0;
  29754. }
  29755. int eccsi_test(void)
  29756. {
  29757. int ret = 0;
  29758. WC_RNG rng;
  29759. int rng_inited = 0;
  29760. EccsiKey* priv = NULL;
  29761. EccsiKey* pub = NULL;
  29762. mp_int* ssk = NULL;
  29763. ecc_point* pvt = NULL;
  29764. priv = (EccsiKey*)XMALLOC(sizeof(EccsiKey), HEAP_HINT,
  29765. DYNAMIC_TYPE_TMP_BUFFER);
  29766. if (priv == NULL)
  29767. ret = -10205;
  29768. else
  29769. XMEMSET(priv, 0, sizeof(*priv));
  29770. if (ret == 0) {
  29771. pub = (EccsiKey*)XMALLOC(sizeof(EccsiKey), HEAP_HINT,
  29772. DYNAMIC_TYPE_TMP_BUFFER);
  29773. if (pub == NULL)
  29774. ret = -10206;
  29775. else
  29776. XMEMSET(pub, 0, sizeof(*pub));
  29777. }
  29778. if (ret == 0) {
  29779. ssk = (mp_int*)XMALLOC(sizeof(mp_int), HEAP_HINT,
  29780. DYNAMIC_TYPE_TMP_BUFFER);
  29781. if (ssk == NULL)
  29782. ret = -10207;
  29783. else
  29784. XMEMSET(ssk, 0, sizeof(*ssk));
  29785. }
  29786. if (ret == 0) {
  29787. #ifndef HAVE_FIPS
  29788. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  29789. #else
  29790. ret = wc_InitRng(&rng);
  29791. #endif
  29792. if (ret != 0)
  29793. ret = -10200;
  29794. else
  29795. rng_inited = 1;
  29796. }
  29797. if (ret == 0) {
  29798. pvt = wc_ecc_new_point();
  29799. if (pvt == NULL)
  29800. ret = -10201;
  29801. }
  29802. if (ret == 0) {
  29803. ret = mp_init(ssk);
  29804. if (ret != 0)
  29805. ret = -10202;
  29806. }
  29807. if (ret == 0) {
  29808. ret = eccsi_api_test(&rng, priv, ssk, pvt);
  29809. }
  29810. if (ret == 0) {
  29811. ret = wc_InitEccsiKey(pub, HEAP_HINT, INVALID_DEVID);
  29812. if (ret != 0)
  29813. ret = -10203;
  29814. }
  29815. if (ret == 0) {
  29816. ret = wc_InitEccsiKey(priv, HEAP_HINT, INVALID_DEVID);
  29817. if (ret != 0)
  29818. ret = -10204;
  29819. }
  29820. if (ret == 0) {
  29821. ret = eccsi_kat_verify_test(pub, pvt);
  29822. }
  29823. if (ret == 0) {
  29824. ret = eccsi_make_key_test(priv, pub, &rng, ssk, pvt);
  29825. }
  29826. if (ret == 0) {
  29827. ret = eccsi_sign_verify_test(priv, pub, &rng, ssk, pvt);
  29828. }
  29829. if (pvt != NULL)
  29830. wc_ecc_del_point(pvt);
  29831. if (rng_inited)
  29832. wc_FreeRng(&rng);
  29833. if (ssk != NULL) {
  29834. mp_free(ssk);
  29835. XFREE(ssk, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29836. }
  29837. if (pub != NULL) {
  29838. wc_FreeEccsiKey(pub);
  29839. XFREE(pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29840. }
  29841. if (priv != NULL) {
  29842. wc_FreeEccsiKey(priv);
  29843. XFREE(priv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29844. }
  29845. return ret;
  29846. }
  29847. #endif /* WOLFCRYPT_HAVE_ECCSI */
  29848. #ifdef WOLFCRYPT_HAVE_SAKKE
  29849. static int sakke_api_test(WC_RNG* rng, SakkeKey* key, ecc_point* rsk)
  29850. {
  29851. int ret;
  29852. byte id[1] = { 0x00 };
  29853. int valid;
  29854. byte data[256];
  29855. word32 sz;
  29856. byte auth[257];
  29857. word16 authSz;
  29858. byte ssv[256];
  29859. word16 ssvSz;
  29860. word32 len;
  29861. ret = wc_InitSakkeKey_ex(NULL, 128, ECC_SAKKE_1, NULL, INVALID_DEVID);
  29862. if (ret != BAD_FUNC_ARG)
  29863. return -10205;
  29864. ret = wc_InitSakkeKey_ex(NULL, 128, ECC_SAKKE_1, HEAP_HINT, INVALID_DEVID);
  29865. if (ret != BAD_FUNC_ARG)
  29866. return -10206;
  29867. wc_FreeSakkeKey(NULL);
  29868. XMEMSET(key, 0, sizeof(*key));
  29869. wc_FreeSakkeKey(key);
  29870. ret = wc_InitSakkeKey_ex(key, 128, ECC_SAKKE_1, HEAP_HINT, INVALID_DEVID);
  29871. if (ret != 0)
  29872. return -10207;
  29873. ret = wc_MakeSakkeKey(NULL, NULL);
  29874. if (ret != BAD_FUNC_ARG)
  29875. return -10208;
  29876. ret = wc_MakeSakkeKey(key, NULL);
  29877. if (ret != BAD_FUNC_ARG)
  29878. return -10209;
  29879. ret = wc_MakeSakkeKey(NULL, rng);
  29880. if (ret != BAD_FUNC_ARG)
  29881. return -10210;
  29882. ret = wc_MakeSakkePublicKey(NULL, NULL);
  29883. if (ret != BAD_FUNC_ARG)
  29884. return -10211;
  29885. ret = wc_MakeSakkePublicKey(key, NULL);
  29886. if (ret != BAD_FUNC_ARG)
  29887. return -10212;
  29888. ret = wc_MakeSakkePublicKey(NULL, rsk);
  29889. if (ret != BAD_FUNC_ARG)
  29890. return -10213;
  29891. ret = wc_MakeSakkeRsk(NULL, NULL, 1, NULL);
  29892. if (ret != BAD_FUNC_ARG)
  29893. return -10214;
  29894. ret = wc_MakeSakkeRsk(key, id, 1, NULL);
  29895. if (ret != BAD_FUNC_ARG)
  29896. return -10215;
  29897. ret = wc_MakeSakkeRsk(key, NULL, 1, rsk);
  29898. if (ret != BAD_FUNC_ARG)
  29899. return -10216;
  29900. ret = wc_MakeSakkeRsk(NULL, id, 1, rsk);
  29901. if (ret != BAD_FUNC_ARG)
  29902. return -10217;
  29903. ret = wc_ValidateSakkeRsk(NULL, NULL, 1, NULL, NULL);
  29904. if (ret != BAD_FUNC_ARG)
  29905. return -10218;
  29906. ret = wc_ValidateSakkeRsk(key, id, 1, rsk, NULL);
  29907. if (ret != BAD_FUNC_ARG)
  29908. return -10219;
  29909. ret = wc_ValidateSakkeRsk(NULL, id, 1, rsk, &valid);
  29910. if (ret != BAD_FUNC_ARG)
  29911. return -10220;
  29912. ret = wc_ExportSakkeKey(NULL, NULL, NULL);
  29913. if (ret != BAD_FUNC_ARG)
  29914. return -10221;
  29915. ret = wc_ExportSakkeKey(key, data, NULL);
  29916. if (ret != BAD_FUNC_ARG)
  29917. return -10222;
  29918. ret = wc_ExportSakkeKey(NULL, data, &sz);
  29919. if (ret != BAD_FUNC_ARG)
  29920. return -10223;
  29921. ret = wc_ImportSakkeKey(NULL, NULL, 1);
  29922. if (ret != BAD_FUNC_ARG)
  29923. return -10224;
  29924. ret = wc_ImportSakkeKey(key, NULL, 1);
  29925. if (ret != BAD_FUNC_ARG)
  29926. return -10225;
  29927. ret = wc_ImportSakkeKey(NULL, data, 1);
  29928. if (ret != BAD_FUNC_ARG)
  29929. return -10226;
  29930. ret = wc_ExportSakkePrivateKey(NULL, NULL, NULL);
  29931. if (ret != BAD_FUNC_ARG)
  29932. return -10227;
  29933. ret = wc_ExportSakkePrivateKey(key, data, NULL);
  29934. if (ret != BAD_FUNC_ARG)
  29935. return -10228;
  29936. ret = wc_ExportSakkePrivateKey(NULL, data, &sz);
  29937. if (ret != BAD_FUNC_ARG)
  29938. return -10229;
  29939. ret = wc_ImportSakkePrivateKey(NULL, NULL, 1);
  29940. if (ret != BAD_FUNC_ARG)
  29941. return -10230;
  29942. ret = wc_ImportSakkePrivateKey(key, NULL, 1);
  29943. if (ret != BAD_FUNC_ARG)
  29944. return -10231;
  29945. ret = wc_ImportSakkePrivateKey(NULL, data, 1);
  29946. if (ret != BAD_FUNC_ARG)
  29947. return -10232;
  29948. sz = sizeof(data);
  29949. ret = wc_EncodeSakkeRsk(NULL, NULL, data, NULL, 1);
  29950. if (ret != BAD_FUNC_ARG)
  29951. return -10233;
  29952. ret = wc_EncodeSakkeRsk(key, rsk, data, NULL, 1);
  29953. if (ret != BAD_FUNC_ARG)
  29954. return -10234;
  29955. ret = wc_EncodeSakkeRsk(key, NULL, data, &sz, 1);
  29956. if (ret != BAD_FUNC_ARG)
  29957. return -10235;
  29958. ret = wc_EncodeSakkeRsk(NULL, rsk, data, &sz, 1);
  29959. if (ret != BAD_FUNC_ARG)
  29960. return -10236;
  29961. ret = wc_DecodeSakkeRsk(NULL, NULL, sz, NULL);
  29962. if (ret != BAD_FUNC_ARG)
  29963. return -10237;
  29964. ret = wc_DecodeSakkeRsk(key, data, sz, NULL);
  29965. if (ret != BAD_FUNC_ARG)
  29966. return -10238;
  29967. ret = wc_DecodeSakkeRsk(key, NULL, sz, rsk);
  29968. if (ret != BAD_FUNC_ARG)
  29969. return -10239;
  29970. ret = wc_DecodeSakkeRsk(NULL, data, sz, rsk);
  29971. if (ret != BAD_FUNC_ARG)
  29972. return -10240;
  29973. ret = wc_ImportSakkeRsk(NULL, NULL, sz);
  29974. if (ret != BAD_FUNC_ARG)
  29975. return -10237;
  29976. ret = wc_ImportSakkeRsk(key, NULL, sz);
  29977. if (ret != BAD_FUNC_ARG)
  29978. return -10237;
  29979. ret = wc_ImportSakkeRsk(NULL, data, sz);
  29980. if (ret != BAD_FUNC_ARG)
  29981. return -10237;
  29982. ret = wc_ImportSakkeRsk(key, data, 1);
  29983. if (ret != BUFFER_E)
  29984. return -10237;
  29985. ret = wc_GenerateSakkeRskTable(NULL, NULL, data, NULL);
  29986. if (ret != BAD_FUNC_ARG)
  29987. return -10241;
  29988. ret = wc_GenerateSakkeRskTable(key, NULL, data, NULL);
  29989. if (ret != BAD_FUNC_ARG)
  29990. return -10242;
  29991. ret = wc_GenerateSakkeRskTable(NULL, rsk, data, NULL);
  29992. if (ret != BAD_FUNC_ARG)
  29993. return -10243;
  29994. ret = wc_GenerateSakkeRskTable(NULL, NULL, data, &len);
  29995. if (ret != BAD_FUNC_ARG)
  29996. return -10244;
  29997. ret = wc_GenerateSakkeRskTable(key, rsk, data, NULL);
  29998. if (ret != BAD_FUNC_ARG)
  29999. return -10245;
  30000. ret = wc_GenerateSakkeRskTable(key, NULL, data, &len);
  30001. if (ret != BAD_FUNC_ARG)
  30002. return -10246;
  30003. ret = wc_GenerateSakkeRskTable(NULL, rsk, data, &len);
  30004. if (ret != BAD_FUNC_ARG)
  30005. return -10247;
  30006. ret = wc_GenerateSakkeRskTable(key, rsk, NULL, &len);
  30007. if (ret != LENGTH_ONLY_E)
  30008. return -10248;
  30009. len--;
  30010. ret = wc_GenerateSakkeRskTable(key, rsk, data, &len);
  30011. if (ret != BUFFER_E)
  30012. return -10249;
  30013. ret = wc_ExportSakkePublicKey(NULL, data, NULL, 1);
  30014. if (ret != BAD_FUNC_ARG)
  30015. return -10250;
  30016. ret = wc_ExportSakkePublicKey(key, data, NULL, 1);
  30017. if (ret != BAD_FUNC_ARG)
  30018. return -10251;
  30019. ret = wc_ExportSakkePublicKey(NULL, data, &sz, 1);
  30020. if (ret != BAD_FUNC_ARG)
  30021. return -10252;
  30022. ret = wc_ImportSakkePublicKey(NULL, NULL, sz, 1);
  30023. if (ret != BAD_FUNC_ARG)
  30024. return -10253;
  30025. ret = wc_ImportSakkePublicKey(key, NULL, sz, 1);
  30026. if (ret != BAD_FUNC_ARG)
  30027. return -10254;
  30028. ret = wc_ImportSakkePublicKey(NULL, data, sz, 1);
  30029. if (ret != BAD_FUNC_ARG)
  30030. return -10255;
  30031. ret = wc_GetSakkeAuthSize(NULL, NULL);
  30032. if (ret != BAD_FUNC_ARG)
  30033. return -10256;
  30034. ret = wc_GetSakkeAuthSize(key, NULL);
  30035. if (ret != BAD_FUNC_ARG)
  30036. return -10257;
  30037. ret = wc_GetSakkeAuthSize(NULL, &authSz);
  30038. if (ret != BAD_FUNC_ARG)
  30039. return -10258;
  30040. ret = wc_MakeSakkePointI(NULL, NULL, SAKKE_ID_MAX_SIZE + 1);
  30041. if (ret != BAD_FUNC_ARG)
  30042. return -10259;
  30043. ret = wc_MakeSakkePointI(key, NULL, SAKKE_ID_MAX_SIZE + 1);
  30044. if (ret != BAD_FUNC_ARG)
  30045. return -10260;
  30046. ret = wc_MakeSakkePointI(NULL, id, 1);
  30047. if (ret != BAD_FUNC_ARG)
  30048. return -10261;
  30049. ret = wc_MakeSakkePointI(NULL, NULL, 1);
  30050. if (ret != BAD_FUNC_ARG)
  30051. return -10262;
  30052. ret = wc_MakeSakkePointI(key, id, SAKKE_ID_MAX_SIZE + 1);
  30053. if (ret != BAD_FUNC_ARG)
  30054. return -10263;
  30055. ret = wc_MakeSakkePointI(key, NULL, 1);
  30056. if (ret != BAD_FUNC_ARG)
  30057. return -10264;
  30058. ret = wc_MakeSakkePointI(NULL, id, 1);
  30059. if (ret != BAD_FUNC_ARG)
  30060. return -10265;
  30061. ret = wc_GenerateSakkePointITable(NULL, data, NULL);
  30062. if (ret != BAD_FUNC_ARG)
  30063. return -10266;
  30064. ret = wc_GenerateSakkePointITable(key, data, NULL);
  30065. if (ret != BAD_FUNC_ARG)
  30066. return -10267;
  30067. ret = wc_GenerateSakkePointITable(NULL, data, &len);
  30068. if (ret != BAD_FUNC_ARG)
  30069. return -10268;
  30070. ret = wc_GenerateSakkePointITable(key, NULL, &len);
  30071. if (ret != LENGTH_ONLY_E)
  30072. return -10269;
  30073. len--;
  30074. ret = wc_GenerateSakkePointITable(key, data, &len);
  30075. if (ret != BUFFER_E)
  30076. return -10270;
  30077. ret = wc_SetSakkePointITable(NULL, NULL, 1);
  30078. if (ret != BAD_FUNC_ARG)
  30079. return -10271;
  30080. ret = wc_SetSakkePointITable(key, NULL, 1);
  30081. if (ret != BAD_FUNC_ARG)
  30082. return -10272;
  30083. ret = wc_SetSakkePointITable(NULL, data, 1);
  30084. if (ret != BAD_FUNC_ARG)
  30085. return -10273;
  30086. ret = wc_SetSakkePointITable(key, data, 1);
  30087. if (ret != BUFFER_E)
  30088. return -10274;
  30089. ret = wc_ClearSakkePointITable(NULL);
  30090. if (ret != BAD_FUNC_ARG)
  30091. return -10275;
  30092. ret = wc_GetSakkePointI(NULL, data, NULL);
  30093. if (ret != BAD_FUNC_ARG)
  30094. return -10276;
  30095. ret = wc_GetSakkePointI(key, data, NULL);
  30096. if (ret != BAD_FUNC_ARG)
  30097. return -10277;
  30098. ret = wc_GetSakkePointI(NULL, data, &sz);
  30099. if (ret != BAD_FUNC_ARG)
  30100. return -10278;
  30101. sz = 1;
  30102. ret = wc_GetSakkePointI(key, data, &sz);
  30103. if (ret != BUFFER_E)
  30104. return -10279;
  30105. sz = 256;
  30106. ret = wc_SetSakkePointI(NULL, NULL, 1, NULL, sz);
  30107. if (ret != BAD_FUNC_ARG)
  30108. return -10280;
  30109. ret = wc_SetSakkePointI(key, NULL, 1, NULL, sz);
  30110. if (ret != BAD_FUNC_ARG)
  30111. return -10281;
  30112. ret = wc_SetSakkePointI(NULL, id, 1, NULL, sz);
  30113. if (ret != BAD_FUNC_ARG)
  30114. return -10282;
  30115. ret = wc_SetSakkePointI(NULL, NULL, 1, data, sz);
  30116. if (ret != BAD_FUNC_ARG)
  30117. return -10283;
  30118. ret = wc_SetSakkePointI(key, id, 1, NULL, sz);
  30119. if (ret != BAD_FUNC_ARG)
  30120. return -10284;
  30121. ret = wc_SetSakkePointI(key, NULL, 1, data, sz);
  30122. if (ret != BAD_FUNC_ARG)
  30123. return -10285;
  30124. ret = wc_SetSakkePointI(NULL, id, 1, data, sz);
  30125. if (ret != BAD_FUNC_ARG)
  30126. return -10286;
  30127. ret = wc_SetSakkePointI(key, id, SAKKE_ID_MAX_SIZE + 1, data, sz);
  30128. if (ret != BUFFER_E)
  30129. return -10287;
  30130. ret = wc_SetSakkePointI(key, id, 1, data, sz - 1);
  30131. if (ret != BUFFER_E)
  30132. return -10288;
  30133. ret = wc_SetSakkeIdentity(NULL, NULL, 1);
  30134. if (ret != BAD_FUNC_ARG)
  30135. return -10286;
  30136. ret = wc_SetSakkeIdentity(key, NULL, 1);
  30137. if (ret != BAD_FUNC_ARG)
  30138. return -10286;
  30139. ret = wc_SetSakkeIdentity(NULL, id, 1);
  30140. if (ret != BAD_FUNC_ARG)
  30141. return -10286;
  30142. ssvSz = sizeof(ssv);
  30143. ret = wc_MakeSakkeEncapsulatedSSV(NULL, WC_HASH_TYPE_SHA256, NULL, ssvSz,
  30144. auth, NULL);
  30145. if (ret != BAD_FUNC_ARG)
  30146. return -10289;
  30147. ret = wc_MakeSakkeEncapsulatedSSV(key, WC_HASH_TYPE_SHA256, NULL, ssvSz,
  30148. auth, NULL);
  30149. if (ret != BAD_FUNC_ARG)
  30150. return -10290;
  30151. ret = wc_MakeSakkeEncapsulatedSSV(NULL, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  30152. auth, NULL);
  30153. if (ret != BAD_FUNC_ARG)
  30154. return -10291;
  30155. ret = wc_MakeSakkeEncapsulatedSSV(NULL, WC_HASH_TYPE_SHA256, NULL, ssvSz,
  30156. auth, &authSz);
  30157. if (ret != BAD_FUNC_ARG)
  30158. return -10292;
  30159. ret = wc_MakeSakkeEncapsulatedSSV(key, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  30160. auth, NULL);
  30161. if (ret != BAD_FUNC_ARG)
  30162. return -10293;
  30163. ret = wc_MakeSakkeEncapsulatedSSV(key, WC_HASH_TYPE_SHA256, NULL, ssvSz,
  30164. auth, &authSz);
  30165. if (ret != BAD_FUNC_ARG)
  30166. return -10294;
  30167. ret = wc_MakeSakkeEncapsulatedSSV(NULL, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  30168. auth, &authSz);
  30169. if (ret != BAD_FUNC_ARG)
  30170. return -10295;
  30171. ret = wc_MakeSakkeEncapsulatedSSV(key, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  30172. auth, &authSz);
  30173. if (ret != BAD_STATE_E)
  30174. return -10295;
  30175. ret = wc_GenerateSakkeSSV(NULL, NULL, data, NULL);
  30176. if (ret != BAD_FUNC_ARG)
  30177. return -10296;
  30178. ret = wc_GenerateSakkeSSV(key, rng, data, NULL);
  30179. if (ret != BAD_FUNC_ARG)
  30180. return -10297;
  30181. ret = wc_GenerateSakkeSSV(key, NULL, data, &ssvSz);
  30182. if (ret != BAD_FUNC_ARG)
  30183. return -10298;
  30184. ret = wc_GenerateSakkeSSV(NULL, rng, data, &ssvSz);
  30185. if (ret != BAD_FUNC_ARG)
  30186. return -10299;
  30187. ret = wc_SetSakkeRsk(NULL, NULL, data, 1);
  30188. if (ret != BAD_FUNC_ARG)
  30189. return -10286;
  30190. ret = wc_SetSakkeRsk(key, NULL, data, 1);
  30191. if (ret != BAD_FUNC_ARG)
  30192. return -10286;
  30193. ret = wc_SetSakkeRsk(NULL, rsk, data, 1);
  30194. if (ret != BAD_FUNC_ARG)
  30195. return -10286;
  30196. ssvSz = sizeof(ssv);
  30197. authSz = sizeof(auth);
  30198. ret = wc_DeriveSakkeSSV(NULL, WC_HASH_TYPE_SHA256, NULL, ssvSz, NULL,
  30199. authSz);
  30200. if (ret != BAD_FUNC_ARG)
  30201. return -10300;
  30202. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, NULL, ssvSz, NULL,
  30203. authSz);
  30204. if (ret != BAD_FUNC_ARG)
  30205. return -10300;
  30206. ret = wc_DeriveSakkeSSV(NULL, WC_HASH_TYPE_SHA256, ssv, ssvSz, NULL,
  30207. authSz);
  30208. if (ret != BAD_FUNC_ARG)
  30209. return -10300;
  30210. ret = wc_DeriveSakkeSSV(NULL, WC_HASH_TYPE_SHA256, NULL, ssvSz, auth,
  30211. authSz);
  30212. if (ret != BAD_FUNC_ARG)
  30213. return -10300;
  30214. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, ssv, ssvSz, NULL,
  30215. authSz);
  30216. if (ret != BAD_FUNC_ARG)
  30217. return -10300;
  30218. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, NULL, ssvSz, auth,
  30219. authSz);
  30220. if (ret != BAD_FUNC_ARG)
  30221. return -10300;
  30222. ret = wc_DeriveSakkeSSV(NULL, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  30223. authSz);
  30224. if (ret != BAD_FUNC_ARG)
  30225. return -10300;
  30226. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  30227. authSz);
  30228. if (ret != BAD_STATE_E)
  30229. return -10300;
  30230. ret = wc_SetSakkeIdentity(key, id, 1);
  30231. if (ret != 0)
  30232. return -10286;
  30233. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  30234. authSz);
  30235. if (ret != BAD_STATE_E)
  30236. return -10300;
  30237. ret = wc_SetSakkeIdentity(key, id, 0);
  30238. if (ret != 0)
  30239. return -10286;
  30240. ret = wc_SetSakkeRsk(key, rsk, data, 1);
  30241. if (ret != 0)
  30242. return -10286;
  30243. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  30244. authSz);
  30245. if (ret != BAD_STATE_E)
  30246. return -10300;
  30247. wc_FreeSakkeKey(key);
  30248. return 0;
  30249. }
  30250. static int sakke_kat_derive_test(SakkeKey* key, ecc_point* rsk)
  30251. {
  30252. WOLFSSL_SMALL_STACK_STATIC const byte pubData[] = {
  30253. 0x59, 0x58, 0xEF, 0x1B, 0x16, 0x79, 0xBF, 0x09,
  30254. 0x9B, 0x3A, 0x03, 0x0D, 0xF2, 0x55, 0xAA, 0x6A,
  30255. 0x23, 0xC1, 0xD8, 0xF1, 0x43, 0xD4, 0xD2, 0x3F,
  30256. 0x75, 0x3E, 0x69, 0xBD, 0x27, 0xA8, 0x32, 0xF3,
  30257. 0x8C, 0xB4, 0xAD, 0x53, 0xDD, 0xEF, 0x42, 0x60,
  30258. 0xB0, 0xFE, 0x8B, 0xB4, 0x5C, 0x4C, 0x1F, 0xF5,
  30259. 0x10, 0xEF, 0xFE, 0x30, 0x03, 0x67, 0xA3, 0x7B,
  30260. 0x61, 0xF7, 0x01, 0xD9, 0x14, 0xAE, 0xF0, 0x97,
  30261. 0x24, 0x82, 0x5F, 0xA0, 0x70, 0x7D, 0x61, 0xA6,
  30262. 0xDF, 0xF4, 0xFB, 0xD7, 0x27, 0x35, 0x66, 0xCD,
  30263. 0xDE, 0x35, 0x2A, 0x0B, 0x04, 0xB7, 0xC1, 0x6A,
  30264. 0x78, 0x30, 0x9B, 0xE6, 0x40, 0x69, 0x7D, 0xE7,
  30265. 0x47, 0x61, 0x3A, 0x5F, 0xC1, 0x95, 0xE8, 0xB9,
  30266. 0xF3, 0x28, 0x85, 0x2A, 0x57, 0x9D, 0xB8, 0xF9,
  30267. 0x9B, 0x1D, 0x00, 0x34, 0x47, 0x9E, 0xA9, 0xC5,
  30268. 0x59, 0x5F, 0x47, 0xC4, 0xB2, 0xF5, 0x4F, 0xF2,
  30269. 0x15, 0x08, 0xD3, 0x75, 0x14, 0xDC, 0xF7, 0xA8,
  30270. 0xE1, 0x43, 0xA6, 0x05, 0x8C, 0x09, 0xA6, 0xBF,
  30271. 0x2C, 0x98, 0x58, 0xCA, 0x37, 0xC2, 0x58, 0x06,
  30272. 0x5A, 0xE6, 0xBF, 0x75, 0x32, 0xBC, 0x8B, 0x5B,
  30273. 0x63, 0x38, 0x38, 0x66, 0xE0, 0x75, 0x3C, 0x5A,
  30274. 0xC0, 0xE7, 0x27, 0x09, 0xF8, 0x44, 0x5F, 0x2E,
  30275. 0x61, 0x78, 0xE0, 0x65, 0x85, 0x7E, 0x0E, 0xDA,
  30276. 0x10, 0xF6, 0x82, 0x06, 0xB6, 0x35, 0x05, 0xED,
  30277. 0x87, 0xE5, 0x34, 0xFB, 0x28, 0x31, 0xFF, 0x95,
  30278. 0x7F, 0xB7, 0xDC, 0x61, 0x9D, 0xAE, 0x61, 0x30,
  30279. 0x1E, 0xEA, 0xCC, 0x2F, 0xDA, 0x36, 0x80, 0xEA,
  30280. 0x49, 0x99, 0x25, 0x8A, 0x83, 0x3C, 0xEA, 0x8F,
  30281. 0xC6, 0x7C, 0x6D, 0x19, 0x48, 0x7F, 0xB4, 0x49,
  30282. 0x05, 0x9F, 0x26, 0xCC, 0x8A, 0xAB, 0x65, 0x5A,
  30283. 0xB5, 0x8B, 0x7C, 0xC7, 0x96, 0xE2, 0x4E, 0x9A,
  30284. 0x39, 0x40, 0x95, 0x75, 0x4F, 0x5F, 0x8B, 0xAE
  30285. };
  30286. WOLFSSL_SMALL_STACK_STATIC const byte rskData[] = {
  30287. 0x93, 0xAF, 0x67, 0xE5, 0x00, 0x7B, 0xA6, 0xE6,
  30288. 0xA8, 0x0D, 0xA7, 0x93, 0xDA, 0x30, 0x0F, 0xA4,
  30289. 0xB5, 0x2D, 0x0A, 0x74, 0xE2, 0x5E, 0x6E, 0x7B,
  30290. 0x2B, 0x3D, 0x6E, 0xE9, 0xD1, 0x8A, 0x9B, 0x5C,
  30291. 0x50, 0x23, 0x59, 0x7B, 0xD8, 0x2D, 0x80, 0x62,
  30292. 0xD3, 0x40, 0x19, 0x56, 0x3B, 0xA1, 0xD2, 0x5C,
  30293. 0x0D, 0xC5, 0x6B, 0x7B, 0x97, 0x9D, 0x74, 0xAA,
  30294. 0x50, 0xF2, 0x9F, 0xBF, 0x11, 0xCC, 0x2C, 0x93,
  30295. 0xF5, 0xDF, 0xCA, 0x61, 0x5E, 0x60, 0x92, 0x79,
  30296. 0xF6, 0x17, 0x5C, 0xEA, 0xDB, 0x00, 0xB5, 0x8C,
  30297. 0x6B, 0xEE, 0x1E, 0x7A, 0x2A, 0x47, 0xC4, 0xF0,
  30298. 0xC4, 0x56, 0xF0, 0x52, 0x59, 0xA6, 0xFA, 0x94,
  30299. 0xA6, 0x34, 0xA4, 0x0D, 0xAE, 0x1D, 0xF5, 0x93,
  30300. 0xD4, 0xFE, 0xCF, 0x68, 0x8D, 0x5F, 0xC6, 0x78,
  30301. 0xBE, 0x7E, 0xFC, 0x6D, 0xF3, 0xD6, 0x83, 0x53,
  30302. 0x25, 0xB8, 0x3B, 0x2C, 0x6E, 0x69, 0x03, 0x6B,
  30303. 0x15, 0x5F, 0x0A, 0x27, 0x24, 0x10, 0x94, 0xB0,
  30304. 0x4B, 0xFB, 0x0B, 0xDF, 0xAC, 0x6C, 0x67, 0x0A,
  30305. 0x65, 0xC3, 0x25, 0xD3, 0x9A, 0x06, 0x9F, 0x03,
  30306. 0x65, 0x9D, 0x44, 0xCA, 0x27, 0xD3, 0xBE, 0x8D,
  30307. 0xF3, 0x11, 0x17, 0x2B, 0x55, 0x41, 0x60, 0x18,
  30308. 0x1C, 0xBE, 0x94, 0xA2, 0xA7, 0x83, 0x32, 0x0C,
  30309. 0xED, 0x59, 0x0B, 0xC4, 0x26, 0x44, 0x70, 0x2C,
  30310. 0xF3, 0x71, 0x27, 0x1E, 0x49, 0x6B, 0xF2, 0x0F,
  30311. 0x58, 0x8B, 0x78, 0xA1, 0xBC, 0x01, 0xEC, 0xBB,
  30312. 0x65, 0x59, 0x93, 0x4B, 0xDD, 0x2F, 0xB6, 0x5D,
  30313. 0x28, 0x84, 0x31, 0x8A, 0x33, 0xD1, 0xA4, 0x2A,
  30314. 0xDF, 0x5E, 0x33, 0xCC, 0x58, 0x00, 0x28, 0x0B,
  30315. 0x28, 0x35, 0x64, 0x97, 0xF8, 0x71, 0x35, 0xBA,
  30316. 0xB9, 0x61, 0x2A, 0x17, 0x26, 0x04, 0x24, 0x40,
  30317. 0x9A, 0xC1, 0x5F, 0xEE, 0x99, 0x6B, 0x74, 0x4C,
  30318. 0x33, 0x21, 0x51, 0x23, 0x5D, 0xEC, 0xB0, 0xF5
  30319. };
  30320. WOLFSSL_SMALL_STACK_STATIC const byte id[] = {
  30321. 0x32, 0x30, 0x31, 0x31, 0x2D, 0x30, 0x32, 0x00,
  30322. 0x74, 0x65, 0x6C, 0x3A, 0x2B, 0x34, 0x34, 0x37,
  30323. 0x37, 0x30, 0x30, 0x39, 0x30, 0x30, 0x31, 0x32,
  30324. 0x33, 0x00
  30325. };
  30326. WOLFSSL_SMALL_STACK_STATIC const byte ssv[] = {
  30327. 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0,
  30328. 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0
  30329. };
  30330. WOLFSSL_SMALL_STACK_STATIC const byte auth[] = {
  30331. 0x04,
  30332. 0x44, 0xE8, 0xAD, 0x44, 0xAB, 0x85, 0x92, 0xA6,
  30333. 0xA5, 0xA3, 0xDD, 0xCA, 0x5C, 0xF8, 0x96, 0xC7,
  30334. 0x18, 0x04, 0x36, 0x06, 0xA0, 0x1D, 0x65, 0x0D,
  30335. 0xEF, 0x37, 0xA0, 0x1F, 0x37, 0xC2, 0x28, 0xC3,
  30336. 0x32, 0xFC, 0x31, 0x73, 0x54, 0xE2, 0xC2, 0x74,
  30337. 0xD4, 0xDA, 0xF8, 0xAD, 0x00, 0x10, 0x54, 0xC7,
  30338. 0x6C, 0xE5, 0x79, 0x71, 0xC6, 0xF4, 0x48, 0x6D,
  30339. 0x57, 0x23, 0x04, 0x32, 0x61, 0xC5, 0x06, 0xEB,
  30340. 0xF5, 0xBE, 0x43, 0x8F, 0x53, 0xDE, 0x04, 0xF0,
  30341. 0x67, 0xC7, 0x76, 0xE0, 0xDD, 0x3B, 0x71, 0xA6,
  30342. 0x29, 0x01, 0x33, 0x28, 0x37, 0x25, 0xA5, 0x32,
  30343. 0xF2, 0x1A, 0xF1, 0x45, 0x12, 0x6D, 0xC1, 0xD7,
  30344. 0x77, 0xEC, 0xC2, 0x7B, 0xE5, 0x08, 0x35, 0xBD,
  30345. 0x28, 0x09, 0x8B, 0x8A, 0x73, 0xD9, 0xF8, 0x01,
  30346. 0xD8, 0x93, 0x79, 0x3A, 0x41, 0xFF, 0x5C, 0x49,
  30347. 0xB8, 0x7E, 0x79, 0xF2, 0xBE, 0x4D, 0x56, 0xCE,
  30348. 0x55, 0x7E, 0x13, 0x4A, 0xD8, 0x5B, 0xB1, 0xD4,
  30349. 0xB9, 0xCE, 0x4F, 0x8B, 0xE4, 0xB0, 0x8A, 0x12,
  30350. 0xBA, 0xBF, 0x55, 0xB1, 0xD6, 0xF1, 0xD7, 0xA6,
  30351. 0x38, 0x01, 0x9E, 0xA2, 0x8E, 0x15, 0xAB, 0x1C,
  30352. 0x9F, 0x76, 0x37, 0x5F, 0xDD, 0x12, 0x10, 0xD4,
  30353. 0xF4, 0x35, 0x1B, 0x9A, 0x00, 0x94, 0x86, 0xB7,
  30354. 0xF3, 0xED, 0x46, 0xC9, 0x65, 0xDE, 0xD2, 0xD8,
  30355. 0x0D, 0xAD, 0xE4, 0xF3, 0x8C, 0x67, 0x21, 0xD5,
  30356. 0x2C, 0x3A, 0xD1, 0x03, 0xA1, 0x0E, 0xBD, 0x29,
  30357. 0x59, 0x24, 0x8B, 0x4E, 0xF0, 0x06, 0x83, 0x6B,
  30358. 0xF0, 0x97, 0x44, 0x8E, 0x61, 0x07, 0xC9, 0xED,
  30359. 0xEE, 0x9F, 0xB7, 0x04, 0x82, 0x3D, 0xF1, 0x99,
  30360. 0xF8, 0x32, 0xC9, 0x05, 0xAE, 0x45, 0xF8, 0xA2,
  30361. 0x47, 0xA0, 0x72, 0xD8, 0xEF, 0x72, 0x9E, 0xAB,
  30362. 0xC5, 0xE2, 0x75, 0x74, 0xB0, 0x77, 0x39, 0xB3,
  30363. 0x4B, 0xE7, 0x4A, 0x53, 0x2F, 0x74, 0x7B, 0x86
  30364. };
  30365. WOLFSSL_SMALL_STACK_STATIC const byte encSsv[] = {
  30366. 0x89, 0xE0, 0xBC, 0x66, 0x1A, 0xA1, 0xE9, 0x16,
  30367. 0x38, 0xE6, 0xAC, 0xC8, 0x4E, 0x49, 0x65, 0x07
  30368. };
  30369. int ret;
  30370. int valid;
  30371. byte pubKey[sizeof(pubData) + 1];
  30372. word32 sz = sizeof(pubKey);
  30373. byte tmpSsv[sizeof(encSsv)];
  30374. byte* iTable = NULL;
  30375. word32 iTableLen;
  30376. byte* table = NULL;
  30377. word32 len;
  30378. ret = wc_ImportSakkePublicKey(key, pubData, sizeof(pubData), 0);
  30379. if (ret != 0)
  30380. return -10315;
  30381. ret = wc_DecodeSakkeRsk(key, rskData, sizeof(rskData), rsk);
  30382. if (ret != 0)
  30383. return -10316;
  30384. ret = wc_ValidateSakkeRsk(key, id, sizeof(id), rsk, &valid);
  30385. if (ret != 0)
  30386. return -10317;
  30387. if (valid != 1)
  30388. return -10318;
  30389. ret = wc_SetSakkeRsk(key, rsk, NULL, 0);
  30390. if (ret != 0)
  30391. return -10319;
  30392. ret = wc_SetSakkeIdentity(key, id, sizeof(id));
  30393. if (ret != 0)
  30394. return -10319;
  30395. XMEMCPY(tmpSsv, encSsv, sizeof(encSsv));
  30396. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, tmpSsv, sizeof(tmpSsv),
  30397. auth, sizeof(auth));
  30398. if (ret != 0)
  30399. return -10322;
  30400. if (XMEMCMP(tmpSsv, ssv, sizeof(ssv)) != 0)
  30401. return -10320;
  30402. ret = wc_MakeSakkePointI(key, id, sizeof(id));
  30403. if (ret != 0)
  30404. return -10321;
  30405. iTableLen = 0;
  30406. ret = wc_GenerateSakkePointITable(key, NULL, &iTableLen);
  30407. if (ret != LENGTH_ONLY_E)
  30408. return -10322;
  30409. if (iTableLen != 0) {
  30410. iTable = (byte*)XMALLOC(iTableLen, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30411. if (iTable == NULL)
  30412. return -10323;
  30413. ret = wc_GenerateSakkePointITable(key, iTable, &iTableLen);
  30414. if (ret != 0)
  30415. return -10324;
  30416. }
  30417. len = 0;
  30418. ret = wc_GenerateSakkeRskTable(key, rsk, NULL, &len);
  30419. if (ret != LENGTH_ONLY_E)
  30420. return -10325;
  30421. if (len > 0) {
  30422. table = (byte*)XMALLOC(len, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30423. if (table == NULL)
  30424. return -10326;
  30425. ret = wc_GenerateSakkeRskTable(key, rsk, table, &len);
  30426. if (ret != 0)
  30427. return -10327;
  30428. }
  30429. ret = wc_SetSakkeRsk(key, rsk, table, len);
  30430. if (ret != 0)
  30431. return -10319;
  30432. XMEMCPY(tmpSsv, encSsv, sizeof(encSsv));
  30433. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, tmpSsv, sizeof(tmpSsv),
  30434. auth, sizeof(auth));
  30435. if (ret != 0)
  30436. return -10328;
  30437. if (XMEMCMP(tmpSsv, ssv, sizeof(ssv)) != 0)
  30438. return -10329;
  30439. /* Don't reference table that is about to be freed. */
  30440. ret = wc_ClearSakkePointITable(key);
  30441. if (ret != 0)
  30442. return -10330;
  30443. /* Dispose of tables */
  30444. if (iTable != NULL)
  30445. XFREE(iTable, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30446. if (table != NULL)
  30447. XFREE(table, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30448. /* Make sure the key public key is exportable - convert to Montgomery form
  30449. * in Validation.
  30450. */
  30451. ret = wc_ExportSakkePublicKey(key, pubKey, &sz, 1);
  30452. if (ret != 0)
  30453. return -10331;
  30454. if (sz != sizeof(pubData))
  30455. return -10332;
  30456. if (XMEMCMP(pubKey, pubData, sizeof(pubData)) != 0)
  30457. return -10333;
  30458. sz = sizeof(pubData) + 1;
  30459. ret = wc_ExportSakkePublicKey(key, pubKey, &sz, 0);
  30460. if (ret != 0)
  30461. return -10334;
  30462. if (sz != sizeof(pubData) + 1)
  30463. return -10335;
  30464. if (pubKey[0] != 0x04)
  30465. return -10336;
  30466. if (XMEMCMP(pubKey + 1, pubData, sizeof(pubData)) != 0)
  30467. return -10337;
  30468. return 0;
  30469. }
  30470. static int sakke_kat_encapsulate_test(SakkeKey* key)
  30471. {
  30472. static const byte pubData[] = {
  30473. 0x59, 0x58, 0xEF, 0x1B, 0x16, 0x79, 0xBF, 0x09,
  30474. 0x9B, 0x3A, 0x03, 0x0D, 0xF2, 0x55, 0xAA, 0x6A,
  30475. 0x23, 0xC1, 0xD8, 0xF1, 0x43, 0xD4, 0xD2, 0x3F,
  30476. 0x75, 0x3E, 0x69, 0xBD, 0x27, 0xA8, 0x32, 0xF3,
  30477. 0x8C, 0xB4, 0xAD, 0x53, 0xDD, 0xEF, 0x42, 0x60,
  30478. 0xB0, 0xFE, 0x8B, 0xB4, 0x5C, 0x4C, 0x1F, 0xF5,
  30479. 0x10, 0xEF, 0xFE, 0x30, 0x03, 0x67, 0xA3, 0x7B,
  30480. 0x61, 0xF7, 0x01, 0xD9, 0x14, 0xAE, 0xF0, 0x97,
  30481. 0x24, 0x82, 0x5F, 0xA0, 0x70, 0x7D, 0x61, 0xA6,
  30482. 0xDF, 0xF4, 0xFB, 0xD7, 0x27, 0x35, 0x66, 0xCD,
  30483. 0xDE, 0x35, 0x2A, 0x0B, 0x04, 0xB7, 0xC1, 0x6A,
  30484. 0x78, 0x30, 0x9B, 0xE6, 0x40, 0x69, 0x7D, 0xE7,
  30485. 0x47, 0x61, 0x3A, 0x5F, 0xC1, 0x95, 0xE8, 0xB9,
  30486. 0xF3, 0x28, 0x85, 0x2A, 0x57, 0x9D, 0xB8, 0xF9,
  30487. 0x9B, 0x1D, 0x00, 0x34, 0x47, 0x9E, 0xA9, 0xC5,
  30488. 0x59, 0x5F, 0x47, 0xC4, 0xB2, 0xF5, 0x4F, 0xF2,
  30489. 0x15, 0x08, 0xD3, 0x75, 0x14, 0xDC, 0xF7, 0xA8,
  30490. 0xE1, 0x43, 0xA6, 0x05, 0x8C, 0x09, 0xA6, 0xBF,
  30491. 0x2C, 0x98, 0x58, 0xCA, 0x37, 0xC2, 0x58, 0x06,
  30492. 0x5A, 0xE6, 0xBF, 0x75, 0x32, 0xBC, 0x8B, 0x5B,
  30493. 0x63, 0x38, 0x38, 0x66, 0xE0, 0x75, 0x3C, 0x5A,
  30494. 0xC0, 0xE7, 0x27, 0x09, 0xF8, 0x44, 0x5F, 0x2E,
  30495. 0x61, 0x78, 0xE0, 0x65, 0x85, 0x7E, 0x0E, 0xDA,
  30496. 0x10, 0xF6, 0x82, 0x06, 0xB6, 0x35, 0x05, 0xED,
  30497. 0x87, 0xE5, 0x34, 0xFB, 0x28, 0x31, 0xFF, 0x95,
  30498. 0x7F, 0xB7, 0xDC, 0x61, 0x9D, 0xAE, 0x61, 0x30,
  30499. 0x1E, 0xEA, 0xCC, 0x2F, 0xDA, 0x36, 0x80, 0xEA,
  30500. 0x49, 0x99, 0x25, 0x8A, 0x83, 0x3C, 0xEA, 0x8F,
  30501. 0xC6, 0x7C, 0x6D, 0x19, 0x48, 0x7F, 0xB4, 0x49,
  30502. 0x05, 0x9F, 0x26, 0xCC, 0x8A, 0xAB, 0x65, 0x5A,
  30503. 0xB5, 0x8B, 0x7C, 0xC7, 0x96, 0xE2, 0x4E, 0x9A,
  30504. 0x39, 0x40, 0x95, 0x75, 0x4F, 0x5F, 0x8B, 0xAE
  30505. };
  30506. static const byte id[] = {
  30507. 0x32, 0x30, 0x31, 0x31, 0x2D, 0x30, 0x32, 0x00,
  30508. 0x74, 0x65, 0x6C, 0x3A, 0x2B, 0x34, 0x34, 0x37,
  30509. 0x37, 0x30, 0x30, 0x39, 0x30, 0x30, 0x31, 0x32,
  30510. 0x33, 0x00
  30511. };
  30512. static word32 idSz = sizeof(id);
  30513. byte ssv[] = {
  30514. 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0,
  30515. 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0
  30516. };
  30517. static word16 ssvSz = sizeof(ssv);
  30518. static const byte expAuth[] = {
  30519. 0x04,
  30520. 0x44, 0xE8, 0xAD, 0x44, 0xAB, 0x85, 0x92, 0xA6,
  30521. 0xA5, 0xA3, 0xDD, 0xCA, 0x5C, 0xF8, 0x96, 0xC7,
  30522. 0x18, 0x04, 0x36, 0x06, 0xA0, 0x1D, 0x65, 0x0D,
  30523. 0xEF, 0x37, 0xA0, 0x1F, 0x37, 0xC2, 0x28, 0xC3,
  30524. 0x32, 0xFC, 0x31, 0x73, 0x54, 0xE2, 0xC2, 0x74,
  30525. 0xD4, 0xDA, 0xF8, 0xAD, 0x00, 0x10, 0x54, 0xC7,
  30526. 0x6C, 0xE5, 0x79, 0x71, 0xC6, 0xF4, 0x48, 0x6D,
  30527. 0x57, 0x23, 0x04, 0x32, 0x61, 0xC5, 0x06, 0xEB,
  30528. 0xF5, 0xBE, 0x43, 0x8F, 0x53, 0xDE, 0x04, 0xF0,
  30529. 0x67, 0xC7, 0x76, 0xE0, 0xDD, 0x3B, 0x71, 0xA6,
  30530. 0x29, 0x01, 0x33, 0x28, 0x37, 0x25, 0xA5, 0x32,
  30531. 0xF2, 0x1A, 0xF1, 0x45, 0x12, 0x6D, 0xC1, 0xD7,
  30532. 0x77, 0xEC, 0xC2, 0x7B, 0xE5, 0x08, 0x35, 0xBD,
  30533. 0x28, 0x09, 0x8B, 0x8A, 0x73, 0xD9, 0xF8, 0x01,
  30534. 0xD8, 0x93, 0x79, 0x3A, 0x41, 0xFF, 0x5C, 0x49,
  30535. 0xB8, 0x7E, 0x79, 0xF2, 0xBE, 0x4D, 0x56, 0xCE,
  30536. 0x55, 0x7E, 0x13, 0x4A, 0xD8, 0x5B, 0xB1, 0xD4,
  30537. 0xB9, 0xCE, 0x4F, 0x8B, 0xE4, 0xB0, 0x8A, 0x12,
  30538. 0xBA, 0xBF, 0x55, 0xB1, 0xD6, 0xF1, 0xD7, 0xA6,
  30539. 0x38, 0x01, 0x9E, 0xA2, 0x8E, 0x15, 0xAB, 0x1C,
  30540. 0x9F, 0x76, 0x37, 0x5F, 0xDD, 0x12, 0x10, 0xD4,
  30541. 0xF4, 0x35, 0x1B, 0x9A, 0x00, 0x94, 0x86, 0xB7,
  30542. 0xF3, 0xED, 0x46, 0xC9, 0x65, 0xDE, 0xD2, 0xD8,
  30543. 0x0D, 0xAD, 0xE4, 0xF3, 0x8C, 0x67, 0x21, 0xD5,
  30544. 0x2C, 0x3A, 0xD1, 0x03, 0xA1, 0x0E, 0xBD, 0x29,
  30545. 0x59, 0x24, 0x8B, 0x4E, 0xF0, 0x06, 0x83, 0x6B,
  30546. 0xF0, 0x97, 0x44, 0x8E, 0x61, 0x07, 0xC9, 0xED,
  30547. 0xEE, 0x9F, 0xB7, 0x04, 0x82, 0x3D, 0xF1, 0x99,
  30548. 0xF8, 0x32, 0xC9, 0x05, 0xAE, 0x45, 0xF8, 0xA2,
  30549. 0x47, 0xA0, 0x72, 0xD8, 0xEF, 0x72, 0x9E, 0xAB,
  30550. 0xC5, 0xE2, 0x75, 0x74, 0xB0, 0x77, 0x39, 0xB3,
  30551. 0x4B, 0xE7, 0x4A, 0x53, 0x2F, 0x74, 0x7B, 0x86
  30552. };
  30553. static const byte encSsv[] = {
  30554. 0x89, 0xE0, 0xBC, 0x66, 0x1A, 0xA1, 0xE9, 0x16,
  30555. 0x38, 0xE6, 0xAC, 0xC8, 0x4E, 0x49, 0x65, 0x07
  30556. };
  30557. int ret;
  30558. byte auth[257];
  30559. word16 authSz = sizeof(auth);
  30560. ret = wc_ImportSakkePublicKey(key, pubData, sizeof(pubData), 0);
  30561. if (ret != 0)
  30562. return -10334;
  30563. ret = wc_SetSakkeIdentity(key, id, idSz);
  30564. if (ret != 0)
  30565. return -10335;
  30566. ret = wc_MakeSakkeEncapsulatedSSV(key, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  30567. auth, &authSz);
  30568. if (ret != 0)
  30569. return -10336;
  30570. if (authSz != 257)
  30571. return -10337;
  30572. if (XMEMCMP(ssv, encSsv, ssvSz) != 0)
  30573. return -10338;
  30574. if (XMEMCMP(auth, expAuth, authSz) != 0)
  30575. return -10339;
  30576. return 0;
  30577. }
  30578. static int sakke_make_key_test(SakkeKey* priv, SakkeKey* pub, SakkeKey* key,
  30579. WC_RNG* rng, ecc_point* rsk)
  30580. {
  30581. int ret;
  30582. byte data[440];
  30583. byte pubData[257];
  30584. word32 sz;
  30585. char mail[] = "test@wolfssl.com";
  30586. byte* id = (byte*)mail;
  30587. word32 idSz = (word32)XSTRLEN(mail);
  30588. int valid;
  30589. ecc_point* pubKey = rsk;
  30590. ret = wc_InitSakkeKey_ex(key, 128, ECC_SAKKE_1, NULL, INVALID_DEVID);
  30591. if (ret != 0)
  30592. return -10339;
  30593. ret = wc_MakeSakkeKey(priv, rng);
  30594. if (ret != 0)
  30595. return -10340;
  30596. ret = wc_ExportSakkeKey(priv, NULL, &sz);
  30597. if (ret != LENGTH_ONLY_E)
  30598. return -10341;
  30599. if (sz != 384)
  30600. return -10342;
  30601. sz--;
  30602. ret = wc_ExportSakkeKey(priv, data, &sz);
  30603. if (ret == 0)
  30604. return -10343;
  30605. sz++;
  30606. ret = wc_ExportSakkeKey(priv, data, &sz);
  30607. if (ret != 0)
  30608. return -10344;
  30609. if (sz != 384)
  30610. return -10345;
  30611. ret = wc_ImportSakkeKey(key, data, sz - 1);
  30612. if (ret == 0)
  30613. return -10346;
  30614. ret = wc_ImportSakkeKey(key, data, sz);
  30615. if (ret != 0)
  30616. return -10347;
  30617. wc_FreeSakkeKey(key);
  30618. ret = wc_InitSakkeKey_ex(key, 128, ECC_SAKKE_1, NULL, INVALID_DEVID);
  30619. if (ret != 0)
  30620. return -10348;
  30621. ret = wc_ExportSakkePrivateKey(priv, NULL, &sz);
  30622. if (ret != LENGTH_ONLY_E)
  30623. return -10349;
  30624. if (sz != 128)
  30625. return -10350;
  30626. sz--;
  30627. ret = wc_ExportSakkePrivateKey(priv, data, &sz);
  30628. if (ret == 0)
  30629. return -10351;
  30630. sz++;
  30631. ret = wc_ExportSakkePrivateKey(priv, data, &sz);
  30632. if (ret != 0)
  30633. return -10352;
  30634. if (sz != 128)
  30635. return -10353;
  30636. ret = wc_ImportSakkePrivateKey(key, data, sz - 1);
  30637. if (ret == 0)
  30638. return -10354;
  30639. ret = wc_ImportSakkePrivateKey(key, data, sz);
  30640. if (ret != 0)
  30641. return -10355;
  30642. ret = wc_MakeSakkePublicKey(key, pubKey);
  30643. if (ret != 0)
  30644. return -10356;
  30645. ret = wc_ExportSakkePublicKey(priv, NULL, &sz, 1);
  30646. if (ret != LENGTH_ONLY_E)
  30647. return -10357;
  30648. if (sz != 256)
  30649. return -10358;
  30650. sz--;
  30651. ret = wc_ExportSakkePublicKey(priv, data, &sz, 1);
  30652. if (ret == 0)
  30653. return -10359;
  30654. sz++;
  30655. ret = wc_ExportSakkePublicKey(priv, data, &sz, 1);
  30656. if (ret != 0)
  30657. return -10360;
  30658. if (sz != 256)
  30659. return -10361;
  30660. ret = wc_ImportSakkePublicKey(pub, data, sz - 1, 1);
  30661. if (ret == 0)
  30662. return -10362;
  30663. ret = wc_ImportSakkePublicKey(pub, data, sz, 1);
  30664. if (ret != 0)
  30665. return -10363;
  30666. ret = wc_ExportSakkePublicKey(pub, pubData, &sz, 1);
  30667. if (ret != 0)
  30668. return -10364;
  30669. if (sz != 256)
  30670. return -10365;
  30671. if (XMEMCMP(data, pubData, sz) != 0)
  30672. return -10366;
  30673. ret = wc_MakeSakkeRsk(priv, id, idSz, rsk);
  30674. if (ret != 0)
  30675. return -10367;
  30676. ret = wc_ValidateSakkeRsk(priv, id, idSz, rsk, &valid);
  30677. if (ret != 0)
  30678. return -10368;
  30679. if (valid != 1)
  30680. return -10369;
  30681. ret = wc_ValidateSakkeRsk(pub, id, idSz, rsk, &valid);
  30682. if (ret != 0)
  30683. return -10370;
  30684. if (valid != 1)
  30685. return -10371;
  30686. sz = sizeof(data);
  30687. ret = wc_EncodeSakkeRsk(priv, rsk, data, &sz, 1);
  30688. if (ret != 0)
  30689. return -10372;
  30690. if (sz != 256)
  30691. return -10373;
  30692. ret = wc_DecodeSakkeRsk(priv, data, sz, rsk);
  30693. if (ret != 0)
  30694. return -10374;
  30695. sz = sizeof(pubData);
  30696. ret = wc_EncodeSakkeRsk(priv, rsk, pubData, &sz, 0);
  30697. if (ret != 0)
  30698. return -10375;
  30699. if (sz != sizeof(pubData))
  30700. return -10376;
  30701. ret = wc_DecodeSakkeRsk(priv, pubData, sz, rsk);
  30702. if (ret != 0)
  30703. return -10377;
  30704. wc_FreeSakkeKey(key);
  30705. return 0;
  30706. }
  30707. static int sakke_op_test(SakkeKey* priv, SakkeKey* pub, WC_RNG* rng,
  30708. ecc_point* rsk)
  30709. {
  30710. int ret;
  30711. byte ssv[16];
  30712. word16 ssvSz;
  30713. byte auth[257];
  30714. word16 authSz;
  30715. char mail[] = "test@wolfssl.com";
  30716. byte* id = (byte*)mail;
  30717. word32 idSz = (word32)XSTRLEN(mail);
  30718. byte pointI[256];
  30719. word32 sz;
  30720. ret = wc_GenerateSakkeSSV(pub, rng, NULL, &ssvSz);
  30721. if (ret != LENGTH_ONLY_E)
  30722. return -10375;
  30723. if (ssvSz != 16)
  30724. return -10376;
  30725. ssvSz += 128;
  30726. ret = wc_GenerateSakkeSSV(pub, rng, ssv, &ssvSz);
  30727. if (ret == 0)
  30728. return -10377;
  30729. ssvSz -= 128;
  30730. ret = wc_GenerateSakkeSSV(pub, rng, ssv, &ssvSz);
  30731. if (ret != 0)
  30732. return -10378;
  30733. if (ssvSz != 16)
  30734. return -10379;
  30735. ret = wc_GetSakkeAuthSize(pub, &authSz);
  30736. if (ret != 0)
  30737. return -10380;
  30738. ret = wc_SetSakkeIdentity(pub, id, idSz);
  30739. if (ret != 0)
  30740. return -10380;
  30741. ret = wc_MakeSakkeEncapsulatedSSV(pub, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  30742. NULL, &authSz);
  30743. if (ret != LENGTH_ONLY_E)
  30744. return -10381;
  30745. if (authSz != 257)
  30746. return -10382;
  30747. authSz--;
  30748. ret = wc_MakeSakkeEncapsulatedSSV(pub, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  30749. auth, &authSz);
  30750. if (ret == 0)
  30751. return -10383;
  30752. authSz++;
  30753. ret = wc_MakeSakkeEncapsulatedSSV(pub, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  30754. auth, &authSz);
  30755. if (ret != 0)
  30756. return -10384;
  30757. if (authSz != 257)
  30758. return -10385;
  30759. ret = wc_GetSakkePointI(pub, NULL, &sz);
  30760. if (ret != LENGTH_ONLY_E)
  30761. return -10386;
  30762. if (sz != 256)
  30763. return -10387;
  30764. ret = wc_GetSakkePointI(pub, pointI, &sz);
  30765. if (ret != 0)
  30766. return -10388;
  30767. if (sz != 256)
  30768. return -10389;
  30769. /* Bogus identity - make it check and regenerate I. */
  30770. ret = wc_MakeSakkePointI(pub, ssv, ssvSz);
  30771. if (ret != 0)
  30772. return -10391;
  30773. ret = wc_MakeSakkeEncapsulatedSSV(pub, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  30774. auth, &authSz);
  30775. if (ret != 0)
  30776. return -10392;
  30777. if (authSz != 257)
  30778. return -10393;
  30779. ret = wc_SetSakkeRsk(priv, rsk, NULL, 0);
  30780. if (ret != 0)
  30781. return -10392;
  30782. ret = wc_SetSakkeIdentity(priv, id, idSz);
  30783. if (ret != 0)
  30784. return -10392;
  30785. authSz--;
  30786. ret = wc_DeriveSakkeSSV(priv, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  30787. authSz);
  30788. if (ret == 0)
  30789. return -10394;
  30790. authSz++;
  30791. ret = wc_DeriveSakkeSSV(priv, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  30792. authSz);
  30793. if (ret != 0)
  30794. return -10395;
  30795. ssv[0] ^= 0x80;
  30796. ret = wc_DeriveSakkeSSV(priv, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  30797. authSz);
  30798. if (ret != SAKKE_VERIFY_FAIL_E)
  30799. return -10396;
  30800. ssv[0] ^= 0x80;
  30801. /* Bogus identity - make it check and regenerate I. */
  30802. ret = wc_MakeSakkePointI(pub, ssv, idSz);
  30803. if (ret != 0)
  30804. return -10397;
  30805. ret = wc_DeriveSakkeSSV(priv, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  30806. authSz);
  30807. if (ret != 0)
  30808. return -10398;
  30809. return 0;
  30810. }
  30811. int sakke_test(void)
  30812. {
  30813. int ret = 0;
  30814. WC_RNG rng;
  30815. int rng_inited = 0;
  30816. SakkeKey* priv = NULL;
  30817. SakkeKey* pub = NULL;
  30818. SakkeKey* key = NULL;
  30819. ecc_point* rsk = NULL;
  30820. priv = (SakkeKey*)XMALLOC(sizeof(SakkeKey), HEAP_HINT,
  30821. DYNAMIC_TYPE_TMP_BUFFER);
  30822. if (priv == NULL)
  30823. ret = -10404;
  30824. else
  30825. XMEMSET(priv, 0, sizeof(*priv));
  30826. if (ret == 0) {
  30827. pub = (SakkeKey*)XMALLOC(sizeof(SakkeKey), HEAP_HINT,
  30828. DYNAMIC_TYPE_TMP_BUFFER);
  30829. if (pub == NULL)
  30830. ret = -10405;
  30831. else
  30832. XMEMSET(pub, 0, sizeof(*pub));
  30833. }
  30834. if (ret == 0) {
  30835. key = (SakkeKey*)XMALLOC(sizeof(SakkeKey), HEAP_HINT,
  30836. DYNAMIC_TYPE_TMP_BUFFER);
  30837. if (key == NULL)
  30838. ret = -10406;
  30839. else
  30840. XMEMSET(key, 0, sizeof(*key));
  30841. }
  30842. if (ret == 0) {
  30843. #ifndef HAVE_FIPS
  30844. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  30845. #else
  30846. ret = wc_InitRng(&rng);
  30847. #endif
  30848. if (ret == 0)
  30849. rng_inited = 1;
  30850. else
  30851. ret = -10400;
  30852. }
  30853. if (ret == 0) {
  30854. rsk = wc_ecc_new_point();
  30855. if (rsk == NULL)
  30856. ret = -10401;
  30857. }
  30858. if (ret == 0) {
  30859. ret = wc_InitSakkeKey(pub, HEAP_HINT, INVALID_DEVID);
  30860. if (ret != 0)
  30861. ret = -10402;
  30862. }
  30863. if (ret == 0) {
  30864. ret = wc_InitSakkeKey(priv, HEAP_HINT, INVALID_DEVID);
  30865. if (ret != 0)
  30866. ret = -10403;
  30867. }
  30868. if (ret == 0) {
  30869. ret = sakke_api_test(&rng, key, rsk);
  30870. }
  30871. if (ret == 0) {
  30872. ret = sakke_kat_derive_test(pub, rsk);
  30873. }
  30874. if (ret == 0) {
  30875. ret = sakke_kat_encapsulate_test(pub);
  30876. }
  30877. if (ret == 0) {
  30878. ret = sakke_make_key_test(priv, pub, key, &rng, rsk);
  30879. }
  30880. if (ret == 0) {
  30881. ret = sakke_op_test(priv, pub, &rng, rsk);
  30882. }
  30883. if (rsk != NULL) {
  30884. wc_ecc_forcezero_point(rsk);
  30885. wc_ecc_del_point(rsk);
  30886. }
  30887. if (rng_inited)
  30888. wc_FreeRng(&rng);
  30889. if (key != NULL)
  30890. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30891. if (pub != NULL) {
  30892. wc_FreeSakkeKey(pub);
  30893. XFREE(pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30894. }
  30895. if (priv != NULL) {
  30896. wc_FreeSakkeKey(priv);
  30897. XFREE(priv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30898. }
  30899. return ret;
  30900. }
  30901. #endif /* WOLFCRYPT_HAVE_SAKKE */
  30902. #if defined(WOLFSSL_CMAC) && !defined(NO_AES)
  30903. typedef struct CMAC_Test_Case {
  30904. int type;
  30905. int partial;
  30906. const byte* m;
  30907. word32 mSz;
  30908. const byte* k;
  30909. word32 kSz;
  30910. const byte* t;
  30911. word32 tSz;
  30912. } CMAC_Test_Case;
  30913. WOLFSSL_TEST_SUBROUTINE int cmac_test(void)
  30914. {
  30915. #ifdef WOLFSSL_AES_128
  30916. WOLFSSL_SMALL_STACK_STATIC const byte k128[] =
  30917. {
  30918. 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
  30919. 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c
  30920. };
  30921. #define KLEN_128 (sizeof(k128))
  30922. #endif
  30923. #ifdef WOLFSSL_AES_192
  30924. WOLFSSL_SMALL_STACK_STATIC const byte k192[] =
  30925. {
  30926. 0x8e, 0x73, 0xb0, 0xf7, 0xda, 0x0e, 0x64, 0x52,
  30927. 0xc8, 0x10, 0xf3, 0x2b, 0x80, 0x90, 0x79, 0xe5,
  30928. 0x62, 0xf8, 0xea, 0xd2, 0x52, 0x2c, 0x6b, 0x7b
  30929. };
  30930. #define KLEN_192 (sizeof(k192))
  30931. #endif
  30932. #ifdef WOLFSSL_AES_256
  30933. WOLFSSL_SMALL_STACK_STATIC const byte k256[] =
  30934. {
  30935. 0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe,
  30936. 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
  30937. 0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7,
  30938. 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4
  30939. };
  30940. #define KLEN_256 (sizeof(k256))
  30941. #endif
  30942. WOLFSSL_SMALL_STACK_STATIC const byte m[] =
  30943. {
  30944. 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
  30945. 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
  30946. 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c,
  30947. 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51,
  30948. 0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11,
  30949. 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef,
  30950. 0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17,
  30951. 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10
  30952. };
  30953. #define MLEN_0 (0)
  30954. #define MLEN_128 (128/8)
  30955. #define MLEN_320 (320/8)
  30956. #define MLEN_319 (MLEN_320 - 1)
  30957. #define MLEN_512 (512/8)
  30958. #ifdef WOLFSSL_AES_128
  30959. WOLFSSL_SMALL_STACK_STATIC const byte t128_0[] =
  30960. {
  30961. 0xbb, 0x1d, 0x69, 0x29, 0xe9, 0x59, 0x37, 0x28,
  30962. 0x7f, 0xa3, 0x7d, 0x12, 0x9b, 0x75, 0x67, 0x46
  30963. };
  30964. WOLFSSL_SMALL_STACK_STATIC const byte t128_128[] =
  30965. {
  30966. 0x07, 0x0a, 0x16, 0xb4, 0x6b, 0x4d, 0x41, 0x44,
  30967. 0xf7, 0x9b, 0xdd, 0x9d, 0xd0, 0x4a, 0x28, 0x7c
  30968. };
  30969. WOLFSSL_SMALL_STACK_STATIC const byte t128_319[] =
  30970. {
  30971. 0x2c, 0x17, 0x84, 0x4c, 0x93, 0x1c, 0x07, 0x95,
  30972. 0x15, 0x92, 0x73, 0x0a, 0x34, 0xd0, 0xd9, 0xd2
  30973. };
  30974. WOLFSSL_SMALL_STACK_STATIC const byte t128_320[] =
  30975. {
  30976. 0xdf, 0xa6, 0x67, 0x47, 0xde, 0x9a, 0xe6, 0x30,
  30977. 0x30, 0xca, 0x32, 0x61, 0x14, 0x97, 0xc8, 0x27
  30978. };
  30979. WOLFSSL_SMALL_STACK_STATIC const byte t128_512[] =
  30980. {
  30981. 0x51, 0xf0, 0xbe, 0xbf, 0x7e, 0x3b, 0x9d, 0x92,
  30982. 0xfc, 0x49, 0x74, 0x17, 0x79, 0x36, 0x3c, 0xfe
  30983. };
  30984. #endif
  30985. #ifdef WOLFSSL_AES_192
  30986. WOLFSSL_SMALL_STACK_STATIC const byte t192_0[] =
  30987. {
  30988. 0xd1, 0x7d, 0xdf, 0x46, 0xad, 0xaa, 0xcd, 0xe5,
  30989. 0x31, 0xca, 0xc4, 0x83, 0xde, 0x7a, 0x93, 0x67
  30990. };
  30991. WOLFSSL_SMALL_STACK_STATIC const byte t192_128[] =
  30992. {
  30993. 0x9e, 0x99, 0xa7, 0xbf, 0x31, 0xe7, 0x10, 0x90,
  30994. 0x06, 0x62, 0xf6, 0x5e, 0x61, 0x7c, 0x51, 0x84
  30995. };
  30996. WOLFSSL_SMALL_STACK_STATIC const byte t192_320[] =
  30997. {
  30998. 0x8a, 0x1d, 0xe5, 0xbe, 0x2e, 0xb3, 0x1a, 0xad,
  30999. 0x08, 0x9a, 0x82, 0xe6, 0xee, 0x90, 0x8b, 0x0e
  31000. };
  31001. WOLFSSL_SMALL_STACK_STATIC const byte t192_512[] =
  31002. {
  31003. 0xa1, 0xd5, 0xdf, 0x0e, 0xed, 0x79, 0x0f, 0x79,
  31004. 0x4d, 0x77, 0x58, 0x96, 0x59, 0xf3, 0x9a, 0x11
  31005. };
  31006. #endif
  31007. #ifdef WOLFSSL_AES_256
  31008. WOLFSSL_SMALL_STACK_STATIC const byte t256_0[] =
  31009. {
  31010. 0x02, 0x89, 0x62, 0xf6, 0x1b, 0x7b, 0xf8, 0x9e,
  31011. 0xfc, 0x6b, 0x55, 0x1f, 0x46, 0x67, 0xd9, 0x83
  31012. };
  31013. WOLFSSL_SMALL_STACK_STATIC const byte t256_128[] =
  31014. {
  31015. 0x28, 0xa7, 0x02, 0x3f, 0x45, 0x2e, 0x8f, 0x82,
  31016. 0xbd, 0x4b, 0xf2, 0x8d, 0x8c, 0x37, 0xc3, 0x5c
  31017. };
  31018. WOLFSSL_SMALL_STACK_STATIC const byte t256_320[] =
  31019. {
  31020. 0xaa, 0xf3, 0xd8, 0xf1, 0xde, 0x56, 0x40, 0xc2,
  31021. 0x32, 0xf5, 0xb1, 0x69, 0xb9, 0xc9, 0x11, 0xe6
  31022. };
  31023. WOLFSSL_SMALL_STACK_STATIC const byte t256_512[] =
  31024. {
  31025. 0xe1, 0x99, 0x21, 0x90, 0x54, 0x9f, 0x6e, 0xd5,
  31026. 0x69, 0x6a, 0x2c, 0x05, 0x6c, 0x31, 0x54, 0x10
  31027. };
  31028. #endif
  31029. const CMAC_Test_Case testCases[] =
  31030. {
  31031. #ifdef WOLFSSL_AES_128
  31032. {WC_CMAC_AES, 0, m, MLEN_0, k128, KLEN_128, t128_0, AES_BLOCK_SIZE},
  31033. {WC_CMAC_AES, 0, m, MLEN_128, k128, KLEN_128, t128_128, AES_BLOCK_SIZE},
  31034. {WC_CMAC_AES, 0, m, MLEN_320, k128, KLEN_128, t128_320, AES_BLOCK_SIZE},
  31035. {WC_CMAC_AES, 0, m, MLEN_512, k128, KLEN_128, t128_512, AES_BLOCK_SIZE},
  31036. {WC_CMAC_AES, 5, m, MLEN_512, k128, KLEN_128, t128_512, AES_BLOCK_SIZE},
  31037. #endif
  31038. #ifdef WOLFSSL_AES_192
  31039. {WC_CMAC_AES, 0, m, MLEN_0, k192, KLEN_192, t192_0, AES_BLOCK_SIZE},
  31040. {WC_CMAC_AES, 0, m, MLEN_128, k192, KLEN_192, t192_128, AES_BLOCK_SIZE},
  31041. {WC_CMAC_AES, 0, m, MLEN_320, k192, KLEN_192, t192_320, AES_BLOCK_SIZE},
  31042. {WC_CMAC_AES, 0, m, MLEN_512, k192, KLEN_192, t192_512, AES_BLOCK_SIZE},
  31043. #endif
  31044. #ifdef WOLFSSL_AES_256
  31045. {WC_CMAC_AES, 0, m, MLEN_0, k256, KLEN_256, t256_0, AES_BLOCK_SIZE},
  31046. {WC_CMAC_AES, 0, m, MLEN_128, k256, KLEN_256, t256_128, AES_BLOCK_SIZE},
  31047. {WC_CMAC_AES, 0, m, MLEN_320, k256, KLEN_256, t256_320, AES_BLOCK_SIZE},
  31048. {WC_CMAC_AES, 0, m, MLEN_512, k256, KLEN_256, t256_512, AES_BLOCK_SIZE},
  31049. #endif
  31050. #ifdef WOLFSSL_AES_128
  31051. {WC_CMAC_AES, 0, m, MLEN_319, k128, KLEN_128, t128_319, AES_BLOCK_SIZE}
  31052. #endif
  31053. };
  31054. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  31055. Cmac *cmac;
  31056. #else
  31057. Cmac cmac[1];
  31058. #endif
  31059. byte tag[AES_BLOCK_SIZE];
  31060. const CMAC_Test_Case* tc;
  31061. word32 i, tagSz;
  31062. int ret;
  31063. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  31064. if ((cmac = (Cmac *)XMALLOC(sizeof *cmac, HEAP_HINT, DYNAMIC_TYPE_CMAC)) == NULL)
  31065. ERROR_OUT(-12009, out);
  31066. #endif
  31067. for (i = 0, tc = testCases;
  31068. i < sizeof(testCases)/sizeof(CMAC_Test_Case);
  31069. i++, tc++) {
  31070. XMEMSET(tag, 0, sizeof(tag));
  31071. tagSz = AES_BLOCK_SIZE;
  31072. #if !defined(HAVE_FIPS) || \
  31073. defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3)
  31074. if (wc_InitCmac_ex(cmac, tc->k, tc->kSz, tc->type, NULL, HEAP_HINT, devId) != 0)
  31075. #else
  31076. if (wc_InitCmac(cmac, tc->k, tc->kSz, tc->type, NULL) != 0)
  31077. #endif
  31078. {
  31079. ERROR_OUT(-12000, out);
  31080. }
  31081. if (tc->partial) {
  31082. if (wc_CmacUpdate(cmac, tc->m,
  31083. tc->mSz/2 - tc->partial) != 0)
  31084. ERROR_OUT(-12001, out);
  31085. if (wc_CmacUpdate(cmac, tc->m + tc->mSz/2 - tc->partial,
  31086. tc->mSz/2 + tc->partial) != 0)
  31087. ERROR_OUT(-12002, out);
  31088. }
  31089. else {
  31090. if (wc_CmacUpdate(cmac, tc->m, tc->mSz) != 0)
  31091. ERROR_OUT(-12003, out);
  31092. }
  31093. if (wc_CmacFinal(cmac, tag, &tagSz) != 0)
  31094. ERROR_OUT(-12004, out);
  31095. if (XMEMCMP(tag, tc->t, AES_BLOCK_SIZE) != 0)
  31096. ERROR_OUT(-12005, out);
  31097. XMEMSET(tag, 0, sizeof(tag));
  31098. tagSz = sizeof(tag);
  31099. if (wc_AesCmacGenerate(tag, &tagSz, tc->m, tc->mSz,
  31100. tc->k, tc->kSz) != 0)
  31101. ERROR_OUT(-12006, out);
  31102. if (XMEMCMP(tag, tc->t, AES_BLOCK_SIZE) != 0)
  31103. ERROR_OUT(-12007, out);
  31104. if (wc_AesCmacVerify(tc->t, tc->tSz, tc->m, tc->mSz,
  31105. tc->k, tc->kSz) != 0)
  31106. ERROR_OUT(-12008, out);
  31107. }
  31108. ret = 0;
  31109. out:
  31110. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  31111. if (cmac)
  31112. XFREE(cmac, HEAP_HINT, DYNAMIC_TYPE_CMAC);
  31113. #endif
  31114. return ret;
  31115. }
  31116. #endif /* NO_AES && WOLFSSL_CMAC */
  31117. #if defined(WOLFSSL_SIPHASH)
  31118. #if WOLFSSL_SIPHASH_CROUNDS == 2 && WOLFSSL_SIPHASH_DROUNDS == 4
  31119. /* Test vectors from:
  31120. * https://github.com/veorq/SipHash/blob/master/vectors.h
  31121. */
  31122. static const unsigned char siphash_key[SIPHASH_KEY_SIZE] = {
  31123. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  31124. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
  31125. };
  31126. static const unsigned char siphash_msg[64] = {
  31127. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  31128. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  31129. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  31130. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
  31131. 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  31132. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
  31133. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  31134. 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f
  31135. };
  31136. static const unsigned char siphash_r8[64][SIPHASH_MAC_SIZE_8] = {
  31137. { 0x31, 0x0e, 0x0e, 0xdd, 0x47, 0xdb, 0x6f, 0x72, },
  31138. { 0xfd, 0x67, 0xdc, 0x93, 0xc5, 0x39, 0xf8, 0x74, },
  31139. { 0x5a, 0x4f, 0xa9, 0xd9, 0x09, 0x80, 0x6c, 0x0d, },
  31140. { 0x2d, 0x7e, 0xfb, 0xd7, 0x96, 0x66, 0x67, 0x85, },
  31141. { 0xb7, 0x87, 0x71, 0x27, 0xe0, 0x94, 0x27, 0xcf, },
  31142. { 0x8d, 0xa6, 0x99, 0xcd, 0x64, 0x55, 0x76, 0x18, },
  31143. { 0xce, 0xe3, 0xfe, 0x58, 0x6e, 0x46, 0xc9, 0xcb, },
  31144. { 0x37, 0xd1, 0x01, 0x8b, 0xf5, 0x00, 0x02, 0xab, },
  31145. { 0x62, 0x24, 0x93, 0x9a, 0x79, 0xf5, 0xf5, 0x93, },
  31146. { 0xb0, 0xe4, 0xa9, 0x0b, 0xdf, 0x82, 0x00, 0x9e, },
  31147. { 0xf3, 0xb9, 0xdd, 0x94, 0xc5, 0xbb, 0x5d, 0x7a, },
  31148. { 0xa7, 0xad, 0x6b, 0x22, 0x46, 0x2f, 0xb3, 0xf4, },
  31149. { 0xfb, 0xe5, 0x0e, 0x86, 0xbc, 0x8f, 0x1e, 0x75, },
  31150. { 0x90, 0x3d, 0x84, 0xc0, 0x27, 0x56, 0xea, 0x14, },
  31151. { 0xee, 0xf2, 0x7a, 0x8e, 0x90, 0xca, 0x23, 0xf7, },
  31152. { 0xe5, 0x45, 0xbe, 0x49, 0x61, 0xca, 0x29, 0xa1, },
  31153. { 0xdb, 0x9b, 0xc2, 0x57, 0x7f, 0xcc, 0x2a, 0x3f, },
  31154. { 0x94, 0x47, 0xbe, 0x2c, 0xf5, 0xe9, 0x9a, 0x69, },
  31155. { 0x9c, 0xd3, 0x8d, 0x96, 0xf0, 0xb3, 0xc1, 0x4b, },
  31156. { 0xbd, 0x61, 0x79, 0xa7, 0x1d, 0xc9, 0x6d, 0xbb, },
  31157. { 0x98, 0xee, 0xa2, 0x1a, 0xf2, 0x5c, 0xd6, 0xbe, },
  31158. { 0xc7, 0x67, 0x3b, 0x2e, 0xb0, 0xcb, 0xf2, 0xd0, },
  31159. { 0x88, 0x3e, 0xa3, 0xe3, 0x95, 0x67, 0x53, 0x93, },
  31160. { 0xc8, 0xce, 0x5c, 0xcd, 0x8c, 0x03, 0x0c, 0xa8, },
  31161. { 0x94, 0xaf, 0x49, 0xf6, 0xc6, 0x50, 0xad, 0xb8, },
  31162. { 0xea, 0xb8, 0x85, 0x8a, 0xde, 0x92, 0xe1, 0xbc, },
  31163. { 0xf3, 0x15, 0xbb, 0x5b, 0xb8, 0x35, 0xd8, 0x17, },
  31164. { 0xad, 0xcf, 0x6b, 0x07, 0x63, 0x61, 0x2e, 0x2f, },
  31165. { 0xa5, 0xc9, 0x1d, 0xa7, 0xac, 0xaa, 0x4d, 0xde, },
  31166. { 0x71, 0x65, 0x95, 0x87, 0x66, 0x50, 0xa2, 0xa6, },
  31167. { 0x28, 0xef, 0x49, 0x5c, 0x53, 0xa3, 0x87, 0xad, },
  31168. { 0x42, 0xc3, 0x41, 0xd8, 0xfa, 0x92, 0xd8, 0x32, },
  31169. { 0xce, 0x7c, 0xf2, 0x72, 0x2f, 0x51, 0x27, 0x71, },
  31170. { 0xe3, 0x78, 0x59, 0xf9, 0x46, 0x23, 0xf3, 0xa7, },
  31171. { 0x38, 0x12, 0x05, 0xbb, 0x1a, 0xb0, 0xe0, 0x12, },
  31172. { 0xae, 0x97, 0xa1, 0x0f, 0xd4, 0x34, 0xe0, 0x15, },
  31173. { 0xb4, 0xa3, 0x15, 0x08, 0xbe, 0xff, 0x4d, 0x31, },
  31174. { 0x81, 0x39, 0x62, 0x29, 0xf0, 0x90, 0x79, 0x02, },
  31175. { 0x4d, 0x0c, 0xf4, 0x9e, 0xe5, 0xd4, 0xdc, 0xca, },
  31176. { 0x5c, 0x73, 0x33, 0x6a, 0x76, 0xd8, 0xbf, 0x9a, },
  31177. { 0xd0, 0xa7, 0x04, 0x53, 0x6b, 0xa9, 0x3e, 0x0e, },
  31178. { 0x92, 0x59, 0x58, 0xfc, 0xd6, 0x42, 0x0c, 0xad, },
  31179. { 0xa9, 0x15, 0xc2, 0x9b, 0xc8, 0x06, 0x73, 0x18, },
  31180. { 0x95, 0x2b, 0x79, 0xf3, 0xbc, 0x0a, 0xa6, 0xd4, },
  31181. { 0xf2, 0x1d, 0xf2, 0xe4, 0x1d, 0x45, 0x35, 0xf9, },
  31182. { 0x87, 0x57, 0x75, 0x19, 0x04, 0x8f, 0x53, 0xa9, },
  31183. { 0x10, 0xa5, 0x6c, 0xf5, 0xdf, 0xcd, 0x9a, 0xdb, },
  31184. { 0xeb, 0x75, 0x09, 0x5c, 0xcd, 0x98, 0x6c, 0xd0, },
  31185. { 0x51, 0xa9, 0xcb, 0x9e, 0xcb, 0xa3, 0x12, 0xe6, },
  31186. { 0x96, 0xaf, 0xad, 0xfc, 0x2c, 0xe6, 0x66, 0xc7, },
  31187. { 0x72, 0xfe, 0x52, 0x97, 0x5a, 0x43, 0x64, 0xee, },
  31188. { 0x5a, 0x16, 0x45, 0xb2, 0x76, 0xd5, 0x92, 0xa1, },
  31189. { 0xb2, 0x74, 0xcb, 0x8e, 0xbf, 0x87, 0x87, 0x0a, },
  31190. { 0x6f, 0x9b, 0xb4, 0x20, 0x3d, 0xe7, 0xb3, 0x81, },
  31191. { 0xea, 0xec, 0xb2, 0xa3, 0x0b, 0x22, 0xa8, 0x7f, },
  31192. { 0x99, 0x24, 0xa4, 0x3c, 0xc1, 0x31, 0x57, 0x24, },
  31193. { 0xbd, 0x83, 0x8d, 0x3a, 0xaf, 0xbf, 0x8d, 0xb7, },
  31194. { 0x0b, 0x1a, 0x2a, 0x32, 0x65, 0xd5, 0x1a, 0xea, },
  31195. { 0x13, 0x50, 0x79, 0xa3, 0x23, 0x1c, 0xe6, 0x60, },
  31196. { 0x93, 0x2b, 0x28, 0x46, 0xe4, 0xd7, 0x06, 0x66, },
  31197. { 0xe1, 0x91, 0x5f, 0x5c, 0xb1, 0xec, 0xa4, 0x6c, },
  31198. { 0xf3, 0x25, 0x96, 0x5c, 0xa1, 0x6d, 0x62, 0x9f, },
  31199. { 0x57, 0x5f, 0xf2, 0x8e, 0x60, 0x38, 0x1b, 0xe5, },
  31200. { 0x72, 0x45, 0x06, 0xeb, 0x4c, 0x32, 0x8a, 0x95, },
  31201. };
  31202. static const unsigned char siphash_r16[64][SIPHASH_MAC_SIZE_16] = {
  31203. { 0xa3, 0x81, 0x7f, 0x04, 0xba, 0x25, 0xa8, 0xe6,
  31204. 0x6d, 0xf6, 0x72, 0x14, 0xc7, 0x55, 0x02, 0x93, },
  31205. { 0xda, 0x87, 0xc1, 0xd8, 0x6b, 0x99, 0xaf, 0x44,
  31206. 0x34, 0x76, 0x59, 0x11, 0x9b, 0x22, 0xfc, 0x45, },
  31207. { 0x81, 0x77, 0x22, 0x8d, 0xa4, 0xa4, 0x5d, 0xc7,
  31208. 0xfc, 0xa3, 0x8b, 0xde, 0xf6, 0x0a, 0xff, 0xe4, },
  31209. { 0x9c, 0x70, 0xb6, 0x0c, 0x52, 0x67, 0xa9, 0x4e,
  31210. 0x5f, 0x33, 0xb6, 0xb0, 0x29, 0x85, 0xed, 0x51, },
  31211. { 0xf8, 0x81, 0x64, 0xc1, 0x2d, 0x9c, 0x8f, 0xaf,
  31212. 0x7d, 0x0f, 0x6e, 0x7c, 0x7b, 0xcd, 0x55, 0x79, },
  31213. { 0x13, 0x68, 0x87, 0x59, 0x80, 0x77, 0x6f, 0x88,
  31214. 0x54, 0x52, 0x7a, 0x07, 0x69, 0x0e, 0x96, 0x27, },
  31215. { 0x14, 0xee, 0xca, 0x33, 0x8b, 0x20, 0x86, 0x13,
  31216. 0x48, 0x5e, 0xa0, 0x30, 0x8f, 0xd7, 0xa1, 0x5e, },
  31217. { 0xa1, 0xf1, 0xeb, 0xbe, 0xd8, 0xdb, 0xc1, 0x53,
  31218. 0xc0, 0xb8, 0x4a, 0xa6, 0x1f, 0xf0, 0x82, 0x39, },
  31219. { 0x3b, 0x62, 0xa9, 0xba, 0x62, 0x58, 0xf5, 0x61,
  31220. 0x0f, 0x83, 0xe2, 0x64, 0xf3, 0x14, 0x97, 0xb4, },
  31221. { 0x26, 0x44, 0x99, 0x06, 0x0a, 0xd9, 0xba, 0xab,
  31222. 0xc4, 0x7f, 0x8b, 0x02, 0xbb, 0x6d, 0x71, 0xed, },
  31223. { 0x00, 0x11, 0x0d, 0xc3, 0x78, 0x14, 0x69, 0x56,
  31224. 0xc9, 0x54, 0x47, 0xd3, 0xf3, 0xd0, 0xfb, 0xba, },
  31225. { 0x01, 0x51, 0xc5, 0x68, 0x38, 0x6b, 0x66, 0x77,
  31226. 0xa2, 0xb4, 0xdc, 0x6f, 0x81, 0xe5, 0xdc, 0x18, },
  31227. { 0xd6, 0x26, 0xb2, 0x66, 0x90, 0x5e, 0xf3, 0x58,
  31228. 0x82, 0x63, 0x4d, 0xf6, 0x85, 0x32, 0xc1, 0x25, },
  31229. { 0x98, 0x69, 0xe2, 0x47, 0xe9, 0xc0, 0x8b, 0x10,
  31230. 0xd0, 0x29, 0x93, 0x4f, 0xc4, 0xb9, 0x52, 0xf7, },
  31231. { 0x31, 0xfc, 0xef, 0xac, 0x66, 0xd7, 0xde, 0x9c,
  31232. 0x7e, 0xc7, 0x48, 0x5f, 0xe4, 0x49, 0x49, 0x02, },
  31233. { 0x54, 0x93, 0xe9, 0x99, 0x33, 0xb0, 0xa8, 0x11,
  31234. 0x7e, 0x08, 0xec, 0x0f, 0x97, 0xcf, 0xc3, 0xd9, },
  31235. { 0x6e, 0xe2, 0xa4, 0xca, 0x67, 0xb0, 0x54, 0xbb,
  31236. 0xfd, 0x33, 0x15, 0xbf, 0x85, 0x23, 0x05, 0x77, },
  31237. { 0x47, 0x3d, 0x06, 0xe8, 0x73, 0x8d, 0xb8, 0x98,
  31238. 0x54, 0xc0, 0x66, 0xc4, 0x7a, 0xe4, 0x77, 0x40, },
  31239. { 0xa4, 0x26, 0xe5, 0xe4, 0x23, 0xbf, 0x48, 0x85,
  31240. 0x29, 0x4d, 0xa4, 0x81, 0xfe, 0xae, 0xf7, 0x23, },
  31241. { 0x78, 0x01, 0x77, 0x31, 0xcf, 0x65, 0xfa, 0xb0,
  31242. 0x74, 0xd5, 0x20, 0x89, 0x52, 0x51, 0x2e, 0xb1, },
  31243. { 0x9e, 0x25, 0xfc, 0x83, 0x3f, 0x22, 0x90, 0x73,
  31244. 0x3e, 0x93, 0x44, 0xa5, 0xe8, 0x38, 0x39, 0xeb, },
  31245. { 0x56, 0x8e, 0x49, 0x5a, 0xbe, 0x52, 0x5a, 0x21,
  31246. 0x8a, 0x22, 0x14, 0xcd, 0x3e, 0x07, 0x1d, 0x12, },
  31247. { 0x4a, 0x29, 0xb5, 0x45, 0x52, 0xd1, 0x6b, 0x9a,
  31248. 0x46, 0x9c, 0x10, 0x52, 0x8e, 0xff, 0x0a, 0xae, },
  31249. { 0xc9, 0xd1, 0x84, 0xdd, 0xd5, 0xa9, 0xf5, 0xe0,
  31250. 0xcf, 0x8c, 0xe2, 0x9a, 0x9a, 0xbf, 0x69, 0x1c, },
  31251. { 0x2d, 0xb4, 0x79, 0xae, 0x78, 0xbd, 0x50, 0xd8,
  31252. 0x88, 0x2a, 0x8a, 0x17, 0x8a, 0x61, 0x32, 0xad, },
  31253. { 0x8e, 0xce, 0x5f, 0x04, 0x2d, 0x5e, 0x44, 0x7b,
  31254. 0x50, 0x51, 0xb9, 0xea, 0xcb, 0x8d, 0x8f, 0x6f, },
  31255. { 0x9c, 0x0b, 0x53, 0xb4, 0xb3, 0xc3, 0x07, 0xe8,
  31256. 0x7e, 0xae, 0xe0, 0x86, 0x78, 0x14, 0x1f, 0x66, },
  31257. { 0xab, 0xf2, 0x48, 0xaf, 0x69, 0xa6, 0xea, 0xe4,
  31258. 0xbf, 0xd3, 0xeb, 0x2f, 0x12, 0x9e, 0xeb, 0x94, },
  31259. { 0x06, 0x64, 0xda, 0x16, 0x68, 0x57, 0x4b, 0x88,
  31260. 0xb9, 0x35, 0xf3, 0x02, 0x73, 0x58, 0xae, 0xf4, },
  31261. { 0xaa, 0x4b, 0x9d, 0xc4, 0xbf, 0x33, 0x7d, 0xe9,
  31262. 0x0c, 0xd4, 0xfd, 0x3c, 0x46, 0x7c, 0x6a, 0xb7, },
  31263. { 0xea, 0x5c, 0x7f, 0x47, 0x1f, 0xaf, 0x6b, 0xde,
  31264. 0x2b, 0x1a, 0xd7, 0xd4, 0x68, 0x6d, 0x22, 0x87, },
  31265. { 0x29, 0x39, 0xb0, 0x18, 0x32, 0x23, 0xfa, 0xfc,
  31266. 0x17, 0x23, 0xde, 0x4f, 0x52, 0xc4, 0x3d, 0x35, },
  31267. { 0x7c, 0x39, 0x56, 0xca, 0x5e, 0xea, 0xfc, 0x3e,
  31268. 0x36, 0x3e, 0x9d, 0x55, 0x65, 0x46, 0xeb, 0x68, },
  31269. { 0x77, 0xc6, 0x07, 0x71, 0x46, 0xf0, 0x1c, 0x32,
  31270. 0xb6, 0xb6, 0x9d, 0x5f, 0x4e, 0xa9, 0xff, 0xcf, },
  31271. { 0x37, 0xa6, 0x98, 0x6c, 0xb8, 0x84, 0x7e, 0xdf,
  31272. 0x09, 0x25, 0xf0, 0xf1, 0x30, 0x9b, 0x54, 0xde, },
  31273. { 0xa7, 0x05, 0xf0, 0xe6, 0x9d, 0xa9, 0xa8, 0xf9,
  31274. 0x07, 0x24, 0x1a, 0x2e, 0x92, 0x3c, 0x8c, 0xc8, },
  31275. { 0x3d, 0xc4, 0x7d, 0x1f, 0x29, 0xc4, 0x48, 0x46,
  31276. 0x1e, 0x9e, 0x76, 0xed, 0x90, 0x4f, 0x67, 0x11, },
  31277. { 0x0d, 0x62, 0xbf, 0x01, 0xe6, 0xfc, 0x0e, 0x1a,
  31278. 0x0d, 0x3c, 0x47, 0x51, 0xc5, 0xd3, 0x69, 0x2b, },
  31279. { 0x8c, 0x03, 0x46, 0x8b, 0xca, 0x7c, 0x66, 0x9e,
  31280. 0xe4, 0xfd, 0x5e, 0x08, 0x4b, 0xbe, 0xe7, 0xb5, },
  31281. { 0x52, 0x8a, 0x5b, 0xb9, 0x3b, 0xaf, 0x2c, 0x9c,
  31282. 0x44, 0x73, 0xcc, 0xe5, 0xd0, 0xd2, 0x2b, 0xd9, },
  31283. { 0xdf, 0x6a, 0x30, 0x1e, 0x95, 0xc9, 0x5d, 0xad,
  31284. 0x97, 0xae, 0x0c, 0xc8, 0xc6, 0x91, 0x3b, 0xd8, },
  31285. { 0x80, 0x11, 0x89, 0x90, 0x2c, 0x85, 0x7f, 0x39,
  31286. 0xe7, 0x35, 0x91, 0x28, 0x5e, 0x70, 0xb6, 0xdb, },
  31287. { 0xe6, 0x17, 0x34, 0x6a, 0xc9, 0xc2, 0x31, 0xbb,
  31288. 0x36, 0x50, 0xae, 0x34, 0xcc, 0xca, 0x0c, 0x5b, },
  31289. { 0x27, 0xd9, 0x34, 0x37, 0xef, 0xb7, 0x21, 0xaa,
  31290. 0x40, 0x18, 0x21, 0xdc, 0xec, 0x5a, 0xdf, 0x89, },
  31291. { 0x89, 0x23, 0x7d, 0x9d, 0xed, 0x9c, 0x5e, 0x78,
  31292. 0xd8, 0xb1, 0xc9, 0xb1, 0x66, 0xcc, 0x73, 0x42, },
  31293. { 0x4a, 0x6d, 0x80, 0x91, 0xbf, 0x5e, 0x7d, 0x65,
  31294. 0x11, 0x89, 0xfa, 0x94, 0xa2, 0x50, 0xb1, 0x4c, },
  31295. { 0x0e, 0x33, 0xf9, 0x60, 0x55, 0xe7, 0xae, 0x89,
  31296. 0x3f, 0xfc, 0x0e, 0x3d, 0xcf, 0x49, 0x29, 0x02, },
  31297. { 0xe6, 0x1c, 0x43, 0x2b, 0x72, 0x0b, 0x19, 0xd1,
  31298. 0x8e, 0xc8, 0xd8, 0x4b, 0xdc, 0x63, 0x15, 0x1b, },
  31299. { 0xf7, 0xe5, 0xae, 0xf5, 0x49, 0xf7, 0x82, 0xcf,
  31300. 0x37, 0x90, 0x55, 0xa6, 0x08, 0x26, 0x9b, 0x16, },
  31301. { 0x43, 0x8d, 0x03, 0x0f, 0xd0, 0xb7, 0xa5, 0x4f,
  31302. 0xa8, 0x37, 0xf2, 0xad, 0x20, 0x1a, 0x64, 0x03, },
  31303. { 0xa5, 0x90, 0xd3, 0xee, 0x4f, 0xbf, 0x04, 0xe3,
  31304. 0x24, 0x7e, 0x0d, 0x27, 0xf2, 0x86, 0x42, 0x3f, },
  31305. { 0x5f, 0xe2, 0xc1, 0xa1, 0x72, 0xfe, 0x93, 0xc4,
  31306. 0xb1, 0x5c, 0xd3, 0x7c, 0xae, 0xf9, 0xf5, 0x38, },
  31307. { 0x2c, 0x97, 0x32, 0x5c, 0xbd, 0x06, 0xb3, 0x6e,
  31308. 0xb2, 0x13, 0x3d, 0xd0, 0x8b, 0x3a, 0x01, 0x7c, },
  31309. { 0x92, 0xc8, 0x14, 0x22, 0x7a, 0x6b, 0xca, 0x94,
  31310. 0x9f, 0xf0, 0x65, 0x9f, 0x00, 0x2a, 0xd3, 0x9e, },
  31311. { 0xdc, 0xe8, 0x50, 0x11, 0x0b, 0xd8, 0x32, 0x8c,
  31312. 0xfb, 0xd5, 0x08, 0x41, 0xd6, 0x91, 0x1d, 0x87, },
  31313. { 0x67, 0xf1, 0x49, 0x84, 0xc7, 0xda, 0x79, 0x12,
  31314. 0x48, 0xe3, 0x2b, 0xb5, 0x92, 0x25, 0x83, 0xda, },
  31315. { 0x19, 0x38, 0xf2, 0xcf, 0x72, 0xd5, 0x4e, 0xe9,
  31316. 0x7e, 0x94, 0x16, 0x6f, 0xa9, 0x1d, 0x2a, 0x36, },
  31317. { 0x74, 0x48, 0x1e, 0x96, 0x46, 0xed, 0x49, 0xfe,
  31318. 0x0f, 0x62, 0x24, 0x30, 0x16, 0x04, 0x69, 0x8e, },
  31319. { 0x57, 0xfc, 0xa5, 0xde, 0x98, 0xa9, 0xd6, 0xd8,
  31320. 0x00, 0x64, 0x38, 0xd0, 0x58, 0x3d, 0x8a, 0x1d, },
  31321. { 0x9f, 0xec, 0xde, 0x1c, 0xef, 0xdc, 0x1c, 0xbe,
  31322. 0xd4, 0x76, 0x36, 0x74, 0xd9, 0x57, 0x53, 0x59, },
  31323. { 0xe3, 0x04, 0x0c, 0x00, 0xeb, 0x28, 0xf1, 0x53,
  31324. 0x66, 0xca, 0x73, 0xcb, 0xd8, 0x72, 0xe7, 0x40, },
  31325. { 0x76, 0x97, 0x00, 0x9a, 0x6a, 0x83, 0x1d, 0xfe,
  31326. 0xcc, 0xa9, 0x1c, 0x59, 0x93, 0x67, 0x0f, 0x7a, },
  31327. { 0x58, 0x53, 0x54, 0x23, 0x21, 0xf5, 0x67, 0xa0,
  31328. 0x05, 0xd5, 0x47, 0xa4, 0xf0, 0x47, 0x59, 0xbd, },
  31329. { 0x51, 0x50, 0xd1, 0x77, 0x2f, 0x50, 0x83, 0x4a,
  31330. 0x50, 0x3e, 0x06, 0x9a, 0x97, 0x3f, 0xbd, 0x7c, },
  31331. };
  31332. #endif
  31333. WOLFSSL_TEST_SUBROUTINE int siphash_test(void)
  31334. {
  31335. int ret = 0;
  31336. int i;
  31337. #if WOLFSSL_SIPHASH_CROUNDS == 2 && WOLFSSL_SIPHASH_DROUNDS == 4
  31338. unsigned char res[SIPHASH_MAC_SIZE_16];
  31339. unsigned char tmp[SIPHASH_MAC_SIZE_8];
  31340. SipHash siphash;
  31341. for (i = 0; i < 64; i++) {
  31342. ret = wc_InitSipHash(&siphash, siphash_key, SIPHASH_MAC_SIZE_8);
  31343. if (ret != 0)
  31344. return -12100 - i;
  31345. ret = wc_SipHashUpdate(&siphash, siphash_msg, i);
  31346. if (ret != 0)
  31347. return -12200 - i;
  31348. ret = wc_SipHashFinal(&siphash, res, SIPHASH_MAC_SIZE_8);
  31349. if (ret != 0)
  31350. return -12300 - i;
  31351. if (XMEMCMP(res, siphash_r8[i], SIPHASH_MAC_SIZE_8) != 0)
  31352. return -12400 - i;
  31353. ret = wc_SipHash(siphash_key, siphash_msg, i, res, SIPHASH_MAC_SIZE_8);
  31354. if (ret != 0)
  31355. return -12500 - i;
  31356. if (XMEMCMP(res, siphash_r8[i], SIPHASH_MAC_SIZE_8) != 0)
  31357. return -12600 - i;
  31358. }
  31359. for (i = 0; i < 64; i++) {
  31360. ret = wc_InitSipHash(&siphash, siphash_key, SIPHASH_MAC_SIZE_16);
  31361. if (ret != 0)
  31362. return -12700 - i;
  31363. ret = wc_SipHashUpdate(&siphash, siphash_msg, i);
  31364. if (ret != 0)
  31365. return -12800 - i;
  31366. ret = wc_SipHashFinal(&siphash, res, SIPHASH_MAC_SIZE_16);
  31367. if (ret != 0)
  31368. return -12900 - i;
  31369. if (XMEMCMP(res, siphash_r16[i], SIPHASH_MAC_SIZE_16) != 0)
  31370. return -13000 - i;
  31371. ret = wc_SipHash(siphash_key, siphash_msg, i, res, SIPHASH_MAC_SIZE_16);
  31372. if (ret != 0)
  31373. return -13100 - i;
  31374. if (XMEMCMP(res, siphash_r16[i], SIPHASH_MAC_SIZE_16) != 0)
  31375. return -13200 - i;
  31376. }
  31377. #endif
  31378. /* Testing bad parameters. */
  31379. ret = wc_InitSipHash(NULL, NULL, SIPHASH_MAC_SIZE_8);
  31380. if (ret != BAD_FUNC_ARG)
  31381. return -13300;
  31382. ret = wc_InitSipHash(NULL, siphash_key, SIPHASH_MAC_SIZE_8);
  31383. if (ret != BAD_FUNC_ARG)
  31384. return -13301;
  31385. ret = wc_InitSipHash(&siphash, NULL, SIPHASH_MAC_SIZE_8);
  31386. if (ret != BAD_FUNC_ARG)
  31387. return -13302;
  31388. ret = wc_InitSipHash(&siphash, siphash_key, 7);
  31389. if (ret != BAD_FUNC_ARG)
  31390. return -13303;
  31391. ret = wc_InitSipHash(&siphash, siphash_key, SIPHASH_MAC_SIZE_8);
  31392. if (ret != 0)
  31393. return -13304;
  31394. ret = wc_SipHashUpdate(NULL, NULL, 0);
  31395. if (ret != BAD_FUNC_ARG)
  31396. return -13305;
  31397. ret = wc_SipHashUpdate(&siphash, NULL, 1);
  31398. if (ret != BAD_FUNC_ARG)
  31399. return -13306;
  31400. ret = wc_SipHashFinal(NULL, NULL, SIPHASH_MAC_SIZE_8);
  31401. if (ret != BAD_FUNC_ARG)
  31402. return -13307;
  31403. ret = wc_SipHashFinal(&siphash, NULL, SIPHASH_MAC_SIZE_8);
  31404. if (ret != BAD_FUNC_ARG)
  31405. return -13308;
  31406. ret = wc_SipHashFinal(NULL, res, SIPHASH_MAC_SIZE_8);
  31407. if (ret != BAD_FUNC_ARG)
  31408. return -13309;
  31409. ret = wc_SipHashFinal(&siphash, res, SIPHASH_MAC_SIZE_16);
  31410. if (ret != BAD_FUNC_ARG)
  31411. return -13310;
  31412. ret = wc_SipHash(NULL, NULL, 0, NULL, SIPHASH_MAC_SIZE_16);
  31413. if (ret != BAD_FUNC_ARG)
  31414. return -13311;
  31415. ret = wc_SipHash(siphash_key, NULL, 0, NULL, SIPHASH_MAC_SIZE_16);
  31416. if (ret != BAD_FUNC_ARG)
  31417. return -13312;
  31418. ret = wc_SipHash(NULL, NULL, 0, res, SIPHASH_MAC_SIZE_16);
  31419. if (ret != BAD_FUNC_ARG)
  31420. return -13313;
  31421. ret = wc_SipHash(siphash_key, NULL, 0, res, 15);
  31422. if (ret != BAD_FUNC_ARG)
  31423. return -13314;
  31424. ret = wc_SipHash(siphash_key, NULL, 1, res, SIPHASH_MAC_SIZE_16);
  31425. if (ret != BAD_FUNC_ARG)
  31426. return -13315;
  31427. /* Test cache with multiple non blocksize bytes */
  31428. ret = wc_InitSipHash(&siphash, siphash_key, SIPHASH_MAC_SIZE_8);
  31429. if (ret != 0)
  31430. return -13316;
  31431. ret = wc_SipHashUpdate(&siphash, siphash_msg, 5);
  31432. if (ret != 0)
  31433. return -13317;
  31434. ret = wc_SipHashUpdate(&siphash, siphash_msg + 5, 4);
  31435. if (ret != 0)
  31436. return -13318;
  31437. ret = wc_SipHashFinal(&siphash, res, SIPHASH_MAC_SIZE_8);
  31438. if (ret != 0)
  31439. return -13319;
  31440. ret = wc_InitSipHash(&siphash, siphash_key, SIPHASH_MAC_SIZE_8);
  31441. if (ret != 0)
  31442. return -13320;
  31443. ret = wc_SipHashUpdate(&siphash, siphash_msg, 9);
  31444. if (ret != 0)
  31445. return -13321;
  31446. ret = wc_SipHashFinal(&siphash, tmp, SIPHASH_MAC_SIZE_8);
  31447. if (ret != 0)
  31448. return -13322;
  31449. if (XMEMCMP(res, tmp, SIPHASH_MAC_SIZE_8) != 0)
  31450. return -13323;
  31451. return 0;
  31452. }
  31453. #endif /* WOLFSSL_SIPHASH */
  31454. #ifdef HAVE_LIBZ
  31455. static const byte sample_text[] =
  31456. "Biodiesel cupidatat marfa, cliche aute put a bird on it incididunt elit\n"
  31457. "polaroid. Sunt tattooed bespoke reprehenderit. Sint twee organic id\n"
  31458. "marfa. Commodo veniam ad esse gastropub. 3 wolf moon sartorial vero,\n"
  31459. "plaid delectus biodiesel squid +1 vice. Post-ironic keffiyeh leggings\n"
  31460. "selfies cray fap hoodie, forage anim. Carles cupidatat shoreditch, VHS\n"
  31461. "small batch meggings kogi dolore food truck bespoke gastropub.\n"
  31462. "\n"
  31463. "Terry richardson adipisicing actually typewriter tumblr, twee whatever\n"
  31464. "four loko you probably haven't heard of them high life. Messenger bag\n"
  31465. "whatever tattooed deep v mlkshk. Brooklyn pinterest assumenda chillwave\n"
  31466. "et, banksy ullamco messenger bag umami pariatur direct trade forage.\n"
  31467. "Typewriter culpa try-hard, pariatur sint brooklyn meggings. Gentrify\n"
  31468. "food truck next level, tousled irony non semiotics PBR ethical anim cred\n"
  31469. "readymade. Mumblecore brunch lomo odd future, portland organic terry\n"
  31470. "richardson elit leggings adipisicing ennui raw denim banjo hella. Godard\n"
  31471. "mixtape polaroid, pork belly readymade organic cray typewriter helvetica\n"
  31472. "four loko whatever street art yr farm-to-table.\n"
  31473. "\n"
  31474. "Vinyl keytar vice tofu. Locavore you probably haven't heard of them pug\n"
  31475. "pickled, hella tonx labore truffaut DIY mlkshk elit cosby sweater sint\n"
  31476. "et mumblecore. Elit swag semiotics, reprehenderit DIY sartorial nisi ugh\n"
  31477. "nesciunt pug pork belly wayfarers selfies delectus. Ethical hoodie\n"
  31478. "seitan fingerstache kale chips. Terry richardson artisan williamsburg,\n"
  31479. "eiusmod fanny pack irony tonx ennui lo-fi incididunt tofu YOLO\n"
  31480. "readymade. 8-bit sed ethnic beard officia. Pour-over iphone DIY butcher,\n"
  31481. "ethnic art party qui letterpress nisi proident jean shorts mlkshk\n"
  31482. "locavore.\n"
  31483. "\n"
  31484. "Narwhal flexitarian letterpress, do gluten-free voluptate next level\n"
  31485. "banh mi tonx incididunt carles DIY. Odd future nulla 8-bit beard ut\n"
  31486. "cillum pickled velit, YOLO officia you probably haven't heard of them\n"
  31487. "trust fund gastropub. Nisi adipisicing tattooed, Austin mlkshk 90's\n"
  31488. "small batch american apparel. Put a bird on it cosby sweater before they\n"
  31489. "sold out pork belly kogi hella. Street art mollit sustainable polaroid,\n"
  31490. "DIY ethnic ea pug beard dreamcatcher cosby sweater magna scenester nisi.\n"
  31491. "Sed pork belly skateboard mollit, labore proident eiusmod. Sriracha\n"
  31492. "excepteur cosby sweater, anim deserunt laborum eu aliquip ethical et\n"
  31493. "neutra PBR selvage.\n"
  31494. "\n"
  31495. "Raw denim pork belly truffaut, irony plaid sustainable put a bird on it\n"
  31496. "next level jean shorts exercitation. Hashtag keytar whatever, nihil\n"
  31497. "authentic aliquip disrupt laborum. Tattooed selfies deserunt trust fund\n"
  31498. "wayfarers. 3 wolf moon synth church-key sartorial, gastropub leggings\n"
  31499. "tattooed. Labore high life commodo, meggings raw denim fingerstache pug\n"
  31500. "trust fund leggings seitan forage. Nostrud ullamco duis, reprehenderit\n"
  31501. "incididunt flannel sustainable helvetica pork belly pug banksy you\n"
  31502. "probably haven't heard of them nesciunt farm-to-table. Disrupt nostrud\n"
  31503. "mollit magna, sriracha sartorial helvetica.\n"
  31504. "\n"
  31505. "Nulla kogi reprehenderit, skateboard sustainable duis adipisicing viral\n"
  31506. "ad fanny pack salvia. Fanny pack trust fund you probably haven't heard\n"
  31507. "of them YOLO vice nihil. Keffiyeh cray lo-fi pinterest cardigan aliqua,\n"
  31508. "reprehenderit aute. Culpa tousled williamsburg, marfa lomo actually anim\n"
  31509. "skateboard. Iphone aliqua ugh, semiotics pariatur vero readymade\n"
  31510. "organic. Marfa squid nulla, in laborum disrupt laboris irure gastropub.\n"
  31511. "Veniam sunt food truck leggings, sint vinyl fap.\n"
  31512. "\n"
  31513. "Hella dolore pork belly, truffaut carles you probably haven't heard of\n"
  31514. "them PBR helvetica in sapiente. Fashion axe ugh bushwick american\n"
  31515. "apparel. Fingerstache sed iphone, jean shorts blue bottle nisi bushwick\n"
  31516. "flexitarian officia veniam plaid bespoke fap YOLO lo-fi. Blog\n"
  31517. "letterpress mumblecore, food truck id cray brooklyn cillum ad sed.\n"
  31518. "Assumenda chambray wayfarers vinyl mixtape sustainable. VHS vinyl\n"
  31519. "delectus, culpa williamsburg polaroid cliche swag church-key synth kogi\n"
  31520. "magna pop-up literally. Swag thundercats ennui shoreditch vegan\n"
  31521. "pitchfork neutra truffaut etsy, sed single-origin coffee craft beer.\n"
  31522. "\n"
  31523. "Odio letterpress brooklyn elit. Nulla single-origin coffee in occaecat\n"
  31524. "meggings. Irony meggings 8-bit, chillwave lo-fi adipisicing cred\n"
  31525. "dreamcatcher veniam. Put a bird on it irony umami, trust fund bushwick\n"
  31526. "locavore kale chips. Sriracha swag thundercats, chillwave disrupt\n"
  31527. "tousled beard mollit mustache leggings portland next level. Nihil esse\n"
  31528. "est, skateboard art party etsy thundercats sed dreamcatcher ut iphone\n"
  31529. "swag consectetur et. Irure skateboard banjo, nulla deserunt messenger\n"
  31530. "bag dolor terry richardson sapiente.\n";
  31531. static const byte sample_text_gz[] = {
  31532. 0x1F, 0x8B, 0x08, 0x08, 0xC5, 0x49, 0xB5, 0x5B, 0x00, 0x03, 0x63, 0x69, 0x70,
  31533. 0x68, 0x65, 0x72, 0x74, 0x65, 0x78, 0x74, 0x2E, 0x74, 0x78, 0x74, 0x00, 0x8D,
  31534. 0x58, 0xCB, 0x92, 0xE4, 0xB6, 0x11, 0xBC, 0xE3, 0x2B, 0xEA, 0xA6, 0x83, 0xD9,
  31535. 0x1D, 0x72, 0xF8, 0x22, 0x1F, 0xB5, 0x96, 0xA5, 0xDD, 0x90, 0xBC, 0xAB, 0xD0,
  31536. 0x28, 0x36, 0x42, 0x47, 0x90, 0x2C, 0x36, 0xA1, 0x06, 0x09, 0x0A, 0x8F, 0xEE,
  31537. 0xE1, 0xDF, 0x3B, 0x0B, 0xE0, 0x73, 0x2C, 0x4B, 0xBA, 0xCD, 0xCE, 0x80, 0x78,
  31538. 0x64, 0x65, 0x65, 0x66, 0xED, 0x3B, 0xE3, 0x5A, 0xC3, 0x81, 0x2D, 0x35, 0x69,
  31539. 0x32, 0xAD, 0x8E, 0x3A, 0xD2, 0xA0, 0x7D, 0xA7, 0x2B, 0x6A, 0xAC, 0x69, 0x7A,
  31540. 0x26, 0x9D, 0x22, 0xD3, 0x94, 0x22, 0x69, 0xAA, 0x8D, 0x6F, 0xC9, 0x8D, 0x64,
  31541. 0x22, 0x99, 0xB1, 0x31, 0xAD, 0x69, 0xD3, 0x18, 0x89, 0xAD, 0x89, 0x6A, 0x72,
  31542. 0x56, 0x7B, 0x67, 0xDA, 0x2B, 0xBD, 0xC8, 0xEF, 0xB0, 0x4D, 0x74, 0x8E, 0x5B,
  31543. 0xAA, 0x39, 0x4C, 0xEE, 0xCE, 0xE4, 0x79, 0xF2, 0xDC, 0xF3, 0xD8, 0xB2, 0x37,
  31544. 0x11, 0x8B, 0x8C, 0x2C, 0x7A, 0x32, 0x93, 0xF3, 0x37, 0x3D, 0x9A, 0x86, 0x4C,
  31545. 0xAB, 0xF2, 0xB9, 0x57, 0xFA, 0x97, 0x1B, 0x06, 0xD7, 0x3A, 0x7A, 0xF0, 0x68,
  31546. 0xF4, 0x40, 0xBA, 0x25, 0x0E, 0x81, 0xE9, 0xA6, 0x43, 0xF4, 0x6E, 0x4A, 0xF5,
  31547. 0x95, 0xFE, 0x41, 0x4F, 0x67, 0x3B, 0x1A, 0x1C, 0xEE, 0x12, 0xB4, 0x8F, 0xCE,
  31548. 0x1B, 0x6D, 0xB1, 0xDE, 0xBB, 0x4A, 0x4D, 0x56, 0x9B, 0x96, 0x5A, 0xB6, 0xDC,
  31549. 0xC4, 0x14, 0x70, 0xE5, 0xF5, 0x7D, 0xE1, 0xB7, 0x84, 0x3F, 0xFC, 0xED, 0xEF,
  31550. 0xF4, 0x30, 0x0D, 0x5F, 0xE9, 0x47, 0x17, 0xE2, 0xC5, 0x78, 0x27, 0x67, 0xDF,
  31551. 0xB9, 0xEB, 0xCC, 0xCC, 0x3D, 0x59, 0xBE, 0xDD, 0xCC, 0x78, 0x0B, 0x0A, 0x1F,
  31552. 0x74, 0xF8, 0x8C, 0x1A, 0xAF, 0x67, 0xEA, 0xF4, 0x44, 0xBD, 0x93, 0x7D, 0x2A,
  31553. 0xEA, 0x9C, 0xD7, 0x37, 0x80, 0x32, 0x9A, 0x01, 0x37, 0xD5, 0xDE, 0xCA, 0xA2,
  31554. 0x0D, 0xB9, 0xD0, 0x3B, 0xCF, 0xAD, 0x89, 0x4D, 0x5F, 0xD1, 0xE7, 0xF7, 0x2F,
  31555. 0x2A, 0x0C, 0xDA, 0x5A, 0xAA, 0x35, 0x7E, 0x41, 0xC3, 0xB2, 0x37, 0xDD, 0xDD,
  31556. 0xCD, 0x50, 0xEB, 0x2C, 0x96, 0x62, 0x3B, 0xD7, 0x52, 0xF4, 0xA9, 0xB9, 0x6F,
  31557. 0x48, 0xED, 0xEF, 0x54, 0xEA, 0x67, 0xF6, 0x7E, 0x26, 0x8F, 0x3A, 0x68, 0xDF,
  31558. 0x06, 0xBC, 0x56, 0xB7, 0x66, 0x32, 0xC1, 0x34, 0xD8, 0x88, 0x34, 0x1E, 0x88,
  31559. 0xED, 0x67, 0x8A, 0xF3, 0xC4, 0x4F, 0xC0, 0xCA, 0x9E, 0x62, 0x1A, 0x6A, 0xEB,
  31560. 0xAB, 0x02, 0xED, 0xB3, 0xD7, 0x91, 0x81, 0x8A, 0xEA, 0x5C, 0xF2, 0x64, 0xDD,
  31561. 0xDD, 0xD1, 0xEC, 0x12, 0x4D, 0xDE, 0xD5, 0xBA, 0xC6, 0x77, 0xBD, 0x06, 0xC4,
  31562. 0x5F, 0x44, 0xEA, 0x59, 0x4B, 0x5D, 0x3B, 0x8A, 0x3D, 0x0F, 0xD4, 0x9B, 0x1B,
  31563. 0x80, 0x30, 0x1D, 0x30, 0xFA, 0x8F, 0x00, 0x3F, 0xDE, 0xB0, 0x6F, 0xAD, 0x6F,
  31564. 0x6A, 0xDD, 0x6E, 0x2F, 0x6E, 0xCB, 0x3C, 0xD1, 0x83, 0x06, 0x7B, 0x0F, 0xFD,
  31565. 0xFD, 0x4A, 0xEF, 0xBC, 0x73, 0x77, 0x3B, 0x8F, 0x34, 0xA1, 0xBA, 0xEC, 0x39,
  31566. 0x80, 0x33, 0x21, 0xA4, 0x01, 0x55, 0xD7, 0xD4, 0xF4, 0xC6, 0xDA, 0x27, 0x4E,
  31567. 0x54, 0x1C, 0x2B, 0xEC, 0x37, 0xDE, 0xC3, 0x4C, 0xC9, 0x5A, 0x3D, 0x34, 0x0E,
  31568. 0xD8, 0x1C, 0x0E, 0xA2, 0x34, 0xE8, 0xC1, 0xD0, 0xA4, 0x51, 0xD5, 0x88, 0x8B,
  31569. 0xB7, 0xC6, 0xA3, 0x96, 0x40, 0x49, 0xB7, 0xBC, 0xE0, 0x7F, 0x55, 0x3F, 0xEF,
  31570. 0x6F, 0x6E, 0x92, 0x9D, 0x34, 0xFE, 0x3C, 0x5F, 0x04, 0xA5, 0x6A, 0xFF, 0x30,
  31571. 0x08, 0xC9, 0xEA, 0xF5, 0x52, 0x2B, 0xFE, 0x57, 0xFA, 0x8E, 0xC7, 0xE8, 0x4D,
  31572. 0x37, 0xAB, 0x03, 0xFA, 0x23, 0xBF, 0x46, 0x94, 0xFF, 0xC1, 0x16, 0xE0, 0xB9,
  31573. 0x14, 0x2C, 0x9E, 0x27, 0xEC, 0x98, 0x69, 0x14, 0x92, 0xF1, 0x60, 0x5C, 0x34,
  31574. 0x4D, 0xA0, 0x1F, 0xDF, 0xFD, 0x44, 0x1C, 0x7B, 0xD3, 0x80, 0x70, 0x42, 0x02,
  31575. 0x30, 0x84, 0x5B, 0xE5, 0x59, 0xB7, 0xF3, 0x80, 0xFB, 0x01, 0x33, 0xA9, 0x00,
  31576. 0x37, 0x52, 0xDC, 0xDA, 0xA7, 0x11, 0x85, 0xB7, 0x6E, 0x70, 0xE4, 0xDA, 0x96,
  31577. 0xBA, 0x84, 0x5B, 0x81, 0x43, 0x93, 0xF3, 0xD1, 0xEA, 0xB1, 0xDD, 0xB8, 0x1F,
  31578. 0xA5, 0xCC, 0xEA, 0x50, 0x66, 0x69, 0xA9, 0x8D, 0x8C, 0xA7, 0xA2, 0xF3, 0x38,
  31579. 0x26, 0x43, 0x5E, 0x3F, 0x01, 0xBE, 0x1C, 0x0F, 0x20, 0x7F, 0x75, 0xA8, 0x20,
  31580. 0x80, 0xC4, 0xC3, 0x5C, 0x8B, 0x0D, 0xD4, 0x60, 0x5E, 0xA3, 0x9E, 0xD0, 0xB4,
  31581. 0x4B, 0x4F, 0xE6, 0x13, 0x85, 0x60, 0x42, 0x96, 0xED, 0xAA, 0xDB, 0xE9, 0x99,
  31582. 0xE3, 0x07, 0x0E, 0x61, 0xB3, 0x07, 0xE3, 0xB1, 0xFA, 0xC0, 0x9B, 0xAD, 0xF6,
  31583. 0xE0, 0x26, 0x33, 0xEA, 0xEA, 0x23, 0xCD, 0x1E, 0x9D, 0xE1, 0x87, 0x4B, 0x74,
  31584. 0x97, 0x08, 0x3E, 0xA1, 0x28, 0xEA, 0xB3, 0x19, 0x67, 0x8B, 0x76, 0x9A, 0xA3,
  31585. 0xF6, 0xB9, 0xCF, 0x80, 0x65, 0x97, 0xAE, 0xF4, 0x83, 0x6B, 0xF4, 0x43, 0x20,
  31586. 0xF9, 0x0B, 0xFC, 0x9B, 0xD2, 0x4D, 0x4D, 0xA6, 0xB9, 0xA3, 0x02, 0x55, 0x79,
  31587. 0x18, 0x36, 0x19, 0x5F, 0xC9, 0xEA, 0x5A, 0x76, 0x40, 0xB9, 0xBA, 0x0E, 0x9A,
  31588. 0x44, 0xDF, 0x7C, 0xF8, 0x65, 0x61, 0x5E, 0x81, 0xAB, 0x71, 0xA1, 0x9E, 0x29,
  31589. 0x3C, 0x59, 0xCB, 0x23, 0xA4, 0xF6, 0x60, 0x1A, 0x0D, 0x5B, 0x39, 0xAE, 0xF4,
  31590. 0x6F, 0x59, 0x16, 0x9E, 0x60, 0xD8, 0x56, 0xCF, 0xEA, 0x2C, 0x4C, 0x79, 0xD3,
  31591. 0x5D, 0x51, 0x46, 0xA0, 0x4E, 0xE9, 0xD6, 0xAB, 0x91, 0x43, 0x63, 0x44, 0xD7,
  31592. 0x70, 0xB9, 0x23, 0x98, 0x4F, 0x3D, 0x03, 0x02, 0xF6, 0x81, 0x56, 0xC1, 0x58,
  31593. 0x85, 0x07, 0xA7, 0x2D, 0x2C, 0x29, 0xCA, 0x01, 0x45, 0x31, 0x51, 0x8F, 0xD4,
  31594. 0x19, 0xA1, 0x79, 0x88, 0x5A, 0xA4, 0xF5, 0xAE, 0x2D, 0x4B, 0x63, 0x4C, 0x58,
  31595. 0xFE, 0xBF, 0xAD, 0xEE, 0xA3, 0x09, 0xF8, 0xE2, 0x89, 0xBE, 0x81, 0x0E, 0x86,
  31596. 0x3A, 0xF9, 0x5B, 0xA5, 0xD8, 0xA4, 0x00, 0x75, 0x04, 0xF2, 0x23, 0xB8, 0x39,
  31597. 0x69, 0x50, 0xB7, 0xD0, 0x34, 0x63, 0x54, 0xD8, 0x61, 0xDD, 0xA5, 0x33, 0x47,
  31598. 0x85, 0x96, 0x22, 0xD0, 0x2F, 0x9F, 0x7E, 0xF8, 0x74, 0x24, 0xEA, 0x57, 0x97,
  31599. 0x5A, 0xE0, 0x00, 0xCF, 0xC1, 0x67, 0xE1, 0x41, 0xBD, 0x94, 0xA1, 0x03, 0xD3,
  31600. 0xB4, 0x08, 0x64, 0xF2, 0x17, 0x27, 0x35, 0x37, 0x53, 0xEF, 0x46, 0xCE, 0xD8,
  31601. 0xD4, 0x09, 0x52, 0xC6, 0x1E, 0xF7, 0x28, 0xDF, 0x08, 0x0F, 0xD0, 0x6F, 0x71,
  31602. 0xA6, 0xDF, 0xE4, 0x60, 0x8E, 0xC0, 0x1E, 0x78, 0x86, 0x50, 0xB0, 0x9B, 0x84,
  31603. 0x7E, 0xE8, 0x36, 0xFA, 0x95, 0xF1, 0x12, 0x51, 0xC7, 0x18, 0x96, 0xA2, 0x29,
  31604. 0xBB, 0x70, 0x02, 0xB4, 0xF9, 0xA8, 0x3D, 0x08, 0x66, 0xA9, 0xB3, 0xFC, 0x0A,
  31605. 0x94, 0x80, 0xFD, 0x78, 0xDC, 0xAB, 0x82, 0x5A, 0xD2, 0xCD, 0xC2, 0x87, 0xC6,
  31606. 0x4B, 0x07, 0xFA, 0xD1, 0xC3, 0xD9, 0x34, 0x41, 0x85, 0xF8, 0xD0, 0xB6, 0x0A,
  31607. 0x9D, 0x00, 0x91, 0x35, 0x05, 0x88, 0xC3, 0xE3, 0x9B, 0x22, 0xD2, 0xB8, 0xFD,
  31608. 0x95, 0x3E, 0x6D, 0x5D, 0x48, 0xA3, 0x68, 0xCF, 0x02, 0x42, 0x79, 0x79, 0x8A,
  31609. 0xAA, 0x01, 0xD6, 0x09, 0x14, 0x2C, 0xF4, 0x83, 0xA3, 0x80, 0x31, 0x55, 0x46,
  31610. 0x6E, 0xC5, 0xE5, 0x2F, 0x30, 0x58, 0x81, 0xA2, 0x90, 0xBE, 0x2E, 0xA1, 0xC3,
  31611. 0x0F, 0xA6, 0xF5, 0x51, 0x00, 0x39, 0xB6, 0xF2, 0x2A, 0xA3, 0x15, 0x7D, 0x8D,
  31612. 0xF5, 0x66, 0x5C, 0xD9, 0xFC, 0xCF, 0x2F, 0xBF, 0x08, 0x27, 0xE7, 0xD0, 0x03,
  31613. 0xB8, 0xD9, 0x00, 0x13, 0x3D, 0x01, 0x6B, 0xB6, 0xA8, 0xCD, 0x5B, 0x3B, 0x3E,
  31614. 0x93, 0xBF, 0xE6, 0x2E, 0xB7, 0x4A, 0xCF, 0xB3, 0x0A, 0xCE, 0x62, 0x11, 0xD6,
  31615. 0x1F, 0x68, 0x9B, 0x1D, 0x68, 0xD1, 0x8C, 0x97, 0xBD, 0xA1, 0x07, 0x67, 0x73,
  31616. 0x87, 0xE0, 0x36, 0xDA, 0x8C, 0xD2, 0xD2, 0xBB, 0x84, 0x28, 0xA9, 0xFE, 0x52,
  31617. 0x74, 0xD6, 0xB9, 0x0F, 0x0A, 0x6A, 0x2D, 0x28, 0x35, 0x34, 0x3A, 0xD3, 0xE2,
  31618. 0xCD, 0x35, 0x06, 0x7D, 0x1B, 0x35, 0x85, 0x86, 0xD1, 0x3E, 0xF2, 0x6F, 0xA1,
  31619. 0xC4, 0x55, 0xBD, 0x00, 0xD8, 0xC3, 0x5D, 0xC2, 0x1D, 0x6B, 0x6B, 0x27, 0x5B,
  31620. 0x95, 0xF3, 0xAB, 0xB5, 0xD3, 0x37, 0xF2, 0x2C, 0x9C, 0xC7, 0x5D, 0xBD, 0xF1,
  31621. 0x68, 0x1C, 0xAD, 0xF8, 0xB5, 0xE1, 0x29, 0x72, 0x7A, 0x73, 0x62, 0x55, 0x24,
  31622. 0xB9, 0x85, 0xDF, 0x7B, 0x29, 0x7D, 0xDE, 0x08, 0xF5, 0xE4, 0x44, 0xDA, 0x1A,
  31623. 0x30, 0x74, 0xDA, 0xB4, 0x9B, 0x23, 0x9A, 0x3A, 0xC1, 0x53, 0xB2, 0xA2, 0xA3,
  31624. 0x7B, 0x1F, 0xD9, 0x56, 0xD4, 0x4F, 0x9B, 0xB2, 0x1E, 0xEE, 0xB8, 0x6A, 0x4E,
  31625. 0xB5, 0xF4, 0x5A, 0xC9, 0x18, 0x27, 0x9C, 0xDE, 0x14, 0x44, 0xED, 0xC4, 0x3C,
  31626. 0x71, 0x9F, 0x5F, 0xD9, 0x37, 0xA0, 0x78, 0x34, 0x6E, 0xBC, 0xD2, 0x7B, 0x1D,
  31627. 0xFA, 0x08, 0x39, 0x5A, 0x04, 0x73, 0x15, 0xD9, 0x0A, 0x48, 0xC1, 0x2D, 0x15,
  31628. 0x4E, 0x84, 0x30, 0x45, 0x69, 0xB3, 0xE5, 0xF6, 0xAD, 0x09, 0x1E, 0xCC, 0x5F,
  31629. 0x1F, 0x06, 0xD5, 0x58, 0xAD, 0x78, 0xD7, 0x9F, 0xE5, 0xED, 0x3B, 0x09, 0xD5,
  31630. 0xA6, 0x52, 0x6F, 0x92, 0xD3, 0x3C, 0xC6, 0x1E, 0xF2, 0x93, 0x7C, 0xD3, 0x5F,
  31631. 0x70, 0x85, 0x5D, 0xF8, 0xAA, 0x9D, 0xB7, 0x7B, 0x24, 0x5A, 0xE9, 0x0A, 0x35,
  31632. 0x2F, 0xF5, 0xD9, 0x82, 0x02, 0x8A, 0x90, 0x13, 0x5B, 0xB5, 0x67, 0x9C, 0xDD,
  31633. 0xA0, 0x4E, 0x82, 0x27, 0xDA, 0x7E, 0xE8, 0x8E, 0xCD, 0xE1, 0x56, 0x71, 0x2C,
  31634. 0xE6, 0x4E, 0x1F, 0x91, 0xCD, 0x7C, 0x6A, 0xB7, 0x78, 0xD0, 0x26, 0xF3, 0x56,
  31635. 0xA9, 0xD5, 0xA1, 0xC3, 0x3B, 0x98, 0xE9, 0x28, 0x09, 0xEF, 0x50, 0x90, 0xCD,
  31636. 0xC4, 0x8E, 0x75, 0xCC, 0xAC, 0x2D, 0xC9, 0x03, 0x6D, 0xAC, 0xFE, 0xC4, 0x88,
  31637. 0x36, 0xD1, 0x3F, 0xBB, 0x1C, 0x7D, 0xB3, 0x14, 0x61, 0x2C, 0xB7, 0x54, 0x4B,
  31638. 0xDB, 0x64, 0xB6, 0x57, 0x14, 0x16, 0x8E, 0x1E, 0x6C, 0x64, 0xBB, 0x8B, 0x48,
  31639. 0x5D, 0x96, 0x9D, 0xDC, 0x80, 0xA7, 0xF7, 0x54, 0xC7, 0x46, 0x38, 0x3E, 0x44,
  31640. 0xDE, 0x7E, 0x92, 0x8D, 0x07, 0xF6, 0x07, 0x37, 0x4E, 0x16, 0x10, 0xB4, 0x7D,
  31641. 0x88, 0x66, 0x7F, 0xBB, 0xFF, 0xEA, 0x00, 0xF3, 0xFF, 0x97, 0x2C, 0xB5, 0xBE,
  31642. 0x35, 0x4B, 0x5C, 0x36, 0xEC, 0x4C, 0xBD, 0x2B, 0x7D, 0xBF, 0x46, 0xE2, 0x9C,
  31643. 0x0E, 0x8A, 0xA3, 0xEC, 0xB1, 0x0E, 0x9A, 0xDA, 0x9A, 0x9B, 0x28, 0x92, 0x10,
  31644. 0x53, 0x57, 0xEA, 0xEC, 0xA2, 0x32, 0x32, 0x20, 0x1D, 0x97, 0x5C, 0xB6, 0x84,
  31645. 0xA9, 0x93, 0x8D, 0x95, 0x11, 0xA3, 0x24, 0xA3, 0x2D, 0xC6, 0x4A, 0xEF, 0xAA,
  31646. 0x1D, 0x85, 0x2B, 0x7D, 0x28, 0xBE, 0x53, 0xCE, 0x10, 0x1F, 0xAE, 0x0E, 0x41,
  31647. 0x6C, 0x4B, 0x79, 0x12, 0xFB, 0xF7, 0x54, 0xA3, 0x96, 0x54, 0x83, 0x20, 0x96,
  31648. 0x8F, 0x28, 0xA9, 0x3F, 0x8B, 0x3D, 0xBA, 0x77, 0xDC, 0x24, 0xE1, 0xD4, 0x49,
  31649. 0x40, 0xD8, 0x78, 0x31, 0x85, 0x43, 0xF6, 0xFE, 0x5C, 0xA6, 0x8F, 0x90, 0x09,
  31650. 0xB0, 0xE7, 0xC4, 0x95, 0xB2, 0x55, 0x49, 0x97, 0x8F, 0x1C, 0x78, 0x30, 0x20,
  31651. 0xA0, 0xB4, 0xEF, 0x73, 0x56, 0x59, 0x82, 0xFD, 0xCE, 0xBA, 0x6A, 0x8F, 0x2C,
  31652. 0x8B, 0x15, 0xFD, 0xA1, 0x85, 0xA8, 0x5C, 0x0F, 0x11, 0xA5, 0x9D, 0xC2, 0x46,
  31653. 0xC6, 0x9C, 0xC9, 0x40, 0x0B, 0x58, 0x6A, 0x1C, 0x7A, 0x23, 0xF9, 0xE0, 0x95,
  31654. 0x05, 0x13, 0x58, 0x72, 0xE8, 0x9F, 0x30, 0xAC, 0xCD, 0x26, 0xD4, 0x66, 0x13,
  31655. 0xDF, 0x1E, 0x7B, 0x4F, 0x9C, 0xBE, 0x38, 0x79, 0x75, 0x92, 0xA4, 0xDA, 0x26,
  31656. 0x44, 0x55, 0x17, 0xA3, 0xE5, 0x62, 0xDA, 0xEB, 0x86, 0xEA, 0x68, 0xC7, 0xAB,
  31657. 0xFD, 0x2D, 0x43, 0x59, 0x51, 0xC0, 0x75, 0x64, 0x91, 0x01, 0x29, 0x33, 0x28,
  31658. 0xF3, 0x04, 0x83, 0x80, 0x75, 0x37, 0x75, 0x0C, 0x03, 0x7B, 0x0A, 0xAB, 0x8E,
  31659. 0x60, 0x62, 0x8B, 0x4C, 0xAF, 0x2D, 0xA3, 0x2F, 0xFE, 0xAB, 0x45, 0xCF, 0xDA,
  31660. 0xAB, 0xFA, 0xFA, 0x30, 0x3D, 0xE8, 0xA1, 0x96, 0xA5, 0x7B, 0xE2, 0x2A, 0xD0,
  31661. 0xAF, 0x59, 0xF7, 0xD0, 0x32, 0x57, 0x19, 0xBD, 0xCA, 0x9F, 0xD5, 0x1A, 0xC7,
  31662. 0xAA, 0x65, 0x4A, 0x38, 0xB2, 0x70, 0x33, 0xB7, 0x75, 0xD2, 0xCD, 0xD1, 0xF0,
  31663. 0xA8, 0x87, 0x59, 0x20, 0xA5, 0x57, 0x55, 0xB1, 0xB2, 0xC9, 0x4D, 0x97, 0x34,
  31664. 0x41, 0xF3, 0xF0, 0x30, 0xA1, 0x2C, 0x1C, 0x49, 0x3E, 0x89, 0x7D, 0x12, 0xE2,
  31665. 0xC3, 0x04, 0xC3, 0x92, 0xC0, 0xF6, 0x39, 0x10, 0x80, 0x81, 0x8F, 0x08, 0xB4,
  31666. 0xF8, 0xB9, 0x13, 0x4E, 0x2C, 0xAE, 0xB3, 0x71, 0x82, 0x63, 0x98, 0xAB, 0x5C,
  31667. 0x1C, 0x10, 0xEA, 0x66, 0xF9, 0x02, 0x3A, 0x82, 0x61, 0xD0, 0xD4, 0xAE, 0x43,
  31668. 0xD4, 0x01, 0x3E, 0x9D, 0x04, 0x14, 0xF6, 0x60, 0xD8, 0xA7, 0xD6, 0xB8, 0x53,
  31669. 0xC8, 0xDA, 0x80, 0x93, 0xA0, 0x02, 0xDD, 0xCC, 0xE2, 0xF2, 0xBB, 0xFB, 0xE0,
  31670. 0x27, 0xD7, 0x34, 0x9A, 0x71, 0x49, 0xB5, 0x4F, 0x42, 0x1F, 0xB2, 0x9D, 0x6D,
  31671. 0xAA, 0x9D, 0xD3, 0x50, 0xB5, 0x8F, 0x6A, 0x4B, 0xDF, 0x1F, 0xD5, 0x27, 0x8F,
  31672. 0x3B, 0x27, 0xCF, 0x2F, 0x8C, 0xF8, 0x9D, 0x4C, 0x52, 0xBC, 0x32, 0x0F, 0x73,
  31673. 0xD5, 0x51, 0x8E, 0x36, 0x7E, 0xAD, 0x09, 0xF0, 0x94, 0x83, 0x5F, 0x36, 0xFD,
  31674. 0x7C, 0x03, 0xED, 0xF1, 0x5E, 0x4B, 0xF7, 0xAA, 0x55, 0x5C, 0x4A, 0x14, 0x59,
  31675. 0x85, 0x38, 0x2D, 0x8C, 0xDF, 0xEC, 0x65, 0x1B, 0xB8, 0x76, 0x57, 0x96, 0x3C,
  31676. 0x86, 0xED, 0xF2, 0x7F, 0x2D, 0x28, 0x48, 0xDA, 0x49, 0x7F, 0xF7, 0x54, 0x2B,
  31677. 0xD5, 0x39, 0xD5, 0x57, 0x0A, 0x75, 0x7A, 0x3E, 0x5E, 0x5D, 0xBA, 0x4A, 0x15,
  31678. 0xFA, 0xB8, 0x31, 0x80, 0x71, 0x2C, 0xCA, 0xC4, 0x51, 0x10, 0x16, 0x5D, 0x39,
  31679. 0xEC, 0x9D, 0x07, 0xB6, 0x6A, 0x89, 0x9F, 0x9B, 0x5B, 0x6F, 0x03, 0xB0, 0x92,
  31680. 0x01, 0x38, 0x6B, 0x48, 0x99, 0x0A, 0x8F, 0x13, 0xC1, 0xA6, 0x01, 0xEA, 0xBF,
  31681. 0x6F, 0x86, 0x43, 0x51, 0xB6, 0x11, 0x00, 0x00
  31682. };
  31683. WOLFSSL_TEST_SUBROUTINE int compress_test(void)
  31684. {
  31685. int ret = 0;
  31686. word32 dSz = sizeof(sample_text);
  31687. word32 cSz = (dSz + (word32)(dSz * 0.001) + 12);
  31688. byte *c;
  31689. byte *d;
  31690. c = (byte *)XMALLOC(cSz * sizeof(byte), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  31691. d = (byte *)XMALLOC(dSz * sizeof(byte), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  31692. if (c == NULL || d == NULL) {
  31693. ERROR_OUT(-12100, exit);
  31694. }
  31695. /* follow calloc and initialize to 0 */
  31696. XMEMSET(c, 0, cSz);
  31697. XMEMSET(d, 0, dSz);
  31698. if ((ret = wc_Compress(c, cSz, sample_text, dSz, 0)) < 0) {
  31699. ERROR_OUT(-12101, exit);
  31700. }
  31701. cSz = (word32)ret;
  31702. if ((ret = wc_DeCompress(d, dSz, c, cSz)) != (int)dSz) {
  31703. ERROR_OUT(-12102, exit);
  31704. }
  31705. dSz = (word32)ret;
  31706. if (XMEMCMP(d, sample_text, dSz) != 0) {
  31707. ERROR_OUT(-12103, exit);
  31708. }
  31709. /* GZIP tests */
  31710. cSz = (dSz + (word32)(dSz * 0.001) + 12); /* reset cSz */
  31711. XMEMSET(c, 0, cSz);
  31712. XMEMSET(d, 0, dSz);
  31713. ret = wc_Compress_ex(c, cSz, sample_text, dSz, 0, LIBZ_WINBITS_GZIP);
  31714. if (ret < 0) {
  31715. ERROR_OUT(-12104, exit);
  31716. }
  31717. cSz = (word32)ret;
  31718. ret = wc_DeCompress_ex(d, dSz, c, cSz, LIBZ_WINBITS_GZIP);
  31719. if (ret < 0) {
  31720. ERROR_OUT(-12105, exit);
  31721. }
  31722. if (XMEMCMP(d, sample_text, dSz) != 0) {
  31723. ERROR_OUT(-12106, exit);
  31724. }
  31725. /* Try with gzip generated output */
  31726. XMEMSET(d, 0, dSz);
  31727. ret = wc_DeCompress_ex(d, dSz, sample_text_gz, sizeof(sample_text_gz),
  31728. LIBZ_WINBITS_GZIP);
  31729. if (ret < 0) {
  31730. ERROR_OUT(-12107, exit);
  31731. }
  31732. dSz = (word32)ret;
  31733. if (XMEMCMP(d, sample_text, dSz) != 0) {
  31734. ERROR_OUT(-12108, exit);
  31735. }
  31736. ret = 0; /* success */
  31737. exit:
  31738. if (c) XFREE(c, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  31739. if (d) XFREE(d, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  31740. return ret;
  31741. }
  31742. #endif /* HAVE_LIBZ */
  31743. #ifdef HAVE_PKCS7
  31744. /* External Debugging/Testing Note:
  31745. *
  31746. * PKCS#7 test functions can output generated PKCS#7/CMS bundles for
  31747. * additional testing. To dump bundles to files DER encoded files, please
  31748. * define:
  31749. *
  31750. * #define PKCS7_OUTPUT_TEST_BUNDLES
  31751. */
  31752. /* Loads certs and keys for use with PKCS7 tests, from either files
  31753. * or buffers.
  31754. *
  31755. * rsaClientCertBuf - output buffer for RSA client cert
  31756. * rsaClientCertBufSz - IN/OUT size of output buffer, size of RSA client cert
  31757. * rsaClientPrivKeyBuf - output buffer for RSA client private key
  31758. * rsaClientPrivKeyBufSz - IN/OUT size of output buffer, size of RSA client key
  31759. *
  31760. * rsaServerCertBuf - output buffer for RSA server cert
  31761. * rsaServerCertBufSz - IN/OUT size of output buffer, size of RSA server cert
  31762. * rsaServerPrivKeyBuf - output buffer for RSA server private key
  31763. * rsaServerPrivKeyBufSz - IN/OUT size of output buffer, size of RSA server key
  31764. *
  31765. * rsaCaCertBuf - output buffer for RSA CA cert
  31766. * rsaCaCertBufSz - IN/OUT size of output buffer, size of RSA ca cert
  31767. * rsaCaPrivKeyBuf - output buffer for RSA CA private key
  31768. * rsaCaPrivKeyBufSz - IN/OUT size of output buffer, size of RSA CA key
  31769. *
  31770. * eccClientCertBuf - output buffer for ECC cert
  31771. * eccClientCertBufSz - IN/OUT size of output buffer, size of ECC cert
  31772. * eccClientPrivKeyBuf - output buffer for ECC private key
  31773. * eccClientPrivKeyBufSz - IN/OUT size of output buffer, size of ECC private key
  31774. *
  31775. * Returns 0 on success, negative on error
  31776. */
  31777. static int pkcs7_load_certs_keys(
  31778. byte* rsaClientCertBuf, word32* rsaClientCertBufSz,
  31779. byte* rsaClientPrivKeyBuf, word32* rsaClientPrivKeyBufSz,
  31780. byte* rsaServerCertBuf, word32* rsaServerCertBufSz,
  31781. byte* rsaServerPrivKeyBuf, word32* rsaServerPrivKeyBufSz,
  31782. byte* rsaCaCertBuf, word32* rsaCaCertBufSz,
  31783. byte* rsaCaPrivKeyBuf, word32* rsaCaPrivKeyBufSz,
  31784. byte* eccClientCertBuf, word32* eccClientCertBufSz,
  31785. byte* eccClientPrivKeyBuf, word32* eccClientPrivKeyBufSz)
  31786. {
  31787. #ifndef NO_FILESYSTEM
  31788. XFILE certFile;
  31789. XFILE keyFile;
  31790. (void)certFile;
  31791. (void)keyFile;
  31792. #endif
  31793. #ifndef NO_RSA
  31794. if (rsaClientCertBuf == NULL || rsaClientCertBufSz == NULL ||
  31795. rsaClientPrivKeyBuf == NULL || rsaClientPrivKeyBufSz == NULL)
  31796. return BAD_FUNC_ARG;
  31797. #endif
  31798. #ifdef HAVE_ECC
  31799. if (eccClientCertBuf == NULL || eccClientCertBufSz == NULL ||
  31800. eccClientPrivKeyBuf == NULL || eccClientPrivKeyBufSz == NULL)
  31801. return BAD_FUNC_ARG;
  31802. #endif
  31803. /* RSA */
  31804. #ifndef NO_RSA
  31805. #ifdef USE_CERT_BUFFERS_1024
  31806. if (*rsaClientCertBufSz < (word32)sizeof_client_cert_der_1024)
  31807. return -12110;
  31808. XMEMCPY(rsaClientCertBuf, client_cert_der_1024,
  31809. sizeof_client_cert_der_1024);
  31810. *rsaClientCertBufSz = sizeof_client_cert_der_1024;
  31811. if (rsaServerCertBuf != NULL) {
  31812. if (*rsaServerCertBufSz < (word32)sizeof_server_cert_der_1024)
  31813. return -12111;
  31814. XMEMCPY(rsaServerCertBuf, server_cert_der_1024,
  31815. sizeof_server_cert_der_1024);
  31816. *rsaServerCertBufSz = sizeof_server_cert_der_1024;
  31817. }
  31818. if (rsaCaCertBuf != NULL) {
  31819. if (*rsaCaCertBufSz < (word32)sizeof_ca_cert_der_1024)
  31820. return -12112;
  31821. XMEMCPY(rsaCaCertBuf, ca_cert_der_1024, sizeof_ca_cert_der_1024);
  31822. *rsaCaCertBufSz = sizeof_ca_cert_der_1024;
  31823. }
  31824. #elif defined(USE_CERT_BUFFERS_2048)
  31825. if (*rsaClientCertBufSz < (word32)sizeof_client_cert_der_2048)
  31826. return -12113;
  31827. XMEMCPY(rsaClientCertBuf, client_cert_der_2048,
  31828. sizeof_client_cert_der_2048);
  31829. *rsaClientCertBufSz = sizeof_client_cert_der_2048;
  31830. if (rsaServerCertBuf != NULL) {
  31831. if (*rsaServerCertBufSz < (word32)sizeof_server_cert_der_2048)
  31832. return -12114;
  31833. XMEMCPY(rsaServerCertBuf, server_cert_der_2048,
  31834. sizeof_server_cert_der_2048);
  31835. *rsaServerCertBufSz = sizeof_server_cert_der_2048;
  31836. }
  31837. if (rsaCaCertBuf != NULL) {
  31838. if (*rsaCaCertBufSz < (word32)sizeof_ca_cert_der_2048)
  31839. return -12115;
  31840. XMEMCPY(rsaCaCertBuf, ca_cert_der_2048, sizeof_ca_cert_der_2048);
  31841. *rsaCaCertBufSz = sizeof_ca_cert_der_2048;
  31842. }
  31843. #else
  31844. certFile = XFOPEN(clientCert, "rb");
  31845. if (!certFile)
  31846. return -12116;
  31847. *rsaClientCertBufSz = (word32)XFREAD(rsaClientCertBuf, 1,
  31848. *rsaClientCertBufSz, certFile);
  31849. XFCLOSE(certFile);
  31850. if (rsaServerCertBuf != NULL) {
  31851. certFile = XFOPEN(rsaServerCertDerFile, "rb");
  31852. if (!certFile)
  31853. return -12117;
  31854. *rsaServerCertBufSz = (word32)XFREAD(rsaServerCertBuf, 1,
  31855. *rsaServerCertBufSz, certFile);
  31856. XFCLOSE(certFile);
  31857. }
  31858. if (rsaCaCertBuf != NULL) {
  31859. certFile = XFOPEN(rsaCaCertDerFile, "rb");
  31860. if (!certFile)
  31861. return -12118;
  31862. *rsaCaCertBufSz = (word32)XFREAD(rsaCaCertBuf, 1, *rsaCaCertBufSz,
  31863. certFile);
  31864. XFCLOSE(certFile);
  31865. }
  31866. #endif
  31867. #ifdef USE_CERT_BUFFERS_1024
  31868. if (*rsaClientPrivKeyBufSz < (word32)sizeof_client_key_der_1024)
  31869. return -12119;
  31870. XMEMCPY(rsaClientPrivKeyBuf, client_key_der_1024,
  31871. sizeof_client_key_der_1024);
  31872. *rsaClientPrivKeyBufSz = sizeof_client_key_der_1024;
  31873. if (rsaServerPrivKeyBuf != NULL) {
  31874. if (*rsaServerPrivKeyBufSz < (word32)sizeof_server_key_der_1024)
  31875. return -12120;
  31876. XMEMCPY(rsaServerPrivKeyBuf, server_key_der_1024,
  31877. sizeof_server_key_der_1024);
  31878. *rsaServerPrivKeyBufSz = sizeof_server_key_der_1024;
  31879. }
  31880. if (rsaCaPrivKeyBuf != NULL) {
  31881. if (*rsaCaPrivKeyBufSz < (word32)sizeof_ca_key_der_1024)
  31882. return -12121;
  31883. XMEMCPY(rsaCaPrivKeyBuf, ca_key_der_1024, sizeof_ca_key_der_1024);
  31884. *rsaCaPrivKeyBufSz = sizeof_ca_key_der_1024;
  31885. }
  31886. #elif defined(USE_CERT_BUFFERS_2048)
  31887. if (*rsaClientPrivKeyBufSz < (word32)sizeof_client_key_der_2048)
  31888. return -12122;
  31889. XMEMCPY(rsaClientPrivKeyBuf, client_key_der_2048,
  31890. sizeof_client_key_der_2048);
  31891. *rsaClientPrivKeyBufSz = sizeof_client_key_der_2048;
  31892. if (rsaServerPrivKeyBuf != NULL) {
  31893. if (*rsaServerPrivKeyBufSz < (word32)sizeof_server_key_der_2048)
  31894. return -12123;
  31895. XMEMCPY(rsaServerPrivKeyBuf, server_key_der_2048,
  31896. sizeof_server_key_der_2048);
  31897. *rsaServerPrivKeyBufSz = sizeof_server_key_der_2048;
  31898. }
  31899. if (rsaCaPrivKeyBuf != NULL) {
  31900. if (*rsaCaPrivKeyBufSz < (word32)sizeof_ca_key_der_2048)
  31901. return -12124;
  31902. XMEMCPY(rsaCaPrivKeyBuf, ca_key_der_2048, sizeof_ca_key_der_2048);
  31903. *rsaCaPrivKeyBufSz = sizeof_ca_key_der_2048;
  31904. }
  31905. #else
  31906. keyFile = XFOPEN(clientKey, "rb");
  31907. if (!keyFile)
  31908. return -12125;
  31909. *rsaClientPrivKeyBufSz = (word32)XFREAD(rsaClientPrivKeyBuf, 1,
  31910. *rsaClientPrivKeyBufSz, keyFile);
  31911. XFCLOSE(keyFile);
  31912. if (rsaServerPrivKeyBuf != NULL) {
  31913. keyFile = XFOPEN(rsaServerKeyDerFile, "rb");
  31914. if (!keyFile)
  31915. return -12126;
  31916. *rsaServerPrivKeyBufSz = (word32)XFREAD(rsaServerPrivKeyBuf, 1,
  31917. *rsaServerPrivKeyBufSz, keyFile);
  31918. XFCLOSE(keyFile);
  31919. }
  31920. if (rsaCaPrivKeyBuf != NULL) {
  31921. keyFile = XFOPEN(rsaCaKeyFile, "rb");
  31922. if (!keyFile)
  31923. return -12127;
  31924. *rsaCaPrivKeyBufSz = (word32)XFREAD(rsaCaPrivKeyBuf, 1,
  31925. *rsaCaPrivKeyBufSz, keyFile);
  31926. XFCLOSE(keyFile);
  31927. }
  31928. #endif /* USE_CERT_BUFFERS */
  31929. #endif /* NO_RSA */
  31930. /* ECC */
  31931. #ifdef HAVE_ECC
  31932. #ifdef USE_CERT_BUFFERS_256
  31933. if (*eccClientCertBufSz < (word32)sizeof_cliecc_cert_der_256)
  31934. return -12128;
  31935. XMEMCPY(eccClientCertBuf, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  31936. *eccClientCertBufSz = sizeof_cliecc_cert_der_256;
  31937. #else
  31938. certFile = XFOPEN(eccClientCert, "rb");
  31939. if (!certFile)
  31940. return -12129;
  31941. *eccClientCertBufSz = (word32)XFREAD(eccClientCertBuf, 1,
  31942. *eccClientCertBufSz, certFile);
  31943. XFCLOSE(certFile);
  31944. #endif /* USE_CERT_BUFFERS_256 */
  31945. #ifdef USE_CERT_BUFFERS_256
  31946. if (*eccClientPrivKeyBufSz < (word32)sizeof_ecc_clikey_der_256)
  31947. return -12130;
  31948. XMEMCPY(eccClientPrivKeyBuf, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  31949. *eccClientPrivKeyBufSz = sizeof_ecc_clikey_der_256;
  31950. #else
  31951. keyFile = XFOPEN(eccClientKey, "rb");
  31952. if (!keyFile)
  31953. return -12131;
  31954. *eccClientPrivKeyBufSz = (word32)XFREAD(eccClientPrivKeyBuf, 1,
  31955. *eccClientPrivKeyBufSz, keyFile);
  31956. XFCLOSE(keyFile);
  31957. #endif /* USE_CERT_BUFFERS_256 */
  31958. #endif /* HAVE_ECC */
  31959. #ifdef NO_RSA
  31960. (void)rsaClientCertBuf;
  31961. (void)rsaClientCertBufSz;
  31962. (void)rsaClientPrivKeyBuf;
  31963. (void)rsaClientPrivKeyBufSz;
  31964. (void)rsaServerCertBuf;
  31965. (void)rsaServerCertBufSz;
  31966. (void)rsaServerPrivKeyBuf;
  31967. (void)rsaServerPrivKeyBufSz;
  31968. (void)rsaCaCertBuf;
  31969. (void)rsaCaCertBufSz;
  31970. (void)rsaCaPrivKeyBuf;
  31971. (void)rsaCaPrivKeyBufSz;
  31972. #endif
  31973. #ifndef HAVE_ECC
  31974. (void)eccClientCertBuf;
  31975. (void)eccClientCertBufSz;
  31976. (void)eccClientPrivKeyBuf;
  31977. (void)eccClientPrivKeyBufSz;
  31978. #endif
  31979. #ifndef NO_FILESYSTEM
  31980. (void)certFile;
  31981. (void)keyFile;
  31982. #endif
  31983. return 0;
  31984. }
  31985. typedef struct {
  31986. const byte* content;
  31987. word32 contentSz;
  31988. int contentOID;
  31989. int encryptOID;
  31990. int keyWrapOID;
  31991. int keyAgreeOID;
  31992. byte* cert;
  31993. size_t certSz;
  31994. byte* privateKey;
  31995. word32 privateKeySz;
  31996. byte* optionalUkm;
  31997. word32 optionalUkmSz;
  31998. int ktriOptions; /* KTRI options flags */
  31999. int kariOptions; /* KARI options flags */
  32000. /* KEKRI specific */
  32001. const byte* secretKey; /* key, only for kekri RecipientInfo types */
  32002. word32 secretKeySz; /* size of secretKey, bytes */
  32003. const byte* secretKeyId; /* key identifier */
  32004. word32 secretKeyIdSz; /* size of key identifier, bytes */
  32005. void* timePtr; /* time_t pointer */
  32006. byte* otherAttrOID; /* OPTIONAL, other attribute OID */
  32007. word32 otherAttrOIDSz; /* size of otherAttrOID, bytes */
  32008. byte* otherAttr; /* OPTIONAL, other attribute, ASN.1 encoded */
  32009. word32 otherAttrSz; /* size of otherAttr, bytes */
  32010. int kekriOptions; /* KEKRI options flags */
  32011. /* PWRI specific */
  32012. const char* password;
  32013. word32 passwordSz;
  32014. const byte* salt;
  32015. word32 saltSz;
  32016. int kdfOID;
  32017. int hashOID;
  32018. int kdfIterations;
  32019. int pwriOptions; /* PWRI options flags */
  32020. /* ORI specific */
  32021. int isOri;
  32022. int oriOptions; /* ORI options flags */
  32023. const char* outFileName;
  32024. } pkcs7EnvelopedVector;
  32025. static const byte asnDataOid[] = {
  32026. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x07, 0x01
  32027. };
  32028. /* ORI encrypt callback, responsible for encrypting content-encryption key (CEK)
  32029. * and giving wolfCrypt the value for oriOID and oriValue to place in
  32030. * OtherRecipientInfo.
  32031. *
  32032. * Returns 0 on success, negative upon error. */
  32033. static int myOriEncryptCb(PKCS7* pkcs7, byte* cek, word32 cekSz, byte* oriType,
  32034. word32* oriTypeSz, byte* oriValue, word32* oriValueSz,
  32035. void* ctx)
  32036. {
  32037. int i;
  32038. /* make sure buffers are large enough */
  32039. if ((*oriValueSz < (2 + cekSz)) || (*oriTypeSz < sizeof(oriType)))
  32040. return -12140;
  32041. /* our simple encryption algorithm will be take the bitwise complement */
  32042. oriValue[0] = 0x04; /*ASN OCTET STRING */
  32043. oriValue[1] = (byte)cekSz; /* length */
  32044. for (i = 0; i < (int)cekSz; i++) {
  32045. oriValue[2 + i] = ~cek[i];
  32046. }
  32047. *oriValueSz = 2 + cekSz;
  32048. /* set oriType to ASN.1 encoded data OID */
  32049. XMEMCPY(oriType, asnDataOid, sizeof(asnDataOid));
  32050. *oriTypeSz = sizeof(asnDataOid);
  32051. (void)pkcs7;
  32052. (void)ctx;
  32053. return 0;
  32054. }
  32055. /* ORI decrypt callback, responsible for providing a decrypted content
  32056. * encryption key (CEK) placed into decryptedKey and size placed into
  32057. * decryptedKeySz. oriOID and oriValue are given to the callback to help
  32058. * in decrypting the encrypted CEK.
  32059. *
  32060. * Returns 0 on success, negative upon error. */
  32061. static int myOriDecryptCb(PKCS7* pkcs7, byte* oriType, word32 oriTypeSz,
  32062. byte* oriValue, word32 oriValueSz, byte* decryptedKey,
  32063. word32* decryptedKeySz, void* ctx)
  32064. {
  32065. int i;
  32066. /* make sure oriType matches what we expect */
  32067. if (oriTypeSz != sizeof(asnDataOid))
  32068. return -12150;
  32069. if (XMEMCMP(oriType, asnDataOid, sizeof(asnDataOid)) != 0)
  32070. return -12151;
  32071. /* make sure decrypted buffer is large enough */
  32072. if (*decryptedKeySz < oriValueSz)
  32073. return -12152;
  32074. /* decrypt encrypted CEK using simple bitwise complement,
  32075. only for example */
  32076. for (i = 0; i < (int)oriValueSz - 2; i++) {
  32077. decryptedKey[i] = ~oriValue[2 + i];
  32078. }
  32079. *decryptedKeySz = oriValueSz - 2;
  32080. (void)pkcs7;
  32081. (void)ctx;
  32082. return 0;
  32083. }
  32084. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  32085. /* returns 0 on success */
  32086. static int myDecryptionFunc(PKCS7* pkcs7, int encryptOID, byte* iv, int ivSz,
  32087. byte* aad, word32 aadSz, byte* authTag, word32 authTagSz,
  32088. byte* in, int inSz, byte* out, void* usrCtx)
  32089. {
  32090. int keyId = -1, ret, keySz;
  32091. word32 keyIdSz = 8;
  32092. const byte* key;
  32093. byte keyIdRaw[8];
  32094. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  32095. Aes *aes;
  32096. #else
  32097. Aes aes[1];
  32098. #endif
  32099. /* looking for KEY ID
  32100. * fwDecryptKeyID OID "1.2.840.113549.1.9.16.2.37
  32101. */
  32102. WOLFSSL_SMALL_STACK_STATIC const unsigned char OID[] = {
  32103. /* 0x06, 0x0B do not pass in tag and length */
  32104. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  32105. 0x01, 0x09, 0x10, 0x02, 0x25
  32106. };
  32107. WOLFSSL_SMALL_STACK_STATIC const byte defKey[] = {
  32108. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  32109. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  32110. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  32111. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  32112. };
  32113. WOLFSSL_SMALL_STACK_STATIC const byte altKey[] = {
  32114. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  32115. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  32116. };
  32117. /* test user context passed in */
  32118. if (usrCtx == NULL || *(int*)usrCtx != 1) {
  32119. return -12160;
  32120. }
  32121. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  32122. if ((aes = (Aes *)XMALLOC(sizeof *aes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER)) == NULL)
  32123. return -12164;
  32124. #endif
  32125. /* if needing to find keyIdSz can call with NULL */
  32126. ret = wc_PKCS7_GetAttributeValue(pkcs7, OID, sizeof(OID), NULL,
  32127. &keyIdSz);
  32128. if (ret != LENGTH_ONLY_E) {
  32129. printf("Unexpected error %d when getting keyIdSz\n", ret);
  32130. printf("Possibly no KEY ID attribute set\n");
  32131. ERROR_OUT(-12161, out);
  32132. }
  32133. else {
  32134. XMEMSET(keyIdRaw, 0, sizeof(keyIdRaw));
  32135. ret = wc_PKCS7_GetAttributeValue(pkcs7, OID, sizeof(OID), keyIdRaw,
  32136. &keyIdSz);
  32137. if (ret < 0) {
  32138. goto out;
  32139. }
  32140. if (keyIdSz < 3) {
  32141. printf("keyIdSz is smaller than expected\n");
  32142. ERROR_OUT(-12162, out);
  32143. }
  32144. if (keyIdSz > 2 + sizeof(int)) {
  32145. printf("example case was only expecting a keyId of int size\n");
  32146. ERROR_OUT(-12163, out);
  32147. }
  32148. /* keyIdRaw[0] OCTET TAG */
  32149. /* keyIdRaw[1] Length */
  32150. #ifdef BIG_ENDIAN_ORDER
  32151. if (keyIdRaw[1] == 0x01) {
  32152. keyId = 1;
  32153. }
  32154. #else
  32155. XMEMCPY(&keyId, keyIdRaw + 2, sizeof(keyId));
  32156. #endif
  32157. }
  32158. /* Use keyID here if found to select key and decrypt in HSM or in this
  32159. * example just select key and do software decryption */
  32160. if (keyId == 1) {
  32161. key = altKey;
  32162. keySz = sizeof(altKey);
  32163. }
  32164. else {
  32165. key = defKey;
  32166. keySz = sizeof(defKey);
  32167. }
  32168. switch (encryptOID) {
  32169. #ifdef WOLFSSL_AES_256
  32170. case AES256CBCb:
  32171. if ((keySz != 32 ) || (ivSz != AES_BLOCK_SIZE))
  32172. ERROR_OUT(BAD_FUNC_ARG, out);
  32173. break;
  32174. #endif
  32175. #ifdef WOLFSSL_AES_128
  32176. case AES128CBCb:
  32177. if ((keySz != 16 ) || (ivSz != AES_BLOCK_SIZE))
  32178. ERROR_OUT(BAD_FUNC_ARG, out);
  32179. break;
  32180. #endif
  32181. default:
  32182. printf("Unsupported content cipher type for example");
  32183. ERROR_OUT(ALGO_ID_E, out);
  32184. };
  32185. ret = wc_AesInit(aes, HEAP_HINT, INVALID_DEVID);
  32186. if (ret == 0) {
  32187. ret = wc_AesSetKey(aes, key, keySz, iv, AES_DECRYPTION);
  32188. if (ret == 0)
  32189. ret = wc_AesCbcDecrypt(aes, out, in, inSz);
  32190. wc_AesFree(aes);
  32191. }
  32192. out:
  32193. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  32194. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32195. #endif
  32196. (void)aad;
  32197. (void)aadSz;
  32198. (void)authTag;
  32199. (void)authTagSz;
  32200. return ret;
  32201. }
  32202. #endif /* !NO_AES && HAVE_AES_CBC */
  32203. #define PKCS7_BUF_SIZE 2048
  32204. static int pkcs7enveloped_run_vectors(byte* rsaCert, word32 rsaCertSz,
  32205. byte* rsaPrivKey, word32 rsaPrivKeySz,
  32206. byte* eccCert, word32 eccCertSz,
  32207. byte* eccPrivKey, word32 eccPrivKeySz)
  32208. {
  32209. int ret = 0, testSz = 0, i;
  32210. int envelopedSz, decodedSz;
  32211. byte *enveloped = NULL;
  32212. byte *decoded = NULL;
  32213. PKCS7* pkcs7 = NULL;
  32214. #ifdef ECC_TIMING_RESISTANT
  32215. WC_RNG rng;
  32216. #endif
  32217. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  32218. XFILE pkcs7File;
  32219. #endif
  32220. WOLFSSL_SMALL_STACK_STATIC const byte data[] = { /* Hello World */
  32221. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  32222. 0x72,0x6c,0x64
  32223. };
  32224. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_256) && \
  32225. defined(HAVE_ECC) && defined(WOLFSSL_SHA512)
  32226. byte optionalUkm[] = {
  32227. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07
  32228. };
  32229. #endif /* NO_AES */
  32230. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128) && \
  32231. !defined(NO_SHA)
  32232. /* encryption key for kekri recipient types */
  32233. WOLFSSL_SMALL_STACK_STATIC const byte secretKey[] = {
  32234. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  32235. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07
  32236. };
  32237. /* encryption key identifier */
  32238. WOLFSSL_SMALL_STACK_STATIC const byte secretKeyId[] = {
  32239. 0x02,0x02,0x03,0x04
  32240. };
  32241. #endif
  32242. #if !defined(NO_PWDBASED) && !defined(NO_SHA) && \
  32243. !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  32244. #ifndef HAVE_FIPS
  32245. WOLFSSL_SMALL_STACK_STATIC const char password[] = "password"; /* NOTE: Password is too short for FIPS */
  32246. #else
  32247. WOLFSSL_SMALL_STACK_STATIC const char password[] = "passwordFIPS_MODE";
  32248. #endif
  32249. WOLFSSL_SMALL_STACK_STATIC const byte salt[] = {
  32250. 0x12, 0x34, 0x56, 0x78, 0x78, 0x56, 0x34, 0x12
  32251. };
  32252. #endif
  32253. #define MAX_TESTVECTORS_LEN 13
  32254. #define ADD_PKCS7ENVELOPEDVECTOR(...) { \
  32255. pkcs7EnvelopedVector _this_vector = { __VA_ARGS__ }; \
  32256. if (testSz == MAX_TESTVECTORS_LEN) { \
  32257. ret = -12534; \
  32258. goto out; \
  32259. } \
  32260. XMEMCPY(&testVectors[testSz++], &_this_vector, sizeof _this_vector);\
  32261. }
  32262. pkcs7EnvelopedVector *testVectors = NULL;
  32263. #ifdef ECC_TIMING_RESISTANT
  32264. XMEMSET(&rng, 0, sizeof(rng));
  32265. #endif
  32266. testVectors = (pkcs7EnvelopedVector *)XMALLOC(MAX_TESTVECTORS_LEN * sizeof(*testVectors),
  32267. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32268. if (testVectors == NULL) {
  32269. ret = -12534;
  32270. goto out;
  32271. }
  32272. {
  32273. /* key transport key encryption technique */
  32274. #ifndef NO_RSA
  32275. #ifndef NO_DES3
  32276. ADD_PKCS7ENVELOPEDVECTOR(
  32277. data, (word32)sizeof(data), DATA, DES3b, 0, 0, rsaCert, rsaCertSz,
  32278. rsaPrivKey, rsaPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL,
  32279. 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  32280. "pkcs7envelopedDataDES3.der");
  32281. #endif
  32282. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  32283. #ifdef WOLFSSL_AES_128
  32284. ADD_PKCS7ENVELOPEDVECTOR(
  32285. data, (word32)sizeof(data), DATA, AES128CBCb, 0, 0, rsaCert, rsaCertSz,
  32286. rsaPrivKey, rsaPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL,
  32287. 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  32288. "pkcs7envelopedDataAES128CBC.der");
  32289. #endif
  32290. #ifdef WOLFSSL_AES_192
  32291. ADD_PKCS7ENVELOPEDVECTOR(
  32292. data, (word32)sizeof(data), DATA, AES192CBCb, 0, 0, rsaCert, rsaCertSz,
  32293. rsaPrivKey, rsaPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL,
  32294. 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  32295. "pkcs7envelopedDataAES192CBC.der");
  32296. #endif
  32297. #ifdef WOLFSSL_AES_256
  32298. ADD_PKCS7ENVELOPEDVECTOR(
  32299. data, (word32)sizeof(data), DATA, AES256CBCb, 0, 0, rsaCert, rsaCertSz,
  32300. rsaPrivKey, rsaPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL,
  32301. 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  32302. "pkcs7envelopedDataAES256CBC.der");
  32303. /* explicitly using SKID for SubjectKeyIdentifier */
  32304. ADD_PKCS7ENVELOPEDVECTOR(
  32305. data, (word32)sizeof(data), DATA, AES256CBCb, 0, 0, rsaCert, rsaCertSz,
  32306. rsaPrivKey, rsaPrivKeySz, NULL, 0, CMS_SKID, 0, NULL, 0, NULL, 0, NULL,
  32307. NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  32308. "pkcs7envelopedDataAES256CBC_SKID.der");
  32309. /* explicitly using IssuerAndSerialNumber for SubjectKeyIdentifier */
  32310. ADD_PKCS7ENVELOPEDVECTOR(
  32311. data, (word32)sizeof(data), DATA, AES256CBCb, 0, 0, rsaCert, rsaCertSz,
  32312. rsaPrivKey, rsaPrivKeySz, NULL, 0, CMS_ISSUER_AND_SERIAL_NUMBER, 0,
  32313. NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0,
  32314. 0, 0, 0, 0, "pkcs7envelopedDataAES256CBC_IANDS.der");
  32315. #endif
  32316. #endif /* !NO_AES && HAVE_AES_CBC */
  32317. #endif
  32318. /* key agreement key encryption technique*/
  32319. #ifdef HAVE_ECC
  32320. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  32321. #if !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  32322. ADD_PKCS7ENVELOPEDVECTOR(
  32323. data, (word32)sizeof(data), DATA, AES128CBCb, AES128_WRAP,
  32324. dhSinglePass_stdDH_sha1kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  32325. eccPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0,
  32326. 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  32327. "pkcs7envelopedDataAES128CBC_ECDH_SHA1KDF.der");
  32328. #endif
  32329. #if !defined(NO_SHA256) && defined(WOLFSSL_AES_256)
  32330. ADD_PKCS7ENVELOPEDVECTOR(
  32331. data, (word32)sizeof(data), DATA, AES256CBCb, AES256_WRAP,
  32332. dhSinglePass_stdDH_sha256kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  32333. eccPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0,
  32334. 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  32335. "pkcs7envelopedDataAES256CBC_ECDH_SHA256KDF.der");
  32336. #endif /* NO_SHA256 && WOLFSSL_AES_256 */
  32337. #if defined(WOLFSSL_SHA512) && defined(WOLFSSL_AES_256)
  32338. ADD_PKCS7ENVELOPEDVECTOR(
  32339. data, (word32)sizeof(data), DATA, AES256CBCb, AES256_WRAP,
  32340. dhSinglePass_stdDH_sha512kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  32341. eccPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0,
  32342. 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  32343. "pkcs7envelopedDataAES256CBC_ECDH_SHA512KDF.der");
  32344. /* with optional user keying material (ukm) */
  32345. ADD_PKCS7ENVELOPEDVECTOR(
  32346. data, (word32)sizeof(data), DATA, AES256CBCb, AES256_WRAP,
  32347. dhSinglePass_stdDH_sha512kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  32348. eccPrivKeySz, optionalUkm, sizeof(optionalUkm), 0, 0, NULL, 0,
  32349. NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  32350. "pkcs7envelopedDataAES256CBC_ECDH_SHA512KDF_ukm.der");
  32351. #endif /* WOLFSSL_SHA512 && WOLFSSL_AES_256 */
  32352. #endif /* !NO_AES && HAVE_AES_CBC */
  32353. #endif
  32354. /* kekri (KEKRecipientInfo) recipient types */
  32355. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  32356. #if !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  32357. ADD_PKCS7ENVELOPEDVECTOR(
  32358. data, (word32)sizeof(data), DATA, AES128CBCb, AES128_WRAP, 0,
  32359. NULL, 0, NULL, 0, NULL, 0, 0, 0, secretKey, sizeof(secretKey),
  32360. secretKeyId, sizeof(secretKeyId), NULL, NULL, 0, NULL, 0,
  32361. 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  32362. "pkcs7envelopedDataAES128CBC_KEKRI.der");
  32363. #endif
  32364. #endif /* !NO_AES && HAVE_AES_CBC */
  32365. /* pwri (PasswordRecipientInfo) recipient types */
  32366. #if !defined(NO_PWDBASED) && !defined(NO_AES) && defined(HAVE_AES_CBC)
  32367. #if !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  32368. ADD_PKCS7ENVELOPEDVECTOR(
  32369. data, (word32)sizeof(data), DATA, AES128CBCb, 0, 0,
  32370. NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0,
  32371. NULL, 0, NULL, NULL, 0, NULL, 0, 0, password,
  32372. (word32)XSTRLEN(password), salt, sizeof(salt), PBKDF2_OID, WC_SHA, 5,
  32373. 0, 0, 0, "pkcs7envelopedDataAES128CBC_PWRI.der");
  32374. #endif
  32375. #endif
  32376. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && !defined(NO_AES_128)
  32377. /* ori (OtherRecipientInfo) recipient types */
  32378. ADD_PKCS7ENVELOPEDVECTOR(
  32379. data, (word32)sizeof(data), DATA, AES128CBCb, 0, 0, NULL, 0, NULL, 0,
  32380. NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0,
  32381. NULL, 0, 0, 0, 0, 0, 1, 0, "pkcs7envelopedDataAES128CBC_ORI.der");
  32382. #endif
  32383. };
  32384. #undef MAX_TESTVECTORS_LEN
  32385. #undef ADD_PKCS7ENVELOPEDVECTOR
  32386. enveloped = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32387. decoded = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32388. if ((! enveloped) || (! decoded)) {
  32389. ERROR_OUT(-12170, out);
  32390. }
  32391. #ifdef ECC_TIMING_RESISTANT
  32392. #ifndef HAVE_FIPS
  32393. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  32394. #else
  32395. ret = wc_InitRng(&rng);
  32396. #endif
  32397. if (ret != 0) {
  32398. ERROR_OUT(-12171, out);
  32399. }
  32400. #endif
  32401. for (i = 0; i < testSz; i++) {
  32402. pkcs7 = wc_PKCS7_New(HEAP_HINT,
  32403. #ifdef WOLFSSL_ASYNC_CRYPT
  32404. INVALID_DEVID /* async PKCS7 is not supported */
  32405. #else
  32406. devId
  32407. #endif
  32408. );
  32409. if (pkcs7 == NULL) {
  32410. ERROR_OUT(-12172, out);
  32411. }
  32412. if (testVectors[i].secretKey != NULL) {
  32413. /* KEKRI recipient type */
  32414. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  32415. if (ret != 0) {
  32416. ERROR_OUT(-12173, out);
  32417. }
  32418. pkcs7->content = (byte*)testVectors[i].content;
  32419. pkcs7->contentSz = testVectors[i].contentSz;
  32420. pkcs7->contentOID = testVectors[i].contentOID;
  32421. pkcs7->encryptOID = testVectors[i].encryptOID;
  32422. pkcs7->ukm = testVectors[i].optionalUkm;
  32423. pkcs7->ukmSz = testVectors[i].optionalUkmSz;
  32424. ret = wc_PKCS7_AddRecipient_KEKRI(pkcs7, testVectors[i].keyWrapOID,
  32425. (byte *)testVectors[i].secretKey, testVectors[i].secretKeySz,
  32426. (byte *)testVectors[i].secretKeyId, testVectors[i].secretKeyIdSz,
  32427. testVectors[i].timePtr, testVectors[i].otherAttrOID,
  32428. testVectors[i].otherAttrOIDSz, testVectors[i].otherAttr,
  32429. testVectors[i].otherAttrSz, testVectors[i].kekriOptions);
  32430. if (ret < 0) {
  32431. wc_PKCS7_Free(pkcs7);
  32432. ERROR_OUT(-12174, out);
  32433. }
  32434. /* set key, for decryption */
  32435. ret = wc_PKCS7_SetKey(pkcs7, (byte *)testVectors[i].secretKey,
  32436. testVectors[i].secretKeySz);
  32437. if (ret != 0) {
  32438. wc_PKCS7_Free(pkcs7);
  32439. ERROR_OUT(-12175, out);
  32440. }
  32441. } else if (testVectors[i].password != NULL) {
  32442. #if !defined(NO_PWDBASED) && !defined(NO_SHA)
  32443. /* PWRI recipient type */
  32444. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  32445. if (ret != 0) {
  32446. ERROR_OUT(-12176, out);
  32447. }
  32448. pkcs7->content = (byte*)testVectors[i].content;
  32449. pkcs7->contentSz = testVectors[i].contentSz;
  32450. pkcs7->contentOID = testVectors[i].contentOID;
  32451. pkcs7->encryptOID = testVectors[i].encryptOID;
  32452. pkcs7->ukm = testVectors[i].optionalUkm;
  32453. pkcs7->ukmSz = testVectors[i].optionalUkmSz;
  32454. ret = wc_PKCS7_AddRecipient_PWRI(pkcs7,
  32455. (byte *)testVectors[i].password, testVectors[i].passwordSz,
  32456. (byte *)testVectors[i].salt, testVectors[i].saltSz,
  32457. testVectors[i].kdfOID,
  32458. testVectors[i].hashOID, testVectors[i].kdfIterations,
  32459. testVectors[i].encryptOID, testVectors[i].pwriOptions);
  32460. if (ret < 0) {
  32461. wc_PKCS7_Free(pkcs7);
  32462. ERROR_OUT(-12177, out);
  32463. }
  32464. /* set password, for decryption */
  32465. ret = wc_PKCS7_SetPassword(pkcs7, (byte*)testVectors[i].password,
  32466. testVectors[i].passwordSz);
  32467. if (ret < 0) {
  32468. wc_PKCS7_Free(pkcs7);
  32469. ERROR_OUT(-12178, out);
  32470. }
  32471. #endif /* ! NO_PWDBASED && ! NO_SHA */
  32472. } else if (testVectors[i].isOri == 1) {
  32473. /* ORI recipient type */
  32474. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  32475. if (ret != 0) {
  32476. ERROR_OUT(-12179, out);
  32477. }
  32478. pkcs7->content = (byte*)testVectors[i].content;
  32479. pkcs7->contentSz = testVectors[i].contentSz;
  32480. pkcs7->contentOID = testVectors[i].contentOID;
  32481. pkcs7->encryptOID = testVectors[i].encryptOID;
  32482. ret = wc_PKCS7_AddRecipient_ORI(pkcs7, myOriEncryptCb,
  32483. testVectors[i].oriOptions);
  32484. if (ret < 0) {
  32485. wc_PKCS7_Free(pkcs7);
  32486. ERROR_OUT(-12180, out);
  32487. }
  32488. /* set decrypt callback for decryption */
  32489. ret = wc_PKCS7_SetOriDecryptCb(pkcs7, myOriDecryptCb);
  32490. if (ret < 0) {
  32491. wc_PKCS7_Free(pkcs7);
  32492. ERROR_OUT(-12181, out);
  32493. }
  32494. } else {
  32495. /* KTRI or KARI recipient types */
  32496. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  32497. if (ret != 0) {
  32498. ERROR_OUT(-12182, out);
  32499. }
  32500. ret = wc_PKCS7_InitWithCert(pkcs7, testVectors[i].cert,
  32501. (word32)testVectors[i].certSz);
  32502. if (ret != 0) {
  32503. wc_PKCS7_Free(pkcs7);
  32504. ERROR_OUT(-12183, out);
  32505. }
  32506. pkcs7->keyWrapOID = testVectors[i].keyWrapOID;
  32507. pkcs7->keyAgreeOID = testVectors[i].keyAgreeOID;
  32508. pkcs7->privateKey = testVectors[i].privateKey;
  32509. pkcs7->privateKeySz = testVectors[i].privateKeySz;
  32510. pkcs7->content = (byte*)testVectors[i].content;
  32511. pkcs7->contentSz = testVectors[i].contentSz;
  32512. pkcs7->contentOID = testVectors[i].contentOID;
  32513. pkcs7->encryptOID = testVectors[i].encryptOID;
  32514. pkcs7->ukm = testVectors[i].optionalUkm;
  32515. pkcs7->ukmSz = testVectors[i].optionalUkmSz;
  32516. /* set SubjectIdentifier type for KTRI types */
  32517. if (testVectors[i].ktriOptions & CMS_SKID) {
  32518. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID);
  32519. if (ret != 0) {
  32520. wc_PKCS7_Free(pkcs7);
  32521. ERROR_OUT(-12184, out);
  32522. }
  32523. } else if (testVectors[i].ktriOptions &
  32524. CMS_ISSUER_AND_SERIAL_NUMBER) {
  32525. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7,
  32526. CMS_ISSUER_AND_SERIAL_NUMBER);
  32527. if (ret != 0) {
  32528. wc_PKCS7_Free(pkcs7);
  32529. ERROR_OUT(-12185, out);
  32530. }
  32531. }
  32532. }
  32533. #ifdef ECC_TIMING_RESISTANT
  32534. pkcs7->rng = &rng;
  32535. #endif
  32536. /* encode envelopedData */
  32537. envelopedSz = wc_PKCS7_EncodeEnvelopedData(pkcs7, enveloped,
  32538. PKCS7_BUF_SIZE);
  32539. if (envelopedSz <= 0) {
  32540. wc_PKCS7_Free(pkcs7);
  32541. ERROR_OUT(-12186, out);
  32542. }
  32543. /* decode envelopedData */
  32544. pkcs7->contentOID = 0;
  32545. decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, enveloped, envelopedSz,
  32546. decoded, PKCS7_BUF_SIZE);
  32547. if (pkcs7->contentOID != testVectors[i].contentOID ||
  32548. decodedSz <= 0) {
  32549. wc_PKCS7_Free(pkcs7);
  32550. ERROR_OUT(-12187, out);
  32551. }
  32552. /* test decode result */
  32553. if (XMEMCMP(decoded, data, sizeof(data)) != 0){
  32554. wc_PKCS7_Free(pkcs7);
  32555. ERROR_OUT(-12188, out);
  32556. }
  32557. #ifndef NO_PKCS7_STREAM
  32558. { /* test reading byte by byte */
  32559. int z;
  32560. for (z = 0; z < envelopedSz; z++) {
  32561. decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, enveloped + z, 1,
  32562. decoded, PKCS7_BUF_SIZE);
  32563. if (decodedSz <= 0 && decodedSz != WC_PKCS7_WANT_READ_E) {
  32564. printf("unexpected error %d\n", decodedSz);
  32565. ERROR_OUT(-12189, out);
  32566. }
  32567. }
  32568. /* test decode result */
  32569. if (XMEMCMP(decoded, data, sizeof(data)) != 0) {
  32570. printf("stream read compare failed\n");
  32571. wc_PKCS7_Free(pkcs7);
  32572. ERROR_OUT(-12190, out);
  32573. }
  32574. }
  32575. #endif
  32576. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  32577. /* output pkcs7 envelopedData for external testing */
  32578. pkcs7File = XFOPEN(testVectors[i].outFileName, "wb");
  32579. if (!pkcs7File) {
  32580. wc_PKCS7_Free(pkcs7);
  32581. ERROR_OUT(-12191, out);
  32582. }
  32583. ret = (int)XFWRITE(enveloped, 1, envelopedSz, pkcs7File);
  32584. XFCLOSE(pkcs7File);
  32585. if (ret != envelopedSz) {
  32586. wc_PKCS7_Free(pkcs7);
  32587. ERROR_OUT(-12192, out);
  32588. } else {
  32589. /* reset ret to 0 for success */
  32590. ret = 0;
  32591. }
  32592. #endif /* PKCS7_OUTPUT_TEST_BUNDLES */
  32593. wc_PKCS7_Free(pkcs7);
  32594. pkcs7 = NULL;
  32595. }
  32596. #ifdef ECC_TIMING_RESISTANT
  32597. wc_FreeRng(&rng);
  32598. #endif
  32599. (void)eccCert;
  32600. (void)eccCertSz;
  32601. (void)eccPrivKey;
  32602. (void)eccPrivKeySz;
  32603. (void)rsaCert;
  32604. (void)rsaCertSz;
  32605. (void)rsaPrivKey;
  32606. (void)rsaPrivKeySz;
  32607. out:
  32608. if (testVectors)
  32609. XFREE(testVectors, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32610. if (enveloped)
  32611. XFREE(enveloped, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32612. if (decoded)
  32613. XFREE(decoded, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32614. return ret;
  32615. }
  32616. WOLFSSL_TEST_SUBROUTINE int pkcs7enveloped_test(void)
  32617. {
  32618. int ret = 0;
  32619. byte* rsaCert = NULL;
  32620. byte* rsaPrivKey = NULL;
  32621. word32 rsaCertSz = 0;
  32622. word32 rsaPrivKeySz = 0;
  32623. byte* eccCert = NULL;
  32624. byte* eccPrivKey = NULL;
  32625. word32 eccCertSz = 0;
  32626. word32 eccPrivKeySz = 0;
  32627. #ifndef NO_RSA
  32628. /* read client RSA cert and key in DER format */
  32629. rsaCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32630. if (rsaCert == NULL)
  32631. return -12200;
  32632. rsaPrivKey = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32633. if (rsaPrivKey == NULL) {
  32634. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32635. return -12201;
  32636. }
  32637. rsaCertSz = FOURK_BUF;
  32638. rsaPrivKeySz = FOURK_BUF;
  32639. #endif /* NO_RSA */
  32640. #ifdef HAVE_ECC
  32641. /* read client ECC cert and key in DER format */
  32642. eccCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32643. if (eccCert == NULL) {
  32644. #ifndef NO_RSA
  32645. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32646. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32647. #endif
  32648. return -12202;
  32649. }
  32650. eccPrivKey =(byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32651. if (eccPrivKey == NULL) {
  32652. #ifndef NO_RSA
  32653. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32654. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32655. #endif
  32656. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32657. return -12203;
  32658. }
  32659. eccCertSz = FOURK_BUF;
  32660. eccPrivKeySz = FOURK_BUF;
  32661. #endif /* HAVE_ECC */
  32662. ret = pkcs7_load_certs_keys(rsaCert, &rsaCertSz, rsaPrivKey,
  32663. &rsaPrivKeySz, NULL, NULL, NULL, NULL,
  32664. NULL, NULL, NULL, NULL, eccCert, &eccCertSz,
  32665. eccPrivKey, &eccPrivKeySz);
  32666. if (ret < 0) {
  32667. #ifndef NO_RSA
  32668. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32669. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32670. #endif
  32671. #ifdef HAVE_ECC
  32672. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32673. XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32674. #endif
  32675. return -12204;
  32676. }
  32677. ret = pkcs7enveloped_run_vectors(rsaCert, (word32)rsaCertSz,
  32678. rsaPrivKey, (word32)rsaPrivKeySz,
  32679. eccCert, (word32)eccCertSz,
  32680. eccPrivKey, (word32)eccPrivKeySz);
  32681. #ifndef NO_RSA
  32682. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32683. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32684. #endif
  32685. #ifdef HAVE_ECC
  32686. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32687. XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32688. #endif
  32689. return ret;
  32690. }
  32691. #if defined(HAVE_AESGCM) || defined(HAVE_AESCCM)
  32692. typedef struct {
  32693. const byte* content;
  32694. word32 contentSz;
  32695. int contentOID;
  32696. int encryptOID;
  32697. int keyWrapOID;
  32698. int keyAgreeOID;
  32699. byte* cert;
  32700. size_t certSz;
  32701. byte* privateKey;
  32702. word32 privateKeySz;
  32703. PKCS7Attrib* authAttribs;
  32704. word32 authAttribsSz;
  32705. PKCS7Attrib* unauthAttribs;
  32706. word32 unauthAttribsSz;
  32707. /* KARI / KTRI specific */
  32708. byte* optionalUkm;
  32709. word32 optionalUkmSz;
  32710. int ktriOptions; /* KTRI options flags */
  32711. int kariOptions; /* KARI options flags */
  32712. /* KEKRI specific */
  32713. byte* secretKey; /* key, only for kekri RecipientInfo types */
  32714. word32 secretKeySz; /* size of secretKey, bytes */
  32715. byte* secretKeyId; /* key identifier */
  32716. word32 secretKeyIdSz; /* size of key identifier, bytes */
  32717. void* timePtr; /* time_t pointer */
  32718. byte* otherAttrOID; /* OPTIONAL, other attribute OID */
  32719. word32 otherAttrOIDSz; /* size of otherAttrOID, bytes */
  32720. byte* otherAttr; /* OPTIONAL, other attribute, ASN.1 encoded */
  32721. word32 otherAttrSz; /* size of otherAttr, bytes */
  32722. int kekriOptions; /* KEKRI options flags */
  32723. /* PWRI specific */
  32724. char* password; /* password */
  32725. word32 passwordSz; /* password size, bytes */
  32726. byte* salt; /* KDF salt */
  32727. word32 saltSz; /* KDF salt size, bytes */
  32728. int kdfOID; /* KDF OID */
  32729. int hashOID; /* KDF hash algorithm OID */
  32730. int kdfIterations; /* KDF iterations */
  32731. int kekEncryptOID; /* KEK encryption algorithm OID */
  32732. int pwriOptions; /* PWRI options flags */
  32733. /* ORI specific */
  32734. int isOri;
  32735. int oriOptions; /* ORI options flags */
  32736. const char* outFileName;
  32737. } pkcs7AuthEnvelopedVector;
  32738. static int pkcs7authenveloped_run_vectors(byte* rsaCert, word32 rsaCertSz,
  32739. byte* rsaPrivKey, word32 rsaPrivKeySz,
  32740. byte* eccCert, word32 eccCertSz,
  32741. byte* eccPrivKey, word32 eccPrivKeySz)
  32742. {
  32743. int ret = 0, testSz = 0, i;
  32744. int envelopedSz, decodedSz;
  32745. byte *enveloped = NULL;
  32746. byte *decoded = NULL;
  32747. WC_RNG rng;
  32748. PKCS7* pkcs7;
  32749. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  32750. XFILE pkcs7File;
  32751. #endif
  32752. WOLFSSL_SMALL_STACK_STATIC const byte data[] = { /* Hello World */
  32753. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  32754. 0x72,0x6c,0x64
  32755. };
  32756. byte senderNonce[PKCS7_NONCE_SZ + 2];
  32757. #ifdef HAVE_ECC
  32758. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  32759. #if !defined(NO_SHA256) && defined(WOLFSSL_AES_256)
  32760. WOLFSSL_SMALL_STACK_STATIC const byte senderNonceOid[] =
  32761. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  32762. 0x09, 0x05 };
  32763. PKCS7Attrib attribs[] =
  32764. {
  32765. { senderNonceOid, sizeof(senderNonceOid), senderNonce,
  32766. sizeof(senderNonce) }
  32767. };
  32768. #endif
  32769. #endif
  32770. #endif
  32771. #if !defined(NO_AES) && defined(WOLFSSL_AES_256) && defined(HAVE_ECC) && \
  32772. defined(WOLFSSL_SHA512)
  32773. WOLFSSL_SMALL_STACK_STATIC const byte optionalUkm[] = {
  32774. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07
  32775. };
  32776. #endif /* NO_AES */
  32777. #if !defined(NO_AES) && !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  32778. /* encryption key for kekri recipient types */
  32779. WOLFSSL_SMALL_STACK_STATIC const byte secretKey[] = {
  32780. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  32781. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07
  32782. };
  32783. /* encryption key identifier */
  32784. WOLFSSL_SMALL_STACK_STATIC const byte secretKeyId[] = {
  32785. 0x02,0x02,0x03,0x04
  32786. };
  32787. #endif
  32788. #if !defined(NO_PWDBASED) && !defined(NO_AES) && defined(HAVE_AESGCM) && \
  32789. !defined(NO_SHA) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  32790. #ifndef HAVE_FIPS
  32791. WOLFSSL_SMALL_STACK_STATIC const char password[] = "password";
  32792. #else
  32793. WOLFSSL_SMALL_STACK_STATIC const char password[] = "passwordFIPS_MODE";
  32794. #endif
  32795. WOLFSSL_SMALL_STACK_STATIC const byte salt[] = {
  32796. 0x12, 0x34, 0x56, 0x78, 0x78, 0x56, 0x34, 0x12
  32797. };
  32798. #endif
  32799. #define MAX_TESTVECTORS_LEN 20
  32800. #define ADD_PKCS7AUTHENVELOPEDVECTOR(...) { \
  32801. pkcs7AuthEnvelopedVector _this_vector = { __VA_ARGS__ }; \
  32802. if (testSz == MAX_TESTVECTORS_LEN) { \
  32803. ret = -12534; \
  32804. goto out; \
  32805. } \
  32806. XMEMCPY(&testVectors[testSz++], &_this_vector, sizeof _this_vector);\
  32807. }
  32808. pkcs7AuthEnvelopedVector *testVectors = NULL;
  32809. XMEMSET(&rng, 0, sizeof(rng));
  32810. testVectors = (pkcs7AuthEnvelopedVector *)XMALLOC(MAX_TESTVECTORS_LEN * sizeof(*testVectors),
  32811. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32812. if (testVectors == NULL) {
  32813. ret = -12534;
  32814. goto out;
  32815. }
  32816. {
  32817. /* key transport key encryption technique */
  32818. #ifndef NO_RSA
  32819. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  32820. #ifdef WOLFSSL_AES_128
  32821. ADD_PKCS7AUTHENVELOPEDVECTOR(
  32822. data, (word32)sizeof(data), DATA, AES128GCMb, 0, 0, rsaCert, rsaCertSz,
  32823. rsaPrivKey, rsaPrivKeySz, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0,
  32824. NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0,
  32825. 0, 0, "pkcs7authEnvelopedDataAES128GCM.der");
  32826. #endif
  32827. #ifdef WOLFSSL_AES_192
  32828. ADD_PKCS7AUTHENVELOPEDVECTOR(
  32829. data, (word32)sizeof(data), DATA, AES192GCMb, 0, 0, rsaCert, rsaCertSz,
  32830. rsaPrivKey, rsaPrivKeySz, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0,
  32831. NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0,
  32832. 0, 0, "pkcs7authEnvelopedDataAES192GCM.der");
  32833. #endif
  32834. #ifdef WOLFSSL_AES_256
  32835. ADD_PKCS7AUTHENVELOPEDVECTOR(
  32836. data, (word32)sizeof(data), DATA, AES256GCMb, 0, 0, rsaCert, rsaCertSz,
  32837. rsaPrivKey, rsaPrivKeySz, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0,
  32838. NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0,
  32839. 0, 0, "pkcs7authEnvelopedDataAES256GCM.der");
  32840. /* test with contentType set to FirmwarePkgData */
  32841. ADD_PKCS7AUTHENVELOPEDVECTOR(
  32842. data, (word32)sizeof(data), FIRMWARE_PKG_DATA, AES256GCMb, 0, 0,
  32843. rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz, NULL, 0, NULL, 0, NULL,
  32844. 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL,
  32845. 0, 0, 0, 0, 0, 0, 0, 0,
  32846. "pkcs7authEnvelopedDataAES256GCM_firmwarePkgData.der");
  32847. /* explicitly using SKID for SubjectKeyIdentifier */
  32848. ADD_PKCS7AUTHENVELOPEDVECTOR(
  32849. data, (word32)sizeof(data), DATA, AES256GCMb, 0, 0, rsaCert, rsaCertSz,
  32850. rsaPrivKey, rsaPrivKeySz, NULL, 0, NULL, 0, NULL, 0, CMS_SKID, 0,
  32851. NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0,
  32852. 0, 0, 0, 0, 0, "pkcs7authEnvelopedDataAES256GCM_SKID.der");
  32853. /* explicitly using IssuerAndSerialNumber for SubjectKeyIdentifier */
  32854. ADD_PKCS7AUTHENVELOPEDVECTOR(
  32855. data, (word32)sizeof(data), DATA, AES256GCMb, 0, 0, rsaCert, rsaCertSz,
  32856. rsaPrivKey, rsaPrivKeySz, NULL, 0, NULL, 0, NULL, 0,
  32857. CMS_ISSUER_AND_SERIAL_NUMBER, 0, NULL, 0, NULL, 0, NULL, NULL, 0,
  32858. NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0, 0,
  32859. "pkcs7authEnvelopedDataAES256GCM_IANDS.der");
  32860. #endif
  32861. #endif /* NO_AES */
  32862. #endif
  32863. /* key agreement key encryption technique*/
  32864. #ifdef HAVE_ECC
  32865. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  32866. #if !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  32867. ADD_PKCS7AUTHENVELOPEDVECTOR(
  32868. data, (word32)sizeof(data), DATA, AES128GCMb, AES128_WRAP,
  32869. dhSinglePass_stdDH_sha1kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  32870. eccPrivKeySz, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0, NULL, 0,
  32871. NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0, 0,
  32872. "pkcs7authEnvelopedDataAES128GCM_ECDH_SHA1KDF.der");
  32873. #endif
  32874. #if !defined(NO_SHA256) && defined(WOLFSSL_AES_256)
  32875. ADD_PKCS7AUTHENVELOPEDVECTOR(
  32876. data, (word32)sizeof(data), DATA, AES256GCMb, AES256_WRAP,
  32877. dhSinglePass_stdDH_sha256kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  32878. eccPrivKeySz, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0, NULL, 0,
  32879. NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0, 0,
  32880. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA256KDF.der");
  32881. /* with authenticated attributes */
  32882. ADD_PKCS7AUTHENVELOPEDVECTOR(
  32883. data, (word32)sizeof(data), DATA, AES256GCMb, AES256_WRAP,
  32884. dhSinglePass_stdDH_sha256kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  32885. eccPrivKeySz, attribs, (sizeof(attribs) / sizeof(PKCS7Attrib)),
  32886. NULL, 0, NULL, 0, 0, 0, NULL, 0,
  32887. NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0,
  32888. 0, 0, 0,
  32889. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA256KDF_authAttribs.der");
  32890. /* with unauthenticated attributes */
  32891. ADD_PKCS7AUTHENVELOPEDVECTOR(
  32892. data, (word32)sizeof(data), DATA, AES256GCMb, AES256_WRAP,
  32893. dhSinglePass_stdDH_sha256kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  32894. eccPrivKeySz, NULL, 0, attribs,
  32895. (sizeof(attribs) / sizeof(PKCS7Attrib)), NULL, 0, 0, 0, NULL, 0,
  32896. NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0,
  32897. 0, 0, 0,
  32898. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA256KDF_unauthAttribs.der");
  32899. /* with authenticated AND unauthenticated attributes */
  32900. ADD_PKCS7AUTHENVELOPEDVECTOR(
  32901. data, (word32)sizeof(data), DATA, AES256GCMb, AES256_WRAP,
  32902. dhSinglePass_stdDH_sha256kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  32903. eccPrivKeySz, attribs, (sizeof(attribs) / sizeof(PKCS7Attrib)),
  32904. attribs, (sizeof(attribs) / sizeof(PKCS7Attrib)), NULL, 0, 0, 0,
  32905. NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0,
  32906. 0, 0, 0, 0, 0, 0,
  32907. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA256KDF_bothAttribs.der");
  32908. /* with authenticated AND unauthenticated attributes AND
  32909. * contentType of FirmwarePkgData */
  32910. ADD_PKCS7AUTHENVELOPEDVECTOR(
  32911. data, (word32)sizeof(data), FIRMWARE_PKG_DATA, AES256GCMb, AES256_WRAP,
  32912. dhSinglePass_stdDH_sha256kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  32913. eccPrivKeySz, attribs, (sizeof(attribs) / sizeof(PKCS7Attrib)),
  32914. attribs, (sizeof(attribs) / sizeof(PKCS7Attrib)), NULL, 0, 0, 0,
  32915. NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0,
  32916. 0, 0, 0, 0, 0, 0,
  32917. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA256KDF_fw_bothAttribs.der");
  32918. #endif /* NO_SHA256 && WOLFSSL_AES_256 */
  32919. #if defined(WOLFSSL_SHA512) && defined(WOLFSSL_AES_256)
  32920. ADD_PKCS7AUTHENVELOPEDVECTOR(
  32921. data, (word32)sizeof(data), DATA, AES256GCMb, AES256_WRAP,
  32922. dhSinglePass_stdDH_sha512kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  32923. eccPrivKeySz, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL,
  32924. NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0, 0,
  32925. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA512KDF.der");
  32926. /* with optional user keying material (ukm) */
  32927. ADD_PKCS7AUTHENVELOPEDVECTOR(
  32928. data, (word32)sizeof(data), DATA, AES256GCMb, AES256_WRAP,
  32929. dhSinglePass_stdDH_sha512kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  32930. eccPrivKeySz, NULL, 0, NULL, 0, (byte *)optionalUkm, sizeof(optionalUkm), 0,
  32931. 0, NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0,
  32932. 0, 0, 0, 0, 0, 0,
  32933. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA512KDF_ukm.der");
  32934. #endif /* WOLFSSL_SHA512 && WOLFSSL_AES_256 */
  32935. #endif /* NO_AES */
  32936. #endif
  32937. /* kekri (KEKRecipientInfo) recipient types */
  32938. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  32939. #if !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  32940. ADD_PKCS7AUTHENVELOPEDVECTOR(
  32941. data, (word32)sizeof(data), DATA, AES128GCMb, AES128_WRAP, 0,
  32942. NULL, 0, NULL, 0, NULL, 0, NULL, 0, NULL, 0, 0, 0,
  32943. (byte *)secretKey, sizeof(secretKey), (byte *)secretKeyId, sizeof(secretKeyId),
  32944. NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0, 0,
  32945. "pkcs7authEnvelopedDataAES128GCM_KEKRI.der");
  32946. #endif
  32947. #endif
  32948. /* pwri (PasswordRecipientInfo) recipient types */
  32949. #if !defined(NO_PWDBASED) && !defined(NO_AES) && defined(HAVE_AESGCM)
  32950. #if !defined(NO_SHA) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  32951. ADD_PKCS7AUTHENVELOPEDVECTOR(
  32952. data, (word32)sizeof(data), DATA, AES128GCMb, 0, 0,
  32953. NULL, 0, NULL, 0, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0,
  32954. NULL, 0, NULL, NULL, 0, NULL, 0, 0, (char *)password,
  32955. (word32)XSTRLEN(password), (byte *)salt, sizeof(salt), PBKDF2_OID, WC_SHA, 5,
  32956. AES128CBCb, 0, 0, 0, "pkcs7authEnvelopedDataAES128GCM_PWRI.der");
  32957. #endif
  32958. #endif
  32959. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  32960. #ifdef WOLFSSL_AES_128
  32961. /* ori (OtherRecipientInfo) recipient types */
  32962. ADD_PKCS7AUTHENVELOPEDVECTOR(
  32963. data, (word32)sizeof(data), DATA, AES128GCMb, 0, 0, NULL, 0, NULL, 0,
  32964. NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL, 0,
  32965. NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 1, 0,
  32966. "pkcs7authEnvelopedDataAES128GCM_ORI.der");
  32967. #endif
  32968. #endif
  32969. }
  32970. #undef MAX_TESTVECTORS_LEN
  32971. #undef ADD_PKCS7AUTHENVELOPEDVECTOR
  32972. enveloped = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32973. decoded = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32974. if ((! enveloped) || (! decoded)) {
  32975. ERROR_OUT(-12210, out);
  32976. }
  32977. /* generate senderNonce */
  32978. {
  32979. #ifndef HAVE_FIPS
  32980. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  32981. #else
  32982. ret = wc_InitRng(&rng);
  32983. #endif
  32984. if (ret != 0) {
  32985. ERROR_OUT(-12211, out);
  32986. }
  32987. senderNonce[0] = 0x04;
  32988. senderNonce[1] = PKCS7_NONCE_SZ;
  32989. ret = wc_RNG_GenerateBlock(&rng, &senderNonce[2], PKCS7_NONCE_SZ);
  32990. if (ret != 0) {
  32991. wc_FreeRng(&rng);
  32992. ERROR_OUT(-12212, out);
  32993. }
  32994. }
  32995. for (i = 0; i < testSz; i++) {
  32996. pkcs7 = wc_PKCS7_New(HEAP_HINT,
  32997. #ifdef WOLFSSL_ASYNC_CRYPT
  32998. INVALID_DEVID /* async PKCS7 is not supported */
  32999. #else
  33000. devId
  33001. #endif
  33002. );
  33003. if (pkcs7 == NULL) {
  33004. ERROR_OUT(-12213, out);
  33005. }
  33006. if (testVectors[i].secretKey != NULL) {
  33007. /* KEKRI recipient type */
  33008. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  33009. if (ret != 0) {
  33010. ERROR_OUT(-12214, out);
  33011. }
  33012. pkcs7->content = (byte*)testVectors[i].content;
  33013. pkcs7->contentSz = testVectors[i].contentSz;
  33014. pkcs7->contentOID = testVectors[i].contentOID;
  33015. pkcs7->encryptOID = testVectors[i].encryptOID;
  33016. pkcs7->ukm = testVectors[i].optionalUkm;
  33017. pkcs7->ukmSz = testVectors[i].optionalUkmSz;
  33018. pkcs7->authAttribs = testVectors[i].authAttribs;
  33019. pkcs7->authAttribsSz = testVectors[i].authAttribsSz;
  33020. pkcs7->unauthAttribs = testVectors[i].unauthAttribs;
  33021. pkcs7->unauthAttribsSz = testVectors[i].unauthAttribsSz;
  33022. ret = wc_PKCS7_AddRecipient_KEKRI(pkcs7, testVectors[i].keyWrapOID,
  33023. testVectors[i].secretKey, testVectors[i].secretKeySz,
  33024. testVectors[i].secretKeyId, testVectors[i].secretKeyIdSz,
  33025. testVectors[i].timePtr, testVectors[i].otherAttrOID,
  33026. testVectors[i].otherAttrOIDSz, testVectors[i].otherAttr,
  33027. testVectors[i].otherAttrSz, testVectors[i].kekriOptions);
  33028. if (ret < 0) {
  33029. wc_PKCS7_Free(pkcs7);
  33030. ERROR_OUT(-12215, out);
  33031. }
  33032. /* set key, for decryption */
  33033. ret = wc_PKCS7_SetKey(pkcs7, testVectors[i].secretKey,
  33034. testVectors[i].secretKeySz);
  33035. if (ret != 0) {
  33036. wc_PKCS7_Free(pkcs7);
  33037. ERROR_OUT(-12216, out);
  33038. }
  33039. } else if (testVectors[i].password != NULL) {
  33040. #if !defined(NO_PWDBASED) && !defined(NO_SHA)
  33041. /* PWRI recipient type */
  33042. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  33043. if (ret != 0) {
  33044. ERROR_OUT(-12217, out);
  33045. }
  33046. pkcs7->content = (byte*)testVectors[i].content;
  33047. pkcs7->contentSz = testVectors[i].contentSz;
  33048. pkcs7->contentOID = testVectors[i].contentOID;
  33049. pkcs7->encryptOID = testVectors[i].encryptOID;
  33050. pkcs7->ukm = testVectors[i].optionalUkm;
  33051. pkcs7->ukmSz = testVectors[i].optionalUkmSz;
  33052. pkcs7->authAttribs = testVectors[i].authAttribs;
  33053. pkcs7->authAttribsSz = testVectors[i].authAttribsSz;
  33054. pkcs7->unauthAttribs = testVectors[i].unauthAttribs;
  33055. pkcs7->unauthAttribsSz = testVectors[i].unauthAttribsSz;
  33056. ret = wc_PKCS7_AddRecipient_PWRI(pkcs7,
  33057. (byte*)testVectors[i].password,
  33058. testVectors[i].passwordSz, testVectors[i].salt,
  33059. testVectors[i].saltSz, testVectors[i].kdfOID,
  33060. testVectors[i].hashOID, testVectors[i].kdfIterations,
  33061. testVectors[i].kekEncryptOID, testVectors[i].pwriOptions);
  33062. if (ret < 0) {
  33063. wc_PKCS7_Free(pkcs7);
  33064. ERROR_OUT(-12218, out);
  33065. }
  33066. /* set password, for decryption */
  33067. ret = wc_PKCS7_SetPassword(pkcs7, (byte*)testVectors[i].password,
  33068. testVectors[i].passwordSz);
  33069. if (ret < 0) {
  33070. wc_PKCS7_Free(pkcs7);
  33071. ERROR_OUT(-12219, out);
  33072. }
  33073. #endif /* ! NO_PWDBASED && ! NO_SHA */
  33074. } else if (testVectors[i].isOri == 1) {
  33075. /* ORI recipient type */
  33076. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  33077. if (ret != 0) {
  33078. ERROR_OUT(-12220, out);
  33079. }
  33080. pkcs7->content = (byte*)testVectors[i].content;
  33081. pkcs7->contentSz = testVectors[i].contentSz;
  33082. pkcs7->contentOID = testVectors[i].contentOID;
  33083. pkcs7->encryptOID = testVectors[i].encryptOID;
  33084. pkcs7->authAttribs = testVectors[i].authAttribs;
  33085. pkcs7->authAttribsSz = testVectors[i].authAttribsSz;
  33086. pkcs7->unauthAttribs = testVectors[i].unauthAttribs;
  33087. pkcs7->unauthAttribsSz = testVectors[i].unauthAttribsSz;
  33088. ret = wc_PKCS7_AddRecipient_ORI(pkcs7, myOriEncryptCb,
  33089. testVectors[i].oriOptions);
  33090. if (ret < 0) {
  33091. wc_PKCS7_Free(pkcs7);
  33092. ERROR_OUT(-12221, out);
  33093. }
  33094. /* set decrypt callback for decryption */
  33095. ret = wc_PKCS7_SetOriDecryptCb(pkcs7, myOriDecryptCb);
  33096. if (ret < 0) {
  33097. wc_PKCS7_Free(pkcs7);
  33098. ERROR_OUT(-12222, out);
  33099. }
  33100. } else {
  33101. /* KTRI or KARI recipient types */
  33102. ret = wc_PKCS7_InitWithCert(pkcs7, testVectors[i].cert,
  33103. (word32)testVectors[i].certSz);
  33104. if (ret != 0) {
  33105. wc_PKCS7_Free(pkcs7);
  33106. ERROR_OUT(-12223, out);
  33107. }
  33108. pkcs7->keyWrapOID = testVectors[i].keyWrapOID;
  33109. pkcs7->keyAgreeOID = testVectors[i].keyAgreeOID;
  33110. pkcs7->privateKey = testVectors[i].privateKey;
  33111. pkcs7->privateKeySz = testVectors[i].privateKeySz;
  33112. pkcs7->content = (byte*)testVectors[i].content;
  33113. pkcs7->contentSz = testVectors[i].contentSz;
  33114. pkcs7->contentOID = testVectors[i].contentOID;
  33115. pkcs7->encryptOID = testVectors[i].encryptOID;
  33116. pkcs7->ukm = testVectors[i].optionalUkm;
  33117. pkcs7->ukmSz = testVectors[i].optionalUkmSz;
  33118. pkcs7->authAttribs = testVectors[i].authAttribs;
  33119. pkcs7->authAttribsSz = testVectors[i].authAttribsSz;
  33120. pkcs7->unauthAttribs = testVectors[i].unauthAttribs;
  33121. pkcs7->unauthAttribsSz = testVectors[i].unauthAttribsSz;
  33122. /* set SubjectIdentifier type for KTRI types */
  33123. if (testVectors[i].ktriOptions & CMS_SKID) {
  33124. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID);
  33125. if (ret != 0) {
  33126. wc_PKCS7_Free(pkcs7);
  33127. ERROR_OUT(-12224, out);
  33128. }
  33129. } else if (testVectors[i].ktriOptions &
  33130. CMS_ISSUER_AND_SERIAL_NUMBER) {
  33131. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7,
  33132. CMS_ISSUER_AND_SERIAL_NUMBER);
  33133. if (ret != 0) {
  33134. wc_PKCS7_Free(pkcs7);
  33135. ERROR_OUT(-12225, out);
  33136. }
  33137. }
  33138. }
  33139. #ifdef ECC_TIMING_RESISTANT
  33140. pkcs7->rng = &rng;
  33141. #endif
  33142. /* encode envelopedData */
  33143. envelopedSz = wc_PKCS7_EncodeAuthEnvelopedData(pkcs7, enveloped,
  33144. PKCS7_BUF_SIZE);
  33145. if (envelopedSz <= 0) {
  33146. wc_PKCS7_Free(pkcs7);
  33147. ERROR_OUT(-12226, out);
  33148. }
  33149. #ifndef NO_PKCS7_STREAM
  33150. { /* test reading byte by byte */
  33151. int z;
  33152. for (z = 0; z < envelopedSz; z++) {
  33153. decodedSz = wc_PKCS7_DecodeAuthEnvelopedData(pkcs7,
  33154. enveloped + z, 1, decoded, PKCS7_BUF_SIZE);
  33155. if (decodedSz <= 0 && decodedSz != WC_PKCS7_WANT_READ_E) {
  33156. printf("unexpected error %d\n", decodedSz);
  33157. ERROR_OUT(-12227, out);
  33158. }
  33159. }
  33160. /* test decode result */
  33161. if (XMEMCMP(decoded, data, sizeof(data)) != 0) {
  33162. printf("stream read compare failed\n");
  33163. wc_PKCS7_Free(pkcs7);
  33164. ERROR_OUT(-12228, out);
  33165. }
  33166. }
  33167. #endif
  33168. /* decode envelopedData */
  33169. decodedSz = wc_PKCS7_DecodeAuthEnvelopedData(pkcs7, enveloped,
  33170. envelopedSz, decoded,
  33171. PKCS7_BUF_SIZE);
  33172. if (decodedSz <= 0) {
  33173. wc_PKCS7_Free(pkcs7);
  33174. ERROR_OUT(-12229, out);
  33175. }
  33176. /* test decode result */
  33177. if (XMEMCMP(decoded, data, sizeof(data)) != 0){
  33178. wc_PKCS7_Free(pkcs7);
  33179. ERROR_OUT(-12230, out);
  33180. }
  33181. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  33182. /* output pkcs7 envelopedData for external testing */
  33183. pkcs7File = XFOPEN(testVectors[i].outFileName, "wb");
  33184. if (!pkcs7File) {
  33185. wc_PKCS7_Free(pkcs7);
  33186. ERROR_OUT(-12231, out);
  33187. }
  33188. ret = (int)XFWRITE(enveloped, 1, envelopedSz, pkcs7File);
  33189. XFCLOSE(pkcs7File);
  33190. if (ret != envelopedSz) {
  33191. wc_PKCS7_Free(pkcs7);
  33192. ERROR_OUT(-12232, out);
  33193. } else {
  33194. /* reset ret to 0 for success */
  33195. ret = 0;
  33196. }
  33197. #endif /* PKCS7_OUTPUT_TEST_BUNDLES */
  33198. wc_PKCS7_Free(pkcs7);
  33199. pkcs7 = NULL;
  33200. }
  33201. wc_FreeRng(&rng);
  33202. (void)eccCert;
  33203. (void)eccCertSz;
  33204. (void)eccPrivKey;
  33205. (void)eccPrivKeySz;
  33206. #if !defined(NO_AES) && !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  33207. (void)secretKey;
  33208. (void)secretKeyId;
  33209. #endif
  33210. #ifdef NO_RSA
  33211. (void)rsaCert;
  33212. (void)rsaCertSz;
  33213. (void)rsaPrivKey;
  33214. (void)rsaPrivKeySz;
  33215. #endif
  33216. out:
  33217. if (testVectors)
  33218. XFREE(testVectors, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33219. if (enveloped)
  33220. XFREE(enveloped, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33221. if (decoded)
  33222. XFREE(decoded, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33223. return ret;
  33224. }
  33225. WOLFSSL_TEST_SUBROUTINE int pkcs7authenveloped_test(void)
  33226. {
  33227. int ret = 0;
  33228. byte* rsaCert = NULL;
  33229. byte* rsaPrivKey = NULL;
  33230. word32 rsaCertSz = 0;
  33231. word32 rsaPrivKeySz = 0;
  33232. byte* eccCert = NULL;
  33233. byte* eccPrivKey = NULL;
  33234. word32 eccCertSz = 0;
  33235. word32 eccPrivKeySz = 0;
  33236. #ifndef NO_RSA
  33237. /* read client RSA cert and key in DER format */
  33238. rsaCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33239. if (rsaCert == NULL)
  33240. return -12300;
  33241. rsaPrivKey = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33242. if (rsaPrivKey == NULL) {
  33243. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33244. return -12301;
  33245. }
  33246. rsaCertSz = FOURK_BUF;
  33247. rsaPrivKeySz = FOURK_BUF;
  33248. #endif /* NO_RSA */
  33249. #ifdef HAVE_ECC
  33250. /* read client ECC cert and key in DER format */
  33251. eccCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33252. if (eccCert == NULL) {
  33253. #ifndef NO_RSA
  33254. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33255. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33256. #endif
  33257. return -12302;
  33258. }
  33259. eccPrivKey =(byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33260. if (eccPrivKey == NULL) {
  33261. #ifndef NO_RSA
  33262. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33263. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33264. #endif
  33265. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33266. return -12303;
  33267. }
  33268. eccCertSz = FOURK_BUF;
  33269. eccPrivKeySz = FOURK_BUF;
  33270. #endif /* HAVE_ECC */
  33271. ret = pkcs7_load_certs_keys(rsaCert, &rsaCertSz, rsaPrivKey,
  33272. &rsaPrivKeySz, NULL, NULL, NULL, NULL,
  33273. NULL, NULL, NULL, NULL, eccCert, &eccCertSz,
  33274. eccPrivKey, &eccPrivKeySz);
  33275. if (ret < 0) {
  33276. #ifndef NO_RSA
  33277. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33278. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33279. #endif
  33280. #ifdef HAVE_ECC
  33281. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33282. XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33283. #endif
  33284. return -12304;
  33285. }
  33286. ret = pkcs7authenveloped_run_vectors(rsaCert, (word32)rsaCertSz,
  33287. rsaPrivKey, (word32)rsaPrivKeySz,
  33288. eccCert, (word32)eccCertSz,
  33289. eccPrivKey, (word32)eccPrivKeySz);
  33290. #ifndef NO_RSA
  33291. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33292. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33293. #endif
  33294. #ifdef HAVE_ECC
  33295. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33296. XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33297. #endif
  33298. return ret;
  33299. }
  33300. #endif /* HAVE_AESGCM || HAVE_AESCCM */
  33301. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  33302. static const byte p7DefKey[] = {
  33303. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  33304. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  33305. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  33306. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  33307. };
  33308. static const byte p7AltKey[] = {
  33309. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  33310. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  33311. };
  33312. static int myCEKwrapFunc(PKCS7* pkcs7, byte* cek, word32 cekSz, byte* keyId,
  33313. word32 keyIdSz, byte* orginKey, word32 orginKeySz,
  33314. byte* out, word32 outSz, int keyWrapAlgo, int type, int direction)
  33315. {
  33316. int ret;
  33317. if (cek == NULL || out == NULL)
  33318. return BAD_FUNC_ARG;
  33319. /* test case sanity checks */
  33320. if (keyIdSz != 1) {
  33321. return -12310;
  33322. }
  33323. if (keyId[0] != 0x00) {
  33324. return -12311;
  33325. }
  33326. if (type != (int)PKCS7_KEKRI) {
  33327. return -12312;
  33328. }
  33329. switch (keyWrapAlgo) {
  33330. case AES256_WRAP:
  33331. ret = wc_AesKeyUnWrap(p7DefKey, sizeof(p7DefKey), cek, cekSz,
  33332. out, outSz, NULL);
  33333. if (ret <= 0)
  33334. return ret;
  33335. break;
  33336. default:
  33337. WOLFSSL_MSG("Unsupported key wrap algorithm in example");
  33338. return BAD_KEYWRAP_ALG_E;
  33339. };
  33340. (void)pkcs7;
  33341. (void)direction;
  33342. (void)orginKey; /* used with KAKRI */
  33343. (void)orginKeySz;
  33344. return ret;
  33345. }
  33346. /* returns key size on success */
  33347. static int getFirmwareKey(PKCS7* pkcs7, byte* key, word32 keySz)
  33348. {
  33349. int ret;
  33350. word32 atrSz;
  33351. byte atr[256];
  33352. /* Additionally can look for fwWrappedFirmwareKey
  33353. * 1.2.840.113529.1.9.16.1.16 */
  33354. const unsigned char fwWrappedFirmwareKey[] = {
  33355. /* 0x06, 0x0B */
  33356. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  33357. 0x01, 0x09, 0x10, 0x02, 0x27
  33358. };
  33359. /* find keyID in fwWrappedFirmwareKey */
  33360. ret = wc_PKCS7_GetAttributeValue(pkcs7, fwWrappedFirmwareKey,
  33361. sizeof(fwWrappedFirmwareKey), NULL, &atrSz);
  33362. if (ret == LENGTH_ONLY_E) {
  33363. XMEMSET(atr, 0, sizeof(atr));
  33364. ret = wc_PKCS7_GetAttributeValue(pkcs7, fwWrappedFirmwareKey,
  33365. sizeof(fwWrappedFirmwareKey), atr, &atrSz);
  33366. /* keyIdRaw[0] OCTET TAG */
  33367. /* keyIdRaw[1] Length */
  33368. if (ret > 0) {
  33369. PKCS7* envPkcs7;
  33370. envPkcs7 = wc_PKCS7_New(NULL, 0);
  33371. if (envPkcs7 == NULL) {
  33372. return MEMORY_E;
  33373. }
  33374. wc_PKCS7_Init(envPkcs7, NULL, 0);
  33375. ret = wc_PKCS7_SetWrapCEKCb(envPkcs7, myCEKwrapFunc);
  33376. if (ret == 0) {
  33377. /* expecting FIRMWARE_PKG_DATA content */
  33378. envPkcs7->contentOID = FIRMWARE_PKG_DATA;
  33379. ret = wc_PKCS7_DecodeEnvelopedData(envPkcs7, atr, atrSz,
  33380. key, keySz);
  33381. if (envPkcs7->contentOID != FIRMWARE_PKG_DATA) {
  33382. /* the contentOID should have been set to the inner
  33383. * FIRMWARE_PKG_DATA content */
  33384. ret = BAD_STATE_E;
  33385. }
  33386. }
  33387. wc_PKCS7_Free(envPkcs7);
  33388. }
  33389. }
  33390. return ret;
  33391. }
  33392. /* create a KEKRI enveloped data
  33393. * return size on success */
  33394. static int envelopedData_encrypt(byte* in, word32 inSz, byte* out,
  33395. word32 outSz)
  33396. {
  33397. int ret;
  33398. PKCS7* pkcs7;
  33399. WOLFSSL_SMALL_STACK_STATIC const byte keyId[] = { 0x00 };
  33400. pkcs7 = wc_PKCS7_New(NULL, INVALID_DEVID);
  33401. if (pkcs7 == NULL)
  33402. return -12330;
  33403. pkcs7->content = in;
  33404. pkcs7->contentSz = inSz;
  33405. pkcs7->contentOID = FIRMWARE_PKG_DATA;
  33406. pkcs7->encryptOID = AES256CBCb;
  33407. pkcs7->ukm = NULL;
  33408. pkcs7->ukmSz = 0;
  33409. /* add recipient (KEKRI type) */
  33410. ret = wc_PKCS7_AddRecipient_KEKRI(pkcs7, AES256_WRAP, (byte*)p7DefKey,
  33411. sizeof(p7DefKey), (byte*)keyId,
  33412. sizeof(keyId), NULL, NULL, 0, NULL, 0, 0);
  33413. if (ret < 0) {
  33414. printf("wc_PKCS7_AddRecipient_KEKRI() failed, ret = %d\n", ret);
  33415. wc_PKCS7_Free(pkcs7);
  33416. return -12331;
  33417. }
  33418. /* encode envelopedData, returns size */
  33419. ret = wc_PKCS7_EncodeEnvelopedData(pkcs7, out, outSz);
  33420. if (ret <= 0) {
  33421. printf("wc_PKCS7_EncodeEnvelopedData() failed, ret = %d\n", ret);
  33422. wc_PKCS7_Free(pkcs7);
  33423. return -12332;
  33424. }
  33425. wc_PKCS7_Free(pkcs7);
  33426. return ret;
  33427. }
  33428. /*
  33429. * keyHint is the KeyID to be set in the fwDecryptKeyID attribute
  33430. * returns size of buffer output on success
  33431. */
  33432. static int generateBundle(byte* out, word32 *outSz, const byte* encryptKey,
  33433. word32 encryptKeySz, byte keyHint, byte* cert, word32 certSz,
  33434. byte* key, word32 keySz)
  33435. {
  33436. int ret, attribNum = 1;
  33437. PKCS7* pkcs7;
  33438. /* KEY ID
  33439. * fwDecryptKeyID OID 1.2.840.113549.1.9.16.2.37
  33440. */
  33441. const unsigned char fwDecryptKeyID[] = {
  33442. 0x06, 0x0B,
  33443. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  33444. 0x01, 0x09, 0x10, 0x02, 0x25
  33445. };
  33446. /* fwWrappedFirmwareKey 1.2.840.113529.1.9.16.1.16 */
  33447. const unsigned char fwWrappedFirmwareKey[] = {
  33448. 0x06, 0x0B, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  33449. 0x01, 0x09, 0x10, 0x02, 0x27
  33450. };
  33451. byte keyID[] = { 0x04, 0x01, 0x00 };
  33452. byte env[256];
  33453. char data[] = "Test of wolfSSL PKCS7 decrypt callback";
  33454. PKCS7Attrib attribs[] =
  33455. {
  33456. { fwDecryptKeyID, sizeof(fwDecryptKeyID), keyID, sizeof(keyID) },
  33457. { fwWrappedFirmwareKey, sizeof(fwWrappedFirmwareKey), env, 0 }
  33458. };
  33459. keyID[2] = keyHint;
  33460. /* If using keyHint 0 then create a bundle with fwWrappedFirmwareKey */
  33461. if (keyHint == 0) {
  33462. ret = envelopedData_encrypt((byte*)p7DefKey, sizeof(p7DefKey), env,
  33463. sizeof(env));
  33464. if (ret <= 0) {
  33465. return ret;
  33466. }
  33467. attribs[1].valueSz = ret;
  33468. attribNum++;
  33469. }
  33470. /* init PKCS7 */
  33471. pkcs7 = wc_PKCS7_New(NULL, INVALID_DEVID);
  33472. if (pkcs7 == NULL)
  33473. return -12340;
  33474. ret = wc_PKCS7_InitWithCert(pkcs7, cert, certSz);
  33475. if (ret != 0) {
  33476. printf("ERROR: wc_PKCS7_InitWithCert() failed, ret = %d\n", ret);
  33477. wc_PKCS7_Free(pkcs7);
  33478. return -12341;
  33479. }
  33480. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID);
  33481. if (ret != 0) {
  33482. wc_PKCS7_Free(pkcs7);
  33483. return -12342;
  33484. }
  33485. /* encode Signed Encrypted FirmwarePkgData */
  33486. if (encryptKeySz == 16) {
  33487. ret = wc_PKCS7_EncodeSignedEncryptedFPD(pkcs7, (byte*)encryptKey,
  33488. encryptKeySz, key, keySz, AES128CBCb, RSAk, SHA256h,
  33489. (byte*)data, sizeof(data), NULL, 0,
  33490. attribs, attribNum, out, *outSz);
  33491. }
  33492. else {
  33493. ret = wc_PKCS7_EncodeSignedEncryptedFPD(pkcs7, (byte*)encryptKey,
  33494. encryptKeySz, key, keySz, AES256CBCb, RSAk, SHA256h,
  33495. (byte*)data, sizeof(data), NULL, 0,
  33496. attribs, attribNum, out, *outSz);
  33497. }
  33498. if (ret <= 0) {
  33499. printf("ERROR: wc_PKCS7_EncodeSignedEncryptedFPD() failed, "
  33500. "ret = %d\n", ret);
  33501. wc_PKCS7_Free(pkcs7);
  33502. return -12343;
  33503. } else {
  33504. *outSz = ret;
  33505. }
  33506. wc_PKCS7_Free(pkcs7);
  33507. return ret;
  33508. }
  33509. /* test verification and decryption of PKCS7 bundle
  33510. * return 0 on success
  33511. */
  33512. static int verifyBundle(byte* derBuf, word32 derSz, int keyHint)
  33513. {
  33514. int ret = 0;
  33515. int usrCtx = 1; /* test value to pass as user context to callback */
  33516. PKCS7* pkcs7 = NULL;
  33517. byte* sid = NULL;
  33518. word32 sidSz;
  33519. byte key[256];
  33520. word32 keySz = sizeof(key);
  33521. byte *decoded = NULL;
  33522. int decodedSz = FOURK_BUF/2;
  33523. WOLFSSL_SMALL_STACK_STATIC const byte expectedSid[] = {
  33524. #ifdef NO_SHA
  33525. #ifdef USE_CERT_BUFFERS_1024
  33526. 0x70, 0xe7, 0x79, 0x60, 0x8f, 0x41, 0xdc, 0xe9,
  33527. 0xad, 0x8b, 0x3d, 0x0c, 0x20, 0xf4, 0xc3, 0xf2,
  33528. 0x8e, 0x05, 0xe8, 0xa1, 0xb6, 0x68, 0x74, 0x06,
  33529. 0xbc, 0xe7, 0xc5, 0x3c, 0x13, 0x99, 0x79, 0xb9
  33530. #else
  33531. 0xce, 0x06, 0x07, 0xbe, 0xf1, 0xa6, 0x1e, 0x36,
  33532. 0xef, 0xfa, 0xbc, 0x89, 0x71, 0xf3, 0x23, 0x9e,
  33533. 0x34, 0x6d, 0xae, 0x86, 0xae, 0x2b, 0xdc, 0xf4,
  33534. 0x4a, 0x27, 0xd5, 0x63, 0x59, 0x4f, 0x4a, 0x71
  33535. #endif
  33536. #else /* !NO_SHA */
  33537. #ifdef USE_CERT_BUFFERS_1024
  33538. 0x81, 0x69, 0x0f, 0xf8, 0xdf, 0xdd, 0xcf, 0x34,
  33539. 0x29, 0xd5, 0x67, 0x75, 0x71, 0x85, 0xc7, 0x75,
  33540. 0x10, 0x69, 0x59, 0xec,
  33541. #else
  33542. 0x33, 0xD8, 0x45, 0x66, 0xD7, 0x68, 0x87, 0x18,
  33543. 0x7E, 0x54, 0x0D, 0x70, 0x27, 0x91, 0xC7, 0x26,
  33544. 0xD7, 0x85, 0x65, 0xC0
  33545. #endif
  33546. #endif /* !NO_SHA */
  33547. };
  33548. decoded = (byte *)XMALLOC(decodedSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33549. if (decoded == NULL) {
  33550. ret = MEMORY_E;
  33551. goto out;
  33552. }
  33553. pkcs7 = wc_PKCS7_New(HEAP_HINT, INVALID_DEVID);
  33554. if (pkcs7 == NULL) {
  33555. ret = MEMORY_E;
  33556. goto out;
  33557. }
  33558. /* Test verify */
  33559. ret = wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID);
  33560. if (ret != 0)
  33561. goto out;
  33562. ret = wc_PKCS7_InitWithCert(pkcs7, NULL, 0);
  33563. if (ret != 0)
  33564. goto out;
  33565. ret = wc_PKCS7_VerifySignedData(pkcs7, derBuf, derSz);
  33566. if (ret != 0)
  33567. goto out;
  33568. /* Get size of SID and print it out */
  33569. ret = wc_PKCS7_GetSignerSID(pkcs7, NULL, &sidSz);
  33570. if (ret != LENGTH_ONLY_E)
  33571. goto out;
  33572. sid = (byte*)XMALLOC(sidSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33573. if (sid == NULL) {
  33574. ret = MEMORY_E;
  33575. goto out;
  33576. }
  33577. ret = wc_PKCS7_GetSignerSID(pkcs7, sid, &sidSz);
  33578. if (ret != 0)
  33579. goto out;
  33580. ret = XMEMCMP(sid, expectedSid, sidSz);
  33581. if (ret != 0) {
  33582. ret = PKCS7_NO_SIGNER_E; /* close enough */
  33583. goto out;
  33584. }
  33585. /* get expected fwWrappedFirmwareKey */
  33586. if (keyHint == 0) {
  33587. ret = getFirmwareKey(pkcs7, key, keySz);
  33588. if (ret < 0)
  33589. goto out;
  33590. pkcs7->encryptionKey = key;
  33591. pkcs7->encryptionKeySz = ret;
  33592. }
  33593. else {
  33594. decodedSz = PKCS7_BUF_SIZE;
  33595. ret = wc_PKCS7_SetDecodeEncryptedCb(pkcs7, myDecryptionFunc);
  33596. if (ret != 0)
  33597. goto out;
  33598. ret = wc_PKCS7_SetDecodeEncryptedCtx(pkcs7, (void*)&usrCtx);
  33599. if (ret != 0)
  33600. goto out;
  33601. }
  33602. decodedSz = wc_PKCS7_DecodeEncryptedData(pkcs7, pkcs7->content,
  33603. pkcs7->contentSz, decoded, decodedSz);
  33604. if (decodedSz < 0) {
  33605. ret = decodedSz;
  33606. goto out;
  33607. }
  33608. ret = 0;
  33609. out:
  33610. if (decoded)
  33611. XFREE(decoded, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33612. if (pkcs7)
  33613. wc_PKCS7_Free(pkcs7);
  33614. if (sid)
  33615. XFREE(sid, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33616. return ret;
  33617. }
  33618. WOLFSSL_TEST_SUBROUTINE int pkcs7callback_test(byte* cert, word32 certSz, byte* key, word32 keySz)
  33619. {
  33620. int ret = 0;
  33621. word32 derSz;
  33622. byte *derBuf = (byte *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33623. if (! derBuf)
  33624. ERROR_OUT(-12360, out);
  33625. /* Doing default generation and verify */
  33626. derSz = FOURK_BUF;
  33627. ret = generateBundle(derBuf, &derSz, p7DefKey, sizeof(p7DefKey), 0, cert,
  33628. certSz, key, keySz);
  33629. if (ret <= 0) {
  33630. ERROR_OUT(-12361, out);
  33631. }
  33632. ret = verifyBundle(derBuf, derSz, 0);
  33633. if (ret != 0) {
  33634. ERROR_OUT(-12362, out);
  33635. }
  33636. /* test choosing other key with keyID */
  33637. derSz = FOURK_BUF;
  33638. ret = generateBundle(derBuf, &derSz, p7AltKey, sizeof(p7AltKey), 1,
  33639. cert, certSz, key, keySz);
  33640. if (ret <= 0) {
  33641. ERROR_OUT(-12363, out);
  33642. }
  33643. ret = verifyBundle(derBuf, derSz, 1);
  33644. if (ret != 0) {
  33645. ERROR_OUT(-12364, out);
  33646. }
  33647. /* test fail case with wrong keyID */
  33648. derSz = FOURK_BUF;
  33649. ret = generateBundle(derBuf, &derSz, p7DefKey, sizeof(p7DefKey), 1,
  33650. cert, certSz, key, keySz);
  33651. if (ret <= 0) {
  33652. ERROR_OUT(-12365, out);
  33653. }
  33654. ret = verifyBundle(derBuf, derSz, 1);
  33655. if (ret == 0) {
  33656. ERROR_OUT(-12366, out);
  33657. }
  33658. ret = 0;
  33659. out:
  33660. if (derBuf)
  33661. XFREE(derBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33662. return ret;
  33663. }
  33664. #endif /* !NO_AES && HAVE_AES_CBC */
  33665. #ifndef NO_PKCS7_ENCRYPTED_DATA
  33666. typedef struct {
  33667. const byte* content;
  33668. word32 contentSz;
  33669. int contentOID;
  33670. int encryptOID;
  33671. byte* encryptionKey;
  33672. word32 encryptionKeySz;
  33673. PKCS7Attrib* attribs;
  33674. word32 attribsSz;
  33675. const char* outFileName;
  33676. } pkcs7EncryptedVector;
  33677. WOLFSSL_TEST_SUBROUTINE int pkcs7encrypted_test(void)
  33678. {
  33679. int ret = 0;
  33680. int i, testSz;
  33681. int encryptedSz, decodedSz, attribIdx;
  33682. PKCS7* pkcs7;
  33683. byte *encrypted;
  33684. byte *decoded;
  33685. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  33686. XFILE pkcs7File;
  33687. #endif
  33688. PKCS7Attrib* expectedAttrib;
  33689. PKCS7DecodedAttrib* decodedAttrib;
  33690. WOLFSSL_SMALL_STACK_STATIC const byte data[] = { /* Hello World */
  33691. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  33692. 0x72,0x6c,0x64
  33693. };
  33694. #ifndef NO_DES3
  33695. byte desKey[] = {
  33696. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
  33697. };
  33698. byte des3Key[] = {
  33699. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  33700. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  33701. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  33702. };
  33703. #endif
  33704. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  33705. #ifdef WOLFSSL_AES_128
  33706. byte aes128Key[] = {
  33707. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  33708. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  33709. };
  33710. #endif
  33711. #ifdef WOLFSSL_AES_192
  33712. byte aes192Key[] = {
  33713. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  33714. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  33715. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  33716. };
  33717. #endif
  33718. #ifdef WOLFSSL_AES_256
  33719. byte aes256Key[] = {
  33720. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  33721. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  33722. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  33723. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  33724. };
  33725. #endif
  33726. #ifdef WOLFSSL_AES_256
  33727. /* Attribute example from RFC 4134, Section 7.2
  33728. * OID = 1.2.5555
  33729. * OCTET STRING = 'This is a test General ASN Attribute, number 1.' */
  33730. static byte genAttrOid[] = { 0x06, 0x03, 0x2a, 0xab, 0x33 };
  33731. static byte genAttr[] = { 0x04, 47,
  33732. 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20,
  33733. 0x61, 0x20, 0x74, 0x65, 0x73, 0x74, 0x20, 0x47,
  33734. 0x65, 0x6e, 0x65, 0x72, 0x61, 0x6c, 0x20, 0x41,
  33735. 0x53, 0x4e, 0x20, 0x41, 0x74, 0x74, 0x72, 0x69,
  33736. 0x62, 0x75, 0x74, 0x65, 0x2c, 0x20, 0x6e, 0x75,
  33737. 0x6d, 0x62, 0x65, 0x72, 0x20, 0x31, 0x2e };
  33738. static byte genAttrOid2[] = { 0x06, 0x03, 0x2a, 0xab, 0x34 };
  33739. static byte genAttr2[] = { 0x04, 47,
  33740. 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20,
  33741. 0x61, 0x20, 0x74, 0x65, 0x73, 0x74, 0x20, 0x47,
  33742. 0x65, 0x6e, 0x65, 0x72, 0x61, 0x6c, 0x20, 0x41,
  33743. 0x53, 0x4e, 0x20, 0x41, 0x74, 0x74, 0x72, 0x69,
  33744. 0x62, 0x75, 0x74, 0x65, 0x2c, 0x20, 0x6e, 0x75,
  33745. 0x6d, 0x62, 0x65, 0x72, 0x20, 0x32, 0x2e };
  33746. PKCS7Attrib attribs[] =
  33747. {
  33748. { genAttrOid, sizeof(genAttrOid), genAttr, sizeof(genAttr) }
  33749. };
  33750. PKCS7Attrib multiAttribs[] =
  33751. {
  33752. { genAttrOid, sizeof(genAttrOid), genAttr, sizeof(genAttr) },
  33753. { genAttrOid2, sizeof(genAttrOid2), genAttr2, sizeof(genAttr2) }
  33754. };
  33755. #endif
  33756. #endif /* NO_AES */
  33757. const pkcs7EncryptedVector testVectors[] =
  33758. {
  33759. #ifndef NO_DES3
  33760. {data, (word32)sizeof(data), DATA, DES3b, des3Key, sizeof(des3Key),
  33761. NULL, 0, "pkcs7encryptedDataDES3.der"},
  33762. {data, (word32)sizeof(data), DATA, DESb, desKey, sizeof(desKey),
  33763. NULL, 0, "pkcs7encryptedDataDES.der"},
  33764. #endif /* NO_DES3 */
  33765. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  33766. #ifdef WOLFSSL_AES_128
  33767. {data, (word32)sizeof(data), DATA, AES128CBCb, aes128Key,
  33768. sizeof(aes128Key), NULL, 0, "pkcs7encryptedDataAES128CBC.der"},
  33769. #endif
  33770. #ifdef WOLFSSL_AES_192
  33771. {data, (word32)sizeof(data), DATA, AES192CBCb, aes192Key,
  33772. sizeof(aes192Key), NULL, 0, "pkcs7encryptedDataAES192CBC.der"},
  33773. #endif
  33774. #ifdef WOLFSSL_AES_256
  33775. {data, (word32)sizeof(data), DATA, AES256CBCb, aes256Key,
  33776. sizeof(aes256Key), NULL, 0, "pkcs7encryptedDataAES256CBC.der"},
  33777. /* test with optional unprotected attributes */
  33778. {data, (word32)sizeof(data), DATA, AES256CBCb, aes256Key,
  33779. sizeof(aes256Key), attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  33780. "pkcs7encryptedDataAES256CBC_attribs.der"},
  33781. /* test with multiple optional unprotected attributes */
  33782. {data, (word32)sizeof(data), DATA, AES256CBCb, aes256Key,
  33783. sizeof(aes256Key), multiAttribs,
  33784. (sizeof(multiAttribs)/sizeof(PKCS7Attrib)),
  33785. "pkcs7encryptedDataAES256CBC_multi_attribs.der"},
  33786. /* test with contentType set to FirmwarePkgData */
  33787. {data, (word32)sizeof(data), FIRMWARE_PKG_DATA, AES256CBCb, aes256Key,
  33788. sizeof(aes256Key), NULL, 0,
  33789. "pkcs7encryptedDataAES256CBC_firmwarePkgData.der"},
  33790. #endif
  33791. #endif /* !NO_AES && HAVE_AES_CBC */
  33792. };
  33793. encrypted = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33794. decoded = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33795. if ((! encrypted) || (! decoded)) {
  33796. ERROR_OUT(MEMORY_E, out);
  33797. }
  33798. testSz = sizeof(testVectors) / sizeof(pkcs7EncryptedVector);
  33799. for (i = 0; i < testSz; i++) {
  33800. pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
  33801. if (pkcs7 == NULL) {
  33802. ERROR_OUT(-12400, out);
  33803. }
  33804. pkcs7->content = (byte*)testVectors[i].content;
  33805. pkcs7->contentSz = testVectors[i].contentSz;
  33806. pkcs7->contentOID = testVectors[i].contentOID;
  33807. pkcs7->encryptOID = testVectors[i].encryptOID;
  33808. pkcs7->encryptionKey = testVectors[i].encryptionKey;
  33809. pkcs7->encryptionKeySz = testVectors[i].encryptionKeySz;
  33810. pkcs7->unprotectedAttribs = testVectors[i].attribs;
  33811. pkcs7->unprotectedAttribsSz = testVectors[i].attribsSz;
  33812. /* encode encryptedData */
  33813. encryptedSz = wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  33814. PKCS7_BUF_SIZE);
  33815. if (encryptedSz <= 0) {
  33816. wc_PKCS7_Free(pkcs7);
  33817. ERROR_OUT(-12401, out);
  33818. }
  33819. /* decode encryptedData */
  33820. #ifndef NO_PKCS7_STREAM
  33821. { /* test reading byte by byte */
  33822. int z;
  33823. for (z = 0; z < encryptedSz; z++) {
  33824. decodedSz = wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted + z, 1,
  33825. decoded, PKCS7_BUF_SIZE);
  33826. if (decodedSz <= 0 && decodedSz != WC_PKCS7_WANT_READ_E) {
  33827. printf("unexpected error %d\n", decodedSz);
  33828. ERROR_OUT(-12402, out);
  33829. }
  33830. }
  33831. /* test decode result */
  33832. if (XMEMCMP(decoded, data, sizeof(data)) != 0) {
  33833. printf("stream read failed\n");
  33834. wc_PKCS7_Free(pkcs7);
  33835. ERROR_OUT(-12403, out);
  33836. }
  33837. }
  33838. #endif
  33839. decodedSz = wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  33840. decoded, PKCS7_BUF_SIZE);
  33841. if (decodedSz <= 0){
  33842. wc_PKCS7_Free(pkcs7);
  33843. ERROR_OUT(-12404, out);
  33844. }
  33845. /* test decode result */
  33846. if (XMEMCMP(decoded, data, sizeof(data)) != 0) {
  33847. wc_PKCS7_Free(pkcs7);
  33848. ERROR_OUT(-12405, out);
  33849. }
  33850. /* verify decoded unprotected attributes */
  33851. if (pkcs7->decodedAttrib != NULL) {
  33852. decodedAttrib = pkcs7->decodedAttrib;
  33853. attribIdx = 1;
  33854. while (decodedAttrib != NULL) {
  33855. /* expected attribute, stored list is reversed */
  33856. expectedAttrib = &(pkcs7->unprotectedAttribs
  33857. [pkcs7->unprotectedAttribsSz - attribIdx]);
  33858. /* verify oid */
  33859. if (XMEMCMP(decodedAttrib->oid, expectedAttrib->oid,
  33860. decodedAttrib->oidSz) != 0) {
  33861. wc_PKCS7_Free(pkcs7);
  33862. ERROR_OUT(-12406, out);
  33863. }
  33864. /* verify value */
  33865. if (XMEMCMP(decodedAttrib->value, expectedAttrib->value,
  33866. decodedAttrib->valueSz) != 0) {
  33867. wc_PKCS7_Free(pkcs7);
  33868. ERROR_OUT(-12407, out);
  33869. }
  33870. decodedAttrib = decodedAttrib->next;
  33871. attribIdx++;
  33872. }
  33873. }
  33874. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  33875. /* output pkcs7 envelopedData for external testing */
  33876. pkcs7File = XFOPEN(testVectors[i].outFileName, "wb");
  33877. if (!pkcs7File) {
  33878. wc_PKCS7_Free(pkcs7);
  33879. ERROR_OUT(-12408, out);
  33880. }
  33881. ret = (int)XFWRITE(encrypted, encryptedSz, 1, pkcs7File);
  33882. XFCLOSE(pkcs7File);
  33883. if (ret > 0)
  33884. ret = 0;
  33885. #endif
  33886. wc_PKCS7_Free(pkcs7);
  33887. }
  33888. out:
  33889. if (encrypted)
  33890. XFREE(encrypted, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33891. if (decoded)
  33892. XFREE(decoded, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33893. return ret;
  33894. }
  33895. #endif /* NO_PKCS7_ENCRYPTED_DATA */
  33896. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  33897. typedef struct {
  33898. const byte* content;
  33899. word32 contentSz;
  33900. int contentOID;
  33901. const char* outFileName;
  33902. } pkcs7CompressedVector;
  33903. WOLFSSL_TEST_SUBROUTINE int pkcs7compressed_test(void)
  33904. {
  33905. int ret = 0;
  33906. int i, testSz;
  33907. int compressedSz, decodedSz;
  33908. PKCS7* pkcs7;
  33909. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  33910. byte *compressed;
  33911. byte *decoded;
  33912. #else
  33913. byte compressed[PKCS7_BUF_SIZE];
  33914. byte decoded[PKCS7_BUF_SIZE];
  33915. #endif
  33916. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  33917. XFILE pkcs7File;
  33918. #endif
  33919. WOLFSSL_SMALL_STACK_STATIC const byte data[] = { /* Hello World */
  33920. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  33921. 0x72,0x6c,0x64
  33922. };
  33923. const pkcs7CompressedVector testVectors[] =
  33924. {
  33925. {data, (word32)sizeof(data), DATA,
  33926. "pkcs7compressedData_data_zlib.der"},
  33927. {data, (word32)sizeof(data), FIRMWARE_PKG_DATA,
  33928. "pkcs7compressedData_firmwarePkgData_zlib.der"},
  33929. };
  33930. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  33931. compressed = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33932. decoded = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33933. if ((! compressed) || (! decoded)) {
  33934. ERROR_OUT(MEMORY_E, out);
  33935. }
  33936. #endif
  33937. testSz = sizeof(testVectors) / sizeof(pkcs7CompressedVector);
  33938. for (i = 0; i < testSz; i++) {
  33939. pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
  33940. if (pkcs7 == NULL) {
  33941. ERROR_OUT(-12500, out);
  33942. }
  33943. pkcs7->content = (byte*)testVectors[i].content;
  33944. pkcs7->contentSz = testVectors[i].contentSz;
  33945. pkcs7->contentOID = testVectors[i].contentOID;
  33946. /* encode compressedData */
  33947. compressedSz = wc_PKCS7_EncodeCompressedData(pkcs7, compressed,
  33948. PKCS7_BUF_SIZE);
  33949. if (compressedSz <= 0) {
  33950. wc_PKCS7_Free(pkcs7);
  33951. ERROR_OUT(-12501, out);
  33952. }
  33953. /* decode compressedData */
  33954. decodedSz = wc_PKCS7_DecodeCompressedData(pkcs7, compressed,
  33955. compressedSz, decoded,
  33956. PKCS7_BUF_SIZE);
  33957. if (decodedSz <= 0){
  33958. wc_PKCS7_Free(pkcs7);
  33959. ERROR_OUT(-12502, out);
  33960. }
  33961. /* test decode result */
  33962. if (XMEMCMP(decoded, testVectors[i].content,
  33963. testVectors[i].contentSz) != 0) {
  33964. wc_PKCS7_Free(pkcs7);
  33965. ERROR_OUT(-12503, out);
  33966. }
  33967. /* make sure content type is the same */
  33968. if (testVectors[i].contentOID != pkcs7->contentOID) {
  33969. ERROR_OUT(-12504, out);
  33970. }
  33971. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  33972. /* output pkcs7 compressedData for external testing */
  33973. pkcs7File = XFOPEN(testVectors[i].outFileName, "wb");
  33974. if (!pkcs7File) {
  33975. wc_PKCS7_Free(pkcs7);
  33976. ERROR_OUT(-12505, out);
  33977. }
  33978. ret = (int)XFWRITE(compressed, compressedSz, 1, pkcs7File);
  33979. XFCLOSE(pkcs7File);
  33980. if (ret > 0)
  33981. ret = 0;
  33982. #endif
  33983. wc_PKCS7_Free(pkcs7);
  33984. }
  33985. out:
  33986. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  33987. if (compressed)
  33988. XFREE(compressed, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33989. if (decoded)
  33990. XFREE(decoded, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33991. #endif
  33992. return ret;
  33993. } /* pkcs7compressed_test() */
  33994. #undef PKCS7_BUF_SIZE
  33995. #endif /* HAVE_LIBZ */
  33996. typedef struct {
  33997. const byte* content;
  33998. word32 contentSz;
  33999. int hashOID;
  34000. int signOID;
  34001. byte* privateKey;
  34002. word32 privateKeySz;
  34003. byte* cert;
  34004. size_t certSz;
  34005. byte* caCert;
  34006. size_t caCertSz;
  34007. PKCS7Attrib* signedAttribs;
  34008. word32 signedAttribsSz;
  34009. const char* outFileName;
  34010. int contentOID;
  34011. byte* contentType;
  34012. word32 contentTypeSz;
  34013. int sidType;
  34014. int encryptOID; /* for single-shot encrypt alg OID */
  34015. int encCompFlag; /* for single-shot. 1 = enc, 2 = comp, 3 = both*/
  34016. byte* encryptKey; /* for single-shot, encryptedData */
  34017. word32 encryptKeySz; /* for single-shot, encryptedData */
  34018. PKCS7Attrib* unprotectedAttribs; /* for single-shot, encryptedData */
  34019. word32 unprotectedAttribsSz; /* for single-shot, encryptedData */
  34020. word16 detachedSignature; /* generate detached signature (0:1) */
  34021. } pkcs7SignedVector;
  34022. static int pkcs7signed_run_vectors(
  34023. byte* rsaClientCertBuf, word32 rsaClientCertBufSz,
  34024. byte* rsaClientPrivKeyBuf, word32 rsaClientPrivKeyBufSz,
  34025. byte* rsaServerCertBuf, word32 rsaServerCertBufSz,
  34026. byte* rsaServerPrivKeyBuf, word32 rsaServerPrivKeyBufSz,
  34027. byte* rsaCaCertBuf, word32 rsaCaCertBufSz,
  34028. byte* rsaCaPrivKeyBuf, word32 rsaCaPrivKeyBufSz,
  34029. byte* eccClientCertBuf, word32 eccClientCertBufSz,
  34030. byte* eccClientPrivKeyBuf, word32 eccClientPrivKeyBufSz)
  34031. {
  34032. int ret, testSz = 0, i;
  34033. int encodedSz;
  34034. byte* out = NULL;
  34035. word32 outSz;
  34036. WC_RNG rng;
  34037. PKCS7* pkcs7 = NULL;
  34038. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  34039. XFILE file;
  34040. #endif
  34041. WOLFSSL_SMALL_STACK_STATIC const byte data[] = { /* Hello World */
  34042. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  34043. 0x72,0x6c,0x64
  34044. };
  34045. static byte transIdOid[] =
  34046. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  34047. 0x09, 0x07 };
  34048. static byte messageTypeOid[] =
  34049. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  34050. 0x09, 0x02 };
  34051. static byte senderNonceOid[] =
  34052. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  34053. 0x09, 0x05 };
  34054. #ifndef NO_SHA
  34055. static byte transId[(WC_SHA_DIGEST_SIZE + 1) * 2 + 1];
  34056. #else
  34057. static byte transId[(WC_SHA256_DIGEST_SIZE + 1) * 2 + 1];
  34058. #endif
  34059. static byte messageType[] = { 0x13, 2, '1', '9' };
  34060. static byte senderNonce[PKCS7_NONCE_SZ + 2];
  34061. static PKCS7Attrib attribs[] =
  34062. {
  34063. { transIdOid, sizeof(transIdOid), transId,
  34064. sizeof(transId) - 1 }, /* take off the null */
  34065. { messageTypeOid, sizeof(messageTypeOid), messageType,
  34066. sizeof(messageType) },
  34067. { senderNonceOid, sizeof(senderNonceOid), senderNonce,
  34068. sizeof(senderNonce) }
  34069. };
  34070. /* for testing custom contentType, FirmwarePkgData */
  34071. static byte customContentType[] = { 0x06, 0x0B, 0x2A, 0x86,
  34072. 0x48, 0x86, 0xF7, 0x0D,
  34073. 0x01, 0x09, 0x10, 0x01, 0x10 };
  34074. #define MAX_TESTVECTORS_LEN 20
  34075. #define ADD_PKCS7SIGNEDVECTOR(...) { \
  34076. pkcs7SignedVector _this_vector = { __VA_ARGS__ }; \
  34077. if (testSz == MAX_TESTVECTORS_LEN) { \
  34078. ret = -12534; \
  34079. goto out; \
  34080. } \
  34081. XMEMCPY(&testVectors[testSz++], &_this_vector, sizeof _this_vector);\
  34082. }
  34083. pkcs7SignedVector *testVectors = NULL;
  34084. XMEMSET(&rng, 0, sizeof(rng));
  34085. testVectors = (pkcs7SignedVector *)XMALLOC(MAX_TESTVECTORS_LEN * sizeof(*testVectors),
  34086. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34087. if (testVectors == NULL) {
  34088. ret = -12567;
  34089. goto out;
  34090. }
  34091. {
  34092. #ifndef NO_RSA
  34093. #ifndef NO_SHA
  34094. /* RSA with SHA */
  34095. ADD_PKCS7SIGNEDVECTOR(
  34096. data, (word32)sizeof(data), SHAh, RSAk, rsaClientPrivKeyBuf,
  34097. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  34098. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  34099. "pkcs7signedData_RSA_SHA.der", 0, NULL, 0, 0, 0, 0, NULL, 0, NULL,
  34100. 0, 0);
  34101. /* RSA with SHA, no signed attributes */
  34102. ADD_PKCS7SIGNEDVECTOR(
  34103. data, (word32)sizeof(data), SHAh, RSAk, rsaClientPrivKeyBuf,
  34104. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz,
  34105. NULL, 0, NULL, 0,
  34106. "pkcs7signedData_RSA_SHA_noattr.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  34107. NULL, 0, 0);
  34108. #endif
  34109. #ifdef WOLFSSL_SHA224
  34110. /* RSA with SHA224 */
  34111. ADD_PKCS7SIGNEDVECTOR(
  34112. data, (word32)sizeof(data), SHA224h, RSAk, rsaClientPrivKeyBuf,
  34113. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  34114. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  34115. "pkcs7signedData_RSA_SHA224.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  34116. NULL, 0, 0);
  34117. #endif
  34118. #ifndef NO_SHA256
  34119. /* RSA with SHA256 */
  34120. ADD_PKCS7SIGNEDVECTOR(
  34121. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  34122. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  34123. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  34124. "pkcs7signedData_RSA_SHA256.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  34125. NULL, 0, 0);
  34126. /* RSA with SHA256, detached signature */
  34127. ADD_PKCS7SIGNEDVECTOR(
  34128. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  34129. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  34130. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  34131. "pkcs7signedData_RSA_SHA256_detachedSig.der", 0, NULL, 0, 0, 0, 0,
  34132. NULL, 0, NULL, 0, 1);
  34133. /* RSA with SHA256 and SubjectKeyIdentifier in SignerIdentifier */
  34134. ADD_PKCS7SIGNEDVECTOR(
  34135. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  34136. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  34137. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  34138. "pkcs7signedData_RSA_SHA256_SKID.der", 0, NULL, 0, CMS_SKID, 0, 0,
  34139. NULL, 0, NULL, 0, 0);
  34140. /* RSA with SHA256 and custom contentType */
  34141. ADD_PKCS7SIGNEDVECTOR(
  34142. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  34143. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  34144. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  34145. "pkcs7signedData_RSA_SHA256_custom_contentType.der", 0,
  34146. customContentType, sizeof(customContentType), 0, 0, 0, NULL, 0,
  34147. NULL, 0, 0);
  34148. /* RSA with SHA256 and FirmwarePkgData contentType */
  34149. ADD_PKCS7SIGNEDVECTOR(
  34150. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  34151. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  34152. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  34153. "pkcs7signedData_RSA_SHA256_firmwarePkgData.der",
  34154. FIRMWARE_PKG_DATA, NULL, 0, 0, 0, 0, NULL, 0, NULL, 0, 0);
  34155. /* RSA with SHA256 using server cert and ca cert */
  34156. ADD_PKCS7SIGNEDVECTOR(
  34157. data, (word32)sizeof(data), SHA256h, RSAk, rsaServerPrivKeyBuf,
  34158. rsaServerPrivKeyBufSz, rsaServerCertBuf, rsaServerCertBufSz,
  34159. rsaCaCertBuf, rsaCaCertBufSz,
  34160. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  34161. "pkcs7signedData_RSA_SHA256_with_ca_cert.der", 0, NULL, 0, 0, 0, 0,
  34162. NULL, 0, NULL, 0, 0);
  34163. #endif
  34164. #if defined(WOLFSSL_SHA384)
  34165. /* RSA with SHA384 */
  34166. ADD_PKCS7SIGNEDVECTOR(
  34167. data, (word32)sizeof(data), SHA384h, RSAk, rsaClientPrivKeyBuf,
  34168. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  34169. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  34170. "pkcs7signedData_RSA_SHA384.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  34171. NULL, 0, 0);
  34172. #endif
  34173. #if defined(WOLFSSL_SHA512)
  34174. /* RSA with SHA512 */
  34175. ADD_PKCS7SIGNEDVECTOR(
  34176. data, (word32)sizeof(data), SHA512h, RSAk, rsaClientPrivKeyBuf,
  34177. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  34178. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  34179. "pkcs7signedData_RSA_SHA512.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  34180. NULL, 0, 0);
  34181. #endif
  34182. #endif /* NO_RSA */
  34183. #ifdef HAVE_ECC
  34184. #ifndef NO_SHA
  34185. /* ECDSA with SHA */
  34186. ADD_PKCS7SIGNEDVECTOR(
  34187. data, (word32)sizeof(data), SHAh, ECDSAk, eccClientPrivKeyBuf,
  34188. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  34189. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  34190. "pkcs7signedData_ECDSA_SHA.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  34191. NULL, 0, 0);
  34192. /* ECDSA with SHA, no signed attributes */
  34193. ADD_PKCS7SIGNEDVECTOR(
  34194. data, (word32)sizeof(data), SHAh, ECDSAk, eccClientPrivKeyBuf,
  34195. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz,
  34196. NULL, 0, NULL, 0,
  34197. "pkcs7signedData_ECDSA_SHA_noattr.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  34198. NULL, 0, 0);
  34199. #endif
  34200. #ifdef WOLFSSL_SHA224
  34201. /* ECDSA with SHA224 */
  34202. ADD_PKCS7SIGNEDVECTOR(
  34203. data, (word32)sizeof(data), SHA224h, ECDSAk, eccClientPrivKeyBuf,
  34204. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  34205. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  34206. "pkcs7signedData_ECDSA_SHA224.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  34207. NULL, 0, 0);
  34208. #endif
  34209. #ifndef NO_SHA256
  34210. /* ECDSA with SHA256 */
  34211. ADD_PKCS7SIGNEDVECTOR(
  34212. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  34213. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  34214. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  34215. "pkcs7signedData_ECDSA_SHA256.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  34216. NULL, 0, 0);
  34217. /* ECDSA with SHA256 and SubjectKeyIdentifier in SigherIdentifier */
  34218. ADD_PKCS7SIGNEDVECTOR(
  34219. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  34220. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  34221. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  34222. "pkcs7signedData_ECDSA_SHA256_SKID.der", 0, NULL, 0, CMS_SKID, 0, 0,
  34223. NULL, 0, NULL, 0, 0);
  34224. /* ECDSA with SHA256 and custom contentType */
  34225. ADD_PKCS7SIGNEDVECTOR(
  34226. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  34227. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  34228. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  34229. "pkcs7signedData_ECDSA_SHA256_custom_contentType.der", 0,
  34230. customContentType, sizeof(customContentType), 0, 0, 0, NULL, 0,
  34231. NULL, 0, 0);
  34232. /* ECDSA with SHA256 and FirmwarePkgData contentType */
  34233. ADD_PKCS7SIGNEDVECTOR(
  34234. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  34235. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  34236. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  34237. "pkcs7signedData_ECDSA_SHA256_firmwarePkgData.der",
  34238. FIRMWARE_PKG_DATA, NULL, 0, 0, 0, 0, NULL, 0, NULL, 0, 0);
  34239. #endif
  34240. #ifdef WOLFSSL_SHA384
  34241. /* ECDSA with SHA384 */
  34242. ADD_PKCS7SIGNEDVECTOR(
  34243. data, (word32)sizeof(data), SHA384h, ECDSAk, eccClientPrivKeyBuf,
  34244. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  34245. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  34246. "pkcs7signedData_ECDSA_SHA384.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  34247. NULL, 0, 0);
  34248. #endif
  34249. #ifdef WOLFSSL_SHA512
  34250. /* ECDSA with SHA512 */
  34251. ADD_PKCS7SIGNEDVECTOR(
  34252. data, (word32)sizeof(data), SHA512h, ECDSAk, eccClientPrivKeyBuf,
  34253. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  34254. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  34255. "pkcs7signedData_ECDSA_SHA512.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  34256. NULL, 0, 0);
  34257. #endif
  34258. #endif /* HAVE_ECC */
  34259. };
  34260. #undef MAX_TESTVECTORS_LEN
  34261. #undef ADD_PKCS7SIGNEDVECTOR
  34262. outSz = FOURK_BUF;
  34263. out = (byte*)XMALLOC(outSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34264. if (out == NULL)
  34265. ERROR_OUT(-12510, out);
  34266. XMEMSET(out, 0, outSz);
  34267. ret = wc_PKCS7_PadData((byte*)data, sizeof(data), out, outSz, 16);
  34268. if (ret < 0)
  34269. ERROR_OUT(-12511, out);
  34270. #ifndef HAVE_FIPS
  34271. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  34272. #else
  34273. ret = wc_InitRng(&rng);
  34274. #endif
  34275. if (ret != 0)
  34276. ERROR_OUT(-12512, out);
  34277. for (i = 0; i < testSz; i++) {
  34278. if (pkcs7)
  34279. wc_PKCS7_Free(pkcs7);
  34280. pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
  34281. if (pkcs7 == NULL)
  34282. ERROR_OUT(-12513, out);
  34283. ret = wc_PKCS7_InitWithCert(pkcs7, testVectors[i].cert,
  34284. (word32)testVectors[i].certSz);
  34285. if (ret != 0)
  34286. ERROR_OUT(-12514, out);
  34287. /* load CA certificate, if present */
  34288. if (testVectors[i].caCert != NULL) {
  34289. ret = wc_PKCS7_AddCertificate(pkcs7, testVectors[i].caCert,
  34290. (word32)testVectors[i].caCertSz);
  34291. if (ret != 0)
  34292. ERROR_OUT(-12515, out);
  34293. }
  34294. pkcs7->rng = &rng;
  34295. pkcs7->content = (byte*)testVectors[i].content;
  34296. pkcs7->contentSz = testVectors[i].contentSz;
  34297. pkcs7->contentOID = testVectors[i].contentOID;
  34298. pkcs7->hashOID = testVectors[i].hashOID;
  34299. pkcs7->encryptOID = testVectors[i].signOID;
  34300. pkcs7->privateKey = testVectors[i].privateKey;
  34301. pkcs7->privateKeySz = testVectors[i].privateKeySz;
  34302. pkcs7->signedAttribs = testVectors[i].signedAttribs;
  34303. pkcs7->signedAttribsSz = testVectors[i].signedAttribsSz;
  34304. /* optional custom contentType, default is DATA,
  34305. overrides contentOID if set */
  34306. if (testVectors[i].contentType != NULL) {
  34307. ret = wc_PKCS7_SetContentType(pkcs7, testVectors[i].contentType,
  34308. testVectors[i].contentTypeSz);
  34309. if (ret != 0)
  34310. ERROR_OUT(-12516, out);
  34311. }
  34312. /* set SignerIdentifier to use SubjectKeyIdentifier if desired,
  34313. default is IssuerAndSerialNumber */
  34314. if (testVectors[i].sidType == CMS_SKID) {
  34315. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID);
  34316. if (ret != 0)
  34317. ERROR_OUT(-12517, out);
  34318. }
  34319. /* generate senderNonce */
  34320. {
  34321. senderNonce[0] = 0x04;
  34322. senderNonce[1] = PKCS7_NONCE_SZ;
  34323. ret = wc_RNG_GenerateBlock(&rng, &senderNonce[2], PKCS7_NONCE_SZ);
  34324. if (ret != 0)
  34325. ERROR_OUT(-12518, out);
  34326. }
  34327. /* generate transactionID (used with SCEP) */
  34328. {
  34329. #ifndef NO_SHA
  34330. wc_Sha sha;
  34331. byte digest[WC_SHA_DIGEST_SIZE];
  34332. #else
  34333. wc_Sha256 sha;
  34334. byte digest[WC_SHA256_DIGEST_SIZE];
  34335. #endif
  34336. int j,k;
  34337. transId[0] = 0x13;
  34338. transId[1] = sizeof(digest) * 2;
  34339. #ifndef NO_SHA
  34340. ret = wc_InitSha_ex(&sha, HEAP_HINT, devId);
  34341. if (ret != 0)
  34342. ERROR_OUT(-12519, out);
  34343. wc_ShaUpdate(&sha, pkcs7->publicKey, pkcs7->publicKeySz);
  34344. wc_ShaFinal(&sha, digest);
  34345. wc_ShaFree(&sha);
  34346. #else
  34347. ret = wc_InitSha256_ex(&sha, HEAP_HINT, devId);
  34348. if (ret != 0)
  34349. ERROR_OUT(-12520, out);
  34350. wc_Sha256Update(&sha, pkcs7->publicKey, pkcs7->publicKeySz);
  34351. wc_Sha256Final(&sha, digest);
  34352. wc_Sha256Free(&sha);
  34353. #endif
  34354. for (j = 0, k = 2; j < (int)sizeof(digest); j++, k += 2) {
  34355. #if defined(WOLF_C89)
  34356. XSPRINTF((char*)&transId[k], "%02x", digest[j]);
  34357. #else
  34358. (void)XSNPRINTF((char*)&transId[k], 3, "%02x", digest[j]);
  34359. #endif
  34360. }
  34361. }
  34362. /* enable detached signature generation, if set */
  34363. if (testVectors[i].detachedSignature == 1) {
  34364. ret = wc_PKCS7_SetDetached(pkcs7, 1);
  34365. if (ret != 0)
  34366. ERROR_OUT(-12521, out);
  34367. }
  34368. encodedSz = wc_PKCS7_EncodeSignedData(pkcs7, out, outSz);
  34369. if (encodedSz < 0)
  34370. ERROR_OUT(-12522, out);
  34371. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  34372. /* write PKCS#7 to output file for more testing */
  34373. file = XFOPEN(testVectors[i].outFileName, "wb");
  34374. if (!file) {
  34375. ERROR_OUT(-12523, out);
  34376. }
  34377. ret = (int)XFWRITE(out, 1, encodedSz, file);
  34378. XFCLOSE(file);
  34379. if (ret != (int)encodedSz)
  34380. ERROR_OUT(-12524, out);
  34381. #endif /* PKCS7_OUTPUT_TEST_BUNDLES */
  34382. wc_PKCS7_Free(pkcs7);
  34383. pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
  34384. if (pkcs7 == NULL)
  34385. ERROR_OUT(-12525, out);
  34386. wc_PKCS7_InitWithCert(pkcs7, NULL, 0);
  34387. if (testVectors[i].detachedSignature == 1) {
  34388. /* set content for verifying detached signatures */
  34389. pkcs7->content = (byte*)testVectors[i].content;
  34390. pkcs7->contentSz = testVectors[i].contentSz;
  34391. }
  34392. ret = wc_PKCS7_VerifySignedData(pkcs7, out, outSz);
  34393. if (ret < 0)
  34394. ERROR_OUT(-12526, out);
  34395. /* verify contentType extracted successfully for custom content types */
  34396. if (testVectors[i].contentTypeSz > 0) {
  34397. if (pkcs7->contentTypeSz != testVectors[i].contentTypeSz) {
  34398. ERROR_OUT(-12527, out);
  34399. } else if (XMEMCMP(pkcs7->contentType, testVectors[i].contentType,
  34400. pkcs7->contentTypeSz) != 0) {
  34401. ERROR_OUT(-12528, out);
  34402. }
  34403. }
  34404. if (pkcs7->singleCert == NULL || pkcs7->singleCertSz == 0)
  34405. ERROR_OUT(-12529, out);
  34406. {
  34407. /* check getting signed attributes */
  34408. #ifndef NO_SHA
  34409. byte buf[(WC_SHA_DIGEST_SIZE + 1) * 2 + 1];
  34410. #else
  34411. byte buf[(WC_SHA256_DIGEST_SIZE + 1) * 2 + 1];
  34412. #endif
  34413. byte* oidPt = transIdOid + 2; /* skip object id tag and size */
  34414. int oidSz = (int)sizeof(transIdOid) - 2;
  34415. int bufSz = 0;
  34416. if (testVectors[i].signedAttribs != NULL &&
  34417. wc_PKCS7_GetAttributeValue(pkcs7, oidPt, oidSz,
  34418. NULL, (word32*)&bufSz) != LENGTH_ONLY_E)
  34419. ERROR_OUT(-12530, out);
  34420. if (bufSz > (int)sizeof(buf))
  34421. ERROR_OUT(-12531, out);
  34422. bufSz = wc_PKCS7_GetAttributeValue(pkcs7, oidPt, oidSz,
  34423. buf, (word32*)&bufSz);
  34424. if ((testVectors[i].signedAttribs != NULL && bufSz < 0) ||
  34425. (testVectors[i].signedAttribs == NULL && bufSz > 0))
  34426. ERROR_OUT(-12532, out);
  34427. }
  34428. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  34429. file = XFOPEN("./pkcs7cert.der", "wb");
  34430. if (!file)
  34431. ERROR_OUT(-12533, out);
  34432. ret = (int)XFWRITE(pkcs7->singleCert, 1, pkcs7->singleCertSz, file);
  34433. XFCLOSE(file);
  34434. #endif /* PKCS7_OUTPUT_TEST_BUNDLES */
  34435. }
  34436. out:
  34437. if (pkcs7 != NULL)
  34438. wc_PKCS7_Free(pkcs7);
  34439. if (out != NULL)
  34440. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34441. if (testVectors != NULL)
  34442. XFREE(testVectors, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34443. wc_FreeRng(&rng);
  34444. if (ret > 0)
  34445. return 0;
  34446. (void)rsaClientCertBuf;
  34447. (void)rsaClientCertBufSz;
  34448. (void)rsaClientPrivKeyBuf;
  34449. (void)rsaClientPrivKeyBufSz;
  34450. (void)rsaServerCertBuf;
  34451. (void)rsaServerCertBufSz;
  34452. (void)rsaServerPrivKeyBuf;
  34453. (void)rsaServerPrivKeyBufSz;
  34454. (void)rsaCaCertBuf;
  34455. (void)rsaCaCertBufSz;
  34456. (void)rsaCaPrivKeyBuf;
  34457. (void)rsaCaPrivKeyBufSz;
  34458. (void)eccClientCertBuf;
  34459. (void)eccClientCertBufSz;
  34460. (void)eccClientPrivKeyBuf;
  34461. (void)eccClientPrivKeyBufSz;
  34462. return ret;
  34463. }
  34464. static int pkcs7signed_run_SingleShotVectors(
  34465. byte* rsaClientCertBuf, word32 rsaClientCertBufSz,
  34466. byte* rsaClientPrivKeyBuf, word32 rsaClientPrivKeyBufSz,
  34467. byte* rsaServerCertBuf, word32 rsaServerCertBufSz,
  34468. byte* rsaServerPrivKeyBuf, word32 rsaServerPrivKeyBufSz,
  34469. byte* rsaCaCertBuf, word32 rsaCaCertBufSz,
  34470. byte* rsaCaPrivKeyBuf, word32 rsaCaPrivKeyBufSz,
  34471. byte* eccClientCertBuf, word32 eccClientCertBufSz,
  34472. byte* eccClientPrivKeyBuf, word32 eccClientPrivKeyBufSz)
  34473. {
  34474. int ret, testSz = 0, i;
  34475. int encodedSz;
  34476. byte* out = NULL;
  34477. word32 outSz;
  34478. WC_RNG rng;
  34479. PKCS7* pkcs7 = NULL;
  34480. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  34481. XFILE file;
  34482. #endif
  34483. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA) && \
  34484. !defined(NO_PKCS7_ENCRYPTED_DATA)
  34485. byte* encryptedTmp = NULL;
  34486. int encryptedTmpSz;
  34487. #endif
  34488. WOLFSSL_SMALL_STACK_STATIC const byte data[] = { /* Hello World */
  34489. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  34490. 0x72,0x6c,0x64
  34491. };
  34492. #if !defined(NO_PKCS7_ENCRYPTED_DATA) && \
  34493. defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_256)
  34494. static byte aes256Key[] = {
  34495. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  34496. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  34497. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  34498. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  34499. };
  34500. #endif
  34501. static byte messageTypeOid[] =
  34502. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  34503. 0x09, 0x02 };
  34504. static byte messageType[] = { 0x13, 2, '1', '9' };
  34505. PKCS7Attrib attribs[] =
  34506. {
  34507. { messageTypeOid, sizeof(messageTypeOid), messageType,
  34508. sizeof(messageType) },
  34509. };
  34510. #define MAX_TESTVECTORS_LEN 19
  34511. #define ADD_PKCS7SIGNEDVECTOR(...) { \
  34512. pkcs7SignedVector _this_vector = { __VA_ARGS__ }; \
  34513. if (testSz == MAX_TESTVECTORS_LEN) { \
  34514. ret = -12568; \
  34515. goto out; \
  34516. } \
  34517. XMEMCPY(&testVectors[testSz++], &_this_vector, sizeof _this_vector);\
  34518. }
  34519. pkcs7SignedVector *testVectors = NULL;
  34520. XMEMSET(&rng, 0, sizeof(rng));
  34521. testVectors = (pkcs7SignedVector *)XMALLOC(MAX_TESTVECTORS_LEN * sizeof(*testVectors),
  34522. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34523. if (testVectors == NULL) {
  34524. ret = -12567;
  34525. goto out;
  34526. }
  34527. {
  34528. #ifndef NO_RSA
  34529. #ifndef NO_SHA256
  34530. /* Signed FirmwarePkgData, RSA, SHA256, no attribs */
  34531. ADD_PKCS7SIGNEDVECTOR(
  34532. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  34533. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  34534. NULL, 0,
  34535. "pkcs7signedFirmwarePkgData_RSA_SHA256_noattr.der", 0, NULL, 0, 0,
  34536. 0, 0, NULL, 0, NULL, 0, 0);
  34537. /* Signed FirmwarePkgData, RSA, SHA256, attrs */
  34538. ADD_PKCS7SIGNEDVECTOR(
  34539. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  34540. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  34541. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  34542. "pkcs7signedFirmwarePkgData_RSA_SHA256.der", 0, NULL, 0, 0, 0, 0,
  34543. NULL, 0, NULL, 0, 0);
  34544. /* Signed FirmwarePkgData, RSA, SHA256, SubjectKeyIdentifier, attrs */
  34545. ADD_PKCS7SIGNEDVECTOR(
  34546. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  34547. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  34548. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  34549. "pkcs7signedFirmwarePkgData_RSA_SHA256_SKID.der", 0, NULL,
  34550. 0, CMS_SKID, 0, 0, NULL, 0, NULL, 0, 0);
  34551. /* Signed FirmwraePkgData, RSA, SHA256, server cert and ca cert, attr */
  34552. ADD_PKCS7SIGNEDVECTOR(
  34553. data, (word32)sizeof(data), SHA256h, RSAk, rsaServerPrivKeyBuf,
  34554. rsaServerPrivKeyBufSz, rsaServerCertBuf, rsaServerCertBufSz,
  34555. rsaCaCertBuf, rsaCaCertBufSz,
  34556. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  34557. "pkcs7signedFirmwarePkgData_RSA_SHA256_with_ca_cert.der", 0, NULL,
  34558. 0, 0, 0, 0, NULL, 0, NULL, 0, 0);
  34559. #if !defined(NO_PKCS7_ENCRYPTED_DATA) && \
  34560. defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_256)
  34561. /* Signed Encrypted FirmwarePkgData, RSA, SHA256, no attribs */
  34562. ADD_PKCS7SIGNEDVECTOR(
  34563. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  34564. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  34565. NULL, 0,
  34566. "pkcs7signedEncryptedFirmwarePkgData_RSA_SHA256_noattr.der", 0,
  34567. NULL, 0, 0, AES256CBCb, 1, aes256Key, sizeof(aes256Key), NULL, 0, 0);
  34568. /* Signed Encrypted FirmwarePkgData, RSA, SHA256, attribs */
  34569. ADD_PKCS7SIGNEDVECTOR(
  34570. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  34571. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  34572. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  34573. "pkcs7signedEncryptedFirmwarePkgData_RSA_SHA256.der", 0,
  34574. NULL, 0, 0, AES256CBCb, 1, aes256Key, sizeof(aes256Key),
  34575. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)), 0);
  34576. #endif /* WOLFSSL_AES_256 && !NO_PKCS7_ENCRYPTED_DATA */
  34577. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  34578. /* Signed Compressed FirmwarePkgData, RSA, SHA256, no attribs */
  34579. ADD_PKCS7SIGNEDVECTOR(
  34580. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  34581. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  34582. NULL, 0,
  34583. "pkcs7signedCompressedFirmwarePkgData_RSA_SHA256_noattr.der", 0,
  34584. NULL, 0, 0, 0, 2, NULL, 0, NULL, 0, 0);
  34585. /* Signed Compressed FirmwarePkgData, RSA, SHA256, attribs */
  34586. ADD_PKCS7SIGNEDVECTOR(
  34587. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  34588. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  34589. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  34590. "pkcs7signedCompressedFirmwarePkgData_RSA_SHA256.der", 0,
  34591. NULL, 0, 0, 0, 2, NULL, 0, NULL, 0, 0);
  34592. #ifndef NO_PKCS7_ENCRYPTED_DATA
  34593. /* Signed Encrypted Compressed FirmwarePkgData, RSA, SHA256,
  34594. no attribs */
  34595. ADD_PKCS7SIGNEDVECTOR(
  34596. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  34597. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  34598. NULL, 0,
  34599. "pkcs7signedEncryptedCompressedFirmwarePkgData_RSA_SHA256_noattr.der",
  34600. 0, NULL, 0, 0, AES256CBCb, 3, aes256Key, sizeof(aes256Key), NULL,
  34601. 0, 0);
  34602. /* Signed Encrypted Compressed FirmwarePkgData, RSA, SHA256,
  34603. attribs */
  34604. ADD_PKCS7SIGNEDVECTOR(
  34605. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  34606. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  34607. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  34608. "pkcs7signedEncryptedCompressedFirmwarePkgData_RSA_SHA256.der",
  34609. 0, NULL, 0, 0, AES256CBCb, 3, aes256Key, sizeof(aes256Key),
  34610. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)), 0);
  34611. #endif /* !NO_PKCS7_ENCRYPTED_DATA */
  34612. #endif /* HAVE_LIBZ && !NO_PKCS7_COMPRESSED_DATA */
  34613. #endif /* NO_SHA256 */
  34614. #endif /* NO_RSA */
  34615. #ifdef HAVE_ECC
  34616. #ifndef NO_SHA256
  34617. /* Signed FirmwarePkgData, ECDSA, SHA256, no attribs */
  34618. ADD_PKCS7SIGNEDVECTOR(
  34619. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  34620. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  34621. NULL, 0,
  34622. "pkcs7signedFirmwarePkgData_ECDSA_SHA256_noattr.der", 0, NULL,
  34623. 0, 0, 0, 0, NULL, 0, NULL, 0, 0);
  34624. /* Signed FirmwarePkgData, ECDSA, SHA256, attribs */
  34625. ADD_PKCS7SIGNEDVECTOR(
  34626. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  34627. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  34628. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  34629. "pkcs7signedFirmwarePkgData_ECDSA_SHA256.der", 0, NULL,
  34630. 0, 0, 0, 0, NULL, 0, NULL, 0, 0);
  34631. /* Signed FirmwarePkgData, ECDSA, SHA256, SubjectKeyIdentifier, attr */
  34632. ADD_PKCS7SIGNEDVECTOR(
  34633. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  34634. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  34635. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  34636. "pkcs7signedFirmwarePkgData_ECDSA_SHA256_SKID.der", 0, NULL,
  34637. 0, CMS_SKID, 0, 0, NULL, 0, NULL, 0, 0);
  34638. #if !defined(NO_PKCS7_ENCRYPTED_DATA) && \
  34639. defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_256)
  34640. /* Signed Encrypted FirmwarePkgData, ECDSA, SHA256, no attribs */
  34641. ADD_PKCS7SIGNEDVECTOR(
  34642. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  34643. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  34644. NULL, 0,
  34645. "pkcs7signedEncryptedFirmwarePkgData_ECDSA_SHA256_noattr.der", 0, NULL,
  34646. 0, 0, AES256CBCb, 1, aes256Key, sizeof(aes256Key), NULL, 0, 0);
  34647. /* Signed Encrypted FirmwarePkgData, ECDSA, SHA256, attribs */
  34648. ADD_PKCS7SIGNEDVECTOR(
  34649. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  34650. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  34651. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  34652. "pkcs7signedEncryptedFirmwarePkgData_ECDSA_SHA256.der", 0, NULL,
  34653. 0, 0, AES256CBCb, 1, aes256Key, sizeof(aes256Key),
  34654. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)), 0);
  34655. #endif /* WOLFSSL_AES_256 && !NO_PKCS7_ENCRYPTED_DATA */
  34656. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  34657. /* Signed Compressed FirmwarePkgData, ECDSA, SHA256, no attribs */
  34658. ADD_PKCS7SIGNEDVECTOR(
  34659. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  34660. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  34661. NULL, 0,
  34662. "pkcs7signedCompressedFirmwarePkgData_ECDSA_SHA256_noattr.der", 0, NULL,
  34663. 0, 0, 0, 2, NULL, 0, NULL, 0, 0);
  34664. /* Signed Compressed FirmwarePkgData, ECDSA, SHA256, attrib */
  34665. ADD_PKCS7SIGNEDVECTOR(
  34666. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  34667. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  34668. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  34669. "pkcs7signedCompressedFirmwarePkgData_ECDSA_SHA256.der", 0, NULL,
  34670. 0, 0, 0, 2, NULL, 0, NULL, 0, 0);
  34671. #ifndef NO_PKCS7_ENCRYPTED_DATA
  34672. /* Signed Encrypted Compressed FirmwarePkgData, ECDSA, SHA256,
  34673. no attribs */
  34674. ADD_PKCS7SIGNEDVECTOR(
  34675. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  34676. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  34677. NULL, 0,
  34678. "pkcs7signedEncryptedCompressedFirmwarePkgData_ECDSA_SHA256_noattr.der",
  34679. 0, NULL, 0, 0, AES256CBCb, 3, aes256Key, sizeof(aes256Key), NULL,
  34680. 0, 0);
  34681. /* Signed Encrypted Compressed FirmwarePkgData, ECDSA, SHA256,
  34682. attribs */
  34683. ADD_PKCS7SIGNEDVECTOR(
  34684. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  34685. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  34686. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  34687. "pkcs7signedEncryptedCompressedFirmwarePkgData_ECDSA_SHA256.der",
  34688. 0, NULL, 0, 0, AES256CBCb, 3, aes256Key, sizeof(aes256Key),
  34689. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)), 0);
  34690. #endif /* !NO_PKCS7_ENCRYPTED_DATA */
  34691. #endif /* HAVE_LIBZ && !NO_PKCS7_COMPRESSED_DATA */
  34692. #endif /* NO_SHA256 */
  34693. #endif /* HAVE_ECC */
  34694. };
  34695. #undef MAX_TESTVECTORS_LEN
  34696. #undef ADD_PKCS7SIGNEDVECTOR
  34697. outSz = FOURK_BUF;
  34698. out = (byte*)XMALLOC(outSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34699. if (out == NULL)
  34700. ERROR_OUT(-12540, out);
  34701. XMEMSET(out, 0, outSz);
  34702. ret = wc_PKCS7_PadData((byte*)data, sizeof(data), out, outSz, 16);
  34703. if (ret < 0)
  34704. ERROR_OUT(-12541, out);
  34705. #ifndef HAVE_FIPS
  34706. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  34707. #else
  34708. ret = wc_InitRng(&rng);
  34709. #endif
  34710. if (ret != 0)
  34711. ERROR_OUT(-12542, out);
  34712. for (i = 0; i < testSz; i++) {
  34713. if (pkcs7)
  34714. wc_PKCS7_Free(pkcs7);
  34715. pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
  34716. if (pkcs7 == NULL)
  34717. ERROR_OUT(-12543, out);
  34718. ret = wc_PKCS7_InitWithCert(pkcs7, testVectors[i].cert,
  34719. (word32)testVectors[i].certSz);
  34720. if (ret != 0)
  34721. ERROR_OUT(-12544, out);
  34722. /* load CA certificate, if present */
  34723. if (testVectors[i].caCert != NULL) {
  34724. ret = wc_PKCS7_AddCertificate(pkcs7, testVectors[i].caCert,
  34725. (word32)testVectors[i].caCertSz);
  34726. if (ret != 0)
  34727. ERROR_OUT(-12545, out);
  34728. }
  34729. /* set SignerIdentifier to use SubjectKeyIdentifier if desired,
  34730. default is IssuerAndSerialNumber */
  34731. if (testVectors[i].sidType == CMS_SKID) {
  34732. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID);
  34733. if (ret != 0)
  34734. ERROR_OUT(-12546, out);
  34735. }
  34736. if (testVectors[i].encCompFlag == 0) {
  34737. /* encode Signed FirmwarePkgData */
  34738. encodedSz = wc_PKCS7_EncodeSignedFPD(pkcs7,
  34739. testVectors[i].privateKey, testVectors[i].privateKeySz,
  34740. testVectors[i].signOID, testVectors[i].hashOID,
  34741. (byte*)testVectors[i].content, testVectors[i].contentSz,
  34742. testVectors[i].signedAttribs,
  34743. testVectors[i].signedAttribsSz, out, outSz);
  34744. if (encodedSz < 0)
  34745. ERROR_OUT(-12547, out);
  34746. #ifndef NO_PKCS7_ENCRYPTED_DATA
  34747. } else if (testVectors[i].encCompFlag == 1) {
  34748. /* encode Signed Encrypted FirmwarePkgData */
  34749. encodedSz = wc_PKCS7_EncodeSignedEncryptedFPD(pkcs7,
  34750. testVectors[i].encryptKey, testVectors[i].encryptKeySz,
  34751. testVectors[i].privateKey, testVectors[i].privateKeySz,
  34752. testVectors[i].encryptOID, testVectors[i].signOID,
  34753. testVectors[i].hashOID, (byte*)testVectors[i].content,
  34754. testVectors[i].contentSz, testVectors[i].unprotectedAttribs,
  34755. testVectors[i].unprotectedAttribsSz,
  34756. testVectors[i].signedAttribs,
  34757. testVectors[i].signedAttribsSz, out, outSz);
  34758. if (encodedSz <= 0)
  34759. ERROR_OUT(-12548, out);
  34760. #endif
  34761. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  34762. } else if (testVectors[i].encCompFlag == 2) {
  34763. /* encode Signed Compressed FirmwarePkgData */
  34764. encodedSz = wc_PKCS7_EncodeSignedCompressedFPD(pkcs7,
  34765. testVectors[i].privateKey, testVectors[i].privateKeySz,
  34766. testVectors[i].signOID, testVectors[i].hashOID,
  34767. (byte*)testVectors[i].content, testVectors[i].contentSz,
  34768. testVectors[i].signedAttribs,
  34769. testVectors[i].signedAttribsSz, out, outSz);
  34770. if (encodedSz <= 0)
  34771. ERROR_OUT(-12549, out);
  34772. #ifndef NO_PKCS7_ENCRYPTED_DATA
  34773. } else if (testVectors[i].encCompFlag == 3) {
  34774. /* encode Signed Encrypted Compressed FirmwarePkgData */
  34775. encodedSz = wc_PKCS7_EncodeSignedEncryptedCompressedFPD(pkcs7,
  34776. testVectors[i].encryptKey, testVectors[i].encryptKeySz,
  34777. testVectors[i].privateKey, testVectors[i].privateKeySz,
  34778. testVectors[i].encryptOID, testVectors[i].signOID,
  34779. testVectors[i].hashOID, (byte*)testVectors[i].content,
  34780. testVectors[i].contentSz, testVectors[i].unprotectedAttribs,
  34781. testVectors[i].unprotectedAttribsSz,
  34782. testVectors[i].signedAttribs,
  34783. testVectors[i].signedAttribsSz, out, outSz);
  34784. if (encodedSz <= 0)
  34785. ERROR_OUT(-12550, out);
  34786. #endif /* NO_PKCS7_ENCRYPTED_DATA */
  34787. #endif /* HAVE_LIBZ && !NO_PKCS7_COMPRESSED_DATA */
  34788. } else {
  34789. /* unsupported SignedData single-shot combination */
  34790. ERROR_OUT(-12551, out);
  34791. }
  34792. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  34793. /* write PKCS#7 to output file for more testing */
  34794. file = XFOPEN(testVectors[i].outFileName, "wb");
  34795. if (!file)
  34796. ERROR_OUT(-12552, out);
  34797. ret = (int)XFWRITE(out, 1, encodedSz, file);
  34798. XFCLOSE(file);
  34799. file = NULL;
  34800. if (ret != (int)encodedSz)
  34801. ERROR_OUT(-12553, out);
  34802. #endif /* PKCS7_OUTPUT_TEST_BUNDLES */
  34803. wc_PKCS7_Free(pkcs7);
  34804. pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
  34805. if (pkcs7 == NULL)
  34806. ERROR_OUT(-12554, out);
  34807. wc_PKCS7_InitWithCert(pkcs7, NULL, 0);
  34808. ret = wc_PKCS7_VerifySignedData(pkcs7, out, outSz);
  34809. if (ret < 0)
  34810. ERROR_OUT(-12555, out);
  34811. #ifndef NO_PKCS7_STREAM
  34812. {
  34813. word32 z;
  34814. for (z = 0; z < outSz && ret != 0; z++) {
  34815. ret = wc_PKCS7_VerifySignedData(pkcs7, out + z, 1);
  34816. if (ret < 0 && ret != WC_PKCS7_WANT_READ_E) {
  34817. printf("unexpected error %d\n", ret);
  34818. ERROR_OUT(-12556, out);
  34819. }
  34820. }
  34821. }
  34822. #endif
  34823. if (pkcs7->singleCert == NULL || pkcs7->singleCertSz == 0)
  34824. ERROR_OUT(-12557, out);
  34825. if (testVectors[i].encCompFlag == 0) {
  34826. /* verify decoded content matches expected */
  34827. if ((pkcs7->contentSz != testVectors[i].contentSz) ||
  34828. XMEMCMP(pkcs7->content, testVectors[i].content,
  34829. pkcs7->contentSz)) {
  34830. ERROR_OUT(-12558, out);
  34831. }
  34832. }
  34833. #ifndef NO_PKCS7_ENCRYPTED_DATA
  34834. else if (testVectors[i].encCompFlag == 1) {
  34835. /* decrypt inner encryptedData */
  34836. pkcs7->encryptionKey = testVectors[i].encryptKey;
  34837. pkcs7->encryptionKeySz = testVectors[i].encryptKeySz;
  34838. ret = wc_PKCS7_DecodeEncryptedData(pkcs7, pkcs7->content,
  34839. pkcs7->contentSz, out, outSz);
  34840. if (ret < 0)
  34841. ERROR_OUT(-12559, out);
  34842. /* compare decrypted to expected */
  34843. if (((word32)ret != testVectors[i].contentSz) ||
  34844. XMEMCMP(out, testVectors[i].content, ret))
  34845. ERROR_OUT(-12560, out);
  34846. }
  34847. #endif
  34848. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  34849. else if (testVectors[i].encCompFlag == 2) {
  34850. /* decompress inner compressedData */
  34851. ret = wc_PKCS7_DecodeCompressedData(pkcs7, pkcs7->content,
  34852. pkcs7->contentSz, out, outSz);
  34853. if (ret < 0)
  34854. ERROR_OUT(-12561, out);
  34855. /* compare decompressed to expected */
  34856. if (((word32)ret != testVectors[i].contentSz) ||
  34857. XMEMCMP(out, testVectors[i].content, ret))
  34858. ERROR_OUT(-12562, out);
  34859. }
  34860. #ifndef NO_PKCS7_ENCRYPTED_DATA
  34861. else if (testVectors[i].encCompFlag == 3) {
  34862. encryptedTmpSz = FOURK_BUF;
  34863. encryptedTmp = (byte*)XMALLOC(encryptedTmpSz, HEAP_HINT,
  34864. DYNAMIC_TYPE_TMP_BUFFER);
  34865. if (encryptedTmp == NULL)
  34866. ERROR_OUT(-12563, out);
  34867. XMEMSET(encryptedTmp, 0, encryptedTmpSz);
  34868. /* decrypt inner encryptedData */
  34869. pkcs7->encryptionKey = testVectors[i].encryptKey;
  34870. pkcs7->encryptionKeySz = testVectors[i].encryptKeySz;
  34871. encryptedTmpSz = wc_PKCS7_DecodeEncryptedData(pkcs7, pkcs7->content,
  34872. pkcs7->contentSz, encryptedTmp,
  34873. encryptedTmpSz);
  34874. if (encryptedTmpSz < 0 || pkcs7->contentOID != COMPRESSED_DATA)
  34875. ERROR_OUT(-12564, out);
  34876. /* decompress inner compressedData */
  34877. ret = wc_PKCS7_DecodeCompressedData(pkcs7, encryptedTmp,
  34878. encryptedTmpSz, out, outSz);
  34879. if (ret < 0)
  34880. ERROR_OUT(-12565, out);
  34881. /* compare decompressed to expected */
  34882. if (((word32)ret != testVectors[i].contentSz) ||
  34883. XMEMCMP(out, testVectors[i].content, ret))
  34884. ERROR_OUT(-12566, out);
  34885. }
  34886. #endif /* NO_PKCS7_ENCRYPTED_DATA */
  34887. #endif /* HAVE_LIBZ && !NO_PKCS7_COMPRESSED_DATA */
  34888. }
  34889. out:
  34890. if (pkcs7 != NULL)
  34891. wc_PKCS7_Free(pkcs7);
  34892. if (out != NULL)
  34893. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34894. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA) && \
  34895. !defined(NO_PKCS7_ENCRYPTED_DATA)
  34896. if (encryptedTmp != NULL)
  34897. XFREE(encryptedTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34898. #endif
  34899. if (testVectors != NULL)
  34900. XFREE(testVectors, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34901. wc_FreeRng(&rng);
  34902. if (ret > 0)
  34903. return 0;
  34904. (void)eccClientCertBuf;
  34905. (void)eccClientCertBufSz;
  34906. (void)eccClientPrivKeyBuf;
  34907. (void)eccClientPrivKeyBufSz;
  34908. (void)rsaClientCertBuf;
  34909. (void)rsaClientCertBufSz;
  34910. (void)rsaClientPrivKeyBuf;
  34911. (void)rsaClientPrivKeyBufSz;
  34912. (void)rsaServerCertBuf;
  34913. (void)rsaServerCertBufSz;
  34914. (void)rsaServerPrivKeyBuf;
  34915. (void)rsaServerPrivKeyBufSz;
  34916. (void)rsaCaCertBuf;
  34917. (void)rsaCaCertBufSz;
  34918. (void)rsaCaPrivKeyBuf;
  34919. (void)rsaCaPrivKeyBufSz;
  34920. return ret;
  34921. }
  34922. WOLFSSL_TEST_SUBROUTINE int pkcs7signed_test(void)
  34923. {
  34924. int ret = 0;
  34925. byte* rsaClientCertBuf = NULL;
  34926. byte* rsaServerCertBuf = NULL;
  34927. byte* rsaCaCertBuf = NULL;
  34928. byte* eccClientCertBuf = NULL;
  34929. byte* rsaClientPrivKeyBuf = NULL;
  34930. byte* rsaServerPrivKeyBuf = NULL;
  34931. byte* rsaCaPrivKeyBuf = NULL;
  34932. byte* eccClientPrivKeyBuf = NULL;
  34933. word32 rsaClientCertBufSz = 0;
  34934. word32 rsaServerCertBufSz = 0;
  34935. word32 rsaCaCertBufSz = 0;
  34936. word32 eccClientCertBufSz = 0;
  34937. word32 rsaClientPrivKeyBufSz = 0;
  34938. word32 rsaServerPrivKeyBufSz = 0;
  34939. word32 rsaCaPrivKeyBufSz = 0;
  34940. word32 eccClientPrivKeyBufSz = 0;
  34941. #ifndef NO_RSA
  34942. /* read client RSA cert and key in DER format */
  34943. rsaClientCertBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  34944. DYNAMIC_TYPE_TMP_BUFFER);
  34945. if (rsaClientCertBuf == NULL)
  34946. ret = -12600;
  34947. rsaClientPrivKeyBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  34948. DYNAMIC_TYPE_TMP_BUFFER);
  34949. if (ret == 0 && rsaClientPrivKeyBuf == NULL) {
  34950. ret = -12601;
  34951. }
  34952. rsaClientCertBufSz = FOURK_BUF;
  34953. rsaClientPrivKeyBufSz = FOURK_BUF;
  34954. /* read server RSA cert and key in DER format */
  34955. rsaServerCertBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  34956. DYNAMIC_TYPE_TMP_BUFFER);
  34957. if (ret == 0 && rsaServerCertBuf == NULL)
  34958. ret = -12602;
  34959. rsaServerPrivKeyBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  34960. DYNAMIC_TYPE_TMP_BUFFER);
  34961. if (ret == 0 && rsaServerPrivKeyBuf == NULL) {
  34962. ret = -12603;
  34963. }
  34964. rsaServerCertBufSz = FOURK_BUF;
  34965. rsaServerPrivKeyBufSz = FOURK_BUF;
  34966. /* read CA RSA cert and key in DER format, for use with server cert */
  34967. rsaCaCertBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  34968. DYNAMIC_TYPE_TMP_BUFFER);
  34969. if (ret == 0 && rsaCaCertBuf == NULL)
  34970. ret = -12604;
  34971. rsaCaPrivKeyBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  34972. DYNAMIC_TYPE_TMP_BUFFER);
  34973. if (ret == 0 && rsaCaPrivKeyBuf == NULL) {
  34974. ret = -12605;
  34975. }
  34976. rsaCaCertBufSz = FOURK_BUF;
  34977. rsaCaPrivKeyBufSz = FOURK_BUF;
  34978. #endif /* NO_RSA */
  34979. #ifdef HAVE_ECC
  34980. /* read client ECC cert and key in DER format */
  34981. eccClientCertBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  34982. DYNAMIC_TYPE_TMP_BUFFER);
  34983. if (ret == 0 && eccClientCertBuf == NULL) {
  34984. ret = -12606;
  34985. }
  34986. eccClientPrivKeyBuf =(byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  34987. DYNAMIC_TYPE_TMP_BUFFER);
  34988. if (ret == 0 && eccClientPrivKeyBuf == NULL) {
  34989. ret = -12607;
  34990. }
  34991. eccClientCertBufSz = FOURK_BUF;
  34992. eccClientPrivKeyBufSz = FOURK_BUF;
  34993. #endif /* HAVE_ECC */
  34994. if (ret >= 0)
  34995. ret = pkcs7_load_certs_keys(rsaClientCertBuf, &rsaClientCertBufSz,
  34996. rsaClientPrivKeyBuf, &rsaClientPrivKeyBufSz,
  34997. rsaServerCertBuf, &rsaServerCertBufSz,
  34998. rsaServerPrivKeyBuf, &rsaServerPrivKeyBufSz,
  34999. rsaCaCertBuf, &rsaCaCertBufSz,
  35000. rsaCaPrivKeyBuf, &rsaCaPrivKeyBufSz,
  35001. eccClientCertBuf, &eccClientCertBufSz,
  35002. eccClientPrivKeyBuf, &eccClientPrivKeyBufSz);
  35003. if (ret < 0) {
  35004. ret = -12608;
  35005. }
  35006. if (ret >= 0)
  35007. ret = pkcs7signed_run_vectors(rsaClientCertBuf, (word32)rsaClientCertBufSz,
  35008. rsaClientPrivKeyBuf, (word32)rsaClientPrivKeyBufSz,
  35009. rsaServerCertBuf, (word32)rsaServerCertBufSz,
  35010. rsaServerPrivKeyBuf, (word32)rsaServerPrivKeyBufSz,
  35011. rsaCaCertBuf, (word32)rsaCaCertBufSz,
  35012. rsaCaPrivKeyBuf, (word32)rsaCaPrivKeyBufSz,
  35013. eccClientCertBuf, (word32)eccClientCertBufSz,
  35014. eccClientPrivKeyBuf, (word32)eccClientPrivKeyBufSz);
  35015. if (ret >= 0)
  35016. ret = pkcs7signed_run_SingleShotVectors(
  35017. rsaClientCertBuf, (word32)rsaClientCertBufSz,
  35018. rsaClientPrivKeyBuf, (word32)rsaClientPrivKeyBufSz,
  35019. rsaServerCertBuf, (word32)rsaServerCertBufSz,
  35020. rsaServerPrivKeyBuf, (word32)rsaServerPrivKeyBufSz,
  35021. rsaCaCertBuf, (word32)rsaCaCertBufSz,
  35022. rsaCaPrivKeyBuf, (word32)rsaCaPrivKeyBufSz,
  35023. eccClientCertBuf, (word32)eccClientCertBufSz,
  35024. eccClientPrivKeyBuf, (word32)eccClientPrivKeyBufSz);
  35025. #if !defined(NO_RSA) && !defined(NO_AES) && defined(HAVE_AES_CBC)
  35026. if (ret >= 0)
  35027. ret = pkcs7callback_test(
  35028. rsaClientCertBuf, (word32)rsaClientCertBufSz,
  35029. rsaClientPrivKeyBuf, (word32)rsaClientPrivKeyBufSz);
  35030. #endif
  35031. XFREE(rsaClientCertBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35032. XFREE(rsaClientPrivKeyBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35033. XFREE(rsaServerCertBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35034. XFREE(rsaServerPrivKeyBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35035. XFREE(rsaCaCertBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35036. XFREE(rsaCaPrivKeyBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35037. XFREE(eccClientCertBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35038. XFREE(eccClientPrivKeyBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35039. return ret;
  35040. }
  35041. #endif /* HAVE_PKCS7 */
  35042. #ifdef HAVE_VALGRIND
  35043. /* Need a static build to have access to symbols. */
  35044. /* Maximum number of bytes in a number to test. */
  35045. #define MP_MAX_TEST_BYTE_LEN 32
  35046. static int randNum(mp_int* n, int len, WC_RNG* rng, void* heap)
  35047. {
  35048. byte d[MP_MAX_TEST_BYTE_LEN];
  35049. int ret;
  35050. (void)heap;
  35051. do {
  35052. ret = wc_RNG_GenerateBlock(rng, d, len);
  35053. if (ret != 0)
  35054. return ret;
  35055. ret = mp_read_unsigned_bin(n, d, len);
  35056. if (ret != 0)
  35057. return ret;
  35058. } while (mp_iszero(n));
  35059. return 0;
  35060. }
  35061. #if defined(WOLFSSL_SP_MATH_ALL) || !defined(USE_FAST_MATH)
  35062. static int mp_test_div_3(mp_int* a, mp_int* r, WC_RNG* rng)
  35063. {
  35064. int i, j;
  35065. mp_digit rem;
  35066. mp_digit rem2;
  35067. #if (defined(WOLFSSL_SP_MATH_ALL) && !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
  35068. defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  35069. for (i = 0; i < 10; i++) {
  35070. for (j = 1; j < 10; j++) {
  35071. if (randNum(a, j, rng, NULL) != 0)
  35072. return -12620;
  35073. if (mp_div_3(a, r, &rem) != 0)
  35074. return -12621;
  35075. if (mp_mul_d(r, 3, r) != 0)
  35076. return -12622;
  35077. if (mp_add_d(r, rem, r) != 0)
  35078. return -12623;
  35079. if (mp_cmp(r, a) != MP_EQ)
  35080. return -12624;
  35081. }
  35082. }
  35083. if (mp_div_3(a, r, &rem) != 0)
  35084. return -12625;
  35085. if (mp_div_3(a, a, NULL) != 0)
  35086. return -12626;
  35087. if (mp_cmp(r, a) != MP_EQ)
  35088. return -12627;
  35089. #endif
  35090. #if defined(WOLFSSL_SP_MATH_ALL)
  35091. if (mp_div_d(a, 10, r, &rem) != 0)
  35092. return -12628;
  35093. if (mp_div_d(a, 10, a, NULL) != 0)
  35094. return -12629;
  35095. if (mp_cmp(r, a) != MP_EQ)
  35096. return -12630;
  35097. if (mp_div_d(a, 12, r, &rem) != 0)
  35098. return -12631;
  35099. if (mp_div_d(a, 12, a, NULL) != 0)
  35100. return -12632;
  35101. if (mp_cmp(r, a) != MP_EQ)
  35102. return -12633;
  35103. if (mp_div_d(a, (mp_digit)1 << (DIGIT_BIT / 2), r, &rem) != 0)
  35104. return -12634;
  35105. if (mp_div_d(a, (mp_digit)1 << (DIGIT_BIT / 2), NULL, &rem2) != 0)
  35106. return -12635;
  35107. if (mp_div_d(a, (mp_digit)1 << (DIGIT_BIT / 2), a, NULL) != 0)
  35108. return -12636;
  35109. if (mp_cmp(r, a) != MP_EQ)
  35110. return -12637;
  35111. if (rem != rem2)
  35112. return -12638;
  35113. #endif
  35114. (void)a;
  35115. (void)r;
  35116. (void)rng;
  35117. (void)i;
  35118. (void)j;
  35119. (void)rem;
  35120. (void)rem2;
  35121. return 0;
  35122. }
  35123. #endif /* WOLFSSL_SP_MATH || !USE_FAST_MATH */
  35124. #if (defined(WOLFSSL_SP_MATH_ALL) && !defined(NO_RSA) && \
  35125. !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
  35126. (!defined WOLFSSL_SP_MATH && !defined(WOLFSSL_SP_MATH_ALL) && \
  35127. (defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)))
  35128. static int mp_test_radix_10(mp_int* a, mp_int* r, WC_RNG* rng)
  35129. {
  35130. int ret;
  35131. int i, j;
  35132. int size;
  35133. char str[30];
  35134. WOLFSSL_SMALL_STACK_STATIC const char* badStr1 = "A";
  35135. WOLFSSL_SMALL_STACK_STATIC const char* badStr2 = "a";
  35136. WOLFSSL_SMALL_STACK_STATIC const char* badStr3 = " ";
  35137. WOLFSSL_SMALL_STACK_STATIC const char* zeros = "000";
  35138. WOLFSSL_SMALL_STACK_STATIC const char* empty = "";
  35139. for (i = 0; i < 10; i++) {
  35140. for (j = 2; j < 12; j++) {
  35141. if (randNum(a, j, rng, NULL) != 0)
  35142. return -12640;
  35143. if (mp_radix_size(a, MP_RADIX_DEC, &size) != MP_OKAY)
  35144. return -12641;
  35145. if (mp_toradix(a, str, MP_RADIX_DEC) != MP_OKAY)
  35146. return -12660;
  35147. if ((int)XSTRLEN(str) != size - 1)
  35148. return -12642;
  35149. if (mp_read_radix(r, str, MP_RADIX_DEC) != MP_OKAY)
  35150. return -12661;
  35151. if (mp_cmp(a, r) != MP_EQ)
  35152. return -12643;
  35153. }
  35154. }
  35155. if (mp_read_radix(r, badStr1, MP_RADIX_DEC) != MP_VAL)
  35156. return -12644;
  35157. if (mp_read_radix(r, badStr2, MP_RADIX_DEC) != MP_VAL)
  35158. return -12645;
  35159. if (mp_read_radix(r, badStr3, MP_RADIX_DEC) != MP_VAL)
  35160. return -12646;
  35161. if (mp_read_radix(r, zeros, MP_RADIX_DEC) != MP_OKAY)
  35162. return -12647;
  35163. if (!mp_iszero(r))
  35164. return -12648;
  35165. mp_set(r, 1);
  35166. if (mp_read_radix(r, empty, MP_RADIX_DEC) != MP_OKAY)
  35167. return -12649;
  35168. if (!mp_iszero(r))
  35169. return -12650;
  35170. mp_zero(a);
  35171. ret = mp_radix_size(a, MP_RADIX_DEC, &size);
  35172. if (ret != 0)
  35173. return -12651;
  35174. if (size != 2)
  35175. return -12652;
  35176. ret = mp_toradix(a, str, MP_RADIX_DEC);
  35177. if (ret != 0)
  35178. return -12653;
  35179. if ((int)XSTRLEN(str) != size - 1)
  35180. return -12654;
  35181. ret = mp_read_radix(r, str, MP_RADIX_DEC);
  35182. if (ret != 0)
  35183. return -12655;
  35184. if (!mp_iszero(r))
  35185. return -12656;
  35186. return 0;
  35187. }
  35188. #endif
  35189. #if defined(WOLFSSL_SP_MATH_ALL) || (!defined WOLFSSL_SP_MATH && \
  35190. defined(HAVE_ECC))
  35191. static int mp_test_radix_16(mp_int* a, mp_int* r, WC_RNG* rng)
  35192. {
  35193. int ret;
  35194. int i, j;
  35195. int size;
  35196. char str[30];
  35197. #if defined(WOLFSSL_SP_MATH) || defined(USE_FAST_MATH)
  35198. static char longStr[2 * sizeof(a->dp) + 2];
  35199. #endif
  35200. WOLFSSL_SMALL_STACK_STATIC const char* badStr1 = " ";
  35201. WOLFSSL_SMALL_STACK_STATIC const char* badStr2 = "}";
  35202. WOLFSSL_SMALL_STACK_STATIC const char* empty = "";
  35203. for (i = 0; i < 10; i++) {
  35204. for (j = 2; j < 12; j++) {
  35205. if (randNum(a, j, rng, NULL) != 0)
  35206. return -12660;
  35207. mp_radix_size(a, MP_RADIX_HEX, &size);
  35208. mp_toradix(a, str, MP_RADIX_HEX);
  35209. if ((int)XSTRLEN(str) != size - 1)
  35210. return -12661;
  35211. mp_read_radix(r, str, MP_RADIX_HEX);
  35212. if (mp_cmp(a, r) != MP_EQ)
  35213. return -12662;
  35214. }
  35215. }
  35216. if (mp_read_radix(r, badStr1, MP_RADIX_HEX) != MP_VAL)
  35217. return -12663;
  35218. if (mp_read_radix(r, badStr2, MP_RADIX_HEX) != MP_VAL)
  35219. return -12664;
  35220. mp_set(r, 1);
  35221. if (mp_read_radix(r, empty, MP_RADIX_HEX) != MP_OKAY)
  35222. return -12665;
  35223. if (!mp_iszero(r))
  35224. return -12666;
  35225. #if defined(WOLFSSL_SP_MATH) || defined(USE_FAST_MATH)
  35226. /* Fixed MP data size - string can be too long. */
  35227. longStr[0] = '8';
  35228. XMEMSET(longStr+1, '0', sizeof(longStr) - 2);
  35229. longStr[sizeof(longStr)-1] = '\0';
  35230. if (mp_read_radix(r, longStr, MP_RADIX_HEX) != MP_VAL)
  35231. return -12667;
  35232. #endif
  35233. mp_zero(a);
  35234. ret = mp_radix_size(a, MP_RADIX_HEX, &size);
  35235. if (ret != 0)
  35236. return -12668;
  35237. #ifndef WC_DISABLE_RADIX_ZERO_PAD
  35238. if (size != 3)
  35239. #else
  35240. if (size != 2)
  35241. #endif
  35242. return -12669;
  35243. ret = mp_toradix(a, str, MP_RADIX_HEX);
  35244. if (ret != 0)
  35245. return -12670;
  35246. if ((int)XSTRLEN(str) != size - 1)
  35247. return -12671;
  35248. ret = mp_read_radix(r, str, MP_RADIX_HEX);
  35249. if (ret != 0)
  35250. return -12672;
  35251. if (!mp_iszero(r))
  35252. return -12673;
  35253. #ifdef WOLFSSL_SP_MATH
  35254. ret = mp_toradix(a, str, 8);
  35255. if (ret != MP_VAL)
  35256. return -12674;
  35257. ret = mp_radix_size(a, 8, &size);
  35258. if (ret != MP_VAL)
  35259. return -12675;
  35260. #endif
  35261. return 0;
  35262. }
  35263. #endif
  35264. static int mp_test_shift(mp_int* a, mp_int* r1, WC_RNG* rng)
  35265. {
  35266. int i;
  35267. if (randNum(a, 4, rng, NULL) != 0)
  35268. return -12680;
  35269. for (i = 0; i < 4; i++) {
  35270. mp_copy(r1, a);
  35271. if (mp_lshd(r1, i) != MP_OKAY)
  35272. return -12681;
  35273. #ifndef WOLFSSL_SP_MATH
  35274. mp_rshd(r1, i);
  35275. #else
  35276. mp_rshb(r1, i * SP_WORD_SIZE);
  35277. #endif
  35278. if (mp_cmp(a, r1) != MP_EQ)
  35279. return -12682;
  35280. }
  35281. #ifndef WOLFSSL_SP_MATH
  35282. for (i = 0; i < DIGIT_BIT+1; i++) {
  35283. if (mp_mul_2d(a, i, r1) != MP_OKAY)
  35284. return -12683;
  35285. mp_rshb(r1, i);
  35286. if (mp_cmp(a, r1) != MP_EQ)
  35287. return -12684;
  35288. }
  35289. #endif
  35290. return 0;
  35291. }
  35292. static int mp_test_add_sub_d(mp_int* a, mp_int* r1)
  35293. {
  35294. int i, j;
  35295. for (i = 0; i <= DIGIT_BIT * 2; i++) {
  35296. mp_zero(a);
  35297. mp_set_bit(a, i);
  35298. if (a->used != (i + DIGIT_BIT) / DIGIT_BIT)
  35299. return -12690;
  35300. for (j = 0; j < i && j < DIGIT_BIT; j++) {
  35301. mp_zero(r1);
  35302. mp_set_bit(r1, i);
  35303. if (mp_sub_d(r1, (mp_digit)1 << j, r1) != MP_OKAY)
  35304. return -12691;
  35305. if (mp_add_d(r1, (mp_digit)1 << j, r1) != MP_OKAY)
  35306. return -12692;
  35307. if (mp_cmp(a, r1) != MP_EQ)
  35308. return -12693;
  35309. }
  35310. }
  35311. mp_zero(r1);
  35312. if (mp_add_d(r1, 1, r1) != MP_OKAY)
  35313. return -12694;
  35314. if (r1->used != 1)
  35315. return -12695;
  35316. if (mp_sub_d(r1, 1, r1) != MP_OKAY)
  35317. return -12696;
  35318. if (r1->used != 0)
  35319. return -12697;
  35320. return 0;
  35321. }
  35322. static int mp_test_read_to_bin(mp_int* a)
  35323. {
  35324. WOLFSSL_SMALL_STACK_STATIC const byte in[16] = {
  35325. 0x91, 0xa2, 0xb3, 0xc4, 0xd5, 0xe6, 0xf7, 0x08,
  35326. 0x93, 0xa4, 0xb4, 0xc5, 0xd6, 0xe7, 0xf8, 0x09
  35327. };
  35328. byte out[24];
  35329. int i, j, k;
  35330. const byte* p;
  35331. int ret;
  35332. for (i = 0; i < (int)sizeof(in); i++) {
  35333. p = in + sizeof(in) - i;
  35334. ret = mp_read_unsigned_bin(a, p, i);
  35335. if (ret != 0)
  35336. return -12710;
  35337. for (j = i; j < (int)sizeof(out); j++) {
  35338. XMEMSET(out, 0xff, sizeof(out));
  35339. ret = mp_to_unsigned_bin_len(a, out, j);
  35340. if (ret != 0)
  35341. return -12711;
  35342. for (k = 0; k < j - i; k++) {
  35343. if (out[k] != 0)
  35344. return -12712;
  35345. }
  35346. for (; k < j; k++) {
  35347. if (out[k] != p[k - (j - i)])
  35348. return -12713;
  35349. }
  35350. }
  35351. }
  35352. ret = mp_read_unsigned_bin(a, NULL, 0);
  35353. if (ret != 0)
  35354. return -12714;
  35355. if (!mp_iszero(a))
  35356. return -12715;
  35357. return 0;
  35358. }
  35359. #if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  35360. static int mp_test_set_int(mp_int* a)
  35361. {
  35362. #if SP_ULONG_BITS == 64
  35363. unsigned long n = 0xfedcba9876543210UL;
  35364. byte exp[8] = { 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10 };
  35365. byte out[8] = { 0 };
  35366. #elif SP_ULONG_BITS == 32
  35367. unsigned long n = 0xfedcba98UL;
  35368. byte exp[4] = { 0xfe, 0xdc, 0xba, 0x98 };
  35369. byte out[4] = { 0 };
  35370. #elif SP_ULONG_BITS == 16
  35371. unsigned long n = 0xfedc;
  35372. byte exp[2] = { 0xfe, 0xdc };
  35373. byte out[2] = { 0 };
  35374. #elif SP_ULONG_BITS == 8
  35375. unsigned long n = 0xfe;
  35376. byte exp[1] = { 0xfe };
  35377. byte out[1] = { 0 };
  35378. #endif
  35379. int ret;
  35380. ret = mp_set_int(a, n);
  35381. if (ret != 0)
  35382. return -12720;
  35383. ret = mp_unsigned_bin_size(a);
  35384. if (ret != sizeof(exp))
  35385. return -12721;
  35386. ret = mp_to_unsigned_bin(a, out);
  35387. if (ret != 0)
  35388. return -12722;
  35389. if (XMEMCMP(exp, out, sizeof(exp)) != 0)
  35390. return -12723;
  35391. return 0;
  35392. }
  35393. #endif
  35394. #if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  35395. static int mp_test_param(mp_int* a, mp_int* b, mp_int* r, WC_RNG* rng)
  35396. {
  35397. byte buffer[16];
  35398. #if defined(HAVE_ECC) || defined(WOLFSSL_SP_MATH_ALL)
  35399. char hexStr[] = "abcdef0123456789";
  35400. #ifndef WOLFSSL_SP_INT_NEGATIVE
  35401. char negStr[] = "-1234";
  35402. #endif
  35403. #endif
  35404. #if !defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_KEY_GEN) || \
  35405. defined(HAVE_COMP_KEY)
  35406. char decStr[] = "0987654321";
  35407. #endif
  35408. int ret;
  35409. #ifdef WOLFSSL_SP_MATH_ALL
  35410. mp_digit rho;
  35411. int size;
  35412. #endif
  35413. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH)
  35414. int result;
  35415. #endif
  35416. #if (defined(HAVE_ECC) && defined(HAVE_COMP_KEY)) || \
  35417. (defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN))
  35418. mp_digit rd;
  35419. #endif
  35420. (void)rng;
  35421. (void)r;
  35422. ret = mp_init(NULL);
  35423. if (ret != MP_VAL)
  35424. return -12730;
  35425. #if !defined(WOLFSSL_RSA_PUBLIC_ONLY) || (!defined(NO_DH) || defined(HAVE_ECC))
  35426. ret = mp_init_multi(NULL, NULL, NULL, NULL, NULL, NULL);
  35427. if (ret != MP_OKAY)
  35428. return -12731;
  35429. #endif
  35430. mp_free(NULL);
  35431. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) || !defined(NO_DH) || defined(HAVE_ECC)
  35432. ret = mp_grow(NULL, 1);
  35433. if (ret != MP_VAL)
  35434. return -12732;
  35435. #ifdef WOLFSSL_SP_MATH
  35436. ret = mp_grow(a, SP_INT_DIGITS + 1);
  35437. if (ret != MP_MEM)
  35438. return -12733;
  35439. #endif
  35440. #endif
  35441. mp_clear(NULL);
  35442. ret = mp_abs(NULL, NULL);
  35443. if (ret != MP_VAL)
  35444. return -12734;
  35445. ret = mp_abs(a, NULL);
  35446. if (ret != MP_VAL)
  35447. return -12735;
  35448. ret = mp_abs(NULL, b);
  35449. if (ret != MP_VAL)
  35450. return -12736;
  35451. ret = mp_unsigned_bin_size(NULL);
  35452. if (ret != 0)
  35453. return -12737;
  35454. ret = mp_read_unsigned_bin(NULL, NULL, sizeof(buffer));
  35455. if (ret != MP_VAL)
  35456. return -12738;
  35457. ret = mp_read_unsigned_bin(NULL, buffer, sizeof(buffer));
  35458. if (ret != MP_VAL)
  35459. return -12739;
  35460. ret = mp_read_unsigned_bin(a, NULL, sizeof(buffer));
  35461. if (ret != MP_VAL)
  35462. return -12740;
  35463. ret = mp_read_unsigned_bin(a, buffer, SP_INT_DIGITS * SP_WORD_SIZEOF + 1);
  35464. if (ret != MP_VAL)
  35465. return -12741;
  35466. #if defined(HAVE_ECC) || defined(WOLFSSL_SP_MATH_ALL)
  35467. ret = mp_read_radix(NULL, NULL, 16);
  35468. if (ret != MP_VAL)
  35469. return -12742;
  35470. ret = mp_read_radix(a, NULL, 16);
  35471. if (ret != MP_VAL)
  35472. return -12743;
  35473. ret = mp_read_radix(NULL, hexStr, 16);
  35474. if (ret != MP_VAL)
  35475. return -12744;
  35476. #ifndef WOLFSSL_SP_INT_NEGATIVE
  35477. ret = mp_read_radix(a, negStr, 16);
  35478. if (ret != MP_VAL)
  35479. return -12745;
  35480. #ifdef WOLFSSL_SP_MATH_ALL
  35481. ret = mp_read_radix(a, negStr, 10);
  35482. if (ret != MP_VAL)
  35483. return -12746;
  35484. #endif /* WOLFSSL_SP_MATH_ALL */
  35485. #endif /* WOLFSSL_SP_INT_NEGATIVE */
  35486. #endif
  35487. #ifndef WOLFSSL_SP_MATH_ALL
  35488. /* Radix 10 only supported with ALL. */
  35489. ret = mp_read_radix(a, decStr, 10);
  35490. if (ret != MP_VAL)
  35491. return -12747;
  35492. #endif
  35493. /* Radix 8 not supported SP_INT. */
  35494. ret = mp_read_radix(a, "0123", 8);
  35495. if (ret != MP_VAL)
  35496. return -12748;
  35497. ret = mp_count_bits(NULL);
  35498. if (ret != 0)
  35499. return -12749;
  35500. ret = mp_is_bit_set(NULL, 0);
  35501. if (ret != 0)
  35502. return -12750;
  35503. ret = mp_leading_bit(NULL);
  35504. if (ret != 0)
  35505. return -12751;
  35506. mp_zero(a);
  35507. ret = mp_leading_bit(a);
  35508. if (ret != 0)
  35509. return -12752;
  35510. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH) || \
  35511. defined(HAVE_ECC) || defined(WOLFSSL_KEY_GEN) || defined(OPENSSL_EXTRA) || \
  35512. !defined(NO_RSA)
  35513. ret = mp_set_bit(NULL, 1);
  35514. if (ret != MP_VAL)
  35515. return -12753;
  35516. #endif
  35517. #if !defined(NO_DH) || defined(HAVE_ECC) || defined(WC_RSA_BLINDING) || \
  35518. !defined(WOLFSSL_RSA_VERIFY_ONLY)
  35519. ret = mp_to_unsigned_bin(NULL, NULL);
  35520. if (ret != MP_VAL)
  35521. return -12754;
  35522. ret = mp_to_unsigned_bin(a, NULL);
  35523. if (ret != MP_VAL)
  35524. return -12755;
  35525. ret = mp_to_unsigned_bin(NULL, buffer);
  35526. if (ret != MP_VAL)
  35527. return -12756;
  35528. #endif
  35529. ret = mp_to_unsigned_bin_len(NULL, NULL, 1);
  35530. if (ret != MP_VAL)
  35531. return -12757;
  35532. ret = mp_to_unsigned_bin_len(a, NULL, 1);
  35533. if (ret != MP_VAL)
  35534. return -12758;
  35535. ret = mp_to_unsigned_bin_len(NULL, buffer, 1);
  35536. if (ret != MP_VAL)
  35537. return -12759;
  35538. #if defined(WOLFSSL_SP_MATH_ALL) && !defined(NO_RSA) && \
  35539. !defined(WOLFSSL_RSA_VERIFY_ONLY)
  35540. ret = mp_to_unsigned_bin_at_pos(0, NULL, NULL);
  35541. if (ret != MP_VAL)
  35542. return -12760;
  35543. ret = mp_to_unsigned_bin_at_pos(0, a, NULL);
  35544. if (ret != MP_VAL)
  35545. return -12761;
  35546. ret = mp_to_unsigned_bin_at_pos(0, NULL, buffer);
  35547. if (ret != MP_VAL)
  35548. return -12762;
  35549. ret = mp_to_unsigned_bin_at_pos(0, a, buffer);
  35550. if (ret != MP_OKAY)
  35551. return -12763;
  35552. #endif
  35553. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) || (!defined(NO_DH) || defined(HAVE_ECC))
  35554. ret = mp_copy(NULL, NULL);
  35555. if (ret != MP_VAL)
  35556. return -12764;
  35557. ret = mp_copy(a, NULL);
  35558. if (ret != MP_VAL)
  35559. return -12765;
  35560. ret = mp_copy(NULL, b);
  35561. if (ret != MP_VAL)
  35562. return -12766;
  35563. #endif
  35564. #if defined(WOLFSSL_KEY_GEN) || !defined(NO_DH)
  35565. ret = sp_2expt(NULL, 1);
  35566. if (ret != MP_VAL)
  35567. return -12767;
  35568. #endif
  35569. ret = mp_set(NULL, 0);
  35570. if (ret != MP_VAL)
  35571. return -12768;
  35572. ret = mp_cmp_d(NULL, 0);
  35573. if (ret != MP_LT)
  35574. return -12769;
  35575. ret = mp_cmp(NULL, NULL);
  35576. if (ret != MP_EQ)
  35577. return -12770;
  35578. ret = mp_cmp(a, NULL);
  35579. if (ret != MP_GT)
  35580. return -12771;
  35581. ret = mp_cmp(NULL, b);
  35582. if (ret != MP_LT)
  35583. return -12772;
  35584. #ifdef WOLFSSL_SP_MATH_ALL
  35585. mp_rshd(NULL, 1);
  35586. #endif
  35587. mp_zero(NULL);
  35588. #if !defined(NO_DH) || defined(HAVE_ECC) || defined(WC_RSA_BLINDING) || \
  35589. !defined(WOLFSSL_RSA_VERIFY_ONLY)
  35590. ret = mp_lshd(NULL, 0);
  35591. if (ret != MP_VAL)
  35592. return -12773;
  35593. ret = mp_lshd(a, SP_INT_DIGITS + 1);
  35594. if (ret != MP_VAL)
  35595. return -12774;
  35596. #endif
  35597. #if defined(WOLFSSL_SP_MATH_ALL)
  35598. ret = mp_div(NULL, NULL, a, b);
  35599. if (ret != MP_VAL)
  35600. return -12775;
  35601. ret = mp_div(a, NULL, a, b);
  35602. if (ret != MP_VAL)
  35603. return -12776;
  35604. ret = mp_div(NULL, b, a, b);
  35605. if (ret != MP_VAL)
  35606. return -12777;
  35607. ret = mp_div(a, b, NULL, NULL);
  35608. if (ret != MP_VAL)
  35609. return -12778;
  35610. #endif
  35611. #if defined(WOLFSSL_SP_MATH_ALL) || !defined(NO_DH) || defined(HAVE_ECC) || \
  35612. (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY))
  35613. ret = mp_mod(NULL, NULL, NULL);
  35614. if (ret != MP_VAL)
  35615. return -12779;
  35616. ret = mp_mod(a, NULL, NULL);
  35617. if (ret != MP_VAL)
  35618. return -12780;
  35619. ret = mp_mod(NULL, b, NULL);
  35620. if (ret != MP_VAL)
  35621. return -12781;
  35622. ret = mp_mod(NULL, NULL, r);
  35623. if (ret != MP_VAL)
  35624. return -12782;
  35625. ret = mp_mod(a, b, NULL);
  35626. if (ret != MP_VAL)
  35627. return -12783;
  35628. ret = mp_mod(a, NULL, r);
  35629. if (ret != MP_VAL)
  35630. return -12784;
  35631. ret = mp_mod(NULL, b, r);
  35632. if (ret != MP_VAL)
  35633. return -12785;
  35634. #endif
  35635. #if !defined(NO_RSA) || defined(WOLFSSL_SP_MATH_ALL)
  35636. ret = mp_set_int(NULL, 0);
  35637. if (ret != MP_VAL)
  35638. return -12786;
  35639. #endif
  35640. #if !defined(NO_RSA) || !defined(NO_DSA) || !defined(NO_DH) || \
  35641. (defined(HAVE_ECC) && defined(HAVE_COMP_KEY)) || defined(OPENSSL_EXTRA)
  35642. ret = mp_exptmod_ex(NULL, NULL, 1, NULL, NULL);
  35643. if (ret != MP_VAL)
  35644. return 9950;
  35645. ret = mp_exptmod_ex(a, NULL, 1, NULL, NULL);
  35646. if (ret != MP_VAL)
  35647. return 9951;
  35648. ret = mp_exptmod_ex(NULL, a, 1, NULL, NULL);
  35649. if (ret != MP_VAL)
  35650. return 9952;
  35651. ret = mp_exptmod_ex(NULL, NULL, 1, a, NULL);
  35652. if (ret != MP_VAL)
  35653. return 9953;
  35654. ret = mp_exptmod_ex(NULL, NULL, 1, NULL, a);
  35655. if (ret != MP_VAL)
  35656. return 9954;
  35657. ret = mp_exptmod_ex(a, a, 1, a, NULL);
  35658. if (ret != MP_VAL)
  35659. return 9955;
  35660. ret = mp_exptmod_ex(a, a, 1, NULL, a);
  35661. if (ret != MP_VAL)
  35662. return 9956;
  35663. ret = mp_exptmod_ex(a, NULL, 1, a, a);
  35664. if (ret != MP_VAL)
  35665. return 9957;
  35666. ret = mp_exptmod_ex(NULL, a, 1, a, a);
  35667. if (ret != MP_VAL)
  35668. return 9958;
  35669. ret = mp_exptmod_nct(NULL, NULL, NULL, NULL);
  35670. if (ret != MP_VAL)
  35671. return 9960;
  35672. ret = mp_exptmod_nct(a, NULL, NULL, NULL);
  35673. if (ret != MP_VAL)
  35674. return 9961;
  35675. ret = mp_exptmod_nct(NULL, a, NULL, NULL);
  35676. if (ret != MP_VAL)
  35677. return 9962;
  35678. ret = mp_exptmod_nct(NULL, NULL, a, NULL);
  35679. if (ret != MP_VAL)
  35680. return 9963;
  35681. ret = mp_exptmod_nct(NULL, NULL, NULL, a);
  35682. if (ret != MP_VAL)
  35683. return 9964;
  35684. ret = mp_exptmod_nct(a, a, a, NULL);
  35685. if (ret != MP_VAL)
  35686. return 9965;
  35687. ret = mp_exptmod_nct(a, a, NULL, a);
  35688. if (ret != MP_VAL)
  35689. return 9966;
  35690. ret = mp_exptmod_nct(a, NULL, a, a);
  35691. if (ret != MP_VAL)
  35692. return 9967;
  35693. ret = mp_exptmod_nct(NULL, a, a, a);
  35694. if (ret != MP_VAL)
  35695. return 9968;
  35696. #endif
  35697. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_DH) || !defined(NO_DSA)) && \
  35698. !defined(WC_NO_RNG)
  35699. ret = mp_rand_prime(NULL, 32, NULL, NULL);
  35700. if (ret != MP_VAL)
  35701. return -12787;
  35702. ret = mp_rand_prime(a, 32, NULL, NULL);
  35703. if (ret != MP_VAL)
  35704. return -12788;
  35705. ret = mp_rand_prime(NULL, 32, rng, NULL);
  35706. if (ret != MP_VAL)
  35707. return -12789;
  35708. ret = mp_rand_prime(a, 0, rng, NULL);
  35709. if (ret != MP_VAL)
  35710. return -9969;
  35711. #endif
  35712. #if defined(WOLFSSL_SP_MATH_ALL) && !defined(WOLFSSL_RSA_VERIFY_ONLY)
  35713. ret = mp_mul(NULL, NULL, NULL);
  35714. if (ret != MP_VAL)
  35715. return -12790;
  35716. ret = mp_mul(a, NULL, NULL);
  35717. if (ret != MP_VAL)
  35718. return -12791;
  35719. ret = mp_mul(NULL, b, NULL);
  35720. if (ret != MP_VAL)
  35721. return -12792;
  35722. ret = mp_mul(NULL, NULL, r);
  35723. if (ret != MP_VAL)
  35724. return -12793;
  35725. ret = mp_mul(a, b, NULL);
  35726. if (ret != MP_VAL)
  35727. return -12794;
  35728. ret = mp_mul(a, NULL, r);
  35729. if (ret != MP_VAL)
  35730. return -12795;
  35731. ret = mp_mul(NULL, b, r);
  35732. if (ret != MP_VAL)
  35733. return -12796;
  35734. #endif
  35735. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH) || \
  35736. defined(HAVE_ECC) || (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY))
  35737. ret = mp_sqr(NULL, NULL);
  35738. if (ret != MP_VAL)
  35739. return -12797;
  35740. ret = mp_sqr(a, NULL);
  35741. if (ret != MP_VAL)
  35742. return -12798;
  35743. ret = mp_sqr(NULL, r);
  35744. if (ret != MP_VAL)
  35745. return -12799;
  35746. #endif
  35747. #if !defined(WOLFSSL_RSA_VERIFY_ONLY)
  35748. ret = mp_sqrmod(NULL, NULL, NULL);
  35749. if (ret != MP_VAL)
  35750. return -12800;
  35751. ret = mp_sqrmod(a, NULL, NULL);
  35752. if (ret != MP_VAL)
  35753. return -12801;
  35754. ret = mp_sqrmod(NULL, a, NULL);
  35755. if (ret != MP_VAL)
  35756. return -12802;
  35757. ret = mp_sqrmod(NULL, NULL, a);
  35758. if (ret != MP_VAL)
  35759. return -12803;
  35760. ret = mp_sqrmod(a, b, NULL);
  35761. if (ret != MP_VAL)
  35762. return -12804;
  35763. ret = mp_sqrmod(a, NULL, b);
  35764. if (ret != MP_VAL)
  35765. return -12805;
  35766. ret = mp_sqrmod(NULL, a, b);
  35767. if (ret != MP_VAL)
  35768. return -12806;
  35769. ret = mp_mulmod(NULL, NULL, NULL, NULL);
  35770. if (ret != MP_VAL)
  35771. return -12807;
  35772. ret = mp_mulmod(a, NULL, NULL, NULL);
  35773. if (ret != MP_VAL)
  35774. return -12808;
  35775. ret = mp_mulmod(NULL, a, NULL, NULL);
  35776. if (ret != MP_VAL)
  35777. return -12809;
  35778. ret = mp_mulmod(NULL, NULL, a, NULL);
  35779. if (ret != MP_VAL)
  35780. return -12810;
  35781. ret = mp_mulmod(NULL, NULL, NULL, a);
  35782. if (ret != MP_VAL)
  35783. return -12811;
  35784. ret = mp_mulmod(a, b, b, NULL);
  35785. if (ret != MP_VAL)
  35786. return -12812;
  35787. ret = mp_mulmod(a, b, NULL, a);
  35788. if (ret != MP_VAL)
  35789. return -12813;
  35790. ret = mp_mulmod(a, NULL, b, a);
  35791. if (ret != MP_VAL)
  35792. return -12814;
  35793. ret = mp_mulmod(NULL, b, b, a);
  35794. if (ret != MP_VAL)
  35795. return -12815;
  35796. #endif
  35797. #if !defined(NO_PWDBASED) || defined(WOLFSSL_KEY_GEN) || !defined(NO_DH) || \
  35798. !defined(NO_RSA) || !defined(NO_DSA)
  35799. ret = mp_add_d(NULL, 1, NULL);
  35800. if (ret != MP_VAL)
  35801. return -12816;
  35802. ret = mp_add_d(a, 1, NULL);
  35803. if (ret != MP_VAL)
  35804. return -12817;
  35805. ret = mp_add_d(NULL, 1, b);
  35806. if (ret != MP_VAL)
  35807. return -12818;
  35808. #endif
  35809. #if (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
  35810. !defined(NO_DH) || defined(HAVE_ECC) || !defined(NO_DSA)
  35811. ret = mp_sub_d(NULL, 1, NULL);
  35812. if (ret != MP_VAL)
  35813. return -12819;
  35814. ret = mp_sub_d(a, 1, NULL);
  35815. if (ret != MP_VAL)
  35816. return -12820;
  35817. ret = mp_sub_d(NULL, 1, b);
  35818. if (ret != MP_VAL)
  35819. return -12821;
  35820. #endif
  35821. #if (defined(WOLFSSL_SP_MATH_ALL) && !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
  35822. defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  35823. ret = mp_div_d(NULL, 0, NULL, NULL);
  35824. if (ret != MP_VAL)
  35825. return -12822;
  35826. ret = mp_div_d(a, 0, NULL, NULL);
  35827. if (ret != MP_VAL)
  35828. return -12823;
  35829. ret = mp_div_d(NULL, 1, NULL, NULL);
  35830. if (ret != MP_VAL)
  35831. return -12824;
  35832. #endif
  35833. #if (defined(HAVE_ECC) && defined(HAVE_COMP_KEY)) || \
  35834. (defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN))
  35835. ret = mp_mod_d(NULL, 0, NULL);
  35836. if (ret != MP_VAL)
  35837. return -12825;
  35838. ret = mp_mod_d(a, 0, NULL);
  35839. if (ret != MP_VAL)
  35840. return -12826;
  35841. ret = mp_mod_d(NULL, 0, &rd);
  35842. if (ret != MP_VAL)
  35843. return -12827;
  35844. #endif
  35845. #if defined(WOLFSSL_SP_MATH_ALL) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  35846. ret = mp_gcd(NULL, NULL, NULL);
  35847. if (ret != MP_VAL)
  35848. return -12828;
  35849. ret = mp_gcd(a, NULL, NULL);
  35850. if (ret != MP_VAL)
  35851. return -12829;
  35852. ret = mp_gcd(NULL, a, NULL);
  35853. if (ret != MP_VAL)
  35854. return -12830;
  35855. ret = mp_gcd(NULL, NULL, a);
  35856. if (ret != MP_VAL)
  35857. return -12831;
  35858. ret = mp_gcd(a, b, NULL);
  35859. if (ret != MP_VAL)
  35860. return -12832;
  35861. ret = mp_gcd(a, NULL, b);
  35862. if (ret != MP_VAL)
  35863. return -12833;
  35864. ret = mp_gcd(NULL, a, b);
  35865. if (ret != MP_VAL)
  35866. return -12834;
  35867. #endif
  35868. #if !defined(WOLFSSL_SP_MATH) && defined(HAVE_ECC)
  35869. ret = mp_div_2_mod_ct(NULL, NULL, NULL);
  35870. if (ret != MP_VAL)
  35871. return -12835;
  35872. ret = mp_div_2_mod_ct(a, NULL, NULL);
  35873. if (ret != MP_VAL)
  35874. return -12836;
  35875. ret = mp_div_2_mod_ct(NULL, b, NULL);
  35876. if (ret != MP_VAL)
  35877. return -12837;
  35878. ret = mp_div_2_mod_ct(NULL, NULL, a);
  35879. if (ret != MP_VAL)
  35880. return -12838;
  35881. ret = mp_div_2_mod_ct(a, b, NULL);
  35882. if (ret != MP_VAL)
  35883. return -12839;
  35884. ret = mp_div_2_mod_ct(a, b, NULL);
  35885. if (ret != MP_VAL)
  35886. return -12840;
  35887. ret = mp_div_2_mod_ct(NULL, b, a);
  35888. if (ret != MP_VAL)
  35889. return -12841;
  35890. ret = mp_div_2(NULL, NULL);
  35891. if (ret != MP_VAL)
  35892. return -12842;
  35893. ret = mp_div_2(a, NULL);
  35894. if (ret != MP_VAL)
  35895. return -12843;
  35896. ret = mp_div_2(NULL, a);
  35897. if (ret != MP_VAL)
  35898. return -12844;
  35899. #endif
  35900. #if (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
  35901. defined(HAVE_ECC) || !defined(NO_DSA) || defined(OPENSSL_EXTRA)
  35902. ret = mp_invmod(NULL, NULL, NULL);
  35903. if (ret != MP_VAL)
  35904. return -12845;
  35905. ret = mp_invmod(a, NULL, NULL);
  35906. if (ret != MP_VAL)
  35907. return -12846;
  35908. ret = mp_invmod(NULL, b, NULL);
  35909. if (ret != MP_VAL)
  35910. return -12847;
  35911. ret = mp_invmod(NULL, NULL, a);
  35912. if (ret != MP_VAL)
  35913. return -12848;
  35914. ret = mp_invmod(a, b, NULL);
  35915. if (ret != MP_VAL)
  35916. return -12849;
  35917. ret = mp_invmod(a, NULL, a);
  35918. if (ret != MP_VAL)
  35919. return -12850;
  35920. ret = mp_invmod(NULL, b, a);
  35921. if (ret != MP_VAL)
  35922. return -12851;
  35923. #endif
  35924. #if !defined(WOLFSSL_SP_MATH) && defined(HAVE_ECC)
  35925. ret = mp_invmod_mont_ct(NULL, NULL, NULL, 1);
  35926. if (ret != MP_VAL)
  35927. return -12852;
  35928. ret = mp_invmod_mont_ct(a, NULL, NULL, 1);
  35929. if (ret != MP_VAL)
  35930. return -12853;
  35931. ret = mp_invmod_mont_ct(NULL, b, NULL, 1);
  35932. if (ret != MP_VAL)
  35933. return -12854;
  35934. ret = mp_invmod_mont_ct(NULL, NULL, a, 1);
  35935. if (ret != MP_VAL)
  35936. return -12855;
  35937. ret = mp_invmod_mont_ct(a, b, NULL, 1);
  35938. if (ret != MP_VAL)
  35939. return -12856;
  35940. ret = mp_invmod_mont_ct(a, NULL, a, 1);
  35941. if (ret != MP_VAL)
  35942. return -12857;
  35943. ret = mp_invmod_mont_ct(NULL, b, a, 1);
  35944. if (ret != MP_VAL)
  35945. return -12858;
  35946. #endif
  35947. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(WC_RSA_BLINDING)
  35948. ret = mp_lcm(NULL, NULL, NULL);
  35949. if (ret != MP_VAL)
  35950. return -12859;
  35951. ret = mp_lcm(a, NULL, NULL);
  35952. if (ret != MP_VAL)
  35953. return -12860;
  35954. ret = mp_lcm(NULL, b, NULL);
  35955. if (ret != MP_VAL)
  35956. return -12861;
  35957. ret = mp_lcm(NULL, NULL, a);
  35958. if (ret != MP_VAL)
  35959. return -12862;
  35960. ret = mp_lcm(a, b, NULL);
  35961. if (ret != MP_VAL)
  35962. return -12863;
  35963. ret = mp_lcm(a, NULL, a);
  35964. if (ret != MP_VAL)
  35965. return -12864;
  35966. ret = mp_lcm(NULL, b, a);
  35967. if (ret != MP_VAL)
  35968. return -12865;
  35969. #endif
  35970. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH)
  35971. ret = mp_exptmod_ex(NULL, NULL, 1, NULL, NULL);
  35972. if (ret != MP_VAL)
  35973. return -12866;
  35974. ret = mp_exptmod_ex(a, NULL, 1, NULL, NULL);
  35975. if (ret != MP_VAL)
  35976. return -12867;
  35977. ret = mp_exptmod_ex(NULL, b, 1, NULL, NULL);
  35978. if (ret != MP_VAL)
  35979. return -12868;
  35980. ret = mp_exptmod_ex(NULL, NULL, 1, b, NULL);
  35981. if (ret != MP_VAL)
  35982. return -12869;
  35983. ret = mp_exptmod_ex(NULL, NULL, 1, NULL, a);
  35984. if (ret != MP_VAL)
  35985. return -12870;
  35986. ret = mp_exptmod_ex(a, b, 1, b, NULL);
  35987. if (ret != MP_VAL)
  35988. return -12871;
  35989. ret = mp_exptmod_ex(a, b, 1, NULL, a);
  35990. if (ret != MP_VAL)
  35991. return -12872;
  35992. ret = mp_exptmod_ex(a, NULL, 1, b, a);
  35993. if (ret != MP_VAL)
  35994. return -12873;
  35995. ret = mp_exptmod_ex(NULL, b, 1, b, a);
  35996. if (ret != MP_VAL)
  35997. return -12874;
  35998. ret = mp_exptmod(NULL, NULL, NULL, NULL);
  35999. if (ret != MP_VAL)
  36000. return -12875;
  36001. ret = mp_exptmod(a, NULL, NULL, NULL);
  36002. if (ret != MP_VAL)
  36003. return -12876;
  36004. ret = mp_exptmod(NULL, b, NULL, NULL);
  36005. if (ret != MP_VAL)
  36006. return -12877;
  36007. ret = mp_exptmod(NULL, NULL, b, NULL);
  36008. if (ret != MP_VAL)
  36009. return -12878;
  36010. ret = mp_exptmod(NULL, NULL, NULL, a);
  36011. if (ret != MP_VAL)
  36012. return -12879;
  36013. ret = mp_exptmod(a, b, b, NULL);
  36014. if (ret != MP_VAL)
  36015. return -12880;
  36016. ret = mp_exptmod(a, b, NULL, a);
  36017. if (ret != MP_VAL)
  36018. return -12881;
  36019. ret = mp_exptmod(a, NULL, b, a);
  36020. if (ret != MP_VAL)
  36021. return -12882;
  36022. ret = mp_exptmod(NULL, b, b, a);
  36023. if (ret != MP_VAL)
  36024. return -12883;
  36025. ret = mp_exptmod_nct(NULL, NULL, NULL, NULL);
  36026. if (ret != MP_VAL)
  36027. return -12884;
  36028. ret = mp_exptmod_nct(a, NULL, NULL, NULL);
  36029. if (ret != MP_VAL)
  36030. return -12885;
  36031. ret = mp_exptmod_nct(NULL, b, NULL, NULL);
  36032. if (ret != MP_VAL)
  36033. return -12886;
  36034. ret = mp_exptmod_nct(NULL, NULL, b, NULL);
  36035. if (ret != MP_VAL)
  36036. return -12887;
  36037. ret = mp_exptmod_nct(NULL, NULL, NULL, a);
  36038. if (ret != MP_VAL)
  36039. return -12888;
  36040. ret = mp_exptmod_nct(a, b, b, NULL);
  36041. if (ret != MP_VAL)
  36042. return -12889;
  36043. ret = mp_exptmod_nct(a, b, NULL, a);
  36044. if (ret != MP_VAL)
  36045. return -12890;
  36046. ret = mp_exptmod_nct(a, NULL, b, a);
  36047. if (ret != MP_VAL)
  36048. return -12891;
  36049. ret = mp_exptmod_nct(NULL, b, b, a);
  36050. if (ret != MP_VAL)
  36051. return -12892;
  36052. #endif
  36053. #if defined(HAVE_ECC) && defined(HAVE_COMP_KEY)
  36054. ret = mp_cnt_lsb(NULL);
  36055. if (ret != 0)
  36056. return -12893;
  36057. #endif
  36058. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH)
  36059. ret = mp_prime_is_prime(NULL, 1, NULL);
  36060. if (ret != MP_VAL)
  36061. return -12894;
  36062. ret = mp_prime_is_prime(a, 1, NULL);
  36063. if (ret != MP_VAL)
  36064. return -12895;
  36065. ret = mp_prime_is_prime(NULL, 1, &result);
  36066. if (ret != MP_VAL)
  36067. return -12896;
  36068. ret = mp_prime_is_prime(a, 0, &result);
  36069. if (ret != MP_VAL)
  36070. return -12897;
  36071. ret = mp_prime_is_prime(a, 1024, &result);
  36072. if (ret != MP_VAL)
  36073. return -12898;
  36074. ret = mp_prime_is_prime_ex(NULL, 1, NULL, NULL);
  36075. if (ret != MP_VAL)
  36076. return -12899;
  36077. ret = mp_prime_is_prime_ex(a, 1, NULL, NULL);
  36078. if (ret != MP_VAL)
  36079. return -12900;
  36080. ret = mp_prime_is_prime_ex(NULL, 1, &result, NULL);
  36081. if (ret != MP_VAL)
  36082. return -12901;
  36083. ret = mp_prime_is_prime_ex(NULL, 1, NULL, rng);
  36084. if (ret != MP_VAL)
  36085. return -12902;
  36086. ret = mp_prime_is_prime_ex(a, 1, &result, NULL);
  36087. if (ret != MP_VAL)
  36088. return -12903;
  36089. ret = mp_prime_is_prime_ex(a, 1, NULL, rng);
  36090. if (ret != MP_VAL)
  36091. return -12904;
  36092. ret = mp_prime_is_prime_ex(NULL, 1, &result, rng);
  36093. if (ret != MP_VAL)
  36094. return -12905;
  36095. #endif
  36096. #if defined(WOLFSSL_SP_MATH_ALL) || !defined(NO_DH) || !defined(NO_DSA)
  36097. ret = mp_exch(NULL, NULL);
  36098. if (ret != MP_VAL)
  36099. return -12906;
  36100. ret = mp_exch(a, NULL);
  36101. if (ret != MP_VAL)
  36102. return -12907;
  36103. ret = mp_exch(NULL, b);
  36104. if (ret != MP_VAL)
  36105. return -12908;
  36106. #endif
  36107. #if (defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA)) || \
  36108. defined(WOLFSSL_SP_MATH_ALL)
  36109. ret = mp_mul_d(NULL, 1, NULL);
  36110. if (ret != MP_VAL)
  36111. return -12909;
  36112. ret = mp_mul_d(a, 1, NULL);
  36113. if (ret != MP_VAL)
  36114. return -12910;
  36115. ret = mp_mul_d(NULL, 1, b);
  36116. if (ret != MP_VAL)
  36117. return -12911;
  36118. #endif
  36119. #if !defined(WOLFSSL_RSA_VERIFY_ONLY)
  36120. ret = mp_add(NULL, NULL, NULL);
  36121. if (ret != MP_VAL)
  36122. return -12912;
  36123. ret = mp_add(a, NULL, NULL);
  36124. if (ret != MP_VAL)
  36125. return -12913;
  36126. ret = mp_add(NULL, b, NULL);
  36127. if (ret != MP_VAL)
  36128. return -12914;
  36129. ret = mp_add(NULL, NULL, r);
  36130. if (ret != MP_VAL)
  36131. return -12915;
  36132. ret = mp_add(a, b, NULL);
  36133. if (ret != MP_VAL)
  36134. return -12916;
  36135. ret = mp_add(a, NULL, r);
  36136. if (ret != MP_VAL)
  36137. return -12917;
  36138. ret = mp_add(NULL, b, r);
  36139. if (ret != MP_VAL)
  36140. return -12918;
  36141. #endif
  36142. #if defined(WOLFSSL_SP_MATH_ALL) || !defined(NO_DH) || defined(HAVE_ECC) || \
  36143. (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY))
  36144. ret = mp_sub(NULL, NULL, NULL);
  36145. if (ret != MP_VAL)
  36146. return -12919;
  36147. ret = mp_sub(a, NULL, NULL);
  36148. if (ret != MP_VAL)
  36149. return -12920;
  36150. ret = mp_sub(NULL, b, NULL);
  36151. if (ret != MP_VAL)
  36152. return -12921;
  36153. ret = mp_sub(NULL, NULL, r);
  36154. if (ret != MP_VAL)
  36155. return -12922;
  36156. ret = mp_sub(a, b, NULL);
  36157. if (ret != MP_VAL)
  36158. return -12923;
  36159. ret = mp_sub(a, NULL, r);
  36160. if (ret != MP_VAL)
  36161. return -12924;
  36162. ret = mp_sub(NULL, b, r);
  36163. if (ret != MP_VAL)
  36164. return -12925;
  36165. #endif
  36166. #if defined(WOLFSSL_SP_MATH_ALL) || (!defined(WOLFSSL_SP_MATH) && \
  36167. defined(WOLFSSL_CUSTOM_CURVES))
  36168. ret = mp_addmod(NULL, NULL, NULL, NULL);
  36169. if (ret != MP_VAL)
  36170. return -12926;
  36171. ret = mp_addmod(a, NULL, NULL, NULL);
  36172. if (ret != MP_VAL)
  36173. return -12927;
  36174. ret = mp_addmod(NULL, b, NULL, NULL);
  36175. if (ret != MP_VAL)
  36176. return -12928;
  36177. ret = mp_addmod(NULL, NULL, b, NULL);
  36178. if (ret != MP_VAL)
  36179. return -12929;
  36180. ret = mp_addmod(NULL, NULL, NULL, a);
  36181. if (ret != MP_VAL)
  36182. return -12930;
  36183. ret = mp_addmod(a, b, b, NULL);
  36184. if (ret != MP_VAL)
  36185. return -12931;
  36186. ret = mp_addmod(a, b, NULL, a);
  36187. if (ret != MP_VAL)
  36188. return -12932;
  36189. ret = mp_addmod(a, NULL, b, a);
  36190. if (ret != MP_VAL)
  36191. return -12933;
  36192. ret = mp_addmod(NULL, b, b, a);
  36193. if (ret != MP_VAL)
  36194. return -12934;
  36195. #endif
  36196. #ifdef WOLFSSL_SP_MATH_ALL
  36197. ret = mp_submod(NULL, NULL, NULL, NULL);
  36198. if (ret != MP_VAL)
  36199. return -12935;
  36200. ret = mp_submod(a, NULL, NULL, NULL);
  36201. if (ret != MP_VAL)
  36202. return -12936;
  36203. ret = mp_submod(NULL, b, NULL, NULL);
  36204. if (ret != MP_VAL)
  36205. return -12937;
  36206. ret = mp_submod(NULL, NULL, b, NULL);
  36207. if (ret != MP_VAL)
  36208. return -12938;
  36209. ret = mp_submod(NULL, NULL, NULL, a);
  36210. if (ret != MP_VAL)
  36211. return -12939;
  36212. ret = mp_submod(a, b, b, NULL);
  36213. if (ret != MP_VAL)
  36214. return -12940;
  36215. ret = mp_submod(a, b, NULL, a);
  36216. if (ret != MP_VAL)
  36217. return -12941;
  36218. ret = mp_submod(a, NULL, b, a);
  36219. if (ret != MP_VAL)
  36220. return -12942;
  36221. ret = mp_submod(NULL, b, b, a);
  36222. if (ret != MP_VAL)
  36223. return -12943;
  36224. #endif
  36225. #ifdef WOLFSSL_SP_MATH_ALL
  36226. ret = mp_div_2d(NULL, 1, a, b);
  36227. if (ret != MP_VAL)
  36228. return -12944;
  36229. ret = mp_mod_2d(NULL, 1, NULL);
  36230. if (ret != MP_VAL)
  36231. return -12945;
  36232. ret = mp_mod_2d(a, 1, NULL);
  36233. if (ret != MP_VAL)
  36234. return -12946;
  36235. ret = mp_mod_2d(NULL, 1, b);
  36236. if (ret != MP_VAL)
  36237. return -12947;
  36238. ret = mp_mul_2d(NULL, 1, NULL);
  36239. if (ret != MP_VAL)
  36240. return -12948;
  36241. ret = mp_mul_2d(a, 1, NULL);
  36242. if (ret != MP_VAL)
  36243. return -12949;
  36244. ret = mp_mul_2d(NULL, 1, b);
  36245. if (ret != MP_VAL)
  36246. return -12950;
  36247. #endif
  36248. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH) || \
  36249. defined(HAVE_ECC) || (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY))
  36250. ret = mp_montgomery_reduce(NULL, NULL, 1);
  36251. if (ret != MP_VAL)
  36252. return -12951;
  36253. ret = mp_montgomery_reduce(a, NULL, 1);
  36254. if (ret != MP_VAL)
  36255. return -12952;
  36256. ret = mp_montgomery_reduce(NULL, b, 1);
  36257. if (ret != MP_VAL)
  36258. return -12953;
  36259. mp_zero(b);
  36260. ret = mp_montgomery_reduce(a, b, 1);
  36261. if (ret != MP_VAL)
  36262. return -12954;
  36263. #endif
  36264. #ifdef WOLFSSL_SP_MATH_ALL
  36265. ret = mp_montgomery_setup(NULL, NULL);
  36266. if (ret != MP_VAL)
  36267. return -12955;
  36268. ret = mp_montgomery_setup(a, NULL);
  36269. if (ret != MP_VAL)
  36270. return -12956;
  36271. ret = mp_montgomery_setup(NULL, &rho);
  36272. if (ret != MP_VAL)
  36273. return -12957;
  36274. ret = mp_montgomery_calc_normalization(NULL, NULL);
  36275. if (ret != MP_VAL)
  36276. return -12958;
  36277. ret = mp_montgomery_calc_normalization(a, NULL);
  36278. if (ret != MP_VAL)
  36279. return -12959;
  36280. ret = mp_montgomery_calc_normalization(NULL, b);
  36281. if (ret != MP_VAL)
  36282. return -12960;
  36283. #endif
  36284. ret = mp_unsigned_bin_size(NULL);
  36285. if (ret != 0)
  36286. return -12961;
  36287. #if defined(WC_MP_TO_RADIX) || defined(WOLFSSL_SP_MATH_ALL)
  36288. ret = mp_tohex(NULL, NULL);
  36289. if (ret != MP_VAL)
  36290. return -12962;
  36291. ret = mp_tohex(a, NULL);
  36292. if (ret != MP_VAL)
  36293. return -12963;
  36294. ret = mp_tohex(NULL, hexStr);
  36295. if (ret != MP_VAL)
  36296. return -12964;
  36297. #endif
  36298. #if defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  36299. ret = mp_todecimal(NULL, NULL);
  36300. if (ret != MP_VAL)
  36301. return -12965;
  36302. ret = mp_todecimal(a, NULL);
  36303. if (ret != MP_VAL)
  36304. return -12966;
  36305. ret = mp_todecimal(NULL, decStr);
  36306. if (ret != MP_VAL)
  36307. return -12967;
  36308. #endif
  36309. #ifdef WOLFSSL_SP_MATH_ALL
  36310. ret = mp_toradix(NULL, NULL, MP_RADIX_HEX);
  36311. if (ret != MP_VAL)
  36312. return -12968;
  36313. ret = mp_toradix(a, NULL, MP_RADIX_HEX);
  36314. if (ret != MP_VAL)
  36315. return -12969;
  36316. ret = mp_toradix(NULL, hexStr, MP_RADIX_HEX);
  36317. if (ret != MP_VAL)
  36318. return -12970;
  36319. ret = mp_toradix(a, hexStr, 3);
  36320. if (ret != MP_VAL)
  36321. return -12971;
  36322. ret = mp_radix_size(NULL, MP_RADIX_HEX, NULL);
  36323. if (ret != MP_VAL)
  36324. return -12972;
  36325. ret = mp_radix_size(a, MP_RADIX_HEX, NULL);
  36326. if (ret != MP_VAL)
  36327. return -12973;
  36328. ret = mp_radix_size(NULL, MP_RADIX_HEX, &size);
  36329. if (ret != MP_VAL)
  36330. return -12974;
  36331. ret = mp_radix_size(a, 3, &size);
  36332. if (ret != MP_VAL)
  36333. return -12975;
  36334. #endif
  36335. return 0;
  36336. }
  36337. #endif
  36338. #if !defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  36339. static int mp_test_set_is_bit(mp_int* a)
  36340. {
  36341. int i, j;
  36342. mp_zero(a);
  36343. for (i = 0; i <= DIGIT_BIT * 2; i++) {
  36344. if (mp_is_bit_set(a, i))
  36345. return -12980;
  36346. for (j = 0; j < i; j++) {
  36347. if (!mp_is_bit_set(a, j))
  36348. return -12981;
  36349. }
  36350. if (mp_set_bit(a, i) != 0)
  36351. return -12982;
  36352. if (!mp_is_bit_set(a, i))
  36353. return -12983;
  36354. }
  36355. mp_zero(a);
  36356. for (i = 0; i <= DIGIT_BIT * 2; i++) {
  36357. if (mp_is_bit_set(a, i))
  36358. return -12984;
  36359. }
  36360. for (i = 0; i <= DIGIT_BIT * 2; i++) {
  36361. mp_zero(a);
  36362. if (mp_set_bit(a, i) != 0)
  36363. return -12985;
  36364. for (j = 0; j < i; j++) {
  36365. if (mp_is_bit_set(a, j))
  36366. return -12986;
  36367. }
  36368. if (!mp_is_bit_set(a, i))
  36369. return -12987;
  36370. }
  36371. #ifdef WOLFSSL_KEY_GEN
  36372. for (i = 0; i < DIGIT_BIT * 2; i++) {
  36373. mp_set(a, 1);
  36374. if (mp_2expt(a, i) != 0)
  36375. return -12988;
  36376. for (j = 0; j < i; j++) {
  36377. if (mp_is_bit_set(a, j))
  36378. return -12989;
  36379. }
  36380. if (!mp_is_bit_set(a, i))
  36381. return -12990;
  36382. }
  36383. #endif
  36384. #ifdef WOLFSSL_SP_MATH
  36385. mp_zero(a);
  36386. for (j = 1; j <= 3; j++) {
  36387. i = SP_INT_MAX_BITS - j;
  36388. if (mp_is_bit_set(a, i))
  36389. return -12991;
  36390. if (mp_set_bit(a, i) != 0)
  36391. return -12992;
  36392. if (!mp_is_bit_set(a, i))
  36393. return -12993;
  36394. #ifdef WOLFSSL_KEY_GEN
  36395. if (mp_2expt(a, i) != 0)
  36396. return -12994;
  36397. if (!mp_is_bit_set(a, i))
  36398. return -12995;
  36399. #endif
  36400. }
  36401. mp_zero(a);
  36402. for (j = 0; j <= 3; j++) {
  36403. i = SP_INT_MAX_BITS + j;
  36404. if (mp_is_bit_set(a, i))
  36405. return -12996;
  36406. if (mp_set_bit(a, i) != MP_VAL)
  36407. return -12997;
  36408. #ifdef WOLFSSL_KEY_GEN
  36409. if (mp_2expt(a, i) != MP_VAL)
  36410. return -12998;
  36411. #endif
  36412. }
  36413. #endif
  36414. return 0;
  36415. }
  36416. #endif /* !WOLFSSL_SP_MATH || WOLFSSL_SP_MATH_ALL */
  36417. static int mp_test_cmp(mp_int* a, mp_int* b)
  36418. {
  36419. int ret;
  36420. mp_zero(a);
  36421. mp_zero(b);
  36422. ret = mp_cmp_d(a, 0);
  36423. if (ret != MP_EQ)
  36424. return -13000;
  36425. ret = mp_cmp_d(a, 1);
  36426. if (ret != MP_LT)
  36427. return -13001;
  36428. ret = mp_cmp(a, b);
  36429. if (ret != MP_EQ)
  36430. return -13002;
  36431. mp_set(a, 1);
  36432. ret = mp_cmp_d(a, 0);
  36433. if (ret != MP_GT)
  36434. return -13003;
  36435. ret = mp_cmp_d(a, 1);
  36436. if (ret != MP_EQ)
  36437. return -13004;
  36438. ret = mp_cmp_d(a, 2);
  36439. if (ret != MP_LT)
  36440. return -13005;
  36441. ret = mp_cmp(a, b);
  36442. if (ret != MP_GT)
  36443. return -13006;
  36444. mp_read_radix(b, "1234567890123456789", MP_RADIX_HEX);
  36445. ret = mp_cmp_d(b, -1);
  36446. if (ret != MP_GT)
  36447. return -13007;
  36448. ret = mp_cmp(a, b);
  36449. if (ret != MP_LT)
  36450. return -13008;
  36451. ret = mp_cmp(b, a);
  36452. if (ret != MP_GT)
  36453. return -13009;
  36454. ret = mp_cmp(b, b);
  36455. if (ret != MP_EQ)
  36456. return -13010;
  36457. #if (!defined(WOLFSSL_SP_MATH) && !defined(WOLFSSL_SP_MATH_ALL)) || \
  36458. defined(WOLFSSL_SP_INT_NEGATIVE)
  36459. mp_read_radix(a, "-1", MP_RADIX_HEX);
  36460. mp_read_radix(a, "1", MP_RADIX_HEX);
  36461. ret = mp_cmp(a, b);
  36462. if (ret != MP_LT)
  36463. return -13011;
  36464. ret = mp_cmp(b, a);
  36465. if (ret != MP_GT)
  36466. return -13012;
  36467. mp_read_radix(b, "-2", MP_RADIX_HEX);
  36468. ret = mp_cmp(a, b);
  36469. if (ret != MP_GT)
  36470. return -13013;
  36471. ret = mp_cmp(b, a);
  36472. if (ret != MP_LT)
  36473. return -13014;
  36474. mp_read_radix(a, "-2", MP_RADIX_HEX);
  36475. ret = mp_cmp(a, b);
  36476. if (ret != MP_EQ)
  36477. return -13015;
  36478. #endif
  36479. return 0;
  36480. }
  36481. #if !defined(NO_DH) || defined(HAVE_ECC) || !defined(WOLFSSL_RSA_VERIFY_ONLY)
  36482. static int mp_test_shbd(mp_int* a, mp_int* b, WC_RNG* rng)
  36483. {
  36484. int ret;
  36485. int i, j, k;
  36486. #ifndef WOLFSSL_SP_MATH
  36487. for (i = 0; i < 10; i++) {
  36488. for (j = 1; j < (DIGIT_BIT + 7) / 8 * 3; j++) {
  36489. ret = randNum(a, j, rng, NULL);
  36490. if (ret != MP_OKAY)
  36491. return -13020;
  36492. mp_copy(a, b);
  36493. for (k = 0; k <= DIGIT_BIT * 2; k++) {
  36494. ret = mp_mul_2d(a, k, a);
  36495. if (ret != MP_OKAY)
  36496. return -13021;
  36497. mp_rshb(a, k);
  36498. if (mp_cmp(a, b) != MP_EQ)
  36499. return -13022;
  36500. }
  36501. }
  36502. }
  36503. #endif
  36504. for (i = 0; i < 10; i++) {
  36505. for (j = 1; j < (DIGIT_BIT + 7) / 8 * 3; j++) {
  36506. ret = randNum(a, j, rng, NULL);
  36507. if (ret != MP_OKAY)
  36508. return -13023;
  36509. mp_copy(a, b);
  36510. for (k = 0; k < 10; k++) {
  36511. ret = mp_lshd(a, k);
  36512. if (ret != MP_OKAY)
  36513. return -13024;
  36514. #ifndef WOLFSSL_SP_MATH
  36515. mp_rshd(a, k);
  36516. #else
  36517. mp_rshb(a, k * SP_WORD_SIZE);
  36518. #endif
  36519. if (mp_cmp(a, b) != MP_EQ)
  36520. return -13025;
  36521. }
  36522. }
  36523. }
  36524. #ifndef WOLFSSL_SP_MATH
  36525. mp_zero(a);
  36526. mp_rshd(a, 1);
  36527. if (!mp_iszero(a))
  36528. return -13026;
  36529. mp_set(a, 1);
  36530. mp_rshd(a, 1);
  36531. if (!mp_iszero(a))
  36532. return -13027;
  36533. mp_set(a, 1);
  36534. mp_rshd(a, 2);
  36535. if (!mp_iszero(a))
  36536. return -13028;
  36537. #endif
  36538. return 0;
  36539. }
  36540. #endif
  36541. #if defined(WOLFSSL_SP_MATH_ALL) || !defined(NO_DH) || defined(HAVE_ECC) || \
  36542. (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY) && \
  36543. !defined(WOLFSSL_RSA_PUBLIC_ONLY))
  36544. static int mp_test_div(mp_int* a, mp_int* d, mp_int* r, mp_int* rem,
  36545. WC_RNG* rng)
  36546. {
  36547. int ret;
  36548. int i, j, k;
  36549. mp_zero(a);
  36550. mp_zero(d);
  36551. ret = mp_div(a, d, r, rem);
  36552. if (ret != MP_VAL)
  36553. return -13030;
  36554. mp_set(d, 1);
  36555. ret = mp_div(a, d, r, rem);
  36556. if (ret != MP_OKAY)
  36557. return -13031;
  36558. if (!mp_iszero(r))
  36559. return -13032;
  36560. if (!mp_iszero(rem))
  36561. return -13033;
  36562. mp_set(a, 1);
  36563. ret = mp_div(a, d, r, rem);
  36564. if (ret != MP_OKAY)
  36565. return -13034;
  36566. if (!mp_isone(r))
  36567. return -13035;
  36568. if (!mp_iszero(rem))
  36569. return -13036;
  36570. for (i = 0; i < 100; i++) {
  36571. for (j = 1; j < (DIGIT_BIT + 7) / 8 * 2; j++) {
  36572. ret = randNum(d, j, rng, NULL);
  36573. if (ret != MP_OKAY)
  36574. return -13037;
  36575. for (k = 1; k < (DIGIT_BIT + 7) / 8 * 2 + 1; k++) {
  36576. ret = randNum(a, k, rng, NULL);
  36577. if (ret != MP_OKAY)
  36578. return -13038;
  36579. ret = mp_div(a, d, NULL, rem);
  36580. if (ret != MP_OKAY)
  36581. return -13039;
  36582. ret = mp_div(a, d, r, NULL);
  36583. if (ret != MP_OKAY)
  36584. return -13040;
  36585. ret = mp_div(a, d, r, rem);
  36586. if (ret != MP_OKAY)
  36587. return -13041;
  36588. mp_mul(r, d, r);
  36589. mp_add(r, rem, r);
  36590. if (mp_cmp(r, a) != MP_EQ)
  36591. return -13042;
  36592. }
  36593. }
  36594. }
  36595. ret = randNum(d, (DIGIT_BIT + 7) / 8 * 2, rng, NULL);
  36596. if (ret != MP_OKAY)
  36597. return -13043;
  36598. mp_add(d, d, a);
  36599. mp_set(rem, 1);
  36600. mp_div(a, d, NULL, rem);
  36601. if (ret != MP_OKAY)
  36602. return -13044;
  36603. if (!mp_iszero(rem))
  36604. return -13045;
  36605. mp_set(r, 1);
  36606. mp_div(a, d, r, NULL);
  36607. if (ret != MP_OKAY)
  36608. return -13046;
  36609. if (mp_cmp_d(r, 2) != MP_EQ)
  36610. return -13047;
  36611. mp_set(r, 1);
  36612. mp_set(rem, 1);
  36613. mp_div(a, d, r, rem);
  36614. if (ret != MP_OKAY)
  36615. return -13048;
  36616. if (mp_cmp_d(r, 2) != MP_EQ)
  36617. return -13049;
  36618. if (!mp_iszero(rem))
  36619. return -13050;
  36620. mp_set(a, 0xfe);
  36621. mp_lshd(a, 3);
  36622. mp_add_d(a, 0xff, a);
  36623. mp_set(d, 0xfe);
  36624. mp_lshd(d, 2);
  36625. ret = mp_div(a, d, r, rem);
  36626. if (ret != MP_OKAY)
  36627. return -13051;
  36628. mp_mul(r, d, d);
  36629. mp_add(rem, d, d);
  36630. if (mp_cmp(a, d) != MP_EQ)
  36631. return -13052;
  36632. /* Force (hi | lo) / d to be (d | 0) / d which will would not fit in
  36633. * a digit. So mp_div must detect and handle.
  36634. * For example: 0x800000 / 0x8001, DIGIT_BIT = 8
  36635. */
  36636. mp_zero(a);
  36637. mp_set_bit(a, DIGIT_BIT * 3 - 1);
  36638. mp_zero(d);
  36639. mp_set_bit(d, DIGIT_BIT * 2 - 1);
  36640. mp_add_d(d, 1, d);
  36641. ret = mp_div(a, d, r, rem);
  36642. if (ret != MP_OKAY)
  36643. return -13053;
  36644. /* Make sure [d | d] / d is handled. */
  36645. mp_zero(a);
  36646. mp_set_bit(a, DIGIT_BIT * 2 - 1);
  36647. mp_set_bit(a, DIGIT_BIT * 1 - 1);
  36648. mp_zero(d);
  36649. mp_set_bit(d, DIGIT_BIT - 1);
  36650. ret = mp_div(a, d, r, rem);
  36651. if (ret != MP_OKAY)
  36652. return -13054;
  36653. mp_zero(a);
  36654. mp_set_bit(a, DIGIT_BIT);
  36655. mp_set_bit(a, 0);
  36656. mp_zero(d);
  36657. if (mp_cmp(r, a) != MP_EQ)
  36658. return -13055;
  36659. if (mp_cmp(rem, d) != MP_EQ)
  36660. return -13056;
  36661. return 0;
  36662. }
  36663. #endif
  36664. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_DH) || !defined(NO_DSA)) && \
  36665. !defined(WC_NO_RNG)
  36666. static int mp_test_prime(mp_int* a, WC_RNG* rng)
  36667. {
  36668. int ret;
  36669. int res;
  36670. ret = mp_rand_prime(a, 1, rng, NULL);
  36671. #if defined(WOLFSSL_SP_MATH_ALL)
  36672. if (ret != 0)
  36673. return -13060;
  36674. #else
  36675. if (ret != MP_VAL)
  36676. return -13060;
  36677. #endif
  36678. #ifndef WOLFSSL_SP_MATH
  36679. ret = mp_rand_prime(a, -5, rng, NULL);
  36680. if (ret != 0 || (a->dp[0] & 3) != 3)
  36681. return -13061;
  36682. #endif
  36683. ret = mp_prime_is_prime(a, 1, &res);
  36684. if (ret != MP_OKAY)
  36685. return -13062;
  36686. #ifndef WOLFSSL_SP_MATH
  36687. if (res != MP_YES)
  36688. return -13063;
  36689. #else
  36690. if (res != MP_NO)
  36691. return -13063;
  36692. #endif
  36693. ret = mp_prime_is_prime(a, 0, &res);
  36694. if (ret != MP_VAL)
  36695. return -13064;
  36696. ret = mp_prime_is_prime(a, -1, &res);
  36697. if (ret != MP_VAL)
  36698. return -13065;
  36699. ret = mp_prime_is_prime(a, 257, &res);
  36700. if (ret != MP_VAL)
  36701. return -13066;
  36702. mp_set(a, 1);
  36703. ret = mp_prime_is_prime(a, 1, &res);
  36704. if (ret != MP_OKAY)
  36705. return -13067;
  36706. if (res != MP_NO)
  36707. return -13068;
  36708. ret = mp_prime_is_prime_ex(a, 1, &res, rng);
  36709. if (ret != MP_OKAY)
  36710. return -13069;
  36711. if (res != MP_NO)
  36712. return -13070;
  36713. mp_set(a, 2);
  36714. ret = mp_prime_is_prime(a, 1, &res);
  36715. if (ret != MP_OKAY)
  36716. return -13071;
  36717. if (res != MP_YES)
  36718. return -13072;
  36719. ret = mp_prime_is_prime_ex(a, 1, &res, rng);
  36720. if (ret != MP_OKAY)
  36721. return -13073;
  36722. if (res != MP_YES)
  36723. return -13074;
  36724. mp_set(a, 0xfb);
  36725. ret = mp_prime_is_prime(a, 1, &res);
  36726. if (ret != MP_OKAY)
  36727. return -13075;
  36728. if (res != MP_YES)
  36729. return -13076;
  36730. ret = mp_prime_is_prime_ex(a, 1, &res, rng);
  36731. if (ret != MP_OKAY)
  36732. return -13077;
  36733. if (res != MP_YES)
  36734. return -13078;
  36735. mp_set(a, 0x6);
  36736. ret = mp_prime_is_prime(a, 1, &res);
  36737. if (ret != MP_OKAY)
  36738. return -13079;
  36739. if (res != MP_NO)
  36740. return -13080;
  36741. ret = mp_prime_is_prime_ex(a, 1, &res, rng);
  36742. if (ret != MP_OKAY)
  36743. return -13081;
  36744. if (res != MP_NO)
  36745. return -13082;
  36746. mp_set_int(a, 0x655 * 0x65b);
  36747. ret = mp_prime_is_prime(a, 10, &res);
  36748. if (ret != MP_OKAY)
  36749. return -13083;
  36750. if (res != MP_NO)
  36751. return -13084;
  36752. ret = mp_prime_is_prime_ex(a, 10, &res, rng);
  36753. if (ret != MP_OKAY)
  36754. return -13085;
  36755. if (res != MP_NO)
  36756. return -13086;
  36757. return 0;
  36758. }
  36759. #endif
  36760. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(WC_RSA_BLINDING)
  36761. static int mp_test_lcm_gcd(mp_int* a, mp_int* b, mp_int* r, mp_int* exp,
  36762. WC_RNG* rng)
  36763. {
  36764. int ret;
  36765. int i;
  36766. WOLFSSL_SMALL_STACK_STATIC const int kat[][3] = {
  36767. { 1, 1, 1 }, { 2, 1, 2 }, { 1, 2, 2 }, { 2, 4, 4 }, { 4, 2, 4 },
  36768. { 12, 56, 168 }, { 56, 12, 168 }
  36769. };
  36770. (void)exp;
  36771. mp_set(a, 0);
  36772. mp_set(b, 1);
  36773. ret = mp_lcm(a, a, r);
  36774. if (ret != MP_VAL)
  36775. return -13090;
  36776. ret = mp_lcm(a, b, r);
  36777. if (ret != MP_VAL)
  36778. return -13091;
  36779. ret = mp_lcm(b, a, r);
  36780. if (ret != MP_VAL)
  36781. return -13092;
  36782. for (i = 0; i < (int)(sizeof(kat) / sizeof(*kat)); i++) {
  36783. mp_set(a, kat[i][0]);
  36784. mp_set(b, kat[i][1]);
  36785. ret = mp_lcm(a, b, r);
  36786. if (ret != MP_OKAY)
  36787. return -13093;
  36788. mp_set(exp, kat[i][2]);
  36789. if (mp_cmp(r, exp) != MP_EQ)
  36790. return -13094;
  36791. }
  36792. (void)rng;
  36793. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_DH) || !defined(NO_DSA)) && \
  36794. !defined(WC_NO_RNG)
  36795. if (mp_rand_prime(a, 20, rng, NULL) != MP_OKAY)
  36796. return -13095;
  36797. if (mp_rand_prime(b, 20, rng, NULL) != MP_OKAY)
  36798. return -13096;
  36799. if (mp_mul(a, b, exp) != MP_OKAY)
  36800. return -13097;
  36801. ret = mp_lcm(a, b, r);
  36802. if (ret != MP_OKAY)
  36803. return -13098;
  36804. if (mp_cmp(r, exp) != MP_EQ)
  36805. return -13099;
  36806. ret = mp_lcm(b, a, r);
  36807. if (ret != MP_OKAY)
  36808. return -13100;
  36809. if (mp_cmp(r, exp) != MP_EQ)
  36810. return -13101;
  36811. #endif
  36812. mp_set(a, 11);
  36813. mp_zero(b);
  36814. ret = mp_gcd(a, b, r);
  36815. if (ret != MP_OKAY)
  36816. return -13102;
  36817. if (mp_cmp_d(r, 11) != MP_EQ)
  36818. return -13103;
  36819. ret = mp_gcd(b, a, r);
  36820. if (ret != MP_OKAY)
  36821. return -13104;
  36822. if (mp_cmp_d(r, 11) != MP_EQ)
  36823. return -13105;
  36824. ret = mp_gcd(b, b, r);
  36825. if (ret != MP_VAL)
  36826. return -13106;
  36827. return 0;
  36828. }
  36829. #endif
  36830. #if (!defined(WOLFSSL_SP_MATH) && !defined(USE_FAST_MATH)) || \
  36831. defined(WOLFSSL_SP_MATH_ALL)
  36832. static int mp_test_mod_2d(mp_int* a, mp_int* r, mp_int* t, WC_RNG* rng)
  36833. {
  36834. int ret;
  36835. int i;
  36836. int j;
  36837. mp_set(a, 10);
  36838. ret = mp_mod_2d(a, 0, r);
  36839. if (ret != MP_OKAY)
  36840. return -13110;
  36841. if (!mp_iszero(r))
  36842. return -13111;
  36843. ret = mp_mod_2d(a, 1, r);
  36844. if (ret != MP_OKAY)
  36845. return -13112;
  36846. if (!mp_iszero(r))
  36847. return -13113;
  36848. ret = mp_mod_2d(a, 2, r);
  36849. if (ret != MP_OKAY)
  36850. return -13114;
  36851. if (mp_cmp_d(r, 2))
  36852. return -13115;
  36853. for (i = 2; i < 20; i++) {
  36854. ret = randNum(a, i, rng, NULL);
  36855. if (ret != 0)
  36856. return -13116;
  36857. for (j = 1; j <= mp_count_bits(a); j++) {
  36858. /* Get top part */
  36859. ret = mp_div_2d(a, j, t, NULL);
  36860. if (ret != 0)
  36861. return -13117;
  36862. ret = mp_mul_2d(t, j, t);
  36863. if (ret != 0)
  36864. return -13118;
  36865. /* Get bottom part */
  36866. ret = mp_mod_2d(a, j, r);
  36867. if (ret != 0)
  36868. return -13119;
  36869. /* Reassemble */
  36870. ret = mp_add(t, r, r);
  36871. if (ret != 0)
  36872. return -13120;
  36873. if (mp_cmp(a, r) != MP_EQ)
  36874. return -13121;
  36875. }
  36876. }
  36877. #if !defined(WOLFSSL_SP_MATH) && defined(WOLFSSL_SP_INT_NEGATIVE)
  36878. /* Test negative value being moded. */
  36879. for (j = 0; j < 20; j++) {
  36880. ret = randNum(a, 2, rng, NULL);
  36881. if (ret != 0)
  36882. return -13122;
  36883. a->sign = MP_NEG;
  36884. for (i = 1; i < DIGIT_BIT * 3 + 1; i++) {
  36885. ret = mp_mod_2d(a, i, r);
  36886. if (ret != 0)
  36887. return -13124;
  36888. mp_zero(t);
  36889. ret = mp_set_bit(t, i);
  36890. if (ret != 0)
  36891. return -13125;
  36892. ret = mp_mod(a, t, t);
  36893. if (ret != 0)
  36894. return -13126;
  36895. if (mp_cmp(r, t) != MP_EQ)
  36896. return -13127;
  36897. }
  36898. }
  36899. #endif
  36900. return 0;
  36901. }
  36902. #endif
  36903. #if (defined(HAVE_ECC) && defined(HAVE_COMP_KEY)) || defined(WOLFSSL_KEY_GEN)
  36904. static int mp_test_mod_d(mp_int* a, WC_RNG* rng)
  36905. {
  36906. int ret;
  36907. mp_digit r;
  36908. mp_digit rem;
  36909. int i;
  36910. if (mp_set(a, 1) != MP_OKAY)
  36911. return -13130;
  36912. ret = mp_mod_d(a, 0, &r);
  36913. if (ret != MP_VAL)
  36914. return -13131;
  36915. mp_zero(a);
  36916. ret = mp_mod_d(a, 1, &r);
  36917. if (ret != MP_OKAY)
  36918. return -13132;
  36919. ret = mp_mod_d(a, 3, &r);
  36920. if (ret != MP_OKAY)
  36921. return -13133;
  36922. ret = mp_mod_d(a, 5, &r);
  36923. if (ret != MP_OKAY)
  36924. return -13134;
  36925. for (i = MP_MAX_TEST_BYTE_LEN - 16; i <= MP_MAX_TEST_BYTE_LEN; i++) {
  36926. ret = randNum(a, i, rng, NULL);
  36927. if (ret != MP_OKAY)
  36928. return -13135;
  36929. ret = mp_mod_d(a, 3, &r);
  36930. if (ret != MP_OKAY)
  36931. return -13136;
  36932. ret = mp_div_d(a, 3, a, &rem);
  36933. if (ret != MP_OKAY)
  36934. return -13137;
  36935. if (r != rem)
  36936. return -13138;
  36937. }
  36938. return 0;
  36939. }
  36940. #endif
  36941. static int mp_test_mul_sqr(mp_int* a, mp_int* b, mp_int* r1, mp_int* r2,
  36942. WC_RNG* rng)
  36943. {
  36944. int ret;
  36945. int i;
  36946. for (i = 1; i < 16; i++) {
  36947. ret = randNum(a, i, rng, NULL);
  36948. if (ret != 0)
  36949. return -13140;
  36950. ret = mp_mul(a, a, r1);
  36951. if (ret != 0)
  36952. return -13141;
  36953. ret = mp_sqr(a, r2);
  36954. if (ret != 0)
  36955. return -13142;
  36956. if (mp_cmp(r1, r2) != MP_EQ)
  36957. return -13143;
  36958. }
  36959. ret = mp_set(b, 0);
  36960. if (ret != MP_OKAY)
  36961. return -13144;
  36962. ret = mp_mul(a, b, r1);
  36963. if (ret != MP_OKAY)
  36964. return -13145;
  36965. if (!mp_iszero(r1))
  36966. return -13146;
  36967. ret = mp_sqr(b, r1);
  36968. if (ret != MP_OKAY)
  36969. return -13147;
  36970. if (!mp_iszero(r1))
  36971. return -13148;
  36972. #ifdef WOLFSSL_SP_MATH_ALL
  36973. ret = mp_set(a, 1);
  36974. if (ret != MP_OKAY)
  36975. return -13149;
  36976. i = (SP_INT_DIGITS / 2) + 1;
  36977. ret = mp_mul_2d(a, i * SP_WORD_SIZE - 1, a);
  36978. if (ret != MP_OKAY)
  36979. return -13150;
  36980. ret = mp_set(b, 1);
  36981. if (ret != MP_OKAY)
  36982. return -13151;
  36983. ret = mp_mul_2d(b, (SP_INT_DIGITS - 1 - i) * SP_WORD_SIZE - 1, b);
  36984. if (ret != MP_OKAY)
  36985. return -13152;
  36986. ret = mp_mul(a, b, r1);
  36987. if (ret != MP_OKAY)
  36988. return -13153;
  36989. ret = mp_mul(a, a, r1);
  36990. if (ret == MP_OKAY)
  36991. return -13154;
  36992. ret = mp_sqr(a, r1);
  36993. if (ret == MP_OKAY)
  36994. return -13155;
  36995. ret = mp_sqr(b, r1);
  36996. if (ret != MP_OKAY)
  36997. return -13156;
  36998. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH) || \
  36999. (defined(HAVE_ECC) && defined(FP_ECC))
  37000. ret = mp_mulmod(a, b, b, r1);
  37001. if (ret != MP_OKAY)
  37002. return -13157;
  37003. ret = mp_mulmod(a, a, b, r1);
  37004. if (ret == MP_OKAY)
  37005. return -13158;
  37006. #if defined(HAVE_ECC) && (defined(ECC_SHAMIR) || defined(FP_ECC))
  37007. ret = mp_sqrmod(a, b, r1);
  37008. if (ret == MP_OKAY)
  37009. return -13159;
  37010. ret = mp_sqrmod(b, a, r1);
  37011. if (ret != MP_OKAY)
  37012. return -13160;
  37013. #endif /* HAVE_ECC && (ECC_SHAMIR || FP_ECC) */
  37014. #endif /* WOLFSSL_SP_MATH_ALL || WOLFSSL_HAVE_SP_DH || (HAVE_ECC && FP_ECC) */
  37015. #endif /* WOLFSSL_SP_MATH_ALL */
  37016. return 0;
  37017. }
  37018. #if !defined(NO_RSA) || defined(HAVE_ECC) || !defined(NO_DSA) || \
  37019. defined(OPENSSL_EXTRA)
  37020. static int mp_test_invmod(mp_int* a, mp_int* m, mp_int* r)
  37021. {
  37022. int ret;
  37023. mp_set(a, 0);
  37024. mp_set(m, 1);
  37025. ret = mp_invmod(a, m, r);
  37026. if (ret != MP_VAL)
  37027. return -13170;
  37028. ret = mp_invmod(m, a, r);
  37029. if (ret != MP_VAL)
  37030. return -13171;
  37031. mp_set(a, 2);
  37032. mp_set(m, 4);
  37033. ret = mp_invmod(a, m, r);
  37034. if (ret != MP_VAL)
  37035. return -13172;
  37036. mp_set(a, 3);
  37037. mp_set(m, 6);
  37038. ret = mp_invmod(a, m, r);
  37039. if (ret != MP_VAL)
  37040. return -13181;
  37041. mp_set(a, 5*9);
  37042. mp_set(m, 6*9);
  37043. ret = mp_invmod(a, m, r);
  37044. if (ret != MP_VAL)
  37045. return -13182;
  37046. mp_set(a, 1);
  37047. mp_set(m, 4);
  37048. ret = mp_invmod(a, m, r);
  37049. if (ret != MP_OKAY)
  37050. return -13173;
  37051. if (!mp_isone(r))
  37052. return -13174;
  37053. mp_set(a, 3);
  37054. mp_set(m, 4);
  37055. ret = mp_invmod(a, m, r);
  37056. if (ret != MP_OKAY)
  37057. return -13175;
  37058. if (mp_cmp_d(r, 3))
  37059. return -13176;
  37060. mp_set(a, 3);
  37061. mp_set(m, 5);
  37062. ret = mp_invmod(a, m, r);
  37063. if (ret != MP_OKAY)
  37064. return -13177;
  37065. #if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  37066. /* Maximum 'a' */
  37067. mp_set(a, 0);
  37068. mp_set_bit(a, (r->size / 2)* SP_WORD_SIZE - 1);
  37069. mp_sub_d(a, 1, a);
  37070. /* Modulus too big. */
  37071. mp_set(m, 0);
  37072. mp_set_bit(m, (r->size / 2) * SP_WORD_SIZE);
  37073. ret = mp_invmod(a, m, r);
  37074. if (ret != MP_VAL)
  37075. return -13178;
  37076. /* Maximum modulus - even. */
  37077. mp_set(m, 0);
  37078. mp_set_bit(m, (r->size / 2) * SP_WORD_SIZE - 1);
  37079. ret = mp_invmod(a, m, r);
  37080. if (ret != MP_OKAY)
  37081. return -13179;
  37082. #endif
  37083. #if !defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_INT_NEGATIVE)
  37084. mp_read_radix(a, "-3", 16);
  37085. ret = mp_invmod(a, m, r);
  37086. if (ret != MP_OKAY)
  37087. return -13180;
  37088. #endif
  37089. #if defined(WOLFSSL_SP_MATH_ALL) && defined(HAVE_ECC)
  37090. mp_set(a, 0);
  37091. mp_set(m, 3);
  37092. ret = mp_invmod_mont_ct(a, m, r, 1);
  37093. if (ret != MP_VAL)
  37094. return -13190;
  37095. mp_set(a, 1);
  37096. mp_set(m, 0);
  37097. ret = mp_invmod_mont_ct(a, m, r, 1);
  37098. if (ret != MP_VAL)
  37099. return -13191;
  37100. mp_set(a, 1);
  37101. mp_set(m, 1);
  37102. ret = mp_invmod_mont_ct(a, m, r, 1);
  37103. if (ret != MP_VAL)
  37104. return -13192;
  37105. mp_set(a, 1);
  37106. mp_set(m, 2);
  37107. ret = mp_invmod_mont_ct(a, m, r, 1);
  37108. if (ret != MP_VAL)
  37109. return -13193;
  37110. mp_set(a, 1);
  37111. mp_set(m, 3);
  37112. ret = mp_invmod_mont_ct(a, m, r, 1);
  37113. if (ret != MP_OKAY)
  37114. return -13194;
  37115. #endif
  37116. return 0;
  37117. }
  37118. #endif /* !NO_RSA || HAVE_ECC || !NO_DSA || OPENSSL_EXTRA */
  37119. #if !defined(NO_RSA) || !defined(NO_DSA) || !defined(NO_DH) || \
  37120. (defined(HAVE_ECC) && defined(HAVE_COMP_KEY)) || defined(OPENSSL_EXTRA)
  37121. static int mp_test_exptmod(mp_int* b, mp_int* e, mp_int* m, mp_int* r)
  37122. {
  37123. int ret;
  37124. mp_set(b, 0x2);
  37125. mp_set(e, 0x3);
  37126. mp_set(m, 0x0);
  37127. ret = mp_exptmod_ex(b, e, 1, m, r);
  37128. if (ret != MP_VAL)
  37129. return -13190;
  37130. ret = mp_exptmod_nct(b, e, m, r);
  37131. if (ret != MP_VAL)
  37132. return -13191;
  37133. mp_set(b, 0x2);
  37134. mp_set(e, 0x3);
  37135. mp_set(m, 0x1);
  37136. ret = mp_exptmod_ex(b, e, 1, m, r);
  37137. if (ret != MP_OKAY)
  37138. return -13192;
  37139. if (!mp_iszero(r))
  37140. return -13193;
  37141. ret = mp_exptmod_nct(b, e, m, r);
  37142. if (ret != MP_OKAY)
  37143. return -13194;
  37144. if (!mp_iszero(r))
  37145. return -13195;
  37146. mp_set(b, 0x2);
  37147. mp_set(e, 0x0);
  37148. mp_set(m, 0x7);
  37149. ret = mp_exptmod_ex(b, e, 1, m, r);
  37150. if (ret != MP_OKAY)
  37151. return -13196;
  37152. if (!mp_isone(r))
  37153. return -13197;
  37154. ret = mp_exptmod_nct(b, e, m, r);
  37155. if (ret != MP_OKAY)
  37156. return -13198;
  37157. if (!mp_isone(r))
  37158. return -13199;
  37159. mp_set(b, 0x0);
  37160. mp_set(e, 0x3);
  37161. mp_set(m, 0x7);
  37162. ret = mp_exptmod_ex(b, e, 1, m, r);
  37163. if (ret != MP_OKAY)
  37164. return -13200;
  37165. if (!mp_iszero(r))
  37166. return -13201;
  37167. ret = mp_exptmod_nct(b, e, m, r);
  37168. if (ret != MP_OKAY)
  37169. return -13202;
  37170. if (!mp_iszero(r))
  37171. return -13203;
  37172. mp_set(b, 0x10);
  37173. mp_set(e, 0x3);
  37174. mp_set(m, 0x7);
  37175. ret = mp_exptmod_ex(b, e, 1, m, r);
  37176. if (ret != MP_OKAY)
  37177. return -13204;
  37178. ret = mp_exptmod_nct(b, e, m, r);
  37179. if (ret != MP_OKAY)
  37180. return -13205;
  37181. mp_set(b, 0x7);
  37182. mp_set(e, 0x3);
  37183. mp_set(m, 0x7);
  37184. ret = mp_exptmod_ex(b, e, 1, m, r);
  37185. if (ret != MP_OKAY)
  37186. return -13206;
  37187. if (!mp_iszero(r))
  37188. return -13207;
  37189. ret = mp_exptmod_nct(b, e, m, r);
  37190. if (ret != MP_OKAY)
  37191. return -13208;
  37192. if (!mp_iszero(r))
  37193. return -13209;
  37194. #ifndef WOLFSSL_SP_MATH
  37195. mp_set(b, 0x01);
  37196. mp_mul_2d(b, DIGIT_BIT, b);
  37197. mp_add_d(b, 1, b);
  37198. mp_set(e, 0x3);
  37199. mp_copy(b, m);
  37200. ret = mp_exptmod_ex(b, e, 1, m, r);
  37201. if (ret != MP_OKAY)
  37202. return -13210;
  37203. if (!mp_iszero(r))
  37204. return -13211;
  37205. ret = mp_exptmod_nct(b, e, m, r);
  37206. if (ret != MP_OKAY)
  37207. return -13212;
  37208. if (!mp_iszero(r))
  37209. return -13213;
  37210. #endif
  37211. mp_set(b, 0x2);
  37212. mp_set(e, 0x3);
  37213. mp_set(m, 0x7);
  37214. ret = mp_exptmod_ex(b, e, 1, m, r);
  37215. if (ret != MP_OKAY)
  37216. return -13214;
  37217. ret = mp_exptmod_nct(b, e, m, r);
  37218. if (ret != MP_OKAY)
  37219. return -13215;
  37220. #ifdef WOLFSSL_SP_MATH_ALL
  37221. mp_set(b, 0x2);
  37222. mp_set(e, 0x3);
  37223. mp_set(m, 0x01);
  37224. mp_mul_2d(m, SP_WORD_SIZE * SP_INT_DIGITS / 2, m);
  37225. mp_add_d(m, 0x01, m);
  37226. ret = mp_exptmod_ex(b, e, 1, m, r);
  37227. if (ret != MP_VAL)
  37228. return -13216;
  37229. ret = mp_exptmod_nct(b, e, m, r);
  37230. if (ret != MP_VAL)
  37231. return -13217;
  37232. #endif
  37233. return 0;
  37234. }
  37235. #endif /* !NO_RSA || !NO_DSA || !NO_DH || (HAVE_ECC && HAVE_COMP_KEY) ||
  37236. * OPENSSL_EXTRA */
  37237. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH) || \
  37238. defined(HAVE_ECC) || (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY))
  37239. static int mp_test_mont(mp_int* a, mp_int* m, mp_int* n, mp_int* r, WC_RNG* rng)
  37240. {
  37241. int ret;
  37242. mp_digit mp;
  37243. static int exp[] = { 7, 8, 16, 27, 32, 64,
  37244. 127, 128, 255, 256,
  37245. #if defined(SP_WORD_SIZE) && SP_WORD_SIZE > 8
  37246. 383, 384, 2033, 2048
  37247. #endif
  37248. };
  37249. static mp_digit sub[] = { 0x01, 0x05, 0x0f, 0x27, 0x05, 0x3b,
  37250. 0x01, 0x9f, 0x13, 0xbd,
  37251. #if defined(SP_WORD_SIZE) && SP_WORD_SIZE > 8
  37252. 0x1f, 0x13d, 0x45, 0x615
  37253. #endif
  37254. };
  37255. int bits[] = { 256, 384,
  37256. #if defined(SP_INT_MAX_BITS) && SP_INT_MAX_BITS > 4096
  37257. 2048,
  37258. #endif
  37259. #if defined(SP_INT_MAX_BITS) && SP_INT_MAX_BITS > 6144
  37260. 3072
  37261. #endif
  37262. };
  37263. int i;
  37264. int j;
  37265. for (i = 0; i < (int)(sizeof(exp) / sizeof(*exp)); i++) {
  37266. if (exp[i] >= DIGIT_BIT)
  37267. continue;
  37268. mp_zero(m);
  37269. ret = mp_set_bit(m, exp[i]);
  37270. if (ret != MP_OKAY)
  37271. return -13220;
  37272. ret = mp_sub_d(m, sub[i], m);
  37273. if (ret != MP_OKAY)
  37274. return -13221;
  37275. ret = mp_montgomery_setup(m, &mp);
  37276. if (ret != MP_OKAY)
  37277. return -13222;
  37278. ret = mp_montgomery_calc_normalization(n, m);
  37279. if (ret != MP_OKAY)
  37280. return -13223;
  37281. for (j = 0; j < 10; j++) {
  37282. ret = randNum(a, (exp[i] + DIGIT_BIT - 1) / DIGIT_BIT, rng, NULL);
  37283. if (ret != 0)
  37284. return -13224;
  37285. ret = mp_mod(a, m, a);
  37286. if (ret != 0)
  37287. return -13225;
  37288. /* r = a * a */
  37289. ret = mp_sqrmod(a, m, r);
  37290. if (ret != MP_OKAY)
  37291. return -13226;
  37292. /* Convert to Montgomery form = a*n */
  37293. ret = mp_mulmod(a, n, m, a);
  37294. if (ret != MP_OKAY)
  37295. return -13227;
  37296. /* a*a mod m == ((a*n) * (a*n)) / n / n */
  37297. ret = mp_sqr(a, a);
  37298. if (ret != MP_OKAY)
  37299. return -13228;
  37300. ret = mp_montgomery_reduce(a, m, mp);
  37301. if (ret != MP_OKAY)
  37302. return -13229;
  37303. ret = mp_montgomery_reduce(a, m, mp);
  37304. if (ret != MP_OKAY)
  37305. return -13230;
  37306. if (mp_cmp(a, r) != MP_EQ)
  37307. return -13231;
  37308. }
  37309. }
  37310. /* Force carries. */
  37311. for (i = 0; i < (int)(sizeof(bits) / sizeof(*bits)); i++) {
  37312. /* a = 2^(bits*2) - 1 */
  37313. mp_zero(a);
  37314. mp_set_bit(a, bits[i] * 2);
  37315. mp_sub_d(a, 1, a);
  37316. /* m = 2^(bits) - 1 */
  37317. mp_zero(m);
  37318. mp_set_bit(m, bits[i]);
  37319. mp_sub_d(m, 1, m);
  37320. mp = 1;
  37321. /* result = r = 2^(bits) - 1 */
  37322. mp_zero(r);
  37323. mp_set_bit(r, bits[i]);
  37324. mp_sub_d(r, 1, r);
  37325. ret = mp_montgomery_reduce(a, m, mp);
  37326. if (ret != MP_OKAY)
  37327. return -13240;
  37328. /* Result is m or 0 if reduced to range of modulus. */
  37329. if (mp_cmp(a, r) != MP_EQ && mp_iszero(a) != MP_YES)
  37330. return -13241;
  37331. }
  37332. return 0;
  37333. }
  37334. #endif
  37335. WOLFSSL_TEST_SUBROUTINE int mp_test(void)
  37336. {
  37337. WC_RNG rng;
  37338. int ret;
  37339. #if defined(HAVE_ECC) || defined(WOLFSSL_KEY_GEN)
  37340. int i, j;
  37341. #ifndef WOLFSSL_SP_MATH
  37342. int k;
  37343. #endif
  37344. mp_digit d;
  37345. #endif
  37346. mp_int a, b, r1, r2, p;
  37347. ret = mp_init_multi(&a, &b, &r1, &r2, NULL, NULL);
  37348. if (ret != 0)
  37349. return -13300;
  37350. #ifdef WOLFSSL_SP_MATH_ALL
  37351. mp_init_copy(&p, &a);
  37352. #else
  37353. ret = mp_init(&p);
  37354. if (ret != 0)
  37355. return -13301;
  37356. #endif
  37357. #ifndef HAVE_FIPS
  37358. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  37359. #else
  37360. ret = wc_InitRng(&rng);
  37361. #endif
  37362. if (ret != 0)
  37363. goto done;
  37364. #if defined(HAVE_ECC) || defined(WOLFSSL_KEY_GEN)
  37365. mp_set_int(&a, 0);
  37366. if (a.used != 0 || a.dp[0] != 0)
  37367. return -13302;
  37368. for (j = 1; j <= MP_MAX_TEST_BYTE_LEN; j++) {
  37369. for (i = 0; i < 4 * j; i++) {
  37370. /* New values to use. */
  37371. ret = randNum(&p, j, &rng, NULL);
  37372. if (ret != 0)
  37373. return -13303;
  37374. ret = randNum(&a, j, &rng, NULL);
  37375. if (ret != 0)
  37376. return -13304;
  37377. ret = randNum(&b, j, &rng, NULL);
  37378. if (ret != 0)
  37379. return -13305;
  37380. ret = wc_RNG_GenerateBlock(&rng, (byte*)&d, sizeof(d));
  37381. if (ret != 0)
  37382. return -13306;
  37383. d &= MP_MASK;
  37384. #if !defined(WOLFSSL_SP_MATH) || (defined(HAVE_ECC) && \
  37385. (defined(ECC_SHAMIR) || defined(FP_ECC)))
  37386. /* Ensure sqrmod produce same result as mulmod. */
  37387. ret = mp_sqrmod(&a, &p, &r1);
  37388. if (ret != 0)
  37389. return -13307;
  37390. ret = mp_mulmod(&a, &a, &p, &r2);
  37391. if (ret != 0)
  37392. return -13308;
  37393. if (mp_cmp(&r1, &r2) != 0)
  37394. return -13309;
  37395. #endif
  37396. #if defined(WOLFSSL_SP_MATH_ALL) && !defined(WOLFSSL_RSA_VERIFY_ONLY)
  37397. #if defined(WOLFSSL_SP_MATH) || (defined(WOLFSSL_SP_MATH_ALL) && \
  37398. !defined(WOLFSSL_SP_INT_NEGATIVE))
  37399. ret = mp_addmod(&a, &b, &p, &r1);
  37400. if (ret != 0)
  37401. return -13310;
  37402. ret = mp_submod(&r1, &b, &p, &r2);
  37403. if (ret != 0)
  37404. return -13311;
  37405. ret = mp_mod(&a, &p, &r1);
  37406. if (ret != 0)
  37407. return -13312;
  37408. if (mp_cmp(&r1, &r2) != MP_EQ)
  37409. return -13313;
  37410. #else
  37411. /* Ensure add with mod produce same result as sub with mod. */
  37412. ret = mp_addmod(&a, &b, &p, &r1);
  37413. if (ret != 0)
  37414. return -13314;
  37415. b.sign ^= 1;
  37416. ret = mp_submod(&a, &b, &p, &r2);
  37417. if (ret != 0)
  37418. return -13315;
  37419. if (mp_cmp(&r1, &r2) != 0)
  37420. return -13316;
  37421. #endif
  37422. #endif
  37423. /* Ensure add digit produce same result as sub digit. */
  37424. ret = mp_add_d(&a, d, &r1);
  37425. if (ret != 0)
  37426. return -13317;
  37427. ret = mp_sub_d(&r1, d, &r2);
  37428. if (ret != 0)
  37429. return -13318;
  37430. if (mp_cmp(&a, &r2) != 0)
  37431. return -13319;
  37432. /* Invert - if p is even it will use the slow impl.
  37433. * - if p and a are even it will fail.
  37434. */
  37435. ret = mp_invmod(&a, &p, &r1);
  37436. if (ret != 0 && ret != MP_VAL)
  37437. return -13320;
  37438. ret = 0;
  37439. #ifndef WOLFSSL_SP_MATH
  37440. /* Shift up and down number all bits in a digit. */
  37441. for (k = 0; k < DIGIT_BIT; k++) {
  37442. mp_mul_2d(&a, k, &r1);
  37443. mp_div_2d(&r1, k, &r2, &p);
  37444. if (mp_cmp(&a, &r2) != 0)
  37445. return -13321;
  37446. if (!mp_iszero(&p))
  37447. return -13322;
  37448. mp_rshb(&r1, k);
  37449. if (mp_cmp(&a, &r1) != 0)
  37450. return -13323;
  37451. }
  37452. #endif
  37453. }
  37454. }
  37455. /* Test adding and subtracting zero from zero. */
  37456. mp_zero(&a);
  37457. ret = mp_add_d(&a, 0, &r1);
  37458. if (ret != 0)
  37459. return -13329;
  37460. if (!mp_iszero(&r1)) {
  37461. return -13330;
  37462. }
  37463. ret = mp_sub_d(&a, 0, &r2);
  37464. if (ret != 0)
  37465. return -13331;
  37466. if (!mp_iszero(&r2)) {
  37467. return -13332;
  37468. }
  37469. #if DIGIT_BIT >= 32
  37470. /* Check that setting a 32-bit digit works. */
  37471. d &= 0xffffffffU;
  37472. mp_set_int(&a, d);
  37473. if (a.used != 1 || a.dp[0] != d)
  37474. return -13324;
  37475. #endif
  37476. /* Check setting a bit and testing a bit works. */
  37477. for (i = 0; i < MP_MAX_TEST_BYTE_LEN * 8; i++) {
  37478. mp_zero(&a);
  37479. mp_set_bit(&a, i);
  37480. if (!mp_is_bit_set(&a, i))
  37481. return -13325;
  37482. }
  37483. #endif
  37484. #if defined(HAVE_ECC) && defined(HAVE_COMP_KEY)
  37485. mp_zero(&a);
  37486. i = mp_cnt_lsb(&a);
  37487. if (i != 0)
  37488. return -13326;
  37489. mp_set(&a, 1);
  37490. i = mp_cnt_lsb(&a);
  37491. if (i != 0)
  37492. return -13327;
  37493. mp_set(&a, 32);
  37494. i = mp_cnt_lsb(&a);
  37495. if (i != 5)
  37496. return -13328;
  37497. mp_zero(&a);
  37498. mp_set_bit(&a, 129);
  37499. i = mp_cnt_lsb(&a);
  37500. if (i != 129)
  37501. return -13328;
  37502. #endif
  37503. #if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  37504. if ((ret = mp_test_param(&a, &b, &r1, &rng)) != 0)
  37505. return ret;
  37506. #endif
  37507. #if defined(WOLFSSL_SP_MATH_ALL) || !defined(USE_FAST_MATH)
  37508. if ((ret = mp_test_div_3(&a, &r1, &rng)) != 0)
  37509. return ret;
  37510. #endif
  37511. #if (defined(WOLFSSL_SP_MATH_ALL) && !defined(NO_RSA) && \
  37512. !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
  37513. (!defined WOLFSSL_SP_MATH && !defined(WOLFSSL_SP_MATH_ALL) && \
  37514. (defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)))
  37515. if ((ret = mp_test_radix_10(&a, &r1, &rng)) != 0)
  37516. return ret;
  37517. #endif
  37518. #if defined(WOLFSSL_SP_MATH_ALL) || (!defined WOLFSSL_SP_MATH && \
  37519. defined(HAVE_ECC))
  37520. if ((ret = mp_test_radix_16(&a, &r1, &rng)) != 0)
  37521. return ret;
  37522. #endif
  37523. if ((ret = mp_test_shift(&a, &r1, &rng)) != 0)
  37524. return ret;
  37525. if ((ret = mp_test_add_sub_d(&a, &r1)) != 0)
  37526. return ret;
  37527. if ((ret = mp_test_read_to_bin(&a)) != 0)
  37528. return ret;
  37529. #if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  37530. if ((ret = mp_test_set_int(&a)) != 0)
  37531. return ret;
  37532. #endif
  37533. if ((ret = mp_test_cmp(&a, &r1)) != 0)
  37534. return ret;
  37535. #if !defined(NO_DH) || defined(HAVE_ECC) || !defined(WOLFSSL_RSA_VERIFY_ONLY)
  37536. if ((ret = mp_test_shbd(&a, &b, &rng)) != 0)
  37537. return ret;
  37538. #endif
  37539. #if !defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  37540. if ((ret = mp_test_set_is_bit(&a)) != 0)
  37541. return ret;
  37542. #endif
  37543. #if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  37544. if ((ret = mp_test_div(&a, &b, &r1, &r2, &rng)) != 0)
  37545. return ret;
  37546. #endif
  37547. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_DH) || !defined(NO_DSA)) && \
  37548. !defined(WC_NO_RNG)
  37549. if ((ret = mp_test_prime(&a, &rng)) != 0)
  37550. return ret;
  37551. #endif
  37552. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(WC_RSA_BLINDING)
  37553. if ((ret = mp_test_lcm_gcd(&a, &b, &r1, &r2, &rng)) != 0)
  37554. return ret;
  37555. #endif
  37556. #if (!defined(WOLFSSL_SP_MATH) && !defined(USE_FAST_MATH)) || \
  37557. defined(WOLFSSL_SP_MATH_ALL)
  37558. if ((ret = mp_test_mod_2d(&a, &r1, &p, &rng)) != 0)
  37559. return ret;
  37560. #endif
  37561. #if (defined(HAVE_ECC) && defined(HAVE_COMP_KEY)) || defined(WOLFSSL_KEY_GEN)
  37562. if ((ret = mp_test_mod_d(&a, &rng)) != 0)
  37563. return ret;
  37564. #endif
  37565. if ((ret = mp_test_mul_sqr(&a, &b, &r1, &r2, &rng)) != 0)
  37566. return ret;
  37567. #if !defined(NO_RSA) || defined(HAVE_ECC) || !defined(NO_DSA) || \
  37568. defined(OPENSSL_EXTRA)
  37569. if ((ret = mp_test_invmod(&a, &b, &r1)) != 0)
  37570. return ret;
  37571. #endif
  37572. #if !defined(NO_RSA) || !defined(NO_DSA) || !defined(NO_DH) || \
  37573. (defined(HAVE_ECC) && defined(HAVE_COMP_KEY)) || defined(OPENSSL_EXTRA)
  37574. if ((ret = mp_test_exptmod(&a, &b, &r1, &r2)) != 0)
  37575. return ret;
  37576. #endif
  37577. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH) || \
  37578. defined(HAVE_ECC) || (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY))
  37579. if ((ret = mp_test_mont(&a, &b, &r1, &r2, &rng)) != 0)
  37580. return ret;
  37581. #endif
  37582. done:
  37583. mp_clear(&p);
  37584. mp_clear(&r2);
  37585. mp_clear(&r1);
  37586. mp_clear(&b);
  37587. mp_clear(&a);
  37588. wc_FreeRng(&rng);
  37589. return ret;
  37590. }
  37591. #endif
  37592. #if defined(WOLFSSL_PUBLIC_MP) && defined(WOLFSSL_KEY_GEN)
  37593. typedef struct pairs_t {
  37594. const unsigned char* coeff;
  37595. int coeffSz;
  37596. int exp;
  37597. } pairs_t;
  37598. /*
  37599. n =p1p2p3, where pi = ki(p1-1)+1 with (k2,k3) = (173,293)
  37600. p1 = 2^192 * 0x000000000000e24fd4f6d6363200bf2323ec46285cac1d3a
  37601. + 2^0 * 0x0b2488b0c29d96c5e67f8bec15b54b189ae5636efe89b45b
  37602. */
  37603. static const unsigned char c192a[] =
  37604. {
  37605. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe2, 0x4f,
  37606. 0xd4, 0xf6, 0xd6, 0x36, 0x32, 0x00, 0xbf, 0x23,
  37607. 0x23, 0xec, 0x46, 0x28, 0x5c, 0xac, 0x1d, 0x3a
  37608. };
  37609. static const unsigned char c0a[] =
  37610. {
  37611. 0x0b, 0x24, 0x88, 0xb0, 0xc2, 0x9d, 0x96, 0xc5,
  37612. 0xe6, 0x7f, 0x8b, 0xec, 0x15, 0xb5, 0x4b, 0x18,
  37613. 0x9a, 0xe5, 0x63, 0x6e, 0xfe, 0x89, 0xb4, 0x5b
  37614. };
  37615. static const pairs_t ecPairsA[] =
  37616. {
  37617. {c192a, sizeof(c192a), 192},
  37618. {c0a, sizeof(c0a), 0}
  37619. };
  37620. static const int kA[] = {173, 293};
  37621. static const unsigned char controlPrime[] = {
  37622. 0xe1, 0x76, 0x45, 0x80, 0x59, 0xb6, 0xd3, 0x49,
  37623. 0xdf, 0x0a, 0xef, 0x12, 0xd6, 0x0f, 0xf0, 0xb7,
  37624. 0xcb, 0x2a, 0x37, 0xbf, 0xa7, 0xf8, 0xb5, 0x4d,
  37625. 0xf5, 0x31, 0x35, 0xad, 0xe4, 0xa3, 0x94, 0xa1,
  37626. 0xdb, 0xf1, 0x96, 0xad, 0xb5, 0x05, 0x64, 0x85,
  37627. 0x83, 0xfc, 0x1b, 0x5b, 0x29, 0xaa, 0xbe, 0xf8,
  37628. 0x26, 0x3f, 0x76, 0x7e, 0xad, 0x1c, 0xf0, 0xcb,
  37629. 0xd7, 0x26, 0xb4, 0x1b, 0x05, 0x8e, 0x56, 0x86,
  37630. 0x7e, 0x08, 0x62, 0x21, 0xc1, 0x86, 0xd6, 0x47,
  37631. 0x79, 0x3e, 0xb7, 0x5d, 0xa4, 0xc6, 0x3a, 0xd7,
  37632. 0xb1, 0x74, 0x20, 0xf6, 0x50, 0x97, 0x41, 0x04,
  37633. 0x53, 0xed, 0x3f, 0x26, 0xd6, 0x6f, 0x91, 0xfa,
  37634. 0x68, 0x26, 0xec, 0x2a, 0xdc, 0x9a, 0xf1, 0xe7,
  37635. 0xdc, 0xfb, 0x73, 0xf0, 0x79, 0x43, 0x1b, 0x21,
  37636. 0xa3, 0x59, 0x04, 0x63, 0x52, 0x07, 0xc9, 0xd7,
  37637. 0xe6, 0xd1, 0x1b, 0x5d, 0x5e, 0x96, 0xfa, 0x53
  37638. };
  37639. static const unsigned char testOne[] = { 1 };
  37640. static int GenerateNextP(mp_int* p1, mp_int* p2, int k)
  37641. {
  37642. int ret;
  37643. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  37644. mp_int *ki = (mp_int *)XMALLOC(sizeof(*ki), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37645. if (ki == NULL)
  37646. return MEMORY_E;
  37647. #else
  37648. mp_int ki[1];
  37649. #endif
  37650. ret = mp_init(ki);
  37651. if (ret == 0)
  37652. ret = mp_set(ki, k);
  37653. if (ret == 0)
  37654. ret = mp_sub_d(p1, 1, p2);
  37655. if (ret == 0)
  37656. ret = mp_mul(p2, ki, p2);
  37657. if (ret == 0)
  37658. ret = mp_add_d(p2, 1, p2);
  37659. mp_clear(ki);
  37660. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  37661. XFREE(ki, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37662. #endif
  37663. return ret;
  37664. }
  37665. static int GenerateP(mp_int* p1, mp_int* p2, mp_int* p3,
  37666. const pairs_t* ecPairs, int ecPairsSz,
  37667. const int* k)
  37668. {
  37669. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  37670. mp_int *x = NULL, *y = NULL;
  37671. #else
  37672. mp_int x[1], y[1];
  37673. #endif
  37674. int ret, i;
  37675. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  37676. if (((x = (mp_int *)XMALLOC(sizeof(*x), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER)) == NULL) ||
  37677. ((y = (mp_int *)XMALLOC(sizeof(*x), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER)) == NULL)) {
  37678. ret = MEMORY_E;
  37679. goto out;
  37680. }
  37681. #endif
  37682. ret = mp_init_multi(x, y, NULL, NULL, NULL, NULL);
  37683. if (ret != 0) {
  37684. ret = MP_MEM;
  37685. goto out;
  37686. }
  37687. for (i = 0; ret == 0 && i < ecPairsSz; i++) {
  37688. ret = mp_read_unsigned_bin(x, ecPairs[i].coeff, ecPairs[i].coeffSz);
  37689. /* p1 = 2^exp */
  37690. if (ret == 0)
  37691. ret = mp_2expt(y, ecPairs[i].exp);
  37692. /* p1 = p1 * m */
  37693. if (ret == 0)
  37694. ret = mp_mul(x, y, x);
  37695. /* p1 += */
  37696. if (ret == 0)
  37697. ret = mp_add(p1, x, p1);
  37698. mp_zero(x);
  37699. mp_zero(y);
  37700. }
  37701. if (ret == 0)
  37702. ret = GenerateNextP(p1, p2, k[0]);
  37703. if (ret == 0)
  37704. ret = GenerateNextP(p1, p3, k[1]);
  37705. out:
  37706. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  37707. if (x != NULL) {
  37708. mp_clear(x);
  37709. XFREE(x, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37710. }
  37711. if (y != NULL) {
  37712. mp_clear(y);
  37713. XFREE(y, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37714. }
  37715. #else
  37716. mp_clear(x);
  37717. mp_clear(y);
  37718. #endif
  37719. return ret;
  37720. }
  37721. WOLFSSL_TEST_SUBROUTINE int prime_test(void)
  37722. {
  37723. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  37724. mp_int *n = (mp_int *)XMALLOC(sizeof *n, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER),
  37725. *p1 = (mp_int *)XMALLOC(sizeof *p1, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER),
  37726. *p2 = (mp_int *)XMALLOC(sizeof *p2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER),
  37727. *p3 = (mp_int *)XMALLOC(sizeof *p3, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37728. #else
  37729. mp_int n[1],
  37730. p1[1],
  37731. p2[1],
  37732. p3[1];
  37733. #endif
  37734. int ret, isPrime = 0;
  37735. WC_RNG rng;
  37736. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  37737. if ((n == NULL) ||
  37738. (p1 == NULL) ||
  37739. (p2 == NULL) ||
  37740. (p3 == NULL))
  37741. ERROR_OUT(MEMORY_E, out);
  37742. #endif
  37743. ret = wc_InitRng(&rng);
  37744. if (ret == 0)
  37745. ret = mp_init_multi(n, p1, p2, p3, NULL, NULL);
  37746. if (ret == 0)
  37747. ret = GenerateP(p1, p2, p3,
  37748. ecPairsA, sizeof(ecPairsA) / sizeof(ecPairsA[0]), kA);
  37749. if (ret == 0)
  37750. ret = mp_mul(p1, p2, n);
  37751. if (ret == 0)
  37752. ret = mp_mul(n, p3, n);
  37753. if (ret != 0)
  37754. ERROR_OUT(-13400, out);
  37755. /* Check the old prime test using the number that false positives.
  37756. * This test result should indicate as not prime. */
  37757. ret = mp_prime_is_prime(n, 40, &isPrime);
  37758. if (ret != 0)
  37759. ERROR_OUT(-13401, out);
  37760. if (isPrime)
  37761. ERROR_OUT(-13402, out);
  37762. /* This test result should fail. It should indicate the value as prime. */
  37763. ret = mp_prime_is_prime(n, 8, &isPrime);
  37764. if (ret != 0)
  37765. ERROR_OUT(-13403, out);
  37766. if (!isPrime)
  37767. ERROR_OUT(-13404, out);
  37768. /* This test result should indicate the value as not prime. */
  37769. ret = mp_prime_is_prime_ex(n, 8, &isPrime, &rng);
  37770. if (ret != 0)
  37771. ERROR_OUT(-13405, out);
  37772. if (isPrime)
  37773. ERROR_OUT(-13406, out);
  37774. ret = mp_read_unsigned_bin(n, controlPrime, sizeof(controlPrime));
  37775. if (ret != 0)
  37776. ERROR_OUT(-13407, out);
  37777. /* This test result should indicate the value as prime. */
  37778. ret = mp_prime_is_prime_ex(n, 8, &isPrime, &rng);
  37779. if (ret != 0)
  37780. ERROR_OUT(-13408, out);
  37781. if (!isPrime)
  37782. ERROR_OUT(-13409, out);
  37783. /* This test result should indicate the value as prime. */
  37784. isPrime = -1;
  37785. ret = mp_prime_is_prime(n, 8, &isPrime);
  37786. if (ret != 0)
  37787. ERROR_OUT(-13410, out);
  37788. if (!isPrime)
  37789. ERROR_OUT(-13411, out);
  37790. ret = mp_read_unsigned_bin(n, testOne, sizeof(testOne));
  37791. if (ret != 0)
  37792. ERROR_OUT(-13412, out);
  37793. /* This test result should indicate the value as not prime. */
  37794. ret = mp_prime_is_prime_ex(n, 8, &isPrime, &rng);
  37795. if (ret != 0)
  37796. ERROR_OUT(-13413, out);
  37797. if (isPrime)
  37798. ERROR_OUT(-13414, out);
  37799. ret = mp_prime_is_prime(n, 8, &isPrime);
  37800. if (ret != 0)
  37801. ERROR_OUT(-13415, out);
  37802. if (isPrime)
  37803. ERROR_OUT(-13416, out);
  37804. ret = 0;
  37805. out:
  37806. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  37807. if (n != NULL) {
  37808. mp_clear(n);
  37809. XFREE(n, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37810. }
  37811. if (p1 != NULL) {
  37812. mp_clear(p1);
  37813. XFREE(p1, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37814. }
  37815. if (p2 != NULL) {
  37816. mp_clear(p2);
  37817. XFREE(p2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37818. }
  37819. if (p3 != NULL) {
  37820. mp_clear(p3);
  37821. XFREE(p3, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37822. }
  37823. #else
  37824. mp_clear(p3);
  37825. mp_clear(p2);
  37826. mp_clear(p1);
  37827. mp_clear(n);
  37828. #endif
  37829. wc_FreeRng(&rng);
  37830. return ret;
  37831. }
  37832. #endif /* WOLFSSL_PUBLIC_MP */
  37833. #if defined(ASN_BER_TO_DER) && \
  37834. (defined(WOLFSSL_TEST_CERT) || defined(OPENSSL_EXTRA) || \
  37835. defined(OPENSSL_EXTRA_X509_SMALL))
  37836. /* wc_BerToDer is only public facing in the case of test cert or opensslextra */
  37837. typedef struct berDerTestData {
  37838. const byte *in;
  37839. word32 inSz;
  37840. const byte *out;
  37841. word32 outSz;
  37842. } berDerTestData;
  37843. WOLFSSL_TEST_SUBROUTINE int berder_test(void)
  37844. {
  37845. int ret;
  37846. int i;
  37847. word32 len = 0, l;
  37848. byte out[32];
  37849. WOLFSSL_SMALL_STACK_STATIC const byte good1_in[] = { 0x30, 0x80, 0x00, 0x00 };
  37850. WOLFSSL_SMALL_STACK_STATIC const byte good1_out[] = { 0x30, 0x00 };
  37851. WOLFSSL_SMALL_STACK_STATIC const byte good2_in[] = { 0x30, 0x80, 0x02, 0x01, 0x01, 0x00, 0x00 };
  37852. WOLFSSL_SMALL_STACK_STATIC const byte good2_out[] = { 0x30, 0x03, 0x02, 0x01, 0x01 };
  37853. WOLFSSL_SMALL_STACK_STATIC const byte good3_in[] = {
  37854. 0x24, 0x80, 0x04, 0x01, 0x01, 0x00, 0x00
  37855. };
  37856. WOLFSSL_SMALL_STACK_STATIC const byte good3_out[] = { 0x04, 0x1, 0x01 };
  37857. WOLFSSL_SMALL_STACK_STATIC const byte good4_in[] = {
  37858. 0x30, 0x80,
  37859. 0x02, 0x01, 0x01,
  37860. 0x30, 0x80,
  37861. 0x24, 0x80,
  37862. 0x04, 0x01, 0x01,
  37863. 0x04, 0x02, 0x02, 0x03,
  37864. 0x00, 0x00,
  37865. 0x06, 0x01, 0x01,
  37866. 0x00, 0x00,
  37867. 0x31, 0x80,
  37868. 0x06, 0x01, 0x01,
  37869. 0x00, 0x00,
  37870. 0x00, 0x00,
  37871. };
  37872. WOLFSSL_SMALL_STACK_STATIC const byte good4_out[] = {
  37873. 0x30, 0x12,
  37874. 0x02, 0x01, 0x01,
  37875. 0x30, 0x08,
  37876. 0x04, 0x03, 0x01, 0x02, 0x03,
  37877. 0x06, 0x01, 0x01,
  37878. 0x31, 0x03,
  37879. 0x06, 0x01, 0x01
  37880. };
  37881. WOLFSSL_SMALL_STACK_STATIC const byte good5_in[] = { 0x30, 0x03, 0x02, 0x01, 0x01 };
  37882. berDerTestData testData[] = {
  37883. { good1_in, sizeof(good1_in), good1_out, sizeof(good1_out) },
  37884. { good2_in, sizeof(good2_in), good2_out, sizeof(good2_out) },
  37885. { good3_in, sizeof(good3_in), good3_out, sizeof(good3_out) },
  37886. { good4_in, sizeof(good4_in), good4_out, sizeof(good4_out) },
  37887. { good5_in, sizeof(good5_in), good5_in , sizeof(good5_in ) },
  37888. };
  37889. for (i = 0; i < (int)(sizeof(testData) / sizeof(*testData)); i++) {
  37890. ret = wc_BerToDer(testData[i].in, testData[i].inSz, NULL, &len);
  37891. if (ret != LENGTH_ONLY_E)
  37892. return -13500 - i;
  37893. if (len != testData[i].outSz)
  37894. return -13510 - i;
  37895. len = testData[i].outSz;
  37896. ret = wc_BerToDer(testData[i].in, testData[i].inSz, out, &len);
  37897. if (ret != 0)
  37898. return -13520 - i;
  37899. if (XMEMCMP(out, testData[i].out, len) != 0)
  37900. return -13530 - i;
  37901. for (l = 1; l < testData[i].inSz; l++) {
  37902. ret = wc_BerToDer(testData[i].in, l, NULL, &len);
  37903. if (ret != ASN_PARSE_E)
  37904. return -13540;
  37905. len = testData[i].outSz;
  37906. ret = wc_BerToDer(testData[i].in, l, out, &len);
  37907. if (ret != ASN_PARSE_E)
  37908. return -13541;
  37909. }
  37910. for (l = 0; l < testData[i].outSz-1; l++) {
  37911. ret = wc_BerToDer(testData[i].in, testData[i].inSz, out, &l);
  37912. if (ret != BUFFER_E)
  37913. return -13542;
  37914. }
  37915. }
  37916. ret = wc_BerToDer(NULL, 4, NULL, NULL);
  37917. if (ret != BAD_FUNC_ARG)
  37918. return -13543;
  37919. ret = wc_BerToDer(out, 4, NULL, NULL);
  37920. if (ret != BAD_FUNC_ARG)
  37921. return -13544;
  37922. ret = wc_BerToDer(NULL, 4, NULL, &len);
  37923. if (ret != BAD_FUNC_ARG)
  37924. return -13545;
  37925. ret = wc_BerToDer(NULL, 4, out, NULL);
  37926. if (ret != BAD_FUNC_ARG)
  37927. return -13546;
  37928. ret = wc_BerToDer(out, 4, out, NULL);
  37929. if (ret != BAD_FUNC_ARG)
  37930. return -13547;
  37931. ret = wc_BerToDer(NULL, 4, out, &len);
  37932. if (ret != BAD_FUNC_ARG)
  37933. return -13548;
  37934. for (l = 1; l < sizeof(good4_out); l++) {
  37935. len = l;
  37936. ret = wc_BerToDer(good4_in, sizeof(good4_in), out, &len);
  37937. if (ret != BUFFER_E)
  37938. return -13549;
  37939. }
  37940. return 0;
  37941. }
  37942. #endif
  37943. #ifdef DEBUG_WOLFSSL
  37944. static THREAD_LS_T int log_cnt = 0;
  37945. static void my_Logging_cb(const int logLevel, const char *const logMessage)
  37946. {
  37947. (void)logLevel;
  37948. (void)logMessage;
  37949. log_cnt++;
  37950. }
  37951. #endif /* DEBUG_WOLFSSL */
  37952. WOLFSSL_TEST_SUBROUTINE int logging_test(void)
  37953. {
  37954. #ifdef DEBUG_WOLFSSL
  37955. const char* msg = "Testing, testing. 1, 2, 3, 4 ...";
  37956. byte a[8] = { 1, 2, 3, 4, 5, 6, 7, 8 };
  37957. byte b[256];
  37958. int i;
  37959. for (i = 0; i < (int)sizeof(b); i++)
  37960. b[i] = i;
  37961. if (wolfSSL_Debugging_ON() != 0)
  37962. return -13600;
  37963. if (wolfSSL_SetLoggingCb(my_Logging_cb) != 0)
  37964. return -13601;
  37965. WOLFSSL_MSG(msg);
  37966. WOLFSSL_BUFFER(a, sizeof(a));
  37967. WOLFSSL_BUFFER(b, sizeof(b));
  37968. WOLFSSL_BUFFER(NULL, 0);
  37969. WOLFSSL_ERROR(MEMORY_E);
  37970. WOLFSSL_ERROR_MSG(msg);
  37971. /* turn off logs */
  37972. wolfSSL_Debugging_OFF();
  37973. /* capture log count */
  37974. i = log_cnt;
  37975. /* validate no logs are output when disabled */
  37976. WOLFSSL_MSG(msg);
  37977. WOLFSSL_BUFFER(a, sizeof(a));
  37978. WOLFSSL_BUFFER(b, sizeof(b));
  37979. WOLFSSL_BUFFER(NULL, 0);
  37980. WOLFSSL_ERROR(MEMORY_E);
  37981. WOLFSSL_ERROR_MSG(msg);
  37982. /* check the logs were disabled */
  37983. if (i != log_cnt)
  37984. return -13602;
  37985. /* restore callback and leave logging enabled */
  37986. wolfSSL_SetLoggingCb(NULL);
  37987. wolfSSL_Debugging_ON();
  37988. /* suppress unused args */
  37989. (void)a;
  37990. (void)b;
  37991. #else
  37992. if (wolfSSL_Debugging_ON() != NOT_COMPILED_IN)
  37993. return -13603;
  37994. wolfSSL_Debugging_OFF();
  37995. if (wolfSSL_SetLoggingCb(NULL) != NOT_COMPILED_IN)
  37996. return -13604;
  37997. #endif /* DEBUG_WOLFSSL */
  37998. return 0;
  37999. }
  38000. WOLFSSL_TEST_SUBROUTINE int mutex_test(void)
  38001. {
  38002. #ifdef WOLFSSL_PTHREADS
  38003. wolfSSL_Mutex m;
  38004. #endif
  38005. #if !defined(WOLFSSL_NO_MALLOC) && !defined(WOLFSSL_USER_MUTEX)
  38006. #ifndef WOLFSSL_STATIC_MEMORY
  38007. wolfSSL_Mutex *mm = wc_InitAndAllocMutex();
  38008. #else
  38009. wolfSSL_Mutex *mm = (wolfSSL_Mutex*) XMALLOC(sizeof(wolfSSL_Mutex),
  38010. HEAP_HINT, DYNAMIC_TYPE_MUTEX);
  38011. if (mm != NULL) {
  38012. if (wc_InitMutex(mm) != 0) {
  38013. WOLFSSL_MSG("Init Mutex failed");
  38014. XFREE(mm, HEAP_HINT, DYNAMIC_TYPE_MUTEX);
  38015. mm = NULL;
  38016. }
  38017. }
  38018. #endif
  38019. if (mm == NULL)
  38020. return -13700;
  38021. wc_FreeMutex(mm);
  38022. XFREE(mm, HEAP_HINT, DYNAMIC_TYPE_MUTEX);
  38023. #endif
  38024. /* Can optionally enable advanced pthread tests using "ENABLE_PTHREAD_LOCKFREE_TESTS" */
  38025. #ifdef WOLFSSL_PTHREADS
  38026. if (wc_InitMutex(&m) != 0)
  38027. return -13701;
  38028. if (wc_LockMutex(&m) != 0)
  38029. return -13702;
  38030. #if !defined(WOLFSSL_SOLARIS) && defined(ENABLE_PTHREAD_LOCKFREE_TESTS)
  38031. /* trying to free a locked mutex is not portable behavior with pthread */
  38032. /* Attempting to destroy a locked mutex results in undefined behavior */
  38033. if (wc_FreeMutex(&m) != BAD_MUTEX_E)
  38034. return -13703;
  38035. #endif
  38036. if (wc_UnLockMutex(&m) != 0)
  38037. return -13704;
  38038. if (wc_FreeMutex(&m) != 0)
  38039. return -13705;
  38040. #if !defined(WOLFSSL_SOLARIS) && defined(ENABLE_PTHREAD_LOCKFREE_TESTS)
  38041. /* Trying to use a pthread after free'ing is not portable behavior */
  38042. if (wc_LockMutex(&m) != BAD_MUTEX_E)
  38043. return -13706;
  38044. if (wc_UnLockMutex(&m) != BAD_MUTEX_E)
  38045. return -13707;
  38046. #endif
  38047. #endif
  38048. return 0;
  38049. }
  38050. #if defined(USE_WOLFSSL_MEMORY) && !defined(FREERTOS)
  38051. #if !defined(WOLFSSL_NO_MALLOC) && !defined(WOLFSSL_LINUXKM) && \
  38052. !defined(WOLFSSL_STATIC_MEMORY)
  38053. static int malloc_cnt = 0;
  38054. static int realloc_cnt = 0;
  38055. static int free_cnt = 0;
  38056. #ifdef WOLFSSL_DEBUG_MEMORY
  38057. static void *my_Malloc_cb(size_t size, const char* func, unsigned int line)
  38058. {
  38059. (void) func;
  38060. (void) line;
  38061. #else
  38062. static void *my_Malloc_cb(size_t size)
  38063. {
  38064. #endif
  38065. malloc_cnt++;
  38066. #ifndef WOLFSSL_NO_MALLOC
  38067. return malloc(size);
  38068. #else
  38069. WOLFSSL_MSG("No malloc available");
  38070. (void)size;
  38071. return NULL;
  38072. #endif
  38073. }
  38074. #ifdef WOLFSSL_DEBUG_MEMORY
  38075. static void my_Free_cb(void *ptr, const char* func, unsigned int line)
  38076. {
  38077. (void) func;
  38078. (void) line;
  38079. #else
  38080. static void my_Free_cb(void *ptr)
  38081. {
  38082. #endif
  38083. free_cnt++;
  38084. #ifndef WOLFSSL_NO_MALLOC
  38085. free(ptr);
  38086. #else
  38087. WOLFSSL_MSG("No free available");
  38088. (void)ptr;
  38089. #endif
  38090. }
  38091. #ifdef WOLFSSL_DEBUG_MEMORY
  38092. static void *my_Realloc_cb(void *ptr, size_t size, const char* func, unsigned int line)
  38093. {
  38094. (void) func;
  38095. (void) line;
  38096. #else
  38097. static void *my_Realloc_cb(void *ptr, size_t size)
  38098. {
  38099. #endif
  38100. realloc_cnt++;
  38101. #ifndef WOLFSSL_NO_MALLOC
  38102. return realloc(ptr, size);
  38103. #else
  38104. WOLFSSL_MSG("No realloc available");
  38105. (void)ptr;
  38106. (void)size;
  38107. return NULL;
  38108. #endif
  38109. }
  38110. #endif /* !WOLFSSL_NO_MALLOC */
  38111. WOLFSSL_TEST_SUBROUTINE int memcb_test(void)
  38112. {
  38113. int ret = 0;
  38114. #if !defined(WOLFSSL_NO_MALLOC) && !defined(WOLFSSL_LINUXKM) && \
  38115. !defined(WOLFSSL_STATIC_MEMORY)
  38116. byte* b = NULL;
  38117. #endif
  38118. wolfSSL_Malloc_cb mc;
  38119. wolfSSL_Free_cb fc;
  38120. wolfSSL_Realloc_cb rc;
  38121. /* Save existing memory callbacks */
  38122. if (wolfSSL_GetAllocators(&mc, &fc, &rc) != 0)
  38123. return -13800;
  38124. #if !defined(WOLFSSL_NO_MALLOC) && !defined(WOLFSSL_LINUXKM) && \
  38125. !defined(WOLFSSL_STATIC_MEMORY)
  38126. /* test realloc */
  38127. b = (byte*)XREALLOC(b, 1024, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38128. if (b == NULL) {
  38129. ERROR_OUT(-13801, exit_memcb);
  38130. }
  38131. XFREE(b, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38132. b = NULL;
  38133. /* Use API. */
  38134. if (wolfSSL_SetAllocators((wolfSSL_Malloc_cb)my_Malloc_cb,
  38135. (wolfSSL_Free_cb)my_Free_cb,
  38136. (wolfSSL_Realloc_cb)my_Realloc_cb) != 0) {
  38137. ERROR_OUT(-13802, exit_memcb);
  38138. }
  38139. b = (byte*)XMALLOC(1024, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38140. b = (byte*)XREALLOC(b, 1024, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38141. XFREE(b, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38142. #ifndef WOLFSSL_STATIC_MEMORY
  38143. #ifndef WOLFSSL_CHECK_MEM_ZERO
  38144. if (malloc_cnt != 1 || free_cnt != 1 || realloc_cnt != 1)
  38145. #else
  38146. /* Checking zeroized memory means realloc is a malloc and free. */
  38147. if (malloc_cnt != 2 || free_cnt != 2 || realloc_cnt != 0)
  38148. #endif
  38149. #else
  38150. if (malloc_cnt != 0 || free_cnt != 0 || realloc_cnt != 0)
  38151. #endif
  38152. ret = -13803;
  38153. #endif /* !WOLFSSL_NO_MALLOC */
  38154. #if !defined(WOLFSSL_NO_MALLOC) && !defined(WOLFSSL_LINUXKM) && \
  38155. !defined(WOLFSSL_STATIC_MEMORY)
  38156. exit_memcb:
  38157. /* reset malloc/free/realloc counts */
  38158. malloc_cnt = 0;
  38159. free_cnt = 0;
  38160. realloc_cnt = 0;
  38161. #endif
  38162. /* restore memory callbacks */
  38163. wolfSSL_SetAllocators(mc, fc, rc);
  38164. return ret;
  38165. }
  38166. #endif /* USE_WOLFSSL_MEMORY && !WOLFSSL_NO_MALLOC */
  38167. #ifdef WOLFSSL_IMX6_CAAM_BLOB
  38168. WOLFSSL_TEST_SUBROUTINE int blob_test(void)
  38169. {
  38170. int ret = 0;
  38171. byte out[112];
  38172. byte blob[112];
  38173. word32 outSz;
  38174. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  38175. {
  38176. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  38177. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  38178. };
  38179. WOLFSSL_SMALL_STACK_STATIC const byte text[] =
  38180. {
  38181. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  38182. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  38183. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  38184. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  38185. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  38186. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  38187. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  38188. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  38189. };
  38190. XMEMSET(blob, 0, sizeof(blob));
  38191. XMEMSET(out, 0, sizeof(out));
  38192. outSz = sizeof(blob);
  38193. ret = wc_caamCreateBlob((byte*)iv, sizeof(iv), blob, &outSz);
  38194. if (ret != 0) {
  38195. ERROR_OUT(-13900, exit_blob);
  38196. }
  38197. blob[outSz - 2] += 1;
  38198. ret = wc_caamOpenBlob(blob, outSz, out, &outSz);
  38199. if (ret == 0) { /* should fail with altered blob */
  38200. ERROR_OUT(-13901, exit_blob);
  38201. }
  38202. XMEMSET(blob, 0, sizeof(blob));
  38203. outSz = sizeof(blob);
  38204. ret = wc_caamCreateBlob((byte*)iv, sizeof(iv), blob, &outSz);
  38205. if (ret != 0) {
  38206. ERROR_OUT(-13902, exit_blob);
  38207. }
  38208. ret = wc_caamOpenBlob(blob, outSz, out, &outSz);
  38209. if (ret != 0) {
  38210. ERROR_OUT(-13903, exit_blob);
  38211. }
  38212. if (XMEMCMP(out, iv, sizeof(iv))) {
  38213. ERROR_OUT(-13904, exit_blob);
  38214. }
  38215. XMEMSET(blob, 0, sizeof(blob));
  38216. outSz = sizeof(blob);
  38217. ret = wc_caamCreateBlob((byte*)text, sizeof(text), blob, &outSz);
  38218. if (ret != 0) {
  38219. ERROR_OUT(-13905, exit_blob);
  38220. }
  38221. ret = wc_caamOpenBlob(blob, outSz, out, &outSz);
  38222. if (ret != 0) {
  38223. ERROR_OUT(-13906, exit_blob);
  38224. }
  38225. if (XMEMCMP(out, text, sizeof(text))) {
  38226. ERROR_OUT(-13907, exit_blob);
  38227. }
  38228. exit_blob:
  38229. return ret;
  38230. }
  38231. #endif /* WOLFSSL_IMX6_CAAM_BLOB */
  38232. #ifdef WOLF_CRYPTO_CB
  38233. /* Example custom context for crypto callback */
  38234. typedef struct {
  38235. int exampleVar; /* flag for testing if only crypt is enabled. */
  38236. } myCryptoDevCtx;
  38237. #ifdef WOLF_CRYPTO_CB_ONLY_RSA
  38238. /* Testing rsa cb when CB_ONLY_RSA is enabled
  38239. * When CB_ONLY_RSA is enabled, software imple. is not available.
  38240. *
  38241. * ctx callback ctx
  38242. * returen 0 on success, otherwise return -8000 - -8007
  38243. */
  38244. static int rsa_onlycb_test(myCryptoDevCtx *ctx)
  38245. {
  38246. int ret = 0;
  38247. #if !defined(NO_RSA)
  38248. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  38249. RsaKey *key = (RsaKey *)XMALLOC(sizeof *key,
  38250. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38251. byte* tmp = NULL;
  38252. #else
  38253. RsaKey key[1];
  38254. byte tmp[FOURK_BUF];
  38255. #endif
  38256. size_t bytes;
  38257. const word32 inLen = (word32)TEST_STRING_SZ;
  38258. word32 idx = 0;
  38259. word32 sigSz;
  38260. WOLFSSL_SMALL_STACK_STATIC const byte in[] = TEST_STRING;
  38261. byte out[RSA_TEST_BYTES];
  38262. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  38263. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096) && \
  38264. !defined(NO_FILESYSTEM)
  38265. XFILE file;
  38266. #endif
  38267. #ifdef WOLFSSL_KEY_GEN
  38268. WC_RNG rng;
  38269. #endif
  38270. #ifdef USE_CERT_BUFFERS_1024
  38271. bytes = (size_t)sizeof_client_key_der_1024;
  38272. if (bytes < (size_t)sizeof_client_cert_der_1024)
  38273. bytes = (size_t)sizeof_client_cert_der_1024;
  38274. #elif defined(USE_CERT_BUFFERS_2048)
  38275. bytes = (size_t)sizeof_client_key_der_2048;
  38276. if (bytes < (size_t)sizeof_client_cert_der_2048)
  38277. bytes = (size_t)sizeof_client_cert_der_2048;
  38278. #elif defined(USE_CERT_BUFFERS_3072)
  38279. bytes = (size_t)sizeof_client_key_der_3072;
  38280. if (bytes < (size_t)sizeof_client_cert_der_3072)
  38281. bytes = (size_t)sizeof_client_cert_der_3072;
  38282. #elif defined(USE_CERT_BUFFERS_4096)
  38283. bytes = (size_t)sizeof_client_key_der_4096;
  38284. if (bytes < (size_t)sizeof_client_cert_der_4096)
  38285. bytes = (size_t)sizeof_client_cert_der_4096;
  38286. #else
  38287. bytes = FOURK_BUF;
  38288. #endif
  38289. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  38290. tmp = (byte*)XMALLOC(bytes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38291. if (tmp == NULL)
  38292. ERROR_OUT(-8000, exit_onlycb);
  38293. #endif
  38294. #ifdef USE_CERT_BUFFERS_1024
  38295. XMEMCPY(tmp, client_key_der_1024, (size_t)sizeof_client_key_der_1024);
  38296. #elif defined(USE_CERT_BUFFERS_2048)
  38297. XMEMCPY(tmp, client_key_der_2048, (size_t)sizeof_client_key_der_2048);
  38298. #elif defined(USE_CERT_BUFFERS_3072)
  38299. XMEMCPY(tmp, client_key_der_3072, (size_t)sizeof_client_key_der_3072);
  38300. #elif defined(USE_CERT_BUFFERS_4096)
  38301. XMEMCPY(tmp, client_key_der_4096, (size_t)sizeof_client_key_der_4096);
  38302. #elif !defined(NO_FILESYSTEM)
  38303. file = XFOPEN(clientKey, "rb");
  38304. if (!file) {
  38305. err_sys("can't open ./certs/client-key.der, "
  38306. "Please run from wolfSSL home dir", -40);
  38307. ERROR_OUT(-8001, exit_onlycb);
  38308. }
  38309. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  38310. XFCLOSE(file);
  38311. #endif
  38312. #ifdef WOLFSSL_KEY_GEN
  38313. /* wc_CryptoCb_MakeRsaKey cb test, no actual making key
  38314. * wc_MakeRsaKey() -> rsa cb ->
  38315. * myCryptoDevCb -> wc_MakeRsaKey(CBONLY_TEST_DEVID)
  38316. * wc_MakeRsaKey(CBONLY_TEST_DEVID) expects to return 0(success)
  38317. */
  38318. ctx->exampleVar = 99;
  38319. ret = wc_MakeRsaKey(key, keySz, WC_RSA_EXPONENT, rng);
  38320. if (ret != 0) {
  38321. ERROR_OUT(-8002, exit_onlycb);
  38322. }
  38323. /* wc_MakeRsaKey() -> rsa cb ->
  38324. * myCryptoDevCb -> wc_MakeRsaKey(INVALID_DEVID)
  38325. * wc_MakeRsaKey(CBONLY_TEST_DEVID) expects to return NO_VALID_DEVID(failure)
  38326. */
  38327. ctx->exampleVar = 1;
  38328. ret = wc_MakeRsaKey(key, keySz, WC_RSA_EXPONENT, rng);
  38329. if (ret != NO_VALID_DEVID) {
  38330. ERROR_OUT(-8003, exit_onlycb);
  38331. } else
  38332. /* reset return code */
  38333. ret = 0;
  38334. #endif
  38335. ret = wc_InitRsaKey_ex(key, HEAP_HINT, devId);
  38336. if (ret != 0) {
  38337. ERROR_OUT(-8004, exit_onlycb);
  38338. }
  38339. ret = wc_RsaPrivateKeyDecode(tmp, &idx, key, (word32)bytes);
  38340. if (ret != 0) {
  38341. ERROR_OUT(-8005, exit_onlycb);
  38342. }
  38343. sigSz = (word32)wc_RsaEncryptSize(key);
  38344. /* wc_CryptoCb_Rsa cb test, no actual rsa operation */
  38345. if (ret == 0) {
  38346. /* wc_SignatureGenerate() -> rsa cb ->
  38347. * myCryptoDevCb -> wc_RsaFunction(CBONLY_TEST_DEVID)
  38348. * wc_RsaFunction(CBONLY_TEST_DEVID) expects to return 0(success)
  38349. */
  38350. ctx->exampleVar = 99;
  38351. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA,
  38352. in, inLen, out, &sigSz, key, sizeof(*key), NULL);
  38353. if (ret != 0) {
  38354. ERROR_OUT(-8006, exit_onlycb);
  38355. }
  38356. }
  38357. if (ret == 0) {
  38358. /* wc_SignatureGenerate() -> rsa cb ->
  38359. * myCryptoDevCb -> wc_RsaFunction(INVALID_DEVID)
  38360. * wc_SignatureGenerate(INVALID_DEVID) expects to
  38361. * return NO_VALID_DEVID(failure)
  38362. */
  38363. ctx->exampleVar = 1;
  38364. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA,
  38365. in, inLen, out, &sigSz, key, sizeof(*key), NULL);
  38366. if (ret != NO_VALID_DEVID) {
  38367. ERROR_OUT(-8007, exit_onlycb);
  38368. } else
  38369. /* reset return code */
  38370. ret = 0;
  38371. }
  38372. exit_onlycb:
  38373. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  38374. if (key != NULL) {
  38375. wc_FreeRsaKey(key);
  38376. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38377. }
  38378. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38379. #else
  38380. wc_FreeRsaKey(key);
  38381. #endif
  38382. #endif
  38383. return ret;
  38384. }
  38385. #endif
  38386. #ifdef WOLF_CRYPTO_CB_ONLY_ECC
  38387. /* Testing rsa cb when CB_ONLY_ECC is enabled
  38388. * When CB_ONLY_ECC is enabled, software imple. is not available.
  38389. *
  38390. * ctx callback ctx
  38391. * returen 0 on success, otherwise return -8008 - -8018
  38392. */
  38393. static int ecc_onlycb_test(myCryptoDevCtx *ctx)
  38394. {
  38395. int ret = 0;
  38396. #if defined(HAVE_ECC)
  38397. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  38398. ecc_key* key = (ecc_key *)XMALLOC(sizeof *key,
  38399. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38400. ecc_key* pub = (ecc_key *)XMALLOC(sizeof *pub,
  38401. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38402. byte* out = (byte*)XMALLOC(sizeof(byte),
  38403. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38404. #ifdef OPENSSL_EXTRA
  38405. byte* check = (byte*)XMALLOC(sizeof(byte)*(256), HEAP_HINT,
  38406. DYNAMIC_TYPE_TMP_BUFFER);
  38407. #endif
  38408. #else
  38409. ecc_key key[1];
  38410. ecc_key pub[1];
  38411. byte out[256];
  38412. #ifdef OPENSSL_EXTRA
  38413. unsigned char check[256];
  38414. #endif
  38415. #endif
  38416. #ifdef OPENSSL_EXTRA
  38417. EVP_PKEY* privKey = NULL;
  38418. EVP_PKEY* pubKey = NULL;
  38419. ecc_key* pkey;
  38420. EVP_MD_CTX mdCtx;
  38421. const char testData[] = "Hi There";
  38422. size_t checkSz = -1;
  38423. const unsigned char* cp;
  38424. const unsigned char* p;
  38425. const unsigned char check_v[256] = {
  38426. 0x30,0x45,0x02,0x20,0x1b,0x5c,0x2a,0xf0,0x18,0x09,
  38427. 0x74,0x65,0xa1,0x04,0x76,0x3a,0xce,0xcc,0xe5,0x34,
  38428. 0x5e,0x89,0xed,0x40,0x1e,0x5a,0xb1,0x53,0xb4,0xff,
  38429. 0xc7,0x18,0xfe,0x0f,0xc7,0xa6,0x02,0x21,0x00,0xe5,
  38430. 0x70,0x21,0xfc,0xf9,0x63,0x36,0xfd,0x16,0x18,0x08,
  38431. 0x9a,0x63,0x61,0x0f,0xe7,0x7c,0xa3,0xc9,0x14,0xa3,
  38432. 0x30,0x87,0xf7,0xf5,0x70,0x19,0xaf,0x56,0x96,0x9b,
  38433. 0xd8,0x64,0xcd,0xd9,0xff,0x7b,0x2a,0x55,0x52,0xca,
  38434. 0x41,0xb2,0xa6,0xa4,0x8a,0x3b,0x02,0x20,0x8c,0xc5,
  38435. 0xf9,0xc1,0x7d,0x2a,0x65,0x6c,0xe6,0x5a,0xe3,0x76,
  38436. 0x9b,0xab,0x0b,0x9f,0xaf,0x62,0x5d,0xb2,0x60,0xd7,
  38437. 0xeb,0xb4,0x1b,0x73,0xdc,0x01,0x7d,0x7b,0xab,0xc1,
  38438. 0x0c,0x74,0x96,0x41,0xe6,0x3f,0xc5,0x86,0xe6,0x7d,
  38439. 0x2b,0x9d,0x54,0x6b,0xcd,0x31,0x35,0x1f,0xdb,0x49,
  38440. 0x1f,0x32,0x34,0xf8,0x57,0x12,0x86,0x5c,0x0e,0x80,
  38441. 0x55,0x8d,0xff,0xd8,0xbd,0xdf,0x32,0x26,0x62,0x42,
  38442. 0x09,0xda,0xf7,0x74,0xf2,0x3f,0xe6,0xf1,0x77,0x82,
  38443. 0xce,0xe4,0xbb,0x61,0xa6,0xc0,0x17,0x0c,0x6c,0x47,
  38444. 0x2a,0x40,0x1c,0x2b,0xe0,0x98,0x3b,0xbf,0xc6,0xf8,
  38445. 0x6d,0xfd,0xd0,0xfa,0xc1,0x02,0xfb,0x5f,0xfb,0xb0,
  38446. 0xcb,0xd9,0xa3,0x59,0x94,0xe9,0x0f,0x74,0xbb,0x3f,
  38447. 0x64,0xa3,0x83,0xc4,0x2b,0xf7,0xd2,0x97,0xbf,0x3b,
  38448. 0xcf,0xbb,0x60,0x81,0x33,0x94,0xfa,0x0d,0x35,0xd2,
  38449. 0x3d,0xb9,0x99,0xe3,0x12,0xf8,0xf4,0xa3,0x74,0xf4,
  38450. 0x94,0x1d,0x7a,0x66,0xf8,0xd1,0x1d,0xcf,0xb0,0x48,
  38451. 0xef,0x8c,0x94,0x6f,0xdd,0x62,
  38452. };
  38453. #endif
  38454. WC_RNG rng;
  38455. EncryptedInfo encInfo;
  38456. int keyFormat = 0;
  38457. word32 keyIdx = 0;
  38458. byte in[] = "Everyone gets Friday off. ecc p";
  38459. word32 inLen = (word32)XSTRLEN((char*)in);
  38460. word32 outLen;
  38461. int verify;
  38462. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  38463. if (key == NULL || pub == NULL) {
  38464. ERROR_OUT(-8008, exit_onlycb);
  38465. }
  38466. #endif
  38467. ret = wc_ecc_init_ex(key, HEAP_HINT, devId);
  38468. if (ret != 0) {
  38469. ERROR_OUT(-8009, exit_onlycb);
  38470. }
  38471. /* wc_CryptoCb_MakeEccKey cb test, , no actual testing */
  38472. ctx->exampleVar = 99;
  38473. ret = wc_ecc_make_key(&rng, ECC_KEYGEN_SIZE, key);
  38474. if (ret != 0) {
  38475. ERROR_OUT(-8010, exit_onlycb);
  38476. }
  38477. ctx->exampleVar = 1;
  38478. ret = wc_ecc_make_key(&rng, ECC_KEYGEN_SIZE, key);
  38479. if (ret != NO_VALID_DEVID) {
  38480. ERROR_OUT(-8011, exit_onlycb);
  38481. } else
  38482. /* reset return code */
  38483. ret = 0;
  38484. #ifdef USE_CERT_BUFFERS_256
  38485. if (ret == 0) {
  38486. /* load ECC private key and perform private transform */
  38487. ret = wc_EccPrivateKeyDecode(ecc_key_der_256, &keyIdx,
  38488. key, sizeof_ecc_key_der_256);
  38489. }
  38490. if (ret != 0) {
  38491. ERROR_OUT(-8012, exit_onlycb);
  38492. }
  38493. /* wc_CryptoCb_EccSign cb test, no actual testing */
  38494. ctx->exampleVar = 99;
  38495. if (ret == 0) {
  38496. ret = wc_ecc_sign_hash(in, inLen, out, &outLen, &rng, key);
  38497. }
  38498. if (ret != 0) {
  38499. ERROR_OUT(-8013, exit_onlycb);
  38500. }
  38501. ctx->exampleVar = 1;
  38502. if (ret == 0) {
  38503. ret = wc_ecc_sign_hash(in, inLen, out, &outLen, &rng, key);
  38504. }
  38505. if (ret != NO_VALID_DEVID) {
  38506. ERROR_OUT(-8014, exit_onlycb);
  38507. }
  38508. else
  38509. ret = 0;
  38510. /* wc_CryptoCb_EccVerify cb test, no actual testing */
  38511. ctx->exampleVar = 99;
  38512. if (ret == 0) {
  38513. ret = wc_ecc_verify_hash(in, inLen, out, outLen, &verify, key);
  38514. }
  38515. if (ret != 0) {
  38516. ERROR_OUT(-8015, exit_onlycb);
  38517. }
  38518. ctx->exampleVar = 1;
  38519. if (ret == 0) {
  38520. ret = wc_ecc_verify_hash(in, inLen, out, outLen, &verify, key);
  38521. }
  38522. if (ret != NO_VALID_DEVID) {
  38523. ERROR_OUT(-8016, exit_onlycb);
  38524. }
  38525. else
  38526. ret = 0;
  38527. /* wc_CryptoCb_Ecdh cb test, no actual testing */
  38528. /* make public key for shared secret */
  38529. wc_ecc_init_ex(pub, HEAP_HINT, devId);
  38530. ctx->exampleVar = 99;
  38531. if (ret == 0) {
  38532. ret = wc_ecc_shared_secret(key, pub, out, &outLen);
  38533. }
  38534. if (ret != 0) {
  38535. ERROR_OUT(-8017, exit_onlycb);
  38536. }
  38537. ctx->exampleVar = 1;
  38538. if (ret == 0) {
  38539. ret = wc_ecc_shared_secret(key, pub, out, &outLen);
  38540. }
  38541. if (ret != NO_VALID_DEVID) {
  38542. ERROR_OUT(-8018, exit_onlycb);
  38543. }
  38544. else
  38545. ret = 0;
  38546. #ifdef OPENSSL_EXTRA
  38547. (void)pkey;
  38548. cp = ecc_clikey_der_256;
  38549. privKey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &cp,
  38550. sizeof_ecc_clikey_der_256);
  38551. if (privKey == NULL) {
  38552. ERROR_OUT(-8019, exit_onlycb);
  38553. }
  38554. pkey = (ecc_key*)privKey->ecc->internal;
  38555. pkey->devId = devId;
  38556. p = ecc_clikeypub_der_256;
  38557. pubKey = d2i_PUBKEY(NULL, &p, sizeof_ecc_clikeypub_der_256);
  38558. if (pubKey == NULL) {
  38559. ERROR_OUT(-8020, exit_onlycb);
  38560. }
  38561. pkey = (ecc_key*)pubKey->ecc->internal;
  38562. pkey->devId = devId;
  38563. /* sign */
  38564. EVP_MD_CTX_init(&mdCtx);
  38565. ret = EVP_DigestSignInit(&mdCtx, NULL, EVP_sha256(), NULL, privKey);
  38566. if (ret != WOLFSSL_SUCCESS) {
  38567. ERROR_OUT(-8021, exit_onlycb);
  38568. }
  38569. ret = EVP_DigestSignUpdate(&mdCtx, testData,
  38570. (unsigned int)XSTRLEN(testData));
  38571. if (ret != WOLFSSL_SUCCESS) {
  38572. ERROR_OUT(-8022, exit_onlycb);
  38573. }
  38574. ret = EVP_DigestSignFinal(&mdCtx, NULL, &checkSz);
  38575. if (ret != WOLFSSL_SUCCESS) {
  38576. ERROR_OUT(-8023, exit_onlycb);
  38577. }
  38578. ctx->exampleVar = 99;
  38579. ret = EVP_DigestSignFinal(&mdCtx, check, &checkSz);
  38580. /* just called crypt callback as dummy
  38581. * EVP_DigestSignFinal returns 0 internally.
  38582. */
  38583. if (ret != 0) {
  38584. ERROR_OUT(-8024, exit_onlycb);
  38585. }
  38586. ctx->exampleVar = 1;
  38587. ret = EVP_DigestSignFinal(&mdCtx, check, &checkSz);
  38588. /* just called crypt callback as dummy
  38589. * EVP_DigestSignFinal returns 0 internally.
  38590. */
  38591. if (ret != 0) {
  38592. ERROR_OUT(-8025, exit_onlycb);
  38593. }
  38594. /* restore checkSz for verify */
  38595. checkSz = 71;
  38596. ret = EVP_MD_CTX_cleanup(&mdCtx);
  38597. if (ret != SSL_SUCCESS) {
  38598. ERROR_OUT(-8026, exit_onlycb);
  38599. }
  38600. /* verify */
  38601. EVP_MD_CTX_init(&mdCtx);
  38602. if (ret == SSL_SUCCESS) {
  38603. ret = EVP_DigestVerifyInit(&mdCtx, NULL, EVP_sha256(), NULL, pubKey);
  38604. }
  38605. if (ret != WOLFSSL_SUCCESS) {
  38606. ERROR_OUT(-8027, exit_onlycb);
  38607. }
  38608. if (ret == WOLFSSL_SUCCESS) {
  38609. ret = EVP_DigestVerifyUpdate(&mdCtx, testData,
  38610. (unsigned int)XSTRLEN(testData));
  38611. }
  38612. if (ret != WOLFSSL_SUCCESS) {
  38613. ERROR_OUT(-8028, exit_onlycb);
  38614. }
  38615. ctx->exampleVar = 99;
  38616. ret = EVP_DigestVerifyFinal(&mdCtx, check_v, checkSz);
  38617. /* just called crypt callback as dummy
  38618. * EVP_DigestSignFinal returns 0 internally.
  38619. */
  38620. if (ret != 0) {
  38621. ERROR_OUT(-8029, exit_onlycb);
  38622. }
  38623. ctx->exampleVar = 1;
  38624. ret = EVP_DigestVerifyFinal(&mdCtx, check_v, checkSz);
  38625. /* just called crypt callback as dummy
  38626. * EVP_DigestVerifyFinal returns -1 internally rather than NO_VALID_DEVID.
  38627. */
  38628. if (ret != -1) {
  38629. ERROR_OUT(-8030, exit_onlycb);
  38630. }
  38631. ret = EVP_MD_CTX_cleanup(&mdCtx);
  38632. if (ret != SSL_SUCCESS) {
  38633. ERROR_OUT(-8031, exit_onlycb);
  38634. } else
  38635. ret = 0;
  38636. #endif
  38637. #else
  38638. (void)verify;
  38639. (void)outLen;
  38640. (void)inLen;
  38641. (void)out;
  38642. (void)pub;
  38643. #ifdef OPENSSL_EXTRA
  38644. (void)privKey;
  38645. (void)pubKey;
  38646. (void)mdCtx;
  38647. (void)check;
  38648. (void)checkSz;
  38649. (void)p;
  38650. #endif
  38651. #endif
  38652. (void)keyFormat;
  38653. (void)encInfo;
  38654. exit_onlycb:
  38655. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  38656. if (key != NULL) {
  38657. wc_ecc_free(key);
  38658. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38659. }
  38660. if (pub != NULL) {
  38661. XFREE(pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38662. }
  38663. if (out != NULL) {
  38664. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38665. }
  38666. #ifdef OPENSSL_EXTRA
  38667. if (check) {
  38668. FREE(check, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38669. }
  38670. #endif
  38671. #else
  38672. wc_ecc_free(key);
  38673. #ifdef OPENSSL_EXTRA
  38674. if (privKey)
  38675. EVP_PKEY_free(privKey);
  38676. if (pubKey)
  38677. EVP_PKEY_free(pubKey);
  38678. #endif
  38679. #endif
  38680. #endif /* HAVE_ECC */
  38681. return ret;
  38682. }
  38683. #endif
  38684. /* Example crypto dev callback function that calls software version */
  38685. static int myCryptoDevCb(int devIdArg, wc_CryptoInfo* info, void* ctx)
  38686. {
  38687. int ret = NOT_COMPILED_IN; /* return this to bypass HW and use SW */
  38688. myCryptoDevCtx* myCtx = (myCryptoDevCtx*)ctx;
  38689. if (info == NULL)
  38690. return BAD_FUNC_ARG;
  38691. #ifdef DEBUG_WOLFSSL
  38692. printf("CryptoDevCb: Algo Type %d\n", info->algo_type);
  38693. #endif
  38694. if (info->algo_type == WC_ALGO_TYPE_RNG) {
  38695. #ifndef WC_NO_RNG
  38696. /* set devId to invalid, so software is used */
  38697. info->rng.rng->devId = INVALID_DEVID;
  38698. ret = wc_RNG_GenerateBlock(info->rng.rng,
  38699. info->rng.out, info->rng.sz);
  38700. /* reset devId */
  38701. info->rng.rng->devId = devIdArg;
  38702. #endif
  38703. }
  38704. else if (info->algo_type == WC_ALGO_TYPE_SEED) {
  38705. #ifndef WC_NO_RNG
  38706. ALIGN32 static byte seed[sizeof(word32)] = { 0x00, 0x00, 0x00, 0x01 };
  38707. word32* seedWord32 = (word32*)seed;
  38708. word32 len;
  38709. /* wc_GenerateSeed is a local symbol so we need to fake the entropy. */
  38710. while (info->seed.sz > 0) {
  38711. len = (word32)sizeof(seed);
  38712. if (info->seed.sz < len)
  38713. len = info->seed.sz;
  38714. XMEMCPY(info->seed.seed, seed, sizeof(seed));
  38715. info->seed.seed += len;
  38716. info->seed.sz -= len;
  38717. (*seedWord32)++;
  38718. }
  38719. ret = 0;
  38720. #endif
  38721. }
  38722. else if (info->algo_type == WC_ALGO_TYPE_PK) {
  38723. #ifdef DEBUG_WOLFSSL
  38724. printf("CryptoDevCb: Pk Type %d\n", info->pk.type);
  38725. #endif
  38726. #ifndef NO_RSA
  38727. if (info->pk.type == WC_PK_TYPE_RSA) {
  38728. /* set devId to invalid, so software is used */
  38729. info->pk.rsa.key->devId = INVALID_DEVID;
  38730. #if defined(WOLF_CRYPTO_CB_ONLY_RSA)
  38731. #ifdef DEBUG_WOLFSSL
  38732. printf("CryptoDevCb: exampleVar %d\n", myCtx->exampleVar);
  38733. #endif
  38734. if (myCtx->exampleVar == 99) {
  38735. info->pk.rsa.key->devId = devIdArg;
  38736. return 0;
  38737. }
  38738. #endif
  38739. switch (info->pk.rsa.type) {
  38740. case RSA_PUBLIC_ENCRYPT:
  38741. case RSA_PUBLIC_DECRYPT:
  38742. /* perform software based RSA public op */
  38743. ret = wc_RsaFunction(
  38744. info->pk.rsa.in, info->pk.rsa.inLen,
  38745. info->pk.rsa.out, info->pk.rsa.outLen,
  38746. info->pk.rsa.type, info->pk.rsa.key, info->pk.rsa.rng);
  38747. break;
  38748. case RSA_PRIVATE_ENCRYPT:
  38749. case RSA_PRIVATE_DECRYPT:
  38750. /* perform software based RSA private op */
  38751. ret = wc_RsaFunction(
  38752. info->pk.rsa.in, info->pk.rsa.inLen,
  38753. info->pk.rsa.out, info->pk.rsa.outLen,
  38754. info->pk.rsa.type, info->pk.rsa.key, info->pk.rsa.rng);
  38755. break;
  38756. }
  38757. /* reset devId */
  38758. info->pk.rsa.key->devId = devIdArg;
  38759. }
  38760. #ifdef WOLFSSL_KEY_GEN
  38761. else if (info->pk.type == WC_PK_TYPE_RSA_KEYGEN) {
  38762. info->pk.rsakg.key->devId = INVALID_DEVID;
  38763. #if defined(WOLF_CRYPTO_CB_ONLY_RSA)
  38764. #ifdef DEBUG_WOLFSSL
  38765. printf("CryptoDevCb: exampleVar %d\n", myCtx->exampleVar);
  38766. #endif
  38767. if (myCtx->exampleVar == 99) {
  38768. info->pk.rsakg.key->devId = devIdArg;
  38769. return 0;
  38770. }
  38771. #endif
  38772. #ifdef HAVE_FIPS
  38773. for (;;) {
  38774. #endif
  38775. ret = wc_MakeRsaKey(info->pk.rsakg.key, info->pk.rsakg.size,
  38776. info->pk.rsakg.e, info->pk.rsakg.rng);
  38777. #ifdef HAVE_FIPS
  38778. if (ret == PRIME_GEN_E)
  38779. continue;
  38780. break;
  38781. }
  38782. #endif
  38783. /* reset devId */
  38784. info->pk.rsakg.key->devId = devIdArg;
  38785. }
  38786. #endif
  38787. #endif /* !NO_RSA */
  38788. #ifdef HAVE_ECC
  38789. if (info->pk.type == WC_PK_TYPE_EC_KEYGEN) {
  38790. /* set devId to invalid, so software is used */
  38791. info->pk.eckg.key->devId = INVALID_DEVID;
  38792. #if defined(WOLF_CRYPTO_CB_ONLY_ECC)
  38793. #ifdef DEBUG_WOLFSSL
  38794. printf("CryptoDevCb: exampleVar %d\n", myCtx->exampleVar);
  38795. #endif
  38796. if (myCtx->exampleVar == 99) {
  38797. info->pk.eckg.key->devId = devIdArg;
  38798. return 0;
  38799. }
  38800. #endif
  38801. ret = wc_ecc_make_key_ex(info->pk.eckg.rng, info->pk.eckg.size,
  38802. info->pk.eckg.key, info->pk.eckg.curveId);
  38803. /* reset devId */
  38804. info->pk.eckg.key->devId = devIdArg;
  38805. }
  38806. else if (info->pk.type == WC_PK_TYPE_ECDSA_SIGN) {
  38807. /* set devId to invalid, so software is used */
  38808. info->pk.eccsign.key->devId = INVALID_DEVID;
  38809. #if defined(WOLF_CRYPTO_CB_ONLY_ECC)
  38810. #ifdef DEBUG_WOLFSSL
  38811. printf("CryptoDevCb: exampleVar %d\n", myCtx->exampleVar);
  38812. #endif
  38813. if (myCtx->exampleVar == 99) {
  38814. info->pk.eccsign.key->devId = devIdArg;
  38815. return 0;
  38816. }
  38817. #endif
  38818. ret = wc_ecc_sign_hash(
  38819. info->pk.eccsign.in, info->pk.eccsign.inlen,
  38820. info->pk.eccsign.out, info->pk.eccsign.outlen,
  38821. info->pk.eccsign.rng, info->pk.eccsign.key);
  38822. /* reset devId */
  38823. info->pk.eccsign.key->devId = devIdArg;
  38824. }
  38825. else if (info->pk.type == WC_PK_TYPE_ECDSA_VERIFY) {
  38826. /* set devId to invalid, so software is used */
  38827. info->pk.eccverify.key->devId = INVALID_DEVID;
  38828. #if defined(WOLF_CRYPTO_CB_ONLY_ECC)
  38829. #ifdef DEBUG_WOLFSSL
  38830. printf("CryptoDevCb: exampleVar %d\n", myCtx->exampleVar);
  38831. #endif
  38832. if (myCtx->exampleVar == 99) {
  38833. info->pk.eccverify.key->devId = devIdArg;
  38834. return 0;
  38835. }
  38836. #endif
  38837. ret = wc_ecc_verify_hash(
  38838. info->pk.eccverify.sig, info->pk.eccverify.siglen,
  38839. info->pk.eccverify.hash, info->pk.eccverify.hashlen,
  38840. info->pk.eccverify.res, info->pk.eccverify.key);
  38841. /* reset devId */
  38842. info->pk.eccverify.key->devId = devIdArg;
  38843. }
  38844. else if (info->pk.type == WC_PK_TYPE_ECDH) {
  38845. /* set devId to invalid, so software is used */
  38846. info->pk.ecdh.private_key->devId = INVALID_DEVID;
  38847. #if defined(WOLF_CRYPTO_CB_ONLY_ECC)
  38848. #ifdef DEBUG_WOLFSSL
  38849. printf("CryptoDevCb: exampleVar %d\n", myCtx->exampleVar);
  38850. #endif
  38851. if (myCtx->exampleVar == 99) {
  38852. info->pk.ecdh.private_key->devId = devIdArg;
  38853. return 0;
  38854. }
  38855. #endif
  38856. ret = wc_ecc_shared_secret(
  38857. info->pk.ecdh.private_key, info->pk.ecdh.public_key,
  38858. info->pk.ecdh.out, info->pk.ecdh.outlen);
  38859. /* reset devId */
  38860. info->pk.ecdh.private_key->devId = devIdArg;
  38861. }
  38862. #endif /* HAVE_ECC */
  38863. #ifdef HAVE_CURVE25519
  38864. if (info->pk.type == WC_PK_TYPE_CURVE25519_KEYGEN) {
  38865. /* set devId to invalid, so software is used */
  38866. info->pk.curve25519kg.key->devId = INVALID_DEVID;
  38867. ret = wc_curve25519_make_key(info->pk.curve25519kg.rng,
  38868. info->pk.curve25519kg.size, info->pk.curve25519kg.key);
  38869. /* reset devId */
  38870. info->pk.curve25519kg.key->devId = devIdArg;
  38871. }
  38872. else if (info->pk.type == WC_PK_TYPE_CURVE25519) {
  38873. /* set devId to invalid, so software is used */
  38874. info->pk.curve25519.private_key->devId = INVALID_DEVID;
  38875. ret = wc_curve25519_shared_secret_ex(
  38876. info->pk.curve25519.private_key, info->pk.curve25519.public_key,
  38877. info->pk.curve25519.out, info->pk.curve25519.outlen,
  38878. info->pk.curve25519.endian);
  38879. /* reset devId */
  38880. info->pk.curve25519.private_key->devId = devIdArg;
  38881. }
  38882. #endif /* HAVE_CURVE25519 */
  38883. #ifdef HAVE_ED25519
  38884. if (info->pk.type == WC_PK_TYPE_ED25519_KEYGEN) {
  38885. /* set devId to invalid, so software is used */
  38886. info->pk.ed25519kg.key->devId = INVALID_DEVID;
  38887. ret = wc_ed25519_make_key(info->pk.ed25519kg.rng,
  38888. info->pk.ed25519kg.size, info->pk.ed25519kg.key);
  38889. /* reset devId */
  38890. info->pk.ed25519kg.key->devId = devIdArg;
  38891. }
  38892. #ifdef HAVE_ED25519_SIGN
  38893. else if (info->pk.type == WC_PK_TYPE_ED25519_SIGN) {
  38894. /* set devId to invalid, so software is used */
  38895. info->pk.ed25519sign.key->devId = INVALID_DEVID;
  38896. ret = wc_ed25519_sign_msg_ex(
  38897. info->pk.ed25519sign.in, info->pk.ed25519sign.inLen,
  38898. info->pk.ed25519sign.out, info->pk.ed25519sign.outLen,
  38899. info->pk.ed25519sign.key, info->pk.ed25519sign.type,
  38900. info->pk.ed25519sign.context, info->pk.ed25519sign.contextLen);
  38901. /* reset devId */
  38902. info->pk.ed25519sign.key->devId = devIdArg;
  38903. }
  38904. #endif
  38905. #ifdef HAVE_ED25519_VERIFY
  38906. else if (info->pk.type == WC_PK_TYPE_ED25519_VERIFY) {
  38907. /* set devId to invalid, so software is used */
  38908. info->pk.ed25519verify.key->devId = INVALID_DEVID;
  38909. ret = wc_ed25519_verify_msg_ex(
  38910. info->pk.ed25519verify.sig, info->pk.ed25519verify.sigLen,
  38911. info->pk.ed25519verify.msg, info->pk.ed25519verify.msgLen,
  38912. info->pk.ed25519verify.res, info->pk.ed25519verify.key,
  38913. info->pk.ed25519verify.type, info->pk.ed25519verify.context,
  38914. info->pk.ed25519verify.contextLen);
  38915. /* reset devId */
  38916. info->pk.ed25519verify.key->devId = devIdArg;
  38917. }
  38918. #endif
  38919. #endif /* HAVE_ED25519 */
  38920. }
  38921. else if (info->algo_type == WC_ALGO_TYPE_CIPHER) {
  38922. #if !defined(NO_AES) || !defined(NO_DES3)
  38923. #ifdef HAVE_AESGCM
  38924. if (info->cipher.type == WC_CIPHER_AES_GCM) {
  38925. if (info->cipher.enc) {
  38926. /* set devId to invalid, so software is used */
  38927. info->cipher.aesgcm_enc.aes->devId = INVALID_DEVID;
  38928. ret = wc_AesGcmEncrypt(
  38929. info->cipher.aesgcm_enc.aes,
  38930. info->cipher.aesgcm_enc.out,
  38931. info->cipher.aesgcm_enc.in,
  38932. info->cipher.aesgcm_enc.sz,
  38933. info->cipher.aesgcm_enc.iv,
  38934. info->cipher.aesgcm_enc.ivSz,
  38935. info->cipher.aesgcm_enc.authTag,
  38936. info->cipher.aesgcm_enc.authTagSz,
  38937. info->cipher.aesgcm_enc.authIn,
  38938. info->cipher.aesgcm_enc.authInSz);
  38939. /* reset devId */
  38940. info->cipher.aesgcm_enc.aes->devId = devIdArg;
  38941. }
  38942. else {
  38943. /* set devId to invalid, so software is used */
  38944. info->cipher.aesgcm_dec.aes->devId = INVALID_DEVID;
  38945. ret = wc_AesGcmDecrypt(
  38946. info->cipher.aesgcm_dec.aes,
  38947. info->cipher.aesgcm_dec.out,
  38948. info->cipher.aesgcm_dec.in,
  38949. info->cipher.aesgcm_dec.sz,
  38950. info->cipher.aesgcm_dec.iv,
  38951. info->cipher.aesgcm_dec.ivSz,
  38952. info->cipher.aesgcm_dec.authTag,
  38953. info->cipher.aesgcm_dec.authTagSz,
  38954. info->cipher.aesgcm_dec.authIn,
  38955. info->cipher.aesgcm_dec.authInSz);
  38956. /* reset devId */
  38957. info->cipher.aesgcm_dec.aes->devId = devIdArg;
  38958. }
  38959. }
  38960. #endif /* HAVE_AESGCM */
  38961. #ifdef HAVE_AES_CBC
  38962. if (info->cipher.type == WC_CIPHER_AES_CBC) {
  38963. if (info->cipher.enc) {
  38964. /* set devId to invalid, so software is used */
  38965. info->cipher.aescbc.aes->devId = INVALID_DEVID;
  38966. ret = wc_AesCbcEncrypt(
  38967. info->cipher.aescbc.aes,
  38968. info->cipher.aescbc.out,
  38969. info->cipher.aescbc.in,
  38970. info->cipher.aescbc.sz);
  38971. /* reset devId */
  38972. info->cipher.aescbc.aes->devId = devIdArg;
  38973. }
  38974. else {
  38975. /* set devId to invalid, so software is used */
  38976. info->cipher.aescbc.aes->devId = INVALID_DEVID;
  38977. ret = wc_AesCbcDecrypt(
  38978. info->cipher.aescbc.aes,
  38979. info->cipher.aescbc.out,
  38980. info->cipher.aescbc.in,
  38981. info->cipher.aescbc.sz);
  38982. /* reset devId */
  38983. info->cipher.aescbc.aes->devId = devIdArg;
  38984. }
  38985. }
  38986. #endif /* HAVE_AES_CBC */
  38987. #if defined(HAVE_AES_ECB) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  38988. if (info->cipher.type == WC_CIPHER_AES_ECB) {
  38989. if (info->cipher.enc) {
  38990. /* set devId to invalid, so software is used */
  38991. info->cipher.aesecb.aes->devId = INVALID_DEVID;
  38992. ret = wc_AesEcbEncrypt(
  38993. info->cipher.aesecb.aes,
  38994. info->cipher.aesecb.out,
  38995. info->cipher.aesecb.in,
  38996. info->cipher.aesecb.sz);
  38997. /* reset devId */
  38998. info->cipher.aesecb.aes->devId = devIdArg;
  38999. }
  39000. else {
  39001. /* set devId to invalid, so software is used */
  39002. info->cipher.aesecb.aes->devId = INVALID_DEVID;
  39003. ret = wc_AesEcbDecrypt(
  39004. info->cipher.aesecb.aes,
  39005. info->cipher.aesecb.out,
  39006. info->cipher.aesecb.in,
  39007. info->cipher.aesecb.sz);
  39008. /* reset devId */
  39009. info->cipher.aesecb.aes->devId = devIdArg;
  39010. }
  39011. }
  39012. #endif /* HAVE_AES_ECB */
  39013. #if defined(WOLFSSL_AES_COUNTER) && !defined(HAVE_FIPS) && \
  39014. !defined(HAVE_SELFTEST)
  39015. if (info->cipher.type == WC_CIPHER_AES_CTR) {
  39016. /* set devId to invalid, so software is used */
  39017. info->cipher.aesctr.aes->devId = INVALID_DEVID;
  39018. ret = wc_AesCtrEncrypt(
  39019. info->cipher.aesctr.aes,
  39020. info->cipher.aesctr.out,
  39021. info->cipher.aesctr.in,
  39022. info->cipher.aesctr.sz);
  39023. /* reset devId */
  39024. info->cipher.aesctr.aes->devId = devIdArg;
  39025. }
  39026. #endif /* WOLFSSL_AES_COUNTER */
  39027. #if defined(HAVE_AESCCM) && defined(WOLFSSL_AES_128)
  39028. if (info->cipher.type == WC_CIPHER_AES_CCM) {
  39029. if (info->cipher.enc) {
  39030. /* set devId to invalid, so software is used */
  39031. info->cipher.aesccm_enc.aes->devId = INVALID_DEVID;
  39032. ret = wc_AesCcmEncrypt(
  39033. info->cipher.aesccm_enc.aes,
  39034. info->cipher.aesccm_enc.out,
  39035. info->cipher.aesccm_enc.in,
  39036. info->cipher.aesccm_enc.sz,
  39037. info->cipher.aesccm_enc.nonce,
  39038. info->cipher.aesccm_enc.nonceSz,
  39039. info->cipher.aesccm_enc.authTag,
  39040. info->cipher.aesccm_enc.authTagSz,
  39041. info->cipher.aesccm_enc.authIn,
  39042. info->cipher.aesccm_enc.authInSz);
  39043. /* reset devId */
  39044. info->cipher.aesccm_enc.aes->devId = devIdArg;
  39045. }
  39046. else {
  39047. /* set devId to invalid, so software is used */
  39048. info->cipher.aesccm_dec.aes->devId = INVALID_DEVID;
  39049. ret = wc_AesCcmDecrypt(
  39050. info->cipher.aesccm_dec.aes,
  39051. info->cipher.aesccm_dec.out,
  39052. info->cipher.aesccm_dec.in,
  39053. info->cipher.aesccm_dec.sz,
  39054. info->cipher.aesccm_dec.nonce,
  39055. info->cipher.aesccm_dec.nonceSz,
  39056. info->cipher.aesccm_dec.authTag,
  39057. info->cipher.aesccm_dec.authTagSz,
  39058. info->cipher.aesccm_dec.authIn,
  39059. info->cipher.aesccm_dec.authInSz);
  39060. /* reset devId */
  39061. info->cipher.aesccm_dec.aes->devId = devIdArg;
  39062. }
  39063. }
  39064. #endif
  39065. #ifndef NO_DES3
  39066. if (info->cipher.type == WC_CIPHER_DES3) {
  39067. if (info->cipher.enc) {
  39068. /* set devId to invalid, so software is used */
  39069. info->cipher.des3.des->devId = INVALID_DEVID;
  39070. ret = wc_Des3_CbcEncrypt(
  39071. info->cipher.des3.des,
  39072. info->cipher.des3.out,
  39073. info->cipher.des3.in,
  39074. info->cipher.des3.sz);
  39075. /* reset devId */
  39076. info->cipher.des3.des->devId = devIdArg;
  39077. }
  39078. else {
  39079. /* set devId to invalid, so software is used */
  39080. info->cipher.des3.des->devId = INVALID_DEVID;
  39081. ret = wc_Des3_CbcDecrypt(
  39082. info->cipher.des3.des,
  39083. info->cipher.des3.out,
  39084. info->cipher.des3.in,
  39085. info->cipher.des3.sz);
  39086. /* reset devId */
  39087. info->cipher.des3.des->devId = devIdArg;
  39088. }
  39089. }
  39090. #endif /* !NO_DES3 */
  39091. #endif /* !NO_AES || !NO_DES3 */
  39092. }
  39093. #if !defined(NO_SHA) || !defined(NO_SHA256) || \
  39094. defined(WOLFSSL_SHA384) || defined(WOLFSSL_SHA512)
  39095. else if (info->algo_type == WC_ALGO_TYPE_HASH) {
  39096. #if !defined(NO_SHA)
  39097. if (info->hash.type == WC_HASH_TYPE_SHA) {
  39098. if (info->hash.sha1 == NULL)
  39099. return NOT_COMPILED_IN;
  39100. /* set devId to invalid, so software is used */
  39101. info->hash.sha1->devId = INVALID_DEVID;
  39102. if (info->hash.in != NULL) {
  39103. ret = wc_ShaUpdate(
  39104. info->hash.sha1,
  39105. info->hash.in,
  39106. info->hash.inSz);
  39107. }
  39108. if (info->hash.digest != NULL) {
  39109. ret = wc_ShaFinal(
  39110. info->hash.sha1,
  39111. info->hash.digest);
  39112. }
  39113. /* reset devId */
  39114. info->hash.sha1->devId = devIdArg;
  39115. }
  39116. else
  39117. #endif
  39118. #if !defined(NO_SHA256)
  39119. if (info->hash.type == WC_HASH_TYPE_SHA256) {
  39120. if (info->hash.sha256 == NULL)
  39121. return NOT_COMPILED_IN;
  39122. /* set devId to invalid, so software is used */
  39123. info->hash.sha256->devId = INVALID_DEVID;
  39124. if (info->hash.in != NULL) {
  39125. ret = wc_Sha256Update(
  39126. info->hash.sha256,
  39127. info->hash.in,
  39128. info->hash.inSz);
  39129. }
  39130. if (info->hash.digest != NULL) {
  39131. ret = wc_Sha256Final(
  39132. info->hash.sha256,
  39133. info->hash.digest);
  39134. }
  39135. /* reset devId */
  39136. info->hash.sha256->devId = devIdArg;
  39137. }
  39138. else
  39139. #endif
  39140. #ifdef WOLFSSL_SHA384
  39141. if (info->hash.type == WC_HASH_TYPE_SHA384) {
  39142. if (info->hash.sha384 == NULL)
  39143. return NOT_COMPILED_IN;
  39144. #ifndef NO_SHA2_CRYPTO_CB
  39145. /* set devId to invalid, so software is used */
  39146. info->hash.sha384->devId = INVALID_DEVID;
  39147. #endif
  39148. if (info->hash.in != NULL) {
  39149. ret = wc_Sha384Update(
  39150. info->hash.sha384,
  39151. info->hash.in,
  39152. info->hash.inSz);
  39153. }
  39154. if (info->hash.digest != NULL) {
  39155. ret = wc_Sha384Final(
  39156. info->hash.sha384,
  39157. info->hash.digest);
  39158. }
  39159. #ifndef NO_SHA2_CRYPTO_CB
  39160. /* reset devId */
  39161. info->hash.sha384->devId = devIdArg;
  39162. #endif
  39163. }
  39164. else
  39165. #endif
  39166. #ifdef WOLFSSL_SHA512
  39167. if (info->hash.type == WC_HASH_TYPE_SHA512) {
  39168. if (info->hash.sha512 == NULL)
  39169. return NOT_COMPILED_IN;
  39170. #ifndef NO_SHA2_CRYPTO_CB
  39171. /* set devId to invalid, so software is used */
  39172. info->hash.sha512->devId = INVALID_DEVID;
  39173. #endif
  39174. if (info->hash.in != NULL) {
  39175. ret = wc_Sha512Update(
  39176. info->hash.sha512,
  39177. info->hash.in,
  39178. info->hash.inSz);
  39179. }
  39180. if (info->hash.digest != NULL) {
  39181. ret = wc_Sha512Final(
  39182. info->hash.sha512,
  39183. info->hash.digest);
  39184. }
  39185. #ifndef NO_SHA2_CRYPTO_CB
  39186. /* reset devId */
  39187. info->hash.sha512->devId = devIdArg;
  39188. #endif
  39189. }
  39190. else
  39191. #endif
  39192. {
  39193. }
  39194. }
  39195. #endif /* !NO_SHA || !NO_SHA256 */
  39196. #ifndef NO_HMAC
  39197. else if (info->algo_type == WC_ALGO_TYPE_HMAC) {
  39198. if (info->hmac.hmac == NULL)
  39199. return NOT_COMPILED_IN;
  39200. /* set devId to invalid, so software is used */
  39201. info->hmac.hmac->devId = INVALID_DEVID;
  39202. if (info->hash.in != NULL) {
  39203. ret = wc_HmacUpdate(
  39204. info->hmac.hmac,
  39205. info->hmac.in,
  39206. info->hmac.inSz);
  39207. }
  39208. else if (info->hash.digest != NULL) {
  39209. ret = wc_HmacFinal(
  39210. info->hmac.hmac,
  39211. info->hmac.digest);
  39212. }
  39213. /* reset devId */
  39214. info->hmac.hmac->devId = devIdArg;
  39215. }
  39216. #endif
  39217. (void)devIdArg;
  39218. (void)myCtx;
  39219. return ret;
  39220. }
  39221. WOLFSSL_TEST_SUBROUTINE int cryptocb_test(void)
  39222. {
  39223. int ret = 0;
  39224. myCryptoDevCtx myCtx;
  39225. /* example data for callback */
  39226. myCtx.exampleVar = 1;
  39227. /* set devId to something other than INVALID_DEVID */
  39228. devId = 1;
  39229. ret = wc_CryptoCb_RegisterDevice(devId, myCryptoDevCb, &myCtx);
  39230. #ifndef WC_NO_RNG
  39231. if (ret == 0)
  39232. ret = random_test();
  39233. #endif /* WC_NO_RNG */
  39234. #if !defined(NO_RSA)
  39235. PRIVATE_KEY_UNLOCK();
  39236. if (ret == 0)
  39237. ret = rsa_test();
  39238. PRIVATE_KEY_LOCK();
  39239. #endif
  39240. #if defined(WOLF_CRYPTO_CB_ONLY_RSA)
  39241. PRIVATE_KEY_UNLOCK();
  39242. if (ret == 0)
  39243. ret = rsa_onlycb_test(&myCtx);
  39244. PRIVATE_KEY_LOCK();
  39245. #endif
  39246. #if defined(HAVE_ECC)
  39247. PRIVATE_KEY_UNLOCK();
  39248. if (ret == 0)
  39249. ret = ecc_test();
  39250. PRIVATE_KEY_LOCK();
  39251. #endif
  39252. #if defined(WOLF_CRYPTO_CB_ONLY_ECC)
  39253. PRIVATE_KEY_UNLOCK();
  39254. if (ret == 0)
  39255. ret = ecc_onlycb_test(&myCtx);
  39256. PRIVATE_KEY_LOCK();
  39257. #endif
  39258. #ifdef HAVE_ED25519
  39259. if (ret == 0)
  39260. ret = ed25519_test();
  39261. #endif
  39262. #ifdef HAVE_CURVE25519
  39263. if (ret == 0)
  39264. ret = curve25519_test();
  39265. #endif
  39266. #ifndef NO_AES
  39267. #ifdef HAVE_AESGCM
  39268. if (ret == 0)
  39269. ret = aesgcm_test();
  39270. #endif
  39271. #ifdef HAVE_AES_CBC
  39272. if (ret == 0)
  39273. ret = aes_test();
  39274. #endif
  39275. #if defined(HAVE_AESCCM) && defined(WOLFSSL_AES_128)
  39276. if (ret == 0)
  39277. ret = aesccm_test();
  39278. #endif
  39279. #endif /* !NO_AES */
  39280. #ifndef NO_DES3
  39281. if (ret == 0)
  39282. ret = des3_test();
  39283. #endif /* !NO_DES3 */
  39284. #ifndef NO_SHA
  39285. if (ret == 0)
  39286. ret = sha_test();
  39287. #endif
  39288. #ifndef NO_SHA256
  39289. if (ret == 0)
  39290. ret = sha256_test();
  39291. #endif
  39292. #ifdef WOLFSSL_SHA384
  39293. if (ret == 0)
  39294. ret = sha384_test();
  39295. #endif
  39296. #ifdef WOLFSSL_SHA512
  39297. if (ret == 0)
  39298. ret = sha512_test();
  39299. #endif
  39300. #ifndef NO_HMAC
  39301. #ifndef NO_SHA
  39302. if (ret == 0)
  39303. ret = hmac_sha_test();
  39304. #endif
  39305. #ifndef NO_SHA256
  39306. if (ret == 0)
  39307. ret = hmac_sha256_test();
  39308. #endif
  39309. #endif
  39310. #ifndef NO_PWDBASED
  39311. #if defined(HAVE_PBKDF2) && !defined(NO_SHA256)
  39312. if (ret == 0)
  39313. ret = pbkdf2_test();
  39314. #endif
  39315. #endif
  39316. #if defined(WOLFSSL_CMAC) && !defined(NO_AES)
  39317. if (ret == 0)
  39318. ret = cmac_test();
  39319. #endif
  39320. /* reset devId */
  39321. devId = INVALID_DEVID;
  39322. return ret;
  39323. }
  39324. #endif /* WOLF_CRYPTO_CB */
  39325. #ifdef WOLFSSL_CERT_PIV
  39326. WOLFSSL_TEST_SUBROUTINE int certpiv_test(void)
  39327. {
  39328. int ret;
  39329. wc_CertPIV piv;
  39330. /* Template for Identiv PIV cert, nonce and signature */
  39331. WOLFSSL_SMALL_STACK_STATIC const byte pivCertIdentiv[] = {
  39332. 0x0A, 0x0B,
  39333. 0x53, 0x09, /* NIST PIV Cert */
  39334. 0x70, 0x02, /* Certificate */
  39335. 0x30, 0x00,
  39336. 0x71, 0x01, 0x05, /* Cert Info */
  39337. 0xFE, 0x00, /* Error Detection */
  39338. 0x0B, 0x01, 0x00, /* Nonce */
  39339. 0x0C, 0x01, 0x00, /* Signed Nonce */
  39340. };
  39341. /* PIV certificate data including certificate, info and error dectection. */
  39342. WOLFSSL_SMALL_STACK_STATIC const byte pivCert[] = {
  39343. 0x53, 0x09, /* NIST PIV Cert */
  39344. 0x70, 0x02, /* Certificate */
  39345. 0x30, 0x00,
  39346. 0x71, 0x01, 0x04, /* Cert Info */
  39347. 0xFE, 0x00, /* Error Detection */
  39348. };
  39349. XMEMSET(&piv, 0, sizeof(piv));
  39350. /* Test with Identiv 0x0A, 0x0B and 0x0C markers */
  39351. ret = wc_ParseCertPIV(&piv, pivCertIdentiv, sizeof(pivCertIdentiv));
  39352. if (ret != 0) {
  39353. return -14000;
  39354. }
  39355. if (!piv.isIdentiv) {
  39356. return -14001;
  39357. }
  39358. if ((piv.cert == NULL) || (piv.certSz != 2)) {
  39359. return -14002;
  39360. }
  39361. if ((piv.certErrDet == NULL) || (piv.certErrDetSz != 0)) {
  39362. return -14003;
  39363. }
  39364. if ((piv.compression != ASN_PIV_CERT_INFO_GZIP)) {
  39365. return -14004;
  39366. }
  39367. if (!piv.isX509) {
  39368. return -14005;
  39369. }
  39370. if ((piv.nonce == NULL) || (piv.nonceSz != 1)) {
  39371. return -14006;
  39372. }
  39373. if ((piv.signedNonce == NULL) || (piv.signedNonceSz != 1)) {
  39374. return -14007;
  39375. }
  39376. XMEMSET(&piv, 0, sizeof(piv));
  39377. /* Test with NIST PIV format */
  39378. ret = wc_ParseCertPIV(&piv, pivCert, sizeof(pivCert));
  39379. if (ret != 0) {
  39380. return -14010;
  39381. }
  39382. if (piv.isIdentiv) {
  39383. return -14011;
  39384. }
  39385. if ((piv.cert == NULL) || (piv.certSz != 2)) {
  39386. return -14012;
  39387. }
  39388. if ((piv.certErrDet == NULL) || (piv.certErrDetSz != 0)) {
  39389. return -14013;
  39390. }
  39391. if ((piv.compression != 0)) {
  39392. return -14014;
  39393. }
  39394. if (!piv.isX509) {
  39395. return -14015;
  39396. }
  39397. return ret;
  39398. }
  39399. #endif /* WOLFSSL_CERT_PIV */
  39400. #if !defined(NO_ASN) && !defined(NO_ASN_TIME)
  39401. static time_t time_cb(time_t* t)
  39402. {
  39403. if (t != NULL) {
  39404. *t = 99;
  39405. }
  39406. return 99;
  39407. }
  39408. WOLFSSL_TEST_SUBROUTINE int time_test(void)
  39409. {
  39410. time_t t;
  39411. if (wc_SetTimeCb(time_cb) != 0)
  39412. return -15000;
  39413. t = wc_Time(NULL);
  39414. if (t != 99)
  39415. return -15001;
  39416. if (wc_GetTime(&t, sizeof(time_t)) != 0)
  39417. return -15002;
  39418. if (t != 99)
  39419. return -15003;
  39420. if (wc_SetTimeCb(NULL) != 0)
  39421. return -15004;
  39422. return 0;
  39423. }
  39424. #endif
  39425. #ifdef WOLFSSL_AES_SIV
  39426. typedef struct {
  39427. const byte key[33];
  39428. word32 keySz;
  39429. const byte nonce[49];
  39430. word32 nonceSz;
  39431. const byte assoc[81];
  39432. word32 assocSz;
  39433. const byte plaintext[83];
  39434. word32 plaintextSz;
  39435. const byte siv[AES_BLOCK_SIZE+1];
  39436. const byte ciphertext[82];
  39437. word32 ciphertextSz;
  39438. } AesSivTestVector;
  39439. #define AES_SIV_TEST_VECTORS 7
  39440. WOLFSSL_TEST_SUBROUTINE int aes_siv_test(void)
  39441. {
  39442. /* These test vectors come from chrony 4.1's SIV unit tests. */
  39443. WOLFSSL_SMALL_STACK_STATIC const AesSivTestVector testVectors[AES_SIV_TEST_VECTORS] = {
  39444. { "\x01\x23\x45\x67\x89\xab\xcd\xef\xf0\x12\x34\x56\x78\x9a\xbc\xde"
  39445. "\xef\x01\x23\x45\x67\x89\xab\xcd\xde\xf0\x12\x34\x56\x78\x9a\xbc", 32,
  39446. "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f", 16,
  39447. "", 0,
  39448. "", 0,
  39449. "\x22\x3e\xb5\x94\xe0\xe0\x25\x4b\x00\x25\x8e\x21\x9a\x1c\xa4\x21",
  39450. "", 0
  39451. },
  39452. { "\x01\x23\x45\x67\x89\xab\xcd\xef\xf0\x12\x34\x56\x78\x9a\xbc\xde"
  39453. "\xef\x01\x23\x45\x67\x89\xab\xcd\xde\xf0\x12\x34\x56\x78\x9a\xbc", 32,
  39454. "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f", 16,
  39455. "\x4c\x9d\x4f\xca\xed\x8a\xe2\xba\xad\x3f\x3e\xa6\xe9\x3c\x8c\x8b", 16,
  39456. "", 0,
  39457. "\xd7\x20\x19\x89\xc6\xdb\xc6\xd6\x61\xfc\x62\xbc\x86\x5e\xee\xef",
  39458. "", 0
  39459. },
  39460. { "\x01\x23\x45\x67\x89\xab\xcd\xef\xf0\x12\x34\x56\x78\x9a\xbc\xde"
  39461. "\xef\x01\x23\x45\x67\x89\xab\xcd\xde\xf0\x12\x34\x56\x78\x9a\xbc", 32,
  39462. "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f", 16,
  39463. "", 0,
  39464. "\x4c\x9d\x4f\xca\xed\x8a\xe2\xba\xad\x3f\x3e\xa6\xe9\x3c\x8c\x8b", 16,
  39465. "\xb6\xc1\x60\xe9\xc2\xfd\x2a\xe8\xde\xc5\x36\x8b\x2a\x33\xed\xe1",
  39466. "\x14\xff\xb3\x97\x34\x5c\xcb\xe4\x4a\xa4\xde\xac\xd9\x36\x90\x46", 16
  39467. },
  39468. { "\x01\x23\x45\x67\x89\xab\xcd\xef\xf0\x12\x34\x56\x78\x9a\xbc\xde"
  39469. "\xef\x01\x23\x45\x67\x89\xab\xcd\xde\xf0\x12\x34\x56\x78\x9a\xbc", 32,
  39470. "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e", 15,
  39471. "\x4c\x9d\x4f\xca\xed\x8a\xe2\xba\xad\x3f\x3e\xa6\xe9\x3c\x8c", 15,
  39472. "\xba\x99\x79\x31\x23\x7e\x3c\x53\x58\x7e\xd4\x93\x02\xab\xe4", 15,
  39473. "\x03\x8c\x41\x51\xba\x7a\x8f\x77\x6e\x56\x31\x99\x42\x0b\xc7\x03",
  39474. "\xe7\x6c\x67\xc9\xda\xb7\x0d\x5b\x44\x06\x26\x5a\xd0\xd2\x3b", 15
  39475. },
  39476. { "\x01\x23\x45\x67\x89\xab\xcd\xef\xf0\x12\x34\x56\x78\x9a\xbc\xde"
  39477. "\xef\x01\x23\x45\x67\x89\xab\xcd\xde\xf0\x12\x34\x56\x78\x9a\xbc", 32,
  39478. "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f", 16,
  39479. "\x4c\x9d\x4f\xca\xed\x8a\xe2\xba\xad\x3f\x3e\xa6\xe9\x3c\x8c\x8b", 16,
  39480. "\xba\x99\x79\x31\x23\x7e\x3c\x53\x58\x7e\xd4\x93\x02\xab\xe4\xa7", 16,
  39481. "\x5c\x05\x23\x65\xf4\x57\x0a\xa0\xfb\x38\x3e\xce\x9b\x75\x85\xeb",
  39482. "\x68\x85\x19\x36\x0c\x7c\x48\x11\x40\xcb\x9b\x57\x9a\x0e\x65\x32", 16
  39483. },
  39484. { "\x01\x23\x45\x67\x89\xab\xcd\xef\xf0\x12\x34\x56\x78\x9a\xbc\xde"
  39485. "\xef\x01\x23\x45\x67\x89\xab\xcd\xde\xf0\x12\x34\x56\x78\x9a\xbc", 32,
  39486. "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f"
  39487. "\xd5", 17,
  39488. "\x4c\x9d\x4f\xca\xed\x8a\xe2\xba\xad\x3f\x3e\xa6\xe9\x3c\x8c\x8b"
  39489. "\xa0", 17,
  39490. "\xba\x99\x79\x31\x23\x7e\x3c\x53\x58\x7e\xd4\x93\x02\xab\xe4\xa7"
  39491. "\x08", 17,
  39492. "\xaf\x58\x4b\xe7\x82\x1e\x96\x19\x29\x91\x25\xe0\xdd\x80\x3b\x49",
  39493. "\xa5\x11\xcd\xb6\x08\xf3\x76\xa0\xb6\xfa\x15\x82\xf3\x95\xe1\xeb"
  39494. "\xbd", 17
  39495. },
  39496. { "\x01\x23\x45\x67\x89\xab\xcd\xef\xf0\x12\x34\x56\x78\x9a\xbc\xde"
  39497. "\xef\x01\x23\x45\x67\x89\xab\xcd\xde\xf0\x12\x34\x56\x78\x9a\xbc", 32,
  39498. "\xb0\x5a\x1b\xc7\x56\xe7\xb6\x2c\xb4\x85\xe5\x56\xa5\x28\xc0\x6c"
  39499. "\x2f\x3b\x0b\x9d\x1a\x0c\xdf\x69\x47\xe0\xcc\xc0\x87\xaa\x5c\x09"
  39500. "\x98\x48\x8d\x6a\x8e\x1e\x05\xd7\x8b\x68\x74\x83\xb5\x1d\xf1\x2c", 48,
  39501. "\xe5\x8b\xd2\x6a\x30\xc5\xc5\x61\xcc\xbd\x7c\x27\xbf\xfe\xf9\x06"
  39502. "\x00\x5b\xd7\xfc\x11\x0b\xcf\x16\x61\xef\xac\x05\xa7\xaf\xec\x27"
  39503. "\x41\xc8\x5e\x9e\x0d\xf9\x2f\xaf\x20\x79\x17\xe5\x17\x91\x2a\x27"
  39504. "\x34\x1c\xbc\xaf\xeb\xef\x7f\x52\xe7\x1e\x4c\x2a\xca\xbd\x2b\xbe"
  39505. "\x34\xd6\xfb\x69\xd3\x3e\x49\x59\x60\xb4\x26\xc9\xb8\xce\xba", 79,
  39506. "\x6c\xe7\xcf\x7e\xab\x7b\xa0\xe1\xa7\x22\xcb\x88\xde\x5e\x42\xd2"
  39507. "\xec\x79\xe0\xa2\xcf\x5f\x0f\x6f\x6b\x89\x57\xcd\xae\x17\xd4\xc2"
  39508. "\xf3\x1b\xa2\xa8\x13\x78\x23\x2f\x83\xa8\xd4\x0c\xc0\xd2\xf3\x99"
  39509. "\xae\x81\xa1\xca\x5b\x5f\x45\xa6\x6f\x0c\x8a\xf3\xd4\x67\x40\x81"
  39510. "\x26\xe2\x01\x86\xe8\x5a\xd5\xf8\x58\x80\x9f\x56\xaa\x76\x96\xbf"
  39511. "\x31", 81,
  39512. "\x9a\x06\x33\xe0\xee\x00\x6a\x9b\xc8\x20\xd5\xe2\xc2\xed\xb5\x75",
  39513. "\xfa\x9e\x42\x2a\x31\x6b\xda\xca\xaa\x7d\x31\x8b\x84\x7a\xb8\xd7"
  39514. "\x8a\x81\x25\x64\xed\x41\x9b\xa9\x77\x10\xbd\x05\x0c\x4e\xc5\x31"
  39515. "\x0c\xa2\x86\xec\x8a\x94\xc8\x24\x23\x3c\x13\xee\xa5\x51\xc9\xdf"
  39516. "\x48\xc9\x55\xc5\x2f\x40\x73\x3f\x98\xbb\x8d\x69\x78\x46\x64\x17"
  39517. "\x8d\x49\x2f\x14\x62\xa4\x7c\x2a\x57\x38\x87\xce\xc6\x72\xd3\x5c"
  39518. "\xa1", 81
  39519. }};
  39520. int i;
  39521. byte computedCiphertext[82];
  39522. byte computedPlaintext[82];
  39523. byte siv[AES_BLOCK_SIZE];
  39524. int rc = 0;
  39525. for (i = 0; i < AES_SIV_TEST_VECTORS; ++i) {
  39526. rc = wc_AesSivEncrypt(testVectors[i].key, testVectors[i].keySz,
  39527. testVectors[i].assoc, testVectors[i].assocSz,
  39528. testVectors[i].nonce, testVectors[i].nonceSz,
  39529. testVectors[i].plaintext,
  39530. testVectors[i].plaintextSz, siv,
  39531. computedCiphertext);
  39532. if (rc != 0) {
  39533. return -16000;
  39534. }
  39535. rc = XMEMCMP(siv, testVectors[i].siv, AES_BLOCK_SIZE);
  39536. if (rc != 0) {
  39537. return -16001;
  39538. }
  39539. rc = XMEMCMP(computedCiphertext, testVectors[i].ciphertext,
  39540. testVectors[i].ciphertextSz);
  39541. if (rc != 0) {
  39542. return -16002;
  39543. }
  39544. rc = wc_AesSivDecrypt(testVectors[i].key, testVectors[i].keySz,
  39545. testVectors[i].assoc, testVectors[i].assocSz,
  39546. testVectors[i].nonce, testVectors[i].nonceSz,
  39547. computedCiphertext, testVectors[i].plaintextSz,
  39548. siv, computedPlaintext);
  39549. if (rc != 0) {
  39550. return -16003;
  39551. }
  39552. rc = XMEMCMP(computedPlaintext, testVectors[i].plaintext,
  39553. testVectors[i].plaintextSz);
  39554. if (rc != 0) {
  39555. return -16004;
  39556. }
  39557. }
  39558. return 0;
  39559. }
  39560. #endif
  39561. #undef ERROR_OUT
  39562. #else
  39563. #ifndef NO_MAIN_DRIVER
  39564. int main(void) { return 0; }
  39565. #endif
  39566. #endif /* NO_CRYPT_TEST */