test.c 1.3 MB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180221812218222183221842218522186221872218822189221902219122192221932219422195221962219722198221992220022201222022220322204222052220622207222082220922210222112221222213222142221522216222172221822219222202222122222222232222422225222262222722228222292223022231222322223322234222352223622237222382223922240222412224222243222442224522246222472224822249222502225122252222532225422255222562225722258222592226022261222622226322264222652226622267222682226922270222712227222273222742227522276222772227822279222802228122282222832228422285222862228722288222892229022291222922229322294222952229622297222982229922300223012230222303223042230522306223072230822309223102231122312223132231422315223162231722318223192232022321223222232322324223252232622327223282232922330223312233222333223342233522336223372233822339223402234122342223432234422345223462234722348223492235022351223522235322354223552235622357223582235922360223612236222363223642236522366223672236822369223702237122372223732237422375223762237722378223792238022381223822238322384223852238622387223882238922390223912239222393223942239522396223972239822399224002240122402224032240422405224062240722408224092241022411224122241322414224152241622417224182241922420224212242222423224242242522426224272242822429224302243122432224332243422435224362243722438224392244022441224422244322444224452244622447224482244922450224512245222453224542245522456224572245822459224602246122462224632246422465224662246722468224692247022471224722247322474224752247622477224782247922480224812248222483224842248522486224872248822489224902249122492224932249422495224962249722498224992250022501225022250322504225052250622507225082250922510225112251222513225142251522516225172251822519225202252122522225232252422525225262252722528225292253022531225322253322534225352253622537225382253922540225412254222543225442254522546225472254822549225502255122552225532255422555225562255722558225592256022561225622256322564225652256622567225682256922570225712257222573225742257522576225772257822579225802258122582225832258422585225862258722588225892259022591225922259322594225952259622597225982259922600226012260222603226042260522606226072260822609226102261122612226132261422615226162261722618226192262022621226222262322624226252262622627226282262922630226312263222633226342263522636226372263822639226402264122642226432264422645226462264722648226492265022651226522265322654226552265622657226582265922660226612266222663226642266522666226672266822669226702267122672226732267422675226762267722678226792268022681226822268322684226852268622687226882268922690226912269222693226942269522696226972269822699227002270122702227032270422705227062270722708227092271022711227122271322714227152271622717227182271922720227212272222723227242272522726227272272822729227302273122732227332273422735227362273722738227392274022741227422274322744227452274622747227482274922750227512275222753227542275522756227572275822759227602276122762227632276422765227662276722768227692277022771227722277322774227752277622777227782277922780227812278222783227842278522786227872278822789227902279122792227932279422795227962279722798227992280022801228022280322804228052280622807228082280922810228112281222813228142281522816228172281822819228202282122822228232282422825228262282722828228292283022831228322283322834228352283622837228382283922840228412284222843228442284522846228472284822849228502285122852228532285422855228562285722858228592286022861228622286322864228652286622867228682286922870228712287222873228742287522876228772287822879228802288122882228832288422885228862288722888228892289022891228922289322894228952289622897228982289922900229012290222903229042290522906229072290822909229102291122912229132291422915229162291722918229192292022921229222292322924229252292622927229282292922930229312293222933229342293522936229372293822939229402294122942229432294422945229462294722948229492295022951229522295322954229552295622957229582295922960229612296222963229642296522966229672296822969229702297122972229732297422975229762297722978229792298022981229822298322984229852298622987229882298922990229912299222993229942299522996229972299822999230002300123002230032300423005230062300723008230092301023011230122301323014230152301623017230182301923020230212302223023230242302523026230272302823029230302303123032230332303423035230362303723038230392304023041230422304323044230452304623047230482304923050230512305223053230542305523056230572305823059230602306123062230632306423065230662306723068230692307023071230722307323074230752307623077230782307923080230812308223083230842308523086230872308823089230902309123092230932309423095230962309723098230992310023101231022310323104231052310623107231082310923110231112311223113231142311523116231172311823119231202312123122231232312423125231262312723128231292313023131231322313323134231352313623137231382313923140231412314223143231442314523146231472314823149231502315123152231532315423155231562315723158231592316023161231622316323164231652316623167231682316923170231712317223173231742317523176231772317823179231802318123182231832318423185231862318723188231892319023191231922319323194231952319623197231982319923200232012320223203232042320523206232072320823209232102321123212232132321423215232162321723218232192322023221232222322323224232252322623227232282322923230232312323223233232342323523236232372323823239232402324123242232432324423245232462324723248232492325023251232522325323254232552325623257232582325923260232612326223263232642326523266232672326823269232702327123272232732327423275232762327723278232792328023281232822328323284232852328623287232882328923290232912329223293232942329523296232972329823299233002330123302233032330423305233062330723308233092331023311233122331323314233152331623317233182331923320233212332223323233242332523326233272332823329233302333123332233332333423335233362333723338233392334023341233422334323344233452334623347233482334923350233512335223353233542335523356233572335823359233602336123362233632336423365233662336723368233692337023371233722337323374233752337623377233782337923380233812338223383233842338523386233872338823389233902339123392233932339423395233962339723398233992340023401234022340323404234052340623407234082340923410234112341223413234142341523416234172341823419234202342123422234232342423425234262342723428234292343023431234322343323434234352343623437234382343923440234412344223443234442344523446234472344823449234502345123452234532345423455234562345723458234592346023461234622346323464234652346623467234682346923470234712347223473234742347523476234772347823479234802348123482234832348423485234862348723488234892349023491234922349323494234952349623497234982349923500235012350223503235042350523506235072350823509235102351123512235132351423515235162351723518235192352023521235222352323524235252352623527235282352923530235312353223533235342353523536235372353823539235402354123542235432354423545235462354723548235492355023551235522355323554235552355623557235582355923560235612356223563235642356523566235672356823569235702357123572235732357423575235762357723578235792358023581235822358323584235852358623587235882358923590235912359223593235942359523596235972359823599236002360123602236032360423605236062360723608236092361023611236122361323614236152361623617236182361923620236212362223623236242362523626236272362823629236302363123632236332363423635236362363723638236392364023641236422364323644236452364623647236482364923650236512365223653236542365523656236572365823659236602366123662236632366423665236662366723668236692367023671236722367323674236752367623677236782367923680236812368223683236842368523686236872368823689236902369123692236932369423695236962369723698236992370023701237022370323704237052370623707237082370923710237112371223713237142371523716237172371823719237202372123722237232372423725237262372723728237292373023731237322373323734237352373623737237382373923740237412374223743237442374523746237472374823749237502375123752237532375423755237562375723758237592376023761237622376323764237652376623767237682376923770237712377223773237742377523776237772377823779237802378123782237832378423785237862378723788237892379023791237922379323794237952379623797237982379923800238012380223803238042380523806238072380823809238102381123812238132381423815238162381723818238192382023821238222382323824238252382623827238282382923830238312383223833238342383523836238372383823839238402384123842238432384423845238462384723848238492385023851238522385323854238552385623857238582385923860238612386223863238642386523866238672386823869238702387123872238732387423875238762387723878238792388023881238822388323884238852388623887238882388923890238912389223893238942389523896238972389823899239002390123902239032390423905239062390723908239092391023911239122391323914239152391623917239182391923920239212392223923239242392523926239272392823929239302393123932239332393423935239362393723938239392394023941239422394323944239452394623947239482394923950239512395223953239542395523956239572395823959239602396123962239632396423965239662396723968239692397023971239722397323974239752397623977239782397923980239812398223983239842398523986239872398823989239902399123992239932399423995239962399723998239992400024001240022400324004240052400624007240082400924010240112401224013240142401524016240172401824019240202402124022240232402424025240262402724028240292403024031240322403324034240352403624037240382403924040240412404224043240442404524046240472404824049240502405124052240532405424055240562405724058240592406024061240622406324064240652406624067240682406924070240712407224073240742407524076240772407824079240802408124082240832408424085240862408724088240892409024091240922409324094240952409624097240982409924100241012410224103241042410524106241072410824109241102411124112241132411424115241162411724118241192412024121241222412324124241252412624127241282412924130241312413224133241342413524136241372413824139241402414124142241432414424145241462414724148241492415024151241522415324154241552415624157241582415924160241612416224163241642416524166241672416824169241702417124172241732417424175241762417724178241792418024181241822418324184241852418624187241882418924190241912419224193241942419524196241972419824199242002420124202242032420424205242062420724208242092421024211242122421324214242152421624217242182421924220242212422224223242242422524226242272422824229242302423124232242332423424235242362423724238242392424024241242422424324244242452424624247242482424924250242512425224253242542425524256242572425824259242602426124262242632426424265242662426724268242692427024271242722427324274242752427624277242782427924280242812428224283242842428524286242872428824289242902429124292242932429424295242962429724298242992430024301243022430324304243052430624307243082430924310243112431224313243142431524316243172431824319243202432124322243232432424325243262432724328243292433024331243322433324334243352433624337243382433924340243412434224343243442434524346243472434824349243502435124352243532435424355243562435724358243592436024361243622436324364243652436624367243682436924370243712437224373243742437524376243772437824379243802438124382243832438424385243862438724388243892439024391243922439324394243952439624397243982439924400244012440224403244042440524406244072440824409244102441124412244132441424415244162441724418244192442024421244222442324424244252442624427244282442924430244312443224433244342443524436244372443824439244402444124442244432444424445244462444724448244492445024451244522445324454244552445624457244582445924460244612446224463244642446524466244672446824469244702447124472244732447424475244762447724478244792448024481244822448324484244852448624487244882448924490244912449224493244942449524496244972449824499245002450124502245032450424505245062450724508245092451024511245122451324514245152451624517245182451924520245212452224523245242452524526245272452824529245302453124532245332453424535245362453724538245392454024541245422454324544245452454624547245482454924550245512455224553245542455524556245572455824559245602456124562245632456424565245662456724568245692457024571245722457324574245752457624577245782457924580245812458224583245842458524586245872458824589245902459124592245932459424595245962459724598245992460024601246022460324604246052460624607246082460924610246112461224613246142461524616246172461824619246202462124622246232462424625246262462724628246292463024631246322463324634246352463624637246382463924640246412464224643246442464524646246472464824649246502465124652246532465424655246562465724658246592466024661246622466324664246652466624667246682466924670246712467224673246742467524676246772467824679246802468124682246832468424685246862468724688246892469024691246922469324694246952469624697246982469924700247012470224703247042470524706247072470824709247102471124712247132471424715247162471724718247192472024721247222472324724247252472624727247282472924730247312473224733247342473524736247372473824739247402474124742247432474424745247462474724748247492475024751247522475324754247552475624757247582475924760247612476224763247642476524766247672476824769247702477124772247732477424775247762477724778247792478024781247822478324784247852478624787247882478924790247912479224793247942479524796247972479824799248002480124802248032480424805248062480724808248092481024811248122481324814248152481624817248182481924820248212482224823248242482524826248272482824829248302483124832248332483424835248362483724838248392484024841248422484324844248452484624847248482484924850248512485224853248542485524856248572485824859248602486124862248632486424865248662486724868248692487024871248722487324874248752487624877248782487924880248812488224883248842488524886248872488824889248902489124892248932489424895248962489724898248992490024901249022490324904249052490624907249082490924910249112491224913249142491524916249172491824919249202492124922249232492424925249262492724928249292493024931249322493324934249352493624937249382493924940249412494224943249442494524946249472494824949249502495124952249532495424955249562495724958249592496024961249622496324964249652496624967249682496924970249712497224973249742497524976249772497824979249802498124982249832498424985249862498724988249892499024991249922499324994249952499624997249982499925000250012500225003250042500525006250072500825009250102501125012250132501425015250162501725018250192502025021250222502325024250252502625027250282502925030250312503225033250342503525036250372503825039250402504125042250432504425045250462504725048250492505025051250522505325054250552505625057250582505925060250612506225063250642506525066250672506825069250702507125072250732507425075250762507725078250792508025081250822508325084250852508625087250882508925090250912509225093250942509525096250972509825099251002510125102251032510425105251062510725108251092511025111251122511325114251152511625117251182511925120251212512225123251242512525126251272512825129251302513125132251332513425135251362513725138251392514025141251422514325144251452514625147251482514925150251512515225153251542515525156251572515825159251602516125162251632516425165251662516725168251692517025171251722517325174251752517625177251782517925180251812518225183251842518525186251872518825189251902519125192251932519425195251962519725198251992520025201252022520325204252052520625207252082520925210252112521225213252142521525216252172521825219252202522125222252232522425225252262522725228252292523025231252322523325234252352523625237252382523925240252412524225243252442524525246252472524825249252502525125252252532525425255252562525725258252592526025261252622526325264252652526625267252682526925270252712527225273252742527525276252772527825279252802528125282252832528425285252862528725288252892529025291252922529325294252952529625297252982529925300253012530225303253042530525306253072530825309253102531125312253132531425315253162531725318253192532025321253222532325324253252532625327253282532925330253312533225333253342533525336253372533825339253402534125342253432534425345253462534725348253492535025351253522535325354253552535625357253582535925360253612536225363253642536525366253672536825369253702537125372253732537425375253762537725378253792538025381253822538325384253852538625387253882538925390253912539225393253942539525396253972539825399254002540125402254032540425405254062540725408254092541025411254122541325414254152541625417254182541925420254212542225423254242542525426254272542825429254302543125432254332543425435254362543725438254392544025441254422544325444254452544625447254482544925450254512545225453254542545525456254572545825459254602546125462254632546425465254662546725468254692547025471254722547325474254752547625477254782547925480254812548225483254842548525486254872548825489254902549125492254932549425495254962549725498254992550025501255022550325504255052550625507255082550925510255112551225513255142551525516255172551825519255202552125522255232552425525255262552725528255292553025531255322553325534255352553625537255382553925540255412554225543255442554525546255472554825549255502555125552255532555425555255562555725558255592556025561255622556325564255652556625567255682556925570255712557225573255742557525576255772557825579255802558125582255832558425585255862558725588255892559025591255922559325594255952559625597255982559925600256012560225603256042560525606256072560825609256102561125612256132561425615256162561725618256192562025621256222562325624256252562625627256282562925630256312563225633256342563525636256372563825639256402564125642256432564425645256462564725648256492565025651256522565325654256552565625657256582565925660256612566225663256642566525666256672566825669256702567125672256732567425675256762567725678256792568025681256822568325684256852568625687256882568925690256912569225693256942569525696256972569825699257002570125702257032570425705257062570725708257092571025711257122571325714257152571625717257182571925720257212572225723257242572525726257272572825729257302573125732257332573425735257362573725738257392574025741257422574325744257452574625747257482574925750257512575225753257542575525756257572575825759257602576125762257632576425765257662576725768257692577025771257722577325774257752577625777257782577925780257812578225783257842578525786257872578825789257902579125792257932579425795257962579725798257992580025801258022580325804258052580625807258082580925810258112581225813258142581525816258172581825819258202582125822258232582425825258262582725828258292583025831258322583325834258352583625837258382583925840258412584225843258442584525846258472584825849258502585125852258532585425855258562585725858258592586025861258622586325864258652586625867258682586925870258712587225873258742587525876258772587825879258802588125882258832588425885258862588725888258892589025891258922589325894258952589625897258982589925900259012590225903259042590525906259072590825909259102591125912259132591425915259162591725918259192592025921259222592325924259252592625927259282592925930259312593225933259342593525936259372593825939259402594125942259432594425945259462594725948259492595025951259522595325954259552595625957259582595925960259612596225963259642596525966259672596825969259702597125972259732597425975259762597725978259792598025981259822598325984259852598625987259882598925990259912599225993259942599525996259972599825999260002600126002260032600426005260062600726008260092601026011260122601326014260152601626017260182601926020260212602226023260242602526026260272602826029260302603126032260332603426035260362603726038260392604026041260422604326044260452604626047260482604926050260512605226053260542605526056260572605826059260602606126062260632606426065260662606726068260692607026071260722607326074260752607626077260782607926080260812608226083260842608526086260872608826089260902609126092260932609426095260962609726098260992610026101261022610326104261052610626107261082610926110261112611226113261142611526116261172611826119261202612126122261232612426125261262612726128261292613026131261322613326134261352613626137261382613926140261412614226143261442614526146261472614826149261502615126152261532615426155261562615726158261592616026161261622616326164261652616626167261682616926170261712617226173261742617526176261772617826179261802618126182261832618426185261862618726188261892619026191261922619326194261952619626197261982619926200262012620226203262042620526206262072620826209262102621126212262132621426215262162621726218262192622026221262222622326224262252622626227262282622926230262312623226233262342623526236262372623826239262402624126242262432624426245262462624726248262492625026251262522625326254262552625626257262582625926260262612626226263262642626526266262672626826269262702627126272262732627426275262762627726278262792628026281262822628326284262852628626287262882628926290262912629226293262942629526296262972629826299263002630126302263032630426305263062630726308263092631026311263122631326314263152631626317263182631926320263212632226323263242632526326263272632826329263302633126332263332633426335263362633726338263392634026341263422634326344263452634626347263482634926350263512635226353263542635526356263572635826359263602636126362263632636426365263662636726368263692637026371263722637326374263752637626377263782637926380263812638226383263842638526386263872638826389263902639126392263932639426395263962639726398263992640026401264022640326404264052640626407264082640926410264112641226413264142641526416264172641826419264202642126422264232642426425264262642726428264292643026431264322643326434264352643626437264382643926440264412644226443264442644526446264472644826449264502645126452264532645426455264562645726458264592646026461264622646326464264652646626467264682646926470264712647226473264742647526476264772647826479264802648126482264832648426485264862648726488264892649026491264922649326494264952649626497264982649926500265012650226503265042650526506265072650826509265102651126512265132651426515265162651726518265192652026521265222652326524265252652626527265282652926530265312653226533265342653526536265372653826539265402654126542265432654426545265462654726548265492655026551265522655326554265552655626557265582655926560265612656226563265642656526566265672656826569265702657126572265732657426575265762657726578265792658026581265822658326584265852658626587265882658926590265912659226593265942659526596265972659826599266002660126602266032660426605266062660726608266092661026611266122661326614266152661626617266182661926620266212662226623266242662526626266272662826629266302663126632266332663426635266362663726638266392664026641266422664326644266452664626647266482664926650266512665226653266542665526656266572665826659266602666126662266632666426665266662666726668266692667026671266722667326674266752667626677266782667926680266812668226683266842668526686266872668826689266902669126692266932669426695266962669726698266992670026701267022670326704267052670626707267082670926710267112671226713267142671526716267172671826719267202672126722267232672426725267262672726728267292673026731267322673326734267352673626737267382673926740267412674226743267442674526746267472674826749267502675126752267532675426755267562675726758267592676026761267622676326764267652676626767267682676926770267712677226773267742677526776267772677826779267802678126782267832678426785267862678726788267892679026791267922679326794267952679626797267982679926800268012680226803268042680526806268072680826809268102681126812268132681426815268162681726818268192682026821268222682326824268252682626827268282682926830268312683226833268342683526836268372683826839268402684126842268432684426845268462684726848268492685026851268522685326854268552685626857268582685926860268612686226863268642686526866268672686826869268702687126872268732687426875268762687726878268792688026881268822688326884268852688626887268882688926890268912689226893268942689526896268972689826899269002690126902269032690426905269062690726908269092691026911269122691326914269152691626917269182691926920269212692226923269242692526926269272692826929269302693126932269332693426935269362693726938269392694026941269422694326944269452694626947269482694926950269512695226953269542695526956269572695826959269602696126962269632696426965269662696726968269692697026971269722697326974269752697626977269782697926980269812698226983269842698526986269872698826989269902699126992269932699426995269962699726998269992700027001270022700327004270052700627007270082700927010270112701227013270142701527016270172701827019270202702127022270232702427025270262702727028270292703027031270322703327034270352703627037270382703927040270412704227043270442704527046270472704827049270502705127052270532705427055270562705727058270592706027061270622706327064270652706627067270682706927070270712707227073270742707527076270772707827079270802708127082270832708427085270862708727088270892709027091270922709327094270952709627097270982709927100271012710227103271042710527106271072710827109271102711127112271132711427115271162711727118271192712027121271222712327124271252712627127271282712927130271312713227133271342713527136271372713827139271402714127142271432714427145271462714727148271492715027151271522715327154271552715627157271582715927160271612716227163271642716527166271672716827169271702717127172271732717427175271762717727178271792718027181271822718327184271852718627187271882718927190271912719227193271942719527196271972719827199272002720127202272032720427205272062720727208272092721027211272122721327214272152721627217272182721927220272212722227223272242722527226272272722827229272302723127232272332723427235272362723727238272392724027241272422724327244272452724627247272482724927250272512725227253272542725527256272572725827259272602726127262272632726427265272662726727268272692727027271272722727327274272752727627277272782727927280272812728227283272842728527286272872728827289272902729127292272932729427295272962729727298272992730027301273022730327304273052730627307273082730927310273112731227313273142731527316273172731827319273202732127322273232732427325273262732727328273292733027331273322733327334273352733627337273382733927340273412734227343273442734527346273472734827349273502735127352273532735427355273562735727358273592736027361273622736327364273652736627367273682736927370273712737227373273742737527376273772737827379273802738127382273832738427385273862738727388273892739027391273922739327394273952739627397273982739927400274012740227403274042740527406274072740827409274102741127412274132741427415274162741727418274192742027421274222742327424274252742627427274282742927430274312743227433274342743527436274372743827439274402744127442274432744427445274462744727448274492745027451274522745327454274552745627457274582745927460274612746227463274642746527466274672746827469274702747127472274732747427475274762747727478274792748027481274822748327484274852748627487274882748927490274912749227493274942749527496274972749827499275002750127502275032750427505275062750727508275092751027511275122751327514275152751627517275182751927520275212752227523275242752527526275272752827529275302753127532275332753427535275362753727538275392754027541275422754327544275452754627547275482754927550275512755227553275542755527556275572755827559275602756127562275632756427565275662756727568275692757027571275722757327574275752757627577275782757927580275812758227583275842758527586275872758827589275902759127592275932759427595275962759727598275992760027601276022760327604276052760627607276082760927610276112761227613276142761527616276172761827619276202762127622276232762427625276262762727628276292763027631276322763327634276352763627637276382763927640276412764227643276442764527646276472764827649276502765127652276532765427655276562765727658276592766027661276622766327664276652766627667276682766927670276712767227673276742767527676276772767827679276802768127682276832768427685276862768727688276892769027691276922769327694276952769627697276982769927700277012770227703277042770527706277072770827709277102771127712277132771427715277162771727718277192772027721277222772327724277252772627727277282772927730277312773227733277342773527736277372773827739277402774127742277432774427745277462774727748277492775027751277522775327754277552775627757277582775927760277612776227763277642776527766277672776827769277702777127772277732777427775277762777727778277792778027781277822778327784277852778627787277882778927790277912779227793277942779527796277972779827799278002780127802278032780427805278062780727808278092781027811278122781327814278152781627817278182781927820278212782227823278242782527826278272782827829278302783127832278332783427835278362783727838278392784027841278422784327844278452784627847278482784927850278512785227853278542785527856278572785827859278602786127862278632786427865278662786727868278692787027871278722787327874278752787627877278782787927880278812788227883278842788527886278872788827889278902789127892278932789427895278962789727898278992790027901279022790327904279052790627907279082790927910279112791227913279142791527916279172791827919279202792127922279232792427925279262792727928279292793027931279322793327934279352793627937279382793927940279412794227943279442794527946279472794827949279502795127952279532795427955279562795727958279592796027961279622796327964279652796627967279682796927970279712797227973279742797527976279772797827979279802798127982279832798427985279862798727988279892799027991279922799327994279952799627997279982799928000280012800228003280042800528006280072800828009280102801128012280132801428015280162801728018280192802028021280222802328024280252802628027280282802928030280312803228033280342803528036280372803828039280402804128042280432804428045280462804728048280492805028051280522805328054280552805628057280582805928060280612806228063280642806528066280672806828069280702807128072280732807428075280762807728078280792808028081280822808328084280852808628087280882808928090280912809228093280942809528096280972809828099281002810128102281032810428105281062810728108281092811028111281122811328114281152811628117281182811928120281212812228123281242812528126281272812828129281302813128132281332813428135281362813728138281392814028141281422814328144281452814628147281482814928150281512815228153281542815528156281572815828159281602816128162281632816428165281662816728168281692817028171281722817328174281752817628177281782817928180281812818228183281842818528186281872818828189281902819128192281932819428195281962819728198281992820028201282022820328204282052820628207282082820928210282112821228213282142821528216282172821828219282202822128222282232822428225282262822728228282292823028231282322823328234282352823628237282382823928240282412824228243282442824528246282472824828249282502825128252282532825428255282562825728258282592826028261282622826328264282652826628267282682826928270282712827228273282742827528276282772827828279282802828128282282832828428285282862828728288282892829028291282922829328294282952829628297282982829928300283012830228303283042830528306283072830828309283102831128312283132831428315283162831728318283192832028321283222832328324283252832628327283282832928330283312833228333283342833528336283372833828339283402834128342283432834428345283462834728348283492835028351283522835328354283552835628357283582835928360283612836228363283642836528366283672836828369283702837128372283732837428375283762837728378283792838028381283822838328384283852838628387283882838928390283912839228393283942839528396283972839828399284002840128402284032840428405284062840728408284092841028411284122841328414284152841628417284182841928420284212842228423284242842528426284272842828429284302843128432284332843428435284362843728438284392844028441284422844328444284452844628447284482844928450284512845228453284542845528456284572845828459284602846128462284632846428465284662846728468284692847028471284722847328474284752847628477284782847928480284812848228483284842848528486284872848828489284902849128492284932849428495284962849728498284992850028501285022850328504285052850628507285082850928510285112851228513285142851528516285172851828519285202852128522285232852428525285262852728528285292853028531285322853328534285352853628537285382853928540285412854228543285442854528546285472854828549285502855128552285532855428555285562855728558285592856028561285622856328564285652856628567285682856928570285712857228573285742857528576285772857828579285802858128582285832858428585285862858728588285892859028591285922859328594285952859628597285982859928600286012860228603286042860528606286072860828609286102861128612286132861428615286162861728618286192862028621286222862328624286252862628627286282862928630286312863228633286342863528636286372863828639286402864128642286432864428645286462864728648286492865028651286522865328654286552865628657286582865928660286612866228663286642866528666286672866828669286702867128672286732867428675286762867728678286792868028681286822868328684286852868628687286882868928690286912869228693286942869528696286972869828699287002870128702287032870428705287062870728708287092871028711287122871328714287152871628717287182871928720287212872228723287242872528726287272872828729287302873128732287332873428735287362873728738287392874028741287422874328744287452874628747287482874928750287512875228753287542875528756287572875828759287602876128762287632876428765287662876728768287692877028771287722877328774287752877628777287782877928780287812878228783287842878528786287872878828789287902879128792287932879428795287962879728798287992880028801288022880328804288052880628807288082880928810288112881228813288142881528816288172881828819288202882128822288232882428825288262882728828288292883028831288322883328834288352883628837288382883928840288412884228843288442884528846288472884828849288502885128852288532885428855288562885728858288592886028861288622886328864288652886628867288682886928870288712887228873288742887528876288772887828879288802888128882288832888428885288862888728888288892889028891288922889328894288952889628897288982889928900289012890228903289042890528906289072890828909289102891128912289132891428915289162891728918289192892028921289222892328924289252892628927289282892928930289312893228933289342893528936289372893828939289402894128942289432894428945289462894728948289492895028951289522895328954289552895628957289582895928960289612896228963289642896528966289672896828969289702897128972289732897428975289762897728978289792898028981289822898328984289852898628987289882898928990289912899228993289942899528996289972899828999290002900129002290032900429005290062900729008290092901029011290122901329014290152901629017290182901929020290212902229023290242902529026290272902829029290302903129032290332903429035290362903729038290392904029041290422904329044290452904629047290482904929050290512905229053290542905529056290572905829059290602906129062290632906429065290662906729068290692907029071290722907329074290752907629077290782907929080290812908229083290842908529086290872908829089290902909129092290932909429095290962909729098290992910029101291022910329104291052910629107291082910929110291112911229113291142911529116291172911829119291202912129122291232912429125291262912729128291292913029131291322913329134291352913629137291382913929140291412914229143291442914529146291472914829149291502915129152291532915429155291562915729158291592916029161291622916329164291652916629167291682916929170291712917229173291742917529176291772917829179291802918129182291832918429185291862918729188291892919029191291922919329194291952919629197291982919929200292012920229203292042920529206292072920829209292102921129212292132921429215292162921729218292192922029221292222922329224292252922629227292282922929230292312923229233292342923529236292372923829239292402924129242292432924429245292462924729248292492925029251292522925329254292552925629257292582925929260292612926229263292642926529266292672926829269292702927129272292732927429275292762927729278292792928029281292822928329284292852928629287292882928929290292912929229293292942929529296292972929829299293002930129302293032930429305293062930729308293092931029311293122931329314293152931629317293182931929320293212932229323293242932529326293272932829329293302933129332293332933429335293362933729338293392934029341293422934329344293452934629347293482934929350293512935229353293542935529356293572935829359293602936129362293632936429365293662936729368293692937029371293722937329374293752937629377293782937929380293812938229383293842938529386293872938829389293902939129392293932939429395293962939729398293992940029401294022940329404294052940629407294082940929410294112941229413294142941529416294172941829419294202942129422294232942429425294262942729428294292943029431294322943329434294352943629437294382943929440294412944229443294442944529446294472944829449294502945129452294532945429455294562945729458294592946029461294622946329464294652946629467294682946929470294712947229473294742947529476294772947829479294802948129482294832948429485294862948729488294892949029491294922949329494294952949629497294982949929500295012950229503295042950529506295072950829509295102951129512295132951429515295162951729518295192952029521295222952329524295252952629527295282952929530295312953229533295342953529536295372953829539295402954129542295432954429545295462954729548295492955029551295522955329554295552955629557295582955929560295612956229563295642956529566295672956829569295702957129572295732957429575295762957729578295792958029581295822958329584295852958629587295882958929590295912959229593295942959529596295972959829599296002960129602296032960429605296062960729608296092961029611296122961329614296152961629617296182961929620296212962229623296242962529626296272962829629296302963129632296332963429635296362963729638296392964029641296422964329644296452964629647296482964929650296512965229653296542965529656296572965829659296602966129662296632966429665296662966729668296692967029671296722967329674296752967629677296782967929680296812968229683296842968529686296872968829689296902969129692296932969429695296962969729698296992970029701297022970329704297052970629707297082970929710297112971229713297142971529716297172971829719297202972129722297232972429725297262972729728297292973029731297322973329734297352973629737297382973929740297412974229743297442974529746297472974829749297502975129752297532975429755297562975729758297592976029761297622976329764297652976629767297682976929770297712977229773297742977529776297772977829779297802978129782297832978429785297862978729788297892979029791297922979329794297952979629797297982979929800298012980229803298042980529806298072980829809298102981129812298132981429815298162981729818298192982029821298222982329824298252982629827298282982929830298312983229833298342983529836298372983829839298402984129842298432984429845298462984729848298492985029851298522985329854298552985629857298582985929860298612986229863298642986529866298672986829869298702987129872298732987429875298762987729878298792988029881298822988329884298852988629887298882988929890298912989229893298942989529896298972989829899299002990129902299032990429905299062990729908299092991029911299122991329914299152991629917299182991929920299212992229923299242992529926299272992829929299302993129932299332993429935299362993729938299392994029941299422994329944299452994629947299482994929950299512995229953299542995529956299572995829959299602996129962299632996429965299662996729968299692997029971299722997329974299752997629977299782997929980299812998229983299842998529986299872998829989299902999129992299932999429995299962999729998299993000030001300023000330004300053000630007300083000930010300113001230013300143001530016300173001830019300203002130022300233002430025300263002730028300293003030031300323003330034300353003630037300383003930040300413004230043300443004530046300473004830049300503005130052300533005430055300563005730058300593006030061300623006330064300653006630067300683006930070300713007230073300743007530076300773007830079300803008130082300833008430085300863008730088300893009030091300923009330094300953009630097300983009930100301013010230103301043010530106301073010830109301103011130112301133011430115301163011730118301193012030121301223012330124301253012630127301283012930130301313013230133301343013530136301373013830139301403014130142301433014430145301463014730148301493015030151301523015330154301553015630157301583015930160301613016230163301643016530166301673016830169301703017130172301733017430175301763017730178301793018030181301823018330184301853018630187301883018930190301913019230193301943019530196301973019830199302003020130202302033020430205302063020730208302093021030211302123021330214302153021630217302183021930220302213022230223302243022530226302273022830229302303023130232302333023430235302363023730238302393024030241302423024330244302453024630247302483024930250302513025230253302543025530256302573025830259302603026130262302633026430265302663026730268302693027030271302723027330274302753027630277302783027930280302813028230283302843028530286302873028830289302903029130292302933029430295302963029730298302993030030301303023030330304303053030630307303083030930310303113031230313303143031530316303173031830319303203032130322303233032430325303263032730328303293033030331303323033330334303353033630337303383033930340303413034230343303443034530346303473034830349303503035130352303533035430355303563035730358303593036030361303623036330364303653036630367303683036930370303713037230373303743037530376303773037830379303803038130382303833038430385303863038730388303893039030391303923039330394303953039630397303983039930400304013040230403304043040530406304073040830409304103041130412304133041430415304163041730418304193042030421304223042330424304253042630427304283042930430304313043230433304343043530436304373043830439304403044130442304433044430445304463044730448304493045030451304523045330454304553045630457304583045930460304613046230463304643046530466304673046830469304703047130472304733047430475304763047730478304793048030481304823048330484304853048630487304883048930490304913049230493304943049530496304973049830499305003050130502305033050430505305063050730508305093051030511305123051330514305153051630517305183051930520305213052230523305243052530526305273052830529305303053130532305333053430535305363053730538305393054030541305423054330544305453054630547305483054930550305513055230553305543055530556305573055830559305603056130562305633056430565305663056730568305693057030571305723057330574305753057630577305783057930580305813058230583305843058530586305873058830589305903059130592305933059430595305963059730598305993060030601306023060330604306053060630607306083060930610306113061230613306143061530616306173061830619306203062130622306233062430625306263062730628306293063030631306323063330634306353063630637306383063930640306413064230643306443064530646306473064830649306503065130652306533065430655306563065730658306593066030661306623066330664306653066630667306683066930670306713067230673306743067530676306773067830679306803068130682306833068430685306863068730688306893069030691306923069330694306953069630697306983069930700307013070230703307043070530706307073070830709307103071130712307133071430715307163071730718307193072030721307223072330724307253072630727307283072930730307313073230733307343073530736307373073830739307403074130742307433074430745307463074730748307493075030751307523075330754307553075630757307583075930760307613076230763307643076530766307673076830769307703077130772307733077430775307763077730778307793078030781307823078330784307853078630787307883078930790307913079230793307943079530796307973079830799308003080130802308033080430805308063080730808308093081030811308123081330814308153081630817308183081930820308213082230823308243082530826308273082830829308303083130832308333083430835308363083730838308393084030841308423084330844308453084630847308483084930850308513085230853308543085530856308573085830859308603086130862308633086430865308663086730868308693087030871308723087330874308753087630877308783087930880308813088230883308843088530886308873088830889308903089130892308933089430895308963089730898308993090030901309023090330904309053090630907309083090930910309113091230913309143091530916309173091830919309203092130922309233092430925309263092730928309293093030931309323093330934309353093630937309383093930940309413094230943309443094530946309473094830949309503095130952309533095430955309563095730958309593096030961309623096330964309653096630967309683096930970309713097230973309743097530976309773097830979309803098130982309833098430985309863098730988309893099030991309923099330994309953099630997309983099931000310013100231003310043100531006310073100831009310103101131012310133101431015310163101731018310193102031021310223102331024310253102631027310283102931030310313103231033310343103531036310373103831039310403104131042310433104431045310463104731048310493105031051310523105331054310553105631057310583105931060310613106231063310643106531066310673106831069310703107131072310733107431075310763107731078310793108031081310823108331084310853108631087310883108931090310913109231093310943109531096310973109831099311003110131102311033110431105311063110731108311093111031111311123111331114311153111631117311183111931120311213112231123311243112531126311273112831129311303113131132311333113431135311363113731138311393114031141311423114331144311453114631147311483114931150311513115231153311543115531156311573115831159311603116131162311633116431165311663116731168311693117031171311723117331174311753117631177311783117931180311813118231183311843118531186311873118831189311903119131192311933119431195311963119731198311993120031201312023120331204312053120631207312083120931210312113121231213312143121531216312173121831219312203122131222312233122431225312263122731228312293123031231312323123331234312353123631237312383123931240312413124231243312443124531246312473124831249312503125131252312533125431255312563125731258312593126031261312623126331264312653126631267312683126931270312713127231273312743127531276312773127831279312803128131282312833128431285312863128731288312893129031291312923129331294312953129631297312983129931300313013130231303313043130531306313073130831309313103131131312313133131431315313163131731318313193132031321313223132331324313253132631327313283132931330313313133231333313343133531336313373133831339313403134131342313433134431345313463134731348313493135031351313523135331354313553135631357313583135931360313613136231363313643136531366313673136831369313703137131372313733137431375313763137731378313793138031381313823138331384313853138631387313883138931390313913139231393313943139531396313973139831399314003140131402314033140431405314063140731408314093141031411314123141331414314153141631417314183141931420314213142231423314243142531426314273142831429314303143131432314333143431435314363143731438314393144031441314423144331444314453144631447314483144931450314513145231453314543145531456314573145831459314603146131462314633146431465314663146731468314693147031471314723147331474314753147631477314783147931480314813148231483314843148531486314873148831489314903149131492314933149431495314963149731498314993150031501315023150331504315053150631507315083150931510315113151231513315143151531516315173151831519315203152131522315233152431525315263152731528315293153031531315323153331534315353153631537315383153931540315413154231543315443154531546315473154831549315503155131552315533155431555315563155731558315593156031561315623156331564315653156631567315683156931570315713157231573315743157531576315773157831579315803158131582315833158431585315863158731588315893159031591315923159331594315953159631597315983159931600316013160231603316043160531606316073160831609316103161131612316133161431615316163161731618316193162031621316223162331624316253162631627316283162931630316313163231633316343163531636316373163831639316403164131642316433164431645316463164731648316493165031651316523165331654316553165631657316583165931660316613166231663316643166531666316673166831669316703167131672316733167431675316763167731678316793168031681316823168331684316853168631687316883168931690316913169231693316943169531696316973169831699317003170131702317033170431705317063170731708317093171031711317123171331714317153171631717317183171931720317213172231723317243172531726317273172831729317303173131732317333173431735317363173731738317393174031741317423174331744317453174631747317483174931750317513175231753317543175531756317573175831759317603176131762317633176431765317663176731768317693177031771317723177331774317753177631777317783177931780317813178231783317843178531786317873178831789317903179131792317933179431795317963179731798317993180031801318023180331804318053180631807318083180931810318113181231813318143181531816318173181831819318203182131822318233182431825318263182731828318293183031831318323183331834318353183631837318383183931840318413184231843318443184531846318473184831849318503185131852318533185431855318563185731858318593186031861318623186331864318653186631867318683186931870318713187231873318743187531876318773187831879318803188131882318833188431885318863188731888318893189031891318923189331894318953189631897318983189931900319013190231903319043190531906319073190831909319103191131912319133191431915319163191731918319193192031921319223192331924319253192631927319283192931930319313193231933319343193531936319373193831939319403194131942319433194431945319463194731948319493195031951319523195331954319553195631957319583195931960319613196231963319643196531966319673196831969319703197131972319733197431975319763197731978319793198031981319823198331984319853198631987319883198931990319913199231993319943199531996319973199831999320003200132002320033200432005320063200732008320093201032011320123201332014320153201632017320183201932020320213202232023320243202532026320273202832029320303203132032320333203432035320363203732038320393204032041320423204332044320453204632047320483204932050320513205232053320543205532056320573205832059320603206132062320633206432065320663206732068320693207032071320723207332074320753207632077320783207932080320813208232083320843208532086320873208832089320903209132092320933209432095320963209732098320993210032101321023210332104321053210632107321083210932110321113211232113321143211532116321173211832119321203212132122321233212432125321263212732128321293213032131321323213332134321353213632137321383213932140321413214232143321443214532146321473214832149321503215132152321533215432155321563215732158321593216032161321623216332164321653216632167321683216932170321713217232173321743217532176321773217832179321803218132182321833218432185321863218732188321893219032191321923219332194321953219632197321983219932200322013220232203322043220532206322073220832209322103221132212322133221432215322163221732218322193222032221322223222332224322253222632227322283222932230322313223232233322343223532236322373223832239322403224132242322433224432245322463224732248322493225032251322523225332254322553225632257322583225932260322613226232263322643226532266322673226832269322703227132272322733227432275322763227732278322793228032281322823228332284322853228632287322883228932290322913229232293322943229532296322973229832299323003230132302323033230432305323063230732308323093231032311323123231332314323153231632317323183231932320323213232232323323243232532326323273232832329323303233132332323333233432335323363233732338323393234032341323423234332344323453234632347323483234932350323513235232353323543235532356323573235832359323603236132362323633236432365323663236732368323693237032371323723237332374323753237632377323783237932380323813238232383323843238532386323873238832389323903239132392323933239432395323963239732398323993240032401324023240332404324053240632407324083240932410324113241232413324143241532416324173241832419324203242132422324233242432425324263242732428324293243032431324323243332434324353243632437324383243932440324413244232443324443244532446324473244832449324503245132452324533245432455324563245732458324593246032461324623246332464324653246632467324683246932470324713247232473324743247532476324773247832479324803248132482324833248432485324863248732488324893249032491324923249332494324953249632497324983249932500325013250232503325043250532506325073250832509325103251132512325133251432515325163251732518325193252032521325223252332524325253252632527325283252932530325313253232533325343253532536325373253832539325403254132542325433254432545325463254732548325493255032551325523255332554325553255632557325583255932560325613256232563325643256532566325673256832569325703257132572325733257432575325763257732578325793258032581325823258332584325853258632587325883258932590325913259232593325943259532596325973259832599326003260132602326033260432605326063260732608326093261032611326123261332614326153261632617326183261932620326213262232623326243262532626326273262832629326303263132632326333263432635326363263732638326393264032641326423264332644326453264632647326483264932650326513265232653326543265532656326573265832659326603266132662326633266432665326663266732668326693267032671326723267332674326753267632677326783267932680326813268232683326843268532686326873268832689326903269132692326933269432695326963269732698326993270032701327023270332704327053270632707327083270932710327113271232713327143271532716327173271832719327203272132722327233272432725327263272732728327293273032731327323273332734327353273632737327383273932740327413274232743327443274532746327473274832749327503275132752327533275432755327563275732758327593276032761327623276332764327653276632767327683276932770327713277232773327743277532776327773277832779327803278132782327833278432785327863278732788327893279032791327923279332794327953279632797327983279932800328013280232803328043280532806328073280832809328103281132812328133281432815328163281732818328193282032821328223282332824328253282632827328283282932830328313283232833328343283532836328373283832839328403284132842328433284432845328463284732848328493285032851328523285332854328553285632857328583285932860328613286232863328643286532866328673286832869328703287132872328733287432875328763287732878328793288032881328823288332884328853288632887328883288932890328913289232893328943289532896328973289832899329003290132902329033290432905329063290732908329093291032911329123291332914329153291632917329183291932920329213292232923329243292532926329273292832929329303293132932329333293432935329363293732938329393294032941329423294332944329453294632947329483294932950329513295232953329543295532956329573295832959329603296132962329633296432965329663296732968329693297032971329723297332974329753297632977329783297932980329813298232983329843298532986329873298832989329903299132992329933299432995329963299732998329993300033001330023300333004330053300633007330083300933010330113301233013330143301533016330173301833019330203302133022330233302433025330263302733028330293303033031330323303333034330353303633037330383303933040330413304233043330443304533046330473304833049330503305133052330533305433055330563305733058330593306033061330623306333064330653306633067330683306933070330713307233073330743307533076330773307833079330803308133082330833308433085330863308733088330893309033091330923309333094330953309633097330983309933100331013310233103331043310533106331073310833109331103311133112331133311433115331163311733118331193312033121331223312333124331253312633127331283312933130331313313233133331343313533136331373313833139331403314133142331433314433145331463314733148331493315033151331523315333154331553315633157331583315933160331613316233163331643316533166331673316833169331703317133172331733317433175331763317733178331793318033181331823318333184331853318633187331883318933190331913319233193331943319533196331973319833199332003320133202332033320433205332063320733208332093321033211332123321333214332153321633217332183321933220332213322233223332243322533226332273322833229332303323133232332333323433235332363323733238332393324033241332423324333244332453324633247332483324933250332513325233253332543325533256332573325833259332603326133262332633326433265332663326733268332693327033271332723327333274332753327633277332783327933280332813328233283332843328533286332873328833289332903329133292332933329433295332963329733298332993330033301333023330333304333053330633307333083330933310333113331233313333143331533316333173331833319333203332133322333233332433325333263332733328333293333033331333323333333334333353333633337333383333933340333413334233343333443334533346333473334833349333503335133352333533335433355333563335733358333593336033361333623336333364333653336633367333683336933370333713337233373333743337533376333773337833379333803338133382333833338433385333863338733388333893339033391333923339333394333953339633397333983339933400334013340233403334043340533406334073340833409334103341133412334133341433415334163341733418334193342033421334223342333424334253342633427334283342933430334313343233433334343343533436334373343833439334403344133442334433344433445334463344733448334493345033451334523345333454334553345633457334583345933460334613346233463334643346533466334673346833469334703347133472334733347433475334763347733478334793348033481334823348333484334853348633487334883348933490334913349233493334943349533496334973349833499335003350133502335033350433505335063350733508335093351033511335123351333514335153351633517335183351933520335213352233523335243352533526335273352833529335303353133532335333353433535335363353733538335393354033541335423354333544335453354633547335483354933550335513355233553335543355533556335573355833559335603356133562335633356433565335663356733568335693357033571335723357333574335753357633577335783357933580335813358233583335843358533586335873358833589335903359133592335933359433595335963359733598335993360033601336023360333604336053360633607336083360933610336113361233613336143361533616336173361833619336203362133622336233362433625336263362733628336293363033631336323363333634336353363633637336383363933640336413364233643336443364533646336473364833649336503365133652336533365433655336563365733658336593366033661336623366333664336653366633667336683366933670336713367233673336743367533676336773367833679336803368133682336833368433685336863368733688336893369033691336923369333694336953369633697336983369933700337013370233703337043370533706337073370833709337103371133712337133371433715337163371733718337193372033721337223372333724337253372633727337283372933730337313373233733337343373533736337373373833739337403374133742337433374433745337463374733748337493375033751337523375333754337553375633757337583375933760337613376233763337643376533766337673376833769337703377133772337733377433775337763377733778337793378033781337823378333784337853378633787337883378933790337913379233793337943379533796337973379833799338003380133802338033380433805338063380733808338093381033811338123381333814338153381633817338183381933820338213382233823338243382533826338273382833829338303383133832338333383433835338363383733838338393384033841338423384333844338453384633847338483384933850338513385233853338543385533856338573385833859338603386133862338633386433865338663386733868338693387033871338723387333874338753387633877338783387933880338813388233883338843388533886338873388833889338903389133892338933389433895338963389733898338993390033901339023390333904339053390633907339083390933910339113391233913339143391533916339173391833919339203392133922339233392433925339263392733928339293393033931339323393333934339353393633937339383393933940339413394233943339443394533946339473394833949339503395133952339533395433955339563395733958339593396033961339623396333964339653396633967339683396933970339713397233973339743397533976339773397833979339803398133982339833398433985339863398733988339893399033991339923399333994339953399633997339983399934000340013400234003340043400534006340073400834009340103401134012340133401434015340163401734018340193402034021340223402334024340253402634027340283402934030340313403234033340343403534036340373403834039340403404134042340433404434045340463404734048340493405034051340523405334054340553405634057340583405934060340613406234063340643406534066340673406834069340703407134072340733407434075340763407734078340793408034081340823408334084340853408634087340883408934090340913409234093340943409534096340973409834099341003410134102341033410434105341063410734108341093411034111341123411334114341153411634117341183411934120341213412234123341243412534126341273412834129341303413134132341333413434135341363413734138341393414034141341423414334144341453414634147341483414934150341513415234153341543415534156341573415834159341603416134162341633416434165341663416734168341693417034171341723417334174341753417634177341783417934180341813418234183341843418534186341873418834189341903419134192341933419434195341963419734198341993420034201342023420334204342053420634207342083420934210342113421234213342143421534216342173421834219342203422134222342233422434225342263422734228342293423034231342323423334234342353423634237342383423934240342413424234243342443424534246342473424834249342503425134252342533425434255342563425734258342593426034261342623426334264342653426634267342683426934270342713427234273342743427534276342773427834279342803428134282342833428434285342863428734288342893429034291342923429334294342953429634297342983429934300343013430234303343043430534306343073430834309343103431134312343133431434315343163431734318343193432034321343223432334324343253432634327343283432934330343313433234333343343433534336343373433834339343403434134342343433434434345343463434734348343493435034351343523435334354343553435634357343583435934360343613436234363343643436534366343673436834369343703437134372343733437434375343763437734378343793438034381343823438334384343853438634387343883438934390343913439234393343943439534396343973439834399344003440134402344033440434405344063440734408344093441034411344123441334414344153441634417344183441934420344213442234423344243442534426344273442834429344303443134432344333443434435344363443734438344393444034441344423444334444344453444634447344483444934450344513445234453344543445534456344573445834459344603446134462344633446434465344663446734468344693447034471344723447334474344753447634477344783447934480344813448234483344843448534486344873448834489344903449134492344933449434495344963449734498344993450034501345023450334504345053450634507345083450934510345113451234513345143451534516345173451834519345203452134522345233452434525345263452734528345293453034531345323453334534345353453634537345383453934540345413454234543345443454534546345473454834549345503455134552345533455434555345563455734558345593456034561345623456334564345653456634567345683456934570345713457234573345743457534576345773457834579345803458134582345833458434585345863458734588345893459034591345923459334594345953459634597345983459934600346013460234603346043460534606346073460834609346103461134612346133461434615346163461734618346193462034621346223462334624346253462634627346283462934630346313463234633346343463534636346373463834639346403464134642346433464434645346463464734648346493465034651346523465334654346553465634657346583465934660346613466234663346643466534666346673466834669346703467134672346733467434675346763467734678346793468034681346823468334684346853468634687346883468934690346913469234693346943469534696346973469834699347003470134702347033470434705347063470734708347093471034711347123471334714347153471634717347183471934720347213472234723347243472534726347273472834729347303473134732347333473434735347363473734738347393474034741347423474334744347453474634747347483474934750347513475234753347543475534756347573475834759347603476134762347633476434765347663476734768347693477034771347723477334774347753477634777347783477934780347813478234783347843478534786347873478834789347903479134792347933479434795347963479734798347993480034801348023480334804348053480634807348083480934810348113481234813348143481534816348173481834819348203482134822348233482434825348263482734828348293483034831348323483334834348353483634837348383483934840348413484234843348443484534846348473484834849348503485134852348533485434855348563485734858348593486034861348623486334864348653486634867348683486934870348713487234873348743487534876348773487834879348803488134882348833488434885348863488734888348893489034891348923489334894348953489634897348983489934900349013490234903349043490534906349073490834909349103491134912349133491434915349163491734918349193492034921349223492334924349253492634927349283492934930349313493234933349343493534936349373493834939349403494134942349433494434945349463494734948349493495034951349523495334954349553495634957349583495934960349613496234963349643496534966349673496834969349703497134972349733497434975349763497734978349793498034981349823498334984349853498634987349883498934990349913499234993349943499534996349973499834999350003500135002350033500435005350063500735008350093501035011350123501335014350153501635017350183501935020350213502235023350243502535026350273502835029350303503135032350333503435035350363503735038350393504035041350423504335044350453504635047350483504935050350513505235053350543505535056350573505835059350603506135062350633506435065350663506735068350693507035071350723507335074350753507635077350783507935080350813508235083350843508535086350873508835089350903509135092350933509435095350963509735098350993510035101351023510335104351053510635107351083510935110351113511235113351143511535116351173511835119351203512135122351233512435125351263512735128351293513035131351323513335134351353513635137351383513935140351413514235143351443514535146351473514835149351503515135152351533515435155351563515735158351593516035161351623516335164351653516635167351683516935170351713517235173351743517535176351773517835179351803518135182351833518435185351863518735188351893519035191351923519335194351953519635197351983519935200352013520235203352043520535206352073520835209352103521135212352133521435215352163521735218352193522035221352223522335224352253522635227352283522935230352313523235233352343523535236352373523835239352403524135242352433524435245352463524735248352493525035251352523525335254352553525635257352583525935260352613526235263352643526535266352673526835269352703527135272352733527435275352763527735278352793528035281352823528335284352853528635287352883528935290352913529235293352943529535296352973529835299353003530135302353033530435305353063530735308353093531035311353123531335314353153531635317353183531935320353213532235323353243532535326353273532835329353303533135332353333533435335353363533735338353393534035341353423534335344353453534635347353483534935350353513535235353353543535535356353573535835359353603536135362353633536435365353663536735368353693537035371353723537335374353753537635377353783537935380353813538235383353843538535386353873538835389353903539135392353933539435395353963539735398353993540035401354023540335404354053540635407354083540935410354113541235413354143541535416354173541835419354203542135422354233542435425354263542735428354293543035431354323543335434354353543635437354383543935440354413544235443354443544535446354473544835449354503545135452354533545435455354563545735458354593546035461354623546335464354653546635467354683546935470354713547235473354743547535476354773547835479354803548135482354833548435485354863548735488354893549035491354923549335494354953549635497354983549935500355013550235503355043550535506355073550835509355103551135512355133551435515355163551735518355193552035521355223552335524355253552635527355283552935530355313553235533355343553535536355373553835539355403554135542355433554435545355463554735548355493555035551355523555335554355553555635557355583555935560355613556235563355643556535566355673556835569355703557135572355733557435575355763557735578355793558035581355823558335584355853558635587355883558935590355913559235593355943559535596355973559835599356003560135602356033560435605356063560735608356093561035611356123561335614356153561635617356183561935620356213562235623356243562535626356273562835629356303563135632356333563435635356363563735638356393564035641356423564335644356453564635647356483564935650356513565235653356543565535656356573565835659356603566135662356633566435665356663566735668356693567035671356723567335674356753567635677356783567935680356813568235683356843568535686356873568835689356903569135692356933569435695356963569735698356993570035701357023570335704357053570635707357083570935710357113571235713357143571535716357173571835719357203572135722357233572435725357263572735728357293573035731357323573335734357353573635737357383573935740357413574235743357443574535746357473574835749357503575135752357533575435755357563575735758357593576035761357623576335764357653576635767357683576935770357713577235773357743577535776357773577835779357803578135782357833578435785357863578735788357893579035791357923579335794357953579635797357983579935800358013580235803358043580535806358073580835809358103581135812358133581435815358163581735818358193582035821358223582335824358253582635827358283582935830358313583235833358343583535836358373583835839358403584135842358433584435845358463584735848358493585035851358523585335854358553585635857358583585935860358613586235863358643586535866358673586835869358703587135872358733587435875358763587735878358793588035881358823588335884358853588635887358883588935890358913589235893358943589535896358973589835899359003590135902359033590435905359063590735908359093591035911359123591335914359153591635917359183591935920359213592235923359243592535926359273592835929359303593135932359333593435935359363593735938359393594035941359423594335944359453594635947359483594935950359513595235953359543595535956359573595835959359603596135962359633596435965359663596735968359693597035971359723597335974359753597635977359783597935980359813598235983359843598535986359873598835989359903599135992359933599435995359963599735998359993600036001360023600336004360053600636007360083600936010360113601236013360143601536016360173601836019360203602136022360233602436025360263602736028360293603036031360323603336034360353603636037360383603936040360413604236043360443604536046360473604836049360503605136052360533605436055360563605736058360593606036061360623606336064360653606636067360683606936070360713607236073360743607536076360773607836079360803608136082360833608436085360863608736088360893609036091360923609336094360953609636097360983609936100361013610236103361043610536106361073610836109361103611136112361133611436115361163611736118361193612036121361223612336124361253612636127361283612936130361313613236133361343613536136361373613836139361403614136142361433614436145361463614736148361493615036151361523615336154361553615636157361583615936160361613616236163361643616536166361673616836169361703617136172361733617436175361763617736178361793618036181361823618336184361853618636187361883618936190361913619236193361943619536196361973619836199362003620136202362033620436205362063620736208362093621036211362123621336214362153621636217362183621936220362213622236223362243622536226362273622836229362303623136232362333623436235362363623736238362393624036241362423624336244362453624636247362483624936250362513625236253362543625536256362573625836259362603626136262362633626436265362663626736268362693627036271362723627336274362753627636277362783627936280362813628236283362843628536286362873628836289362903629136292362933629436295362963629736298362993630036301363023630336304363053630636307363083630936310363113631236313363143631536316363173631836319363203632136322363233632436325363263632736328363293633036331363323633336334363353633636337363383633936340363413634236343363443634536346363473634836349363503635136352363533635436355363563635736358363593636036361363623636336364363653636636367363683636936370363713637236373363743637536376363773637836379363803638136382363833638436385363863638736388363893639036391363923639336394363953639636397363983639936400364013640236403364043640536406364073640836409364103641136412364133641436415364163641736418364193642036421364223642336424364253642636427364283642936430364313643236433364343643536436364373643836439364403644136442364433644436445364463644736448364493645036451364523645336454364553645636457364583645936460364613646236463364643646536466364673646836469364703647136472364733647436475364763647736478364793648036481364823648336484364853648636487364883648936490364913649236493364943649536496364973649836499365003650136502365033650436505365063650736508365093651036511365123651336514365153651636517365183651936520365213652236523365243652536526365273652836529365303653136532365333653436535365363653736538365393654036541365423654336544365453654636547365483654936550365513655236553365543655536556365573655836559365603656136562365633656436565365663656736568365693657036571365723657336574365753657636577365783657936580365813658236583365843658536586365873658836589365903659136592365933659436595365963659736598365993660036601366023660336604366053660636607366083660936610366113661236613366143661536616366173661836619366203662136622366233662436625366263662736628366293663036631366323663336634366353663636637366383663936640366413664236643366443664536646366473664836649366503665136652366533665436655366563665736658366593666036661366623666336664366653666636667366683666936670366713667236673366743667536676366773667836679366803668136682366833668436685366863668736688366893669036691366923669336694366953669636697366983669936700367013670236703367043670536706367073670836709367103671136712367133671436715367163671736718367193672036721367223672336724367253672636727367283672936730367313673236733367343673536736367373673836739367403674136742367433674436745367463674736748367493675036751367523675336754367553675636757367583675936760367613676236763367643676536766367673676836769367703677136772367733677436775367763677736778367793678036781367823678336784367853678636787367883678936790367913679236793367943679536796367973679836799368003680136802368033680436805368063680736808368093681036811368123681336814368153681636817368183681936820368213682236823368243682536826368273682836829368303683136832368333683436835368363683736838368393684036841368423684336844368453684636847368483684936850368513685236853368543685536856368573685836859368603686136862368633686436865368663686736868368693687036871368723687336874368753687636877368783687936880368813688236883368843688536886368873688836889368903689136892368933689436895368963689736898368993690036901369023690336904369053690636907369083690936910369113691236913369143691536916369173691836919369203692136922369233692436925369263692736928369293693036931369323693336934369353693636937369383693936940369413694236943369443694536946369473694836949369503695136952369533695436955369563695736958369593696036961369623696336964369653696636967369683696936970369713697236973369743697536976369773697836979369803698136982369833698436985369863698736988369893699036991369923699336994369953699636997369983699937000370013700237003370043700537006370073700837009370103701137012370133701437015370163701737018370193702037021370223702337024370253702637027370283702937030370313703237033370343703537036370373703837039370403704137042370433704437045370463704737048370493705037051370523705337054370553705637057370583705937060370613706237063370643706537066370673706837069370703707137072370733707437075370763707737078370793708037081370823708337084370853708637087370883708937090370913709237093370943709537096370973709837099371003710137102371033710437105371063710737108371093711037111371123711337114371153711637117371183711937120371213712237123371243712537126371273712837129371303713137132371333713437135371363713737138371393714037141371423714337144371453714637147371483714937150371513715237153371543715537156371573715837159371603716137162371633716437165371663716737168371693717037171371723717337174371753717637177371783717937180371813718237183371843718537186371873718837189371903719137192371933719437195371963719737198371993720037201372023720337204372053720637207372083720937210372113721237213372143721537216372173721837219372203722137222372233722437225372263722737228372293723037231372323723337234372353723637237372383723937240372413724237243372443724537246372473724837249372503725137252372533725437255372563725737258372593726037261372623726337264372653726637267372683726937270372713727237273372743727537276372773727837279372803728137282372833728437285372863728737288372893729037291372923729337294372953729637297372983729937300373013730237303373043730537306373073730837309373103731137312373133731437315373163731737318373193732037321373223732337324373253732637327373283732937330373313733237333373343733537336373373733837339373403734137342373433734437345373463734737348373493735037351373523735337354373553735637357373583735937360373613736237363373643736537366373673736837369373703737137372373733737437375373763737737378373793738037381373823738337384373853738637387373883738937390373913739237393373943739537396373973739837399374003740137402374033740437405374063740737408374093741037411374123741337414374153741637417374183741937420374213742237423374243742537426374273742837429374303743137432374333743437435374363743737438374393744037441374423744337444374453744637447374483744937450374513745237453374543745537456374573745837459374603746137462374633746437465374663746737468374693747037471374723747337474374753747637477374783747937480374813748237483374843748537486374873748837489374903749137492374933749437495374963749737498374993750037501375023750337504375053750637507375083750937510375113751237513375143751537516375173751837519375203752137522375233752437525375263752737528375293753037531375323753337534375353753637537375383753937540375413754237543375443754537546375473754837549375503755137552375533755437555375563755737558375593756037561375623756337564375653756637567375683756937570375713757237573375743757537576375773757837579375803758137582375833758437585375863758737588375893759037591375923759337594375953759637597375983759937600376013760237603376043760537606376073760837609376103761137612376133761437615376163761737618376193762037621376223762337624376253762637627376283762937630376313763237633376343763537636376373763837639376403764137642376433764437645376463764737648376493765037651376523765337654376553765637657376583765937660376613766237663376643766537666376673766837669376703767137672376733767437675376763767737678376793768037681376823768337684376853768637687376883768937690376913769237693376943769537696376973769837699377003770137702377033770437705377063770737708377093771037711377123771337714377153771637717377183771937720377213772237723377243772537726377273772837729377303773137732377333773437735377363773737738377393774037741377423774337744377453774637747377483774937750377513775237753377543775537756377573775837759377603776137762377633776437765377663776737768377693777037771377723777337774377753777637777377783777937780377813778237783377843778537786377873778837789377903779137792377933779437795377963779737798377993780037801378023780337804378053780637807378083780937810378113781237813378143781537816378173781837819378203782137822378233782437825378263782737828378293783037831378323783337834378353783637837378383783937840378413784237843378443784537846378473784837849378503785137852378533785437855378563785737858378593786037861378623786337864378653786637867378683786937870378713787237873378743787537876378773787837879378803788137882378833788437885378863788737888378893789037891378923789337894378953789637897378983789937900379013790237903379043790537906379073790837909379103791137912379133791437915379163791737918379193792037921379223792337924379253792637927379283792937930379313793237933379343793537936379373793837939379403794137942379433794437945379463794737948379493795037951379523795337954379553795637957379583795937960379613796237963379643796537966379673796837969379703797137972379733797437975379763797737978379793798037981379823798337984379853798637987379883798937990379913799237993379943799537996379973799837999380003800138002380033800438005380063800738008380093801038011380123801338014380153801638017380183801938020380213802238023380243802538026380273802838029380303803138032380333803438035380363803738038380393804038041380423804338044380453804638047380483804938050380513805238053380543805538056380573805838059380603806138062380633806438065380663806738068380693807038071380723807338074380753807638077380783807938080380813808238083380843808538086380873808838089380903809138092380933809438095380963809738098380993810038101381023810338104381053810638107381083810938110381113811238113381143811538116381173811838119381203812138122381233812438125381263812738128381293813038131381323813338134381353813638137381383813938140381413814238143381443814538146381473814838149381503815138152381533815438155381563815738158381593816038161381623816338164381653816638167381683816938170381713817238173381743817538176381773817838179381803818138182381833818438185381863818738188381893819038191381923819338194381953819638197381983819938200382013820238203382043820538206382073820838209382103821138212382133821438215382163821738218382193822038221382223822338224382253822638227382283822938230382313823238233382343823538236382373823838239382403824138242382433824438245382463824738248382493825038251382523825338254382553825638257382583825938260382613826238263382643826538266382673826838269382703827138272382733827438275382763827738278382793828038281382823828338284382853828638287382883828938290382913829238293382943829538296382973829838299383003830138302383033830438305383063830738308383093831038311383123831338314383153831638317383183831938320383213832238323383243832538326383273832838329383303833138332383333833438335383363833738338383393834038341383423834338344383453834638347383483834938350383513835238353383543835538356383573835838359383603836138362383633836438365383663836738368383693837038371383723837338374383753837638377383783837938380383813838238383383843838538386383873838838389383903839138392383933839438395383963839738398383993840038401384023840338404384053840638407384083840938410384113841238413384143841538416384173841838419384203842138422384233842438425384263842738428384293843038431384323843338434384353843638437384383843938440384413844238443384443844538446384473844838449384503845138452384533845438455384563845738458384593846038461384623846338464384653846638467384683846938470384713847238473384743847538476384773847838479384803848138482384833848438485384863848738488384893849038491384923849338494384953849638497384983849938500385013850238503385043850538506385073850838509385103851138512385133851438515385163851738518385193852038521385223852338524385253852638527385283852938530385313853238533385343853538536385373853838539385403854138542385433854438545385463854738548385493855038551385523855338554385553855638557385583855938560385613856238563385643856538566385673856838569385703857138572385733857438575385763857738578385793858038581385823858338584385853858638587385883858938590385913859238593385943859538596385973859838599386003860138602386033860438605386063860738608386093861038611386123861338614386153861638617386183861938620386213862238623386243862538626386273862838629386303863138632386333863438635386363863738638386393864038641386423864338644386453864638647386483864938650386513865238653386543865538656386573865838659386603866138662386633866438665386663866738668386693867038671386723867338674386753867638677386783867938680386813868238683386843868538686386873868838689386903869138692386933869438695386963869738698386993870038701387023870338704387053870638707387083870938710387113871238713387143871538716387173871838719387203872138722387233872438725387263872738728387293873038731387323873338734387353873638737387383873938740387413874238743387443874538746387473874838749387503875138752387533875438755387563875738758387593876038761387623876338764387653876638767387683876938770387713877238773387743877538776387773877838779387803878138782387833878438785387863878738788387893879038791387923879338794387953879638797387983879938800388013880238803388043880538806388073880838809388103881138812388133881438815388163881738818388193882038821388223882338824388253882638827388283882938830388313883238833388343883538836388373883838839388403884138842388433884438845388463884738848388493885038851388523885338854388553885638857388583885938860388613886238863388643886538866388673886838869388703887138872388733887438875388763887738878388793888038881388823888338884388853888638887388883888938890388913889238893388943889538896388973889838899389003890138902389033890438905389063890738908389093891038911389123891338914389153891638917389183891938920389213892238923389243892538926389273892838929389303893138932389333893438935389363893738938389393894038941389423894338944389453894638947389483894938950389513895238953389543895538956389573895838959389603896138962389633896438965389663896738968389693897038971389723897338974389753897638977389783897938980389813898238983389843898538986389873898838989389903899138992389933899438995389963899738998389993900039001390023900339004390053900639007390083900939010390113901239013390143901539016390173901839019390203902139022390233902439025390263902739028390293903039031390323903339034390353903639037390383903939040390413904239043390443904539046390473904839049390503905139052390533905439055390563905739058390593906039061390623906339064390653906639067390683906939070390713907239073390743907539076390773907839079390803908139082390833908439085390863908739088390893909039091390923909339094390953909639097390983909939100391013910239103391043910539106391073910839109391103911139112391133911439115391163911739118391193912039121391223912339124391253912639127391283912939130391313913239133391343913539136391373913839139391403914139142391433914439145391463914739148391493915039151391523915339154391553915639157391583915939160391613916239163391643916539166391673916839169391703917139172391733917439175391763917739178391793918039181391823918339184391853918639187391883918939190391913919239193391943919539196391973919839199392003920139202392033920439205392063920739208392093921039211392123921339214392153921639217392183921939220392213922239223392243922539226392273922839229392303923139232392333923439235392363923739238392393924039241392423924339244392453924639247392483924939250392513925239253392543925539256392573925839259392603926139262392633926439265392663926739268392693927039271392723927339274392753927639277392783927939280392813928239283392843928539286392873928839289392903929139292392933929439295392963929739298392993930039301393023930339304393053930639307393083930939310393113931239313393143931539316393173931839319393203932139322393233932439325393263932739328393293933039331393323933339334393353933639337393383933939340393413934239343393443934539346393473934839349393503935139352393533935439355393563935739358393593936039361393623936339364393653936639367393683936939370393713937239373393743937539376393773937839379393803938139382393833938439385393863938739388393893939039391393923939339394393953939639397393983939939400394013940239403394043940539406394073940839409394103941139412394133941439415394163941739418394193942039421394223942339424394253942639427394283942939430394313943239433394343943539436394373943839439394403944139442394433944439445394463944739448394493945039451394523945339454394553945639457394583945939460394613946239463394643946539466394673946839469394703947139472394733947439475394763947739478394793948039481394823948339484394853948639487394883948939490394913949239493394943949539496394973949839499395003950139502395033950439505395063950739508395093951039511395123951339514395153951639517395183951939520395213952239523395243952539526395273952839529395303953139532395333953439535395363953739538395393954039541395423954339544395453954639547395483954939550395513955239553395543955539556395573955839559395603956139562395633956439565395663956739568395693957039571395723957339574395753957639577395783957939580395813958239583395843958539586395873958839589395903959139592395933959439595395963959739598395993960039601396023960339604396053960639607396083960939610396113961239613396143961539616396173961839619396203962139622396233962439625396263962739628396293963039631396323963339634396353963639637396383963939640396413964239643396443964539646396473964839649396503965139652396533965439655396563965739658396593966039661396623966339664396653966639667396683966939670396713967239673396743967539676396773967839679396803968139682396833968439685396863968739688396893969039691396923969339694396953969639697396983969939700397013970239703397043970539706397073970839709397103971139712397133971439715397163971739718397193972039721397223972339724397253972639727397283972939730397313973239733397343973539736397373973839739397403974139742397433974439745397463974739748397493975039751397523975339754397553975639757397583975939760397613976239763397643976539766397673976839769397703977139772397733977439775397763977739778397793978039781397823978339784397853978639787397883978939790397913979239793397943979539796397973979839799398003980139802398033980439805398063980739808398093981039811398123981339814398153981639817398183981939820398213982239823398243982539826398273982839829398303983139832398333983439835398363983739838398393984039841398423984339844398453984639847398483984939850398513985239853398543985539856398573985839859398603986139862398633986439865398663986739868398693987039871398723987339874398753987639877398783987939880398813988239883398843988539886398873988839889398903989139892398933989439895398963989739898398993990039901399023990339904399053990639907399083990939910399113991239913399143991539916399173991839919399203992139922399233992439925399263992739928399293993039931399323993339934399353993639937399383993939940399413994239943399443994539946399473994839949399503995139952399533995439955399563995739958399593996039961399623996339964399653996639967399683996939970399713997239973399743997539976399773997839979399803998139982399833998439985399863998739988399893999039991399923999339994399953999639997399983999940000400014000240003400044000540006400074000840009400104001140012400134001440015400164001740018400194002040021400224002340024400254002640027400284002940030400314003240033400344003540036400374003840039400404004140042400434004440045400464004740048400494005040051400524005340054400554005640057400584005940060400614006240063400644006540066400674006840069400704007140072400734007440075400764007740078400794008040081400824008340084400854008640087400884008940090400914009240093400944009540096400974009840099401004010140102401034010440105401064010740108401094011040111401124011340114401154011640117401184011940120401214012240123401244012540126401274012840129401304013140132401334013440135401364013740138401394014040141401424014340144401454014640147401484014940150401514015240153401544015540156401574015840159401604016140162401634016440165401664016740168401694017040171401724017340174401754017640177401784017940180401814018240183401844018540186401874018840189401904019140192401934019440195401964019740198401994020040201402024020340204402054020640207402084020940210402114021240213402144021540216402174021840219402204022140222402234022440225402264022740228402294023040231402324023340234402354023640237402384023940240402414024240243402444024540246402474024840249402504025140252402534025440255402564025740258402594026040261402624026340264402654026640267402684026940270402714027240273402744027540276402774027840279402804028140282402834028440285402864028740288402894029040291402924029340294402954029640297402984029940300403014030240303403044030540306403074030840309403104031140312403134031440315403164031740318403194032040321403224032340324403254032640327403284032940330403314033240333403344033540336403374033840339403404034140342403434034440345403464034740348403494035040351403524035340354403554035640357403584035940360403614036240363403644036540366403674036840369403704037140372403734037440375403764037740378403794038040381403824038340384403854038640387403884038940390403914039240393403944039540396403974039840399404004040140402404034040440405404064040740408404094041040411404124041340414404154041640417404184041940420404214042240423404244042540426404274042840429404304043140432404334043440435404364043740438404394044040441404424044340444404454044640447404484044940450404514045240453404544045540456404574045840459404604046140462404634046440465404664046740468404694047040471404724047340474404754047640477404784047940480404814048240483404844048540486404874048840489404904049140492404934049440495404964049740498404994050040501405024050340504405054050640507405084050940510405114051240513405144051540516405174051840519405204052140522405234052440525405264052740528405294053040531405324053340534405354053640537405384053940540405414054240543405444054540546405474054840549405504055140552405534055440555405564055740558405594056040561405624056340564405654056640567405684056940570405714057240573405744057540576405774057840579405804058140582405834058440585405864058740588405894059040591405924059340594405954059640597405984059940600406014060240603406044060540606406074060840609406104061140612406134061440615406164061740618406194062040621406224062340624406254062640627406284062940630406314063240633406344063540636406374063840639406404064140642406434064440645406464064740648406494065040651406524065340654406554065640657406584065940660406614066240663406644066540666406674066840669406704067140672406734067440675406764067740678406794068040681406824068340684406854068640687406884068940690406914069240693406944069540696406974069840699407004070140702407034070440705407064070740708407094071040711407124071340714407154071640717407184071940720407214072240723407244072540726407274072840729407304073140732407334073440735407364073740738407394074040741407424074340744407454074640747407484074940750407514075240753407544075540756407574075840759407604076140762407634076440765407664076740768407694077040771407724077340774407754077640777407784077940780407814078240783407844078540786407874078840789407904079140792407934079440795407964079740798407994080040801408024080340804408054080640807408084080940810408114081240813408144081540816408174081840819408204082140822408234082440825408264082740828408294083040831408324083340834408354083640837408384083940840408414084240843408444084540846408474084840849408504085140852408534085440855408564085740858408594086040861408624086340864408654086640867408684086940870408714087240873408744087540876408774087840879408804088140882408834088440885408864088740888408894089040891408924089340894408954089640897408984089940900409014090240903409044090540906409074090840909409104091140912409134091440915409164091740918409194092040921409224092340924409254092640927409284092940930409314093240933409344093540936409374093840939409404094140942409434094440945409464094740948409494095040951409524095340954409554095640957409584095940960409614096240963409644096540966409674096840969409704097140972409734097440975409764097740978409794098040981409824098340984409854098640987409884098940990409914099240993409944099540996409974099840999410004100141002410034100441005410064100741008410094101041011410124101341014410154101641017410184101941020410214102241023410244102541026410274102841029410304103141032410334103441035410364103741038410394104041041410424104341044410454104641047410484104941050410514105241053410544105541056410574105841059410604106141062410634106441065410664106741068410694107041071410724107341074410754107641077410784107941080410814108241083410844108541086410874108841089410904109141092410934109441095410964109741098410994110041101411024110341104411054110641107411084110941110411114111241113411144111541116411174111841119411204112141122411234112441125411264112741128411294113041131411324113341134411354113641137411384113941140411414114241143411444114541146411474114841149411504115141152411534115441155411564115741158411594116041161411624116341164411654116641167411684116941170411714117241173411744117541176411774117841179411804118141182411834118441185411864118741188411894119041191411924119341194411954119641197411984119941200412014120241203412044120541206412074120841209412104121141212412134121441215412164121741218412194122041221412224122341224412254122641227412284122941230412314123241233412344123541236412374123841239412404124141242412434124441245412464124741248412494125041251412524125341254412554125641257412584125941260412614126241263412644126541266412674126841269412704127141272412734127441275412764127741278412794128041281412824128341284412854128641287412884128941290412914129241293412944129541296412974129841299413004130141302413034130441305413064130741308413094131041311413124131341314413154131641317413184131941320413214132241323413244132541326413274132841329413304133141332413334133441335413364133741338413394134041341413424134341344413454134641347413484134941350413514135241353413544135541356413574135841359413604136141362413634136441365413664136741368413694137041371413724137341374413754137641377413784137941380413814138241383413844138541386413874138841389413904139141392413934139441395413964139741398413994140041401414024140341404414054140641407414084140941410414114141241413414144141541416414174141841419414204142141422414234142441425414264142741428414294143041431414324143341434414354143641437414384143941440414414144241443414444144541446414474144841449414504145141452414534145441455414564145741458414594146041461414624146341464414654146641467414684146941470414714147241473414744147541476414774147841479414804148141482414834148441485414864148741488414894149041491414924149341494414954149641497414984149941500415014150241503415044150541506415074150841509415104151141512415134151441515415164151741518415194152041521415224152341524415254152641527415284152941530415314153241533415344153541536415374153841539415404154141542415434154441545415464154741548415494155041551415524155341554415554155641557415584155941560415614156241563415644156541566415674156841569415704157141572415734157441575415764157741578415794158041581415824158341584415854158641587415884158941590415914159241593
  1. /* test.c
  2. *
  3. * Copyright (C) 2006-2022 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL.
  6. *
  7. * wolfSSL is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * wolfSSL is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  20. */
  21. #ifdef HAVE_CONFIG_H
  22. #include <config.h>
  23. #endif
  24. #ifndef WOLFSSL_USER_SETTINGS
  25. #include <wolfssl/options.h>
  26. #endif
  27. #include <wolfssl/wolfcrypt/settings.h>
  28. #include <wolfssl/version.h>
  29. #include <wolfssl/wolfcrypt/wc_port.h>
  30. #ifndef NO_CRYPT_TEST
  31. #if defined(HAVE_STACK_SIZE) && !defined(HAVE_WOLFCRYPT_TEST_OPTIONS)
  32. #define HAVE_WOLFCRYPT_TEST_OPTIONS
  33. #endif
  34. #if defined(HAVE_WOLFCRYPT_TEST_OPTIONS)
  35. #include <wolfssl/ssl.h>
  36. #define err_sys err_sys_remap /* remap err_sys */
  37. #include <wolfssl/test.h>
  38. #undef err_sys
  39. #endif
  40. #if defined(WC_ECC_NONBLOCK) && defined(WOLFSSL_PUBLIC_MP) && \
  41. defined(HAVE_ECC_SIGN) && defined(HAVE_ECC_VERIFY)
  42. #include <stdint.h>
  43. #endif
  44. #if defined(HAVE_STACK_SIZE_VERBOSE)
  45. #ifdef WOLFSSL_TEST_MAX_RELATIVE_STACK_BYTES
  46. static ssize_t max_relative_stack = WOLFSSL_TEST_MAX_RELATIVE_STACK_BYTES;
  47. #else
  48. static ssize_t max_relative_stack = -1;
  49. #endif
  50. #else
  51. #define STACK_SIZE_CHECKPOINT_WITH_MAX_CHECK(max, ...) (__VA_ARGS__, 0)
  52. #define STACK_SIZE_INIT()
  53. #endif
  54. #ifdef WOLFSSL_TRACK_MEMORY_VERBOSE
  55. #ifdef WOLFSSL_TEST_MAX_RELATIVE_HEAP_ALLOCS
  56. static ssize_t max_relative_heap_allocs = WOLFSSL_TEST_MAX_RELATIVE_HEAP_ALLOCS;
  57. #else
  58. static ssize_t max_relative_heap_allocs = -1;
  59. #endif
  60. #ifdef WOLFSSL_TEST_MAX_RELATIVE_HEAP_BYTES
  61. static ssize_t max_relative_heap_bytes = WOLFSSL_TEST_MAX_RELATIVE_HEAP_BYTES;
  62. #else
  63. static ssize_t max_relative_heap_bytes = -1;
  64. #endif
  65. #define PRINT_HEAP_CHECKPOINT() { \
  66. const ssize_t _rha = wolfCrypt_heap_peakAllocs_checkpoint() - heap_baselineAllocs; \
  67. const ssize_t _rhb = wolfCrypt_heap_peakBytes_checkpoint() - heap_baselineBytes; \
  68. printf(" relative heap peak usage: %ld alloc%s, %ld bytes\n", \
  69. (long int)_rha, \
  70. _rha == 1 ? "" : "s", \
  71. (long int)_rhb); \
  72. if ((max_relative_heap_allocs > 0) && (_rha > max_relative_heap_allocs)) \
  73. return err_sys("heap allocs exceed designated max.", -1); \
  74. if ((max_relative_heap_bytes > 0) && (_rhb > max_relative_heap_bytes)) \
  75. return err_sys("heap bytes exceed designated max.", -1); \
  76. heap_baselineAllocs = wolfCrypt_heap_peakAllocs_checkpoint(); \
  77. heap_baselineBytes = wolfCrypt_heap_peakBytes_checkpoint(); \
  78. }
  79. #else
  80. #define PRINT_HEAP_CHECKPOINT()
  81. #endif
  82. #ifdef USE_FLAT_TEST_H
  83. #ifdef HAVE_CONFIG_H
  84. #include "test_paths.h"
  85. #endif
  86. #include "test.h"
  87. #else
  88. #ifdef HAVE_CONFIG_H
  89. #include "wolfcrypt/test/test_paths.h"
  90. #endif
  91. #include "wolfcrypt/test/test.h"
  92. #endif
  93. /* printf mappings */
  94. #if defined(FREESCALE_MQX) || defined(FREESCALE_KSDK_MQX)
  95. #include <mqx.h>
  96. #include <stdlib.h>
  97. /* see wc_port.h for fio.h and nio.h includes */
  98. #elif defined(FREESCALE_KSDK_BM)
  99. #include "fsl_debug_console.h"
  100. #undef printf
  101. #define printf PRINTF
  102. #elif defined(WOLFSSL_APACHE_MYNEWT)
  103. #include <assert.h>
  104. #include <string.h>
  105. #include "sysinit/sysinit.h"
  106. #include "os/os.h"
  107. #ifdef ARCH_sim
  108. #include "mcu/mcu_sim.h"
  109. #endif
  110. #include "os/os_time.h"
  111. #elif defined(WOLFSSL_ESPIDF)
  112. #include <time.h>
  113. #include <sys/time.h>
  114. #elif defined(WOLFSSL_ZEPHYR)
  115. #include <stdio.h>
  116. #define printf printk
  117. #elif defined(MICRIUM)
  118. #include <os.h>
  119. #if (OS_VERSION < 50000)
  120. #include <bsp_ser.h>
  121. void BSP_Ser_Printf (CPU_CHAR* format, ...);
  122. #undef printf
  123. #define printf BSP_Ser_Printf
  124. #else
  125. #include <stdio.h>
  126. #endif
  127. #elif defined(WOLFSSL_PB)
  128. #include <stdarg.h>
  129. int wolfssl_pb_print(const char*, ...);
  130. #undef printf
  131. #define printf wolfssl_pb_print
  132. #elif defined(WOLFSSL_TELIT_M2MB)
  133. #include "wolfssl/wolfcrypt/wc_port.h" /* for m2mb headers */
  134. #include "m2m_log.h" /* for M2M_LOG_INFO - not standard API */
  135. /* remap printf */
  136. #undef printf
  137. #define printf M2M_LOG_INFO
  138. /* OS requires occasional sleep() */
  139. #ifndef TEST_SLEEP_MS
  140. #define TEST_SLEEP_MS 50
  141. #endif
  142. #define TEST_SLEEP() m2mb_os_taskSleep(M2MB_OS_MS2TICKS(TEST_SLEEP_MS))
  143. /* don't use file system for these tests, since ./certs dir isn't loaded */
  144. #undef NO_FILESYSTEM
  145. #define NO_FILESYSTEM
  146. #elif defined(THREADX) && !defined(WOLFSSL_WICED) && \
  147. !defined(THREADX_NO_DC_PRINTF)
  148. #ifndef NETOS
  149. /* since just testing, use THREADX log printf instead (NETOS prototypes
  150. * this elsewhere) */
  151. int dc_log_printf(char*, ...);
  152. #endif
  153. #undef printf
  154. #define printf dc_log_printf
  155. #elif defined(ANDROID)
  156. #ifdef XMALLOC_USER
  157. #include <stdlib.h> /* we're using malloc / free direct here */
  158. #endif
  159. #ifndef STRING_USER
  160. #include <stdio.h>
  161. #endif
  162. #include <android/log.h>
  163. #ifdef ANDROID_V454 /* See fips/android/wolfCrypt_v454_android */
  164. #ifndef NO_FILESYSTEM
  165. #define NO_FILESYSTEM /* Turn off tests that want to call SaveDerAndPem() */
  166. #endif
  167. #else
  168. #define printf(...) \
  169. __android_log_print(ANDROID_LOG_DEBUG, "[WOLFCRYPT]", __VA_ARGS__)
  170. #define fprintf(fp, ...) \
  171. __android_log_print(ANDROID_LOG_DEBUG, "[WOLFCRYPT]", __VA_ARGS__)
  172. #endif
  173. #elif defined(WOLFSSL_DEOS)
  174. #include <printx.h>
  175. #undef printf
  176. #define printf printx
  177. #else
  178. #ifdef XMALLOC_USER
  179. #include <stdlib.h> /* we're using malloc / free direct here */
  180. #endif
  181. #if !defined(STRING_USER) && !defined(WOLFSSL_LINUXKM)
  182. #include <stdio.h>
  183. #endif
  184. #if defined(WOLFSSL_LINUXKM) && !defined(WOLFSSL_LINUXKM_VERBOSE_DEBUG)
  185. #undef printf
  186. #define printf(...) ({})
  187. #endif
  188. /* enable way for customer to override test/bench printf */
  189. #ifdef XPRINTF
  190. #undef printf
  191. #define printf XPRINTF
  192. #elif !defined(printf)
  193. /* arrange for printf() to flush after every message -- this assures
  194. * redirected output (to a log file) records progress right up to the
  195. * moment of a crash/abort(); otherwise anything queued in stdout would
  196. * be lost.
  197. */
  198. #define printf(...) ( printf(__VA_ARGS__), fflush(stdout) )
  199. #endif
  200. #endif
  201. #include <wolfssl/wolfcrypt/memory.h>
  202. #include <wolfssl/wolfcrypt/wc_port.h>
  203. #include <wolfssl/wolfcrypt/logging.h>
  204. #include <wolfssl/wolfcrypt/types.h>
  205. #include <wolfssl/wolfcrypt/asn.h>
  206. #include <wolfssl/wolfcrypt/md2.h>
  207. #include <wolfssl/wolfcrypt/md5.h>
  208. #include <wolfssl/wolfcrypt/md4.h>
  209. #include <wolfssl/wolfcrypt/sha.h>
  210. #include <wolfssl/wolfcrypt/sha256.h>
  211. #include <wolfssl/wolfcrypt/sha512.h>
  212. #include <wolfssl/wolfcrypt/rc2.h>
  213. #include <wolfssl/wolfcrypt/arc4.h>
  214. #if defined(WC_NO_RNG)
  215. #include <wolfssl/wolfcrypt/integer.h>
  216. #else
  217. #include <wolfssl/wolfcrypt/random.h>
  218. #endif
  219. #include <wolfssl/wolfcrypt/coding.h>
  220. #include <wolfssl/wolfcrypt/signature.h>
  221. #include <wolfssl/wolfcrypt/rsa.h>
  222. #include <wolfssl/wolfcrypt/des3.h>
  223. #include <wolfssl/wolfcrypt/aes.h>
  224. #include <wolfssl/wolfcrypt/wc_encrypt.h>
  225. #include <wolfssl/wolfcrypt/cmac.h>
  226. #include <wolfssl/wolfcrypt/siphash.h>
  227. #include <wolfssl/wolfcrypt/poly1305.h>
  228. #include <wolfssl/wolfcrypt/camellia.h>
  229. #include <wolfssl/wolfcrypt/hmac.h>
  230. #include <wolfssl/wolfcrypt/kdf.h>
  231. #include <wolfssl/wolfcrypt/dh.h>
  232. #include <wolfssl/wolfcrypt/dsa.h>
  233. #include <wolfssl/wolfcrypt/srp.h>
  234. #include <wolfssl/wolfcrypt/chacha.h>
  235. #include <wolfssl/wolfcrypt/chacha20_poly1305.h>
  236. #include <wolfssl/wolfcrypt/pwdbased.h>
  237. #include <wolfssl/wolfcrypt/ripemd.h>
  238. #include <wolfssl/wolfcrypt/error-crypt.h>
  239. #ifdef HAVE_ECC
  240. #include <wolfssl/wolfcrypt/ecc.h>
  241. #endif
  242. #ifdef HAVE_CURVE25519
  243. #include <wolfssl/wolfcrypt/curve25519.h>
  244. #endif
  245. #ifdef HAVE_ED25519
  246. #include <wolfssl/wolfcrypt/ed25519.h>
  247. #endif
  248. #ifdef HAVE_CURVE448
  249. #include <wolfssl/wolfcrypt/curve448.h>
  250. #endif
  251. #ifdef HAVE_ED448
  252. #include <wolfssl/wolfcrypt/ed448.h>
  253. #endif
  254. #ifdef WOLFCRYPT_HAVE_ECCSI
  255. #include <wolfssl/wolfcrypt/eccsi.h>
  256. #endif
  257. #ifdef WOLFCRYPT_HAVE_SAKKE
  258. #include <wolfssl/wolfcrypt/sakke.h>
  259. #endif
  260. #if defined(HAVE_BLAKE2) || defined(HAVE_BLAKE2S)
  261. #include <wolfssl/wolfcrypt/blake2.h>
  262. #endif
  263. #ifdef WOLFSSL_SHA3
  264. #include <wolfssl/wolfcrypt/sha3.h>
  265. #endif
  266. #ifdef HAVE_LIBZ
  267. #include <wolfssl/wolfcrypt/compress.h>
  268. #endif
  269. #ifdef HAVE_PKCS7
  270. #include <wolfssl/wolfcrypt/pkcs7.h>
  271. #endif
  272. #ifdef HAVE_FIPS
  273. #include <wolfssl/wolfcrypt/fips_test.h>
  274. #endif
  275. #ifdef HAVE_SELFTEST
  276. #include <wolfssl/wolfcrypt/selftest.h>
  277. #endif
  278. #ifdef WOLFSSL_ASYNC_CRYPT
  279. #include <wolfssl/wolfcrypt/async.h>
  280. #endif
  281. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  282. #include <wolfssl/wolfcrypt/logging.h>
  283. #endif
  284. #ifdef WOLFSSL_CAAM
  285. #include <wolfssl/wolfcrypt/port/caam/wolfcaam.h>
  286. #endif
  287. #ifdef WOLF_CRYPTO_CB
  288. #include <wolfssl/wolfcrypt/cryptocb.h>
  289. #ifdef HAVE_INTEL_QA_SYNC
  290. #include <wolfssl/wolfcrypt/port/intel/quickassist_sync.h>
  291. #endif
  292. #ifdef HAVE_CAVIUM_OCTEON_SYNC
  293. #include <wolfssl/wolfcrypt/port/cavium/cavium_octeon_sync.h>
  294. #endif
  295. #endif
  296. #ifdef _MSC_VER
  297. /* 4996 warning to use MS extensions e.g., strcpy_s instead of strncpy */
  298. #pragma warning(disable: 4996)
  299. #endif
  300. #ifdef OPENSSL_EXTRA
  301. #ifndef WOLFCRYPT_ONLY
  302. #include <wolfssl/openssl/evp.h>
  303. #include <wolfssl/openssl/hmac.h>
  304. #endif
  305. #include <wolfssl/openssl/rand.h>
  306. #include <wolfssl/openssl/aes.h>
  307. #include <wolfssl/openssl/des.h>
  308. #endif
  309. #if defined(NO_FILESYSTEM) || defined(WC_NO_RNG)
  310. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  311. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096)
  312. #define USE_CERT_BUFFERS_2048
  313. #endif
  314. #if !defined(USE_CERT_BUFFERS_256)
  315. #define USE_CERT_BUFFERS_256
  316. #endif
  317. #endif
  318. #if defined(WOLFSSL_CERT_GEN) && (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES))
  319. #define ENABLE_ECC384_CERT_GEN_TEST
  320. #endif
  321. #include <wolfssl/certs_test.h>
  322. #ifdef DEVKITPRO
  323. #include <wiiuse/wpad.h>
  324. #endif
  325. #ifdef WOLFSSL_STATIC_MEMORY
  326. static WOLFSSL_HEAP_HINT* HEAP_HINT;
  327. #else
  328. #define HEAP_HINT NULL
  329. #endif /* WOLFSSL_STATIC_MEMORY */
  330. /* these cases do not have intermediate hashing support */
  331. #if (defined(WOLFSSL_AFALG_XILINX_SHA3) && !defined(WOLFSSL_AFALG_HASH_KEEP)) \
  332. && !defined(WOLFSSL_XILINX_CRYPT)
  333. #define NO_INTM_HASH_TEST
  334. #endif
  335. #if defined(WOLFSSL_RENESAS_TSIP) || defined(WOLFSSL_RENESAS_SCEPROTECT) || \
  336. defined(WOLFSSL_SECO_CAAM)
  337. #define HASH_SIZE_LIMIT
  338. #endif
  339. #if defined(WOLFSSL_CERT_GEN) && (!defined(NO_RSA) || defined(HAVE_ECC)) || \
  340. (defined(WOLFSSL_TEST_CERT) && (defined(HAVE_ED25519) || defined(HAVE_ED448)))
  341. static void initDefaultName(void);
  342. #endif
  343. /* for async devices */
  344. #ifdef WOLFSSL_CAAM_DEVID
  345. static int devId = WOLFSSL_CAAM_DEVID;
  346. #else
  347. static int devId = INVALID_DEVID;
  348. #endif
  349. #ifdef HAVE_WNR
  350. const char* wnrConfigFile = "wnr-example.conf";
  351. #endif
  352. #define TEST_STRING "Everyone gets Friday off."
  353. #define TEST_STRING_SZ 25
  354. typedef struct testVector {
  355. const char* input;
  356. const char* output;
  357. size_t inLen;
  358. size_t outLen;
  359. } testVector;
  360. #ifndef WOLFSSL_TEST_SUBROUTINE
  361. #define WOLFSSL_TEST_SUBROUTINE
  362. #endif
  363. PRAGMA_GCC("GCC diagnostic ignored \"-Wunused-function\"")
  364. WOLFSSL_TEST_SUBROUTINE int error_test(void);
  365. WOLFSSL_TEST_SUBROUTINE int base64_test(void);
  366. WOLFSSL_TEST_SUBROUTINE int base16_test(void);
  367. WOLFSSL_TEST_SUBROUTINE int asn_test(void);
  368. WOLFSSL_TEST_SUBROUTINE int md2_test(void);
  369. WOLFSSL_TEST_SUBROUTINE int md5_test(void);
  370. WOLFSSL_TEST_SUBROUTINE int md4_test(void);
  371. WOLFSSL_TEST_SUBROUTINE int sha_test(void);
  372. WOLFSSL_TEST_SUBROUTINE int sha224_test(void);
  373. WOLFSSL_TEST_SUBROUTINE int sha256_test(void);
  374. WOLFSSL_TEST_SUBROUTINE int sha512_test(void);
  375. WOLFSSL_TEST_SUBROUTINE int sha384_test(void);
  376. WOLFSSL_TEST_SUBROUTINE int sha3_test(void);
  377. WOLFSSL_TEST_SUBROUTINE int shake128_test(void);
  378. WOLFSSL_TEST_SUBROUTINE int shake256_test(void);
  379. WOLFSSL_TEST_SUBROUTINE int hash_test(void);
  380. WOLFSSL_TEST_SUBROUTINE int hmac_md5_test(void);
  381. WOLFSSL_TEST_SUBROUTINE int hmac_sha_test(void);
  382. WOLFSSL_TEST_SUBROUTINE int hmac_sha224_test(void);
  383. WOLFSSL_TEST_SUBROUTINE int hmac_sha256_test(void);
  384. WOLFSSL_TEST_SUBROUTINE int hmac_sha384_test(void);
  385. WOLFSSL_TEST_SUBROUTINE int hmac_sha512_test(void);
  386. WOLFSSL_TEST_SUBROUTINE int hmac_sha3_test(void);
  387. #if defined(HAVE_HKDF) && !defined(NO_HMAC)
  388. /* hkdf_test has issue with WOLFSSL_TEST_SUBROUTINE set on Xilinx with afalg */
  389. static int hkdf_test(void);
  390. #endif
  391. WOLFSSL_TEST_SUBROUTINE int sshkdf_test(void);
  392. #ifdef WOLFSSL_TLS13
  393. WOLFSSL_TEST_SUBROUTINE int tls13_kdf_test(void);
  394. #endif
  395. WOLFSSL_TEST_SUBROUTINE int x963kdf_test(void);
  396. WOLFSSL_TEST_SUBROUTINE int arc4_test(void);
  397. #ifdef WC_RC2
  398. WOLFSSL_TEST_SUBROUTINE int rc2_test(void);
  399. #endif
  400. WOLFSSL_TEST_SUBROUTINE int chacha_test(void);
  401. WOLFSSL_TEST_SUBROUTINE int XChaCha_test(void);
  402. WOLFSSL_TEST_SUBROUTINE int chacha20_poly1305_aead_test(void);
  403. WOLFSSL_TEST_SUBROUTINE int XChaCha20Poly1305_test(void);
  404. WOLFSSL_TEST_SUBROUTINE int des_test(void);
  405. WOLFSSL_TEST_SUBROUTINE int des3_test(void);
  406. WOLFSSL_TEST_SUBROUTINE int aes_test(void);
  407. WOLFSSL_TEST_SUBROUTINE int aes192_test(void);
  408. WOLFSSL_TEST_SUBROUTINE int aes256_test(void);
  409. WOLFSSL_TEST_SUBROUTINE int aesofb_test(void);
  410. WOLFSSL_TEST_SUBROUTINE int cmac_test(void);
  411. #if defined(WOLFSSL_SIPHASH)
  412. WOLFSSL_TEST_SUBROUTINE int siphash_test(void);
  413. #endif
  414. WOLFSSL_TEST_SUBROUTINE int poly1305_test(void);
  415. WOLFSSL_TEST_SUBROUTINE int aesgcm_test(void);
  416. WOLFSSL_TEST_SUBROUTINE int aesgcm_default_test(void);
  417. WOLFSSL_TEST_SUBROUTINE int gmac_test(void);
  418. WOLFSSL_TEST_SUBROUTINE int aesccm_test(void);
  419. WOLFSSL_TEST_SUBROUTINE int aeskeywrap_test(void);
  420. WOLFSSL_TEST_SUBROUTINE int camellia_test(void);
  421. #ifdef WC_RSA_NO_PADDING
  422. WOLFSSL_TEST_SUBROUTINE int rsa_no_pad_test(void);
  423. #endif
  424. WOLFSSL_TEST_SUBROUTINE int rsa_test(void);
  425. WOLFSSL_TEST_SUBROUTINE int dh_test(void);
  426. WOLFSSL_TEST_SUBROUTINE int dsa_test(void);
  427. WOLFSSL_TEST_SUBROUTINE int srp_test(void);
  428. #ifndef WC_NO_RNG
  429. WOLFSSL_TEST_SUBROUTINE int random_test(void);
  430. #endif /* WC_NO_RNG */
  431. WOLFSSL_TEST_SUBROUTINE int pwdbased_test(void);
  432. WOLFSSL_TEST_SUBROUTINE int ripemd_test(void);
  433. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  434. WOLFSSL_TEST_SUBROUTINE int openssl_test(void); /* test mini api */
  435. WOLFSSL_TEST_SUBROUTINE int openssl_pkey0_test(void);
  436. WOLFSSL_TEST_SUBROUTINE int openssl_pkey1_test(void);
  437. WOLFSSL_TEST_SUBROUTINE int openSSL_evpMD_test(void);
  438. WOLFSSL_TEST_SUBROUTINE int openssl_evpSig_test(void);
  439. #endif
  440. WOLFSSL_TEST_SUBROUTINE int pbkdf1_test(void);
  441. WOLFSSL_TEST_SUBROUTINE int pkcs12_test(void);
  442. WOLFSSL_TEST_SUBROUTINE int pbkdf2_test(void);
  443. WOLFSSL_TEST_SUBROUTINE int scrypt_test(void);
  444. #ifdef HAVE_ECC
  445. WOLFSSL_TEST_SUBROUTINE int ecc_test(void);
  446. #if defined(HAVE_ECC_ENCRYPT) && defined(HAVE_AES_CBC) && \
  447. (defined(WOLFSSL_AES_128) || defined(WOLFSSL_AES_256))
  448. WOLFSSL_TEST_SUBROUTINE int ecc_encrypt_test(void);
  449. #endif
  450. #if defined(USE_CERT_BUFFERS_256) && !defined(WOLFSSL_ATECC508A) && \
  451. !defined(WOLFSSL_ATECC608A) && !defined(NO_ECC256) && \
  452. defined(HAVE_ECC_VERIFY) && defined(HAVE_ECC_SIGN) && \
  453. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  454. /* skip for ATECC508/608A, cannot import private key buffers */
  455. WOLFSSL_TEST_SUBROUTINE int ecc_test_buffers(void);
  456. #endif
  457. #endif
  458. #ifdef HAVE_CURVE25519
  459. WOLFSSL_TEST_SUBROUTINE int curve25519_test(void);
  460. #endif
  461. #ifdef HAVE_ED25519
  462. WOLFSSL_TEST_SUBROUTINE int ed25519_test(void);
  463. #endif
  464. #ifdef HAVE_CURVE448
  465. WOLFSSL_TEST_SUBROUTINE int curve448_test(void);
  466. #endif
  467. #ifdef HAVE_ED448
  468. WOLFSSL_TEST_SUBROUTINE int ed448_test(void);
  469. #endif
  470. #ifdef WOLFCRYPT_HAVE_ECCSI
  471. WOLFSSL_TEST_SUBROUTINE int eccsi_test(void);
  472. #endif
  473. #ifdef WOLFCRYPT_HAVE_SAKKE
  474. WOLFSSL_TEST_SUBROUTINE int sakke_test(void);
  475. #endif
  476. #ifdef HAVE_BLAKE2
  477. WOLFSSL_TEST_SUBROUTINE int blake2b_test(void);
  478. #endif
  479. #ifdef HAVE_BLAKE2S
  480. WOLFSSL_TEST_SUBROUTINE int blake2s_test(void);
  481. #endif
  482. #ifdef HAVE_LIBZ
  483. WOLFSSL_TEST_SUBROUTINE int compress_test(void);
  484. #endif
  485. #ifdef HAVE_PKCS7
  486. #ifndef NO_PKCS7_ENCRYPTED_DATA
  487. WOLFSSL_TEST_SUBROUTINE int pkcs7encrypted_test(void);
  488. #endif
  489. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  490. WOLFSSL_TEST_SUBROUTINE int pkcs7compressed_test(void);
  491. #endif
  492. WOLFSSL_TEST_SUBROUTINE int pkcs7signed_test(void);
  493. WOLFSSL_TEST_SUBROUTINE int pkcs7enveloped_test(void);
  494. #if defined(HAVE_AESGCM) || defined(HAVE_AESCCM)
  495. WOLFSSL_TEST_SUBROUTINE int pkcs7authenveloped_test(void);
  496. #endif
  497. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  498. WOLFSSL_TEST_SUBROUTINE int pkcs7callback_test(byte* cert, word32 certSz, byte* key,
  499. word32 keySz);
  500. #endif
  501. #endif
  502. #if !defined(NO_ASN_TIME) && !defined(NO_RSA) && defined(WOLFSSL_TEST_CERT) && \
  503. !defined(NO_FILESYSTEM)
  504. WOLFSSL_TEST_SUBROUTINE int cert_test(void);
  505. #endif
  506. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_TEST_CERT) && \
  507. !defined(NO_FILESYSTEM) && defined(WOLFSSL_CERT_GEN)
  508. WOLFSSL_TEST_SUBROUTINE int certext_test(void);
  509. #endif
  510. #if defined(WOLFSSL_CERT_GEN_CACHE) && defined(WOLFSSL_TEST_CERT) && \
  511. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  512. WOLFSSL_TEST_SUBROUTINE int decodedCertCache_test(void);
  513. #endif
  514. WOLFSSL_TEST_SUBROUTINE int memory_test(void);
  515. #ifdef HAVE_VALGRIND
  516. WOLFSSL_TEST_SUBROUTINE int mp_test(void);
  517. #endif
  518. #if defined(WOLFSSL_PUBLIC_MP) && defined(WOLFSSL_KEY_GEN)
  519. WOLFSSL_TEST_SUBROUTINE int prime_test(void);
  520. #endif
  521. #if defined(ASN_BER_TO_DER) && \
  522. (defined(WOLFSSL_TEST_CERT) || defined(OPENSSL_EXTRA) || \
  523. defined(OPENSSL_EXTRA_X509_SMALL))
  524. WOLFSSL_TEST_SUBROUTINE int berder_test(void);
  525. #endif
  526. WOLFSSL_TEST_SUBROUTINE int logging_test(void);
  527. #if !defined(NO_ASN) && !defined(NO_ASN_TIME)
  528. WOLFSSL_TEST_SUBROUTINE int time_test(void);
  529. #endif
  530. WOLFSSL_TEST_SUBROUTINE int mutex_test(void);
  531. #if defined(USE_WOLFSSL_MEMORY) && !defined(FREERTOS)
  532. WOLFSSL_TEST_SUBROUTINE int memcb_test(void);
  533. #endif
  534. #ifdef WOLFSSL_IMX6_CAAM_BLOB
  535. WOLFSSL_TEST_SUBROUTINE int blob_test(void);
  536. #endif
  537. #ifdef WOLF_CRYPTO_CB
  538. WOLFSSL_TEST_SUBROUTINE int cryptocb_test(void);
  539. #endif
  540. #ifdef WOLFSSL_CERT_PIV
  541. WOLFSSL_TEST_SUBROUTINE int certpiv_test(void);
  542. #endif
  543. #ifdef WOLFSSL_AES_SIV
  544. WOLFSSL_TEST_SUBROUTINE int aes_siv_test(void);
  545. #endif
  546. /* General big buffer size for many tests. */
  547. #define FOURK_BUF 4096
  548. #define ERROR_OUT(err, eLabel) do { ret = (err); goto eLabel; } while (0)
  549. #ifdef HAVE_STACK_SIZE
  550. static THREAD_RETURN err_sys(const char* msg, int es)
  551. #else
  552. static int err_sys(const char* msg, int es)
  553. #endif
  554. {
  555. (void)msg;
  556. (void)es;
  557. #ifdef WOLFSSL_LINUXKM
  558. lkm_printf("%s error = %d\n", msg, es);
  559. EXIT_TEST(es);
  560. #else
  561. printf("%s error = %d\n", msg, es);
  562. EXIT_TEST(-1);
  563. #endif
  564. }
  565. #ifndef HAVE_WOLFCRYPT_TEST_OPTIONS
  566. /* func_args from test.h, so don't have to pull in other stuff */
  567. typedef struct func_args {
  568. int argc;
  569. char** argv;
  570. int return_code;
  571. } func_args;
  572. #endif /* !HAVE_WOLFCRYPT_TEST_OPTIONS */
  573. #if defined(HAVE_FIPS) && !defined(WOLFSSL_LINUXKM)
  574. static void myFipsCb(int ok, int err, const char* hash)
  575. {
  576. printf("in my Fips callback, ok = %d, err = %d\n", ok, err);
  577. printf("message = %s\n", wc_GetErrorString(err));
  578. printf("hash = %s\n", hash);
  579. if (err == IN_CORE_FIPS_E) {
  580. printf("In core integrity hash check failure, copy above hash\n");
  581. printf("into verifyCore[] in fips_test.c and rebuild\n");
  582. }
  583. }
  584. #endif /* HAVE_FIPS && !WOLFSSL_LINUXKM */
  585. #ifdef WOLFSSL_STATIC_MEMORY
  586. #ifdef BENCH_EMBEDDED
  587. static byte gTestMemory[14000];
  588. #elif defined(WOLFSSL_CERT_EXT)
  589. static byte gTestMemory[140000];
  590. #elif (defined(WOLFSSL_SP_MATH_ALL) || defined(USE_FAST_MATH)) && \
  591. !defined(ALT_ECC_SIZE)
  592. static byte gTestMemory[160000];
  593. #else
  594. static byte gTestMemory[80000];
  595. #endif
  596. #endif
  597. #ifdef WOLFSSL_PB
  598. static int wolfssl_pb_print(const char* msg, ...)
  599. {
  600. int ret;
  601. va_list args;
  602. char tmpBuf[80];
  603. va_start(args, msg);
  604. ret = vsprint(tmpBuf, msg, args);
  605. va_end(args);
  606. fnDumpStringToSystemLog(tmpBuf);
  607. return ret;
  608. }
  609. #endif /* WOLFSSL_PB */
  610. /* optional macro to add sleep between tests */
  611. #ifndef TEST_SLEEP
  612. #define TEST_SLEEP()
  613. #else
  614. #define TEST_PASS test_pass
  615. #include <stdarg.h> /* for var args */
  616. static WC_INLINE void test_pass(const char* fmt, ...)
  617. {
  618. va_list args;
  619. va_start(args, fmt);
  620. STACK_SIZE_CHECKPOINT_WITH_MAX_CHECK(max_relative_stack, vprintf(fmt, args));
  621. va_end(args);
  622. PRINT_HEAP_CHECKPOINT();
  623. TEST_SLEEP();
  624. ASSERT_RESTORED_VECTOR_REGISTERS(exit(1););
  625. }
  626. #endif
  627. /* set test pass output to printf if not overriden */
  628. #ifndef TEST_PASS
  629. /* redirect to printf */
  630. #define TEST_PASS(...) { \
  631. if (STACK_SIZE_CHECKPOINT_WITH_MAX_CHECK \
  632. (max_relative_stack, printf(__VA_ARGS__)) < 0) { \
  633. return err_sys("post-test check failed", -1); \
  634. } \
  635. PRINT_HEAP_CHECKPOINT(); \
  636. ASSERT_RESTORED_VECTOR_REGISTERS(exit(1);); \
  637. }
  638. #endif
  639. #ifdef HAVE_STACK_SIZE
  640. THREAD_RETURN WOLFSSL_THREAD wolfcrypt_test(void* args)
  641. #else
  642. int wolfcrypt_test(void* args)
  643. #endif
  644. {
  645. int ret;
  646. #ifdef WOLFSSL_TRACK_MEMORY_VERBOSE
  647. long heap_baselineAllocs, heap_baselineBytes;
  648. #endif
  649. STACK_SIZE_INIT();
  650. #ifdef WOLFSSL_TRACK_MEMORY_VERBOSE
  651. (void)wolfCrypt_heap_peakAllocs_checkpoint();
  652. heap_baselineAllocs = wolfCrypt_heap_peakAllocs_checkpoint();
  653. (void)wolfCrypt_heap_peakBytes_checkpoint();
  654. heap_baselineBytes = wolfCrypt_heap_peakBytes_checkpoint();
  655. #endif
  656. printf("------------------------------------------------------------------------------\n");
  657. printf(" wolfSSL version %s\n", LIBWOLFSSL_VERSION_STRING);
  658. printf("------------------------------------------------------------------------------\n");
  659. if (args) {
  660. #ifdef HAVE_WOLFCRYPT_TEST_OPTIONS
  661. int ch;
  662. #endif
  663. ((func_args*)args)->return_code = -1; /* error state */
  664. #ifdef HAVE_WOLFCRYPT_TEST_OPTIONS
  665. while ((ch = mygetopt(((func_args*)args)->argc, ((func_args*)args)->argv, "s:m:a:h")) != -1) {
  666. switch(ch) {
  667. case 's':
  668. #ifdef HAVE_STACK_SIZE_VERBOSE
  669. max_relative_stack = (ssize_t)atoi(myoptarg);
  670. break;
  671. #else
  672. return err_sys("-s (max relative stack bytes) requires HAVE_STACK_SIZE_VERBOSE (--enable-stacksize=verbose).", -1);
  673. #endif
  674. case 'm':
  675. #ifdef WOLFSSL_TRACK_MEMORY_VERBOSE
  676. max_relative_heap_bytes = (ssize_t)atoi(myoptarg);
  677. break;
  678. #else
  679. return err_sys("-m (max relative heap memory bytes) requires WOLFSSL_TRACK_MEMORY_VERBOSE (--enable-trackmemory=verbose).", -1);
  680. #endif
  681. case 'a':
  682. #ifdef WOLFSSL_TRACK_MEMORY_VERBOSE
  683. max_relative_heap_allocs = (ssize_t)atoi(myoptarg);
  684. break;
  685. #else
  686. return err_sys("-a (max relative heap allocs) requires WOLFSSL_TRACK_MEMORY_VERBOSE (--enable-trackmemory=verbose).", -1);
  687. #endif
  688. case 'h':
  689. return err_sys("\
  690. options: [-s max_relative_stack_bytes] [-m max_relative_heap_memory_bytes]\n\
  691. [-a max_relative_heap_allocs] [-h]\n", 0);
  692. default:
  693. return err_sys("unknown test option. try -h.", -1);
  694. }
  695. }
  696. #endif
  697. }
  698. #ifdef WOLFSSL_STATIC_MEMORY
  699. if (wc_LoadStaticMemory(&HEAP_HINT, gTestMemory, sizeof(gTestMemory),
  700. WOLFMEM_GENERAL, 1) != 0) {
  701. printf("unable to load static memory.\n");
  702. return(EXIT_FAILURE);
  703. }
  704. #endif
  705. #if defined(DEBUG_WOLFSSL) && !defined(HAVE_VALGRIND)
  706. wolfSSL_Debugging_ON();
  707. #endif
  708. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  709. wc_SetLoggingHeap(HEAP_HINT);
  710. #endif
  711. #if defined(HAVE_FIPS) && !defined(WOLFSSL_LINUXKM)
  712. wolfCrypt_SetCb_fips(myFipsCb);
  713. #endif
  714. #if !defined(NO_BIG_INT)
  715. if (CheckCtcSettings() != 1) {
  716. printf("Sizeof mismatch (build) %x != (run) %lx\n",
  717. CTC_SETTINGS, (unsigned long)CheckRunTimeSettings());
  718. return err_sys("Build vs runtime math mismatch\n", -1000);
  719. }
  720. #if defined(USE_FAST_MATH) && \
  721. (!defined(NO_RSA) || !defined(NO_DH) || defined(HAVE_ECC))
  722. if (CheckFastMathSettings() != 1)
  723. return err_sys("Build vs runtime fastmath FP_MAX_BITS mismatch\n",
  724. -1001);
  725. #endif /* USE_FAST_MATH */
  726. #endif /* !NO_BIG_INT */
  727. #if defined(WOLFSSL_CERT_GEN) && (!defined(NO_RSA) || defined(HAVE_ECC)) || \
  728. (defined(WOLFSSL_TEST_CERT) && (defined(HAVE_ED25519) || defined(HAVE_ED448)))
  729. initDefaultName();
  730. #endif
  731. #ifdef WOLFSSL_ASYNC_CRYPT
  732. ret = wolfAsync_DevOpen(&devId);
  733. if (ret < 0) {
  734. printf("Async device open failed\nRunning without async\n");
  735. }
  736. #else
  737. (void)devId;
  738. #endif /* WOLFSSL_ASYNC_CRYPT */
  739. #ifdef WOLF_CRYPTO_CB
  740. #ifdef HAVE_INTEL_QA_SYNC
  741. devId = wc_CryptoCb_InitIntelQa();
  742. if (INVALID_DEVID == devId) {
  743. printf("Couldn't init the Intel QA\n");
  744. }
  745. #endif
  746. #ifdef HAVE_CAVIUM_OCTEON_SYNC
  747. devId = wc_CryptoCb_InitOcteon();
  748. if (INVALID_DEVID == devId) {
  749. printf("Couldn't init the Cavium Octeon\n");
  750. }
  751. #endif
  752. #endif
  753. #ifdef HAVE_SELFTEST
  754. if ( (ret = wolfCrypt_SelfTest()) != 0)
  755. return err_sys("CAVP selftest failed!\n", ret);
  756. else
  757. TEST_PASS("CAVP selftest passed!\n");
  758. #endif
  759. if ( (ret = error_test()) != 0)
  760. return err_sys("error test failed!\n", ret);
  761. else
  762. TEST_PASS("error test passed!\n");
  763. if ( (ret = memory_test()) != 0)
  764. return err_sys("MEMORY test failed!\n", ret);
  765. else
  766. TEST_PASS("MEMORY test passed!\n");
  767. #ifndef NO_CODING
  768. if ( (ret = base64_test()) != 0)
  769. return err_sys("base64 test failed!\n", ret);
  770. else
  771. TEST_PASS("base64 test passed!\n");
  772. #ifdef WOLFSSL_BASE16
  773. if ( (ret = base16_test()) != 0)
  774. return err_sys("base16 test failed!\n", ret);
  775. else
  776. TEST_PASS("base16 test passed!\n");
  777. #endif
  778. #endif /* !NO_CODING */
  779. #ifndef NO_ASN
  780. if ( (ret = asn_test()) != 0)
  781. return err_sys("asn test failed!\n", ret);
  782. else
  783. TEST_PASS("asn test passed!\n");
  784. #endif
  785. #ifndef WC_NO_RNG
  786. if ( (ret = random_test()) != 0)
  787. return err_sys("RANDOM test failed!\n", ret);
  788. else
  789. TEST_PASS("RANDOM test passed!\n");
  790. #endif /* WC_NO_RNG */
  791. #ifndef NO_MD5
  792. if ( (ret = md5_test()) != 0)
  793. return err_sys("MD5 test failed!\n", ret);
  794. else
  795. TEST_PASS("MD5 test passed!\n");
  796. #endif
  797. #ifdef WOLFSSL_MD2
  798. if ( (ret = md2_test()) != 0)
  799. return err_sys("MD2 test failed!\n", ret);
  800. else
  801. TEST_PASS("MD2 test passed!\n");
  802. #endif
  803. #ifndef NO_MD4
  804. if ( (ret = md4_test()) != 0)
  805. return err_sys("MD4 test failed!\n", ret);
  806. else
  807. TEST_PASS("MD4 test passed!\n");
  808. #endif
  809. #ifndef NO_SHA
  810. if ( (ret = sha_test()) != 0)
  811. return err_sys("SHA test failed!\n", ret);
  812. else
  813. TEST_PASS("SHA test passed!\n");
  814. #endif
  815. #ifdef WOLFSSL_SHA224
  816. if ( (ret = sha224_test()) != 0)
  817. return err_sys("SHA-224 test failed!\n", ret);
  818. else
  819. TEST_PASS("SHA-224 test passed!\n");
  820. #endif
  821. #ifndef NO_SHA256
  822. if ( (ret = sha256_test()) != 0)
  823. return err_sys("SHA-256 test failed!\n", ret);
  824. else
  825. TEST_PASS("SHA-256 test passed!\n");
  826. #endif
  827. #ifdef WOLFSSL_SHA384
  828. if ( (ret = sha384_test()) != 0)
  829. return err_sys("SHA-384 test failed!\n", ret);
  830. else
  831. TEST_PASS("SHA-384 test passed!\n");
  832. #endif
  833. #ifdef WOLFSSL_SHA512
  834. if ( (ret = sha512_test()) != 0)
  835. return err_sys("SHA-512 test failed!\n", ret);
  836. else
  837. TEST_PASS("SHA-512 test passed!\n");
  838. #endif
  839. #ifdef WOLFSSL_SHA3
  840. if ( (ret = sha3_test()) != 0)
  841. return err_sys("SHA-3 test failed!\n", ret);
  842. else
  843. TEST_PASS("SHA-3 test passed!\n");
  844. #endif
  845. #ifdef WOLFSSL_SHAKE128
  846. if ( (ret = shake128_test()) != 0)
  847. return err_sys("SHAKE128 test failed!\n", ret);
  848. else
  849. TEST_PASS("SHAKE128 test passed!\n");
  850. #endif
  851. #ifdef WOLFSSL_SHAKE256
  852. if ( (ret = shake256_test()) != 0)
  853. return err_sys("SHAKE256 test failed!\n", ret);
  854. else
  855. TEST_PASS("SHAKE256 test passed!\n");
  856. #endif
  857. #ifndef NO_HASH_WRAPPER
  858. if ( (ret = hash_test()) != 0)
  859. return err_sys("Hash test failed!\n", ret);
  860. else
  861. TEST_PASS("Hash test passed!\n");
  862. #endif
  863. #ifdef WOLFSSL_RIPEMD
  864. if ( (ret = ripemd_test()) != 0)
  865. return err_sys("RIPEMD test failed!\n", ret);
  866. else
  867. TEST_PASS("RIPEMD test passed!\n");
  868. #endif
  869. #ifdef HAVE_BLAKE2
  870. if ( (ret = blake2b_test()) != 0)
  871. return err_sys("BLAKE2b test failed!\n", ret);
  872. else
  873. TEST_PASS("BLAKE2b test passed!\n");
  874. #endif
  875. #ifdef HAVE_BLAKE2S
  876. if ( (ret = blake2s_test()) != 0)
  877. return err_sys("BLAKE2s test failed!\n", ret);
  878. else
  879. TEST_PASS("BLAKE2s test passed!\n");
  880. #endif
  881. #ifndef NO_HMAC
  882. #if !defined(NO_MD5) && !(defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) \
  883. && (HAVE_FIPS_VERSION >= 5))
  884. if ( (ret = hmac_md5_test()) != 0)
  885. return err_sys("HMAC-MD5 test failed!\n", ret);
  886. else
  887. TEST_PASS("HMAC-MD5 test passed!\n");
  888. #endif
  889. #ifndef NO_SHA
  890. if ( (ret = hmac_sha_test()) != 0)
  891. return err_sys("HMAC-SHA test failed!\n", ret);
  892. else
  893. TEST_PASS("HMAC-SHA test passed!\n");
  894. #endif
  895. #ifdef WOLFSSL_SHA224
  896. if ( (ret = hmac_sha224_test()) != 0)
  897. return err_sys("HMAC-SHA224 test failed!\n", ret);
  898. else
  899. TEST_PASS("HMAC-SHA224 test passed!\n");
  900. #endif
  901. #ifndef NO_SHA256
  902. if ( (ret = hmac_sha256_test()) != 0)
  903. return err_sys("HMAC-SHA256 test failed!\n", ret);
  904. else
  905. TEST_PASS("HMAC-SHA256 test passed!\n");
  906. #endif
  907. #ifdef WOLFSSL_SHA384
  908. if ( (ret = hmac_sha384_test()) != 0)
  909. return err_sys("HMAC-SHA384 test failed!\n", ret);
  910. else
  911. TEST_PASS("HMAC-SHA384 test passed!\n");
  912. #endif
  913. #ifdef WOLFSSL_SHA512
  914. if ( (ret = hmac_sha512_test()) != 0)
  915. return err_sys("HMAC-SHA512 test failed!\n", ret);
  916. else
  917. TEST_PASS("HMAC-SHA512 test passed!\n");
  918. #endif
  919. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA3) && \
  920. !defined(WOLFSSL_NOSHA3_224) && !defined(WOLFSSL_NOSHA3_256) && \
  921. !defined(WOLFSSL_NOSHA3_384) && !defined(WOLFSSL_NOSHA3_512)
  922. if ( (ret = hmac_sha3_test()) != 0)
  923. return err_sys("HMAC-SHA3 test failed!\n", ret);
  924. else
  925. TEST_PASS("HMAC-SHA3 test passed!\n");
  926. #endif
  927. #if defined(HAVE_HKDF) && !defined(NO_HMAC)
  928. PRIVATE_KEY_UNLOCK();
  929. if ( (ret = hkdf_test()) != 0)
  930. return err_sys("HMAC-KDF test failed!\n", ret);
  931. else
  932. TEST_PASS("HMAC-KDF test passed!\n");
  933. PRIVATE_KEY_LOCK();
  934. #endif
  935. #endif /* !NO_HMAC */
  936. #ifdef WOLFSSL_WOLFSSH
  937. PRIVATE_KEY_UNLOCK();
  938. if ( (ret = sshkdf_test()) != 0)
  939. return err_sys("SSH-KDF test failed!\n", ret);
  940. else
  941. TEST_PASS("SSH-KDF test passed!\n");
  942. PRIVATE_KEY_LOCK();
  943. #endif /* WOLFSSL_WOLFSSH */
  944. #ifdef WOLFSSL_TLS13
  945. PRIVATE_KEY_UNLOCK();
  946. if ( (ret = tls13_kdf_test()) != 0)
  947. return err_sys("TLSv1.3 KDF test failed!\n", ret);
  948. else
  949. TEST_PASS("TLSv1.3 KDF test passed!\n");
  950. PRIVATE_KEY_LOCK();
  951. #endif /* WOLFSSL_TLS13 */
  952. #if defined(HAVE_X963_KDF) && defined(HAVE_ECC)
  953. if ( (ret = x963kdf_test()) != 0)
  954. return err_sys("X963-KDF test failed!\n", ret);
  955. else
  956. TEST_PASS("X963-KDF test passed!\n");
  957. #endif
  958. #if defined(HAVE_AESGCM) && defined(WOLFSSL_AES_128) && \
  959. !defined(WOLFSSL_AFALG_XILINX_AES) && !defined(WOLFSSL_XILINX_CRYPT)
  960. if ( (ret = gmac_test()) != 0)
  961. return err_sys("GMAC test failed!\n", ret);
  962. else
  963. TEST_PASS("GMAC test passed!\n");
  964. #endif
  965. #ifdef WC_RC2
  966. if ( (ret = rc2_test()) != 0)
  967. return err_sys("RC2 test failed!\n", ret);
  968. else
  969. TEST_PASS("RC2 test passed!\n");
  970. #endif
  971. #ifndef NO_RC4
  972. if ( (ret = arc4_test()) != 0)
  973. return err_sys("ARC4 test failed!\n", ret);
  974. else
  975. TEST_PASS("ARC4 test passed!\n");
  976. #endif
  977. #ifdef HAVE_CHACHA
  978. if ( (ret = chacha_test()) != 0)
  979. return err_sys("Chacha test failed!\n", ret);
  980. else
  981. TEST_PASS("Chacha test passed!\n");
  982. #endif
  983. #ifdef HAVE_XCHACHA
  984. if ( (ret = XChaCha_test()) != 0)
  985. return err_sys("XChacha test failed!\n", ret);
  986. else
  987. TEST_PASS("XChacha test passed!\n");
  988. #endif
  989. #ifdef HAVE_POLY1305
  990. if ( (ret = poly1305_test()) != 0)
  991. return err_sys("POLY1305 test failed!\n", ret);
  992. else
  993. TEST_PASS("POLY1305 test passed!\n");
  994. #endif
  995. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  996. if ( (ret = chacha20_poly1305_aead_test()) != 0)
  997. return err_sys("ChaCha20-Poly1305 AEAD test failed!\n", ret);
  998. else
  999. TEST_PASS("ChaCha20-Poly1305 AEAD test passed!\n");
  1000. #endif
  1001. #if defined(HAVE_XCHACHA) && defined(HAVE_POLY1305)
  1002. if ( (ret = XChaCha20Poly1305_test()) != 0)
  1003. return err_sys("XChaCha20-Poly1305 AEAD test failed!\n", ret);
  1004. else
  1005. TEST_PASS("XChaCha20-Poly1305 AEAD test passed!\n");
  1006. #endif
  1007. #ifndef NO_DES3
  1008. if ( (ret = des_test()) != 0)
  1009. return err_sys("DES test failed!\n", ret);
  1010. else
  1011. TEST_PASS("DES test passed!\n");
  1012. #endif
  1013. #ifndef NO_DES3
  1014. if ( (ret = des3_test()) != 0)
  1015. return err_sys("DES3 test failed!\n", ret);
  1016. else
  1017. TEST_PASS("DES3 test passed!\n");
  1018. #endif
  1019. #ifndef NO_AES
  1020. if ( (ret = aes_test()) != 0)
  1021. return err_sys("AES test failed!\n", ret);
  1022. else
  1023. TEST_PASS("AES test passed!\n");
  1024. #ifdef WOLFSSL_AES_192
  1025. if ( (ret = aes192_test()) != 0)
  1026. return err_sys("AES192 test failed!\n", ret);
  1027. else
  1028. TEST_PASS("AES192 test passed!\n");
  1029. #endif
  1030. #ifdef WOLFSSL_AES_256
  1031. if ( (ret = aes256_test()) != 0)
  1032. return err_sys("AES256 test failed!\n", ret);
  1033. else
  1034. TEST_PASS("AES256 test passed!\n");
  1035. #endif
  1036. #ifdef WOLFSSL_AES_OFB
  1037. if ( (ret = aesofb_test()) != 0)
  1038. return err_sys("AES-OFB test failed!\n", ret);
  1039. else
  1040. TEST_PASS("AESOFB test passed!\n");
  1041. #endif
  1042. #ifdef HAVE_AESGCM
  1043. #if !defined(WOLFSSL_AFALG) && !defined(WOLFSSL_DEVCRYPTO)
  1044. if ( (ret = aesgcm_test()) != 0)
  1045. return err_sys("AES-GCM test failed!\n", ret);
  1046. #endif
  1047. #if !defined(WOLFSSL_AFALG_XILINX_AES) && !defined(WOLFSSL_XILINX_CRYPT) && \
  1048. !defined(WOLFSSL_KCAPI_AES) && !(defined(WOLF_CRYPTO_CB) && \
  1049. (defined(HAVE_INTEL_QA_SYNC) || defined(HAVE_CAVIUM_OCTEON_SYNC)))
  1050. if ((ret = aesgcm_default_test()) != 0) {
  1051. return err_sys("AES-GCM test failed!\n", ret);
  1052. }
  1053. #endif
  1054. if (ret == 0) {
  1055. TEST_PASS("AES-GCM test passed!\n");
  1056. }
  1057. #endif
  1058. #if defined(HAVE_AESCCM) && defined(WOLFSSL_AES_128)
  1059. if ( (ret = aesccm_test()) != 0)
  1060. return err_sys("AES-CCM test failed!\n", ret);
  1061. else
  1062. TEST_PASS("AES-CCM test passed!\n");
  1063. #endif
  1064. #ifdef HAVE_AES_KEYWRAP
  1065. if ( (ret = aeskeywrap_test()) != 0)
  1066. return err_sys("AES Key Wrap test failed!\n", ret);
  1067. else
  1068. TEST_PASS("AES Key Wrap test passed!\n");
  1069. #endif
  1070. #ifdef WOLFSSL_AES_SIV
  1071. if ( (ret = aes_siv_test()) != 0)
  1072. return err_sys("AES-SIV test failed!\n", ret);
  1073. else
  1074. TEST_PASS("AES-SIV test passed!\n");
  1075. #endif
  1076. #endif
  1077. #ifdef HAVE_CAMELLIA
  1078. if ( (ret = camellia_test()) != 0)
  1079. return err_sys("CAMELLIA test failed!\n", ret);
  1080. else
  1081. TEST_PASS("CAMELLIA test passed!\n");
  1082. #endif
  1083. #if !defined(NO_RSA)
  1084. #ifdef WC_RSA_NO_PADDING
  1085. if ( (ret = rsa_no_pad_test()) != 0)
  1086. return err_sys("RSA NOPAD test failed!\n", ret);
  1087. else
  1088. TEST_PASS("RSA NOPAD test passed!\n");
  1089. #endif
  1090. if ( (ret = rsa_test()) != 0)
  1091. return err_sys("RSA test failed!\n", ret);
  1092. else
  1093. TEST_PASS("RSA test passed!\n");
  1094. #endif
  1095. #ifndef NO_DH
  1096. PRIVATE_KEY_UNLOCK();
  1097. if ( (ret = dh_test()) != 0)
  1098. return err_sys("DH test failed!\n", ret);
  1099. else
  1100. TEST_PASS("DH test passed!\n");
  1101. PRIVATE_KEY_LOCK();
  1102. #endif
  1103. #ifndef NO_DSA
  1104. if ( (ret = dsa_test()) != 0)
  1105. return err_sys("DSA test failed!\n", ret);
  1106. else
  1107. TEST_PASS("DSA test passed!\n");
  1108. #endif
  1109. #ifdef WOLFCRYPT_HAVE_SRP
  1110. if ( (ret = srp_test()) != 0)
  1111. return err_sys("SRP test failed!\n", ret);
  1112. else
  1113. TEST_PASS("SRP test passed!\n");
  1114. #endif
  1115. #ifndef NO_PWDBASED
  1116. if ( (ret = pwdbased_test()) != 0)
  1117. return err_sys("PWDBASED test failed!\n", ret);
  1118. else
  1119. TEST_PASS("PWDBASED test passed!\n");
  1120. #endif
  1121. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  1122. if ( (ret = openssl_test()) != 0)
  1123. return err_sys("OPENSSL test failed!\n", ret);
  1124. else
  1125. TEST_PASS("OPENSSL test passed!\n");
  1126. if ( (ret = openSSL_evpMD_test()) != 0)
  1127. return err_sys("OPENSSL (EVP MD) test failed!\n", ret);
  1128. else
  1129. TEST_PASS("OPENSSL (EVP MD) passed!\n");
  1130. if ( (ret = openssl_pkey0_test()) != 0)
  1131. return err_sys("OPENSSL (PKEY0) test failed!\n", ret);
  1132. else
  1133. TEST_PASS("OPENSSL (PKEY0) passed!\n");
  1134. if ( (ret = openssl_pkey1_test()) != 0)
  1135. return err_sys("OPENSSL (PKEY1) test failed!\n", ret);
  1136. else
  1137. TEST_PASS("OPENSSL (PKEY1) passed!\n");
  1138. #if !defined(WOLF_CRYPTO_CB_ONLY_RSA) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  1139. if ( (ret = openssl_evpSig_test()) != 0)
  1140. return err_sys("OPENSSL (EVP Sign/Verify) test failed!\n", ret);
  1141. else
  1142. TEST_PASS("OPENSSL (EVP Sign/Verify) passed!\n");
  1143. #endif
  1144. #endif
  1145. #if defined(HAVE_ECC)
  1146. PRIVATE_KEY_UNLOCK();
  1147. if ( (ret = ecc_test()) != 0)
  1148. return err_sys("ECC test failed!\n", ret);
  1149. else
  1150. TEST_PASS("ECC test passed!\n");
  1151. PRIVATE_KEY_LOCK();
  1152. #if defined(HAVE_ECC_ENCRYPT) && defined(HAVE_AES_CBC) && \
  1153. (defined(WOLFSSL_AES_128) || defined(WOLFSSL_AES_256))
  1154. if ( (ret = ecc_encrypt_test()) != 0)
  1155. return err_sys("ECC Enc test failed!\n", ret);
  1156. else
  1157. TEST_PASS("ECC Enc test passed!\n");
  1158. #endif
  1159. #if defined(USE_CERT_BUFFERS_256) && !defined(WOLFSSL_ATECC508A) && \
  1160. !defined(WOLFSSL_ATECC608A) && !defined(NO_ECC256) && \
  1161. defined(HAVE_ECC_VERIFY) && defined(HAVE_ECC_SIGN) && \
  1162. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  1163. /* skip for ATECC508/608A, cannot import private key buffers */
  1164. if ( (ret = ecc_test_buffers()) != 0)
  1165. return err_sys("ECC buffer test failed!\n", ret);
  1166. else
  1167. TEST_PASS("ECC buffer test passed!\n");
  1168. #endif
  1169. #endif
  1170. #if !defined(NO_ASN_TIME) && !defined(NO_RSA) && defined(WOLFSSL_TEST_CERT) && \
  1171. !defined(NO_FILESYSTEM)
  1172. if ( (ret = cert_test()) != 0)
  1173. return err_sys("CERT test failed!\n", ret);
  1174. else
  1175. TEST_PASS("CERT test passed!\n");
  1176. #endif
  1177. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_TEST_CERT) && \
  1178. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && defined(WOLFSSL_GEN_CERT)
  1179. if ( (ret = certext_test()) != 0)
  1180. return err_sys("CERT EXT test failed!\n", ret);
  1181. else
  1182. TEST_PASS("CERT EXT test passed!\n");
  1183. #endif
  1184. #if defined(WOLFSSL_CERT_GEN_CACHE) && defined(WOLFSSL_TEST_CERT) && \
  1185. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  1186. if ( (ret = decodedCertCache_test()) != 0)
  1187. return err_sys("DECODED CERT CACHE test failed!\n", ret);
  1188. else
  1189. TEST_PASS("DECODED CERT CACHE test passed!\n");
  1190. #endif
  1191. #ifdef HAVE_CURVE25519
  1192. if ( (ret = curve25519_test()) != 0)
  1193. return err_sys("CURVE25519 test failed!\n", ret);
  1194. else
  1195. TEST_PASS("CURVE25519 test passed!\n");
  1196. #endif
  1197. #ifdef HAVE_ED25519
  1198. if ( (ret = ed25519_test()) != 0)
  1199. return err_sys("ED25519 test failed!\n", ret);
  1200. else
  1201. TEST_PASS("ED25519 test passed!\n");
  1202. #endif
  1203. #ifdef HAVE_CURVE448
  1204. if ( (ret = curve448_test()) != 0)
  1205. return err_sys("CURVE448 test failed!\n", ret);
  1206. else
  1207. TEST_PASS("CURVE448 test passed!\n");
  1208. #endif
  1209. #ifdef HAVE_ED448
  1210. if ( (ret = ed448_test()) != 0)
  1211. return err_sys("ED448 test failed!\n", ret);
  1212. else
  1213. TEST_PASS("ED448 test passed!\n");
  1214. #endif
  1215. #ifdef WOLFCRYPT_HAVE_ECCSI
  1216. if ( (ret = eccsi_test()) != 0)
  1217. return err_sys("ECCSI test failed!\n", ret);
  1218. else
  1219. TEST_PASS("ECCSI test passed!\n");
  1220. #endif
  1221. #ifdef WOLFCRYPT_HAVE_SAKKE
  1222. if ( (ret = sakke_test()) != 0)
  1223. return err_sys("SAKKE test failed!\n", ret);
  1224. else
  1225. TEST_PASS("SAKKE test passed!\n");
  1226. #endif
  1227. #if defined(WOLFSSL_CMAC) && !defined(NO_AES)
  1228. if ( (ret = cmac_test()) != 0)
  1229. return err_sys("CMAC test failed!\n", ret);
  1230. else
  1231. TEST_PASS("CMAC test passed!\n");
  1232. #endif
  1233. #if defined(WOLFSSL_SIPHASH)
  1234. if ( (ret = siphash_test()) != 0)
  1235. return err_sys("SipHash test failed!\n", ret);
  1236. else
  1237. TEST_PASS("SipHash test passed!\n");
  1238. #endif
  1239. #ifdef HAVE_LIBZ
  1240. if ( (ret = compress_test()) != 0)
  1241. return err_sys("COMPRESS test failed!\n", ret);
  1242. else
  1243. TEST_PASS("COMPRESS test passed!\n");
  1244. #endif
  1245. #ifdef HAVE_PKCS7
  1246. #ifndef NO_PKCS7_ENCRYPTED_DATA
  1247. if ( (ret = pkcs7encrypted_test()) != 0)
  1248. return err_sys("PKCS7encrypted test failed!\n", ret);
  1249. else
  1250. TEST_PASS("PKCS7encrypted test passed!\n");
  1251. #endif
  1252. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  1253. if ( (ret = pkcs7compressed_test()) != 0)
  1254. return err_sys("PKCS7compressed test failed!\n", ret);
  1255. else
  1256. TEST_PASS("PKCS7compressed test passed!\n");
  1257. #endif
  1258. if ( (ret = pkcs7signed_test()) != 0)
  1259. return err_sys("PKCS7signed test failed!\n", ret);
  1260. else
  1261. TEST_PASS("PKCS7signed test passed!\n");
  1262. if ( (ret = pkcs7enveloped_test()) != 0)
  1263. return err_sys("PKCS7enveloped test failed!\n", ret);
  1264. else
  1265. TEST_PASS("PKCS7enveloped test passed!\n");
  1266. #if defined(HAVE_AESGCM) || defined(HAVE_AESCCM)
  1267. if ( (ret = pkcs7authenveloped_test()) != 0)
  1268. return err_sys("PKCS7authenveloped test failed!\n", ret);
  1269. else
  1270. TEST_PASS("PKCS7authenveloped test passed!\n");
  1271. #endif
  1272. #endif
  1273. #ifdef HAVE_VALGRIND
  1274. if ( (ret = mp_test()) != 0)
  1275. return err_sys("mp test failed!\n", ret);
  1276. else
  1277. TEST_PASS("mp test passed!\n");
  1278. #endif
  1279. #if defined(WOLFSSL_PUBLIC_MP) && defined(WOLFSSL_KEY_GEN)
  1280. if ( (ret = prime_test()) != 0)
  1281. return err_sys("prime test failed!\n", ret);
  1282. else
  1283. TEST_PASS("prime test passed!\n");
  1284. #endif
  1285. #if defined(ASN_BER_TO_DER) && \
  1286. (defined(WOLFSSL_TEST_CERT) || defined(OPENSSL_EXTRA) || \
  1287. defined(OPENSSL_EXTRA_X509_SMALL))
  1288. if ( (ret = berder_test()) != 0)
  1289. return err_sys("ber-der test failed!\n", ret);
  1290. else
  1291. TEST_PASS("ber-der test passed!\n");
  1292. #endif
  1293. if ( (ret = logging_test()) != 0)
  1294. return err_sys("logging test failed!\n", ret);
  1295. else
  1296. TEST_PASS("logging test passed!\n");
  1297. #if !defined(NO_ASN) && !defined(NO_ASN_TIME)
  1298. if ( (ret = time_test()) != 0)
  1299. return err_sys("time test failed!\n", ret);
  1300. else
  1301. TEST_PASS("time test passed!\n");
  1302. #endif
  1303. if ( (ret = mutex_test()) != 0)
  1304. return err_sys("mutex test failed!\n", ret);
  1305. else
  1306. TEST_PASS("mutex test passed!\n");
  1307. #if defined(USE_WOLFSSL_MEMORY) && !defined(FREERTOS)
  1308. if ( (ret = memcb_test()) != 0)
  1309. return err_sys("memcb test failed!\n", ret);
  1310. else
  1311. TEST_PASS("memcb test passed!\n");
  1312. #endif
  1313. #ifdef WOLFSSL_IMX6_CAAM_BLOB
  1314. if ( (ret = blob_test()) != 0)
  1315. return err_sys("blob test failed!\n", ret);
  1316. else
  1317. TEST_PASS("blob test passed!\n");
  1318. #endif
  1319. #if defined(WOLF_CRYPTO_CB) && \
  1320. !(defined(HAVE_INTEL_QAT_SYNC) || defined(HAVE_CAVIUM_OCTEON_SYNC) || \
  1321. defined(WOLFSSL_QNX_CAAM))
  1322. if ( (ret = cryptocb_test()) != 0)
  1323. return err_sys("crypto callback test failed!\n", ret);
  1324. else
  1325. TEST_PASS("crypto callback test passed!\n");
  1326. #endif
  1327. #ifdef WOLFSSL_CERT_PIV
  1328. if ( (ret = certpiv_test()) != 0)
  1329. return err_sys("cert piv test failed!\n", ret);
  1330. else
  1331. TEST_PASS("cert piv test passed!\n");
  1332. #endif
  1333. #ifdef WOLF_CRYPTO_CB
  1334. #ifdef HAVE_INTEL_QA_SYNC
  1335. wc_CryptoCb_CleanupIntelQa(&devId);
  1336. #endif
  1337. #ifdef HAVE_CAVIUM_OCTEON_SYNC
  1338. wc_CryptoCb_CleanupOcteon(&devId);
  1339. #endif
  1340. #endif
  1341. #ifdef WOLFSSL_ASYNC_CRYPT
  1342. wolfAsync_DevClose(&devId);
  1343. #endif
  1344. /* cleanup the thread if fixed point cache is enabled and have thread local */
  1345. #if defined(HAVE_THREAD_LS) && defined(HAVE_ECC) && defined(FP_ECC)
  1346. wc_ecc_fp_free();
  1347. #endif
  1348. if (args)
  1349. ((func_args*)args)->return_code = ret;
  1350. TEST_PASS("Test complete\n");
  1351. EXIT_TEST(ret);
  1352. }
  1353. #ifndef NO_MAIN_DRIVER
  1354. /* so overall tests can pull in test function */
  1355. #ifdef WOLFSSL_ESPIDF
  1356. void app_main( )
  1357. #else
  1358. #ifdef HAVE_WOLFCRYPT_TEST_OPTIONS
  1359. int myoptind = 0;
  1360. char* myoptarg = NULL;
  1361. #endif
  1362. int main(int argc, char** argv)
  1363. #endif
  1364. {
  1365. int ret;
  1366. func_args args;
  1367. #ifdef WOLFSSL_ESPIDF
  1368. /* set dummy wallclock time. */
  1369. struct timeval utctime;
  1370. struct timezone tz;
  1371. utctime.tv_sec = 1521725159; /* dummy time: 2018-03-22T13:25:59+00:00 */
  1372. utctime.tv_usec = 0;
  1373. tz.tz_minuteswest = 0;
  1374. tz.tz_dsttime = 0;
  1375. settimeofday(&utctime, &tz);
  1376. #endif
  1377. #ifdef WOLFSSL_APACHE_MYNEWT
  1378. #ifdef ARCH_sim
  1379. mcu_sim_parse_args(argc, argv);
  1380. #endif
  1381. sysinit();
  1382. /* set dummy wallclock time. */
  1383. struct os_timeval utctime;
  1384. struct os_timezone tz;
  1385. utctime.tv_sec = 1521725159; /* dummy time: 2018-03-22T13:25:59+00:00 */
  1386. utctime.tv_usec = 0;
  1387. tz.tz_minuteswest = 0;
  1388. tz.tz_dsttime = 0;
  1389. os_settimeofday(&utctime, &tz);
  1390. #endif
  1391. #ifdef WOLFSSL_ZEPHYR
  1392. /* set dummy wallclock time. */
  1393. struct timespec utctime;
  1394. utctime.tv_sec = 1521725159; /* dummy time: 2018-03-22T13:25:59+00:00 */
  1395. utctime.tv_nsec = 0;
  1396. clock_settime(CLOCK_REALTIME, &utctime);
  1397. #endif
  1398. #ifdef DEVKITPRO
  1399. void *framebuffer;
  1400. GXRModeObj *rmode = NULL;
  1401. VIDEO_Init();
  1402. WPAD_Init();
  1403. rmode = VIDEO_GetPreferredMode(NULL);
  1404. #pragma GCC diagnostic ignored "-Wbad-function-cast"
  1405. framebuffer = MEM_K0_TO_K1(SYS_AllocateFramebuffer(rmode));
  1406. #pragma GCC diagnostic pop
  1407. console_init(framebuffer,20,20,rmode->fbWidth,rmode->xfbHeight,rmode->fbWidth*VI_DISPLAY_PIX_SZ);
  1408. VIDEO_Configure(rmode);
  1409. VIDEO_SetNextFramebuffer(framebuffer);
  1410. VIDEO_SetBlack(FALSE);
  1411. VIDEO_Flush();
  1412. VIDEO_WaitVSync();
  1413. if(rmode->viTVMode&VI_NON_INTERLACE) VIDEO_WaitVSync();
  1414. #endif
  1415. #ifdef HAVE_WNR
  1416. if (wc_InitNetRandom(wnrConfigFile, NULL, 5000) != 0) {
  1417. err_sys("Whitewood netRandom global config failed", -1001);
  1418. return -1002;
  1419. }
  1420. #endif
  1421. #ifndef WOLFSSL_ESPIDF
  1422. args.argc = argc;
  1423. args.argv = argv;
  1424. #endif
  1425. if ((ret = wolfCrypt_Init()) != 0) {
  1426. printf("wolfCrypt_Init failed %d\n", ret);
  1427. err_sys("Error with wolfCrypt_Init!\n", -1003);
  1428. }
  1429. #ifdef WC_RNG_SEED_CB
  1430. wc_SetSeed_Cb(wc_GenerateSeed);
  1431. #endif
  1432. #ifdef HAVE_STACK_SIZE
  1433. StackSizeCheck(&args, wolfcrypt_test);
  1434. #else
  1435. wolfcrypt_test(&args);
  1436. #endif
  1437. if ((ret = wolfCrypt_Cleanup()) != 0) {
  1438. printf("wolfCrypt_Cleanup failed %d\n", ret);
  1439. err_sys("Error with wolfCrypt_Cleanup!\n", -1004);
  1440. }
  1441. #ifdef HAVE_WNR
  1442. if (wc_FreeNetRandom() < 0)
  1443. err_sys("Failed to free netRandom context", -1005);
  1444. #endif /* HAVE_WNR */
  1445. #ifdef DOLPHIN_EMULATOR
  1446. /* Returning from main panics the emulator. Just hang
  1447. * and let the user force quit the emulator window. */
  1448. printf("args.return_code: %d\n", args.return_code);
  1449. printf("Testing complete. You may close the window now\n");
  1450. while (1);
  1451. #endif
  1452. #ifndef WOLFSSL_ESPIDF
  1453. printf("Exiting main with return code: %d\n", args.return_code);
  1454. return args.return_code;
  1455. #endif
  1456. }
  1457. #endif /* NO_MAIN_DRIVER */
  1458. /* helper to save DER, convert to PEM and save PEM */
  1459. #if !defined(NO_ASN) && (defined(HAVE_ECC) || !defined(NO_DSA) || \
  1460. (!defined(NO_RSA) && (defined(WOLFSSL_KEY_GEN) || defined(WOLFSSL_CERT_GEN)))) \
  1461. && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  1462. #if !defined(NO_FILESYSTEM) && !defined(NO_WRITE_TEMP_FILES)
  1463. #define SaveDerAndPem(d, dSz, fD, fP, pT, eB) _SaveDerAndPem(d, dSz, fD, fP, pT, eB)
  1464. #else
  1465. #define SaveDerAndPem(d, dSz, fD, fP, pT, eB) _SaveDerAndPem(d, dSz, NULL, NULL, pT, eB)
  1466. #endif
  1467. static int _SaveDerAndPem(const byte* der, int derSz,
  1468. const char* fileDer, const char* filePem, int pemType, int errBase)
  1469. {
  1470. #if !defined(NO_FILESYSTEM) && !defined(NO_WRITE_TEMP_FILES)
  1471. int ret;
  1472. XFILE derFile;
  1473. derFile = XFOPEN(fileDer, "wb");
  1474. if (!derFile) {
  1475. return errBase + 0;
  1476. }
  1477. ret = (int)XFWRITE(der, 1, derSz, derFile);
  1478. XFCLOSE(derFile);
  1479. if (ret != derSz) {
  1480. return errBase + 1;
  1481. }
  1482. #endif
  1483. #ifdef WOLFSSL_DER_TO_PEM
  1484. if (filePem) {
  1485. #if !defined(NO_FILESYSTEM) && !defined(NO_WRITE_TEMP_FILES)
  1486. XFILE pemFile;
  1487. #endif
  1488. byte* pem;
  1489. int pemSz;
  1490. /* calculate PEM size */
  1491. pemSz = wc_DerToPem(der, derSz, NULL, 0, pemType);
  1492. if (pemSz < 0) {
  1493. return pemSz;
  1494. }
  1495. pem = (byte*)XMALLOC(pemSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  1496. if (pem == NULL) {
  1497. return MEMORY_E;
  1498. }
  1499. /* Convert to PEM */
  1500. pemSz = wc_DerToPem(der, derSz, pem, pemSz, pemType);
  1501. if (pemSz < 0) {
  1502. XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  1503. return errBase + 2;
  1504. }
  1505. #if !defined(NO_FILESYSTEM) && !defined(NO_WRITE_TEMP_FILES)
  1506. pemFile = XFOPEN(filePem, "wb");
  1507. if (!pemFile) {
  1508. XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  1509. return errBase + 3;
  1510. }
  1511. ret = (int)XFWRITE(pem, 1, pemSz, pemFile);
  1512. XFCLOSE(pemFile);
  1513. if (ret != pemSz) {
  1514. XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  1515. return errBase + 4;
  1516. }
  1517. #endif
  1518. XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  1519. }
  1520. #endif /* WOLFSSL_DER_TO_PEM */
  1521. /* suppress unused variable warnings */
  1522. (void)der;
  1523. (void)derSz;
  1524. (void)filePem;
  1525. (void)fileDer;
  1526. (void)pemType;
  1527. (void)errBase;
  1528. return 0;
  1529. }
  1530. #endif /* WOLFSSL_KEY_GEN || WOLFSSL_CERT_GEN */
  1531. WOLFSSL_TEST_SUBROUTINE int error_test(void)
  1532. {
  1533. const char* errStr;
  1534. char out[WOLFSSL_MAX_ERROR_SZ];
  1535. const char* unknownStr = wc_GetErrorString(0);
  1536. #ifdef NO_ERROR_STRINGS
  1537. /* Ensure a valid error code's string matches an invalid code's.
  1538. * The string is that error strings are not available.
  1539. */
  1540. errStr = wc_GetErrorString(OPEN_RAN_E);
  1541. wc_ErrorString(OPEN_RAN_E, out);
  1542. if (XSTRCMP(errStr, unknownStr) != 0)
  1543. return -1100;
  1544. if (XSTRCMP(out, unknownStr) != 0)
  1545. return -1101;
  1546. #else
  1547. int i;
  1548. int j = 0;
  1549. /* Values that are not or no longer error codes. */
  1550. int missing[] = { -122, -123, -124, -127, -128, -129, -159,
  1551. -163, -164, -165, -166, -167, -168, -169, -233,
  1552. 0 };
  1553. /* Check that all errors have a string and it's the same through the two
  1554. * APIs. Check that the values that are not errors map to the unknown
  1555. * string.
  1556. */
  1557. for (i = MAX_CODE_E-1; i >= WC_LAST_E; i--) {
  1558. errStr = wc_GetErrorString(i);
  1559. wc_ErrorString(i, out);
  1560. if (i != missing[j]) {
  1561. if (XSTRCMP(errStr, unknownStr) == 0)
  1562. return -1102;
  1563. if (XSTRCMP(out, unknownStr) == 0)
  1564. return -1103;
  1565. if (XSTRCMP(errStr, out) != 0)
  1566. return -1104;
  1567. if (XSTRLEN(errStr) >= WOLFSSL_MAX_ERROR_SZ)
  1568. return -1105;
  1569. }
  1570. else {
  1571. j++;
  1572. if (XSTRCMP(errStr, unknownStr) != 0)
  1573. return -1106;
  1574. if (XSTRCMP(out, unknownStr) != 0)
  1575. return -1107;
  1576. }
  1577. }
  1578. /* Check if the next possible value has been given a string. */
  1579. errStr = wc_GetErrorString(i);
  1580. wc_ErrorString(i, out);
  1581. if (XSTRCMP(errStr, unknownStr) != 0)
  1582. return -1108;
  1583. if (XSTRCMP(out, unknownStr) != 0)
  1584. return -1109;
  1585. #endif
  1586. return 0;
  1587. }
  1588. #ifndef NO_CODING
  1589. WOLFSSL_TEST_SUBROUTINE int base64_test(void)
  1590. {
  1591. int ret;
  1592. WOLFSSL_SMALL_STACK_STATIC const byte good[] = "A+Gd\0\0\0";
  1593. WOLFSSL_SMALL_STACK_STATIC const byte goodEnd[] = "A+Gd \r\n";
  1594. WOLFSSL_SMALL_STACK_STATIC const byte good_spaces[] = " A + G d \0";
  1595. byte out[128];
  1596. word32 outLen;
  1597. #ifdef WOLFSSL_BASE64_ENCODE
  1598. byte data[3];
  1599. word32 dataLen;
  1600. byte longData[79] = { 0 };
  1601. WOLFSSL_SMALL_STACK_STATIC const byte symbols[] = "+/A=";
  1602. #endif
  1603. WOLFSSL_SMALL_STACK_STATIC const byte badSmall[] = "AAA!Gdj=";
  1604. WOLFSSL_SMALL_STACK_STATIC const byte badLarge[] = "AAA~Gdj=";
  1605. WOLFSSL_SMALL_STACK_STATIC const byte badEOL[] = "A+Gd!AA";
  1606. WOLFSSL_SMALL_STACK_STATIC const byte badPadding[] = "AA=A";
  1607. WOLFSSL_SMALL_STACK_STATIC const byte badChar[] = ",-.:;<=>?@[\\]^_`";
  1608. byte goodChar[] =
  1609. "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  1610. "abcdefghijklmnopqrstuvwxyz"
  1611. "0123456789+/;";
  1612. byte charTest[] = "A+Gd\0\0\0";
  1613. int i;
  1614. /* Good Base64 encodings. */
  1615. outLen = sizeof(out);
  1616. ret = Base64_Decode(good, sizeof(good), out, &outLen);
  1617. if (ret != 0)
  1618. return -1200;
  1619. outLen = sizeof(out);
  1620. ret = Base64_Decode(goodEnd, sizeof(goodEnd), out, &outLen);
  1621. if (ret != 0)
  1622. return -1201;
  1623. outLen = sizeof(goodChar);
  1624. ret = Base64_Decode(goodChar, sizeof(goodChar), goodChar, &outLen);
  1625. if (ret != 0)
  1626. return -1235;
  1627. if (outLen != 64 / 4 * 3)
  1628. return -1236;
  1629. outLen = sizeof(out);
  1630. ret = Base64_Decode(good_spaces, sizeof(good_spaces), out, &outLen);
  1631. if (ret != 0)
  1632. return -1201;
  1633. /* Bad parameters. */
  1634. outLen = 1;
  1635. ret = Base64_Decode(good, sizeof(good), out, &outLen);
  1636. if (ret != BAD_FUNC_ARG)
  1637. return -1202;
  1638. outLen = sizeof(out);
  1639. ret = Base64_Decode(badEOL, sizeof(badEOL), out, &outLen);
  1640. if (ret != ASN_INPUT_E)
  1641. return -1203;
  1642. outLen = sizeof(out);
  1643. ret = Base64_Decode(badPadding, sizeof(badPadding), out, &outLen);
  1644. if (ret != ASN_INPUT_E)
  1645. return -1203;
  1646. /* Bad character at each offset 0-3. */
  1647. for (i = 0; i < 4; i++) {
  1648. outLen = sizeof(out);
  1649. ret = Base64_Decode(badSmall + i, 4, out, &outLen);
  1650. if (ret != ASN_INPUT_E)
  1651. return -1204 - i;
  1652. ret = Base64_Decode(badLarge + i, 4, out, &outLen);
  1653. if (ret != ASN_INPUT_E)
  1654. return -1214 - i;
  1655. }
  1656. /* Invalid character less than 0x2b */
  1657. for (i = 1; i < 0x2b; i++) {
  1658. outLen = sizeof(out);
  1659. charTest[0] = (byte)i;
  1660. ret = Base64_Decode(charTest, sizeof(charTest), out, &outLen);
  1661. if (ret != ASN_INPUT_E)
  1662. return -1240 - i;
  1663. }
  1664. /* Bad characters in range 0x2b - 0x7a. */
  1665. for (i = 0; i < (int)sizeof(badChar) - 1; i++) {
  1666. outLen = sizeof(out);
  1667. charTest[0] = badChar[i];
  1668. ret = Base64_Decode(charTest, sizeof(charTest), out, &outLen);
  1669. if (ret != ASN_INPUT_E)
  1670. return -1270 - i;
  1671. }
  1672. /* Invalid character greater than 0x7a */
  1673. for (i = 0x7b; i < 0x100; i++) {
  1674. outLen = sizeof(out);
  1675. charTest[0] = (byte)i;
  1676. ret = Base64_Decode(charTest, sizeof(charTest), out, &outLen);
  1677. if (ret != ASN_INPUT_E)
  1678. return -1290 - i;
  1679. }
  1680. #ifdef WOLFSSL_BASE64_ENCODE
  1681. /* Decode and encode all symbols - non-alphanumeric. */
  1682. dataLen = sizeof(data);
  1683. ret = Base64_Decode(symbols, sizeof(symbols), data, &dataLen);
  1684. if (ret != 0)
  1685. return -1224;
  1686. outLen = sizeof(out);
  1687. ret = Base64_Encode(data, dataLen, NULL, &outLen);
  1688. if (ret != LENGTH_ONLY_E)
  1689. return -1225;
  1690. outLen = sizeof(out);
  1691. ret = Base64_Encode(data, dataLen, out, &outLen);
  1692. if (ret != 0)
  1693. return -1226;
  1694. outLen = 7;
  1695. ret = Base64_EncodeEsc(data, dataLen, out, &outLen);
  1696. if (ret != BUFFER_E)
  1697. return -1227;
  1698. outLen = sizeof(out);
  1699. ret = Base64_EncodeEsc(data, dataLen, NULL, &outLen);
  1700. if (ret != LENGTH_ONLY_E)
  1701. return -1228;
  1702. outLen = sizeof(out);
  1703. ret = Base64_EncodeEsc(data, dataLen, out, &outLen);
  1704. if (ret != 0)
  1705. return -1229;
  1706. outLen = sizeof(out);
  1707. ret = Base64_Encode_NoNl(data, dataLen, out, &outLen);
  1708. if (ret != 0)
  1709. return -1230;
  1710. /* Data that results in an encoding longer than one line. */
  1711. outLen = sizeof(out);
  1712. dataLen = sizeof(longData);
  1713. ret = Base64_Encode(longData, dataLen, out, &outLen);
  1714. if (ret != 0)
  1715. return -1231;
  1716. outLen = sizeof(out);
  1717. ret = Base64_EncodeEsc(longData, dataLen, out, &outLen);
  1718. if (ret != 0)
  1719. return -1232;
  1720. outLen = sizeof(out);
  1721. ret = Base64_Encode_NoNl(longData, dataLen, out, &outLen);
  1722. if (ret != 0)
  1723. return -1233;
  1724. #endif
  1725. return 0;
  1726. }
  1727. #ifdef WOLFSSL_BASE16
  1728. WOLFSSL_TEST_SUBROUTINE int base16_test(void)
  1729. {
  1730. int ret;
  1731. WOLFSSL_SMALL_STACK_STATIC const byte testData[] = "SomeDataToEncode\n";
  1732. WOLFSSL_SMALL_STACK_STATIC const byte encodedTestData[] = "536F6D6544617461546F456E636F64650A00";
  1733. byte encoded[40];
  1734. word32 encodedLen;
  1735. byte plain[40];
  1736. word32 len;
  1737. /* length returned includes null termination */
  1738. encodedLen = sizeof(encoded);
  1739. ret = Base16_Encode(testData, sizeof(testData), encoded, &encodedLen);
  1740. if (ret != 0)
  1741. return -1300;
  1742. len = (word32)XSTRLEN((char*)encoded);
  1743. if (len != encodedLen - 1)
  1744. return -1301;
  1745. len = sizeof(plain);
  1746. ret = Base16_Decode(encoded, encodedLen - 1, plain, &len);
  1747. if (ret != 0)
  1748. return -1302;
  1749. if (len != sizeof(testData) || XMEMCMP(testData, plain, len) != 0)
  1750. return -1303;
  1751. if (encodedLen != sizeof(encodedTestData) ||
  1752. XMEMCMP(encoded, encodedTestData, encodedLen) != 0) {
  1753. return -1304;
  1754. }
  1755. return 0;
  1756. }
  1757. #endif /* WOLFSSL_BASE16 */
  1758. #endif /* !NO_CODING */
  1759. #ifndef NO_ASN
  1760. WOLFSSL_TEST_SUBROUTINE int asn_test(void)
  1761. {
  1762. int ret;
  1763. /* ASN1 encoded date buffer */
  1764. WOLFSSL_SMALL_STACK_STATIC const byte dateBuf[] = {0x17, 0x0d, 0x31, 0x36, 0x30, 0x38, 0x31, 0x31,
  1765. 0x32, 0x30, 0x30, 0x37, 0x33, 0x37, 0x5a};
  1766. byte format;
  1767. int length;
  1768. const byte* datePart;
  1769. #ifndef NO_ASN_TIME
  1770. struct tm timearg;
  1771. time_t now;
  1772. #endif
  1773. ret = wc_GetDateInfo(dateBuf, (int)sizeof(dateBuf), &datePart, &format,
  1774. &length);
  1775. if (ret != 0)
  1776. return -1400;
  1777. #ifndef NO_ASN_TIME
  1778. /* Parameter Validation tests. */
  1779. if (wc_GetTime(NULL, sizeof(now)) != BAD_FUNC_ARG)
  1780. return -1401;
  1781. if (wc_GetTime(&now, 0) != BUFFER_E)
  1782. return -1402;
  1783. now = 0;
  1784. if (wc_GetTime(&now, sizeof(now)) != 0) {
  1785. return -1403;
  1786. }
  1787. if (now == 0) {
  1788. printf("RTC/Time not set!\n");
  1789. return -1404;
  1790. }
  1791. ret = wc_GetDateAsCalendarTime(datePart, length, format, &timearg);
  1792. if (ret != 0)
  1793. return -1405;
  1794. #endif /* !NO_ASN_TIME */
  1795. return 0;
  1796. }
  1797. #endif /* !NO_ASN */
  1798. #ifdef WOLFSSL_MD2
  1799. WOLFSSL_TEST_SUBROUTINE int md2_test(void)
  1800. {
  1801. int ret = 0;
  1802. Md2 md2;
  1803. byte hash[MD2_DIGEST_SIZE];
  1804. testVector a, b, c, d, e, f, g;
  1805. testVector test_md2[7];
  1806. int times = sizeof(test_md2) / sizeof(testVector), i;
  1807. a.input = "";
  1808. a.output = "\x83\x50\xe5\xa3\xe2\x4c\x15\x3d\xf2\x27\x5c\x9f\x80\x69"
  1809. "\x27\x73";
  1810. a.inLen = XSTRLEN(a.input);
  1811. a.outLen = MD2_DIGEST_SIZE;
  1812. b.input = "a";
  1813. b.output = "\x32\xec\x01\xec\x4a\x6d\xac\x72\xc0\xab\x96\xfb\x34\xc0"
  1814. "\xb5\xd1";
  1815. b.inLen = XSTRLEN(b.input);
  1816. b.outLen = MD2_DIGEST_SIZE;
  1817. c.input = "abc";
  1818. c.output = "\xda\x85\x3b\x0d\x3f\x88\xd9\x9b\x30\x28\x3a\x69\xe6\xde"
  1819. "\xd6\xbb";
  1820. c.inLen = XSTRLEN(c.input);
  1821. c.outLen = MD2_DIGEST_SIZE;
  1822. d.input = "message digest";
  1823. d.output = "\xab\x4f\x49\x6b\xfb\x2a\x53\x0b\x21\x9f\xf3\x30\x31\xfe"
  1824. "\x06\xb0";
  1825. d.inLen = XSTRLEN(d.input);
  1826. d.outLen = MD2_DIGEST_SIZE;
  1827. e.input = "abcdefghijklmnopqrstuvwxyz";
  1828. e.output = "\x4e\x8d\xdf\xf3\x65\x02\x92\xab\x5a\x41\x08\xc3\xaa\x47"
  1829. "\x94\x0b";
  1830. e.inLen = XSTRLEN(e.input);
  1831. e.outLen = MD2_DIGEST_SIZE;
  1832. f.input = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345"
  1833. "6789";
  1834. f.output = "\xda\x33\xde\xf2\xa4\x2d\xf1\x39\x75\x35\x28\x46\xc3\x03"
  1835. "\x38\xcd";
  1836. f.inLen = XSTRLEN(f.input);
  1837. f.outLen = MD2_DIGEST_SIZE;
  1838. g.input = "1234567890123456789012345678901234567890123456789012345678"
  1839. "9012345678901234567890";
  1840. g.output = "\xd5\x97\x6f\x79\xd8\x3d\x3a\x0d\xc9\x80\x6c\x3c\x66\xf3"
  1841. "\xef\xd8";
  1842. g.inLen = XSTRLEN(g.input);
  1843. g.outLen = MD2_DIGEST_SIZE;
  1844. test_md2[0] = a;
  1845. test_md2[1] = b;
  1846. test_md2[2] = c;
  1847. test_md2[3] = d;
  1848. test_md2[4] = e;
  1849. test_md2[5] = f;
  1850. test_md2[6] = g;
  1851. wc_InitMd2(&md2);
  1852. for (i = 0; i < times; ++i) {
  1853. wc_Md2Update(&md2, (byte*)test_md2[i].input, (word32)test_md2[i].inLen);
  1854. wc_Md2Final(&md2, hash);
  1855. if (XMEMCMP(hash, test_md2[i].output, MD2_DIGEST_SIZE) != 0)
  1856. return -1500 - i;
  1857. }
  1858. for (i = 0; i < times; ++i) {
  1859. ret = wc_Md2Hash((byte*)test_md2[i].input, (word32)test_md2[i].inLen, hash);
  1860. if (ret != 0) {
  1861. return -1507 - i;
  1862. }
  1863. if (XMEMCMP(hash, test_md2[i].output, MD2_DIGEST_SIZE) != 0) {
  1864. return -1507 - i;
  1865. }
  1866. }
  1867. return 0;
  1868. }
  1869. #endif
  1870. #ifndef NO_MD5
  1871. WOLFSSL_TEST_SUBROUTINE int md5_test(void)
  1872. {
  1873. int ret = 0;
  1874. wc_Md5 md5, md5Copy;
  1875. byte hash[WC_MD5_DIGEST_SIZE];
  1876. byte hashcopy[WC_MD5_DIGEST_SIZE];
  1877. testVector a, b, c, d, e, f;
  1878. testVector test_md5[6];
  1879. int times = sizeof(test_md5) / sizeof(testVector), i;
  1880. a.input = "";
  1881. a.output = "\xd4\x1d\x8c\xd9\x8f\x00\xb2\x04\xe9\x80\x09\x98\xec\xf8\x42"
  1882. "\x7e";
  1883. a.inLen = XSTRLEN(a.input);
  1884. a.outLen = WC_MD5_DIGEST_SIZE;
  1885. b.input = "abc";
  1886. b.output = "\x90\x01\x50\x98\x3c\xd2\x4f\xb0\xd6\x96\x3f\x7d\x28\xe1\x7f"
  1887. "\x72";
  1888. b.inLen = XSTRLEN(b.input);
  1889. b.outLen = WC_MD5_DIGEST_SIZE;
  1890. c.input = "message digest";
  1891. c.output = "\xf9\x6b\x69\x7d\x7c\xb7\x93\x8d\x52\x5a\x2f\x31\xaa\xf1\x61"
  1892. "\xd0";
  1893. c.inLen = XSTRLEN(c.input);
  1894. c.outLen = WC_MD5_DIGEST_SIZE;
  1895. d.input = "abcdefghijklmnopqrstuvwxyz";
  1896. d.output = "\xc3\xfc\xd3\xd7\x61\x92\xe4\x00\x7d\xfb\x49\x6c\xca\x67\xe1"
  1897. "\x3b";
  1898. d.inLen = XSTRLEN(d.input);
  1899. d.outLen = WC_MD5_DIGEST_SIZE;
  1900. e.input = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345"
  1901. "6789";
  1902. e.output = "\xd1\x74\xab\x98\xd2\x77\xd9\xf5\xa5\x61\x1c\x2c\x9f\x41\x9d"
  1903. "\x9f";
  1904. e.inLen = XSTRLEN(e.input);
  1905. e.outLen = WC_MD5_DIGEST_SIZE;
  1906. f.input = "1234567890123456789012345678901234567890123456789012345678"
  1907. "9012345678901234567890";
  1908. f.output = "\x57\xed\xf4\xa2\x2b\xe3\xc9\x55\xac\x49\xda\x2e\x21\x07\xb6"
  1909. "\x7a";
  1910. f.inLen = XSTRLEN(f.input);
  1911. f.outLen = WC_MD5_DIGEST_SIZE;
  1912. test_md5[0] = a;
  1913. test_md5[1] = b;
  1914. test_md5[2] = c;
  1915. test_md5[3] = d;
  1916. test_md5[4] = e;
  1917. test_md5[5] = f;
  1918. ret = wc_InitMd5_ex(&md5, HEAP_HINT, devId);
  1919. if (ret != 0)
  1920. return -1600;
  1921. ret = wc_InitMd5_ex(&md5Copy, HEAP_HINT, devId);
  1922. if (ret != 0) {
  1923. wc_Md5Free(&md5);
  1924. return -1601;
  1925. }
  1926. for (i = 0; i < times; ++i) {
  1927. ret = wc_Md5Update(&md5, (byte*)test_md5[i].input,
  1928. (word32)test_md5[i].inLen);
  1929. if (ret != 0)
  1930. ERROR_OUT(-1602 - i, exit);
  1931. ret = wc_Md5GetHash(&md5, hashcopy);
  1932. if (ret != 0)
  1933. ERROR_OUT(-1603 - i, exit);
  1934. ret = wc_Md5Copy(&md5, &md5Copy);
  1935. if (ret != 0)
  1936. ERROR_OUT(-1604 - i, exit);
  1937. ret = wc_Md5Final(&md5, hash);
  1938. if (ret != 0)
  1939. ERROR_OUT(-1605 - i, exit);
  1940. wc_Md5Free(&md5Copy);
  1941. if (XMEMCMP(hash, test_md5[i].output, WC_MD5_DIGEST_SIZE) != 0)
  1942. ERROR_OUT(-1606 - i, exit);
  1943. if (XMEMCMP(hash, hashcopy, WC_MD5_DIGEST_SIZE) != 0)
  1944. ERROR_OUT(-1607 - i, exit);
  1945. }
  1946. /* BEGIN LARGE HASH TEST */ {
  1947. byte large_input[1024];
  1948. const char* large_digest =
  1949. "\x44\xd0\x88\xce\xf1\x36\xd1\x78\xe9\xc8\xba\x84\xc3\xfd\xf6\xca";
  1950. for (i = 0; i < (int)sizeof(large_input); i++) {
  1951. large_input[i] = (byte)(i & 0xFF);
  1952. }
  1953. times = 100;
  1954. #ifdef WOLFSSL_PIC32MZ_HASH
  1955. wc_Md5SizeSet(&md5, times * sizeof(large_input));
  1956. #endif
  1957. for (i = 0; i < times; ++i) {
  1958. ret = wc_Md5Update(&md5, (byte*)large_input,
  1959. (word32)sizeof(large_input));
  1960. if (ret != 0)
  1961. ERROR_OUT(-1608, exit);
  1962. }
  1963. ret = wc_Md5Final(&md5, hash);
  1964. if (ret != 0)
  1965. ERROR_OUT(-1609, exit);
  1966. if (XMEMCMP(hash, large_digest, WC_MD5_DIGEST_SIZE) != 0)
  1967. ERROR_OUT(-1610, exit);
  1968. } /* END LARGE HASH TEST */
  1969. exit:
  1970. wc_Md5Free(&md5);
  1971. wc_Md5Free(&md5Copy);
  1972. return ret;
  1973. }
  1974. #endif /* NO_MD5 */
  1975. #ifndef NO_MD4
  1976. WOLFSSL_TEST_SUBROUTINE int md4_test(void)
  1977. {
  1978. Md4 md4;
  1979. byte hash[MD4_DIGEST_SIZE];
  1980. testVector a, b, c, d, e, f, g;
  1981. testVector test_md4[7];
  1982. int times = sizeof(test_md4) / sizeof(testVector), i;
  1983. a.input = "";
  1984. a.output = "\x31\xd6\xcf\xe0\xd1\x6a\xe9\x31\xb7\x3c\x59\xd7\xe0\xc0\x89"
  1985. "\xc0";
  1986. a.inLen = XSTRLEN(a.input);
  1987. a.outLen = MD4_DIGEST_SIZE;
  1988. b.input = "a";
  1989. b.output = "\xbd\xe5\x2c\xb3\x1d\xe3\x3e\x46\x24\x5e\x05\xfb\xdb\xd6\xfb"
  1990. "\x24";
  1991. b.inLen = XSTRLEN(b.input);
  1992. b.outLen = MD4_DIGEST_SIZE;
  1993. c.input = "abc";
  1994. c.output = "\xa4\x48\x01\x7a\xaf\x21\xd8\x52\x5f\xc1\x0a\xe8\x7a\xa6\x72"
  1995. "\x9d";
  1996. c.inLen = XSTRLEN(c.input);
  1997. c.outLen = MD4_DIGEST_SIZE;
  1998. d.input = "message digest";
  1999. d.output = "\xd9\x13\x0a\x81\x64\x54\x9f\xe8\x18\x87\x48\x06\xe1\xc7\x01"
  2000. "\x4b";
  2001. d.inLen = XSTRLEN(d.input);
  2002. d.outLen = MD4_DIGEST_SIZE;
  2003. e.input = "abcdefghijklmnopqrstuvwxyz";
  2004. e.output = "\xd7\x9e\x1c\x30\x8a\xa5\xbb\xcd\xee\xa8\xed\x63\xdf\x41\x2d"
  2005. "\xa9";
  2006. e.inLen = XSTRLEN(e.input);
  2007. e.outLen = MD4_DIGEST_SIZE;
  2008. f.input = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345"
  2009. "6789";
  2010. f.output = "\x04\x3f\x85\x82\xf2\x41\xdb\x35\x1c\xe6\x27\xe1\x53\xe7\xf0"
  2011. "\xe4";
  2012. f.inLen = XSTRLEN(f.input);
  2013. f.outLen = MD4_DIGEST_SIZE;
  2014. g.input = "1234567890123456789012345678901234567890123456789012345678"
  2015. "9012345678901234567890";
  2016. g.output = "\xe3\x3b\x4d\xdc\x9c\x38\xf2\x19\x9c\x3e\x7b\x16\x4f\xcc\x05"
  2017. "\x36";
  2018. g.inLen = XSTRLEN(g.input);
  2019. g.outLen = MD4_DIGEST_SIZE;
  2020. test_md4[0] = a;
  2021. test_md4[1] = b;
  2022. test_md4[2] = c;
  2023. test_md4[3] = d;
  2024. test_md4[4] = e;
  2025. test_md4[5] = f;
  2026. test_md4[6] = g;
  2027. wc_InitMd4(&md4);
  2028. for (i = 0; i < times; ++i) {
  2029. wc_Md4Update(&md4, (byte*)test_md4[i].input, (word32)test_md4[i].inLen);
  2030. wc_Md4Final(&md4, hash);
  2031. if (XMEMCMP(hash, test_md4[i].output, MD4_DIGEST_SIZE) != 0)
  2032. return -1700 - i;
  2033. }
  2034. return 0;
  2035. }
  2036. #endif /* NO_MD4 */
  2037. #ifndef NO_SHA
  2038. WOLFSSL_TEST_SUBROUTINE int sha_test(void)
  2039. {
  2040. int ret = 0;
  2041. wc_Sha sha, shaCopy;
  2042. byte hash[WC_SHA_DIGEST_SIZE];
  2043. byte hashcopy[WC_SHA_DIGEST_SIZE];
  2044. testVector a, b, c, d, e;
  2045. testVector test_sha[5];
  2046. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2047. a.input = "";
  2048. a.output = "\xda\x39\xa3\xee\x5e\x6b\x4b\x0d\x32\x55\xbf\xef\x95\x60\x18"
  2049. "\x90\xaf\xd8\x07\x09";
  2050. a.inLen = XSTRLEN(a.input);
  2051. a.outLen = WC_SHA_DIGEST_SIZE;
  2052. b.input = "abc";
  2053. b.output = "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E\x25\x71\x78\x50\xC2"
  2054. "\x6C\x9C\xD0\xD8\x9D";
  2055. b.inLen = XSTRLEN(b.input);
  2056. b.outLen = WC_SHA_DIGEST_SIZE;
  2057. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  2058. c.output = "\x84\x98\x3E\x44\x1C\x3B\xD2\x6E\xBA\xAE\x4A\xA1\xF9\x51\x29"
  2059. "\xE5\xE5\x46\x70\xF1";
  2060. c.inLen = XSTRLEN(c.input);
  2061. c.outLen = WC_SHA_DIGEST_SIZE;
  2062. d.input = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  2063. "aaaaaa";
  2064. d.output = "\x00\x98\xBA\x82\x4B\x5C\x16\x42\x7B\xD7\xA1\x12\x2A\x5A\x44"
  2065. "\x2A\x25\xEC\x64\x4D";
  2066. d.inLen = XSTRLEN(d.input);
  2067. d.outLen = WC_SHA_DIGEST_SIZE;
  2068. e.input = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  2069. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  2070. "aaaaaaaaaa";
  2071. e.output = "\xAD\x5B\x3F\xDB\xCB\x52\x67\x78\xC2\x83\x9D\x2F\x15\x1E\xA7"
  2072. "\x53\x99\x5E\x26\xA0";
  2073. e.inLen = XSTRLEN(e.input);
  2074. e.outLen = WC_SHA_DIGEST_SIZE;
  2075. test_sha[0] = a;
  2076. test_sha[1] = b;
  2077. test_sha[2] = c;
  2078. test_sha[3] = d;
  2079. test_sha[4] = e;
  2080. ret = wc_InitSha_ex(&sha, HEAP_HINT, devId);
  2081. if (ret != 0)
  2082. return -1800;
  2083. ret = wc_InitSha_ex(&shaCopy, HEAP_HINT, devId);
  2084. if (ret != 0) {
  2085. wc_ShaFree(&sha);
  2086. return -1801;
  2087. }
  2088. for (i = 0; i < times; ++i) {
  2089. ret = wc_ShaUpdate(&sha, (byte*)test_sha[i].input,
  2090. (word32)test_sha[i].inLen);
  2091. if (ret != 0)
  2092. ERROR_OUT(-1802 - i, exit);
  2093. ret = wc_ShaGetHash(&sha, hashcopy);
  2094. if (ret != 0)
  2095. ERROR_OUT(-1803 - i, exit);
  2096. ret = wc_ShaCopy(&sha, &shaCopy);
  2097. if (ret != 0)
  2098. ERROR_OUT(-1804 - i, exit);
  2099. ret = wc_ShaFinal(&sha, hash);
  2100. if (ret != 0)
  2101. ERROR_OUT(-1805 - i, exit);
  2102. wc_ShaFree(&shaCopy);
  2103. if (XMEMCMP(hash, test_sha[i].output, WC_SHA_DIGEST_SIZE) != 0)
  2104. ERROR_OUT(-1806 - i, exit);
  2105. if (XMEMCMP(hash, hashcopy, WC_SHA_DIGEST_SIZE) != 0)
  2106. ERROR_OUT(-1807 - i, exit);
  2107. }
  2108. /* BEGIN LARGE HASH TEST */ {
  2109. byte large_input[1024];
  2110. #if defined(WOLFSSL_RENESAS_TSIP) || defined(WOLFSSL_RENESAS_SCEPROTECT)
  2111. const char* large_digest =
  2112. "\x1d\x6a\x5a\xf6\xe5\x7c\x86\xce\x7f\x7c\xaf\xd5\xdb\x08\xcd\x59"
  2113. "\x15\x8c\x6d\xb6";
  2114. #else
  2115. const char* large_digest =
  2116. "\x8b\x77\x02\x48\x39\xe8\xdb\xd3\x9a\xf4\x05\x24\x66\x12\x2d\x9e"
  2117. "\xc5\xd9\x0a\xac";
  2118. #endif
  2119. for (i = 0; i < (int)sizeof(large_input); i++) {
  2120. large_input[i] = (byte)(i & 0xFF);
  2121. }
  2122. #if defined(WOLFSSL_RENESAS_TSIP) || defined(WOLFSSL_RENESAS_SCEPROTECT)
  2123. times = 20;
  2124. #else
  2125. times = 100;
  2126. #endif
  2127. #ifdef WOLFSSL_PIC32MZ_HASH
  2128. wc_ShaSizeSet(&sha, times * sizeof(large_input));
  2129. #endif
  2130. for (i = 0; i < times; ++i) {
  2131. ret = wc_ShaUpdate(&sha, (byte*)large_input,
  2132. (word32)sizeof(large_input));
  2133. if (ret != 0)
  2134. ERROR_OUT(-1808, exit);
  2135. }
  2136. ret = wc_ShaFinal(&sha, hash);
  2137. if (ret != 0)
  2138. ERROR_OUT(-1809, exit);
  2139. if (XMEMCMP(hash, large_digest, WC_SHA_DIGEST_SIZE) != 0)
  2140. ERROR_OUT(-1810, exit);
  2141. } /* END LARGE HASH TEST */
  2142. exit:
  2143. wc_ShaFree(&sha);
  2144. wc_ShaFree(&shaCopy);
  2145. return ret;
  2146. }
  2147. #endif /* NO_SHA */
  2148. #ifdef WOLFSSL_RIPEMD
  2149. WOLFSSL_TEST_SUBROUTINE int ripemd_test(void)
  2150. {
  2151. RipeMd ripemd;
  2152. int ret;
  2153. byte hash[RIPEMD_DIGEST_SIZE];
  2154. testVector a, b, c, d;
  2155. testVector test_ripemd[4];
  2156. int times = sizeof(test_ripemd) / sizeof(struct testVector), i;
  2157. a.input = "abc";
  2158. a.output = "\x8e\xb2\x08\xf7\xe0\x5d\x98\x7a\x9b\x04\x4a\x8e\x98\xc6"
  2159. "\xb0\x87\xf1\x5a\x0b\xfc";
  2160. a.inLen = XSTRLEN(a.input);
  2161. a.outLen = RIPEMD_DIGEST_SIZE;
  2162. b.input = "message digest";
  2163. b.output = "\x5d\x06\x89\xef\x49\xd2\xfa\xe5\x72\xb8\x81\xb1\x23\xa8"
  2164. "\x5f\xfa\x21\x59\x5f\x36";
  2165. b.inLen = XSTRLEN(b.input);
  2166. b.outLen = RIPEMD_DIGEST_SIZE;
  2167. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  2168. c.output = "\x12\xa0\x53\x38\x4a\x9c\x0c\x88\xe4\x05\xa0\x6c\x27\xdc"
  2169. "\xf4\x9a\xda\x62\xeb\x2b";
  2170. c.inLen = XSTRLEN(c.input);
  2171. c.outLen = RIPEMD_DIGEST_SIZE;
  2172. d.input = "12345678901234567890123456789012345678901234567890123456"
  2173. "789012345678901234567890";
  2174. d.output = "\x9b\x75\x2e\x45\x57\x3d\x4b\x39\xf4\xdb\xd3\x32\x3c\xab"
  2175. "\x82\xbf\x63\x32\x6b\xfb";
  2176. d.inLen = XSTRLEN(d.input);
  2177. d.outLen = RIPEMD_DIGEST_SIZE;
  2178. test_ripemd[0] = a;
  2179. test_ripemd[1] = b;
  2180. test_ripemd[2] = c;
  2181. test_ripemd[3] = d;
  2182. ret = wc_InitRipeMd(&ripemd);
  2183. if (ret != 0) {
  2184. return -1900;
  2185. }
  2186. for (i = 0; i < times; ++i) {
  2187. ret = wc_RipeMdUpdate(&ripemd, (byte*)test_ripemd[i].input,
  2188. (word32)test_ripemd[i].inLen);
  2189. if (ret != 0) {
  2190. return -1901 - i;
  2191. }
  2192. ret = wc_RipeMdFinal(&ripemd, hash);
  2193. if (ret != 0) {
  2194. return -1911 - i;
  2195. }
  2196. if (XMEMCMP(hash, test_ripemd[i].output, RIPEMD_DIGEST_SIZE) != 0)
  2197. return -1921 - i;
  2198. }
  2199. return 0;
  2200. }
  2201. #endif /* WOLFSSL_RIPEMD */
  2202. #ifdef HAVE_BLAKE2
  2203. #define BLAKE2B_TESTS 3
  2204. static const byte blake2b_vec[BLAKE2B_TESTS][BLAKE2B_OUTBYTES] =
  2205. {
  2206. {
  2207. 0x78, 0x6A, 0x02, 0xF7, 0x42, 0x01, 0x59, 0x03,
  2208. 0xC6, 0xC6, 0xFD, 0x85, 0x25, 0x52, 0xD2, 0x72,
  2209. 0x91, 0x2F, 0x47, 0x40, 0xE1, 0x58, 0x47, 0x61,
  2210. 0x8A, 0x86, 0xE2, 0x17, 0xF7, 0x1F, 0x54, 0x19,
  2211. 0xD2, 0x5E, 0x10, 0x31, 0xAF, 0xEE, 0x58, 0x53,
  2212. 0x13, 0x89, 0x64, 0x44, 0x93, 0x4E, 0xB0, 0x4B,
  2213. 0x90, 0x3A, 0x68, 0x5B, 0x14, 0x48, 0xB7, 0x55,
  2214. 0xD5, 0x6F, 0x70, 0x1A, 0xFE, 0x9B, 0xE2, 0xCE
  2215. },
  2216. {
  2217. 0x2F, 0xA3, 0xF6, 0x86, 0xDF, 0x87, 0x69, 0x95,
  2218. 0x16, 0x7E, 0x7C, 0x2E, 0x5D, 0x74, 0xC4, 0xC7,
  2219. 0xB6, 0xE4, 0x8F, 0x80, 0x68, 0xFE, 0x0E, 0x44,
  2220. 0x20, 0x83, 0x44, 0xD4, 0x80, 0xF7, 0x90, 0x4C,
  2221. 0x36, 0x96, 0x3E, 0x44, 0x11, 0x5F, 0xE3, 0xEB,
  2222. 0x2A, 0x3A, 0xC8, 0x69, 0x4C, 0x28, 0xBC, 0xB4,
  2223. 0xF5, 0xA0, 0xF3, 0x27, 0x6F, 0x2E, 0x79, 0x48,
  2224. 0x7D, 0x82, 0x19, 0x05, 0x7A, 0x50, 0x6E, 0x4B
  2225. },
  2226. {
  2227. 0x1C, 0x08, 0x79, 0x8D, 0xC6, 0x41, 0xAB, 0xA9,
  2228. 0xDE, 0xE4, 0x35, 0xE2, 0x25, 0x19, 0xA4, 0x72,
  2229. 0x9A, 0x09, 0xB2, 0xBF, 0xE0, 0xFF, 0x00, 0xEF,
  2230. 0x2D, 0xCD, 0x8E, 0xD6, 0xF8, 0xA0, 0x7D, 0x15,
  2231. 0xEA, 0xF4, 0xAE, 0xE5, 0x2B, 0xBF, 0x18, 0xAB,
  2232. 0x56, 0x08, 0xA6, 0x19, 0x0F, 0x70, 0xB9, 0x04,
  2233. 0x86, 0xC8, 0xA7, 0xD4, 0x87, 0x37, 0x10, 0xB1,
  2234. 0x11, 0x5D, 0x3D, 0xEB, 0xBB, 0x43, 0x27, 0xB5
  2235. }
  2236. };
  2237. WOLFSSL_TEST_SUBROUTINE int blake2b_test(void)
  2238. {
  2239. Blake2b b2b;
  2240. byte digest[64];
  2241. byte input[64];
  2242. int i, ret;
  2243. for (i = 0; i < (int)sizeof(input); i++)
  2244. input[i] = (byte)i;
  2245. for (i = 0; i < BLAKE2B_TESTS; i++) {
  2246. ret = wc_InitBlake2b(&b2b, 64);
  2247. if (ret != 0)
  2248. return -2000 - i;
  2249. ret = wc_Blake2bUpdate(&b2b, input, i);
  2250. if (ret != 0)
  2251. return -2010 - 1;
  2252. ret = wc_Blake2bFinal(&b2b, digest, 64);
  2253. if (ret != 0)
  2254. return -2020 - i;
  2255. if (XMEMCMP(digest, blake2b_vec[i], 64) != 0) {
  2256. return -2030 - i;
  2257. }
  2258. }
  2259. return 0;
  2260. }
  2261. #endif /* HAVE_BLAKE2 */
  2262. #ifdef HAVE_BLAKE2S
  2263. #define BLAKE2S_TESTS 3
  2264. static const byte blake2s_vec[BLAKE2S_TESTS][BLAKE2S_OUTBYTES] =
  2265. {
  2266. {
  2267. 0x69, 0x21, 0x7a, 0x30, 0x79, 0x90, 0x80, 0x94,
  2268. 0xe1, 0x11, 0x21, 0xd0, 0x42, 0x35, 0x4a, 0x7c,
  2269. 0x1f, 0x55, 0xb6, 0x48, 0x2c, 0xa1, 0xa5, 0x1e,
  2270. 0x1b, 0x25, 0x0d, 0xfd, 0x1e, 0xd0, 0xee, 0xf9,
  2271. },
  2272. {
  2273. 0xe3, 0x4d, 0x74, 0xdb, 0xaf, 0x4f, 0xf4, 0xc6,
  2274. 0xab, 0xd8, 0x71, 0xcc, 0x22, 0x04, 0x51, 0xd2,
  2275. 0xea, 0x26, 0x48, 0x84, 0x6c, 0x77, 0x57, 0xfb,
  2276. 0xaa, 0xc8, 0x2f, 0xe5, 0x1a, 0xd6, 0x4b, 0xea,
  2277. },
  2278. {
  2279. 0xdd, 0xad, 0x9a, 0xb1, 0x5d, 0xac, 0x45, 0x49,
  2280. 0xba, 0x42, 0xf4, 0x9d, 0x26, 0x24, 0x96, 0xbe,
  2281. 0xf6, 0xc0, 0xba, 0xe1, 0xdd, 0x34, 0x2a, 0x88,
  2282. 0x08, 0xf8, 0xea, 0x26, 0x7c, 0x6e, 0x21, 0x0c,
  2283. }
  2284. };
  2285. WOLFSSL_TEST_SUBROUTINE int blake2s_test(void)
  2286. {
  2287. Blake2s b2s;
  2288. byte digest[32];
  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 < BLAKE2S_TESTS; i++) {
  2294. ret = wc_InitBlake2s(&b2s, 32);
  2295. if (ret != 0)
  2296. return -2100 - i;
  2297. ret = wc_Blake2sUpdate(&b2s, input, i);
  2298. if (ret != 0)
  2299. return -2110 - 1;
  2300. ret = wc_Blake2sFinal(&b2s, digest, 32);
  2301. if (ret != 0)
  2302. return -2120 - i;
  2303. if (XMEMCMP(digest, blake2s_vec[i], 32) != 0) {
  2304. return -2130 - i;
  2305. }
  2306. }
  2307. return 0;
  2308. }
  2309. #endif /* HAVE_BLAKE2S */
  2310. #ifdef WOLFSSL_SHA224
  2311. WOLFSSL_TEST_SUBROUTINE int sha224_test(void)
  2312. {
  2313. wc_Sha224 sha, shaCopy;
  2314. byte hash[WC_SHA224_DIGEST_SIZE];
  2315. byte hashcopy[WC_SHA224_DIGEST_SIZE];
  2316. int ret = 0;
  2317. testVector a, b, c;
  2318. testVector test_sha[3];
  2319. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2320. a.input = "";
  2321. a.output = "\xd1\x4a\x02\x8c\x2a\x3a\x2b\xc9\x47\x61\x02\xbb\x28\x82\x34"
  2322. "\xc4\x15\xa2\xb0\x1f\x82\x8e\xa6\x2a\xc5\xb3\xe4\x2f";
  2323. a.inLen = XSTRLEN(a.input);
  2324. a.outLen = WC_SHA224_DIGEST_SIZE;
  2325. b.input = "abc";
  2326. b.output = "\x23\x09\x7d\x22\x34\x05\xd8\x22\x86\x42\xa4\x77\xbd\xa2\x55"
  2327. "\xb3\x2a\xad\xbc\xe4\xbd\xa0\xb3\xf7\xe3\x6c\x9d\xa7";
  2328. b.inLen = XSTRLEN(b.input);
  2329. b.outLen = WC_SHA224_DIGEST_SIZE;
  2330. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  2331. c.output = "\x75\x38\x8b\x16\x51\x27\x76\xcc\x5d\xba\x5d\xa1\xfd\x89\x01"
  2332. "\x50\xb0\xc6\x45\x5c\xb4\xf5\x8b\x19\x52\x52\x25\x25";
  2333. c.inLen = XSTRLEN(c.input);
  2334. c.outLen = WC_SHA224_DIGEST_SIZE;
  2335. test_sha[0] = a;
  2336. test_sha[1] = b;
  2337. test_sha[2] = c;
  2338. ret = wc_InitSha224_ex(&sha, HEAP_HINT, devId);
  2339. if (ret != 0)
  2340. return -2200;
  2341. ret = wc_InitSha224_ex(&shaCopy, HEAP_HINT, devId);
  2342. if (ret != 0) {
  2343. wc_Sha224Free(&sha);
  2344. return -2201;
  2345. }
  2346. for (i = 0; i < times; ++i) {
  2347. ret = wc_Sha224Update(&sha, (byte*)test_sha[i].input,
  2348. (word32)test_sha[i].inLen);
  2349. if (ret != 0)
  2350. ERROR_OUT(-2202 - i, exit);
  2351. ret = wc_Sha224GetHash(&sha, hashcopy);
  2352. if (ret != 0)
  2353. ERROR_OUT(-2203 - i, exit);
  2354. ret = wc_Sha224Copy(&sha, &shaCopy);
  2355. if (ret != 0)
  2356. ERROR_OUT(-2204 - i, exit);
  2357. ret = wc_Sha224Final(&sha, hash);
  2358. if (ret != 0)
  2359. ERROR_OUT(-2205 - i, exit);
  2360. wc_Sha224Free(&shaCopy);
  2361. if (XMEMCMP(hash, test_sha[i].output, WC_SHA224_DIGEST_SIZE) != 0)
  2362. ERROR_OUT(-2206 - i, exit);
  2363. if (XMEMCMP(hash, hashcopy, WC_SHA224_DIGEST_SIZE) != 0)
  2364. ERROR_OUT(-2207 - i, exit);
  2365. }
  2366. exit:
  2367. wc_Sha224Free(&sha);
  2368. wc_Sha224Free(&shaCopy);
  2369. return ret;
  2370. }
  2371. #endif
  2372. #ifndef NO_SHA256
  2373. WOLFSSL_TEST_SUBROUTINE int sha256_test(void)
  2374. {
  2375. wc_Sha256 sha, shaCopy;
  2376. byte hash[WC_SHA256_DIGEST_SIZE];
  2377. byte hashcopy[WC_SHA256_DIGEST_SIZE];
  2378. int ret = 0;
  2379. testVector a, b, c;
  2380. testVector test_sha[3];
  2381. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2382. a.input = "";
  2383. a.output = "\xe3\xb0\xc4\x42\x98\xfc\x1c\x14\x9a\xfb\xf4\xc8\x99\x6f\xb9"
  2384. "\x24\x27\xae\x41\xe4\x64\x9b\x93\x4c\xa4\x95\x99\x1b\x78\x52"
  2385. "\xb8\x55";
  2386. a.inLen = XSTRLEN(a.input);
  2387. a.outLen = WC_SHA256_DIGEST_SIZE;
  2388. b.input = "abc";
  2389. b.output = "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  2390. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  2391. "\x15\xAD";
  2392. b.inLen = XSTRLEN(b.input);
  2393. b.outLen = WC_SHA256_DIGEST_SIZE;
  2394. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  2395. c.output = "\x24\x8D\x6A\x61\xD2\x06\x38\xB8\xE5\xC0\x26\x93\x0C\x3E\x60"
  2396. "\x39\xA3\x3C\xE4\x59\x64\xFF\x21\x67\xF6\xEC\xED\xD4\x19\xDB"
  2397. "\x06\xC1";
  2398. c.inLen = XSTRLEN(c.input);
  2399. c.outLen = WC_SHA256_DIGEST_SIZE;
  2400. test_sha[0] = a;
  2401. test_sha[1] = b;
  2402. test_sha[2] = c;
  2403. ret = wc_InitSha256_ex(&sha, HEAP_HINT, devId);
  2404. if (ret != 0)
  2405. return -2300;
  2406. ret = wc_InitSha256_ex(&shaCopy, HEAP_HINT, devId);
  2407. if (ret != 0) {
  2408. wc_Sha256Free(&sha);
  2409. return -2301;
  2410. }
  2411. for (i = 0; i < times; ++i) {
  2412. ret = wc_Sha256Update(&sha, (byte*)test_sha[i].input,
  2413. (word32)test_sha[i].inLen);
  2414. if (ret != 0) {
  2415. ERROR_OUT(-2302 - i, exit);
  2416. }
  2417. ret = wc_Sha256GetHash(&sha, hashcopy);
  2418. if (ret != 0)
  2419. ERROR_OUT(-2303 - i, exit);
  2420. ret = wc_Sha256Copy(&sha, &shaCopy);
  2421. if (ret != 0)
  2422. ERROR_OUT(-2304 - i, exit);
  2423. ret = wc_Sha256Final(&sha, hash);
  2424. if (ret != 0)
  2425. ERROR_OUT(-2305 - i, exit);
  2426. wc_Sha256Free(&shaCopy);
  2427. if (XMEMCMP(hash, test_sha[i].output, WC_SHA256_DIGEST_SIZE) != 0)
  2428. ERROR_OUT(-2306 - i, exit);
  2429. if (XMEMCMP(hash, hashcopy, WC_SHA256_DIGEST_SIZE) != 0)
  2430. ERROR_OUT(-2307 - i, exit);
  2431. }
  2432. /* BEGIN LARGE HASH TEST */ {
  2433. byte large_input[1024];
  2434. #ifdef HASH_SIZE_LIMIT
  2435. const char* large_digest =
  2436. "\xa4\x75\x9e\x7a\xa2\x03\x38\x32\x88\x66\xa2\xea\x17\xea\xf8\xc7"
  2437. "\xfe\x4e\xc6\xbb\xe3\xbb\x71\xce\xe7\xdf\x7c\x04\x61\xb3\xc2\x2f";
  2438. #else
  2439. const char* large_digest =
  2440. "\x27\x78\x3e\x87\x96\x3a\x4e\xfb\x68\x29\xb5\x31\xc9\xba\x57\xb4"
  2441. "\x4f\x45\x79\x7f\x67\x70\xbd\x63\x7f\xbf\x0d\x80\x7c\xbd\xba\xe0";
  2442. #endif
  2443. for (i = 0; i < (int)sizeof(large_input); i++) {
  2444. large_input[i] = (byte)(i & 0xFF);
  2445. }
  2446. #ifdef HASH_SIZE_LIMIT
  2447. times = 20;
  2448. #else
  2449. times = 100;
  2450. #endif
  2451. #ifdef WOLFSSL_PIC32MZ_HASH
  2452. wc_Sha256SizeSet(&sha, times * sizeof(large_input));
  2453. #endif
  2454. for (i = 0; i < times; ++i) {
  2455. ret = wc_Sha256Update(&sha, (byte*)large_input,
  2456. (word32)sizeof(large_input));
  2457. if (ret != 0)
  2458. ERROR_OUT(-2308, exit);
  2459. }
  2460. ret = wc_Sha256Final(&sha, hash);
  2461. if (ret != 0)
  2462. ERROR_OUT(-2309, exit);
  2463. if (XMEMCMP(hash, large_digest, WC_SHA256_DIGEST_SIZE) != 0)
  2464. ERROR_OUT(-2310, exit);
  2465. } /* END LARGE HASH TEST */
  2466. exit:
  2467. wc_Sha256Free(&sha);
  2468. wc_Sha256Free(&shaCopy);
  2469. return ret;
  2470. }
  2471. #endif
  2472. #ifdef WOLFSSL_SHA512
  2473. WOLFSSL_TEST_SUBROUTINE int sha512_test(void)
  2474. {
  2475. wc_Sha512 sha, shaCopy;
  2476. byte hash[WC_SHA512_DIGEST_SIZE];
  2477. byte hashcopy[WC_SHA512_DIGEST_SIZE];
  2478. int ret = 0;
  2479. testVector a, b, c;
  2480. testVector test_sha[3];
  2481. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2482. a.input = "";
  2483. a.output = "\xcf\x83\xe1\x35\x7e\xef\xb8\xbd\xf1\x54\x28\x50\xd6\x6d\x80"
  2484. "\x07\xd6\x20\xe4\x05\x0b\x57\x15\xdc\x83\xf4\xa9\x21\xd3\x6c"
  2485. "\xe9\xce\x47\xd0\xd1\x3c\x5d\x85\xf2\xb0\xff\x83\x18\xd2\x87"
  2486. "\x7e\xec\x2f\x63\xb9\x31\xbd\x47\x41\x7a\x81\xa5\x38\x32\x7a"
  2487. "\xf9\x27\xda\x3e";
  2488. a.inLen = XSTRLEN(a.input);
  2489. a.outLen = WC_SHA512_DIGEST_SIZE;
  2490. b.input = "abc";
  2491. b.output = "\xdd\xaf\x35\xa1\x93\x61\x7a\xba\xcc\x41\x73\x49\xae\x20\x41"
  2492. "\x31\x12\xe6\xfa\x4e\x89\xa9\x7e\xa2\x0a\x9e\xee\xe6\x4b\x55"
  2493. "\xd3\x9a\x21\x92\x99\x2a\x27\x4f\xc1\xa8\x36\xba\x3c\x23\xa3"
  2494. "\xfe\xeb\xbd\x45\x4d\x44\x23\x64\x3c\xe8\x0e\x2a\x9a\xc9\x4f"
  2495. "\xa5\x4c\xa4\x9f";
  2496. b.inLen = XSTRLEN(b.input);
  2497. b.outLen = WC_SHA512_DIGEST_SIZE;
  2498. c.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  2499. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  2500. c.output = "\x8e\x95\x9b\x75\xda\xe3\x13\xda\x8c\xf4\xf7\x28\x14\xfc\x14"
  2501. "\x3f\x8f\x77\x79\xc6\xeb\x9f\x7f\xa1\x72\x99\xae\xad\xb6\x88"
  2502. "\x90\x18\x50\x1d\x28\x9e\x49\x00\xf7\xe4\x33\x1b\x99\xde\xc4"
  2503. "\xb5\x43\x3a\xc7\xd3\x29\xee\xb6\xdd\x26\x54\x5e\x96\xe5\x5b"
  2504. "\x87\x4b\xe9\x09";
  2505. c.inLen = XSTRLEN(c.input);
  2506. c.outLen = WC_SHA512_DIGEST_SIZE;
  2507. test_sha[0] = a;
  2508. test_sha[1] = b;
  2509. test_sha[2] = c;
  2510. ret = wc_InitSha512_ex(&sha, HEAP_HINT, devId);
  2511. if (ret != 0)
  2512. return -2400;
  2513. ret = wc_InitSha512_ex(&shaCopy, HEAP_HINT, devId);
  2514. if (ret != 0) {
  2515. wc_Sha512Free(&sha);
  2516. return -2401;
  2517. }
  2518. for (i = 0; i < times; ++i) {
  2519. ret = wc_Sha512Update(&sha, (byte*)test_sha[i].input,
  2520. (word32)test_sha[i].inLen);
  2521. if (ret != 0)
  2522. ERROR_OUT(-2402 - i, exit);
  2523. ret = wc_Sha512GetHash(&sha, hashcopy);
  2524. if (ret != 0)
  2525. ERROR_OUT(-2403 - i, exit);
  2526. ret = wc_Sha512Copy(&sha, &shaCopy);
  2527. if (ret != 0)
  2528. ERROR_OUT(-2404 - i, exit);
  2529. ret = wc_Sha512Final(&sha, hash);
  2530. if (ret != 0)
  2531. ERROR_OUT(-2405 - i, exit);
  2532. wc_Sha512Free(&shaCopy);
  2533. if (XMEMCMP(hash, test_sha[i].output, WC_SHA512_DIGEST_SIZE) != 0)
  2534. ERROR_OUT(-2406 - i, exit);
  2535. if (XMEMCMP(hash, hashcopy, WC_SHA512_DIGEST_SIZE) != 0)
  2536. ERROR_OUT(-2407 - i, exit);
  2537. }
  2538. /* BEGIN LARGE HASH TEST */ {
  2539. byte large_input[1024];
  2540. #ifdef HASH_SIZE_LIMIT
  2541. const char* large_digest =
  2542. "\x30\x9B\x96\xA6\xE9\x43\x78\x30\xA3\x71\x51\x61\xC1\xEB\xE1\xBE"
  2543. "\xC8\xA5\xF9\x13\x5A\xD6\x6D\x9E\x46\x31\x31\x67\x8D\xE2\xC0\x0B"
  2544. "\x2A\x1A\x03\xE1\xF3\x48\xA7\x33\xBD\x49\xF8\xFF\xF1\xC2\xC2\x95"
  2545. "\xCB\xF0\xAF\x87\x61\x85\x58\x63\x6A\xCA\x70\x9C\x8B\x83\x3F\x5D";
  2546. #else
  2547. const char* large_digest =
  2548. "\x5a\x1f\x73\x90\xbd\x8c\xe4\x63\x54\xce\xa0\x9b\xef\x32\x78\x2d"
  2549. "\x2e\xe7\x0d\x5e\x2f\x9d\x15\x1b\xdd\x2d\xde\x65\x0c\x7b\xfa\x83"
  2550. "\x5e\x80\x02\x13\x84\xb8\x3f\xff\x71\x62\xb5\x09\x89\x63\xe1\xdc"
  2551. "\xa5\xdc\xfc\xfa\x9d\x1a\x4d\xc0\xfa\x3a\x14\xf6\x01\x51\x90\xa4";
  2552. #endif
  2553. for (i = 0; i < (int)sizeof(large_input); i++) {
  2554. large_input[i] = (byte)(i & 0xFF);
  2555. }
  2556. #ifdef HASH_SIZE_LIMIT
  2557. times = 20;
  2558. #else
  2559. times = 100;
  2560. #endif
  2561. for (i = 0; i < times; ++i) {
  2562. ret = wc_Sha512Update(&sha, (byte*)large_input,
  2563. (word32)sizeof(large_input));
  2564. if (ret != 0)
  2565. ERROR_OUT(-2408, exit);
  2566. }
  2567. ret = wc_Sha512Final(&sha, hash);
  2568. if (ret != 0)
  2569. ERROR_OUT(-2409, exit);
  2570. if (XMEMCMP(hash, large_digest, WC_SHA512_DIGEST_SIZE) != 0)
  2571. ERROR_OUT(-2410, exit);
  2572. #ifndef NO_UNALIGNED_MEMORY_TEST
  2573. /* Unaligned memory access test */
  2574. for (i = 1; i < 16; i++) {
  2575. ret = wc_Sha512Update(&sha, (byte*)large_input + i,
  2576. (word32)sizeof(large_input) - i);
  2577. if (ret != 0)
  2578. ERROR_OUT(-2411, exit);
  2579. ret = wc_Sha512Final(&sha, hash);
  2580. }
  2581. #endif
  2582. } /* END LARGE HASH TEST */
  2583. exit:
  2584. wc_Sha512Free(&sha);
  2585. wc_Sha512Free(&shaCopy);
  2586. return ret;
  2587. }
  2588. #endif
  2589. #ifdef WOLFSSL_SHA384
  2590. WOLFSSL_TEST_SUBROUTINE int sha384_test(void)
  2591. {
  2592. wc_Sha384 sha, shaCopy;
  2593. byte hash[WC_SHA384_DIGEST_SIZE];
  2594. byte hashcopy[WC_SHA384_DIGEST_SIZE];
  2595. int ret = 0;
  2596. testVector a, b, c;
  2597. testVector test_sha[3];
  2598. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2599. a.input = "";
  2600. a.output = "\x38\xb0\x60\xa7\x51\xac\x96\x38\x4c\xd9\x32\x7e\xb1\xb1\xe3"
  2601. "\x6a\x21\xfd\xb7\x11\x14\xbe\x07\x43\x4c\x0c\xc7\xbf\x63\xf6"
  2602. "\xe1\xda\x27\x4e\xde\xbf\xe7\x6f\x65\xfb\xd5\x1a\xd2\xf1\x48"
  2603. "\x98\xb9\x5b";
  2604. a.inLen = XSTRLEN(a.input);
  2605. a.outLen = WC_SHA384_DIGEST_SIZE;
  2606. b.input = "abc";
  2607. b.output = "\xcb\x00\x75\x3f\x45\xa3\x5e\x8b\xb5\xa0\x3d\x69\x9a\xc6\x50"
  2608. "\x07\x27\x2c\x32\xab\x0e\xde\xd1\x63\x1a\x8b\x60\x5a\x43\xff"
  2609. "\x5b\xed\x80\x86\x07\x2b\xa1\xe7\xcc\x23\x58\xba\xec\xa1\x34"
  2610. "\xc8\x25\xa7";
  2611. b.inLen = XSTRLEN(b.input);
  2612. b.outLen = WC_SHA384_DIGEST_SIZE;
  2613. c.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  2614. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  2615. c.output = "\x09\x33\x0c\x33\xf7\x11\x47\xe8\x3d\x19\x2f\xc7\x82\xcd\x1b"
  2616. "\x47\x53\x11\x1b\x17\x3b\x3b\x05\xd2\x2f\xa0\x80\x86\xe3\xb0"
  2617. "\xf7\x12\xfc\xc7\xc7\x1a\x55\x7e\x2d\xb9\x66\xc3\xe9\xfa\x91"
  2618. "\x74\x60\x39";
  2619. c.inLen = XSTRLEN(c.input);
  2620. c.outLen = WC_SHA384_DIGEST_SIZE;
  2621. test_sha[0] = a;
  2622. test_sha[1] = b;
  2623. test_sha[2] = c;
  2624. ret = wc_InitSha384_ex(&sha, HEAP_HINT, devId);
  2625. if (ret != 0)
  2626. return -2500;
  2627. ret = wc_InitSha384_ex(&shaCopy, HEAP_HINT, devId);
  2628. if (ret != 0) {
  2629. wc_Sha384Free(&sha);
  2630. return -2501;
  2631. }
  2632. for (i = 0; i < times; ++i) {
  2633. ret = wc_Sha384Update(&sha, (byte*)test_sha[i].input,
  2634. (word32)test_sha[i].inLen);
  2635. if (ret != 0)
  2636. ERROR_OUT(-2502 - i, exit);
  2637. ret = wc_Sha384GetHash(&sha, hashcopy);
  2638. if (ret != 0)
  2639. ERROR_OUT(-2503 - i, exit);
  2640. ret = wc_Sha384Copy(&sha, &shaCopy);
  2641. if (ret != 0)
  2642. ERROR_OUT(-2504 - i, exit);
  2643. ret = wc_Sha384Final(&sha, hash);
  2644. if (ret != 0)
  2645. ERROR_OUT(-2505 - i, exit);
  2646. wc_Sha384Free(&shaCopy);
  2647. if (XMEMCMP(hash, test_sha[i].output, WC_SHA384_DIGEST_SIZE) != 0)
  2648. ERROR_OUT(-2506 - i, exit);
  2649. if (XMEMCMP(hash, hashcopy, WC_SHA384_DIGEST_SIZE) != 0)
  2650. ERROR_OUT(-2507 - i, exit);
  2651. }
  2652. /* BEGIN LARGE HASH TEST */ {
  2653. byte large_input[1024];
  2654. #ifdef HASH_SIZE_LIMIT
  2655. const char* large_digest =
  2656. "\xB5\xAD\x66\x6F\xD9\x58\x5E\x68\xDD\x5E\x30\xD3\x95\x72\x33\xA4"
  2657. "\xE9\x4B\x99\x3A\xEF\xF8\xE1\xBF\x1F\x05\x32\xAA\x16\x00\x82\xEC"
  2658. "\x15\xDA\xF2\x75\xEE\xE9\x06\xAF\x52\x8A\x5C\xEF\x72\x81\x80\xD6";
  2659. #else
  2660. const char* large_digest =
  2661. "\x37\x01\xdb\xff\x1e\x40\x4f\xe1\xe2\xea\x0b\x40\xbb\x3b\x39\x9a"
  2662. "\xcc\xe8\x44\x8e\x7e\xe5\x64\xb5\x6b\x7f\x56\x64\xa7\x2b\x84\xe3"
  2663. "\xc5\xd7\x79\x03\x25\x90\xf7\xa4\x58\xcb\x97\xa8\x8b\xb1\xa4\x81";
  2664. #endif
  2665. for (i = 0; i < (int)sizeof(large_input); i++) {
  2666. large_input[i] = (byte)(i & 0xFF);
  2667. }
  2668. #ifdef HASH_SIZE_LIMIT
  2669. times = 20;
  2670. #else
  2671. times = 100;
  2672. #endif
  2673. for (i = 0; i < times; ++i) {
  2674. ret = wc_Sha384Update(&sha, (byte*)large_input,
  2675. (word32)sizeof(large_input));
  2676. if (ret != 0)
  2677. ERROR_OUT(-2508, exit);
  2678. }
  2679. ret = wc_Sha384Final(&sha, hash);
  2680. if (ret != 0)
  2681. ERROR_OUT(-2509, exit);
  2682. if (XMEMCMP(hash, large_digest, WC_SHA384_DIGEST_SIZE) != 0)
  2683. ERROR_OUT(-2510, exit);
  2684. } /* END LARGE HASH TEST */
  2685. exit:
  2686. wc_Sha384Free(&sha);
  2687. wc_Sha384Free(&shaCopy);
  2688. return ret;
  2689. }
  2690. #endif /* WOLFSSL_SHA384 */
  2691. #ifdef WOLFSSL_SHA3
  2692. #ifndef WOLFSSL_NOSHA3_224
  2693. static int sha3_224_test(void)
  2694. {
  2695. wc_Sha3 sha;
  2696. byte hash[WC_SHA3_224_DIGEST_SIZE];
  2697. byte hashcopy[WC_SHA3_224_DIGEST_SIZE];
  2698. testVector a, b, c;
  2699. testVector test_sha[3];
  2700. int ret = 0;
  2701. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2702. a.input = "";
  2703. a.output = "\x6b\x4e\x03\x42\x36\x67\xdb\xb7\x3b\x6e\x15\x45\x4f\x0e\xb1"
  2704. "\xab\xd4\x59\x7f\x9a\x1b\x07\x8e\x3f\x5b\x5a\x6b\xc7";
  2705. a.inLen = XSTRLEN(a.input);
  2706. a.outLen = WC_SHA3_224_DIGEST_SIZE;
  2707. b.input = "abc";
  2708. b.output = "\xe6\x42\x82\x4c\x3f\x8c\xf2\x4a\xd0\x92\x34\xee\x7d\x3c\x76"
  2709. "\x6f\xc9\xa3\xa5\x16\x8d\x0c\x94\xad\x73\xb4\x6f\xdf";
  2710. b.inLen = XSTRLEN(b.input);
  2711. b.outLen = WC_SHA3_224_DIGEST_SIZE;
  2712. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  2713. c.output = "\x8a\x24\x10\x8b\x15\x4a\xda\x21\xc9\xfd\x55\x74\x49\x44\x79"
  2714. "\xba\x5c\x7e\x7a\xb7\x6e\xf2\x64\xea\xd0\xfc\xce\x33";
  2715. c.inLen = XSTRLEN(c.input);
  2716. c.outLen = WC_SHA3_224_DIGEST_SIZE;
  2717. test_sha[0] = a;
  2718. test_sha[1] = b;
  2719. test_sha[2] = c;
  2720. ret = wc_InitSha3_224(&sha, HEAP_HINT, devId);
  2721. if (ret != 0)
  2722. return -2600;
  2723. for (i = 0; i < times; ++i) {
  2724. ret = wc_Sha3_224_Update(&sha, (byte*)test_sha[i].input,
  2725. (word32)test_sha[i].inLen);
  2726. if (ret != 0)
  2727. ERROR_OUT(-2601 - i, exit);
  2728. ret = wc_Sha3_224_GetHash(&sha, hashcopy);
  2729. if (ret != 0)
  2730. ERROR_OUT(-2602 - i, exit);
  2731. ret = wc_Sha3_224_Final(&sha, hash);
  2732. if (ret != 0)
  2733. ERROR_OUT(-2603 - i, exit);
  2734. if (XMEMCMP(hash, test_sha[i].output, WC_SHA3_224_DIGEST_SIZE) != 0)
  2735. ERROR_OUT(-2604 - i, exit);
  2736. if (XMEMCMP(hash, hashcopy, WC_SHA3_224_DIGEST_SIZE) != 0)
  2737. ERROR_OUT(-2605 - i, exit);
  2738. }
  2739. /* BEGIN LARGE HASH TEST */ {
  2740. byte large_input[1024];
  2741. const char* large_digest =
  2742. "\x13\xe5\xd3\x98\x7b\x94\xda\x41\x12\xc7\x1e\x92\x3a\x19"
  2743. "\x21\x20\x86\x6f\x24\xbf\x0a\x31\xbc\xfd\xd6\x70\x36\xf3";
  2744. for (i = 0; i < (int)sizeof(large_input); i++) {
  2745. large_input[i] = (byte)(i & 0xFF);
  2746. }
  2747. times = 100;
  2748. for (i = 0; i < times; ++i) {
  2749. ret = wc_Sha3_224_Update(&sha, (byte*)large_input,
  2750. (word32)sizeof(large_input));
  2751. if (ret != 0)
  2752. ERROR_OUT(-2606, exit);
  2753. }
  2754. ret = wc_Sha3_224_Final(&sha, hash);
  2755. if (ret != 0)
  2756. ERROR_OUT(-2607, exit);
  2757. if (XMEMCMP(hash, large_digest, WC_SHA3_224_DIGEST_SIZE) != 0)
  2758. ERROR_OUT(-2608, exit);
  2759. } /* END LARGE HASH TEST */
  2760. exit:
  2761. wc_Sha3_224_Free(&sha);
  2762. return ret;
  2763. }
  2764. #endif /* WOLFSSL_NOSHA3_224 */
  2765. #ifndef WOLFSSL_NOSHA3_256
  2766. static int sha3_256_test(void)
  2767. {
  2768. wc_Sha3 sha;
  2769. byte hash[WC_SHA3_256_DIGEST_SIZE];
  2770. byte hashcopy[WC_SHA3_256_DIGEST_SIZE];
  2771. testVector a, b, c;
  2772. testVector test_sha[3];
  2773. int ret = 0;
  2774. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2775. byte large_input[1024];
  2776. const char* large_digest =
  2777. "\xdc\x90\xc0\xb1\x25\xdb\x2c\x34\x81\xa3\xff\xbc\x1e\x2e\x87\xeb"
  2778. "\x6d\x70\x85\x61\xe0\xe9\x63\x61\xff\xe5\x84\x4b\x1f\x68\x05\x15";
  2779. #if defined(WOLFSSL_HASH_FLAGS) && !defined(WOLFSSL_ASYNC_CRYPT)
  2780. /* test vector with hash of empty string */
  2781. const char* Keccak256EmptyOut =
  2782. "\xc5\xd2\x46\x01\x86\xf7\x23\x3c\x92\x7e\x7d\xb2\xdc\xc7\x03\xc0"
  2783. "\xe5\x00\xb6\x53\xca\x82\x27\x3b\x7b\xfa\xd8\x04\x5d\x85\xa4\x70";
  2784. #endif
  2785. a.input = "";
  2786. a.output = "\xa7\xff\xc6\xf8\xbf\x1e\xd7\x66\x51\xc1\x47\x56\xa0\x61\xd6"
  2787. "\x62\xf5\x80\xff\x4d\xe4\x3b\x49\xfa\x82\xd8\x0a\x4b\x80\xf8"
  2788. "\x43\x4a";
  2789. a.inLen = XSTRLEN(a.input);
  2790. a.outLen = WC_SHA3_256_DIGEST_SIZE;
  2791. b.input = "abc";
  2792. b.output = "\x3a\x98\x5d\xa7\x4f\xe2\x25\xb2\x04\x5c\x17\x2d\x6b\xd3\x90"
  2793. "\xbd\x85\x5f\x08\x6e\x3e\x9d\x52\x5b\x46\xbf\xe2\x45\x11\x43"
  2794. "\x15\x32";
  2795. b.inLen = XSTRLEN(b.input);
  2796. b.outLen = WC_SHA3_256_DIGEST_SIZE;
  2797. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  2798. c.output = "\x41\xc0\xdb\xa2\xa9\xd6\x24\x08\x49\x10\x03\x76\xa8\x23\x5e"
  2799. "\x2c\x82\xe1\xb9\x99\x8a\x99\x9e\x21\xdb\x32\xdd\x97\x49\x6d"
  2800. "\x33\x76";
  2801. c.inLen = XSTRLEN(c.input);
  2802. c.outLen = WC_SHA3_256_DIGEST_SIZE;
  2803. test_sha[0] = a;
  2804. test_sha[1] = b;
  2805. test_sha[2] = c;
  2806. ret = wc_InitSha3_256(&sha, HEAP_HINT, devId);
  2807. if (ret != 0)
  2808. return -2700;
  2809. for (i = 0; i < times; ++i) {
  2810. ret = wc_Sha3_256_Update(&sha, (byte*)test_sha[i].input,
  2811. (word32)test_sha[i].inLen);
  2812. if (ret != 0)
  2813. ERROR_OUT(-2701 - i, exit);
  2814. ret = wc_Sha3_256_GetHash(&sha, hashcopy);
  2815. if (ret != 0)
  2816. ERROR_OUT(-2702 - i, exit);
  2817. ret = wc_Sha3_256_Final(&sha, hash);
  2818. if (ret != 0)
  2819. ERROR_OUT(-2703 - i, exit);
  2820. if (XMEMCMP(hash, test_sha[i].output, WC_SHA3_256_DIGEST_SIZE) != 0)
  2821. ERROR_OUT(-2704 - i, exit);
  2822. if (XMEMCMP(hash, hashcopy, WC_SHA3_256_DIGEST_SIZE) != 0)
  2823. ERROR_OUT(-2705 - i, exit);
  2824. }
  2825. /* BEGIN LARGE HASH TEST */ {
  2826. for (i = 0; i < (int)sizeof(large_input); i++) {
  2827. large_input[i] = (byte)(i & 0xFF);
  2828. }
  2829. times = 100;
  2830. for (i = 0; i < times; ++i) {
  2831. ret = wc_Sha3_256_Update(&sha, (byte*)large_input,
  2832. (word32)sizeof(large_input));
  2833. if (ret != 0)
  2834. ERROR_OUT(-2706, exit);
  2835. }
  2836. ret = wc_Sha3_256_Final(&sha, hash);
  2837. if (ret != 0)
  2838. ERROR_OUT(-2707, exit);
  2839. if (XMEMCMP(hash, large_digest, WC_SHA3_256_DIGEST_SIZE) != 0)
  2840. ERROR_OUT(-2708, exit);
  2841. } /* END LARGE HASH TEST */
  2842. /* this is a software only variant of SHA3 not supported by external hardware devices */
  2843. #if defined(WOLFSSL_HASH_FLAGS) && !defined(WOLFSSL_ASYNC_CRYPT)
  2844. /* Test for Keccak256 */
  2845. ret = wc_Sha3_SetFlags(&sha, WC_HASH_SHA3_KECCAK256);
  2846. if (ret != 0) {
  2847. ERROR_OUT(-2709, exit);
  2848. }
  2849. ret = wc_Sha3_256_Update(&sha, (byte*)"", 0);
  2850. if (ret != 0) {
  2851. ERROR_OUT(-2710, exit);
  2852. }
  2853. ret = wc_Sha3_256_Final(&sha, hash);
  2854. if (ret != 0) {
  2855. ERROR_OUT(-2711, exit);
  2856. }
  2857. if (XMEMCMP(hash, Keccak256EmptyOut, WC_SHA3_256_DIGEST_SIZE) != 0) {
  2858. ERROR_OUT(-2712, exit);
  2859. }
  2860. #endif /* WOLFSSL_HASH_FLAGS && !WOLFSSL_ASYNC_CRYPT */
  2861. exit:
  2862. wc_Sha3_256_Free(&sha);
  2863. return ret;
  2864. }
  2865. #endif /* WOLFSSL_NOSHA3_256 */
  2866. #ifndef WOLFSSL_NOSHA3_384
  2867. static int sha3_384_test(void)
  2868. {
  2869. wc_Sha3 sha;
  2870. byte hash[WC_SHA3_384_DIGEST_SIZE];
  2871. #ifndef NO_INTM_HASH_TEST
  2872. byte hashcopy[WC_SHA3_384_DIGEST_SIZE];
  2873. #endif
  2874. testVector a, b, c;
  2875. testVector test_sha[3];
  2876. int ret;
  2877. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2878. a.input = "";
  2879. a.output = "\x0c\x63\xa7\x5b\x84\x5e\x4f\x7d\x01\x10\x7d\x85\x2e\x4c\x24"
  2880. "\x85\xc5\x1a\x50\xaa\xaa\x94\xfc\x61\x99\x5e\x71\xbb\xee\x98"
  2881. "\x3a\x2a\xc3\x71\x38\x31\x26\x4a\xdb\x47\xfb\x6b\xd1\xe0\x58"
  2882. "\xd5\xf0\x04";
  2883. a.inLen = XSTRLEN(a.input);
  2884. a.outLen = WC_SHA3_384_DIGEST_SIZE;
  2885. #if defined(WOLFSSL_AFALG_XILINX_SHA3) || defined(WOLFSSL_XILINX_CRYPT)
  2886. /* NIST test vector with a length that is a multiple of 4 */
  2887. b.input = "\x7d\x80\xb1\x60\xc4\xb5\x36\xa3\xbe\xb7\x99\x80\x59\x93\x44"
  2888. "\x04\x7c\x5f\x82\xa1\xdf\xc3\xee\xd4";
  2889. b.output = "\x04\x1c\xc5\x86\x1b\xa3\x34\x56\x3c\x61\xd4\xef\x97\x10\xd4"
  2890. "\x89\x6c\x31\x1c\x92\xed\xbe\x0d\x7c\xd5\x3e\x80\x3b\xf2\xf4"
  2891. "\xeb\x60\x57\x23\x55\x70\x77\x0c\xe8\x7c\x55\x20\xd7\xec\x14"
  2892. "\x19\x87\x22";
  2893. b.inLen = XSTRLEN(b.input);
  2894. b.outLen = WC_SHA3_384_DIGEST_SIZE;
  2895. #else
  2896. b.input = "abc";
  2897. b.output = "\xec\x01\x49\x82\x88\x51\x6f\xc9\x26\x45\x9f\x58\xe2\xc6\xad"
  2898. "\x8d\xf9\xb4\x73\xcb\x0f\xc0\x8c\x25\x96\xda\x7c\xf0\xe4\x9b"
  2899. "\xe4\xb2\x98\xd8\x8c\xea\x92\x7a\xc7\xf5\x39\xf1\xed\xf2\x28"
  2900. "\x37\x6d\x25";
  2901. b.inLen = XSTRLEN(b.input);
  2902. b.outLen = WC_SHA3_384_DIGEST_SIZE;
  2903. #endif
  2904. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  2905. c.output = "\x99\x1c\x66\x57\x55\xeb\x3a\x4b\x6b\xbd\xfb\x75\xc7\x8a\x49"
  2906. "\x2e\x8c\x56\xa2\x2c\x5c\x4d\x7e\x42\x9b\xfd\xbc\x32\xb9\xd4"
  2907. "\xad\x5a\xa0\x4a\x1f\x07\x6e\x62\xfe\xa1\x9e\xef\x51\xac\xd0"
  2908. "\x65\x7c\x22";
  2909. c.inLen = XSTRLEN(c.input);
  2910. c.outLen = WC_SHA3_384_DIGEST_SIZE;
  2911. #ifdef WOLFSSL_XILINX_CRYPT
  2912. test_sha[0] = b; /* hardware acc. can not handle "" string */
  2913. #else
  2914. test_sha[0] = a;
  2915. #endif
  2916. test_sha[1] = b;
  2917. test_sha[2] = c;
  2918. ret = wc_InitSha3_384(&sha, HEAP_HINT, devId);
  2919. if (ret != 0)
  2920. return -2800;
  2921. for (i = 0; i < times; ++i) {
  2922. ret = wc_Sha3_384_Update(&sha, (byte*)test_sha[i].input,
  2923. (word32)test_sha[i].inLen);
  2924. if (ret != 0)
  2925. ERROR_OUT(-2801 - i, exit);
  2926. #ifndef NO_INTM_HASH_TEST
  2927. ret = wc_Sha3_384_GetHash(&sha, hashcopy);
  2928. if (ret != 0)
  2929. ERROR_OUT(-2802 - i, exit);
  2930. #endif
  2931. ret = wc_Sha3_384_Final(&sha, hash);
  2932. if (ret != 0)
  2933. ERROR_OUT(-2803 - i, exit);
  2934. if (XMEMCMP(hash, test_sha[i].output, WC_SHA3_384_DIGEST_SIZE) != 0)
  2935. ERROR_OUT(-2804 - i, exit);
  2936. #ifndef NO_INTM_HASH_TEST
  2937. if (XMEMCMP(hash, hashcopy, WC_SHA3_384_DIGEST_SIZE) != 0)
  2938. ERROR_OUT(-2805 - i, exit);
  2939. #endif
  2940. }
  2941. /* BEGIN LARGE HASH TEST */ {
  2942. byte large_input[1024];
  2943. const char* large_digest =
  2944. "\x30\x44\xec\x17\xef\x47\x9f\x55\x36\x11\xd6\x3f\x8a\x31\x5a\x71"
  2945. "\x8a\x71\xa7\x1d\x8e\x84\xe8\x6c\x24\x02\x2f\x7a\x08\x4e\xea\xd7"
  2946. "\x42\x36\x5d\xa8\xc2\xb7\x42\xad\xec\x19\xfb\xca\xc6\x64\xb3\xa4";
  2947. for (i = 0; i < (int)sizeof(large_input); i++) {
  2948. large_input[i] = (byte)(i & 0xFF);
  2949. }
  2950. times = 100;
  2951. for (i = 0; i < times; ++i) {
  2952. ret = wc_Sha3_384_Update(&sha, (byte*)large_input,
  2953. (word32)sizeof(large_input));
  2954. if (ret != 0)
  2955. ERROR_OUT(-2806, exit);
  2956. }
  2957. ret = wc_Sha3_384_Final(&sha, hash);
  2958. if (ret != 0)
  2959. ERROR_OUT(-2807, exit);
  2960. if (XMEMCMP(hash, large_digest, WC_SHA3_384_DIGEST_SIZE) != 0)
  2961. ERROR_OUT(-2808, exit);
  2962. } /* END LARGE HASH TEST */
  2963. exit:
  2964. wc_Sha3_384_Free(&sha);
  2965. return ret;
  2966. }
  2967. #endif /* WOLFSSL_NOSHA3_384 */
  2968. #ifndef WOLFSSL_NOSHA3_512
  2969. static int sha3_512_test(void)
  2970. {
  2971. wc_Sha3 sha;
  2972. byte hash[WC_SHA3_512_DIGEST_SIZE];
  2973. byte hashcopy[WC_SHA3_512_DIGEST_SIZE];
  2974. testVector a, b, c;
  2975. testVector test_sha[3];
  2976. int ret;
  2977. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2978. a.input = "";
  2979. a.output = "\xa6\x9f\x73\xcc\xa2\x3a\x9a\xc5\xc8\xb5\x67\xdc\x18\x5a\x75"
  2980. "\x6e\x97\xc9\x82\x16\x4f\xe2\x58\x59\xe0\xd1\xdc\xc1\x47\x5c"
  2981. "\x80\xa6\x15\xb2\x12\x3a\xf1\xf5\xf9\x4c\x11\xe3\xe9\x40\x2c"
  2982. "\x3a\xc5\x58\xf5\x00\x19\x9d\x95\xb6\xd3\xe3\x01\x75\x85\x86"
  2983. "\x28\x1d\xcd\x26";
  2984. a.inLen = XSTRLEN(a.input);
  2985. a.outLen = WC_SHA3_512_DIGEST_SIZE;
  2986. b.input = "abc";
  2987. b.output = "\xb7\x51\x85\x0b\x1a\x57\x16\x8a\x56\x93\xcd\x92\x4b\x6b\x09"
  2988. "\x6e\x08\xf6\x21\x82\x74\x44\xf7\x0d\x88\x4f\x5d\x02\x40\xd2"
  2989. "\x71\x2e\x10\xe1\x16\xe9\x19\x2a\xf3\xc9\x1a\x7e\xc5\x76\x47"
  2990. "\xe3\x93\x40\x57\x34\x0b\x4c\xf4\x08\xd5\xa5\x65\x92\xf8\x27"
  2991. "\x4e\xec\x53\xf0";
  2992. b.inLen = XSTRLEN(b.input);
  2993. b.outLen = WC_SHA3_512_DIGEST_SIZE;
  2994. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  2995. c.output = "\x04\xa3\x71\xe8\x4e\xcf\xb5\xb8\xb7\x7c\xb4\x86\x10\xfc\xa8"
  2996. "\x18\x2d\xd4\x57\xce\x6f\x32\x6a\x0f\xd3\xd7\xec\x2f\x1e\x91"
  2997. "\x63\x6d\xee\x69\x1f\xbe\x0c\x98\x53\x02\xba\x1b\x0d\x8d\xc7"
  2998. "\x8c\x08\x63\x46\xb5\x33\xb4\x9c\x03\x0d\x99\xa2\x7d\xaf\x11"
  2999. "\x39\xd6\xe7\x5e";
  3000. c.inLen = XSTRLEN(c.input);
  3001. c.outLen = WC_SHA3_512_DIGEST_SIZE;
  3002. test_sha[0] = a;
  3003. test_sha[1] = b;
  3004. test_sha[2] = c;
  3005. ret = wc_InitSha3_512(&sha, HEAP_HINT, devId);
  3006. if (ret != 0)
  3007. return -2900;
  3008. for (i = 0; i < times; ++i) {
  3009. ret = wc_Sha3_512_Update(&sha, (byte*)test_sha[i].input,
  3010. (word32)test_sha[i].inLen);
  3011. if (ret != 0)
  3012. ERROR_OUT(-2901 - i, exit);
  3013. ret = wc_Sha3_512_GetHash(&sha, hashcopy);
  3014. if (ret != 0)
  3015. ERROR_OUT(-2902 - i, exit);
  3016. ret = wc_Sha3_512_Final(&sha, hash);
  3017. if (ret != 0)
  3018. ERROR_OUT(-2903 - i, exit);
  3019. if (XMEMCMP(hash, test_sha[i].output, WC_SHA3_512_DIGEST_SIZE) != 0)
  3020. ERROR_OUT(-2904 - i, exit);
  3021. if (XMEMCMP(hash, hashcopy, WC_SHA3_512_DIGEST_SIZE) != 0)
  3022. ERROR_OUT(-2905 - i, exit);
  3023. }
  3024. /* BEGIN LARGE HASH TEST */ {
  3025. byte large_input[1024];
  3026. const char* large_digest =
  3027. "\x9c\x13\x26\xb6\x26\xb2\x94\x31\xbc\xf4\x34\xe9\x6f\xf2\xd6\x29"
  3028. "\x9a\xd0\x9b\x32\x63\x2f\x18\xa7\x5f\x23\xc9\x60\xc2\x32\x0c\xbc"
  3029. "\x57\x77\x33\xf1\x83\x81\x8a\xd3\x15\x7c\x93\xdc\x80\x9f\xed\x61"
  3030. "\x41\xa7\x5b\xfd\x32\x0e\x38\x15\xb0\x46\x3b\x7a\x4f\xfd\x44\x88";
  3031. for (i = 0; i < (int)sizeof(large_input); i++) {
  3032. large_input[i] = (byte)(i & 0xFF);
  3033. }
  3034. times = 100;
  3035. for (i = 0; i < times; ++i) {
  3036. ret = wc_Sha3_512_Update(&sha, (byte*)large_input,
  3037. (word32)sizeof(large_input));
  3038. if (ret != 0)
  3039. ERROR_OUT(-2906, exit);
  3040. }
  3041. ret = wc_Sha3_512_Final(&sha, hash);
  3042. if (ret != 0)
  3043. ERROR_OUT(-2907, exit);
  3044. if (XMEMCMP(hash, large_digest, WC_SHA3_512_DIGEST_SIZE) != 0)
  3045. ERROR_OUT(-2908, exit);
  3046. } /* END LARGE HASH TEST */
  3047. exit:
  3048. wc_Sha3_512_Free(&sha);
  3049. return ret;
  3050. }
  3051. #endif /* WOLFSSL_NOSHA3_512 */
  3052. WOLFSSL_TEST_SUBROUTINE int sha3_test(void)
  3053. {
  3054. int ret;
  3055. (void)ret;
  3056. #ifndef WOLFSSL_NOSHA3_224
  3057. if ((ret = sha3_224_test()) != 0)
  3058. return ret;
  3059. #endif
  3060. #ifndef WOLFSSL_NOSHA3_256
  3061. if ((ret = sha3_256_test()) != 0)
  3062. return ret;
  3063. #endif
  3064. #ifndef WOLFSSL_NOSHA3_384
  3065. if ((ret = sha3_384_test()) != 0)
  3066. return ret;
  3067. #endif
  3068. #ifndef WOLFSSL_NOSHA3_512
  3069. if ((ret = sha3_512_test()) != 0)
  3070. return ret;
  3071. #endif
  3072. return 0;
  3073. }
  3074. #endif /* WOLFSSL_SHA3 */
  3075. #ifdef WOLFSSL_SHAKE128
  3076. static int shake128_absorb_test(wc_Shake* sha)
  3077. {
  3078. byte hash[WC_SHA3_128_BLOCK_SIZE*2];
  3079. testVector a, b, c, d, e;
  3080. testVector test_sha[5];
  3081. int ret = 0;
  3082. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  3083. byte large_input[1024];
  3084. const char* large_digest =
  3085. "\x2b\xd1\x69\x9f\xb3\x75\x40\x74\xb8\xb2\xd2\x0b\x92\x47\x9b\xfe"
  3086. "\xc9\x91\x48\xbe\xda\xa4\x09\xd7\x61\x35\x18\x05\x07\x71\xa5\x61"
  3087. "\x4d\xc4\x94\xad\xbe\x04\x7d\xad\x95\x2f\xeb\x2c\xc0\x10\x67\x43"
  3088. "\x40\xf1\x4a\x58\x1c\x54\xfa\x24\x1c\x1a\x4e\x8d\x9b\xbc\xea\xa7"
  3089. "\x32\xf2\x4c\xc7\x86\x05\x36\xdc\xb4\x42\xd8\x35\xd1\xb4\xa2\x79"
  3090. "\xa2\xe6\xee\x67\x4f\xbf\x2a\x93\x41\x88\x25\x56\x29\x90\x1a\x06"
  3091. "\xba\xfe\x9f\xa6\x1a\x74\xe8\x7e\x85\x4a\xc8\x58\x60\xb1\x7b\x18"
  3092. "\xdf\x77\x59\x46\x04\xc1\xff\x4b\x9b\xcb\xad\xfe\x91\x28\xf0\x01"
  3093. "\xc1\x33\xd0\x99\x99\x2e\x0c\x86\x84\x67\x4d\x37\xa4\x42\x45\x10"
  3094. "\xdc\x8f\xdb\x6f\xa6\x9b\xee\x8a\x60\xa5\x1f\x95\x3f\x8f\xf5\x31"
  3095. "\x4b\x1d\x48\x1e\x45\xff\x79\x5c\xbe\x72\xfc\x56\xed\x6d\x1a\x99"
  3096. "\x7f\x23\x7c\xd1\xa5\x50\x9e\xb0\x4d\x61\x37\xa5\xcb\x24\x71\x3b"
  3097. "\xa3\x60\x51\x2e\x80\x83\x8b\xe0\x55\x50\xa7\x1e\xcc\x9f\xac\x41"
  3098. "\x77\x2c\x79\x22\x30\x09\x1b\x1a\x83\x5b\x2c\x48\xdc\x09\x7d\x59"
  3099. "\x0d\xf0\x54\x17\xfb\x5e\x38\x68\xde\xdb\xc5\x93\xab\x17\x5f\x4b"
  3100. "\x4d\x6d\xf2\xc7\x4e\x15\x1e\x10\x76\xc4\xcb\x87\xd8\xb7\x9d\xa8"
  3101. "\xbf\xc5\x2e\x5e\xfc\xd3\x6c\x45\xd4\x5d\x72\x0f\x66\xeb\x67\x86"
  3102. "\xfa\x6c\xd6\x80\xa4\x23\xcb\x5d\xed\x3c\xde\xdc\x5b\x3d\xca\x95"
  3103. "\x43\x4b\xdc\xe8\x49\xd3\xe1\x01\xd4\xf1\xe4\x47\xcf\x56\xba\x71"
  3104. "\xb4\x69\xed\xe7\xdb\x0f\x89\xd6\xbb\xcd\x1a\xff\xb4\xbe\x72\x26"
  3105. "\xdc\x76\x79\xb3\x1a\x4b\xe6\x8d\x9b\x8e\xd9\xe9\xe6\xf9\xff\xa5";
  3106. a.input = "";
  3107. a.output = "\x7f\x9c\x2b\xa4\xe8\x8f\x82\x7d\x61\x60\x45\x50\x76\x05\x85"
  3108. "\x3e\xd7\x3b\x80\x93\xf6\xef\xbc\x88\xeb\x1a\x6e\xac\xfa\x66"
  3109. "\xef\x26\x3c\xb1\xee\xa9\x88\x00\x4b\x93\x10\x3c\xfb\x0a\xee"
  3110. "\xfd\x2a\x68\x6e\x01\xfa\x4a\x58\xe8\xa3\x63\x9c\xa8\xa1\xe3"
  3111. "\xf9\xae\x57\xe2\x35\xb8\xcc\x87\x3c\x23\xdc\x62\xb8\xd2\x60"
  3112. "\x16\x9a\xfa\x2f\x75\xab\x91\x6a\x58\xd9\x74\x91\x88\x35\xd2"
  3113. "\x5e\x6a\x43\x50\x85\xb2\xba\xdf\xd6\xdf\xaa\xc3\x59\xa5\xef"
  3114. "\xbb\x7b\xcc\x4b\x59\xd5\x38\xdf\x9a\x04\x30\x2e\x10\xc8\xbc"
  3115. "\x1c\xbf\x1a\x0b\x3a\x51\x20\xea\x17\xcd\xa7\xcf\xad\x76\x5f"
  3116. "\x56\x23\x47\x4d\x36\x8c\xcc\xa8\xaf\x00\x07\xcd\x9f\x5e\x4c"
  3117. "\x84\x9f\x16\x7a\x58\x0b\x14\xaa\xbd\xef\xae\xe7\xee\xf4\x7c"
  3118. "\xb0\xfc\xa9";
  3119. a.inLen = XSTRLEN(a.input);
  3120. a.outLen = WC_SHA3_128_BLOCK_SIZE;
  3121. b.input = "abc";
  3122. b.output = "\x58\x81\x09\x2d\xd8\x18\xbf\x5c\xf8\xa3\xdd\xb7\x93\xfb\xcb"
  3123. "\xa7\x40\x97\xd5\xc5\x26\xa6\xd3\x5f\x97\xb8\x33\x51\x94\x0f"
  3124. "\x2c\xc8\x44\xc5\x0a\xf3\x2a\xcd\x3f\x2c\xdd\x06\x65\x68\x70"
  3125. "\x6f\x50\x9b\xc1\xbd\xde\x58\x29\x5d\xae\x3f\x89\x1a\x9a\x0f"
  3126. "\xca\x57\x83\x78\x9a\x41\xf8\x61\x12\x14\xce\x61\x23\x94\xdf"
  3127. "\x28\x6a\x62\xd1\xa2\x25\x2a\xa9\x4d\xb9\xc5\x38\x95\x6c\x71"
  3128. "\x7d\xc2\xbe\xd4\xf2\x32\xa0\x29\x4c\x85\x7c\x73\x0a\xa1\x60"
  3129. "\x67\xac\x10\x62\xf1\x20\x1f\xb0\xd3\x77\xcf\xb9\xcd\xe4\xc6"
  3130. "\x35\x99\xb2\x7f\x34\x62\xbb\xa4\xa0\xed\x29\x6c\x80\x1f\x9f"
  3131. "\xf7\xf5\x73\x02\xbb\x30\x76\xee\x14\x5f\x97\xa3\x2a\xe6\x8e"
  3132. "\x76\xab\x66\xc4\x8d\x51\x67\x5b\xd4\x9a\xcc\x29\x08\x2f\x56"
  3133. "\x47\x58\x4e";
  3134. b.inLen = XSTRLEN(b.input);
  3135. b.outLen = WC_SHA3_128_BLOCK_SIZE;
  3136. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  3137. c.output = "\x1a\x96\x18\x2b\x50\xfb\x8c\x7e\x74\xe0\xa7\x07\x78\x8f\x55"
  3138. "\xe9\x82\x09\xb8\xd9\x1f\xad\xe8\xf3\x2f\x8d\xd5\xcf\xf7\xbf"
  3139. "\x21\xf5\x4e\xe5\xf1\x95\x50\x82\x5a\x6e\x07\x00\x30\x51\x9e"
  3140. "\x94\x42\x63\xac\x1c\x67\x65\x28\x70\x65\x62\x1f\x9f\xcb\x32"
  3141. "\x01\x72\x3e\x32\x23\xb6\x3a\x46\xc2\x93\x8a\xa9\x53\xba\x84"
  3142. "\x01\xd0\xea\x77\xb8\xd2\x64\x90\x77\x55\x66\x40\x7b\x95\x67"
  3143. "\x3c\x0f\x4c\xc1\xce\x9f\xd9\x66\x14\x8d\x7e\xfd\xff\x26\xbb"
  3144. "\xf9\xf4\x8a\x21\xc6\xda\x35\xbf\xaa\x54\x56\x54\xf7\x0a\xe5"
  3145. "\x86\xff\x10\x13\x14\x20\x77\x14\x83\xec\x92\xed\xab\x40\x8c"
  3146. "\x76\x7b\xf4\xc5\xb4\xff\xfa\xa8\x0c\x8c\xa2\x14\xd8\x4c\x4d"
  3147. "\xc7\x00\xd0\xc5\x06\x30\xb2\xff\xc3\x79\x3e\xa4\xd8\x72\x58"
  3148. "\xb4\xc9\x54";
  3149. c.inLen = XSTRLEN(c.input);
  3150. c.outLen = WC_SHA3_128_BLOCK_SIZE;
  3151. /* Taken from NIST CAVP test vectors - full rate output. */
  3152. d.input = "\xdc\x88\x6d\xf3\xf6\x9c\x49\x51\x3d\xe3\x62\x7e\x94\x81\xdb"
  3153. "\x58\x71\xe8\xee\x88\xeb\x9f\x99\x61\x15\x41\x93\x0a\x8b\xc8"
  3154. "\x85\xe0";
  3155. d.output = "\x93\x68\xf0\x15\x10\x92\x44\xeb\x02\x47\xfa\x3a\x0e\x57\xf5"
  3156. "\x2e\xa7\xd9\xeb\xa2\x3d\xae\x7a\x19\x7f\x0a\x29\xe9\x22\x55"
  3157. "\x06\x05\x98\x16\xb7\x84\x48\xb6\x49\x7a\x76\xeb\x96\x2d\xb3"
  3158. "\xf8\x4d\x37\x60\xf1\xfe\xb4\xbd\xc1\xfd\x4a\xc9\x4e\x91\x7a"
  3159. "\xc2\xea\x5e\x4f\x38\x37\x4a\xa5\x6e\x4f\x47\x67\xb8\xd7\x83"
  3160. "\x1b\x2d\x51\x49\x5a\xb8\xea\xb7\xc9\x82\x20\xaf\x13\x41\x5a"
  3161. "\x59\xbb\x7c\x17\x7a\xcd\x62\x8e\xf0\xff\xe3\x6c\xeb\x18\x59"
  3162. "\x5d\x14\x4c\xbf\x25\xef\xc0\x6c\xd9\x56\xa5\x78\x20\x6e\xa8"
  3163. "\xf9\x14\x5e\xf9\xce\x19\x50\x6a\x9d\x04\x4e\xc7\x00\x79\x9f"
  3164. "\xa1\x41\x9b\xaf\x60\x52\xc0\xc1\xb4\x45\xf8\x35\x17\x57\xb0"
  3165. "\xd0\x22\x87\x21\x89\xe2\xc0\x27\x3f\x82\xd9\x69\x69\x66\x3e"
  3166. "\x55\x4d\x09";
  3167. d.inLen = 32;
  3168. d.outLen = WC_SHA3_128_BLOCK_SIZE;
  3169. /* Taken from NIST CAVP test vectors - more than one output block. */
  3170. e.input = "\x8d\x80\x01\xe2\xc0\x96\xf1\xb8\x8e\x7c\x92\x24\xa0\x86\xef"
  3171. "\xd4\x79\x7f\xbf\x74\xa8\x03\x3a\x2d\x42\x2a\x2b\x6b\x8f\x67"
  3172. "\x47\xe4";
  3173. e.output = "\xe1\x7e\xab\x0d\xa4\x04\xf9\xb6\xac\xc0\x84\x97\x2f\xc5\x79"
  3174. "\xe8\x6d\xaa\x76\x10\xa5\xe1\x7c\x23\x2f\x79\x19\x83\x96\xfd"
  3175. "\x01\xc2\x4c\x34\xbb\x54\xf4\xb0\x1e\xf7\x40\xb4\x25\x33\x4a"
  3176. "\x55\xdd\x24\x81\x3d\xc8\xea\x86\xf5\x6e\xf7\x27\x67\x26\x2b"
  3177. "\xf2\x25\x74\x8c\xcc\x3d\x9f\x48\x6f\xfb\x72\x8f\x4e\xad\x29"
  3178. "\x60\xc9\x6c\x3e\x44\x63\x86\xea\xce\x21\x9c\x84\x28\x16\x11"
  3179. "\x63\x58\xb0\xf4\x2d\x7d\xff\xf7\xdd\x24\x11\xfa\x2a\x56\x79"
  3180. "\xfd\x7a\x94\x77\x45\x75\xba\xf9\xfc\xad\x68\xa1\x9e\x30\xd1"
  3181. "\x49\xb0\x59\xb5\x9c\x44\x6c\x4e\xdc\xa5\x9b\xc5\xa4\x79\x9d"
  3182. "\xc4\x65\xaa\x9e\x78\x2c\xed\x9f\x21\xc5\x5d\xe2\x42\xdd\x25"
  3183. "\xd0\xd9\xde\x60\xd0\x9f\xf8\x6a\xba\xf3\xa0\x3a\x76\x71\xb3"
  3184. "\x05\x42\xdf\xbe\x72\xfc\x56\xed\x6d\x1a\x99\x7f\x23\x7c\xd1"
  3185. "\xa5\x50\x9e\xb0\x4d\x61\x37\xa5\xcb\x24\x71\x3b\xa3\x60\x51"
  3186. "\x2e\x80\x83\x8b\xe0\x55\x50\xa7\x1e\xcc\x9f\xac\x41\x77\x2c"
  3187. "\x79\x22\x30\x09\x1b\x1a\x83\x5b\x2c\x48\xdc\x09\x7d\x59\x0d"
  3188. "\xf0\x54\x17\xfb\x5e\x38\x68\xde\xdb\xc5\x93\xab\x17\x5f\x4b"
  3189. "\x4d\x6d\xf2\xc7\x4e\x15\x1e\x10\x76\xc4\xcb\x87\xd8\xb7\x9d"
  3190. "\xa8\xbf\xc5\x2e\x5e\xfc\xd3\x6c\x45\xd4\x5d\x72\x0f\x66\xeb"
  3191. "\x67\x86\xfa\x6c\xd6\x80\xa4\x23\xcb\x5d\xed\x3c\xde\xdc\x5b"
  3192. "\x3d\xca\x95\x43\x4b\xdc\xe8\x49\xd3\xe1\x01\xd4\xf1\xe4\x47"
  3193. "\xcf\x56\xba\x71\xb4\x69\xed\xe7\xdb\x0f\x89\xd6\xbb\xcd\x1a"
  3194. "\xff\xb4\xbe\x72\x26\xdc\x76\x79\xb3\x1a\x4b\xe6\x8d\x9b\x8e"
  3195. "\xd9\xe9\xe6\xf9\xff\xa5";
  3196. e.inLen = 32;
  3197. e.outLen = 2 * WC_SHA3_128_BLOCK_SIZE;
  3198. test_sha[0] = a;
  3199. test_sha[1] = b;
  3200. test_sha[2] = c;
  3201. test_sha[3] = d;
  3202. test_sha[4] = e;
  3203. for (i = 0; i < times; ++i) {
  3204. ret = wc_InitShake128(sha, HEAP_HINT, devId);
  3205. if (ret != 0)
  3206. ERROR_OUT(-3100 - i, exit);
  3207. ret = wc_Shake128_Absorb(sha, (byte*)test_sha[i].input,
  3208. (word32)test_sha[i].inLen);
  3209. if (ret != 0)
  3210. ERROR_OUT(-3101 - i, exit);
  3211. ret = wc_Shake128_SqueezeBlocks(sha, hash,
  3212. (word32)test_sha[i].outLen / WC_SHA3_128_BLOCK_SIZE);
  3213. if (ret != 0)
  3214. ERROR_OUT(-3102 - i, exit);
  3215. if (XMEMCMP(hash, test_sha[i].output, (word32)test_sha[i].outLen) != 0)
  3216. ERROR_OUT(-3103 - i, exit);
  3217. }
  3218. /* BEGIN LARGE HASH TEST */ {
  3219. for (i = 0; i < (int)sizeof(large_input); i++) {
  3220. large_input[i] = (byte)(i & 0xFF);
  3221. }
  3222. ret = wc_InitShake128(sha, HEAP_HINT, devId);
  3223. if (ret != 0)
  3224. ERROR_OUT(-3104, exit);
  3225. /* Absorb is non-incremental. */
  3226. ret = wc_Shake128_Absorb(sha, (byte*)large_input,
  3227. (word32)sizeof(large_input));
  3228. if (ret != 0)
  3229. ERROR_OUT(-3105, exit);
  3230. /* Able to squeeze out blocks incrementally. */
  3231. ret = wc_Shake128_SqueezeBlocks(sha, hash, 1);
  3232. if (ret != 0)
  3233. ERROR_OUT(-3106, exit);
  3234. ret = wc_Shake128_SqueezeBlocks(sha, hash,
  3235. ((word32)sizeof(hash) / WC_SHA3_128_BLOCK_SIZE) - 1);
  3236. if (ret != 0)
  3237. ERROR_OUT(-3106, exit);
  3238. if (XMEMCMP(hash, large_digest, sizeof(hash)) != 0)
  3239. ERROR_OUT(-3107, exit);
  3240. } /* END LARGE HASH TEST */
  3241. exit:
  3242. return ret;
  3243. }
  3244. WOLFSSL_TEST_SUBROUTINE int shake128_test(void)
  3245. {
  3246. wc_Shake sha;
  3247. byte hash[250];
  3248. testVector a, b, c, d, e;
  3249. testVector test_sha[5];
  3250. int ret = 0;
  3251. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  3252. byte large_input[1024];
  3253. const char* large_digest =
  3254. "\x88\xd7\x0e\x86\x46\x72\x6b\x3d\x7d\x22\xe1\xa9\x2d\x02\xdb\x35"
  3255. "\x92\x4f\x1b\x03\x90\xee\xa3\xce\xd1\x3a\x08\x3a\xd7\x4e\x10\xdf"
  3256. "\x09\x67\x33\x35\x4f\xdd\x38\x50\x5b\xcb\x75\xc7\xba\x65\xe5\xe8"
  3257. "\xb8\x76\xde\xc5\xee\xd7\xf1\x65\x93\x4e\x5e\xc4\xb1\xd7\x6b\xee"
  3258. "\x4b\x57\x48\xf5\x38\x49\x9e\x45\xa0\xf7\x32\xe9\x05\x26\x6a\x10"
  3259. "\x70\xd4\x7c\x19\x01\x1f\x6d\x37\xba\x7b\x74\xc2\xbc\xb6\xbc\x74"
  3260. "\xa3\x66\x6c\x9b\x11\x84\x9d\x4a\x36\xbc\x8a\x0d\x4c\xe3\x39\xfa"
  3261. "\xfa\x1b";
  3262. a.input = "";
  3263. a.output = "\x7f\x9c\x2b\xa4\xe8\x8f\x82\x7d\x61\x60\x45\x50\x76\x05\x85"
  3264. "\x3e\xd7\x3b\x80\x93\xf6\xef\xbc\x88\xeb\x1a\x6e\xac\xfa\x66"
  3265. "\xef\x26\x3c\xb1\xee\xa9\x88\x00\x4b\x93\x10\x3c\xfb\x0a\xee"
  3266. "\xfd\x2a\x68\x6e\x01\xfa\x4a\x58\xe8\xa3\x63\x9c\xa8\xa1\xe3"
  3267. "\xf9\xae\x57\xe2\x35\xb8\xcc\x87\x3c\x23\xdc\x62\xb8\xd2\x60"
  3268. "\x16\x9a\xfa\x2f\x75\xab\x91\x6a\x58\xd9\x74\x91\x88\x35\xd2"
  3269. "\x5e\x6a\x43\x50\x85\xb2\xba\xdf\xd6\xdf\xaa\xc3\x59\xa5\xef"
  3270. "\xbb\x7b\xcc\x4b\x59\xd5\x38\xdf\x9a";
  3271. a.inLen = XSTRLEN(a.input);
  3272. a.outLen = 114;
  3273. b.input = "abc";
  3274. b.output = "\x58\x81\x09\x2d\xd8\x18\xbf\x5c\xf8\xa3\xdd\xb7\x93\xfb\xcb"
  3275. "\xa7\x40\x97\xd5\xc5\x26\xa6\xd3\x5f\x97\xb8\x33\x51\x94\x0f"
  3276. "\x2c\xc8\x44\xc5\x0a\xf3\x2a\xcd\x3f\x2c\xdd\x06\x65\x68\x70"
  3277. "\x6f\x50\x9b\xc1\xbd\xde\x58\x29\x5d\xae\x3f\x89\x1a\x9a\x0f"
  3278. "\xca\x57\x83\x78\x9a\x41\xf8\x61\x12\x14\xce\x61\x23\x94\xdf"
  3279. "\x28\x6a\x62\xd1\xa2\x25\x2a\xa9\x4d\xb9\xc5\x38\x95\x6c\x71"
  3280. "\x7d\xc2\xbe\xd4\xf2\x32\xa0\x29\x4c\x85\x7c\x73\x0a\xa1\x60"
  3281. "\x67\xac\x10\x62\xf1\x20\x1f\xb0\xd3";
  3282. b.inLen = XSTRLEN(b.input);
  3283. b.outLen = 114;
  3284. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  3285. c.output = "\x1a\x96\x18\x2b\x50\xfb\x8c\x7e\x74\xe0\xa7\x07\x78\x8f\x55"
  3286. "\xe9\x82\x09\xb8\xd9\x1f\xad\xe8\xf3\x2f\x8d\xd5\xcf\xf7\xbf"
  3287. "\x21\xf5\x4e\xe5\xf1\x95\x50\x82\x5a\x6e\x07\x00\x30\x51\x9e"
  3288. "\x94\x42\x63\xac\x1c\x67\x65\x28\x70\x65\x62\x1f\x9f\xcb\x32"
  3289. "\x01\x72\x3e\x32\x23\xb6\x3a\x46\xc2\x93\x8a\xa9\x53\xba\x84"
  3290. "\x01\xd0\xea\x77\xb8\xd2\x64\x90\x77\x55\x66\x40\x7b\x95\x67"
  3291. "\x3c\x0f\x4c\xc1\xce\x9f\xd9\x66\x14\x8d\x7e\xfd\xff\x26\xbb"
  3292. "\xf9\xf4\x8a\x21\xc6\xda\x35\xbf\xaa";
  3293. c.inLen = XSTRLEN(c.input);
  3294. c.outLen = 114;
  3295. /* Taken from NIST CAVP test vectors - full rate output. */
  3296. d.input = "\xdc\x88\x6d\xf3\xf6\x9c\x49\x51\x3d\xe3\x62\x7e\x94\x81\xdb"
  3297. "\x58\x71\xe8\xee\x88\xeb\x9f\x99\x61\x15\x41\x93\x0a\x8b\xc8"
  3298. "\x85\xe0";
  3299. d.output = "\x93\x68\xf0\x15\x10\x92\x44\xeb\x02\x47\xfa\x3a\x0e\x57\xf5"
  3300. "\x2e\xa7\xd9\xeb\xa2\x3d\xae\x7a\x19\x7f\x0a\x29\xe9\x22\x55"
  3301. "\x06\x05\x98\x16\xb7\x84\x48\xb6\x49\x7a\x76\xeb\x96\x2d\xb3"
  3302. "\xf8\x4d\x37\x60\xf1\xfe\xb4\xbd\xc1\xfd\x4a\xc9\x4e\x91\x7a"
  3303. "\xc2\xea\x5e\x4f\x38\x37\x4a\xa5\x6e\x4f\x47\x67\xb8\xd7\x83"
  3304. "\x1b\x2d\x51\x49\x5a\xb8\xea\xb7\xc9\x82\x20\xaf\x13\x41\x5a"
  3305. "\x59\xbb\x7c\x17\x7a\xcd\x62\x8e\xf0\xff\xe3\x6c\xeb\x18\x59"
  3306. "\x5d\x14\x4c\xbf\x25\xef\xc0\x6c\xd9\x56\xa5\x78\x20\x6e\xa8"
  3307. "\xf9\x14\x5e\xf9\xce\x19\x50\x6a\x9d\x04\x4e\xc7\x00\x79\x9f"
  3308. "\xa1";
  3309. d.inLen = 32;
  3310. d.outLen = 136;
  3311. /* Taken from NIST CAVP test vectors - more than one output block. */
  3312. e.input = "\x8d\x80\x01\xe2\xc0\x96\xf1\xb8\x8e\x7c\x92\x24\xa0\x86\xef"
  3313. "\xd4\x79\x7f\xbf\x74\xa8\x03\x3a\x2d\x42\x2a\x2b\x6b\x8f\x67"
  3314. "\x47\xe4";
  3315. e.output = "\xe1\x7e\xab\x0d\xa4\x04\xf9\xb6\xac\xc0\x84\x97\x2f\xc5\x79"
  3316. "\xe8\x6d\xaa\x76\x10\xa5\xe1\x7c\x23\x2f\x79\x19\x83\x96\xfd"
  3317. "\x01\xc2\x4c\x34\xbb\x54\xf4\xb0\x1e\xf7\x40\xb4\x25\x33\x4a"
  3318. "\x55\xdd\x24\x81\x3d\xc8\xea\x86\xf5\x6e\xf7\x27\x67\x26\x2b"
  3319. "\xf2\x25\x74\x8c\xcc\x3d\x9f\x48\x6f\xfb\x72\x8f\x4e\xad\x29"
  3320. "\x60\xc9\x6c\x3e\x44\x63\x86\xea\xce\x21\x9c\x84\x28\x16\x11"
  3321. "\x63\x58\xb0\xf4\x2d\x7d\xff\xf7\xdd\x24\x11\xfa\x2a\x56\x79"
  3322. "\xfd\x7a\x94\x77\x45\x75\xba\xf9\xfc\xad\x68\xa1\x9e\x30\xd1"
  3323. "\x49\xb0\x59\xb5\x9c\x44\x6c\x4e\xdc\xa5\x9b\xc5\xa4\x79\x9d"
  3324. "\xc4\x65\xaa\x9e\x78\x2c\xed\x9f\x21\xc5\x5d\xe2\x42\xdd\x25"
  3325. "\xd0\xd9\xde\x60\xd0\x9f\xf8\x6a\xba\xf3\xa0\x3a\x76\x71\xb3"
  3326. "\x05\x42\xdf\xbe\x72\xfc\x56\xed\x6d\x1a\x99\x7f\x23\x7c\xd1"
  3327. "\xa5\x50\x9e\xb0\x4d\x61\x37\xa5\xcb\x24\x71\x3b\xa3\x60\x51"
  3328. "\x2e\x80\x83\x8b\xe0\x55\x50\xa7\x1e\xcc\x9f\xac\x41\x77\x2c"
  3329. "\x79\x22\x30\x09\x1b\x1a\x83\x5b\x2c\x48\xdc\x09\x7d\x59\x0d"
  3330. "\xf0\x54\x17\xfb\x5e\x38\x68\xde\xdb\xc5\x93\xab\x17\x5f\x4b"
  3331. "\x4d\x6d\xf2\xc7\x4e\x15\x1e\x10\x76\xc4";
  3332. e.inLen = 32;
  3333. e.outLen = 250;
  3334. test_sha[0] = a;
  3335. test_sha[1] = b;
  3336. test_sha[2] = c;
  3337. test_sha[3] = d;
  3338. test_sha[4] = e;
  3339. ret = wc_InitShake128(&sha, HEAP_HINT, devId);
  3340. if (ret != 0)
  3341. return -3100;
  3342. for (i = 0; i < times; ++i) {
  3343. ret = wc_Shake128_Update(&sha, (byte*)test_sha[i].input,
  3344. (word32)test_sha[i].inLen);
  3345. if (ret != 0)
  3346. ERROR_OUT(-3101 - i, exit);
  3347. ret = wc_Shake128_Final(&sha, hash, (word32)test_sha[i].outLen);
  3348. if (ret != 0)
  3349. ERROR_OUT(-3102 - i, exit);
  3350. if (XMEMCMP(hash, test_sha[i].output, test_sha[i].outLen) != 0)
  3351. ERROR_OUT(-3103 - i, exit);
  3352. }
  3353. /* BEGIN LARGE HASH TEST */ {
  3354. for (i = 0; i < (int)sizeof(large_input); i++) {
  3355. large_input[i] = (byte)(i & 0xFF);
  3356. }
  3357. times = 100;
  3358. for (i = 0; i < times; ++i) {
  3359. ret = wc_Shake128_Update(&sha, (byte*)large_input,
  3360. (word32)sizeof(large_input));
  3361. if (ret != 0)
  3362. ERROR_OUT(-3104, exit);
  3363. }
  3364. ret = wc_Shake128_Final(&sha, hash, (word32)sizeof(hash));
  3365. if (ret != 0)
  3366. ERROR_OUT(-3105, exit);
  3367. if (XMEMCMP(hash, large_digest, 114) != 0)
  3368. ERROR_OUT(-3106, exit);
  3369. } /* END LARGE HASH TEST */
  3370. ret = shake128_absorb_test(&sha);
  3371. exit:
  3372. wc_Shake128_Free(&sha);
  3373. return ret;
  3374. }
  3375. #endif
  3376. #ifdef WOLFSSL_SHAKE256
  3377. static int shake256_absorb_test(wc_Shake* sha)
  3378. {
  3379. byte hash[WC_SHA3_256_BLOCK_SIZE*2];
  3380. testVector a, b, c, d, e;
  3381. testVector test_sha[5];
  3382. int ret = 0;
  3383. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  3384. byte large_input[1024];
  3385. const char* large_digest =
  3386. "\x21\x25\x8e\xae\x6e\x4f\xa7\xe1\xb9\x6d\xa7\xc9\x7d\x46\x03\x69"
  3387. "\x29\x0d\x81\x49\xba\x5d\xaf\x37\xfd\xeb\x25\x52\x1d\xd9\xbd\x65"
  3388. "\xfa\x99\xb9\xd1\x70\x6b\xeb\xd4\xc1\x2c\xea\x24\x20\x27\xa7\xcd"
  3389. "\xfa\xe1\x81\xd9\xd5\xc1\x1c\xc7\xe9\x70\xc3\xc7\x21\x6f\x32\x22"
  3390. "\xe3\x27\xdb\x58\x5e\xea\x18\x2d\x63\x4d\x14\x6c\x94\xcf\x2b\x7e"
  3391. "\x6e\x2a\x74\xf3\xe0\xac\xb3\xb2\xcc\xef\x38\xe9\xe7\x35\xb3\xc5"
  3392. "\x77\x9d\xff\xe3\x08\x8e\xf8\x2c\x89\xbb\x45\x22\x16\x99\x91\xc0"
  3393. "\xe7\x71\x57\x75\xc5\xb1\xc6\xaf\x27\xcb\x64\x8c\xc4\xee\x3d\x5f"
  3394. "\x4c\x35\xfb\x1c\xf3\xf8\x0e\xfd\x5e\xfc\x07\xd8\x4d\x55\x32\x49"
  3395. "\x45\x0d\xab\x4a\x49\xc4\x83\xde\xd2\x50\xc9\x33\x8f\x85\xcd\x93"
  3396. "\x7a\xe6\x6b\xb4\x36\xf3\xb4\x02\x6e\x85\x9f\xda\x1c\xa5\x71\x43"
  3397. "\x2f\x3b\xfc\x09\xe7\xc0\x3c\xa4\xd1\x83\xb7\x41\x11\x1c\xa0\x48"
  3398. "\x3d\x0e\xda\xbc\x03\xfe\xb2\x3b\x17\xee\x48\xe8\x44\xba\x24\x08"
  3399. "\xd9\xdc\xfd\x01\x39\xd2\xe8\xc7\x31\x01\x25\xae\xe8\x01\xc6\x1a"
  3400. "\xb7\x90\x0d\x1e\xfc\x47\xc0\x78\x28\x17\x66\xf3\x61\xc5\xe6\x11"
  3401. "\x13\x46\x23\x5e\x1d\xc3\x83\x25\x66\x6c\x68\x1b\x30\xdd\xc4\xe6"
  3402. "\x83\x8b\x0f\x23\x58\x7e\x06\x5f\x4a\x2b\xed\xc9\x6c\x97\x68\x44";
  3403. a.input = "";
  3404. a.output = "\x46\xb9\xdd\x2b\x0b\xa8\x8d\x13\x23\x3b\x3f\xeb\x74\x3e\xeb"
  3405. "\x24\x3f\xcd\x52\xea\x62\xb8\x1b\x82\xb5\x0c\x27\x64\x6e\xd5"
  3406. "\x76\x2f\xd7\x5d\xc4\xdd\xd8\xc0\xf2\x00\xcb\x05\x01\x9d\x67"
  3407. "\xb5\x92\xf6\xfc\x82\x1c\x49\x47\x9a\xb4\x86\x40\x29\x2e\xac"
  3408. "\xb3\xb7\xc4\xbe\x14\x1e\x96\x61\x6f\xb1\x39\x57\x69\x2c\xc7"
  3409. "\xed\xd0\xb4\x5a\xe3\xdc\x07\x22\x3c\x8e\x92\x93\x7b\xef\x84"
  3410. "\xbc\x0e\xab\x86\x28\x53\x34\x9e\xc7\x55\x46\xf5\x8f\xb7\xc2"
  3411. "\x77\x5c\x38\x46\x2c\x50\x10\xd8\x46\xc1\x85\xc1\x51\x11\xe5"
  3412. "\x95\x52\x2a\x6b\xcd\x16\xcf\x86\xf3\xd1\x22\x10\x9e\x3b\x1f"
  3413. "\xdd";
  3414. a.inLen = XSTRLEN(a.input);
  3415. a.outLen = WC_SHA3_256_BLOCK_SIZE;
  3416. b.input = "abc";
  3417. b.output = "\x48\x33\x66\x60\x13\x60\xa8\x77\x1c\x68\x63\x08\x0c\xc4\x11"
  3418. "\x4d\x8d\xb4\x45\x30\xf8\xf1\xe1\xee\x4f\x94\xea\x37\xe7\x8b"
  3419. "\x57\x39\xd5\xa1\x5b\xef\x18\x6a\x53\x86\xc7\x57\x44\xc0\x52"
  3420. "\x7e\x1f\xaa\x9f\x87\x26\xe4\x62\xa1\x2a\x4f\xeb\x06\xbd\x88"
  3421. "\x01\xe7\x51\xe4\x13\x85\x14\x12\x04\xf3\x29\x97\x9f\xd3\x04"
  3422. "\x7a\x13\xc5\x65\x77\x24\xad\xa6\x4d\x24\x70\x15\x7b\x3c\xdc"
  3423. "\x28\x86\x20\x94\x4d\x78\xdb\xcd\xdb\xd9\x12\x99\x3f\x09\x13"
  3424. "\xf1\x64\xfb\x2c\xe9\x51\x31\xa2\xd0\x9a\x3e\x6d\x51\xcb\xfc"
  3425. "\x62\x27\x20\xd7\xa7\x5c\x63\x34\xe8\xa2\xd7\xec\x71\xa7\xcc"
  3426. "\x29";
  3427. b.inLen = XSTRLEN(b.input);
  3428. b.outLen = WC_SHA3_256_BLOCK_SIZE;
  3429. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  3430. c.output = "\x4d\x8c\x2d\xd2\x43\x5a\x01\x28\xee\xfb\xb8\xc3\x6f\x6f\x87"
  3431. "\x13\x3a\x79\x11\xe1\x8d\x97\x9e\xe1\xae\x6b\xe5\xd4\xfd\x2e"
  3432. "\x33\x29\x40\xd8\x68\x8a\x4e\x6a\x59\xaa\x80\x60\xf1\xf9\xbc"
  3433. "\x99\x6c\x05\xac\xa3\xc6\x96\xa8\xb6\x62\x79\xdc\x67\x2c\x74"
  3434. "\x0b\xb2\x24\xec\x37\xa9\x2b\x65\xdb\x05\x39\xc0\x20\x34\x55"
  3435. "\xf5\x1d\x97\xcc\xe4\xcf\xc4\x91\x27\xd7\x26\x0a\xfc\x67\x3a"
  3436. "\xf2\x08\xba\xf1\x9b\xe2\x12\x33\xf3\xde\xbe\x78\xd0\x67\x60"
  3437. "\xcf\xa5\x51\xee\x1e\x07\x91\x41\xd4\x9d\xd3\xef\x7e\x18\x2b"
  3438. "\x15\x24\xdf\x82\xea\x1c\xef\xe1\xc6\xc3\x96\x61\x75\xf0\x22"
  3439. "\x8d";
  3440. c.inLen = XSTRLEN(c.input);
  3441. c.outLen = WC_SHA3_256_BLOCK_SIZE;
  3442. /* Taken from NIST CAVP test vectors - full rate output. */
  3443. d.input = "\xdc\x88\x6d\xf3\xf6\x9c\x49\x51\x3d\xe3\x62\x7e\x94\x81\xdb"
  3444. "\x58\x71\xe8\xee\x88\xeb\x9f\x99\x61\x15\x41\x93\x0a\x8b\xc8"
  3445. "\x85\xe0";
  3446. d.output = "\x00\x64\x8a\xfb\xc5\xe6\x51\x64\x9d\xb1\xfd\x82\x93\x6b\x00"
  3447. "\xdb\xbc\x12\x2f\xb4\xc8\x77\x86\x0d\x38\x5c\x49\x50\xd5\x6d"
  3448. "\xe7\xe0\x96\xd6\x13\xd7\xa3\xf2\x7e\xd8\xf2\x63\x34\xb0\xcc"
  3449. "\xc1\x40\x7b\x41\xdc\xcb\x23\xdf\xaa\x52\x98\x18\xd1\x12\x5c"
  3450. "\xd5\x34\x80\x92\x52\x43\x66\xb8\x5f\xab\xb9\x7c\x6c\xd1\xe6"
  3451. "\x06\x6f\x45\x9b\xcc\x56\x6d\xa8\x7e\xc9\xb7\xba\x36\x79\x2d"
  3452. "\x11\x8a\xc3\x9a\x4c\xce\xf6\x19\x2b\xbf\x3a\x54\xaf\x18\xe5"
  3453. "\x7b\x0c\x14\x61\x01\xf6\xae\xaa\x82\x2b\xc4\xb4\xc9\x70\x8b"
  3454. "\x09\xf0\xb3\xba\xb4\x1b\xcc\xe9\x64\xd9\x99\xd1\x10\x7b\xd7"
  3455. "\xc2";
  3456. d.inLen = 32;
  3457. d.outLen = WC_SHA3_256_BLOCK_SIZE;
  3458. /* Taken from NIST CAVP test vectors - more than one output block. */
  3459. e.input = "\x8d\x80\x01\xe2\xc0\x96\xf1\xb8\x8e\x7c\x92\x24\xa0\x86\xef"
  3460. "\xd4\x79\x7f\xbf\x74\xa8\x03\x3a\x2d\x42\x2a\x2b\x6b\x8f\x67"
  3461. "\x47\xe4";
  3462. e.output = "\x2e\x97\x5f\x6a\x8a\x14\xf0\x70\x4d\x51\xb1\x36\x67\xd8\x19"
  3463. "\x5c\x21\x9f\x71\xe6\x34\x56\x96\xc4\x9f\xa4\xb9\xd0\x8e\x92"
  3464. "\x25\xd3\xd3\x93\x93\x42\x51\x52\xc9\x7e\x71\xdd\x24\x60\x1c"
  3465. "\x11\xab\xcf\xa0\xf1\x2f\x53\xc6\x80\xbd\x3a\xe7\x57\xb8\x13"
  3466. "\x4a\x9c\x10\xd4\x29\x61\x58\x69\x21\x7f\xdd\x58\x85\xc4\xdb"
  3467. "\x17\x49\x85\x70\x3a\x6d\x6d\xe9\x4a\x66\x7e\xac\x30\x23\x44"
  3468. "\x3a\x83\x37\xae\x1b\xc6\x01\xb7\x6d\x7d\x38\xec\x3c\x34\x46"
  3469. "\x31\x05\xf0\xd3\x94\x9d\x78\xe5\x62\xa0\x39\xe4\x46\x95\x48"
  3470. "\xb6\x09\x39\x5d\xe5\xa4\xfd\x43\xc4\x6c\xa9\xfd\x6e\xe2\x9a"
  3471. "\xda\x5e\xfc\x07\xd8\x4d\x55\x32\x49\x45\x0d\xab\x4a\x49\xc4"
  3472. "\x83\xde\xd2\x50\xc9\x33\x8f\x85\xcd\x93\x7a\xe6\x6b\xb4\x36"
  3473. "\xf3\xb4\x02\x6e\x85\x9f\xda\x1c\xa5\x71\x43\x2f\x3b\xfc\x09"
  3474. "\xe7\xc0\x3c\xa4\xd1\x83\xb7\x41\x11\x1c\xa0\x48\x3d\x0e\xda"
  3475. "\xbc\x03\xfe\xb2\x3b\x17\xee\x48\xe8\x44\xba\x24\x08\xd9\xdc"
  3476. "\xfd\x01\x39\xd2\xe8\xc7\x31\x01\x25\xae\xe8\x01\xc6\x1a\xb7"
  3477. "\x90\x0d\x1e\xfc\x47\xc0\x78\x28\x17\x66\xf3\x61\xc5\xe6\x11"
  3478. "\x13\x46\x23\x5e\x1d\xc3\x83\x25\x66\x6c\x68\x1b\x30\xdd\xc4"
  3479. "\xe6\x83\x8b\x0f\x23\x58\x7e\x06\x5f\x4a\x2b\xed\xc9\x6c\x97"
  3480. "\x68\x44";
  3481. e.inLen = 32;
  3482. e.outLen = 2 * WC_SHA3_256_BLOCK_SIZE;
  3483. test_sha[0] = a;
  3484. test_sha[1] = b;
  3485. test_sha[2] = c;
  3486. test_sha[3] = d;
  3487. test_sha[4] = e;
  3488. for (i = 0; i < times; ++i) {
  3489. ret = wc_InitShake256(sha, HEAP_HINT, devId);
  3490. if (ret != 0)
  3491. ERROR_OUT(-3100 - i, exit);
  3492. ret = wc_Shake256_Absorb(sha, (byte*)test_sha[i].input,
  3493. (word32)test_sha[i].inLen);
  3494. if (ret != 0)
  3495. ERROR_OUT(-3101 - i, exit);
  3496. ret = wc_Shake256_SqueezeBlocks(sha, hash,
  3497. (word32)test_sha[i].outLen / WC_SHA3_256_BLOCK_SIZE);
  3498. if (ret != 0)
  3499. ERROR_OUT(-3102 - i, exit);
  3500. if (XMEMCMP(hash, test_sha[i].output, (word32)test_sha[i].outLen) != 0)
  3501. ERROR_OUT(-3103 - i, exit);
  3502. }
  3503. /* BEGIN LARGE HASH TEST */ {
  3504. for (i = 0; i < (int)sizeof(large_input); i++) {
  3505. large_input[i] = (byte)(i & 0xFF);
  3506. }
  3507. ret = wc_InitShake256(sha, HEAP_HINT, devId);
  3508. if (ret != 0)
  3509. ERROR_OUT(-3104, exit);
  3510. /* Absorb is non-incremental. */
  3511. ret = wc_Shake256_Absorb(sha, (byte*)large_input,
  3512. (word32)sizeof(large_input));
  3513. if (ret != 0)
  3514. ERROR_OUT(-3105, exit);
  3515. /* Able to squeeze out blocks incrementally. */
  3516. ret = wc_Shake256_SqueezeBlocks(sha, hash, 1);
  3517. if (ret != 0)
  3518. ERROR_OUT(-3106, exit);
  3519. ret = wc_Shake256_SqueezeBlocks(sha, hash,
  3520. ((word32)sizeof(hash) / WC_SHA3_256_BLOCK_SIZE) - 1);
  3521. if (ret != 0)
  3522. ERROR_OUT(-3106, exit);
  3523. if (XMEMCMP(hash, large_digest, sizeof(hash)) != 0)
  3524. ERROR_OUT(-3107, exit);
  3525. } /* END LARGE HASH TEST */
  3526. exit:
  3527. return ret;
  3528. }
  3529. WOLFSSL_TEST_SUBROUTINE int shake256_test(void)
  3530. {
  3531. wc_Shake sha;
  3532. byte hash[250];
  3533. testVector a, b, c, d, e;
  3534. testVector test_sha[5];
  3535. int ret = 0;
  3536. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  3537. byte large_input[1024];
  3538. const char* large_digest =
  3539. "\x90\x32\x4a\xcc\xd1\xdf\xb8\x0b\x79\x1f\xb8\xc8\x5b\x54\xc8\xe7"
  3540. "\x45\xf5\x60\x6b\x38\x26\xb2\x0a\xee\x38\x01\xf3\xd9\xfa\x96\x9f"
  3541. "\x6a\xd7\x15\xdf\xb6\xc2\xf4\x20\x33\x44\x55\xe8\x2a\x09\x2b\x68"
  3542. "\x2e\x18\x65\x5e\x65\x93\x28\xbc\xb1\x9e\xe2\xb1\x92\xea\x98\xac"
  3543. "\x21\xef\x4c\xe1\xb4\xb7\xbe\x81\x5c\x1d\xd3\xb7\x17\xe5\xbb\xc5"
  3544. "\x8c\x68\xb7\xfb\xac\x55\x8a\x9b\x4d\x91\xe4\x9f\x72\xbb\x6e\x38"
  3545. "\xaf\x21\x7d\x21\xaa\x98\x4e\x75\xc4\xb4\x1c\x7c\x50\x45\x54\xf9"
  3546. "\xea\x26";
  3547. a.input = "";
  3548. a.output = "\x46\xb9\xdd\x2b\x0b\xa8\x8d\x13\x23\x3b\x3f\xeb\x74\x3e\xeb"
  3549. "\x24\x3f\xcd\x52\xea\x62\xb8\x1b\x82\xb5\x0c\x27\x64\x6e\xd5"
  3550. "\x76\x2f\xd7\x5d\xc4\xdd\xd8\xc0\xf2\x00\xcb\x05\x01\x9d\x67"
  3551. "\xb5\x92\xf6\xfc\x82\x1c\x49\x47\x9a\xb4\x86\x40\x29\x2e\xac"
  3552. "\xb3\xb7\xc4\xbe\x14\x1e\x96\x61\x6f\xb1\x39\x57\x69\x2c\xc7"
  3553. "\xed\xd0\xb4\x5a\xe3\xdc\x07\x22\x3c\x8e\x92\x93\x7b\xef\x84"
  3554. "\xbc\x0e\xab\x86\x28\x53\x34\x9e\xc7\x55\x46\xf5\x8f\xb7\xc2"
  3555. "\x77\x5c\x38\x46\x2c\x50\x10\xd8\x46";
  3556. a.inLen = XSTRLEN(a.input);
  3557. a.outLen = 114;
  3558. b.input = "abc";
  3559. b.output = "\x48\x33\x66\x60\x13\x60\xa8\x77\x1c\x68\x63\x08\x0c\xc4\x11"
  3560. "\x4d\x8d\xb4\x45\x30\xf8\xf1\xe1\xee\x4f\x94\xea\x37\xe7\x8b"
  3561. "\x57\x39\xd5\xa1\x5b\xef\x18\x6a\x53\x86\xc7\x57\x44\xc0\x52"
  3562. "\x7e\x1f\xaa\x9f\x87\x26\xe4\x62\xa1\x2a\x4f\xeb\x06\xbd\x88"
  3563. "\x01\xe7\x51\xe4\x13\x85\x14\x12\x04\xf3\x29\x97\x9f\xd3\x04"
  3564. "\x7a\x13\xc5\x65\x77\x24\xad\xa6\x4d\x24\x70\x15\x7b\x3c\xdc"
  3565. "\x28\x86\x20\x94\x4d\x78\xdb\xcd\xdb\xd9\x12\x99\x3f\x09\x13"
  3566. "\xf1\x64\xfb\x2c\xe9\x51\x31\xa2\xd0";
  3567. b.inLen = XSTRLEN(b.input);
  3568. b.outLen = 114;
  3569. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  3570. c.output = "\x4d\x8c\x2d\xd2\x43\x5a\x01\x28\xee\xfb\xb8\xc3\x6f\x6f\x87"
  3571. "\x13\x3a\x79\x11\xe1\x8d\x97\x9e\xe1\xae\x6b\xe5\xd4\xfd\x2e"
  3572. "\x33\x29\x40\xd8\x68\x8a\x4e\x6a\x59\xaa\x80\x60\xf1\xf9\xbc"
  3573. "\x99\x6c\x05\xac\xa3\xc6\x96\xa8\xb6\x62\x79\xdc\x67\x2c\x74"
  3574. "\x0b\xb2\x24\xec\x37\xa9\x2b\x65\xdb\x05\x39\xc0\x20\x34\x55"
  3575. "\xf5\x1d\x97\xcc\xe4\xcf\xc4\x91\x27\xd7\x26\x0a\xfc\x67\x3a"
  3576. "\xf2\x08\xba\xf1\x9b\xe2\x12\x33\xf3\xde\xbe\x78\xd0\x67\x60"
  3577. "\xcf\xa5\x51\xee\x1e\x07\x91\x41\xd4";
  3578. c.inLen = XSTRLEN(c.input);
  3579. c.outLen = 114;
  3580. /* Taken from NIST CAVP test vectors - full rate output. */
  3581. d.input = "\xdc\x88\x6d\xf3\xf6\x9c\x49\x51\x3d\xe3\x62\x7e\x94\x81\xdb"
  3582. "\x58\x71\xe8\xee\x88\xeb\x9f\x99\x61\x15\x41\x93\x0a\x8b\xc8"
  3583. "\x85\xe0";
  3584. d.output = "\x00\x64\x8a\xfb\xc5\xe6\x51\x64\x9d\xb1\xfd\x82\x93\x6b\x00"
  3585. "\xdb\xbc\x12\x2f\xb4\xc8\x77\x86\x0d\x38\x5c\x49\x50\xd5\x6d"
  3586. "\xe7\xe0\x96\xd6\x13\xd7\xa3\xf2\x7e\xd8\xf2\x63\x34\xb0\xcc"
  3587. "\xc1\x40\x7b\x41\xdc\xcb\x23\xdf\xaa\x52\x98\x18\xd1\x12\x5c"
  3588. "\xd5\x34\x80\x92\x52\x43\x66\xb8\x5f\xab\xb9\x7c\x6c\xd1\xe6"
  3589. "\x06\x6f\x45\x9b\xcc\x56\x6d\xa8\x7e\xc9\xb7\xba\x36\x79\x2d"
  3590. "\x11\x8a\xc3\x9a\x4c\xce\xf6\x19\x2b\xbf\x3a\x54\xaf\x18\xe5"
  3591. "\x7b\x0c\x14\x61\x01\xf6\xae\xaa\x82\x2b\xc4\xb4\xc9\x70\x8b"
  3592. "\x09\xf0\xb3\xba\xb4\x1b\xcc\xe9\x64\xd9\x99\xd1\x10\x7b\xd7"
  3593. "\xc2";
  3594. d.inLen = 32;
  3595. d.outLen = 136;
  3596. /* Taken from NIST CAVP test vectors - more than one output block. */
  3597. e.input = "\x8d\x80\x01\xe2\xc0\x96\xf1\xb8\x8e\x7c\x92\x24\xa0\x86\xef"
  3598. "\xd4\x79\x7f\xbf\x74\xa8\x03\x3a\x2d\x42\x2a\x2b\x6b\x8f\x67"
  3599. "\x47\xe4";
  3600. e.output = "\x2e\x97\x5f\x6a\x8a\x14\xf0\x70\x4d\x51\xb1\x36\x67\xd8\x19"
  3601. "\x5c\x21\x9f\x71\xe6\x34\x56\x96\xc4\x9f\xa4\xb9\xd0\x8e\x92"
  3602. "\x25\xd3\xd3\x93\x93\x42\x51\x52\xc9\x7e\x71\xdd\x24\x60\x1c"
  3603. "\x11\xab\xcf\xa0\xf1\x2f\x53\xc6\x80\xbd\x3a\xe7\x57\xb8\x13"
  3604. "\x4a\x9c\x10\xd4\x29\x61\x58\x69\x21\x7f\xdd\x58\x85\xc4\xdb"
  3605. "\x17\x49\x85\x70\x3a\x6d\x6d\xe9\x4a\x66\x7e\xac\x30\x23\x44"
  3606. "\x3a\x83\x37\xae\x1b\xc6\x01\xb7\x6d\x7d\x38\xec\x3c\x34\x46"
  3607. "\x31\x05\xf0\xd3\x94\x9d\x78\xe5\x62\xa0\x39\xe4\x46\x95\x48"
  3608. "\xb6\x09\x39\x5d\xe5\xa4\xfd\x43\xc4\x6c\xa9\xfd\x6e\xe2\x9a"
  3609. "\xda\x5e\xfc\x07\xd8\x4d\x55\x32\x49\x45\x0d\xab\x4a\x49\xc4"
  3610. "\x83\xde\xd2\x50\xc9\x33\x8f\x85\xcd\x93\x7a\xe6\x6b\xb4\x36"
  3611. "\xf3\xb4\x02\x6e\x85\x9f\xda\x1c\xa5\x71\x43\x2f\x3b\xfc\x09"
  3612. "\xe7\xc0\x3c\xa4\xd1\x83\xb7\x41\x11\x1c\xa0\x48\x3d\x0e\xda"
  3613. "\xbc\x03\xfe\xb2\x3b\x17\xee\x48\xe8\x44\xba\x24\x08\xd9\xdc"
  3614. "\xfd\x01\x39\xd2\xe8\xc7\x31\x01\x25\xae\xe8\x01\xc6\x1a\xb7"
  3615. "\x90\x0d\x1e\xfc\x47\xc0\x78\x28\x17\x66\xf3\x61\xc5\xe6\x11"
  3616. "\x13\x46\x23\x5e\x1d\xc3\x83\x25\x66\x6c";
  3617. e.inLen = 32;
  3618. e.outLen = 250;
  3619. test_sha[0] = a;
  3620. test_sha[1] = b;
  3621. test_sha[2] = c;
  3622. test_sha[3] = d;
  3623. test_sha[4] = e;
  3624. ret = wc_InitShake256(&sha, HEAP_HINT, devId);
  3625. if (ret != 0)
  3626. return -3100;
  3627. for (i = 0; i < times; ++i) {
  3628. ret = wc_Shake256_Update(&sha, (byte*)test_sha[i].input,
  3629. (word32)test_sha[i].inLen);
  3630. if (ret != 0)
  3631. ERROR_OUT(-3101 - i, exit);
  3632. ret = wc_Shake256_Final(&sha, hash, (word32)test_sha[i].outLen);
  3633. if (ret != 0)
  3634. ERROR_OUT(-3102 - i, exit);
  3635. if (XMEMCMP(hash, test_sha[i].output, test_sha[i].outLen) != 0)
  3636. ERROR_OUT(-3103 - i, exit);
  3637. }
  3638. /* BEGIN LARGE HASH TEST */ {
  3639. for (i = 0; i < (int)sizeof(large_input); i++) {
  3640. large_input[i] = (byte)(i & 0xFF);
  3641. }
  3642. times = 100;
  3643. for (i = 0; i < times; ++i) {
  3644. ret = wc_Shake256_Update(&sha, (byte*)large_input,
  3645. (word32)sizeof(large_input));
  3646. if (ret != 0)
  3647. ERROR_OUT(-3104, exit);
  3648. }
  3649. ret = wc_Shake256_Final(&sha, hash, (word32)sizeof(hash));
  3650. if (ret != 0)
  3651. ERROR_OUT(-3105, exit);
  3652. if (XMEMCMP(hash, large_digest, 114) != 0)
  3653. ERROR_OUT(-3106, exit);
  3654. } /* END LARGE HASH TEST */
  3655. ret = shake256_absorb_test(&sha);
  3656. exit:
  3657. wc_Shake256_Free(&sha);
  3658. return ret;
  3659. }
  3660. #endif
  3661. #ifndef NO_HASH_WRAPPER
  3662. WOLFSSL_TEST_SUBROUTINE int hash_test(void)
  3663. {
  3664. wc_HashAlg hash;
  3665. int ret, exp_ret;
  3666. int i, j;
  3667. int digestSz;
  3668. byte data[] = "0123456789abcdef0123456789abcdef0123456";
  3669. byte out[WC_MAX_DIGEST_SIZE];
  3670. byte hashOut[WC_MAX_DIGEST_SIZE];
  3671. #if !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC)
  3672. enum wc_HashType hashType;
  3673. #endif
  3674. enum wc_HashType typesGood[] = { WC_HASH_TYPE_MD5, WC_HASH_TYPE_SHA,
  3675. WC_HASH_TYPE_SHA224, WC_HASH_TYPE_SHA256,
  3676. WC_HASH_TYPE_SHA384, WC_HASH_TYPE_SHA512,
  3677. WC_HASH_TYPE_SHA3_224,
  3678. WC_HASH_TYPE_SHA3_256,
  3679. WC_HASH_TYPE_SHA3_384,
  3680. WC_HASH_TYPE_SHA3_512 };
  3681. enum wc_HashType typesNoImpl[] = {
  3682. #ifdef NO_MD5
  3683. WC_HASH_TYPE_MD5,
  3684. #endif
  3685. #ifdef NO_SHA
  3686. WC_HASH_TYPE_SHA,
  3687. #endif
  3688. #ifndef WOLFSSL_SHA224
  3689. WC_HASH_TYPE_SHA224,
  3690. #endif
  3691. #ifdef NO_SHA256
  3692. WC_HASH_TYPE_SHA256,
  3693. #endif
  3694. #ifndef WOLFSSL_SHA384
  3695. WC_HASH_TYPE_SHA384,
  3696. #endif
  3697. #ifndef WOLFSSL_SHA512
  3698. WC_HASH_TYPE_SHA512,
  3699. #endif
  3700. #if !defined(WOLFSSL_SHA3) || defined(WOLFSSL_NOSHA3_224)
  3701. WC_HASH_TYPE_SHA3_224,
  3702. #endif
  3703. #if !defined(WOLFSSL_SHA3) || defined(WOLFSSL_NOSHA3_256)
  3704. WC_HASH_TYPE_SHA3_256,
  3705. #endif
  3706. #if !defined(WOLFSSL_SHA3) || defined(WOLFSSL_NOSHA3_384)
  3707. WC_HASH_TYPE_SHA3_384,
  3708. #endif
  3709. #if !defined(WOLFSSL_SHA3) || defined(WOLFSSL_NOSHA3_512)
  3710. WC_HASH_TYPE_SHA3_512,
  3711. #endif
  3712. WC_HASH_TYPE_NONE
  3713. };
  3714. enum wc_HashType typesBad[] = { WC_HASH_TYPE_NONE, WC_HASH_TYPE_MD5_SHA,
  3715. WC_HASH_TYPE_MD2, WC_HASH_TYPE_MD4 };
  3716. enum wc_HashType typesHashBad[] = { WC_HASH_TYPE_MD2, WC_HASH_TYPE_MD4,
  3717. WC_HASH_TYPE_BLAKE2B,
  3718. WC_HASH_TYPE_NONE };
  3719. /* Parameter Validation testing. */
  3720. ret = wc_HashInit(NULL, WC_HASH_TYPE_SHA256);
  3721. if (ret != BAD_FUNC_ARG)
  3722. return -3200;
  3723. ret = wc_HashUpdate(NULL, WC_HASH_TYPE_SHA256, NULL, sizeof(data));
  3724. if (ret != BAD_FUNC_ARG)
  3725. return -3201;
  3726. ret = wc_HashUpdate(&hash, WC_HASH_TYPE_SHA256, NULL, sizeof(data));
  3727. if (ret != BAD_FUNC_ARG)
  3728. return -3202;
  3729. ret = wc_HashUpdate(NULL, WC_HASH_TYPE_SHA256, data, sizeof(data));
  3730. if (ret != BAD_FUNC_ARG)
  3731. return -3203;
  3732. ret = wc_HashFinal(NULL, WC_HASH_TYPE_SHA256, NULL);
  3733. if (ret != BAD_FUNC_ARG)
  3734. return -3204;
  3735. ret = wc_HashFinal(&hash, WC_HASH_TYPE_SHA256, NULL);
  3736. if (ret != BAD_FUNC_ARG)
  3737. return -3205;
  3738. ret = wc_HashFinal(NULL, WC_HASH_TYPE_SHA256, out);
  3739. if (ret != BAD_FUNC_ARG)
  3740. return -3206;
  3741. /* Try invalid hash algorithms. */
  3742. for (i = 0; i < (int)(sizeof(typesBad)/sizeof(*typesBad)); i++) {
  3743. ret = wc_HashInit(&hash, typesBad[i]);
  3744. if (ret != BAD_FUNC_ARG)
  3745. return -3207 - i;
  3746. ret = wc_HashUpdate(&hash, typesBad[i], data, sizeof(data));
  3747. if (ret != BAD_FUNC_ARG)
  3748. return -3217 - i;
  3749. ret = wc_HashFinal(&hash, typesBad[i], out);
  3750. if (ret != BAD_FUNC_ARG)
  3751. return -3227 - i;
  3752. wc_HashFree(&hash, typesBad[i]);
  3753. }
  3754. /* Try valid hash algorithms. */
  3755. for (i = 0, j = 0; i < (int)(sizeof(typesGood)/sizeof(*typesGood)); i++) {
  3756. exp_ret = 0;
  3757. if (typesGood[i] == typesNoImpl[j]) {
  3758. /* Recognized but no implementation compiled in. */
  3759. exp_ret = HASH_TYPE_E;
  3760. j++;
  3761. }
  3762. ret = wc_HashInit(&hash, typesGood[i]);
  3763. if (ret != exp_ret)
  3764. return -3237 - i;
  3765. ret = wc_HashUpdate(&hash, typesGood[i], data, sizeof(data));
  3766. if (ret != exp_ret)
  3767. return -3247 - i;
  3768. ret = wc_HashFinal(&hash, typesGood[i], out);
  3769. if (ret != exp_ret)
  3770. return -3257 - i;
  3771. wc_HashFree(&hash, typesGood[i]);
  3772. digestSz = wc_HashGetDigestSize(typesGood[i]);
  3773. if (exp_ret < 0 && digestSz != exp_ret)
  3774. return -3267 - i;
  3775. if (exp_ret == 0 && digestSz < 0)
  3776. return -3277 - i;
  3777. if (exp_ret == 0) {
  3778. ret = wc_Hash(typesGood[i], data, sizeof(data), hashOut,
  3779. digestSz - 1);
  3780. if (ret != BUFFER_E)
  3781. return -3287 - i;
  3782. }
  3783. ret = wc_Hash(typesGood[i], data, sizeof(data), hashOut, digestSz);
  3784. if (ret != exp_ret)
  3785. return -3297 - i;
  3786. if (exp_ret == 0 && XMEMCMP(out, hashOut, digestSz) != 0)
  3787. return -3307 -i;
  3788. ret = wc_HashGetBlockSize(typesGood[i]);
  3789. if (exp_ret < 0 && ret != exp_ret)
  3790. return -3308 - i;
  3791. if (exp_ret == 0 && ret < 0)
  3792. return -3318 - i;
  3793. #if !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC)
  3794. ret = wc_HashGetOID(typesGood[i]);
  3795. if (ret == BAD_FUNC_ARG ||
  3796. (exp_ret == 0 && ret == HASH_TYPE_E) ||
  3797. (exp_ret != 0 && ret != HASH_TYPE_E)) {
  3798. return -3328 - i;
  3799. }
  3800. hashType = wc_OidGetHash(ret);
  3801. if (exp_ret == 0 && hashType != typesGood[i])
  3802. return -3338 - i;
  3803. #endif /* !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC) */
  3804. }
  3805. for (i = 0; i < (int)(sizeof(typesHashBad)/sizeof(*typesHashBad)); i++) {
  3806. ret = wc_Hash(typesHashBad[i], data, sizeof(data), out, sizeof(out));
  3807. if (ret != BAD_FUNC_ARG && ret != BUFFER_E)
  3808. return -3348 - i;
  3809. }
  3810. #if !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC)
  3811. ret = wc_HashGetOID(WC_HASH_TYPE_MD2);
  3812. #ifdef WOLFSSL_MD2
  3813. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  3814. return -3358;
  3815. #else
  3816. if (ret != HASH_TYPE_E)
  3817. return -3359;
  3818. #endif
  3819. hashType = wc_OidGetHash(646); /* Md2h */
  3820. #ifdef WOLFSSL_MD2
  3821. if (hashType != WC_HASH_TYPE_MD2)
  3822. return -3360;
  3823. #else
  3824. if (hashType != WC_HASH_TYPE_NONE)
  3825. return -3361;
  3826. #endif
  3827. ret = wc_HashGetOID(WC_HASH_TYPE_MD5_SHA);
  3828. #ifndef NO_MD5
  3829. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  3830. return -3362;
  3831. #else
  3832. if (ret != HASH_TYPE_E)
  3833. return -3363;
  3834. #endif
  3835. ret = wc_HashGetOID(WC_HASH_TYPE_MD4);
  3836. if (ret != BAD_FUNC_ARG)
  3837. return -3364;
  3838. ret = wc_HashGetOID(WC_HASH_TYPE_NONE);
  3839. if (ret != BAD_FUNC_ARG)
  3840. return -3365;
  3841. hashType = wc_OidGetHash(0);
  3842. if (hashType != WC_HASH_TYPE_NONE)
  3843. return -3366;
  3844. #endif /* !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC) */
  3845. ret = wc_HashGetBlockSize(WC_HASH_TYPE_MD2);
  3846. #ifdef WOLFSSL_MD2
  3847. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  3848. return -3367;
  3849. #else
  3850. if (ret != HASH_TYPE_E)
  3851. return -3368;
  3852. #endif
  3853. ret = wc_HashGetDigestSize(WC_HASH_TYPE_MD2);
  3854. #ifdef WOLFSSL_MD2
  3855. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  3856. return -3369;
  3857. #else
  3858. if (ret != HASH_TYPE_E)
  3859. return -3370;
  3860. #endif
  3861. ret = wc_HashGetBlockSize(WC_HASH_TYPE_MD4);
  3862. #ifndef NO_MD4
  3863. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  3864. return -3371;
  3865. #else
  3866. if (ret != HASH_TYPE_E)
  3867. return -3372;
  3868. #endif
  3869. ret = wc_HashGetDigestSize(WC_HASH_TYPE_MD4);
  3870. #ifndef NO_MD4
  3871. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  3872. return -3373;
  3873. #else
  3874. if (ret != HASH_TYPE_E)
  3875. return -3374;
  3876. #endif
  3877. ret = wc_HashGetBlockSize(WC_HASH_TYPE_MD5_SHA);
  3878. #if !defined(NO_MD5) && !defined(NO_SHA)
  3879. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  3880. return -3375;
  3881. #else
  3882. if (ret != HASH_TYPE_E)
  3883. return -3376;
  3884. #endif
  3885. ret = wc_HashGetBlockSize(WC_HASH_TYPE_BLAKE2B);
  3886. #if defined(HAVE_BLAKE2) || defined(HAVE_BLAKE2S)
  3887. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  3888. return -3377;
  3889. #else
  3890. if (ret != HASH_TYPE_E)
  3891. return -3378;
  3892. #endif
  3893. ret = wc_HashGetDigestSize(WC_HASH_TYPE_BLAKE2B);
  3894. #if defined(HAVE_BLAKE2) || defined(HAVE_BLAKE2S)
  3895. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  3896. return -3379;
  3897. #else
  3898. if (ret != HASH_TYPE_E)
  3899. return -3380;
  3900. #endif
  3901. ret = wc_HashGetBlockSize(WC_HASH_TYPE_NONE);
  3902. if (ret != BAD_FUNC_ARG)
  3903. return -3381;
  3904. ret = wc_HashGetDigestSize(WC_HASH_TYPE_NONE);
  3905. if (ret != BAD_FUNC_ARG)
  3906. return -3382;
  3907. #if !defined(NO_CERTS) && !defined(NO_ASN)
  3908. #if defined(WOLFSSL_MD2) && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  3909. ret = wc_GetCTC_HashOID(MD2);
  3910. if (ret == 0)
  3911. return -3383;
  3912. #endif
  3913. #ifndef NO_MD5
  3914. ret = wc_GetCTC_HashOID(WC_MD5);
  3915. if (ret == 0)
  3916. return -3384;
  3917. #endif
  3918. #ifndef NO_SHA
  3919. ret = wc_GetCTC_HashOID(WC_SHA);
  3920. if (ret == 0)
  3921. return -3385;
  3922. #endif
  3923. #ifdef WOLFSSL_SHA224
  3924. ret = wc_GetCTC_HashOID(WC_SHA224);
  3925. if (ret == 0)
  3926. return -3386;
  3927. #endif
  3928. #ifndef NO_SHA256
  3929. ret = wc_GetCTC_HashOID(WC_SHA256);
  3930. if (ret == 0)
  3931. return -3387;
  3932. #endif
  3933. #ifdef WOLFSSL_SHA384
  3934. ret = wc_GetCTC_HashOID(WC_SHA384);
  3935. if (ret == 0)
  3936. return -3388;
  3937. #endif
  3938. #ifdef WOLFSSL_SHA512
  3939. ret = wc_GetCTC_HashOID(WC_SHA512);
  3940. if (ret == 0)
  3941. return -3389;
  3942. #endif
  3943. ret = wc_GetCTC_HashOID(-1);
  3944. if (ret != 0)
  3945. return -3390;
  3946. #endif
  3947. return 0;
  3948. }
  3949. #endif /* !NO_HASH_WRAPPER */
  3950. #if !defined(NO_HMAC) && !defined(NO_MD5) && !(defined(HAVE_FIPS) && \
  3951. defined(HAVE_FIPS_VERSION) && \
  3952. (HAVE_FIPS_VERSION >= 5))
  3953. WOLFSSL_TEST_SUBROUTINE int hmac_md5_test(void)
  3954. {
  3955. Hmac hmac;
  3956. byte hash[WC_MD5_DIGEST_SIZE];
  3957. const char* keys[]=
  3958. {
  3959. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
  3960. "Jefe",
  3961. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  3962. };
  3963. testVector a, b, c;
  3964. testVector test_hmac[3];
  3965. int ret;
  3966. int times = sizeof(test_hmac) / sizeof(testVector), i;
  3967. a.input = "Hi There";
  3968. a.output = "\x92\x94\x72\x7a\x36\x38\xbb\x1c\x13\xf4\x8e\xf8\x15\x8b\xfc"
  3969. "\x9d";
  3970. a.inLen = XSTRLEN(a.input);
  3971. a.outLen = WC_MD5_DIGEST_SIZE;
  3972. b.input = "what do ya want for nothing?";
  3973. b.output = "\x75\x0c\x78\x3e\x6a\xb0\xb5\x03\xea\xa8\x6e\x31\x0a\x5d\xb7"
  3974. "\x38";
  3975. b.inLen = XSTRLEN(b.input);
  3976. b.outLen = WC_MD5_DIGEST_SIZE;
  3977. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  3978. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  3979. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  3980. "\xDD\xDD\xDD\xDD\xDD\xDD";
  3981. c.output = "\x56\xbe\x34\x52\x1d\x14\x4c\x88\xdb\xb8\xc7\x33\xf0\xe8\xb3"
  3982. "\xf6";
  3983. c.inLen = XSTRLEN(c.input);
  3984. c.outLen = WC_MD5_DIGEST_SIZE;
  3985. test_hmac[0] = a;
  3986. test_hmac[1] = b;
  3987. test_hmac[2] = c;
  3988. for (i = 0; i < times; ++i) {
  3989. #if defined(HAVE_FIPS) || defined(HAVE_CAVIUM)
  3990. if (i == 1) {
  3991. continue; /* cavium can't handle short keys, fips not allowed */
  3992. }
  3993. #endif
  3994. if (wc_HmacInit(&hmac, HEAP_HINT, devId) != 0) {
  3995. return -3400;
  3996. }
  3997. ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys[i],
  3998. (word32)XSTRLEN(keys[i]));
  3999. if (ret != 0)
  4000. return -3401;
  4001. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  4002. (word32)test_hmac[i].inLen);
  4003. if (ret != 0)
  4004. return -3402;
  4005. ret = wc_HmacFinal(&hmac, hash);
  4006. if (ret != 0)
  4007. return -3403;
  4008. if (XMEMCMP(hash, test_hmac[i].output, WC_MD5_DIGEST_SIZE) != 0)
  4009. return -3404 - i;
  4010. wc_HmacFree(&hmac);
  4011. }
  4012. #ifndef HAVE_FIPS
  4013. if (wc_HmacSizeByType(WC_MD5) != WC_MD5_DIGEST_SIZE)
  4014. return -3414;
  4015. #endif
  4016. return 0;
  4017. }
  4018. #endif /* !NO_HMAC && !NO_MD5 && (!HAVE_FIPS || (HAVE_FIPS_VERSION < 5)) */
  4019. #if !defined(NO_HMAC) && !defined(NO_SHA)
  4020. WOLFSSL_TEST_SUBROUTINE int hmac_sha_test(void)
  4021. {
  4022. Hmac hmac;
  4023. byte hash[WC_SHA_DIGEST_SIZE];
  4024. const char* keys[]=
  4025. {
  4026. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  4027. "\x0b\x0b\x0b",
  4028. "Jefe",
  4029. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  4030. "\xAA\xAA\xAA"
  4031. };
  4032. testVector a, b, c;
  4033. testVector test_hmac[3];
  4034. int ret;
  4035. int times = sizeof(test_hmac) / sizeof(testVector), i;
  4036. a.input = "Hi There";
  4037. a.output = "\xb6\x17\x31\x86\x55\x05\x72\x64\xe2\x8b\xc0\xb6\xfb\x37\x8c"
  4038. "\x8e\xf1\x46\xbe\x00";
  4039. a.inLen = XSTRLEN(a.input);
  4040. a.outLen = WC_SHA_DIGEST_SIZE;
  4041. b.input = "what do ya want for nothing?";
  4042. b.output = "\xef\xfc\xdf\x6a\xe5\xeb\x2f\xa2\xd2\x74\x16\xd5\xf1\x84\xdf"
  4043. "\x9c\x25\x9a\x7c\x79";
  4044. b.inLen = XSTRLEN(b.input);
  4045. b.outLen = WC_SHA_DIGEST_SIZE;
  4046. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4047. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4048. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4049. "\xDD\xDD\xDD\xDD\xDD\xDD";
  4050. c.output = "\x12\x5d\x73\x42\xb9\xac\x11\xcd\x91\xa3\x9a\xf4\x8a\xa1\x7b"
  4051. "\x4f\x63\xf1\x75\xd3";
  4052. c.inLen = XSTRLEN(c.input);
  4053. c.outLen = WC_SHA_DIGEST_SIZE;
  4054. test_hmac[0] = a;
  4055. test_hmac[1] = b;
  4056. test_hmac[2] = c;
  4057. for (i = 0; i < times; ++i) {
  4058. #if defined(HAVE_FIPS) || defined(HAVE_CAVIUM)
  4059. if (i == 1)
  4060. continue; /* cavium can't handle short keys, fips not allowed */
  4061. #endif
  4062. if (wc_HmacInit(&hmac, HEAP_HINT, devId) != 0)
  4063. return -3500;
  4064. ret = wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys[i],
  4065. (word32)XSTRLEN(keys[i]));
  4066. if (ret != 0)
  4067. return -3501;
  4068. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  4069. (word32)test_hmac[i].inLen);
  4070. if (ret != 0)
  4071. return -3502;
  4072. ret = wc_HmacFinal(&hmac, hash);
  4073. if (ret != 0)
  4074. return -3503;
  4075. if (XMEMCMP(hash, test_hmac[i].output, WC_SHA_DIGEST_SIZE) != 0)
  4076. return -3504 - i;
  4077. wc_HmacFree(&hmac);
  4078. }
  4079. #ifndef HAVE_FIPS
  4080. if (wc_HmacSizeByType(WC_SHA) != WC_SHA_DIGEST_SIZE)
  4081. return -3514;
  4082. #endif
  4083. return 0;
  4084. }
  4085. #endif
  4086. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
  4087. WOLFSSL_TEST_SUBROUTINE int hmac_sha224_test(void)
  4088. {
  4089. Hmac hmac;
  4090. byte hash[WC_SHA224_DIGEST_SIZE];
  4091. const char* keys[]=
  4092. {
  4093. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  4094. "\x0b\x0b\x0b",
  4095. "Jefe",
  4096. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  4097. "\xAA\xAA\xAA",
  4098. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4099. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4100. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4101. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4102. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4103. };
  4104. testVector a, b, c, d;
  4105. testVector test_hmac[4];
  4106. int ret;
  4107. int times = sizeof(test_hmac) / sizeof(testVector), i;
  4108. a.input = "Hi There";
  4109. a.output = "\x89\x6f\xb1\x12\x8a\xbb\xdf\x19\x68\x32\x10\x7c\xd4\x9d\xf3"
  4110. "\x3f\x47\xb4\xb1\x16\x99\x12\xba\x4f\x53\x68\x4b\x22";
  4111. a.inLen = XSTRLEN(a.input);
  4112. a.outLen = WC_SHA224_DIGEST_SIZE;
  4113. b.input = "what do ya want for nothing?";
  4114. b.output = "\xa3\x0e\x01\x09\x8b\xc6\xdb\xbf\x45\x69\x0f\x3a\x7e\x9e\x6d"
  4115. "\x0f\x8b\xbe\xa2\xa3\x9e\x61\x48\x00\x8f\xd0\x5e\x44";
  4116. b.inLen = XSTRLEN(b.input);
  4117. b.outLen = WC_SHA224_DIGEST_SIZE;
  4118. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4119. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4120. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4121. "\xDD\xDD\xDD\xDD\xDD\xDD";
  4122. c.output = "\x7f\xb3\xcb\x35\x88\xc6\xc1\xf6\xff\xa9\x69\x4d\x7d\x6a\xd2"
  4123. "\x64\x93\x65\xb0\xc1\xf6\x5d\x69\xd1\xec\x83\x33\xea";
  4124. c.inLen = XSTRLEN(c.input);
  4125. c.outLen = WC_SHA224_DIGEST_SIZE;
  4126. d.input = "Big Key Input";
  4127. d.output = "\xe7\x4e\x2b\x8a\xa9\xf0\x37\x2f\xed\xae\x70\x0c\x49\x47\xf1"
  4128. "\x46\x54\xa7\x32\x6b\x55\x01\x87\xd2\xc8\x02\x0e\x3a";
  4129. d.inLen = XSTRLEN(d.input);
  4130. d.outLen = WC_SHA224_DIGEST_SIZE;
  4131. test_hmac[0] = a;
  4132. test_hmac[1] = b;
  4133. test_hmac[2] = c;
  4134. test_hmac[3] = d;
  4135. for (i = 0; i < times; ++i) {
  4136. #if defined(HAVE_FIPS) || defined(HAVE_CAVIUM)
  4137. if (i == 1)
  4138. continue; /* cavium can't handle short keys, fips not allowed */
  4139. #endif
  4140. if (wc_HmacInit(&hmac, HEAP_HINT, devId) != 0)
  4141. return -3600;
  4142. ret = wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys[i],
  4143. (word32)XSTRLEN(keys[i]));
  4144. if (ret != 0)
  4145. return -3601;
  4146. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  4147. (word32)test_hmac[i].inLen);
  4148. if (ret != 0)
  4149. return -3602;
  4150. ret = wc_HmacFinal(&hmac, hash);
  4151. if (ret != 0)
  4152. return -3603;
  4153. if (XMEMCMP(hash, test_hmac[i].output, WC_SHA224_DIGEST_SIZE) != 0)
  4154. return -3604 - i;
  4155. wc_HmacFree(&hmac);
  4156. }
  4157. #ifndef HAVE_FIPS
  4158. if (wc_HmacSizeByType(WC_SHA224) != WC_SHA224_DIGEST_SIZE)
  4159. return -3614;
  4160. #endif
  4161. return 0;
  4162. }
  4163. #endif
  4164. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  4165. WOLFSSL_TEST_SUBROUTINE int hmac_sha256_test(void)
  4166. {
  4167. Hmac hmac;
  4168. byte hash[WC_SHA256_DIGEST_SIZE];
  4169. const char* keys[]=
  4170. {
  4171. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  4172. "\x0b\x0b\x0b",
  4173. "Jefe",
  4174. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  4175. "\xAA\xAA\xAA",
  4176. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  4177. "\xAA\xAA\xAA",
  4178. };
  4179. testVector a, b, c, d;
  4180. testVector test_hmac[4];
  4181. int ret;
  4182. int times = sizeof(test_hmac) / sizeof(testVector), i;
  4183. a.input = "Hi There";
  4184. a.output = "\xb0\x34\x4c\x61\xd8\xdb\x38\x53\x5c\xa8\xaf\xce\xaf\x0b\xf1"
  4185. "\x2b\x88\x1d\xc2\x00\xc9\x83\x3d\xa7\x26\xe9\x37\x6c\x2e\x32"
  4186. "\xcf\xf7";
  4187. a.inLen = XSTRLEN(a.input);
  4188. a.outLen = WC_SHA256_DIGEST_SIZE;
  4189. b.input = "what do ya want for nothing?";
  4190. b.output = "\x5b\xdc\xc1\x46\xbf\x60\x75\x4e\x6a\x04\x24\x26\x08\x95\x75"
  4191. "\xc7\x5a\x00\x3f\x08\x9d\x27\x39\x83\x9d\xec\x58\xb9\x64\xec"
  4192. "\x38\x43";
  4193. b.inLen = XSTRLEN(b.input);
  4194. b.outLen = WC_SHA256_DIGEST_SIZE;
  4195. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4196. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4197. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4198. "\xDD\xDD\xDD\xDD\xDD\xDD";
  4199. c.output = "\x77\x3e\xa9\x1e\x36\x80\x0e\x46\x85\x4d\xb8\xeb\xd0\x91\x81"
  4200. "\xa7\x29\x59\x09\x8b\x3e\xf8\xc1\x22\xd9\x63\x55\x14\xce\xd5"
  4201. "\x65\xfe";
  4202. c.inLen = XSTRLEN(c.input);
  4203. c.outLen = WC_SHA256_DIGEST_SIZE;
  4204. d.input = 0;
  4205. d.output = "\x86\xe5\x4f\xd4\x48\x72\x5d\x7e\x5d\xcf\xe2\x23\x53\xc8\x28"
  4206. "\xaf\x48\x78\x1e\xb4\x8c\xae\x81\x06\xa7\xe1\xd4\x98\x94\x9f"
  4207. "\x3e\x46";
  4208. d.inLen = 0;
  4209. d.outLen = WC_SHA256_DIGEST_SIZE;
  4210. test_hmac[0] = a;
  4211. test_hmac[1] = b;
  4212. test_hmac[2] = c;
  4213. test_hmac[3] = d;
  4214. for (i = 0; i < times; ++i) {
  4215. #if defined(HAVE_FIPS) || defined(HAVE_CAVIUM)
  4216. if (i == 1)
  4217. continue; /* cavium can't handle short keys, fips not allowed */
  4218. #endif
  4219. #if defined(HAVE_INTEL_QA) || defined(HAVE_CAVIUM)
  4220. if (i == 3)
  4221. continue; /* QuickAssist can't handle empty HMAC */
  4222. #endif
  4223. if (wc_HmacInit(&hmac, HEAP_HINT, devId) != 0)
  4224. return -3700 - i;
  4225. ret = wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys[i],
  4226. (word32)XSTRLEN(keys[i]));
  4227. if (ret != 0)
  4228. return -3710 - i;
  4229. if (test_hmac[i].input != NULL) {
  4230. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  4231. (word32)test_hmac[i].inLen);
  4232. if (ret != 0)
  4233. return -3720 - i;
  4234. }
  4235. ret = wc_HmacFinal(&hmac, hash);
  4236. if (ret != 0)
  4237. return -3730 - i;
  4238. if (XMEMCMP(hash, test_hmac[i].output, WC_SHA256_DIGEST_SIZE) != 0)
  4239. return -3740 - i;
  4240. wc_HmacFree(&hmac);
  4241. }
  4242. #ifndef HAVE_FIPS
  4243. if (wc_HmacSizeByType(WC_SHA256) != WC_SHA256_DIGEST_SIZE)
  4244. return -3750;
  4245. if (wc_HmacSizeByType(20) != BAD_FUNC_ARG)
  4246. return -3751;
  4247. #endif
  4248. if (wolfSSL_GetHmacMaxSize() != WC_MAX_DIGEST_SIZE)
  4249. return -3752;
  4250. return 0;
  4251. }
  4252. #endif
  4253. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  4254. WOLFSSL_TEST_SUBROUTINE int hmac_sha384_test(void)
  4255. {
  4256. Hmac hmac;
  4257. byte hash[WC_SHA384_DIGEST_SIZE];
  4258. const char* keys[]=
  4259. {
  4260. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  4261. "\x0b\x0b\x0b",
  4262. "Jefe",
  4263. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  4264. "\xAA\xAA\xAA",
  4265. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4266. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4267. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4268. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4269. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4270. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4271. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4272. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4273. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4274. };
  4275. testVector a, b, c, d;
  4276. testVector test_hmac[4];
  4277. int ret;
  4278. int times = sizeof(test_hmac) / sizeof(testVector), i;
  4279. a.input = "Hi There";
  4280. a.output = "\xaf\xd0\x39\x44\xd8\x48\x95\x62\x6b\x08\x25\xf4\xab\x46\x90"
  4281. "\x7f\x15\xf9\xda\xdb\xe4\x10\x1e\xc6\x82\xaa\x03\x4c\x7c\xeb"
  4282. "\xc5\x9c\xfa\xea\x9e\xa9\x07\x6e\xde\x7f\x4a\xf1\x52\xe8\xb2"
  4283. "\xfa\x9c\xb6";
  4284. a.inLen = XSTRLEN(a.input);
  4285. a.outLen = WC_SHA384_DIGEST_SIZE;
  4286. b.input = "what do ya want for nothing?";
  4287. b.output = "\xaf\x45\xd2\xe3\x76\x48\x40\x31\x61\x7f\x78\xd2\xb5\x8a\x6b"
  4288. "\x1b\x9c\x7e\xf4\x64\xf5\xa0\x1b\x47\xe4\x2e\xc3\x73\x63\x22"
  4289. "\x44\x5e\x8e\x22\x40\xca\x5e\x69\xe2\xc7\x8b\x32\x39\xec\xfa"
  4290. "\xb2\x16\x49";
  4291. b.inLen = XSTRLEN(b.input);
  4292. b.outLen = WC_SHA384_DIGEST_SIZE;
  4293. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4294. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4295. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4296. "\xDD\xDD\xDD\xDD\xDD\xDD";
  4297. c.output = "\x88\x06\x26\x08\xd3\xe6\xad\x8a\x0a\xa2\xac\xe0\x14\xc8\xa8"
  4298. "\x6f\x0a\xa6\x35\xd9\x47\xac\x9f\xeb\xe8\x3e\xf4\xe5\x59\x66"
  4299. "\x14\x4b\x2a\x5a\xb3\x9d\xc1\x38\x14\xb9\x4e\x3a\xb6\xe1\x01"
  4300. "\xa3\x4f\x27";
  4301. c.inLen = XSTRLEN(c.input);
  4302. c.outLen = WC_SHA384_DIGEST_SIZE;
  4303. d.input = "Big Key Input";
  4304. d.output = "\xd2\x3d\x29\x6e\xf5\x1e\x23\x23\x49\x18\xb3\xbf\x4c\x38\x7b"
  4305. "\x31\x21\x17\xbb\x09\x73\x27\xf8\x12\x9d\xe9\xc6\x5d\xf9\x54"
  4306. "\xd6\x38\x5a\x68\x53\x14\xee\xe0\xa6\x4f\x36\x7e\xb2\xf3\x1a"
  4307. "\x57\x41\x69";
  4308. d.inLen = XSTRLEN(d.input);
  4309. d.outLen = WC_SHA384_DIGEST_SIZE;
  4310. test_hmac[0] = a;
  4311. test_hmac[1] = b;
  4312. test_hmac[2] = c;
  4313. test_hmac[3] = d;
  4314. for (i = 0; i < times; ++i) {
  4315. #if defined(HAVE_FIPS)
  4316. if (i == 1)
  4317. continue; /* fips not allowed */
  4318. #endif
  4319. if (wc_HmacInit(&hmac, HEAP_HINT, devId) != 0)
  4320. return -3800;
  4321. ret = wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys[i],
  4322. (word32)XSTRLEN(keys[i]));
  4323. if (ret != 0)
  4324. return -3801;
  4325. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  4326. (word32)test_hmac[i].inLen);
  4327. if (ret != 0)
  4328. return -3802;
  4329. ret = wc_HmacFinal(&hmac, hash);
  4330. if (ret != 0)
  4331. return -3803;
  4332. if (XMEMCMP(hash, test_hmac[i].output, WC_SHA384_DIGEST_SIZE) != 0)
  4333. return -3804 - i;
  4334. wc_HmacFree(&hmac);
  4335. }
  4336. #ifndef HAVE_FIPS
  4337. if (wc_HmacSizeByType(WC_SHA384) != WC_SHA384_DIGEST_SIZE)
  4338. return -3814;
  4339. #endif
  4340. return 0;
  4341. }
  4342. #endif
  4343. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA512)
  4344. WOLFSSL_TEST_SUBROUTINE int hmac_sha512_test(void)
  4345. {
  4346. Hmac hmac;
  4347. byte hash[WC_SHA512_DIGEST_SIZE];
  4348. const char* keys[]=
  4349. {
  4350. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  4351. "\x0b\x0b\x0b",
  4352. "Jefe",
  4353. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  4354. "\xAA\xAA\xAA",
  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. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4362. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4363. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4364. };
  4365. testVector a, b, c, d;
  4366. testVector test_hmac[4];
  4367. int ret;
  4368. int times = sizeof(test_hmac) / sizeof(testVector), i;
  4369. a.input = "Hi There";
  4370. a.output = "\x87\xaa\x7c\xde\xa5\xef\x61\x9d\x4f\xf0\xb4\x24\x1a\x1d\x6c"
  4371. "\xb0\x23\x79\xf4\xe2\xce\x4e\xc2\x78\x7a\xd0\xb3\x05\x45\xe1"
  4372. "\x7c\xde\xda\xa8\x33\xb7\xd6\xb8\xa7\x02\x03\x8b\x27\x4e\xae"
  4373. "\xa3\xf4\xe4\xbe\x9d\x91\x4e\xeb\x61\xf1\x70\x2e\x69\x6c\x20"
  4374. "\x3a\x12\x68\x54";
  4375. a.inLen = XSTRLEN(a.input);
  4376. a.outLen = WC_SHA512_DIGEST_SIZE;
  4377. b.input = "what do ya want for nothing?";
  4378. b.output = "\x16\x4b\x7a\x7b\xfc\xf8\x19\xe2\xe3\x95\xfb\xe7\x3b\x56\xe0"
  4379. "\xa3\x87\xbd\x64\x22\x2e\x83\x1f\xd6\x10\x27\x0c\xd7\xea\x25"
  4380. "\x05\x54\x97\x58\xbf\x75\xc0\x5a\x99\x4a\x6d\x03\x4f\x65\xf8"
  4381. "\xf0\xe6\xfd\xca\xea\xb1\xa3\x4d\x4a\x6b\x4b\x63\x6e\x07\x0a"
  4382. "\x38\xbc\xe7\x37";
  4383. b.inLen = XSTRLEN(b.input);
  4384. b.outLen = WC_SHA512_DIGEST_SIZE;
  4385. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4386. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4387. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4388. "\xDD\xDD\xDD\xDD\xDD\xDD";
  4389. c.output = "\xfa\x73\xb0\x08\x9d\x56\xa2\x84\xef\xb0\xf0\x75\x6c\x89\x0b"
  4390. "\xe9\xb1\xb5\xdb\xdd\x8e\xe8\x1a\x36\x55\xf8\x3e\x33\xb2\x27"
  4391. "\x9d\x39\xbf\x3e\x84\x82\x79\xa7\x22\xc8\x06\xb4\x85\xa4\x7e"
  4392. "\x67\xc8\x07\xb9\x46\xa3\x37\xbe\xe8\x94\x26\x74\x27\x88\x59"
  4393. "\xe1\x32\x92\xfb";
  4394. c.inLen = XSTRLEN(c.input);
  4395. c.outLen = WC_SHA512_DIGEST_SIZE;
  4396. d.input = "Big Key Input";
  4397. d.output = "\x3f\xa9\xc9\xe1\xbd\xbb\x04\x55\x1f\xef\xcc\x92\x33\x08\xeb"
  4398. "\xcf\xc1\x9a\x5b\x5b\xc0\x7c\x86\x84\xae\x8c\x40\xaf\xb1\x27"
  4399. "\x87\x38\x92\x04\xa8\xed\xd7\xd7\x07\xa9\x85\xa0\xc2\xcd\x30"
  4400. "\xc0\x56\x14\x49\xbc\x2f\x69\x15\x6a\x97\xd8\x79\x2f\xb3\x3b"
  4401. "\x1e\x18\xfe\xfa";
  4402. d.inLen = XSTRLEN(d.input);
  4403. d.outLen = WC_SHA512_DIGEST_SIZE;
  4404. test_hmac[0] = a;
  4405. test_hmac[1] = b;
  4406. test_hmac[2] = c;
  4407. test_hmac[3] = d;
  4408. for (i = 0; i < times; ++i) {
  4409. #if defined(HAVE_FIPS)
  4410. if (i == 1)
  4411. continue; /* fips not allowed */
  4412. #endif
  4413. if (wc_HmacInit(&hmac, HEAP_HINT, devId) != 0)
  4414. return -3900;
  4415. ret = wc_HmacSetKey(&hmac, WC_SHA512, (byte*)keys[i],
  4416. (word32)XSTRLEN(keys[i]));
  4417. if (ret != 0)
  4418. return -3901;
  4419. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  4420. (word32)test_hmac[i].inLen);
  4421. if (ret != 0)
  4422. return -3902;
  4423. ret = wc_HmacFinal(&hmac, hash);
  4424. if (ret != 0)
  4425. return -3903;
  4426. if (XMEMCMP(hash, test_hmac[i].output, WC_SHA512_DIGEST_SIZE) != 0)
  4427. return -3904 - i;
  4428. wc_HmacFree(&hmac);
  4429. }
  4430. #ifndef HAVE_FIPS
  4431. if (wc_HmacSizeByType(WC_SHA512) != WC_SHA512_DIGEST_SIZE)
  4432. return -3914;
  4433. #endif
  4434. return 0;
  4435. }
  4436. #endif
  4437. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA3) && \
  4438. !defined(WOLFSSL_NOSHA3_224) && !defined(WOLFSSL_NOSHA3_256) && \
  4439. !defined(WOLFSSL_NOSHA3_384) && !defined(WOLFSSL_NOSHA3_512)
  4440. WOLFSSL_TEST_SUBROUTINE int hmac_sha3_test(void)
  4441. {
  4442. Hmac hmac;
  4443. byte hash[WC_SHA3_512_DIGEST_SIZE];
  4444. const char* key[4] =
  4445. {
  4446. "Jefe",
  4447. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  4448. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
  4449. "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
  4450. "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa",
  4451. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4452. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4453. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4454. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4455. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4456. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4457. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4458. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4459. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4460. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4461. };
  4462. const char* input[4] =
  4463. {
  4464. "what do ya want for nothing?",
  4465. "Hi There",
  4466. "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
  4467. "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
  4468. "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
  4469. "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
  4470. "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd",
  4471. "Big Key Input"
  4472. };
  4473. const int hashType[4] =
  4474. {
  4475. WC_SHA3_224, WC_SHA3_256, WC_SHA3_384, WC_SHA3_512
  4476. };
  4477. const int hashSz[4] =
  4478. {
  4479. WC_SHA3_224_DIGEST_SIZE, WC_SHA3_256_DIGEST_SIZE,
  4480. WC_SHA3_384_DIGEST_SIZE, WC_SHA3_512_DIGEST_SIZE
  4481. };
  4482. const char* output[16] =
  4483. {
  4484. /* key = jefe, input = what do ya want for nothing? */
  4485. /* HMAC-SHA3-224 */
  4486. "\x7f\xdb\x8d\xd8\x8b\xd2\xf6\x0d\x1b\x79\x86\x34\xad\x38\x68\x11"
  4487. "\xc2\xcf\xc8\x5b\xfa\xf5\xd5\x2b\xba\xce\x5e\x66",
  4488. /* HMAC-SHA3-256 */
  4489. "\xc7\xd4\x07\x2e\x78\x88\x77\xae\x35\x96\xbb\xb0\xda\x73\xb8\x87"
  4490. "\xc9\x17\x1f\x93\x09\x5b\x29\x4a\xe8\x57\xfb\xe2\x64\x5e\x1b\xa5",
  4491. /* HMAC-SHA3-384 */
  4492. "\xf1\x10\x1f\x8c\xbf\x97\x66\xfd\x67\x64\xd2\xed\x61\x90\x3f\x21"
  4493. "\xca\x9b\x18\xf5\x7c\xf3\xe1\xa2\x3c\xa1\x35\x08\xa9\x32\x43\xce"
  4494. "\x48\xc0\x45\xdc\x00\x7f\x26\xa2\x1b\x3f\x5e\x0e\x9d\xf4\xc2\x0a",
  4495. /* HMAC-SHA3-512 */
  4496. "\x5a\x4b\xfe\xab\x61\x66\x42\x7c\x7a\x36\x47\xb7\x47\x29\x2b\x83"
  4497. "\x84\x53\x7c\xdb\x89\xaf\xb3\xbf\x56\x65\xe4\xc5\xe7\x09\x35\x0b"
  4498. "\x28\x7b\xae\xc9\x21\xfd\x7c\xa0\xee\x7a\x0c\x31\xd0\x22\xa9\x5e"
  4499. "\x1f\xc9\x2b\xa9\xd7\x7d\xf8\x83\x96\x02\x75\xbe\xb4\xe6\x20\x24",
  4500. /* key = 0b..., input = Hi There */
  4501. /* HMAC-SHA3-224 */
  4502. "\x3b\x16\x54\x6b\xbc\x7b\xe2\x70\x6a\x03\x1d\xca\xfd\x56\x37\x3d"
  4503. "\x98\x84\x36\x76\x41\xd8\xc5\x9a\xf3\xc8\x60\xf7",
  4504. /* HMAC-SHA3-256 */
  4505. "\xba\x85\x19\x23\x10\xdf\xfa\x96\xe2\xa3\xa4\x0e\x69\x77\x43\x51"
  4506. "\x14\x0b\xb7\x18\x5e\x12\x02\xcd\xcc\x91\x75\x89\xf9\x5e\x16\xbb",
  4507. /* HMAC-SHA3-384 */
  4508. "\x68\xd2\xdc\xf7\xfd\x4d\xdd\x0a\x22\x40\xc8\xa4\x37\x30\x5f\x61"
  4509. "\xfb\x73\x34\xcf\xb5\xd0\x22\x6e\x1b\xc2\x7d\xc1\x0a\x2e\x72\x3a"
  4510. "\x20\xd3\x70\xb4\x77\x43\x13\x0e\x26\xac\x7e\x3d\x53\x28\x86\xbd",
  4511. /* HMAC-SHA3-512 */
  4512. "\xeb\x3f\xbd\x4b\x2e\xaa\xb8\xf5\xc5\x04\xbd\x3a\x41\x46\x5a\xac"
  4513. "\xec\x15\x77\x0a\x7c\xab\xac\x53\x1e\x48\x2f\x86\x0b\x5e\xc7\xba"
  4514. "\x47\xcc\xb2\xc6\xf2\xaf\xce\x8f\x88\xd2\x2b\x6d\xc6\x13\x80\xf2"
  4515. "\x3a\x66\x8f\xd3\x88\x8b\xb8\x05\x37\xc0\xa0\xb8\x64\x07\x68\x9e",
  4516. /* key = aa..., output = dd... */
  4517. /* HMAC-SHA3-224 */
  4518. "\x67\x6c\xfc\x7d\x16\x15\x36\x38\x78\x03\x90\x69\x2b\xe1\x42\xd2"
  4519. "\xdf\x7c\xe9\x24\xb9\x09\xc0\xc0\x8d\xbf\xdc\x1a",
  4520. /* HMAC-SHA3-256 */
  4521. "\x84\xec\x79\x12\x4a\x27\x10\x78\x65\xce\xdd\x8b\xd8\x2d\xa9\x96"
  4522. "\x5e\x5e\xd8\xc3\x7b\x0a\xc9\x80\x05\xa7\xf3\x9e\xd5\x8a\x42\x07",
  4523. /* HMAC-SHA3-384 */
  4524. "\x27\x5c\xd0\xe6\x61\xbb\x8b\x15\x1c\x64\xd2\x88\xf1\xf7\x82\xfb"
  4525. "\x91\xa8\xab\xd5\x68\x58\xd7\x2b\xab\xb2\xd4\x76\xf0\x45\x83\x73"
  4526. "\xb4\x1b\x6a\xb5\xbf\x17\x4b\xec\x42\x2e\x53\xfc\x31\x35\xac\x6e",
  4527. /* HMAC-SHA3-512 */
  4528. "\x30\x9e\x99\xf9\xec\x07\x5e\xc6\xc6\xd4\x75\xed\xa1\x18\x06\x87"
  4529. "\xfc\xf1\x53\x11\x95\x80\x2a\x99\xb5\x67\x74\x49\xa8\x62\x51\x82"
  4530. "\x85\x1c\xb3\x32\xaf\xb6\xa8\x9c\x41\x13\x25\xfb\xcb\xcd\x42\xaf"
  4531. "\xcb\x7b\x6e\x5a\xab\x7e\xa4\x2c\x66\x0f\x97\xfd\x85\x84\xbf\x03",
  4532. /* key = big key, input = Big Key Input */
  4533. /* HMAC-SHA3-224 */
  4534. "\x29\xe0\x5e\x46\xc4\xa4\x5e\x46\x74\xbf\xd7\x2d\x1a\xd8\x66\xdb"
  4535. "\x2d\x0d\x10\x4e\x2b\xfa\xad\x53\x7d\x15\x69\x8b",
  4536. /* HMAC-SHA3-256 */
  4537. "\xb5\x5b\x8d\x64\xb6\x9c\x21\xd0\xbf\x20\x5c\xa2\xf7\xb9\xb1\x4e"
  4538. "\x88\x21\x61\x2c\x66\xc3\x91\xae\x6c\x95\x16\x85\x83\xe6\xf4\x9b",
  4539. /* HMAC-SHA3-384 */
  4540. "\xaa\x91\xb3\xa6\x2f\x56\xa1\xbe\x8c\x3e\x74\x38\xdb\x58\xd9\xd3"
  4541. "\x34\xde\xa0\x60\x6d\x8d\x46\xe0\xec\xa9\xf6\x06\x35\x14\xe6\xed"
  4542. "\x83\xe6\x7c\x77\x24\x6c\x11\xb5\x90\x82\xb5\x75\xda\x7b\x83\x2d",
  4543. /* HMAC-SHA3-512 */
  4544. "\x1c\xc3\xa9\x24\x4a\x4a\x3f\xbd\xc7\x20\x00\x16\x9b\x79\x47\x03"
  4545. "\x78\x75\x2c\xb5\xf1\x2e\x62\x7c\xbe\xef\x4e\x8f\x0b\x11\x2b\x32"
  4546. "\xa0\xee\xc9\xd0\x4d\x64\x64\x0b\x37\xf4\xdd\x66\xf7\x8b\xb3\xad"
  4547. "\x52\x52\x6b\x65\x12\xde\x0d\x7c\xc0\x8b\x60\x01\x6c\x37\xd7\xa8"
  4548. };
  4549. int i = 0, iMax = sizeof(input) / sizeof(input[0]),
  4550. j, jMax = sizeof(hashType) / sizeof(hashType[0]),
  4551. ret;
  4552. #ifdef HAVE_FIPS
  4553. /* FIPS requires a minimum length for HMAC keys, and "Jefe" is too
  4554. * short. Skip it in FIPS builds. */
  4555. i = 1;
  4556. #endif
  4557. for (; i < iMax; i++) {
  4558. for (j = 0; j < jMax; j++) {
  4559. if (wc_HmacInit(&hmac, HEAP_HINT, devId) != 0)
  4560. return -4000;
  4561. ret = wc_HmacSetKey(&hmac, hashType[j], (byte*)key[i],
  4562. (word32)XSTRLEN(key[i]));
  4563. if (ret != 0)
  4564. return -4001;
  4565. ret = wc_HmacUpdate(&hmac, (byte*)input[i],
  4566. (word32)XSTRLEN(input[i]));
  4567. if (ret != 0)
  4568. return -4002;
  4569. ret = wc_HmacFinal(&hmac, hash);
  4570. if (ret != 0)
  4571. return -4003;
  4572. if (XMEMCMP(hash, output[(i*jMax) + j], hashSz[j]) != 0)
  4573. return -4004;
  4574. wc_HmacFree(&hmac);
  4575. if (i > 0)
  4576. continue;
  4577. #ifndef HAVE_FIPS
  4578. ret = wc_HmacSizeByType(hashType[j]);
  4579. if (ret != hashSz[j])
  4580. return -4005;
  4581. #endif
  4582. }
  4583. }
  4584. return 0;
  4585. }
  4586. #endif
  4587. #ifdef WC_RC2
  4588. typedef struct rc2TestVector {
  4589. const char* input;
  4590. const char* output;
  4591. const char* key; /* Key, variable up to 128 bytes */
  4592. const char* iv; /* IV, 8-bytes */
  4593. int inLen;
  4594. int outLen;
  4595. int keyLen;
  4596. int effectiveKeyBits; /* Up to 1024 bits supported */
  4597. } rc2TestVector;
  4598. static int rc2_ecb_test(void)
  4599. {
  4600. int ret = 0;
  4601. byte cipher[RC2_BLOCK_SIZE];
  4602. byte plain[RC2_BLOCK_SIZE];
  4603. rc2TestVector a, b, c, d, e, f, g, h;
  4604. rc2TestVector test_rc2[8];
  4605. int times = sizeof(test_rc2) / sizeof(rc2TestVector), i;
  4606. a.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4607. a.output = "\xeb\xb7\x73\xf9\x93\x27\x8e\xff";
  4608. a.key = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4609. a.inLen = RC2_BLOCK_SIZE;
  4610. a.outLen = RC2_BLOCK_SIZE;
  4611. a.keyLen = 8;
  4612. a.effectiveKeyBits = 63;
  4613. b.input = "\xff\xff\xff\xff\xff\xff\xff\xff";
  4614. b.output = "\x27\x8b\x27\xe4\x2e\x2f\x0d\x49";
  4615. b.key = "\xff\xff\xff\xff\xff\xff\xff\xff";
  4616. b.inLen = RC2_BLOCK_SIZE;
  4617. b.outLen = RC2_BLOCK_SIZE;
  4618. b.keyLen = 8;
  4619. b.effectiveKeyBits = 64;
  4620. c.input = "\x10\x00\x00\x00\x00\x00\x00\x01";
  4621. c.output = "\x30\x64\x9e\xdf\x9b\xe7\xd2\xc2";
  4622. c.key = "\x30\x00\x00\x00\x00\x00\x00\x00";
  4623. c.inLen = RC2_BLOCK_SIZE;
  4624. c.outLen = RC2_BLOCK_SIZE;
  4625. c.keyLen = 8;
  4626. c.effectiveKeyBits = 64;
  4627. d.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4628. d.output = "\x61\xa8\xa2\x44\xad\xac\xcc\xf0";
  4629. d.key = "\x88";
  4630. d.inLen = RC2_BLOCK_SIZE;
  4631. d.outLen = RC2_BLOCK_SIZE;
  4632. d.keyLen = 1;
  4633. d.effectiveKeyBits = 64;
  4634. e.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4635. e.output = "\x6c\xcf\x43\x08\x97\x4c\x26\x7f";
  4636. e.key = "\x88\xbc\xa9\x0e\x90\x87\x5a";
  4637. e.inLen = RC2_BLOCK_SIZE;
  4638. e.outLen = RC2_BLOCK_SIZE;
  4639. e.keyLen = 7;
  4640. e.effectiveKeyBits = 64;
  4641. f.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4642. f.output = "\x1a\x80\x7d\x27\x2b\xbe\x5d\xb1";
  4643. f.key = "\x88\xbc\xa9\x0e\x90\x87\x5a\x7f"
  4644. "\x0f\x79\xc3\x84\x62\x7b\xaf\xb2";
  4645. f.inLen = RC2_BLOCK_SIZE;
  4646. f.outLen = RC2_BLOCK_SIZE;
  4647. f.keyLen = 16;
  4648. f.effectiveKeyBits = 64;
  4649. g.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4650. g.output = "\x22\x69\x55\x2a\xb0\xf8\x5c\xa6";
  4651. g.key = "\x88\xbc\xa9\x0e\x90\x87\x5a\x7f"
  4652. "\x0f\x79\xc3\x84\x62\x7b\xaf\xb2";
  4653. g.inLen = RC2_BLOCK_SIZE;
  4654. g.outLen = RC2_BLOCK_SIZE;
  4655. g.keyLen = 16;
  4656. g.effectiveKeyBits = 128;
  4657. h.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4658. h.output = "\x5b\x78\xd3\xa4\x3d\xff\xf1\xf1";
  4659. h.key = "\x88\xbc\xa9\x0e\x90\x87\x5a\x7f"
  4660. "\x0f\x79\xc3\x84\x62\x7b\xaf\xb2"
  4661. "\x16\xf8\x0a\x6f\x85\x92\x05\x84"
  4662. "\xc4\x2f\xce\xb0\xbe\x25\x5d\xaf"
  4663. "\x1e";
  4664. h.inLen = RC2_BLOCK_SIZE;
  4665. h.outLen = RC2_BLOCK_SIZE;
  4666. h.keyLen = 33;
  4667. h.effectiveKeyBits = 129;
  4668. a.iv = b.iv = c.iv = d.iv = e.iv = f.iv = g.iv = h.iv = NULL;
  4669. test_rc2[0] = a;
  4670. test_rc2[1] = b;
  4671. test_rc2[2] = c;
  4672. test_rc2[3] = d;
  4673. test_rc2[4] = e;
  4674. test_rc2[5] = f;
  4675. test_rc2[6] = g;
  4676. test_rc2[7] = h;
  4677. for (i = 0; i < times; ++i) {
  4678. Rc2 enc;
  4679. XMEMSET(cipher, 0, RC2_BLOCK_SIZE);
  4680. XMEMSET(plain, 0, RC2_BLOCK_SIZE);
  4681. ret = wc_Rc2SetKey(&enc, (byte*)test_rc2[i].key, test_rc2[i].keyLen,
  4682. NULL, test_rc2[i].effectiveKeyBits);
  4683. if (ret != 0) {
  4684. return -4100;
  4685. }
  4686. /* ECB encrypt */
  4687. ret = wc_Rc2EcbEncrypt(&enc, cipher, (byte*)test_rc2[i].input,
  4688. (word32)test_rc2[i].outLen);
  4689. if (ret != 0) {
  4690. return -4101;
  4691. }
  4692. if (XMEMCMP(cipher, test_rc2[i].output, test_rc2[i].outLen)) {
  4693. return -4102;
  4694. }
  4695. /* ECB decrypt */
  4696. ret = wc_Rc2EcbDecrypt(&enc, plain, cipher, RC2_BLOCK_SIZE);
  4697. if (ret != 0) {
  4698. return -4103;
  4699. }
  4700. if (XMEMCMP(plain, test_rc2[i].input, RC2_BLOCK_SIZE)) {
  4701. return -4104;
  4702. }
  4703. }
  4704. return 0;
  4705. }
  4706. static int rc2_cbc_test(void)
  4707. {
  4708. int ret = 0;
  4709. byte cipher[128];
  4710. byte plain[128];
  4711. rc2TestVector a, b, c, d, e, f, g, h, i;
  4712. rc2TestVector test_rc2[9];
  4713. int times = sizeof(test_rc2) / sizeof(rc2TestVector), j;
  4714. /* key length = 7, effective key bits = 63 */
  4715. a.input = "\x00\x00\x00\x00\x00\x00\x00\x00"
  4716. "\x00\x00\x00\x00\x00\x00\x00\x00";
  4717. a.output = "\xEB\xB7\x73\xF9\x93\x27\x8E\xFF"
  4718. "\xF0\x51\x77\x8B\x65\xDB\x13\x57";
  4719. a.key = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4720. a.iv = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4721. a.inLen = RC2_BLOCK_SIZE*2;
  4722. a.outLen = RC2_BLOCK_SIZE*2;
  4723. a.keyLen = 8;
  4724. a.effectiveKeyBits = 63;
  4725. /* key length = 8, effective key bits = 64, all 0xFF */
  4726. b.input = "\xff\xff\xff\xff\xff\xff\xff\xff"
  4727. "\xff\xff\xff\xff\xff\xff\xff\xff";
  4728. b.output = "\xA3\xA1\x12\x65\x4F\x81\xC5\xCD"
  4729. "\xB6\x94\x3E\xEA\x3E\x8B\x9D\x1F";
  4730. b.key = "\xff\xff\xff\xff\xff\xff\xff\xff";
  4731. b.iv = "\xff\xff\xff\xff\xff\xff\xff\xff";
  4732. b.inLen = RC2_BLOCK_SIZE*2;
  4733. b.outLen = RC2_BLOCK_SIZE*2;
  4734. b.keyLen = 8;
  4735. b.effectiveKeyBits = 64;
  4736. /* key length = 8, effective key bits = 64 */
  4737. c.input = "\x10\x00\x00\x00\x00\x00\x00\x01"
  4738. "\x10\x00\x00\x00\x00\x00\x00\x01";
  4739. c.output = "\xB5\x70\x14\xA2\x5F\x40\xE3\x6D"
  4740. "\x81\x99\x8D\xE0\xB5\xD5\x3A\x05";
  4741. c.key = "\x30\x00\x00\x00\x00\x00\x00\x00";
  4742. c.iv = "\x30\x00\x00\x00\x00\x00\x00\x00";
  4743. c.inLen = RC2_BLOCK_SIZE*2;
  4744. c.outLen = RC2_BLOCK_SIZE*2;
  4745. c.keyLen = 8;
  4746. c.effectiveKeyBits = 64;
  4747. /* key length = 1, effective key bits = 64 */
  4748. d.input = "\x00\x00\x00\x00\x00\x00\x00\x00"
  4749. "\x00\x00\x00\x00\x00\x00\x00\x00";
  4750. d.output = "\x61\xA8\xA2\x44\xAD\xAC\xCC\xF0"
  4751. "\x6D\x19\xE8\xF1\xFC\xE7\x38\x87";
  4752. d.key = "\x88";
  4753. d.iv = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4754. d.inLen = RC2_BLOCK_SIZE*2;
  4755. d.outLen = RC2_BLOCK_SIZE*2;
  4756. d.keyLen = 1;
  4757. d.effectiveKeyBits = 64;
  4758. /* key length = 7, effective key bits = 64 */
  4759. e.input = "\x00\x00\x00\x00\x00\x00\x00\x00"
  4760. "\x00\x00\x00\x00\x00\x00\x00\x00";
  4761. e.output = "\x6C\xCF\x43\x08\x97\x4C\x26\x7F"
  4762. "\xCC\x3C\x53\x57\x7C\xA1\xA4\x4B";
  4763. e.key = "\x88\xbc\xa9\x0e\x90\x87\x5a";
  4764. e.iv = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4765. e.inLen = RC2_BLOCK_SIZE*2;
  4766. e.outLen = RC2_BLOCK_SIZE*2;
  4767. e.keyLen = 7;
  4768. e.effectiveKeyBits = 64;
  4769. /* key length = 16, effective key bits = 64 */
  4770. f.input = "\x00\x00\x00\x00\x00\x00\x00\x00"
  4771. "\x00\x00\x00\x00\x00\x00\x00\x00";
  4772. f.output = "\x1A\x80\x7D\x27\x2B\xBE\x5D\xB1"
  4773. "\x64\xEF\xE1\xC3\xB8\xAD\xFB\xBA";
  4774. f.key = "\x88\xbc\xa9\x0e\x90\x87\x5a\x7f"
  4775. "\x0f\x79\xc3\x84\x62\x7b\xaf\xb2";
  4776. f.iv = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4777. f.inLen = RC2_BLOCK_SIZE*2;
  4778. f.outLen = RC2_BLOCK_SIZE*2;
  4779. f.keyLen = 16;
  4780. f.effectiveKeyBits = 64;
  4781. /* key length = 16, effective bits = 128 */
  4782. g.input = "\x00\x00\x00\x00\x00\x00\x00\x00"
  4783. "\x00\x00\x00\x00\x00\x00\x00\x00";
  4784. g.output = "\x22\x69\x55\x2A\xB0\xF8\x5C\xA6"
  4785. "\x53\x6E\xFD\x2D\x89\xE1\x2A\x73";
  4786. g.key = "\x88\xbc\xa9\x0e\x90\x87\x5a\x7f"
  4787. "\x0f\x79\xc3\x84\x62\x7b\xaf\xb2";
  4788. g.iv = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4789. g.inLen = RC2_BLOCK_SIZE*2;
  4790. g.outLen = RC2_BLOCK_SIZE*2;
  4791. g.keyLen = 16;
  4792. g.effectiveKeyBits = 128;
  4793. /* key length = 33, effective bits = 129 */
  4794. h.input = "\x00\x00\x00\x00\x00\x00\x00\x00"
  4795. "\x00\x00\x00\x00\x00\x00\x00\x00";
  4796. h.output = "\x5B\x78\xD3\xA4\x3D\xFF\xF1\xF1"
  4797. "\x45\x30\xA8\xD5\xC7\x7C\x46\x19";
  4798. h.key = "\x88\xbc\xa9\x0e\x90\x87\x5a\x7f"
  4799. "\x0f\x79\xc3\x84\x62\x7b\xaf\xb2"
  4800. "\x16\xf8\x0a\x6f\x85\x92\x05\x84"
  4801. "\xc4\x2f\xce\xb0\xbe\x25\x5d\xaf"
  4802. "\x1e";
  4803. h.iv = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4804. h.inLen = RC2_BLOCK_SIZE*2;
  4805. h.outLen = RC2_BLOCK_SIZE*2;
  4806. h.keyLen = 33;
  4807. h.effectiveKeyBits = 129;
  4808. /* key length = 10, effective bits = 40 */
  4809. i.input = "\x11\x22\x33\x44\x55\x66\x77\x88"
  4810. "\x99\xAA\xBB\xCC\xDD\xEE\xFF\x00"
  4811. "\x11\x22\x33\x44\x55\x66\x77\x88"
  4812. "\x99\xAA\xBB\xCC\xDD\xEE\xFF\x00";
  4813. i.output = "\x71\x2D\x11\x99\xC9\xA0\x78\x4F"
  4814. "\xCD\xF1\x1E\x3D\xFD\x21\x7E\xDB"
  4815. "\xB2\x6E\x0D\xA4\x72\xBC\x31\x51"
  4816. "\x48\xEF\x4E\x68\x3B\xDC\xCD\x7D";
  4817. i.key = "\x26\x1E\x57\x8E\xC9\x62\xBF\xB8"
  4818. "\x3E\x96";
  4819. i.iv = "\x01\x02\x03\x04\x05\x06\x07\x08";
  4820. i.inLen = RC2_BLOCK_SIZE*4;
  4821. i.outLen = RC2_BLOCK_SIZE*4;
  4822. i.keyLen = 10;
  4823. i.effectiveKeyBits = 40;
  4824. test_rc2[0] = a;
  4825. test_rc2[1] = b;
  4826. test_rc2[2] = c;
  4827. test_rc2[3] = d;
  4828. test_rc2[4] = e;
  4829. test_rc2[5] = f;
  4830. test_rc2[6] = g;
  4831. test_rc2[7] = h;
  4832. test_rc2[8] = i;
  4833. for (j = 0; j < times; ++j) {
  4834. Rc2 rc2;
  4835. XMEMSET(cipher, 0, sizeof(cipher));
  4836. XMEMSET(plain, 0, sizeof(plain));
  4837. ret = wc_Rc2SetKey(&rc2, (byte*)test_rc2[j].key, test_rc2[j].keyLen,
  4838. (byte*)test_rc2[j].iv, test_rc2[j].effectiveKeyBits);
  4839. if (ret != 0) {
  4840. return -4200;
  4841. }
  4842. ret = wc_Rc2CbcEncrypt(&rc2, cipher, (byte*)test_rc2[j].input,
  4843. test_rc2[j].inLen);
  4844. if (ret != 0) {
  4845. return -4201;
  4846. }
  4847. if (XMEMCMP(cipher, (byte*)test_rc2[j].output, test_rc2[j].outLen)) {
  4848. return -4202;
  4849. }
  4850. /* reset IV for decrypt, since overriden by encrypt operation */
  4851. ret = wc_Rc2SetIV(&rc2, (byte*)test_rc2[j].iv);
  4852. if (ret != 0) {
  4853. return -4203;
  4854. }
  4855. ret = wc_Rc2CbcDecrypt(&rc2, plain, cipher, test_rc2[j].outLen);
  4856. if (ret != 0) {
  4857. return -4204;
  4858. }
  4859. if (XMEMCMP(plain, (byte*)test_rc2[j].input, test_rc2[j].inLen)) {
  4860. return -4205;
  4861. }
  4862. }
  4863. return 0;
  4864. }
  4865. WOLFSSL_TEST_SUBROUTINE int rc2_test(void)
  4866. {
  4867. int ret = 0;
  4868. ret = rc2_ecb_test();
  4869. if (ret != 0) {
  4870. return ret;
  4871. }
  4872. return rc2_cbc_test();
  4873. }
  4874. #endif
  4875. #ifndef NO_RC4
  4876. WOLFSSL_TEST_SUBROUTINE int arc4_test(void)
  4877. {
  4878. byte cipher[16];
  4879. byte plain[16];
  4880. const char* keys[] =
  4881. {
  4882. "\x01\x23\x45\x67\x89\xab\xcd\xef",
  4883. "\x01\x23\x45\x67\x89\xab\xcd\xef",
  4884. "\x00\x00\x00\x00\x00\x00\x00\x00",
  4885. "\xef\x01\x23\x45"
  4886. };
  4887. testVector a, b, c, d;
  4888. testVector test_arc4[4];
  4889. int times = sizeof(test_arc4) / sizeof(testVector), i;
  4890. a.input = "\x01\x23\x45\x67\x89\xab\xcd\xef";
  4891. a.output = "\x75\xb7\x87\x80\x99\xe0\xc5\x96";
  4892. a.inLen = 8;
  4893. a.outLen = 8;
  4894. b.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4895. b.output = "\x74\x94\xc2\xe7\x10\x4b\x08\x79";
  4896. b.inLen = 8;
  4897. b.outLen = 8;
  4898. c.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4899. c.output = "\xde\x18\x89\x41\xa3\x37\x5d\x3a";
  4900. c.inLen = 8;
  4901. c.outLen = 8;
  4902. d.input = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";
  4903. d.output = "\xd6\xa1\x41\xa7\xec\x3c\x38\xdf\xbd\x61";
  4904. d.inLen = 10;
  4905. d.outLen = 10;
  4906. test_arc4[0] = a;
  4907. test_arc4[1] = b;
  4908. test_arc4[2] = c;
  4909. test_arc4[3] = d;
  4910. for (i = 0; i < times; ++i) {
  4911. Arc4 enc;
  4912. Arc4 dec;
  4913. int keylen = 8; /* XSTRLEN with key 0x00 not good */
  4914. if (i == 3)
  4915. keylen = 4;
  4916. if (wc_Arc4Init(&enc, HEAP_HINT, devId) != 0)
  4917. return -4400;
  4918. if (wc_Arc4Init(&dec, HEAP_HINT, devId) != 0)
  4919. return -4401;
  4920. wc_Arc4SetKey(&enc, (byte*)keys[i], keylen);
  4921. wc_Arc4SetKey(&dec, (byte*)keys[i], keylen);
  4922. wc_Arc4Process(&enc, cipher, (byte*)test_arc4[i].input,
  4923. (word32)test_arc4[i].outLen);
  4924. wc_Arc4Process(&dec, plain, cipher, (word32)test_arc4[i].outLen);
  4925. if (XMEMCMP(plain, test_arc4[i].input, test_arc4[i].outLen))
  4926. return -4402 - i;
  4927. if (XMEMCMP(cipher, test_arc4[i].output, test_arc4[i].outLen))
  4928. return -4412 - i;
  4929. wc_Arc4Free(&enc);
  4930. wc_Arc4Free(&dec);
  4931. }
  4932. return 0;
  4933. }
  4934. #endif
  4935. #ifdef HAVE_CHACHA
  4936. WOLFSSL_TEST_SUBROUTINE int chacha_test(void)
  4937. {
  4938. ChaCha enc;
  4939. ChaCha dec;
  4940. byte cipher[128];
  4941. byte plain[128];
  4942. byte sliver[64];
  4943. byte input[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  4944. word32 keySz = 32;
  4945. int ret = 0;
  4946. int i;
  4947. int times = 4;
  4948. WOLFSSL_SMALL_STACK_STATIC const byte key1[] =
  4949. {
  4950. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  4951. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  4952. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  4953. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  4954. };
  4955. WOLFSSL_SMALL_STACK_STATIC const byte key2[] =
  4956. {
  4957. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  4958. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  4959. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  4960. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  4961. };
  4962. WOLFSSL_SMALL_STACK_STATIC const byte key3[] =
  4963. {
  4964. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  4965. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  4966. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  4967. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  4968. };
  4969. /* 128 bit key */
  4970. WOLFSSL_SMALL_STACK_STATIC const byte key4[] =
  4971. {
  4972. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  4973. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  4974. };
  4975. const byte* keys[] = {key1, key2, key3, key4};
  4976. WOLFSSL_SMALL_STACK_STATIC const byte ivs1[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
  4977. WOLFSSL_SMALL_STACK_STATIC const byte ivs2[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
  4978. WOLFSSL_SMALL_STACK_STATIC const byte ivs3[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00};
  4979. WOLFSSL_SMALL_STACK_STATIC const byte ivs4[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
  4980. const byte* ivs[] = {ivs1, ivs2, ivs3, ivs4};
  4981. #ifndef BENCH_EMBEDDED
  4982. WOLFSSL_SMALL_STACK_STATIC const byte cipher_big_result[] = {
  4983. 0x06, 0xa6, 0x5d, 0x31, 0x21, 0x6c, 0xdb, 0x37, 0x48, 0x7c, 0x01, 0x9d,
  4984. 0x72, 0xdf, 0x0a, 0x5b, 0x64, 0x74, 0x20, 0xba, 0x9e, 0xe0, 0x26, 0x7a,
  4985. 0xbf, 0xdf, 0x83, 0x34, 0x3b, 0x4f, 0x94, 0x3f, 0x37, 0x89, 0xaf, 0x00,
  4986. 0xdf, 0x0f, 0x2e, 0x75, 0x16, 0x41, 0xf6, 0x7a, 0x86, 0x94, 0x9d, 0x32,
  4987. 0x56, 0xf0, 0x79, 0x71, 0x68, 0x6f, 0xa6, 0x6b, 0xc6, 0x59, 0x49, 0xf6,
  4988. 0x10, 0x34, 0x03, 0x03, 0x16, 0x53, 0x9a, 0x98, 0x2a, 0x46, 0xde, 0x17,
  4989. 0x06, 0x65, 0x70, 0xca, 0x0a, 0x1f, 0xab, 0x80, 0x26, 0x96, 0x3f, 0x3e,
  4990. 0x7a, 0x3c, 0xa8, 0x87, 0xbb, 0x65, 0xdd, 0x5e, 0x07, 0x7b, 0x34, 0xe0,
  4991. 0x56, 0xda, 0x32, 0x13, 0x30, 0xc9, 0x0c, 0xd7, 0xba, 0xe4, 0x1f, 0xa6,
  4992. 0x91, 0x4f, 0x72, 0x9f, 0xd9, 0x5c, 0x62, 0x7d, 0xa6, 0xc2, 0xbc, 0x87,
  4993. 0xae, 0x64, 0x11, 0x94, 0x3b, 0xbc, 0x6c, 0x23, 0xbd, 0x7d, 0x00, 0xb4,
  4994. 0x99, 0xf2, 0x68, 0xb5, 0x59, 0x70, 0x93, 0xad, 0x69, 0xd0, 0xb1, 0x28,
  4995. 0x70, 0x92, 0xeb, 0xec, 0x39, 0x80, 0x82, 0xde, 0x44, 0xe2, 0x8a, 0x26,
  4996. 0xb3, 0xe9, 0x45, 0xcf, 0x83, 0x76, 0x9f, 0x6a, 0xa0, 0x46, 0x4a, 0x3d,
  4997. 0x26, 0x56, 0xaf, 0x49, 0x41, 0x26, 0x1b, 0x6a, 0x41, 0x37, 0x65, 0x91,
  4998. 0x72, 0xc4, 0xe7, 0x3c, 0x17, 0x31, 0xae, 0x2e, 0x2b, 0x31, 0x45, 0xe4,
  4999. 0x93, 0xd3, 0x10, 0xaa, 0xc5, 0x62, 0xd5, 0x11, 0x4b, 0x57, 0x1d, 0xad,
  5000. 0x48, 0x06, 0xd0, 0x0d, 0x98, 0xa5, 0xc6, 0x5b, 0xd0, 0x9e, 0x22, 0xc0,
  5001. 0x00, 0x32, 0x5a, 0xf5, 0x1c, 0x89, 0x6d, 0x54, 0x97, 0x55, 0x6b, 0x46,
  5002. 0xc5, 0xc7, 0xc4, 0x48, 0x9c, 0xbf, 0x47, 0xdc, 0x03, 0xc4, 0x1b, 0xcb,
  5003. 0x65, 0xa6, 0x91, 0x9d, 0x6d, 0xf1, 0xb0, 0x7a, 0x4d, 0x3b, 0x03, 0x95,
  5004. 0xf4, 0x8b, 0x0b, 0xae, 0x39, 0xff, 0x3f, 0xf6, 0xc0, 0x14, 0x18, 0x8a,
  5005. 0xe5, 0x19, 0xbd, 0xc1, 0xb4, 0x05, 0x4e, 0x29, 0x2f, 0x0b, 0x33, 0x76,
  5006. 0x28, 0x16, 0xa4, 0xa6, 0x93, 0x04, 0xb5, 0x55, 0x6b, 0x89, 0x3d, 0xa5,
  5007. 0x0f, 0xd3, 0xad, 0xfa, 0xd9, 0xfd, 0x05, 0x5d, 0x48, 0x94, 0x25, 0x5a,
  5008. 0x2c, 0x9a, 0x94, 0x80, 0xb0, 0xe7, 0xcb, 0x4d, 0x77, 0xbf, 0xca, 0xd8,
  5009. 0x55, 0x48, 0xbd, 0x66, 0xb1, 0x85, 0x81, 0xb1, 0x37, 0x79, 0xab, 0x52,
  5010. 0x08, 0x14, 0x12, 0xac, 0xcd, 0x45, 0x4d, 0x53, 0x6b, 0xca, 0x96, 0xc7,
  5011. 0x3b, 0x2f, 0x73, 0xb1, 0x5a, 0x23, 0xbd, 0x65, 0xd5, 0xea, 0x17, 0xb3,
  5012. 0xdc, 0xa1, 0x17, 0x1b, 0x2d, 0xb3, 0x9c, 0xd0, 0xdb, 0x41, 0x77, 0xef,
  5013. 0x93, 0x20, 0x52, 0x3e, 0x9d, 0xf5, 0xbf, 0x33, 0xf7, 0x52, 0xc1, 0x90,
  5014. 0xa0, 0x15, 0x17, 0xce, 0xf7, 0xf7, 0xd0, 0x3a, 0x3b, 0xd1, 0x72, 0x56,
  5015. 0x31, 0x81, 0xae, 0x60, 0xab, 0x40, 0xc1, 0xd1, 0x28, 0x77, 0x53, 0xac,
  5016. 0x9f, 0x11, 0x0a, 0x88, 0x36, 0x4b, 0xda, 0x57, 0xa7, 0x28, 0x5c, 0x85,
  5017. 0xd3, 0x85, 0x9b, 0x79, 0xad, 0x05, 0x1c, 0x37, 0x14, 0x5e, 0x0d, 0xd0,
  5018. 0x23, 0x03, 0x42, 0x1d, 0x48, 0x5d, 0xc5, 0x3c, 0x5a, 0x08, 0xa9, 0x0d,
  5019. 0x6e, 0x82, 0x7c, 0x2e, 0x3c, 0x41, 0xcc, 0x96, 0x8e, 0xad, 0xee, 0x2a,
  5020. 0x61, 0x0b, 0x16, 0x0f, 0xa9, 0x24, 0x40, 0x85, 0xbc, 0x9f, 0x28, 0x8d,
  5021. 0xe6, 0x68, 0x4d, 0x8f, 0x30, 0x48, 0xd9, 0x73, 0x73, 0x6c, 0x9a, 0x7f,
  5022. 0x67, 0xf7, 0xde, 0x4c, 0x0a, 0x8b, 0xe4, 0xb3, 0x08, 0x2a, 0x52, 0xda,
  5023. 0x54, 0xee, 0xcd, 0xb5, 0x62, 0x4a, 0x26, 0x20, 0xfb, 0x40, 0xbb, 0x39,
  5024. 0x3a, 0x0f, 0x09, 0xe8, 0x00, 0xd1, 0x24, 0x97, 0x60, 0xe9, 0x83, 0x83,
  5025. 0xfe, 0x9f, 0x9c, 0x15, 0xcf, 0x69, 0x03, 0x9f, 0x03, 0xe1, 0xe8, 0x6e,
  5026. 0xbd, 0x87, 0x58, 0x68, 0xee, 0xec, 0xd8, 0x29, 0x46, 0x23, 0x49, 0x92,
  5027. 0x72, 0x95, 0x5b, 0x49, 0xca, 0xe0, 0x45, 0x59, 0xb2, 0xca, 0xf4, 0xfc,
  5028. 0xb7, 0x59, 0x37, 0x49, 0x28, 0xbc, 0xf3, 0xd7, 0x61, 0xbc, 0x4b, 0xf3,
  5029. 0xa9, 0x4b, 0x2f, 0x05, 0xa8, 0x01, 0xa5, 0xdc, 0x00, 0x6e, 0x01, 0xb6,
  5030. 0x45, 0x3c, 0xd5, 0x49, 0x7d, 0x5c, 0x25, 0xe8, 0x31, 0x87, 0xb2, 0xb9,
  5031. 0xbf, 0xb3, 0x01, 0x62, 0x0c, 0xd0, 0x48, 0x77, 0xa2, 0x34, 0x0f, 0x16,
  5032. 0x22, 0x28, 0xee, 0x54, 0x08, 0x93, 0x3b, 0xe4, 0xde, 0x7e, 0x63, 0xf7,
  5033. 0x97, 0x16, 0x5d, 0x71, 0x58, 0xc2, 0x2e, 0xf2, 0x36, 0xa6, 0x12, 0x65,
  5034. 0x94, 0x17, 0xac, 0x66, 0x23, 0x7e, 0xc6, 0x72, 0x79, 0x24, 0xce, 0x8f,
  5035. 0x55, 0x19, 0x97, 0x44, 0xfc, 0x55, 0xec, 0x85, 0x26, 0x27, 0xdb, 0x38,
  5036. 0xb1, 0x42, 0x0a, 0xdd, 0x05, 0x99, 0x28, 0xeb, 0x03, 0x6c, 0x9a, 0xe9,
  5037. 0x17, 0xf6, 0x2c, 0xb0, 0xfe, 0xe7, 0xa4, 0xa7, 0x31, 0xda, 0x4d, 0xb0,
  5038. 0x29, 0xdb, 0xdd, 0x8d, 0x12, 0x13, 0x9c, 0xb4, 0xcc, 0x83, 0x97, 0xfb,
  5039. 0x1a, 0xdc, 0x08, 0xd6, 0x30, 0x62, 0xe8, 0xeb, 0x8b, 0x61, 0xcb, 0x1d,
  5040. 0x06, 0xe3, 0xa5, 0x4d, 0x35, 0xdb, 0x59, 0xa8, 0x2d, 0x87, 0x27, 0x44,
  5041. 0x6f, 0xc0, 0x38, 0x97, 0xe4, 0x85, 0x00, 0x02, 0x09, 0xf6, 0x69, 0x3a,
  5042. 0xcf, 0x08, 0x1b, 0x21, 0xbb, 0x79, 0xb1, 0xa1, 0x34, 0x09, 0xe0, 0x80,
  5043. 0xca, 0xb0, 0x78, 0x8a, 0x11, 0x97, 0xd4, 0x07, 0xbe, 0x1b, 0x6a, 0x5d,
  5044. 0xdb, 0xd6, 0x1f, 0x76, 0x6b, 0x16, 0xf0, 0x58, 0x84, 0x5f, 0x59, 0xce,
  5045. 0x62, 0x34, 0xc3, 0xdf, 0x94, 0xb8, 0x2f, 0x84, 0x68, 0xf0, 0xb8, 0x51,
  5046. 0xd9, 0x6d, 0x8e, 0x4a, 0x1d, 0xe6, 0x5c, 0xd8, 0x86, 0x25, 0xe3, 0x24,
  5047. 0xfd, 0x21, 0x61, 0x13, 0x48, 0x3e, 0xf6, 0x7d, 0xa6, 0x71, 0x9b, 0xd2,
  5048. 0x6e, 0xe6, 0xd2, 0x08, 0x94, 0x62, 0x6c, 0x98, 0xfe, 0x2f, 0x9c, 0x88,
  5049. 0x7e, 0x78, 0x15, 0x02, 0x00, 0xf0, 0xba, 0x24, 0x91, 0xf2, 0xdc, 0x47,
  5050. 0x51, 0x4d, 0x15, 0x5e, 0x91, 0x5f, 0x57, 0x5b, 0x1d, 0x35, 0x24, 0x45,
  5051. 0x75, 0x9b, 0x88, 0x75, 0xf1, 0x2f, 0x85, 0xe7, 0x89, 0xd1, 0x01, 0xb4,
  5052. 0xc8, 0x18, 0xb7, 0x97, 0xef, 0x4b, 0x90, 0xf4, 0xbf, 0x10, 0x27, 0x3c,
  5053. 0x60, 0xff, 0xc4, 0x94, 0x20, 0x2f, 0x93, 0x4b, 0x4d, 0xe3, 0x80, 0xf7,
  5054. 0x2c, 0x71, 0xd9, 0xe3, 0x68, 0xb4, 0x77, 0x2b, 0xc7, 0x0d, 0x39, 0x92,
  5055. 0xef, 0x91, 0x0d, 0xb2, 0x11, 0x50, 0x0e, 0xe8, 0xad, 0x3b, 0xf6, 0xb5,
  5056. 0xc6, 0x14, 0x4d, 0x33, 0x53, 0xa7, 0x60, 0x15, 0xc7, 0x27, 0x51, 0xdc,
  5057. 0x54, 0x29, 0xa7, 0x0d, 0x6a, 0x7b, 0x72, 0x13, 0xad, 0x7d, 0x41, 0x19,
  5058. 0x4e, 0x42, 0x49, 0xcc, 0x42, 0xe4, 0xbd, 0x99, 0x13, 0xd9, 0x7f, 0xf3,
  5059. 0x38, 0xa4, 0xb6, 0x33, 0xed, 0x07, 0x48, 0x7e, 0x8e, 0x82, 0xfe, 0x3a,
  5060. 0x9d, 0x75, 0x93, 0xba, 0x25, 0x4e, 0x37, 0x3c, 0x0c, 0xd5, 0x69, 0xa9,
  5061. 0x2d, 0x9e, 0xfd, 0xe8, 0xbb, 0xf5, 0x0c, 0xe2, 0x86, 0xb9, 0x5e, 0x6f,
  5062. 0x28, 0xe4, 0x19, 0xb3, 0x0b, 0xa4, 0x86, 0xd7, 0x24, 0xd0, 0xb8, 0x89,
  5063. 0x7b, 0x76, 0xec, 0x05, 0x10, 0x5b, 0x68, 0xe9, 0x58, 0x66, 0xa3, 0xc5,
  5064. 0xb6, 0x63, 0x20, 0x0e, 0x0e, 0xea, 0x3d, 0x61, 0x5e, 0xda, 0x3d, 0x3c,
  5065. 0xf9, 0xfd, 0xed, 0xa9, 0xdb, 0x52, 0x94, 0x8a, 0x00, 0xca, 0x3c, 0x8d,
  5066. 0x66, 0x8f, 0xb0, 0xf0, 0x5a, 0xca, 0x3f, 0x63, 0x71, 0xbf, 0xca, 0x99,
  5067. 0x37, 0x9b, 0x75, 0x97, 0x89, 0x10, 0x6e, 0xcf, 0xf2, 0xf5, 0xe3, 0xd5,
  5068. 0x45, 0x9b, 0xad, 0x10, 0x71, 0x6c, 0x5f, 0x6f, 0x7f, 0x22, 0x77, 0x18,
  5069. 0x2f, 0xf9, 0x99, 0xc5, 0x69, 0x58, 0x03, 0x12, 0x86, 0x82, 0x3e, 0xbf,
  5070. 0xc2, 0x12, 0x35, 0x43, 0xa3, 0xd9, 0x18, 0x4f, 0x41, 0x11, 0x6b, 0xf3,
  5071. 0x67, 0xaf, 0x3d, 0x78, 0xe4, 0x22, 0x2d, 0xb3, 0x48, 0x43, 0x31, 0x1d,
  5072. 0xef, 0xa8, 0xba, 0x49, 0x8e, 0xa9, 0xa7, 0xb6, 0x18, 0x77, 0x84, 0xca,
  5073. 0xbd, 0xa2, 0x02, 0x1b, 0x6a, 0xf8, 0x5f, 0xda, 0xff, 0xcf, 0x01, 0x6a,
  5074. 0x86, 0x69, 0xa9, 0xe9, 0xcb, 0x60, 0x1e, 0x15, 0xdc, 0x8f, 0x5d, 0x39,
  5075. 0xb5, 0xce, 0x55, 0x5f, 0x47, 0x97, 0xb1, 0x19, 0x6e, 0x21, 0xd6, 0x13,
  5076. 0x39, 0xb2, 0x24, 0xe0, 0x62, 0x82, 0x9f, 0xed, 0x12, 0x81, 0xed, 0xee,
  5077. 0xab, 0xd0, 0x2f, 0x19, 0x89, 0x3f, 0x57, 0x2e, 0xc2, 0xe2, 0x67, 0xe8,
  5078. 0xae, 0x03, 0x56, 0xba, 0xd4, 0xd0, 0xa4, 0x89, 0x03, 0x06, 0x5b, 0xcc,
  5079. 0xf2, 0x22, 0xb8, 0x0e, 0x76, 0x79, 0x4a, 0x42, 0x1d, 0x37, 0x51, 0x5a,
  5080. 0xaa, 0x46, 0x6c, 0x2a, 0xdd, 0x66, 0xfe, 0xc6, 0x68, 0xc3, 0x38, 0xa2,
  5081. 0xae, 0x5b, 0x98, 0x24, 0x5d, 0x43, 0x05, 0x82, 0x38, 0x12, 0xd3, 0xd1,
  5082. 0x75, 0x2d, 0x4f, 0x61, 0xbd, 0xb9, 0x10, 0x87, 0x44, 0x2a, 0x78, 0x07,
  5083. 0xff, 0xf4, 0x0f, 0xa1, 0xf3, 0x68, 0x9f, 0xbe, 0xae, 0xa2, 0x91, 0xf0,
  5084. 0xc7, 0x55, 0x7a, 0x52, 0xd5, 0xa3, 0x8d, 0x6f, 0xe4, 0x90, 0x5c, 0xf3,
  5085. 0x5f, 0xce, 0x3d, 0x23, 0xf9, 0x8e, 0xae, 0x14, 0xfb, 0x82, 0x9a, 0xa3,
  5086. 0x04, 0x5f, 0xbf, 0xad, 0x3e, 0xf2, 0x97, 0x0a, 0x60, 0x40, 0x70, 0x19,
  5087. 0x72, 0xad, 0x66, 0xfb, 0x78, 0x1b, 0x84, 0x6c, 0x98, 0xbc, 0x8c, 0xf8,
  5088. 0x4f, 0xcb, 0xb5, 0xf6, 0xaf, 0x7a, 0xb7, 0x93, 0xef, 0x67, 0x48, 0x02,
  5089. 0x2c, 0xcb, 0xe6, 0x77, 0x0f, 0x7b, 0xc1, 0xee, 0xc5, 0xb6, 0x2d, 0x7e,
  5090. 0x62, 0xa0, 0xc0, 0xa7, 0xa5, 0x80, 0x31, 0x92, 0x50, 0xa1, 0x28, 0x22,
  5091. 0x95, 0x03, 0x17, 0xd1, 0x0f, 0xf6, 0x08, 0xe5, 0xec
  5092. };
  5093. #define CHACHA_BIG_TEST_SIZE 1305
  5094. #ifndef WOLFSSL_SMALL_STACK
  5095. byte cipher_big[CHACHA_BIG_TEST_SIZE] = {0};
  5096. byte plain_big[CHACHA_BIG_TEST_SIZE] = {0};
  5097. byte input_big[CHACHA_BIG_TEST_SIZE] = {0};
  5098. #else
  5099. byte* cipher_big;
  5100. byte* plain_big;
  5101. byte* input_big;
  5102. #endif /* WOLFSSL_SMALL_STACK */
  5103. int block_size;
  5104. #endif /* BENCH_EMBEDDED */
  5105. byte a[] = {0x76,0xb8,0xe0,0xad,0xa0,0xf1,0x3d,0x90};
  5106. byte b[] = {0x45,0x40,0xf0,0x5a,0x9f,0x1f,0xb2,0x96};
  5107. byte c[] = {0xde,0x9c,0xba,0x7b,0xf3,0xd6,0x9e,0xf5};
  5108. byte d[] = {0x89,0x67,0x09,0x52,0x60,0x83,0x64,0xfd};
  5109. byte* test_chacha[4];
  5110. test_chacha[0] = a;
  5111. test_chacha[1] = b;
  5112. test_chacha[2] = c;
  5113. test_chacha[3] = d;
  5114. #ifndef BENCH_EMBEDDED
  5115. #ifdef WOLFSSL_SMALL_STACK
  5116. cipher_big = (byte*)XMALLOC(CHACHA_BIG_TEST_SIZE, HEAP_HINT,
  5117. DYNAMIC_TYPE_TMP_BUFFER);
  5118. if (cipher_big == NULL) {
  5119. return MEMORY_E;
  5120. }
  5121. plain_big = (byte*)XMALLOC(CHACHA_BIG_TEST_SIZE, HEAP_HINT,
  5122. DYNAMIC_TYPE_TMP_BUFFER);
  5123. if (plain_big == NULL) {
  5124. return MEMORY_E;
  5125. }
  5126. input_big = (byte*)XMALLOC(CHACHA_BIG_TEST_SIZE, HEAP_HINT,
  5127. DYNAMIC_TYPE_TMP_BUFFER);
  5128. if (input_big == NULL) {
  5129. return MEMORY_E;
  5130. }
  5131. XMEMSET(cipher_big, 0, CHACHA_BIG_TEST_SIZE);
  5132. XMEMSET(plain_big, 0, CHACHA_BIG_TEST_SIZE);
  5133. XMEMSET(input_big, 0, CHACHA_BIG_TEST_SIZE);
  5134. #endif /* WOLFSSL_SMALL_STACK */
  5135. #endif /* BENCH_EMBEDDED */
  5136. for (i = 0; i < times; ++i) {
  5137. if (i < 3) {
  5138. keySz = 32;
  5139. }
  5140. else {
  5141. keySz = 16;
  5142. }
  5143. XMEMCPY(plain, keys[i], keySz);
  5144. XMEMSET(cipher, 0, 32);
  5145. XMEMCPY(cipher + 4, ivs[i], 8);
  5146. ret |= wc_Chacha_SetKey(&enc, keys[i], keySz);
  5147. ret |= wc_Chacha_SetKey(&dec, keys[i], keySz);
  5148. if (ret != 0)
  5149. return ret;
  5150. ret |= wc_Chacha_SetIV(&enc, cipher, 0);
  5151. ret |= wc_Chacha_SetIV(&dec, cipher, 0);
  5152. if (ret != 0)
  5153. return ret;
  5154. XMEMCPY(plain, input, 8);
  5155. ret |= wc_Chacha_Process(&enc, cipher, plain, (word32)8);
  5156. ret |= wc_Chacha_Process(&dec, plain, cipher, (word32)8);
  5157. if (ret != 0)
  5158. return ret;
  5159. if (XMEMCMP(test_chacha[i], cipher, 8))
  5160. return -4700 - i;
  5161. if (XMEMCMP(plain, input, 8))
  5162. return -4710 - i;
  5163. }
  5164. /* test of starting at a different counter
  5165. encrypts all of the information and decrypts starting at 2nd chunk */
  5166. XMEMSET(plain, 0, sizeof(plain));
  5167. XMEMSET(sliver, 1, sizeof(sliver)); /* set as 1's to not match plain */
  5168. XMEMSET(cipher, 0, sizeof(cipher));
  5169. XMEMCPY(cipher + 4, ivs[0], 8);
  5170. ret |= wc_Chacha_SetKey(&enc, keys[0], keySz);
  5171. ret |= wc_Chacha_SetKey(&dec, keys[0], keySz);
  5172. if (ret != 0)
  5173. return ret;
  5174. ret |= wc_Chacha_SetIV(&enc, cipher, 0);
  5175. ret |= wc_Chacha_SetIV(&dec, cipher, 1);
  5176. if (ret != 0)
  5177. return ret;
  5178. ret |= wc_Chacha_Process(&enc, cipher, plain, sizeof(plain));
  5179. ret |= wc_Chacha_Process(&dec, sliver, cipher + 64, sizeof(sliver));
  5180. if (ret != 0)
  5181. return ret;
  5182. if (XMEMCMP(plain + 64, sliver, 64))
  5183. return -4720;
  5184. #ifndef BENCH_EMBEDDED
  5185. /* test of encrypting more data */
  5186. keySz = 32;
  5187. ret |= wc_Chacha_SetKey(&enc, keys[0], keySz);
  5188. ret |= wc_Chacha_SetKey(&dec, keys[0], keySz);
  5189. if (ret != 0)
  5190. return ret;
  5191. ret |= wc_Chacha_SetIV(&enc, ivs[2], 0);
  5192. ret |= wc_Chacha_SetIV(&dec, ivs[2], 0);
  5193. if (ret != 0)
  5194. return ret;
  5195. ret |= wc_Chacha_Process(&enc, cipher_big, plain_big, CHACHA_BIG_TEST_SIZE);
  5196. ret |= wc_Chacha_Process(&dec, plain_big, cipher_big,
  5197. CHACHA_BIG_TEST_SIZE);
  5198. if (ret != 0)
  5199. return ret;
  5200. if (XMEMCMP(plain_big, input_big, CHACHA_BIG_TEST_SIZE))
  5201. return -4721;
  5202. if (XMEMCMP(cipher_big, cipher_big_result, CHACHA_BIG_TEST_SIZE))
  5203. return -4722;
  5204. for (i = 0; i < 18; ++i) {
  5205. /* this will test all paths */
  5206. /* block sizes: 1 2 3 4 7 8 15 16 31 32 63 64 127 128 255 256 511 512 */
  5207. block_size = (2 << (i%9)) - (i<9?1:0);
  5208. keySz = 32;
  5209. ret |= wc_Chacha_SetKey(&enc, keys[0], keySz);
  5210. ret |= wc_Chacha_SetKey(&dec, keys[0], keySz);
  5211. if (ret != 0)
  5212. return ret;
  5213. ret |= wc_Chacha_SetIV(&enc, ivs[2], 0);
  5214. ret |= wc_Chacha_SetIV(&dec, ivs[2], 0);
  5215. if (ret != 0)
  5216. return ret;
  5217. ret |= wc_Chacha_Process(&enc, cipher_big, plain_big, block_size);
  5218. ret |= wc_Chacha_Process(&dec, plain_big, cipher_big, block_size);
  5219. if (ret != 0)
  5220. return ret;
  5221. if (XMEMCMP(plain_big, input_big, block_size))
  5222. return -4723-i;
  5223. if (XMEMCMP(cipher_big, cipher_big_result, block_size))
  5224. return -4724-i;
  5225. }
  5226. /* Streaming test */
  5227. for (i = 1; i <= (int)CHACHA_CHUNK_BYTES + 1; i++) {
  5228. int j, rem;
  5229. ret = wc_Chacha_SetKey(&enc, keys[0], keySz);
  5230. if (ret != 0)
  5231. return -4725;
  5232. ret = wc_Chacha_SetKey(&dec, keys[0], keySz);
  5233. if (ret != 0)
  5234. return -4726;
  5235. ret = wc_Chacha_SetIV(&enc, ivs[2], 0);
  5236. if (ret != 0)
  5237. return -4727;
  5238. ret = wc_Chacha_SetIV(&dec, ivs[2], 0);
  5239. if (ret != 0)
  5240. return -4728;
  5241. for (j = 0; j < CHACHA_BIG_TEST_SIZE - i; j+= i) {
  5242. ret = wc_Chacha_Process(&enc, cipher_big + j, plain_big + j, i);
  5243. if (ret != 0)
  5244. return -4729;
  5245. ret = wc_Chacha_Process(&dec, plain_big + j, cipher_big + j, i);
  5246. if (ret != 0)
  5247. return -4730;
  5248. }
  5249. rem = CHACHA_BIG_TEST_SIZE - j;
  5250. ret = wc_Chacha_Process(&enc, cipher_big + j, plain_big + j, rem);
  5251. if (ret != 0)
  5252. return -4731;
  5253. ret = wc_Chacha_Process(&dec, plain_big + j, cipher_big + j, rem);
  5254. if (ret != 0)
  5255. return -4732;
  5256. if (XMEMCMP(plain_big, input_big, CHACHA_BIG_TEST_SIZE))
  5257. return -4733;
  5258. if (XMEMCMP(cipher_big, cipher_big_result, CHACHA_BIG_TEST_SIZE))
  5259. return -4734;
  5260. }
  5261. #ifdef WOLFSSL_SMALL_STACK
  5262. XFREE(cipher_big, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  5263. XFREE(plain_big, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  5264. XFREE(input_big, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  5265. #endif /* WOLFSSL_SMALL_STACK */
  5266. #endif /* BENCH_EMBEDDED */
  5267. return 0;
  5268. }
  5269. #endif /* HAVE_CHACHA */
  5270. #ifdef HAVE_POLY1305
  5271. WOLFSSL_TEST_SUBROUTINE int poly1305_test(void)
  5272. {
  5273. int ret = 0;
  5274. int i;
  5275. byte tag[16];
  5276. Poly1305 enc;
  5277. WOLFSSL_SMALL_STACK_STATIC const byte msg1[] =
  5278. {
  5279. 0x43,0x72,0x79,0x70,0x74,0x6f,0x67,0x72,
  5280. 0x61,0x70,0x68,0x69,0x63,0x20,0x46,0x6f,
  5281. 0x72,0x75,0x6d,0x20,0x52,0x65,0x73,0x65,
  5282. 0x61,0x72,0x63,0x68,0x20,0x47,0x72,0x6f,
  5283. 0x75,0x70
  5284. };
  5285. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] =
  5286. {
  5287. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x77,0x6f,0x72,
  5288. 0x6c,0x64,0x21
  5289. };
  5290. WOLFSSL_SMALL_STACK_STATIC const byte msg3[] =
  5291. {
  5292. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5293. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5294. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5295. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  5296. };
  5297. WOLFSSL_SMALL_STACK_STATIC const byte msg4[] =
  5298. {
  5299. 0xd3,0x1a,0x8d,0x34,0x64,0x8e,0x60,0xdb,
  5300. 0x7b,0x86,0xaf,0xbc,0x53,0xef,0x7e,0xc2,
  5301. 0xa4,0xad,0xed,0x51,0x29,0x6e,0x08,0xfe,
  5302. 0xa9,0xe2,0xb5,0xa7,0x36,0xee,0x62,0xd6,
  5303. 0x3d,0xbe,0xa4,0x5e,0x8c,0xa9,0x67,0x12,
  5304. 0x82,0xfa,0xfb,0x69,0xda,0x92,0x72,0x8b,
  5305. 0x1a,0x71,0xde,0x0a,0x9e,0x06,0x0b,0x29,
  5306. 0x05,0xd6,0xa5,0xb6,0x7e,0xcd,0x3b,0x36,
  5307. 0x92,0xdd,0xbd,0x7f,0x2d,0x77,0x8b,0x8c,
  5308. 0x98,0x03,0xae,0xe3,0x28,0x09,0x1b,0x58,
  5309. 0xfa,0xb3,0x24,0xe4,0xfa,0xd6,0x75,0x94,
  5310. 0x55,0x85,0x80,0x8b,0x48,0x31,0xd7,0xbc,
  5311. 0x3f,0xf4,0xde,0xf0,0x8e,0x4b,0x7a,0x9d,
  5312. 0xe5,0x76,0xd2,0x65,0x86,0xce,0xc6,0x4b,
  5313. 0x61,0x16
  5314. };
  5315. WOLFSSL_SMALL_STACK_STATIC const byte msg5[] =
  5316. {
  5317. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  5318. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  5319. };
  5320. WOLFSSL_SMALL_STACK_STATIC const byte msg6[] =
  5321. {
  5322. 0xd3,0x1a,0x8d,0x34,0x64,0x8e,0x60,0xdb,
  5323. 0x7b,0x86,0xaf,0xbc,0x53,0xef,0x7e,0xc2,
  5324. 0xa4,0xad,0xed,0x51,0x29,0x6e,0x08,0xfe,
  5325. 0xa9,0xe2,0xb5,0xa7,0x36,0xee,0x62,0xd6,
  5326. 0x3d,0xbe,0xa4,0x5e,0x8c,0xa9,0x67,0x12,
  5327. 0x82,0xfa,0xfb,0x69,0xda,0x92,0x72,0x8b,
  5328. 0xfa,0xb3,0x24,0xe4,0xfa,0xd6,0x75,0x94,
  5329. 0x1a,0x71,0xde,0x0a,0x9e,0x06,0x0b,0x29,
  5330. 0xa9,0xe2,0xb5,0xa7,0x36,0xee,0x62,0xd6,
  5331. 0x3d,0xbe,0xa4,0x5e,0x8c,0xa9,0x67,0x12,
  5332. 0xfa,0xb3,0x24,0xe4,0xfa,0xd6,0x75,0x94,
  5333. 0x05,0xd6,0xa5,0xb6,0x7e,0xcd,0x3b,0x36,
  5334. 0x92,0xdd,0xbd,0x7f,0x2d,0x77,0x8b,0x8c,
  5335. 0x7b,0x86,0xaf,0xbc,0x53,0xef,0x7e,0xc2,
  5336. 0x98,0x03,0xae,0xe3,0x28,0x09,0x1b,0x58,
  5337. 0xfa,0xb3,0x24,0xe4,0xfa,0xd6,0x75,0x94,
  5338. 0x55,0x85,0x80,0x8b,0x48,0x31,0xd7,0xbc,
  5339. 0x3f,0xf4,0xde,0xf0,0x8e,0x4b,0x7a,0x9d,
  5340. 0xe5,0x76,0xd2,0x65,0x86,0xce,0xc6,0x4b,
  5341. 0x61,0x16
  5342. };
  5343. byte additional[] =
  5344. {
  5345. 0x50,0x51,0x52,0x53,0xc0,0xc1,0xc2,0xc3,
  5346. 0xc4,0xc5,0xc6,0xc7
  5347. };
  5348. WOLFSSL_SMALL_STACK_STATIC const byte correct0[] =
  5349. {
  5350. 0x01,0x03,0x80,0x8a,0xfb,0x0d,0xb2,0xfd,
  5351. 0x4a,0xbf,0xf6,0xaf,0x41,0x49,0xf5,0x1b
  5352. };
  5353. WOLFSSL_SMALL_STACK_STATIC const byte correct1[] =
  5354. {
  5355. 0xa8,0x06,0x1d,0xc1,0x30,0x51,0x36,0xc6,
  5356. 0xc2,0x2b,0x8b,0xaf,0x0c,0x01,0x27,0xa9
  5357. };
  5358. WOLFSSL_SMALL_STACK_STATIC const byte correct2[] =
  5359. {
  5360. 0xa6,0xf7,0x45,0x00,0x8f,0x81,0xc9,0x16,
  5361. 0xa2,0x0d,0xcc,0x74,0xee,0xf2,0xb2,0xf0
  5362. };
  5363. WOLFSSL_SMALL_STACK_STATIC const byte correct3[] =
  5364. {
  5365. 0x49,0xec,0x78,0x09,0x0e,0x48,0x1e,0xc6,
  5366. 0xc2,0x6b,0x33,0xb9,0x1c,0xcc,0x03,0x07
  5367. };
  5368. WOLFSSL_SMALL_STACK_STATIC const byte correct4[] =
  5369. {
  5370. 0x1a,0xe1,0x0b,0x59,0x4f,0x09,0xe2,0x6a,
  5371. 0x7e,0x90,0x2e,0xcb,0xd0,0x60,0x06,0x91
  5372. };
  5373. WOLFSSL_SMALL_STACK_STATIC const byte correct5[] =
  5374. {
  5375. 0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5376. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5377. };
  5378. WOLFSSL_SMALL_STACK_STATIC const byte correct6[] =
  5379. {
  5380. 0xea,0x11,0x5c,0x4f,0xd0,0xc0,0x10,0xae,
  5381. 0xf7,0xdf,0xda,0x77,0xa2,0xe9,0xaf,0xca
  5382. };
  5383. WOLFSSL_SMALL_STACK_STATIC const byte key[] = {
  5384. 0x85,0xd6,0xbe,0x78,0x57,0x55,0x6d,0x33,
  5385. 0x7f,0x44,0x52,0xfe,0x42,0xd5,0x06,0xa8,
  5386. 0x01,0x03,0x80,0x8a,0xfb,0x0d,0xb2,0xfd,
  5387. 0x4a,0xbf,0xf6,0xaf,0x41,0x49,0xf5,0x1b
  5388. };
  5389. WOLFSSL_SMALL_STACK_STATIC const byte key2[] = {
  5390. 0x74,0x68,0x69,0x73,0x20,0x69,0x73,0x20,
  5391. 0x33,0x32,0x2d,0x62,0x79,0x74,0x65,0x20,
  5392. 0x6b,0x65,0x79,0x20,0x66,0x6f,0x72,0x20,
  5393. 0x50,0x6f,0x6c,0x79,0x31,0x33,0x30,0x35
  5394. };
  5395. WOLFSSL_SMALL_STACK_STATIC const byte key4[] = {
  5396. 0x7b,0xac,0x2b,0x25,0x2d,0xb4,0x47,0xaf,
  5397. 0x09,0xb6,0x7a,0x55,0xa4,0xe9,0x55,0x84,
  5398. 0x0a,0xe1,0xd6,0x73,0x10,0x75,0xd9,0xeb,
  5399. 0x2a,0x93,0x75,0x78,0x3e,0xd5,0x53,0xff
  5400. };
  5401. WOLFSSL_SMALL_STACK_STATIC const byte key5[] = {
  5402. 0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5403. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5404. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5405. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  5406. };
  5407. const byte* msgs[] = {NULL, msg1, msg2, msg3, msg5, msg6};
  5408. word32 szm[] = {0, sizeof(msg1), sizeof(msg2),
  5409. sizeof(msg3), sizeof(msg5), sizeof(msg6)};
  5410. const byte* keys[] = {key, key, key2, key2, key5, key};
  5411. const byte* tests[] = {correct0, correct1, correct2, correct3, correct5,
  5412. correct6};
  5413. for (i = 0; i < 6; i++) {
  5414. ret = wc_Poly1305SetKey(&enc, keys[i], 32);
  5415. if (ret != 0)
  5416. return -4800 - i;
  5417. ret = wc_Poly1305Update(&enc, msgs[i], szm[i]);
  5418. if (ret != 0)
  5419. return -4810 - i;
  5420. ret = wc_Poly1305Final(&enc, tag);
  5421. if (ret != 0)
  5422. return -4820 - i;
  5423. if (XMEMCMP(tag, tests[i], sizeof(tag)))
  5424. return -4830 - i;
  5425. }
  5426. /* Check TLS MAC function from 2.8.2 https://tools.ietf.org/html/rfc7539 */
  5427. XMEMSET(tag, 0, sizeof(tag));
  5428. ret = wc_Poly1305SetKey(&enc, key4, sizeof(key4));
  5429. if (ret != 0)
  5430. return -4840;
  5431. ret = wc_Poly1305_MAC(&enc, additional, sizeof(additional),
  5432. (byte*)msg4, sizeof(msg4), tag, sizeof(tag));
  5433. if (ret != 0)
  5434. return -4841;
  5435. if (XMEMCMP(tag, correct4, sizeof(tag)))
  5436. return -4842;
  5437. /* Check fail of TLS MAC function if altering additional data */
  5438. XMEMSET(tag, 0, sizeof(tag));
  5439. additional[0]++;
  5440. ret = wc_Poly1305_MAC(&enc, additional, sizeof(additional),
  5441. (byte*)msg4, sizeof(msg4), tag, sizeof(tag));
  5442. if (ret != 0)
  5443. return -4843;
  5444. if (XMEMCMP(tag, correct4, sizeof(tag)) == 0)
  5445. return -4844;
  5446. return 0;
  5447. }
  5448. #endif /* HAVE_POLY1305 */
  5449. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  5450. WOLFSSL_TEST_SUBROUTINE int chacha20_poly1305_aead_test(void)
  5451. {
  5452. /* Test #1 from Section 2.8.2 of draft-irtf-cfrg-chacha20-poly1305-10 */
  5453. /* https://tools.ietf.org/html/draft-irtf-cfrg-chacha20-poly1305-10 */
  5454. WOLFSSL_SMALL_STACK_STATIC const byte key1[] = {
  5455. 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
  5456. 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  5457. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
  5458. 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f
  5459. };
  5460. WOLFSSL_SMALL_STACK_STATIC const byte plaintext1[] = {
  5461. 0x4c, 0x61, 0x64, 0x69, 0x65, 0x73, 0x20, 0x61,
  5462. 0x6e, 0x64, 0x20, 0x47, 0x65, 0x6e, 0x74, 0x6c,
  5463. 0x65, 0x6d, 0x65, 0x6e, 0x20, 0x6f, 0x66, 0x20,
  5464. 0x74, 0x68, 0x65, 0x20, 0x63, 0x6c, 0x61, 0x73,
  5465. 0x73, 0x20, 0x6f, 0x66, 0x20, 0x27, 0x39, 0x39,
  5466. 0x3a, 0x20, 0x49, 0x66, 0x20, 0x49, 0x20, 0x63,
  5467. 0x6f, 0x75, 0x6c, 0x64, 0x20, 0x6f, 0x66, 0x66,
  5468. 0x65, 0x72, 0x20, 0x79, 0x6f, 0x75, 0x20, 0x6f,
  5469. 0x6e, 0x6c, 0x79, 0x20, 0x6f, 0x6e, 0x65, 0x20,
  5470. 0x74, 0x69, 0x70, 0x20, 0x66, 0x6f, 0x72, 0x20,
  5471. 0x74, 0x68, 0x65, 0x20, 0x66, 0x75, 0x74, 0x75,
  5472. 0x72, 0x65, 0x2c, 0x20, 0x73, 0x75, 0x6e, 0x73,
  5473. 0x63, 0x72, 0x65, 0x65, 0x6e, 0x20, 0x77, 0x6f,
  5474. 0x75, 0x6c, 0x64, 0x20, 0x62, 0x65, 0x20, 0x69,
  5475. 0x74, 0x2e
  5476. };
  5477. WOLFSSL_SMALL_STACK_STATIC const byte iv1[] = {
  5478. 0x07, 0x00, 0x00, 0x00, 0x40, 0x41, 0x42, 0x43,
  5479. 0x44, 0x45, 0x46, 0x47
  5480. };
  5481. WOLFSSL_SMALL_STACK_STATIC const byte aad1[] = { /* additional data */
  5482. 0x50, 0x51, 0x52, 0x53, 0xc0, 0xc1, 0xc2, 0xc3,
  5483. 0xc4, 0xc5, 0xc6, 0xc7
  5484. };
  5485. WOLFSSL_SMALL_STACK_STATIC const byte cipher1[] = { /* expected output from operation */
  5486. 0xd3, 0x1a, 0x8d, 0x34, 0x64, 0x8e, 0x60, 0xdb,
  5487. 0x7b, 0x86, 0xaf, 0xbc, 0x53, 0xef, 0x7e, 0xc2,
  5488. 0xa4, 0xad, 0xed, 0x51, 0x29, 0x6e, 0x08, 0xfe,
  5489. 0xa9, 0xe2, 0xb5, 0xa7, 0x36, 0xee, 0x62, 0xd6,
  5490. 0x3d, 0xbe, 0xa4, 0x5e, 0x8c, 0xa9, 0x67, 0x12,
  5491. 0x82, 0xfa, 0xfb, 0x69, 0xda, 0x92, 0x72, 0x8b,
  5492. 0x1a, 0x71, 0xde, 0x0a, 0x9e, 0x06, 0x0b, 0x29,
  5493. 0x05, 0xd6, 0xa5, 0xb6, 0x7e, 0xcd, 0x3b, 0x36,
  5494. 0x92, 0xdd, 0xbd, 0x7f, 0x2d, 0x77, 0x8b, 0x8c,
  5495. 0x98, 0x03, 0xae, 0xe3, 0x28, 0x09, 0x1b, 0x58,
  5496. 0xfa, 0xb3, 0x24, 0xe4, 0xfa, 0xd6, 0x75, 0x94,
  5497. 0x55, 0x85, 0x80, 0x8b, 0x48, 0x31, 0xd7, 0xbc,
  5498. 0x3f, 0xf4, 0xde, 0xf0, 0x8e, 0x4b, 0x7a, 0x9d,
  5499. 0xe5, 0x76, 0xd2, 0x65, 0x86, 0xce, 0xc6, 0x4b,
  5500. 0x61, 0x16
  5501. };
  5502. WOLFSSL_SMALL_STACK_STATIC const byte authTag1[] = { /* expected output from operation */
  5503. 0x1a, 0xe1, 0x0b, 0x59, 0x4f, 0x09, 0xe2, 0x6a,
  5504. 0x7e, 0x90, 0x2e, 0xcb, 0xd0, 0x60, 0x06, 0x91
  5505. };
  5506. /* Test #2 from Appendix A.2 in draft-irtf-cfrg-chacha20-poly1305-10 */
  5507. /* https://tools.ietf.org/html/draft-irtf-cfrg-chacha20-poly1305-10 */
  5508. WOLFSSL_SMALL_STACK_STATIC const byte key2[] = {
  5509. 0x1c, 0x92, 0x40, 0xa5, 0xeb, 0x55, 0xd3, 0x8a,
  5510. 0xf3, 0x33, 0x88, 0x86, 0x04, 0xf6, 0xb5, 0xf0,
  5511. 0x47, 0x39, 0x17, 0xc1, 0x40, 0x2b, 0x80, 0x09,
  5512. 0x9d, 0xca, 0x5c, 0xbc, 0x20, 0x70, 0x75, 0xc0
  5513. };
  5514. WOLFSSL_SMALL_STACK_STATIC const byte plaintext2[] = {
  5515. 0x49, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x65, 0x74,
  5516. 0x2d, 0x44, 0x72, 0x61, 0x66, 0x74, 0x73, 0x20,
  5517. 0x61, 0x72, 0x65, 0x20, 0x64, 0x72, 0x61, 0x66,
  5518. 0x74, 0x20, 0x64, 0x6f, 0x63, 0x75, 0x6d, 0x65,
  5519. 0x6e, 0x74, 0x73, 0x20, 0x76, 0x61, 0x6c, 0x69,
  5520. 0x64, 0x20, 0x66, 0x6f, 0x72, 0x20, 0x61, 0x20,
  5521. 0x6d, 0x61, 0x78, 0x69, 0x6d, 0x75, 0x6d, 0x20,
  5522. 0x6f, 0x66, 0x20, 0x73, 0x69, 0x78, 0x20, 0x6d,
  5523. 0x6f, 0x6e, 0x74, 0x68, 0x73, 0x20, 0x61, 0x6e,
  5524. 0x64, 0x20, 0x6d, 0x61, 0x79, 0x20, 0x62, 0x65,
  5525. 0x20, 0x75, 0x70, 0x64, 0x61, 0x74, 0x65, 0x64,
  5526. 0x2c, 0x20, 0x72, 0x65, 0x70, 0x6c, 0x61, 0x63,
  5527. 0x65, 0x64, 0x2c, 0x20, 0x6f, 0x72, 0x20, 0x6f,
  5528. 0x62, 0x73, 0x6f, 0x6c, 0x65, 0x74, 0x65, 0x64,
  5529. 0x20, 0x62, 0x79, 0x20, 0x6f, 0x74, 0x68, 0x65,
  5530. 0x72, 0x20, 0x64, 0x6f, 0x63, 0x75, 0x6d, 0x65,
  5531. 0x6e, 0x74, 0x73, 0x20, 0x61, 0x74, 0x20, 0x61,
  5532. 0x6e, 0x79, 0x20, 0x74, 0x69, 0x6d, 0x65, 0x2e,
  5533. 0x20, 0x49, 0x74, 0x20, 0x69, 0x73, 0x20, 0x69,
  5534. 0x6e, 0x61, 0x70, 0x70, 0x72, 0x6f, 0x70, 0x72,
  5535. 0x69, 0x61, 0x74, 0x65, 0x20, 0x74, 0x6f, 0x20,
  5536. 0x75, 0x73, 0x65, 0x20, 0x49, 0x6e, 0x74, 0x65,
  5537. 0x72, 0x6e, 0x65, 0x74, 0x2d, 0x44, 0x72, 0x61,
  5538. 0x66, 0x74, 0x73, 0x20, 0x61, 0x73, 0x20, 0x72,
  5539. 0x65, 0x66, 0x65, 0x72, 0x65, 0x6e, 0x63, 0x65,
  5540. 0x20, 0x6d, 0x61, 0x74, 0x65, 0x72, 0x69, 0x61,
  5541. 0x6c, 0x20, 0x6f, 0x72, 0x20, 0x74, 0x6f, 0x20,
  5542. 0x63, 0x69, 0x74, 0x65, 0x20, 0x74, 0x68, 0x65,
  5543. 0x6d, 0x20, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20,
  5544. 0x74, 0x68, 0x61, 0x6e, 0x20, 0x61, 0x73, 0x20,
  5545. 0x2f, 0xe2, 0x80, 0x9c, 0x77, 0x6f, 0x72, 0x6b,
  5546. 0x20, 0x69, 0x6e, 0x20, 0x70, 0x72, 0x6f, 0x67,
  5547. 0x72, 0x65, 0x73, 0x73, 0x2e, 0x2f, 0xe2, 0x80,
  5548. 0x9d
  5549. };
  5550. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] = {
  5551. 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04,
  5552. 0x05, 0x06, 0x07, 0x08
  5553. };
  5554. WOLFSSL_SMALL_STACK_STATIC const byte aad2[] = { /* additional data */
  5555. 0xf3, 0x33, 0x88, 0x86, 0x00, 0x00, 0x00, 0x00,
  5556. 0x00, 0x00, 0x4e, 0x91
  5557. };
  5558. WOLFSSL_SMALL_STACK_STATIC const byte cipher2[] = { /* expected output from operation */
  5559. 0x64, 0xa0, 0x86, 0x15, 0x75, 0x86, 0x1a, 0xf4,
  5560. 0x60, 0xf0, 0x62, 0xc7, 0x9b, 0xe6, 0x43, 0xbd,
  5561. 0x5e, 0x80, 0x5c, 0xfd, 0x34, 0x5c, 0xf3, 0x89,
  5562. 0xf1, 0x08, 0x67, 0x0a, 0xc7, 0x6c, 0x8c, 0xb2,
  5563. 0x4c, 0x6c, 0xfc, 0x18, 0x75, 0x5d, 0x43, 0xee,
  5564. 0xa0, 0x9e, 0xe9, 0x4e, 0x38, 0x2d, 0x26, 0xb0,
  5565. 0xbd, 0xb7, 0xb7, 0x3c, 0x32, 0x1b, 0x01, 0x00,
  5566. 0xd4, 0xf0, 0x3b, 0x7f, 0x35, 0x58, 0x94, 0xcf,
  5567. 0x33, 0x2f, 0x83, 0x0e, 0x71, 0x0b, 0x97, 0xce,
  5568. 0x98, 0xc8, 0xa8, 0x4a, 0xbd, 0x0b, 0x94, 0x81,
  5569. 0x14, 0xad, 0x17, 0x6e, 0x00, 0x8d, 0x33, 0xbd,
  5570. 0x60, 0xf9, 0x82, 0xb1, 0xff, 0x37, 0xc8, 0x55,
  5571. 0x97, 0x97, 0xa0, 0x6e, 0xf4, 0xf0, 0xef, 0x61,
  5572. 0xc1, 0x86, 0x32, 0x4e, 0x2b, 0x35, 0x06, 0x38,
  5573. 0x36, 0x06, 0x90, 0x7b, 0x6a, 0x7c, 0x02, 0xb0,
  5574. 0xf9, 0xf6, 0x15, 0x7b, 0x53, 0xc8, 0x67, 0xe4,
  5575. 0xb9, 0x16, 0x6c, 0x76, 0x7b, 0x80, 0x4d, 0x46,
  5576. 0xa5, 0x9b, 0x52, 0x16, 0xcd, 0xe7, 0xa4, 0xe9,
  5577. 0x90, 0x40, 0xc5, 0xa4, 0x04, 0x33, 0x22, 0x5e,
  5578. 0xe2, 0x82, 0xa1, 0xb0, 0xa0, 0x6c, 0x52, 0x3e,
  5579. 0xaf, 0x45, 0x34, 0xd7, 0xf8, 0x3f, 0xa1, 0x15,
  5580. 0x5b, 0x00, 0x47, 0x71, 0x8c, 0xbc, 0x54, 0x6a,
  5581. 0x0d, 0x07, 0x2b, 0x04, 0xb3, 0x56, 0x4e, 0xea,
  5582. 0x1b, 0x42, 0x22, 0x73, 0xf5, 0x48, 0x27, 0x1a,
  5583. 0x0b, 0xb2, 0x31, 0x60, 0x53, 0xfa, 0x76, 0x99,
  5584. 0x19, 0x55, 0xeb, 0xd6, 0x31, 0x59, 0x43, 0x4e,
  5585. 0xce, 0xbb, 0x4e, 0x46, 0x6d, 0xae, 0x5a, 0x10,
  5586. 0x73, 0xa6, 0x72, 0x76, 0x27, 0x09, 0x7a, 0x10,
  5587. 0x49, 0xe6, 0x17, 0xd9, 0x1d, 0x36, 0x10, 0x94,
  5588. 0xfa, 0x68, 0xf0, 0xff, 0x77, 0x98, 0x71, 0x30,
  5589. 0x30, 0x5b, 0xea, 0xba, 0x2e, 0xda, 0x04, 0xdf,
  5590. 0x99, 0x7b, 0x71, 0x4d, 0x6c, 0x6f, 0x2c, 0x29,
  5591. 0xa6, 0xad, 0x5c, 0xb4, 0x02, 0x2b, 0x02, 0x70,
  5592. 0x9b
  5593. };
  5594. WOLFSSL_SMALL_STACK_STATIC const byte authTag2[] = { /* expected output from operation */
  5595. 0xee, 0xad, 0x9d, 0x67, 0x89, 0x0c, 0xbb, 0x22,
  5596. 0x39, 0x23, 0x36, 0xfe, 0xa1, 0x85, 0x1f, 0x38
  5597. };
  5598. byte generatedCiphertext[265]; /* max plaintext2/cipher2 */
  5599. byte generatedPlaintext[265]; /* max plaintext2/cipher2 */
  5600. byte generatedAuthTag[CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE];
  5601. int err;
  5602. ChaChaPoly_Aead aead;
  5603. #if !defined(USE_INTEL_CHACHA_SPEEDUP) && !defined(WOLFSSL_ARMASM)
  5604. #define TEST_SMALL_CHACHA_CHUNKS 32
  5605. #else
  5606. #define TEST_SMALL_CHACHA_CHUNKS 64
  5607. #endif
  5608. #ifdef TEST_SMALL_CHACHA_CHUNKS
  5609. word32 testLen;
  5610. #endif
  5611. XMEMSET(generatedCiphertext, 0, sizeof(generatedCiphertext));
  5612. XMEMSET(generatedAuthTag, 0, sizeof(generatedAuthTag));
  5613. XMEMSET(generatedPlaintext, 0, sizeof(generatedPlaintext));
  5614. /* Parameter Validation testing */
  5615. /* Encrypt */
  5616. err = wc_ChaCha20Poly1305_Encrypt(NULL, iv1, aad1, sizeof(aad1), plaintext1,
  5617. sizeof(plaintext1), generatedCiphertext, generatedAuthTag);
  5618. if (err != BAD_FUNC_ARG)
  5619. return -4900;
  5620. err = wc_ChaCha20Poly1305_Encrypt(key1, NULL, aad1, sizeof(aad1),
  5621. plaintext1, sizeof(plaintext1), generatedCiphertext,
  5622. generatedAuthTag);
  5623. if (err != BAD_FUNC_ARG)
  5624. return -4901;
  5625. err = wc_ChaCha20Poly1305_Encrypt(key1, iv1, aad1, sizeof(aad1), NULL,
  5626. sizeof(plaintext1), generatedCiphertext, generatedAuthTag);
  5627. if (err != BAD_FUNC_ARG)
  5628. return -4902;
  5629. err = wc_ChaCha20Poly1305_Encrypt(key1, iv1, aad1, sizeof(aad1), plaintext1,
  5630. sizeof(plaintext1), NULL, generatedAuthTag);
  5631. if (err != BAD_FUNC_ARG)
  5632. return -4903;
  5633. err = wc_ChaCha20Poly1305_Encrypt(key1, iv1, aad1, sizeof(aad1), plaintext1,
  5634. sizeof(plaintext1), generatedCiphertext, NULL);
  5635. if (err != BAD_FUNC_ARG)
  5636. return -4904;
  5637. err = wc_ChaCha20Poly1305_Encrypt(key1, iv1, aad1, sizeof(aad1), NULL,
  5638. sizeof(plaintext1), generatedCiphertext, generatedAuthTag);
  5639. if (err != BAD_FUNC_ARG)
  5640. return -4905;
  5641. /* Decrypt */
  5642. err = wc_ChaCha20Poly1305_Decrypt(NULL, iv2, aad2, sizeof(aad2), cipher2,
  5643. sizeof(cipher2), authTag2, generatedPlaintext);
  5644. if (err != BAD_FUNC_ARG)
  5645. return -4906;
  5646. err = wc_ChaCha20Poly1305_Decrypt(key2, NULL, aad2, sizeof(aad2), cipher2,
  5647. sizeof(cipher2), authTag2, generatedPlaintext);
  5648. if (err != BAD_FUNC_ARG)
  5649. return -4907;
  5650. err = wc_ChaCha20Poly1305_Decrypt(key2, iv2, aad2, sizeof(aad2), NULL,
  5651. sizeof(cipher2), authTag2, generatedPlaintext);
  5652. if (err != BAD_FUNC_ARG)
  5653. return -4908;
  5654. err = wc_ChaCha20Poly1305_Decrypt(key2, iv2, aad2, sizeof(aad2), cipher2,
  5655. sizeof(cipher2), NULL, generatedPlaintext);
  5656. if (err != BAD_FUNC_ARG)
  5657. return -4909;
  5658. err = wc_ChaCha20Poly1305_Decrypt(key2, iv2, aad2, sizeof(aad2), cipher2,
  5659. sizeof(cipher2), authTag2, NULL);
  5660. if (err != BAD_FUNC_ARG)
  5661. return -4910;
  5662. err = wc_ChaCha20Poly1305_Decrypt(key2, iv2, aad2, sizeof(aad2), NULL,
  5663. sizeof(cipher2), authTag2, generatedPlaintext);
  5664. if (err != BAD_FUNC_ARG)
  5665. return -4911;
  5666. /* Test #1 */
  5667. err = wc_ChaCha20Poly1305_Encrypt(key1, iv1,
  5668. aad1, sizeof(aad1),
  5669. plaintext1, sizeof(plaintext1),
  5670. generatedCiphertext, generatedAuthTag);
  5671. if (err) {
  5672. return err;
  5673. }
  5674. /* -- Check the ciphertext and authtag */
  5675. if (XMEMCMP(generatedCiphertext, cipher1, sizeof(cipher1))) {
  5676. return -4912;
  5677. }
  5678. if (XMEMCMP(generatedAuthTag, authTag1, sizeof(authTag1))) {
  5679. return -4913;
  5680. }
  5681. /* -- Verify decryption works */
  5682. err = wc_ChaCha20Poly1305_Decrypt(key1, iv1,
  5683. aad1, sizeof(aad1),
  5684. cipher1, sizeof(cipher1),
  5685. authTag1, generatedPlaintext);
  5686. if (err) {
  5687. return err;
  5688. }
  5689. if (XMEMCMP(generatedPlaintext, plaintext1, sizeof(plaintext1))) {
  5690. return -4914;
  5691. }
  5692. XMEMSET(generatedCiphertext, 0, sizeof(generatedCiphertext));
  5693. XMEMSET(generatedAuthTag, 0, sizeof(generatedAuthTag));
  5694. XMEMSET(generatedPlaintext, 0, sizeof(generatedPlaintext));
  5695. /* Test #2 */
  5696. err = wc_ChaCha20Poly1305_Encrypt(key2, iv2,
  5697. aad2, sizeof(aad2),
  5698. plaintext2, sizeof(plaintext2),
  5699. generatedCiphertext, generatedAuthTag);
  5700. if (err) {
  5701. return err;
  5702. }
  5703. /* -- Check the ciphertext and authtag */
  5704. if (XMEMCMP(generatedCiphertext, cipher2, sizeof(cipher2))) {
  5705. return -4915;
  5706. }
  5707. if (XMEMCMP(generatedAuthTag, authTag2, sizeof(authTag2))) {
  5708. return -4916;
  5709. }
  5710. /* -- Verify decryption works */
  5711. err = wc_ChaCha20Poly1305_Decrypt(key2, iv2,
  5712. aad2, sizeof(aad2),
  5713. cipher2, sizeof(cipher2),
  5714. authTag2, generatedPlaintext);
  5715. if (err) {
  5716. return err;
  5717. }
  5718. if (XMEMCMP(generatedPlaintext, plaintext2, sizeof(plaintext2))) {
  5719. return -4917;
  5720. }
  5721. /* AEAD init/update/final - bad argument tests */
  5722. err = wc_ChaCha20Poly1305_Init(NULL, key1, iv1,
  5723. CHACHA20_POLY1305_AEAD_DECRYPT);
  5724. if (err != BAD_FUNC_ARG)
  5725. return -4918;
  5726. err = wc_ChaCha20Poly1305_Init(&aead, NULL, iv1,
  5727. CHACHA20_POLY1305_AEAD_DECRYPT);
  5728. if (err != BAD_FUNC_ARG)
  5729. return -4919;
  5730. err = wc_ChaCha20Poly1305_Init(&aead, key1, NULL,
  5731. CHACHA20_POLY1305_AEAD_DECRYPT);
  5732. if (err != BAD_FUNC_ARG)
  5733. return -4920;
  5734. err = wc_ChaCha20Poly1305_UpdateAad(NULL, aad1, sizeof(aad1));
  5735. if (err != BAD_FUNC_ARG)
  5736. return -4921;
  5737. err = wc_ChaCha20Poly1305_UpdateAad(&aead, NULL, sizeof(aad1));
  5738. if (err != BAD_FUNC_ARG)
  5739. return -4922;
  5740. err = wc_ChaCha20Poly1305_UpdateData(NULL, generatedPlaintext,
  5741. generatedPlaintext, sizeof(plaintext1));
  5742. if (err != BAD_FUNC_ARG)
  5743. return -4923;
  5744. err = wc_ChaCha20Poly1305_UpdateData(&aead, generatedPlaintext, NULL,
  5745. sizeof(plaintext1));
  5746. if (err != BAD_FUNC_ARG)
  5747. return -4924;
  5748. err = wc_ChaCha20Poly1305_UpdateData(&aead, NULL, generatedPlaintext,
  5749. sizeof(plaintext1));
  5750. if (err != BAD_FUNC_ARG)
  5751. return -4925;
  5752. err = wc_ChaCha20Poly1305_Final(NULL, generatedAuthTag);
  5753. if (err != BAD_FUNC_ARG)
  5754. return -4926;
  5755. err = wc_ChaCha20Poly1305_Final(&aead, NULL);
  5756. if (err != BAD_FUNC_ARG)
  5757. return -4927;
  5758. /* AEAD init/update/final - bad state tests */
  5759. /* clear struct - make valgrind happy to resolve
  5760. "Conditional jump or move depends on uninitialised value(s)".
  5761. The enum is "int" size and aead.state is "byte" */
  5762. /* The wc_ChaCha20Poly1305_Init function does this normally */
  5763. XMEMSET(&aead, 0, sizeof(aead));
  5764. aead.state = CHACHA20_POLY1305_STATE_INIT;
  5765. err = wc_ChaCha20Poly1305_UpdateAad(&aead, aad1, sizeof(aad1));
  5766. if (err != BAD_STATE_E)
  5767. return -4928;
  5768. aead.state = CHACHA20_POLY1305_STATE_DATA;
  5769. err = wc_ChaCha20Poly1305_UpdateAad(&aead, aad1, sizeof(aad1));
  5770. if (err != BAD_STATE_E)
  5771. return -4929;
  5772. aead.state = CHACHA20_POLY1305_STATE_INIT;
  5773. err = wc_ChaCha20Poly1305_UpdateData(&aead, generatedPlaintext,
  5774. generatedPlaintext, sizeof(plaintext1));
  5775. if (err != BAD_STATE_E)
  5776. return -4930;
  5777. aead.state = CHACHA20_POLY1305_STATE_INIT;
  5778. err = wc_ChaCha20Poly1305_Final(&aead, generatedAuthTag);
  5779. if (err != BAD_STATE_E)
  5780. return -4931;
  5781. aead.state = CHACHA20_POLY1305_STATE_READY;
  5782. err = wc_ChaCha20Poly1305_Final(&aead, generatedAuthTag);
  5783. if (err != BAD_STATE_E)
  5784. return -4932;
  5785. XMEMSET(generatedCiphertext, 0, sizeof(generatedCiphertext));
  5786. XMEMSET(generatedAuthTag, 0, sizeof(generatedAuthTag));
  5787. XMEMSET(generatedPlaintext, 0, sizeof(generatedPlaintext));
  5788. /* Test 1 - Encrypt */
  5789. err = wc_ChaCha20Poly1305_Init(&aead, key1, iv1,
  5790. CHACHA20_POLY1305_AEAD_ENCRYPT);
  5791. if (err != 0)
  5792. return -4933;
  5793. err = wc_ChaCha20Poly1305_UpdateAad(&aead, aad1, sizeof(aad1));
  5794. if (err != 0)
  5795. return -4934;
  5796. #ifdef TEST_SMALL_CHACHA_CHUNKS
  5797. /* test doing data in smaller chunks */
  5798. for (testLen=0; testLen<sizeof(plaintext1); ) {
  5799. word32 dataLen = sizeof(plaintext1) - testLen;
  5800. if (dataLen > TEST_SMALL_CHACHA_CHUNKS)
  5801. dataLen = TEST_SMALL_CHACHA_CHUNKS;
  5802. err = wc_ChaCha20Poly1305_UpdateData(&aead, &plaintext1[testLen],
  5803. &generatedCiphertext[testLen], dataLen);
  5804. if (err != 0)
  5805. return -4935;
  5806. testLen += dataLen;
  5807. }
  5808. #else
  5809. err = wc_ChaCha20Poly1305_UpdateData(&aead, plaintext1,
  5810. generatedCiphertext, sizeof(plaintext1));
  5811. #endif
  5812. err = wc_ChaCha20Poly1305_Final(&aead, generatedAuthTag);
  5813. if (err != 0)
  5814. return -4936;
  5815. err = wc_ChaCha20Poly1305_CheckTag(generatedAuthTag, authTag1);
  5816. if (err != 0)
  5817. return -4937;
  5818. if (XMEMCMP(generatedCiphertext, cipher1, sizeof(cipher1))) {
  5819. return -4938;
  5820. }
  5821. /* Test 1 - Decrypt */
  5822. err = wc_ChaCha20Poly1305_Init(&aead, key1, iv1,
  5823. CHACHA20_POLY1305_AEAD_DECRYPT);
  5824. if (err != 0)
  5825. return -4939;
  5826. err = wc_ChaCha20Poly1305_UpdateAad(&aead, aad1, sizeof(aad1));
  5827. if (err != 0)
  5828. return -4940;
  5829. #ifdef TEST_SMALL_CHACHA_CHUNKS
  5830. /* test doing data in smaller chunks */
  5831. for (testLen=0; testLen<sizeof(plaintext1); ) {
  5832. word32 dataLen = sizeof(plaintext1) - testLen;
  5833. if (dataLen > TEST_SMALL_CHACHA_CHUNKS)
  5834. dataLen = TEST_SMALL_CHACHA_CHUNKS;
  5835. err = wc_ChaCha20Poly1305_UpdateData(&aead,
  5836. &generatedCiphertext[testLen], &generatedPlaintext[testLen],
  5837. dataLen);
  5838. if (err != 0)
  5839. return -4941;
  5840. testLen += dataLen;
  5841. }
  5842. #else
  5843. err = wc_ChaCha20Poly1305_UpdateData(&aead, generatedCiphertext,
  5844. generatedPlaintext, sizeof(cipher1));
  5845. #endif
  5846. err = wc_ChaCha20Poly1305_Final(&aead, generatedAuthTag);
  5847. if (err != 0)
  5848. return -4942;
  5849. err = wc_ChaCha20Poly1305_CheckTag(generatedAuthTag, authTag1);
  5850. if (err != 0)
  5851. return -4943;
  5852. if (XMEMCMP(generatedPlaintext, plaintext1, sizeof(plaintext1))) {
  5853. return -4944;
  5854. }
  5855. XMEMSET(generatedCiphertext, 0, sizeof(generatedCiphertext));
  5856. XMEMSET(generatedAuthTag, 0, sizeof(generatedAuthTag));
  5857. XMEMSET(generatedPlaintext, 0, sizeof(generatedPlaintext));
  5858. /* Test 2 - Encrypt */
  5859. err = wc_ChaCha20Poly1305_Init(&aead, key2, iv2,
  5860. CHACHA20_POLY1305_AEAD_ENCRYPT);
  5861. if (err != 0)
  5862. return -4945;
  5863. err = wc_ChaCha20Poly1305_UpdateAad(&aead, aad2, sizeof(aad2));
  5864. if (err != 0)
  5865. return -4946;
  5866. #ifdef TEST_SMALL_CHACHA_CHUNKS
  5867. /* test doing data in smaller chunks */
  5868. for (testLen=0; testLen<sizeof(plaintext2); ) {
  5869. word32 dataLen = sizeof(plaintext2) - testLen;
  5870. if (dataLen > TEST_SMALL_CHACHA_CHUNKS)
  5871. dataLen = TEST_SMALL_CHACHA_CHUNKS;
  5872. err = wc_ChaCha20Poly1305_UpdateData(&aead, &plaintext2[testLen],
  5873. &generatedCiphertext[testLen], dataLen);
  5874. if (err != 0)
  5875. return -4947;
  5876. testLen += dataLen;
  5877. }
  5878. #else
  5879. err = wc_ChaCha20Poly1305_UpdateData(&aead, plaintext2, generatedCiphertext,
  5880. sizeof(plaintext2));
  5881. #endif
  5882. err = wc_ChaCha20Poly1305_Final(&aead, generatedAuthTag);
  5883. if (err != 0)
  5884. return -4948;
  5885. err = wc_ChaCha20Poly1305_CheckTag(generatedAuthTag, authTag2);
  5886. if (err != 0)
  5887. return -4949;
  5888. if (XMEMCMP(generatedCiphertext, cipher2, sizeof(cipher2))) {
  5889. return -4950;
  5890. }
  5891. /* Test 2 - Decrypt */
  5892. err = wc_ChaCha20Poly1305_Init(&aead, key2, iv2,
  5893. CHACHA20_POLY1305_AEAD_DECRYPT);
  5894. if (err != 0)
  5895. return -4951;
  5896. err = wc_ChaCha20Poly1305_UpdateAad(&aead, aad2, sizeof(aad2));
  5897. if (err != 0)
  5898. return -4952;
  5899. #ifdef TEST_SMALL_CHACHA_CHUNKS
  5900. /* test doing data in smaller chunks */
  5901. for (testLen=0; testLen<sizeof(plaintext2); ) {
  5902. word32 dataLen = sizeof(plaintext2) - testLen;
  5903. if (dataLen > TEST_SMALL_CHACHA_CHUNKS)
  5904. dataLen = TEST_SMALL_CHACHA_CHUNKS;
  5905. err = wc_ChaCha20Poly1305_UpdateData(&aead,
  5906. &generatedCiphertext[testLen], &generatedPlaintext[testLen],
  5907. dataLen);
  5908. if (err != 0)
  5909. return -4953;
  5910. testLen += dataLen;
  5911. }
  5912. #else
  5913. err = wc_ChaCha20Poly1305_UpdateData(&aead, generatedCiphertext,
  5914. generatedPlaintext, sizeof(cipher2));
  5915. #endif
  5916. err = wc_ChaCha20Poly1305_Final(&aead, generatedAuthTag);
  5917. if (err != 0)
  5918. return -4954;
  5919. err = wc_ChaCha20Poly1305_CheckTag(generatedAuthTag, authTag2);
  5920. if (err != 0)
  5921. return -4955;
  5922. if (XMEMCMP(generatedPlaintext, plaintext2, sizeof(plaintext2))) {
  5923. return -4956;
  5924. }
  5925. return err;
  5926. }
  5927. #endif /* HAVE_CHACHA && HAVE_POLY1305 */
  5928. #ifndef NO_DES3
  5929. WOLFSSL_TEST_SUBROUTINE int des_test(void)
  5930. {
  5931. WOLFSSL_SMALL_STACK_STATIC const byte vector[] = { /* "now is the time for all " w/o trailing 0 */
  5932. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  5933. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  5934. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  5935. };
  5936. byte plain[24];
  5937. byte cipher[24];
  5938. Des enc;
  5939. Des dec;
  5940. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  5941. {
  5942. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
  5943. };
  5944. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  5945. {
  5946. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef
  5947. };
  5948. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  5949. {
  5950. 0x8b,0x7c,0x52,0xb0,0x01,0x2b,0x6c,0xb8,
  5951. 0x4f,0x0f,0xeb,0xf3,0xfb,0x5f,0x86,0x73,
  5952. 0x15,0x85,0xb3,0x22,0x4b,0x86,0x2b,0x4b
  5953. };
  5954. int ret;
  5955. ret = wc_Des_SetKey(&enc, key, iv, DES_ENCRYPTION);
  5956. if (ret != 0)
  5957. return -5000;
  5958. ret = wc_Des_CbcEncrypt(&enc, cipher, vector, sizeof(vector));
  5959. if (ret != 0)
  5960. return -5001;
  5961. ret = wc_Des_SetKey(&dec, key, iv, DES_DECRYPTION);
  5962. if (ret != 0)
  5963. return -5002;
  5964. ret = wc_Des_CbcDecrypt(&dec, plain, cipher, sizeof(cipher));
  5965. if (ret != 0)
  5966. return -5003;
  5967. if (XMEMCMP(plain, vector, sizeof(plain)))
  5968. return -5004;
  5969. if (XMEMCMP(cipher, verify, sizeof(cipher)))
  5970. return -5005;
  5971. ret = wc_Des_CbcEncryptWithKey(cipher, vector, sizeof(vector), key, iv);
  5972. if (ret != 0)
  5973. return -5006;
  5974. #if defined(WOLFSSL_ENCRYPTED_KEYS) && !defined(NO_SHA)
  5975. {
  5976. EncryptedInfo info;
  5977. XMEMSET(&info, 0, sizeof(EncryptedInfo));
  5978. XMEMCPY(info.iv, iv, sizeof(iv));
  5979. info.ivSz = sizeof(iv);
  5980. info.keySz = sizeof(key);
  5981. info.cipherType = WC_CIPHER_DES;
  5982. ret = wc_BufferKeyEncrypt(&info, cipher, sizeof(cipher), key,
  5983. sizeof(key), WC_HASH_TYPE_SHA);
  5984. if (ret != 0)
  5985. return -5007;
  5986. /* Test invalid info ptr */
  5987. ret = wc_BufferKeyEncrypt(NULL, cipher, sizeof(cipher), key,
  5988. sizeof(key), WC_HASH_TYPE_SHA);
  5989. if (ret != BAD_FUNC_ARG)
  5990. return -5008;
  5991. #ifndef NO_PWDBASED
  5992. /* Test invalid hash type - only applies to wc_PBKDF1 call */
  5993. ret = wc_BufferKeyEncrypt(&info, cipher, sizeof(cipher), key,
  5994. sizeof(key), WC_HASH_TYPE_NONE);
  5995. if (ret == 0)
  5996. return -5009;
  5997. #endif /* !NO_PWDBASED */
  5998. }
  5999. #endif
  6000. return 0;
  6001. }
  6002. #endif /* !NO_DES3 */
  6003. #ifndef NO_DES3
  6004. WOLFSSL_TEST_SUBROUTINE int des3_test(void)
  6005. {
  6006. WOLFSSL_SMALL_STACK_STATIC const byte vector[] = { /* "Now is the time for all " w/o trailing 0 */
  6007. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  6008. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  6009. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  6010. };
  6011. byte plain[24];
  6012. byte cipher[24];
  6013. Des3 enc;
  6014. Des3 dec;
  6015. WOLFSSL_SMALL_STACK_STATIC const byte key3[] =
  6016. {
  6017. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  6018. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  6019. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  6020. };
  6021. WOLFSSL_SMALL_STACK_STATIC const byte iv3[] =
  6022. {
  6023. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  6024. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  6025. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  6026. };
  6027. WOLFSSL_SMALL_STACK_STATIC const byte verify3[] =
  6028. {
  6029. 0x43,0xa0,0x29,0x7e,0xd1,0x84,0xf8,0x0e,
  6030. 0x89,0x64,0x84,0x32,0x12,0xd5,0x08,0x98,
  6031. 0x18,0x94,0x15,0x74,0x87,0x12,0x7d,0xb0
  6032. };
  6033. int ret;
  6034. if (wc_Des3Init(&enc, HEAP_HINT, devId) != 0)
  6035. return -5100;
  6036. if (wc_Des3Init(&dec, HEAP_HINT, devId) != 0)
  6037. return -5101;
  6038. ret = wc_Des3_SetKey(&enc, key3, iv3, DES_ENCRYPTION);
  6039. if (ret != 0)
  6040. return -5102;
  6041. ret = wc_Des3_SetKey(&dec, key3, iv3, DES_DECRYPTION);
  6042. if (ret != 0)
  6043. return -5103;
  6044. ret = wc_Des3_CbcEncrypt(&enc, cipher, vector, sizeof(vector));
  6045. #if defined(WOLFSSL_ASYNC_CRYPT)
  6046. ret = wc_AsyncWait(ret, &enc.asyncDev, WC_ASYNC_FLAG_NONE);
  6047. #endif
  6048. if (ret != 0)
  6049. return -5104;
  6050. ret = wc_Des3_CbcDecrypt(&dec, plain, cipher, sizeof(cipher));
  6051. #if defined(WOLFSSL_ASYNC_CRYPT)
  6052. ret = wc_AsyncWait(ret, &dec.asyncDev, WC_ASYNC_FLAG_NONE);
  6053. #endif
  6054. if (ret != 0)
  6055. return -5105;
  6056. if (XMEMCMP(plain, vector, sizeof(plain)))
  6057. return -5106;
  6058. if (XMEMCMP(cipher, verify3, sizeof(cipher)))
  6059. return -5107;
  6060. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  6061. /* test the same vectors with using compatibility layer */
  6062. {
  6063. DES_key_schedule ks1;
  6064. DES_key_schedule ks2;
  6065. DES_key_schedule ks3;
  6066. DES_cblock iv4;
  6067. XMEMCPY(ks1, key3, sizeof(DES_key_schedule));
  6068. XMEMCPY(ks2, key3 + 8, sizeof(DES_key_schedule));
  6069. XMEMCPY(ks3, key3 + 16, sizeof(DES_key_schedule));
  6070. XMEMCPY(iv4, iv3, sizeof(DES_cblock));
  6071. XMEMSET(plain, 0, sizeof(plain));
  6072. XMEMSET(cipher, 0, sizeof(cipher));
  6073. DES_ede3_cbc_encrypt(vector, cipher, sizeof(vector), &ks1, &ks2, &ks3,
  6074. &iv4, DES_ENCRYPT);
  6075. DES_ede3_cbc_encrypt(cipher, plain, sizeof(cipher), &ks1, &ks2, &ks3,
  6076. &iv4, DES_DECRYPT);
  6077. if (XMEMCMP(plain, vector, sizeof(plain)))
  6078. return -5108;
  6079. if (XMEMCMP(cipher, verify3, sizeof(cipher)))
  6080. return -5109;
  6081. }
  6082. #endif /* OPENSSL_EXTRA */
  6083. wc_Des3Free(&enc);
  6084. wc_Des3Free(&dec);
  6085. #if defined(WOLFSSL_ENCRYPTED_KEYS) && !defined(NO_SHA)
  6086. {
  6087. EncryptedInfo info;
  6088. XMEMSET(&info, 0, sizeof(EncryptedInfo));
  6089. XMEMCPY(info.iv, iv3, sizeof(iv3));
  6090. info.ivSz = sizeof(iv3);
  6091. info.keySz = sizeof(key3);
  6092. info.cipherType = WC_CIPHER_DES3;
  6093. ret = wc_BufferKeyEncrypt(&info, cipher, sizeof(cipher), key3,
  6094. sizeof(key3), WC_HASH_TYPE_SHA);
  6095. if (ret != 0)
  6096. return -5110;
  6097. }
  6098. #endif
  6099. return 0;
  6100. }
  6101. #endif /* NO_DES3 */
  6102. #ifndef NO_AES
  6103. #if defined(WOLFSSL_AES_OFB) || defined(WOLFSSL_AES_CFB) || \
  6104. defined(WOLFSSL_AES_XTS)
  6105. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  6106. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  6107. /* pass in the function, key, iv, plain text and expected and this function
  6108. * tests that the encryption and decryption is successful */
  6109. static int EVP_test(const WOLFSSL_EVP_CIPHER* type, const byte* key,
  6110. const byte* iv, const byte* plain, int plainSz,
  6111. const byte* expected, int expectedSz)
  6112. {
  6113. #ifdef WOLFSSL_SMALL_STACK
  6114. EVP_CIPHER_CTX *ctx = NULL;
  6115. #else
  6116. EVP_CIPHER_CTX ctx[1];
  6117. #endif
  6118. int idx, ret = 0, cipherSz;
  6119. byte* cipher;
  6120. #ifdef WOLFSSL_SMALL_STACK
  6121. if ((ctx = wolfSSL_EVP_CIPHER_CTX_new()) == NULL)
  6122. return MEMORY_E;
  6123. #endif
  6124. cipher = (byte*)XMALLOC(plainSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  6125. if (cipher == NULL) {
  6126. ret = -5120;
  6127. goto EVP_TEST_END;
  6128. }
  6129. /* test encrypt */
  6130. EVP_CIPHER_CTX_init(ctx);
  6131. if (EVP_CipherInit(ctx, type, key, iv, 1) == 0) {
  6132. ret = -5121;
  6133. goto EVP_TEST_END;
  6134. }
  6135. if (EVP_CipherUpdate(ctx, cipher, &idx, plain, expectedSz) == 0) {
  6136. ret = -5122;
  6137. goto EVP_TEST_END;
  6138. }
  6139. cipherSz = idx;
  6140. if (EVP_CipherFinal(ctx, cipher + cipherSz, &idx) == 0) {
  6141. ret = -5123;
  6142. goto EVP_TEST_END;
  6143. }
  6144. cipherSz += idx;
  6145. if (XMEMCMP(cipher, expected, plainSz)) {
  6146. ret = -5124;
  6147. goto EVP_TEST_END;
  6148. }
  6149. /* test decrypt */
  6150. EVP_CIPHER_CTX_init(ctx);
  6151. if (EVP_CipherInit(ctx, type, key, iv, 0) == 0) {
  6152. ret = -5125;
  6153. goto EVP_TEST_END;
  6154. }
  6155. if (EVP_CipherUpdate(ctx, cipher, &idx, cipher, expectedSz) == 0) {
  6156. ret = -5126;
  6157. goto EVP_TEST_END;
  6158. }
  6159. cipherSz = idx;
  6160. if (EVP_CipherFinal(ctx, cipher + cipherSz, &idx) == 0) {
  6161. ret = -5127;
  6162. goto EVP_TEST_END;
  6163. }
  6164. cipherSz += idx;
  6165. if ((expectedSz != cipherSz) || XMEMCMP(plain, cipher, plainSz)) {
  6166. ret = -5128;
  6167. goto EVP_TEST_END;
  6168. }
  6169. EVP_TEST_END:
  6170. if (cipher)
  6171. XFREE(cipher, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  6172. (void)cipherSz;
  6173. #ifdef WOLFSSL_SMALL_STACK
  6174. wolfSSL_EVP_CIPHER_CTX_free(ctx);
  6175. #endif
  6176. return ret;
  6177. }
  6178. #endif /* OPENSSL_EXTRA */
  6179. #endif /* WOLFSSL_AES_OFB || WOLFSSL_AES_CFB */
  6180. #ifdef WOLFSSL_AES_OFB
  6181. /* test vector from https://csrc.nist.gov/Projects/cryptographic-algorithm-validation-program/Block-Ciphers */
  6182. WOLFSSL_TEST_SUBROUTINE int aesofb_test(void)
  6183. {
  6184. #ifdef WOLFSSL_AES_256
  6185. WOLFSSL_SMALL_STACK_STATIC const byte key1[] =
  6186. {
  6187. 0xc4,0xc7,0xfa,0xd6,0x53,0x5c,0xb8,0x71,
  6188. 0x4a,0x5c,0x40,0x77,0x9a,0x8b,0xa1,0xd2,
  6189. 0x53,0x3e,0x23,0xb4,0xb2,0x58,0x73,0x2a,
  6190. 0x5b,0x78,0x01,0xf4,0xe3,0x71,0xa7,0x94
  6191. };
  6192. WOLFSSL_SMALL_STACK_STATIC const byte iv1[] =
  6193. {
  6194. 0x5e,0xb9,0x33,0x13,0xb8,0x71,0xff,0x16,
  6195. 0xb9,0x8a,0x9b,0xcb,0x43,0x33,0x0d,0x6f
  6196. };
  6197. WOLFSSL_SMALL_STACK_STATIC const byte plain1[] =
  6198. {
  6199. 0x6d,0x0b,0xb0,0x79,0x63,0x84,0x71,0xe9,
  6200. 0x39,0xd4,0x53,0x14,0x86,0xc1,0x4c,0x25,
  6201. 0x9a,0xee,0xc6,0xf3,0xc0,0x0d,0xfd,0xd6,
  6202. 0xc0,0x50,0xa8,0xba,0xa8,0x20,0xdb,0x71,
  6203. 0xcc,0x12,0x2c,0x4e,0x0c,0x17,0x15,0xef,
  6204. 0x55,0xf3,0x99,0x5a,0x6b,0xf0,0x2a,0x4c
  6205. };
  6206. WOLFSSL_SMALL_STACK_STATIC const byte cipher1[] =
  6207. {
  6208. 0x0f,0x54,0x61,0x71,0x59,0xd0,0x3f,0xfc,
  6209. 0x1b,0xfa,0xfb,0x60,0x29,0x30,0xd7,0x00,
  6210. 0xf4,0xa4,0xa8,0xe6,0xdd,0x93,0x94,0x46,
  6211. 0x64,0xd2,0x19,0xc4,0xc5,0x4d,0xde,0x1b,
  6212. 0x04,0x53,0xe1,0x73,0xf5,0x18,0x74,0xae,
  6213. 0xfd,0x64,0xa2,0xe1,0xe2,0x76,0x13,0xb0
  6214. };
  6215. #endif /* WOLFSSL_AES_256 */
  6216. #ifdef WOLFSSL_AES_128
  6217. WOLFSSL_SMALL_STACK_STATIC const byte key2[] =
  6218. {
  6219. 0x10,0xa5,0x88,0x69,0xd7,0x4b,0xe5,0xa3,
  6220. 0x74,0xcf,0x86,0x7c,0xfb,0x47,0x38,0x59
  6221. };
  6222. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] =
  6223. {
  6224. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6225. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  6226. };
  6227. WOLFSSL_SMALL_STACK_STATIC const byte plain2[] =
  6228. {
  6229. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6230. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  6231. };
  6232. WOLFSSL_SMALL_STACK_STATIC const byte cipher2[] =
  6233. {
  6234. 0x6d,0x25,0x1e,0x69,0x44,0xb0,0x51,0xe0,
  6235. 0x4e,0xaa,0x6f,0xb4,0xdb,0xf7,0x84,0x65
  6236. };
  6237. #endif /* WOLFSSL_AES_128 */
  6238. #ifdef WOLFSSL_AES_192
  6239. WOLFSSL_SMALL_STACK_STATIC const byte key3[] = {
  6240. 0xd0,0x77,0xa0,0x3b,0xd8,0xa3,0x89,0x73,
  6241. 0x92,0x8c,0xca,0xfe,0x4a,0x9d,0x2f,0x45,
  6242. 0x51,0x30,0xbd,0x0a,0xf5,0xae,0x46,0xa9
  6243. };
  6244. WOLFSSL_SMALL_STACK_STATIC const byte iv3[] =
  6245. {
  6246. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6247. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  6248. };
  6249. WOLFSSL_SMALL_STACK_STATIC const byte cipher3[] =
  6250. {
  6251. 0xab,0xc7,0x86,0xfb,0x1e,0xdb,0x50,0x45,
  6252. 0x80,0xc4,0xd8,0x82,0xef,0x29,0xa0,0xc7
  6253. };
  6254. WOLFSSL_SMALL_STACK_STATIC const byte plain3[] =
  6255. {
  6256. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6257. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  6258. };
  6259. #endif /* WOLFSSL_AES_192 */
  6260. #ifdef WOLFSSL_SMALL_STACK
  6261. Aes *enc = NULL;
  6262. #else
  6263. Aes enc[1];
  6264. #endif
  6265. byte cipher[AES_BLOCK_SIZE * 4];
  6266. #ifdef HAVE_AES_DECRYPT
  6267. #ifdef WOLFSSL_SMALL_STACK
  6268. Aes *dec = NULL;
  6269. #else
  6270. Aes dec[1];
  6271. #endif
  6272. byte plain [AES_BLOCK_SIZE * 4];
  6273. #endif
  6274. int ret = 0;
  6275. #ifdef WOLFSSL_SMALL_STACK
  6276. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  6277. ERROR_OUT(-1, out);
  6278. #ifdef HAVE_AES_DECRYPT
  6279. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  6280. ERROR_OUT(-1, out);
  6281. #endif
  6282. #endif
  6283. XMEMSET(enc, 0, sizeof *enc);
  6284. #ifdef HAVE_AES_DECRYPT
  6285. XMEMSET(dec, 0, sizeof *dec);
  6286. #endif
  6287. #ifdef WOLFSSL_AES_128
  6288. /* 128 key size test */
  6289. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  6290. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  6291. ret = EVP_test(EVP_aes_128_ofb(), key2, iv2, plain2, sizeof(plain2),
  6292. cipher2, sizeof(cipher2));
  6293. if (ret != 0) {
  6294. goto out;
  6295. }
  6296. #endif
  6297. ret = wc_AesSetKey(enc, key2, sizeof(key2), iv2, AES_ENCRYPTION);
  6298. if (ret != 0)
  6299. ERROR_OUT(-5129, out);
  6300. #ifdef HAVE_AES_DECRYPT
  6301. /* decrypt uses AES_ENCRYPTION */
  6302. ret = wc_AesSetKey(dec, key2, sizeof(key2), iv2, AES_ENCRYPTION);
  6303. if (ret != 0)
  6304. ERROR_OUT(-5130, out);
  6305. #endif
  6306. XMEMSET(cipher, 0, sizeof(cipher));
  6307. ret = wc_AesOfbEncrypt(enc, cipher, plain2, AES_BLOCK_SIZE);
  6308. if (ret != 0)
  6309. ERROR_OUT(-5131, out);
  6310. if (XMEMCMP(cipher, cipher2, AES_BLOCK_SIZE))
  6311. ERROR_OUT(-5132, out);
  6312. #ifdef HAVE_AES_DECRYPT
  6313. ret = wc_AesOfbDecrypt(dec, plain, cipher2, AES_BLOCK_SIZE);
  6314. if (ret != 0)
  6315. ERROR_OUT(-5133, out);
  6316. if (XMEMCMP(plain, plain2, AES_BLOCK_SIZE))
  6317. ERROR_OUT(-5134, out);
  6318. #endif /* HAVE_AES_DECRYPT */
  6319. #endif /* WOLFSSL_AES_128 */
  6320. #ifdef WOLFSSL_AES_192
  6321. /* 192 key size test */
  6322. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  6323. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  6324. ret = EVP_test(EVP_aes_192_ofb(), key3, iv3, plain3, sizeof(plain3),
  6325. cipher3, sizeof(cipher3));
  6326. if (ret != 0) {
  6327. goto out;
  6328. }
  6329. #endif
  6330. ret = wc_AesSetKey(enc, key3, sizeof(key3), iv3, AES_ENCRYPTION);
  6331. if (ret != 0)
  6332. ERROR_OUT(-5135, out);
  6333. #ifdef HAVE_AES_DECRYPT
  6334. /* decrypt uses AES_ENCRYPTION */
  6335. ret = wc_AesSetKey(dec, key3, sizeof(key3), iv3, AES_ENCRYPTION);
  6336. if (ret != 0)
  6337. ERROR_OUT(-5136, out);
  6338. #endif
  6339. XMEMSET(cipher, 0, sizeof(cipher));
  6340. ret = wc_AesOfbEncrypt(enc, cipher, plain3, AES_BLOCK_SIZE);
  6341. if (ret != 0)
  6342. ERROR_OUT(-5137, out);
  6343. if (XMEMCMP(cipher, cipher3, AES_BLOCK_SIZE))
  6344. ERROR_OUT(-5138, out);
  6345. #ifdef HAVE_AES_DECRYPT
  6346. ret = wc_AesOfbDecrypt(dec, plain, cipher3, AES_BLOCK_SIZE);
  6347. if (ret != 0)
  6348. ERROR_OUT(-5139, out);
  6349. if (XMEMCMP(plain, plain3, AES_BLOCK_SIZE))
  6350. ERROR_OUT(-5140, out);
  6351. #endif /* HAVE_AES_DECRYPT */
  6352. #endif /* WOLFSSL_AES_192 */
  6353. #ifdef WOLFSSL_AES_256
  6354. /* 256 key size test */
  6355. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  6356. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  6357. ret = EVP_test(EVP_aes_256_ofb(), key1, iv1, plain1, sizeof(plain1),
  6358. cipher1, sizeof(cipher1));
  6359. if (ret != 0) {
  6360. goto out;
  6361. }
  6362. #endif
  6363. ret = wc_AesSetKey(enc, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  6364. if (ret != 0)
  6365. ERROR_OUT(-5141, out);
  6366. #ifdef HAVE_AES_DECRYPT
  6367. /* decrypt uses AES_ENCRYPTION */
  6368. ret = wc_AesSetKey(dec, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  6369. if (ret != 0)
  6370. ERROR_OUT(-5142, out);
  6371. #endif
  6372. XMEMSET(cipher, 0, sizeof(cipher));
  6373. ret = wc_AesOfbEncrypt(enc, cipher, plain1, AES_BLOCK_SIZE);
  6374. if (ret != 0)
  6375. ERROR_OUT(-5143, out);
  6376. if (XMEMCMP(cipher, cipher1, AES_BLOCK_SIZE))
  6377. ERROR_OUT(-5144, out);
  6378. ret = wc_AesOfbEncrypt(enc, cipher + AES_BLOCK_SIZE,
  6379. plain1 + AES_BLOCK_SIZE, AES_BLOCK_SIZE);
  6380. if (ret != 0)
  6381. ERROR_OUT(-5145, out);
  6382. if (XMEMCMP(cipher + AES_BLOCK_SIZE, cipher1 + AES_BLOCK_SIZE,
  6383. AES_BLOCK_SIZE))
  6384. ERROR_OUT(-5146, out);
  6385. #ifdef HAVE_AES_DECRYPT
  6386. ret = wc_AesOfbDecrypt(dec, plain, cipher1, AES_BLOCK_SIZE);
  6387. if (ret != 0)
  6388. ERROR_OUT(-5147, out);
  6389. if (XMEMCMP(plain, plain1, AES_BLOCK_SIZE))
  6390. ERROR_OUT(-5148, out);
  6391. ret = wc_AesOfbDecrypt(dec, plain + AES_BLOCK_SIZE,
  6392. cipher1 + AES_BLOCK_SIZE, AES_BLOCK_SIZE);
  6393. if (ret != 0)
  6394. ERROR_OUT(-5149, out);
  6395. if (XMEMCMP(plain + AES_BLOCK_SIZE, plain1 + AES_BLOCK_SIZE,
  6396. AES_BLOCK_SIZE))
  6397. ERROR_OUT(-5150, out);
  6398. #endif /* HAVE_AES_DECRYPT */
  6399. /* multiple blocks at once */
  6400. ret = wc_AesSetKey(enc, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  6401. if (ret != 0)
  6402. ERROR_OUT(-5151, out);
  6403. #ifdef HAVE_AES_DECRYPT
  6404. /* decrypt uses AES_ENCRYPTION */
  6405. ret = wc_AesSetKey(dec, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  6406. if (ret != 0)
  6407. ERROR_OUT(-5152, out);
  6408. #endif
  6409. XMEMSET(cipher, 0, sizeof(cipher));
  6410. ret = wc_AesOfbEncrypt(enc, cipher, plain1, AES_BLOCK_SIZE * 3);
  6411. if (ret != 0)
  6412. ERROR_OUT(-5153, out);
  6413. if (XMEMCMP(cipher, cipher1, AES_BLOCK_SIZE * 3))
  6414. ERROR_OUT(-5154, out);
  6415. #ifdef HAVE_AES_DECRYPT
  6416. ret = wc_AesOfbDecrypt(dec, plain, cipher1, AES_BLOCK_SIZE * 3);
  6417. if (ret != 0)
  6418. ERROR_OUT(-5155, out);
  6419. if (XMEMCMP(plain, plain1, AES_BLOCK_SIZE * 3))
  6420. ERROR_OUT(-5156, out);
  6421. #endif /* HAVE_AES_DECRYPT */
  6422. /* inline decrypt/encrypt*/
  6423. ret = wc_AesSetKey(enc, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  6424. if (ret != 0)
  6425. ERROR_OUT(-5157, out);
  6426. #ifdef HAVE_AES_DECRYPT
  6427. /* decrypt uses AES_ENCRYPTION */
  6428. ret = wc_AesSetKey(dec, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  6429. if (ret != 0)
  6430. ERROR_OUT(-5158, out);
  6431. #endif
  6432. XMEMCPY(cipher, plain1, AES_BLOCK_SIZE * 2);
  6433. ret = wc_AesOfbEncrypt(enc, cipher, cipher, AES_BLOCK_SIZE * 2);
  6434. if (ret != 0)
  6435. ERROR_OUT(-5159, out);
  6436. if (XMEMCMP(cipher, cipher1, AES_BLOCK_SIZE * 2))
  6437. ERROR_OUT(-5160, out);
  6438. #ifdef HAVE_AES_DECRYPT
  6439. ret = wc_AesOfbDecrypt(dec, cipher, cipher, AES_BLOCK_SIZE * 2);
  6440. if (ret != 0)
  6441. ERROR_OUT(-5161, out);
  6442. if (XMEMCMP(cipher, plain1, AES_BLOCK_SIZE * 2))
  6443. ERROR_OUT(-5162, out);
  6444. #endif /* HAVE_AES_DECRYPT */
  6445. /* 256 key size test leftover support */
  6446. ret = wc_AesSetKey(enc, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  6447. if (ret != 0)
  6448. ERROR_OUT(-5163, out);
  6449. #ifdef HAVE_AES_DECRYPT
  6450. /* decrypt uses AES_ENCRYPTION */
  6451. ret = wc_AesSetKey(dec, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  6452. if (ret != 0)
  6453. ERROR_OUT(-5164, out);
  6454. #endif
  6455. XMEMSET(cipher, 0, sizeof(cipher));
  6456. ret = wc_AesOfbEncrypt(enc, cipher, plain1, 3);
  6457. if (ret != 0)
  6458. ERROR_OUT(-5165, out);
  6459. if (XMEMCMP(cipher, cipher1, 3))
  6460. ERROR_OUT(-5166, out);
  6461. ret = wc_AesOfbEncrypt(enc, cipher + 3, plain1 + 3, AES_BLOCK_SIZE);
  6462. if (ret != 0)
  6463. ERROR_OUT(-5167, out);
  6464. if (XMEMCMP(cipher + 3, cipher1 + 3, AES_BLOCK_SIZE))
  6465. ERROR_OUT(-5168, out);
  6466. #ifdef HAVE_AES_DECRYPT
  6467. ret = wc_AesOfbDecrypt(dec, plain, cipher1, 6);
  6468. if (ret != 0)
  6469. ERROR_OUT(-5169, out);
  6470. if (XMEMCMP(plain, plain1, 6))
  6471. ERROR_OUT(-5170, out);
  6472. ret = wc_AesOfbDecrypt(dec, plain + 6, cipher1 + 6, AES_BLOCK_SIZE);
  6473. if (ret != 0)
  6474. ERROR_OUT(-5171, out);
  6475. if (XMEMCMP(plain + 6, plain1 + 6, AES_BLOCK_SIZE))
  6476. ERROR_OUT(-5172, out);
  6477. #endif /* HAVE_AES_DECRYPT */
  6478. out:
  6479. wc_AesFree(enc);
  6480. wc_AesFree(dec);
  6481. #ifdef WOLFSSL_SMALL_STACK
  6482. if (enc)
  6483. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  6484. #ifdef HAVE_AES_DECRYPT
  6485. if (dec)
  6486. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  6487. #endif
  6488. #endif
  6489. #endif /* WOLFSSL_AES_256 */
  6490. return ret;
  6491. }
  6492. #endif /* WOLFSSL_AES_OFB */
  6493. #if defined(WOLFSSL_AES_CFB)
  6494. /* Test cases from NIST SP 800-38A, Recommendation for Block Cipher Modes of Operation Methods an*/
  6495. static int aescfb_test(void)
  6496. {
  6497. #ifdef WOLFSSL_SMALL_STACK
  6498. Aes *enc = NULL;
  6499. #else
  6500. Aes enc[1];
  6501. #endif
  6502. int enc_inited = 0;
  6503. byte cipher[AES_BLOCK_SIZE * 4];
  6504. #ifdef HAVE_AES_DECRYPT
  6505. #ifdef WOLFSSL_SMALL_STACK
  6506. Aes *dec = NULL;
  6507. #else
  6508. Aes dec[1];
  6509. #endif
  6510. int dec_inited = 0;
  6511. byte plain [AES_BLOCK_SIZE * 4];
  6512. #endif
  6513. int ret = 0;
  6514. WOLFSSL_SMALL_STACK_STATIC const byte iv[] = {
  6515. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  6516. 0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f
  6517. };
  6518. #ifdef WOLFSSL_AES_128
  6519. WOLFSSL_SMALL_STACK_STATIC const byte key1[] =
  6520. {
  6521. 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,
  6522. 0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c
  6523. };
  6524. WOLFSSL_SMALL_STACK_STATIC const byte cipher1[] =
  6525. {
  6526. 0x3b,0x3f,0xd9,0x2e,0xb7,0x2d,0xad,0x20,
  6527. 0x33,0x34,0x49,0xf8,0xe8,0x3c,0xfb,0x4a,
  6528. 0xc8,0xa6,0x45,0x37,0xa0,0xb3,0xa9,0x3f,
  6529. 0xcd,0xe3,0xcd,0xad,0x9f,0x1c,0xe5,0x8b,
  6530. 0x26,0x75,0x1f,0x67,0xa3,0xcb,0xb1,0x40,
  6531. 0xb1,0x80,0x8c,0xf1,0x87,0xa4,0xf4,0xdf
  6532. };
  6533. WOLFSSL_SMALL_STACK_STATIC const byte msg1[] =
  6534. {
  6535. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  6536. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  6537. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  6538. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  6539. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  6540. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef
  6541. };
  6542. #endif /* WOLFSSL_AES_128 */
  6543. #ifdef WOLFSSL_AES_192
  6544. /* 192 size key test */
  6545. WOLFSSL_SMALL_STACK_STATIC const byte key2[] =
  6546. {
  6547. 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  6548. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  6549. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b
  6550. };
  6551. WOLFSSL_SMALL_STACK_STATIC const byte cipher2[] =
  6552. {
  6553. 0xcd,0xc8,0x0d,0x6f,0xdd,0xf1,0x8c,0xab,
  6554. 0x34,0xc2,0x59,0x09,0xc9,0x9a,0x41,0x74,
  6555. 0x67,0xce,0x7f,0x7f,0x81,0x17,0x36,0x21,
  6556. 0x96,0x1a,0x2b,0x70,0x17,0x1d,0x3d,0x7a,
  6557. 0x2e,0x1e,0x8a,0x1d,0xd5,0x9b,0x88,0xb1,
  6558. 0xc8,0xe6,0x0f,0xed,0x1e,0xfa,0xc4,0xc9,
  6559. 0xc0,0x5f,0x9f,0x9c,0xa9,0x83,0x4f,0xa0,
  6560. 0x42,0xae,0x8f,0xba,0x58,0x4b,0x09,0xff
  6561. };
  6562. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] =
  6563. {
  6564. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  6565. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  6566. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  6567. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  6568. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  6569. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  6570. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  6571. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  6572. };
  6573. #endif /* WOLFSSL_AES_192 */
  6574. #ifdef WOLFSSL_AES_256
  6575. /* 256 size key simple test */
  6576. WOLFSSL_SMALL_STACK_STATIC const byte key3[] =
  6577. {
  6578. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  6579. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  6580. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  6581. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  6582. };
  6583. WOLFSSL_SMALL_STACK_STATIC const byte cipher3[] =
  6584. {
  6585. 0xdc,0x7e,0x84,0xbf,0xda,0x79,0x16,0x4b,
  6586. 0x7e,0xcd,0x84,0x86,0x98,0x5d,0x38,0x60,
  6587. 0x39,0xff,0xed,0x14,0x3b,0x28,0xb1,0xc8,
  6588. 0x32,0x11,0x3c,0x63,0x31,0xe5,0x40,0x7b,
  6589. 0xdf,0x10,0x13,0x24,0x15,0xe5,0x4b,0x92,
  6590. 0xa1,0x3e,0xd0,0xa8,0x26,0x7a,0xe2,0xf9,
  6591. 0x75,0xa3,0x85,0x74,0x1a,0xb9,0xce,0xf8,
  6592. 0x20,0x31,0x62,0x3d,0x55,0xb1,0xe4,0x71
  6593. };
  6594. WOLFSSL_SMALL_STACK_STATIC const byte msg3[] =
  6595. {
  6596. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  6597. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  6598. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  6599. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  6600. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  6601. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  6602. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  6603. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  6604. };
  6605. #endif /* WOLFSSL_AES_256 */
  6606. #ifdef WOLFSSL_SMALL_STACK
  6607. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  6608. ERROR_OUT(-1, out);
  6609. #ifdef HAVE_AES_DECRYPT
  6610. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  6611. ERROR_OUT(-1, out);
  6612. #endif
  6613. #endif
  6614. if (wc_AesInit(enc, HEAP_HINT, devId) != 0)
  6615. ERROR_OUT(-5173, out);
  6616. else
  6617. enc_inited = 1;
  6618. #ifdef HAVE_AES_DECRYPT
  6619. if (wc_AesInit(dec, HEAP_HINT, devId) != 0)
  6620. ERROR_OUT(-5174, out);
  6621. else
  6622. dec_inited = 1;
  6623. #endif
  6624. #ifdef WOLFSSL_AES_128
  6625. /* 128 key tests */
  6626. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  6627. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  6628. ret = EVP_test(EVP_aes_128_cfb128(), key1, iv, msg1, sizeof(msg1),
  6629. cipher1, sizeof(cipher1));
  6630. if (ret != 0) {
  6631. return ret;
  6632. }
  6633. #endif
  6634. ret = wc_AesSetKey(enc, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  6635. if (ret != 0)
  6636. ERROR_OUT(-5175, out);
  6637. #ifdef HAVE_AES_DECRYPT
  6638. /* decrypt uses AES_ENCRYPTION */
  6639. ret = wc_AesSetKey(dec, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  6640. if (ret != 0)
  6641. ERROR_OUT(-5176, out);
  6642. #endif
  6643. XMEMSET(cipher, 0, sizeof(cipher));
  6644. ret = wc_AesCfbEncrypt(enc, cipher, msg1, AES_BLOCK_SIZE * 2);
  6645. if (ret != 0)
  6646. ERROR_OUT(-5177, out);
  6647. if (XMEMCMP(cipher, cipher1, AES_BLOCK_SIZE * 2))
  6648. ERROR_OUT(-5178, out);
  6649. /* test restarting encryption process */
  6650. ret = wc_AesCfbEncrypt(enc, cipher + (AES_BLOCK_SIZE * 2),
  6651. msg1 + (AES_BLOCK_SIZE * 2), AES_BLOCK_SIZE);
  6652. if (ret != 0)
  6653. ERROR_OUT(-5179, out);
  6654. if (XMEMCMP(cipher + (AES_BLOCK_SIZE * 2),
  6655. cipher1 + (AES_BLOCK_SIZE * 2), AES_BLOCK_SIZE))
  6656. ERROR_OUT(-5180, out);
  6657. #ifdef HAVE_AES_DECRYPT
  6658. ret = wc_AesCfbDecrypt(dec, plain, cipher, AES_BLOCK_SIZE * 3);
  6659. if (ret != 0)
  6660. ERROR_OUT(-5181, out);
  6661. if (XMEMCMP(plain, msg1, AES_BLOCK_SIZE * 3))
  6662. ERROR_OUT(-5182, out);
  6663. #endif /* HAVE_AES_DECRYPT */
  6664. #endif /* WOLFSSL_AES_128 */
  6665. #ifdef WOLFSSL_AES_192
  6666. /* 192 key size test */
  6667. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  6668. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  6669. ret = EVP_test(EVP_aes_192_cfb128(), key2, iv, msg2, sizeof(msg2),
  6670. cipher2, sizeof(cipher2));
  6671. if (ret != 0) {
  6672. return ret;
  6673. }
  6674. #endif
  6675. ret = wc_AesSetKey(enc, key2, sizeof(key2), iv, AES_ENCRYPTION);
  6676. if (ret != 0)
  6677. ERROR_OUT(-5183, out);
  6678. #ifdef HAVE_AES_DECRYPT
  6679. /* decrypt uses AES_ENCRYPTION */
  6680. ret = wc_AesSetKey(dec, key2, sizeof(key2), iv, AES_ENCRYPTION);
  6681. if (ret != 0)
  6682. ERROR_OUT(-5184, out);
  6683. #endif
  6684. XMEMSET(cipher, 0, sizeof(cipher));
  6685. ret = wc_AesCfbEncrypt(enc, cipher, msg2, AES_BLOCK_SIZE * 4);
  6686. if (ret != 0)
  6687. ERROR_OUT(-5185, out);
  6688. if (XMEMCMP(cipher, cipher2, AES_BLOCK_SIZE * 4))
  6689. ERROR_OUT(-5186, out);
  6690. #ifdef HAVE_AES_DECRYPT
  6691. ret = wc_AesCfbDecrypt(dec, plain, cipher, AES_BLOCK_SIZE * 4);
  6692. if (ret != 0)
  6693. ERROR_OUT(-5187, out);
  6694. if (XMEMCMP(plain, msg2, AES_BLOCK_SIZE * 4))
  6695. ERROR_OUT(-5188, out);
  6696. #endif /* HAVE_AES_DECRYPT */
  6697. #endif /* WOLFSSL_AES_192 */
  6698. #ifdef WOLFSSL_AES_256
  6699. /* 256 key size test */
  6700. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  6701. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  6702. ret = EVP_test(EVP_aes_256_cfb128(), key3, iv, msg3, sizeof(msg3),
  6703. cipher3, sizeof(cipher3));
  6704. if (ret != 0) {
  6705. return ret;
  6706. }
  6707. #endif
  6708. ret = wc_AesSetKey(enc, key3, sizeof(key3), iv, AES_ENCRYPTION);
  6709. if (ret != 0)
  6710. ERROR_OUT(-5189, out);
  6711. #ifdef HAVE_AES_DECRYPT
  6712. /* decrypt uses AES_ENCRYPTION */
  6713. ret = wc_AesSetKey(dec, key3, sizeof(key3), iv, AES_ENCRYPTION);
  6714. if (ret != 0)
  6715. ERROR_OUT(-5190, out);
  6716. #endif
  6717. /* test with data left overs, magic lengths are checking near edges */
  6718. XMEMSET(cipher, 0, sizeof(cipher));
  6719. ret = wc_AesCfbEncrypt(enc, cipher, msg3, 4);
  6720. if (ret != 0)
  6721. ERROR_OUT(-5191, out);
  6722. if (XMEMCMP(cipher, cipher3, 4))
  6723. ERROR_OUT(-5192, out);
  6724. ret = wc_AesCfbEncrypt(enc, cipher + 4, msg3 + 4, 27);
  6725. if (ret != 0)
  6726. ERROR_OUT(-5193, out);
  6727. if (XMEMCMP(cipher + 4, cipher3 + 4, 27))
  6728. ERROR_OUT(-5194, out);
  6729. ret = wc_AesCfbEncrypt(enc, cipher + 31, msg3 + 31,
  6730. (AES_BLOCK_SIZE * 4) - 31);
  6731. if (ret != 0)
  6732. ERROR_OUT(-5195, out);
  6733. if (XMEMCMP(cipher, cipher3, AES_BLOCK_SIZE * 4))
  6734. ERROR_OUT(-5196, out);
  6735. #ifdef HAVE_AES_DECRYPT
  6736. ret = wc_AesCfbDecrypt(dec, plain, cipher, 4);
  6737. if (ret != 0)
  6738. ERROR_OUT(-5197, out);
  6739. if (XMEMCMP(plain, msg3, 4))
  6740. ERROR_OUT(-5198, out);
  6741. ret = wc_AesCfbDecrypt(dec, plain + 4, cipher + 4, 4);
  6742. if (ret != 0)
  6743. ERROR_OUT(-5199, out);
  6744. ret = wc_AesCfbDecrypt(dec, plain + 8, cipher + 8, 23);
  6745. if (ret != 0)
  6746. ERROR_OUT(-5200, out);
  6747. if (XMEMCMP(plain + 4, msg3 + 4, 27))
  6748. ERROR_OUT(-5201, out);
  6749. ret = wc_AesCfbDecrypt(dec, plain + 31, cipher + 31,
  6750. (AES_BLOCK_SIZE * 4) - 31);
  6751. if (ret != 0)
  6752. ERROR_OUT(-5202, out);
  6753. if (XMEMCMP(plain, msg3, AES_BLOCK_SIZE * 4))
  6754. ERROR_OUT(-5203, out);
  6755. #endif /* HAVE_AES_DECRYPT */
  6756. #endif /* WOLFSSL_AES_256 */
  6757. out:
  6758. if (enc_inited)
  6759. wc_AesFree(enc);
  6760. if (dec_inited)
  6761. wc_AesFree(dec);
  6762. #ifdef WOLFSSL_SMALL_STACK
  6763. if (enc)
  6764. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  6765. #ifdef HAVE_AES_DECRYPT
  6766. if (dec)
  6767. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  6768. #endif
  6769. #endif
  6770. return ret;
  6771. }
  6772. #if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  6773. static int aescfb1_test(void)
  6774. {
  6775. #ifdef WOLFSSL_SMALL_STACK
  6776. Aes *enc = NULL;
  6777. #else
  6778. Aes enc[1];
  6779. #endif
  6780. int enc_inited = 0;
  6781. byte cipher[AES_BLOCK_SIZE];
  6782. #ifdef HAVE_AES_DECRYPT
  6783. #ifdef WOLFSSL_SMALL_STACK
  6784. Aes *dec = NULL;
  6785. #else
  6786. Aes dec[1];
  6787. #endif
  6788. int dec_inited = 0;
  6789. byte plain [AES_BLOCK_SIZE];
  6790. #endif
  6791. int ret = 0;
  6792. #ifdef WOLFSSL_AES_128
  6793. WOLFSSL_SMALL_STACK_STATIC const byte iv[] = {
  6794. 0x4d,0xbb,0xdc,0xaa,0x59,0xf3,0x63,0xc9,
  6795. 0x2a,0x3b,0x98,0x43,0xad,0x20,0xe2,0xb7
  6796. };
  6797. WOLFSSL_SMALL_STACK_STATIC const byte key1[] =
  6798. {
  6799. 0xcd,0xef,0x9d,0x06,0x61,0xba,0xe4,0x73,
  6800. 0x8d,0x1a,0x58,0xa2,0xa6,0x22,0x8b,0x66
  6801. };
  6802. WOLFSSL_SMALL_STACK_STATIC const byte cipher1[] =
  6803. {
  6804. 0x00
  6805. };
  6806. WOLFSSL_SMALL_STACK_STATIC const byte msg1[] =
  6807. {
  6808. 0xC0
  6809. };
  6810. #endif /* WOLFSSL_AES_128 */
  6811. #ifdef WOLFSSL_AES_192
  6812. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] = {
  6813. 0x57,0xc6,0x89,0x7c,0x99,0x52,0x28,0x13,
  6814. 0xbf,0x67,0x9c,0xe1,0x13,0x70,0xaf,0x5e
  6815. };
  6816. WOLFSSL_SMALL_STACK_STATIC const byte key2[] =
  6817. {
  6818. 0xba,0xa1,0x58,0xa1,0x6b,0x50,0x4a,0x10,
  6819. 0x8e,0xd4,0x33,0x2e,0xe7,0xf2,0x9b,0xf6,
  6820. 0xd1,0xac,0x46,0xa8,0xde,0x5a,0xfe,0x7a
  6821. };
  6822. WOLFSSL_SMALL_STACK_STATIC const byte cipher2[] =
  6823. {
  6824. 0x30
  6825. };
  6826. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] =
  6827. {
  6828. 0x80
  6829. };
  6830. #endif /* WOLFSSL_AES_192 */
  6831. #ifdef WOLFSSL_AES_256
  6832. WOLFSSL_SMALL_STACK_STATIC const byte iv3[] = {
  6833. 0x63,0x2e,0x9f,0x83,0x1f,0xa3,0x80,0x5e,
  6834. 0x52,0x02,0xbc,0xe0,0x6d,0x04,0xf9,0xa0
  6835. };
  6836. WOLFSSL_SMALL_STACK_STATIC const byte key3[] =
  6837. {
  6838. 0xf6,0xfa,0xe4,0xf1,0x5d,0x91,0xfc,0x50,
  6839. 0x88,0x78,0x4f,0x84,0xa5,0x37,0x12,0x7e,
  6840. 0x32,0x63,0x55,0x9c,0x62,0x73,0x88,0x20,
  6841. 0xc2,0xcf,0x3d,0xe1,0x1c,0x2a,0x30,0x40
  6842. };
  6843. WOLFSSL_SMALL_STACK_STATIC const byte cipher3[] =
  6844. {
  6845. 0xF7, 0x00
  6846. };
  6847. WOLFSSL_SMALL_STACK_STATIC const byte msg3[] =
  6848. {
  6849. 0x41, 0xC0
  6850. };
  6851. #endif /* WOLFSSL_AES_256 */
  6852. #ifdef WOLFSSL_SMALL_STACK
  6853. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  6854. ERROR_OUT(-1, out);
  6855. #ifdef HAVE_AES_DECRYPT
  6856. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  6857. ERROR_OUT(-1, out);
  6858. #endif
  6859. #endif
  6860. if (wc_AesInit(enc, HEAP_HINT, devId) != 0)
  6861. ERROR_OUT(-5204, out);
  6862. else
  6863. enc_inited = 1;
  6864. #ifdef HAVE_AES_DECRYPT
  6865. if (wc_AesInit(dec, HEAP_HINT, devId) != 0)
  6866. ERROR_OUT(-5205, out);
  6867. else
  6868. dec_inited = 1;
  6869. #endif
  6870. #ifdef WOLFSSL_AES_128
  6871. /* 128 key tests */
  6872. ret = wc_AesSetKey(enc, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  6873. if (ret != 0)
  6874. ERROR_OUT(-5206, out);
  6875. #ifdef HAVE_AES_DECRYPT
  6876. /* decrypt uses AES_ENCRYPTION */
  6877. ret = wc_AesSetKey(dec, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  6878. if (ret != 0)
  6879. ERROR_OUT(-5207, out);
  6880. #endif
  6881. XMEMSET(cipher, 0, sizeof(cipher));
  6882. ret = wc_AesCfb1Encrypt(enc, cipher, msg1, 2);
  6883. if (ret != 0)
  6884. ERROR_OUT(-5208, out);
  6885. if (cipher[0] != cipher1[0])
  6886. ERROR_OUT(-5209, out);
  6887. #ifdef HAVE_AES_DECRYPT
  6888. ret = wc_AesCfb1Decrypt(dec, plain, cipher, 2);
  6889. if (ret != 0)
  6890. ERROR_OUT(-5210, out);
  6891. if (plain[0] != msg1[0])
  6892. ERROR_OUT(-5211, out);
  6893. #endif /* HAVE_AES_DECRYPT */
  6894. #ifdef OPENSSL_EXTRA
  6895. ret = wc_AesSetKey(enc, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  6896. if (ret != 0)
  6897. ERROR_OUT(-5212, out);
  6898. XMEMSET(cipher, 0, sizeof(cipher));
  6899. ret = wc_AesCfb1Encrypt(enc, cipher, msg1,
  6900. sizeof(msg1) * WOLFSSL_BIT_SIZE);
  6901. if (ret != 0)
  6902. ERROR_OUT(-5213, out);
  6903. #ifndef WOLFCRYPT_ONLY
  6904. ret = EVP_test(EVP_aes_128_cfb1(), key1, iv, msg1, sizeof(msg1),
  6905. cipher, sizeof(msg1));
  6906. if (ret != 0) {
  6907. goto out;
  6908. }
  6909. #endif
  6910. #endif
  6911. #endif /* WOLFSSL_AES_128 */
  6912. #ifdef WOLFSSL_AES_192
  6913. /* 192 key tests */
  6914. ret = wc_AesSetKey(enc, key2, sizeof(key2), iv2, AES_ENCRYPTION);
  6915. if (ret != 0)
  6916. ERROR_OUT(-5214, out);
  6917. XMEMSET(cipher, 0, sizeof(cipher));
  6918. ret = wc_AesCfb1Encrypt(enc, cipher, msg2, 4);
  6919. if (ret != 0)
  6920. ERROR_OUT(-5215, out);
  6921. if (XMEMCMP(cipher, cipher2, sizeof(cipher2)) != 0)
  6922. ERROR_OUT(-5216, out);
  6923. #ifdef OPENSSL_EXTRA
  6924. ret = wc_AesSetKey(enc, key2, sizeof(key2), iv2, AES_ENCRYPTION);
  6925. if (ret != 0)
  6926. ERROR_OUT(-5217, out);
  6927. XMEMSET(cipher, 0, sizeof(cipher));
  6928. ret = wc_AesCfb1Encrypt(enc, cipher, msg2,
  6929. sizeof(msg2) * WOLFSSL_BIT_SIZE);
  6930. if (ret != 0)
  6931. ERROR_OUT(-5218, out);
  6932. #ifndef WOLFCRYPT_ONLY
  6933. ret = EVP_test(EVP_aes_192_cfb1(), key2, iv2, msg2, sizeof(msg2),
  6934. cipher, sizeof(msg2));
  6935. if (ret != 0) {
  6936. goto out;
  6937. }
  6938. #endif
  6939. #endif
  6940. #endif /* WOLFSSL_AES_192 */
  6941. #ifdef WOLFSSL_AES_256
  6942. /* 256 key tests */
  6943. ret = wc_AesSetKey(enc, key3, sizeof(key3), iv3, AES_ENCRYPTION);
  6944. if (ret != 0)
  6945. ERROR_OUT(-5219, out);
  6946. XMEMSET(cipher, 0, sizeof(cipher));
  6947. ret = wc_AesCfb1Encrypt(enc, cipher, msg3, 10);
  6948. if (ret != 0)
  6949. ERROR_OUT(-5220, out);
  6950. if (XMEMCMP(cipher, cipher3, sizeof(cipher3)) != 0)
  6951. ERROR_OUT(-5221, out);
  6952. #ifdef OPENSSL_EXTRA
  6953. ret = wc_AesSetKey(enc, key3, sizeof(key3), iv3, AES_ENCRYPTION);
  6954. if (ret != 0)
  6955. ERROR_OUT(-5222, out);
  6956. XMEMSET(cipher, 0, sizeof(cipher));
  6957. ret = wc_AesCfb1Encrypt(enc, cipher, msg3,
  6958. sizeof(msg3) * WOLFSSL_BIT_SIZE);
  6959. if (ret != 0)
  6960. ERROR_OUT(-5223, out);
  6961. #ifndef WOLFCRYPT_ONLY
  6962. ret = EVP_test(EVP_aes_256_cfb1(), key3, iv3, msg3, sizeof(msg3),
  6963. cipher, sizeof(msg3));
  6964. if (ret != 0) {
  6965. goto out;
  6966. }
  6967. #endif
  6968. #endif
  6969. out:
  6970. if (enc_inited)
  6971. wc_AesFree(enc);
  6972. #ifdef HAVE_AES_DECRYPT
  6973. if (dec_inited)
  6974. wc_AesFree(dec);
  6975. #endif
  6976. #ifdef WOLFSSL_SMALL_STACK
  6977. if (enc)
  6978. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  6979. #ifdef HAVE_AES_DECRYPT
  6980. if (dec)
  6981. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  6982. #endif
  6983. #endif
  6984. #endif /* WOLFSSL_AES_256 */
  6985. return ret;
  6986. }
  6987. static int aescfb8_test(void)
  6988. {
  6989. #ifdef WOLFSSL_SMALL_STACK
  6990. Aes *enc = NULL;
  6991. #else
  6992. Aes enc[1];
  6993. #endif
  6994. int enc_inited = 0;
  6995. byte cipher[AES_BLOCK_SIZE];
  6996. #ifdef HAVE_AES_DECRYPT
  6997. #ifdef WOLFSSL_SMALL_STACK
  6998. Aes *dec = NULL;
  6999. #else
  7000. Aes dec[1];
  7001. #endif
  7002. int dec_inited = 0;
  7003. byte plain [AES_BLOCK_SIZE];
  7004. #endif
  7005. int ret = 0;
  7006. #ifdef WOLFSSL_AES_128
  7007. WOLFSSL_SMALL_STACK_STATIC const byte iv[] = {
  7008. 0xf4,0x75,0xc6,0x49,0x91,0xb2,0x0e,0xae,
  7009. 0xe1,0x83,0xa2,0x26,0x29,0xe2,0x1e,0x22
  7010. };
  7011. WOLFSSL_SMALL_STACK_STATIC const byte key1[] =
  7012. {
  7013. 0xc8,0xfe,0x9b,0xf7,0x7b,0x93,0x0f,0x46,
  7014. 0xd2,0x07,0x8b,0x8c,0x0e,0x65,0x7c,0xd4
  7015. };
  7016. WOLFSSL_SMALL_STACK_STATIC const byte cipher1[] =
  7017. {
  7018. 0xd2,0x76,0x91
  7019. };
  7020. WOLFSSL_SMALL_STACK_STATIC const byte msg1[] =
  7021. {
  7022. 0xc9,0x06,0x35
  7023. };
  7024. #endif /* WOLFSSL_AES_128 */
  7025. #ifdef WOLFSSL_AES_192
  7026. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] = {
  7027. 0x0a,0x02,0x84,0x6b,0x62,0xab,0xb6,0x93,
  7028. 0xef,0x31,0xd7,0x54,0x84,0x2e,0xed,0x29
  7029. };
  7030. WOLFSSL_SMALL_STACK_STATIC const byte key2[] =
  7031. {
  7032. 0xba,0xf0,0x8b,0x76,0x31,0x7a,0x65,0xc5,
  7033. 0xf0,0x7a,0xe6,0xf5,0x7e,0xb0,0xe6,0x54,
  7034. 0x88,0x65,0x93,0x24,0xd2,0x97,0x09,0xe3
  7035. };
  7036. WOLFSSL_SMALL_STACK_STATIC const byte cipher2[] =
  7037. {
  7038. 0x72,0x9c,0x0b,0x6d,0xeb,0x75,0xfa,0x6e,
  7039. 0xb5,0xe8
  7040. };
  7041. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] =
  7042. {
  7043. 0x98,0x95,0x93,0x24,0x02,0x39,0x3d,0xc3,
  7044. 0x3a,0x60
  7045. };
  7046. #endif
  7047. #ifdef WOLFSSL_AES_256
  7048. WOLFSSL_SMALL_STACK_STATIC const byte iv3[] = {
  7049. 0x33,0x8c,0x55,0x2f,0xf1,0xec,0xa1,0x44,
  7050. 0x08,0xe0,0x5d,0x8c,0xf9,0xf3,0xb3,0x1b
  7051. };
  7052. WOLFSSL_SMALL_STACK_STATIC const byte key3[] =
  7053. {
  7054. 0x06,0x48,0x74,0x09,0x2f,0x7a,0x13,0xcc,
  7055. 0x44,0x62,0x24,0x7a,0xd4,0x23,0xd0,0xe9,
  7056. 0x6e,0xdf,0x42,0xe8,0xb6,0x7a,0x5a,0x23,
  7057. 0xb7,0xa0,0xa6,0x47,0x7b,0x09,0x8e,0x66
  7058. };
  7059. WOLFSSL_SMALL_STACK_STATIC const byte cipher3[] =
  7060. {
  7061. 0x1c,0xff,0x95
  7062. };
  7063. WOLFSSL_SMALL_STACK_STATIC const byte msg3[] =
  7064. {
  7065. 0xb9,0x74,0xfa
  7066. };
  7067. #endif
  7068. #ifdef WOLFSSL_SMALL_STACK
  7069. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  7070. ERROR_OUT(-5238, out);
  7071. #ifdef HAVE_AES_DECRYPT
  7072. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  7073. ERROR_OUT(-5239, out);
  7074. #endif
  7075. #endif
  7076. if (wc_AesInit(enc, HEAP_HINT, devId) != 0)
  7077. ERROR_OUT(-5224, out);
  7078. else
  7079. enc_inited = 1;
  7080. #ifdef HAVE_AES_DECRYPT
  7081. if (wc_AesInit(dec, HEAP_HINT, devId) != 0)
  7082. ERROR_OUT(-5225, out);
  7083. else
  7084. dec_inited = 1;
  7085. #endif
  7086. #ifdef WOLFSSL_AES_128
  7087. /* 128 key tests */
  7088. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  7089. ret = EVP_test(EVP_aes_128_cfb8(), key1, iv, msg1, sizeof(msg1),
  7090. cipher1, sizeof(cipher1));
  7091. if (ret != 0) {
  7092. return ret;
  7093. }
  7094. #endif
  7095. ret = wc_AesSetKey(enc, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  7096. if (ret != 0)
  7097. ERROR_OUT(-5226, out);
  7098. #ifdef HAVE_AES_DECRYPT
  7099. /* decrypt uses AES_ENCRYPTION */
  7100. ret = wc_AesSetKey(dec, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  7101. if (ret != 0)
  7102. ERROR_OUT(-5227, out);
  7103. #endif
  7104. XMEMSET(cipher, 0, sizeof(cipher));
  7105. ret = wc_AesCfb8Encrypt(enc, cipher, msg1, sizeof(msg1));
  7106. if (ret != 0)
  7107. ERROR_OUT(-5228, out);
  7108. if (XMEMCMP(cipher, cipher1, sizeof(cipher1)) != 0)
  7109. ERROR_OUT(-5229, out);
  7110. #ifdef HAVE_AES_DECRYPT
  7111. ret = wc_AesCfb8Decrypt(dec, plain, cipher, sizeof(msg1));
  7112. if (ret != 0)
  7113. ERROR_OUT(-5230, out);
  7114. if (XMEMCMP(plain, msg1, sizeof(msg1)) != 0)
  7115. ERROR_OUT(-5231, out);
  7116. #endif /* HAVE_AES_DECRYPT */
  7117. #endif /* WOLFSSL_AES_128 */
  7118. #ifdef WOLFSSL_AES_192
  7119. /* 192 key tests */
  7120. ret = wc_AesSetKey(enc, key2, sizeof(key2), iv2, AES_ENCRYPTION);
  7121. if (ret != 0)
  7122. ERROR_OUT(-5232, out);
  7123. XMEMSET(cipher, 0, sizeof(cipher));
  7124. ret = wc_AesCfb8Encrypt(enc, cipher, msg2, sizeof(msg2));
  7125. if (ret != 0)
  7126. ERROR_OUT(-5233, out);
  7127. if (XMEMCMP(cipher, cipher2, sizeof(msg2)) != 0)
  7128. ERROR_OUT(-5234, out);
  7129. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  7130. ret = EVP_test(EVP_aes_192_cfb8(), key2, iv2, msg2, sizeof(msg2),
  7131. cipher2, sizeof(msg2));
  7132. if (ret != 0) {
  7133. return ret;
  7134. }
  7135. #endif
  7136. #endif /* WOLFSSL_AES_192 */
  7137. #ifdef WOLFSSL_AES_256
  7138. /* 256 key tests */
  7139. ret = wc_AesSetKey(enc, key3, sizeof(key3), iv3, AES_ENCRYPTION);
  7140. if (ret != 0)
  7141. ERROR_OUT(-5235, out);
  7142. XMEMSET(cipher, 0, sizeof(cipher));
  7143. ret = wc_AesCfb8Encrypt(enc, cipher, msg3, sizeof(msg3));
  7144. if (ret != 0)
  7145. ERROR_OUT(-5236, out);
  7146. if (XMEMCMP(cipher, cipher3, sizeof(cipher3)) != 0)
  7147. ERROR_OUT(-5237, out);
  7148. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  7149. ret = EVP_test(EVP_aes_256_cfb8(), key3, iv3, msg3, sizeof(msg3),
  7150. cipher3, sizeof(msg3));
  7151. if (ret != 0) {
  7152. goto out;
  7153. }
  7154. #endif
  7155. out:
  7156. if (enc_inited)
  7157. wc_AesFree(enc);
  7158. #ifdef HAVE_AES_DECRYPT
  7159. if (dec_inited)
  7160. wc_AesFree(dec);
  7161. #endif
  7162. #ifdef WOLFSSL_SMALL_STACK
  7163. if (enc)
  7164. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  7165. #ifdef HAVE_AES_DECRYPT
  7166. if (dec)
  7167. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  7168. #endif
  7169. #endif
  7170. #endif /* WOLFSSL_AES_256 */
  7171. return ret;
  7172. }
  7173. #endif /* !HAVE_SELFTEST && !HAVE_FIPS */
  7174. #endif /* WOLFSSL_AES_CFB */
  7175. static int aes_key_size_test(void)
  7176. {
  7177. int ret;
  7178. #ifdef WOLFSSL_SMALL_STACK
  7179. Aes *aes;
  7180. #else
  7181. Aes aes[1];
  7182. #endif
  7183. byte key16[] = { 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  7184. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66 };
  7185. #ifndef WOLFSSL_CRYPTOCELL
  7186. byte key24[] = { 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  7187. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  7188. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37 };
  7189. #endif
  7190. byte key32[] = { 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  7191. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  7192. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  7193. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66 };
  7194. byte iv[] = "1234567890abcdef";
  7195. #ifndef HAVE_FIPS
  7196. word32 keySize;
  7197. #endif
  7198. #ifdef WOLFSSL_SMALL_STACK
  7199. if ((aes = (Aes *)XMALLOC(sizeof *aes, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  7200. return -5315;
  7201. #endif
  7202. #if !defined(HAVE_FIPS) || \
  7203. defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)
  7204. /* w/ FIPS v1 (cert 2425) wc_AesInit just returns 0 always as it's not
  7205. * supported with that FIPS version */
  7206. ret = wc_AesInit(NULL, HEAP_HINT, devId);
  7207. if (ret != BAD_FUNC_ARG)
  7208. ERROR_OUT(-5300, out);
  7209. #endif
  7210. ret = wc_AesInit(aes, HEAP_HINT, devId);
  7211. /* 0 check OK for FIPSv1 */
  7212. if (ret != 0)
  7213. ERROR_OUT(-5301, out);
  7214. #ifndef HAVE_FIPS
  7215. /* Parameter Validation testing. */
  7216. ret = wc_AesGetKeySize(NULL, NULL);
  7217. if (ret != BAD_FUNC_ARG)
  7218. ERROR_OUT(-5302, out);
  7219. ret = wc_AesGetKeySize(aes, NULL);
  7220. if (ret != BAD_FUNC_ARG)
  7221. ERROR_OUT(-5303, out);
  7222. ret = wc_AesGetKeySize(NULL, &keySize);
  7223. if (ret != BAD_FUNC_ARG)
  7224. ERROR_OUT(-5304, out);
  7225. /* Crashes in FIPS */
  7226. ret = wc_AesSetKey(NULL, key16, sizeof(key16), iv, AES_ENCRYPTION);
  7227. if (ret != BAD_FUNC_ARG)
  7228. ERROR_OUT(-5305, out);
  7229. #endif
  7230. /* NULL IV indicates to use all zeros IV. */
  7231. ret = wc_AesSetKey(aes, key16, sizeof(key16), NULL, AES_ENCRYPTION);
  7232. #ifdef WOLFSSL_AES_128
  7233. if (ret != 0)
  7234. #else
  7235. if (ret != BAD_FUNC_ARG)
  7236. #endif
  7237. ERROR_OUT(-5306, out);
  7238. ret = wc_AesSetKey(aes, key32, sizeof(key32) - 1, iv, AES_ENCRYPTION);
  7239. if (ret != BAD_FUNC_ARG)
  7240. ERROR_OUT(-5307, out);
  7241. /* CryptoCell handles rounds internally */
  7242. #if !defined(HAVE_FIPS) && !defined(WOLFSSL_CRYPTOCELL)
  7243. /* PSA don't use aes->rounds */
  7244. #if !defined(WOLFSSL_HAVE_PSA) || defined(WOLFSSL_PSA_NO_AES)
  7245. /* Force invalid rounds */
  7246. aes->rounds = 16;
  7247. ret = wc_AesGetKeySize(aes, &keySize);
  7248. if (ret != BAD_FUNC_ARG)
  7249. ERROR_OUT(-5308, out);
  7250. #endif
  7251. #endif
  7252. ret = wc_AesSetKey(aes, key16, sizeof(key16), iv, AES_ENCRYPTION);
  7253. #ifdef WOLFSSL_AES_128
  7254. if (ret != 0)
  7255. #else
  7256. if (ret != BAD_FUNC_ARG)
  7257. #endif
  7258. ERROR_OUT(-5309, out);
  7259. #if !defined(HAVE_FIPS) && defined(WOLFSSL_AES_128)
  7260. ret = wc_AesGetKeySize(aes, &keySize);
  7261. if (ret != 0 || keySize != sizeof(key16))
  7262. ERROR_OUT(-5310, out);
  7263. #endif
  7264. #ifndef WOLFSSL_CRYPTOCELL
  7265. /* Cryptocell only supports AES-128 key size */
  7266. ret = wc_AesSetKey(aes, key24, sizeof(key24), iv, AES_ENCRYPTION);
  7267. #ifdef WOLFSSL_AES_192
  7268. if (ret != 0)
  7269. #else
  7270. if (ret != BAD_FUNC_ARG)
  7271. #endif
  7272. ERROR_OUT(-5311, out);
  7273. #if !defined(HAVE_FIPS) && defined(WOLFSSL_AES_192)
  7274. ret = wc_AesGetKeySize(aes, &keySize);
  7275. if (ret != 0 || keySize != sizeof(key24))
  7276. ERROR_OUT(-5312, out);
  7277. #endif
  7278. ret = wc_AesSetKey(aes, key32, sizeof(key32), iv, AES_ENCRYPTION);
  7279. #ifdef WOLFSSL_AES_256
  7280. if (ret != 0)
  7281. #else
  7282. if (ret != BAD_FUNC_ARG)
  7283. #endif
  7284. ERROR_OUT(-5313, out);
  7285. #if !defined(HAVE_FIPS) && defined(WOLFSSL_AES_256)
  7286. ret = wc_AesGetKeySize(aes, &keySize);
  7287. if (ret != 0 || keySize != sizeof(key32))
  7288. ERROR_OUT(-5314, out);
  7289. #endif
  7290. #endif /* !WOLFSSL_CRYPTOCELL */
  7291. ret = 0; /* success */
  7292. out:
  7293. wc_AesFree(aes);
  7294. #ifdef WOLFSSL_SMALL_STACK
  7295. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_AES);
  7296. #endif
  7297. return ret;
  7298. }
  7299. #if defined(WOLFSSL_AES_XTS)
  7300. /* test vectors from http://csrc.nist.gov/groups/STM/cavp/block-cipher-modes.html */
  7301. #ifdef WOLFSSL_AES_128
  7302. static int aes_xts_128_test(void)
  7303. {
  7304. #ifdef WOLFSSL_SMALL_STACK
  7305. XtsAes *aes = NULL;
  7306. #else
  7307. XtsAes aes[1];
  7308. #endif
  7309. int aes_inited = 0;
  7310. int ret = 0;
  7311. unsigned char buf[AES_BLOCK_SIZE * 2];
  7312. unsigned char cipher[AES_BLOCK_SIZE * 2];
  7313. /* 128 key tests */
  7314. WOLFSSL_SMALL_STACK_STATIC unsigned char k1[] = {
  7315. 0xa1, 0xb9, 0x0c, 0xba, 0x3f, 0x06, 0xac, 0x35,
  7316. 0x3b, 0x2c, 0x34, 0x38, 0x76, 0x08, 0x17, 0x62,
  7317. 0x09, 0x09, 0x23, 0x02, 0x6e, 0x91, 0x77, 0x18,
  7318. 0x15, 0xf2, 0x9d, 0xab, 0x01, 0x93, 0x2f, 0x2f
  7319. };
  7320. WOLFSSL_SMALL_STACK_STATIC unsigned char i1[] = {
  7321. 0x4f, 0xae, 0xf7, 0x11, 0x7c, 0xda, 0x59, 0xc6,
  7322. 0x6e, 0x4b, 0x92, 0x01, 0x3e, 0x76, 0x8a, 0xd5
  7323. };
  7324. WOLFSSL_SMALL_STACK_STATIC unsigned char p1[] = {
  7325. 0xeb, 0xab, 0xce, 0x95, 0xb1, 0x4d, 0x3c, 0x8d,
  7326. 0x6f, 0xb3, 0x50, 0x39, 0x07, 0x90, 0x31, 0x1c
  7327. };
  7328. /* plain text test of partial block is not from NIST test vector list */
  7329. WOLFSSL_SMALL_STACK_STATIC unsigned char pp[] = {
  7330. 0xeb, 0xab, 0xce, 0x95, 0xb1, 0x4d, 0x3c, 0x8d,
  7331. 0x6f, 0xb3, 0x50, 0x39, 0x07, 0x90, 0x31, 0x1c,
  7332. 0x6e, 0x4b, 0x92, 0x01, 0x3e, 0x76, 0x8a, 0xd5
  7333. };
  7334. WOLFSSL_SMALL_STACK_STATIC unsigned char c1[] = {
  7335. 0x77, 0x8a, 0xe8, 0xb4, 0x3c, 0xb9, 0x8d, 0x5a,
  7336. 0x82, 0x50, 0x81, 0xd5, 0xbe, 0x47, 0x1c, 0x63
  7337. };
  7338. WOLFSSL_SMALL_STACK_STATIC unsigned char k2[] = {
  7339. 0x39, 0x25, 0x79, 0x05, 0xdf, 0xcc, 0x77, 0x76,
  7340. 0x6c, 0x87, 0x0a, 0x80, 0x6a, 0x60, 0xe3, 0xc0,
  7341. 0x93, 0xd1, 0x2a, 0xcf, 0xcb, 0x51, 0x42, 0xfa,
  7342. 0x09, 0x69, 0x89, 0x62, 0x5b, 0x60, 0xdb, 0x16
  7343. };
  7344. WOLFSSL_SMALL_STACK_STATIC unsigned char i2[] = {
  7345. 0x5c, 0xf7, 0x9d, 0xb6, 0xc5, 0xcd, 0x99, 0x1a,
  7346. 0x1c, 0x78, 0x81, 0x42, 0x24, 0x95, 0x1e, 0x84
  7347. };
  7348. WOLFSSL_SMALL_STACK_STATIC unsigned char p2[] = {
  7349. 0xbd, 0xc5, 0x46, 0x8f, 0xbc, 0x8d, 0x50, 0xa1,
  7350. 0x0d, 0x1c, 0x85, 0x7f, 0x79, 0x1c, 0x5c, 0xba,
  7351. 0xb3, 0x81, 0x0d, 0x0d, 0x73, 0xcf, 0x8f, 0x20,
  7352. 0x46, 0xb1, 0xd1, 0x9e, 0x7d, 0x5d, 0x8a, 0x56
  7353. };
  7354. WOLFSSL_SMALL_STACK_STATIC unsigned char c2[] = {
  7355. 0xd6, 0xbe, 0x04, 0x6d, 0x41, 0xf2, 0x3b, 0x5e,
  7356. 0xd7, 0x0b, 0x6b, 0x3d, 0x5c, 0x8e, 0x66, 0x23,
  7357. 0x2b, 0xe6, 0xb8, 0x07, 0xd4, 0xdc, 0xc6, 0x0e,
  7358. 0xff, 0x8d, 0xbc, 0x1d, 0x9f, 0x7f, 0xc8, 0x22
  7359. };
  7360. #ifdef WOLFSSL_SMALL_STACK
  7361. if ((aes = (XtsAes *)XMALLOC(sizeof *aes, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  7362. ERROR_OUT(-5417, out);
  7363. #endif
  7364. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  7365. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  7366. ret = EVP_test(EVP_aes_128_xts(), k2, i2, p2, sizeof(p2), c2, sizeof(c2));
  7367. if (ret != 0) {
  7368. printf("EVP_aes_128_xts failed!\n");
  7369. goto out;
  7370. }
  7371. #endif
  7372. XMEMSET(buf, 0, sizeof(buf));
  7373. if (wc_AesXtsSetKey(aes, k2, sizeof(k2), AES_ENCRYPTION,
  7374. HEAP_HINT, devId) != 0)
  7375. ERROR_OUT(-5400, out);
  7376. else
  7377. aes_inited = 1;
  7378. ret = wc_AesXtsEncrypt(aes, buf, p2, sizeof(p2), i2, sizeof(i2));
  7379. #if defined(WOLFSSL_ASYNC_CRYPT)
  7380. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7381. #endif
  7382. if (ret != 0)
  7383. ERROR_OUT(-5401, out);
  7384. if (XMEMCMP(c2, buf, sizeof(c2)))
  7385. ERROR_OUT(-5402, out);
  7386. XMEMSET(buf, 0, sizeof(buf));
  7387. wc_AesXtsFree(aes);
  7388. if (wc_AesXtsSetKey(aes, k1, sizeof(k1), AES_ENCRYPTION,
  7389. HEAP_HINT, devId) != 0)
  7390. ERROR_OUT(-5403, out);
  7391. ret = wc_AesXtsEncrypt(aes, buf, p1, sizeof(p1), i1, sizeof(i1));
  7392. #if defined(WOLFSSL_ASYNC_CRYPT)
  7393. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7394. #endif
  7395. if (ret != 0)
  7396. ERROR_OUT(-5404, out);
  7397. if (XMEMCMP(c1, buf, AES_BLOCK_SIZE))
  7398. ERROR_OUT(-5405, out);
  7399. /* partial block encryption test */
  7400. XMEMSET(cipher, 0, sizeof(cipher));
  7401. ret = wc_AesXtsEncrypt(aes, cipher, pp, sizeof(pp), i1, sizeof(i1));
  7402. #if defined(WOLFSSL_ASYNC_CRYPT)
  7403. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7404. #endif
  7405. if (ret != 0)
  7406. ERROR_OUT(-5406, out);
  7407. wc_AesXtsFree(aes);
  7408. /* partial block decrypt test */
  7409. XMEMSET(buf, 0, sizeof(buf));
  7410. if (wc_AesXtsSetKey(aes, k1, sizeof(k1), AES_DECRYPTION,
  7411. HEAP_HINT, devId) != 0)
  7412. ERROR_OUT(-5407, out);
  7413. ret = wc_AesXtsDecrypt(aes, buf, cipher, sizeof(pp), i1, sizeof(i1));
  7414. #if defined(WOLFSSL_ASYNC_CRYPT)
  7415. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7416. #endif
  7417. if (ret != 0)
  7418. ERROR_OUT(-5408, out);
  7419. if (XMEMCMP(pp, buf, sizeof(pp)))
  7420. ERROR_OUT(-5409, out);
  7421. /* NIST decrypt test vector */
  7422. XMEMSET(buf, 0, sizeof(buf));
  7423. ret = wc_AesXtsDecrypt(aes, buf, c1, sizeof(c1), i1, sizeof(i1));
  7424. #if defined(WOLFSSL_ASYNC_CRYPT)
  7425. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7426. #endif
  7427. if (ret != 0)
  7428. ERROR_OUT(-5410, out);
  7429. if (XMEMCMP(p1, buf, AES_BLOCK_SIZE))
  7430. ERROR_OUT(-5411, out);
  7431. /* fail case with decrypting using wrong key */
  7432. XMEMSET(buf, 0, sizeof(buf));
  7433. ret = wc_AesXtsDecrypt(aes, buf, c2, sizeof(c2), i2, sizeof(i2));
  7434. #if defined(WOLFSSL_ASYNC_CRYPT)
  7435. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7436. #endif
  7437. if (ret != 0)
  7438. ERROR_OUT(-5412, out);
  7439. if (XMEMCMP(p2, buf, sizeof(p2)) == 0) /* fail case with wrong key */
  7440. ERROR_OUT(-5413, out);
  7441. wc_AesXtsFree(aes);
  7442. /* set correct key and retest */
  7443. XMEMSET(buf, 0, sizeof(buf));
  7444. if (wc_AesXtsSetKey(aes, k2, sizeof(k2), AES_DECRYPTION,
  7445. HEAP_HINT, devId) != 0)
  7446. ERROR_OUT(-5414, out);
  7447. ret = wc_AesXtsDecrypt(aes, buf, c2, sizeof(c2), i2, sizeof(i2));
  7448. #if defined(WOLFSSL_ASYNC_CRYPT)
  7449. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7450. #endif
  7451. if (ret != 0)
  7452. ERROR_OUT(-5415, out);
  7453. if (XMEMCMP(p2, buf, sizeof(p2)))
  7454. ERROR_OUT(-5416, out);
  7455. out:
  7456. if (aes_inited)
  7457. wc_AesXtsFree(aes);
  7458. #ifdef WOLFSSL_SMALL_STACK
  7459. if (aes)
  7460. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_AES);
  7461. #endif
  7462. return ret;
  7463. }
  7464. #endif /* WOLFSSL_AES_128 */
  7465. #ifdef WOLFSSL_AES_256
  7466. static int aes_xts_256_test(void)
  7467. {
  7468. #ifdef WOLFSSL_SMALL_STACK
  7469. XtsAes *aes = NULL;
  7470. #else
  7471. XtsAes aes[1];
  7472. #endif
  7473. int aes_inited = 0;
  7474. int ret = 0;
  7475. unsigned char buf[AES_BLOCK_SIZE * 3];
  7476. unsigned char cipher[AES_BLOCK_SIZE * 3];
  7477. /* 256 key tests */
  7478. WOLFSSL_SMALL_STACK_STATIC unsigned char k1[] = {
  7479. 0x1e, 0xa6, 0x61, 0xc5, 0x8d, 0x94, 0x3a, 0x0e,
  7480. 0x48, 0x01, 0xe4, 0x2f, 0x4b, 0x09, 0x47, 0x14,
  7481. 0x9e, 0x7f, 0x9f, 0x8e, 0x3e, 0x68, 0xd0, 0xc7,
  7482. 0x50, 0x52, 0x10, 0xbd, 0x31, 0x1a, 0x0e, 0x7c,
  7483. 0xd6, 0xe1, 0x3f, 0xfd, 0xf2, 0x41, 0x8d, 0x8d,
  7484. 0x19, 0x11, 0xc0, 0x04, 0xcd, 0xa5, 0x8d, 0xa3,
  7485. 0xd6, 0x19, 0xb7, 0xe2, 0xb9, 0x14, 0x1e, 0x58,
  7486. 0x31, 0x8e, 0xea, 0x39, 0x2c, 0xf4, 0x1b, 0x08
  7487. };
  7488. WOLFSSL_SMALL_STACK_STATIC unsigned char i1[] = {
  7489. 0xad, 0xf8, 0xd9, 0x26, 0x27, 0x46, 0x4a, 0xd2,
  7490. 0xf0, 0x42, 0x8e, 0x84, 0xa9, 0xf8, 0x75, 0x64
  7491. };
  7492. WOLFSSL_SMALL_STACK_STATIC unsigned char p1[] = {
  7493. 0x2e, 0xed, 0xea, 0x52, 0xcd, 0x82, 0x15, 0xe1,
  7494. 0xac, 0xc6, 0x47, 0xe8, 0x10, 0xbb, 0xc3, 0x64,
  7495. 0x2e, 0x87, 0x28, 0x7f, 0x8d, 0x2e, 0x57, 0xe3,
  7496. 0x6c, 0x0a, 0x24, 0xfb, 0xc1, 0x2a, 0x20, 0x2e
  7497. };
  7498. /* plain text test of partial block is not from NIST test vector list */
  7499. WOLFSSL_SMALL_STACK_STATIC unsigned char pp[] = {
  7500. 0xeb, 0xab, 0xce, 0x95, 0xb1, 0x4d, 0x3c, 0x8d,
  7501. 0x6f, 0xb3, 0x50, 0x39, 0x07, 0x90, 0x31, 0x1c,
  7502. 0x6e, 0x4b, 0x92, 0x01, 0x3e, 0x76, 0x8a, 0xd5
  7503. };
  7504. WOLFSSL_SMALL_STACK_STATIC unsigned char c1[] = {
  7505. 0xcb, 0xaa, 0xd0, 0xe2, 0xf6, 0xce, 0xa3, 0xf5,
  7506. 0x0b, 0x37, 0xf9, 0x34, 0xd4, 0x6a, 0x9b, 0x13,
  7507. 0x0b, 0x9d, 0x54, 0xf0, 0x7e, 0x34, 0xf3, 0x6a,
  7508. 0xf7, 0x93, 0xe8, 0x6f, 0x73, 0xc6, 0xd7, 0xdb
  7509. };
  7510. WOLFSSL_SMALL_STACK_STATIC unsigned char k2[] = {
  7511. 0xad, 0x50, 0x4b, 0x85, 0xd7, 0x51, 0xbf, 0xba,
  7512. 0x69, 0x13, 0xb4, 0xcc, 0x79, 0xb6, 0x5a, 0x62,
  7513. 0xf7, 0xf3, 0x9d, 0x36, 0x0f, 0x35, 0xb5, 0xec,
  7514. 0x4a, 0x7e, 0x95, 0xbd, 0x9b, 0xa5, 0xf2, 0xec,
  7515. 0xc1, 0xd7, 0x7e, 0xa3, 0xc3, 0x74, 0xbd, 0x4b,
  7516. 0x13, 0x1b, 0x07, 0x83, 0x87, 0xdd, 0x55, 0x5a,
  7517. 0xb5, 0xb0, 0xc7, 0xe5, 0x2d, 0xb5, 0x06, 0x12,
  7518. 0xd2, 0xb5, 0x3a, 0xcb, 0x47, 0x8a, 0x53, 0xb4
  7519. };
  7520. WOLFSSL_SMALL_STACK_STATIC unsigned char i2[] = {
  7521. 0xe6, 0x42, 0x19, 0xed, 0xe0, 0xe1, 0xc2, 0xa0,
  7522. 0x0e, 0xf5, 0x58, 0x6a, 0xc4, 0x9b, 0xeb, 0x6f
  7523. };
  7524. WOLFSSL_SMALL_STACK_STATIC unsigned char p2[] = {
  7525. 0x24, 0xcb, 0x76, 0x22, 0x55, 0xb5, 0xa8, 0x00,
  7526. 0xf4, 0x6e, 0x80, 0x60, 0x56, 0x9e, 0x05, 0x53,
  7527. 0xbc, 0xfe, 0x86, 0x55, 0x3b, 0xca, 0xd5, 0x89,
  7528. 0xc7, 0x54, 0x1a, 0x73, 0xac, 0xc3, 0x9a, 0xbd,
  7529. 0x53, 0xc4, 0x07, 0x76, 0xd8, 0xe8, 0x22, 0x61,
  7530. 0x9e, 0xa9, 0xad, 0x77, 0xa0, 0x13, 0x4c, 0xfc
  7531. };
  7532. WOLFSSL_SMALL_STACK_STATIC unsigned char c2[] = {
  7533. 0xa3, 0xc6, 0xf3, 0xf3, 0x82, 0x79, 0x5b, 0x10,
  7534. 0x87, 0xd7, 0x02, 0x50, 0xdb, 0x2c, 0xd3, 0xb1,
  7535. 0xa1, 0x62, 0xa8, 0xb6, 0xdc, 0x12, 0x60, 0x61,
  7536. 0xc1, 0x0a, 0x84, 0xa5, 0x85, 0x3f, 0x3a, 0x89,
  7537. 0xe6, 0x6c, 0xdb, 0xb7, 0x9a, 0xb4, 0x28, 0x9b,
  7538. 0xc3, 0xea, 0xd8, 0x10, 0xe9, 0xc0, 0xaf, 0x92
  7539. };
  7540. #ifdef WOLFSSL_SMALL_STACK
  7541. if ((aes = (XtsAes *)XMALLOC(sizeof *aes, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  7542. ERROR_OUT(-5515, out);
  7543. #endif
  7544. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  7545. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  7546. ret = EVP_test(EVP_aes_256_xts(), k2, i2, p2, sizeof(p2), c2, sizeof(c2));
  7547. if (ret != 0) {
  7548. printf("EVP_aes_256_xts failed\n");
  7549. goto out;
  7550. }
  7551. #endif
  7552. XMEMSET(buf, 0, sizeof(buf));
  7553. if (wc_AesXtsSetKey(aes, k2, sizeof(k2), AES_ENCRYPTION,
  7554. HEAP_HINT, devId) != 0)
  7555. ERROR_OUT(-5500, out);
  7556. else
  7557. aes_inited = 1;
  7558. ret = wc_AesXtsEncrypt(aes, buf, p2, sizeof(p2), i2, sizeof(i2));
  7559. #if defined(WOLFSSL_ASYNC_CRYPT)
  7560. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7561. #endif
  7562. if (ret != 0)
  7563. ERROR_OUT(-5501, out);
  7564. if (XMEMCMP(c2, buf, sizeof(c2)))
  7565. ERROR_OUT(-5502, out);
  7566. wc_AesXtsFree(aes);
  7567. XMEMSET(buf, 0, sizeof(buf));
  7568. if (wc_AesXtsSetKey(aes, k1, sizeof(k1), AES_ENCRYPTION,
  7569. HEAP_HINT, devId) != 0)
  7570. ERROR_OUT(-5503, out);
  7571. ret = wc_AesXtsEncrypt(aes, buf, p1, sizeof(p1), i1, sizeof(i1));
  7572. #if defined(WOLFSSL_ASYNC_CRYPT)
  7573. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7574. #endif
  7575. if (ret != 0)
  7576. ERROR_OUT(-5504, out);
  7577. if (XMEMCMP(c1, buf, AES_BLOCK_SIZE))
  7578. ERROR_OUT(-5505, out);
  7579. /* partial block encryption test */
  7580. XMEMSET(cipher, 0, sizeof(cipher));
  7581. ret = wc_AesXtsEncrypt(aes, cipher, pp, sizeof(pp), i1, sizeof(i1));
  7582. #if defined(WOLFSSL_ASYNC_CRYPT)
  7583. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7584. #endif
  7585. if (ret != 0)
  7586. ERROR_OUT(-5506, out);
  7587. wc_AesXtsFree(aes);
  7588. /* partial block decrypt test */
  7589. XMEMSET(buf, 0, sizeof(buf));
  7590. if (wc_AesXtsSetKey(aes, k1, sizeof(k1), AES_DECRYPTION,
  7591. HEAP_HINT, devId) != 0)
  7592. ERROR_OUT(-5507, out);
  7593. ret = wc_AesXtsDecrypt(aes, buf, cipher, sizeof(pp), i1, sizeof(i1));
  7594. #if defined(WOLFSSL_ASYNC_CRYPT)
  7595. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7596. #endif
  7597. if (ret != 0)
  7598. ERROR_OUT(-5508, out);
  7599. if (XMEMCMP(pp, buf, sizeof(pp)))
  7600. ERROR_OUT(-5509, out);
  7601. /* NIST decrypt test vector */
  7602. XMEMSET(buf, 0, sizeof(buf));
  7603. ret = wc_AesXtsDecrypt(aes, buf, c1, sizeof(c1), i1, sizeof(i1));
  7604. #if defined(WOLFSSL_ASYNC_CRYPT)
  7605. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7606. #endif
  7607. if (ret != 0)
  7608. ERROR_OUT(-5510, out);
  7609. if (XMEMCMP(p1, buf, AES_BLOCK_SIZE))
  7610. ERROR_OUT(-5511, out);
  7611. wc_AesXtsFree(aes);
  7612. XMEMSET(buf, 0, sizeof(buf));
  7613. if (wc_AesXtsSetKey(aes, k2, sizeof(k2), AES_DECRYPTION,
  7614. HEAP_HINT, devId) != 0)
  7615. ERROR_OUT(-5512, out);
  7616. ret = wc_AesXtsDecrypt(aes, buf, c2, sizeof(c2), i2, sizeof(i2));
  7617. #if defined(WOLFSSL_ASYNC_CRYPT)
  7618. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7619. #endif
  7620. if (ret != 0)
  7621. ERROR_OUT(-5513, out);
  7622. if (XMEMCMP(p2, buf, sizeof(p2)))
  7623. ERROR_OUT(-5514, out);
  7624. out:
  7625. if (aes_inited)
  7626. wc_AesXtsFree(aes);
  7627. #ifdef WOLFSSL_SMALL_STACK
  7628. if (aes)
  7629. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_AES);
  7630. #endif
  7631. return ret;
  7632. }
  7633. #endif /* WOLFSSL_AES_256 */
  7634. #if defined(WOLFSSL_AES_128) && defined(WOLFSSL_AES_256)
  7635. /* both 128 and 256 bit key test */
  7636. static int aes_xts_sector_test(void)
  7637. {
  7638. #ifdef WOLFSSL_SMALL_STACK
  7639. XtsAes *aes = NULL;
  7640. #else
  7641. XtsAes aes[1];
  7642. #endif
  7643. int aes_inited = 0;
  7644. int ret = 0;
  7645. unsigned char buf[AES_BLOCK_SIZE * 2];
  7646. /* 128 key tests */
  7647. WOLFSSL_SMALL_STACK_STATIC unsigned char k1[] = {
  7648. 0xa3, 0xe4, 0x0d, 0x5b, 0xd4, 0xb6, 0xbb, 0xed,
  7649. 0xb2, 0xd1, 0x8c, 0x70, 0x0a, 0xd2, 0xdb, 0x22,
  7650. 0x10, 0xc8, 0x11, 0x90, 0x64, 0x6d, 0x67, 0x3c,
  7651. 0xbc, 0xa5, 0x3f, 0x13, 0x3e, 0xab, 0x37, 0x3c
  7652. };
  7653. WOLFSSL_SMALL_STACK_STATIC unsigned char p1[] = {
  7654. 0x20, 0xe0, 0x71, 0x94, 0x05, 0x99, 0x3f, 0x09,
  7655. 0xa6, 0x6a, 0xe5, 0xbb, 0x50, 0x0e, 0x56, 0x2c
  7656. };
  7657. WOLFSSL_SMALL_STACK_STATIC unsigned char c1[] = {
  7658. 0x74, 0x62, 0x35, 0x51, 0x21, 0x02, 0x16, 0xac,
  7659. 0x92, 0x6b, 0x96, 0x50, 0xb6, 0xd3, 0xfa, 0x52
  7660. };
  7661. word64 s1 = 141;
  7662. /* 256 key tests */
  7663. WOLFSSL_SMALL_STACK_STATIC unsigned char k2[] = {
  7664. 0xef, 0x01, 0x0c, 0xa1, 0xa3, 0x66, 0x3e, 0x32,
  7665. 0x53, 0x43, 0x49, 0xbc, 0x0b, 0xae, 0x62, 0x23,
  7666. 0x2a, 0x15, 0x73, 0x34, 0x85, 0x68, 0xfb, 0x9e,
  7667. 0xf4, 0x17, 0x68, 0xa7, 0x67, 0x4f, 0x50, 0x7a,
  7668. 0x72, 0x7f, 0x98, 0x75, 0x53, 0x97, 0xd0, 0xe0,
  7669. 0xaa, 0x32, 0xf8, 0x30, 0x33, 0x8c, 0xc7, 0xa9,
  7670. 0x26, 0xc7, 0x73, 0xf0, 0x9e, 0x57, 0xb3, 0x57,
  7671. 0xcd, 0x15, 0x6a, 0xfb, 0xca, 0x46, 0xe1, 0xa0
  7672. };
  7673. WOLFSSL_SMALL_STACK_STATIC unsigned char p2[] = {
  7674. 0xed, 0x98, 0xe0, 0x17, 0x70, 0xa8, 0x53, 0xb4,
  7675. 0x9d, 0xb9, 0xe6, 0xaa, 0xf8, 0x8f, 0x0a, 0x41,
  7676. 0xb9, 0xb5, 0x6e, 0x91, 0xa5, 0xa2, 0xb1, 0x1d,
  7677. 0x40, 0x52, 0x92, 0x54, 0xf5, 0x52, 0x3e, 0x75
  7678. };
  7679. WOLFSSL_SMALL_STACK_STATIC unsigned char c2[] = {
  7680. 0xca, 0x20, 0xc5, 0x5e, 0x8d, 0xc1, 0x49, 0x68,
  7681. 0x7d, 0x25, 0x41, 0xde, 0x39, 0xc3, 0xdf, 0x63,
  7682. 0x00, 0xbb, 0x5a, 0x16, 0x3c, 0x10, 0xce, 0xd3,
  7683. 0x66, 0x6b, 0x13, 0x57, 0xdb, 0x8b, 0xd3, 0x9d
  7684. };
  7685. word64 s2 = 187;
  7686. #ifdef WOLFSSL_SMALL_STACK
  7687. if ((aes = (XtsAes *)XMALLOC(sizeof *aes, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  7688. ERROR_OUT(-5612, out);
  7689. #endif
  7690. XMEMSET(buf, 0, sizeof(buf));
  7691. if (wc_AesXtsSetKey(aes, k1, sizeof(k1), AES_ENCRYPTION,
  7692. HEAP_HINT, devId) != 0)
  7693. ERROR_OUT(-5600, out);
  7694. else
  7695. aes_inited = 1;
  7696. ret = wc_AesXtsEncryptSector(aes, buf, p1, sizeof(p1), s1);
  7697. #if defined(WOLFSSL_ASYNC_CRYPT)
  7698. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7699. #endif
  7700. if (ret != 0)
  7701. ERROR_OUT(-5601, out);
  7702. if (XMEMCMP(c1, buf, AES_BLOCK_SIZE))
  7703. ERROR_OUT(-5602, out);
  7704. wc_AesXtsFree(aes);
  7705. /* decrypt test */
  7706. XMEMSET(buf, 0, sizeof(buf));
  7707. if (wc_AesXtsSetKey(aes, k1, sizeof(k1), AES_DECRYPTION,
  7708. HEAP_HINT, devId) != 0)
  7709. ERROR_OUT(-5603, out);
  7710. ret = wc_AesXtsDecryptSector(aes, buf, c1, sizeof(c1), s1);
  7711. #if defined(WOLFSSL_ASYNC_CRYPT)
  7712. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7713. #endif
  7714. if (ret != 0)
  7715. ERROR_OUT(-5604, out);
  7716. if (XMEMCMP(p1, buf, AES_BLOCK_SIZE))
  7717. ERROR_OUT(-5605, out);
  7718. wc_AesXtsFree(aes);
  7719. /* 256 bit key tests */
  7720. XMEMSET(buf, 0, sizeof(buf));
  7721. if (wc_AesXtsSetKey(aes, k2, sizeof(k2), AES_ENCRYPTION,
  7722. HEAP_HINT, devId) != 0)
  7723. ERROR_OUT(-5606, out);
  7724. ret = wc_AesXtsEncryptSector(aes, buf, p2, sizeof(p2), s2);
  7725. #if defined(WOLFSSL_ASYNC_CRYPT)
  7726. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7727. #endif
  7728. if (ret != 0)
  7729. ERROR_OUT(-5607, out);
  7730. if (XMEMCMP(c2, buf, sizeof(c2)))
  7731. ERROR_OUT(-5608, out);
  7732. wc_AesXtsFree(aes);
  7733. /* decrypt test */
  7734. XMEMSET(buf, 0, sizeof(buf));
  7735. if (wc_AesXtsSetKey(aes, k2, sizeof(k2), AES_DECRYPTION,
  7736. HEAP_HINT, devId) != 0)
  7737. ERROR_OUT(-5609, out);
  7738. ret = wc_AesXtsDecryptSector(aes, buf, c2, sizeof(c2), s2);
  7739. #if defined(WOLFSSL_ASYNC_CRYPT)
  7740. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7741. #endif
  7742. if (ret != 0)
  7743. ERROR_OUT(-5610, out);
  7744. if (XMEMCMP(p2, buf, sizeof(p2)))
  7745. ERROR_OUT(-5611, out);
  7746. out:
  7747. if (aes_inited)
  7748. wc_AesXtsFree(aes);
  7749. #ifdef WOLFSSL_SMALL_STACK
  7750. if (aes)
  7751. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_AES);
  7752. #endif
  7753. return ret;
  7754. }
  7755. #endif /* WOLFSSL_AES_128 && WOLFSSL_AES_256 */
  7756. #ifdef WOLFSSL_AES_128
  7757. /* testing of bad arguments */
  7758. static int aes_xts_args_test(void)
  7759. {
  7760. #ifdef WOLFSSL_SMALL_STACK
  7761. XtsAes *aes = NULL;
  7762. #else
  7763. XtsAes aes[1];
  7764. #endif
  7765. int aes_inited = 0;
  7766. int ret;
  7767. unsigned char buf[AES_BLOCK_SIZE * 2];
  7768. /* 128 key tests */
  7769. WOLFSSL_SMALL_STACK_STATIC unsigned char k1[] = {
  7770. 0xa3, 0xe4, 0x0d, 0x5b, 0xd4, 0xb6, 0xbb, 0xed,
  7771. 0xb2, 0xd1, 0x8c, 0x70, 0x0a, 0xd2, 0xdb, 0x22,
  7772. 0x10, 0xc8, 0x11, 0x90, 0x64, 0x6d, 0x67, 0x3c,
  7773. 0xbc, 0xa5, 0x3f, 0x13, 0x3e, 0xab, 0x37, 0x3c
  7774. };
  7775. WOLFSSL_SMALL_STACK_STATIC unsigned char p1[] = {
  7776. 0x20, 0xe0, 0x71, 0x94, 0x05, 0x99, 0x3f, 0x09,
  7777. 0xa6, 0x6a, 0xe5, 0xbb, 0x50, 0x0e, 0x56, 0x2c
  7778. };
  7779. WOLFSSL_SMALL_STACK_STATIC unsigned char c1[] = {
  7780. 0x74, 0x62, 0x35, 0x51, 0x21, 0x02, 0x16, 0xac,
  7781. 0x92, 0x6b, 0x96, 0x50, 0xb6, 0xd3, 0xfa, 0x52
  7782. };
  7783. word64 s1 = 141;
  7784. #ifdef WOLFSSL_SMALL_STACK
  7785. if ((aes = (XtsAes *)XMALLOC(sizeof *aes, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  7786. ERROR_OUT(-5708, out);
  7787. #endif
  7788. if (wc_AesXtsSetKey(NULL, k1, sizeof(k1), AES_ENCRYPTION,
  7789. HEAP_HINT, devId) == 0)
  7790. ERROR_OUT(-5700, out);
  7791. if (wc_AesXtsSetKey(aes, NULL, sizeof(k1), AES_ENCRYPTION,
  7792. HEAP_HINT, devId) == 0)
  7793. ERROR_OUT(-5701, out);
  7794. /* encryption operations */
  7795. if (wc_AesXtsSetKey(aes, k1, sizeof(k1), AES_ENCRYPTION,
  7796. HEAP_HINT, devId) != 0)
  7797. ERROR_OUT(-5702, out);
  7798. else
  7799. aes_inited = 1;
  7800. ret = wc_AesXtsEncryptSector(NULL, buf, p1, sizeof(p1), s1);
  7801. #if defined(WOLFSSL_ASYNC_CRYPT)
  7802. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7803. #endif
  7804. if (ret == 0)
  7805. ERROR_OUT(-5703, out);
  7806. ret = wc_AesXtsEncryptSector(aes, NULL, p1, sizeof(p1), s1);
  7807. #if defined(WOLFSSL_ASYNC_CRYPT)
  7808. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7809. #endif
  7810. if (ret == 0)
  7811. ERROR_OUT(-5704, out);
  7812. wc_AesXtsFree(aes);
  7813. /* decryption operations */
  7814. if (wc_AesXtsSetKey(aes, k1, sizeof(k1), AES_DECRYPTION,
  7815. HEAP_HINT, devId) != 0)
  7816. ERROR_OUT(-5705, out);
  7817. ret = wc_AesXtsDecryptSector(NULL, buf, c1, sizeof(c1), s1);
  7818. #if defined(WOLFSSL_ASYNC_CRYPT)
  7819. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7820. #endif
  7821. if (ret == 0)
  7822. ERROR_OUT(-5706, out);
  7823. ret = wc_AesXtsDecryptSector(aes, NULL, c1, sizeof(c1), s1);
  7824. #if defined(WOLFSSL_ASYNC_CRYPT)
  7825. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7826. #endif
  7827. if (ret == 0)
  7828. ERROR_OUT(-5707, out);
  7829. ret = 0;
  7830. out:
  7831. if (aes_inited)
  7832. wc_AesXtsFree(aes);
  7833. #ifdef WOLFSSL_SMALL_STACK
  7834. if (aes)
  7835. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_AES);
  7836. #endif
  7837. return ret;
  7838. }
  7839. #endif /* WOLFSSL_AES_128 */
  7840. #endif /* WOLFSSL_AES_XTS */
  7841. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  7842. static int aes_cbc_test(void)
  7843. {
  7844. byte cipher[AES_BLOCK_SIZE];
  7845. byte plain[AES_BLOCK_SIZE];
  7846. int ret;
  7847. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = { /* "Now is the time for all " w/o trailing 0 */
  7848. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  7849. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  7850. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  7851. };
  7852. byte key[] = "0123456789abcdef "; /* align */
  7853. byte iv[] = "1234567890abcdef "; /* align */
  7854. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  7855. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  7856. /* Parameter Validation testing. */
  7857. ret = wc_AesCbcEncryptWithKey(cipher, msg, AES_BLOCK_SIZE, key, 17, NULL);
  7858. if (ret != BAD_FUNC_ARG)
  7859. return -5800;
  7860. #ifdef HAVE_AES_DECRYPT
  7861. ret = wc_AesCbcDecryptWithKey(plain, cipher, AES_BLOCK_SIZE, key, 17, NULL);
  7862. if (ret != BAD_FUNC_ARG)
  7863. return -5801;
  7864. #endif
  7865. ret = wc_AesCbcEncryptWithKey(cipher, msg, AES_BLOCK_SIZE, key,
  7866. AES_BLOCK_SIZE, iv);
  7867. if (ret != 0)
  7868. return -5802;
  7869. #ifdef HAVE_AES_DECRYPT
  7870. ret = wc_AesCbcDecryptWithKey(plain, cipher, AES_BLOCK_SIZE, key,
  7871. AES_BLOCK_SIZE, iv);
  7872. if (ret != 0)
  7873. return -5803;
  7874. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE) != 0)
  7875. return -5804;
  7876. #endif /* HAVE_AES_DECRYPT */
  7877. (void)plain;
  7878. return 0;
  7879. }
  7880. #endif
  7881. #if defined(HAVE_AES_ECB) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  7882. static int aesecb_test(void)
  7883. {
  7884. #ifdef WOLFSSL_SMALL_STACK
  7885. Aes *enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  7886. #else
  7887. Aes enc[1];
  7888. #endif
  7889. byte cipher[AES_BLOCK_SIZE * 4];
  7890. #ifdef HAVE_AES_DECRYPT
  7891. #ifdef WOLFSSL_SMALL_STACK
  7892. Aes *dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  7893. #else
  7894. Aes dec[1];
  7895. #endif
  7896. byte plain [AES_BLOCK_SIZE * 4];
  7897. #endif /* HAVE_AES_DECRYPT */
  7898. int ret = 0;
  7899. #if defined(WOLFSSL_AES_256)
  7900. {
  7901. WOLFSSL_SMALL_STACK_STATIC const byte niPlain[] =
  7902. {
  7903. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  7904. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  7905. };
  7906. WOLFSSL_SMALL_STACK_STATIC const byte niCipher[] =
  7907. {
  7908. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  7909. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  7910. };
  7911. WOLFSSL_SMALL_STACK_STATIC const byte niKey[] =
  7912. {
  7913. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  7914. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  7915. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  7916. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  7917. };
  7918. if (wc_AesInit(enc, HEAP_HINT, devId) != 0)
  7919. ERROR_OUT(-5900, out);
  7920. #if defined(HAVE_AES_DECRYPT)
  7921. if (wc_AesInit(dec, HEAP_HINT, devId) != 0)
  7922. ERROR_OUT(-5901, out);
  7923. #endif
  7924. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  7925. ret = wc_AesSetKey(enc, niKey, sizeof(niKey), cipher, AES_ENCRYPTION);
  7926. if (ret != 0)
  7927. ERROR_OUT(-5923, out);
  7928. if (wc_AesEcbEncrypt(enc, cipher, niPlain, AES_BLOCK_SIZE) != 0)
  7929. ERROR_OUT(-5924, out);
  7930. if (XMEMCMP(cipher, niCipher, AES_BLOCK_SIZE) != 0)
  7931. ERROR_OUT(-5925, out);
  7932. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  7933. ret = wc_AesSetKey(dec, niKey, sizeof(niKey), plain, AES_DECRYPTION);
  7934. if (ret != 0)
  7935. ERROR_OUT(-5926, out);
  7936. if (wc_AesEcbDecrypt(dec, plain, niCipher, AES_BLOCK_SIZE) != 0)
  7937. ERROR_OUT(-5927, out);
  7938. wc_AesEcbDecrypt(dec, plain, niCipher, AES_BLOCK_SIZE);
  7939. if (XMEMCMP(plain, niPlain, AES_BLOCK_SIZE) != 0)
  7940. ERROR_OUT(-5928, out);
  7941. }
  7942. wc_AesFree(enc);
  7943. #ifdef HAVE_AES_DECRYPT
  7944. wc_AesFree(dec);
  7945. #endif
  7946. out:
  7947. #ifdef WOLFSSL_SMALL_STACK
  7948. if (enc)
  7949. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  7950. #ifdef HAVE_AES_DECRYPT
  7951. if (dec)
  7952. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  7953. #endif
  7954. #endif
  7955. #endif /* WOLFSSL_AES_256 */
  7956. return ret;
  7957. }
  7958. #endif /* HAVE_AES_ECB */
  7959. #ifdef WOLFSSL_AES_COUNTER
  7960. static int aesctr_test(Aes* enc, Aes* dec, byte* cipher, byte* plain)
  7961. {
  7962. int ret;
  7963. /* test vectors from "Recommendation for Block Cipher Modes of
  7964. * Operation" NIST Special Publication 800-38A */
  7965. WOLFSSL_SMALL_STACK_STATIC const byte ctrIv[] =
  7966. {
  7967. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  7968. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  7969. };
  7970. WOLFSSL_SMALL_STACK_STATIC const byte ctrPlain[] =
  7971. {
  7972. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  7973. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  7974. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  7975. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  7976. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  7977. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  7978. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  7979. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  7980. };
  7981. #ifdef WOLFSSL_ARMASM
  7982. WOLFSSL_SMALL_STACK_STATIC const byte ctrIvWrap32[] =
  7983. {
  7984. 0xff,0xff,0xff,0xff,0x0f,0xff,0xff,0xff,
  7985. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff
  7986. };
  7987. WOLFSSL_SMALL_STACK_STATIC const byte ctrIvWrap32_2[] =
  7988. {
  7989. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  7990. 0xff,0xff,0xff,0xff,0x0f,0xff,0xff,0xfe
  7991. };
  7992. WOLFSSL_SMALL_STACK_STATIC const byte ctrIvWrap64[] =
  7993. {
  7994. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  7995. 0x0f,0xff,0xff,0xff,0xff,0xff,0xff,0xff
  7996. };
  7997. WOLFSSL_SMALL_STACK_STATIC const byte ctrIvWrap64_2[] =
  7998. {
  7999. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xf0,
  8000. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfe
  8001. };
  8002. WOLFSSL_SMALL_STACK_STATIC const byte ctrIvWrap96[] =
  8003. {
  8004. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  8005. 0xff,0xff,0xff,0xff,0x0f,0xff,0xff,0xff
  8006. };
  8007. WOLFSSL_SMALL_STACK_STATIC const byte ctrIvWrap96_2[] =
  8008. {
  8009. 0xff,0xff,0xff,0xf0,0xff,0xff,0xff,0xff,
  8010. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfe
  8011. };
  8012. #endif
  8013. WOLFSSL_SMALL_STACK_STATIC const byte ctrIvWrap128[] =
  8014. {
  8015. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  8016. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff
  8017. };
  8018. #ifdef WOLFSSL_ARMASM
  8019. WOLFSSL_SMALL_STACK_STATIC const byte ctrIvWrap128_2[] =
  8020. {
  8021. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  8022. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfe
  8023. };
  8024. #endif
  8025. #ifdef WOLFSSL_AES_128
  8026. WOLFSSL_SMALL_STACK_STATIC const byte oddCipher[] =
  8027. {
  8028. 0xb9,0xd7,0xcb,0x08,0xb0,0xe1,0x7b,0xa0,
  8029. 0xc2
  8030. };
  8031. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Key[] =
  8032. {
  8033. 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,
  8034. 0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c
  8035. };
  8036. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Cipher[] =
  8037. {
  8038. 0x87,0x4d,0x61,0x91,0xb6,0x20,0xe3,0x26,
  8039. 0x1b,0xef,0x68,0x64,0x99,0x0d,0xb6,0xce,
  8040. 0x98,0x06,0xf6,0x6b,0x79,0x70,0xfd,0xff,
  8041. 0x86,0x17,0x18,0x7b,0xb9,0xff,0xfd,0xff,
  8042. 0x5a,0xe4,0xdf,0x3e,0xdb,0xd5,0xd3,0x5e,
  8043. 0x5b,0x4f,0x09,0x02,0x0d,0xb0,0x3e,0xab,
  8044. 0x1e,0x03,0x1d,0xda,0x2f,0xbe,0x03,0xd1,
  8045. 0x79,0x21,0x70,0xa0,0xf3,0x00,0x9c,0xee
  8046. };
  8047. #ifdef WOLFSSL_ARMASM
  8048. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap32Cipher[] =
  8049. {
  8050. 0xb3,0x8b,0x58,0xbc,0xce,0xf4,0x71,0x78,
  8051. 0xf6,0x7c,0xdb,0xb4,0x27,0x2b,0x0a,0xbf,
  8052. 0x7e,0xad,0xea,0x5c,0xd1
  8053. };
  8054. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap32CipherLong[] =
  8055. {
  8056. 0xb3,0x8b,0x58,0xbc,0xce,0xf4,0x71,0x78,
  8057. 0xf6,0x7c,0xdb,0xb4,0x27,0x2b,0x0a,0xbf,
  8058. 0x7e,0xad,0xea,0x5c,0xd1,0xb7,0x98,0xf0,
  8059. 0x22,0x20,0xfe,0x67,0xb0,0x02,0x23,0x50
  8060. };
  8061. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap32_2CipherLong[] =
  8062. {
  8063. 0x6e,0xa1,0x27,0x4d,0xea,0x20,0x5f,0x39,
  8064. 0x68,0xc8,0xb6,0x78,0xde,0xfc,0x53,0x5c,
  8065. 0x90,0xc8,0xf6,0xc6,0xfa,0xe0,0x7b,0x09,
  8066. 0x7c,0xf8,0x9c,0x6a,0x5a,0xa5,0x17,0x7f,
  8067. 0x03,0x92,0x5f,0x4e,0x85,0xea,0x26,0xc9,
  8068. 0x5a,0xc2,0x74,0xe2,0xbf,0xe4,0x1b,0xd4
  8069. };
  8070. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap64Cipher[] =
  8071. {
  8072. 0xdd,0x17,0x10,0x7c,0x45,0x04,0xac,0x43,
  8073. 0xef,0xa8,0xcc,0x32,0x34,0x87,0x88,0xd7,
  8074. 0xae,0x74,0x94,0x72,0x8e
  8075. };
  8076. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap64CipherLong[] =
  8077. {
  8078. 0xdd,0x17,0x10,0x7c,0x45,0x04,0xac,0x43,
  8079. 0xef,0xa8,0xcc,0x32,0x34,0x87,0x88,0xd7,
  8080. 0xae,0x74,0x94,0x72,0x8e,0xd0,0x71,0xc0,
  8081. 0x89,0x8a,0xa1,0xb0,0x29,0xa0,0x10,0x9e
  8082. };
  8083. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap64_2CipherLong[] =
  8084. {
  8085. 0x3f,0xe7,0xd5,0xf3,0xfa,0x09,0xfe,0x40,
  8086. 0xa6,0xa1,0x32,0x8b,0x57,0x12,0xb9,0xfa,
  8087. 0xf2,0x2d,0xe4,0x3c,0x66,0x1d,0x0a,0x8e,
  8088. 0x46,0xf8,0x2e,0x33,0xce,0x8d,0x4e,0x3b,
  8089. 0x17,0x67,0x9e,0x9f,0x76,0x9e,0xc2,0x99,
  8090. 0xd5,0xd4,0x71,0xed,0xb4,0x33,0xb2,0xcd
  8091. };
  8092. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap96Cipher[] =
  8093. {
  8094. 0x55,0x24,0xc2,0x73,0xca,0xa3,0x48,0x03,
  8095. 0x0b,0x72,0x8d,0xd7,0x6c,0x99,0x8e,0x04,
  8096. 0x9d,0x77,0xc9,0x5f,0x38
  8097. };
  8098. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap96CipherLong[] =
  8099. {
  8100. 0x55,0x24,0xc2,0x73,0xca,0xa3,0x48,0x03,
  8101. 0x0b,0x72,0x8d,0xd7,0x6c,0x99,0x8e,0x04,
  8102. 0x9d,0x77,0xc9,0x5f,0x38,0xb5,0x6e,0x44,
  8103. 0x21,0x8e,0xda,0x57,0xe0,0x41,0xc7,0x6a
  8104. };
  8105. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap96_2CipherLong[] =
  8106. {
  8107. 0xc8,0x81,0x1a,0xbe,0xc7,0x5b,0x93,0x6f,
  8108. 0xe6,0x52,0xe4,0xb1,0x2d,0x1c,0x39,0xbc,
  8109. 0xeb,0x82,0x27,0x0a,0x7e,0xa5,0x0e,0x2d,
  8110. 0x32,0xda,0xbe,0x10,0x7a,0x10,0xcc,0xd3,
  8111. 0x6f,0xc6,0x83,0x28,0x05,0x57,0x8a,0x24,
  8112. 0x44,0x76,0x17,0x81,0xb9,0x5c,0x94,0x81
  8113. };
  8114. #endif
  8115. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap128Cipher[] =
  8116. {
  8117. 0xe1,0x33,0x38,0xe3,0x6c,0xb7,0x19,0x62,
  8118. 0xe0,0x0d,0x02,0x0b,0x4c,0xed,0xbd,0x86,
  8119. 0xd3,0xda,0xe1,0x5b,0x04
  8120. };
  8121. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap128CipherLong[] =
  8122. {
  8123. 0xe1,0x33,0x38,0xe3,0x6c,0xb7,0x19,0x62,
  8124. 0xe0,0x0d,0x02,0x0b,0x4c,0xed,0xbd,0x86,
  8125. 0xd3,0xda,0xe1,0x5b,0x04,0xbb,0x35,0x2f,
  8126. 0xa0,0xf5,0x9f,0xeb,0xfc,0xb4,0xda,0x3e
  8127. };
  8128. #ifdef WOLFSSL_ARMASM
  8129. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap128_2CipherLong[] =
  8130. {
  8131. 0xba,0x76,0xaa,0x54,0xd5,0xb5,0x60,0x67,
  8132. 0xc1,0xa7,0x90,0x3b,0x3f,0xdd,0xfa,0x89,
  8133. 0x24,0xdf,0x0c,0x56,0x5c,0xf4,0x2a,0x68,
  8134. 0x97,0x87,0x13,0xb6,0x7a,0xd1,0x24,0xfd,
  8135. 0x4d,0x3f,0x77,0x4a,0xb9,0xe4,0x7d,0xa2,
  8136. 0xdb,0xb9,0x31,0x5e,0xa3,0x11,0x06,0x80
  8137. };
  8138. #endif
  8139. #endif /* WOLFSSL_AES_128 */
  8140. #ifdef WOLFSSL_AES_192
  8141. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Key[] =
  8142. {
  8143. 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  8144. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  8145. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b
  8146. };
  8147. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Cipher[] =
  8148. {
  8149. 0x1a,0xbc,0x93,0x24,0x17,0x52,0x1c,0xa2,
  8150. 0x4f,0x2b,0x04,0x59,0xfe,0x7e,0x6e,0x0b,
  8151. 0x09,0x03,0x39,0xec,0x0a,0xa6,0xfa,0xef,
  8152. 0xd5,0xcc,0xc2,0xc6,0xf4,0xce,0x8e,0x94,
  8153. 0x1e,0x36,0xb2,0x6b,0xd1,0xeb,0xc6,0x70,
  8154. 0xd1,0xbd,0x1d,0x66,0x56,0x20,0xab,0xf7,
  8155. 0x4f,0x78,0xa7,0xf6,0xd2,0x98,0x09,0x58,
  8156. 0x5a,0x97,0xda,0xec,0x58,0xc6,0xb0,0x50
  8157. };
  8158. #ifdef WOLFSSL_ARMASM
  8159. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap32Cipher[] =
  8160. {
  8161. 0x28,0xaa,0xfa,0x90,0x72,0x74,0x86,0xaf,
  8162. 0x72,0x73,0x35,0x17,0x70,0x4e,0x7d,0xca,
  8163. 0x0c,0x33,0x97,0x06,0xc0
  8164. };
  8165. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap32CipherLong[] =
  8166. {
  8167. 0x28,0xaa,0xfa,0x90,0x72,0x74,0x86,0xaf,
  8168. 0x72,0x73,0x35,0x17,0x70,0x4e,0x7d,0xca,
  8169. 0x0c,0x33,0x97,0x06,0xc0,0xbe,0x83,0x87,
  8170. 0xdd,0xd3,0xff,0xd8,0xe4,0x6a,0x5b,0x84
  8171. };
  8172. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap32_2CipherLong[] =
  8173. {
  8174. 0xf5,0x00,0xa2,0x91,0x54,0xa3,0x76,0xa2,
  8175. 0xdd,0xad,0x16,0x89,0xe5,0xf0,0x1d,0x40,
  8176. 0x84,0xcd,0x74,0x84,0xcb,0x8b,0x9e,0x29,
  8177. 0xa9,0x8a,0x12,0x65,0xa0,0x79,0x5e,0xce,
  8178. 0xd9,0x50,0x65,0x21,0x86,0xb0,0x85,0x0d,
  8179. 0x98,0x2d,0x9a,0x5a,0x11,0xbe,0xa5,0x7f
  8180. };
  8181. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap64Cipher[] =
  8182. {
  8183. 0xfe,0x39,0x27,0x97,0xac,0xe5,0xb8,0x74,
  8184. 0xb9,0x8c,0xbf,0x58,0x71,0xa4,0x80,0x33,
  8185. 0x3d,0xf7,0xb4,0xfd,0x8c
  8186. };
  8187. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap64CipherLong[] =
  8188. {
  8189. 0xfe,0x39,0x27,0x97,0xac,0xe5,0xb8,0x74,
  8190. 0xb9,0x8c,0xbf,0x58,0x71,0xa4,0x80,0x33,
  8191. 0x3d,0xf7,0xb4,0xfd,0x8c,0x55,0x47,0x10,
  8192. 0xd5,0x91,0x35,0xbe,0xd8,0x0d,0xa5,0x9e
  8193. };
  8194. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap64_2CipherLong[] =
  8195. {
  8196. 0x59,0xf1,0xed,0x70,0x62,0x42,0xa8,0x06,
  8197. 0x07,0x36,0xe1,0xc5,0x04,0x79,0xc3,0x9b,
  8198. 0xd1,0x14,0x5c,0xcc,0x6f,0x81,0x5f,0x2f,
  8199. 0xa0,0xde,0xcf,0x61,0x55,0x18,0x7a,0xac,
  8200. 0xb0,0x59,0x37,0x90,0x53,0xb3,0x00,0x88,
  8201. 0xb4,0x49,0x90,0x7b,0x96,0xcd,0xcc,0xc3
  8202. };
  8203. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap96Cipher[] =
  8204. {
  8205. 0x41,0x21,0x40,0x31,0xfb,0xc8,0xad,0x23,
  8206. 0xde,0x00,0x03,0xd8,0x96,0x45,0xc7,0xb5,
  8207. 0x47,0xb5,0xf3,0x30,0x3b
  8208. };
  8209. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap96CipherLong[] =
  8210. {
  8211. 0x41,0x21,0x40,0x31,0xfb,0xc8,0xad,0x23,
  8212. 0xde,0x00,0x03,0xd8,0x96,0x45,0xc7,0xb5,
  8213. 0x47,0xb5,0xf3,0x30,0x3b,0xef,0xcd,0x80,
  8214. 0xe3,0x61,0x34,0xef,0x4e,0x1b,0x79,0xc1
  8215. };
  8216. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap96_2CipherLong[] =
  8217. {
  8218. 0x3c,0xb2,0xff,0xc0,0x24,0xe1,0xf5,0xc4,
  8219. 0x0f,0xd1,0x0a,0x1b,0xbe,0x1f,0x23,0xa1,
  8220. 0x8e,0xbf,0x2b,0x96,0xb6,0x37,0xc8,0x25,
  8221. 0x06,0x90,0xe2,0xca,0x71,0x24,0x52,0x95,
  8222. 0xaa,0x8c,0x80,0xdf,0xb7,0xd7,0x30,0xb0,
  8223. 0xcc,0x06,0x4f,0x28,0xa2,0x74,0x27,0xf8
  8224. };
  8225. #endif
  8226. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap128Cipher[] =
  8227. {
  8228. 0x5c,0xc3,0x8f,0xab,0x30,0xb6,0xac,0x67,
  8229. 0xdc,0xc2,0x1e,0x7b,0x01,0x2e,0xcf,0x98,
  8230. 0x8c,0x68,0xa7,0xd9,0x57
  8231. };
  8232. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap128CipherLong[] =
  8233. {
  8234. 0x5c,0xc3,0x8f,0xab,0x30,0xb6,0xac,0x67,
  8235. 0xdc,0xc2,0x1e,0x7b,0x01,0x2e,0xcf,0x98,
  8236. 0x8c,0x68,0xa7,0xd9,0x57,0xab,0x09,0x0f,
  8237. 0x01,0xc4,0x4e,0x62,0xaf,0xc2,0xdf,0x1a
  8238. };
  8239. #ifdef WOLFSSL_ARMASM
  8240. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap128_2CipherLong[] =
  8241. {
  8242. 0x88,0x0a,0x26,0x4e,0xa8,0x26,0x21,0xe0,
  8243. 0xfc,0xbc,0x63,0xdc,0xd9,0x60,0x52,0xb2,
  8244. 0x99,0x2f,0xbb,0x1e,0x00,0xf5,0x9f,0x6d,
  8245. 0xab,0x48,0x0f,0xc6,0x37,0x12,0x56,0xe3,
  8246. 0x12,0x8d,0x31,0xc8,0xea,0xf4,0x41,0x82,
  8247. 0x7a,0x88,0xe0,0xd7,0xf0,0x67,0x03,0xa4
  8248. };
  8249. #endif
  8250. #endif
  8251. #ifdef WOLFSSL_AES_256
  8252. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Key[] =
  8253. {
  8254. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  8255. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  8256. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  8257. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  8258. };
  8259. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Cipher[] =
  8260. {
  8261. 0x60,0x1e,0xc3,0x13,0x77,0x57,0x89,0xa5,
  8262. 0xb7,0xa7,0xf5,0x04,0xbb,0xf3,0xd2,0x28,
  8263. 0xf4,0x43,0xe3,0xca,0x4d,0x62,0xb5,0x9a,
  8264. 0xca,0x84,0xe9,0x90,0xca,0xca,0xf5,0xc5,
  8265. 0x2b,0x09,0x30,0xda,0xa2,0x3d,0xe9,0x4c,
  8266. 0xe8,0x70,0x17,0xba,0x2d,0x84,0x98,0x8d,
  8267. 0xdf,0xc9,0xc5,0x8d,0xb6,0x7a,0xad,0xa6,
  8268. 0x13,0xc2,0xdd,0x08,0x45,0x79,0x41,0xa6
  8269. };
  8270. #ifdef WOLFSSL_ARMASM
  8271. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap32Cipher[] =
  8272. {
  8273. 0xb0,0xa8,0xc0,0x65,0x85,0x20,0x0d,0x5c,
  8274. 0x25,0xcf,0xe7,0x58,0x63,0xc8,0xd4,0xea,
  8275. 0xa2,0x13,0x47,0x74,0xda
  8276. };
  8277. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap32CipherLong[] =
  8278. {
  8279. 0xb0,0xa8,0xc0,0x65,0x85,0x20,0x0d,0x5c,
  8280. 0x25,0xcf,0xe7,0x58,0x63,0xc8,0xd4,0xea,
  8281. 0xa2,0x13,0x47,0x74,0xda,0x89,0x77,0x40,
  8282. 0x28,0x9c,0xe8,0x19,0x26,0x32,0xd8,0x1f
  8283. };
  8284. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap32_2CipherLong[] =
  8285. {
  8286. 0xf6,0xd9,0x22,0xc6,0x80,0x29,0xaf,0x14,
  8287. 0x54,0x6c,0x0a,0xce,0x42,0xea,0x3c,0xa1,
  8288. 0x7c,0xeb,0x36,0x0d,0x8e,0xd7,0x8c,0x59,
  8289. 0xa8,0x09,0x9f,0x9e,0xba,0x5b,0x95,0xfa,
  8290. 0x26,0x8c,0x37,0x59,0xf8,0xae,0x8e,0xaa,
  8291. 0x4d,0xe4,0x1c,0xfe,0x51,0xc7,0xb7,0xcc
  8292. };
  8293. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap64Cipher[] =
  8294. {
  8295. 0x74,0x1a,0x52,0x41,0x76,0xb4,0x11,0x8f,
  8296. 0xfd,0x57,0x31,0xfd,0x3d,0x76,0x8f,0x07,
  8297. 0xd4,0x94,0x4c,0xcd,0x4d
  8298. };
  8299. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap64CipherLong[] =
  8300. {
  8301. 0x74,0x1a,0x52,0x41,0x76,0xb4,0x11,0x8f,
  8302. 0xfd,0x57,0x31,0xfd,0x3d,0x76,0x8f,0x07,
  8303. 0xd4,0x94,0x4c,0xcd,0x4d,0x47,0x5a,0x92,
  8304. 0x26,0x49,0x81,0x7a,0xda,0x36,0x27,0x01
  8305. };
  8306. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap64_2CipherLong[] =
  8307. {
  8308. 0xf7,0x9c,0xbf,0xf6,0xa2,0xaa,0x8a,0x0a,
  8309. 0x63,0x8a,0x20,0x2f,0x12,0xf1,0x8e,0x49,
  8310. 0x30,0xc0,0x8d,0x5c,0x5f,0x8b,0xbc,0x16,
  8311. 0xdd,0x71,0xee,0x13,0x14,0x7b,0xe1,0x25,
  8312. 0xcb,0x87,0x8a,0xc6,0xdc,0x1d,0x54,0x7a,
  8313. 0xe1,0xe4,0x6f,0x0d,0x95,0x1b,0xd1,0x8b
  8314. };
  8315. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap96Cipher[] =
  8316. {
  8317. 0xb9,0x07,0x02,0xb8,0xbe,0x94,0xbf,0x53,
  8318. 0xdf,0x83,0x8e,0x23,0x8c,0x67,0x0c,0x81,
  8319. 0xb8,0x69,0xa1,0x48,0x45
  8320. };
  8321. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap96CipherLong[] =
  8322. {
  8323. 0xb9,0x07,0x02,0xb8,0xbe,0x94,0xbf,0x53,
  8324. 0xdf,0x83,0x8e,0x23,0x8c,0x67,0x0c,0x81,
  8325. 0xb8,0x69,0xa1,0x48,0x45,0xf1,0xc6,0x27,
  8326. 0x36,0xa8,0xb2,0x4b,0x0e,0x62,0x6b,0x72
  8327. };
  8328. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap96_2CipherLong[] =
  8329. {
  8330. 0xd5,0x56,0x73,0xaa,0xb8,0xe4,0x06,0xf6,
  8331. 0x83,0x45,0x3a,0xb4,0xb9,0x63,0xec,0xad,
  8332. 0x73,0xc5,0xab,0x78,0xb1,0x21,0xab,0xef,
  8333. 0x69,0x15,0xb7,0x0c,0xe9,0xb4,0x3a,0xe7,
  8334. 0xbc,0xc4,0x22,0xbd,0x93,0xba,0x52,0xe0,
  8335. 0x91,0x2f,0x5e,0x8d,0x6d,0x59,0xf7,0xc2
  8336. };
  8337. #endif
  8338. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap128Cipher[] =
  8339. {
  8340. 0x50,0xfd,0x97,0xc3,0xe6,0x1a,0xbb,0x48,
  8341. 0x73,0xfb,0x78,0xdf,0x1e,0x8e,0x77,0xe6,
  8342. 0x4b,0x45,0x7c,0xd6,0x8a
  8343. };
  8344. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap128CipherLong[] =
  8345. {
  8346. 0x50,0xfd,0x97,0xc3,0xe6,0x1a,0xbb,0x48,
  8347. 0x73,0xfb,0x78,0xdf,0x1e,0x8e,0x77,0xe6,
  8348. 0x4b,0x45,0x7c,0xd6,0x8a,0xcc,0xda,0x4a,
  8349. 0x89,0xfa,0x23,0x6c,0x06,0xbf,0x26,0x05
  8350. };
  8351. #ifdef WOLFSSL_ARMASM
  8352. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap128_2CipherLong[] =
  8353. {
  8354. 0x24,0x5c,0x09,0xa0,0x3b,0x1a,0x5a,0x94,
  8355. 0x2b,0x93,0x56,0x13,0x48,0xa0,0x21,0xce,
  8356. 0x95,0x11,0xa3,0x76,0xd6,0x59,0x88,0x42,
  8357. 0x04,0x71,0x69,0x62,0x28,0xb2,0xee,0x9d,
  8358. 0xd5,0xa0,0xea,0xc7,0x37,0x93,0x92,0xc7,
  8359. 0xf2,0xb6,0x8d,0xd9,0x59,0x1a,0xfa,0xbb
  8360. };
  8361. #endif
  8362. #endif
  8363. int i;
  8364. struct {
  8365. const byte* key;
  8366. int keySz;
  8367. const byte* iv;
  8368. const byte* plain;
  8369. int len;
  8370. const byte* cipher;
  8371. } testVec[] = {
  8372. #ifdef WOLFSSL_AES_128
  8373. { ctr128Key, (int)sizeof(ctr128Key), ctrIv,
  8374. ctrPlain, (int)sizeof(ctrPlain), ctr128Cipher },
  8375. /* let's try with just 9 bytes, non block size test */
  8376. { ctr128Key, (int)sizeof(ctr128Key), ctrIv,
  8377. ctrPlain, (int)sizeof(oddCipher), ctr128Cipher },
  8378. /* and an additional 9 bytes to reuse tmp left buffer */
  8379. { NULL, 0, NULL, ctrPlain, (int)sizeof(oddCipher), oddCipher },
  8380. /* Counter wrapping */
  8381. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap128,
  8382. ctrPlain, (int)sizeof(ctr128Wrap128Cipher), ctr128Wrap128Cipher },
  8383. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap128,
  8384. ctrPlain, (int)sizeof(ctr128Wrap128CipherLong),
  8385. ctr128Wrap128CipherLong },
  8386. #ifdef WOLFSSL_ARMASM
  8387. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap128_2,
  8388. ctrPlain, (int)sizeof(ctr128Wrap128_2CipherLong),
  8389. ctr128Wrap128_2CipherLong },
  8390. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap96,
  8391. ctrPlain, (int)sizeof(ctr128Wrap96Cipher), ctr128Wrap96Cipher },
  8392. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap96,
  8393. ctrPlain, (int)sizeof(ctr128Wrap96CipherLong),
  8394. ctr128Wrap96CipherLong },
  8395. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap96_2,
  8396. ctrPlain, (int)sizeof(ctr128Wrap96_2CipherLong),
  8397. ctr128Wrap96_2CipherLong },
  8398. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap64,
  8399. ctrPlain, (int)sizeof(ctr128Wrap64Cipher), ctr128Wrap64Cipher },
  8400. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap64,
  8401. ctrPlain, (int)sizeof(ctr128Wrap64CipherLong),
  8402. ctr128Wrap64CipherLong },
  8403. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap64_2,
  8404. ctrPlain, (int)sizeof(ctr128Wrap64_2CipherLong),
  8405. ctr128Wrap64_2CipherLong },
  8406. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap32,
  8407. ctrPlain, (int)sizeof(ctr128Wrap32Cipher), ctr128Wrap32Cipher },
  8408. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap32,
  8409. ctrPlain, (int)sizeof(ctr128Wrap32CipherLong),
  8410. ctr128Wrap32CipherLong },
  8411. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap32_2,
  8412. ctrPlain, (int)sizeof(ctr128Wrap32_2CipherLong),
  8413. ctr128Wrap32_2CipherLong },
  8414. #endif
  8415. #endif
  8416. #ifdef WOLFSSL_AES_192
  8417. { ctr192Key, (int)sizeof(ctr192Key), ctrIv,
  8418. ctrPlain, (int)sizeof(ctrPlain), ctr192Cipher },
  8419. /* let's try with just 9 bytes, non block size test */
  8420. { ctr192Key, (int)sizeof(ctr192Key), ctrIv,
  8421. ctrPlain, (int)sizeof(oddCipher), ctr192Cipher },
  8422. /* Counter wrapping */
  8423. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap128,
  8424. ctrPlain, (int)sizeof(ctr192Wrap128Cipher), ctr192Wrap128Cipher },
  8425. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap128,
  8426. ctrPlain, (int)sizeof(ctr192Wrap128CipherLong),
  8427. ctr192Wrap128CipherLong },
  8428. #ifdef WOLFSSL_ARMASM
  8429. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap128_2,
  8430. ctrPlain, (int)sizeof(ctr192Wrap128_2CipherLong),
  8431. ctr192Wrap128_2CipherLong },
  8432. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap96,
  8433. ctrPlain, (int)sizeof(ctr192Wrap96Cipher), ctr192Wrap96Cipher },
  8434. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap96,
  8435. ctrPlain, (int)sizeof(ctr192Wrap96CipherLong),
  8436. ctr192Wrap96CipherLong },
  8437. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap96_2,
  8438. ctrPlain, (int)sizeof(ctr192Wrap96_2CipherLong),
  8439. ctr192Wrap96_2CipherLong },
  8440. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap64,
  8441. ctrPlain, (int)sizeof(ctr192Wrap64Cipher), ctr192Wrap64Cipher },
  8442. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap64,
  8443. ctrPlain, (int)sizeof(ctr192Wrap64CipherLong),
  8444. ctr192Wrap64CipherLong },
  8445. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap64_2,
  8446. ctrPlain, (int)sizeof(ctr192Wrap64_2CipherLong),
  8447. ctr192Wrap64_2CipherLong },
  8448. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap32,
  8449. ctrPlain, (int)sizeof(ctr192Wrap32Cipher), ctr192Wrap32Cipher },
  8450. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap32,
  8451. ctrPlain, (int)sizeof(ctr192Wrap32CipherLong),
  8452. ctr192Wrap32CipherLong },
  8453. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap32_2,
  8454. ctrPlain, (int)sizeof(ctr192Wrap32_2CipherLong),
  8455. ctr192Wrap32_2CipherLong },
  8456. #endif
  8457. #endif
  8458. #ifdef WOLFSSL_AES_256
  8459. { ctr256Key, (int)sizeof(ctr256Key), ctrIv,
  8460. ctrPlain, (int)sizeof(ctrPlain), ctr256Cipher },
  8461. /* let's try with just 9 bytes, non block size test */
  8462. { ctr256Key, (int)sizeof(ctr256Key), ctrIv,
  8463. ctrPlain, (int)sizeof(oddCipher), ctr256Cipher },
  8464. /* Counter wrapping */
  8465. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap128,
  8466. ctrPlain, (int)sizeof(ctr256Wrap128Cipher), ctr256Wrap128Cipher },
  8467. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap128,
  8468. ctrPlain, (int)sizeof(ctr256Wrap128CipherLong),
  8469. ctr256Wrap128CipherLong },
  8470. #ifdef WOLFSSL_ARMASM
  8471. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap128_2,
  8472. ctrPlain, (int)sizeof(ctr256Wrap128_2CipherLong),
  8473. ctr256Wrap128_2CipherLong },
  8474. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap96,
  8475. ctrPlain, (int)sizeof(ctr256Wrap96Cipher), ctr256Wrap96Cipher },
  8476. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap96,
  8477. ctrPlain, (int)sizeof(ctr256Wrap96CipherLong),
  8478. ctr256Wrap96CipherLong },
  8479. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap96_2,
  8480. ctrPlain, (int)sizeof(ctr256Wrap96_2CipherLong),
  8481. ctr256Wrap96_2CipherLong },
  8482. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap64,
  8483. ctrPlain, (int)sizeof(ctr256Wrap64Cipher), ctr256Wrap64Cipher },
  8484. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap64,
  8485. ctrPlain, (int)sizeof(ctr256Wrap64CipherLong),
  8486. ctr256Wrap64CipherLong },
  8487. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap64_2,
  8488. ctrPlain, (int)sizeof(ctr256Wrap64_2CipherLong),
  8489. ctr256Wrap64_2CipherLong },
  8490. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap32,
  8491. ctrPlain, (int)sizeof(ctr256Wrap32Cipher), ctr256Wrap32Cipher },
  8492. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap32,
  8493. ctrPlain, (int)sizeof(ctr256Wrap32CipherLong),
  8494. ctr256Wrap32CipherLong },
  8495. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap32_2,
  8496. ctrPlain, (int)sizeof(ctr256Wrap32_2CipherLong),
  8497. ctr256Wrap32_2CipherLong },
  8498. #endif
  8499. #endif
  8500. };
  8501. #define AES_CTR_TEST_LEN (int)(sizeof(testVec) / sizeof(*testVec))
  8502. for (i = 0; i < AES_CTR_TEST_LEN; i++) {
  8503. if (testVec[i].key != NULL) {
  8504. ret = wc_AesSetKeyDirect(enc, testVec[i].key, testVec[i].keySz,
  8505. testVec[i].iv, AES_ENCRYPTION);
  8506. if (ret != 0) {
  8507. ERROR_OUT(-5930 - i * 10, out);
  8508. }
  8509. /* Ctr only uses encrypt, even on key setup */
  8510. ret = wc_AesSetKeyDirect(dec, testVec[i].key, testVec[i].keySz,
  8511. testVec[i].iv, AES_ENCRYPTION);
  8512. if (ret != 0) {
  8513. ERROR_OUT(-5931 - i * 10, out);
  8514. }
  8515. }
  8516. ret = wc_AesCtrEncrypt(enc, cipher, testVec[i].plain, testVec[i].len);
  8517. if (ret != 0) {
  8518. ERROR_OUT(-5932 - i * 10, out);
  8519. }
  8520. ret = wc_AesCtrEncrypt(dec, plain, cipher, testVec[i].len);
  8521. if (ret != 0) {
  8522. ERROR_OUT(-5933 - i * 10, out);
  8523. }
  8524. if (XMEMCMP(plain, ctrPlain, testVec[i].len)) {
  8525. ERROR_OUT(-5934 - i * 10, out);
  8526. }
  8527. if (XMEMCMP(cipher, testVec[i].cipher, testVec[i].len)) {
  8528. ERROR_OUT(-5935 - i * 10, out);
  8529. }
  8530. }
  8531. out:
  8532. return ret;
  8533. }
  8534. #endif /* WOLFSSL_AES_COUNTER */
  8535. WOLFSSL_TEST_SUBROUTINE int aes_test(void)
  8536. {
  8537. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_COUNTER) || defined(WOLFSSL_AES_DIRECT)
  8538. #ifdef WOLFSSL_SMALL_STACK
  8539. Aes *enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  8540. #else
  8541. Aes enc[1];
  8542. #endif
  8543. byte cipher[AES_BLOCK_SIZE * 4];
  8544. #ifdef HAVE_AES_DECRYPT
  8545. #ifdef WOLFSSL_SMALL_STACK
  8546. Aes *dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  8547. #else
  8548. Aes dec[1];
  8549. #endif
  8550. byte plain [AES_BLOCK_SIZE * 4];
  8551. #endif /* HAVE_AES_DECRYPT */
  8552. #endif /* HAVE_AES_CBC || WOLFSSL_AES_COUNTER || WOLFSSL_AES_DIRECT */
  8553. int ret = 0;
  8554. #ifdef HAVE_AES_CBC
  8555. #ifdef WOLFSSL_AES_128
  8556. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = { /* "Now is the time for all " w/o trailing 0 */
  8557. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  8558. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  8559. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  8560. };
  8561. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  8562. {
  8563. 0x95,0x94,0x92,0x57,0x5f,0x42,0x81,0x53,
  8564. 0x2c,0xcc,0x9d,0x46,0x77,0xa2,0x33,0xcb
  8565. };
  8566. WOLFSSL_SMALL_STACK_STATIC const byte key[] = "0123456789abcdef "; /* align */
  8567. WOLFSSL_SMALL_STACK_STATIC const byte iv[] = "1234567890abcdef "; /* align */
  8568. #ifdef WOLFSSL_SMALL_STACK
  8569. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_COUNTER) || defined(WOLFSSL_AES_DIRECT)
  8570. if (enc == NULL)
  8571. ERROR_OUT(-5990, out);
  8572. #endif
  8573. #if defined(HAVE_AES_DECRYPT) || defined(WOLFSSL_AES_COUNTER) || defined(WOLFSSL_AES_DIRECT)
  8574. if (dec == NULL)
  8575. ERROR_OUT(-5991, out);
  8576. #endif
  8577. #endif
  8578. if (wc_AesInit(enc, HEAP_HINT, devId) != 0)
  8579. ERROR_OUT(-5900, out); /* note this error code is used programmatically in cleanup. */
  8580. #if defined(HAVE_AES_DECRYPT) || defined(WOLFSSL_AES_COUNTER)
  8581. if (wc_AesInit(dec, HEAP_HINT, devId) != 0)
  8582. ERROR_OUT(-5901, out); /* note this error code is used programmatically in cleanup. */
  8583. #endif
  8584. ret = wc_AesSetKey(enc, key, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  8585. if (ret != 0)
  8586. ERROR_OUT(-5902, out);
  8587. #if defined(HAVE_AES_DECRYPT) || defined(WOLFSSL_AES_COUNTER)
  8588. ret = wc_AesSetKey(dec, key, AES_BLOCK_SIZE, iv, AES_DECRYPTION);
  8589. if (ret != 0)
  8590. ERROR_OUT(-5903, out);
  8591. #endif
  8592. XMEMSET(cipher, 0, AES_BLOCK_SIZE * 4);
  8593. ret = wc_AesCbcEncrypt(enc, cipher, msg, AES_BLOCK_SIZE);
  8594. #if defined(WOLFSSL_ASYNC_CRYPT)
  8595. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  8596. #endif
  8597. if (ret != 0)
  8598. ERROR_OUT(-5904, out);
  8599. #ifdef HAVE_AES_DECRYPT
  8600. XMEMSET(plain, 0, AES_BLOCK_SIZE * 4);
  8601. ret = wc_AesCbcDecrypt(dec, plain, cipher, AES_BLOCK_SIZE);
  8602. #if defined(WOLFSSL_ASYNC_CRYPT)
  8603. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  8604. #endif
  8605. if (ret != 0)
  8606. ERROR_OUT(-5905, out);
  8607. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  8608. ERROR_OUT(-5906, out);
  8609. #endif /* HAVE_AES_DECRYPT */
  8610. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE))
  8611. ERROR_OUT(-5907, out);
  8612. #endif /* WOLFSSL_AES_128 */
  8613. #if defined(WOLFSSL_AESNI) && defined(HAVE_AES_DECRYPT)
  8614. {
  8615. WOLFSSL_SMALL_STACK_STATIC const byte bigMsg[] = {
  8616. /* "All work and no play makes Jack a dull boy. " */
  8617. 0x41,0x6c,0x6c,0x20,0x77,0x6f,0x72,0x6b,
  8618. 0x20,0x61,0x6e,0x64,0x20,0x6e,0x6f,0x20,
  8619. 0x70,0x6c,0x61,0x79,0x20,0x6d,0x61,0x6b,
  8620. 0x65,0x73,0x20,0x4a,0x61,0x63,0x6b,0x20,
  8621. 0x61,0x20,0x64,0x75,0x6c,0x6c,0x20,0x62,
  8622. 0x6f,0x79,0x2e,0x20,0x41,0x6c,0x6c,0x20,
  8623. 0x77,0x6f,0x72,0x6b,0x20,0x61,0x6e,0x64,
  8624. 0x20,0x6e,0x6f,0x20,0x70,0x6c,0x61,0x79,
  8625. 0x20,0x6d,0x61,0x6b,0x65,0x73,0x20,0x4a,
  8626. 0x61,0x63,0x6b,0x20,0x61,0x20,0x64,0x75,
  8627. 0x6c,0x6c,0x20,0x62,0x6f,0x79,0x2e,0x20,
  8628. 0x41,0x6c,0x6c,0x20,0x77,0x6f,0x72,0x6b,
  8629. 0x20,0x61,0x6e,0x64,0x20,0x6e,0x6f,0x20,
  8630. 0x70,0x6c,0x61,0x79,0x20,0x6d,0x61,0x6b,
  8631. 0x65,0x73,0x20,0x4a,0x61,0x63,0x6b,0x20,
  8632. 0x61,0x20,0x64,0x75,0x6c,0x6c,0x20,0x62,
  8633. 0x6f,0x79,0x2e,0x20,0x41,0x6c,0x6c,0x20,
  8634. 0x77,0x6f,0x72,0x6b,0x20,0x61,0x6e,0x64,
  8635. 0x20,0x6e,0x6f,0x20,0x70,0x6c,0x61,0x79,
  8636. 0x20,0x6d,0x61,0x6b,0x65,0x73,0x20,0x4a,
  8637. 0x61,0x63,0x6b,0x20,0x61,0x20,0x64,0x75,
  8638. 0x6c,0x6c,0x20,0x62,0x6f,0x79,0x2e,0x20,
  8639. 0x41,0x6c,0x6c,0x20,0x77,0x6f,0x72,0x6b,
  8640. 0x20,0x61,0x6e,0x64,0x20,0x6e,0x6f,0x20,
  8641. 0x70,0x6c,0x61,0x79,0x20,0x6d,0x61,0x6b,
  8642. 0x65,0x73,0x20,0x4a,0x61,0x63,0x6b,0x20,
  8643. 0x61,0x20,0x64,0x75,0x6c,0x6c,0x20,0x62,
  8644. 0x6f,0x79,0x2e,0x20,0x41,0x6c,0x6c,0x20,
  8645. 0x77,0x6f,0x72,0x6b,0x20,0x61,0x6e,0x64,
  8646. 0x20,0x6e,0x6f,0x20,0x70,0x6c,0x61,0x79,
  8647. 0x20,0x6d,0x61,0x6b,0x65,0x73,0x20,0x4a,
  8648. 0x61,0x63,0x6b,0x20,0x61,0x20,0x64,0x75,
  8649. 0x6c,0x6c,0x20,0x62,0x6f,0x79,0x2e,0x20,
  8650. 0x41,0x6c,0x6c,0x20,0x77,0x6f,0x72,0x6b,
  8651. 0x20,0x61,0x6e,0x64,0x20,0x6e,0x6f,0x20,
  8652. 0x70,0x6c,0x61,0x79,0x20,0x6d,0x61,0x6b,
  8653. 0x65,0x73,0x20,0x4a,0x61,0x63,0x6b,0x20,
  8654. 0x61,0x20,0x64,0x75,0x6c,0x6c,0x20,0x62,
  8655. 0x6f,0x79,0x2e,0x20,0x41,0x6c,0x6c,0x20,
  8656. 0x77,0x6f,0x72,0x6b,0x20,0x61,0x6e,0x64,
  8657. 0x20,0x6e,0x6f,0x20,0x70,0x6c,0x61,0x79,
  8658. 0x20,0x6d,0x61,0x6b,0x65,0x73,0x20,0x4a,
  8659. 0x61,0x63,0x6b,0x20,0x61,0x20,0x64,0x75,
  8660. 0x6c,0x6c,0x20,0x62,0x6f,0x79,0x2e,0x20,
  8661. 0x41,0x6c,0x6c,0x20,0x77,0x6f,0x72,0x6b,
  8662. 0x20,0x61,0x6e,0x64,0x20,0x6e,0x6f,0x20,
  8663. 0x70,0x6c,0x61,0x79,0x20,0x6d,0x61,0x6b,
  8664. 0x65,0x73,0x20,0x4a,0x61,0x63,0x6b,0x20
  8665. };
  8666. WOLFSSL_SMALL_STACK_STATIC const byte bigKey[] = "0123456789abcdeffedcba9876543210";
  8667. word32 keySz, msgSz;
  8668. #ifdef WOLFSSL_SMALL_STACK
  8669. byte *bigCipher = (byte *)XMALLOC(sizeof(bigMsg), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8670. byte *bigPlain = (byte *)XMALLOC(sizeof(bigMsg), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8671. if ((bigCipher == NULL) ||
  8672. (bigPlain == NULL)) {
  8673. if (bigCipher != NULL)
  8674. XFREE(bigCipher, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8675. ERROR_OUT(-5992, out);
  8676. }
  8677. #else
  8678. byte bigCipher[sizeof(bigMsg)];
  8679. byte bigPlain[sizeof(bigMsg)];
  8680. #endif
  8681. /* Iterate from one AES_BLOCK_SIZE of bigMsg through the whole
  8682. * message by AES_BLOCK_SIZE for each size of AES key. */
  8683. for (keySz = 16; keySz <= 32; keySz += 8) {
  8684. for (msgSz = AES_BLOCK_SIZE;
  8685. msgSz <= sizeof(bigMsg);
  8686. msgSz += AES_BLOCK_SIZE) {
  8687. XMEMSET(bigCipher, 0, sizeof(bigMsg));
  8688. XMEMSET(bigPlain, 0, sizeof(bigMsg));
  8689. ret = wc_AesSetKey(enc, bigKey, keySz, iv, AES_ENCRYPTION);
  8690. if (ret != 0) {
  8691. ret = -5908;
  8692. break;
  8693. }
  8694. ret = wc_AesSetKey(dec, bigKey, keySz, iv, AES_DECRYPTION);
  8695. if (ret != 0) {
  8696. ret = -5909;
  8697. break;
  8698. }
  8699. ret = wc_AesCbcEncrypt(enc, bigCipher, bigMsg, msgSz);
  8700. #if defined(WOLFSSL_ASYNC_CRYPT)
  8701. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  8702. #endif
  8703. if (ret != 0) {
  8704. ret = -5910;
  8705. break;
  8706. }
  8707. ret = wc_AesCbcDecrypt(dec, bigPlain, bigCipher, msgSz);
  8708. #if defined(WOLFSSL_ASYNC_CRYPT)
  8709. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  8710. #endif
  8711. if (ret != 0) {
  8712. ret = -5911;
  8713. break;
  8714. }
  8715. if (XMEMCMP(bigPlain, bigMsg, msgSz)) {
  8716. ret = -5912;
  8717. break;
  8718. }
  8719. }
  8720. if (ret != 0)
  8721. break;
  8722. }
  8723. #ifdef WOLFSSL_SMALL_STACK
  8724. XFREE(bigCipher, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8725. XFREE(bigPlain, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8726. #endif
  8727. if (ret != 0)
  8728. goto out;
  8729. }
  8730. #endif /* WOLFSSL_AESNI && HAVE_AES_DECRYPT */
  8731. /* Test of AES IV state with encrypt/decrypt */
  8732. #ifdef WOLFSSL_AES_128
  8733. {
  8734. /* Test Vector from "NIST Special Publication 800-38A, 2001 Edition"
  8735. * https://nvlpubs.nist.gov/nistpubs/legacy/sp/nistspecialpublication800-38a.pdf
  8736. */
  8737. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] =
  8738. {
  8739. 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
  8740. 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
  8741. 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c,
  8742. 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51
  8743. };
  8744. WOLFSSL_SMALL_STACK_STATIC const byte verify2[] =
  8745. {
  8746. 0x76, 0x49, 0xab, 0xac, 0x81, 0x19, 0xb2, 0x46,
  8747. 0xce, 0xe9, 0x8e, 0x9b, 0x12, 0xe9, 0x19, 0x7d,
  8748. 0x50, 0x86, 0xcb, 0x9b, 0x50, 0x72, 0x19, 0xee,
  8749. 0x95, 0xdb, 0x11, 0x3a, 0x91, 0x76, 0x78, 0xb2
  8750. };
  8751. WOLFSSL_SMALL_STACK_STATIC const byte key2[] = {
  8752. 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
  8753. 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c
  8754. };
  8755. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] = {
  8756. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  8757. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
  8758. };
  8759. ret = wc_AesSetKey(enc, key2, sizeof(key2), iv2, AES_ENCRYPTION);
  8760. if (ret != 0)
  8761. ERROR_OUT(-5913, out);
  8762. XMEMSET(cipher, 0, AES_BLOCK_SIZE * 2);
  8763. ret = wc_AesCbcEncrypt(enc, cipher, msg2, AES_BLOCK_SIZE);
  8764. #if defined(WOLFSSL_ASYNC_CRYPT)
  8765. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  8766. #endif
  8767. if (ret != 0)
  8768. ERROR_OUT(-5914, out);
  8769. if (XMEMCMP(cipher, verify2, AES_BLOCK_SIZE))
  8770. ERROR_OUT(-5915, out);
  8771. ret = wc_AesCbcEncrypt(enc, cipher + AES_BLOCK_SIZE,
  8772. msg2 + AES_BLOCK_SIZE, AES_BLOCK_SIZE);
  8773. #if defined(WOLFSSL_ASYNC_CRYPT)
  8774. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  8775. #endif
  8776. if (ret != 0)
  8777. ERROR_OUT(-5916, out);
  8778. if (XMEMCMP(cipher + AES_BLOCK_SIZE, verify2 + AES_BLOCK_SIZE,
  8779. AES_BLOCK_SIZE))
  8780. ERROR_OUT(-5917, out);
  8781. #if defined(HAVE_AES_DECRYPT)
  8782. ret = wc_AesSetKey(dec, key2, sizeof(key2), iv2, AES_DECRYPTION);
  8783. if (ret != 0)
  8784. ERROR_OUT(-5918, out);
  8785. XMEMSET(plain, 0, AES_BLOCK_SIZE * 2);
  8786. ret = wc_AesCbcDecrypt(dec, plain, verify2, AES_BLOCK_SIZE);
  8787. #if defined(WOLFSSL_ASYNC_CRYPT)
  8788. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  8789. #endif
  8790. if (ret != 0)
  8791. ERROR_OUT(-5919, out);
  8792. if (XMEMCMP(plain, msg2, AES_BLOCK_SIZE))
  8793. ERROR_OUT(-5920, out);
  8794. ret = wc_AesCbcDecrypt(dec, plain + AES_BLOCK_SIZE,
  8795. verify2 + AES_BLOCK_SIZE, AES_BLOCK_SIZE);
  8796. #if defined(WOLFSSL_ASYNC_CRYPT)
  8797. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  8798. #endif
  8799. if (ret != 0)
  8800. ERROR_OUT(-5921, out);
  8801. if (XMEMCMP(plain + AES_BLOCK_SIZE, msg2 + AES_BLOCK_SIZE,
  8802. AES_BLOCK_SIZE))
  8803. ERROR_OUT(-5922, out);
  8804. #endif /* HAVE_AES_DECRYPT */
  8805. }
  8806. #endif /* WOLFSSL_AES_128 */
  8807. #endif /* HAVE_AES_CBC */
  8808. #ifdef WOLFSSL_AES_COUNTER
  8809. ret = aesctr_test(enc, dec, cipher, plain);
  8810. if (ret != 0)
  8811. return ret;
  8812. #endif
  8813. #if defined(WOLFSSL_AES_DIRECT) && defined(WOLFSSL_AES_256)
  8814. {
  8815. WOLFSSL_SMALL_STACK_STATIC const byte niPlain[] =
  8816. {
  8817. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  8818. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  8819. };
  8820. WOLFSSL_SMALL_STACK_STATIC const byte niCipher[] =
  8821. {
  8822. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  8823. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  8824. };
  8825. WOLFSSL_SMALL_STACK_STATIC const byte niKey[] =
  8826. {
  8827. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  8828. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  8829. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  8830. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  8831. };
  8832. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  8833. ret = wc_AesSetKey(enc, niKey, sizeof(niKey), cipher, AES_ENCRYPTION);
  8834. if (ret != 0)
  8835. ERROR_OUT(-5994, out);
  8836. #if !defined(HAVE_SELFTEST) && \
  8837. (defined(WOLFSSL_LINUXKM) || \
  8838. !defined(HAVE_FIPS) || \
  8839. (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3)))
  8840. if (wc_AesEncryptDirect(enc, cipher, niPlain) != 0)
  8841. ERROR_OUT(-5995, out);
  8842. #else
  8843. wc_AesEncryptDirect(enc, cipher, niPlain);
  8844. #endif
  8845. if (XMEMCMP(cipher, niCipher, AES_BLOCK_SIZE) != 0)
  8846. ERROR_OUT(-5996, out);
  8847. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  8848. ret = wc_AesSetKey(dec, niKey, sizeof(niKey), plain, AES_DECRYPTION);
  8849. if (ret != 0)
  8850. ERROR_OUT(-5997, out);
  8851. #if !defined(HAVE_SELFTEST) && \
  8852. (defined(WOLFSSL_LINUXKM) || \
  8853. !defined(HAVE_FIPS) || \
  8854. (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3)))
  8855. if (wc_AesDecryptDirect(dec, plain, niCipher) != 0)
  8856. ERROR_OUT(-5998, out);
  8857. #else
  8858. wc_AesDecryptDirect(dec, plain, niCipher);
  8859. #endif
  8860. if (XMEMCMP(plain, niPlain, AES_BLOCK_SIZE) != 0)
  8861. ERROR_OUT(-5999, out);
  8862. }
  8863. #endif /* WOLFSSL_AES_DIRECT && WOLFSSL_AES_256 */
  8864. ret = aes_key_size_test();
  8865. if (ret != 0)
  8866. goto out;
  8867. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  8868. ret = aes_cbc_test();
  8869. if (ret != 0)
  8870. goto out;
  8871. #endif
  8872. #if defined(WOLFSSL_AES_XTS)
  8873. #ifdef WOLFSSL_AES_128
  8874. ret = aes_xts_128_test();
  8875. if (ret != 0)
  8876. goto out;
  8877. #endif
  8878. #ifdef WOLFSSL_AES_256
  8879. ret = aes_xts_256_test();
  8880. if (ret != 0)
  8881. goto out;
  8882. #endif
  8883. #if defined(WOLFSSL_AES_128) && defined(WOLFSSL_AES_256)
  8884. ret = aes_xts_sector_test();
  8885. if (ret != 0)
  8886. goto out;
  8887. #endif
  8888. #ifdef WOLFSSL_AES_128
  8889. ret = aes_xts_args_test();
  8890. if (ret != 0)
  8891. goto out;
  8892. #endif
  8893. #endif
  8894. #if defined(WOLFSSL_AES_CFB)
  8895. ret = aescfb_test();
  8896. if (ret != 0)
  8897. goto out;
  8898. #if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  8899. ret = aescfb1_test();
  8900. if (ret != 0)
  8901. goto out;
  8902. ret = aescfb8_test();
  8903. if (ret != 0)
  8904. goto out;
  8905. #endif
  8906. #endif
  8907. #if defined(HAVE_AES_ECB) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  8908. ret = aesecb_test();
  8909. if (ret != 0)
  8910. goto out;
  8911. #endif
  8912. out:
  8913. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_COUNTER) || defined(WOLFSSL_AES_DIRECT)
  8914. #ifdef WOLFSSL_SMALL_STACK
  8915. if (enc) {
  8916. if (ret != -5900) /* note this must match ERRROR_OUT() code
  8917. * for wc_AesInit(enc, ...) failure above.
  8918. */
  8919. wc_AesFree(enc);
  8920. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  8921. }
  8922. #else
  8923. if (ret != -5900)
  8924. wc_AesFree(enc);
  8925. #endif
  8926. (void)cipher;
  8927. #ifdef HAVE_AES_DECRYPT
  8928. #ifdef WOLFSSL_SMALL_STACK
  8929. if (dec) {
  8930. if ((ret != -5900) && (ret != -5901))
  8931. /* note these codes must match the ERRROR_OUT() codes for
  8932. * wc_AesInit() failures above.
  8933. */
  8934. wc_AesFree(dec);
  8935. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  8936. }
  8937. #else
  8938. if ((ret != -5900) && (ret != -5901))
  8939. wc_AesFree(dec);
  8940. #endif
  8941. (void)plain;
  8942. #endif /* HAVE_AES_DECRYPT */
  8943. #endif /* HAVE_AES_CBC || WOLFSSL_AES_COUNTER || WOLFSSL_AES_DIRECT */
  8944. return ret;
  8945. }
  8946. #ifdef WOLFSSL_AES_192
  8947. WOLFSSL_TEST_SUBROUTINE int aes192_test(void)
  8948. {
  8949. #ifdef HAVE_AES_CBC
  8950. #ifdef WOLFSSL_SMALL_STACK
  8951. Aes *enc = NULL;
  8952. #else
  8953. Aes enc[1];
  8954. #endif
  8955. byte cipher[AES_BLOCK_SIZE];
  8956. #ifdef HAVE_AES_DECRYPT
  8957. #ifdef WOLFSSL_SMALL_STACK
  8958. Aes *dec = NULL;
  8959. #else
  8960. Aes dec[1];
  8961. #endif
  8962. byte plain[AES_BLOCK_SIZE];
  8963. #endif
  8964. #endif /* HAVE_AES_CBC */
  8965. int ret = 0;
  8966. #ifdef HAVE_AES_CBC
  8967. /* Test vectors from NIST Special Publication 800-38A, 2001 Edition
  8968. * Appendix F.2.3 */
  8969. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = {
  8970. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  8971. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  8972. };
  8973. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  8974. {
  8975. 0x4f,0x02,0x1d,0xb2,0x43,0xbc,0x63,0x3d,
  8976. 0x71,0x78,0x18,0x3a,0x9f,0xa0,0x71,0xe8
  8977. };
  8978. WOLFSSL_SMALL_STACK_STATIC byte key[] = {
  8979. 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  8980. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  8981. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b
  8982. };
  8983. WOLFSSL_SMALL_STACK_STATIC byte iv[] = {
  8984. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  8985. 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F
  8986. };
  8987. #ifdef WOLFSSL_SMALL_STACK
  8988. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  8989. ERROR_OUT(-6008, out);
  8990. #ifdef HAVE_AES_DECRYPT
  8991. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  8992. ERROR_OUT(-6009, out);
  8993. #endif
  8994. #endif
  8995. if (wc_AesInit(enc, HEAP_HINT, devId) != 0)
  8996. ERROR_OUT(-6000, out);
  8997. #ifdef HAVE_AES_DECRYPT
  8998. if (wc_AesInit(dec, HEAP_HINT, devId) != 0)
  8999. ERROR_OUT(-6001, out);
  9000. #endif
  9001. ret = wc_AesSetKey(enc, key, (int) sizeof(key), iv, AES_ENCRYPTION);
  9002. if (ret != 0)
  9003. ERROR_OUT(-6002, out);
  9004. #ifdef HAVE_AES_DECRYPT
  9005. ret = wc_AesSetKey(dec, key, (int) sizeof(key), iv, AES_DECRYPTION);
  9006. if (ret != 0)
  9007. ERROR_OUT(-6003, out);
  9008. #endif
  9009. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  9010. ret = wc_AesCbcEncrypt(enc, cipher, msg, (int) sizeof(msg));
  9011. #if defined(WOLFSSL_ASYNC_CRYPT)
  9012. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  9013. #endif
  9014. if (ret != 0)
  9015. ERROR_OUT(-6004, out);
  9016. #ifdef HAVE_AES_DECRYPT
  9017. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  9018. ret = wc_AesCbcDecrypt(dec, plain, cipher, (int) sizeof(cipher));
  9019. #if defined(WOLFSSL_ASYNC_CRYPT)
  9020. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  9021. #endif
  9022. if (ret != 0)
  9023. ERROR_OUT(-6005, out);
  9024. if (XMEMCMP(plain, msg, (int) sizeof(plain))) {
  9025. ERROR_OUT(-6006, out);
  9026. }
  9027. #endif
  9028. if (XMEMCMP(cipher, verify, (int) sizeof(cipher)))
  9029. ERROR_OUT(-6007, out);
  9030. wc_AesFree(enc);
  9031. #ifdef HAVE_AES_DECRYPT
  9032. wc_AesFree(dec);
  9033. #endif
  9034. out:
  9035. #ifdef WOLFSSL_SMALL_STACK
  9036. if (enc)
  9037. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  9038. #ifdef HAVE_AES_DECRYPT
  9039. if (dec)
  9040. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  9041. #endif
  9042. #endif
  9043. #endif /* HAVE_AES_CBC */
  9044. return ret;
  9045. }
  9046. #endif /* WOLFSSL_AES_192 */
  9047. #ifdef WOLFSSL_AES_256
  9048. WOLFSSL_TEST_SUBROUTINE int aes256_test(void)
  9049. {
  9050. #ifdef HAVE_AES_CBC
  9051. #ifdef WOLFSSL_SMALL_STACK
  9052. Aes *enc = NULL;
  9053. #else
  9054. Aes enc[1];
  9055. #endif
  9056. byte cipher[AES_BLOCK_SIZE];
  9057. #ifdef HAVE_AES_DECRYPT
  9058. #ifdef WOLFSSL_SMALL_STACK
  9059. Aes *dec = NULL;
  9060. #else
  9061. Aes dec[1];
  9062. #endif
  9063. byte plain[AES_BLOCK_SIZE];
  9064. #endif
  9065. #endif /* HAVE_AES_CBC */
  9066. int ret = 0;
  9067. #ifdef HAVE_AES_CBC
  9068. /* Test vectors from NIST Special Publication 800-38A, 2001 Edition,
  9069. * Appendix F.2.5 */
  9070. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = {
  9071. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  9072. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  9073. };
  9074. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  9075. {
  9076. 0xf5,0x8c,0x4c,0x04,0xd6,0xe5,0xf1,0xba,
  9077. 0x77,0x9e,0xab,0xfb,0x5f,0x7b,0xfb,0xd6
  9078. };
  9079. WOLFSSL_SMALL_STACK_STATIC byte key[] = {
  9080. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  9081. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  9082. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  9083. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  9084. };
  9085. WOLFSSL_SMALL_STACK_STATIC byte iv[] = {
  9086. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  9087. 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F
  9088. };
  9089. #ifdef WOLFSSL_SMALL_STACK
  9090. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  9091. ERROR_OUT(-6108, out);
  9092. #ifdef HAVE_AES_DECRYPT
  9093. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  9094. ERROR_OUT(-6109, out);
  9095. #endif
  9096. #endif
  9097. if (wc_AesInit(enc, HEAP_HINT, devId) != 0)
  9098. ERROR_OUT(-6100, out);
  9099. #ifdef HAVE_AES_DECRYPT
  9100. if (wc_AesInit(dec, HEAP_HINT, devId) != 0)
  9101. ERROR_OUT(-6101, out);
  9102. #endif
  9103. ret = wc_AesSetKey(enc, key, (int) sizeof(key), iv, AES_ENCRYPTION);
  9104. if (ret != 0)
  9105. ERROR_OUT(-6102, out);
  9106. #ifdef HAVE_AES_DECRYPT
  9107. ret = wc_AesSetKey(dec, key, (int) sizeof(key), iv, AES_DECRYPTION);
  9108. if (ret != 0)
  9109. ERROR_OUT(-6103, out);
  9110. #endif
  9111. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  9112. ret = wc_AesCbcEncrypt(enc, cipher, msg, (int) sizeof(msg));
  9113. #if defined(WOLFSSL_ASYNC_CRYPT)
  9114. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  9115. #endif
  9116. if (ret != 0)
  9117. ERROR_OUT(-6104, out);
  9118. #ifdef HAVE_AES_DECRYPT
  9119. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  9120. ret = wc_AesCbcDecrypt(dec, plain, cipher, (int) sizeof(cipher));
  9121. #if defined(WOLFSSL_ASYNC_CRYPT)
  9122. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  9123. #endif
  9124. if (ret != 0)
  9125. ERROR_OUT(-6105, out);
  9126. if (XMEMCMP(plain, msg, (int) sizeof(plain))) {
  9127. ERROR_OUT(-6106, out);
  9128. }
  9129. #endif
  9130. if (XMEMCMP(cipher, verify, (int) sizeof(cipher)))
  9131. ERROR_OUT(-6107, out);
  9132. wc_AesFree(enc);
  9133. #ifdef HAVE_AES_DECRYPT
  9134. wc_AesFree(dec);
  9135. #endif
  9136. out:
  9137. #ifdef WOLFSSL_SMALL_STACK
  9138. if (enc)
  9139. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  9140. #ifdef HAVE_AES_DECRYPT
  9141. if (dec)
  9142. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  9143. #endif
  9144. #endif
  9145. #endif /* HAVE_AES_CBC */
  9146. return ret;
  9147. }
  9148. #endif /* WOLFSSL_AES_256 */
  9149. #ifdef HAVE_AESGCM
  9150. #ifdef WOLFSSL_AES_128
  9151. static int aesgcm_default_test_helper(byte* key, int keySz, byte* iv, int ivSz,
  9152. byte* plain, int plainSz, byte* cipher, int cipherSz,
  9153. byte* aad, int aadSz, byte* tag, int tagSz)
  9154. {
  9155. int ret, enc_inited = 0, dec_inited = 0;
  9156. #ifdef WOLFSSL_SMALL_STACK
  9157. Aes *enc = NULL;
  9158. Aes *dec = NULL;
  9159. #else
  9160. Aes enc[1];
  9161. Aes dec[1];
  9162. #endif
  9163. byte resultT[AES_BLOCK_SIZE];
  9164. byte resultP[AES_BLOCK_SIZE * 3];
  9165. byte resultC[AES_BLOCK_SIZE * 3];
  9166. int result;
  9167. #ifdef WOLFSSL_SMALL_STACK
  9168. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  9169. ERROR_OUT(-6118, out);
  9170. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  9171. ERROR_OUT(-6119, out);
  9172. #endif
  9173. XMEMSET(resultT, 0, sizeof(resultT));
  9174. XMEMSET(resultC, 0, sizeof(resultC));
  9175. XMEMSET(resultP, 0, sizeof(resultP));
  9176. if (wc_AesInit(enc, HEAP_HINT, devId) != 0)
  9177. ERROR_OUT(-6110, out);
  9178. else
  9179. enc_inited = 1;
  9180. if (wc_AesInit(dec, HEAP_HINT, devId) != 0)
  9181. ERROR_OUT(-6111, out);
  9182. else
  9183. dec_inited = 1;
  9184. result = wc_AesGcmSetKey(enc, key, keySz);
  9185. if (result != 0)
  9186. ERROR_OUT(-6112, out);
  9187. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  9188. result = wc_AesGcmEncrypt(enc, resultC, plain, plainSz, iv, ivSz,
  9189. resultT, tagSz, aad, aadSz);
  9190. #if defined(WOLFSSL_ASYNC_CRYPT)
  9191. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  9192. #endif
  9193. if (result != 0)
  9194. ERROR_OUT(-6113, out);
  9195. if (cipher != NULL) {
  9196. if (XMEMCMP(cipher, resultC, cipherSz))
  9197. ERROR_OUT(-6114, out);
  9198. }
  9199. if (XMEMCMP(tag, resultT, tagSz))
  9200. ERROR_OUT(-6115, out);
  9201. #ifdef HAVE_AES_DECRYPT
  9202. result = wc_AesGcmSetKey(dec, key, keySz);
  9203. if (result != 0)
  9204. ERROR_OUT(-6116, out);
  9205. result = wc_AesGcmDecrypt(dec, resultP, resultC, cipherSz,
  9206. iv, ivSz, resultT, tagSz, aad, aadSz);
  9207. #if defined(WOLFSSL_ASYNC_CRYPT)
  9208. result = wc_AsyncWait(result, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  9209. #endif
  9210. if (result != 0)
  9211. ERROR_OUT(-6117, out);
  9212. if (plain != NULL) {
  9213. if (XMEMCMP(plain, resultP, plainSz))
  9214. ERROR_OUT(-6118, out);
  9215. }
  9216. #endif /* HAVE_AES_DECRYPT */
  9217. ret = 0;
  9218. out:
  9219. if (enc_inited)
  9220. wc_AesFree(enc);
  9221. if (dec_inited)
  9222. wc_AesFree(dec);
  9223. #ifdef WOLFSSL_SMALL_STACK
  9224. if (enc)
  9225. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  9226. if (dec)
  9227. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  9228. #endif
  9229. return ret;
  9230. }
  9231. #endif
  9232. /* tests that only use 12 byte IV and 16 or less byte AAD
  9233. * test vectors are from NIST SP 800-38D
  9234. * https://csrc.nist.gov/Projects/Cryptographic-Algorithm-Validation-Program/CAVP-TESTING-BLOCK-CIPHER-MODES*/
  9235. WOLFSSL_TEST_SUBROUTINE int aesgcm_default_test(void)
  9236. {
  9237. #ifdef WOLFSSL_AES_128
  9238. byte key1[] = {
  9239. 0x29, 0x8e, 0xfa, 0x1c, 0xcf, 0x29, 0xcf, 0x62,
  9240. 0xae, 0x68, 0x24, 0xbf, 0xc1, 0x95, 0x57, 0xfc
  9241. };
  9242. byte iv1[] = {
  9243. 0x6f, 0x58, 0xa9, 0x3f, 0xe1, 0xd2, 0x07, 0xfa,
  9244. 0xe4, 0xed, 0x2f, 0x6d
  9245. };
  9246. ALIGN64 byte plain1[] = {
  9247. 0xcc, 0x38, 0xbc, 0xcd, 0x6b, 0xc5, 0x36, 0xad,
  9248. 0x91, 0x9b, 0x13, 0x95, 0xf5, 0xd6, 0x38, 0x01,
  9249. 0xf9, 0x9f, 0x80, 0x68, 0xd6, 0x5c, 0xa5, 0xac,
  9250. 0x63, 0x87, 0x2d, 0xaf, 0x16, 0xb9, 0x39, 0x01
  9251. };
  9252. byte aad1[] = {
  9253. 0x02, 0x1f, 0xaf, 0xd2, 0x38, 0x46, 0x39, 0x73,
  9254. 0xff, 0xe8, 0x02, 0x56, 0xe5, 0xb1, 0xc6, 0xb1
  9255. };
  9256. ALIGN64 byte cipher1[] = {
  9257. 0xdf, 0xce, 0x4e, 0x9c, 0xd2, 0x91, 0x10, 0x3d,
  9258. 0x7f, 0xe4, 0xe6, 0x33, 0x51, 0xd9, 0xe7, 0x9d,
  9259. 0x3d, 0xfd, 0x39, 0x1e, 0x32, 0x67, 0x10, 0x46,
  9260. 0x58, 0x21, 0x2d, 0xa9, 0x65, 0x21, 0xb7, 0xdb
  9261. };
  9262. byte tag1[] = {
  9263. 0x54, 0x24, 0x65, 0xef, 0x59, 0x93, 0x16, 0xf7,
  9264. 0x3a, 0x7a, 0x56, 0x05, 0x09, 0xa2, 0xd9, 0xf2
  9265. };
  9266. byte key2[] = {
  9267. 0x01, 0x6d, 0xbb, 0x38, 0xda, 0xa7, 0x6d, 0xfe,
  9268. 0x7d, 0xa3, 0x84, 0xeb, 0xf1, 0x24, 0x03, 0x64
  9269. };
  9270. byte iv2[] = {
  9271. 0x07, 0x93, 0xef, 0x3a, 0xda, 0x78, 0x2f, 0x78,
  9272. 0xc9, 0x8a, 0xff, 0xe3
  9273. };
  9274. ALIGN64 byte plain2[] = {
  9275. 0x4b, 0x34, 0xa9, 0xec, 0x57, 0x63, 0x52, 0x4b,
  9276. 0x19, 0x1d, 0x56, 0x16, 0xc5, 0x47, 0xf6, 0xb7
  9277. };
  9278. ALIGN64 byte cipher2[] = {
  9279. 0x60, 0x9a, 0xa3, 0xf4, 0x54, 0x1b, 0xc0, 0xfe,
  9280. 0x99, 0x31, 0xda, 0xad, 0x2e, 0xe1, 0x5d, 0x0c
  9281. };
  9282. byte tag2[] = {
  9283. 0x33, 0xaf, 0xec, 0x59, 0xc4, 0x5b, 0xaf, 0x68,
  9284. 0x9a, 0x5e, 0x1b, 0x13, 0xae, 0x42, 0x36, 0x19
  9285. };
  9286. byte key3[] = {
  9287. 0xb0, 0x1e, 0x45, 0xcc, 0x30, 0x88, 0xaa, 0xba,
  9288. 0x9f, 0xa4, 0x3d, 0x81, 0xd4, 0x81, 0x82, 0x3f
  9289. };
  9290. byte iv3[] = {
  9291. 0x5a, 0x2c, 0x4a, 0x66, 0x46, 0x87, 0x13, 0x45,
  9292. 0x6a, 0x4b, 0xd5, 0xe1
  9293. };
  9294. byte tag3[] = {
  9295. 0x01, 0x42, 0x80, 0xf9, 0x44, 0xf5, 0x3c, 0x68,
  9296. 0x11, 0x64, 0xb2, 0xff
  9297. };
  9298. int ret;
  9299. ret = aesgcm_default_test_helper(key1, sizeof(key1), iv1, sizeof(iv1),
  9300. plain1, sizeof(plain1), cipher1, sizeof(cipher1),
  9301. aad1, sizeof(aad1), tag1, sizeof(tag1));
  9302. if (ret != 0) {
  9303. return ret;
  9304. }
  9305. ret = aesgcm_default_test_helper(key2, sizeof(key2), iv2, sizeof(iv2),
  9306. plain2, sizeof(plain2), cipher2, sizeof(cipher2),
  9307. NULL, 0, tag2, sizeof(tag2));
  9308. if (ret != 0) {
  9309. return ret;
  9310. }
  9311. ret = aesgcm_default_test_helper(key3, sizeof(key3), iv3, sizeof(iv3),
  9312. NULL, 0, NULL, 0,
  9313. NULL, 0, tag3, sizeof(tag3));
  9314. if (ret != 0) {
  9315. return ret;
  9316. }
  9317. #endif
  9318. return 0;
  9319. }
  9320. WOLFSSL_TEST_SUBROUTINE int aesgcm_test(void)
  9321. {
  9322. #ifdef WOLFSSL_SMALL_STACK
  9323. Aes *enc = NULL;
  9324. Aes *dec = NULL;
  9325. #else
  9326. Aes enc[1];
  9327. Aes dec[1];
  9328. #endif
  9329. /*
  9330. * This is Test Case 16 from the document Galois/
  9331. * Counter Mode of Operation (GCM) by McGrew and
  9332. * Viega.
  9333. */
  9334. WOLFSSL_SMALL_STACK_STATIC const byte p[] =
  9335. {
  9336. 0xd9, 0x31, 0x32, 0x25, 0xf8, 0x84, 0x06, 0xe5,
  9337. 0xa5, 0x59, 0x09, 0xc5, 0xaf, 0xf5, 0x26, 0x9a,
  9338. 0x86, 0xa7, 0xa9, 0x53, 0x15, 0x34, 0xf7, 0xda,
  9339. 0x2e, 0x4c, 0x30, 0x3d, 0x8a, 0x31, 0x8a, 0x72,
  9340. 0x1c, 0x3c, 0x0c, 0x95, 0x95, 0x68, 0x09, 0x53,
  9341. 0x2f, 0xcf, 0x0e, 0x24, 0x49, 0xa6, 0xb5, 0x25,
  9342. 0xb1, 0x6a, 0xed, 0xf5, 0xaa, 0x0d, 0xe6, 0x57,
  9343. 0xba, 0x63, 0x7b, 0x39
  9344. };
  9345. #if defined(WOLFSSL_AES_256) || defined(WOLFSSL_AES_192)
  9346. WOLFSSL_SMALL_STACK_STATIC const byte a[] =
  9347. {
  9348. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  9349. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  9350. 0xab, 0xad, 0xda, 0xd2
  9351. };
  9352. #endif
  9353. #ifdef WOLFSSL_AES_256
  9354. WOLFSSL_SMALL_STACK_STATIC const byte k1[] =
  9355. {
  9356. 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c,
  9357. 0x6d, 0x6a, 0x8f, 0x94, 0x67, 0x30, 0x83, 0x08,
  9358. 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c,
  9359. 0x6d, 0x6a, 0x8f, 0x94, 0x67, 0x30, 0x83, 0x08
  9360. };
  9361. WOLFSSL_SMALL_STACK_STATIC const byte iv1[] =
  9362. {
  9363. 0xca, 0xfe, 0xba, 0xbe, 0xfa, 0xce, 0xdb, 0xad,
  9364. 0xde, 0xca, 0xf8, 0x88
  9365. };
  9366. #endif /* WOLFSSL_AES_256 */
  9367. #if defined(WOLFSSL_AES_256) || defined(WOLFSSL_AES_192)
  9368. WOLFSSL_SMALL_STACK_STATIC const byte c1[] =
  9369. {
  9370. 0x52, 0x2d, 0xc1, 0xf0, 0x99, 0x56, 0x7d, 0x07,
  9371. 0xf4, 0x7f, 0x37, 0xa3, 0x2a, 0x84, 0x42, 0x7d,
  9372. 0x64, 0x3a, 0x8c, 0xdc, 0xbf, 0xe5, 0xc0, 0xc9,
  9373. 0x75, 0x98, 0xa2, 0xbd, 0x25, 0x55, 0xd1, 0xaa,
  9374. 0x8c, 0xb0, 0x8e, 0x48, 0x59, 0x0d, 0xbb, 0x3d,
  9375. 0xa7, 0xb0, 0x8b, 0x10, 0x56, 0x82, 0x88, 0x38,
  9376. 0xc5, 0xf6, 0x1e, 0x63, 0x93, 0xba, 0x7a, 0x0a,
  9377. 0xbc, 0xc9, 0xf6, 0x62
  9378. };
  9379. #endif /* WOLFSSL_AES_256 || WOLFSSL_AES_192 */
  9380. WOLFSSL_SMALL_STACK_STATIC const byte t1[] =
  9381. {
  9382. 0x76, 0xfc, 0x6e, 0xce, 0x0f, 0x4e, 0x17, 0x68,
  9383. 0xcd, 0xdf, 0x88, 0x53, 0xbb, 0x2d, 0x55, 0x1b
  9384. };
  9385. /* FIPS, QAT and PIC32MZ HW Crypto only support 12-byte IV */
  9386. #if !defined(HAVE_FIPS) && \
  9387. !defined(WOLFSSL_PIC32MZ_CRYPT) && \
  9388. !defined(FREESCALE_LTC) && !defined(FREESCALE_MMCAU) && \
  9389. !defined(WOLFSSL_XILINX_CRYPT) && !defined(WOLFSSL_AFALG_XILINX_AES) && \
  9390. !defined(WOLFSSL_SILABS_SE_ACCEL) && !defined(WOLFSSL_KCAPI_AES) && \
  9391. !(defined(WOLF_CRYPTO_CB) && \
  9392. (defined(HAVE_INTEL_QA_SYNC) || defined(HAVE_CAVIUM_OCTEON_SYNC)))
  9393. #define ENABLE_NON_12BYTE_IV_TEST
  9394. #ifdef WOLFSSL_AES_192
  9395. /* Test Case 12, uses same plaintext and AAD data. */
  9396. WOLFSSL_SMALL_STACK_STATIC const byte k2[] =
  9397. {
  9398. 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c,
  9399. 0x6d, 0x6a, 0x8f, 0x94, 0x67, 0x30, 0x83, 0x08,
  9400. 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c
  9401. };
  9402. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] =
  9403. {
  9404. 0x93, 0x13, 0x22, 0x5d, 0xf8, 0x84, 0x06, 0xe5,
  9405. 0x55, 0x90, 0x9c, 0x5a, 0xff, 0x52, 0x69, 0xaa,
  9406. 0x6a, 0x7a, 0x95, 0x38, 0x53, 0x4f, 0x7d, 0xa1,
  9407. 0xe4, 0xc3, 0x03, 0xd2, 0xa3, 0x18, 0xa7, 0x28,
  9408. 0xc3, 0xc0, 0xc9, 0x51, 0x56, 0x80, 0x95, 0x39,
  9409. 0xfc, 0xf0, 0xe2, 0x42, 0x9a, 0x6b, 0x52, 0x54,
  9410. 0x16, 0xae, 0xdb, 0xf5, 0xa0, 0xde, 0x6a, 0x57,
  9411. 0xa6, 0x37, 0xb3, 0x9b
  9412. };
  9413. WOLFSSL_SMALL_STACK_STATIC const byte c2[] =
  9414. {
  9415. 0xd2, 0x7e, 0x88, 0x68, 0x1c, 0xe3, 0x24, 0x3c,
  9416. 0x48, 0x30, 0x16, 0x5a, 0x8f, 0xdc, 0xf9, 0xff,
  9417. 0x1d, 0xe9, 0xa1, 0xd8, 0xe6, 0xb4, 0x47, 0xef,
  9418. 0x6e, 0xf7, 0xb7, 0x98, 0x28, 0x66, 0x6e, 0x45,
  9419. 0x81, 0xe7, 0x90, 0x12, 0xaf, 0x34, 0xdd, 0xd9,
  9420. 0xe2, 0xf0, 0x37, 0x58, 0x9b, 0x29, 0x2d, 0xb3,
  9421. 0xe6, 0x7c, 0x03, 0x67, 0x45, 0xfa, 0x22, 0xe7,
  9422. 0xe9, 0xb7, 0x37, 0x3b
  9423. };
  9424. WOLFSSL_SMALL_STACK_STATIC const byte t2[] =
  9425. {
  9426. 0xdc, 0xf5, 0x66, 0xff, 0x29, 0x1c, 0x25, 0xbb,
  9427. 0xb8, 0x56, 0x8f, 0xc3, 0xd3, 0x76, 0xa6, 0xd9
  9428. };
  9429. #endif /* WOLFSSL_AES_192 */
  9430. #ifdef WOLFSSL_AES_128
  9431. /* The following is an interesting test case from the example
  9432. * FIPS test vectors for AES-GCM. IVlen = 1 byte */
  9433. WOLFSSL_SMALL_STACK_STATIC const byte p3[] =
  9434. {
  9435. 0x57, 0xce, 0x45, 0x1f, 0xa5, 0xe2, 0x35, 0xa5,
  9436. 0x8e, 0x1a, 0xa2, 0x3b, 0x77, 0xcb, 0xaf, 0xe2
  9437. };
  9438. WOLFSSL_SMALL_STACK_STATIC const byte k3[] =
  9439. {
  9440. 0xbb, 0x01, 0xd7, 0x03, 0x81, 0x1c, 0x10, 0x1a,
  9441. 0x35, 0xe0, 0xff, 0xd2, 0x91, 0xba, 0xf2, 0x4b
  9442. };
  9443. WOLFSSL_SMALL_STACK_STATIC const byte iv3[] =
  9444. {
  9445. 0xca
  9446. };
  9447. WOLFSSL_SMALL_STACK_STATIC const byte c3[] =
  9448. {
  9449. 0x6b, 0x5f, 0xb3, 0x9d, 0xc1, 0xc5, 0x7a, 0x4f,
  9450. 0xf3, 0x51, 0x4d, 0xc2, 0xd5, 0xf0, 0xd0, 0x07
  9451. };
  9452. WOLFSSL_SMALL_STACK_STATIC const byte a3[] =
  9453. {
  9454. 0x40, 0xfc, 0xdc, 0xd7, 0x4a, 0xd7, 0x8b, 0xf1,
  9455. 0x3e, 0x7c, 0x60, 0x55, 0x50, 0x51, 0xdd, 0x54
  9456. };
  9457. WOLFSSL_SMALL_STACK_STATIC const byte t3[] =
  9458. {
  9459. 0x06, 0x90, 0xed, 0x01, 0x34, 0xdd, 0xc6, 0x95,
  9460. 0x31, 0x2e, 0x2a, 0xf9, 0x57, 0x7a, 0x1e, 0xa6
  9461. };
  9462. #endif /* WOLFSSL_AES_128 */
  9463. #ifdef WOLFSSL_AES_256
  9464. int ivlen;
  9465. #endif
  9466. #endif
  9467. byte resultT[sizeof(t1)];
  9468. byte resultP[sizeof(p) + AES_BLOCK_SIZE];
  9469. byte resultC[sizeof(p) + AES_BLOCK_SIZE];
  9470. int result = 0;
  9471. int ret;
  9472. #ifdef WOLFSSL_AES_256
  9473. int alen;
  9474. #if !defined(WOLFSSL_AFALG_XILINX_AES) && !defined(WOLFSSL_XILINX_CRYPT)
  9475. int plen;
  9476. #endif
  9477. #endif
  9478. #if !defined(BENCH_EMBEDDED) && !defined(HAVE_CAVIUM)
  9479. #if !defined(BENCH_AESGCM_LARGE)
  9480. #define BENCH_AESGCM_LARGE 1024
  9481. #endif
  9482. byte *large_input = (byte *)XMALLOC(BENCH_AESGCM_LARGE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9483. byte *large_output = (byte *)XMALLOC(BENCH_AESGCM_LARGE + AES_BLOCK_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9484. byte *large_outdec = (byte *)XMALLOC(BENCH_AESGCM_LARGE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9485. if ((! large_input) || (! large_output) || (! large_outdec))
  9486. ERROR_OUT(MEMORY_E, out);
  9487. XMEMSET(large_input, 0, BENCH_AESGCM_LARGE);
  9488. XMEMSET(large_output, 0, BENCH_AESGCM_LARGE + AES_BLOCK_SIZE);
  9489. XMEMSET(large_outdec, 0, BENCH_AESGCM_LARGE);
  9490. #endif
  9491. #ifdef WOLFSSL_SMALL_STACK
  9492. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  9493. ERROR_OUT(-6342, out);
  9494. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  9495. ERROR_OUT(-6343, out);
  9496. #endif
  9497. (void)result;
  9498. XMEMSET(resultT, 0, sizeof(resultT));
  9499. XMEMSET(resultC, 0, sizeof(resultC));
  9500. XMEMSET(resultP, 0, sizeof(resultP));
  9501. if (wc_AesInit(enc, HEAP_HINT, devId) != 0) {
  9502. ERROR_OUT(-6300, out);
  9503. }
  9504. if (wc_AesInit(dec, HEAP_HINT, devId) != 0) {
  9505. ERROR_OUT(-6301, out);
  9506. }
  9507. #ifdef WOLFSSL_AES_256
  9508. result = wc_AesGcmSetKey(enc, k1, sizeof(k1));
  9509. if (result != 0)
  9510. ERROR_OUT(-6302, out);
  9511. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  9512. result = wc_AesGcmEncrypt(enc, resultC, p, sizeof(p), iv1, sizeof(iv1),
  9513. resultT, sizeof(resultT), a, sizeof(a));
  9514. #if defined(WOLFSSL_ASYNC_CRYPT)
  9515. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  9516. #endif
  9517. if (result != 0)
  9518. ERROR_OUT(-6303, out);
  9519. if (XMEMCMP(c1, resultC, sizeof(c1)))
  9520. ERROR_OUT(-6304, out);
  9521. if (XMEMCMP(t1, resultT, sizeof(resultT)))
  9522. ERROR_OUT(-6305, out);
  9523. #ifdef HAVE_AES_DECRYPT
  9524. result = wc_AesGcmSetKey(dec, k1, sizeof(k1));
  9525. if (result != 0)
  9526. ERROR_OUT(-6306, out);
  9527. result = wc_AesGcmDecrypt(dec, resultP, resultC, sizeof(c1),
  9528. iv1, sizeof(iv1), resultT, sizeof(resultT), a, sizeof(a));
  9529. #if defined(WOLFSSL_ASYNC_CRYPT)
  9530. result = wc_AsyncWait(result, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  9531. #endif
  9532. if (result != 0)
  9533. ERROR_OUT(-6307, out);
  9534. if (XMEMCMP(p, resultP, sizeof(p)))
  9535. ERROR_OUT(-6308, out);
  9536. #endif /* HAVE_AES_DECRYPT */
  9537. /* Large buffer test */
  9538. #ifdef BENCH_AESGCM_LARGE
  9539. /* setup test buffer */
  9540. for (alen=0; alen<BENCH_AESGCM_LARGE; alen++)
  9541. large_input[alen] = (byte)alen;
  9542. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  9543. result = wc_AesGcmEncrypt(enc, large_output, large_input,
  9544. BENCH_AESGCM_LARGE, iv1, sizeof(iv1),
  9545. resultT, sizeof(resultT), a, sizeof(a));
  9546. #if defined(WOLFSSL_ASYNC_CRYPT)
  9547. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  9548. #endif
  9549. if (result != 0)
  9550. ERROR_OUT(-6309, out);
  9551. #ifdef HAVE_AES_DECRYPT
  9552. result = wc_AesGcmDecrypt(dec, large_outdec, large_output,
  9553. BENCH_AESGCM_LARGE, iv1, sizeof(iv1), resultT,
  9554. sizeof(resultT), a, sizeof(a));
  9555. #if defined(WOLFSSL_ASYNC_CRYPT)
  9556. result = wc_AsyncWait(result, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  9557. #endif
  9558. if (result != 0)
  9559. ERROR_OUT(-6310, out);
  9560. if (XMEMCMP(large_input, large_outdec, BENCH_AESGCM_LARGE))
  9561. ERROR_OUT(-6311, out);
  9562. #endif /* HAVE_AES_DECRYPT */
  9563. #endif /* BENCH_AESGCM_LARGE */
  9564. #if defined(ENABLE_NON_12BYTE_IV_TEST) && defined(WOLFSSL_AES_256)
  9565. /* Variable IV length test */
  9566. for (ivlen=1; ivlen<(int)sizeof(k1); ivlen++) {
  9567. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  9568. result = wc_AesGcmEncrypt(enc, resultC, p, sizeof(p), k1,
  9569. (word32)ivlen, resultT, sizeof(resultT), a, sizeof(a));
  9570. #if defined(WOLFSSL_ASYNC_CRYPT)
  9571. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  9572. #endif
  9573. if (result != 0)
  9574. ERROR_OUT(-6312, out);
  9575. #ifdef HAVE_AES_DECRYPT
  9576. result = wc_AesGcmDecrypt(dec, resultP, resultC, sizeof(c1), k1,
  9577. (word32)ivlen, resultT, sizeof(resultT), a, sizeof(a));
  9578. #if defined(WOLFSSL_ASYNC_CRYPT)
  9579. result = wc_AsyncWait(result, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  9580. #endif
  9581. if (result != 0)
  9582. ERROR_OUT(-6313, out);
  9583. #endif /* HAVE_AES_DECRYPT */
  9584. }
  9585. #endif
  9586. #if !(defined(WOLF_CRYPTO_CB) && defined(HAVE_INTEL_QA_SYNC))
  9587. /* Variable authenticated data length test */
  9588. for (alen=0; alen<(int)sizeof(p); alen++) {
  9589. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  9590. result = wc_AesGcmEncrypt(enc, resultC, p, sizeof(p), iv1,
  9591. sizeof(iv1), resultT, sizeof(resultT), p, (word32)alen);
  9592. #if defined(WOLFSSL_ASYNC_CRYPT)
  9593. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  9594. #endif
  9595. if (result != 0)
  9596. ERROR_OUT(-6314, out);
  9597. #ifdef HAVE_AES_DECRYPT
  9598. result = wc_AesGcmDecrypt(dec, resultP, resultC, sizeof(c1), iv1,
  9599. sizeof(iv1), resultT, sizeof(resultT), p, (word32)alen);
  9600. #if defined(WOLFSSL_ASYNC_CRYPT)
  9601. result = wc_AsyncWait(result, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  9602. #endif
  9603. if (result != 0)
  9604. ERROR_OUT(-6315, out);
  9605. #endif /* HAVE_AES_DECRYPT */
  9606. }
  9607. #endif
  9608. #if !defined(WOLFSSL_AFALG_XILINX_AES) && !defined(WOLFSSL_XILINX_CRYPT)
  9609. #ifdef BENCH_AESGCM_LARGE
  9610. /* Variable plain text length test */
  9611. for (plen=1; plen<BENCH_AESGCM_LARGE; plen++) {
  9612. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  9613. result = wc_AesGcmEncrypt(enc, large_output, large_input,
  9614. plen, iv1, sizeof(iv1), resultT,
  9615. sizeof(resultT), a, sizeof(a));
  9616. #if defined(WOLFSSL_ASYNC_CRYPT)
  9617. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  9618. #endif
  9619. if (result != 0)
  9620. ERROR_OUT(-6316, out);
  9621. #ifdef HAVE_AES_DECRYPT
  9622. result = wc_AesGcmDecrypt(dec, large_outdec, large_output,
  9623. plen, iv1, sizeof(iv1), resultT,
  9624. sizeof(resultT), a, sizeof(a));
  9625. #if defined(WOLFSSL_ASYNC_CRYPT)
  9626. result = wc_AsyncWait(result, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  9627. #endif
  9628. if (result != 0)
  9629. ERROR_OUT(-6317, out);
  9630. #endif /* HAVE_AES_DECRYPT */
  9631. }
  9632. #else /* BENCH_AESGCM_LARGE */
  9633. /* Variable plain text length test */
  9634. for (plen=1; plen<(int)sizeof(p); plen++) {
  9635. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  9636. result = wc_AesGcmEncrypt(enc, resultC, p, (word32)plen, iv1,
  9637. sizeof(iv1), resultT, sizeof(resultT), a, sizeof(a));
  9638. #if defined(WOLFSSL_ASYNC_CRYPT)
  9639. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  9640. #endif
  9641. if (result != 0)
  9642. ERROR_OUT(-6318, out);
  9643. #ifdef HAVE_AES_DECRYPT
  9644. result = wc_AesGcmDecrypt(dec, resultP, resultC, (word32)plen, iv1,
  9645. sizeof(iv1), resultT, sizeof(resultT), a, sizeof(a));
  9646. #if defined(WOLFSSL_ASYNC_CRYPT)
  9647. result = wc_AsyncWait(result, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  9648. #endif
  9649. if (result != 0)
  9650. ERROR_OUT(-6319, out);
  9651. #endif /* HAVE_AES_DECRYPT */
  9652. }
  9653. #endif /* BENCH_AESGCM_LARGE */
  9654. #endif
  9655. #endif /* WOLFSSL_AES_256 */
  9656. /* test with IV != 12 bytes */
  9657. #ifdef ENABLE_NON_12BYTE_IV_TEST
  9658. XMEMSET(resultT, 0, sizeof(resultT));
  9659. XMEMSET(resultC, 0, sizeof(resultC));
  9660. XMEMSET(resultP, 0, sizeof(resultP));
  9661. #ifdef WOLFSSL_AES_192
  9662. wc_AesGcmSetKey(enc, k2, sizeof(k2));
  9663. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  9664. result = wc_AesGcmEncrypt(enc, resultC, p, sizeof(p), iv2, sizeof(iv2),
  9665. resultT, sizeof(resultT), a, sizeof(a));
  9666. #if defined(WOLFSSL_ASYNC_CRYPT)
  9667. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  9668. #endif
  9669. if (result != 0)
  9670. ERROR_OUT(-6320, out);
  9671. if (XMEMCMP(c2, resultC, sizeof(c2)))
  9672. ERROR_OUT(-6321, out);
  9673. if (XMEMCMP(t2, resultT, sizeof(resultT)))
  9674. ERROR_OUT(-6322, out);
  9675. #ifdef HAVE_AES_DECRYPT
  9676. result = wc_AesGcmDecrypt(enc, resultP, resultC, sizeof(c1),
  9677. iv2, sizeof(iv2), resultT, sizeof(resultT), a, sizeof(a));
  9678. #if defined(WOLFSSL_ASYNC_CRYPT)
  9679. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  9680. #endif
  9681. if (result != 0)
  9682. ERROR_OUT(-6323, out);
  9683. if (XMEMCMP(p, resultP, sizeof(p)))
  9684. ERROR_OUT(-6324, out);
  9685. #endif /* HAVE_AES_DECRYPT */
  9686. XMEMSET(resultT, 0, sizeof(resultT));
  9687. XMEMSET(resultC, 0, sizeof(resultC));
  9688. XMEMSET(resultP, 0, sizeof(resultP));
  9689. #endif /* WOLFSSL_AES_192 */
  9690. #ifdef WOLFSSL_AES_128
  9691. wc_AesGcmSetKey(enc, k3, sizeof(k3));
  9692. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  9693. result = wc_AesGcmEncrypt(enc, resultC, p3, sizeof(p3), iv3, sizeof(iv3),
  9694. resultT, sizeof(t3), a3, sizeof(a3));
  9695. #if defined(WOLFSSL_ASYNC_CRYPT)
  9696. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  9697. #endif
  9698. if (result != 0)
  9699. ERROR_OUT(-6325, out);
  9700. if (XMEMCMP(c3, resultC, sizeof(c3)))
  9701. ERROR_OUT(-6326, out);
  9702. if (XMEMCMP(t3, resultT, sizeof(t3)))
  9703. ERROR_OUT(-6327, out);
  9704. #ifdef HAVE_AES_DECRYPT
  9705. result = wc_AesGcmDecrypt(enc, resultP, resultC, sizeof(c3),
  9706. iv3, sizeof(iv3), resultT, sizeof(t3), a3, sizeof(a3));
  9707. #if defined(WOLFSSL_ASYNC_CRYPT)
  9708. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  9709. #endif
  9710. if (result != 0)
  9711. ERROR_OUT(-6328, out);
  9712. if (XMEMCMP(p3, resultP, sizeof(p3)))
  9713. ERROR_OUT(-6329, out);
  9714. #endif /* HAVE_AES_DECRYPT */
  9715. #endif /* WOLFSSL_AES_128 */
  9716. #endif /* ENABLE_NON_12BYTE_IV_TEST */
  9717. #if defined(WOLFSSL_AES_256) && !defined(WOLFSSL_AFALG_XILINX_AES) && \
  9718. !defined(WOLFSSL_XILINX_CRYPT) && \
  9719. !(defined(WOLF_CRYPTO_CB) && \
  9720. defined(HAVE_INTEL_QA_SYNC) || defined(HAVE_CAVIUM_OCTEON_SYNC))
  9721. XMEMSET(resultT, 0, sizeof(resultT));
  9722. XMEMSET(resultC, 0, sizeof(resultC));
  9723. XMEMSET(resultP, 0, sizeof(resultP));
  9724. wc_AesGcmSetKey(enc, k1, sizeof(k1));
  9725. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  9726. result = wc_AesGcmEncrypt(enc, resultC, p, sizeof(p), iv1, sizeof(iv1),
  9727. resultT + 1, sizeof(resultT) - 1, a, sizeof(a));
  9728. #if defined(WOLFSSL_ASYNC_CRYPT)
  9729. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  9730. #endif
  9731. if (result != 0)
  9732. ERROR_OUT(-6330, out);
  9733. if (XMEMCMP(c1, resultC, sizeof(c1)))
  9734. ERROR_OUT(-6331, out);
  9735. if (XMEMCMP(t1, resultT + 1, sizeof(resultT) - 1))
  9736. ERROR_OUT(-6332, out);
  9737. #ifdef HAVE_AES_DECRYPT
  9738. result = wc_AesGcmDecrypt(enc, resultP, resultC, sizeof(p),
  9739. iv1, sizeof(iv1), resultT + 1, sizeof(resultT) - 1, a, sizeof(a));
  9740. #if defined(WOLFSSL_ASYNC_CRYPT)
  9741. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  9742. #endif
  9743. if (result != 0)
  9744. ERROR_OUT(-6333, out);
  9745. if (XMEMCMP(p, resultP, sizeof(p)))
  9746. ERROR_OUT(-6334, out);
  9747. #endif /* HAVE_AES_DECRYPT */
  9748. #endif /* WOLFSSL_AES_256 */
  9749. #if !defined(HAVE_FIPS) || \
  9750. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))
  9751. /* Test encrypt with internally generated IV */
  9752. #if defined(WOLFSSL_AES_256) && !(defined(WC_NO_RNG) || defined(HAVE_SELFTEST)) \
  9753. && !(defined(WOLF_CRYPTO_CB) && defined(HAVE_CAVIUM_OCTEON_SYNC))
  9754. {
  9755. WC_RNG rng;
  9756. byte randIV[12];
  9757. result = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  9758. if (result != 0)
  9759. ERROR_OUT(-6335, out);
  9760. XMEMSET(randIV, 0, sizeof(randIV));
  9761. XMEMSET(resultT, 0, sizeof(resultT));
  9762. XMEMSET(resultC, 0, sizeof(resultC));
  9763. XMEMSET(resultP, 0, sizeof(resultP));
  9764. wc_AesGcmSetKey(enc, k1, sizeof(k1));
  9765. result = wc_AesGcmSetIV(enc, sizeof(randIV), NULL, 0, &rng);
  9766. if (result != 0)
  9767. ERROR_OUT(-6336, out);
  9768. result = wc_AesGcmEncrypt_ex(enc,
  9769. resultC, p, sizeof(p),
  9770. randIV, sizeof(randIV),
  9771. resultT, sizeof(resultT),
  9772. a, sizeof(a));
  9773. #if defined(WOLFSSL_ASYNC_CRYPT)
  9774. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  9775. #endif
  9776. if (result != 0)
  9777. ERROR_OUT(-6337, out);
  9778. /* Check the IV has been set. */
  9779. {
  9780. word32 i, ivSum = 0;
  9781. for (i = 0; i < sizeof(randIV); i++)
  9782. ivSum += randIV[i];
  9783. if (ivSum == 0)
  9784. ERROR_OUT(-6338, out);
  9785. }
  9786. #ifdef HAVE_AES_DECRYPT
  9787. wc_AesGcmSetKey(dec, k1, sizeof(k1));
  9788. result = wc_AesGcmSetIV(dec, sizeof(randIV), NULL, 0, &rng);
  9789. if (result != 0)
  9790. ERROR_OUT(-6339, out);
  9791. result = wc_AesGcmDecrypt(dec,
  9792. resultP, resultC, sizeof(c1),
  9793. randIV, sizeof(randIV),
  9794. resultT, sizeof(resultT),
  9795. a, sizeof(a));
  9796. #if defined(WOLFSSL_ASYNC_CRYPT)
  9797. result = wc_AsyncWait(result, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  9798. #endif
  9799. if (result != 0)
  9800. ERROR_OUT(-6340, out);
  9801. if (XMEMCMP(p, resultP, sizeof(p)))
  9802. ERROR_OUT(-6341, out);
  9803. #endif /* HAVE_AES_DECRYPT */
  9804. wc_FreeRng(&rng);
  9805. }
  9806. #endif /* WOLFSSL_AES_256 && !(WC_NO_RNG || HAVE_SELFTEST) */
  9807. #endif /* HAVE_FIPS_VERSION >= 2 */
  9808. #if !defined(WOLFSSL_AFALG_XILINX_AES) && !defined(WOLFSSL_XILINX_CRYPT)
  9809. #ifdef WOLFSSL_AES_256
  9810. #ifdef WOLFSSL_AESGCM_STREAM
  9811. result = wc_AesGcmEncryptInit(enc, k1, sizeof(k1), iv1, sizeof(iv1));
  9812. if (result != 0)
  9813. ERROR_OUT(-6360, out);
  9814. result = wc_AesGcmEncryptUpdate(enc, resultC, p, sizeof(p), a, sizeof(a));
  9815. if (result != 0)
  9816. ERROR_OUT(-6361, out);
  9817. result = wc_AesGcmEncryptFinal(enc, resultT, sizeof(resultT));
  9818. if (result != 0)
  9819. ERROR_OUT(-6362, out);
  9820. if (XMEMCMP(resultC, c1, sizeof(c1)) != 0)
  9821. ERROR_OUT(-6363, out);
  9822. if (XMEMCMP(resultT, t1, sizeof(t1)) != 0)
  9823. ERROR_OUT(-6364, out);
  9824. #ifdef HAVE_AES_DECRYPT
  9825. result = wc_AesGcmDecryptInit(enc, k1, sizeof(k1), iv1, sizeof(iv1));
  9826. if (result != 0)
  9827. ERROR_OUT(-6370, out);
  9828. result = wc_AesGcmDecryptUpdate(enc, resultP, c1, sizeof(c1), a, sizeof(a));
  9829. if (result != 0)
  9830. ERROR_OUT(-6371, out);
  9831. result = wc_AesGcmDecryptFinal(enc, t1, sizeof(t1));
  9832. if (result != 0)
  9833. ERROR_OUT(-6372, out);
  9834. if (XMEMCMP(resultP, p, sizeof(p)) != 0)
  9835. ERROR_OUT(-6373, out);
  9836. #endif
  9837. /* alen is the size to pass in with each update. */
  9838. for (alen = 1; alen < AES_BLOCK_SIZE + 1; alen++) {
  9839. result = wc_AesGcmEncryptInit(enc, k1, sizeof(k1), iv1, sizeof(iv1));
  9840. if (result != 0)
  9841. ERROR_OUT(-6380, out);
  9842. /* plen is the offset into AAD to update with. */
  9843. for (plen = 0; plen < (int)sizeof(a); plen += alen) {
  9844. int len = sizeof(a) - plen;
  9845. if (len > alen) len = alen;
  9846. result = wc_AesGcmEncryptUpdate(enc, NULL, NULL, 0, a + plen, len);
  9847. if (result != 0)
  9848. ERROR_OUT(-6381, out);
  9849. }
  9850. /* plen is the offset into plaintext to update with. */
  9851. for (plen = 0; plen < (int)sizeof(p); plen += alen) {
  9852. int len = sizeof(p) - plen;
  9853. if (len > alen) len = alen;
  9854. result = wc_AesGcmEncryptUpdate(enc, resultC + plen, p + plen, len,
  9855. NULL, 0);
  9856. if (result != 0)
  9857. ERROR_OUT(-6382, out);
  9858. }
  9859. result = wc_AesGcmEncryptFinal(enc, resultT, sizeof(resultT));
  9860. if (result != 0)
  9861. ERROR_OUT(-6383, out);
  9862. if (XMEMCMP(resultC, c1, sizeof(c1)) != 0)
  9863. ERROR_OUT(-6384, out);
  9864. if (XMEMCMP(resultT, t1, sizeof(t1)) != 0)
  9865. ERROR_OUT(-6385, out);
  9866. }
  9867. #ifdef HAVE_AES_DECRYPT
  9868. for (alen = 1; alen < AES_BLOCK_SIZE + 1; alen++) {
  9869. result = wc_AesGcmDecryptInit(enc, k1, sizeof(k1), iv1, sizeof(iv1));
  9870. if (result != 0)
  9871. ERROR_OUT(-6390, out);
  9872. /* plen is the offset into AAD to update with. */
  9873. for (plen = 0; plen < (int)sizeof(a); plen += alen) {
  9874. int len = sizeof(a) - plen;
  9875. if (len > alen) len = alen;
  9876. result = wc_AesGcmDecryptUpdate(enc, NULL, NULL, 0, a + plen, len);
  9877. if (result != 0)
  9878. ERROR_OUT(-6391, out);
  9879. }
  9880. /* plen is the offset into cipher text to update with. */
  9881. for (plen = 0; plen < (int)sizeof(c1); plen += alen) {
  9882. int len = sizeof(c1) - plen;
  9883. if (len > alen) len = alen;
  9884. result = wc_AesGcmDecryptUpdate(enc, resultP + plen, c1 + plen, len,
  9885. NULL, 0);
  9886. if (result != 0)
  9887. ERROR_OUT(-6392, out);
  9888. }
  9889. result = wc_AesGcmDecryptFinal(enc, t1, sizeof(t1));
  9890. if (result != 0)
  9891. ERROR_OUT(-6393, out);
  9892. if (XMEMCMP(resultP, p, sizeof(p)) != 0)
  9893. ERROR_OUT(-6394, out);
  9894. }
  9895. #endif /* HAVE_AES_DECRYPT */
  9896. #endif /* WOLFSSL_AESGCM_STREAM */
  9897. #endif /* WOLFSSL_AES_256 */
  9898. #endif /* !WOLFSSL_AFALG_XILINX_AES && !WOLFSSL_XILINX_CRYPT */
  9899. wc_AesFree(enc);
  9900. wc_AesFree(dec);
  9901. ret = 0;
  9902. out:
  9903. #if !defined(BENCH_EMBEDDED) && !defined(HAVE_CAVIUM)
  9904. if (large_input)
  9905. XFREE(large_input, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9906. if (large_output)
  9907. XFREE(large_output, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9908. if (large_outdec)
  9909. XFREE(large_outdec, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9910. #endif
  9911. #ifdef WOLFSSL_SMALL_STACK
  9912. if (enc)
  9913. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  9914. if (dec)
  9915. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  9916. #endif
  9917. return ret;
  9918. }
  9919. #ifdef WOLFSSL_AES_128
  9920. WOLFSSL_TEST_SUBROUTINE int gmac_test(void)
  9921. {
  9922. int ret;
  9923. #ifdef WOLFSSL_SMALL_STACK
  9924. Gmac *gmac;
  9925. #else
  9926. Gmac gmac[1];
  9927. #endif
  9928. WOLFSSL_SMALL_STACK_STATIC const byte k1[] =
  9929. {
  9930. 0x89, 0xc9, 0x49, 0xe9, 0xc8, 0x04, 0xaf, 0x01,
  9931. 0x4d, 0x56, 0x04, 0xb3, 0x94, 0x59, 0xf2, 0xc8
  9932. };
  9933. WOLFSSL_SMALL_STACK_STATIC const byte iv1[] =
  9934. {
  9935. 0xd1, 0xb1, 0x04, 0xc8, 0x15, 0xbf, 0x1e, 0x94,
  9936. 0xe2, 0x8c, 0x8f, 0x16
  9937. };
  9938. WOLFSSL_SMALL_STACK_STATIC const byte a1[] =
  9939. {
  9940. 0x82, 0xad, 0xcd, 0x63, 0x8d, 0x3f, 0xa9, 0xd9,
  9941. 0xf3, 0xe8, 0x41, 0x00, 0xd6, 0x1e, 0x07, 0x77
  9942. };
  9943. WOLFSSL_SMALL_STACK_STATIC const byte t1[] =
  9944. {
  9945. 0x88, 0xdb, 0x9d, 0x62, 0x17, 0x2e, 0xd0, 0x43,
  9946. 0xaa, 0x10, 0xf1, 0x6d, 0x22, 0x7d, 0xc4, 0x1b
  9947. };
  9948. #if (!defined(HAVE_FIPS) || \
  9949. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)))
  9950. /* FIPS builds only allow 16-byte auth tags. */
  9951. /* This sample uses a 15-byte auth tag. */
  9952. WOLFSSL_SMALL_STACK_STATIC const byte k2[] =
  9953. {
  9954. 0x40, 0xf7, 0xec, 0xb2, 0x52, 0x6d, 0xaa, 0xd4,
  9955. 0x74, 0x25, 0x1d, 0xf4, 0x88, 0x9e, 0xf6, 0x5b
  9956. };
  9957. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] =
  9958. {
  9959. 0xee, 0x9c, 0x6e, 0x06, 0x15, 0x45, 0x45, 0x03,
  9960. 0x1a, 0x60, 0x24, 0xa7
  9961. };
  9962. WOLFSSL_SMALL_STACK_STATIC const byte a2[] =
  9963. {
  9964. 0x94, 0x81, 0x2c, 0x87, 0x07, 0x4e, 0x15, 0x18,
  9965. 0x34, 0xb8, 0x35, 0xaf, 0x1c, 0xa5, 0x7e, 0x56
  9966. };
  9967. WOLFSSL_SMALL_STACK_STATIC const byte t2[] =
  9968. {
  9969. 0xc6, 0x81, 0x79, 0x8e, 0x3d, 0xda, 0xb0, 0x9f,
  9970. 0x8d, 0x83, 0xb0, 0xbb, 0x14, 0xb6, 0x91
  9971. };
  9972. #endif
  9973. byte tag[16];
  9974. #ifdef WOLFSSL_SMALL_STACK
  9975. if ((gmac = (Gmac *)XMALLOC(sizeof *gmac, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  9976. return -6409;
  9977. #endif
  9978. XMEMSET(gmac, 0, sizeof *gmac); /* clear context */
  9979. (void)wc_AesInit(&gmac->aes, HEAP_HINT, INVALID_DEVID); /* Make sure devId updated */
  9980. XMEMSET(tag, 0, sizeof(tag));
  9981. wc_GmacSetKey(gmac, k1, sizeof(k1));
  9982. wc_GmacUpdate(gmac, iv1, sizeof(iv1), a1, sizeof(a1), tag, sizeof(t1));
  9983. if (XMEMCMP(t1, tag, sizeof(t1)) != 0)
  9984. ERROR_OUT(-6400, out);
  9985. #if (!defined(HAVE_FIPS) || \
  9986. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)) )
  9987. XMEMSET(tag, 0, sizeof(tag));
  9988. wc_GmacSetKey(gmac, k2, sizeof(k2));
  9989. wc_GmacUpdate(gmac, iv2, sizeof(iv2), a2, sizeof(a2), tag, sizeof(t2));
  9990. if (XMEMCMP(t2, tag, sizeof(t2)) != 0)
  9991. ERROR_OUT(-6401, out);
  9992. #if !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && !defined(NO_AES_DECRYPT)
  9993. {
  9994. WOLFSSL_SMALL_STACK_STATIC const byte badT[] =
  9995. {
  9996. 0xde, 0xad, 0xbe, 0xef, 0x17, 0x2e, 0xd0, 0x43,
  9997. 0xaa, 0x10, 0xf1, 0x6d, 0x22, 0x7d, 0xc4, 0x1b
  9998. };
  9999. WC_RNG rng;
  10000. byte iv[12];
  10001. #ifndef HAVE_FIPS
  10002. if (wc_InitRng_ex(&rng, HEAP_HINT, devId) != 0)
  10003. ERROR_OUT(-6402, out);
  10004. #else
  10005. if (wc_InitRng(&rng) != 0)
  10006. ERROR_OUT(-6403, out);
  10007. #endif
  10008. if (wc_GmacVerify(k1, sizeof(k1), iv1, sizeof(iv1), a1, sizeof(a1),
  10009. t1, sizeof(t1)) != 0)
  10010. ERROR_OUT(-6404, out);
  10011. if (wc_GmacVerify(k1, sizeof(k1), iv1, sizeof(iv1), a1, sizeof(a1),
  10012. badT, sizeof(badT)) != AES_GCM_AUTH_E)
  10013. ERROR_OUT(-6405, out);
  10014. if (wc_GmacVerify(k2, sizeof(k2), iv2, sizeof(iv2), a2, sizeof(a2),
  10015. t2, sizeof(t2)) != 0)
  10016. ERROR_OUT(-6406, out);
  10017. XMEMSET(tag, 0, sizeof(tag));
  10018. XMEMSET(iv, 0, sizeof(iv));
  10019. if (wc_Gmac(k1, sizeof(k1), iv, sizeof(iv), a1, sizeof(a1),
  10020. tag, sizeof(tag), &rng) != 0)
  10021. ERROR_OUT(-6407, out);
  10022. if (wc_GmacVerify(k1, sizeof(k1), iv, sizeof(iv), a1, sizeof(a1),
  10023. tag, sizeof(tag)) != 0)
  10024. ERROR_OUT(-6408, out);
  10025. wc_FreeRng(&rng);
  10026. }
  10027. #endif /* !WC_NO_RNG && !HAVE_SELFTEST && !NO_AES_DECRYPT */
  10028. #endif /* HAVE_FIPS */
  10029. ret = 0;
  10030. out:
  10031. wc_AesFree(&gmac->aes);
  10032. #ifdef WOLFSSL_SMALL_STACK
  10033. XFREE(gmac, HEAP_HINT, DYNAMIC_TYPE_AES);
  10034. #endif
  10035. return ret;
  10036. }
  10037. #endif /* WOLFSSL_AES_128 */
  10038. #endif /* HAVE_AESGCM */
  10039. #if defined(HAVE_AESCCM) && defined(WOLFSSL_AES_128)
  10040. WOLFSSL_TEST_SUBROUTINE int aesccm_test(void)
  10041. {
  10042. int ret;
  10043. #ifdef WOLFSSL_SMALL_STACK
  10044. Aes *enc;
  10045. #else
  10046. Aes enc[1];
  10047. #endif
  10048. /* key */
  10049. WOLFSSL_SMALL_STACK_STATIC const byte k[] =
  10050. {
  10051. 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
  10052. 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf
  10053. };
  10054. /* nonce */
  10055. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  10056. {
  10057. 0x00, 0x00, 0x00, 0x03, 0x02, 0x01, 0x00, 0xa0,
  10058. 0xa1, 0xa2, 0xa3, 0xa4, 0xa5
  10059. };
  10060. /* plaintext */
  10061. WOLFSSL_SMALL_STACK_STATIC const byte p[] =
  10062. {
  10063. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  10064. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  10065. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e
  10066. };
  10067. /* plaintext - long */
  10068. WOLFSSL_SMALL_STACK_STATIC const byte pl[] =
  10069. {
  10070. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  10071. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  10072. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
  10073. 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  10074. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
  10075. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  10076. 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
  10077. 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
  10078. 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
  10079. 0x50
  10080. };
  10081. WOLFSSL_SMALL_STACK_STATIC const byte a[] =
  10082. {
  10083. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
  10084. };
  10085. /* ciphertext */
  10086. WOLFSSL_SMALL_STACK_STATIC const byte c[] =
  10087. {
  10088. 0x58, 0x8c, 0x97, 0x9a, 0x61, 0xc6, 0x63, 0xd2,
  10089. 0xf0, 0x66, 0xd0, 0xc2, 0xc0, 0xf9, 0x89, 0x80,
  10090. 0x6d, 0x5f, 0x6b, 0x61, 0xda, 0xc3, 0x84
  10091. };
  10092. /* tag - authentication */
  10093. WOLFSSL_SMALL_STACK_STATIC const byte t[] =
  10094. {
  10095. 0x17, 0xe8, 0xd1, 0x2c, 0xfd, 0xf9, 0x26, 0xe0
  10096. };
  10097. /* ciphertext - long */
  10098. WOLFSSL_SMALL_STACK_STATIC const byte cl[] =
  10099. {
  10100. 0x58, 0x8c, 0x97, 0x9a, 0x61, 0xc6, 0x63, 0xd2,
  10101. 0xf0, 0x66, 0xd0, 0xc2, 0xc0, 0xf9, 0x89, 0x80,
  10102. 0x6d, 0x5f, 0x6b, 0x61, 0xda, 0xc3, 0x84, 0xe0,
  10103. 0x44, 0x2d, 0xbe, 0x25, 0xfa, 0x48, 0x2b, 0xa8,
  10104. 0x36, 0x0b, 0xbf, 0x01, 0xc0, 0x12, 0x45, 0xa4,
  10105. 0x82, 0x9f, 0x20, 0x6c, 0xc3, 0xd6, 0xae, 0x5b,
  10106. 0x54, 0x8d, 0xd0, 0xb1, 0x69, 0x2c, 0xec, 0x5e,
  10107. 0x95, 0xa5, 0x6b, 0x48, 0xc3, 0xc6, 0xc8, 0x9e,
  10108. 0xc7, 0x92, 0x98, 0x9d, 0x26, 0x7d, 0x2a, 0x10,
  10109. 0x0b
  10110. };
  10111. /* tag - authentication - long */
  10112. WOLFSSL_SMALL_STACK_STATIC const byte tl[] =
  10113. {
  10114. 0x89, 0xd8, 0xd2, 0x02, 0xc5, 0xcf, 0xae, 0xf4
  10115. };
  10116. /* tag - authentication - empty plaintext */
  10117. WOLFSSL_SMALL_STACK_STATIC const byte t_empty[] =
  10118. {
  10119. 0xe4, 0x28, 0x8a, 0xc3, 0x78, 0x00, 0x0f, 0xf5
  10120. };
  10121. byte t2[sizeof(t)];
  10122. byte p2[sizeof(p)];
  10123. byte c2[sizeof(c)];
  10124. byte iv2[sizeof(iv)];
  10125. byte pl2[sizeof(pl)];
  10126. byte cl2[sizeof(cl)];
  10127. byte tl2[sizeof(tl)];
  10128. byte t_empty2[sizeof(t_empty)];
  10129. int result;
  10130. #ifdef WOLFSSL_SMALL_STACK
  10131. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  10132. return -6521;
  10133. #endif
  10134. XMEMSET(enc, 0, sizeof *enc); /* clear context */
  10135. XMEMSET(t2, 0, sizeof(t2));
  10136. XMEMSET(c2, 0, sizeof(c2));
  10137. XMEMSET(p2, 0, sizeof(p2));
  10138. result = wc_AesInit(enc, HEAP_HINT, devId);
  10139. if (result != 0)
  10140. ERROR_OUT(-6499, out);
  10141. result = wc_AesCcmSetKey(enc, k, sizeof(k));
  10142. if (result != 0)
  10143. ERROR_OUT(-6500, out);
  10144. /* AES-CCM encrypt and decrypt both use AES encrypt internally */
  10145. result = wc_AesCcmEncrypt(enc, c2, p, sizeof(c2), iv, sizeof(iv),
  10146. t2, sizeof(t2), a, sizeof(a));
  10147. if (result != 0)
  10148. ERROR_OUT(-6501, out);
  10149. if (XMEMCMP(c, c2, sizeof(c2)))
  10150. ERROR_OUT(-6502, out);
  10151. if (XMEMCMP(t, t2, sizeof(t2)))
  10152. ERROR_OUT(-6503, out);
  10153. result = wc_AesCcmDecrypt(enc, p2, c2, sizeof(p2), iv, sizeof(iv),
  10154. t2, sizeof(t2), a, sizeof(a));
  10155. if (result != 0)
  10156. ERROR_OUT(-6504, out);
  10157. if (XMEMCMP(p, p2, sizeof(p2)))
  10158. ERROR_OUT(-6505, out);
  10159. /* Test the authentication failure */
  10160. t2[0]++; /* Corrupt the authentication tag. */
  10161. result = wc_AesCcmDecrypt(enc, p2, c, sizeof(p2), iv, sizeof(iv),
  10162. t2, sizeof(t2), a, sizeof(a));
  10163. if (result == 0)
  10164. ERROR_OUT(-6506, out);
  10165. /* Clear c2 to compare against p2. p2 should be set to zero in case of
  10166. * authentication fail. */
  10167. XMEMSET(c2, 0, sizeof(c2));
  10168. if (XMEMCMP(p2, c2, sizeof(p2)))
  10169. ERROR_OUT(-6507, out);
  10170. wc_AesFree(enc);
  10171. XMEMSET(enc, 0, sizeof(Aes)); /* clear context */
  10172. XMEMSET(t2, 0, sizeof(t2));
  10173. XMEMSET(c2, 0, sizeof(c2));
  10174. XMEMSET(p2, 0, sizeof(p2));
  10175. XMEMSET(iv2, 0, sizeof(iv2));
  10176. #ifndef HAVE_SELFTEST
  10177. /* selftest build does not have wc_AesCcmSetNonce() or
  10178. * wc_AesCcmEncrypt_ex() */
  10179. if (wc_AesCcmSetKey(enc, k, sizeof(k)) != 0)
  10180. ERROR_OUT(-6508, out);
  10181. if (wc_AesCcmSetNonce(enc, iv, sizeof(iv)) != 0)
  10182. ERROR_OUT(-6509, out);
  10183. if (wc_AesCcmEncrypt_ex(enc, c2, p, sizeof(c2), iv2, sizeof(iv2),
  10184. t2, sizeof(t2), a, sizeof(a)) != 0)
  10185. ERROR_OUT(-6510, out);
  10186. if (XMEMCMP(iv, iv2, sizeof(iv2)))
  10187. ERROR_OUT(-6511, out);
  10188. if (XMEMCMP(c, c2, sizeof(c2)))
  10189. ERROR_OUT(-6512, out);
  10190. if (XMEMCMP(t, t2, sizeof(t2)))
  10191. ERROR_OUT(-6513, out);
  10192. #endif
  10193. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  10194. /* test fail on invalid IV sizes */
  10195. result = wc_AesCcmSetKey(enc, k, sizeof(k));
  10196. if (result != 0)
  10197. ERROR_OUT(-6514, out);
  10198. /* AES-CCM encrypt and decrypt both use AES encrypt internally */
  10199. result = wc_AesCcmEncrypt(enc, c2, p, sizeof(c2), iv, sizeof(iv),
  10200. t2, 1, a, sizeof(a));
  10201. if (result == 0) {
  10202. ERROR_OUT(-6515, out);
  10203. }
  10204. #endif
  10205. /* AES-CCM encrypt and decrypt both use AES encrypt internally */
  10206. result = wc_AesCcmEncrypt(enc, cl2, pl, sizeof(cl2), iv, sizeof(iv),
  10207. tl2, sizeof(tl2), a, sizeof(a));
  10208. if (result != 0)
  10209. ERROR_OUT(-6516, out);
  10210. if (XMEMCMP(cl, cl2, sizeof(cl2)))
  10211. ERROR_OUT(-6517, out);
  10212. if (XMEMCMP(tl, tl2, sizeof(tl2)))
  10213. ERROR_OUT(-6518, out);
  10214. result = wc_AesCcmDecrypt(enc, pl2, cl2, sizeof(pl2), iv, sizeof(iv),
  10215. tl2, sizeof(tl2), a, sizeof(a));
  10216. if (result != 0)
  10217. ERROR_OUT(-6519, out);
  10218. if (XMEMCMP(pl, pl2, sizeof(pl2)))
  10219. ERROR_OUT(-6520, out);
  10220. /* test empty message as null input or output with nonzero inSz. */
  10221. result = wc_AesCcmEncrypt(enc, pl2 /* out */, NULL /* in */, 1 /* inSz */,
  10222. iv, sizeof(iv), t_empty2, sizeof(t_empty2),
  10223. a, sizeof(a));
  10224. if (result != BAD_FUNC_ARG)
  10225. ERROR_OUT(-6527, out);
  10226. result = wc_AesCcmEncrypt(enc, NULL /* out */, (const byte *)"" /* in */, 1 /* inSz */,
  10227. iv, sizeof(iv), t_empty2, sizeof(t_empty2),
  10228. a, sizeof(a));
  10229. if (result != BAD_FUNC_ARG)
  10230. ERROR_OUT(-6528, out);
  10231. result = wc_AesCcmDecrypt(enc, pl2, NULL /* in */, 1 /* inSz */,
  10232. iv, sizeof(iv), t_empty2, sizeof(t_empty2), a,
  10233. sizeof(a));
  10234. if (result != BAD_FUNC_ARG)
  10235. ERROR_OUT(-6529, out);
  10236. result = wc_AesCcmDecrypt(enc, NULL /* out */, (const byte *)"" /* in */, 1 /* inSz */,
  10237. iv, sizeof(iv), t_empty2, sizeof(t_empty2), a,
  10238. sizeof(a));
  10239. if (result != BAD_FUNC_ARG)
  10240. ERROR_OUT(-6530, out);
  10241. /* test empty message as null input and output with zero inSz --
  10242. * must either succeed, or fail early with BAD_FUNC_ARG.
  10243. */
  10244. result = wc_AesCcmEncrypt(enc, NULL /* out */, NULL /* in */, 0 /* inSz */,
  10245. iv, sizeof(iv), t_empty2, sizeof(t_empty2),
  10246. a, sizeof(a));
  10247. if (result != BAD_FUNC_ARG) {
  10248. if (result != 0)
  10249. ERROR_OUT(-6521, out);
  10250. if (XMEMCMP(t_empty, t_empty2, sizeof(t_empty2)))
  10251. ERROR_OUT(-6522, out);
  10252. result = wc_AesCcmDecrypt(enc, NULL /* out */, NULL /* in */,
  10253. 0 /* inSz */, iv, sizeof(iv), t_empty2,
  10254. sizeof(t_empty2), a, sizeof(a));
  10255. if (result != 0)
  10256. ERROR_OUT(-6523, out);
  10257. }
  10258. /* test empty message as zero-length string -- must work. */
  10259. result = wc_AesCcmEncrypt(enc, pl2, (const byte *)"", 0 /* inSz */, iv,
  10260. sizeof(iv), t_empty2, sizeof(t_empty2), a,
  10261. sizeof(a));
  10262. if (result != 0)
  10263. ERROR_OUT(-6524, out);
  10264. if (XMEMCMP(t_empty, t_empty2, sizeof(t_empty2)))
  10265. ERROR_OUT(-6525, out);
  10266. result = wc_AesCcmDecrypt(enc, pl2, (const byte *)"", 0 /* inSz */,
  10267. iv, sizeof(iv), t_empty2, sizeof(t_empty2), a,
  10268. sizeof(a));
  10269. if (result != 0)
  10270. ERROR_OUT(-6526, out);
  10271. wc_AesFree(enc);
  10272. ret = 0;
  10273. out:
  10274. #ifdef WOLFSSL_SMALL_STACK
  10275. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  10276. #endif
  10277. return ret;
  10278. }
  10279. #endif /* HAVE_AESCCM WOLFSSL_AES_128 */
  10280. #ifdef HAVE_AES_KEYWRAP
  10281. #define MAX_KEYWRAP_TEST_OUTLEN 40
  10282. #define MAX_KEYWRAP_TEST_PLAINLEN 32
  10283. typedef struct keywrapVector {
  10284. const byte* kek;
  10285. const byte* data;
  10286. const byte* verify;
  10287. word32 kekLen;
  10288. word32 dataLen;
  10289. word32 verifyLen;
  10290. } keywrapVector;
  10291. WOLFSSL_TEST_SUBROUTINE int aeskeywrap_test(void)
  10292. {
  10293. int wrapSz, plainSz, testSz, i;
  10294. /* test vectors from RFC 3394 (kek, data, verify) */
  10295. #ifdef WOLFSSL_AES_128
  10296. /* Wrap 128 bits of Key Data with a 128-bit KEK */
  10297. WOLFSSL_SMALL_STACK_STATIC const byte k1[] = {
  10298. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  10299. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  10300. };
  10301. WOLFSSL_SMALL_STACK_STATIC const byte d1[] = {
  10302. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  10303. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF
  10304. };
  10305. WOLFSSL_SMALL_STACK_STATIC const byte v1[] = {
  10306. 0x1F, 0xA6, 0x8B, 0x0A, 0x81, 0x12, 0xB4, 0x47,
  10307. 0xAE, 0xF3, 0x4B, 0xD8, 0xFB, 0x5A, 0x7B, 0x82,
  10308. 0x9D, 0x3E, 0x86, 0x23, 0x71, 0xD2, 0xCF, 0xE5
  10309. };
  10310. #endif /* WOLFSSL_AES_128 */
  10311. #ifdef WOLFSSL_AES_192
  10312. /* Wrap 128 bits of Key Data with a 192-bit KEK */
  10313. WOLFSSL_SMALL_STACK_STATIC const byte k2[] = {
  10314. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  10315. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  10316. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17
  10317. };
  10318. WOLFSSL_SMALL_STACK_STATIC const byte d2[] = {
  10319. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  10320. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF
  10321. };
  10322. WOLFSSL_SMALL_STACK_STATIC const byte v2[] = {
  10323. 0x96, 0x77, 0x8B, 0x25, 0xAE, 0x6C, 0xA4, 0x35,
  10324. 0xF9, 0x2B, 0x5B, 0x97, 0xC0, 0x50, 0xAE, 0xD2,
  10325. 0x46, 0x8A, 0xB8, 0xA1, 0x7A, 0xD8, 0x4E, 0x5D
  10326. };
  10327. #endif
  10328. #ifdef WOLFSSL_AES_256
  10329. /* Wrap 128 bits of Key Data with a 256-bit KEK */
  10330. WOLFSSL_SMALL_STACK_STATIC const byte k3[] = {
  10331. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  10332. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  10333. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  10334. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F
  10335. };
  10336. WOLFSSL_SMALL_STACK_STATIC const byte d3[] = {
  10337. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  10338. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF
  10339. };
  10340. WOLFSSL_SMALL_STACK_STATIC const byte v3[] = {
  10341. 0x64, 0xE8, 0xC3, 0xF9, 0xCE, 0x0F, 0x5B, 0xA2,
  10342. 0x63, 0xE9, 0x77, 0x79, 0x05, 0x81, 0x8A, 0x2A,
  10343. 0x93, 0xC8, 0x19, 0x1E, 0x7D, 0x6E, 0x8A, 0xE7
  10344. };
  10345. #endif
  10346. #ifdef WOLFSSL_AES_192
  10347. /* Wrap 192 bits of Key Data with a 192-bit KEK */
  10348. WOLFSSL_SMALL_STACK_STATIC const byte k4[] = {
  10349. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  10350. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  10351. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17
  10352. };
  10353. WOLFSSL_SMALL_STACK_STATIC const byte d4[] = {
  10354. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  10355. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF,
  10356. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
  10357. };
  10358. WOLFSSL_SMALL_STACK_STATIC const byte v4[] = {
  10359. 0x03, 0x1D, 0x33, 0x26, 0x4E, 0x15, 0xD3, 0x32,
  10360. 0x68, 0xF2, 0x4E, 0xC2, 0x60, 0x74, 0x3E, 0xDC,
  10361. 0xE1, 0xC6, 0xC7, 0xDD, 0xEE, 0x72, 0x5A, 0x93,
  10362. 0x6B, 0xA8, 0x14, 0x91, 0x5C, 0x67, 0x62, 0xD2
  10363. };
  10364. #endif
  10365. #ifdef WOLFSSL_AES_256
  10366. /* Wrap 192 bits of Key Data with a 256-bit KEK */
  10367. WOLFSSL_SMALL_STACK_STATIC const byte k5[] = {
  10368. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  10369. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  10370. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  10371. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F
  10372. };
  10373. WOLFSSL_SMALL_STACK_STATIC const byte d5[] = {
  10374. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  10375. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF,
  10376. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
  10377. };
  10378. WOLFSSL_SMALL_STACK_STATIC const byte v5[] = {
  10379. 0xA8, 0xF9, 0xBC, 0x16, 0x12, 0xC6, 0x8B, 0x3F,
  10380. 0xF6, 0xE6, 0xF4, 0xFB, 0xE3, 0x0E, 0x71, 0xE4,
  10381. 0x76, 0x9C, 0x8B, 0x80, 0xA3, 0x2C, 0xB8, 0x95,
  10382. 0x8C, 0xD5, 0xD1, 0x7D, 0x6B, 0x25, 0x4D, 0xA1
  10383. };
  10384. /* Wrap 256 bits of Key Data with a 256-bit KEK */
  10385. WOLFSSL_SMALL_STACK_STATIC const byte k6[] = {
  10386. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  10387. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  10388. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  10389. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F
  10390. };
  10391. WOLFSSL_SMALL_STACK_STATIC const byte d6[] = {
  10392. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  10393. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF,
  10394. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  10395. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  10396. };
  10397. WOLFSSL_SMALL_STACK_STATIC const byte v6[] = {
  10398. 0x28, 0xC9, 0xF4, 0x04, 0xC4, 0xB8, 0x10, 0xF4,
  10399. 0xCB, 0xCC, 0xB3, 0x5C, 0xFB, 0x87, 0xF8, 0x26,
  10400. 0x3F, 0x57, 0x86, 0xE2, 0xD8, 0x0E, 0xD3, 0x26,
  10401. 0xCB, 0xC7, 0xF0, 0xE7, 0x1A, 0x99, 0xF4, 0x3B,
  10402. 0xFB, 0x98, 0x8B, 0x9B, 0x7A, 0x02, 0xDD, 0x21
  10403. };
  10404. #endif /* WOLFSSL_AES_256 */
  10405. byte output[MAX_KEYWRAP_TEST_OUTLEN];
  10406. byte plain [MAX_KEYWRAP_TEST_PLAINLEN];
  10407. const keywrapVector test_wrap[] =
  10408. {
  10409. #ifdef WOLFSSL_AES_128
  10410. {k1, d1, v1, sizeof(k1), sizeof(d1), sizeof(v1)},
  10411. #endif
  10412. #ifdef WOLFSSL_AES_192
  10413. {k2, d2, v2, sizeof(k2), sizeof(d2), sizeof(v2)},
  10414. #endif
  10415. #ifdef WOLFSSL_AES_256
  10416. {k3, d3, v3, sizeof(k3), sizeof(d3), sizeof(v3)},
  10417. #endif
  10418. #ifdef WOLFSSL_AES_192
  10419. {k4, d4, v4, sizeof(k4), sizeof(d4), sizeof(v4)},
  10420. #endif
  10421. #ifdef WOLFSSL_AES_256
  10422. {k5, d5, v5, sizeof(k5), sizeof(d5), sizeof(v5)},
  10423. {k6, d6, v6, sizeof(k6), sizeof(d6), sizeof(v6)}
  10424. #endif
  10425. };
  10426. testSz = sizeof(test_wrap) / sizeof(keywrapVector);
  10427. XMEMSET(output, 0, sizeof(output));
  10428. XMEMSET(plain, 0, sizeof(plain));
  10429. for (i = 0; i < testSz; i++) {
  10430. wrapSz = wc_AesKeyWrap(test_wrap[i].kek, test_wrap[i].kekLen,
  10431. test_wrap[i].data, test_wrap[i].dataLen,
  10432. output, sizeof(output), NULL);
  10433. if ( (wrapSz < 0) || (wrapSz != (int)test_wrap[i].verifyLen) )
  10434. return -6600;
  10435. if (XMEMCMP(output, test_wrap[i].verify, test_wrap[i].verifyLen) != 0)
  10436. return -6601;
  10437. plainSz = wc_AesKeyUnWrap((byte*)test_wrap[i].kek, test_wrap[i].kekLen,
  10438. output, wrapSz,
  10439. plain, sizeof(plain), NULL);
  10440. if ( (plainSz < 0) || (plainSz != (int)test_wrap[i].dataLen) )
  10441. return -6602;
  10442. if (XMEMCMP(plain, test_wrap[i].data, test_wrap[i].dataLen) != 0)
  10443. return -6603 - i;
  10444. }
  10445. return 0;
  10446. }
  10447. #endif /* HAVE_AES_KEYWRAP */
  10448. #endif /* NO_AES */
  10449. #ifdef HAVE_CAMELLIA
  10450. enum {
  10451. CAM_ECB_ENC, CAM_ECB_DEC, CAM_CBC_ENC, CAM_CBC_DEC
  10452. };
  10453. typedef struct {
  10454. int type;
  10455. const byte* plaintext;
  10456. const byte* iv;
  10457. const byte* ciphertext;
  10458. const byte* key;
  10459. word32 keySz;
  10460. int errorCode;
  10461. } test_vector_t;
  10462. WOLFSSL_TEST_SUBROUTINE int camellia_test(void)
  10463. {
  10464. /* Camellia ECB Test Plaintext */
  10465. WOLFSSL_SMALL_STACK_STATIC const byte pte[] =
  10466. {
  10467. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  10468. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
  10469. };
  10470. /* Camellia ECB Test Initialization Vector */
  10471. WOLFSSL_SMALL_STACK_STATIC const byte ive[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  10472. /* Test 1: Camellia ECB 128-bit key */
  10473. WOLFSSL_SMALL_STACK_STATIC const byte k1[] =
  10474. {
  10475. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  10476. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
  10477. };
  10478. WOLFSSL_SMALL_STACK_STATIC const byte c1[] =
  10479. {
  10480. 0x67, 0x67, 0x31, 0x38, 0x54, 0x96, 0x69, 0x73,
  10481. 0x08, 0x57, 0x06, 0x56, 0x48, 0xea, 0xbe, 0x43
  10482. };
  10483. /* Test 2: Camellia ECB 192-bit key */
  10484. WOLFSSL_SMALL_STACK_STATIC const byte k2[] =
  10485. {
  10486. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  10487. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  10488. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  10489. };
  10490. WOLFSSL_SMALL_STACK_STATIC const byte c2[] =
  10491. {
  10492. 0xb4, 0x99, 0x34, 0x01, 0xb3, 0xe9, 0x96, 0xf8,
  10493. 0x4e, 0xe5, 0xce, 0xe7, 0xd7, 0x9b, 0x09, 0xb9
  10494. };
  10495. /* Test 3: Camellia ECB 256-bit key */
  10496. WOLFSSL_SMALL_STACK_STATIC const byte k3[] =
  10497. {
  10498. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  10499. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  10500. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  10501. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff
  10502. };
  10503. WOLFSSL_SMALL_STACK_STATIC const byte c3[] =
  10504. {
  10505. 0x9a, 0xcc, 0x23, 0x7d, 0xff, 0x16, 0xd7, 0x6c,
  10506. 0x20, 0xef, 0x7c, 0x91, 0x9e, 0x3a, 0x75, 0x09
  10507. };
  10508. /* Camellia CBC Test Plaintext */
  10509. WOLFSSL_SMALL_STACK_STATIC const byte ptc[] =
  10510. {
  10511. 0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96,
  10512. 0xE9, 0x3D, 0x7E, 0x11, 0x73, 0x93, 0x17, 0x2A
  10513. };
  10514. /* Camellia CBC Test Initialization Vector */
  10515. WOLFSSL_SMALL_STACK_STATIC const byte ivc[] =
  10516. {
  10517. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  10518. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  10519. };
  10520. /* Test 4: Camellia-CBC 128-bit key */
  10521. WOLFSSL_SMALL_STACK_STATIC const byte k4[] =
  10522. {
  10523. 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6,
  10524. 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C
  10525. };
  10526. WOLFSSL_SMALL_STACK_STATIC const byte c4[] =
  10527. {
  10528. 0x16, 0x07, 0xCF, 0x49, 0x4B, 0x36, 0xBB, 0xF0,
  10529. 0x0D, 0xAE, 0xB0, 0xB5, 0x03, 0xC8, 0x31, 0xAB
  10530. };
  10531. /* Test 5: Camellia-CBC 192-bit key */
  10532. WOLFSSL_SMALL_STACK_STATIC const byte k5[] =
  10533. {
  10534. 0x8E, 0x73, 0xB0, 0xF7, 0xDA, 0x0E, 0x64, 0x52,
  10535. 0xC8, 0x10, 0xF3, 0x2B, 0x80, 0x90, 0x79, 0xE5,
  10536. 0x62, 0xF8, 0xEA, 0xD2, 0x52, 0x2C, 0x6B, 0x7B
  10537. };
  10538. WOLFSSL_SMALL_STACK_STATIC const byte c5[] =
  10539. {
  10540. 0x2A, 0x48, 0x30, 0xAB, 0x5A, 0xC4, 0xA1, 0xA2,
  10541. 0x40, 0x59, 0x55, 0xFD, 0x21, 0x95, 0xCF, 0x93
  10542. };
  10543. /* Test 6: CBC 256-bit key */
  10544. WOLFSSL_SMALL_STACK_STATIC const byte k6[] =
  10545. {
  10546. 0x60, 0x3D, 0xEB, 0x10, 0x15, 0xCA, 0x71, 0xBE,
  10547. 0x2B, 0x73, 0xAE, 0xF0, 0x85, 0x7D, 0x77, 0x81,
  10548. 0x1F, 0x35, 0x2C, 0x07, 0x3B, 0x61, 0x08, 0xD7,
  10549. 0x2D, 0x98, 0x10, 0xA3, 0x09, 0x14, 0xDF, 0xF4
  10550. };
  10551. WOLFSSL_SMALL_STACK_STATIC const byte c6[] =
  10552. {
  10553. 0xE6, 0xCF, 0xA3, 0x5F, 0xC0, 0x2B, 0x13, 0x4A,
  10554. 0x4D, 0x2C, 0x0B, 0x67, 0x37, 0xAC, 0x3E, 0xDA
  10555. };
  10556. byte out[CAMELLIA_BLOCK_SIZE];
  10557. Camellia cam;
  10558. int i, testsSz, ret;
  10559. WOLFSSL_SMALL_STACK_STATIC const test_vector_t testVectors[] =
  10560. {
  10561. {CAM_ECB_ENC, pte, ive, c1, k1, sizeof(k1), -114},
  10562. {CAM_ECB_ENC, pte, ive, c2, k2, sizeof(k2), -115},
  10563. {CAM_ECB_ENC, pte, ive, c3, k3, sizeof(k3), -116},
  10564. {CAM_ECB_DEC, pte, ive, c1, k1, sizeof(k1), -117},
  10565. {CAM_ECB_DEC, pte, ive, c2, k2, sizeof(k2), -118},
  10566. {CAM_ECB_DEC, pte, ive, c3, k3, sizeof(k3), -119},
  10567. {CAM_CBC_ENC, ptc, ivc, c4, k4, sizeof(k4), -120},
  10568. {CAM_CBC_ENC, ptc, ivc, c5, k5, sizeof(k5), -121},
  10569. {CAM_CBC_ENC, ptc, ivc, c6, k6, sizeof(k6), -122},
  10570. {CAM_CBC_DEC, ptc, ivc, c4, k4, sizeof(k4), -123},
  10571. {CAM_CBC_DEC, ptc, ivc, c5, k5, sizeof(k5), -124},
  10572. {CAM_CBC_DEC, ptc, ivc, c6, k6, sizeof(k6), -125}
  10573. };
  10574. testsSz = sizeof(testVectors)/sizeof(test_vector_t);
  10575. for (i = 0; i < testsSz; i++) {
  10576. if (wc_CamelliaSetKey(&cam, testVectors[i].key, testVectors[i].keySz,
  10577. testVectors[i].iv) != 0)
  10578. return testVectors[i].errorCode;
  10579. switch (testVectors[i].type) {
  10580. case CAM_ECB_ENC:
  10581. ret = wc_CamelliaEncryptDirect(&cam, out,
  10582. testVectors[i].plaintext);
  10583. if (ret != 0 || XMEMCMP(out, testVectors[i].ciphertext,
  10584. CAMELLIA_BLOCK_SIZE))
  10585. return testVectors[i].errorCode;
  10586. break;
  10587. case CAM_ECB_DEC:
  10588. ret = wc_CamelliaDecryptDirect(&cam, out,
  10589. testVectors[i].ciphertext);
  10590. if (ret != 0 || XMEMCMP(out, testVectors[i].plaintext,
  10591. CAMELLIA_BLOCK_SIZE))
  10592. return testVectors[i].errorCode;
  10593. break;
  10594. case CAM_CBC_ENC:
  10595. ret = wc_CamelliaCbcEncrypt(&cam, out, testVectors[i].plaintext,
  10596. CAMELLIA_BLOCK_SIZE);
  10597. if (ret != 0 || XMEMCMP(out, testVectors[i].ciphertext,
  10598. CAMELLIA_BLOCK_SIZE))
  10599. return testVectors[i].errorCode;
  10600. break;
  10601. case CAM_CBC_DEC:
  10602. ret = wc_CamelliaCbcDecrypt(&cam, out,
  10603. testVectors[i].ciphertext, CAMELLIA_BLOCK_SIZE);
  10604. if (ret != 0 || XMEMCMP(out, testVectors[i].plaintext,
  10605. CAMELLIA_BLOCK_SIZE))
  10606. return testVectors[i].errorCode;
  10607. break;
  10608. default:
  10609. break;
  10610. }
  10611. }
  10612. /* Setting the IV and checking it was actually set. */
  10613. ret = wc_CamelliaSetIV(&cam, ivc);
  10614. if (ret != 0 || XMEMCMP(cam.reg, ivc, CAMELLIA_BLOCK_SIZE))
  10615. return -6700;
  10616. /* Setting the IV to NULL should be same as all zeros IV */
  10617. if (wc_CamelliaSetIV(&cam, NULL) != 0 ||
  10618. XMEMCMP(cam.reg, ive, CAMELLIA_BLOCK_SIZE))
  10619. return -6701;
  10620. /* First parameter should never be null */
  10621. if (wc_CamelliaSetIV(NULL, NULL) == 0)
  10622. return -6702;
  10623. /* First parameter should never be null, check it fails */
  10624. if (wc_CamelliaSetKey(NULL, k1, sizeof(k1), NULL) == 0)
  10625. return -6703;
  10626. /* Key should have a size of 16, 24, or 32 */
  10627. if (wc_CamelliaSetKey(&cam, k1, 0, NULL) == 0)
  10628. return -6704;
  10629. return 0;
  10630. }
  10631. #endif /* HAVE_CAMELLIA */
  10632. #ifdef HAVE_XCHACHA
  10633. WOLFSSL_TEST_SUBROUTINE int XChaCha_test(void) {
  10634. int ret = -6830;
  10635. WOLFSSL_SMALL_STACK_STATIC const byte Plaintext[] = {
  10636. 0x54, 0x68, 0x65, 0x20, 0x64, 0x68, 0x6f, 0x6c, 0x65, 0x20, 0x28, 0x70, 0x72, 0x6f, 0x6e, 0x6f, /* The dhole (prono */
  10637. 0x75, 0x6e, 0x63, 0x65, 0x64, 0x20, 0x22, 0x64, 0x6f, 0x6c, 0x65, 0x22, 0x29, 0x20, 0x69, 0x73, /* unced "dole") is */
  10638. 0x20, 0x61, 0x6c, 0x73, 0x6f, 0x20, 0x6b, 0x6e, 0x6f, 0x77, 0x6e, 0x20, 0x61, 0x73, 0x20, 0x74, /* also known as t */
  10639. 0x68, 0x65, 0x20, 0x41, 0x73, 0x69, 0x61, 0x74, 0x69, 0x63, 0x20, 0x77, 0x69, 0x6c, 0x64, 0x20, /* he Asiatic wild */
  10640. 0x64, 0x6f, 0x67, 0x2c, 0x20, 0x72, 0x65, 0x64, 0x20, 0x64, 0x6f, 0x67, 0x2c, 0x20, 0x61, 0x6e, /* dog, red dog, an */
  10641. 0x64, 0x20, 0x77, 0x68, 0x69, 0x73, 0x74, 0x6c, 0x69, 0x6e, 0x67, 0x20, 0x64, 0x6f, 0x67, 0x2e, /* d whistling dog. */
  10642. 0x20, 0x49, 0x74, 0x20, 0x69, 0x73, 0x20, 0x61, 0x62, 0x6f, 0x75, 0x74, 0x20, 0x74, 0x68, 0x65, /* It is about the */
  10643. 0x20, 0x73, 0x69, 0x7a, 0x65, 0x20, 0x6f, 0x66, 0x20, 0x61, 0x20, 0x47, 0x65, 0x72, 0x6d, 0x61, /* size of a Germa */
  10644. 0x6e, 0x20, 0x73, 0x68, 0x65, 0x70, 0x68, 0x65, 0x72, 0x64, 0x20, 0x62, 0x75, 0x74, 0x20, 0x6c, /* n shepherd but l */
  10645. 0x6f, 0x6f, 0x6b, 0x73, 0x20, 0x6d, 0x6f, 0x72, 0x65, 0x20, 0x6c, 0x69, 0x6b, 0x65, 0x20, 0x61, /* ooks more like a */
  10646. 0x20, 0x6c, 0x6f, 0x6e, 0x67, 0x2d, 0x6c, 0x65, 0x67, 0x67, 0x65, 0x64, 0x20, 0x66, 0x6f, 0x78, /* long-legged fox */
  10647. 0x2e, 0x20, 0x54, 0x68, 0x69, 0x73, 0x20, 0x68, 0x69, 0x67, 0x68, 0x6c, 0x79, 0x20, 0x65, 0x6c, /* . This highly el */
  10648. 0x75, 0x73, 0x69, 0x76, 0x65, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x73, 0x6b, 0x69, 0x6c, 0x6c, 0x65, /* usive and skille */
  10649. 0x64, 0x20, 0x6a, 0x75, 0x6d, 0x70, 0x65, 0x72, 0x20, 0x69, 0x73, 0x20, 0x63, 0x6c, 0x61, 0x73, /* d jumper is clas */
  10650. 0x73, 0x69, 0x66, 0x69, 0x65, 0x64, 0x20, 0x77, 0x69, 0x74, 0x68, 0x20, 0x77, 0x6f, 0x6c, 0x76, /* sified with wolv */
  10651. 0x65, 0x73, 0x2c, 0x20, 0x63, 0x6f, 0x79, 0x6f, 0x74, 0x65, 0x73, 0x2c, 0x20, 0x6a, 0x61, 0x63, /* es, coyotes, jac */
  10652. 0x6b, 0x61, 0x6c, 0x73, 0x2c, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x66, 0x6f, 0x78, 0x65, 0x73, 0x20, /* kals, and foxes */
  10653. 0x69, 0x6e, 0x20, 0x74, 0x68, 0x65, 0x20, 0x74, 0x61, 0x78, 0x6f, 0x6e, 0x6f, 0x6d, 0x69, 0x63, /* in the taxonomic */
  10654. 0x20, 0x66, 0x61, 0x6d, 0x69, 0x6c, 0x79, 0x20, 0x43, 0x61, 0x6e, 0x69, 0x64, 0x61, 0x65, 0x2e /* family Canidae. */
  10655. };
  10656. WOLFSSL_SMALL_STACK_STATIC const byte Key[] = {
  10657. 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  10658. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f
  10659. };
  10660. WOLFSSL_SMALL_STACK_STATIC const byte IV[] = {
  10661. 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, /* @ABCDEFGHIJKLMNO */
  10662. 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x58 }; /* PQRSTUVW */
  10663. WOLFSSL_SMALL_STACK_STATIC const byte Ciphertext[] = {
  10664. 0x45, 0x59, 0xab, 0xba, 0x4e, 0x48, 0xc1, 0x61, 0x02, 0xe8, 0xbb, 0x2c, 0x05, 0xe6, 0x94, 0x7f,
  10665. 0x50, 0xa7, 0x86, 0xde, 0x16, 0x2f, 0x9b, 0x0b, 0x7e, 0x59, 0x2a, 0x9b, 0x53, 0xd0, 0xd4, 0xe9,
  10666. 0x8d, 0x8d, 0x64, 0x10, 0xd5, 0x40, 0xa1, 0xa6, 0x37, 0x5b, 0x26, 0xd8, 0x0d, 0xac, 0xe4, 0xfa,
  10667. 0xb5, 0x23, 0x84, 0xc7, 0x31, 0xac, 0xbf, 0x16, 0xa5, 0x92, 0x3c, 0x0c, 0x48, 0xd3, 0x57, 0x5d,
  10668. 0x4d, 0x0d, 0x2c, 0x67, 0x3b, 0x66, 0x6f, 0xaa, 0x73, 0x10, 0x61, 0x27, 0x77, 0x01, 0x09, 0x3a,
  10669. 0x6b, 0xf7, 0xa1, 0x58, 0xa8, 0x86, 0x42, 0x92, 0xa4, 0x1c, 0x48, 0xe3, 0xa9, 0xb4, 0xc0, 0xda,
  10670. 0xec, 0xe0, 0xf8, 0xd9, 0x8d, 0x0d, 0x7e, 0x05, 0xb3, 0x7a, 0x30, 0x7b, 0xbb, 0x66, 0x33, 0x31,
  10671. 0x64, 0xec, 0x9e, 0x1b, 0x24, 0xea, 0x0d, 0x6c, 0x3f, 0xfd, 0xdc, 0xec, 0x4f, 0x68, 0xe7, 0x44,
  10672. 0x30, 0x56, 0x19, 0x3a, 0x03, 0xc8, 0x10, 0xe1, 0x13, 0x44, 0xca, 0x06, 0xd8, 0xed, 0x8a, 0x2b,
  10673. 0xfb, 0x1e, 0x8d, 0x48, 0xcf, 0xa6, 0xbc, 0x0e, 0xb4, 0xe2, 0x46, 0x4b, 0x74, 0x81, 0x42, 0x40,
  10674. 0x7c, 0x9f, 0x43, 0x1a, 0xee, 0x76, 0x99, 0x60, 0xe1, 0x5b, 0xa8, 0xb9, 0x68, 0x90, 0x46, 0x6e,
  10675. 0xf2, 0x45, 0x75, 0x99, 0x85, 0x23, 0x85, 0xc6, 0x61, 0xf7, 0x52, 0xce, 0x20, 0xf9, 0xda, 0x0c,
  10676. 0x09, 0xab, 0x6b, 0x19, 0xdf, 0x74, 0xe7, 0x6a, 0x95, 0x96, 0x74, 0x46, 0xf8, 0xd0, 0xfd, 0x41,
  10677. 0x5e, 0x7b, 0xee, 0x2a, 0x12, 0xa1, 0x14, 0xc2, 0x0e, 0xb5, 0x29, 0x2a, 0xe7, 0xa3, 0x49, 0xae,
  10678. 0x57, 0x78, 0x20, 0xd5, 0x52, 0x0a, 0x1f, 0x3f, 0xb6, 0x2a, 0x17, 0xce, 0x6a, 0x7e, 0x68, 0xfa,
  10679. 0x7c, 0x79, 0x11, 0x1d, 0x88, 0x60, 0x92, 0x0b, 0xc0, 0x48, 0xef, 0x43, 0xfe, 0x84, 0x48, 0x6c,
  10680. 0xcb, 0x87, 0xc2, 0x5f, 0x0a, 0xe0, 0x45, 0xf0, 0xcc, 0xe1, 0xe7, 0x98, 0x9a, 0x9a, 0xa2, 0x20,
  10681. 0xa2, 0x8b, 0xdd, 0x48, 0x27, 0xe7, 0x51, 0xa2, 0x4a, 0x6d, 0x5c, 0x62, 0xd7, 0x90, 0xa6, 0x63,
  10682. 0x93, 0xb9, 0x31, 0x11, 0xc1, 0xa5, 0x5d, 0xd7, 0x42, 0x1a, 0x10, 0x18, 0x49, 0x74, 0xc7, 0xc5
  10683. };
  10684. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10685. struct ChaCha *chacha = (struct ChaCha *)XMALLOC(sizeof *chacha, HEAP_HINT, DYNAMIC_TYPE_CIPHER);
  10686. byte *buf1 = (byte *)XMALLOC(sizeof Plaintext, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10687. byte *buf2 = (byte *)XMALLOC(sizeof Plaintext, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10688. if ((chacha == NULL) || (buf1 == NULL) || (buf2 == NULL))
  10689. ERROR_OUT(MEMORY_E, out);
  10690. #else
  10691. struct ChaCha chacha[1];
  10692. byte buf1[sizeof Plaintext];
  10693. byte buf2[sizeof Plaintext];
  10694. #endif
  10695. ret = wc_XChacha_SetKey(chacha, Key, sizeof Key, IV, sizeof IV, 0);
  10696. if (ret < 0)
  10697. ERROR_OUT(-6831, out);
  10698. ret = wc_Chacha_Process(chacha, buf1, Plaintext, sizeof Plaintext);
  10699. if (ret < 0)
  10700. ERROR_OUT(-6832, out);
  10701. if (XMEMCMP(buf1, Ciphertext, sizeof Plaintext))
  10702. ERROR_OUT(-6833, out);
  10703. ret = wc_XChacha_SetKey(chacha, Key, sizeof Key, IV, sizeof IV, 0);
  10704. if (ret < 0)
  10705. ERROR_OUT(-6834, out);
  10706. ret = wc_Chacha_Process(chacha, buf2, buf1, sizeof Plaintext);
  10707. if (ret < 0)
  10708. ERROR_OUT(-6835, out);
  10709. if (XMEMCMP(buf2, Plaintext, sizeof Plaintext))
  10710. ERROR_OUT(-6836, out);
  10711. out:
  10712. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10713. if (chacha)
  10714. XFREE(chacha, HEAP_HINT, DYNAMIC_TYPE_CIPHER);
  10715. if (buf1)
  10716. XFREE(buf1, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10717. if (buf2)
  10718. XFREE(buf2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10719. #endif
  10720. return ret;
  10721. }
  10722. #endif /* HAVE_XCHACHA */
  10723. #if defined(HAVE_XCHACHA) && defined(HAVE_POLY1305)
  10724. WOLFSSL_TEST_SUBROUTINE int XChaCha20Poly1305_test(void) {
  10725. int ret;
  10726. WOLFSSL_SMALL_STACK_STATIC const byte Plaintext[] = {
  10727. 0x4c, 0x61, 0x64, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x47, 0x65, 0x6e, 0x74, 0x6c, /* Ladies and Gentl */
  10728. 0x65, 0x6d, 0x65, 0x6e, 0x20, 0x6f, 0x66, 0x20, 0x74, 0x68, 0x65, 0x20, 0x63, 0x6c, 0x61, 0x73, /* emen of the clas */
  10729. 0x73, 0x20, 0x6f, 0x66, 0x20, 0x27, 0x39, 0x39, 0x3a, 0x20, 0x49, 0x66, 0x20, 0x49, 0x20, 0x63, /* s of '99: If I c */
  10730. 0x6f, 0x75, 0x6c, 0x64, 0x20, 0x6f, 0x66, 0x66, 0x65, 0x72, 0x20, 0x79, 0x6f, 0x75, 0x20, 0x6f, /* ould offer you o */
  10731. 0x6e, 0x6c, 0x79, 0x20, 0x6f, 0x6e, 0x65, 0x20, 0x74, 0x69, 0x70, 0x20, 0x66, 0x6f, 0x72, 0x20, /* nly one tip for */
  10732. 0x74, 0x68, 0x65, 0x20, 0x66, 0x75, 0x74, 0x75, 0x72, 0x65, 0x2c, 0x20, 0x73, 0x75, 0x6e, 0x73, /* the future, suns */
  10733. 0x63, 0x72, 0x65, 0x65, 0x6e, 0x20, 0x77, 0x6f, 0x75, 0x6c, 0x64, 0x20, 0x62, 0x65, 0x20, 0x69, /* creen would be i */
  10734. 0x74, 0x2e }; /* t. */
  10735. WOLFSSL_SMALL_STACK_STATIC const byte AAD[] = { 0x50, 0x51, 0x52, 0x53, 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7 }; /* PQRS........ */
  10736. WOLFSSL_SMALL_STACK_STATIC const byte Key[] = {
  10737. 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  10738. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f
  10739. };
  10740. WOLFSSL_SMALL_STACK_STATIC const byte IV[] = {
  10741. 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, /* @ABCDEFGHIJKLMNO */
  10742. 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57 }; /* PQRSTUVW */
  10743. WOLFSSL_SMALL_STACK_STATIC const byte Ciphertext[] = {
  10744. 0xbd, 0x6d, 0x17, 0x9d, 0x3e, 0x83, 0xd4, 0x3b, 0x95, 0x76, 0x57, 0x94, 0x93, 0xc0, 0xe9, 0x39,
  10745. 0x57, 0x2a, 0x17, 0x00, 0x25, 0x2b, 0xfa, 0xcc, 0xbe, 0xd2, 0x90, 0x2c, 0x21, 0x39, 0x6c, 0xbb,
  10746. 0x73, 0x1c, 0x7f, 0x1b, 0x0b, 0x4a, 0xa6, 0x44, 0x0b, 0xf3, 0xa8, 0x2f, 0x4e, 0xda, 0x7e, 0x39,
  10747. 0xae, 0x64, 0xc6, 0x70, 0x8c, 0x54, 0xc2, 0x16, 0xcb, 0x96, 0xb7, 0x2e, 0x12, 0x13, 0xb4, 0x52,
  10748. 0x2f, 0x8c, 0x9b, 0xa4, 0x0d, 0xb5, 0xd9, 0x45, 0xb1, 0x1b, 0x69, 0xb9, 0x82, 0xc1, 0xbb, 0x9e,
  10749. 0x3f, 0x3f, 0xac, 0x2b, 0xc3, 0x69, 0x48, 0x8f, 0x76, 0xb2, 0x38, 0x35, 0x65, 0xd3, 0xff, 0xf9,
  10750. 0x21, 0xf9, 0x66, 0x4c, 0x97, 0x63, 0x7d, 0xa9, 0x76, 0x88, 0x12, 0xf6, 0x15, 0xc6, 0x8b, 0x13,
  10751. 0xb5, 0x2e };
  10752. WOLFSSL_SMALL_STACK_STATIC const byte Tag[] = {
  10753. 0xc0, 0x87, 0x59, 0x24, 0xc1, 0xc7, 0x98, 0x79, 0x47, 0xde, 0xaf, 0xd8, 0x78, 0x0a, 0xcf, 0x49
  10754. };
  10755. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10756. byte *buf1 = (byte *)XMALLOC(sizeof Ciphertext + sizeof Tag, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10757. byte *buf2 = (byte *)XMALLOC(sizeof Plaintext, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10758. if ((buf1 == NULL) || (buf2 == NULL))
  10759. ERROR_OUT(-6480, out);
  10760. #else
  10761. byte buf1[sizeof Ciphertext + sizeof Tag];
  10762. byte buf2[sizeof Plaintext];
  10763. #endif
  10764. ret = wc_XChaCha20Poly1305_Encrypt(buf1, sizeof Ciphertext + sizeof Tag,
  10765. Plaintext, sizeof Plaintext,
  10766. AAD, sizeof AAD,
  10767. IV, sizeof IV,
  10768. Key, sizeof Key);
  10769. if (ret < 0)
  10770. ERROR_OUT(-6841, out);
  10771. if (XMEMCMP(buf1, Ciphertext, sizeof Ciphertext))
  10772. ERROR_OUT(-6842, out);
  10773. if (XMEMCMP(buf1 + sizeof Ciphertext, Tag, CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE))
  10774. ERROR_OUT(-6843, out);
  10775. ret = wc_XChaCha20Poly1305_Decrypt(buf2, sizeof Plaintext,
  10776. buf1, sizeof Ciphertext + sizeof Tag,
  10777. AAD, sizeof AAD,
  10778. IV, sizeof IV,
  10779. Key, sizeof Key);
  10780. if (ret < 0)
  10781. ERROR_OUT(-6844, out);
  10782. if (XMEMCMP(buf2, Plaintext, sizeof Plaintext))
  10783. ERROR_OUT(-6845, out);
  10784. out:
  10785. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10786. if (buf1 != NULL)
  10787. XFREE(buf1, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10788. if (buf2 != NULL)
  10789. XFREE(buf2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10790. #endif
  10791. return ret;
  10792. }
  10793. #endif /* defined(HAVE_XCHACHA) && defined(HAVE_POLY1305) */
  10794. #ifndef WC_NO_RNG
  10795. static int _rng_test(WC_RNG* rng, int errorOffset)
  10796. {
  10797. byte block[32];
  10798. int ret, i;
  10799. XMEMSET(block, 0, sizeof(block));
  10800. ret = wc_RNG_GenerateBlock(rng, block, sizeof(block));
  10801. if (ret != 0) {
  10802. ret = -6850;
  10803. goto exit;
  10804. }
  10805. /* Check for 0's */
  10806. for (i=0; i<(int)sizeof(block); i++) {
  10807. if (block[i] == 0) {
  10808. ret++;
  10809. }
  10810. }
  10811. /* All zeros count check */
  10812. if (ret >= (int)sizeof(block)) {
  10813. ret = -6851;
  10814. goto exit;
  10815. }
  10816. ret = wc_RNG_GenerateByte(rng, block);
  10817. if (ret != 0) {
  10818. ret = -6852;
  10819. goto exit;
  10820. }
  10821. /* Parameter validation testing. */
  10822. ret = wc_RNG_GenerateBlock(NULL, block, sizeof(block));
  10823. if (ret != BAD_FUNC_ARG) {
  10824. ret = -6853;
  10825. goto exit;
  10826. }
  10827. ret = wc_RNG_GenerateBlock(rng, NULL, sizeof(block));
  10828. if (ret != BAD_FUNC_ARG) {
  10829. ret = -6854;
  10830. goto exit;
  10831. }
  10832. ret = wc_RNG_GenerateByte(NULL, block);
  10833. if (ret != BAD_FUNC_ARG) {
  10834. ret = -6855;
  10835. goto exit;
  10836. }
  10837. ret = wc_RNG_GenerateByte(rng, NULL);
  10838. if (ret != BAD_FUNC_ARG) {
  10839. ret = -6856;
  10840. goto exit;
  10841. }
  10842. ret = 0;
  10843. exit:
  10844. if (ret != 0)
  10845. ret += errorOffset;
  10846. return ret;
  10847. }
  10848. static int random_rng_test(void)
  10849. {
  10850. WC_RNG localRng;
  10851. WC_RNG* rng;
  10852. int ret;
  10853. rng = &localRng;
  10854. /* Test stack based RNG. */
  10855. #ifndef HAVE_FIPS
  10856. ret = wc_InitRng_ex(rng, HEAP_HINT, devId);
  10857. #else
  10858. ret = wc_InitRng(rng);
  10859. #endif
  10860. if (ret != 0) return -6900;
  10861. ret = _rng_test(rng, -6300);
  10862. /* Make sure and free RNG */
  10863. wc_FreeRng(rng);
  10864. if (ret != 0) return ret;
  10865. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && !defined(WOLFSSL_NO_MALLOC)
  10866. {
  10867. byte nonce[8] = { 0 };
  10868. /* Test dynamic RNG. */
  10869. rng = wc_rng_new(nonce, (word32)sizeof(nonce), HEAP_HINT);
  10870. if (rng == NULL) return -6901;
  10871. ret = _rng_test(rng, -6310);
  10872. wc_rng_free(rng);
  10873. }
  10874. #endif
  10875. return ret;
  10876. }
  10877. #if defined(HAVE_HASHDRBG) && !defined(CUSTOM_RAND_GENERATE_BLOCK)
  10878. #ifdef WC_RNG_SEED_CB
  10879. static int seed_cb(OS_Seed* os, byte* output, word32 sz)
  10880. {
  10881. word32 i;
  10882. (void)os;
  10883. /* Known answer test. Set the seed to the same value every time. */
  10884. for (i = 0; i < sz; i++)
  10885. output[i] = (byte)i;
  10886. return 0;
  10887. }
  10888. static int rng_seed_test(void)
  10889. {
  10890. #ifndef HAVE_FIPS
  10891. WOLFSSL_SMALL_STACK_STATIC const byte check[] =
  10892. {
  10893. 0x83, 0x46, 0x65, 0x2f, 0x5c, 0x44, 0x16, 0x5f,
  10894. 0xb3, 0x89, 0x26, 0xde, 0x0b, 0x6b, 0xa2, 0x06,
  10895. 0x7e, 0xa7, 0x9a, 0x55, 0x22, 0x01, 0xb0, 0x22,
  10896. 0xf4, 0x7e, 0xa2, 0x66, 0xc4, 0x08, 0x6f, 0xba
  10897. };
  10898. #else
  10899. /* FIPS uses a longer seed, so different check value. */
  10900. WOLFSSL_SMALL_STACK_STATIC const byte check[] =
  10901. {
  10902. 0xaf, 0x31, 0xcc, 0xef, 0xa9, 0x29, 0x4c, 0x24,
  10903. 0xbd, 0xa5, 0xa3, 0x52, 0x69, 0xf3, 0xb9, 0xb2,
  10904. 0x1e, 0xd4, 0x52, 0x3b, 0x9a, 0x96, 0x06, 0x20,
  10905. 0xc0, 0x5f, 0x44, 0x06, 0x1f, 0x80, 0xdf, 0xe0
  10906. };
  10907. #endif
  10908. byte output[WC_SHA256_DIGEST_SIZE];
  10909. WC_RNG rng;
  10910. int ret;
  10911. ret = wc_SetSeed_Cb(seed_cb);
  10912. if (ret != 0) {
  10913. ret = -7007;
  10914. goto exit;
  10915. }
  10916. ret = wc_InitRng(&rng);
  10917. if (ret != 0) {
  10918. ret = -7008;
  10919. goto exit;
  10920. }
  10921. ret = wc_RNG_GenerateBlock(&rng, output, sizeof(output));
  10922. if (ret != 0) {
  10923. ret = -7009;
  10924. goto exit;
  10925. }
  10926. ret = XMEMCMP(output, check, sizeof(output));
  10927. if (ret != 0) {
  10928. ret = -7010;
  10929. goto exit;
  10930. }
  10931. ret = wc_FreeRng(&rng);
  10932. if (ret != 0) {
  10933. ret = -7011;
  10934. goto exit;
  10935. }
  10936. ret = wc_SetSeed_Cb(wc_GenerateSeed);
  10937. if (ret != 0) {
  10938. ret = -7012;
  10939. }
  10940. exit:
  10941. return ret;
  10942. }
  10943. #endif
  10944. WOLFSSL_TEST_SUBROUTINE int random_test(void)
  10945. {
  10946. WOLFSSL_SMALL_STACK_STATIC const byte test1Entropy[] =
  10947. {
  10948. 0xa6, 0x5a, 0xd0, 0xf3, 0x45, 0xdb, 0x4e, 0x0e, 0xff, 0xe8, 0x75, 0xc3,
  10949. 0xa2, 0xe7, 0x1f, 0x42, 0xc7, 0x12, 0x9d, 0x62, 0x0f, 0xf5, 0xc1, 0x19,
  10950. 0xa9, 0xef, 0x55, 0xf0, 0x51, 0x85, 0xe0, 0xfb, 0x85, 0x81, 0xf9, 0x31,
  10951. 0x75, 0x17, 0x27, 0x6e, 0x06, 0xe9, 0x60, 0x7d, 0xdb, 0xcb, 0xcc, 0x2e
  10952. };
  10953. WOLFSSL_SMALL_STACK_STATIC const byte test1Output[] =
  10954. {
  10955. 0xd3, 0xe1, 0x60, 0xc3, 0x5b, 0x99, 0xf3, 0x40, 0xb2, 0x62, 0x82, 0x64,
  10956. 0xd1, 0x75, 0x10, 0x60, 0xe0, 0x04, 0x5d, 0xa3, 0x83, 0xff, 0x57, 0xa5,
  10957. 0x7d, 0x73, 0xa6, 0x73, 0xd2, 0xb8, 0xd8, 0x0d, 0xaa, 0xf6, 0xa6, 0xc3,
  10958. 0x5a, 0x91, 0xbb, 0x45, 0x79, 0xd7, 0x3f, 0xd0, 0xc8, 0xfe, 0xd1, 0x11,
  10959. 0xb0, 0x39, 0x13, 0x06, 0x82, 0x8a, 0xdf, 0xed, 0x52, 0x8f, 0x01, 0x81,
  10960. 0x21, 0xb3, 0xfe, 0xbd, 0xc3, 0x43, 0xe7, 0x97, 0xb8, 0x7d, 0xbb, 0x63,
  10961. 0xdb, 0x13, 0x33, 0xde, 0xd9, 0xd1, 0xec, 0xe1, 0x77, 0xcf, 0xa6, 0xb7,
  10962. 0x1f, 0xe8, 0xab, 0x1d, 0xa4, 0x66, 0x24, 0xed, 0x64, 0x15, 0xe5, 0x1c,
  10963. 0xcd, 0xe2, 0xc7, 0xca, 0x86, 0xe2, 0x83, 0x99, 0x0e, 0xea, 0xeb, 0x91,
  10964. 0x12, 0x04, 0x15, 0x52, 0x8b, 0x22, 0x95, 0x91, 0x02, 0x81, 0xb0, 0x2d,
  10965. 0xd4, 0x31, 0xf4, 0xc9, 0xf7, 0x04, 0x27, 0xdf
  10966. };
  10967. WOLFSSL_SMALL_STACK_STATIC const byte test2EntropyA[] =
  10968. {
  10969. 0x63, 0x36, 0x33, 0x77, 0xe4, 0x1e, 0x86, 0x46, 0x8d, 0xeb, 0x0a, 0xb4,
  10970. 0xa8, 0xed, 0x68, 0x3f, 0x6a, 0x13, 0x4e, 0x47, 0xe0, 0x14, 0xc7, 0x00,
  10971. 0x45, 0x4e, 0x81, 0xe9, 0x53, 0x58, 0xa5, 0x69, 0x80, 0x8a, 0xa3, 0x8f,
  10972. 0x2a, 0x72, 0xa6, 0x23, 0x59, 0x91, 0x5a, 0x9f, 0x8a, 0x04, 0xca, 0x68
  10973. };
  10974. WOLFSSL_SMALL_STACK_STATIC const byte test2EntropyB[] =
  10975. {
  10976. 0xe6, 0x2b, 0x8a, 0x8e, 0xe8, 0xf1, 0x41, 0xb6, 0x98, 0x05, 0x66, 0xe3,
  10977. 0xbf, 0xe3, 0xc0, 0x49, 0x03, 0xda, 0xd4, 0xac, 0x2c, 0xdf, 0x9f, 0x22,
  10978. 0x80, 0x01, 0x0a, 0x67, 0x39, 0xbc, 0x83, 0xd3
  10979. };
  10980. WOLFSSL_SMALL_STACK_STATIC const byte test2Output[] =
  10981. {
  10982. 0x04, 0xee, 0xc6, 0x3b, 0xb2, 0x31, 0xdf, 0x2c, 0x63, 0x0a, 0x1a, 0xfb,
  10983. 0xe7, 0x24, 0x94, 0x9d, 0x00, 0x5a, 0x58, 0x78, 0x51, 0xe1, 0xaa, 0x79,
  10984. 0x5e, 0x47, 0x73, 0x47, 0xc8, 0xb0, 0x56, 0x62, 0x1c, 0x18, 0xbd, 0xdc,
  10985. 0xdd, 0x8d, 0x99, 0xfc, 0x5f, 0xc2, 0xb9, 0x20, 0x53, 0xd8, 0xcf, 0xac,
  10986. 0xfb, 0x0b, 0xb8, 0x83, 0x12, 0x05, 0xfa, 0xd1, 0xdd, 0xd6, 0xc0, 0x71,
  10987. 0x31, 0x8a, 0x60, 0x18, 0xf0, 0x3b, 0x73, 0xf5, 0xed, 0xe4, 0xd4, 0xd0,
  10988. 0x71, 0xf9, 0xde, 0x03, 0xfd, 0x7a, 0xea, 0x10, 0x5d, 0x92, 0x99, 0xb8,
  10989. 0xaf, 0x99, 0xaa, 0x07, 0x5b, 0xdb, 0x4d, 0xb9, 0xaa, 0x28, 0xc1, 0x8d,
  10990. 0x17, 0x4b, 0x56, 0xee, 0x2a, 0x01, 0x4d, 0x09, 0x88, 0x96, 0xff, 0x22,
  10991. 0x82, 0xc9, 0x55, 0xa8, 0x19, 0x69, 0xe0, 0x69, 0xfa, 0x8c, 0xe0, 0x07,
  10992. 0xa1, 0x80, 0x18, 0x3a, 0x07, 0xdf, 0xae, 0x17
  10993. };
  10994. byte output[WC_SHA256_DIGEST_SIZE * 4];
  10995. int ret;
  10996. ret = wc_RNG_HealthTest(0, test1Entropy, sizeof(test1Entropy), NULL, 0,
  10997. output, sizeof(output));
  10998. if (ret != 0)
  10999. return -7000;
  11000. if (XMEMCMP(test1Output, output, sizeof(output)) != 0)
  11001. return -7001;
  11002. ret = wc_RNG_HealthTest(1, test2EntropyA, sizeof(test2EntropyA),
  11003. test2EntropyB, sizeof(test2EntropyB),
  11004. output, sizeof(output));
  11005. if (ret != 0)
  11006. return -7002;
  11007. if (XMEMCMP(test2Output, output, sizeof(output)) != 0)
  11008. return -7003;
  11009. /* Basic RNG generate block test */
  11010. if ((ret = random_rng_test()) != 0)
  11011. return ret;
  11012. /* Test the seed check function. */
  11013. #if !(defined(HAVE_FIPS) || defined(HAVE_SELFTEST)) || \
  11014. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))
  11015. {
  11016. word32 i, outputSz;
  11017. /* Repeat the same byte over and over. Should fail. */
  11018. outputSz = sizeof(output);
  11019. XMEMSET(output, 1, outputSz);
  11020. ret = wc_RNG_TestSeed(output, outputSz);
  11021. if (ret == 0)
  11022. return -7004;
  11023. /* Every byte of the entropy scratch is different,
  11024. * entropy is a single byte that shouldn't match. */
  11025. outputSz = (sizeof(output) / 2) + 1;
  11026. for (i = 0; i < outputSz; i++)
  11027. output[i] = (byte)i;
  11028. ret = wc_RNG_TestSeed(output, outputSz);
  11029. if (ret != 0)
  11030. return -7005;
  11031. outputSz = sizeof(output);
  11032. for (i = 0; i < outputSz; i++)
  11033. output[i] = (byte)i;
  11034. ret = wc_RNG_TestSeed(output, outputSz);
  11035. if (ret != 0)
  11036. return -7006;
  11037. }
  11038. #endif
  11039. /* Test the seed callback. */
  11040. #ifdef WC_RNG_SEED_CB
  11041. if ((ret = rng_seed_test()) != 0)
  11042. return ret;
  11043. #endif
  11044. return 0;
  11045. }
  11046. #else
  11047. WOLFSSL_TEST_SUBROUTINE int random_test(void)
  11048. {
  11049. /* Basic RNG generate block test */
  11050. return random_rng_test();
  11051. }
  11052. #endif /* HAVE_HASHDRBG && !CUSTOM_RAND_GENERATE_BLOCK */
  11053. #endif /* WC_NO_RNG */
  11054. #ifndef MEM_TEST_SZ
  11055. #define MEM_TEST_SZ 1024
  11056. #endif
  11057. #if defined(WOLFSSL_STATIC_MEMORY) || !defined(WOLFSSL_NO_MALLOC)
  11058. static int simple_mem_test(int sz)
  11059. {
  11060. int ret = 0;
  11061. byte* b;
  11062. int i;
  11063. b = (byte*)XMALLOC(sz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11064. if (b == NULL) {
  11065. return -7110;
  11066. }
  11067. /* utilize memory */
  11068. for (i = 0; i < sz; i++) {
  11069. b[i] = (byte)i;
  11070. }
  11071. /* read back and verify */
  11072. for (i = 0; i < sz; i++) {
  11073. if (b[i] != (byte)i) {
  11074. ret = -7111;
  11075. break;
  11076. }
  11077. }
  11078. XFREE(b, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11079. return ret;
  11080. }
  11081. #endif
  11082. WOLFSSL_TEST_SUBROUTINE int memory_test(void)
  11083. {
  11084. int ret = 0;
  11085. #if defined(COMPLEX_MEM_TEST) || defined(WOLFSSL_STATIC_MEMORY)
  11086. int i;
  11087. #endif
  11088. #ifdef WOLFSSL_STATIC_MEMORY
  11089. word32 size[] = { WOLFMEM_BUCKETS };
  11090. word32 dist[] = { WOLFMEM_DIST };
  11091. byte buffer[30000]; /* make large enough to involve many bucket sizes */
  11092. int pad = -(int)((wc_ptr_t)buffer) & (WOLFSSL_STATIC_ALIGN - 1);
  11093. /* pad to account for if head of buffer is not at set memory
  11094. * alignment when tests are ran */
  11095. #endif
  11096. #ifdef WOLFSSL_STATIC_MEMORY
  11097. /* check macro settings */
  11098. if (sizeof(size)/sizeof(word32) != WOLFMEM_MAX_BUCKETS) {
  11099. return -7200;
  11100. }
  11101. if (sizeof(dist)/sizeof(word32) != WOLFMEM_MAX_BUCKETS) {
  11102. return -7201;
  11103. }
  11104. for (i = 0; i < WOLFMEM_MAX_BUCKETS; i++) {
  11105. if ((size[i] % WOLFSSL_STATIC_ALIGN) != 0) {
  11106. /* each element in array should be divisible by alignment size */
  11107. return -7202;
  11108. }
  11109. }
  11110. for (i = 1; i < WOLFMEM_MAX_BUCKETS; i++) {
  11111. if (size[i - 1] >= size[i]) {
  11112. return -7203; /* sizes should be in increasing order */
  11113. }
  11114. }
  11115. /* check that padding size returned is possible */
  11116. if (wolfSSL_MemoryPaddingSz() < WOLFSSL_STATIC_ALIGN) {
  11117. return -7204; /* no room for wc_Memory struct */
  11118. }
  11119. if (wolfSSL_MemoryPaddingSz() < 0) {
  11120. return -7205;
  11121. }
  11122. if (wolfSSL_MemoryPaddingSz() % WOLFSSL_STATIC_ALIGN != 0) {
  11123. return -7206; /* not aligned! */
  11124. }
  11125. /* check function to return optimum buffer size (rounded down) */
  11126. ret = wolfSSL_StaticBufferSz(buffer, sizeof(buffer), WOLFMEM_GENERAL);
  11127. if ((ret - pad) % WOLFSSL_STATIC_ALIGN != 0) {
  11128. return -7207; /* not aligned! */
  11129. }
  11130. if (ret < 0) {
  11131. return -7208;
  11132. }
  11133. if ((unsigned int)ret > sizeof(buffer)) {
  11134. return -7209; /* did not round down as expected */
  11135. }
  11136. if (ret != wolfSSL_StaticBufferSz(buffer, ret, WOLFMEM_GENERAL)) {
  11137. return -7210; /* return value changed when using suggested value */
  11138. }
  11139. ret = wolfSSL_MemoryPaddingSz();
  11140. ret += pad; /* add space that is going to be needed if buffer not aligned */
  11141. if (wolfSSL_StaticBufferSz(buffer, size[0] + ret + 1, WOLFMEM_GENERAL) !=
  11142. (ret + (int)size[0])) {
  11143. return -7211; /* did not round down to nearest bucket value */
  11144. }
  11145. ret = wolfSSL_StaticBufferSz(buffer, sizeof(buffer), WOLFMEM_IO_POOL);
  11146. if ((ret - pad) < 0) {
  11147. return -7212;
  11148. }
  11149. if (((ret - pad) % (WOLFMEM_IO_SZ + wolfSSL_MemoryPaddingSz())) != 0) {
  11150. return -7213; /* not even chunks of memory for IO size */
  11151. }
  11152. if (((ret - pad) % WOLFSSL_STATIC_ALIGN) != 0) {
  11153. return -7214; /* memory not aligned */
  11154. }
  11155. /* check for passing bad or unknown arguments to functions */
  11156. if (wolfSSL_StaticBufferSz(NULL, 1, WOLFMEM_GENERAL) > 0) {
  11157. return -7215;
  11158. }
  11159. if (wolfSSL_StaticBufferSz(buffer, 1, WOLFMEM_GENERAL) != 0) {
  11160. return -7216; /* should round to 0 since struct + bucket will not fit */
  11161. }
  11162. (void)dist; /* avoid static analysis warning of variable not used */
  11163. #endif
  11164. #if defined(WOLFSSL_STATIC_MEMORY) || !defined(WOLFSSL_NO_MALLOC)
  11165. /* simple test */
  11166. ret = simple_mem_test(MEM_TEST_SZ);
  11167. if (ret != 0)
  11168. return ret;
  11169. #endif
  11170. #ifdef COMPLEX_MEM_TEST
  11171. /* test various size blocks */
  11172. for (i = 1; i < MEM_TEST_SZ; i*=2) {
  11173. ret = simple_mem_test(i);
  11174. if (ret != 0)
  11175. return ret;
  11176. }
  11177. #endif
  11178. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_NO_MALLOC)
  11179. /* realloc test */
  11180. {
  11181. byte *c = NULL;
  11182. byte *b = (byte*)XMALLOC(MEM_TEST_SZ, HEAP_HINT,
  11183. DYNAMIC_TYPE_TMP_BUFFER);
  11184. if (b) {
  11185. c = (byte*)XREALLOC(b, MEM_TEST_SZ+sizeof(word32), HEAP_HINT,
  11186. DYNAMIC_TYPE_TMP_BUFFER);
  11187. if (c)
  11188. b = c;
  11189. }
  11190. if (b)
  11191. XFREE(b, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11192. if ((b == NULL) || (c == NULL)) {
  11193. return -7217;
  11194. }
  11195. }
  11196. #endif
  11197. return ret;
  11198. }
  11199. #ifndef NO_FILESYSTEM
  11200. /* Cert Paths */
  11201. #ifdef FREESCALE_MQX
  11202. #define CERT_PREFIX "a:\\"
  11203. #define CERT_PATH_SEP "\\"
  11204. #elif defined(WOLFSSL_uTKERNEL2)
  11205. #define CERT_PREFIX "/uda/"
  11206. #define CERT_PATH_SEP "/"
  11207. #elif defined(_WIN32_WCE)
  11208. #define CERT_PREFIX "\\windows\\"
  11209. #define CERT_PATH_SEP "\\"
  11210. #endif
  11211. #ifndef CERT_PREFIX
  11212. #define CERT_PREFIX "./"
  11213. #endif
  11214. #ifndef CERT_PATH_SEP
  11215. #define CERT_PATH_SEP "/"
  11216. #endif
  11217. #ifndef CERT_WRITE_TEMP_DIR
  11218. #define CERT_WRITE_TEMP_DIR CERT_PREFIX
  11219. #endif
  11220. #define CERT_ROOT CERT_PREFIX "certs" CERT_PATH_SEP
  11221. /* Generated Test Certs */
  11222. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  11223. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096)
  11224. #if !defined(NO_RSA) && !defined(NO_ASN)
  11225. static const char* clientKey = CERT_ROOT "client-key.der";
  11226. static const char* clientCert = CERT_ROOT "client-cert.der";
  11227. #ifdef WOLFSSL_CERT_EXT
  11228. static const char* clientKeyPub = CERT_ROOT "client-keyPub.der";
  11229. #endif
  11230. #endif /* !NO_RSA && !NO_ASN */
  11231. #endif
  11232. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  11233. #if !defined(NO_RSA) && !defined(NO_ASN)
  11234. #if defined(WOLFSSL_CERT_GEN) || defined(HAVE_PKCS7)
  11235. static const char* rsaCaKeyFile = CERT_ROOT "ca-key.der";
  11236. #ifdef WOLFSSL_CERT_GEN
  11237. static const char* rsaCaCertFile = CERT_ROOT "ca-cert.pem";
  11238. #endif
  11239. #if defined(WOLFSSL_ALT_NAMES) || defined(HAVE_PKCS7)
  11240. static const char* rsaCaCertDerFile = CERT_ROOT "ca-cert.der";
  11241. #endif
  11242. #ifdef HAVE_PKCS7
  11243. static const char* rsaServerCertDerFile =
  11244. CERT_ROOT "server-cert.der";
  11245. static const char* rsaServerKeyDerFile =
  11246. CERT_ROOT "server-key.der";
  11247. #endif
  11248. #endif
  11249. #endif /* !NO_RSA && !NO_ASN */
  11250. #endif /* !USE_CERT_BUFFER_* */
  11251. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  11252. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096)
  11253. #if !defined(NO_ASN) && !defined(NO_DH)
  11254. static const char* dhParamsFile = CERT_ROOT "dh2048.der";
  11255. #endif
  11256. #endif
  11257. #if !defined(NO_ASN) && !defined(NO_DH)
  11258. #if defined(WOLFSSL_DH_EXTRA) && (!defined(HAVE_FIPS) || \
  11259. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  11260. static const char* dhKeyFile = CERT_ROOT "statickeys/dh-ffdhe2048.der";
  11261. static const char* dhKeyPubFile = CERT_ROOT "statickeys/dh-ffdhe2048-pub.der";
  11262. #endif
  11263. #endif
  11264. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  11265. #ifndef NO_DSA
  11266. static const char* dsaKey = CERT_ROOT "dsa2048.der";
  11267. #endif
  11268. #endif /* !USE_CERT_BUFFER_* */
  11269. #if !defined(USE_CERT_BUFFERS_256) && !defined(NO_ECC256)
  11270. #ifdef HAVE_ECC
  11271. /* cert files to be used in rsa cert gen test, check if RSA enabled */
  11272. #ifdef HAVE_ECC_KEY_IMPORT
  11273. static const char* eccKeyDerFile = CERT_ROOT "ecc-key.der";
  11274. #endif
  11275. #endif
  11276. #if !defined(USE_CERT_BUFFERS_256) && !defined(NO_ASN)
  11277. #if defined(HAVE_ECC) && defined(WOLFSSL_CERT_GEN)
  11278. #ifndef NO_RSA
  11279. static const char* eccKeyPubFileDer = CERT_ROOT "ecc-keyPub.der";
  11280. #endif
  11281. static const char* eccCaKeyFile = CERT_ROOT "ca-ecc-key.der";
  11282. static const char* eccCaCertFile = CERT_ROOT "ca-ecc-cert.pem";
  11283. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  11284. static const char* eccCaKey384File =
  11285. CERT_ROOT "ca-ecc384-key.der";
  11286. static const char* eccCaCert384File =
  11287. CERT_ROOT "ca-ecc384-cert.pem";
  11288. #endif
  11289. #endif
  11290. #if defined(HAVE_PKCS7) && defined(HAVE_ECC)
  11291. static const char* eccClientKey = CERT_ROOT "ecc-client-key.der";
  11292. static const char* eccClientCert = CERT_ROOT "client-ecc-cert.der";
  11293. #endif
  11294. #endif /* HAVE_ECC */
  11295. #ifdef HAVE_ED25519
  11296. #ifdef WOLFSSL_TEST_CERT
  11297. static const char* serverEd25519Cert =
  11298. CERT_ROOT "ed25519/server-ed25519.der";
  11299. static const char* caEd25519Cert =
  11300. CERT_ROOT "ed25519/ca-ed25519.der";
  11301. #endif
  11302. #endif
  11303. #ifdef HAVE_ED448
  11304. #ifdef WOLFSSL_TEST_CERT
  11305. static const char* serverEd448Cert =
  11306. CERT_ROOT "ed448/server-ed448.der";
  11307. static const char* caEd448Cert = CERT_ROOT "ed448/ca-ed448.der";
  11308. #endif
  11309. #endif
  11310. #endif /* !USE_CERT_BUFFER_* */
  11311. #if !defined(NO_ASN_TIME) && !defined(NO_RSA) && defined(WOLFSSL_TEST_CERT) && \
  11312. !defined(NO_FILESYSTEM)
  11313. static const char* certExtNc =
  11314. CERT_ROOT "test" CERT_PATH_SEP "cert-ext-nc.der";
  11315. static const char* certExtIa =
  11316. CERT_ROOT "test" CERT_PATH_SEP "cert-ext-ia.der";
  11317. static const char* certExtNct =
  11318. CERT_ROOT "test" CERT_PATH_SEP "cert-ext-nct.der";
  11319. #endif
  11320. #ifndef NO_WRITE_TEMP_FILES
  11321. #ifdef HAVE_ECC
  11322. #ifdef WOLFSSL_CERT_GEN
  11323. static const char* certEccPemFile = CERT_WRITE_TEMP_DIR "certecc.pem";
  11324. static const char* certEccDerFile = CERT_WRITE_TEMP_DIR "certecc.der";
  11325. #endif
  11326. #if defined(WOLFSSL_CERT_GEN) && !defined(NO_RSA)
  11327. static const char* certEccRsaPemFile = CERT_WRITE_TEMP_DIR "certeccrsa.pem";
  11328. static const char* certEccRsaDerFile = CERT_WRITE_TEMP_DIR "certeccrsa.der";
  11329. #endif
  11330. #if defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG) && \
  11331. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  11332. static const char* eccCaKeyPemFile = CERT_WRITE_TEMP_DIR "ecc-key.pem";
  11333. static const char* eccPubKeyDerFile = CERT_WRITE_TEMP_DIR "ecc-public-key.der";
  11334. static const char* eccCaKeyTempFile = CERT_WRITE_TEMP_DIR "ecc-key.der";
  11335. #if defined(HAVE_PKCS8) && !defined(WC_NO_RNG) && \
  11336. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  11337. static const char* eccPkcs8KeyDerFile = CERT_WRITE_TEMP_DIR "ecc-key-pkcs8.der";
  11338. #endif
  11339. #endif /* HAVE_ECC_KEY_EXPORT */
  11340. #endif /* HAVE_ECC */
  11341. #ifndef NO_RSA
  11342. #ifdef WOLFSSL_CERT_GEN
  11343. static const char* otherCertDerFile = CERT_WRITE_TEMP_DIR "othercert.der";
  11344. static const char* certDerFile = CERT_WRITE_TEMP_DIR "cert.der";
  11345. static const char* otherCertPemFile = CERT_WRITE_TEMP_DIR "othercert.pem";
  11346. static const char* certPemFile = CERT_WRITE_TEMP_DIR "cert.pem";
  11347. #if defined(WOLFSSL_CERT_REQ) && !defined(WOLFSSL_NO_MALLOC)
  11348. static const char* certReqDerFile = CERT_WRITE_TEMP_DIR "certreq.der";
  11349. static const char* certReqPemFile = CERT_WRITE_TEMP_DIR "certreq.pem";
  11350. #endif
  11351. #endif
  11352. #endif /* !NO_RSA */
  11353. #if !defined(NO_RSA) || !defined(NO_DSA)
  11354. #ifdef WOLFSSL_KEY_GEN
  11355. static const char* keyDerFile = CERT_WRITE_TEMP_DIR "key.der";
  11356. static const char* keyPemFile = CERT_WRITE_TEMP_DIR "key.pem";
  11357. #endif
  11358. #endif
  11359. #endif /* !NO_WRITE_TEMP_FILES */
  11360. #endif /* !NO_FILESYSTEM */
  11361. #if defined(WOLFSSL_CERT_GEN) && (!defined(NO_RSA) || defined(HAVE_ECC)) || \
  11362. (defined(WOLFSSL_TEST_CERT) && (defined(HAVE_ED25519) || defined(HAVE_ED448)))
  11363. static CertName certDefaultName;
  11364. static void initDefaultName(void)
  11365. {
  11366. #if defined(WOLFSSL_MULTI_ATTRIB) && defined(WOLFSSL_TEST_CERT)
  11367. NameAttrib* n;
  11368. #endif
  11369. XMEMCPY(certDefaultName.country, "US", sizeof("US"));
  11370. certDefaultName.countryEnc = CTC_PRINTABLE;
  11371. XMEMCPY(certDefaultName.state, "Oregon", sizeof("Oregon"));
  11372. certDefaultName.stateEnc = CTC_UTF8;
  11373. XMEMCPY(certDefaultName.street, "Main St", sizeof("Main St"));
  11374. certDefaultName.streetEnc = CTC_UTF8;
  11375. XMEMCPY(certDefaultName.locality, "Portland", sizeof("Portland"));
  11376. certDefaultName.localityEnc = CTC_UTF8;
  11377. XMEMCPY(certDefaultName.sur, "Test", sizeof("Test"));
  11378. certDefaultName.surEnc = CTC_UTF8;
  11379. XMEMCPY(certDefaultName.org, "wolfSSL", sizeof("wolfSSL"));
  11380. certDefaultName.orgEnc = CTC_UTF8;
  11381. XMEMCPY(certDefaultName.unit, "Development", sizeof("Development"));
  11382. certDefaultName.unitEnc = CTC_UTF8;
  11383. XMEMCPY(certDefaultName.commonName, "www.wolfssl.com", sizeof("www.wolfssl.com"));
  11384. certDefaultName.commonNameEnc = CTC_UTF8;
  11385. XMEMCPY(certDefaultName.serialDev, "wolfSSL12345", sizeof("wolfSSL12345"));
  11386. certDefaultName.serialDevEnc = CTC_PRINTABLE;
  11387. XMEMCPY(certDefaultName.postalCode, "12-456", sizeof("12-456"));
  11388. certDefaultName.postalCodeEnc = CTC_PRINTABLE;
  11389. #ifdef WOLFSSL_CERT_EXT
  11390. XMEMCPY(certDefaultName.busCat, "Private Organization", sizeof("Private Organization"));
  11391. certDefaultName.busCatEnc = CTC_UTF8;
  11392. XMEMCPY(certDefaultName.joiSt, "US", sizeof("US"));
  11393. certDefaultName.joiStEnc = CTC_PRINTABLE;
  11394. XMEMCPY(certDefaultName.joiC, "Oregon", sizeof("Oregon"));
  11395. certDefaultName.joiCEnc = CTC_PRINTABLE;
  11396. #endif
  11397. XMEMCPY(certDefaultName.email, "info@wolfssl.com", sizeof("info@wolfssl.com"));
  11398. XMEMCPY(certDefaultName.userId, "TestUserID", sizeof("TestUserID"));
  11399. certDefaultName.userIdEnc = CTC_PRINTABLE;
  11400. #if defined(WOLFSSL_MULTI_ATTRIB) && defined(WOLFSSL_TEST_CERT)
  11401. /* test having additional OUs and setting DC */
  11402. n = &certDefaultName.name[0];
  11403. n->id = ASN_ORGUNIT_NAME;
  11404. n->type = CTC_UTF8;
  11405. n->sz = sizeof("Development-2");
  11406. XMEMCPY(n->value, "Development-2", sizeof("Development-2"));
  11407. #if CTC_MAX_ATTRIB > 3
  11408. n = &certDefaultName.name[1];
  11409. n->id = ASN_DOMAIN_COMPONENT;
  11410. n->type = CTC_UTF8;
  11411. n->sz = sizeof("com");
  11412. XMEMCPY(n->value, "com", sizeof("com"));
  11413. n = &certDefaultName.name[2];
  11414. n->id = ASN_DOMAIN_COMPONENT;
  11415. n->type = CTC_UTF8;
  11416. n->sz = sizeof("wolfssl");
  11417. XMEMCPY(n->value, "wolfssl", sizeof("wolfssl"));
  11418. #endif
  11419. #endif /* WOLFSSL_MULTI_ATTRIB && WOLFSSL_TEST_CERT */
  11420. #ifdef WOLFSSL_CUSTOM_OID
  11421. /* TODO: Add test case for custom OID's */
  11422. #endif
  11423. }
  11424. #ifdef WOLFSSL_CERT_EXT
  11425. #if ((defined(HAVE_ED25519) || defined(HAVE_ED448)) && \
  11426. defined(WOLFSSL_TEST_CERT)) || defined(HAVE_ECC)
  11427. WOLFSSL_SMALL_STACK_STATIC const char certKeyUsage[] =
  11428. "digitalSignature,nonRepudiation";
  11429. #endif
  11430. #if defined(WOLFSSL_CERT_REQ) && !defined(NO_RSA)
  11431. WOLFSSL_SMALL_STACK_STATIC const char certKeyUsage2[] =
  11432. "digitalSignature,nonRepudiation,keyEncipherment,keyAgreement";
  11433. #endif
  11434. #endif /* WOLFSSL_CERT_EXT */
  11435. #endif /* WOLFSSL_CERT_GEN */
  11436. #ifndef NO_RSA
  11437. #if !defined(NO_ASN_TIME) && !defined(NO_RSA) && defined(WOLFSSL_TEST_CERT) && \
  11438. !defined(NO_FILESYSTEM)
  11439. static byte minSerial[] = { 0x02, 0x01, 0x01 };
  11440. static byte minName[] = { 0x30, 0x00 };
  11441. static byte nameBad[] = {
  11442. 0x30, 0x08,
  11443. 0x31, 0x06,
  11444. 0x30, 0x04,
  11445. 0x06, 0x02,
  11446. 0x55, 0x04,
  11447. };
  11448. static byte minDates[] = {
  11449. 0x30, 0x1e,
  11450. 0x17, 0x0d,
  11451. 0x31, 0x38, 0x30, 0x34, 0x31, 0x33, 0x31, 0x35,
  11452. 0x32, 0x33, 0x31, 0x30, 0x5a,
  11453. 0x17, 0x0d,
  11454. 0x32, 0x31, 0x30, 0x31, 0x30, 0x37, 0x31, 0x35,
  11455. 0x32, 0x33, 0x31, 0x30, 0x5a
  11456. };
  11457. static byte minPubKey[] = {
  11458. 0x30, 0x1c,
  11459. 0x30, 0x0d,
  11460. 0x06, 0x09,
  11461. 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01,
  11462. 0x01,
  11463. 0x05, 0x00,
  11464. 0x03, 0x0b,
  11465. 0x00, 0x30, 0x08,
  11466. 0x02, 0x01,
  11467. 0x03,
  11468. 0x02, 0x03,
  11469. 0x01, 0x00, 0x01
  11470. };
  11471. static byte minSigAlg[] = {
  11472. 0x30, 0x0d,
  11473. 0x06, 0x09,
  11474. 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01,
  11475. 0x0b,
  11476. 0x05, 0x00
  11477. };
  11478. static byte minSig[] = {
  11479. 0x03, 0x01,
  11480. 0x00
  11481. };
  11482. static int add_seq(byte* certData, int offset, byte* data, byte length)
  11483. {
  11484. XMEMMOVE(certData + offset + 2, data, length);
  11485. certData[offset++] = 0x30;
  11486. certData[offset++] = length;
  11487. return offset + length;
  11488. }
  11489. static int add_data(byte* certData, int offset, byte* data, byte length)
  11490. {
  11491. XMEMCPY(certData + offset, data, length);
  11492. return offset + length;
  11493. }
  11494. static int cert_asn1_test(void)
  11495. {
  11496. int ret;
  11497. int len[3];
  11498. DecodedCert cert;
  11499. byte certData[114];
  11500. byte* badCert = NULL;
  11501. len[2] = add_data(certData, 0, minSerial, (byte)sizeof(minSerial));
  11502. len[2] = add_data(certData, len[2], minSigAlg, (byte)sizeof(minSigAlg));
  11503. len[2] = add_data(certData, len[2], minName, (byte)sizeof(minName));
  11504. len[2] = add_data(certData, len[2], minDates, (byte)sizeof(minDates));
  11505. len[2] = add_data(certData, len[2], minName, (byte)sizeof(minName));
  11506. len[2] = add_data(certData, len[2], minPubKey, (byte)sizeof(minPubKey));
  11507. len[1] = add_seq(certData, 0, certData, len[2]);
  11508. len[1] = add_data(certData, len[1], minSigAlg, (byte)sizeof(minSigAlg));
  11509. len[1] = add_data(certData, len[1], minSig, (byte)sizeof(minSig));
  11510. len[0] = add_seq(certData, 0, certData, len[1]);
  11511. /* Minimal good certificate */
  11512. InitDecodedCert(&cert, certData, len[0], 0);
  11513. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL);
  11514. FreeDecodedCert(&cert);
  11515. if (ret != 0) {
  11516. ERROR_OUT(-7300, done);
  11517. }
  11518. /* Bad issuer name */
  11519. len[2] = add_data(certData, 0, minSerial, (byte)sizeof(minSerial));
  11520. len[2] = add_data(certData, len[2], minSigAlg, (byte)sizeof(minSigAlg));
  11521. len[2] = add_data(certData, len[2], nameBad, (byte)sizeof(nameBad));
  11522. len[2] = add_data(certData, len[2], minDates, (byte)sizeof(minDates));
  11523. len[2] = add_data(certData, len[2], minName, (byte)sizeof(minName));
  11524. len[2] = add_data(certData, len[2], minPubKey, (byte)sizeof(minPubKey));
  11525. len[1] = add_seq(certData, 0, certData, len[2]);
  11526. len[1] = add_data(certData, len[1], minSigAlg, (byte)sizeof(minSigAlg));
  11527. len[1] = add_data(certData, len[1], minSig, (byte)sizeof(minSig));
  11528. len[0] = add_seq(certData, 0, certData, len[1]);
  11529. /* Put data into allocated buffer to allow access error checking. */
  11530. badCert = (byte*)XMALLOC(len[0], HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11531. XMEMCPY(badCert, certData, len[0]);
  11532. InitDecodedCert(&cert, badCert, len[0], 0);
  11533. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL);
  11534. FreeDecodedCert(&cert);
  11535. if (ret != ASN_PARSE_E) {
  11536. ERROR_OUT(-7301, done);
  11537. }
  11538. XFREE(badCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11539. badCert = NULL;
  11540. ret = 0;
  11541. done:
  11542. if (badCert != NULL)
  11543. XFREE(badCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11544. return ret;
  11545. }
  11546. WOLFSSL_TEST_SUBROUTINE int cert_test(void)
  11547. {
  11548. #if !defined(NO_FILESYSTEM)
  11549. DecodedCert cert;
  11550. byte* tmp;
  11551. size_t bytes;
  11552. XFILE file;
  11553. int ret;
  11554. tmp = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11555. if (tmp == NULL)
  11556. return -7400;
  11557. /* Certificate with Name Constraints extension. */
  11558. file = XFOPEN(certExtNc, "rb");
  11559. if (!file) {
  11560. ERROR_OUT(-7401, done);
  11561. }
  11562. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  11563. XFCLOSE(file);
  11564. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  11565. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL);
  11566. if (ret != 0) {
  11567. ERROR_OUT(-7402, done);
  11568. }
  11569. FreeDecodedCert(&cert);
  11570. /* Certificate with Inhibit Any Policy extension. */
  11571. file = XFOPEN(certExtIa, "rb");
  11572. if (!file) {
  11573. ERROR_OUT(-7403, done);
  11574. }
  11575. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  11576. XFCLOSE(file);
  11577. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  11578. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL);
  11579. if (ret != 0) {
  11580. ERROR_OUT(-7404, done);
  11581. }
  11582. FreeDecodedCert(&cert);
  11583. /* Certificate with Netscape Certificate Type extension. */
  11584. file = XFOPEN(certExtNct, "rb");
  11585. if (!file) {
  11586. ERROR_OUT(-7405, done);
  11587. }
  11588. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  11589. XFCLOSE(file);
  11590. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  11591. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL);
  11592. #ifndef IGNORE_NETSCAPE_CERT_TYPE
  11593. if (ret != 0) {
  11594. ERROR_OUT(-7406, done);
  11595. }
  11596. #else
  11597. if (ret != ASN_CRIT_EXT_E) {
  11598. ERROR_OUT(-7407, done);
  11599. }
  11600. ret = 0;
  11601. #endif
  11602. done:
  11603. FreeDecodedCert(&cert);
  11604. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11605. #endif /* !NO_FILESYSTEM */
  11606. if (ret == 0)
  11607. ret = cert_asn1_test();
  11608. return ret;
  11609. }
  11610. #endif /* WOLFSSL_TEST_CERT */
  11611. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_TEST_CERT) && \
  11612. !defined(NO_FILESYSTEM) && defined(WOLFSSL_CERT_GEN)
  11613. WOLFSSL_TEST_SUBROUTINE int certext_test(void)
  11614. {
  11615. DecodedCert cert;
  11616. byte* tmp;
  11617. size_t bytes;
  11618. XFILE file;
  11619. int ret;
  11620. /* created from rsa_test : othercert.der */
  11621. byte skid_rsa[] = "\x33\xD8\x45\x66\xD7\x68\x87\x18\x7E\x54"
  11622. "\x0D\x70\x27\x91\xC7\x26\xD7\x85\x65\xC0";
  11623. /* created from rsa_test : othercert.der */
  11624. byte akid_rsa[] = "\x27\x8E\x67\x11\x74\xC3\x26\x1D\x3F\xED"
  11625. "\x33\x63\xB3\xA4\xD8\x1D\x30\xE5\xE8\xD5";
  11626. #ifdef HAVE_ECC
  11627. /* created from ecc_test_cert_gen : certecc.der */
  11628. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  11629. /* Authority key id from ./certs/ca-ecc384-cert.pem */
  11630. byte akid_ecc[] = "\xAB\xE0\xC3\x26\x4C\x18\xD4\x72\xBB\xD2"
  11631. "\x84\x8C\x9C\x0A\x05\x92\x80\x12\x53\x52";
  11632. #else
  11633. /* Authority key id from ./certs/ca-ecc-cert.pem */
  11634. byte akid_ecc[] = "\x56\x8E\x9A\xC3\xF0\x42\xDE\x18\xB9\x45"
  11635. "\x55\x6E\xF9\x93\xCF\xEA\xC3\xF3\xA5\x21";
  11636. #endif
  11637. #endif /* HAVE_ECC */
  11638. /* created from rsa_test : cert.der */
  11639. byte kid_ca[] = "\x33\xD8\x45\x66\xD7\x68\x87\x18\x7E\x54"
  11640. "\x0D\x70\x27\x91\xC7\x26\xD7\x85\x65\xC0";
  11641. tmp = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11642. if (tmp == NULL)
  11643. return -7500;
  11644. /* load othercert.der (Cert signed by an authority) */
  11645. file = XFOPEN(otherCertDerFile, "rb");
  11646. if (!file) {
  11647. XFREE(tmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  11648. return -7501;
  11649. }
  11650. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  11651. XFCLOSE(file);
  11652. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  11653. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, 0);
  11654. if (ret != 0)
  11655. return -7502;
  11656. /* check the SKID from a RSA certificate */
  11657. if (XMEMCMP(skid_rsa, cert.extSubjKeyId, sizeof(cert.extSubjKeyId)))
  11658. return -7503;
  11659. /* check the AKID from an RSA certificate */
  11660. if (XMEMCMP(akid_rsa, cert.extAuthKeyId, sizeof(cert.extAuthKeyId)))
  11661. return -7504;
  11662. /* check the Key Usage from an RSA certificate */
  11663. if (!cert.extKeyUsageSet)
  11664. return -7505;
  11665. if (cert.extKeyUsage != (KEYUSE_KEY_ENCIPHER|KEYUSE_KEY_AGREE))
  11666. return -7506;
  11667. /* check the CA Basic Constraints from an RSA certificate */
  11668. if (cert.isCA)
  11669. return -7507;
  11670. #ifndef WOLFSSL_SEP /* test only if not using SEP policies */
  11671. /* check the Certificate Policies Id */
  11672. if (cert.extCertPoliciesNb != 1)
  11673. return -7508;
  11674. if (strncmp(cert.extCertPolicies[0], "2.16.840.1.101.3.4.1.42", 23))
  11675. return -7509;
  11676. #endif
  11677. FreeDecodedCert(&cert);
  11678. #ifdef HAVE_ECC
  11679. /* load certecc.der (Cert signed by our ECC CA test in ecc_test_cert_gen) */
  11680. file = XFOPEN(certEccDerFile, "rb");
  11681. if (!file) {
  11682. XFREE(tmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  11683. return -7510;
  11684. }
  11685. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  11686. XFCLOSE(file);
  11687. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  11688. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, 0);
  11689. if (ret != 0)
  11690. return -7511;
  11691. /* check the SKID from a ECC certificate - generated dynamically */
  11692. /* check the AKID from an ECC certificate */
  11693. if (XMEMCMP(akid_ecc, cert.extAuthKeyId, sizeof(cert.extAuthKeyId)))
  11694. return -7512;
  11695. /* check the Key Usage from an ECC certificate */
  11696. if (!cert.extKeyUsageSet)
  11697. return -7513;
  11698. if (cert.extKeyUsage != (KEYUSE_DIGITAL_SIG|KEYUSE_CONTENT_COMMIT))
  11699. return -7514;
  11700. /* check the CA Basic Constraints from an ECC certificate */
  11701. if (cert.isCA)
  11702. return -7515;
  11703. #ifndef WOLFSSL_SEP /* test only if not using SEP policies */
  11704. /* check the Certificate Policies Id */
  11705. if (cert.extCertPoliciesNb != 2)
  11706. return -7516;
  11707. if (strncmp(cert.extCertPolicies[0], "2.4.589440.587.101.2.1.9632587.1", 32))
  11708. return -7517;
  11709. if (strncmp(cert.extCertPolicies[1], "1.2.13025.489.1.113549", 22))
  11710. return -7518;
  11711. #endif
  11712. FreeDecodedCert(&cert);
  11713. #endif /* HAVE_ECC */
  11714. /* load cert.der (self signed certificate) */
  11715. file = XFOPEN(certDerFile, "rb");
  11716. if (!file) {
  11717. XFREE(tmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  11718. return -7519;
  11719. }
  11720. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  11721. XFCLOSE(file);
  11722. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  11723. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, 0);
  11724. if (ret != 0)
  11725. return -7520;
  11726. /* check the SKID from a CA certificate */
  11727. if (XMEMCMP(kid_ca, cert.extSubjKeyId, sizeof(cert.extSubjKeyId)))
  11728. return -7521;
  11729. /* check the AKID from an CA certificate */
  11730. if (XMEMCMP(kid_ca, cert.extAuthKeyId, sizeof(cert.extAuthKeyId)))
  11731. return -7522;
  11732. /* check the Key Usage from CA certificate */
  11733. if (!cert.extKeyUsageSet)
  11734. return -7523;
  11735. if (cert.extKeyUsage != (KEYUSE_KEY_CERT_SIGN|KEYUSE_CRL_SIGN))
  11736. return -7524;
  11737. /* check the CA Basic Constraints CA certificate */
  11738. if (!cert.isCA)
  11739. return -7525;
  11740. #ifndef WOLFSSL_SEP /* test only if not using SEP policies */
  11741. /* check the Certificate Policies Id */
  11742. if (cert.extCertPoliciesNb != 2)
  11743. return -7526;
  11744. if (strncmp(cert.extCertPolicies[0], "2.16.840.1.101.3.4.1.42", 23))
  11745. return -7527;
  11746. if (strncmp(cert.extCertPolicies[1], "1.2.840.113549.1.9.16.6.5", 25))
  11747. return -7528;
  11748. #endif
  11749. FreeDecodedCert(&cert);
  11750. XFREE(tmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  11751. return 0;
  11752. }
  11753. #endif /* WOLFSSL_CERT_EXT && WOLFSSL_TEST_CERT &&
  11754. !NO_FILESYSTEM && WOLFSSL_CERT_GEN */
  11755. #if defined(WOLFSSL_CERT_GEN_CACHE) && defined(WOLFSSL_TEST_CERT) && \
  11756. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  11757. WOLFSSL_TEST_SUBROUTINE int decodedCertCache_test(void)
  11758. {
  11759. int ret = 0;
  11760. Cert cert;
  11761. FILE* file;
  11762. byte* der;
  11763. word32 derSz;
  11764. derSz = FOURK_BUF;
  11765. der = (byte *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11766. if (der == NULL)
  11767. ret = -7600;
  11768. if (ret == 0) {
  11769. /* load cert.der */
  11770. file = XFOPEN(certDerFile, "rb");
  11771. if (file != NULL) {
  11772. derSz = (word32)XFREAD(der, 1, FOURK_BUF, file);
  11773. XFCLOSE(file);
  11774. }
  11775. else
  11776. ret = -7601;
  11777. }
  11778. if (ret == 0) {
  11779. if (wc_InitCert_ex(&cert, HEAP_HINT, devId)) {
  11780. ret = -7602;
  11781. }
  11782. }
  11783. if (ret == 0) {
  11784. ret = wc_SetSubjectBuffer(&cert, der, derSz);
  11785. }
  11786. if (ret == 0) {
  11787. if(wc_SetSubjectBuffer(NULL, der, derSz) != BAD_FUNC_ARG)
  11788. ret = -7603;
  11789. }
  11790. if (ret == 0) {
  11791. if (wc_SetSubjectRaw(&cert, der, derSz) != 0)
  11792. ret = -7604;
  11793. }
  11794. if (ret == 0) {
  11795. if(wc_SetSubjectRaw(NULL, der, derSz) != BAD_FUNC_ARG)
  11796. ret = -7605;
  11797. }
  11798. if (ret == 0) {
  11799. if(wc_SetIssuerBuffer(&cert, der, derSz) != 0)
  11800. ret = -7606;
  11801. }
  11802. if (ret == 0) {
  11803. if(wc_SetIssuerBuffer(NULL, der, derSz) != BAD_FUNC_ARG)
  11804. ret = -7607;
  11805. }
  11806. if (ret == 0) {
  11807. if(wc_SetIssuerRaw(&cert, der, derSz) != 0)
  11808. ret = -7608;
  11809. }
  11810. if (ret == 0) {
  11811. if(wc_SetIssuerRaw(NULL, der, derSz) != BAD_FUNC_ARG)
  11812. ret = -7609;
  11813. }
  11814. #ifdef WOLFSSL_ALT_NAMES
  11815. if (ret == 0) {
  11816. if(wc_SetAltNamesBuffer(&cert, der, derSz) != 0)
  11817. ret = -7610;
  11818. }
  11819. if (ret == 0) {
  11820. if(wc_SetAltNamesBuffer(NULL, der, derSz) != BAD_FUNC_ARG)
  11821. ret = -7611;
  11822. }
  11823. if (ret == 0) {
  11824. if(wc_SetDatesBuffer(&cert, der, derSz) != 0)
  11825. ret = -7612;
  11826. }
  11827. if (ret == 0) {
  11828. if(wc_SetDatesBuffer(NULL, der, derSz) != BAD_FUNC_ARG)
  11829. ret = -7613;
  11830. }
  11831. #endif
  11832. if (ret == 0) {
  11833. if(wc_SetAuthKeyIdFromCert(&cert, der, derSz) != 0)
  11834. ret = -7614;
  11835. }
  11836. if (ret == 0) {
  11837. if(wc_SetAuthKeyIdFromCert(NULL, der, derSz) != BAD_FUNC_ARG)
  11838. ret = -7615;
  11839. }
  11840. wc_SetCert_Free(&cert);
  11841. if (ret == 0) {
  11842. if(cert.decodedCert != NULL)
  11843. ret = -7616;
  11844. }
  11845. XFREE(der, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  11846. return ret;
  11847. }
  11848. #endif /* defined(WOLFSSL_CERT_GEN_CACHE) && defined(WOLFSSL_TEST_CERT) &&
  11849. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN) */
  11850. #define RSA_TEST_BYTES 512 /* up to 4096-bit key */
  11851. #if !defined(NO_ASN) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
  11852. !defined(WOLFSSL_RSA_VERIFY_ONLY)
  11853. static int rsa_flatten_test(RsaKey* key)
  11854. {
  11855. int ret;
  11856. byte e[RSA_TEST_BYTES];
  11857. byte n[RSA_TEST_BYTES];
  11858. word32 eSz = sizeof(e);
  11859. word32 nSz = sizeof(n);
  11860. /* Parameter Validation testing. */
  11861. ret = wc_RsaFlattenPublicKey(NULL, e, &eSz, n, &nSz);
  11862. #ifdef HAVE_USER_RSA
  11863. /* Implementation using IPP Libraries returns:
  11864. * -101 = USER_CRYPTO_ERROR
  11865. */
  11866. if (ret == 0)
  11867. #else
  11868. if (ret != BAD_FUNC_ARG)
  11869. #endif
  11870. return -7620;
  11871. ret = wc_RsaFlattenPublicKey(key, NULL, &eSz, n, &nSz);
  11872. #ifdef HAVE_USER_RSA
  11873. /* Implementation using IPP Libraries returns:
  11874. * -101 = USER_CRYPTO_ERROR
  11875. */
  11876. if (ret == 0)
  11877. #else
  11878. if (ret != BAD_FUNC_ARG)
  11879. #endif
  11880. return -7621;
  11881. ret = wc_RsaFlattenPublicKey(key, e, NULL, n, &nSz);
  11882. #ifdef HAVE_USER_RSA
  11883. /* Implementation using IPP Libraries returns:
  11884. * -101 = USER_CRYPTO_ERROR
  11885. */
  11886. if (ret == 0)
  11887. #else
  11888. if (ret != BAD_FUNC_ARG)
  11889. #endif
  11890. return -7622;
  11891. ret = wc_RsaFlattenPublicKey(key, e, &eSz, NULL, &nSz);
  11892. #ifdef HAVE_USER_RSA
  11893. /* Implementation using IPP Libraries returns:
  11894. * -101 = USER_CRYPTO_ERROR
  11895. */
  11896. if (ret == 0)
  11897. #else
  11898. if (ret != BAD_FUNC_ARG)
  11899. #endif
  11900. return -7623;
  11901. ret = wc_RsaFlattenPublicKey(key, e, &eSz, n, NULL);
  11902. #ifdef HAVE_USER_RSA
  11903. /* Implementation using IPP Libraries returns:
  11904. * -101 = USER_CRYPTO_ERROR
  11905. */
  11906. if (ret == 0)
  11907. #else
  11908. if (ret != BAD_FUNC_ARG)
  11909. #endif
  11910. return -7624;
  11911. ret = wc_RsaFlattenPublicKey(key, e, &eSz, n, &nSz);
  11912. if (ret != 0)
  11913. return -7625;
  11914. eSz = 0;
  11915. ret = wc_RsaFlattenPublicKey(key, e, &eSz, n, &nSz);
  11916. #ifdef HAVE_USER_RSA
  11917. /* Implementation using IPP Libraries returns:
  11918. * -101 = USER_CRYPTO_ERROR
  11919. */
  11920. if (ret == 0)
  11921. #elif defined(HAVE_FIPS) && \
  11922. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 2))
  11923. if (ret != 0)
  11924. #else
  11925. if (ret != RSA_BUFFER_E)
  11926. #endif
  11927. return -7626;
  11928. eSz = sizeof(e);
  11929. nSz = 0;
  11930. ret = wc_RsaFlattenPublicKey(key, e, &eSz, n, &nSz);
  11931. #ifdef HAVE_USER_RSA
  11932. /* Implementation using IPP Libraries returns:
  11933. * -101 = USER_CRYPTO_ERROR
  11934. */
  11935. if (ret == 0)
  11936. #else
  11937. if (ret != RSA_BUFFER_E)
  11938. #endif
  11939. return -7627;
  11940. return 0;
  11941. }
  11942. #endif /* NO_ASN */
  11943. #if !defined(HAVE_FIPS) && !defined(HAVE_USER_RSA) && !defined(NO_ASN) \
  11944. && !defined(WOLFSSL_RSA_VERIFY_ONLY)
  11945. static int rsa_export_key_test(RsaKey* key)
  11946. {
  11947. int ret;
  11948. byte e[3];
  11949. word32 eSz = sizeof(e);
  11950. byte n[RSA_TEST_BYTES];
  11951. word32 nSz = sizeof(n);
  11952. byte d[RSA_TEST_BYTES];
  11953. word32 dSz = sizeof(d);
  11954. byte p[RSA_TEST_BYTES/2];
  11955. word32 pSz = sizeof(p);
  11956. byte q[RSA_TEST_BYTES/2];
  11957. word32 qSz = sizeof(q);
  11958. word32 zero = 0;
  11959. ret = wc_RsaExportKey(NULL, e, &eSz, n, &nSz, d, &dSz, p, &pSz, q, &qSz);
  11960. if (ret != BAD_FUNC_ARG)
  11961. return -7630;
  11962. ret = wc_RsaExportKey(key, NULL, &eSz, n, &nSz, d, &dSz, p, &pSz, q, &qSz);
  11963. if (ret != BAD_FUNC_ARG)
  11964. return -7631;
  11965. ret = wc_RsaExportKey(key, e, NULL, n, &nSz, d, &dSz, p, &pSz, q, &qSz);
  11966. if (ret != BAD_FUNC_ARG)
  11967. return -7632;
  11968. ret = wc_RsaExportKey(key, e, &eSz, NULL, &nSz, d, &dSz, p, &pSz, q, &qSz);
  11969. if (ret != BAD_FUNC_ARG)
  11970. return -7633;
  11971. ret = wc_RsaExportKey(key, e, &eSz, n, NULL, d, &dSz, p, &pSz, q, &qSz);
  11972. if (ret != BAD_FUNC_ARG)
  11973. return -7634;
  11974. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, NULL, &dSz, p, &pSz, q, &qSz);
  11975. if (ret != BAD_FUNC_ARG)
  11976. return -7635;
  11977. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, NULL, p, &pSz, q, &qSz);
  11978. if (ret != BAD_FUNC_ARG)
  11979. return -7636;
  11980. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, NULL, &pSz, q, &qSz);
  11981. if (ret != BAD_FUNC_ARG)
  11982. return -7637;
  11983. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, NULL, q, &qSz);
  11984. if (ret != BAD_FUNC_ARG)
  11985. return -7638;
  11986. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, &pSz, NULL, &qSz);
  11987. if (ret != BAD_FUNC_ARG)
  11988. return -7639;
  11989. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, &pSz, q, NULL);
  11990. if (ret != BAD_FUNC_ARG)
  11991. return -7640;
  11992. ret = wc_RsaExportKey(key, e, &zero, n, &nSz, d, &dSz, p, &pSz, q, &qSz);
  11993. if (ret != RSA_BUFFER_E)
  11994. return -7641;
  11995. ret = wc_RsaExportKey(key, e, &eSz, n, &zero, d, &dSz, p, &pSz, q, &qSz);
  11996. if (ret != RSA_BUFFER_E)
  11997. return -7642;
  11998. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  11999. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &zero, p, &pSz, q, &qSz);
  12000. if (ret != RSA_BUFFER_E)
  12001. return -7643;
  12002. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, &zero, q, &qSz);
  12003. if (ret != RSA_BUFFER_E)
  12004. return -7644;
  12005. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, &pSz, q, &zero);
  12006. if (ret != RSA_BUFFER_E)
  12007. return -7645;
  12008. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  12009. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, &pSz, q, &qSz);
  12010. if (ret != 0)
  12011. return -7646;
  12012. return 0;
  12013. }
  12014. #endif /* !HAVE_FIPS && !USER_RSA && !NO_ASN */
  12015. #ifndef NO_SIG_WRAPPER
  12016. static int rsa_sig_test(RsaKey* key, word32 keyLen, int modLen, WC_RNG* rng)
  12017. {
  12018. int ret;
  12019. word32 sigSz;
  12020. WOLFSSL_SMALL_STACK_STATIC const byte in[] = TEST_STRING;
  12021. WOLFSSL_SMALL_STACK_STATIC const byte hash[] = {
  12022. 0xf2, 0x02, 0x95, 0x65, 0xcb, 0xf6, 0x2a, 0x59,
  12023. 0x39, 0x2c, 0x05, 0xff, 0x0e, 0x29, 0xaf, 0xfe,
  12024. 0x47, 0x33, 0x8c, 0x99, 0x8d, 0x58, 0x64, 0x83,
  12025. 0xa6, 0x58, 0x0a, 0x33, 0x0b, 0x84, 0x5f, 0x5f
  12026. };
  12027. WOLFSSL_SMALL_STACK_STATIC const byte hashEnc[] = {
  12028. 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
  12029. 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05,
  12030. 0x00, 0x04, 0x20,
  12031. 0xf2, 0x02, 0x95, 0x65, 0xcb, 0xf6, 0x2a, 0x59,
  12032. 0x39, 0x2c, 0x05, 0xff, 0x0e, 0x29, 0xaf, 0xfe,
  12033. 0x47, 0x33, 0x8c, 0x99, 0x8d, 0x58, 0x64, 0x83,
  12034. 0xa6, 0x58, 0x0a, 0x33, 0x0b, 0x84, 0x5f, 0x5f
  12035. };
  12036. word32 inLen = (word32)XSTRLEN((char*)in);
  12037. byte out[RSA_TEST_BYTES];
  12038. /* Parameter Validation testing. */
  12039. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_NONE, key, keyLen);
  12040. if (ret != BAD_FUNC_ARG)
  12041. return -7650;
  12042. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_RSA, key, 0);
  12043. if (ret != BAD_FUNC_ARG)
  12044. return -7651;
  12045. sigSz = (word32)modLen;
  12046. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, NULL,
  12047. inLen, out, &sigSz, key, keyLen, rng);
  12048. if (ret != BAD_FUNC_ARG)
  12049. return -7652;
  12050. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  12051. 0, out, &sigSz, key, keyLen, rng);
  12052. if (ret != BAD_FUNC_ARG)
  12053. return -7653;
  12054. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  12055. inLen, NULL, &sigSz, key, keyLen, rng);
  12056. if (ret != BAD_FUNC_ARG)
  12057. return -7654;
  12058. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  12059. inLen, out, NULL, key, keyLen, rng);
  12060. if (ret != BAD_FUNC_ARG)
  12061. return -7655;
  12062. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  12063. inLen, out, &sigSz, NULL, keyLen, rng);
  12064. if (ret != BAD_FUNC_ARG)
  12065. return -7656;
  12066. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  12067. inLen, out, &sigSz, key, 0, rng);
  12068. if (ret != BAD_FUNC_ARG)
  12069. return -7657;
  12070. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  12071. inLen, out, &sigSz, key, keyLen, NULL);
  12072. #ifdef HAVE_USER_RSA
  12073. /* Implementation using IPP Libraries returns:
  12074. * -101 = USER_CRYPTO_ERROR
  12075. */
  12076. if (ret == 0)
  12077. #elif defined(WOLFSSL_AFALG_XILINX_RSA) || defined(WOLFSSL_XILINX_CRYPT)
  12078. /* blinding / rng handled with hardware acceleration */
  12079. if (ret != 0)
  12080. #elif defined(WOLFSSL_ASYNC_CRYPT) || defined(WOLF_CRYPTO_CB)
  12081. /* async may not require RNG */
  12082. #if defined(WOLF_CRYPTO_CB_ONLY_RSA)
  12083. if (ret != NO_VALID_DEVID)
  12084. #else
  12085. if (ret != 0 && ret != MISSING_RNG_E)
  12086. #endif
  12087. #elif defined(HAVE_FIPS) || !defined(WC_RSA_BLINDING)
  12088. /* FIPS140 implementation does not do blinding */
  12089. if (ret != 0)
  12090. #elif defined(WOLFSSL_RSA_PUBLIC_ONLY) || defined(WOLFSSL_RSA_VERIFY_ONLY)
  12091. if (ret != SIG_TYPE_E)
  12092. #elif defined(WOLFSSL_CRYPTOCELL)
  12093. /* RNG is handled with the cryptocell */
  12094. if (ret != 0)
  12095. #else
  12096. if (ret != MISSING_RNG_E)
  12097. #endif
  12098. return -7658;
  12099. sigSz = 0;
  12100. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  12101. inLen, out, &sigSz, key, keyLen, rng);
  12102. if (ret != BAD_FUNC_ARG)
  12103. return -7659;
  12104. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, NULL,
  12105. inLen, out, (word32)modLen, key, keyLen);
  12106. if (ret != BAD_FUNC_ARG)
  12107. return -7660;
  12108. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  12109. 0, out, (word32)modLen, key, keyLen);
  12110. if (ret != BAD_FUNC_ARG)
  12111. return -7661;
  12112. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  12113. inLen, NULL, (word32)modLen, key, keyLen);
  12114. if (ret != BAD_FUNC_ARG)
  12115. return -7662;
  12116. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  12117. inLen, out, 0, key, keyLen);
  12118. if (ret != BAD_FUNC_ARG)
  12119. return -7663;
  12120. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  12121. inLen, out, (word32)modLen, NULL, keyLen);
  12122. if (ret != BAD_FUNC_ARG)
  12123. return -7664;
  12124. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  12125. inLen, out, (word32)modLen, key, 0);
  12126. if (ret != BAD_FUNC_ARG)
  12127. return -7665;
  12128. #ifndef HAVE_ECC
  12129. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_ECC, key, keyLen);
  12130. if (ret != SIG_TYPE_E)
  12131. return -7666;
  12132. #endif
  12133. #if defined(WOLF_CRYPTO_CB_ONLY_RSA)
  12134. return 0;
  12135. #endif
  12136. /* Use APIs. */
  12137. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_RSA, key, keyLen);
  12138. if (ret != modLen)
  12139. return -7667;
  12140. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_RSA_W_ENC, key, keyLen);
  12141. if (ret != modLen)
  12142. return -7668;
  12143. sigSz = (word32)ret;
  12144. #if !defined(WOLFSSL_RSA_PUBLIC_ONLY) && !defined(WOLFSSL_RSA_VERIFY_ONLY)
  12145. XMEMSET(out, 0, sizeof(out));
  12146. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  12147. inLen, out, &sigSz, key, keyLen, rng);
  12148. if (ret != 0)
  12149. return -7669;
  12150. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  12151. inLen, out, (word32)modLen, key, keyLen);
  12152. if (ret != 0)
  12153. return -7670;
  12154. sigSz = (word32)sizeof(out);
  12155. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA_W_ENC,
  12156. in, inLen, out, &sigSz, key, keyLen, rng);
  12157. if (ret != 0)
  12158. return -7671;
  12159. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA_W_ENC,
  12160. in, inLen, out, (word32)modLen, key, keyLen);
  12161. if (ret != 0)
  12162. return -7672;
  12163. /* Wrong signature type. */
  12164. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  12165. inLen, out, (word32)modLen, key, keyLen);
  12166. if (ret == 0)
  12167. return -7673;
  12168. /* check hash functions */
  12169. sigSz = (word32)sizeof(out);
  12170. ret = wc_SignatureGenerateHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA,
  12171. hash, (int)sizeof(hash), out, &sigSz, key, keyLen, rng);
  12172. if (ret != 0)
  12173. return -7674;
  12174. ret = wc_SignatureVerifyHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA,
  12175. hash, (int)sizeof(hash), out, (word32)modLen, key, keyLen);
  12176. if (ret != 0)
  12177. return -7675;
  12178. sigSz = (word32)sizeof(out);
  12179. ret = wc_SignatureGenerateHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA_W_ENC,
  12180. hashEnc, (int)sizeof(hashEnc), out, &sigSz, key, keyLen, rng);
  12181. if (ret != 0)
  12182. return -7676;
  12183. ret = wc_SignatureVerifyHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA_W_ENC,
  12184. hashEnc, (int)sizeof(hashEnc), out, (word32)modLen, key, keyLen);
  12185. if (ret != 0)
  12186. return -7677;
  12187. #else
  12188. (void)hash;
  12189. (void)hashEnc;
  12190. #endif /* !WOLFSSL_RSA_PUBLIC_ONLY && !WOLFSSL_RSA_VERIFY_ONLY */
  12191. return 0;
  12192. }
  12193. #endif /* !NO_SIG_WRAPPER */
  12194. #ifdef WC_RSA_NONBLOCK
  12195. static int rsa_nb_test(RsaKey* key, const byte* in, word32 inLen, byte* out,
  12196. word32 outSz, byte* plain, word32 plainSz, WC_RNG* rng)
  12197. {
  12198. int ret = 0, count;
  12199. int signSz = 0;
  12200. RsaNb nb;
  12201. byte* inlinePlain = NULL;
  12202. /* Enable non-blocking RSA mode - provide context */
  12203. ret = wc_RsaSetNonBlock(key, &nb);
  12204. if (ret != 0)
  12205. return ret;
  12206. #ifdef WC_RSA_NONBLOCK_TIME
  12207. /* Enable time based RSA blocking. 8 microseconds max (3.1GHz) */
  12208. ret = wc_RsaSetNonBlockTime(key, 8, 3100);
  12209. if (ret != 0)
  12210. return ret;
  12211. #endif
  12212. count = 0;
  12213. do {
  12214. ret = wc_RsaSSL_Sign(in, inLen, out, outSz, key, rng);
  12215. count++; /* track number of would blocks */
  12216. if (ret == FP_WOULDBLOCK) {
  12217. /* do "other" work here */
  12218. }
  12219. } while (ret == FP_WOULDBLOCK);
  12220. if (ret < 0) {
  12221. return ret;
  12222. }
  12223. #ifdef DEBUG_WOLFSSL
  12224. printf("RSA non-block sign: %d times\n", count);
  12225. #endif
  12226. signSz = ret;
  12227. /* Test non-blocking verify */
  12228. XMEMSET(plain, 0, plainSz);
  12229. count = 0;
  12230. do {
  12231. ret = wc_RsaSSL_Verify(out, (word32)signSz, plain, plainSz, key);
  12232. count++; /* track number of would blocks */
  12233. if (ret == FP_WOULDBLOCK) {
  12234. /* do "other" work here */
  12235. }
  12236. } while (ret == FP_WOULDBLOCK);
  12237. if (ret < 0) {
  12238. return ret;
  12239. }
  12240. #ifdef DEBUG_WOLFSSL
  12241. printf("RSA non-block verify: %d times\n", count);
  12242. #endif
  12243. if (signSz == ret && XMEMCMP(plain, in, (size_t)ret)) {
  12244. return SIG_VERIFY_E;
  12245. }
  12246. /* Test inline non-blocking verify */
  12247. count = 0;
  12248. do {
  12249. ret = wc_RsaSSL_VerifyInline(out, (word32)signSz, &inlinePlain, key);
  12250. count++; /* track number of would blocks */
  12251. if (ret == FP_WOULDBLOCK) {
  12252. /* do "other" work here */
  12253. }
  12254. } while (ret == FP_WOULDBLOCK);
  12255. if (ret < 0) {
  12256. return ret;
  12257. }
  12258. #ifdef DEBUG_WOLFSSL
  12259. printf("RSA non-block inline verify: %d times\n", count);
  12260. #endif
  12261. if (signSz == ret && XMEMCMP(inlinePlain, in, (size_t)ret)) {
  12262. return SIG_VERIFY_E;
  12263. }
  12264. /* Disabling non-block RSA mode */
  12265. ret = wc_RsaSetNonBlock(key, NULL);
  12266. (void)count;
  12267. return 0;
  12268. }
  12269. #endif
  12270. #if !defined(HAVE_USER_RSA) && !defined(NO_ASN)
  12271. static int rsa_decode_test(RsaKey* keyPub)
  12272. {
  12273. int ret;
  12274. word32 inSz;
  12275. word32 inOutIdx;
  12276. WOLFSSL_SMALL_STACK_STATIC const byte n[2] = { 0x00, 0x23 };
  12277. WOLFSSL_SMALL_STACK_STATIC const byte e[2] = { 0x00, 0x03 };
  12278. WOLFSSL_SMALL_STACK_STATIC const byte good[] = { 0x30, 0x06, 0x02, 0x01, 0x23, 0x02, 0x1,
  12279. 0x03 };
  12280. WOLFSSL_SMALL_STACK_STATIC const byte goodAlgId[] = {
  12281. 0x30, 0x18, 0x30, 0x16,
  12282. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
  12283. 0x03, 0x09, 0x00, 0x30, 0x06, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  12284. WOLFSSL_SMALL_STACK_STATIC const byte goodAlgIdNull[] = {
  12285. 0x30, 0x1a, 0x30, 0x18,
  12286. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
  12287. 0x05, 0x00, 0x03, 0x09, 0x00, 0x30, 0x06, 0x02, 0x01, 0x23,
  12288. 0x02, 0x1, 0x03 };
  12289. WOLFSSL_SMALL_STACK_STATIC const byte badAlgIdNull[] = {
  12290. 0x30, 0x1b, 0x30, 0x19,
  12291. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
  12292. 0x05, 0x01, 0x00, 0x03, 0x09, 0x00, 0x30, 0x06, 0x02, 0x01, 0x23,
  12293. 0x02, 0x1, 0x03 };
  12294. WOLFSSL_SMALL_STACK_STATIC const byte badNotBitString[] = {
  12295. 0x30, 0x18, 0x30, 0x16,
  12296. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
  12297. 0x04, 0x09, 0x00, 0x30, 0x06, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  12298. WOLFSSL_SMALL_STACK_STATIC const byte badBitStringLen[] = {
  12299. 0x30, 0x18, 0x30, 0x16,
  12300. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
  12301. 0x03, 0x0a, 0x00, 0x30, 0x06, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  12302. WOLFSSL_SMALL_STACK_STATIC const byte badNoSeq[] = {
  12303. 0x30, 0x16, 0x30, 0x14,
  12304. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
  12305. 0x07, 0x00, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  12306. WOLFSSL_SMALL_STACK_STATIC const byte badNoObj[] = {
  12307. 0x30, 0x0f, 0x30, 0x0d, 0x05, 0x00, 0x03, 0x09, 0x00, 0x30, 0x06,
  12308. 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  12309. WOLFSSL_SMALL_STACK_STATIC const byte badIntN[] = {
  12310. 0x30, 0x06, 0x02, 0x05, 0x23, 0x02, 0x1, 0x03 };
  12311. WOLFSSL_SMALL_STACK_STATIC const byte badNotIntE[] = {
  12312. 0x30, 0x06, 0x02, 0x01, 0x23, 0x04, 0x1, 0x03 };
  12313. WOLFSSL_SMALL_STACK_STATIC const byte badLength[] = {
  12314. 0x30, 0x04, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  12315. WOLFSSL_SMALL_STACK_STATIC const byte badBitStrNoZero[] = {
  12316. 0x30, 0x17, 0x30, 0x15,
  12317. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
  12318. 0x03, 0x08, 0x30, 0x06, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  12319. ret = wc_InitRsaKey(keyPub, NULL);
  12320. if (ret != 0)
  12321. return -7690;
  12322. /* Parameter Validation testing. */
  12323. ret = wc_RsaPublicKeyDecodeRaw(NULL, sizeof(n), e, sizeof(e), keyPub);
  12324. if (ret != BAD_FUNC_ARG) {
  12325. ret = -7691;
  12326. goto done;
  12327. }
  12328. ret = wc_RsaPublicKeyDecodeRaw(n, sizeof(n), NULL, sizeof(e), keyPub);
  12329. if (ret != BAD_FUNC_ARG) {
  12330. ret = -7692;
  12331. goto done;
  12332. }
  12333. ret = wc_RsaPublicKeyDecodeRaw(n, sizeof(n), e, sizeof(e), NULL);
  12334. if (ret != BAD_FUNC_ARG) {
  12335. ret = -7693;
  12336. goto done;
  12337. }
  12338. ret = wc_RsaPublicKeyDecodeRaw(n, (word32)-1, e, sizeof(e), keyPub);
  12339. #if !defined(WOLFSSL_SP_MATH) & !defined(WOLFSSL_SP_MATH_ALL)
  12340. if (ret != 0) {
  12341. #else
  12342. if (ret != ASN_GETINT_E) {
  12343. #endif
  12344. ret = -7694;
  12345. goto done;
  12346. }
  12347. wc_FreeRsaKey(keyPub);
  12348. ret = wc_InitRsaKey(keyPub, NULL);
  12349. if (ret != 0)
  12350. return -7695;
  12351. ret = wc_RsaPublicKeyDecodeRaw(n, sizeof(n), e, (word32)-1, keyPub);
  12352. #if !defined(WOLFSSL_SP_MATH) & !defined(WOLFSSL_SP_MATH_ALL)
  12353. if (ret != 0) {
  12354. #else
  12355. if (ret != ASN_GETINT_E) {
  12356. #endif
  12357. ret = -7696;
  12358. goto done;
  12359. }
  12360. wc_FreeRsaKey(keyPub);
  12361. ret = wc_InitRsaKey(keyPub, NULL);
  12362. if (ret != 0)
  12363. return -7697;
  12364. /* Use API. */
  12365. ret = wc_RsaPublicKeyDecodeRaw(n, sizeof(n), e, sizeof(e), keyPub);
  12366. if (ret != 0) {
  12367. ret = -7698;
  12368. goto done;
  12369. }
  12370. wc_FreeRsaKey(keyPub);
  12371. ret = wc_InitRsaKey(keyPub, NULL);
  12372. if (ret != 0)
  12373. return -7699;
  12374. /* Parameter Validation testing. */
  12375. inSz = sizeof(good);
  12376. ret = wc_RsaPublicKeyDecode(NULL, &inOutIdx, keyPub, inSz);
  12377. if (ret != BAD_FUNC_ARG) {
  12378. ret = -7700;
  12379. goto done;
  12380. }
  12381. ret = wc_RsaPublicKeyDecode(good, NULL, keyPub, inSz);
  12382. if (ret != BAD_FUNC_ARG) {
  12383. ret = -7701;
  12384. goto done;
  12385. }
  12386. ret = wc_RsaPublicKeyDecode(good, &inOutIdx, NULL, inSz);
  12387. if (ret != BAD_FUNC_ARG) {
  12388. ret = -7702;
  12389. goto done;
  12390. }
  12391. /* Use good data and offset to bad data. */
  12392. inOutIdx = 2;
  12393. inSz = sizeof(good) - inOutIdx;
  12394. ret = wc_RsaPublicKeyDecode(good, &inOutIdx, keyPub, inSz);
  12395. if (ret != ASN_PARSE_E) {
  12396. ret = -7703;
  12397. goto done;
  12398. }
  12399. inOutIdx = 2;
  12400. inSz = sizeof(goodAlgId) - inOutIdx;
  12401. ret = wc_RsaPublicKeyDecode(goodAlgId, &inOutIdx, keyPub, inSz);
  12402. if (ret != ASN_PARSE_E) {
  12403. ret = -7704;
  12404. goto done;
  12405. }
  12406. inOutIdx = 2;
  12407. inSz = sizeof(goodAlgId);
  12408. ret = wc_RsaPublicKeyDecode(goodAlgId, &inOutIdx, keyPub, inSz);
  12409. #ifndef WOLFSSL_NO_DECODE_EXTRA
  12410. if (ret != ASN_PARSE_E)
  12411. #else
  12412. if (ret != ASN_RSA_KEY_E)
  12413. #endif
  12414. {
  12415. ret = -7705;
  12416. goto done;
  12417. }
  12418. /* Try different bad data. */
  12419. inSz = sizeof(badAlgIdNull);
  12420. inOutIdx = 0;
  12421. ret = wc_RsaPublicKeyDecode(badAlgIdNull, &inOutIdx, keyPub, inSz);
  12422. if (ret != ASN_EXPECT_0_E) {
  12423. ret = -7706;
  12424. goto done;
  12425. }
  12426. inSz = sizeof(badNotBitString);
  12427. inOutIdx = 0;
  12428. ret = wc_RsaPublicKeyDecode(badNotBitString, &inOutIdx, keyPub, inSz);
  12429. if (ret != ASN_BITSTR_E) {
  12430. ret = -7707;
  12431. goto done;
  12432. }
  12433. inSz = sizeof(badBitStringLen);
  12434. inOutIdx = 0;
  12435. ret = wc_RsaPublicKeyDecode(badBitStringLen, &inOutIdx, keyPub, inSz);
  12436. if (ret != ASN_PARSE_E) {
  12437. ret = -7708;
  12438. goto done;
  12439. }
  12440. inSz = sizeof(badNoSeq);
  12441. inOutIdx = 0;
  12442. ret = wc_RsaPublicKeyDecode(badNoSeq, &inOutIdx, keyPub, inSz);
  12443. if (ret != ASN_PARSE_E) {
  12444. ret = -7709;
  12445. goto done;
  12446. }
  12447. inSz = sizeof(badNoObj);
  12448. inOutIdx = 0;
  12449. ret = wc_RsaPublicKeyDecode(badNoObj, &inOutIdx, keyPub, inSz);
  12450. if (ret != ASN_PARSE_E && ret != ASN_OBJECT_ID_E) {
  12451. ret = -7710;
  12452. goto done;
  12453. }
  12454. inSz = sizeof(badIntN);
  12455. inOutIdx = 0;
  12456. ret = wc_RsaPublicKeyDecode(badIntN, &inOutIdx, keyPub, inSz);
  12457. if (ret != ASN_RSA_KEY_E && ret != ASN_PARSE_E) {
  12458. ret = -7711;
  12459. goto done;
  12460. }
  12461. inSz = sizeof(badNotIntE);
  12462. inOutIdx = 0;
  12463. ret = wc_RsaPublicKeyDecode(badNotIntE, &inOutIdx, keyPub, inSz);
  12464. if (ret != ASN_RSA_KEY_E && ret != ASN_PARSE_E) {
  12465. ret = -7712;
  12466. goto done;
  12467. }
  12468. /* TODO: Shouldn't pass as the sequence length is too small. */
  12469. inSz = sizeof(badLength);
  12470. inOutIdx = 0;
  12471. ret = wc_RsaPublicKeyDecode(badLength, &inOutIdx, keyPub, inSz);
  12472. #ifndef WOLFSSL_ASN_TEMPLATE
  12473. if (ret != 0)
  12474. #else
  12475. if (ret != ASN_PARSE_E)
  12476. #endif
  12477. {
  12478. ret = -7713;
  12479. goto done;
  12480. }
  12481. /* TODO: Shouldn't ignore object id's data. */
  12482. wc_FreeRsaKey(keyPub);
  12483. ret = wc_InitRsaKey(keyPub, NULL);
  12484. if (ret != 0)
  12485. return -7714;
  12486. inSz = sizeof(badBitStrNoZero);
  12487. inOutIdx = 0;
  12488. ret = wc_RsaPublicKeyDecode(badBitStrNoZero, &inOutIdx, keyPub, inSz);
  12489. if (ret != ASN_EXPECT_0_E && ret != ASN_PARSE_E) {
  12490. ret = -7715;
  12491. goto done;
  12492. }
  12493. wc_FreeRsaKey(keyPub);
  12494. ret = wc_InitRsaKey(keyPub, NULL);
  12495. if (ret != 0)
  12496. return -7716;
  12497. /* Valid data cases. */
  12498. inSz = sizeof(good);
  12499. inOutIdx = 0;
  12500. ret = wc_RsaPublicKeyDecode(good, &inOutIdx, keyPub, inSz);
  12501. if (ret != 0) {
  12502. ret = -7717;
  12503. goto done;
  12504. }
  12505. if (inOutIdx != inSz) {
  12506. ret = -7718;
  12507. goto done;
  12508. }
  12509. wc_FreeRsaKey(keyPub);
  12510. ret = wc_InitRsaKey(keyPub, NULL);
  12511. if (ret != 0)
  12512. return -7719;
  12513. inSz = sizeof(goodAlgId);
  12514. inOutIdx = 0;
  12515. ret = wc_RsaPublicKeyDecode(goodAlgId, &inOutIdx, keyPub, inSz);
  12516. if (ret != 0) {
  12517. ret = -7720;
  12518. goto done;
  12519. }
  12520. if (inOutIdx != inSz) {
  12521. ret = -7721;
  12522. goto done;
  12523. }
  12524. wc_FreeRsaKey(keyPub);
  12525. ret = wc_InitRsaKey(keyPub, NULL);
  12526. if (ret != 0)
  12527. return -7722;
  12528. inSz = sizeof(goodAlgIdNull);
  12529. inOutIdx = 0;
  12530. ret = wc_RsaPublicKeyDecode(goodAlgIdNull, &inOutIdx, keyPub, inSz);
  12531. if (ret != 0) {
  12532. ret = -7723;
  12533. goto done;
  12534. }
  12535. if (inOutIdx != inSz) {
  12536. ret = -7724;
  12537. goto done;
  12538. }
  12539. done:
  12540. wc_FreeRsaKey(keyPub);
  12541. return ret;
  12542. }
  12543. #endif
  12544. #if defined(WC_RSA_PSS) && !defined(HAVE_FIPS_VERSION) /* not supported with FIPSv1 */
  12545. /* Need to create known good signatures to test with this. */
  12546. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
  12547. !defined(WOLF_CRYPTO_CB_ONLY_RSA)
  12548. static int rsa_pss_test(WC_RNG* rng, RsaKey* key)
  12549. {
  12550. byte digest[WC_MAX_DIGEST_SIZE];
  12551. int ret = 0;
  12552. const char inStr[] = TEST_STRING;
  12553. word32 inLen = (word32)TEST_STRING_SZ;
  12554. word32 outSz;
  12555. word32 plainSz;
  12556. word32 digestSz;
  12557. int i, j;
  12558. #ifdef RSA_PSS_TEST_WRONG_PARAMS
  12559. int k, l;
  12560. #endif
  12561. int len;
  12562. byte* plain;
  12563. int mgf[] = {
  12564. #ifndef NO_SHA
  12565. WC_MGF1SHA1,
  12566. #endif
  12567. #ifdef WOLFSSL_SHA224
  12568. WC_MGF1SHA224,
  12569. #endif
  12570. WC_MGF1SHA256,
  12571. #ifdef WOLFSSL_SHA384
  12572. WC_MGF1SHA384,
  12573. #endif
  12574. #ifdef WOLFSSL_SHA512
  12575. WC_MGF1SHA512
  12576. #endif
  12577. };
  12578. enum wc_HashType hash[] = {
  12579. #ifndef NO_SHA
  12580. WC_HASH_TYPE_SHA,
  12581. #endif
  12582. #ifdef WOLFSSL_SHA224
  12583. WC_HASH_TYPE_SHA224,
  12584. #endif
  12585. WC_HASH_TYPE_SHA256,
  12586. #ifdef WOLFSSL_SHA384
  12587. WC_HASH_TYPE_SHA384,
  12588. #endif
  12589. #ifdef WOLFSSL_SHA512
  12590. WC_HASH_TYPE_SHA512,
  12591. #endif
  12592. };
  12593. WC_DECLARE_VAR(in, byte, RSA_TEST_BYTES, HEAP_HINT);
  12594. WC_DECLARE_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
  12595. WC_DECLARE_VAR(sig, byte, RSA_TEST_BYTES, HEAP_HINT);
  12596. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  12597. if (in == NULL || out == NULL || sig == NULL)
  12598. ERROR_OUT(MEMORY_E, exit_rsa_pss);
  12599. #endif
  12600. XMEMCPY(in, inStr, inLen);
  12601. /* Test all combinations of hash and MGF. */
  12602. for (j = 0; j < (int)(sizeof(hash)/sizeof(*hash)); j++) {
  12603. /* Calculate hash of message. */
  12604. ret = wc_Hash(hash[j], in, inLen, digest, sizeof(digest));
  12605. if (ret != 0)
  12606. ERROR_OUT(-7730, exit_rsa_pss);
  12607. digestSz = wc_HashGetDigestSize(hash[j]);
  12608. for (i = 0; i < (int)(sizeof(mgf)/sizeof(*mgf)); i++) {
  12609. outSz = RSA_TEST_BYTES;
  12610. do {
  12611. #if defined(WOLFSSL_ASYNC_CRYPT)
  12612. ret = wc_AsyncWait(ret, &key->asyncDev,
  12613. WC_ASYNC_FLAG_CALL_AGAIN);
  12614. #endif
  12615. if (ret >= 0) {
  12616. ret = wc_RsaPSS_Sign_ex(digest, digestSz, out, outSz,
  12617. hash[j], mgf[i], -1, key, rng);
  12618. }
  12619. } while (ret == WC_PENDING_E);
  12620. if (ret <= 0)
  12621. ERROR_OUT(-7731, exit_rsa_pss);
  12622. outSz = ret;
  12623. XMEMCPY(sig, out, outSz);
  12624. plain = NULL;
  12625. TEST_SLEEP();
  12626. do {
  12627. #if defined(WOLFSSL_ASYNC_CRYPT)
  12628. ret = wc_AsyncWait(ret, &key->asyncDev,
  12629. WC_ASYNC_FLAG_CALL_AGAIN);
  12630. #endif
  12631. if (ret >= 0) {
  12632. ret = wc_RsaPSS_VerifyInline_ex(sig, outSz, &plain, hash[j],
  12633. mgf[i], -1, key);
  12634. }
  12635. } while (ret == WC_PENDING_E);
  12636. if (ret <= 0)
  12637. ERROR_OUT(-7732, exit_rsa_pss);
  12638. plainSz = ret;
  12639. TEST_SLEEP();
  12640. #if defined(HAVE_SELFTEST) && \
  12641. (!defined(HAVE_SELFTEST_VERSION) || (HAVE_SELFTEST_VERSION < 2))
  12642. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  12643. hash[j], -1);
  12644. #elif defined(HAVE_SELFTEST) && (HAVE_SELFTEST_VERSION == 2)
  12645. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  12646. hash[j], -1, 0);
  12647. #else
  12648. ret = wc_RsaPSS_CheckPadding_ex2(digest, digestSz, plain, plainSz,
  12649. hash[j], -1, wc_RsaEncryptSize(key)*8, HEAP_HINT);
  12650. #endif
  12651. if (ret != 0)
  12652. ERROR_OUT(-7733, exit_rsa_pss);
  12653. #ifdef RSA_PSS_TEST_WRONG_PARAMS
  12654. for (k = 0; k < (int)(sizeof(mgf)/sizeof(*mgf)); k++) {
  12655. for (l = 0; l < (int)(sizeof(hash)/sizeof(*hash)); l++) {
  12656. if (i == k && j == l)
  12657. continue;
  12658. XMEMCPY(sig, out, outSz);
  12659. do {
  12660. #if defined(WOLFSSL_ASYNC_CRYPT)
  12661. ret = wc_AsyncWait(ret, &key->asyncDev,
  12662. WC_ASYNC_FLAG_CALL_AGAIN);
  12663. #endif
  12664. if (ret >= 0) {
  12665. ret = wc_RsaPSS_VerifyInline_ex(sig, outSz,
  12666. (byte**)&plain, hash[l], mgf[k], -1, key);
  12667. }
  12668. } while (ret == WC_PENDING_E);
  12669. if (ret >= 0)
  12670. ERROR_OUT(-7734, exit_rsa_pss);
  12671. }
  12672. }
  12673. #endif
  12674. }
  12675. }
  12676. /* Test that a salt length of zero works. */
  12677. digestSz = wc_HashGetDigestSize(hash[0]);
  12678. outSz = RSA_TEST_BYTES;
  12679. do {
  12680. #if defined(WOLFSSL_ASYNC_CRYPT)
  12681. ret = wc_AsyncWait(ret, &key->asyncDev,
  12682. WC_ASYNC_FLAG_CALL_AGAIN);
  12683. #endif
  12684. if (ret >= 0) {
  12685. ret = wc_RsaPSS_Sign_ex(digest, digestSz, out, outSz, hash[0],
  12686. mgf[0], 0, key, rng);
  12687. }
  12688. } while (ret == WC_PENDING_E);
  12689. if (ret <= 0)
  12690. ERROR_OUT(-7735, exit_rsa_pss);
  12691. outSz = ret;
  12692. TEST_SLEEP();
  12693. do {
  12694. #if defined(WOLFSSL_ASYNC_CRYPT)
  12695. ret = wc_AsyncWait(ret, &key->asyncDev,
  12696. WC_ASYNC_FLAG_CALL_AGAIN);
  12697. #endif
  12698. if (ret >= 0) {
  12699. ret = wc_RsaPSS_Verify_ex(out, outSz, sig, outSz, hash[0], mgf[0],
  12700. 0, key);
  12701. }
  12702. } while (ret == WC_PENDING_E);
  12703. if (ret <= 0)
  12704. ERROR_OUT(-7736, exit_rsa_pss);
  12705. plainSz = ret;
  12706. TEST_SLEEP();
  12707. do {
  12708. #if defined(WOLFSSL_ASYNC_CRYPT)
  12709. ret = wc_AsyncWait(ret, &key->asyncDev,
  12710. WC_ASYNC_FLAG_CALL_AGAIN);
  12711. #endif
  12712. if (ret >= 0) {
  12713. #if defined(HAVE_SELFTEST) && \
  12714. (!defined(HAVE_SELFTEST_VERSION) || (HAVE_SELFTEST_VERSION < 2))
  12715. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, sig, plainSz,
  12716. hash[0], 0);
  12717. #elif defined(HAVE_SELFTEST) && (HAVE_SELFTEST_VERSION == 2)
  12718. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, sig, plainSz,
  12719. hash[0], 0, 0);
  12720. #else
  12721. ret = wc_RsaPSS_CheckPadding_ex2(digest, digestSz, sig, plainSz,
  12722. hash[0], 0, 0, HEAP_HINT);
  12723. #endif
  12724. }
  12725. } while (ret == WC_PENDING_E);
  12726. if (ret != 0)
  12727. ERROR_OUT(-7737, exit_rsa_pss);
  12728. XMEMCPY(sig, out, outSz);
  12729. plain = NULL;
  12730. do {
  12731. #if defined(WOLFSSL_ASYNC_CRYPT)
  12732. ret = wc_AsyncWait(ret, &key->asyncDev,
  12733. WC_ASYNC_FLAG_CALL_AGAIN);
  12734. #endif
  12735. if (ret >= 0) {
  12736. ret = wc_RsaPSS_VerifyInline_ex(sig, outSz, &plain, hash[0], mgf[0],
  12737. 0, key);
  12738. }
  12739. } while (ret == WC_PENDING_E);
  12740. if (ret <= 0)
  12741. ERROR_OUT(-7738, exit_rsa_pss);
  12742. plainSz = ret;
  12743. TEST_SLEEP();
  12744. #if defined(HAVE_SELFTEST) && \
  12745. (!defined(HAVE_SELFTEST_VERSION) || (HAVE_SELFTEST_VERSION < 2))
  12746. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  12747. hash[0], 0);
  12748. #elif defined(HAVE_SELFTEST) && (HAVE_SELFTEST_VERSION == 2)
  12749. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  12750. hash[0], 0, 0);
  12751. #else
  12752. ret = wc_RsaPSS_CheckPadding_ex2(digest, digestSz, plain, plainSz, hash[0],
  12753. 0, 0, HEAP_HINT);
  12754. #endif
  12755. if (ret != 0)
  12756. ERROR_OUT(-7739, exit_rsa_pss);
  12757. /* Test bad salt lengths in various APIs. */
  12758. digestSz = wc_HashGetDigestSize(hash[0]);
  12759. outSz = RSA_TEST_BYTES;
  12760. #ifndef WOLFSSL_PSS_SALT_LEN_DISCOVER
  12761. len = -2;
  12762. #else
  12763. len = -3;
  12764. #endif
  12765. do {
  12766. #if defined(WOLFSSL_ASYNC_CRYPT)
  12767. ret = wc_AsyncWait(ret, &key->asyncDev,
  12768. WC_ASYNC_FLAG_CALL_AGAIN);
  12769. #endif
  12770. if (ret >= 0) {
  12771. ret = wc_RsaPSS_Sign_ex(digest, digestSz, out, outSz, hash[0],
  12772. mgf[0], len, key, rng);
  12773. }
  12774. } while (ret == WC_PENDING_E);
  12775. if (ret != PSS_SALTLEN_E)
  12776. ERROR_OUT(-7740, exit_rsa_pss);
  12777. do {
  12778. #if defined(WOLFSSL_ASYNC_CRYPT)
  12779. ret = wc_AsyncWait(ret, &key->asyncDev,
  12780. WC_ASYNC_FLAG_CALL_AGAIN);
  12781. #endif
  12782. if (ret >= 0) {
  12783. ret = wc_RsaPSS_Sign_ex(digest, digestSz, out, outSz, hash[0],
  12784. mgf[0], digestSz + 1, key, rng);
  12785. }
  12786. } while (ret == WC_PENDING_E);
  12787. if (ret != PSS_SALTLEN_E)
  12788. ERROR_OUT(-7741, exit_rsa_pss);
  12789. TEST_SLEEP();
  12790. do {
  12791. #if defined(WOLFSSL_ASYNC_CRYPT)
  12792. ret = wc_AsyncWait(ret, &key->asyncDev,
  12793. WC_ASYNC_FLAG_CALL_AGAIN);
  12794. #endif
  12795. if (ret >= 0) {
  12796. ret = wc_RsaPSS_VerifyInline_ex(sig, outSz, &plain, hash[0],
  12797. mgf[0], -2, key);
  12798. }
  12799. } while (ret == WC_PENDING_E);
  12800. if (ret != PSS_SALTLEN_E)
  12801. ERROR_OUT(-7742, exit_rsa_pss);
  12802. TEST_SLEEP();
  12803. do {
  12804. #if defined(WOLFSSL_ASYNC_CRYPT)
  12805. ret = wc_AsyncWait(ret, &key->asyncDev,
  12806. WC_ASYNC_FLAG_CALL_AGAIN);
  12807. #endif
  12808. if (ret >= 0) {
  12809. ret = wc_RsaPSS_VerifyInline_ex(sig, outSz, &plain, hash[0], mgf[0],
  12810. digestSz + 1, key);
  12811. }
  12812. } while (ret == WC_PENDING_E);
  12813. if (ret != PSS_SALTLEN_E)
  12814. ERROR_OUT(-7743, exit_rsa_pss);
  12815. TEST_SLEEP();
  12816. #ifndef WOLFSSL_PSS_SALT_LEN_DISCOVER
  12817. len = -2;
  12818. #else
  12819. len = -3;
  12820. #endif
  12821. #if defined(HAVE_SELFTEST) && \
  12822. (!defined(HAVE_SELFTEST_VERSION) || (HAVE_SELFTEST_VERSION < 2))
  12823. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  12824. hash[0], len);
  12825. #elif defined(HAVE_SELFTEST) && (HAVE_SELFTEST_VERSION == 2)
  12826. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  12827. hash[0], len, 0);
  12828. #else
  12829. ret = wc_RsaPSS_CheckPadding_ex2(digest, digestSz, plain, plainSz, hash[0],
  12830. len, 0, HEAP_HINT);
  12831. #endif
  12832. if (ret != PSS_SALTLEN_E)
  12833. ERROR_OUT(-7744, exit_rsa_pss);
  12834. #ifndef WOLFSSL_PSS_LONG_SALT
  12835. len = digestSz + 1;
  12836. #else
  12837. len = plainSz - digestSz - 1;
  12838. #endif
  12839. #if defined(HAVE_SELFTEST) && \
  12840. (!defined(HAVE_SELFTEST_VERSION) || (HAVE_SELFTEST_VERSION < 2))
  12841. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  12842. hash[0], len);
  12843. #elif defined(HAVE_SELFTEST) && (HAVE_SELFTEST_VERSION == 2)
  12844. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  12845. hash[0], len, 0);
  12846. #else
  12847. ret = wc_RsaPSS_CheckPadding_ex2(digest, digestSz, plain, plainSz, hash[0],
  12848. len, 0, HEAP_HINT);
  12849. #endif
  12850. if (ret != PSS_SALTLEN_E)
  12851. ERROR_OUT(-7745, exit_rsa_pss);
  12852. ret = 0;
  12853. exit_rsa_pss:
  12854. WC_FREE_VAR(sig, HEAP_HINT);
  12855. WC_FREE_VAR(in, HEAP_HINT);
  12856. WC_FREE_VAR(out, HEAP_HINT);
  12857. return ret;
  12858. }
  12859. #endif /* !WOLFSSL_RSA_VERIFY_ONLY && !WOLFSSL_RSA_PUBLIC_ONLY */
  12860. #endif
  12861. #ifdef WC_RSA_NO_PADDING
  12862. WOLFSSL_TEST_SUBROUTINE int rsa_no_pad_test(void)
  12863. {
  12864. WC_RNG rng;
  12865. byte* tmp = NULL;
  12866. size_t bytes;
  12867. int ret;
  12868. word32 inLen = 0;
  12869. word32 idx = 0;
  12870. word32 outSz = RSA_TEST_BYTES;
  12871. word32 plainSz = RSA_TEST_BYTES;
  12872. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  12873. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096) && \
  12874. !defined(NO_FILESYSTEM)
  12875. XFILE file;
  12876. #endif
  12877. WC_DECLARE_VAR(key, RsaKey, 1, HEAP_HINT);
  12878. WC_DECLARE_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
  12879. WC_DECLARE_VAR(plain, byte, RSA_TEST_BYTES, HEAP_HINT);
  12880. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  12881. if (key == NULL || out == NULL || plain == NULL)
  12882. ERROR_OUT(MEMORY_E, exit_rsa_nopadding);
  12883. #endif
  12884. /* initialize stack structures */
  12885. XMEMSET(&rng, 0, sizeof(rng));
  12886. XMEMSET(key, 0, sizeof(RsaKey));
  12887. #ifdef USE_CERT_BUFFERS_1024
  12888. bytes = (size_t)sizeof_client_key_der_1024;
  12889. if (bytes < (size_t)sizeof_client_cert_der_1024)
  12890. bytes = (size_t)sizeof_client_cert_der_1024;
  12891. #elif defined(USE_CERT_BUFFERS_2048)
  12892. bytes = (size_t)sizeof_client_key_der_2048;
  12893. if (bytes < (size_t)sizeof_client_cert_der_2048)
  12894. bytes = (size_t)sizeof_client_cert_der_2048;
  12895. #else
  12896. bytes = FOURK_BUF;
  12897. #endif
  12898. tmp = (byte*)XMALLOC(bytes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12899. if (tmp == NULL
  12900. #ifdef WOLFSSL_ASYNC_CRYPT
  12901. || out == NULL || plain == NULL
  12902. #endif
  12903. ) {
  12904. ERROR_OUT(-7800, exit_rsa_nopadding);
  12905. }
  12906. #ifdef USE_CERT_BUFFERS_1024
  12907. XMEMCPY(tmp, client_key_der_1024, (size_t)sizeof_client_key_der_1024);
  12908. #elif defined(USE_CERT_BUFFERS_2048)
  12909. XMEMCPY(tmp, client_key_der_2048, (size_t)sizeof_client_key_der_2048);
  12910. #elif defined(USE_CERT_BUFFERS_3072)
  12911. XMEMCPY(tmp, client_key_der_3072, (size_t)sizeof_client_key_der_3072);
  12912. #elif defined(USE_CERT_BUFFERS_4096)
  12913. XMEMCPY(tmp, client_key_der_4096, (size_t)sizeof_client_key_der_4096);
  12914. #elif !defined(NO_FILESYSTEM)
  12915. file = XFOPEN(clientKey, "rb");
  12916. if (!file) {
  12917. err_sys("can't open clientKey, Please run from wolfSSL home dir", -40);
  12918. ERROR_OUT(-7801, exit_rsa_nopadding);
  12919. }
  12920. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  12921. XFCLOSE(file);
  12922. #else
  12923. /* No key to use. */
  12924. ERROR_OUT(-7802, exit_rsa_nopadding);
  12925. #endif /* USE_CERT_BUFFERS */
  12926. ret = wc_InitRsaKey_ex(key, HEAP_HINT, devId);
  12927. if (ret != 0) {
  12928. ERROR_OUT(-7803, exit_rsa_nopadding);
  12929. }
  12930. ret = wc_RsaPrivateKeyDecode(tmp, &idx, key, (word32)bytes);
  12931. if (ret != 0) {
  12932. ERROR_OUT(-7804, exit_rsa_nopadding);
  12933. }
  12934. /* after loading in key use tmp as the test buffer */
  12935. #ifndef HAVE_FIPS
  12936. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  12937. #else
  12938. ret = wc_InitRng(&rng);
  12939. #endif
  12940. if (ret != 0) {
  12941. ERROR_OUT(-7805, exit_rsa_nopadding);
  12942. }
  12943. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  12944. inLen = wc_RsaEncryptSize(key);
  12945. outSz = inLen;
  12946. plainSz = inLen;
  12947. XMEMSET(tmp, 7, inLen);
  12948. do {
  12949. #if defined(WOLFSSL_ASYNC_CRYPT)
  12950. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  12951. #endif
  12952. if (ret >= 0) {
  12953. ret = wc_RsaDirect(tmp, inLen, out, &outSz, key,
  12954. RSA_PRIVATE_ENCRYPT, &rng);
  12955. }
  12956. } while (ret == WC_PENDING_E);
  12957. if (ret <= 0) {
  12958. ERROR_OUT(-7806, exit_rsa_nopadding);
  12959. }
  12960. /* encrypted result should not be the same as input */
  12961. if (XMEMCMP(out, tmp, inLen) == 0) {
  12962. ERROR_OUT(-7807, exit_rsa_nopadding);
  12963. }
  12964. TEST_SLEEP();
  12965. /* decrypt with public key and compare result */
  12966. do {
  12967. #if defined(WOLFSSL_ASYNC_CRYPT)
  12968. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  12969. #endif
  12970. if (ret >= 0) {
  12971. ret = wc_RsaDirect(out, outSz, plain, &plainSz, key,
  12972. RSA_PUBLIC_DECRYPT, &rng);
  12973. }
  12974. } while (ret == WC_PENDING_E);
  12975. if (ret <= 0) {
  12976. ERROR_OUT(-7808, exit_rsa_nopadding);
  12977. }
  12978. if (XMEMCMP(plain, tmp, inLen) != 0) {
  12979. ERROR_OUT(-7809, exit_rsa_nopadding);
  12980. }
  12981. TEST_SLEEP();
  12982. #endif
  12983. #ifdef WC_RSA_BLINDING
  12984. ret = wc_RsaSetRNG(NULL, &rng);
  12985. if (ret != BAD_FUNC_ARG) {
  12986. ERROR_OUT(-7810, exit_rsa_nopadding);
  12987. }
  12988. ret = wc_RsaSetRNG(key, &rng);
  12989. if (ret < 0) {
  12990. ERROR_OUT(-7811, exit_rsa_nopadding);
  12991. }
  12992. #endif
  12993. /* test encrypt and decrypt using WC_RSA_NO_PAD */
  12994. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  12995. do {
  12996. #if defined(WOLFSSL_ASYNC_CRYPT)
  12997. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  12998. #endif
  12999. if (ret >= 0) {
  13000. ret = wc_RsaPublicEncrypt_ex(tmp, inLen, out, (int)outSz, key, &rng,
  13001. WC_RSA_NO_PAD, WC_HASH_TYPE_NONE, WC_MGF1NONE, NULL, 0);
  13002. }
  13003. } while (ret == WC_PENDING_E);
  13004. if (ret < 0) {
  13005. ERROR_OUT(-7812, exit_rsa_nopadding);
  13006. }
  13007. TEST_SLEEP();
  13008. #endif /* WOLFSSL_RSA_VERIFY_ONLY */
  13009. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  13010. do {
  13011. #if defined(WOLFSSL_ASYNC_CRYPT)
  13012. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13013. #endif
  13014. if (ret >= 0) {
  13015. ret = wc_RsaPrivateDecrypt_ex(out, outSz, plain, (int)plainSz, key,
  13016. WC_RSA_NO_PAD, WC_HASH_TYPE_NONE, WC_MGF1NONE, NULL, 0);
  13017. }
  13018. } while (ret == WC_PENDING_E);
  13019. if (ret < 0) {
  13020. ERROR_OUT(-7813, exit_rsa_nopadding);
  13021. }
  13022. if (XMEMCMP(plain, tmp, inLen) != 0) {
  13023. ERROR_OUT(-7814, exit_rsa_nopadding);
  13024. }
  13025. TEST_SLEEP();
  13026. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  13027. /* test some bad arguments */
  13028. ret = wc_RsaDirect(out, outSz, plain, &plainSz, key, -1,
  13029. &rng);
  13030. if (ret != BAD_FUNC_ARG) {
  13031. ERROR_OUT(-7815, exit_rsa_nopadding);
  13032. }
  13033. ret = wc_RsaDirect(out, outSz, plain, &plainSz, NULL, RSA_PUBLIC_DECRYPT,
  13034. &rng);
  13035. if (ret != BAD_FUNC_ARG) {
  13036. ERROR_OUT(-7816, exit_rsa_nopadding);
  13037. }
  13038. ret = wc_RsaDirect(out, outSz, NULL, &plainSz, key, RSA_PUBLIC_DECRYPT,
  13039. &rng);
  13040. if (ret != LENGTH_ONLY_E || plainSz != inLen) {
  13041. ERROR_OUT(-7817, exit_rsa_nopadding);
  13042. }
  13043. ret = wc_RsaDirect(out, outSz - 10, plain, &plainSz, key,
  13044. RSA_PUBLIC_DECRYPT, &rng);
  13045. if (ret != BAD_FUNC_ARG) {
  13046. ERROR_OUT(-7818, exit_rsa_nopadding);
  13047. }
  13048. /* if making it to this point of code without hitting an ERROR_OUT then
  13049. * all tests have passed */
  13050. ret = 0;
  13051. exit_rsa_nopadding:
  13052. wc_FreeRsaKey(key);
  13053. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13054. WC_FREE_VAR(key, HEAP_HINT);
  13055. WC_FREE_VAR(out, HEAP_HINT);
  13056. WC_FREE_VAR(plain, HEAP_HINT);
  13057. wc_FreeRng(&rng);
  13058. return ret;
  13059. }
  13060. #endif /* WC_RSA_NO_PADDING */
  13061. #if defined(WOLFSSL_HAVE_SP_RSA) && defined(USE_FAST_MATH)
  13062. static int rsa_even_mod_test(WC_RNG* rng, RsaKey* key)
  13063. {
  13064. byte* tmp = NULL;
  13065. size_t bytes;
  13066. int ret;
  13067. word32 inLen = 0;
  13068. #ifndef NO_ASN
  13069. word32 idx = 0;
  13070. #endif
  13071. word32 outSz = RSA_TEST_BYTES;
  13072. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  13073. word32 plainSz = RSA_TEST_BYTES;
  13074. #endif
  13075. #if !defined(USE_CERT_BUFFERS_2048) && !defined(USE_CERT_BUFFERS_3072) && \
  13076. !defined(USE_CERT_BUFFERS_4096) && !defined(NO_FILESYSTEM)
  13077. XFILE file;
  13078. #endif
  13079. WC_DECLARE_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
  13080. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  13081. WC_DECLARE_VAR(plain, byte, RSA_TEST_BYTES, HEAP_HINT);
  13082. #endif
  13083. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  13084. if (out == NULL
  13085. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  13086. || plain == NULL
  13087. #endif
  13088. ) {
  13089. ERROR_OUT(MEMORY_E, exit_rsa_even_mod);
  13090. }
  13091. #endif
  13092. #if defined(USE_CERT_BUFFERS_2048)
  13093. bytes = (size_t)sizeof_client_key_der_2048;
  13094. if (bytes < (size_t)sizeof_client_cert_der_2048)
  13095. bytes = (size_t)sizeof_client_cert_der_2048;
  13096. #else
  13097. bytes = FOURK_BUF;
  13098. #endif
  13099. tmp = (byte*)XMALLOC(bytes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13100. if (tmp == NULL
  13101. #ifdef WOLFSSL_ASYNC_CRYPT
  13102. || out == NULL || plain == NULL
  13103. #endif
  13104. ) {
  13105. ERROR_OUT(-7800, exit_rsa_even_mod);
  13106. }
  13107. #if defined(USE_CERT_BUFFERS_2048)
  13108. XMEMCPY(tmp, client_key_der_2048, (size_t)sizeof_client_key_der_2048);
  13109. #elif defined(USE_CERT_BUFFERS_3072)
  13110. XMEMCPY(tmp, client_key_der_3072, (size_t)sizeof_client_key_der_3072);
  13111. #elif defined(USE_CERT_BUFFERS_4096)
  13112. XMEMCPY(tmp, client_key_der_4096, (size_t)sizeof_client_key_der_4096);
  13113. #elif !defined(NO_FILESYSTEM)
  13114. file = XFOPEN(clientKey, "rb");
  13115. if (!file) {
  13116. err_sys("can't open ./certs/client-key.der, "
  13117. "Please run from wolfSSL home dir", -40);
  13118. ERROR_OUT(-7801, exit_rsa_even_mod);
  13119. }
  13120. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  13121. XFCLOSE(file);
  13122. #else
  13123. /* No key to use. */
  13124. ERROR_OUT(-7802, exit_rsa_even_mod);
  13125. #endif /* USE_CERT_BUFFERS */
  13126. #ifndef NO_ASN
  13127. ret = wc_RsaPrivateKeyDecode(tmp, &idx, key, (word32)bytes);
  13128. if (ret != 0) {
  13129. ERROR_OUT(-7804, exit_rsa_even_mod);
  13130. }
  13131. #else
  13132. #ifdef USE_CERT_BUFFERS_2048
  13133. ret = mp_read_unsigned_bin(&key->n, &tmp[12], 256);
  13134. if (ret != 0) {
  13135. ERROR_OUT(-7804, exit_rsa_even_mod);
  13136. }
  13137. ret = mp_set_int(&key->e, WC_RSA_EXPONENT);
  13138. if (ret != 0) {
  13139. ERROR_OUT(-7804, exit_rsa_even_mod);
  13140. }
  13141. #ifndef NO_SIG_WRAPPER
  13142. modLen = 2048;
  13143. #endif
  13144. #else
  13145. #error Not supported yet!
  13146. #endif
  13147. #endif
  13148. key->n.dp[0] &= (mp_digit)-2;
  13149. if (ret != 0) {
  13150. ERROR_OUT(-7804, exit_rsa_even_mod);
  13151. }
  13152. /* after loading in key use tmp as the test buffer */
  13153. #if !(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION == 2) && \
  13154. (defined(WOLFSSL_SP_ARM64_ASM) || defined(WOLFSSL_SP_ARM32_ASM)))
  13155. /* The ARM64_ASM code that was FIPS validated did not return these expected
  13156. * failure codes. These tests cases were added after the assembly was
  13157. * in-lined in the module and validated, these tests will be available in
  13158. * the 140-3 module */
  13159. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_PUBLIC_ONLY)
  13160. inLen = 32;
  13161. outSz = wc_RsaEncryptSize(key);
  13162. XMEMSET(tmp, 7, plainSz);
  13163. ret = wc_RsaSSL_Sign(tmp, inLen, out, outSz, key, rng);
  13164. if (ret != MP_VAL && ret != MP_EXPTMOD_E && ret != MP_INVMOD_E) {
  13165. ERROR_OUT(-7806, exit_rsa_even_mod);
  13166. }
  13167. ret = wc_RsaSSL_Verify(out, outSz, tmp, inLen, key);
  13168. if (ret != MP_VAL && ret != MP_EXPTMOD_E) {
  13169. ERROR_OUT(-7808, exit_rsa_even_mod);
  13170. }
  13171. #endif
  13172. #ifdef WC_RSA_BLINDING
  13173. ret = wc_RsaSetRNG(key, rng);
  13174. if (ret < 0) {
  13175. ERROR_OUT(-7811, exit_rsa_even_mod);
  13176. }
  13177. #endif
  13178. /* test encrypt and decrypt using WC_RSA_NO_PAD */
  13179. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_PUBLIC_ONLY)
  13180. ret = wc_RsaPublicEncrypt(tmp, inLen, out, (int)outSz, key, rng);
  13181. if (ret != MP_VAL && ret != MP_EXPTMOD_E) {
  13182. ERROR_OUT(-7812, exit_rsa_even_mod);
  13183. }
  13184. #endif /* WOLFSSL_RSA_VERIFY_ONLY */
  13185. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  13186. ret = wc_RsaPrivateDecrypt(out, outSz, plain, (int)plainSz, key);
  13187. if (ret != MP_VAL && ret != MP_EXPTMOD_E && ret != MP_INVMOD_E) {
  13188. ERROR_OUT(-7813, exit_rsa_even_mod);
  13189. }
  13190. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  13191. #endif /* !(HAVE_FIPS_VERSION == 2 && WOLFSSL_SP_ARMxx_ASM) */
  13192. /* if making it to this point of code without hitting an ERROR_OUT then
  13193. * all tests have passed */
  13194. ret = 0;
  13195. exit_rsa_even_mod:
  13196. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13197. WC_FREE_VAR(out, HEAP_HINT);
  13198. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  13199. WC_FREE_VAR(plain, HEAP_HINT);
  13200. #endif
  13201. (void)out;
  13202. (void)outSz;
  13203. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  13204. (void)plain;
  13205. (void)plainSz;
  13206. #endif
  13207. (void)inLen;
  13208. (void)rng;
  13209. return ret;
  13210. }
  13211. #endif /* WOLFSSL_HAVE_SP_RSA */
  13212. #ifdef WOLFSSL_CERT_GEN
  13213. static int rsa_certgen_test(RsaKey* key, RsaKey* keypub, WC_RNG* rng, byte* tmp)
  13214. {
  13215. #ifdef WOLFSSL_SMALL_STACK
  13216. RsaKey *caKey = (RsaKey *)XMALLOC(sizeof *caKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13217. #ifdef WOLFSSL_TEST_CERT
  13218. DecodedCert *decode = (DecodedCert *)XMALLOC(sizeof *decode, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13219. #endif
  13220. #else
  13221. RsaKey caKey[1];
  13222. #ifdef WOLFSSL_TEST_CERT
  13223. DecodedCert decode[1];
  13224. #endif
  13225. #endif
  13226. byte* der = NULL;
  13227. int ret;
  13228. Cert* myCert = NULL;
  13229. int certSz;
  13230. size_t bytes3;
  13231. word32 idx3 = 0;
  13232. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  13233. XFILE file3;
  13234. #endif
  13235. #if defined(WOLFSSL_ALT_NAMES) && !defined(NO_ASN_TIME)
  13236. struct tm beforeTime;
  13237. struct tm afterTime;
  13238. #endif
  13239. const byte mySerial[8] = {1,2,3,4,5,6,7,8};
  13240. (void)keypub;
  13241. #ifdef WOLFSSL_SMALL_STACK
  13242. if (caKey == NULL)
  13243. ERROR_OUT(MEMORY_E, exit_rsa);
  13244. #ifdef WOLFSSL_TEST_CERT
  13245. if (decode == NULL)
  13246. ERROR_OUT(MEMORY_E, exit_rsa);
  13247. #endif
  13248. #endif
  13249. XMEMSET(caKey, 0, sizeof *caKey);
  13250. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13251. if (der == NULL) {
  13252. ERROR_OUT(-7820, exit_rsa);
  13253. }
  13254. myCert = (Cert*)XMALLOC(sizeof(Cert), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13255. if (myCert == NULL) {
  13256. ERROR_OUT(-7821, exit_rsa);
  13257. }
  13258. /* self signed */
  13259. if (wc_InitCert_ex(myCert, HEAP_HINT, devId)) {
  13260. ERROR_OUT(-7822, exit_rsa);
  13261. }
  13262. XMEMCPY(&myCert->subject, &certDefaultName, sizeof(CertName));
  13263. XMEMCPY(myCert->serial, mySerial, sizeof(mySerial));
  13264. myCert->serialSz = (int)sizeof(mySerial);
  13265. myCert->isCA = 1;
  13266. #ifndef NO_SHA256
  13267. myCert->sigType = CTC_SHA256wRSA;
  13268. #else
  13269. myCert->sigType = CTC_SHAwRSA;
  13270. #endif
  13271. #ifdef WOLFSSL_CERT_EXT
  13272. /* add Policies */
  13273. XSTRNCPY(myCert->certPolicies[0], "2.16.840.1.101.3.4.1.42",
  13274. CTC_MAX_CERTPOL_SZ);
  13275. XSTRNCPY(myCert->certPolicies[1], "1.2.840.113549.1.9.16.6.5",
  13276. CTC_MAX_CERTPOL_SZ);
  13277. myCert->certPoliciesNb = 2;
  13278. /* add SKID from the Public Key */
  13279. if (wc_SetSubjectKeyIdFromPublicKey(myCert, keypub, NULL) != 0) {
  13280. ERROR_OUT(-7823, exit_rsa);
  13281. }
  13282. /* add AKID from the Public Key */
  13283. if (wc_SetAuthKeyIdFromPublicKey(myCert, keypub, NULL) != 0) {
  13284. ERROR_OUT(-7824, exit_rsa);
  13285. }
  13286. /* add Key Usage */
  13287. if (wc_SetKeyUsage(myCert,"cRLSign,keyCertSign") != 0) {
  13288. ERROR_OUT(-7825, exit_rsa);
  13289. }
  13290. #ifdef WOLFSSL_EKU_OID
  13291. {
  13292. const char unique[] = "2.16.840.1.111111.100.1.10.1";
  13293. if (wc_SetExtKeyUsageOID(myCert, unique, sizeof(unique), 0,
  13294. HEAP_HINT) != 0) {
  13295. ERROR_OUT(-7826, exit_rsa);
  13296. }
  13297. }
  13298. #endif /* WOLFSSL_EKU_OID */
  13299. #endif /* WOLFSSL_CERT_EXT */
  13300. ret = 0;
  13301. do {
  13302. #if defined(WOLFSSL_ASYNC_CRYPT)
  13303. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13304. #endif
  13305. if (ret >= 0) {
  13306. ret = wc_MakeSelfCert(myCert, der, FOURK_BUF, key, rng);
  13307. }
  13308. } while (ret == WC_PENDING_E);
  13309. if (ret < 0) {
  13310. ERROR_OUT(-7827, exit_rsa);
  13311. }
  13312. certSz = ret;
  13313. #ifdef WOLFSSL_TEST_CERT
  13314. InitDecodedCert(decode, der, certSz, HEAP_HINT);
  13315. ret = ParseCert(decode, CERT_TYPE, NO_VERIFY, 0);
  13316. if (ret != 0) {
  13317. FreeDecodedCert(decode);
  13318. ERROR_OUT(-7828, exit_rsa);
  13319. }
  13320. FreeDecodedCert(decode);
  13321. #endif
  13322. ret = SaveDerAndPem(der, certSz, certDerFile, certPemFile,
  13323. CERT_TYPE, -5578);
  13324. if (ret != 0) {
  13325. goto exit_rsa;
  13326. }
  13327. /* Setup Certificate */
  13328. if (wc_InitCert_ex(myCert, HEAP_HINT, devId)) {
  13329. ERROR_OUT(-7829, exit_rsa);
  13330. }
  13331. #ifdef WOLFSSL_ALT_NAMES
  13332. /* Get CA Cert for testing */
  13333. #ifdef USE_CERT_BUFFERS_1024
  13334. XMEMCPY(tmp, ca_cert_der_1024, sizeof_ca_cert_der_1024);
  13335. bytes3 = sizeof_ca_cert_der_1024;
  13336. #elif defined(USE_CERT_BUFFERS_2048)
  13337. XMEMCPY(tmp, ca_cert_der_2048, sizeof_ca_cert_der_2048);
  13338. bytes3 = sizeof_ca_cert_der_2048;
  13339. #else
  13340. file3 = XFOPEN(rsaCaCertDerFile, "rb");
  13341. if (!file3) {
  13342. ERROR_OUT(-7830, exit_rsa);
  13343. }
  13344. bytes3 = XFREAD(tmp, 1, FOURK_BUF, file3);
  13345. XFCLOSE(file3);
  13346. #endif /* USE_CERT_BUFFERS */
  13347. #if !defined(NO_FILESYSTEM) && !defined(USE_CERT_BUFFERS_1024) && \
  13348. !defined(USE_CERT_BUFFERS_2048) && !defined(NO_ASN)
  13349. ret = wc_SetAltNames(myCert, rsaCaCertFile);
  13350. if (ret != 0) {
  13351. ERROR_OUT(-7831, exit_rsa);
  13352. }
  13353. #endif
  13354. /* get alt names from der */
  13355. ret = wc_SetAltNamesBuffer(myCert, tmp, (int)bytes3);
  13356. if (ret != 0) {
  13357. ERROR_OUT(-7832, exit_rsa);
  13358. }
  13359. /* get dates from der */
  13360. ret = wc_SetDatesBuffer(myCert, tmp, (int)bytes3);
  13361. if (ret != 0) {
  13362. ERROR_OUT(-7833, exit_rsa);
  13363. }
  13364. #ifndef NO_ASN_TIME
  13365. ret = wc_GetCertDates(myCert, &beforeTime, &afterTime);
  13366. if (ret < 0) {
  13367. ERROR_OUT(-7834, exit_rsa);
  13368. }
  13369. #endif
  13370. #endif /* WOLFSSL_ALT_NAMES */
  13371. /* Get CA Key */
  13372. #ifdef USE_CERT_BUFFERS_1024
  13373. XMEMCPY(tmp, ca_key_der_1024, sizeof_ca_key_der_1024);
  13374. bytes3 = sizeof_ca_key_der_1024;
  13375. #elif defined(USE_CERT_BUFFERS_2048)
  13376. XMEMCPY(tmp, ca_key_der_2048, sizeof_ca_key_der_2048);
  13377. bytes3 = sizeof_ca_key_der_2048;
  13378. #else
  13379. file3 = XFOPEN(rsaCaKeyFile, "rb");
  13380. if (!file3) {
  13381. ERROR_OUT(-7835, exit_rsa);
  13382. }
  13383. bytes3 = XFREAD(tmp, 1, FOURK_BUF, file3);
  13384. XFCLOSE(file3);
  13385. #endif /* USE_CERT_BUFFERS */
  13386. ret = wc_InitRsaKey(caKey, HEAP_HINT);
  13387. if (ret != 0) {
  13388. ERROR_OUT(-7836, exit_rsa);
  13389. }
  13390. ret = wc_RsaPrivateKeyDecode(tmp, &idx3, caKey, (word32)bytes3);
  13391. if (ret != 0) {
  13392. ERROR_OUT(-7837, exit_rsa);
  13393. }
  13394. #ifndef NO_SHA256
  13395. myCert->sigType = CTC_SHA256wRSA;
  13396. #else
  13397. myCert->sigType = CTC_SHAwRSA;
  13398. #endif
  13399. XMEMCPY(&myCert->subject, &certDefaultName, sizeof(CertName));
  13400. #ifdef WOLFSSL_CERT_EXT
  13401. /* add Policies */
  13402. XSTRNCPY(myCert->certPolicies[0], "2.16.840.1.101.3.4.1.42",
  13403. CTC_MAX_CERTPOL_SZ);
  13404. myCert->certPoliciesNb =1;
  13405. /* add SKID from the Public Key */
  13406. if (wc_SetSubjectKeyIdFromPublicKey(myCert, key, NULL) != 0) {
  13407. ERROR_OUT(-7838, exit_rsa);
  13408. }
  13409. /* add AKID from the CA certificate */
  13410. #if defined(USE_CERT_BUFFERS_2048)
  13411. ret = wc_SetAuthKeyIdFromCert(myCert, ca_cert_der_2048,
  13412. sizeof_ca_cert_der_2048);
  13413. #elif defined(USE_CERT_BUFFERS_1024)
  13414. ret = wc_SetAuthKeyIdFromCert(myCert, ca_cert_der_1024,
  13415. sizeof_ca_cert_der_1024);
  13416. #else
  13417. ret = wc_SetAuthKeyId(myCert, rsaCaCertFile);
  13418. #endif
  13419. if (ret != 0) {
  13420. ERROR_OUT(-7839, exit_rsa);
  13421. }
  13422. /* add Key Usage */
  13423. if (wc_SetKeyUsage(myCert,"keyEncipherment,keyAgreement") != 0) {
  13424. ERROR_OUT(-7840, exit_rsa);
  13425. }
  13426. #endif /* WOLFSSL_CERT_EXT */
  13427. #if defined(USE_CERT_BUFFERS_2048)
  13428. ret = wc_SetIssuerBuffer(myCert, ca_cert_der_2048,
  13429. sizeof_ca_cert_der_2048);
  13430. #elif defined(USE_CERT_BUFFERS_1024)
  13431. ret = wc_SetIssuerBuffer(myCert, ca_cert_der_1024,
  13432. sizeof_ca_cert_der_1024);
  13433. #else
  13434. ret = wc_SetIssuer(myCert, rsaCaCertFile);
  13435. #endif
  13436. if (ret < 0) {
  13437. ERROR_OUT(-7841, exit_rsa);
  13438. }
  13439. certSz = wc_MakeCert(myCert, der, FOURK_BUF, key, NULL, rng);
  13440. if (certSz < 0) {
  13441. ERROR_OUT(-7842, exit_rsa);
  13442. }
  13443. ret = 0;
  13444. do {
  13445. #if defined(WOLFSSL_ASYNC_CRYPT)
  13446. ret = wc_AsyncWait(ret, &caKey->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13447. #endif
  13448. if (ret >= 0) {
  13449. ret = wc_SignCert(myCert->bodySz, myCert->sigType, der, FOURK_BUF,
  13450. caKey, NULL, rng);
  13451. }
  13452. } while (ret == WC_PENDING_E);
  13453. if (ret < 0) {
  13454. ERROR_OUT(-7843, exit_rsa);
  13455. }
  13456. certSz = ret;
  13457. #ifdef WOLFSSL_TEST_CERT
  13458. InitDecodedCert(decode, der, certSz, HEAP_HINT);
  13459. ret = ParseCert(decode, CERT_TYPE, NO_VERIFY, 0);
  13460. if (ret != 0) {
  13461. FreeDecodedCert(decode);
  13462. ERROR_OUT(-7844, exit_rsa);
  13463. }
  13464. FreeDecodedCert(decode);
  13465. #endif
  13466. ret = SaveDerAndPem(der, certSz, otherCertDerFile, otherCertPemFile,
  13467. CERT_TYPE, -5598);
  13468. if (ret != 0) {
  13469. goto exit_rsa;
  13470. }
  13471. exit_rsa:
  13472. #ifdef WOLFSSL_SMALL_STACK
  13473. if (caKey != NULL) {
  13474. wc_FreeRsaKey(caKey);
  13475. XFREE(caKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13476. }
  13477. #ifdef WOLFSSL_TEST_CERT
  13478. if (decode != NULL)
  13479. XFREE(decode, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13480. #endif
  13481. #else
  13482. wc_FreeRsaKey(caKey);
  13483. #endif
  13484. XFREE(myCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13485. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13486. return ret;
  13487. }
  13488. #endif
  13489. #if !defined(NO_RSA) && defined(HAVE_ECC) && defined(WOLFSSL_CERT_GEN)
  13490. /* Make Cert / Sign example for ECC cert and RSA CA */
  13491. static int rsa_ecc_certgen_test(WC_RNG* rng, byte* tmp)
  13492. {
  13493. #ifdef WOLFSSL_SMALL_STACK
  13494. RsaKey *caKey = (RsaKey *)XMALLOC(sizeof *caKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13495. ecc_key *caEccKey = (ecc_key *)XMALLOC(sizeof *caEccKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13496. ecc_key *caEccKeyPub = (ecc_key *)XMALLOC(sizeof *caEccKeyPub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13497. #ifdef WOLFSSL_TEST_CERT
  13498. DecodedCert *decode = (DecodedCert *)XMALLOC(sizeof *decode, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13499. #endif
  13500. #else
  13501. RsaKey caKey[1];
  13502. ecc_key caEccKey[1];
  13503. ecc_key caEccKeyPub[1];
  13504. #ifdef WOLFSSL_TEST_CERT
  13505. DecodedCert decode[1];
  13506. #endif
  13507. #endif
  13508. byte* der = NULL;
  13509. Cert* myCert = NULL;
  13510. int certSz;
  13511. size_t bytes3;
  13512. word32 idx3 = 0;
  13513. #if (!defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)) \
  13514. || !defined(USE_CERT_BUFFERS_256)
  13515. XFILE file3;
  13516. #endif
  13517. int ret;
  13518. #ifdef WOLFSSL_SMALL_STACK
  13519. if ((caKey == NULL) || (caEccKey == NULL) || (caEccKeyPub == NULL)
  13520. #ifdef WOLFSSL_TEST_CERT
  13521. || (decode == NULL)
  13522. #endif
  13523. )
  13524. ERROR_OUT(MEMORY_E, exit_rsa);
  13525. #endif
  13526. XMEMSET(caKey, 0, sizeof *caKey);
  13527. XMEMSET(caEccKey, 0, sizeof *caEccKey);
  13528. XMEMSET(caEccKeyPub, 0, sizeof *caEccKeyPub);
  13529. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13530. if (der == NULL) {
  13531. ERROR_OUT(-7850, exit_rsa);
  13532. }
  13533. myCert = (Cert*)XMALLOC(sizeof(Cert), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13534. if (myCert == NULL) {
  13535. ERROR_OUT(-7851, exit_rsa);
  13536. }
  13537. /* Get CA Key */
  13538. #ifdef USE_CERT_BUFFERS_1024
  13539. XMEMCPY(tmp, ca_key_der_1024, sizeof_ca_key_der_1024);
  13540. bytes3 = sizeof_ca_key_der_1024;
  13541. #elif defined(USE_CERT_BUFFERS_2048)
  13542. XMEMCPY(tmp, ca_key_der_2048, sizeof_ca_key_der_2048);
  13543. bytes3 = sizeof_ca_key_der_2048;
  13544. #else
  13545. file3 = XFOPEN(rsaCaKeyFile, "rb");
  13546. if (!file3) {
  13547. ERROR_OUT(-7852, exit_rsa);
  13548. }
  13549. bytes3 = XFREAD(tmp, 1, FOURK_BUF, file3);
  13550. XFCLOSE(file3);
  13551. #endif /* USE_CERT_BUFFERS */
  13552. ret = wc_InitRsaKey(caKey, HEAP_HINT);
  13553. if (ret != 0) {
  13554. ERROR_OUT(-7853, exit_rsa);
  13555. }
  13556. ret = wc_RsaPrivateKeyDecode(tmp, &idx3, caKey, (word32)bytes3);
  13557. if (ret != 0) {
  13558. ERROR_OUT(-7854, exit_rsa);
  13559. }
  13560. /* Get Cert Key */
  13561. #ifdef USE_CERT_BUFFERS_256
  13562. XMEMCPY(tmp, ecc_key_pub_der_256, sizeof_ecc_key_pub_der_256);
  13563. bytes3 = sizeof_ecc_key_pub_der_256;
  13564. #else
  13565. file3 = XFOPEN(eccKeyPubFileDer, "rb");
  13566. if (!file3) {
  13567. ERROR_OUT(-7855, exit_rsa);
  13568. }
  13569. bytes3 = XFREAD(tmp, 1, FOURK_BUF, file3);
  13570. XFCLOSE(file3);
  13571. #endif
  13572. ret = wc_ecc_init_ex(caEccKeyPub, HEAP_HINT, devId);
  13573. if (ret != 0) {
  13574. ERROR_OUT(-7856, exit_rsa);
  13575. }
  13576. idx3 = 0;
  13577. ret = wc_EccPublicKeyDecode(tmp, &idx3, caEccKeyPub, (word32)bytes3);
  13578. if (ret != 0) {
  13579. ERROR_OUT(-7857, exit_rsa);
  13580. }
  13581. /* Setup Certificate */
  13582. if (wc_InitCert_ex(myCert, HEAP_HINT, devId)) {
  13583. ERROR_OUT(-7858, exit_rsa);
  13584. }
  13585. #ifndef NO_SHA256
  13586. myCert->sigType = CTC_SHA256wRSA;
  13587. #else
  13588. myCert->sigType = CTC_SHAwRSA;
  13589. #endif
  13590. XMEMCPY(&myCert->subject, &certDefaultName, sizeof(CertName));
  13591. #ifdef WOLFSSL_CERT_EXT
  13592. /* add Policies */
  13593. XSTRNCPY(myCert->certPolicies[0], "2.4.589440.587.101.2.1.9632587.1",
  13594. CTC_MAX_CERTPOL_SZ);
  13595. XSTRNCPY(myCert->certPolicies[1], "1.2.13025.489.1.113549",
  13596. CTC_MAX_CERTPOL_SZ);
  13597. myCert->certPoliciesNb = 2;
  13598. /* add SKID from the Public Key */
  13599. if (wc_SetSubjectKeyIdFromPublicKey(myCert, NULL, caEccKeyPub) != 0) {
  13600. ERROR_OUT(-7859, exit_rsa);
  13601. }
  13602. /* add AKID from the CA certificate */
  13603. #if defined(USE_CERT_BUFFERS_2048)
  13604. ret = wc_SetAuthKeyIdFromCert(myCert, ca_cert_der_2048,
  13605. sizeof_ca_cert_der_2048);
  13606. #elif defined(USE_CERT_BUFFERS_1024)
  13607. ret = wc_SetAuthKeyIdFromCert(myCert, ca_cert_der_1024,
  13608. sizeof_ca_cert_der_1024);
  13609. #else
  13610. ret = wc_SetAuthKeyId(myCert, rsaCaCertFile);
  13611. #endif
  13612. if (ret != 0) {
  13613. ERROR_OUT(-7860, exit_rsa);
  13614. }
  13615. /* add Key Usage */
  13616. if (wc_SetKeyUsage(myCert, certKeyUsage) != 0) {
  13617. ERROR_OUT(-7861, exit_rsa);
  13618. }
  13619. #endif /* WOLFSSL_CERT_EXT */
  13620. #if defined(USE_CERT_BUFFERS_2048)
  13621. ret = wc_SetIssuerBuffer(myCert, ca_cert_der_2048,
  13622. sizeof_ca_cert_der_2048);
  13623. #elif defined(USE_CERT_BUFFERS_1024)
  13624. ret = wc_SetIssuerBuffer(myCert, ca_cert_der_1024,
  13625. sizeof_ca_cert_der_1024);
  13626. #else
  13627. ret = wc_SetIssuer(myCert, rsaCaCertFile);
  13628. #endif
  13629. if (ret < 0) {
  13630. ERROR_OUT(-7862, exit_rsa);
  13631. }
  13632. certSz = wc_MakeCert(myCert, der, FOURK_BUF, NULL, caEccKeyPub, rng);
  13633. if (certSz < 0) {
  13634. ERROR_OUT(-7863, exit_rsa);
  13635. }
  13636. ret = 0;
  13637. do {
  13638. #if defined(WOLFSSL_ASYNC_CRYPT)
  13639. ret = wc_AsyncWait(ret, &caEccKey->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13640. #endif
  13641. if (ret >= 0) {
  13642. ret = wc_SignCert(myCert->bodySz, myCert->sigType, der,
  13643. FOURK_BUF, caKey, NULL, rng);
  13644. }
  13645. } while (ret == WC_PENDING_E);
  13646. if (ret < 0) {
  13647. ERROR_OUT(-7864, exit_rsa);
  13648. }
  13649. certSz = ret;
  13650. #ifdef WOLFSSL_TEST_CERT
  13651. InitDecodedCert(decode, der, certSz, 0);
  13652. ret = ParseCert(decode, CERT_TYPE, NO_VERIFY, 0);
  13653. if (ret != 0) {
  13654. FreeDecodedCert(decode);
  13655. ERROR_OUT(-7865, exit_rsa);
  13656. }
  13657. FreeDecodedCert(decode);
  13658. #endif
  13659. ret = SaveDerAndPem(der, certSz, certEccRsaDerFile, certEccRsaPemFile,
  13660. CERT_TYPE, -5616);
  13661. if (ret != 0) {
  13662. goto exit_rsa;
  13663. }
  13664. exit_rsa:
  13665. #ifdef WOLFSSL_SMALL_STACK
  13666. if (caKey != NULL) {
  13667. wc_FreeRsaKey(caKey);
  13668. XFREE(caKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13669. }
  13670. if (caEccKey != NULL) {
  13671. wc_ecc_free(caEccKey);
  13672. XFREE(caEccKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13673. }
  13674. if (caEccKeyPub != NULL) {
  13675. wc_ecc_free(caEccKeyPub);
  13676. XFREE(caEccKeyPub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13677. }
  13678. #ifdef WOLFSSL_TEST_CERT
  13679. if (decode != NULL)
  13680. XFREE(decode, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13681. #endif
  13682. #else
  13683. wc_FreeRsaKey(caKey);
  13684. wc_ecc_free(caEccKey);
  13685. wc_ecc_free(caEccKeyPub);
  13686. #endif
  13687. XFREE(myCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13688. myCert = NULL;
  13689. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13690. der = NULL;
  13691. if (ret >= 0)
  13692. ret = 0;
  13693. return ret;
  13694. }
  13695. #endif /* !NO_RSA && HAVE_ECC && WOLFSSL_CERT_GEN */
  13696. #ifdef WOLFSSL_KEY_GEN
  13697. static int rsa_keygen_test(WC_RNG* rng)
  13698. {
  13699. #ifdef WOLFSSL_SMALL_STACK
  13700. RsaKey *genKey = (RsaKey *)XMALLOC(sizeof *genKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13701. #else
  13702. RsaKey genKey[1];
  13703. #endif
  13704. int ret;
  13705. byte* der = NULL;
  13706. #ifndef WOLFSSL_CRYPTOCELL
  13707. word32 idx = 0;
  13708. #endif
  13709. int derSz = 0;
  13710. #if !defined(WOLFSSL_SP_MATH) && !defined(HAVE_FIPS)
  13711. int keySz = 1024;
  13712. #else
  13713. int keySz = 2048;
  13714. #endif
  13715. #ifdef WOLFSSL_SMALL_STACK
  13716. if (! genKey)
  13717. ERROR_OUT(MEMORY_E, exit_rsa);
  13718. #endif
  13719. XMEMSET(genKey, 0, sizeof *genKey);
  13720. ret = wc_InitRsaKey_ex(genKey, HEAP_HINT, devId);
  13721. if (ret != 0) {
  13722. ERROR_OUT(-7870, exit_rsa);
  13723. }
  13724. #ifdef HAVE_FIPS
  13725. for (;;) {
  13726. #endif
  13727. ret = wc_MakeRsaKey(genKey, keySz, WC_RSA_EXPONENT, rng);
  13728. #if defined(WOLFSSL_ASYNC_CRYPT)
  13729. ret = wc_AsyncWait(ret, &genKey->asyncDev, WC_ASYNC_FLAG_NONE);
  13730. #endif
  13731. #ifdef HAVE_FIPS
  13732. if (ret == PRIME_GEN_E)
  13733. continue;
  13734. break;
  13735. }
  13736. #endif
  13737. if (ret != 0) {
  13738. ERROR_OUT(-7871, exit_rsa);
  13739. }
  13740. TEST_SLEEP();
  13741. #ifdef WOLFSSL_RSA_KEY_CHECK
  13742. ret = wc_CheckRsaKey(genKey);
  13743. if (ret != 0) {
  13744. ERROR_OUT(-7872, exit_rsa);
  13745. }
  13746. #endif
  13747. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13748. if (der == NULL) {
  13749. ERROR_OUT(-7873, exit_rsa);
  13750. }
  13751. derSz = wc_RsaKeyToDer(genKey, der, FOURK_BUF);
  13752. if (derSz < 0) {
  13753. ERROR_OUT(-7874, exit_rsa);
  13754. }
  13755. ret = SaveDerAndPem(der, derSz, keyDerFile, keyPemFile,
  13756. PRIVATEKEY_TYPE, -5555);
  13757. if (ret != 0) {
  13758. goto exit_rsa;
  13759. }
  13760. wc_FreeRsaKey(genKey);
  13761. ret = wc_InitRsaKey(genKey, HEAP_HINT);
  13762. if (ret != 0) {
  13763. ERROR_OUT(-7875, exit_rsa);
  13764. }
  13765. #ifndef WOLFSSL_CRYPTOCELL
  13766. idx = 0;
  13767. /* The private key part of the key gen pairs from cryptocell can't be exported */
  13768. ret = wc_RsaPrivateKeyDecode(der, &idx, genKey, derSz);
  13769. if (ret != 0) {
  13770. ERROR_OUT(-7876, exit_rsa);
  13771. }
  13772. #endif /* WOLFSSL_CRYPTOCELL */
  13773. exit_rsa:
  13774. #ifdef WOLFSSL_SMALL_STACK
  13775. if (genKey) {
  13776. wc_FreeRsaKey(genKey);
  13777. XFREE(genKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13778. }
  13779. #else
  13780. wc_FreeRsaKey(genKey);
  13781. #endif
  13782. if (der != NULL) {
  13783. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13784. der = NULL;
  13785. }
  13786. return ret;
  13787. }
  13788. #endif
  13789. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  13790. #if !defined(WC_NO_RSA_OAEP) && !defined(WC_NO_RNG) && \
  13791. !defined(HAVE_FAST_RSA) && !defined(HAVE_USER_RSA) && \
  13792. (!defined(HAVE_FIPS) || \
  13793. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))) \
  13794. && !defined(WOLF_CRYPTO_CB_ONLY_RSA)
  13795. static int rsa_oaep_padding_test(RsaKey* key, WC_RNG* rng)
  13796. {
  13797. int ret = 0;
  13798. word32 idx = 0;
  13799. const char inStr[] = TEST_STRING;
  13800. const word32 inLen = (word32)TEST_STRING_SZ;
  13801. const word32 outSz = RSA_TEST_BYTES;
  13802. const word32 plainSz = RSA_TEST_BYTES;
  13803. byte* res = NULL;
  13804. WC_DECLARE_VAR(in, byte, TEST_STRING_SZ, HEAP_HINT);
  13805. WC_DECLARE_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
  13806. WC_DECLARE_VAR(plain, byte, RSA_TEST_BYTES, HEAP_HINT);
  13807. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  13808. if (in == NULL || out == NULL || plain == NULL)
  13809. ERROR_OUT(MEMORY_E, exit_rsa);
  13810. #endif
  13811. XMEMCPY(in, inStr, inLen);
  13812. #ifndef NO_SHA
  13813. do {
  13814. #if defined(WOLFSSL_ASYNC_CRYPT)
  13815. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13816. #endif
  13817. if (ret >= 0) {
  13818. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, rng,
  13819. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA, WC_MGF1SHA1, NULL, 0);
  13820. }
  13821. } while (ret == WC_PENDING_E);
  13822. if (ret < 0) {
  13823. ERROR_OUT(-7918, exit_rsa);
  13824. }
  13825. TEST_SLEEP();
  13826. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  13827. idx = (word32)ret;
  13828. do {
  13829. #if defined(WOLFSSL_ASYNC_CRYPT)
  13830. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13831. #endif
  13832. if (ret >= 0) {
  13833. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  13834. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA, WC_MGF1SHA1, NULL, 0);
  13835. }
  13836. } while (ret == WC_PENDING_E);
  13837. if (ret < 0) {
  13838. ERROR_OUT(-7919, exit_rsa);
  13839. }
  13840. if (XMEMCMP(plain, in, inLen)) {
  13841. ERROR_OUT(-7920, exit_rsa);
  13842. }
  13843. TEST_SLEEP();
  13844. #endif /* NO_SHA */
  13845. #endif
  13846. #ifndef NO_SHA256
  13847. XMEMSET(plain, 0, plainSz);
  13848. do {
  13849. #if defined(WOLFSSL_ASYNC_CRYPT)
  13850. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13851. #endif
  13852. if (ret >= 0) {
  13853. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, rng,
  13854. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0);
  13855. }
  13856. } while (ret == WC_PENDING_E);
  13857. if (ret < 0) {
  13858. ERROR_OUT(-7921, exit_rsa);
  13859. }
  13860. TEST_SLEEP();
  13861. idx = (word32)ret;
  13862. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  13863. do {
  13864. #if defined(WOLFSSL_ASYNC_CRYPT)
  13865. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13866. #endif
  13867. if (ret >= 0) {
  13868. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  13869. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0);
  13870. }
  13871. } while (ret == WC_PENDING_E);
  13872. if (ret < 0) {
  13873. ERROR_OUT(-7922, exit_rsa);
  13874. }
  13875. if (XMEMCMP(plain, in, inLen)) {
  13876. ERROR_OUT(-7923, exit_rsa);
  13877. }
  13878. TEST_SLEEP();
  13879. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  13880. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  13881. do {
  13882. #if defined(WOLFSSL_ASYNC_CRYPT)
  13883. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13884. #endif
  13885. if (ret >= 0) {
  13886. ret = wc_RsaPrivateDecryptInline_ex(out, idx, &res, key,
  13887. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0);
  13888. }
  13889. } while (ret == WC_PENDING_E);
  13890. if (ret < 0) {
  13891. ERROR_OUT(-7924, exit_rsa);
  13892. }
  13893. if (ret != (int)inLen) {
  13894. ERROR_OUT(-7925, exit_rsa);
  13895. }
  13896. if (XMEMCMP(res, in, inLen)) {
  13897. ERROR_OUT(-7926, exit_rsa);
  13898. }
  13899. TEST_SLEEP();
  13900. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  13901. /* check fails if not using the same optional label */
  13902. XMEMSET(plain, 0, plainSz);
  13903. do {
  13904. #if defined(WOLFSSL_ASYNC_CRYPT)
  13905. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13906. #endif
  13907. if (ret >= 0) {
  13908. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, rng,
  13909. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0);
  13910. }
  13911. } while (ret == WC_PENDING_E);
  13912. if (ret < 0) {
  13913. ERROR_OUT(-7927, exit_rsa);
  13914. }
  13915. TEST_SLEEP();
  13916. /* TODO: investigate why Cavium Nitrox doesn't detect decrypt error here */
  13917. #if !defined(HAVE_CAVIUM) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
  13918. !defined(WOLFSSL_CRYPTOCELL)
  13919. /* label is unused in cryptocell so it won't detect decrypt error due to label */
  13920. idx = (word32)ret;
  13921. do {
  13922. #if defined(WOLFSSL_ASYNC_CRYPT)
  13923. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13924. #endif
  13925. if (ret >= 0) {
  13926. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  13927. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, in, inLen);
  13928. }
  13929. } while (ret == WC_PENDING_E);
  13930. if (ret > 0) { /* in this case decrypt should fail */
  13931. ERROR_OUT(-7928, exit_rsa);
  13932. }
  13933. ret = 0;
  13934. TEST_SLEEP();
  13935. #endif /* !HAVE_CAVIUM */
  13936. /* check using optional label with encrypt/decrypt */
  13937. XMEMSET(plain, 0, plainSz);
  13938. do {
  13939. #if defined(WOLFSSL_ASYNC_CRYPT)
  13940. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13941. #endif
  13942. if (ret >= 0) {
  13943. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, rng,
  13944. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, in, inLen);
  13945. }
  13946. } while (ret == WC_PENDING_E);
  13947. if (ret < 0) {
  13948. ERROR_OUT(-7929, exit_rsa);
  13949. }
  13950. TEST_SLEEP();
  13951. idx = (word32)ret;
  13952. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  13953. do {
  13954. #if defined(WOLFSSL_ASYNC_CRYPT)
  13955. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13956. #endif
  13957. if (ret >= 0) {
  13958. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  13959. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, in, inLen);
  13960. }
  13961. } while (ret == WC_PENDING_E);
  13962. if (ret < 0) {
  13963. ERROR_OUT(-7930, exit_rsa);
  13964. }
  13965. if (XMEMCMP(plain, in, inLen)) {
  13966. ERROR_OUT(-7931, exit_rsa);
  13967. }
  13968. TEST_SLEEP();
  13969. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  13970. #ifndef NO_SHA
  13971. /* check fail using mismatch hash algorithms */
  13972. XMEMSET(plain, 0, plainSz);
  13973. do {
  13974. #if defined(WOLFSSL_ASYNC_CRYPT)
  13975. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13976. #endif
  13977. if (ret >= 0) {
  13978. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, rng,
  13979. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA, WC_MGF1SHA1, in, inLen);
  13980. }
  13981. } while (ret == WC_PENDING_E);
  13982. if (ret < 0) {
  13983. ERROR_OUT(-7932, exit_rsa);
  13984. }
  13985. TEST_SLEEP();
  13986. /* TODO: investigate why Cavium Nitrox doesn't detect decrypt error here */
  13987. #if !defined(HAVE_CAVIUM) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
  13988. !defined(WOLFSSL_CRYPTOCELL)
  13989. idx = (word32)ret;
  13990. do {
  13991. #if defined(WOLFSSL_ASYNC_CRYPT)
  13992. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13993. #endif
  13994. if (ret >= 0) {
  13995. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  13996. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256,
  13997. in, inLen);
  13998. }
  13999. } while (ret == WC_PENDING_E);
  14000. if (ret > 0) { /* should fail */
  14001. ERROR_OUT(-7933, exit_rsa);
  14002. }
  14003. ret = 0;
  14004. TEST_SLEEP();
  14005. #endif /* !HAVE_CAVIUM */
  14006. #endif /* NO_SHA */
  14007. #endif /* NO_SHA256 */
  14008. #ifdef WOLFSSL_SHA512
  14009. /* Check valid RSA key size is used while using hash length of SHA512
  14010. If key size is less than (hash length * 2) + 2 then is invalid use
  14011. and test, since OAEP padding requires this.
  14012. BAD_FUNC_ARG is returned when this case is not met */
  14013. if (wc_RsaEncryptSize(key) > ((int)WC_SHA512_DIGEST_SIZE * 2) + 2) {
  14014. XMEMSET(plain, 0, plainSz);
  14015. do {
  14016. #if defined(WOLFSSL_ASYNC_CRYPT)
  14017. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14018. #endif
  14019. if (ret >= 0) {
  14020. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, rng,
  14021. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA512, WC_MGF1SHA512, NULL, 0);
  14022. }
  14023. } while (ret == WC_PENDING_E);
  14024. if (ret < 0) {
  14025. ERROR_OUT(-7934, exit_rsa);
  14026. }
  14027. TEST_SLEEP();
  14028. idx = ret;
  14029. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  14030. do {
  14031. #if defined(WOLFSSL_ASYNC_CRYPT)
  14032. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14033. #endif
  14034. if (ret >= 0) {
  14035. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  14036. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA512, WC_MGF1SHA512, NULL, 0);
  14037. }
  14038. } while (ret == WC_PENDING_E);
  14039. if (ret < 0) {
  14040. ERROR_OUT(-7935, exit_rsa);
  14041. }
  14042. if (XMEMCMP(plain, in, inLen)) {
  14043. ERROR_OUT(-7936, exit_rsa);
  14044. }
  14045. TEST_SLEEP();
  14046. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  14047. }
  14048. #endif /* WOLFSSL_SHA512 */
  14049. /* check using pkcsv15 padding with _ex API */
  14050. XMEMSET(plain, 0, plainSz);
  14051. do {
  14052. #if defined(WOLFSSL_ASYNC_CRYPT)
  14053. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14054. #endif
  14055. if (ret >= 0) {
  14056. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, rng,
  14057. WC_RSA_PKCSV15_PAD, WC_HASH_TYPE_NONE, 0, NULL, 0);
  14058. }
  14059. } while (ret == WC_PENDING_E);
  14060. if (ret < 0) {
  14061. ERROR_OUT(-7937, exit_rsa);
  14062. }
  14063. TEST_SLEEP();
  14064. idx = (word32)ret;
  14065. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  14066. do {
  14067. #if defined(WOLFSSL_ASYNC_CRYPT)
  14068. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14069. #endif
  14070. if (ret >= 0) {
  14071. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  14072. WC_RSA_PKCSV15_PAD, WC_HASH_TYPE_NONE, 0, NULL, 0);
  14073. }
  14074. } while (ret == WC_PENDING_E);
  14075. if (ret < 0) {
  14076. ERROR_OUT(-7938, exit_rsa);
  14077. }
  14078. if (XMEMCMP(plain, in, inLen)) {
  14079. ERROR_OUT(-7939, exit_rsa);
  14080. }
  14081. TEST_SLEEP();
  14082. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  14083. exit_rsa:
  14084. WC_FREE_VAR(in, HEAP_HINT);
  14085. WC_FREE_VAR(out, HEAP_HINT);
  14086. WC_FREE_VAR(plain, HEAP_HINT);
  14087. (void)idx;
  14088. (void)inStr;
  14089. (void)res;
  14090. if (ret >= 0)
  14091. ret = 0;
  14092. return ret;
  14093. }
  14094. #endif
  14095. #endif
  14096. WOLFSSL_TEST_SUBROUTINE int rsa_test(void)
  14097. {
  14098. int ret;
  14099. size_t bytes;
  14100. WC_RNG rng;
  14101. #ifdef WOLFSSL_SMALL_STACK
  14102. byte* tmp = NULL;
  14103. byte* der = NULL;
  14104. RsaKey *key = (RsaKey *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14105. #else
  14106. RsaKey key[1];
  14107. byte tmp[FOURK_BUF];
  14108. #endif
  14109. #if defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_CERT_GEN)
  14110. #ifdef WOLFSSL_SMALL_STACK
  14111. RsaKey *keypub = (RsaKey *)XMALLOC(sizeof *keypub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14112. #else
  14113. RsaKey keypub[1];
  14114. #endif
  14115. #endif
  14116. word32 idx = 0;
  14117. const char inStr[] = TEST_STRING;
  14118. const word32 inLen = (word32)TEST_STRING_SZ;
  14119. const word32 outSz = RSA_TEST_BYTES;
  14120. const word32 plainSz = RSA_TEST_BYTES;
  14121. byte* res = NULL;
  14122. #ifndef NO_SIG_WRAPPER
  14123. int modLen;
  14124. #endif
  14125. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  14126. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096) && \
  14127. !defined(NO_FILESYSTEM)
  14128. XFILE file;
  14129. #ifdef WOLFSSL_TEST_CERT
  14130. XFILE file2;
  14131. #endif
  14132. #endif
  14133. #ifdef WOLFSSL_TEST_CERT
  14134. #ifdef WOLFSSL_SMALL_STACK
  14135. DecodedCert *cert = (DecodedCert *)XMALLOC(sizeof *cert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14136. #else
  14137. DecodedCert cert[1];
  14138. #endif
  14139. #ifndef NO_ASN_TIME
  14140. struct tm timearg;
  14141. const byte* date;
  14142. byte dateFormat;
  14143. int dateLength;
  14144. #endif
  14145. #endif
  14146. WC_DECLARE_VAR(in, byte, TEST_STRING_SZ, HEAP_HINT);
  14147. WC_DECLARE_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
  14148. WC_DECLARE_VAR(plain, byte, RSA_TEST_BYTES, HEAP_HINT);
  14149. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  14150. if (in == NULL || out == NULL || plain == NULL)
  14151. ERROR_OUT(MEMORY_E, exit_rsa);
  14152. #endif
  14153. XMEMCPY(in, inStr, inLen);
  14154. #ifdef WOLFSSL_SMALL_STACK
  14155. if (key == NULL)
  14156. ERROR_OUT(MEMORY_E, exit_rsa);
  14157. #if defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_CERT_GEN)
  14158. if (keypub == NULL)
  14159. ERROR_OUT(MEMORY_E, exit_rsa);
  14160. #endif
  14161. #ifdef WOLFSSL_TEST_CERT
  14162. if (cert == NULL)
  14163. ERROR_OUT(MEMORY_E, exit_rsa);
  14164. #endif
  14165. #endif /* WOLFSSL_SMALL_STACK */
  14166. /* initialize stack structures */
  14167. XMEMSET(&rng, 0, sizeof(rng));
  14168. XMEMSET(key, 0, sizeof *key);
  14169. #if defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_CERT_GEN)
  14170. XMEMSET(keypub, 0, sizeof *keypub);
  14171. #endif
  14172. #if !defined(HAVE_USER_RSA) && !defined(NO_ASN)
  14173. ret = rsa_decode_test(key);
  14174. if (ret != 0)
  14175. ERROR_OUT(ret, exit_rsa);
  14176. #endif
  14177. #ifdef USE_CERT_BUFFERS_1024
  14178. bytes = (size_t)sizeof_client_key_der_1024;
  14179. if (bytes < (size_t)sizeof_client_cert_der_1024)
  14180. bytes = (size_t)sizeof_client_cert_der_1024;
  14181. #elif defined(USE_CERT_BUFFERS_2048)
  14182. bytes = (size_t)sizeof_client_key_der_2048;
  14183. if (bytes < (size_t)sizeof_client_cert_der_2048)
  14184. bytes = (size_t)sizeof_client_cert_der_2048;
  14185. #elif defined(USE_CERT_BUFFERS_3072)
  14186. bytes = (size_t)sizeof_client_key_der_3072;
  14187. if (bytes < (size_t)sizeof_client_cert_der_3072)
  14188. bytes = (size_t)sizeof_client_cert_der_3072;
  14189. #elif defined(USE_CERT_BUFFERS_4096)
  14190. bytes = (size_t)sizeof_client_key_der_4096;
  14191. if (bytes < (size_t)sizeof_client_cert_der_4096)
  14192. bytes = (size_t)sizeof_client_cert_der_4096;
  14193. #else
  14194. bytes = FOURK_BUF;
  14195. #endif
  14196. #if defined(WOLFSSL_SMALL_STACK)
  14197. tmp = (byte*)XMALLOC(bytes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14198. if (tmp == NULL)
  14199. ERROR_OUT(-7900, exit_rsa);
  14200. #endif
  14201. #ifdef USE_CERT_BUFFERS_1024
  14202. XMEMCPY(tmp, client_key_der_1024, (size_t)sizeof_client_key_der_1024);
  14203. #elif defined(USE_CERT_BUFFERS_2048)
  14204. XMEMCPY(tmp, client_key_der_2048, (size_t)sizeof_client_key_der_2048);
  14205. #elif defined(USE_CERT_BUFFERS_3072)
  14206. XMEMCPY(tmp, client_key_der_3072, (size_t)sizeof_client_key_der_3072);
  14207. #elif defined(USE_CERT_BUFFERS_4096)
  14208. XMEMCPY(tmp, client_key_der_4096, (size_t)sizeof_client_key_der_4096);
  14209. #elif !defined(NO_FILESYSTEM)
  14210. file = XFOPEN(clientKey, "rb");
  14211. if (!file) {
  14212. err_sys("can't open ./certs/client-key.der, "
  14213. "Please run from wolfSSL home dir", -40);
  14214. ERROR_OUT(-7901, exit_rsa);
  14215. }
  14216. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  14217. XFCLOSE(file);
  14218. #else
  14219. /* No key to use. */
  14220. ERROR_OUT(-7902, exit_rsa);
  14221. #endif /* USE_CERT_BUFFERS */
  14222. ret = wc_InitRsaKey_ex(key, HEAP_HINT, devId);
  14223. if (ret != 0) {
  14224. ERROR_OUT(-7903, exit_rsa);
  14225. }
  14226. #ifndef NO_ASN
  14227. ret = wc_RsaPrivateKeyDecode(tmp, &idx, key, (word32)bytes);
  14228. if (ret != 0) {
  14229. ERROR_OUT(-7904, exit_rsa);
  14230. }
  14231. #ifndef NO_SIG_WRAPPER
  14232. modLen = wc_RsaEncryptSize(key);
  14233. #endif
  14234. #else
  14235. #ifdef USE_CERT_BUFFERS_2048
  14236. ret = mp_read_unsigned_bin(&key->n, &tmp[12], 256);
  14237. if (ret != 0) {
  14238. ERROR_OUT(-7905, exit_rsa);
  14239. }
  14240. ret = mp_set_int(&key->e, WC_RSA_EXPONENT);
  14241. if (ret != 0) {
  14242. ERROR_OUT(-7906, exit_rsa);
  14243. }
  14244. #ifndef NO_SIG_WRAPPER
  14245. modLen = 2048;
  14246. #endif
  14247. #else
  14248. #error Not supported yet!
  14249. #endif
  14250. #endif
  14251. #ifndef WC_NO_RNG
  14252. #ifndef HAVE_FIPS
  14253. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  14254. #else
  14255. ret = wc_InitRng(&rng);
  14256. #endif
  14257. if (ret != 0) {
  14258. ERROR_OUT(-7907, exit_rsa);
  14259. }
  14260. #endif
  14261. #ifndef NO_SIG_WRAPPER
  14262. ret = rsa_sig_test(key, sizeof *key, modLen, &rng);
  14263. if (ret != 0)
  14264. goto exit_rsa;
  14265. #endif
  14266. #ifdef WC_RSA_NONBLOCK
  14267. ret = rsa_nb_test(key, in, inLen, out, outSz, plain, plainSz, &rng);
  14268. if (ret != 0)
  14269. goto exit_rsa;
  14270. #endif
  14271. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
  14272. !defined(WC_NO_RNG) && !defined(WOLF_CRYPTO_CB_ONLY_RSA)
  14273. do {
  14274. #if defined(WOLFSSL_ASYNC_CRYPT)
  14275. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14276. #endif
  14277. if (ret >= 0) {
  14278. ret = wc_RsaPublicEncrypt(in, inLen, out, outSz, key, &rng);
  14279. }
  14280. } while (ret == WC_PENDING_E);
  14281. if (ret < 0) {
  14282. ERROR_OUT(-7908, exit_rsa);
  14283. }
  14284. TEST_SLEEP();
  14285. #ifdef WC_RSA_BLINDING
  14286. {
  14287. int tmpret = ret;
  14288. ret = wc_RsaSetRNG(key, &rng);
  14289. if (ret < 0) {
  14290. ERROR_OUT(-7909, exit_rsa);
  14291. }
  14292. ret = tmpret;
  14293. }
  14294. #endif
  14295. idx = (word32)ret; /* save off encrypted length */
  14296. do {
  14297. #if defined(WOLFSSL_ASYNC_CRYPT)
  14298. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14299. #endif
  14300. if (ret >= 0) {
  14301. ret = wc_RsaPrivateDecrypt(out, idx, plain, plainSz, key);
  14302. }
  14303. } while (ret == WC_PENDING_E);
  14304. if (ret < 0) {
  14305. ERROR_OUT(-7910, exit_rsa);
  14306. }
  14307. if (XMEMCMP(plain, in, inLen)) {
  14308. ERROR_OUT(-7911, exit_rsa);
  14309. }
  14310. TEST_SLEEP();
  14311. do {
  14312. #if defined(WOLFSSL_ASYNC_CRYPT)
  14313. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14314. #endif
  14315. if (ret >= 0) {
  14316. ret = wc_RsaPrivateDecryptInline(out, idx, &res, key);
  14317. }
  14318. } while (ret == WC_PENDING_E);
  14319. if (ret < 0) {
  14320. ERROR_OUT(-7912, exit_rsa);
  14321. }
  14322. if (ret != (int)inLen) {
  14323. ERROR_OUT(-7913, exit_rsa);
  14324. }
  14325. if (XMEMCMP(res, in, inLen)) {
  14326. ERROR_OUT(-7914, exit_rsa);
  14327. }
  14328. TEST_SLEEP();
  14329. do {
  14330. #if defined(WOLFSSL_ASYNC_CRYPT)
  14331. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14332. #endif
  14333. if (ret >= 0) {
  14334. ret = wc_RsaSSL_Sign(in, inLen, out, outSz, key, &rng);
  14335. }
  14336. } while (ret == WC_PENDING_E);
  14337. if (ret < 0) {
  14338. ERROR_OUT(-7915, exit_rsa);
  14339. }
  14340. TEST_SLEEP();
  14341. #elif defined(WOLFSSL_PUBLIC_MP)
  14342. {
  14343. static byte signature_2048[] = {
  14344. 0x07, 0x6f, 0xc9, 0x85, 0x73, 0x9e, 0x21, 0x79,
  14345. 0x47, 0xf1, 0xa3, 0xd7, 0xf4, 0x27, 0x29, 0xbe,
  14346. 0x99, 0x5d, 0xac, 0xb2, 0x10, 0x3f, 0x95, 0xda,
  14347. 0x89, 0x23, 0xb8, 0x96, 0x13, 0x57, 0x72, 0x30,
  14348. 0xa1, 0xfe, 0x5a, 0x68, 0x9c, 0x99, 0x9d, 0x1e,
  14349. 0x05, 0xa4, 0x80, 0xb0, 0xbb, 0xd9, 0xd9, 0xa1,
  14350. 0x69, 0x97, 0x74, 0xb3, 0x41, 0x21, 0x3b, 0x47,
  14351. 0xf5, 0x51, 0xb1, 0xfb, 0xc7, 0xaa, 0xcc, 0xdc,
  14352. 0xcd, 0x76, 0xa0, 0x28, 0x4d, 0x27, 0x14, 0xa4,
  14353. 0xb9, 0x41, 0x68, 0x7c, 0xb3, 0x66, 0xe6, 0x6f,
  14354. 0x40, 0x76, 0xe4, 0x12, 0xfd, 0xae, 0x29, 0xb5,
  14355. 0x63, 0x60, 0x87, 0xce, 0x49, 0x6b, 0xf3, 0x05,
  14356. 0x9a, 0x14, 0xb5, 0xcc, 0xcd, 0xf7, 0x30, 0x95,
  14357. 0xd2, 0x72, 0x52, 0x1d, 0x5b, 0x7e, 0xef, 0x4a,
  14358. 0x02, 0x96, 0x21, 0x6c, 0x55, 0xa5, 0x15, 0xb1,
  14359. 0x57, 0x63, 0x2c, 0xa3, 0x8e, 0x9d, 0x3d, 0x45,
  14360. 0xcc, 0xb8, 0xe6, 0xa1, 0xc8, 0x59, 0xcd, 0xf5,
  14361. 0xdc, 0x0a, 0x51, 0xb6, 0x9d, 0xfb, 0xf4, 0x6b,
  14362. 0xfd, 0x32, 0x71, 0x6e, 0xcf, 0xcb, 0xb3, 0xd9,
  14363. 0xe0, 0x4a, 0x77, 0x34, 0xd6, 0x61, 0xf5, 0x7c,
  14364. 0xf9, 0xa9, 0xa4, 0xb0, 0x8e, 0x3b, 0xd6, 0x04,
  14365. 0xe0, 0xde, 0x2b, 0x5b, 0x5a, 0xbf, 0xd9, 0xef,
  14366. 0x8d, 0xa3, 0xf5, 0xb1, 0x67, 0xf3, 0xb9, 0x72,
  14367. 0x0a, 0x37, 0x12, 0x35, 0x6c, 0x8e, 0x10, 0x8b,
  14368. 0x38, 0x06, 0x16, 0x4b, 0x20, 0x20, 0x13, 0x00,
  14369. 0x2e, 0x6d, 0xc2, 0x59, 0x23, 0x67, 0x4a, 0x6d,
  14370. 0xa1, 0x46, 0x8b, 0xee, 0xcf, 0x44, 0xb4, 0x3e,
  14371. 0x56, 0x75, 0x00, 0x68, 0xb5, 0x7d, 0x0f, 0x20,
  14372. 0x79, 0x5d, 0x7f, 0x12, 0x15, 0x32, 0x89, 0x61,
  14373. 0x6b, 0x29, 0xb7, 0x52, 0xf5, 0x25, 0xd8, 0x98,
  14374. 0xe8, 0x6f, 0xf9, 0x22, 0xb4, 0xbb, 0xe5, 0xff,
  14375. 0xd0, 0x92, 0x86, 0x9a, 0x88, 0xa2, 0xaf, 0x6b
  14376. };
  14377. ret = sizeof(signature_2048);
  14378. XMEMCPY(out, signature_2048, ret);
  14379. }
  14380. #endif
  14381. #if !defined(WC_NO_RNG) && !defined(WC_NO_RSA_OAEP) && \
  14382. ((!defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_PUBLIC_ONLY)) || \
  14383. defined(WOLFSSL_PUBLIC_MP)) && !defined(WOLF_CRYPTO_CB_ONLY_RSA)
  14384. idx = (word32)ret;
  14385. XMEMSET(plain, 0, plainSz);
  14386. do {
  14387. #if defined(WOLFSSL_ASYNC_CRYPT)
  14388. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14389. #endif
  14390. if (ret >= 0) {
  14391. #ifndef WOLFSSL_RSA_VERIFY_INLINE
  14392. #if defined(WOLFSSL_CRYPTOCELL)
  14393. /*
  14394. Cryptocell requires the input data and signature byte array to verify.
  14395. first argument must be the input data
  14396. second argument must be the length of input data
  14397. third argument must be the signature byte array or the output from
  14398. wc_RsaSSL_Sign()
  14399. fourth argument must be the length of the signature byte array
  14400. */
  14401. ret = wc_RsaSSL_Verify(in, inLen, out, outSz, key);
  14402. #else
  14403. ret = wc_RsaSSL_Verify(out, idx, plain, plainSz, key);
  14404. #endif /* WOLFSSL_CRYPTOCELL */
  14405. #else
  14406. byte* dec = NULL;
  14407. ret = wc_RsaSSL_VerifyInline(out, idx, &dec, key);
  14408. if (ret > 0) {
  14409. XMEMCPY(plain, dec, ret);
  14410. }
  14411. #endif
  14412. }
  14413. } while (ret == WC_PENDING_E);
  14414. if (ret < 0) {
  14415. ERROR_OUT(-7916, exit_rsa);
  14416. }
  14417. if (XMEMCMP(plain, in, (size_t)ret)) {
  14418. ERROR_OUT(-7917, exit_rsa);
  14419. }
  14420. TEST_SLEEP();
  14421. #endif
  14422. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  14423. #if !defined(WC_NO_RSA_OAEP) && !defined(WC_NO_RNG)
  14424. #if !defined(HAVE_FAST_RSA) && !defined(HAVE_USER_RSA) && \
  14425. (!defined(HAVE_FIPS) || \
  14426. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))) \
  14427. && !defined(WOLF_CRYPTO_CB_ONLY_RSA)
  14428. ret = rsa_oaep_padding_test(key, &rng);
  14429. if (ret != 0)
  14430. return ret;
  14431. #endif /* !HAVE_FAST_RSA && !HAVE_FIPS */
  14432. #endif /* WC_NO_RSA_OAEP && !WC_NO_RNG */
  14433. #endif /* WOLFSSL_RSA_VERIFY_ONLY */
  14434. #if !defined(HAVE_FIPS) && !defined(HAVE_USER_RSA) && !defined(NO_ASN) \
  14435. && !defined(WOLFSSL_RSA_VERIFY_ONLY)
  14436. ret = rsa_export_key_test(key);
  14437. if (ret != 0)
  14438. return ret;
  14439. #endif
  14440. #if !defined(NO_ASN) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
  14441. !defined(WOLFSSL_RSA_VERIFY_ONLY)
  14442. ret = rsa_flatten_test(key);
  14443. if (ret != 0)
  14444. return ret;
  14445. #endif
  14446. #if !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_ASN) && \
  14447. !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  14448. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096)
  14449. (void)clientCert;
  14450. #endif
  14451. #ifdef WOLFSSL_TEST_CERT
  14452. #if defined(WOLFSSL_MDK_ARM)
  14453. #define sizeof(s) XSTRLEN((char *)(s))
  14454. #endif
  14455. #ifdef USE_CERT_BUFFERS_1024
  14456. XMEMCPY(tmp, client_cert_der_1024, (size_t)sizeof_client_cert_der_1024);
  14457. bytes = (size_t)sizeof_client_cert_der_1024;
  14458. #elif defined(USE_CERT_BUFFERS_2048)
  14459. XMEMCPY(tmp, client_cert_der_2048, (size_t)sizeof_client_cert_der_2048);
  14460. bytes = (size_t)sizeof_client_cert_der_2048;
  14461. #elif defined(USE_CERT_BUFFERS_3072)
  14462. XMEMCPY(tmp, client_cert_der_3072, (size_t)sizeof_client_cert_der_3072);
  14463. bytes = (size_t)sizeof_client_cert_der_3072;
  14464. #elif defined(USE_CERT_BUFFERS_4096)
  14465. XMEMCPY(tmp, client_cert_der_4096, (size_t)sizeof_client_cert_der_4096);
  14466. bytes = (size_t)sizeof_client_cert_der_4096;
  14467. #elif !defined(NO_FILESYSTEM)
  14468. file2 = XFOPEN(clientCert, "rb");
  14469. if (!file2) {
  14470. ERROR_OUT(-7940, exit_rsa);
  14471. }
  14472. bytes = XFREAD(tmp, 1, FOURK_BUF, file2);
  14473. XFCLOSE(file2);
  14474. #else
  14475. /* No certificate to use. */
  14476. ERROR_OUT(-7941, exit_rsa);
  14477. #endif
  14478. #ifdef sizeof
  14479. #undef sizeof
  14480. #endif
  14481. InitDecodedCert(cert, tmp, (word32)bytes, NULL);
  14482. ret = ParseCert(cert, CERT_TYPE, NO_VERIFY, NULL);
  14483. if (ret != 0) {
  14484. FreeDecodedCert(cert);
  14485. ERROR_OUT(-7942, exit_rsa);
  14486. }
  14487. #ifndef NO_ASN_TIME
  14488. ret = wc_GetDateInfo(cert->afterDate, cert->afterDateLen, &date,
  14489. &dateFormat, &dateLength);
  14490. if (ret != 0) {
  14491. FreeDecodedCert(cert);
  14492. ERROR_OUT(-7943, exit_rsa);
  14493. }
  14494. ret = wc_GetDateAsCalendarTime(date, dateLength, dateFormat, &timearg);
  14495. if (ret != 0) {
  14496. FreeDecodedCert(cert);
  14497. ERROR_OUT(-7944, exit_rsa);
  14498. }
  14499. #endif
  14500. FreeDecodedCert(cert);
  14501. #endif /* WOLFSSL_TEST_CERT */
  14502. #ifdef WOLFSSL_CERT_EXT
  14503. #ifdef USE_CERT_BUFFERS_1024
  14504. XMEMCPY(tmp, client_keypub_der_1024, sizeof_client_keypub_der_1024);
  14505. bytes = sizeof_client_keypub_der_1024;
  14506. #elif defined(USE_CERT_BUFFERS_2048)
  14507. XMEMCPY(tmp, client_keypub_der_2048, sizeof_client_keypub_der_2048);
  14508. bytes = sizeof_client_keypub_der_2048;
  14509. #elif defined(USE_CERT_BUFFERS_3072)
  14510. XMEMCPY(tmp, client_keypub_der_3072, sizeof_client_keypub_der_3072);
  14511. bytes = sizeof_client_keypub_der_3072;
  14512. #elif defined(USE_CERT_BUFFERS_4096)
  14513. XMEMCPY(tmp, client_keypub_der_4096, sizeof_client_keypub_der_4096);
  14514. bytes = sizeof_client_keypub_der_4096;
  14515. #else
  14516. file = XFOPEN(clientKeyPub, "rb");
  14517. if (!file) {
  14518. err_sys("can't open ./certs/client-keyPub.der, "
  14519. "Please run from wolfSSL home dir", -40);
  14520. ERROR_OUT(-7945, exit_rsa);
  14521. }
  14522. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  14523. XFCLOSE(file);
  14524. #endif /* USE_CERT_BUFFERS */
  14525. ret = wc_InitRsaKey(keypub, HEAP_HINT);
  14526. if (ret != 0) {
  14527. ERROR_OUT(-7946, exit_rsa);
  14528. }
  14529. idx = 0;
  14530. ret = wc_RsaPublicKeyDecode(tmp, &idx, keypub, (word32)bytes);
  14531. if (ret != 0) {
  14532. ERROR_OUT(-7947, exit_rsa);
  14533. }
  14534. #endif /* WOLFSSL_CERT_EXT */
  14535. #ifdef WOLFSSL_KEY_GEN
  14536. ret = rsa_keygen_test(&rng);
  14537. if (ret != 0)
  14538. goto exit_rsa;
  14539. #endif
  14540. #ifdef WOLFSSL_CERT_GEN
  14541. /* Make Cert / Sign example for RSA cert and RSA CA */
  14542. ret = rsa_certgen_test(key, keypub, &rng, tmp);
  14543. if (ret != 0)
  14544. goto exit_rsa;
  14545. #if !defined(NO_RSA) && defined(HAVE_ECC)
  14546. ret = rsa_ecc_certgen_test(&rng, tmp);
  14547. if (ret != 0)
  14548. goto exit_rsa;
  14549. #endif
  14550. #if defined(WOLFSSL_CERT_REQ) && !defined(WOLFSSL_NO_MALLOC)
  14551. {
  14552. Cert *req;
  14553. int derSz;
  14554. #ifndef WOLFSSL_SMALL_STACK
  14555. byte* der = NULL;
  14556. #endif
  14557. req = (Cert *)XMALLOC(sizeof *req, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14558. if (! req)
  14559. ERROR_OUT(MEMORY_E, exit_rsa);
  14560. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,DYNAMIC_TYPE_TMP_BUFFER);
  14561. if (der == NULL) {
  14562. ERROR_OUT(-7964, exit_rsa);
  14563. }
  14564. if (wc_InitCert_ex(req, HEAP_HINT, devId)) {
  14565. ERROR_OUT(-7965, exit_rsa);
  14566. }
  14567. req->version = 0;
  14568. req->isCA = 1;
  14569. XSTRNCPY(req->challengePw, "wolf123", CTC_NAME_SIZE);
  14570. XMEMCPY(&req->subject, &certDefaultName, sizeof(CertName));
  14571. #ifndef NO_SHA256
  14572. req->sigType = CTC_SHA256wRSA;
  14573. #else
  14574. req->sigType = CTC_SHAwRSA;
  14575. #endif
  14576. #ifdef WOLFSSL_CERT_EXT
  14577. /* add SKID from the Public Key */
  14578. if (wc_SetSubjectKeyIdFromPublicKey(req, keypub, NULL) != 0) {
  14579. ERROR_OUT(-7966, exit_rsa);
  14580. }
  14581. /* add Key Usage */
  14582. if (wc_SetKeyUsage(req, certKeyUsage2) != 0) {
  14583. ERROR_OUT(-7967, exit_rsa);
  14584. }
  14585. /* add Extended Key Usage */
  14586. if (wc_SetExtKeyUsage(req, "serverAuth,clientAuth,codeSigning,"
  14587. "emailProtection,timeStamping,OCSPSigning") != 0) {
  14588. ERROR_OUT(-7968, exit_rsa);
  14589. }
  14590. #ifdef WOLFSSL_EKU_OID
  14591. {
  14592. WOLFSSL_SMALL_STACK_STATIC const char unique[] = "2.16.840.1.111111.100.1.10.1";
  14593. if (wc_SetExtKeyUsageOID(req, unique, sizeof(unique), 0,
  14594. HEAP_HINT) != 0) {
  14595. ERROR_OUT(-7969, exit_rsa);
  14596. }
  14597. }
  14598. #endif /* WOLFSSL_EKU_OID */
  14599. #endif /* WOLFSSL_CERT_EXT */
  14600. derSz = wc_MakeCertReq(req, der, FOURK_BUF, key, NULL);
  14601. if (derSz < 0) {
  14602. ERROR_OUT(-7970, exit_rsa);
  14603. }
  14604. #ifdef WOLFSSL_CERT_EXT
  14605. /* Try again with "any" flag set, will override all others */
  14606. if (wc_SetExtKeyUsage(req, "any") != 0) {
  14607. ERROR_OUT(-7971, exit_rsa);
  14608. }
  14609. derSz = wc_MakeCertReq(req, der, FOURK_BUF, key, NULL);
  14610. if (derSz < 0) {
  14611. ERROR_OUT(-7972, exit_rsa);
  14612. }
  14613. #endif /* WOLFSSL_CERT_EXT */
  14614. ret = 0;
  14615. do {
  14616. #if defined(WOLFSSL_ASYNC_CRYPT)
  14617. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14618. #endif
  14619. if (ret >= 0) {
  14620. ret = wc_SignCert(req->bodySz, req->sigType, der, FOURK_BUF,
  14621. key, NULL, &rng);
  14622. }
  14623. } while (ret == WC_PENDING_E);
  14624. if (ret < 0) {
  14625. ERROR_OUT(-7973, exit_rsa);
  14626. }
  14627. derSz = ret;
  14628. ret = SaveDerAndPem(der, derSz, certReqDerFile, certReqPemFile,
  14629. CERTREQ_TYPE, -5650);
  14630. if (ret != 0) {
  14631. goto exit_rsa;
  14632. }
  14633. derSz = wc_MakeCertReq_ex(req, der, FOURK_BUF, RSA_TYPE, key);
  14634. if (derSz < 0) {
  14635. ERROR_OUT(-7974, exit_rsa);
  14636. }
  14637. /* Test getting the size of the buffer without providing the buffer.
  14638. * derSz is set to the "largest buffer" we are willing to allocate. */
  14639. derSz = wc_MakeCertReq(req, NULL, 10000, key, NULL);
  14640. if (derSz < 0) {
  14641. ERROR_OUT(-7975, exit_rsa);
  14642. }
  14643. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14644. XFREE(req, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14645. der = NULL;
  14646. }
  14647. #endif /* WOLFSSL_CERT_REQ */
  14648. #endif /* WOLFSSL_CERT_GEN */
  14649. #if defined(WC_RSA_PSS) && !defined(HAVE_FIPS_VERSION) /* not supported with FIPSv1 */
  14650. /* Need to create known good signatures to test with this. */
  14651. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
  14652. !defined(WOLF_CRYPTO_CB_ONLY_RSA)
  14653. ret = rsa_pss_test(&rng, key);
  14654. if (ret != 0)
  14655. goto exit_rsa;
  14656. #endif
  14657. #endif
  14658. #if defined(WOLFSSL_HAVE_SP_RSA) && defined(USE_FAST_MATH)
  14659. #ifdef WOLFSSL_SMALL_STACK
  14660. /* New key to be loaded in rsa_even_mod_test(). */
  14661. if (key != NULL)
  14662. #endif
  14663. wc_FreeRsaKey(key);
  14664. /* New key to be loaded in rsa_even_mod_test(). */
  14665. ret = rsa_even_mod_test(&rng, key);
  14666. #endif
  14667. exit_rsa:
  14668. #ifdef WOLFSSL_SMALL_STACK
  14669. if (key != NULL) {
  14670. wc_FreeRsaKey(key);
  14671. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14672. }
  14673. #if defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_CERT_GEN)
  14674. if (keypub != NULL) {
  14675. wc_FreeRsaKey(keypub);
  14676. XFREE(keypub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14677. }
  14678. #endif
  14679. #ifdef WOLFSSL_TEST_CERT
  14680. if (cert != NULL)
  14681. XFREE(cert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14682. #endif
  14683. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14684. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14685. #else
  14686. wc_FreeRsaKey(key);
  14687. #if defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_CERT_GEN)
  14688. wc_FreeRsaKey(keypub);
  14689. #endif
  14690. #endif /* WOLFSSL_SMALL_STACK */
  14691. wc_FreeRng(&rng);
  14692. WC_FREE_VAR(in, HEAP_HINT);
  14693. WC_FREE_VAR(out, HEAP_HINT);
  14694. WC_FREE_VAR(plain, HEAP_HINT);
  14695. (void)res;
  14696. (void)bytes;
  14697. (void)idx;
  14698. (void)in;
  14699. (void)out;
  14700. (void)plain;
  14701. (void)idx;
  14702. (void)inStr;
  14703. (void)inLen;
  14704. (void)outSz;
  14705. (void)plainSz;
  14706. /* ret can be greater then 0 with certgen but all negative values should
  14707. * be returned and treated as an error */
  14708. if (ret >= 0) {
  14709. return 0;
  14710. }
  14711. else {
  14712. return ret;
  14713. }
  14714. }
  14715. #endif /* !NO_RSA */
  14716. #ifndef NO_DH
  14717. static int dh_fips_generate_test(WC_RNG *rng)
  14718. {
  14719. int ret = 0;
  14720. #ifdef WOLFSSL_SMALL_STACK
  14721. DhKey *key = (DhKey *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14722. #else
  14723. DhKey key[1];
  14724. #endif
  14725. WOLFSSL_SMALL_STACK_STATIC const byte p[] = {
  14726. 0xc5, 0x7c, 0xa2, 0x4f, 0x4b, 0xd6, 0x8c, 0x3c,
  14727. 0xda, 0xc7, 0xba, 0xaa, 0xea, 0x2e, 0x5c, 0x1e,
  14728. 0x18, 0xb2, 0x7b, 0x8c, 0x55, 0x65, 0x9f, 0xea,
  14729. 0xe0, 0xa1, 0x36, 0x53, 0x2b, 0x36, 0xe0, 0x4e,
  14730. 0x3e, 0x64, 0xa9, 0xe4, 0xfc, 0x8f, 0x32, 0x62,
  14731. 0x97, 0xe4, 0xbe, 0xf7, 0xc1, 0xde, 0x07, 0x5a,
  14732. 0x89, 0x28, 0xf3, 0xfe, 0x4f, 0xfe, 0x68, 0xbc,
  14733. 0xfb, 0x0a, 0x7c, 0xa4, 0xb3, 0x14, 0x48, 0x89,
  14734. 0x9f, 0xaf, 0xb8, 0x43, 0xe2, 0xa0, 0x62, 0x5c,
  14735. 0xb4, 0x88, 0x3f, 0x06, 0x50, 0x11, 0xfe, 0x65,
  14736. 0x8d, 0x49, 0xd2, 0xf5, 0x4b, 0x74, 0x79, 0xdb,
  14737. 0x06, 0x62, 0x92, 0x89, 0xed, 0xda, 0xcb, 0x87,
  14738. 0x37, 0x16, 0xd2, 0xa1, 0x7a, 0xe8, 0xde, 0x92,
  14739. 0xee, 0x3e, 0x41, 0x4a, 0x91, 0x5e, 0xed, 0xf3,
  14740. 0x6c, 0x6b, 0x7e, 0xfd, 0x15, 0x92, 0x18, 0xfc,
  14741. 0xa7, 0xac, 0x42, 0x85, 0x57, 0xe9, 0xdc, 0xda,
  14742. 0x55, 0xc9, 0x8b, 0x28, 0x9e, 0xc1, 0xc4, 0x46,
  14743. 0x4d, 0x88, 0xed, 0x62, 0x8e, 0xdb, 0x3f, 0xb9,
  14744. 0xd7, 0xc8, 0xe3, 0xcf, 0xb8, 0x34, 0x2c, 0xd2,
  14745. 0x6f, 0x28, 0x06, 0x41, 0xe3, 0x66, 0x8c, 0xfc,
  14746. 0x72, 0xff, 0x26, 0x3b, 0x6b, 0x6c, 0x6f, 0x73,
  14747. 0xde, 0xf2, 0x90, 0x29, 0xe0, 0x61, 0x32, 0xc4,
  14748. 0x12, 0x74, 0x09, 0x52, 0xec, 0xf3, 0x1b, 0xa6,
  14749. 0x45, 0x98, 0xac, 0xf9, 0x1c, 0x65, 0x8e, 0x3a,
  14750. 0x91, 0x84, 0x4b, 0x23, 0x8a, 0xb2, 0x3c, 0xc9,
  14751. 0xfa, 0xea, 0xf1, 0x38, 0xce, 0xd8, 0x05, 0xe0,
  14752. 0xfa, 0x44, 0x68, 0x1f, 0xeb, 0xd9, 0x57, 0xb8,
  14753. 0x4a, 0x97, 0x5b, 0x88, 0xc5, 0xf1, 0xbb, 0xb0,
  14754. 0x49, 0xc3, 0x91, 0x7c, 0xd3, 0x13, 0xb9, 0x47,
  14755. 0xbb, 0x91, 0x8f, 0xe5, 0x26, 0x07, 0xab, 0xa9,
  14756. 0xc5, 0xd0, 0x3d, 0x95, 0x41, 0x26, 0x92, 0x9d,
  14757. 0x13, 0x67, 0xf2, 0x7e, 0x11, 0x88, 0xdc, 0x2d
  14758. };
  14759. WOLFSSL_SMALL_STACK_STATIC const byte g[] = {
  14760. 0x4a, 0x1a, 0xf3, 0xa4, 0x92, 0xe9, 0xee, 0x74,
  14761. 0x6e, 0x57, 0xd5, 0x8c, 0x2c, 0x5b, 0x41, 0x41,
  14762. 0x5e, 0xd4, 0x55, 0x19, 0xdc, 0xd9, 0x32, 0x91,
  14763. 0xf7, 0xfd, 0xc2, 0x57, 0xff, 0x03, 0x14, 0xdb,
  14764. 0xf1, 0xb7, 0x60, 0x0c, 0x43, 0x59, 0x3f, 0xff,
  14765. 0xac, 0xf1, 0x80, 0x9a, 0x15, 0x6f, 0xd8, 0x6e,
  14766. 0xb7, 0x85, 0x18, 0xc8, 0xec, 0x4e, 0x59, 0x4a,
  14767. 0xe2, 0x91, 0x43, 0x4c, 0xeb, 0x95, 0xb6, 0x2e,
  14768. 0x9a, 0xea, 0x53, 0x68, 0x80, 0x64, 0x69, 0x40,
  14769. 0xf9, 0xec, 0xbd, 0x85, 0x89, 0x26, 0x97, 0x67,
  14770. 0xaf, 0xb0, 0xad, 0x00, 0x1b, 0xd4, 0xfd, 0x94,
  14771. 0xd3, 0xe9, 0x92, 0xb1, 0xb4, 0xbc, 0x5a, 0xaa,
  14772. 0x92, 0x80, 0x89, 0x3b, 0x39, 0x05, 0x6c, 0x22,
  14773. 0x26, 0xfe, 0x5a, 0x28, 0x6c, 0x37, 0x50, 0x5a,
  14774. 0x38, 0x99, 0xcf, 0xf3, 0xc1, 0x96, 0x45, 0xdc,
  14775. 0x01, 0xcb, 0x20, 0x87, 0xa5, 0x00, 0x8c, 0xf5,
  14776. 0x4d, 0xc2, 0xef, 0xb8, 0x9b, 0xd1, 0x87, 0xbe,
  14777. 0xed, 0xd5, 0x0a, 0x29, 0x15, 0x34, 0x59, 0x4c,
  14778. 0x3a, 0x05, 0x22, 0x05, 0x44, 0x4f, 0x9f, 0xc8,
  14779. 0x47, 0x12, 0x24, 0x8e, 0xa8, 0x79, 0xe4, 0x67,
  14780. 0xba, 0x4d, 0x5b, 0x75, 0x56, 0x95, 0xeb, 0xe8,
  14781. 0x8a, 0xfa, 0x8e, 0x01, 0x8c, 0x1b, 0x74, 0x63,
  14782. 0xd9, 0x2f, 0xf7, 0xd3, 0x44, 0x8f, 0xa8, 0xf5,
  14783. 0xaf, 0x6c, 0x4f, 0xdb, 0xe7, 0xc9, 0x6c, 0x71,
  14784. 0x22, 0xa3, 0x1d, 0xf1, 0x40, 0xb2, 0xe0, 0x9a,
  14785. 0xb6, 0x72, 0xc9, 0xc0, 0x13, 0x16, 0xa2, 0x4a,
  14786. 0xe1, 0x92, 0xc7, 0x54, 0x23, 0xab, 0x9d, 0xa1,
  14787. 0xa1, 0xe5, 0x0b, 0xed, 0xba, 0xe8, 0x84, 0x37,
  14788. 0xb2, 0xe7, 0xfe, 0x32, 0x8d, 0xfa, 0x1c, 0x53,
  14789. 0x77, 0x97, 0xc7, 0xf3, 0x48, 0xc9, 0xdb, 0x2d,
  14790. 0x75, 0x52, 0x9d, 0x42, 0x51, 0x78, 0x62, 0x68,
  14791. 0x05, 0x45, 0x15, 0xf8, 0xa2, 0x4e, 0xf3, 0x0b
  14792. };
  14793. WOLFSSL_SMALL_STACK_STATIC const byte q[] = {
  14794. 0xe0, 0x35, 0x37, 0xaf, 0xb2, 0x50, 0x91, 0x8e,
  14795. 0xf2, 0x62, 0x2b, 0xd9, 0x9f, 0x6c, 0x11, 0x75,
  14796. 0xec, 0x24, 0x5d, 0x78, 0x59, 0xe7, 0x8d, 0xb5,
  14797. 0x40, 0x52, 0xed, 0x41
  14798. };
  14799. WOLFSSL_SMALL_STACK_STATIC const byte q0[] = {
  14800. 0x00,
  14801. 0xe0, 0x35, 0x37, 0xaf, 0xb2, 0x50, 0x91, 0x8e,
  14802. 0xf2, 0x62, 0x2b, 0xd9, 0x9f, 0x6c, 0x11, 0x75,
  14803. 0xec, 0x24, 0x5d, 0x78, 0x59, 0xe7, 0x8d, 0xb5,
  14804. 0x40, 0x52, 0xed, 0x41
  14805. };
  14806. byte priv[256];
  14807. byte pub[256];
  14808. word32 privSz = sizeof(priv);
  14809. word32 pubSz = sizeof(pub);
  14810. #ifdef WOLFSSL_SMALL_STACK
  14811. if (key == NULL)
  14812. ERROR_OUT(MEMORY_E, exit_gen_test);
  14813. #endif
  14814. /* Parameter Validation testing. */
  14815. ret = wc_DhGenerateKeyPair(NULL, rng, priv, &privSz, pub, &pubSz);
  14816. if (ret != BAD_FUNC_ARG)
  14817. ERROR_OUT(-7980, exit_gen_test);
  14818. ret = wc_DhGenerateKeyPair(key, NULL, priv, &privSz, pub, &pubSz);
  14819. if (ret != BAD_FUNC_ARG)
  14820. ERROR_OUT(-7981, exit_gen_test);
  14821. ret = wc_DhGenerateKeyPair(key, rng, NULL, &privSz, pub, &pubSz);
  14822. if (ret != BAD_FUNC_ARG)
  14823. ERROR_OUT(-7982, exit_gen_test);
  14824. ret = wc_DhGenerateKeyPair(key, rng, priv, NULL, pub, &pubSz);
  14825. if (ret != BAD_FUNC_ARG)
  14826. ERROR_OUT(-7983, exit_gen_test);
  14827. ret = wc_DhGenerateKeyPair(key, rng, priv, &privSz, NULL, &pubSz);
  14828. if (ret != BAD_FUNC_ARG)
  14829. ERROR_OUT(-7984, exit_gen_test);
  14830. ret = wc_DhGenerateKeyPair(key, rng, priv, &privSz, pub, NULL);
  14831. if (ret != BAD_FUNC_ARG)
  14832. ERROR_OUT(-7985, exit_gen_test);
  14833. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  14834. if (ret != 0)
  14835. ERROR_OUT(-7986, exit_gen_test);
  14836. ret = wc_DhSetKey_ex(key, p, sizeof(p), g, sizeof(g), q0, sizeof(q0));
  14837. if (ret != 0) {
  14838. ERROR_OUT(-7987, exit_gen_test);
  14839. }
  14840. wc_FreeDhKey(key);
  14841. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  14842. if (ret != 0)
  14843. ERROR_OUT(-7988, exit_gen_test);
  14844. ret = wc_DhSetKey_ex(key, p, sizeof(p), g, sizeof(g), q, sizeof(q));
  14845. if (ret != 0) {
  14846. ERROR_OUT(-7989, exit_gen_test);
  14847. }
  14848. /* Use API. */
  14849. ret = wc_DhGenerateKeyPair(key, rng, priv, &privSz, pub, &pubSz);
  14850. #if defined(WOLFSSL_ASYNC_CRYPT)
  14851. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  14852. #endif
  14853. if (ret != 0) {
  14854. ERROR_OUT(-7990, exit_gen_test);
  14855. }
  14856. ret = wc_DhCheckPubKey_ex(key, pub, pubSz, q0, sizeof(q0));
  14857. if (ret != 0) {
  14858. ERROR_OUT(-7991, exit_gen_test);
  14859. }
  14860. wc_FreeDhKey(key);
  14861. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  14862. if (ret != 0)
  14863. ERROR_OUT(-7992, exit_gen_test);
  14864. ret = wc_DhSetKey(key, p, sizeof(p), g, sizeof(g));
  14865. if (ret != 0) {
  14866. ERROR_OUT(-7993, exit_gen_test);
  14867. }
  14868. ret = wc_DhCheckPubKey_ex(key, pub, pubSz, q, sizeof(q));
  14869. if (ret != 0) {
  14870. ERROR_OUT(-7994, exit_gen_test);
  14871. }
  14872. #ifndef HAVE_SELFTEST
  14873. ret = wc_DhCheckKeyPair(key, pub, pubSz, priv, privSz);
  14874. if (ret != 0) {
  14875. ERROR_OUT(-7995, exit_gen_test);
  14876. }
  14877. /* Taint the public key so the check fails. */
  14878. pub[0]++;
  14879. ret = wc_DhCheckKeyPair(key, pub, pubSz, priv, privSz);
  14880. if (ret != MP_CMP_E) {
  14881. ERROR_OUT(-7996, exit_gen_test);
  14882. }
  14883. #ifdef WOLFSSL_KEY_GEN
  14884. wc_FreeDhKey(key);
  14885. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  14886. if (ret != 0)
  14887. ERROR_OUT(-7997, exit_gen_test);
  14888. ret = wc_DhGenerateParams(rng, 2048, key);
  14889. if (ret != 0) {
  14890. ERROR_OUT(-7998, exit_gen_test);
  14891. }
  14892. privSz = sizeof(priv);
  14893. pubSz = sizeof(pub);
  14894. ret = wc_DhGenerateKeyPair(key, rng, priv, &privSz, pub, &pubSz);
  14895. #if defined(WOLFSSL_ASYNC_CRYPT)
  14896. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  14897. #endif
  14898. if (ret != 0) {
  14899. ERROR_OUT(-7999, exit_gen_test);
  14900. }
  14901. #endif /* WOLFSSL_KEY_GEN */
  14902. #endif /* HAVE_SELFTEST */
  14903. ret = 0;
  14904. exit_gen_test:
  14905. #ifdef WOLFSSL_SMALL_STACK
  14906. if (key) {
  14907. wc_FreeDhKey(key);
  14908. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14909. }
  14910. #else
  14911. wc_FreeDhKey(key);
  14912. #endif
  14913. return ret;
  14914. }
  14915. static int dh_generate_test(WC_RNG *rng)
  14916. {
  14917. int ret = 0;
  14918. #ifdef WOLFSSL_SMALL_STACK
  14919. DhKey *smallKey = NULL;
  14920. #else
  14921. DhKey smallKey[1];
  14922. #endif
  14923. byte p[2] = { 1, 7 }; /* 263 in decimal */
  14924. byte g[2] = { 0, 2 };
  14925. #if !defined(WOLFSSL_SP_MATH) && !defined(HAVE_FFDHE)
  14926. #ifdef WOLFSSL_DH_CONST
  14927. /* the table for constant DH lookup will round to the lowest byte size 21 */
  14928. byte priv[21];
  14929. byte pub[21];
  14930. #else
  14931. byte priv[2];
  14932. byte pub[2];
  14933. #endif
  14934. word32 privSz = sizeof(priv);
  14935. word32 pubSz = sizeof(pub);
  14936. #endif
  14937. int smallKey_inited = 0;
  14938. #ifdef WOLFSSL_SMALL_STACK
  14939. if ((smallKey = (DhKey *)XMALLOC(sizeof(*smallKey), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER)) == NULL)
  14940. return -8019;
  14941. #endif
  14942. ret = wc_InitDhKey_ex(smallKey, HEAP_HINT, devId);
  14943. if (ret != 0)
  14944. ERROR_OUT(-8010, exit_gen_test);
  14945. smallKey_inited = 1;
  14946. /* Parameter Validation testing. */
  14947. ret = wc_InitDhKey_ex(NULL, HEAP_HINT, devId);
  14948. if (ret != BAD_FUNC_ARG)
  14949. return -8011;
  14950. wc_FreeDhKey(NULL);
  14951. ret = wc_DhSetKey(NULL, p, sizeof(p), g, sizeof(g));
  14952. if (ret != BAD_FUNC_ARG) {
  14953. ERROR_OUT(-8012, exit_gen_test);
  14954. }
  14955. ret = wc_DhSetKey(smallKey, NULL, sizeof(p), g, sizeof(g));
  14956. if (ret != BAD_FUNC_ARG) {
  14957. ERROR_OUT(-8013, exit_gen_test);
  14958. }
  14959. ret = wc_DhSetKey(smallKey, p, 0, g, sizeof(g));
  14960. if (ret != BAD_FUNC_ARG) {
  14961. ERROR_OUT(-8014, exit_gen_test);
  14962. }
  14963. ret = wc_DhSetKey(smallKey, p, sizeof(p), NULL, sizeof(g));
  14964. if (ret != BAD_FUNC_ARG) {
  14965. ERROR_OUT(-8015, exit_gen_test);
  14966. }
  14967. ret = wc_DhSetKey(smallKey, p, sizeof(p), g, 0);
  14968. if (ret != BAD_FUNC_ARG) {
  14969. ERROR_OUT(-8016, exit_gen_test);
  14970. }
  14971. ret = wc_DhSetKey(smallKey, p, sizeof(p), g, sizeof(g));
  14972. if (ret != 0) {
  14973. ERROR_OUT(-8017, exit_gen_test);
  14974. }
  14975. #if !defined(WOLFSSL_SP_MATH) && !defined(HAVE_FFDHE)
  14976. /* Use API. */
  14977. ret = wc_DhGenerateKeyPair(smallKey, rng, priv, &privSz, pub, &pubSz);
  14978. #if defined(WOLFSSL_ASYNC_CRYPT)
  14979. ret = wc_AsyncWait(ret, &smallKey->asyncDev, WC_ASYNC_FLAG_NONE);
  14980. #endif
  14981. if (ret != 0) {
  14982. ret = -8018;
  14983. }
  14984. #else
  14985. (void)rng;
  14986. #if defined(HAVE_FIPS) || !defined(WOLFSSL_NO_DH186)
  14987. ret = 0;
  14988. #endif
  14989. #endif
  14990. #if !defined(HAVE_FIPS) && defined(WOLFSSL_NO_DH186)
  14991. {
  14992. byte priv[260];
  14993. byte pub[260];
  14994. word32 privSz = sizeof(priv);
  14995. word32 pubSz = sizeof(pub);
  14996. /* test odd ball param generation with DH */
  14997. wc_FreeDhKey(smallKey);
  14998. ret = wc_InitDhKey_ex(smallKey, HEAP_HINT, devId);
  14999. if (ret != 0)
  15000. ERROR_OUT(-8019, exit_gen_test);
  15001. ret = wc_DhGenerateParams(rng, 2056, smallKey);
  15002. if (ret != 0) {
  15003. ERROR_OUT(-8020, exit_gen_test);
  15004. }
  15005. privSz = sizeof(priv);
  15006. pubSz = sizeof(pub);
  15007. ret = wc_DhGenerateKeyPair(smallKey, rng, priv, &privSz, pub, &pubSz);
  15008. #if defined(WOLFSSL_ASYNC_CRYPT)
  15009. ret = wc_AsyncWait(ret, &smallKey->asyncDev, WC_ASYNC_FLAG_NONE);
  15010. #endif
  15011. if (ret != 0) {
  15012. ERROR_OUT(-8021, exit_gen_test);
  15013. }
  15014. }
  15015. #endif /* !HAVE_FIPS and WOLFSSL_NO_DH186 */
  15016. exit_gen_test:
  15017. if (smallKey_inited)
  15018. wc_FreeDhKey(smallKey);
  15019. #ifdef WOLFSSL_SMALL_STACK
  15020. if (smallKey != NULL)
  15021. XFREE(smallKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15022. #endif
  15023. return ret;
  15024. }
  15025. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  15026. typedef struct dh_pubvalue_test {
  15027. const byte* data;
  15028. word32 len;
  15029. } dh_pubvalue_test;
  15030. static int dh_test_check_pubvalue(void)
  15031. {
  15032. int ret;
  15033. word32 i;
  15034. WOLFSSL_SMALL_STACK_STATIC const byte prime[] = {0x01, 0x00, 0x01};
  15035. WOLFSSL_SMALL_STACK_STATIC const byte pubValZero[] = { 0x00 };
  15036. WOLFSSL_SMALL_STACK_STATIC const byte pubValZeroLong[] = { 0x00, 0x00, 0x00 };
  15037. WOLFSSL_SMALL_STACK_STATIC const byte pubValOne[] = { 0x01 };
  15038. WOLFSSL_SMALL_STACK_STATIC const byte pubValOneLong[] = { 0x00, 0x00, 0x01 };
  15039. WOLFSSL_SMALL_STACK_STATIC const byte pubValPrimeMinusOne[] = { 0x01, 0x00, 0x00 };
  15040. WOLFSSL_SMALL_STACK_STATIC const byte pubValPrimeLong[] = {0x00, 0x01, 0x00, 0x01};
  15041. WOLFSSL_SMALL_STACK_STATIC const byte pubValPrimePlusOne[] = { 0x01, 0x00, 0x02 };
  15042. WOLFSSL_SMALL_STACK_STATIC const byte pubValTooBig0[] = { 0x02, 0x00, 0x01 };
  15043. WOLFSSL_SMALL_STACK_STATIC const byte pubValTooBig1[] = { 0x01, 0x01, 0x01 };
  15044. WOLFSSL_SMALL_STACK_STATIC const byte pubValTooLong[] = { 0x01, 0x00, 0x00, 0x01 };
  15045. const dh_pubvalue_test dh_pubval_fail[] = {
  15046. { prime, sizeof(prime) },
  15047. { pubValZero, sizeof(pubValZero) },
  15048. { pubValZeroLong, sizeof(pubValZeroLong) },
  15049. { pubValOne, sizeof(pubValOne) },
  15050. { pubValOneLong, sizeof(pubValOneLong) },
  15051. { pubValPrimeMinusOne, sizeof(pubValPrimeMinusOne) },
  15052. { pubValPrimeLong, sizeof(pubValPrimeLong) },
  15053. { pubValPrimePlusOne, sizeof(pubValPrimePlusOne) },
  15054. { pubValTooBig0, sizeof(pubValTooBig0) },
  15055. { pubValTooBig1, sizeof(pubValTooBig1) },
  15056. { pubValTooLong, sizeof(pubValTooLong) },
  15057. };
  15058. WOLFSSL_SMALL_STACK_STATIC const byte pubValTwo[] = { 0x02 };
  15059. WOLFSSL_SMALL_STACK_STATIC const byte pubValTwoLong[] = { 0x00, 0x00, 0x02 };
  15060. WOLFSSL_SMALL_STACK_STATIC const byte pubValGood[] = { 0x12, 0x34 };
  15061. WOLFSSL_SMALL_STACK_STATIC const byte pubValGoodLen[] = { 0x00, 0x12, 0x34 };
  15062. WOLFSSL_SMALL_STACK_STATIC const byte pubValGoodLong[] = { 0x00, 0x00, 0x12, 0x34 };
  15063. const dh_pubvalue_test dh_pubval_pass[] = {
  15064. { pubValTwo, sizeof(pubValTwo) },
  15065. { pubValTwoLong, sizeof(pubValTwoLong) },
  15066. { pubValGood, sizeof(pubValGood) },
  15067. { pubValGoodLen, sizeof(pubValGoodLen) },
  15068. { pubValGoodLong, sizeof(pubValGoodLong) },
  15069. };
  15070. for (i = 0; i < sizeof(dh_pubval_fail) / sizeof(*dh_pubval_fail); i++) {
  15071. ret = wc_DhCheckPubValue(prime, sizeof(prime), dh_pubval_fail[i].data,
  15072. dh_pubval_fail[i].len);
  15073. if (ret != MP_VAL)
  15074. return -8020 - (int)i;
  15075. }
  15076. for (i = 0; i < sizeof(dh_pubval_pass) / sizeof(*dh_pubval_pass); i++) {
  15077. ret = wc_DhCheckPubValue(prime, sizeof(prime), dh_pubval_pass[i].data,
  15078. dh_pubval_pass[i].len);
  15079. if (ret != 0)
  15080. return -8030 - (int)i;
  15081. }
  15082. return 0;
  15083. }
  15084. #endif
  15085. #if defined(HAVE_FFDHE)
  15086. #if defined(HAVE_FFDHE_4096)
  15087. #define MAX_DH_PRIV_SZ 39
  15088. #define MAX_DH_KEY_SZ 512
  15089. #elif defined(HAVE_FFDHE_3072)
  15090. #define MAX_DH_PRIV_SZ 34
  15091. #define MAX_DH_KEY_SZ 384
  15092. #else
  15093. #define MAX_DH_PRIV_SZ 29
  15094. #define MAX_DH_KEY_SZ 256
  15095. #endif
  15096. #ifndef WC_NO_RNG
  15097. #if !(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION == 2) && \
  15098. (defined(WOLFSSL_SP_ARM64_ASM) || defined(WOLFSSL_SP_ARM32_ASM)))
  15099. #ifdef HAVE_PUBLIC_FFDHE
  15100. static int dh_ffdhe_test(WC_RNG *rng, const DhParams* params)
  15101. #else
  15102. static int dh_ffdhe_test(WC_RNG *rng, int name)
  15103. #endif
  15104. {
  15105. int ret;
  15106. word32 privSz, pubSz, privSz2, pubSz2;
  15107. #ifdef WOLFSSL_SMALL_STACK
  15108. byte *priv = (byte*)XMALLOC(MAX_DH_PRIV_SZ, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15109. byte *pub = (byte*)XMALLOC(MAX_DH_KEY_SZ, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15110. byte *priv2 = (byte*)XMALLOC(MAX_DH_PRIV_SZ, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15111. byte *pub2 = (byte*)XMALLOC(MAX_DH_KEY_SZ, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15112. byte *agree = (byte*)XMALLOC(MAX_DH_KEY_SZ, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15113. byte *agree2 = (byte*)XMALLOC(MAX_DH_KEY_SZ, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15114. DhKey *key = (DhKey*)XMALLOC(sizeof(*key), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15115. DhKey *key2 = (DhKey*)XMALLOC(sizeof(*key2), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15116. #else
  15117. byte priv[MAX_DH_PRIV_SZ];
  15118. byte pub[MAX_DH_KEY_SZ];
  15119. byte priv2[MAX_DH_PRIV_SZ];
  15120. byte pub2[MAX_DH_KEY_SZ];
  15121. byte agree[MAX_DH_KEY_SZ];
  15122. byte agree2[MAX_DH_KEY_SZ];
  15123. DhKey key[1];
  15124. DhKey key2[1];
  15125. #endif
  15126. word32 agreeSz = MAX_DH_KEY_SZ;
  15127. word32 agreeSz2 = MAX_DH_KEY_SZ;
  15128. #ifdef WOLFSSL_SMALL_STACK
  15129. if ((priv == NULL) ||
  15130. (pub == NULL) ||
  15131. (priv2 == NULL) ||
  15132. (pub2 == NULL) ||
  15133. (agree == NULL) ||
  15134. (agree2 == NULL) ||
  15135. (key == NULL) ||
  15136. (key2 == NULL))
  15137. ERROR_OUT(-8050, done);
  15138. #endif
  15139. pubSz = MAX_DH_KEY_SZ;
  15140. pubSz2 = MAX_DH_KEY_SZ;
  15141. #ifdef HAVE_PUBLIC_FFDHE
  15142. privSz = MAX_DH_PRIV_SZ;
  15143. privSz2 = MAX_DH_PRIV_SZ;
  15144. #else
  15145. privSz = wc_DhGetNamedKeyMinSize(name);
  15146. privSz2 = privSz;
  15147. #endif
  15148. XMEMSET(key, 0, sizeof(*key));
  15149. XMEMSET(key2, 0, sizeof(*key2));
  15150. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  15151. if (ret != 0) {
  15152. ERROR_OUT(-8051, done);
  15153. }
  15154. ret = wc_InitDhKey_ex(key2, HEAP_HINT, devId);
  15155. if (ret != 0) {
  15156. ERROR_OUT(-8052, done);
  15157. }
  15158. #ifdef HAVE_PUBLIC_FFDHE
  15159. ret = wc_DhSetKey(key, params->p, params->p_len, params->g, params->g_len);
  15160. #else
  15161. ret = wc_DhSetNamedKey(key, name);
  15162. #endif
  15163. if (ret != 0) {
  15164. ERROR_OUT(-8053, done);
  15165. }
  15166. #ifdef HAVE_PUBLIC_FFDHE
  15167. ret = wc_DhSetKey(key2, params->p, params->p_len, params->g,
  15168. params->g_len);
  15169. #else
  15170. ret = wc_DhSetNamedKey(key2, name);
  15171. #endif
  15172. if (ret != 0) {
  15173. ERROR_OUT(-8054, done);
  15174. }
  15175. ret = wc_DhGenerateKeyPair(key, rng, priv, &privSz, pub, &pubSz);
  15176. #if defined(WOLFSSL_ASYNC_CRYPT)
  15177. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  15178. #endif
  15179. if (ret != 0) {
  15180. ERROR_OUT(-8055, done);
  15181. }
  15182. ret = wc_DhGenerateKeyPair(key2, rng, priv2, &privSz2, pub2, &pubSz2);
  15183. #if defined(WOLFSSL_ASYNC_CRYPT)
  15184. ret = wc_AsyncWait(ret, &key2->asyncDev, WC_ASYNC_FLAG_NONE);
  15185. #endif
  15186. if (ret != 0) {
  15187. ERROR_OUT(-8056, done);
  15188. }
  15189. ret = wc_DhAgree(key, agree, &agreeSz, priv, privSz, pub2, pubSz2);
  15190. #if defined(WOLFSSL_ASYNC_CRYPT)
  15191. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  15192. #endif
  15193. if (ret != 0) {
  15194. ERROR_OUT(-8057, done);
  15195. }
  15196. ret = wc_DhAgree(key2, agree2, &agreeSz2, priv2, privSz2, pub, pubSz);
  15197. #if defined(WOLFSSL_ASYNC_CRYPT)
  15198. ret = wc_AsyncWait(ret, &key2->asyncDev, WC_ASYNC_FLAG_NONE);
  15199. #endif
  15200. if (ret != 0) {
  15201. ERROR_OUT(-8058, done);
  15202. }
  15203. if (agreeSz != agreeSz2 || XMEMCMP(agree, agree2, agreeSz)) {
  15204. ERROR_OUT(-8059, done);
  15205. }
  15206. #if defined(WOLFSSL_HAVE_SP_DH) || defined(USE_FAST_MATH)
  15207. /* Make p even */
  15208. key->p.dp[0] &= (mp_digit)-2;
  15209. if (ret != 0) {
  15210. ERROR_OUT(-8058, done);
  15211. }
  15212. ret = wc_DhGenerateKeyPair(key, rng, priv, &privSz, pub, &pubSz);
  15213. #if defined(WOLFSSL_ASYNC_CRYPT)
  15214. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  15215. #endif
  15216. if (ret != MP_VAL && ret != MP_EXPTMOD_E) {
  15217. ERROR_OUT(-8058, done);
  15218. }
  15219. ret = wc_DhAgree(key, agree, &agreeSz, priv, privSz, pub2, pubSz2);
  15220. #if defined(WOLFSSL_ASYNC_CRYPT)
  15221. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  15222. #endif
  15223. if (ret != MP_VAL && ret != MP_EXPTMOD_E && ret != ASYNC_OP_E) {
  15224. ERROR_OUT(-8057, done);
  15225. }
  15226. ret = wc_DhCheckKeyPair(key, pub, pubSz, priv, privSz);
  15227. if (ret != MP_VAL && ret != MP_EXPTMOD_E && ret != MP_CMP_E &&
  15228. ret != ASYNC_OP_E) {
  15229. ERROR_OUT(-8057, done);
  15230. }
  15231. /* Getting here means success - set ret to 0. */
  15232. ret = 0;
  15233. #endif
  15234. done:
  15235. #if defined(WOLFSSL_SMALL_STACK) && !defined(WC_NO_RNG)
  15236. if (priv)
  15237. XFREE(priv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15238. if (pub)
  15239. XFREE(pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15240. if (priv2)
  15241. XFREE(priv2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15242. if (pub2)
  15243. XFREE(pub2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15244. if (agree)
  15245. XFREE(agree, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15246. if (agree2)
  15247. XFREE(agree2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15248. if (key) {
  15249. wc_FreeDhKey(key);
  15250. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15251. }
  15252. if (key2) {
  15253. wc_FreeDhKey(key2);
  15254. XFREE(key2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15255. }
  15256. #else
  15257. wc_FreeDhKey(key);
  15258. wc_FreeDhKey(key2);
  15259. #endif
  15260. return ret;
  15261. }
  15262. #endif /* !(HAVE_FIPS_VERSION == 2 && WOLFSSL_SP_ARMxx_ASM) */
  15263. #endif /* !WC_NO_RNG */
  15264. #endif /* HAVE_FFDHE */
  15265. WOLFSSL_TEST_SUBROUTINE int dh_test(void)
  15266. {
  15267. int ret;
  15268. word32 bytes;
  15269. word32 idx = 0, privSz, pubSz, privSz2, pubSz2;
  15270. #ifndef WC_NO_RNG
  15271. WC_RNG rng;
  15272. #endif
  15273. int keyInit = 0;
  15274. #define DH_TEST_TMP_SIZE 1024
  15275. #if !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096)
  15276. #define DH_TEST_BUF_SIZE 256
  15277. #else
  15278. #define DH_TEST_BUF_SIZE 512
  15279. #endif
  15280. #ifndef WC_NO_RNG
  15281. word32 agreeSz = DH_TEST_BUF_SIZE;
  15282. word32 agreeSz2 = DH_TEST_BUF_SIZE;
  15283. #endif
  15284. #ifdef WOLFSSL_SMALL_STACK
  15285. DhKey *key = (DhKey *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15286. DhKey *key2 = (DhKey *)XMALLOC(sizeof *key2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15287. byte *tmp = (byte *)XMALLOC(DH_TEST_TMP_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15288. #else
  15289. DhKey key[1];
  15290. DhKey key2[1];
  15291. byte tmp[DH_TEST_TMP_SIZE];
  15292. #endif
  15293. #ifndef WC_NO_RNG
  15294. #ifdef WOLFSSL_SMALL_STACK
  15295. byte *priv = (byte *)XMALLOC(DH_TEST_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15296. byte *pub = (byte *)XMALLOC(DH_TEST_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15297. byte *priv2 = (byte *)XMALLOC(DH_TEST_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15298. byte *pub2 = (byte *)XMALLOC(DH_TEST_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15299. byte *agree = (byte *)XMALLOC(DH_TEST_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15300. byte *agree2 = (byte *)XMALLOC(DH_TEST_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15301. if (priv == NULL || pub == NULL || priv2 == NULL || pub2 == NULL ||
  15302. agree == NULL || agree2 == NULL) {
  15303. ERROR_OUT(-8100, done);
  15304. }
  15305. #else
  15306. byte priv[DH_TEST_BUF_SIZE];
  15307. byte pub[DH_TEST_BUF_SIZE];
  15308. byte priv2[DH_TEST_BUF_SIZE];
  15309. byte pub2[DH_TEST_BUF_SIZE];
  15310. byte agree[DH_TEST_BUF_SIZE];
  15311. byte agree2[DH_TEST_BUF_SIZE];
  15312. #endif
  15313. #endif /* !WC_NO_RNG */
  15314. #ifdef WOLFSSL_SMALL_STACK
  15315. if (key == NULL || key2 == NULL || tmp == NULL) {
  15316. ERROR_OUT(-8100, done);
  15317. }
  15318. #endif
  15319. #ifdef USE_CERT_BUFFERS_1024
  15320. XMEMCPY(tmp, dh_key_der_1024, (size_t)sizeof_dh_key_der_1024);
  15321. bytes = (size_t)sizeof_dh_key_der_1024;
  15322. #elif defined(USE_CERT_BUFFERS_2048)
  15323. XMEMCPY(tmp, dh_key_der_2048, (size_t)sizeof_dh_key_der_2048);
  15324. bytes = (size_t)sizeof_dh_key_der_2048;
  15325. #elif defined(USE_CERT_BUFFERS_3072)
  15326. XMEMCPY(tmp, dh_key_der_3072, (size_t)sizeof_dh_key_der_3072);
  15327. bytes = (size_t)sizeof_dh_key_der_3072;
  15328. #elif defined(USE_CERT_BUFFERS_4096)
  15329. XMEMCPY(tmp, dh_key_der_4096, (size_t)sizeof_dh_key_der_4096);
  15330. bytes = (size_t)sizeof_dh_key_der_4096;
  15331. #elif defined(NO_ASN)
  15332. /* don't use file, no DER parsing */
  15333. #elif !defined(NO_FILESYSTEM)
  15334. {
  15335. XFILE file = XFOPEN(dhParamsFile, "rb");
  15336. if (! file)
  15337. ERROR_OUT(-8101, done);
  15338. bytes = (word32) XFREAD(tmp, 1, DH_TEST_TMP_SIZE, file);
  15339. XFCLOSE(file);
  15340. }
  15341. #else
  15342. /* No DH key to use. */
  15343. ERROR_OUT(-8102, done);
  15344. #endif /* USE_CERT_BUFFERS */
  15345. (void)idx;
  15346. (void)tmp;
  15347. (void)bytes;
  15348. pubSz = DH_TEST_BUF_SIZE;
  15349. pubSz2 = DH_TEST_BUF_SIZE;
  15350. privSz = DH_TEST_BUF_SIZE;
  15351. privSz2 = DH_TEST_BUF_SIZE;
  15352. #ifndef WC_NO_RNG
  15353. XMEMSET(&rng, 0, sizeof(rng));
  15354. #endif
  15355. /* Use API for coverage. */
  15356. ret = wc_InitDhKey(key);
  15357. if (ret != 0) {
  15358. ERROR_OUT(-8103, done);
  15359. }
  15360. wc_FreeDhKey(key);
  15361. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  15362. if (ret != 0) {
  15363. ERROR_OUT(-8104, done);
  15364. }
  15365. keyInit = 1;
  15366. ret = wc_InitDhKey_ex(key2, HEAP_HINT, devId);
  15367. if (ret != 0) {
  15368. ERROR_OUT(-8105, done);
  15369. }
  15370. #ifdef NO_ASN
  15371. #ifndef WOLFSSL_SP_MATH
  15372. ret = wc_DhSetKey(key, dh_p, sizeof(dh_p), dh_g, sizeof(dh_g));
  15373. if (ret != 0) {
  15374. ERROR_OUT(-8106, done);
  15375. }
  15376. ret = wc_DhSetKey(key2, dh_p, sizeof(dh_p), dh_g, sizeof(dh_g));
  15377. if (ret != 0) {
  15378. ERROR_OUT(-8107, done);
  15379. }
  15380. #else
  15381. ret = wc_DhSetKey(key, dh2048_p, sizeof(dh2048_p), dh2048_g,
  15382. sizeof(dh2048_g));
  15383. if (ret != 0) {
  15384. ERROR_OUT(-8106, done);
  15385. }
  15386. ret = wc_DhSetKey(key2, dh2048_p, sizeof(dh2048_p), dh2048_g,
  15387. sizeof(dh2048_g));
  15388. if (ret != 0) {
  15389. ERROR_OUT(-8107, done);
  15390. }
  15391. #endif
  15392. #else
  15393. ret = wc_DhKeyDecode(tmp, &idx, key, bytes);
  15394. if (ret != 0) {
  15395. ERROR_OUT(-8108, done);
  15396. }
  15397. idx = 0;
  15398. ret = wc_DhKeyDecode(tmp, &idx, key2, bytes);
  15399. if (ret != 0) {
  15400. ERROR_OUT(-8109, done);
  15401. }
  15402. #endif
  15403. #ifndef WC_NO_RNG
  15404. #ifndef HAVE_FIPS
  15405. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  15406. #else
  15407. ret = wc_InitRng(&rng);
  15408. #endif
  15409. if (ret != 0) {
  15410. ERROR_OUT(-8110, done);
  15411. }
  15412. ret = wc_DhGenerateKeyPair(key, &rng, priv, &privSz, pub, &pubSz);
  15413. #if defined(WOLFSSL_ASYNC_CRYPT)
  15414. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  15415. #endif
  15416. if (ret != 0) {
  15417. ERROR_OUT(-8111, done);
  15418. }
  15419. ret = wc_DhGenerateKeyPair(key2, &rng, priv2, &privSz2, pub2, &pubSz2);
  15420. #if defined(WOLFSSL_ASYNC_CRYPT)
  15421. ret = wc_AsyncWait(ret, &key2->asyncDev, WC_ASYNC_FLAG_NONE);
  15422. #endif
  15423. if (ret != 0) {
  15424. ERROR_OUT(-8112, done);
  15425. }
  15426. ret = wc_DhAgree(key, agree, &agreeSz, priv, privSz, pub2, pubSz2);
  15427. #if defined(WOLFSSL_ASYNC_CRYPT)
  15428. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  15429. #endif
  15430. if (ret != 0) {
  15431. ERROR_OUT(-8113, done);
  15432. }
  15433. ret = wc_DhAgree(key2, agree2, &agreeSz2, priv2, privSz2, pub, pubSz);
  15434. #if defined(WOLFSSL_ASYNC_CRYPT)
  15435. ret = wc_AsyncWait(ret, &key2->asyncDev, WC_ASYNC_FLAG_NONE);
  15436. #endif
  15437. if (ret != 0) {
  15438. ERROR_OUT(-8114, done);
  15439. }
  15440. if (agreeSz != agreeSz2 || XMEMCMP(agree, agree2, agreeSz)) {
  15441. ERROR_OUT(-8115, done);
  15442. }
  15443. #endif /* !WC_NO_RNG */
  15444. #if defined(WOLFSSL_KEY_GEN) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  15445. if (wc_DhCheckPrivKey(NULL, NULL, 0) != BAD_FUNC_ARG)
  15446. ERROR_OUT(-8116, done);
  15447. if (wc_DhCheckPrivKey(key, priv, privSz) != 0)
  15448. ERROR_OUT(-8117, done);
  15449. if (wc_DhExportParamsRaw(NULL, NULL, NULL, NULL, NULL, NULL, NULL) != BAD_FUNC_ARG)
  15450. ERROR_OUT(-8118, done);
  15451. {
  15452. word32 pSz, qSz, gSz;
  15453. if (wc_DhExportParamsRaw(key, NULL, &pSz, NULL, &qSz, NULL, &gSz) != LENGTH_ONLY_E)
  15454. ERROR_OUT(-8119, done);
  15455. }
  15456. #endif
  15457. /* Test DH key import / export */
  15458. #if defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM) && \
  15459. (!defined(HAVE_FIPS) || \
  15460. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  15461. wc_FreeDhKey(key);
  15462. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  15463. if (ret != 0) {
  15464. ERROR_OUT(-8120, done);
  15465. }
  15466. #ifndef NO_ASN
  15467. {
  15468. /* DH Private - Key Export / Import */
  15469. #ifdef WOLFSSL_SMALL_STACK
  15470. byte *tmp2;
  15471. #else
  15472. byte tmp2[DH_TEST_TMP_SIZE];
  15473. #endif
  15474. XFILE file = XFOPEN(dhKeyFile, "rb");
  15475. if (!file)
  15476. ERROR_OUT(-8130, done);
  15477. bytes = (word32)XFREAD(tmp, 1, DH_TEST_TMP_SIZE, file);
  15478. XFCLOSE(file);
  15479. #ifdef WOLFSSL_SMALL_STACK
  15480. tmp2 = (byte*)XMALLOC(DH_TEST_TMP_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15481. if (tmp2 == NULL)
  15482. ERROR_OUT(-8131, done);
  15483. #endif
  15484. idx = 0;
  15485. XMEMSET(tmp2, 0, DH_TEST_TMP_SIZE);
  15486. /* Import DH Private key as DER */
  15487. ret = wc_DhKeyDecode(tmp, &idx, key, bytes);
  15488. if (ret == 0) {
  15489. /* Export as DER */
  15490. idx = DH_TEST_TMP_SIZE;
  15491. ret = wc_DhPrivKeyToDer(key, tmp2, &idx);
  15492. }
  15493. /* Verify export matches original */
  15494. if (ret <= 0 || bytes != idx || XMEMCMP(tmp, tmp2, bytes) != 0) {
  15495. ERROR_OUT(-8132, done);
  15496. }
  15497. /* DH Public Key - Export / Import */
  15498. file = XFOPEN(dhKeyPubFile, "rb");
  15499. if (!file)
  15500. ERROR_OUT(-8133, done);
  15501. bytes = (word32)XFREAD(tmp, 1, DH_TEST_TMP_SIZE, file);
  15502. XFCLOSE(file);
  15503. /* for HAVE_WOLF_BIGINT prevent leak */
  15504. wc_FreeDhKey(key);
  15505. (void)wc_InitDhKey_ex(key, HEAP_HINT, devId);
  15506. idx = 0;
  15507. XMEMSET(tmp2, 0, DH_TEST_TMP_SIZE);
  15508. /* Import DH Public key as DER */
  15509. ret = wc_DhKeyDecode(tmp, &idx, key, bytes);
  15510. if (ret == 0) {
  15511. /* Export as DER */
  15512. idx = DH_TEST_TMP_SIZE;
  15513. ret = wc_DhPubKeyToDer(key, tmp2, &idx);
  15514. }
  15515. /* Verify export matches original */
  15516. if (ret <= 0 || bytes != idx || XMEMCMP(tmp, tmp2, bytes) != 0) {
  15517. ERROR_OUT(-8134, done);
  15518. }
  15519. #ifdef WOLFSSL_SMALL_STACK
  15520. XFREE(tmp2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15521. #endif
  15522. }
  15523. #else
  15524. ret = wc_DhSetKey(key, dh_p, sizeof(dh_p), dh_g, sizeof(dh_g));
  15525. if (ret != 0) {
  15526. ERROR_OUT(-8121, done);
  15527. }
  15528. #endif /* !NO_ASN */
  15529. privSz = DH_TEST_BUF_SIZE;
  15530. pubSz = DH_TEST_BUF_SIZE;
  15531. ret = wc_DhExportKeyPair(key, priv, &privSz, pub, &pubSz);
  15532. if (ret != 0) {
  15533. ERROR_OUT(-8122, done);
  15534. }
  15535. ret = wc_DhImportKeyPair(key2, priv, privSz, pub, pubSz);
  15536. if (ret != 0) {
  15537. ERROR_OUT(-8125, done);
  15538. }
  15539. #endif /* WOLFSSL_DH_EXTRA && !NO_FILESYSTEM && !FIPS <= 2 */
  15540. #ifndef WC_NO_RNG
  15541. ret = dh_generate_test(&rng);
  15542. if (ret != 0)
  15543. ERROR_OUT(-8123, done);
  15544. ret = dh_fips_generate_test(&rng);
  15545. if (ret != 0)
  15546. ERROR_OUT(-8124, done);
  15547. #endif /* !WC_NO_RNG */
  15548. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  15549. ret = dh_test_check_pubvalue();
  15550. if (ret != 0)
  15551. ERROR_OUT(-8125, done);
  15552. #endif
  15553. #if !(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION == 2) && \
  15554. (defined(WOLFSSL_SP_ARM64_ASM) || defined(WOLFSSL_SP_ARM32_ASM)))
  15555. /* RNG with DH and SP_ASM code not supported in the in-lined FIPS ASM code,
  15556. * this will be available for testing in the 140-3 module */
  15557. #ifndef WC_NO_RNG
  15558. /* Specialized code for key gen when using FFDHE-2048, FFDHE-3072 and FFDHE-4096 */
  15559. #ifdef HAVE_FFDHE_2048
  15560. #ifdef HAVE_PUBLIC_FFDHE
  15561. ret = dh_ffdhe_test(&rng, wc_Dh_ffdhe2048_Get());
  15562. #else
  15563. ret = dh_ffdhe_test(&rng, WC_FFDHE_2048);
  15564. #endif
  15565. if (ret != 0)
  15566. ERROR_OUT(-8126, done);
  15567. #endif
  15568. #ifdef HAVE_FFDHE_3072
  15569. #ifdef HAVE_PUBLIC_FFDHE
  15570. ret = dh_ffdhe_test(&rng, wc_Dh_ffdhe3072_Get());
  15571. #else
  15572. ret = dh_ffdhe_test(&rng, WC_FFDHE_3072);
  15573. #endif
  15574. if (ret != 0)
  15575. ERROR_OUT(-8127, done);
  15576. #endif
  15577. #ifdef HAVE_FFDHE_4096
  15578. #ifdef HAVE_PUBLIC_FFDHE
  15579. ret = dh_ffdhe_test(&rng, wc_Dh_ffdhe4096_Get());
  15580. #else
  15581. ret = dh_ffdhe_test(&rng, WC_FFDHE_4096);
  15582. #endif
  15583. if (ret != 0)
  15584. ERROR_OUT(-8128, done);
  15585. #endif
  15586. #endif /* !WC_NO_RNG */
  15587. #endif /* HAVE_FIPS_VERSION == 2 && !WOLFSSL_SP_ARM64_ASM */
  15588. wc_FreeDhKey(key);
  15589. keyInit = 0;
  15590. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  15591. !defined(WOLFSSL_OLD_PRIME_CHECK) && !defined(WC_NO_RNG)
  15592. /* Test Check Key */
  15593. ret = wc_DhSetCheckKey(key, dh_p, sizeof(dh_p), dh_g, sizeof(dh_g),
  15594. NULL, 0, 0, &rng);
  15595. if (ret != 0)
  15596. ERROR_OUT(-8129, done);
  15597. keyInit = 1; /* DhSetCheckKey also initializes the key, free it */
  15598. #endif
  15599. done:
  15600. #ifndef WC_NO_RNG
  15601. wc_FreeRng(&rng);
  15602. #endif
  15603. #ifdef WOLFSSL_SMALL_STACK
  15604. if (key) {
  15605. if (keyInit)
  15606. wc_FreeDhKey(key);
  15607. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15608. }
  15609. if (key2) {
  15610. wc_FreeDhKey(key2);
  15611. XFREE(key2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15612. }
  15613. if (tmp)
  15614. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15615. if (priv)
  15616. XFREE(priv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15617. if (pub)
  15618. XFREE(pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15619. if (priv2)
  15620. XFREE(priv2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15621. if (pub2)
  15622. XFREE(pub2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15623. if (agree)
  15624. XFREE(agree, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15625. if (agree2)
  15626. XFREE(agree2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15627. #else
  15628. if (keyInit)
  15629. wc_FreeDhKey(key);
  15630. wc_FreeDhKey(key2);
  15631. #endif
  15632. (void)privSz;
  15633. (void)pubSz;
  15634. (void)pubSz2;
  15635. (void)privSz2;
  15636. return ret;
  15637. #undef DH_TEST_BUF_SIZE
  15638. #undef DH_TEST_TMP_SIZE
  15639. }
  15640. #endif /* NO_DH */
  15641. #ifndef NO_DSA
  15642. WOLFSSL_TEST_SUBROUTINE int dsa_test(void)
  15643. {
  15644. int ret = 0, answer;
  15645. word32 bytes;
  15646. word32 idx = 0;
  15647. WC_RNG rng;
  15648. wc_Sha sha;
  15649. byte hash[WC_SHA_DIGEST_SIZE];
  15650. byte signature[40];
  15651. #ifdef WOLFSSL_KEY_GEN
  15652. byte* der = 0;
  15653. #endif
  15654. #define DSA_TEST_TMP_SIZE 1024
  15655. #ifdef WOLFSSL_SMALL_STACK
  15656. byte *tmp = (byte *)XMALLOC(DSA_TEST_TMP_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15657. DsaKey *key = (DsaKey *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15658. #ifdef WOLFSSL_KEY_GEN
  15659. DsaKey *derIn = (DsaKey *)XMALLOC(sizeof *derIn, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15660. DsaKey *genKey = (DsaKey *)XMALLOC(sizeof *genKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15661. #endif
  15662. if ((tmp == NULL) ||
  15663. (key == NULL)
  15664. #ifdef WOLFSSL_KEY_GEN
  15665. || (derIn == NULL)
  15666. || (genKey == NULL)
  15667. #endif
  15668. ) {
  15669. ret = -8216;
  15670. goto out;
  15671. }
  15672. #else
  15673. byte tmp[1024];
  15674. DsaKey key[1];
  15675. #ifdef WOLFSSL_KEY_GEN
  15676. DsaKey derIn[1];
  15677. DsaKey genKey[1];
  15678. #endif
  15679. #endif
  15680. #ifdef USE_CERT_BUFFERS_1024
  15681. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  15682. bytes = sizeof_dsa_key_der_1024;
  15683. #elif defined(USE_CERT_BUFFERS_2048)
  15684. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  15685. bytes = sizeof_dsa_key_der_2048;
  15686. #else
  15687. {
  15688. XFILE file = XFOPEN(dsaKey, "rb");
  15689. if (!file)
  15690. ERROR_OUT(-8200, out);
  15691. bytes = (word32) XFREAD(tmp, 1, DSA_TEST_TMP_SIZE, file);
  15692. XFCLOSE(file);
  15693. }
  15694. #endif /* USE_CERT_BUFFERS */
  15695. ret = wc_InitSha_ex(&sha, HEAP_HINT, devId);
  15696. if (ret != 0)
  15697. ERROR_OUT(-8201, out);
  15698. wc_ShaUpdate(&sha, tmp, bytes);
  15699. wc_ShaFinal(&sha, hash);
  15700. wc_ShaFree(&sha);
  15701. ret = wc_InitDsaKey(key);
  15702. if (ret != 0)
  15703. ERROR_OUT(-8202, out);
  15704. ret = wc_DsaPrivateKeyDecode(tmp, &idx, key, bytes);
  15705. if (ret != 0)
  15706. ERROR_OUT(-8203, out);
  15707. #ifndef HAVE_FIPS
  15708. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  15709. #else
  15710. ret = wc_InitRng(&rng);
  15711. #endif
  15712. if (ret != 0)
  15713. ERROR_OUT(-8204, out);
  15714. ret = wc_DsaSign(hash, signature, key, &rng);
  15715. if (ret != 0)
  15716. ERROR_OUT(-8205, out);
  15717. ret = wc_DsaVerify(hash, signature, key, &answer);
  15718. if (ret != 0)
  15719. ERROR_OUT(-8206, out);
  15720. if (answer != 1)
  15721. ERROR_OUT(-8207, out);
  15722. wc_FreeDsaKey(key);
  15723. #ifdef WOLFSSL_KEY_GEN
  15724. {
  15725. int derSz = 0;
  15726. ret = wc_InitDsaKey(genKey);
  15727. if (ret != 0)
  15728. ERROR_OUT(-8208, out);
  15729. ret = wc_MakeDsaParameters(&rng, 1024, genKey);
  15730. if (ret != 0) {
  15731. wc_FreeDsaKey(genKey);
  15732. ERROR_OUT(-8209, out);
  15733. }
  15734. ret = wc_MakeDsaKey(&rng, genKey);
  15735. if (ret != 0) {
  15736. wc_FreeDsaKey(genKey);
  15737. ERROR_OUT(-8210, out);
  15738. }
  15739. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15740. if (der == NULL) {
  15741. wc_FreeDsaKey(genKey);
  15742. ERROR_OUT(-8211, out);
  15743. }
  15744. derSz = wc_DsaKeyToDer(genKey, der, FOURK_BUF);
  15745. if (derSz < 0) {
  15746. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15747. ERROR_OUT(-8212, out);
  15748. }
  15749. ret = SaveDerAndPem(der, derSz, keyDerFile, keyPemFile,
  15750. DSA_PRIVATEKEY_TYPE, -5814);
  15751. if (ret != 0) {
  15752. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15753. wc_FreeDsaKey(genKey);
  15754. goto out;
  15755. }
  15756. ret = wc_InitDsaKey(derIn);
  15757. if (ret != 0) {
  15758. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15759. wc_FreeDsaKey(genKey);
  15760. ERROR_OUT(-8213, out);
  15761. }
  15762. idx = 0;
  15763. ret = wc_DsaPrivateKeyDecode(der, &idx, derIn, derSz);
  15764. if (ret != 0) {
  15765. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15766. wc_FreeDsaKey(derIn);
  15767. wc_FreeDsaKey(genKey);
  15768. ERROR_OUT(-8214, out);
  15769. }
  15770. }
  15771. #endif /* WOLFSSL_KEY_GEN */
  15772. out:
  15773. #ifdef WOLFSSL_SMALL_STACK
  15774. if (key) {
  15775. #endif
  15776. if (wc_InitDsaKey_h(key, NULL) != 0)
  15777. ret = -8215;
  15778. #ifdef WOLFSSL_SMALL_STACK
  15779. }
  15780. #endif
  15781. #ifdef WOLFSSL_KEY_GEN
  15782. if (der)
  15783. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15784. #endif
  15785. #ifdef WOLFSSL_SMALL_STACK
  15786. if (tmp)
  15787. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15788. if (key)
  15789. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15790. #ifdef WOLFSSL_KEY_GEN
  15791. if (derIn) {
  15792. wc_FreeDsaKey(derIn);
  15793. XFREE(derIn, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15794. }
  15795. if (genKey) {
  15796. wc_FreeDsaKey(genKey);
  15797. XFREE(genKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15798. }
  15799. #endif
  15800. #else /* !WOLFSSL_SMALL_STACK */
  15801. #ifdef WOLFSSL_KEY_GEN
  15802. wc_FreeDsaKey(derIn);
  15803. wc_FreeDsaKey(genKey);
  15804. #endif
  15805. #endif
  15806. wc_FreeRng(&rng);
  15807. return ret;
  15808. }
  15809. #endif /* NO_DSA */
  15810. #ifdef WOLFCRYPT_HAVE_SRP
  15811. static int generate_random_salt(byte *buf, word32 size)
  15812. {
  15813. int ret = -8220;
  15814. WC_RNG rng;
  15815. if(NULL == buf || !size)
  15816. return -8221;
  15817. if (buf && size && wc_InitRng_ex(&rng, HEAP_HINT, devId) == 0) {
  15818. ret = wc_RNG_GenerateBlock(&rng, (byte *)buf, size);
  15819. wc_FreeRng(&rng);
  15820. }
  15821. return ret;
  15822. }
  15823. static int srp_test_digest(SrpType dgstType)
  15824. {
  15825. int r;
  15826. byte clientPubKey[192]; /* A */
  15827. byte serverPubKey[192]; /* B */
  15828. word32 clientPubKeySz = 192;
  15829. word32 serverPubKeySz = 192;
  15830. byte username[] = "user";
  15831. word32 usernameSz = 4;
  15832. byte password[] = "password";
  15833. word32 passwordSz = 8;
  15834. WOLFSSL_SMALL_STACK_STATIC const byte N[] = {
  15835. 0xfc, 0x58, 0x7a, 0x8a, 0x70, 0xfb, 0x5a, 0x9a,
  15836. 0x5d, 0x39, 0x48, 0xbf, 0x1c, 0x46, 0xd8, 0x3b,
  15837. 0x7a, 0xe9, 0x1f, 0x85, 0x36, 0x18, 0xc4, 0x35,
  15838. 0x3f, 0xf8, 0x8a, 0x8f, 0x8c, 0x10, 0x2e, 0x01,
  15839. 0x58, 0x1d, 0x41, 0xcb, 0xc4, 0x47, 0xa8, 0xaf,
  15840. 0x9a, 0x6f, 0x58, 0x14, 0xa4, 0x68, 0xf0, 0x9c,
  15841. 0xa6, 0xe7, 0xbf, 0x0d, 0xe9, 0x62, 0x0b, 0xd7,
  15842. 0x26, 0x46, 0x5b, 0x27, 0xcb, 0x4c, 0xf9, 0x7e,
  15843. 0x1e, 0x8b, 0xe6, 0xdd, 0x29, 0xb7, 0xb7, 0x15,
  15844. 0x2e, 0xcf, 0x23, 0xa6, 0x4b, 0x97, 0x9f, 0x89,
  15845. 0xd4, 0x86, 0xc4, 0x90, 0x63, 0x92, 0xf4, 0x30,
  15846. 0x26, 0x69, 0x48, 0x9d, 0x7a, 0x4f, 0xad, 0xb5,
  15847. 0x6a, 0x51, 0xad, 0xeb, 0xf9, 0x90, 0x31, 0x77,
  15848. 0x53, 0x30, 0x2a, 0x85, 0xf7, 0x11, 0x21, 0x0c,
  15849. 0xb8, 0x4b, 0x56, 0x03, 0x5e, 0xbb, 0x25, 0x33,
  15850. 0x7c, 0xd9, 0x5a, 0xd1, 0x5c, 0xb2, 0xd4, 0x53,
  15851. 0xc5, 0x16, 0x68, 0xf0, 0xdf, 0x48, 0x55, 0x3e,
  15852. 0xd4, 0x59, 0x87, 0x64, 0x59, 0xaa, 0x39, 0x01,
  15853. 0x45, 0x89, 0x9c, 0x72, 0xff, 0xdd, 0x8f, 0x6d,
  15854. 0xa0, 0x42, 0xbc, 0x6f, 0x6e, 0x62, 0x18, 0x2d,
  15855. 0x50, 0xe8, 0x18, 0x97, 0x87, 0xfc, 0xef, 0x1f,
  15856. 0xf5, 0x53, 0x68, 0xe8, 0x49, 0xd1, 0xa2, 0xe8,
  15857. 0xb9, 0x26, 0x03, 0xba, 0xb5, 0x58, 0x6f, 0x6c,
  15858. 0x8b, 0x08, 0xa1, 0x7b, 0x6f, 0x42, 0xc9, 0x53
  15859. };
  15860. WOLFSSL_SMALL_STACK_STATIC const byte g[] = {
  15861. 0x02
  15862. };
  15863. byte salt[10];
  15864. byte verifier[192];
  15865. word32 v_size = sizeof(verifier);
  15866. word32 clientProofSz = SRP_MAX_DIGEST_SIZE;
  15867. word32 serverProofSz = SRP_MAX_DIGEST_SIZE;
  15868. #ifdef WOLFSSL_SMALL_STACK
  15869. Srp *cli = (Srp *)XMALLOC(sizeof *cli, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15870. Srp *srv = (Srp *)XMALLOC(sizeof *srv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15871. byte *clientProof = (byte *)XMALLOC(SRP_MAX_DIGEST_SIZE, HEAP_HINT,
  15872. DYNAMIC_TYPE_TMP_BUFFER); /* M1 */
  15873. byte *serverProof = (byte *)XMALLOC(SRP_MAX_DIGEST_SIZE, HEAP_HINT,
  15874. DYNAMIC_TYPE_TMP_BUFFER); /* M2 */
  15875. if ((cli == NULL) ||
  15876. (srv == NULL) ||
  15877. (clientProof == NULL) ||
  15878. (serverProof == NULL)) {
  15879. r = -8222;
  15880. goto out;
  15881. }
  15882. #else
  15883. Srp cli[1], srv[1];
  15884. byte clientProof[SRP_MAX_DIGEST_SIZE]; /* M1 */
  15885. byte serverProof[SRP_MAX_DIGEST_SIZE]; /* M2 */
  15886. #endif
  15887. /* set as 0's so if second init on srv not called SrpTerm is not on
  15888. * garbage values */
  15889. XMEMSET(srv, 0, sizeof *srv);
  15890. XMEMSET(cli, 0, sizeof *cli);
  15891. /* generating random salt */
  15892. r = generate_random_salt(salt, sizeof(salt));
  15893. /* client knows username and password. */
  15894. /* server knows N, g, salt and verifier. */
  15895. if (!r) r = wc_SrpInit_ex(cli, dgstType, SRP_CLIENT_SIDE, HEAP_HINT, devId);
  15896. if (!r) r = wc_SrpSetUsername(cli, username, usernameSz);
  15897. /* loading N, g and salt in advance to generate the verifier. */
  15898. if (!r) r = wc_SrpSetParams(cli, N, sizeof(N),
  15899. g, sizeof(g),
  15900. salt, sizeof(salt));
  15901. if (!r) r = wc_SrpSetPassword(cli, password, passwordSz);
  15902. if (!r) r = wc_SrpGetVerifier(cli, verifier, &v_size);
  15903. /* client sends username to server */
  15904. if (!r) r = wc_SrpInit_ex(srv, dgstType, SRP_SERVER_SIDE, HEAP_HINT, devId);
  15905. if (!r) r = wc_SrpSetUsername(srv, username, usernameSz);
  15906. if (!r) r = wc_SrpSetParams(srv, N, sizeof(N),
  15907. g, sizeof(g),
  15908. salt, sizeof(salt));
  15909. if (!r) r = wc_SrpSetVerifier(srv, verifier, v_size);
  15910. if (!r) r = wc_SrpGetPublic(srv, serverPubKey, &serverPubKeySz);
  15911. /* server sends N, g, salt and B to client */
  15912. if (!r) r = wc_SrpGetPublic(cli, clientPubKey, &clientPubKeySz);
  15913. if (!r) r = wc_SrpComputeKey(cli, clientPubKey, clientPubKeySz,
  15914. serverPubKey, serverPubKeySz);
  15915. if (!r) r = wc_SrpGetProof(cli, clientProof, &clientProofSz);
  15916. /* client sends A and M1 to server */
  15917. if (!r) r = wc_SrpComputeKey(srv, clientPubKey, clientPubKeySz,
  15918. serverPubKey, serverPubKeySz);
  15919. if (!r) r = wc_SrpVerifyPeersProof(srv, clientProof, clientProofSz);
  15920. if (!r) r = wc_SrpGetProof(srv, serverProof, &serverProofSz);
  15921. /* server sends M2 to client */
  15922. if (!r) r = wc_SrpVerifyPeersProof(cli, serverProof, serverProofSz);
  15923. wc_SrpTerm(cli);
  15924. wc_SrpTerm(srv);
  15925. #ifdef WOLFSSL_SMALL_STACK
  15926. out:
  15927. if (cli)
  15928. XFREE(cli, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15929. if (srv)
  15930. XFREE(srv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15931. if (clientProof)
  15932. XFREE(clientProof, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15933. if (serverProof)
  15934. XFREE(serverProof, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15935. #endif
  15936. return r;
  15937. }
  15938. WOLFSSL_TEST_SUBROUTINE int srp_test(void)
  15939. {
  15940. int ret;
  15941. #ifndef NO_SHA
  15942. ret = srp_test_digest(SRP_TYPE_SHA);
  15943. if (ret != 0)
  15944. return ret;
  15945. #endif
  15946. #ifndef NO_SHA256
  15947. ret = srp_test_digest(SRP_TYPE_SHA256);
  15948. if (ret != 0)
  15949. return ret;
  15950. #endif
  15951. #ifdef WOLFSSL_SHA384
  15952. ret = srp_test_digest(SRP_TYPE_SHA384);
  15953. if (ret != 0)
  15954. return ret;
  15955. #endif
  15956. #ifdef WOLFSSL_SHA512
  15957. ret = srp_test_digest(SRP_TYPE_SHA512);
  15958. if (ret != 0)
  15959. return ret;
  15960. #endif
  15961. return ret;
  15962. }
  15963. #endif /* WOLFCRYPT_HAVE_SRP */
  15964. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  15965. #if !defined(NO_AES) && !defined(WOLFCRYPT_ONLY)
  15966. static int openssl_aes_test(void)
  15967. {
  15968. #ifdef HAVE_AES_CBC
  15969. #ifdef WOLFSSL_AES_128
  15970. {
  15971. /* EVP_CipherUpdate test */
  15972. WOLFSSL_SMALL_STACK_STATIC const byte cbcPlain[] =
  15973. {
  15974. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  15975. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  15976. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  15977. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  15978. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  15979. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  15980. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  15981. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  15982. };
  15983. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  15984. "0123456789abcdef "; /* align */
  15985. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  15986. "1234567890abcdef "; /* align */
  15987. byte cipher[AES_BLOCK_SIZE * 4];
  15988. byte plain [AES_BLOCK_SIZE * 4];
  15989. #ifdef WOLFSSL_SMALL_STACK
  15990. EVP_CIPHER_CTX *en = wolfSSL_EVP_CIPHER_CTX_new();
  15991. EVP_CIPHER_CTX *de = wolfSSL_EVP_CIPHER_CTX_new();
  15992. #else
  15993. EVP_CIPHER_CTX en[1];
  15994. EVP_CIPHER_CTX de[1];
  15995. #endif
  15996. int outlen ;
  15997. int total = 0;
  15998. int i;
  15999. #ifdef WOLFSSL_SMALL_STACK
  16000. if ((en == NULL) || (de == NULL))
  16001. return MEMORY_E;
  16002. #endif
  16003. EVP_CIPHER_CTX_init(en);
  16004. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  16005. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  16006. return -8400;
  16007. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen,
  16008. (byte*)cbcPlain, 9) == 0)
  16009. return -8401;
  16010. if (outlen != 0)
  16011. return -8402;
  16012. total += outlen;
  16013. if (EVP_CipherUpdate(en, (byte*)&cipher[total], &outlen,
  16014. (byte*)&cbcPlain[9] , 9) == 0)
  16015. return -8403;
  16016. if (outlen != 16)
  16017. return -8404;
  16018. total += outlen;
  16019. if (EVP_CipherFinal(en, (byte*)&cipher[total], &outlen) == 0)
  16020. return -8405;
  16021. if (outlen != 16)
  16022. return -8406;
  16023. total += outlen;
  16024. if (total != 32)
  16025. return 3408;
  16026. total = 0;
  16027. EVP_CIPHER_CTX_init(de);
  16028. if (EVP_CipherInit(de, EVP_aes_128_cbc(),
  16029. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  16030. return -8407;
  16031. if (EVP_CipherUpdate(de, (byte*)plain, &outlen, (byte*)cipher, 6) == 0)
  16032. return -8408;
  16033. if (outlen != 0)
  16034. return -8409;
  16035. total += outlen;
  16036. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen,
  16037. (byte*)&cipher[6], 12) == 0)
  16038. return -8410;
  16039. if (outlen != 0)
  16040. total += outlen;
  16041. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen,
  16042. (byte*)&cipher[6+12], 14) == 0)
  16043. return -8411;
  16044. if (outlen != 16)
  16045. return -8412;
  16046. total += outlen;
  16047. if (EVP_CipherFinal(de, (byte*)&plain[total], &outlen) == 0)
  16048. return -8413;
  16049. if (outlen != 2)
  16050. return -8414;
  16051. total += outlen;
  16052. if (total != 18)
  16053. return 3427;
  16054. if (XMEMCMP(plain, cbcPlain, 18))
  16055. return -8415;
  16056. /* test with encrypting/decrypting more than 16 bytes at once */
  16057. total = 0;
  16058. EVP_CIPHER_CTX_init(en);
  16059. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  16060. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  16061. return -8416;
  16062. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen,
  16063. (byte*)cbcPlain, 17) == 0)
  16064. return -8417;
  16065. if (outlen != 16)
  16066. return -8418;
  16067. total += outlen;
  16068. if (EVP_CipherUpdate(en, (byte*)&cipher[total], &outlen,
  16069. (byte*)&cbcPlain[17] , 1) == 0)
  16070. return -8419;
  16071. if (outlen != 0)
  16072. return -8420;
  16073. total += outlen;
  16074. if (EVP_CipherFinal(en, (byte*)&cipher[total], &outlen) == 0)
  16075. return -8421;
  16076. if (outlen != 16)
  16077. return -8422;
  16078. total += outlen;
  16079. if (total != 32)
  16080. return -8423;
  16081. total = 0;
  16082. EVP_CIPHER_CTX_init(de);
  16083. if (EVP_CipherInit(de, EVP_aes_128_cbc(),
  16084. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  16085. return -8424;
  16086. if (EVP_CipherUpdate(de, (byte*)plain, &outlen, (byte*)cipher, 17) == 0)
  16087. return -8425;
  16088. if (outlen != 16)
  16089. return -8426;
  16090. total += outlen;
  16091. /* final call on non block size should fail */
  16092. if (EVP_CipherFinal(de, (byte*)&plain[total], &outlen) != 0)
  16093. return -8427;
  16094. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen,
  16095. (byte*)&cipher[17], 1) == 0)
  16096. return -8428;
  16097. if (outlen != 0)
  16098. total += outlen;
  16099. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen,
  16100. (byte*)&cipher[17+1], 14) == 0)
  16101. return -8429;
  16102. if (outlen != 0)
  16103. return -8430;
  16104. total += outlen;
  16105. if (EVP_CipherFinal(de, (byte*)&plain[total], &outlen) == 0)
  16106. return -8431;
  16107. if (outlen != 2)
  16108. return -8432;
  16109. total += outlen;
  16110. if (total != 18)
  16111. return -8433;
  16112. if (XMEMCMP(plain, cbcPlain, 18))
  16113. return -8434;
  16114. /* test byte by byte decrypt */
  16115. for (i = 0; i < AES_BLOCK_SIZE * 3; i++) {
  16116. plain[i] = i;
  16117. }
  16118. total = 0;
  16119. EVP_CIPHER_CTX_init(en);
  16120. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  16121. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  16122. return -8435;
  16123. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen,
  16124. (byte*)plain, AES_BLOCK_SIZE * 3) == 0)
  16125. return -8436;
  16126. if (outlen != AES_BLOCK_SIZE * 3)
  16127. return -8437;
  16128. total += outlen;
  16129. if (EVP_CipherFinal(en, (byte*)&cipher[total], &outlen) == 0)
  16130. return -8438;
  16131. if (outlen != AES_BLOCK_SIZE)
  16132. return -8439;
  16133. total += outlen;
  16134. if (total != sizeof(plain))
  16135. return -8440;
  16136. total = 0;
  16137. EVP_CIPHER_CTX_init(de);
  16138. if (EVP_CipherInit(de, EVP_aes_128_cbc(),
  16139. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  16140. return -8441;
  16141. for (i = 0; i < AES_BLOCK_SIZE * 4; i++) {
  16142. if (EVP_CipherUpdate(de, (byte*)plain + total, &outlen,
  16143. (byte*)cipher + i, 1) == 0)
  16144. return -8442;
  16145. if (outlen > 0) {
  16146. int j;
  16147. total += outlen;
  16148. for (j = 0; j < total; j++) {
  16149. if (plain[j] != j) {
  16150. return -8443;
  16151. }
  16152. }
  16153. }
  16154. }
  16155. if (EVP_CipherFinal(de, (byte*)&plain[total], &outlen) == 0)
  16156. return -8444;
  16157. total += outlen;
  16158. if (total != AES_BLOCK_SIZE * 3) {
  16159. return -8445;
  16160. }
  16161. for (i = 0; i < AES_BLOCK_SIZE * 3; i++) {
  16162. if (plain[i] != i) {
  16163. return -8446;
  16164. }
  16165. }
  16166. #ifdef WOLFSSL_SMALL_STACK
  16167. wolfSSL_EVP_CIPHER_CTX_free(en);
  16168. wolfSSL_EVP_CIPHER_CTX_free(de);
  16169. #endif
  16170. }
  16171. /* set buffers to be exact size to catch potential over read/write */
  16172. {
  16173. /* EVP_CipherUpdate test */
  16174. WOLFSSL_SMALL_STACK_STATIC const byte cbcPlain[] =
  16175. {
  16176. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  16177. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  16178. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  16179. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  16180. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  16181. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  16182. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  16183. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  16184. };
  16185. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  16186. "0123456789abcdef "; /* align */
  16187. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  16188. "1234567890abcdef "; /* align */
  16189. #define EVP_TEST_BUF_SZ 18
  16190. #define EVP_TEST_BUF_PAD 32
  16191. byte cipher[EVP_TEST_BUF_SZ];
  16192. byte plain [EVP_TEST_BUF_SZ];
  16193. byte padded[EVP_TEST_BUF_PAD];
  16194. #ifdef WOLFSSL_SMALL_STACK
  16195. EVP_CIPHER_CTX *en = wolfSSL_EVP_CIPHER_CTX_new();
  16196. EVP_CIPHER_CTX *de = wolfSSL_EVP_CIPHER_CTX_new();
  16197. #else
  16198. EVP_CIPHER_CTX en[1];
  16199. EVP_CIPHER_CTX de[1];
  16200. #endif
  16201. int outlen ;
  16202. int total = 0;
  16203. #ifdef WOLFSSL_SMALL_STACK
  16204. if ((en == NULL) || (de == NULL))
  16205. return MEMORY_E;
  16206. #endif
  16207. EVP_CIPHER_CTX_init(en);
  16208. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  16209. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  16210. return -8447;
  16211. if (EVP_CIPHER_CTX_set_padding(en, 0) != 1)
  16212. return -8448;
  16213. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen,
  16214. (byte*)cbcPlain, EVP_TEST_BUF_SZ) == 0)
  16215. return -8449;
  16216. if (outlen != 16)
  16217. return -8450;
  16218. total += outlen;
  16219. /* should fail here */
  16220. if (EVP_CipherFinal(en, (byte*)&cipher[total], &outlen) != 0)
  16221. return -8451;
  16222. /* turn padding back on and do successful encrypt */
  16223. total = 0;
  16224. EVP_CIPHER_CTX_init(en);
  16225. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  16226. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  16227. return -8452;
  16228. if (EVP_CIPHER_CTX_set_padding(en, 1) != 1)
  16229. return -8453;
  16230. if (EVP_CipherUpdate(en, (byte*)padded, &outlen,
  16231. (byte*)cbcPlain, EVP_TEST_BUF_SZ) == 0)
  16232. return -8454;
  16233. if (outlen != 16)
  16234. return -8455;
  16235. total += outlen;
  16236. if (EVP_CipherFinal(en, (byte*)&padded[total], &outlen) == 0)
  16237. return -8456;
  16238. total += outlen;
  16239. if (total != 32)
  16240. return -8457;
  16241. XMEMCPY(cipher, padded, EVP_TEST_BUF_SZ);
  16242. /* test out of bounds read on buffers w/o padding during decryption */
  16243. total = 0;
  16244. EVP_CIPHER_CTX_init(de);
  16245. if (EVP_CipherInit(de, EVP_aes_128_cbc(),
  16246. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  16247. return -8458;
  16248. if (EVP_CIPHER_CTX_set_padding(de, 0) != 1)
  16249. return -8459;
  16250. if (EVP_CipherUpdate(de, (byte*)plain, &outlen, (byte*)cipher,
  16251. EVP_TEST_BUF_SZ) == 0)
  16252. return -8460;
  16253. if (outlen != 16)
  16254. return -8461;
  16255. total += outlen;
  16256. /* should fail since not using padding */
  16257. if (EVP_CipherFinal(de, (byte*)&plain[total], &outlen) != 0)
  16258. return -8462;
  16259. total = 0;
  16260. EVP_CIPHER_CTX_init(de);
  16261. if (EVP_CipherInit(de, EVP_aes_128_cbc(),
  16262. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  16263. return -8463;
  16264. if (EVP_CIPHER_CTX_set_padding(de, 1) != 1)
  16265. return -8464;
  16266. if (EVP_CipherUpdate(de, (byte*)padded, &outlen, (byte*)padded,
  16267. EVP_TEST_BUF_PAD) == 0)
  16268. return -8465;
  16269. if (outlen != 16)
  16270. return -8466;
  16271. total += outlen;
  16272. if (EVP_CipherFinal(de, (byte*)&padded[total], &outlen) == 0)
  16273. return -8467;
  16274. if (XMEMCMP(padded, cbcPlain, EVP_TEST_BUF_SZ))
  16275. return -8468;
  16276. #ifdef WOLFSSL_SMALL_STACK
  16277. wolfSSL_EVP_CIPHER_CTX_free(en);
  16278. wolfSSL_EVP_CIPHER_CTX_free(de);
  16279. #endif
  16280. }
  16281. { /* evp_cipher test: EVP_aes_128_cbc */
  16282. #ifdef WOLFSSL_SMALL_STACK
  16283. EVP_CIPHER_CTX *ctx = wolfSSL_EVP_CIPHER_CTX_new();
  16284. #else
  16285. EVP_CIPHER_CTX ctx[1];
  16286. #endif
  16287. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = { /* "Now is the time for all " w/o trailing 0 */
  16288. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  16289. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  16290. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  16291. };
  16292. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  16293. {
  16294. 0x95,0x94,0x92,0x57,0x5f,0x42,0x81,0x53,
  16295. 0x2c,0xcc,0x9d,0x46,0x77,0xa2,0x33,0xcb
  16296. };
  16297. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  16298. "0123456789abcdef "; /* align */
  16299. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  16300. "1234567890abcdef "; /* align */
  16301. byte cipher[AES_BLOCK_SIZE * 4];
  16302. byte plain [AES_BLOCK_SIZE * 4];
  16303. #ifdef WOLFSSL_SMALL_STACK
  16304. if (ctx == NULL)
  16305. return MEMORY_E;
  16306. #endif
  16307. EVP_CIPHER_CTX_init(ctx);
  16308. if (EVP_CipherInit(ctx, EVP_aes_128_cbc(), key, iv, 1) == 0)
  16309. return -8469;
  16310. if (EVP_Cipher(ctx, cipher, (byte*)msg, 16) != 16)
  16311. return -8470;
  16312. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE))
  16313. return -8471;
  16314. EVP_CIPHER_CTX_init(ctx);
  16315. if (EVP_CipherInit(ctx, EVP_aes_128_cbc(), key, iv, 0) == 0)
  16316. return -8472;
  16317. if (EVP_Cipher(ctx, plain, cipher, 16) != 16)
  16318. return -8473;
  16319. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  16320. return -8474;
  16321. #ifdef WOLFSSL_SMALL_STACK
  16322. wolfSSL_EVP_CIPHER_CTX_free(ctx);
  16323. #endif
  16324. } /* end evp_cipher test: EVP_aes_128_cbc*/
  16325. #endif /* WOLFSSL_AES_128 */
  16326. #endif /* HAVE_AES_CBC */
  16327. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_256)
  16328. { /* evp_cipher test: EVP_aes_256_ecb*/
  16329. #ifdef WOLFSSL_SMALL_STACK
  16330. EVP_CIPHER_CTX *ctx = wolfSSL_EVP_CIPHER_CTX_new();
  16331. #else
  16332. EVP_CIPHER_CTX ctx[1];
  16333. #endif
  16334. WOLFSSL_SMALL_STACK_STATIC const byte msg[] =
  16335. {
  16336. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  16337. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  16338. };
  16339. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  16340. {
  16341. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  16342. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  16343. };
  16344. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  16345. {
  16346. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  16347. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  16348. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  16349. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  16350. };
  16351. byte cipher[AES_BLOCK_SIZE * 4];
  16352. byte plain [AES_BLOCK_SIZE * 4];
  16353. #ifdef WOLFSSL_SMALL_STACK
  16354. if (ctx == NULL)
  16355. return MEMORY_E;
  16356. #endif
  16357. EVP_CIPHER_CTX_init(ctx);
  16358. if (EVP_CipherInit(ctx, EVP_aes_256_ecb(), (unsigned char*)key, NULL, 1) == 0)
  16359. return -8475;
  16360. if (EVP_Cipher(ctx, cipher, (byte*)msg, 16) != 16)
  16361. return -8476;
  16362. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE))
  16363. return -8477;
  16364. EVP_CIPHER_CTX_init(ctx);
  16365. if (EVP_CipherInit(ctx, EVP_aes_256_ecb(), (unsigned char*)key, NULL, 0) == 0)
  16366. return -8478;
  16367. if (EVP_Cipher(ctx, plain, cipher, 16) != 16)
  16368. return -8479;
  16369. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  16370. return -8480;
  16371. #ifdef WOLFSSL_SMALL_STACK
  16372. wolfSSL_EVP_CIPHER_CTX_free(ctx);
  16373. #endif
  16374. } /* end evp_cipher test */
  16375. #endif /* HAVE_AES_ECB && WOLFSSL_AES_256 */
  16376. #if defined(WOLFSSL_AES_DIRECT) && defined(WOLFSSL_AES_256)
  16377. /* enable HAVE_AES_DECRYPT for AES_encrypt/decrypt */
  16378. {
  16379. /* Test: AES_encrypt/decrypt/set Key */
  16380. #ifdef WOLFSSL_SMALL_STACK
  16381. AES_KEY *enc = (AES_KEY *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  16382. #ifdef HAVE_AES_DECRYPT
  16383. AES_KEY *dec = (AES_KEY *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  16384. #endif
  16385. #else
  16386. AES_KEY enc[1];
  16387. #ifdef HAVE_AES_DECRYPT
  16388. AES_KEY dec[1];
  16389. #endif
  16390. #endif
  16391. WOLFSSL_SMALL_STACK_STATIC const byte msg[] =
  16392. {
  16393. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  16394. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  16395. };
  16396. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  16397. {
  16398. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  16399. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  16400. };
  16401. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  16402. {
  16403. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  16404. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  16405. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  16406. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  16407. };
  16408. byte plain[sizeof(msg)];
  16409. byte cipher[sizeof(msg)];
  16410. #ifdef WOLFSSL_SMALL_STACK
  16411. if (enc == NULL)
  16412. return MEMORY_E;
  16413. #ifdef HAVE_AES_DECRYPT
  16414. if (dec == NULL)
  16415. return MEMORY_E;
  16416. #endif
  16417. #endif
  16418. AES_set_encrypt_key(key, sizeof(key)*8, enc);
  16419. AES_set_decrypt_key(key, sizeof(key)*8, dec);
  16420. AES_encrypt(msg, cipher, enc);
  16421. #ifdef HAVE_AES_DECRYPT
  16422. AES_decrypt(cipher, plain, dec);
  16423. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  16424. return -8481;
  16425. #endif /* HAVE_AES_DECRYPT */
  16426. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE))
  16427. return -8482;
  16428. #ifdef WOLFSSL_SMALL_STACK
  16429. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  16430. #ifdef HAVE_AES_DECRYPT
  16431. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  16432. #endif
  16433. #endif
  16434. }
  16435. #endif /* WOLFSSL_AES_DIRECT && WOLFSSL_AES_256 */
  16436. /* EVP_Cipher with EVP_aes_xxx_ctr() */
  16437. #ifdef WOLFSSL_AES_COUNTER
  16438. {
  16439. byte plainBuff [64];
  16440. byte cipherBuff[64];
  16441. #ifdef WOLFSSL_AES_128
  16442. WOLFSSL_SMALL_STACK_STATIC const byte ctrKey[] =
  16443. {
  16444. 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,
  16445. 0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c
  16446. };
  16447. WOLFSSL_SMALL_STACK_STATIC const byte ctrIv[] =
  16448. {
  16449. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  16450. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  16451. };
  16452. WOLFSSL_SMALL_STACK_STATIC const byte ctrPlain[] =
  16453. {
  16454. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  16455. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  16456. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  16457. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  16458. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  16459. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  16460. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  16461. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  16462. };
  16463. WOLFSSL_SMALL_STACK_STATIC const byte ctrCipher[] =
  16464. {
  16465. 0x87,0x4d,0x61,0x91,0xb6,0x20,0xe3,0x26,
  16466. 0x1b,0xef,0x68,0x64,0x99,0x0d,0xb6,0xce,
  16467. 0x98,0x06,0xf6,0x6b,0x79,0x70,0xfd,0xff,
  16468. 0x86,0x17,0x18,0x7b,0xb9,0xff,0xfd,0xff,
  16469. 0x5a,0xe4,0xdf,0x3e,0xdb,0xd5,0xd3,0x5e,
  16470. 0x5b,0x4f,0x09,0x02,0x0d,0xb0,0x3e,0xab,
  16471. 0x1e,0x03,0x1d,0xda,0x2f,0xbe,0x03,0xd1,
  16472. 0x79,0x21,0x70,0xa0,0xf3,0x00,0x9c,0xee
  16473. };
  16474. WOLFSSL_SMALL_STACK_STATIC const byte oddCipher[] =
  16475. {
  16476. 0xb9,0xd7,0xcb,0x08,0xb0,0xe1,0x7b,0xa0,
  16477. 0xc2
  16478. };
  16479. #endif
  16480. /* test vector from "Recommendation for Block Cipher Modes of Operation"
  16481. * NIST Special Publication 800-38A */
  16482. #ifdef WOLFSSL_AES_192
  16483. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Key[] =
  16484. {
  16485. 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  16486. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  16487. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b
  16488. };
  16489. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Iv[] =
  16490. {
  16491. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  16492. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  16493. };
  16494. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Plain[] =
  16495. {
  16496. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  16497. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  16498. };
  16499. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Cipher[] =
  16500. {
  16501. 0x1a,0xbc,0x93,0x24,0x17,0x52,0x1c,0xa2,
  16502. 0x4f,0x2b,0x04,0x59,0xfe,0x7e,0x6e,0x0b
  16503. };
  16504. #endif /* WOLFSSL_AES_192 */
  16505. #ifdef WOLFSSL_AES_256
  16506. /* test vector from "Recommendation for Block Cipher Modes of Operation"
  16507. * NIST Special Publication 800-38A */
  16508. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Key[] =
  16509. {
  16510. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  16511. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  16512. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  16513. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  16514. };
  16515. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Iv[] =
  16516. {
  16517. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  16518. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  16519. };
  16520. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Plain[] =
  16521. {
  16522. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  16523. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  16524. };
  16525. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Cipher[] =
  16526. {
  16527. 0x60,0x1e,0xc3,0x13,0x77,0x57,0x89,0xa5,
  16528. 0xb7,0xa7,0xf5,0x04,0xbb,0xf3,0xd2,0x28
  16529. };
  16530. #endif /* WOLFSSL_AES_256 */
  16531. #ifdef WOLFSSL_SMALL_STACK
  16532. EVP_CIPHER_CTX *en = wolfSSL_EVP_CIPHER_CTX_new();
  16533. EVP_CIPHER_CTX *de = wolfSSL_EVP_CIPHER_CTX_new();
  16534. #else
  16535. EVP_CIPHER_CTX en[1];
  16536. EVP_CIPHER_CTX de[1];
  16537. #endif
  16538. #ifdef WOLFSSL_AES_128
  16539. #ifndef WOLFSSL_SMALL_STACK
  16540. EVP_CIPHER_CTX *p_en;
  16541. EVP_CIPHER_CTX *p_de;
  16542. #endif
  16543. #ifdef WOLFSSL_SMALL_STACK
  16544. if ((en == NULL) || (de == NULL))
  16545. return MEMORY_E;
  16546. #endif
  16547. EVP_CIPHER_CTX_init(en);
  16548. if (EVP_CipherInit(en, EVP_aes_128_ctr(),
  16549. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  16550. return -8483;
  16551. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctrPlain,
  16552. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  16553. return -8484;
  16554. EVP_CIPHER_CTX_init(de);
  16555. if (EVP_CipherInit(de, EVP_aes_128_ctr(),
  16556. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  16557. return -8485;
  16558. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff,
  16559. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  16560. return -8486;
  16561. if (XMEMCMP(cipherBuff, ctrCipher, AES_BLOCK_SIZE*4))
  16562. return -8487;
  16563. if (XMEMCMP(plainBuff, ctrPlain, AES_BLOCK_SIZE*4))
  16564. return -8488;
  16565. #ifndef WOLFSSL_SMALL_STACK
  16566. p_en = wolfSSL_EVP_CIPHER_CTX_new();
  16567. if (p_en == NULL)
  16568. return -8489;
  16569. p_de = wolfSSL_EVP_CIPHER_CTX_new();
  16570. if (p_de == NULL)
  16571. return -8490;
  16572. if (EVP_CipherInit(p_en, EVP_aes_128_ctr(),
  16573. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  16574. return -8491;
  16575. if (EVP_Cipher(p_en, (byte*)cipherBuff, (byte*)ctrPlain,
  16576. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  16577. return -8492;
  16578. if (EVP_CipherInit(p_de, EVP_aes_128_ctr(),
  16579. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  16580. return -8493;
  16581. if (EVP_Cipher(p_de, (byte*)plainBuff, (byte*)cipherBuff,
  16582. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  16583. return -8494;
  16584. wolfSSL_EVP_CIPHER_CTX_free(p_en);
  16585. wolfSSL_EVP_CIPHER_CTX_free(p_de);
  16586. #endif /* !WOLFSSL_SMALL_STACK */
  16587. if (XMEMCMP(cipherBuff, ctrCipher, AES_BLOCK_SIZE*4))
  16588. return -8495;
  16589. if (XMEMCMP(plainBuff, ctrPlain, AES_BLOCK_SIZE*4))
  16590. return -8496;
  16591. EVP_CIPHER_CTX_init(en);
  16592. if (EVP_CipherInit(en, EVP_aes_128_ctr(),
  16593. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  16594. return -8497;
  16595. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctrPlain, 9) != 9)
  16596. return -8498;
  16597. EVP_CIPHER_CTX_init(de);
  16598. if (EVP_CipherInit(de, EVP_aes_128_ctr(),
  16599. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  16600. return -8499;
  16601. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff, 9) != 9)
  16602. return -8500;
  16603. if (XMEMCMP(plainBuff, ctrPlain, 9))
  16604. return -8501;
  16605. if (XMEMCMP(cipherBuff, ctrCipher, 9))
  16606. return -8502;
  16607. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctrPlain, 9) != 9)
  16608. return -8503;
  16609. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff, 9) != 9)
  16610. return -8504;
  16611. if (XMEMCMP(plainBuff, ctrPlain, 9))
  16612. return -8505;
  16613. if (XMEMCMP(cipherBuff, oddCipher, 9))
  16614. return -8506;
  16615. #endif /* WOLFSSL_AES_128 */
  16616. #ifdef WOLFSSL_AES_192
  16617. EVP_CIPHER_CTX_init(en);
  16618. if (EVP_CipherInit(en, EVP_aes_192_ctr(),
  16619. (unsigned char*)ctr192Key, (unsigned char*)ctr192Iv, 0) == 0)
  16620. return -8507;
  16621. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctr192Plain,
  16622. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  16623. return -8508;
  16624. EVP_CIPHER_CTX_init(de);
  16625. if (EVP_CipherInit(de, EVP_aes_192_ctr(),
  16626. (unsigned char*)ctr192Key, (unsigned char*)ctr192Iv, 0) == 0)
  16627. return -8509;
  16628. XMEMSET(plainBuff, 0, sizeof(plainBuff));
  16629. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff,
  16630. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  16631. return -8510;
  16632. if (XMEMCMP(plainBuff, ctr192Plain, sizeof(ctr192Plain)))
  16633. return -8511;
  16634. if (XMEMCMP(ctr192Cipher, cipherBuff, sizeof(ctr192Cipher)))
  16635. return -8512;
  16636. #endif /* WOLFSSL_AES_192 */
  16637. #ifdef WOLFSSL_AES_256
  16638. EVP_CIPHER_CTX_init(en);
  16639. if (EVP_CipherInit(en, EVP_aes_256_ctr(),
  16640. (unsigned char*)ctr256Key, (unsigned char*)ctr256Iv, 0) == 0)
  16641. return -8513;
  16642. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctr256Plain,
  16643. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  16644. return -8514;
  16645. EVP_CIPHER_CTX_init(de);
  16646. if (EVP_CipherInit(de, EVP_aes_256_ctr(),
  16647. (unsigned char*)ctr256Key, (unsigned char*)ctr256Iv, 0) == 0)
  16648. return -8515;
  16649. XMEMSET(plainBuff, 0, sizeof(plainBuff));
  16650. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff,
  16651. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  16652. return -8516;
  16653. if (XMEMCMP(plainBuff, ctr256Plain, sizeof(ctr256Plain)))
  16654. return -8517;
  16655. if (XMEMCMP(ctr256Cipher, cipherBuff, sizeof(ctr256Cipher)))
  16656. return -8518;
  16657. #ifdef WOLFSSL_SMALL_STACK
  16658. wolfSSL_EVP_CIPHER_CTX_free(en);
  16659. wolfSSL_EVP_CIPHER_CTX_free(de);
  16660. #endif
  16661. #endif /* WOLFSSL_AES_256 */
  16662. }
  16663. #endif /* HAVE_AES_COUNTER */
  16664. #if defined(WOLFSSL_AES_CFB) && defined(WOLFSSL_AES_128)
  16665. {
  16666. #ifdef WOLFSSL_SMALL_STACK
  16667. AES_KEY *enc = (AES_KEY *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  16668. AES_KEY *dec = (AES_KEY *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  16669. #else
  16670. AES_KEY enc[1];
  16671. AES_KEY dec[1];
  16672. #endif
  16673. WOLFSSL_SMALL_STACK_STATIC const byte setIv[] = {
  16674. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  16675. 0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f
  16676. };
  16677. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  16678. {
  16679. 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,
  16680. 0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c
  16681. };
  16682. WOLFSSL_SMALL_STACK_STATIC const byte cipher1[] =
  16683. {
  16684. 0x3b,0x3f,0xd9,0x2e,0xb7,0x2d,0xad,0x20,
  16685. 0x33,0x34,0x49,0xf8,0xe8,0x3c,0xfb,0x4a,
  16686. 0xc8,0xa6,0x45,0x37,0xa0,0xb3,0xa9,0x3f,
  16687. 0xcd,0xe3,0xcd,0xad,0x9f,0x1c,0xe5,0x8b
  16688. };
  16689. WOLFSSL_SMALL_STACK_STATIC const byte msg[] =
  16690. {
  16691. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  16692. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  16693. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  16694. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51
  16695. };
  16696. byte cipher[AES_BLOCK_SIZE * 2];
  16697. byte iv[AES_BLOCK_SIZE]; /* iv buffer is updeated by API */
  16698. int num = 0;
  16699. #ifdef WOLFSSL_SMALL_STACK
  16700. if ((enc == NULL) || (dec == NULL))
  16701. return MEMORY_E;
  16702. #endif
  16703. XMEMCPY(iv, setIv, sizeof(setIv));
  16704. wolfSSL_AES_set_encrypt_key(key, sizeof(key) * 8, enc);
  16705. wolfSSL_AES_set_encrypt_key(key, sizeof(key) * 8, dec);
  16706. wolfSSL_AES_cfb128_encrypt(msg, cipher, AES_BLOCK_SIZE - 1, enc, iv,
  16707. &num, AES_ENCRYPT);
  16708. if (XMEMCMP(cipher, cipher1, AES_BLOCK_SIZE - 1))
  16709. return -8519;
  16710. if (num != 15) /* should have used 15 of the 16 bytes */
  16711. return -8520;
  16712. wolfSSL_AES_cfb128_encrypt(msg + AES_BLOCK_SIZE - 1,
  16713. cipher + AES_BLOCK_SIZE - 1, AES_BLOCK_SIZE + 1, enc, iv,
  16714. &num, AES_ENCRYPT);
  16715. if (XMEMCMP(cipher, cipher1, AES_BLOCK_SIZE * 2))
  16716. return -8521;
  16717. if (num != 0)
  16718. return -8522;
  16719. #ifdef WOLFSSL_SMALL_STACK
  16720. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  16721. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  16722. #endif
  16723. }
  16724. #endif /* WOLFSSL_AES_CFB && WOLFSSL_AES_128 */
  16725. return 0;
  16726. }
  16727. #endif /* !defined(NO_AES) && !defined(WOLFCRYPT_ONLY) */
  16728. WOLFSSL_TEST_SUBROUTINE int openssl_test(void)
  16729. {
  16730. int ret;
  16731. EVP_MD_CTX md_ctx;
  16732. testVector a, b, c, d, e, f;
  16733. byte hash[WC_SHA256_DIGEST_SIZE*2]; /* max size */
  16734. a.inLen = 0;
  16735. b.inLen = c.inLen = d.inLen = e.inLen = f.inLen = a.inLen;
  16736. (void)a;
  16737. (void)b;
  16738. (void)c;
  16739. (void)d;
  16740. (void)e;
  16741. (void)f;
  16742. /* test malloc / free , 10 is an arbitrary amount of memory chosen */
  16743. {
  16744. byte* p;
  16745. p = (byte*)CRYPTO_malloc(10, "", 0);
  16746. if (p == NULL) {
  16747. return -8600;
  16748. }
  16749. XMEMSET(p, 0, 10);
  16750. CRYPTO_free(p, "", 0);
  16751. }
  16752. #ifndef NO_MD5
  16753. a.input = "1234567890123456789012345678901234567890123456789012345678"
  16754. "9012345678901234567890";
  16755. a.output = "\x57\xed\xf4\xa2\x2b\xe3\xc9\x55\xac\x49\xda\x2e\x21\x07\xb6"
  16756. "\x7a";
  16757. a.inLen = XSTRLEN(a.input);
  16758. a.outLen = WC_MD5_DIGEST_SIZE;
  16759. EVP_MD_CTX_init(&md_ctx);
  16760. ret = EVP_DigestInit(&md_ctx, EVP_md5());
  16761. if (ret == WOLFSSL_SUCCESS) {
  16762. ret = EVP_DigestUpdate(&md_ctx, a.input, (unsigned long)a.inLen);
  16763. }
  16764. if (ret == WOLFSSL_SUCCESS) {
  16765. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  16766. }
  16767. EVP_MD_CTX_cleanup(&md_ctx);
  16768. if (ret != WOLFSSL_SUCCESS)
  16769. return -18601;
  16770. if (XMEMCMP(hash, a.output, WC_MD5_DIGEST_SIZE) != 0)
  16771. return -8601;
  16772. #endif /* NO_MD5 */
  16773. #ifndef NO_SHA
  16774. b.input = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  16775. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  16776. "aaaaaaaaaa";
  16777. b.output = "\xAD\x5B\x3F\xDB\xCB\x52\x67\x78\xC2\x83\x9D\x2F\x15\x1E\xA7"
  16778. "\x53\x99\x5E\x26\xA0";
  16779. b.inLen = XSTRLEN(b.input);
  16780. b.outLen = WC_SHA_DIGEST_SIZE;
  16781. EVP_MD_CTX_init(&md_ctx);
  16782. ret = EVP_DigestInit(&md_ctx, EVP_sha1());
  16783. if (ret == WOLFSSL_SUCCESS) {
  16784. ret = EVP_DigestUpdate(&md_ctx, b.input, (unsigned long)b.inLen);
  16785. if (ret == WOLFSSL_SUCCESS)
  16786. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  16787. }
  16788. EVP_MD_CTX_cleanup(&md_ctx);
  16789. if (ret != WOLFSSL_SUCCESS)
  16790. return -18602;
  16791. if (XMEMCMP(hash, b.output, WC_SHA_DIGEST_SIZE) != 0)
  16792. return -8602;
  16793. #endif /* NO_SHA */
  16794. #ifdef WOLFSSL_SHA224
  16795. e.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  16796. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  16797. e.output = "\xc9\x7c\xa9\xa5\x59\x85\x0c\xe9\x7a\x04\xa9\x6d\xef\x6d\x99"
  16798. "\xa9\xe0\xe0\xe2\xab\x14\xe6\xb8\xdf\x26\x5f\xc0\xb3";
  16799. e.inLen = XSTRLEN(e.input);
  16800. e.outLen = WC_SHA224_DIGEST_SIZE;
  16801. EVP_MD_CTX_init(&md_ctx);
  16802. ret = EVP_DigestInit(&md_ctx, EVP_sha224());
  16803. if (ret == WOLFSSL_SUCCESS) {
  16804. ret = EVP_DigestUpdate(&md_ctx, e.input, (unsigned long)e.inLen);
  16805. if (ret == WOLFSSL_SUCCESS)
  16806. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  16807. }
  16808. EVP_MD_CTX_cleanup(&md_ctx);
  16809. if (ret != WOLFSSL_SUCCESS ||
  16810. XMEMCMP(hash, e.output, WC_SHA224_DIGEST_SIZE) != 0) {
  16811. return -8603;
  16812. }
  16813. #endif /* WOLFSSL_SHA224 */
  16814. #ifndef NO_SHA256
  16815. d.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  16816. d.output = "\x24\x8D\x6A\x61\xD2\x06\x38\xB8\xE5\xC0\x26\x93\x0C\x3E\x60"
  16817. "\x39\xA3\x3C\xE4\x59\x64\xFF\x21\x67\xF6\xEC\xED\xD4\x19\xDB"
  16818. "\x06\xC1";
  16819. d.inLen = XSTRLEN(d.input);
  16820. d.outLen = WC_SHA256_DIGEST_SIZE;
  16821. EVP_MD_CTX_init(&md_ctx);
  16822. ret = EVP_DigestInit(&md_ctx, EVP_sha256());
  16823. if (ret == WOLFSSL_SUCCESS) {
  16824. ret = EVP_DigestUpdate(&md_ctx, d.input, (unsigned long)d.inLen);
  16825. if (ret == WOLFSSL_SUCCESS)
  16826. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  16827. }
  16828. EVP_MD_CTX_cleanup(&md_ctx);
  16829. if (ret != WOLFSSL_SUCCESS ||
  16830. XMEMCMP(hash, d.output, WC_SHA256_DIGEST_SIZE) != 0) {
  16831. return -8604;
  16832. }
  16833. #endif /* !NO_SHA256 */
  16834. #ifdef WOLFSSL_SHA384
  16835. e.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  16836. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  16837. e.output = "\x09\x33\x0c\x33\xf7\x11\x47\xe8\x3d\x19\x2f\xc7\x82\xcd\x1b"
  16838. "\x47\x53\x11\x1b\x17\x3b\x3b\x05\xd2\x2f\xa0\x80\x86\xe3\xb0"
  16839. "\xf7\x12\xfc\xc7\xc7\x1a\x55\x7e\x2d\xb9\x66\xc3\xe9\xfa\x91"
  16840. "\x74\x60\x39";
  16841. e.inLen = XSTRLEN(e.input);
  16842. e.outLen = WC_SHA384_DIGEST_SIZE;
  16843. EVP_MD_CTX_init(&md_ctx);
  16844. ret = EVP_DigestInit(&md_ctx, EVP_sha384());
  16845. if (ret == WOLFSSL_SUCCESS) {
  16846. ret = EVP_DigestUpdate(&md_ctx, e.input, (unsigned long)e.inLen);
  16847. if (ret == WOLFSSL_SUCCESS)
  16848. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  16849. }
  16850. EVP_MD_CTX_cleanup(&md_ctx);
  16851. if (ret != WOLFSSL_SUCCESS ||
  16852. XMEMCMP(hash, e.output, WC_SHA384_DIGEST_SIZE) != 0) {
  16853. return -8605;
  16854. }
  16855. #endif /* WOLFSSL_SHA384 */
  16856. #ifdef WOLFSSL_SHA512
  16857. f.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  16858. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  16859. f.output = "\x8e\x95\x9b\x75\xda\xe3\x13\xda\x8c\xf4\xf7\x28\x14\xfc\x14"
  16860. "\x3f\x8f\x77\x79\xc6\xeb\x9f\x7f\xa1\x72\x99\xae\xad\xb6\x88"
  16861. "\x90\x18\x50\x1d\x28\x9e\x49\x00\xf7\xe4\x33\x1b\x99\xde\xc4"
  16862. "\xb5\x43\x3a\xc7\xd3\x29\xee\xb6\xdd\x26\x54\x5e\x96\xe5\x5b"
  16863. "\x87\x4b\xe9\x09";
  16864. f.inLen = XSTRLEN(f.input);
  16865. f.outLen = WC_SHA512_DIGEST_SIZE;
  16866. EVP_MD_CTX_init(&md_ctx);
  16867. ret = EVP_DigestInit(&md_ctx, EVP_sha512());
  16868. if (ret == WOLFSSL_SUCCESS) {
  16869. ret = EVP_DigestUpdate(&md_ctx, f.input, (unsigned long)f.inLen);
  16870. if (ret == WOLFSSL_SUCCESS)
  16871. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  16872. }
  16873. EVP_MD_CTX_cleanup(&md_ctx);
  16874. if (ret != WOLFSSL_SUCCESS ||
  16875. XMEMCMP(hash, f.output, WC_SHA512_DIGEST_SIZE) != 0) {
  16876. return -8606;
  16877. }
  16878. #endif /* WOLFSSL_SHA512 */
  16879. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  16880. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  16881. f.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  16882. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  16883. f.output = "\x23\xfe\xc5\xbb\x94\xd6\x0b\x23\x30\x81\x92\x64\x0b\x0c\x45"
  16884. "\x33\x35\xd6\x64\x73\x4f\xe4\x0e\x72\x68\x67\x4a\xf9";
  16885. f.inLen = XSTRLEN(f.input);
  16886. f.outLen = WC_SHA512_224_DIGEST_SIZE;
  16887. EVP_MD_CTX_init(&md_ctx);
  16888. ret = EVP_DigestInit(&md_ctx, EVP_sha512_224());
  16889. if (ret == WOLFSSL_SUCCESS) {
  16890. ret = EVP_DigestUpdate(&md_ctx, f.input, (unsigned long)f.inLen);
  16891. if (ret == WOLFSSL_SUCCESS)
  16892. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  16893. }
  16894. EVP_MD_CTX_cleanup(&md_ctx);
  16895. if (ret != WOLFSSL_SUCCESS ||
  16896. XMEMCMP(hash, f.output, WC_SHA512_224_DIGEST_SIZE) != 0) {
  16897. return -8722;
  16898. }
  16899. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_224 */
  16900. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  16901. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  16902. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  16903. f.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  16904. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  16905. f.output = "\x39\x28\xe1\x84\xfb\x86\x90\xf8\x40\xda\x39\x88\x12\x1d\x31"
  16906. "\xbe\x65\xcb\x9d\x3e\xf8\x3e\xe6\x14\x6f\xea\xc8\x61\xe1\x9b"
  16907. "\x56\x3a";
  16908. f.inLen = XSTRLEN(f.input);
  16909. f.outLen = WC_SHA512_256_DIGEST_SIZE;
  16910. EVP_MD_CTX_init(&md_ctx);
  16911. ret = EVP_DigestInit(&md_ctx, EVP_sha512_256());
  16912. if (ret == WOLFSSL_SUCCESS) {
  16913. ret = EVP_DigestUpdate(&md_ctx, f.input, (unsigned long)f.inLen);
  16914. if (ret == WOLFSSL_SUCCESS)
  16915. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  16916. }
  16917. EVP_MD_CTX_cleanup(&md_ctx);
  16918. if (ret != WOLFSSL_SUCCESS ||
  16919. XMEMCMP(hash, f.output, WC_SHA512_256_DIGEST_SIZE) != 0) {
  16920. return -8723;
  16921. }
  16922. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_224 */
  16923. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  16924. #ifdef WOLFSSL_SHA3
  16925. #ifndef WOLFSSL_NOSHA3_224
  16926. e.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  16927. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  16928. e.output = "\x54\x3e\x68\x68\xe1\x66\x6c\x1a\x64\x36\x30\xdf\x77\x36\x7a"
  16929. "\xe5\xa6\x2a\x85\x07\x0a\x51\xc1\x4c\xbf\x66\x5c\xbc";
  16930. e.inLen = XSTRLEN(e.input);
  16931. e.outLen = WC_SHA3_224_DIGEST_SIZE;
  16932. EVP_MD_CTX_init(&md_ctx);
  16933. ret = EVP_DigestInit(&md_ctx, EVP_sha3_224());
  16934. if (ret == WOLFSSL_SUCCESS) {
  16935. ret = EVP_DigestUpdate(&md_ctx, e.input, (unsigned long)e.inLen);
  16936. if (ret == WOLFSSL_SUCCESS)
  16937. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  16938. }
  16939. EVP_MD_CTX_cleanup(&md_ctx);
  16940. if (ret != WOLFSSL_SUCCESS ||
  16941. XMEMCMP(hash, e.output, WC_SHA3_224_DIGEST_SIZE) != 0) {
  16942. return -8607;
  16943. }
  16944. #endif /* WOLFSSL_NOSHA3_224 */
  16945. #ifndef WOLFSSL_NOSHA3_256
  16946. d.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  16947. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  16948. d.output = "\x91\x6f\x60\x61\xfe\x87\x97\x41\xca\x64\x69\xb4\x39\x71\xdf"
  16949. "\xdb\x28\xb1\xa3\x2d\xc3\x6c\xb3\x25\x4e\x81\x2b\xe2\x7a\xad"
  16950. "\x1d\x18";
  16951. d.inLen = XSTRLEN(d.input);
  16952. d.outLen = WC_SHA3_256_DIGEST_SIZE;
  16953. EVP_MD_CTX_init(&md_ctx);
  16954. ret = EVP_DigestInit(&md_ctx, EVP_sha3_256());
  16955. if (ret == WOLFSSL_SUCCESS) {
  16956. ret = EVP_DigestUpdate(&md_ctx, d.input, (unsigned long)d.inLen);
  16957. if (ret == WOLFSSL_SUCCESS)
  16958. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  16959. }
  16960. EVP_MD_CTX_cleanup(&md_ctx);
  16961. if (ret != WOLFSSL_SUCCESS ||
  16962. XMEMCMP(hash, d.output, WC_SHA3_256_DIGEST_SIZE) != 0) {
  16963. return -8608;
  16964. }
  16965. #endif /* WOLFSSL_NOSHA3_256 */
  16966. e.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  16967. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  16968. e.output = "\x79\x40\x7d\x3b\x59\x16\xb5\x9c\x3e\x30\xb0\x98\x22\x97\x47"
  16969. "\x91\xc3\x13\xfb\x9e\xcc\x84\x9e\x40\x6f\x23\x59\x2d\x04\xf6"
  16970. "\x25\xdc\x8c\x70\x9b\x98\xb4\x3b\x38\x52\xb3\x37\x21\x61\x79"
  16971. "\xaa\x7f\xc7";
  16972. e.inLen = XSTRLEN(e.input);
  16973. e.outLen = WC_SHA3_384_DIGEST_SIZE;
  16974. EVP_MD_CTX_init(&md_ctx);
  16975. ret = EVP_DigestInit(&md_ctx, EVP_sha3_384());
  16976. if (ret == WOLFSSL_SUCCESS) {
  16977. ret = EVP_DigestUpdate(&md_ctx, e.input, (unsigned long)e.inLen);
  16978. if (ret == WOLFSSL_SUCCESS)
  16979. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  16980. }
  16981. EVP_MD_CTX_cleanup(&md_ctx);
  16982. if (ret != WOLFSSL_SUCCESS ||
  16983. XMEMCMP(hash, e.output, WC_SHA3_384_DIGEST_SIZE) != 0) {
  16984. return -8609;
  16985. }
  16986. #ifndef WOLFSSL_NOSHA3_512
  16987. f.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  16988. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  16989. f.output = "\xaf\xeb\xb2\xef\x54\x2e\x65\x79\xc5\x0c\xad\x06\xd2\xe5\x78"
  16990. "\xf9\xf8\xdd\x68\x81\xd7\xdc\x82\x4d\x26\x36\x0f\xee\xbf\x18"
  16991. "\xa4\xfa\x73\xe3\x26\x11\x22\x94\x8e\xfc\xfd\x49\x2e\x74\xe8"
  16992. "\x2e\x21\x89\xed\x0f\xb4\x40\xd1\x87\xf3\x82\x27\x0c\xb4\x55"
  16993. "\xf2\x1d\xd1\x85";
  16994. f.inLen = XSTRLEN(f.input);
  16995. f.outLen = WC_SHA3_512_DIGEST_SIZE;
  16996. EVP_MD_CTX_init(&md_ctx);
  16997. ret = EVP_DigestInit(&md_ctx, EVP_sha3_512());
  16998. if (ret == WOLFSSL_SUCCESS) {
  16999. ret = EVP_DigestUpdate(&md_ctx, f.input, (unsigned long)f.inLen);
  17000. if (ret == WOLFSSL_SUCCESS)
  17001. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  17002. }
  17003. EVP_MD_CTX_cleanup(&md_ctx);
  17004. if (ret != WOLFSSL_SUCCESS ||
  17005. XMEMCMP(hash, f.output, WC_SHA3_512_DIGEST_SIZE) != 0) {
  17006. return -8610;
  17007. }
  17008. #endif /* WOLFSSL_NOSHA3_512 */
  17009. #endif /* WOLFSSL_SHA3 */
  17010. #ifndef WC_NO_RNG
  17011. if (RAND_bytes(hash, sizeof(hash)) != WOLFSSL_SUCCESS)
  17012. return -8611;
  17013. #endif
  17014. #ifndef NO_MD5
  17015. c.input = "what do ya want for nothing?";
  17016. c.output = "\x55\x78\xe8\x48\x4b\xcc\x93\x80\x93\xec\x53\xaf\x22\xd6\x14"
  17017. "\x76";
  17018. c.inLen = XSTRLEN(c.input);
  17019. c.outLen = WC_MD5_DIGEST_SIZE;
  17020. #if defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)
  17021. /* Expect failure with MD5 + HMAC when using FIPS 140-3. */
  17022. if (HMAC(EVP_md5(), "JefeJefeJefeJefe", 16, (byte*)c.input, (int)c.inLen,
  17023. hash, 0) != NULL)
  17024. #else
  17025. if (HMAC(EVP_md5(), "JefeJefeJefeJefe", 16, (byte*)c.input, (int)c.inLen,
  17026. hash, 0) == NULL ||
  17027. XMEMCMP(hash, c.output, WC_MD5_DIGEST_SIZE) != 0)
  17028. #endif
  17029. {
  17030. return -8612;
  17031. }
  17032. #endif /* NO_MD5 */
  17033. #ifndef NO_DES3
  17034. { /* des test */
  17035. WOLFSSL_SMALL_STACK_STATIC const byte vector[] = { /* "now is the time for all " w/o trailing 0 */
  17036. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  17037. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  17038. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  17039. };
  17040. byte plain[24];
  17041. byte cipher[24];
  17042. const_DES_cblock key = {
  17043. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
  17044. };
  17045. DES_cblock iv = {
  17046. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef
  17047. };
  17048. DES_key_schedule sched;
  17049. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  17050. 0x8b,0x7c,0x52,0xb0,0x01,0x2b,0x6c,0xb8,
  17051. 0x4f,0x0f,0xeb,0xf3,0xfb,0x5f,0x86,0x73,
  17052. 0x15,0x85,0xb3,0x22,0x4b,0x86,0x2b,0x4b
  17053. };
  17054. DES_key_sched(&key, &sched);
  17055. DES_cbc_encrypt(vector, cipher, sizeof(vector), &sched, &iv, DES_ENCRYPT);
  17056. DES_cbc_encrypt(cipher, plain, sizeof(vector), &sched, &iv, DES_DECRYPT);
  17057. if (XMEMCMP(plain, vector, sizeof(vector)) != 0)
  17058. return -8613;
  17059. if (XMEMCMP(cipher, verify, sizeof(verify)) != 0)
  17060. return -8614;
  17061. /* test changing iv */
  17062. DES_ncbc_encrypt(vector, cipher, 8, &sched, &iv, DES_ENCRYPT);
  17063. DES_ncbc_encrypt(vector + 8, cipher + 8, 16, &sched, &iv, DES_ENCRYPT);
  17064. if (XMEMCMP(cipher, verify, sizeof(verify)) != 0)
  17065. return -8615;
  17066. } /* end des test */
  17067. #endif /* NO_DES3 */
  17068. #if !defined(NO_AES) && !defined(WOLFCRYPT_ONLY)
  17069. if (openssl_aes_test() != 0) {
  17070. return -8616;
  17071. }
  17072. #if defined(WOLFSSL_AES_128) && defined(HAVE_AES_CBC)
  17073. { /* evp_cipher test: EVP_aes_128_cbc */
  17074. #ifdef WOLFSSL_SMALL_STACK
  17075. EVP_CIPHER_CTX *ctx = wolfSSL_EVP_CIPHER_CTX_new();
  17076. #else
  17077. EVP_CIPHER_CTX ctx[1];
  17078. #endif
  17079. int idx, cipherSz, plainSz;
  17080. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = { /* "Now is the time for all " w/o trailing 0 */
  17081. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  17082. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  17083. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  17084. };
  17085. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  17086. 0x95,0x94,0x92,0x57,0x5f,0x42,0x81,0x53,
  17087. 0x2c,0xcc,0x9d,0x46,0x77,0xa2,0x33,0xcb,
  17088. 0x3b,0x5d,0x41,0x97,0x94,0x25,0xa4,0xb4,
  17089. 0xae,0x7b,0x34,0xd0,0x3f,0x0c,0xbc,0x06
  17090. };
  17091. WOLFSSL_SMALL_STACK_STATIC const byte verify2[] = {
  17092. 0x95,0x94,0x92,0x57,0x5f,0x42,0x81,0x53,
  17093. 0x2c,0xcc,0x9d,0x46,0x77,0xa2,0x33,0xcb,
  17094. 0x7d,0x37,0x7b,0x0b,0x44,0xaa,0xb5,0xf0,
  17095. 0x5f,0x34,0xb4,0xde,0xb5,0xbd,0x2a,0xbb
  17096. };
  17097. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  17098. "0123456789abcdef "; /* align */
  17099. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  17100. "1234567890abcdef "; /* align */
  17101. byte cipher[AES_BLOCK_SIZE * 4];
  17102. byte plain [AES_BLOCK_SIZE * 4];
  17103. #ifdef WOLFSSL_SMALL_STACK
  17104. if (ctx == NULL)
  17105. return MEMORY_E;
  17106. #endif
  17107. cipherSz = 0;
  17108. EVP_CIPHER_CTX_init(ctx);
  17109. ret = EVP_CipherInit(ctx, EVP_aes_128_cbc(), key, iv, 1);
  17110. if (ret == WOLFSSL_SUCCESS) {
  17111. ret = EVP_CipherUpdate(ctx, cipher, &idx, (byte*)msg, sizeof(msg));
  17112. if (ret == WOLFSSL_SUCCESS)
  17113. cipherSz += idx;
  17114. }
  17115. if (ret == WOLFSSL_SUCCESS) {
  17116. ret = EVP_CipherFinal(ctx, cipher + cipherSz, &idx);
  17117. if (ret == WOLFSSL_SUCCESS)
  17118. cipherSz += idx;
  17119. }
  17120. EVP_CIPHER_CTX_cleanup(ctx);
  17121. if (ret != WOLFSSL_SUCCESS)
  17122. return -8617;
  17123. if (cipherSz != (int)sizeof(verify) || XMEMCMP(cipher, verify, cipherSz))
  17124. return -8618;
  17125. /* check partial decrypt (not enough padding for full block) */
  17126. plainSz = 0;
  17127. EVP_CIPHER_CTX_init(ctx);
  17128. ret = EVP_CipherInit(ctx, EVP_aes_128_cbc(), key, iv, 0);
  17129. if (ret == WOLFSSL_SUCCESS) {
  17130. ret = EVP_CipherUpdate(ctx, plain, &idx, cipher, 1);
  17131. if (ret == WOLFSSL_SUCCESS)
  17132. plainSz += idx;
  17133. }
  17134. if (ret == WOLFSSL_SUCCESS) {
  17135. /* this test should fail... not enough padding for full block */
  17136. ret = EVP_CipherFinal(ctx, plain + plainSz, &idx);
  17137. if (plainSz == 0 && ret != WOLFSSL_SUCCESS)
  17138. ret = WOLFSSL_SUCCESS;
  17139. else
  17140. ret = -8619;
  17141. }
  17142. else
  17143. ret = -8620;
  17144. EVP_CIPHER_CTX_cleanup(ctx);
  17145. if (ret != WOLFSSL_SUCCESS)
  17146. return ret;
  17147. plainSz = 0;
  17148. EVP_CIPHER_CTX_init(ctx);
  17149. ret = EVP_CipherInit(ctx, EVP_aes_128_cbc(), key, iv, 0);
  17150. if (ret == WOLFSSL_SUCCESS) {
  17151. ret = EVP_CipherUpdate(ctx, plain, &idx, cipher, cipherSz);
  17152. if (ret == WOLFSSL_SUCCESS)
  17153. plainSz += idx;
  17154. }
  17155. if (ret == WOLFSSL_SUCCESS) {
  17156. ret = EVP_CipherFinal(ctx, plain + plainSz, &idx);
  17157. if (ret == WOLFSSL_SUCCESS)
  17158. plainSz += idx;
  17159. }
  17160. EVP_CIPHER_CTX_cleanup(ctx);
  17161. if (ret != WOLFSSL_SUCCESS)
  17162. return -8621;
  17163. if (plainSz != (int)sizeof(msg) || XMEMCMP(plain, msg, sizeof(msg)))
  17164. return -8622;
  17165. cipherSz = 0;
  17166. EVP_CIPHER_CTX_init(ctx);
  17167. ret = EVP_CipherInit(ctx, EVP_aes_128_cbc(), key, iv, 1);
  17168. if (ret == WOLFSSL_SUCCESS) {
  17169. ret = EVP_CipherUpdate(ctx, cipher, &idx, msg, AES_BLOCK_SIZE);
  17170. if (ret == WOLFSSL_SUCCESS)
  17171. cipherSz += idx;
  17172. }
  17173. if (ret == WOLFSSL_SUCCESS) {
  17174. ret = EVP_CipherFinal(ctx, cipher + cipherSz, &idx);
  17175. if (ret == WOLFSSL_SUCCESS)
  17176. cipherSz += idx;
  17177. }
  17178. EVP_CIPHER_CTX_cleanup(ctx);
  17179. if (ret != WOLFSSL_SUCCESS)
  17180. return -8623;
  17181. if (cipherSz != (int)sizeof(verify2) || XMEMCMP(cipher, verify2, cipherSz))
  17182. return -8624;
  17183. #ifdef WOLFSSL_SMALL_STACK
  17184. wolfSSL_EVP_CIPHER_CTX_free(ctx);
  17185. #endif
  17186. } /* end evp_cipher test: EVP_aes_128_cbc*/
  17187. #endif /* WOLFSSL_AES_128 && HAVE_AES_CBC */
  17188. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_256)
  17189. { /* evp_cipher test: EVP_aes_256_ecb*/
  17190. #ifdef WOLFSSL_SMALL_STACK
  17191. EVP_CIPHER_CTX *ctx = wolfSSL_EVP_CIPHER_CTX_new();
  17192. #else
  17193. EVP_CIPHER_CTX ctx[1];
  17194. #endif
  17195. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = {
  17196. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  17197. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  17198. };
  17199. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  17200. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  17201. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  17202. };
  17203. WOLFSSL_SMALL_STACK_STATIC const byte key[] = {
  17204. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  17205. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  17206. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  17207. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  17208. };
  17209. byte cipher[AES_BLOCK_SIZE * 4];
  17210. byte plain [AES_BLOCK_SIZE * 4];
  17211. #ifdef WOLFSSL_SMALL_STACK
  17212. if (ctx == NULL)
  17213. return MEMORY_E;
  17214. #endif
  17215. EVP_CIPHER_CTX_init(ctx);
  17216. ret = EVP_CipherInit(ctx, EVP_aes_256_ecb(), (unsigned char*)key, NULL, 1);
  17217. if (ret == WOLFSSL_SUCCESS)
  17218. ret = EVP_Cipher(ctx, cipher, (byte*)msg, 16);
  17219. EVP_CIPHER_CTX_cleanup(ctx);
  17220. if (ret != 16)
  17221. return -8625;
  17222. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE))
  17223. return -8626;
  17224. EVP_CIPHER_CTX_init(ctx);
  17225. ret = EVP_CipherInit(ctx, EVP_aes_256_ecb(), (unsigned char*)key, NULL, 0);
  17226. if (ret == WOLFSSL_SUCCESS)
  17227. ret = EVP_Cipher(ctx, plain, cipher, 16);
  17228. EVP_CIPHER_CTX_cleanup(ctx);
  17229. if (ret != 16)
  17230. return -8627;
  17231. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  17232. return -8628;
  17233. #ifdef WOLFSSL_SMALL_STACK
  17234. wolfSSL_EVP_CIPHER_CTX_free(ctx);
  17235. #endif
  17236. } /* end evp_cipher test */
  17237. #endif /* HAVE_AES_ECB && WOLFSSL_AES_128 */
  17238. #define OPENSSL_TEST_ERROR (-10000)
  17239. #if defined(WOLFSSL_AES_DIRECT) && defined(WOLFSSL_AES_256)
  17240. /* enable HAVE_AES_DECRYPT for AES_encrypt/decrypt */
  17241. {
  17242. /* Test: AES_encrypt/decrypt/set Key */
  17243. #ifdef WOLFSSL_SMALL_STACK
  17244. AES_KEY *enc = (AES_KEY *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  17245. #ifdef HAVE_AES_DECRYPT
  17246. AES_KEY *dec = (AES_KEY *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  17247. #endif
  17248. #else
  17249. AES_KEY enc[1];
  17250. #ifdef HAVE_AES_DECRYPT
  17251. AES_KEY dec[1];
  17252. #endif
  17253. #endif
  17254. WOLFSSL_SMALL_STACK_STATIC const byte msg[] =
  17255. {
  17256. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  17257. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  17258. };
  17259. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  17260. {
  17261. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  17262. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  17263. };
  17264. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  17265. {
  17266. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  17267. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  17268. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  17269. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  17270. };
  17271. byte plain[sizeof(msg)];
  17272. byte cipher[sizeof(msg)];
  17273. printf("openSSL extra test\n") ;
  17274. #ifdef WOLFSSL_SMALL_STACK
  17275. if (enc == NULL)
  17276. return MEMORY_E;
  17277. #ifdef HAVE_AES_DECRYPT
  17278. if (dec == NULL)
  17279. return MEMORY_E;
  17280. #endif
  17281. #endif
  17282. AES_set_encrypt_key(key, sizeof(key)*8, enc);
  17283. AES_set_decrypt_key(key, sizeof(key)*8, dec);
  17284. AES_encrypt(msg, cipher, enc);
  17285. #ifdef HAVE_AES_DECRYPT
  17286. AES_decrypt(cipher, plain, dec);
  17287. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  17288. return OPENSSL_TEST_ERROR-60;
  17289. #endif /* HAVE_AES_DECRYPT */
  17290. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE))
  17291. return OPENSSL_TEST_ERROR-61;
  17292. #ifdef WOLFSSL_SMALL_STACK
  17293. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  17294. #ifdef HAVE_AES_DECRYPT
  17295. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  17296. #endif
  17297. #endif
  17298. }
  17299. #endif /* WOLFSSL_AES_DIRECT && WOLFSSL_AES_256 */
  17300. /* EVP_Cipher with EVP_aes_xxx_ctr() */
  17301. #ifdef WOLFSSL_AES_COUNTER
  17302. {
  17303. byte plainBuff [64];
  17304. byte cipherBuff[64];
  17305. #ifdef WOLFSSL_AES_128
  17306. WOLFSSL_SMALL_STACK_STATIC const byte ctrKey[] =
  17307. {
  17308. 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,
  17309. 0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c
  17310. };
  17311. WOLFSSL_SMALL_STACK_STATIC const byte ctrIv[] =
  17312. {
  17313. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  17314. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  17315. };
  17316. WOLFSSL_SMALL_STACK_STATIC const byte ctrPlain[] =
  17317. {
  17318. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  17319. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  17320. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  17321. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  17322. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  17323. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  17324. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  17325. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  17326. };
  17327. WOLFSSL_SMALL_STACK_STATIC const byte ctrCipher[] =
  17328. {
  17329. 0x87,0x4d,0x61,0x91,0xb6,0x20,0xe3,0x26,
  17330. 0x1b,0xef,0x68,0x64,0x99,0x0d,0xb6,0xce,
  17331. 0x98,0x06,0xf6,0x6b,0x79,0x70,0xfd,0xff,
  17332. 0x86,0x17,0x18,0x7b,0xb9,0xff,0xfd,0xff,
  17333. 0x5a,0xe4,0xdf,0x3e,0xdb,0xd5,0xd3,0x5e,
  17334. 0x5b,0x4f,0x09,0x02,0x0d,0xb0,0x3e,0xab,
  17335. 0x1e,0x03,0x1d,0xda,0x2f,0xbe,0x03,0xd1,
  17336. 0x79,0x21,0x70,0xa0,0xf3,0x00,0x9c,0xee
  17337. };
  17338. WOLFSSL_SMALL_STACK_STATIC const byte oddCipher[] =
  17339. {
  17340. 0xb9,0xd7,0xcb,0x08,0xb0,0xe1,0x7b,0xa0,
  17341. 0xc2
  17342. };
  17343. #endif /* WOLFSSL_AES_128 */
  17344. #ifdef WOLFSSL_AES_192
  17345. /* test vector from "Recommendation for Block Cipher Modes of Operation"
  17346. * NIST Special Publication 800-38A */
  17347. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Key[] =
  17348. {
  17349. 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  17350. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  17351. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b
  17352. };
  17353. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Iv[] =
  17354. {
  17355. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  17356. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  17357. };
  17358. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Plain[] =
  17359. {
  17360. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  17361. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  17362. };
  17363. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Cipher[] =
  17364. {
  17365. 0x1a,0xbc,0x93,0x24,0x17,0x52,0x1c,0xa2,
  17366. 0x4f,0x2b,0x04,0x59,0xfe,0x7e,0x6e,0x0b
  17367. };
  17368. #endif /* WOLFSSL_AES_192 */
  17369. #ifdef WOLFSSL_AES_256
  17370. /* test vector from "Recommendation for Block Cipher Modes of Operation"
  17371. * NIST Special Publication 800-38A */
  17372. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Key[] =
  17373. {
  17374. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  17375. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  17376. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  17377. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  17378. };
  17379. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Iv[] =
  17380. {
  17381. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  17382. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  17383. };
  17384. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Plain[] =
  17385. {
  17386. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  17387. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  17388. };
  17389. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Cipher[] =
  17390. {
  17391. 0x60,0x1e,0xc3,0x13,0x77,0x57,0x89,0xa5,
  17392. 0xb7,0xa7,0xf5,0x04,0xbb,0xf3,0xd2,0x28
  17393. };
  17394. #endif /* WOLFSSL_AES_256 */
  17395. #ifdef WOLFSSL_SMALL_STACK
  17396. EVP_CIPHER_CTX *en = wolfSSL_EVP_CIPHER_CTX_new();
  17397. EVP_CIPHER_CTX *de = wolfSSL_EVP_CIPHER_CTX_new();
  17398. if ((en == NULL) || (de == NULL))
  17399. return MEMORY_E;
  17400. #else
  17401. EVP_CIPHER_CTX en[1];
  17402. EVP_CIPHER_CTX de[1];
  17403. #endif
  17404. #ifdef WOLFSSL_AES_128
  17405. #ifndef WOLFSSL_SMALL_STACK
  17406. EVP_CIPHER_CTX *p_en;
  17407. EVP_CIPHER_CTX *p_de;
  17408. #endif
  17409. EVP_CIPHER_CTX_init(en);
  17410. if (EVP_CipherInit(en, EVP_aes_128_ctr(),
  17411. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  17412. return -8629;
  17413. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctrPlain,
  17414. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  17415. return -8630;
  17416. EVP_CIPHER_CTX_init(de);
  17417. if (EVP_CipherInit(de, EVP_aes_128_ctr(),
  17418. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  17419. return -8631;
  17420. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff,
  17421. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  17422. return -8632;
  17423. if (XMEMCMP(cipherBuff, ctrCipher, AES_BLOCK_SIZE*4))
  17424. return -8633;
  17425. if (XMEMCMP(plainBuff, ctrPlain, AES_BLOCK_SIZE*4))
  17426. return -8634;
  17427. #ifndef WOLFSSL_SMALL_STACK
  17428. p_en = wolfSSL_EVP_CIPHER_CTX_new();
  17429. if(p_en == NULL)return -8635;
  17430. p_de = wolfSSL_EVP_CIPHER_CTX_new();
  17431. if(p_de == NULL)return -8636;
  17432. if (EVP_CipherInit(p_en, EVP_aes_128_ctr(),
  17433. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  17434. return -8637;
  17435. if (EVP_Cipher(p_en, (byte*)cipherBuff, (byte*)ctrPlain,
  17436. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  17437. return -8638;
  17438. if (EVP_CipherInit(p_de, EVP_aes_128_ctr(),
  17439. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  17440. return -8639;
  17441. if (EVP_Cipher(p_de, (byte*)plainBuff, (byte*)cipherBuff,
  17442. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  17443. return -8640;
  17444. wolfSSL_EVP_CIPHER_CTX_free(p_en);
  17445. wolfSSL_EVP_CIPHER_CTX_free(p_de);
  17446. #endif /* !WOLFSSL_SMALL_STACK */
  17447. if (XMEMCMP(cipherBuff, ctrCipher, AES_BLOCK_SIZE*4))
  17448. return -8641;
  17449. if (XMEMCMP(plainBuff, ctrPlain, AES_BLOCK_SIZE*4))
  17450. return -8642;
  17451. EVP_CIPHER_CTX_init(en);
  17452. if (EVP_CipherInit(en, EVP_aes_128_ctr(),
  17453. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  17454. return -8643;
  17455. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctrPlain, 9) != 9)
  17456. return -8644;
  17457. EVP_CIPHER_CTX_init(de);
  17458. if (EVP_CipherInit(de, EVP_aes_128_ctr(),
  17459. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  17460. return -8645;
  17461. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff, 9) != 9)
  17462. return -8646;
  17463. if (XMEMCMP(plainBuff, ctrPlain, 9))
  17464. return -8647;
  17465. if (XMEMCMP(cipherBuff, ctrCipher, 9))
  17466. return -8648;
  17467. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctrPlain, 9) != 9)
  17468. return -8649;
  17469. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff, 9) != 9)
  17470. return -8650;
  17471. if (XMEMCMP(plainBuff, ctrPlain, 9))
  17472. return -8651;
  17473. if (XMEMCMP(cipherBuff, oddCipher, 9))
  17474. return -8652;
  17475. #endif /* WOLFSSL_AES_128 */
  17476. #ifdef WOLFSSL_AES_192
  17477. EVP_CIPHER_CTX_init(en);
  17478. if (EVP_CipherInit(en, EVP_aes_192_ctr(),
  17479. (unsigned char*)ctr192Key, (unsigned char*)ctr192Iv, 0) == 0)
  17480. return -8653;
  17481. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctr192Plain,
  17482. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  17483. return -8654;
  17484. EVP_CIPHER_CTX_init(de);
  17485. if (EVP_CipherInit(de, EVP_aes_192_ctr(),
  17486. (unsigned char*)ctr192Key, (unsigned char*)ctr192Iv, 0) == 0)
  17487. return -8655;
  17488. XMEMSET(plainBuff, 0, sizeof(plainBuff));
  17489. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff,
  17490. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  17491. return -8656;
  17492. if (XMEMCMP(plainBuff, ctr192Plain, sizeof(ctr192Plain)))
  17493. return -8657;
  17494. if (XMEMCMP(ctr192Cipher, cipherBuff, sizeof(ctr192Cipher)))
  17495. return -8658;
  17496. #endif /* WOLFSSL_AES_192 */
  17497. #ifdef WOLFSSL_AES_256
  17498. EVP_CIPHER_CTX_init(en);
  17499. if (EVP_CipherInit(en, EVP_aes_256_ctr(),
  17500. (unsigned char*)ctr256Key, (unsigned char*)ctr256Iv, 0) == 0)
  17501. return -8659;
  17502. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctr256Plain,
  17503. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  17504. return -8660;
  17505. EVP_CIPHER_CTX_init(de);
  17506. if (EVP_CipherInit(de, EVP_aes_256_ctr(),
  17507. (unsigned char*)ctr256Key, (unsigned char*)ctr256Iv, 0) == 0)
  17508. return -8661;
  17509. XMEMSET(plainBuff, 0, sizeof(plainBuff));
  17510. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff,
  17511. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  17512. return -8662;
  17513. if (XMEMCMP(plainBuff, ctr256Plain, sizeof(ctr256Plain)))
  17514. return -8663;
  17515. if (XMEMCMP(ctr256Cipher, cipherBuff, sizeof(ctr256Cipher)))
  17516. return -8664;
  17517. #endif /* WOLFSSL_AES_256 */
  17518. #ifdef WOLFSSL_SMALL_STACK
  17519. wolfSSL_EVP_CIPHER_CTX_free(en);
  17520. wolfSSL_EVP_CIPHER_CTX_free(de);
  17521. #endif
  17522. }
  17523. #endif /* HAVE_AES_COUNTER */
  17524. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  17525. {
  17526. /* EVP_CipherUpdate test */
  17527. WOLFSSL_SMALL_STACK_STATIC const byte cbcPlain[] =
  17528. {
  17529. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  17530. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  17531. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  17532. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  17533. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  17534. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  17535. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  17536. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  17537. };
  17538. byte key[] = "0123456789abcdef "; /* align */
  17539. byte iv[] = "1234567890abcdef "; /* align */
  17540. byte cipher[AES_BLOCK_SIZE * 4];
  17541. byte plain [AES_BLOCK_SIZE * 4];
  17542. #ifdef WOLFSSL_SMALL_STACK
  17543. EVP_CIPHER_CTX *en = wolfSSL_EVP_CIPHER_CTX_new();
  17544. EVP_CIPHER_CTX *de = wolfSSL_EVP_CIPHER_CTX_new();
  17545. #else
  17546. EVP_CIPHER_CTX en[1];
  17547. EVP_CIPHER_CTX de[1];
  17548. #endif
  17549. int outlen ;
  17550. int total = 0;
  17551. #ifdef WOLFSSL_SMALL_STACK
  17552. if ((en == NULL) || (de == NULL))
  17553. return MEMORY_E;
  17554. #endif
  17555. EVP_CIPHER_CTX_init(en);
  17556. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  17557. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  17558. return -8665;
  17559. /* openSSL compatibility, if(inlen == 0)return 1; */
  17560. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen,
  17561. (byte*)cbcPlain, 0) != 1)
  17562. return -8666;
  17563. EVP_CIPHER_CTX_init(en);
  17564. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  17565. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  17566. return -8667;
  17567. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen,
  17568. (byte*)cbcPlain, 9) == 0)
  17569. return -8668;
  17570. if(outlen != 0)
  17571. return -8669;
  17572. total += outlen;
  17573. if (EVP_CipherUpdate(en, (byte*)&cipher[total], &outlen,
  17574. (byte*)&cbcPlain[9] , 9) == 0)
  17575. return -8670;
  17576. if(outlen != 16)
  17577. return -8671;
  17578. total += outlen;
  17579. if (EVP_CipherFinal(en, (byte*)&cipher[total], &outlen) == 0)
  17580. return -8672;
  17581. if(outlen != 16)
  17582. return -8673;
  17583. total += outlen;
  17584. if(total != 32)
  17585. return -8674;
  17586. total = 0;
  17587. EVP_CIPHER_CTX_init(de);
  17588. if (EVP_CipherInit(de, EVP_aes_128_cbc(),
  17589. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  17590. return -8675;
  17591. if (EVP_CipherUpdate(de, (byte*)plain, &outlen, (byte*)cipher, 6) == 0)
  17592. return -8676;
  17593. if(outlen != 0)
  17594. return -8677;
  17595. total += outlen;
  17596. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen,
  17597. (byte*)&cipher[6], 12) == 0)
  17598. return -8678;
  17599. if(outlen != 0)
  17600. total += outlen;
  17601. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen,
  17602. (byte*)&cipher[6+12], 14) == 0)
  17603. return -8679;
  17604. if(outlen != 16)
  17605. return -8680;
  17606. total += outlen;
  17607. if (EVP_CipherFinal(de, (byte*)&plain[total], &outlen) == 0)
  17608. return -8681;
  17609. if(outlen != 2)
  17610. return -8682;
  17611. total += outlen;
  17612. if(total != 18)
  17613. return -8683;
  17614. if (XMEMCMP(plain, cbcPlain, 18))
  17615. return -8684;
  17616. total = 0;
  17617. EVP_CIPHER_CTX_init(en);
  17618. if (EVP_EncryptInit(en, EVP_aes_128_cbc(),
  17619. (unsigned char*)key, (unsigned char*)iv) == 0)
  17620. return -8685;
  17621. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen, (byte*)cbcPlain, 9) == 0)
  17622. return -8686;
  17623. if(outlen != 0)
  17624. return -8687;
  17625. total += outlen;
  17626. if (EVP_CipherUpdate(en, (byte*)&cipher[total], &outlen, (byte*)&cbcPlain[9] , 9) == 0)
  17627. return -8688;
  17628. if(outlen != 16)
  17629. return -8689;
  17630. total += outlen;
  17631. if (EVP_EncryptFinal(en, (byte*)&cipher[total], &outlen) == 0)
  17632. return -8690;
  17633. if(outlen != 16)
  17634. return -8691;
  17635. total += outlen;
  17636. if(total != 32)
  17637. return 3438;
  17638. total = 0;
  17639. EVP_CIPHER_CTX_init(de);
  17640. if (EVP_DecryptInit(de, EVP_aes_128_cbc(),
  17641. (unsigned char*)key, (unsigned char*)iv) == 0)
  17642. return -8692;
  17643. if (EVP_CipherUpdate(de, (byte*)plain, &outlen, (byte*)cipher, 6) == 0)
  17644. return -8693;
  17645. if(outlen != 0)
  17646. return -8694;
  17647. total += outlen;
  17648. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen, (byte*)&cipher[6], 12) == 0)
  17649. return -8695;
  17650. if(outlen != 0)
  17651. total += outlen;
  17652. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen, (byte*)&cipher[6+12], 14) == 0)
  17653. return -8696;
  17654. if(outlen != 16)
  17655. return -8697;
  17656. total += outlen;
  17657. if (EVP_DecryptFinal(de, (byte*)&plain[total], &outlen) == 0)
  17658. return -8698;
  17659. if(outlen != 2)
  17660. return -8699;
  17661. total += outlen;
  17662. if(total != 18)
  17663. return 3447;
  17664. if (XMEMCMP(plain, cbcPlain, 18))
  17665. return -8700;
  17666. if (EVP_CIPHER_key_length(NULL) != 0)
  17667. return -8701;
  17668. if (EVP_CIPHER_key_length(EVP_aes_128_cbc()) != 16)
  17669. return -8702;
  17670. if (EVP_CIPHER_CTX_mode(NULL) != 0)
  17671. return -8703;
  17672. if (EVP_CIPHER_CTX_mode(en) != (en->flags & WOLFSSL_EVP_CIPH_MODE))
  17673. return -8704;
  17674. EVP_CIPHER_CTX_init(en);
  17675. if (EVP_CipherInit_ex(en, EVP_aes_128_cbc(), NULL,
  17676. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  17677. return -8705;
  17678. EVP_CIPHER_CTX_init(en);
  17679. if (EVP_EncryptInit_ex(en, EVP_aes_128_cbc(), NULL,
  17680. (unsigned char*)key, (unsigned char*)iv) == 0)
  17681. return -8706;
  17682. if (wolfSSL_EVP_EncryptFinal_ex(NULL, NULL, NULL) != WOLFSSL_FAILURE)
  17683. return -8707;
  17684. if (wolfSSL_EVP_EncryptFinal(NULL, NULL, NULL) != WOLFSSL_FAILURE)
  17685. return -8708;
  17686. EVP_CIPHER_CTX_init(de);
  17687. if (EVP_DecryptInit_ex(de, EVP_aes_128_cbc(), NULL,
  17688. (unsigned char*)key, (unsigned char*)iv) == 0)
  17689. return -8709;
  17690. if (wolfSSL_EVP_DecryptFinal(NULL, NULL, NULL) != WOLFSSL_FAILURE)
  17691. return -8710;
  17692. if (wolfSSL_EVP_DecryptFinal_ex(NULL, NULL, NULL) != WOLFSSL_FAILURE)
  17693. return -8711;
  17694. if (EVP_CIPHER_CTX_block_size(NULL) != BAD_FUNC_ARG)
  17695. return -8712;
  17696. EVP_CIPHER_CTX_init(en);
  17697. EVP_EncryptInit_ex(en, EVP_aes_128_cbc(), NULL,
  17698. (unsigned char*)key, (unsigned char*)iv);
  17699. if (EVP_CIPHER_CTX_block_size(en) != en->block_size)
  17700. return -8713;
  17701. if (EVP_CIPHER_block_size(NULL) != BAD_FUNC_ARG)
  17702. return -8714;
  17703. if (EVP_CIPHER_block_size(EVP_aes_128_cbc()) != AES_BLOCK_SIZE)
  17704. return -8715;
  17705. if (WOLFSSL_EVP_CIPHER_mode(NULL) != 0)
  17706. return -8716;
  17707. if (EVP_CIPHER_flags(EVP_aes_128_cbc()) != WOLFSSL_EVP_CIPH_CBC_MODE)
  17708. return -8717;
  17709. EVP_CIPHER_CTX_clear_flags(en, 0xFFFFFFFF);
  17710. EVP_CIPHER_CTX_set_flags(en, 42);
  17711. if (en->flags != 42)
  17712. return -8718;
  17713. if (EVP_CIPHER_CTX_set_padding(NULL, 0) != BAD_FUNC_ARG)
  17714. return -8719;
  17715. if (EVP_CIPHER_CTX_set_padding(en, 0) != WOLFSSL_SUCCESS)
  17716. return -8720;
  17717. if (EVP_CIPHER_CTX_set_padding(en, 1) != WOLFSSL_SUCCESS)
  17718. return -8721;
  17719. #ifdef WOLFSSL_SMALL_STACK
  17720. wolfSSL_EVP_CIPHER_CTX_free(en);
  17721. wolfSSL_EVP_CIPHER_CTX_free(de);
  17722. #endif
  17723. }
  17724. #endif /* WOLFSSL_AES_128 && HAVE_AES_CBC */
  17725. #endif /* ifndef NO_AES */
  17726. return 0;
  17727. }
  17728. WOLFSSL_TEST_SUBROUTINE int openSSL_evpMD_test(void)
  17729. {
  17730. int ret = 0;
  17731. #if !defined(NO_SHA256) && !defined(NO_SHA)
  17732. WOLFSSL_EVP_MD_CTX* ctx;
  17733. WOLFSSL_EVP_MD_CTX* ctx2;
  17734. ctx = EVP_MD_CTX_create();
  17735. ctx2 = EVP_MD_CTX_create();
  17736. ret = EVP_DigestInit(ctx, EVP_sha256());
  17737. if (ret != SSL_SUCCESS) {
  17738. ret = -8800;
  17739. goto openSSL_evpMD_test_done;
  17740. }
  17741. ret = EVP_MD_CTX_copy(ctx2, ctx);
  17742. if (ret != SSL_SUCCESS) {
  17743. ret = -8801;
  17744. goto openSSL_evpMD_test_done;
  17745. }
  17746. if (EVP_MD_type(EVP_sha256()) != EVP_MD_CTX_type(ctx2)) {
  17747. ret = -8802;
  17748. goto openSSL_evpMD_test_done;
  17749. }
  17750. ret = EVP_DigestInit(ctx, EVP_sha1());
  17751. if (ret != SSL_SUCCESS) {
  17752. ret = -8803;
  17753. goto openSSL_evpMD_test_done;
  17754. }
  17755. if (EVP_MD_type(EVP_sha256()) != EVP_MD_CTX_type(ctx2)) {
  17756. ret = -8804;
  17757. goto openSSL_evpMD_test_done;
  17758. }
  17759. ret = EVP_MD_CTX_copy_ex(ctx2, ctx);
  17760. if (ret != SSL_SUCCESS) {
  17761. ret = -8805;
  17762. goto openSSL_evpMD_test_done;
  17763. }
  17764. if (EVP_MD_type(EVP_sha256()) == EVP_MD_CTX_type(ctx2)) {
  17765. ret = -8806;
  17766. goto openSSL_evpMD_test_done;
  17767. }
  17768. if (EVP_MD_type(EVP_sha1()) != EVP_MD_CTX_type(ctx2)) {
  17769. ret = -8807;
  17770. goto openSSL_evpMD_test_done;
  17771. }
  17772. if (EVP_DigestInit_ex(ctx, EVP_sha1(), NULL) != SSL_SUCCESS) {
  17773. ret = -8808;
  17774. goto openSSL_evpMD_test_done;
  17775. }
  17776. if (EVP_add_digest(NULL) != 0) {
  17777. ret = -8809;
  17778. goto openSSL_evpMD_test_done;
  17779. }
  17780. if (wolfSSL_EVP_add_cipher(NULL) != 0) {
  17781. ret = -8810;
  17782. goto openSSL_evpMD_test_done;
  17783. }
  17784. ret = 0; /* got to success state without jumping to end with a fail */
  17785. openSSL_evpMD_test_done:
  17786. EVP_MD_CTX_destroy(ctx);
  17787. EVP_MD_CTX_destroy(ctx2);
  17788. #endif /* NO_SHA256 */
  17789. return ret;
  17790. }
  17791. #ifdef DEBUG_SIGN
  17792. static void show(const char *title, const char *p, unsigned int s) {
  17793. char* i;
  17794. printf("%s: ", title);
  17795. for (i = p;
  17796. i < p + s;
  17797. printf("%c", *i), i++);
  17798. printf("\n");
  17799. }
  17800. #else
  17801. #define show(a,b,c)
  17802. #endif
  17803. #define FOURK_BUFF 4096
  17804. #define ERR_BASE_PKEY (-5000)
  17805. WOLFSSL_TEST_SUBROUTINE int openssl_pkey0_test(void)
  17806. {
  17807. int ret = 0;
  17808. #if !defined(NO_RSA) && !defined(HAVE_USER_RSA) && !defined(NO_SHA)
  17809. byte* prvTmp;
  17810. byte* pubTmp;
  17811. int prvBytes;
  17812. int pubBytes;
  17813. RSA *prvRsa = NULL;
  17814. RSA *pubRsa = NULL;
  17815. EVP_PKEY *prvPkey = NULL;
  17816. EVP_PKEY *pubPkey = NULL;
  17817. EVP_PKEY_CTX *enc = NULL;
  17818. EVP_PKEY_CTX *dec = NULL;
  17819. byte in[] = TEST_STRING;
  17820. byte out[256];
  17821. size_t outlen;
  17822. size_t keySz;
  17823. byte plain[256];
  17824. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  17825. XFILE keyFile;
  17826. XFILE keypubFile;
  17827. char cliKey[] = "./certs/client-key.der";
  17828. char cliKeypub[] = "./certs/client-keyPub.der";
  17829. #endif
  17830. prvTmp = (byte*)XMALLOC(FOURK_BUFF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17831. if (prvTmp == NULL)
  17832. return ERR_BASE_PKEY-1;
  17833. pubTmp = (byte*)XMALLOC(FOURK_BUFF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17834. if (pubTmp == NULL) {
  17835. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  17836. return ERR_BASE_PKEY-2;
  17837. }
  17838. #ifdef USE_CERT_BUFFERS_1024
  17839. XMEMCPY(prvTmp, client_key_der_1024, sizeof_client_key_der_1024);
  17840. prvBytes = sizeof_client_key_der_1024;
  17841. XMEMCPY(pubTmp, client_keypub_der_1024, sizeof_client_keypub_der_1024);
  17842. pubBytes = sizeof_client_keypub_der_1024;
  17843. #elif defined(USE_CERT_BUFFERS_2048)
  17844. XMEMCPY(prvTmp, client_key_der_2048, sizeof_client_key_der_2048);
  17845. prvBytes = sizeof_client_key_der_2048;
  17846. XMEMCPY(pubTmp, client_keypub_der_2048, sizeof_client_keypub_der_2048);
  17847. pubBytes = sizeof_client_keypub_der_2048;
  17848. #else
  17849. keyFile = XFOPEN(cliKey, "rb");
  17850. if (!keyFile) {
  17851. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  17852. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  17853. err_sys("can't open ./certs/client-key.der, "
  17854. "Please run from wolfSSL home dir", ERR_BASE_PKEY-3);
  17855. return ERR_BASE_PKEY-3;
  17856. }
  17857. prvBytes = (int)XFREAD(prvTmp, 1, (int)FOURK_BUFF, keyFile);
  17858. XFCLOSE(keyFile);
  17859. keypubFile = XFOPEN(cliKeypub, "rb");
  17860. if (!keypubFile) {
  17861. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  17862. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  17863. err_sys("can't open ./certs/client-cert.der, "
  17864. "Please run from wolfSSL home dir", -4);
  17865. return ERR_BASE_PKEY-4;
  17866. }
  17867. pubBytes = (int)XFREAD(pubTmp, 1, (int)FOURK_BUFF, keypubFile);
  17868. XFCLOSE(keypubFile);
  17869. #endif /* USE_CERT_BUFFERS */
  17870. prvRsa = wolfSSL_RSA_new();
  17871. pubRsa = wolfSSL_RSA_new();
  17872. if((prvRsa == NULL) || (pubRsa == NULL)){
  17873. printf("error with RSA_new\n");
  17874. ret = ERR_BASE_PKEY-10;
  17875. goto openssl_pkey0_test_done;
  17876. }
  17877. ret = wolfSSL_RSA_LoadDer_ex(prvRsa, prvTmp, prvBytes, WOLFSSL_RSA_LOAD_PRIVATE);
  17878. if(ret != SSL_SUCCESS){
  17879. printf("error with RSA_LoadDer_ex\n");
  17880. ret = ERR_BASE_PKEY-11;
  17881. goto openssl_pkey0_test_done;
  17882. }
  17883. ret = wolfSSL_RSA_LoadDer_ex(pubRsa, pubTmp, pubBytes, WOLFSSL_RSA_LOAD_PUBLIC);
  17884. if(ret != SSL_SUCCESS){
  17885. printf("error with RSA_LoadDer_ex\n");
  17886. ret = ERR_BASE_PKEY-12;
  17887. goto openssl_pkey0_test_done;
  17888. }
  17889. keySz = (size_t)RSA_size(pubRsa);
  17890. prvPkey = wolfSSL_EVP_PKEY_new();
  17891. pubPkey = wolfSSL_EVP_PKEY_new();
  17892. if((prvPkey == NULL) || (pubPkey == NULL)){
  17893. printf("error with PKEY_new\n");
  17894. ret = ERR_BASE_PKEY-13;
  17895. goto openssl_pkey0_test_done;
  17896. }
  17897. ret = wolfSSL_EVP_PKEY_set1_RSA(prvPkey, prvRsa);
  17898. ret += wolfSSL_EVP_PKEY_set1_RSA(pubPkey, pubRsa);
  17899. if(ret != 2){
  17900. printf("error with PKEY_set1_RSA\n");
  17901. ret = ERR_BASE_PKEY-14;
  17902. goto openssl_pkey0_test_done;
  17903. }
  17904. dec = EVP_PKEY_CTX_new(prvPkey, NULL);
  17905. enc = EVP_PKEY_CTX_new(pubPkey, NULL);
  17906. if((dec == NULL)||(enc==NULL)){
  17907. printf("error with EVP_PKEY_CTX_new\n");
  17908. ret = ERR_BASE_PKEY-15;
  17909. goto openssl_pkey0_test_done;
  17910. }
  17911. ret = EVP_PKEY_decrypt_init(dec);
  17912. if (ret != 1) {
  17913. printf("error with decrypt init\n");
  17914. ret = ERR_BASE_PKEY-16;
  17915. goto openssl_pkey0_test_done;
  17916. }
  17917. ret = EVP_PKEY_encrypt_init(enc);
  17918. if (ret != 1) {
  17919. printf("error with encrypt init\n");
  17920. ret = ERR_BASE_PKEY-17;
  17921. goto openssl_pkey0_test_done;
  17922. }
  17923. XMEMSET(out, 0, sizeof(out));
  17924. ret = EVP_PKEY_encrypt(enc, out, &outlen, in, sizeof(in));
  17925. if (ret != 1) {
  17926. printf("error encrypting msg\n");
  17927. ret = ERR_BASE_PKEY-18;
  17928. goto openssl_pkey0_test_done;
  17929. }
  17930. show("encrypted msg", out, outlen);
  17931. XMEMSET(plain, 0, sizeof(plain));
  17932. ret = EVP_PKEY_decrypt(dec, plain, &outlen, out, keySz);
  17933. if (ret != 1) {
  17934. printf("error decrypting msg\n");
  17935. ret = ERR_BASE_PKEY-19;
  17936. goto openssl_pkey0_test_done;
  17937. }
  17938. show("decrypted msg", plain, outlen);
  17939. /* RSA_PKCS1_OAEP_PADDING test */
  17940. ret = EVP_PKEY_decrypt_init(dec);
  17941. if (ret != 1) {
  17942. printf("error with decrypt init\n");
  17943. ret = ERR_BASE_PKEY-30;
  17944. goto openssl_pkey0_test_done;
  17945. }
  17946. ret = EVP_PKEY_encrypt_init(enc);
  17947. if (ret != 1) {
  17948. printf("error with encrypt init\n");
  17949. ret = ERR_BASE_PKEY-31;
  17950. goto openssl_pkey0_test_done;
  17951. }
  17952. if (EVP_PKEY_CTX_set_rsa_padding(dec, RSA_PKCS1_PADDING) <= 0) {
  17953. printf("first set rsa padding error\n");
  17954. ret = ERR_BASE_PKEY-32;
  17955. goto openssl_pkey0_test_done;
  17956. }
  17957. #ifndef HAVE_FIPS
  17958. if (EVP_PKEY_CTX_set_rsa_padding(dec, RSA_PKCS1_OAEP_PADDING) <= 0){
  17959. printf("second set rsa padding error\n");
  17960. ret = ERR_BASE_PKEY-33;
  17961. goto openssl_pkey0_test_done;
  17962. }
  17963. if (EVP_PKEY_CTX_set_rsa_padding(enc, RSA_PKCS1_OAEP_PADDING) <= 0) {
  17964. printf("third set rsa padding error\n");
  17965. ret = ERR_BASE_PKEY-34;
  17966. goto openssl_pkey0_test_done;
  17967. }
  17968. #endif
  17969. XMEMSET(out, 0, sizeof(out));
  17970. ret = EVP_PKEY_encrypt(enc, out, &outlen, in, sizeof(in));
  17971. if (ret != 1) {
  17972. printf("error encrypting msg\n");
  17973. ret = ERR_BASE_PKEY-35;
  17974. goto openssl_pkey0_test_done;
  17975. }
  17976. show("encrypted msg", out, outlen);
  17977. XMEMSET(plain, 0, sizeof(plain));
  17978. ret = EVP_PKEY_decrypt(dec, plain, &outlen, out, keySz);
  17979. if (ret != 1) {
  17980. printf("error decrypting msg\n");
  17981. ret = ERR_BASE_PKEY-36;
  17982. goto openssl_pkey0_test_done;
  17983. }
  17984. show("decrypted msg", plain, outlen);
  17985. ret = 0; /* made it to this point without error then set success */
  17986. openssl_pkey0_test_done:
  17987. wolfSSL_RSA_free(prvRsa);
  17988. wolfSSL_RSA_free(pubRsa);
  17989. EVP_PKEY_free(pubPkey);
  17990. EVP_PKEY_free(prvPkey);
  17991. EVP_PKEY_CTX_free(dec);
  17992. EVP_PKEY_CTX_free(enc);
  17993. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17994. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17995. #endif /* NO_RSA */
  17996. return ret;
  17997. }
  17998. WOLFSSL_TEST_SUBROUTINE int openssl_pkey1_test(void)
  17999. {
  18000. int ret = 0;
  18001. #if !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(HAVE_USER_RSA) && \
  18002. !defined(NO_SHA)
  18003. EVP_PKEY_CTX* dec = NULL;
  18004. EVP_PKEY_CTX* enc = NULL;
  18005. EVP_PKEY* pubKey = NULL;
  18006. EVP_PKEY* prvKey = NULL;
  18007. X509* x509 = NULL;
  18008. WOLFSSL_SMALL_STACK_STATIC const unsigned char msg[] = "sugar slapped";
  18009. const unsigned char* clikey;
  18010. long cliKeySz;
  18011. size_t outlen;
  18012. int keyLenBits = 2048;
  18013. #ifdef WOLFSSL_SMALL_STACK
  18014. unsigned char *tmp = (unsigned char *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18015. unsigned char *cipher = (unsigned char *)XMALLOC(RSA_TEST_BYTES, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18016. unsigned char *plain = (unsigned char *)XMALLOC(RSA_TEST_BYTES, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18017. if ((tmp == NULL) ||
  18018. (cipher == NULL) ||
  18019. (plain == NULL)) {
  18020. ret = -9015;
  18021. goto openssl_pkey1_test_done;
  18022. }
  18023. #else
  18024. unsigned char tmp[FOURK_BUF];
  18025. unsigned char cipher[RSA_TEST_BYTES];
  18026. unsigned char plain[RSA_TEST_BYTES];
  18027. #endif
  18028. #if defined(USE_CERT_BUFFERS_1024)
  18029. XMEMCPY(tmp, client_key_der_1024, sizeof_client_key_der_1024);
  18030. cliKeySz = (long)sizeof_client_key_der_1024;
  18031. x509 = wolfSSL_X509_load_certificate_buffer(client_cert_der_1024,
  18032. sizeof_client_cert_der_1024, SSL_FILETYPE_ASN1);
  18033. keyLenBits = 1024;
  18034. #elif defined(USE_CERT_BUFFERS_2048)
  18035. XMEMCPY(tmp, client_key_der_2048, sizeof_client_key_der_2048);
  18036. cliKeySz = (long)sizeof_client_key_der_2048;
  18037. x509 = wolfSSL_X509_load_certificate_buffer(client_cert_der_2048,
  18038. sizeof_client_cert_der_2048, SSL_FILETYPE_ASN1);
  18039. #elif defined(USE_CERT_BUFFERS_3072)
  18040. XMEMCPY(tmp, client_key_der_3072, sizeof_client_key_der_3072);
  18041. cliKeySz = (long)sizeof_client_key_der_3072;
  18042. x509 = wolfSSL_X509_load_certificate_buffer(client_cert_der_3072,
  18043. sizeof_client_cert_der_3072, SSL_FILETYPE_ASN1);
  18044. keyLenBits = 3072;
  18045. #elif defined(USE_CERT_BUFFERS_4096)
  18046. XMEMCPY(tmp, client_key_der_4096, sizeof_client_key_der_4096);
  18047. cliKeySz = (long)sizeof_client_key_der_4096;
  18048. x509 = wolfSSL_X509_load_certificate_buffer(client_cert_der_4096,
  18049. sizeof_client_cert_der_4096, SSL_FILETYPE_ASN1);
  18050. keyLenBits = 4096;
  18051. #else
  18052. {
  18053. XFILE f;
  18054. f = XFOPEN(clientKey, "rb");
  18055. if (!f) {
  18056. err_sys("can't open ./certs/client-key.der, "
  18057. "Please run from wolfSSL home dir", -41);
  18058. ret = -9000;
  18059. goto openssl_pkey1_test_done;
  18060. }
  18061. cliKeySz = (long)XFREAD(tmp, 1, FOURK_BUF, f);
  18062. XFCLOSE(f);
  18063. }
  18064. /* using existing wolfSSL api to get public and private key */
  18065. x509 = wolfSSL_X509_load_certificate_file(clientCert, SSL_FILETYPE_ASN1);
  18066. #endif /* USE_CERT_BUFFERS */
  18067. clikey = tmp;
  18068. if ((prvKey = EVP_PKEY_new()) == NULL) {
  18069. ret = -9001;
  18070. goto openssl_pkey1_test_done;
  18071. }
  18072. EVP_PKEY_free(prvKey);
  18073. prvKey = NULL;
  18074. if (x509 == NULL) {
  18075. ret = -9002;
  18076. goto openssl_pkey1_test_done;
  18077. }
  18078. pubKey = X509_get_pubkey(x509);
  18079. if (pubKey == NULL) {
  18080. ret = -9003;
  18081. goto openssl_pkey1_test_done;
  18082. }
  18083. prvKey = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &clikey, cliKeySz);
  18084. if (prvKey == NULL) {
  18085. ret = -9004;
  18086. goto openssl_pkey1_test_done;
  18087. }
  18088. /* phase 2 API to create EVP_PKEY_CTX and encrypt/decrypt */
  18089. if (EVP_PKEY_bits(prvKey) != keyLenBits) {
  18090. ret = -9005;
  18091. goto openssl_pkey1_test_done;
  18092. }
  18093. if (EVP_PKEY_size(prvKey) != keyLenBits/8) {
  18094. ret = -9006;
  18095. goto openssl_pkey1_test_done;
  18096. }
  18097. dec = EVP_PKEY_CTX_new(prvKey, NULL);
  18098. enc = EVP_PKEY_CTX_new(pubKey, NULL);
  18099. if (dec == NULL || enc == NULL) {
  18100. ret = -9007;
  18101. goto openssl_pkey1_test_done;
  18102. }
  18103. if (EVP_PKEY_decrypt_init(dec) != 1) {
  18104. ret = -9008;
  18105. goto openssl_pkey1_test_done;
  18106. }
  18107. if (EVP_PKEY_encrypt_init(enc) != 1) {
  18108. ret = -9009;
  18109. goto openssl_pkey1_test_done;
  18110. }
  18111. if (EVP_PKEY_CTX_set_rsa_padding(dec, RSA_PKCS1_PADDING) <= 0) {
  18112. ret = -9010;
  18113. goto openssl_pkey1_test_done;
  18114. }
  18115. #ifndef HAVE_FIPS
  18116. if (EVP_PKEY_CTX_set_rsa_padding(dec, RSA_PKCS1_OAEP_PADDING) <= 0){
  18117. ret = -9011;
  18118. goto openssl_pkey1_test_done;
  18119. }
  18120. if (EVP_PKEY_CTX_set_rsa_padding(enc, RSA_PKCS1_OAEP_PADDING) <= 0) {
  18121. ret = -9012;
  18122. goto openssl_pkey1_test_done;
  18123. }
  18124. #endif
  18125. XMEMSET(cipher, 0, RSA_TEST_BYTES);
  18126. outlen = keyLenBits/8;
  18127. if (EVP_PKEY_encrypt(enc, cipher, &outlen, msg, sizeof(msg)) < 0) {
  18128. ret = -9013;
  18129. goto openssl_pkey1_test_done;
  18130. }
  18131. XMEMSET(plain, 0, RSA_TEST_BYTES);
  18132. if (EVP_PKEY_decrypt(dec, plain, &outlen, cipher, outlen) != 1) {
  18133. ret = -9014;
  18134. goto openssl_pkey1_test_done;
  18135. }
  18136. openssl_pkey1_test_done:
  18137. if (pubKey != NULL) {
  18138. EVP_PKEY_free(pubKey);
  18139. }
  18140. if (prvKey != NULL) {
  18141. EVP_PKEY_free(prvKey);
  18142. }
  18143. if (dec != NULL) {
  18144. EVP_PKEY_CTX_free(dec);
  18145. }
  18146. if (enc != NULL) {
  18147. EVP_PKEY_CTX_free(enc);
  18148. }
  18149. if (x509 != NULL) {
  18150. X509_free(x509);
  18151. }
  18152. #ifdef WOLFSSL_SMALL_STACK
  18153. if (tmp != NULL)
  18154. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18155. if (cipher != NULL)
  18156. XFREE(cipher, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18157. if (plain != NULL)
  18158. XFREE(plain, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18159. #endif
  18160. #endif
  18161. return ret;
  18162. }
  18163. #define ERR_BASE_EVPSIG (-5100)
  18164. WOLFSSL_TEST_SUBROUTINE int openssl_evpSig_test(void)
  18165. {
  18166. #if !defined(NO_RSA) && !defined(NO_SHA) && !defined(HAVE_USER_RSA)
  18167. byte* prvTmp;
  18168. byte* pubTmp;
  18169. int prvBytes;
  18170. int pubBytes;
  18171. RSA *prvRsa;
  18172. RSA *pubRsa;
  18173. EVP_PKEY *prvPkey;
  18174. EVP_PKEY *pubPkey;
  18175. EVP_MD_CTX* sign;
  18176. EVP_MD_CTX* verf;
  18177. char msg[] = "see spot run";
  18178. unsigned char sig[256];
  18179. unsigned int sigSz;
  18180. const void* pt;
  18181. unsigned int count;
  18182. int ret, ret1, ret2;
  18183. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  18184. XFILE keyFile;
  18185. XFILE keypubFile;
  18186. char cliKey[] = "./certs/client-key.der";
  18187. char cliKeypub[] = "./certs/client-keyPub.der";
  18188. #endif
  18189. prvTmp = (byte*)XMALLOC(FOURK_BUFF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18190. if (prvTmp == NULL)
  18191. return ERR_BASE_EVPSIG-1;
  18192. pubTmp = (byte*)XMALLOC(FOURK_BUFF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18193. if (pubTmp == NULL) {
  18194. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18195. return ERR_BASE_EVPSIG-2;
  18196. }
  18197. #ifdef USE_CERT_BUFFERS_1024
  18198. XMEMCPY(prvTmp, client_key_der_1024, sizeof_client_key_der_1024);
  18199. prvBytes = sizeof_client_key_der_1024;
  18200. XMEMCPY(pubTmp, client_keypub_der_1024, sizeof_client_keypub_der_1024);
  18201. pubBytes = sizeof_client_keypub_der_1024;
  18202. #elif defined(USE_CERT_BUFFERS_2048)
  18203. XMEMCPY(prvTmp, client_key_der_2048, sizeof_client_key_der_2048);
  18204. prvBytes = sizeof_client_key_der_2048;
  18205. XMEMCPY(pubTmp, client_keypub_der_2048, sizeof_client_keypub_der_2048);
  18206. pubBytes = sizeof_client_keypub_der_2048;
  18207. #else
  18208. keyFile = XFOPEN(cliKey, "rb");
  18209. if (!keyFile) {
  18210. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18211. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18212. err_sys("can't open ./certs/client-key.der, "
  18213. "Please run from wolfSSL home dir", -40);
  18214. return ERR_BASE_EVPSIG-3;
  18215. }
  18216. prvBytes = (int)XFREAD(prvTmp, 1, (int)FOURK_BUFF, keyFile);
  18217. XFCLOSE(keyFile);
  18218. keypubFile = XFOPEN(cliKeypub, "rb");
  18219. if (!keypubFile) {
  18220. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18221. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18222. err_sys("can't open ./certs/client-cert.der, "
  18223. "Please run from wolfSSL home dir", -41);
  18224. return ERR_BASE_EVPSIG-4;
  18225. }
  18226. pubBytes = (int)XFREAD(pubTmp, 1, (int)FOURK_BUFF, keypubFile);
  18227. XFCLOSE(keypubFile);
  18228. #endif /* USE_CERT_BUFFERS */
  18229. prvRsa = wolfSSL_RSA_new();
  18230. pubRsa = wolfSSL_RSA_new();
  18231. if((prvRsa == NULL) || (pubRsa == NULL)){
  18232. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18233. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18234. err_sys("ERROR with RSA_new", -9100);
  18235. return ERR_BASE_EVPSIG-5;
  18236. }
  18237. ret1 = wolfSSL_RSA_LoadDer_ex(prvRsa, prvTmp, prvBytes, WOLFSSL_RSA_LOAD_PRIVATE);
  18238. ret2 = wolfSSL_RSA_LoadDer_ex(pubRsa, pubTmp, pubBytes, WOLFSSL_RSA_LOAD_PUBLIC);
  18239. if((ret1 != SSL_SUCCESS) || (ret2 != SSL_SUCCESS)){
  18240. printf("error with RSA_LoadDer_ex\n");
  18241. return ERR_BASE_EVPSIG-6;
  18242. }
  18243. prvPkey = wolfSSL_EVP_PKEY_new();
  18244. pubPkey = wolfSSL_EVP_PKEY_new();
  18245. if((prvPkey == NULL) || (pubPkey == NULL)){
  18246. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18247. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18248. printf("error with KEY_new\n");
  18249. return ERR_BASE_EVPSIG-7;
  18250. }
  18251. ret1 = wolfSSL_EVP_PKEY_set1_RSA(prvPkey, prvRsa);
  18252. ret2 = wolfSSL_EVP_PKEY_set1_RSA(pubPkey, pubRsa);
  18253. if((ret1 != 1) || (ret2 != 1)){
  18254. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18255. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18256. printf("error with EVP_PKEY_set1_RSA\n");
  18257. return ERR_BASE_EVPSIG-8;
  18258. }
  18259. /****************** sign and verify *******************/
  18260. sign = EVP_MD_CTX_create();
  18261. verf = EVP_MD_CTX_create();
  18262. if((sign == NULL)||(verf == NULL)){
  18263. printf("error with EVP_MD_CTX_create\n");
  18264. EVP_MD_CTX_destroy(sign);
  18265. EVP_MD_CTX_destroy(verf);
  18266. return ERR_BASE_EVPSIG-10;
  18267. }
  18268. ret = EVP_SignInit(sign, EVP_sha1());
  18269. if (ret != SSL_SUCCESS){
  18270. printf("error with EVP_SignInit\n");
  18271. EVP_MD_CTX_destroy(sign);
  18272. EVP_MD_CTX_destroy(verf);
  18273. return ERR_BASE_EVPSIG-11;
  18274. }
  18275. count = sizeof(msg);
  18276. show("message = ", (char *)msg, count);
  18277. /* sign */
  18278. XMEMSET(sig, 0, sizeof(sig));
  18279. pt = (const void*)msg;
  18280. ret1 = EVP_SignUpdate(sign, pt, count);
  18281. ret2 = EVP_SignFinal(sign, sig, &sigSz, prvPkey);
  18282. if((ret1 != SSL_SUCCESS) || (ret2 != SSL_SUCCESS)){
  18283. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18284. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18285. EVP_MD_CTX_destroy(sign);
  18286. EVP_MD_CTX_destroy(verf);
  18287. printf("error with EVP_MD_CTX_create\n");
  18288. return ERR_BASE_EVPSIG-12;
  18289. }
  18290. show("signature = ", (char *)sig, sigSz);
  18291. /* verify */
  18292. pt = (const void*)msg;
  18293. ret1 = EVP_VerifyInit(verf, EVP_sha1());
  18294. ret2 = EVP_VerifyUpdate(verf, pt, count);
  18295. if((ret1 != SSL_SUCCESS) || (ret2 != SSL_SUCCESS)){
  18296. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18297. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18298. EVP_MD_CTX_destroy(sign);
  18299. EVP_MD_CTX_destroy(verf);
  18300. printf("error with EVP_Verify\n");
  18301. return ERR_BASE_EVPSIG-13;
  18302. }
  18303. if (EVP_VerifyFinal(verf, sig, sigSz, pubPkey) != 1) {
  18304. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18305. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18306. EVP_MD_CTX_destroy(sign);
  18307. EVP_MD_CTX_destroy(verf);
  18308. printf("error with EVP_VerifyFinal\n");
  18309. return ERR_BASE_EVPSIG-14;
  18310. }
  18311. /* expect fail without update */
  18312. EVP_VerifyInit(verf, EVP_sha1());
  18313. if (EVP_VerifyFinal(verf, sig, sigSz, pubPkey) == 1) {
  18314. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18315. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18316. EVP_MD_CTX_destroy(sign);
  18317. EVP_MD_CTX_destroy(verf);
  18318. printf("EVP_VerifyInit without update not detected\n");
  18319. return ERR_BASE_EVPSIG-15;
  18320. }
  18321. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18322. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18323. EVP_MD_CTX_destroy(sign);
  18324. EVP_MD_CTX_destroy(verf);
  18325. wolfSSL_RSA_free(prvRsa);
  18326. wolfSSL_RSA_free(pubRsa);
  18327. EVP_PKEY_free(pubPkey);
  18328. EVP_PKEY_free(prvPkey);
  18329. #endif /* NO_RSA */
  18330. return 0;
  18331. }
  18332. #endif /* OPENSSL_EXTRA */
  18333. #ifndef NO_PWDBASED
  18334. #ifdef HAVE_SCRYPT
  18335. /* Test vectors taken from RFC 7914: scrypt PBKDF - Section 12. */
  18336. WOLFSSL_TEST_SUBROUTINE int scrypt_test(void)
  18337. {
  18338. #ifdef HAVE_FIPS
  18339. /* RFC 7914 test vector keys are too short for FIPS. */
  18340. #else
  18341. int ret;
  18342. byte derived[64];
  18343. WOLFSSL_SMALL_STACK_STATIC const byte verify1[] = {
  18344. 0x77, 0xd6, 0x57, 0x62, 0x38, 0x65, 0x7b, 0x20,
  18345. 0x3b, 0x19, 0xca, 0x42, 0xc1, 0x8a, 0x04, 0x97,
  18346. 0xf1, 0x6b, 0x48, 0x44, 0xe3, 0x07, 0x4a, 0xe8,
  18347. 0xdf, 0xdf, 0xfa, 0x3f, 0xed, 0xe2, 0x14, 0x42,
  18348. 0xfc, 0xd0, 0x06, 0x9d, 0xed, 0x09, 0x48, 0xf8,
  18349. 0x32, 0x6a, 0x75, 0x3a, 0x0f, 0xc8, 0x1f, 0x17,
  18350. 0xe8, 0xd3, 0xe0, 0xfb, 0x2e, 0x0d, 0x36, 0x28,
  18351. 0xcf, 0x35, 0xe2, 0x0c, 0x38, 0xd1, 0x89, 0x06
  18352. };
  18353. WOLFSSL_SMALL_STACK_STATIC const byte verify2[] = {
  18354. 0xfd, 0xba, 0xbe, 0x1c, 0x9d, 0x34, 0x72, 0x00,
  18355. 0x78, 0x56, 0xe7, 0x19, 0x0d, 0x01, 0xe9, 0xfe,
  18356. 0x7c, 0x6a, 0xd7, 0xcb, 0xc8, 0x23, 0x78, 0x30,
  18357. 0xe7, 0x73, 0x76, 0x63, 0x4b, 0x37, 0x31, 0x62,
  18358. 0x2e, 0xaf, 0x30, 0xd9, 0x2e, 0x22, 0xa3, 0x88,
  18359. 0x6f, 0xf1, 0x09, 0x27, 0x9d, 0x98, 0x30, 0xda,
  18360. 0xc7, 0x27, 0xaf, 0xb9, 0x4a, 0x83, 0xee, 0x6d,
  18361. 0x83, 0x60, 0xcb, 0xdf, 0xa2, 0xcc, 0x06, 0x40
  18362. };
  18363. #if !defined(BENCH_EMBEDDED) && !defined(WOLFSSL_LINUXKM) && !defined(HAVE_INTEL_QA)
  18364. WOLFSSL_SMALL_STACK_STATIC const byte verify3[] = {
  18365. 0x70, 0x23, 0xbd, 0xcb, 0x3a, 0xfd, 0x73, 0x48,
  18366. 0x46, 0x1c, 0x06, 0xcd, 0x81, 0xfd, 0x38, 0xeb,
  18367. 0xfd, 0xa8, 0xfb, 0xba, 0x90, 0x4f, 0x8e, 0x3e,
  18368. 0xa9, 0xb5, 0x43, 0xf6, 0x54, 0x5d, 0xa1, 0xf2,
  18369. 0xd5, 0x43, 0x29, 0x55, 0x61, 0x3f, 0x0f, 0xcf,
  18370. 0x62, 0xd4, 0x97, 0x05, 0x24, 0x2a, 0x9a, 0xf9,
  18371. 0xe6, 0x1e, 0x85, 0xdc, 0x0d, 0x65, 0x1e, 0x40,
  18372. 0xdf, 0xcf, 0x01, 0x7b, 0x45, 0x57, 0x58, 0x87
  18373. };
  18374. #endif
  18375. #ifdef SCRYPT_TEST_ALL
  18376. /* Test case is very slow.
  18377. * Use for confirmation after code change or new platform.
  18378. */
  18379. WOLFSSL_SMALL_STACK_STATIC const byte verify4[] = {
  18380. 0x21, 0x01, 0xcb, 0x9b, 0x6a, 0x51, 0x1a, 0xae,
  18381. 0xad, 0xdb, 0xbe, 0x09, 0xcf, 0x70, 0xf8, 0x81,
  18382. 0xec, 0x56, 0x8d, 0x57, 0x4a, 0x2f, 0xfd, 0x4d,
  18383. 0xab, 0xe5, 0xee, 0x98, 0x20, 0xad, 0xaa, 0x47,
  18384. 0x8e, 0x56, 0xfd, 0x8f, 0x4b, 0xa5, 0xd0, 0x9f,
  18385. 0xfa, 0x1c, 0x6d, 0x92, 0x7c, 0x40, 0xf4, 0xc3,
  18386. 0x37, 0x30, 0x40, 0x49, 0xe8, 0xa9, 0x52, 0xfb,
  18387. 0xcb, 0xf4, 0x5c, 0x6f, 0xa7, 0x7a, 0x41, 0xa4
  18388. };
  18389. #endif
  18390. ret = wc_scrypt(derived, NULL, 0, NULL, 0, 4, 1, 1, sizeof(verify1));
  18391. if (ret != 0)
  18392. return -9200;
  18393. if (XMEMCMP(derived, verify1, sizeof(verify1)) != 0)
  18394. return -9201;
  18395. ret = wc_scrypt(derived, (byte*)"password", 8, (byte*)"NaCl", 4, 10, 8, 16,
  18396. sizeof(verify2));
  18397. if (ret != 0)
  18398. return -9202;
  18399. if (XMEMCMP(derived, verify2, sizeof(verify2)) != 0)
  18400. return -9203;
  18401. /* Test case with parallel overflowing */
  18402. ret = wc_scrypt(derived, (byte*)"password", 16, (byte*)"NaCl", 16, 2, 4, 8388608,
  18403. sizeof(verify2));
  18404. if (ret != BAD_FUNC_ARG)
  18405. return -9210;
  18406. /* Don't run these test on embedded, since they use large mallocs */
  18407. #if !defined(BENCH_EMBEDDED) && !defined(WOLFSSL_LINUXKM) && !defined(HAVE_INTEL_QA)
  18408. ret = wc_scrypt(derived, (byte*)"pleaseletmein", 13,
  18409. (byte*)"SodiumChloride", 14, 14, 8, 1, sizeof(verify3));
  18410. if (ret != 0)
  18411. return -9204;
  18412. if (XMEMCMP(derived, verify3, sizeof(verify3)) != 0)
  18413. return -9205;
  18414. #ifdef SCRYPT_TEST_ALL
  18415. ret = wc_scrypt(derived, (byte*)"pleaseletmein", 13,
  18416. (byte*)"SodiumChloride", 14, 20, 8, 1, sizeof(verify4));
  18417. if (ret != 0)
  18418. return -9206;
  18419. if (XMEMCMP(derived, verify4, sizeof(verify4)) != 0)
  18420. return -9207;
  18421. #endif
  18422. #endif /* !BENCH_EMBEDDED && !defined(WOLFSSL_LINUXKM) && !HAVE_INTEL_QA */
  18423. ret = wc_scrypt_ex(derived, (byte*)"password", 8, (byte*)"NaCl", 4, 1<<10,
  18424. 8, 16, sizeof(verify2));
  18425. if (ret != 0)
  18426. return -9208;
  18427. if (XMEMCMP(derived, verify2, sizeof(verify2)) != 0)
  18428. return -9209;
  18429. #endif /* !HAVE_FIPS */
  18430. return 0;
  18431. }
  18432. #endif
  18433. #ifdef HAVE_PKCS12
  18434. WOLFSSL_TEST_SUBROUTINE int pkcs12_test(void)
  18435. {
  18436. WOLFSSL_SMALL_STACK_STATIC const byte passwd[] = { 0x00, 0x73, 0x00, 0x6d, 0x00, 0x65, 0x00, 0x67,
  18437. 0x00, 0x00 };
  18438. WOLFSSL_SMALL_STACK_STATIC const byte salt[] = { 0x0a, 0x58, 0xCF, 0x64, 0x53, 0x0d, 0x82, 0x3f };
  18439. WOLFSSL_SMALL_STACK_STATIC const byte passwd2[] = { 0x00, 0x71, 0x00, 0x75, 0x00, 0x65, 0x00, 0x65,
  18440. 0x00, 0x67, 0x00, 0x00 };
  18441. WOLFSSL_SMALL_STACK_STATIC const byte salt2[] = { 0x16, 0x82, 0xC0, 0xfC, 0x5b, 0x3f, 0x7e, 0xc5 };
  18442. byte derived[64];
  18443. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  18444. 0x27, 0xE9, 0x0D, 0x7E, 0xD5, 0xA1, 0xC4, 0x11,
  18445. 0xBA, 0x87, 0x8B, 0xC0, 0x90, 0xF5, 0xCE, 0xBE,
  18446. 0x5E, 0x9D, 0x5F, 0xE3, 0xD6, 0x2B, 0x73, 0xAA
  18447. };
  18448. WOLFSSL_SMALL_STACK_STATIC const byte verify2[] = {
  18449. 0x90, 0x1B, 0x49, 0x70, 0xF0, 0x94, 0xF0, 0xF8,
  18450. 0x45, 0xC0, 0xF3, 0xF3, 0x13, 0x59, 0x18, 0x6A,
  18451. 0x35, 0xE3, 0x67, 0xFE, 0xD3, 0x21, 0xFD, 0x7C
  18452. };
  18453. int id = 1;
  18454. int kLen = 24;
  18455. int iterations = 1;
  18456. int ret = wc_PKCS12_PBKDF(derived, passwd, sizeof(passwd), salt, 8,
  18457. iterations, kLen, WC_SHA256, id);
  18458. if (ret < 0)
  18459. return -9300;
  18460. if (XMEMCMP(derived, verify, kLen) != 0)
  18461. return -9301;
  18462. iterations = 1000;
  18463. ret = wc_PKCS12_PBKDF(derived, passwd2, sizeof(passwd2), salt2, 8,
  18464. iterations, kLen, WC_SHA256, id);
  18465. if (ret < 0)
  18466. return -9302;
  18467. ret = wc_PKCS12_PBKDF_ex(derived, passwd2, sizeof(passwd2), salt2, 8,
  18468. iterations, kLen, WC_SHA256, id, HEAP_HINT);
  18469. if (ret < 0)
  18470. return -9303;
  18471. if (XMEMCMP(derived, verify2, 24) != 0)
  18472. return -9304;
  18473. return 0;
  18474. }
  18475. #endif /* HAVE_PKCS12 */
  18476. #if defined(HAVE_PBKDF2) && !defined(NO_SHA256) && !defined(NO_HMAC)
  18477. WOLFSSL_TEST_SUBROUTINE int pbkdf2_test(void)
  18478. {
  18479. char passwd[] = "passwordpassword";
  18480. WOLFSSL_SMALL_STACK_STATIC const byte salt[] = { 0x78, 0x57, 0x8E, 0x5a, 0x5d, 0x63, 0xcb, 0x06 };
  18481. int iterations = 2048;
  18482. int kLen = 24;
  18483. byte derived[64];
  18484. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  18485. 0x43, 0x6d, 0xb5, 0xe8, 0xd0, 0xfb, 0x3f, 0x35, 0x42, 0x48, 0x39, 0xbc,
  18486. 0x2d, 0xd4, 0xf9, 0x37, 0xd4, 0x95, 0x16, 0xa7, 0x2a, 0x9a, 0x21, 0xd1
  18487. };
  18488. int ret = wc_PBKDF2_ex(derived, (byte*)passwd, (int)XSTRLEN(passwd), salt,
  18489. (int)sizeof(salt), iterations, kLen, WC_SHA256, HEAP_HINT, devId);
  18490. if (ret != 0)
  18491. return ret;
  18492. if (XMEMCMP(derived, verify, sizeof(verify)) != 0)
  18493. return -9400;
  18494. return 0;
  18495. }
  18496. #endif /* HAVE_PBKDF2 && !NO_SHA256 && !NO_HMAC */
  18497. #if defined(HAVE_PBKDF1) && !defined(NO_SHA)
  18498. WOLFSSL_TEST_SUBROUTINE int pbkdf1_test(void)
  18499. {
  18500. char passwd[] = "password";
  18501. WOLFSSL_SMALL_STACK_STATIC const byte salt[] = { 0x78, 0x57, 0x8E, 0x5a, 0x5d, 0x63, 0xcb, 0x06 };
  18502. int iterations = 1000;
  18503. int kLen = 16;
  18504. byte derived[16];
  18505. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  18506. 0xDC, 0x19, 0x84, 0x7E, 0x05, 0xC6, 0x4D, 0x2F,
  18507. 0xAF, 0x10, 0xEB, 0xFB, 0x4A, 0x3D, 0x2A, 0x20
  18508. };
  18509. int ret = wc_PBKDF1_ex(derived, kLen, NULL, 0, (byte*)passwd,
  18510. (int)XSTRLEN(passwd), salt, (int)sizeof(salt), iterations, WC_SHA,
  18511. HEAP_HINT);
  18512. if (ret != 0)
  18513. return ret;
  18514. if (XMEMCMP(derived, verify, sizeof(verify)) != 0)
  18515. return -9500;
  18516. return 0;
  18517. }
  18518. #endif /* HAVE_PBKDF2 && !NO_SHA */
  18519. WOLFSSL_TEST_SUBROUTINE int pwdbased_test(void)
  18520. {
  18521. int ret = 0;
  18522. #if defined(HAVE_PBKDF1) && !defined(NO_SHA)
  18523. ret = pbkdf1_test();
  18524. if (ret != 0)
  18525. return ret;
  18526. #endif
  18527. #if defined(HAVE_PBKDF2) && !defined(NO_SHA256) && !defined(NO_HMAC)
  18528. ret = pbkdf2_test();
  18529. if (ret != 0)
  18530. return ret;
  18531. #endif
  18532. #ifdef HAVE_PKCS12
  18533. ret = pkcs12_test();
  18534. if (ret != 0)
  18535. return ret;
  18536. #endif
  18537. #ifdef HAVE_SCRYPT
  18538. ret = scrypt_test();
  18539. if (ret != 0)
  18540. return ret;
  18541. #endif
  18542. return ret;
  18543. }
  18544. #endif /* NO_PWDBASED */
  18545. #if defined(HAVE_HKDF) && !defined(NO_HMAC)
  18546. /* hkdf_test has issue with WOLFSSL_TEST_SUBROUTINE set on Xilinx with afalg */
  18547. static int hkdf_test(void)
  18548. {
  18549. int ret = 0;
  18550. #if !defined(NO_SHA) || !defined(NO_SHA256)
  18551. int L;
  18552. byte okm1[42];
  18553. byte ikm1[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  18554. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  18555. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b };
  18556. #ifndef HAVE_FIPS
  18557. byte salt1[13] ={ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18558. 0x08, 0x09, 0x0a, 0x0b, 0x0c };
  18559. byte info1[10] ={ 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
  18560. 0xf8, 0xf9 };
  18561. #endif
  18562. #ifndef NO_SHA
  18563. byte res1[42] = { 0x0a, 0xc1, 0xaf, 0x70, 0x02, 0xb3, 0xd7, 0x61,
  18564. 0xd1, 0xe5, 0x52, 0x98, 0xda, 0x9d, 0x05, 0x06,
  18565. 0xb9, 0xae, 0x52, 0x05, 0x72, 0x20, 0xa3, 0x06,
  18566. 0xe0, 0x7b, 0x6b, 0x87, 0xe8, 0xdf, 0x21, 0xd0,
  18567. 0xea, 0x00, 0x03, 0x3d, 0xe0, 0x39, 0x84, 0xd3,
  18568. 0x49, 0x18 };
  18569. #ifndef HAVE_FIPS
  18570. byte res2[42] = { 0x08, 0x5a, 0x01, 0xea, 0x1b, 0x10, 0xf3, 0x69,
  18571. 0x33, 0x06, 0x8b, 0x56, 0xef, 0xa5, 0xad, 0x81,
  18572. 0xa4, 0xf1, 0x4b, 0x82, 0x2f, 0x5b, 0x09, 0x15,
  18573. 0x68, 0xa9, 0xcd, 0xd4, 0xf1, 0x55, 0xfd, 0xa2,
  18574. 0xc2, 0x2e, 0x42, 0x24, 0x78, 0xd3, 0x05, 0xf3,
  18575. 0xf8, 0x96 };
  18576. #endif
  18577. #endif /* !NO_SHA */
  18578. #ifndef NO_SHA256
  18579. byte res3[42] = { 0x8d, 0xa4, 0xe7, 0x75, 0xa5, 0x63, 0xc1, 0x8f,
  18580. 0x71, 0x5f, 0x80, 0x2a, 0x06, 0x3c, 0x5a, 0x31,
  18581. 0xb8, 0xa1, 0x1f, 0x5c, 0x5e, 0xe1, 0x87, 0x9e,
  18582. 0xc3, 0x45, 0x4e, 0x5f, 0x3c, 0x73, 0x8d, 0x2d,
  18583. 0x9d, 0x20, 0x13, 0x95, 0xfa, 0xa4, 0xb6, 0x1a,
  18584. 0x96, 0xc8 };
  18585. #ifndef HAVE_FIPS
  18586. byte res4[42] = { 0x3c, 0xb2, 0x5f, 0x25, 0xfa, 0xac, 0xd5, 0x7a,
  18587. 0x90, 0x43, 0x4f, 0x64, 0xd0, 0x36, 0x2f, 0x2a,
  18588. 0x2d, 0x2d, 0x0a, 0x90, 0xcf, 0x1a, 0x5a, 0x4c,
  18589. 0x5d, 0xb0, 0x2d, 0x56, 0xec, 0xc4, 0xc5, 0xbf,
  18590. 0x34, 0x00, 0x72, 0x08, 0xd5, 0xb8, 0x87, 0x18,
  18591. 0x58, 0x65 };
  18592. #endif
  18593. #endif /* !NO_SHA256 */
  18594. XMEMSET(okm1, 0, sizeof(okm1));
  18595. L = (int)sizeof(okm1);
  18596. #ifndef NO_SHA
  18597. ret = wc_HKDF(WC_SHA, ikm1, (word32)sizeof(ikm1), NULL, 0, NULL, 0,
  18598. okm1, L);
  18599. if (ret != 0)
  18600. return -9700;
  18601. if (XMEMCMP(okm1, res1, L) != 0)
  18602. return -9701;
  18603. #ifndef HAVE_FIPS
  18604. /* fips can't have key size under 14 bytes, salt is key too */
  18605. L = (int)sizeof(okm1);
  18606. ret = wc_HKDF(WC_SHA, ikm1, 11, salt1, (word32)sizeof(salt1),
  18607. info1, (word32)sizeof(info1), okm1, L);
  18608. if (ret != 0)
  18609. return -9702;
  18610. if (XMEMCMP(okm1, res2, L) != 0)
  18611. return -9703;
  18612. #endif /* HAVE_FIPS */
  18613. #endif /* !NO_SHA */
  18614. #ifndef NO_SHA256
  18615. ret = wc_HKDF(WC_SHA256, ikm1, (word32)sizeof(ikm1), NULL, 0, NULL, 0,
  18616. okm1, L);
  18617. if (ret != 0)
  18618. return -9704;
  18619. if (XMEMCMP(okm1, res3, L) != 0)
  18620. return -9705;
  18621. #ifndef HAVE_FIPS
  18622. /* fips can't have key size under 14 bytes, salt is key too */
  18623. ret = wc_HKDF(WC_SHA256, ikm1, (word32)sizeof(ikm1),
  18624. salt1, (word32)sizeof(salt1), info1, (word32)sizeof(info1), okm1, L);
  18625. if (ret != 0)
  18626. return -9706;
  18627. if (XMEMCMP(okm1, res4, L) != 0)
  18628. return -9707;
  18629. #endif /* HAVE_FIPS */
  18630. #endif /* !NO_SHA256 */
  18631. #endif /* !NO_SHA || !NO_SHA256 */
  18632. return ret;
  18633. }
  18634. #endif /* HAVE_HKDF */
  18635. #ifdef WOLFSSL_WOLFSSH
  18636. typedef struct {
  18637. byte hashId;
  18638. byte keyId;
  18639. const byte* k;
  18640. word32 kSz;
  18641. const byte* h;
  18642. word32 hSz;
  18643. const byte* sessionId;
  18644. word32 sessionIdSz;
  18645. const byte* expectedKey;
  18646. word32 expectedKeySz;
  18647. } SshKdfTestVector;
  18648. /** Test Vector Set #3: SHA-256 **/
  18649. static const byte sshKdfTvSet3k[] = {
  18650. 0x6A, 0xC3, 0x82, 0xEA, 0xAC, 0xA0, 0x93, 0xE1,
  18651. 0x25, 0xE2, 0x5C, 0x24, 0xBE, 0xBC, 0x84, 0x64,
  18652. 0x0C, 0x11, 0x98, 0x75, 0x07, 0x34, 0x4B, 0x5C,
  18653. 0x73, 0x9C, 0xEB, 0x84, 0xA9, 0xE0, 0xB2, 0x22,
  18654. 0xB9, 0xA8, 0xB5, 0x1C, 0x83, 0x9E, 0x5E, 0xBE,
  18655. 0x49, 0xCF, 0xAD, 0xBF, 0xB3, 0x95, 0x99, 0x76,
  18656. 0x4E, 0xD5, 0x22, 0x09, 0x9D, 0xC9, 0x12, 0x75,
  18657. 0x19, 0x50, 0xDC, 0x7D, 0xC9, 0x7F, 0xBD, 0xC0,
  18658. 0x63, 0x28, 0xB6, 0x8F, 0x22, 0x78, 0x1F, 0xD3,
  18659. 0x15, 0xAF, 0x56, 0x80, 0x09, 0xA5, 0x50, 0x9E,
  18660. 0x5B, 0x87, 0xA1, 0x1B, 0xF5, 0x27, 0xC0, 0x56,
  18661. 0xDA, 0xFF, 0xD8, 0x2A, 0xB6, 0xCB, 0xC2, 0x5C,
  18662. 0xCA, 0x37, 0x14, 0x34, 0x59, 0xE7, 0xBC, 0x63,
  18663. 0xBC, 0xDE, 0x52, 0x75, 0x7A, 0xDE, 0xB7, 0xDF,
  18664. 0x01, 0xCF, 0x12, 0x17, 0x3F, 0x1F, 0xEF, 0x81,
  18665. 0x02, 0xEC, 0x5A, 0xB1, 0x42, 0xC2, 0x13, 0xDD,
  18666. 0x9D, 0x30, 0x69, 0x62, 0x78, 0xA8, 0xD8, 0xBC,
  18667. 0x32, 0xDD, 0xE9, 0x59, 0x2D, 0x28, 0xC0, 0x78,
  18668. 0xC6, 0xD9, 0x2B, 0x94, 0x7D, 0x82, 0x5A, 0xCA,
  18669. 0xAB, 0x64, 0x94, 0x84, 0x6A, 0x49, 0xDE, 0x24,
  18670. 0xB9, 0x62, 0x3F, 0x48, 0x89, 0xE8, 0xAD, 0xC3,
  18671. 0x8E, 0x8C, 0x66, 0x9E, 0xFF, 0xEF, 0x17, 0x60,
  18672. 0x40, 0xAD, 0x94, 0x5E, 0x90, 0xA7, 0xD3, 0xEE,
  18673. 0xC1, 0x5E, 0xFE, 0xEE, 0x78, 0xAE, 0x71, 0x04,
  18674. 0x3C, 0x96, 0x51, 0x11, 0x03, 0xA1, 0x6B, 0xA7,
  18675. 0xCA, 0xF0, 0xAC, 0xD0, 0x64, 0x2E, 0xFD, 0xBE,
  18676. 0x80, 0x99, 0x34, 0xFA, 0xA1, 0xA5, 0xF1, 0xBD,
  18677. 0x11, 0x04, 0x36, 0x49, 0xB2, 0x5C, 0xCD, 0x1F,
  18678. 0xEE, 0x2E, 0x38, 0x81, 0x5D, 0x4D, 0x5F, 0x5F,
  18679. 0xC6, 0xB4, 0x10, 0x29, 0x69, 0xF2, 0x1C, 0x22,
  18680. 0xAE, 0x1B, 0x0E, 0x7D, 0x36, 0x03, 0xA5, 0x56,
  18681. 0xA1, 0x32, 0x62, 0xFF, 0x62, 0x8D, 0xE2, 0x22
  18682. };
  18683. static const byte sshKdfTvSet3h[] = {
  18684. 0x7B, 0x70, 0x01, 0x18, 0x5E, 0x25, 0x6D, 0x44,
  18685. 0x93, 0x44, 0x5F, 0x39, 0xA5, 0x5F, 0xB9, 0x05,
  18686. 0xE6, 0x32, 0x1F, 0x4B, 0x5D, 0xD8, 0xBB, 0xF3,
  18687. 0x10, 0x0D, 0x51, 0xBA, 0x0B, 0xDA, 0x3D, 0x2D
  18688. };
  18689. static const byte sshKdfTvSet3sid[] = {
  18690. 0x7B, 0x70, 0x01, 0x18, 0x5E, 0x25, 0x6D, 0x44,
  18691. 0x93, 0x44, 0x5F, 0x39, 0xA5, 0x5F, 0xB9, 0x05,
  18692. 0xE6, 0x32, 0x1F, 0x4B, 0x5D, 0xD8, 0xBB, 0xF3,
  18693. 0x10, 0x0D, 0x51, 0xBA, 0x0B, 0xDA, 0x3D, 0x2D
  18694. };
  18695. static const byte sshKdfTvSet3a[] = {
  18696. 0x81, 0xF0, 0x33, 0x0E, 0xF6, 0xF0, 0x53, 0x61,
  18697. 0xB3, 0x82, 0x3B, 0xFD, 0xED, 0x6E, 0x1D, 0xE9
  18698. };
  18699. static const byte sshKdfTvSet3b[] = {
  18700. 0x3F, 0x6F, 0xD2, 0x06, 0x5E, 0xEB, 0x2B, 0x0B,
  18701. 0x1D, 0x93, 0x19, 0x5A, 0x1F, 0xED, 0x48, 0xA5
  18702. };
  18703. static const byte sshKdfTvSet3c[] = {
  18704. 0xC3, 0x54, 0x71, 0x03, 0x4E, 0x6F, 0xD6, 0x54,
  18705. 0x76, 0x13, 0x17, 0x8E, 0x23, 0x43, 0x5F, 0x21
  18706. };
  18707. static const byte sshKdfTvSet3d[] = {
  18708. 0x7E, 0x9D, 0x79, 0x03, 0x20, 0x90, 0xD9, 0x9F,
  18709. 0x98, 0xB0, 0x15, 0x63, 0x4D, 0xD9, 0xF4, 0x62
  18710. };
  18711. static const byte sshKdfTvSet3e[] = {
  18712. 0x24, 0xEE, 0x55, 0x9A, 0xD7, 0xCE, 0x71, 0x2B,
  18713. 0x68, 0x5D, 0x0B, 0x22, 0x71, 0xE4, 0x43, 0xC1,
  18714. 0x7A, 0xB1, 0xD1, 0xDC, 0xEB, 0x5A, 0x36, 0x05,
  18715. 0x69, 0xD2, 0x5D, 0x5D, 0xC2, 0x43, 0x00, 0x2F
  18716. };
  18717. static const byte sshKdfTvSet3f[] = {
  18718. 0xC3, 0x41, 0x9C, 0x2B, 0x96, 0x62, 0x35, 0x86,
  18719. 0x9D, 0x71, 0x4B, 0xA5, 0xAC, 0x48, 0xDD, 0xB7,
  18720. 0xD9, 0xE3, 0x5C, 0x8C, 0x19, 0xAA, 0xC7, 0x34,
  18721. 0x22, 0x33, 0x7A, 0x37, 0x34, 0x53, 0x60, 0x7E
  18722. };
  18723. static const SshKdfTestVector sshKdfTestVectors[] = {
  18724. {WC_HASH_TYPE_SHA256, 'A',
  18725. sshKdfTvSet3k, sizeof(sshKdfTvSet3k),
  18726. sshKdfTvSet3h, sizeof(sshKdfTvSet3h),
  18727. sshKdfTvSet3sid, sizeof(sshKdfTvSet3sid),
  18728. sshKdfTvSet3a, sizeof(sshKdfTvSet3a)},
  18729. {WC_HASH_TYPE_SHA256, 'B',
  18730. sshKdfTvSet3k, sizeof(sshKdfTvSet3k),
  18731. sshKdfTvSet3h, sizeof(sshKdfTvSet3h),
  18732. sshKdfTvSet3sid, sizeof(sshKdfTvSet3sid),
  18733. sshKdfTvSet3b, sizeof(sshKdfTvSet3b)},
  18734. {WC_HASH_TYPE_SHA256, 'C',
  18735. sshKdfTvSet3k, sizeof(sshKdfTvSet3k),
  18736. sshKdfTvSet3h, sizeof(sshKdfTvSet3h),
  18737. sshKdfTvSet3sid, sizeof(sshKdfTvSet3sid),
  18738. sshKdfTvSet3c, sizeof(sshKdfTvSet3c)},
  18739. {WC_HASH_TYPE_SHA256, 'D',
  18740. sshKdfTvSet3k, sizeof(sshKdfTvSet3k),
  18741. sshKdfTvSet3h, sizeof(sshKdfTvSet3h),
  18742. sshKdfTvSet3sid, sizeof(sshKdfTvSet3sid),
  18743. sshKdfTvSet3d, sizeof(sshKdfTvSet3d)},
  18744. {WC_HASH_TYPE_SHA256, 'E',
  18745. sshKdfTvSet3k, sizeof(sshKdfTvSet3k),
  18746. sshKdfTvSet3h, sizeof(sshKdfTvSet3h),
  18747. sshKdfTvSet3sid, sizeof(sshKdfTvSet3sid),
  18748. sshKdfTvSet3e, sizeof(sshKdfTvSet3e)},
  18749. {WC_HASH_TYPE_SHA256, 'F',
  18750. sshKdfTvSet3k, sizeof(sshKdfTvSet3k),
  18751. sshKdfTvSet3h, sizeof(sshKdfTvSet3h),
  18752. sshKdfTvSet3sid, sizeof(sshKdfTvSet3sid),
  18753. sshKdfTvSet3f, sizeof(sshKdfTvSet3f)},
  18754. };
  18755. int sshkdf_test(void)
  18756. {
  18757. int result = 0;
  18758. word32 i;
  18759. word32 tc = sizeof(sshKdfTestVectors)/sizeof(SshKdfTestVector);
  18760. const SshKdfTestVector* tv = NULL;
  18761. byte cKey[32]; /* Greater of SHA256_DIGEST_SIZE and AES_BLOCK_SIZE */
  18762. /* sId - Session ID, eKey - Expected Key, cKey - Calculated Key */
  18763. for (i = 0, tv = sshKdfTestVectors; i < tc; i++, tv++) {
  18764. result = wc_SSH_KDF(tv->hashId, tv->keyId,
  18765. cKey, tv->expectedKeySz,
  18766. tv->k, tv->kSz, tv->h, tv->hSz,
  18767. tv->sessionId, tv->sessionIdSz);
  18768. if (result != 0) {
  18769. printf("KDF: Could not derive key.\n");
  18770. result = -101;
  18771. }
  18772. else {
  18773. if (memcmp(cKey, tv->expectedKey, tv->expectedKeySz) != 0) {
  18774. printf("KDF: Calculated Key does not match Expected Key.\n");
  18775. result = -102;
  18776. }
  18777. }
  18778. if (result != 0) break;
  18779. }
  18780. return result;
  18781. }
  18782. #endif /* WOLFSSL_WOLFSSH */
  18783. #ifdef WOLFSSL_TLS13
  18784. #define TLSV13_PSK_DHE_SZ 40
  18785. typedef struct {
  18786. enum wc_HashType hashAlg;
  18787. word32 pskSz;
  18788. word32 dheSz;
  18789. byte psk[TLSV13_PSK_DHE_SZ];
  18790. byte dhe[TLSV13_PSK_DHE_SZ];
  18791. byte hashHello1[WC_MAX_DIGEST_SIZE];
  18792. byte hashHello2[WC_MAX_DIGEST_SIZE];
  18793. byte hashFinished1[WC_MAX_DIGEST_SIZE];
  18794. byte hashFinished2[WC_MAX_DIGEST_SIZE];
  18795. /* Expected */
  18796. byte clientEarlyTrafficSecret[WC_MAX_DIGEST_SIZE];
  18797. byte earlyExporterMasterSecret[WC_MAX_DIGEST_SIZE];
  18798. byte clientHandshakeTrafficSecret[WC_MAX_DIGEST_SIZE];
  18799. byte serverHandshakeTrafficSecret[WC_MAX_DIGEST_SIZE];
  18800. byte clientApplicationTrafficSecret[WC_MAX_DIGEST_SIZE];
  18801. byte serverApplicationTrafficSecret[WC_MAX_DIGEST_SIZE];
  18802. byte exporterMasterSecret[WC_MAX_DIGEST_SIZE];
  18803. byte resumptionMasterSecret[WC_MAX_DIGEST_SIZE];
  18804. } Tls13KdfTestVector;
  18805. /* The following tests come from the CAVP test vectors we used for
  18806. * our FIPS validation. The hash values used are the components from
  18807. * the test hashed together. hashHello1 is the hash of the
  18808. * clientHelloRandom value of the test vector. hashHello2 is the hash
  18809. * of the clientHelloRandom and serverHelloRandom values from the test
  18810. * vector. hashFinished1 is clientHelloRandom, serverHelloRandom, and
  18811. * serverFinishedRandom. hashFinished2 is clientHelloRandom,
  18812. * serverHelloRandom, serverFinishedRandom, and clietnFinishedRandom
  18813. * hashed together. */
  18814. static const Tls13KdfTestVector tls13KdfTestVectors[] = {
  18815. { /* 1 */
  18816. WC_HASH_TYPE_SHA256, 35, 35,
  18817. { /* PSK */
  18818. 0x7b, 0xf1, 0x05, 0x31, 0x36, 0xfa, 0x03, 0xdc,
  18819. 0x31, 0x97, 0x88, 0x04, 0x9c, 0xbc, 0xee, 0xf7,
  18820. 0x8d, 0x84, 0x95, 0x26, 0xaf, 0x1d, 0x68, 0xb0,
  18821. 0x60, 0x7a, 0xcc, 0x4f, 0xc1, 0xd3, 0xa1, 0x68,
  18822. 0x7f, 0x6d, 0xbe
  18823. },
  18824. { /* DHE */
  18825. 0x6e, 0xa1, 0x77, 0xab, 0x2f, 0x43, 0xd2, 0x4b,
  18826. 0xe5, 0xa1, 0x09, 0xe0, 0x7a, 0xd0, 0x01, 0x35,
  18827. 0x8d, 0xf8, 0xf2, 0x5c, 0x91, 0x02, 0xb0, 0x6c,
  18828. 0x3f, 0xeb, 0xee, 0xa4, 0x42, 0x19, 0xce, 0xdc,
  18829. 0x81, 0x26, 0x40
  18830. },
  18831. { /* Hello 1 */
  18832. 0xd9, 0x4b, 0xe4, 0x17, 0xef, 0x58, 0x73, 0x7d,
  18833. 0x28, 0x3d, 0xf0, 0xcc, 0x05, 0x03, 0xaf, 0xac,
  18834. 0x3d, 0x92, 0x79, 0x48, 0xe8, 0x8c, 0xdb, 0xce,
  18835. 0x95, 0x82, 0x21, 0x31, 0x7b, 0x61, 0xd7, 0xc6
  18836. },
  18837. { /* Hello 2 */
  18838. 0xb7, 0x7f, 0x29, 0x91, 0xa4, 0x8b, 0x34, 0xdb,
  18839. 0xbd, 0xc7, 0x54, 0x1c, 0x3b, 0x86, 0xa3, 0x69,
  18840. 0xfe, 0x26, 0xe4, 0x7b, 0xac, 0x57, 0x71, 0xb3,
  18841. 0x32, 0x97, 0xed, 0xd2, 0x0e, 0x95, 0xb8, 0x63
  18842. },
  18843. { /* Finished 1 */
  18844. 0x65, 0xdb, 0x6d, 0x71, 0x71, 0xd0, 0xd8, 0x49,
  18845. 0xd0, 0x3c, 0x8e, 0x2b, 0x24, 0xdf, 0xc2, 0xe9,
  18846. 0xd6, 0xfd, 0xea, 0x04, 0x95, 0x7c, 0xf0, 0x7e,
  18847. 0x57, 0x74, 0x7c, 0xdd, 0xa3, 0x0b, 0x2b, 0x36
  18848. },
  18849. { /* Finished 2 */
  18850. 0x28, 0xf2, 0xf2, 0x79, 0xcf, 0x20, 0x52, 0x90,
  18851. 0x1d, 0x91, 0x05, 0xad, 0x44, 0x26, 0x23, 0x96,
  18852. 0x32, 0xce, 0xec, 0x61, 0xd1, 0xbf, 0x00, 0x48,
  18853. 0x4a, 0xa5, 0x60, 0xcc, 0x28, 0xb5, 0x8d, 0x98
  18854. },
  18855. { /* Client Early Traffic Secret */
  18856. 0x07, 0x14, 0x6a, 0x26, 0x5b, 0x6c, 0x7f, 0x4d, 0x6b, 0x47, 0x3f, 0xd5,
  18857. 0x03, 0x1d, 0xd2, 0x23, 0x3d, 0x89, 0x3e, 0xc6, 0x51, 0xd1, 0xac, 0xf8,
  18858. 0x28, 0xae, 0x4b, 0x76, 0xc8, 0x10, 0x7e, 0xdd
  18859. },
  18860. { /* Early Exporter Master Secret */
  18861. 0xb8, 0xd3, 0x25, 0x7e, 0x2d, 0x41, 0x7b, 0xcb, 0x5e, 0x82, 0x49, 0xf5,
  18862. 0x51, 0x3d, 0xb7, 0x59, 0x32, 0xb3, 0xdf, 0x99, 0x4e, 0x04, 0x69, 0xc6,
  18863. 0x96, 0x8e, 0xe6, 0x3d, 0x91, 0xe4, 0x81, 0x11
  18864. },
  18865. { /* Client Handshake Traffic Secret */
  18866. 0xd9, 0x3b, 0x54, 0xe2, 0xb0, 0xd1, 0x85, 0xf0, 0xfd, 0xf3, 0x48, 0x4a,
  18867. 0xf8, 0x0b, 0xa5, 0xdc, 0x4c, 0x37, 0xcb, 0xd4, 0x20, 0xaf, 0x60, 0xc7,
  18868. 0xd5, 0x50, 0x5d, 0x0c, 0x77, 0x3b, 0x6f, 0xd2
  18869. },
  18870. { /* Server Handshake Traffic Secret */
  18871. 0x4d, 0x40, 0x2b, 0xd2, 0x8c, 0x33, 0x90, 0x39, 0x67, 0x67, 0x05, 0xf7,
  18872. 0x5d, 0x37, 0x1e, 0xdc, 0x4a, 0x70, 0x6b, 0x9e, 0xf8, 0x06, 0x61, 0x89,
  18873. 0x70, 0xe1, 0x3d, 0x36, 0xad, 0x88, 0x7e, 0x5b
  18874. },
  18875. { /* Client Application Traffic Secret */
  18876. 0x74, 0x6e, 0xa0, 0x13, 0x18, 0x34, 0x48, 0x4d, 0x23, 0x31, 0xf1, 0xf9,
  18877. 0xee, 0x44, 0x6d, 0xad, 0xc1, 0xad, 0x92, 0x73, 0xca, 0x27, 0x16, 0x91,
  18878. 0xa2, 0x50, 0x9a, 0xfc, 0xec, 0xf0, 0x6b, 0x24
  18879. },
  18880. { /* Server Application Traffic Secret */
  18881. 0x89, 0x18, 0x7e, 0x34, 0x8d, 0xfc, 0x14, 0xb1, 0x4f, 0x21, 0xd8, 0x29,
  18882. 0xdb, 0x9b, 0xfb, 0x55, 0xcf, 0xa1, 0x4f, 0x95, 0xf8, 0xe0, 0xb0, 0x83,
  18883. 0xd5, 0x34, 0x9e, 0x0b, 0x83, 0x37, 0x42, 0x93
  18884. },
  18885. { /* Exporter Master Secret */
  18886. 0x7d, 0xc8, 0x88, 0x46, 0xd5, 0x57, 0x15, 0xb6, 0x24, 0x25, 0x92, 0x61,
  18887. 0xb1, 0x18, 0x86, 0x2a, 0x6d, 0xa5, 0x84, 0xeb, 0x59, 0xdf, 0x13, 0xbd,
  18888. 0x73, 0xaa, 0x5d, 0x65, 0xab, 0xd9, 0xb4, 0x56
  18889. },
  18890. { /* Resumption Master Secret */
  18891. 0x20, 0xb7, 0xd0, 0xe3, 0x82, 0x01, 0xa1, 0x04, 0xb8, 0x13, 0x29, 0xed,
  18892. 0x35, 0xe4, 0x2f, 0xbf, 0x58, 0x23, 0x7f, 0x21, 0xdb, 0x9f, 0xf8, 0xe0,
  18893. 0xe8, 0xe4, 0xab, 0xc4, 0xa1, 0x61, 0xb9, 0xbb
  18894. }
  18895. },
  18896. { /* 6 */
  18897. WC_HASH_TYPE_SHA256, 0, 33,
  18898. { 0 }, /* PSK */
  18899. { /* DHE */
  18900. 0x7a, 0x46, 0x8c, 0x5a, 0xd1, 0x8e, 0x95, 0xba,
  18901. 0x61, 0xe6, 0x6f, 0xe6, 0x76, 0x0c, 0x20, 0x43,
  18902. 0x16, 0x82, 0x15, 0xfe, 0x54, 0xa3, 0xc7, 0xfd,
  18903. 0x3b, 0x2c, 0x88, 0xb4, 0xd3, 0x42, 0x70, 0x12,
  18904. 0x18
  18905. },
  18906. { /* Hello 1 */
  18907. 0x63, 0x83, 0x58, 0xab, 0x36, 0xcd, 0x0c, 0xf3,
  18908. 0x26, 0x07, 0xb5, 0x5f, 0x0b, 0x8b, 0x45, 0xd6,
  18909. 0x7d, 0x5b, 0x42, 0xdc, 0xa8, 0xaa, 0x06, 0xfb,
  18910. 0x20, 0xa5, 0xbb, 0x85, 0xdb, 0x54, 0xd8, 0x8b
  18911. },
  18912. { /* Hello 2 */
  18913. 0xea, 0xfe, 0x9e, 0x8e, 0xff, 0x1f, 0x6f, 0x43,
  18914. 0xf9, 0x5d, 0xfd, 0xbf, 0xe2, 0x5f, 0x02, 0x2f,
  18915. 0x6d, 0x47, 0x60, 0x9a, 0x48, 0x9a, 0x75, 0xfb,
  18916. 0xb5, 0x4a, 0xbf, 0x9c, 0x4e, 0xff, 0xbf, 0x0b
  18917. },
  18918. { /* Finished 1 */
  18919. 0xca, 0x25, 0xb3, 0x53, 0x8e, 0x6d, 0xc3, 0x36,
  18920. 0x17, 0x30, 0x07, 0xdf, 0x0d, 0xd7, 0x79, 0xb0,
  18921. 0x7f, 0xcb, 0xbe, 0x7a, 0xbc, 0x2d, 0x9f, 0x2d,
  18922. 0x94, 0x44, 0x94, 0xe6, 0xa4, 0xf3, 0xe8, 0x53
  18923. },
  18924. { /* Finished 2 */
  18925. 0x2e, 0xa6, 0x5a, 0xaf, 0xb5, 0xba, 0x9f, 0x2f,
  18926. 0x74, 0x83, 0x5d, 0xbf, 0x86, 0xa4, 0xa6, 0xf6,
  18927. 0xb9, 0x89, 0xdf, 0x17, 0xe1, 0xa8, 0x14, 0xc0,
  18928. 0xe1, 0x50, 0xfa, 0xec, 0xfa, 0xae, 0x8b, 0x7b
  18929. },
  18930. {
  18931. 0x20, 0x18, 0x72, 0x7c, 0xde, 0x3a, 0x85, 0x17, 0x72, 0xdc, 0xd7, 0x72,
  18932. 0xb0, 0xfc, 0x45, 0xd0, 0x62, 0xb9, 0xbb, 0x38, 0x69, 0x05, 0x7b, 0xb4,
  18933. 0x5e, 0x58, 0x5d, 0xed, 0xcd, 0x0b, 0x96, 0xd3
  18934. },
  18935. {
  18936. 0x68, 0x10, 0x20, 0xd1, 0x5e, 0xfc, 0x0c, 0x53, 0x85, 0xbb, 0xdb, 0x18,
  18937. 0xa8, 0x78, 0xf1, 0x2b, 0x13, 0xba, 0x64, 0x1d, 0xe7, 0x09, 0xbe, 0x13,
  18938. 0x49, 0x26, 0xf9, 0x98, 0x56, 0xf1, 0x43, 0xfb
  18939. },
  18940. {
  18941. 0x24, 0x35, 0x3e, 0x10, 0x6f, 0x39, 0x50, 0xd6, 0xa2, 0x12, 0x99, 0xf2,
  18942. 0xd5, 0xf5, 0x19, 0xf5, 0x84, 0xed, 0xee, 0x78, 0x2a, 0xa6, 0xfa, 0x3d,
  18943. 0x06, 0xa8, 0xa7, 0x5d, 0x97, 0x78, 0xd6, 0x58
  18944. },
  18945. {
  18946. 0xf4, 0x57, 0xac, 0x24, 0x7a, 0xfb, 0x7c, 0x3b, 0xb6, 0x39, 0x17, 0x14,
  18947. 0xd9, 0xd4, 0x58, 0x4d, 0x46, 0xd5, 0x1b, 0xde, 0xf7, 0x9d, 0x06, 0xee,
  18948. 0x8d, 0x1a, 0x2c, 0x25, 0x6d, 0x64, 0xde, 0x89
  18949. },
  18950. {
  18951. 0xb6, 0x00, 0xce, 0x63, 0xed, 0x65, 0x8b, 0x66, 0x66, 0x42, 0xc6, 0xbd,
  18952. 0x89, 0xc4, 0x71, 0x6f, 0xce, 0x28, 0xb2, 0xac, 0x97, 0x07, 0x5b, 0xea,
  18953. 0xb8, 0x1d, 0x4c, 0xeb, 0x9e, 0x71, 0x07, 0x8f
  18954. },
  18955. {
  18956. 0xf8, 0x92, 0xc8, 0xba, 0xe7, 0x83, 0xfe, 0x68, 0xe4, 0xd6, 0x5e, 0xcb,
  18957. 0xb3, 0xef, 0x49, 0xd0, 0xe7, 0xb1, 0xac, 0xcb, 0x39, 0x19, 0xfd, 0xa7,
  18958. 0xf7, 0xca, 0xab, 0x1e, 0x42, 0x14, 0xd8, 0xe7
  18959. },
  18960. {
  18961. 0x32, 0x4a, 0x1a, 0xad, 0xe2, 0xbb, 0x55, 0x8a, 0xdd, 0xe9, 0xa5, 0x2a,
  18962. 0x46, 0x5e, 0x6c, 0x83, 0x66, 0x27, 0x27, 0x94, 0xdd, 0x68, 0x59, 0xa0,
  18963. 0xbb, 0xe8, 0x31, 0x7c, 0x39, 0xd7, 0xfd, 0x6d
  18964. },
  18965. {
  18966. 0x58, 0xbc, 0x6c, 0x5b, 0x24, 0xad, 0x82, 0xb3, 0xcc, 0xc7, 0xd1, 0xa1,
  18967. 0xaa, 0x2b, 0x98, 0x9f, 0x2f, 0x7e, 0xa9, 0x63, 0xc2, 0x8e, 0xb6, 0x06,
  18968. 0xc2, 0x2b, 0x74, 0x4b, 0x79, 0x19, 0x7e, 0x2e
  18969. }
  18970. },
  18971. { /* 11 */
  18972. WC_HASH_TYPE_SHA256, 33, 0,
  18973. { /* PSK */
  18974. 0x3d, 0x39, 0x49, 0x36, 0x98, 0xc5, 0xfd, 0xcd,
  18975. 0xa0, 0x17, 0xbd, 0x65, 0x0a, 0xdb, 0xd4, 0x07,
  18976. 0x56, 0xa2, 0x7b, 0xb8, 0x2a, 0x7e, 0xfb, 0x26,
  18977. 0x74, 0xe1, 0xbc, 0x08, 0x4b, 0xf0, 0x30, 0x14,
  18978. 0x12
  18979. },
  18980. { 0 }, /* DHE */
  18981. { /* Hello 1 */
  18982. 0xb7, 0x44, 0x74, 0x6c, 0x57, 0x1f, 0xf3, 0x84,
  18983. 0x8f, 0x63, 0xfb, 0x8c, 0x94, 0x6c, 0x16, 0x68,
  18984. 0x4b, 0xe1, 0xb5, 0xb5, 0x2a, 0x4e, 0x5f, 0xdf,
  18985. 0x4b, 0x53, 0xb2, 0x35, 0xfc, 0x30, 0xf1, 0x36
  18986. },
  18987. { /* Hello 2 */
  18988. 0xe6, 0x4f, 0x3a, 0x4f, 0xd7, 0xe0, 0x64, 0xd4,
  18989. 0x69, 0x50, 0xe4, 0x8b, 0xba, 0xbc, 0x47, 0x74,
  18990. 0xa7, 0x9b, 0x40, 0x91, 0x8f, 0xa8, 0x72, 0x22,
  18991. 0x97, 0xad, 0x43, 0xa7, 0x11, 0x86, 0xb5, 0x72
  18992. },
  18993. { /* Finished 1 */
  18994. 0x5f, 0xa6, 0x10, 0xe2, 0xa3, 0x99, 0x0b, 0x5e,
  18995. 0x57, 0xee, 0xc3, 0x3a, 0x8e, 0x04, 0xf3, 0x0e,
  18996. 0x58, 0x02, 0x09, 0xb2, 0x7e, 0x2d, 0xc6, 0xd2,
  18997. 0x08, 0xae, 0x68, 0x0a, 0x55, 0xa5, 0xda, 0x51
  18998. },
  18999. { /* Finished 2 */
  19000. 0xfc, 0x5b, 0xc0, 0x7e, 0x1b, 0xaa, 0xc0, 0xb4,
  19001. 0x34, 0x85, 0x49, 0x8e, 0x16, 0x31, 0x98, 0xdf,
  19002. 0x10, 0x54, 0x22, 0xda, 0x1e, 0x6b, 0x51, 0xf6,
  19003. 0x97, 0x57, 0xa0, 0x7a, 0x92, 0xe7, 0x47, 0x52
  19004. },
  19005. {
  19006. 0x80, 0xfa, 0x36, 0x30, 0xb8, 0x65, 0xb3, 0x2a, 0x1d, 0x68, 0x91, 0x06,
  19007. 0x98, 0xa0, 0x17, 0x8f, 0xee, 0xb7, 0x9e, 0x3d, 0xd8, 0x84, 0x99, 0x30,
  19008. 0xb9, 0xd6, 0x09, 0x25, 0x5e, 0xfb, 0x8f, 0xd3 },
  19009. {
  19010. 0xa9, 0x89, 0x29, 0x70, 0xe4, 0x55, 0xec, 0x97, 0xfb, 0x24, 0x5b, 0xf9,
  19011. 0xf1, 0xa3, 0x19, 0x3d, 0xf1, 0x31, 0x14, 0xcd, 0x2a, 0xed, 0x21, 0xc8,
  19012. 0xb1, 0x53, 0xad, 0x11, 0x0b, 0x9e, 0x5a, 0xee },
  19013. {
  19014. 0x72, 0xad, 0x8d, 0x7f, 0xfc, 0xb7, 0x68, 0xda, 0x27, 0x60, 0x37, 0xa3,
  19015. 0x4a, 0x63, 0xe8, 0xa5, 0xc8, 0xcd, 0x36, 0x6a, 0x77, 0x99, 0x0d, 0xa9,
  19016. 0xb1, 0x5b, 0x2f, 0x47, 0x2e, 0x22, 0xa7, 0x5e },
  19017. {
  19018. 0x95, 0x6e, 0x85, 0x09, 0xe5, 0x04, 0x88, 0x14, 0x28, 0x8d, 0xdf, 0xe6,
  19019. 0x0d, 0x0f, 0x0d, 0x6b, 0x4e, 0x66, 0x1c, 0x03, 0xb9, 0xaa, 0x2d, 0x45,
  19020. 0x56, 0x67, 0x5c, 0x55, 0x29, 0xd6, 0x89, 0xd0 },
  19021. {
  19022. 0xe8, 0xf2, 0x14, 0xf9, 0x9b, 0x2b, 0x9f, 0x24, 0x2b, 0x37, 0xbe, 0x86,
  19023. 0xdb, 0x23, 0x4b, 0xbe, 0x39, 0x57, 0xe8, 0xa9, 0xa5, 0xee, 0x08, 0xf2,
  19024. 0x75, 0x58, 0xdb, 0xd9, 0x51, 0xc1, 0x46, 0x02 },
  19025. {
  19026. 0x3d, 0x19, 0xaf, 0xa3, 0x0b, 0x21, 0xf7, 0x3d, 0xe7, 0x37, 0x6e, 0x32,
  19027. 0x13, 0x48, 0x9d, 0xea, 0xe0, 0x90, 0xbf, 0x64, 0x48, 0xf7, 0x1e, 0xcc,
  19028. 0xf0, 0xbc, 0x92, 0xd7, 0x8a, 0x4a, 0xa8, 0xc1 },
  19029. {
  19030. 0x16, 0x35, 0xb1, 0x66, 0x28, 0xa3, 0x3e, 0x19, 0xf5, 0x2d, 0x92, 0x22,
  19031. 0x95, 0x48, 0xe8, 0x34, 0x7b, 0x30, 0x50, 0xa2, 0xa0, 0xd9, 0xc2, 0x59,
  19032. 0x39, 0xf9, 0x8c, 0x69, 0xf2, 0x2a, 0xb9, 0xff },
  19033. {
  19034. 0x32, 0x71, 0xa6, 0x87, 0x0c, 0x97, 0x42, 0x07, 0xdd, 0x5f, 0xc9, 0x44,
  19035. 0xa5, 0x7c, 0x50, 0x14, 0xfd, 0xe7, 0x5f, 0x8b, 0xd3, 0x2f, 0xdc, 0x9b,
  19036. 0xa9, 0x93, 0x22, 0x19, 0xe6, 0xf2, 0x0c, 0xd8 }
  19037. },
  19038. #ifdef WOLFSSL_SHA384
  19039. { /* 26 */
  19040. WC_HASH_TYPE_SHA384, 35, 35,
  19041. { /* PSK */
  19042. 0x62, 0x83, 0x25, 0xc7, 0xcc, 0x08, 0x5e, 0x63,
  19043. 0x64, 0x56, 0xf0, 0xc6, 0x88, 0x27, 0x5a, 0x5b,
  19044. 0x68, 0x59, 0x0b, 0x14, 0x55, 0x13, 0x2e, 0xfd,
  19045. 0x8f, 0x28, 0x5b, 0x3d, 0xe3, 0xad, 0x67, 0xe4,
  19046. 0x68, 0xba, 0xf9
  19047. },
  19048. { /* DHE */
  19049. 0xa8, 0xb1, 0xab, 0xd8, 0xc8, 0x5b, 0x52, 0xdf,
  19050. 0x7f, 0x49, 0x10, 0xf4, 0xa1, 0x31, 0xd1, 0x91,
  19051. 0x36, 0xc1, 0x87, 0x5d, 0x42, 0x2a, 0xe7, 0x1d,
  19052. 0x2c, 0x29, 0x3d, 0x40, 0x64, 0x61, 0x63, 0x76,
  19053. 0xd8, 0x66, 0xac
  19054. },
  19055. { /* Hello 1 */
  19056. 0x6f, 0xc6, 0x4c, 0xe1, 0xc6, 0x68, 0x34, 0x8c,
  19057. 0x0a, 0xe1, 0xf8, 0xb8, 0x3e, 0xd4, 0xf8, 0x0b,
  19058. 0x54, 0x50, 0xe4, 0xc5, 0x4a, 0x33, 0x7d, 0xbd,
  19059. 0x90, 0xd2, 0xa2, 0xb9, 0xb7, 0x92, 0xed, 0xab,
  19060. 0x14, 0xf1, 0xe4, 0x86, 0x22, 0x67, 0xd7, 0x44,
  19061. 0x03, 0x21, 0xdc, 0x51, 0x52, 0x7f, 0x35, 0x80
  19062. },
  19063. { /* Hello 2 */
  19064. 0x3e, 0xcf, 0x2f, 0xc3, 0x87, 0xba, 0xc5, 0xbd,
  19065. 0x7c, 0xe8, 0x35, 0x5b, 0x95, 0x51, 0x30, 0x3b,
  19066. 0x08, 0xcc, 0x2a, 0x7d, 0xb5, 0x74, 0x7c, 0x16,
  19067. 0xb3, 0x0b, 0xe7, 0x61, 0xa3, 0x7c, 0x6c, 0xbd,
  19068. 0x39, 0x74, 0xfd, 0x1e, 0x4c, 0xff, 0xc8, 0xcc,
  19069. 0xa0, 0xef, 0x29, 0x4d, 0x94, 0xaa, 0x55, 0x6f,
  19070. },
  19071. { /* Finished 1 */
  19072. 0x06, 0xc1, 0x47, 0x78, 0x66, 0x53, 0x6f, 0x24,
  19073. 0x94, 0x61, 0x69, 0xec, 0xd8, 0x60, 0x31, 0x2f,
  19074. 0xbf, 0xd6, 0x8a, 0x29, 0x17, 0xff, 0xa3, 0x88,
  19075. 0x13, 0x09, 0x8c, 0x9d, 0x6c, 0x64, 0x84, 0x48,
  19076. 0x44, 0xdd, 0x2d, 0x29, 0x4d, 0xe6, 0x98, 0x2b,
  19077. 0x45, 0x3b, 0x84, 0x33, 0x79, 0xb2, 0x75, 0x68
  19078. },
  19079. { /* Finished 2 */
  19080. 0x28, 0x1e, 0x18, 0xf7, 0x9c, 0x32, 0xa9, 0xbf,
  19081. 0x0c, 0x24, 0x58, 0x21, 0xce, 0xbc, 0xf2, 0x44,
  19082. 0xb1, 0x18, 0xaf, 0x9d, 0xd9, 0x20, 0xf9, 0xf4,
  19083. 0xed, 0xcc, 0x53, 0x82, 0x66, 0x5c, 0x46, 0x94,
  19084. 0x8c, 0x36, 0x5e, 0xca, 0x9f, 0xd8, 0x9a, 0xd3,
  19085. 0xf0, 0xe1, 0x53, 0x71, 0xdd, 0x19, 0x1e, 0x59
  19086. },
  19087. {
  19088. 0xd0, 0xef, 0xa8, 0xcb, 0x5b, 0x14, 0x0f, 0x0a, 0x62, 0xba, 0x5a, 0xb1,
  19089. 0xc5, 0xb5, 0x3f, 0x11, 0xda, 0xa1, 0x0c, 0x9c, 0xb4, 0x32, 0x48, 0x4e,
  19090. 0xfa, 0x84, 0x4f, 0xe4, 0xe7, 0x91, 0x8f, 0x42, 0x3f, 0xc7, 0x4e, 0xd3,
  19091. 0x83, 0x3d, 0x7f, 0x70, 0x12, 0xee, 0x9a, 0x37, 0x01, 0xbb, 0x14, 0xd3
  19092. },
  19093. {
  19094. 0x48, 0x6f, 0x77, 0x1d, 0x39, 0x1b, 0xa5, 0x9a, 0x76, 0xd9, 0x1d, 0x7d,
  19095. 0xb3, 0xd9, 0xb9, 0x78, 0x35, 0x0f, 0xd0, 0xe1, 0x07, 0x1f, 0x8d, 0xe5,
  19096. 0x75, 0x00, 0xda, 0xc0, 0x19, 0x01, 0xfb, 0x08, 0x35, 0xe7, 0x18, 0x8f,
  19097. 0xf0, 0x19, 0xfb, 0x46, 0xf6, 0xa5, 0x77, 0x0e, 0x90, 0x38, 0x8b, 0x15
  19098. },
  19099. {
  19100. 0x80, 0x8c, 0xa7, 0x24, 0x97, 0xf9, 0xd3, 0x52, 0xb0, 0x69, 0x9d, 0x4b,
  19101. 0xa4, 0x19, 0x4a, 0xb1, 0x46, 0x53, 0x3a, 0xc8, 0xe4, 0x02, 0x69, 0xf2,
  19102. 0xe7, 0xb6, 0x1d, 0x33, 0x51, 0xcc, 0x14, 0x40, 0x4a, 0xb0, 0xe7, 0x58,
  19103. 0x84, 0xba, 0xc2, 0x14, 0x58, 0x6b, 0xb9, 0xdc, 0x50, 0x98, 0x67, 0x01
  19104. },
  19105. {
  19106. 0xb1, 0xa8, 0xc0, 0x06, 0xb3, 0x2e, 0xa7, 0x8a, 0x6a, 0x12, 0x88, 0x00,
  19107. 0x65, 0x88, 0x9c, 0x5d, 0x35, 0xee, 0xe5, 0x51, 0x0b, 0x62, 0xf8, 0x67,
  19108. 0xe5, 0xef, 0x15, 0x1f, 0x23, 0x02, 0x74, 0x08, 0x9c, 0xc8, 0xba, 0x27,
  19109. 0x5d, 0x32, 0x19, 0x6f, 0x6d, 0x5d, 0x72, 0x5e, 0x15, 0xde, 0x30, 0xc3
  19110. },
  19111. {
  19112. 0xfd, 0xce, 0xf5, 0x65, 0x45, 0x84, 0xfb, 0x8c, 0x79, 0xa4, 0x6c, 0x1b,
  19113. 0x0e, 0x1b, 0xfd, 0x26, 0xa2, 0x53, 0xf4, 0x4e, 0x00, 0x4d, 0x4b, 0x0b,
  19114. 0x24, 0x6d, 0x35, 0x35, 0xd9, 0x97, 0x70, 0xc5, 0xf4, 0xee, 0xe3, 0xba,
  19115. 0x31, 0x1e, 0x2a, 0x42, 0xcb, 0xdf, 0x40, 0xb1, 0x14, 0xb8, 0x53, 0xce
  19116. },
  19117. {
  19118. 0xbb, 0xb3, 0x26, 0x7c, 0x22, 0x21, 0x9b, 0x72, 0x32, 0xa1, 0x97, 0xfb,
  19119. 0x78, 0x8c, 0xbe, 0x3d, 0x71, 0x45, 0xb8, 0xf5, 0x24, 0x8f, 0x0f, 0xac,
  19120. 0x42, 0x5b, 0x81, 0xe8, 0xd0, 0x71, 0x4a, 0xcb, 0x32, 0x3f, 0x03, 0xfb,
  19121. 0xec, 0x6a, 0x1f, 0x76, 0x80, 0x65, 0x01, 0x7a, 0x3d, 0xce, 0xc4, 0xdf
  19122. },
  19123. {
  19124. 0x3f, 0xcf, 0x2f, 0x63, 0x94, 0x94, 0x99, 0xfd, 0x04, 0x3a, 0x89, 0x83,
  19125. 0xcf, 0x06, 0x05, 0xec, 0x20, 0x3e, 0x5f, 0x51, 0x9d, 0x6e, 0x4a, 0xc6,
  19126. 0xf1, 0x2b, 0x37, 0x17, 0x34, 0x72, 0x6e, 0x1d, 0x2a, 0xfd, 0xc7, 0x73,
  19127. 0xb5, 0x07, 0x22, 0x81, 0x32, 0x2e, 0x21, 0x85, 0xaf, 0x10, 0xb2, 0x73
  19128. },
  19129. {
  19130. 0x52, 0x0c, 0x3d, 0x2e, 0x2d, 0x4a, 0x11, 0xae, 0x96, 0x78, 0xe9, 0x5b,
  19131. 0xd8, 0x0f, 0x6c, 0xf4, 0xbd, 0x96, 0x13, 0x55, 0x88, 0xdd, 0xa3, 0x67,
  19132. 0x36, 0x86, 0x1e, 0x0b, 0x36, 0x41, 0xec, 0xf6, 0x04, 0xb2, 0xc4, 0x16,
  19133. 0xbc, 0x2c, 0xdb, 0x30, 0x02, 0x94, 0xd4, 0x42, 0xbf, 0x38, 0xee, 0x9d
  19134. }
  19135. },
  19136. { /* 36 */
  19137. WC_HASH_TYPE_SHA384, 0, 33,
  19138. { 0 }, /* PSK */
  19139. { /* DHE */
  19140. 0xd3, 0x00, 0x72, 0x9a, 0xa8, 0xc5, 0xf3, 0xc4,
  19141. 0xf1, 0xa0, 0x26, 0x89, 0x65, 0x70, 0xc7, 0x0b,
  19142. 0x77, 0xbb, 0xe1, 0x4b, 0x2b, 0xa8, 0x4f, 0xa6,
  19143. 0x09, 0x4b, 0xba, 0x45, 0x36, 0x15, 0xee, 0x68,
  19144. 0xfd
  19145. },
  19146. { /* Hello 1 */
  19147. 0x10, 0x9d, 0x8b, 0xa2, 0x93, 0xe7, 0xd3, 0xb9,
  19148. 0xb4, 0x0f, 0xeb, 0x6a, 0xb9, 0x69, 0xcb, 0x39,
  19149. 0x16, 0x29, 0xcc, 0xd3, 0xcc, 0x1a, 0x4c, 0x1b,
  19150. 0x53, 0x7c, 0x33, 0x88, 0x06, 0xbc, 0x0a, 0x02,
  19151. 0xa0, 0xbe, 0x62, 0xc0, 0xe6, 0x5e, 0x97, 0x5b,
  19152. 0x6a, 0xa1, 0x98, 0xf3, 0xd2, 0x1e, 0xcd, 0xc5
  19153. },
  19154. { /* Hello 2 */
  19155. 0x74, 0xc0, 0x07, 0x2c, 0xc1, 0x63, 0xcc, 0x11,
  19156. 0xad, 0x1a, 0x55, 0x63, 0xbc, 0x20, 0x77, 0x96,
  19157. 0x30, 0x1c, 0x68, 0x45, 0x1e, 0x9b, 0xa7, 0xb4,
  19158. 0xf3, 0x04, 0x45, 0x16, 0x76, 0x55, 0xf9, 0xdf,
  19159. 0x4b, 0x2f, 0x1a, 0xdf, 0x5a, 0xb0, 0x93, 0xc9,
  19160. 0xab, 0xf5, 0x32, 0x47, 0x79, 0x9c, 0x01, 0xeb
  19161. },
  19162. { /* Finished 1 */
  19163. 0x27, 0x08, 0x8e, 0xa5, 0xf1, 0x30, 0xe1, 0xd6,
  19164. 0x4f, 0xa2, 0x9e, 0x3b, 0x03, 0x2d, 0x2e, 0xa3,
  19165. 0x84, 0x75, 0x51, 0x3a, 0xc3, 0xf6, 0xee, 0x2e,
  19166. 0x37, 0x0c, 0xe3, 0x28, 0x46, 0xa5, 0x2d, 0xc7,
  19167. 0xf0, 0x64, 0x78, 0x53, 0x66, 0x43, 0x02, 0xa4,
  19168. 0x7a, 0x43, 0x66, 0x4b, 0xa7, 0xcb, 0x97, 0x16
  19169. },
  19170. { /* Finished 2 */
  19171. 0x1d, 0x0d, 0xf8, 0xe1, 0x81, 0xa5, 0xbd, 0xa8,
  19172. 0x6f, 0x9d, 0x01, 0xa4, 0x9a, 0x92, 0xe2, 0xef,
  19173. 0x08, 0xab, 0xef, 0x3e, 0x2d, 0xd4, 0x82, 0xac,
  19174. 0x68, 0x9d, 0xe0, 0x54, 0x17, 0xde, 0x1a, 0xed,
  19175. 0x57, 0xcb, 0xd9, 0x2d, 0xc8, 0xbc, 0x93, 0xe6,
  19176. 0xa3, 0xec, 0xde, 0xee, 0xa1, 0x1c, 0x41, 0x85
  19177. },
  19178. {
  19179. 0x7f, 0x1f, 0xe6, 0x7b, 0xd8, 0xf5, 0x2b, 0x37, 0xbe, 0xb7, 0xd0, 0x37,
  19180. 0xce, 0x46, 0xad, 0x04, 0x2f, 0xc7, 0xdb, 0xc9, 0x9a, 0xb6, 0x00, 0x3f,
  19181. 0xc1, 0x97, 0xe9, 0x5c, 0x5e, 0x14, 0xd1, 0x38, 0x4d, 0x55, 0xe1, 0x07,
  19182. 0xb5, 0x85, 0x6d, 0xfa, 0xa7, 0x66, 0xad, 0xfa, 0xb6, 0xad, 0x29, 0x44
  19183. },
  19184. {
  19185. 0x4e, 0x6b, 0x20, 0x99, 0x55, 0x1b, 0x21, 0x89, 0xb6, 0x70, 0xdb, 0xe8,
  19186. 0xa7, 0x16, 0x55, 0xf2, 0x93, 0x13, 0x90, 0x7d, 0xfa, 0x62, 0x65, 0x53,
  19187. 0xa0, 0x97, 0xe9, 0xb4, 0xc0, 0xf1, 0xc9, 0x1a, 0x67, 0xdd, 0xca, 0x57,
  19188. 0xbc, 0xca, 0x39, 0xe6, 0x39, 0x6b, 0x63, 0x47, 0x25, 0x08, 0x3a, 0xd7
  19189. },
  19190. {
  19191. 0x35, 0x0d, 0xac, 0xd8, 0x10, 0x6a, 0x46, 0x50, 0x66, 0xae, 0x02, 0xc9,
  19192. 0xde, 0x13, 0x48, 0xce, 0x53, 0xd4, 0x92, 0x62, 0xc5, 0x65, 0x10, 0x08,
  19193. 0xc2, 0xc2, 0x82, 0xed, 0x9d, 0xc9, 0x6f, 0xa8, 0xc3, 0xc1, 0x0b, 0x7c,
  19194. 0xe1, 0x97, 0x85, 0xd6, 0x46, 0x29, 0x0e, 0x42, 0x51, 0xc1, 0x35, 0xcf
  19195. },
  19196. {
  19197. 0x3d, 0x5d, 0x84, 0xbd, 0x16, 0x46, 0x34, 0xb3, 0xf6, 0x31, 0x49, 0x3e,
  19198. 0x8d, 0xdc, 0xcb, 0x8c, 0x6a, 0x42, 0xf4, 0x88, 0xfc, 0x19, 0xfa, 0xa2,
  19199. 0x25, 0xc7, 0xa0, 0xa4, 0xca, 0xf0, 0xea, 0x2d, 0xe8, 0xc4, 0x02, 0x14,
  19200. 0x63, 0xfb, 0xd3, 0x7b, 0x51, 0x1c, 0xce, 0xca, 0xa3, 0xc3, 0xe4, 0xa5
  19201. },
  19202. {
  19203. 0x7c, 0x3a, 0x55, 0x92, 0x2e, 0xdd, 0x75, 0xdd, 0x76, 0x54, 0x4a, 0x9f,
  19204. 0xd0, 0xa2, 0x88, 0x83, 0xe9, 0x27, 0xda, 0x30, 0xe9, 0x96, 0x58, 0xc5,
  19205. 0xb7, 0x56, 0xfc, 0x4b, 0xb8, 0x5d, 0xee, 0x46, 0x70, 0x4e, 0x1b, 0x06,
  19206. 0x86, 0xaf, 0x48, 0x5c, 0x17, 0x35, 0xfa, 0x69, 0xc2, 0x4d, 0xfb, 0x09
  19207. },
  19208. {
  19209. 0x00, 0x0e, 0x28, 0x51, 0xc1, 0x7f, 0x41, 0x89, 0x6f, 0x9a, 0xca, 0x15,
  19210. 0xee, 0xed, 0x43, 0xca, 0x6d, 0x65, 0x6f, 0x51, 0x18, 0x6c, 0x08, 0x4b,
  19211. 0x77, 0xca, 0x75, 0xc4, 0xc3, 0xde, 0x29, 0x41, 0x8b, 0xaf, 0xa7, 0x1c,
  19212. 0x28, 0x37, 0xa0, 0xa0, 0x74, 0x8e, 0x09, 0x42, 0x7a, 0x1b, 0x68, 0xdb
  19213. },
  19214. {
  19215. 0x14, 0x8f, 0xab, 0x28, 0x64, 0xea, 0x45, 0x88, 0xdb, 0xc1, 0xc6, 0xa0,
  19216. 0x48, 0xdf, 0x15, 0xd0, 0x28, 0x07, 0x2d, 0x6c, 0xb8, 0x42, 0xbb, 0x60,
  19217. 0x02, 0x08, 0x9e, 0x29, 0x9b, 0x8d, 0xd6, 0x1c, 0xaf, 0xf2, 0x1a, 0xdc,
  19218. 0xf0, 0x78, 0x0b, 0x4d, 0x90, 0xa1, 0x0c, 0xb3, 0x13, 0xde, 0xca, 0x5a
  19219. },
  19220. {
  19221. 0x4d, 0x80, 0x7d, 0x0b, 0xb9, 0x00, 0x6f, 0x65, 0x51, 0x65, 0x23, 0xde,
  19222. 0x72, 0xdc, 0x4f, 0x04, 0xa5, 0xa2, 0x90, 0x45, 0x51, 0x9e, 0xd0, 0x3a,
  19223. 0xe4, 0xd7, 0x78, 0xa3, 0x0f, 0x2d, 0x65, 0x12, 0xad, 0xc8, 0x92, 0x30,
  19224. 0x79, 0x9d, 0x9d, 0x08, 0x7a, 0x9c, 0x9f, 0x83, 0xb1, 0xca, 0x59, 0x56
  19225. }
  19226. },
  19227. { /* 41 */
  19228. WC_HASH_TYPE_SHA384, 33, 0,
  19229. { /* PSK */
  19230. 0xa4, 0x8b, 0x1b, 0x5f, 0xd0, 0xea, 0x75, 0x62,
  19231. 0x06, 0x4d, 0x68, 0x40, 0x85, 0x20, 0x45, 0x95,
  19232. 0x4a, 0x00, 0xca, 0x05, 0xeb, 0xd4, 0x1d, 0x48,
  19233. 0x81, 0x89, 0xe8, 0x86, 0x43, 0xfa, 0x28, 0x17,
  19234. 0x12
  19235. },
  19236. { 0 }, /* DHE */
  19237. { /* Hello 1 */
  19238. 0x03, 0x7c, 0x33, 0x75, 0xdc, 0xc5, 0x46, 0x3a,
  19239. 0x0d, 0x56, 0xc6, 0xfb, 0xab, 0x1e, 0x1d, 0xda,
  19240. 0x59, 0xc2, 0xb2, 0xb1, 0x7c, 0x48, 0x9b, 0x06,
  19241. 0x0a, 0x5a, 0xbb, 0xf8, 0x98, 0x53, 0x78, 0x2d,
  19242. 0xd2, 0xcc, 0x87, 0x68, 0x25, 0xdd, 0x88, 0x22,
  19243. 0xcd, 0xb7, 0x74, 0x55, 0x21, 0xf9, 0x34, 0x98
  19244. },
  19245. { /* Hello 2 */
  19246. 0x03, 0xb4, 0xfb, 0xcc, 0x28, 0x2c, 0xc1, 0x70,
  19247. 0x42, 0x73, 0x57, 0xac, 0xdb, 0x47, 0x71, 0xf6,
  19248. 0x2e, 0x11, 0x8a, 0x5b, 0x47, 0x2f, 0x02, 0x54,
  19249. 0x95, 0x34, 0xed, 0x5f, 0x19, 0xc1, 0x75, 0xe0,
  19250. 0x76, 0xad, 0xb0, 0x90, 0x57, 0xcd, 0xfd, 0xd7,
  19251. 0x58, 0x1f, 0x0d, 0x6b, 0x9e, 0x51, 0x3c, 0x08
  19252. },
  19253. { /* Finished 1 */
  19254. 0x2b, 0x50, 0xd9, 0xa7, 0x43, 0x24, 0xda, 0x2c,
  19255. 0x7a, 0xaa, 0x0e, 0x37, 0xd7, 0x6b, 0x2c, 0xab,
  19256. 0x8e, 0xb2, 0xfe, 0x31, 0x1b, 0xa8, 0x12, 0x59,
  19257. 0x5b, 0x7b, 0xdc, 0x3e, 0xa7, 0x86, 0xa5, 0x48,
  19258. 0xe4, 0x46, 0x2b, 0x4c, 0xc1, 0x66, 0x4b, 0xf3,
  19259. 0x2a, 0x99, 0x93, 0x08, 0xbc, 0x3d, 0x08, 0x76
  19260. },
  19261. { /* Finished 2 */
  19262. 0x7c, 0x34, 0xc8, 0x56, 0x17, 0xf1, 0x62, 0x1c,
  19263. 0x9f, 0x0b, 0xeb, 0xfd, 0x69, 0x72, 0x51, 0xc5,
  19264. 0xfa, 0x74, 0x87, 0xc9, 0xbd, 0x50, 0xe9, 0x48,
  19265. 0xa7, 0x3c, 0x94, 0x3e, 0x06, 0x7d, 0xe8, 0x8e,
  19266. 0xc1, 0xd1, 0x08, 0x1f, 0x5d, 0x48, 0x8a, 0x25,
  19267. 0xfc, 0xea, 0xe7, 0xd9, 0xd4, 0xd0, 0xf9, 0xad
  19268. },
  19269. {
  19270. 0x4b, 0x0b, 0xed, 0xb9, 0xc8, 0xb8, 0xa8, 0x1e, 0xb0, 0x81, 0x76, 0xd5,
  19271. 0x33, 0x22, 0x71, 0x33, 0x3a, 0x85, 0x19, 0x67, 0x7e, 0x91, 0x37, 0xf2,
  19272. 0xa6, 0x11, 0x22, 0xdf, 0x41, 0x04, 0x3d, 0xa9, 0x13, 0xb9, 0xb2, 0xb1,
  19273. 0xbb, 0xd8, 0xef, 0x23, 0x7c, 0xc2, 0xab, 0x70, 0x1b, 0x51, 0x9f, 0xc9
  19274. },
  19275. {
  19276. 0xeb, 0x96, 0x10, 0x8c, 0x7d, 0x92, 0xea, 0x80, 0x86, 0xb2, 0xf8, 0x27,
  19277. 0xf2, 0x9a, 0x09, 0xc1, 0x7c, 0x09, 0x43, 0xbc, 0xfe, 0xc8, 0x75, 0xe0,
  19278. 0x97, 0xe7, 0x6d, 0xd5, 0xb2, 0x3c, 0xed, 0x12, 0xb7, 0x74, 0x0e, 0xe3,
  19279. 0xb6, 0xe0, 0xba, 0xe1, 0x8d, 0x89, 0xcf, 0x4f, 0x57, 0xf6, 0x6d, 0x90
  19280. },
  19281. {
  19282. 0x22, 0xb0, 0x39, 0x34, 0xb6, 0x6c, 0x2d, 0x7a, 0x97, 0x1c, 0x5d, 0xcc,
  19283. 0x78, 0x84, 0x71, 0xbb, 0xc6, 0x7b, 0xb6, 0xbc, 0xcc, 0x0b, 0xf8, 0xac,
  19284. 0x8e, 0xd7, 0x20, 0xbd, 0xbe, 0x32, 0xf0, 0xd6, 0xe9, 0x69, 0x13, 0xf2,
  19285. 0x9a, 0xce, 0xfe, 0x86, 0xd3, 0xee, 0xba, 0x69, 0x51, 0xb6, 0x77, 0x56
  19286. },
  19287. {
  19288. 0x16, 0xfd, 0xda, 0xf3, 0x5e, 0xb9, 0xa6, 0x17, 0x24, 0xb2, 0x16, 0x9f,
  19289. 0xb6, 0x59, 0x13, 0x0f, 0x25, 0x5a, 0xf1, 0x5b, 0x5f, 0xe4, 0x54, 0x2a,
  19290. 0xa7, 0xbf, 0x29, 0xaf, 0x5a, 0x77, 0xf4, 0x4f, 0x25, 0xba, 0x94, 0xad,
  19291. 0x6b, 0x91, 0x3b, 0xe7, 0xd5, 0x73, 0x0d, 0xff, 0xaa, 0xe3, 0x72, 0x2c
  19292. },
  19293. {
  19294. 0x22, 0xb4, 0x94, 0xc0, 0x53, 0xd7, 0x82, 0x06, 0x38, 0x9d, 0x4a, 0xa0,
  19295. 0x3f, 0xf1, 0x5f, 0x6e, 0x23, 0x8d, 0x09, 0x62, 0xbf, 0x6f, 0x7c, 0x84,
  19296. 0xc6, 0x3e, 0x15, 0xad, 0x18, 0x37, 0x76, 0x29, 0xc7, 0xd6, 0x68, 0x0c,
  19297. 0x1e, 0xc6, 0x93, 0x31, 0xef, 0x85, 0x69, 0x30, 0x68, 0xf0, 0x1e, 0x37
  19298. },
  19299. {
  19300. 0x6d, 0x4d, 0x20, 0xaf, 0x47, 0xe8, 0x1b, 0xfa, 0xd0, 0xb6, 0xc8, 0x97,
  19301. 0xd1, 0x03, 0xfc, 0x9d, 0x59, 0xa0, 0x68, 0x9d, 0xe9, 0x17, 0x8b, 0xce,
  19302. 0x48, 0x2c, 0x77, 0x8a, 0x22, 0x4b, 0x5c, 0x54, 0x22, 0xa1, 0x15, 0x12,
  19303. 0xe1, 0x07, 0x8e, 0x15, 0xd8, 0x7b, 0x16, 0x65, 0x99, 0x6b, 0xcb, 0x71
  19304. },
  19305. {
  19306. 0x79, 0x64, 0x79, 0xdd, 0x75, 0x5c, 0x6f, 0x98, 0xac, 0x03, 0xe0, 0xcd,
  19307. 0x92, 0xba, 0x0e, 0x2d, 0xb4, 0xd1, 0x8b, 0x97, 0xd0, 0x85, 0xbb, 0x2e,
  19308. 0x4f, 0x26, 0x93, 0xf5, 0x1d, 0xf3, 0xd2, 0x43, 0x4f, 0xd2, 0x47, 0xaa,
  19309. 0x91, 0x1e, 0xf3, 0x67, 0x10, 0x18, 0x2c, 0xb9, 0x01, 0xba, 0x10, 0x9f
  19310. },
  19311. {
  19312. 0x79, 0xb6, 0x9c, 0xbe, 0xf1, 0x6a, 0xb0, 0x92, 0xa0, 0x29, 0x52, 0x61,
  19313. 0xf1, 0xcd, 0x3a, 0x67, 0xe1, 0x6b, 0xb8, 0x9d, 0x0d, 0x95, 0xb6, 0x03,
  19314. 0x80, 0x1f, 0xd5, 0x75, 0xb6, 0x1d, 0x79, 0x02, 0x93, 0x43, 0x77, 0xa7,
  19315. 0x9d, 0x2f, 0xc3, 0x84, 0xc6, 0x83, 0x76, 0x16, 0x06, 0x98, 0x7b, 0x79
  19316. }
  19317. },
  19318. #endif /* WOLFSSL_SHA384 */
  19319. };
  19320. const char protocolLabel[] = "tls13 ";
  19321. const char ceTrafficLabel[] = "c e traffic";
  19322. const char eExpMasterLabel[] = "e exp master";
  19323. const char cHsTrafficLabel[] = "c hs traffic";
  19324. const char sHsTrafficLabel[] = "s hs traffic";
  19325. const char cAppTrafficLabel[] = "c ap traffic";
  19326. const char sAppTrafficLabel[] = "s ap traffic";
  19327. const char expMasterLabel[] = "exp master";
  19328. const char resMasterLabel[] = "res master";
  19329. const char derivedLabel[] = "derived";
  19330. WOLFSSL_TEST_SUBROUTINE int tls13_kdf_test(void)
  19331. {
  19332. int ret = 0;
  19333. word32 i;
  19334. word32 tc = sizeof(tls13KdfTestVectors)/sizeof(Tls13KdfTestVector);
  19335. const Tls13KdfTestVector* tv = NULL;
  19336. for (i = 0, tv = tls13KdfTestVectors; i < tc; i++, tv++) {
  19337. byte output[WC_MAX_DIGEST_SIZE];
  19338. byte secret[WC_MAX_DIGEST_SIZE];
  19339. byte salt[WC_MAX_DIGEST_SIZE];
  19340. byte zeroes[WC_MAX_DIGEST_SIZE];
  19341. byte hashZero[WC_MAX_DIGEST_SIZE];
  19342. int hashAlgSz;
  19343. XMEMSET(zeroes, 0, sizeof zeroes);
  19344. hashAlgSz = wc_HashGetDigestSize(tv->hashAlg);
  19345. if (hashAlgSz == BAD_FUNC_ARG) break;
  19346. ret = wc_Hash(tv->hashAlg, NULL, 0, hashZero, hashAlgSz);
  19347. if (ret != 0) break;
  19348. ret = wc_Tls13_HKDF_Extract(secret, NULL, 0,
  19349. (tv->pskSz == 0) ? zeroes : (byte*)tv->psk,
  19350. tv->pskSz, tv->hashAlg);
  19351. if (ret != 0) break;
  19352. ret = wc_Tls13_HKDF_Expand_Label(output, hashAlgSz,
  19353. secret, hashAlgSz,
  19354. (byte*)protocolLabel, (word32)strlen(protocolLabel),
  19355. (byte*)ceTrafficLabel, (word32)strlen(ceTrafficLabel),
  19356. tv->hashHello1, hashAlgSz, tv->hashAlg);
  19357. if (ret != 0) break;
  19358. ret = XMEMCMP(tv->clientEarlyTrafficSecret, output, hashAlgSz);
  19359. if (ret != 0) break;
  19360. ret = wc_Tls13_HKDF_Expand_Label(output, hashAlgSz,
  19361. secret, hashAlgSz,
  19362. (byte*)protocolLabel, (word32)strlen(protocolLabel),
  19363. (byte*)eExpMasterLabel, (word32)strlen(eExpMasterLabel),
  19364. tv->hashHello1, hashAlgSz, tv->hashAlg);
  19365. if (ret != 0) break;
  19366. ret = XMEMCMP(tv->earlyExporterMasterSecret, output, hashAlgSz);
  19367. if (ret != 0) break;
  19368. ret = wc_Tls13_HKDF_Expand_Label(salt, hashAlgSz,
  19369. secret, hashAlgSz,
  19370. (byte*)protocolLabel, (word32)strlen(protocolLabel),
  19371. (byte*)derivedLabel, (word32)strlen(derivedLabel),
  19372. hashZero, hashAlgSz, tv->hashAlg);
  19373. if (ret != 0) break;
  19374. ret = wc_Tls13_HKDF_Extract(secret, salt, hashAlgSz,
  19375. (tv->dheSz == 0) ? zeroes : (byte*)tv->dhe,
  19376. tv->dheSz, tv->hashAlg);
  19377. if (ret != 0) break;
  19378. ret = wc_Tls13_HKDF_Expand_Label(output, hashAlgSz,
  19379. secret, hashAlgSz,
  19380. (byte*)protocolLabel, (word32)strlen(protocolLabel),
  19381. (byte*)cHsTrafficLabel, (word32)strlen(cHsTrafficLabel),
  19382. tv->hashHello2, hashAlgSz, tv->hashAlg);
  19383. if (ret != 0) break;
  19384. ret = XMEMCMP(tv->clientHandshakeTrafficSecret,
  19385. output, hashAlgSz);
  19386. if (ret != 0) break;
  19387. ret = wc_Tls13_HKDF_Expand_Label(output, hashAlgSz,
  19388. secret, hashAlgSz,
  19389. (byte*)protocolLabel, (word32)strlen(protocolLabel),
  19390. (byte*)sHsTrafficLabel, (word32)strlen(sHsTrafficLabel),
  19391. tv->hashHello2, hashAlgSz, tv->hashAlg);
  19392. if (ret != 0) break;
  19393. ret = XMEMCMP(tv->serverHandshakeTrafficSecret, output, hashAlgSz);
  19394. if (ret != 0) break;
  19395. ret = wc_Tls13_HKDF_Expand_Label(salt, hashAlgSz,
  19396. secret, hashAlgSz,
  19397. (byte*)protocolLabel, (word32)strlen(protocolLabel),
  19398. (byte*)derivedLabel, (word32)strlen(derivedLabel),
  19399. hashZero, hashAlgSz, tv->hashAlg);
  19400. if (ret != 0) break;
  19401. ret = wc_Tls13_HKDF_Extract(secret, salt, hashAlgSz,
  19402. zeroes, hashAlgSz, tv->hashAlg);
  19403. if (ret != 0) break;
  19404. ret = wc_Tls13_HKDF_Expand_Label(output, hashAlgSz,
  19405. secret, hashAlgSz,
  19406. (byte*)protocolLabel, (word32)strlen(protocolLabel),
  19407. (byte*)cAppTrafficLabel, (word32)strlen(cAppTrafficLabel),
  19408. tv->hashFinished1, hashAlgSz, tv->hashAlg);
  19409. if (ret != 0) break;
  19410. ret = XMEMCMP(tv->clientApplicationTrafficSecret, output, hashAlgSz);
  19411. if (ret != 0) break;
  19412. ret = wc_Tls13_HKDF_Expand_Label(output, hashAlgSz,
  19413. secret, hashAlgSz,
  19414. (byte*)protocolLabel, (word32)strlen(protocolLabel),
  19415. (byte*)sAppTrafficLabel, (word32)strlen(sAppTrafficLabel),
  19416. tv->hashFinished1, hashAlgSz, tv->hashAlg);
  19417. if (ret != 0) break;
  19418. ret = XMEMCMP(tv->serverApplicationTrafficSecret, output, hashAlgSz);
  19419. if (ret != 0) break;
  19420. ret = wc_Tls13_HKDF_Expand_Label(output, hashAlgSz,
  19421. secret, hashAlgSz,
  19422. (byte*)protocolLabel, (word32)strlen(protocolLabel),
  19423. (byte*)expMasterLabel, (word32)strlen(expMasterLabel),
  19424. tv->hashFinished1, hashAlgSz, tv->hashAlg);
  19425. if (ret != 0) break;
  19426. ret = XMEMCMP(tv->exporterMasterSecret, output, hashAlgSz);
  19427. if (ret != 0) break;
  19428. ret = wc_Tls13_HKDF_Expand_Label(output, hashAlgSz,
  19429. secret, hashAlgSz,
  19430. (byte*)protocolLabel, (word32)strlen(protocolLabel),
  19431. (byte*)resMasterLabel, (word32)strlen(resMasterLabel),
  19432. tv->hashFinished2, hashAlgSz, tv->hashAlg);
  19433. if (ret != 0) break;
  19434. ret = XMEMCMP(tv->resumptionMasterSecret, output, hashAlgSz);
  19435. if (ret != 0) break;
  19436. }
  19437. return ret;
  19438. }
  19439. #endif /* WOLFSSL_TLS13 */
  19440. #if defined(HAVE_ECC) && defined(HAVE_X963_KDF)
  19441. WOLFSSL_TEST_SUBROUTINE int x963kdf_test(void)
  19442. {
  19443. int ret;
  19444. byte kek[128];
  19445. #ifndef NO_SHA
  19446. /* SHA-1, COUNT = 0
  19447. * shared secret length: 192
  19448. * SharedInfo length: 0
  19449. * key data length: 128
  19450. */
  19451. WOLFSSL_SMALL_STACK_STATIC const byte Z[] = {
  19452. 0x1c, 0x7d, 0x7b, 0x5f, 0x05, 0x97, 0xb0, 0x3d,
  19453. 0x06, 0xa0, 0x18, 0x46, 0x6e, 0xd1, 0xa9, 0x3e,
  19454. 0x30, 0xed, 0x4b, 0x04, 0xdc, 0x64, 0xcc, 0xdd
  19455. };
  19456. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  19457. 0xbf, 0x71, 0xdf, 0xfd, 0x8f, 0x4d, 0x99, 0x22,
  19458. 0x39, 0x36, 0xbe, 0xb4, 0x6f, 0xee, 0x8c, 0xcc
  19459. };
  19460. #endif
  19461. #ifndef NO_SHA256
  19462. /* SHA-256, COUNT = 3
  19463. * shared secret length: 192
  19464. * SharedInfo length: 0
  19465. * key data length: 128
  19466. */
  19467. WOLFSSL_SMALL_STACK_STATIC const byte Z2[] = {
  19468. 0xd3, 0x8b, 0xdb, 0xe5, 0xc4, 0xfc, 0x16, 0x4c,
  19469. 0xdd, 0x96, 0x7f, 0x63, 0xc0, 0x4f, 0xe0, 0x7b,
  19470. 0x60, 0xcd, 0xe8, 0x81, 0xc2, 0x46, 0x43, 0x8c
  19471. };
  19472. WOLFSSL_SMALL_STACK_STATIC const byte verify2[] = {
  19473. 0x5e, 0x67, 0x4d, 0xb9, 0x71, 0xba, 0xc2, 0x0a,
  19474. 0x80, 0xba, 0xd0, 0xd4, 0x51, 0x4d, 0xc4, 0x84
  19475. };
  19476. #endif
  19477. #ifdef WOLFSSL_SHA512
  19478. /* SHA-512, COUNT = 0
  19479. * shared secret length: 192
  19480. * SharedInfo length: 0
  19481. * key data length: 128
  19482. */
  19483. WOLFSSL_SMALL_STACK_STATIC const byte Z3[] = {
  19484. 0x87, 0xfc, 0x0d, 0x8c, 0x44, 0x77, 0x48, 0x5b,
  19485. 0xb5, 0x74, 0xf5, 0xfc, 0xea, 0x26, 0x4b, 0x30,
  19486. 0x88, 0x5d, 0xc8, 0xd9, 0x0a, 0xd8, 0x27, 0x82
  19487. };
  19488. WOLFSSL_SMALL_STACK_STATIC const byte verify3[] = {
  19489. 0x94, 0x76, 0x65, 0xfb, 0xb9, 0x15, 0x21, 0x53,
  19490. 0xef, 0x46, 0x02, 0x38, 0x50, 0x6a, 0x02, 0x45
  19491. };
  19492. /* SHA-512, COUNT = 0
  19493. * shared secret length: 521
  19494. * SharedInfo length: 128
  19495. * key data length: 1024
  19496. */
  19497. WOLFSSL_SMALL_STACK_STATIC const byte Z4[] = {
  19498. 0x00, 0xaa, 0x5b, 0xb7, 0x9b, 0x33, 0xe3, 0x89,
  19499. 0xfa, 0x58, 0xce, 0xad, 0xc0, 0x47, 0x19, 0x7f,
  19500. 0x14, 0xe7, 0x37, 0x12, 0xf4, 0x52, 0xca, 0xa9,
  19501. 0xfc, 0x4c, 0x9a, 0xdb, 0x36, 0x93, 0x48, 0xb8,
  19502. 0x15, 0x07, 0x39, 0x2f, 0x1a, 0x86, 0xdd, 0xfd,
  19503. 0xb7, 0xc4, 0xff, 0x82, 0x31, 0xc4, 0xbd, 0x0f,
  19504. 0x44, 0xe4, 0x4a, 0x1b, 0x55, 0xb1, 0x40, 0x47,
  19505. 0x47, 0xa9, 0xe2, 0xe7, 0x53, 0xf5, 0x5e, 0xf0,
  19506. 0x5a, 0x2d
  19507. };
  19508. WOLFSSL_SMALL_STACK_STATIC const byte info4[] = {
  19509. 0xe3, 0xb5, 0xb4, 0xc1, 0xb0, 0xd5, 0xcf, 0x1d,
  19510. 0x2b, 0x3a, 0x2f, 0x99, 0x37, 0x89, 0x5d, 0x31
  19511. };
  19512. WOLFSSL_SMALL_STACK_STATIC const byte verify4[] = {
  19513. 0x44, 0x63, 0xf8, 0x69, 0xf3, 0xcc, 0x18, 0x76,
  19514. 0x9b, 0x52, 0x26, 0x4b, 0x01, 0x12, 0xb5, 0x85,
  19515. 0x8f, 0x7a, 0xd3, 0x2a, 0x5a, 0x2d, 0x96, 0xd8,
  19516. 0xcf, 0xfa, 0xbf, 0x7f, 0xa7, 0x33, 0x63, 0x3d,
  19517. 0x6e, 0x4d, 0xd2, 0xa5, 0x99, 0xac, 0xce, 0xb3,
  19518. 0xea, 0x54, 0xa6, 0x21, 0x7c, 0xe0, 0xb5, 0x0e,
  19519. 0xef, 0x4f, 0x6b, 0x40, 0xa5, 0xc3, 0x02, 0x50,
  19520. 0xa5, 0xa8, 0xee, 0xee, 0x20, 0x80, 0x02, 0x26,
  19521. 0x70, 0x89, 0xdb, 0xf3, 0x51, 0xf3, 0xf5, 0x02,
  19522. 0x2a, 0xa9, 0x63, 0x8b, 0xf1, 0xee, 0x41, 0x9d,
  19523. 0xea, 0x9c, 0x4f, 0xf7, 0x45, 0xa2, 0x5a, 0xc2,
  19524. 0x7b, 0xda, 0x33, 0xca, 0x08, 0xbd, 0x56, 0xdd,
  19525. 0x1a, 0x59, 0xb4, 0x10, 0x6c, 0xf2, 0xdb, 0xbc,
  19526. 0x0a, 0xb2, 0xaa, 0x8e, 0x2e, 0xfa, 0x7b, 0x17,
  19527. 0x90, 0x2d, 0x34, 0x27, 0x69, 0x51, 0xce, 0xcc,
  19528. 0xab, 0x87, 0xf9, 0x66, 0x1c, 0x3e, 0x88, 0x16
  19529. };
  19530. #endif
  19531. #ifndef NO_SHA
  19532. ret = wc_X963_KDF(WC_HASH_TYPE_SHA, Z, sizeof(Z), NULL, 0,
  19533. kek, sizeof(verify));
  19534. if (ret != 0)
  19535. return -9800;
  19536. if (XMEMCMP(verify, kek, sizeof(verify)) != 0)
  19537. return -9801;
  19538. #endif
  19539. #ifndef NO_SHA256
  19540. ret = wc_X963_KDF(WC_HASH_TYPE_SHA256, Z2, sizeof(Z2), NULL, 0,
  19541. kek, sizeof(verify2));
  19542. if (ret != 0)
  19543. return -9802;
  19544. if (XMEMCMP(verify2, kek, sizeof(verify2)) != 0)
  19545. return -9803;
  19546. #endif
  19547. #ifdef WOLFSSL_SHA512
  19548. ret = wc_X963_KDF(WC_HASH_TYPE_SHA512, Z3, sizeof(Z3), NULL, 0,
  19549. kek, sizeof(verify3));
  19550. if (ret != 0)
  19551. return -9804;
  19552. if (XMEMCMP(verify3, kek, sizeof(verify3)) != 0)
  19553. return -9805;
  19554. ret = wc_X963_KDF(WC_HASH_TYPE_SHA512, Z4, sizeof(Z4), info4,
  19555. sizeof(info4), kek, sizeof(verify4));
  19556. if (ret != 0)
  19557. return -9806;
  19558. if (XMEMCMP(verify4, kek, sizeof(verify4)) != 0)
  19559. return -9807;
  19560. #endif
  19561. return 0;
  19562. }
  19563. #endif /* HAVE_X963_KDF */
  19564. #ifdef HAVE_ECC
  19565. /* size to use for ECC key gen tests */
  19566. #ifndef ECC_KEYGEN_SIZE
  19567. #ifndef NO_ECC256
  19568. #define ECC_KEYGEN_SIZE 32
  19569. #elif defined(HAVE_ECC384)
  19570. #define ECC_KEYGEN_SIZE 48
  19571. #elif defined(HAVE_ECC224)
  19572. #define ECC_KEYGEN_SIZE 28
  19573. #elif defined(HAVE_ECC521)
  19574. #define ECC_KEYGEN_SIZE 66
  19575. #else
  19576. #error No ECC keygen size defined for test
  19577. #endif
  19578. #endif
  19579. #ifdef BENCH_EMBEDDED
  19580. #define ECC_SHARED_SIZE 128
  19581. #else
  19582. #define ECC_SHARED_SIZE MAX_ECC_BYTES
  19583. #endif
  19584. #define ECC_DIGEST_SIZE MAX_ECC_BYTES
  19585. #define ECC_SIG_SIZE ECC_MAX_SIG_SIZE
  19586. #ifndef NO_ECC_VECTOR_TEST
  19587. #if (defined(HAVE_ECC192) || defined(HAVE_ECC224) ||\
  19588. !defined(NO_ECC256) || defined(HAVE_ECC384) ||\
  19589. defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES))
  19590. #define HAVE_ECC_VECTOR_TEST
  19591. #endif
  19592. #endif
  19593. #ifdef HAVE_ECC_VECTOR_TEST
  19594. typedef struct eccVector {
  19595. const char* msg; /* SHA-1 Encoded Message */
  19596. const char* Qx;
  19597. const char* Qy;
  19598. const char* d; /* Private Key */
  19599. const char* R;
  19600. const char* S;
  19601. const char* curveName;
  19602. word32 msgLen;
  19603. word32 keySize;
  19604. #ifndef NO_ASN
  19605. const byte* r;
  19606. word32 rSz;
  19607. const byte* s;
  19608. word32 sSz;
  19609. #endif
  19610. } eccVector;
  19611. #if !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  19612. static int ecc_test_vector_item(const eccVector* vector)
  19613. {
  19614. int ret = 0, verify = 0;
  19615. word32 sigSz;
  19616. #ifdef WOLFSSL_SMALL_STACK
  19617. ecc_key *userA = (ecc_key *)XMALLOC(sizeof *userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19618. #else
  19619. ecc_key userA[1];
  19620. #endif
  19621. WC_DECLARE_VAR(sig, byte, ECC_SIG_SIZE, HEAP_HINT);
  19622. #if !defined(NO_ASN) && !defined(HAVE_SELFTEST)
  19623. word32 sigRawSz, rSz = MAX_ECC_BYTES, sSz = MAX_ECC_BYTES;
  19624. WC_DECLARE_VAR(sigRaw, byte, ECC_SIG_SIZE, HEAP_HINT);
  19625. WC_DECLARE_VAR(r, byte, MAX_ECC_BYTES, HEAP_HINT);
  19626. WC_DECLARE_VAR(s, byte, MAX_ECC_BYTES, HEAP_HINT);
  19627. #endif
  19628. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  19629. if (sig == NULL)
  19630. ERROR_OUT(MEMORY_E, done);
  19631. #if !defined(NO_ASN) && !defined(HAVE_SELFTEST)
  19632. if (sigRaw == NULL || r == NULL || s == NULL)
  19633. ERROR_OUT(MEMORY_E, done);
  19634. #endif
  19635. #endif
  19636. #ifdef WOLFSSL_SMALL_STACK
  19637. if (userA == NULL)
  19638. ERROR_OUT(MEMORY_E, done);
  19639. #endif
  19640. ret = wc_ecc_init_ex(userA, HEAP_HINT, devId);
  19641. if (ret != 0)
  19642. goto done;
  19643. ret = wc_ecc_import_raw(userA, vector->Qx, vector->Qy,
  19644. vector->d, vector->curveName);
  19645. if (ret != 0)
  19646. goto done;
  19647. XMEMSET(sig, 0, ECC_SIG_SIZE);
  19648. sigSz = ECC_SIG_SIZE;
  19649. ret = wc_ecc_rs_to_sig(vector->R, vector->S, sig, &sigSz);
  19650. if (ret != 0)
  19651. goto done;
  19652. #if !defined(NO_ASN) && !defined(HAVE_SELFTEST)
  19653. XMEMSET(sigRaw, 0, ECC_SIG_SIZE);
  19654. sigRawSz = ECC_SIG_SIZE;
  19655. ret = wc_ecc_rs_raw_to_sig(vector->r, vector->rSz, vector->s, vector->sSz,
  19656. sigRaw, &sigRawSz);
  19657. if (ret != 0)
  19658. goto done;
  19659. if (sigSz != sigRawSz || XMEMCMP(sig, sigRaw, sigSz) != 0) {
  19660. ret = -9810;
  19661. goto done;
  19662. }
  19663. ret = wc_ecc_sig_to_rs(sig, sigSz, r, &rSz, s, &sSz);
  19664. if (ret != 0)
  19665. goto done;
  19666. if (rSz != vector->rSz || XMEMCMP(r, vector->r, rSz) != 0 ||
  19667. sSz != vector->sSz || XMEMCMP(s, vector->s, sSz) != 0) {
  19668. ret = -9811;
  19669. goto done;
  19670. }
  19671. #endif
  19672. do {
  19673. #if defined(WOLFSSL_ASYNC_CRYPT)
  19674. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  19675. #endif
  19676. if (ret == 0)
  19677. ret = wc_ecc_verify_hash(sig, sigSz, (byte*)vector->msg,
  19678. vector->msgLen, &verify, userA);
  19679. } while (ret == WC_PENDING_E);
  19680. if (ret != 0)
  19681. goto done;
  19682. TEST_SLEEP();
  19683. if (verify != 1)
  19684. ret = -9812;
  19685. done:
  19686. #ifdef WOLFSSL_SMALL_STACK
  19687. if (userA != NULL) {
  19688. wc_ecc_free(userA);
  19689. XFREE(userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19690. }
  19691. #else
  19692. wc_ecc_free(userA);
  19693. #endif
  19694. #if !defined(NO_ASN) && !defined(HAVE_SELFTEST)
  19695. WC_FREE_VAR(sigRaw, HEAP_HINT);
  19696. WC_FREE_VAR(r, HEAP_HINT);
  19697. WC_FREE_VAR(s, HEAP_HINT);
  19698. #endif
  19699. WC_FREE_VAR(sig, HEAP_HINT);
  19700. return ret;
  19701. }
  19702. static int ecc_test_vector(int keySize)
  19703. {
  19704. int ret;
  19705. eccVector vec;
  19706. XMEMSET(&vec, 0, sizeof(vec));
  19707. vec.keySize = (word32)keySize;
  19708. switch(keySize) {
  19709. #if defined(HAVE_ECC112) || defined(HAVE_ALL_CURVES)
  19710. case 14:
  19711. return 0;
  19712. #endif /* HAVE_ECC112 */
  19713. #if defined(HAVE_ECC128) || defined(HAVE_ALL_CURVES)
  19714. case 16:
  19715. return 0;
  19716. #endif /* HAVE_ECC128 */
  19717. #if defined(HAVE_ECC160) || defined(HAVE_ALL_CURVES)
  19718. case 20:
  19719. return 0;
  19720. #endif /* HAVE_ECC160 */
  19721. #if defined(HAVE_ECC192) || defined(HAVE_ALL_CURVES)
  19722. case 24:
  19723. /* first [P-192,SHA-1] vector from FIPS 186-3 NIST vectors */
  19724. #if 1
  19725. vec.msg = "\x60\x80\x79\x42\x3f\x12\x42\x1d\xe6\x16\xb7\x49\x3e\xbe\x55\x1c\xf4\xd6\x5b\x92";
  19726. vec.msgLen = 20;
  19727. #else
  19728. /* This is the raw message prior to SHA-1 */
  19729. vec.msg =
  19730. "\xeb\xf7\x48\xd7\x48\xeb\xbc\xa7\xd2\x9f\xb4\x73\x69\x8a\x6e\x6b"
  19731. "\x4f\xb1\x0c\x86\x5d\x4a\xf0\x24\xcc\x39\xae\x3d\xf3\x46\x4b\xa4"
  19732. "\xf1\xd6\xd4\x0f\x32\xbf\x96\x18\xa9\x1b\xb5\x98\x6f\xa1\xa2\xaf"
  19733. "\x04\x8a\x0e\x14\xdc\x51\xe5\x26\x7e\xb0\x5e\x12\x7d\x68\x9d\x0a"
  19734. "\xc6\xf1\xa7\xf1\x56\xce\x06\x63\x16\xb9\x71\xcc\x7a\x11\xd0\xfd"
  19735. "\x7a\x20\x93\xe2\x7c\xf2\xd0\x87\x27\xa4\xe6\x74\x8c\xc3\x2f\xd5"
  19736. "\x9c\x78\x10\xc5\xb9\x01\x9d\xf2\x1c\xdc\xc0\xbc\xa4\x32\xc0\xa3"
  19737. "\xee\xd0\x78\x53\x87\x50\x88\x77\x11\x43\x59\xce\xe4\xa0\x71\xcf";
  19738. vec.msgLen = 128;
  19739. #endif
  19740. vec.Qx = "07008ea40b08dbe76432096e80a2494c94982d2d5bcf98e6";
  19741. vec.Qy = "76fab681d00b414ea636ba215de26d98c41bd7f2e4d65477";
  19742. vec.d = "e14f37b3d1374ff8b03f41b9b3fdd2f0ebccf275d660d7f3";
  19743. vec.R = "6994d962bdd0d793ffddf855ec5bf2f91a9698b46258a63e";
  19744. vec.S = "02ba6465a234903744ab02bc8521405b73cf5fc00e1a9f41";
  19745. vec.curveName = "SECP192R1";
  19746. #ifndef NO_ASN
  19747. vec.r = (byte*)"\x69\x94\xd9\x62\xbd\xd0\xd7\x93\xff\xdd\xf8\x55"
  19748. "\xec\x5b\xf2\xf9\x1a\x96\x98\xb4\x62\x58\xa6\x3e";
  19749. vec.rSz = 24;
  19750. vec.s = (byte*)"\x02\xba\x64\x65\xa2\x34\x90\x37\x44\xab\x02\xbc"
  19751. "\x85\x21\x40\x5b\x73\xcf\x5f\xc0\x0e\x1a\x9f\x41";
  19752. vec.sSz = 24;
  19753. #endif
  19754. break;
  19755. #endif /* HAVE_ECC192 */
  19756. #if defined(HAVE_ECC224) || defined(HAVE_ALL_CURVES)
  19757. case 28:
  19758. /* first [P-224,SHA-1] vector from FIPS 186-3 NIST vectors */
  19759. #if 1
  19760. vec.msg = "\xb9\xa3\xb8\x6d\xb0\xba\x99\xfd\xc6\xd2\x94\x6b\xfe\xbe\x9c\xe8\x3f\x10\x74\xfc";
  19761. vec.msgLen = 20;
  19762. #else
  19763. /* This is the raw message prior to SHA-1 */
  19764. vec.msg =
  19765. "\x36\xc8\xb2\x29\x86\x48\x7f\x67\x7c\x18\xd0\x97\x2a\x9e\x20\x47"
  19766. "\xb3\xaf\xa5\x9e\xc1\x62\x76\x4e\xc3\x0b\x5b\x69\xe0\x63\x0f\x99"
  19767. "\x0d\x4e\x05\xc2\x73\xb0\xe5\xa9\xd4\x28\x27\xb6\x95\xfc\x2d\x64"
  19768. "\xd9\x13\x8b\x1c\xf4\xc1\x21\x55\x89\x4c\x42\x13\x21\xa7\xbb\x97"
  19769. "\x0b\xdc\xe0\xfb\xf0\xd2\xae\x85\x61\xaa\xd8\x71\x7f\x2e\x46\xdf"
  19770. "\xe3\xff\x8d\xea\xb4\xd7\x93\x23\x56\x03\x2c\x15\x13\x0d\x59\x9e"
  19771. "\x26\xc1\x0f\x2f\xec\x96\x30\x31\xac\x69\x38\xa1\x8d\x66\x45\x38"
  19772. "\xb9\x4d\xac\x55\x34\xef\x7b\x59\x94\x24\xd6\x9b\xe1\xf7\x1c\x20";
  19773. vec.msgLen = 128;
  19774. #endif
  19775. vec.Qx = "8a4dca35136c4b70e588e23554637ae251077d1365a6ba5db9585de7";
  19776. vec.Qy = "ad3dee06de0be8279d4af435d7245f14f3b4f82eb578e519ee0057b1";
  19777. vec.d = "97c4b796e1639dd1035b708fc00dc7ba1682cec44a1002a1a820619f";
  19778. vec.R = "147b33758321e722a0360a4719738af848449e2c1d08defebc1671a7";
  19779. vec.S = "24fc7ed7f1352ca3872aa0916191289e2e04d454935d50fe6af3ad5b";
  19780. vec.curveName = "SECP224R1";
  19781. #ifndef NO_ASN
  19782. vec.r = (byte*)"\x14\x7b\x33\x75\x83\x21\xe7\x22\xa0\x36\x0a\x47"
  19783. "\x19\x73\x8a\xf8\x48\x44\x9e\x2c\x1d\x08\xde\xfe"
  19784. "\xbc\x16\x71\xa7";
  19785. vec.rSz = 28;
  19786. vec.s = (byte*)"\x24\xfc\x7e\xd7\xf1\x35\x2c\xa3\x87\x2a\xa0\x91"
  19787. "\x61\x91\x28\x9e\x2e\x04\xd4\x54\x93\x5d\x50\xfe"
  19788. "\x6a\xf3\xad\x5b";
  19789. vec.sSz = 28;
  19790. #endif
  19791. break;
  19792. #endif /* HAVE_ECC224 */
  19793. #if defined(HAVE_ECC239) || defined(HAVE_ALL_CURVES)
  19794. case 30:
  19795. return 0;
  19796. #endif /* HAVE_ECC239 */
  19797. #if !defined(NO_ECC256) || defined(HAVE_ALL_CURVES)
  19798. case 32:
  19799. /* first [P-256,SHA-1] vector from FIPS 186-3 NIST vectors */
  19800. #if 1
  19801. vec.msg = "\xa3\xf9\x1a\xe2\x1b\xa6\xb3\x03\x98\x64\x47\x2f\x18\x41\x44\xc6\xaf\x62\xcd\x0e";
  19802. vec.msgLen = 20;
  19803. #else
  19804. /* This is the raw message prior to SHA-1 */
  19805. vec.msg =
  19806. "\xa2\x4b\x21\x76\x2e\x6e\xdb\x15\x3c\xc1\x14\x38\xdb\x0e\x92\xcd"
  19807. "\xf5\x2b\x86\xb0\x6c\xa9\x70\x16\x06\x27\x59\xc7\x0d\x36\xd1\x56"
  19808. "\x2c\xc9\x63\x0d\x7f\xc7\xc7\x74\xb2\x8b\x54\xe3\x1e\xf5\x58\x72"
  19809. "\xb2\xa6\x5d\xf1\xd7\xec\x26\xde\xbb\x33\xe7\xd9\x27\xef\xcc\xf4"
  19810. "\x6b\x63\xde\x52\xa4\xf4\x31\xea\xca\x59\xb0\x5d\x2e\xde\xc4\x84"
  19811. "\x5f\xff\xc0\xee\x15\x03\x94\xd6\x1f\x3d\xfe\xcb\xcd\xbf\x6f\x5a"
  19812. "\x73\x38\xd0\xbe\x3f\x2a\x77\x34\x51\x98\x3e\xba\xeb\x48\xf6\x73"
  19813. "\x8f\xc8\x95\xdf\x35\x7e\x1a\x48\xa6\x53\xbb\x35\x5a\x31\xa1\xb4"
  19814. vec.msgLen = 128;
  19815. #endif
  19816. vec.Qx = "fa2737fb93488d19caef11ae7faf6b7f4bcd67b286e3fc54e8a65c2b74aeccb0";
  19817. vec.Qy = "d4ccd6dae698208aa8c3a6f39e45510d03be09b2f124bfc067856c324f9b4d09";
  19818. vec.d = "be34baa8d040a3b991f9075b56ba292f755b90e4b6dc10dad36715c33cfdac25";
  19819. vec.R = "2b826f5d44e2d0b6de531ad96b51e8f0c56fdfead3c236892e4d84eacfc3b75c";
  19820. vec.S = "a2248b62c03db35a7cd63e8a120a3521a89d3d2f61ff99035a2148ae32e3a248";
  19821. #ifndef NO_ASN
  19822. vec.r = (byte*)"\x2b\x82\x6f\x5d\x44\xe2\xd0\xb6\xde\x53\x1a\xd9"
  19823. "\x6b\x51\xe8\xf0\xc5\x6f\xdf\xea\xd3\xc2\x36\x89"
  19824. "\x2e\x4d\x84\xea\xcf\xc3\xb7\x5c";
  19825. vec.rSz = 32;
  19826. vec.s = (byte*)"\xa2\x24\x8b\x62\xc0\x3d\xb3\x5a\x7c\xd6\x3e\x8a"
  19827. "\x12\x0a\x35\x21\xa8\x9d\x3d\x2f\x61\xff\x99\x03"
  19828. "\x5a\x21\x48\xae\x32\xe3\xa2\x48";
  19829. vec.sSz = 32;
  19830. #endif
  19831. vec.curveName = "SECP256R1";
  19832. break;
  19833. #endif /* !NO_ECC256 */
  19834. #if defined(HAVE_ECC320) || defined(HAVE_ALL_CURVES)
  19835. case 40:
  19836. return 0;
  19837. #endif /* HAVE_ECC320 */
  19838. #if defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)
  19839. case 48:
  19840. /* first [P-384,SHA-1] vector from FIPS 186-3 NIST vectors */
  19841. #if 1
  19842. vec.msg = "\x9b\x9f\x8c\x95\x35\xa5\xca\x26\x60\x5d\xb7\xf2\xfa\x57\x3b\xdf\xc3\x2e\xab\x8b";
  19843. vec.msgLen = 20;
  19844. #else
  19845. /* This is the raw message prior to SHA-1 */
  19846. vec.msg =
  19847. "\xab\xe1\x0a\xce\x13\xe7\xe1\xd9\x18\x6c\x48\xf7\x88\x9d\x51\x47"
  19848. "\x3d\x3a\x09\x61\x98\x4b\xc8\x72\xdf\x70\x8e\xcc\x3e\xd3\xb8\x16"
  19849. "\x9d\x01\xe3\xd9\x6f\xc4\xf1\xd5\xea\x00\xa0\x36\x92\xbc\xc5\xcf"
  19850. "\xfd\x53\x78\x7c\x88\xb9\x34\xaf\x40\x4c\x03\x9d\x32\x89\xb5\xba"
  19851. "\xc5\xae\x7d\xb1\x49\x68\x75\xb5\xdc\x73\xc3\x09\xf9\x25\xc1\x3d"
  19852. "\x1c\x01\xab\xda\xaf\xeb\xcd\xac\x2c\xee\x43\x39\x39\xce\x8d\x4a"
  19853. "\x0a\x5d\x57\xbb\x70\x5f\x3b\xf6\xec\x08\x47\x95\x11\xd4\xb4\xa3"
  19854. "\x21\x1f\x61\x64\x9a\xd6\x27\x43\x14\xbf\x0d\x43\x8a\x81\xe0\x60"
  19855. vec.msgLen = 128;
  19856. #endif
  19857. vec.Qx = "e55fee6c49d8d523f5ce7bf9c0425ce4ff650708b7de5cfb095901523979a7f042602db30854735369813b5c3f5ef868";
  19858. vec.Qy = "28f59cc5dc509892a988d38a8e2519de3d0c4fd0fbdb0993e38f18506c17606c5e24249246f1ce94983a5361c5be983e";
  19859. vec.d = "a492ce8fa90084c227e1a32f7974d39e9ff67a7e8705ec3419b35fb607582bebd461e0b1520ac76ec2dd4e9b63ebae71";
  19860. vec.R = "6820b8585204648aed63bdff47f6d9acebdea62944774a7d14f0e14aa0b9a5b99545b2daee6b3c74ebf606667a3f39b7";
  19861. vec.S = "491af1d0cccd56ddd520b233775d0bc6b40a6255cc55207d8e9356741f23c96c14714221078dbd5c17f4fdd89b32a907";
  19862. vec.curveName = "SECP384R1";
  19863. #ifndef NO_ASN
  19864. vec.r = (byte*)"\x68\x20\xb8\x58\x52\x04\x64\x8a\xed\x63\xbd\xff"
  19865. "\x47\xf6\xd9\xac\xeb\xde\xa6\x29\x44\x77\x4a\x7d"
  19866. "\x14\xf0\xe1\x4a\xa0\xb9\xa5\xb9\x95\x45\xb2\xda"
  19867. "\xee\x6b\x3c\x74\xeb\xf6\x06\x66\x7a\x3f\x39\xb7";
  19868. vec.rSz = 48;
  19869. vec.s = (byte*)"\x49\x1a\xf1\xd0\xcc\xcd\x56\xdd\xd5\x20\xb2\x33"
  19870. "\x77\x5d\x0b\xc6\xb4\x0a\x62\x55\xcc\x55\x20\x7d"
  19871. "\x8e\x93\x56\x74\x1f\x23\xc9\x6c\x14\x71\x42\x21"
  19872. "\x07\x8d\xbd\x5c\x17\xf4\xfd\xd8\x9b\x32\xa9\x07";
  19873. vec.sSz = 48;
  19874. #endif
  19875. break;
  19876. #endif /* HAVE_ECC384 */
  19877. #if defined(HAVE_ECC512) || defined(HAVE_ALL_CURVES)
  19878. case 64:
  19879. return 0;
  19880. #endif /* HAVE_ECC512 */
  19881. #if defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)
  19882. case 66:
  19883. /* first [P-521,SHA-1] vector from FIPS 186-3 NIST vectors */
  19884. #if 1
  19885. vec.msg = "\x1b\xf7\x03\x9c\xca\x23\x94\x27\x3f\x11\xa1\xd4\x8d\xcc\xb4\x46\x6f\x31\x61\xdf";
  19886. vec.msgLen = 20;
  19887. #else
  19888. /* This is the raw message prior to SHA-1 */
  19889. vec.msg =
  19890. "\x50\x3f\x79\x39\x34\x0a\xc7\x23\xcd\x4a\x2f\x4e\x6c\xcc\x27\x33"
  19891. "\x38\x3a\xca\x2f\xba\x90\x02\x19\x9d\x9e\x1f\x94\x8b\xe0\x41\x21"
  19892. "\x07\xa3\xfd\xd5\x14\xd9\x0c\xd4\xf3\x7c\xc3\xac\x62\xef\x00\x3a"
  19893. "\x2d\xb1\xd9\x65\x7a\xb7\x7f\xe7\x55\xbf\x71\xfa\x59\xe4\xd9\x6e"
  19894. "\xa7\x2a\xe7\xbf\x9d\xe8\x7d\x79\x34\x3b\xc1\xa4\xbb\x14\x4d\x16"
  19895. "\x28\xd1\xe9\xe9\xc8\xed\x80\x8b\x96\x2c\x54\xe5\xf9\x6d\x53\xda"
  19896. "\x14\x7a\x96\x38\xf9\x4a\x91\x75\xd8\xed\x61\x05\x5f\x0b\xa5\x73"
  19897. "\xa8\x2b\xb7\xe0\x18\xee\xda\xc4\xea\x7b\x36\x2e\xc8\x9c\x38\x2b"
  19898. vec.msgLen = 128;
  19899. #endif
  19900. vec.Qx = "12fbcaeffa6a51f3ee4d3d2b51c5dec6d7c726ca353fc014ea2bf7cfbb9b910d32cbfa6a00fe39b6cdb8946f22775398b2e233c0cf144d78c8a7742b5c7a3bb5d23";
  19901. vec.Qy = "09cdef823dd7bf9a79e8cceacd2e4527c231d0ae5967af0958e931d7ddccf2805a3e618dc3039fec9febbd33052fe4c0fee98f033106064982d88f4e03549d4a64d";
  19902. vec.d = "1bd56bd106118eda246155bd43b42b8e13f0a6e25dd3bb376026fab4dc92b6157bc6dfec2d15dd3d0cf2a39aa68494042af48ba9601118da82c6f2108a3a203ad74";
  19903. vec.R = "0bd117b4807710898f9dd7778056485777668f0e78e6ddf5b000356121eb7a220e9493c7f9a57c077947f89ac45d5acb6661bbcd17abb3faea149ba0aa3bb1521be";
  19904. vec.S = "019cd2c5c3f9870ecdeb9b323abdf3a98cd5e231d85c6ddc5b71ab190739f7f226e6b134ba1d5889ddeb2751dabd97911dff90c34684cdbe7bb669b6c3d22f2480c";
  19905. vec.curveName = "SECP521R1";
  19906. #ifndef NO_ASN
  19907. vec.r = (byte*)"\xbd\x11\x7b\x48\x07\x71\x08\x98\xf9\xdd\x77\x78"
  19908. "\x05\x64\x85\x77\x76\x68\xf0\xe7\x8e\x6d\xdf\x5b"
  19909. "\x00\x03\x56\x12\x1e\xb7\xa2\x20\xe9\x49\x3c\x7f"
  19910. "\x9a\x57\xc0\x77\x94\x7f\x89\xac\x45\xd5\xac\xb6"
  19911. "\x66\x1b\xbc\xd1\x7a\xbb\x3f\xae\xa1\x49\xba\x0a"
  19912. "\xa3\xbb\x15\x21\xbe";
  19913. vec.rSz = 65;
  19914. vec.s = (byte*)"\x19\xcd\x2c\x5c\x3f\x98\x70\xec\xde\xb9\xb3\x23"
  19915. "\xab\xdf\x3a\x98\xcd\x5e\x23\x1d\x85\xc6\xdd\xc5"
  19916. "\xb7\x1a\xb1\x90\x73\x9f\x7f\x22\x6e\x6b\x13\x4b"
  19917. "\xa1\xd5\x88\x9d\xde\xb2\x75\x1d\xab\xd9\x79\x11"
  19918. "\xdf\xf9\x0c\x34\x68\x4c\xdb\xe7\xbb\x66\x9b\x6c"
  19919. "\x3d\x22\xf2\x48\x0c";
  19920. vec.sSz = 65;
  19921. #endif
  19922. break;
  19923. #endif /* HAVE_ECC521 */
  19924. default:
  19925. return NOT_COMPILED_IN; /* Invalid key size / Not supported */
  19926. }; /* Switch */
  19927. ret = ecc_test_vector_item(&vec);
  19928. if (ret < 0) {
  19929. return ret;
  19930. }
  19931. return 0;
  19932. }
  19933. #endif /* WOLF_CRYPTO_CB_ONLY_ECC */
  19934. #if defined(HAVE_ECC_SIGN) && defined(WOLFSSL_ECDSA_DETERMINISTIC_K)
  19935. static int ecc_test_deterministic_k(WC_RNG* rng)
  19936. {
  19937. int ret;
  19938. ecc_key key;
  19939. byte sig[72];
  19940. word32 sigSz;
  19941. unsigned char msg[] = "sample";
  19942. unsigned char hash[32];
  19943. const char* dIUT =
  19944. "C9AFA9D845BA75166B5C215767B1D6934E50C3DB36E89B127B8A622B120F6721";
  19945. const char* QIUTx =
  19946. "60FED4BA255A9D31C961EB74C6356D68C049B8923B61FA6CE669622E60F29FB6";
  19947. const char* QIUTy =
  19948. "7903FE1008B8BC99A41AE9E95628BC64F2F1B20C2D7E9F5177A3C294D4462299";
  19949. const byte expSig[] = {
  19950. 0x30, 0x46, 0x02, 0x21, 0x00, 0xEF, 0xD4, 0x8B,
  19951. 0x2A, 0xAC, 0xB6, 0xA8, 0xFD, 0x11, 0x40, 0xDD,
  19952. 0x9C, 0xD4, 0x5E, 0x81, 0xD6, 0x9D, 0x2C, 0x87,
  19953. 0x7B, 0x56, 0xAA, 0xF9, 0x91, 0xC3, 0x4D, 0x0E,
  19954. 0xA8, 0x4E, 0xAF, 0x37, 0x16, 0x02, 0x21, 0x00,
  19955. 0xF7, 0xCB, 0x1C, 0x94, 0x2D, 0x65, 0x7C, 0x41,
  19956. 0xD4, 0x36, 0xC7, 0xA1, 0xB6, 0xE2, 0x9F, 0x65,
  19957. 0xF3, 0xE9, 0x00, 0xDB, 0xB9, 0xAF, 0xF4, 0x06,
  19958. 0x4D, 0xC4, 0xAB, 0x2F, 0x84, 0x3A, 0xCD, 0xA8
  19959. };
  19960. ret = wc_ecc_init_ex(&key, HEAP_HINT, devId);
  19961. if (ret != 0) {
  19962. return ret;
  19963. }
  19964. ret = wc_ecc_import_raw(&key, QIUTx, QIUTy, dIUT, "SECP256R1");
  19965. if (ret != 0) {
  19966. goto done;
  19967. }
  19968. ret = wc_Hash(WC_HASH_TYPE_SHA256, msg,
  19969. (word32)XSTRLEN((const char*)msg), hash, sizeof(hash));
  19970. if (ret != 0) {
  19971. goto done;
  19972. }
  19973. ret = wc_ecc_set_deterministic(&key, 1);
  19974. if (ret != 0) {
  19975. goto done;
  19976. }
  19977. sigSz = sizeof(sig);
  19978. do {
  19979. #if defined(WOLFSSL_ASYNC_CRYPT)
  19980. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  19981. #endif
  19982. if (ret == 0)
  19983. ret = wc_ecc_sign_hash(hash, sizeof(hash), sig, &sigSz, rng, &key);
  19984. } while (ret == WC_PENDING_E);
  19985. if (ret != 0) {
  19986. goto done;
  19987. }
  19988. TEST_SLEEP();
  19989. if (sigSz != sizeof(expSig)) {
  19990. ret = -9830;
  19991. goto done;
  19992. }
  19993. if (XMEMCMP(sig, expSig, sigSz) != 0) {
  19994. ret = -9831;
  19995. goto done;
  19996. }
  19997. sigSz = sizeof(sig);
  19998. do {
  19999. #if defined(WOLFSSL_ASYNC_CRYPT)
  20000. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  20001. #endif
  20002. if (ret == 0)
  20003. ret = wc_ecc_sign_hash(hash, sizeof(hash), sig, &sigSz, rng, &key);
  20004. } while (ret == WC_PENDING_E);
  20005. if (ret != 0) {
  20006. goto done;
  20007. }
  20008. TEST_SLEEP();
  20009. done:
  20010. wc_ecc_free(&key);
  20011. return ret;
  20012. }
  20013. #ifdef WOLFSSL_PUBLIC_MP
  20014. #if defined(HAVE_ECC384)
  20015. /* KAT from RFC6979 */
  20016. static int ecc384_test_deterministic_k(WC_RNG* rng)
  20017. {
  20018. int ret;
  20019. ecc_key key;
  20020. byte sig[72];
  20021. word32 sigSz;
  20022. unsigned char msg[] = "sample";
  20023. unsigned char hash[32];
  20024. const char* dIUT =
  20025. "6B9D3DAD2E1B8C1C05B19875B6659F4DE23C3B667BF297BA9AA47740787137D8"
  20026. "96D5724E4C70A825F872C9EA60D2EDF5";
  20027. const char* QIUTx =
  20028. "EC3A4E415B4E19A4568618029F427FA5DA9A8BC4AE92E02E06AAE5286B300C64"
  20029. "DEF8F0EA9055866064A254515480BC13";
  20030. const char* QIUTy =
  20031. "8015D9B72D7D57244EA8EF9AC0C621896708A59367F9DFB9F54CA84B3F1C9DB1"
  20032. "288B231C3AE0D4FE7344FD2533264720";
  20033. const char* expRstr =
  20034. "21B13D1E013C7FA1392D03C5F99AF8B30C570C6F98D4EA8E354B63A21D3DAA33"
  20035. "BDE1E888E63355D92FA2B3C36D8FB2CD";
  20036. const char* expSstr =
  20037. "F3AA443FB107745BF4BD77CB3891674632068A10CA67E3D45DB2266FA7D1FEEB"
  20038. "EFDC63ECCD1AC42EC0CB8668A4FA0AB0";
  20039. mp_int r,s, expR, expS;
  20040. mp_init_multi(&r, &s, &expR, &expS, NULL, NULL);
  20041. ret = wc_ecc_init_ex(&key, HEAP_HINT, devId);
  20042. if (ret != 0) {
  20043. return ret;
  20044. }
  20045. ret = wc_ecc_import_raw(&key, QIUTx, QIUTy, dIUT, "SECP384R1");
  20046. if (ret != 0) {
  20047. goto done;
  20048. }
  20049. ret = wc_Hash(WC_HASH_TYPE_SHA256, msg,
  20050. (word32)XSTRLEN((const char*)msg), hash, sizeof(hash));
  20051. if (ret != 0) {
  20052. goto done;
  20053. }
  20054. ret = wc_ecc_set_deterministic(&key, 1);
  20055. if (ret != 0) {
  20056. goto done;
  20057. }
  20058. sigSz = sizeof(sig);
  20059. do {
  20060. #if defined(WOLFSSL_ASYNC_CRYPT)
  20061. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  20062. #endif
  20063. if (ret == 0)
  20064. ret = wc_ecc_sign_hash_ex(hash, sizeof(hash), rng, &key, &r, &s);
  20065. } while (ret == WC_PENDING_E);
  20066. if (ret != 0) {
  20067. goto done;
  20068. }
  20069. TEST_SLEEP();
  20070. mp_read_radix(&expR, expRstr, MP_RADIX_HEX);
  20071. mp_read_radix(&expS, expSstr, MP_RADIX_HEX);
  20072. if (mp_cmp(&r, &expR) != MP_EQ) {
  20073. ret = -1;
  20074. }
  20075. done:
  20076. wc_ecc_free(&key);
  20077. return ret;
  20078. }
  20079. #endif /* HAVE_ECC384 */
  20080. #if defined(HAVE_ECC521)
  20081. /* KAT from RFC6979 */
  20082. static int ecc521_test_deterministic_k(WC_RNG* rng)
  20083. {
  20084. int ret;
  20085. ecc_key key;
  20086. byte sig[ECC_MAX_SIG_SIZE];
  20087. word32 sigSz;
  20088. unsigned char msg[] = "sample";
  20089. unsigned char hash[32];
  20090. const char* dIUT =
  20091. "0FAD06DAA62BA3B25D2FB40133DA757205DE67F5BB0018FEE8C86E1B68C7E75C"
  20092. "AA896EB32F1F47C70855836A6D16FCC1466F6D8FBEC67DB89EC0C08B0E996B83"
  20093. "538";
  20094. const char* QIUTx =
  20095. "1894550D0785932E00EAA23B694F213F8C3121F86DC97A04E5A7167DB4E5BCD3"
  20096. "71123D46E45DB6B5D5370A7F20FB633155D38FFA16D2BD761DCAC474B9A2F502"
  20097. "3A4";
  20098. const char* QIUTy =
  20099. "0493101C962CD4D2FDDF782285E64584139C2F91B47F87FF82354D6630F746A2"
  20100. "8A0DB25741B5B34A828008B22ACC23F924FAAFBD4D33F81EA66956DFEAA2BFDF"
  20101. "CF5";
  20102. const char* expRstr =
  20103. "1511BB4D675114FE266FC4372B87682BAECC01D3CC62CF2303C92B3526012659"
  20104. "D16876E25C7C1E57648F23B73564D67F61C6F14D527D54972810421E7D87589E"
  20105. "1A7";
  20106. const char* expSstr =
  20107. "04A171143A83163D6DF460AAF61522695F207A58B95C0644D87E52AA1A347916"
  20108. "E4F7A72930B1BC06DBE22CE3F58264AFD23704CBB63B29B931F7DE6C9D949A7E"
  20109. "CFC";
  20110. mp_int r,s, expR, expS;
  20111. mp_init_multi(&r, &s, &expR, &expS, NULL, NULL);
  20112. ret = wc_ecc_init_ex(&key, HEAP_HINT, devId);
  20113. if (ret != 0) {
  20114. return ret;
  20115. }
  20116. ret = wc_ecc_import_raw(&key, QIUTx, QIUTy, dIUT, "SECP521R1");
  20117. if (ret != 0) {
  20118. goto done;
  20119. }
  20120. ret = wc_Hash(WC_HASH_TYPE_SHA256, msg,
  20121. (word32)XSTRLEN((const char*)msg), hash, sizeof(hash));
  20122. if (ret != 0) {
  20123. goto done;
  20124. }
  20125. ret = wc_ecc_set_deterministic(&key, 1);
  20126. if (ret != 0) {
  20127. goto done;
  20128. }
  20129. sigSz = sizeof(sig);
  20130. do {
  20131. #if defined(WOLFSSL_ASYNC_CRYPT)
  20132. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  20133. #endif
  20134. if (ret == 0)
  20135. ret = wc_ecc_sign_hash_ex(hash, sizeof(hash), rng, &key, &r, &s);
  20136. } while (ret == WC_PENDING_E);
  20137. if (ret != 0) {
  20138. goto done;
  20139. }
  20140. TEST_SLEEP();
  20141. mp_read_radix(&expR, expRstr, MP_RADIX_HEX);
  20142. mp_read_radix(&expS, expSstr, MP_RADIX_HEX);
  20143. if (mp_cmp(&r, &expR) != MP_EQ) {
  20144. ret = -1;
  20145. }
  20146. done:
  20147. wc_ecc_free(&key);
  20148. return ret;
  20149. }
  20150. #endif /* HAVE_ECC521 */
  20151. #endif /* WOLFSSL_PUBLIC_MP */
  20152. #endif
  20153. #if defined(HAVE_ECC_SIGN) && defined(WOLFSSL_ECDSA_SET_K) && \
  20154. !defined(WOLFSSL_KCAPI_ECC)
  20155. static int ecc_test_sign_vectors(WC_RNG* rng)
  20156. {
  20157. int ret;
  20158. #ifdef WOLFSSL_SMALL_STACK
  20159. ecc_key *key = NULL;
  20160. #else
  20161. ecc_key key[1];
  20162. #endif
  20163. int key_inited = 0;
  20164. byte sig[72];
  20165. word32 sigSz;
  20166. WOLFSSL_SMALL_STACK_STATIC const unsigned char hash[32] = "test wolfSSL deterministic sign";
  20167. WOLFSSL_SMALL_STACK_STATIC const char* dIUT = "7d7dc5f71eb29ddaf80d6214632eeae03d9058af1fb6d22ed80badb62bc1a534";
  20168. WOLFSSL_SMALL_STACK_STATIC const char* QIUTx = "ead218590119e8876b29146ff89ca61770c4edbbf97d38ce385ed281d8a6b230";
  20169. WOLFSSL_SMALL_STACK_STATIC const char* QIUTy = "28af61281fd35e2fa7002523acc85a429cb06ee6648325389f59edfce1405141";
  20170. WOLFSSL_SMALL_STACK_STATIC const byte k[1] = { 0x02 };
  20171. WOLFSSL_SMALL_STACK_STATIC const byte expSig[71] = {
  20172. 0x30, 0x45, 0x02, 0x20, 0x7c, 0xf2, 0x7b, 0x18,
  20173. 0x8d, 0x03, 0x4f, 0x7e, 0x8a, 0x52, 0x38, 0x03,
  20174. 0x04, 0xb5, 0x1a, 0xc3, 0xc0, 0x89, 0x69, 0xe2,
  20175. 0x77, 0xf2, 0x1b, 0x35, 0xa6, 0x0b, 0x48, 0xfc,
  20176. 0x47, 0x66, 0x99, 0x78, 0x02, 0x21, 0x00, 0xa8,
  20177. 0x43, 0xa0, 0xce, 0x6c, 0x5e, 0x17, 0x8a, 0x53,
  20178. 0x4d, 0xaf, 0xd2, 0x95, 0x78, 0x9f, 0x84, 0x4f,
  20179. 0x94, 0xb8, 0x75, 0xa3, 0x19, 0xa5, 0xd4, 0xdf,
  20180. 0xe1, 0xd4, 0x5e, 0x9d, 0x97, 0xfe, 0x81
  20181. };
  20182. #ifdef WOLFSSL_SMALL_STACK
  20183. if ((key = (ecc_key *)XMALLOC(sizeof(*key), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER)) == NULL)
  20184. return MEMORY_E;
  20185. #endif
  20186. ret = wc_ecc_init_ex(key, HEAP_HINT, devId);
  20187. if (ret != 0) {
  20188. goto done;
  20189. }
  20190. key_inited = 1;
  20191. ret = wc_ecc_import_raw(key, QIUTx, QIUTy, dIUT, "SECP256R1");
  20192. if (ret != 0) {
  20193. goto done;
  20194. }
  20195. wc_ecc_set_flags(key, WC_ECC_FLAG_DEC_SIGN);
  20196. ret = wc_ecc_sign_set_k(k, sizeof(k), key);
  20197. if (ret != 0) {
  20198. goto done;
  20199. }
  20200. sigSz = sizeof(sig);
  20201. do {
  20202. #if defined(WOLFSSL_ASYNC_CRYPT)
  20203. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  20204. #endif
  20205. if (ret == 0)
  20206. ret = wc_ecc_sign_hash(hash, sizeof(hash), sig, &sigSz, rng, key);
  20207. } while (ret == WC_PENDING_E);
  20208. if (ret != 0) {
  20209. goto done;
  20210. }
  20211. TEST_SLEEP();
  20212. if (sigSz != sizeof(expSig)) {
  20213. ret = -9830;
  20214. goto done;
  20215. }
  20216. if (XMEMCMP(sig, expSig, sigSz) != 0) {
  20217. ret = -9831;
  20218. goto done;
  20219. }
  20220. sigSz = sizeof(sig);
  20221. do {
  20222. #if defined(WOLFSSL_ASYNC_CRYPT)
  20223. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  20224. #endif
  20225. if (ret == 0)
  20226. ret = wc_ecc_sign_hash(hash, sizeof(hash), sig, &sigSz, rng, key);
  20227. } while (ret == WC_PENDING_E);
  20228. if (ret != 0) {
  20229. goto done;
  20230. }
  20231. TEST_SLEEP();
  20232. done:
  20233. if (key_inited)
  20234. wc_ecc_free(key);
  20235. #ifdef WOLFSSL_SMALL_STACK
  20236. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20237. #endif
  20238. return ret;
  20239. }
  20240. #endif
  20241. #ifdef HAVE_ECC_CDH
  20242. static int ecc_test_cdh_vectors(WC_RNG* rng)
  20243. {
  20244. int ret;
  20245. #ifdef WOLFSSL_SMALL_STACK
  20246. ecc_key *pub_key = (ecc_key *)XMALLOC(sizeof *pub_key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20247. ecc_key *priv_key = (ecc_key *)XMALLOC(sizeof *priv_key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20248. #else
  20249. ecc_key pub_key[1], priv_key[1];
  20250. #endif
  20251. byte sharedA[32] = {0}, sharedB[32] = {0};
  20252. word32 x, z;
  20253. WOLFSSL_SMALL_STACK_STATIC const char* QCAVSx = "700c48f77f56584c5cc632ca65640db91b6bacce3a4df6b42ce7cc838833d287";
  20254. WOLFSSL_SMALL_STACK_STATIC const char* QCAVSy = "db71e509e3fd9b060ddb20ba5c51dcc5948d46fbf640dfe0441782cab85fa4ac";
  20255. WOLFSSL_SMALL_STACK_STATIC const char* dIUT = "7d7dc5f71eb29ddaf80d6214632eeae03d9058af1fb6d22ed80badb62bc1a534";
  20256. WOLFSSL_SMALL_STACK_STATIC const char* QIUTx = "ead218590119e8876b29146ff89ca61770c4edbbf97d38ce385ed281d8a6b230";
  20257. WOLFSSL_SMALL_STACK_STATIC const char* QIUTy = "28af61281fd35e2fa7002523acc85a429cb06ee6648325389f59edfce1405141";
  20258. WOLFSSL_SMALL_STACK_STATIC const char* ZIUT = "46fc62106420ff012e54a434fbdd2d25ccc5852060561e68040dd7778997bd7b";
  20259. #ifdef WOLFSSL_SMALL_STACK
  20260. if ((pub_key == NULL) ||
  20261. (priv_key == NULL)) {
  20262. ret = MEMORY_E;
  20263. goto done;
  20264. }
  20265. #endif
  20266. XMEMSET(pub_key, 0, sizeof *pub_key);
  20267. XMEMSET(priv_key, 0, sizeof *priv_key);
  20268. /* setup private and public keys */
  20269. ret = wc_ecc_init_ex(pub_key, HEAP_HINT, devId);
  20270. if (ret != 0)
  20271. goto done;
  20272. ret = wc_ecc_init_ex(priv_key, HEAP_HINT, devId);
  20273. if (ret != 0)
  20274. goto done;
  20275. wc_ecc_set_flags(pub_key, WC_ECC_FLAG_COFACTOR);
  20276. wc_ecc_set_flags(priv_key, WC_ECC_FLAG_COFACTOR);
  20277. ret = wc_ecc_import_raw(pub_key, QCAVSx, QCAVSy, NULL, "SECP256R1");
  20278. if (ret != 0)
  20279. goto done;
  20280. ret = wc_ecc_import_raw(priv_key, QIUTx, QIUTy, dIUT, "SECP256R1");
  20281. if (ret != 0)
  20282. goto done;
  20283. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  20284. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  20285. !defined(HAVE_SELFTEST)
  20286. ret = wc_ecc_set_rng(priv_key, rng);
  20287. if (ret != 0)
  20288. goto done;
  20289. #else
  20290. (void)rng;
  20291. #endif
  20292. /* compute ECC Cofactor shared secret */
  20293. x = sizeof(sharedA);
  20294. do {
  20295. #if defined(WOLFSSL_ASYNC_CRYPT)
  20296. ret = wc_AsyncWait(ret, &priv_key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  20297. #endif
  20298. if (ret == 0)
  20299. ret = wc_ecc_shared_secret(priv_key, pub_key, sharedA, &x);
  20300. } while (ret == WC_PENDING_E);
  20301. if (ret != 0) {
  20302. goto done;
  20303. }
  20304. TEST_SLEEP();
  20305. /* read in expected Z */
  20306. z = sizeof(sharedB);
  20307. ret = Base16_Decode((const byte*)ZIUT, (word32)XSTRLEN(ZIUT), sharedB, &z);
  20308. if (ret != 0)
  20309. goto done;
  20310. /* compare results */
  20311. if (x != z || XMEMCMP(sharedA, sharedB, x)) {
  20312. ERROR_OUT(-9840, done);
  20313. }
  20314. done:
  20315. #ifdef WOLFSSL_SMALL_STACK
  20316. if (priv_key) {
  20317. wc_ecc_free(priv_key);
  20318. XFREE(priv_key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20319. }
  20320. if (pub_key) {
  20321. wc_ecc_free(pub_key);
  20322. XFREE(pub_key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20323. }
  20324. #else
  20325. wc_ecc_free(priv_key);
  20326. wc_ecc_free(pub_key);
  20327. #endif
  20328. return ret;
  20329. }
  20330. #endif /* HAVE_ECC_CDH */
  20331. #endif /* HAVE_ECC_VECTOR_TEST */
  20332. #ifdef HAVE_ECC_KEY_IMPORT
  20333. /* returns 0 on success */
  20334. static int ecc_test_make_pub(WC_RNG* rng)
  20335. {
  20336. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20337. ecc_key *key = (ecc_key *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20338. #if defined(HAVE_ECC_DHE) && defined(HAVE_ECC_KEY_EXPORT)
  20339. ecc_key *pub = (ecc_key *)XMALLOC(sizeof *pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20340. #endif
  20341. byte *exportBuf = (byte *)XMALLOC(ECC_BUFSIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20342. byte *tmp = (byte *)XMALLOC(ECC_BUFSIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20343. #else
  20344. ecc_key key[1];
  20345. #if defined(HAVE_ECC_DHE) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG) && \
  20346. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  20347. ecc_key pub[1];
  20348. #endif
  20349. byte exportBuf[ECC_BUFSIZE];
  20350. byte tmp[ECC_BUFSIZE];
  20351. #endif
  20352. const byte* msg = (const byte*)"test wolfSSL ECC public gen";
  20353. word32 x;
  20354. word32 tmpSz;
  20355. int ret = 0;
  20356. ecc_point* pubPoint = NULL;
  20357. #ifdef HAVE_ECC_VERIFY
  20358. int verify = 0;
  20359. #endif
  20360. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20361. if ((key == NULL) ||
  20362. #if defined(HAVE_ECC_DHE) && defined(HAVE_ECC_KEY_EXPORT)
  20363. (pub == NULL) ||
  20364. #endif
  20365. (exportBuf == NULL) ||
  20366. (tmp == NULL))
  20367. ERROR_OUT(MEMORY_E, done);
  20368. #endif
  20369. (void)msg;
  20370. (void)verify;
  20371. (void)exportBuf;
  20372. (void)rng;
  20373. wc_ecc_init_ex(key, HEAP_HINT, devId);
  20374. #ifndef NO_ECC256
  20375. #ifdef USE_CERT_BUFFERS_256
  20376. XMEMCPY(tmp, ecc_key_der_256, (size_t)sizeof_ecc_key_der_256);
  20377. tmpSz = (size_t)sizeof_ecc_key_der_256;
  20378. #else
  20379. {
  20380. XFILE file = XFOPEN(eccKeyDerFile, "rb");
  20381. if (!file) {
  20382. ERROR_OUT(-9850, done);
  20383. }
  20384. tmpSz = (word32)XFREAD(tmp, 1, ECC_BUFSIZE, file);
  20385. XFCLOSE(file);
  20386. }
  20387. #endif /* USE_CERT_BUFFERS_256 */
  20388. /* import private only then test with */
  20389. ret = wc_ecc_import_private_key(tmp, tmpSz, NULL, 0, NULL);
  20390. if (ret == 0) {
  20391. ERROR_OUT(-9851, done);
  20392. }
  20393. ret = wc_ecc_import_private_key(NULL, tmpSz, NULL, 0, key);
  20394. if (ret == 0) {
  20395. ERROR_OUT(-9852, done);
  20396. }
  20397. x = 0;
  20398. ret = wc_EccPrivateKeyDecode(tmp, &x, key, tmpSz);
  20399. if (ret != 0) {
  20400. ERROR_OUT(-9853, done);
  20401. }
  20402. #ifdef HAVE_ECC_KEY_EXPORT
  20403. x = ECC_BUFSIZE;
  20404. ret = wc_ecc_export_private_only(key, exportBuf, &x);
  20405. if (ret != 0) {
  20406. ERROR_OUT(-9854, done);
  20407. }
  20408. /* make private only key */
  20409. wc_ecc_free(key);
  20410. wc_ecc_init_ex(key, HEAP_HINT, devId);
  20411. ret = wc_ecc_import_private_key(exportBuf, x, NULL, 0, key);
  20412. if (ret != 0) {
  20413. ERROR_OUT(-9855, done);
  20414. }
  20415. x = ECC_BUFSIZE;
  20416. ret = wc_ecc_export_x963_ex(key, exportBuf, &x, 0);
  20417. if (ret == 0) {
  20418. ERROR_OUT(-9856, done);
  20419. }
  20420. #endif /* HAVE_ECC_KEY_EXPORT */
  20421. ret = wc_ecc_make_pub(NULL, NULL);
  20422. if (ret == 0) {
  20423. ERROR_OUT(-9857, done);
  20424. }
  20425. TEST_SLEEP();
  20426. #ifndef WOLFSSL_NO_MALLOC
  20427. pubPoint = wc_ecc_new_point_h(HEAP_HINT);
  20428. if (pubPoint == NULL) {
  20429. ERROR_OUT(-9858, done);
  20430. }
  20431. #if !defined(WOLFSSL_CRYPTOCELL)
  20432. ret = wc_ecc_make_pub(key, pubPoint);
  20433. #if defined(WOLFSSL_ASYNC_CRYPT)
  20434. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  20435. #endif
  20436. if (ret != 0) {
  20437. ERROR_OUT(-9859, done);
  20438. }
  20439. #endif
  20440. TEST_SLEEP();
  20441. #ifdef HAVE_ECC_KEY_EXPORT
  20442. /* export should still fail, is private only key */
  20443. x = ECC_BUFSIZE;
  20444. ret = wc_ecc_export_x963_ex(key, exportBuf, &x, 0);
  20445. if (ret == 0) {
  20446. ERROR_OUT(-9860, done);
  20447. }
  20448. #endif /* HAVE_ECC_KEY_EXPORT */
  20449. #endif /* !WOLFSSL_NO_MALLOC */
  20450. #endif /* !NO_ECC256 */
  20451. /* create a new key since above test for loading key is not supported */
  20452. #if defined(WOLFSSL_CRYPTOCELL) || defined(NO_ECC256) || \
  20453. defined(WOLFSSL_QNX_CAAM) || defined(WOLFSSL_SE050) || \
  20454. defined(WOLFSSL_SECO_CAAM)
  20455. ret = wc_ecc_make_key(rng, ECC_KEYGEN_SIZE, key);
  20456. if (ret != 0) {
  20457. ERROR_OUT(-9861, done);
  20458. }
  20459. #endif
  20460. #if defined(HAVE_ECC_SIGN) && (!defined(ECC_TIMING_RESISTANT) || \
  20461. (defined(ECC_TIMING_RESISTANT) && !defined(WC_NO_RNG))) && \
  20462. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  20463. tmpSz = ECC_BUFSIZE;
  20464. ret = 0;
  20465. do {
  20466. #if defined(WOLFSSL_ASYNC_CRYPT)
  20467. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  20468. #endif
  20469. if (ret == 0) {
  20470. ret = wc_ecc_sign_hash(msg, (word32)XSTRLEN((const char* )msg), tmp,
  20471. &tmpSz, rng, key);
  20472. }
  20473. } while (ret == WC_PENDING_E);
  20474. if (ret != 0) {
  20475. ERROR_OUT(-9862, done);
  20476. }
  20477. TEST_SLEEP();
  20478. #ifdef HAVE_ECC_VERIFY
  20479. /* try verify with private only key */
  20480. ret = 0;
  20481. do {
  20482. #if defined(WOLFSSL_ASYNC_CRYPT)
  20483. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  20484. #endif
  20485. if (ret == 0) {
  20486. ret = wc_ecc_verify_hash(tmp, tmpSz, msg,
  20487. (word32)XSTRLEN((const char*)msg), &verify, key);
  20488. }
  20489. } while (ret == WC_PENDING_E);
  20490. if (ret != 0) {
  20491. ERROR_OUT(-9863, done);
  20492. }
  20493. if (verify != 1) {
  20494. ERROR_OUT(-9864, done);
  20495. }
  20496. TEST_SLEEP();
  20497. #ifdef HAVE_ECC_KEY_EXPORT
  20498. /* exporting the public part should now work */
  20499. x = ECC_BUFSIZE;
  20500. ret = wc_ecc_export_x963_ex(key, exportBuf, &x, 0);
  20501. if (ret != 0) {
  20502. ERROR_OUT(-9865, done);
  20503. }
  20504. #endif /* HAVE_ECC_KEY_EXPORT */
  20505. #endif /* HAVE_ECC_VERIFY */
  20506. #endif /* HAVE_ECC_SIGN */
  20507. #if defined(HAVE_ECC_DHE) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  20508. /* now test private only key with creating a shared secret */
  20509. x = ECC_BUFSIZE;
  20510. ret = wc_ecc_export_private_only(key, exportBuf, &x);
  20511. if (ret != 0) {
  20512. ERROR_OUT(-9866, done);
  20513. }
  20514. #if !defined(WOLFSSL_QNX_CAAM) && !defined(WOLFSSL_SE050)
  20515. /* make private only key */
  20516. wc_ecc_free(key);
  20517. wc_ecc_init_ex(key, HEAP_HINT, devId);
  20518. ret = wc_ecc_import_private_key(exportBuf, x, NULL, 0, key);
  20519. if (ret != 0) {
  20520. ERROR_OUT(-9867, done);
  20521. }
  20522. /* check that public export fails with private only key */
  20523. x = ECC_BUFSIZE;
  20524. ret = wc_ecc_export_x963_ex(key, exportBuf, &x, 0);
  20525. if (ret == 0) {
  20526. ERROR_OUT(-9868, done);
  20527. }
  20528. #endif /* WOLFSSL_QNX_CAAM */
  20529. #ifndef WOLF_CRYPTO_CB_ONLY_ECC
  20530. /* make public key for shared secret */
  20531. wc_ecc_init_ex(pub, HEAP_HINT, devId);
  20532. ret = wc_ecc_make_key(rng, ECC_KEYGEN_SIZE, pub);
  20533. #ifdef HAVE_ECC_CDH
  20534. wc_ecc_set_flags(key, WC_ECC_FLAG_COFACTOR);
  20535. #endif
  20536. #if defined(WOLFSSL_ASYNC_CRYPT)
  20537. ret = wc_AsyncWait(ret, &pub->asyncDev, WC_ASYNC_FLAG_NONE);
  20538. #endif
  20539. if (ret != 0) {
  20540. ERROR_OUT(-9869, done);
  20541. }
  20542. TEST_SLEEP();
  20543. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  20544. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  20545. !defined(HAVE_SELFTEST)
  20546. ret = wc_ecc_set_rng(key, rng);
  20547. if (ret != 0)
  20548. goto done;
  20549. #endif
  20550. x = ECC_BUFSIZE;
  20551. do {
  20552. #if defined(WOLFSSL_ASYNC_CRYPT)
  20553. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  20554. #endif
  20555. if (ret == 0) {
  20556. ret = wc_ecc_shared_secret(key, pub, exportBuf, &x);
  20557. }
  20558. } while (ret == WC_PENDING_E);
  20559. wc_ecc_free(pub);
  20560. if (ret != 0) {
  20561. ERROR_OUT(-9870, done);
  20562. }
  20563. TEST_SLEEP();
  20564. #endif /* HAVE_ECC_DHE && HAVE_ECC_KEY_EXPORT && !WC_NO_RNG */
  20565. #endif /* WOLF_CRYPTO_CB_ONLY_ECC */
  20566. ret = 0;
  20567. done:
  20568. wc_ecc_del_point_h(pubPoint, HEAP_HINT);
  20569. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20570. if (key != NULL) {
  20571. wc_ecc_free(key);
  20572. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20573. }
  20574. #if defined(HAVE_ECC_DHE) && defined(HAVE_ECC_KEY_EXPORT)
  20575. if (pub != NULL)
  20576. XFREE(pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20577. #endif
  20578. if (exportBuf != NULL)
  20579. XFREE(exportBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20580. if (tmp != NULL)
  20581. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20582. #else
  20583. wc_ecc_free(key);
  20584. #endif
  20585. return ret;
  20586. }
  20587. #if defined(HAVE_ECC_KEY_EXPORT) && !defined(NO_ASN_CRYPT) && \
  20588. !defined(WC_NO_RNG) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  20589. static int ecc_test_key_decode(WC_RNG* rng, int keySize)
  20590. {
  20591. int ret;
  20592. #ifdef WOLFSSL_SMALL_STACK
  20593. ecc_key *eccKey = (ecc_key *)XMALLOC(sizeof *eccKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20594. byte *tmpBuf = (byte *)XMALLOC(ECC_BUFSIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20595. #else
  20596. ecc_key eccKey[1];
  20597. byte tmpBuf[ECC_BUFSIZE];
  20598. #endif
  20599. word32 tmpSz;
  20600. word32 idx;
  20601. #ifdef WOLFSSL_SMALL_STACK
  20602. if ((eccKey == NULL) || (tmpBuf == NULL))
  20603. ERROR_OUT(MEMORY_E, done);
  20604. #endif
  20605. ret = wc_ecc_init(eccKey);
  20606. if (ret != 0) {
  20607. goto done;
  20608. }
  20609. ret = wc_ecc_make_key(rng, keySize, eccKey);
  20610. #if defined(WOLFSSL_ASYNC_CRYPT)
  20611. ret = wc_AsyncWait(ret, &eccKey->asyncDev, WC_ASYNC_FLAG_NONE);
  20612. #endif
  20613. if (ret != 0) {
  20614. goto done;
  20615. }
  20616. tmpSz = ECC_BUFSIZE;
  20617. ret = wc_EccKeyToDer(eccKey, tmpBuf, tmpSz);
  20618. wc_ecc_free(eccKey);
  20619. if (ret < 0) {
  20620. goto done;
  20621. }
  20622. tmpSz = ret;
  20623. ret = wc_ecc_init(eccKey);
  20624. if (ret != 0) {
  20625. goto done;
  20626. }
  20627. idx = 0;
  20628. ret = wc_EccPrivateKeyDecode(tmpBuf, &idx, eccKey, tmpSz);
  20629. if (ret != 0) {
  20630. goto done;
  20631. }
  20632. wc_ecc_free(eccKey);
  20633. ret = wc_ecc_init(eccKey);
  20634. if (ret != 0) {
  20635. goto done;
  20636. }
  20637. idx = 0;
  20638. ret = wc_EccPublicKeyDecode(tmpBuf, &idx, eccKey, tmpSz);
  20639. if (ret != 0) {
  20640. goto done;
  20641. }
  20642. ret = 0;
  20643. done:
  20644. #ifdef WOLFSSL_SMALL_STACK
  20645. if (eccKey != NULL) {
  20646. wc_ecc_free(eccKey);
  20647. XFREE(eccKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20648. }
  20649. if (tmpBuf != NULL)
  20650. XFREE(tmpBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20651. #else
  20652. wc_ecc_free(eccKey);
  20653. #endif
  20654. return ret;
  20655. }
  20656. #endif /* HAVE_ECC_KEY_EXPORT && !NO_ASN_CRYPT */
  20657. #endif /* HAVE_ECC_KEY_IMPORT */
  20658. #if defined(HAVE_ECC_KEY_EXPORT) && !defined(NO_ASN_CRYPT) && \
  20659. !defined(WC_NO_RNG) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  20660. static int ecc_test_key_gen(WC_RNG* rng, int keySize)
  20661. {
  20662. int ret = 0;
  20663. int derSz;
  20664. #ifdef HAVE_PKCS8
  20665. word32 pkcs8Sz;
  20666. #endif
  20667. #ifdef WOLFSSL_SMALL_STACK
  20668. byte *der = (byte *)XMALLOC(ECC_BUFSIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20669. ecc_key *userA = (ecc_key *)XMALLOC(sizeof *userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20670. #else
  20671. byte der[ECC_BUFSIZE];
  20672. ecc_key userA[1];
  20673. #endif
  20674. #ifdef WOLFSSL_SMALL_STACK
  20675. if ((der == NULL) || (userA == NULL))
  20676. ERROR_OUT(MEMORY_E, done);
  20677. #endif
  20678. ret = wc_ecc_init_ex(userA, HEAP_HINT, devId);
  20679. if (ret != 0)
  20680. goto done;
  20681. ret = wc_ecc_make_key(rng, keySize, userA);
  20682. #if defined(WOLFSSL_ASYNC_CRYPT)
  20683. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_NONE);
  20684. #endif
  20685. if (ret != 0)
  20686. goto done;
  20687. TEST_SLEEP();
  20688. ret = wc_ecc_check_key(userA);
  20689. if (ret != 0)
  20690. goto done;
  20691. TEST_SLEEP();
  20692. derSz = wc_EccKeyToDer(userA, der, ECC_BUFSIZE);
  20693. if (derSz < 0) {
  20694. ERROR_OUT(derSz, done);
  20695. }
  20696. ret = SaveDerAndPem(der, derSz, eccCaKeyTempFile, eccCaKeyPemFile,
  20697. ECC_PRIVATEKEY_TYPE, -8347);
  20698. if (ret != 0) {
  20699. goto done;
  20700. }
  20701. /* test export of public key */
  20702. derSz = wc_EccPublicKeyToDer(userA, der, ECC_BUFSIZE, 1);
  20703. if (derSz < 0) {
  20704. ERROR_OUT(derSz, done);
  20705. }
  20706. if (derSz == 0) {
  20707. ERROR_OUT(-9890, done);
  20708. }
  20709. #ifdef HAVE_COMP_KEY
  20710. /* test export of compressed public key */
  20711. derSz = wc_EccPublicKeyToDer_ex(userA, der, ECC_BUFSIZE, 1, 1);
  20712. if (derSz < 0) {
  20713. ERROR_OUT(derSz, done);
  20714. }
  20715. if (derSz == 0) {
  20716. ERROR_OUT(-9890, done);
  20717. }
  20718. #endif
  20719. ret = SaveDerAndPem(der, derSz, eccPubKeyDerFile, NULL, 0, -8348);
  20720. if (ret != 0) {
  20721. goto done;
  20722. }
  20723. #ifdef HAVE_PKCS8
  20724. /* test export of PKCS#8 unencrypted private key */
  20725. pkcs8Sz = FOURK_BUF;
  20726. derSz = wc_EccPrivateKeyToPKCS8(userA, der, &pkcs8Sz);
  20727. if (derSz < 0) {
  20728. ERROR_OUT(derSz, done);
  20729. }
  20730. if (derSz == 0) {
  20731. ERROR_OUT(-9891, done);
  20732. }
  20733. ret = SaveDerAndPem(der, derSz, eccPkcs8KeyDerFile, NULL, 0, -8349);
  20734. if (ret != 0) {
  20735. goto done;
  20736. }
  20737. #endif /* HAVE_PKCS8 */
  20738. done:
  20739. #ifdef WOLFSSL_SMALL_STACK
  20740. if (der != NULL)
  20741. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20742. if (userA != NULL) {
  20743. wc_ecc_free(userA);
  20744. XFREE(userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20745. }
  20746. #else
  20747. wc_ecc_free(userA);
  20748. #endif
  20749. return ret;
  20750. }
  20751. #endif /* HAVE_ECC_KEY_EXPORT && !NO_ASN_CRYPT */
  20752. static int ecc_test_curve_size(WC_RNG* rng, int keySize, int testVerifyCount,
  20753. int curve_id, const ecc_set_type* dp)
  20754. {
  20755. #if (defined(HAVE_ECC_DHE) || defined(HAVE_ECC_CDH)) && !defined(WC_NO_RNG) && \
  20756. !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A)
  20757. WC_DECLARE_VAR(sharedA, byte, ECC_SHARED_SIZE, HEAP_HINT);
  20758. WC_DECLARE_VAR(sharedB, byte, ECC_SHARED_SIZE, HEAP_HINT);
  20759. #endif
  20760. #ifdef HAVE_ECC_KEY_EXPORT
  20761. #define ECC_KEY_EXPORT_BUF_SIZE (MAX_ECC_BYTES * 2 + 32)
  20762. WC_DECLARE_VAR(exportBuf, byte, ECC_KEY_EXPORT_BUF_SIZE, HEAP_HINT);
  20763. #endif
  20764. word32 x = 0;
  20765. #if (defined(HAVE_ECC_DHE) || defined(HAVE_ECC_CDH)) && !defined(WC_NO_RNG) && \
  20766. !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A)
  20767. word32 y;
  20768. #endif
  20769. #if defined(HAVE_ECC_SIGN) && !defined(WOLFSSL_KCAPI_ECC)
  20770. WC_DECLARE_VAR(sig, byte, ECC_SIG_SIZE, HEAP_HINT);
  20771. WC_DECLARE_VAR(digest, byte, ECC_DIGEST_SIZE, HEAP_HINT);
  20772. int i;
  20773. #ifdef HAVE_ECC_VERIFY
  20774. int verify;
  20775. #endif /* HAVE_ECC_VERIFY */
  20776. #endif /* HAVE_ECC_SIGN */
  20777. int ret;
  20778. #ifdef WOLFSSL_SMALL_STACK
  20779. ecc_key *userA = (ecc_key *)XMALLOC(sizeof *userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20780. ecc_key *userB = (ecc_key *)XMALLOC(sizeof *userB, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20781. ecc_key *pubKey = (ecc_key *)XMALLOC(sizeof *pubKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20782. #else
  20783. ecc_key userA[1];
  20784. ecc_key userB[1];
  20785. ecc_key pubKey[1];
  20786. #endif
  20787. #ifndef WC_NO_RNG
  20788. int curveSize;
  20789. #endif
  20790. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  20791. #if (defined(HAVE_ECC_DHE) || defined(HAVE_ECC_CDH)) && !defined(WC_NO_RNG) && \
  20792. !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A)
  20793. if (sharedA == NULL || sharedB == NULL)
  20794. ERROR_OUT(-9900, done);
  20795. #endif
  20796. #ifdef HAVE_ECC_KEY_EXPORT
  20797. if (exportBuf == NULL)
  20798. ERROR_OUT(-9901, done);
  20799. #endif
  20800. #if defined(HAVE_ECC_SIGN) && !defined(WOLFSSL_KCAPI_ECC)
  20801. if (sig == NULL || digest == NULL)
  20802. ERROR_OUT(-9902, done);
  20803. #endif
  20804. #endif /* WOLFSSL_SMALL_STACK */
  20805. (void)testVerifyCount;
  20806. (void)dp;
  20807. (void)x;
  20808. #ifdef WOLFSSL_SMALL_STACK
  20809. if ((userA == NULL) ||
  20810. (userB == NULL) ||
  20811. (pubKey == NULL))
  20812. ERROR_OUT(-9903, done);
  20813. #endif
  20814. XMEMSET(userA, 0, sizeof *userA);
  20815. XMEMSET(userB, 0, sizeof *userB);
  20816. XMEMSET(pubKey, 0, sizeof *pubKey);
  20817. ret = wc_ecc_init_ex(userA, HEAP_HINT, devId);
  20818. if (ret != 0)
  20819. ERROR_OUT(-9904, done);
  20820. ret = wc_ecc_init_ex(userB, HEAP_HINT, devId);
  20821. if (ret != 0)
  20822. ERROR_OUT(-9905, done);
  20823. ret = wc_ecc_init_ex(pubKey, HEAP_HINT, devId);
  20824. if (ret != 0)
  20825. ERROR_OUT(-9906, done);
  20826. #ifdef WOLFSSL_CUSTOM_CURVES
  20827. if (dp != NULL) {
  20828. ret = wc_ecc_set_custom_curve(userA, dp);
  20829. if (ret != 0)
  20830. ERROR_OUT(-9907, done);
  20831. ret = wc_ecc_set_custom_curve(userB, dp);
  20832. if (ret != 0)
  20833. ERROR_OUT(-9908, done);
  20834. }
  20835. #endif
  20836. #ifndef WC_NO_RNG
  20837. ret = wc_ecc_make_key_ex(rng, keySize, userA, curve_id);
  20838. #if defined(WOLFSSL_ASYNC_CRYPT)
  20839. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_NONE);
  20840. #endif
  20841. #ifdef WOLF_CRYPTO_CB_ONLY_ECC
  20842. if (ret == NO_VALID_DEVID) {
  20843. ret = 0;
  20844. goto done; /* no software case */
  20845. }
  20846. #endif
  20847. if (ret == ECC_CURVE_OID_E)
  20848. goto done; /* catch case, where curve is not supported */
  20849. if (ret != 0)
  20850. ERROR_OUT(-9910, done);
  20851. TEST_SLEEP();
  20852. if (wc_ecc_get_curve_idx(curve_id) != -1) {
  20853. curveSize = wc_ecc_get_curve_size_from_id(userA->dp->id);
  20854. if (curveSize != userA->dp->size)
  20855. ERROR_OUT(-9911, done);
  20856. }
  20857. ret = wc_ecc_check_key(userA);
  20858. if (ret != 0)
  20859. ERROR_OUT(-9912, done);
  20860. TEST_SLEEP();
  20861. /* ATECC508/608 configuration may not support more than one ECDH key */
  20862. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A)
  20863. ret = wc_ecc_make_key_ex(rng, keySize, userB, curve_id);
  20864. #if defined(WOLFSSL_ASYNC_CRYPT)
  20865. ret = wc_AsyncWait(ret, &userB->asyncDev, WC_ASYNC_FLAG_NONE);
  20866. #endif
  20867. if (ret != 0)
  20868. ERROR_OUT(-9914, done);
  20869. TEST_SLEEP();
  20870. /* only perform the below tests if the key size matches */
  20871. if (dp == NULL && keySize > 0 && wc_ecc_size(userA) != keySize)
  20872. ERROR_OUT(ECC_CURVE_OID_E, done);
  20873. #ifdef HAVE_ECC_DHE
  20874. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  20875. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  20876. !defined(HAVE_SELFTEST)
  20877. ret = wc_ecc_set_rng(userA, rng);
  20878. if (ret != 0)
  20879. ERROR_OUT(-9915, done);
  20880. ret = wc_ecc_set_rng(userB, rng);
  20881. if (ret != 0)
  20882. ERROR_OUT(-9916, done);
  20883. #endif
  20884. x = ECC_SHARED_SIZE;
  20885. do {
  20886. #if defined(WOLFSSL_ASYNC_CRYPT)
  20887. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  20888. #endif
  20889. if (ret == 0)
  20890. ret = wc_ecc_shared_secret(userA, userB, sharedA, &x);
  20891. } while (ret == WC_PENDING_E);
  20892. if (ret != 0) {
  20893. ERROR_OUT(-9917, done);
  20894. }
  20895. TEST_SLEEP();
  20896. y = ECC_SHARED_SIZE;
  20897. do {
  20898. #if defined(WOLFSSL_ASYNC_CRYPT)
  20899. ret = wc_AsyncWait(ret, &userB->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  20900. #endif
  20901. if (ret == 0)
  20902. ret = wc_ecc_shared_secret(userB, userA, sharedB, &y);
  20903. } while (ret == WC_PENDING_E);
  20904. if (ret != 0)
  20905. ERROR_OUT(-9918, done);
  20906. if (y != x)
  20907. ERROR_OUT(-9919, done);
  20908. if (XMEMCMP(sharedA, sharedB, x))
  20909. ERROR_OUT(-9920, done);
  20910. TEST_SLEEP();
  20911. #endif /* HAVE_ECC_DHE */
  20912. #ifdef HAVE_ECC_CDH
  20913. /* add cofactor flag */
  20914. wc_ecc_set_flags(userA, WC_ECC_FLAG_COFACTOR);
  20915. wc_ecc_set_flags(userB, WC_ECC_FLAG_COFACTOR);
  20916. x = ECC_SHARED_SIZE;
  20917. do {
  20918. #if defined(WOLFSSL_ASYNC_CRYPT)
  20919. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  20920. #endif
  20921. if (ret == 0)
  20922. ret = wc_ecc_shared_secret(userA, userB, sharedA, &x);
  20923. } while (ret == WC_PENDING_E);
  20924. if (ret != 0)
  20925. ERROR_OUT(-9921, done);
  20926. TEST_SLEEP();
  20927. y = ECC_SHARED_SIZE;
  20928. do {
  20929. #if defined(WOLFSSL_ASYNC_CRYPT)
  20930. ret = wc_AsyncWait(ret, &userB->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  20931. #endif
  20932. if (ret == 0)
  20933. ret = wc_ecc_shared_secret(userB, userA, sharedB, &y);
  20934. } while (ret == WC_PENDING_E);
  20935. if (ret != 0)
  20936. ERROR_OUT(-9922, done);
  20937. if (y != x)
  20938. ERROR_OUT(-9923, done);
  20939. if (XMEMCMP(sharedA, sharedB, x))
  20940. ERROR_OUT(-9924, done);
  20941. TEST_SLEEP();
  20942. /* remove cofactor flag */
  20943. wc_ecc_set_flags(userA, 0);
  20944. wc_ecc_set_flags(userB, 0);
  20945. #endif /* HAVE_ECC_CDH */
  20946. #endif /* !WOLFSSL_ATECC508A && WOLFSSL_ATECC608A */
  20947. #ifdef HAVE_ECC_KEY_EXPORT
  20948. x = ECC_KEY_EXPORT_BUF_SIZE;
  20949. ret = wc_ecc_export_x963_ex(userA, exportBuf, &x, 0);
  20950. if (ret != 0)
  20951. ERROR_OUT(-9925, done);
  20952. #ifdef HAVE_ECC_KEY_IMPORT
  20953. #ifdef WOLFSSL_CUSTOM_CURVES
  20954. if (dp != NULL) {
  20955. ret = wc_ecc_set_custom_curve(pubKey, dp);
  20956. if (ret != 0)
  20957. ERROR_OUT(-9926, done);
  20958. }
  20959. #endif
  20960. ret = wc_ecc_import_x963_ex(exportBuf, x, pubKey, curve_id);
  20961. if (ret != 0)
  20962. ERROR_OUT(-9927, done);
  20963. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A)
  20964. #ifdef HAVE_ECC_DHE
  20965. y = ECC_SHARED_SIZE;
  20966. do {
  20967. #if defined(WOLFSSL_ASYNC_CRYPT)
  20968. ret = wc_AsyncWait(ret, &userB->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  20969. #endif
  20970. if (ret == 0)
  20971. ret = wc_ecc_shared_secret(userB, pubKey, sharedB, &y);
  20972. } while (ret == WC_PENDING_E);
  20973. if (ret != 0)
  20974. ERROR_OUT(-9928, done);
  20975. if (XMEMCMP(sharedA, sharedB, y))
  20976. ERROR_OUT(-9929, done);
  20977. TEST_SLEEP();
  20978. #endif /* HAVE_ECC_DHE */
  20979. #ifdef HAVE_COMP_KEY
  20980. /* try compressed export / import too */
  20981. x = ECC_KEY_EXPORT_BUF_SIZE;
  20982. ret = wc_ecc_export_x963_ex(userA, exportBuf, &x, 1);
  20983. if (ret != 0)
  20984. ERROR_OUT(-9930, done);
  20985. wc_ecc_free(pubKey);
  20986. ret = wc_ecc_init_ex(pubKey, HEAP_HINT, devId);
  20987. if (ret != 0)
  20988. ERROR_OUT(-9931, done);
  20989. #ifdef WOLFSSL_CUSTOM_CURVES
  20990. if (dp != NULL) {
  20991. ret = wc_ecc_set_custom_curve(pubKey, dp);
  20992. if (ret != 0)
  20993. ERROR_OUT(-9932, done);
  20994. }
  20995. #endif
  20996. ret = wc_ecc_import_x963_ex(exportBuf, x, pubKey, curve_id);
  20997. if (ret != 0)
  20998. ERROR_OUT(-9933, done);
  20999. #ifdef HAVE_ECC_DHE
  21000. y = ECC_SHARED_SIZE;
  21001. do {
  21002. #if defined(WOLFSSL_ASYNC_CRYPT)
  21003. ret = wc_AsyncWait(ret, &userB->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  21004. #endif
  21005. if (ret == 0)
  21006. ret = wc_ecc_shared_secret(userB, pubKey, sharedB, &y);
  21007. } while (ret == WC_PENDING_E);
  21008. if (ret != 0)
  21009. ERROR_OUT(-9934, done);
  21010. if (XMEMCMP(sharedA, sharedB, y))
  21011. ERROR_OUT(-9935, done);
  21012. TEST_SLEEP();
  21013. #endif /* HAVE_ECC_DHE */
  21014. #endif /* HAVE_COMP_KEY */
  21015. #endif /* !WOLFSSL_ATECC508A && !WOLFSSL_ATECC608A */
  21016. #endif /* !WC_NO_RNG */
  21017. #endif /* HAVE_ECC_KEY_IMPORT */
  21018. #endif /* HAVE_ECC_KEY_EXPORT */
  21019. /* For KCAPI cannot sign using generated ECDH key */
  21020. #if !defined(ECC_TIMING_RESISTANT) || (defined(ECC_TIMING_RESISTANT) && \
  21021. !defined(WC_NO_RNG) && !defined(WOLFSSL_KCAPI_ECC))
  21022. #ifdef HAVE_ECC_SIGN
  21023. /* ECC w/out Shamir has issue with all 0 digest */
  21024. /* WC_BIGINT doesn't have 0 len well on hardware */
  21025. /* Cryptocell has issues with all 0 digest */
  21026. #if defined(ECC_SHAMIR) && !defined(WOLFSSL_ASYNC_CRYPT) && \
  21027. !defined(WOLFSSL_CRYPTOCELL)
  21028. /* test DSA sign hash with zeros */
  21029. for (i = 0; i < (int)ECC_DIGEST_SIZE; i++) {
  21030. digest[i] = 0;
  21031. }
  21032. x = ECC_SIG_SIZE;
  21033. do {
  21034. #if defined(WOLFSSL_ASYNC_CRYPT)
  21035. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  21036. #endif
  21037. if (ret == 0)
  21038. ret = wc_ecc_sign_hash(digest, ECC_DIGEST_SIZE, sig, &x, rng,
  21039. userA);
  21040. } while (ret == WC_PENDING_E);
  21041. if (ret != 0)
  21042. ERROR_OUT(-9936, done);
  21043. TEST_SLEEP();
  21044. #ifdef HAVE_ECC_VERIFY
  21045. for (i=0; i<testVerifyCount; i++) {
  21046. verify = 0;
  21047. do {
  21048. #if defined(WOLFSSL_ASYNC_CRYPT)
  21049. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  21050. #endif
  21051. if (ret == 0)
  21052. ret = wc_ecc_verify_hash(sig, x, digest, ECC_DIGEST_SIZE,
  21053. &verify, userA);
  21054. } while (ret == WC_PENDING_E);
  21055. if (ret != 0)
  21056. ERROR_OUT(-9937, done);
  21057. if (verify != 1)
  21058. ERROR_OUT(-9938, done);
  21059. TEST_SLEEP();
  21060. }
  21061. #endif /* HAVE_ECC_VERIFY */
  21062. #endif /* ECC_SHAMIR && !WOLFSSL_ASYNC_CRYPT && !WOLFSSL_CRYPTOCELL */
  21063. /* test DSA sign hash with sequence (0,1,2,3,4,...) */
  21064. for (i = 0; i < (int)ECC_DIGEST_SIZE; i++) {
  21065. digest[i] = (byte)i;
  21066. }
  21067. x = ECC_SIG_SIZE;
  21068. do {
  21069. #if defined(WOLFSSL_ASYNC_CRYPT)
  21070. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  21071. #endif
  21072. if (ret == 0)
  21073. ret = wc_ecc_sign_hash(digest, ECC_DIGEST_SIZE, sig, &x, rng,
  21074. userA);
  21075. } while (ret == WC_PENDING_E);
  21076. if (ret != 0)
  21077. ERROR_OUT(-9939, done);
  21078. TEST_SLEEP();
  21079. #ifdef HAVE_ECC_VERIFY
  21080. for (i=0; i<testVerifyCount; i++) {
  21081. verify = 0;
  21082. do {
  21083. #if defined(WOLFSSL_ASYNC_CRYPT)
  21084. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  21085. #endif
  21086. if (ret == 0)
  21087. ret = wc_ecc_verify_hash(sig, x, digest, ECC_DIGEST_SIZE,
  21088. &verify, userA);
  21089. } while (ret == WC_PENDING_E);
  21090. if (ret != 0)
  21091. ERROR_OUT(-9940, done);
  21092. if (verify != 1)
  21093. ERROR_OUT(-9941, done);
  21094. TEST_SLEEP();
  21095. }
  21096. #endif /* HAVE_ECC_VERIFY */
  21097. #endif /* HAVE_ECC_SIGN */
  21098. #endif /* !ECC_TIMING_RESISTANT || (ECC_TIMING_RESISTANT &&
  21099. * !WC_NO_RNG && !WOLFSSL_KCAPI_ECC) */
  21100. #if defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG) && \
  21101. !defined(WOLFSSL_ATECC508) && !defined(WOLFSSL_ATECC608A) && \
  21102. !defined(WOLFSSL_KCAPI_ECC)
  21103. x = ECC_KEY_EXPORT_BUF_SIZE;
  21104. ret = wc_ecc_export_private_only(userA, exportBuf, &x);
  21105. if (ret != 0)
  21106. ERROR_OUT(-9942, done);
  21107. #elif defined(HAVE_ECC_KEY_EXPORT)
  21108. (void)exportBuf;
  21109. #endif /* HAVE_ECC_KEY_EXPORT */
  21110. done:
  21111. #ifdef WOLFSSL_SMALL_STACK
  21112. if (userA != NULL) {
  21113. wc_ecc_free(userA);
  21114. XFREE(userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21115. }
  21116. if (userB != NULL) {
  21117. wc_ecc_free(userB);
  21118. XFREE(userB, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21119. }
  21120. if (pubKey != NULL) {
  21121. wc_ecc_free(pubKey);
  21122. XFREE(pubKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21123. }
  21124. #else
  21125. wc_ecc_free(pubKey);
  21126. wc_ecc_free(userB);
  21127. wc_ecc_free(userA);
  21128. #endif
  21129. #if defined(HAVE_ECC_DHE) || defined(HAVE_ECC_CDH)
  21130. WC_FREE_VAR(sharedA, HEAP_HINT);
  21131. WC_FREE_VAR(sharedB, HEAP_HINT);
  21132. #endif
  21133. #ifdef HAVE_ECC_KEY_EXPORT
  21134. WC_FREE_VAR(exportBuf, HEAP_HINT);
  21135. #endif
  21136. #ifdef HAVE_ECC_SIGN
  21137. WC_FREE_VAR(sig, HEAP_HINT);
  21138. WC_FREE_VAR(digest, HEAP_HINT);
  21139. #endif
  21140. (void)keySize;
  21141. (void)curve_id;
  21142. (void)rng;
  21143. return ret;
  21144. }
  21145. #undef ECC_TEST_VERIFY_COUNT
  21146. #define ECC_TEST_VERIFY_COUNT 2
  21147. static int ecc_test_curve(WC_RNG* rng, int keySize)
  21148. {
  21149. int ret;
  21150. ret = ecc_test_curve_size(rng, keySize, ECC_TEST_VERIFY_COUNT,
  21151. ECC_CURVE_DEF, NULL);
  21152. if (ret < 0) {
  21153. if (ret == ECC_CURVE_OID_E) {
  21154. /* ignore error for curves not found */
  21155. /* some curve sizes are only available with:
  21156. HAVE_ECC_SECPR2, HAVE_ECC_SECPR3, HAVE_ECC_BRAINPOOL
  21157. and HAVE_ECC_KOBLITZ */
  21158. }
  21159. else {
  21160. printf("ecc_test_curve_size %d failed!: %d\n", keySize, ret);
  21161. return ret;
  21162. }
  21163. }
  21164. #ifndef WOLF_CRYPTO_CB_ONLY_ECC
  21165. #ifdef HAVE_ECC_VECTOR_TEST
  21166. ret = ecc_test_vector(keySize);
  21167. if (ret < 0) {
  21168. printf("ecc_test_vector %d failed!: %d\n", keySize, ret);
  21169. return ret;
  21170. }
  21171. #endif
  21172. #if defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT) && \
  21173. !defined(NO_ASN_CRYPT) && !defined(WC_NO_RNG)
  21174. ret = ecc_test_key_decode(rng, keySize);
  21175. if (ret < 0) {
  21176. if (ret == ECC_CURVE_OID_E) {
  21177. /* ignore error for curves not found */
  21178. }
  21179. else {
  21180. printf("ecc_test_key_decode %d failed!: %d\n", keySize, ret);
  21181. return ret;
  21182. }
  21183. }
  21184. #endif
  21185. #if defined(HAVE_ECC_KEY_EXPORT) && !defined(NO_ASN_CRYPT) && !defined(WC_NO_RNG)
  21186. ret = ecc_test_key_gen(rng, keySize);
  21187. if (ret < 0) {
  21188. if (ret == ECC_CURVE_OID_E) {
  21189. /* ignore error for curves not found */
  21190. }
  21191. else {
  21192. printf("ecc_test_key_gen %d failed!: %d\n", keySize, ret);
  21193. return ret;
  21194. }
  21195. }
  21196. #endif
  21197. #endif /* WOLF_CRYPTO_CB_ONLY_ECC */
  21198. return 0;
  21199. }
  21200. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  21201. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  21202. defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT) && \
  21203. !defined(WOLFSSL_NO_MALLOC) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  21204. static int ecc_point_test(void)
  21205. {
  21206. int ret;
  21207. ecc_point* point;
  21208. ecc_point* point2;
  21209. #ifdef HAVE_COMP_KEY
  21210. ecc_point* point3;
  21211. ecc_point* point4;
  21212. #endif
  21213. word32 outLen;
  21214. byte out[65];
  21215. byte der[] = { 0x04, /* = Uncompressed */
  21216. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  21217. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  21218. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  21219. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  21220. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  21221. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  21222. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  21223. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  21224. #if defined(HAVE_COMP_KEY) && (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) || \
  21225. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  21226. byte derComp0[] = { 0x02, /* = Compressed, y even */
  21227. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  21228. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  21229. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  21230. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  21231. byte derComp1[] = { 0x03, /* = Compressed, y odd */
  21232. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  21233. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  21234. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  21235. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  21236. #endif
  21237. byte altDer[] = { 0x04, /* = Uncompressed */
  21238. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  21239. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  21240. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  21241. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  21242. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  21243. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  21244. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  21245. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
  21246. int curve_idx = wc_ecc_get_curve_idx(ECC_SECP256R1);
  21247. /* if curve P256 is not enabled then test should not fail */
  21248. if (curve_idx == ECC_CURVE_INVALID)
  21249. return 0;
  21250. outLen = sizeof(out);
  21251. point = wc_ecc_new_point();
  21252. if (point == NULL)
  21253. return -10000;
  21254. point2 = wc_ecc_new_point();
  21255. if (point2 == NULL) {
  21256. wc_ecc_del_point(point);
  21257. return -10001;
  21258. }
  21259. #ifdef HAVE_COMP_KEY
  21260. point3 = wc_ecc_new_point();
  21261. if (point3 == NULL) {
  21262. wc_ecc_del_point(point2);
  21263. wc_ecc_del_point(point);
  21264. return -10002;
  21265. }
  21266. point4 = wc_ecc_new_point();
  21267. if (point4 == NULL) {
  21268. wc_ecc_del_point(point3);
  21269. wc_ecc_del_point(point2);
  21270. wc_ecc_del_point(point);
  21271. return -10003;
  21272. }
  21273. #endif
  21274. /* Parameter Validation testing. */
  21275. wc_ecc_del_point(NULL);
  21276. ret = wc_ecc_import_point_der(NULL, sizeof(der), curve_idx, point);
  21277. if (ret != ECC_BAD_ARG_E) {
  21278. ret = -10004;
  21279. goto done;
  21280. }
  21281. ret = wc_ecc_import_point_der(der, sizeof(der), ECC_CURVE_INVALID, point);
  21282. if (ret != ECC_BAD_ARG_E) {
  21283. ret = -10005;
  21284. goto done;
  21285. }
  21286. ret = wc_ecc_import_point_der(der, sizeof(der), curve_idx, NULL);
  21287. if (ret != ECC_BAD_ARG_E) {
  21288. ret = -10006;
  21289. goto done;
  21290. }
  21291. ret = wc_ecc_export_point_der(-1, point, out, &outLen);
  21292. if (ret != ECC_BAD_ARG_E) {
  21293. ret = -10007;
  21294. goto done;
  21295. }
  21296. ret = wc_ecc_export_point_der(curve_idx, NULL, out, &outLen);
  21297. if (ret != ECC_BAD_ARG_E) {
  21298. ret = -10008;
  21299. goto done;
  21300. }
  21301. ret = wc_ecc_export_point_der(curve_idx, point, NULL, &outLen);
  21302. if (ret != LENGTH_ONLY_E || outLen != sizeof(out)) {
  21303. ret = -10009;
  21304. goto done;
  21305. }
  21306. ret = wc_ecc_export_point_der(curve_idx, point, out, NULL);
  21307. if (ret != ECC_BAD_ARG_E) {
  21308. ret = -10010;
  21309. goto done;
  21310. }
  21311. outLen = 0;
  21312. ret = wc_ecc_export_point_der(curve_idx, point, out, &outLen);
  21313. if (ret != BUFFER_E) {
  21314. ret = -10011;
  21315. goto done;
  21316. }
  21317. ret = wc_ecc_copy_point(NULL, NULL);
  21318. if (ret != ECC_BAD_ARG_E) {
  21319. ret = -10012;
  21320. goto done;
  21321. }
  21322. ret = wc_ecc_copy_point(NULL, point2);
  21323. if (ret != ECC_BAD_ARG_E) {
  21324. ret = -10013;
  21325. goto done;
  21326. }
  21327. ret = wc_ecc_copy_point(point, NULL);
  21328. if (ret != ECC_BAD_ARG_E) {
  21329. ret = -10014;
  21330. goto done;
  21331. }
  21332. ret = wc_ecc_cmp_point(NULL, NULL);
  21333. if (ret != BAD_FUNC_ARG) {
  21334. ret = -10015;
  21335. goto done;
  21336. }
  21337. ret = wc_ecc_cmp_point(NULL, point2);
  21338. if (ret != BAD_FUNC_ARG) {
  21339. ret = -10016;
  21340. goto done;
  21341. }
  21342. ret = wc_ecc_cmp_point(point, NULL);
  21343. if (ret != BAD_FUNC_ARG) {
  21344. ret = -10017;
  21345. goto done;
  21346. }
  21347. /* Use API. */
  21348. ret = wc_ecc_import_point_der(der, sizeof(der), curve_idx, point);
  21349. if (ret != 0) {
  21350. ret = -10018;
  21351. goto done;
  21352. }
  21353. outLen = sizeof(out);
  21354. ret = wc_ecc_export_point_der(curve_idx, point, out, &outLen);
  21355. if (ret != 0) {
  21356. ret = -10019;
  21357. goto done;
  21358. }
  21359. if (outLen != sizeof(der)) {
  21360. ret = -10020;
  21361. goto done;
  21362. }
  21363. if (XMEMCMP(out, der, outLen) != 0) {
  21364. ret = -10021;
  21365. goto done;
  21366. }
  21367. ret = wc_ecc_copy_point(point2, point);
  21368. if (ret != MP_OKAY) {
  21369. ret = -10022;
  21370. goto done;
  21371. }
  21372. ret = wc_ecc_cmp_point(point2, point);
  21373. if (ret != MP_EQ) {
  21374. ret = -10023;
  21375. goto done;
  21376. }
  21377. ret = wc_ecc_import_point_der(altDer, sizeof(altDer), curve_idx, point2);
  21378. if (ret != 0) {
  21379. ret = -10024;
  21380. goto done;
  21381. }
  21382. ret = wc_ecc_cmp_point(point2, point);
  21383. if (ret != MP_GT) {
  21384. ret = -10025;
  21385. goto done;
  21386. }
  21387. #if defined(HAVE_COMP_KEY) && (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) || \
  21388. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  21389. ret = wc_ecc_import_point_der(derComp0, sizeof(derComp0)*2-1, curve_idx, point3);
  21390. if (ret != 0) {
  21391. ret = -10026;
  21392. goto done;
  21393. }
  21394. ret = wc_ecc_import_point_der_ex(derComp0, sizeof(derComp0), curve_idx, point4, 0);
  21395. if (ret != 0) {
  21396. ret = -10027;
  21397. goto done;
  21398. }
  21399. ret = wc_ecc_cmp_point(point3, point4);
  21400. if (ret != MP_EQ) {
  21401. ret = -10028;
  21402. goto done;
  21403. }
  21404. ret = wc_ecc_import_point_der(derComp1, sizeof(derComp1)*2-1, curve_idx, point3);
  21405. if (ret != 0) {
  21406. ret = -10029;
  21407. goto done;
  21408. }
  21409. ret = wc_ecc_import_point_der_ex(derComp1, sizeof(derComp1), curve_idx, point4, 0);
  21410. if (ret != 0) {
  21411. ret = -10030;
  21412. goto done;
  21413. }
  21414. ret = wc_ecc_cmp_point(point3, point4);
  21415. if (ret != MP_EQ) {
  21416. ret = -10031;
  21417. goto done;
  21418. }
  21419. #endif
  21420. done:
  21421. #ifdef HAVE_COMP_KEY
  21422. wc_ecc_del_point(point4);
  21423. wc_ecc_del_point(point3);
  21424. #endif
  21425. wc_ecc_del_point(point2);
  21426. wc_ecc_del_point(point);
  21427. return ret;
  21428. }
  21429. #endif /* !WOLFSSL_ATECC508A && HAVE_ECC_KEY_IMPORT && HAVE_ECC_KEY_EXPORT */
  21430. #if !defined(NO_SIG_WRAPPER) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  21431. static int ecc_sig_test(WC_RNG* rng, ecc_key* key)
  21432. {
  21433. int ret;
  21434. word32 sigSz;
  21435. int size;
  21436. byte out[ECC_MAX_SIG_SIZE];
  21437. byte in[] = TEST_STRING;
  21438. WOLFSSL_SMALL_STACK_STATIC const byte hash[] = {
  21439. 0xf2, 0x02, 0x95, 0x65, 0xcb, 0xf6, 0x2a, 0x59,
  21440. 0x39, 0x2c, 0x05, 0xff, 0x0e, 0x29, 0xaf, 0xfe,
  21441. 0x47, 0x33, 0x8c, 0x99, 0x8d, 0x58, 0x64, 0x83,
  21442. 0xa6, 0x58, 0x0a, 0x33, 0x0b, 0x84, 0x5f, 0x5f
  21443. };
  21444. word32 inLen = (word32)XSTRLEN((char*)in);
  21445. size = wc_ecc_sig_size(key);
  21446. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_ECC, key, sizeof(*key));
  21447. if (ret != size)
  21448. return -10040;
  21449. sigSz = (word32)ret;
  21450. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_ECC, in,
  21451. inLen, out, &sigSz, key, sizeof(*key), rng);
  21452. if (ret != 0)
  21453. return -10041;
  21454. TEST_SLEEP();
  21455. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_ECC, in,
  21456. inLen, out, sigSz, key, sizeof(*key));
  21457. if (ret != 0)
  21458. return -10042;
  21459. TEST_SLEEP();
  21460. sigSz = (word32)sizeof(out);
  21461. ret = wc_SignatureGenerateHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_ECC,
  21462. hash, (int)sizeof(hash), out, &sigSz, key, sizeof(*key), rng);
  21463. if (ret != 0)
  21464. return -10043;
  21465. TEST_SLEEP();
  21466. ret = wc_SignatureVerifyHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_ECC,
  21467. hash, (int)sizeof(hash), out, sigSz, key, sizeof(*key));
  21468. if (ret != 0)
  21469. return -10044;
  21470. TEST_SLEEP();
  21471. return 0;
  21472. }
  21473. #endif
  21474. #if defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT) && \
  21475. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  21476. static int ecc_exp_imp_test(ecc_key* key)
  21477. {
  21478. int ret;
  21479. int curve_id;
  21480. #ifdef WOLFSSL_SMALL_STACK
  21481. ecc_key *keyImp = (ecc_key *)XMALLOC(sizeof *keyImp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21482. #else
  21483. ecc_key keyImp[1];
  21484. #endif
  21485. byte priv[32];
  21486. word32 privLen;
  21487. byte pub[65*2];
  21488. word32 pubLen, pubLenX, pubLenY;
  21489. const char qx[] = "7a4e287890a1a47ad3457e52f2f76a83"
  21490. "ce46cbc947616d0cbaa82323818a793d";
  21491. const char qy[] = "eec4084f5b29ebf29c44cce3b3059610"
  21492. "922f8b30ea6e8811742ac7238fe87308";
  21493. const char d[] = "8c14b793cb19137e323a6d2e2a870bca"
  21494. "2e7a493ec1153b3a95feb8a4873f8d08";
  21495. #ifdef WOLFSSL_SMALL_STACK
  21496. if (keyImp == NULL)
  21497. ERROR_OUT(-10050, done);
  21498. #endif
  21499. wc_ecc_init_ex(keyImp, HEAP_HINT, devId);
  21500. privLen = sizeof(priv);
  21501. ret = wc_ecc_export_private_only(key, priv, &privLen);
  21502. if (ret != 0) {
  21503. ret = -10051;
  21504. goto done;
  21505. }
  21506. pubLen = sizeof(pub);
  21507. ret = wc_ecc_export_point_der(key->idx, &key->pubkey, pub, &pubLen);
  21508. if (ret != 0) {
  21509. ret = -10052;
  21510. goto done;
  21511. }
  21512. ret = wc_ecc_import_private_key(priv, privLen, pub, pubLen, keyImp);
  21513. if (ret != 0) {
  21514. ret = -10053;
  21515. goto done;
  21516. }
  21517. wc_ecc_free(keyImp);
  21518. wc_ecc_init_ex(keyImp, HEAP_HINT, devId);
  21519. ret = wc_ecc_import_raw_ex(keyImp, qx, qy, d, ECC_SECP256R1);
  21520. if (ret != 0) {
  21521. ret = -10054;
  21522. goto done;
  21523. }
  21524. wc_ecc_free(keyImp);
  21525. wc_ecc_init_ex(keyImp, HEAP_HINT, devId);
  21526. curve_id = wc_ecc_get_curve_id(key->idx);
  21527. if (curve_id < 0) {
  21528. ret = -10055;
  21529. goto done;
  21530. }
  21531. /* test import private only */
  21532. ret = wc_ecc_import_private_key_ex(priv, privLen, NULL, 0, keyImp,
  21533. curve_id);
  21534. if (ret != 0) {
  21535. ret = -10056;
  21536. goto done;
  21537. }
  21538. wc_ecc_free(keyImp);
  21539. wc_ecc_init_ex(keyImp, HEAP_HINT, devId);
  21540. /* test export public raw */
  21541. pubLenX = pubLenY = 32;
  21542. ret = wc_ecc_export_public_raw(key, pub, &pubLenX, &pub[32], &pubLenY);
  21543. if (ret != 0) {
  21544. ret = -10057;
  21545. goto done;
  21546. }
  21547. #ifndef HAVE_SELFTEST
  21548. /* test import of public */
  21549. ret = wc_ecc_import_unsigned(keyImp, pub, &pub[32], NULL, ECC_SECP256R1);
  21550. if (ret != 0) {
  21551. ret = -10058;
  21552. goto done;
  21553. }
  21554. #endif
  21555. wc_ecc_free(keyImp);
  21556. wc_ecc_init_ex(keyImp, HEAP_HINT, devId);
  21557. /* test export private and public raw */
  21558. pubLenX = pubLenY = privLen = 32;
  21559. ret = wc_ecc_export_private_raw(key, pub, &pubLenX, &pub[32], &pubLenY,
  21560. priv, &privLen);
  21561. if (ret != 0) {
  21562. ret = -10059;
  21563. goto done;
  21564. }
  21565. #ifndef HAVE_SELFTEST
  21566. /* test import of private and public */
  21567. ret = wc_ecc_import_unsigned(keyImp, pub, &pub[32], priv, ECC_SECP256R1);
  21568. if (ret != 0) {
  21569. ret = -10060;
  21570. goto done;
  21571. }
  21572. #endif
  21573. done:
  21574. #ifdef WOLFSSL_SMALL_STACK
  21575. if (keyImp != NULL) {
  21576. wc_ecc_free(keyImp);
  21577. XFREE(keyImp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21578. }
  21579. #else
  21580. wc_ecc_free(keyImp);
  21581. #endif
  21582. return ret;
  21583. }
  21584. #endif /* HAVE_ECC_KEY_IMPORT && HAVE_ECC_KEY_EXPORT */
  21585. #if defined(HAVE_ECC_KEY_IMPORT) && !defined(WOLFSSL_VALIDATE_ECC_IMPORT) && \
  21586. !defined(WOLFSSL_CRYPTOCELL) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  21587. static int ecc_mulmod_test(ecc_key* key1)
  21588. {
  21589. int ret;
  21590. #ifdef WOLFSSL_SMALL_STACK
  21591. ecc_key *key2 = (ecc_key *)XMALLOC(sizeof *key2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21592. ecc_key *key3 = (ecc_key *)XMALLOC(sizeof *key3, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21593. #else
  21594. ecc_key key2[1];
  21595. ecc_key key3[1];
  21596. #endif
  21597. #ifdef WOLFSSL_SMALL_STACK
  21598. if ((key2 == NULL) || (key3 == NULL))
  21599. ERROR_OUT(MEMORY_E, done);
  21600. #endif
  21601. wc_ecc_init_ex(key2, HEAP_HINT, devId);
  21602. wc_ecc_init_ex(key3, HEAP_HINT, devId);
  21603. /* TODO: Use test data, test with WOLFSSL_VALIDATE_ECC_IMPORT. */
  21604. /* Need base point (Gx,Gy) and parameter A - load them as the public and
  21605. * private key in key2.
  21606. */
  21607. ret = wc_ecc_import_raw_ex(key2, key1->dp->Gx, key1->dp->Gy, key1->dp->Af,
  21608. ECC_SECP256R1);
  21609. if (ret != 0)
  21610. goto done;
  21611. /* Need a point (Gx,Gy) and prime - load them as the public and private key
  21612. * in key3.
  21613. */
  21614. ret = wc_ecc_import_raw_ex(key3, key1->dp->Gx, key1->dp->Gy,
  21615. key1->dp->prime, ECC_SECP256R1);
  21616. if (ret != 0)
  21617. goto done;
  21618. ret = wc_ecc_mulmod(&key1->k, &key2->pubkey, &key3->pubkey, &key2->k, &key3->k,
  21619. 1);
  21620. if (ret != 0) {
  21621. ret = -10070;
  21622. goto done;
  21623. }
  21624. done:
  21625. #ifdef WOLFSSL_SMALL_STACK
  21626. if (key2 != NULL) {
  21627. wc_ecc_free(key2);
  21628. XFREE(key2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21629. }
  21630. if (key3 != NULL) {
  21631. wc_ecc_free(key3);
  21632. XFREE(key3, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21633. }
  21634. #else
  21635. wc_ecc_free(key3);
  21636. wc_ecc_free(key2);
  21637. #endif
  21638. return ret;
  21639. }
  21640. #endif
  21641. #if defined(HAVE_ECC_DHE) && !defined(WC_NO_RNG) && \
  21642. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  21643. static int ecc_ssh_test(ecc_key* key, WC_RNG* rng)
  21644. {
  21645. int ret;
  21646. byte out[128];
  21647. word32 outLen = sizeof(out);
  21648. /* Parameter Validation testing. */
  21649. ret = wc_ecc_shared_secret_ssh(NULL, &key->pubkey, out, &outLen);
  21650. if (ret != BAD_FUNC_ARG)
  21651. return -10080;
  21652. ret = wc_ecc_shared_secret_ssh(key, NULL, out, &outLen);
  21653. if (ret != BAD_FUNC_ARG)
  21654. return -10081;
  21655. ret = wc_ecc_shared_secret_ssh(key, &key->pubkey, NULL, &outLen);
  21656. if (ret != BAD_FUNC_ARG)
  21657. return -10082;
  21658. ret = wc_ecc_shared_secret_ssh(key, &key->pubkey, out, NULL);
  21659. if (ret != BAD_FUNC_ARG)
  21660. return -10083;
  21661. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  21662. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  21663. !defined(HAVE_SELFTEST)
  21664. ret = wc_ecc_set_rng(key, rng);
  21665. if (ret != 0)
  21666. return -10084;
  21667. #else
  21668. (void)rng;
  21669. #endif
  21670. /* Use API. */
  21671. ret = 0;
  21672. do {
  21673. #if defined(WOLFSSL_ASYNC_CRYPT)
  21674. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  21675. #endif
  21676. if (ret == 0)
  21677. ret = wc_ecc_shared_secret_ssh(key, &key->pubkey, out, &outLen);
  21678. } while (ret == WC_PENDING_E);
  21679. if (ret != 0)
  21680. return -10085;
  21681. TEST_SLEEP();
  21682. return 0;
  21683. }
  21684. #endif /* HAVE_ECC_DHE && !WC_NO_RNG */
  21685. static int ecc_def_curve_test(WC_RNG *rng)
  21686. {
  21687. int ret;
  21688. #ifdef WOLFSSL_SMALL_STACK
  21689. ecc_key *key = (ecc_key *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21690. #else
  21691. ecc_key key[1];
  21692. #endif
  21693. #if (defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT)) || \
  21694. (defined(HAVE_ECC_KEY_IMPORT) && !defined(WOLFSSL_VALIDATE_ECC_IMPORT))
  21695. word32 idx = 0;
  21696. #endif
  21697. #ifdef WOLFSSL_SMALL_STACK
  21698. if (key == NULL)
  21699. ERROR_OUT(MEMORY_E, done);
  21700. #endif
  21701. wc_ecc_init_ex(key, HEAP_HINT, devId);
  21702. /* Use API */
  21703. ret = wc_ecc_set_flags(NULL, 0);
  21704. if (ret != BAD_FUNC_ARG) {
  21705. ret = -10090;
  21706. goto done;
  21707. }
  21708. ret = wc_ecc_set_flags(key, 0);
  21709. if (ret != 0) {
  21710. ret = -10091;
  21711. goto done;
  21712. }
  21713. #ifndef WOLF_CRYPTO_CB_ONLY_ECC
  21714. #ifndef WC_NO_RNG
  21715. ret = wc_ecc_make_key(rng, ECC_KEYGEN_SIZE, key);
  21716. #if defined(WOLFSSL_ASYNC_CRYPT)
  21717. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  21718. #endif
  21719. if (ret != 0) {
  21720. goto done;
  21721. }
  21722. #ifndef NO_SIG_WRAPPER
  21723. ret = ecc_sig_test(rng, key);
  21724. if (ret < 0)
  21725. goto done;
  21726. #endif
  21727. TEST_SLEEP();
  21728. #if defined(HAVE_ECC_DHE) && !defined(WOLFSSL_CRYPTOCELL) && \
  21729. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  21730. ret = ecc_ssh_test(key, rng);
  21731. if (ret < 0)
  21732. goto done;
  21733. #endif
  21734. wc_ecc_free(key);
  21735. #else
  21736. (void)rng;
  21737. #endif /* !WC_NO_RNG */
  21738. #if (defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT)) || \
  21739. (defined(HAVE_ECC_KEY_IMPORT) && !defined(WOLFSSL_VALIDATE_ECC_IMPORT))
  21740. /* Use test ECC key - ensure real private "d" exists */
  21741. #ifdef USE_CERT_BUFFERS_256
  21742. ret = wc_EccPrivateKeyDecode(ecc_key_der_256, &idx, key,
  21743. sizeof_ecc_key_der_256);
  21744. #else
  21745. {
  21746. XFILE file = XFOPEN(eccKeyDerFile, "rb");
  21747. byte der[128];
  21748. word32 derSz;
  21749. if (!file) {
  21750. ERROR_OUT(-10093, done);
  21751. }
  21752. derSz = (word32)XFREAD(der, 1, sizeof(der), file);
  21753. XFCLOSE(file);
  21754. ret = wc_EccPrivateKeyDecode(der, &idx, key, derSz);
  21755. }
  21756. #endif
  21757. if (ret != 0) {
  21758. goto done;
  21759. }
  21760. #if defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT)
  21761. ret = ecc_exp_imp_test(key);
  21762. if (ret < 0)
  21763. goto done;
  21764. #endif
  21765. #if defined(HAVE_ECC_KEY_IMPORT) && !defined(WOLFSSL_VALIDATE_ECC_IMPORT) && \
  21766. !defined(WOLFSSL_CRYPTOCELL)
  21767. ret = ecc_mulmod_test(key);
  21768. if (ret < 0)
  21769. goto done;
  21770. #endif
  21771. #endif
  21772. #else
  21773. (void)rng;
  21774. (void)idx;
  21775. #endif /* WOLF_CRYPTO_CB_ONLY_ECC */
  21776. done:
  21777. wc_ecc_free(key);
  21778. #ifdef WOLFSSL_SMALL_STACK
  21779. if (key != NULL) {
  21780. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21781. }
  21782. #endif
  21783. return ret;
  21784. }
  21785. #endif /* !NO_ECC256 || HAVE_ALL_CURVES */
  21786. #if defined(WOLFSSL_CERT_EXT) && \
  21787. (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  21788. static int ecc_decode_test(void)
  21789. {
  21790. int ret;
  21791. word32 inSz;
  21792. word32 inOutIdx;
  21793. #ifdef WOLFSSL_SMALL_STACK
  21794. ecc_key *key = (ecc_key *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21795. #else
  21796. ecc_key key[1];
  21797. #endif
  21798. /* SECP256R1 OID: 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07 */
  21799. /* This is ecc_clikeypub_der_256. */
  21800. WOLFSSL_SMALL_STACK_STATIC const byte good[] = {
  21801. 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce,
  21802. 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d,
  21803. 0x03, 0x01, 0x07, 0x03, 0x42, 0x00, 0x04, 0x55, 0xbf, 0xf4,
  21804. 0x0f, 0x44, 0x50, 0x9a, 0x3d, 0xce, 0x9b, 0xb7, 0xf0, 0xc5,
  21805. 0x4d, 0xf5, 0x70, 0x7b, 0xd4, 0xec, 0x24, 0x8e, 0x19, 0x80,
  21806. 0xec, 0x5a, 0x4c, 0xa2, 0x24, 0x03, 0x62, 0x2c, 0x9b, 0xda,
  21807. 0xef, 0xa2, 0x35, 0x12, 0x43, 0x84, 0x76, 0x16, 0xc6, 0x56,
  21808. 0x95, 0x06, 0xcc, 0x01, 0xa9, 0xbd, 0xf6, 0x75, 0x1a, 0x42,
  21809. 0xf7, 0xbd, 0xa9, 0xb2, 0x36, 0x22, 0x5f, 0xc7, 0x5d, 0x7f,
  21810. 0xb4 };
  21811. WOLFSSL_SMALL_STACK_STATIC const byte badNoObjId[] = { 0x30, 0x08, 0x30, 0x06, 0x03, 0x04,
  21812. 0x00, 0x04, 0x01, 0x01 };
  21813. WOLFSSL_SMALL_STACK_STATIC const byte badOneObjId[] = { 0x30, 0x0a, 0x30, 0x08, 0x06, 0x00,
  21814. 0x03, 0x04, 0x00, 0x04, 0x01, 0x01 };
  21815. WOLFSSL_SMALL_STACK_STATIC const byte badObjId1Len[] = { 0x30, 0x0c, 0x30, 0x0a, 0x06, 0x09,
  21816. 0x06, 0x00, 0x03, 0x04, 0x00, 0x04, 0x01, 0x01 };
  21817. WOLFSSL_SMALL_STACK_STATIC const byte badObj2d1Len[] = { 0x30, 0x0c, 0x30, 0x0a, 0x06, 0x00,
  21818. 0x06, 0x07, 0x03, 0x04, 0x00, 0x04, 0x01, 0x01 };
  21819. WOLFSSL_SMALL_STACK_STATIC const byte badNotBitStr[] = { 0x30, 0x14, 0x30, 0x0b, 0x06, 0x00,
  21820. 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07,
  21821. 0x04, 0x04, 0x00, 0x04, 0x01, 0x01 };
  21822. WOLFSSL_SMALL_STACK_STATIC const byte badBitStrLen[] = { 0x30, 0x14, 0x30, 0x0b, 0x06, 0x00,
  21823. 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07,
  21824. 0x03, 0x05, 0x00, 0x04, 0x01, 0x01 };
  21825. WOLFSSL_SMALL_STACK_STATIC const byte badNoBitStrZero[] = { 0x30, 0x13, 0x30, 0x0a, 0x06, 0x00,
  21826. 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07,
  21827. 0x03, 0x03, 0x04, 0x01, 0x01 };
  21828. WOLFSSL_SMALL_STACK_STATIC const byte badPoint[] = { 0x30, 0x12, 0x30, 0x09, 0x06, 0x00,
  21829. 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07,
  21830. 0x03, 0x03, 0x00, 0x04, 0x01 };
  21831. #ifdef WOLFSSL_SMALL_STACK
  21832. if (key == NULL)
  21833. ERROR_OUT(MEMORY_E, done);
  21834. #endif
  21835. XMEMSET(key, 0, sizeof *key);
  21836. wc_ecc_init_ex(key, HEAP_HINT, devId);
  21837. inSz = sizeof(good);
  21838. ret = wc_EccPublicKeyDecode(NULL, &inOutIdx, key, inSz);
  21839. if (ret != BAD_FUNC_ARG) {
  21840. ret = -10100;
  21841. goto done;
  21842. }
  21843. ret = wc_EccPublicKeyDecode(good, NULL, key, inSz);
  21844. if (ret != BAD_FUNC_ARG) {
  21845. ret = -10101;
  21846. goto done;
  21847. }
  21848. ret = wc_EccPublicKeyDecode(good, &inOutIdx, NULL, inSz);
  21849. if (ret != BAD_FUNC_ARG) {
  21850. ret = -10102;
  21851. goto done;
  21852. }
  21853. ret = wc_EccPublicKeyDecode(good, &inOutIdx, key, 0);
  21854. if (ret != BAD_FUNC_ARG) {
  21855. ret = -10103;
  21856. goto done;
  21857. }
  21858. /* Change offset to produce bad input data. */
  21859. inOutIdx = 2;
  21860. inSz = sizeof(good) - inOutIdx;
  21861. ret = wc_EccPublicKeyDecode(good, &inOutIdx, key, inSz);
  21862. if (ret != ASN_PARSE_E) {
  21863. ret = -10104;
  21864. goto done;
  21865. }
  21866. inOutIdx = 4;
  21867. inSz = sizeof(good) - inOutIdx;
  21868. ret = wc_EccPublicKeyDecode(good, &inOutIdx, key, inSz);
  21869. if (ret != ASN_PARSE_E) {
  21870. ret = -10105;
  21871. goto done;
  21872. }
  21873. /* Bad data. */
  21874. inSz = sizeof(badNoObjId);
  21875. inOutIdx = 0;
  21876. ret = wc_EccPublicKeyDecode(badNoObjId, &inOutIdx, key, inSz);
  21877. if (ret != ASN_OBJECT_ID_E && ret != ASN_PARSE_E) {
  21878. ret = -10106;
  21879. goto done;
  21880. }
  21881. inSz = sizeof(badOneObjId);
  21882. inOutIdx = 0;
  21883. ret = wc_EccPublicKeyDecode(badOneObjId, &inOutIdx, key, inSz);
  21884. if (ret != ASN_OBJECT_ID_E && ret != ASN_PARSE_E) {
  21885. ret = -10107;
  21886. goto done;
  21887. }
  21888. inSz = sizeof(badObjId1Len);
  21889. inOutIdx = 0;
  21890. ret = wc_EccPublicKeyDecode(badObjId1Len, &inOutIdx, key, inSz);
  21891. if (ret != ASN_PARSE_E) {
  21892. ret = -10108;
  21893. goto done;
  21894. }
  21895. inSz = sizeof(badObj2d1Len);
  21896. inOutIdx = 0;
  21897. ret = wc_EccPublicKeyDecode(badObj2d1Len, &inOutIdx, key, inSz);
  21898. if (ret != ASN_PARSE_E) {
  21899. ret = -10109;
  21900. goto done;
  21901. }
  21902. inSz = sizeof(badNotBitStr);
  21903. inOutIdx = 0;
  21904. ret = wc_EccPublicKeyDecode(badNotBitStr, &inOutIdx, key, inSz);
  21905. if (ret != ASN_BITSTR_E && ret != ASN_PARSE_E) {
  21906. ret = -10110;
  21907. goto done;
  21908. }
  21909. inSz = sizeof(badBitStrLen);
  21910. inOutIdx = 0;
  21911. ret = wc_EccPublicKeyDecode(badBitStrLen, &inOutIdx, key, inSz);
  21912. if (ret != ASN_PARSE_E) {
  21913. ret = -10111;
  21914. goto done;
  21915. }
  21916. inSz = sizeof(badNoBitStrZero);
  21917. inOutIdx = 0;
  21918. ret = wc_EccPublicKeyDecode(badNoBitStrZero, &inOutIdx, key, inSz);
  21919. if (ret != ASN_EXPECT_0_E && ret != ASN_PARSE_E) {
  21920. ret = -10112;
  21921. goto done;
  21922. }
  21923. inSz = sizeof(badPoint);
  21924. inOutIdx = 0;
  21925. ret = wc_EccPublicKeyDecode(badPoint, &inOutIdx, key, inSz);
  21926. if (ret != ASN_ECC_KEY_E && ret != ASN_PARSE_E) {
  21927. ret = -10113;
  21928. goto done;
  21929. }
  21930. inSz = sizeof(good);
  21931. inOutIdx = 0;
  21932. ret = wc_EccPublicKeyDecode(good, &inOutIdx, key, inSz);
  21933. if (ret != 0) {
  21934. ret = -10114;
  21935. goto done;
  21936. }
  21937. done:
  21938. #ifdef WOLFSSL_SMALL_STACK
  21939. if (key != NULL) {
  21940. wc_ecc_free(key);
  21941. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21942. }
  21943. #else
  21944. wc_ecc_free(key);
  21945. #endif
  21946. return ret;
  21947. }
  21948. #endif /* WOLFSSL_CERT_EXT */
  21949. #ifdef WOLFSSL_CUSTOM_CURVES
  21950. static const byte eccKeyExplicitCurve[] = {
  21951. 0x30, 0x81, 0xf5, 0x30, 0x81, 0xae, 0x06, 0x07,
  21952. 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01, 0x30,
  21953. 0x81, 0xa2, 0x02, 0x01, 0x01, 0x30, 0x2c, 0x06,
  21954. 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x01, 0x01,
  21955. 0x02, 0x21, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff,
  21956. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  21957. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  21958. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0xff,
  21959. 0xff, 0xfc, 0x2f, 0x30, 0x06, 0x04, 0x01, 0x00,
  21960. 0x04, 0x01, 0x07, 0x04, 0x41, 0x04, 0x79, 0xbe,
  21961. 0x66, 0x7e, 0xf9, 0xdc, 0xbb, 0xac, 0x55, 0xa0,
  21962. 0x62, 0x95, 0xce, 0x87, 0x0b, 0x07, 0x02, 0x9b,
  21963. 0xfc, 0xdb, 0x2d, 0xce, 0x28, 0xd9, 0x59, 0xf2,
  21964. 0x81, 0x5b, 0x16, 0xf8, 0x17, 0x98, 0x48, 0x3a,
  21965. 0xda, 0x77, 0x26, 0xa3, 0xc4, 0x65, 0x5d, 0xa4,
  21966. 0xfb, 0xfc, 0x0e, 0x11, 0x08, 0xa8, 0xfd, 0x17,
  21967. 0xb4, 0x48, 0xa6, 0x85, 0x54, 0x19, 0x9c, 0x47,
  21968. 0xd0, 0x8f, 0xfb, 0x10, 0xd4, 0xb8, 0x02, 0x21,
  21969. 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  21970. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  21971. 0xfe, 0xba, 0xae, 0xdc, 0xe6, 0xaf, 0x48, 0xa0,
  21972. 0x3b, 0xbf, 0xd2, 0x5e, 0x8c, 0xd0, 0x36, 0x41,
  21973. 0x41, 0x02, 0x01, 0x01, 0x03, 0x42, 0x00, 0x04,
  21974. 0x3c, 0x4c, 0xc9, 0x5e, 0x2e, 0xa2, 0x3d, 0x49,
  21975. 0xcc, 0x5b, 0xff, 0x4f, 0xc9, 0x2e, 0x1d, 0x4a,
  21976. 0xc6, 0x21, 0xf6, 0xf3, 0xe6, 0x0b, 0x4f, 0xa9,
  21977. 0x9d, 0x74, 0x99, 0xdd, 0x97, 0xc7, 0x6e, 0xbe,
  21978. 0x14, 0x2b, 0x39, 0x9d, 0x63, 0xc7, 0x97, 0x0d,
  21979. 0x45, 0x25, 0x40, 0x30, 0x77, 0x05, 0x76, 0x88,
  21980. 0x38, 0x96, 0x29, 0x7d, 0x9c, 0xe1, 0x50, 0xbe,
  21981. 0xac, 0xf0, 0x1d, 0x86, 0xf4, 0x2f, 0x65, 0x0b
  21982. };
  21983. static int ecc_test_custom_curves(WC_RNG* rng)
  21984. {
  21985. int ret;
  21986. word32 inOutIdx;
  21987. #ifdef WOLFSSL_SMALL_STACK
  21988. ecc_key *key = (ecc_key *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21989. #else
  21990. ecc_key key[1];
  21991. #endif
  21992. /* test use of custom curve - using BRAINPOOLP256R1 for test */
  21993. #if defined(HAVE_ECC_BRAINPOOL) && !defined(HAVE_INTEL_QA)
  21994. #ifndef WOLFSSL_ECC_CURVE_STATIC
  21995. WOLFSSL_SMALL_STACK_STATIC const ecc_oid_t ecc_oid_brainpoolp256r1[] = {
  21996. 0x2B,0x24,0x03,0x03,0x02,0x08,0x01,0x01,0x07
  21997. };
  21998. #define ecc_oid_brainpoolp256r1_sz \
  21999. (sizeof(ecc_oid_brainpoolp256r1) / sizeof(ecc_oid_t))
  22000. #else
  22001. #define ecc_oid_brainpoolp256r1 { \
  22002. 0x2B,0x24,0x03,0x03,0x02,0x08,0x01,0x01,0x07 \
  22003. }
  22004. #define ecc_oid_brainpoolp256r1_sz 9
  22005. #endif
  22006. #define ecc_oid_brainpoolp256r1_sum 104
  22007. WOLFSSL_SMALL_STACK_STATIC const ecc_set_type ecc_dp_brainpool256r1 = {
  22008. 32, /* size/bytes */
  22009. ECC_CURVE_CUSTOM, /* ID */
  22010. "BRAINPOOLP256R1", /* curve name */
  22011. "A9FB57DBA1EEA9BC3E660A909D838D726E3BF623D52620282013481D1F6E5377", /* prime */
  22012. "7D5A0975FC2C3057EEF67530417AFFE7FB8055C126DC5C6CE94A4B44F330B5D9", /* A */
  22013. "26DC5C6CE94A4B44F330B5D9BBD77CBF958416295CF7E1CE6BCCDC18FF8C07B6", /* B */
  22014. "A9FB57DBA1EEA9BC3E660A909D838D718C397AA3B561A6F7901E0E82974856A7", /* order */
  22015. "8BD2AEB9CB7E57CB2C4B482FFC81B7AFB9DE27E1E3BD23C23A4453BD9ACE3262", /* Gx */
  22016. "547EF835C3DAC4FD97F8461A14611DC9C27745132DED8E545C1D54C72F046997", /* Gy */
  22017. ecc_oid_brainpoolp256r1, /* oid/oidSz */
  22018. ecc_oid_brainpoolp256r1_sz,
  22019. ecc_oid_brainpoolp256r1_sum, /* oid sum */
  22020. 1, /* cofactor */
  22021. };
  22022. #endif /* HAVE_ECC_BRAINPOOL */
  22023. #ifdef WOLFSSL_SMALL_STACK
  22024. if (! key) {
  22025. ret = MEMORY_E;
  22026. goto done;
  22027. }
  22028. #endif
  22029. XMEMSET(key, 0, sizeof *key);
  22030. #if defined(HAVE_ECC_BRAINPOOL) && !defined(HAVE_INTEL_QA)
  22031. ret = ecc_test_curve_size(rng, 0, ECC_TEST_VERIFY_COUNT, ECC_CURVE_DEF,
  22032. &ecc_dp_brainpool256r1);
  22033. if (ret != 0) {
  22034. printf("ECC test for custom curve failed! %d\n", ret);
  22035. goto done;
  22036. }
  22037. #endif
  22038. #if defined(HAVE_ECC_BRAINPOOL) || defined(HAVE_ECC_KOBLITZ)
  22039. {
  22040. int curve_id;
  22041. #ifdef HAVE_ECC_BRAINPOOL
  22042. curve_id = ECC_BRAINPOOLP256R1;
  22043. #else
  22044. curve_id = ECC_SECP256K1;
  22045. #endif
  22046. /* Test and demonstrate use of non-SECP curve */
  22047. ret = ecc_test_curve_size(rng, 0, ECC_TEST_VERIFY_COUNT, curve_id, NULL);
  22048. if (ret < 0) {
  22049. printf("ECC test for curve_id %d failed! %d\n", curve_id, ret);
  22050. goto done;
  22051. }
  22052. }
  22053. #endif
  22054. ret = wc_ecc_init_ex(key, HEAP_HINT, devId);
  22055. if (ret != 0) {
  22056. ret = -10120;
  22057. goto done;
  22058. }
  22059. inOutIdx = 0;
  22060. ret = wc_EccPublicKeyDecode(eccKeyExplicitCurve, &inOutIdx, key,
  22061. sizeof(eccKeyExplicitCurve));
  22062. if (ret != 0)
  22063. ret = -10121;
  22064. done:
  22065. #ifdef WOLFSSL_SMALL_STACK
  22066. if (key) {
  22067. wc_ecc_free(key);
  22068. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22069. }
  22070. #else
  22071. wc_ecc_free(key);
  22072. #endif
  22073. (void)rng;
  22074. return ret;
  22075. }
  22076. #endif /* WOLFSSL_CUSTOM_CURVES */
  22077. #ifdef WOLFSSL_CERT_GEN
  22078. /* Make Cert / Sign example for ECC cert and ECC CA */
  22079. static int ecc_test_cert_gen(WC_RNG* rng)
  22080. {
  22081. int ret;
  22082. #ifdef WOLFSSL_SMALL_STACK
  22083. Cert *myCert = (Cert *)XMALLOC(sizeof *myCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22084. #ifdef WOLFSSL_TEST_CERT
  22085. DecodedCert *decode = (DecodedCert *)XMALLOC(sizeof *decode, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22086. #endif
  22087. ecc_key *caEccKey = (ecc_key *)XMALLOC(sizeof *caEccKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22088. ecc_key *certPubKey = (ecc_key *)XMALLOC(sizeof *certPubKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22089. #else
  22090. Cert myCert[1];
  22091. #ifdef WOLFSSL_TEST_CERT
  22092. DecodedCert decode[1];
  22093. #endif
  22094. ecc_key caEccKey[1];
  22095. ecc_key certPubKey[1];
  22096. #endif
  22097. int certSz;
  22098. size_t bytes;
  22099. word32 idx = 0;
  22100. #ifndef USE_CERT_BUFFERS_256
  22101. XFILE file;
  22102. #endif
  22103. #ifdef WOLFSSL_SMALL_STACK
  22104. byte* der = NULL;
  22105. #else
  22106. byte der[FOURK_BUF];
  22107. #endif
  22108. #ifdef WOLFSSL_SMALL_STACK
  22109. if ((myCert == NULL)
  22110. #ifdef WOLFSSL_TEST_CERT
  22111. || (decode == NULL)
  22112. #endif
  22113. || (caEccKey == NULL) || (certPubKey == NULL))
  22114. ERROR_OUT(MEMORY_E, exit);
  22115. #endif
  22116. XMEMSET(caEccKey, 0, sizeof *caEccKey);
  22117. XMEMSET(certPubKey, 0, sizeof *certPubKey);
  22118. #ifdef WOLFSSL_SMALL_STACK
  22119. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22120. if (der == NULL) {
  22121. ERROR_OUT(-10130, exit);
  22122. }
  22123. #endif
  22124. /* Get cert private key */
  22125. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  22126. /* Get Cert Key 384 */
  22127. #ifdef USE_CERT_BUFFERS_256
  22128. XMEMCPY(der, ca_ecc_key_der_384, sizeof_ca_ecc_key_der_384);
  22129. bytes = sizeof_ca_ecc_key_der_384;
  22130. #else
  22131. file = XFOPEN(eccCaKey384File, "rb");
  22132. if (!file) {
  22133. ERROR_OUT(-10131, exit);
  22134. }
  22135. bytes = XFREAD(der, 1, FOURK_BUF, file);
  22136. XFCLOSE(file);
  22137. (void)eccCaKeyFile;
  22138. #endif /* USE_CERT_BUFFERS_256 */
  22139. #else
  22140. #ifdef USE_CERT_BUFFERS_256
  22141. XMEMCPY(der, ca_ecc_key_der_256, sizeof_ca_ecc_key_der_256);
  22142. bytes = sizeof_ca_ecc_key_der_256;
  22143. #else
  22144. file = XFOPEN(eccCaKeyFile, "rb");
  22145. if (!file) {
  22146. ERROR_OUT(-10132, exit);
  22147. }
  22148. bytes = XFREAD(der, 1, FOURK_BUF, file);
  22149. XFCLOSE(file);
  22150. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  22151. (void)eccCaKey384File;
  22152. #endif
  22153. #endif /* USE_CERT_BUFFERS_256 */
  22154. #endif /* ENABLE_ECC384_CERT_GEN_TEST */
  22155. /* Get CA Key */
  22156. ret = wc_ecc_init_ex(caEccKey, HEAP_HINT, devId);
  22157. if (ret != 0) {
  22158. ERROR_OUT(-10133, exit);
  22159. }
  22160. ret = wc_EccPrivateKeyDecode(der, &idx, caEccKey, (word32)bytes);
  22161. if (ret != 0) {
  22162. ERROR_OUT(-10134, exit);
  22163. }
  22164. /* Make a public key */
  22165. ret = wc_ecc_init_ex(certPubKey, HEAP_HINT, devId);
  22166. if (ret != 0) {
  22167. ERROR_OUT(-10135, exit);
  22168. }
  22169. ret = wc_ecc_make_key(rng, ECC_KEYGEN_SIZE, certPubKey);
  22170. #if defined(WOLFSSL_ASYNC_CRYPT)
  22171. ret = wc_AsyncWait(ret, &certPubKey->asyncDev, WC_ASYNC_FLAG_NONE);
  22172. #endif
  22173. if (ret != 0) {
  22174. ERROR_OUT(-10136, exit);
  22175. }
  22176. TEST_SLEEP();
  22177. /* Setup Certificate */
  22178. if (wc_InitCert_ex(myCert, HEAP_HINT, devId)) {
  22179. ERROR_OUT(-10137, exit);
  22180. }
  22181. #ifndef NO_SHA256
  22182. myCert->sigType = CTC_SHA256wECDSA;
  22183. #else
  22184. myCert->sigType = CTC_SHAwECDSA;
  22185. #endif
  22186. XMEMCPY(&myCert->subject, &certDefaultName, sizeof(CertName));
  22187. #ifdef WOLFSSL_CERT_EXT
  22188. /* add Policies */
  22189. XSTRNCPY(myCert->certPolicies[0], "2.4.589440.587.101.2.1.9632587.1",
  22190. CTC_MAX_CERTPOL_SZ);
  22191. XSTRNCPY(myCert->certPolicies[1], "1.2.13025.489.1.113549",
  22192. CTC_MAX_CERTPOL_SZ);
  22193. myCert->certPoliciesNb = 2;
  22194. /* add SKID from the Public Key */
  22195. if (wc_SetSubjectKeyIdFromPublicKey(myCert, NULL, certPubKey) != 0) {
  22196. ERROR_OUT(-10138, exit);
  22197. }
  22198. /* add AKID from the Public Key */
  22199. if (wc_SetAuthKeyIdFromPublicKey(myCert, NULL, caEccKey) != 0) {
  22200. ERROR_OUT(-10139, exit);
  22201. }
  22202. /* add Key Usage */
  22203. if (wc_SetKeyUsage(myCert, certKeyUsage) != 0) {
  22204. ERROR_OUT(-10140, exit);
  22205. }
  22206. #endif /* WOLFSSL_CERT_EXT */
  22207. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  22208. #if defined(USE_CERT_BUFFERS_256)
  22209. ret = wc_SetIssuerBuffer(myCert, ca_ecc_cert_der_384,
  22210. sizeof_ca_ecc_cert_der_384);
  22211. #else
  22212. ret = wc_SetIssuer(myCert, eccCaCert384File);
  22213. (void)eccCaCertFile;
  22214. #endif
  22215. #else
  22216. #if defined(USE_CERT_BUFFERS_256)
  22217. ret = wc_SetIssuerBuffer(myCert, ca_ecc_cert_der_256,
  22218. sizeof_ca_ecc_cert_der_256);
  22219. #else
  22220. ret = wc_SetIssuer(myCert, eccCaCertFile);
  22221. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  22222. (void)eccCaCert384File;
  22223. #endif
  22224. #endif
  22225. #endif /* ENABLE_ECC384_CERT_GEN_TEST */
  22226. if (ret < 0) {
  22227. ERROR_OUT(-10141, exit);
  22228. }
  22229. certSz = wc_MakeCert(myCert, der, FOURK_BUF, NULL, certPubKey, rng);
  22230. if (certSz < 0) {
  22231. ERROR_OUT(-10142, exit);
  22232. }
  22233. ret = 0;
  22234. do {
  22235. #if defined(WOLFSSL_ASYNC_CRYPT)
  22236. ret = wc_AsyncWait(ret, &caEccKey->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  22237. #endif
  22238. if (ret >= 0) {
  22239. ret = wc_SignCert(myCert->bodySz, myCert->sigType, der,
  22240. FOURK_BUF, NULL, caEccKey, rng);
  22241. }
  22242. } while (ret == WC_PENDING_E);
  22243. if (ret < 0) {
  22244. ERROR_OUT(-10143, exit);
  22245. }
  22246. certSz = ret;
  22247. TEST_SLEEP();
  22248. #ifdef WOLFSSL_TEST_CERT
  22249. InitDecodedCert(decode, der, certSz, HEAP_HINT);
  22250. ret = ParseCert(decode, CERT_TYPE, NO_VERIFY, 0);
  22251. if (ret != 0) {
  22252. FreeDecodedCert(decode);
  22253. ERROR_OUT(-10144, exit);
  22254. }
  22255. FreeDecodedCert(decode);
  22256. #endif
  22257. ret = SaveDerAndPem(der, certSz, certEccDerFile, certEccPemFile,
  22258. CERT_TYPE, -6735);
  22259. if (ret != 0) {
  22260. goto exit;
  22261. }
  22262. exit:
  22263. #ifdef WOLFSSL_SMALL_STACK
  22264. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22265. #endif
  22266. #ifdef WOLFSSL_SMALL_STACK
  22267. if (myCert != NULL)
  22268. XFREE(myCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22269. #ifdef WOLFSSL_TEST_CERT
  22270. if (decode != NULL)
  22271. XFREE(decode, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22272. #endif
  22273. if (caEccKey != NULL) {
  22274. wc_ecc_free(caEccKey);
  22275. XFREE(caEccKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22276. }
  22277. if (certPubKey != NULL) {
  22278. wc_ecc_free(certPubKey);
  22279. XFREE(certPubKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22280. }
  22281. #else
  22282. wc_ecc_free(certPubKey);
  22283. wc_ecc_free(caEccKey);
  22284. #endif
  22285. return ret;
  22286. }
  22287. #endif /* WOLFSSL_CERT_GEN */
  22288. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  22289. !defined(WOLFSSL_NO_MALLOC) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  22290. /* Test for the wc_ecc_key_new() and wc_ecc_key_free() functions. */
  22291. static int ecc_test_allocator(WC_RNG* rng)
  22292. {
  22293. int ret = 0;
  22294. ecc_key* key;
  22295. #ifdef WC_NO_RNG
  22296. word32 idx = 0;
  22297. #endif
  22298. key = wc_ecc_key_new(HEAP_HINT);
  22299. if (key == NULL) {
  22300. ERROR_OUT(-10150, exit);
  22301. }
  22302. #ifndef WC_NO_RNG
  22303. ret = wc_ecc_make_key(rng, ECC_KEYGEN_SIZE, key);
  22304. #if defined(WOLFSSL_ASYNC_CRYPT)
  22305. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  22306. #endif
  22307. if (ret != 0) {
  22308. ERROR_OUT(-10151, exit);
  22309. }
  22310. #else
  22311. /* use test ECC key */
  22312. ret = wc_EccPrivateKeyDecode(ecc_key_der_256, &idx, key,
  22313. (word32)sizeof_ecc_key_der_256);
  22314. (void)rng;
  22315. #endif
  22316. exit:
  22317. wc_ecc_key_free(key);
  22318. return ret;
  22319. }
  22320. #endif
  22321. /* ECC Non-blocking tests for Sign and Verify */
  22322. /* Requires SP math and supports P384 or P256 */
  22323. /* ./configure --enable-ecc=nonblock --enable-sp=yes,nonblock CFLAGS="-DWOLFSSL_PUBLIC_MP" */
  22324. #if defined(WC_ECC_NONBLOCK) && defined(WOLFSSL_HAVE_SP_ECC) && \
  22325. defined(WOLFSSL_PUBLIC_MP)
  22326. /* ECC Private Key "d" */
  22327. static const byte p256PrivKey[] = {
  22328. /* SECP256R1 */
  22329. /* d */
  22330. 0x1e, 0xe7, 0x70, 0x07, 0xd3, 0x30, 0x94, 0x39,
  22331. 0x28, 0x90, 0xdf, 0x23, 0x88, 0x2c, 0x4a, 0x34,
  22332. 0x15, 0xdb, 0x4c, 0x43, 0xcd, 0xfa, 0xe5, 0x1f,
  22333. 0x3d, 0x4c, 0x37, 0xfe, 0x59, 0x3b, 0x96, 0xd8
  22334. };
  22335. #ifdef HAVE_ECC384
  22336. static const byte p384PrivKey[] = {
  22337. /* SECP384R1 */
  22338. /* d */
  22339. 0xa4, 0xe5, 0x06, 0xe8, 0x06, 0x16, 0x3e, 0xab,
  22340. 0x89, 0xf8, 0x60, 0x43, 0xc0, 0x60, 0x25, 0xdb,
  22341. 0xba, 0x7b, 0xfe, 0x19, 0x35, 0x08, 0x55, 0x65,
  22342. 0x76, 0xe2, 0xdc, 0xe0, 0x01, 0x8b, 0x6b, 0x68,
  22343. 0xdf, 0xcf, 0x6f, 0x80, 0x12, 0xce, 0x79, 0x37,
  22344. 0xeb, 0x2b, 0x9c, 0x7b, 0xc4, 0x68, 0x1c, 0x74
  22345. };
  22346. #endif /* HAVE_ECC384 */
  22347. #ifdef HAVE_ECC521
  22348. static const byte p521PrivKey[] = {
  22349. /* SECP521R1 */
  22350. /* d */
  22351. 0x01, 0x68, 0x91, 0x33, 0x53, 0xe2, 0x90, 0x68,
  22352. 0x11, 0x8f, 0xaa, 0xa8, 0x76, 0x0c, 0xf7, 0x2a,
  22353. 0x07, 0x1b, 0x92, 0x2a, 0xa7, 0x82, 0x3d, 0xfa,
  22354. 0x83, 0xce, 0x70, 0xc8, 0xc2, 0x60, 0x82, 0xfe,
  22355. 0x18, 0x88, 0x68, 0xda, 0x6a, 0x83, 0x46, 0x78,
  22356. 0xe4, 0xe9, 0xe9, 0xcc, 0x51, 0x7f, 0xed, 0x81,
  22357. 0x02, 0x32, 0xee, 0x26, 0x87, 0xcc, 0xed, 0x63,
  22358. 0x3f, 0x39, 0x27, 0xf0, 0xd7, 0x17, 0x77, 0xa1,
  22359. 0xa4, 0x36
  22360. };
  22361. #endif /* HAVE_ECC521 */
  22362. /* ECC public key Qx/Qy */
  22363. static const byte p256PubKey[] = {
  22364. /* SECP256R1 */
  22365. /* Qx */
  22366. 0x96, 0x93, 0x1c, 0x53, 0x0b, 0x43, 0x6c, 0x42,
  22367. 0x0c, 0x52, 0x90, 0xe4, 0xa7, 0xec, 0x98, 0xb1,
  22368. 0xaf, 0xd4, 0x14, 0x49, 0xd8, 0xc1, 0x42, 0x82,
  22369. 0x04, 0x78, 0xd1, 0x90, 0xae, 0xa0, 0x6c, 0x07,
  22370. /* Qy */
  22371. 0xf2, 0x3a, 0xb5, 0x10, 0x32, 0x8d, 0xce, 0x9e,
  22372. 0x76, 0xa0, 0xd2, 0x8c, 0xf3, 0xfc, 0xa9, 0x94,
  22373. 0x43, 0x24, 0xe6, 0x82, 0x00, 0x40, 0xc6, 0xdb,
  22374. 0x1c, 0x2f, 0xcd, 0x38, 0x4b, 0x60, 0xdd, 0x61
  22375. };
  22376. #ifdef HAVE_ECC384
  22377. static const byte p384PubKey[] = {
  22378. /* SECP384R1 */
  22379. /* Qx */
  22380. 0xea, 0xcf, 0x93, 0x4f, 0x2c, 0x09, 0xbb, 0x39,
  22381. 0x14, 0x0f, 0x56, 0x64, 0xc3, 0x40, 0xb4, 0xdf,
  22382. 0x0e, 0x63, 0xae, 0xe5, 0x71, 0x4b, 0x00, 0xcc,
  22383. 0x04, 0x97, 0xff, 0xe1, 0xe9, 0x38, 0x96, 0xbb,
  22384. 0x5f, 0x91, 0xb2, 0x6a, 0xcc, 0xb5, 0x39, 0x5f,
  22385. 0x8f, 0x70, 0x59, 0xf1, 0x01, 0xf6, 0x5a, 0x2b,
  22386. /* Qy */
  22387. 0x01, 0x6c, 0x68, 0x0b, 0xcf, 0x55, 0x25, 0xaf,
  22388. 0x6d, 0x98, 0x48, 0x0a, 0xa8, 0x74, 0xc9, 0xa9,
  22389. 0x17, 0xa0, 0x0c, 0xc3, 0xfb, 0xd3, 0x23, 0x68,
  22390. 0xfe, 0x04, 0x3c, 0x63, 0x50, 0x88, 0x3b, 0xb9,
  22391. 0x4f, 0x7c, 0x67, 0x34, 0xf7, 0x3b, 0xa9, 0x73,
  22392. 0xe7, 0x1b, 0xc3, 0x51, 0x5e, 0x22, 0x18, 0xec
  22393. };
  22394. #endif
  22395. #ifdef HAVE_ECC521
  22396. static const byte p521PubKey[] = {
  22397. /* SECP521R1 */
  22398. /* Qx */
  22399. 0x01, 0x62, 0x6e, 0xf1, 0x00, 0xec, 0xd8, 0x99,
  22400. 0x58, 0x9b, 0x80, 0x6b, 0xfe, 0x2c, 0xf1, 0xb2,
  22401. 0xf0, 0xc8, 0x48, 0xdf, 0xac, 0xd2, 0x3b, 0x71,
  22402. 0x29, 0xab, 0xf0, 0x66, 0x63, 0xd8, 0x8e, 0xb5,
  22403. 0xc8, 0xc2, 0xfc, 0x99, 0x44, 0xe2, 0x45, 0xb1,
  22404. 0x5a, 0x7b, 0xb9, 0x73, 0x01, 0xda, 0x79, 0xec,
  22405. 0x9c, 0x26, 0x27, 0x34, 0x45, 0x26, 0xd5, 0x89,
  22406. 0x4b, 0x44, 0xfe, 0x69, 0x4e, 0x72, 0x14, 0xe3,
  22407. 0x8b, 0xbc,
  22408. /* Qy */
  22409. 0x00, 0x0f, 0x09, 0xa2, 0x03, 0xc3, 0x5a, 0xdc,
  22410. 0x95, 0x82, 0xf6, 0xf9, 0xf6, 0x9c, 0xff, 0xb5,
  22411. 0x6b, 0x75, 0x95, 0x4b, 0xa4, 0x28, 0x5d, 0x9e,
  22412. 0x90, 0x04, 0xd1, 0xc0, 0x1e, 0xd5, 0xfd, 0x43,
  22413. 0x9e, 0x1e, 0x83, 0xc0, 0x11, 0x2b, 0x2b, 0x07,
  22414. 0x6d, 0xa9, 0x7a, 0x10, 0xd7, 0x67, 0xe7, 0x51,
  22415. 0x37, 0x24, 0xd8, 0xbf, 0x03, 0x0d, 0x8b, 0xb5,
  22416. 0x40, 0x5c, 0x4f, 0xd6, 0x13, 0x73, 0x42, 0xbc,
  22417. 0x91, 0xd9
  22418. };
  22419. /* perform verify of signature and hash using public key */
  22420. /* key is public Qx + public Qy */
  22421. /* sig is r + s */
  22422. static int crypto_ecc_verify(const uint8_t *key, uint32_t keySz,
  22423. const uint8_t *hash, uint32_t hashSz, const uint8_t *sig, uint32_t sigSz,
  22424. uint32_t curveSz, int curveId)
  22425. {
  22426. int ret, verify_res = 0, count = 0;
  22427. mp_int r, s;
  22428. ecc_key ecc;
  22429. ecc_nb_ctx_t nb_ctx;
  22430. /* validate arguments */
  22431. if (key == NULL || hash == NULL || sig == NULL || curveSz == 0 ||
  22432. hashSz == 0 || keySz < (curveSz*2) || sigSz < (curveSz*2))
  22433. {
  22434. return BAD_FUNC_ARG;
  22435. }
  22436. /* Setup the ECC key */
  22437. ret = wc_ecc_init(&ecc);
  22438. if (ret < 0) {
  22439. return ret;
  22440. }
  22441. ret = wc_ecc_set_nonblock(&ecc, &nb_ctx);
  22442. if (ret != MP_OKAY) {
  22443. wc_ecc_free(&ecc);
  22444. return ret;
  22445. }
  22446. /* Setup the signature r/s variables */
  22447. ret = mp_init(&r);
  22448. if (ret != MP_OKAY) {
  22449. wc_ecc_free(&ecc);
  22450. return ret;
  22451. }
  22452. ret = mp_init(&s);
  22453. if (ret != MP_OKAY) {
  22454. mp_clear(&r);
  22455. wc_ecc_free(&ecc);
  22456. return ret;
  22457. }
  22458. /* Import public key x/y */
  22459. ret = wc_ecc_import_unsigned(
  22460. &ecc,
  22461. (byte*)key, /* Public "x" Coordinate */
  22462. (byte*)(key + curveSz), /* Public "y" Coordinate */
  22463. NULL, /* Private "d" (optional) */
  22464. curveId /* ECC Curve Id */
  22465. );
  22466. /* Make sure it was a public key imported */
  22467. if (ret == 0 && ecc.type != ECC_PUBLICKEY) {
  22468. ret = ECC_BAD_ARG_E;
  22469. }
  22470. /* Import signature r/s */
  22471. if (ret == 0) {
  22472. ret = mp_read_unsigned_bin(&r, sig, curveSz);
  22473. }
  22474. if (ret == 0) {
  22475. ret = mp_read_unsigned_bin(&s, sig + curveSz, curveSz);
  22476. }
  22477. /* Verify ECC Signature */
  22478. if (ret == 0) {
  22479. do {
  22480. ret = wc_ecc_verify_hash_ex(
  22481. &r, &s, /* r/s as mp_int */
  22482. hash, hashSz, /* computed hash digest */
  22483. &verify_res, /* verification result 1=success */
  22484. &ecc
  22485. );
  22486. count++;
  22487. /* This is where real-time work could be called */
  22488. } while (ret == FP_WOULDBLOCK);
  22489. #ifdef DEBUG_WOLFSSL
  22490. printf("ECC non-block verify: %d times\n", count);
  22491. #endif
  22492. }
  22493. /* check verify result */
  22494. if (ret == 0 && verify_res == 0) {
  22495. ret = SIG_VERIFY_E;
  22496. }
  22497. mp_clear(&r);
  22498. mp_clear(&s);
  22499. wc_ecc_free(&ecc);
  22500. (void)count;
  22501. return ret;
  22502. }
  22503. /* perform signature operation against hash using private key */
  22504. static int crypto_ecc_sign(const uint8_t *key, uint32_t keySz,
  22505. const uint8_t *hash, uint32_t hashSz, uint8_t *sig, uint32_t* sigSz,
  22506. uint32_t curveSz, int curveId, WC_RNG* rng)
  22507. {
  22508. int ret, count = 0;
  22509. mp_int r, s;
  22510. ecc_key ecc;
  22511. ecc_nb_ctx_t nb_ctx;
  22512. /* validate arguments */
  22513. if (key == NULL || hash == NULL || sig == NULL || sigSz == NULL ||
  22514. curveSz == 0 || hashSz == 0 || keySz < curveSz || *sigSz < (curveSz*2))
  22515. {
  22516. return BAD_FUNC_ARG;
  22517. }
  22518. /* Initialize signature result */
  22519. memset(sig, 0, curveSz*2);
  22520. /* Setup the ECC key */
  22521. ret = wc_ecc_init(&ecc);
  22522. if (ret < 0) {
  22523. return ret;
  22524. }
  22525. ret = wc_ecc_set_nonblock(&ecc, &nb_ctx);
  22526. if (ret != MP_OKAY) {
  22527. wc_ecc_free(&ecc);
  22528. return ret;
  22529. }
  22530. /* Setup the signature r/s variables */
  22531. ret = mp_init(&r);
  22532. if (ret != MP_OKAY) {
  22533. wc_ecc_free(&ecc);
  22534. return ret;
  22535. }
  22536. ret = mp_init(&s);
  22537. if (ret != MP_OKAY) {
  22538. mp_clear(&r);
  22539. wc_ecc_free(&ecc);
  22540. return ret;
  22541. }
  22542. /* Import private key "k" */
  22543. ret = wc_ecc_import_private_key_ex(
  22544. key, keySz, /* private key "d" */
  22545. NULL, 0, /* public (optional) */
  22546. &ecc,
  22547. curveId /* ECC Curve Id */
  22548. );
  22549. if (ret == 0) {
  22550. do {
  22551. /* Verify ECC Signature */
  22552. ret = wc_ecc_sign_hash_ex(
  22553. hash, hashSz, /* computed hash digest */
  22554. rng, &ecc, /* random and key context */
  22555. &r, &s /* r/s as mp_int */
  22556. );
  22557. count++;
  22558. /* This is where real-time work could be called */
  22559. } while (ret == FP_WOULDBLOCK);
  22560. #ifdef DEBUG_WOLFSSL
  22561. printf("ECC non-block sign: %d times\n", count);
  22562. #endif
  22563. }
  22564. if (ret == 0) {
  22565. /* export r/s */
  22566. mp_to_unsigned_bin_len(&r, sig, curveSz);
  22567. mp_to_unsigned_bin_len(&s, sig + curveSz, curveSz);
  22568. }
  22569. mp_clear(&r);
  22570. mp_clear(&s);
  22571. wc_ecc_free(&ecc);
  22572. (void)count;
  22573. return ret;
  22574. }
  22575. #endif /* HAVE_ECC_SIGN && HAVE_ECC_VERIFY */
  22576. /*
  22577. * This test doesn't work with WOLFSSL_VALIDATE_ECC_KEYGEN defined because we
  22578. * don't have non-blocking versions of the key checking functions, yet.
  22579. */
  22580. #if defined(HAVE_ECC_DHE) && !defined(WOLFSSL_VALIDATE_ECC_KEYGEN)
  22581. static int ecc_test_nonblock_dhe(int curveId, word32 curveSz,
  22582. const byte* privKey, const byte* pubKey, WC_RNG* rng)
  22583. {
  22584. int ret;
  22585. ecc_key keyA;
  22586. ecc_key keyB;
  22587. ecc_nb_ctx_t nbCtxA;
  22588. ecc_nb_ctx_t nbCtxB;
  22589. byte secretA[ECC_SHARED_SIZE];
  22590. byte secretB[ECC_SHARED_SIZE];
  22591. word32 secretSzA = ECC_SHARED_SIZE;
  22592. word32 secretSzB = ECC_SHARED_SIZE;
  22593. int count = 0;
  22594. ret = wc_ecc_init(&keyA);
  22595. if (ret == 0) {
  22596. ret = wc_ecc_init(&keyB);
  22597. }
  22598. if (ret == 0) {
  22599. ret = wc_ecc_set_nonblock(&keyA, &nbCtxA);
  22600. }
  22601. if (ret == 0) {
  22602. ret = wc_ecc_set_nonblock(&keyB, &nbCtxB);
  22603. }
  22604. if (ret == 0) {
  22605. do {
  22606. ret = wc_ecc_make_key_ex(rng, curveSz, &keyA, curveId);
  22607. count++;
  22608. } while (ret == FP_WOULDBLOCK);
  22609. }
  22610. #ifdef DEBUG_WOLFSSL
  22611. fprintf(stderr, "ECC non-block key gen: %d times\n", count);
  22612. #endif
  22613. if (ret == 0) {
  22614. ret = wc_ecc_check_key(&keyA);
  22615. }
  22616. if (ret == 0) {
  22617. ret = wc_ecc_import_unsigned(&keyB, pubKey, pubKey + curveSz,
  22618. privKey, curveId);
  22619. }
  22620. count = 0;
  22621. if (ret == 0) {
  22622. do {
  22623. ret = wc_ecc_shared_secret(&keyA, &keyB, secretA, &secretSzA);
  22624. count++;
  22625. } while (ret == FP_WOULDBLOCK);
  22626. }
  22627. #ifdef DEBUG_WOLFSSL
  22628. fprintf(stderr, "ECC non-block shared secret: %d times\n", count);
  22629. #endif
  22630. if (ret == 0) {
  22631. do {
  22632. ret = wc_ecc_shared_secret(&keyB, &keyA, secretB, &secretSzB);
  22633. } while (ret == FP_WOULDBLOCK);
  22634. }
  22635. if (ret == 0) {
  22636. if (secretSzA != secretSzB ||
  22637. XMEMCMP(secretA, secretB, secretSzA) != 0) {
  22638. ret = -1;
  22639. }
  22640. }
  22641. wc_ecc_free(&keyA);
  22642. wc_ecc_free(&keyB);
  22643. return ret;
  22644. }
  22645. #endif /* HAVE_ECC_DHE && !WOLFSSL_VALIDATE_ECC_KEYGEN */
  22646. #if defined(HAVE_ECC_SIGN) && defined(HAVE_ECC_VERIFY)
  22647. static int ecc_test_nonblock_ecdsa(int curveId, word32 curveSz,
  22648. const byte* privKey, word32 privKeySz, const byte* pubKey, word32 pubKeySz,
  22649. WC_RNG* rng)
  22650. {
  22651. int ret = 0;
  22652. byte* sig = NULL;
  22653. word32 sigSz = curveSz * 2;
  22654. static const byte hash[] = {
  22655. 0x8d, 0x28, 0xa3, 0x8b, 0x0b, 0xa9, 0xfe, 0xd4, 0x0e, 0x54, 0xc4, 0x17,
  22656. 0x3d, 0x54, 0x66, 0x34, 0xbf, 0x5d, 0x6f, 0x46, 0xc2, 0x20, 0xcb, 0xc3,
  22657. 0x22, 0xe9, 0xb0, 0xdf, 0xe7, 0x64, 0x3f, 0xd9
  22658. };
  22659. sig = (byte*)XMALLOC(sigSz, HEAP_HINT, DYNAMIC_TYPE_SIGNATURE);
  22660. if (sig == NULL) {
  22661. ret = -1;
  22662. }
  22663. if (ret == 0) {
  22664. /* Sign hash using private key */
  22665. /* Note: result of an ECC sign varies for each call even with same
  22666. private key and hash. This is because a new random public key is
  22667. used for each operation. */
  22668. ret = crypto_ecc_sign(privKey, privKeySz, hash, sizeof(hash), sig,
  22669. &sigSz, curveSz, curveId, rng);
  22670. }
  22671. if (ret == 0) {
  22672. /* Verify generated signature is valid */
  22673. ret = crypto_ecc_verify(pubKey, pubKeySz, hash, sizeof(hash), sig,
  22674. sigSz, curveSz, curveId);
  22675. }
  22676. if (sig != NULL) {
  22677. XFREE(sig, HEAP_HINT, DYNAMIC_TYPE_SIGNATURE);
  22678. }
  22679. return ret;
  22680. }
  22681. #endif /* HAVE_ECC_SIGN && HAVE_ECC_VERIFY */
  22682. static int ecc_test_nonblock(WC_RNG* rng)
  22683. {
  22684. int ret = 0;
  22685. word32 i;
  22686. int curveIds[3] = {0, 0, 0};
  22687. word32 curveSzs[3] = {0, 0, 0};
  22688. const byte* privKeys[3] = {NULL, NULL, NULL};
  22689. word32 privKeySzs[3] = {0, 0, 0};
  22690. const byte* pubKeys[3] = {NULL, NULL, NULL};
  22691. word32 pubKeySzs[3] = {0, 0, 0};
  22692. curveIds[0] = ECC_SECP256R1;
  22693. curveSzs[0] = 32;
  22694. privKeys[0] = p256PrivKey;
  22695. privKeySzs[0] = sizeof(p256PrivKey);
  22696. pubKeys[0] = p256PubKey;
  22697. pubKeySzs[0] = sizeof(p256PubKey);
  22698. #ifdef HAVE_ECC384
  22699. curveIds[1] = ECC_SECP384R1;
  22700. curveSzs[1] = 48;
  22701. privKeys[1] = p384PrivKey;
  22702. privKeySzs[1] = sizeof(p384PrivKey);
  22703. pubKeys[1] = p384PubKey;
  22704. pubKeySzs[1] = sizeof(p384PubKey);
  22705. #endif
  22706. #ifdef HAVE_ECC521
  22707. curveIds[2] = ECC_SECP521R1;
  22708. curveSzs[2] = 66;
  22709. privKeys[2] = p521PrivKey;
  22710. privKeySzs[2] = sizeof(p521PrivKey);
  22711. pubKeys[2] = p521PubKey;
  22712. pubKeySzs[2] = sizeof(p521PubKey);
  22713. #endif
  22714. for (i = 0; ret == 0 && i < sizeof(curveIds) / sizeof(curveIds[0]); ++i) {
  22715. if (curveIds[i] == 0) {
  22716. continue;
  22717. }
  22718. #if defined(HAVE_ECC_SIGN) && defined(HAVE_ECC_VERIFY)
  22719. ret = ecc_test_nonblock_ecdsa(curveIds[i], curveSzs[i], privKeys[i],
  22720. privKeySzs[i], pubKeys[i], pubKeySzs[i], rng);
  22721. #endif /* HAVE_ECC_SIGN && HAVE_ECC_VERIFY */
  22722. #if defined(HAVE_ECC_DHE) && !defined(WOLFSSL_VALIDATE_ECC_KEYGEN)
  22723. if (ret == 0) {
  22724. ret = ecc_test_nonblock_dhe(curveIds[i], curveSzs[i], privKeys[i],
  22725. pubKeys[i], rng);
  22726. }
  22727. #endif /* HAVE_ECC_DHE && !WOLFSSL_VALIDATE_ECC_KEYGEN */
  22728. }
  22729. return ret;
  22730. }
  22731. #endif /* WC_ECC_NONBLOCK && WOLFSSL_HAVE_SP_ECC && WOLFSSL_PUBLIC_MP */
  22732. WOLFSSL_TEST_SUBROUTINE int ecc_test(void)
  22733. {
  22734. int ret;
  22735. WC_RNG rng;
  22736. #if defined(WOLFSSL_CERT_EXT) && \
  22737. (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  22738. ret = ecc_decode_test();
  22739. if (ret < 0)
  22740. return ret;
  22741. #endif
  22742. #ifndef HAVE_FIPS
  22743. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  22744. #else
  22745. ret = wc_InitRng(&rng);
  22746. #endif
  22747. #ifndef WC_NO_RNG
  22748. if (ret != 0)
  22749. return -10300;
  22750. #else
  22751. (void)ret;
  22752. #endif
  22753. #if (defined(HAVE_ECC112) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 112
  22754. ret = ecc_test_curve(&rng, 14);
  22755. if (ret < 0) {
  22756. goto done;
  22757. }
  22758. #endif /* HAVE_ECC112 */
  22759. #if (defined(HAVE_ECC128) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 128
  22760. ret = ecc_test_curve(&rng, 16);
  22761. if (ret < 0) {
  22762. goto done;
  22763. }
  22764. #endif /* HAVE_ECC128 */
  22765. #if (defined(HAVE_ECC160) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 160
  22766. ret = ecc_test_curve(&rng, 20);
  22767. if (ret < 0) {
  22768. goto done;
  22769. }
  22770. #endif /* HAVE_ECC160 */
  22771. #if (defined(HAVE_ECC192) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 192
  22772. ret = ecc_test_curve(&rng, 24);
  22773. if (ret < 0) {
  22774. goto done;
  22775. }
  22776. #endif /* HAVE_ECC192 */
  22777. #if (defined(HAVE_ECC224) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 224
  22778. ret = ecc_test_curve(&rng, 28);
  22779. if (ret < 0) {
  22780. goto done;
  22781. }
  22782. #endif /* HAVE_ECC224 */
  22783. #if (defined(HAVE_ECC239) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 239
  22784. ret = ecc_test_curve(&rng, 30);
  22785. if (ret < 0) {
  22786. goto done;
  22787. }
  22788. #endif /* HAVE_ECC239 */
  22789. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  22790. ret = ecc_test_curve(&rng, 32);
  22791. if (ret < 0) {
  22792. goto done;
  22793. }
  22794. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  22795. defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT) && \
  22796. !defined(WOLFSSL_NO_MALLOC) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  22797. ret = ecc_point_test();
  22798. if (ret < 0) {
  22799. goto done;
  22800. }
  22801. #endif
  22802. ret = ecc_def_curve_test(&rng);
  22803. if (ret < 0) {
  22804. goto done;
  22805. }
  22806. #endif /* !NO_ECC256 */
  22807. #if (defined(HAVE_ECC320) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 320
  22808. ret = ecc_test_curve(&rng, 40);
  22809. if (ret < 0) {
  22810. goto done;
  22811. }
  22812. #endif /* HAVE_ECC320 */
  22813. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 384
  22814. ret = ecc_test_curve(&rng, 48);
  22815. if (ret < 0) {
  22816. goto done;
  22817. }
  22818. #endif /* HAVE_ECC384 */
  22819. #if (defined(HAVE_ECC512) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 512
  22820. ret = ecc_test_curve(&rng, 64);
  22821. if (ret < 0) {
  22822. goto done;
  22823. }
  22824. #endif /* HAVE_ECC512 */
  22825. #if (defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 521
  22826. ret = ecc_test_curve(&rng, 66);
  22827. if (ret < 0) {
  22828. goto done;
  22829. }
  22830. #endif /* HAVE_ECC521 */
  22831. #if defined(WOLFSSL_CUSTOM_CURVES)
  22832. ret = ecc_test_custom_curves(&rng);
  22833. if (ret != 0) {
  22834. goto done;
  22835. }
  22836. #endif
  22837. #if defined(HAVE_ECC_SIGN) && defined(WOLFSSL_ECDSA_DETERMINISTIC_K)
  22838. ret = ecc_test_deterministic_k(&rng);
  22839. if (ret != 0) {
  22840. printf("ecc_test_deterministic_k failed! %d\n", ret);
  22841. goto done;
  22842. }
  22843. #ifdef WOLFSSL_PUBLIC_MP
  22844. #if defined(HAVE_ECC384)
  22845. ret = ecc384_test_deterministic_k(&rng);
  22846. if (ret != 0) {
  22847. printf("ecc384_test_deterministic_k failed! %d\n", ret);
  22848. goto done;
  22849. }
  22850. #endif
  22851. #if defined(HAVE_ECC521)
  22852. ret = ecc521_test_deterministic_k(&rng);
  22853. if (ret != 0) {
  22854. printf("ecc512_test_deterministic_k failed! %d\n", ret);
  22855. goto done;
  22856. }
  22857. #endif
  22858. #endif
  22859. #endif
  22860. #if defined(HAVE_ECC_SIGN) && defined(WOLFSSL_ECDSA_SET_K) && \
  22861. !defined(WOLFSSL_KCAPI_ECC)
  22862. ret = ecc_test_sign_vectors(&rng);
  22863. if (ret != 0) {
  22864. printf("ecc_test_sign_vectors failed! %d\n", ret);
  22865. goto done;
  22866. }
  22867. #endif
  22868. #ifdef HAVE_ECC_CDH
  22869. ret = ecc_test_cdh_vectors(&rng);
  22870. if (ret != 0) {
  22871. printf("ecc_test_cdh_vectors failed! %d\n", ret);
  22872. goto done;
  22873. }
  22874. #endif
  22875. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  22876. !defined(WOLFSSL_STM32_PKA) && !defined(WOLFSSL_SILABS_SE_ACCEL) && \
  22877. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  22878. ret = ecc_test_make_pub(&rng);
  22879. if (ret != 0) {
  22880. printf("ecc_test_make_pub failed!: %d\n", ret);
  22881. goto done;
  22882. }
  22883. #elif defined(HAVE_ECC_KEY_IMPORT)
  22884. (void)ecc_test_make_pub; /* for compiler warning */
  22885. #endif
  22886. #ifdef WOLFSSL_CERT_GEN
  22887. ret = ecc_test_cert_gen(&rng);
  22888. if (ret != 0) {
  22889. printf("ecc_test_cert_gen failed!: %d\n", ret);
  22890. goto done;
  22891. }
  22892. #endif
  22893. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && !defined(WOLFSSL_NO_MALLOC) && \
  22894. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  22895. ret = ecc_test_allocator(&rng);
  22896. if (ret != 0) {
  22897. printf("ecc_test_allocator failed!: %d\n", ret);
  22898. goto done;
  22899. }
  22900. #endif
  22901. #if defined(WC_ECC_NONBLOCK) && defined(WOLFSSL_PUBLIC_MP) && \
  22902. defined(HAVE_ECC_SIGN) && defined(HAVE_ECC_VERIFY)
  22903. ret = ecc_test_nonblock(&rng);
  22904. if (ret != 0) {
  22905. printf("ecc_test_nonblock failed!: %d\n", ret);
  22906. goto done;
  22907. }
  22908. #endif
  22909. done:
  22910. wc_FreeRng(&rng);
  22911. return ret;
  22912. }
  22913. #if defined(HAVE_ECC_ENCRYPT) && defined(HAVE_AES_CBC) && \
  22914. (defined(WOLFSSL_AES_128) || defined(WOLFSSL_AES_256))
  22915. /* ecc_encrypt_e2e_test() uses wc_ecc_ctx_set_algo(), which was added in
  22916. * wolfFIPS 5.3.
  22917. * ecc_encrypt_kat() is used only by ecc_encrypt_e2e_test().
  22918. */
  22919. #if !defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3))
  22920. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && \
  22921. ECC_MIN_KEY_SZ <= 256 && defined(WOLFSSL_AES_128)
  22922. static int ecc_encrypt_kat(WC_RNG *rng)
  22923. {
  22924. int ret = 0;
  22925. #ifdef WOLFSSL_ECIES_OLD
  22926. #ifdef WOLFSSL_SMALL_STACK
  22927. ecc_key* userA = NULL;
  22928. #else
  22929. ecc_key userA[1];
  22930. #endif
  22931. int userAInit = 0;
  22932. #endif
  22933. #ifdef WOLFSSL_SMALL_STACK
  22934. ecc_key* userB = NULL;
  22935. #else
  22936. ecc_key userB[1];
  22937. #endif
  22938. int userBInit = 0;
  22939. ecc_key* tmpKey;
  22940. byte plain[48];
  22941. word32 plainSz = sizeof(plain);
  22942. WOLFSSL_SMALL_STACK_STATIC const byte privKey[] = {
  22943. 0x04, 0x80, 0xef, 0x1d, 0xbe, 0x02, 0x0c, 0x20,
  22944. 0x5b, 0xab, 0x80, 0x35, 0x5b, 0x2a, 0x0f, 0x6d,
  22945. 0xd3, 0xb0, 0x7f, 0x7e, 0x7f, 0x86, 0x8a, 0x49,
  22946. 0xee, 0xb4, 0xaa, 0x09, 0x2d, 0x1e, 0x1d, 0x02
  22947. };
  22948. #if defined(WOLFSSL_ECIES_OLD) || defined(WOLFSSL_QNX_CAAM)
  22949. WOLFSSL_SMALL_STACK_STATIC const byte pubKey[] = {
  22950. 0x04,
  22951. /* X */
  22952. 0x50, 0xf2, 0x93, 0xa2, 0x48, 0xa9, 0xc0, 0x5a,
  22953. 0x9a, 0xa7, 0x70, 0x34, 0xb7, 0x7f, 0x4c, 0x3a,
  22954. 0xad, 0xfc, 0xd8, 0xb6, 0x76, 0x0a, 0xe3, 0xc1,
  22955. 0x87, 0x17, 0x07, 0x2d, 0x8d, 0xa3, 0x63, 0xa0,
  22956. /* X */
  22957. 0xc1, 0x27, 0xb2, 0x97, 0x9b, 0x84, 0xe7, 0xcd,
  22958. 0x20, 0x65, 0x8d, 0x2b, 0x6a, 0x93, 0x75, 0xaa,
  22959. 0x8b, 0xe1, 0x3a, 0x7b, 0x24, 0x1a, 0xbe, 0xe8,
  22960. 0x36, 0xd2, 0xe6, 0x34, 0x8a, 0x7a, 0xb3, 0x28
  22961. };
  22962. #endif
  22963. WOLFSSL_SMALL_STACK_STATIC const byte enc_msg[] = {
  22964. #ifdef WOLFSSL_ECIES_OLD
  22965. 0x42, 0x70, 0xbf, 0xf9, 0xf4, 0x7e, 0x4b, 0x9b,
  22966. 0xb5, 0x4c, 0xcc, 0xc5, 0x94, 0xa7, 0xef, 0xaa,
  22967. 0xc3, 0x7c, 0x85, 0xa6, 0x51, 0x6e, 0xd3, 0xfa,
  22968. 0x56, 0xc9, 0x10, 0x4d, 0x14, 0x32, 0x61, 0xb8,
  22969. 0xbb, 0x66, 0x7a, 0xb5, 0xbc, 0x95, 0xf8, 0xca,
  22970. 0xd1, 0x2a, 0x19, 0x51, 0x44, 0xd8, 0x0e, 0x57,
  22971. 0x34, 0xed, 0x45, 0x89, 0x2e, 0x57, 0xbe, 0xd5,
  22972. 0x06, 0x22, 0xd7, 0x13, 0x0a, 0x0e, 0x40, 0x36,
  22973. 0x0d, 0x05, 0x0d, 0xb6, 0xae, 0x61, 0x37, 0x18,
  22974. 0x83, 0x90, 0x0a, 0x27, 0x95, 0x41, 0x8c, 0x45
  22975. #elif defined(WOLFSSL_ECIES_ISO18033)
  22976. 0x04, 0x50, 0xf2, 0x93, 0xa2, 0x48, 0xa9, 0xc0,
  22977. 0x5a, 0x9a, 0xa7, 0x70, 0x34, 0xb7, 0x7f, 0x4c,
  22978. 0x3a, 0xad, 0xfc, 0xd8, 0xb6, 0x76, 0x0a, 0xe3,
  22979. 0xc1, 0x87, 0x17, 0x07, 0x2d, 0x8d, 0xa3, 0x63,
  22980. 0xa0, 0xc1, 0x27, 0xb2, 0x97, 0x9b, 0x84, 0xe7,
  22981. 0xcd, 0x20, 0x65, 0x8d, 0x2b, 0x6a, 0x93, 0x75,
  22982. 0xaa, 0x8b, 0xe1, 0x3a, 0x7b, 0x24, 0x1a, 0xbe,
  22983. 0xe8, 0x36, 0xd2, 0xe6, 0x34, 0x8a, 0x7a, 0xb3,
  22984. 0x28, 0xbb, 0x9f, 0xa8, 0x2d, 0xe1, 0xf1, 0x67,
  22985. 0x45, 0x02, 0x19, 0xdc, 0xc8, 0x24, 0x8b, 0x20,
  22986. 0x02, 0xa0, 0x8f, 0x95, 0x12, 0x55, 0x51, 0xf8,
  22987. 0x03, 0xc4, 0x54, 0x13, 0x98, 0x2d, 0xf0, 0x31,
  22988. 0x51, 0x80, 0x45, 0x24, 0xcb, 0x8b, 0x48, 0xa6,
  22989. 0x8b, 0x8e, 0x97, 0x9c, 0x56, 0x4d, 0x70, 0x00,
  22990. 0x53, 0xd3, 0x47, 0x00, 0x5a, 0x23, 0x8c, 0xf9,
  22991. 0xfd, 0xd2, 0x33, 0x2c, 0x43, 0x6e, 0x9e, 0xb2,
  22992. 0xf4, 0x95, 0xd4, 0xcf, 0x30, 0xd6, 0xa2, 0xc5,
  22993. 0x35, 0x96, 0x6a, 0xd4, 0x36, 0x15, 0xa9, 0xbd,
  22994. 0x7f
  22995. #elif defined(WOLFSSL_ECIES_GEN_IV)
  22996. /* EC P-256 point */
  22997. 0x04,
  22998. /* X */
  22999. 0x50, 0xf2, 0x93, 0xa2, 0x48, 0xa9, 0xc0, 0x5a,
  23000. 0x9a, 0xa7, 0x70, 0x34, 0xb7, 0x7f, 0x4c, 0x3a,
  23001. 0xad, 0xfc, 0xd8, 0xb6, 0x76, 0x0a, 0xe3, 0xc1,
  23002. 0x87, 0x17, 0x07, 0x2d, 0x8d, 0xa3, 0x63, 0xa0,
  23003. /* Y */
  23004. 0xc1, 0x27, 0xb2, 0x97, 0x9b, 0x84, 0xe7, 0xcd,
  23005. 0x20, 0x65, 0x8d, 0x2b, 0x6a, 0x93, 0x75, 0xaa,
  23006. 0x8b, 0xe1, 0x3a, 0x7b, 0x24, 0x1a, 0xbe, 0xe8,
  23007. 0x36, 0xd2, 0xe6, 0x34, 0x8a, 0x7a, 0xb3, 0x28,
  23008. /* IV */
  23009. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  23010. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  23011. /* Encrypted Msg */
  23012. 0xe5, 0x17, 0xaf, 0x0d, 0x65, 0x4d, 0x3d, 0x50,
  23013. 0x96, 0x05, 0xc9, 0x63, 0x2c, 0xef, 0x1c, 0x1f,
  23014. 0x78, 0xc9, 0x90, 0x7a, 0x14, 0x00, 0xfc, 0x44,
  23015. 0x71, 0x6d, 0x57, 0x8c, 0xdf, 0x23, 0xca, 0x65,
  23016. 0xcf, 0x93, 0x06, 0xb6, 0x9a, 0xf4, 0x61, 0xbd,
  23017. 0x44, 0x1a, 0xeb, 0x52, 0x68, 0x0f, 0xd1, 0xde,
  23018. /* HMAC */
  23019. 0x5a, 0x22, 0xc1, 0x5d, 0x99, 0x66, 0x3f, 0x24,
  23020. 0x35, 0x96, 0xac, 0xf7, 0xf6, 0x28, 0x45, 0x16,
  23021. 0x52, 0x19, 0x0d, 0xe4, 0xb2, 0xca, 0x5b, 0x28,
  23022. 0x4e, 0xbb, 0xf3, 0x98, 0x57, 0xd7, 0x3b, 0xe2
  23023. #else
  23024. 0x04, 0x50, 0xf2, 0x93, 0xa2, 0x48, 0xa9, 0xc0,
  23025. 0x5a, 0x9a, 0xa7, 0x70, 0x34, 0xb7, 0x7f, 0x4c,
  23026. 0x3a, 0xad, 0xfc, 0xd8, 0xb6, 0x76, 0x0a, 0xe3,
  23027. 0xc1, 0x87, 0x17, 0x07, 0x2d, 0x8d, 0xa3, 0x63,
  23028. 0xa0, 0xc1, 0x27, 0xb2, 0x97, 0x9b, 0x84, 0xe7,
  23029. 0xcd, 0x20, 0x65, 0x8d, 0x2b, 0x6a, 0x93, 0x75,
  23030. 0xaa, 0x8b, 0xe1, 0x3a, 0x7b, 0x24, 0x1a, 0xbe,
  23031. 0xe8, 0x36, 0xd2, 0xe6, 0x34, 0x8a, 0x7a, 0xb3,
  23032. 0x28, 0xe5, 0x17, 0xaf, 0x0d, 0x65, 0x4d, 0x3d,
  23033. 0x50, 0x96, 0x05, 0xc9, 0x63, 0x2c, 0xef, 0x1c,
  23034. 0x1f, 0x78, 0xc9, 0x90, 0x7a, 0x14, 0x00, 0xfc,
  23035. 0x44, 0x71, 0x6d, 0x57, 0x8c, 0xdf, 0x23, 0xca,
  23036. 0x65, 0xcf, 0x93, 0x06, 0xb6, 0x9a, 0xf4, 0x61,
  23037. 0xbd, 0x44, 0x1a, 0xeb, 0x52, 0x68, 0x0f, 0xd1,
  23038. 0xde, 0xc7, 0x3f, 0x6f, 0xce, 0xbe, 0x49, 0x61,
  23039. 0x48, 0x01, 0x77, 0x41, 0xd0, 0xd8, 0x5b, 0x48,
  23040. 0xca, 0x4e, 0x47, 0x3e, 0x47, 0xbf, 0x1d, 0x28,
  23041. 0x4c, 0x18, 0x1a, 0xfb, 0x96, 0x95, 0xda, 0xde,
  23042. 0x55
  23043. #endif
  23044. };
  23045. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = {
  23046. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  23047. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  23048. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  23049. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
  23050. 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  23051. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f
  23052. };
  23053. #ifdef WOLFSSL_SMALL_STACK
  23054. userB = (ecc_key *)XMALLOC(sizeof(*userB), HEAP_HINT,
  23055. DYNAMIC_TYPE_TMP_BUFFER);
  23056. if (userB == NULL) {
  23057. ret = -10451;
  23058. }
  23059. #ifdef WOLFSSL_ECIES_OLD
  23060. if (ret == 0) {
  23061. userA = (ecc_key *)XMALLOC(sizeof(*userA), HEAP_HINT,
  23062. DYNAMIC_TYPE_TMP_BUFFER);
  23063. if (userA == NULL) {
  23064. ret = -10450;
  23065. }
  23066. }
  23067. #endif
  23068. #endif
  23069. if (ret == 0) {
  23070. ret = wc_ecc_init_ex(userB, HEAP_HINT, devId);
  23071. if (ret != 0)
  23072. ret = -10453;
  23073. }
  23074. if (ret == 0) {
  23075. userBInit = 1;
  23076. #ifdef WOLFSSL_ECIES_OLD
  23077. ret = wc_ecc_init_ex(userA, HEAP_HINT, devId);
  23078. if (ret != 0)
  23079. ret = -10452;
  23080. }
  23081. if (ret == 0) {
  23082. userAInit = 1;
  23083. tmpKey = userA;
  23084. #else
  23085. tmpKey = NULL;
  23086. #endif
  23087. }
  23088. if (ret == 0) {
  23089. #ifdef WOLFSSL_QNX_CAAM
  23090. ret = wc_ecc_import_private_key_ex(privKey, sizeof(privKey), pubKey,
  23091. sizeof(pubKey), userB, ECC_SECP256R1);
  23092. #else
  23093. ret = wc_ecc_import_private_key_ex(privKey, sizeof(privKey), NULL, 0,
  23094. userB, ECC_SECP256R1);
  23095. #endif
  23096. if (ret != 0)
  23097. ret = -10454;
  23098. }
  23099. #ifdef WOLFSSL_ECIES_OLD
  23100. if (ret == 0) {
  23101. ret = wc_ecc_import_x963_ex(pubKey, sizeof(pubKey), userA,
  23102. ECC_SECP256R1);
  23103. if (ret != 0)
  23104. ret = -10455;
  23105. }
  23106. #endif
  23107. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  23108. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  23109. !defined(HAVE_SELFTEST)
  23110. if (ret == 0) {
  23111. ret = wc_ecc_set_rng(userB, rng);
  23112. if (ret != 0) {
  23113. ret = -10456;
  23114. }
  23115. }
  23116. #else
  23117. (void)rng;
  23118. #endif
  23119. if (ret == 0) {
  23120. ret = wc_ecc_decrypt(userB, tmpKey, enc_msg, sizeof(enc_msg), plain,
  23121. &plainSz, NULL);
  23122. if (ret != 0)
  23123. ret = -10457;
  23124. }
  23125. if (ret == 0) {
  23126. if (XMEMCMP(plain, msg, sizeof(msg)) != 0) {
  23127. ret = -10458;
  23128. }
  23129. }
  23130. if (userBInit)
  23131. wc_ecc_free(userB);
  23132. #ifdef WOLFSSL_ECIES_OLD
  23133. if (userAInit)
  23134. wc_ecc_free(userA);
  23135. #endif
  23136. #ifdef WOLFSSL_SMALL_STACK
  23137. if (userB != NULL) {
  23138. XFREE(userB, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23139. }
  23140. #ifdef WOLFSSL_ECIES_OLD
  23141. if (userA != NULL) {
  23142. XFREE(userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23143. }
  23144. #endif
  23145. #endif
  23146. return ret;
  23147. }
  23148. #endif
  23149. static int ecc_encrypt_e2e_test(WC_RNG* rng, ecc_key* userA, ecc_key* userB,
  23150. byte encAlgo, byte kdfAlgo, byte macAlgo)
  23151. {
  23152. int ret = 0;
  23153. byte msg[48];
  23154. byte plain[48];
  23155. #ifdef WOLFSSL_ECIES_OLD
  23156. byte out[80];
  23157. #elif defined(WOLFSSL_ECIES_GEN_IV)
  23158. byte out[1 + ECC_KEYGEN_SIZE * 2 + 16 + 80];
  23159. #else
  23160. byte out[1 + ECC_KEYGEN_SIZE * 2 + 80];
  23161. #endif
  23162. word32 outSz = sizeof(out);
  23163. word32 plainSz = sizeof(plain);
  23164. int i;
  23165. ecEncCtx* cliCtx = NULL;
  23166. ecEncCtx* srvCtx = NULL;
  23167. byte cliSalt[EXCHANGE_SALT_SZ];
  23168. byte srvSalt[EXCHANGE_SALT_SZ];
  23169. const byte* tmpSalt;
  23170. byte msg2[48];
  23171. byte plain2[48];
  23172. #ifdef WOLFSSL_ECIES_OLD
  23173. byte out2[80];
  23174. #elif defined(WOLFSSL_ECIES_GEN_IV)
  23175. byte out2[1 + ECC_KEYGEN_SIZE * 2 + 16 + 80];
  23176. #else
  23177. byte out2[1 + ECC_KEYGEN_SIZE * 2 + 80];
  23178. #endif
  23179. word32 outSz2 = sizeof(out2);
  23180. word32 plainSz2 = sizeof(plain2);
  23181. #ifdef WOLFSSL_SMALL_STACK
  23182. ecc_key *tmpKey = (ecc_key *)XMALLOC(sizeof(ecc_key), HEAP_HINT,
  23183. DYNAMIC_TYPE_TMP_BUFFER);
  23184. #else
  23185. ecc_key tmpKey[1];
  23186. #endif
  23187. #ifdef WOLFSSL_SMALL_STACK
  23188. if (tmpKey == NULL) {
  23189. ERROR_OUT(MEMORY_E, done);
  23190. }
  23191. #endif
  23192. ret = wc_ecc_init_ex(tmpKey, HEAP_HINT, devId);
  23193. if (ret != 0)
  23194. goto done;
  23195. /* set message to incrementing 0,1,2,etc... */
  23196. for (i = 0; i < (int)sizeof(msg); i++)
  23197. msg[i] = i;
  23198. /* encrypt msg to B */
  23199. ret = wc_ecc_encrypt(userA, userB, msg, sizeof(msg), out, &outSz, NULL);
  23200. if (ret != 0) {
  23201. ret = -10405; goto done;
  23202. }
  23203. #ifdef WOLFSSL_ECIES_OLD
  23204. tmpKey->dp = userA->dp;
  23205. ret = wc_ecc_copy_point(&userA->pubkey, &tmpKey->pubkey);
  23206. if (ret != 0) {
  23207. ret = -10413; goto done;
  23208. }
  23209. #endif
  23210. /* decrypt msg from A */
  23211. ret = wc_ecc_decrypt(userB, tmpKey, out, outSz, plain, &plainSz, NULL);
  23212. if (ret != 0) {
  23213. ret = -10406; goto done;
  23214. }
  23215. if (XMEMCMP(plain, msg, sizeof(msg)) != 0) {
  23216. ret = -10407; goto done;
  23217. }
  23218. #ifndef WOLFSSL_ECIES_OLD
  23219. /* A decrypts msg (response) from B */
  23220. ret = wc_ecc_decrypt(userB, NULL, out, outSz, plain2, &plainSz2, NULL);
  23221. if (ret != 0)
  23222. goto done;
  23223. if (XMEMCMP(plain, msg, sizeof(msg)) != 0) {
  23224. ret = -10415; goto done;
  23225. }
  23226. #endif
  23227. /* let's verify message exchange works, A is client, B is server */
  23228. cliCtx = wc_ecc_ctx_new(REQ_RESP_CLIENT, rng);
  23229. srvCtx = wc_ecc_ctx_new(REQ_RESP_SERVER, rng);
  23230. if (cliCtx == NULL || srvCtx == NULL) {
  23231. ret = -10408; goto done;
  23232. }
  23233. ret = wc_ecc_ctx_set_algo(cliCtx, encAlgo, kdfAlgo, macAlgo);
  23234. if (ret != 0)
  23235. goto done;
  23236. ret = wc_ecc_ctx_set_algo(srvCtx, encAlgo, kdfAlgo, macAlgo);
  23237. if (ret != 0)
  23238. goto done;
  23239. /* get salt to send to peer */
  23240. tmpSalt = wc_ecc_ctx_get_own_salt(cliCtx);
  23241. if (tmpSalt == NULL) {
  23242. ret = -10409; goto done;
  23243. }
  23244. XMEMCPY(cliSalt, tmpSalt, EXCHANGE_SALT_SZ);
  23245. tmpSalt = wc_ecc_ctx_get_own_salt(srvCtx);
  23246. if (tmpSalt == NULL) {
  23247. ret = -10410; goto done;
  23248. }
  23249. XMEMCPY(srvSalt, tmpSalt, EXCHANGE_SALT_SZ);
  23250. /* in actual use, we'd get the peer's salt over the transport */
  23251. ret = wc_ecc_ctx_set_peer_salt(cliCtx, srvSalt);
  23252. if (ret != 0)
  23253. goto done;
  23254. ret = wc_ecc_ctx_set_peer_salt(srvCtx, cliSalt);
  23255. if (ret != 0)
  23256. goto done;
  23257. ret = wc_ecc_ctx_set_info(cliCtx, (byte*)"wolfSSL MSGE", 11);
  23258. if (ret != 0)
  23259. goto done;
  23260. ret = wc_ecc_ctx_set_info(srvCtx, (byte*)"wolfSSL MSGE", 11);
  23261. if (ret != 0)
  23262. goto done;
  23263. /* get encrypted msg (request) to send to B */
  23264. outSz = sizeof(out);
  23265. ret = wc_ecc_encrypt(userA, userB, msg, sizeof(msg), out, &outSz,cliCtx);
  23266. if (ret != 0)
  23267. goto done;
  23268. #ifndef WOLFSSL_ECIES_OLD
  23269. wc_ecc_free(tmpKey);
  23270. #endif
  23271. /* B decrypts msg (request) from A */
  23272. plainSz = sizeof(plain);
  23273. ret = wc_ecc_decrypt(userB, tmpKey, out, outSz, plain, &plainSz, srvCtx);
  23274. if (ret != 0)
  23275. goto done;
  23276. if (XMEMCMP(plain, msg, sizeof(msg)) != 0) {
  23277. ret = -10411; goto done;
  23278. }
  23279. /* msg2 (response) from B to A */
  23280. for (i = 0; i < (int)sizeof(msg2); i++)
  23281. msg2[i] = i + sizeof(msg2);
  23282. /* get encrypted msg (response) to send to B */
  23283. ret = wc_ecc_encrypt(userB, userA, msg2, sizeof(msg2), out2,
  23284. &outSz2, srvCtx);
  23285. if (ret != 0)
  23286. goto done;
  23287. #ifdef WOLFSSL_ECIES_OLD
  23288. tmpKey->dp = userB->dp;
  23289. ret = wc_ecc_copy_point(&userB->pubkey, &tmpKey->pubkey);
  23290. if (ret != 0) {
  23291. ret = -10414; goto done;
  23292. }
  23293. #else
  23294. wc_ecc_free(tmpKey);
  23295. #endif
  23296. /* A decrypts msg (response) from B */
  23297. ret = wc_ecc_decrypt(userA, tmpKey, out2, outSz2, plain2, &plainSz2,
  23298. cliCtx);
  23299. if (ret != 0)
  23300. goto done;
  23301. if (XMEMCMP(plain2, msg2, sizeof(msg2)) != 0) {
  23302. ret = -10412; goto done;
  23303. }
  23304. #if defined(HAVE_COMP_KEY) && \
  23305. (! defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3)))
  23306. /* Create new client and server contexts. */
  23307. wc_ecc_ctx_free(srvCtx);
  23308. wc_ecc_ctx_free(cliCtx);
  23309. /* let's verify message exchange works, A is client, B is server */
  23310. cliCtx = wc_ecc_ctx_new(REQ_RESP_CLIENT, rng);
  23311. srvCtx = wc_ecc_ctx_new(REQ_RESP_SERVER, rng);
  23312. if (cliCtx == NULL || srvCtx == NULL) {
  23313. ret = -10416; goto done;
  23314. }
  23315. ret = wc_ecc_ctx_set_algo(cliCtx, encAlgo, kdfAlgo, macAlgo);
  23316. if (ret != 0)
  23317. goto done;
  23318. ret = wc_ecc_ctx_set_algo(srvCtx, encAlgo, kdfAlgo, macAlgo);
  23319. if (ret != 0)
  23320. goto done;
  23321. /* get salt to send to peer */
  23322. tmpSalt = wc_ecc_ctx_get_own_salt(cliCtx);
  23323. if (tmpSalt == NULL) {
  23324. ret = -10417; goto done;
  23325. }
  23326. XMEMCPY(cliSalt, tmpSalt, EXCHANGE_SALT_SZ);
  23327. tmpSalt = wc_ecc_ctx_get_own_salt(srvCtx);
  23328. if (tmpSalt == NULL) {
  23329. ret = -10418; goto done;
  23330. }
  23331. XMEMCPY(srvSalt, tmpSalt, EXCHANGE_SALT_SZ);
  23332. /* in actual use, we'd get the peer's salt over the transport */
  23333. ret = wc_ecc_ctx_set_peer_salt(cliCtx, srvSalt);
  23334. if (ret != 0)
  23335. goto done;
  23336. ret = wc_ecc_ctx_set_peer_salt(srvCtx, cliSalt);
  23337. if (ret != 0)
  23338. goto done;
  23339. ret = wc_ecc_ctx_set_info(cliCtx, (byte*)"wolfSSL MSGE", 12);
  23340. if (ret != 0)
  23341. goto done;
  23342. ret = wc_ecc_ctx_set_info(srvCtx, (byte*)"wolfSSL MSGE", 12);
  23343. if (ret != 0)
  23344. goto done;
  23345. /* get encrypted msg (request) to send to B - compressed public key */
  23346. outSz = sizeof(out);
  23347. ret = wc_ecc_encrypt_ex(userA, userB, msg, sizeof(msg), out, &outSz, cliCtx,
  23348. 1);
  23349. if (ret != 0)
  23350. goto done;
  23351. #ifndef WOLFSSL_ECIES_OLD
  23352. wc_ecc_free(tmpKey);
  23353. #endif
  23354. /* B decrypts msg (request) from A - out has a compressed public key */
  23355. plainSz = sizeof(plain);
  23356. ret = wc_ecc_decrypt(userB, tmpKey, out, outSz, plain, &plainSz, srvCtx);
  23357. if (ret != 0)
  23358. goto done;
  23359. if (XMEMCMP(plain, msg, sizeof(msg)) != 0) {
  23360. ret = -10419; goto done;
  23361. }
  23362. #endif /* HAVE_COMP_KEY && (!FIPS || FIPS>=5.3) */
  23363. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && \
  23364. (ECC_MIN_KEY_SZ <= 256) && defined(WOLFSSL_AES_128)
  23365. ret = ecc_encrypt_kat(rng);
  23366. #endif
  23367. done:
  23368. /* cleanup */
  23369. wc_ecc_ctx_free(srvCtx);
  23370. wc_ecc_ctx_free(cliCtx);
  23371. #ifdef WOLFSSL_SMALL_STACK
  23372. if (tmpKey != NULL) {
  23373. wc_ecc_free(tmpKey);
  23374. XFREE(tmpKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23375. }
  23376. #else
  23377. wc_ecc_free(tmpKey);
  23378. #endif
  23379. return ret;
  23380. }
  23381. #endif /* !HAVE_FIPS || FIPS_VERSION_GE(5,3) */
  23382. WOLFSSL_TEST_SUBROUTINE int ecc_encrypt_test(void)
  23383. {
  23384. WC_RNG rng;
  23385. int ret;
  23386. #ifdef WOLFSSL_SMALL_STACK
  23387. ecc_key *userA;
  23388. ecc_key *userB;
  23389. #else
  23390. ecc_key userA[1];
  23391. ecc_key userB[1];
  23392. #endif
  23393. #ifndef HAVE_FIPS
  23394. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  23395. #else
  23396. ret = wc_InitRng(&rng);
  23397. #endif
  23398. if (ret != 0)
  23399. return -10400;
  23400. #ifdef WOLFSSL_SMALL_STACK
  23401. userA = (ecc_key *)XMALLOC(sizeof *userA, HEAP_HINT,
  23402. DYNAMIC_TYPE_TMP_BUFFER);
  23403. userB = (ecc_key *)XMALLOC(sizeof *userB, HEAP_HINT,
  23404. DYNAMIC_TYPE_TMP_BUFFER);
  23405. if ((userA == NULL) || (userB == NULL)) {
  23406. ERROR_OUT(MEMORY_E, done);
  23407. }
  23408. #endif
  23409. XMEMSET(userA, 0, sizeof *userA);
  23410. XMEMSET(userB, 0, sizeof *userB);
  23411. ret = wc_ecc_init_ex(userA, HEAP_HINT, devId);
  23412. if (ret != 0)
  23413. goto done;
  23414. ret = wc_ecc_init_ex(userB, HEAP_HINT, devId);
  23415. if (ret != 0)
  23416. goto done;
  23417. ret = wc_ecc_make_key(&rng, ECC_KEYGEN_SIZE, userA);
  23418. #if defined(WOLFSSL_ASYNC_CRYPT)
  23419. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_NONE);
  23420. #endif
  23421. if (ret != 0){
  23422. ret = -10401; goto done;
  23423. }
  23424. ret = wc_ecc_make_key(&rng, ECC_KEYGEN_SIZE, userB);
  23425. #if defined(WOLFSSL_ASYNC_CRYPT)
  23426. ret = wc_AsyncWait(ret, &userB->asyncDev, WC_ASYNC_FLAG_NONE);
  23427. #endif
  23428. if (ret != 0){
  23429. ret = -10402; goto done;
  23430. }
  23431. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  23432. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  23433. !defined(HAVE_SELFTEST)
  23434. ret = wc_ecc_set_rng(userA, &rng);
  23435. if (ret != 0) {
  23436. ret = -10403; goto done;
  23437. }
  23438. ret = wc_ecc_set_rng(userB, &rng);
  23439. if (ret != 0) {
  23440. ret = -10404; goto done;
  23441. }
  23442. #endif
  23443. #if !defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3))
  23444. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  23445. #ifdef WOLFSSL_AES_128
  23446. if (ret == 0) {
  23447. ret = ecc_encrypt_e2e_test(&rng, userA, userB, ecAES_128_CBC,
  23448. ecHKDF_SHA256, ecHMAC_SHA256);
  23449. if (ret != 0) {
  23450. printf("ECIES: AES_128_CBC, HKDF_SHA256, HMAC_SHA256\n");
  23451. }
  23452. }
  23453. #endif
  23454. #ifdef WOLFSSL_AES_256
  23455. if (ret == 0) {
  23456. ret = ecc_encrypt_e2e_test(&rng, userA, userB, ecAES_256_CBC,
  23457. ecHKDF_SHA256, ecHMAC_SHA256);
  23458. if (ret != 0) {
  23459. printf("ECIES: AES_256_CBC, HKDF_SHA256, HMAC_SHA256\n");
  23460. }
  23461. }
  23462. #endif
  23463. #endif
  23464. #if !defined(NO_AES) && defined(WOLFSSL_AES_COUNTER)
  23465. #ifdef WOLFSSL_AES_128
  23466. if (ret == 0) {
  23467. ret = ecc_encrypt_e2e_test(&rng, userA, userB, ecAES_128_CTR,
  23468. ecHKDF_SHA256, ecHMAC_SHA256);
  23469. if (ret != 0) {
  23470. printf("ECIES: AES_128_CTR, HKDF_SHA256, HMAC_SHA256\n");
  23471. }
  23472. }
  23473. #endif
  23474. #ifdef WOLFSSL_AES_256
  23475. if (ret == 0) {
  23476. ret = ecc_encrypt_e2e_test(&rng, userA, userB, ecAES_256_CTR,
  23477. ecHKDF_SHA256, ecHMAC_SHA256);
  23478. if (ret != 0) {
  23479. printf("ECIES: AES_256_CTR, HKDF_SHA256, HMAC_SHA256\n");
  23480. }
  23481. }
  23482. #endif
  23483. #endif
  23484. #endif /* !HAVE_FIPS || FIPS_VERSION_GE(5,3) */
  23485. done:
  23486. #ifdef WOLFSSL_SMALL_STACK
  23487. if (userA != NULL) {
  23488. wc_ecc_free(userA);
  23489. XFREE(userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23490. }
  23491. if (userB != NULL) {
  23492. wc_ecc_free(userB);
  23493. XFREE(userB, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23494. }
  23495. #else
  23496. wc_ecc_free(userB);
  23497. wc_ecc_free(userA);
  23498. #endif
  23499. wc_FreeRng(&rng);
  23500. return ret;
  23501. }
  23502. #endif /* HAVE_ECC_ENCRYPT && HAVE_AES_CBC && WOLFSSL_AES_128 */
  23503. #if defined(USE_CERT_BUFFERS_256) && !defined(WOLFSSL_ATECC508A) && \
  23504. !defined(WOLFSSL_ATECC608A) && !defined(NO_ECC256) && \
  23505. defined(HAVE_ECC_VERIFY) && defined(HAVE_ECC_SIGN) && \
  23506. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  23507. WOLFSSL_TEST_SUBROUTINE int ecc_test_buffers(void)
  23508. {
  23509. size_t bytes;
  23510. #ifdef WOLFSSL_SMALL_STACK
  23511. ecc_key *cliKey = (ecc_key *)XMALLOC(sizeof *cliKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23512. ecc_key *servKey = (ecc_key *)XMALLOC(sizeof *servKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23513. ecc_key *tmpKey = (ecc_key *)XMALLOC(sizeof *tmpKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23514. #else
  23515. ecc_key cliKey[1];
  23516. ecc_key servKey[1];
  23517. ecc_key tmpKey[1];
  23518. #endif
  23519. WC_RNG rng;
  23520. word32 idx = 0;
  23521. int ret;
  23522. /* pad our test message to 32 bytes so evenly divisible by AES_BLOCK_SZ */
  23523. byte in[] = "Everyone gets Friday off. ecc p";
  23524. word32 inLen = (word32)XSTRLEN((char*)in);
  23525. byte out[256];
  23526. byte plain[256];
  23527. int verify = 0;
  23528. word32 x;
  23529. #ifdef WOLFSSL_SMALL_STACK
  23530. if ((cliKey == NULL) || (servKey == NULL) || (tmpKey == NULL))
  23531. ERROR_OUT(MEMORY_E, done);
  23532. #endif
  23533. ret = wc_ecc_init_ex(cliKey, HEAP_HINT, devId);
  23534. if (ret != 0)
  23535. ERROR_OUT(-10420, done);
  23536. ret = wc_ecc_init_ex(servKey, HEAP_HINT, devId);
  23537. if (ret != 0)
  23538. ERROR_OUT(-10421, done);
  23539. ret = wc_ecc_init_ex(tmpKey, HEAP_HINT, devId);
  23540. if (ret != 0)
  23541. ERROR_OUT(-10421, done);
  23542. bytes = (size_t)sizeof_ecc_clikey_der_256;
  23543. /* place client key into ecc_key struct cliKey */
  23544. ret = wc_EccPrivateKeyDecode(ecc_clikey_der_256, &idx, cliKey,
  23545. (word32)bytes);
  23546. if (ret != 0)
  23547. ERROR_OUT(-10422, done);
  23548. idx = 0;
  23549. bytes = (size_t)sizeof_ecc_key_der_256;
  23550. /* place server key into ecc_key struct servKey */
  23551. ret = wc_EccPrivateKeyDecode(ecc_key_der_256, &idx, servKey,
  23552. (word32)bytes);
  23553. if (ret != 0)
  23554. ERROR_OUT(-10423, done);
  23555. #ifndef WC_NO_RNG
  23556. #ifndef HAVE_FIPS
  23557. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  23558. #else
  23559. ret = wc_InitRng(&rng);
  23560. #endif
  23561. if (ret != 0)
  23562. ERROR_OUT(-10424, done);
  23563. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  23564. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  23565. !defined(HAVE_SELFTEST)
  23566. ret = wc_ecc_set_rng(cliKey, &rng);
  23567. if (ret != 0) {
  23568. ERROR_OUT(-10425, done);
  23569. }
  23570. ret = wc_ecc_set_rng(servKey, &rng);
  23571. if (ret != 0) {
  23572. ERROR_OUT(-10425, done);
  23573. }
  23574. #endif
  23575. #endif /* !WC_NO_RNG */
  23576. #if defined(HAVE_ECC_ENCRYPT) && defined(HAVE_HKDF) && \
  23577. defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  23578. {
  23579. word32 y;
  23580. /* test encrypt and decrypt if they're available */
  23581. x = sizeof(out);
  23582. ret = wc_ecc_encrypt(cliKey, servKey, in, sizeof(in), out, &x, NULL);
  23583. if (ret < 0)
  23584. ERROR_OUT(-10426, done);
  23585. #ifdef WOLFSSL_ECIES_OLD
  23586. tmpKey->dp = cliKey->dp;
  23587. ret = wc_ecc_copy_point(&cliKey->pubkey, &tmpKey->pubkey);
  23588. if (ret != 0) {
  23589. ret = -10414; goto done;
  23590. }
  23591. #endif
  23592. y = sizeof(plain);
  23593. ret = wc_ecc_decrypt(servKey, tmpKey, out, x, plain, &y, NULL);
  23594. if (ret < 0)
  23595. ERROR_OUT(-10427, done);
  23596. if (XMEMCMP(plain, in, inLen))
  23597. ERROR_OUT(-10428, done);
  23598. }
  23599. #endif
  23600. x = sizeof(out);
  23601. do {
  23602. #if defined(WOLFSSL_ASYNC_CRYPT)
  23603. ret = wc_AsyncWait(ret, &cliKey->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  23604. #endif
  23605. if (ret == 0)
  23606. ret = wc_ecc_sign_hash(in, inLen, out, &x, &rng, cliKey);
  23607. } while (ret == WC_PENDING_E);
  23608. if (ret < 0)
  23609. ERROR_OUT(-10429, done);
  23610. TEST_SLEEP();
  23611. XMEMSET(plain, 0, sizeof(plain));
  23612. do {
  23613. #if defined(WOLFSSL_ASYNC_CRYPT)
  23614. ret = wc_AsyncWait(ret, &cliKey->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  23615. #endif
  23616. if (ret == 0)
  23617. ret = wc_ecc_verify_hash(out, x, in, inLen, &verify,
  23618. cliKey);
  23619. } while (ret == WC_PENDING_E);
  23620. if (ret < 0)
  23621. ERROR_OUT(-10430, done);
  23622. if (verify != 1)
  23623. ERROR_OUT(-10431, done);
  23624. TEST_SLEEP();
  23625. #ifdef WOLFSSL_CERT_EXT
  23626. idx = 0;
  23627. bytes = sizeof_ecc_clikeypub_der_256;
  23628. ret = wc_EccPublicKeyDecode(ecc_clikeypub_der_256, &idx, cliKey,
  23629. (word32) bytes);
  23630. if (ret != 0)
  23631. ERROR_OUT(-10432, done);
  23632. #endif
  23633. ret = 0;
  23634. done:
  23635. #ifdef WOLFSSL_SMALL_STACK
  23636. if (cliKey != NULL) {
  23637. wc_ecc_free(cliKey);
  23638. XFREE(cliKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23639. }
  23640. if (servKey != NULL) {
  23641. wc_ecc_free(servKey);
  23642. XFREE(servKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23643. }
  23644. if (tmpKey != NULL) {
  23645. wc_ecc_free(tmpKey);
  23646. XFREE(tmpKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23647. }
  23648. #else
  23649. wc_ecc_free(cliKey);
  23650. wc_ecc_free(servKey);
  23651. wc_ecc_free(tmpKey);
  23652. #endif
  23653. wc_FreeRng(&rng);
  23654. return ret;
  23655. }
  23656. #endif /* USE_CERT_BUFFERS_256 && !WOLFSSL_ATECCX08A && !NO_ECC256 */
  23657. #endif /* HAVE_ECC */
  23658. #ifdef HAVE_CURVE25519
  23659. #if defined(HAVE_CURVE25519_SHARED_SECRET) && \
  23660. defined(HAVE_CURVE25519_KEY_IMPORT)
  23661. #ifdef CURVE25519_OVERFLOW_ALL_TESTS
  23662. #define X25519_TEST_CNT 5
  23663. #else
  23664. #define X25519_TEST_CNT 1
  23665. #endif
  23666. static int curve25519_overflow_test(void)
  23667. {
  23668. /* secret key for party a */
  23669. byte sa[X25519_TEST_CNT][32] = {
  23670. {
  23671. 0x8d,0xaf,0x6e,0x7a,0xc1,0xeb,0x8d,0x30,
  23672. 0x99,0x86,0xd3,0x90,0x47,0x96,0x21,0x3c,
  23673. 0x3a,0x75,0xc0,0x7b,0x75,0x01,0x75,0xa3,
  23674. 0x81,0x4b,0xff,0x5a,0xbc,0x96,0x87,0x28
  23675. },
  23676. #ifdef CURVE25519_OVERFLOW_ALL_TESTS
  23677. {
  23678. 0x9d,0x63,0x5f,0xce,0xe2,0xe8,0xd7,0xfb,
  23679. 0x68,0x77,0x0e,0x44,0xd1,0xad,0x87,0x2b,
  23680. 0xf4,0x65,0x06,0xb7,0xbb,0xdb,0xbe,0x6e,
  23681. 0x02,0x43,0x24,0xc7,0x3d,0x7b,0x88,0x60
  23682. },
  23683. {
  23684. 0x63,0xbf,0x76,0xa9,0x73,0xa0,0x09,0xb9,
  23685. 0xcc,0xc9,0x4d,0x47,0x2d,0x14,0x0e,0x52,
  23686. 0xa3,0x84,0x55,0xb8,0x7c,0xdb,0xce,0xb1,
  23687. 0xe4,0x5b,0x8a,0xb9,0x30,0xf1,0xa4,0xa0
  23688. },
  23689. {
  23690. 0x63,0xbf,0x76,0xa9,0x73,0xa0,0x09,0xb9,
  23691. 0xcc,0xc9,0x4d,0x47,0x2d,0x14,0x0e,0x52,
  23692. 0xa3,0x84,0x55,0xb8,0x7c,0xdb,0xce,0xb1,
  23693. 0xe4,0x5b,0x8a,0xb9,0x30,0xf1,0xa4,0xa0
  23694. },
  23695. {
  23696. 0x63,0xbf,0x76,0xa9,0x73,0xa0,0x09,0xb9,
  23697. 0xcc,0xc9,0x4d,0x47,0x2d,0x14,0x0e,0x52,
  23698. 0xa3,0x84,0x55,0xb8,0x7c,0xdb,0xce,0xb1,
  23699. 0xe4,0x5b,0x8a,0xb9,0x30,0xf1,0xa4,0xa0
  23700. }
  23701. #endif
  23702. };
  23703. /* public key for party b */
  23704. byte pb[X25519_TEST_CNT][32] = {
  23705. {
  23706. 0x7f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  23707. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  23708. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  23709. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xf0
  23710. },
  23711. #ifdef CURVE25519_OVERFLOW_ALL_TESTS
  23712. {
  23713. /* 0xff first byte in original - invalid! */
  23714. 0x7f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  23715. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  23716. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  23717. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xf0
  23718. },
  23719. {
  23720. 0x36,0x1a,0x74,0x87,0x28,0x59,0xe0,0xb6,
  23721. 0xe4,0x2b,0x17,0x9b,0x16,0xb0,0x3b,0xf8,
  23722. 0xb8,0x9f,0x2a,0x8f,0xc5,0x33,0x68,0x4f,
  23723. 0xde,0x4d,0xd8,0x80,0x63,0xe7,0xb4,0x0a
  23724. },
  23725. {
  23726. 0x00,0x80,0x38,0x59,0x19,0x3a,0x66,0x12,
  23727. 0xfd,0xa1,0xec,0x1c,0x40,0x84,0x40,0xbd,
  23728. 0x64,0x10,0x8b,0x53,0x81,0x21,0x03,0x2d,
  23729. 0x7d,0x33,0xb4,0x01,0x57,0x0d,0xe1,0x89
  23730. },
  23731. {
  23732. 0x1d,0xf8,0xf8,0x33,0x89,0x6c,0xb7,0xba,
  23733. 0x94,0x73,0xfa,0xc2,0x36,0xac,0xbe,0x49,
  23734. 0xaf,0x85,0x3e,0x93,0x5f,0xae,0xb2,0xc0,
  23735. 0xc8,0x80,0x8f,0x4a,0xaa,0xd3,0x55,0x2b
  23736. }
  23737. #endif
  23738. };
  23739. /* expected shared key */
  23740. byte ss[X25519_TEST_CNT][32] = {
  23741. {
  23742. 0x5c,0x4c,0x85,0x5f,0xfb,0x20,0x38,0xcc,
  23743. 0x55,0x16,0x5b,0x8a,0xa7,0xed,0x57,0x6e,
  23744. 0x35,0xaa,0x71,0x67,0x85,0x1f,0xb6,0x28,
  23745. 0x17,0x07,0x7b,0xda,0x76,0xdd,0xe0,0xb4
  23746. },
  23747. #ifdef CURVE25519_OVERFLOW_ALL_TESTS
  23748. {
  23749. 0x33,0xf6,0xc1,0x34,0x62,0x92,0x06,0x02,
  23750. 0x95,0xdb,0x91,0x4c,0x5d,0x52,0x54,0xc7,
  23751. 0xd2,0x5b,0x24,0xb5,0x4f,0x33,0x59,0x79,
  23752. 0x9f,0x6d,0x7e,0x4a,0x4c,0x30,0xd6,0x38
  23753. },
  23754. {
  23755. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  23756. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  23757. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  23758. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x02
  23759. },
  23760. {
  23761. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  23762. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  23763. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  23764. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x09
  23765. },
  23766. {
  23767. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  23768. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  23769. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  23770. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x10
  23771. }
  23772. #endif
  23773. };
  23774. int ret = 0;
  23775. int i;
  23776. word32 y;
  23777. byte shared[32];
  23778. curve25519_key userA;
  23779. wc_curve25519_init_ex(&userA, HEAP_HINT, devId);
  23780. for (i = 0; i < X25519_TEST_CNT; i++) {
  23781. if (wc_curve25519_import_private_raw(sa[i], sizeof(sa[i]), pb[i],
  23782. sizeof(pb[i]), &userA) != 0) {
  23783. ret = -10500 - i; break;
  23784. }
  23785. /* test against known test vector */
  23786. XMEMSET(shared, 0, sizeof(shared));
  23787. y = sizeof(shared);
  23788. if (wc_curve25519_shared_secret(&userA, &userA, shared, &y) != 0) {
  23789. ret = -10510 - i; break;
  23790. }
  23791. if (XMEMCMP(ss[i], shared, y)) {
  23792. ret = -10520 - i; break;
  23793. }
  23794. }
  23795. wc_curve25519_free(&userA);
  23796. return ret;
  23797. }
  23798. /* Test the wc_curve25519_check_public API.
  23799. *
  23800. * returns 0 on success and -ve on failure.
  23801. */
  23802. static int curve25519_check_public_test(void)
  23803. {
  23804. /* Little-endian values that will fail */
  23805. byte fail_le[][CURVE25519_KEYSIZE] = {
  23806. {
  23807. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  23808. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  23809. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  23810. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  23811. },
  23812. {
  23813. 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  23814. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  23815. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  23816. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  23817. },
  23818. {
  23819. 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  23820. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  23821. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  23822. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x81
  23823. },
  23824. };
  23825. /* Big-endian values that will fail */
  23826. byte fail_be[][CURVE25519_KEYSIZE] = {
  23827. {
  23828. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  23829. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  23830. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  23831. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  23832. },
  23833. {
  23834. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  23835. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  23836. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  23837. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  23838. },
  23839. {
  23840. 0x81,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  23841. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  23842. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  23843. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  23844. },
  23845. };
  23846. /* Good or valid public value */
  23847. byte good[CURVE25519_KEYSIZE] = {
  23848. 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  23849. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  23850. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  23851. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  23852. };
  23853. int i;
  23854. /* Parameter checks */
  23855. /* NULL pointer */
  23856. if (wc_curve25519_check_public(NULL, 0, EC25519_LITTLE_ENDIAN) !=
  23857. BAD_FUNC_ARG) {
  23858. return -10600;
  23859. }
  23860. if (wc_curve25519_check_public(NULL, 0, EC25519_BIG_ENDIAN) !=
  23861. BAD_FUNC_ARG) {
  23862. return -10601;
  23863. }
  23864. /* Length of 0 treated differently to other invalid lengths for TLS */
  23865. if (wc_curve25519_check_public(good, 0, EC25519_LITTLE_ENDIAN) != BUFFER_E)
  23866. return -10602;
  23867. if (wc_curve25519_check_public(good, 0, EC25519_BIG_ENDIAN) != BUFFER_E)
  23868. return -10603;
  23869. /* Length not CURVE25519_KEYSIZE */
  23870. for (i = 1; i < CURVE25519_KEYSIZE + 2; i++) {
  23871. if (i == CURVE25519_KEYSIZE)
  23872. continue;
  23873. if (wc_curve25519_check_public(good, i, EC25519_LITTLE_ENDIAN) !=
  23874. ECC_BAD_ARG_E) {
  23875. return -10604 - i;
  23876. }
  23877. if (wc_curve25519_check_public(good, i, EC25519_BIG_ENDIAN) !=
  23878. ECC_BAD_ARG_E) {
  23879. return -10614 - i;
  23880. }
  23881. }
  23882. /* Little-endian fail cases */
  23883. for (i = 0; i < (int)(sizeof(fail_le) / sizeof(*fail_le)); i++) {
  23884. if (wc_curve25519_check_public(fail_le[i], CURVE25519_KEYSIZE,
  23885. EC25519_LITTLE_ENDIAN) == 0) {
  23886. return -10624 - i;
  23887. }
  23888. }
  23889. /* Big-endian fail cases */
  23890. for (i = 0; i < (int)(sizeof(fail_be) / sizeof(*fail_be)); i++) {
  23891. if (wc_curve25519_check_public(fail_be[i], CURVE25519_KEYSIZE,
  23892. EC25519_BIG_ENDIAN) == 0) {
  23893. return -10634 - i;
  23894. }
  23895. }
  23896. /* Check a valid public value works! */
  23897. if (wc_curve25519_check_public(good, CURVE25519_KEYSIZE,
  23898. EC25519_LITTLE_ENDIAN) != 0) {
  23899. return -10644;
  23900. }
  23901. if (wc_curve25519_check_public(good, CURVE25519_KEYSIZE,
  23902. EC25519_BIG_ENDIAN) != 0) {
  23903. return -10645;
  23904. }
  23905. return 0;
  23906. }
  23907. #endif /* HAVE_CURVE25519_SHARED_SECRET && HAVE_CURVE25519_KEY_IMPORT */
  23908. #if !defined(NO_ASN) && defined(HAVE_CURVE25519_KEY_EXPORT) && \
  23909. defined(HAVE_CURVE25519_KEY_IMPORT)
  23910. static int curve255519_der_test(void)
  23911. {
  23912. int ret = 0;
  23913. /* certs/statickeys/x25519.der */
  23914. const byte kCurve25519PrivDer[] = {
  23915. 0x30, 0x2E, 0x02, 0x01, 0x00, 0x30, 0x05, 0x06, 0x03, 0x2B, 0x65, 0x6E,
  23916. 0x04, 0x22, 0x04, 0x20, 0x78, 0x8E, 0x31, 0x5C, 0x33, 0xA9, 0x19, 0xC0,
  23917. 0x5E, 0x36, 0x70, 0x1B, 0xA4, 0xE8, 0xEF, 0xC1, 0x89, 0x8C, 0xB3, 0x15,
  23918. 0xC6, 0x79, 0xD3, 0xAC, 0x22, 0x00, 0xAE, 0xFA, 0xB3, 0xB7, 0x0F, 0x78
  23919. };
  23920. /* certs/statickeys/x25519-pub.der */
  23921. const byte kCurve25519PubDer[] = {
  23922. 0x30, 0x2A, 0x30, 0x05, 0x06, 0x03, 0x2B, 0x65, 0x6E, 0x03, 0x21, 0x00,
  23923. 0x09, 0xBC, 0x8C, 0xC7, 0x45, 0x0D, 0xC1, 0xC2, 0x02, 0x57, 0x9A, 0x68,
  23924. 0x3A, 0xFD, 0x7A, 0xA8, 0xA5, 0x2F, 0xF0, 0x99, 0x39, 0x98, 0xEA, 0x26,
  23925. 0xA2, 0x5B, 0x38, 0xFD, 0x96, 0xDB, 0x2A, 0x26
  23926. };
  23927. curve25519_key key;
  23928. byte output[128];
  23929. word32 outputSz = 128;
  23930. word32 idx;
  23931. if (wc_curve25519_init_ex(&key, HEAP_HINT, devId) != 0) {
  23932. return -10723;
  23933. }
  23934. /* Test decode / encode of Curve25519 private key only */
  23935. if (ret == 0) {
  23936. idx = 0;
  23937. ret = wc_Curve25519PrivateKeyDecode(kCurve25519PrivDer, &idx, &key,
  23938. (word32)sizeof(kCurve25519PrivDer));
  23939. }
  23940. if (ret == 0) {
  23941. outputSz = (word32)sizeof(output);
  23942. ret = wc_Curve25519PrivateKeyToDer(&key, output, outputSz);
  23943. if (ret >= 0) {
  23944. outputSz = ret;
  23945. ret = 0;
  23946. }
  23947. else {
  23948. ret = -10724;
  23949. }
  23950. }
  23951. if (ret == 0 && (outputSz != (word32)sizeof(kCurve25519PrivDer) ||
  23952. XMEMCMP(output, kCurve25519PrivDer, outputSz) != 0)) {
  23953. ret = -10725;
  23954. }
  23955. /* Test decode / encode of Curve25519 public key only */
  23956. if (ret == 0) {
  23957. idx = 0;
  23958. ret = wc_Curve25519PublicKeyDecode(kCurve25519PubDer, &idx, &key,
  23959. (word32)sizeof(kCurve25519PubDer));
  23960. }
  23961. if (ret == 0) {
  23962. outputSz = (word32)sizeof(output);
  23963. ret = wc_Curve25519PublicKeyToDer(&key, output, outputSz, 1);
  23964. if (ret >= 0) {
  23965. outputSz = ret;
  23966. ret = 0;
  23967. }
  23968. else {
  23969. ret = -10726;
  23970. }
  23971. }
  23972. if (ret == 0 && (outputSz != (word32)sizeof(kCurve25519PubDer) ||
  23973. XMEMCMP(output, kCurve25519PubDer, outputSz) != 0)) {
  23974. ret = -10727;
  23975. }
  23976. wc_curve25519_free(&key);
  23977. return ret;
  23978. }
  23979. #endif /* !NO_ASN && HAVE_CURVE25519_KEY_EXPORT && HAVE_CURVE25519_KEY_IMPORT */
  23980. WOLFSSL_TEST_SUBROUTINE int curve25519_test(void)
  23981. {
  23982. WC_RNG rng;
  23983. int ret;
  23984. #ifdef HAVE_CURVE25519_SHARED_SECRET
  23985. byte sharedA[32];
  23986. byte sharedB[32];
  23987. word32 y;
  23988. #endif
  23989. #ifdef HAVE_CURVE25519_KEY_EXPORT
  23990. byte exportBuf[32];
  23991. #endif
  23992. word32 x = 0;
  23993. curve25519_key userA, userB, pubKey;
  23994. #if defined(HAVE_CURVE25519_SHARED_SECRET) && \
  23995. defined(HAVE_CURVE25519_KEY_IMPORT)
  23996. /* test vectors from
  23997. https://tools.ietf.org/html/draft-josefsson-tls-curve25519-03
  23998. */
  23999. /* secret key for party a */
  24000. byte sa[] = {
  24001. 0x5A,0xC9,0x9F,0x33,0x63,0x2E,0x5A,0x76,
  24002. 0x8D,0xE7,0xE8,0x1B,0xF8,0x54,0xC2,0x7C,
  24003. 0x46,0xE3,0xFB,0xF2,0xAB,0xBA,0xCD,0x29,
  24004. 0xEC,0x4A,0xFF,0x51,0x73,0x69,0xC6,0x60
  24005. };
  24006. /* public key for party a */
  24007. byte pa[] = {
  24008. 0x05,0x7E,0x23,0xEA,0x9F,0x1C,0xBE,0x8A,
  24009. 0x27,0x16,0x8F,0x6E,0x69,0x6A,0x79,0x1D,
  24010. 0xE6,0x1D,0xD3,0xAF,0x7A,0xCD,0x4E,0xEA,
  24011. 0xCC,0x6E,0x7B,0xA5,0x14,0xFD,0xA8,0x63
  24012. };
  24013. /* secret key for party b */
  24014. byte sb[] = {
  24015. 0x47,0xDC,0x3D,0x21,0x41,0x74,0x82,0x0E,
  24016. 0x11,0x54,0xB4,0x9B,0xC6,0xCD,0xB2,0xAB,
  24017. 0xD4,0x5E,0xE9,0x58,0x17,0x05,0x5D,0x25,
  24018. 0x5A,0xA3,0x58,0x31,0xB7,0x0D,0x32,0x60
  24019. };
  24020. /* public key for party b */
  24021. byte pb[] = {
  24022. 0x6E,0xB8,0x9D,0xA9,0x19,0x89,0xAE,0x37,
  24023. 0xC7,0xEA,0xC7,0x61,0x8D,0x9E,0x5C,0x49,
  24024. 0x51,0xDB,0xA1,0xD7,0x3C,0x28,0x5A,0xE1,
  24025. 0xCD,0x26,0xA8,0x55,0x02,0x0E,0xEF,0x04
  24026. };
  24027. /* expected shared key */
  24028. byte ss[] = {
  24029. 0x61,0x45,0x0C,0xD9,0x8E,0x36,0x01,0x6B,
  24030. 0x58,0x77,0x6A,0x89,0x7A,0x9F,0x0A,0xEF,
  24031. 0x73,0x8B,0x99,0xF0,0x94,0x68,0xB8,0xD6,
  24032. 0xB8,0x51,0x11,0x84,0xD5,0x34,0x94,0xAB
  24033. };
  24034. #endif /* HAVE_CURVE25519_SHARED_SECRET */
  24035. (void)x;
  24036. #ifndef HAVE_FIPS
  24037. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  24038. #else
  24039. ret = wc_InitRng(&rng);
  24040. #endif
  24041. if (ret != 0)
  24042. return -10700;
  24043. wc_curve25519_init_ex(&userA, HEAP_HINT, devId);
  24044. wc_curve25519_init_ex(&userB, HEAP_HINT, devId);
  24045. wc_curve25519_init_ex(&pubKey, HEAP_HINT, devId);
  24046. /* make curve25519 keys */
  24047. if (wc_curve25519_make_key(&rng, 32, &userA) != 0)
  24048. return -10701;
  24049. if (wc_curve25519_make_key(&rng, 32, &userB) != 0)
  24050. return -10702;
  24051. #ifdef HAVE_CURVE25519_SHARED_SECRET
  24052. /* find shared secret key */
  24053. x = sizeof(sharedA);
  24054. if ((ret = wc_curve25519_shared_secret(&userA, &userB, sharedA, &x)) != 0) {
  24055. printf("wc_curve25519_shared_secret 1 %d\n", ret);
  24056. return -10703;
  24057. }
  24058. y = sizeof(sharedB);
  24059. if ((ret = wc_curve25519_shared_secret(&userB, &userA, sharedB, &y)) != 0) {
  24060. printf("wc_curve25519_shared_secret 2 %d\n", ret);
  24061. return -10704;
  24062. }
  24063. /* compare shared secret keys to test they are the same */
  24064. if (y != x)
  24065. return -10705;
  24066. if (XMEMCMP(sharedA, sharedB, x))
  24067. return -10706;
  24068. #endif
  24069. #ifdef HAVE_CURVE25519_KEY_EXPORT
  24070. /* export a public key and import it for another user */
  24071. x = sizeof(exportBuf);
  24072. if (wc_curve25519_export_public(&userA, exportBuf, &x) != 0)
  24073. return -10707;
  24074. #ifdef HAVE_CURVE25519_KEY_IMPORT
  24075. if (wc_curve25519_import_public(exportBuf, x, &pubKey) != 0)
  24076. return -10708;
  24077. #endif
  24078. #endif
  24079. #if defined(HAVE_CURVE25519_SHARED_SECRET) && \
  24080. defined(HAVE_CURVE25519_KEY_IMPORT)
  24081. /* test shared key after importing a public key */
  24082. XMEMSET(sharedB, 0, sizeof(sharedB));
  24083. y = sizeof(sharedB);
  24084. if (wc_curve25519_shared_secret(&userB, &pubKey, sharedB, &y) != 0)
  24085. return -10709;
  24086. if (XMEMCMP(sharedA, sharedB, y))
  24087. return -10710;
  24088. /* import RFC test vectors and compare shared key */
  24089. if (wc_curve25519_import_private_raw(sa, sizeof(sa), pa, sizeof(pa), &userA)
  24090. != 0)
  24091. return -10711;
  24092. if (wc_curve25519_import_private_raw(sb, sizeof(sb), pb, sizeof(pb), &userB)
  24093. != 0)
  24094. return -10712;
  24095. /* test against known test vector */
  24096. XMEMSET(sharedB, 0, sizeof(sharedB));
  24097. y = sizeof(sharedB);
  24098. if (wc_curve25519_shared_secret(&userA, &userB, sharedB, &y) != 0)
  24099. return -10713;
  24100. if (XMEMCMP(ss, sharedB, y))
  24101. return -10714;
  24102. /* test swapping roles of keys and generating same shared key */
  24103. XMEMSET(sharedB, 0, sizeof(sharedB));
  24104. y = sizeof(sharedB);
  24105. if (wc_curve25519_shared_secret(&userB, &userA, sharedB, &y) != 0)
  24106. return -10715;
  24107. if (XMEMCMP(ss, sharedB, y))
  24108. return -10716;
  24109. /* test with 1 generated key and 1 from known test vector */
  24110. if (wc_curve25519_import_private_raw(sa, sizeof(sa), pa, sizeof(pa), &userA)
  24111. != 0)
  24112. return -10717;
  24113. wc_curve25519_free(&userB);
  24114. wc_curve25519_init_ex(&userB, HEAP_HINT, devId);
  24115. if (wc_curve25519_make_key(&rng, 32, &userB) != 0)
  24116. return -10718;
  24117. x = sizeof(sharedA);
  24118. if (wc_curve25519_shared_secret(&userA, &userB, sharedA, &x) != 0)
  24119. return -10719;
  24120. y = sizeof(sharedB);
  24121. if (wc_curve25519_shared_secret(&userB, &userA, sharedB, &y) != 0)
  24122. return -10720;
  24123. /* compare shared secret keys to test they are the same */
  24124. if (y != x)
  24125. return -10721;
  24126. if (XMEMCMP(sharedA, sharedB, x))
  24127. return -10722;
  24128. ret = curve25519_overflow_test();
  24129. if (ret != 0)
  24130. return ret;
  24131. ret = curve25519_check_public_test();
  24132. if (ret != 0)
  24133. return ret;
  24134. #endif /* HAVE_CURVE25519_SHARED_SECRET && HAVE_CURVE25519_KEY_IMPORT */
  24135. #if !defined(NO_ASN) && defined(HAVE_CURVE25519_KEY_EXPORT) && \
  24136. defined(HAVE_CURVE25519_KEY_IMPORT)
  24137. ret = curve255519_der_test();
  24138. if (ret != 0)
  24139. return ret;
  24140. #endif
  24141. /* clean up keys when done */
  24142. wc_curve25519_free(&pubKey);
  24143. wc_curve25519_free(&userB);
  24144. wc_curve25519_free(&userA);
  24145. wc_FreeRng(&rng);
  24146. return 0;
  24147. }
  24148. #endif /* HAVE_CURVE25519 */
  24149. #ifdef HAVE_ED25519
  24150. #ifdef WOLFSSL_TEST_CERT
  24151. static int ed25519_test_cert(void)
  24152. {
  24153. DecodedCert cert[2];
  24154. DecodedCert* serverCert = NULL;
  24155. DecodedCert* caCert = NULL;
  24156. #ifdef HAVE_ED25519_VERIFY
  24157. ed25519_key key;
  24158. ed25519_key* pubKey = NULL;
  24159. int verify;
  24160. #endif /* HAVE_ED25519_VERIFY */
  24161. int ret;
  24162. byte* tmp;
  24163. size_t bytes;
  24164. XFILE file;
  24165. tmp = (byte *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24166. if (tmp == NULL) {
  24167. ERROR_OUT(-10730, done);
  24168. }
  24169. #ifdef USE_CERT_BUFFERS_256
  24170. XMEMCPY(tmp, ca_ed25519_cert, sizeof_ca_ed25519_cert);
  24171. bytes = sizeof_ca_ed25519_cert;
  24172. #elif !defined(NO_FILESYSTEM)
  24173. file = XFOPEN(caEd25519Cert, "rb");
  24174. if (file == NULL) {
  24175. ERROR_OUT(-10731, done);
  24176. }
  24177. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  24178. XFCLOSE(file);
  24179. #else
  24180. /* No certificate to use. */
  24181. ERROR_OUT(-10732, done);
  24182. #endif
  24183. InitDecodedCert(&cert[0], tmp, (word32)bytes, 0);
  24184. caCert = &cert[0];
  24185. ret = ParseCert(caCert, CERT_TYPE, NO_VERIFY, NULL);
  24186. if (ret != 0) {
  24187. ERROR_OUT(-10733, done);
  24188. }
  24189. #ifdef USE_CERT_BUFFERS_256
  24190. XMEMCPY(tmp, server_ed25519_cert, sizeof_server_ed25519_cert);
  24191. bytes = sizeof_server_ed25519_cert;
  24192. #elif !defined(NO_FILESYSTEM)
  24193. file = XFOPEN(serverEd25519Cert, "rb");
  24194. if (file == NULL) {
  24195. ERROR_OUT(-10734, done);
  24196. }
  24197. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  24198. XFCLOSE(file);
  24199. #else
  24200. /* No certificate to use. */
  24201. ERROR_OUT(-10735, done);
  24202. #endif
  24203. InitDecodedCert(&cert[1], tmp, (word32)bytes, 0);
  24204. serverCert = &cert[1];
  24205. ret = ParseCert(serverCert, CERT_TYPE, NO_VERIFY, NULL);
  24206. if (ret != 0) {
  24207. ERROR_OUT(-10736, done);
  24208. }
  24209. #ifdef HAVE_ED25519_VERIFY
  24210. ret = wc_ed25519_init(&key);
  24211. if (ret < 0) {
  24212. ERROR_OUT(-10737, done);
  24213. }
  24214. pubKey = &key;
  24215. ret = wc_ed25519_import_public(caCert->publicKey, caCert->pubKeySize,
  24216. pubKey);
  24217. if (ret < 0) {
  24218. ERROR_OUT(-10738, done);
  24219. }
  24220. if (wc_ed25519_verify_msg(serverCert->signature, serverCert->sigLength,
  24221. serverCert->source + serverCert->certBegin,
  24222. serverCert->sigIndex - serverCert->certBegin,
  24223. &verify, pubKey) < 0 || verify != 1) {
  24224. ERROR_OUT(-10739, done);
  24225. }
  24226. #endif /* HAVE_ED25519_VERIFY */
  24227. done:
  24228. if (tmp != NULL)
  24229. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24230. #ifdef HAVE_ED25519_VERIFY
  24231. wc_ed25519_free(pubKey);
  24232. #endif /* HAVE_ED25519_VERIFY */
  24233. if (caCert != NULL)
  24234. FreeDecodedCert(caCert);
  24235. if (serverCert != NULL)
  24236. FreeDecodedCert(serverCert);
  24237. return ret;
  24238. }
  24239. static int ed25519_test_make_cert(void)
  24240. {
  24241. WC_RNG rng;
  24242. Cert cert;
  24243. DecodedCert decode;
  24244. ed25519_key key;
  24245. ed25519_key* privKey = NULL;
  24246. int ret = 0;
  24247. byte* tmp = NULL;
  24248. wc_InitCert_ex(&cert, HEAP_HINT, devId);
  24249. #ifndef HAVE_FIPS
  24250. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  24251. #else
  24252. ret = wc_InitRng(&rng);
  24253. #endif
  24254. if (ret != 0)
  24255. return -10750;
  24256. wc_ed25519_init(&key);
  24257. privKey = &key;
  24258. wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, privKey);
  24259. cert.daysValid = 365 * 2;
  24260. cert.selfSigned = 1;
  24261. XMEMCPY(&cert.issuer, &certDefaultName, sizeof(CertName));
  24262. XMEMCPY(&cert.subject, &certDefaultName, sizeof(CertName));
  24263. cert.isCA = 0;
  24264. #ifdef WOLFSSL_CERT_EXT
  24265. ret = wc_SetKeyUsage(&cert, certKeyUsage);
  24266. if (ret < 0) {
  24267. ERROR_OUT(-10751, done);
  24268. }
  24269. ret = wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ED25519_TYPE, privKey);
  24270. if (ret < 0) {
  24271. ERROR_OUT(-10752, done);
  24272. }
  24273. ret = wc_SetAuthKeyIdFromPublicKey_ex(&cert, ED25519_TYPE, privKey);
  24274. if (ret < 0) {
  24275. ERROR_OUT(-10753, done);
  24276. }
  24277. #endif
  24278. tmp = (byte *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24279. if (tmp == NULL) {
  24280. ERROR_OUT(-10754, done);
  24281. }
  24282. cert.sigType = CTC_ED25519;
  24283. ret = wc_MakeCert_ex(&cert, tmp, FOURK_BUF, ED25519_TYPE, privKey, &rng);
  24284. if (ret < 0) {
  24285. ERROR_OUT(-10755, done);
  24286. }
  24287. ret = wc_SignCert_ex(cert.bodySz, cert.sigType, tmp, FOURK_BUF,
  24288. ED25519_TYPE, privKey, &rng);
  24289. if (ret < 0) {
  24290. ERROR_OUT(-10756, done);
  24291. }
  24292. InitDecodedCert(&decode, tmp, ret, HEAP_HINT);
  24293. ret = ParseCert(&decode, CERT_TYPE, NO_VERIFY, 0);
  24294. FreeDecodedCert(&decode);
  24295. if (ret != 0) {
  24296. ERROR_OUT(-10757, done);
  24297. }
  24298. done:
  24299. if (tmp != NULL)
  24300. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24301. wc_ed25519_free(privKey);
  24302. wc_FreeRng(&rng);
  24303. return ret;
  24304. }
  24305. #endif /* WOLFSSL_TEST_CERT */
  24306. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_EXPORT) && \
  24307. defined(HAVE_ED25519_KEY_IMPORT)
  24308. static int ed25519ctx_test(void)
  24309. {
  24310. int ret;
  24311. byte out[ED25519_SIG_SIZE];
  24312. word32 outlen;
  24313. #ifdef HAVE_ED25519_VERIFY
  24314. int verify = 0;
  24315. #endif /* HAVE_ED25519_VERIFY */
  24316. ed25519_key key;
  24317. WOLFSSL_SMALL_STACK_STATIC const byte sKeyCtx[] = {
  24318. 0x03,0x05,0x33,0x4e,0x38,0x1a,0xf7,0x8f,
  24319. 0x14,0x1c,0xb6,0x66,0xf6,0x19,0x9f,0x57,
  24320. 0xbc,0x34,0x95,0x33,0x5a,0x25,0x6a,0x95,
  24321. 0xbd,0x2a,0x55,0xbf,0x54,0x66,0x63,0xf6
  24322. };
  24323. WOLFSSL_SMALL_STACK_STATIC const byte pKeyCtx[] = {
  24324. 0xdf,0xc9,0x42,0x5e,0x4f,0x96,0x8f,0x7f,
  24325. 0x0c,0x29,0xf0,0x25,0x9c,0xf5,0xf9,0xae,
  24326. 0xd6,0x85,0x1c,0x2b,0xb4,0xad,0x8b,0xfb,
  24327. 0x86,0x0c,0xfe,0xe0,0xab,0x24,0x82,0x92
  24328. };
  24329. WOLFSSL_SMALL_STACK_STATIC const byte sigCtx1[] = {
  24330. 0x55,0xa4,0xcc,0x2f,0x70,0xa5,0x4e,0x04,
  24331. 0x28,0x8c,0x5f,0x4c,0xd1,0xe4,0x5a,0x7b,
  24332. 0xb5,0x20,0xb3,0x62,0x92,0x91,0x18,0x76,
  24333. 0xca,0xda,0x73,0x23,0x19,0x8d,0xd8,0x7a,
  24334. 0x8b,0x36,0x95,0x0b,0x95,0x13,0x00,0x22,
  24335. 0x90,0x7a,0x7f,0xb7,0xc4,0xe9,0xb2,0xd5,
  24336. 0xf6,0xcc,0xa6,0x85,0xa5,0x87,0xb4,0xb2,
  24337. 0x1f,0x4b,0x88,0x8e,0x4e,0x7e,0xdb,0x0d
  24338. };
  24339. WOLFSSL_SMALL_STACK_STATIC const byte sigCtx2[] = {
  24340. 0xcc,0x5e,0x63,0xa2,0x7e,0x94,0xaf,0xd3,
  24341. 0x41,0x83,0x38,0xd2,0x48,0x6f,0xa9,0x2a,
  24342. 0xf9,0x91,0x7c,0x2d,0x98,0x9e,0x06,0xe5,
  24343. 0x02,0x77,0x72,0x1c,0x34,0x38,0x18,0xb4,
  24344. 0x21,0x96,0xbc,0x29,0x2e,0x68,0xf3,0x4d,
  24345. 0x85,0x9b,0xbe,0xad,0x17,0x9f,0x54,0x54,
  24346. 0x2d,0x4b,0x04,0xdc,0xfb,0xfa,0x4a,0x68,
  24347. 0x4e,0x39,0x50,0xfb,0x1c,0xcd,0x8d,0x0d
  24348. };
  24349. WOLFSSL_SMALL_STACK_STATIC const byte msgCtx[] = {
  24350. 0xf7,0x26,0x93,0x6d,0x19,0xc8,0x00,0x49,
  24351. 0x4e,0x3f,0xda,0xff,0x20,0xb2,0x76,0xa8
  24352. };
  24353. WOLFSSL_SMALL_STACK_STATIC const byte contextCtx[] = {
  24354. 0x66,0x6f,0x6f
  24355. };
  24356. outlen = sizeof(out);
  24357. XMEMSET(out, 0, sizeof(out));
  24358. ret = wc_ed25519_init_ex(&key, HEAP_HINT, devId);
  24359. if (ret != 0)
  24360. return 10800;
  24361. ret = wc_ed25519_import_private_key(sKeyCtx, ED25519_KEY_SIZE, pKeyCtx,
  24362. sizeof(pKeyCtx), &key);
  24363. if (ret == 0)
  24364. ret = wc_ed25519ctx_sign_msg(msgCtx, sizeof(msgCtx), out, &outlen, &key,
  24365. contextCtx, sizeof(contextCtx));
  24366. if (ret == 0 && XMEMCMP(out, sigCtx1, 64) != 0)
  24367. ret = -10801;
  24368. #if defined(HAVE_ED25519_VERIFY)
  24369. /* test verify on good msg */
  24370. if (ret == 0)
  24371. ret = wc_ed25519ctx_verify_msg(out, outlen, msgCtx, sizeof(msgCtx),
  24372. &verify, &key, contextCtx, sizeof(contextCtx));
  24373. if (ret == 0 && verify != 1)
  24374. ret = -10802;
  24375. #endif
  24376. if (ret == 0)
  24377. ret = wc_ed25519ctx_sign_msg(msgCtx, sizeof(msgCtx), out, &outlen, &key,
  24378. NULL, 0);
  24379. if (ret == 0 && XMEMCMP(out, sigCtx2, 64) != 0)
  24380. ret = -10803;
  24381. #if defined(HAVE_ED25519_VERIFY)
  24382. /* test verify on good msg */
  24383. if (ret == 0)
  24384. ret = wc_ed25519ctx_verify_msg(out, outlen, msgCtx, sizeof(msgCtx),
  24385. &verify, &key, NULL, 0);
  24386. if (ret == 0 && verify != 1)
  24387. ret = -10804;
  24388. #endif
  24389. wc_ed25519_free(&key);
  24390. return ret;
  24391. }
  24392. static int ed25519ph_test(void)
  24393. {
  24394. int ret = 0;
  24395. byte out[ED25519_SIG_SIZE];
  24396. word32 outlen;
  24397. #ifdef HAVE_ED25519_VERIFY
  24398. int verify = 0;
  24399. #endif /* HAVE_ED25519_VERIFY */
  24400. ed25519_key key;
  24401. WOLFSSL_SMALL_STACK_STATIC const byte sKeyPh[] = {
  24402. 0x83,0x3f,0xe6,0x24,0x09,0x23,0x7b,0x9d,
  24403. 0x62,0xec,0x77,0x58,0x75,0x20,0x91,0x1e,
  24404. 0x9a,0x75,0x9c,0xec,0x1d,0x19,0x75,0x5b,
  24405. 0x7d,0xa9,0x01,0xb9,0x6d,0xca,0x3d,0x42
  24406. };
  24407. WOLFSSL_SMALL_STACK_STATIC const byte pKeyPh[] = {
  24408. 0xec,0x17,0x2b,0x93,0xad,0x5e,0x56,0x3b,
  24409. 0xf4,0x93,0x2c,0x70,0xe1,0x24,0x50,0x34,
  24410. 0xc3,0x54,0x67,0xef,0x2e,0xfd,0x4d,0x64,
  24411. 0xeb,0xf8,0x19,0x68,0x34,0x67,0xe2,0xbf
  24412. };
  24413. WOLFSSL_SMALL_STACK_STATIC const byte sigPh1[] = {
  24414. 0x98,0xa7,0x02,0x22,0xf0,0xb8,0x12,0x1a,
  24415. 0xa9,0xd3,0x0f,0x81,0x3d,0x68,0x3f,0x80,
  24416. 0x9e,0x46,0x2b,0x46,0x9c,0x7f,0xf8,0x76,
  24417. 0x39,0x49,0x9b,0xb9,0x4e,0x6d,0xae,0x41,
  24418. 0x31,0xf8,0x50,0x42,0x46,0x3c,0x2a,0x35,
  24419. 0x5a,0x20,0x03,0xd0,0x62,0xad,0xf5,0xaa,
  24420. 0xa1,0x0b,0x8c,0x61,0xe6,0x36,0x06,0x2a,
  24421. 0xaa,0xd1,0x1c,0x2a,0x26,0x08,0x34,0x06
  24422. };
  24423. WOLFSSL_SMALL_STACK_STATIC const byte sigPh2[] = {
  24424. 0xe0,0x39,0x70,0x2b,0x4c,0x25,0x95,0xa6,
  24425. 0xa5,0x41,0xac,0x85,0x09,0x23,0x6e,0x29,
  24426. 0x90,0x47,0x47,0x95,0x33,0x0c,0x9b,0x34,
  24427. 0xa7,0x5f,0x58,0xa6,0x60,0x12,0x9e,0x08,
  24428. 0xfd,0x73,0x69,0x43,0xfb,0x19,0x43,0xa5,
  24429. 0x57,0x20,0xb9,0xe0,0x95,0x7b,0x1e,0xd6,
  24430. 0x73,0x48,0x16,0x61,0x9f,0x13,0x88,0xf4,
  24431. 0x3f,0x73,0xe6,0xe3,0xba,0xa8,0x1c,0x0e
  24432. };
  24433. WOLFSSL_SMALL_STACK_STATIC const byte msgPh[] = {
  24434. 0x61,0x62,0x63
  24435. };
  24436. /* SHA-512 hash of msgPh */
  24437. WOLFSSL_SMALL_STACK_STATIC const byte hashPh[] = {
  24438. 0xdd,0xaf,0x35,0xa1,0x93,0x61,0x7a,0xba,
  24439. 0xcc,0x41,0x73,0x49,0xae,0x20,0x41,0x31,
  24440. 0x12,0xe6,0xfa,0x4e,0x89,0xa9,0x7e,0xa2,
  24441. 0x0a,0x9e,0xee,0xe6,0x4b,0x55,0xd3,0x9a,
  24442. 0x21,0x92,0x99,0x2a,0x27,0x4f,0xc1,0xa8,
  24443. 0x36,0xba,0x3c,0x23,0xa3,0xfe,0xeb,0xbd,
  24444. 0x45,0x4d,0x44,0x23,0x64,0x3c,0xe8,0x0e,
  24445. 0x2a,0x9a,0xc9,0x4f,0xa5,0x4c,0xa4,0x9f
  24446. };
  24447. WOLFSSL_SMALL_STACK_STATIC const byte contextPh2[] = {
  24448. 0x66,0x6f,0x6f
  24449. };
  24450. outlen = sizeof(out);
  24451. XMEMSET(out, 0, sizeof(out));
  24452. ret = wc_ed25519_init_ex(&key, HEAP_HINT, devId);
  24453. if (ret != 0)
  24454. return -10900;
  24455. ret = wc_ed25519_import_private_key(sKeyPh, ED25519_KEY_SIZE, pKeyPh,
  24456. sizeof(pKeyPh), &key);
  24457. if (ret == 0)
  24458. ret = wc_ed25519ph_sign_msg(msgPh, sizeof(msgPh), out, &outlen, &key,
  24459. NULL, 0);
  24460. if (ret == 0 && XMEMCMP(out, sigPh1, 64) != 0)
  24461. ret = -10901;
  24462. #if defined(HAVE_ED25519_VERIFY)
  24463. /* test verify on good msg */
  24464. if (ret == 0)
  24465. ret = wc_ed25519ph_verify_msg(out, outlen, msgPh, sizeof(msgPh),
  24466. &verify, &key, NULL, 0);
  24467. if (ret == 0 && verify != 1)
  24468. ret = -10902;
  24469. #endif
  24470. if (ret == 0)
  24471. ret = wc_ed25519ph_sign_msg(msgPh, sizeof(msgPh), out, &outlen, &key,
  24472. contextPh2, sizeof(contextPh2));
  24473. if (ret == 0 && XMEMCMP(out, sigPh2, 64) != 0)
  24474. ret = -10903;
  24475. #if defined(HAVE_ED25519_VERIFY)
  24476. /* test verify on good msg */
  24477. if (ret == 0)
  24478. ret = wc_ed25519ph_verify_msg(out, outlen, msgPh, sizeof(msgPh), &verify,
  24479. &key, contextPh2, sizeof(contextPh2));
  24480. if (ret == 0 && verify != 1)
  24481. ret = -10904;
  24482. #endif
  24483. if (ret == 0)
  24484. ret = wc_ed25519ph_sign_hash(hashPh, sizeof(hashPh), out, &outlen, &key,
  24485. NULL, 0);
  24486. if (ret == 0 && XMEMCMP(out, sigPh1, 64) != 0)
  24487. ret = -10905;
  24488. #if defined(HAVE_ED25519_VERIFY)
  24489. if (ret == 0)
  24490. ret = wc_ed25519ph_verify_hash(out, outlen, hashPh, sizeof(hashPh),
  24491. &verify, &key, NULL, 0);
  24492. if (ret == 0 && verify != 1)
  24493. ret = -10906;
  24494. #endif
  24495. if (ret == 0)
  24496. ret = wc_ed25519ph_sign_hash(hashPh, sizeof(hashPh), out, &outlen, &key,
  24497. contextPh2, sizeof(contextPh2));
  24498. if (ret == 0 && XMEMCMP(out, sigPh2, 64) != 0)
  24499. ret = -10907;
  24500. #if defined(HAVE_ED25519_VERIFY)
  24501. if (ret == 0)
  24502. ret = wc_ed25519ph_verify_hash(out, outlen, hashPh, sizeof(hashPh), &verify,
  24503. &key, contextPh2, sizeof(contextPh2));
  24504. if (ret == 0 && verify != 1)
  24505. ret = -10908;
  24506. #endif
  24507. wc_ed25519_free(&key);
  24508. return ret;
  24509. }
  24510. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_EXPORT && HAVE_ED25519_KEY_IMPORT */
  24511. WOLFSSL_TEST_SUBROUTINE int ed25519_test(void)
  24512. {
  24513. int ret;
  24514. WC_RNG rng;
  24515. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_EXPORT) &&\
  24516. defined(HAVE_ED25519_KEY_IMPORT)
  24517. byte out[ED25519_SIG_SIZE];
  24518. byte exportPKey[ED25519_KEY_SIZE];
  24519. byte exportSKey[ED25519_KEY_SIZE];
  24520. word32 exportPSz;
  24521. word32 exportSSz;
  24522. int i;
  24523. word32 outlen;
  24524. #ifdef HAVE_ED25519_VERIFY
  24525. #ifdef WOLFSSL_ED25519_STREAMING_VERIFY
  24526. int j;
  24527. #endif
  24528. int verify;
  24529. #endif /* HAVE_ED25519_VERIFY */
  24530. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_EXPORT && HAVE_ED25519_KEY_IMPORT */
  24531. word32 keySz, sigSz;
  24532. ed25519_key key;
  24533. ed25519_key key2;
  24534. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_EXPORT) && \
  24535. defined(HAVE_ED25519_KEY_IMPORT)
  24536. /* test vectors from
  24537. https://tools.ietf.org/html/draft-josefsson-eddsa-ed25519-02
  24538. */
  24539. WOLFSSL_SMALL_STACK_STATIC const byte sKey1[] = {
  24540. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  24541. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  24542. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  24543. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60
  24544. };
  24545. WOLFSSL_SMALL_STACK_STATIC const byte sKey2[] = {
  24546. 0x4c,0xcd,0x08,0x9b,0x28,0xff,0x96,0xda,
  24547. 0x9d,0xb6,0xc3,0x46,0xec,0x11,0x4e,0x0f,
  24548. 0x5b,0x8a,0x31,0x9f,0x35,0xab,0xa6,0x24,
  24549. 0xda,0x8c,0xf6,0xed,0x4f,0xb8,0xa6,0xfb
  24550. };
  24551. WOLFSSL_SMALL_STACK_STATIC const byte sKey3[] = {
  24552. 0xc5,0xaa,0x8d,0xf4,0x3f,0x9f,0x83,0x7b,
  24553. 0xed,0xb7,0x44,0x2f,0x31,0xdc,0xb7,0xb1,
  24554. 0x66,0xd3,0x85,0x35,0x07,0x6f,0x09,0x4b,
  24555. 0x85,0xce,0x3a,0x2e,0x0b,0x44,0x58,0xf7
  24556. };
  24557. /* uncompressed test */
  24558. WOLFSSL_SMALL_STACK_STATIC const byte sKey4[] = {
  24559. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  24560. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  24561. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  24562. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60
  24563. };
  24564. /* compressed prefix test */
  24565. WOLFSSL_SMALL_STACK_STATIC const byte sKey5[] = {
  24566. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  24567. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  24568. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  24569. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60
  24570. };
  24571. WOLFSSL_SMALL_STACK_STATIC const byte sKey6[] = {
  24572. 0xf5,0xe5,0x76,0x7c,0xf1,0x53,0x31,0x95,
  24573. 0x17,0x63,0x0f,0x22,0x68,0x76,0xb8,0x6c,
  24574. 0x81,0x60,0xcc,0x58,0x3b,0xc0,0x13,0x74,
  24575. 0x4c,0x6b,0xf2,0x55,0xf5,0xcc,0x0e,0xe5
  24576. };
  24577. WOLFSSL_SMALL_STACK_STATIC const byte* sKeys[] = {sKey1, sKey2, sKey3, sKey4, sKey5, sKey6};
  24578. WOLFSSL_SMALL_STACK_STATIC const byte pKey1[] = {
  24579. 0xd7,0x5a,0x98,0x01,0x82,0xb1,0x0a,0xb7,
  24580. 0xd5,0x4b,0xfe,0xd3,0xc9,0x64,0x07,0x3a,
  24581. 0x0e,0xe1,0x72,0xf3,0xda,0xa6,0x23,0x25,
  24582. 0xaf,0x02,0x1a,0x68,0xf7,0x07,0x51,0x1a
  24583. };
  24584. WOLFSSL_SMALL_STACK_STATIC const byte pKey2[] = {
  24585. 0x3d,0x40,0x17,0xc3,0xe8,0x43,0x89,0x5a,
  24586. 0x92,0xb7,0x0a,0xa7,0x4d,0x1b,0x7e,0xbc,
  24587. 0x9c,0x98,0x2c,0xcf,0x2e,0xc4,0x96,0x8c,
  24588. 0xc0,0xcd,0x55,0xf1,0x2a,0xf4,0x66,0x0c
  24589. };
  24590. WOLFSSL_SMALL_STACK_STATIC const byte pKey3[] = {
  24591. 0xfc,0x51,0xcd,0x8e,0x62,0x18,0xa1,0xa3,
  24592. 0x8d,0xa4,0x7e,0xd0,0x02,0x30,0xf0,0x58,
  24593. 0x08,0x16,0xed,0x13,0xba,0x33,0x03,0xac,
  24594. 0x5d,0xeb,0x91,0x15,0x48,0x90,0x80,0x25
  24595. };
  24596. /* uncompressed test */
  24597. WOLFSSL_SMALL_STACK_STATIC const byte pKey4[] = {
  24598. 0x04,0x55,0xd0,0xe0,0x9a,0x2b,0x9d,0x34,
  24599. 0x29,0x22,0x97,0xe0,0x8d,0x60,0xd0,0xf6,
  24600. 0x20,0xc5,0x13,0xd4,0x72,0x53,0x18,0x7c,
  24601. 0x24,0xb1,0x27,0x86,0xbd,0x77,0x76,0x45,
  24602. 0xce,0x1a,0x51,0x07,0xf7,0x68,0x1a,0x02,
  24603. 0xaf,0x25,0x23,0xa6,0xda,0xf3,0x72,0xe1,
  24604. 0x0e,0x3a,0x07,0x64,0xc9,0xd3,0xfe,0x4b,
  24605. 0xd5,0xb7,0x0a,0xb1,0x82,0x01,0x98,0x5a,
  24606. 0xd7
  24607. };
  24608. /* compressed prefix */
  24609. WOLFSSL_SMALL_STACK_STATIC const byte pKey5[] = {
  24610. 0x40,0xd7,0x5a,0x98,0x01,0x82,0xb1,0x0a,0xb7,
  24611. 0xd5,0x4b,0xfe,0xd3,0xc9,0x64,0x07,0x3a,
  24612. 0x0e,0xe1,0x72,0xf3,0xda,0xa6,0x23,0x25,
  24613. 0xaf,0x02,0x1a,0x68,0xf7,0x07,0x51,0x1a
  24614. };
  24615. WOLFSSL_SMALL_STACK_STATIC const byte pKey6[] = {
  24616. 0x27,0x81,0x17,0xfc,0x14,0x4c,0x72,0x34,
  24617. 0x0f,0x67,0xd0,0xf2,0x31,0x6e,0x83,0x86,
  24618. 0xce,0xff,0xbf,0x2b,0x24,0x28,0xc9,0xc5,
  24619. 0x1f,0xef,0x7c,0x59,0x7f,0x1d,0x42,0x6e
  24620. };
  24621. WOLFSSL_SMALL_STACK_STATIC const byte* pKeys[] = {pKey1, pKey2, pKey3, pKey4, pKey5, pKey6};
  24622. WOLFSSL_SMALL_STACK_STATIC const byte pKeySz[] = {sizeof(pKey1), sizeof(pKey2), sizeof(pKey3),
  24623. sizeof(pKey4), sizeof(pKey5), sizeof(pKey6)};
  24624. WOLFSSL_SMALL_STACK_STATIC const byte sig1[] = {
  24625. 0xe5,0x56,0x43,0x00,0xc3,0x60,0xac,0x72,
  24626. 0x90,0x86,0xe2,0xcc,0x80,0x6e,0x82,0x8a,
  24627. 0x84,0x87,0x7f,0x1e,0xb8,0xe5,0xd9,0x74,
  24628. 0xd8,0x73,0xe0,0x65,0x22,0x49,0x01,0x55,
  24629. 0x5f,0xb8,0x82,0x15,0x90,0xa3,0x3b,0xac,
  24630. 0xc6,0x1e,0x39,0x70,0x1c,0xf9,0xb4,0x6b,
  24631. 0xd2,0x5b,0xf5,0xf0,0x59,0x5b,0xbe,0x24,
  24632. 0x65,0x51,0x41,0x43,0x8e,0x7a,0x10,0x0b
  24633. };
  24634. WOLFSSL_SMALL_STACK_STATIC const byte sig2[] = {
  24635. 0x92,0xa0,0x09,0xa9,0xf0,0xd4,0xca,0xb8,
  24636. 0x72,0x0e,0x82,0x0b,0x5f,0x64,0x25,0x40,
  24637. 0xa2,0xb2,0x7b,0x54,0x16,0x50,0x3f,0x8f,
  24638. 0xb3,0x76,0x22,0x23,0xeb,0xdb,0x69,0xda,
  24639. 0x08,0x5a,0xc1,0xe4,0x3e,0x15,0x99,0x6e,
  24640. 0x45,0x8f,0x36,0x13,0xd0,0xf1,0x1d,0x8c,
  24641. 0x38,0x7b,0x2e,0xae,0xb4,0x30,0x2a,0xee,
  24642. 0xb0,0x0d,0x29,0x16,0x12,0xbb,0x0c,0x00
  24643. };
  24644. WOLFSSL_SMALL_STACK_STATIC const byte sig3[] = {
  24645. 0x62,0x91,0xd6,0x57,0xde,0xec,0x24,0x02,
  24646. 0x48,0x27,0xe6,0x9c,0x3a,0xbe,0x01,0xa3,
  24647. 0x0c,0xe5,0x48,0xa2,0x84,0x74,0x3a,0x44,
  24648. 0x5e,0x36,0x80,0xd7,0xdb,0x5a,0xc3,0xac,
  24649. 0x18,0xff,0x9b,0x53,0x8d,0x16,0xf2,0x90,
  24650. 0xae,0x67,0xf7,0x60,0x98,0x4d,0xc6,0x59,
  24651. 0x4a,0x7c,0x15,0xe9,0x71,0x6e,0xd2,0x8d,
  24652. 0xc0,0x27,0xbe,0xce,0xea,0x1e,0xc4,0x0a
  24653. };
  24654. /* uncompressed test */
  24655. WOLFSSL_SMALL_STACK_STATIC const byte sig4[] = {
  24656. 0xe5,0x56,0x43,0x00,0xc3,0x60,0xac,0x72,
  24657. 0x90,0x86,0xe2,0xcc,0x80,0x6e,0x82,0x8a,
  24658. 0x84,0x87,0x7f,0x1e,0xb8,0xe5,0xd9,0x74,
  24659. 0xd8,0x73,0xe0,0x65,0x22,0x49,0x01,0x55,
  24660. 0x5f,0xb8,0x82,0x15,0x90,0xa3,0x3b,0xac,
  24661. 0xc6,0x1e,0x39,0x70,0x1c,0xf9,0xb4,0x6b,
  24662. 0xd2,0x5b,0xf5,0xf0,0x59,0x5b,0xbe,0x24,
  24663. 0x65,0x51,0x41,0x43,0x8e,0x7a,0x10,0x0b
  24664. };
  24665. /* compressed prefix */
  24666. WOLFSSL_SMALL_STACK_STATIC const byte sig5[] = {
  24667. 0xe5,0x56,0x43,0x00,0xc3,0x60,0xac,0x72,
  24668. 0x90,0x86,0xe2,0xcc,0x80,0x6e,0x82,0x8a,
  24669. 0x84,0x87,0x7f,0x1e,0xb8,0xe5,0xd9,0x74,
  24670. 0xd8,0x73,0xe0,0x65,0x22,0x49,0x01,0x55,
  24671. 0x5f,0xb8,0x82,0x15,0x90,0xa3,0x3b,0xac,
  24672. 0xc6,0x1e,0x39,0x70,0x1c,0xf9,0xb4,0x6b,
  24673. 0xd2,0x5b,0xf5,0xf0,0x59,0x5b,0xbe,0x24,
  24674. 0x65,0x51,0x41,0x43,0x8e,0x7a,0x10,0x0b
  24675. };
  24676. WOLFSSL_SMALL_STACK_STATIC const byte sig6[] = {
  24677. 0x0a,0xab,0x4c,0x90,0x05,0x01,0xb3,0xe2,
  24678. 0x4d,0x7c,0xdf,0x46,0x63,0x32,0x6a,0x3a,
  24679. 0x87,0xdf,0x5e,0x48,0x43,0xb2,0xcb,0xdb,
  24680. 0x67,0xcb,0xf6,0xe4,0x60,0xfe,0xc3,0x50,
  24681. 0xaa,0x53,0x71,0xb1,0x50,0x8f,0x9f,0x45,
  24682. 0x28,0xec,0xea,0x23,0xc4,0x36,0xd9,0x4b,
  24683. 0x5e,0x8f,0xcd,0x4f,0x68,0x1e,0x30,0xa6,
  24684. 0xac,0x00,0xa9,0x70,0x4a,0x18,0x8a,0x03
  24685. };
  24686. WOLFSSL_SMALL_STACK_STATIC const byte* sigs[] = {sig1, sig2, sig3, sig4, sig5, sig6};
  24687. WOLFSSL_SMALL_STACK_STATIC const byte msg1[] = {0x0 };
  24688. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] = {0x72};
  24689. WOLFSSL_SMALL_STACK_STATIC const byte msg3[] = {0xAF,0x82};
  24690. /* test of a 1024 byte long message */
  24691. WOLFSSL_SMALL_STACK_STATIC const byte msg4[] = {
  24692. 0x08,0xb8,0xb2,0xb7,0x33,0x42,0x42,0x43,
  24693. 0x76,0x0f,0xe4,0x26,0xa4,0xb5,0x49,0x08,
  24694. 0x63,0x21,0x10,0xa6,0x6c,0x2f,0x65,0x91,
  24695. 0xea,0xbd,0x33,0x45,0xe3,0xe4,0xeb,0x98,
  24696. 0xfa,0x6e,0x26,0x4b,0xf0,0x9e,0xfe,0x12,
  24697. 0xee,0x50,0xf8,0xf5,0x4e,0x9f,0x77,0xb1,
  24698. 0xe3,0x55,0xf6,0xc5,0x05,0x44,0xe2,0x3f,
  24699. 0xb1,0x43,0x3d,0xdf,0x73,0xbe,0x84,0xd8,
  24700. 0x79,0xde,0x7c,0x00,0x46,0xdc,0x49,0x96,
  24701. 0xd9,0xe7,0x73,0xf4,0xbc,0x9e,0xfe,0x57,
  24702. 0x38,0x82,0x9a,0xdb,0x26,0xc8,0x1b,0x37,
  24703. 0xc9,0x3a,0x1b,0x27,0x0b,0x20,0x32,0x9d,
  24704. 0x65,0x86,0x75,0xfc,0x6e,0xa5,0x34,0xe0,
  24705. 0x81,0x0a,0x44,0x32,0x82,0x6b,0xf5,0x8c,
  24706. 0x94,0x1e,0xfb,0x65,0xd5,0x7a,0x33,0x8b,
  24707. 0xbd,0x2e,0x26,0x64,0x0f,0x89,0xff,0xbc,
  24708. 0x1a,0x85,0x8e,0xfc,0xb8,0x55,0x0e,0xe3,
  24709. 0xa5,0xe1,0x99,0x8b,0xd1,0x77,0xe9,0x3a,
  24710. 0x73,0x63,0xc3,0x44,0xfe,0x6b,0x19,0x9e,
  24711. 0xe5,0xd0,0x2e,0x82,0xd5,0x22,0xc4,0xfe,
  24712. 0xba,0x15,0x45,0x2f,0x80,0x28,0x8a,0x82,
  24713. 0x1a,0x57,0x91,0x16,0xec,0x6d,0xad,0x2b,
  24714. 0x3b,0x31,0x0d,0xa9,0x03,0x40,0x1a,0xa6,
  24715. 0x21,0x00,0xab,0x5d,0x1a,0x36,0x55,0x3e,
  24716. 0x06,0x20,0x3b,0x33,0x89,0x0c,0xc9,0xb8,
  24717. 0x32,0xf7,0x9e,0xf8,0x05,0x60,0xcc,0xb9,
  24718. 0xa3,0x9c,0xe7,0x67,0x96,0x7e,0xd6,0x28,
  24719. 0xc6,0xad,0x57,0x3c,0xb1,0x16,0xdb,0xef,
  24720. 0xef,0xd7,0x54,0x99,0xda,0x96,0xbd,0x68,
  24721. 0xa8,0xa9,0x7b,0x92,0x8a,0x8b,0xbc,0x10,
  24722. 0x3b,0x66,0x21,0xfc,0xde,0x2b,0xec,0xa1,
  24723. 0x23,0x1d,0x20,0x6b,0xe6,0xcd,0x9e,0xc7,
  24724. 0xaf,0xf6,0xf6,0xc9,0x4f,0xcd,0x72,0x04,
  24725. 0xed,0x34,0x55,0xc6,0x8c,0x83,0xf4,0xa4,
  24726. 0x1d,0xa4,0xaf,0x2b,0x74,0xef,0x5c,0x53,
  24727. 0xf1,0xd8,0xac,0x70,0xbd,0xcb,0x7e,0xd1,
  24728. 0x85,0xce,0x81,0xbd,0x84,0x35,0x9d,0x44,
  24729. 0x25,0x4d,0x95,0x62,0x9e,0x98,0x55,0xa9,
  24730. 0x4a,0x7c,0x19,0x58,0xd1,0xf8,0xad,0xa5,
  24731. 0xd0,0x53,0x2e,0xd8,0xa5,0xaa,0x3f,0xb2,
  24732. 0xd1,0x7b,0xa7,0x0e,0xb6,0x24,0x8e,0x59,
  24733. 0x4e,0x1a,0x22,0x97,0xac,0xbb,0xb3,0x9d,
  24734. 0x50,0x2f,0x1a,0x8c,0x6e,0xb6,0xf1,0xce,
  24735. 0x22,0xb3,0xde,0x1a,0x1f,0x40,0xcc,0x24,
  24736. 0x55,0x41,0x19,0xa8,0x31,0xa9,0xaa,0xd6,
  24737. 0x07,0x9c,0xad,0x88,0x42,0x5d,0xe6,0xbd,
  24738. 0xe1,0xa9,0x18,0x7e,0xbb,0x60,0x92,0xcf,
  24739. 0x67,0xbf,0x2b,0x13,0xfd,0x65,0xf2,0x70,
  24740. 0x88,0xd7,0x8b,0x7e,0x88,0x3c,0x87,0x59,
  24741. 0xd2,0xc4,0xf5,0xc6,0x5a,0xdb,0x75,0x53,
  24742. 0x87,0x8a,0xd5,0x75,0xf9,0xfa,0xd8,0x78,
  24743. 0xe8,0x0a,0x0c,0x9b,0xa6,0x3b,0xcb,0xcc,
  24744. 0x27,0x32,0xe6,0x94,0x85,0xbb,0xc9,0xc9,
  24745. 0x0b,0xfb,0xd6,0x24,0x81,0xd9,0x08,0x9b,
  24746. 0xec,0xcf,0x80,0xcf,0xe2,0xdf,0x16,0xa2,
  24747. 0xcf,0x65,0xbd,0x92,0xdd,0x59,0x7b,0x07,
  24748. 0x07,0xe0,0x91,0x7a,0xf4,0x8b,0xbb,0x75,
  24749. 0xfe,0xd4,0x13,0xd2,0x38,0xf5,0x55,0x5a,
  24750. 0x7a,0x56,0x9d,0x80,0xc3,0x41,0x4a,0x8d,
  24751. 0x08,0x59,0xdc,0x65,0xa4,0x61,0x28,0xba,
  24752. 0xb2,0x7a,0xf8,0x7a,0x71,0x31,0x4f,0x31,
  24753. 0x8c,0x78,0x2b,0x23,0xeb,0xfe,0x80,0x8b,
  24754. 0x82,0xb0,0xce,0x26,0x40,0x1d,0x2e,0x22,
  24755. 0xf0,0x4d,0x83,0xd1,0x25,0x5d,0xc5,0x1a,
  24756. 0xdd,0xd3,0xb7,0x5a,0x2b,0x1a,0xe0,0x78,
  24757. 0x45,0x04,0xdf,0x54,0x3a,0xf8,0x96,0x9b,
  24758. 0xe3,0xea,0x70,0x82,0xff,0x7f,0xc9,0x88,
  24759. 0x8c,0x14,0x4d,0xa2,0xaf,0x58,0x42,0x9e,
  24760. 0xc9,0x60,0x31,0xdb,0xca,0xd3,0xda,0xd9,
  24761. 0xaf,0x0d,0xcb,0xaa,0xaf,0x26,0x8c,0xb8,
  24762. 0xfc,0xff,0xea,0xd9,0x4f,0x3c,0x7c,0xa4,
  24763. 0x95,0xe0,0x56,0xa9,0xb4,0x7a,0xcd,0xb7,
  24764. 0x51,0xfb,0x73,0xe6,0x66,0xc6,0xc6,0x55,
  24765. 0xad,0xe8,0x29,0x72,0x97,0xd0,0x7a,0xd1,
  24766. 0xba,0x5e,0x43,0xf1,0xbc,0xa3,0x23,0x01,
  24767. 0x65,0x13,0x39,0xe2,0x29,0x04,0xcc,0x8c,
  24768. 0x42,0xf5,0x8c,0x30,0xc0,0x4a,0xaf,0xdb,
  24769. 0x03,0x8d,0xda,0x08,0x47,0xdd,0x98,0x8d,
  24770. 0xcd,0xa6,0xf3,0xbf,0xd1,0x5c,0x4b,0x4c,
  24771. 0x45,0x25,0x00,0x4a,0xa0,0x6e,0xef,0xf8,
  24772. 0xca,0x61,0x78,0x3a,0xac,0xec,0x57,0xfb,
  24773. 0x3d,0x1f,0x92,0xb0,0xfe,0x2f,0xd1,0xa8,
  24774. 0x5f,0x67,0x24,0x51,0x7b,0x65,0xe6,0x14,
  24775. 0xad,0x68,0x08,0xd6,0xf6,0xee,0x34,0xdf,
  24776. 0xf7,0x31,0x0f,0xdc,0x82,0xae,0xbf,0xd9,
  24777. 0x04,0xb0,0x1e,0x1d,0xc5,0x4b,0x29,0x27,
  24778. 0x09,0x4b,0x2d,0xb6,0x8d,0x6f,0x90,0x3b,
  24779. 0x68,0x40,0x1a,0xde,0xbf,0x5a,0x7e,0x08,
  24780. 0xd7,0x8f,0xf4,0xef,0x5d,0x63,0x65,0x3a,
  24781. 0x65,0x04,0x0c,0xf9,0xbf,0xd4,0xac,0xa7,
  24782. 0x98,0x4a,0x74,0xd3,0x71,0x45,0x98,0x67,
  24783. 0x80,0xfc,0x0b,0x16,0xac,0x45,0x16,0x49,
  24784. 0xde,0x61,0x88,0xa7,0xdb,0xdf,0x19,0x1f,
  24785. 0x64,0xb5,0xfc,0x5e,0x2a,0xb4,0x7b,0x57,
  24786. 0xf7,0xf7,0x27,0x6c,0xd4,0x19,0xc1,0x7a,
  24787. 0x3c,0xa8,0xe1,0xb9,0x39,0xae,0x49,0xe4,
  24788. 0x88,0xac,0xba,0x6b,0x96,0x56,0x10,0xb5,
  24789. 0x48,0x01,0x09,0xc8,0xb1,0x7b,0x80,0xe1,
  24790. 0xb7,0xb7,0x50,0xdf,0xc7,0x59,0x8d,0x5d,
  24791. 0x50,0x11,0xfd,0x2d,0xcc,0x56,0x00,0xa3,
  24792. 0x2e,0xf5,0xb5,0x2a,0x1e,0xcc,0x82,0x0e,
  24793. 0x30,0x8a,0xa3,0x42,0x72,0x1a,0xac,0x09,
  24794. 0x43,0xbf,0x66,0x86,0xb6,0x4b,0x25,0x79,
  24795. 0x37,0x65,0x04,0xcc,0xc4,0x93,0xd9,0x7e,
  24796. 0x6a,0xed,0x3f,0xb0,0xf9,0xcd,0x71,0xa4,
  24797. 0x3d,0xd4,0x97,0xf0,0x1f,0x17,0xc0,0xe2,
  24798. 0xcb,0x37,0x97,0xaa,0x2a,0x2f,0x25,0x66,
  24799. 0x56,0x16,0x8e,0x6c,0x49,0x6a,0xfc,0x5f,
  24800. 0xb9,0x32,0x46,0xf6,0xb1,0x11,0x63,0x98,
  24801. 0xa3,0x46,0xf1,0xa6,0x41,0xf3,0xb0,0x41,
  24802. 0xe9,0x89,0xf7,0x91,0x4f,0x90,0xcc,0x2c,
  24803. 0x7f,0xff,0x35,0x78,0x76,0xe5,0x06,0xb5,
  24804. 0x0d,0x33,0x4b,0xa7,0x7c,0x22,0x5b,0xc3,
  24805. 0x07,0xba,0x53,0x71,0x52,0xf3,0xf1,0x61,
  24806. 0x0e,0x4e,0xaf,0xe5,0x95,0xf6,0xd9,0xd9,
  24807. 0x0d,0x11,0xfa,0xa9,0x33,0xa1,0x5e,0xf1,
  24808. 0x36,0x95,0x46,0x86,0x8a,0x7f,0x3a,0x45,
  24809. 0xa9,0x67,0x68,0xd4,0x0f,0xd9,0xd0,0x34,
  24810. 0x12,0xc0,0x91,0xc6,0x31,0x5c,0xf4,0xfd,
  24811. 0xe7,0xcb,0x68,0x60,0x69,0x37,0x38,0x0d,
  24812. 0xb2,0xea,0xaa,0x70,0x7b,0x4c,0x41,0x85,
  24813. 0xc3,0x2e,0xdd,0xcd,0xd3,0x06,0x70,0x5e,
  24814. 0x4d,0xc1,0xff,0xc8,0x72,0xee,0xee,0x47,
  24815. 0x5a,0x64,0xdf,0xac,0x86,0xab,0xa4,0x1c,
  24816. 0x06,0x18,0x98,0x3f,0x87,0x41,0xc5,0xef,
  24817. 0x68,0xd3,0xa1,0x01,0xe8,0xa3,0xb8,0xca,
  24818. 0xc6,0x0c,0x90,0x5c,0x15,0xfc,0x91,0x08,
  24819. 0x40,0xb9,0x4c,0x00,0xa0,0xb9,0xd0
  24820. };
  24821. WOLFSSL_SMALL_STACK_STATIC const byte* msgs[] = {msg1, msg2, msg3, msg1, msg1, msg4};
  24822. WOLFSSL_SMALL_STACK_STATIC const word16 msgSz[] = {0 /*sizeof(msg1)*/,
  24823. sizeof(msg2),
  24824. sizeof(msg3),
  24825. 0 /*sizeof(msg1)*/,
  24826. 0 /*sizeof(msg1)*/,
  24827. sizeof(msg4)
  24828. };
  24829. #ifndef NO_ASN
  24830. static byte privateEd25519[] = {
  24831. 0x30,0x2e,0x02,0x01,0x00,0x30,0x05,0x06,
  24832. 0x03,0x2b,0x65,0x70,0x04,0x22,0x04,0x20,
  24833. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  24834. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  24835. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  24836. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60
  24837. };
  24838. static byte badPrivateEd25519[] = {
  24839. 0x30,0x52,0x02,0x01,0x00,0x30,0x05,0x06,
  24840. 0x03,0x2b,0x65,0x70,0x04,0x22,0x04,0x20,
  24841. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  24842. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  24843. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  24844. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60,
  24845. 0xa1,0x22,0x04,0x21,0xd7,0x5a,0x98,0x01, /* octet len 0x20 -> 0x21 */
  24846. 0x82,0xb1,0x0a,0xb7,0xd5,0x4b,0xfe,0xd3,
  24847. 0xc9,0x64,0x07,0x3a,0x0e,0xe1,0x72,0xf3,
  24848. 0xda,0xa6,0x23,0x25,0xaf,0x02,0x1a,0x68,
  24849. 0xf7,0x07,0x51,0x1a,
  24850. 0x00 /* add additional bytes to make the pubkey bigger */
  24851. };
  24852. static byte publicEd25519[] = {
  24853. 0x30,0x2a,0x30,0x05,0x06,0x03,0x2b,0x65,
  24854. 0x70,0x03,0x21,0x00,0xd7,0x5a,0x98,0x01,
  24855. 0x82,0xb1,0x0a,0xb7,0xd5,0x4b,0xfe,0xd3,
  24856. 0xc9,0x64,0x07,0x3a,0x0e,0xe1,0x72,0xf3,
  24857. 0xda,0xa6,0x23,0x25,0xaf,0x02,0x1a,0x68,
  24858. 0xf7,0x07,0x51,0x1a
  24859. };
  24860. /* size has been altered to catch if sanity check is done */
  24861. static byte badPublicEd25519[] = {
  24862. 0x30,0x2a,0x30,0x05,0x06,0x03,0x2b,0x65,
  24863. 0x70,0x03,0x21,0x00,0xd7,0x5a,0x98,0x01,
  24864. 0x82,0xb1,0x0a,0xb7,0xd5,0x4b,0xfe,0xd3,
  24865. 0xc9,0x64,0x07,0x3a,0x0e,0xe1,0x72,0xf3,
  24866. 0xda,0xa6,0x23,0x25,0xaf,0x02,0x1a,0x68,
  24867. 0xf7,0x07,0x51,0x1a,
  24868. 0x00 /* add an additional byte to make the pubkey appear bigger */
  24869. };
  24870. static byte privPubEd25519[] = {
  24871. 0x30,0x50,0x02,0x01,0x00,0x30,0x05,0x06,
  24872. 0x03,0x2b,0x65,0x70,0x04,0x22,0x04,0x20,
  24873. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  24874. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  24875. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  24876. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60,
  24877. 0x81,0x20,0xd7,0x5a,0x98,0x01,0x82,0xb1,
  24878. 0x0a,0xb7,0xd5,0x4b,0xfe,0xd3,0xc9,0x64,
  24879. 0x07,0x3a,0x0e,0xe1,0x72,0xf3,0xda,0xa6,
  24880. 0x23,0x25,0xaf,0x02,0x1a,0x68,0xf7,0x07,
  24881. 0x51,0x1a
  24882. };
  24883. word32 idx;
  24884. #endif /* NO_ASN */
  24885. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_EXPORT && HAVE_ED25519_KEY_IMPORT */
  24886. #if !defined(NO_ASN) && defined(HAVE_ED25519_SIGN)
  24887. ed25519_key key3;
  24888. #endif
  24889. /* create ed25519 keys */
  24890. #ifndef HAVE_FIPS
  24891. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  24892. #else
  24893. ret = wc_InitRng(&rng);
  24894. #endif
  24895. if (ret != 0)
  24896. return -11000;
  24897. wc_ed25519_init_ex(&key, HEAP_HINT, devId);
  24898. wc_ed25519_init_ex(&key2, HEAP_HINT, devId);
  24899. #if !defined(NO_ASN) && defined(HAVE_ED25519_SIGN)
  24900. wc_ed25519_init_ex(&key3, HEAP_HINT, devId);
  24901. #endif
  24902. wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  24903. wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key2);
  24904. /* helper functions for signature and key size */
  24905. keySz = wc_ed25519_size(&key);
  24906. sigSz = wc_ed25519_sig_size(&key);
  24907. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_EXPORT) &&\
  24908. defined(HAVE_ED25519_KEY_IMPORT)
  24909. for (i = 0; i < 6; i++) {
  24910. outlen = sizeof(out);
  24911. XMEMSET(out, 0, sizeof(out));
  24912. if (wc_ed25519_import_private_key(sKeys[i], ED25519_KEY_SIZE, pKeys[i],
  24913. pKeySz[i], &key) != 0)
  24914. return -11001 - i;
  24915. if (wc_ed25519_sign_msg(msgs[i], msgSz[i], out, &outlen, &key) != 0)
  24916. return -11011 - i;
  24917. if (XMEMCMP(out, sigs[i], 64))
  24918. return -11021 - i;
  24919. #if defined(HAVE_ED25519_VERIFY)
  24920. /* test verify on good msg */
  24921. if (wc_ed25519_verify_msg(out, outlen, msgs[i], msgSz[i], &verify,
  24922. &key) != 0 || verify != 1)
  24923. return -11031 - i;
  24924. #ifdef WOLFSSL_ED25519_STREAMING_VERIFY
  24925. /* test verify on good msg using streaming interface directly */
  24926. if (wc_ed25519_verify_msg_init(out, outlen,
  24927. &key, (byte)Ed25519, NULL, 0) != 0)
  24928. return -11211 - i;
  24929. for (j = 0; j < msgSz[i]; j += i) {
  24930. if (wc_ed25519_verify_msg_update(msgs[i] + j, MIN(i, msgSz[i] - j), &key) != 0)
  24931. return -11221 - i;
  24932. }
  24933. if (wc_ed25519_verify_msg_final(out, outlen, &verify,
  24934. &key) != 0 || verify != 1)
  24935. return -11231 - i;
  24936. #endif /* WOLFSSL_ED25519_STREAMING_VERIFY */
  24937. /* test verify on bad msg */
  24938. out[outlen-1] = out[outlen-1] + 1;
  24939. if (wc_ed25519_verify_msg(out, outlen, msgs[i], msgSz[i], &verify,
  24940. &key) == 0 || verify == 1)
  24941. return -11041 - i;
  24942. #endif /* HAVE_ED25519_VERIFY */
  24943. /* test api for import/exporting keys */
  24944. exportPSz = sizeof(exportPKey);
  24945. exportSSz = sizeof(exportSKey);
  24946. if (wc_ed25519_export_public(&key, exportPKey, &exportPSz) != 0)
  24947. return -11051 - i;
  24948. if (wc_ed25519_import_public_ex(exportPKey, exportPSz, &key2, 1) != 0)
  24949. return -11061 - i;
  24950. if (wc_ed25519_export_private_only(&key, exportSKey, &exportSSz) != 0)
  24951. return -11071 - i;
  24952. if (wc_ed25519_import_private_key(exportSKey, exportSSz,
  24953. exportPKey, exportPSz, &key2) != 0)
  24954. return -11081 - i;
  24955. /* clear "out" buffer and test sign with imported keys */
  24956. outlen = sizeof(out);
  24957. XMEMSET(out, 0, sizeof(out));
  24958. if (wc_ed25519_sign_msg(msgs[i], msgSz[i], out, &outlen, &key2) != 0)
  24959. return -11091 - i;
  24960. #if defined(HAVE_ED25519_VERIFY)
  24961. if (wc_ed25519_verify_msg(out, outlen, msgs[i], msgSz[i], &verify,
  24962. &key2) != 0 || verify != 1)
  24963. return -11101 - i;
  24964. if (XMEMCMP(out, sigs[i], 64))
  24965. return -11111 - i;
  24966. #endif /* HAVE_ED25519_VERIFY */
  24967. }
  24968. ret = ed25519ctx_test();
  24969. if (ret != 0)
  24970. return ret;
  24971. ret = ed25519ph_test();
  24972. if (ret != 0)
  24973. return ret;
  24974. #ifndef NO_ASN
  24975. /* Try ASN.1 encoded private-only key and public key. */
  24976. idx = 0;
  24977. if (wc_Ed25519PrivateKeyDecode(privateEd25519, &idx, &key3,
  24978. sizeof(privateEd25519)) != 0)
  24979. return -11121;
  24980. idx = 0;
  24981. if (wc_Ed25519PrivateKeyDecode(badPrivateEd25519, &idx, &key3,
  24982. sizeof(badPrivateEd25519)) == 0)
  24983. return -11122;
  24984. if (wc_ed25519_sign_msg(msgs[0], msgSz[0], out, &outlen, &key3)
  24985. != BAD_FUNC_ARG)
  24986. return -11131;
  24987. /* try with a buffer size that is too large */
  24988. idx = 0;
  24989. if (wc_Ed25519PublicKeyDecode(badPublicEd25519, &idx, &key3,
  24990. sizeof(badPublicEd25519)) == 0)
  24991. return -11140;
  24992. idx = 0;
  24993. if (wc_Ed25519PublicKeyDecode(publicEd25519, &idx, &key3,
  24994. sizeof(publicEd25519)) != 0)
  24995. return -11141;
  24996. if (wc_ed25519_sign_msg(msgs[0], msgSz[0], out, &outlen, &key3) != 0)
  24997. return -11151;
  24998. if (XMEMCMP(out, sigs[0], 64))
  24999. return -11161;
  25000. #if defined(HAVE_ED25519_VERIFY)
  25001. /* test verify on good msg */
  25002. if (wc_ed25519_verify_msg(out, outlen, msgs[0], msgSz[0], &verify, &key3)
  25003. != 0 || verify != 1)
  25004. return -11171;
  25005. #endif /* HAVE_ED25519_VERIFY */
  25006. wc_ed25519_free(&key3);
  25007. wc_ed25519_init(&key3);
  25008. idx = 0;
  25009. if (wc_Ed25519PrivateKeyDecode(privPubEd25519, &idx, &key3,
  25010. sizeof(privPubEd25519)) != 0)
  25011. return -11181;
  25012. if (wc_ed25519_sign_msg(msgs[0], msgSz[0], out, &outlen, &key3) != 0)
  25013. return -11191;
  25014. if (XMEMCMP(out, sigs[0], 64))
  25015. return -11201;
  25016. wc_ed25519_free(&key3);
  25017. #endif /* NO_ASN */
  25018. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_EXPORT && HAVE_ED25519_KEY_IMPORT */
  25019. /* clean up keys when done */
  25020. wc_ed25519_free(&key);
  25021. wc_ed25519_free(&key2);
  25022. #if defined(HAVE_HASHDRBG) || defined(NO_RC4)
  25023. wc_FreeRng(&rng);
  25024. #endif
  25025. /* hush warnings of unused keySz and sigSz */
  25026. (void)keySz;
  25027. (void)sigSz;
  25028. #ifdef WOLFSSL_TEST_CERT
  25029. ret = ed25519_test_cert();
  25030. if (ret < 0)
  25031. return ret;
  25032. #ifdef WOLFSSL_CERT_GEN
  25033. ret = ed25519_test_make_cert();
  25034. if (ret < 0)
  25035. return ret;
  25036. #endif /* WOLFSSL_CERT_GEN */
  25037. #endif /* WOLFSSL_TEST_CERT */
  25038. return 0;
  25039. }
  25040. #endif /* HAVE_ED25519 */
  25041. #ifdef HAVE_CURVE448
  25042. #if defined(HAVE_CURVE448_SHARED_SECRET) && \
  25043. defined(HAVE_CURVE448_KEY_IMPORT)
  25044. /* Test the wc_curve448_check_public API.
  25045. *
  25046. * returns 0 on success and -ve on failure.
  25047. */
  25048. static int curve448_check_public_test(void)
  25049. {
  25050. /* Little-endian values that will fail */
  25051. byte fail_le[][CURVE448_KEY_SIZE] = {
  25052. {
  25053. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25054. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25055. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25056. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25057. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25058. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25059. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  25060. },
  25061. {
  25062. 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25063. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25064. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25065. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25066. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25067. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25068. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  25069. },
  25070. };
  25071. /* Big-endian values that will fail */
  25072. byte fail_be[][CURVE448_KEY_SIZE] = {
  25073. {
  25074. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25075. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25076. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25077. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25078. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25079. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25080. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  25081. },
  25082. {
  25083. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25084. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25085. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25086. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25087. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25088. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25089. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  25090. },
  25091. };
  25092. /* Good or valid public value */
  25093. byte good[CURVE448_KEY_SIZE] = {
  25094. 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25095. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25096. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25097. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25098. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25099. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25100. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  25101. };
  25102. int i;
  25103. /* Parameter checks */
  25104. /* NULL pointer */
  25105. if (wc_curve448_check_public(NULL, 0, EC448_LITTLE_ENDIAN) !=
  25106. BAD_FUNC_ARG) {
  25107. return -11300;
  25108. }
  25109. if (wc_curve448_check_public(NULL, 0, EC448_BIG_ENDIAN) != BAD_FUNC_ARG) {
  25110. return -11301;
  25111. }
  25112. /* Length of 0 treated differently to other invalid lengths for TLS */
  25113. if (wc_curve448_check_public(good, 0, EC448_LITTLE_ENDIAN) != BUFFER_E)
  25114. return -11302;
  25115. if (wc_curve448_check_public(good, 0, EC448_BIG_ENDIAN) != BUFFER_E)
  25116. return -11303;
  25117. /* Length not CURVE448_KEY_SIZE */
  25118. for (i = 1; i < CURVE448_KEY_SIZE + 2; i++) {
  25119. if (i == CURVE448_KEY_SIZE)
  25120. continue;
  25121. if (wc_curve448_check_public(good, i, EC448_LITTLE_ENDIAN) !=
  25122. ECC_BAD_ARG_E) {
  25123. return -11304 - i;
  25124. }
  25125. if (wc_curve448_check_public(good, i, EC448_BIG_ENDIAN) !=
  25126. ECC_BAD_ARG_E) {
  25127. return -11314 - i;
  25128. }
  25129. }
  25130. /* Little-endian fail cases */
  25131. for (i = 0; i < (int)(sizeof(fail_le) / sizeof(*fail_le)); i++) {
  25132. if (wc_curve448_check_public(fail_le[i], CURVE448_KEY_SIZE,
  25133. EC448_LITTLE_ENDIAN) == 0) {
  25134. return -11324 - i;
  25135. }
  25136. }
  25137. /* Big-endian fail cases */
  25138. for (i = 0; i < (int)(sizeof(fail_be) / sizeof(*fail_be)); i++) {
  25139. if (wc_curve448_check_public(fail_be[i], CURVE448_KEY_SIZE,
  25140. EC448_BIG_ENDIAN) == 0) {
  25141. return -11334 - i;
  25142. }
  25143. }
  25144. /* Check a valid public value works! */
  25145. if (wc_curve448_check_public(good, CURVE448_KEY_SIZE,
  25146. EC448_LITTLE_ENDIAN) != 0) {
  25147. return -11344;
  25148. }
  25149. if (wc_curve448_check_public(good, CURVE448_KEY_SIZE,
  25150. EC448_BIG_ENDIAN) != 0) {
  25151. return -11345;
  25152. }
  25153. return 0;
  25154. }
  25155. #endif /* HAVE_CURVE448_SHARED_SECRET && HAVE_CURVE448_KEY_IMPORT */
  25156. WOLFSSL_TEST_SUBROUTINE int curve448_test(void)
  25157. {
  25158. WC_RNG rng;
  25159. int ret;
  25160. #ifdef HAVE_CURVE448_SHARED_SECRET
  25161. byte sharedA[CURVE448_KEY_SIZE];
  25162. byte sharedB[CURVE448_KEY_SIZE];
  25163. word32 y;
  25164. #endif
  25165. #ifdef HAVE_CURVE448_KEY_EXPORT
  25166. byte exportBuf[CURVE448_KEY_SIZE];
  25167. #endif
  25168. word32 x;
  25169. curve448_key userA, userB, pubKey;
  25170. #if defined(HAVE_CURVE448_SHARED_SECRET) && \
  25171. defined(HAVE_CURVE448_KEY_IMPORT)
  25172. /* test vectors from
  25173. https://www.rfc-editor.org/rfc/rfc7748.html
  25174. */
  25175. /* secret key for party a */
  25176. byte sa[] = {
  25177. 0x6b, 0x72, 0x98, 0xa5, 0xc0, 0xd8, 0xc2, 0x9a,
  25178. 0x1d, 0xab, 0x27, 0xf1, 0xa6, 0x82, 0x63, 0x00,
  25179. 0x91, 0x73, 0x89, 0x44, 0x97, 0x41, 0xa9, 0x74,
  25180. 0xf5, 0xba, 0xc9, 0xd9, 0x8d, 0xc2, 0x98, 0xd4,
  25181. 0x65, 0x55, 0xbc, 0xe8, 0xba, 0xe8, 0x9e, 0xee,
  25182. 0xd4, 0x00, 0x58, 0x4b, 0xb0, 0x46, 0xcf, 0x75,
  25183. 0x57, 0x9f, 0x51, 0xd1, 0x25, 0x49, 0x8f, 0x9a,
  25184. };
  25185. /* public key for party a */
  25186. byte pa[] = {
  25187. 0xa0, 0x1f, 0xc4, 0x32, 0xe5, 0x80, 0x7f, 0x17,
  25188. 0x53, 0x0d, 0x12, 0x88, 0xda, 0x12, 0x5b, 0x0c,
  25189. 0xd4, 0x53, 0xd9, 0x41, 0x72, 0x64, 0x36, 0xc8,
  25190. 0xbb, 0xd9, 0xc5, 0x22, 0x2c, 0x3d, 0xa7, 0xfa,
  25191. 0x63, 0x9c, 0xe0, 0x3d, 0xb8, 0xd2, 0x3b, 0x27,
  25192. 0x4a, 0x07, 0x21, 0xa1, 0xae, 0xd5, 0x22, 0x7d,
  25193. 0xe6, 0xe3, 0xb7, 0x31, 0xcc, 0xf7, 0x08, 0x9b,
  25194. };
  25195. /* secret key for party b */
  25196. byte sb[] = {
  25197. 0x2d, 0x99, 0x73, 0x51, 0xb6, 0x10, 0x6f, 0x36,
  25198. 0xb0, 0xd1, 0x09, 0x1b, 0x92, 0x9c, 0x4c, 0x37,
  25199. 0x21, 0x3e, 0x0d, 0x2b, 0x97, 0xe8, 0x5e, 0xbb,
  25200. 0x20, 0xc1, 0x27, 0x69, 0x1d, 0x0d, 0xad, 0x8f,
  25201. 0x1d, 0x81, 0x75, 0xb0, 0x72, 0x37, 0x45, 0xe6,
  25202. 0x39, 0xa3, 0xcb, 0x70, 0x44, 0x29, 0x0b, 0x99,
  25203. 0xe0, 0xe2, 0xa0, 0xc2, 0x7a, 0x6a, 0x30, 0x1c,
  25204. };
  25205. /* public key for party b */
  25206. byte pb[] = {
  25207. 0x09, 0x36, 0xf3, 0x7b, 0xc6, 0xc1, 0xbd, 0x07,
  25208. 0xae, 0x3d, 0xec, 0x7a, 0xb5, 0xdc, 0x06, 0xa7,
  25209. 0x3c, 0xa1, 0x32, 0x42, 0xfb, 0x34, 0x3e, 0xfc,
  25210. 0x72, 0xb9, 0xd8, 0x27, 0x30, 0xb4, 0x45, 0xf3,
  25211. 0xd4, 0xb0, 0xbd, 0x07, 0x71, 0x62, 0xa4, 0x6d,
  25212. 0xcf, 0xec, 0x6f, 0x9b, 0x59, 0x0b, 0xfc, 0xbc,
  25213. 0xf5, 0x20, 0xcd, 0xb0, 0x29, 0xa8, 0xb7, 0x3e,
  25214. };
  25215. /* expected shared key */
  25216. byte ss[] = {
  25217. 0x9d, 0x87, 0x4a, 0x51, 0x37, 0x50, 0x9a, 0x44,
  25218. 0x9a, 0xd5, 0x85, 0x30, 0x40, 0x24, 0x1c, 0x52,
  25219. 0x36, 0x39, 0x54, 0x35, 0xc3, 0x64, 0x24, 0xfd,
  25220. 0x56, 0x0b, 0x0c, 0xb6, 0x2b, 0x28, 0x1d, 0x28,
  25221. 0x52, 0x75, 0xa7, 0x40, 0xce, 0x32, 0xa2, 0x2d,
  25222. 0xd1, 0x74, 0x0f, 0x4a, 0xa9, 0x16, 0x1c, 0xec,
  25223. 0x95, 0xcc, 0xc6, 0x1a, 0x18, 0xf4, 0xff, 0x07,
  25224. };
  25225. #endif /* HAVE_CURVE448_SHARED_SECRET */
  25226. (void)x;
  25227. #ifndef HAVE_FIPS
  25228. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  25229. #else
  25230. ret = wc_InitRng(&rng);
  25231. #endif
  25232. if (ret != 0)
  25233. return -11400;
  25234. wc_curve448_init(&userA);
  25235. wc_curve448_init(&userB);
  25236. wc_curve448_init(&pubKey);
  25237. /* make curve448 keys */
  25238. if (wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &userA) != 0)
  25239. return -11401;
  25240. if (wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &userB) != 0)
  25241. return -11402;
  25242. #ifdef HAVE_CURVE448_SHARED_SECRET
  25243. /* find shared secret key */
  25244. x = sizeof(sharedA);
  25245. if (wc_curve448_shared_secret(&userA, &userB, sharedA, &x) != 0)
  25246. return -11403;
  25247. y = sizeof(sharedB);
  25248. if (wc_curve448_shared_secret(&userB, &userA, sharedB, &y) != 0)
  25249. return -11404;
  25250. /* compare shared secret keys to test they are the same */
  25251. if (y != x)
  25252. return -11405;
  25253. if (XMEMCMP(sharedA, sharedB, x))
  25254. return -11406;
  25255. #endif
  25256. #ifdef HAVE_CURVE448_KEY_EXPORT
  25257. /* export a public key and import it for another user */
  25258. x = sizeof(exportBuf);
  25259. if (wc_curve448_export_public(&userA, exportBuf, &x) != 0)
  25260. return -11407;
  25261. #ifdef HAVE_CURVE448_KEY_IMPORT
  25262. if (wc_curve448_import_public(exportBuf, x, &pubKey) != 0)
  25263. return -11408;
  25264. #endif
  25265. #endif
  25266. #if defined(HAVE_CURVE448_SHARED_SECRET) && \
  25267. defined(HAVE_CURVE448_KEY_IMPORT)
  25268. /* test shared key after importing a public key */
  25269. XMEMSET(sharedB, 0, sizeof(sharedB));
  25270. y = sizeof(sharedB);
  25271. if (wc_curve448_shared_secret(&userB, &pubKey, sharedB, &y) != 0)
  25272. return -11409;
  25273. if (XMEMCMP(sharedA, sharedB, y))
  25274. return -11410;
  25275. /* import RFC test vectors and compare shared key */
  25276. if (wc_curve448_import_private_raw(sa, sizeof(sa), pa, sizeof(pa), &userA)
  25277. != 0)
  25278. return -11411;
  25279. if (wc_curve448_import_private_raw(sb, sizeof(sb), pb, sizeof(pb), &userB)
  25280. != 0)
  25281. return -11412;
  25282. /* test against known test vector */
  25283. XMEMSET(sharedB, 0, sizeof(sharedB));
  25284. y = sizeof(sharedB);
  25285. if (wc_curve448_shared_secret(&userA, &userB, sharedB, &y) != 0)
  25286. return -11413;
  25287. if (XMEMCMP(ss, sharedB, y))
  25288. return -11414;
  25289. /* test swapping roles of keys and generating same shared key */
  25290. XMEMSET(sharedB, 0, sizeof(sharedB));
  25291. y = sizeof(sharedB);
  25292. if (wc_curve448_shared_secret(&userB, &userA, sharedB, &y) != 0)
  25293. return -11415;
  25294. if (XMEMCMP(ss, sharedB, y))
  25295. return -11416;
  25296. /* test with 1 generated key and 1 from known test vector */
  25297. if (wc_curve448_import_private_raw(sa, sizeof(sa), pa, sizeof(pa), &userA)
  25298. != 0)
  25299. return -11417;
  25300. if (wc_curve448_make_key(&rng, 56, &userB) != 0)
  25301. return -11418;
  25302. x = sizeof(sharedA);
  25303. if (wc_curve448_shared_secret(&userA, &userB, sharedA, &x) != 0)
  25304. return -11419;
  25305. y = sizeof(sharedB);
  25306. if (wc_curve448_shared_secret(&userB, &userA, sharedB, &y) != 0)
  25307. return -11420;
  25308. /* compare shared secret keys to test they are the same */
  25309. if (y != x)
  25310. return -11421;
  25311. if (XMEMCMP(sharedA, sharedB, x))
  25312. return -11422;
  25313. ret = curve448_check_public_test();
  25314. if (ret != 0)
  25315. return ret;
  25316. #endif /* HAVE_CURVE448_SHARED_SECRET && HAVE_CURVE448_KEY_IMPORT */
  25317. /* clean up keys when done */
  25318. wc_curve448_free(&pubKey);
  25319. wc_curve448_free(&userB);
  25320. wc_curve448_free(&userA);
  25321. wc_FreeRng(&rng);
  25322. return 0;
  25323. }
  25324. #endif /* HAVE_CURVE448 */
  25325. #ifdef HAVE_ED448
  25326. #ifdef WOLFSSL_TEST_CERT
  25327. static int ed448_test_cert(void)
  25328. {
  25329. DecodedCert cert[2];
  25330. DecodedCert* serverCert = NULL;
  25331. DecodedCert* caCert = NULL;
  25332. #ifdef HAVE_ED448_VERIFY
  25333. ed448_key key;
  25334. ed448_key* pubKey = NULL;
  25335. int verify;
  25336. #endif /* HAVE_ED448_VERIFY */
  25337. int ret;
  25338. byte* tmp;
  25339. size_t bytes;
  25340. XFILE file;
  25341. tmp = (byte *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25342. if (tmp == NULL) {
  25343. ERROR_OUT(-11430, done);
  25344. }
  25345. #ifdef USE_CERT_BUFFERS_256
  25346. XMEMCPY(tmp, ca_ed448_cert, sizeof_ca_ed448_cert);
  25347. bytes = sizeof_ca_ed448_cert;
  25348. #elif !defined(NO_FILESYSTEM)
  25349. file = XFOPEN(caEd448Cert, "rb");
  25350. if (file == NULL) {
  25351. ERROR_OUT(-11431, done);
  25352. }
  25353. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  25354. XFCLOSE(file);
  25355. #else
  25356. /* No certificate to use. */
  25357. ERROR_OUT(-11432, done);
  25358. #endif
  25359. InitDecodedCert(&cert[0], tmp, (word32)bytes, 0);
  25360. caCert = &cert[0];
  25361. ret = ParseCert(caCert, CERT_TYPE, NO_VERIFY, NULL);
  25362. if (ret != 0) {
  25363. ERROR_OUT(-11433, done);
  25364. }
  25365. #ifdef USE_CERT_BUFFERS_256
  25366. XMEMCPY(tmp, server_ed448_cert, sizeof_server_ed448_cert);
  25367. bytes = sizeof_server_ed448_cert;
  25368. #elif !defined(NO_FILESYSTEM)
  25369. file = XFOPEN(serverEd448Cert, "rb");
  25370. if (file == NULL) {
  25371. ERROR_OUT(-11434, done);
  25372. }
  25373. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  25374. XFCLOSE(file);
  25375. #else
  25376. /* No certificate to use. */
  25377. ERROR_OUT(-11435, done);
  25378. #endif
  25379. InitDecodedCert(&cert[1], tmp, (word32)bytes, 0);
  25380. serverCert = &cert[1];
  25381. ret = ParseCert(serverCert, CERT_TYPE, NO_VERIFY, NULL);
  25382. if (ret != 0) {
  25383. ERROR_OUT(-11436, done);
  25384. }
  25385. #ifdef HAVE_ED448_VERIFY
  25386. ret = wc_ed448_init(&key);
  25387. if (ret < 0) {
  25388. ERROR_OUT(-11437, done);
  25389. }
  25390. pubKey = &key;
  25391. ret = wc_ed448_import_public(caCert->publicKey, caCert->pubKeySize, pubKey);
  25392. if (ret < 0) {
  25393. ERROR_OUT(-11438, done);
  25394. }
  25395. if (wc_ed448_verify_msg(serverCert->signature, serverCert->sigLength,
  25396. serverCert->source + serverCert->certBegin,
  25397. serverCert->sigIndex - serverCert->certBegin,
  25398. &verify, pubKey, NULL, 0) < 0 || verify != 1) {
  25399. ERROR_OUT(-11439, done);
  25400. }
  25401. #endif /* HAVE_ED448_VERIFY */
  25402. done:
  25403. if (tmp != NULL)
  25404. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25405. #ifdef HAVE_ED448_VERIFY
  25406. wc_ed448_free(pubKey);
  25407. #endif /* HAVE_ED448_VERIFY */
  25408. if (caCert != NULL)
  25409. FreeDecodedCert(caCert);
  25410. if (serverCert != NULL)
  25411. FreeDecodedCert(serverCert);
  25412. return ret;
  25413. }
  25414. static int ed448_test_make_cert(void)
  25415. {
  25416. WC_RNG rng;
  25417. Cert cert;
  25418. DecodedCert decode;
  25419. ed448_key key;
  25420. ed448_key* privKey = NULL;
  25421. int ret = 0;
  25422. byte* tmp = NULL;
  25423. wc_InitCert_ex(&cert, HEAP_HINT, devId);
  25424. #ifndef HAVE_FIPS
  25425. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  25426. #else
  25427. ret = wc_InitRng(&rng);
  25428. #endif
  25429. if (ret != 0)
  25430. return -11450;
  25431. wc_ed448_init(&key);
  25432. privKey = &key;
  25433. wc_ed448_make_key(&rng, ED448_KEY_SIZE, privKey);
  25434. cert.daysValid = 365 * 2;
  25435. cert.selfSigned = 1;
  25436. XMEMCPY(&cert.issuer, &certDefaultName, sizeof(CertName));
  25437. XMEMCPY(&cert.subject, &certDefaultName, sizeof(CertName));
  25438. cert.isCA = 0;
  25439. #ifdef WOLFSSL_CERT_EXT
  25440. ret = wc_SetKeyUsage(&cert, certKeyUsage);
  25441. if (ret < 0) {
  25442. ERROR_OUT(-11451, done);
  25443. }
  25444. ret = wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ED448_TYPE, privKey);
  25445. if (ret < 0) {
  25446. ERROR_OUT(-11452, done);
  25447. }
  25448. ret = wc_SetAuthKeyIdFromPublicKey_ex(&cert, ED448_TYPE, privKey);
  25449. if (ret < 0) {
  25450. ERROR_OUT(-11453, done);
  25451. }
  25452. #endif
  25453. tmp = (byte *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25454. if (tmp == NULL) {
  25455. ERROR_OUT(-11454, done);
  25456. }
  25457. cert.sigType = CTC_ED448;
  25458. ret = wc_MakeCert_ex(&cert, tmp, FOURK_BUF, ED448_TYPE, privKey, &rng);
  25459. if (ret < 0) {
  25460. ERROR_OUT(-11455, done);
  25461. }
  25462. ret = wc_SignCert_ex(cert.bodySz, cert.sigType, tmp, FOURK_BUF, ED448_TYPE,
  25463. privKey, &rng);
  25464. if (ret < 0) {
  25465. ERROR_OUT(-11456, done);
  25466. }
  25467. InitDecodedCert(&decode, tmp, ret, HEAP_HINT);
  25468. ret = ParseCert(&decode, CERT_TYPE, NO_VERIFY, 0);
  25469. FreeDecodedCert(&decode);
  25470. if (ret != 0) {
  25471. ERROR_OUT(-11457, done);
  25472. }
  25473. done:
  25474. if (tmp != NULL)
  25475. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25476. wc_ed448_free(privKey);
  25477. wc_FreeRng(&rng);
  25478. return ret;
  25479. }
  25480. #endif /* WOLFSSL_TEST_CERT */
  25481. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_EXPORT) && \
  25482. defined(HAVE_ED448_KEY_IMPORT)
  25483. static int ed448_ctx_test(void)
  25484. {
  25485. byte out[ED448_SIG_SIZE];
  25486. word32 outlen;
  25487. #ifdef HAVE_ED448_VERIFY
  25488. int verify;
  25489. #endif /* HAVE_ED448_VERIFY */
  25490. ed448_key key;
  25491. WOLFSSL_SMALL_STACK_STATIC const byte sKeyCtx[] = {
  25492. 0xc4, 0xea, 0xb0, 0x5d, 0x35, 0x70, 0x07, 0xc6,
  25493. 0x32, 0xf3, 0xdb, 0xb4, 0x84, 0x89, 0x92, 0x4d,
  25494. 0x55, 0x2b, 0x08, 0xfe, 0x0c, 0x35, 0x3a, 0x0d,
  25495. 0x4a, 0x1f, 0x00, 0xac, 0xda, 0x2c, 0x46, 0x3a,
  25496. 0xfb, 0xea, 0x67, 0xc5, 0xe8, 0xd2, 0x87, 0x7c,
  25497. 0x5e, 0x3b, 0xc3, 0x97, 0xa6, 0x59, 0x94, 0x9e,
  25498. 0xf8, 0x02, 0x1e, 0x95, 0x4e, 0x0a, 0x12, 0x27,
  25499. 0x4e
  25500. };
  25501. WOLFSSL_SMALL_STACK_STATIC const byte pKeyCtx[] = {
  25502. 0x43, 0xba, 0x28, 0xf4, 0x30, 0xcd, 0xff, 0x45,
  25503. 0x6a, 0xe5, 0x31, 0x54, 0x5f, 0x7e, 0xcd, 0x0a,
  25504. 0xc8, 0x34, 0xa5, 0x5d, 0x93, 0x58, 0xc0, 0x37,
  25505. 0x2b, 0xfa, 0x0c, 0x6c, 0x67, 0x98, 0xc0, 0x86,
  25506. 0x6a, 0xea, 0x01, 0xeb, 0x00, 0x74, 0x28, 0x02,
  25507. 0xb8, 0x43, 0x8e, 0xa4, 0xcb, 0x82, 0x16, 0x9c,
  25508. 0x23, 0x51, 0x60, 0x62, 0x7b, 0x4c, 0x3a, 0x94,
  25509. 0x80
  25510. };
  25511. WOLFSSL_SMALL_STACK_STATIC const byte sigCtx[] = {
  25512. 0xd4, 0xf8, 0xf6, 0x13, 0x17, 0x70, 0xdd, 0x46,
  25513. 0xf4, 0x08, 0x67, 0xd6, 0xfd, 0x5d, 0x50, 0x55,
  25514. 0xde, 0x43, 0x54, 0x1f, 0x8c, 0x5e, 0x35, 0xab,
  25515. 0xbc, 0xd0, 0x01, 0xb3, 0x2a, 0x89, 0xf7, 0xd2,
  25516. 0x15, 0x1f, 0x76, 0x47, 0xf1, 0x1d, 0x8c, 0xa2,
  25517. 0xae, 0x27, 0x9f, 0xb8, 0x42, 0xd6, 0x07, 0x21,
  25518. 0x7f, 0xce, 0x6e, 0x04, 0x2f, 0x68, 0x15, 0xea,
  25519. 0x00, 0x0c, 0x85, 0x74, 0x1d, 0xe5, 0xc8, 0xda,
  25520. 0x11, 0x44, 0xa6, 0xa1, 0xab, 0xa7, 0xf9, 0x6d,
  25521. 0xe4, 0x25, 0x05, 0xd7, 0xa7, 0x29, 0x85, 0x24,
  25522. 0xfd, 0xa5, 0x38, 0xfc, 0xcb, 0xbb, 0x75, 0x4f,
  25523. 0x57, 0x8c, 0x1c, 0xad, 0x10, 0xd5, 0x4d, 0x0d,
  25524. 0x54, 0x28, 0x40, 0x7e, 0x85, 0xdc, 0xbc, 0x98,
  25525. 0xa4, 0x91, 0x55, 0xc1, 0x37, 0x64, 0xe6, 0x6c,
  25526. 0x3c, 0x00
  25527. };
  25528. WOLFSSL_SMALL_STACK_STATIC const byte msgCtx[] = {
  25529. 0x03
  25530. };
  25531. WOLFSSL_SMALL_STACK_STATIC const byte contextCtx[] = {
  25532. 0x66,0x6f,0x6f
  25533. };
  25534. outlen = sizeof(out);
  25535. XMEMSET(out, 0, sizeof(out));
  25536. if (wc_ed448_init_ex(&key, HEAP_HINT, devId) != 0)
  25537. return -11500;
  25538. if (wc_ed448_import_private_key(sKeyCtx, ED448_KEY_SIZE, pKeyCtx,
  25539. sizeof(pKeyCtx), &key) != 0)
  25540. return -11501;
  25541. if (wc_ed448_sign_msg(msgCtx, sizeof(msgCtx), out, &outlen, &key,
  25542. contextCtx, sizeof(contextCtx)) != 0)
  25543. return -11502;
  25544. if (XMEMCMP(out, sigCtx, sizeof(sigCtx)))
  25545. return -11503;
  25546. #if defined(HAVE_ED448_VERIFY)
  25547. /* test verify on good msg */
  25548. if (wc_ed448_verify_msg(out, outlen, msgCtx, sizeof(msgCtx), &verify, &key,
  25549. contextCtx, sizeof(contextCtx)) != 0 || verify != 1)
  25550. return -11504;
  25551. #endif
  25552. wc_ed448_free(&key);
  25553. return 0;
  25554. }
  25555. static int ed448ph_test(void)
  25556. {
  25557. byte out[ED448_SIG_SIZE];
  25558. word32 outlen;
  25559. #ifdef HAVE_ED448_VERIFY
  25560. int verify;
  25561. #endif /* HAVE_ED448_VERIFY */
  25562. ed448_key key;
  25563. WOLFSSL_SMALL_STACK_STATIC const byte sKeyPh[] = {
  25564. 0x83, 0x3f, 0xe6, 0x24, 0x09, 0x23, 0x7b, 0x9d,
  25565. 0x62, 0xec, 0x77, 0x58, 0x75, 0x20, 0x91, 0x1e,
  25566. 0x9a, 0x75, 0x9c, 0xec, 0x1d, 0x19, 0x75, 0x5b,
  25567. 0x7d, 0xa9, 0x01, 0xb9, 0x6d, 0xca, 0x3d, 0x42,
  25568. 0xef, 0x78, 0x22, 0xe0, 0xd5, 0x10, 0x41, 0x27,
  25569. 0xdc, 0x05, 0xd6, 0xdb, 0xef, 0xde, 0x69, 0xe3,
  25570. 0xab, 0x2c, 0xec, 0x7c, 0x86, 0x7c, 0x6e, 0x2c,
  25571. 0x49
  25572. };
  25573. WOLFSSL_SMALL_STACK_STATIC const byte pKeyPh[] = {
  25574. 0x25, 0x9b, 0x71, 0xc1, 0x9f, 0x83, 0xef, 0x77,
  25575. 0xa7, 0xab, 0xd2, 0x65, 0x24, 0xcb, 0xdb, 0x31,
  25576. 0x61, 0xb5, 0x90, 0xa4, 0x8f, 0x7d, 0x17, 0xde,
  25577. 0x3e, 0xe0, 0xba, 0x9c, 0x52, 0xbe, 0xb7, 0x43,
  25578. 0xc0, 0x94, 0x28, 0xa1, 0x31, 0xd6, 0xb1, 0xb5,
  25579. 0x73, 0x03, 0xd9, 0x0d, 0x81, 0x32, 0xc2, 0x76,
  25580. 0xd5, 0xed, 0x3d, 0x5d, 0x01, 0xc0, 0xf5, 0x38,
  25581. 0x80
  25582. };
  25583. WOLFSSL_SMALL_STACK_STATIC const byte sigPh1[] = {
  25584. 0x82, 0x2f, 0x69, 0x01, 0xf7, 0x48, 0x0f, 0x3d,
  25585. 0x5f, 0x56, 0x2c, 0x59, 0x29, 0x94, 0xd9, 0x69,
  25586. 0x36, 0x02, 0x87, 0x56, 0x14, 0x48, 0x32, 0x56,
  25587. 0x50, 0x56, 0x00, 0xbb, 0xc2, 0x81, 0xae, 0x38,
  25588. 0x1f, 0x54, 0xd6, 0xbc, 0xe2, 0xea, 0x91, 0x15,
  25589. 0x74, 0x93, 0x2f, 0x52, 0xa4, 0xe6, 0xca, 0xdd,
  25590. 0x78, 0x76, 0x93, 0x75, 0xec, 0x3f, 0xfd, 0x1b,
  25591. 0x80, 0x1a, 0x0d, 0x9b, 0x3f, 0x40, 0x30, 0xcd,
  25592. 0x43, 0x39, 0x64, 0xb6, 0x45, 0x7e, 0xa3, 0x94,
  25593. 0x76, 0x51, 0x12, 0x14, 0xf9, 0x74, 0x69, 0xb5,
  25594. 0x7d, 0xd3, 0x2d, 0xbc, 0x56, 0x0a, 0x9a, 0x94,
  25595. 0xd0, 0x0b, 0xff, 0x07, 0x62, 0x04, 0x64, 0xa3,
  25596. 0xad, 0x20, 0x3d, 0xf7, 0xdc, 0x7c, 0xe3, 0x60,
  25597. 0xc3, 0xcd, 0x36, 0x96, 0xd9, 0xd9, 0xfa, 0xb9,
  25598. 0x0f, 0x00
  25599. };
  25600. WOLFSSL_SMALL_STACK_STATIC const byte sigPh2[] = {
  25601. 0xc3, 0x22, 0x99, 0xd4, 0x6e, 0xc8, 0xff, 0x02,
  25602. 0xb5, 0x45, 0x40, 0x98, 0x28, 0x14, 0xdc, 0xe9,
  25603. 0xa0, 0x58, 0x12, 0xf8, 0x19, 0x62, 0xb6, 0x49,
  25604. 0xd5, 0x28, 0x09, 0x59, 0x16, 0xa2, 0xaa, 0x48,
  25605. 0x10, 0x65, 0xb1, 0x58, 0x04, 0x23, 0xef, 0x92,
  25606. 0x7e, 0xcf, 0x0a, 0xf5, 0x88, 0x8f, 0x90, 0xda,
  25607. 0x0f, 0x6a, 0x9a, 0x85, 0xad, 0x5d, 0xc3, 0xf2,
  25608. 0x80, 0xd9, 0x12, 0x24, 0xba, 0x99, 0x11, 0xa3,
  25609. 0x65, 0x3d, 0x00, 0xe4, 0x84, 0xe2, 0xce, 0x23,
  25610. 0x25, 0x21, 0x48, 0x1c, 0x86, 0x58, 0xdf, 0x30,
  25611. 0x4b, 0xb7, 0x74, 0x5a, 0x73, 0x51, 0x4c, 0xdb,
  25612. 0x9b, 0xf3, 0xe1, 0x57, 0x84, 0xab, 0x71, 0x28,
  25613. 0x4f, 0x8d, 0x07, 0x04, 0xa6, 0x08, 0xc5, 0x4a,
  25614. 0x6b, 0x62, 0xd9, 0x7b, 0xeb, 0x51, 0x1d, 0x13,
  25615. 0x21, 0x00
  25616. };
  25617. WOLFSSL_SMALL_STACK_STATIC const byte msgPh[] = {
  25618. 0x61,0x62,0x63
  25619. };
  25620. /* SHA-512 hash of msgPh */
  25621. WOLFSSL_SMALL_STACK_STATIC const byte hashPh[] = {
  25622. 0x48, 0x33, 0x66, 0x60, 0x13, 0x60, 0xa8, 0x77,
  25623. 0x1c, 0x68, 0x63, 0x08, 0x0c, 0xc4, 0x11, 0x4d,
  25624. 0x8d, 0xb4, 0x45, 0x30, 0xf8, 0xf1, 0xe1, 0xee,
  25625. 0x4f, 0x94, 0xea, 0x37, 0xe7, 0x8b, 0x57, 0x39,
  25626. 0xd5, 0xa1, 0x5b, 0xef, 0x18, 0x6a, 0x53, 0x86,
  25627. 0xc7, 0x57, 0x44, 0xc0, 0x52, 0x7e, 0x1f, 0xaa,
  25628. 0x9f, 0x87, 0x26, 0xe4, 0x62, 0xa1, 0x2a, 0x4f,
  25629. 0xeb, 0x06, 0xbd, 0x88, 0x01, 0xe7, 0x51, 0xe4
  25630. };
  25631. WOLFSSL_SMALL_STACK_STATIC const byte contextPh2[] = {
  25632. 0x66,0x6f,0x6f
  25633. };
  25634. outlen = sizeof(out);
  25635. XMEMSET(out, 0, sizeof(out));
  25636. if (wc_ed448_init_ex(&key, HEAP_HINT, devId) != 0)
  25637. return -11600;
  25638. if (wc_ed448_import_private_key(sKeyPh, ED448_KEY_SIZE, pKeyPh,
  25639. sizeof(pKeyPh), &key) != 0) {
  25640. return -11601;
  25641. }
  25642. if (wc_ed448ph_sign_msg(msgPh, sizeof(msgPh), out, &outlen, &key, NULL,
  25643. 0) != 0) {
  25644. return -11602;
  25645. }
  25646. if (XMEMCMP(out, sigPh1, sizeof(sigPh1)))
  25647. return -11603;
  25648. #if defined(HAVE_ED448_VERIFY)
  25649. /* test verify on good msg */
  25650. if (wc_ed448ph_verify_msg(out, outlen, msgPh, sizeof(msgPh), &verify, &key,
  25651. NULL, 0) != 0 || verify != 1) {
  25652. return -11604;
  25653. }
  25654. #endif
  25655. if (wc_ed448ph_sign_msg(msgPh, sizeof(msgPh), out, &outlen, &key,
  25656. contextPh2, sizeof(contextPh2)) != 0) {
  25657. return -11605;
  25658. }
  25659. if (XMEMCMP(out, sigPh2, sizeof(sigPh2)))
  25660. return -11606;
  25661. #if defined(HAVE_ED448_VERIFY)
  25662. /* test verify on good msg */
  25663. if (wc_ed448ph_verify_msg(out, outlen, msgPh, sizeof(msgPh), &verify, &key,
  25664. contextPh2, sizeof(contextPh2)) != 0 ||
  25665. verify != 1) {
  25666. return -11607;
  25667. }
  25668. #endif
  25669. if (wc_ed448ph_sign_hash(hashPh, sizeof(hashPh), out, &outlen, &key, NULL,
  25670. 0) != 0) {
  25671. return -11608;
  25672. }
  25673. if (XMEMCMP(out, sigPh1, sizeof(sigPh1)))
  25674. return -11609;
  25675. #if defined(HAVE_ED448_VERIFY)
  25676. if (wc_ed448ph_verify_hash(out, outlen, hashPh, sizeof(hashPh), &verify,
  25677. &key, NULL, 0) != 0 || verify != 1) {
  25678. return -11610;
  25679. }
  25680. #endif
  25681. if (wc_ed448ph_sign_hash(hashPh, sizeof(hashPh), out, &outlen, &key,
  25682. contextPh2, sizeof(contextPh2)) != 0) {
  25683. return -11611;
  25684. }
  25685. if (XMEMCMP(out, sigPh2, sizeof(sigPh2)))
  25686. return -11612;
  25687. #if defined(HAVE_ED448_VERIFY)
  25688. if (wc_ed448ph_verify_hash(out, outlen, hashPh, sizeof(hashPh), &verify,
  25689. &key, contextPh2, sizeof(contextPh2)) != 0 ||
  25690. verify != 1) {
  25691. return -11613;
  25692. }
  25693. #endif
  25694. wc_ed448_free(&key);
  25695. return 0;
  25696. }
  25697. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_EXPORT && HAVE_ED448_KEY_IMPORT */
  25698. WOLFSSL_TEST_SUBROUTINE int ed448_test(void)
  25699. {
  25700. int ret;
  25701. WC_RNG rng;
  25702. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_EXPORT) &&\
  25703. defined(HAVE_ED448_KEY_IMPORT)
  25704. byte out[ED448_SIG_SIZE];
  25705. int i;
  25706. word32 outlen;
  25707. #ifdef HAVE_ED448_VERIFY
  25708. #ifdef WOLFSSL_ED448_STREAMING_VERIFY
  25709. int j;
  25710. #endif /* WOLFSSL_ED448_STREAMING_VERIFY */
  25711. int verify;
  25712. #endif /* HAVE_ED448_VERIFY */
  25713. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_EXPORT && HAVE_ED448_KEY_IMPORT */
  25714. word32 keySz, sigSz;
  25715. #ifdef WOLFSSL_SMALL_STACK
  25716. ed448_key *key = NULL;
  25717. ed448_key *key2 = NULL;
  25718. #else
  25719. ed448_key key[1];
  25720. ed448_key key2[1];
  25721. #endif
  25722. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_EXPORT) && \
  25723. defined(HAVE_ED448_KEY_IMPORT)
  25724. /* test vectors from
  25725. https://tools.ietf.org/html/rfc8032
  25726. */
  25727. WOLFSSL_SMALL_STACK_STATIC const byte sKey1[] = {
  25728. 0x6c, 0x82, 0xa5, 0x62, 0xcb, 0x80, 0x8d, 0x10,
  25729. 0xd6, 0x32, 0xbe, 0x89, 0xc8, 0x51, 0x3e, 0xbf,
  25730. 0x6c, 0x92, 0x9f, 0x34, 0xdd, 0xfa, 0x8c, 0x9f,
  25731. 0x63, 0xc9, 0x96, 0x0e, 0xf6, 0xe3, 0x48, 0xa3,
  25732. 0x52, 0x8c, 0x8a, 0x3f, 0xcc, 0x2f, 0x04, 0x4e,
  25733. 0x39, 0xa3, 0xfc, 0x5b, 0x94, 0x49, 0x2f, 0x8f,
  25734. 0x03, 0x2e, 0x75, 0x49, 0xa2, 0x00, 0x98, 0xf9,
  25735. 0x5b
  25736. };
  25737. WOLFSSL_SMALL_STACK_STATIC const byte sKey2[] = {
  25738. 0xc4, 0xea, 0xb0, 0x5d, 0x35, 0x70, 0x07, 0xc6,
  25739. 0x32, 0xf3, 0xdb, 0xb4, 0x84, 0x89, 0x92, 0x4d,
  25740. 0x55, 0x2b, 0x08, 0xfe, 0x0c, 0x35, 0x3a, 0x0d,
  25741. 0x4a, 0x1f, 0x00, 0xac, 0xda, 0x2c, 0x46, 0x3a,
  25742. 0xfb, 0xea, 0x67, 0xc5, 0xe8, 0xd2, 0x87, 0x7c,
  25743. 0x5e, 0x3b, 0xc3, 0x97, 0xa6, 0x59, 0x94, 0x9e,
  25744. 0xf8, 0x02, 0x1e, 0x95, 0x4e, 0x0a, 0x12, 0x27,
  25745. 0x4e
  25746. };
  25747. WOLFSSL_SMALL_STACK_STATIC const byte sKey3[] = {
  25748. 0x25, 0x8c, 0xdd, 0x4a, 0xda, 0x32, 0xed, 0x9c,
  25749. 0x9f, 0xf5, 0x4e, 0x63, 0x75, 0x6a, 0xe5, 0x82,
  25750. 0xfb, 0x8f, 0xab, 0x2a, 0xc7, 0x21, 0xf2, 0xc8,
  25751. 0xe6, 0x76, 0xa7, 0x27, 0x68, 0x51, 0x3d, 0x93,
  25752. 0x9f, 0x63, 0xdd, 0xdb, 0x55, 0x60, 0x91, 0x33,
  25753. 0xf2, 0x9a, 0xdf, 0x86, 0xec, 0x99, 0x29, 0xdc,
  25754. 0xcb, 0x52, 0xc1, 0xc5, 0xfd, 0x2f, 0xf7, 0xe2,
  25755. 0x1b
  25756. };
  25757. /* uncompressed test */
  25758. WOLFSSL_SMALL_STACK_STATIC const byte sKey4[] = {
  25759. 0x6c, 0x82, 0xa5, 0x62, 0xcb, 0x80, 0x8d, 0x10,
  25760. 0xd6, 0x32, 0xbe, 0x89, 0xc8, 0x51, 0x3e, 0xbf,
  25761. 0x6c, 0x92, 0x9f, 0x34, 0xdd, 0xfa, 0x8c, 0x9f,
  25762. 0x63, 0xc9, 0x96, 0x0e, 0xf6, 0xe3, 0x48, 0xa3,
  25763. 0x52, 0x8c, 0x8a, 0x3f, 0xcc, 0x2f, 0x04, 0x4e,
  25764. 0x39, 0xa3, 0xfc, 0x5b, 0x94, 0x49, 0x2f, 0x8f,
  25765. 0x03, 0x2e, 0x75, 0x49, 0xa2, 0x00, 0x98, 0xf9,
  25766. 0x5b
  25767. };
  25768. /* compressed prefix test */
  25769. WOLFSSL_SMALL_STACK_STATIC const byte sKey5[] = {
  25770. 0x6c, 0x82, 0xa5, 0x62, 0xcb, 0x80, 0x8d, 0x10,
  25771. 0xd6, 0x32, 0xbe, 0x89, 0xc8, 0x51, 0x3e, 0xbf,
  25772. 0x6c, 0x92, 0x9f, 0x34, 0xdd, 0xfa, 0x8c, 0x9f,
  25773. 0x63, 0xc9, 0x96, 0x0e, 0xf6, 0xe3, 0x48, 0xa3,
  25774. 0x52, 0x8c, 0x8a, 0x3f, 0xcc, 0x2f, 0x04, 0x4e,
  25775. 0x39, 0xa3, 0xfc, 0x5b, 0x94, 0x49, 0x2f, 0x8f,
  25776. 0x03, 0x2e, 0x75, 0x49, 0xa2, 0x00, 0x98, 0xf9,
  25777. 0x5b
  25778. };
  25779. WOLFSSL_SMALL_STACK_STATIC const byte sKey6[] = {
  25780. 0x87, 0x2d, 0x09, 0x37, 0x80, 0xf5, 0xd3, 0x73,
  25781. 0x0d, 0xf7, 0xc2, 0x12, 0x66, 0x4b, 0x37, 0xb8,
  25782. 0xa0, 0xf2, 0x4f, 0x56, 0x81, 0x0d, 0xaa, 0x83,
  25783. 0x82, 0xcd, 0x4f, 0xa3, 0xf7, 0x76, 0x34, 0xec,
  25784. 0x44, 0xdc, 0x54, 0xf1, 0xc2, 0xed, 0x9b, 0xea,
  25785. 0x86, 0xfa, 0xfb, 0x76, 0x32, 0xd8, 0xbe, 0x19,
  25786. 0x9e, 0xa1, 0x65, 0xf5, 0xad, 0x55, 0xdd, 0x9c,
  25787. 0xe8
  25788. };
  25789. WOLFSSL_SMALL_STACK_STATIC const byte* sKeys[] = {sKey1, sKey2, sKey3, sKey4, sKey5, sKey6};
  25790. WOLFSSL_SMALL_STACK_STATIC const byte pKey1[] = {
  25791. 0x5f, 0xd7, 0x44, 0x9b, 0x59, 0xb4, 0x61, 0xfd,
  25792. 0x2c, 0xe7, 0x87, 0xec, 0x61, 0x6a, 0xd4, 0x6a,
  25793. 0x1d, 0xa1, 0x34, 0x24, 0x85, 0xa7, 0x0e, 0x1f,
  25794. 0x8a, 0x0e, 0xa7, 0x5d, 0x80, 0xe9, 0x67, 0x78,
  25795. 0xed, 0xf1, 0x24, 0x76, 0x9b, 0x46, 0xc7, 0x06,
  25796. 0x1b, 0xd6, 0x78, 0x3d, 0xf1, 0xe5, 0x0f, 0x6c,
  25797. 0xd1, 0xfa, 0x1a, 0xbe, 0xaf, 0xe8, 0x25, 0x61,
  25798. 0x80
  25799. };
  25800. WOLFSSL_SMALL_STACK_STATIC const byte pKey2[] = {
  25801. 0x43, 0xba, 0x28, 0xf4, 0x30, 0xcd, 0xff, 0x45,
  25802. 0x6a, 0xe5, 0x31, 0x54, 0x5f, 0x7e, 0xcd, 0x0a,
  25803. 0xc8, 0x34, 0xa5, 0x5d, 0x93, 0x58, 0xc0, 0x37,
  25804. 0x2b, 0xfa, 0x0c, 0x6c, 0x67, 0x98, 0xc0, 0x86,
  25805. 0x6a, 0xea, 0x01, 0xeb, 0x00, 0x74, 0x28, 0x02,
  25806. 0xb8, 0x43, 0x8e, 0xa4, 0xcb, 0x82, 0x16, 0x9c,
  25807. 0x23, 0x51, 0x60, 0x62, 0x7b, 0x4c, 0x3a, 0x94,
  25808. 0x80
  25809. };
  25810. WOLFSSL_SMALL_STACK_STATIC const byte pKey3[] = {
  25811. 0x3b, 0xa1, 0x6d, 0xa0, 0xc6, 0xf2, 0xcc, 0x1f,
  25812. 0x30, 0x18, 0x77, 0x40, 0x75, 0x6f, 0x5e, 0x79,
  25813. 0x8d, 0x6b, 0xc5, 0xfc, 0x01, 0x5d, 0x7c, 0x63,
  25814. 0xcc, 0x95, 0x10, 0xee, 0x3f, 0xd4, 0x4a, 0xdc,
  25815. 0x24, 0xd8, 0xe9, 0x68, 0xb6, 0xe4, 0x6e, 0x6f,
  25816. 0x94, 0xd1, 0x9b, 0x94, 0x53, 0x61, 0x72, 0x6b,
  25817. 0xd7, 0x5e, 0x14, 0x9e, 0xf0, 0x98, 0x17, 0xf5,
  25818. 0x80
  25819. };
  25820. /* uncompressed test */
  25821. WOLFSSL_SMALL_STACK_STATIC const byte pKey4[] = {
  25822. 0x5f, 0xd7, 0x44, 0x9b, 0x59, 0xb4, 0x61, 0xfd,
  25823. 0x2c, 0xe7, 0x87, 0xec, 0x61, 0x6a, 0xd4, 0x6a,
  25824. 0x1d, 0xa1, 0x34, 0x24, 0x85, 0xa7, 0x0e, 0x1f,
  25825. 0x8a, 0x0e, 0xa7, 0x5d, 0x80, 0xe9, 0x67, 0x78,
  25826. 0xed, 0xf1, 0x24, 0x76, 0x9b, 0x46, 0xc7, 0x06,
  25827. 0x1b, 0xd6, 0x78, 0x3d, 0xf1, 0xe5, 0x0f, 0x6c,
  25828. 0xd1, 0xfa, 0x1a, 0xbe, 0xaf, 0xe8, 0x25, 0x61,
  25829. 0x80
  25830. };
  25831. /* compressed prefix */
  25832. WOLFSSL_SMALL_STACK_STATIC const byte pKey5[] = {
  25833. 0x5f, 0xd7, 0x44, 0x9b, 0x59, 0xb4, 0x61, 0xfd,
  25834. 0x2c, 0xe7, 0x87, 0xec, 0x61, 0x6a, 0xd4, 0x6a,
  25835. 0x1d, 0xa1, 0x34, 0x24, 0x85, 0xa7, 0x0e, 0x1f,
  25836. 0x8a, 0x0e, 0xa7, 0x5d, 0x80, 0xe9, 0x67, 0x78,
  25837. 0xed, 0xf1, 0x24, 0x76, 0x9b, 0x46, 0xc7, 0x06,
  25838. 0x1b, 0xd6, 0x78, 0x3d, 0xf1, 0xe5, 0x0f, 0x6c,
  25839. 0xd1, 0xfa, 0x1a, 0xbe, 0xaf, 0xe8, 0x25, 0x61,
  25840. 0x80
  25841. };
  25842. WOLFSSL_SMALL_STACK_STATIC const byte pKey6[] = {
  25843. 0xa8, 0x1b, 0x2e, 0x8a, 0x70, 0xa5, 0xac, 0x94,
  25844. 0xff, 0xdb, 0xcc, 0x9b, 0xad, 0xfc, 0x3f, 0xeb,
  25845. 0x08, 0x01, 0xf2, 0x58, 0x57, 0x8b, 0xb1, 0x14,
  25846. 0xad, 0x44, 0xec, 0xe1, 0xec, 0x0e, 0x79, 0x9d,
  25847. 0xa0, 0x8e, 0xff, 0xb8, 0x1c, 0x5d, 0x68, 0x5c,
  25848. 0x0c, 0x56, 0xf6, 0x4e, 0xec, 0xae, 0xf8, 0xcd,
  25849. 0xf1, 0x1c, 0xc3, 0x87, 0x37, 0x83, 0x8c, 0xf4,
  25850. 0x00
  25851. };
  25852. WOLFSSL_SMALL_STACK_STATIC const byte* pKeys[] = {pKey1, pKey2, pKey3, pKey4, pKey5, pKey6};
  25853. WOLFSSL_SMALL_STACK_STATIC const byte pKeySz[] = {sizeof(pKey1), sizeof(pKey2), sizeof(pKey3),
  25854. sizeof(pKey4), sizeof(pKey5), sizeof(pKey6)};
  25855. WOLFSSL_SMALL_STACK_STATIC const byte sig1[] = {
  25856. 0x53, 0x3a, 0x37, 0xf6, 0xbb, 0xe4, 0x57, 0x25,
  25857. 0x1f, 0x02, 0x3c, 0x0d, 0x88, 0xf9, 0x76, 0xae,
  25858. 0x2d, 0xfb, 0x50, 0x4a, 0x84, 0x3e, 0x34, 0xd2,
  25859. 0x07, 0x4f, 0xd8, 0x23, 0xd4, 0x1a, 0x59, 0x1f,
  25860. 0x2b, 0x23, 0x3f, 0x03, 0x4f, 0x62, 0x82, 0x81,
  25861. 0xf2, 0xfd, 0x7a, 0x22, 0xdd, 0xd4, 0x7d, 0x78,
  25862. 0x28, 0xc5, 0x9b, 0xd0, 0xa2, 0x1b, 0xfd, 0x39,
  25863. 0x80, 0xff, 0x0d, 0x20, 0x28, 0xd4, 0xb1, 0x8a,
  25864. 0x9d, 0xf6, 0x3e, 0x00, 0x6c, 0x5d, 0x1c, 0x2d,
  25865. 0x34, 0x5b, 0x92, 0x5d, 0x8d, 0xc0, 0x0b, 0x41,
  25866. 0x04, 0x85, 0x2d, 0xb9, 0x9a, 0xc5, 0xc7, 0xcd,
  25867. 0xda, 0x85, 0x30, 0xa1, 0x13, 0xa0, 0xf4, 0xdb,
  25868. 0xb6, 0x11, 0x49, 0xf0, 0x5a, 0x73, 0x63, 0x26,
  25869. 0x8c, 0x71, 0xd9, 0x58, 0x08, 0xff, 0x2e, 0x65,
  25870. 0x26, 0x00
  25871. };
  25872. WOLFSSL_SMALL_STACK_STATIC const byte sig2[] = {
  25873. 0x26, 0xb8, 0xf9, 0x17, 0x27, 0xbd, 0x62, 0x89,
  25874. 0x7a, 0xf1, 0x5e, 0x41, 0xeb, 0x43, 0xc3, 0x77,
  25875. 0xef, 0xb9, 0xc6, 0x10, 0xd4, 0x8f, 0x23, 0x35,
  25876. 0xcb, 0x0b, 0xd0, 0x08, 0x78, 0x10, 0xf4, 0x35,
  25877. 0x25, 0x41, 0xb1, 0x43, 0xc4, 0xb9, 0x81, 0xb7,
  25878. 0xe1, 0x8f, 0x62, 0xde, 0x8c, 0xcd, 0xf6, 0x33,
  25879. 0xfc, 0x1b, 0xf0, 0x37, 0xab, 0x7c, 0xd7, 0x79,
  25880. 0x80, 0x5e, 0x0d, 0xbc, 0xc0, 0xaa, 0xe1, 0xcb,
  25881. 0xce, 0xe1, 0xaf, 0xb2, 0xe0, 0x27, 0xdf, 0x36,
  25882. 0xbc, 0x04, 0xdc, 0xec, 0xbf, 0x15, 0x43, 0x36,
  25883. 0xc1, 0x9f, 0x0a, 0xf7, 0xe0, 0xa6, 0x47, 0x29,
  25884. 0x05, 0xe7, 0x99, 0xf1, 0x95, 0x3d, 0x2a, 0x0f,
  25885. 0xf3, 0x34, 0x8a, 0xb2, 0x1a, 0xa4, 0xad, 0xaf,
  25886. 0xd1, 0xd2, 0x34, 0x44, 0x1c, 0xf8, 0x07, 0xc0,
  25887. 0x3a, 0x00
  25888. };
  25889. WOLFSSL_SMALL_STACK_STATIC const byte sig3[] = {
  25890. 0x7e, 0xee, 0xab, 0x7c, 0x4e, 0x50, 0xfb, 0x79,
  25891. 0x9b, 0x41, 0x8e, 0xe5, 0xe3, 0x19, 0x7f, 0xf6,
  25892. 0xbf, 0x15, 0xd4, 0x3a, 0x14, 0xc3, 0x43, 0x89,
  25893. 0xb5, 0x9d, 0xd1, 0xa7, 0xb1, 0xb8, 0x5b, 0x4a,
  25894. 0xe9, 0x04, 0x38, 0xac, 0xa6, 0x34, 0xbe, 0xa4,
  25895. 0x5e, 0x3a, 0x26, 0x95, 0xf1, 0x27, 0x0f, 0x07,
  25896. 0xfd, 0xcd, 0xf7, 0xc6, 0x2b, 0x8e, 0xfe, 0xaf,
  25897. 0x00, 0xb4, 0x5c, 0x2c, 0x96, 0xba, 0x45, 0x7e,
  25898. 0xb1, 0xa8, 0xbf, 0x07, 0x5a, 0x3d, 0xb2, 0x8e,
  25899. 0x5c, 0x24, 0xf6, 0xb9, 0x23, 0xed, 0x4a, 0xd7,
  25900. 0x47, 0xc3, 0xc9, 0xe0, 0x3c, 0x70, 0x79, 0xef,
  25901. 0xb8, 0x7c, 0xb1, 0x10, 0xd3, 0xa9, 0x98, 0x61,
  25902. 0xe7, 0x20, 0x03, 0xcb, 0xae, 0x6d, 0x6b, 0x8b,
  25903. 0x82, 0x7e, 0x4e, 0x6c, 0x14, 0x30, 0x64, 0xff,
  25904. 0x3c, 0x00
  25905. };
  25906. /* uncompressed test */
  25907. WOLFSSL_SMALL_STACK_STATIC const byte sig4[] = {
  25908. 0x53, 0x3a, 0x37, 0xf6, 0xbb, 0xe4, 0x57, 0x25,
  25909. 0x1f, 0x02, 0x3c, 0x0d, 0x88, 0xf9, 0x76, 0xae,
  25910. 0x2d, 0xfb, 0x50, 0x4a, 0x84, 0x3e, 0x34, 0xd2,
  25911. 0x07, 0x4f, 0xd8, 0x23, 0xd4, 0x1a, 0x59, 0x1f,
  25912. 0x2b, 0x23, 0x3f, 0x03, 0x4f, 0x62, 0x82, 0x81,
  25913. 0xf2, 0xfd, 0x7a, 0x22, 0xdd, 0xd4, 0x7d, 0x78,
  25914. 0x28, 0xc5, 0x9b, 0xd0, 0xa2, 0x1b, 0xfd, 0x39,
  25915. 0x80, 0xff, 0x0d, 0x20, 0x28, 0xd4, 0xb1, 0x8a,
  25916. 0x9d, 0xf6, 0x3e, 0x00, 0x6c, 0x5d, 0x1c, 0x2d,
  25917. 0x34, 0x5b, 0x92, 0x5d, 0x8d, 0xc0, 0x0b, 0x41,
  25918. 0x04, 0x85, 0x2d, 0xb9, 0x9a, 0xc5, 0xc7, 0xcd,
  25919. 0xda, 0x85, 0x30, 0xa1, 0x13, 0xa0, 0xf4, 0xdb,
  25920. 0xb6, 0x11, 0x49, 0xf0, 0x5a, 0x73, 0x63, 0x26,
  25921. 0x8c, 0x71, 0xd9, 0x58, 0x08, 0xff, 0x2e, 0x65,
  25922. 0x26, 0x00
  25923. };
  25924. /* compressed prefix */
  25925. WOLFSSL_SMALL_STACK_STATIC const byte sig5[] = {
  25926. 0x53, 0x3a, 0x37, 0xf6, 0xbb, 0xe4, 0x57, 0x25,
  25927. 0x1f, 0x02, 0x3c, 0x0d, 0x88, 0xf9, 0x76, 0xae,
  25928. 0x2d, 0xfb, 0x50, 0x4a, 0x84, 0x3e, 0x34, 0xd2,
  25929. 0x07, 0x4f, 0xd8, 0x23, 0xd4, 0x1a, 0x59, 0x1f,
  25930. 0x2b, 0x23, 0x3f, 0x03, 0x4f, 0x62, 0x82, 0x81,
  25931. 0xf2, 0xfd, 0x7a, 0x22, 0xdd, 0xd4, 0x7d, 0x78,
  25932. 0x28, 0xc5, 0x9b, 0xd0, 0xa2, 0x1b, 0xfd, 0x39,
  25933. 0x80, 0xff, 0x0d, 0x20, 0x28, 0xd4, 0xb1, 0x8a,
  25934. 0x9d, 0xf6, 0x3e, 0x00, 0x6c, 0x5d, 0x1c, 0x2d,
  25935. 0x34, 0x5b, 0x92, 0x5d, 0x8d, 0xc0, 0x0b, 0x41,
  25936. 0x04, 0x85, 0x2d, 0xb9, 0x9a, 0xc5, 0xc7, 0xcd,
  25937. 0xda, 0x85, 0x30, 0xa1, 0x13, 0xa0, 0xf4, 0xdb,
  25938. 0xb6, 0x11, 0x49, 0xf0, 0x5a, 0x73, 0x63, 0x26,
  25939. 0x8c, 0x71, 0xd9, 0x58, 0x08, 0xff, 0x2e, 0x65,
  25940. 0x26, 0x00
  25941. };
  25942. WOLFSSL_SMALL_STACK_STATIC const byte sig6[] = {
  25943. 0xe3, 0x01, 0x34, 0x5a, 0x41, 0xa3, 0x9a, 0x4d,
  25944. 0x72, 0xff, 0xf8, 0xdf, 0x69, 0xc9, 0x80, 0x75,
  25945. 0xa0, 0xcc, 0x08, 0x2b, 0x80, 0x2f, 0xc9, 0xb2,
  25946. 0xb6, 0xbc, 0x50, 0x3f, 0x92, 0x6b, 0x65, 0xbd,
  25947. 0xdf, 0x7f, 0x4c, 0x8f, 0x1c, 0xb4, 0x9f, 0x63,
  25948. 0x96, 0xaf, 0xc8, 0xa7, 0x0a, 0xbe, 0x6d, 0x8a,
  25949. 0xef, 0x0d, 0xb4, 0x78, 0xd4, 0xc6, 0xb2, 0x97,
  25950. 0x00, 0x76, 0xc6, 0xa0, 0x48, 0x4f, 0xe7, 0x6d,
  25951. 0x76, 0xb3, 0xa9, 0x76, 0x25, 0xd7, 0x9f, 0x1c,
  25952. 0xe2, 0x40, 0xe7, 0xc5, 0x76, 0x75, 0x0d, 0x29,
  25953. 0x55, 0x28, 0x28, 0x6f, 0x71, 0x9b, 0x41, 0x3d,
  25954. 0xe9, 0xad, 0xa3, 0xe8, 0xeb, 0x78, 0xed, 0x57,
  25955. 0x36, 0x03, 0xce, 0x30, 0xd8, 0xbb, 0x76, 0x17,
  25956. 0x85, 0xdc, 0x30, 0xdb, 0xc3, 0x20, 0x86, 0x9e,
  25957. 0x1a, 0x00
  25958. };
  25959. WOLFSSL_SMALL_STACK_STATIC const byte* sigs[] = {sig1, sig2, sig3, sig4, sig5, sig6};
  25960. #define SIGSZ sizeof(sig1)
  25961. PEDANTIC_EXTENSION WOLFSSL_SMALL_STACK_STATIC const byte msg1[] = { };
  25962. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] = { 0x03 };
  25963. WOLFSSL_SMALL_STACK_STATIC const byte msg3[] = { 0x64, 0xa6, 0x5f, 0x3c, 0xde, 0xdc, 0xdd,
  25964. 0x66, 0x81, 0x1e, 0x29, 0x15 };
  25965. /* test of a 1023 byte long message */
  25966. WOLFSSL_SMALL_STACK_STATIC const byte msg4[] = {
  25967. 0x6d, 0xdf, 0x80, 0x2e, 0x1a, 0xae, 0x49, 0x86,
  25968. 0x93, 0x5f, 0x7f, 0x98, 0x1b, 0xa3, 0xf0, 0x35,
  25969. 0x1d, 0x62, 0x73, 0xc0, 0xa0, 0xc2, 0x2c, 0x9c,
  25970. 0x0e, 0x83, 0x39, 0x16, 0x8e, 0x67, 0x54, 0x12,
  25971. 0xa3, 0xde, 0xbf, 0xaf, 0x43, 0x5e, 0xd6, 0x51,
  25972. 0x55, 0x80, 0x07, 0xdb, 0x43, 0x84, 0xb6, 0x50,
  25973. 0xfc, 0xc0, 0x7e, 0x3b, 0x58, 0x6a, 0x27, 0xa4,
  25974. 0xf7, 0xa0, 0x0a, 0xc8, 0xa6, 0xfe, 0xc2, 0xcd,
  25975. 0x86, 0xae, 0x4b, 0xf1, 0x57, 0x0c, 0x41, 0xe6,
  25976. 0xa4, 0x0c, 0x93, 0x1d, 0xb2, 0x7b, 0x2f, 0xaa,
  25977. 0x15, 0xa8, 0xce, 0xdd, 0x52, 0xcf, 0xf7, 0x36,
  25978. 0x2c, 0x4e, 0x6e, 0x23, 0xda, 0xec, 0x0f, 0xbc,
  25979. 0x3a, 0x79, 0xb6, 0x80, 0x6e, 0x31, 0x6e, 0xfc,
  25980. 0xc7, 0xb6, 0x81, 0x19, 0xbf, 0x46, 0xbc, 0x76,
  25981. 0xa2, 0x60, 0x67, 0xa5, 0x3f, 0x29, 0x6d, 0xaf,
  25982. 0xdb, 0xdc, 0x11, 0xc7, 0x7f, 0x77, 0x77, 0xe9,
  25983. 0x72, 0x66, 0x0c, 0xf4, 0xb6, 0xa9, 0xb3, 0x69,
  25984. 0xa6, 0x66, 0x5f, 0x02, 0xe0, 0xcc, 0x9b, 0x6e,
  25985. 0xdf, 0xad, 0x13, 0x6b, 0x4f, 0xab, 0xe7, 0x23,
  25986. 0xd2, 0x81, 0x3d, 0xb3, 0x13, 0x6c, 0xfd, 0xe9,
  25987. 0xb6, 0xd0, 0x44, 0x32, 0x2f, 0xee, 0x29, 0x47,
  25988. 0x95, 0x2e, 0x03, 0x1b, 0x73, 0xab, 0x5c, 0x60,
  25989. 0x33, 0x49, 0xb3, 0x07, 0xbd, 0xc2, 0x7b, 0xc6,
  25990. 0xcb, 0x8b, 0x8b, 0xbd, 0x7b, 0xd3, 0x23, 0x21,
  25991. 0x9b, 0x80, 0x33, 0xa5, 0x81, 0xb5, 0x9e, 0xad,
  25992. 0xeb, 0xb0, 0x9b, 0x3c, 0x4f, 0x3d, 0x22, 0x77,
  25993. 0xd4, 0xf0, 0x34, 0x36, 0x24, 0xac, 0xc8, 0x17,
  25994. 0x80, 0x47, 0x28, 0xb2, 0x5a, 0xb7, 0x97, 0x17,
  25995. 0x2b, 0x4c, 0x5c, 0x21, 0xa2, 0x2f, 0x9c, 0x78,
  25996. 0x39, 0xd6, 0x43, 0x00, 0x23, 0x2e, 0xb6, 0x6e,
  25997. 0x53, 0xf3, 0x1c, 0x72, 0x3f, 0xa3, 0x7f, 0xe3,
  25998. 0x87, 0xc7, 0xd3, 0xe5, 0x0b, 0xdf, 0x98, 0x13,
  25999. 0xa3, 0x0e, 0x5b, 0xb1, 0x2c, 0xf4, 0xcd, 0x93,
  26000. 0x0c, 0x40, 0xcf, 0xb4, 0xe1, 0xfc, 0x62, 0x25,
  26001. 0x92, 0xa4, 0x95, 0x88, 0x79, 0x44, 0x94, 0xd5,
  26002. 0x6d, 0x24, 0xea, 0x4b, 0x40, 0xc8, 0x9f, 0xc0,
  26003. 0x59, 0x6c, 0xc9, 0xeb, 0xb9, 0x61, 0xc8, 0xcb,
  26004. 0x10, 0xad, 0xde, 0x97, 0x6a, 0x5d, 0x60, 0x2b,
  26005. 0x1c, 0x3f, 0x85, 0xb9, 0xb9, 0xa0, 0x01, 0xed,
  26006. 0x3c, 0x6a, 0x4d, 0x3b, 0x14, 0x37, 0xf5, 0x20,
  26007. 0x96, 0xcd, 0x19, 0x56, 0xd0, 0x42, 0xa5, 0x97,
  26008. 0xd5, 0x61, 0xa5, 0x96, 0xec, 0xd3, 0xd1, 0x73,
  26009. 0x5a, 0x8d, 0x57, 0x0e, 0xa0, 0xec, 0x27, 0x22,
  26010. 0x5a, 0x2c, 0x4a, 0xaf, 0xf2, 0x63, 0x06, 0xd1,
  26011. 0x52, 0x6c, 0x1a, 0xf3, 0xca, 0x6d, 0x9c, 0xf5,
  26012. 0xa2, 0xc9, 0x8f, 0x47, 0xe1, 0xc4, 0x6d, 0xb9,
  26013. 0xa3, 0x32, 0x34, 0xcf, 0xd4, 0xd8, 0x1f, 0x2c,
  26014. 0x98, 0x53, 0x8a, 0x09, 0xeb, 0xe7, 0x69, 0x98,
  26015. 0xd0, 0xd8, 0xfd, 0x25, 0x99, 0x7c, 0x7d, 0x25,
  26016. 0x5c, 0x6d, 0x66, 0xec, 0xe6, 0xfa, 0x56, 0xf1,
  26017. 0x11, 0x44, 0x95, 0x0f, 0x02, 0x77, 0x95, 0xe6,
  26018. 0x53, 0x00, 0x8f, 0x4b, 0xd7, 0xca, 0x2d, 0xee,
  26019. 0x85, 0xd8, 0xe9, 0x0f, 0x3d, 0xc3, 0x15, 0x13,
  26020. 0x0c, 0xe2, 0xa0, 0x03, 0x75, 0xa3, 0x18, 0xc7,
  26021. 0xc3, 0xd9, 0x7b, 0xe2, 0xc8, 0xce, 0x5b, 0x6d,
  26022. 0xb4, 0x1a, 0x62, 0x54, 0xff, 0x26, 0x4f, 0xa6,
  26023. 0x15, 0x5b, 0xae, 0xe3, 0xb0, 0x77, 0x3c, 0x0f,
  26024. 0x49, 0x7c, 0x57, 0x3f, 0x19, 0xbb, 0x4f, 0x42,
  26025. 0x40, 0x28, 0x1f, 0x0b, 0x1f, 0x4f, 0x7b, 0xe8,
  26026. 0x57, 0xa4, 0xe5, 0x9d, 0x41, 0x6c, 0x06, 0xb4,
  26027. 0xc5, 0x0f, 0xa0, 0x9e, 0x18, 0x10, 0xdd, 0xc6,
  26028. 0xb1, 0x46, 0x7b, 0xae, 0xac, 0x5a, 0x36, 0x68,
  26029. 0xd1, 0x1b, 0x6e, 0xca, 0xa9, 0x01, 0x44, 0x00,
  26030. 0x16, 0xf3, 0x89, 0xf8, 0x0a, 0xcc, 0x4d, 0xb9,
  26031. 0x77, 0x02, 0x5e, 0x7f, 0x59, 0x24, 0x38, 0x8c,
  26032. 0x7e, 0x34, 0x0a, 0x73, 0x2e, 0x55, 0x44, 0x40,
  26033. 0xe7, 0x65, 0x70, 0xf8, 0xdd, 0x71, 0xb7, 0xd6,
  26034. 0x40, 0xb3, 0x45, 0x0d, 0x1f, 0xd5, 0xf0, 0x41,
  26035. 0x0a, 0x18, 0xf9, 0xa3, 0x49, 0x4f, 0x70, 0x7c,
  26036. 0x71, 0x7b, 0x79, 0xb4, 0xbf, 0x75, 0xc9, 0x84,
  26037. 0x00, 0xb0, 0x96, 0xb2, 0x16, 0x53, 0xb5, 0xd2,
  26038. 0x17, 0xcf, 0x35, 0x65, 0xc9, 0x59, 0x74, 0x56,
  26039. 0xf7, 0x07, 0x03, 0x49, 0x7a, 0x07, 0x87, 0x63,
  26040. 0x82, 0x9b, 0xc0, 0x1b, 0xb1, 0xcb, 0xc8, 0xfa,
  26041. 0x04, 0xea, 0xdc, 0x9a, 0x6e, 0x3f, 0x66, 0x99,
  26042. 0x58, 0x7a, 0x9e, 0x75, 0xc9, 0x4e, 0x5b, 0xab,
  26043. 0x00, 0x36, 0xe0, 0xb2, 0xe7, 0x11, 0x39, 0x2c,
  26044. 0xff, 0x00, 0x47, 0xd0, 0xd6, 0xb0, 0x5b, 0xd2,
  26045. 0xa5, 0x88, 0xbc, 0x10, 0x97, 0x18, 0x95, 0x42,
  26046. 0x59, 0xf1, 0xd8, 0x66, 0x78, 0xa5, 0x79, 0xa3,
  26047. 0x12, 0x0f, 0x19, 0xcf, 0xb2, 0x96, 0x3f, 0x17,
  26048. 0x7a, 0xeb, 0x70, 0xf2, 0xd4, 0x84, 0x48, 0x26,
  26049. 0x26, 0x2e, 0x51, 0xb8, 0x02, 0x71, 0x27, 0x20,
  26050. 0x68, 0xef, 0x5b, 0x38, 0x56, 0xfa, 0x85, 0x35,
  26051. 0xaa, 0x2a, 0x88, 0xb2, 0xd4, 0x1f, 0x2a, 0x0e,
  26052. 0x2f, 0xda, 0x76, 0x24, 0xc2, 0x85, 0x02, 0x72,
  26053. 0xac, 0x4a, 0x2f, 0x56, 0x1f, 0x8f, 0x2f, 0x7a,
  26054. 0x31, 0x8b, 0xfd, 0x5c, 0xaf, 0x96, 0x96, 0x14,
  26055. 0x9e, 0x4a, 0xc8, 0x24, 0xad, 0x34, 0x60, 0x53,
  26056. 0x8f, 0xdc, 0x25, 0x42, 0x1b, 0xee, 0xc2, 0xcc,
  26057. 0x68, 0x18, 0x16, 0x2d, 0x06, 0xbb, 0xed, 0x0c,
  26058. 0x40, 0xa3, 0x87, 0x19, 0x23, 0x49, 0xdb, 0x67,
  26059. 0xa1, 0x18, 0xba, 0xda, 0x6c, 0xd5, 0xab, 0x01,
  26060. 0x40, 0xee, 0x27, 0x32, 0x04, 0xf6, 0x28, 0xaa,
  26061. 0xd1, 0xc1, 0x35, 0xf7, 0x70, 0x27, 0x9a, 0x65,
  26062. 0x1e, 0x24, 0xd8, 0xc1, 0x4d, 0x75, 0xa6, 0x05,
  26063. 0x9d, 0x76, 0xb9, 0x6a, 0x6f, 0xd8, 0x57, 0xde,
  26064. 0xf5, 0xe0, 0xb3, 0x54, 0xb2, 0x7a, 0xb9, 0x37,
  26065. 0xa5, 0x81, 0x5d, 0x16, 0xb5, 0xfa, 0xe4, 0x07,
  26066. 0xff, 0x18, 0x22, 0x2c, 0x6d, 0x1e, 0xd2, 0x63,
  26067. 0xbe, 0x68, 0xc9, 0x5f, 0x32, 0xd9, 0x08, 0xbd,
  26068. 0x89, 0x5c, 0xd7, 0x62, 0x07, 0xae, 0x72, 0x64,
  26069. 0x87, 0x56, 0x7f, 0x9a, 0x67, 0xda, 0xd7, 0x9a,
  26070. 0xbe, 0xc3, 0x16, 0xf6, 0x83, 0xb1, 0x7f, 0x2d,
  26071. 0x02, 0xbf, 0x07, 0xe0, 0xac, 0x8b, 0x5b, 0xc6,
  26072. 0x16, 0x2c, 0xf9, 0x46, 0x97, 0xb3, 0xc2, 0x7c,
  26073. 0xd1, 0xfe, 0xa4, 0x9b, 0x27, 0xf2, 0x3b, 0xa2,
  26074. 0x90, 0x18, 0x71, 0x96, 0x25, 0x06, 0x52, 0x0c,
  26075. 0x39, 0x2d, 0xa8, 0xb6, 0xad, 0x0d, 0x99, 0xf7,
  26076. 0x01, 0x3f, 0xbc, 0x06, 0xc2, 0xc1, 0x7a, 0x56,
  26077. 0x95, 0x00, 0xc8, 0xa7, 0x69, 0x64, 0x81, 0xc1,
  26078. 0xcd, 0x33, 0xe9, 0xb1, 0x4e, 0x40, 0xb8, 0x2e,
  26079. 0x79, 0xa5, 0xf5, 0xdb, 0x82, 0x57, 0x1b, 0xa9,
  26080. 0x7b, 0xae, 0x3a, 0xd3, 0xe0, 0x47, 0x95, 0x15,
  26081. 0xbb, 0x0e, 0x2b, 0x0f, 0x3b, 0xfc, 0xd1, 0xfd,
  26082. 0x33, 0x03, 0x4e, 0xfc, 0x62, 0x45, 0xed, 0xdd,
  26083. 0x7e, 0xe2, 0x08, 0x6d, 0xda, 0xe2, 0x60, 0x0d,
  26084. 0x8c, 0xa7, 0x3e, 0x21, 0x4e, 0x8c, 0x2b, 0x0b,
  26085. 0xdb, 0x2b, 0x04, 0x7c, 0x6a, 0x46, 0x4a, 0x56,
  26086. 0x2e, 0xd7, 0x7b, 0x73, 0xd2, 0xd8, 0x41, 0xc4,
  26087. 0xb3, 0x49, 0x73, 0x55, 0x12, 0x57, 0x71, 0x3b,
  26088. 0x75, 0x36, 0x32, 0xef, 0xba, 0x34, 0x81, 0x69,
  26089. 0xab, 0xc9, 0x0a, 0x68, 0xf4, 0x26, 0x11, 0xa4,
  26090. 0x01, 0x26, 0xd7, 0xcb, 0x21, 0xb5, 0x86, 0x95,
  26091. 0x56, 0x81, 0x86, 0xf7, 0xe5, 0x69, 0xd2, 0xff,
  26092. 0x0f, 0x9e, 0x74, 0x5d, 0x04, 0x87, 0xdd, 0x2e,
  26093. 0xb9, 0x97, 0xca, 0xfc, 0x5a, 0xbf, 0x9d, 0xd1,
  26094. 0x02, 0xe6, 0x2f, 0xf6, 0x6c, 0xba, 0x87
  26095. };
  26096. WOLFSSL_SMALL_STACK_STATIC const byte* msgs[] = {msg1, msg2, msg3, msg1, msg1, msg4};
  26097. WOLFSSL_SMALL_STACK_STATIC const word16 msgSz[] = {0 /*sizeof(msg1)*/,
  26098. sizeof(msg2),
  26099. sizeof(msg3),
  26100. 0 /*sizeof(msg1)*/,
  26101. 0 /*sizeof(msg1)*/,
  26102. sizeof(msg4)
  26103. };
  26104. #ifndef NO_ASN
  26105. static const byte privateEd448[] = {
  26106. 0x30, 0x47, 0x02, 0x01, 0x00, 0x30, 0x05, 0x06,
  26107. 0x03, 0x2b, 0x65, 0x71, 0x04, 0x3b, 0x04, 0x39,
  26108. 0x6c, 0x82, 0xa5, 0x62, 0xcb, 0x80, 0x8d, 0x10,
  26109. 0xd6, 0x32, 0xbe, 0x89, 0xc8, 0x51, 0x3e, 0xbf,
  26110. 0x6c, 0x92, 0x9f, 0x34, 0xdd, 0xfa, 0x8c, 0x9f,
  26111. 0x63, 0xc9, 0x96, 0x0e, 0xf6, 0xe3, 0x48, 0xa3,
  26112. 0x52, 0x8c, 0x8a, 0x3f, 0xcc, 0x2f, 0x04, 0x4e,
  26113. 0x39, 0xa3, 0xfc, 0x5b, 0x94, 0x49, 0x2f, 0x8f,
  26114. 0x03, 0x2e, 0x75, 0x49, 0xa2, 0x00, 0x98, 0xf9,
  26115. 0x5b
  26116. };
  26117. static const byte publicEd448[] = {
  26118. 0x30, 0x43, 0x30, 0x05, 0x06, 0x03, 0x2b, 0x65,
  26119. 0x71, 0x03, 0x3a, 0x00, 0x5f, 0xd7, 0x44, 0x9b,
  26120. 0x59, 0xb4, 0x61, 0xfd, 0x2c, 0xe7, 0x87, 0xec,
  26121. 0x61, 0x6a, 0xd4, 0x6a, 0x1d, 0xa1, 0x34, 0x24,
  26122. 0x85, 0xa7, 0x0e, 0x1f, 0x8a, 0x0e, 0xa7, 0x5d,
  26123. 0x80, 0xe9, 0x67, 0x78, 0xed, 0xf1, 0x24, 0x76,
  26124. 0x9b, 0x46, 0xc7, 0x06, 0x1b, 0xd6, 0x78, 0x3d,
  26125. 0xf1, 0xe5, 0x0f, 0x6c, 0xd1, 0xfa, 0x1a, 0xbe,
  26126. 0xaf, 0xe8, 0x25, 0x61, 0x80
  26127. };
  26128. static const byte privPubEd448[] = {
  26129. 0x30, 0x81, 0x82, 0x02, 0x01, 0x00, 0x30, 0x05,
  26130. 0x06, 0x03, 0x2b, 0x65, 0x71, 0x04, 0x3b, 0x04,
  26131. 0x39, 0x6c, 0x82, 0xa5, 0x62, 0xcb, 0x80, 0x8d,
  26132. 0x10, 0xd6, 0x32, 0xbe, 0x89, 0xc8, 0x51, 0x3e,
  26133. 0xbf, 0x6c, 0x92, 0x9f, 0x34, 0xdd, 0xfa, 0x8c,
  26134. 0x9f, 0x63, 0xc9, 0x96, 0x0e, 0xf6, 0xe3, 0x48,
  26135. 0xa3, 0x52, 0x8c, 0x8a, 0x3f, 0xcc, 0x2f, 0x04,
  26136. 0x4e, 0x39, 0xa3, 0xfc, 0x5b, 0x94, 0x49, 0x2f,
  26137. 0x8f, 0x03, 0x2e, 0x75, 0x49, 0xa2, 0x00, 0x98,
  26138. 0xf9, 0x5b, 0x81, 0x39, 0x5f, 0xd7, 0x44, 0x9b,
  26139. 0x59, 0xb4, 0x61, 0xfd, 0x2c, 0xe7, 0x87, 0xec,
  26140. 0x61, 0x6a, 0xd4, 0x6a, 0x1d, 0xa1, 0x34, 0x24,
  26141. 0x85, 0xa7, 0x0e, 0x1f, 0x8a, 0x0e, 0xa7, 0x5d,
  26142. 0x80, 0xe9, 0x67, 0x78, 0xed, 0xf1, 0x24, 0x76,
  26143. 0x9b, 0x46, 0xc7, 0x06, 0x1b, 0xd6, 0x78, 0x3d,
  26144. 0xf1, 0xe5, 0x0f, 0x6c, 0xd1, 0xfa, 0x1a, 0xbe,
  26145. 0xaf, 0xe8, 0x25, 0x61, 0x80
  26146. };
  26147. word32 idx;
  26148. #endif /* NO_ASN */
  26149. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_EXPORT && HAVE_ED448_KEY_IMPORT */
  26150. #if !defined(NO_ASN) && defined(HAVE_ED448_SIGN)
  26151. #ifdef WOLFSSL_SMALL_STACK
  26152. ed448_key *key3 = NULL;
  26153. #else
  26154. ed448_key key3[1];
  26155. #endif
  26156. #endif
  26157. #ifdef WOLFSSL_SMALL_STACK
  26158. key = (ed448_key *)XMALLOC(sizeof(*key), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26159. key2 = (ed448_key *)XMALLOC(sizeof(*key2), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26160. #if !defined(NO_ASN) && defined(HAVE_ED448_SIGN)
  26161. key3 = (ed448_key *)XMALLOC(sizeof(*key3), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26162. #endif
  26163. #endif
  26164. /* create ed448 keys */
  26165. #ifndef HAVE_FIPS
  26166. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  26167. #else
  26168. ret = wc_InitRng(&rng);
  26169. #endif
  26170. if (ret != 0) {
  26171. XMEMSET(&rng, 0, sizeof(rng));
  26172. ERROR_OUT(-11700, out);
  26173. }
  26174. if (wc_ed448_init(key) < 0)
  26175. ERROR_OUT(-11903, out);
  26176. if (wc_ed448_init(key2) < 0)
  26177. ERROR_OUT(-11904, out);
  26178. #if !defined(NO_ASN) && defined(HAVE_ED448_SIGN)
  26179. if (wc_ed448_init(key3) < 0)
  26180. ERROR_OUT(-11905, out);
  26181. #endif
  26182. if (wc_ed448_make_key(&rng, ED448_KEY_SIZE, key) < 0)
  26183. ERROR_OUT(-11906, out);
  26184. if (wc_ed448_make_key(&rng, ED448_KEY_SIZE, key2) < 0)
  26185. ERROR_OUT(-11907, out);
  26186. /* helper functions for signature and key size */
  26187. keySz = wc_ed448_size(key);
  26188. sigSz = wc_ed448_sig_size(key);
  26189. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_EXPORT) &&\
  26190. defined(HAVE_ED448_KEY_IMPORT)
  26191. for (i = 0; i < 6; i++) {
  26192. outlen = sizeof(out);
  26193. XMEMSET(out, 0, sizeof(out));
  26194. if (wc_ed448_import_private_key(sKeys[i], ED448_KEY_SIZE, pKeys[i],
  26195. pKeySz[i], key) != 0)
  26196. ERROR_OUT(-11701 - i, out);
  26197. if (wc_ed448_sign_msg(msgs[i], msgSz[i], out, &outlen, key, NULL,
  26198. 0) != 0)
  26199. ERROR_OUT(-11711 - i, out);
  26200. if (XMEMCMP(out, sigs[i], 114))
  26201. ERROR_OUT(-11721 - i, out);
  26202. #if defined(HAVE_ED448_VERIFY)
  26203. /* test verify on good msg */
  26204. if (wc_ed448_verify_msg(out, outlen, msgs[i], msgSz[i], &verify, key,
  26205. NULL, 0) != 0 || verify != 1)
  26206. ERROR_OUT(-11731 - i, out);
  26207. #ifdef WOLFSSL_ED448_STREAMING_VERIFY
  26208. /* test verify on good msg using streaming interface directly */
  26209. if (wc_ed448_verify_msg_init(out, outlen,
  26210. key, (byte)Ed448, NULL, 0) != 0)
  26211. ERROR_OUT(-11911 - i, out);
  26212. for (j = 0; j < msgSz[i]; j += i) {
  26213. if (wc_ed448_verify_msg_update(msgs[i] + j, MIN(i, msgSz[i] - j), key) != 0)
  26214. ERROR_OUT(-11921 - i, out);
  26215. }
  26216. if (wc_ed448_verify_msg_final(out, outlen, &verify,
  26217. key) != 0 || verify != 1)
  26218. ERROR_OUT(-11931 - i, out);
  26219. #endif /* WOLFSSL_ED448_STREAMING_VERIFY */
  26220. /* test verify on bad msg */
  26221. out[outlen-2] = out[outlen-2] + 1;
  26222. if (wc_ed448_verify_msg(out, outlen, msgs[i], msgSz[i], &verify, key,
  26223. NULL, 0) == 0 || verify == 1)
  26224. ERROR_OUT(-11741 - i, out);
  26225. #endif /* HAVE_ED448_VERIFY */
  26226. /* test api for import/exporting keys */
  26227. {
  26228. byte *exportPKey = NULL;
  26229. byte *exportSKey = NULL;
  26230. word32 exportPSz = ED448_KEY_SIZE;
  26231. word32 exportSSz = ED448_KEY_SIZE;
  26232. exportPKey = (byte *)XMALLOC(exportPSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26233. exportSKey = (byte *)XMALLOC(exportSSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26234. if ((exportPKey == NULL) || (exportSKey == NULL))
  26235. ERROR_OUT(-11902, out);
  26236. ret = 0;
  26237. do {
  26238. if (wc_ed448_export_public(key, exportPKey, &exportPSz) != 0) {
  26239. ret = -11751 - i;
  26240. break;
  26241. }
  26242. if (wc_ed448_import_public_ex(exportPKey, exportPSz, key2, 1) != 0) {
  26243. ret = -11761 - i;
  26244. break;
  26245. }
  26246. if (wc_ed448_export_private_only(key, exportSKey, &exportSSz) != 0) {
  26247. ret = -11771 - i;
  26248. break;
  26249. }
  26250. if (wc_ed448_import_private_key(exportSKey, exportSSz,
  26251. exportPKey, exportPSz, key2) != 0) {
  26252. ret = -11781 - i;
  26253. break;
  26254. }
  26255. /* clear "out" buffer and test sign with imported keys */
  26256. outlen = sizeof(out);
  26257. XMEMSET(out, 0, sizeof(out));
  26258. if (wc_ed448_sign_msg(msgs[i], msgSz[i], out, &outlen, key2, NULL,
  26259. 0) != 0) {
  26260. ret = -11791 - i;
  26261. break;
  26262. }
  26263. } while(0);
  26264. XFREE(exportPKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26265. XFREE(exportSKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26266. if (ret != 0)
  26267. goto out;
  26268. }
  26269. #if defined(HAVE_ED448_VERIFY)
  26270. if (wc_ed448_verify_msg(out, outlen, msgs[i], msgSz[i], &verify, key2,
  26271. NULL, 0) != 0 || verify != 1)
  26272. ERROR_OUT(-11801 - i, out);
  26273. if (XMEMCMP(out, sigs[i], SIGSZ))
  26274. ERROR_OUT(-11811 - i, out);
  26275. #endif /* HAVE_ED448_VERIFY */
  26276. }
  26277. ret = ed448_ctx_test();
  26278. if (ret != 0)
  26279. goto out;
  26280. ret = ed448ph_test();
  26281. if (ret != 0)
  26282. goto out;
  26283. #ifndef NO_ASN
  26284. /* Try ASN.1 encoded private-only key and public key. */
  26285. idx = 0;
  26286. if (wc_Ed448PrivateKeyDecode(privateEd448, &idx, key3,
  26287. sizeof(privateEd448)) != 0)
  26288. ERROR_OUT(-11821, out);
  26289. if (wc_ed448_sign_msg(msgs[0], msgSz[0], out, &outlen, key3, NULL, 0)
  26290. != BAD_FUNC_ARG)
  26291. ERROR_OUT(-11831, out);
  26292. idx = 0;
  26293. if (wc_Ed448PublicKeyDecode(publicEd448, &idx, key3,
  26294. sizeof(publicEd448)) != 0)
  26295. ERROR_OUT(-11841, out);
  26296. if (wc_ed448_sign_msg(msgs[0], msgSz[0], out, &outlen, key3, NULL, 0) != 0)
  26297. ERROR_OUT(-11851, out);
  26298. if (XMEMCMP(out, sigs[0], SIGSZ))
  26299. ERROR_OUT(-11861, out);
  26300. #if defined(HAVE_ED448_VERIFY)
  26301. /* test verify on good msg */
  26302. if (wc_ed448_verify_msg(out, outlen, msgs[0], msgSz[0], &verify, key3,
  26303. NULL, 0) != 0 || verify != 1)
  26304. ERROR_OUT(-11871, out);
  26305. #endif /* HAVE_ED448_VERIFY */
  26306. wc_ed448_free(key3);
  26307. if (wc_ed448_init(key3) < 0)
  26308. ERROR_OUT(-11908, out);
  26309. idx = 0;
  26310. if (wc_Ed448PrivateKeyDecode(privPubEd448, &idx, key3,
  26311. sizeof(privPubEd448)) != 0)
  26312. ERROR_OUT(-11881, out);
  26313. if (wc_ed448_sign_msg(msgs[0], msgSz[0], out, &outlen, key3, NULL, 0) != 0)
  26314. ERROR_OUT(-11891, out);
  26315. if (XMEMCMP(out, sigs[0], SIGSZ))
  26316. ERROR_OUT(-11901, out);
  26317. #endif /* NO_ASN */
  26318. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_EXPORT && HAVE_ED448_KEY_IMPORT */
  26319. ret = 0;
  26320. out:
  26321. #ifdef WOLFSSL_SMALL_STACK
  26322. if (key) {
  26323. wc_ed448_free(key);
  26324. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26325. }
  26326. if (key2) {
  26327. wc_ed448_free(key2);
  26328. XFREE(key2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26329. }
  26330. #if !defined(NO_ASN) && defined(HAVE_ED448_SIGN)
  26331. if (key3) {
  26332. wc_ed448_free(key3);
  26333. XFREE(key3, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26334. }
  26335. #endif
  26336. #else
  26337. wc_ed448_free(key);
  26338. wc_ed448_free(key2);
  26339. #if !defined(NO_ASN) && defined(HAVE_ED448_SIGN)
  26340. wc_ed448_free(key3);
  26341. #endif
  26342. #endif
  26343. #if defined(HAVE_HASHDRBG) || defined(NO_RC4)
  26344. wc_FreeRng(&rng);
  26345. #endif
  26346. if (ret < 0)
  26347. return ret;
  26348. /* hush warnings of unused keySz and sigSz */
  26349. (void)keySz;
  26350. (void)sigSz;
  26351. #ifdef WOLFSSL_TEST_CERT
  26352. ret = ed448_test_cert();
  26353. if (ret < 0)
  26354. return ret;
  26355. #ifdef WOLFSSL_CERT_GEN
  26356. ret = ed448_test_make_cert();
  26357. if (ret < 0)
  26358. return ret;
  26359. #endif /* WOLFSSL_CERT_GEN */
  26360. #endif /* WOLFSSL_TEST_CERT */
  26361. return 0;
  26362. }
  26363. #endif /* HAVE_ED448 */
  26364. #ifdef WOLFCRYPT_HAVE_ECCSI
  26365. static int eccsi_api_test(WC_RNG* rng, EccsiKey* key, mp_int* ssk,
  26366. ecc_point* pvt)
  26367. {
  26368. int ret;
  26369. byte id[1] = { 0x00 };
  26370. int valid;
  26371. word32 sz;
  26372. byte data[256];
  26373. byte hash[WC_MAX_DIGEST_SIZE];
  26374. byte hashSz;
  26375. byte sig[257];
  26376. word32 sigSz;
  26377. ret = wc_InitEccsiKey_ex(NULL, 32, ECC_SECP256R1, HEAP_HINT, INVALID_DEVID);
  26378. if (ret != BAD_FUNC_ARG)
  26379. return -10023;
  26380. ret = wc_InitEccsiKey_ex(NULL, 32, ECC_SECP256R1, HEAP_HINT, INVALID_DEVID);
  26381. if (ret != BAD_FUNC_ARG)
  26382. return -10024;
  26383. ret = wc_InitEccsiKey(NULL, NULL, INVALID_DEVID);
  26384. if (ret != BAD_FUNC_ARG)
  26385. return -10025;
  26386. ret = wc_InitEccsiKey(NULL, HEAP_HINT, INVALID_DEVID);
  26387. if (ret != BAD_FUNC_ARG)
  26388. return -10026;
  26389. wc_FreeEccsiKey(NULL);
  26390. /* Create a valid key. */
  26391. ret = wc_InitEccsiKey(key, NULL, INVALID_DEVID);
  26392. if (ret != 0)
  26393. return -10027;
  26394. ret = wc_MakeEccsiKey(NULL, NULL);
  26395. if (ret != BAD_FUNC_ARG)
  26396. return -10028;
  26397. ret = wc_MakeEccsiKey(key, NULL);
  26398. if (ret != BAD_FUNC_ARG)
  26399. return -10029;
  26400. ret = wc_MakeEccsiKey(NULL, rng);
  26401. if (ret != BAD_FUNC_ARG)
  26402. return -10030;
  26403. ret = wc_MakeEccsiPair(NULL, NULL, WC_HASH_TYPE_SHA256, NULL, 1, NULL,
  26404. NULL);
  26405. if (ret != BAD_FUNC_ARG)
  26406. return -10031;
  26407. ret = wc_MakeEccsiPair(key, rng, WC_HASH_TYPE_SHA256, id, 1, ssk, NULL);
  26408. if (ret != BAD_FUNC_ARG)
  26409. return -10032;
  26410. ret = wc_MakeEccsiPair(key, rng, WC_HASH_TYPE_SHA256, id, 1, NULL, pvt);
  26411. if (ret != BAD_FUNC_ARG)
  26412. return -10033;
  26413. ret = wc_MakeEccsiPair(key, rng, WC_HASH_TYPE_SHA256, NULL, 1, ssk, pvt);
  26414. if (ret != BAD_FUNC_ARG)
  26415. return -10034;
  26416. ret = wc_MakeEccsiPair(key, NULL, WC_HASH_TYPE_SHA256, id, 1, ssk, pvt);
  26417. if (ret != BAD_FUNC_ARG)
  26418. return -10035;
  26419. ret = wc_MakeEccsiPair(NULL, rng, WC_HASH_TYPE_SHA256, id, 1, ssk, pvt);
  26420. if (ret != BAD_FUNC_ARG)
  26421. return -10036;
  26422. /* No key set */
  26423. ret = wc_MakeEccsiPair(key, rng, WC_HASH_TYPE_SHA256, id, 1, ssk, pvt);
  26424. if (ret != BAD_STATE_E)
  26425. return -10037;
  26426. ret = wc_ValidateEccsiPair(NULL, WC_HASH_TYPE_SHA256, NULL, 1, NULL, NULL,
  26427. NULL);
  26428. if (ret != BAD_FUNC_ARG)
  26429. return -10038;
  26430. ret = wc_ValidateEccsiPair(key, WC_HASH_TYPE_SHA256, id, 1, ssk, pvt,
  26431. NULL);
  26432. if (ret != BAD_FUNC_ARG)
  26433. return -10039;
  26434. ret = wc_ValidateEccsiPair(key, WC_HASH_TYPE_SHA256, id, 1, ssk, NULL,
  26435. &valid);
  26436. if (ret != BAD_FUNC_ARG)
  26437. return -10040;
  26438. ret = wc_ValidateEccsiPair(key, WC_HASH_TYPE_SHA256, id, 1, NULL, pvt,
  26439. &valid);
  26440. if (ret != BAD_FUNC_ARG)
  26441. return -10041;
  26442. ret = wc_ValidateEccsiPair(key, WC_HASH_TYPE_SHA256, NULL, 1, ssk, pvt,
  26443. &valid);
  26444. if (ret != BAD_FUNC_ARG)
  26445. return -10042;
  26446. ret = wc_ValidateEccsiPair(NULL, WC_HASH_TYPE_SHA256, id, 1, ssk, pvt,
  26447. &valid);
  26448. if (ret != BAD_FUNC_ARG)
  26449. return -10043;
  26450. /* No key set */
  26451. ret = wc_ValidateEccsiPair(key, WC_HASH_TYPE_SHA256, id, 1, ssk, pvt,
  26452. &valid);
  26453. if (ret != BAD_STATE_E)
  26454. return -10044;
  26455. ret = wc_ValidateEccsiPvt(NULL, NULL, NULL);
  26456. if (ret != BAD_FUNC_ARG)
  26457. return -10045;
  26458. ret = wc_ValidateEccsiPvt(key, NULL, NULL);
  26459. if (ret != BAD_FUNC_ARG)
  26460. return -10046;
  26461. ret = wc_ValidateEccsiPvt(NULL, pvt, NULL);
  26462. if (ret != BAD_FUNC_ARG)
  26463. return -10047;
  26464. ret = wc_ValidateEccsiPvt(NULL, NULL, &valid);
  26465. if (ret != BAD_FUNC_ARG)
  26466. return -10048;
  26467. ret = wc_ValidateEccsiPvt(key, pvt, NULL);
  26468. if (ret != BAD_FUNC_ARG)
  26469. return -10049;
  26470. ret = wc_ValidateEccsiPvt(key, NULL, &valid);
  26471. if (ret != BAD_FUNC_ARG)
  26472. return -10050;
  26473. ret = wc_ValidateEccsiPvt(NULL, pvt, &valid);
  26474. if (ret != BAD_FUNC_ARG)
  26475. return -10051;
  26476. ret = wc_EncodeEccsiPair(NULL, NULL, NULL, data, NULL);
  26477. if (ret != BAD_FUNC_ARG)
  26478. return -10052;
  26479. ret = wc_EncodeEccsiPair(key, ssk, pvt, data, NULL);
  26480. if (ret != BAD_FUNC_ARG)
  26481. return -10053;
  26482. ret = wc_EncodeEccsiPair(key, ssk, NULL, data, &sz);
  26483. if (ret != BAD_FUNC_ARG)
  26484. return -10054;
  26485. ret = wc_EncodeEccsiPair(key, NULL, pvt, data, &sz);
  26486. if (ret != BAD_FUNC_ARG)
  26487. return -10055;
  26488. ret = wc_EncodeEccsiPair(NULL, ssk, pvt, data, &sz);
  26489. if (ret != BAD_FUNC_ARG)
  26490. return -10056;
  26491. /* No key created so no curve information. */
  26492. ret = wc_EncodeEccsiPair(key, ssk, pvt, NULL, &sz);
  26493. if (ret != LENGTH_ONLY_E)
  26494. return -10057;
  26495. ret = wc_EncodeEccsiSsk(NULL, NULL, data, NULL);
  26496. if (ret != BAD_FUNC_ARG)
  26497. return -10058;
  26498. ret = wc_EncodeEccsiSsk(key, ssk, data, NULL);
  26499. if (ret != BAD_FUNC_ARG)
  26500. return -10059;
  26501. ret = wc_EncodeEccsiSsk(key, NULL, data, &sz);
  26502. if (ret != BAD_FUNC_ARG)
  26503. return -10060;
  26504. ret = wc_EncodeEccsiSsk(NULL, ssk, data, &sz);
  26505. if (ret != BAD_FUNC_ARG)
  26506. return -10061;
  26507. ret = wc_EncodeEccsiPvt(NULL, NULL, data, NULL, 1);
  26508. if (ret != BAD_FUNC_ARG)
  26509. return -10058;
  26510. ret = wc_EncodeEccsiPvt(key, pvt, data, NULL, 1);
  26511. if (ret != BAD_FUNC_ARG)
  26512. return -10059;
  26513. ret = wc_EncodeEccsiPvt(key, NULL, data, &sz, 1);
  26514. if (ret != BAD_FUNC_ARG)
  26515. return -10060;
  26516. ret = wc_EncodeEccsiPvt(NULL, pvt, data, &sz, 1);
  26517. if (ret != BAD_FUNC_ARG)
  26518. return -10061;
  26519. ret = wc_DecodeEccsiPair(NULL, NULL, 0, NULL, NULL);
  26520. if (ret != BAD_FUNC_ARG)
  26521. return -10062;
  26522. ret = wc_DecodeEccsiPair(key, data, 0, ssk, NULL);
  26523. if (ret != BAD_FUNC_ARG)
  26524. return -10063;
  26525. ret = wc_DecodeEccsiPair(key, data, 0, NULL, pvt);
  26526. if (ret != BAD_FUNC_ARG)
  26527. return -10064;
  26528. ret = wc_DecodeEccsiPair(key, NULL, 0, ssk, pvt);
  26529. if (ret != BAD_FUNC_ARG)
  26530. return -10065;
  26531. ret = wc_DecodeEccsiPair(NULL, data, 0, ssk, pvt);
  26532. if (ret != BAD_FUNC_ARG)
  26533. return -10066;
  26534. ret = wc_DecodeEccsiSsk(NULL, NULL, 0, NULL);
  26535. if (ret != BAD_FUNC_ARG)
  26536. return -10067;
  26537. ret = wc_DecodeEccsiSsk(key, data, 0, NULL);
  26538. if (ret != BAD_FUNC_ARG)
  26539. return -10068;
  26540. ret = wc_DecodeEccsiSsk(key, NULL, 0, ssk);
  26541. if (ret != BAD_FUNC_ARG)
  26542. return -10069;
  26543. ret = wc_DecodeEccsiSsk(NULL, data, 0, ssk);
  26544. if (ret != BAD_FUNC_ARG)
  26545. return -10070;
  26546. ret = wc_DecodeEccsiPvt(NULL, NULL, 0, NULL);
  26547. if (ret != BAD_FUNC_ARG)
  26548. return -10067;
  26549. ret = wc_DecodeEccsiPvt(key, data, 0, NULL);
  26550. if (ret != BAD_FUNC_ARG)
  26551. return -10068;
  26552. ret = wc_DecodeEccsiPvt(key, NULL, 0, pvt);
  26553. if (ret != BAD_FUNC_ARG)
  26554. return -10069;
  26555. ret = wc_DecodeEccsiPvt(NULL, data, 0, pvt);
  26556. if (ret != BAD_FUNC_ARG)
  26557. return -10070;
  26558. ret = wc_DecodeEccsiPvtFromSig(NULL, NULL, 0, NULL);
  26559. if (ret != BAD_FUNC_ARG)
  26560. return -10067;
  26561. ret = wc_DecodeEccsiPvtFromSig(key, data, 0, NULL);
  26562. if (ret != BAD_FUNC_ARG)
  26563. return -10068;
  26564. ret = wc_DecodeEccsiPvtFromSig(key, NULL, 0, pvt);
  26565. if (ret != BAD_FUNC_ARG)
  26566. return -10069;
  26567. ret = wc_DecodeEccsiPvtFromSig(NULL, data, 0, pvt);
  26568. if (ret != BAD_FUNC_ARG)
  26569. return -10070;
  26570. ret = wc_ExportEccsiKey(NULL, data, NULL);
  26571. if (ret != BAD_FUNC_ARG)
  26572. return -10071;
  26573. ret = wc_ExportEccsiKey(key, data, NULL);
  26574. if (ret != BAD_FUNC_ARG)
  26575. return -10072;
  26576. ret = wc_ExportEccsiKey(NULL, data, &sz);
  26577. if (ret != BAD_FUNC_ARG)
  26578. return -10073;
  26579. /* No key to export */
  26580. ret = wc_ExportEccsiKey(key, NULL, &sz);
  26581. if (ret != BAD_STATE_E)
  26582. return -10074;
  26583. ret = wc_ImportEccsiKey(NULL, NULL, 0);
  26584. if (ret != BAD_FUNC_ARG)
  26585. return -10075;
  26586. ret = wc_ImportEccsiKey(key, NULL, 0);
  26587. if (ret != BAD_FUNC_ARG)
  26588. return -10076;
  26589. ret = wc_ImportEccsiKey(NULL, data, 0);
  26590. if (ret != BAD_FUNC_ARG)
  26591. return -10077;
  26592. ret = wc_ExportEccsiPrivateKey(NULL, data, NULL);
  26593. if (ret != BAD_FUNC_ARG)
  26594. return -10071;
  26595. ret = wc_ExportEccsiPrivateKey(key, data, NULL);
  26596. if (ret != BAD_FUNC_ARG)
  26597. return -10072;
  26598. ret = wc_ExportEccsiPrivateKey(NULL, data, &sz);
  26599. if (ret != BAD_FUNC_ARG)
  26600. return -10073;
  26601. /* No key to export */
  26602. ret = wc_ExportEccsiPrivateKey(key, NULL, &sz);
  26603. if (ret != BAD_STATE_E)
  26604. return -10074;
  26605. ret = wc_ImportEccsiPrivateKey(NULL, NULL, 0);
  26606. if (ret != BAD_FUNC_ARG)
  26607. return -10075;
  26608. ret = wc_ImportEccsiPrivateKey(key, NULL, 0);
  26609. if (ret != BAD_FUNC_ARG)
  26610. return -10076;
  26611. ret = wc_ImportEccsiPrivateKey(NULL, data, 0);
  26612. if (ret != BAD_FUNC_ARG)
  26613. return -10077;
  26614. ret = wc_ExportEccsiPublicKey(NULL, data, NULL, 1);
  26615. if (ret != BAD_FUNC_ARG)
  26616. return -10078;
  26617. ret = wc_ExportEccsiPublicKey(key, data, NULL, 1);
  26618. if (ret != BAD_FUNC_ARG)
  26619. return -10079;
  26620. ret = wc_ExportEccsiPublicKey(NULL, data, &sz, 1);
  26621. if (ret != BAD_FUNC_ARG)
  26622. return -10080;
  26623. /* No key to export */
  26624. ret = wc_ExportEccsiPublicKey(key, data, &sz, 1);
  26625. if (ret != BAD_STATE_E)
  26626. return -10081;
  26627. ret = wc_ImportEccsiPublicKey(NULL, NULL, 0, 1);
  26628. if (ret != BAD_FUNC_ARG)
  26629. return -10082;
  26630. ret = wc_ImportEccsiPublicKey(key, NULL, 0, 1);
  26631. if (ret != BAD_FUNC_ARG)
  26632. return -10083;
  26633. ret = wc_ImportEccsiPublicKey(NULL, data, 0, 1);
  26634. if (ret != BAD_FUNC_ARG)
  26635. return -10084;
  26636. ret = wc_HashEccsiId(NULL, WC_HASH_TYPE_SHA256, NULL, 1, NULL, NULL, NULL);
  26637. if (ret != BAD_FUNC_ARG)
  26638. return -10085;
  26639. ret = wc_HashEccsiId(key, WC_HASH_TYPE_SHA256, id, 1, pvt, hash, NULL);
  26640. if (ret != BAD_FUNC_ARG)
  26641. return -10086;
  26642. ret = wc_HashEccsiId(key, WC_HASH_TYPE_SHA256, id, 1, pvt, NULL, &hashSz);
  26643. if (ret != BAD_FUNC_ARG)
  26644. return -10087;
  26645. ret = wc_HashEccsiId(key, WC_HASH_TYPE_SHA256, id, 1, NULL, hash, &hashSz);
  26646. if (ret != BAD_FUNC_ARG)
  26647. return -10088;
  26648. ret = wc_HashEccsiId(key, WC_HASH_TYPE_SHA256, NULL, 1, pvt, hash,
  26649. &hashSz);
  26650. if (ret != BAD_FUNC_ARG)
  26651. return -10089;
  26652. ret = wc_HashEccsiId(NULL, WC_HASH_TYPE_SHA256, id, 1, pvt, hash, &hashSz);
  26653. if (ret != BAD_FUNC_ARG)
  26654. return -10090;
  26655. ret = wc_HashEccsiId(key, WC_HASH_TYPE_SHA256, id, 1, pvt, hash, &hashSz);
  26656. if (ret != BAD_STATE_E)
  26657. return -10091;
  26658. ret = wc_SetEccsiHash(NULL, NULL, 1);
  26659. if (ret != BAD_FUNC_ARG)
  26660. return -10090;
  26661. ret = wc_SetEccsiHash(key, NULL, 1);
  26662. if (ret != BAD_FUNC_ARG)
  26663. return -10090;
  26664. ret = wc_SetEccsiHash(NULL, hash, 1);
  26665. if (ret != BAD_FUNC_ARG)
  26666. return -10090;
  26667. ret = wc_SetEccsiPair(NULL, NULL, NULL);
  26668. if (ret != BAD_FUNC_ARG)
  26669. return -10090;
  26670. ret = wc_SetEccsiPair(key, NULL, NULL);
  26671. if (ret != BAD_FUNC_ARG)
  26672. return -10090;
  26673. ret = wc_SetEccsiPair(NULL, ssk, NULL);
  26674. if (ret != BAD_FUNC_ARG)
  26675. return -10090;
  26676. ret = wc_SetEccsiPair(NULL, NULL, pvt);
  26677. if (ret != BAD_FUNC_ARG)
  26678. return -10090;
  26679. ret = wc_SetEccsiPair(key, ssk, NULL);
  26680. if (ret != BAD_FUNC_ARG)
  26681. return -10090;
  26682. ret = wc_SetEccsiPair(key, NULL, pvt);
  26683. if (ret != BAD_FUNC_ARG)
  26684. return -10090;
  26685. ret = wc_SetEccsiPair(NULL, ssk, pvt);
  26686. if (ret != BAD_FUNC_ARG)
  26687. return -10090;
  26688. ret = wc_SignEccsiHash(NULL, NULL, WC_HASH_TYPE_SHA256, NULL, 0, sig, NULL);
  26689. if (ret != BAD_FUNC_ARG)
  26690. return -10092;
  26691. ret = wc_SignEccsiHash(key, rng, WC_HASH_TYPE_SHA256, data, 0, sig, NULL);
  26692. if (ret != BAD_FUNC_ARG)
  26693. return -10093;
  26694. ret = wc_SignEccsiHash(key, rng, WC_HASH_TYPE_SHA256, NULL, 0, sig,
  26695. &sigSz);
  26696. if (ret != BAD_FUNC_ARG)
  26697. return -10096;
  26698. ret = wc_SignEccsiHash(key, NULL, WC_HASH_TYPE_SHA256, data, 0, sig,
  26699. &sigSz);
  26700. if (ret != BAD_FUNC_ARG)
  26701. return -10098;
  26702. ret = wc_SignEccsiHash(NULL, rng, WC_HASH_TYPE_SHA256, data, 0, sig,
  26703. &sigSz);
  26704. if (ret != BAD_FUNC_ARG)
  26705. return -10099;
  26706. /* Key not set. */
  26707. ret = wc_SignEccsiHash(key, rng, WC_HASH_TYPE_SHA256, data, 0, NULL,
  26708. &sigSz);
  26709. if (ret != BAD_STATE_E)
  26710. return -10100;
  26711. ret = wc_VerifyEccsiHash(NULL, WC_HASH_TYPE_SHA256, NULL, 0, NULL, 0, NULL);
  26712. if (ret != BAD_FUNC_ARG)
  26713. return -10101;
  26714. ret = wc_VerifyEccsiHash(key, WC_HASH_TYPE_SHA256, NULL, 0, NULL, 0, NULL);
  26715. if (ret != BAD_FUNC_ARG)
  26716. return -10101;
  26717. ret = wc_VerifyEccsiHash(NULL, WC_HASH_TYPE_SHA256, data, 0, NULL, 0, NULL);
  26718. if (ret != BAD_FUNC_ARG)
  26719. return -10101;
  26720. ret = wc_VerifyEccsiHash(NULL, WC_HASH_TYPE_SHA256, NULL, 0, sig, 0, NULL);
  26721. if (ret != BAD_FUNC_ARG)
  26722. return -10101;
  26723. ret = wc_VerifyEccsiHash(NULL, WC_HASH_TYPE_SHA256, NULL, 0, NULL, 0,
  26724. &valid);
  26725. if (ret != BAD_FUNC_ARG)
  26726. return -10101;
  26727. ret = wc_VerifyEccsiHash(key, WC_HASH_TYPE_SHA256, data, 0, sig, 0, NULL);
  26728. if (ret != BAD_FUNC_ARG)
  26729. return -10102;
  26730. ret = wc_VerifyEccsiHash(key, WC_HASH_TYPE_SHA256, data, 0, NULL, 0,
  26731. &valid);
  26732. if (ret != BAD_FUNC_ARG)
  26733. return -10103;
  26734. ret = wc_VerifyEccsiHash(key, WC_HASH_TYPE_SHA256, NULL, 0, sig, 0,
  26735. &valid);
  26736. if (ret != BAD_FUNC_ARG)
  26737. return -10104;
  26738. ret = wc_VerifyEccsiHash(NULL, WC_HASH_TYPE_SHA256, data, 0, sig, 0,
  26739. &valid);
  26740. if (ret != BAD_FUNC_ARG)
  26741. return -10106;
  26742. ret = wc_VerifyEccsiHash(key, WC_HASH_TYPE_SHA256, data, 0, sig, 0,
  26743. &valid);
  26744. if (ret != BAD_STATE_E)
  26745. return -10106;
  26746. ret = wc_SetEccsiPair(key, ssk, pvt);
  26747. if (ret != 0)
  26748. return -10107;
  26749. /* Identity hash not set. */
  26750. ret = wc_SignEccsiHash(key, rng, WC_HASH_TYPE_SHA256, data, 0, NULL,
  26751. &sigSz);
  26752. if (ret != BAD_STATE_E)
  26753. return -10108;
  26754. wc_FreeEccsiKey(key);
  26755. return 0;
  26756. }
  26757. /* RFC 6507: Appendix A */
  26758. static int eccsi_kat_verify_test(EccsiKey* key, ecc_point* pvt)
  26759. {
  26760. int ret;
  26761. int verified;
  26762. const byte msg[] = { 0x6D, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x00 };
  26763. word32 msgSz = sizeof(msg);
  26764. byte hash[WC_SHA256_DIGEST_SIZE];
  26765. byte hashSz = WC_SHA256_DIGEST_SIZE;
  26766. static const byte id[] = {
  26767. 0x32, 0x30, 0x31, 0x31, 0x2D, 0x30, 0x32, 0x00,
  26768. 0x74, 0x65, 0x6C, 0x3A, 0x2B, 0x34, 0x34, 0x37,
  26769. 0x37, 0x30, 0x30, 0x39, 0x30, 0x30, 0x31, 0x32,
  26770. 0x33, 0x00
  26771. };
  26772. word32 idSz = sizeof(id);
  26773. static const byte sig[] = {
  26774. 0x26, 0x9D, 0x4C, 0x8F, 0xDE, 0xB6, 0x6A, 0x74,
  26775. 0xE4, 0xEF, 0x8C, 0x0D, 0x5D, 0xCC, 0x59, 0x7D,
  26776. 0xDF, 0xE6, 0x02, 0x9C, 0x2A, 0xFF, 0xC4, 0x93,
  26777. 0x60, 0x08, 0xCD, 0x2C, 0xC1, 0x04, 0x5D, 0x81,
  26778. 0xE0, 0x9B, 0x52, 0x8D, 0x0E, 0xF8, 0xD6, 0xDF,
  26779. 0x1A, 0xA3, 0xEC, 0xBF, 0x80, 0x11, 0x0C, 0xFC,
  26780. 0xEC, 0x9F, 0xC6, 0x82, 0x52, 0xCE, 0xBB, 0x67,
  26781. 0x9F, 0x41, 0x34, 0x84, 0x69, 0x40, 0xCC, 0xFD,
  26782. 0x04,
  26783. 0x75, 0x8A, 0x14, 0x27, 0x79, 0xBE, 0x89, 0xE8,
  26784. 0x29, 0xE7, 0x19, 0x84, 0xCB, 0x40, 0xEF, 0x75,
  26785. 0x8C, 0xC4, 0xAD, 0x77, 0x5F, 0xC5, 0xB9, 0xA3,
  26786. 0xE1, 0xC8, 0xED, 0x52, 0xF6, 0xFA, 0x36, 0xD9,
  26787. 0xA7, 0x9D, 0x24, 0x76, 0x92, 0xF4, 0xED, 0xA3,
  26788. 0xA6, 0xBD, 0xAB, 0x77, 0xD6, 0xAA, 0x64, 0x74,
  26789. 0xA4, 0x64, 0xAE, 0x49, 0x34, 0x66, 0x3C, 0x52,
  26790. 0x65, 0xBA, 0x70, 0x18, 0xBA, 0x09, 0x1F, 0x79
  26791. };
  26792. word32 sigSz = sizeof(sig);
  26793. static const byte pubData[] = {
  26794. 0x50, 0xD4, 0x67, 0x0B, 0xDE, 0x75, 0x24, 0x4F,
  26795. 0x28, 0xD2, 0x83, 0x8A, 0x0D, 0x25, 0x55, 0x8A,
  26796. 0x7A, 0x72, 0x68, 0x6D, 0x45, 0x22, 0xD4, 0xC8,
  26797. 0x27, 0x3F, 0xB6, 0x44, 0x2A, 0xEB, 0xFA, 0x93,
  26798. 0xDB, 0xDD, 0x37, 0x55, 0x1A, 0xFD, 0x26, 0x3B,
  26799. 0x5D, 0xFD, 0x61, 0x7F, 0x39, 0x60, 0xC6, 0x5A,
  26800. 0x8C, 0x29, 0x88, 0x50, 0xFF, 0x99, 0xF2, 0x03,
  26801. 0x66, 0xDC, 0xE7, 0xD4, 0x36, 0x72, 0x17, 0xF4
  26802. };
  26803. static const byte expHash[] = {
  26804. 0x49, 0x0f, 0x3f, 0xeb, 0xbc, 0x1c, 0x90, 0x2f,
  26805. 0x62, 0x89, 0x72, 0x3d, 0x7f, 0x8c, 0xbf, 0x79,
  26806. 0xdb, 0x88, 0x93, 0x08, 0x49, 0xd1, 0x9f, 0x38,
  26807. 0xf0, 0x29, 0x5b, 0x5c, 0x27, 0x6c, 0x14, 0xd1
  26808. };
  26809. ret = wc_ImportEccsiPublicKey(key, pubData, sizeof(pubData), 0);
  26810. if (ret != 0)
  26811. return -10108;
  26812. ret = wc_DecodeEccsiPvtFromSig(key, sig, sigSz, pvt);
  26813. if (ret != 0)
  26814. return -10109;
  26815. ret = wc_HashEccsiId(key, WC_HASH_TYPE_SHA256, id, idSz, pvt, hash,
  26816. &hashSz);
  26817. if (ret != 0)
  26818. return -10112;
  26819. if (hashSz != sizeof(expHash))
  26820. return -10113;
  26821. if (XMEMCMP(hash, expHash, hashSz) != 0)
  26822. return -10114;
  26823. ret = wc_SetEccsiHash(key, hash, hashSz);
  26824. if (ret != 0)
  26825. return -10112;
  26826. ret = wc_VerifyEccsiHash(key, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  26827. &verified);
  26828. if (ret != 0)
  26829. return -10115;
  26830. if (!verified)
  26831. return -10116;
  26832. return 0;
  26833. }
  26834. static int eccsi_enc_dec_pair_test(EccsiKey* priv, mp_int* ssk, ecc_point* pvt)
  26835. {
  26836. int ret;
  26837. byte data[32 * 3];
  26838. word32 sz;
  26839. ecc_point* decPvt = NULL;
  26840. #ifdef WOLFSSL_SMALL_STACK
  26841. mp_int *decSsk = (mp_int *)XMALLOC(sizeof(*decSsk), HEAP_HINT,
  26842. DYNAMIC_TYPE_TMP_BUFFER);
  26843. if (decSsk == NULL)
  26844. return -10173;
  26845. #else
  26846. mp_int decSsk[1];
  26847. #endif
  26848. ret = mp_init(decSsk);
  26849. if (ret != 0)
  26850. ERROR_OUT(-10117, out);
  26851. decPvt = wc_ecc_new_point();
  26852. if (decPvt == NULL)
  26853. ERROR_OUT(-10118, out);
  26854. ret = wc_EncodeEccsiPair(priv, ssk, pvt, NULL, &sz);
  26855. if (ret != LENGTH_ONLY_E)
  26856. ERROR_OUT(-10119, out);
  26857. if (sz != 32 * 3)
  26858. ERROR_OUT(-10120, out);
  26859. ret = wc_EncodeEccsiPair(priv, ssk, pvt, data, &sz);
  26860. if (ret != 0)
  26861. ERROR_OUT(-10121, out);
  26862. if (sz != 32* 3)
  26863. ERROR_OUT(-10122, out);
  26864. ret = wc_DecodeEccsiPair(priv, data, sz, decSsk, decPvt);
  26865. if (ret != 0)
  26866. ERROR_OUT(-10123, out);
  26867. if (mp_cmp(ssk, decSsk) != MP_EQ)
  26868. ERROR_OUT(-10124, out);
  26869. if (wc_ecc_cmp_point(pvt, decPvt) != MP_EQ)
  26870. ERROR_OUT(-10125, out);
  26871. ret = wc_EncodeEccsiSsk(priv, ssk, NULL, &sz);
  26872. if (ret != LENGTH_ONLY_E)
  26873. ERROR_OUT(-10119, out);
  26874. if (sz != 32)
  26875. ERROR_OUT(-10120, out);
  26876. ret = wc_EncodeEccsiSsk(priv, ssk, data, &sz);
  26877. if (ret != 0)
  26878. ERROR_OUT(-10121, out);
  26879. if (sz != 32)
  26880. ERROR_OUT(-10122, out);
  26881. ret = wc_DecodeEccsiSsk(priv, data, sz, decSsk);
  26882. if (ret != 0)
  26883. ERROR_OUT(-10123, out);
  26884. if (mp_cmp(ssk, decSsk) != MP_EQ)
  26885. ERROR_OUT(-10124, out);
  26886. ret = wc_EncodeEccsiPvt(priv, pvt, NULL, &sz, 1);
  26887. if (ret != LENGTH_ONLY_E)
  26888. ERROR_OUT(-10126, out);
  26889. if (sz != 32 * 2)
  26890. ERROR_OUT(-10127, out);
  26891. ret = wc_EncodeEccsiPvt(priv, pvt, data, &sz, 1);
  26892. if (ret != 0)
  26893. ERROR_OUT(-10128, out);
  26894. if (sz != 32 * 2)
  26895. ERROR_OUT(-10129, out);
  26896. ret = wc_DecodeEccsiPvt(priv, data, sz, decPvt);
  26897. if (ret != 0)
  26898. ERROR_OUT(-10130, out);
  26899. if (wc_ecc_cmp_point(pvt, decPvt) != MP_EQ)
  26900. ERROR_OUT(-10131, out);
  26901. sz = sizeof(data);
  26902. ret = wc_EncodeEccsiPvt(priv, pvt, data, &sz, 0);
  26903. if (ret != 0)
  26904. ERROR_OUT(-10128, out);
  26905. if (sz != 32 * 2 + 1)
  26906. ERROR_OUT(-10129, out);
  26907. ret = wc_DecodeEccsiPvt(priv, data, sz, decPvt);
  26908. if (ret != 0)
  26909. ERROR_OUT(-10130, out);
  26910. if (wc_ecc_cmp_point(pvt, decPvt) != MP_EQ)
  26911. ERROR_OUT(-10131, out);
  26912. wc_ecc_del_point(decPvt);
  26913. out:
  26914. mp_free(decSsk);
  26915. #ifdef WOLFSSL_SMALL_STACK
  26916. XFREE(decSsk, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26917. #endif
  26918. return ret;
  26919. }
  26920. static int eccsi_imp_exp_key_test(EccsiKey* priv)
  26921. {
  26922. int ret;
  26923. byte data[32 * 3];
  26924. byte out[32 * 3];
  26925. word32 sz;
  26926. ret = wc_ExportEccsiKey(priv, NULL, &sz);
  26927. if (ret != LENGTH_ONLY_E)
  26928. return -10132;
  26929. if (sz != 32 * 3)
  26930. return -10133;
  26931. ret = wc_ExportEccsiKey(priv, data, &sz);
  26932. if (ret != 0)
  26933. return -10134;
  26934. ret = wc_ImportEccsiKey(priv, data, sz);
  26935. if (ret != 0)
  26936. return -10135;
  26937. ret = wc_ExportEccsiKey(priv, NULL, &sz);
  26938. if (ret != LENGTH_ONLY_E)
  26939. return -10132;
  26940. if (sz != 32 * 3)
  26941. return -10143;
  26942. ret = wc_ExportEccsiKey(priv, out, &sz);
  26943. if (ret != 0)
  26944. return -10144;
  26945. if (sz != 32 * 3)
  26946. return -10145;
  26947. if (XMEMCMP(data, out, sz) != 0)
  26948. return -10146;
  26949. ret = wc_ExportEccsiPrivateKey(priv, NULL, &sz);
  26950. if (ret != LENGTH_ONLY_E)
  26951. return -10156;
  26952. if (sz != 32)
  26953. return -10157;
  26954. ret = wc_ExportEccsiPrivateKey(priv, data, &sz);
  26955. if (ret != 0)
  26956. return -10158;
  26957. ret = wc_ImportEccsiPrivateKey(priv, data, sz);
  26958. if (ret != 0)
  26959. return -10159;
  26960. ret = wc_ExportEccsiPrivateKey(priv, NULL, &sz);
  26961. if (ret != LENGTH_ONLY_E)
  26962. return -10152;
  26963. if (sz != 32)
  26964. return -10163;
  26965. ret = wc_ExportEccsiPrivateKey(priv, out, &sz);
  26966. if (ret != 0)
  26967. return -10164;
  26968. if (sz != 32)
  26969. return -10165;
  26970. if (XMEMCMP(data, out, sz) != 0)
  26971. return -10166;
  26972. return 0;
  26973. }
  26974. static int eccsi_imp_exp_pubkey_test(EccsiKey* key1, EccsiKey* key2)
  26975. {
  26976. int ret;
  26977. byte data[32 * 2 + 1];
  26978. byte pubData[32 * 2 + 1];
  26979. word32 sz;
  26980. ret = wc_ExportEccsiPublicKey(key1, NULL, &sz, 1);
  26981. if (ret != LENGTH_ONLY_E)
  26982. return -10136;
  26983. if (sz != 32 * 2)
  26984. return -10137;
  26985. ret = wc_ExportEccsiPublicKey(key1, data, &sz, 1);
  26986. if (ret != 0)
  26987. return -10138;
  26988. ret = wc_ImportEccsiPublicKey(key2, data, sz, 1);
  26989. if (ret != 0)
  26990. return -10139;
  26991. sz = sizeof(pubData);
  26992. ret = wc_ExportEccsiPublicKey(key2, pubData, &sz, 1);
  26993. if (ret != 0)
  26994. return -10140;
  26995. if (sz != 32 * 2)
  26996. return -10141;
  26997. if (XMEMCMP(data, pubData, sz) != 0)
  26998. return -10142;
  26999. sz = sizeof(pubData);
  27000. ret = wc_ExportEccsiPublicKey(key2, pubData, &sz, 0);
  27001. if (ret != 0)
  27002. return -10140;
  27003. if (sz != 32 * 2 + 1)
  27004. return -10141;
  27005. if (pubData[0] != 0x04)
  27006. return -10140;
  27007. if (XMEMCMP(pubData + 1, data, sz - 1) != 0)
  27008. return -10142;
  27009. ret = wc_ImportEccsiPublicKey(key2, pubData, sz, 0);
  27010. if (ret != 0)
  27011. return -10139;
  27012. return 0;
  27013. }
  27014. static int eccsi_make_key_test(EccsiKey* priv, EccsiKey* pub, WC_RNG* rng,
  27015. mp_int* ssk, ecc_point* pvt)
  27016. {
  27017. int ret;
  27018. char mail[] = "test@wolfssl.com";
  27019. byte* id = (byte*)mail;
  27020. word32 idSz = (word32) XSTRLEN(mail);
  27021. int valid;
  27022. ret = wc_MakeEccsiKey(priv, rng);
  27023. if (ret != 0)
  27024. return -10143;
  27025. ret = eccsi_imp_exp_key_test(priv);
  27026. if (ret < 0)
  27027. return ret;
  27028. ret = eccsi_imp_exp_pubkey_test(priv, pub);
  27029. if (ret < 0)
  27030. return ret;
  27031. ret = wc_MakeEccsiPair(priv, rng, WC_HASH_TYPE_SHA256, id, idSz, ssk, pvt);
  27032. if (ret != 0)
  27033. return -10144;
  27034. ret = wc_ValidateEccsiPair(pub, WC_HASH_TYPE_SHA256, id, idSz, ssk, pvt,
  27035. &valid);
  27036. if (ret != 0)
  27037. return -10145;
  27038. if (!valid)
  27039. return -10146;
  27040. ret = eccsi_enc_dec_pair_test(priv, ssk, pvt);
  27041. if (ret != 0)
  27042. return ret;
  27043. return 0;
  27044. }
  27045. static int eccsi_sign_verify_test(EccsiKey* priv, EccsiKey* pub, WC_RNG* rng,
  27046. mp_int* ssk, ecc_point* pvt)
  27047. {
  27048. int ret;
  27049. byte hashPriv[WC_MAX_DIGEST_SIZE];
  27050. byte hashPub[WC_MAX_DIGEST_SIZE];
  27051. byte hashSz;
  27052. byte sig[144];
  27053. word32 sigSz;
  27054. int verified, valid;
  27055. char mail[] = "test@wolfssl.com";
  27056. byte* id = (byte*)mail;
  27057. word32 idSz = (word32) XSTRLEN(mail);
  27058. byte msg[] = { 0x00 };
  27059. word32 msgSz = sizeof(msg);
  27060. ret = wc_HashEccsiId(priv, WC_HASH_TYPE_SHA256, id, idSz, pvt, hashPriv,
  27061. &hashSz);
  27062. if (ret != 0)
  27063. return -10147;
  27064. if (hashSz != 32)
  27065. return -10148;
  27066. ret = wc_HashEccsiId(priv, WC_HASH_TYPE_SHA256, id, idSz, pvt, hashPub,
  27067. &hashSz);
  27068. if (ret != 0)
  27069. return -10149;
  27070. if (hashSz != 32)
  27071. return -10150;
  27072. if (XMEMCMP(hashPriv, hashPub, hashSz) != 0)
  27073. return -10151;
  27074. ret = wc_SetEccsiHash(priv, hashPriv, hashSz);
  27075. if (ret != 0)
  27076. return -10149;
  27077. ret = wc_SetEccsiPair(priv, ssk, pvt);
  27078. if (ret != 0)
  27079. return -10149;
  27080. ret = wc_SignEccsiHash(priv, rng, WC_HASH_TYPE_SHA256, msg, msgSz, NULL,
  27081. &sigSz);
  27082. if (ret != LENGTH_ONLY_E)
  27083. return -10152;
  27084. if (sigSz != 129)
  27085. return -10153;
  27086. ret = wc_SignEccsiHash(priv, rng, WC_HASH_TYPE_SHA256, msg, msgSz, sig,
  27087. &sigSz);
  27088. if (ret != 0)
  27089. return -10154;
  27090. ret = wc_SetEccsiHash(pub, hashPub, hashSz);
  27091. if (ret != 0)
  27092. return -10149;
  27093. ret = wc_VerifyEccsiHash(pub, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  27094. &verified);
  27095. if (ret != 0)
  27096. return -10155;
  27097. if (!verified)
  27098. return -10156;
  27099. /* Check that changing HS results in verification failure. */
  27100. hashPub[0] ^= 0x80;
  27101. ret = wc_SetEccsiHash(pub, hashPub, hashSz);
  27102. if (ret != 0)
  27103. return -10149;
  27104. ret = wc_VerifyEccsiHash(pub, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  27105. &verified);
  27106. if (ret != 0)
  27107. return -10157;
  27108. if (verified)
  27109. return -10158;
  27110. hashPub[0] ^= 0x80;
  27111. ret = wc_SetEccsiHash(pub, hashPub, hashSz);
  27112. if (ret != 0)
  27113. return -10149;
  27114. /* Check that changing msg results in verification failure. */
  27115. msg[0] ^= 0x80;
  27116. ret = wc_VerifyEccsiHash(pub, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  27117. &verified);
  27118. if (ret != 0)
  27119. return -10159;
  27120. if (verified)
  27121. return -10160;
  27122. msg[0] ^= 0x80;
  27123. /* Check that changing signature results in verification failure. */
  27124. sig[0] ^= 0x80;
  27125. ret = wc_VerifyEccsiHash(pub, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  27126. &verified);
  27127. if (ret != 0)
  27128. return -10161;
  27129. if (verified)
  27130. return -10162;
  27131. sig[0] ^= 0x80;
  27132. /* Check that key state hasn't been invalidated. */
  27133. ret = wc_VerifyEccsiHash(pub, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  27134. &verified);
  27135. if (ret != 0)
  27136. return -10163;
  27137. if (!verified)
  27138. return -10164;
  27139. /* Check that verifying with the private key works. */
  27140. ret = wc_VerifyEccsiHash(priv, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  27141. &verified);
  27142. if (ret != 0)
  27143. return -10165;
  27144. if (!verified)
  27145. return -10166;
  27146. /* Check that the KPAK is converted from montgomery form. */
  27147. ret = eccsi_imp_exp_key_test(priv);
  27148. if (ret != 0)
  27149. return ret;
  27150. /* Check that KPAK can converted to Montgomery form again. */
  27151. ret = wc_VerifyEccsiHash(priv, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  27152. &verified);
  27153. if (ret != 0)
  27154. return -10167;
  27155. if (!verified)
  27156. return -10168;
  27157. /* Check that the KPAK is converted from montgomery form. */
  27158. ret = wc_ValidateEccsiPair(pub, WC_HASH_TYPE_SHA256, id, idSz, ssk, pvt,
  27159. &valid);
  27160. if (ret != 0)
  27161. return -10169;
  27162. if (!valid)
  27163. return -10170;
  27164. /* Check that KPAK can converted to Montgomery form again. */
  27165. ret = wc_VerifyEccsiHash(priv, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  27166. &verified);
  27167. if (ret != 0)
  27168. return -10171;
  27169. if (!verified)
  27170. return -10172;
  27171. /* Check that the KPAK is converted from montgomery form. */
  27172. ret = eccsi_imp_exp_pubkey_test(priv, pub);
  27173. if (ret != 0)
  27174. return ret;
  27175. return 0;
  27176. }
  27177. int eccsi_test(void)
  27178. {
  27179. int ret = 0;
  27180. WC_RNG rng;
  27181. int rng_inited = 0;
  27182. EccsiKey* priv = NULL;
  27183. EccsiKey* pub = NULL;
  27184. mp_int* ssk = NULL;
  27185. ecc_point* pvt = NULL;
  27186. priv = (EccsiKey*)XMALLOC(sizeof(EccsiKey), HEAP_HINT,
  27187. DYNAMIC_TYPE_TMP_BUFFER);
  27188. if (priv == NULL)
  27189. ret = -10205;
  27190. else
  27191. XMEMSET(priv, 0, sizeof(*priv));
  27192. if (ret == 0) {
  27193. pub = (EccsiKey*)XMALLOC(sizeof(EccsiKey), HEAP_HINT,
  27194. DYNAMIC_TYPE_TMP_BUFFER);
  27195. if (pub == NULL)
  27196. ret = -10206;
  27197. else
  27198. XMEMSET(pub, 0, sizeof(*pub));
  27199. }
  27200. if (ret == 0) {
  27201. ssk = (mp_int*)XMALLOC(sizeof(mp_int), HEAP_HINT,
  27202. DYNAMIC_TYPE_TMP_BUFFER);
  27203. if (ssk == NULL)
  27204. ret = -10207;
  27205. else
  27206. XMEMSET(ssk, 0, sizeof(*ssk));
  27207. }
  27208. if (ret == 0) {
  27209. #ifndef HAVE_FIPS
  27210. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  27211. #else
  27212. ret = wc_InitRng(&rng);
  27213. #endif
  27214. if (ret != 0)
  27215. ret = -10200;
  27216. else
  27217. rng_inited = 1;
  27218. }
  27219. if (ret == 0) {
  27220. pvt = wc_ecc_new_point();
  27221. if (pvt == NULL)
  27222. ret = -10201;
  27223. }
  27224. if (ret == 0) {
  27225. ret = mp_init(ssk);
  27226. if (ret != 0)
  27227. ret = -10202;
  27228. }
  27229. if (ret == 0) {
  27230. ret = eccsi_api_test(&rng, priv, ssk, pvt);
  27231. }
  27232. if (ret == 0) {
  27233. ret = wc_InitEccsiKey(pub, HEAP_HINT, INVALID_DEVID);
  27234. if (ret != 0)
  27235. ret = -10203;
  27236. }
  27237. if (ret == 0) {
  27238. ret = wc_InitEccsiKey(priv, HEAP_HINT, INVALID_DEVID);
  27239. if (ret != 0)
  27240. ret = -10204;
  27241. }
  27242. if (ret == 0) {
  27243. ret = eccsi_kat_verify_test(pub, pvt);
  27244. }
  27245. if (ret == 0) {
  27246. ret = eccsi_make_key_test(priv, pub, &rng, ssk, pvt);
  27247. }
  27248. if (ret == 0) {
  27249. ret = eccsi_sign_verify_test(priv, pub, &rng, ssk, pvt);
  27250. }
  27251. if (pvt != NULL)
  27252. wc_ecc_del_point(pvt);
  27253. if (rng_inited)
  27254. wc_FreeRng(&rng);
  27255. if (ssk != NULL) {
  27256. mp_free(ssk);
  27257. XFREE(ssk, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27258. }
  27259. if (pub != NULL) {
  27260. wc_FreeEccsiKey(pub);
  27261. XFREE(pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27262. }
  27263. if (priv != NULL) {
  27264. wc_FreeEccsiKey(priv);
  27265. XFREE(priv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27266. }
  27267. return ret;
  27268. }
  27269. #endif /* WOLFCRYPT_HAVE_ECCSI */
  27270. #ifdef WOLFCRYPT_HAVE_SAKKE
  27271. static int sakke_api_test(WC_RNG* rng, SakkeKey* key, ecc_point* rsk)
  27272. {
  27273. int ret;
  27274. byte id[1] = { 0x00 };
  27275. int valid;
  27276. byte data[256];
  27277. word32 sz;
  27278. byte auth[257];
  27279. word16 authSz;
  27280. byte ssv[256];
  27281. word16 ssvSz;
  27282. word32 len;
  27283. ret = wc_InitSakkeKey_ex(NULL, 128, ECC_SAKKE_1, NULL, INVALID_DEVID);
  27284. if (ret != BAD_FUNC_ARG)
  27285. return -10205;
  27286. ret = wc_InitSakkeKey_ex(NULL, 128, ECC_SAKKE_1, HEAP_HINT, INVALID_DEVID);
  27287. if (ret != BAD_FUNC_ARG)
  27288. return -10206;
  27289. wc_FreeSakkeKey(NULL);
  27290. XMEMSET(key, 0, sizeof(*key));
  27291. wc_FreeSakkeKey(key);
  27292. ret = wc_InitSakkeKey_ex(key, 128, ECC_SAKKE_1, HEAP_HINT, INVALID_DEVID);
  27293. if (ret != 0)
  27294. return -10207;
  27295. ret = wc_MakeSakkeKey(NULL, NULL);
  27296. if (ret != BAD_FUNC_ARG)
  27297. return -10208;
  27298. ret = wc_MakeSakkeKey(key, NULL);
  27299. if (ret != BAD_FUNC_ARG)
  27300. return -10209;
  27301. ret = wc_MakeSakkeKey(NULL, rng);
  27302. if (ret != BAD_FUNC_ARG)
  27303. return -10210;
  27304. ret = wc_MakeSakkePublicKey(NULL, NULL);
  27305. if (ret != BAD_FUNC_ARG)
  27306. return -10211;
  27307. ret = wc_MakeSakkePublicKey(key, NULL);
  27308. if (ret != BAD_FUNC_ARG)
  27309. return -10212;
  27310. ret = wc_MakeSakkePublicKey(NULL, rsk);
  27311. if (ret != BAD_FUNC_ARG)
  27312. return -10213;
  27313. ret = wc_MakeSakkeRsk(NULL, NULL, 1, NULL);
  27314. if (ret != BAD_FUNC_ARG)
  27315. return -10214;
  27316. ret = wc_MakeSakkeRsk(key, id, 1, NULL);
  27317. if (ret != BAD_FUNC_ARG)
  27318. return -10215;
  27319. ret = wc_MakeSakkeRsk(key, NULL, 1, rsk);
  27320. if (ret != BAD_FUNC_ARG)
  27321. return -10216;
  27322. ret = wc_MakeSakkeRsk(NULL, id, 1, rsk);
  27323. if (ret != BAD_FUNC_ARG)
  27324. return -10217;
  27325. ret = wc_ValidateSakkeRsk(NULL, NULL, 1, NULL, NULL);
  27326. if (ret != BAD_FUNC_ARG)
  27327. return -10218;
  27328. ret = wc_ValidateSakkeRsk(key, id, 1, rsk, NULL);
  27329. if (ret != BAD_FUNC_ARG)
  27330. return -10219;
  27331. ret = wc_ValidateSakkeRsk(NULL, id, 1, rsk, &valid);
  27332. if (ret != BAD_FUNC_ARG)
  27333. return -10220;
  27334. ret = wc_ExportSakkeKey(NULL, NULL, NULL);
  27335. if (ret != BAD_FUNC_ARG)
  27336. return -10221;
  27337. ret = wc_ExportSakkeKey(key, data, NULL);
  27338. if (ret != BAD_FUNC_ARG)
  27339. return -10222;
  27340. ret = wc_ExportSakkeKey(NULL, data, &sz);
  27341. if (ret != BAD_FUNC_ARG)
  27342. return -10223;
  27343. ret = wc_ImportSakkeKey(NULL, NULL, 1);
  27344. if (ret != BAD_FUNC_ARG)
  27345. return -10224;
  27346. ret = wc_ImportSakkeKey(key, NULL, 1);
  27347. if (ret != BAD_FUNC_ARG)
  27348. return -10225;
  27349. ret = wc_ImportSakkeKey(NULL, data, 1);
  27350. if (ret != BAD_FUNC_ARG)
  27351. return -10226;
  27352. ret = wc_ExportSakkePrivateKey(NULL, NULL, NULL);
  27353. if (ret != BAD_FUNC_ARG)
  27354. return -10227;
  27355. ret = wc_ExportSakkePrivateKey(key, data, NULL);
  27356. if (ret != BAD_FUNC_ARG)
  27357. return -10228;
  27358. ret = wc_ExportSakkePrivateKey(NULL, data, &sz);
  27359. if (ret != BAD_FUNC_ARG)
  27360. return -10229;
  27361. ret = wc_ImportSakkePrivateKey(NULL, NULL, 1);
  27362. if (ret != BAD_FUNC_ARG)
  27363. return -10230;
  27364. ret = wc_ImportSakkePrivateKey(key, NULL, 1);
  27365. if (ret != BAD_FUNC_ARG)
  27366. return -10231;
  27367. ret = wc_ImportSakkePrivateKey(NULL, data, 1);
  27368. if (ret != BAD_FUNC_ARG)
  27369. return -10232;
  27370. sz = sizeof(data);
  27371. ret = wc_EncodeSakkeRsk(NULL, NULL, data, NULL, 1);
  27372. if (ret != BAD_FUNC_ARG)
  27373. return -10233;
  27374. ret = wc_EncodeSakkeRsk(key, rsk, data, NULL, 1);
  27375. if (ret != BAD_FUNC_ARG)
  27376. return -10234;
  27377. ret = wc_EncodeSakkeRsk(key, NULL, data, &sz, 1);
  27378. if (ret != BAD_FUNC_ARG)
  27379. return -10235;
  27380. ret = wc_EncodeSakkeRsk(NULL, rsk, data, &sz, 1);
  27381. if (ret != BAD_FUNC_ARG)
  27382. return -10236;
  27383. ret = wc_DecodeSakkeRsk(NULL, NULL, sz, NULL);
  27384. if (ret != BAD_FUNC_ARG)
  27385. return -10237;
  27386. ret = wc_DecodeSakkeRsk(key, data, sz, NULL);
  27387. if (ret != BAD_FUNC_ARG)
  27388. return -10238;
  27389. ret = wc_DecodeSakkeRsk(key, NULL, sz, rsk);
  27390. if (ret != BAD_FUNC_ARG)
  27391. return -10239;
  27392. ret = wc_DecodeSakkeRsk(NULL, data, sz, rsk);
  27393. if (ret != BAD_FUNC_ARG)
  27394. return -10240;
  27395. ret = wc_ImportSakkeRsk(NULL, NULL, sz);
  27396. if (ret != BAD_FUNC_ARG)
  27397. return -10237;
  27398. ret = wc_ImportSakkeRsk(key, NULL, sz);
  27399. if (ret != BAD_FUNC_ARG)
  27400. return -10237;
  27401. ret = wc_ImportSakkeRsk(NULL, data, sz);
  27402. if (ret != BAD_FUNC_ARG)
  27403. return -10237;
  27404. ret = wc_ImportSakkeRsk(key, data, 1);
  27405. if (ret != BUFFER_E)
  27406. return -10237;
  27407. ret = wc_GenerateSakkeRskTable(NULL, NULL, data, NULL);
  27408. if (ret != BAD_FUNC_ARG)
  27409. return -10241;
  27410. ret = wc_GenerateSakkeRskTable(key, NULL, data, NULL);
  27411. if (ret != BAD_FUNC_ARG)
  27412. return -10242;
  27413. ret = wc_GenerateSakkeRskTable(NULL, rsk, data, NULL);
  27414. if (ret != BAD_FUNC_ARG)
  27415. return -10243;
  27416. ret = wc_GenerateSakkeRskTable(NULL, NULL, data, &len);
  27417. if (ret != BAD_FUNC_ARG)
  27418. return -10244;
  27419. ret = wc_GenerateSakkeRskTable(key, rsk, data, NULL);
  27420. if (ret != BAD_FUNC_ARG)
  27421. return -10245;
  27422. ret = wc_GenerateSakkeRskTable(key, NULL, data, &len);
  27423. if (ret != BAD_FUNC_ARG)
  27424. return -10246;
  27425. ret = wc_GenerateSakkeRskTable(NULL, rsk, data, &len);
  27426. if (ret != BAD_FUNC_ARG)
  27427. return -10247;
  27428. ret = wc_GenerateSakkeRskTable(key, rsk, NULL, &len);
  27429. if (ret != LENGTH_ONLY_E)
  27430. return -10248;
  27431. len--;
  27432. ret = wc_GenerateSakkeRskTable(key, rsk, data, &len);
  27433. if (ret != BUFFER_E)
  27434. return -10249;
  27435. ret = wc_ExportSakkePublicKey(NULL, data, NULL, 1);
  27436. if (ret != BAD_FUNC_ARG)
  27437. return -10250;
  27438. ret = wc_ExportSakkePublicKey(key, data, NULL, 1);
  27439. if (ret != BAD_FUNC_ARG)
  27440. return -10251;
  27441. ret = wc_ExportSakkePublicKey(NULL, data, &sz, 1);
  27442. if (ret != BAD_FUNC_ARG)
  27443. return -10252;
  27444. ret = wc_ImportSakkePublicKey(NULL, NULL, sz, 1);
  27445. if (ret != BAD_FUNC_ARG)
  27446. return -10253;
  27447. ret = wc_ImportSakkePublicKey(key, NULL, sz, 1);
  27448. if (ret != BAD_FUNC_ARG)
  27449. return -10254;
  27450. ret = wc_ImportSakkePublicKey(NULL, data, sz, 1);
  27451. if (ret != BAD_FUNC_ARG)
  27452. return -10255;
  27453. ret = wc_GetSakkeAuthSize(NULL, NULL);
  27454. if (ret != BAD_FUNC_ARG)
  27455. return -10256;
  27456. ret = wc_GetSakkeAuthSize(key, NULL);
  27457. if (ret != BAD_FUNC_ARG)
  27458. return -10257;
  27459. ret = wc_GetSakkeAuthSize(NULL, &authSz);
  27460. if (ret != BAD_FUNC_ARG)
  27461. return -10258;
  27462. ret = wc_MakeSakkePointI(NULL, NULL, SAKKE_ID_MAX_SIZE + 1);
  27463. if (ret != BAD_FUNC_ARG)
  27464. return -10259;
  27465. ret = wc_MakeSakkePointI(key, NULL, SAKKE_ID_MAX_SIZE + 1);
  27466. if (ret != BAD_FUNC_ARG)
  27467. return -10260;
  27468. ret = wc_MakeSakkePointI(NULL, id, 1);
  27469. if (ret != BAD_FUNC_ARG)
  27470. return -10261;
  27471. ret = wc_MakeSakkePointI(NULL, NULL, 1);
  27472. if (ret != BAD_FUNC_ARG)
  27473. return -10262;
  27474. ret = wc_MakeSakkePointI(key, id, SAKKE_ID_MAX_SIZE + 1);
  27475. if (ret != BAD_FUNC_ARG)
  27476. return -10263;
  27477. ret = wc_MakeSakkePointI(key, NULL, 1);
  27478. if (ret != BAD_FUNC_ARG)
  27479. return -10264;
  27480. ret = wc_MakeSakkePointI(NULL, id, 1);
  27481. if (ret != BAD_FUNC_ARG)
  27482. return -10265;
  27483. ret = wc_GenerateSakkePointITable(NULL, data, NULL);
  27484. if (ret != BAD_FUNC_ARG)
  27485. return -10266;
  27486. ret = wc_GenerateSakkePointITable(key, data, NULL);
  27487. if (ret != BAD_FUNC_ARG)
  27488. return -10267;
  27489. ret = wc_GenerateSakkePointITable(NULL, data, &len);
  27490. if (ret != BAD_FUNC_ARG)
  27491. return -10268;
  27492. ret = wc_GenerateSakkePointITable(key, NULL, &len);
  27493. if (ret != LENGTH_ONLY_E)
  27494. return -10269;
  27495. len--;
  27496. ret = wc_GenerateSakkePointITable(key, data, &len);
  27497. if (ret != BUFFER_E)
  27498. return -10270;
  27499. ret = wc_SetSakkePointITable(NULL, NULL, 1);
  27500. if (ret != BAD_FUNC_ARG)
  27501. return -10271;
  27502. ret = wc_SetSakkePointITable(key, NULL, 1);
  27503. if (ret != BAD_FUNC_ARG)
  27504. return -10272;
  27505. ret = wc_SetSakkePointITable(NULL, data, 1);
  27506. if (ret != BAD_FUNC_ARG)
  27507. return -10273;
  27508. ret = wc_SetSakkePointITable(key, data, 1);
  27509. if (ret != BUFFER_E)
  27510. return -10274;
  27511. ret = wc_ClearSakkePointITable(NULL);
  27512. if (ret != BAD_FUNC_ARG)
  27513. return -10275;
  27514. ret = wc_GetSakkePointI(NULL, data, NULL);
  27515. if (ret != BAD_FUNC_ARG)
  27516. return -10276;
  27517. ret = wc_GetSakkePointI(key, data, NULL);
  27518. if (ret != BAD_FUNC_ARG)
  27519. return -10277;
  27520. ret = wc_GetSakkePointI(NULL, data, &sz);
  27521. if (ret != BAD_FUNC_ARG)
  27522. return -10278;
  27523. sz = 1;
  27524. ret = wc_GetSakkePointI(key, data, &sz);
  27525. if (ret != BUFFER_E)
  27526. return -10279;
  27527. sz = 256;
  27528. ret = wc_SetSakkePointI(NULL, NULL, 1, NULL, sz);
  27529. if (ret != BAD_FUNC_ARG)
  27530. return -10280;
  27531. ret = wc_SetSakkePointI(key, NULL, 1, NULL, sz);
  27532. if (ret != BAD_FUNC_ARG)
  27533. return -10281;
  27534. ret = wc_SetSakkePointI(NULL, id, 1, NULL, sz);
  27535. if (ret != BAD_FUNC_ARG)
  27536. return -10282;
  27537. ret = wc_SetSakkePointI(NULL, NULL, 1, data, sz);
  27538. if (ret != BAD_FUNC_ARG)
  27539. return -10283;
  27540. ret = wc_SetSakkePointI(key, id, 1, NULL, sz);
  27541. if (ret != BAD_FUNC_ARG)
  27542. return -10284;
  27543. ret = wc_SetSakkePointI(key, NULL, 1, data, sz);
  27544. if (ret != BAD_FUNC_ARG)
  27545. return -10285;
  27546. ret = wc_SetSakkePointI(NULL, id, 1, data, sz);
  27547. if (ret != BAD_FUNC_ARG)
  27548. return -10286;
  27549. ret = wc_SetSakkePointI(key, id, SAKKE_ID_MAX_SIZE + 1, data, sz);
  27550. if (ret != BUFFER_E)
  27551. return -10287;
  27552. ret = wc_SetSakkePointI(key, id, 1, data, sz - 1);
  27553. if (ret != BUFFER_E)
  27554. return -10288;
  27555. ret = wc_SetSakkeIdentity(NULL, NULL, 1);
  27556. if (ret != BAD_FUNC_ARG)
  27557. return -10286;
  27558. ret = wc_SetSakkeIdentity(key, NULL, 1);
  27559. if (ret != BAD_FUNC_ARG)
  27560. return -10286;
  27561. ret = wc_SetSakkeIdentity(NULL, id, 1);
  27562. if (ret != BAD_FUNC_ARG)
  27563. return -10286;
  27564. ssvSz = sizeof(ssv);
  27565. ret = wc_MakeSakkeEncapsulatedSSV(NULL, WC_HASH_TYPE_SHA256, NULL, ssvSz,
  27566. auth, NULL);
  27567. if (ret != BAD_FUNC_ARG)
  27568. return -10289;
  27569. ret = wc_MakeSakkeEncapsulatedSSV(key, WC_HASH_TYPE_SHA256, NULL, ssvSz,
  27570. auth, NULL);
  27571. if (ret != BAD_FUNC_ARG)
  27572. return -10290;
  27573. ret = wc_MakeSakkeEncapsulatedSSV(NULL, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  27574. auth, NULL);
  27575. if (ret != BAD_FUNC_ARG)
  27576. return -10291;
  27577. ret = wc_MakeSakkeEncapsulatedSSV(NULL, WC_HASH_TYPE_SHA256, NULL, ssvSz,
  27578. auth, &authSz);
  27579. if (ret != BAD_FUNC_ARG)
  27580. return -10292;
  27581. ret = wc_MakeSakkeEncapsulatedSSV(key, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  27582. auth, NULL);
  27583. if (ret != BAD_FUNC_ARG)
  27584. return -10293;
  27585. ret = wc_MakeSakkeEncapsulatedSSV(key, WC_HASH_TYPE_SHA256, NULL, ssvSz,
  27586. auth, &authSz);
  27587. if (ret != BAD_FUNC_ARG)
  27588. return -10294;
  27589. ret = wc_MakeSakkeEncapsulatedSSV(NULL, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  27590. auth, &authSz);
  27591. if (ret != BAD_FUNC_ARG)
  27592. return -10295;
  27593. ret = wc_MakeSakkeEncapsulatedSSV(key, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  27594. auth, &authSz);
  27595. if (ret != BAD_STATE_E)
  27596. return -10295;
  27597. ret = wc_GenerateSakkeSSV(NULL, NULL, data, NULL);
  27598. if (ret != BAD_FUNC_ARG)
  27599. return -10296;
  27600. ret = wc_GenerateSakkeSSV(key, rng, data, NULL);
  27601. if (ret != BAD_FUNC_ARG)
  27602. return -10297;
  27603. ret = wc_GenerateSakkeSSV(key, NULL, data, &ssvSz);
  27604. if (ret != BAD_FUNC_ARG)
  27605. return -10298;
  27606. ret = wc_GenerateSakkeSSV(NULL, rng, data, &ssvSz);
  27607. if (ret != BAD_FUNC_ARG)
  27608. return -10299;
  27609. ret = wc_SetSakkeRsk(NULL, NULL, data, 1);
  27610. if (ret != BAD_FUNC_ARG)
  27611. return -10286;
  27612. ret = wc_SetSakkeRsk(key, NULL, data, 1);
  27613. if (ret != BAD_FUNC_ARG)
  27614. return -10286;
  27615. ret = wc_SetSakkeRsk(NULL, rsk, data, 1);
  27616. if (ret != BAD_FUNC_ARG)
  27617. return -10286;
  27618. ssvSz = sizeof(ssv);
  27619. authSz = sizeof(auth);
  27620. ret = wc_DeriveSakkeSSV(NULL, WC_HASH_TYPE_SHA256, NULL, ssvSz, NULL,
  27621. authSz);
  27622. if (ret != BAD_FUNC_ARG)
  27623. return -10300;
  27624. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, NULL, ssvSz, NULL,
  27625. authSz);
  27626. if (ret != BAD_FUNC_ARG)
  27627. return -10300;
  27628. ret = wc_DeriveSakkeSSV(NULL, WC_HASH_TYPE_SHA256, ssv, ssvSz, NULL,
  27629. authSz);
  27630. if (ret != BAD_FUNC_ARG)
  27631. return -10300;
  27632. ret = wc_DeriveSakkeSSV(NULL, WC_HASH_TYPE_SHA256, NULL, ssvSz, auth,
  27633. authSz);
  27634. if (ret != BAD_FUNC_ARG)
  27635. return -10300;
  27636. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, ssv, ssvSz, NULL,
  27637. authSz);
  27638. if (ret != BAD_FUNC_ARG)
  27639. return -10300;
  27640. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, NULL, ssvSz, auth,
  27641. authSz);
  27642. if (ret != BAD_FUNC_ARG)
  27643. return -10300;
  27644. ret = wc_DeriveSakkeSSV(NULL, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  27645. authSz);
  27646. if (ret != BAD_FUNC_ARG)
  27647. return -10300;
  27648. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  27649. authSz);
  27650. if (ret != BAD_STATE_E)
  27651. return -10300;
  27652. ret = wc_SetSakkeIdentity(key, id, 1);
  27653. if (ret != 0)
  27654. return -10286;
  27655. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  27656. authSz);
  27657. if (ret != BAD_STATE_E)
  27658. return -10300;
  27659. ret = wc_SetSakkeIdentity(key, id, 0);
  27660. if (ret != 0)
  27661. return -10286;
  27662. ret = wc_SetSakkeRsk(key, rsk, data, 1);
  27663. if (ret != 0)
  27664. return -10286;
  27665. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  27666. authSz);
  27667. if (ret != BAD_STATE_E)
  27668. return -10300;
  27669. wc_FreeSakkeKey(key);
  27670. return 0;
  27671. }
  27672. static int sakke_kat_derive_test(SakkeKey* key, ecc_point* rsk)
  27673. {
  27674. WOLFSSL_SMALL_STACK_STATIC const byte pubData[] = {
  27675. 0x59, 0x58, 0xEF, 0x1B, 0x16, 0x79, 0xBF, 0x09,
  27676. 0x9B, 0x3A, 0x03, 0x0D, 0xF2, 0x55, 0xAA, 0x6A,
  27677. 0x23, 0xC1, 0xD8, 0xF1, 0x43, 0xD4, 0xD2, 0x3F,
  27678. 0x75, 0x3E, 0x69, 0xBD, 0x27, 0xA8, 0x32, 0xF3,
  27679. 0x8C, 0xB4, 0xAD, 0x53, 0xDD, 0xEF, 0x42, 0x60,
  27680. 0xB0, 0xFE, 0x8B, 0xB4, 0x5C, 0x4C, 0x1F, 0xF5,
  27681. 0x10, 0xEF, 0xFE, 0x30, 0x03, 0x67, 0xA3, 0x7B,
  27682. 0x61, 0xF7, 0x01, 0xD9, 0x14, 0xAE, 0xF0, 0x97,
  27683. 0x24, 0x82, 0x5F, 0xA0, 0x70, 0x7D, 0x61, 0xA6,
  27684. 0xDF, 0xF4, 0xFB, 0xD7, 0x27, 0x35, 0x66, 0xCD,
  27685. 0xDE, 0x35, 0x2A, 0x0B, 0x04, 0xB7, 0xC1, 0x6A,
  27686. 0x78, 0x30, 0x9B, 0xE6, 0x40, 0x69, 0x7D, 0xE7,
  27687. 0x47, 0x61, 0x3A, 0x5F, 0xC1, 0x95, 0xE8, 0xB9,
  27688. 0xF3, 0x28, 0x85, 0x2A, 0x57, 0x9D, 0xB8, 0xF9,
  27689. 0x9B, 0x1D, 0x00, 0x34, 0x47, 0x9E, 0xA9, 0xC5,
  27690. 0x59, 0x5F, 0x47, 0xC4, 0xB2, 0xF5, 0x4F, 0xF2,
  27691. 0x15, 0x08, 0xD3, 0x75, 0x14, 0xDC, 0xF7, 0xA8,
  27692. 0xE1, 0x43, 0xA6, 0x05, 0x8C, 0x09, 0xA6, 0xBF,
  27693. 0x2C, 0x98, 0x58, 0xCA, 0x37, 0xC2, 0x58, 0x06,
  27694. 0x5A, 0xE6, 0xBF, 0x75, 0x32, 0xBC, 0x8B, 0x5B,
  27695. 0x63, 0x38, 0x38, 0x66, 0xE0, 0x75, 0x3C, 0x5A,
  27696. 0xC0, 0xE7, 0x27, 0x09, 0xF8, 0x44, 0x5F, 0x2E,
  27697. 0x61, 0x78, 0xE0, 0x65, 0x85, 0x7E, 0x0E, 0xDA,
  27698. 0x10, 0xF6, 0x82, 0x06, 0xB6, 0x35, 0x05, 0xED,
  27699. 0x87, 0xE5, 0x34, 0xFB, 0x28, 0x31, 0xFF, 0x95,
  27700. 0x7F, 0xB7, 0xDC, 0x61, 0x9D, 0xAE, 0x61, 0x30,
  27701. 0x1E, 0xEA, 0xCC, 0x2F, 0xDA, 0x36, 0x80, 0xEA,
  27702. 0x49, 0x99, 0x25, 0x8A, 0x83, 0x3C, 0xEA, 0x8F,
  27703. 0xC6, 0x7C, 0x6D, 0x19, 0x48, 0x7F, 0xB4, 0x49,
  27704. 0x05, 0x9F, 0x26, 0xCC, 0x8A, 0xAB, 0x65, 0x5A,
  27705. 0xB5, 0x8B, 0x7C, 0xC7, 0x96, 0xE2, 0x4E, 0x9A,
  27706. 0x39, 0x40, 0x95, 0x75, 0x4F, 0x5F, 0x8B, 0xAE
  27707. };
  27708. WOLFSSL_SMALL_STACK_STATIC const byte rskData[] = {
  27709. 0x93, 0xAF, 0x67, 0xE5, 0x00, 0x7B, 0xA6, 0xE6,
  27710. 0xA8, 0x0D, 0xA7, 0x93, 0xDA, 0x30, 0x0F, 0xA4,
  27711. 0xB5, 0x2D, 0x0A, 0x74, 0xE2, 0x5E, 0x6E, 0x7B,
  27712. 0x2B, 0x3D, 0x6E, 0xE9, 0xD1, 0x8A, 0x9B, 0x5C,
  27713. 0x50, 0x23, 0x59, 0x7B, 0xD8, 0x2D, 0x80, 0x62,
  27714. 0xD3, 0x40, 0x19, 0x56, 0x3B, 0xA1, 0xD2, 0x5C,
  27715. 0x0D, 0xC5, 0x6B, 0x7B, 0x97, 0x9D, 0x74, 0xAA,
  27716. 0x50, 0xF2, 0x9F, 0xBF, 0x11, 0xCC, 0x2C, 0x93,
  27717. 0xF5, 0xDF, 0xCA, 0x61, 0x5E, 0x60, 0x92, 0x79,
  27718. 0xF6, 0x17, 0x5C, 0xEA, 0xDB, 0x00, 0xB5, 0x8C,
  27719. 0x6B, 0xEE, 0x1E, 0x7A, 0x2A, 0x47, 0xC4, 0xF0,
  27720. 0xC4, 0x56, 0xF0, 0x52, 0x59, 0xA6, 0xFA, 0x94,
  27721. 0xA6, 0x34, 0xA4, 0x0D, 0xAE, 0x1D, 0xF5, 0x93,
  27722. 0xD4, 0xFE, 0xCF, 0x68, 0x8D, 0x5F, 0xC6, 0x78,
  27723. 0xBE, 0x7E, 0xFC, 0x6D, 0xF3, 0xD6, 0x83, 0x53,
  27724. 0x25, 0xB8, 0x3B, 0x2C, 0x6E, 0x69, 0x03, 0x6B,
  27725. 0x15, 0x5F, 0x0A, 0x27, 0x24, 0x10, 0x94, 0xB0,
  27726. 0x4B, 0xFB, 0x0B, 0xDF, 0xAC, 0x6C, 0x67, 0x0A,
  27727. 0x65, 0xC3, 0x25, 0xD3, 0x9A, 0x06, 0x9F, 0x03,
  27728. 0x65, 0x9D, 0x44, 0xCA, 0x27, 0xD3, 0xBE, 0x8D,
  27729. 0xF3, 0x11, 0x17, 0x2B, 0x55, 0x41, 0x60, 0x18,
  27730. 0x1C, 0xBE, 0x94, 0xA2, 0xA7, 0x83, 0x32, 0x0C,
  27731. 0xED, 0x59, 0x0B, 0xC4, 0x26, 0x44, 0x70, 0x2C,
  27732. 0xF3, 0x71, 0x27, 0x1E, 0x49, 0x6B, 0xF2, 0x0F,
  27733. 0x58, 0x8B, 0x78, 0xA1, 0xBC, 0x01, 0xEC, 0xBB,
  27734. 0x65, 0x59, 0x93, 0x4B, 0xDD, 0x2F, 0xB6, 0x5D,
  27735. 0x28, 0x84, 0x31, 0x8A, 0x33, 0xD1, 0xA4, 0x2A,
  27736. 0xDF, 0x5E, 0x33, 0xCC, 0x58, 0x00, 0x28, 0x0B,
  27737. 0x28, 0x35, 0x64, 0x97, 0xF8, 0x71, 0x35, 0xBA,
  27738. 0xB9, 0x61, 0x2A, 0x17, 0x26, 0x04, 0x24, 0x40,
  27739. 0x9A, 0xC1, 0x5F, 0xEE, 0x99, 0x6B, 0x74, 0x4C,
  27740. 0x33, 0x21, 0x51, 0x23, 0x5D, 0xEC, 0xB0, 0xF5
  27741. };
  27742. WOLFSSL_SMALL_STACK_STATIC const byte id[] = {
  27743. 0x32, 0x30, 0x31, 0x31, 0x2D, 0x30, 0x32, 0x00,
  27744. 0x74, 0x65, 0x6C, 0x3A, 0x2B, 0x34, 0x34, 0x37,
  27745. 0x37, 0x30, 0x30, 0x39, 0x30, 0x30, 0x31, 0x32,
  27746. 0x33, 0x00
  27747. };
  27748. WOLFSSL_SMALL_STACK_STATIC const byte ssv[] = {
  27749. 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0,
  27750. 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0
  27751. };
  27752. WOLFSSL_SMALL_STACK_STATIC const byte auth[] = {
  27753. 0x04,
  27754. 0x44, 0xE8, 0xAD, 0x44, 0xAB, 0x85, 0x92, 0xA6,
  27755. 0xA5, 0xA3, 0xDD, 0xCA, 0x5C, 0xF8, 0x96, 0xC7,
  27756. 0x18, 0x04, 0x36, 0x06, 0xA0, 0x1D, 0x65, 0x0D,
  27757. 0xEF, 0x37, 0xA0, 0x1F, 0x37, 0xC2, 0x28, 0xC3,
  27758. 0x32, 0xFC, 0x31, 0x73, 0x54, 0xE2, 0xC2, 0x74,
  27759. 0xD4, 0xDA, 0xF8, 0xAD, 0x00, 0x10, 0x54, 0xC7,
  27760. 0x6C, 0xE5, 0x79, 0x71, 0xC6, 0xF4, 0x48, 0x6D,
  27761. 0x57, 0x23, 0x04, 0x32, 0x61, 0xC5, 0x06, 0xEB,
  27762. 0xF5, 0xBE, 0x43, 0x8F, 0x53, 0xDE, 0x04, 0xF0,
  27763. 0x67, 0xC7, 0x76, 0xE0, 0xDD, 0x3B, 0x71, 0xA6,
  27764. 0x29, 0x01, 0x33, 0x28, 0x37, 0x25, 0xA5, 0x32,
  27765. 0xF2, 0x1A, 0xF1, 0x45, 0x12, 0x6D, 0xC1, 0xD7,
  27766. 0x77, 0xEC, 0xC2, 0x7B, 0xE5, 0x08, 0x35, 0xBD,
  27767. 0x28, 0x09, 0x8B, 0x8A, 0x73, 0xD9, 0xF8, 0x01,
  27768. 0xD8, 0x93, 0x79, 0x3A, 0x41, 0xFF, 0x5C, 0x49,
  27769. 0xB8, 0x7E, 0x79, 0xF2, 0xBE, 0x4D, 0x56, 0xCE,
  27770. 0x55, 0x7E, 0x13, 0x4A, 0xD8, 0x5B, 0xB1, 0xD4,
  27771. 0xB9, 0xCE, 0x4F, 0x8B, 0xE4, 0xB0, 0x8A, 0x12,
  27772. 0xBA, 0xBF, 0x55, 0xB1, 0xD6, 0xF1, 0xD7, 0xA6,
  27773. 0x38, 0x01, 0x9E, 0xA2, 0x8E, 0x15, 0xAB, 0x1C,
  27774. 0x9F, 0x76, 0x37, 0x5F, 0xDD, 0x12, 0x10, 0xD4,
  27775. 0xF4, 0x35, 0x1B, 0x9A, 0x00, 0x94, 0x86, 0xB7,
  27776. 0xF3, 0xED, 0x46, 0xC9, 0x65, 0xDE, 0xD2, 0xD8,
  27777. 0x0D, 0xAD, 0xE4, 0xF3, 0x8C, 0x67, 0x21, 0xD5,
  27778. 0x2C, 0x3A, 0xD1, 0x03, 0xA1, 0x0E, 0xBD, 0x29,
  27779. 0x59, 0x24, 0x8B, 0x4E, 0xF0, 0x06, 0x83, 0x6B,
  27780. 0xF0, 0x97, 0x44, 0x8E, 0x61, 0x07, 0xC9, 0xED,
  27781. 0xEE, 0x9F, 0xB7, 0x04, 0x82, 0x3D, 0xF1, 0x99,
  27782. 0xF8, 0x32, 0xC9, 0x05, 0xAE, 0x45, 0xF8, 0xA2,
  27783. 0x47, 0xA0, 0x72, 0xD8, 0xEF, 0x72, 0x9E, 0xAB,
  27784. 0xC5, 0xE2, 0x75, 0x74, 0xB0, 0x77, 0x39, 0xB3,
  27785. 0x4B, 0xE7, 0x4A, 0x53, 0x2F, 0x74, 0x7B, 0x86
  27786. };
  27787. WOLFSSL_SMALL_STACK_STATIC const byte encSsv[] = {
  27788. 0x89, 0xE0, 0xBC, 0x66, 0x1A, 0xA1, 0xE9, 0x16,
  27789. 0x38, 0xE6, 0xAC, 0xC8, 0x4E, 0x49, 0x65, 0x07
  27790. };
  27791. int ret;
  27792. int valid;
  27793. byte pubKey[sizeof(pubData) + 1];
  27794. word32 sz = sizeof(pubKey);
  27795. byte tmpSsv[sizeof(encSsv)];
  27796. byte* iTable = NULL;
  27797. word32 iTableLen;
  27798. byte* table = NULL;
  27799. word32 len;
  27800. ret = wc_ImportSakkePublicKey(key, pubData, sizeof(pubData), 0);
  27801. if (ret != 0)
  27802. return -10315;
  27803. ret = wc_DecodeSakkeRsk(key, rskData, sizeof(rskData), rsk);
  27804. if (ret != 0)
  27805. return -10316;
  27806. ret = wc_ValidateSakkeRsk(key, id, sizeof(id), rsk, &valid);
  27807. if (ret != 0)
  27808. return -10317;
  27809. if (valid != 1)
  27810. return -10318;
  27811. ret = wc_SetSakkeRsk(key, rsk, NULL, 0);
  27812. if (ret != 0)
  27813. return -10319;
  27814. ret = wc_SetSakkeIdentity(key, id, sizeof(id));
  27815. if (ret != 0)
  27816. return -10319;
  27817. XMEMCPY(tmpSsv, encSsv, sizeof(encSsv));
  27818. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, tmpSsv, sizeof(tmpSsv),
  27819. auth, sizeof(auth));
  27820. if (ret != 0)
  27821. return -10322;
  27822. if (XMEMCMP(tmpSsv, ssv, sizeof(ssv)) != 0)
  27823. return -10320;
  27824. ret = wc_MakeSakkePointI(key, id, sizeof(id));
  27825. if (ret != 0)
  27826. return -10321;
  27827. iTableLen = 0;
  27828. ret = wc_GenerateSakkePointITable(key, NULL, &iTableLen);
  27829. if (ret != LENGTH_ONLY_E)
  27830. return -10322;
  27831. if (iTableLen != 0) {
  27832. iTable = (byte*)XMALLOC(iTableLen, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27833. if (iTable == NULL)
  27834. return -10323;
  27835. ret = wc_GenerateSakkePointITable(key, iTable, &iTableLen);
  27836. if (ret != 0)
  27837. return -10324;
  27838. }
  27839. len = 0;
  27840. ret = wc_GenerateSakkeRskTable(key, rsk, NULL, &len);
  27841. if (ret != LENGTH_ONLY_E)
  27842. return -10325;
  27843. if (len > 0) {
  27844. table = (byte*)XMALLOC(len, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27845. if (table == NULL)
  27846. return -10326;
  27847. ret = wc_GenerateSakkeRskTable(key, rsk, table, &len);
  27848. if (ret != 0)
  27849. return -10327;
  27850. }
  27851. ret = wc_SetSakkeRsk(key, rsk, table, len);
  27852. if (ret != 0)
  27853. return -10319;
  27854. XMEMCPY(tmpSsv, encSsv, sizeof(encSsv));
  27855. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, tmpSsv, sizeof(tmpSsv),
  27856. auth, sizeof(auth));
  27857. if (ret != 0)
  27858. return -10328;
  27859. if (XMEMCMP(tmpSsv, ssv, sizeof(ssv)) != 0)
  27860. return -10329;
  27861. /* Don't reference table that is about to be freed. */
  27862. ret = wc_ClearSakkePointITable(key);
  27863. if (ret != 0)
  27864. return -10330;
  27865. /* Dispose of tables */
  27866. if (iTable != NULL)
  27867. XFREE(iTable, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27868. if (table != NULL)
  27869. XFREE(table, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27870. /* Make sure the key public key is exportable - convert to Montgomery form
  27871. * in Validation.
  27872. */
  27873. ret = wc_ExportSakkePublicKey(key, pubKey, &sz, 1);
  27874. if (ret != 0)
  27875. return -10331;
  27876. if (sz != sizeof(pubData))
  27877. return -10332;
  27878. if (XMEMCMP(pubKey, pubData, sizeof(pubData)) != 0)
  27879. return -10333;
  27880. sz = sizeof(pubData) + 1;
  27881. ret = wc_ExportSakkePublicKey(key, pubKey, &sz, 0);
  27882. if (ret != 0)
  27883. return -10334;
  27884. if (sz != sizeof(pubData) + 1)
  27885. return -10335;
  27886. if (pubKey[0] != 0x04)
  27887. return -10336;
  27888. if (XMEMCMP(pubKey + 1, pubData, sizeof(pubData)) != 0)
  27889. return -10337;
  27890. return 0;
  27891. }
  27892. static int sakke_kat_encapsulate_test(SakkeKey* key)
  27893. {
  27894. static const byte pubData[] = {
  27895. 0x59, 0x58, 0xEF, 0x1B, 0x16, 0x79, 0xBF, 0x09,
  27896. 0x9B, 0x3A, 0x03, 0x0D, 0xF2, 0x55, 0xAA, 0x6A,
  27897. 0x23, 0xC1, 0xD8, 0xF1, 0x43, 0xD4, 0xD2, 0x3F,
  27898. 0x75, 0x3E, 0x69, 0xBD, 0x27, 0xA8, 0x32, 0xF3,
  27899. 0x8C, 0xB4, 0xAD, 0x53, 0xDD, 0xEF, 0x42, 0x60,
  27900. 0xB0, 0xFE, 0x8B, 0xB4, 0x5C, 0x4C, 0x1F, 0xF5,
  27901. 0x10, 0xEF, 0xFE, 0x30, 0x03, 0x67, 0xA3, 0x7B,
  27902. 0x61, 0xF7, 0x01, 0xD9, 0x14, 0xAE, 0xF0, 0x97,
  27903. 0x24, 0x82, 0x5F, 0xA0, 0x70, 0x7D, 0x61, 0xA6,
  27904. 0xDF, 0xF4, 0xFB, 0xD7, 0x27, 0x35, 0x66, 0xCD,
  27905. 0xDE, 0x35, 0x2A, 0x0B, 0x04, 0xB7, 0xC1, 0x6A,
  27906. 0x78, 0x30, 0x9B, 0xE6, 0x40, 0x69, 0x7D, 0xE7,
  27907. 0x47, 0x61, 0x3A, 0x5F, 0xC1, 0x95, 0xE8, 0xB9,
  27908. 0xF3, 0x28, 0x85, 0x2A, 0x57, 0x9D, 0xB8, 0xF9,
  27909. 0x9B, 0x1D, 0x00, 0x34, 0x47, 0x9E, 0xA9, 0xC5,
  27910. 0x59, 0x5F, 0x47, 0xC4, 0xB2, 0xF5, 0x4F, 0xF2,
  27911. 0x15, 0x08, 0xD3, 0x75, 0x14, 0xDC, 0xF7, 0xA8,
  27912. 0xE1, 0x43, 0xA6, 0x05, 0x8C, 0x09, 0xA6, 0xBF,
  27913. 0x2C, 0x98, 0x58, 0xCA, 0x37, 0xC2, 0x58, 0x06,
  27914. 0x5A, 0xE6, 0xBF, 0x75, 0x32, 0xBC, 0x8B, 0x5B,
  27915. 0x63, 0x38, 0x38, 0x66, 0xE0, 0x75, 0x3C, 0x5A,
  27916. 0xC0, 0xE7, 0x27, 0x09, 0xF8, 0x44, 0x5F, 0x2E,
  27917. 0x61, 0x78, 0xE0, 0x65, 0x85, 0x7E, 0x0E, 0xDA,
  27918. 0x10, 0xF6, 0x82, 0x06, 0xB6, 0x35, 0x05, 0xED,
  27919. 0x87, 0xE5, 0x34, 0xFB, 0x28, 0x31, 0xFF, 0x95,
  27920. 0x7F, 0xB7, 0xDC, 0x61, 0x9D, 0xAE, 0x61, 0x30,
  27921. 0x1E, 0xEA, 0xCC, 0x2F, 0xDA, 0x36, 0x80, 0xEA,
  27922. 0x49, 0x99, 0x25, 0x8A, 0x83, 0x3C, 0xEA, 0x8F,
  27923. 0xC6, 0x7C, 0x6D, 0x19, 0x48, 0x7F, 0xB4, 0x49,
  27924. 0x05, 0x9F, 0x26, 0xCC, 0x8A, 0xAB, 0x65, 0x5A,
  27925. 0xB5, 0x8B, 0x7C, 0xC7, 0x96, 0xE2, 0x4E, 0x9A,
  27926. 0x39, 0x40, 0x95, 0x75, 0x4F, 0x5F, 0x8B, 0xAE
  27927. };
  27928. static const byte id[] = {
  27929. 0x32, 0x30, 0x31, 0x31, 0x2D, 0x30, 0x32, 0x00,
  27930. 0x74, 0x65, 0x6C, 0x3A, 0x2B, 0x34, 0x34, 0x37,
  27931. 0x37, 0x30, 0x30, 0x39, 0x30, 0x30, 0x31, 0x32,
  27932. 0x33, 0x00
  27933. };
  27934. static word32 idSz = sizeof(id);
  27935. byte ssv[] = {
  27936. 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0,
  27937. 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0
  27938. };
  27939. static word16 ssvSz = sizeof(ssv);
  27940. static const byte expAuth[] = {
  27941. 0x04,
  27942. 0x44, 0xE8, 0xAD, 0x44, 0xAB, 0x85, 0x92, 0xA6,
  27943. 0xA5, 0xA3, 0xDD, 0xCA, 0x5C, 0xF8, 0x96, 0xC7,
  27944. 0x18, 0x04, 0x36, 0x06, 0xA0, 0x1D, 0x65, 0x0D,
  27945. 0xEF, 0x37, 0xA0, 0x1F, 0x37, 0xC2, 0x28, 0xC3,
  27946. 0x32, 0xFC, 0x31, 0x73, 0x54, 0xE2, 0xC2, 0x74,
  27947. 0xD4, 0xDA, 0xF8, 0xAD, 0x00, 0x10, 0x54, 0xC7,
  27948. 0x6C, 0xE5, 0x79, 0x71, 0xC6, 0xF4, 0x48, 0x6D,
  27949. 0x57, 0x23, 0x04, 0x32, 0x61, 0xC5, 0x06, 0xEB,
  27950. 0xF5, 0xBE, 0x43, 0x8F, 0x53, 0xDE, 0x04, 0xF0,
  27951. 0x67, 0xC7, 0x76, 0xE0, 0xDD, 0x3B, 0x71, 0xA6,
  27952. 0x29, 0x01, 0x33, 0x28, 0x37, 0x25, 0xA5, 0x32,
  27953. 0xF2, 0x1A, 0xF1, 0x45, 0x12, 0x6D, 0xC1, 0xD7,
  27954. 0x77, 0xEC, 0xC2, 0x7B, 0xE5, 0x08, 0x35, 0xBD,
  27955. 0x28, 0x09, 0x8B, 0x8A, 0x73, 0xD9, 0xF8, 0x01,
  27956. 0xD8, 0x93, 0x79, 0x3A, 0x41, 0xFF, 0x5C, 0x49,
  27957. 0xB8, 0x7E, 0x79, 0xF2, 0xBE, 0x4D, 0x56, 0xCE,
  27958. 0x55, 0x7E, 0x13, 0x4A, 0xD8, 0x5B, 0xB1, 0xD4,
  27959. 0xB9, 0xCE, 0x4F, 0x8B, 0xE4, 0xB0, 0x8A, 0x12,
  27960. 0xBA, 0xBF, 0x55, 0xB1, 0xD6, 0xF1, 0xD7, 0xA6,
  27961. 0x38, 0x01, 0x9E, 0xA2, 0x8E, 0x15, 0xAB, 0x1C,
  27962. 0x9F, 0x76, 0x37, 0x5F, 0xDD, 0x12, 0x10, 0xD4,
  27963. 0xF4, 0x35, 0x1B, 0x9A, 0x00, 0x94, 0x86, 0xB7,
  27964. 0xF3, 0xED, 0x46, 0xC9, 0x65, 0xDE, 0xD2, 0xD8,
  27965. 0x0D, 0xAD, 0xE4, 0xF3, 0x8C, 0x67, 0x21, 0xD5,
  27966. 0x2C, 0x3A, 0xD1, 0x03, 0xA1, 0x0E, 0xBD, 0x29,
  27967. 0x59, 0x24, 0x8B, 0x4E, 0xF0, 0x06, 0x83, 0x6B,
  27968. 0xF0, 0x97, 0x44, 0x8E, 0x61, 0x07, 0xC9, 0xED,
  27969. 0xEE, 0x9F, 0xB7, 0x04, 0x82, 0x3D, 0xF1, 0x99,
  27970. 0xF8, 0x32, 0xC9, 0x05, 0xAE, 0x45, 0xF8, 0xA2,
  27971. 0x47, 0xA0, 0x72, 0xD8, 0xEF, 0x72, 0x9E, 0xAB,
  27972. 0xC5, 0xE2, 0x75, 0x74, 0xB0, 0x77, 0x39, 0xB3,
  27973. 0x4B, 0xE7, 0x4A, 0x53, 0x2F, 0x74, 0x7B, 0x86
  27974. };
  27975. static const byte encSsv[] = {
  27976. 0x89, 0xE0, 0xBC, 0x66, 0x1A, 0xA1, 0xE9, 0x16,
  27977. 0x38, 0xE6, 0xAC, 0xC8, 0x4E, 0x49, 0x65, 0x07
  27978. };
  27979. int ret;
  27980. byte auth[257];
  27981. word16 authSz = sizeof(auth);
  27982. ret = wc_ImportSakkePublicKey(key, pubData, sizeof(pubData), 0);
  27983. if (ret != 0)
  27984. return -10334;
  27985. ret = wc_SetSakkeIdentity(key, id, idSz);
  27986. if (ret != 0)
  27987. return -10335;
  27988. ret = wc_MakeSakkeEncapsulatedSSV(key, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  27989. auth, &authSz);
  27990. if (ret != 0)
  27991. return -10336;
  27992. if (authSz != 257)
  27993. return -10337;
  27994. if (XMEMCMP(ssv, encSsv, ssvSz) != 0)
  27995. return -10338;
  27996. if (XMEMCMP(auth, expAuth, authSz) != 0)
  27997. return -10339;
  27998. return 0;
  27999. }
  28000. static int sakke_make_key_test(SakkeKey* priv, SakkeKey* pub, SakkeKey* key,
  28001. WC_RNG* rng, ecc_point* rsk)
  28002. {
  28003. int ret;
  28004. byte data[440];
  28005. byte pubData[257];
  28006. word32 sz;
  28007. char mail[] = "test@wolfssl.com";
  28008. byte* id = (byte*)mail;
  28009. word32 idSz = (word32)XSTRLEN(mail);
  28010. int valid;
  28011. ecc_point* pubKey = rsk;
  28012. ret = wc_InitSakkeKey_ex(key, 128, ECC_SAKKE_1, NULL, INVALID_DEVID);
  28013. if (ret != 0)
  28014. return -10339;
  28015. ret = wc_MakeSakkeKey(priv, rng);
  28016. if (ret != 0)
  28017. return -10340;
  28018. ret = wc_ExportSakkeKey(priv, NULL, &sz);
  28019. if (ret != LENGTH_ONLY_E)
  28020. return -10341;
  28021. if (sz != 384)
  28022. return -10342;
  28023. sz--;
  28024. ret = wc_ExportSakkeKey(priv, data, &sz);
  28025. if (ret == 0)
  28026. return -10343;
  28027. sz++;
  28028. ret = wc_ExportSakkeKey(priv, data, &sz);
  28029. if (ret != 0)
  28030. return -10344;
  28031. if (sz != 384)
  28032. return -10345;
  28033. ret = wc_ImportSakkeKey(key, data, sz - 1);
  28034. if (ret == 0)
  28035. return -10346;
  28036. ret = wc_ImportSakkeKey(key, data, sz);
  28037. if (ret != 0)
  28038. return -10347;
  28039. wc_FreeSakkeKey(key);
  28040. ret = wc_InitSakkeKey_ex(key, 128, ECC_SAKKE_1, NULL, INVALID_DEVID);
  28041. if (ret != 0)
  28042. return -10348;
  28043. ret = wc_ExportSakkePrivateKey(priv, NULL, &sz);
  28044. if (ret != LENGTH_ONLY_E)
  28045. return -10349;
  28046. if (sz != 128)
  28047. return -10350;
  28048. sz--;
  28049. ret = wc_ExportSakkePrivateKey(priv, data, &sz);
  28050. if (ret == 0)
  28051. return -10351;
  28052. sz++;
  28053. ret = wc_ExportSakkePrivateKey(priv, data, &sz);
  28054. if (ret != 0)
  28055. return -10352;
  28056. if (sz != 128)
  28057. return -10353;
  28058. ret = wc_ImportSakkePrivateKey(key, data, sz - 1);
  28059. if (ret == 0)
  28060. return -10354;
  28061. ret = wc_ImportSakkePrivateKey(key, data, sz);
  28062. if (ret != 0)
  28063. return -10355;
  28064. ret = wc_MakeSakkePublicKey(key, pubKey);
  28065. if (ret != 0)
  28066. return -10356;
  28067. ret = wc_ExportSakkePublicKey(priv, NULL, &sz, 1);
  28068. if (ret != LENGTH_ONLY_E)
  28069. return -10357;
  28070. if (sz != 256)
  28071. return -10358;
  28072. sz--;
  28073. ret = wc_ExportSakkePublicKey(priv, data, &sz, 1);
  28074. if (ret == 0)
  28075. return -10359;
  28076. sz++;
  28077. ret = wc_ExportSakkePublicKey(priv, data, &sz, 1);
  28078. if (ret != 0)
  28079. return -10360;
  28080. if (sz != 256)
  28081. return -10361;
  28082. ret = wc_ImportSakkePublicKey(pub, data, sz - 1, 1);
  28083. if (ret == 0)
  28084. return -10362;
  28085. ret = wc_ImportSakkePublicKey(pub, data, sz, 1);
  28086. if (ret != 0)
  28087. return -10363;
  28088. ret = wc_ExportSakkePublicKey(pub, pubData, &sz, 1);
  28089. if (ret != 0)
  28090. return -10364;
  28091. if (sz != 256)
  28092. return -10365;
  28093. if (XMEMCMP(data, pubData, sz) != 0)
  28094. return -10366;
  28095. ret = wc_MakeSakkeRsk(priv, id, idSz, rsk);
  28096. if (ret != 0)
  28097. return -10367;
  28098. ret = wc_ValidateSakkeRsk(priv, id, idSz, rsk, &valid);
  28099. if (ret != 0)
  28100. return -10368;
  28101. if (valid != 1)
  28102. return -10369;
  28103. ret = wc_ValidateSakkeRsk(pub, id, idSz, rsk, &valid);
  28104. if (ret != 0)
  28105. return -10370;
  28106. if (valid != 1)
  28107. return -10371;
  28108. sz = sizeof(data);
  28109. ret = wc_EncodeSakkeRsk(priv, rsk, data, &sz, 1);
  28110. if (ret != 0)
  28111. return -10372;
  28112. if (sz != 256)
  28113. return -10373;
  28114. ret = wc_DecodeSakkeRsk(priv, data, sz, rsk);
  28115. if (ret != 0)
  28116. return -10374;
  28117. sz = sizeof(pubData);
  28118. ret = wc_EncodeSakkeRsk(priv, rsk, pubData, &sz, 0);
  28119. if (ret != 0)
  28120. return -10375;
  28121. if (sz != sizeof(pubData))
  28122. return -10376;
  28123. ret = wc_DecodeSakkeRsk(priv, pubData, sz, rsk);
  28124. if (ret != 0)
  28125. return -10377;
  28126. wc_FreeSakkeKey(key);
  28127. return 0;
  28128. }
  28129. static int sakke_op_test(SakkeKey* priv, SakkeKey* pub, WC_RNG* rng,
  28130. ecc_point* rsk)
  28131. {
  28132. int ret;
  28133. byte ssv[16];
  28134. word16 ssvSz;
  28135. byte auth[257];
  28136. word16 authSz;
  28137. char mail[] = "test@wolfssl.com";
  28138. byte* id = (byte*)mail;
  28139. word32 idSz = (word32)XSTRLEN(mail);
  28140. byte pointI[256];
  28141. word32 sz;
  28142. ret = wc_GenerateSakkeSSV(pub, rng, NULL, &ssvSz);
  28143. if (ret != LENGTH_ONLY_E)
  28144. return -10375;
  28145. if (ssvSz != 16)
  28146. return -10376;
  28147. ssvSz += 128;
  28148. ret = wc_GenerateSakkeSSV(pub, rng, ssv, &ssvSz);
  28149. if (ret == 0)
  28150. return -10377;
  28151. ssvSz -= 128;
  28152. ret = wc_GenerateSakkeSSV(pub, rng, ssv, &ssvSz);
  28153. if (ret != 0)
  28154. return -10378;
  28155. if (ssvSz != 16)
  28156. return -10379;
  28157. ret = wc_GetSakkeAuthSize(pub, &authSz);
  28158. if (ret != 0)
  28159. return -10380;
  28160. ret = wc_SetSakkeIdentity(pub, id, idSz);
  28161. if (ret != 0)
  28162. return -10380;
  28163. ret = wc_MakeSakkeEncapsulatedSSV(pub, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  28164. NULL, &authSz);
  28165. if (ret != LENGTH_ONLY_E)
  28166. return -10381;
  28167. if (authSz != 257)
  28168. return -10382;
  28169. authSz--;
  28170. ret = wc_MakeSakkeEncapsulatedSSV(pub, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  28171. auth, &authSz);
  28172. if (ret == 0)
  28173. return -10383;
  28174. authSz++;
  28175. ret = wc_MakeSakkeEncapsulatedSSV(pub, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  28176. auth, &authSz);
  28177. if (ret != 0)
  28178. return -10384;
  28179. if (authSz != 257)
  28180. return -10385;
  28181. ret = wc_GetSakkePointI(pub, NULL, &sz);
  28182. if (ret != LENGTH_ONLY_E)
  28183. return -10386;
  28184. if (sz != 256)
  28185. return -10387;
  28186. ret = wc_GetSakkePointI(pub, pointI, &sz);
  28187. if (ret != 0)
  28188. return -10388;
  28189. if (sz != 256)
  28190. return -10389;
  28191. /* Bogus identity - make it check and regenerate I. */
  28192. ret = wc_MakeSakkePointI(pub, ssv, ssvSz);
  28193. if (ret != 0)
  28194. return -10391;
  28195. ret = wc_MakeSakkeEncapsulatedSSV(pub, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  28196. auth, &authSz);
  28197. if (ret != 0)
  28198. return -10392;
  28199. if (authSz != 257)
  28200. return -10393;
  28201. ret = wc_SetSakkeRsk(priv, rsk, NULL, 0);
  28202. if (ret != 0)
  28203. return -10392;
  28204. ret = wc_SetSakkeIdentity(priv, id, idSz);
  28205. if (ret != 0)
  28206. return -10392;
  28207. authSz--;
  28208. ret = wc_DeriveSakkeSSV(priv, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  28209. authSz);
  28210. if (ret == 0)
  28211. return -10394;
  28212. authSz++;
  28213. ret = wc_DeriveSakkeSSV(priv, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  28214. authSz);
  28215. if (ret != 0)
  28216. return -10395;
  28217. ssv[0] ^= 0x80;
  28218. ret = wc_DeriveSakkeSSV(priv, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  28219. authSz);
  28220. if (ret != SAKKE_VERIFY_FAIL_E)
  28221. return -10396;
  28222. ssv[0] ^= 0x80;
  28223. /* Bogus identity - make it check and regenerate I. */
  28224. ret = wc_MakeSakkePointI(pub, ssv, idSz);
  28225. if (ret != 0)
  28226. return -10397;
  28227. ret = wc_DeriveSakkeSSV(priv, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  28228. authSz);
  28229. if (ret != 0)
  28230. return -10398;
  28231. return 0;
  28232. }
  28233. int sakke_test(void)
  28234. {
  28235. int ret = 0;
  28236. WC_RNG rng;
  28237. int rng_inited = 0;
  28238. SakkeKey* priv = NULL;
  28239. SakkeKey* pub = NULL;
  28240. SakkeKey* key = NULL;
  28241. ecc_point* rsk = NULL;
  28242. priv = (SakkeKey*)XMALLOC(sizeof(SakkeKey), HEAP_HINT,
  28243. DYNAMIC_TYPE_TMP_BUFFER);
  28244. if (priv == NULL)
  28245. ret = -10404;
  28246. else
  28247. XMEMSET(priv, 0, sizeof(*priv));
  28248. if (ret == 0) {
  28249. pub = (SakkeKey*)XMALLOC(sizeof(SakkeKey), HEAP_HINT,
  28250. DYNAMIC_TYPE_TMP_BUFFER);
  28251. if (pub == NULL)
  28252. ret = -10405;
  28253. else
  28254. XMEMSET(pub, 0, sizeof(*pub));
  28255. }
  28256. if (ret == 0) {
  28257. key = (SakkeKey*)XMALLOC(sizeof(SakkeKey), HEAP_HINT,
  28258. DYNAMIC_TYPE_TMP_BUFFER);
  28259. if (key == NULL)
  28260. ret = -10406;
  28261. else
  28262. XMEMSET(key, 0, sizeof(*key));
  28263. }
  28264. if (ret == 0) {
  28265. #ifndef HAVE_FIPS
  28266. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  28267. #else
  28268. ret = wc_InitRng(&rng);
  28269. #endif
  28270. if (ret == 0)
  28271. rng_inited = 1;
  28272. else
  28273. ret = -10400;
  28274. }
  28275. if (ret == 0) {
  28276. rsk = wc_ecc_new_point();
  28277. if (rsk == NULL)
  28278. ret = -10401;
  28279. }
  28280. if (ret == 0) {
  28281. ret = wc_InitSakkeKey(pub, HEAP_HINT, INVALID_DEVID);
  28282. if (ret != 0)
  28283. ret = -10402;
  28284. }
  28285. if (ret == 0) {
  28286. ret = wc_InitSakkeKey(priv, HEAP_HINT, INVALID_DEVID);
  28287. if (ret != 0)
  28288. ret = -10403;
  28289. }
  28290. if (ret == 0) {
  28291. ret = sakke_api_test(&rng, key, rsk);
  28292. }
  28293. if (ret == 0) {
  28294. ret = sakke_kat_derive_test(pub, rsk);
  28295. }
  28296. if (ret == 0) {
  28297. ret = sakke_kat_encapsulate_test(pub);
  28298. }
  28299. if (ret == 0) {
  28300. ret = sakke_make_key_test(priv, pub, key, &rng, rsk);
  28301. }
  28302. if (ret == 0) {
  28303. ret = sakke_op_test(priv, pub, &rng, rsk);
  28304. }
  28305. if (rsk != NULL) {
  28306. wc_ecc_forcezero_point(rsk);
  28307. wc_ecc_del_point(rsk);
  28308. }
  28309. if (rng_inited)
  28310. wc_FreeRng(&rng);
  28311. if (key != NULL)
  28312. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28313. if (pub != NULL) {
  28314. wc_FreeSakkeKey(pub);
  28315. XFREE(pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28316. }
  28317. if (priv != NULL) {
  28318. wc_FreeSakkeKey(priv);
  28319. XFREE(priv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28320. }
  28321. return ret;
  28322. }
  28323. #endif /* WOLFCRYPT_HAVE_SAKKE */
  28324. #if defined(WOLFSSL_CMAC) && !defined(NO_AES)
  28325. typedef struct CMAC_Test_Case {
  28326. int type;
  28327. int partial;
  28328. const byte* m;
  28329. word32 mSz;
  28330. const byte* k;
  28331. word32 kSz;
  28332. const byte* t;
  28333. word32 tSz;
  28334. } CMAC_Test_Case;
  28335. WOLFSSL_TEST_SUBROUTINE int cmac_test(void)
  28336. {
  28337. #ifdef WOLFSSL_AES_128
  28338. WOLFSSL_SMALL_STACK_STATIC const byte k128[] =
  28339. {
  28340. 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
  28341. 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c
  28342. };
  28343. #define KLEN_128 (sizeof(k128))
  28344. #endif
  28345. #ifdef WOLFSSL_AES_192
  28346. WOLFSSL_SMALL_STACK_STATIC const byte k192[] =
  28347. {
  28348. 0x8e, 0x73, 0xb0, 0xf7, 0xda, 0x0e, 0x64, 0x52,
  28349. 0xc8, 0x10, 0xf3, 0x2b, 0x80, 0x90, 0x79, 0xe5,
  28350. 0x62, 0xf8, 0xea, 0xd2, 0x52, 0x2c, 0x6b, 0x7b
  28351. };
  28352. #define KLEN_192 (sizeof(k192))
  28353. #endif
  28354. #ifdef WOLFSSL_AES_256
  28355. WOLFSSL_SMALL_STACK_STATIC const byte k256[] =
  28356. {
  28357. 0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe,
  28358. 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
  28359. 0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7,
  28360. 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4
  28361. };
  28362. #define KLEN_256 (sizeof(k256))
  28363. #endif
  28364. WOLFSSL_SMALL_STACK_STATIC const byte m[] =
  28365. {
  28366. 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
  28367. 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
  28368. 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c,
  28369. 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51,
  28370. 0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11,
  28371. 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef,
  28372. 0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17,
  28373. 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10
  28374. };
  28375. #define MLEN_0 (0)
  28376. #define MLEN_128 (128/8)
  28377. #define MLEN_320 (320/8)
  28378. #define MLEN_319 (MLEN_320 - 1)
  28379. #define MLEN_512 (512/8)
  28380. #ifdef WOLFSSL_AES_128
  28381. WOLFSSL_SMALL_STACK_STATIC const byte t128_0[] =
  28382. {
  28383. 0xbb, 0x1d, 0x69, 0x29, 0xe9, 0x59, 0x37, 0x28,
  28384. 0x7f, 0xa3, 0x7d, 0x12, 0x9b, 0x75, 0x67, 0x46
  28385. };
  28386. WOLFSSL_SMALL_STACK_STATIC const byte t128_128[] =
  28387. {
  28388. 0x07, 0x0a, 0x16, 0xb4, 0x6b, 0x4d, 0x41, 0x44,
  28389. 0xf7, 0x9b, 0xdd, 0x9d, 0xd0, 0x4a, 0x28, 0x7c
  28390. };
  28391. WOLFSSL_SMALL_STACK_STATIC const byte t128_319[] =
  28392. {
  28393. 0x2c, 0x17, 0x84, 0x4c, 0x93, 0x1c, 0x07, 0x95,
  28394. 0x15, 0x92, 0x73, 0x0a, 0x34, 0xd0, 0xd9, 0xd2
  28395. };
  28396. WOLFSSL_SMALL_STACK_STATIC const byte t128_320[] =
  28397. {
  28398. 0xdf, 0xa6, 0x67, 0x47, 0xde, 0x9a, 0xe6, 0x30,
  28399. 0x30, 0xca, 0x32, 0x61, 0x14, 0x97, 0xc8, 0x27
  28400. };
  28401. WOLFSSL_SMALL_STACK_STATIC const byte t128_512[] =
  28402. {
  28403. 0x51, 0xf0, 0xbe, 0xbf, 0x7e, 0x3b, 0x9d, 0x92,
  28404. 0xfc, 0x49, 0x74, 0x17, 0x79, 0x36, 0x3c, 0xfe
  28405. };
  28406. #endif
  28407. #ifdef WOLFSSL_AES_192
  28408. WOLFSSL_SMALL_STACK_STATIC const byte t192_0[] =
  28409. {
  28410. 0xd1, 0x7d, 0xdf, 0x46, 0xad, 0xaa, 0xcd, 0xe5,
  28411. 0x31, 0xca, 0xc4, 0x83, 0xde, 0x7a, 0x93, 0x67
  28412. };
  28413. WOLFSSL_SMALL_STACK_STATIC const byte t192_128[] =
  28414. {
  28415. 0x9e, 0x99, 0xa7, 0xbf, 0x31, 0xe7, 0x10, 0x90,
  28416. 0x06, 0x62, 0xf6, 0x5e, 0x61, 0x7c, 0x51, 0x84
  28417. };
  28418. WOLFSSL_SMALL_STACK_STATIC const byte t192_320[] =
  28419. {
  28420. 0x8a, 0x1d, 0xe5, 0xbe, 0x2e, 0xb3, 0x1a, 0xad,
  28421. 0x08, 0x9a, 0x82, 0xe6, 0xee, 0x90, 0x8b, 0x0e
  28422. };
  28423. WOLFSSL_SMALL_STACK_STATIC const byte t192_512[] =
  28424. {
  28425. 0xa1, 0xd5, 0xdf, 0x0e, 0xed, 0x79, 0x0f, 0x79,
  28426. 0x4d, 0x77, 0x58, 0x96, 0x59, 0xf3, 0x9a, 0x11
  28427. };
  28428. #endif
  28429. #ifdef WOLFSSL_AES_256
  28430. WOLFSSL_SMALL_STACK_STATIC const byte t256_0[] =
  28431. {
  28432. 0x02, 0x89, 0x62, 0xf6, 0x1b, 0x7b, 0xf8, 0x9e,
  28433. 0xfc, 0x6b, 0x55, 0x1f, 0x46, 0x67, 0xd9, 0x83
  28434. };
  28435. WOLFSSL_SMALL_STACK_STATIC const byte t256_128[] =
  28436. {
  28437. 0x28, 0xa7, 0x02, 0x3f, 0x45, 0x2e, 0x8f, 0x82,
  28438. 0xbd, 0x4b, 0xf2, 0x8d, 0x8c, 0x37, 0xc3, 0x5c
  28439. };
  28440. WOLFSSL_SMALL_STACK_STATIC const byte t256_320[] =
  28441. {
  28442. 0xaa, 0xf3, 0xd8, 0xf1, 0xde, 0x56, 0x40, 0xc2,
  28443. 0x32, 0xf5, 0xb1, 0x69, 0xb9, 0xc9, 0x11, 0xe6
  28444. };
  28445. WOLFSSL_SMALL_STACK_STATIC const byte t256_512[] =
  28446. {
  28447. 0xe1, 0x99, 0x21, 0x90, 0x54, 0x9f, 0x6e, 0xd5,
  28448. 0x69, 0x6a, 0x2c, 0x05, 0x6c, 0x31, 0x54, 0x10
  28449. };
  28450. #endif
  28451. const CMAC_Test_Case testCases[] =
  28452. {
  28453. #ifdef WOLFSSL_AES_128
  28454. {WC_CMAC_AES, 0, m, MLEN_0, k128, KLEN_128, t128_0, AES_BLOCK_SIZE},
  28455. {WC_CMAC_AES, 0, m, MLEN_128, k128, KLEN_128, t128_128, AES_BLOCK_SIZE},
  28456. {WC_CMAC_AES, 0, m, MLEN_320, k128, KLEN_128, t128_320, AES_BLOCK_SIZE},
  28457. {WC_CMAC_AES, 0, m, MLEN_512, k128, KLEN_128, t128_512, AES_BLOCK_SIZE},
  28458. {WC_CMAC_AES, 5, m, MLEN_512, k128, KLEN_128, t128_512, AES_BLOCK_SIZE},
  28459. #endif
  28460. #ifdef WOLFSSL_AES_192
  28461. {WC_CMAC_AES, 0, m, MLEN_0, k192, KLEN_192, t192_0, AES_BLOCK_SIZE},
  28462. {WC_CMAC_AES, 0, m, MLEN_128, k192, KLEN_192, t192_128, AES_BLOCK_SIZE},
  28463. {WC_CMAC_AES, 0, m, MLEN_320, k192, KLEN_192, t192_320, AES_BLOCK_SIZE},
  28464. {WC_CMAC_AES, 0, m, MLEN_512, k192, KLEN_192, t192_512, AES_BLOCK_SIZE},
  28465. #endif
  28466. #ifdef WOLFSSL_AES_256
  28467. {WC_CMAC_AES, 0, m, MLEN_0, k256, KLEN_256, t256_0, AES_BLOCK_SIZE},
  28468. {WC_CMAC_AES, 0, m, MLEN_128, k256, KLEN_256, t256_128, AES_BLOCK_SIZE},
  28469. {WC_CMAC_AES, 0, m, MLEN_320, k256, KLEN_256, t256_320, AES_BLOCK_SIZE},
  28470. {WC_CMAC_AES, 0, m, MLEN_512, k256, KLEN_256, t256_512, AES_BLOCK_SIZE},
  28471. #endif
  28472. #ifdef WOLFSSL_AES_128
  28473. {WC_CMAC_AES, 0, m, MLEN_319, k128, KLEN_128, t128_319, AES_BLOCK_SIZE}
  28474. #endif
  28475. };
  28476. #ifdef WOLFSSL_SMALL_STACK
  28477. Cmac *cmac;
  28478. #else
  28479. Cmac cmac[1];
  28480. #endif
  28481. byte tag[AES_BLOCK_SIZE];
  28482. const CMAC_Test_Case* tc;
  28483. word32 i, tagSz;
  28484. int ret;
  28485. #ifdef WOLFSSL_SMALL_STACK
  28486. if ((cmac = (Cmac *)XMALLOC(sizeof *cmac, HEAP_HINT, DYNAMIC_TYPE_CMAC)) == NULL)
  28487. ERROR_OUT(-12009, out);
  28488. #endif
  28489. for (i = 0, tc = testCases;
  28490. i < sizeof(testCases)/sizeof(CMAC_Test_Case);
  28491. i++, tc++) {
  28492. XMEMSET(tag, 0, sizeof(tag));
  28493. tagSz = AES_BLOCK_SIZE;
  28494. #if !defined(HAVE_FIPS) || \
  28495. defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3)
  28496. if (wc_InitCmac_ex(cmac, tc->k, tc->kSz, tc->type, NULL, HEAP_HINT, devId) != 0)
  28497. #else
  28498. if (wc_InitCmac(cmac, tc->k, tc->kSz, tc->type, NULL) != 0)
  28499. #endif
  28500. {
  28501. ERROR_OUT(-12000, out);
  28502. }
  28503. if (tc->partial) {
  28504. if (wc_CmacUpdate(cmac, tc->m,
  28505. tc->mSz/2 - tc->partial) != 0)
  28506. ERROR_OUT(-12001, out);
  28507. if (wc_CmacUpdate(cmac, tc->m + tc->mSz/2 - tc->partial,
  28508. tc->mSz/2 + tc->partial) != 0)
  28509. ERROR_OUT(-12002, out);
  28510. }
  28511. else {
  28512. if (wc_CmacUpdate(cmac, tc->m, tc->mSz) != 0)
  28513. ERROR_OUT(-12003, out);
  28514. }
  28515. if (wc_CmacFinal(cmac, tag, &tagSz) != 0)
  28516. ERROR_OUT(-12004, out);
  28517. if (XMEMCMP(tag, tc->t, AES_BLOCK_SIZE) != 0)
  28518. ERROR_OUT(-12005, out);
  28519. XMEMSET(tag, 0, sizeof(tag));
  28520. tagSz = sizeof(tag);
  28521. if (wc_AesCmacGenerate(tag, &tagSz, tc->m, tc->mSz,
  28522. tc->k, tc->kSz) != 0)
  28523. ERROR_OUT(-12006, out);
  28524. if (XMEMCMP(tag, tc->t, AES_BLOCK_SIZE) != 0)
  28525. ERROR_OUT(-12007, out);
  28526. if (wc_AesCmacVerify(tc->t, tc->tSz, tc->m, tc->mSz,
  28527. tc->k, tc->kSz) != 0)
  28528. ERROR_OUT(-12008, out);
  28529. }
  28530. ret = 0;
  28531. out:
  28532. #ifdef WOLFSSL_SMALL_STACK
  28533. if (cmac)
  28534. XFREE(cmac, HEAP_HINT, DYNAMIC_TYPE_CMAC);
  28535. #endif
  28536. return ret;
  28537. }
  28538. #endif /* NO_AES && WOLFSSL_CMAC */
  28539. #if defined(WOLFSSL_SIPHASH)
  28540. #if WOLFSSL_SIPHASH_CROUNDS == 2 && WOLFSSL_SIPHASH_DROUNDS == 4
  28541. /* Test vectors from:
  28542. * https://github.com/veorq/SipHash/blob/master/vectors.h
  28543. */
  28544. static const unsigned char siphash_key[SIPHASH_KEY_SIZE] = {
  28545. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  28546. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
  28547. };
  28548. static const unsigned char siphash_msg[64] = {
  28549. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  28550. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  28551. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  28552. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
  28553. 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  28554. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
  28555. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  28556. 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f
  28557. };
  28558. static const unsigned char siphash_r8[64][SIPHASH_MAC_SIZE_8] = {
  28559. { 0x31, 0x0e, 0x0e, 0xdd, 0x47, 0xdb, 0x6f, 0x72, },
  28560. { 0xfd, 0x67, 0xdc, 0x93, 0xc5, 0x39, 0xf8, 0x74, },
  28561. { 0x5a, 0x4f, 0xa9, 0xd9, 0x09, 0x80, 0x6c, 0x0d, },
  28562. { 0x2d, 0x7e, 0xfb, 0xd7, 0x96, 0x66, 0x67, 0x85, },
  28563. { 0xb7, 0x87, 0x71, 0x27, 0xe0, 0x94, 0x27, 0xcf, },
  28564. { 0x8d, 0xa6, 0x99, 0xcd, 0x64, 0x55, 0x76, 0x18, },
  28565. { 0xce, 0xe3, 0xfe, 0x58, 0x6e, 0x46, 0xc9, 0xcb, },
  28566. { 0x37, 0xd1, 0x01, 0x8b, 0xf5, 0x00, 0x02, 0xab, },
  28567. { 0x62, 0x24, 0x93, 0x9a, 0x79, 0xf5, 0xf5, 0x93, },
  28568. { 0xb0, 0xe4, 0xa9, 0x0b, 0xdf, 0x82, 0x00, 0x9e, },
  28569. { 0xf3, 0xb9, 0xdd, 0x94, 0xc5, 0xbb, 0x5d, 0x7a, },
  28570. { 0xa7, 0xad, 0x6b, 0x22, 0x46, 0x2f, 0xb3, 0xf4, },
  28571. { 0xfb, 0xe5, 0x0e, 0x86, 0xbc, 0x8f, 0x1e, 0x75, },
  28572. { 0x90, 0x3d, 0x84, 0xc0, 0x27, 0x56, 0xea, 0x14, },
  28573. { 0xee, 0xf2, 0x7a, 0x8e, 0x90, 0xca, 0x23, 0xf7, },
  28574. { 0xe5, 0x45, 0xbe, 0x49, 0x61, 0xca, 0x29, 0xa1, },
  28575. { 0xdb, 0x9b, 0xc2, 0x57, 0x7f, 0xcc, 0x2a, 0x3f, },
  28576. { 0x94, 0x47, 0xbe, 0x2c, 0xf5, 0xe9, 0x9a, 0x69, },
  28577. { 0x9c, 0xd3, 0x8d, 0x96, 0xf0, 0xb3, 0xc1, 0x4b, },
  28578. { 0xbd, 0x61, 0x79, 0xa7, 0x1d, 0xc9, 0x6d, 0xbb, },
  28579. { 0x98, 0xee, 0xa2, 0x1a, 0xf2, 0x5c, 0xd6, 0xbe, },
  28580. { 0xc7, 0x67, 0x3b, 0x2e, 0xb0, 0xcb, 0xf2, 0xd0, },
  28581. { 0x88, 0x3e, 0xa3, 0xe3, 0x95, 0x67, 0x53, 0x93, },
  28582. { 0xc8, 0xce, 0x5c, 0xcd, 0x8c, 0x03, 0x0c, 0xa8, },
  28583. { 0x94, 0xaf, 0x49, 0xf6, 0xc6, 0x50, 0xad, 0xb8, },
  28584. { 0xea, 0xb8, 0x85, 0x8a, 0xde, 0x92, 0xe1, 0xbc, },
  28585. { 0xf3, 0x15, 0xbb, 0x5b, 0xb8, 0x35, 0xd8, 0x17, },
  28586. { 0xad, 0xcf, 0x6b, 0x07, 0x63, 0x61, 0x2e, 0x2f, },
  28587. { 0xa5, 0xc9, 0x1d, 0xa7, 0xac, 0xaa, 0x4d, 0xde, },
  28588. { 0x71, 0x65, 0x95, 0x87, 0x66, 0x50, 0xa2, 0xa6, },
  28589. { 0x28, 0xef, 0x49, 0x5c, 0x53, 0xa3, 0x87, 0xad, },
  28590. { 0x42, 0xc3, 0x41, 0xd8, 0xfa, 0x92, 0xd8, 0x32, },
  28591. { 0xce, 0x7c, 0xf2, 0x72, 0x2f, 0x51, 0x27, 0x71, },
  28592. { 0xe3, 0x78, 0x59, 0xf9, 0x46, 0x23, 0xf3, 0xa7, },
  28593. { 0x38, 0x12, 0x05, 0xbb, 0x1a, 0xb0, 0xe0, 0x12, },
  28594. { 0xae, 0x97, 0xa1, 0x0f, 0xd4, 0x34, 0xe0, 0x15, },
  28595. { 0xb4, 0xa3, 0x15, 0x08, 0xbe, 0xff, 0x4d, 0x31, },
  28596. { 0x81, 0x39, 0x62, 0x29, 0xf0, 0x90, 0x79, 0x02, },
  28597. { 0x4d, 0x0c, 0xf4, 0x9e, 0xe5, 0xd4, 0xdc, 0xca, },
  28598. { 0x5c, 0x73, 0x33, 0x6a, 0x76, 0xd8, 0xbf, 0x9a, },
  28599. { 0xd0, 0xa7, 0x04, 0x53, 0x6b, 0xa9, 0x3e, 0x0e, },
  28600. { 0x92, 0x59, 0x58, 0xfc, 0xd6, 0x42, 0x0c, 0xad, },
  28601. { 0xa9, 0x15, 0xc2, 0x9b, 0xc8, 0x06, 0x73, 0x18, },
  28602. { 0x95, 0x2b, 0x79, 0xf3, 0xbc, 0x0a, 0xa6, 0xd4, },
  28603. { 0xf2, 0x1d, 0xf2, 0xe4, 0x1d, 0x45, 0x35, 0xf9, },
  28604. { 0x87, 0x57, 0x75, 0x19, 0x04, 0x8f, 0x53, 0xa9, },
  28605. { 0x10, 0xa5, 0x6c, 0xf5, 0xdf, 0xcd, 0x9a, 0xdb, },
  28606. { 0xeb, 0x75, 0x09, 0x5c, 0xcd, 0x98, 0x6c, 0xd0, },
  28607. { 0x51, 0xa9, 0xcb, 0x9e, 0xcb, 0xa3, 0x12, 0xe6, },
  28608. { 0x96, 0xaf, 0xad, 0xfc, 0x2c, 0xe6, 0x66, 0xc7, },
  28609. { 0x72, 0xfe, 0x52, 0x97, 0x5a, 0x43, 0x64, 0xee, },
  28610. { 0x5a, 0x16, 0x45, 0xb2, 0x76, 0xd5, 0x92, 0xa1, },
  28611. { 0xb2, 0x74, 0xcb, 0x8e, 0xbf, 0x87, 0x87, 0x0a, },
  28612. { 0x6f, 0x9b, 0xb4, 0x20, 0x3d, 0xe7, 0xb3, 0x81, },
  28613. { 0xea, 0xec, 0xb2, 0xa3, 0x0b, 0x22, 0xa8, 0x7f, },
  28614. { 0x99, 0x24, 0xa4, 0x3c, 0xc1, 0x31, 0x57, 0x24, },
  28615. { 0xbd, 0x83, 0x8d, 0x3a, 0xaf, 0xbf, 0x8d, 0xb7, },
  28616. { 0x0b, 0x1a, 0x2a, 0x32, 0x65, 0xd5, 0x1a, 0xea, },
  28617. { 0x13, 0x50, 0x79, 0xa3, 0x23, 0x1c, 0xe6, 0x60, },
  28618. { 0x93, 0x2b, 0x28, 0x46, 0xe4, 0xd7, 0x06, 0x66, },
  28619. { 0xe1, 0x91, 0x5f, 0x5c, 0xb1, 0xec, 0xa4, 0x6c, },
  28620. { 0xf3, 0x25, 0x96, 0x5c, 0xa1, 0x6d, 0x62, 0x9f, },
  28621. { 0x57, 0x5f, 0xf2, 0x8e, 0x60, 0x38, 0x1b, 0xe5, },
  28622. { 0x72, 0x45, 0x06, 0xeb, 0x4c, 0x32, 0x8a, 0x95, },
  28623. };
  28624. static const unsigned char siphash_r16[64][SIPHASH_MAC_SIZE_16] = {
  28625. { 0xa3, 0x81, 0x7f, 0x04, 0xba, 0x25, 0xa8, 0xe6,
  28626. 0x6d, 0xf6, 0x72, 0x14, 0xc7, 0x55, 0x02, 0x93, },
  28627. { 0xda, 0x87, 0xc1, 0xd8, 0x6b, 0x99, 0xaf, 0x44,
  28628. 0x34, 0x76, 0x59, 0x11, 0x9b, 0x22, 0xfc, 0x45, },
  28629. { 0x81, 0x77, 0x22, 0x8d, 0xa4, 0xa4, 0x5d, 0xc7,
  28630. 0xfc, 0xa3, 0x8b, 0xde, 0xf6, 0x0a, 0xff, 0xe4, },
  28631. { 0x9c, 0x70, 0xb6, 0x0c, 0x52, 0x67, 0xa9, 0x4e,
  28632. 0x5f, 0x33, 0xb6, 0xb0, 0x29, 0x85, 0xed, 0x51, },
  28633. { 0xf8, 0x81, 0x64, 0xc1, 0x2d, 0x9c, 0x8f, 0xaf,
  28634. 0x7d, 0x0f, 0x6e, 0x7c, 0x7b, 0xcd, 0x55, 0x79, },
  28635. { 0x13, 0x68, 0x87, 0x59, 0x80, 0x77, 0x6f, 0x88,
  28636. 0x54, 0x52, 0x7a, 0x07, 0x69, 0x0e, 0x96, 0x27, },
  28637. { 0x14, 0xee, 0xca, 0x33, 0x8b, 0x20, 0x86, 0x13,
  28638. 0x48, 0x5e, 0xa0, 0x30, 0x8f, 0xd7, 0xa1, 0x5e, },
  28639. { 0xa1, 0xf1, 0xeb, 0xbe, 0xd8, 0xdb, 0xc1, 0x53,
  28640. 0xc0, 0xb8, 0x4a, 0xa6, 0x1f, 0xf0, 0x82, 0x39, },
  28641. { 0x3b, 0x62, 0xa9, 0xba, 0x62, 0x58, 0xf5, 0x61,
  28642. 0x0f, 0x83, 0xe2, 0x64, 0xf3, 0x14, 0x97, 0xb4, },
  28643. { 0x26, 0x44, 0x99, 0x06, 0x0a, 0xd9, 0xba, 0xab,
  28644. 0xc4, 0x7f, 0x8b, 0x02, 0xbb, 0x6d, 0x71, 0xed, },
  28645. { 0x00, 0x11, 0x0d, 0xc3, 0x78, 0x14, 0x69, 0x56,
  28646. 0xc9, 0x54, 0x47, 0xd3, 0xf3, 0xd0, 0xfb, 0xba, },
  28647. { 0x01, 0x51, 0xc5, 0x68, 0x38, 0x6b, 0x66, 0x77,
  28648. 0xa2, 0xb4, 0xdc, 0x6f, 0x81, 0xe5, 0xdc, 0x18, },
  28649. { 0xd6, 0x26, 0xb2, 0x66, 0x90, 0x5e, 0xf3, 0x58,
  28650. 0x82, 0x63, 0x4d, 0xf6, 0x85, 0x32, 0xc1, 0x25, },
  28651. { 0x98, 0x69, 0xe2, 0x47, 0xe9, 0xc0, 0x8b, 0x10,
  28652. 0xd0, 0x29, 0x93, 0x4f, 0xc4, 0xb9, 0x52, 0xf7, },
  28653. { 0x31, 0xfc, 0xef, 0xac, 0x66, 0xd7, 0xde, 0x9c,
  28654. 0x7e, 0xc7, 0x48, 0x5f, 0xe4, 0x49, 0x49, 0x02, },
  28655. { 0x54, 0x93, 0xe9, 0x99, 0x33, 0xb0, 0xa8, 0x11,
  28656. 0x7e, 0x08, 0xec, 0x0f, 0x97, 0xcf, 0xc3, 0xd9, },
  28657. { 0x6e, 0xe2, 0xa4, 0xca, 0x67, 0xb0, 0x54, 0xbb,
  28658. 0xfd, 0x33, 0x15, 0xbf, 0x85, 0x23, 0x05, 0x77, },
  28659. { 0x47, 0x3d, 0x06, 0xe8, 0x73, 0x8d, 0xb8, 0x98,
  28660. 0x54, 0xc0, 0x66, 0xc4, 0x7a, 0xe4, 0x77, 0x40, },
  28661. { 0xa4, 0x26, 0xe5, 0xe4, 0x23, 0xbf, 0x48, 0x85,
  28662. 0x29, 0x4d, 0xa4, 0x81, 0xfe, 0xae, 0xf7, 0x23, },
  28663. { 0x78, 0x01, 0x77, 0x31, 0xcf, 0x65, 0xfa, 0xb0,
  28664. 0x74, 0xd5, 0x20, 0x89, 0x52, 0x51, 0x2e, 0xb1, },
  28665. { 0x9e, 0x25, 0xfc, 0x83, 0x3f, 0x22, 0x90, 0x73,
  28666. 0x3e, 0x93, 0x44, 0xa5, 0xe8, 0x38, 0x39, 0xeb, },
  28667. { 0x56, 0x8e, 0x49, 0x5a, 0xbe, 0x52, 0x5a, 0x21,
  28668. 0x8a, 0x22, 0x14, 0xcd, 0x3e, 0x07, 0x1d, 0x12, },
  28669. { 0x4a, 0x29, 0xb5, 0x45, 0x52, 0xd1, 0x6b, 0x9a,
  28670. 0x46, 0x9c, 0x10, 0x52, 0x8e, 0xff, 0x0a, 0xae, },
  28671. { 0xc9, 0xd1, 0x84, 0xdd, 0xd5, 0xa9, 0xf5, 0xe0,
  28672. 0xcf, 0x8c, 0xe2, 0x9a, 0x9a, 0xbf, 0x69, 0x1c, },
  28673. { 0x2d, 0xb4, 0x79, 0xae, 0x78, 0xbd, 0x50, 0xd8,
  28674. 0x88, 0x2a, 0x8a, 0x17, 0x8a, 0x61, 0x32, 0xad, },
  28675. { 0x8e, 0xce, 0x5f, 0x04, 0x2d, 0x5e, 0x44, 0x7b,
  28676. 0x50, 0x51, 0xb9, 0xea, 0xcb, 0x8d, 0x8f, 0x6f, },
  28677. { 0x9c, 0x0b, 0x53, 0xb4, 0xb3, 0xc3, 0x07, 0xe8,
  28678. 0x7e, 0xae, 0xe0, 0x86, 0x78, 0x14, 0x1f, 0x66, },
  28679. { 0xab, 0xf2, 0x48, 0xaf, 0x69, 0xa6, 0xea, 0xe4,
  28680. 0xbf, 0xd3, 0xeb, 0x2f, 0x12, 0x9e, 0xeb, 0x94, },
  28681. { 0x06, 0x64, 0xda, 0x16, 0x68, 0x57, 0x4b, 0x88,
  28682. 0xb9, 0x35, 0xf3, 0x02, 0x73, 0x58, 0xae, 0xf4, },
  28683. { 0xaa, 0x4b, 0x9d, 0xc4, 0xbf, 0x33, 0x7d, 0xe9,
  28684. 0x0c, 0xd4, 0xfd, 0x3c, 0x46, 0x7c, 0x6a, 0xb7, },
  28685. { 0xea, 0x5c, 0x7f, 0x47, 0x1f, 0xaf, 0x6b, 0xde,
  28686. 0x2b, 0x1a, 0xd7, 0xd4, 0x68, 0x6d, 0x22, 0x87, },
  28687. { 0x29, 0x39, 0xb0, 0x18, 0x32, 0x23, 0xfa, 0xfc,
  28688. 0x17, 0x23, 0xde, 0x4f, 0x52, 0xc4, 0x3d, 0x35, },
  28689. { 0x7c, 0x39, 0x56, 0xca, 0x5e, 0xea, 0xfc, 0x3e,
  28690. 0x36, 0x3e, 0x9d, 0x55, 0x65, 0x46, 0xeb, 0x68, },
  28691. { 0x77, 0xc6, 0x07, 0x71, 0x46, 0xf0, 0x1c, 0x32,
  28692. 0xb6, 0xb6, 0x9d, 0x5f, 0x4e, 0xa9, 0xff, 0xcf, },
  28693. { 0x37, 0xa6, 0x98, 0x6c, 0xb8, 0x84, 0x7e, 0xdf,
  28694. 0x09, 0x25, 0xf0, 0xf1, 0x30, 0x9b, 0x54, 0xde, },
  28695. { 0xa7, 0x05, 0xf0, 0xe6, 0x9d, 0xa9, 0xa8, 0xf9,
  28696. 0x07, 0x24, 0x1a, 0x2e, 0x92, 0x3c, 0x8c, 0xc8, },
  28697. { 0x3d, 0xc4, 0x7d, 0x1f, 0x29, 0xc4, 0x48, 0x46,
  28698. 0x1e, 0x9e, 0x76, 0xed, 0x90, 0x4f, 0x67, 0x11, },
  28699. { 0x0d, 0x62, 0xbf, 0x01, 0xe6, 0xfc, 0x0e, 0x1a,
  28700. 0x0d, 0x3c, 0x47, 0x51, 0xc5, 0xd3, 0x69, 0x2b, },
  28701. { 0x8c, 0x03, 0x46, 0x8b, 0xca, 0x7c, 0x66, 0x9e,
  28702. 0xe4, 0xfd, 0x5e, 0x08, 0x4b, 0xbe, 0xe7, 0xb5, },
  28703. { 0x52, 0x8a, 0x5b, 0xb9, 0x3b, 0xaf, 0x2c, 0x9c,
  28704. 0x44, 0x73, 0xcc, 0xe5, 0xd0, 0xd2, 0x2b, 0xd9, },
  28705. { 0xdf, 0x6a, 0x30, 0x1e, 0x95, 0xc9, 0x5d, 0xad,
  28706. 0x97, 0xae, 0x0c, 0xc8, 0xc6, 0x91, 0x3b, 0xd8, },
  28707. { 0x80, 0x11, 0x89, 0x90, 0x2c, 0x85, 0x7f, 0x39,
  28708. 0xe7, 0x35, 0x91, 0x28, 0x5e, 0x70, 0xb6, 0xdb, },
  28709. { 0xe6, 0x17, 0x34, 0x6a, 0xc9, 0xc2, 0x31, 0xbb,
  28710. 0x36, 0x50, 0xae, 0x34, 0xcc, 0xca, 0x0c, 0x5b, },
  28711. { 0x27, 0xd9, 0x34, 0x37, 0xef, 0xb7, 0x21, 0xaa,
  28712. 0x40, 0x18, 0x21, 0xdc, 0xec, 0x5a, 0xdf, 0x89, },
  28713. { 0x89, 0x23, 0x7d, 0x9d, 0xed, 0x9c, 0x5e, 0x78,
  28714. 0xd8, 0xb1, 0xc9, 0xb1, 0x66, 0xcc, 0x73, 0x42, },
  28715. { 0x4a, 0x6d, 0x80, 0x91, 0xbf, 0x5e, 0x7d, 0x65,
  28716. 0x11, 0x89, 0xfa, 0x94, 0xa2, 0x50, 0xb1, 0x4c, },
  28717. { 0x0e, 0x33, 0xf9, 0x60, 0x55, 0xe7, 0xae, 0x89,
  28718. 0x3f, 0xfc, 0x0e, 0x3d, 0xcf, 0x49, 0x29, 0x02, },
  28719. { 0xe6, 0x1c, 0x43, 0x2b, 0x72, 0x0b, 0x19, 0xd1,
  28720. 0x8e, 0xc8, 0xd8, 0x4b, 0xdc, 0x63, 0x15, 0x1b, },
  28721. { 0xf7, 0xe5, 0xae, 0xf5, 0x49, 0xf7, 0x82, 0xcf,
  28722. 0x37, 0x90, 0x55, 0xa6, 0x08, 0x26, 0x9b, 0x16, },
  28723. { 0x43, 0x8d, 0x03, 0x0f, 0xd0, 0xb7, 0xa5, 0x4f,
  28724. 0xa8, 0x37, 0xf2, 0xad, 0x20, 0x1a, 0x64, 0x03, },
  28725. { 0xa5, 0x90, 0xd3, 0xee, 0x4f, 0xbf, 0x04, 0xe3,
  28726. 0x24, 0x7e, 0x0d, 0x27, 0xf2, 0x86, 0x42, 0x3f, },
  28727. { 0x5f, 0xe2, 0xc1, 0xa1, 0x72, 0xfe, 0x93, 0xc4,
  28728. 0xb1, 0x5c, 0xd3, 0x7c, 0xae, 0xf9, 0xf5, 0x38, },
  28729. { 0x2c, 0x97, 0x32, 0x5c, 0xbd, 0x06, 0xb3, 0x6e,
  28730. 0xb2, 0x13, 0x3d, 0xd0, 0x8b, 0x3a, 0x01, 0x7c, },
  28731. { 0x92, 0xc8, 0x14, 0x22, 0x7a, 0x6b, 0xca, 0x94,
  28732. 0x9f, 0xf0, 0x65, 0x9f, 0x00, 0x2a, 0xd3, 0x9e, },
  28733. { 0xdc, 0xe8, 0x50, 0x11, 0x0b, 0xd8, 0x32, 0x8c,
  28734. 0xfb, 0xd5, 0x08, 0x41, 0xd6, 0x91, 0x1d, 0x87, },
  28735. { 0x67, 0xf1, 0x49, 0x84, 0xc7, 0xda, 0x79, 0x12,
  28736. 0x48, 0xe3, 0x2b, 0xb5, 0x92, 0x25, 0x83, 0xda, },
  28737. { 0x19, 0x38, 0xf2, 0xcf, 0x72, 0xd5, 0x4e, 0xe9,
  28738. 0x7e, 0x94, 0x16, 0x6f, 0xa9, 0x1d, 0x2a, 0x36, },
  28739. { 0x74, 0x48, 0x1e, 0x96, 0x46, 0xed, 0x49, 0xfe,
  28740. 0x0f, 0x62, 0x24, 0x30, 0x16, 0x04, 0x69, 0x8e, },
  28741. { 0x57, 0xfc, 0xa5, 0xde, 0x98, 0xa9, 0xd6, 0xd8,
  28742. 0x00, 0x64, 0x38, 0xd0, 0x58, 0x3d, 0x8a, 0x1d, },
  28743. { 0x9f, 0xec, 0xde, 0x1c, 0xef, 0xdc, 0x1c, 0xbe,
  28744. 0xd4, 0x76, 0x36, 0x74, 0xd9, 0x57, 0x53, 0x59, },
  28745. { 0xe3, 0x04, 0x0c, 0x00, 0xeb, 0x28, 0xf1, 0x53,
  28746. 0x66, 0xca, 0x73, 0xcb, 0xd8, 0x72, 0xe7, 0x40, },
  28747. { 0x76, 0x97, 0x00, 0x9a, 0x6a, 0x83, 0x1d, 0xfe,
  28748. 0xcc, 0xa9, 0x1c, 0x59, 0x93, 0x67, 0x0f, 0x7a, },
  28749. { 0x58, 0x53, 0x54, 0x23, 0x21, 0xf5, 0x67, 0xa0,
  28750. 0x05, 0xd5, 0x47, 0xa4, 0xf0, 0x47, 0x59, 0xbd, },
  28751. { 0x51, 0x50, 0xd1, 0x77, 0x2f, 0x50, 0x83, 0x4a,
  28752. 0x50, 0x3e, 0x06, 0x9a, 0x97, 0x3f, 0xbd, 0x7c, },
  28753. };
  28754. #endif
  28755. WOLFSSL_TEST_SUBROUTINE int siphash_test(void)
  28756. {
  28757. int ret = 0;
  28758. int i;
  28759. #if WOLFSSL_SIPHASH_CROUNDS == 2 && WOLFSSL_SIPHASH_DROUNDS == 4
  28760. unsigned char res[SIPHASH_MAC_SIZE_16];
  28761. SipHash siphash;
  28762. for (i = 0; i < 64; i++) {
  28763. ret = wc_InitSipHash(&siphash, siphash_key, SIPHASH_MAC_SIZE_8);
  28764. if (ret != 0)
  28765. return -12100 - i;
  28766. ret = wc_SipHashUpdate(&siphash, siphash_msg, i);
  28767. if (ret != 0)
  28768. return -12200 - i;
  28769. ret = wc_SipHashFinal(&siphash, res, SIPHASH_MAC_SIZE_8);
  28770. if (ret != 0)
  28771. return -12300 - i;
  28772. if (XMEMCMP(res, siphash_r8[i], SIPHASH_MAC_SIZE_8) != 0)
  28773. return -12400 - i;
  28774. ret = wc_SipHash(siphash_key, siphash_msg, i, res, SIPHASH_MAC_SIZE_8);
  28775. if (ret != 0)
  28776. return -12500 - i;
  28777. if (XMEMCMP(res, siphash_r8[i], SIPHASH_MAC_SIZE_8) != 0)
  28778. return -12600 - i;
  28779. }
  28780. for (i = 0; i < 64; i++) {
  28781. ret = wc_InitSipHash(&siphash, siphash_key, SIPHASH_MAC_SIZE_16);
  28782. if (ret != 0)
  28783. return -12700 - i;
  28784. ret = wc_SipHashUpdate(&siphash, siphash_msg, i);
  28785. if (ret != 0)
  28786. return -12800 - i;
  28787. ret = wc_SipHashFinal(&siphash, res, SIPHASH_MAC_SIZE_16);
  28788. if (ret != 0)
  28789. return -12900 - i;
  28790. if (XMEMCMP(res, siphash_r16[i], SIPHASH_MAC_SIZE_16) != 0)
  28791. return -13000 - i;
  28792. ret = wc_SipHash(siphash_key, siphash_msg, i, res, SIPHASH_MAC_SIZE_16);
  28793. if (ret != 0)
  28794. return -13100 - i;
  28795. if (XMEMCMP(res, siphash_r16[i], SIPHASH_MAC_SIZE_16) != 0)
  28796. return -13200 - i;
  28797. }
  28798. #endif
  28799. /* Testing bad parameters. */
  28800. ret = wc_InitSipHash(NULL, NULL, SIPHASH_MAC_SIZE_8);
  28801. if (ret != BAD_FUNC_ARG)
  28802. return -13300;
  28803. ret = wc_InitSipHash(NULL, siphash_key, SIPHASH_MAC_SIZE_8);
  28804. if (ret != BAD_FUNC_ARG)
  28805. return -13301;
  28806. ret = wc_InitSipHash(&siphash, NULL, SIPHASH_MAC_SIZE_8);
  28807. if (ret != BAD_FUNC_ARG)
  28808. return -13302;
  28809. ret = wc_InitSipHash(&siphash, siphash_key, 7);
  28810. if (ret != BAD_FUNC_ARG)
  28811. return -13303;
  28812. ret = wc_InitSipHash(&siphash, siphash_key, SIPHASH_MAC_SIZE_8);
  28813. if (ret != 0)
  28814. return -13304;
  28815. ret = wc_SipHashUpdate(NULL, NULL, 0);
  28816. if (ret != BAD_FUNC_ARG)
  28817. return -13305;
  28818. ret = wc_SipHashUpdate(&siphash, NULL, 1);
  28819. if (ret != BAD_FUNC_ARG)
  28820. return -13306;
  28821. ret = wc_SipHashFinal(NULL, NULL, SIPHASH_MAC_SIZE_8);
  28822. if (ret != BAD_FUNC_ARG)
  28823. return -13307;
  28824. ret = wc_SipHashFinal(&siphash, NULL, SIPHASH_MAC_SIZE_8);
  28825. if (ret != BAD_FUNC_ARG)
  28826. return -13308;
  28827. ret = wc_SipHashFinal(NULL, res, SIPHASH_MAC_SIZE_8);
  28828. if (ret != BAD_FUNC_ARG)
  28829. return -13309;
  28830. ret = wc_SipHashFinal(&siphash, res, SIPHASH_MAC_SIZE_16);
  28831. if (ret != BAD_FUNC_ARG)
  28832. return -13310;
  28833. ret = wc_SipHash(NULL, NULL, 0, NULL, SIPHASH_MAC_SIZE_16);
  28834. if (ret != BAD_FUNC_ARG)
  28835. return -13311;
  28836. ret = wc_SipHash(siphash_key, NULL, 0, NULL, SIPHASH_MAC_SIZE_16);
  28837. if (ret != BAD_FUNC_ARG)
  28838. return -13312;
  28839. ret = wc_SipHash(NULL, NULL, 0, res, SIPHASH_MAC_SIZE_16);
  28840. if (ret != BAD_FUNC_ARG)
  28841. return -13313;
  28842. ret = wc_SipHash(siphash_key, NULL, 0, res, 15);
  28843. if (ret != BAD_FUNC_ARG)
  28844. return -13314;
  28845. ret = wc_SipHash(siphash_key, NULL, 1, res, SIPHASH_MAC_SIZE_16);
  28846. if (ret != BAD_FUNC_ARG)
  28847. return -13315;
  28848. return 0;
  28849. }
  28850. #endif /* WOLFSSL_SIPHASH */
  28851. #ifdef HAVE_LIBZ
  28852. static const byte sample_text[] =
  28853. "Biodiesel cupidatat marfa, cliche aute put a bird on it incididunt elit\n"
  28854. "polaroid. Sunt tattooed bespoke reprehenderit. Sint twee organic id\n"
  28855. "marfa. Commodo veniam ad esse gastropub. 3 wolf moon sartorial vero,\n"
  28856. "plaid delectus biodiesel squid +1 vice. Post-ironic keffiyeh leggings\n"
  28857. "selfies cray fap hoodie, forage anim. Carles cupidatat shoreditch, VHS\n"
  28858. "small batch meggings kogi dolore food truck bespoke gastropub.\n"
  28859. "\n"
  28860. "Terry richardson adipisicing actually typewriter tumblr, twee whatever\n"
  28861. "four loko you probably haven't heard of them high life. Messenger bag\n"
  28862. "whatever tattooed deep v mlkshk. Brooklyn pinterest assumenda chillwave\n"
  28863. "et, banksy ullamco messenger bag umami pariatur direct trade forage.\n"
  28864. "Typewriter culpa try-hard, pariatur sint brooklyn meggings. Gentrify\n"
  28865. "food truck next level, tousled irony non semiotics PBR ethical anim cred\n"
  28866. "readymade. Mumblecore brunch lomo odd future, portland organic terry\n"
  28867. "richardson elit leggings adipisicing ennui raw denim banjo hella. Godard\n"
  28868. "mixtape polaroid, pork belly readymade organic cray typewriter helvetica\n"
  28869. "four loko whatever street art yr farm-to-table.\n"
  28870. "\n"
  28871. "Vinyl keytar vice tofu. Locavore you probably haven't heard of them pug\n"
  28872. "pickled, hella tonx labore truffaut DIY mlkshk elit cosby sweater sint\n"
  28873. "et mumblecore. Elit swag semiotics, reprehenderit DIY sartorial nisi ugh\n"
  28874. "nesciunt pug pork belly wayfarers selfies delectus. Ethical hoodie\n"
  28875. "seitan fingerstache kale chips. Terry richardson artisan williamsburg,\n"
  28876. "eiusmod fanny pack irony tonx ennui lo-fi incididunt tofu YOLO\n"
  28877. "readymade. 8-bit sed ethnic beard officia. Pour-over iphone DIY butcher,\n"
  28878. "ethnic art party qui letterpress nisi proident jean shorts mlkshk\n"
  28879. "locavore.\n"
  28880. "\n"
  28881. "Narwhal flexitarian letterpress, do gluten-free voluptate next level\n"
  28882. "banh mi tonx incididunt carles DIY. Odd future nulla 8-bit beard ut\n"
  28883. "cillum pickled velit, YOLO officia you probably haven't heard of them\n"
  28884. "trust fund gastropub. Nisi adipisicing tattooed, Austin mlkshk 90's\n"
  28885. "small batch american apparel. Put a bird on it cosby sweater before they\n"
  28886. "sold out pork belly kogi hella. Street art mollit sustainable polaroid,\n"
  28887. "DIY ethnic ea pug beard dreamcatcher cosby sweater magna scenester nisi.\n"
  28888. "Sed pork belly skateboard mollit, labore proident eiusmod. Sriracha\n"
  28889. "excepteur cosby sweater, anim deserunt laborum eu aliquip ethical et\n"
  28890. "neutra PBR selvage.\n"
  28891. "\n"
  28892. "Raw denim pork belly truffaut, irony plaid sustainable put a bird on it\n"
  28893. "next level jean shorts exercitation. Hashtag keytar whatever, nihil\n"
  28894. "authentic aliquip disrupt laborum. Tattooed selfies deserunt trust fund\n"
  28895. "wayfarers. 3 wolf moon synth church-key sartorial, gastropub leggings\n"
  28896. "tattooed. Labore high life commodo, meggings raw denim fingerstache pug\n"
  28897. "trust fund leggings seitan forage. Nostrud ullamco duis, reprehenderit\n"
  28898. "incididunt flannel sustainable helvetica pork belly pug banksy you\n"
  28899. "probably haven't heard of them nesciunt farm-to-table. Disrupt nostrud\n"
  28900. "mollit magna, sriracha sartorial helvetica.\n"
  28901. "\n"
  28902. "Nulla kogi reprehenderit, skateboard sustainable duis adipisicing viral\n"
  28903. "ad fanny pack salvia. Fanny pack trust fund you probably haven't heard\n"
  28904. "of them YOLO vice nihil. Keffiyeh cray lo-fi pinterest cardigan aliqua,\n"
  28905. "reprehenderit aute. Culpa tousled williamsburg, marfa lomo actually anim\n"
  28906. "skateboard. Iphone aliqua ugh, semiotics pariatur vero readymade\n"
  28907. "organic. Marfa squid nulla, in laborum disrupt laboris irure gastropub.\n"
  28908. "Veniam sunt food truck leggings, sint vinyl fap.\n"
  28909. "\n"
  28910. "Hella dolore pork belly, truffaut carles you probably haven't heard of\n"
  28911. "them PBR helvetica in sapiente. Fashion axe ugh bushwick american\n"
  28912. "apparel. Fingerstache sed iphone, jean shorts blue bottle nisi bushwick\n"
  28913. "flexitarian officia veniam plaid bespoke fap YOLO lo-fi. Blog\n"
  28914. "letterpress mumblecore, food truck id cray brooklyn cillum ad sed.\n"
  28915. "Assumenda chambray wayfarers vinyl mixtape sustainable. VHS vinyl\n"
  28916. "delectus, culpa williamsburg polaroid cliche swag church-key synth kogi\n"
  28917. "magna pop-up literally. Swag thundercats ennui shoreditch vegan\n"
  28918. "pitchfork neutra truffaut etsy, sed single-origin coffee craft beer.\n"
  28919. "\n"
  28920. "Odio letterpress brooklyn elit. Nulla single-origin coffee in occaecat\n"
  28921. "meggings. Irony meggings 8-bit, chillwave lo-fi adipisicing cred\n"
  28922. "dreamcatcher veniam. Put a bird on it irony umami, trust fund bushwick\n"
  28923. "locavore kale chips. Sriracha swag thundercats, chillwave disrupt\n"
  28924. "tousled beard mollit mustache leggings portland next level. Nihil esse\n"
  28925. "est, skateboard art party etsy thundercats sed dreamcatcher ut iphone\n"
  28926. "swag consectetur et. Irure skateboard banjo, nulla deserunt messenger\n"
  28927. "bag dolor terry richardson sapiente.\n";
  28928. static const byte sample_text_gz[] = {
  28929. 0x1F, 0x8B, 0x08, 0x08, 0xC5, 0x49, 0xB5, 0x5B, 0x00, 0x03, 0x63, 0x69, 0x70,
  28930. 0x68, 0x65, 0x72, 0x74, 0x65, 0x78, 0x74, 0x2E, 0x74, 0x78, 0x74, 0x00, 0x8D,
  28931. 0x58, 0xCB, 0x92, 0xE4, 0xB6, 0x11, 0xBC, 0xE3, 0x2B, 0xEA, 0xA6, 0x83, 0xD9,
  28932. 0x1D, 0x72, 0xF8, 0x22, 0x1F, 0xB5, 0x96, 0xA5, 0xDD, 0x90, 0xBC, 0xAB, 0xD0,
  28933. 0x28, 0x36, 0x42, 0x47, 0x90, 0x2C, 0x36, 0xA1, 0x06, 0x09, 0x0A, 0x8F, 0xEE,
  28934. 0xE1, 0xDF, 0x3B, 0x0B, 0xE0, 0x73, 0x2C, 0x4B, 0xBA, 0xCD, 0xCE, 0x80, 0x78,
  28935. 0x64, 0x65, 0x65, 0x66, 0xED, 0x3B, 0xE3, 0x5A, 0xC3, 0x81, 0x2D, 0x35, 0x69,
  28936. 0x32, 0xAD, 0x8E, 0x3A, 0xD2, 0xA0, 0x7D, 0xA7, 0x2B, 0x6A, 0xAC, 0x69, 0x7A,
  28937. 0x26, 0x9D, 0x22, 0xD3, 0x94, 0x22, 0x69, 0xAA, 0x8D, 0x6F, 0xC9, 0x8D, 0x64,
  28938. 0x22, 0x99, 0xB1, 0x31, 0xAD, 0x69, 0xD3, 0x18, 0x89, 0xAD, 0x89, 0x6A, 0x72,
  28939. 0x56, 0x7B, 0x67, 0xDA, 0x2B, 0xBD, 0xC8, 0xEF, 0xB0, 0x4D, 0x74, 0x8E, 0x5B,
  28940. 0xAA, 0x39, 0x4C, 0xEE, 0xCE, 0xE4, 0x79, 0xF2, 0xDC, 0xF3, 0xD8, 0xB2, 0x37,
  28941. 0x11, 0x8B, 0x8C, 0x2C, 0x7A, 0x32, 0x93, 0xF3, 0x37, 0x3D, 0x9A, 0x86, 0x4C,
  28942. 0xAB, 0xF2, 0xB9, 0x57, 0xFA, 0x97, 0x1B, 0x06, 0xD7, 0x3A, 0x7A, 0xF0, 0x68,
  28943. 0xF4, 0x40, 0xBA, 0x25, 0x0E, 0x81, 0xE9, 0xA6, 0x43, 0xF4, 0x6E, 0x4A, 0xF5,
  28944. 0x95, 0xFE, 0x41, 0x4F, 0x67, 0x3B, 0x1A, 0x1C, 0xEE, 0x12, 0xB4, 0x8F, 0xCE,
  28945. 0x1B, 0x6D, 0xB1, 0xDE, 0xBB, 0x4A, 0x4D, 0x56, 0x9B, 0x96, 0x5A, 0xB6, 0xDC,
  28946. 0xC4, 0x14, 0x70, 0xE5, 0xF5, 0x7D, 0xE1, 0xB7, 0x84, 0x3F, 0xFC, 0xED, 0xEF,
  28947. 0xF4, 0x30, 0x0D, 0x5F, 0xE9, 0x47, 0x17, 0xE2, 0xC5, 0x78, 0x27, 0x67, 0xDF,
  28948. 0xB9, 0xEB, 0xCC, 0xCC, 0x3D, 0x59, 0xBE, 0xDD, 0xCC, 0x78, 0x0B, 0x0A, 0x1F,
  28949. 0x74, 0xF8, 0x8C, 0x1A, 0xAF, 0x67, 0xEA, 0xF4, 0x44, 0xBD, 0x93, 0x7D, 0x2A,
  28950. 0xEA, 0x9C, 0xD7, 0x37, 0x80, 0x32, 0x9A, 0x01, 0x37, 0xD5, 0xDE, 0xCA, 0xA2,
  28951. 0x0D, 0xB9, 0xD0, 0x3B, 0xCF, 0xAD, 0x89, 0x4D, 0x5F, 0xD1, 0xE7, 0xF7, 0x2F,
  28952. 0x2A, 0x0C, 0xDA, 0x5A, 0xAA, 0x35, 0x7E, 0x41, 0xC3, 0xB2, 0x37, 0xDD, 0xDD,
  28953. 0xCD, 0x50, 0xEB, 0x2C, 0x96, 0x62, 0x3B, 0xD7, 0x52, 0xF4, 0xA9, 0xB9, 0x6F,
  28954. 0x48, 0xED, 0xEF, 0x54, 0xEA, 0x67, 0xF6, 0x7E, 0x26, 0x8F, 0x3A, 0x68, 0xDF,
  28955. 0x06, 0xBC, 0x56, 0xB7, 0x66, 0x32, 0xC1, 0x34, 0xD8, 0x88, 0x34, 0x1E, 0x88,
  28956. 0xED, 0x67, 0x8A, 0xF3, 0xC4, 0x4F, 0xC0, 0xCA, 0x9E, 0x62, 0x1A, 0x6A, 0xEB,
  28957. 0xAB, 0x02, 0xED, 0xB3, 0xD7, 0x91, 0x81, 0x8A, 0xEA, 0x5C, 0xF2, 0x64, 0xDD,
  28958. 0xDD, 0xD1, 0xEC, 0x12, 0x4D, 0xDE, 0xD5, 0xBA, 0xC6, 0x77, 0xBD, 0x06, 0xC4,
  28959. 0x5F, 0x44, 0xEA, 0x59, 0x4B, 0x5D, 0x3B, 0x8A, 0x3D, 0x0F, 0xD4, 0x9B, 0x1B,
  28960. 0x80, 0x30, 0x1D, 0x30, 0xFA, 0x8F, 0x00, 0x3F, 0xDE, 0xB0, 0x6F, 0xAD, 0x6F,
  28961. 0x6A, 0xDD, 0x6E, 0x2F, 0x6E, 0xCB, 0x3C, 0xD1, 0x83, 0x06, 0x7B, 0x0F, 0xFD,
  28962. 0xFD, 0x4A, 0xEF, 0xBC, 0x73, 0x77, 0x3B, 0x8F, 0x34, 0xA1, 0xBA, 0xEC, 0x39,
  28963. 0x80, 0x33, 0x21, 0xA4, 0x01, 0x55, 0xD7, 0xD4, 0xF4, 0xC6, 0xDA, 0x27, 0x4E,
  28964. 0x54, 0x1C, 0x2B, 0xEC, 0x37, 0xDE, 0xC3, 0x4C, 0xC9, 0x5A, 0x3D, 0x34, 0x0E,
  28965. 0xD8, 0x1C, 0x0E, 0xA2, 0x34, 0xE8, 0xC1, 0xD0, 0xA4, 0x51, 0xD5, 0x88, 0x8B,
  28966. 0xB7, 0xC6, 0xA3, 0x96, 0x40, 0x49, 0xB7, 0xBC, 0xE0, 0x7F, 0x55, 0x3F, 0xEF,
  28967. 0x6F, 0x6E, 0x92, 0x9D, 0x34, 0xFE, 0x3C, 0x5F, 0x04, 0xA5, 0x6A, 0xFF, 0x30,
  28968. 0x08, 0xC9, 0xEA, 0xF5, 0x52, 0x2B, 0xFE, 0x57, 0xFA, 0x8E, 0xC7, 0xE8, 0x4D,
  28969. 0x37, 0xAB, 0x03, 0xFA, 0x23, 0xBF, 0x46, 0x94, 0xFF, 0xC1, 0x16, 0xE0, 0xB9,
  28970. 0x14, 0x2C, 0x9E, 0x27, 0xEC, 0x98, 0x69, 0x14, 0x92, 0xF1, 0x60, 0x5C, 0x34,
  28971. 0x4D, 0xA0, 0x1F, 0xDF, 0xFD, 0x44, 0x1C, 0x7B, 0xD3, 0x80, 0x70, 0x42, 0x02,
  28972. 0x30, 0x84, 0x5B, 0xE5, 0x59, 0xB7, 0xF3, 0x80, 0xFB, 0x01, 0x33, 0xA9, 0x00,
  28973. 0x37, 0x52, 0xDC, 0xDA, 0xA7, 0x11, 0x85, 0xB7, 0x6E, 0x70, 0xE4, 0xDA, 0x96,
  28974. 0xBA, 0x84, 0x5B, 0x81, 0x43, 0x93, 0xF3, 0xD1, 0xEA, 0xB1, 0xDD, 0xB8, 0x1F,
  28975. 0xA5, 0xCC, 0xEA, 0x50, 0x66, 0x69, 0xA9, 0x8D, 0x8C, 0xA7, 0xA2, 0xF3, 0x38,
  28976. 0x26, 0x43, 0x5E, 0x3F, 0x01, 0xBE, 0x1C, 0x0F, 0x20, 0x7F, 0x75, 0xA8, 0x20,
  28977. 0x80, 0xC4, 0xC3, 0x5C, 0x8B, 0x0D, 0xD4, 0x60, 0x5E, 0xA3, 0x9E, 0xD0, 0xB4,
  28978. 0x4B, 0x4F, 0xE6, 0x13, 0x85, 0x60, 0x42, 0x96, 0xED, 0xAA, 0xDB, 0xE9, 0x99,
  28979. 0xE3, 0x07, 0x0E, 0x61, 0xB3, 0x07, 0xE3, 0xB1, 0xFA, 0xC0, 0x9B, 0xAD, 0xF6,
  28980. 0xE0, 0x26, 0x33, 0xEA, 0xEA, 0x23, 0xCD, 0x1E, 0x9D, 0xE1, 0x87, 0x4B, 0x74,
  28981. 0x97, 0x08, 0x3E, 0xA1, 0x28, 0xEA, 0xB3, 0x19, 0x67, 0x8B, 0x76, 0x9A, 0xA3,
  28982. 0xF6, 0xB9, 0xCF, 0x80, 0x65, 0x97, 0xAE, 0xF4, 0x83, 0x6B, 0xF4, 0x43, 0x20,
  28983. 0xF9, 0x0B, 0xFC, 0x9B, 0xD2, 0x4D, 0x4D, 0xA6, 0xB9, 0xA3, 0x02, 0x55, 0x79,
  28984. 0x18, 0x36, 0x19, 0x5F, 0xC9, 0xEA, 0x5A, 0x76, 0x40, 0xB9, 0xBA, 0x0E, 0x9A,
  28985. 0x44, 0xDF, 0x7C, 0xF8, 0x65, 0x61, 0x5E, 0x81, 0xAB, 0x71, 0xA1, 0x9E, 0x29,
  28986. 0x3C, 0x59, 0xCB, 0x23, 0xA4, 0xF6, 0x60, 0x1A, 0x0D, 0x5B, 0x39, 0xAE, 0xF4,
  28987. 0x6F, 0x59, 0x16, 0x9E, 0x60, 0xD8, 0x56, 0xCF, 0xEA, 0x2C, 0x4C, 0x79, 0xD3,
  28988. 0x5D, 0x51, 0x46, 0xA0, 0x4E, 0xE9, 0xD6, 0xAB, 0x91, 0x43, 0x63, 0x44, 0xD7,
  28989. 0x70, 0xB9, 0x23, 0x98, 0x4F, 0x3D, 0x03, 0x02, 0xF6, 0x81, 0x56, 0xC1, 0x58,
  28990. 0x85, 0x07, 0xA7, 0x2D, 0x2C, 0x29, 0xCA, 0x01, 0x45, 0x31, 0x51, 0x8F, 0xD4,
  28991. 0x19, 0xA1, 0x79, 0x88, 0x5A, 0xA4, 0xF5, 0xAE, 0x2D, 0x4B, 0x63, 0x4C, 0x58,
  28992. 0xFE, 0xBF, 0xAD, 0xEE, 0xA3, 0x09, 0xF8, 0xE2, 0x89, 0xBE, 0x81, 0x0E, 0x86,
  28993. 0x3A, 0xF9, 0x5B, 0xA5, 0xD8, 0xA4, 0x00, 0x75, 0x04, 0xF2, 0x23, 0xB8, 0x39,
  28994. 0x69, 0x50, 0xB7, 0xD0, 0x34, 0x63, 0x54, 0xD8, 0x61, 0xDD, 0xA5, 0x33, 0x47,
  28995. 0x85, 0x96, 0x22, 0xD0, 0x2F, 0x9F, 0x7E, 0xF8, 0x74, 0x24, 0xEA, 0x57, 0x97,
  28996. 0x5A, 0xE0, 0x00, 0xCF, 0xC1, 0x67, 0xE1, 0x41, 0xBD, 0x94, 0xA1, 0x03, 0xD3,
  28997. 0xB4, 0x08, 0x64, 0xF2, 0x17, 0x27, 0x35, 0x37, 0x53, 0xEF, 0x46, 0xCE, 0xD8,
  28998. 0xD4, 0x09, 0x52, 0xC6, 0x1E, 0xF7, 0x28, 0xDF, 0x08, 0x0F, 0xD0, 0x6F, 0x71,
  28999. 0xA6, 0xDF, 0xE4, 0x60, 0x8E, 0xC0, 0x1E, 0x78, 0x86, 0x50, 0xB0, 0x9B, 0x84,
  29000. 0x7E, 0xE8, 0x36, 0xFA, 0x95, 0xF1, 0x12, 0x51, 0xC7, 0x18, 0x96, 0xA2, 0x29,
  29001. 0xBB, 0x70, 0x02, 0xB4, 0xF9, 0xA8, 0x3D, 0x08, 0x66, 0xA9, 0xB3, 0xFC, 0x0A,
  29002. 0x94, 0x80, 0xFD, 0x78, 0xDC, 0xAB, 0x82, 0x5A, 0xD2, 0xCD, 0xC2, 0x87, 0xC6,
  29003. 0x4B, 0x07, 0xFA, 0xD1, 0xC3, 0xD9, 0x34, 0x41, 0x85, 0xF8, 0xD0, 0xB6, 0x0A,
  29004. 0x9D, 0x00, 0x91, 0x35, 0x05, 0x88, 0xC3, 0xE3, 0x9B, 0x22, 0xD2, 0xB8, 0xFD,
  29005. 0x95, 0x3E, 0x6D, 0x5D, 0x48, 0xA3, 0x68, 0xCF, 0x02, 0x42, 0x79, 0x79, 0x8A,
  29006. 0xAA, 0x01, 0xD6, 0x09, 0x14, 0x2C, 0xF4, 0x83, 0xA3, 0x80, 0x31, 0x55, 0x46,
  29007. 0x6E, 0xC5, 0xE5, 0x2F, 0x30, 0x58, 0x81, 0xA2, 0x90, 0xBE, 0x2E, 0xA1, 0xC3,
  29008. 0x0F, 0xA6, 0xF5, 0x51, 0x00, 0x39, 0xB6, 0xF2, 0x2A, 0xA3, 0x15, 0x7D, 0x8D,
  29009. 0xF5, 0x66, 0x5C, 0xD9, 0xFC, 0xCF, 0x2F, 0xBF, 0x08, 0x27, 0xE7, 0xD0, 0x03,
  29010. 0xB8, 0xD9, 0x00, 0x13, 0x3D, 0x01, 0x6B, 0xB6, 0xA8, 0xCD, 0x5B, 0x3B, 0x3E,
  29011. 0x93, 0xBF, 0xE6, 0x2E, 0xB7, 0x4A, 0xCF, 0xB3, 0x0A, 0xCE, 0x62, 0x11, 0xD6,
  29012. 0x1F, 0x68, 0x9B, 0x1D, 0x68, 0xD1, 0x8C, 0x97, 0xBD, 0xA1, 0x07, 0x67, 0x73,
  29013. 0x87, 0xE0, 0x36, 0xDA, 0x8C, 0xD2, 0xD2, 0xBB, 0x84, 0x28, 0xA9, 0xFE, 0x52,
  29014. 0x74, 0xD6, 0xB9, 0x0F, 0x0A, 0x6A, 0x2D, 0x28, 0x35, 0x34, 0x3A, 0xD3, 0xE2,
  29015. 0xCD, 0x35, 0x06, 0x7D, 0x1B, 0x35, 0x85, 0x86, 0xD1, 0x3E, 0xF2, 0x6F, 0xA1,
  29016. 0xC4, 0x55, 0xBD, 0x00, 0xD8, 0xC3, 0x5D, 0xC2, 0x1D, 0x6B, 0x6B, 0x27, 0x5B,
  29017. 0x95, 0xF3, 0xAB, 0xB5, 0xD3, 0x37, 0xF2, 0x2C, 0x9C, 0xC7, 0x5D, 0xBD, 0xF1,
  29018. 0x68, 0x1C, 0xAD, 0xF8, 0xB5, 0xE1, 0x29, 0x72, 0x7A, 0x73, 0x62, 0x55, 0x24,
  29019. 0xB9, 0x85, 0xDF, 0x7B, 0x29, 0x7D, 0xDE, 0x08, 0xF5, 0xE4, 0x44, 0xDA, 0x1A,
  29020. 0x30, 0x74, 0xDA, 0xB4, 0x9B, 0x23, 0x9A, 0x3A, 0xC1, 0x53, 0xB2, 0xA2, 0xA3,
  29021. 0x7B, 0x1F, 0xD9, 0x56, 0xD4, 0x4F, 0x9B, 0xB2, 0x1E, 0xEE, 0xB8, 0x6A, 0x4E,
  29022. 0xB5, 0xF4, 0x5A, 0xC9, 0x18, 0x27, 0x9C, 0xDE, 0x14, 0x44, 0xED, 0xC4, 0x3C,
  29023. 0x71, 0x9F, 0x5F, 0xD9, 0x37, 0xA0, 0x78, 0x34, 0x6E, 0xBC, 0xD2, 0x7B, 0x1D,
  29024. 0xFA, 0x08, 0x39, 0x5A, 0x04, 0x73, 0x15, 0xD9, 0x0A, 0x48, 0xC1, 0x2D, 0x15,
  29025. 0x4E, 0x84, 0x30, 0x45, 0x69, 0xB3, 0xE5, 0xF6, 0xAD, 0x09, 0x1E, 0xCC, 0x5F,
  29026. 0x1F, 0x06, 0xD5, 0x58, 0xAD, 0x78, 0xD7, 0x9F, 0xE5, 0xED, 0x3B, 0x09, 0xD5,
  29027. 0xA6, 0x52, 0x6F, 0x92, 0xD3, 0x3C, 0xC6, 0x1E, 0xF2, 0x93, 0x7C, 0xD3, 0x5F,
  29028. 0x70, 0x85, 0x5D, 0xF8, 0xAA, 0x9D, 0xB7, 0x7B, 0x24, 0x5A, 0xE9, 0x0A, 0x35,
  29029. 0x2F, 0xF5, 0xD9, 0x82, 0x02, 0x8A, 0x90, 0x13, 0x5B, 0xB5, 0x67, 0x9C, 0xDD,
  29030. 0xA0, 0x4E, 0x82, 0x27, 0xDA, 0x7E, 0xE8, 0x8E, 0xCD, 0xE1, 0x56, 0x71, 0x2C,
  29031. 0xE6, 0x4E, 0x1F, 0x91, 0xCD, 0x7C, 0x6A, 0xB7, 0x78, 0xD0, 0x26, 0xF3, 0x56,
  29032. 0xA9, 0xD5, 0xA1, 0xC3, 0x3B, 0x98, 0xE9, 0x28, 0x09, 0xEF, 0x50, 0x90, 0xCD,
  29033. 0xC4, 0x8E, 0x75, 0xCC, 0xAC, 0x2D, 0xC9, 0x03, 0x6D, 0xAC, 0xFE, 0xC4, 0x88,
  29034. 0x36, 0xD1, 0x3F, 0xBB, 0x1C, 0x7D, 0xB3, 0x14, 0x61, 0x2C, 0xB7, 0x54, 0x4B,
  29035. 0xDB, 0x64, 0xB6, 0x57, 0x14, 0x16, 0x8E, 0x1E, 0x6C, 0x64, 0xBB, 0x8B, 0x48,
  29036. 0x5D, 0x96, 0x9D, 0xDC, 0x80, 0xA7, 0xF7, 0x54, 0xC7, 0x46, 0x38, 0x3E, 0x44,
  29037. 0xDE, 0x7E, 0x92, 0x8D, 0x07, 0xF6, 0x07, 0x37, 0x4E, 0x16, 0x10, 0xB4, 0x7D,
  29038. 0x88, 0x66, 0x7F, 0xBB, 0xFF, 0xEA, 0x00, 0xF3, 0xFF, 0x97, 0x2C, 0xB5, 0xBE,
  29039. 0x35, 0x4B, 0x5C, 0x36, 0xEC, 0x4C, 0xBD, 0x2B, 0x7D, 0xBF, 0x46, 0xE2, 0x9C,
  29040. 0x0E, 0x8A, 0xA3, 0xEC, 0xB1, 0x0E, 0x9A, 0xDA, 0x9A, 0x9B, 0x28, 0x92, 0x10,
  29041. 0x53, 0x57, 0xEA, 0xEC, 0xA2, 0x32, 0x32, 0x20, 0x1D, 0x97, 0x5C, 0xB6, 0x84,
  29042. 0xA9, 0x93, 0x8D, 0x95, 0x11, 0xA3, 0x24, 0xA3, 0x2D, 0xC6, 0x4A, 0xEF, 0xAA,
  29043. 0x1D, 0x85, 0x2B, 0x7D, 0x28, 0xBE, 0x53, 0xCE, 0x10, 0x1F, 0xAE, 0x0E, 0x41,
  29044. 0x6C, 0x4B, 0x79, 0x12, 0xFB, 0xF7, 0x54, 0xA3, 0x96, 0x54, 0x83, 0x20, 0x96,
  29045. 0x8F, 0x28, 0xA9, 0x3F, 0x8B, 0x3D, 0xBA, 0x77, 0xDC, 0x24, 0xE1, 0xD4, 0x49,
  29046. 0x40, 0xD8, 0x78, 0x31, 0x85, 0x43, 0xF6, 0xFE, 0x5C, 0xA6, 0x8F, 0x90, 0x09,
  29047. 0xB0, 0xE7, 0xC4, 0x95, 0xB2, 0x55, 0x49, 0x97, 0x8F, 0x1C, 0x78, 0x30, 0x20,
  29048. 0xA0, 0xB4, 0xEF, 0x73, 0x56, 0x59, 0x82, 0xFD, 0xCE, 0xBA, 0x6A, 0x8F, 0x2C,
  29049. 0x8B, 0x15, 0xFD, 0xA1, 0x85, 0xA8, 0x5C, 0x0F, 0x11, 0xA5, 0x9D, 0xC2, 0x46,
  29050. 0xC6, 0x9C, 0xC9, 0x40, 0x0B, 0x58, 0x6A, 0x1C, 0x7A, 0x23, 0xF9, 0xE0, 0x95,
  29051. 0x05, 0x13, 0x58, 0x72, 0xE8, 0x9F, 0x30, 0xAC, 0xCD, 0x26, 0xD4, 0x66, 0x13,
  29052. 0xDF, 0x1E, 0x7B, 0x4F, 0x9C, 0xBE, 0x38, 0x79, 0x75, 0x92, 0xA4, 0xDA, 0x26,
  29053. 0x44, 0x55, 0x17, 0xA3, 0xE5, 0x62, 0xDA, 0xEB, 0x86, 0xEA, 0x68, 0xC7, 0xAB,
  29054. 0xFD, 0x2D, 0x43, 0x59, 0x51, 0xC0, 0x75, 0x64, 0x91, 0x01, 0x29, 0x33, 0x28,
  29055. 0xF3, 0x04, 0x83, 0x80, 0x75, 0x37, 0x75, 0x0C, 0x03, 0x7B, 0x0A, 0xAB, 0x8E,
  29056. 0x60, 0x62, 0x8B, 0x4C, 0xAF, 0x2D, 0xA3, 0x2F, 0xFE, 0xAB, 0x45, 0xCF, 0xDA,
  29057. 0xAB, 0xFA, 0xFA, 0x30, 0x3D, 0xE8, 0xA1, 0x96, 0xA5, 0x7B, 0xE2, 0x2A, 0xD0,
  29058. 0xAF, 0x59, 0xF7, 0xD0, 0x32, 0x57, 0x19, 0xBD, 0xCA, 0x9F, 0xD5, 0x1A, 0xC7,
  29059. 0xAA, 0x65, 0x4A, 0x38, 0xB2, 0x70, 0x33, 0xB7, 0x75, 0xD2, 0xCD, 0xD1, 0xF0,
  29060. 0xA8, 0x87, 0x59, 0x20, 0xA5, 0x57, 0x55, 0xB1, 0xB2, 0xC9, 0x4D, 0x97, 0x34,
  29061. 0x41, 0xF3, 0xF0, 0x30, 0xA1, 0x2C, 0x1C, 0x49, 0x3E, 0x89, 0x7D, 0x12, 0xE2,
  29062. 0xC3, 0x04, 0xC3, 0x92, 0xC0, 0xF6, 0x39, 0x10, 0x80, 0x81, 0x8F, 0x08, 0xB4,
  29063. 0xF8, 0xB9, 0x13, 0x4E, 0x2C, 0xAE, 0xB3, 0x71, 0x82, 0x63, 0x98, 0xAB, 0x5C,
  29064. 0x1C, 0x10, 0xEA, 0x66, 0xF9, 0x02, 0x3A, 0x82, 0x61, 0xD0, 0xD4, 0xAE, 0x43,
  29065. 0xD4, 0x01, 0x3E, 0x9D, 0x04, 0x14, 0xF6, 0x60, 0xD8, 0xA7, 0xD6, 0xB8, 0x53,
  29066. 0xC8, 0xDA, 0x80, 0x93, 0xA0, 0x02, 0xDD, 0xCC, 0xE2, 0xF2, 0xBB, 0xFB, 0xE0,
  29067. 0x27, 0xD7, 0x34, 0x9A, 0x71, 0x49, 0xB5, 0x4F, 0x42, 0x1F, 0xB2, 0x9D, 0x6D,
  29068. 0xAA, 0x9D, 0xD3, 0x50, 0xB5, 0x8F, 0x6A, 0x4B, 0xDF, 0x1F, 0xD5, 0x27, 0x8F,
  29069. 0x3B, 0x27, 0xCF, 0x2F, 0x8C, 0xF8, 0x9D, 0x4C, 0x52, 0xBC, 0x32, 0x0F, 0x73,
  29070. 0xD5, 0x51, 0x8E, 0x36, 0x7E, 0xAD, 0x09, 0xF0, 0x94, 0x83, 0x5F, 0x36, 0xFD,
  29071. 0x7C, 0x03, 0xED, 0xF1, 0x5E, 0x4B, 0xF7, 0xAA, 0x55, 0x5C, 0x4A, 0x14, 0x59,
  29072. 0x85, 0x38, 0x2D, 0x8C, 0xDF, 0xEC, 0x65, 0x1B, 0xB8, 0x76, 0x57, 0x96, 0x3C,
  29073. 0x86, 0xED, 0xF2, 0x7F, 0x2D, 0x28, 0x48, 0xDA, 0x49, 0x7F, 0xF7, 0x54, 0x2B,
  29074. 0xD5, 0x39, 0xD5, 0x57, 0x0A, 0x75, 0x7A, 0x3E, 0x5E, 0x5D, 0xBA, 0x4A, 0x15,
  29075. 0xFA, 0xB8, 0x31, 0x80, 0x71, 0x2C, 0xCA, 0xC4, 0x51, 0x10, 0x16, 0x5D, 0x39,
  29076. 0xEC, 0x9D, 0x07, 0xB6, 0x6A, 0x89, 0x9F, 0x9B, 0x5B, 0x6F, 0x03, 0xB0, 0x92,
  29077. 0x01, 0x38, 0x6B, 0x48, 0x99, 0x0A, 0x8F, 0x13, 0xC1, 0xA6, 0x01, 0xEA, 0xBF,
  29078. 0x6F, 0x86, 0x43, 0x51, 0xB6, 0x11, 0x00, 0x00
  29079. };
  29080. WOLFSSL_TEST_SUBROUTINE int compress_test(void)
  29081. {
  29082. int ret = 0;
  29083. word32 dSz = sizeof(sample_text);
  29084. word32 cSz = (dSz + (word32)(dSz * 0.001) + 12);
  29085. byte *c;
  29086. byte *d;
  29087. c = (byte *)XMALLOC(cSz * sizeof(byte), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29088. d = (byte *)XMALLOC(dSz * sizeof(byte), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29089. if (c == NULL || d == NULL) {
  29090. ERROR_OUT(-12100, exit);
  29091. }
  29092. /* follow calloc and initialize to 0 */
  29093. XMEMSET(c, 0, cSz);
  29094. XMEMSET(d, 0, dSz);
  29095. if ((ret = wc_Compress(c, cSz, sample_text, dSz, 0)) < 0) {
  29096. ERROR_OUT(-12101, exit);
  29097. }
  29098. cSz = (word32)ret;
  29099. if ((ret = wc_DeCompress(d, dSz, c, cSz)) != (int)dSz) {
  29100. ERROR_OUT(-12102, exit);
  29101. }
  29102. dSz = (word32)ret;
  29103. if (XMEMCMP(d, sample_text, dSz) != 0) {
  29104. ERROR_OUT(-12103, exit);
  29105. }
  29106. /* GZIP tests */
  29107. cSz = (dSz + (word32)(dSz * 0.001) + 12); /* reset cSz */
  29108. XMEMSET(c, 0, cSz);
  29109. XMEMSET(d, 0, dSz);
  29110. ret = wc_Compress_ex(c, cSz, sample_text, dSz, 0, LIBZ_WINBITS_GZIP);
  29111. if (ret < 0) {
  29112. ERROR_OUT(-12104, exit);
  29113. }
  29114. cSz = (word32)ret;
  29115. ret = wc_DeCompress_ex(d, dSz, c, cSz, LIBZ_WINBITS_GZIP);
  29116. if (ret < 0) {
  29117. ERROR_OUT(-12105, exit);
  29118. }
  29119. if (XMEMCMP(d, sample_text, dSz) != 0) {
  29120. ERROR_OUT(-12106, exit);
  29121. }
  29122. /* Try with gzip generated output */
  29123. XMEMSET(d, 0, dSz);
  29124. ret = wc_DeCompress_ex(d, dSz, sample_text_gz, sizeof(sample_text_gz),
  29125. LIBZ_WINBITS_GZIP);
  29126. if (ret < 0) {
  29127. ERROR_OUT(-12107, exit);
  29128. }
  29129. dSz = (word32)ret;
  29130. if (XMEMCMP(d, sample_text, dSz) != 0) {
  29131. ERROR_OUT(-12108, exit);
  29132. }
  29133. ret = 0; /* success */
  29134. exit:
  29135. if (c) XFREE(c, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29136. if (d) XFREE(d, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29137. return ret;
  29138. }
  29139. #endif /* HAVE_LIBZ */
  29140. #ifdef HAVE_PKCS7
  29141. /* External Debugging/Testing Note:
  29142. *
  29143. * PKCS#7 test functions can output generated PKCS#7/CMS bundles for
  29144. * additional testing. To dump bundles to files DER encoded files, please
  29145. * define:
  29146. *
  29147. * #define PKCS7_OUTPUT_TEST_BUNDLES
  29148. */
  29149. /* Loads certs and keys for use with PKCS7 tests, from either files
  29150. * or buffers.
  29151. *
  29152. * rsaClientCertBuf - output buffer for RSA client cert
  29153. * rsaClientCertBufSz - IN/OUT size of output buffer, size of RSA client cert
  29154. * rsaClientPrivKeyBuf - output buffer for RSA client private key
  29155. * rsaClientPrivKeyBufSz - IN/OUT size of output buffer, size of RSA client key
  29156. *
  29157. * rsaServerCertBuf - output buffer for RSA server cert
  29158. * rsaServerCertBufSz - IN/OUT size of output buffer, size of RSA server cert
  29159. * rsaServerPrivKeyBuf - output buffer for RSA server private key
  29160. * rsaServerPrivKeyBufSz - IN/OUT size of output buffer, size of RSA server key
  29161. *
  29162. * rsaCaCertBuf - output buffer for RSA CA cert
  29163. * rsaCaCertBufSz - IN/OUT size of output buffer, size of RSA ca cert
  29164. * rsaCaPrivKeyBuf - output buffer for RSA CA private key
  29165. * rsaCaPrivKeyBufSz - IN/OUT size of output buffer, size of RSA CA key
  29166. *
  29167. * eccClientCertBuf - output buffer for ECC cert
  29168. * eccClientCertBufSz - IN/OUT size of output buffer, size of ECC cert
  29169. * eccClientPrivKeyBuf - output buffer for ECC private key
  29170. * eccClientPrivKeyBufSz - IN/OUT size of output buffer, size of ECC private key
  29171. *
  29172. * Returns 0 on success, negative on error
  29173. */
  29174. static int pkcs7_load_certs_keys(
  29175. byte* rsaClientCertBuf, word32* rsaClientCertBufSz,
  29176. byte* rsaClientPrivKeyBuf, word32* rsaClientPrivKeyBufSz,
  29177. byte* rsaServerCertBuf, word32* rsaServerCertBufSz,
  29178. byte* rsaServerPrivKeyBuf, word32* rsaServerPrivKeyBufSz,
  29179. byte* rsaCaCertBuf, word32* rsaCaCertBufSz,
  29180. byte* rsaCaPrivKeyBuf, word32* rsaCaPrivKeyBufSz,
  29181. byte* eccClientCertBuf, word32* eccClientCertBufSz,
  29182. byte* eccClientPrivKeyBuf, word32* eccClientPrivKeyBufSz)
  29183. {
  29184. #ifndef NO_FILESYSTEM
  29185. XFILE certFile;
  29186. XFILE keyFile;
  29187. (void)certFile;
  29188. (void)keyFile;
  29189. #endif
  29190. #ifndef NO_RSA
  29191. if (rsaClientCertBuf == NULL || rsaClientCertBufSz == NULL ||
  29192. rsaClientPrivKeyBuf == NULL || rsaClientPrivKeyBufSz == NULL)
  29193. return BAD_FUNC_ARG;
  29194. #endif
  29195. #ifdef HAVE_ECC
  29196. if (eccClientCertBuf == NULL || eccClientCertBufSz == NULL ||
  29197. eccClientPrivKeyBuf == NULL || eccClientPrivKeyBufSz == NULL)
  29198. return BAD_FUNC_ARG;
  29199. #endif
  29200. /* RSA */
  29201. #ifndef NO_RSA
  29202. #ifdef USE_CERT_BUFFERS_1024
  29203. if (*rsaClientCertBufSz < (word32)sizeof_client_cert_der_1024)
  29204. return -12110;
  29205. XMEMCPY(rsaClientCertBuf, client_cert_der_1024,
  29206. sizeof_client_cert_der_1024);
  29207. *rsaClientCertBufSz = sizeof_client_cert_der_1024;
  29208. if (rsaServerCertBuf != NULL) {
  29209. if (*rsaServerCertBufSz < (word32)sizeof_server_cert_der_1024)
  29210. return -12111;
  29211. XMEMCPY(rsaServerCertBuf, server_cert_der_1024,
  29212. sizeof_server_cert_der_1024);
  29213. *rsaServerCertBufSz = sizeof_server_cert_der_1024;
  29214. }
  29215. if (rsaCaCertBuf != NULL) {
  29216. if (*rsaCaCertBufSz < (word32)sizeof_ca_cert_der_1024)
  29217. return -12112;
  29218. XMEMCPY(rsaCaCertBuf, ca_cert_der_1024, sizeof_ca_cert_der_1024);
  29219. *rsaCaCertBufSz = sizeof_ca_cert_der_1024;
  29220. }
  29221. #elif defined(USE_CERT_BUFFERS_2048)
  29222. if (*rsaClientCertBufSz < (word32)sizeof_client_cert_der_2048)
  29223. return -12113;
  29224. XMEMCPY(rsaClientCertBuf, client_cert_der_2048,
  29225. sizeof_client_cert_der_2048);
  29226. *rsaClientCertBufSz = sizeof_client_cert_der_2048;
  29227. if (rsaServerCertBuf != NULL) {
  29228. if (*rsaServerCertBufSz < (word32)sizeof_server_cert_der_2048)
  29229. return -12114;
  29230. XMEMCPY(rsaServerCertBuf, server_cert_der_2048,
  29231. sizeof_server_cert_der_2048);
  29232. *rsaServerCertBufSz = sizeof_server_cert_der_2048;
  29233. }
  29234. if (rsaCaCertBuf != NULL) {
  29235. if (*rsaCaCertBufSz < (word32)sizeof_ca_cert_der_2048)
  29236. return -12115;
  29237. XMEMCPY(rsaCaCertBuf, ca_cert_der_2048, sizeof_ca_cert_der_2048);
  29238. *rsaCaCertBufSz = sizeof_ca_cert_der_2048;
  29239. }
  29240. #else
  29241. certFile = XFOPEN(clientCert, "rb");
  29242. if (!certFile)
  29243. return -12116;
  29244. *rsaClientCertBufSz = (word32)XFREAD(rsaClientCertBuf, 1,
  29245. *rsaClientCertBufSz, certFile);
  29246. XFCLOSE(certFile);
  29247. if (rsaServerCertBuf != NULL) {
  29248. certFile = XFOPEN(rsaServerCertDerFile, "rb");
  29249. if (!certFile)
  29250. return -12117;
  29251. *rsaServerCertBufSz = (word32)XFREAD(rsaServerCertBuf, 1,
  29252. *rsaServerCertBufSz, certFile);
  29253. XFCLOSE(certFile);
  29254. }
  29255. if (rsaCaCertBuf != NULL) {
  29256. certFile = XFOPEN(rsaCaCertDerFile, "rb");
  29257. if (!certFile)
  29258. return -12118;
  29259. *rsaCaCertBufSz = (word32)XFREAD(rsaCaCertBuf, 1, *rsaCaCertBufSz,
  29260. certFile);
  29261. XFCLOSE(certFile);
  29262. }
  29263. #endif
  29264. #ifdef USE_CERT_BUFFERS_1024
  29265. if (*rsaClientPrivKeyBufSz < (word32)sizeof_client_key_der_1024)
  29266. return -12119;
  29267. XMEMCPY(rsaClientPrivKeyBuf, client_key_der_1024,
  29268. sizeof_client_key_der_1024);
  29269. *rsaClientPrivKeyBufSz = sizeof_client_key_der_1024;
  29270. if (rsaServerPrivKeyBuf != NULL) {
  29271. if (*rsaServerPrivKeyBufSz < (word32)sizeof_server_key_der_1024)
  29272. return -12120;
  29273. XMEMCPY(rsaServerPrivKeyBuf, server_key_der_1024,
  29274. sizeof_server_key_der_1024);
  29275. *rsaServerPrivKeyBufSz = sizeof_server_key_der_1024;
  29276. }
  29277. if (rsaCaPrivKeyBuf != NULL) {
  29278. if (*rsaCaPrivKeyBufSz < (word32)sizeof_ca_key_der_1024)
  29279. return -12121;
  29280. XMEMCPY(rsaCaPrivKeyBuf, ca_key_der_1024, sizeof_ca_key_der_1024);
  29281. *rsaCaPrivKeyBufSz = sizeof_ca_key_der_1024;
  29282. }
  29283. #elif defined(USE_CERT_BUFFERS_2048)
  29284. if (*rsaClientPrivKeyBufSz < (word32)sizeof_client_key_der_2048)
  29285. return -12122;
  29286. XMEMCPY(rsaClientPrivKeyBuf, client_key_der_2048,
  29287. sizeof_client_key_der_2048);
  29288. *rsaClientPrivKeyBufSz = sizeof_client_key_der_2048;
  29289. if (rsaServerPrivKeyBuf != NULL) {
  29290. if (*rsaServerPrivKeyBufSz < (word32)sizeof_server_key_der_2048)
  29291. return -12123;
  29292. XMEMCPY(rsaServerPrivKeyBuf, server_key_der_2048,
  29293. sizeof_server_key_der_2048);
  29294. *rsaServerPrivKeyBufSz = sizeof_server_key_der_2048;
  29295. }
  29296. if (rsaCaPrivKeyBuf != NULL) {
  29297. if (*rsaCaPrivKeyBufSz < (word32)sizeof_ca_key_der_2048)
  29298. return -12124;
  29299. XMEMCPY(rsaCaPrivKeyBuf, ca_key_der_2048, sizeof_ca_key_der_2048);
  29300. *rsaCaPrivKeyBufSz = sizeof_ca_key_der_2048;
  29301. }
  29302. #else
  29303. keyFile = XFOPEN(clientKey, "rb");
  29304. if (!keyFile)
  29305. return -12125;
  29306. *rsaClientPrivKeyBufSz = (word32)XFREAD(rsaClientPrivKeyBuf, 1,
  29307. *rsaClientPrivKeyBufSz, keyFile);
  29308. XFCLOSE(keyFile);
  29309. if (rsaServerPrivKeyBuf != NULL) {
  29310. keyFile = XFOPEN(rsaServerKeyDerFile, "rb");
  29311. if (!keyFile)
  29312. return -12126;
  29313. *rsaServerPrivKeyBufSz = (word32)XFREAD(rsaServerPrivKeyBuf, 1,
  29314. *rsaServerPrivKeyBufSz, keyFile);
  29315. XFCLOSE(keyFile);
  29316. }
  29317. if (rsaCaPrivKeyBuf != NULL) {
  29318. keyFile = XFOPEN(rsaCaKeyFile, "rb");
  29319. if (!keyFile)
  29320. return -12127;
  29321. *rsaCaPrivKeyBufSz = (word32)XFREAD(rsaCaPrivKeyBuf, 1,
  29322. *rsaCaPrivKeyBufSz, keyFile);
  29323. XFCLOSE(keyFile);
  29324. }
  29325. #endif /* USE_CERT_BUFFERS */
  29326. #endif /* NO_RSA */
  29327. /* ECC */
  29328. #ifdef HAVE_ECC
  29329. #ifdef USE_CERT_BUFFERS_256
  29330. if (*eccClientCertBufSz < (word32)sizeof_cliecc_cert_der_256)
  29331. return -12128;
  29332. XMEMCPY(eccClientCertBuf, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  29333. *eccClientCertBufSz = sizeof_cliecc_cert_der_256;
  29334. #else
  29335. certFile = XFOPEN(eccClientCert, "rb");
  29336. if (!certFile)
  29337. return -12129;
  29338. *eccClientCertBufSz = (word32)XFREAD(eccClientCertBuf, 1,
  29339. *eccClientCertBufSz, certFile);
  29340. XFCLOSE(certFile);
  29341. #endif /* USE_CERT_BUFFERS_256 */
  29342. #ifdef USE_CERT_BUFFERS_256
  29343. if (*eccClientPrivKeyBufSz < (word32)sizeof_ecc_clikey_der_256)
  29344. return -12130;
  29345. XMEMCPY(eccClientPrivKeyBuf, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  29346. *eccClientPrivKeyBufSz = sizeof_ecc_clikey_der_256;
  29347. #else
  29348. keyFile = XFOPEN(eccClientKey, "rb");
  29349. if (!keyFile)
  29350. return -12131;
  29351. *eccClientPrivKeyBufSz = (word32)XFREAD(eccClientPrivKeyBuf, 1,
  29352. *eccClientPrivKeyBufSz, keyFile);
  29353. XFCLOSE(keyFile);
  29354. #endif /* USE_CERT_BUFFERS_256 */
  29355. #endif /* HAVE_ECC */
  29356. #ifdef NO_RSA
  29357. (void)rsaClientCertBuf;
  29358. (void)rsaClientCertBufSz;
  29359. (void)rsaClientPrivKeyBuf;
  29360. (void)rsaClientPrivKeyBufSz;
  29361. (void)rsaServerCertBuf;
  29362. (void)rsaServerCertBufSz;
  29363. (void)rsaServerPrivKeyBuf;
  29364. (void)rsaServerPrivKeyBufSz;
  29365. (void)rsaCaCertBuf;
  29366. (void)rsaCaCertBufSz;
  29367. (void)rsaCaPrivKeyBuf;
  29368. (void)rsaCaPrivKeyBufSz;
  29369. #endif
  29370. #ifndef HAVE_ECC
  29371. (void)eccClientCertBuf;
  29372. (void)eccClientCertBufSz;
  29373. (void)eccClientPrivKeyBuf;
  29374. (void)eccClientPrivKeyBufSz;
  29375. #endif
  29376. #ifndef NO_FILESYSTEM
  29377. (void)certFile;
  29378. (void)keyFile;
  29379. #endif
  29380. return 0;
  29381. }
  29382. typedef struct {
  29383. const byte* content;
  29384. word32 contentSz;
  29385. int contentOID;
  29386. int encryptOID;
  29387. int keyWrapOID;
  29388. int keyAgreeOID;
  29389. byte* cert;
  29390. size_t certSz;
  29391. byte* privateKey;
  29392. word32 privateKeySz;
  29393. byte* optionalUkm;
  29394. word32 optionalUkmSz;
  29395. int ktriOptions; /* KTRI options flags */
  29396. int kariOptions; /* KARI options flags */
  29397. /* KEKRI specific */
  29398. const byte* secretKey; /* key, only for kekri RecipientInfo types */
  29399. word32 secretKeySz; /* size of secretKey, bytes */
  29400. const byte* secretKeyId; /* key identifier */
  29401. word32 secretKeyIdSz; /* size of key identifier, bytes */
  29402. void* timePtr; /* time_t pointer */
  29403. byte* otherAttrOID; /* OPTIONAL, other attribute OID */
  29404. word32 otherAttrOIDSz; /* size of otherAttrOID, bytes */
  29405. byte* otherAttr; /* OPTIONAL, other attribute, ASN.1 encoded */
  29406. word32 otherAttrSz; /* size of otherAttr, bytes */
  29407. int kekriOptions; /* KEKRI options flags */
  29408. /* PWRI specific */
  29409. const char* password;
  29410. word32 passwordSz;
  29411. const byte* salt;
  29412. word32 saltSz;
  29413. int kdfOID;
  29414. int hashOID;
  29415. int kdfIterations;
  29416. int pwriOptions; /* PWRI options flags */
  29417. /* ORI specific */
  29418. int isOri;
  29419. int oriOptions; /* ORI options flags */
  29420. const char* outFileName;
  29421. } pkcs7EnvelopedVector;
  29422. static const byte asnDataOid[] = {
  29423. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x07, 0x01
  29424. };
  29425. /* ORI encrypt callback, responsible for encrypting content-encryption key (CEK)
  29426. * and giving wolfCrypt the value for oriOID and oriValue to place in
  29427. * OtherRecipientInfo.
  29428. *
  29429. * Returns 0 on success, negative upon error. */
  29430. static int myOriEncryptCb(PKCS7* pkcs7, byte* cek, word32 cekSz, byte* oriType,
  29431. word32* oriTypeSz, byte* oriValue, word32* oriValueSz,
  29432. void* ctx)
  29433. {
  29434. int i;
  29435. /* make sure buffers are large enough */
  29436. if ((*oriValueSz < (2 + cekSz)) || (*oriTypeSz < sizeof(oriType)))
  29437. return -12140;
  29438. /* our simple encryption algorithm will be take the bitwise complement */
  29439. oriValue[0] = 0x04; /*ASN OCTET STRING */
  29440. oriValue[1] = (byte)cekSz; /* length */
  29441. for (i = 0; i < (int)cekSz; i++) {
  29442. oriValue[2 + i] = ~cek[i];
  29443. }
  29444. *oriValueSz = 2 + cekSz;
  29445. /* set oriType to ASN.1 encoded data OID */
  29446. XMEMCPY(oriType, asnDataOid, sizeof(asnDataOid));
  29447. *oriTypeSz = sizeof(asnDataOid);
  29448. (void)pkcs7;
  29449. (void)ctx;
  29450. return 0;
  29451. }
  29452. /* ORI decrypt callback, responsible for providing a decrypted content
  29453. * encryption key (CEK) placed into decryptedKey and size placed into
  29454. * decryptedKeySz. oriOID and oriValue are given to the callback to help
  29455. * in decrypting the encrypted CEK.
  29456. *
  29457. * Returns 0 on success, negative upon error. */
  29458. static int myOriDecryptCb(PKCS7* pkcs7, byte* oriType, word32 oriTypeSz,
  29459. byte* oriValue, word32 oriValueSz, byte* decryptedKey,
  29460. word32* decryptedKeySz, void* ctx)
  29461. {
  29462. int i;
  29463. /* make sure oriType matches what we expect */
  29464. if (oriTypeSz != sizeof(asnDataOid))
  29465. return -12150;
  29466. if (XMEMCMP(oriType, asnDataOid, sizeof(asnDataOid)) != 0)
  29467. return -12151;
  29468. /* make sure decrypted buffer is large enough */
  29469. if (*decryptedKeySz < oriValueSz)
  29470. return -12152;
  29471. /* decrypt encrypted CEK using simple bitwise complement,
  29472. only for example */
  29473. for (i = 0; i < (int)oriValueSz - 2; i++) {
  29474. decryptedKey[i] = ~oriValue[2 + i];
  29475. }
  29476. *decryptedKeySz = oriValueSz - 2;
  29477. (void)pkcs7;
  29478. (void)ctx;
  29479. return 0;
  29480. }
  29481. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  29482. /* returns 0 on success */
  29483. static int myDecryptionFunc(PKCS7* pkcs7, int encryptOID, byte* iv, int ivSz,
  29484. byte* aad, word32 aadSz, byte* authTag, word32 authTagSz,
  29485. byte* in, int inSz, byte* out, void* usrCtx)
  29486. {
  29487. int keyId = -1, ret, keySz;
  29488. word32 keyIdSz = 8;
  29489. const byte* key;
  29490. byte keyIdRaw[8];
  29491. #ifdef WOLFSSL_SMALL_STACK
  29492. Aes *aes;
  29493. #else
  29494. Aes aes[1];
  29495. #endif
  29496. /* looking for KEY ID
  29497. * fwDecryptKeyID OID "1.2.840.113549.1.9.16.2.37
  29498. */
  29499. WOLFSSL_SMALL_STACK_STATIC const unsigned char OID[] = {
  29500. /* 0x06, 0x0B do not pass in tag and length */
  29501. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  29502. 0x01, 0x09, 0x10, 0x02, 0x25
  29503. };
  29504. WOLFSSL_SMALL_STACK_STATIC const byte defKey[] = {
  29505. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  29506. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  29507. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  29508. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  29509. };
  29510. WOLFSSL_SMALL_STACK_STATIC const byte altKey[] = {
  29511. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  29512. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  29513. };
  29514. /* test user context passed in */
  29515. if (usrCtx == NULL || *(int*)usrCtx != 1) {
  29516. return -12160;
  29517. }
  29518. #ifdef WOLFSSL_SMALL_STACK
  29519. if ((aes = (Aes *)XMALLOC(sizeof *aes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER)) == NULL)
  29520. return -12164;
  29521. #endif
  29522. /* if needing to find keyIdSz can call with NULL */
  29523. ret = wc_PKCS7_GetAttributeValue(pkcs7, OID, sizeof(OID), NULL,
  29524. &keyIdSz);
  29525. if (ret != LENGTH_ONLY_E) {
  29526. printf("Unexpected error %d when getting keyIdSz\n", ret);
  29527. printf("Possibly no KEY ID attribute set\n");
  29528. ERROR_OUT(-12161, out);
  29529. }
  29530. else {
  29531. XMEMSET(keyIdRaw, 0, sizeof(keyIdRaw));
  29532. ret = wc_PKCS7_GetAttributeValue(pkcs7, OID, sizeof(OID), keyIdRaw,
  29533. &keyIdSz);
  29534. if (ret < 0) {
  29535. goto out;
  29536. }
  29537. if (keyIdSz < 3) {
  29538. printf("keyIdSz is smaller than expected\n");
  29539. ERROR_OUT(-12162, out);
  29540. }
  29541. if (keyIdSz > 2 + sizeof(int)) {
  29542. printf("example case was only expecting a keyId of int size\n");
  29543. ERROR_OUT(-12163, out);
  29544. }
  29545. /* keyIdRaw[0] OCTET TAG */
  29546. /* keyIdRaw[1] Length */
  29547. #ifdef BIG_ENDIAN_ORDER
  29548. if (keyIdRaw[1] == 0x01) {
  29549. keyId = 1;
  29550. }
  29551. #else
  29552. XMEMCPY(&keyId, keyIdRaw + 2, sizeof(keyId));
  29553. #endif
  29554. }
  29555. /* Use keyID here if found to select key and decrypt in HSM or in this
  29556. * example just select key and do software decryption */
  29557. if (keyId == 1) {
  29558. key = altKey;
  29559. keySz = sizeof(altKey);
  29560. }
  29561. else {
  29562. key = defKey;
  29563. keySz = sizeof(defKey);
  29564. }
  29565. switch (encryptOID) {
  29566. #ifdef WOLFSSL_AES_256
  29567. case AES256CBCb:
  29568. if ((keySz != 32 ) || (ivSz != AES_BLOCK_SIZE))
  29569. ERROR_OUT(BAD_FUNC_ARG, out);
  29570. break;
  29571. #endif
  29572. #ifdef WOLFSSL_AES_128
  29573. case AES128CBCb:
  29574. if ((keySz != 16 ) || (ivSz != AES_BLOCK_SIZE))
  29575. ERROR_OUT(BAD_FUNC_ARG, out);
  29576. break;
  29577. #endif
  29578. default:
  29579. printf("Unsupported content cipher type for example");
  29580. ERROR_OUT(ALGO_ID_E, out);
  29581. };
  29582. ret = wc_AesInit(aes, HEAP_HINT, INVALID_DEVID);
  29583. if (ret == 0) {
  29584. ret = wc_AesSetKey(aes, key, keySz, iv, AES_DECRYPTION);
  29585. if (ret == 0)
  29586. ret = wc_AesCbcDecrypt(aes, out, in, inSz);
  29587. wc_AesFree(aes);
  29588. }
  29589. out:
  29590. #ifdef WOLFSSL_SMALL_STACK
  29591. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29592. #endif
  29593. (void)aad;
  29594. (void)aadSz;
  29595. (void)authTag;
  29596. (void)authTagSz;
  29597. return ret;
  29598. }
  29599. #endif /* !NO_AES && HAVE_AES_CBC */
  29600. #define PKCS7_BUF_SIZE 2048
  29601. static int pkcs7enveloped_run_vectors(byte* rsaCert, word32 rsaCertSz,
  29602. byte* rsaPrivKey, word32 rsaPrivKeySz,
  29603. byte* eccCert, word32 eccCertSz,
  29604. byte* eccPrivKey, word32 eccPrivKeySz)
  29605. {
  29606. int ret = 0, testSz = 0, i;
  29607. int envelopedSz, decodedSz;
  29608. byte *enveloped = NULL;
  29609. byte *decoded = NULL;
  29610. PKCS7* pkcs7 = NULL;
  29611. #ifdef ECC_TIMING_RESISTANT
  29612. WC_RNG rng;
  29613. #endif
  29614. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  29615. XFILE pkcs7File;
  29616. #endif
  29617. WOLFSSL_SMALL_STACK_STATIC const byte data[] = { /* Hello World */
  29618. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  29619. 0x72,0x6c,0x64
  29620. };
  29621. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_256) && \
  29622. defined(HAVE_ECC) && defined(WOLFSSL_SHA512)
  29623. byte optionalUkm[] = {
  29624. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07
  29625. };
  29626. #endif /* NO_AES */
  29627. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128) && \
  29628. !defined(NO_SHA)
  29629. /* encryption key for kekri recipient types */
  29630. WOLFSSL_SMALL_STACK_STATIC const byte secretKey[] = {
  29631. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  29632. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07
  29633. };
  29634. /* encryption key identifier */
  29635. WOLFSSL_SMALL_STACK_STATIC const byte secretKeyId[] = {
  29636. 0x02,0x02,0x03,0x04
  29637. };
  29638. #endif
  29639. #if !defined(NO_PWDBASED) && !defined(NO_SHA) && \
  29640. !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  29641. #ifndef HAVE_FIPS
  29642. WOLFSSL_SMALL_STACK_STATIC const char password[] = "password"; /* NOTE: Password is too short for FIPS */
  29643. #else
  29644. WOLFSSL_SMALL_STACK_STATIC const char password[] = "passwordFIPS_MODE";
  29645. #endif
  29646. WOLFSSL_SMALL_STACK_STATIC const byte salt[] = {
  29647. 0x12, 0x34, 0x56, 0x78, 0x78, 0x56, 0x34, 0x12
  29648. };
  29649. #endif
  29650. #define MAX_TESTVECTORS_LEN 13
  29651. #define ADD_PKCS7ENVELOPEDVECTOR(...) { \
  29652. pkcs7EnvelopedVector _this_vector = { __VA_ARGS__ }; \
  29653. if (testSz == MAX_TESTVECTORS_LEN) { \
  29654. ret = -12534; \
  29655. goto out; \
  29656. } \
  29657. XMEMCPY(&testVectors[testSz++], &_this_vector, sizeof _this_vector);\
  29658. }
  29659. pkcs7EnvelopedVector *testVectors = NULL;
  29660. #ifdef ECC_TIMING_RESISTANT
  29661. XMEMSET(&rng, 0, sizeof(rng));
  29662. #endif
  29663. testVectors = (pkcs7EnvelopedVector *)XMALLOC(MAX_TESTVECTORS_LEN * sizeof(*testVectors),
  29664. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29665. if (testVectors == NULL) {
  29666. ret = -12534;
  29667. goto out;
  29668. }
  29669. {
  29670. /* key transport key encryption technique */
  29671. #ifndef NO_RSA
  29672. #ifndef NO_DES3
  29673. ADD_PKCS7ENVELOPEDVECTOR(
  29674. data, (word32)sizeof(data), DATA, DES3b, 0, 0, rsaCert, rsaCertSz,
  29675. rsaPrivKey, rsaPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL,
  29676. 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  29677. "pkcs7envelopedDataDES3.der");
  29678. #endif
  29679. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  29680. #ifdef WOLFSSL_AES_128
  29681. ADD_PKCS7ENVELOPEDVECTOR(
  29682. data, (word32)sizeof(data), DATA, AES128CBCb, 0, 0, rsaCert, rsaCertSz,
  29683. rsaPrivKey, rsaPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL,
  29684. 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  29685. "pkcs7envelopedDataAES128CBC.der");
  29686. #endif
  29687. #ifdef WOLFSSL_AES_192
  29688. ADD_PKCS7ENVELOPEDVECTOR(
  29689. data, (word32)sizeof(data), DATA, AES192CBCb, 0, 0, rsaCert, rsaCertSz,
  29690. rsaPrivKey, rsaPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL,
  29691. 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  29692. "pkcs7envelopedDataAES192CBC.der");
  29693. #endif
  29694. #ifdef WOLFSSL_AES_256
  29695. ADD_PKCS7ENVELOPEDVECTOR(
  29696. data, (word32)sizeof(data), DATA, AES256CBCb, 0, 0, rsaCert, rsaCertSz,
  29697. rsaPrivKey, rsaPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL,
  29698. 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  29699. "pkcs7envelopedDataAES256CBC.der");
  29700. /* explicitly using SKID for SubjectKeyIdentifier */
  29701. ADD_PKCS7ENVELOPEDVECTOR(
  29702. data, (word32)sizeof(data), DATA, AES256CBCb, 0, 0, rsaCert, rsaCertSz,
  29703. rsaPrivKey, rsaPrivKeySz, NULL, 0, CMS_SKID, 0, NULL, 0, NULL, 0, NULL,
  29704. NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  29705. "pkcs7envelopedDataAES256CBC_SKID.der");
  29706. /* explicitly using IssuerAndSerialNumber for SubjectKeyIdentifier */
  29707. ADD_PKCS7ENVELOPEDVECTOR(
  29708. data, (word32)sizeof(data), DATA, AES256CBCb, 0, 0, rsaCert, rsaCertSz,
  29709. rsaPrivKey, rsaPrivKeySz, NULL, 0, CMS_ISSUER_AND_SERIAL_NUMBER, 0,
  29710. NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0,
  29711. 0, 0, 0, 0, "pkcs7envelopedDataAES256CBC_IANDS.der");
  29712. #endif
  29713. #endif /* !NO_AES && HAVE_AES_CBC */
  29714. #endif
  29715. /* key agreement key encryption technique*/
  29716. #ifdef HAVE_ECC
  29717. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  29718. #if !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  29719. ADD_PKCS7ENVELOPEDVECTOR(
  29720. data, (word32)sizeof(data), DATA, AES128CBCb, AES128_WRAP,
  29721. dhSinglePass_stdDH_sha1kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  29722. eccPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0,
  29723. 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  29724. "pkcs7envelopedDataAES128CBC_ECDH_SHA1KDF.der");
  29725. #endif
  29726. #if !defined(NO_SHA256) && defined(WOLFSSL_AES_256)
  29727. ADD_PKCS7ENVELOPEDVECTOR(
  29728. data, (word32)sizeof(data), DATA, AES256CBCb, AES256_WRAP,
  29729. dhSinglePass_stdDH_sha256kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  29730. eccPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0,
  29731. 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  29732. "pkcs7envelopedDataAES256CBC_ECDH_SHA256KDF.der");
  29733. #endif /* NO_SHA256 && WOLFSSL_AES_256 */
  29734. #if defined(WOLFSSL_SHA512) && defined(WOLFSSL_AES_256)
  29735. ADD_PKCS7ENVELOPEDVECTOR(
  29736. data, (word32)sizeof(data), DATA, AES256CBCb, AES256_WRAP,
  29737. dhSinglePass_stdDH_sha512kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  29738. eccPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0,
  29739. 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  29740. "pkcs7envelopedDataAES256CBC_ECDH_SHA512KDF.der");
  29741. /* with optional user keying material (ukm) */
  29742. ADD_PKCS7ENVELOPEDVECTOR(
  29743. data, (word32)sizeof(data), DATA, AES256CBCb, AES256_WRAP,
  29744. dhSinglePass_stdDH_sha512kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  29745. eccPrivKeySz, optionalUkm, sizeof(optionalUkm), 0, 0, NULL, 0,
  29746. NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  29747. "pkcs7envelopedDataAES256CBC_ECDH_SHA512KDF_ukm.der");
  29748. #endif /* WOLFSSL_SHA512 && WOLFSSL_AES_256 */
  29749. #endif /* !NO_AES && HAVE_AES_CBC */
  29750. #endif
  29751. /* kekri (KEKRecipientInfo) recipient types */
  29752. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  29753. #if !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  29754. ADD_PKCS7ENVELOPEDVECTOR(
  29755. data, (word32)sizeof(data), DATA, AES128CBCb, AES128_WRAP, 0,
  29756. NULL, 0, NULL, 0, NULL, 0, 0, 0, secretKey, sizeof(secretKey),
  29757. secretKeyId, sizeof(secretKeyId), NULL, NULL, 0, NULL, 0,
  29758. 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  29759. "pkcs7envelopedDataAES128CBC_KEKRI.der");
  29760. #endif
  29761. #endif /* !NO_AES && HAVE_AES_CBC */
  29762. /* pwri (PasswordRecipientInfo) recipient types */
  29763. #if !defined(NO_PWDBASED) && !defined(NO_AES) && defined(HAVE_AES_CBC)
  29764. #if !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  29765. ADD_PKCS7ENVELOPEDVECTOR(
  29766. data, (word32)sizeof(data), DATA, AES128CBCb, 0, 0,
  29767. NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0,
  29768. NULL, 0, NULL, NULL, 0, NULL, 0, 0, password,
  29769. (word32)XSTRLEN(password), salt, sizeof(salt), PBKDF2_OID, WC_SHA, 5,
  29770. 0, 0, 0, "pkcs7envelopedDataAES128CBC_PWRI.der");
  29771. #endif
  29772. #endif
  29773. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && !defined(NO_AES_128)
  29774. /* ori (OtherRecipientInfo) recipient types */
  29775. ADD_PKCS7ENVELOPEDVECTOR(
  29776. data, (word32)sizeof(data), DATA, AES128CBCb, 0, 0, NULL, 0, NULL, 0,
  29777. NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0,
  29778. NULL, 0, 0, 0, 0, 0, 1, 0, "pkcs7envelopedDataAES128CBC_ORI.der");
  29779. #endif
  29780. };
  29781. #undef MAX_TESTVECTORS_LEN
  29782. #undef ADD_PKCS7ENVELOPEDVECTOR
  29783. enveloped = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29784. decoded = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29785. if ((! enveloped) || (! decoded)) {
  29786. ERROR_OUT(-12170, out);
  29787. }
  29788. #ifdef ECC_TIMING_RESISTANT
  29789. #ifndef HAVE_FIPS
  29790. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  29791. #else
  29792. ret = wc_InitRng(&rng);
  29793. #endif
  29794. if (ret != 0) {
  29795. ERROR_OUT(-12171, out);
  29796. }
  29797. #endif
  29798. for (i = 0; i < testSz; i++) {
  29799. pkcs7 = wc_PKCS7_New(HEAP_HINT,
  29800. #ifdef WOLFSSL_ASYNC_CRYPT
  29801. INVALID_DEVID /* async PKCS7 is not supported */
  29802. #else
  29803. devId
  29804. #endif
  29805. );
  29806. if (pkcs7 == NULL) {
  29807. ERROR_OUT(-12172, out);
  29808. }
  29809. if (testVectors[i].secretKey != NULL) {
  29810. /* KEKRI recipient type */
  29811. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  29812. if (ret != 0) {
  29813. ERROR_OUT(-12173, out);
  29814. }
  29815. pkcs7->content = (byte*)testVectors[i].content;
  29816. pkcs7->contentSz = testVectors[i].contentSz;
  29817. pkcs7->contentOID = testVectors[i].contentOID;
  29818. pkcs7->encryptOID = testVectors[i].encryptOID;
  29819. pkcs7->ukm = testVectors[i].optionalUkm;
  29820. pkcs7->ukmSz = testVectors[i].optionalUkmSz;
  29821. ret = wc_PKCS7_AddRecipient_KEKRI(pkcs7, testVectors[i].keyWrapOID,
  29822. (byte *)testVectors[i].secretKey, testVectors[i].secretKeySz,
  29823. (byte *)testVectors[i].secretKeyId, testVectors[i].secretKeyIdSz,
  29824. testVectors[i].timePtr, testVectors[i].otherAttrOID,
  29825. testVectors[i].otherAttrOIDSz, testVectors[i].otherAttr,
  29826. testVectors[i].otherAttrSz, testVectors[i].kekriOptions);
  29827. if (ret < 0) {
  29828. wc_PKCS7_Free(pkcs7);
  29829. ERROR_OUT(-12174, out);
  29830. }
  29831. /* set key, for decryption */
  29832. ret = wc_PKCS7_SetKey(pkcs7, (byte *)testVectors[i].secretKey,
  29833. testVectors[i].secretKeySz);
  29834. if (ret != 0) {
  29835. wc_PKCS7_Free(pkcs7);
  29836. ERROR_OUT(-12175, out);
  29837. }
  29838. } else if (testVectors[i].password != NULL) {
  29839. #if !defined(NO_PWDBASED) && !defined(NO_SHA)
  29840. /* PWRI recipient type */
  29841. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  29842. if (ret != 0) {
  29843. ERROR_OUT(-12176, out);
  29844. }
  29845. pkcs7->content = (byte*)testVectors[i].content;
  29846. pkcs7->contentSz = testVectors[i].contentSz;
  29847. pkcs7->contentOID = testVectors[i].contentOID;
  29848. pkcs7->encryptOID = testVectors[i].encryptOID;
  29849. pkcs7->ukm = testVectors[i].optionalUkm;
  29850. pkcs7->ukmSz = testVectors[i].optionalUkmSz;
  29851. ret = wc_PKCS7_AddRecipient_PWRI(pkcs7,
  29852. (byte *)testVectors[i].password, testVectors[i].passwordSz,
  29853. (byte *)testVectors[i].salt, testVectors[i].saltSz,
  29854. testVectors[i].kdfOID,
  29855. testVectors[i].hashOID, testVectors[i].kdfIterations,
  29856. testVectors[i].encryptOID, testVectors[i].pwriOptions);
  29857. if (ret < 0) {
  29858. wc_PKCS7_Free(pkcs7);
  29859. ERROR_OUT(-12177, out);
  29860. }
  29861. /* set password, for decryption */
  29862. ret = wc_PKCS7_SetPassword(pkcs7, (byte*)testVectors[i].password,
  29863. testVectors[i].passwordSz);
  29864. if (ret < 0) {
  29865. wc_PKCS7_Free(pkcs7);
  29866. ERROR_OUT(-12178, out);
  29867. }
  29868. #endif /* ! NO_PWDBASED && ! NO_SHA */
  29869. } else if (testVectors[i].isOri == 1) {
  29870. /* ORI recipient type */
  29871. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  29872. if (ret != 0) {
  29873. ERROR_OUT(-12179, out);
  29874. }
  29875. pkcs7->content = (byte*)testVectors[i].content;
  29876. pkcs7->contentSz = testVectors[i].contentSz;
  29877. pkcs7->contentOID = testVectors[i].contentOID;
  29878. pkcs7->encryptOID = testVectors[i].encryptOID;
  29879. ret = wc_PKCS7_AddRecipient_ORI(pkcs7, myOriEncryptCb,
  29880. testVectors[i].oriOptions);
  29881. if (ret < 0) {
  29882. wc_PKCS7_Free(pkcs7);
  29883. ERROR_OUT(-12180, out);
  29884. }
  29885. /* set decrypt callback for decryption */
  29886. ret = wc_PKCS7_SetOriDecryptCb(pkcs7, myOriDecryptCb);
  29887. if (ret < 0) {
  29888. wc_PKCS7_Free(pkcs7);
  29889. ERROR_OUT(-12181, out);
  29890. }
  29891. } else {
  29892. /* KTRI or KARI recipient types */
  29893. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  29894. if (ret != 0) {
  29895. ERROR_OUT(-12182, out);
  29896. }
  29897. ret = wc_PKCS7_InitWithCert(pkcs7, testVectors[i].cert,
  29898. (word32)testVectors[i].certSz);
  29899. if (ret != 0) {
  29900. wc_PKCS7_Free(pkcs7);
  29901. ERROR_OUT(-12183, out);
  29902. }
  29903. pkcs7->keyWrapOID = testVectors[i].keyWrapOID;
  29904. pkcs7->keyAgreeOID = testVectors[i].keyAgreeOID;
  29905. pkcs7->privateKey = testVectors[i].privateKey;
  29906. pkcs7->privateKeySz = testVectors[i].privateKeySz;
  29907. pkcs7->content = (byte*)testVectors[i].content;
  29908. pkcs7->contentSz = testVectors[i].contentSz;
  29909. pkcs7->contentOID = testVectors[i].contentOID;
  29910. pkcs7->encryptOID = testVectors[i].encryptOID;
  29911. pkcs7->ukm = testVectors[i].optionalUkm;
  29912. pkcs7->ukmSz = testVectors[i].optionalUkmSz;
  29913. /* set SubjectIdentifier type for KTRI types */
  29914. if (testVectors[i].ktriOptions & CMS_SKID) {
  29915. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID);
  29916. if (ret != 0) {
  29917. wc_PKCS7_Free(pkcs7);
  29918. ERROR_OUT(-12184, out);
  29919. }
  29920. } else if (testVectors[i].ktriOptions &
  29921. CMS_ISSUER_AND_SERIAL_NUMBER) {
  29922. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7,
  29923. CMS_ISSUER_AND_SERIAL_NUMBER);
  29924. if (ret != 0) {
  29925. wc_PKCS7_Free(pkcs7);
  29926. ERROR_OUT(-12185, out);
  29927. }
  29928. }
  29929. }
  29930. #ifdef ECC_TIMING_RESISTANT
  29931. pkcs7->rng = &rng;
  29932. #endif
  29933. /* encode envelopedData */
  29934. envelopedSz = wc_PKCS7_EncodeEnvelopedData(pkcs7, enveloped,
  29935. PKCS7_BUF_SIZE);
  29936. if (envelopedSz <= 0) {
  29937. wc_PKCS7_Free(pkcs7);
  29938. ERROR_OUT(-12186, out);
  29939. }
  29940. /* decode envelopedData */
  29941. pkcs7->contentOID = 0;
  29942. decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, enveloped, envelopedSz,
  29943. decoded, PKCS7_BUF_SIZE);
  29944. if (pkcs7->contentOID != testVectors[i].contentOID ||
  29945. decodedSz <= 0) {
  29946. wc_PKCS7_Free(pkcs7);
  29947. ERROR_OUT(-12187, out);
  29948. }
  29949. /* test decode result */
  29950. if (XMEMCMP(decoded, data, sizeof(data)) != 0){
  29951. wc_PKCS7_Free(pkcs7);
  29952. ERROR_OUT(-12188, out);
  29953. }
  29954. #ifndef NO_PKCS7_STREAM
  29955. { /* test reading byte by byte */
  29956. int z;
  29957. for (z = 0; z < envelopedSz; z++) {
  29958. decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, enveloped + z, 1,
  29959. decoded, PKCS7_BUF_SIZE);
  29960. if (decodedSz <= 0 && decodedSz != WC_PKCS7_WANT_READ_E) {
  29961. printf("unexpected error %d\n", decodedSz);
  29962. ERROR_OUT(-12189, out);
  29963. }
  29964. }
  29965. /* test decode result */
  29966. if (XMEMCMP(decoded, data, sizeof(data)) != 0) {
  29967. printf("stream read compare failed\n");
  29968. wc_PKCS7_Free(pkcs7);
  29969. ERROR_OUT(-12190, out);
  29970. }
  29971. }
  29972. #endif
  29973. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  29974. /* output pkcs7 envelopedData for external testing */
  29975. pkcs7File = XFOPEN(testVectors[i].outFileName, "wb");
  29976. if (!pkcs7File) {
  29977. wc_PKCS7_Free(pkcs7);
  29978. ERROR_OUT(-12191, out);
  29979. }
  29980. ret = (int)XFWRITE(enveloped, 1, envelopedSz, pkcs7File);
  29981. XFCLOSE(pkcs7File);
  29982. if (ret != envelopedSz) {
  29983. wc_PKCS7_Free(pkcs7);
  29984. ERROR_OUT(-12192, out);
  29985. } else {
  29986. /* reset ret to 0 for success */
  29987. ret = 0;
  29988. }
  29989. #endif /* PKCS7_OUTPUT_TEST_BUNDLES */
  29990. wc_PKCS7_Free(pkcs7);
  29991. pkcs7 = NULL;
  29992. }
  29993. #ifdef ECC_TIMING_RESISTANT
  29994. wc_FreeRng(&rng);
  29995. #endif
  29996. (void)eccCert;
  29997. (void)eccCertSz;
  29998. (void)eccPrivKey;
  29999. (void)eccPrivKeySz;
  30000. (void)rsaCert;
  30001. (void)rsaCertSz;
  30002. (void)rsaPrivKey;
  30003. (void)rsaPrivKeySz;
  30004. out:
  30005. if (testVectors)
  30006. XFREE(testVectors, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30007. if (enveloped)
  30008. XFREE(enveloped, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30009. if (decoded)
  30010. XFREE(decoded, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30011. return ret;
  30012. }
  30013. WOLFSSL_TEST_SUBROUTINE int pkcs7enveloped_test(void)
  30014. {
  30015. int ret = 0;
  30016. byte* rsaCert = NULL;
  30017. byte* rsaPrivKey = NULL;
  30018. word32 rsaCertSz = 0;
  30019. word32 rsaPrivKeySz = 0;
  30020. byte* eccCert = NULL;
  30021. byte* eccPrivKey = NULL;
  30022. word32 eccCertSz = 0;
  30023. word32 eccPrivKeySz = 0;
  30024. #ifndef NO_RSA
  30025. /* read client RSA cert and key in DER format */
  30026. rsaCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30027. if (rsaCert == NULL)
  30028. return -12200;
  30029. rsaPrivKey = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30030. if (rsaPrivKey == NULL) {
  30031. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30032. return -12201;
  30033. }
  30034. rsaCertSz = FOURK_BUF;
  30035. rsaPrivKeySz = FOURK_BUF;
  30036. #endif /* NO_RSA */
  30037. #ifdef HAVE_ECC
  30038. /* read client ECC cert and key in DER format */
  30039. eccCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30040. if (eccCert == NULL) {
  30041. #ifndef NO_RSA
  30042. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30043. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30044. #endif
  30045. return -12202;
  30046. }
  30047. eccPrivKey =(byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30048. if (eccPrivKey == NULL) {
  30049. #ifndef NO_RSA
  30050. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30051. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30052. #endif
  30053. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30054. return -12203;
  30055. }
  30056. eccCertSz = FOURK_BUF;
  30057. eccPrivKeySz = FOURK_BUF;
  30058. #endif /* HAVE_ECC */
  30059. ret = pkcs7_load_certs_keys(rsaCert, &rsaCertSz, rsaPrivKey,
  30060. &rsaPrivKeySz, NULL, NULL, NULL, NULL,
  30061. NULL, NULL, NULL, NULL, eccCert, &eccCertSz,
  30062. eccPrivKey, &eccPrivKeySz);
  30063. if (ret < 0) {
  30064. #ifndef NO_RSA
  30065. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30066. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30067. #endif
  30068. #ifdef HAVE_ECC
  30069. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30070. XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30071. #endif
  30072. return -12204;
  30073. }
  30074. ret = pkcs7enveloped_run_vectors(rsaCert, (word32)rsaCertSz,
  30075. rsaPrivKey, (word32)rsaPrivKeySz,
  30076. eccCert, (word32)eccCertSz,
  30077. eccPrivKey, (word32)eccPrivKeySz);
  30078. #ifndef NO_RSA
  30079. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30080. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30081. #endif
  30082. #ifdef HAVE_ECC
  30083. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30084. XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30085. #endif
  30086. return ret;
  30087. }
  30088. #if defined(HAVE_AESGCM) || defined(HAVE_AESCCM)
  30089. typedef struct {
  30090. const byte* content;
  30091. word32 contentSz;
  30092. int contentOID;
  30093. int encryptOID;
  30094. int keyWrapOID;
  30095. int keyAgreeOID;
  30096. byte* cert;
  30097. size_t certSz;
  30098. byte* privateKey;
  30099. word32 privateKeySz;
  30100. PKCS7Attrib* authAttribs;
  30101. word32 authAttribsSz;
  30102. PKCS7Attrib* unauthAttribs;
  30103. word32 unauthAttribsSz;
  30104. /* KARI / KTRI specific */
  30105. byte* optionalUkm;
  30106. word32 optionalUkmSz;
  30107. int ktriOptions; /* KTRI options flags */
  30108. int kariOptions; /* KARI options flags */
  30109. /* KEKRI specific */
  30110. byte* secretKey; /* key, only for kekri RecipientInfo types */
  30111. word32 secretKeySz; /* size of secretKey, bytes */
  30112. byte* secretKeyId; /* key identifier */
  30113. word32 secretKeyIdSz; /* size of key identifier, bytes */
  30114. void* timePtr; /* time_t pointer */
  30115. byte* otherAttrOID; /* OPTIONAL, other attribute OID */
  30116. word32 otherAttrOIDSz; /* size of otherAttrOID, bytes */
  30117. byte* otherAttr; /* OPTIONAL, other attribute, ASN.1 encoded */
  30118. word32 otherAttrSz; /* size of otherAttr, bytes */
  30119. int kekriOptions; /* KEKRI options flags */
  30120. /* PWRI specific */
  30121. char* password; /* password */
  30122. word32 passwordSz; /* password size, bytes */
  30123. byte* salt; /* KDF salt */
  30124. word32 saltSz; /* KDF salt size, bytes */
  30125. int kdfOID; /* KDF OID */
  30126. int hashOID; /* KDF hash algorithm OID */
  30127. int kdfIterations; /* KDF iterations */
  30128. int kekEncryptOID; /* KEK encryption algorithm OID */
  30129. int pwriOptions; /* PWRI options flags */
  30130. /* ORI specific */
  30131. int isOri;
  30132. int oriOptions; /* ORI options flags */
  30133. const char* outFileName;
  30134. } pkcs7AuthEnvelopedVector;
  30135. static int pkcs7authenveloped_run_vectors(byte* rsaCert, word32 rsaCertSz,
  30136. byte* rsaPrivKey, word32 rsaPrivKeySz,
  30137. byte* eccCert, word32 eccCertSz,
  30138. byte* eccPrivKey, word32 eccPrivKeySz)
  30139. {
  30140. int ret = 0, testSz = 0, i;
  30141. int envelopedSz, decodedSz;
  30142. byte *enveloped = NULL;
  30143. byte *decoded = NULL;
  30144. WC_RNG rng;
  30145. PKCS7* pkcs7;
  30146. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  30147. XFILE pkcs7File;
  30148. #endif
  30149. WOLFSSL_SMALL_STACK_STATIC const byte data[] = { /* Hello World */
  30150. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  30151. 0x72,0x6c,0x64
  30152. };
  30153. byte senderNonce[PKCS7_NONCE_SZ + 2];
  30154. #ifdef HAVE_ECC
  30155. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  30156. #if !defined(NO_SHA256) && defined(WOLFSSL_AES_256)
  30157. WOLFSSL_SMALL_STACK_STATIC const byte senderNonceOid[] =
  30158. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  30159. 0x09, 0x05 };
  30160. PKCS7Attrib attribs[] =
  30161. {
  30162. { senderNonceOid, sizeof(senderNonceOid), senderNonce,
  30163. sizeof(senderNonce) }
  30164. };
  30165. #endif
  30166. #endif
  30167. #endif
  30168. #if !defined(NO_AES) && defined(WOLFSSL_AES_256) && defined(HAVE_ECC) && \
  30169. defined(WOLFSSL_SHA512)
  30170. WOLFSSL_SMALL_STACK_STATIC const byte optionalUkm[] = {
  30171. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07
  30172. };
  30173. #endif /* NO_AES */
  30174. #if !defined(NO_AES) && !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  30175. /* encryption key for kekri recipient types */
  30176. WOLFSSL_SMALL_STACK_STATIC const byte secretKey[] = {
  30177. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  30178. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07
  30179. };
  30180. /* encryption key identifier */
  30181. WOLFSSL_SMALL_STACK_STATIC const byte secretKeyId[] = {
  30182. 0x02,0x02,0x03,0x04
  30183. };
  30184. #endif
  30185. #if !defined(NO_PWDBASED) && !defined(NO_AES) && defined(HAVE_AESGCM) && \
  30186. !defined(NO_SHA) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  30187. #ifndef HAVE_FIPS
  30188. WOLFSSL_SMALL_STACK_STATIC const char password[] = "password";
  30189. #else
  30190. WOLFSSL_SMALL_STACK_STATIC const char password[] = "passwordFIPS_MODE";
  30191. #endif
  30192. WOLFSSL_SMALL_STACK_STATIC const byte salt[] = {
  30193. 0x12, 0x34, 0x56, 0x78, 0x78, 0x56, 0x34, 0x12
  30194. };
  30195. #endif
  30196. #define MAX_TESTVECTORS_LEN 20
  30197. #define ADD_PKCS7AUTHENVELOPEDVECTOR(...) { \
  30198. pkcs7AuthEnvelopedVector _this_vector = { __VA_ARGS__ }; \
  30199. if (testSz == MAX_TESTVECTORS_LEN) { \
  30200. ret = -12534; \
  30201. goto out; \
  30202. } \
  30203. XMEMCPY(&testVectors[testSz++], &_this_vector, sizeof _this_vector);\
  30204. }
  30205. pkcs7AuthEnvelopedVector *testVectors = NULL;
  30206. XMEMSET(&rng, 0, sizeof(rng));
  30207. testVectors = (pkcs7AuthEnvelopedVector *)XMALLOC(MAX_TESTVECTORS_LEN * sizeof(*testVectors),
  30208. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30209. if (testVectors == NULL) {
  30210. ret = -12534;
  30211. goto out;
  30212. }
  30213. {
  30214. /* key transport key encryption technique */
  30215. #ifndef NO_RSA
  30216. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  30217. #ifdef WOLFSSL_AES_128
  30218. ADD_PKCS7AUTHENVELOPEDVECTOR(
  30219. data, (word32)sizeof(data), DATA, AES128GCMb, 0, 0, rsaCert, rsaCertSz,
  30220. rsaPrivKey, rsaPrivKeySz, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0,
  30221. NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0,
  30222. 0, 0, "pkcs7authEnvelopedDataAES128GCM.der");
  30223. #endif
  30224. #ifdef WOLFSSL_AES_192
  30225. ADD_PKCS7AUTHENVELOPEDVECTOR(
  30226. data, (word32)sizeof(data), DATA, AES192GCMb, 0, 0, rsaCert, rsaCertSz,
  30227. rsaPrivKey, rsaPrivKeySz, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0,
  30228. NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0,
  30229. 0, 0, "pkcs7authEnvelopedDataAES192GCM.der");
  30230. #endif
  30231. #ifdef WOLFSSL_AES_256
  30232. ADD_PKCS7AUTHENVELOPEDVECTOR(
  30233. data, (word32)sizeof(data), DATA, AES256GCMb, 0, 0, rsaCert, rsaCertSz,
  30234. rsaPrivKey, rsaPrivKeySz, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0,
  30235. NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0,
  30236. 0, 0, "pkcs7authEnvelopedDataAES256GCM.der");
  30237. /* test with contentType set to FirmwarePkgData */
  30238. ADD_PKCS7AUTHENVELOPEDVECTOR(
  30239. data, (word32)sizeof(data), FIRMWARE_PKG_DATA, AES256GCMb, 0, 0,
  30240. rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz, NULL, 0, NULL, 0, NULL,
  30241. 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL,
  30242. 0, 0, 0, 0, 0, 0, 0, 0,
  30243. "pkcs7authEnvelopedDataAES256GCM_firmwarePkgData.der");
  30244. /* explicitly using SKID for SubjectKeyIdentifier */
  30245. ADD_PKCS7AUTHENVELOPEDVECTOR(
  30246. data, (word32)sizeof(data), DATA, AES256GCMb, 0, 0, rsaCert, rsaCertSz,
  30247. rsaPrivKey, rsaPrivKeySz, NULL, 0, NULL, 0, NULL, 0, CMS_SKID, 0,
  30248. NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0,
  30249. 0, 0, 0, 0, 0, "pkcs7authEnvelopedDataAES256GCM_SKID.der");
  30250. /* explicitly using IssuerAndSerialNumber for SubjectKeyIdentifier */
  30251. ADD_PKCS7AUTHENVELOPEDVECTOR(
  30252. data, (word32)sizeof(data), DATA, AES256GCMb, 0, 0, rsaCert, rsaCertSz,
  30253. rsaPrivKey, rsaPrivKeySz, NULL, 0, NULL, 0, NULL, 0,
  30254. CMS_ISSUER_AND_SERIAL_NUMBER, 0, NULL, 0, NULL, 0, NULL, NULL, 0,
  30255. NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0, 0,
  30256. "pkcs7authEnvelopedDataAES256GCM_IANDS.der");
  30257. #endif
  30258. #endif /* NO_AES */
  30259. #endif
  30260. /* key agreement key encryption technique*/
  30261. #ifdef HAVE_ECC
  30262. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  30263. #if !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  30264. ADD_PKCS7AUTHENVELOPEDVECTOR(
  30265. data, (word32)sizeof(data), DATA, AES128GCMb, AES128_WRAP,
  30266. dhSinglePass_stdDH_sha1kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  30267. eccPrivKeySz, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0, NULL, 0,
  30268. NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0, 0,
  30269. "pkcs7authEnvelopedDataAES128GCM_ECDH_SHA1KDF.der");
  30270. #endif
  30271. #if !defined(NO_SHA256) && defined(WOLFSSL_AES_256)
  30272. ADD_PKCS7AUTHENVELOPEDVECTOR(
  30273. data, (word32)sizeof(data), DATA, AES256GCMb, AES256_WRAP,
  30274. dhSinglePass_stdDH_sha256kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  30275. eccPrivKeySz, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0, NULL, 0,
  30276. NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0, 0,
  30277. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA256KDF.der");
  30278. /* with authenticated attributes */
  30279. ADD_PKCS7AUTHENVELOPEDVECTOR(
  30280. data, (word32)sizeof(data), DATA, AES256GCMb, AES256_WRAP,
  30281. dhSinglePass_stdDH_sha256kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  30282. eccPrivKeySz, attribs, (sizeof(attribs) / sizeof(PKCS7Attrib)),
  30283. NULL, 0, NULL, 0, 0, 0, NULL, 0,
  30284. NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0,
  30285. 0, 0, 0,
  30286. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA256KDF_authAttribs.der");
  30287. /* with unauthenticated attributes */
  30288. ADD_PKCS7AUTHENVELOPEDVECTOR(
  30289. data, (word32)sizeof(data), DATA, AES256GCMb, AES256_WRAP,
  30290. dhSinglePass_stdDH_sha256kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  30291. eccPrivKeySz, NULL, 0, attribs,
  30292. (sizeof(attribs) / sizeof(PKCS7Attrib)), NULL, 0, 0, 0, NULL, 0,
  30293. NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0,
  30294. 0, 0, 0,
  30295. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA256KDF_unauthAttribs.der");
  30296. /* with authenticated AND unauthenticated attributes */
  30297. ADD_PKCS7AUTHENVELOPEDVECTOR(
  30298. data, (word32)sizeof(data), DATA, AES256GCMb, AES256_WRAP,
  30299. dhSinglePass_stdDH_sha256kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  30300. eccPrivKeySz, attribs, (sizeof(attribs) / sizeof(PKCS7Attrib)),
  30301. attribs, (sizeof(attribs) / sizeof(PKCS7Attrib)), NULL, 0, 0, 0,
  30302. NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0,
  30303. 0, 0, 0, 0, 0, 0,
  30304. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA256KDF_bothAttribs.der");
  30305. /* with authenticated AND unauthenticated attributes AND
  30306. * contentType of FirmwarePkgData */
  30307. ADD_PKCS7AUTHENVELOPEDVECTOR(
  30308. data, (word32)sizeof(data), FIRMWARE_PKG_DATA, AES256GCMb, AES256_WRAP,
  30309. dhSinglePass_stdDH_sha256kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  30310. eccPrivKeySz, attribs, (sizeof(attribs) / sizeof(PKCS7Attrib)),
  30311. attribs, (sizeof(attribs) / sizeof(PKCS7Attrib)), NULL, 0, 0, 0,
  30312. NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0,
  30313. 0, 0, 0, 0, 0, 0,
  30314. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA256KDF_fw_bothAttribs.der");
  30315. #endif /* NO_SHA256 && WOLFSSL_AES_256 */
  30316. #if defined(WOLFSSL_SHA512) && defined(WOLFSSL_AES_256)
  30317. ADD_PKCS7AUTHENVELOPEDVECTOR(
  30318. data, (word32)sizeof(data), DATA, AES256GCMb, AES256_WRAP,
  30319. dhSinglePass_stdDH_sha512kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  30320. eccPrivKeySz, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL,
  30321. NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0, 0,
  30322. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA512KDF.der");
  30323. /* with optional user keying material (ukm) */
  30324. ADD_PKCS7AUTHENVELOPEDVECTOR(
  30325. data, (word32)sizeof(data), DATA, AES256GCMb, AES256_WRAP,
  30326. dhSinglePass_stdDH_sha512kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  30327. eccPrivKeySz, NULL, 0, NULL, 0, (byte *)optionalUkm, sizeof(optionalUkm), 0,
  30328. 0, NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0,
  30329. 0, 0, 0, 0, 0, 0,
  30330. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA512KDF_ukm.der");
  30331. #endif /* WOLFSSL_SHA512 && WOLFSSL_AES_256 */
  30332. #endif /* NO_AES */
  30333. #endif
  30334. /* kekri (KEKRecipientInfo) recipient types */
  30335. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  30336. #if !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  30337. ADD_PKCS7AUTHENVELOPEDVECTOR(
  30338. data, (word32)sizeof(data), DATA, AES128GCMb, AES128_WRAP, 0,
  30339. NULL, 0, NULL, 0, NULL, 0, NULL, 0, NULL, 0, 0, 0,
  30340. (byte *)secretKey, sizeof(secretKey), (byte *)secretKeyId, sizeof(secretKeyId),
  30341. NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0, 0,
  30342. "pkcs7authEnvelopedDataAES128GCM_KEKRI.der");
  30343. #endif
  30344. #endif
  30345. /* pwri (PasswordRecipientInfo) recipient types */
  30346. #if !defined(NO_PWDBASED) && !defined(NO_AES) && defined(HAVE_AESGCM)
  30347. #if !defined(NO_SHA) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  30348. ADD_PKCS7AUTHENVELOPEDVECTOR(
  30349. data, (word32)sizeof(data), DATA, AES128GCMb, 0, 0,
  30350. NULL, 0, NULL, 0, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0,
  30351. NULL, 0, NULL, NULL, 0, NULL, 0, 0, (char *)password,
  30352. (word32)XSTRLEN(password), (byte *)salt, sizeof(salt), PBKDF2_OID, WC_SHA, 5,
  30353. AES128CBCb, 0, 0, 0, "pkcs7authEnvelopedDataAES128GCM_PWRI.der");
  30354. #endif
  30355. #endif
  30356. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  30357. #ifdef WOLFSSL_AES_128
  30358. /* ori (OtherRecipientInfo) recipient types */
  30359. ADD_PKCS7AUTHENVELOPEDVECTOR(
  30360. data, (word32)sizeof(data), DATA, AES128GCMb, 0, 0, NULL, 0, NULL, 0,
  30361. NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL, 0,
  30362. NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 1, 0,
  30363. "pkcs7authEnvelopedDataAES128GCM_ORI.der");
  30364. #endif
  30365. #endif
  30366. }
  30367. #undef MAX_TESTVECTORS_LEN
  30368. #undef ADD_PKCS7AUTHENVELOPEDVECTOR
  30369. enveloped = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30370. decoded = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30371. if ((! enveloped) || (! decoded)) {
  30372. ERROR_OUT(-12210, out);
  30373. }
  30374. /* generate senderNonce */
  30375. {
  30376. #ifndef HAVE_FIPS
  30377. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  30378. #else
  30379. ret = wc_InitRng(&rng);
  30380. #endif
  30381. if (ret != 0) {
  30382. ERROR_OUT(-12211, out);
  30383. }
  30384. senderNonce[0] = 0x04;
  30385. senderNonce[1] = PKCS7_NONCE_SZ;
  30386. ret = wc_RNG_GenerateBlock(&rng, &senderNonce[2], PKCS7_NONCE_SZ);
  30387. if (ret != 0) {
  30388. wc_FreeRng(&rng);
  30389. ERROR_OUT(-12212, out);
  30390. }
  30391. }
  30392. for (i = 0; i < testSz; i++) {
  30393. pkcs7 = wc_PKCS7_New(HEAP_HINT,
  30394. #ifdef WOLFSSL_ASYNC_CRYPT
  30395. INVALID_DEVID /* async PKCS7 is not supported */
  30396. #else
  30397. devId
  30398. #endif
  30399. );
  30400. if (pkcs7 == NULL) {
  30401. ERROR_OUT(-12213, out);
  30402. }
  30403. if (testVectors[i].secretKey != NULL) {
  30404. /* KEKRI recipient type */
  30405. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  30406. if (ret != 0) {
  30407. ERROR_OUT(-12214, out);
  30408. }
  30409. pkcs7->content = (byte*)testVectors[i].content;
  30410. pkcs7->contentSz = testVectors[i].contentSz;
  30411. pkcs7->contentOID = testVectors[i].contentOID;
  30412. pkcs7->encryptOID = testVectors[i].encryptOID;
  30413. pkcs7->ukm = testVectors[i].optionalUkm;
  30414. pkcs7->ukmSz = testVectors[i].optionalUkmSz;
  30415. pkcs7->authAttribs = testVectors[i].authAttribs;
  30416. pkcs7->authAttribsSz = testVectors[i].authAttribsSz;
  30417. pkcs7->unauthAttribs = testVectors[i].unauthAttribs;
  30418. pkcs7->unauthAttribsSz = testVectors[i].unauthAttribsSz;
  30419. ret = wc_PKCS7_AddRecipient_KEKRI(pkcs7, testVectors[i].keyWrapOID,
  30420. testVectors[i].secretKey, testVectors[i].secretKeySz,
  30421. testVectors[i].secretKeyId, testVectors[i].secretKeyIdSz,
  30422. testVectors[i].timePtr, testVectors[i].otherAttrOID,
  30423. testVectors[i].otherAttrOIDSz, testVectors[i].otherAttr,
  30424. testVectors[i].otherAttrSz, testVectors[i].kekriOptions);
  30425. if (ret < 0) {
  30426. wc_PKCS7_Free(pkcs7);
  30427. ERROR_OUT(-12215, out);
  30428. }
  30429. /* set key, for decryption */
  30430. ret = wc_PKCS7_SetKey(pkcs7, testVectors[i].secretKey,
  30431. testVectors[i].secretKeySz);
  30432. if (ret != 0) {
  30433. wc_PKCS7_Free(pkcs7);
  30434. ERROR_OUT(-12216, out);
  30435. }
  30436. } else if (testVectors[i].password != NULL) {
  30437. #if !defined(NO_PWDBASED) && !defined(NO_SHA)
  30438. /* PWRI recipient type */
  30439. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  30440. if (ret != 0) {
  30441. ERROR_OUT(-12217, out);
  30442. }
  30443. pkcs7->content = (byte*)testVectors[i].content;
  30444. pkcs7->contentSz = testVectors[i].contentSz;
  30445. pkcs7->contentOID = testVectors[i].contentOID;
  30446. pkcs7->encryptOID = testVectors[i].encryptOID;
  30447. pkcs7->ukm = testVectors[i].optionalUkm;
  30448. pkcs7->ukmSz = testVectors[i].optionalUkmSz;
  30449. pkcs7->authAttribs = testVectors[i].authAttribs;
  30450. pkcs7->authAttribsSz = testVectors[i].authAttribsSz;
  30451. pkcs7->unauthAttribs = testVectors[i].unauthAttribs;
  30452. pkcs7->unauthAttribsSz = testVectors[i].unauthAttribsSz;
  30453. ret = wc_PKCS7_AddRecipient_PWRI(pkcs7,
  30454. (byte*)testVectors[i].password,
  30455. testVectors[i].passwordSz, testVectors[i].salt,
  30456. testVectors[i].saltSz, testVectors[i].kdfOID,
  30457. testVectors[i].hashOID, testVectors[i].kdfIterations,
  30458. testVectors[i].kekEncryptOID, testVectors[i].pwriOptions);
  30459. if (ret < 0) {
  30460. wc_PKCS7_Free(pkcs7);
  30461. ERROR_OUT(-12218, out);
  30462. }
  30463. /* set password, for decryption */
  30464. ret = wc_PKCS7_SetPassword(pkcs7, (byte*)testVectors[i].password,
  30465. testVectors[i].passwordSz);
  30466. if (ret < 0) {
  30467. wc_PKCS7_Free(pkcs7);
  30468. ERROR_OUT(-12219, out);
  30469. }
  30470. #endif /* ! NO_PWDBASED && ! NO_SHA */
  30471. } else if (testVectors[i].isOri == 1) {
  30472. /* ORI recipient type */
  30473. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  30474. if (ret != 0) {
  30475. ERROR_OUT(-12220, out);
  30476. }
  30477. pkcs7->content = (byte*)testVectors[i].content;
  30478. pkcs7->contentSz = testVectors[i].contentSz;
  30479. pkcs7->contentOID = testVectors[i].contentOID;
  30480. pkcs7->encryptOID = testVectors[i].encryptOID;
  30481. pkcs7->authAttribs = testVectors[i].authAttribs;
  30482. pkcs7->authAttribsSz = testVectors[i].authAttribsSz;
  30483. pkcs7->unauthAttribs = testVectors[i].unauthAttribs;
  30484. pkcs7->unauthAttribsSz = testVectors[i].unauthAttribsSz;
  30485. ret = wc_PKCS7_AddRecipient_ORI(pkcs7, myOriEncryptCb,
  30486. testVectors[i].oriOptions);
  30487. if (ret < 0) {
  30488. wc_PKCS7_Free(pkcs7);
  30489. ERROR_OUT(-12221, out);
  30490. }
  30491. /* set decrypt callback for decryption */
  30492. ret = wc_PKCS7_SetOriDecryptCb(pkcs7, myOriDecryptCb);
  30493. if (ret < 0) {
  30494. wc_PKCS7_Free(pkcs7);
  30495. ERROR_OUT(-12222, out);
  30496. }
  30497. } else {
  30498. /* KTRI or KARI recipient types */
  30499. ret = wc_PKCS7_InitWithCert(pkcs7, testVectors[i].cert,
  30500. (word32)testVectors[i].certSz);
  30501. if (ret != 0) {
  30502. wc_PKCS7_Free(pkcs7);
  30503. ERROR_OUT(-12223, out);
  30504. }
  30505. pkcs7->keyWrapOID = testVectors[i].keyWrapOID;
  30506. pkcs7->keyAgreeOID = testVectors[i].keyAgreeOID;
  30507. pkcs7->privateKey = testVectors[i].privateKey;
  30508. pkcs7->privateKeySz = testVectors[i].privateKeySz;
  30509. pkcs7->content = (byte*)testVectors[i].content;
  30510. pkcs7->contentSz = testVectors[i].contentSz;
  30511. pkcs7->contentOID = testVectors[i].contentOID;
  30512. pkcs7->encryptOID = testVectors[i].encryptOID;
  30513. pkcs7->ukm = testVectors[i].optionalUkm;
  30514. pkcs7->ukmSz = testVectors[i].optionalUkmSz;
  30515. pkcs7->authAttribs = testVectors[i].authAttribs;
  30516. pkcs7->authAttribsSz = testVectors[i].authAttribsSz;
  30517. pkcs7->unauthAttribs = testVectors[i].unauthAttribs;
  30518. pkcs7->unauthAttribsSz = testVectors[i].unauthAttribsSz;
  30519. /* set SubjectIdentifier type for KTRI types */
  30520. if (testVectors[i].ktriOptions & CMS_SKID) {
  30521. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID);
  30522. if (ret != 0) {
  30523. wc_PKCS7_Free(pkcs7);
  30524. ERROR_OUT(-12224, out);
  30525. }
  30526. } else if (testVectors[i].ktriOptions &
  30527. CMS_ISSUER_AND_SERIAL_NUMBER) {
  30528. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7,
  30529. CMS_ISSUER_AND_SERIAL_NUMBER);
  30530. if (ret != 0) {
  30531. wc_PKCS7_Free(pkcs7);
  30532. ERROR_OUT(-12225, out);
  30533. }
  30534. }
  30535. }
  30536. #ifdef ECC_TIMING_RESISTANT
  30537. pkcs7->rng = &rng;
  30538. #endif
  30539. /* encode envelopedData */
  30540. envelopedSz = wc_PKCS7_EncodeAuthEnvelopedData(pkcs7, enveloped,
  30541. PKCS7_BUF_SIZE);
  30542. if (envelopedSz <= 0) {
  30543. wc_PKCS7_Free(pkcs7);
  30544. ERROR_OUT(-12226, out);
  30545. }
  30546. #ifndef NO_PKCS7_STREAM
  30547. { /* test reading byte by byte */
  30548. int z;
  30549. for (z = 0; z < envelopedSz; z++) {
  30550. decodedSz = wc_PKCS7_DecodeAuthEnvelopedData(pkcs7,
  30551. enveloped + z, 1, decoded, PKCS7_BUF_SIZE);
  30552. if (decodedSz <= 0 && decodedSz != WC_PKCS7_WANT_READ_E) {
  30553. printf("unexpected error %d\n", decodedSz);
  30554. ERROR_OUT(-12227, out);
  30555. }
  30556. }
  30557. /* test decode result */
  30558. if (XMEMCMP(decoded, data, sizeof(data)) != 0) {
  30559. printf("stream read compare failed\n");
  30560. wc_PKCS7_Free(pkcs7);
  30561. ERROR_OUT(-12228, out);
  30562. }
  30563. }
  30564. #endif
  30565. /* decode envelopedData */
  30566. decodedSz = wc_PKCS7_DecodeAuthEnvelopedData(pkcs7, enveloped,
  30567. envelopedSz, decoded,
  30568. PKCS7_BUF_SIZE);
  30569. if (decodedSz <= 0) {
  30570. wc_PKCS7_Free(pkcs7);
  30571. ERROR_OUT(-12229, out);
  30572. }
  30573. /* test decode result */
  30574. if (XMEMCMP(decoded, data, sizeof(data)) != 0){
  30575. wc_PKCS7_Free(pkcs7);
  30576. ERROR_OUT(-12230, out);
  30577. }
  30578. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  30579. /* output pkcs7 envelopedData for external testing */
  30580. pkcs7File = XFOPEN(testVectors[i].outFileName, "wb");
  30581. if (!pkcs7File) {
  30582. wc_PKCS7_Free(pkcs7);
  30583. ERROR_OUT(-12231, out);
  30584. }
  30585. ret = (int)XFWRITE(enveloped, 1, envelopedSz, pkcs7File);
  30586. XFCLOSE(pkcs7File);
  30587. if (ret != envelopedSz) {
  30588. wc_PKCS7_Free(pkcs7);
  30589. ERROR_OUT(-12232, out);
  30590. } else {
  30591. /* reset ret to 0 for success */
  30592. ret = 0;
  30593. }
  30594. #endif /* PKCS7_OUTPUT_TEST_BUNDLES */
  30595. wc_PKCS7_Free(pkcs7);
  30596. pkcs7 = NULL;
  30597. }
  30598. wc_FreeRng(&rng);
  30599. (void)eccCert;
  30600. (void)eccCertSz;
  30601. (void)eccPrivKey;
  30602. (void)eccPrivKeySz;
  30603. #if !defined(NO_AES) && !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  30604. (void)secretKey;
  30605. (void)secretKeyId;
  30606. #endif
  30607. #ifdef NO_RSA
  30608. (void)rsaCert;
  30609. (void)rsaCertSz;
  30610. (void)rsaPrivKey;
  30611. (void)rsaPrivKeySz;
  30612. #endif
  30613. out:
  30614. if (testVectors)
  30615. XFREE(testVectors, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30616. if (enveloped)
  30617. XFREE(enveloped, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30618. if (decoded)
  30619. XFREE(decoded, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30620. return ret;
  30621. }
  30622. WOLFSSL_TEST_SUBROUTINE int pkcs7authenveloped_test(void)
  30623. {
  30624. int ret = 0;
  30625. byte* rsaCert = NULL;
  30626. byte* rsaPrivKey = NULL;
  30627. word32 rsaCertSz = 0;
  30628. word32 rsaPrivKeySz = 0;
  30629. byte* eccCert = NULL;
  30630. byte* eccPrivKey = NULL;
  30631. word32 eccCertSz = 0;
  30632. word32 eccPrivKeySz = 0;
  30633. #ifndef NO_RSA
  30634. /* read client RSA cert and key in DER format */
  30635. rsaCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30636. if (rsaCert == NULL)
  30637. return -12300;
  30638. rsaPrivKey = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30639. if (rsaPrivKey == NULL) {
  30640. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30641. return -12301;
  30642. }
  30643. rsaCertSz = FOURK_BUF;
  30644. rsaPrivKeySz = FOURK_BUF;
  30645. #endif /* NO_RSA */
  30646. #ifdef HAVE_ECC
  30647. /* read client ECC cert and key in DER format */
  30648. eccCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30649. if (eccCert == NULL) {
  30650. #ifndef NO_RSA
  30651. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30652. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30653. #endif
  30654. return -12302;
  30655. }
  30656. eccPrivKey =(byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30657. if (eccPrivKey == NULL) {
  30658. #ifndef NO_RSA
  30659. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30660. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30661. #endif
  30662. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30663. return -12303;
  30664. }
  30665. eccCertSz = FOURK_BUF;
  30666. eccPrivKeySz = FOURK_BUF;
  30667. #endif /* HAVE_ECC */
  30668. ret = pkcs7_load_certs_keys(rsaCert, &rsaCertSz, rsaPrivKey,
  30669. &rsaPrivKeySz, NULL, NULL, NULL, NULL,
  30670. NULL, NULL, NULL, NULL, eccCert, &eccCertSz,
  30671. eccPrivKey, &eccPrivKeySz);
  30672. if (ret < 0) {
  30673. #ifndef NO_RSA
  30674. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30675. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30676. #endif
  30677. #ifdef HAVE_ECC
  30678. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30679. XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30680. #endif
  30681. return -12304;
  30682. }
  30683. ret = pkcs7authenveloped_run_vectors(rsaCert, (word32)rsaCertSz,
  30684. rsaPrivKey, (word32)rsaPrivKeySz,
  30685. eccCert, (word32)eccCertSz,
  30686. eccPrivKey, (word32)eccPrivKeySz);
  30687. #ifndef NO_RSA
  30688. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30689. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30690. #endif
  30691. #ifdef HAVE_ECC
  30692. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30693. XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30694. #endif
  30695. return ret;
  30696. }
  30697. #endif /* HAVE_AESGCM || HAVE_AESCCM */
  30698. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  30699. static const byte p7DefKey[] = {
  30700. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  30701. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  30702. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  30703. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  30704. };
  30705. static const byte p7AltKey[] = {
  30706. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  30707. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  30708. };
  30709. static int myCEKwrapFunc(PKCS7* pkcs7, byte* cek, word32 cekSz, byte* keyId,
  30710. word32 keyIdSz, byte* orginKey, word32 orginKeySz,
  30711. byte* out, word32 outSz, int keyWrapAlgo, int type, int direction)
  30712. {
  30713. int ret;
  30714. if (cek == NULL || out == NULL)
  30715. return BAD_FUNC_ARG;
  30716. /* test case sanity checks */
  30717. if (keyIdSz != 1) {
  30718. return -12310;
  30719. }
  30720. if (keyId[0] != 0x00) {
  30721. return -12311;
  30722. }
  30723. if (type != (int)PKCS7_KEKRI) {
  30724. return -12312;
  30725. }
  30726. switch (keyWrapAlgo) {
  30727. case AES256_WRAP:
  30728. ret = wc_AesKeyUnWrap(p7DefKey, sizeof(p7DefKey), cek, cekSz,
  30729. out, outSz, NULL);
  30730. if (ret <= 0)
  30731. return ret;
  30732. break;
  30733. default:
  30734. WOLFSSL_MSG("Unsupported key wrap algorithm in example");
  30735. return BAD_KEYWRAP_ALG_E;
  30736. };
  30737. (void)pkcs7;
  30738. (void)direction;
  30739. (void)orginKey; /* used with KAKRI */
  30740. (void)orginKeySz;
  30741. return ret;
  30742. }
  30743. /* returns key size on success */
  30744. static int getFirmwareKey(PKCS7* pkcs7, byte* key, word32 keySz)
  30745. {
  30746. int ret;
  30747. word32 atrSz;
  30748. byte atr[256];
  30749. /* Additionally can look for fwWrappedFirmwareKey
  30750. * 1.2.840.113529.1.9.16.1.16 */
  30751. const unsigned char fwWrappedFirmwareKey[] = {
  30752. /* 0x06, 0x0B */
  30753. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  30754. 0x01, 0x09, 0x10, 0x02, 0x27
  30755. };
  30756. /* find keyID in fwWrappedFirmwareKey */
  30757. ret = wc_PKCS7_GetAttributeValue(pkcs7, fwWrappedFirmwareKey,
  30758. sizeof(fwWrappedFirmwareKey), NULL, &atrSz);
  30759. if (ret == LENGTH_ONLY_E) {
  30760. XMEMSET(atr, 0, sizeof(atr));
  30761. ret = wc_PKCS7_GetAttributeValue(pkcs7, fwWrappedFirmwareKey,
  30762. sizeof(fwWrappedFirmwareKey), atr, &atrSz);
  30763. /* keyIdRaw[0] OCTET TAG */
  30764. /* keyIdRaw[1] Length */
  30765. if (ret > 0) {
  30766. PKCS7* envPkcs7;
  30767. envPkcs7 = wc_PKCS7_New(NULL, 0);
  30768. if (envPkcs7 == NULL) {
  30769. return MEMORY_E;
  30770. }
  30771. wc_PKCS7_Init(envPkcs7, NULL, 0);
  30772. ret = wc_PKCS7_SetWrapCEKCb(envPkcs7, myCEKwrapFunc);
  30773. if (ret == 0) {
  30774. /* expecting FIRMWARE_PKG_DATA content */
  30775. envPkcs7->contentOID = FIRMWARE_PKG_DATA;
  30776. ret = wc_PKCS7_DecodeEnvelopedData(envPkcs7, atr, atrSz,
  30777. key, keySz);
  30778. if (envPkcs7->contentOID != FIRMWARE_PKG_DATA) {
  30779. /* the contentOID should have been set to the inner
  30780. * FIRMWARE_PKG_DATA content */
  30781. ret = BAD_STATE_E;
  30782. }
  30783. }
  30784. wc_PKCS7_Free(envPkcs7);
  30785. }
  30786. }
  30787. return ret;
  30788. }
  30789. /* create a KEKRI enveloped data
  30790. * return size on success */
  30791. static int envelopedData_encrypt(byte* in, word32 inSz, byte* out,
  30792. word32 outSz)
  30793. {
  30794. int ret;
  30795. PKCS7* pkcs7;
  30796. WOLFSSL_SMALL_STACK_STATIC const byte keyId[] = { 0x00 };
  30797. pkcs7 = wc_PKCS7_New(NULL, INVALID_DEVID);
  30798. if (pkcs7 == NULL)
  30799. return -12330;
  30800. pkcs7->content = in;
  30801. pkcs7->contentSz = inSz;
  30802. pkcs7->contentOID = FIRMWARE_PKG_DATA;
  30803. pkcs7->encryptOID = AES256CBCb;
  30804. pkcs7->ukm = NULL;
  30805. pkcs7->ukmSz = 0;
  30806. /* add recipient (KEKRI type) */
  30807. ret = wc_PKCS7_AddRecipient_KEKRI(pkcs7, AES256_WRAP, (byte*)p7DefKey,
  30808. sizeof(p7DefKey), (byte*)keyId,
  30809. sizeof(keyId), NULL, NULL, 0, NULL, 0, 0);
  30810. if (ret < 0) {
  30811. printf("wc_PKCS7_AddRecipient_KEKRI() failed, ret = %d\n", ret);
  30812. wc_PKCS7_Free(pkcs7);
  30813. return -12331;
  30814. }
  30815. /* encode envelopedData, returns size */
  30816. ret = wc_PKCS7_EncodeEnvelopedData(pkcs7, out, outSz);
  30817. if (ret <= 0) {
  30818. printf("wc_PKCS7_EncodeEnvelopedData() failed, ret = %d\n", ret);
  30819. wc_PKCS7_Free(pkcs7);
  30820. return -12332;
  30821. }
  30822. wc_PKCS7_Free(pkcs7);
  30823. return ret;
  30824. }
  30825. /*
  30826. * keyHint is the KeyID to be set in the fwDecryptKeyID attribute
  30827. * returns size of buffer output on success
  30828. */
  30829. static int generateBundle(byte* out, word32 *outSz, const byte* encryptKey,
  30830. word32 encryptKeySz, byte keyHint, byte* cert, word32 certSz,
  30831. byte* key, word32 keySz)
  30832. {
  30833. int ret, attribNum = 1;
  30834. PKCS7* pkcs7;
  30835. /* KEY ID
  30836. * fwDecryptKeyID OID 1.2.840.113549.1.9.16.2.37
  30837. */
  30838. const unsigned char fwDecryptKeyID[] = {
  30839. 0x06, 0x0B,
  30840. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  30841. 0x01, 0x09, 0x10, 0x02, 0x25
  30842. };
  30843. /* fwWrappedFirmwareKey 1.2.840.113529.1.9.16.1.16 */
  30844. const unsigned char fwWrappedFirmwareKey[] = {
  30845. 0x06, 0x0B, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  30846. 0x01, 0x09, 0x10, 0x02, 0x27
  30847. };
  30848. byte keyID[] = { 0x04, 0x01, 0x00 };
  30849. byte env[256];
  30850. char data[] = "Test of wolfSSL PKCS7 decrypt callback";
  30851. PKCS7Attrib attribs[] =
  30852. {
  30853. { fwDecryptKeyID, sizeof(fwDecryptKeyID), keyID, sizeof(keyID) },
  30854. { fwWrappedFirmwareKey, sizeof(fwWrappedFirmwareKey), env, 0 }
  30855. };
  30856. keyID[2] = keyHint;
  30857. /* If using keyHint 0 then create a bundle with fwWrappedFirmwareKey */
  30858. if (keyHint == 0) {
  30859. ret = envelopedData_encrypt((byte*)p7DefKey, sizeof(p7DefKey), env,
  30860. sizeof(env));
  30861. if (ret <= 0) {
  30862. return ret;
  30863. }
  30864. attribs[1].valueSz = ret;
  30865. attribNum++;
  30866. }
  30867. /* init PKCS7 */
  30868. pkcs7 = wc_PKCS7_New(NULL, INVALID_DEVID);
  30869. if (pkcs7 == NULL)
  30870. return -12340;
  30871. ret = wc_PKCS7_InitWithCert(pkcs7, cert, certSz);
  30872. if (ret != 0) {
  30873. printf("ERROR: wc_PKCS7_InitWithCert() failed, ret = %d\n", ret);
  30874. wc_PKCS7_Free(pkcs7);
  30875. return -12341;
  30876. }
  30877. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID);
  30878. if (ret != 0) {
  30879. wc_PKCS7_Free(pkcs7);
  30880. return -12342;
  30881. }
  30882. /* encode Signed Encrypted FirmwarePkgData */
  30883. if (encryptKeySz == 16) {
  30884. ret = wc_PKCS7_EncodeSignedEncryptedFPD(pkcs7, (byte*)encryptKey,
  30885. encryptKeySz, key, keySz, AES128CBCb, RSAk, SHA256h,
  30886. (byte*)data, sizeof(data), NULL, 0,
  30887. attribs, attribNum, out, *outSz);
  30888. }
  30889. else {
  30890. ret = wc_PKCS7_EncodeSignedEncryptedFPD(pkcs7, (byte*)encryptKey,
  30891. encryptKeySz, key, keySz, AES256CBCb, RSAk, SHA256h,
  30892. (byte*)data, sizeof(data), NULL, 0,
  30893. attribs, attribNum, out, *outSz);
  30894. }
  30895. if (ret <= 0) {
  30896. printf("ERROR: wc_PKCS7_EncodeSignedEncryptedFPD() failed, "
  30897. "ret = %d\n", ret);
  30898. wc_PKCS7_Free(pkcs7);
  30899. return -12343;
  30900. } else {
  30901. *outSz = ret;
  30902. }
  30903. wc_PKCS7_Free(pkcs7);
  30904. return ret;
  30905. }
  30906. /* test verification and decryption of PKCS7 bundle
  30907. * return 0 on success
  30908. */
  30909. static int verifyBundle(byte* derBuf, word32 derSz, int keyHint)
  30910. {
  30911. int ret = 0;
  30912. int usrCtx = 1; /* test value to pass as user context to callback */
  30913. PKCS7* pkcs7 = NULL;
  30914. byte* sid = NULL;
  30915. word32 sidSz;
  30916. byte key[256];
  30917. word32 keySz = sizeof(key);
  30918. byte *decoded = NULL;
  30919. int decodedSz = FOURK_BUF/2;
  30920. WOLFSSL_SMALL_STACK_STATIC const byte expectedSid[] = {
  30921. #ifdef USE_CERT_BUFFERS_1024
  30922. 0x81, 0x69, 0x0f, 0xf8, 0xdf, 0xdd, 0xcf, 0x34,
  30923. 0x29, 0xd5, 0x67, 0x75, 0x71, 0x85, 0xc7, 0x75,
  30924. 0x10, 0x69, 0x59, 0xec,
  30925. #else
  30926. 0x33, 0xD8, 0x45, 0x66, 0xD7, 0x68, 0x87, 0x18,
  30927. 0x7E, 0x54, 0x0D, 0x70, 0x27, 0x91, 0xC7, 0x26,
  30928. 0xD7, 0x85, 0x65, 0xC0
  30929. #endif
  30930. };
  30931. decoded = (byte *)XMALLOC(decodedSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30932. if (decoded == NULL) {
  30933. ret = MEMORY_E;
  30934. goto out;
  30935. }
  30936. pkcs7 = wc_PKCS7_New(HEAP_HINT, INVALID_DEVID);
  30937. if (pkcs7 == NULL) {
  30938. ret = MEMORY_E;
  30939. goto out;
  30940. }
  30941. /* Test verify */
  30942. ret = wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID);
  30943. if (ret != 0)
  30944. goto out;
  30945. ret = wc_PKCS7_InitWithCert(pkcs7, NULL, 0);
  30946. if (ret != 0)
  30947. goto out;
  30948. ret = wc_PKCS7_VerifySignedData(pkcs7, derBuf, derSz);
  30949. if (ret != 0)
  30950. goto out;
  30951. /* Get size of SID and print it out */
  30952. ret = wc_PKCS7_GetSignerSID(pkcs7, NULL, &sidSz);
  30953. if (ret != LENGTH_ONLY_E)
  30954. goto out;
  30955. sid = (byte*)XMALLOC(sidSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30956. if (sid == NULL) {
  30957. ret = MEMORY_E;
  30958. goto out;
  30959. }
  30960. ret = wc_PKCS7_GetSignerSID(pkcs7, sid, &sidSz);
  30961. if (ret != 0)
  30962. goto out;
  30963. ret = XMEMCMP(sid, expectedSid, sidSz);
  30964. if (ret != 0) {
  30965. ret = PKCS7_NO_SIGNER_E; /* close enough */
  30966. goto out;
  30967. }
  30968. /* get expected fwWrappedFirmwareKey */
  30969. if (keyHint == 0) {
  30970. ret = getFirmwareKey(pkcs7, key, keySz);
  30971. if (ret < 0)
  30972. goto out;
  30973. pkcs7->encryptionKey = key;
  30974. pkcs7->encryptionKeySz = ret;
  30975. }
  30976. else {
  30977. decodedSz = PKCS7_BUF_SIZE;
  30978. ret = wc_PKCS7_SetDecodeEncryptedCb(pkcs7, myDecryptionFunc);
  30979. if (ret != 0)
  30980. goto out;
  30981. ret = wc_PKCS7_SetDecodeEncryptedCtx(pkcs7, (void*)&usrCtx);
  30982. if (ret != 0)
  30983. goto out;
  30984. }
  30985. decodedSz = wc_PKCS7_DecodeEncryptedData(pkcs7, pkcs7->content,
  30986. pkcs7->contentSz, decoded, decodedSz);
  30987. if (decodedSz < 0) {
  30988. ret = decodedSz;
  30989. goto out;
  30990. }
  30991. ret = 0;
  30992. out:
  30993. if (decoded)
  30994. XFREE(decoded, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30995. if (pkcs7)
  30996. wc_PKCS7_Free(pkcs7);
  30997. if (sid)
  30998. XFREE(sid, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30999. return ret;
  31000. }
  31001. WOLFSSL_TEST_SUBROUTINE int pkcs7callback_test(byte* cert, word32 certSz, byte* key, word32 keySz)
  31002. {
  31003. int ret = 0;
  31004. word32 derSz;
  31005. byte *derBuf = (byte *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  31006. if (! derBuf)
  31007. ERROR_OUT(-12360, out);
  31008. /* Doing default generation and verify */
  31009. derSz = FOURK_BUF;
  31010. ret = generateBundle(derBuf, &derSz, p7DefKey, sizeof(p7DefKey), 0, cert,
  31011. certSz, key, keySz);
  31012. if (ret <= 0) {
  31013. ERROR_OUT(-12361, out);
  31014. }
  31015. ret = verifyBundle(derBuf, derSz, 0);
  31016. if (ret != 0) {
  31017. ERROR_OUT(-12362, out);
  31018. }
  31019. /* test choosing other key with keyID */
  31020. derSz = FOURK_BUF;
  31021. ret = generateBundle(derBuf, &derSz, p7AltKey, sizeof(p7AltKey), 1,
  31022. cert, certSz, key, keySz);
  31023. if (ret <= 0) {
  31024. ERROR_OUT(-12363, out);
  31025. }
  31026. ret = verifyBundle(derBuf, derSz, 1);
  31027. if (ret != 0) {
  31028. ERROR_OUT(-12364, out);
  31029. }
  31030. /* test fail case with wrong keyID */
  31031. derSz = FOURK_BUF;
  31032. ret = generateBundle(derBuf, &derSz, p7DefKey, sizeof(p7DefKey), 1,
  31033. cert, certSz, key, keySz);
  31034. if (ret <= 0) {
  31035. ERROR_OUT(-12365, out);
  31036. }
  31037. ret = verifyBundle(derBuf, derSz, 1);
  31038. if (ret == 0) {
  31039. ERROR_OUT(-12366, out);
  31040. }
  31041. ret = 0;
  31042. out:
  31043. if (derBuf)
  31044. XFREE(derBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  31045. return ret;
  31046. }
  31047. #endif /* !NO_AES && HAVE_AES_CBC */
  31048. #ifndef NO_PKCS7_ENCRYPTED_DATA
  31049. typedef struct {
  31050. const byte* content;
  31051. word32 contentSz;
  31052. int contentOID;
  31053. int encryptOID;
  31054. byte* encryptionKey;
  31055. word32 encryptionKeySz;
  31056. PKCS7Attrib* attribs;
  31057. word32 attribsSz;
  31058. const char* outFileName;
  31059. } pkcs7EncryptedVector;
  31060. WOLFSSL_TEST_SUBROUTINE int pkcs7encrypted_test(void)
  31061. {
  31062. int ret = 0;
  31063. int i, testSz;
  31064. int encryptedSz, decodedSz, attribIdx;
  31065. PKCS7* pkcs7;
  31066. byte *encrypted;
  31067. byte *decoded;
  31068. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  31069. XFILE pkcs7File;
  31070. #endif
  31071. PKCS7Attrib* expectedAttrib;
  31072. PKCS7DecodedAttrib* decodedAttrib;
  31073. WOLFSSL_SMALL_STACK_STATIC const byte data[] = { /* Hello World */
  31074. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  31075. 0x72,0x6c,0x64
  31076. };
  31077. #ifndef NO_DES3
  31078. byte desKey[] = {
  31079. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
  31080. };
  31081. byte des3Key[] = {
  31082. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  31083. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  31084. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  31085. };
  31086. #endif
  31087. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  31088. #ifdef WOLFSSL_AES_128
  31089. byte aes128Key[] = {
  31090. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  31091. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  31092. };
  31093. #endif
  31094. #ifdef WOLFSSL_AES_192
  31095. byte aes192Key[] = {
  31096. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  31097. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  31098. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  31099. };
  31100. #endif
  31101. #ifdef WOLFSSL_AES_256
  31102. byte aes256Key[] = {
  31103. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  31104. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  31105. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  31106. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  31107. };
  31108. #endif
  31109. #ifdef WOLFSSL_AES_256
  31110. /* Attribute example from RFC 4134, Section 7.2
  31111. * OID = 1.2.5555
  31112. * OCTET STRING = 'This is a test General ASN Attribute, number 1.' */
  31113. static byte genAttrOid[] = { 0x06, 0x03, 0x2a, 0xab, 0x33 };
  31114. static byte genAttr[] = { 0x04, 47,
  31115. 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20,
  31116. 0x61, 0x20, 0x74, 0x65, 0x73, 0x74, 0x20, 0x47,
  31117. 0x65, 0x6e, 0x65, 0x72, 0x61, 0x6c, 0x20, 0x41,
  31118. 0x53, 0x4e, 0x20, 0x41, 0x74, 0x74, 0x72, 0x69,
  31119. 0x62, 0x75, 0x74, 0x65, 0x2c, 0x20, 0x6e, 0x75,
  31120. 0x6d, 0x62, 0x65, 0x72, 0x20, 0x31, 0x2e };
  31121. static byte genAttrOid2[] = { 0x06, 0x03, 0x2a, 0xab, 0x34 };
  31122. static byte genAttr2[] = { 0x04, 47,
  31123. 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20,
  31124. 0x61, 0x20, 0x74, 0x65, 0x73, 0x74, 0x20, 0x47,
  31125. 0x65, 0x6e, 0x65, 0x72, 0x61, 0x6c, 0x20, 0x41,
  31126. 0x53, 0x4e, 0x20, 0x41, 0x74, 0x74, 0x72, 0x69,
  31127. 0x62, 0x75, 0x74, 0x65, 0x2c, 0x20, 0x6e, 0x75,
  31128. 0x6d, 0x62, 0x65, 0x72, 0x20, 0x32, 0x2e };
  31129. PKCS7Attrib attribs[] =
  31130. {
  31131. { genAttrOid, sizeof(genAttrOid), genAttr, sizeof(genAttr) }
  31132. };
  31133. PKCS7Attrib multiAttribs[] =
  31134. {
  31135. { genAttrOid, sizeof(genAttrOid), genAttr, sizeof(genAttr) },
  31136. { genAttrOid2, sizeof(genAttrOid2), genAttr2, sizeof(genAttr2) }
  31137. };
  31138. #endif
  31139. #endif /* NO_AES */
  31140. const pkcs7EncryptedVector testVectors[] =
  31141. {
  31142. #ifndef NO_DES3
  31143. {data, (word32)sizeof(data), DATA, DES3b, des3Key, sizeof(des3Key),
  31144. NULL, 0, "pkcs7encryptedDataDES3.der"},
  31145. {data, (word32)sizeof(data), DATA, DESb, desKey, sizeof(desKey),
  31146. NULL, 0, "pkcs7encryptedDataDES.der"},
  31147. #endif /* NO_DES3 */
  31148. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  31149. #ifdef WOLFSSL_AES_128
  31150. {data, (word32)sizeof(data), DATA, AES128CBCb, aes128Key,
  31151. sizeof(aes128Key), NULL, 0, "pkcs7encryptedDataAES128CBC.der"},
  31152. #endif
  31153. #ifdef WOLFSSL_AES_192
  31154. {data, (word32)sizeof(data), DATA, AES192CBCb, aes192Key,
  31155. sizeof(aes192Key), NULL, 0, "pkcs7encryptedDataAES192CBC.der"},
  31156. #endif
  31157. #ifdef WOLFSSL_AES_256
  31158. {data, (word32)sizeof(data), DATA, AES256CBCb, aes256Key,
  31159. sizeof(aes256Key), NULL, 0, "pkcs7encryptedDataAES256CBC.der"},
  31160. /* test with optional unprotected attributes */
  31161. {data, (word32)sizeof(data), DATA, AES256CBCb, aes256Key,
  31162. sizeof(aes256Key), attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  31163. "pkcs7encryptedDataAES256CBC_attribs.der"},
  31164. /* test with multiple optional unprotected attributes */
  31165. {data, (word32)sizeof(data), DATA, AES256CBCb, aes256Key,
  31166. sizeof(aes256Key), multiAttribs,
  31167. (sizeof(multiAttribs)/sizeof(PKCS7Attrib)),
  31168. "pkcs7encryptedDataAES256CBC_multi_attribs.der"},
  31169. /* test with contentType set to FirmwarePkgData */
  31170. {data, (word32)sizeof(data), FIRMWARE_PKG_DATA, AES256CBCb, aes256Key,
  31171. sizeof(aes256Key), NULL, 0,
  31172. "pkcs7encryptedDataAES256CBC_firmwarePkgData.der"},
  31173. #endif
  31174. #endif /* !NO_AES && HAVE_AES_CBC */
  31175. };
  31176. encrypted = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  31177. decoded = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  31178. if ((! encrypted) || (! decoded)) {
  31179. ERROR_OUT(MEMORY_E, out);
  31180. }
  31181. testSz = sizeof(testVectors) / sizeof(pkcs7EncryptedVector);
  31182. for (i = 0; i < testSz; i++) {
  31183. pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
  31184. if (pkcs7 == NULL) {
  31185. ERROR_OUT(-12400, out);
  31186. }
  31187. pkcs7->content = (byte*)testVectors[i].content;
  31188. pkcs7->contentSz = testVectors[i].contentSz;
  31189. pkcs7->contentOID = testVectors[i].contentOID;
  31190. pkcs7->encryptOID = testVectors[i].encryptOID;
  31191. pkcs7->encryptionKey = testVectors[i].encryptionKey;
  31192. pkcs7->encryptionKeySz = testVectors[i].encryptionKeySz;
  31193. pkcs7->unprotectedAttribs = testVectors[i].attribs;
  31194. pkcs7->unprotectedAttribsSz = testVectors[i].attribsSz;
  31195. /* encode encryptedData */
  31196. encryptedSz = wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  31197. PKCS7_BUF_SIZE);
  31198. if (encryptedSz <= 0) {
  31199. wc_PKCS7_Free(pkcs7);
  31200. ERROR_OUT(-12401, out);
  31201. }
  31202. /* decode encryptedData */
  31203. #ifndef NO_PKCS7_STREAM
  31204. { /* test reading byte by byte */
  31205. int z;
  31206. for (z = 0; z < encryptedSz; z++) {
  31207. decodedSz = wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted + z, 1,
  31208. decoded, PKCS7_BUF_SIZE);
  31209. if (decodedSz <= 0 && decodedSz != WC_PKCS7_WANT_READ_E) {
  31210. printf("unexpected error %d\n", decodedSz);
  31211. ERROR_OUT(-12402, out);
  31212. }
  31213. }
  31214. /* test decode result */
  31215. if (XMEMCMP(decoded, data, sizeof(data)) != 0) {
  31216. printf("stream read failed\n");
  31217. wc_PKCS7_Free(pkcs7);
  31218. ERROR_OUT(-12403, out);
  31219. }
  31220. }
  31221. #endif
  31222. decodedSz = wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  31223. decoded, PKCS7_BUF_SIZE);
  31224. if (decodedSz <= 0){
  31225. wc_PKCS7_Free(pkcs7);
  31226. ERROR_OUT(-12404, out);
  31227. }
  31228. /* test decode result */
  31229. if (XMEMCMP(decoded, data, sizeof(data)) != 0) {
  31230. wc_PKCS7_Free(pkcs7);
  31231. ERROR_OUT(-12405, out);
  31232. }
  31233. /* verify decoded unprotected attributes */
  31234. if (pkcs7->decodedAttrib != NULL) {
  31235. decodedAttrib = pkcs7->decodedAttrib;
  31236. attribIdx = 1;
  31237. while (decodedAttrib != NULL) {
  31238. /* expected attribute, stored list is reversed */
  31239. expectedAttrib = &(pkcs7->unprotectedAttribs
  31240. [pkcs7->unprotectedAttribsSz - attribIdx]);
  31241. /* verify oid */
  31242. if (XMEMCMP(decodedAttrib->oid, expectedAttrib->oid,
  31243. decodedAttrib->oidSz) != 0) {
  31244. wc_PKCS7_Free(pkcs7);
  31245. ERROR_OUT(-12406, out);
  31246. }
  31247. /* verify value */
  31248. if (XMEMCMP(decodedAttrib->value, expectedAttrib->value,
  31249. decodedAttrib->valueSz) != 0) {
  31250. wc_PKCS7_Free(pkcs7);
  31251. ERROR_OUT(-12407, out);
  31252. }
  31253. decodedAttrib = decodedAttrib->next;
  31254. attribIdx++;
  31255. }
  31256. }
  31257. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  31258. /* output pkcs7 envelopedData for external testing */
  31259. pkcs7File = XFOPEN(testVectors[i].outFileName, "wb");
  31260. if (!pkcs7File) {
  31261. wc_PKCS7_Free(pkcs7);
  31262. ERROR_OUT(-12408, out);
  31263. }
  31264. ret = (int)XFWRITE(encrypted, encryptedSz, 1, pkcs7File);
  31265. XFCLOSE(pkcs7File);
  31266. if (ret > 0)
  31267. ret = 0;
  31268. #endif
  31269. wc_PKCS7_Free(pkcs7);
  31270. }
  31271. out:
  31272. if (encrypted)
  31273. XFREE(encrypted, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  31274. if (decoded)
  31275. XFREE(decoded, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  31276. return ret;
  31277. }
  31278. #endif /* NO_PKCS7_ENCRYPTED_DATA */
  31279. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  31280. typedef struct {
  31281. const byte* content;
  31282. word32 contentSz;
  31283. int contentOID;
  31284. const char* outFileName;
  31285. } pkcs7CompressedVector;
  31286. WOLFSSL_TEST_SUBROUTINE int pkcs7compressed_test(void)
  31287. {
  31288. int ret = 0;
  31289. int i, testSz;
  31290. int compressedSz, decodedSz;
  31291. PKCS7* pkcs7;
  31292. #ifdef WOLFSSL_SMALL_STACK
  31293. byte *compressed;
  31294. byte *decoded;
  31295. #else
  31296. byte compressed[PKCS7_BUF_SIZE];
  31297. byte decoded[PKCS7_BUF_SIZE];
  31298. #endif
  31299. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  31300. XFILE pkcs7File;
  31301. #endif
  31302. WOLFSSL_SMALL_STACK_STATIC const byte data[] = { /* Hello World */
  31303. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  31304. 0x72,0x6c,0x64
  31305. };
  31306. const pkcs7CompressedVector testVectors[] =
  31307. {
  31308. {data, (word32)sizeof(data), DATA,
  31309. "pkcs7compressedData_data_zlib.der"},
  31310. {data, (word32)sizeof(data), FIRMWARE_PKG_DATA,
  31311. "pkcs7compressedData_firmwarePkgData_zlib.der"},
  31312. };
  31313. #ifdef WOLFSSL_SMALL_STACK
  31314. compressed = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  31315. decoded = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  31316. if ((! compressed) || (! decoded)) {
  31317. ERROR_OUT(MEMORY_E, out);
  31318. }
  31319. #endif
  31320. testSz = sizeof(testVectors) / sizeof(pkcs7CompressedVector);
  31321. for (i = 0; i < testSz; i++) {
  31322. pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
  31323. if (pkcs7 == NULL) {
  31324. ERROR_OUT(-12500, out);
  31325. }
  31326. pkcs7->content = (byte*)testVectors[i].content;
  31327. pkcs7->contentSz = testVectors[i].contentSz;
  31328. pkcs7->contentOID = testVectors[i].contentOID;
  31329. /* encode compressedData */
  31330. compressedSz = wc_PKCS7_EncodeCompressedData(pkcs7, compressed,
  31331. PKCS7_BUF_SIZE);
  31332. if (compressedSz <= 0) {
  31333. wc_PKCS7_Free(pkcs7);
  31334. ERROR_OUT(-12501, out);
  31335. }
  31336. /* decode compressedData */
  31337. decodedSz = wc_PKCS7_DecodeCompressedData(pkcs7, compressed,
  31338. compressedSz, decoded,
  31339. PKCS7_BUF_SIZE);
  31340. if (decodedSz <= 0){
  31341. wc_PKCS7_Free(pkcs7);
  31342. ERROR_OUT(-12502, out);
  31343. }
  31344. /* test decode result */
  31345. if (XMEMCMP(decoded, testVectors[i].content,
  31346. testVectors[i].contentSz) != 0) {
  31347. wc_PKCS7_Free(pkcs7);
  31348. ERROR_OUT(-12503, out);
  31349. }
  31350. /* make sure content type is the same */
  31351. if (testVectors[i].contentOID != pkcs7->contentOID) {
  31352. ERROR_OUT(-12504, out);
  31353. }
  31354. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  31355. /* output pkcs7 compressedData for external testing */
  31356. pkcs7File = XFOPEN(testVectors[i].outFileName, "wb");
  31357. if (!pkcs7File) {
  31358. wc_PKCS7_Free(pkcs7);
  31359. ERROR_OUT(-12505, out);
  31360. }
  31361. ret = (int)XFWRITE(compressed, compressedSz, 1, pkcs7File);
  31362. XFCLOSE(pkcs7File);
  31363. if (ret > 0)
  31364. ret = 0;
  31365. #endif
  31366. wc_PKCS7_Free(pkcs7);
  31367. }
  31368. out:
  31369. #ifdef WOLFSSL_SMALL_STACK
  31370. if (compressed)
  31371. XFREE(compressed, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  31372. if (decoded)
  31373. XFREE(decoded, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  31374. #endif
  31375. return ret;
  31376. } /* pkcs7compressed_test() */
  31377. #undef PKCS7_BUF_SIZE
  31378. #endif /* HAVE_LIBZ */
  31379. typedef struct {
  31380. const byte* content;
  31381. word32 contentSz;
  31382. int hashOID;
  31383. int signOID;
  31384. byte* privateKey;
  31385. word32 privateKeySz;
  31386. byte* cert;
  31387. size_t certSz;
  31388. byte* caCert;
  31389. size_t caCertSz;
  31390. PKCS7Attrib* signedAttribs;
  31391. word32 signedAttribsSz;
  31392. const char* outFileName;
  31393. int contentOID;
  31394. byte* contentType;
  31395. word32 contentTypeSz;
  31396. int sidType;
  31397. int encryptOID; /* for single-shot encrypt alg OID */
  31398. int encCompFlag; /* for single-shot. 1 = enc, 2 = comp, 3 = both*/
  31399. byte* encryptKey; /* for single-shot, encryptedData */
  31400. word32 encryptKeySz; /* for single-shot, encryptedData */
  31401. PKCS7Attrib* unprotectedAttribs; /* for single-shot, encryptedData */
  31402. word32 unprotectedAttribsSz; /* for single-shot, encryptedData */
  31403. word16 detachedSignature; /* generate detached signature (0:1) */
  31404. } pkcs7SignedVector;
  31405. static int pkcs7signed_run_vectors(
  31406. byte* rsaClientCertBuf, word32 rsaClientCertBufSz,
  31407. byte* rsaClientPrivKeyBuf, word32 rsaClientPrivKeyBufSz,
  31408. byte* rsaServerCertBuf, word32 rsaServerCertBufSz,
  31409. byte* rsaServerPrivKeyBuf, word32 rsaServerPrivKeyBufSz,
  31410. byte* rsaCaCertBuf, word32 rsaCaCertBufSz,
  31411. byte* rsaCaPrivKeyBuf, word32 rsaCaPrivKeyBufSz,
  31412. byte* eccClientCertBuf, word32 eccClientCertBufSz,
  31413. byte* eccClientPrivKeyBuf, word32 eccClientPrivKeyBufSz)
  31414. {
  31415. int ret, testSz = 0, i;
  31416. int encodedSz;
  31417. byte* out = NULL;
  31418. word32 outSz;
  31419. WC_RNG rng;
  31420. PKCS7* pkcs7 = NULL;
  31421. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  31422. XFILE file;
  31423. #endif
  31424. WOLFSSL_SMALL_STACK_STATIC const byte data[] = { /* Hello World */
  31425. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  31426. 0x72,0x6c,0x64
  31427. };
  31428. static byte transIdOid[] =
  31429. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  31430. 0x09, 0x07 };
  31431. static byte messageTypeOid[] =
  31432. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  31433. 0x09, 0x02 };
  31434. static byte senderNonceOid[] =
  31435. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  31436. 0x09, 0x05 };
  31437. #ifndef NO_SHA
  31438. static byte transId[(WC_SHA_DIGEST_SIZE + 1) * 2 + 1];
  31439. #else
  31440. static byte transId[(WC_SHA256_DIGEST_SIZE + 1) * 2 + 1];
  31441. #endif
  31442. static byte messageType[] = { 0x13, 2, '1', '9' };
  31443. static byte senderNonce[PKCS7_NONCE_SZ + 2];
  31444. static PKCS7Attrib attribs[] =
  31445. {
  31446. { transIdOid, sizeof(transIdOid), transId,
  31447. sizeof(transId) - 1 }, /* take off the null */
  31448. { messageTypeOid, sizeof(messageTypeOid), messageType,
  31449. sizeof(messageType) },
  31450. { senderNonceOid, sizeof(senderNonceOid), senderNonce,
  31451. sizeof(senderNonce) }
  31452. };
  31453. /* for testing custom contentType, FirmwarePkgData */
  31454. static byte customContentType[] = { 0x06, 0x0B, 0x2A, 0x86,
  31455. 0x48, 0x86, 0xF7, 0x0D,
  31456. 0x01, 0x09, 0x10, 0x01, 0x10 };
  31457. #define MAX_TESTVECTORS_LEN 20
  31458. #define ADD_PKCS7SIGNEDVECTOR(...) { \
  31459. pkcs7SignedVector _this_vector = { __VA_ARGS__ }; \
  31460. if (testSz == MAX_TESTVECTORS_LEN) { \
  31461. ret = -12534; \
  31462. goto out; \
  31463. } \
  31464. XMEMCPY(&testVectors[testSz++], &_this_vector, sizeof _this_vector);\
  31465. }
  31466. pkcs7SignedVector *testVectors = NULL;
  31467. XMEMSET(&rng, 0, sizeof(rng));
  31468. testVectors = (pkcs7SignedVector *)XMALLOC(MAX_TESTVECTORS_LEN * sizeof(*testVectors),
  31469. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  31470. if (testVectors == NULL) {
  31471. ret = -12567;
  31472. goto out;
  31473. }
  31474. {
  31475. #ifndef NO_RSA
  31476. #ifndef NO_SHA
  31477. /* RSA with SHA */
  31478. ADD_PKCS7SIGNEDVECTOR(
  31479. data, (word32)sizeof(data), SHAh, RSAk, rsaClientPrivKeyBuf,
  31480. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  31481. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  31482. "pkcs7signedData_RSA_SHA.der", 0, NULL, 0, 0, 0, 0, NULL, 0, NULL,
  31483. 0, 0);
  31484. /* RSA with SHA, no signed attributes */
  31485. ADD_PKCS7SIGNEDVECTOR(
  31486. data, (word32)sizeof(data), SHAh, RSAk, rsaClientPrivKeyBuf,
  31487. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz,
  31488. NULL, 0, NULL, 0,
  31489. "pkcs7signedData_RSA_SHA_noattr.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  31490. NULL, 0, 0);
  31491. #endif
  31492. #ifdef WOLFSSL_SHA224
  31493. /* RSA with SHA224 */
  31494. ADD_PKCS7SIGNEDVECTOR(
  31495. data, (word32)sizeof(data), SHA224h, RSAk, rsaClientPrivKeyBuf,
  31496. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  31497. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  31498. "pkcs7signedData_RSA_SHA224.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  31499. NULL, 0, 0);
  31500. #endif
  31501. #ifndef NO_SHA256
  31502. /* RSA with SHA256 */
  31503. ADD_PKCS7SIGNEDVECTOR(
  31504. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  31505. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  31506. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  31507. "pkcs7signedData_RSA_SHA256.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  31508. NULL, 0, 0);
  31509. /* RSA with SHA256, detached signature */
  31510. ADD_PKCS7SIGNEDVECTOR(
  31511. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  31512. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  31513. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  31514. "pkcs7signedData_RSA_SHA256_detachedSig.der", 0, NULL, 0, 0, 0, 0,
  31515. NULL, 0, NULL, 0, 1);
  31516. /* RSA with SHA256 and SubjectKeyIdentifier in SignerIdentifier */
  31517. ADD_PKCS7SIGNEDVECTOR(
  31518. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  31519. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  31520. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  31521. "pkcs7signedData_RSA_SHA256_SKID.der", 0, NULL, 0, CMS_SKID, 0, 0,
  31522. NULL, 0, NULL, 0, 0);
  31523. /* RSA with SHA256 and custom contentType */
  31524. ADD_PKCS7SIGNEDVECTOR(
  31525. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  31526. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  31527. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  31528. "pkcs7signedData_RSA_SHA256_custom_contentType.der", 0,
  31529. customContentType, sizeof(customContentType), 0, 0, 0, NULL, 0,
  31530. NULL, 0, 0);
  31531. /* RSA with SHA256 and FirmwarePkgData contentType */
  31532. ADD_PKCS7SIGNEDVECTOR(
  31533. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  31534. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  31535. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  31536. "pkcs7signedData_RSA_SHA256_firmwarePkgData.der",
  31537. FIRMWARE_PKG_DATA, NULL, 0, 0, 0, 0, NULL, 0, NULL, 0, 0);
  31538. /* RSA with SHA256 using server cert and ca cert */
  31539. ADD_PKCS7SIGNEDVECTOR(
  31540. data, (word32)sizeof(data), SHA256h, RSAk, rsaServerPrivKeyBuf,
  31541. rsaServerPrivKeyBufSz, rsaServerCertBuf, rsaServerCertBufSz,
  31542. rsaCaCertBuf, rsaCaCertBufSz,
  31543. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  31544. "pkcs7signedData_RSA_SHA256_with_ca_cert.der", 0, NULL, 0, 0, 0, 0,
  31545. NULL, 0, NULL, 0, 0);
  31546. #endif
  31547. #if defined(WOLFSSL_SHA384)
  31548. /* RSA with SHA384 */
  31549. ADD_PKCS7SIGNEDVECTOR(
  31550. data, (word32)sizeof(data), SHA384h, RSAk, rsaClientPrivKeyBuf,
  31551. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  31552. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  31553. "pkcs7signedData_RSA_SHA384.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  31554. NULL, 0, 0);
  31555. #endif
  31556. #if defined(WOLFSSL_SHA512)
  31557. /* RSA with SHA512 */
  31558. ADD_PKCS7SIGNEDVECTOR(
  31559. data, (word32)sizeof(data), SHA512h, RSAk, rsaClientPrivKeyBuf,
  31560. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  31561. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  31562. "pkcs7signedData_RSA_SHA512.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  31563. NULL, 0, 0);
  31564. #endif
  31565. #endif /* NO_RSA */
  31566. #ifdef HAVE_ECC
  31567. #ifndef NO_SHA
  31568. /* ECDSA with SHA */
  31569. ADD_PKCS7SIGNEDVECTOR(
  31570. data, (word32)sizeof(data), SHAh, ECDSAk, eccClientPrivKeyBuf,
  31571. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  31572. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  31573. "pkcs7signedData_ECDSA_SHA.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  31574. NULL, 0, 0);
  31575. /* ECDSA with SHA, no signed attributes */
  31576. ADD_PKCS7SIGNEDVECTOR(
  31577. data, (word32)sizeof(data), SHAh, ECDSAk, eccClientPrivKeyBuf,
  31578. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz,
  31579. NULL, 0, NULL, 0,
  31580. "pkcs7signedData_ECDSA_SHA_noattr.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  31581. NULL, 0, 0);
  31582. #endif
  31583. #ifdef WOLFSSL_SHA224
  31584. /* ECDSA with SHA224 */
  31585. ADD_PKCS7SIGNEDVECTOR(
  31586. data, (word32)sizeof(data), SHA224h, ECDSAk, eccClientPrivKeyBuf,
  31587. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  31588. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  31589. "pkcs7signedData_ECDSA_SHA224.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  31590. NULL, 0, 0);
  31591. #endif
  31592. #ifndef NO_SHA256
  31593. /* ECDSA with SHA256 */
  31594. ADD_PKCS7SIGNEDVECTOR(
  31595. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  31596. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  31597. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  31598. "pkcs7signedData_ECDSA_SHA256.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  31599. NULL, 0, 0);
  31600. /* ECDSA with SHA256 and SubjectKeyIdentifier in SigherIdentifier */
  31601. ADD_PKCS7SIGNEDVECTOR(
  31602. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  31603. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  31604. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  31605. "pkcs7signedData_ECDSA_SHA256_SKID.der", 0, NULL, 0, CMS_SKID, 0, 0,
  31606. NULL, 0, NULL, 0, 0);
  31607. /* ECDSA with SHA256 and custom contentType */
  31608. ADD_PKCS7SIGNEDVECTOR(
  31609. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  31610. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  31611. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  31612. "pkcs7signedData_ECDSA_SHA256_custom_contentType.der", 0,
  31613. customContentType, sizeof(customContentType), 0, 0, 0, NULL, 0,
  31614. NULL, 0, 0);
  31615. /* ECDSA with SHA256 and FirmwarePkgData contentType */
  31616. ADD_PKCS7SIGNEDVECTOR(
  31617. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  31618. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  31619. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  31620. "pkcs7signedData_ECDSA_SHA256_firmwarePkgData.der",
  31621. FIRMWARE_PKG_DATA, NULL, 0, 0, 0, 0, NULL, 0, NULL, 0, 0);
  31622. #endif
  31623. #ifdef WOLFSSL_SHA384
  31624. /* ECDSA with SHA384 */
  31625. ADD_PKCS7SIGNEDVECTOR(
  31626. data, (word32)sizeof(data), SHA384h, ECDSAk, eccClientPrivKeyBuf,
  31627. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  31628. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  31629. "pkcs7signedData_ECDSA_SHA384.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  31630. NULL, 0, 0);
  31631. #endif
  31632. #ifdef WOLFSSL_SHA512
  31633. /* ECDSA with SHA512 */
  31634. ADD_PKCS7SIGNEDVECTOR(
  31635. data, (word32)sizeof(data), SHA512h, ECDSAk, eccClientPrivKeyBuf,
  31636. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  31637. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  31638. "pkcs7signedData_ECDSA_SHA512.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  31639. NULL, 0, 0);
  31640. #endif
  31641. #endif /* HAVE_ECC */
  31642. };
  31643. #undef MAX_TESTVECTORS_LEN
  31644. #undef ADD_PKCS7SIGNEDVECTOR
  31645. outSz = FOURK_BUF;
  31646. out = (byte*)XMALLOC(outSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  31647. if (out == NULL)
  31648. ERROR_OUT(-12510, out);
  31649. XMEMSET(out, 0, outSz);
  31650. ret = wc_PKCS7_PadData((byte*)data, sizeof(data), out, outSz, 16);
  31651. if (ret < 0)
  31652. ERROR_OUT(-12511, out);
  31653. #ifndef HAVE_FIPS
  31654. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  31655. #else
  31656. ret = wc_InitRng(&rng);
  31657. #endif
  31658. if (ret != 0)
  31659. ERROR_OUT(-12512, out);
  31660. for (i = 0; i < testSz; i++) {
  31661. if (pkcs7)
  31662. wc_PKCS7_Free(pkcs7);
  31663. pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
  31664. if (pkcs7 == NULL)
  31665. ERROR_OUT(-12513, out);
  31666. ret = wc_PKCS7_InitWithCert(pkcs7, testVectors[i].cert,
  31667. (word32)testVectors[i].certSz);
  31668. if (ret != 0)
  31669. ERROR_OUT(-12514, out);
  31670. /* load CA certificate, if present */
  31671. if (testVectors[i].caCert != NULL) {
  31672. ret = wc_PKCS7_AddCertificate(pkcs7, testVectors[i].caCert,
  31673. (word32)testVectors[i].caCertSz);
  31674. if (ret != 0)
  31675. ERROR_OUT(-12515, out);
  31676. }
  31677. pkcs7->rng = &rng;
  31678. pkcs7->content = (byte*)testVectors[i].content;
  31679. pkcs7->contentSz = testVectors[i].contentSz;
  31680. pkcs7->contentOID = testVectors[i].contentOID;
  31681. pkcs7->hashOID = testVectors[i].hashOID;
  31682. pkcs7->encryptOID = testVectors[i].signOID;
  31683. pkcs7->privateKey = testVectors[i].privateKey;
  31684. pkcs7->privateKeySz = testVectors[i].privateKeySz;
  31685. pkcs7->signedAttribs = testVectors[i].signedAttribs;
  31686. pkcs7->signedAttribsSz = testVectors[i].signedAttribsSz;
  31687. /* optional custom contentType, default is DATA,
  31688. overrides contentOID if set */
  31689. if (testVectors[i].contentType != NULL) {
  31690. ret = wc_PKCS7_SetContentType(pkcs7, testVectors[i].contentType,
  31691. testVectors[i].contentTypeSz);
  31692. if (ret != 0)
  31693. ERROR_OUT(-12516, out);
  31694. }
  31695. /* set SignerIdentifier to use SubjectKeyIdentifier if desired,
  31696. default is IssuerAndSerialNumber */
  31697. if (testVectors[i].sidType == CMS_SKID) {
  31698. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID);
  31699. if (ret != 0)
  31700. ERROR_OUT(-12517, out);
  31701. }
  31702. /* generate senderNonce */
  31703. {
  31704. senderNonce[0] = 0x04;
  31705. senderNonce[1] = PKCS7_NONCE_SZ;
  31706. ret = wc_RNG_GenerateBlock(&rng, &senderNonce[2], PKCS7_NONCE_SZ);
  31707. if (ret != 0)
  31708. ERROR_OUT(-12518, out);
  31709. }
  31710. /* generate transactionID (used with SCEP) */
  31711. {
  31712. #ifndef NO_SHA
  31713. wc_Sha sha;
  31714. byte digest[WC_SHA_DIGEST_SIZE];
  31715. #else
  31716. wc_Sha256 sha;
  31717. byte digest[WC_SHA256_DIGEST_SIZE];
  31718. #endif
  31719. int j,k;
  31720. transId[0] = 0x13;
  31721. transId[1] = sizeof(digest) * 2;
  31722. #ifndef NO_SHA
  31723. ret = wc_InitSha_ex(&sha, HEAP_HINT, devId);
  31724. if (ret != 0)
  31725. ERROR_OUT(-12519, out);
  31726. wc_ShaUpdate(&sha, pkcs7->publicKey, pkcs7->publicKeySz);
  31727. wc_ShaFinal(&sha, digest);
  31728. wc_ShaFree(&sha);
  31729. #else
  31730. ret = wc_InitSha256_ex(&sha, HEAP_HINT, devId);
  31731. if (ret != 0)
  31732. ERROR_OUT(-12520, out);
  31733. wc_Sha256Update(&sha, pkcs7->publicKey, pkcs7->publicKeySz);
  31734. wc_Sha256Final(&sha, digest);
  31735. wc_Sha256Free(&sha);
  31736. #endif
  31737. for (j = 0, k = 2; j < (int)sizeof(digest); j++, k += 2) {
  31738. #if defined(WOLF_C89)
  31739. XSPRINTF((char*)&transId[k], "%02x", digest[j]);
  31740. #else
  31741. (void)XSNPRINTF((char*)&transId[k], 3, "%02x", digest[j]);
  31742. #endif
  31743. }
  31744. }
  31745. /* enable detached signature generation, if set */
  31746. if (testVectors[i].detachedSignature == 1) {
  31747. ret = wc_PKCS7_SetDetached(pkcs7, 1);
  31748. if (ret != 0)
  31749. ERROR_OUT(-12521, out);
  31750. }
  31751. encodedSz = wc_PKCS7_EncodeSignedData(pkcs7, out, outSz);
  31752. if (encodedSz < 0)
  31753. ERROR_OUT(-12522, out);
  31754. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  31755. /* write PKCS#7 to output file for more testing */
  31756. file = XFOPEN(testVectors[i].outFileName, "wb");
  31757. if (!file) {
  31758. ERROR_OUT(-12523, out);
  31759. }
  31760. ret = (int)XFWRITE(out, 1, encodedSz, file);
  31761. XFCLOSE(file);
  31762. if (ret != (int)encodedSz)
  31763. ERROR_OUT(-12524, out);
  31764. #endif /* PKCS7_OUTPUT_TEST_BUNDLES */
  31765. wc_PKCS7_Free(pkcs7);
  31766. pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
  31767. if (pkcs7 == NULL)
  31768. ERROR_OUT(-12525, out);
  31769. wc_PKCS7_InitWithCert(pkcs7, NULL, 0);
  31770. if (testVectors[i].detachedSignature == 1) {
  31771. /* set content for verifying detached signatures */
  31772. pkcs7->content = (byte*)testVectors[i].content;
  31773. pkcs7->contentSz = testVectors[i].contentSz;
  31774. }
  31775. ret = wc_PKCS7_VerifySignedData(pkcs7, out, outSz);
  31776. if (ret < 0)
  31777. ERROR_OUT(-12526, out);
  31778. /* verify contentType extracted successfully for custom content types */
  31779. if (testVectors[i].contentTypeSz > 0) {
  31780. if (pkcs7->contentTypeSz != testVectors[i].contentTypeSz) {
  31781. ERROR_OUT(-12527, out);
  31782. } else if (XMEMCMP(pkcs7->contentType, testVectors[i].contentType,
  31783. pkcs7->contentTypeSz) != 0) {
  31784. ERROR_OUT(-12528, out);
  31785. }
  31786. }
  31787. if (pkcs7->singleCert == NULL || pkcs7->singleCertSz == 0)
  31788. ERROR_OUT(-12529, out);
  31789. {
  31790. /* check getting signed attributes */
  31791. #ifndef NO_SHA
  31792. byte buf[(WC_SHA_DIGEST_SIZE + 1) * 2 + 1];
  31793. #else
  31794. byte buf[(WC_SHA256_DIGEST_SIZE + 1) * 2 + 1];
  31795. #endif
  31796. byte* oidPt = transIdOid + 2; /* skip object id tag and size */
  31797. int oidSz = (int)sizeof(transIdOid) - 2;
  31798. int bufSz = 0;
  31799. if (testVectors[i].signedAttribs != NULL &&
  31800. wc_PKCS7_GetAttributeValue(pkcs7, oidPt, oidSz,
  31801. NULL, (word32*)&bufSz) != LENGTH_ONLY_E)
  31802. ERROR_OUT(-12530, out);
  31803. if (bufSz > (int)sizeof(buf))
  31804. ERROR_OUT(-12531, out);
  31805. bufSz = wc_PKCS7_GetAttributeValue(pkcs7, oidPt, oidSz,
  31806. buf, (word32*)&bufSz);
  31807. if ((testVectors[i].signedAttribs != NULL && bufSz < 0) ||
  31808. (testVectors[i].signedAttribs == NULL && bufSz > 0))
  31809. ERROR_OUT(-12532, out);
  31810. }
  31811. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  31812. file = XFOPEN("./pkcs7cert.der", "wb");
  31813. if (!file)
  31814. ERROR_OUT(-12533, out);
  31815. ret = (int)XFWRITE(pkcs7->singleCert, 1, pkcs7->singleCertSz, file);
  31816. XFCLOSE(file);
  31817. #endif /* PKCS7_OUTPUT_TEST_BUNDLES */
  31818. }
  31819. out:
  31820. if (pkcs7 != NULL)
  31821. wc_PKCS7_Free(pkcs7);
  31822. if (out != NULL)
  31823. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  31824. if (testVectors != NULL)
  31825. XFREE(testVectors, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  31826. wc_FreeRng(&rng);
  31827. if (ret > 0)
  31828. return 0;
  31829. (void)rsaClientCertBuf;
  31830. (void)rsaClientCertBufSz;
  31831. (void)rsaClientPrivKeyBuf;
  31832. (void)rsaClientPrivKeyBufSz;
  31833. (void)rsaServerCertBuf;
  31834. (void)rsaServerCertBufSz;
  31835. (void)rsaServerPrivKeyBuf;
  31836. (void)rsaServerPrivKeyBufSz;
  31837. (void)rsaCaCertBuf;
  31838. (void)rsaCaCertBufSz;
  31839. (void)rsaCaPrivKeyBuf;
  31840. (void)rsaCaPrivKeyBufSz;
  31841. (void)eccClientCertBuf;
  31842. (void)eccClientCertBufSz;
  31843. (void)eccClientPrivKeyBuf;
  31844. (void)eccClientPrivKeyBufSz;
  31845. return ret;
  31846. }
  31847. static int pkcs7signed_run_SingleShotVectors(
  31848. byte* rsaClientCertBuf, word32 rsaClientCertBufSz,
  31849. byte* rsaClientPrivKeyBuf, word32 rsaClientPrivKeyBufSz,
  31850. byte* rsaServerCertBuf, word32 rsaServerCertBufSz,
  31851. byte* rsaServerPrivKeyBuf, word32 rsaServerPrivKeyBufSz,
  31852. byte* rsaCaCertBuf, word32 rsaCaCertBufSz,
  31853. byte* rsaCaPrivKeyBuf, word32 rsaCaPrivKeyBufSz,
  31854. byte* eccClientCertBuf, word32 eccClientCertBufSz,
  31855. byte* eccClientPrivKeyBuf, word32 eccClientPrivKeyBufSz)
  31856. {
  31857. int ret, testSz = 0, i;
  31858. int encodedSz;
  31859. byte* out = NULL;
  31860. word32 outSz;
  31861. WC_RNG rng;
  31862. PKCS7* pkcs7 = NULL;
  31863. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  31864. XFILE file;
  31865. #endif
  31866. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA) && \
  31867. !defined(NO_PKCS7_ENCRYPTED_DATA)
  31868. byte* encryptedTmp = NULL;
  31869. int encryptedTmpSz;
  31870. #endif
  31871. WOLFSSL_SMALL_STACK_STATIC const byte data[] = { /* Hello World */
  31872. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  31873. 0x72,0x6c,0x64
  31874. };
  31875. #if !defined(NO_PKCS7_ENCRYPTED_DATA) && \
  31876. defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_256)
  31877. static byte aes256Key[] = {
  31878. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  31879. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  31880. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  31881. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  31882. };
  31883. #endif
  31884. static byte messageTypeOid[] =
  31885. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  31886. 0x09, 0x02 };
  31887. static byte messageType[] = { 0x13, 2, '1', '9' };
  31888. PKCS7Attrib attribs[] =
  31889. {
  31890. { messageTypeOid, sizeof(messageTypeOid), messageType,
  31891. sizeof(messageType) },
  31892. };
  31893. #define MAX_TESTVECTORS_LEN 19
  31894. #define ADD_PKCS7SIGNEDVECTOR(...) { \
  31895. pkcs7SignedVector _this_vector = { __VA_ARGS__ }; \
  31896. if (testSz == MAX_TESTVECTORS_LEN) { \
  31897. ret = -12568; \
  31898. goto out; \
  31899. } \
  31900. XMEMCPY(&testVectors[testSz++], &_this_vector, sizeof _this_vector);\
  31901. }
  31902. pkcs7SignedVector *testVectors = NULL;
  31903. XMEMSET(&rng, 0, sizeof(rng));
  31904. testVectors = (pkcs7SignedVector *)XMALLOC(MAX_TESTVECTORS_LEN * sizeof(*testVectors),
  31905. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  31906. if (testVectors == NULL) {
  31907. ret = -12567;
  31908. goto out;
  31909. }
  31910. {
  31911. #ifndef NO_RSA
  31912. #ifndef NO_SHA256
  31913. /* Signed FirmwarePkgData, RSA, SHA256, no attribs */
  31914. ADD_PKCS7SIGNEDVECTOR(
  31915. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  31916. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  31917. NULL, 0,
  31918. "pkcs7signedFirmwarePkgData_RSA_SHA256_noattr.der", 0, NULL, 0, 0,
  31919. 0, 0, NULL, 0, NULL, 0, 0);
  31920. /* Signed FirmwarePkgData, RSA, SHA256, attrs */
  31921. ADD_PKCS7SIGNEDVECTOR(
  31922. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  31923. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  31924. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  31925. "pkcs7signedFirmwarePkgData_RSA_SHA256.der", 0, NULL, 0, 0, 0, 0,
  31926. NULL, 0, NULL, 0, 0);
  31927. /* Signed FirmwarePkgData, RSA, SHA256, SubjectKeyIdentifier, attrs */
  31928. ADD_PKCS7SIGNEDVECTOR(
  31929. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  31930. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  31931. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  31932. "pkcs7signedFirmwarePkgData_RSA_SHA256_SKID.der", 0, NULL,
  31933. 0, CMS_SKID, 0, 0, NULL, 0, NULL, 0, 0);
  31934. /* Signed FirmwraePkgData, RSA, SHA256, server cert and ca cert, attr */
  31935. ADD_PKCS7SIGNEDVECTOR(
  31936. data, (word32)sizeof(data), SHA256h, RSAk, rsaServerPrivKeyBuf,
  31937. rsaServerPrivKeyBufSz, rsaServerCertBuf, rsaServerCertBufSz,
  31938. rsaCaCertBuf, rsaCaCertBufSz,
  31939. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  31940. "pkcs7signedFirmwarePkgData_RSA_SHA256_with_ca_cert.der", 0, NULL,
  31941. 0, 0, 0, 0, NULL, 0, NULL, 0, 0);
  31942. #if !defined(NO_PKCS7_ENCRYPTED_DATA) && \
  31943. defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_256)
  31944. /* Signed Encrypted FirmwarePkgData, RSA, SHA256, no attribs */
  31945. ADD_PKCS7SIGNEDVECTOR(
  31946. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  31947. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  31948. NULL, 0,
  31949. "pkcs7signedEncryptedFirmwarePkgData_RSA_SHA256_noattr.der", 0,
  31950. NULL, 0, 0, AES256CBCb, 1, aes256Key, sizeof(aes256Key), NULL, 0, 0);
  31951. /* Signed Encrypted FirmwarePkgData, RSA, SHA256, attribs */
  31952. ADD_PKCS7SIGNEDVECTOR(
  31953. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  31954. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  31955. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  31956. "pkcs7signedEncryptedFirmwarePkgData_RSA_SHA256.der", 0,
  31957. NULL, 0, 0, AES256CBCb, 1, aes256Key, sizeof(aes256Key),
  31958. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)), 0);
  31959. #endif /* WOLFSSL_AES_256 && !NO_PKCS7_ENCRYPTED_DATA */
  31960. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  31961. /* Signed Compressed FirmwarePkgData, RSA, SHA256, no attribs */
  31962. ADD_PKCS7SIGNEDVECTOR(
  31963. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  31964. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  31965. NULL, 0,
  31966. "pkcs7signedCompressedFirmwarePkgData_RSA_SHA256_noattr.der", 0,
  31967. NULL, 0, 0, 0, 2, NULL, 0, NULL, 0, 0);
  31968. /* Signed Compressed FirmwarePkgData, RSA, SHA256, attribs */
  31969. ADD_PKCS7SIGNEDVECTOR(
  31970. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  31971. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  31972. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  31973. "pkcs7signedCompressedFirmwarePkgData_RSA_SHA256.der", 0,
  31974. NULL, 0, 0, 0, 2, NULL, 0, NULL, 0, 0);
  31975. #ifndef NO_PKCS7_ENCRYPTED_DATA
  31976. /* Signed Encrypted Compressed FirmwarePkgData, RSA, SHA256,
  31977. no attribs */
  31978. ADD_PKCS7SIGNEDVECTOR(
  31979. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  31980. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  31981. NULL, 0,
  31982. "pkcs7signedEncryptedCompressedFirmwarePkgData_RSA_SHA256_noattr.der",
  31983. 0, NULL, 0, 0, AES256CBCb, 3, aes256Key, sizeof(aes256Key), NULL,
  31984. 0, 0);
  31985. /* Signed Encrypted Compressed FirmwarePkgData, RSA, SHA256,
  31986. attribs */
  31987. ADD_PKCS7SIGNEDVECTOR(
  31988. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  31989. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  31990. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  31991. "pkcs7signedEncryptedCompressedFirmwarePkgData_RSA_SHA256.der",
  31992. 0, NULL, 0, 0, AES256CBCb, 3, aes256Key, sizeof(aes256Key),
  31993. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)), 0);
  31994. #endif /* !NO_PKCS7_ENCRYPTED_DATA */
  31995. #endif /* HAVE_LIBZ && !NO_PKCS7_COMPRESSED_DATA */
  31996. #endif /* NO_SHA256 */
  31997. #endif /* NO_RSA */
  31998. #ifdef HAVE_ECC
  31999. #ifndef NO_SHA256
  32000. /* Signed FirmwarePkgData, ECDSA, SHA256, no attribs */
  32001. ADD_PKCS7SIGNEDVECTOR(
  32002. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  32003. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  32004. NULL, 0,
  32005. "pkcs7signedFirmwarePkgData_ECDSA_SHA256_noattr.der", 0, NULL,
  32006. 0, 0, 0, 0, NULL, 0, NULL, 0, 0);
  32007. /* Signed FirmwarePkgData, ECDSA, SHA256, attribs */
  32008. ADD_PKCS7SIGNEDVECTOR(
  32009. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  32010. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  32011. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  32012. "pkcs7signedFirmwarePkgData_ECDSA_SHA256.der", 0, NULL,
  32013. 0, 0, 0, 0, NULL, 0, NULL, 0, 0);
  32014. /* Signed FirmwarePkgData, ECDSA, SHA256, SubjectKeyIdentifier, attr */
  32015. ADD_PKCS7SIGNEDVECTOR(
  32016. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  32017. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  32018. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  32019. "pkcs7signedFirmwarePkgData_ECDSA_SHA256_SKID.der", 0, NULL,
  32020. 0, CMS_SKID, 0, 0, NULL, 0, NULL, 0, 0);
  32021. #if !defined(NO_PKCS7_ENCRYPTED_DATA) && \
  32022. defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_256)
  32023. /* Signed Encrypted FirmwarePkgData, ECDSA, SHA256, no attribs */
  32024. ADD_PKCS7SIGNEDVECTOR(
  32025. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  32026. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  32027. NULL, 0,
  32028. "pkcs7signedEncryptedFirmwarePkgData_ECDSA_SHA256_noattr.der", 0, NULL,
  32029. 0, 0, AES256CBCb, 1, aes256Key, sizeof(aes256Key), NULL, 0, 0);
  32030. /* Signed Encrypted FirmwarePkgData, ECDSA, SHA256, attribs */
  32031. ADD_PKCS7SIGNEDVECTOR(
  32032. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  32033. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  32034. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  32035. "pkcs7signedEncryptedFirmwarePkgData_ECDSA_SHA256.der", 0, NULL,
  32036. 0, 0, AES256CBCb, 1, aes256Key, sizeof(aes256Key),
  32037. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)), 0);
  32038. #endif /* WOLFSSL_AES_256 && !NO_PKCS7_ENCRYPTED_DATA */
  32039. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  32040. /* Signed Compressed FirmwarePkgData, ECDSA, SHA256, no attribs */
  32041. ADD_PKCS7SIGNEDVECTOR(
  32042. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  32043. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  32044. NULL, 0,
  32045. "pkcs7signedCompressedFirmwarePkgData_ECDSA_SHA256_noattr.der", 0, NULL,
  32046. 0, 0, 0, 2, NULL, 0, NULL, 0, 0);
  32047. /* Signed Compressed FirmwarePkgData, ECDSA, SHA256, attrib */
  32048. ADD_PKCS7SIGNEDVECTOR(
  32049. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  32050. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  32051. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  32052. "pkcs7signedCompressedFirmwarePkgData_ECDSA_SHA256.der", 0, NULL,
  32053. 0, 0, 0, 2, NULL, 0, NULL, 0, 0);
  32054. #ifndef NO_PKCS7_ENCRYPTED_DATA
  32055. /* Signed Encrypted Compressed FirmwarePkgData, ECDSA, SHA256,
  32056. no attribs */
  32057. ADD_PKCS7SIGNEDVECTOR(
  32058. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  32059. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  32060. NULL, 0,
  32061. "pkcs7signedEncryptedCompressedFirmwarePkgData_ECDSA_SHA256_noattr.der",
  32062. 0, NULL, 0, 0, AES256CBCb, 3, aes256Key, sizeof(aes256Key), NULL,
  32063. 0, 0);
  32064. /* Signed Encrypted Compressed FirmwarePkgData, ECDSA, SHA256,
  32065. attribs */
  32066. ADD_PKCS7SIGNEDVECTOR(
  32067. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  32068. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  32069. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  32070. "pkcs7signedEncryptedCompressedFirmwarePkgData_ECDSA_SHA256.der",
  32071. 0, NULL, 0, 0, AES256CBCb, 3, aes256Key, sizeof(aes256Key),
  32072. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)), 0);
  32073. #endif /* !NO_PKCS7_ENCRYPTED_DATA */
  32074. #endif /* HAVE_LIBZ && !NO_PKCS7_COMPRESSED_DATA */
  32075. #endif /* NO_SHA256 */
  32076. #endif /* HAVE_ECC */
  32077. };
  32078. #undef MAX_TESTVECTORS_LEN
  32079. #undef ADD_PKCS7SIGNEDVECTOR
  32080. outSz = FOURK_BUF;
  32081. out = (byte*)XMALLOC(outSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32082. if (out == NULL)
  32083. ERROR_OUT(-12540, out);
  32084. XMEMSET(out, 0, outSz);
  32085. ret = wc_PKCS7_PadData((byte*)data, sizeof(data), out, outSz, 16);
  32086. if (ret < 0)
  32087. ERROR_OUT(-12541, out);
  32088. #ifndef HAVE_FIPS
  32089. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  32090. #else
  32091. ret = wc_InitRng(&rng);
  32092. #endif
  32093. if (ret != 0)
  32094. ERROR_OUT(-12542, out);
  32095. for (i = 0; i < testSz; i++) {
  32096. if (pkcs7)
  32097. wc_PKCS7_Free(pkcs7);
  32098. pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
  32099. if (pkcs7 == NULL)
  32100. ERROR_OUT(-12543, out);
  32101. ret = wc_PKCS7_InitWithCert(pkcs7, testVectors[i].cert,
  32102. (word32)testVectors[i].certSz);
  32103. if (ret != 0)
  32104. ERROR_OUT(-12544, out);
  32105. /* load CA certificate, if present */
  32106. if (testVectors[i].caCert != NULL) {
  32107. ret = wc_PKCS7_AddCertificate(pkcs7, testVectors[i].caCert,
  32108. (word32)testVectors[i].caCertSz);
  32109. if (ret != 0)
  32110. ERROR_OUT(-12545, out);
  32111. }
  32112. /* set SignerIdentifier to use SubjectKeyIdentifier if desired,
  32113. default is IssuerAndSerialNumber */
  32114. if (testVectors[i].sidType == CMS_SKID) {
  32115. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID);
  32116. if (ret != 0)
  32117. ERROR_OUT(-12546, out);
  32118. }
  32119. if (testVectors[i].encCompFlag == 0) {
  32120. /* encode Signed FirmwarePkgData */
  32121. encodedSz = wc_PKCS7_EncodeSignedFPD(pkcs7,
  32122. testVectors[i].privateKey, testVectors[i].privateKeySz,
  32123. testVectors[i].signOID, testVectors[i].hashOID,
  32124. (byte*)testVectors[i].content, testVectors[i].contentSz,
  32125. testVectors[i].signedAttribs,
  32126. testVectors[i].signedAttribsSz, out, outSz);
  32127. if (encodedSz < 0)
  32128. ERROR_OUT(-12547, out);
  32129. #ifndef NO_PKCS7_ENCRYPTED_DATA
  32130. } else if (testVectors[i].encCompFlag == 1) {
  32131. /* encode Signed Encrypted FirmwarePkgData */
  32132. encodedSz = wc_PKCS7_EncodeSignedEncryptedFPD(pkcs7,
  32133. testVectors[i].encryptKey, testVectors[i].encryptKeySz,
  32134. testVectors[i].privateKey, testVectors[i].privateKeySz,
  32135. testVectors[i].encryptOID, testVectors[i].signOID,
  32136. testVectors[i].hashOID, (byte*)testVectors[i].content,
  32137. testVectors[i].contentSz, testVectors[i].unprotectedAttribs,
  32138. testVectors[i].unprotectedAttribsSz,
  32139. testVectors[i].signedAttribs,
  32140. testVectors[i].signedAttribsSz, out, outSz);
  32141. if (encodedSz <= 0)
  32142. ERROR_OUT(-12548, out);
  32143. #endif
  32144. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  32145. } else if (testVectors[i].encCompFlag == 2) {
  32146. /* encode Signed Compressed FirmwarePkgData */
  32147. encodedSz = wc_PKCS7_EncodeSignedCompressedFPD(pkcs7,
  32148. testVectors[i].privateKey, testVectors[i].privateKeySz,
  32149. testVectors[i].signOID, testVectors[i].hashOID,
  32150. (byte*)testVectors[i].content, testVectors[i].contentSz,
  32151. testVectors[i].signedAttribs,
  32152. testVectors[i].signedAttribsSz, out, outSz);
  32153. if (encodedSz <= 0)
  32154. ERROR_OUT(-12549, out);
  32155. #ifndef NO_PKCS7_ENCRYPTED_DATA
  32156. } else if (testVectors[i].encCompFlag == 3) {
  32157. /* encode Signed Encrypted Compressed FirmwarePkgData */
  32158. encodedSz = wc_PKCS7_EncodeSignedEncryptedCompressedFPD(pkcs7,
  32159. testVectors[i].encryptKey, testVectors[i].encryptKeySz,
  32160. testVectors[i].privateKey, testVectors[i].privateKeySz,
  32161. testVectors[i].encryptOID, testVectors[i].signOID,
  32162. testVectors[i].hashOID, (byte*)testVectors[i].content,
  32163. testVectors[i].contentSz, testVectors[i].unprotectedAttribs,
  32164. testVectors[i].unprotectedAttribsSz,
  32165. testVectors[i].signedAttribs,
  32166. testVectors[i].signedAttribsSz, out, outSz);
  32167. if (encodedSz <= 0)
  32168. ERROR_OUT(-12550, out);
  32169. #endif /* NO_PKCS7_ENCRYPTED_DATA */
  32170. #endif /* HAVE_LIBZ && !NO_PKCS7_COMPRESSED_DATA */
  32171. } else {
  32172. /* unsupported SignedData single-shot combination */
  32173. ERROR_OUT(-12551, out);
  32174. }
  32175. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  32176. /* write PKCS#7 to output file for more testing */
  32177. file = XFOPEN(testVectors[i].outFileName, "wb");
  32178. if (!file)
  32179. ERROR_OUT(-12552, out);
  32180. ret = (int)XFWRITE(out, 1, encodedSz, file);
  32181. XFCLOSE(file);
  32182. file = NULL;
  32183. if (ret != (int)encodedSz)
  32184. ERROR_OUT(-12553, out);
  32185. #endif /* PKCS7_OUTPUT_TEST_BUNDLES */
  32186. wc_PKCS7_Free(pkcs7);
  32187. pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
  32188. if (pkcs7 == NULL)
  32189. ERROR_OUT(-12554, out);
  32190. wc_PKCS7_InitWithCert(pkcs7, NULL, 0);
  32191. ret = wc_PKCS7_VerifySignedData(pkcs7, out, outSz);
  32192. if (ret < 0)
  32193. ERROR_OUT(-12555, out);
  32194. #ifndef NO_PKCS7_STREAM
  32195. {
  32196. word32 z;
  32197. for (z = 0; z < outSz && ret != 0; z++) {
  32198. ret = wc_PKCS7_VerifySignedData(pkcs7, out + z, 1);
  32199. if (ret < 0 && ret != WC_PKCS7_WANT_READ_E) {
  32200. printf("unexpected error %d\n", ret);
  32201. ERROR_OUT(-12556, out);
  32202. }
  32203. }
  32204. }
  32205. #endif
  32206. if (pkcs7->singleCert == NULL || pkcs7->singleCertSz == 0)
  32207. ERROR_OUT(-12557, out);
  32208. if (testVectors[i].encCompFlag == 0) {
  32209. /* verify decoded content matches expected */
  32210. if ((pkcs7->contentSz != testVectors[i].contentSz) ||
  32211. XMEMCMP(pkcs7->content, testVectors[i].content,
  32212. pkcs7->contentSz)) {
  32213. ERROR_OUT(-12558, out);
  32214. }
  32215. }
  32216. #ifndef NO_PKCS7_ENCRYPTED_DATA
  32217. else if (testVectors[i].encCompFlag == 1) {
  32218. /* decrypt inner encryptedData */
  32219. pkcs7->encryptionKey = testVectors[i].encryptKey;
  32220. pkcs7->encryptionKeySz = testVectors[i].encryptKeySz;
  32221. ret = wc_PKCS7_DecodeEncryptedData(pkcs7, pkcs7->content,
  32222. pkcs7->contentSz, out, outSz);
  32223. if (ret < 0)
  32224. ERROR_OUT(-12559, out);
  32225. /* compare decrypted to expected */
  32226. if (((word32)ret != testVectors[i].contentSz) ||
  32227. XMEMCMP(out, testVectors[i].content, ret))
  32228. ERROR_OUT(-12560, out);
  32229. }
  32230. #endif
  32231. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  32232. else if (testVectors[i].encCompFlag == 2) {
  32233. /* decompress inner compressedData */
  32234. ret = wc_PKCS7_DecodeCompressedData(pkcs7, pkcs7->content,
  32235. pkcs7->contentSz, out, outSz);
  32236. if (ret < 0)
  32237. ERROR_OUT(-12561, out);
  32238. /* compare decompressed to expected */
  32239. if (((word32)ret != testVectors[i].contentSz) ||
  32240. XMEMCMP(out, testVectors[i].content, ret))
  32241. ERROR_OUT(-12562, out);
  32242. }
  32243. #ifndef NO_PKCS7_ENCRYPTED_DATA
  32244. else if (testVectors[i].encCompFlag == 3) {
  32245. encryptedTmpSz = FOURK_BUF;
  32246. encryptedTmp = (byte*)XMALLOC(encryptedTmpSz, HEAP_HINT,
  32247. DYNAMIC_TYPE_TMP_BUFFER);
  32248. if (encryptedTmp == NULL)
  32249. ERROR_OUT(-12563, out);
  32250. XMEMSET(encryptedTmp, 0, encryptedTmpSz);
  32251. /* decrypt inner encryptedData */
  32252. pkcs7->encryptionKey = testVectors[i].encryptKey;
  32253. pkcs7->encryptionKeySz = testVectors[i].encryptKeySz;
  32254. encryptedTmpSz = wc_PKCS7_DecodeEncryptedData(pkcs7, pkcs7->content,
  32255. pkcs7->contentSz, encryptedTmp,
  32256. encryptedTmpSz);
  32257. if (encryptedTmpSz < 0 || pkcs7->contentOID != COMPRESSED_DATA)
  32258. ERROR_OUT(-12564, out);
  32259. /* decompress inner compressedData */
  32260. ret = wc_PKCS7_DecodeCompressedData(pkcs7, encryptedTmp,
  32261. encryptedTmpSz, out, outSz);
  32262. if (ret < 0)
  32263. ERROR_OUT(-12565, out);
  32264. /* compare decompressed to expected */
  32265. if (((word32)ret != testVectors[i].contentSz) ||
  32266. XMEMCMP(out, testVectors[i].content, ret))
  32267. ERROR_OUT(-12566, out);
  32268. }
  32269. #endif /* NO_PKCS7_ENCRYPTED_DATA */
  32270. #endif /* HAVE_LIBZ && !NO_PKCS7_COMPRESSED_DATA */
  32271. }
  32272. out:
  32273. if (pkcs7 != NULL)
  32274. wc_PKCS7_Free(pkcs7);
  32275. if (out != NULL)
  32276. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32277. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA) && \
  32278. !defined(NO_PKCS7_ENCRYPTED_DATA)
  32279. if (encryptedTmp != NULL)
  32280. XFREE(encryptedTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32281. #endif
  32282. if (testVectors != NULL)
  32283. XFREE(testVectors, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32284. wc_FreeRng(&rng);
  32285. if (ret > 0)
  32286. return 0;
  32287. (void)eccClientCertBuf;
  32288. (void)eccClientCertBufSz;
  32289. (void)eccClientPrivKeyBuf;
  32290. (void)eccClientPrivKeyBufSz;
  32291. (void)rsaClientCertBuf;
  32292. (void)rsaClientCertBufSz;
  32293. (void)rsaClientPrivKeyBuf;
  32294. (void)rsaClientPrivKeyBufSz;
  32295. (void)rsaServerCertBuf;
  32296. (void)rsaServerCertBufSz;
  32297. (void)rsaServerPrivKeyBuf;
  32298. (void)rsaServerPrivKeyBufSz;
  32299. (void)rsaCaCertBuf;
  32300. (void)rsaCaCertBufSz;
  32301. (void)rsaCaPrivKeyBuf;
  32302. (void)rsaCaPrivKeyBufSz;
  32303. return ret;
  32304. }
  32305. WOLFSSL_TEST_SUBROUTINE int pkcs7signed_test(void)
  32306. {
  32307. int ret = 0;
  32308. byte* rsaClientCertBuf = NULL;
  32309. byte* rsaServerCertBuf = NULL;
  32310. byte* rsaCaCertBuf = NULL;
  32311. byte* eccClientCertBuf = NULL;
  32312. byte* rsaClientPrivKeyBuf = NULL;
  32313. byte* rsaServerPrivKeyBuf = NULL;
  32314. byte* rsaCaPrivKeyBuf = NULL;
  32315. byte* eccClientPrivKeyBuf = NULL;
  32316. word32 rsaClientCertBufSz = 0;
  32317. word32 rsaServerCertBufSz = 0;
  32318. word32 rsaCaCertBufSz = 0;
  32319. word32 eccClientCertBufSz = 0;
  32320. word32 rsaClientPrivKeyBufSz = 0;
  32321. word32 rsaServerPrivKeyBufSz = 0;
  32322. word32 rsaCaPrivKeyBufSz = 0;
  32323. word32 eccClientPrivKeyBufSz = 0;
  32324. #ifndef NO_RSA
  32325. /* read client RSA cert and key in DER format */
  32326. rsaClientCertBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  32327. DYNAMIC_TYPE_TMP_BUFFER);
  32328. if (rsaClientCertBuf == NULL)
  32329. ret = -12600;
  32330. rsaClientPrivKeyBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  32331. DYNAMIC_TYPE_TMP_BUFFER);
  32332. if (ret == 0 && rsaClientPrivKeyBuf == NULL) {
  32333. ret = -12601;
  32334. }
  32335. rsaClientCertBufSz = FOURK_BUF;
  32336. rsaClientPrivKeyBufSz = FOURK_BUF;
  32337. /* read server RSA cert and key in DER format */
  32338. rsaServerCertBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  32339. DYNAMIC_TYPE_TMP_BUFFER);
  32340. if (ret == 0 && rsaServerCertBuf == NULL)
  32341. ret = -12602;
  32342. rsaServerPrivKeyBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  32343. DYNAMIC_TYPE_TMP_BUFFER);
  32344. if (ret == 0 && rsaServerPrivKeyBuf == NULL) {
  32345. ret = -12603;
  32346. }
  32347. rsaServerCertBufSz = FOURK_BUF;
  32348. rsaServerPrivKeyBufSz = FOURK_BUF;
  32349. /* read CA RSA cert and key in DER format, for use with server cert */
  32350. rsaCaCertBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  32351. DYNAMIC_TYPE_TMP_BUFFER);
  32352. if (ret == 0 && rsaCaCertBuf == NULL)
  32353. ret = -12604;
  32354. rsaCaPrivKeyBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  32355. DYNAMIC_TYPE_TMP_BUFFER);
  32356. if (ret == 0 && rsaCaPrivKeyBuf == NULL) {
  32357. ret = -12605;
  32358. }
  32359. rsaCaCertBufSz = FOURK_BUF;
  32360. rsaCaPrivKeyBufSz = FOURK_BUF;
  32361. #endif /* NO_RSA */
  32362. #ifdef HAVE_ECC
  32363. /* read client ECC cert and key in DER format */
  32364. eccClientCertBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  32365. DYNAMIC_TYPE_TMP_BUFFER);
  32366. if (ret == 0 && eccClientCertBuf == NULL) {
  32367. ret = -12606;
  32368. }
  32369. eccClientPrivKeyBuf =(byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  32370. DYNAMIC_TYPE_TMP_BUFFER);
  32371. if (ret == 0 && eccClientPrivKeyBuf == NULL) {
  32372. ret = -12607;
  32373. }
  32374. eccClientCertBufSz = FOURK_BUF;
  32375. eccClientPrivKeyBufSz = FOURK_BUF;
  32376. #endif /* HAVE_ECC */
  32377. if (ret >= 0)
  32378. ret = pkcs7_load_certs_keys(rsaClientCertBuf, &rsaClientCertBufSz,
  32379. rsaClientPrivKeyBuf, &rsaClientPrivKeyBufSz,
  32380. rsaServerCertBuf, &rsaServerCertBufSz,
  32381. rsaServerPrivKeyBuf, &rsaServerPrivKeyBufSz,
  32382. rsaCaCertBuf, &rsaCaCertBufSz,
  32383. rsaCaPrivKeyBuf, &rsaCaPrivKeyBufSz,
  32384. eccClientCertBuf, &eccClientCertBufSz,
  32385. eccClientPrivKeyBuf, &eccClientPrivKeyBufSz);
  32386. if (ret < 0) {
  32387. ret = -12608;
  32388. }
  32389. if (ret >= 0)
  32390. ret = pkcs7signed_run_vectors(rsaClientCertBuf, (word32)rsaClientCertBufSz,
  32391. rsaClientPrivKeyBuf, (word32)rsaClientPrivKeyBufSz,
  32392. rsaServerCertBuf, (word32)rsaServerCertBufSz,
  32393. rsaServerPrivKeyBuf, (word32)rsaServerPrivKeyBufSz,
  32394. rsaCaCertBuf, (word32)rsaCaCertBufSz,
  32395. rsaCaPrivKeyBuf, (word32)rsaCaPrivKeyBufSz,
  32396. eccClientCertBuf, (word32)eccClientCertBufSz,
  32397. eccClientPrivKeyBuf, (word32)eccClientPrivKeyBufSz);
  32398. if (ret >= 0)
  32399. ret = pkcs7signed_run_SingleShotVectors(
  32400. rsaClientCertBuf, (word32)rsaClientCertBufSz,
  32401. rsaClientPrivKeyBuf, (word32)rsaClientPrivKeyBufSz,
  32402. rsaServerCertBuf, (word32)rsaServerCertBufSz,
  32403. rsaServerPrivKeyBuf, (word32)rsaServerPrivKeyBufSz,
  32404. rsaCaCertBuf, (word32)rsaCaCertBufSz,
  32405. rsaCaPrivKeyBuf, (word32)rsaCaPrivKeyBufSz,
  32406. eccClientCertBuf, (word32)eccClientCertBufSz,
  32407. eccClientPrivKeyBuf, (word32)eccClientPrivKeyBufSz);
  32408. #if !defined(NO_RSA) && !defined(NO_AES) && defined(HAVE_AES_CBC)
  32409. if (ret >= 0)
  32410. ret = pkcs7callback_test(
  32411. rsaClientCertBuf, (word32)rsaClientCertBufSz,
  32412. rsaClientPrivKeyBuf, (word32)rsaClientPrivKeyBufSz);
  32413. #endif
  32414. XFREE(rsaClientCertBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32415. XFREE(rsaClientPrivKeyBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32416. XFREE(rsaServerCertBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32417. XFREE(rsaServerPrivKeyBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32418. XFREE(rsaCaCertBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32419. XFREE(rsaCaPrivKeyBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32420. XFREE(eccClientCertBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32421. XFREE(eccClientPrivKeyBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32422. return ret;
  32423. }
  32424. #endif /* HAVE_PKCS7 */
  32425. #ifdef HAVE_VALGRIND
  32426. /* Need a static build to have access to symbols. */
  32427. /* Maximum number of bytes in a number to test. */
  32428. #define MP_MAX_TEST_BYTE_LEN 32
  32429. static int randNum(mp_int* n, int len, WC_RNG* rng, void* heap)
  32430. {
  32431. byte d[MP_MAX_TEST_BYTE_LEN];
  32432. int ret;
  32433. (void)heap;
  32434. do {
  32435. ret = wc_RNG_GenerateBlock(rng, d, len);
  32436. if (ret != 0)
  32437. return ret;
  32438. ret = mp_read_unsigned_bin(n, d, len);
  32439. if (ret != 0)
  32440. return ret;
  32441. } while (mp_iszero(n));
  32442. return 0;
  32443. }
  32444. #if defined(WOLFSSL_SP_MATH_ALL) || !defined(USE_FAST_MATH)
  32445. static int mp_test_div_3(mp_int* a, mp_int* r, WC_RNG* rng)
  32446. {
  32447. int i, j;
  32448. mp_digit rem;
  32449. mp_digit rem2;
  32450. #if (defined(WOLFSSL_SP_MATH_ALL) && !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
  32451. defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  32452. for (i = 0; i < 10; i++) {
  32453. for (j = 1; j < 10; j++) {
  32454. if (randNum(a, j, rng, NULL) != 0)
  32455. return -12620;
  32456. if (mp_div_3(a, r, &rem) != 0)
  32457. return -12621;
  32458. if (mp_mul_d(r, 3, r) != 0)
  32459. return -12622;
  32460. if (mp_add_d(r, rem, r) != 0)
  32461. return -12623;
  32462. if (mp_cmp(r, a) != MP_EQ)
  32463. return -12624;
  32464. }
  32465. }
  32466. if (mp_div_3(a, r, &rem) != 0)
  32467. return -12625;
  32468. if (mp_div_3(a, a, NULL) != 0)
  32469. return -12626;
  32470. if (mp_cmp(r, a) != MP_EQ)
  32471. return -12627;
  32472. #endif
  32473. #if defined(WOLFSSL_SP_MATH_ALL)
  32474. if (mp_div_d(a, 10, r, &rem) != 0)
  32475. return -12628;
  32476. if (mp_div_d(a, 10, a, NULL) != 0)
  32477. return -12629;
  32478. if (mp_cmp(r, a) != MP_EQ)
  32479. return -12630;
  32480. if (mp_div_d(a, 12, r, &rem) != 0)
  32481. return -12631;
  32482. if (mp_div_d(a, 12, a, NULL) != 0)
  32483. return -12632;
  32484. if (mp_cmp(r, a) != MP_EQ)
  32485. return -12633;
  32486. if (mp_div_d(a, (mp_digit)1 << (DIGIT_BIT / 2), r, &rem) != 0)
  32487. return -12634;
  32488. if (mp_div_d(a, (mp_digit)1 << (DIGIT_BIT / 2), NULL, &rem2) != 0)
  32489. return -12635;
  32490. if (mp_div_d(a, (mp_digit)1 << (DIGIT_BIT / 2), a, NULL) != 0)
  32491. return -12636;
  32492. if (mp_cmp(r, a) != MP_EQ)
  32493. return -12637;
  32494. if (rem != rem2)
  32495. return -12638;
  32496. #endif
  32497. (void)a;
  32498. (void)r;
  32499. (void)rng;
  32500. (void)i;
  32501. (void)j;
  32502. (void)rem;
  32503. (void)rem2;
  32504. return 0;
  32505. }
  32506. #endif /* WOLFSSL_SP_MATH || !USE_FAST_MATH */
  32507. #if (defined(WOLFSSL_SP_MATH_ALL) && !defined(NO_RSA) && \
  32508. !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
  32509. (!defined WOLFSSL_SP_MATH && !defined(WOLFSSL_SP_MATH_ALL) && \
  32510. (defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)))
  32511. static int mp_test_radix_10(mp_int* a, mp_int* r, WC_RNG* rng)
  32512. {
  32513. int ret;
  32514. int i, j;
  32515. int size;
  32516. char str[30];
  32517. WOLFSSL_SMALL_STACK_STATIC const char* badStr1 = "A";
  32518. WOLFSSL_SMALL_STACK_STATIC const char* badStr2 = "a";
  32519. WOLFSSL_SMALL_STACK_STATIC const char* badStr3 = " ";
  32520. WOLFSSL_SMALL_STACK_STATIC const char* zeros = "000";
  32521. WOLFSSL_SMALL_STACK_STATIC const char* empty = "";
  32522. for (i = 0; i < 10; i++) {
  32523. for (j = 2; j < 12; j++) {
  32524. if (randNum(a, j, rng, NULL) != 0)
  32525. return -12640;
  32526. if (mp_radix_size(a, MP_RADIX_DEC, &size) != MP_OKAY)
  32527. return -12641;
  32528. if (mp_toradix(a, str, MP_RADIX_DEC) != MP_OKAY)
  32529. return -12660;
  32530. if ((int)XSTRLEN(str) != size - 1)
  32531. return -12642;
  32532. if (mp_read_radix(r, str, MP_RADIX_DEC) != MP_OKAY)
  32533. return -12661;
  32534. if (mp_cmp(a, r) != MP_EQ)
  32535. return -12643;
  32536. }
  32537. }
  32538. if (mp_read_radix(r, badStr1, MP_RADIX_DEC) != MP_VAL)
  32539. return -12644;
  32540. if (mp_read_radix(r, badStr2, MP_RADIX_DEC) != MP_VAL)
  32541. return -12645;
  32542. if (mp_read_radix(r, badStr3, MP_RADIX_DEC) != MP_VAL)
  32543. return -12646;
  32544. if (mp_read_radix(r, zeros, MP_RADIX_DEC) != MP_OKAY)
  32545. return -12647;
  32546. if (!mp_iszero(r))
  32547. return -12648;
  32548. mp_set(r, 1);
  32549. if (mp_read_radix(r, empty, MP_RADIX_DEC) != MP_OKAY)
  32550. return -12649;
  32551. if (!mp_iszero(r))
  32552. return -12650;
  32553. mp_zero(a);
  32554. ret = mp_radix_size(a, MP_RADIX_DEC, &size);
  32555. if (ret != 0)
  32556. return -12651;
  32557. if (size != 2)
  32558. return -12652;
  32559. ret = mp_toradix(a, str, MP_RADIX_DEC);
  32560. if (ret != 0)
  32561. return -12653;
  32562. if ((int)XSTRLEN(str) != size - 1)
  32563. return -12654;
  32564. ret = mp_read_radix(r, str, MP_RADIX_DEC);
  32565. if (ret != 0)
  32566. return -12655;
  32567. if (!mp_iszero(r))
  32568. return -12656;
  32569. return 0;
  32570. }
  32571. #endif
  32572. #if defined(WOLFSSL_SP_MATH_ALL) || (!defined WOLFSSL_SP_MATH && \
  32573. defined(HAVE_ECC))
  32574. static int mp_test_radix_16(mp_int* a, mp_int* r, WC_RNG* rng)
  32575. {
  32576. int ret;
  32577. int i, j;
  32578. int size;
  32579. char str[30];
  32580. #if defined(WOLFSSL_SP_MATH) || defined(USE_FAST_MATH)
  32581. static char longStr[2 * sizeof(a->dp) + 2];
  32582. #endif
  32583. WOLFSSL_SMALL_STACK_STATIC const char* badStr1 = " ";
  32584. WOLFSSL_SMALL_STACK_STATIC const char* badStr2 = "}";
  32585. WOLFSSL_SMALL_STACK_STATIC const char* empty = "";
  32586. for (i = 0; i < 10; i++) {
  32587. for (j = 2; j < 12; j++) {
  32588. if (randNum(a, j, rng, NULL) != 0)
  32589. return -12660;
  32590. mp_radix_size(a, MP_RADIX_HEX, &size);
  32591. mp_toradix(a, str, MP_RADIX_HEX);
  32592. if ((int)XSTRLEN(str) != size - 1)
  32593. return -12661;
  32594. mp_read_radix(r, str, MP_RADIX_HEX);
  32595. if (mp_cmp(a, r) != MP_EQ)
  32596. return -12662;
  32597. }
  32598. }
  32599. if (mp_read_radix(r, badStr1, MP_RADIX_HEX) != MP_VAL)
  32600. return -12663;
  32601. if (mp_read_radix(r, badStr2, MP_RADIX_HEX) != MP_VAL)
  32602. return -12664;
  32603. mp_set(r, 1);
  32604. if (mp_read_radix(r, empty, MP_RADIX_HEX) != MP_OKAY)
  32605. return -12665;
  32606. if (!mp_iszero(r))
  32607. return -12666;
  32608. #if defined(WOLFSSL_SP_MATH) || defined(USE_FAST_MATH)
  32609. /* Fixed MP data size - string can be too long. */
  32610. longStr[0] = '8';
  32611. XMEMSET(longStr+1, '0', sizeof(longStr) - 2);
  32612. longStr[sizeof(longStr)-1] = '\0';
  32613. if (mp_read_radix(r, longStr, MP_RADIX_HEX) != MP_VAL)
  32614. return -12667;
  32615. #endif
  32616. mp_zero(a);
  32617. ret = mp_radix_size(a, MP_RADIX_HEX, &size);
  32618. if (ret != 0)
  32619. return -12668;
  32620. #ifndef WC_DISABLE_RADIX_ZERO_PAD
  32621. if (size != 3)
  32622. #else
  32623. if (size != 2)
  32624. #endif
  32625. return -12669;
  32626. ret = mp_toradix(a, str, MP_RADIX_HEX);
  32627. if (ret != 0)
  32628. return -12670;
  32629. if ((int)XSTRLEN(str) != size - 1)
  32630. return -12671;
  32631. ret = mp_read_radix(r, str, MP_RADIX_HEX);
  32632. if (ret != 0)
  32633. return -12672;
  32634. if (!mp_iszero(r))
  32635. return -12673;
  32636. #ifdef WOLFSSL_SP_MATH
  32637. ret = mp_toradix(a, str, 8);
  32638. if (ret != MP_VAL)
  32639. return -12674;
  32640. ret = mp_radix_size(a, 8, &size);
  32641. if (ret != MP_VAL)
  32642. return -12675;
  32643. #endif
  32644. return 0;
  32645. }
  32646. #endif
  32647. static int mp_test_shift(mp_int* a, mp_int* r1, WC_RNG* rng)
  32648. {
  32649. int i;
  32650. if (randNum(a, 4, rng, NULL) != 0)
  32651. return -12680;
  32652. for (i = 0; i < 4; i++) {
  32653. mp_copy(r1, a);
  32654. if (mp_lshd(r1, i) != MP_OKAY)
  32655. return -12681;
  32656. mp_rshd(r1, i);
  32657. if (mp_cmp(a, r1) != MP_EQ)
  32658. return -12682;
  32659. }
  32660. #ifndef WOLFSSL_SP_MATH
  32661. for (i = 0; i < DIGIT_BIT+1; i++) {
  32662. if (mp_mul_2d(a, i, r1) != MP_OKAY)
  32663. return -12683;
  32664. mp_rshb(r1, i);
  32665. if (mp_cmp(a, r1) != MP_EQ)
  32666. return -12684;
  32667. }
  32668. #endif
  32669. return 0;
  32670. }
  32671. static int mp_test_add_sub_d(mp_int* a, mp_int* r1)
  32672. {
  32673. int i, j;
  32674. for (i = 0; i <= DIGIT_BIT * 2; i++) {
  32675. mp_zero(a);
  32676. mp_set_bit(a, i);
  32677. if (a->used != (i + DIGIT_BIT) / DIGIT_BIT)
  32678. return -12690;
  32679. for (j = 0; j < i && j < DIGIT_BIT; j++) {
  32680. mp_zero(r1);
  32681. mp_set_bit(r1, i);
  32682. if (mp_sub_d(r1, (mp_digit)1 << j, r1) != MP_OKAY)
  32683. return -12691;
  32684. if (mp_add_d(r1, (mp_digit)1 << j, r1) != MP_OKAY)
  32685. return -12692;
  32686. if (mp_cmp(a, r1) != MP_EQ)
  32687. return -12693;
  32688. }
  32689. }
  32690. mp_zero(r1);
  32691. if (mp_add_d(r1, 1, r1) != MP_OKAY)
  32692. return -12694;
  32693. if (r1->used != 1)
  32694. return -12695;
  32695. if (mp_sub_d(r1, 1, r1) != MP_OKAY)
  32696. return -12696;
  32697. if (r1->used != 0)
  32698. return -12697;
  32699. return 0;
  32700. }
  32701. static int mp_test_read_to_bin(mp_int* a)
  32702. {
  32703. WOLFSSL_SMALL_STACK_STATIC const byte in[16] = {
  32704. 0x91, 0xa2, 0xb3, 0xc4, 0xd5, 0xe6, 0xf7, 0x08,
  32705. 0x93, 0xa4, 0xb4, 0xc5, 0xd6, 0xe7, 0xf8, 0x09
  32706. };
  32707. byte out[24];
  32708. int i, j, k;
  32709. const byte* p;
  32710. int ret;
  32711. for (i = 0; i < (int)sizeof(in); i++) {
  32712. p = in + sizeof(in) - i;
  32713. ret = mp_read_unsigned_bin(a, p, i);
  32714. if (ret != 0)
  32715. return -12710;
  32716. for (j = i; j < (int)sizeof(out); j++) {
  32717. XMEMSET(out, 0xff, sizeof(out));
  32718. ret = mp_to_unsigned_bin_len(a, out, j);
  32719. if (ret != 0)
  32720. return -12711;
  32721. for (k = 0; k < j - i; k++) {
  32722. if (out[k] != 0)
  32723. return -12712;
  32724. }
  32725. for (; k < j; k++) {
  32726. if (out[k] != p[k - (j - i)])
  32727. return -12713;
  32728. }
  32729. }
  32730. }
  32731. ret = mp_read_unsigned_bin(a, NULL, 0);
  32732. if (ret != 0)
  32733. return -12714;
  32734. if (!mp_iszero(a))
  32735. return -12715;
  32736. return 0;
  32737. }
  32738. #if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  32739. static int mp_test_set_int(mp_int* a)
  32740. {
  32741. #if SP_ULONG_BITS == 64
  32742. unsigned long n = 0xfedcba9876543210UL;
  32743. byte exp[8] = { 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10 };
  32744. byte out[8] = { 0 };
  32745. #elif SP_ULONG_BITS == 32
  32746. unsigned long n = 0xfedcba98UL;
  32747. byte exp[4] = { 0xfe, 0xdc, 0xba, 0x98 };
  32748. byte out[4] = { 0 };
  32749. #elif SP_ULONG_BITS == 16
  32750. unsigned long n = 0xfedc;
  32751. byte exp[2] = { 0xfe, 0xdc };
  32752. byte out[2] = { 0 };
  32753. #elif SP_ULONG_BITS == 8
  32754. unsigned long n = 0xfe;
  32755. byte exp[1] = { 0xfe };
  32756. byte out[1] = { 0 };
  32757. #endif
  32758. int ret;
  32759. ret = mp_set_int(a, n);
  32760. if (ret != 0)
  32761. return -12720;
  32762. ret = mp_unsigned_bin_size(a);
  32763. if (ret != sizeof(exp))
  32764. return -12721;
  32765. ret = mp_to_unsigned_bin(a, out);
  32766. if (ret != 0)
  32767. return -12722;
  32768. if (XMEMCMP(exp, out, sizeof(exp)) != 0)
  32769. return -12723;
  32770. return 0;
  32771. }
  32772. #endif
  32773. #if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  32774. static int mp_test_param(mp_int* a, mp_int* b, mp_int* r, WC_RNG* rng)
  32775. {
  32776. byte buffer[16];
  32777. #if defined(HAVE_ECC) || defined(WOLFSSL_SP_MATH_ALL)
  32778. char hexStr[] = "abcdef0123456789";
  32779. #ifndef WOLFSSL_SP_INT_NEGATIVE
  32780. char negStr[] = "-1234";
  32781. #endif
  32782. #endif
  32783. #if !defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_KEY_GEN) || \
  32784. defined(HAVE_COMP_KEY)
  32785. char decStr[] = "0987654321";
  32786. #endif
  32787. int ret;
  32788. #ifdef WOLFSSL_SP_MATH_ALL
  32789. mp_digit rho;
  32790. int size;
  32791. #endif
  32792. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH)
  32793. int result;
  32794. #endif
  32795. #if (defined(HAVE_ECC) && defined(HAVE_COMP_KEY)) || \
  32796. (defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN))
  32797. mp_digit rd;
  32798. #endif
  32799. (void)rng;
  32800. (void)r;
  32801. ret = mp_init(NULL);
  32802. if (ret != MP_VAL)
  32803. return -12730;
  32804. #if !defined(WOLFSSL_RSA_PUBLIC_ONLY) || (!defined(NO_DH) || defined(HAVE_ECC))
  32805. ret = mp_init_multi(NULL, NULL, NULL, NULL, NULL, NULL);
  32806. if (ret != MP_OKAY)
  32807. return -12731;
  32808. #endif
  32809. mp_free(NULL);
  32810. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) || !defined(NO_DH) || defined(HAVE_ECC)
  32811. ret = mp_grow(NULL, 1);
  32812. if (ret != MP_VAL)
  32813. return -12732;
  32814. #ifdef WOLFSSL_SP_MATH
  32815. ret = mp_grow(a, SP_INT_DIGITS + 1);
  32816. if (ret != MP_MEM)
  32817. return -12733;
  32818. #endif
  32819. #endif
  32820. mp_clear(NULL);
  32821. ret = mp_abs(NULL, NULL);
  32822. if (ret != MP_VAL)
  32823. return -12734;
  32824. ret = mp_abs(a, NULL);
  32825. if (ret != MP_VAL)
  32826. return -12735;
  32827. ret = mp_abs(NULL, b);
  32828. if (ret != MP_VAL)
  32829. return -12736;
  32830. ret = mp_unsigned_bin_size(NULL);
  32831. if (ret != 0)
  32832. return -12737;
  32833. ret = mp_read_unsigned_bin(NULL, NULL, sizeof(buffer));
  32834. if (ret != MP_VAL)
  32835. return -12738;
  32836. ret = mp_read_unsigned_bin(NULL, buffer, sizeof(buffer));
  32837. if (ret != MP_VAL)
  32838. return -12739;
  32839. ret = mp_read_unsigned_bin(a, NULL, sizeof(buffer));
  32840. if (ret != MP_VAL)
  32841. return -12740;
  32842. ret = mp_read_unsigned_bin(a, buffer, SP_INT_DIGITS * SP_WORD_SIZEOF + 1);
  32843. if (ret != MP_VAL)
  32844. return -12741;
  32845. #if defined(HAVE_ECC) || defined(WOLFSSL_SP_MATH_ALL)
  32846. ret = mp_read_radix(NULL, NULL, 16);
  32847. if (ret != MP_VAL)
  32848. return -12742;
  32849. ret = mp_read_radix(a, NULL, 16);
  32850. if (ret != MP_VAL)
  32851. return -12743;
  32852. ret = mp_read_radix(NULL, hexStr, 16);
  32853. if (ret != MP_VAL)
  32854. return -12744;
  32855. #ifndef WOLFSSL_SP_INT_NEGATIVE
  32856. ret = mp_read_radix(a, negStr, 16);
  32857. if (ret != MP_VAL)
  32858. return -12745;
  32859. #ifdef WOLFSSL_SP_MATH_ALL
  32860. ret = mp_read_radix(a, negStr, 10);
  32861. if (ret != MP_VAL)
  32862. return -12746;
  32863. #endif /* WOLFSSL_SP_MATH_ALL */
  32864. #endif /* WOLFSSL_SP_INT_NEGATIVE */
  32865. #endif
  32866. #ifndef WOLFSSL_SP_MATH_ALL
  32867. /* Radix 10 only supported with ALL. */
  32868. ret = mp_read_radix(a, decStr, 10);
  32869. if (ret != MP_VAL)
  32870. return -12747;
  32871. #endif
  32872. /* Radix 8 not supported SP_INT. */
  32873. ret = mp_read_radix(a, "0123", 8);
  32874. if (ret != MP_VAL)
  32875. return -12748;
  32876. ret = mp_count_bits(NULL);
  32877. if (ret != 0)
  32878. return -12749;
  32879. ret = mp_is_bit_set(NULL, 0);
  32880. if (ret != 0)
  32881. return -12750;
  32882. ret = mp_leading_bit(NULL);
  32883. if (ret != 0)
  32884. return -12751;
  32885. mp_zero(a);
  32886. ret = mp_leading_bit(a);
  32887. if (ret != 0)
  32888. return -12752;
  32889. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH) || \
  32890. defined(HAVE_ECC) || defined(WOLFSSL_KEY_GEN) || defined(OPENSSL_EXTRA) || \
  32891. !defined(NO_RSA)
  32892. ret = mp_set_bit(NULL, 1);
  32893. if (ret != MP_VAL)
  32894. return -12753;
  32895. #endif
  32896. #if !defined(NO_DH) || defined(HAVE_ECC) || defined(WC_RSA_BLINDING) || \
  32897. !defined(WOLFSSL_RSA_VERIFY_ONLY)
  32898. ret = mp_to_unsigned_bin(NULL, NULL);
  32899. if (ret != MP_VAL)
  32900. return -12754;
  32901. ret = mp_to_unsigned_bin(a, NULL);
  32902. if (ret != MP_VAL)
  32903. return -12755;
  32904. ret = mp_to_unsigned_bin(NULL, buffer);
  32905. if (ret != MP_VAL)
  32906. return -12756;
  32907. #endif
  32908. ret = mp_to_unsigned_bin_len(NULL, NULL, 1);
  32909. if (ret != MP_VAL)
  32910. return -12757;
  32911. ret = mp_to_unsigned_bin_len(a, NULL, 1);
  32912. if (ret != MP_VAL)
  32913. return -12758;
  32914. ret = mp_to_unsigned_bin_len(NULL, buffer, 1);
  32915. if (ret != MP_VAL)
  32916. return -12759;
  32917. #if defined(WOLFSSL_SP_MATH_ALL) && !defined(NO_RSA) && \
  32918. !defined(WOLFSSL_RSA_VERIFY_ONLY)
  32919. ret = mp_to_unsigned_bin_at_pos(0, NULL, NULL);
  32920. if (ret != MP_VAL)
  32921. return -12760;
  32922. ret = mp_to_unsigned_bin_at_pos(0, a, NULL);
  32923. if (ret != MP_VAL)
  32924. return -12761;
  32925. ret = mp_to_unsigned_bin_at_pos(0, NULL, buffer);
  32926. if (ret != MP_VAL)
  32927. return -12762;
  32928. ret = mp_to_unsigned_bin_at_pos(0, a, buffer);
  32929. if (ret != MP_OKAY)
  32930. return -12763;
  32931. #endif
  32932. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) || (!defined(NO_DH) || defined(HAVE_ECC))
  32933. ret = mp_copy(NULL, NULL);
  32934. if (ret != MP_VAL)
  32935. return -12764;
  32936. ret = mp_copy(a, NULL);
  32937. if (ret != MP_VAL)
  32938. return -12765;
  32939. ret = mp_copy(NULL, b);
  32940. if (ret != MP_VAL)
  32941. return -12766;
  32942. #endif
  32943. #if defined(WOLFSSL_KEY_GEN) || !defined(NO_DH)
  32944. ret = sp_2expt(NULL, 1);
  32945. if (ret != MP_VAL)
  32946. return -12767;
  32947. #endif
  32948. ret = mp_set(NULL, 0);
  32949. if (ret != MP_VAL)
  32950. return -12768;
  32951. ret = mp_cmp_d(NULL, 0);
  32952. if (ret != MP_LT)
  32953. return -12769;
  32954. ret = mp_cmp(NULL, NULL);
  32955. if (ret != MP_EQ)
  32956. return -12770;
  32957. ret = mp_cmp(a, NULL);
  32958. if (ret != MP_GT)
  32959. return -12771;
  32960. ret = mp_cmp(NULL, b);
  32961. if (ret != MP_LT)
  32962. return -12772;
  32963. #if !defined(NO_DH) || defined(HAVE_ECC) || !defined(WOLFSSL_RSA_VERIFY_ONLY)
  32964. mp_rshd(NULL, 1);
  32965. #endif
  32966. mp_zero(NULL);
  32967. #if !defined(NO_DH) || defined(HAVE_ECC) || defined(WC_RSA_BLINDING) || \
  32968. !defined(WOLFSSL_RSA_VERIFY_ONLY)
  32969. ret = mp_lshd(NULL, 0);
  32970. if (ret != MP_VAL)
  32971. return -12773;
  32972. ret = mp_lshd(a, SP_INT_DIGITS + 1);
  32973. if (ret != MP_VAL)
  32974. return -12774;
  32975. #endif
  32976. #if defined(WOLFSSL_SP_MATH_ALL)
  32977. ret = mp_div(NULL, NULL, a, b);
  32978. if (ret != MP_VAL)
  32979. return -12775;
  32980. ret = mp_div(a, NULL, a, b);
  32981. if (ret != MP_VAL)
  32982. return -12776;
  32983. ret = mp_div(NULL, b, a, b);
  32984. if (ret != MP_VAL)
  32985. return -12777;
  32986. ret = mp_div(a, b, NULL, NULL);
  32987. if (ret != MP_VAL)
  32988. return -12778;
  32989. #endif
  32990. #if defined(WOLFSSL_SP_MATH_ALL) || !defined(NO_DH) || defined(HAVE_ECC) || \
  32991. (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY))
  32992. ret = mp_mod(NULL, NULL, NULL);
  32993. if (ret != MP_VAL)
  32994. return -12779;
  32995. ret = mp_mod(a, NULL, NULL);
  32996. if (ret != MP_VAL)
  32997. return -12780;
  32998. ret = mp_mod(NULL, b, NULL);
  32999. if (ret != MP_VAL)
  33000. return -12781;
  33001. ret = mp_mod(NULL, NULL, r);
  33002. if (ret != MP_VAL)
  33003. return -12782;
  33004. ret = mp_mod(a, b, NULL);
  33005. if (ret != MP_VAL)
  33006. return -12783;
  33007. ret = mp_mod(a, NULL, r);
  33008. if (ret != MP_VAL)
  33009. return -12784;
  33010. ret = mp_mod(NULL, b, r);
  33011. if (ret != MP_VAL)
  33012. return -12785;
  33013. #endif
  33014. #if !defined(NO_RSA) || defined(WOLFSSL_SP_MATH_ALL)
  33015. ret = mp_set_int(NULL, 0);
  33016. if (ret != MP_VAL)
  33017. return -12786;
  33018. #endif
  33019. #if !defined(NO_RSA) || !defined(NO_DSA) || !defined(NO_DH) || \
  33020. (defined(HAVE_ECC) && defined(HAVE_COMP_KEY)) || defined(OPENSSL_EXTRA)
  33021. ret = mp_exptmod_ex(NULL, NULL, 1, NULL, NULL);
  33022. if (ret != MP_VAL)
  33023. return 9950;
  33024. ret = mp_exptmod_ex(a, NULL, 1, NULL, NULL);
  33025. if (ret != MP_VAL)
  33026. return 9951;
  33027. ret = mp_exptmod_ex(NULL, a, 1, NULL, NULL);
  33028. if (ret != MP_VAL)
  33029. return 9952;
  33030. ret = mp_exptmod_ex(NULL, NULL, 1, a, NULL);
  33031. if (ret != MP_VAL)
  33032. return 9953;
  33033. ret = mp_exptmod_ex(NULL, NULL, 1, NULL, a);
  33034. if (ret != MP_VAL)
  33035. return 9954;
  33036. ret = mp_exptmod_ex(a, a, 1, a, NULL);
  33037. if (ret != MP_VAL)
  33038. return 9955;
  33039. ret = mp_exptmod_ex(a, a, 1, NULL, a);
  33040. if (ret != MP_VAL)
  33041. return 9956;
  33042. ret = mp_exptmod_ex(a, NULL, 1, a, a);
  33043. if (ret != MP_VAL)
  33044. return 9957;
  33045. ret = mp_exptmod_ex(NULL, a, 1, a, a);
  33046. if (ret != MP_VAL)
  33047. return 9958;
  33048. ret = mp_exptmod_nct(NULL, NULL, NULL, NULL);
  33049. if (ret != MP_VAL)
  33050. return 9960;
  33051. ret = mp_exptmod_nct(a, NULL, NULL, NULL);
  33052. if (ret != MP_VAL)
  33053. return 9961;
  33054. ret = mp_exptmod_nct(NULL, a, NULL, NULL);
  33055. if (ret != MP_VAL)
  33056. return 9962;
  33057. ret = mp_exptmod_nct(NULL, NULL, a, NULL);
  33058. if (ret != MP_VAL)
  33059. return 9963;
  33060. ret = mp_exptmod_nct(NULL, NULL, NULL, a);
  33061. if (ret != MP_VAL)
  33062. return 9964;
  33063. ret = mp_exptmod_nct(a, a, a, NULL);
  33064. if (ret != MP_VAL)
  33065. return 9965;
  33066. ret = mp_exptmod_nct(a, a, NULL, a);
  33067. if (ret != MP_VAL)
  33068. return 9966;
  33069. ret = mp_exptmod_nct(a, NULL, a, a);
  33070. if (ret != MP_VAL)
  33071. return 9967;
  33072. ret = mp_exptmod_nct(NULL, a, a, a);
  33073. if (ret != MP_VAL)
  33074. return 9968;
  33075. #endif
  33076. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_DH) || !defined(NO_DSA)) && \
  33077. !defined(WC_NO_RNG)
  33078. ret = mp_rand_prime(NULL, 32, NULL, NULL);
  33079. if (ret != MP_VAL)
  33080. return -12787;
  33081. ret = mp_rand_prime(a, 32, NULL, NULL);
  33082. if (ret != MP_VAL)
  33083. return -12788;
  33084. ret = mp_rand_prime(NULL, 32, rng, NULL);
  33085. if (ret != MP_VAL)
  33086. return -12789;
  33087. ret = mp_rand_prime(a, 0, rng, NULL);
  33088. if (ret != MP_VAL)
  33089. return -9969;
  33090. #endif
  33091. #if defined(WOLFSSL_SP_MATH_ALL) && !defined(WOLFSSL_RSA_VERIFY_ONLY)
  33092. ret = mp_mul(NULL, NULL, NULL);
  33093. if (ret != MP_VAL)
  33094. return -12790;
  33095. ret = mp_mul(a, NULL, NULL);
  33096. if (ret != MP_VAL)
  33097. return -12791;
  33098. ret = mp_mul(NULL, b, NULL);
  33099. if (ret != MP_VAL)
  33100. return -12792;
  33101. ret = mp_mul(NULL, NULL, r);
  33102. if (ret != MP_VAL)
  33103. return -12793;
  33104. ret = mp_mul(a, b, NULL);
  33105. if (ret != MP_VAL)
  33106. return -12794;
  33107. ret = mp_mul(a, NULL, r);
  33108. if (ret != MP_VAL)
  33109. return -12795;
  33110. ret = mp_mul(NULL, b, r);
  33111. if (ret != MP_VAL)
  33112. return -12796;
  33113. #endif
  33114. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH) || \
  33115. defined(HAVE_ECC) || (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY))
  33116. ret = mp_sqr(NULL, NULL);
  33117. if (ret != MP_VAL)
  33118. return -12797;
  33119. ret = mp_sqr(a, NULL);
  33120. if (ret != MP_VAL)
  33121. return -12798;
  33122. ret = mp_sqr(NULL, r);
  33123. if (ret != MP_VAL)
  33124. return -12799;
  33125. #endif
  33126. #if !defined(WOLFSSL_RSA_VERIFY_ONLY)
  33127. ret = mp_sqrmod(NULL, NULL, NULL);
  33128. if (ret != MP_VAL)
  33129. return -12800;
  33130. ret = mp_sqrmod(a, NULL, NULL);
  33131. if (ret != MP_VAL)
  33132. return -12801;
  33133. ret = mp_sqrmod(NULL, a, NULL);
  33134. if (ret != MP_VAL)
  33135. return -12802;
  33136. ret = mp_sqrmod(NULL, NULL, a);
  33137. if (ret != MP_VAL)
  33138. return -12803;
  33139. ret = mp_sqrmod(a, b, NULL);
  33140. if (ret != MP_VAL)
  33141. return -12804;
  33142. ret = mp_sqrmod(a, NULL, b);
  33143. if (ret != MP_VAL)
  33144. return -12805;
  33145. ret = mp_sqrmod(NULL, a, b);
  33146. if (ret != MP_VAL)
  33147. return -12806;
  33148. ret = mp_mulmod(NULL, NULL, NULL, NULL);
  33149. if (ret != MP_VAL)
  33150. return -12807;
  33151. ret = mp_mulmod(a, NULL, NULL, NULL);
  33152. if (ret != MP_VAL)
  33153. return -12808;
  33154. ret = mp_mulmod(NULL, a, NULL, NULL);
  33155. if (ret != MP_VAL)
  33156. return -12809;
  33157. ret = mp_mulmod(NULL, NULL, a, NULL);
  33158. if (ret != MP_VAL)
  33159. return -12810;
  33160. ret = mp_mulmod(NULL, NULL, NULL, a);
  33161. if (ret != MP_VAL)
  33162. return -12811;
  33163. ret = mp_mulmod(a, b, b, NULL);
  33164. if (ret != MP_VAL)
  33165. return -12812;
  33166. ret = mp_mulmod(a, b, NULL, a);
  33167. if (ret != MP_VAL)
  33168. return -12813;
  33169. ret = mp_mulmod(a, NULL, b, a);
  33170. if (ret != MP_VAL)
  33171. return -12814;
  33172. ret = mp_mulmod(NULL, b, b, a);
  33173. if (ret != MP_VAL)
  33174. return -12815;
  33175. #endif
  33176. #if !defined(NO_PWDBASED) || defined(WOLFSSL_KEY_GEN) || !defined(NO_DH) || \
  33177. !defined(NO_RSA) || !defined(NO_DSA)
  33178. ret = mp_add_d(NULL, 1, NULL);
  33179. if (ret != MP_VAL)
  33180. return -12816;
  33181. ret = mp_add_d(a, 1, NULL);
  33182. if (ret != MP_VAL)
  33183. return -12817;
  33184. ret = mp_add_d(NULL, 1, b);
  33185. if (ret != MP_VAL)
  33186. return -12818;
  33187. #endif
  33188. #if (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
  33189. !defined(NO_DH) || defined(HAVE_ECC) || !defined(NO_DSA)
  33190. ret = mp_sub_d(NULL, 1, NULL);
  33191. if (ret != MP_VAL)
  33192. return -12819;
  33193. ret = mp_sub_d(a, 1, NULL);
  33194. if (ret != MP_VAL)
  33195. return -12820;
  33196. ret = mp_sub_d(NULL, 1, b);
  33197. if (ret != MP_VAL)
  33198. return -12821;
  33199. #endif
  33200. #if (defined(WOLFSSL_SP_MATH_ALL) && !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
  33201. defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  33202. ret = mp_div_d(NULL, 0, NULL, NULL);
  33203. if (ret != MP_VAL)
  33204. return -12822;
  33205. ret = mp_div_d(a, 0, NULL, NULL);
  33206. if (ret != MP_VAL)
  33207. return -12823;
  33208. ret = mp_div_d(NULL, 1, NULL, NULL);
  33209. if (ret != MP_VAL)
  33210. return -12824;
  33211. #endif
  33212. #if (defined(HAVE_ECC) && defined(HAVE_COMP_KEY)) || \
  33213. (defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN))
  33214. ret = mp_mod_d(NULL, 0, NULL);
  33215. if (ret != MP_VAL)
  33216. return -12825;
  33217. ret = mp_mod_d(a, 0, NULL);
  33218. if (ret != MP_VAL)
  33219. return -12826;
  33220. ret = mp_mod_d(NULL, 0, &rd);
  33221. if (ret != MP_VAL)
  33222. return -12827;
  33223. #endif
  33224. #if defined(WOLFSSL_SP_MATH_ALL) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  33225. ret = mp_gcd(NULL, NULL, NULL);
  33226. if (ret != MP_VAL)
  33227. return -12828;
  33228. ret = mp_gcd(a, NULL, NULL);
  33229. if (ret != MP_VAL)
  33230. return -12829;
  33231. ret = mp_gcd(NULL, a, NULL);
  33232. if (ret != MP_VAL)
  33233. return -12830;
  33234. ret = mp_gcd(NULL, NULL, a);
  33235. if (ret != MP_VAL)
  33236. return -12831;
  33237. ret = mp_gcd(a, b, NULL);
  33238. if (ret != MP_VAL)
  33239. return -12832;
  33240. ret = mp_gcd(a, NULL, b);
  33241. if (ret != MP_VAL)
  33242. return -12833;
  33243. ret = mp_gcd(NULL, a, b);
  33244. if (ret != MP_VAL)
  33245. return -12834;
  33246. #endif
  33247. #if !defined(WOLFSSL_SP_MATH) && defined(HAVE_ECC)
  33248. ret = mp_div_2_mod_ct(NULL, NULL, NULL);
  33249. if (ret != MP_VAL)
  33250. return -12835;
  33251. ret = mp_div_2_mod_ct(a, NULL, NULL);
  33252. if (ret != MP_VAL)
  33253. return -12836;
  33254. ret = mp_div_2_mod_ct(NULL, b, NULL);
  33255. if (ret != MP_VAL)
  33256. return -12837;
  33257. ret = mp_div_2_mod_ct(NULL, NULL, a);
  33258. if (ret != MP_VAL)
  33259. return -12838;
  33260. ret = mp_div_2_mod_ct(a, b, NULL);
  33261. if (ret != MP_VAL)
  33262. return -12839;
  33263. ret = mp_div_2_mod_ct(a, b, NULL);
  33264. if (ret != MP_VAL)
  33265. return -12840;
  33266. ret = mp_div_2_mod_ct(NULL, b, a);
  33267. if (ret != MP_VAL)
  33268. return -12841;
  33269. ret = mp_div_2(NULL, NULL);
  33270. if (ret != MP_VAL)
  33271. return -12842;
  33272. ret = mp_div_2(a, NULL);
  33273. if (ret != MP_VAL)
  33274. return -12843;
  33275. ret = mp_div_2(NULL, a);
  33276. if (ret != MP_VAL)
  33277. return -12844;
  33278. #endif
  33279. #if (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
  33280. defined(HAVE_ECC) || !defined(NO_DSA) || defined(OPENSSL_EXTRA)
  33281. ret = mp_invmod(NULL, NULL, NULL);
  33282. if (ret != MP_VAL)
  33283. return -12845;
  33284. ret = mp_invmod(a, NULL, NULL);
  33285. if (ret != MP_VAL)
  33286. return -12846;
  33287. ret = mp_invmod(NULL, b, NULL);
  33288. if (ret != MP_VAL)
  33289. return -12847;
  33290. ret = mp_invmod(NULL, NULL, a);
  33291. if (ret != MP_VAL)
  33292. return -12848;
  33293. ret = mp_invmod(a, b, NULL);
  33294. if (ret != MP_VAL)
  33295. return -12849;
  33296. ret = mp_invmod(a, NULL, a);
  33297. if (ret != MP_VAL)
  33298. return -12850;
  33299. ret = mp_invmod(NULL, b, a);
  33300. if (ret != MP_VAL)
  33301. return -12851;
  33302. #endif
  33303. #if !defined(WOLFSSL_SP_MATH) && defined(HAVE_ECC)
  33304. ret = mp_invmod_mont_ct(NULL, NULL, NULL, 1);
  33305. if (ret != MP_VAL)
  33306. return -12852;
  33307. ret = mp_invmod_mont_ct(a, NULL, NULL, 1);
  33308. if (ret != MP_VAL)
  33309. return -12853;
  33310. ret = mp_invmod_mont_ct(NULL, b, NULL, 1);
  33311. if (ret != MP_VAL)
  33312. return -12854;
  33313. ret = mp_invmod_mont_ct(NULL, NULL, a, 1);
  33314. if (ret != MP_VAL)
  33315. return -12855;
  33316. ret = mp_invmod_mont_ct(a, b, NULL, 1);
  33317. if (ret != MP_VAL)
  33318. return -12856;
  33319. ret = mp_invmod_mont_ct(a, NULL, a, 1);
  33320. if (ret != MP_VAL)
  33321. return -12857;
  33322. ret = mp_invmod_mont_ct(NULL, b, a, 1);
  33323. if (ret != MP_VAL)
  33324. return -12858;
  33325. #endif
  33326. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(WC_RSA_BLINDING)
  33327. ret = mp_lcm(NULL, NULL, NULL);
  33328. if (ret != MP_VAL)
  33329. return -12859;
  33330. ret = mp_lcm(a, NULL, NULL);
  33331. if (ret != MP_VAL)
  33332. return -12860;
  33333. ret = mp_lcm(NULL, b, NULL);
  33334. if (ret != MP_VAL)
  33335. return -12861;
  33336. ret = mp_lcm(NULL, NULL, a);
  33337. if (ret != MP_VAL)
  33338. return -12862;
  33339. ret = mp_lcm(a, b, NULL);
  33340. if (ret != MP_VAL)
  33341. return -12863;
  33342. ret = mp_lcm(a, NULL, a);
  33343. if (ret != MP_VAL)
  33344. return -12864;
  33345. ret = mp_lcm(NULL, b, a);
  33346. if (ret != MP_VAL)
  33347. return -12865;
  33348. #endif
  33349. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH)
  33350. ret = mp_exptmod_ex(NULL, NULL, 1, NULL, NULL);
  33351. if (ret != MP_VAL)
  33352. return -12866;
  33353. ret = mp_exptmod_ex(a, NULL, 1, NULL, NULL);
  33354. if (ret != MP_VAL)
  33355. return -12867;
  33356. ret = mp_exptmod_ex(NULL, b, 1, NULL, NULL);
  33357. if (ret != MP_VAL)
  33358. return -12868;
  33359. ret = mp_exptmod_ex(NULL, NULL, 1, b, NULL);
  33360. if (ret != MP_VAL)
  33361. return -12869;
  33362. ret = mp_exptmod_ex(NULL, NULL, 1, NULL, a);
  33363. if (ret != MP_VAL)
  33364. return -12870;
  33365. ret = mp_exptmod_ex(a, b, 1, b, NULL);
  33366. if (ret != MP_VAL)
  33367. return -12871;
  33368. ret = mp_exptmod_ex(a, b, 1, NULL, a);
  33369. if (ret != MP_VAL)
  33370. return -12872;
  33371. ret = mp_exptmod_ex(a, NULL, 1, b, a);
  33372. if (ret != MP_VAL)
  33373. return -12873;
  33374. ret = mp_exptmod_ex(NULL, b, 1, b, a);
  33375. if (ret != MP_VAL)
  33376. return -12874;
  33377. ret = mp_exptmod(NULL, NULL, NULL, NULL);
  33378. if (ret != MP_VAL)
  33379. return -12875;
  33380. ret = mp_exptmod(a, NULL, NULL, NULL);
  33381. if (ret != MP_VAL)
  33382. return -12876;
  33383. ret = mp_exptmod(NULL, b, NULL, NULL);
  33384. if (ret != MP_VAL)
  33385. return -12877;
  33386. ret = mp_exptmod(NULL, NULL, b, NULL);
  33387. if (ret != MP_VAL)
  33388. return -12878;
  33389. ret = mp_exptmod(NULL, NULL, NULL, a);
  33390. if (ret != MP_VAL)
  33391. return -12879;
  33392. ret = mp_exptmod(a, b, b, NULL);
  33393. if (ret != MP_VAL)
  33394. return -12880;
  33395. ret = mp_exptmod(a, b, NULL, a);
  33396. if (ret != MP_VAL)
  33397. return -12881;
  33398. ret = mp_exptmod(a, NULL, b, a);
  33399. if (ret != MP_VAL)
  33400. return -12882;
  33401. ret = mp_exptmod(NULL, b, b, a);
  33402. if (ret != MP_VAL)
  33403. return -12883;
  33404. ret = mp_exptmod_nct(NULL, NULL, NULL, NULL);
  33405. if (ret != MP_VAL)
  33406. return -12884;
  33407. ret = mp_exptmod_nct(a, NULL, NULL, NULL);
  33408. if (ret != MP_VAL)
  33409. return -12885;
  33410. ret = mp_exptmod_nct(NULL, b, NULL, NULL);
  33411. if (ret != MP_VAL)
  33412. return -12886;
  33413. ret = mp_exptmod_nct(NULL, NULL, b, NULL);
  33414. if (ret != MP_VAL)
  33415. return -12887;
  33416. ret = mp_exptmod_nct(NULL, NULL, NULL, a);
  33417. if (ret != MP_VAL)
  33418. return -12888;
  33419. ret = mp_exptmod_nct(a, b, b, NULL);
  33420. if (ret != MP_VAL)
  33421. return -12889;
  33422. ret = mp_exptmod_nct(a, b, NULL, a);
  33423. if (ret != MP_VAL)
  33424. return -12890;
  33425. ret = mp_exptmod_nct(a, NULL, b, a);
  33426. if (ret != MP_VAL)
  33427. return -12891;
  33428. ret = mp_exptmod_nct(NULL, b, b, a);
  33429. if (ret != MP_VAL)
  33430. return -12892;
  33431. #endif
  33432. #if defined(HAVE_ECC) && defined(HAVE_COMP_KEY)
  33433. ret = mp_cnt_lsb(NULL);
  33434. if (ret != 0)
  33435. return -12893;
  33436. #endif
  33437. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH)
  33438. ret = mp_prime_is_prime(NULL, 1, NULL);
  33439. if (ret != MP_VAL)
  33440. return -12894;
  33441. ret = mp_prime_is_prime(a, 1, NULL);
  33442. if (ret != MP_VAL)
  33443. return -12895;
  33444. ret = mp_prime_is_prime(NULL, 1, &result);
  33445. if (ret != MP_VAL)
  33446. return -12896;
  33447. ret = mp_prime_is_prime(a, 0, &result);
  33448. if (ret != MP_VAL)
  33449. return -12897;
  33450. ret = mp_prime_is_prime(a, 1024, &result);
  33451. if (ret != MP_VAL)
  33452. return -12898;
  33453. ret = mp_prime_is_prime_ex(NULL, 1, NULL, NULL);
  33454. if (ret != MP_VAL)
  33455. return -12899;
  33456. ret = mp_prime_is_prime_ex(a, 1, NULL, NULL);
  33457. if (ret != MP_VAL)
  33458. return -12900;
  33459. ret = mp_prime_is_prime_ex(NULL, 1, &result, NULL);
  33460. if (ret != MP_VAL)
  33461. return -12901;
  33462. ret = mp_prime_is_prime_ex(NULL, 1, NULL, rng);
  33463. if (ret != MP_VAL)
  33464. return -12902;
  33465. ret = mp_prime_is_prime_ex(a, 1, &result, NULL);
  33466. if (ret != MP_VAL)
  33467. return -12903;
  33468. ret = mp_prime_is_prime_ex(a, 1, NULL, rng);
  33469. if (ret != MP_VAL)
  33470. return -12904;
  33471. ret = mp_prime_is_prime_ex(NULL, 1, &result, rng);
  33472. if (ret != MP_VAL)
  33473. return -12905;
  33474. #endif
  33475. #if defined(WOLFSSL_SP_MATH_ALL) || !defined(NO_DH) || !defined(NO_DSA)
  33476. ret = mp_exch(NULL, NULL);
  33477. if (ret != MP_VAL)
  33478. return -12906;
  33479. ret = mp_exch(a, NULL);
  33480. if (ret != MP_VAL)
  33481. return -12907;
  33482. ret = mp_exch(NULL, b);
  33483. if (ret != MP_VAL)
  33484. return -12908;
  33485. #endif
  33486. #if (defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA)) || \
  33487. defined(WOLFSSL_SP_MATH_ALL)
  33488. ret = mp_mul_d(NULL, 1, NULL);
  33489. if (ret != MP_VAL)
  33490. return -12909;
  33491. ret = mp_mul_d(a, 1, NULL);
  33492. if (ret != MP_VAL)
  33493. return -12910;
  33494. ret = mp_mul_d(NULL, 1, b);
  33495. if (ret != MP_VAL)
  33496. return -12911;
  33497. #endif
  33498. #if !defined(WOLFSSL_RSA_VERIFY_ONLY)
  33499. ret = mp_add(NULL, NULL, NULL);
  33500. if (ret != MP_VAL)
  33501. return -12912;
  33502. ret = mp_add(a, NULL, NULL);
  33503. if (ret != MP_VAL)
  33504. return -12913;
  33505. ret = mp_add(NULL, b, NULL);
  33506. if (ret != MP_VAL)
  33507. return -12914;
  33508. ret = mp_add(NULL, NULL, r);
  33509. if (ret != MP_VAL)
  33510. return -12915;
  33511. ret = mp_add(a, b, NULL);
  33512. if (ret != MP_VAL)
  33513. return -12916;
  33514. ret = mp_add(a, NULL, r);
  33515. if (ret != MP_VAL)
  33516. return -12917;
  33517. ret = mp_add(NULL, b, r);
  33518. if (ret != MP_VAL)
  33519. return -12918;
  33520. #endif
  33521. #if defined(WOLFSSL_SP_MATH_ALL) || !defined(NO_DH) || defined(HAVE_ECC) || \
  33522. (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY))
  33523. ret = mp_sub(NULL, NULL, NULL);
  33524. if (ret != MP_VAL)
  33525. return -12919;
  33526. ret = mp_sub(a, NULL, NULL);
  33527. if (ret != MP_VAL)
  33528. return -12920;
  33529. ret = mp_sub(NULL, b, NULL);
  33530. if (ret != MP_VAL)
  33531. return -12921;
  33532. ret = mp_sub(NULL, NULL, r);
  33533. if (ret != MP_VAL)
  33534. return -12922;
  33535. ret = mp_sub(a, b, NULL);
  33536. if (ret != MP_VAL)
  33537. return -12923;
  33538. ret = mp_sub(a, NULL, r);
  33539. if (ret != MP_VAL)
  33540. return -12924;
  33541. ret = mp_sub(NULL, b, r);
  33542. if (ret != MP_VAL)
  33543. return -12925;
  33544. #endif
  33545. #if defined(WOLFSSL_SP_MATH_ALL) || (!defined(WOLFSSL_SP_MATH) && \
  33546. defined(WOLFSSL_CUSTOM_CURVES))
  33547. ret = mp_addmod(NULL, NULL, NULL, NULL);
  33548. if (ret != MP_VAL)
  33549. return -12926;
  33550. ret = mp_addmod(a, NULL, NULL, NULL);
  33551. if (ret != MP_VAL)
  33552. return -12927;
  33553. ret = mp_addmod(NULL, b, NULL, NULL);
  33554. if (ret != MP_VAL)
  33555. return -12928;
  33556. ret = mp_addmod(NULL, NULL, b, NULL);
  33557. if (ret != MP_VAL)
  33558. return -12929;
  33559. ret = mp_addmod(NULL, NULL, NULL, a);
  33560. if (ret != MP_VAL)
  33561. return -12930;
  33562. ret = mp_addmod(a, b, b, NULL);
  33563. if (ret != MP_VAL)
  33564. return -12931;
  33565. ret = mp_addmod(a, b, NULL, a);
  33566. if (ret != MP_VAL)
  33567. return -12932;
  33568. ret = mp_addmod(a, NULL, b, a);
  33569. if (ret != MP_VAL)
  33570. return -12933;
  33571. ret = mp_addmod(NULL, b, b, a);
  33572. if (ret != MP_VAL)
  33573. return -12934;
  33574. #endif
  33575. #ifdef WOLFSSL_SP_MATH_ALL
  33576. ret = mp_submod(NULL, NULL, NULL, NULL);
  33577. if (ret != MP_VAL)
  33578. return -12935;
  33579. ret = mp_submod(a, NULL, NULL, NULL);
  33580. if (ret != MP_VAL)
  33581. return -12936;
  33582. ret = mp_submod(NULL, b, NULL, NULL);
  33583. if (ret != MP_VAL)
  33584. return -12937;
  33585. ret = mp_submod(NULL, NULL, b, NULL);
  33586. if (ret != MP_VAL)
  33587. return -12938;
  33588. ret = mp_submod(NULL, NULL, NULL, a);
  33589. if (ret != MP_VAL)
  33590. return -12939;
  33591. ret = mp_submod(a, b, b, NULL);
  33592. if (ret != MP_VAL)
  33593. return -12940;
  33594. ret = mp_submod(a, b, NULL, a);
  33595. if (ret != MP_VAL)
  33596. return -12941;
  33597. ret = mp_submod(a, NULL, b, a);
  33598. if (ret != MP_VAL)
  33599. return -12942;
  33600. ret = mp_submod(NULL, b, b, a);
  33601. if (ret != MP_VAL)
  33602. return -12943;
  33603. #endif
  33604. #ifdef WOLFSSL_SP_MATH_ALL
  33605. ret = mp_div_2d(NULL, 1, a, b);
  33606. if (ret != MP_VAL)
  33607. return -12944;
  33608. ret = mp_mod_2d(NULL, 1, NULL);
  33609. if (ret != MP_VAL)
  33610. return -12945;
  33611. ret = mp_mod_2d(a, 1, NULL);
  33612. if (ret != MP_VAL)
  33613. return -12946;
  33614. ret = mp_mod_2d(NULL, 1, b);
  33615. if (ret != MP_VAL)
  33616. return -12947;
  33617. ret = mp_mul_2d(NULL, 1, NULL);
  33618. if (ret != MP_VAL)
  33619. return -12948;
  33620. ret = mp_mul_2d(a, 1, NULL);
  33621. if (ret != MP_VAL)
  33622. return -12949;
  33623. ret = mp_mul_2d(NULL, 1, b);
  33624. if (ret != MP_VAL)
  33625. return -12950;
  33626. #endif
  33627. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH) || \
  33628. defined(HAVE_ECC) || (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY))
  33629. ret = mp_montgomery_reduce(NULL, NULL, 1);
  33630. if (ret != MP_VAL)
  33631. return -12951;
  33632. ret = mp_montgomery_reduce(a, NULL, 1);
  33633. if (ret != MP_VAL)
  33634. return -12952;
  33635. ret = mp_montgomery_reduce(NULL, b, 1);
  33636. if (ret != MP_VAL)
  33637. return -12953;
  33638. mp_zero(b);
  33639. ret = mp_montgomery_reduce(a, b, 1);
  33640. if (ret != MP_VAL)
  33641. return -12954;
  33642. #endif
  33643. #ifdef WOLFSSL_SP_MATH_ALL
  33644. ret = mp_montgomery_setup(NULL, NULL);
  33645. if (ret != MP_VAL)
  33646. return -12955;
  33647. ret = mp_montgomery_setup(a, NULL);
  33648. if (ret != MP_VAL)
  33649. return -12956;
  33650. ret = mp_montgomery_setup(NULL, &rho);
  33651. if (ret != MP_VAL)
  33652. return -12957;
  33653. ret = mp_montgomery_calc_normalization(NULL, NULL);
  33654. if (ret != MP_VAL)
  33655. return -12958;
  33656. ret = mp_montgomery_calc_normalization(a, NULL);
  33657. if (ret != MP_VAL)
  33658. return -12959;
  33659. ret = mp_montgomery_calc_normalization(NULL, b);
  33660. if (ret != MP_VAL)
  33661. return -12960;
  33662. #endif
  33663. ret = mp_unsigned_bin_size(NULL);
  33664. if (ret != 0)
  33665. return -12961;
  33666. #if defined(WC_MP_TO_RADIX) || defined(WOLFSSL_SP_MATH_ALL)
  33667. ret = mp_tohex(NULL, NULL);
  33668. if (ret != MP_VAL)
  33669. return -12962;
  33670. ret = mp_tohex(a, NULL);
  33671. if (ret != MP_VAL)
  33672. return -12963;
  33673. ret = mp_tohex(NULL, hexStr);
  33674. if (ret != MP_VAL)
  33675. return -12964;
  33676. #endif
  33677. #if defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  33678. ret = mp_todecimal(NULL, NULL);
  33679. if (ret != MP_VAL)
  33680. return -12965;
  33681. ret = mp_todecimal(a, NULL);
  33682. if (ret != MP_VAL)
  33683. return -12966;
  33684. ret = mp_todecimal(NULL, decStr);
  33685. if (ret != MP_VAL)
  33686. return -12967;
  33687. #endif
  33688. #ifdef WOLFSSL_SP_MATH_ALL
  33689. ret = mp_toradix(NULL, NULL, MP_RADIX_HEX);
  33690. if (ret != MP_VAL)
  33691. return -12968;
  33692. ret = mp_toradix(a, NULL, MP_RADIX_HEX);
  33693. if (ret != MP_VAL)
  33694. return -12969;
  33695. ret = mp_toradix(NULL, hexStr, MP_RADIX_HEX);
  33696. if (ret != MP_VAL)
  33697. return -12970;
  33698. ret = mp_toradix(a, hexStr, 3);
  33699. if (ret != MP_VAL)
  33700. return -12971;
  33701. ret = mp_radix_size(NULL, MP_RADIX_HEX, NULL);
  33702. if (ret != MP_VAL)
  33703. return -12972;
  33704. ret = mp_radix_size(a, MP_RADIX_HEX, NULL);
  33705. if (ret != MP_VAL)
  33706. return -12973;
  33707. ret = mp_radix_size(NULL, MP_RADIX_HEX, &size);
  33708. if (ret != MP_VAL)
  33709. return -12974;
  33710. ret = mp_radix_size(a, 3, &size);
  33711. if (ret != MP_VAL)
  33712. return -12975;
  33713. #endif
  33714. return 0;
  33715. }
  33716. #endif
  33717. #if !defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  33718. static int mp_test_set_is_bit(mp_int* a)
  33719. {
  33720. int i, j;
  33721. mp_zero(a);
  33722. for (i = 0; i <= DIGIT_BIT * 2; i++) {
  33723. if (mp_is_bit_set(a, i))
  33724. return -12980;
  33725. for (j = 0; j < i; j++) {
  33726. if (!mp_is_bit_set(a, j))
  33727. return -12981;
  33728. }
  33729. if (mp_set_bit(a, i) != 0)
  33730. return -12982;
  33731. if (!mp_is_bit_set(a, i))
  33732. return -12983;
  33733. }
  33734. mp_zero(a);
  33735. for (i = 0; i <= DIGIT_BIT * 2; i++) {
  33736. if (mp_is_bit_set(a, i))
  33737. return -12984;
  33738. }
  33739. for (i = 0; i <= DIGIT_BIT * 2; i++) {
  33740. mp_zero(a);
  33741. if (mp_set_bit(a, i) != 0)
  33742. return -12985;
  33743. for (j = 0; j < i; j++) {
  33744. if (mp_is_bit_set(a, j))
  33745. return -12986;
  33746. }
  33747. if (!mp_is_bit_set(a, i))
  33748. return -12987;
  33749. }
  33750. #ifdef WOLFSSL_KEY_GEN
  33751. for (i = 0; i < DIGIT_BIT * 2; i++) {
  33752. mp_set(a, 1);
  33753. if (mp_2expt(a, i) != 0)
  33754. return -12988;
  33755. for (j = 0; j < i; j++) {
  33756. if (mp_is_bit_set(a, j))
  33757. return -12989;
  33758. }
  33759. if (!mp_is_bit_set(a, i))
  33760. return -12990;
  33761. }
  33762. #endif
  33763. #ifdef WOLFSSL_SP_MATH
  33764. mp_zero(a);
  33765. for (j = 1; j <= 3; j++) {
  33766. i = SP_INT_MAX_BITS - j;
  33767. if (mp_is_bit_set(a, i))
  33768. return -12991;
  33769. if (mp_set_bit(a, i) != 0)
  33770. return -12992;
  33771. if (!mp_is_bit_set(a, i))
  33772. return -12993;
  33773. #ifdef WOLFSSL_KEY_GEN
  33774. if (mp_2expt(a, i) != 0)
  33775. return -12994;
  33776. if (!mp_is_bit_set(a, i))
  33777. return -12995;
  33778. #endif
  33779. }
  33780. mp_zero(a);
  33781. for (j = 0; j <= 3; j++) {
  33782. i = SP_INT_MAX_BITS + j;
  33783. if (mp_is_bit_set(a, i))
  33784. return -12996;
  33785. if (mp_set_bit(a, i) != MP_VAL)
  33786. return -12997;
  33787. #ifdef WOLFSSL_KEY_GEN
  33788. if (mp_2expt(a, i) != MP_VAL)
  33789. return -12998;
  33790. #endif
  33791. }
  33792. #endif
  33793. return 0;
  33794. }
  33795. #endif /* !WOLFSSL_SP_MATH || WOLFSSL_SP_MATH_ALL */
  33796. static int mp_test_cmp(mp_int* a, mp_int* b)
  33797. {
  33798. int ret;
  33799. mp_zero(a);
  33800. mp_zero(b);
  33801. ret = mp_cmp_d(a, 0);
  33802. if (ret != MP_EQ)
  33803. return -13000;
  33804. ret = mp_cmp_d(a, 1);
  33805. if (ret != MP_LT)
  33806. return -13001;
  33807. ret = mp_cmp(a, b);
  33808. if (ret != MP_EQ)
  33809. return -13002;
  33810. mp_set(a, 1);
  33811. ret = mp_cmp_d(a, 0);
  33812. if (ret != MP_GT)
  33813. return -13003;
  33814. ret = mp_cmp_d(a, 1);
  33815. if (ret != MP_EQ)
  33816. return -13004;
  33817. ret = mp_cmp_d(a, 2);
  33818. if (ret != MP_LT)
  33819. return -13005;
  33820. ret = mp_cmp(a, b);
  33821. if (ret != MP_GT)
  33822. return -13006;
  33823. mp_read_radix(b, "1234567890123456789", MP_RADIX_HEX);
  33824. ret = mp_cmp_d(b, -1);
  33825. if (ret != MP_GT)
  33826. return -13007;
  33827. ret = mp_cmp(a, b);
  33828. if (ret != MP_LT)
  33829. return -13008;
  33830. ret = mp_cmp(b, a);
  33831. if (ret != MP_GT)
  33832. return -13009;
  33833. ret = mp_cmp(b, b);
  33834. if (ret != MP_EQ)
  33835. return -13010;
  33836. #if (!defined(WOLFSSL_SP_MATH) && !defined(WOLFSSL_SP_MATH_ALL)) || \
  33837. defined(WOLFSSL_SP_INT_NEGATIVE)
  33838. mp_read_radix(a, "-1", MP_RADIX_HEX);
  33839. mp_read_radix(a, "1", MP_RADIX_HEX);
  33840. ret = mp_cmp(a, b);
  33841. if (ret != MP_LT)
  33842. return -13011;
  33843. ret = mp_cmp(b, a);
  33844. if (ret != MP_GT)
  33845. return -13012;
  33846. mp_read_radix(b, "-2", MP_RADIX_HEX);
  33847. ret = mp_cmp(a, b);
  33848. if (ret != MP_GT)
  33849. return -13013;
  33850. ret = mp_cmp(b, a);
  33851. if (ret != MP_LT)
  33852. return -13014;
  33853. mp_read_radix(a, "-2", MP_RADIX_HEX);
  33854. ret = mp_cmp(a, b);
  33855. if (ret != MP_EQ)
  33856. return -13015;
  33857. #endif
  33858. return 0;
  33859. }
  33860. #if !defined(NO_DH) || defined(HAVE_ECC) || !defined(WOLFSSL_RSA_VERIFY_ONLY)
  33861. static int mp_test_shbd(mp_int* a, mp_int* b, WC_RNG* rng)
  33862. {
  33863. int ret;
  33864. int i, j, k;
  33865. #ifndef WOLFSSL_SP_MATH
  33866. for (i = 0; i < 10; i++) {
  33867. for (j = 1; j < (DIGIT_BIT + 7) / 8 * 3; j++) {
  33868. ret = randNum(a, j, rng, NULL);
  33869. if (ret != MP_OKAY)
  33870. return -13020;
  33871. mp_copy(a, b);
  33872. for (k = 0; k <= DIGIT_BIT * 2; k++) {
  33873. ret = mp_mul_2d(a, k, a);
  33874. if (ret != MP_OKAY)
  33875. return -13021;
  33876. mp_rshb(a, k);
  33877. if (mp_cmp(a, b) != MP_EQ)
  33878. return -13022;
  33879. }
  33880. }
  33881. }
  33882. #endif
  33883. for (i = 0; i < 10; i++) {
  33884. for (j = 1; j < (DIGIT_BIT + 7) / 8 * 3; j++) {
  33885. ret = randNum(a, j, rng, NULL);
  33886. if (ret != MP_OKAY)
  33887. return -13023;
  33888. mp_copy(a, b);
  33889. for (k = 0; k < 10; k++) {
  33890. ret = mp_lshd(a, k);
  33891. if (ret != MP_OKAY)
  33892. return -13024;
  33893. mp_rshd(a, k);
  33894. if (mp_cmp(a, b) != MP_EQ)
  33895. return -13025;
  33896. }
  33897. }
  33898. }
  33899. mp_zero(a);
  33900. mp_rshd(a, 1);
  33901. if (!mp_iszero(a))
  33902. return -13026;
  33903. mp_set(a, 1);
  33904. mp_rshd(a, 1);
  33905. if (!mp_iszero(a))
  33906. return -13027;
  33907. mp_set(a, 1);
  33908. mp_rshd(a, 2);
  33909. if (!mp_iszero(a))
  33910. return -13028;
  33911. return 0;
  33912. }
  33913. #endif
  33914. #if !defined(WOLFSSL_SP_MATH) && !defined(WOLFSSL_SP_MATH_ALL)
  33915. static int mp_test_div(mp_int* a, mp_int* d, mp_int* r, mp_int* rem,
  33916. WC_RNG* rng)
  33917. {
  33918. int ret;
  33919. int i, j, k;
  33920. mp_zero(a);
  33921. mp_zero(d);
  33922. ret = mp_div(a, d, r, rem);
  33923. if (ret != MP_VAL)
  33924. return -13030;
  33925. mp_set(d, 1);
  33926. ret = mp_div(a, d, r, rem);
  33927. if (ret != MP_OKAY)
  33928. return -13031;
  33929. if (!mp_iszero(r))
  33930. return -13032;
  33931. if (!mp_iszero(rem))
  33932. return -13033;
  33933. mp_set(a, 1);
  33934. ret = mp_div(a, d, r, rem);
  33935. if (ret != MP_OKAY)
  33936. return -13034;
  33937. if (!mp_isone(r))
  33938. return -13035;
  33939. if (!mp_iszero(rem))
  33940. return -13036;
  33941. for (i = 0; i < 100; i++) {
  33942. for (j = 1; j < (DIGIT_BIT + 7) / 8 * 2; j++) {
  33943. ret = randNum(d, j, rng, NULL);
  33944. if (ret != MP_OKAY)
  33945. return -13037;
  33946. for (k = 1; k < (DIGIT_BIT + 7) / 8 * 2 + 1; k++) {
  33947. ret = randNum(a, k, rng, NULL);
  33948. if (ret != MP_OKAY)
  33949. return -13038;
  33950. ret = mp_div(a, d, NULL, rem);
  33951. if (ret != MP_OKAY)
  33952. return -13039;
  33953. ret = mp_div(a, d, r, NULL);
  33954. if (ret != MP_OKAY)
  33955. return -13040;
  33956. ret = mp_div(a, d, r, rem);
  33957. if (ret != MP_OKAY)
  33958. return -13041;
  33959. mp_mul(r, d, r);
  33960. mp_add(r, rem, r);
  33961. if (mp_cmp(r, a) != MP_EQ)
  33962. return -13042;
  33963. }
  33964. }
  33965. }
  33966. ret = randNum(d, (DIGIT_BIT + 7) / 8 * 2, rng, NULL);
  33967. if (ret != MP_OKAY)
  33968. return -13043;
  33969. mp_add(d, d, a);
  33970. mp_set(rem, 1);
  33971. mp_div(a, d, NULL, rem);
  33972. if (ret != MP_OKAY)
  33973. return -13044;
  33974. if (!mp_iszero(rem))
  33975. return -13045;
  33976. mp_set(r, 1);
  33977. mp_div(a, d, r, NULL);
  33978. if (ret != MP_OKAY)
  33979. return -13046;
  33980. if (mp_cmp_d(r, 2) != MP_EQ)
  33981. return -13047;
  33982. mp_set(r, 1);
  33983. mp_set(rem, 1);
  33984. mp_div(a, d, r, rem);
  33985. if (ret != MP_OKAY)
  33986. return -13048;
  33987. if (mp_cmp_d(r, 2) != MP_EQ)
  33988. return -13049;
  33989. if (!mp_iszero(rem))
  33990. return -13050;
  33991. mp_set(a, 0xfe);
  33992. mp_lshd(a, 3);
  33993. mp_add_d(a, 0xff, a);
  33994. mp_set(d, 0xfe);
  33995. mp_lshd(d, 2);
  33996. ret = mp_div(a, d, r, rem);
  33997. if (ret != MP_OKAY)
  33998. return -13051;
  33999. mp_mul(r, d, d);
  34000. mp_add(rem, d, d);
  34001. if (mp_cmp(a, d) != MP_EQ)
  34002. return -13052;
  34003. /* Force (hi | lo) / d to be (d | 0) / d which will would not fit in
  34004. * a digit. So mp_div must detect and handle.
  34005. * For example: 0x800000 / 0x8001, DIGIT_BIT = 8
  34006. */
  34007. mp_set(a, 1);
  34008. mp_mul_2d(a, DIGIT_BIT * 3 - 1, a);
  34009. mp_set(d, 1);
  34010. mp_mul_2d(d, DIGIT_BIT * 2 - 1, d);
  34011. mp_add_d(d, 1, d);
  34012. ret = mp_div(a, d, r, rem);
  34013. if (ret != MP_OKAY)
  34014. return -13053;
  34015. return 0;
  34016. }
  34017. #endif
  34018. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_DH) || !defined(NO_DSA)) && \
  34019. !defined(WC_NO_RNG)
  34020. static int mp_test_prime(mp_int* a, WC_RNG* rng)
  34021. {
  34022. int ret;
  34023. int res;
  34024. ret = mp_rand_prime(a, 1, rng, NULL);
  34025. #if defined(WOLFSSL_SP_MATH_ALL)
  34026. if (ret != 0)
  34027. return -13060;
  34028. #else
  34029. if (ret != MP_VAL)
  34030. return -13060;
  34031. #endif
  34032. #ifndef WOLFSSL_SP_MATH
  34033. ret = mp_rand_prime(a, -5, rng, NULL);
  34034. if (ret != 0)
  34035. return -13061;
  34036. #endif
  34037. ret = mp_prime_is_prime(a, 1, &res);
  34038. if (ret != MP_OKAY)
  34039. return -13062;
  34040. #ifndef WOLFSSL_SP_MATH
  34041. if (res != MP_YES)
  34042. return -13063;
  34043. #else
  34044. if (res != MP_NO)
  34045. return -13063;
  34046. #endif
  34047. ret = mp_prime_is_prime(a, 0, &res);
  34048. if (ret != MP_VAL)
  34049. return -13064;
  34050. ret = mp_prime_is_prime(a, -1, &res);
  34051. if (ret != MP_VAL)
  34052. return -13065;
  34053. ret = mp_prime_is_prime(a, 257, &res);
  34054. if (ret != MP_VAL)
  34055. return -13066;
  34056. mp_set(a, 1);
  34057. ret = mp_prime_is_prime(a, 1, &res);
  34058. if (ret != MP_OKAY)
  34059. return -13067;
  34060. if (res != MP_NO)
  34061. return -13068;
  34062. ret = mp_prime_is_prime_ex(a, 1, &res, rng);
  34063. if (ret != MP_OKAY)
  34064. return -13069;
  34065. if (res != MP_NO)
  34066. return -13070;
  34067. mp_set(a, 2);
  34068. ret = mp_prime_is_prime(a, 1, &res);
  34069. if (ret != MP_OKAY)
  34070. return -13071;
  34071. if (res != MP_YES)
  34072. return -13072;
  34073. ret = mp_prime_is_prime_ex(a, 1, &res, rng);
  34074. if (ret != MP_OKAY)
  34075. return -13073;
  34076. if (res != MP_YES)
  34077. return -13074;
  34078. mp_set(a, 0xfb);
  34079. ret = mp_prime_is_prime(a, 1, &res);
  34080. if (ret != MP_OKAY)
  34081. return -13075;
  34082. if (res != MP_YES)
  34083. return -13076;
  34084. ret = mp_prime_is_prime_ex(a, 1, &res, rng);
  34085. if (ret != MP_OKAY)
  34086. return -13077;
  34087. if (res != MP_YES)
  34088. return -13078;
  34089. mp_set(a, 0x6);
  34090. ret = mp_prime_is_prime(a, 1, &res);
  34091. if (ret != MP_OKAY)
  34092. return -13079;
  34093. if (res != MP_NO)
  34094. return -13080;
  34095. ret = mp_prime_is_prime_ex(a, 1, &res, rng);
  34096. if (ret != MP_OKAY)
  34097. return -13081;
  34098. if (res != MP_NO)
  34099. return -13082;
  34100. mp_set_int(a, 0x655 * 0x65b);
  34101. ret = mp_prime_is_prime(a, 10, &res);
  34102. if (ret != MP_OKAY)
  34103. return -13083;
  34104. if (res != MP_NO)
  34105. return -13084;
  34106. ret = mp_prime_is_prime_ex(a, 10, &res, rng);
  34107. if (ret != MP_OKAY)
  34108. return -13085;
  34109. if (res != MP_NO)
  34110. return -13086;
  34111. return 0;
  34112. }
  34113. #endif
  34114. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(WC_RSA_BLINDING)
  34115. static int mp_test_lcm_gcd(mp_int* a, mp_int* b, mp_int* r, mp_int* exp,
  34116. WC_RNG* rng)
  34117. {
  34118. int ret;
  34119. int i;
  34120. WOLFSSL_SMALL_STACK_STATIC const int kat[][3] = {
  34121. { 1, 1, 1 }, { 2, 1, 2 }, { 1, 2, 2 }, { 2, 4, 4 }, { 4, 2, 4 },
  34122. { 12, 56, 168 }, { 56, 12, 168 }
  34123. };
  34124. (void)exp;
  34125. mp_set(a, 0);
  34126. mp_set(b, 1);
  34127. ret = mp_lcm(a, a, r);
  34128. if (ret != MP_VAL)
  34129. return -13090;
  34130. ret = mp_lcm(a, b, r);
  34131. if (ret != MP_VAL)
  34132. return -13091;
  34133. ret = mp_lcm(b, a, r);
  34134. if (ret != MP_VAL)
  34135. return -13092;
  34136. for (i = 0; i < (int)(sizeof(kat) / sizeof(*kat)); i++) {
  34137. mp_set(a, kat[i][0]);
  34138. mp_set(b, kat[i][1]);
  34139. ret = mp_lcm(a, b, r);
  34140. if (ret != MP_OKAY)
  34141. return -13093;
  34142. mp_set(exp, kat[i][2]);
  34143. if (mp_cmp(r, exp) != MP_EQ)
  34144. return -13094;
  34145. }
  34146. (void)rng;
  34147. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_DH) || !defined(NO_DSA)) && \
  34148. !defined(WC_NO_RNG)
  34149. if (mp_rand_prime(a, 20, rng, NULL) != MP_OKAY)
  34150. return -13095;
  34151. if (mp_rand_prime(b, 20, rng, NULL) != MP_OKAY)
  34152. return -13096;
  34153. if (mp_mul(a, b, exp) != MP_OKAY)
  34154. return -13097;
  34155. ret = mp_lcm(a, b, r);
  34156. if (ret != MP_OKAY)
  34157. return -13098;
  34158. if (mp_cmp(r, exp) != MP_EQ)
  34159. return -13099;
  34160. ret = mp_lcm(b, a, r);
  34161. if (ret != MP_OKAY)
  34162. return -13100;
  34163. if (mp_cmp(r, exp) != MP_EQ)
  34164. return -13101;
  34165. #endif
  34166. mp_set(a, 11);
  34167. mp_zero(b);
  34168. ret = mp_gcd(a, b, r);
  34169. if (ret != MP_OKAY)
  34170. return -13102;
  34171. if (mp_cmp_d(r, 11) != MP_EQ)
  34172. return -13103;
  34173. ret = mp_gcd(b, a, r);
  34174. if (ret != MP_OKAY)
  34175. return -13104;
  34176. if (mp_cmp_d(r, 11) != MP_EQ)
  34177. return -13105;
  34178. ret = mp_gcd(b, b, r);
  34179. if (ret != MP_VAL)
  34180. return -13106;
  34181. return 0;
  34182. }
  34183. #endif
  34184. #if (!defined(WOLFSSL_SP_MATH) && !defined(USE_FAST_MATH)) || \
  34185. defined(WOLFSSL_SP_MATH_ALL)
  34186. static int mp_test_mod_2d(mp_int* a, mp_int* r, mp_int* t, WC_RNG* rng)
  34187. {
  34188. int ret;
  34189. int i;
  34190. int j;
  34191. mp_set(a, 10);
  34192. ret = mp_mod_2d(a, 0, r);
  34193. if (ret != MP_OKAY)
  34194. return -13110;
  34195. if (!mp_iszero(r))
  34196. return -13111;
  34197. ret = mp_mod_2d(a, 1, r);
  34198. if (ret != MP_OKAY)
  34199. return -13112;
  34200. if (!mp_iszero(r))
  34201. return -13113;
  34202. ret = mp_mod_2d(a, 2, r);
  34203. if (ret != MP_OKAY)
  34204. return -13114;
  34205. if (mp_cmp_d(r, 2))
  34206. return -13115;
  34207. for (i = 2; i < 20; i++) {
  34208. ret = randNum(a, i, rng, NULL);
  34209. if (ret != 0)
  34210. return -13116;
  34211. for (j = 1; j <= mp_count_bits(a); j++) {
  34212. /* Get top part */
  34213. ret = mp_div_2d(a, j, t, NULL);
  34214. if (ret != 0)
  34215. return -13117;
  34216. ret = mp_mul_2d(t, j, t);
  34217. if (ret != 0)
  34218. return -13118;
  34219. /* Get bottom part */
  34220. ret = mp_mod_2d(a, j, r);
  34221. if (ret != 0)
  34222. return -13119;
  34223. /* Reassemble */
  34224. ret = mp_add(t, r, r);
  34225. if (ret != 0)
  34226. return -13120;
  34227. if (mp_cmp(a, r) != MP_EQ)
  34228. return -13121;
  34229. }
  34230. }
  34231. #if !defined(WOLFSSL_SP_MATH) && defined(WOLFSSL_SP_INT_NEGATIVE)
  34232. /* Test negative value being moded. */
  34233. for (j = 0; j < 20; j++) {
  34234. ret = randNum(a, 2, rng, NULL);
  34235. if (ret != 0)
  34236. return -13122;
  34237. a->sign = MP_NEG;
  34238. for (i = 1; i < DIGIT_BIT * 3 + 1; i++) {
  34239. ret = mp_mod_2d(a, i, r);
  34240. if (ret != 0)
  34241. return -13124;
  34242. mp_zero(t);
  34243. ret = mp_set_bit(t, i);
  34244. if (ret != 0)
  34245. return -13125;
  34246. ret = mp_mod(a, t, t);
  34247. if (ret != 0)
  34248. return -13126;
  34249. if (mp_cmp(r, t) != MP_EQ)
  34250. return -13127;
  34251. }
  34252. }
  34253. #endif
  34254. return 0;
  34255. }
  34256. #endif
  34257. #if (defined(HAVE_ECC) && defined(HAVE_COMP_KEY)) || \
  34258. (defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN))
  34259. static int mp_test_mod_d(mp_int* a)
  34260. {
  34261. int ret;
  34262. mp_digit r;
  34263. if (mp_set(a, 1) != MP_OKAY)
  34264. return -13130;
  34265. ret = mp_mod_d(a, 0, &r);
  34266. if (ret != MP_VAL)
  34267. return -13131;
  34268. mp_zero(a);
  34269. ret = mp_mod_d(a, 1, &r);
  34270. if (ret != MP_OKAY)
  34271. return -13132;
  34272. ret = mp_mod_d(a, 3, &r);
  34273. if (ret != MP_OKAY)
  34274. return -13133;
  34275. ret = mp_mod_d(a, 5, &r);
  34276. if (ret != MP_OKAY)
  34277. return -13134;
  34278. return 0;
  34279. }
  34280. #endif
  34281. static int mp_test_mul_sqr(mp_int* a, mp_int* b, mp_int* r1, mp_int* r2,
  34282. WC_RNG* rng)
  34283. {
  34284. int ret;
  34285. int i;
  34286. for (i = 1; i < 16; i++) {
  34287. ret = randNum(a, i, rng, NULL);
  34288. if (ret != 0)
  34289. return -13140;
  34290. ret = mp_mul(a, a, r1);
  34291. if (ret != 0)
  34292. return -13141;
  34293. ret = mp_sqr(a, r2);
  34294. if (ret != 0)
  34295. return -13142;
  34296. if (mp_cmp(r1, r2) != MP_EQ)
  34297. return -13143;
  34298. }
  34299. ret = mp_set(b, 0);
  34300. if (ret != MP_OKAY)
  34301. return -13144;
  34302. ret = mp_mul(a, b, r1);
  34303. if (ret != MP_OKAY)
  34304. return -13145;
  34305. if (!mp_iszero(r1))
  34306. return -13146;
  34307. ret = mp_sqr(b, r1);
  34308. if (ret != MP_OKAY)
  34309. return -13147;
  34310. if (!mp_iszero(r1))
  34311. return -13148;
  34312. #ifdef WOLFSSL_SP_MATH_ALL
  34313. ret = mp_set(a, 1);
  34314. if (ret != MP_OKAY)
  34315. return -13149;
  34316. i = (SP_INT_DIGITS / 2) + 1;
  34317. ret = mp_mul_2d(a, i * SP_WORD_SIZE - 1, a);
  34318. if (ret != MP_OKAY)
  34319. return -13150;
  34320. ret = mp_set(b, 1);
  34321. if (ret != MP_OKAY)
  34322. return -13151;
  34323. ret = mp_mul_2d(b, (SP_INT_DIGITS - 1 - i) * SP_WORD_SIZE - 1, b);
  34324. if (ret != MP_OKAY)
  34325. return -13152;
  34326. ret = mp_mul(a, b, r1);
  34327. if (ret != MP_OKAY)
  34328. return -13153;
  34329. ret = mp_mul(a, a, r1);
  34330. if (ret == MP_OKAY)
  34331. return -13154;
  34332. ret = mp_sqr(a, r1);
  34333. if (ret == MP_OKAY)
  34334. return -13155;
  34335. ret = mp_sqr(b, r1);
  34336. if (ret != MP_OKAY)
  34337. return -13156;
  34338. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH) || \
  34339. (defined(HAVE_ECC) && defined(FP_ECC))
  34340. ret = mp_mulmod(a, b, b, r1);
  34341. if (ret != MP_OKAY)
  34342. return -13157;
  34343. ret = mp_mulmod(a, a, b, r1);
  34344. if (ret == MP_OKAY)
  34345. return -13158;
  34346. #if defined(HAVE_ECC) && (defined(ECC_SHAMIR) || defined(FP_ECC))
  34347. ret = mp_sqrmod(a, b, r1);
  34348. if (ret == MP_OKAY)
  34349. return -13159;
  34350. ret = mp_sqrmod(b, a, r1);
  34351. if (ret != MP_OKAY)
  34352. return -13160;
  34353. #endif /* HAVE_ECC && (ECC_SHAMIR || FP_ECC) */
  34354. #endif /* WOLFSSL_SP_MATH_ALL || WOLFSSL_HAVE_SP_DH || (HAVE_ECC && FP_ECC) */
  34355. #endif /* WOLFSSL_SP_MATH_ALL */
  34356. return 0;
  34357. }
  34358. #if !defined(NO_RSA) || defined(HAVE_ECC) || !defined(NO_DSA) || \
  34359. defined(OPENSSL_EXTRA)
  34360. static int mp_test_invmod(mp_int* a, mp_int* m, mp_int* r)
  34361. {
  34362. int ret;
  34363. mp_set(a, 0);
  34364. mp_set(m, 1);
  34365. ret = mp_invmod(a, m, r);
  34366. if (ret != MP_VAL)
  34367. return -13170;
  34368. ret = mp_invmod(m, a, r);
  34369. if (ret != MP_VAL)
  34370. return -13171;
  34371. mp_set(a, 2);
  34372. mp_set(m, 4);
  34373. ret = mp_invmod(a, m, r);
  34374. if (ret != MP_VAL)
  34375. return -13172;
  34376. mp_set(a, 1);
  34377. mp_set(m, 4);
  34378. ret = mp_invmod(a, m, r);
  34379. if (ret != MP_OKAY)
  34380. return -13173;
  34381. if (!mp_isone(r))
  34382. return -13174;
  34383. mp_set(a, 3);
  34384. mp_set(m, 4);
  34385. ret = mp_invmod(a, m, r);
  34386. if (ret != MP_OKAY)
  34387. return -13175;
  34388. if (mp_cmp_d(r, 3))
  34389. return -13176;
  34390. mp_set(a, 3);
  34391. mp_set(m, 5);
  34392. ret = mp_invmod(a, m, r);
  34393. if (ret != MP_OKAY)
  34394. return -13177;
  34395. #if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  34396. /* Maximum 'a' */
  34397. mp_set(a, 0);
  34398. mp_set_bit(a, (r->size / 2)* SP_WORD_SIZE - 1);
  34399. mp_sub_d(a, 1, a);
  34400. /* Modulus too big. */
  34401. mp_set(m, 0);
  34402. mp_set_bit(m, (r->size / 2) * SP_WORD_SIZE);
  34403. ret = mp_invmod(a, m, r);
  34404. if (ret != MP_VAL)
  34405. return -13178;
  34406. /* Maximum modulus - even. */
  34407. mp_set(m, 0);
  34408. mp_set_bit(m, (r->size / 2) * SP_WORD_SIZE - 1);
  34409. ret = mp_invmod(a, m, r);
  34410. if (ret != MP_OKAY)
  34411. return -13179;
  34412. #endif
  34413. #if !defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_INT_NEGATIVE)
  34414. mp_read_radix(a, "-3", 16);
  34415. ret = mp_invmod(a, m, r);
  34416. if (ret != MP_OKAY)
  34417. return -13180;
  34418. #endif
  34419. #if defined(WOLFSSL_SP_MATH_ALL) && defined(HAVE_ECC)
  34420. mp_set(a, 0);
  34421. mp_set(m, 3);
  34422. ret = mp_invmod_mont_ct(a, m, r, 1);
  34423. if (ret != MP_VAL)
  34424. return -13190;
  34425. mp_set(a, 1);
  34426. mp_set(m, 0);
  34427. ret = mp_invmod_mont_ct(a, m, r, 1);
  34428. if (ret != MP_VAL)
  34429. return -13191;
  34430. mp_set(a, 1);
  34431. mp_set(m, 1);
  34432. ret = mp_invmod_mont_ct(a, m, r, 1);
  34433. if (ret != MP_VAL)
  34434. return -13192;
  34435. mp_set(a, 1);
  34436. mp_set(m, 2);
  34437. ret = mp_invmod_mont_ct(a, m, r, 1);
  34438. if (ret != MP_VAL)
  34439. return -13193;
  34440. mp_set(a, 1);
  34441. mp_set(m, 3);
  34442. ret = mp_invmod_mont_ct(a, m, r, 1);
  34443. if (ret != MP_OKAY)
  34444. return -13194;
  34445. #endif
  34446. return 0;
  34447. }
  34448. #endif /* !NO_RSA || HAVE_ECC || !NO_DSA || OPENSSL_EXTRA */
  34449. #if !defined(NO_RSA) || !defined(NO_DSA) || !defined(NO_DH) || \
  34450. (defined(HAVE_ECC) && defined(HAVE_COMP_KEY)) || defined(OPENSSL_EXTRA)
  34451. static int mp_test_exptmod(mp_int* b, mp_int* e, mp_int* m, mp_int* r)
  34452. {
  34453. int ret;
  34454. mp_set(b, 0x2);
  34455. mp_set(e, 0x3);
  34456. mp_set(m, 0x0);
  34457. ret = mp_exptmod_ex(b, e, 1, m, r);
  34458. if (ret != MP_VAL)
  34459. return -13190;
  34460. ret = mp_exptmod_nct(b, e, m, r);
  34461. if (ret != MP_VAL)
  34462. return -13191;
  34463. mp_set(b, 0x2);
  34464. mp_set(e, 0x3);
  34465. mp_set(m, 0x1);
  34466. ret = mp_exptmod_ex(b, e, 1, m, r);
  34467. if (ret != MP_OKAY)
  34468. return -13192;
  34469. if (!mp_iszero(r))
  34470. return -13193;
  34471. ret = mp_exptmod_nct(b, e, m, r);
  34472. if (ret != MP_OKAY)
  34473. return -13194;
  34474. if (!mp_iszero(r))
  34475. return -13195;
  34476. mp_set(b, 0x2);
  34477. mp_set(e, 0x0);
  34478. mp_set(m, 0x7);
  34479. ret = mp_exptmod_ex(b, e, 1, m, r);
  34480. if (ret != MP_OKAY)
  34481. return -13196;
  34482. if (!mp_isone(r))
  34483. return -13197;
  34484. ret = mp_exptmod_nct(b, e, m, r);
  34485. if (ret != MP_OKAY)
  34486. return -13198;
  34487. if (!mp_isone(r))
  34488. return -13199;
  34489. mp_set(b, 0x0);
  34490. mp_set(e, 0x3);
  34491. mp_set(m, 0x7);
  34492. ret = mp_exptmod_ex(b, e, 1, m, r);
  34493. if (ret != MP_OKAY)
  34494. return -13200;
  34495. if (!mp_iszero(r))
  34496. return -13201;
  34497. ret = mp_exptmod_nct(b, e, m, r);
  34498. if (ret != MP_OKAY)
  34499. return -13202;
  34500. if (!mp_iszero(r))
  34501. return -13203;
  34502. mp_set(b, 0x10);
  34503. mp_set(e, 0x3);
  34504. mp_set(m, 0x7);
  34505. ret = mp_exptmod_ex(b, e, 1, m, r);
  34506. if (ret != MP_OKAY)
  34507. return -13204;
  34508. ret = mp_exptmod_nct(b, e, m, r);
  34509. if (ret != MP_OKAY)
  34510. return -13205;
  34511. mp_set(b, 0x7);
  34512. mp_set(e, 0x3);
  34513. mp_set(m, 0x7);
  34514. ret = mp_exptmod_ex(b, e, 1, m, r);
  34515. if (ret != MP_OKAY)
  34516. return -13206;
  34517. if (!mp_iszero(r))
  34518. return -13207;
  34519. ret = mp_exptmod_nct(b, e, m, r);
  34520. if (ret != MP_OKAY)
  34521. return -13208;
  34522. if (!mp_iszero(r))
  34523. return -13209;
  34524. #ifndef WOLFSSL_SP_MATH
  34525. mp_set(b, 0x01);
  34526. mp_mul_2d(b, DIGIT_BIT, b);
  34527. mp_add_d(b, 1, b);
  34528. mp_set(e, 0x3);
  34529. mp_copy(b, m);
  34530. ret = mp_exptmod_ex(b, e, 1, m, r);
  34531. if (ret != MP_OKAY)
  34532. return -13210;
  34533. if (!mp_iszero(r))
  34534. return -13211;
  34535. ret = mp_exptmod_nct(b, e, m, r);
  34536. if (ret != MP_OKAY)
  34537. return -13212;
  34538. if (!mp_iszero(r))
  34539. return -13213;
  34540. #endif
  34541. mp_set(b, 0x2);
  34542. mp_set(e, 0x3);
  34543. mp_set(m, 0x7);
  34544. ret = mp_exptmod_ex(b, e, 1, m, r);
  34545. if (ret != MP_OKAY)
  34546. return -13214;
  34547. ret = mp_exptmod_nct(b, e, m, r);
  34548. if (ret != MP_OKAY)
  34549. return -13215;
  34550. #ifdef WOLFSSL_SP_MATH_ALL
  34551. mp_set(b, 0x2);
  34552. mp_set(e, 0x3);
  34553. mp_set(m, 0x01);
  34554. mp_mul_2d(m, SP_WORD_SIZE * SP_INT_DIGITS / 2, m);
  34555. mp_add_d(m, 0x01, m);
  34556. ret = mp_exptmod_ex(b, e, 1, m, r);
  34557. if (ret != MP_VAL)
  34558. return -13216;
  34559. ret = mp_exptmod_nct(b, e, m, r);
  34560. if (ret != MP_VAL)
  34561. return -13217;
  34562. #endif
  34563. return 0;
  34564. }
  34565. #endif /* !NO_RSA || !NO_DSA || !NO_DH || (HAVE_ECC && HAVE_COMP_KEY) ||
  34566. * OPENSSL_EXTRA */
  34567. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH) || \
  34568. defined(HAVE_ECC) || (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY))
  34569. static int mp_test_mont(mp_int* a, mp_int* m, mp_int* n, mp_int* r, WC_RNG* rng)
  34570. {
  34571. int ret;
  34572. mp_digit mp;
  34573. static int exp[] = { 7, 8, 16, 27, 32, 64,
  34574. 127, 128, 255, 256,
  34575. #if defined(SP_WORD_SIZE) && SP_WORD_SIZE > 8
  34576. 383, 384, 2033, 2048
  34577. #endif
  34578. };
  34579. static mp_digit sub[] = { 0x01, 0x05, 0x0f, 0x27, 0x05, 0x3b,
  34580. 0x01, 0x9f, 0x13, 0xbd,
  34581. #if defined(SP_WORD_SIZE) && SP_WORD_SIZE > 8
  34582. 0x1f, 0x13d, 0x45, 0x615
  34583. #endif
  34584. };
  34585. int bits[] = { 256, 384,
  34586. #if defined(SP_INT_MAX_BITS) && SP_INT_MAX_BITS > 4096
  34587. 2048,
  34588. #endif
  34589. #if defined(SP_INT_MAX_BITS) && SP_INT_MAX_BITS > 6144
  34590. 3072
  34591. #endif
  34592. };
  34593. int i;
  34594. int j;
  34595. for (i = 0; i < (int)(sizeof(exp) / sizeof(*exp)); i++) {
  34596. if (exp[i] >= DIGIT_BIT)
  34597. continue;
  34598. mp_zero(m);
  34599. ret = mp_set_bit(m, exp[i]);
  34600. if (ret != MP_OKAY)
  34601. return -13220;
  34602. ret = mp_sub_d(m, sub[i], m);
  34603. if (ret != MP_OKAY)
  34604. return -13221;
  34605. ret = mp_montgomery_setup(m, &mp);
  34606. if (ret != MP_OKAY)
  34607. return -13222;
  34608. ret = mp_montgomery_calc_normalization(n, m);
  34609. if (ret != MP_OKAY)
  34610. return -13223;
  34611. for (j = 0; j < 10; j++) {
  34612. ret = randNum(a, (exp[i] + DIGIT_BIT - 1) / DIGIT_BIT, rng, NULL);
  34613. if (ret != 0)
  34614. return -13224;
  34615. ret = mp_mod(a, m, a);
  34616. if (ret != 0)
  34617. return -13225;
  34618. /* r = a * a */
  34619. ret = mp_sqrmod(a, m, r);
  34620. if (ret != MP_OKAY)
  34621. return -13226;
  34622. /* Convert to Montgomery form = a*n */
  34623. ret = mp_mulmod(a, n, m, a);
  34624. if (ret != MP_OKAY)
  34625. return -13227;
  34626. /* a*a mod m == ((a*n) * (a*n)) / n / n */
  34627. ret = mp_sqr(a, a);
  34628. if (ret != MP_OKAY)
  34629. return -13228;
  34630. ret = mp_montgomery_reduce(a, m, mp);
  34631. if (ret != MP_OKAY)
  34632. return -13229;
  34633. ret = mp_montgomery_reduce(a, m, mp);
  34634. if (ret != MP_OKAY)
  34635. return -13230;
  34636. if (mp_cmp(a, r) != MP_EQ)
  34637. return -13231;
  34638. }
  34639. }
  34640. /* Force carries. */
  34641. for (i = 0; i < (int)(sizeof(bits) / sizeof(*bits)); i++) {
  34642. /* a = 2^(bits*2) - 1 */
  34643. mp_zero(a);
  34644. mp_set_bit(a, bits[i] * 2);
  34645. mp_sub_d(a, 1, a);
  34646. /* m = 2^(bits) - 1 */
  34647. mp_zero(m);
  34648. mp_set_bit(m, bits[i]);
  34649. mp_sub_d(m, 1, m);
  34650. mp = 1;
  34651. /* result = r = 2^(bits) - 1 */
  34652. mp_zero(r);
  34653. mp_set_bit(r, bits[i]);
  34654. mp_sub_d(r, 1, r);
  34655. ret = mp_montgomery_reduce(a, m, mp);
  34656. if (ret != MP_OKAY)
  34657. return -13240;
  34658. /* Result is m or 0 if reduced to range of modulus. */
  34659. if (mp_cmp(a, r) != MP_EQ && mp_iszero(a) != MP_YES)
  34660. return -13241;
  34661. }
  34662. return 0;
  34663. }
  34664. #endif
  34665. WOLFSSL_TEST_SUBROUTINE int mp_test(void)
  34666. {
  34667. WC_RNG rng;
  34668. int ret;
  34669. #if defined(HAVE_ECC) || defined(WOLFSSL_KEY_GEN)
  34670. int i, j;
  34671. #ifndef WOLFSSL_SP_MATH
  34672. int k;
  34673. #endif
  34674. mp_digit d;
  34675. #endif
  34676. mp_int a, b, r1, r2, p;
  34677. ret = mp_init_multi(&a, &b, &r1, &r2, NULL, NULL);
  34678. if (ret != 0)
  34679. return -13300;
  34680. #ifdef WOLFSSL_SP_MATH_ALL
  34681. mp_init_copy(&p, &a);
  34682. #else
  34683. ret = mp_init(&p);
  34684. if (ret != 0)
  34685. return -13301;
  34686. #endif
  34687. #ifndef HAVE_FIPS
  34688. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  34689. #else
  34690. ret = wc_InitRng(&rng);
  34691. #endif
  34692. if (ret != 0)
  34693. goto done;
  34694. #if defined(HAVE_ECC) || defined(WOLFSSL_KEY_GEN)
  34695. mp_set_int(&a, 0);
  34696. if (a.used != 0 || a.dp[0] != 0)
  34697. return -13302;
  34698. for (j = 1; j <= MP_MAX_TEST_BYTE_LEN; j++) {
  34699. for (i = 0; i < 4 * j; i++) {
  34700. /* New values to use. */
  34701. ret = randNum(&p, j, &rng, NULL);
  34702. if (ret != 0)
  34703. return -13303;
  34704. ret = randNum(&a, j, &rng, NULL);
  34705. if (ret != 0)
  34706. return -13304;
  34707. ret = randNum(&b, j, &rng, NULL);
  34708. if (ret != 0)
  34709. return -13305;
  34710. ret = wc_RNG_GenerateBlock(&rng, (byte*)&d, sizeof(d));
  34711. if (ret != 0)
  34712. return -13306;
  34713. d &= MP_MASK;
  34714. #if !defined(WOLFSSL_SP_MATH) || (defined(HAVE_ECC) && \
  34715. (defined(ECC_SHAMIR) || defined(FP_ECC)))
  34716. /* Ensure sqrmod produce same result as mulmod. */
  34717. ret = mp_sqrmod(&a, &p, &r1);
  34718. if (ret != 0)
  34719. return -13307;
  34720. ret = mp_mulmod(&a, &a, &p, &r2);
  34721. if (ret != 0)
  34722. return -13308;
  34723. if (mp_cmp(&r1, &r2) != 0)
  34724. return -13309;
  34725. #endif
  34726. #if defined(WOLFSSL_SP_MATH_ALL) && !defined(WOLFSSL_RSA_VERIFY_ONLY)
  34727. #if defined(WOLFSSL_SP_MATH) || (defined(WOLFSSL_SP_MATH_ALL) && \
  34728. !defined(WOLFSSL_SP_INT_NEGATIVE))
  34729. ret = mp_addmod(&a, &b, &p, &r1);
  34730. if (ret != 0)
  34731. return -13310;
  34732. ret = mp_submod(&r1, &b, &p, &r2);
  34733. if (ret != 0)
  34734. return -13311;
  34735. ret = mp_mod(&a, &p, &r1);
  34736. if (ret != 0)
  34737. return -13312;
  34738. if (mp_cmp(&r1, &r2) != MP_EQ)
  34739. return -13313;
  34740. #else
  34741. /* Ensure add with mod produce same result as sub with mod. */
  34742. ret = mp_addmod(&a, &b, &p, &r1);
  34743. if (ret != 0)
  34744. return -13314;
  34745. b.sign ^= 1;
  34746. ret = mp_submod(&a, &b, &p, &r2);
  34747. if (ret != 0)
  34748. return -13315;
  34749. if (mp_cmp(&r1, &r2) != 0)
  34750. return -13316;
  34751. #endif
  34752. #endif
  34753. /* Ensure add digit produce same result as sub digit. */
  34754. ret = mp_add_d(&a, d, &r1);
  34755. if (ret != 0)
  34756. return -13317;
  34757. ret = mp_sub_d(&r1, d, &r2);
  34758. if (ret != 0)
  34759. return -13318;
  34760. if (mp_cmp(&a, &r2) != 0)
  34761. return -13319;
  34762. /* Invert - if p is even it will use the slow impl.
  34763. * - if p and a are even it will fail.
  34764. */
  34765. ret = mp_invmod(&a, &p, &r1);
  34766. if (ret != 0 && ret != MP_VAL)
  34767. return -13320;
  34768. ret = 0;
  34769. #ifndef WOLFSSL_SP_MATH
  34770. /* Shift up and down number all bits in a digit. */
  34771. for (k = 0; k < DIGIT_BIT; k++) {
  34772. mp_mul_2d(&a, k, &r1);
  34773. mp_div_2d(&r1, k, &r2, &p);
  34774. if (mp_cmp(&a, &r2) != 0)
  34775. return -13321;
  34776. if (!mp_iszero(&p))
  34777. return -13322;
  34778. mp_rshb(&r1, k);
  34779. if (mp_cmp(&a, &r1) != 0)
  34780. return -13323;
  34781. }
  34782. #endif
  34783. }
  34784. }
  34785. #if DIGIT_BIT >= 32
  34786. /* Check that setting a 32-bit digit works. */
  34787. d &= 0xffffffffU;
  34788. mp_set_int(&a, d);
  34789. if (a.used != 1 || a.dp[0] != d)
  34790. return -13324;
  34791. #endif
  34792. /* Check setting a bit and testing a bit works. */
  34793. for (i = 0; i < MP_MAX_TEST_BYTE_LEN * 8; i++) {
  34794. mp_zero(&a);
  34795. mp_set_bit(&a, i);
  34796. if (!mp_is_bit_set(&a, i))
  34797. return -13325;
  34798. }
  34799. #endif
  34800. #if defined(HAVE_ECC) && defined(HAVE_COMP_KEY)
  34801. mp_zero(&a);
  34802. i = mp_cnt_lsb(&a);
  34803. if (i != 0)
  34804. return -13326;
  34805. mp_set(&a, 1);
  34806. i = mp_cnt_lsb(&a);
  34807. if (i != 0)
  34808. return -13327;
  34809. #endif
  34810. #if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  34811. if ((ret = mp_test_param(&a, &b, &r1, &rng)) != 0)
  34812. return ret;
  34813. #endif
  34814. #if defined(WOLFSSL_SP_MATH_ALL) || !defined(USE_FAST_MATH)
  34815. if ((ret = mp_test_div_3(&a, &r1, &rng)) != 0)
  34816. return ret;
  34817. #endif
  34818. #if (defined(WOLFSSL_SP_MATH_ALL) && !defined(NO_RSA) && \
  34819. !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
  34820. (!defined WOLFSSL_SP_MATH && !defined(WOLFSSL_SP_MATH_ALL) && \
  34821. (defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)))
  34822. if ((ret = mp_test_radix_10(&a, &r1, &rng)) != 0)
  34823. return ret;
  34824. #endif
  34825. #if defined(WOLFSSL_SP_MATH_ALL) || (!defined WOLFSSL_SP_MATH && \
  34826. defined(HAVE_ECC))
  34827. if ((ret = mp_test_radix_16(&a, &r1, &rng)) != 0)
  34828. return ret;
  34829. #endif
  34830. if ((ret = mp_test_shift(&a, &r1, &rng)) != 0)
  34831. return ret;
  34832. if ((ret = mp_test_add_sub_d(&a, &r1)) != 0)
  34833. return ret;
  34834. if ((ret = mp_test_read_to_bin(&a)) != 0)
  34835. return ret;
  34836. #if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  34837. if ((ret = mp_test_set_int(&a)) != 0)
  34838. return ret;
  34839. #endif
  34840. if ((ret = mp_test_cmp(&a, &r1)) != 0)
  34841. return ret;
  34842. #if !defined(NO_DH) || defined(HAVE_ECC) || !defined(WOLFSSL_RSA_VERIFY_ONLY)
  34843. if ((ret = mp_test_shbd(&a, &b, &rng)) != 0)
  34844. return ret;
  34845. #endif
  34846. #if !defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  34847. if ((ret = mp_test_set_is_bit(&a)) != 0)
  34848. return ret;
  34849. #endif
  34850. #if !defined(WOLFSSL_SP_MATH) && !defined(WOLFSSL_SP_MATH_ALL)
  34851. if ((ret = mp_test_div(&a, &b, &r1, &r2, &rng)) != 0)
  34852. return ret;
  34853. #endif
  34854. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_DH) || !defined(NO_DSA)) && \
  34855. !defined(WC_NO_RNG)
  34856. if ((ret = mp_test_prime(&a, &rng)) != 0)
  34857. return ret;
  34858. #endif
  34859. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(WC_RSA_BLINDING)
  34860. if ((ret = mp_test_lcm_gcd(&a, &b, &r1, &r2, &rng)) != 0)
  34861. return ret;
  34862. #endif
  34863. #if (!defined(WOLFSSL_SP_MATH) && !defined(USE_FAST_MATH)) || \
  34864. defined(WOLFSSL_SP_MATH_ALL)
  34865. if ((ret = mp_test_mod_2d(&a, &r1, &p, &rng)) != 0)
  34866. return ret;
  34867. #endif
  34868. #if (defined(HAVE_ECC) && defined(HAVE_COMP_KEY)) || \
  34869. (defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN))
  34870. if ((ret = mp_test_mod_d(&a)) != 0)
  34871. return ret;
  34872. #endif
  34873. if ((ret = mp_test_mul_sqr(&a, &b, &r1, &r2, &rng)) != 0)
  34874. return ret;
  34875. #if !defined(NO_RSA) || defined(HAVE_ECC) || !defined(NO_DSA) || \
  34876. defined(OPENSSL_EXTRA)
  34877. if ((ret = mp_test_invmod(&a, &b, &r1)) != 0)
  34878. return ret;
  34879. #endif
  34880. #if !defined(NO_RSA) || !defined(NO_DSA) || !defined(NO_DH) || \
  34881. (defined(HAVE_ECC) && defined(HAVE_COMP_KEY)) || defined(OPENSSL_EXTRA)
  34882. if ((ret = mp_test_exptmod(&a, &b, &r1, &r2)) != 0)
  34883. return ret;
  34884. #endif
  34885. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH) || \
  34886. defined(HAVE_ECC) || (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY))
  34887. if ((ret = mp_test_mont(&a, &b, &r1, &r2, &rng)) != 0)
  34888. return ret;
  34889. #endif
  34890. done:
  34891. mp_clear(&p);
  34892. mp_clear(&r2);
  34893. mp_clear(&r1);
  34894. mp_clear(&b);
  34895. mp_clear(&a);
  34896. wc_FreeRng(&rng);
  34897. return ret;
  34898. }
  34899. #endif
  34900. #if defined(WOLFSSL_PUBLIC_MP) && defined(WOLFSSL_KEY_GEN)
  34901. typedef struct pairs_t {
  34902. const unsigned char* coeff;
  34903. int coeffSz;
  34904. int exp;
  34905. } pairs_t;
  34906. /*
  34907. n =p1p2p3, where pi = ki(p1-1)+1 with (k2,k3) = (173,293)
  34908. p1 = 2^192 * 0x000000000000e24fd4f6d6363200bf2323ec46285cac1d3a
  34909. + 2^0 * 0x0b2488b0c29d96c5e67f8bec15b54b189ae5636efe89b45b
  34910. */
  34911. static const unsigned char c192a[] =
  34912. {
  34913. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe2, 0x4f,
  34914. 0xd4, 0xf6, 0xd6, 0x36, 0x32, 0x00, 0xbf, 0x23,
  34915. 0x23, 0xec, 0x46, 0x28, 0x5c, 0xac, 0x1d, 0x3a
  34916. };
  34917. static const unsigned char c0a[] =
  34918. {
  34919. 0x0b, 0x24, 0x88, 0xb0, 0xc2, 0x9d, 0x96, 0xc5,
  34920. 0xe6, 0x7f, 0x8b, 0xec, 0x15, 0xb5, 0x4b, 0x18,
  34921. 0x9a, 0xe5, 0x63, 0x6e, 0xfe, 0x89, 0xb4, 0x5b
  34922. };
  34923. static const pairs_t ecPairsA[] =
  34924. {
  34925. {c192a, sizeof(c192a), 192},
  34926. {c0a, sizeof(c0a), 0}
  34927. };
  34928. static const int kA[] = {173, 293};
  34929. static const unsigned char controlPrime[] = {
  34930. 0xe1, 0x76, 0x45, 0x80, 0x59, 0xb6, 0xd3, 0x49,
  34931. 0xdf, 0x0a, 0xef, 0x12, 0xd6, 0x0f, 0xf0, 0xb7,
  34932. 0xcb, 0x2a, 0x37, 0xbf, 0xa7, 0xf8, 0xb5, 0x4d,
  34933. 0xf5, 0x31, 0x35, 0xad, 0xe4, 0xa3, 0x94, 0xa1,
  34934. 0xdb, 0xf1, 0x96, 0xad, 0xb5, 0x05, 0x64, 0x85,
  34935. 0x83, 0xfc, 0x1b, 0x5b, 0x29, 0xaa, 0xbe, 0xf8,
  34936. 0x26, 0x3f, 0x76, 0x7e, 0xad, 0x1c, 0xf0, 0xcb,
  34937. 0xd7, 0x26, 0xb4, 0x1b, 0x05, 0x8e, 0x56, 0x86,
  34938. 0x7e, 0x08, 0x62, 0x21, 0xc1, 0x86, 0xd6, 0x47,
  34939. 0x79, 0x3e, 0xb7, 0x5d, 0xa4, 0xc6, 0x3a, 0xd7,
  34940. 0xb1, 0x74, 0x20, 0xf6, 0x50, 0x97, 0x41, 0x04,
  34941. 0x53, 0xed, 0x3f, 0x26, 0xd6, 0x6f, 0x91, 0xfa,
  34942. 0x68, 0x26, 0xec, 0x2a, 0xdc, 0x9a, 0xf1, 0xe7,
  34943. 0xdc, 0xfb, 0x73, 0xf0, 0x79, 0x43, 0x1b, 0x21,
  34944. 0xa3, 0x59, 0x04, 0x63, 0x52, 0x07, 0xc9, 0xd7,
  34945. 0xe6, 0xd1, 0x1b, 0x5d, 0x5e, 0x96, 0xfa, 0x53
  34946. };
  34947. static const unsigned char testOne[] = { 1 };
  34948. static int GenerateNextP(mp_int* p1, mp_int* p2, int k)
  34949. {
  34950. int ret;
  34951. #ifdef WOLFSSL_SMALL_STACK
  34952. mp_int *ki = (mp_int *)XMALLOC(sizeof(*ki), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34953. if (ki == NULL)
  34954. return MEMORY_E;
  34955. #else
  34956. mp_int ki[1];
  34957. #endif
  34958. ret = mp_init(ki);
  34959. if (ret == 0)
  34960. ret = mp_set(ki, k);
  34961. if (ret == 0)
  34962. ret = mp_sub_d(p1, 1, p2);
  34963. if (ret == 0)
  34964. ret = mp_mul(p2, ki, p2);
  34965. if (ret == 0)
  34966. ret = mp_add_d(p2, 1, p2);
  34967. mp_clear(ki);
  34968. #ifdef WOLFSSL_SMALL_STACK
  34969. XFREE(ki, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34970. #endif
  34971. return ret;
  34972. }
  34973. static int GenerateP(mp_int* p1, mp_int* p2, mp_int* p3,
  34974. const pairs_t* ecPairs, int ecPairsSz,
  34975. const int* k)
  34976. {
  34977. #ifdef WOLFSSL_SMALL_STACK
  34978. mp_int *x = NULL, *y = NULL;
  34979. #else
  34980. mp_int x[1], y[1];
  34981. #endif
  34982. int ret, i;
  34983. #ifdef WOLFSSL_SMALL_STACK
  34984. if (((x = (mp_int *)XMALLOC(sizeof(*x), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER)) == NULL) ||
  34985. ((y = (mp_int *)XMALLOC(sizeof(*x), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER)) == NULL)) {
  34986. ret = MEMORY_E;
  34987. goto out;
  34988. }
  34989. #endif
  34990. ret = mp_init_multi(x, y, NULL, NULL, NULL, NULL);
  34991. if (ret != 0) {
  34992. ret = MP_MEM;
  34993. goto out;
  34994. }
  34995. for (i = 0; ret == 0 && i < ecPairsSz; i++) {
  34996. ret = mp_read_unsigned_bin(x, ecPairs[i].coeff, ecPairs[i].coeffSz);
  34997. /* p1 = 2^exp */
  34998. if (ret == 0)
  34999. ret = mp_2expt(y, ecPairs[i].exp);
  35000. /* p1 = p1 * m */
  35001. if (ret == 0)
  35002. ret = mp_mul(x, y, x);
  35003. /* p1 += */
  35004. if (ret == 0)
  35005. ret = mp_add(p1, x, p1);
  35006. mp_zero(x);
  35007. mp_zero(y);
  35008. }
  35009. if (ret == 0)
  35010. ret = GenerateNextP(p1, p2, k[0]);
  35011. if (ret == 0)
  35012. ret = GenerateNextP(p1, p3, k[1]);
  35013. out:
  35014. #ifdef WOLFSSL_SMALL_STACK
  35015. if (x != NULL) {
  35016. mp_clear(x);
  35017. XFREE(x, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35018. }
  35019. if (y != NULL) {
  35020. mp_clear(y);
  35021. XFREE(y, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35022. }
  35023. #else
  35024. mp_clear(x);
  35025. mp_clear(y);
  35026. #endif
  35027. return ret;
  35028. }
  35029. WOLFSSL_TEST_SUBROUTINE int prime_test(void)
  35030. {
  35031. #ifdef WOLFSSL_SMALL_STACK
  35032. mp_int *n = (mp_int *)XMALLOC(sizeof *n, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER),
  35033. *p1 = (mp_int *)XMALLOC(sizeof *p1, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER),
  35034. *p2 = (mp_int *)XMALLOC(sizeof *p2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER),
  35035. *p3 = (mp_int *)XMALLOC(sizeof *p3, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35036. #else
  35037. mp_int n[1],
  35038. p1[1],
  35039. p2[1],
  35040. p3[1];
  35041. #endif
  35042. int ret, isPrime = 0;
  35043. WC_RNG rng;
  35044. #ifdef WOLFSSL_SMALL_STACK
  35045. if ((n == NULL) ||
  35046. (p1 == NULL) ||
  35047. (p2 == NULL) ||
  35048. (p3 == NULL))
  35049. ERROR_OUT(MEMORY_E, out);
  35050. #endif
  35051. ret = wc_InitRng(&rng);
  35052. if (ret == 0)
  35053. ret = mp_init_multi(n, p1, p2, p3, NULL, NULL);
  35054. if (ret == 0)
  35055. ret = GenerateP(p1, p2, p3,
  35056. ecPairsA, sizeof(ecPairsA) / sizeof(ecPairsA[0]), kA);
  35057. if (ret == 0)
  35058. ret = mp_mul(p1, p2, n);
  35059. if (ret == 0)
  35060. ret = mp_mul(n, p3, n);
  35061. if (ret != 0)
  35062. ERROR_OUT(-13400, out);
  35063. /* Check the old prime test using the number that false positives.
  35064. * This test result should indicate as not prime. */
  35065. ret = mp_prime_is_prime(n, 40, &isPrime);
  35066. if (ret != 0)
  35067. ERROR_OUT(-13401, out);
  35068. if (isPrime)
  35069. ERROR_OUT(-13402, out);
  35070. /* This test result should fail. It should indicate the value as prime. */
  35071. ret = mp_prime_is_prime(n, 8, &isPrime);
  35072. if (ret != 0)
  35073. ERROR_OUT(-13403, out);
  35074. if (!isPrime)
  35075. ERROR_OUT(-13404, out);
  35076. /* This test result should indicate the value as not prime. */
  35077. ret = mp_prime_is_prime_ex(n, 8, &isPrime, &rng);
  35078. if (ret != 0)
  35079. ERROR_OUT(-13405, out);
  35080. if (isPrime)
  35081. ERROR_OUT(-13406, out);
  35082. ret = mp_read_unsigned_bin(n, controlPrime, sizeof(controlPrime));
  35083. if (ret != 0)
  35084. ERROR_OUT(-13407, out);
  35085. /* This test result should indicate the value as prime. */
  35086. ret = mp_prime_is_prime_ex(n, 8, &isPrime, &rng);
  35087. if (ret != 0)
  35088. ERROR_OUT(-13408, out);
  35089. if (!isPrime)
  35090. ERROR_OUT(-13409, out);
  35091. /* This test result should indicate the value as prime. */
  35092. isPrime = -1;
  35093. ret = mp_prime_is_prime(n, 8, &isPrime);
  35094. if (ret != 0)
  35095. ERROR_OUT(-13410, out);
  35096. if (!isPrime)
  35097. ERROR_OUT(-13411, out);
  35098. ret = mp_read_unsigned_bin(n, testOne, sizeof(testOne));
  35099. if (ret != 0)
  35100. ERROR_OUT(-13412, out);
  35101. /* This test result should indicate the value as not prime. */
  35102. ret = mp_prime_is_prime_ex(n, 8, &isPrime, &rng);
  35103. if (ret != 0)
  35104. ERROR_OUT(-13413, out);
  35105. if (isPrime)
  35106. ERROR_OUT(-13414, out);
  35107. ret = mp_prime_is_prime(n, 8, &isPrime);
  35108. if (ret != 0)
  35109. ERROR_OUT(-13415, out);
  35110. if (isPrime)
  35111. ERROR_OUT(-13416, out);
  35112. ret = 0;
  35113. out:
  35114. #ifdef WOLFSSL_SMALL_STACK
  35115. if (n != NULL) {
  35116. mp_clear(n);
  35117. XFREE(n, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35118. }
  35119. if (p1 != NULL) {
  35120. mp_clear(p1);
  35121. XFREE(p1, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35122. }
  35123. if (p2 != NULL) {
  35124. mp_clear(p2);
  35125. XFREE(p2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35126. }
  35127. if (p3 != NULL) {
  35128. mp_clear(p3);
  35129. XFREE(p3, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35130. }
  35131. #else
  35132. mp_clear(p3);
  35133. mp_clear(p2);
  35134. mp_clear(p1);
  35135. mp_clear(n);
  35136. #endif
  35137. wc_FreeRng(&rng);
  35138. return ret;
  35139. }
  35140. #endif /* WOLFSSL_PUBLIC_MP */
  35141. #if defined(ASN_BER_TO_DER) && \
  35142. (defined(WOLFSSL_TEST_CERT) || defined(OPENSSL_EXTRA) || \
  35143. defined(OPENSSL_EXTRA_X509_SMALL))
  35144. /* wc_BerToDer is only public facing in the case of test cert or opensslextra */
  35145. typedef struct berDerTestData {
  35146. const byte *in;
  35147. word32 inSz;
  35148. const byte *out;
  35149. word32 outSz;
  35150. } berDerTestData;
  35151. WOLFSSL_TEST_SUBROUTINE int berder_test(void)
  35152. {
  35153. int ret;
  35154. int i;
  35155. word32 len = 0, l;
  35156. byte out[32];
  35157. WOLFSSL_SMALL_STACK_STATIC const byte good1_in[] = { 0x30, 0x80, 0x00, 0x00 };
  35158. WOLFSSL_SMALL_STACK_STATIC const byte good1_out[] = { 0x30, 0x00 };
  35159. WOLFSSL_SMALL_STACK_STATIC const byte good2_in[] = { 0x30, 0x80, 0x02, 0x01, 0x01, 0x00, 0x00 };
  35160. WOLFSSL_SMALL_STACK_STATIC const byte good2_out[] = { 0x30, 0x03, 0x02, 0x01, 0x01 };
  35161. WOLFSSL_SMALL_STACK_STATIC const byte good3_in[] = {
  35162. 0x24, 0x80, 0x04, 0x01, 0x01, 0x00, 0x00
  35163. };
  35164. WOLFSSL_SMALL_STACK_STATIC const byte good3_out[] = { 0x04, 0x1, 0x01 };
  35165. WOLFSSL_SMALL_STACK_STATIC const byte good4_in[] = {
  35166. 0x30, 0x80,
  35167. 0x02, 0x01, 0x01,
  35168. 0x30, 0x80,
  35169. 0x24, 0x80,
  35170. 0x04, 0x01, 0x01,
  35171. 0x04, 0x02, 0x02, 0x03,
  35172. 0x00, 0x00,
  35173. 0x06, 0x01, 0x01,
  35174. 0x00, 0x00,
  35175. 0x31, 0x80,
  35176. 0x06, 0x01, 0x01,
  35177. 0x00, 0x00,
  35178. 0x00, 0x00,
  35179. };
  35180. WOLFSSL_SMALL_STACK_STATIC const byte good4_out[] = {
  35181. 0x30, 0x12,
  35182. 0x02, 0x01, 0x01,
  35183. 0x30, 0x08,
  35184. 0x04, 0x03, 0x01, 0x02, 0x03,
  35185. 0x06, 0x01, 0x01,
  35186. 0x31, 0x03,
  35187. 0x06, 0x01, 0x01
  35188. };
  35189. WOLFSSL_SMALL_STACK_STATIC const byte good5_in[] = { 0x30, 0x03, 0x02, 0x01, 0x01 };
  35190. berDerTestData testData[] = {
  35191. { good1_in, sizeof(good1_in), good1_out, sizeof(good1_out) },
  35192. { good2_in, sizeof(good2_in), good2_out, sizeof(good2_out) },
  35193. { good3_in, sizeof(good3_in), good3_out, sizeof(good3_out) },
  35194. { good4_in, sizeof(good4_in), good4_out, sizeof(good4_out) },
  35195. { good5_in, sizeof(good5_in), good5_in , sizeof(good5_in ) },
  35196. };
  35197. for (i = 0; i < (int)(sizeof(testData) / sizeof(*testData)); i++) {
  35198. ret = wc_BerToDer(testData[i].in, testData[i].inSz, NULL, &len);
  35199. if (ret != LENGTH_ONLY_E)
  35200. return -13500 - i;
  35201. if (len != testData[i].outSz)
  35202. return -13510 - i;
  35203. len = testData[i].outSz;
  35204. ret = wc_BerToDer(testData[i].in, testData[i].inSz, out, &len);
  35205. if (ret != 0)
  35206. return -13520 - i;
  35207. if (XMEMCMP(out, testData[i].out, len) != 0)
  35208. return -13530 - i;
  35209. for (l = 1; l < testData[i].inSz; l++) {
  35210. ret = wc_BerToDer(testData[i].in, l, NULL, &len);
  35211. if (ret != ASN_PARSE_E)
  35212. return -13540;
  35213. len = testData[i].outSz;
  35214. ret = wc_BerToDer(testData[i].in, l, out, &len);
  35215. if (ret != ASN_PARSE_E)
  35216. return -13541;
  35217. }
  35218. for (l = 0; l < testData[i].outSz-1; l++) {
  35219. ret = wc_BerToDer(testData[i].in, testData[i].inSz, out, &l);
  35220. if (ret != BUFFER_E)
  35221. return -13542;
  35222. }
  35223. }
  35224. ret = wc_BerToDer(NULL, 4, NULL, NULL);
  35225. if (ret != BAD_FUNC_ARG)
  35226. return -13543;
  35227. ret = wc_BerToDer(out, 4, NULL, NULL);
  35228. if (ret != BAD_FUNC_ARG)
  35229. return -13544;
  35230. ret = wc_BerToDer(NULL, 4, NULL, &len);
  35231. if (ret != BAD_FUNC_ARG)
  35232. return -13545;
  35233. ret = wc_BerToDer(NULL, 4, out, NULL);
  35234. if (ret != BAD_FUNC_ARG)
  35235. return -13546;
  35236. ret = wc_BerToDer(out, 4, out, NULL);
  35237. if (ret != BAD_FUNC_ARG)
  35238. return -13547;
  35239. ret = wc_BerToDer(NULL, 4, out, &len);
  35240. if (ret != BAD_FUNC_ARG)
  35241. return -13548;
  35242. for (l = 1; l < sizeof(good4_out); l++) {
  35243. len = l;
  35244. ret = wc_BerToDer(good4_in, sizeof(good4_in), out, &len);
  35245. if (ret != BUFFER_E)
  35246. return -13549;
  35247. }
  35248. return 0;
  35249. }
  35250. #endif
  35251. #ifdef DEBUG_WOLFSSL
  35252. static THREAD_LS_T int log_cnt = 0;
  35253. static void my_Logging_cb(const int logLevel, const char *const logMessage)
  35254. {
  35255. (void)logLevel;
  35256. (void)logMessage;
  35257. log_cnt++;
  35258. }
  35259. #endif /* DEBUG_WOLFSSL */
  35260. WOLFSSL_TEST_SUBROUTINE int logging_test(void)
  35261. {
  35262. #ifdef DEBUG_WOLFSSL
  35263. const char* msg = "Testing, testing. 1, 2, 3, 4 ...";
  35264. byte a[8] = { 1, 2, 3, 4, 5, 6, 7, 8 };
  35265. byte b[256];
  35266. int i;
  35267. for (i = 0; i < (int)sizeof(b); i++)
  35268. b[i] = i;
  35269. if (wolfSSL_Debugging_ON() != 0)
  35270. return -13600;
  35271. if (wolfSSL_SetLoggingCb(my_Logging_cb) != 0)
  35272. return -13601;
  35273. WOLFSSL_MSG(msg);
  35274. WOLFSSL_BUFFER(a, sizeof(a));
  35275. WOLFSSL_BUFFER(b, sizeof(b));
  35276. WOLFSSL_BUFFER(NULL, 0);
  35277. WOLFSSL_ERROR(MEMORY_E);
  35278. WOLFSSL_ERROR_MSG(msg);
  35279. /* turn off logs */
  35280. wolfSSL_Debugging_OFF();
  35281. /* capture log count */
  35282. i = log_cnt;
  35283. /* validate no logs are output when disabled */
  35284. WOLFSSL_MSG(msg);
  35285. WOLFSSL_BUFFER(a, sizeof(a));
  35286. WOLFSSL_BUFFER(b, sizeof(b));
  35287. WOLFSSL_BUFFER(NULL, 0);
  35288. WOLFSSL_ERROR(MEMORY_E);
  35289. WOLFSSL_ERROR_MSG(msg);
  35290. /* check the logs were disabled */
  35291. if (i != log_cnt)
  35292. return -13602;
  35293. /* restore callback and leave logging enabled */
  35294. wolfSSL_SetLoggingCb(NULL);
  35295. wolfSSL_Debugging_ON();
  35296. /* suppress unused args */
  35297. (void)a;
  35298. (void)b;
  35299. #else
  35300. if (wolfSSL_Debugging_ON() != NOT_COMPILED_IN)
  35301. return -13603;
  35302. wolfSSL_Debugging_OFF();
  35303. if (wolfSSL_SetLoggingCb(NULL) != NOT_COMPILED_IN)
  35304. return -13604;
  35305. #endif /* DEBUG_WOLFSSL */
  35306. return 0;
  35307. }
  35308. WOLFSSL_TEST_SUBROUTINE int mutex_test(void)
  35309. {
  35310. #ifdef WOLFSSL_PTHREADS
  35311. wolfSSL_Mutex m;
  35312. #endif
  35313. #if !defined(WOLFSSL_NO_MALLOC) && !defined(WOLFSSL_USER_MUTEX)
  35314. wolfSSL_Mutex *mm = wc_InitAndAllocMutex();
  35315. if (mm == NULL)
  35316. return -13700;
  35317. wc_FreeMutex(mm);
  35318. XFREE(mm, HEAP_HINT, DYNAMIC_TYPE_MUTEX);
  35319. #endif
  35320. /* Can optionally enable advanced pthread tests using "ENABLE_PTHREAD_LOCKFREE_TESTS" */
  35321. #ifdef WOLFSSL_PTHREADS
  35322. if (wc_InitMutex(&m) != 0)
  35323. return -13701;
  35324. if (wc_LockMutex(&m) != 0)
  35325. return -13702;
  35326. #if !defined(WOLFSSL_SOLARIS) && defined(ENABLE_PTHREAD_LOCKFREE_TESTS)
  35327. /* trying to free a locked mutex is not portable behavior with pthread */
  35328. /* Attempting to destroy a locked mutex results in undefined behavior */
  35329. if (wc_FreeMutex(&m) != BAD_MUTEX_E)
  35330. return -13703;
  35331. #endif
  35332. if (wc_UnLockMutex(&m) != 0)
  35333. return -13704;
  35334. if (wc_FreeMutex(&m) != 0)
  35335. return -13705;
  35336. #if !defined(WOLFSSL_SOLARIS) && defined(ENABLE_PTHREAD_LOCKFREE_TESTS)
  35337. /* Trying to use a pthread after free'ing is not portable behavior */
  35338. if (wc_LockMutex(&m) != BAD_MUTEX_E)
  35339. return -13706;
  35340. if (wc_UnLockMutex(&m) != BAD_MUTEX_E)
  35341. return -13707;
  35342. #endif
  35343. #endif
  35344. return 0;
  35345. }
  35346. #if defined(USE_WOLFSSL_MEMORY) && !defined(FREERTOS)
  35347. #if !defined(WOLFSSL_NO_MALLOC) && !defined(WOLFSSL_LINUXKM) && \
  35348. !defined(WOLFSSL_STATIC_MEMORY)
  35349. static int malloc_cnt = 0;
  35350. static int realloc_cnt = 0;
  35351. static int free_cnt = 0;
  35352. #ifdef WOLFSSL_DEBUG_MEMORY
  35353. static void *my_Malloc_cb(size_t size, const char* func, unsigned int line)
  35354. {
  35355. (void) func;
  35356. (void) line;
  35357. #else
  35358. static void *my_Malloc_cb(size_t size)
  35359. {
  35360. #endif
  35361. malloc_cnt++;
  35362. #ifndef WOLFSSL_NO_MALLOC
  35363. return malloc(size);
  35364. #else
  35365. WOLFSSL_MSG("No malloc available");
  35366. (void)size;
  35367. return NULL;
  35368. #endif
  35369. }
  35370. #ifdef WOLFSSL_DEBUG_MEMORY
  35371. static void my_Free_cb(void *ptr, const char* func, unsigned int line)
  35372. {
  35373. (void) func;
  35374. (void) line;
  35375. #else
  35376. static void my_Free_cb(void *ptr)
  35377. {
  35378. #endif
  35379. free_cnt++;
  35380. #ifndef WOLFSSL_NO_MALLOC
  35381. free(ptr);
  35382. #else
  35383. WOLFSSL_MSG("No free available");
  35384. (void)ptr;
  35385. #endif
  35386. }
  35387. #ifdef WOLFSSL_DEBUG_MEMORY
  35388. static void *my_Realloc_cb(void *ptr, size_t size, const char* func, unsigned int line)
  35389. {
  35390. (void) func;
  35391. (void) line;
  35392. #else
  35393. static void *my_Realloc_cb(void *ptr, size_t size)
  35394. {
  35395. #endif
  35396. realloc_cnt++;
  35397. #ifndef WOLFSSL_NO_MALLOC
  35398. return realloc(ptr, size);
  35399. #else
  35400. WOLFSSL_MSG("No realloc available");
  35401. (void)ptr;
  35402. (void)size;
  35403. return NULL;
  35404. #endif
  35405. }
  35406. #endif /* !WOLFSSL_NO_MALLOC */
  35407. WOLFSSL_TEST_SUBROUTINE int memcb_test(void)
  35408. {
  35409. int ret = 0;
  35410. #if !defined(WOLFSSL_NO_MALLOC) && !defined(WOLFSSL_LINUXKM) && \
  35411. !defined(WOLFSSL_STATIC_MEMORY)
  35412. byte* b = NULL;
  35413. #endif
  35414. wolfSSL_Malloc_cb mc;
  35415. wolfSSL_Free_cb fc;
  35416. wolfSSL_Realloc_cb rc;
  35417. /* Save existing memory callbacks */
  35418. if (wolfSSL_GetAllocators(&mc, &fc, &rc) != 0)
  35419. return -13800;
  35420. #if !defined(WOLFSSL_NO_MALLOC) && !defined(WOLFSSL_LINUXKM) && \
  35421. !defined(WOLFSSL_STATIC_MEMORY)
  35422. /* test realloc */
  35423. b = (byte*)XREALLOC(b, 1024, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35424. if (b == NULL) {
  35425. ERROR_OUT(-13801, exit_memcb);
  35426. }
  35427. XFREE(b, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35428. b = NULL;
  35429. /* Use API. */
  35430. if (wolfSSL_SetAllocators((wolfSSL_Malloc_cb)my_Malloc_cb,
  35431. (wolfSSL_Free_cb)my_Free_cb,
  35432. (wolfSSL_Realloc_cb)my_Realloc_cb) != 0) {
  35433. ERROR_OUT(-13802, exit_memcb);
  35434. }
  35435. b = (byte*)XMALLOC(1024, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35436. b = (byte*)XREALLOC(b, 1024, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35437. XFREE(b, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35438. #ifndef WOLFSSL_STATIC_MEMORY
  35439. #ifndef WOLFSSL_CHECK_MEM_ZERO
  35440. if (malloc_cnt != 1 || free_cnt != 1 || realloc_cnt != 1)
  35441. #else
  35442. /* Checking zeroized memory means realloc is a malloc and free. */
  35443. if (malloc_cnt != 2 || free_cnt != 2 || realloc_cnt != 0)
  35444. #endif
  35445. #else
  35446. if (malloc_cnt != 0 || free_cnt != 0 || realloc_cnt != 0)
  35447. #endif
  35448. ret = -13803;
  35449. #endif /* !WOLFSSL_NO_MALLOC */
  35450. #if !defined(WOLFSSL_NO_MALLOC) && !defined(WOLFSSL_LINUXKM) && \
  35451. !defined(WOLFSSL_STATIC_MEMORY)
  35452. exit_memcb:
  35453. /* reset malloc/free/realloc counts */
  35454. malloc_cnt = 0;
  35455. free_cnt = 0;
  35456. realloc_cnt = 0;
  35457. #endif
  35458. /* restore memory callbacks */
  35459. wolfSSL_SetAllocators(mc, fc, rc);
  35460. return ret;
  35461. }
  35462. #endif /* USE_WOLFSSL_MEMORY && !WOLFSSL_NO_MALLOC */
  35463. #ifdef WOLFSSL_IMX6_CAAM_BLOB
  35464. WOLFSSL_TEST_SUBROUTINE int blob_test(void)
  35465. {
  35466. int ret = 0;
  35467. byte out[112];
  35468. byte blob[112];
  35469. word32 outSz;
  35470. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  35471. {
  35472. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  35473. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  35474. };
  35475. WOLFSSL_SMALL_STACK_STATIC const byte text[] =
  35476. {
  35477. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  35478. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  35479. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  35480. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  35481. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  35482. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  35483. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  35484. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  35485. };
  35486. XMEMSET(blob, 0, sizeof(blob));
  35487. XMEMSET(out, 0, sizeof(out));
  35488. outSz = sizeof(blob);
  35489. ret = wc_caamCreateBlob((byte*)iv, sizeof(iv), blob, &outSz);
  35490. if (ret != 0) {
  35491. ERROR_OUT(-13900, exit_blob);
  35492. }
  35493. blob[outSz - 2] += 1;
  35494. ret = wc_caamOpenBlob(blob, outSz, out, &outSz);
  35495. if (ret == 0) { /* should fail with altered blob */
  35496. ERROR_OUT(-13901, exit_blob);
  35497. }
  35498. XMEMSET(blob, 0, sizeof(blob));
  35499. outSz = sizeof(blob);
  35500. ret = wc_caamCreateBlob((byte*)iv, sizeof(iv), blob, &outSz);
  35501. if (ret != 0) {
  35502. ERROR_OUT(-13902, exit_blob);
  35503. }
  35504. ret = wc_caamOpenBlob(blob, outSz, out, &outSz);
  35505. if (ret != 0) {
  35506. ERROR_OUT(-13903, exit_blob);
  35507. }
  35508. if (XMEMCMP(out, iv, sizeof(iv))) {
  35509. ERROR_OUT(-13904, exit_blob);
  35510. }
  35511. XMEMSET(blob, 0, sizeof(blob));
  35512. outSz = sizeof(blob);
  35513. ret = wc_caamCreateBlob((byte*)text, sizeof(text), blob, &outSz);
  35514. if (ret != 0) {
  35515. ERROR_OUT(-13905, exit_blob);
  35516. }
  35517. ret = wc_caamOpenBlob(blob, outSz, out, &outSz);
  35518. if (ret != 0) {
  35519. ERROR_OUT(-13906, exit_blob);
  35520. }
  35521. if (XMEMCMP(out, text, sizeof(text))) {
  35522. ERROR_OUT(-13907, exit_blob);
  35523. }
  35524. exit_blob:
  35525. return ret;
  35526. }
  35527. #endif /* WOLFSSL_IMX6_CAAM_BLOB */
  35528. #ifdef WOLF_CRYPTO_CB
  35529. /* Example custom context for crypto callback */
  35530. typedef struct {
  35531. int exampleVar; /* flag for testing if only crypt is enabled. */
  35532. } myCryptoDevCtx;
  35533. #ifdef WOLF_CRYPTO_CB_ONLY_RSA
  35534. /* Testing rsa cb when CB_ONLY_RSA is enabled
  35535. * When CB_ONLY_RSA is enabled, software imple. is not available.
  35536. *
  35537. * ctx callback ctx
  35538. * returen 0 on success, otherwise return -8000 - -8007
  35539. */
  35540. static int rsa_onlycb_test(myCryptoDevCtx *ctx)
  35541. {
  35542. int ret = 0;
  35543. #if !defined(NO_RSA)
  35544. #ifdef WOLFSSL_SMALL_STACK
  35545. RsaKey *key = (RsaKey *)XMALLOC(sizeof *key,
  35546. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35547. byte* tmp = NULL;
  35548. #else
  35549. RsaKey key[1];
  35550. byte tmp[FOURK_BUF];
  35551. #endif
  35552. size_t bytes;
  35553. const word32 inLen = (word32)TEST_STRING_SZ;
  35554. word32 idx = 0;
  35555. word32 sigSz;
  35556. WOLFSSL_SMALL_STACK_STATIC const byte in[] = TEST_STRING;
  35557. byte out[RSA_TEST_BYTES];
  35558. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  35559. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096) && \
  35560. !defined(NO_FILESYSTEM)
  35561. XFILE file;
  35562. #endif
  35563. #ifdef WOLFSSL_KEY_GEN
  35564. WC_RNG rng;
  35565. #endif
  35566. #ifdef USE_CERT_BUFFERS_1024
  35567. bytes = (size_t)sizeof_client_key_der_1024;
  35568. if (bytes < (size_t)sizeof_client_cert_der_1024)
  35569. bytes = (size_t)sizeof_client_cert_der_1024;
  35570. #elif defined(USE_CERT_BUFFERS_2048)
  35571. bytes = (size_t)sizeof_client_key_der_2048;
  35572. if (bytes < (size_t)sizeof_client_cert_der_2048)
  35573. bytes = (size_t)sizeof_client_cert_der_2048;
  35574. #elif defined(USE_CERT_BUFFERS_3072)
  35575. bytes = (size_t)sizeof_client_key_der_3072;
  35576. if (bytes < (size_t)sizeof_client_cert_der_3072)
  35577. bytes = (size_t)sizeof_client_cert_der_3072;
  35578. #elif defined(USE_CERT_BUFFERS_4096)
  35579. bytes = (size_t)sizeof_client_key_der_4096;
  35580. if (bytes < (size_t)sizeof_client_cert_der_4096)
  35581. bytes = (size_t)sizeof_client_cert_der_4096;
  35582. #else
  35583. bytes = FOURK_BUF;
  35584. #endif
  35585. #ifdef WOLFSSL_SMALL_STACK
  35586. tmp = (byte*)XMALLOC(bytes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35587. if (tmp == NULL)
  35588. ERROR_OUT(-8000, exit_onlycb);
  35589. #endif
  35590. #ifdef USE_CERT_BUFFERS_1024
  35591. XMEMCPY(tmp, client_key_der_1024, (size_t)sizeof_client_key_der_1024);
  35592. #elif defined(USE_CERT_BUFFERS_2048)
  35593. XMEMCPY(tmp, client_key_der_2048, (size_t)sizeof_client_key_der_2048);
  35594. #elif defined(USE_CERT_BUFFERS_3072)
  35595. XMEMCPY(tmp, client_key_der_3072, (size_t)sizeof_client_key_der_3072);
  35596. #elif defined(USE_CERT_BUFFERS_4096)
  35597. XMEMCPY(tmp, client_key_der_4096, (size_t)sizeof_client_key_der_4096);
  35598. #elif !defined(NO_FILESYSTEM)
  35599. file = XFOPEN(clientKey, "rb");
  35600. if (!file) {
  35601. err_sys("can't open ./certs/client-key.der, "
  35602. "Please run from wolfSSL home dir", -40);
  35603. ERROR_OUT(-8001, exit_onlycb);
  35604. }
  35605. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  35606. XFCLOSE(file);
  35607. #endif
  35608. #ifdef WOLFSSL_KEY_GEN
  35609. /* wc_CryptoCb_MakeRsaKey cb test, no actual making key
  35610. * wc_MakeRsaKey() -> rsa cb ->
  35611. * myCryptoDevCb -> wc_MakeRsaKey(CBONLY_TEST_DEVID)
  35612. * wc_MakeRsaKey(CBONLY_TEST_DEVID) expects to return 0(success)
  35613. */
  35614. ctx->exampleVar = 99;
  35615. ret = wc_MakeRsaKey(key, keySz, WC_RSA_EXPONENT, rng);
  35616. if (ret != 0) {
  35617. ERROR_OUT(-8002, exit_onlycb);
  35618. }
  35619. /* wc_MakeRsaKey() -> rsa cb ->
  35620. * myCryptoDevCb -> wc_MakeRsaKey(INVALID_DEVID)
  35621. * wc_MakeRsaKey(CBONLY_TEST_DEVID) expects to return NO_VALID_DEVID(failure)
  35622. */
  35623. ctx->exampleVar = 1;
  35624. ret = wc_MakeRsaKey(key, keySz, WC_RSA_EXPONENT, rng);
  35625. if (ret != NO_VALID_DEVID) {
  35626. ERROR_OUT(-8003, exit_onlycb);
  35627. } else
  35628. /* reset return code */
  35629. ret = 0;
  35630. #endif
  35631. ret = wc_InitRsaKey_ex(key, HEAP_HINT, devId);
  35632. if (ret != 0) {
  35633. ERROR_OUT(-8004, exit_onlycb);
  35634. }
  35635. ret = wc_RsaPrivateKeyDecode(tmp, &idx, key, (word32)bytes);
  35636. if (ret != 0) {
  35637. ERROR_OUT(-8005, exit_onlycb);
  35638. }
  35639. sigSz = (word32)wc_RsaEncryptSize(key);
  35640. /* wc_CryptoCb_Rsa cb test, no actual rsa operation */
  35641. if (ret == 0) {
  35642. /* wc_SignatureGenerate() -> rsa cb ->
  35643. * myCryptoDevCb -> wc_RsaFunction(CBONLY_TEST_DEVID)
  35644. * wc_RsaFunction(CBONLY_TEST_DEVID) expects to return 0(success)
  35645. */
  35646. ctx->exampleVar = 99;
  35647. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA,
  35648. in, inLen, out, &sigSz, key, sizeof(*key), NULL);
  35649. if (ret != 0) {
  35650. ERROR_OUT(-8006, exit_onlycb);
  35651. }
  35652. }
  35653. if (ret == 0) {
  35654. /* wc_SignatureGenerate() -> rsa cb ->
  35655. * myCryptoDevCb -> wc_RsaFunction(INVALID_DEVID)
  35656. * wc_SignatureGenerate(INVALID_DEVID) expects to
  35657. * return NO_VALID_DEVID(failure)
  35658. */
  35659. ctx->exampleVar = 1;
  35660. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA,
  35661. in, inLen, out, &sigSz, key, sizeof(*key), NULL);
  35662. if (ret != NO_VALID_DEVID) {
  35663. ERROR_OUT(-8007, exit_onlycb);
  35664. } else
  35665. /* reset return code */
  35666. ret = 0;
  35667. }
  35668. exit_onlycb:
  35669. #ifdef WOLFSSL_SMALL_STACK
  35670. if (key != NULL) {
  35671. wc_FreeRsaKey(key);
  35672. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35673. }
  35674. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35675. #else
  35676. wc_FreeRsaKey(key);
  35677. #endif
  35678. #endif
  35679. return ret;
  35680. }
  35681. #endif
  35682. #ifdef WOLF_CRYPTO_CB_ONLY_ECC
  35683. /* Testing rsa cb when CB_ONLY_ECC is enabled
  35684. * When CB_ONLY_ECC is enabled, software imple. is not available.
  35685. *
  35686. * ctx callback ctx
  35687. * returen 0 on success, otherwise return -8008 - -8018
  35688. */
  35689. static int ecc_onlycb_test(myCryptoDevCtx *ctx)
  35690. {
  35691. int ret = 0;
  35692. #if defined(HAVE_ECC)
  35693. #ifdef WOLFSSL_SMALL_STACK
  35694. ecc_key* key = (ecc_key *)XMALLOC(sizeof *key,
  35695. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35696. ecc_key* pub = (ecc_key *)XMALLOC(sizeof *pub,
  35697. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35698. byte* out = (byte*)XMALLOC(sizeof(byte),
  35699. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35700. #ifdef OPENSSL_EXTRA
  35701. byte* check = (byte*)XMALLOC(sizeof(byte)*(256), HEAP_HINT,
  35702. DYNAMIC_TYPE_TMP_BUFFER);
  35703. #endif
  35704. #else
  35705. ecc_key key[1];
  35706. ecc_key pub[1];
  35707. byte out[256];
  35708. #ifdef OPENSSL_EXTRA
  35709. unsigned char check[256];
  35710. #endif
  35711. #endif
  35712. #ifdef OPENSSL_EXTRA
  35713. EVP_PKEY* privKey = NULL;
  35714. EVP_PKEY* pubKey = NULL;
  35715. ecc_key* pkey;
  35716. EVP_MD_CTX mdCtx;
  35717. const char testData[] = "Hi There";
  35718. size_t checkSz = -1;
  35719. const unsigned char* cp;
  35720. const unsigned char* p;
  35721. const unsigned char check_v[256] = {
  35722. 0x30,0x45,0x02,0x20,0x1b,0x5c,0x2a,0xf0,0x18,0x09,
  35723. 0x74,0x65,0xa1,0x04,0x76,0x3a,0xce,0xcc,0xe5,0x34,
  35724. 0x5e,0x89,0xed,0x40,0x1e,0x5a,0xb1,0x53,0xb4,0xff,
  35725. 0xc7,0x18,0xfe,0x0f,0xc7,0xa6,0x02,0x21,0x00,0xe5,
  35726. 0x70,0x21,0xfc,0xf9,0x63,0x36,0xfd,0x16,0x18,0x08,
  35727. 0x9a,0x63,0x61,0x0f,0xe7,0x7c,0xa3,0xc9,0x14,0xa3,
  35728. 0x30,0x87,0xf7,0xf5,0x70,0x19,0xaf,0x56,0x96,0x9b,
  35729. 0xd8,0x64,0xcd,0xd9,0xff,0x7b,0x2a,0x55,0x52,0xca,
  35730. 0x41,0xb2,0xa6,0xa4,0x8a,0x3b,0x02,0x20,0x8c,0xc5,
  35731. 0xf9,0xc1,0x7d,0x2a,0x65,0x6c,0xe6,0x5a,0xe3,0x76,
  35732. 0x9b,0xab,0x0b,0x9f,0xaf,0x62,0x5d,0xb2,0x60,0xd7,
  35733. 0xeb,0xb4,0x1b,0x73,0xdc,0x01,0x7d,0x7b,0xab,0xc1,
  35734. 0x0c,0x74,0x96,0x41,0xe6,0x3f,0xc5,0x86,0xe6,0x7d,
  35735. 0x2b,0x9d,0x54,0x6b,0xcd,0x31,0x35,0x1f,0xdb,0x49,
  35736. 0x1f,0x32,0x34,0xf8,0x57,0x12,0x86,0x5c,0x0e,0x80,
  35737. 0x55,0x8d,0xff,0xd8,0xbd,0xdf,0x32,0x26,0x62,0x42,
  35738. 0x09,0xda,0xf7,0x74,0xf2,0x3f,0xe6,0xf1,0x77,0x82,
  35739. 0xce,0xe4,0xbb,0x61,0xa6,0xc0,0x17,0x0c,0x6c,0x47,
  35740. 0x2a,0x40,0x1c,0x2b,0xe0,0x98,0x3b,0xbf,0xc6,0xf8,
  35741. 0x6d,0xfd,0xd0,0xfa,0xc1,0x02,0xfb,0x5f,0xfb,0xb0,
  35742. 0xcb,0xd9,0xa3,0x59,0x94,0xe9,0x0f,0x74,0xbb,0x3f,
  35743. 0x64,0xa3,0x83,0xc4,0x2b,0xf7,0xd2,0x97,0xbf,0x3b,
  35744. 0xcf,0xbb,0x60,0x81,0x33,0x94,0xfa,0x0d,0x35,0xd2,
  35745. 0x3d,0xb9,0x99,0xe3,0x12,0xf8,0xf4,0xa3,0x74,0xf4,
  35746. 0x94,0x1d,0x7a,0x66,0xf8,0xd1,0x1d,0xcf,0xb0,0x48,
  35747. 0xef,0x8c,0x94,0x6f,0xdd,0x62,
  35748. };
  35749. #endif
  35750. WC_RNG rng;
  35751. EncryptedInfo encInfo;
  35752. int keyFormat = 0;
  35753. word32 keyIdx = 0;
  35754. byte in[] = "Everyone gets Friday off. ecc p";
  35755. word32 inLen = (word32)XSTRLEN((char*)in);
  35756. word32 outLen;
  35757. int verify;
  35758. #ifdef WOLFSSL_SMALL_STACK
  35759. if (key == NULL || pub == NULL) {
  35760. ERROR_OUT(-8008, exit_onlycb);
  35761. }
  35762. #endif
  35763. ret = wc_ecc_init_ex(key, HEAP_HINT, devId);
  35764. if (ret != 0) {
  35765. ERROR_OUT(-8009, exit_onlycb);
  35766. }
  35767. /* wc_CryptoCb_MakeEccKey cb test, , no actual testing */
  35768. ctx->exampleVar = 99;
  35769. ret = wc_ecc_make_key(&rng, ECC_KEYGEN_SIZE, key);
  35770. if (ret != 0) {
  35771. ERROR_OUT(-8010, exit_onlycb);
  35772. }
  35773. ctx->exampleVar = 1;
  35774. ret = wc_ecc_make_key(&rng, ECC_KEYGEN_SIZE, key);
  35775. if (ret != NO_VALID_DEVID) {
  35776. ERROR_OUT(-8011, exit_onlycb);
  35777. } else
  35778. /* reset return code */
  35779. ret = 0;
  35780. #ifdef USE_CERT_BUFFERS_256
  35781. if (ret == 0) {
  35782. /* load ECC private key and perform private transform */
  35783. ret = wc_EccPrivateKeyDecode(ecc_key_der_256, &keyIdx,
  35784. key, sizeof_ecc_key_der_256);
  35785. }
  35786. if (ret != 0) {
  35787. ERROR_OUT(-8012, exit_onlycb);
  35788. }
  35789. /* wc_CryptoCb_EccSign cb test, no actual testing */
  35790. ctx->exampleVar = 99;
  35791. if (ret == 0) {
  35792. ret = wc_ecc_sign_hash(in, inLen, out, &outLen, &rng, key);
  35793. }
  35794. if (ret != 0) {
  35795. ERROR_OUT(-8013, exit_onlycb);
  35796. }
  35797. ctx->exampleVar = 1;
  35798. if (ret == 0) {
  35799. ret = wc_ecc_sign_hash(in, inLen, out, &outLen, &rng, key);
  35800. }
  35801. if (ret != NO_VALID_DEVID) {
  35802. ERROR_OUT(-8014, exit_onlycb);
  35803. }
  35804. else
  35805. ret = 0;
  35806. /* wc_CryptoCb_EccVerify cb test, no actual testing */
  35807. ctx->exampleVar = 99;
  35808. if (ret == 0) {
  35809. ret = wc_ecc_verify_hash(in, inLen, out, outLen, &verify, key);
  35810. }
  35811. if (ret != 0) {
  35812. ERROR_OUT(-8015, exit_onlycb);
  35813. }
  35814. ctx->exampleVar = 1;
  35815. if (ret == 0) {
  35816. ret = wc_ecc_verify_hash(in, inLen, out, outLen, &verify, key);
  35817. }
  35818. if (ret != NO_VALID_DEVID) {
  35819. ERROR_OUT(-8016, exit_onlycb);
  35820. }
  35821. else
  35822. ret = 0;
  35823. /* wc_CryptoCb_Ecdh cb test, no actual testing */
  35824. /* make public key for shared secret */
  35825. wc_ecc_init_ex(pub, HEAP_HINT, devId);
  35826. ctx->exampleVar = 99;
  35827. if (ret == 0) {
  35828. ret = wc_ecc_shared_secret(key, pub, out, &outLen);
  35829. }
  35830. if (ret != 0) {
  35831. ERROR_OUT(-8017, exit_onlycb);
  35832. }
  35833. ctx->exampleVar = 1;
  35834. if (ret == 0) {
  35835. ret = wc_ecc_shared_secret(key, pub, out, &outLen);
  35836. }
  35837. if (ret != NO_VALID_DEVID) {
  35838. ERROR_OUT(-8018, exit_onlycb);
  35839. }
  35840. else
  35841. ret = 0;
  35842. #ifdef OPENSSL_EXTRA
  35843. (void)pkey;
  35844. cp = ecc_clikey_der_256;
  35845. privKey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &cp,
  35846. sizeof_ecc_clikey_der_256);
  35847. if (privKey == NULL) {
  35848. ERROR_OUT(-8019, exit_onlycb);
  35849. }
  35850. pkey = (ecc_key*)privKey->ecc->internal;
  35851. pkey->devId = devId;
  35852. p = ecc_clikeypub_der_256;
  35853. pubKey = d2i_PUBKEY(NULL, &p, sizeof_ecc_clikeypub_der_256);
  35854. if (pubKey == NULL) {
  35855. ERROR_OUT(-8020, exit_onlycb);
  35856. }
  35857. pkey = (ecc_key*)pubKey->ecc->internal;
  35858. pkey->devId = devId;
  35859. /* sign */
  35860. EVP_MD_CTX_init(&mdCtx);
  35861. ret = EVP_DigestSignInit(&mdCtx, NULL, EVP_sha256(), NULL, privKey);
  35862. if (ret != WOLFSSL_SUCCESS) {
  35863. ERROR_OUT(-8021, exit_onlycb);
  35864. }
  35865. ret = EVP_DigestSignUpdate(&mdCtx, testData,
  35866. (unsigned int)XSTRLEN(testData));
  35867. if (ret != WOLFSSL_SUCCESS) {
  35868. ERROR_OUT(-8022, exit_onlycb);
  35869. }
  35870. ret = EVP_DigestSignFinal(&mdCtx, NULL, &checkSz);
  35871. if (ret != WOLFSSL_SUCCESS) {
  35872. ERROR_OUT(-8023, exit_onlycb);
  35873. }
  35874. ctx->exampleVar = 99;
  35875. ret = EVP_DigestSignFinal(&mdCtx, check, &checkSz);
  35876. /* just called crypt callback as dummy
  35877. * EVP_DigestSignFinal returns 0 internally.
  35878. */
  35879. if (ret != 0) {
  35880. ERROR_OUT(-8024, exit_onlycb);
  35881. }
  35882. ctx->exampleVar = 1;
  35883. ret = EVP_DigestSignFinal(&mdCtx, check, &checkSz);
  35884. /* just called crypt callback as dummy
  35885. * EVP_DigestSignFinal returns 0 internally.
  35886. */
  35887. if (ret != 0) {
  35888. ERROR_OUT(-8025, exit_onlycb);
  35889. }
  35890. /* restore checkSz for verify */
  35891. checkSz = 71;
  35892. ret = EVP_MD_CTX_cleanup(&mdCtx);
  35893. if (ret != SSL_SUCCESS) {
  35894. ERROR_OUT(-8026, exit_onlycb);
  35895. }
  35896. /* verify */
  35897. EVP_MD_CTX_init(&mdCtx);
  35898. if (ret == SSL_SUCCESS) {
  35899. ret = EVP_DigestVerifyInit(&mdCtx, NULL, EVP_sha256(), NULL, pubKey);
  35900. }
  35901. if (ret != WOLFSSL_SUCCESS) {
  35902. ERROR_OUT(-8027, exit_onlycb);
  35903. }
  35904. if (ret == WOLFSSL_SUCCESS) {
  35905. ret = EVP_DigestVerifyUpdate(&mdCtx, testData,
  35906. (unsigned int)XSTRLEN(testData));
  35907. }
  35908. if (ret != WOLFSSL_SUCCESS) {
  35909. ERROR_OUT(-8028, exit_onlycb);
  35910. }
  35911. ctx->exampleVar = 99;
  35912. ret = EVP_DigestVerifyFinal(&mdCtx, check_v, checkSz);
  35913. /* just called crypt callback as dummy
  35914. * EVP_DigestSignFinal returns 0 internally.
  35915. */
  35916. if (ret != 0) {
  35917. ERROR_OUT(-8029, exit_onlycb);
  35918. }
  35919. ctx->exampleVar = 1;
  35920. ret = EVP_DigestVerifyFinal(&mdCtx, check_v, checkSz);
  35921. /* just called crypt callback as dummy
  35922. * EVP_DigestVerifyFinal returns -1 internally rather than NO_VALID_DEVID.
  35923. */
  35924. if (ret != -1) {
  35925. ERROR_OUT(-8030, exit_onlycb);
  35926. }
  35927. ret = EVP_MD_CTX_cleanup(&mdCtx);
  35928. if (ret != SSL_SUCCESS) {
  35929. ERROR_OUT(-8031, exit_onlycb);
  35930. } else
  35931. ret = 0;
  35932. #endif
  35933. #else
  35934. (void)verify;
  35935. (void)outLen;
  35936. (void)inLen;
  35937. (void)out;
  35938. (void)pub;
  35939. #ifdef OPENSSL_EXTRA
  35940. (void)privKey;
  35941. (void)pubKey;
  35942. (void)mdCtx;
  35943. (void)check;
  35944. (void)checkSz;
  35945. (void)p;
  35946. #endif
  35947. #endif
  35948. (void)keyFormat;
  35949. (void)encInfo;
  35950. exit_onlycb:
  35951. #if defined(WOLFSSL_SMALL_STACK)
  35952. if (key != NULL) {
  35953. wc_ecc_free(key);
  35954. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35955. }
  35956. if (pub != NULL) {
  35957. XFREE(pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35958. }
  35959. if (out != NULL) {
  35960. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35961. }
  35962. #ifdef OPENSSL_EXTRA
  35963. if (check) {
  35964. FREE(check, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35965. }
  35966. #endif
  35967. #else
  35968. wc_ecc_free(key);
  35969. #ifdef OPENSSL_EXTRA
  35970. if (privKey)
  35971. EVP_PKEY_free(privKey);
  35972. if (pubKey)
  35973. EVP_PKEY_free(pubKey);
  35974. #endif
  35975. #endif
  35976. #endif /* HAVE_ECC */
  35977. return ret;
  35978. }
  35979. #endif
  35980. /* Example crypto dev callback function that calls software version */
  35981. static int myCryptoDevCb(int devIdArg, wc_CryptoInfo* info, void* ctx)
  35982. {
  35983. int ret = NOT_COMPILED_IN; /* return this to bypass HW and use SW */
  35984. myCryptoDevCtx* myCtx = (myCryptoDevCtx*)ctx;
  35985. if (info == NULL)
  35986. return BAD_FUNC_ARG;
  35987. #ifdef DEBUG_WOLFSSL
  35988. printf("CryptoDevCb: Algo Type %d\n", info->algo_type);
  35989. #endif
  35990. if (info->algo_type == WC_ALGO_TYPE_RNG) {
  35991. #ifndef WC_NO_RNG
  35992. /* set devId to invalid, so software is used */
  35993. info->rng.rng->devId = INVALID_DEVID;
  35994. ret = wc_RNG_GenerateBlock(info->rng.rng,
  35995. info->rng.out, info->rng.sz);
  35996. /* reset devId */
  35997. info->rng.rng->devId = devIdArg;
  35998. #endif
  35999. }
  36000. else if (info->algo_type == WC_ALGO_TYPE_SEED) {
  36001. #ifndef WC_NO_RNG
  36002. ALIGN32 static byte seed[sizeof(word32)] = { 0x00, 0x00, 0x00, 0x01 };
  36003. word32* seedWord32 = (word32*)seed;
  36004. word32 len;
  36005. /* wc_GenerateSeed is a local symbol so we need to fake the entropy. */
  36006. while (info->seed.sz > 0) {
  36007. len = (word32)sizeof(seed);
  36008. if (info->seed.sz < len)
  36009. len = info->seed.sz;
  36010. XMEMCPY(info->seed.seed, seed, sizeof(seed));
  36011. info->seed.seed += len;
  36012. info->seed.sz -= len;
  36013. (*seedWord32)++;
  36014. }
  36015. ret = 0;
  36016. #endif
  36017. }
  36018. else if (info->algo_type == WC_ALGO_TYPE_PK) {
  36019. #ifdef DEBUG_WOLFSSL
  36020. printf("CryptoDevCb: Pk Type %d\n", info->pk.type);
  36021. #endif
  36022. #ifndef NO_RSA
  36023. if (info->pk.type == WC_PK_TYPE_RSA) {
  36024. /* set devId to invalid, so software is used */
  36025. info->pk.rsa.key->devId = INVALID_DEVID;
  36026. #if defined(WOLF_CRYPTO_CB_ONLY_RSA)
  36027. #ifdef DEBUG_WOLFSSL
  36028. printf("CryptoDevCb: exampleVar %d\n", myCtx->exampleVar);
  36029. #endif
  36030. if (myCtx->exampleVar == 99) {
  36031. info->pk.rsa.key->devId = devIdArg;
  36032. return 0;
  36033. }
  36034. #endif
  36035. switch (info->pk.rsa.type) {
  36036. case RSA_PUBLIC_ENCRYPT:
  36037. case RSA_PUBLIC_DECRYPT:
  36038. /* perform software based RSA public op */
  36039. ret = wc_RsaFunction(
  36040. info->pk.rsa.in, info->pk.rsa.inLen,
  36041. info->pk.rsa.out, info->pk.rsa.outLen,
  36042. info->pk.rsa.type, info->pk.rsa.key, info->pk.rsa.rng);
  36043. break;
  36044. case RSA_PRIVATE_ENCRYPT:
  36045. case RSA_PRIVATE_DECRYPT:
  36046. /* perform software based RSA private op */
  36047. ret = wc_RsaFunction(
  36048. info->pk.rsa.in, info->pk.rsa.inLen,
  36049. info->pk.rsa.out, info->pk.rsa.outLen,
  36050. info->pk.rsa.type, info->pk.rsa.key, info->pk.rsa.rng);
  36051. break;
  36052. }
  36053. /* reset devId */
  36054. info->pk.rsa.key->devId = devIdArg;
  36055. }
  36056. #ifdef WOLFSSL_KEY_GEN
  36057. else if (info->pk.type == WC_PK_TYPE_RSA_KEYGEN) {
  36058. info->pk.rsakg.key->devId = INVALID_DEVID;
  36059. #if defined(WOLF_CRYPTO_CB_ONLY_RSA)
  36060. #ifdef DEBUG_WOLFSSL
  36061. printf("CryptoDevCb: exampleVar %d\n", myCtx->exampleVar);
  36062. #endif
  36063. if (myCtx->exampleVar == 99) {
  36064. info->pk.rsakg.key->devId = devIdArg;
  36065. return 0;
  36066. }
  36067. #endif
  36068. #ifdef HAVE_FIPS
  36069. for (;;) {
  36070. #endif
  36071. ret = wc_MakeRsaKey(info->pk.rsakg.key, info->pk.rsakg.size,
  36072. info->pk.rsakg.e, info->pk.rsakg.rng);
  36073. #ifdef HAVE_FIPS
  36074. if (ret == PRIME_GEN_E)
  36075. continue;
  36076. break;
  36077. }
  36078. #endif
  36079. /* reset devId */
  36080. info->pk.rsakg.key->devId = devIdArg;
  36081. }
  36082. #endif
  36083. #endif /* !NO_RSA */
  36084. #ifdef HAVE_ECC
  36085. if (info->pk.type == WC_PK_TYPE_EC_KEYGEN) {
  36086. /* set devId to invalid, so software is used */
  36087. info->pk.eckg.key->devId = INVALID_DEVID;
  36088. #if defined(WOLF_CRYPTO_CB_ONLY_ECC)
  36089. #ifdef DEBUG_WOLFSSL
  36090. printf("CryptoDevCb: exampleVar %d\n", myCtx->exampleVar);
  36091. #endif
  36092. if (myCtx->exampleVar == 99) {
  36093. info->pk.eckg.key->devId = devIdArg;
  36094. return 0;
  36095. }
  36096. #endif
  36097. ret = wc_ecc_make_key_ex(info->pk.eckg.rng, info->pk.eckg.size,
  36098. info->pk.eckg.key, info->pk.eckg.curveId);
  36099. /* reset devId */
  36100. info->pk.eckg.key->devId = devIdArg;
  36101. }
  36102. else if (info->pk.type == WC_PK_TYPE_ECDSA_SIGN) {
  36103. /* set devId to invalid, so software is used */
  36104. info->pk.eccsign.key->devId = INVALID_DEVID;
  36105. #if defined(WOLF_CRYPTO_CB_ONLY_ECC)
  36106. #ifdef DEBUG_WOLFSSL
  36107. printf("CryptoDevCb: exampleVar %d\n", myCtx->exampleVar);
  36108. #endif
  36109. if (myCtx->exampleVar == 99) {
  36110. info->pk.eccsign.key->devId = devIdArg;
  36111. return 0;
  36112. }
  36113. #endif
  36114. ret = wc_ecc_sign_hash(
  36115. info->pk.eccsign.in, info->pk.eccsign.inlen,
  36116. info->pk.eccsign.out, info->pk.eccsign.outlen,
  36117. info->pk.eccsign.rng, info->pk.eccsign.key);
  36118. /* reset devId */
  36119. info->pk.eccsign.key->devId = devIdArg;
  36120. }
  36121. else if (info->pk.type == WC_PK_TYPE_ECDSA_VERIFY) {
  36122. /* set devId to invalid, so software is used */
  36123. info->pk.eccverify.key->devId = INVALID_DEVID;
  36124. #if defined(WOLF_CRYPTO_CB_ONLY_ECC)
  36125. #ifdef DEBUG_WOLFSSL
  36126. printf("CryptoDevCb: exampleVar %d\n", myCtx->exampleVar);
  36127. #endif
  36128. if (myCtx->exampleVar == 99) {
  36129. info->pk.eccverify.key->devId = devIdArg;
  36130. return 0;
  36131. }
  36132. #endif
  36133. ret = wc_ecc_verify_hash(
  36134. info->pk.eccverify.sig, info->pk.eccverify.siglen,
  36135. info->pk.eccverify.hash, info->pk.eccverify.hashlen,
  36136. info->pk.eccverify.res, info->pk.eccverify.key);
  36137. /* reset devId */
  36138. info->pk.eccverify.key->devId = devIdArg;
  36139. }
  36140. else if (info->pk.type == WC_PK_TYPE_ECDH) {
  36141. /* set devId to invalid, so software is used */
  36142. info->pk.ecdh.private_key->devId = INVALID_DEVID;
  36143. #if defined(WOLF_CRYPTO_CB_ONLY_ECC)
  36144. #ifdef DEBUG_WOLFSSL
  36145. printf("CryptoDevCb: exampleVar %d\n", myCtx->exampleVar);
  36146. #endif
  36147. if (myCtx->exampleVar == 99) {
  36148. info->pk.ecdh.private_key->devId = devIdArg;
  36149. return 0;
  36150. }
  36151. #endif
  36152. ret = wc_ecc_shared_secret(
  36153. info->pk.ecdh.private_key, info->pk.ecdh.public_key,
  36154. info->pk.ecdh.out, info->pk.ecdh.outlen);
  36155. /* reset devId */
  36156. info->pk.ecdh.private_key->devId = devIdArg;
  36157. }
  36158. #endif /* HAVE_ECC */
  36159. #ifdef HAVE_CURVE25519
  36160. if (info->pk.type == WC_PK_TYPE_CURVE25519_KEYGEN) {
  36161. /* set devId to invalid, so software is used */
  36162. info->pk.curve25519kg.key->devId = INVALID_DEVID;
  36163. ret = wc_curve25519_make_key(info->pk.curve25519kg.rng,
  36164. info->pk.curve25519kg.size, info->pk.curve25519kg.key);
  36165. /* reset devId */
  36166. info->pk.curve25519kg.key->devId = devIdArg;
  36167. }
  36168. else if (info->pk.type == WC_PK_TYPE_CURVE25519) {
  36169. /* set devId to invalid, so software is used */
  36170. info->pk.curve25519.private_key->devId = INVALID_DEVID;
  36171. ret = wc_curve25519_shared_secret_ex(
  36172. info->pk.curve25519.private_key, info->pk.curve25519.public_key,
  36173. info->pk.curve25519.out, info->pk.curve25519.outlen,
  36174. info->pk.curve25519.endian);
  36175. /* reset devId */
  36176. info->pk.curve25519.private_key->devId = devIdArg;
  36177. }
  36178. #endif /* HAVE_CURVE25519 */
  36179. #ifdef HAVE_ED25519
  36180. if (info->pk.type == WC_PK_TYPE_ED25519_KEYGEN) {
  36181. /* set devId to invalid, so software is used */
  36182. info->pk.ed25519kg.key->devId = INVALID_DEVID;
  36183. ret = wc_ed25519_make_key(info->pk.ed25519kg.rng,
  36184. info->pk.ed25519kg.size, info->pk.ed25519kg.key);
  36185. /* reset devId */
  36186. info->pk.ed25519kg.key->devId = devIdArg;
  36187. }
  36188. #ifdef HAVE_ED25519_SIGN
  36189. else if (info->pk.type == WC_PK_TYPE_ED25519_SIGN) {
  36190. /* set devId to invalid, so software is used */
  36191. info->pk.ed25519sign.key->devId = INVALID_DEVID;
  36192. ret = wc_ed25519_sign_msg_ex(
  36193. info->pk.ed25519sign.in, info->pk.ed25519sign.inLen,
  36194. info->pk.ed25519sign.out, info->pk.ed25519sign.outLen,
  36195. info->pk.ed25519sign.key, info->pk.ed25519sign.type,
  36196. info->pk.ed25519sign.context, info->pk.ed25519sign.contextLen);
  36197. /* reset devId */
  36198. info->pk.ed25519sign.key->devId = devIdArg;
  36199. }
  36200. #endif
  36201. #ifdef HAVE_ED25519_VERIFY
  36202. else if (info->pk.type == WC_PK_TYPE_ED25519_VERIFY) {
  36203. /* set devId to invalid, so software is used */
  36204. info->pk.ed25519verify.key->devId = INVALID_DEVID;
  36205. ret = wc_ed25519_verify_msg_ex(
  36206. info->pk.ed25519verify.sig, info->pk.ed25519verify.sigLen,
  36207. info->pk.ed25519verify.msg, info->pk.ed25519verify.msgLen,
  36208. info->pk.ed25519verify.res, info->pk.ed25519verify.key,
  36209. info->pk.ed25519verify.type, info->pk.ed25519verify.context,
  36210. info->pk.ed25519verify.contextLen);
  36211. /* reset devId */
  36212. info->pk.ed25519verify.key->devId = devIdArg;
  36213. }
  36214. #endif
  36215. #endif /* HAVE_ED25519 */
  36216. }
  36217. else if (info->algo_type == WC_ALGO_TYPE_CIPHER) {
  36218. #if !defined(NO_AES) || !defined(NO_DES3)
  36219. #ifdef HAVE_AESGCM
  36220. if (info->cipher.type == WC_CIPHER_AES_GCM) {
  36221. if (info->cipher.enc) {
  36222. /* set devId to invalid, so software is used */
  36223. info->cipher.aesgcm_enc.aes->devId = INVALID_DEVID;
  36224. ret = wc_AesGcmEncrypt(
  36225. info->cipher.aesgcm_enc.aes,
  36226. info->cipher.aesgcm_enc.out,
  36227. info->cipher.aesgcm_enc.in,
  36228. info->cipher.aesgcm_enc.sz,
  36229. info->cipher.aesgcm_enc.iv,
  36230. info->cipher.aesgcm_enc.ivSz,
  36231. info->cipher.aesgcm_enc.authTag,
  36232. info->cipher.aesgcm_enc.authTagSz,
  36233. info->cipher.aesgcm_enc.authIn,
  36234. info->cipher.aesgcm_enc.authInSz);
  36235. /* reset devId */
  36236. info->cipher.aesgcm_enc.aes->devId = devIdArg;
  36237. }
  36238. else {
  36239. /* set devId to invalid, so software is used */
  36240. info->cipher.aesgcm_dec.aes->devId = INVALID_DEVID;
  36241. ret = wc_AesGcmDecrypt(
  36242. info->cipher.aesgcm_dec.aes,
  36243. info->cipher.aesgcm_dec.out,
  36244. info->cipher.aesgcm_dec.in,
  36245. info->cipher.aesgcm_dec.sz,
  36246. info->cipher.aesgcm_dec.iv,
  36247. info->cipher.aesgcm_dec.ivSz,
  36248. info->cipher.aesgcm_dec.authTag,
  36249. info->cipher.aesgcm_dec.authTagSz,
  36250. info->cipher.aesgcm_dec.authIn,
  36251. info->cipher.aesgcm_dec.authInSz);
  36252. /* reset devId */
  36253. info->cipher.aesgcm_dec.aes->devId = devIdArg;
  36254. }
  36255. }
  36256. #endif /* HAVE_AESGCM */
  36257. #ifdef HAVE_AES_CBC
  36258. if (info->cipher.type == WC_CIPHER_AES_CBC) {
  36259. if (info->cipher.enc) {
  36260. /* set devId to invalid, so software is used */
  36261. info->cipher.aescbc.aes->devId = INVALID_DEVID;
  36262. ret = wc_AesCbcEncrypt(
  36263. info->cipher.aescbc.aes,
  36264. info->cipher.aescbc.out,
  36265. info->cipher.aescbc.in,
  36266. info->cipher.aescbc.sz);
  36267. /* reset devId */
  36268. info->cipher.aescbc.aes->devId = devIdArg;
  36269. }
  36270. else {
  36271. /* set devId to invalid, so software is used */
  36272. info->cipher.aescbc.aes->devId = INVALID_DEVID;
  36273. ret = wc_AesCbcDecrypt(
  36274. info->cipher.aescbc.aes,
  36275. info->cipher.aescbc.out,
  36276. info->cipher.aescbc.in,
  36277. info->cipher.aescbc.sz);
  36278. /* reset devId */
  36279. info->cipher.aescbc.aes->devId = devIdArg;
  36280. }
  36281. }
  36282. #endif /* HAVE_AES_CBC */
  36283. #if defined(HAVE_AES_ECB) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  36284. if (info->cipher.type == WC_CIPHER_AES_ECB) {
  36285. if (info->cipher.enc) {
  36286. /* set devId to invalid, so software is used */
  36287. info->cipher.aesecb.aes->devId = INVALID_DEVID;
  36288. ret = wc_AesEcbEncrypt(
  36289. info->cipher.aesecb.aes,
  36290. info->cipher.aesecb.out,
  36291. info->cipher.aesecb.in,
  36292. info->cipher.aesecb.sz);
  36293. /* reset devId */
  36294. info->cipher.aesecb.aes->devId = devIdArg;
  36295. }
  36296. else {
  36297. /* set devId to invalid, so software is used */
  36298. info->cipher.aesecb.aes->devId = INVALID_DEVID;
  36299. ret = wc_AesEcbDecrypt(
  36300. info->cipher.aesecb.aes,
  36301. info->cipher.aesecb.out,
  36302. info->cipher.aesecb.in,
  36303. info->cipher.aesecb.sz);
  36304. /* reset devId */
  36305. info->cipher.aesecb.aes->devId = devIdArg;
  36306. }
  36307. }
  36308. #endif /* HAVE_AES_ECB */
  36309. #if defined(WOLFSSL_AES_COUNTER) && !defined(HAVE_FIPS) && \
  36310. !defined(HAVE_SELFTEST)
  36311. if (info->cipher.type == WC_CIPHER_AES_CTR) {
  36312. /* set devId to invalid, so software is used */
  36313. info->cipher.aesctr.aes->devId = INVALID_DEVID;
  36314. ret = wc_AesCtrEncrypt(
  36315. info->cipher.aesctr.aes,
  36316. info->cipher.aesctr.out,
  36317. info->cipher.aesctr.in,
  36318. info->cipher.aesctr.sz);
  36319. /* reset devId */
  36320. info->cipher.aesctr.aes->devId = devIdArg;
  36321. }
  36322. #endif /* WOLFSSL_AES_COUNTER */
  36323. #if defined(HAVE_AESCCM) && defined(WOLFSSL_AES_128)
  36324. if (info->cipher.type == WC_CIPHER_AES_CCM) {
  36325. if (info->cipher.enc) {
  36326. /* set devId to invalid, so software is used */
  36327. info->cipher.aesccm_enc.aes->devId = INVALID_DEVID;
  36328. ret = wc_AesCcmEncrypt(
  36329. info->cipher.aesccm_enc.aes,
  36330. info->cipher.aesccm_enc.out,
  36331. info->cipher.aesccm_enc.in,
  36332. info->cipher.aesccm_enc.sz,
  36333. info->cipher.aesccm_enc.nonce,
  36334. info->cipher.aesccm_enc.nonceSz,
  36335. info->cipher.aesccm_enc.authTag,
  36336. info->cipher.aesccm_enc.authTagSz,
  36337. info->cipher.aesccm_enc.authIn,
  36338. info->cipher.aesccm_enc.authInSz);
  36339. /* reset devId */
  36340. info->cipher.aesccm_enc.aes->devId = devIdArg;
  36341. }
  36342. else {
  36343. /* set devId to invalid, so software is used */
  36344. info->cipher.aesccm_dec.aes->devId = INVALID_DEVID;
  36345. ret = wc_AesCcmDecrypt(
  36346. info->cipher.aesccm_dec.aes,
  36347. info->cipher.aesccm_dec.out,
  36348. info->cipher.aesccm_dec.in,
  36349. info->cipher.aesccm_dec.sz,
  36350. info->cipher.aesccm_dec.nonce,
  36351. info->cipher.aesccm_dec.nonceSz,
  36352. info->cipher.aesccm_dec.authTag,
  36353. info->cipher.aesccm_dec.authTagSz,
  36354. info->cipher.aesccm_dec.authIn,
  36355. info->cipher.aesccm_dec.authInSz);
  36356. /* reset devId */
  36357. info->cipher.aesccm_dec.aes->devId = devIdArg;
  36358. }
  36359. }
  36360. #endif
  36361. #ifndef NO_DES3
  36362. if (info->cipher.type == WC_CIPHER_DES3) {
  36363. if (info->cipher.enc) {
  36364. /* set devId to invalid, so software is used */
  36365. info->cipher.des3.des->devId = INVALID_DEVID;
  36366. ret = wc_Des3_CbcEncrypt(
  36367. info->cipher.des3.des,
  36368. info->cipher.des3.out,
  36369. info->cipher.des3.in,
  36370. info->cipher.des3.sz);
  36371. /* reset devId */
  36372. info->cipher.des3.des->devId = devIdArg;
  36373. }
  36374. else {
  36375. /* set devId to invalid, so software is used */
  36376. info->cipher.des3.des->devId = INVALID_DEVID;
  36377. ret = wc_Des3_CbcDecrypt(
  36378. info->cipher.des3.des,
  36379. info->cipher.des3.out,
  36380. info->cipher.des3.in,
  36381. info->cipher.des3.sz);
  36382. /* reset devId */
  36383. info->cipher.des3.des->devId = devIdArg;
  36384. }
  36385. }
  36386. #endif /* !NO_DES3 */
  36387. #endif /* !NO_AES || !NO_DES3 */
  36388. }
  36389. #if !defined(NO_SHA) || !defined(NO_SHA256) || \
  36390. defined(WOLFSSL_SHA384) || defined(WOLFSSL_SHA512)
  36391. else if (info->algo_type == WC_ALGO_TYPE_HASH) {
  36392. #if !defined(NO_SHA)
  36393. if (info->hash.type == WC_HASH_TYPE_SHA) {
  36394. if (info->hash.sha1 == NULL)
  36395. return NOT_COMPILED_IN;
  36396. /* set devId to invalid, so software is used */
  36397. info->hash.sha1->devId = INVALID_DEVID;
  36398. if (info->hash.in != NULL) {
  36399. ret = wc_ShaUpdate(
  36400. info->hash.sha1,
  36401. info->hash.in,
  36402. info->hash.inSz);
  36403. }
  36404. if (info->hash.digest != NULL) {
  36405. ret = wc_ShaFinal(
  36406. info->hash.sha1,
  36407. info->hash.digest);
  36408. }
  36409. /* reset devId */
  36410. info->hash.sha1->devId = devIdArg;
  36411. }
  36412. else
  36413. #endif
  36414. #if !defined(NO_SHA256)
  36415. if (info->hash.type == WC_HASH_TYPE_SHA256) {
  36416. if (info->hash.sha256 == NULL)
  36417. return NOT_COMPILED_IN;
  36418. /* set devId to invalid, so software is used */
  36419. info->hash.sha256->devId = INVALID_DEVID;
  36420. if (info->hash.in != NULL) {
  36421. ret = wc_Sha256Update(
  36422. info->hash.sha256,
  36423. info->hash.in,
  36424. info->hash.inSz);
  36425. }
  36426. if (info->hash.digest != NULL) {
  36427. ret = wc_Sha256Final(
  36428. info->hash.sha256,
  36429. info->hash.digest);
  36430. }
  36431. /* reset devId */
  36432. info->hash.sha256->devId = devIdArg;
  36433. }
  36434. else
  36435. #endif
  36436. #ifdef WOLFSSL_SHA384
  36437. if (info->hash.type == WC_HASH_TYPE_SHA384) {
  36438. if (info->hash.sha384 == NULL)
  36439. return NOT_COMPILED_IN;
  36440. #ifndef NO_SHA2_CRYPTO_CB
  36441. /* set devId to invalid, so software is used */
  36442. info->hash.sha384->devId = INVALID_DEVID;
  36443. #endif
  36444. if (info->hash.in != NULL) {
  36445. ret = wc_Sha384Update(
  36446. info->hash.sha384,
  36447. info->hash.in,
  36448. info->hash.inSz);
  36449. }
  36450. if (info->hash.digest != NULL) {
  36451. ret = wc_Sha384Final(
  36452. info->hash.sha384,
  36453. info->hash.digest);
  36454. }
  36455. #ifndef NO_SHA2_CRYPTO_CB
  36456. /* reset devId */
  36457. info->hash.sha384->devId = devIdArg;
  36458. #endif
  36459. }
  36460. else
  36461. #endif
  36462. #ifdef WOLFSSL_SHA512
  36463. if (info->hash.type == WC_HASH_TYPE_SHA512) {
  36464. if (info->hash.sha512 == NULL)
  36465. return NOT_COMPILED_IN;
  36466. #ifndef NO_SHA2_CRYPTO_CB
  36467. /* set devId to invalid, so software is used */
  36468. info->hash.sha512->devId = INVALID_DEVID;
  36469. #endif
  36470. if (info->hash.in != NULL) {
  36471. ret = wc_Sha512Update(
  36472. info->hash.sha512,
  36473. info->hash.in,
  36474. info->hash.inSz);
  36475. }
  36476. if (info->hash.digest != NULL) {
  36477. ret = wc_Sha512Final(
  36478. info->hash.sha512,
  36479. info->hash.digest);
  36480. }
  36481. #ifndef NO_SHA2_CRYPTO_CB
  36482. /* reset devId */
  36483. info->hash.sha512->devId = devIdArg;
  36484. #endif
  36485. }
  36486. else
  36487. #endif
  36488. {
  36489. }
  36490. }
  36491. #endif /* !NO_SHA || !NO_SHA256 */
  36492. #ifndef NO_HMAC
  36493. else if (info->algo_type == WC_ALGO_TYPE_HMAC) {
  36494. if (info->hmac.hmac == NULL)
  36495. return NOT_COMPILED_IN;
  36496. /* set devId to invalid, so software is used */
  36497. info->hmac.hmac->devId = INVALID_DEVID;
  36498. if (info->hash.in != NULL) {
  36499. ret = wc_HmacUpdate(
  36500. info->hmac.hmac,
  36501. info->hmac.in,
  36502. info->hmac.inSz);
  36503. }
  36504. else if (info->hash.digest != NULL) {
  36505. ret = wc_HmacFinal(
  36506. info->hmac.hmac,
  36507. info->hmac.digest);
  36508. }
  36509. /* reset devId */
  36510. info->hmac.hmac->devId = devIdArg;
  36511. }
  36512. #endif
  36513. (void)devIdArg;
  36514. (void)myCtx;
  36515. return ret;
  36516. }
  36517. WOLFSSL_TEST_SUBROUTINE int cryptocb_test(void)
  36518. {
  36519. int ret = 0;
  36520. myCryptoDevCtx myCtx;
  36521. /* example data for callback */
  36522. myCtx.exampleVar = 1;
  36523. /* set devId to something other than INVALID_DEVID */
  36524. devId = 1;
  36525. ret = wc_CryptoCb_RegisterDevice(devId, myCryptoDevCb, &myCtx);
  36526. #ifndef WC_NO_RNG
  36527. if (ret == 0)
  36528. ret = random_test();
  36529. #endif /* WC_NO_RNG */
  36530. #if !defined(NO_RSA)
  36531. PRIVATE_KEY_UNLOCK();
  36532. if (ret == 0)
  36533. ret = rsa_test();
  36534. PRIVATE_KEY_LOCK();
  36535. #endif
  36536. #if defined(WOLF_CRYPTO_CB_ONLY_RSA)
  36537. PRIVATE_KEY_UNLOCK();
  36538. if (ret == 0)
  36539. ret = rsa_onlycb_test(&myCtx);
  36540. PRIVATE_KEY_LOCK();
  36541. #endif
  36542. #if defined(HAVE_ECC)
  36543. PRIVATE_KEY_UNLOCK();
  36544. if (ret == 0)
  36545. ret = ecc_test();
  36546. PRIVATE_KEY_LOCK();
  36547. #endif
  36548. #if defined(WOLF_CRYPTO_CB_ONLY_ECC)
  36549. PRIVATE_KEY_UNLOCK();
  36550. if (ret == 0)
  36551. ret = ecc_onlycb_test(&myCtx);
  36552. PRIVATE_KEY_LOCK();
  36553. #endif
  36554. #ifdef HAVE_ED25519
  36555. if (ret == 0)
  36556. ret = ed25519_test();
  36557. #endif
  36558. #ifdef HAVE_CURVE25519
  36559. if (ret == 0)
  36560. ret = curve25519_test();
  36561. #endif
  36562. #ifndef NO_AES
  36563. #ifdef HAVE_AESGCM
  36564. if (ret == 0)
  36565. ret = aesgcm_test();
  36566. #endif
  36567. #ifdef HAVE_AES_CBC
  36568. if (ret == 0)
  36569. ret = aes_test();
  36570. #endif
  36571. #if defined(HAVE_AESCCM) && defined(WOLFSSL_AES_128)
  36572. if (ret == 0)
  36573. ret = aesccm_test();
  36574. #endif
  36575. #endif /* !NO_AES */
  36576. #ifndef NO_DES3
  36577. if (ret == 0)
  36578. ret = des3_test();
  36579. #endif /* !NO_DES3 */
  36580. #ifndef NO_SHA
  36581. if (ret == 0)
  36582. ret = sha_test();
  36583. #endif
  36584. #ifndef NO_SHA256
  36585. if (ret == 0)
  36586. ret = sha256_test();
  36587. #endif
  36588. #ifdef WOLFSSL_SHA384
  36589. if (ret == 0)
  36590. ret = sha384_test();
  36591. #endif
  36592. #ifdef WOLFSSL_SHA512
  36593. if (ret == 0)
  36594. ret = sha512_test();
  36595. #endif
  36596. #ifndef NO_HMAC
  36597. #ifndef NO_SHA
  36598. if (ret == 0)
  36599. ret = hmac_sha_test();
  36600. #endif
  36601. #ifndef NO_SHA256
  36602. if (ret == 0)
  36603. ret = hmac_sha256_test();
  36604. #endif
  36605. #endif
  36606. #ifndef NO_PWDBASED
  36607. #if defined(HAVE_PBKDF2) && !defined(NO_SHA256)
  36608. if (ret == 0)
  36609. ret = pbkdf2_test();
  36610. #endif
  36611. #endif
  36612. #if defined(WOLFSSL_CMAC) && !defined(NO_AES)
  36613. if (ret == 0)
  36614. ret = cmac_test();
  36615. #endif
  36616. /* reset devId */
  36617. devId = INVALID_DEVID;
  36618. return ret;
  36619. }
  36620. #endif /* WOLF_CRYPTO_CB */
  36621. #ifdef WOLFSSL_CERT_PIV
  36622. WOLFSSL_TEST_SUBROUTINE int certpiv_test(void)
  36623. {
  36624. int ret;
  36625. wc_CertPIV piv;
  36626. /* Template for Identiv PIV cert, nonce and signature */
  36627. WOLFSSL_SMALL_STACK_STATIC const byte pivCertIdentiv[] = {
  36628. 0x0A, 0x0B,
  36629. 0x53, 0x09, /* NIST PIV Cert */
  36630. 0x70, 0x02, /* Certificate */
  36631. 0x30, 0x00,
  36632. 0x71, 0x01, 0x05, /* Cert Info */
  36633. 0xFE, 0x00, /* Error Detection */
  36634. 0x0B, 0x01, 0x00, /* Nonce */
  36635. 0x0C, 0x01, 0x00, /* Signed Nonce */
  36636. };
  36637. /* PIV certificate data including certificate, info and error dectection. */
  36638. WOLFSSL_SMALL_STACK_STATIC const byte pivCert[] = {
  36639. 0x53, 0x09, /* NIST PIV Cert */
  36640. 0x70, 0x02, /* Certificate */
  36641. 0x30, 0x00,
  36642. 0x71, 0x01, 0x04, /* Cert Info */
  36643. 0xFE, 0x00, /* Error Detection */
  36644. };
  36645. XMEMSET(&piv, 0, sizeof(piv));
  36646. /* Test with Identiv 0x0A, 0x0B and 0x0C markers */
  36647. ret = wc_ParseCertPIV(&piv, pivCertIdentiv, sizeof(pivCertIdentiv));
  36648. if (ret != 0) {
  36649. return -14000;
  36650. }
  36651. if (!piv.isIdentiv) {
  36652. return -14001;
  36653. }
  36654. if ((piv.cert == NULL) || (piv.certSz != 2)) {
  36655. return -14002;
  36656. }
  36657. if ((piv.certErrDet == NULL) || (piv.certErrDetSz != 0)) {
  36658. return -14003;
  36659. }
  36660. if ((piv.compression != ASN_PIV_CERT_INFO_GZIP)) {
  36661. return -14004;
  36662. }
  36663. if (!piv.isX509) {
  36664. return -14005;
  36665. }
  36666. if ((piv.nonce == NULL) || (piv.nonceSz != 1)) {
  36667. return -14006;
  36668. }
  36669. if ((piv.signedNonce == NULL) || (piv.signedNonceSz != 1)) {
  36670. return -14007;
  36671. }
  36672. XMEMSET(&piv, 0, sizeof(piv));
  36673. /* Test with NIST PIV format */
  36674. ret = wc_ParseCertPIV(&piv, pivCert, sizeof(pivCert));
  36675. if (ret != 0) {
  36676. return -14010;
  36677. }
  36678. if (piv.isIdentiv) {
  36679. return -14011;
  36680. }
  36681. if ((piv.cert == NULL) || (piv.certSz != 2)) {
  36682. return -14012;
  36683. }
  36684. if ((piv.certErrDet == NULL) || (piv.certErrDetSz != 0)) {
  36685. return -14013;
  36686. }
  36687. if ((piv.compression != 0)) {
  36688. return -14014;
  36689. }
  36690. if (!piv.isX509) {
  36691. return -14015;
  36692. }
  36693. return ret;
  36694. }
  36695. #endif /* WOLFSSL_CERT_PIV */
  36696. #if !defined(NO_ASN) && !defined(NO_ASN_TIME)
  36697. static time_t time_cb(time_t* t)
  36698. {
  36699. if (t != NULL) {
  36700. *t = 99;
  36701. }
  36702. return 99;
  36703. }
  36704. WOLFSSL_TEST_SUBROUTINE int time_test(void)
  36705. {
  36706. time_t t;
  36707. if (wc_SetTimeCb(time_cb) != 0)
  36708. return -15000;
  36709. t = wc_Time(NULL);
  36710. if (t != 99)
  36711. return -15001;
  36712. if (wc_GetTime(&t, sizeof(time_t)) != 0)
  36713. return -15002;
  36714. if (t != 99)
  36715. return -15003;
  36716. if (wc_SetTimeCb(NULL) != 0)
  36717. return -15004;
  36718. return 0;
  36719. }
  36720. #endif
  36721. #ifdef WOLFSSL_AES_SIV
  36722. typedef struct {
  36723. const byte key[33];
  36724. word32 keySz;
  36725. const byte nonce[49];
  36726. word32 nonceSz;
  36727. const byte assoc[81];
  36728. word32 assocSz;
  36729. const byte plaintext[83];
  36730. word32 plaintextSz;
  36731. const byte siv[AES_BLOCK_SIZE+1];
  36732. const byte ciphertext[82];
  36733. word32 ciphertextSz;
  36734. } AesSivTestVector;
  36735. #define AES_SIV_TEST_VECTORS 7
  36736. WOLFSSL_TEST_SUBROUTINE int aes_siv_test(void)
  36737. {
  36738. /* These test vectors come from chrony 4.1's SIV unit tests. */
  36739. WOLFSSL_SMALL_STACK_STATIC const AesSivTestVector testVectors[AES_SIV_TEST_VECTORS] = {
  36740. { "\x01\x23\x45\x67\x89\xab\xcd\xef\xf0\x12\x34\x56\x78\x9a\xbc\xde"
  36741. "\xef\x01\x23\x45\x67\x89\xab\xcd\xde\xf0\x12\x34\x56\x78\x9a\xbc", 32,
  36742. "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f", 16,
  36743. "", 0,
  36744. "", 0,
  36745. "\x22\x3e\xb5\x94\xe0\xe0\x25\x4b\x00\x25\x8e\x21\x9a\x1c\xa4\x21",
  36746. "", 0
  36747. },
  36748. { "\x01\x23\x45\x67\x89\xab\xcd\xef\xf0\x12\x34\x56\x78\x9a\xbc\xde"
  36749. "\xef\x01\x23\x45\x67\x89\xab\xcd\xde\xf0\x12\x34\x56\x78\x9a\xbc", 32,
  36750. "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f", 16,
  36751. "\x4c\x9d\x4f\xca\xed\x8a\xe2\xba\xad\x3f\x3e\xa6\xe9\x3c\x8c\x8b", 16,
  36752. "", 0,
  36753. "\xd7\x20\x19\x89\xc6\xdb\xc6\xd6\x61\xfc\x62\xbc\x86\x5e\xee\xef",
  36754. "", 0
  36755. },
  36756. { "\x01\x23\x45\x67\x89\xab\xcd\xef\xf0\x12\x34\x56\x78\x9a\xbc\xde"
  36757. "\xef\x01\x23\x45\x67\x89\xab\xcd\xde\xf0\x12\x34\x56\x78\x9a\xbc", 32,
  36758. "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f", 16,
  36759. "", 0,
  36760. "\x4c\x9d\x4f\xca\xed\x8a\xe2\xba\xad\x3f\x3e\xa6\xe9\x3c\x8c\x8b", 16,
  36761. "\xb6\xc1\x60\xe9\xc2\xfd\x2a\xe8\xde\xc5\x36\x8b\x2a\x33\xed\xe1",
  36762. "\x14\xff\xb3\x97\x34\x5c\xcb\xe4\x4a\xa4\xde\xac\xd9\x36\x90\x46", 16
  36763. },
  36764. { "\x01\x23\x45\x67\x89\xab\xcd\xef\xf0\x12\x34\x56\x78\x9a\xbc\xde"
  36765. "\xef\x01\x23\x45\x67\x89\xab\xcd\xde\xf0\x12\x34\x56\x78\x9a\xbc", 32,
  36766. "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e", 15,
  36767. "\x4c\x9d\x4f\xca\xed\x8a\xe2\xba\xad\x3f\x3e\xa6\xe9\x3c\x8c", 15,
  36768. "\xba\x99\x79\x31\x23\x7e\x3c\x53\x58\x7e\xd4\x93\x02\xab\xe4", 15,
  36769. "\x03\x8c\x41\x51\xba\x7a\x8f\x77\x6e\x56\x31\x99\x42\x0b\xc7\x03",
  36770. "\xe7\x6c\x67\xc9\xda\xb7\x0d\x5b\x44\x06\x26\x5a\xd0\xd2\x3b", 15
  36771. },
  36772. { "\x01\x23\x45\x67\x89\xab\xcd\xef\xf0\x12\x34\x56\x78\x9a\xbc\xde"
  36773. "\xef\x01\x23\x45\x67\x89\xab\xcd\xde\xf0\x12\x34\x56\x78\x9a\xbc", 32,
  36774. "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f", 16,
  36775. "\x4c\x9d\x4f\xca\xed\x8a\xe2\xba\xad\x3f\x3e\xa6\xe9\x3c\x8c\x8b", 16,
  36776. "\xba\x99\x79\x31\x23\x7e\x3c\x53\x58\x7e\xd4\x93\x02\xab\xe4\xa7", 16,
  36777. "\x5c\x05\x23\x65\xf4\x57\x0a\xa0\xfb\x38\x3e\xce\x9b\x75\x85\xeb",
  36778. "\x68\x85\x19\x36\x0c\x7c\x48\x11\x40\xcb\x9b\x57\x9a\x0e\x65\x32", 16
  36779. },
  36780. { "\x01\x23\x45\x67\x89\xab\xcd\xef\xf0\x12\x34\x56\x78\x9a\xbc\xde"
  36781. "\xef\x01\x23\x45\x67\x89\xab\xcd\xde\xf0\x12\x34\x56\x78\x9a\xbc", 32,
  36782. "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f"
  36783. "\xd5", 17,
  36784. "\x4c\x9d\x4f\xca\xed\x8a\xe2\xba\xad\x3f\x3e\xa6\xe9\x3c\x8c\x8b"
  36785. "\xa0", 17,
  36786. "\xba\x99\x79\x31\x23\x7e\x3c\x53\x58\x7e\xd4\x93\x02\xab\xe4\xa7"
  36787. "\x08", 17,
  36788. "\xaf\x58\x4b\xe7\x82\x1e\x96\x19\x29\x91\x25\xe0\xdd\x80\x3b\x49",
  36789. "\xa5\x11\xcd\xb6\x08\xf3\x76\xa0\xb6\xfa\x15\x82\xf3\x95\xe1\xeb"
  36790. "\xbd", 17
  36791. },
  36792. { "\x01\x23\x45\x67\x89\xab\xcd\xef\xf0\x12\x34\x56\x78\x9a\xbc\xde"
  36793. "\xef\x01\x23\x45\x67\x89\xab\xcd\xde\xf0\x12\x34\x56\x78\x9a\xbc", 32,
  36794. "\xb0\x5a\x1b\xc7\x56\xe7\xb6\x2c\xb4\x85\xe5\x56\xa5\x28\xc0\x6c"
  36795. "\x2f\x3b\x0b\x9d\x1a\x0c\xdf\x69\x47\xe0\xcc\xc0\x87\xaa\x5c\x09"
  36796. "\x98\x48\x8d\x6a\x8e\x1e\x05\xd7\x8b\x68\x74\x83\xb5\x1d\xf1\x2c", 48,
  36797. "\xe5\x8b\xd2\x6a\x30\xc5\xc5\x61\xcc\xbd\x7c\x27\xbf\xfe\xf9\x06"
  36798. "\x00\x5b\xd7\xfc\x11\x0b\xcf\x16\x61\xef\xac\x05\xa7\xaf\xec\x27"
  36799. "\x41\xc8\x5e\x9e\x0d\xf9\x2f\xaf\x20\x79\x17\xe5\x17\x91\x2a\x27"
  36800. "\x34\x1c\xbc\xaf\xeb\xef\x7f\x52\xe7\x1e\x4c\x2a\xca\xbd\x2b\xbe"
  36801. "\x34\xd6\xfb\x69\xd3\x3e\x49\x59\x60\xb4\x26\xc9\xb8\xce\xba", 79,
  36802. "\x6c\xe7\xcf\x7e\xab\x7b\xa0\xe1\xa7\x22\xcb\x88\xde\x5e\x42\xd2"
  36803. "\xec\x79\xe0\xa2\xcf\x5f\x0f\x6f\x6b\x89\x57\xcd\xae\x17\xd4\xc2"
  36804. "\xf3\x1b\xa2\xa8\x13\x78\x23\x2f\x83\xa8\xd4\x0c\xc0\xd2\xf3\x99"
  36805. "\xae\x81\xa1\xca\x5b\x5f\x45\xa6\x6f\x0c\x8a\xf3\xd4\x67\x40\x81"
  36806. "\x26\xe2\x01\x86\xe8\x5a\xd5\xf8\x58\x80\x9f\x56\xaa\x76\x96\xbf"
  36807. "\x31", 81,
  36808. "\x9a\x06\x33\xe0\xee\x00\x6a\x9b\xc8\x20\xd5\xe2\xc2\xed\xb5\x75",
  36809. "\xfa\x9e\x42\x2a\x31\x6b\xda\xca\xaa\x7d\x31\x8b\x84\x7a\xb8\xd7"
  36810. "\x8a\x81\x25\x64\xed\x41\x9b\xa9\x77\x10\xbd\x05\x0c\x4e\xc5\x31"
  36811. "\x0c\xa2\x86\xec\x8a\x94\xc8\x24\x23\x3c\x13\xee\xa5\x51\xc9\xdf"
  36812. "\x48\xc9\x55\xc5\x2f\x40\x73\x3f\x98\xbb\x8d\x69\x78\x46\x64\x17"
  36813. "\x8d\x49\x2f\x14\x62\xa4\x7c\x2a\x57\x38\x87\xce\xc6\x72\xd3\x5c"
  36814. "\xa1", 81
  36815. }};
  36816. int i;
  36817. byte computedCiphertext[82];
  36818. byte computedPlaintext[82];
  36819. byte siv[AES_BLOCK_SIZE];
  36820. int rc = 0;
  36821. for (i = 0; i < AES_SIV_TEST_VECTORS; ++i) {
  36822. rc = wc_AesSivEncrypt(testVectors[i].key, testVectors[i].keySz,
  36823. testVectors[i].assoc, testVectors[i].assocSz,
  36824. testVectors[i].nonce, testVectors[i].nonceSz,
  36825. testVectors[i].plaintext,
  36826. testVectors[i].plaintextSz, siv,
  36827. computedCiphertext);
  36828. if (rc != 0) {
  36829. return -16000;
  36830. }
  36831. rc = XMEMCMP(siv, testVectors[i].siv, AES_BLOCK_SIZE);
  36832. if (rc != 0) {
  36833. return -16001;
  36834. }
  36835. rc = XMEMCMP(computedCiphertext, testVectors[i].ciphertext,
  36836. testVectors[i].ciphertextSz);
  36837. if (rc != 0) {
  36838. return -16002;
  36839. }
  36840. rc = wc_AesSivDecrypt(testVectors[i].key, testVectors[i].keySz,
  36841. testVectors[i].assoc, testVectors[i].assocSz,
  36842. testVectors[i].nonce, testVectors[i].nonceSz,
  36843. computedCiphertext, testVectors[i].plaintextSz,
  36844. siv, computedPlaintext);
  36845. if (rc != 0) {
  36846. return -16003;
  36847. }
  36848. rc = XMEMCMP(computedPlaintext, testVectors[i].plaintext,
  36849. testVectors[i].plaintextSz);
  36850. if (rc != 0) {
  36851. return -16004;
  36852. }
  36853. }
  36854. return 0;
  36855. }
  36856. #endif
  36857. #undef ERROR_OUT
  36858. #else
  36859. #ifndef NO_MAIN_DRIVER
  36860. int main() { return 0; }
  36861. #endif
  36862. #endif /* NO_CRYPT_TEST */